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 /** 22 ******************************************************************************* 23 * @file 24 * ih264e_rate_control.c 25 * 26 * @brief 27 * Contains api function definitions for h264 rate control 28 * 29 * @author 30 * ittiam 31 * 32 * @par List of Functions: 33 * - ih264e_rc_init() 34 * - ih264e_rc_get_picture_details() 35 * - ih264e_rc_pre_enc() 36 * - ih264e_update_rc_mb_info() 37 * - ih264e_rc_get_buffer_status() 38 * - ih264e_rc_post_enc() 39 * - ih264e_update_rc_bits_info() 40 * 41 * @remarks 42 * None 43 * 44 ******************************************************************************* 45 */ 46 47 /*****************************************************************************/ 48 /* File Includes */ 49 /*****************************************************************************/ 50 51 /* User include files */ 52 #include "irc_datatypes.h" 53 #include "iv2.h" 54 #include "ive2.h" 55 #include "ih264e.h" 56 #include "ih264_defs.h" 57 #include "ih264_macros.h" 58 #include "ih264_structs.h" 59 #include "ih264_trans_quant_itrans_iquant.h" 60 #include "ih264_inter_pred_filters.h" 61 #include "ih264_mem_fns.h" 62 #include "ih264_padding.h" 63 #include "ih264_intra_pred_filters.h" 64 #include "ih264_deblk_edge_filters.h" 65 #include "ih264_common_tables.h" 66 #include "ih264_cabac_tables.h" 67 #include "ih264e_defs.h" 68 #include "ih264e_globals.h" 69 #include "irc_mem_req_and_acq.h" 70 #include "irc_cntrl_param.h" 71 #include "irc_frame_info_collector.h" 72 #include "irc_rate_control_api.h" 73 #include "ih264e_time_stamp.h" 74 #include "ih264e_modify_frm_rate.h" 75 #include "ih264e_rate_control.h" 76 #include "ih264e_error.h" 77 #include "ih264e_bitstream.h" 78 #include "ime_distortion_metrics.h" 79 #include "ime_defs.h" 80 #include "ime_structs.h" 81 #include "ih264e_cabac_structs.h" 82 #include "ih264e_structs.h" 83 #include "ih264e_utils.h" 84 #include "irc_trace_support.h" 85 86 87 /*****************************************************************************/ 88 /* Function Definitions */ 89 /*****************************************************************************/ 90 91 /** 92 ******************************************************************************* 93 * 94 * @brief This function does nothing 95 * 96 * @par Description 97 * This function does nothing 98 * 99 * @param[in] variadic function 100 101 * @returns none 102 * 103 * @remarks This function is used by the rc library for debugging purposes. 104 * However this function was not part of rc library. So this is defined here 105 * to resolve link issues. 106 * 107 ******************************************************************************* 108 */ 109 int trace_printf(const WORD8 *format, ...) 110 { 111 UNUSED(format); 112 return(0); 113 }; 114 115 /** 116 ******************************************************************************* 117 * 118 * @brief 119 * This function initializes rate control context and variables 120 * 121 * @par Description 122 * This function initializes rate control type, source and target frame rate, 123 * average and peak bitrate, intra-inter frame interval and initial 124 * quantization parameter 125 * 126 * @param[in] pv_rc_api 127 * Handle to rate control api 128 * 129 * @param[in] pv_frame_time 130 * Handle to frame time context 131 * 132 * @param[in] pv_time_stamp 133 * Handle to time stamp context 134 * 135 * @param[in] pv_pd_frm_rate 136 * Handle to pull down frame time context 137 * 138 * @param[in] u4_max_frm_rate 139 * Maximum frame rate 140 * 141 * @param[in] u4_src_frm_rate 142 * Source frame rate 143 * 144 * @param[in] u4_tgt_frm_rate 145 * Target frame rate 146 * 147 * @param[in] e_rate_control_type 148 * Rate control type 149 * 150 * @param[in] u4_avg_bit_rate 151 * Average bit rate 152 * 153 * @param[in] u4_peak_bit_rate 154 * Peak bit rate 155 * 156 * @param[in] u4_max_delay 157 * Maximum delay between frames 158 * 159 * @param[in] u4_intra_frame_interval 160 * Intra frame interval 161 * 162 * @param[in] pu1_init_qp 163 * Initial qp 164 * 165 * @param[in] i4_max_inter_frm_int 166 * Maximum inter frame interval 167 * 168 * @param[in] pu1_min_max_qp 169 * Array of min/max qp 170 * 171 * @param[in] u1_profile_level 172 * Encoder profile level 173 * 174 * @returns none 175 * 176 * @remarks 177 * 178 ******************************************************************************* 179 */ 180 void ih264e_rc_init(void *pv_rc_api, 181 void *pv_frame_time, 182 void *pv_time_stamp, 183 void *pv_pd_frm_rate, 184 UWORD32 u4_max_frm_rate, 185 UWORD32 u4_src_frm_rate, 186 UWORD32 u4_tgt_frm_rate, 187 rc_type_e e_rate_control_type, 188 UWORD32 u4_avg_bit_rate, 189 UWORD32 u4_peak_bit_rate, 190 UWORD32 u4_max_delay, 191 UWORD32 u4_intra_frame_interval, 192 WORD32 i4_inter_frm_int, 193 UWORD8 *pu1_init_qp, 194 WORD32 i4_max_inter_frm_int, 195 UWORD8 *pu1_min_max_qp, 196 UWORD8 u1_profile_level) 197 { 198 // UWORD8 u1_is_mb_level_rc_on = 0; 199 UWORD32 au4_peak_bit_rate[2] = {0,0}; 200 UWORD32 u4_min_bit_rate = 0; 201 WORD32 i4_is_gop_closed = 1; 202 // WORD32 i4_use_est_intra_sad = 1; 203 UWORD32 u4_src_ticks = 0; 204 UWORD32 u4_tgt_ticks = 0; 205 UWORD8 u1_level_idx = ih264e_get_lvl_idx(u1_profile_level); 206 UWORD32 u4_max_cpb_size = 1200 * gas_ih264_lvl_tbl[u1_level_idx].u4_max_cpb_size; 207 208 /* Fill the params needed for the RC init */ 209 if (e_rate_control_type == CBR_NLDRC) 210 { 211 au4_peak_bit_rate[0] = u4_avg_bit_rate; 212 au4_peak_bit_rate[1] = u4_avg_bit_rate; 213 } 214 else 215 { 216 au4_peak_bit_rate[0] = u4_peak_bit_rate; 217 au4_peak_bit_rate[1] = u4_peak_bit_rate; 218 } 219 220 /* Initialize frame time computation module*/ 221 ih264e_init_frame_time(pv_frame_time, 222 u4_src_frm_rate, /* u4_src_frm_rate */ 223 u4_tgt_frm_rate); /* u4_tgt_frm_rate */ 224 225 /* Initialize the pull_down frame rate */ 226 ih264e_init_pd_frm_rate(pv_pd_frm_rate, 227 u4_src_frm_rate); /* u4_input_frm_rate */ 228 229 /* Initialize time stamp structure */ 230 ih264e_init_time_stamp(pv_time_stamp, 231 u4_max_frm_rate, /* u4_max_frm_rate */ 232 u4_src_frm_rate); /* u4_src_frm_rate */ 233 234 u4_src_ticks = ih264e_frame_time_get_src_ticks(pv_frame_time); 235 u4_tgt_ticks = ih264e_frame_time_get_tgt_ticks(pv_frame_time); 236 237 /* Init max_inter_frame int */ 238 i4_max_inter_frm_int = (i4_inter_frm_int == 1) ? 2 : (i4_inter_frm_int + 2); 239 240 /* Initialize the rate control */ 241 irc_initialise_rate_control(pv_rc_api, /* RC handle */ 242 e_rate_control_type, /* RC algo type */ 243 0, /* MB activity on/off */ 244 u4_avg_bit_rate, /* Avg Bitrate */ 245 au4_peak_bit_rate, /* Peak bitrate array[2]:[I][P] */ 246 u4_min_bit_rate, /* Min Bitrate */ 247 u4_src_frm_rate, /* Src frame_rate */ 248 u4_max_delay, /* Max buffer delay */ 249 u4_intra_frame_interval, /* Intra frm_interval */ 250 i4_inter_frm_int, /* Inter frame interval */ 251 pu1_init_qp, /* Init QP array[3]:[I][P][B] */ 252 u4_max_cpb_size, /* Max VBV/CPB Buffer Size */ 253 i4_max_inter_frm_int, /* Max inter frm_interval */ 254 i4_is_gop_closed, /* Open/Closed GOP */ 255 pu1_min_max_qp, /* Min-max QP array[6]:[Imax][Imin][Pmax][Pmin][Bmax][Bmin] */ 256 0, /* How to calc the I-frame estimated_sad */ 257 u4_src_ticks, /* Src_ticks = LCM(src_frm_rate,tgt_frm_rate)/src_frm_rate */ 258 u4_tgt_ticks); /* Tgt_ticks = LCM(src_frm_rate,tgt_frm_rate)/tgt_frm_rate */ 259 } 260 261 /** 262 ******************************************************************************* 263 * 264 * @brief Function to get picture details 265 * 266 * @par Description 267 * This function returns the Picture type(I/P/B) 268 * 269 * @param[in] pv_rc_api 270 * Handle to Rate control api 271 * 272 * @returns 273 * Picture type 274 * 275 * @remarks none 276 * 277 ******************************************************************************* 278 */ 279 picture_type_e ih264e_rc_get_picture_details(void *pv_rc_api, 280 WORD32 *pi4_pic_id, 281 WORD32 *pi4_pic_disp_order_no) 282 { 283 picture_type_e e_rc_pic_type = P_PIC; 284 285 irc_get_picture_details(pv_rc_api, pi4_pic_id, pi4_pic_disp_order_no, 286 &e_rc_pic_type); 287 288 return (e_rc_pic_type); 289 } 290 291 /** 292 ******************************************************************************* 293 * 294 * @brief Function to get rate control output before encoding 295 * 296 * @par Description 297 * This function is called before queing the current frame. It decides if we should 298 * skip the current iput buffer due to frame rate mismatch. It also updates RC about 299 * the acehivble frame rate 300 * 301 * @param[in] ps_rate_control_api 302 * Handle to rate control api 303 * 304 * @param[in] ps_pd_frm_rate 305 * Handle to pull down frm rate context 306 * 307 * @param[in] ps_time_stamp 308 * Handle to time stamp context 309 * 310 * @param[in] ps_frame_time 311 * Handle to frame time context 312 * 313 * @param[in] i4_delta_time_stamp 314 * Time stamp difference between frames 315 * 316 * @param[in] i4_total_mb_in_frame 317 * Total Macro Blocks in frame 318 * 319 * @param[in/out] pe_vop_coding_type 320 * Picture coding type(I/P/B) 321 * 322 * @param[in/out] pu1_frame_qp 323 * QP for current frame 324 * 325 * @returns 326 * Skip or queue the current frame 327 * 328 * @remarks 329 * 330 ******************************************************************************* 331 */ 332 WORD32 ih264e_update_rc_framerates(void *ps_rate_control_api, 333 void *ps_pd_frm_rate, 334 void *ps_time_stamp, 335 void *ps_frame_time) 336 { 337 WORD8 i4_skip_src = 0; 338 UWORD32 u4_src_not_skipped_for_dts = 0; 339 340 /* Update the time stamp for the current frame */ 341 ih264e_update_time_stamp(ps_time_stamp); 342 343 /* Check if a src not needs to be skipped */ 344 i4_skip_src = ih264e_should_src_be_skipped(ps_frame_time, 345 1, 346 &u4_src_not_skipped_for_dts); 347 348 if (i4_skip_src) 349 { 350 /*********************************************************************** 351 *Based on difference in source and target frame rate frames are skipped 352 ***********************************************************************/ 353 /*update the missing frames frm_rate with 0 */ 354 ih264e_update_pd_frm_rate(ps_pd_frm_rate, 0); 355 } 356 else 357 { 358 WORD32 i4_avg_frm_rate, i4_source_frame_rate; 359 360 i4_source_frame_rate = ih264e_frame_time_get_src_frame_rate( 361 ps_frame_time); 362 363 /* Update the frame rate of the frame present with the tgt_frm_rate */ 364 /* If the frm was not skipped due to delta_time_stamp, update the 365 frame_rate with double the tgt_frame_rate value, so that it makes 366 up for one of the frames skipped by the application */ 367 ih264e_update_pd_frm_rate(ps_pd_frm_rate, i4_source_frame_rate); 368 369 /* Based on the update get the average frame rate */ 370 i4_avg_frm_rate = ih264e_get_pd_avg_frm_rate(ps_pd_frm_rate); 371 372 /* Call the RC library function to change the frame_rate to the 373 actually achieved frm_rate */ 374 irc_change_frm_rate_for_bit_alloc(ps_rate_control_api, i4_avg_frm_rate); 375 } 376 377 return (i4_skip_src); 378 } 379 380 /** 381 ******************************************************************************* 382 * 383 * @brief Function to update mb info for rate control context 384 * 385 * @par Description 386 * After encoding a mb, information such as mb type, qp used, mb distortion 387 * resulted in encoding the block and so on needs to be preserved for modeling 388 * RC. This is preserved via this function call. 389 * 390 * @param[in] ps_frame_info 391 * Handle Frame info context 392 * 393 * @param[in] ps_proc 394 * Process context 395 * 396 * @returns 397 * 398 * @remarks 399 * 400 ******************************************************************************* 401 */ 402 void ih264e_update_rc_mb_info(frame_info_t *ps_frame_info, void *pv_proc) 403 { 404 /* proc ctxt */ 405 process_ctxt_t *ps_proc = pv_proc; 406 407 /* is intra or inter */ 408 WORD32 mb_type = !ps_proc->u4_is_intra; 409 410 /* distortion */ 411 ps_frame_info->tot_mb_sad[mb_type] += ps_proc->i4_mb_distortion; 412 413 /* qp */ 414 ps_frame_info->qp_sum[mb_type] += gau1_h264_to_mpeg2_qmap[ps_proc->u4_mb_qp]; 415 416 /* mb cnt */ 417 ps_frame_info->num_mbs[mb_type]++; 418 419 /* cost */ 420 if (ps_proc->u4_is_intra) 421 { 422 ps_frame_info->intra_mb_cost_sum += ps_proc->i4_mb_cost; 423 } 424 } 425 426 /** 427 ******************************************************************************* 428 * 429 * @brief Function to get rate control buffer status 430 * 431 * @par Description 432 * This function is used to get buffer status(underflow/overflow) by rate 433 * control module 434 * 435 * @param[in] pv_rc_api 436 * Handle to rate control api context 437 * 438 * @param[in] i4_total_frame_bits 439 * Total frame bits 440 * 441 * @param[in] u1_pic_type 442 * Picture type 443 * 444 * @param[in] pi4_num_bits_to_prevent_vbv_underflow 445 * Number of bits to prevent underflow 446 * 447 * @param[out] pu1_is_enc_buf_overflow 448 * Buffer overflow indication flag 449 * 450 * @param[out] pu1_is_enc_buf_underflow 451 * Buffer underflow indication flag 452 * 453 * @returns 454 * 455 * @remarks 456 * 457 ******************************************************************************* 458 */ 459 void ih264e_rc_get_buffer_status(void *pv_rc_api, 460 WORD32 i4_total_frame_bits, 461 picture_type_e e_pic_type, 462 WORD32 *pi4_num_bits_to_prevent_vbv_underflow, 463 UWORD8 *pu1_is_enc_buf_overflow, 464 UWORD8 *pu1_is_enc_buf_underflow) 465 { 466 vbv_buf_status_e e_vbv_buf_status = VBV_NORMAL; 467 468 e_vbv_buf_status = irc_get_buffer_status(pv_rc_api, 469 i4_total_frame_bits, 470 e_pic_type, 471 pi4_num_bits_to_prevent_vbv_underflow); 472 473 if (e_vbv_buf_status == VBV_OVERFLOW) 474 { 475 *pu1_is_enc_buf_underflow = 1; 476 *pu1_is_enc_buf_overflow = 0; 477 } 478 else if (e_vbv_buf_status == VBV_UNDERFLOW) 479 { 480 *pu1_is_enc_buf_underflow = 0; 481 *pu1_is_enc_buf_overflow = 1; 482 } 483 else 484 { 485 *pu1_is_enc_buf_underflow = 0; 486 *pu1_is_enc_buf_overflow = 0; 487 } 488 } 489 490 /** 491 ******************************************************************************* 492 * 493 * @brief Function to update rate control module after encoding 494 * 495 * @par Description 496 * This function is used to update the rate control module after the current 497 * frame encoding is done with details such as bits consumed, SAD for I/P/B, 498 * intra cost ,mb type and other 499 * 500 * @param[in] ps_rate_control_api 501 * Handle to rate control api context 502 * 503 * @param[in] ps_frame_info 504 * Handle to frame info context 505 * 506 * @param[in] ps_pd_frm_rate 507 * Handle to pull down frame rate context 508 * 509 * @param[in] ps_time_stamp 510 * Handle to time stamp context 511 * 512 * @param[in] ps_frame_time 513 * Handle to frame time context 514 * 515 * @param[in] i4_total_mb_in_frame 516 * Total mb in frame 517 * 518 * @param[in] pe_vop_coding_type 519 * Picture coding type 520 * 521 * @param[in] i4_is_first_frame 522 * Is first frame 523 * 524 * @param[in] pi4_is_post_encode_skip 525 * Post encoding skip flag 526 * 527 * @param[in] u1_frame_qp 528 * Frame qp 529 * 530 * @param[in] pi4_num_intra_in_prev_frame 531 * Numberf of intra mbs in previous frame 532 * 533 * @param[in] pi4_avg_activity 534 * Average activity 535 * 536 * @returns 537 * 538 * @remarks 539 * 540 ******************************************************************************* 541 */ 542 WORD32 ih264e_rc_post_enc(void * ps_rate_control_api, 543 frame_info_t *ps_frame_info, 544 void * ps_pd_frm_rate, 545 void * ps_time_stamp, 546 void * ps_frame_time, 547 WORD32 i4_total_mb_in_frame, 548 picture_type_e *pe_vop_coding_type, 549 WORD32 i4_is_first_frame, 550 WORD32 *pi4_is_post_encode_skip, 551 UWORD8 u1_frame_qp, 552 WORD32 *pi4_num_intra_in_prev_frame, 553 WORD32 *pi4_avg_activity) 554 { 555 /* Variables for the update_frm_level_info */ 556 WORD32 ai4_tot_mb_in_type[MAX_MB_TYPE]; 557 WORD32 ai4_tot_mb_type_qp[MAX_MB_TYPE] = {0, 0}; 558 WORD32 ai4_mb_type_sad[MAX_MB_TYPE] = {0, 0}; 559 WORD32 ai4_mb_type_tex_bits[MAX_MB_TYPE] = {0, 0}; 560 WORD32 i4_total_frame_bits = 0; 561 WORD32 i4_total_hdr_bits = 0; 562 WORD32 i4_total_texturebits; 563 WORD32 i4_avg_mb_activity = 0; 564 WORD32 i4_intra_frm_cost = 0; 565 UWORD8 u1_is_scd = 0; 566 WORD32 i4_cbr_bits_to_stuff = 0; 567 UWORD32 u4_num_intra_in_prev_frame = *pi4_num_intra_in_prev_frame; 568 UNUSED(ps_pd_frm_rate); 569 UNUSED(ps_time_stamp); 570 UNUSED(ps_frame_time); 571 UNUSED(u1_frame_qp); 572 /* Accumulate RC stats */ 573 ai4_tot_mb_in_type[MB_TYPE_INTRA] = irc_fi_get_total_mb(ps_frame_info,MB_TYPE_INTRA); 574 ai4_tot_mb_in_type[MB_TYPE_INTER] = irc_fi_get_total_mb(ps_frame_info,MB_TYPE_INTER); 575 /* ai4_tot_mb_type_qp[MB_TYPE_INTRA] = 0; 576 ai4_tot_mb_type_qp[MB_TYPE_INTER] = ps_enc->pu1_h264_mpg2quant[u1_frame_qp] * i4_total_mb_in_frame;*/ 577 ai4_tot_mb_type_qp[MB_TYPE_INTRA] = irc_fi_get_total_mb_qp(ps_frame_info,MB_TYPE_INTRA); 578 ai4_tot_mb_type_qp[MB_TYPE_INTER] = irc_fi_get_total_mb_qp(ps_frame_info,MB_TYPE_INTER); 579 ai4_mb_type_sad[MB_TYPE_INTRA] = irc_fi_get_total_mb_sad(ps_frame_info,MB_TYPE_INTRA); 580 ai4_mb_type_sad[MB_TYPE_INTER] = irc_fi_get_total_mb_sad(ps_frame_info,MB_TYPE_INTER); 581 i4_intra_frm_cost = irc_fi_get_total_intra_mb_cost(ps_frame_info); 582 i4_avg_mb_activity = irc_fi_get_avg_activity(ps_frame_info); 583 i4_total_hdr_bits = irc_fi_get_total_header_bits(ps_frame_info); 584 i4_total_texturebits = irc_fi_get_total_mb_texture_bits(ps_frame_info,MB_TYPE_INTRA); 585 i4_total_texturebits += irc_fi_get_total_mb_texture_bits(ps_frame_info,MB_TYPE_INTER); 586 i4_total_frame_bits = i4_total_hdr_bits + i4_total_texturebits ; 587 588 *pi4_avg_activity = i4_avg_mb_activity; 589 590 591 /* Texture bits are not accumulated. Hence subtracting hdr bits from total bits */ 592 ai4_mb_type_tex_bits[MB_TYPE_INTRA] = 0; 593 ai4_mb_type_tex_bits[MB_TYPE_INTER] = i4_total_frame_bits - i4_total_hdr_bits; 594 595 /* Set post encode skip to zero */ 596 pi4_is_post_encode_skip[0]= 0; 597 598 /* For NLDRC, get the buffer status for stuffing or skipping */ 599 if (irc_get_rc_type(ps_rate_control_api) == CBR_NLDRC) 600 { 601 WORD32 i4_get_num_bit_to_prevent_vbv_overflow; 602 UWORD8 u1_enc_buf_overflow,u1_enc_buf_underflow; 603 604 /* Getting the buffer status */ 605 ih264e_rc_get_buffer_status(ps_rate_control_api, i4_total_frame_bits, 606 pe_vop_coding_type[0], &i4_get_num_bit_to_prevent_vbv_overflow, 607 &u1_enc_buf_overflow,&u1_enc_buf_underflow); 608 609 /* We skip the frame if decoder buffer is underflowing. But we never skip first I frame */ 610 if ((u1_enc_buf_overflow == 1) && (i4_is_first_frame != 1)) 611 // if ((u1_enc_buf_overflow == 1) && (i4_is_first_frame != 0)) 612 { 613 irc_post_encode_frame_skip(ps_rate_control_api, (picture_type_e)pe_vop_coding_type[0]); 614 // i4_total_frame_bits = imp4_write_skip_frame_header(ps_enc); 615 i4_total_frame_bits = 0; 616 617 *pi4_is_post_encode_skip = 1; 618 619 /* Adjust the GOP if in case we skipped an I-frame */ 620 if (*pe_vop_coding_type == I_PIC) 621 irc_force_I_frame(ps_rate_control_api); 622 623 /* Since this frame is skipped by writing 7 bytes header, we say this is a P frame */ 624 // *pe_vop_coding_type = P; 625 626 /* Getting the buffer status again,to check if it underflows */ 627 irc_get_buffer_status(ps_rate_control_api, i4_total_frame_bits, 628 (picture_type_e)pe_vop_coding_type[0], &i4_get_num_bit_to_prevent_vbv_overflow); 629 630 } 631 632 /* In this case we stuff bytes as buffer is overflowing */ 633 if (u1_enc_buf_underflow == 1) 634 { 635 /* The stuffing function is directly pulled out from split controller workspace. 636 encode_vop_data() function makes sure alignment data is dumped at the end of a 637 frame. Split controller was identifying this alignment byte, overwriting it with 638 the stuff data and then finally aligning the buffer. Here every thing is inside 639 the DSP. So, ideally encode_vop_data needn't align, and we can start stuffing directly. 640 But in that case, it'll break the logic for a normal frame. 641 Hence for simplicity, not changing this part since it is ok to align and 642 then overwrite since stuffing is not done for every frame */ 643 i4_cbr_bits_to_stuff = irc_get_bits_to_stuff(ps_rate_control_api, i4_total_frame_bits, pe_vop_coding_type[0]); 644 645 /* Just add extra 32 bits to make sure we don't stuff lesser */ 646 i4_cbr_bits_to_stuff += 32; 647 648 /* We can not stuff more than the outbuf size. So have a check here */ 649 /* Add stuffed bits to total bits */ 650 i4_total_frame_bits += i4_cbr_bits_to_stuff; 651 } 652 } 653 654 #define ENABLE_SCD 1 655 #if ENABLE_SCD 656 /* If number of intra MBs are more than 2/3rd of total MBs, assume it as a scene change */ 657 if ((ai4_tot_mb_in_type[MB_TYPE_INTRA] > ((2 * i4_total_mb_in_frame) / 3)) && 658 (*pe_vop_coding_type == P_PIC) && 659 (ai4_tot_mb_in_type[MB_TYPE_INTRA] > ((11 * (WORD32)u4_num_intra_in_prev_frame) / 10))) 660 { 661 u1_is_scd = 1; 662 } 663 #endif 664 665 /* Update num intra mbs of this frame */ 666 if (pi4_is_post_encode_skip[0] == 0) 667 { 668 *pi4_num_intra_in_prev_frame = ai4_tot_mb_in_type[MB_TYPE_INTRA]; 669 } 670 671 /* Reset intra count to zero, if u encounter an I frame */ 672 if (*pe_vop_coding_type == I_PIC) 673 { 674 *pi4_num_intra_in_prev_frame = 0; 675 } 676 677 /* Do an update of rate control after post encode */ 678 irc_update_frame_level_info(ps_rate_control_api, /* RC state */ 679 pe_vop_coding_type[0], /* PIC type */ 680 ai4_mb_type_sad, /* SAD for [Intra/Inter] */ 681 i4_total_frame_bits, /* Total frame bits */ 682 i4_total_hdr_bits, /* header bits for */ 683 ai4_mb_type_tex_bits, /* for MB[Intra/Inter] */ 684 ai4_tot_mb_type_qp, /* for MB[Intra/Inter] */ 685 ai4_tot_mb_in_type, /* for MB[Intra/Inter] */ 686 i4_avg_mb_activity, /* Average mb activity in frame */ 687 u1_is_scd, /* Is a scene change detected */ 688 0, /* Pre encode skip */ 689 (WORD32)i4_intra_frm_cost, /* Intra cost for frame */ 690 0); /* Not done outside */ 691 692 return (i4_cbr_bits_to_stuff >> 3); 693 } 694 695 /** 696 ******************************************************************************* 697 * 698 * @brief Function to update bits consumed info to rate control context 699 * 700 * @par Description 701 * Function to update bits consume info to rate control context 702 * 703 * @param[in] ps_frame_info 704 * Frame info context 705 * 706 * @param[in] ps_entropy 707 * Entropy context 708 * 709 * @returns 710 * total bits consumed by the frame 711 * 712 * @remarks 713 * 714 ******************************************************************************* 715 */ 716 void ih264e_update_rc_bits_info(frame_info_t *ps_frame_info, void *pv_entropy) 717 { 718 entropy_ctxt_t *ps_entropy = pv_entropy; 719 720 ps_frame_info->mb_header_bits[MB_TYPE_INTRA] += ps_entropy->u4_header_bits[MB_TYPE_INTRA]; 721 722 ps_frame_info->mb_texture_bits[MB_TYPE_INTRA] += ps_entropy->u4_residue_bits[MB_TYPE_INTRA]; 723 724 ps_frame_info->mb_header_bits[MB_TYPE_INTER] += ps_entropy->u4_header_bits[MB_TYPE_INTER]; 725 726 ps_frame_info->mb_texture_bits[MB_TYPE_INTER] += ps_entropy->u4_residue_bits[MB_TYPE_INTER]; 727 728 return; 729 } 730 731