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