1 /****************************************************************************** 2 * 3 * Copyright (C) 2015 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ***************************************************************************** 18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore 19 */ 20 21 /** Includes */ 22 #include <stdio.h> 23 #include <string.h> 24 #include "irc_datatypes.h" 25 #include "irc_mem_req_and_acq.h" 26 #include "irc_common.h" 27 #include "irc_cntrl_param.h" 28 #include "irc_fixed_point_error_bits.h" 29 #include "irc_rd_model.h" 30 #include "irc_est_sad.h" 31 #include "irc_picture_type.h" 32 #include "irc_bit_allocation.h" 33 #include "irc_trace_support.h" 34 35 /** Macros **/ 36 #define MIN(x,y) ((x) < (y))? (x) : (y) 37 38 /* State structure for bit allocation */ 39 typedef struct 40 { 41 /* using var_q number as it can cross 31 bits for large intra frameinterval */ 42 number_t vq_rem_bits_in_period; 43 44 /* Storing inputs */ 45 WORD32 i4_tot_frms_in_gop; 46 47 WORD32 i4_num_intra_frm_interval; 48 49 WORD32 i4_bits_per_frm; 50 51 } rem_bit_in_prd_t; 52 53 typedef struct bit_allocation_t 54 { 55 rem_bit_in_prd_t s_rbip; 56 57 /* A universal constant giving the relative complexity between pictures */ 58 WORD32 i2_K[MAX_PIC_TYPE]; 59 60 /* To get a estimate of the header bits consumed */ 61 WORD32 i4_prev_frm_header_bits[MAX_PIC_TYPE]; 62 63 WORD32 i4_bits_per_frm; 64 65 WORD32 i4_num_gops_in_period; 66 67 /* Num gops as set by rate control module */ 68 WORD32 i4_actual_num_gops_in_period; 69 70 number_t vq_saved_bits; 71 72 WORD32 i4_max_bits_per_frm[MAX_NUM_DRAIN_RATES]; 73 74 WORD32 i4_min_bits_per_frm; 75 76 /* Error bits module */ 77 error_bits_handle ps_error_bits; 78 79 /* Storing frame rate */ 80 WORD32 i4_frame_rate; 81 82 WORD32 i4_bit_rate; 83 84 WORD32 ai4_peak_bit_rate[MAX_NUM_DRAIN_RATES]; 85 86 } bit_allocation_t; 87 88 static WORD32 get_number_of_frms_in_a_gop(pic_handling_handle ps_pic_handling) 89 { 90 WORD32 i4_tot_frms_in_gop = 0, i; 91 WORD32 ai4_frms_in_gop[MAX_PIC_TYPE]; 92 93 /* Query the pic_handling struct for the rem frames in the period */ 94 irc_pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_gop); 95 96 /* Get the total frms in the gop */ 97 i4_tot_frms_in_gop = 0; 98 for(i = 0; i < MAX_PIC_TYPE; i++) 99 { 100 i4_tot_frms_in_gop += ai4_frms_in_gop[i]; 101 } 102 return (i4_tot_frms_in_gop); 103 } 104 105 static void init_rbip(rem_bit_in_prd_t *ps_rbip, 106 pic_handling_handle ps_pic_handling, 107 WORD32 i4_bits_per_frm, 108 WORD32 i4_num_intra_frm_interval) 109 { 110 WORD32 i4_tot_frms_in_gop = get_number_of_frms_in_a_gop(ps_pic_handling); 111 112 /* rem_bits_in_period = bits_per_frm * tot_frms_in_gop * num_intra_frm_interval */ 113 { 114 number_t vq_bits_per_frm, vq_tot_frms_in_gop, vq_num_intra_frm_interval; 115 number_t *pvq_rem_bits_in_period = &ps_rbip->vq_rem_bits_in_period; 116 117 SET_VAR_Q(vq_bits_per_frm, i4_bits_per_frm, 0); 118 SET_VAR_Q(vq_tot_frms_in_gop, i4_tot_frms_in_gop, 0); 119 SET_VAR_Q(vq_num_intra_frm_interval, i4_num_intra_frm_interval, 0); 120 121 /* rem_bits_in_period = bits_per_frm * tot_frms_in_gop */ 122 mult32_var_q(vq_bits_per_frm, vq_tot_frms_in_gop, 123 pvq_rem_bits_in_period); 124 125 /* rem_bits_in_period *= num_intra_frm_interval */ 126 mult32_var_q(vq_num_intra_frm_interval, pvq_rem_bits_in_period[0], 127 pvq_rem_bits_in_period); 128 } 129 130 /* 131 * Store the total number of frames in GOP value which is 132 * used from module A 133 */ 134 ps_rbip->i4_tot_frms_in_gop = i4_tot_frms_in_gop; 135 ps_rbip->i4_num_intra_frm_interval = i4_num_intra_frm_interval; 136 ps_rbip->i4_bits_per_frm = i4_bits_per_frm; 137 } 138 139 static void check_update_rbip(rem_bit_in_prd_t *ps_rbip, 140 pic_handling_handle ps_pic_handling) 141 { 142 /* 143 * NOTE: Intra frame interval changes after the first I frame that is 144 * encoded in a GOP 145 */ 146 WORD32 i4_new_tot_frms_in_gop = get_number_of_frms_in_a_gop( 147 ps_pic_handling); 148 149 if(i4_new_tot_frms_in_gop != ps_rbip->i4_tot_frms_in_gop) 150 { 151 WORD32 i4_rem_frames_in_period = 152 ps_rbip->i4_num_intra_frm_interval 153 * (i4_new_tot_frms_in_gop 154 - ps_rbip->i4_tot_frms_in_gop); 155 156 number_t vq_rem_frms_in_period, s_bits_per_frm, vq_delta_bits_in_period; 157 158 SET_VAR_Q(vq_rem_frms_in_period, i4_rem_frames_in_period, 0); 159 SET_VAR_Q(s_bits_per_frm, ps_rbip->i4_bits_per_frm, 0); 160 161 /* delta_bits_in_period = bits_per_frm * rem_frms_in_period */ 162 mult32_var_q(s_bits_per_frm, vq_rem_frms_in_period, 163 &vq_delta_bits_in_period); 164 165 /* rem_bits_in_period += delta_bits_in_period */ 166 add32_var_q(vq_delta_bits_in_period, ps_rbip->vq_rem_bits_in_period, 167 &ps_rbip->vq_rem_bits_in_period); 168 } 169 /* Updated the new values */ 170 ps_rbip->i4_tot_frms_in_gop = i4_new_tot_frms_in_gop; 171 } 172 173 static void irc_ba_update_rbip(rem_bit_in_prd_t *ps_rbip, 174 pic_handling_handle ps_pic_handling, 175 WORD32 i4_num_of_bits) 176 { 177 number_t vq_num_bits; 178 179 check_update_rbip(ps_rbip, ps_pic_handling); 180 181 /* rem_bits_in_period += num_of_bits */ 182 SET_VAR_Q(vq_num_bits, i4_num_of_bits, 0); 183 add32_var_q(vq_num_bits, ps_rbip->vq_rem_bits_in_period, 184 &ps_rbip->vq_rem_bits_in_period); 185 } 186 187 static void irc_ba_change_rbip(rem_bit_in_prd_t *ps_rbip, 188 pic_handling_handle ps_pic_handling, 189 WORD32 i4_new_bits_per_frm, 190 WORD32 i4_new_num_intra_frm_interval) 191 { 192 WORD32 ai4_rem_frms_in_period[MAX_PIC_TYPE], i4_rem_frms_in_gop, i; 193 irc_pic_type_get_rem_frms_in_gop(ps_pic_handling, ai4_rem_frms_in_period); 194 195 i4_rem_frms_in_gop = 0; 196 for(i = 0; i < MAX_PIC_TYPE; i++) 197 i4_rem_frms_in_gop += ai4_rem_frms_in_period[i]; 198 199 if(i4_new_bits_per_frm != ps_rbip->i4_bits_per_frm) 200 { 201 WORD32 i4_rem_frms_in_period = (ps_rbip->i4_num_intra_frm_interval - 1) 202 * ps_rbip->i4_tot_frms_in_gop + i4_rem_frms_in_gop; 203 204 number_t vq_rem_frms_in_period, vq_delta_bits_per_frm, 205 vq_delta_bits_in_period; 206 207 /* delta_bits_per_frm = new_bits_per_frm - old_bits_per_frm */ 208 SET_VAR_Q(vq_delta_bits_per_frm, 209 (i4_new_bits_per_frm - ps_rbip->i4_bits_per_frm), 0); 210 211 SET_VAR_Q(vq_rem_frms_in_period, i4_rem_frms_in_period, 0); 212 213 /* delta_bits_in_period = delta_bits_per_frm * rem_frms_in_period */ 214 mult32_var_q(vq_delta_bits_per_frm, vq_rem_frms_in_period, 215 &vq_delta_bits_in_period); 216 217 /* ps_rbip->rem_bits_in_period += delta_bits_in_period */ 218 add32_var_q(vq_delta_bits_in_period, ps_rbip->vq_rem_bits_in_period, 219 &ps_rbip->vq_rem_bits_in_period); 220 } 221 222 if(i4_new_num_intra_frm_interval != ps_rbip->i4_num_intra_frm_interval) 223 { 224 WORD32 i4_rem_frms_in_period = ps_rbip->i4_tot_frms_in_gop 225 * (i4_new_num_intra_frm_interval 226 - ps_rbip->i4_num_intra_frm_interval); 227 228 number_t vq_rem_frms_in_period, vq_new_bits_per_frm, 229 vq_delta_bits_in_period; 230 231 /* new_bits_per_frm = new_new_bits_per_frm - old_new_bits_per_frm */ 232 SET_VAR_Q(vq_new_bits_per_frm, i4_new_bits_per_frm, 0); 233 234 SET_VAR_Q(vq_rem_frms_in_period, i4_rem_frms_in_period, 0); 235 236 /* delta_bits_in_period = new_bits_per_frm * rem_frms_in_period */ 237 mult32_var_q(vq_new_bits_per_frm, vq_rem_frms_in_period, 238 &vq_delta_bits_in_period); 239 240 /* ps_rbip->rem_bits_in_period += delta_bits_in_period */ 241 add32_var_q(vq_delta_bits_in_period, ps_rbip->vq_rem_bits_in_period, 242 &ps_rbip->vq_rem_bits_in_period); 243 } 244 /* Update the new value */ 245 ps_rbip->i4_num_intra_frm_interval = i4_new_num_intra_frm_interval; 246 ps_rbip->i4_bits_per_frm = i4_new_bits_per_frm; 247 } 248 249 WORD32 irc_ba_num_fill_use_free_memtab(bit_allocation_t **pps_bit_allocation, 250 itt_memtab_t *ps_memtab, 251 ITT_FUNC_TYPE_E e_func_type) 252 { 253 WORD32 i4_mem_tab_idx = 0; 254 bit_allocation_t s_bit_allocation_temp; 255 256 /* 257 * Hack for all alloc, during which we don't have any state memory. 258 * Dereferencing can cause issues 259 */ 260 if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB) 261 (*pps_bit_allocation) = &s_bit_allocation_temp; 262 263 /*for src rate control state structure*/ 264 if(e_func_type != GET_NUM_MEMTAB) 265 { 266 fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(bit_allocation_t), 267 ALIGN_128_BYTE, PERSISTENT, DDR); 268 use_or_fill_base(&ps_memtab[0], (void**)pps_bit_allocation, 269 e_func_type); 270 } 271 i4_mem_tab_idx++; 272 273 i4_mem_tab_idx += irc_error_bits_num_fill_use_free_memtab( 274 &pps_bit_allocation[0]->ps_error_bits, 275 &ps_memtab[i4_mem_tab_idx], e_func_type); 276 277 return (i4_mem_tab_idx); 278 } 279 280 /******************************************************************************* 281 Function Name : irc_ba_init_bit_allocation 282 Description : Initialize the bit_allocation structure. 283 ******************************************************************************/ 284 void irc_ba_init_bit_allocation(bit_allocation_t *ps_bit_allocation, 285 pic_handling_handle ps_pic_handling, 286 WORD32 i4_num_intra_frm_interval, 287 WORD32 i4_bit_rate, 288 WORD32 i4_frm_rate, 289 WORD32 *i4_peak_bit_rate, 290 WORD32 i4_min_bitrate) 291 { 292 WORD32 i; 293 WORD32 i4_bits_per_frm, i4_max_bits_per_frm[MAX_NUM_DRAIN_RATES]; 294 295 /* Calculate the bits per frame */ 296 X_PROD_Y_DIV_Z(i4_bit_rate, 1000, i4_frm_rate, i4_bits_per_frm); 297 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) 298 { 299 X_PROD_Y_DIV_Z(i4_peak_bit_rate[i], 1000, i4_frm_rate, 300 i4_max_bits_per_frm[i]); 301 } 302 /* Initialize the bits_per_frame */ 303 ps_bit_allocation->i4_bits_per_frm = i4_bits_per_frm; 304 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) 305 { 306 ps_bit_allocation->i4_max_bits_per_frm[i] = i4_max_bits_per_frm[i]; 307 } 308 X_PROD_Y_DIV_Z(i4_min_bitrate, 1000, i4_frm_rate, 309 ps_bit_allocation->i4_min_bits_per_frm); 310 311 /* 312 * Initialize the rem_bits in period 313 * The first gop in case of an OPEN GOP may have fewer B_PICs, 314 * That condition is not taken care of 315 */ 316 init_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, i4_bits_per_frm, 317 i4_num_intra_frm_interval); 318 319 /* Initialize the num_gops_in_period */ 320 ps_bit_allocation->i4_num_gops_in_period = i4_num_intra_frm_interval; 321 ps_bit_allocation->i4_actual_num_gops_in_period = i4_num_intra_frm_interval; 322 323 /* Relative complexity between I and P frames */ 324 ps_bit_allocation->i2_K[I_PIC] = (1 << K_Q); 325 ps_bit_allocation->i2_K[P_PIC] = I_TO_P_RATIO; 326 ps_bit_allocation->i2_K[B_PIC] = (P_TO_B_RATIO * I_TO_P_RATIO) >> K_Q; 327 328 /* Initialize the saved bits to 0*/ 329 SET_VAR_Q(ps_bit_allocation->vq_saved_bits, 0, 0); 330 331 /* Update the error bits module with average bits */ 332 irc_init_error_bits(ps_bit_allocation->ps_error_bits, i4_frm_rate, 333 i4_bit_rate); 334 /* Store the input for implementing change in values */ 335 ps_bit_allocation->i4_frame_rate = i4_frm_rate; 336 ps_bit_allocation->i4_bit_rate = i4_bit_rate; 337 338 memset(ps_bit_allocation->i4_prev_frm_header_bits, 0, sizeof(ps_bit_allocation->i4_prev_frm_header_bits)); 339 for(i=0;i<MAX_NUM_DRAIN_RATES;i++) 340 ps_bit_allocation->ai4_peak_bit_rate[i] = i4_peak_bit_rate[i]; 341 } 342 343 /******************************************************************************* 344 Function Name : get_cur_frm_est_bits 345 Description : Based on remaining bits in period and rd_model 346 the number of bits required for the current frame is estimated. 347 ******************************************************************************/ 348 WORD32 irc_ba_get_cur_frm_est_texture_bits(bit_allocation_t *ps_bit_allocation, 349 rc_rd_model_handle *pps_rd_model, 350 est_sad_handle ps_est_sad, 351 pic_handling_handle ps_pic_handling, 352 picture_type_e e_pic_type) 353 { 354 WORD32 i, j; 355 WORD32 i4_est_texture_bits_for_frm; 356 number_t vq_rem_texture_bits; 357 number_t vq_complexity_estimate[MAX_PIC_TYPE]; 358 WORD32 i4_rem_frms_in_period[MAX_PIC_TYPE], i4_frms_in_period[MAX_PIC_TYPE]; 359 number_t vq_max_consumable_bits; 360 number_t vq_rem_frms_in_period[MAX_PIC_TYPE], vq_est_texture_bits_for_frm; 361 number_t vq_prev_hdr_bits[MAX_PIC_TYPE]; 362 363 WORD32 complexity_est = 0; 364 365 /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */ 366 irc_pic_type_get_rem_frms_in_gop(ps_pic_handling, i4_rem_frms_in_period); 367 irc_pic_type_get_frms_in_gop(ps_pic_handling, i4_frms_in_period); 368 369 /* Depending on the number of gops in a period, find the num_frms_in_prd */ 370 for(j = 0; j < MAX_PIC_TYPE; j++) 371 { 372 i4_rem_frms_in_period[j] += (i4_frms_in_period[j] 373 * (ps_bit_allocation->i4_num_gops_in_period - 1)); 374 i4_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period; 375 } 376 377 /* Remove the header bits from the remaining bits to find how many bits you 378 can transfer.*/ 379 irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0); 380 for(i = 0; i < MAX_PIC_TYPE; i++) 381 { 382 SET_VAR_Q(vq_rem_frms_in_period[i], i4_rem_frms_in_period[i], 0); 383 SET_VAR_Q(vq_prev_hdr_bits[i], 384 ps_bit_allocation->i4_prev_frm_header_bits[i], 0); 385 } 386 { 387 /* 388 *rem_texture_bits = rem_bits_in_period - 389 *(rem_frms_in_period[I_PIC] * prev_frm_header_bits[I_PIC]) - 390 *(rem_frms_in_period[P_PIC] * prev_frm_header_bits[P_PIC]) - 391 *(rem_frms_in_period[B_PIC] * prev_frm_header_bits[B_PIC]); 392 */ 393 number_t vq_rem_hdr_bits; 394 vq_rem_texture_bits = ps_bit_allocation->s_rbip.vq_rem_bits_in_period; 395 396 mult32_var_q(vq_prev_hdr_bits[I_PIC], vq_rem_frms_in_period[I_PIC], 397 &vq_rem_hdr_bits); 398 sub32_var_q(vq_rem_texture_bits, vq_rem_hdr_bits, &vq_rem_texture_bits); 399 400 mult32_var_q(vq_prev_hdr_bits[P_PIC], vq_rem_frms_in_period[P_PIC], 401 &vq_rem_hdr_bits); 402 sub32_var_q(vq_rem_texture_bits, vq_rem_hdr_bits, &vq_rem_texture_bits); 403 404 mult32_var_q(vq_prev_hdr_bits[B_PIC], vq_rem_frms_in_period[B_PIC], 405 &vq_rem_hdr_bits); 406 sub32_var_q(vq_rem_texture_bits, vq_rem_hdr_bits, &vq_rem_texture_bits); 407 } 408 { 409 /* max_consumable_bits = 410 *(frms_in_period[I_PIC] * max_bits_per_frm[0] ) + 411 *(frms_in_period[P_PIC] + frms_in_period[B_PIC] ) * max_bits_per_frm[1]; 412 */ 413 number_t vq_max_bits, vq_max_bits_per_frm[2]; 414 415 SET_VAR_Q(vq_max_bits_per_frm[0], 416 ps_bit_allocation->i4_max_bits_per_frm[0], 0); 417 SET_VAR_Q(vq_max_bits_per_frm[1], 418 ps_bit_allocation->i4_max_bits_per_frm[1], 0); 419 420 mult32_var_q(vq_rem_frms_in_period[I_PIC], vq_max_bits_per_frm[0], 421 &vq_max_bits); 422 vq_max_consumable_bits = vq_max_bits; 423 424 mult32_var_q(vq_rem_frms_in_period[P_PIC], vq_max_bits_per_frm[1], 425 &vq_max_bits); 426 add32_var_q(vq_max_bits, vq_max_consumable_bits, 427 &vq_max_consumable_bits); 428 429 mult32_var_q(vq_rem_frms_in_period[B_PIC], vq_max_bits_per_frm[1], 430 &vq_max_bits); 431 add32_var_q(vq_max_bits, vq_max_consumable_bits, 432 &vq_max_consumable_bits); 433 } 434 435 /* rem_texture_bits = MIN(rem_texture_bits, max_consumable_bits) */ 436 MIN_VARQ(vq_max_consumable_bits, vq_rem_texture_bits, vq_rem_texture_bits); 437 438 /* The bits are then allocated based on the relative complexity of the 439 current frame with respect to that of the rest of the frames in period */ 440 for(i = 0; i < MAX_PIC_TYPE; i++) 441 { 442 number_t vq_lin_mod_coeff, vq_est_sad, vq_K; 443 444 /* Getting the linear model coefficient */ 445 vq_lin_mod_coeff = irc_get_linear_coefficient(pps_rd_model[i]); 446 447 /* Getting the estimated SAD */ 448 SET_VAR_Q(vq_est_sad, irc_get_est_sad(ps_est_sad,i), 0); 449 450 /* Making K factor a var Q format */ 451 SET_VAR_Q(vq_K, ps_bit_allocation->i2_K[i], K_Q); 452 453 /* Complexity_estimate = [ (lin_mod_coeff * estimated_sad) / K factor ] */ 454 mult32_var_q(vq_lin_mod_coeff, vq_est_sad, &vq_lin_mod_coeff); 455 div32_var_q(vq_lin_mod_coeff, vq_K, &vq_complexity_estimate[i]); 456 } 457 458 /* 459 * For simple cases, one of the complexities go to zero and in those cases 460 * distribute the bits evenly among frames based on I_TO_P_RATIO 461 */ 462 463 /* Also check the B-pictures complexity only in case they are present*/ 464 if(i4_frms_in_period[B_PIC] == 0) 465 { 466 complexity_est = (vq_complexity_estimate[I_PIC] 467 && vq_complexity_estimate[P_PIC]); 468 } 469 else 470 { 471 complexity_est = (vq_complexity_estimate[I_PIC] 472 && vq_complexity_estimate[P_PIC] 473 && vq_complexity_estimate[B_PIC]); 474 } 475 476 if(complexity_est) 477 { 478 /* 479 * Estimated texture bits = 480 * (remaining bits) * (cur frm complexity) 481 * --------------------------------------- 482 * (num_i_frm*i_frm_complexity) + (num_p_frm*pfrm_complexity) 483 * + (b_frm * b_frm_cm) 484 */ 485 mult32_var_q(vq_rem_texture_bits, vq_complexity_estimate[e_pic_type], 486 &vq_rem_texture_bits); 487 488 for(i = 0; i < MAX_PIC_TYPE; i++) 489 { 490 mult32_var_q(vq_rem_frms_in_period[i], vq_complexity_estimate[i], 491 &vq_rem_frms_in_period[i]); 492 } 493 494 add32_var_q(vq_rem_frms_in_period[I_PIC], vq_rem_frms_in_period[P_PIC], 495 &vq_rem_frms_in_period[I_PIC]); 496 497 add32_var_q(vq_rem_frms_in_period[I_PIC], vq_rem_frms_in_period[B_PIC], 498 &vq_rem_frms_in_period[I_PIC]); 499 500 div32_var_q(vq_rem_texture_bits, vq_rem_frms_in_period[I_PIC], 501 &vq_est_texture_bits_for_frm); 502 503 number_t_to_word32(vq_est_texture_bits_for_frm, 504 &i4_est_texture_bits_for_frm); 505 } 506 else 507 { 508 number_t vq_i_to_p_bit_ratio, vq_rem_frms; 509 510 SET_VAR_Q(vq_i_to_p_bit_ratio, I_TO_P_BIT_RATIO, 0); 511 512 /* rem_frms = ((I_TO_P_BIT_RATIO * rem_frms_in_period[I_PIC]) + 513 * rem_frms_in_period[P_PIC] + rem_frms_in_period[B_PIC]); 514 */ 515 mult32_var_q(vq_rem_frms_in_period[I_PIC], vq_i_to_p_bit_ratio, 516 &vq_rem_frms); 517 add32_var_q(vq_rem_frms_in_period[P_PIC], vq_rem_frms, &vq_rem_frms); 518 add32_var_q(vq_rem_frms_in_period[B_PIC], vq_rem_frms, &vq_rem_frms); 519 520 /* est_texture_bits_for_frm = rem_texture_bits / rem_frms */ 521 div32_var_q(vq_rem_texture_bits, vq_rem_frms, 522 &vq_est_texture_bits_for_frm); 523 number_t_to_word32(vq_est_texture_bits_for_frm, 524 &i4_est_texture_bits_for_frm); 525 526 i4_est_texture_bits_for_frm = 527 (I_PIC == e_pic_type) ? 528 (i4_est_texture_bits_for_frm 529 * I_TO_P_BIT_RATIO) : 530 i4_est_texture_bits_for_frm; 531 } 532 533 /* 534 * If the remaining bits in the period becomes negative then the estimated 535 * texture bits would also become negative. This would send a feedback to 536 * the model which may go for a toss. Thus sending the minimum possible 537 * value = 0 538 */ 539 if(i4_est_texture_bits_for_frm < 0) 540 { 541 i4_est_texture_bits_for_frm = 0; 542 } 543 544 return (i4_est_texture_bits_for_frm); 545 } 546 547 /****************************************************************************** 548 Function Name : irc_ba_get_cur_frm_est_header_bits 549 Description : Based on remaining bits in period and rd_model 550 the number of bits required for the current frame is estimated. 551 ******************************************************************************/ 552 WORD32 irc_ba_get_cur_frm_est_header_bits(bit_allocation_t *ps_bit_allocation, 553 picture_type_e e_pic_type) 554 { 555 return (ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type]); 556 } 557 558 WORD32 irc_ba_get_rem_bits_in_period(bit_allocation_t *ps_bit_allocation, 559 pic_handling_handle ps_pic_handling) 560 { 561 WORD32 i4_rem_bits_in_gop = 0; 562 irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0); 563 number_t_to_word32(ps_bit_allocation->s_rbip.vq_rem_bits_in_period, 564 &i4_rem_bits_in_gop); 565 return (i4_rem_bits_in_gop); 566 } 567 568 /******************************************************************************* 569 Function Name : irc_ba_update_cur_frm_consumed_bits 570 Description : Based on remaining bits in period and rd_model 571 the number of bits required for the current frame is estimated. 572 ******************************************************************************/ 573 void irc_ba_update_cur_frm_consumed_bits(bit_allocation_t *ps_bit_allocation, 574 pic_handling_handle ps_pic_handling, 575 WORD32 i4_total_frame_bits, 576 WORD32 i4_model_updation_hdr_bits, 577 picture_type_e e_pic_type, 578 UWORD8 u1_is_scd, 579 WORD32 i4_last_frm_in_gop) 580 { 581 WORD32 i4_error_bits = irc_get_error_bits(ps_bit_allocation->ps_error_bits); 582 583 /* Update the remaining bits in period */ 584 irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 585 (-i4_total_frame_bits + i4_error_bits)); 586 587 /* 588 * Update the header bits so that it can be used as an estimate to the next 589 * frame 590 */ 591 if(u1_is_scd) 592 { 593 /* 594 * In case of SCD, even though the frame type is P, it is equivalent to 595 * a I frame and so the corresponding header bits is updated 596 */ 597 ps_bit_allocation->i4_prev_frm_header_bits[I_PIC] = 598 i4_model_updation_hdr_bits; 599 600 #define MAX_NUM_GOPS_IN_PERIOD (3) 601 if(ps_bit_allocation->i4_num_gops_in_period < MAX_NUM_GOPS_IN_PERIOD) 602 { 603 /* 604 * Whenever there is a scene change increase the number of gops by 605 * 2 so that the number of bits allocated is not very constrained 606 */ 607 ps_bit_allocation->i4_num_gops_in_period += 2; 608 /* Add the extra bits in GOP to remaining bits in period */ 609 irc_ba_change_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 610 ps_bit_allocation->i4_bits_per_frm, 611 ps_bit_allocation->i4_num_gops_in_period); 612 } 613 } 614 else 615 { 616 ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type] = 617 i4_model_updation_hdr_bits; 618 } 619 620 if(i4_last_frm_in_gop) 621 { 622 WORD32 i4_num_bits_in_a_gop = get_number_of_frms_in_a_gop( 623 ps_pic_handling) * ps_bit_allocation->i4_bits_per_frm; 624 /* 625 * If the number of gops in period has been increased due to scene 626 * change, slowly bring in down across the gops 627 */ 628 if(ps_bit_allocation->i4_num_gops_in_period 629 > ps_bit_allocation->i4_actual_num_gops_in_period) 630 { 631 ps_bit_allocation->i4_num_gops_in_period--; 632 irc_ba_change_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 633 ps_bit_allocation->i4_bits_per_frm, 634 ps_bit_allocation->i4_num_gops_in_period); 635 } 636 /* 637 * If rem_bits_in_period < 0 decrease the number of bits allocated for 638 * the next period else increase it 639 */ 640 irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 641 i4_num_bits_in_a_gop); 642 } 643 /* Update the lower modules */ 644 irc_update_error_bits(ps_bit_allocation->ps_error_bits); 645 } 646 647 void irc_ba_change_remaining_bits_in_period(bit_allocation_t *ps_bit_allocation, 648 pic_handling_handle ps_pic_handling, 649 WORD32 i4_bit_rate, 650 WORD32 i4_frame_rate, 651 WORD32 *i4_peak_bit_rate) 652 { 653 WORD32 i4_new_avg_bits_per_frm; 654 WORD32 i4_new_peak_bits_per_frm[MAX_NUM_DRAIN_RATES]; 655 WORD32 i4_rem_frms_in_period[MAX_PIC_TYPE]; 656 int i; 657 658 /* Calculate the new per frame bits */ 659 X_PROD_Y_DIV_Z(i4_bit_rate, 1000, i4_frame_rate, i4_new_avg_bits_per_frm); 660 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) 661 { 662 X_PROD_Y_DIV_Z(i4_peak_bit_rate[i], 1000, i4_frame_rate, 663 i4_new_peak_bits_per_frm[i]); 664 } 665 666 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) 667 { 668 ps_bit_allocation->i4_max_bits_per_frm[i] = i4_new_peak_bits_per_frm[i]; 669 } 670 671 /* 672 * Get the rem_frms_in_prd & the frms_in_prd from the pic_type state 673 * struct 674 */ 675 irc_pic_type_get_rem_frms_in_gop(ps_pic_handling, i4_rem_frms_in_period); 676 677 /* 678 * If the difference > 0(/ <0), the remaining bits in period needs to be 679 * increased(/decreased) based on the remaining number of frames 680 */ 681 irc_ba_change_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 682 i4_new_avg_bits_per_frm, 683 ps_bit_allocation->i4_num_gops_in_period); 684 685 /* Update the new average bits per frame */ 686 ps_bit_allocation->i4_bits_per_frm = i4_new_avg_bits_per_frm; 687 /* change the lower modules state */ 688 irc_change_bitrate_in_error_bits(ps_bit_allocation->ps_error_bits, 689 i4_bit_rate); 690 irc_change_frm_rate_in_error_bits(ps_bit_allocation->ps_error_bits, 691 i4_frame_rate); 692 693 /* Store the modified frame_rate */ 694 ps_bit_allocation->i4_frame_rate = i4_frame_rate; 695 ps_bit_allocation->i4_bit_rate = i4_bit_rate; 696 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) 697 ps_bit_allocation->ai4_peak_bit_rate[i] = i4_peak_bit_rate[i]; 698 } 699 700 void irc_ba_change_ba_peak_bit_rate(bit_allocation_t *ps_bit_allocation, 701 WORD32 *ai4_peak_bit_rate) 702 { 703 WORD32 i; 704 705 /* Calculate the bits per frame */ 706 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) 707 { 708 X_PROD_Y_DIV_Z(ai4_peak_bit_rate[i], 1000, 709 ps_bit_allocation->i4_frame_rate, 710 ps_bit_allocation->i4_max_bits_per_frm[i]); 711 ps_bit_allocation->ai4_peak_bit_rate[i] = ai4_peak_bit_rate[i]; 712 } 713 } 714 715 /****************************************************************************** 716 * @brief Modifies the remaining bit in period for the gop which has fif. 717 * since fif would cause a new gop to be created, we need to add the number 718 * of encoded frames in the fif GOP worth of bits to remaining bits in 719 * period 720 ******************************************************************************/ 721 void irc_ba_change_rem_bits_in_prd_at_force_I_frame(bit_allocation_t *ps_bit_allocation, 722 pic_handling_handle ps_pic_handling) 723 { 724 WORD32 i4_frms_in_period; 725 i4_frms_in_period = irc_pic_type_get_frms_in_gop_force_I_frm( 726 ps_pic_handling); 727 irc_ba_update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 728 ps_bit_allocation->i4_bits_per_frm * i4_frms_in_period); 729 } 730 731 void irc_ba_check_and_update_bit_allocation(bit_allocation_t *ps_bit_allocation, 732 pic_handling_handle ps_pic_handling, 733 WORD32 i4_cur_buf_size, 734 WORD32 i4_max_buf_size, 735 WORD32 i4_max_bits_inflow_per_frm, 736 WORD32 i4_tot_frame_bits) 737 { 738 739 number_t vq_max_drain_bits, vq_extra_bits, vq_less_bits, 740 vq_allocated_saved_bits, vq_min_bits_for_period; 741 WORD32 i4_num_frms_in_period = get_number_of_frms_in_a_gop(ps_pic_handling); 742 number_t vq_rem_bits_in_period, vq_num_frms_in_period, vq_zero; 743 WORD32 b_rem_bits_gt_max_drain, b_rem_bits_lt_min_bits, 744 b_saved_bits_gt_zero; 745 rem_bit_in_prd_t *ps_rbip = &ps_bit_allocation->s_rbip; 746 747 UNUSED(i4_cur_buf_size); 748 UNUSED(i4_max_buf_size); 749 UNUSED(i4_tot_frame_bits); 750 751 /* 752 * If the remaining bits is greater than what can be drained in that period 753 * Clip the remaining bits in period to the maximum it can drain in that 754 * period with the error of current buffer size.Accumulate the saved bits 755 * if any. else if the remaining bits is lesser than the minimum bit rate 756 * promised in that period Add the excess bits to remaining bits in period 757 * and reduce it from the saved bits Else Provide the extra bits from the 758 * "saved bits pool". 759 */ 760 /* 761 * max_drain_bits = num_gops_in_period * num_frms_in_period * 762 * * max_bits_inflow_per_frm 763 */ 764 SET_VAR_Q(vq_num_frms_in_period, 765 (ps_bit_allocation->i4_num_gops_in_period * i4_num_frms_in_period), 766 0); 767 SET_VAR_Q(vq_max_drain_bits, i4_max_bits_inflow_per_frm, 0); 768 SET_VAR_Q(vq_zero, 0, 0); 769 mult32_var_q(vq_max_drain_bits, vq_num_frms_in_period, &vq_max_drain_bits); 770 771 /* 772 * min_bits_for_period = num_gops_in_period * num_frms_in_period * 773 * min_bits_per_frm 774 */ 775 SET_VAR_Q(vq_min_bits_for_period, ps_bit_allocation->i4_min_bits_per_frm, 776 0); 777 mult32_var_q(vq_min_bits_for_period, vq_num_frms_in_period, 778 &vq_min_bits_for_period); 779 780 vq_rem_bits_in_period = ps_rbip->vq_rem_bits_in_period; 781 782 /* Evaluate rem_bits_in_period > max_drain_bits */ 783 VQ_A_GT_VQ_B(ps_rbip->vq_rem_bits_in_period, vq_max_drain_bits, 784 b_rem_bits_gt_max_drain); 785 786 /* Evaluate rem_bits_in_period < min_bits_for_period */ 787 VQ_A_LT_VQ_B(ps_rbip->vq_rem_bits_in_period, vq_min_bits_for_period, 788 b_rem_bits_lt_min_bits); 789 790 /* Evaluate saved_bits > 0 */ 791 VQ_A_LT_VQ_B(ps_bit_allocation->vq_saved_bits, vq_zero, 792 b_saved_bits_gt_zero); 793 794 /* (i4_rem_bits_in_period > i4_max_drain_bits) */ 795 if(b_rem_bits_gt_max_drain) 796 { 797 /* extra_bits = rem_bits_in_period - max_drain_bits */ 798 sub32_var_q(ps_rbip->vq_rem_bits_in_period, vq_max_drain_bits, 799 &vq_extra_bits); 800 801 /* saved_bits += extra_bits */ 802 add32_var_q(ps_bit_allocation->vq_saved_bits, vq_extra_bits, 803 &ps_bit_allocation->vq_saved_bits); 804 805 /* rem_bits_in_period = vq_max_drain_bits */ 806 ps_rbip->vq_rem_bits_in_period = vq_max_drain_bits; 807 } 808 else if(b_rem_bits_lt_min_bits) 809 { 810 /* extra_bits(-ve) = rem_bits_in_period - i4_min_bits_for_period */ 811 sub32_var_q(ps_rbip->vq_rem_bits_in_period, vq_min_bits_for_period, 812 &vq_extra_bits); 813 814 /* saved_bits += extra_bits(-ve) */ 815 add32_var_q(ps_bit_allocation->vq_saved_bits, vq_extra_bits, 816 &ps_bit_allocation->vq_saved_bits); 817 818 /* rem_bits_in_period = min_bits_for_period */ 819 ps_rbip->vq_rem_bits_in_period = vq_min_bits_for_period; 820 } 821 else if(b_saved_bits_gt_zero) 822 { 823 /* less_bits = max_drain_bits - _rem_bits_in_period */ 824 sub32_var_q(vq_max_drain_bits, vq_rem_bits_in_period, &vq_less_bits); 825 826 /* allocated_saved_bits = MIN (less_bits, saved_bits) */ 827 MIN_VARQ(ps_bit_allocation->vq_saved_bits, vq_less_bits, 828 vq_allocated_saved_bits); 829 830 /* rem_bits_in_period += allocted_save_bits */ 831 add32_var_q(ps_rbip->vq_rem_bits_in_period, vq_allocated_saved_bits, 832 &ps_rbip->vq_rem_bits_in_period); 833 834 /* saved_bits -= allocted_save_bits */ 835 sub32_var_q(ps_bit_allocation->vq_saved_bits, vq_allocated_saved_bits, 836 &ps_bit_allocation->vq_saved_bits); 837 } 838 return; 839 } 840 841 WORD32 irc_ba_get_frame_rate(bit_allocation_t *ps_bit_allocation) 842 { 843 return (ps_bit_allocation->i4_frame_rate); 844 } 845 846 WORD32 irc_ba_get_bit_rate(bit_allocation_t *ps_bit_allocation) 847 { 848 return (ps_bit_allocation->i4_bit_rate); 849 } 850 851 void irc_ba_get_peak_bit_rate(bit_allocation_t *ps_bit_allocation, 852 WORD32 *pi4_peak_bit_rate) 853 { 854 WORD32 i; 855 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++) 856 { 857 pi4_peak_bit_rate[i] = ps_bit_allocation->ai4_peak_bit_rate[i]; 858 } 859 } 860