1 /******************************************************************************
2 *
3 * Copyright (C) 2018 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 * \file rate_control_api.c
23 *
24 * \brief
25 * This file contain rate control API functions
26 *
27 * \date
28 *
29 * \author
30 * ittiam
31 *
32 ******************************************************************************
33 */
34 /*****************************************************************************/
35 /* File Includes */
36 /*****************************************************************************/
37 /* System include files */
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <assert.h>
42 #include <math.h>
43
44 /* User include files */
45 #include "ittiam_datatypes.h"
46 /* Lower module include files. These inclusion can be removed by having
47 fwd declaration for each and every module*/
48 #include "rc_common.h"
49 #include "rc_cntrl_param.h"
50 #include "mem_req_and_acq.h"
51 #include "var_q_operator.h"
52 #include "rc_rd_model.h"
53 #include "est_sad.h"
54 #include "fixed_point_error_bits.h"
55 #include "vbr_storage_vbv.h"
56 #include "picture_type.h"
57 #include "cbr_buffer_control.h"
58 #include "bit_allocation.h"
59 #include "mb_model_based.h"
60 #include "vbr_str_prms.h"
61 #include "init_qp.h"
62 #include "rc_sad_acc.h"
63 #include "rc_frame_info_collector.h"
64 #include "rate_control_api.h"
65 #include "rate_control_api_structs.h"
66 #include "trace_support.h"
67
68 /** Macros **/
69 #define MIN(x, y) ((x) < (y)) ? (x) : (y)
70 #define MAX(x, y) ((x) < (y)) ? (y) : (x)
71 #define CLIP3RC(x, min, max) (((x) > (max)) ? (max) : (((x) < (min)) ? (min) : (x)))
72
73 #define DEV_Q 4 /*Q format(Shift) for Deviation range factor */
74 #define HI_DEV_FCTR 26 //23//32 /* 1.4*16 */
75 #define LO_DEV_B_FCTR 10 //temp change to avoid stuffing12 /* 0.75*16 */
76 #define LO_DEV_FCTR_1B 14 //8 /* 0.75*16 */
77 //#define LO_DEV_FCTR_7B 10//8 /* 0.75*16 */
78 #define LO_DEV_FCTR_3B 12 //8 /* 0.75*16 */
79 #define LO_DEV_FCTR_7B 12 //8 /* 0.75*16 */
80 #define GET_HI_DEV_QP(Qprev) ((((WORD32)Qprev) * HI_DEV_FCTR + (1 << (DEV_Q - 1))) >> DEV_Q)
81
82 #define GET_LO_DEV_QP(Qprev, i4_num_active_pic_types)((i4_num_active_pic_types <= B1_PIC)?(((((WORD32) Qprev)*LO_DEV_FCTR_1B + (1<<(DEV_Q-1)))>>DEV_Q): \
83 ((i4_num_active_pic_types == B2_PIC)? ((((WORD32) Qprev)*LO_DEV_FCTR_3B + (1<<(DEV_Q-1)))>>DEV_Q) \
84 ((((WORD32) Qprev)*LO_DEV_FCTR_7B + (1<<(DEV_Q-1)))>>DEV_Q))))
85
86 #define GET_LO_DEV_QP_B(Qprev) ((((WORD32)Qprev) * LO_DEV_B_FCTR + (1 << (DEV_Q - 1))) >> DEV_Q)
87 #define CLIP_QP(Qc, hi_d, lo_d) (((Qc) < (lo_d)) ? ((lo_d)) : (((Qc) > (hi_d)) ? (hi_d) : (Qc)))
88
89 /*below macors are used when qp is already in q format so adding 0.5 for rounding is not required*/
90 #define GET_HI_DEV_QP_QFAC(Qprev) ((((WORD32)Qprev) * HI_DEV_FCTR) >> DEV_Q)
91 #define GET_LO_DEV_QP_QFAC(Qprev, i4_num_active_pic_types) \
92 ((i4_num_active_pic_types <= B1_PIC) \
93 ? ((((WORD32)Qprev) * LO_DEV_FCTR_1B) >> DEV_Q) \
94 : ((i4_num_active_pic_types == B2_PIC) ? ((((WORD32)Qprev) * LO_DEV_FCTR_3B) >> DEV_Q) \
95 : ((((WORD32)Qprev) * LO_DEV_FCTR_7B) >> DEV_Q)))
96
97 #define GET_LO_DEV_QP_QFAC_B_PIC(Qprev) ((((WORD32)Qprev) * LO_DEV_FCTR_3B) >> DEV_Q)
98
99 #define GET_LO_DEV_QP_B_QFAC(Qprev) ((((WORD32)Qprev) * LO_DEV_B_FCTR) >> DEV_Q)
100
101 #define P_TO_I_RATIO_Q_FACTOR (9)
102 #define MULT_FACTOR_SATD (4)
103 #define GET_L0_SATD_BY_ACT_MAX_PER_PIXEL(i4_num_pixel) \
104 ((5.4191f * i4_num_pixel + 4000000.0f) / i4_num_pixel)
105 #define GET_WEIGH_FACTOR_FOR_MIN_SCD_Q_SCALE(normal_satd_act, f_satd_by_Act_norm) \
106 (MULT_FACTOR_SATD * normal_satd_act + f_satd_by_Act_norm) / \
107 (normal_satd_act + MULT_FACTOR_SATD * f_satd_by_Act_norm)
108
109 void SET_NETRA_TRACE(UWORD8 tag[], WORD32 value);
110 #define NETRA_TRACE (0)
111 #if NETRA_TRACE
112 #else
113 #define SET_NETRA_TRACE(x, y)
114 #endif
115
116 /*****************************************************************************/
117 /* Restricts the quantisation parameter variation within delta */
118 /*****************************************************************************/
119 /* static WORD32 restrict_swing(WORD32 cur_qp, WORD32 prev_qp, WORD32 delta_qp)
120 {
121 if((cur_qp) - (prev_qp) > (delta_qp)) (cur_qp) = (prev_qp) + (delta_qp) ;
122 if((prev_qp) - (cur_qp) > (delta_qp)) (cur_qp) = (prev_qp) - (delta_qp) ;
123 return cur_qp;
124 }*/
125
126 /*****************************************************************************
127 Function Name : rate_control_get_init_free_memtab
128 Description : Takes or gives memtab
129 Inputs : pps_rate_control_api - pointer to RC api pointer
130 ps_memtab - Memtab pointer
131 i4_use_base - Set during init, else 0
132 i4_fill_base - Set during free, else 0
133 Globals :
134 Processing :
135 Outputs :
136 Returns :
137 Issues :
138 Revision History:
139 DD MM YYYY Author(s) Changes (Describe the changes made)
140
141 *****************************************************************************/
142 #if NON_STEADSTATE_CODE
rate_control_num_fill_use_free_memtab(rate_control_handle * pps_rate_control_api,itt_memtab_t * ps_memtab,ITT_FUNC_TYPE_E e_func_type)143 WORD32 rate_control_num_fill_use_free_memtab(
144 rate_control_handle *pps_rate_control_api, itt_memtab_t *ps_memtab, ITT_FUNC_TYPE_E e_func_type)
145 {
146 WORD32 i4_mem_tab_idx = 0, i;
147 static rate_control_api_t s_temp_rc_api;
148
149 /* Hack for al alloc, during which we dont have any state memory.
150 Dereferencing can cause issues */
151 if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
152 (*pps_rate_control_api) = &s_temp_rc_api;
153
154 /*for src rate control state structure*/
155 if(e_func_type != GET_NUM_MEMTAB)
156 {
157 fill_memtab(
158 &ps_memtab[i4_mem_tab_idx],
159 sizeof(rate_control_api_t),
160 MEM_TAB_ALIGNMENT,
161 PERSISTENT,
162 DDR);
163 use_or_fill_base(&ps_memtab[0], (void **)pps_rate_control_api, e_func_type);
164 }
165 i4_mem_tab_idx++;
166
167 /* Get the memory requirement of lower modules */
168 i4_mem_tab_idx += bit_allocation_num_fill_use_free_memtab(
169 &pps_rate_control_api[0]->ps_bit_allocation, &ps_memtab[i4_mem_tab_idx], e_func_type);
170
171 i4_mem_tab_idx += cbr_buffer_num_fill_use_free_memtab(
172 &pps_rate_control_api[0]->ps_cbr_buffer, &ps_memtab[i4_mem_tab_idx], e_func_type);
173
174 i4_mem_tab_idx += est_sad_num_fill_use_free_memtab(
175 &pps_rate_control_api[0]->ps_est_sad, &ps_memtab[i4_mem_tab_idx], e_func_type);
176
177 i4_mem_tab_idx += mbrc_num_fill_use_free_memtab(
178 &pps_rate_control_api[0]->ps_mb_rate_control, &ps_memtab[i4_mem_tab_idx], e_func_type);
179
180 i4_mem_tab_idx += vbr_vbv_num_fill_use_free_memtab(
181 &pps_rate_control_api[0]->ps_vbr_storage_vbv, &ps_memtab[i4_mem_tab_idx], e_func_type);
182
183 i4_mem_tab_idx += init_qp_num_fill_use_free_memtab(
184 &pps_rate_control_api[0]->ps_init_qp, &ps_memtab[i4_mem_tab_idx], e_func_type);
185
186 i4_mem_tab_idx += sad_acc_num_fill_use_free_memtab(
187 &pps_rate_control_api[0]->ps_sad_acc, &ps_memtab[i4_mem_tab_idx], e_func_type);
188
189 for(i = 0; i < MAX_PIC_TYPE; i++)
190 {
191 i4_mem_tab_idx += rc_rd_model_num_fill_use_free_memtab(
192 &pps_rate_control_api[0]->aps_rd_model[i], &ps_memtab[i4_mem_tab_idx], e_func_type);
193 }
194 i4_mem_tab_idx += pic_handling_num_fill_use_free_memtab(
195 &pps_rate_control_api[0]->ps_pic_handling, &ps_memtab[i4_mem_tab_idx], e_func_type);
196 return (i4_mem_tab_idx);
197 }
198
199 /*****************************************************************************
200 Function Name : initialise_rate_control
201 Description : Initialise the rate control structure
202 Inputs : ps_rate_control_api - api struct
203 e_rate_control_type - VBR, CBR (NLDRC/LDRC), VBR_STREAMING
204 u1_is_mb_level_rc_on - enabling mb level RC
205 u4_avg_bit_rate - bit rate to achieved across the entire file size
206 u4_peak_bit_rate - max possible drain rate
207 u4_frame_rate - number of frames in 1000 seconds
208 u4_intra_frame_interval - num frames between two I frames
209 *au1_init_qp - init_qp for I,P,B
210
211
212 Globals :
213 Processing :
214 Outputs :
215 Returns :
216 Issues :
217 Revision History:
218 DD MM YYYY Author(s) Changes (Describe the changes made)
219
220 *****************************************************************************/
initialise_rate_control(rate_control_api_t * ps_rate_control_api,rc_type_e e_rate_control_type,UWORD8 u1_is_mb_level_rc_on,UWORD32 u4_avg_bit_rate,UWORD32 * pu4_peak_bit_rate,UWORD32 u4_min_bit_rate,UWORD32 u4_frame_rate,UWORD32 u4_max_delay,UWORD32 u4_intra_frame_interval,UWORD32 u4_idr_period,WORD32 * pi4_init_qp,UWORD32 u4_max_vbv_buff_size,WORD32 i4_max_inter_frm_int,WORD32 i4_is_gop_closed,WORD32 * pi4_min_max_qp,WORD32 i4_use_est_intra_sad,UWORD32 u4_src_ticks,UWORD32 u4_tgt_ticks,WORD32 i4_frame_height,WORD32 i4_frame_width,WORD32 i4_num_active_pic_type,WORD32 i4_field_pic,WORD32 i4_quality_preset,WORD32 i4_lap_window,WORD32 i4_initial_decoder_delay_frames,float f_max_peak_rate_sustain_dur,LWORD64 i8_num_frames_to_encode,UWORD32 u4_min_scd_hevc_qp,UWORD8 u1_bit_depth,FILE * pf_rc_stat_file,WORD32 i4_pass_num,void * pv_gop_stat,LWORD64 i8_num_gop_mem_alloc,WORD32 i4_is_infinite_gop,WORD32 i4_size_of_lap_out,WORD32 i4_size_of_rc_lap_out,void * pv_sys_rc_api,WORD32 i4_fp_bit_alloc_in_sp,WORD32 i4_num_frame_parallel,WORD32 i4_capped_vbr_flag)221 void initialise_rate_control(
222 rate_control_api_t *ps_rate_control_api,
223 rc_type_e e_rate_control_type,
224 UWORD8 u1_is_mb_level_rc_on,
225 UWORD32 u4_avg_bit_rate,
226 UWORD32 *pu4_peak_bit_rate,
227 UWORD32 u4_min_bit_rate,
228 UWORD32 u4_frame_rate,
229 UWORD32 u4_max_delay,
230 UWORD32 u4_intra_frame_interval,
231 UWORD32 u4_idr_period,
232 WORD32 *pi4_init_qp,
233 UWORD32 u4_max_vbv_buff_size,
234 WORD32 i4_max_inter_frm_int,
235 WORD32 i4_is_gop_closed,
236 WORD32 *pi4_min_max_qp,
237 WORD32 i4_use_est_intra_sad,
238 UWORD32 u4_src_ticks,
239 UWORD32 u4_tgt_ticks,
240 WORD32 i4_frame_height,
241 WORD32 i4_frame_width,
242 WORD32 i4_num_active_pic_type,
243 WORD32 i4_field_pic,
244 WORD32 i4_quality_preset,
245 WORD32 i4_lap_window,
246 WORD32 i4_initial_decoder_delay_frames,
247 float f_max_peak_rate_sustain_dur,
248 LWORD64 i8_num_frames_to_encode,
249 UWORD32 u4_min_scd_hevc_qp,
250 UWORD8 u1_bit_depth,
251 FILE *pf_rc_stat_file,
252 WORD32 i4_pass_num,
253 void *pv_gop_stat,
254 LWORD64 i8_num_gop_mem_alloc,
255 WORD32 i4_is_infinite_gop,
256 WORD32 i4_size_of_lap_out,
257 WORD32 i4_size_of_rc_lap_out,
258 void *pv_sys_rc_api,
259 WORD32 i4_fp_bit_alloc_in_sp,
260 WORD32 i4_num_frame_parallel,
261 WORD32 i4_capped_vbr_flag)
262 {
263 WORD32 i, i4_temp;
264 UWORD32 u4_frms_in_delay_prd = (u4_frame_rate * u4_max_delay) / 1000000;
265 UWORD32 i4_cbr_bit_alloc_period;
266 float f_bit_depth_based_max_qp;
267 UWORD32 u4_bit_depth_based_max_qp;
268 WORD32 i4_pels_in_frame = (3 * (i4_frame_height * i4_frame_width) >> 1);
269
270 if(u4_intra_frame_interval ==
271 1) /*i_only: Set bit allocation period to 15( currently not configurable) for i only mode*/
272 {
273 i4_cbr_bit_alloc_period = u4_frame_rate / 1000; /*changed */
274 }
275 else
276 {
277 i4_cbr_bit_alloc_period = 1;
278 }
279
280 if(CBR_NLDRC_HBR == e_rate_control_type)
281 {
282 e_rate_control_type = CBR_NLDRC;
283 ps_rate_control_api->i4_is_hbr = 1;
284 }
285 else
286 {
287 ps_rate_control_api->i4_is_hbr = 0;
288 }
289 ps_rate_control_api->e_rc_type = e_rate_control_type;
290 ps_rate_control_api->i4_capped_vbr_flag = i4_capped_vbr_flag;
291 ps_rate_control_api->u1_is_mb_level_rc_on = u1_is_mb_level_rc_on;
292 ps_rate_control_api->i4_num_active_pic_type = i4_num_active_pic_type;
293 ps_rate_control_api->i4_quality_preset = i4_quality_preset;
294 ps_rate_control_api->i4_scd_I_frame_estimated_tot_bits = 0;
295 ps_rate_control_api->i4_I_frame_qp_model = 0;
296 ps_rate_control_api->u4_min_scd_hevc_qp = u4_min_scd_hevc_qp;
297 ps_rate_control_api->pf_rc_stat_file = pf_rc_stat_file;
298 ps_rate_control_api->i4_rc_pass = i4_pass_num;
299 ps_rate_control_api->i4_max_frame_height = i4_frame_height;
300 ps_rate_control_api->i4_max_frame_width = i4_frame_width;
301 ps_rate_control_api->i4_underflow_warning = 0;
302 ps_rate_control_api->f_p_to_i_comp_ratio = 1.0f;
303 ps_rate_control_api->i4_scd_in_period_2_pass = 0;
304 ps_rate_control_api->i4_is_infinite_gop = i4_is_infinite_gop;
305 ps_rate_control_api->i4_frames_since_last_scd = 0;
306 ps_rate_control_api->i4_num_frame_parallel = i4_num_frame_parallel;
307
308 /*The memory for gop level summary struct is stored only for 2 pass*/
309 if(i4_pass_num == 2)
310 {
311 ps_rate_control_api->pv_2pass_gop_summary = pv_gop_stat;
312 }
313 else
314 {
315 ps_rate_control_api->pv_2pass_gop_summary = NULL;
316 }
317 /*Initialize the call back funcitons for file related operations*/
318 ps_rate_control_api->pv_rc_sys_api = pv_sys_rc_api;
319
320 ps_rate_control_api->u1_bit_depth = u1_bit_depth;
321
322 f_bit_depth_based_max_qp = (float)((51 + (6 * (u1_bit_depth - 8))) - 4) / 6;
323 u4_bit_depth_based_max_qp = (UWORD32)pow(2.0f, f_bit_depth_based_max_qp);
324
325 ps_rate_control_api->u4_bit_depth_based_max_qp = u4_bit_depth_based_max_qp;
326
327 trace_printf("RC type = %d\n", e_rate_control_type);
328
329 /* Set the avg_bitrate_changed flag for each pic_type to 0 */
330 for(i = 0; i < MAX_PIC_TYPE; i++)
331 {
332 ps_rate_control_api->au1_avg_bitrate_changed[i] = 0;
333 }
334
335 /* Initialize the pic_handling module */
336 init_pic_handling(
337 ps_rate_control_api->ps_pic_handling, /*ps_pic_handling*/
338 (WORD32)u4_intra_frame_interval, /*i4_intra_frm_int,*/
339 i4_max_inter_frm_int, /*i4_max_inter_frm_int,*/
340 i4_is_gop_closed,
341 (WORD32)u4_idr_period,
342 ps_rate_control_api->i4_num_active_pic_type,
343 i4_field_pic); /*gop_struct_e*/
344
345 /* initialise the init Qp module */
346 init_init_qp(
347 ps_rate_control_api->ps_init_qp,
348 pi4_min_max_qp,
349 i4_pels_in_frame,
350 ps_rate_control_api->i4_is_hbr);
351
352 /*** Initialize the rate control modules ***/
353 if(ps_rate_control_api->e_rc_type != CONST_QP)
354 {
355 UWORD32 au4_num_pics_in_delay_prd[MAX_PIC_TYPE] = { 0 };
356
357 /* Initialise the model parameter structures */
358 for(i = 0; i < MAX_PIC_TYPE; i++)
359 {
360 init_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[i], MAX_FRAMES_MODELLED);
361 }
362
363 /* Initialize the buffer mechanism */
364 if((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
365 (ps_rate_control_api->e_rc_type == VBR_STORAGE_DVD_COMP))
366 {
367 /* Assuming both the peak bit rates are same for a VBR_STORAGE and
368 VBR_STORAGE_DVD_COMP */
369 if(pu4_peak_bit_rate[0] != pu4_peak_bit_rate[1])
370 {
371 trace_printf("For VBR_STORAGE and VBR_STORAGE_DVD_COMP the peak bit "
372 "rates should be same\n");
373 }
374 init_vbr_vbv(
375 ps_rate_control_api->ps_vbr_storage_vbv,
376 (WORD32)pu4_peak_bit_rate[0],
377 (WORD32)u4_frame_rate,
378 (WORD32)u4_max_vbv_buff_size);
379 }
380 else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
381 {
382 UWORD32 u4_avg_bit_rate_copy[MAX_NUM_DRAIN_RATES];
383 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
384 {
385 u4_avg_bit_rate_copy[i] = u4_avg_bit_rate;
386 }
387
388 init_cbr_buffer(
389 ps_rate_control_api->ps_cbr_buffer,
390 u4_max_delay,
391 u4_frame_rate,
392 u4_avg_bit_rate,
393 au4_num_pics_in_delay_prd,
394 u4_max_vbv_buff_size,
395 u4_intra_frame_interval,
396 ps_rate_control_api->e_rc_type,
397 pu4_peak_bit_rate[0],
398 i4_initial_decoder_delay_frames,
399 f_max_peak_rate_sustain_dur,
400 i8_num_frames_to_encode,
401 i4_max_inter_frm_int,
402 i4_pass_num,
403 0 /*capped vbr off */);
404 }
405 else if(ps_rate_control_api->e_rc_type == VBR_STREAMING)
406 {
407 init_vbv_str_prms(
408 &ps_rate_control_api->s_vbr_str_prms,
409 u4_intra_frame_interval,
410 u4_src_ticks,
411 u4_tgt_ticks,
412 u4_frms_in_delay_prd);
413
414 init_cbr_buffer(
415 ps_rate_control_api->ps_cbr_buffer,
416 u4_max_delay,
417 u4_frame_rate,
418 u4_avg_bit_rate,
419 au4_num_pics_in_delay_prd,
420 u4_max_vbv_buff_size,
421 u4_intra_frame_interval,
422 ps_rate_control_api->e_rc_type,
423 pu4_peak_bit_rate[0],
424 i4_initial_decoder_delay_frames,
425 f_max_peak_rate_sustain_dur,
426 i8_num_frames_to_encode,
427 i4_max_inter_frm_int,
428 i4_pass_num,
429 ps_rate_control_api->i4_capped_vbr_flag);
430 }
431
432 /* Initalise the SAD estimation module */
433 init_est_sad(ps_rate_control_api->ps_est_sad, i4_use_est_intra_sad);
434
435 /* Initialise the bit allocation module according to VBR or CBR */
436 if((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
437 (ps_rate_control_api->e_rc_type == VBR_STREAMING) ||
438 (ps_rate_control_api->e_rc_type == VBR_STORAGE_DVD_COMP))
439 {
440 /*UWORD32 u4_scaled_avg_bit_rate;*/
441 /*X_PROD_Y_DIV_Z (u4_avg_bit_rate,1126,1024,u4_scaled_avg_bit_rate);*/
442 init_bit_allocation(
443 ps_rate_control_api->ps_bit_allocation,
444 ps_rate_control_api->ps_pic_handling,
445 i4_cbr_bit_alloc_period,
446 u4_avg_bit_rate /*u4_scaled_avg_bit_rate*/,
447 u4_frame_rate,
448 (WORD32 *)pu4_peak_bit_rate,
449 u4_min_bit_rate,
450 i4_pels_in_frame,
451 ps_rate_control_api->i4_is_hbr,
452 ps_rate_control_api->i4_num_active_pic_type,
453 i4_lap_window,
454 i4_field_pic,
455 i4_pass_num,
456 (i4_frame_height * i4_frame_width),
457 i4_fp_bit_alloc_in_sp);
458 }
459 else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
460 {
461 init_bit_allocation(
462 ps_rate_control_api->ps_bit_allocation,
463 ps_rate_control_api->ps_pic_handling,
464 i4_cbr_bit_alloc_period, //i_onlyCBR_BIT_ALLOC_PERIOD,
465 u4_avg_bit_rate,
466 u4_frame_rate,
467 (WORD32 *)pu4_peak_bit_rate,
468 u4_min_bit_rate,
469 i4_pels_in_frame,
470 ps_rate_control_api->i4_is_hbr,
471 ps_rate_control_api->i4_num_active_pic_type,
472 i4_lap_window,
473 i4_field_pic,
474 i4_pass_num,
475 (i4_frame_height * i4_frame_width),
476 i4_fp_bit_alloc_in_sp);
477 }
478 }
479 else
480 {
481 UWORD32 au4_num_pics_in_delay_prd[MAX_PIC_TYPE];
482
483 for(i = 0; i < MAX_PIC_TYPE; i++)
484 au4_num_pics_in_delay_prd[i] = 0;
485
486 init_cbr_buffer(
487 ps_rate_control_api->ps_cbr_buffer,
488 u4_max_delay,
489 u4_frame_rate,
490 u4_avg_bit_rate,
491 au4_num_pics_in_delay_prd,
492 u4_max_vbv_buff_size,
493 u4_intra_frame_interval,
494 ps_rate_control_api->e_rc_type,
495 pu4_peak_bit_rate[0],
496 i4_initial_decoder_delay_frames,
497 f_max_peak_rate_sustain_dur,
498 i8_num_frames_to_encode,
499 i4_max_inter_frm_int,
500 i4_pass_num,
501 0 /*capped vbr off */);
502 }
503
504 /* Initialize the init_qp */
505 for(i4_temp = 0; i4_temp < MAX_SCENE_NUM_RC; i4_temp++)
506 {
507 for(i = 0; i < MAX_PIC_TYPE; i++)
508 {
509 ps_rate_control_api->ai4_prev_frm_qp[i4_temp][i] = 0x7FFFFFFF; //pi4_init_qp[i];
510 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_temp][i] =
511 0x7FFFFFFF; //pi4_init_qp[i] << QSCALE_Q_FAC;
512 ps_rate_control_api->ai4_min_qp[i] = pi4_min_max_qp[(i << 1)];
513 ps_rate_control_api->ai4_max_qp[i] = pi4_min_max_qp[(i << 1) + 1];
514 }
515 }
516 /*init min and max qp in qscale*/
517 for(i = 0; i < MAX_PIC_TYPE; i++)
518 {
519 ps_rate_control_api->ai4_min_qp_q6[i] = MIN_QSCALE_Q6;
520 //ps_rate_control_api->ai4_max_qp_q6[i] = (228 << QSCALE_Q_FAC);
521 ps_rate_control_api->ai4_max_qp_q6[i] = (u4_bit_depth_based_max_qp << QSCALE_Q_FAC);
522 }
523
524 /* Initialize the is_first_frm_encoded */
525 for(i = 0; i < MAX_PIC_TYPE; i++)
526 {
527 ps_rate_control_api->au1_is_first_frm_coded[i] = 0;
528 }
529 ps_rate_control_api->u1_is_first_frm = 1;
530 ps_rate_control_api->i4_prev_ref_is_scd = 0;
531
532 for(i = 0; i < MAX_NUM_FRAME_PARALLEL; i++)
533 {
534 ps_rate_control_api->ai4_est_tot_bits[i] =
535 get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
536 }
537
538 /* Control flag for delayed impact after a change in peak bitrate has been made */
539 ps_rate_control_api->u4_frms_in_delay_prd_for_peak_bit_rate_change = 0;
540 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
541 {
542 ps_rate_control_api->au4_new_peak_bit_rate[i] = pu4_peak_bit_rate[i];
543 }
544
545 /* initialise the mb level rate control module */
546 init_mb_level_rc(ps_rate_control_api->ps_mb_rate_control);
547 ps_rate_control_api->i4_prev_frm_est_bits = u4_avg_bit_rate / (u4_frame_rate / 1000);
548
549 ps_rate_control_api->prev_ref_pic_type = I_PIC;
550 ps_rate_control_api->i4_P_to_I_ratio = (1 << (P_TO_I_RATIO_Q_FACTOR + K_Q)) / I_TO_P_RATIO;
551
552 /* Initialise sad accumulator */
553 init_sad_acc(ps_rate_control_api->ps_sad_acc);
554
555 rc_get_max_hme_sad_per_pixel(ps_rate_control_api, i4_frame_height * i4_frame_width);
556 }
557 #endif /* #if NON_STEADSTATE_CODE */
558
559 /****************************************************************************
560 *Function Name : add_picture_to_stack
561 *Description : calls add_pic_to_stack
562 *Inputs :
563 *Globals :
564 *Processing :
565 *Outputs :
566 *Returns :
567 *Issues :
568 *Revision History:d
569 *DD MM YYYY Author(s) Changes (Describe the changes made)
570 *
571 *****************************************************************************/
add_picture_to_stack(rate_control_api_t * rate_control_api,WORD32 i4_enc_pic_id,WORD32 i4_rc_in_pic)572 void add_picture_to_stack(
573 rate_control_api_t *rate_control_api, WORD32 i4_enc_pic_id, WORD32 i4_rc_in_pic)
574 {
575 /* Call the routine to add the pic to stack in encode order */
576 add_pic_to_stack(rate_control_api->ps_pic_handling, i4_enc_pic_id, i4_rc_in_pic);
577 }
578
579 /****************************************************************************
580 Function Name : add_picture_to_stack_re_enc
581 Description :
582 Inputs :
583 Globals :
584 Processing :
585 Outputs :
586 Returns :
587 Issues :
588 Revision History:
589 DD MM YYYY Author(s) Changes (Describe the changes made)
590
591 *****************************************************************************/
add_picture_to_stack_re_enc(rate_control_api_t * rate_control_api,WORD32 i4_enc_pic_id,picture_type_e e_pic_type)592 void add_picture_to_stack_re_enc(
593 rate_control_api_t *rate_control_api, WORD32 i4_enc_pic_id, picture_type_e e_pic_type)
594 {
595 /* In case of a re-encoder, the pics will come in the encode order itself.
596 So, there is no need to buffer the pics up */
597 add_pic_to_stack_re_enc(rate_control_api->ps_pic_handling, i4_enc_pic_id, e_pic_type);
598 }
599
600 /****************************************************************************
601 Function Name : get_picture_details
602 Description : decides the picture type based on the state
603 Inputs :
604 Globals :
605 Processing :
606 Outputs :
607 Returns :
608 Issues :
609 Revision History:
610 DD MM YYYY Author(s) Changes (Describe the changes made)
611
612 *****************************************************************************/
get_picture_details(rate_control_handle rate_control_api,WORD32 * pi4_pic_id,WORD32 * pi4_pic_disp_order_no,picture_type_e * pe_pic_type,WORD32 * pi4_is_scd)613 void get_picture_details(
614 rate_control_handle rate_control_api,
615 WORD32 *pi4_pic_id,
616 WORD32 *pi4_pic_disp_order_no,
617 picture_type_e *pe_pic_type,
618 WORD32 *pi4_is_scd)
619 {
620 /* Call to get the pic_details */
621 get_pic_from_stack(
622 rate_control_api->ps_pic_handling,
623 pi4_pic_id,
624 pi4_pic_disp_order_no,
625 pe_pic_type,
626 pi4_is_scd);
627 }
628
629 /****************************************************************************
630 Function Name : get_min_max_bits_based_on_buffer
631 Description :
632 Inputs : ps_rate_control_api
633
634 Globals :
635 Processing :
636 Outputs :
637 Returns :
638 Issues :
639 Revision History:
640 DD MM YYYY Author(s) Changes (Describe the changes made)
641
642 *****************************************************************************/
get_min_max_bits_based_on_buffer(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type,WORD32 * pi4_min_bits,WORD32 * pi4_max_bits,WORD32 i4_get_error)643 static void get_min_max_bits_based_on_buffer(
644 rate_control_api_t *ps_rate_control_api,
645 picture_type_e e_pic_type,
646 WORD32 *pi4_min_bits,
647 WORD32 *pi4_max_bits,
648 WORD32 i4_get_error)
649 {
650 WORD32 i4_min_bits = 0, i4_max_bits = 0;
651
652 cbr_modify_ebf_estimate(ps_rate_control_api->ps_cbr_buffer, i4_get_error); //ELP_RC
653
654 /* Find the min and max bits that can be consumed based on the buffer condition */
655 if(ps_rate_control_api->e_rc_type == VBR_STORAGE)
656 {
657 i4_max_bits = get_max_target_bits(ps_rate_control_api->ps_vbr_storage_vbv);
658 }
659 else if(ps_rate_control_api->e_rc_type == VBR_STORAGE_DVD_COMP)
660 {
661 WORD32 i4_rem_bits_in_gop, i4_rem_frms_in_gop;
662 /* WORD32 ai4_rem_frms_in_gop[MAX_PIC_TYPE]; */
663 i4_rem_frms_in_gop = pic_type_get_rem_frms_in_gop(ps_rate_control_api->ps_pic_handling);
664 i4_rem_bits_in_gop = rc_get_rem_bits_in_period(ps_rate_control_api);
665
666 i4_max_bits = get_max_tgt_bits_dvd_comp(
667 ps_rate_control_api->ps_vbr_storage_vbv,
668 i4_rem_bits_in_gop,
669 i4_rem_frms_in_gop,
670 e_pic_type);
671 }
672 else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
673 {
674 cbr_buffer_constraint_check(
675 ps_rate_control_api->ps_cbr_buffer, 0, e_pic_type, &i4_max_bits, &i4_min_bits);
676 }
677 else /* if(ps_rate_control_api->e_rc_type == VBR_STREAMING) */
678 {
679 vbr_stream_buffer_constraint_check(
680 ps_rate_control_api->ps_cbr_buffer, 0, e_pic_type, &i4_max_bits, &i4_min_bits);
681 }
682 /* Fill the min and max bits consumed */
683 if(1 != ps_rate_control_api->i4_capped_vbr_flag)
684 {
685 pi4_min_bits[0] = i4_min_bits;
686 }
687 else
688 {
689 /* Capped VBR case */
690 pi4_min_bits[0] = 0;
691 }
692 pi4_max_bits[0] = i4_max_bits;
693 }
694
695 /****************************************************************************
696 Function Name : is_first_frame_coded
697 Description :
698 Inputs : ps_rate_control_api
699 Revision History:
700 DD MM YYYY Author(s) Changes (Describe the changes made)
701
702 *****************************************************************************/
is_first_frame_coded(rate_control_handle ps_rate_control_api)703 WORD32 is_first_frame_coded(rate_control_handle ps_rate_control_api)
704 {
705 WORD32 i4_is_first_frame_coded = 1, i;
706 /* Check whether atleast one frame of a each picture type gets encoded */
707 /* Check whether it is an IPP or IPB kind of encoding */
708 if(pic_type_get_intra_frame_interval(ps_rate_control_api->ps_pic_handling) == 1)
709 {
710 i4_is_first_frame_coded = ps_rate_control_api->au1_is_first_frm_coded[I_PIC];
711 }
712 else /*HEVC_hierarchy*/
713 {
714 if(pic_type_get_field_pic(ps_rate_control_api->ps_pic_handling))
715 {
716 i4_is_first_frame_coded &= ps_rate_control_api->au1_is_first_frm_coded[I_PIC];
717
718 for(i = 1; i < ps_rate_control_api->i4_num_active_pic_type; i++)
719 {
720 i4_is_first_frame_coded &= ps_rate_control_api->au1_is_first_frm_coded[i];
721 i4_is_first_frame_coded &=
722 ps_rate_control_api->au1_is_first_frm_coded[i + FIELD_OFFSET];
723 }
724 }
725 else
726 {
727 for(i = 0; i < ps_rate_control_api->i4_num_active_pic_type; i++)
728 {
729 i4_is_first_frame_coded &= ps_rate_control_api->au1_is_first_frm_coded[i];
730 }
731 }
732 }
733
734 return i4_is_first_frame_coded;
735 }
736
737 /****************************************************************************
738 Function Name : get_min_max_qp
739 Description :
740 Inputs : ps_rate_control_api
741 Revision History:
742 DD MM YYYY Author(s) Changes (Describe the changes made)
743
744 *****************************************************************************/
745
get_min_max_qp(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type,WORD32 * pi4_hi_dev_qp_q6,WORD32 * pi4_lo_dev_qp_q6,WORD32 i4_complexity_bin,WORD32 i4_scene_num)746 static void get_min_max_qp(
747 rate_control_api_t *ps_rate_control_api,
748 picture_type_e e_pic_type,
749 WORD32 *pi4_hi_dev_qp_q6,
750 WORD32 *pi4_lo_dev_qp_q6,
751 WORD32 i4_complexity_bin,
752 WORD32 i4_scene_num)
753 {
754 WORD32 prev_qp_q6, prev_I_qp_q6;
755 WORD32 hi_dev_qp_q6, lo_dev_qp_q6, hi_dev_qp_temp_q6;
756 WORD32 i4_intra_frm_int, prev_qp_for_high_dev_q6,
757 use_I_frame_qp_high_dev = 0; /*i_only : to detect i only case*/
758 float per_pixel_p_hme_sad =
759 (float)ps_rate_control_api->i8_per_pixel_p_frm_hme_sad_q10 / (1 << 10);
760
761 i4_intra_frm_int = pic_type_get_intra_frame_interval(ps_rate_control_api->ps_pic_handling);
762
763 /* Restricting the Quant swing */
764 prev_qp_q6 = ps_rate_control_api
765 ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type];
766 prev_qp_for_high_dev_q6 = prev_qp_q6;
767 prev_I_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC];
768 if(ps_rate_control_api->prev_ref_pic_type != e_pic_type)
769 {
770 if(e_pic_type == I_PIC)
771 {
772 /* Constrain I-frame QP to be within specified limit of prev_ref_qp/Kp */
773 // SS - suppressing this assuming re-encode will take care
774 /* prev_qp = i4_frame_qp; */
775 prev_qp_q6 = (ps_rate_control_api->i4_P_to_I_ratio * (LWORD64)prev_qp_q6) >>
776 P_TO_I_RATIO_Q_FACTOR;
777 }
778 else if(e_pic_type == P_PIC || e_pic_type == P1_PIC)
779 {
780 /* Constrain P-frame QP to be within specified limit of Kp*prev_ref_qp */
781 prev_qp_q6 = (I_TO_P_RATIO * (LWORD64)prev_qp_q6) >> K_Q;
782 use_I_frame_qp_high_dev = 1;
783 }
784 else if(ps_rate_control_api->prev_ref_pic_type == P_PIC)
785 {
786 /* current frame is B-pic */
787 /* Constrain B-frame QP to be within specified limit of prev_ref_qp/Kb */
788 if(!ps_rate_control_api->i4_is_hbr)
789 {
790 prev_qp_q6 = (P_TO_B_RATIO * (LWORD64)prev_qp_q6) >> (K_Q);
791 }
792 else
793 {
794 prev_qp_q6 = (P_TO_B_RATIO_HBR * (LWORD64)prev_qp_q6) >> (K_Q);
795 }
796 }
797 else /* if(ps_rate_control_api->prev_ref_pic_type == I_PIC) */
798 {
799 /* current frame is B-pic */
800 /* Constrain B-frame QP to be within specified limit of prev_ref_qp/Kb */
801 if(!ps_rate_control_api->i4_is_hbr)
802 {
803 prev_qp_q6 = (P_TO_B_RATIO * I_TO_P_RATIO * (LWORD64)prev_qp_q6) >> (K_Q + K_Q);
804 }
805 else
806 {
807 prev_qp_q6 = (P_TO_B_RATIO_HBR * I_TO_P_RATIO * (LWORD64)prev_qp_q6) >> (K_Q + K_Q);
808 }
809 }
810 }
811
812 /*if (1)//e_pic_type != B_PIC)*/
813 {
814 if(use_I_frame_qp_high_dev)
815 {
816 /*For P pic if previous reference was I then pre_qp = I qp + 1, Then +4 high dev is allowed. To avoid P frame to be +5 off comapared to previous I*/
817 hi_dev_qp_q6 = GET_HI_DEV_QP_QFAC(prev_qp_for_high_dev_q6);
818 }
819 else
820 {
821 hi_dev_qp_q6 = GET_HI_DEV_QP_QFAC(prev_qp_q6);
822 }
823
824 if(e_pic_type == I_PIC || e_pic_type == P_PIC || e_pic_type == P1_PIC)
825 {
826 lo_dev_qp_q6 =
827 GET_LO_DEV_QP_QFAC(prev_qp_q6, ps_rate_control_api->i4_num_active_pic_type);
828 }
829 else
830 {
831 lo_dev_qp_q6 = GET_LO_DEV_QP_QFAC_B_PIC(prev_qp_q6);
832 }
833 }
834 /* For lower QPs due to scale factor and fixed point arithmetic, the
835 hi_dev_qp can be same as that of the prev qp and in which case it gets stuck
836 in the lower most qp and thus not allowing QPs not to change. To avoid this,
837 for lower qps the hi_dev_qp should be made slightly more than prev_qp */
838 if(prev_qp_q6 == hi_dev_qp_q6)
839 {
840 hi_dev_qp_q6 = ((LWORD64)hi_dev_qp_q6 * 18) >> 4;
841 }
842 /*minimum qp should atleast be 1 less than previous*/
843 if(prev_qp_q6 == lo_dev_qp_q6 && lo_dev_qp_q6 > (1 << QSCALE_Q_FAC))
844 {
845 lo_dev_qp_q6 = ((LWORD64)lo_dev_qp_q6 * 14) >> 4;
846 }
847 /*for shorter GOP make sure the P does not get better than I , NEED TO BE REVIEWED as gains seen in bq terrace after this change was with wrong config*/
848 /*Anything with per pixel sad < 1 is considered static. Since the hme sad is at L1 resolution, the threshold chosen is 0.25*/
849 if((per_pixel_p_hme_sad < 0.25f) && (ps_rate_control_api->i4_is_infinite_gop != 1))
850 {
851 if(e_pic_type == P_PIC && ps_rate_control_api->i4_I_frame_qp_model)
852 {
853 /*P is not allowed to get too better compared to previous I in static content*/
854 if(lo_dev_qp_q6<(prev_I_qp_q6 * 14)>> 4)
855 lo_dev_qp_q6 = ((LWORD64)prev_I_qp_q6 * 14) >> 4;
856 /*If previous reference is I then it cannot get better than I in static case*/
857 if(lo_dev_qp_q6 < prev_I_qp_q6)
858 lo_dev_qp_q6 = prev_I_qp_q6;
859 }
860 }
861 if(e_pic_type == I_PIC &&
862 i4_intra_frm_int !=
863 1) /*i_only: In this case P frame Qp will be arbitrary value hence avoiding max_dev_qp to be independent of it*/
864 {
865 //WORD32 i4_p_qp = ps_rate_control_api->ai4_prev_frm_qp[P_PIC];
866 WORD32 i4_p_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][P_PIC];
867 switch(i4_complexity_bin)
868 {
869 case 0:
870 hi_dev_qp_temp_q6 = (WORD32)(
871 ((LWORD64)i4_p_qp_q6 * I_TO_P_RATIO * I_TO_P_RATIO * I_TO_P_RATIO) >>
872 (K_Q + K_Q + K_Q));
873 break;
874 case 1:
875 hi_dev_qp_temp_q6 =
876 (WORD32)(((LWORD64)i4_p_qp_q6 * I_TO_P_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q));
877 break;
878 case 2:
879 hi_dev_qp_temp_q6 = (WORD32)(((LWORD64)i4_p_qp_q6 * I_TO_P_RATIO) >> (K_Q));
880 break;
881 case 3:
882 hi_dev_qp_temp_q6 = i4_p_qp_q6;
883 break;
884 default:
885 hi_dev_qp_temp_q6 = (WORD32)(((LWORD64)i4_p_qp_q6 * P_TO_I_RATIO) >> (K_Q));
886 break;
887 }
888 hi_dev_qp_q6 = (hi_dev_qp_q6 > hi_dev_qp_temp_q6) ? hi_dev_qp_temp_q6 : hi_dev_qp_q6;
889 }
890 pi4_hi_dev_qp_q6[0] = hi_dev_qp_q6;
891 pi4_lo_dev_qp_q6[0] = lo_dev_qp_q6;
892 }
893
894 /****************************************************************************
895 Function Name : get_min
896 Description :
897 Inputs : ps_rate_control_api
898 Revision History:
899 DD MM YYYY Author(s) Changes (Describe the changes made)
900
901 *****************************************************************************/
get_min(WORD32 a,WORD32 b,WORD32 c,WORD32 d)902 static WORD32 get_min(WORD32 a, WORD32 b, WORD32 c, WORD32 d)
903 {
904 WORD32 min = a;
905 if(b < min)
906 min = b;
907 if(c < min)
908 min = c;
909 if(d < min)
910 min = d;
911 return min;
912 }
913
914 /****************************************************************************
915 Function Name : get_max
916 Description :
917 Inputs : ps_rate_control_api
918 Revision History:
919 DD MM YYYY Author(s) Changes (Describe the changes made)
920
921 *****************************************************************************/
get_max(WORD32 a,WORD32 b,WORD32 c)922 static WORD32 get_max(WORD32 a, WORD32 b, WORD32 c)
923 {
924 WORD32 max = a;
925 if(b > max)
926 max = b;
927 if(c > max)
928 max = c;
929 return max;
930 }
931 /****************************************************************************
932 Function Name : rc_modify_est_tot
933 Description : Adds latest Estimated total bits to the loop .
934 Inputs :
935 Globals :
936 Processing :
937 Outputs :
938 Returns :
939 Issues :
940 Revision History:
941 DD MM YYYY Author(s) Changes (Describe the changes made)
942
943 *****************************************************************************/
rc_modify_est_tot(rate_control_api_t * ps_rate_control_api,WORD32 i4_tot_est_bits)944 void rc_modify_est_tot(rate_control_api_t *ps_rate_control_api, WORD32 i4_tot_est_bits) //ELP_RC
945 {
946 WORD32 i4_num_frm_parallel, i;
947 i4_num_frm_parallel = ps_rate_control_api->i4_num_frame_parallel;
948
949 if(i4_num_frm_parallel) //for CPU i4_num_frm_parallel=0
950 {
951 for(i = 1; i < (i4_num_frm_parallel - 1); i++)
952 {
953 ps_rate_control_api->ai4_est_tot_bits[i - 1] = ps_rate_control_api->ai4_est_tot_bits[i];
954 }
955 ps_rate_control_api->ai4_est_tot_bits[i - 1] = i4_tot_est_bits;
956 }
957 }
958 /****************************************************************************
959 Function Name : rc_get_estimate_bit_error
960 Description : function returns the estimated bit error using estimated total
961 bits for the Enc Loop Parallelism based Encoder.
962 Inputs :
963 Globals :
964 Processing :
965 Outputs :
966 Returns :
967 Issues :
968 Revision History:
969 DD MM YYYY Author(s) Changes (Describe the changes made)
970
971 *****************************************************************************/
rc_get_estimate_bit_error(rate_control_api_t * ps_rate_control_api)972 static WORD32 rc_get_estimate_bit_error(rate_control_api_t *ps_rate_control_api)
973 {
974 WORD32 i4_error_bits = 0, i, i4_bits_per_frame;
975 i4_bits_per_frame = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
976 if(ps_rate_control_api->i4_num_frame_parallel >
977 0) // for CPU ps_rate_control_api->i4_num_frame_parallel =0;
978 {
979 for(i = 0; i < (ps_rate_control_api->i4_num_frame_parallel - 1); i++)
980 {
981 i4_error_bits += (ps_rate_control_api->ai4_est_tot_bits[i] - i4_bits_per_frame);
982 }
983 }
984 return i4_error_bits;
985 }
986
987 /****************************************************************************
988 Function Name : get_est_hdr_bits
989 Description :
990 Inputs : ps_rate_control_api
991 Revision History:
992 DD MM YYYY Author(s) Changes (Describe the changes made)
993
994 *****************************************************************************/
get_est_hdr_bits(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type)995 WORD32 get_est_hdr_bits(rate_control_api_t *ps_rate_control_api, picture_type_e e_pic_type)
996 {
997 return (get_cur_frm_est_header_bits(ps_rate_control_api->ps_bit_allocation, e_pic_type));
998 }
999
1000 /****************************************************************************
1001 Function Name : model_availability
1002 Description :
1003 Inputs : ps_rate_control_api
1004 Revision History:
1005 DD MM YYYY Author(s) Changes (Describe the changes made)
1006
1007 *****************************************************************************/
model_availability(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type)1008 WORD32 model_availability(rate_control_api_t *ps_rate_control_api, picture_type_e e_pic_type)
1009 {
1010 return (is_model_valid(ps_rate_control_api->aps_rd_model[e_pic_type]));
1011 }
1012
1013 /****************************************************************************
1014 Function Name : clip_qp_based_on_prev_ref
1015 Description :
1016 Inputs : ps_rate_control_api
1017 Revision History:
1018 DD MM YYYY Author(s) Changes (Describe the changes made)
1019
1020 *****************************************************************************/
clip_qp_based_on_prev_ref(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type,WORD32 i4_call_type,WORD32 i4_scene_num)1021 WORD32 clip_qp_based_on_prev_ref(
1022 rate_control_api_t *ps_rate_control_api,
1023 picture_type_e e_pic_type,
1024 WORD32 i4_call_type,
1025 WORD32 i4_scene_num)
1026 {
1027 /* WORD32 i4_bpp_based_qp; */
1028 /* If the number pf pels is set to zero it uses the value set during init time */
1029 /* i4_frame_qp = get_init_qp_using_pels_bits_per_frame(ps_rate_control_api->ps_init_qp,
1030 e_pic_type, i4_est_tex_bits, 0); */
1031 WORD32 i4_frame_qp, i4_frame_qp_q6 = 0, i4_min_Kp_Kb_factor = 0;
1032 WORD32 Kp_kb_factor = get_Kp_Kb(ps_rate_control_api->ps_bit_allocation, e_pic_type);
1033 WORD32 kp_kb_ref_ref =
1034 get_Kp_Kb(ps_rate_control_api->ps_bit_allocation, ps_rate_control_api->prev_ref_pic_type);
1035
1036 {
1037 WORD32 i4_drain_bits_per_frame = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer),
1038 i4_ebf;
1039 WORD32 i4_delay = cbr_get_delay_frames(ps_rate_control_api->ps_cbr_buffer),
1040 max_buffer_level = 0, rc_type = get_rc_type(ps_rate_control_api->ps_cbr_buffer);
1041
1042 if(rc_type == VBR_STREAMING)
1043 max_buffer_level = i4_drain_bits_per_frame * i4_delay;
1044 else
1045 max_buffer_level = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
1046
1047 i4_ebf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer);
1048
1049 if(i4_ebf > (WORD32)(0.9f * max_buffer_level))
1050 {
1051 switch(e_pic_type)
1052 {
1053 case P_PIC:
1054 case P1_PIC:
1055 i4_min_Kp_Kb_factor = I_TO_P_RATIO;
1056 break;
1057 case B_PIC:
1058 case BB_PIC:
1059 i4_min_Kp_Kb_factor = I_TO_B_RATIO;
1060 break;
1061 case B1_PIC:
1062 case B11_PIC:
1063 i4_min_Kp_Kb_factor = I_TO_B1_RATIO;
1064 break;
1065 default:
1066 i4_min_Kp_Kb_factor = I_TO_B2_RATIO;
1067 break;
1068 }
1069 }
1070 }
1071 if((e_pic_type == I_PIC) &&
1072 (ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] == 0x7FFFFFFF))
1073 {
1074 /*Is this a valid case?*/
1075 ASSERT(0);
1076 }
1077 /*If there is a scene cut I frame followed by a scene cut I frame, non scene cut I frame
1078 better assume the Qp of the I frame same as before instead of using bpp based qp*/
1079 else if(
1080 (e_pic_type == I_PIC) &&
1081 (ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] != 0x7FFFFFFF))
1082 {
1083 i4_frame_qp = ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC];
1084 i4_frame_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC];
1085 }
1086 else /*! ISlice*/
1087 {
1088 if((Kp_kb_factor < i4_min_Kp_Kb_factor) && (i4_call_type == 1))
1089 {
1090 Kp_kb_factor = i4_min_Kp_Kb_factor;
1091 trace_printf("Kp_kb_factor %d", Kp_kb_factor);
1092 }
1093 if((kp_kb_ref_ref > Kp_kb_factor) && (i4_call_type == 1))
1094 {
1095 kp_kb_ref_ref = Kp_kb_factor;
1096 }
1097
1098 if(ps_rate_control_api
1099 ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] ==
1100 0x7FFFFFFF)
1101 {
1102 ps_rate_control_api
1103 ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] =
1104 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC];
1105 kp_kb_ref_ref = 16;
1106 }
1107
1108 i4_frame_qp_q6 =
1109 ((ps_rate_control_api
1110 ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] *
1111 Kp_kb_factor) /
1112 kp_kb_ref_ref);
1113 }
1114 return i4_frame_qp_q6;
1115 }
1116
1117 /****************************************************************************
1118 Function Name : get_frame_level_qp
1119 Description : Get frame qp from the estimated bits
1120 Inputs : ps_rate_control_api
1121 i_to_avg_ratio
1122
1123 Revision History:
1124 DD MM YYYY Author(s) Changes (Describe the changes made)
1125
1126 *****************************************************************************/
get_frame_level_qp(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type,WORD32 i4_ud_max_bits,WORD32 * pi4_cur_est_texture_bits,float af_sum_weigh[MAX_PIC_TYPE][3],WORD32 i4_call_type,float i_to_avg_ratio,frame_info_t * ps_frame_stat,WORD32 i4_complexity_bin,WORD32 i4_scene_num,WORD32 * pi4_tot_bits_estimated,WORD32 * pi4_is_model_valid,WORD32 * pi4_vbv_buf_max_bits,WORD32 * pi4_est_tex_bits,WORD32 * pi4_cur_est_header_bits,WORD32 * pi4_maxEbfQP,WORD32 * pi4_modelQP,WORD32 * pi4_estimate_to_calc_frm_error)1127 WORD32 get_frame_level_qp(
1128 rate_control_api_t *ps_rate_control_api,
1129 picture_type_e e_pic_type,
1130 WORD32 i4_ud_max_bits,
1131 WORD32 *pi4_cur_est_texture_bits,
1132 float af_sum_weigh[MAX_PIC_TYPE][3],
1133 WORD32 i4_call_type,
1134 float i_to_avg_ratio,
1135 frame_info_t *ps_frame_stat,
1136 WORD32 i4_complexity_bin,
1137 WORD32 i4_scene_num,
1138 WORD32 *pi4_tot_bits_estimated,
1139 WORD32 *pi4_is_model_valid,
1140 WORD32 *pi4_vbv_buf_max_bits,
1141 WORD32 *pi4_est_tex_bits,
1142 WORD32 *pi4_cur_est_header_bits,
1143 WORD32 *pi4_maxEbfQP,
1144 WORD32 *pi4_modelQP,
1145 WORD32 *pi4_estimate_to_calc_frm_error)
1146 {
1147 /* UWORD8 u1_frame_qp; */
1148 WORD32 i4_frame_qp /*,i4_min_frame_qp = 1,i4_max_frame_qp = MAX_MPEG2_QP*/;
1149 WORD32 i4_max_frame_qp_q6 = (MAX_MPEG2_QP << QSCALE_Q_FAC),
1150 i4_min_frame_qp_q6 = MIN_QSCALE_Q6; /*0.707 in q6 corresponds to hevc qp = 1*/
1151 WORD32 i4_is_first_frame_coded = 1;
1152 WORD32 i4_is_model_valid = 0;
1153 WORD32 i4_frame_qp_q6, i4_cur_est_header_bits, i4_frame_qp_q6_based_max_vbv_bits;
1154 WORD32 i4_bit_alloc_est_tex_bits = 0, i4_bit_alloc_est_tex_bits_for_invalid_model = 0,
1155 i4_est_tex_bits, i4_qp_based_min_est_tex_bits, i4_qp_based_max_est_tex_bits,
1156 i4_buf_based_min_bits, i4_buf_based_max_bits;
1157 UWORD32 u4_estimated_sad;
1158 WORD32 i4_buffer_based_max_qp_clip_flag = 0;
1159 WORD32 i4_min_Kp_Kb_factor = 0;
1160 WORD32 i4_steady_state_texture_case = 0;
1161
1162 if(i4_call_type == 1)
1163 {
1164 *pi4_maxEbfQP = INVALID_QP;
1165 *pi4_modelQP = INVALID_QP;
1166 }
1167
1168 if((ps_rate_control_api->e_rc_type != VBR_STORAGE) &&
1169 (ps_rate_control_api->e_rc_type != VBR_STORAGE_DVD_COMP) &&
1170 (ps_rate_control_api->e_rc_type != CBR_NLDRC) &&
1171 (ps_rate_control_api->e_rc_type != CONST_QP) &&
1172 (ps_rate_control_api->e_rc_type != VBR_STREAMING))
1173 {
1174 return (0);
1175 }
1176
1177 i4_is_first_frame_coded = is_first_frame_coded(ps_rate_control_api);
1178
1179 assign_complexity_coeffs(ps_rate_control_api->ps_bit_allocation, af_sum_weigh);
1180
1181 if(ps_rate_control_api->e_rc_type == CONST_QP)
1182 {
1183 i4_frame_qp = ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][e_pic_type];
1184 i4_frame_qp_q6 =
1185 (ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][e_pic_type] >> QSCALE_Q_FAC);
1186 }
1187 else
1188 {
1189 i4_cur_est_header_bits =
1190 get_cur_frm_est_header_bits(ps_rate_control_api->ps_bit_allocation, e_pic_type);
1191 u4_estimated_sad = get_est_sad(ps_rate_control_api->ps_est_sad, e_pic_type);
1192 /* Constraining the qp variations based on bits allocated */
1193 /* Step 1: Getting the bits based on bit allocation module */
1194 /*check if model has atleast one data point, otherwise go with default qp*/
1195 i4_is_model_valid = is_model_valid(ps_rate_control_api->aps_rd_model[e_pic_type]);
1196
1197 if(i4_is_model_valid == 1)
1198 {
1199 i4_bit_alloc_est_tex_bits = get_cur_frm_est_texture_bits(
1200 ps_rate_control_api->ps_bit_allocation,
1201 ps_rate_control_api->aps_rd_model,
1202 ps_rate_control_api->ps_est_sad,
1203 ps_rate_control_api->ps_pic_handling,
1204 ps_rate_control_api->ps_cbr_buffer,
1205 e_pic_type,
1206 i4_is_first_frame_coded,
1207 0,
1208 i4_call_type,
1209 i_to_avg_ratio,
1210 i4_is_model_valid);
1211 if(i4_call_type == 1)
1212 {
1213 *pi4_estimate_to_calc_frm_error =
1214 i4_bit_alloc_est_tex_bits + i4_cur_est_header_bits;
1215 }
1216
1217 /* vbv buffer position based error correction to keep away encoder buffer overflow at layer 0 pictures*/
1218 if(e_pic_type == I_PIC || e_pic_type == P_PIC || e_pic_type == P1_PIC)
1219 {
1220 WORD32 i4_cur_ebf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer);
1221 WORD32 i4_vbv_size = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
1222 WORD32 i4_max_ebf = (WORD32)(i4_vbv_size * MAX_THRESHOLD_VBV_FRM_ERROR);
1223 WORD32 i4_drain_rate = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
1224 WORD32 i4_total_bits_allocted = i4_bit_alloc_est_tex_bits + i4_cur_est_header_bits;
1225 WORD32 i4_total_bits_to_be_alloc;
1226 WORD32 i4_expected_ebf = (i4_cur_ebf + i4_total_bits_allocted - i4_drain_rate);
1227 /*if expected ebf is greater than max threashold, correct the allocation such that it never cross max
1228 but if it less than drain rate, atleast give drainrate bits*/
1229 if(i4_expected_ebf > i4_max_ebf)
1230 {
1231 i4_total_bits_to_be_alloc = MAX(
1232 i4_drain_rate, (i4_total_bits_allocted - (i4_expected_ebf - i4_max_ebf)));
1233 i4_bit_alloc_est_tex_bits = i4_total_bits_to_be_alloc - i4_cur_est_header_bits;
1234 }
1235 }
1236 }
1237 else
1238 {
1239 i4_bit_alloc_est_tex_bits_for_invalid_model = get_cur_frm_est_texture_bits(
1240 ps_rate_control_api->ps_bit_allocation,
1241 ps_rate_control_api->aps_rd_model,
1242 ps_rate_control_api->ps_est_sad,
1243 ps_rate_control_api->ps_pic_handling,
1244 ps_rate_control_api->ps_cbr_buffer,
1245 e_pic_type,
1246 i4_is_first_frame_coded,
1247 0,
1248 i4_call_type,
1249 i_to_avg_ratio,
1250 i4_is_model_valid);
1251 if(i4_call_type == 1)
1252 {
1253 *pi4_estimate_to_calc_frm_error =
1254 i4_bit_alloc_est_tex_bits_for_invalid_model + i4_cur_est_header_bits;
1255 }
1256 }
1257
1258 #if 1 /*model_low_bitrate_bug*/
1259 /* This condition is added to use the model for cases when the estimated bits is less than zero.
1260 We assume some bits of the header are used for texture and calcualte the qp */
1261 if(i4_bit_alloc_est_tex_bits <= (i4_cur_est_header_bits >> 3))
1262 {
1263 i4_bit_alloc_est_tex_bits = (i4_cur_est_header_bits >> 3);
1264 }
1265 #endif
1266
1267 /* Step 2: Getting the min and max texture bits based on min and max qp */
1268 if(i4_is_model_valid && ps_rate_control_api->au1_avg_bitrate_changed[e_pic_type] == 0)
1269 {
1270 WORD32 /*i4_min_qp, i4_max_qp,*/ i4_max_qp_q6, i4_min_qp_q6;
1271 number_t s_lin_coeff_wo_int =
1272 get_linear_coefficient(ps_rate_control_api->aps_rd_model[e_pic_type]);
1273
1274 if(s_lin_coeff_wo_int.sm != 0)
1275 {
1276 /* Get the min and max qp deviation allowed based on prev frame qp */
1277 get_min_max_qp(
1278 ps_rate_control_api,
1279 e_pic_type,
1280 &i4_max_qp_q6,
1281 &i4_min_qp_q6,
1282 i4_complexity_bin,
1283 i4_scene_num);
1284
1285 /* Estimate the max bits based on min qp */
1286 i4_qp_based_min_est_tex_bits = estimate_bits_for_qp(
1287 ps_rate_control_api->aps_rd_model[e_pic_type], u4_estimated_sad, i4_max_qp_q6);
1288 /* Estimate the min bits based on max qp */
1289 i4_qp_based_max_est_tex_bits = estimate_bits_for_qp(
1290 ps_rate_control_api->aps_rd_model[e_pic_type], u4_estimated_sad, i4_min_qp_q6);
1291 /*disable qp based min and max swing restriction*/
1292 i4_min_frame_qp_q6 = i4_min_qp_q6;
1293 i4_max_frame_qp_q6 = i4_max_qp_q6;
1294 i4_qp_based_max_est_tex_bits = i4_bit_alloc_est_tex_bits;
1295 i4_qp_based_min_est_tex_bits = i4_bit_alloc_est_tex_bits;
1296 }
1297 else
1298 {
1299 i4_qp_based_min_est_tex_bits = i4_bit_alloc_est_tex_bits;
1300 i4_qp_based_max_est_tex_bits = i4_bit_alloc_est_tex_bits;
1301 }
1302 }
1303 else
1304 {
1305 i4_qp_based_min_est_tex_bits = i4_bit_alloc_est_tex_bits_for_invalid_model;
1306 i4_qp_based_max_est_tex_bits = i4_bit_alloc_est_tex_bits_for_invalid_model;
1307 ps_rate_control_api->au1_avg_bitrate_changed[e_pic_type] = 0;
1308 }
1309
1310 /* Step 3: Getting the min and max texture bits based on buffer fullness */
1311
1312 if(i4_call_type == 1)
1313 {
1314 WORD32 i4_get_error;
1315
1316 i4_get_error = rc_get_estimate_bit_error(ps_rate_control_api);
1317
1318 get_min_max_bits_based_on_buffer(
1319 ps_rate_control_api,
1320 e_pic_type,
1321 &i4_buf_based_min_bits,
1322 &i4_buf_based_max_bits,
1323 i4_get_error);
1324
1325 /*In case buffer limitation will come, no need to reduce the QP further because of warning flag*/
1326 if(i4_bit_alloc_est_tex_bits < (i4_buf_based_min_bits - i4_cur_est_header_bits))
1327 ps_rate_control_api->i4_underflow_warning = 0;
1328
1329 if(i4_buf_based_max_bits < (i4_bit_alloc_est_tex_bits + i4_cur_est_header_bits))
1330 {
1331 i4_buffer_based_max_qp_clip_flag = 1;
1332 }
1333 trace_printf(
1334 "i4_buf_based_min_bits %d i4_buf_based_max_bits %d",
1335 i4_buf_based_min_bits,
1336 i4_buf_based_max_bits);
1337 trace_printf(
1338 "Prev I frame qp q6 %d P frame qp q6 %d",
1339 ps_rate_control_api->ai4_prev_frm_qp_q6[I_PIC],
1340 ps_rate_control_api->ai4_prev_frm_qp_q6[P_PIC]);
1341 }
1342 else
1343 {
1344 i4_buf_based_min_bits = i4_qp_based_min_est_tex_bits;
1345 i4_buf_based_max_bits = i4_qp_based_max_est_tex_bits;
1346 }
1347 /* for I frame the max bits is not restricted based on the user input */
1348 if(e_pic_type == I_PIC)
1349 {
1350 i4_ud_max_bits = 0x7fffffff; /* i4_bit_alloc_est_tex_bits + i4_cur_est_header_bits; */
1351 }
1352
1353 /* Step 4: Clip the bits allocated based on
1354 1) FinalBits = Max of (BitallocBits, MinBitsMaxQp, MinBufferBits)
1355 2) FinalBits = Min of (MaxBitsMinQp, MaxBufferBits, MaxUserDefBits, FinalBits)
1356 Note that max is done after min to prevent over-consumption */
1357 /* Finding the max of all the minimum bits */
1358 i4_est_tex_bits = get_max(
1359 i4_bit_alloc_est_tex_bits,
1360 i4_qp_based_min_est_tex_bits,
1361 (i4_buf_based_min_bits - i4_cur_est_header_bits));
1362 i4_est_tex_bits = get_min(
1363 i4_est_tex_bits,
1364 i4_qp_based_max_est_tex_bits,
1365 (i4_ud_max_bits - i4_cur_est_header_bits),
1366 (i4_buf_based_max_bits - i4_cur_est_header_bits));
1367
1368 /*Highest priority given to min and max qp followed by buffer based min and max to prevent overconsumption in process of preventing stuffing*/
1369 CLIP(
1370 i4_est_tex_bits,
1371 i4_buf_based_max_bits - i4_cur_est_header_bits,
1372 i4_buf_based_min_bits - i4_cur_est_header_bits);
1373
1374 {
1375 WORD32 i4_drain_bits_per_frame =
1376 get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer),
1377 i4_ebf;
1378 WORD32 i4_delay = cbr_get_delay_frames(ps_rate_control_api->ps_cbr_buffer),
1379 max_buffer_level = 0, rc_type = get_rc_type(ps_rate_control_api->ps_cbr_buffer);
1380
1381 if(rc_type == VBR_STREAMING)
1382 max_buffer_level = i4_drain_bits_per_frame * i4_delay;
1383 else
1384 max_buffer_level = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
1385
1386 i4_ebf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer);
1387
1388 if(i4_ebf > (WORD32)(0.9f * max_buffer_level))
1389 {
1390 i4_buffer_based_max_qp_clip_flag = 1;
1391 switch(e_pic_type)
1392 {
1393 case P_PIC:
1394 case P1_PIC:
1395 i4_min_Kp_Kb_factor = I_TO_P_RATIO;
1396 break;
1397 case B_PIC:
1398 case BB_PIC:
1399 i4_min_Kp_Kb_factor = I_TO_B_RATIO;
1400 break;
1401 case B1_PIC:
1402 case B11_PIC:
1403 i4_min_Kp_Kb_factor = I_TO_B1_RATIO;
1404 break;
1405 default:
1406 i4_min_Kp_Kb_factor = I_TO_B2_RATIO;
1407 break;
1408 }
1409 }
1410 }
1411 /*i4_is_first_frame_coded will be considered only in 2 pass, since 2 pass precise I to rest is calcuated considering first sug-gop and full sub-gop complexity separately. Using offset based
1412 qp instead of single frame model(with default bit allocation)*/
1413 /* Step 6: Estimate the qp generated for the given texture bits */
1414 if((!i4_is_first_frame_coded /* && ps_rate_control_api->i4_rc_pass == 2*/) ||
1415 !i4_is_model_valid) //ELP_RC
1416 {
1417 /* WORD32 i4_bpp_based_qp; */
1418 /* If the number pf pels is set to zero it uses the value set during init time */
1419 /* i4_frame_qp = get_init_qp_using_pels_bits_per_frame(ps_rate_control_api->ps_init_qp,
1420 e_pic_type, i4_est_tex_bits, 0); */
1421 WORD32 Kp_kb_factor = get_Kp_Kb(ps_rate_control_api->ps_bit_allocation, e_pic_type);
1422 WORD32 kp_kb_ref_ref = get_Kp_Kb(
1423 ps_rate_control_api->ps_bit_allocation, ps_rate_control_api->prev_ref_pic_type);
1424
1425 if(e_pic_type == I_PIC &&
1426 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] == 0x7FFFFFFF)
1427 {
1428 /*Is this a valid case?*/
1429 ASSERT(0);
1430 i4_frame_qp = get_init_qp_using_pels_bits_per_frame(
1431 ps_rate_control_api->ps_init_qp, e_pic_type, i4_est_tex_bits, 0);
1432 i4_frame_qp_q6 = i4_frame_qp << QSCALE_Q_FAC;
1433 }
1434 /*If there is a scene cut I frame followed by a scene cut I frame, non scene cut I frame
1435 better assume the Qp of the I frame same as before instead of using bpp based qp*/
1436 else if(
1437 e_pic_type == I_PIC &&
1438 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] != 0x7FFFFFFF)
1439 {
1440 i4_frame_qp = ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC];
1441 i4_frame_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC];
1442 }
1443 else /*! ISlice*/
1444 {
1445 if((Kp_kb_factor < i4_min_Kp_Kb_factor) && (i4_call_type == 1))
1446 {
1447 Kp_kb_factor = i4_min_Kp_Kb_factor;
1448 trace_printf("Kp_kb_factor %d", Kp_kb_factor);
1449 }
1450 if((kp_kb_ref_ref > Kp_kb_factor) && (i4_call_type == 1))
1451 {
1452 kp_kb_ref_ref = Kp_kb_factor;
1453 }
1454
1455 if(ps_rate_control_api
1456 ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] ==
1457 0x7FFFFFFF)
1458 {
1459 ps_rate_control_api
1460 ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] =
1461 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC];
1462 kp_kb_ref_ref = 16;
1463 }
1464
1465 i4_frame_qp_q6 =
1466 ((ps_rate_control_api
1467 ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] *
1468 Kp_kb_factor) /
1469 kp_kb_ref_ref);
1470 }
1471
1472 /*HEVC_hierarchy: Breaks pause to resume logic if any and also the HBR mode concept as bit ratios are not known. It is now quaranteed that all frames
1473 encoded after scene cut will belong to new scene(B pic of first sub-gop)Hence the below logic of using max of either current estimate
1474 or previous B frame qp is not required*/
1475 /* Since precise SCD position at B-pic level is not known, take the MAX of earlier B-QP and scaled I_QP after SCD */
1476 /*HEVC_RC : Since precise SCD location is known and it is guranteed that pic encoded after I pic belongs to new scene*/
1477
1478 {
1479 WORD32 i4_bits_per_frame;
1480 i4_bits_per_frame = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
1481 if(i4_call_type == 1)
1482 {
1483 rc_modify_est_tot(ps_rate_control_api, i4_bits_per_frame);
1484 }
1485 }
1486 }
1487 /* The check is becaue the model gives a negative QP when the
1488 i4_est_tex_bits is less than or equal to 0
1489 [This is a bug in the model]. As a temporary fix, the frame QP
1490 is being set to the max QP allowed */
1491 else if(i4_est_tex_bits > 0)
1492 {
1493 if(i4_call_type == 1)
1494 {
1495 rc_modify_est_tot(ps_rate_control_api, (i4_est_tex_bits + i4_cur_est_header_bits));
1496 }
1497 i4_steady_state_texture_case = 1;
1498 /* Query the model for the Qp for the corresponding frame*/
1499 i4_frame_qp_q6_based_max_vbv_bits = find_qp_for_target_bits(
1500 ps_rate_control_api->aps_rd_model[e_pic_type],
1501 i4_buf_based_max_bits - i4_cur_est_header_bits,
1502 u4_estimated_sad,
1503 (ps_rate_control_api->ai4_max_qp_q6[e_pic_type]),
1504 (ps_rate_control_api->ai4_min_qp_q6[e_pic_type]));
1505 if(i4_call_type == 1)
1506 {
1507 *pi4_maxEbfQP = ihevce_rc_get_scaled_hevce_qp_q6(
1508 i4_frame_qp_q6_based_max_vbv_bits, ps_rate_control_api->u1_bit_depth);
1509 }
1510 /* Query the model for the Qp for the corresponding frame*/
1511 i4_frame_qp_q6 = find_qp_for_target_bits(
1512 ps_rate_control_api->aps_rd_model[e_pic_type],
1513 i4_est_tex_bits,
1514 u4_estimated_sad,
1515 (ps_rate_control_api->ai4_max_qp_q6[e_pic_type]),
1516 (ps_rate_control_api->ai4_min_qp_q6[e_pic_type]));
1517 i4_frame_qp = ((i4_frame_qp_q6 + (1 << (QSCALE_Q_FAC - 1))) >> QSCALE_Q_FAC);
1518 }
1519 else
1520 {
1521 {
1522 WORD32 i4_bits_per_frame;
1523 i4_bits_per_frame = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
1524 if(i4_call_type == 1)
1525 {
1526 rc_modify_est_tot(ps_rate_control_api, i4_bits_per_frame);
1527 }
1528 }
1529 i4_frame_qp = ps_rate_control_api->ai4_max_qp[e_pic_type];
1530 i4_frame_qp_q6 = ps_rate_control_api->ai4_max_qp_q6[e_pic_type];
1531 }
1532 if(i4_call_type == 1)
1533 {
1534 *pi4_modelQP =
1535 ihevce_rc_get_scaled_hevce_qp_q6(i4_frame_qp_q6, ps_rate_control_api->u1_bit_depth);
1536 }
1537 {
1538 /*This clip is added to prevent the change in qp close to scene cuts i.e even though the buffer
1539 allows the qp to go low the bit alloc model has a problem of having the denominator considering
1540 the previous subgop complexity and giving bits*/
1541 WORD32 i4_clip_flag =
1542 ((i4_call_type == 1) && (i4_is_model_valid == 1) &&
1543 (ps_rate_control_api->i4_rc_pass == 2) &&
1544 (i4_buf_based_max_bits > i4_est_tex_bits));
1545 WORD32 i4_ebf = rc_get_ebf(ps_rate_control_api),
1546 i4_max_ebf = i4_ebf + i4_buf_based_max_bits;
1547 WORD32 i4_inter_frame_interval =
1548 pic_type_get_inter_frame_interval(ps_rate_control_api->ps_pic_handling);
1549 float f_buffer_fullness = (float)i4_ebf / i4_max_ebf;
1550 i4_clip_flag = i4_clip_flag && (ps_rate_control_api->i4_scd_in_period_2_pass == 1);
1551 i4_clip_flag = i4_clip_flag && (i4_ebf < (i4_max_ebf * 0.5f));
1552 i4_clip_flag = i4_clip_flag && (ps_rate_control_api->e_rc_type == VBR_STREAMING);
1553
1554 i4_clip_flag = i4_clip_flag && (ps_rate_control_api->i4_frames_since_last_scd >
1555 i4_inter_frame_interval);
1556
1557 if(i4_clip_flag == 1)
1558 {
1559 WORD32 i4_prev_frame_tot_est_bits = ba_get_prev_frame_tot_est_bits(
1560 ps_rate_control_api->ps_bit_allocation, (WORD32)ps_rate_control_api->e_rc_type);
1561 WORD32 i4_prev_frame_tot_bits = ba_get_prev_frame_tot_bits(
1562 ps_rate_control_api->ps_bit_allocation, (WORD32)ps_rate_control_api->e_rc_type);
1563 float i4_consumption_ratio =
1564 (float)i4_prev_frame_tot_bits / i4_prev_frame_tot_est_bits;
1565 if(i4_consumption_ratio > 0.7f && i4_consumption_ratio < 1.5f)
1566 i4_clip_flag = 1;
1567 else
1568 i4_clip_flag = 0;
1569 }
1570 if(i4_clip_flag == 1)
1571 {
1572 trace_printf("Clipped");
1573 trace_printf("Before %d", i4_frame_qp_q6);
1574 if(af_sum_weigh[e_pic_type][0] > 1.0f)
1575 {
1576 /*Complex followed by simple*/
1577 if(i4_frame_qp_q6 >
1578 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type])
1579 {
1580 if(f_buffer_fullness < 0.3f)
1581 {
1582 i4_frame_qp_q6 =
1583 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type];
1584 }
1585 else
1586 {
1587 if(i4_frame_qp_q6 >
1588 (ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type] *
1589 72 * 3))
1590 i4_frame_qp_q6 =
1591 (ps_rate_control_api
1592 ->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type] *
1593 72 * 3);
1594 }
1595 }
1596 }
1597 if(af_sum_weigh[e_pic_type][0] < 1.0f)
1598 {
1599 /*Simple followed by complex*/
1600 if(i4_frame_qp_q6 <
1601 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type])
1602 {
1603 /*i4_frame_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[e_pic_type];*/
1604 }
1605 }
1606 trace_printf("After %d", i4_frame_qp_q6);
1607 }
1608 }
1609
1610 /*swing restriciton based on previous frame qp swing*/
1611 {
1612 if(i4_call_type == 1)
1613 {
1614 trace_printf(
1615 "Before i4_frame_qp_q6 = %d min qp = %d max_qp = %d "
1616 "bufclip %d",
1617 i4_frame_qp_q6,
1618 (i4_min_frame_qp_q6),
1619 (i4_max_frame_qp_q6),
1620 i4_buffer_based_max_qp_clip_flag);
1621 }
1622 if(i4_frame_qp_q6 < i4_min_frame_qp_q6)
1623 i4_frame_qp_q6 = i4_min_frame_qp_q6;
1624
1625 /*removed low side clipping to avoid HRD compliance issue*/
1626 if(i4_steady_state_texture_case)
1627 {
1628 if(i4_frame_qp_q6 > i4_max_frame_qp_q6)
1629 {
1630 if(i4_max_frame_qp_q6 > (i4_frame_qp_q6_based_max_vbv_bits))
1631 {
1632 i4_frame_qp_q6 = i4_max_frame_qp_q6;
1633 }
1634 else
1635 {
1636 i4_frame_qp_q6 = i4_frame_qp_q6_based_max_vbv_bits;
1637 }
1638 }
1639 }
1640 }
1641 if(i4_call_type == 1)
1642 {
1643 trace_printf("After i4_frame_qp_q6 = %d", i4_frame_qp_q6);
1644 }
1645
1646 /* SS - Following done to restore this after pause to resume detect - 0.25 is for syntax bits */
1647 ps_rate_control_api->i4_orig_frm_est_bits = (i4_est_tex_bits * 5) >> 2;
1648 ps_rate_control_api->i4_prev_frm_est_bits = (i4_est_tex_bits + i4_cur_est_header_bits);
1649 pi4_cur_est_texture_bits[0] = i4_est_tex_bits;
1650
1651 /*For frames after SCD, when neither online or offline model can estimate the bits,
1652 use the remaining bits in period as max bits*/
1653 *pi4_is_model_valid = i4_is_model_valid;
1654
1655 if(0 == i4_is_model_valid)
1656 {
1657 *pi4_tot_bits_estimated =
1658 i4_bit_alloc_est_tex_bits_for_invalid_model; //(i4_buf_based_max_bits * 0.80);
1659 }
1660 else
1661 {
1662 *pi4_tot_bits_estimated = i4_est_tex_bits + i4_cur_est_header_bits;
1663 }
1664
1665 /*For B pics assigning a non-zero value to avoid asser */
1666 if(*pi4_tot_bits_estimated == 0)
1667 {
1668 *pi4_tot_bits_estimated = 1;
1669 }
1670 ASSERT(*pi4_tot_bits_estimated != 0);
1671 /*Underflow prevention*/
1672 if((ps_rate_control_api->i4_underflow_warning == 1) &&
1673 (i4_est_tex_bits < (i4_buf_based_max_bits - i4_cur_est_header_bits)) &&
1674 (i4_call_type == 1))
1675 {
1676 //printf("\nUnderflow warning\n");
1677 /*Decrement the hevc_qp by 1 for underflow prevention*/
1678 i4_frame_qp_q6 = (WORD32)((float)i4_frame_qp_q6 / (float)1.125f);
1679 ps_rate_control_api->i4_underflow_warning = 0;
1680 if(i4_call_type == 1)
1681 {
1682 trace_printf("\nUnderflow warning");
1683 }
1684 }
1685 }
1686
1687 /* Clip the frame qp within Min and Max QP */
1688 if(i4_frame_qp_q6 < ps_rate_control_api->ai4_min_qp_q6[e_pic_type])
1689 {
1690 i4_frame_qp_q6 = ps_rate_control_api->ai4_min_qp_q6[e_pic_type];
1691 }
1692 else if(i4_frame_qp_q6 > ps_rate_control_api->ai4_max_qp_q6[e_pic_type])
1693 {
1694 i4_frame_qp_q6 = ps_rate_control_api->ai4_max_qp_q6[e_pic_type];
1695 }
1696 if(i4_call_type == 1)
1697 {
1698 *pi4_vbv_buf_max_bits = i4_buf_based_max_bits;
1699 *pi4_est_tex_bits = i4_est_tex_bits;
1700 *pi4_cur_est_header_bits = i4_cur_est_header_bits;
1701 }
1702 return (i4_frame_qp_q6);
1703 }
1704
1705 /****************************************************************************
1706 Function Name : get_bits_for_final_qp
1707 Description :
1708 Inputs : ps_rate_control_api
1709 Revision History:
1710 DD MM YYYY Author(s) Changes (Describe the changes made)
1711
1712 *****************************************************************************/
1713
get_bits_for_final_qp(rate_control_api_t * ps_rate_control_api,WORD32 * pi4_modelQP,WORD32 * pi4_maxEbfQP,LWORD64 * pi8_bits_from_finalQP,WORD32 i4_clipQP,WORD32 i4_frame_qp_q6,WORD32 i4_cur_est_header_bits,WORD32 i4_est_tex_bits,WORD32 i4_buf_based_max_bits,picture_type_e e_pic_type,WORD32 i4_display_num)1714 void get_bits_for_final_qp(
1715 rate_control_api_t *ps_rate_control_api,
1716 WORD32 *pi4_modelQP,
1717 WORD32 *pi4_maxEbfQP,
1718 LWORD64 *pi8_bits_from_finalQP,
1719 WORD32 i4_clipQP,
1720 WORD32 i4_frame_qp_q6,
1721 WORD32 i4_cur_est_header_bits,
1722 WORD32 i4_est_tex_bits,
1723 WORD32 i4_buf_based_max_bits,
1724 picture_type_e e_pic_type,
1725 WORD32 i4_display_num)
1726 {
1727 UWORD32 u4_estimated_sad;
1728 u4_estimated_sad = get_est_sad(ps_rate_control_api->ps_est_sad, e_pic_type);
1729 {
1730 //printf("%d:\ti4_modelQP = %d\t i4_maxEbfQP = %d\t i4_clipQP = %d\t bits = %d\n",i4_display_num,*pi4_modelQP,*pi4_maxEbfQP,i4_clipQP,*pi8_bits_from_finalQP);
1731 if((*pi4_modelQP != INVALID_QP) && (*pi4_maxEbfQP != INVALID_QP) &&
1732 /*(*pi4_modelQP >= i4_clipQP) &&*/
1733 (i4_clipQP > *pi4_maxEbfQP))
1734 {
1735 WORD32 i4_loop = 0, i4_error, i4_prev_error = 0x7FFFFFFF;
1736 WORD32 i4_frame_qp_q6_temp;
1737 WORD32 i4_buf_max_text_bits = i4_buf_based_max_bits - i4_cur_est_header_bits;
1738 WORD32 i4_min_bits = i4_est_tex_bits, i4_max_bits = i4_buf_max_text_bits;
1739 WORD32 i4_temp_bits = (i4_min_bits + i4_max_bits) >> 1;
1740 if(*pi4_modelQP == i4_clipQP)
1741 {
1742 *pi8_bits_from_finalQP = i4_est_tex_bits + i4_cur_est_header_bits;
1743 //printf("%d:\ti4_modelQP = %d\t i4_maxEbfQP = %d\t i4_clipQP = %d\t bits = %d\n",i4_display_num,*pi4_modelQP,*pi4_maxEbfQP,i4_clipQP,*pi8_bits_from_finalQP);
1744 return;
1745 }
1746 //printf("%d:\ti4_modelQP = %d\t i4_maxEbfQP = %d\t i4_clipQP = %d\t bits = %d\n",i4_display_num,*pi4_modelQP,*pi4_maxEbfQP,i4_clipQP,*pi8_bits_from_finalQP);
1747 /*binary search to find out bits corresponds to final QP(clipped)*/
1748 while(i4_loop < 30)
1749 {
1750 i4_frame_qp_q6_temp = find_qp_for_target_bits(
1751 ps_rate_control_api->aps_rd_model[e_pic_type],
1752 i4_temp_bits,
1753 u4_estimated_sad,
1754 (ps_rate_control_api->ai4_max_qp_q6[e_pic_type]),
1755 (ps_rate_control_api->ai4_min_qp_q6[e_pic_type]));
1756 i4_error = abs(i4_frame_qp_q6_temp - i4_frame_qp_q6);
1757 if(i4_error < i4_prev_error)
1758 {
1759 *pi8_bits_from_finalQP = i4_temp_bits + i4_cur_est_header_bits;
1760 i4_prev_error = i4_error;
1761 //printf("*pi8_bits_from_finalQP = %d\n",*pi8_bits_from_finalQP);
1762 }
1763 if(i4_frame_qp_q6_temp < i4_frame_qp_q6)
1764 {
1765 i4_max_bits = i4_temp_bits;
1766 }
1767 else
1768 {
1769 i4_min_bits = i4_temp_bits;
1770 }
1771 i4_temp_bits = (i4_min_bits + i4_max_bits) >> 1;
1772 i4_loop++;
1773 }
1774 }
1775 else
1776 {
1777 /* when est bits is less than 0 , max ebfQP is not updated, hence invalid
1778 as estimated bits are less it will not cause any buffer trouble*/
1779 if(((*pi4_maxEbfQP == INVALID_QP) && (*pi4_modelQP == i4_clipQP)))
1780 {
1781 *pi8_bits_from_finalQP = i4_est_tex_bits + i4_cur_est_header_bits;
1782 }
1783 else
1784 {
1785 *pi8_bits_from_finalQP = i4_buf_based_max_bits;
1786 }
1787 }
1788 }
1789 return;
1790 }
1791 /****************************************************************************
1792 *Function Name : get_buffer_status
1793 *Description : Gets the state of VBV buffer
1794 *Inputs : Rate control API , header and texture bits
1795 *Globals :
1796 *Processing :
1797 *Outputs : 0 = normal, 1 = underflow, 2= overflow
1798 *Returns : vbv_buf_status_e
1799 *Issues :
1800 *Revision History:
1801 *DD MM YYYY Author(s) Changes (Describe the changes made)
1802 *
1803 ********************************************************************************/
get_buffer_status(rate_control_api_t * ps_rate_control_api,WORD32 i4_total_frame_bits,picture_type_e e_pic_type,WORD32 * pi4_num_bits_to_prevent_vbv_underflow)1804 vbv_buf_status_e get_buffer_status(
1805 rate_control_api_t *ps_rate_control_api,
1806 WORD32 i4_total_frame_bits, /* Total frame bits consumed */
1807 picture_type_e e_pic_type,
1808 WORD32 *pi4_num_bits_to_prevent_vbv_underflow)
1809 {
1810 vbv_buf_status_e e_buf_status = VBV_NORMAL;
1811
1812 /* Get the buffer status for the current total consumed bits and error bits*/
1813 if(ps_rate_control_api->e_rc_type == VBR_STORAGE_DVD_COMP)
1814 {
1815 e_buf_status = get_vbv_buffer_status(
1816 ps_rate_control_api->ps_vbr_storage_vbv,
1817 i4_total_frame_bits,
1818 pi4_num_bits_to_prevent_vbv_underflow);
1819 }
1820 else if(ps_rate_control_api->e_rc_type == VBR_STORAGE)
1821 {
1822 /* For VBR case since there is not underflow returning the max value */
1823 pi4_num_bits_to_prevent_vbv_underflow[0] =
1824 get_max_vbv_buf_size(ps_rate_control_api->ps_vbr_storage_vbv);
1825 e_buf_status = VBV_NORMAL;
1826 }
1827 else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
1828 {
1829 e_buf_status = get_cbr_buffer_status(
1830 ps_rate_control_api->ps_cbr_buffer,
1831 i4_total_frame_bits,
1832 pi4_num_bits_to_prevent_vbv_underflow,
1833 e_pic_type,
1834 ps_rate_control_api->e_rc_type);
1835 }
1836 else if(ps_rate_control_api->e_rc_type == VBR_STREAMING)
1837 {
1838 /* For VBR_streaming the error bits are computed according to peak bitrate*/
1839 e_buf_status = get_cbr_buffer_status(
1840 ps_rate_control_api->ps_cbr_buffer,
1841 i4_total_frame_bits,
1842 pi4_num_bits_to_prevent_vbv_underflow,
1843 e_pic_type,
1844 ps_rate_control_api->e_rc_type);
1845 }
1846 return e_buf_status;
1847 }
1848 /****************************************************************************
1849 Function Name : update_pic_handling_state
1850 Description : If the forward path and the backward path of rate control
1851 Inputs :
1852 Globals :
1853 Processing :
1854 Outputs :
1855 Returns :
1856 Issues :
1857 Revision History:
1858 DD MM YYYY Author(s) Changes (Describe the changes made)
1859 KJN Original
1860 *****************************************************************************/
update_pic_handling_state(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type)1861 void update_pic_handling_state(rate_control_api_t *ps_rate_control_api, picture_type_e e_pic_type)
1862 {
1863 WORD32 i4_is_non_ref_pic = 0;
1864 update_pic_handling(ps_rate_control_api->ps_pic_handling, e_pic_type, i4_is_non_ref_pic, 0);
1865 }
get_gop_bits(rate_control_api_t * ps_rate_control_api)1866 LWORD64 get_gop_bits(rate_control_api_t *ps_rate_control_api)
1867 {
1868 return (ba_get_gop_bits(ps_rate_control_api->ps_bit_allocation));
1869 }
get_gop_sad(rate_control_api_t * ps_rate_control_api)1870 LWORD64 get_gop_sad(rate_control_api_t *ps_rate_control_api)
1871 {
1872 return (ba_get_gop_sad(ps_rate_control_api->ps_bit_allocation));
1873 }
check_if_current_GOP_is_simple(rate_control_api_t * ps_rate_control_api)1874 WORD32 check_if_current_GOP_is_simple(rate_control_api_t *ps_rate_control_api)
1875 {
1876 LWORD64 i8_buffer_play_bits =
1877 ba_get_buffer_play_bits_for_cur_gop(ps_rate_control_api->ps_bit_allocation);
1878 if(i8_buffer_play_bits)
1879 {
1880 if((i8_buffer_play_bits + get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer)) >
1881 (0.6 * get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer)))
1882 {
1883 return 0;
1884 }
1885 else
1886 {
1887 return 1;
1888 }
1889 }
1890 else
1891 {
1892 return 1;
1893 }
1894 }
rc_get_rbip_and_num_frames(rate_control_api_t * ps_rate_control_api,WORD32 * pi4_num_frames)1895 LWORD64 rc_get_rbip_and_num_frames(rate_control_api_t *ps_rate_control_api, WORD32 *pi4_num_frames)
1896 {
1897 return (ba_get_rbip_and_num_frames(
1898 ps_rate_control_api->ps_bit_allocation,
1899 ps_rate_control_api->ps_pic_handling,
1900 pi4_num_frames));
1901 }
1902 /****************************************************************************
1903 Function Name : update_frame_level_info
1904 Description : Updates the frame level information into the rate control structure
1905 Inputs :
1906 Globals :
1907 Processing :
1908 Outputs :
1909 Returns :
1910 Issues :
1911 Revision History:
1912 DD MM YYYY Author(s) Changes (Describe the changes made)
1913 KJN Original
1914 25 04 2008 Sushmita Added support to get different bits for model
1915 updation & buffer updation.May be used,in case encoder
1916 decides to follow strict VBV compliance and hence
1917 skips a picture after encoding it.Since it has
1918 statistics of the current picture also we update
1919 the model based on the discarded picture's stats
1920 and the buffer model on the basis of actual bits
1921 consumed by skipped picture
1922 *****************************************************************************/
update_frame_level_info(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type,LWORD64 * pi8_mb_type_sad,WORD32 i4_total_frame_bits,WORD32 i4_model_updation_hdr_bits,WORD32 * pi4_mb_type_tex_bits,LWORD64 * pi8_tot_mb_type_qp_q6,WORD32 * pi4_tot_mb_in_type,WORD32 i4_avg_activity,UWORD8 u1_is_scd,WORD32 i4_is_it_a_skip,WORD32 i4_intra_frm_cost,WORD32 i4_is_pic_handling_done,WORD32 i4_suppress_bpic_update,WORD32 i4_bits_to_be_stuffed,WORD32 i4_is_pause_to_resume,WORD32 i4_lap_window_comp,WORD32 i4_is_end_of_period,WORD32 i4_lap_based_comp_reset,frame_info_t * ps_frame_info,WORD32 i4_is_rc_model_needs_to_be_updated,WORD8 i1_qp_offset,WORD32 i4_scene_num,WORD32 i4_num_frm_enc_in_scene,WORD32 i4_est_text_bits_ctr_update_qp)1923 void update_frame_level_info(
1924 rate_control_api_t *ps_rate_control_api,
1925 picture_type_e e_pic_type,
1926 LWORD64 *pi8_mb_type_sad, /* Frame level SAD for each type of MB[Intra/Inter] */
1927 WORD32 i4_total_frame_bits, /* Total frame bits actually consumed */
1928 WORD32 i4_model_updation_hdr_bits, /*header bits for model updation*/
1929 WORD32 *
1930 pi4_mb_type_tex_bits, /* Total texture bits consumed for each type of MB[Intra/Inter] used for model */
1931 LWORD64 *pi8_tot_mb_type_qp_q6, /* Total qp of all MBs based on mb type */
1932 WORD32 *pi4_tot_mb_in_type, /* total number of mbs in each mb type */
1933 WORD32 i4_avg_activity, /* Average mb activity in frame */
1934 UWORD8 u1_is_scd, /* Is a scene change detected at the current frame */
1935 WORD32 i4_is_it_a_skip,
1936 WORD32 i4_intra_frm_cost,
1937 WORD32
1938 i4_is_pic_handling_done, /* If picture handling is not done then update pic handling module. Special case for staggered endcoding */
1939 WORD32 i4_suppress_bpic_update,
1940 WORD32 i4_bits_to_be_stuffed,
1941 WORD32 i4_is_pause_to_resume,
1942 WORD32 i4_lap_window_comp,
1943 WORD32 i4_is_end_of_period,
1944 WORD32 i4_lap_based_comp_reset,
1945 frame_info_t *ps_frame_info,
1946 WORD32 i4_is_rc_model_needs_to_be_updated,
1947 WORD8 i1_qp_offset,
1948 WORD32 i4_scene_num,
1949 WORD32 i4_num_frm_enc_in_scene,
1950 WORD32 i4_est_text_bits_ctr_update_qp)
1951 {
1952 UWORD8 u1_num_skips = 0;
1953 WORD32 i;
1954 /*picture_type_e e_orig_pic_type = e_pic_type;*/
1955 LWORD64 i8_frame_sad = 0; /* Frame level SAD */
1956 WORD32 i4_tot_texture_bits = 0; /* Total texture bits consumed */
1957 WORD32 i4_tot_mbs = 0; /* Total number of mbs in frame */
1958 LWORD64 i8_avg_qp = 0, i8_avg_qp_q6 = 0;
1959 WORD32 i4_flag_rc_model_update = (i4_is_rc_model_needs_to_be_updated == 1);
1960 WORD32 i4_gop_correction = 0, i4_new_correction = 0;
1961
1962 ps_frame_info->i4_flag_rc_model_update = i4_flag_rc_model_update;
1963 ps_frame_info->i4_num_entries++;
1964 trace_printf(
1965 "update pic_type = %d tbc = %d hbc = %d\n",
1966 e_pic_type,
1967 (i4_total_frame_bits - i4_model_updation_hdr_bits),
1968 i4_model_updation_hdr_bits);
1969 /* NOTE KJN: SCD not supported in case of B Frames */
1970 if(u1_is_scd && (e_pic_type != I_PIC && e_pic_type != P_PIC))
1971 {
1972 u1_is_scd = 0;
1973 }
1974
1975 /*if both pause to resume and scene cut is signalled then ignore pause to resume flag*/
1976 if(u1_is_scd && i4_is_pause_to_resume)
1977 i4_is_pause_to_resume = 0;
1978
1979 if(!i4_is_it_a_skip && !i4_is_pic_handling_done)
1980 {
1981 /* Update the pic_handling struct */
1982 /*: do not update pic handling even in case of non-reference B-PIC*/
1983 update_pic_handling(
1984 ps_rate_control_api->ps_pic_handling, e_pic_type, i4_suppress_bpic_update, u1_is_scd);
1985 }
1986 {
1987 WORD32 *pi4_qp_array =
1988 ps_rate_control_api
1989 ->ai4_prev_frm_qp[(i4_scene_num + HALF_MAX_SCENE_NUM_RC) % MAX_SCENE_NUM_RC];
1990 WORD32 *pi4_qp_array_q6 =
1991 ps_rate_control_api
1992 ->ai4_prev_frm_qp_q6[(i4_scene_num + HALF_MAX_SCENE_NUM_RC) % MAX_SCENE_NUM_RC];
1993 WORD32 i4_i;
1994 for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
1995 {
1996 pi4_qp_array[i4_i] = 0x7FFFFFFF;
1997 pi4_qp_array_q6[i4_i] = 0x7FFFFFFF;
1998 }
1999 }
2000
2001 if(ps_rate_control_api->e_rc_type == CONST_QP)
2002 {
2003 if(!i4_is_it_a_skip)
2004 {
2005 /******************************************************************
2006 Calculate the total values from the individual values
2007 ******************************************************************/
2008 for(i = 0; i < MAX_MB_TYPE; i++)
2009 i8_frame_sad += pi8_mb_type_sad[i];
2010 for(i = 0; i < MAX_MB_TYPE; i++)
2011 i4_tot_texture_bits += pi4_mb_type_tex_bits[i];
2012 for(i = 0; i < MAX_MB_TYPE; i++)
2013 i8_avg_qp += (pi8_tot_mb_type_qp_q6[i] >> 6);
2014
2015 for(i = 0; i < MAX_MB_TYPE; i++)
2016 i8_avg_qp_q6 += pi8_tot_mb_type_qp_q6[i];
2017 for(i = 0; i < MAX_MB_TYPE; i++)
2018 i4_tot_mbs += pi4_tot_mb_in_type[i];
2019 i8_avg_qp /= i4_tot_mbs; /* Calculate the average QP */
2020 i8_avg_qp_q6 /= i4_tot_mbs;
2021
2022 if(ps_rate_control_api->u1_is_mb_level_rc_on)
2023 {
2024 /* The model needs to take into consideration the average activity of the
2025 entire frame while estimating the QP. Thus the frame sad values are scaled by
2026 the average activity before updating it into the model.*/
2027 if(!i4_avg_activity)
2028 i4_avg_activity = 1;
2029 i4_intra_frm_cost /= i4_avg_activity;
2030 i8_frame_sad /= i4_avg_activity;
2031 }
2032
2033 ps_frame_info->i8_frame_num = get_num_frms_encoded(ps_rate_control_api->ps_cbr_buffer);
2034 ps_frame_info->i4_num_entries++;
2035
2036 update_cbr_buffer(
2037 ps_rate_control_api->ps_cbr_buffer,
2038 (i4_total_frame_bits + i4_bits_to_be_stuffed),
2039 e_pic_type);
2040 }
2041 }
2042
2043 if(ps_rate_control_api->e_rc_type != CONST_QP)
2044 {
2045 /* For improving CBR streams quality */
2046 WORD32 i4_buffer_based_bit_error = 0;
2047
2048 if(!i4_is_it_a_skip)
2049 {
2050 WORD32 i4_new_period_flag;
2051 /******************************************************************
2052 Calculate the total values from the individual values
2053 ******************************************************************/
2054 for(i = 0; i < MAX_MB_TYPE; i++)
2055 i8_frame_sad += pi8_mb_type_sad[i];
2056 for(i = 0; i < MAX_MB_TYPE; i++)
2057 i4_tot_texture_bits += pi4_mb_type_tex_bits[i];
2058 for(i = 0; i < MAX_MB_TYPE; i++)
2059 i8_avg_qp += (pi8_tot_mb_type_qp_q6[i] >> 6);
2060
2061 for(i = 0; i < MAX_MB_TYPE; i++)
2062 i8_avg_qp_q6 += pi8_tot_mb_type_qp_q6[i];
2063 for(i = 0; i < MAX_MB_TYPE; i++)
2064 i4_tot_mbs += pi4_tot_mb_in_type[i];
2065 i8_avg_qp /= i4_tot_mbs; /* Calculate the average QP */
2066 i8_avg_qp_q6 /= i4_tot_mbs;
2067
2068 if(ps_rate_control_api->u1_is_mb_level_rc_on)
2069 {
2070 /* The model needs to take into consideration the average activity of the
2071 entire frame while estimating the QP. Thus the frame sad values are scaled by
2072 the average activity before updating it into the model.*/
2073 if(!i4_avg_activity)
2074 i4_avg_activity = 1;
2075 i4_intra_frm_cost /= i4_avg_activity;
2076 i8_frame_sad /= i4_avg_activity;
2077 }
2078
2079 ps_frame_info->i8_frame_num = get_num_frms_encoded(ps_rate_control_api->ps_cbr_buffer);
2080 ps_frame_info->i4_num_entries++;
2081 /******************************************************************
2082 Update the bit allocation module
2083 NOTE: For bit allocation module, the pic_type should not be modified
2084 to that of 'I', in case of a SCD.
2085 ******************************************************************/
2086 i4_new_period_flag = is_last_frame_in_gop(ps_rate_control_api->ps_pic_handling);
2087
2088 update_cur_frm_consumed_bits(
2089 ps_rate_control_api->ps_bit_allocation,
2090 ps_rate_control_api->ps_pic_handling,
2091 ps_rate_control_api->ps_cbr_buffer,
2092 i4_total_frame_bits,
2093 /*((ps_rate_control_api->e_rc_type == CBR_NLDRC)?(i4_total_frame_bits + i4_bits_to_be_stuffed):i4_total_frame_bits)*/ //account for stuffing bits even when encoder does not stuff in case of CBR
2094 i4_model_updation_hdr_bits,
2095 e_pic_type,
2096 u1_is_scd,
2097 i4_is_end_of_period,
2098 i4_lap_based_comp_reset,
2099 i4_suppress_bpic_update,
2100 i4_buffer_based_bit_error,
2101 i4_bits_to_be_stuffed,
2102 i4_lap_window_comp,
2103 ps_rate_control_api->e_rc_type,
2104 ps_rate_control_api->i4_num_gop,
2105 i4_is_pause_to_resume,
2106 i4_est_text_bits_ctr_update_qp,
2107 &i4_gop_correction,
2108 &i4_new_correction);
2109 if(1 == i4_new_period_flag &&
2110 ((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
2111 (ps_rate_control_api->e_rc_type == VBR_STORAGE_DVD_COMP)))
2112 {
2113 check_and_update_bit_allocation(
2114 ps_rate_control_api->ps_bit_allocation,
2115 ps_rate_control_api->ps_pic_handling,
2116 get_max_bits_inflow_per_frm_periode(ps_rate_control_api->ps_vbr_storage_vbv));
2117 }
2118 }
2119
2120 /******************************************************************
2121 Update the buffer status
2122 ******************************************************************/
2123 /* This updation is done after overflow and underflow handling to
2124 account for the actual bits dumped*/
2125 if((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
2126 (ps_rate_control_api->e_rc_type == VBR_STORAGE_DVD_COMP))
2127 {
2128 update_vbr_vbv(ps_rate_control_api->ps_vbr_storage_vbv, i4_total_frame_bits);
2129 }
2130 else if(
2131 ps_rate_control_api->e_rc_type == CBR_NLDRC ||
2132 ps_rate_control_api->e_rc_type == VBR_STREAMING)
2133 {
2134 update_cbr_buffer(
2135 ps_rate_control_api->ps_cbr_buffer,
2136 (i4_total_frame_bits + i4_bits_to_be_stuffed),
2137 e_pic_type);
2138 }
2139
2140 if(e_pic_type != B_PIC || e_pic_type != B1_PIC || e_pic_type != B2_PIC)
2141 {
2142 ps_rate_control_api->i4_prev_ref_is_scd = 0;
2143 }
2144
2145 if(!i4_is_it_a_skip)
2146 {
2147 /******************************************************************
2148 Handle the SCENE CHANGE DETECTED
2149 1) Make the picture type as I, so that updation happens as if it is
2150 a I frame
2151 2) Reset model, SAD and flag to restart the estimation process
2152 ******************************************************************/
2153 if(u1_is_scd || ps_rate_control_api->u1_is_first_frm)
2154 {
2155 e_pic_type = I_PIC;
2156
2157 /* Reset the SAD estimation module */
2158 reset_est_sad(ps_rate_control_api->ps_est_sad);
2159
2160 /*remember the previous reference as SCD. This is required to trigger quering model for B
2161 * frames with delay one sub-gop*/
2162 ps_rate_control_api->i4_prev_ref_is_scd = 1;
2163
2164 /* Reset the MB Rate control */
2165 init_mb_level_rc(ps_rate_control_api->ps_mb_rate_control);
2166
2167 /* Adjust the average QP for the frame based on bits consumption */
2168 /* Initialize the QP for each picture type according to the average QP of the SCD pic */
2169 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] = (WORD32)i8_avg_qp;
2170
2171 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] = (WORD32)i8_avg_qp_q6;
2172
2173 ps_rate_control_api->i4_frames_since_last_scd = 0;
2174
2175 ps_rate_control_api->f_p_to_i_comp_ratio = 1.0f;
2176 /* Reset the number of header bits in a scene change */
2177 //init_prev_header_bits(ps_rate_control_api->ps_bit_allocation, ps_rate_control_api->ps_pic_handling);
2178 }
2179 else if(i4_is_pause_to_resume)
2180 {
2181 reset_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[e_pic_type]); //ELP_RC
2182 }
2183 if(i8_frame_sad && (!i4_suppress_bpic_update))
2184 {
2185 /********************************************************************
2186 Update the model of the correponding picture type
2187 NOTE: For SCD, we force the frame type from 'P' to that of a 'I'
2188 *********************************************************************/
2189 /* For very simple sequences no bits are consumed by texture. These frames
2190 do not add any information to the model and so not added.
2191 Update the model only when there is atleast 1 texture bit for every mb in a frame */
2192 WORD32 i4_tot_texture_bits_added_to_model = i4_tot_texture_bits;
2193 /*update the model only if bits consumed are zero. If this is zero qp for next frame has to be reduced until
2194 * it provides some texture bits to update model*/
2195
2196 if(i4_tot_texture_bits_added_to_model > 0 && (i4_flag_rc_model_update == 1))
2197 {
2198 add_frame_to_rd_model(
2199 ps_rate_control_api->aps_rd_model[e_pic_type],
2200 i4_tot_texture_bits_added_to_model,
2201 (WORD32)i8_avg_qp_q6,
2202 i8_frame_sad,
2203 u1_num_skips);
2204
2205 {
2206 number_t temp =
2207 get_linear_coefficient(ps_rate_control_api->aps_rd_model[e_pic_type]);
2208 ps_frame_info->model_coeff_a_lin_wo_int.e = temp.e;
2209 ps_frame_info->model_coeff_a_lin_wo_int.sm = temp.sm;
2210 }
2211 }
2212
2213 /******************************************************************
2214 Update the sad estimation module
2215 NOTE: For SCD, we force the frame type from 'P' to that of a 'I'
2216 ******************************************************************/
2217 update_actual_sad(
2218 ps_rate_control_api->ps_est_sad, (UWORD32)i8_frame_sad, e_pic_type);
2219 /*: This will update I pic sad with current pic intra SAD. Now for non I-PIC the intra sad is coming same as
2220 *best sad. This will corrupt intra frame sad. So not updating this. I frame SAD is updated only at I pic */
2221
2222 /* Atleast one proper frame in added into the model. Until that
2223 keep using the initial QP */
2224
2225 /*B frames immediatly encoded after scene cut may still belong to previous content, When B frames encoded after one P frame after SCD are guranteed to belong
2226 * new scene, modeling these frames wrt previous B frames might give wrong results. To avoid this model for B frame is not queried unless it is guranteed that one B frame
2227 * has been modeled with new content. So setting is_first_frm_coded for B frames with delay of one frame*/
2228 /*In HEVC implementation it is guranteed to encode new scene after scene cut I pic*/
2229 ps_rate_control_api->au1_is_first_frm_coded[e_pic_type] = 1;
2230 }
2231
2232 if(i4_avg_activity)
2233 {
2234 /* Update the mb_level model */
2235 mb_update_frame_level(ps_rate_control_api->ps_mb_rate_control, i4_avg_activity);
2236 }
2237 /* Update the variable which denotes that a frame has been encountered */
2238 ps_rate_control_api->u1_is_first_frm = 0;
2239 ps_rate_control_api->i4_frames_since_last_scd++;
2240 }
2241 }
2242 return;
2243 }
2244 /* SGI & Enc Loop Parallelism related changes*/
2245 /****************************************************************************
2246 Function Name : update_frame_rc_get_frame_qp_info
2247 Description :
2248 Inputs : ps_rate_control_api
2249 Revision History:
2250 DD MM YYYY Author(s) Changes (Describe the changes made)
2251
2252 *****************************************************************************/
update_frame_rc_get_frame_qp_info(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type,WORD32 i4_is_scd,WORD32 i4_is_pause_to_resume,WORD32 i4_avg_frame_qp_q6,WORD32 i4_suppress_bpic_update,WORD32 i4_scene_num,WORD32 i4_num_frm_enc_in_scene)2253 void update_frame_rc_get_frame_qp_info(
2254 rate_control_api_t *ps_rate_control_api,
2255 picture_type_e e_pic_type,
2256 WORD32 i4_is_scd,
2257 WORD32 i4_is_pause_to_resume,
2258 WORD32 i4_avg_frame_qp_q6,
2259 WORD32 i4_suppress_bpic_update,
2260 WORD32 i4_scene_num,
2261 WORD32 i4_num_frm_enc_in_scene)
2262 {
2263 WORD32 i4_avg_qp = 0, i4_avg_qp_q6 = 0;
2264
2265 i4_avg_qp = (i4_avg_frame_qp_q6 >> 6);
2266 i4_avg_qp_q6 = i4_avg_frame_qp_q6;
2267
2268 if(i4_is_scd && (e_pic_type != I_PIC && e_pic_type != P_PIC))
2269 {
2270 i4_is_scd = 0;
2271 }
2272
2273 if(e_pic_type == I_PIC)
2274 {
2275 ps_rate_control_api->i4_I_frame_qp_model = is_first_frame_coded(ps_rate_control_api);
2276 }
2277 if((i4_is_scd && i4_is_pause_to_resume)) //KISH
2278 i4_is_pause_to_resume = 0;
2279
2280 if(i4_is_scd || ps_rate_control_api->u1_is_first_frm)
2281 {
2282 /* Save previous B-QP since some B-pics may follow detection of SCD */
2283
2284 e_pic_type = I_PIC;
2285
2286 /* Reset the SAD estimation module */
2287 reset_est_sad(ps_rate_control_api->ps_est_sad);
2288
2289 /*remember the previous reference as SCD. This is required to trigger quering model for B
2290 * frames with delay one sub-gop*/
2291 ps_rate_control_api->i4_prev_ref_is_scd = 1;
2292
2293 /* Reset the MB Rate control */
2294 init_mb_level_rc(ps_rate_control_api->ps_mb_rate_control);
2295
2296 /* Adjust the average QP for the frame based on bits consumption */
2297 /* Initialize the QP for each picture type according to the average QP of the SCD pic */
2298 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] = i4_avg_qp;
2299
2300 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] = i4_avg_qp_q6;
2301 }
2302 else if(i4_is_pause_to_resume)
2303 {
2304 /*pause to resume is guranteed to be P_PIC*/
2305 ASSERT(e_pic_type != I_PIC);
2306
2307 /* re-set all models eccept for I PIC model */
2308 /*for(i=1;i<MAX_PIC_TYPE;i++)
2309 {
2310 reset_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[i]);
2311 ps_rate_control_api->au1_is_first_frm_coded[i] = 0;
2312 }*/
2313 /*resetting only current frame model instead of resetting all models*/
2314 /*TO DO: i4_is_pause_to_resume is misnomer, as even non I scd are also handled in similar way*/
2315 //reset_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[e_pic_type]);
2316 ps_rate_control_api->au1_is_first_frm_coded[e_pic_type] = 0;
2317 ps_rate_control_api->i4_frames_since_last_scd = 0;
2318
2319 {
2320 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][e_pic_type] = i4_avg_qp;
2321 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type] = i4_avg_qp_q6;
2322 }
2323 /*also reset previous I pic Qp since it uses I frame qp for qp determination when model is reset*/
2324 if(e_pic_type == I_PIC)
2325 {
2326 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] = i4_avg_qp;
2327 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] = i4_avg_qp_q6;
2328 }
2329 else if(e_pic_type == P_PIC || e_pic_type == P1_PIC)
2330 {
2331 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] =
2332 ((LWORD64)i4_avg_qp * P_TO_I_RATIO) >> K_Q;
2333 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] =
2334 ((LWORD64)i4_avg_qp_q6 * P_TO_I_RATIO) >> K_Q;
2335 }
2336 else if(e_pic_type == B_PIC || e_pic_type == BB_PIC)
2337 {
2338 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] =
2339 ((LWORD64)i4_avg_qp * P_TO_I_RATIO * P_TO_I_RATIO) >> (K_Q + K_Q);
2340 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] =
2341 ((LWORD64)i4_avg_qp_q6 * P_TO_I_RATIO * P_TO_I_RATIO) >> (K_Q + K_Q);
2342 }
2343 else if(e_pic_type == B1_PIC || e_pic_type == B11_PIC)
2344 {
2345 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] =
2346 ((LWORD64)i4_avg_qp * P_TO_I_RATIO * P_TO_I_RATIO * P_TO_I_RATIO) >>
2347 (K_Q + K_Q + K_Q);
2348 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] =
2349 ((LWORD64)i4_avg_qp_q6 * P_TO_I_RATIO * P_TO_I_RATIO * P_TO_I_RATIO) >>
2350 (K_Q + K_Q + K_Q);
2351 }
2352 else if(e_pic_type == B2_PIC || e_pic_type == B22_PIC)
2353 {
2354 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] =
2355 ((LWORD64)i4_avg_qp * P_TO_I_RATIO * P_TO_I_RATIO * P_TO_I_RATIO * P_TO_I_RATIO) >>
2356 (K_Q + K_Q + K_Q + K_Q);
2357 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] =
2358 ((LWORD64)i4_avg_qp_q6 * P_TO_I_RATIO * P_TO_I_RATIO * P_TO_I_RATIO *
2359 P_TO_I_RATIO) >>
2360 (K_Q + K_Q + K_Q + K_Q);
2361 }
2362 }
2363 else
2364 {
2365 #if 1 /* Prev QP updation has happened at the end of the get frame qp call itself */
2366 /******************************************************************
2367 Update the Qp used by the current frame
2368 ******************************************************************/
2369 if(!i4_suppress_bpic_update)
2370 {
2371 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][e_pic_type] = i4_avg_qp;
2372 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type] = i4_avg_qp_q6;
2373 trace_printf("Prev frame qp q6 update %d pic type %d", i4_avg_qp_q6, e_pic_type);
2374 }
2375 #endif
2376 }
2377
2378 if(i4_num_frm_enc_in_scene == 1)
2379 {
2380 WORD32 i4_i = 0;
2381 for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
2382 {
2383 if(ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][i4_i] == 0x7FFFFFFF)
2384 {
2385 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][i4_i] = i4_avg_qp;
2386 ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][i4_i] = i4_avg_qp_q6;
2387 }
2388 }
2389 }
2390
2391 if((!i4_suppress_bpic_update))
2392 {
2393 /*B frames immediatly encoded after scene cut may still belong to previous content, When B frames encoded after one P frame after SCD are guranteed to belong
2394 * new scene, modeling these frames wrt previous B frames might give wrong results. To avoid this model for B frame is not queried unless it is guranteed that one B frame
2395 * has been modeled with new content. So setting is_first_frm_coded for B frames with delay of one frame*/
2396 /*In HEVC implementation it is guranteed to encode new scene after scene cut I pic*/
2397 //ps_rate_control_api->au1_is_first_frm_coded[e_pic_type] = 1; //KISH_ELP
2398 }
2399
2400 /* Update the variable which denotes that a frame has been encountered */
2401 ps_rate_control_api->u1_is_first_frm = 0;
2402
2403 /* Store the prev encoded picture type for restricting Qp swing */
2404 if((e_pic_type == I_PIC) || (e_pic_type == P_PIC))
2405 {
2406 ps_rate_control_api->prev_ref_pic_type = e_pic_type;
2407 }
2408
2409 return;
2410 }
2411
2412 /*update previous frame intra sad */
2413 /****************************************************************************
2414 Function Name : rc_update_prev_frame_intra_sad
2415 Description :
2416 Inputs : ps_rate_control_api
2417 Revision History:
2418 DD MM YYYY Author(s) Changes (Describe the changes made)
2419
2420 *****************************************************************************/
rc_update_prev_frame_intra_sad(rate_control_api_t * ps_rate_control_api,WORD32 i4_intra_frame_sad)2421 void rc_update_prev_frame_intra_sad(
2422 rate_control_api_t *ps_rate_control_api, WORD32 i4_intra_frame_sad)
2423 {
2424 update_prev_frame_intra_sad(ps_rate_control_api->ps_est_sad, i4_intra_frame_sad);
2425 }
2426 /****************************************************************************
2427 Function Name : rc_get_prev_frame_intra_sad
2428 Description :
2429 Inputs : ps_rate_control_api
2430 Revision History:
2431 DD MM YYYY Author(s) Changes (Describe the changes made)
2432
2433 *****************************************************************************/
rc_get_prev_frame_intra_sad(rate_control_api_t * ps_rate_control_api)2434 WORD32 rc_get_prev_frame_intra_sad(rate_control_api_t *ps_rate_control_api)
2435 {
2436 return get_prev_frame_intra_sad(ps_rate_control_api->ps_est_sad);
2437 }
2438 /*update previous frame sad */
2439 /****************************************************************************
2440 Function Name : rc_update_prev_frame_sad
2441 Description :
2442 Inputs : ps_rate_control_api
2443 Revision History:
2444 DD MM YYYY Author(s) Changes (Describe the changes made)
2445
2446 *****************************************************************************/
rc_update_prev_frame_sad(rate_control_api_t * ps_rate_control_api,WORD32 i4_frame_sad,picture_type_e e_pic_type)2447 void rc_update_prev_frame_sad(
2448 rate_control_api_t *ps_rate_control_api, WORD32 i4_frame_sad, picture_type_e e_pic_type)
2449 {
2450 update_prev_frame_sad(ps_rate_control_api->ps_est_sad, i4_frame_sad, e_pic_type);
2451 }
2452 /****************************************************************************
2453 Function Name : rc_get_prev_frame_sad
2454 Description :
2455 Inputs : ps_rate_control_api
2456 Revision History:
2457 DD MM YYYY Author(s) Changes (Describe the changes made)
2458
2459 *****************************************************************************/
rc_get_prev_frame_sad(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type)2460 WORD32 rc_get_prev_frame_sad(rate_control_api_t *ps_rate_control_api, picture_type_e e_pic_type)
2461 {
2462 return get_prev_frame_sad(ps_rate_control_api->ps_est_sad, e_pic_type);
2463 }
2464
2465 /****************************************************************************
2466 Function Name : reset_rc_for_pause_to_play_transition
2467 Description : In this mode it resets RC only for P and B picture, since the
2468 sequece has not changed but only the motion related changes would
2469 take impact
2470 Inputs : ps_rate_control_api
2471 Revision History:
2472 DD MM YYYY Author(s) Changes (Describe the changes made)
2473
2474 *****************************************************************************/
reset_rc_for_pause_to_play_transition(rate_control_api_t * ps_rate_control_api)2475 void reset_rc_for_pause_to_play_transition(rate_control_api_t *ps_rate_control_api)
2476 {
2477 WORD32 i;
2478 /* re-set model only for P and B frame */
2479 for(i = 1; i < MAX_PIC_TYPE; i++)
2480 {
2481 reset_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[i]);
2482 }
2483 /* Reset flag */
2484 for(i = 1; i < MAX_PIC_TYPE; i++)
2485 {
2486 ps_rate_control_api->au1_is_first_frm_coded[i] = 0;
2487 }
2488 }
2489 /****************************************************************************
2490 Function Name : get_rc_target_bits
2491 Description :
2492 Inputs : ps_rate_control_api
2493 Revision History:
2494 DD MM YYYY Author(s) Changes (Describe the changes made)
2495
2496 *****************************************************************************/
get_rc_target_bits(rate_control_api_t * ps_rate_control_api)2497 WORD32 get_rc_target_bits(rate_control_api_t *ps_rate_control_api)
2498 {
2499 return (ps_rate_control_api->i4_prev_frm_est_bits);
2500 }
2501 /****************************************************************************
2502 Function Name : get_orig_rc_target_bits
2503 Description :
2504 Inputs : ps_rate_control_api
2505 Revision History:
2506 DD MM YYYY Author(s) Changes (Describe the changes made)
2507
2508 *****************************************************************************/
get_orig_rc_target_bits(rate_control_api_t * ps_rate_control_api)2509 WORD32 get_orig_rc_target_bits(rate_control_api_t *ps_rate_control_api)
2510 {
2511 return (ps_rate_control_api->i4_orig_frm_est_bits);
2512 }
2513
2514 #if NON_STEADSTATE_CODE
2515 /******************************************************************************
2516 MB Level API functions
2517 ******************************************************************************/
2518 /****************************************************************************
2519 Function Name : init_mb_rc_frame_level
2520 Description : Initialise the frame level details required for a mb level
2521 Inputs : u1_frame_qp - Frame Qp that is to be used to the current frame
2522 Globals :
2523 Processing :
2524 Outputs :
2525 Returns :
2526 Issues :
2527 Revision History:
2528 DD MM YYYY Author(s) Changes (Describe the changes made)
2529
2530 *****************************************************************************/
2531
init_mb_rc_frame_level(rate_control_api_t * ps_rate_control_api,UWORD8 u1_frame_qp)2532 void init_mb_rc_frame_level(rate_control_api_t *ps_rate_control_api, UWORD8 u1_frame_qp)
2533 {
2534 mb_init_frame_level(ps_rate_control_api->ps_mb_rate_control, u1_frame_qp);
2535 }
2536 #endif /* #if NON_STEADSTATE_CODE */
2537
2538 /****************************************************************************
2539 Function Name : get_bits_to_stuff
2540 Description : Gets the bits to stuff to prevent Underflow of Encoder Buffer
2541 Inputs : Rate control API ctxt , total consumed bits
2542 Globals :
2543 Processing :
2544 Outputs : number of bits to stuff
2545 Returns : i4_bits_to_stuff
2546 Issues :
2547 Revision History:
2548 DD MM YYYY Author(s) Changes (Describe the changes made)
2549
2550 *****************************************************************************/
get_bits_to_stuff(rate_control_api_t * ps_rate_control_api,WORD32 i4_tot_consumed_bits,picture_type_e e_pic_type)2551 WORD32 get_bits_to_stuff(
2552 rate_control_api_t *ps_rate_control_api, WORD32 i4_tot_consumed_bits, picture_type_e e_pic_type)
2553 {
2554 WORD32 i4_bits_to_stuff;
2555 /* Get the CBR bits to stuff*/
2556 i4_bits_to_stuff =
2557 get_cbr_bits_to_stuff(ps_rate_control_api->ps_cbr_buffer, i4_tot_consumed_bits, e_pic_type);
2558 return i4_bits_to_stuff;
2559 }
2560
2561 /****************************************************************************
2562 Function Name : get_prev_frm_est_bits
2563 Description : Returns previous frame estimated bits
2564 Inputs : Rate control API ctxt
2565 Globals :
2566 Processing :
2567 Outputs : previous frame estimated bits
2568 Returns : i4_prev_frm_est_bits
2569 Issues :
2570 Revision History:
2571 DD MM YYYY Author(s) Changes (Describe the changes made)
2572
2573 *****************************************************************************/
get_prev_frm_est_bits(rate_control_api_t * ps_rate_control_api)2574 WORD32 get_prev_frm_est_bits(rate_control_api_t *ps_rate_control_api)
2575 {
2576 return (ps_rate_control_api->i4_prev_frm_est_bits);
2577 }
2578
2579 /****************************************************************************
2580 Function Name : change_frm_rate_for_bit_alloc
2581 Description : Does the necessary changes only in the bit_allocation module
2582 there is a change in frame rate
2583 Inputs : u4_frame_rate - new frame rate to be used
2584 Globals :
2585 Processing :
2586 Outputs :
2587 Returns :
2588 Issues :
2589 Revision History:
2590 DD MM YYYY Author(s) Changes (Describe the changes made)
2591
2592 *****************************************************************************/
change_frm_rate_for_bit_alloc(rate_control_api_t * ps_rate_control_api,UWORD32 u4_frame_rate)2593 void change_frm_rate_for_bit_alloc(rate_control_api_t *ps_rate_control_api, UWORD32 u4_frame_rate)
2594 {
2595 if(ps_rate_control_api->e_rc_type != CONST_QP)
2596 {
2597 /* Bit Allocation Module: distribute the excess/deficit bits between the
2598 old and the new frame rate to all the remaining frames */
2599 change_remaining_bits_in_period(
2600 ps_rate_control_api->ps_bit_allocation,
2601 ba_get_bit_rate(ps_rate_control_api->ps_bit_allocation),
2602 u4_frame_rate,
2603 (WORD32 *)(ps_rate_control_api->au4_new_peak_bit_rate));
2604 }
2605 }
2606
2607 /****************************************************************************
2608 * Function Name : rc_get_rem_bits_in_gop
2609 * Description : API call to get remaining bits in GOP
2610 * *****************************************************************************/
rc_get_rem_bits_in_period(rate_control_api_t * ps_rate_control_api)2611 WORD32 rc_get_rem_bits_in_period(rate_control_api_t *ps_rate_control_api)
2612 {
2613 return (get_rem_bits_in_period(
2614 ps_rate_control_api->ps_bit_allocation, ps_rate_control_api->ps_pic_handling));
2615 }
2616
2617 /****************************************************************************
2618 Function Name : flush_buf_frames
2619 Description : API call to flush the buffered up frames
2620 Inputs :
2621 Globals :
2622 Processing :
2623 Outputs :
2624 Returns :
2625 Issues :
2626 Revision History:
2627 DD MM YYYY Author(s) Changes (Describe the changes made)
2628 *****************************************************************************/
flush_buf_frames(rate_control_api_t * ps_rate_control_api)2629 void flush_buf_frames(rate_control_api_t *ps_rate_control_api)
2630 {
2631 flush_frame_from_pic_stack(ps_rate_control_api->ps_pic_handling);
2632 }
2633
2634 /****************************************************************************
2635 Function Name : rc_get_prev_header_bits
2636 Description :
2637 Inputs : ps_rate_control_api
2638 Revision History:
2639 DD MM YYYY Author(s) Changes (Describe the changes made)
2640
2641 *****************************************************************************/
rc_get_prev_header_bits(rate_control_api_t * ps_rate_control_api,WORD32 pic_type)2642 WORD32 rc_get_prev_header_bits(rate_control_api_t *ps_rate_control_api, WORD32 pic_type)
2643 {
2644 return (get_prev_header_bits(ps_rate_control_api->ps_bit_allocation, pic_type));
2645 }
2646 /****************************************************************************
2647 Function Name : rc_get_prev_P_QP
2648 Description :
2649 Inputs : ps_rate_control_api
2650 Revision History:
2651 DD MM YYYY Author(s) Changes (Describe the changes made)
2652
2653 *****************************************************************************/
rc_get_prev_P_QP(rate_control_api_t * ps_rate_control_api,WORD32 i4_scene_num)2654 WORD32 rc_get_prev_P_QP(rate_control_api_t *ps_rate_control_api, WORD32 i4_scene_num)
2655 {
2656 WORD32 i4_prev_qp = ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][P_PIC];
2657 i4_prev_qp =
2658 (ps_rate_control_api->i4_P_to_I_ratio * i4_prev_qp + (1 << (P_TO_I_RATIO_Q_FACTOR - 1))) >>
2659 P_TO_I_RATIO_Q_FACTOR;
2660 return (i4_prev_qp);
2661 }
2662 /****************************************************************************
2663 Function Name : rc_put_sad
2664 Description :
2665 Inputs : ps_rate_control_api
2666 Revision History:
2667 DD MM YYYY Author(s) Changes (Describe the changes made)
2668
2669 *****************************************************************************/
rc_put_sad(rate_control_api_t * ps_rate_control_api,WORD32 i4_cur_intra_sad,WORD32 i4_cur_sad,WORD32 i4_cur_pic_type)2670 void rc_put_sad(
2671 rate_control_api_t *ps_rate_control_api,
2672 WORD32 i4_cur_intra_sad,
2673 WORD32 i4_cur_sad,
2674 WORD32 i4_cur_pic_type)
2675 {
2676 sad_acc_put_sad(ps_rate_control_api->ps_sad_acc, i4_cur_intra_sad, i4_cur_sad, i4_cur_pic_type);
2677 }
2678 /****************************************************************************
2679 Function Name : rc_get_sad
2680 Description :
2681 Inputs : ps_rate_control_api
2682 Revision History:
2683 DD MM YYYY Author(s) Changes (Describe the changes made)
2684
2685 *****************************************************************************/
rc_get_sad(rate_control_api_t * ps_rate_control_api,WORD32 * pi4_sad)2686 void rc_get_sad(rate_control_api_t *ps_rate_control_api, WORD32 *pi4_sad)
2687 {
2688 sad_acc_get_sad(ps_rate_control_api->ps_sad_acc, pi4_sad);
2689 }
2690 /****************************************************************************
2691 Function Name : rc_update_ppic_sad
2692 Description :
2693 Inputs : ps_rate_control_api
2694 Revision History:
2695 DD MM YYYY Author(s) Changes (Describe the changes made)
2696
2697 *****************************************************************************/
rc_update_ppic_sad(rate_control_api_t * ps_rate_control_api,WORD32 i4_est_sad,WORD32 i4_prev_ppic_sad)2698 WORD32 rc_update_ppic_sad(
2699 rate_control_api_t *ps_rate_control_api, WORD32 i4_est_sad, WORD32 i4_prev_ppic_sad)
2700 {
2701 return (update_ppic_sad(ps_rate_control_api->ps_est_sad, i4_est_sad, i4_prev_ppic_sad));
2702 }
2703 /****************************************************************************
2704 Function Name : change_avg_bit_rate
2705 Description : Whenever the average bit rate changes, the excess bits is
2706 between the changed bit rate and the old one is re-distributed
2707 in the bit allocation module
2708 Inputs : u4_average_bit_rate - new average bit rate to be used
2709 : u4_peak_bit_rate - new peak bit rate to be used
2710 Globals :
2711 Processing :
2712 Outputs :
2713 Returns :
2714 Issues :
2715 Revision History:
2716 DD MM YYYY Author(s) Changes (Describe the changes made)
2717
2718 *****************************************************************************/
change_avg_bit_rate(rate_control_api_t * ps_rate_control_api,UWORD32 u4_average_bit_rate,UWORD32 u4_peak_bit_rate)2719 void change_avg_bit_rate(
2720 rate_control_api_t *ps_rate_control_api, UWORD32 u4_average_bit_rate, UWORD32 u4_peak_bit_rate)
2721 {
2722 int i;
2723
2724 if(ps_rate_control_api->e_rc_type != CONST_QP)
2725 {
2726 if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
2727 {
2728 ps_rate_control_api->au4_new_peak_bit_rate[0] = u4_average_bit_rate;
2729 ps_rate_control_api->au4_new_peak_bit_rate[1] = u4_average_bit_rate;
2730 }
2731 else
2732 {
2733 ps_rate_control_api->au4_new_peak_bit_rate[0] = u4_peak_bit_rate;
2734 ps_rate_control_api->au4_new_peak_bit_rate[1] = u4_peak_bit_rate;
2735 }
2736 /* Bit Allocation Module: distribute the excess/deficit bits between the
2737 old and the new frame rate to all the remaining frames */
2738 change_remaining_bits_in_period(
2739 ps_rate_control_api->ps_bit_allocation,
2740 u4_average_bit_rate,
2741 ba_get_frame_rate(ps_rate_control_api->ps_bit_allocation),
2742 (WORD32 *)(ps_rate_control_api->au4_new_peak_bit_rate));
2743 }
2744 //if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
2745 {
2746 UWORD32 u4_average_bit_rate_copy[MAX_NUM_DRAIN_RATES];
2747 /*DYNAMIC_RC*/
2748 //ps_rate_control_api->au4_new_peak_bit_rate[0]=u4_average_bit_rate;
2749 //ps_rate_control_api->au4_new_peak_bit_rate[1]=u4_average_bit_rate;
2750 for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
2751 {
2752 u4_average_bit_rate_copy[i] = u4_average_bit_rate;
2753 }
2754 change_cbr_vbv_bit_rate(
2755 ps_rate_control_api->ps_cbr_buffer,
2756 (WORD32 *)(u4_average_bit_rate_copy),
2757 (WORD32)ps_rate_control_api->au4_new_peak_bit_rate[0]);
2758 }
2759
2760 /* This is done only for average bitrate changing somewhere after the model stabilises.
2761 Here it is assumed that user will not do this call after first few frames.
2762 If we dont have this check, what would happen is since the model has not stabilised, also
2763 bitrate has changed before the first frame, we dont restrict the qp. Qp can go to
2764 very bad values after init qp since if swing is disabled
2765
2766 */
2767 if(ps_rate_control_api->u1_is_first_frm == 0)
2768 {
2769 for(i = 0; i < MAX_PIC_TYPE; i++)
2770 {
2771 /*This also makes sure the qp swing restrictions wont be applied at boundary of bitrate change*/
2772 ps_rate_control_api->au1_avg_bitrate_changed[i] = 1;
2773 }
2774 }
2775 }
2776
2777 #if NON_STEADSTATE_CODE
2778 /******************************************************************************
2779 Control Level API functions
2780 Logic: The control call sets the state structure of the rate control api
2781 accordingly such that the next process call would implement the same.
2782 ******************************************************************************/
2783
2784 /******************************************************************************
2785 Function Name : change_inter_frm_int_call
2786 Description :
2787 Arguments :
2788 Return Values : void
2789 Revision History:
2790 Creation
2791
2792 Assumptions -
2793
2794 Checks -
2795 *****************************************************************************/
change_inter_frm_int_call(rate_control_api_t * ps_rate_control_api,WORD32 i4_inter_frm_int)2796 void change_inter_frm_int_call(rate_control_api_t *ps_rate_control_api, WORD32 i4_inter_frm_int)
2797 {
2798 pic_handling_register_new_inter_frm_interval(
2799 ps_rate_control_api->ps_pic_handling, i4_inter_frm_int);
2800 }
2801 /******************************************************************************
2802 Function Name : change_intra_frm_int_call
2803 Description :
2804 Arguments :
2805 Return Values : void
2806 Revision History:
2807 Creation
2808
2809 Assumptions -
2810
2811 Checks -
2812 *****************************************************************************/
change_intra_frm_int_call(rate_control_api_t * ps_rate_control_api,WORD32 i4_intra_frm_int)2813 void change_intra_frm_int_call(rate_control_api_t *ps_rate_control_api, WORD32 i4_intra_frm_int)
2814 {
2815 pic_handling_register_new_int_frm_interval(
2816 ps_rate_control_api->ps_pic_handling, i4_intra_frm_int);
2817
2818 if(ps_rate_control_api->e_rc_type == VBR_STREAMING)
2819 {
2820 change_vsp_ifi(&ps_rate_control_api->s_vbr_str_prms, i4_intra_frm_int);
2821 }
2822 }
2823
2824 /****************************************************************************
2825 Function Name : change_frame_rate
2826 Description : Does the necessary changes whenever there is a change in
2827 frame rate
2828 Inputs : u4_frame_rate - new frame rate to be used
2829 Globals :
2830 Processing :
2831 Outputs :
2832 Returns :
2833 Issues :
2834 Revision History:
2835 DD MM YYYY Author(s) Changes (Describe the changes made)
2836
2837 *****************************************************************************/
change_frame_rate(rate_control_api_t * ps_rate_control_api,UWORD32 u4_frame_rate,UWORD32 u4_src_ticks,UWORD32 u4_tgt_ticks)2838 void change_frame_rate(
2839 rate_control_api_t *ps_rate_control_api,
2840 UWORD32 u4_frame_rate,
2841 UWORD32 u4_src_ticks,
2842 UWORD32 u4_tgt_ticks)
2843 {
2844 if(ps_rate_control_api->e_rc_type != CONST_QP)
2845 {
2846 UWORD32 u4_frms_in_delay_prd =
2847 ((u4_frame_rate * get_cbr_buffer_delay(ps_rate_control_api->ps_cbr_buffer)) / 1000000);
2848 if((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
2849 (ps_rate_control_api->e_rc_type == VBR_STORAGE_DVD_COMP))
2850 {
2851 change_vbr_vbv_frame_rate(ps_rate_control_api->ps_vbr_storage_vbv, u4_frame_rate);
2852 }
2853 else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
2854 {
2855 change_cbr_vbv_tgt_frame_rate(ps_rate_control_api->ps_cbr_buffer, u4_frame_rate);
2856 }
2857 else if(ps_rate_control_api->e_rc_type == VBR_STREAMING)
2858 {
2859 UWORD32 au4_num_pics_in_delay_prd[MAX_PIC_TYPE];
2860 change_vsp_tgt_ticks(&ps_rate_control_api->s_vbr_str_prms, u4_tgt_ticks);
2861 change_vsp_src_ticks(&ps_rate_control_api->s_vbr_str_prms, u4_src_ticks);
2862 change_vsp_fidp(&ps_rate_control_api->s_vbr_str_prms, u4_frms_in_delay_prd);
2863
2864 change_cbr_vbv_tgt_frame_rate(ps_rate_control_api->ps_cbr_buffer, u4_frame_rate);
2865 change_cbr_vbv_num_pics_in_delay_period(
2866 ps_rate_control_api->ps_cbr_buffer, au4_num_pics_in_delay_prd);
2867 }
2868
2869 /* Bit Allocation Module: distribute the excess/deficit bits between the
2870 old and the new frame rate to all the remaining frames */
2871 change_remaining_bits_in_period(
2872 ps_rate_control_api->ps_bit_allocation,
2873 ba_get_bit_rate(ps_rate_control_api->ps_bit_allocation),
2874 u4_frame_rate,
2875 (WORD32 *)(ps_rate_control_api->au4_new_peak_bit_rate));
2876 }
2877 }
2878 /****************************************************************************
2879 Function Name : change_init_qp
2880 Description :
2881 Inputs : ps_rate_control_api
2882 Revision History:
2883 DD MM YYYY Author(s) Changes (Describe the changes made)
2884
2885 *****************************************************************************/
change_init_qp(rate_control_api_t * ps_rate_control_api,WORD32 * pi4_init_qp,WORD32 i4_scene_num)2886 void change_init_qp(
2887 rate_control_api_t *ps_rate_control_api, WORD32 *pi4_init_qp, WORD32 i4_scene_num)
2888 {
2889 WORD32 i;
2890 /* Initialize the init_qp */
2891 for(i = 0; i < MAX_PIC_TYPE; i++)
2892 {
2893 ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][i] = pi4_init_qp[i];
2894 }
2895 }
2896
2897 /****************************************************************************
2898 Function Name : change_min_max_qp
2899 Description :
2900 Inputs : ps_rate_control_api
2901 Revision History:
2902 DD MM YYYY Author(s) Changes (Describe the changes made)
2903
2904 *****************************************************************************/
change_min_max_qp(rate_control_api_t * ps_rate_control_api,WORD32 * pi4_min_max_qp)2905 void change_min_max_qp(rate_control_api_t *ps_rate_control_api, WORD32 *pi4_min_max_qp)
2906 {
2907 WORD32 i;
2908 for(i = 0; i < MAX_PIC_TYPE; i++)
2909 {
2910 ps_rate_control_api->ai4_min_qp[i] = pi4_min_max_qp[(i << 1)];
2911 ps_rate_control_api->ai4_max_qp[i] = pi4_min_max_qp[(i << 1) + 1];
2912 }
2913
2914 change_init_qp_max_qp(ps_rate_control_api->ps_init_qp, pi4_min_max_qp);
2915 }
2916 /****************************************************************************
2917 Function Name : rc_get_frame_rate
2918 Description :
2919 Inputs : ps_rate_control_api
2920 Revision History:
2921 DD MM YYYY Author(s) Changes (Describe the changes made)
2922
2923 *****************************************************************************/
2924 /* Getter functions to get the current rate control parameters */
rc_get_frame_rate(rate_control_api_t * ps_rate_control_api)2925 UWORD32 rc_get_frame_rate(rate_control_api_t *ps_rate_control_api)
2926 {
2927 return (ba_get_frame_rate(ps_rate_control_api->ps_bit_allocation));
2928 }
2929 /****************************************************************************
2930 Function Name : rc_get_bit_rate
2931 Description :
2932 Inputs : ps_rate_control_api
2933 Revision History:
2934 DD MM YYYY Author(s) Changes (Describe the changes made)
2935
2936 *****************************************************************************/
rc_get_bit_rate(rate_control_api_t * ps_rate_control_api)2937 UWORD32 rc_get_bit_rate(rate_control_api_t *ps_rate_control_api)
2938 {
2939 return (ba_get_bit_rate(ps_rate_control_api->ps_bit_allocation));
2940 }
2941 /****************************************************************************
2942 Function Name : rc_get_peak_bit_rate
2943 Description :
2944 Inputs : ps_rate_control_api
2945 Revision History:
2946 DD MM YYYY Author(s) Changes (Describe the changes made)
2947
2948 *****************************************************************************/
rc_get_peak_bit_rate(rate_control_api_t * ps_rate_control_api,WORD32 i4_index)2949 UWORD32 rc_get_peak_bit_rate(rate_control_api_t *ps_rate_control_api, WORD32 i4_index)
2950 {
2951 return (ps_rate_control_api->au4_new_peak_bit_rate[i4_index]);
2952 }
2953 /****************************************************************************
2954 Function Name : rc_get_intra_frame_interval
2955 Description :
2956 Inputs : ps_rate_control_api
2957 Revision History:
2958 DD MM YYYY Author(s) Changes (Describe the changes made)
2959
2960 *****************************************************************************/
rc_get_intra_frame_interval(rate_control_api_t * ps_rate_control_api)2961 UWORD32 rc_get_intra_frame_interval(rate_control_api_t *ps_rate_control_api)
2962 {
2963 return (pic_type_get_intra_frame_interval(ps_rate_control_api->ps_pic_handling));
2964 }
2965 /****************************************************************************
2966 Function Name : rc_get_inter_frame_interval
2967 Description :
2968 Inputs : ps_rate_control_api
2969 Revision History:
2970 DD MM YYYY Author(s) Changes (Describe the changes made)
2971
2972 *****************************************************************************/
rc_get_inter_frame_interval(rate_control_api_t * ps_rate_control_api)2973 UWORD32 rc_get_inter_frame_interval(rate_control_api_t *ps_rate_control_api)
2974 {
2975 return (pic_type_get_inter_frame_interval(ps_rate_control_api->ps_pic_handling));
2976 }
2977 /****************************************************************************
2978 Function Name : rc_get_rc_type
2979 Description :
2980 Inputs : ps_rate_control_api
2981 Revision History:
2982 DD MM YYYY Author(s) Changes (Describe the changes made)
2983
2984 *****************************************************************************/
rc_get_rc_type(rate_control_api_t * ps_rate_control_api)2985 rc_type_e rc_get_rc_type(rate_control_api_t *ps_rate_control_api)
2986 {
2987 return (ps_rate_control_api->e_rc_type);
2988 }
2989 /****************************************************************************
2990 Function Name : rc_get_bits_per_frame
2991 Description :
2992 Inputs : ps_rate_control_api
2993 Revision History:
2994 DD MM YYYY Author(s) Changes (Describe the changes made)
2995
2996 *****************************************************************************/
rc_get_bits_per_frame(rate_control_api_t * ps_rate_control_api)2997 WORD32 rc_get_bits_per_frame(rate_control_api_t *ps_rate_control_api)
2998 {
2999 WORD32 i4_bits_per_frm;
3000
3001 X_PROD_Y_DIV_Z(
3002 ba_get_bit_rate(ps_rate_control_api->ps_bit_allocation),
3003 (UWORD32)1000,
3004 ba_get_frame_rate(ps_rate_control_api->ps_bit_allocation),
3005 i4_bits_per_frm);
3006
3007 return (i4_bits_per_frm);
3008 }
3009 /****************************************************************************
3010 Function Name : rc_get_max_delay
3011 Description :
3012 Inputs : ps_rate_control_api
3013 Revision History:
3014 DD MM YYYY Author(s) Changes (Describe the changes made)
3015
3016 *****************************************************************************/
rc_get_max_delay(rate_control_api_t * ps_rate_control_api)3017 UWORD32 rc_get_max_delay(rate_control_api_t *ps_rate_control_api)
3018 {
3019 return (get_cbr_buffer_delay(ps_rate_control_api->ps_cbr_buffer));
3020 }
3021 /****************************************************************************
3022 Function Name : rc_get_seq_no
3023 Description :
3024 Inputs : ps_rate_control_api
3025 Revision History:
3026 DD MM YYYY Author(s) Changes (Describe the changes made)
3027
3028 *****************************************************************************/
rc_get_seq_no(rate_control_api_t * ps_rate_control_api)3029 UWORD32 rc_get_seq_no(rate_control_api_t *ps_rate_control_api)
3030 {
3031 return (pic_type_get_disp_order_no(ps_rate_control_api->ps_pic_handling));
3032 }
3033 /****************************************************************************
3034 Function Name : rc_get_rem_frames_in_gop
3035 Description :
3036 Inputs : ps_rate_control_api
3037 Revision History:
3038 DD MM YYYY Author(s) Changes (Describe the changes made)
3039
3040 *****************************************************************************/
rc_get_rem_frames_in_gop(rate_control_api_t * ps_rate_control_api)3041 UWORD32 rc_get_rem_frames_in_gop(rate_control_api_t *ps_rate_control_api)
3042 {
3043 /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
3044 return (pic_type_get_rem_frms_in_gop(ps_rate_control_api->ps_pic_handling));
3045 }
3046
3047 /****************************************************************************
3048 Function Name : flush_buf_frames
3049 Description : API call to flush the buffered up frames
3050 Inputs :
3051 Globals :
3052 Processing :
3053 Outputs :
3054 Returns :
3055 Issues :
3056 Revision History:
3057 DD MM YYYY Author(s) Changes (Describe the changes made)
3058 *****************************************************************************/
post_encode_frame_skip(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type)3059 void post_encode_frame_skip(rate_control_api_t *ps_rate_control_api, picture_type_e e_pic_type)
3060 {
3061 skip_encoded_frame(ps_rate_control_api->ps_pic_handling, e_pic_type);
3062 }
3063
3064 /****************************************************************************
3065 Function Name : force_I_frame
3066 Description : API call to force an I frame
3067 *****************************************************************************/
force_I_frame(rate_control_api_t * ps_rate_control_api)3068 void force_I_frame(rate_control_api_t *ps_rate_control_api)
3069 {
3070 set_force_I_frame_flag(ps_rate_control_api->ps_pic_handling);
3071 }
3072
3073 /****************************************************************************
3074 * Function Name : rc_get_vbv_buf_fullness
3075 * Description : API call to get VBV buffer fullness
3076 ******************************************************************************/
rc_get_vbv_buf_fullness(rate_control_api_t * ps_rate_control_api)3077 WORD32 rc_get_vbv_buf_fullness(rate_control_api_t *ps_rate_control_api)
3078 {
3079 return (get_cur_vbv_buf_size(ps_rate_control_api->ps_vbr_storage_vbv));
3080 }
3081 /****************************************************************************
3082 * Function Name : rc_get_cur_peak_factor_2pass
3083 * Description : API call to get current peak factor
3084 ******************************************************************************/
rc_get_cur_peak_factor_2pass(rate_control_api_t * ps_rate_control_api)3085 float rc_get_cur_peak_factor_2pass(rate_control_api_t *ps_rate_control_api)
3086 {
3087 return (get_cur_peak_factor_2pass(ps_rate_control_api->ps_bit_allocation));
3088 }
3089 /****************************************************************************
3090 * Function Name : rc_get_min_complexity_factor_2pass
3091 * Description : API call to get minimm complexity factor
3092 ******************************************************************************/
rc_get_min_complexity_factor_2pass(rate_control_api_t * ps_rate_control_api)3093 float rc_get_min_complexity_factor_2pass(rate_control_api_t *ps_rate_control_api)
3094 {
3095 return (get_cur_min_complexity_factor_2pass(ps_rate_control_api->ps_bit_allocation));
3096 }
3097 /****************************************************************************
3098 * Function Name : rc_get_vbv_buf_size
3099 * Description : API call to get VBV buffer size
3100 ******************************************************************************/
rc_get_vbv_buf_size(rate_control_api_t * ps_rate_control_api)3101 WORD32 rc_get_vbv_buf_size(rate_control_api_t *ps_rate_control_api)
3102 {
3103 return (get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer));
3104 }
3105 /****************************************************************************
3106 * Function Name : rc_get_vbv_fulness_with_cur_bits
3107 * Description : API call to get VBV buffer fullness with current bits
3108 ******************************************************************************/
rc_get_vbv_fulness_with_cur_bits(rate_control_api_t * ps_rate_control_api,UWORD32 u4_bits)3109 WORD32 rc_get_vbv_fulness_with_cur_bits(rate_control_api_t *ps_rate_control_api, UWORD32 u4_bits)
3110 {
3111 return (get_vbv_buf_fullness(ps_rate_control_api->ps_vbr_storage_vbv, u4_bits));
3112 }
3113 /****************************************************************************
3114 * Function Name : rc_set_avg_mb_act
3115 * Description :
3116 ******************************************************************************/
rc_set_avg_mb_act(rate_control_api_t * ps_rate_control_api,WORD32 i4_avg_activity)3117 void rc_set_avg_mb_act(rate_control_api_t *ps_rate_control_api, WORD32 i4_avg_activity)
3118 {
3119 mb_update_frame_level(ps_rate_control_api->ps_mb_rate_control, i4_avg_activity);
3120 return;
3121 }
3122 /****************************************************************************
3123 * Function Name : rc_init_set_ebf
3124 * Description : API call to set EBF
3125 ******************************************************************************/
rc_init_set_ebf(rate_control_api_t * ps_rate_control_api,WORD32 i32_init_ebf)3126 void rc_init_set_ebf(rate_control_api_t *ps_rate_control_api, WORD32 i32_init_ebf)
3127 {
3128 set_cbr_ebf(ps_rate_control_api->ps_cbr_buffer, i32_init_ebf);
3129 }
3130 #endif /* #if NON_STEADSTATE_CODE */
3131
3132 /****************************************************************************
3133 Function Name : rc_get_qp_scene_change_bits
3134 Description : HEVC specific function to get scene change qp at scene cut location
3135 Inputs : ps_rate_control_api
3136
3137 Revision History:
3138 DD MM YYYY Author(s) Changes (Describe the changes made)
3139 *****************************************************************************/
3140
rc_get_qp_scene_change_bits(rate_control_handle ps_rate_control_api,WORD32 i4_total_bits,LWORD64 i8_satd_by_act_accum,WORD32 i4_num_pixel,void * offline_model_coeff,float f_i_to_average_rest,WORD32 i4_call_type)3141 WORD32 rc_get_qp_scene_change_bits(
3142 rate_control_handle ps_rate_control_api,
3143 WORD32 i4_total_bits,
3144 LWORD64 i8_satd_by_act_accum,
3145 WORD32 i4_num_pixel,
3146 void *offline_model_coeff,
3147 float f_i_to_average_rest,
3148 WORD32 i4_call_type)
3149 {
3150 float f_trial_q_scale;
3151 WORD32 i4_tex_bits = 0, i4_header_bits = 0;
3152 WORD32 error = 0, min_error = 0x7FFFFFFF, i4_is_high_bitrate = 0;
3153 double *model_coeff, min_error_q_scale = (double)127;
3154 double min_scd_qscale, max_scd_q_scale;
3155 WORD32 i4_QP, i4_max_Qp, i4_min_Qp, i4_qp_selection_flag = 0;
3156 WORD32 i4_prev_best = -1;
3157
3158 /*The qp calculation here is based on offline generated stat for around 30 frames belonging to different scene
3159 The I only mode of encode was done for the above sequence for qp range {8,51}. A quadratic and cubic curve was obtained
3160 based on the stat geneated.
3161 eq coeff*/
3162 float coeff_a, coeff_b, coeff_c, coeff_d, X, tex_bpp;
3163 float min_qp_qscale_multiplier =
3164 1; /*For fade-in fade-out case where scene starts with blank frame have higher min frame qp*/
3165 //float head_per;
3166 float normal_satd_act;
3167 float bpp = (float)get_bits_per_frame(ps_rate_control_api->ps_bit_allocation) / i4_num_pixel;
3168
3169 if(i4_num_pixel > 5000000) /*UHD*/
3170 {
3171 if(bpp > 0.12) /*30mbp 2160 30p*/
3172 i4_is_high_bitrate = 1;
3173 else if(bpp > 0.06)
3174 i4_is_high_bitrate = 2;
3175 else if(bpp > 0.03)
3176 i4_is_high_bitrate = 3;
3177 }
3178 else
3179 {
3180 if(bpp > 0.16) /*10mbps 1080 30p*/
3181 i4_is_high_bitrate = 1;
3182 else if(bpp > 0.08)
3183 i4_is_high_bitrate = 2;
3184 else if(bpp > 0.04)
3185 i4_is_high_bitrate = 3;
3186 }
3187 /*Min qp and Max qp at scene cut is critical since offline models are not reliable always*/
3188 /*During fade-in fade-out when LAP places I frame on blank pictures but the content slowly changes to complicated content, Due to low
3189 spatial complxity of I pic a very low SCD qp will be allocated, qp swing restriction will not give enough frames to increase qp to high value
3190 to encode such fast motion inter pictiures .Hence whenever temporal complexity is very high assume some least spatial complexity so that very low qp
3191 is not chosen*/
3192 if(f_i_to_average_rest < I_TO_REST_VVFAST &&
3193 (i4_is_high_bitrate !=
3194 1)) /*The I_TO_AVERAGE RATIO generally comes very low, hence this wont be measure of extent on motion in inter pictures*/
3195 {
3196 WORD32 i4_min_num_pixel = i4_num_pixel;
3197
3198 if(i4_num_pixel > 5000000)
3199 {
3200 i4_min_num_pixel = i4_min_num_pixel / 2;
3201 }
3202
3203 if(i8_satd_by_act_accum <
3204 i4_num_pixel) /*In very fast motion case have min threshold for I frame, Assume atleast one unit per pixel sad*/
3205 {
3206 if(i4_is_high_bitrate == 2)
3207 {
3208 i8_satd_by_act_accum = (LWORD64)(i4_min_num_pixel / 2);
3209 }
3210 else if(i4_is_high_bitrate == 3)
3211 {
3212 i8_satd_by_act_accum = (LWORD64)(i4_min_num_pixel * 3.0f / 4.0f);
3213 }
3214 else
3215 i8_satd_by_act_accum = (LWORD64)(i4_min_num_pixel);
3216
3217 min_qp_qscale_multiplier = (float)pow(
3218 (float)1.125f,
3219 (WORD32)6); //this will make min qp for simple frame with high moiton 24 instead of 18
3220 }
3221 }
3222 min_scd_qscale = pow(2, (double)(ps_rate_control_api->u4_min_scd_hevc_qp - 4) / 6) *
3223 min_qp_qscale_multiplier;
3224 max_scd_q_scale = pow(2, (double)(SCD_MAX_HEVC_QP - 4) / 6);
3225 i4_max_Qp = MAX_HEVC_QP;
3226 i4_min_Qp = ps_rate_control_api->u4_min_scd_hevc_qp;
3227 if((ps_rate_control_api->u1_bit_depth > 8) && (i4_call_type == 1))
3228 {
3229 i8_satd_by_act_accum = i8_satd_by_act_accum << (ps_rate_control_api->u1_bit_depth - 8);
3230 i4_max_Qp = i4_max_Qp + (6 * (ps_rate_control_api->u1_bit_depth - 8));
3231 i4_min_Qp = i4_min_Qp + (6 * (ps_rate_control_api->u1_bit_depth - 8));
3232 max_scd_q_scale = max_scd_q_scale * (1 << (ps_rate_control_api->u1_bit_depth - 8));
3233 }
3234
3235 normal_satd_act = (float)i8_satd_by_act_accum / i4_num_pixel;
3236
3237 {
3238 /* Max satd/act at L0 was taken at qp 18 for
3239 480p - 4410520
3240 720p - 9664235
3241 1080p - 15735650
3242 4k - 50316472
3243 A curve was generated using these points
3244 */
3245
3246 float f_satd_by_Act_norm = GET_L0_SATD_BY_ACT_MAX_PER_PIXEL(i4_num_pixel);
3247 float f_weigh_factor = 0.0f;
3248 f_satd_by_Act_norm = f_satd_by_Act_norm * 0.75f;
3249 f_weigh_factor = GET_WEIGH_FACTOR_FOR_MIN_SCD_Q_SCALE(normal_satd_act, f_satd_by_Act_norm);
3250 CLIP(f_weigh_factor, 1.0f, 1.0f / MULT_FACTOR_SATD);
3251 min_scd_qscale = min_scd_qscale * f_weigh_factor;
3252 CLIP(min_scd_qscale, max_scd_q_scale, 1);
3253 }
3254
3255 /*coeff value based on input resolution
3256 1920x1090 -> 207360,1280x720->921600,720x480->345600(unlike for I_REST_AVG_BIT_RATIO here 720x480 was considered as low resolution)
3257 ultra high res = num_pixek > 5000000
3258 high_res = num_pxel > 1500000
3259 mid res = num_pixel > 600000
3260 low_res = num_pixel < 600000
3261 The fit is based on HEVC qp value between 18 and 48 inclusive
3262 */
3263 /*adding coeff for ultra HD resolution*/
3264 /*
3265 High quality bpp vs nor satd/act/qp
3266 --------------------------------------
3267 480p y = -0.1823x3 + 0.5258x2 + 1.7707x - 0.0394
3268 720p y = -0.1458x3 + 0.4039x2 + 1.8817x - 0.0648
3269 1080p y = -0.4712x3 + 1.3818x2 + 1.2797x - 0.0262
3270 2160p y = -1.1234x3 + 2.6328x2 + 0.8817x - 0.0047
3271
3272
3273 Medium speed
3274 ------------
3275 480p y = -0.1567x3 + 0.4222x2 + 1.8899x - 0.0537
3276 720p y = -0.1417x3 + 0.3699x2 + 1.9611x - 0.0766
3277 1080p y = -0.4841x3 + 1.4123x2 + 1.2981x - 0.0321
3278 2160p y = -1.1989x3 + 2.7935x2 + 0.8648x - 0.0074
3279
3280 High speed
3281 -------------
3282 480p y = -0.1611x3 + 0.4418x2 + 1.8754x - 0.0524
3283 720p y = -0.1455x3 + 0.3854x2 + 1.951x - 0.0753
3284 1080p y = -0.4908x3 + 1.4344x2 + 1.2848x - 0.031
3285 2160p y = -1.2037x3 + 2.8062x2 + 0.8551x - 0.0067
3286 */
3287 model_coeff = (double *)offline_model_coeff;
3288 coeff_a = (float)model_coeff[0];
3289 coeff_b = (float)model_coeff[1];
3290 coeff_c = (float)model_coeff[2];
3291 coeff_d = (float)model_coeff[3];
3292 for(i4_QP = i4_min_Qp; i4_QP < i4_max_Qp; i4_QP++)
3293 {
3294 /*needs to use the array for qp to qscale */
3295
3296 f_trial_q_scale = (float)(pow(2.0, (i4_QP - 4.0) / 6.0));
3297 /* curve fit for texture bits*/
3298 X = (float)normal_satd_act / f_trial_q_scale;
3299 tex_bpp = ((coeff_a * X * X * X) + (coeff_b * X * X) + (coeff_c * X) + coeff_d);
3300 if(tex_bpp < (float)((1 << 30)) / i4_num_pixel)
3301 i4_tex_bits = (tex_bpp * i4_num_pixel);
3302 else
3303 i4_tex_bits = (1 << 30);
3304 i4_header_bits = 0;
3305 if(i4_tex_bits > 0)
3306 {
3307 /*QP increase can't cause increase in bits*/
3308 if(i4_prev_best != -1 && (i4_tex_bits > i4_prev_best))
3309 {
3310 min_error = 0x7FFFFFFF;
3311 i4_qp_selection_flag = 0;
3312 }
3313 /*consider texture bits to get header bits using obtained header percentage. Using header bits on overall bits targetted might not be correct*/
3314 error = i4_total_bits - (i4_tex_bits + i4_header_bits);
3315 if(abs(error) < abs(min_error))
3316 {
3317 min_error = error;
3318 min_error_q_scale = f_trial_q_scale;
3319 i4_qp_selection_flag = 1;
3320 i4_prev_best = i4_tex_bits;
3321 }
3322 }
3323 }
3324 if(!i4_qp_selection_flag)
3325 {
3326 min_error_q_scale = (WORD32)(min_scd_qscale + 0.5);
3327 }
3328 //if((ps_rate_control_api->u1_bit_depth > 8)&& (i4_call_type == 1))
3329 // min_error_q_scale = min_error_q_scale / (1 << (ps_rate_control_api->u1_bit_depth - 8));
3330
3331 /*offline stat generation range considered is mpeg2qp 5 to 161 or hevc qp 18 to 48*/
3332 CLIP(min_error_q_scale, (WORD32)(max_scd_q_scale + 0.5), (WORD32)(min_scd_qscale + .5));
3333 return ((WORD32)(min_error_q_scale * (1 << QSCALE_Q_FAC_3)));
3334 }
3335
3336 /****************************************************************************
3337 Function Name : rc_get_qp_for_scd_frame
3338 Description : Get qp for a scene cut frame
3339 Inputs : ps_rate_control_api
3340
3341 Revision History:
3342 DD MM YYYY Author(s) Changes (Describe the changes made)
3343 *****************************************************************************/
rc_get_qp_for_scd_frame(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type,LWORD64 i8_satd_act_accum,WORD32 i4_num_pels_in_frame,WORD32 i4_est_I_pic_head_bits,WORD32 i4_f_sim_lap_avg,void * offline_model_coeff,float i_to_avg_ratio,WORD32 i4_true_scd,float af_sum_weigh[MAX_PIC_TYPE][3],frame_info_t * ps_frame_stat,WORD32 i4_rc_2_pass,WORD32 i4_is_not_an_I_pic,WORD32 i4_ref_first_pass,WORD32 i4_call_type,WORD32 * pi4_cur_est_tot_bits,WORD32 * pi4_tot_bits_estimated,WORD32 i4_use_offline_model_2pass,LWORD64 * pi8_i_tex_bits,float * pf_i_qs,WORD32 i4_best_br_id,WORD32 * pi4_estimate_to_calc_frm_error)3344 WORD32 rc_get_qp_for_scd_frame(
3345 rate_control_api_t *ps_rate_control_api,
3346 picture_type_e e_pic_type,
3347 LWORD64 i8_satd_act_accum,
3348 WORD32 i4_num_pels_in_frame,
3349 WORD32 i4_est_I_pic_head_bits,
3350 WORD32 i4_f_sim_lap_avg,
3351 void *offline_model_coeff,
3352 float i_to_avg_ratio,
3353 WORD32 i4_true_scd,
3354 float af_sum_weigh[MAX_PIC_TYPE][3],
3355 frame_info_t *ps_frame_stat,
3356 WORD32 i4_rc_2_pass,
3357 WORD32 i4_is_not_an_I_pic,
3358 WORD32 i4_ref_first_pass,
3359 WORD32 i4_call_type,
3360 WORD32 *pi4_cur_est_tot_bits,
3361 WORD32 *pi4_tot_bits_estimated,
3362 WORD32 i4_use_offline_model_2pass,
3363 LWORD64 *pi8_i_tex_bits,
3364 float *pf_i_qs,
3365 WORD32 i4_best_br_id,
3366 WORD32 *pi4_estimate_to_calc_frm_error)
3367 {
3368 WORD32 i4_qs_q3, i4_buf_based_min_bits, i4_buf_based_max_bits, i4_cur_est_tot_bits,
3369 i4_est_texture_bits, i4_get_error = 0;
3370 float f_bits_ratio;
3371
3372 assign_complexity_coeffs(ps_rate_control_api->ps_bit_allocation, af_sum_weigh);
3373
3374 i4_cur_est_tot_bits = get_scene_change_tot_frm_bits(
3375 ps_rate_control_api->ps_bit_allocation,
3376 ps_rate_control_api->ps_pic_handling,
3377 ps_rate_control_api->ps_cbr_buffer,
3378 i4_num_pels_in_frame,
3379 i4_f_sim_lap_avg,
3380 i_to_avg_ratio,
3381 i4_call_type,
3382 i4_is_not_an_I_pic,
3383 ps_rate_control_api->i4_is_infinite_gop);
3384 if(i4_call_type == 1)
3385 {
3386 *pi4_estimate_to_calc_frm_error = i4_cur_est_tot_bits;
3387 }
3388
3389 /* vbv buffer position based error correction to keep away encoder buffer overflow at layer 0 pictures*/
3390 if(e_pic_type == I_PIC || e_pic_type == P_PIC || e_pic_type == P1_PIC)
3391 {
3392 WORD32 i4_cur_ebf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer);
3393 WORD32 i4_vbv_size = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
3394 WORD32 i4_max_ebf = (WORD32)(i4_vbv_size * MAX_THRESHOLD_VBV_FRM_ERROR);
3395 WORD32 i4_drain_rate = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
3396 WORD32 i4_total_bits_allocted = i4_cur_est_tot_bits;
3397 WORD32 i4_total_bits_to_be_alloc;
3398 WORD32 i4_expected_ebf = (i4_cur_ebf + i4_total_bits_allocted - i4_drain_rate);
3399 /*if expected ebf is greater than max threashold, correct the allocation such that it never cross max
3400 but if it less than drain rate, atleast give drainrate bits*/
3401 if(i4_expected_ebf > i4_max_ebf)
3402 {
3403 i4_total_bits_to_be_alloc =
3404 MAX(i4_drain_rate, (i4_total_bits_allocted - (i4_expected_ebf - i4_max_ebf)));
3405 i4_cur_est_tot_bits = i4_total_bits_to_be_alloc;
3406 }
3407 }
3408 if(i4_call_type == 1)
3409 {
3410 i4_get_error = rc_get_estimate_bit_error(ps_rate_control_api);
3411 }
3412 if(i4_est_I_pic_head_bits != -1)
3413 /*get constraints from buffer*/
3414 {
3415 get_min_max_bits_based_on_buffer(
3416 ps_rate_control_api,
3417 e_pic_type,
3418 &i4_buf_based_min_bits,
3419 &i4_buf_based_max_bits,
3420 i4_get_error);
3421 if(i4_cur_est_tot_bits > i4_buf_based_max_bits)
3422 i4_cur_est_tot_bits = i4_buf_based_max_bits;
3423 if((i4_cur_est_tot_bits < i4_buf_based_min_bits) && (i_to_avg_ratio > 8.0))
3424 i4_cur_est_tot_bits = i4_buf_based_min_bits;
3425 }
3426 if(i4_est_I_pic_head_bits <
3427 0) //indicates header bits data is not available. Assume default ratio
3428 {
3429 i4_est_texture_bits = (i4_cur_est_tot_bits * DEFAULT_TEX_PERCENTAGE_Q5) >> 5;
3430 i4_est_I_pic_head_bits = i4_cur_est_tot_bits - i4_est_texture_bits;
3431 }
3432 if((i4_cur_est_tot_bits - i4_est_I_pic_head_bits) < 0)
3433 i4_cur_est_tot_bits = i4_est_I_pic_head_bits;
3434
3435 *pi4_tot_bits_estimated = i4_cur_est_tot_bits;
3436
3437 if(i4_true_scd)
3438 {
3439 /*texture bits should be atleast 25% of header bits*/
3440 if(i4_cur_est_tot_bits < (1.25 * i4_est_I_pic_head_bits))
3441 i4_cur_est_tot_bits = (WORD32)(1.25 * i4_est_I_pic_head_bits);
3442
3443 ps_rate_control_api->i4_scd_I_frame_estimated_tot_bits = i4_cur_est_tot_bits;
3444 }
3445
3446 /* Get qp for scene cut frame based on offline generated data*/
3447
3448 i4_qs_q3 = rc_get_qp_scene_change_bits(
3449 ps_rate_control_api,
3450 (i4_cur_est_tot_bits - i4_est_I_pic_head_bits),
3451 i8_satd_act_accum,
3452 i4_num_pels_in_frame,
3453 offline_model_coeff,
3454 i_to_avg_ratio,
3455 i4_call_type);
3456
3457 if(i4_call_type)
3458 trace_printf(
3459 "i4_qp %d, i8_satd_act_accum %I64d,i_to_avg_ratio %f, "
3460 "i4_est_I_pic_head_bits %d i4_cur_est_tot_bits %d\n",
3461 i4_qp,
3462 i8_satd_act_accum,
3463 i_to_avg_ratio,
3464 i4_est_I_pic_head_bits,
3465 i4_cur_est_tot_bits);
3466
3467 *pi4_cur_est_tot_bits = i4_cur_est_tot_bits;
3468
3469 return (i4_qs_q3);
3470 }
3471
3472 /****************************************************************************
3473 Function Name : rc_set_num_scd_in_lap_window
3474 Description :
3475 Inputs : ps_rate_control_api
3476
3477 Revision History:
3478 DD MM YYYY Author(s) Changes (Describe the changes made)
3479 *****************************************************************************/
rc_set_num_scd_in_lap_window(rate_control_api_t * ps_rate_control_api,WORD32 i4_num_scd_in_lap_window,WORD32 i4_num_frames_b4_scd)3480 void rc_set_num_scd_in_lap_window(
3481 rate_control_api_t *ps_rate_control_api,
3482 WORD32 i4_num_scd_in_lap_window,
3483 WORD32 i4_num_frames_b4_scd)
3484 {
3485 bit_allocation_set_num_scd_lap_window(
3486 ps_rate_control_api->ps_bit_allocation, i4_num_scd_in_lap_window, i4_num_frames_b4_scd);
3487 }
3488 /****************************************************************************
3489 Function Name : rc_set_next_sc_i_in_rc_look_ahead
3490 Description :
3491 Inputs : ps_rate_control_api
3492
3493 Revision History:
3494 DD MM YYYY Author(s) Changes (Describe the changes made)
3495 *****************************************************************************/
rc_set_next_sc_i_in_rc_look_ahead(rate_control_api_t * ps_rate_control_api,WORD32 i4_next_sc_i_in_rc_look_ahead)3496 void rc_set_next_sc_i_in_rc_look_ahead(
3497 rate_control_api_t *ps_rate_control_api, WORD32 i4_next_sc_i_in_rc_look_ahead)
3498 {
3499 bit_allocation_set_sc_i_in_rc_look_ahead(
3500 ps_rate_control_api->ps_bit_allocation, i4_next_sc_i_in_rc_look_ahead);
3501 }
3502
3503 /****************************************************************************
3504 * Function Name : rc_update_mismatch_error
3505 * Description : API call to update remaining bits in period based on error
3506 * between rdopt bits estimate and actual bits produced in entorpy
3507 * *****************************************************************************/
rc_update_mismatch_error(rate_control_api_t * ps_rate_control_api,WORD32 i4_error_bits)3508 void rc_update_mismatch_error(rate_control_api_t *ps_rate_control_api, WORD32 i4_error_bits)
3509 {
3510 bit_allocation_update_gop_level_bit_error(
3511 ps_rate_control_api->ps_bit_allocation, i4_error_bits);
3512 /*Also alter the encoder buffer fullness based on the error*/
3513 /*error = rdopt - entropy hence subtract form current buffer fullness*/
3514 update_cbr_buf_mismatch_bit(ps_rate_control_api->ps_cbr_buffer, i4_error_bits);
3515 }
3516 /****************************************************************************
3517 Function Name : rc_set_estimate_status
3518 Description :
3519 Inputs : ps_rate_control_api
3520
3521 Revision History:
3522 DD MM YYYY Author(s) Changes (Describe the changes made)
3523 *****************************************************************************/
rc_set_estimate_status(rate_control_api_t * ps_rate_control_api,WORD32 i4_tex_bits,WORD32 i4_hdr_bits,WORD32 i4_est_text_bits_ctr_get_qp)3524 WORD32 rc_set_estimate_status(
3525 rate_control_api_t *ps_rate_control_api,
3526 WORD32 i4_tex_bits,
3527 WORD32 i4_hdr_bits,
3528 WORD32 i4_est_text_bits_ctr_get_qp)
3529 {
3530 update_estimate_status(
3531 ps_rate_control_api->ps_bit_allocation,
3532 i4_tex_bits,
3533 i4_hdr_bits,
3534 i4_est_text_bits_ctr_get_qp);
3535
3536 return i4_tex_bits;
3537 }
3538 /****************************************************************************
3539 Function Name : rc_get_bpp_based_scene_cut_qp
3540 Description : bpp based qp for a scene cut frame
3541 Inputs : ps_rate_control_api
3542
3543 Revision History:
3544 DD MM YYYY Author(s) Changes (Describe the changes made)
3545 *****************************************************************************/
rc_get_bpp_based_scene_cut_qp(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type,WORD32 i4_num_pels_in_frame,WORD32 i4_f_sim_lap,float af_sum_weigh[MAX_PIC_TYPE][3],WORD32 i4_call_type)3546 WORD32 rc_get_bpp_based_scene_cut_qp(
3547 rate_control_api_t *ps_rate_control_api,
3548 picture_type_e e_pic_type,
3549 WORD32 i4_num_pels_in_frame,
3550 WORD32 i4_f_sim_lap,
3551 float af_sum_weigh[MAX_PIC_TYPE][3],
3552 WORD32 i4_call_type)
3553 {
3554 WORD32 i4_cur_est_texture_bits, i4_cur_est_header_bits, i4_qp, i4_tot_bits,
3555 i4_buf_based_min_bits, i4_buf_based_max_bits;
3556
3557 /* Reset the number of header bits in a scene change */
3558 //init_prev_header_bits(ps_rate_control_api->ps_bit_allocation, ps_rate_control_api->ps_pic_handling);
3559
3560 /* Get the estimated header bits for the current encoded frame */
3561
3562 assign_complexity_coeffs(ps_rate_control_api->ps_bit_allocation, af_sum_weigh);
3563 i4_cur_est_header_bits =
3564 get_cur_frm_est_header_bits(ps_rate_control_api->ps_bit_allocation, e_pic_type);
3565
3566 /*get estimate of total bits that can be allocated to I frame based on offline generated data*/
3567 i4_tot_bits = get_scene_change_tot_frm_bits(
3568 ps_rate_control_api->ps_bit_allocation,
3569 ps_rate_control_api->ps_pic_handling,
3570 ps_rate_control_api->ps_cbr_buffer,
3571 i4_num_pels_in_frame,
3572 i4_f_sim_lap,
3573 (float)8.00,
3574 0,
3575 0,
3576 ps_rate_control_api->i4_is_infinite_gop);
3577
3578 /* Getting the min and max texture bits based on buffer fullness and constraining the
3579 bit allocation based on this */
3580 if(i4_call_type == 1)
3581 {
3582 get_min_max_bits_based_on_buffer(
3583 ps_rate_control_api, e_pic_type, &i4_buf_based_min_bits, &i4_buf_based_max_bits, 0);
3584 if(i4_tot_bits > i4_buf_based_max_bits)
3585 i4_tot_bits = i4_buf_based_max_bits;
3586 if(i4_tot_bits < i4_buf_based_min_bits)
3587 i4_tot_bits = i4_buf_based_min_bits;
3588 }
3589 /*Assume 30 percent header bits*/
3590 i4_cur_est_texture_bits = (i4_tot_bits * DEFAULT_TEX_PERCENTAGE_Q5) >> 5;
3591
3592 /* Get the texture bits assigned to the current frame */
3593 i4_cur_est_header_bits = i4_tot_bits - i4_cur_est_texture_bits;
3594
3595 if(i4_cur_est_texture_bits < 0)
3596 i4_cur_est_texture_bits = 0;
3597
3598 /* Get the qp for the remaining bits allocated for that frame based on buffer status */
3599 i4_qp = get_init_qp_using_pels_bits_per_frame(
3600 ps_rate_control_api->ps_init_qp, I_PIC, i4_cur_est_texture_bits, i4_num_pels_in_frame);
3601 /* Make sure the qp is with in range */
3602 if(i4_qp < ps_rate_control_api->ai4_min_qp[e_pic_type])
3603 {
3604 i4_qp = ps_rate_control_api->ai4_min_qp[e_pic_type];
3605 }
3606 else if(i4_qp > ps_rate_control_api->ai4_max_qp[e_pic_type])
3607 {
3608 i4_qp = ps_rate_control_api->ai4_max_qp[e_pic_type];
3609 }
3610
3611 return (i4_qp);
3612 }
3613 /****************************************************************************
3614 Function Name : rc_reset_pic_model
3615 Description :
3616 Inputs : ps_rate_control_api
3617
3618 Revision History:
3619 DD MM YYYY Author(s) Changes (Describe the changes made)
3620 *****************************************************************************/
rc_reset_pic_model(rate_control_api_t * ps_rate_control_api,picture_type_e pic_type)3621 void rc_reset_pic_model(rate_control_api_t *ps_rate_control_api, picture_type_e pic_type)
3622 {
3623 reset_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[pic_type]);
3624 }
3625 /****************************************************************************
3626 Function Name : rc_reset_first_frame_coded_flag
3627 Description :
3628 Inputs : ps_rate_control_api
3629
3630 Revision History:
3631 DD MM YYYY Author(s) Changes (Describe the changes made)
3632 *****************************************************************************/
rc_reset_first_frame_coded_flag(rate_control_api_t * ps_rate_control_api,picture_type_e pic_type)3633 void rc_reset_first_frame_coded_flag(
3634 rate_control_api_t *ps_rate_control_api, picture_type_e pic_type)
3635 {
3636 ps_rate_control_api->au1_is_first_frm_coded[pic_type] = 0;
3637 }
3638 /****************************************************************************
3639 Function Name : rc_get_scene_change_est_header_bits
3640 Description :
3641 Inputs : ps_rate_control_api
3642
3643 Revision History:
3644 DD MM YYYY Author(s) Changes (Describe the changes made)
3645 *****************************************************************************/
rc_get_scene_change_est_header_bits(rate_control_api_t * ps_rate_control_api,WORD32 i4_num_pixels,WORD32 i4_fsim_lap,float af_sum_weigh[MAX_PIC_TYPE][3],float i_to_avg_ratio)3646 WORD32 rc_get_scene_change_est_header_bits(
3647 rate_control_api_t *ps_rate_control_api,
3648 WORD32 i4_num_pixels,
3649 WORD32 i4_fsim_lap,
3650 float af_sum_weigh[MAX_PIC_TYPE][3],
3651 float i_to_avg_ratio)
3652 {
3653 WORD32 i4_est_tot_bits;
3654
3655 assign_complexity_coeffs(ps_rate_control_api->ps_bit_allocation, af_sum_weigh);
3656
3657 i4_est_tot_bits = get_scene_change_tot_frm_bits(
3658 ps_rate_control_api->ps_bit_allocation,
3659 ps_rate_control_api->ps_pic_handling,
3660 ps_rate_control_api->ps_cbr_buffer,
3661 i4_num_pixels,
3662 i4_fsim_lap,
3663 i_to_avg_ratio,
3664 0,
3665 0,
3666 ps_rate_control_api->i4_is_infinite_gop);
3667 /*return header bits based on default percentage*/
3668 return (i4_est_tot_bits - ((i4_est_tot_bits * DEFAULT_TEX_PERCENTAGE_Q5) >> 5));
3669 }
3670 /****************************************************************************
3671 Function Name : rc_put_temp_comp_lap
3672 Description :
3673 Inputs : ps_rate_control_api
3674
3675 Revision History:
3676 DD MM YYYY Author(s) Changes (Describe the changes made)
3677 *****************************************************************************/
rc_put_temp_comp_lap(rate_control_api_t * ps_rate_control_api,WORD32 i4_lap_fsim,LWORD64 i8_per_pixel_frm_hme_sad_q10,picture_type_e e_pic_type)3678 void rc_put_temp_comp_lap(
3679 rate_control_api_t *ps_rate_control_api,
3680 WORD32 i4_lap_fsim,
3681 LWORD64 i8_per_pixel_frm_hme_sad_q10,
3682 picture_type_e e_pic_type)
3683 {
3684 ps_rate_control_api->i4_lap_f_sim = i4_lap_fsim;
3685 if(e_pic_type == P_PIC)
3686 {
3687 ps_rate_control_api->i8_per_pixel_p_frm_hme_sad_q10 = i8_per_pixel_frm_hme_sad_q10;
3688 }
3689 }
3690 /****************************************************************************
3691 Function Name : rc_get_pic_distribution
3692 Description :
3693 Inputs : ps_rate_control_api
3694
3695 Revision History:
3696 DD MM YYYY Author(s) Changes (Describe the changes made)
3697 *****************************************************************************/
rc_get_pic_distribution(rate_control_api_t * ps_rate_control_api,WORD32 * ai4_pic_type)3698 void rc_get_pic_distribution(rate_control_api_t *ps_rate_control_api, WORD32 *ai4_pic_type)
3699 {
3700 pic_type_get_frms_in_gop(ps_rate_control_api->ps_pic_handling, ai4_pic_type);
3701 }
3702 /****************************************************************************
3703 Function Name : rc_get_actual_pic_distribution
3704 Description :
3705 Inputs : ps_rate_control_api
3706
3707 Revision History:
3708 DD MM YYYY Author(s) Changes (Describe the changes made)
3709 *****************************************************************************/
rc_get_actual_pic_distribution(rate_control_api_t * ps_rate_control_api,WORD32 * ai4_pic_type)3710 void rc_get_actual_pic_distribution(rate_control_api_t *ps_rate_control_api, WORD32 *ai4_pic_type)
3711 {
3712 pic_type_get_actual_frms_in_gop(ps_rate_control_api->ps_pic_handling, ai4_pic_type);
3713 }
3714 /****************************************************************************
3715 Function Name : rc_reset_Kp_Kb
3716 Description :
3717 Inputs : ps_rate_control_api
3718
3719 Revision History:
3720 DD MM YYYY Author(s) Changes (Describe the changes made)
3721 *****************************************************************************/
rc_reset_Kp_Kb(rate_control_api_t * ps_rate_control_api,float f_i_to_avg_rest,WORD32 i4_num_active_pic_type,float f_curr_hme_sad_per_pixel,WORD32 * pi4_complexity_bin,WORD32 i4_rc_pass)3722 void rc_reset_Kp_Kb(
3723 rate_control_api_t *ps_rate_control_api,
3724 float f_i_to_avg_rest,
3725 WORD32 i4_num_active_pic_type,
3726 float f_curr_hme_sad_per_pixel,
3727 WORD32 *pi4_complexity_bin,
3728 WORD32 i4_rc_pass)
3729 {
3730 reset_Kp_Kb(
3731 ps_rate_control_api->ps_bit_allocation,
3732 f_i_to_avg_rest,
3733 i4_num_active_pic_type,
3734 f_curr_hme_sad_per_pixel,
3735 ps_rate_control_api->f_max_hme_sad_per_pixel,
3736 pi4_complexity_bin,
3737 i4_rc_pass);
3738 }
3739
3740 /****************************************************************************
3741 Function Name : rc_reset_Kp_Kb
3742 Description : Get Kp and Kb values for offset at scene cut
3743 Inputs : ps_rate_control_api
3744
3745 Revision History:
3746 DD MM YYYY Author(s) Changes (Describe the changes made)
3747 *****************************************************************************/
3748
rc_get_kp_kb(rate_control_api_t * ps_rate_control_api,picture_type_e e_pic_type)3749 WORD32 rc_get_kp_kb(rate_control_api_t *ps_rate_control_api, picture_type_e e_pic_type)
3750 {
3751 return get_Kp_Kb(ps_rate_control_api->ps_bit_allocation, e_pic_type);
3752 }
3753 /****************************************************************************
3754 Function Name : rc_get_ebf
3755 Description :
3756 Inputs : ps_rate_control_api
3757
3758 Revision History:
3759 DD MM YYYY Author(s) Changes (Describe the changes made)
3760 *****************************************************************************/
rc_get_ebf(rate_control_api_t * ps_rate_control_api)3761 WORD32 rc_get_ebf(rate_control_api_t *ps_rate_control_api)
3762 {
3763 return (get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer));
3764 }
3765
3766 /****************************************************************************
3767 Function Name : rc_get_offline_normalized_complexity
3768 Description : The complexities of L1 are normalized with the highest offline
3769 global complexity
3770 Inputs :
3771 Globals :
3772 Processing :
3773 Outputs :
3774 Returns :
3775 Issues :
3776 Revision History:
3777 DD MM YYYY Author(s) Changes (Describe the changes made)
3778
3779 *****************************************************************************/
rc_get_offline_normalized_complexity(WORD32 i4_intra_period,WORD32 i4_luma_pels,float f_per_pixel_complexity,WORD32 i4_pass_number)3780 float rc_get_offline_normalized_complexity(
3781 WORD32 i4_intra_period, WORD32 i4_luma_pels, float f_per_pixel_complexity, WORD32 i4_pass_number)
3782 {
3783 {
3784 if((i4_luma_pels) > 1500000)
3785 {
3786 if(i4_intra_period == 1)
3787 {
3788 f_per_pixel_complexity /= (float)3.69;
3789 }
3790 else
3791 {
3792 /*Full HD and above: Based on running few content, exact data needs to be plugged in*/
3793 f_per_pixel_complexity /= (float)2.25;
3794 }
3795 }
3796 else if((i4_luma_pels) > 700000)
3797 {
3798 if(i4_intra_period == 1)
3799 {
3800 f_per_pixel_complexity /= (float)4.28;
3801 }
3802 else
3803 {
3804 f_per_pixel_complexity /=
3805 (float)2.6109; //the max complexity observed for 720p content of netflix_fountain
3806 }
3807 }
3808 else
3809 {
3810 if(i4_intra_period == 1)
3811 f_per_pixel_complexity /= (float)4.91;
3812 else
3813 f_per_pixel_complexity /=
3814 (float)3; //the max complexity observed for 720p content of netflix_fountain
3815 }
3816 }
3817 if(f_per_pixel_complexity > 1.0)
3818 f_per_pixel_complexity = 1;
3819 return f_per_pixel_complexity;
3820 }
3821
3822 /****************************************************************************
3823 Function Name : rc_bit_alloc_detect_ebf_stuff_scenario
3824 Description : To estimate whether there will be a case of underflow based on
3825 estimated bit consumption and drain rate if there is probability
3826 of underflow then we will lower the HEVC qp's by 1 based
3827 on the warning flag.
3828 Inputs :
3829 Globals :
3830 Processing :
3831 Outputs :
3832 Returns :
3833 Issues :
3834 Revision History:
3835 DD MM YYYY Author(s) Changes (Describe the changes made)
3836
3837 *****************************************************************************/
3838
rc_bit_alloc_detect_ebf_stuff_scenario(rate_control_api_t * ps_rate_control_api,WORD32 i4_num_frm_bef_scd_lap2,LWORD64 i8_total_bits_est_consu_lap2,WORD32 i4_max_inter_frm_int)3839 void rc_bit_alloc_detect_ebf_stuff_scenario(
3840 rate_control_api_t *ps_rate_control_api,
3841 WORD32 i4_num_frm_bef_scd_lap2,
3842 LWORD64 i8_total_bits_est_consu_lap2,
3843 WORD32 i4_max_inter_frm_int)
3844 {
3845 WORD32 i4_peak_drain_rate;
3846 LWORD64 i8_ebf, i8_estimate_ebf_at_end;
3847 i4_peak_drain_rate = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
3848 i8_ebf = rc_get_ebf(ps_rate_control_api);
3849 i8_estimate_ebf_at_end =
3850 i8_ebf - (i4_num_frm_bef_scd_lap2 * i4_peak_drain_rate) + i8_total_bits_est_consu_lap2;
3851
3852 ps_rate_control_api->i4_underflow_warning = 0;
3853
3854 if(i8_estimate_ebf_at_end < (i4_max_inter_frm_int * i4_peak_drain_rate))
3855 {
3856 /*If underflow is imminent give a flag*/
3857 ps_rate_control_api->i4_underflow_warning = 1;
3858 }
3859 }
3860
3861 /****************************************************************************
3862 Function Name : bit_alloc_get_estimated_bits_for_pic
3863 Description :
3864 Inputs : ps_rate_control_api
3865
3866 Revision History:
3867 DD MM YYYY Author(s) Changes (Describe the changes made)
3868 *****************************************************************************/
3869
bit_alloc_get_estimated_bits_for_pic(rate_control_api_t * ps_rate_contro_api,WORD32 i4_cur_frm_est_cl_sad,WORD32 i4_prev_frm_cl_sad,picture_type_e e_pic_type)3870 WORD32 bit_alloc_get_estimated_bits_for_pic(
3871 rate_control_api_t *ps_rate_contro_api,
3872 WORD32 i4_cur_frm_est_cl_sad,
3873 WORD32 i4_prev_frm_cl_sad,
3874 picture_type_e e_pic_type)
3875 {
3876 WORD32 i4_prev_frame_bits, i4_curnt_frame_est_bits, i4_prev_frame_header_bits;
3877 get_prev_frame_total_header_bits(
3878 ps_rate_contro_api->ps_bit_allocation,
3879 &i4_prev_frame_bits,
3880 &i4_prev_frame_header_bits,
3881 e_pic_type);
3882
3883 i4_curnt_frame_est_bits = (WORD32)(
3884 ((float)(i4_prev_frame_bits - i4_prev_frame_header_bits) * (float)i4_cur_frm_est_cl_sad /
3885 (float)i4_prev_frm_cl_sad) +
3886 i4_prev_frame_header_bits);
3887 return (i4_curnt_frame_est_bits);
3888 }
3889
3890 /****************************************************************************
3891 Function Name : rc_get_max_hme_sad_per_pixel
3892 Description : At init time based on parameters we pick the max hme sad per pixel.
3893 Inputs :
3894 Globals :
3895 Processing :
3896 Outputs :
3897 Returns :
3898 Issues :
3899 Revision History:
3900 DD MM YYYY Author(s) Changes (Describe the changes made)
3901
3902 *****************************************************************************/
rc_get_max_hme_sad_per_pixel(rate_control_api_t * ps_rate_control_api,WORD32 i4_total_pixels)3903 void rc_get_max_hme_sad_per_pixel(rate_control_api_t *ps_rate_control_api, WORD32 i4_total_pixels)
3904 {
3905 WORD32 i, i4_error = 0x7FFFFFFF, i4_temp_error, i4_res_index = 0, i4_br_index = 0;
3906 WORD32 i4_num_temporal_layers;
3907 /*Max hme sad per pixel based on resolutions, num. of temporal layers (0-3) and also bpp-> whether low bitrate or high bitrate*/
3908 float af_offline_hme_sad_per_pixel_480p[4][2] = {
3909 { 2.94f, 2.63f }, { 2.96f, 2.44f }, { 2.72f, 1.94f }, { 2.70f, 2.04f }
3910 };
3911 float af_offline_hme_sad_per_pixel_720p[4][2] = {
3912 { 3.37f, 2.97f }, { 3.35f, 2.77f }, { 3.18f, 2.40f }, { 2.94f, 1.83f }
3913 };
3914 float af_offline_hme_sad_per_pixel_1080p[4][2] = {
3915 { 3.24f, 2.78f }, { 3.17f, 2.46f }, { 2.91f, 1.98f }, { 2.75f, 1.65f }
3916 };
3917 float af_offline_hme_sad_per_pixel_2160p[4][2] = {
3918 { 2.56f, 2.11f }, { 2.47f, 1.92f }, { 2.19f, 1.46f }, { 2.00f, 1.21f }
3919 };
3920
3921 /*Low BR or HBR is decided by comparing the bpp values as below*/
3922 float af_offline_bpp[4][2] = {
3923 { 0.30f, 0.09f }, { 0.25f, 0.06f }, { 0.16f, 0.04f }, { 0.12f, 0.02f }
3924 };
3925
3926 /*Number of pixels in the picture for picking the closest resolution*/
3927 WORD32 ai4_pixels_res[4] = { 307200, 921600, 2073600, 8294400 };
3928
3929 float f_bpp =
3930 (float)get_bits_per_frame(ps_rate_control_api->ps_bit_allocation) / i4_total_pixels;
3931 float f_max_hme_sad_per_pixel;
3932
3933 i4_num_temporal_layers = ps_rate_control_api->i4_num_active_pic_type - 2;
3934
3935 CLIP(i4_num_temporal_layers, 3, 0);
3936
3937 /*Pick the closest resolution based on error*/
3938 for(i = 0; i < 4; i++)
3939 {
3940 i4_temp_error = abs(i4_total_pixels - ai4_pixels_res[i]);
3941
3942 if(i4_temp_error < i4_error)
3943 {
3944 i4_error = i4_temp_error;
3945 i4_res_index = i;
3946 }
3947 }
3948
3949 /*Decide whether LBR or HBR*/
3950 if((fabs(af_offline_bpp[i4_res_index][0] - f_bpp)) >
3951 (fabs(af_offline_bpp[i4_res_index][1] - f_bpp)))
3952 {
3953 i4_br_index = 1;
3954 }
3955 else
3956 {
3957 i4_br_index = 0;
3958 }
3959
3960 /*After that pick the max hme sad*/
3961 switch(i4_res_index)
3962 {
3963 case 0:
3964 f_max_hme_sad_per_pixel =
3965 af_offline_hme_sad_per_pixel_480p[i4_num_temporal_layers][i4_br_index];
3966 break;
3967 case 1:
3968 f_max_hme_sad_per_pixel =
3969 af_offline_hme_sad_per_pixel_720p[i4_num_temporal_layers][i4_br_index];
3970 break;
3971 case 2:
3972 f_max_hme_sad_per_pixel =
3973 af_offline_hme_sad_per_pixel_1080p[i4_num_temporal_layers][i4_br_index];
3974 break;
3975 case 3:
3976 f_max_hme_sad_per_pixel =
3977 af_offline_hme_sad_per_pixel_2160p[i4_num_temporal_layers][i4_br_index];
3978 break;
3979 default:
3980 f_max_hme_sad_per_pixel =
3981 af_offline_hme_sad_per_pixel_1080p[i4_num_temporal_layers][i4_br_index];
3982 break;
3983 }
3984
3985 ps_rate_control_api->f_max_hme_sad_per_pixel = f_max_hme_sad_per_pixel;
3986 }
3987
3988 /****************************************************************************
3989 Function Name : rc_update_pic_distn_lap_to_rc
3990 Description :
3991 Inputs : ps_rate_control_api
3992
3993 Revision History:
3994 DD MM YYYY Author(s) Changes (Describe the changes made)
3995 *****************************************************************************/
rc_update_pic_distn_lap_to_rc(rate_control_api_t * ps_rate_contro_api,WORD32 ai4_num_pic_type[MAX_PIC_TYPE])3996 void rc_update_pic_distn_lap_to_rc(
3997 rate_control_api_t *ps_rate_contro_api, WORD32 ai4_num_pic_type[MAX_PIC_TYPE])
3998 {
3999 pic_type_update_frms_in_gop(ps_rate_contro_api->ps_pic_handling, ai4_num_pic_type);
4000 }
4001
4002 /****************************************************************************
4003 Function Name : rc_set_bits_based_on_complexity
4004 Description :
4005 Inputs : ps_rate_control_api
4006
4007 Revision History:
4008 DD MM YYYY Author(s) Changes (Describe the changes made)
4009 *****************************************************************************/
rc_set_bits_based_on_complexity(rate_control_api_t * ps_rate_contro_api,WORD32 i4_lap_window_comp,WORD32 i4_num_frames)4010 void rc_set_bits_based_on_complexity(
4011 rate_control_api_t *ps_rate_contro_api, WORD32 i4_lap_window_comp, WORD32 i4_num_frames)
4012 {
4013 set_bit_allocation_i_frames(
4014 ps_rate_contro_api->ps_bit_allocation,
4015 ps_rate_contro_api->ps_cbr_buffer,
4016 ps_rate_contro_api->ps_pic_handling,
4017 i4_lap_window_comp,
4018 i4_num_frames);
4019 }
4020 /****************************************************************************
4021 Function Name : rc_set_avg_qscale_first_pass
4022 Description : Set the average qscale from first pass
4023 Inputs : ps_rate_control_api
4024
4025 Revision History:
4026 DD MM YYYY Author(s) Changes (Describe the changes made)
4027 *****************************************************************************/
4028
rc_set_avg_qscale_first_pass(rate_control_api_t * ps_rate_control_api,float f_average_qscale_1st_pass)4029 void rc_set_avg_qscale_first_pass(
4030 rate_control_api_t *ps_rate_control_api, float f_average_qscale_1st_pass)
4031 {
4032 ba_set_avg_qscale_first_pass(ps_rate_control_api->ps_bit_allocation, f_average_qscale_1st_pass);
4033 }
4034 /****************************************************************************
4035 Function Name : rc_set_max_avg_qscale_first_pass
4036 Description : Set the maximum avergae Qscale in second pass as average Qscale
4037 of first pass + 6 This is for simple contents
4038 Inputs : ps_rate_control_api
4039
4040 Revision History:
4041 DD MM YYYY Author(s) Changes (Describe the changes made)
4042 *****************************************************************************/
4043
rc_set_max_avg_qscale_first_pass(rate_control_api_t * ps_rate_control_api,float f_max_average_qscale_1st_pass)4044 void rc_set_max_avg_qscale_first_pass(
4045 rate_control_api_t *ps_rate_control_api, float f_max_average_qscale_1st_pass)
4046 {
4047 ba_set_max_avg_qscale_first_pass(
4048 ps_rate_control_api->ps_bit_allocation, f_max_average_qscale_1st_pass);
4049 }
4050 /****************************************************************************
4051 Function Name : rc_set_i_to_sum_api_ba
4052 Description :
4053 Inputs : ps_rate_control_api
4054
4055 Revision History:
4056 DD MM YYYY Author(s) Changes (Describe the changes made)
4057 *****************************************************************************/
rc_set_i_to_sum_api_ba(rate_control_api_t * ps_rate_control_api,float f_curr_i_to_sum)4058 void rc_set_i_to_sum_api_ba(rate_control_api_t *ps_rate_control_api, float f_curr_i_to_sum)
4059 {
4060 bit_alloc_set_curr_i_to_sum_i(ps_rate_control_api->ps_bit_allocation, f_curr_i_to_sum);
4061 }
4062 /****************************************************************************
4063 Function Name : rc_set_p_to_i_complexity_ratio
4064 Description :
4065 Inputs : ps_rate_control_api
4066
4067 Revision History:
4068 DD MM YYYY Author(s) Changes (Describe the changes made)
4069 *****************************************************************************/
rc_set_p_to_i_complexity_ratio(rate_control_api_t * ps_rate_control_api,float f_p_to_i_ratio)4070 void rc_set_p_to_i_complexity_ratio(rate_control_api_t *ps_rate_control_api, float f_p_to_i_ratio)
4071 {
4072 ps_rate_control_api->f_p_to_i_comp_ratio = f_p_to_i_ratio;
4073 }
4074 /****************************************************************************
4075 Function Name : rc_set_scd_in_period
4076 Description :
4077 Inputs : ps_rate_control_api
4078
4079 Revision History:
4080 DD MM YYYY Author(s) Changes (Describe the changes made)
4081 *****************************************************************************/
rc_set_scd_in_period(rate_control_api_t * ps_rate_control_api,WORD32 i4_scd_in_period)4082 void rc_set_scd_in_period(rate_control_api_t *ps_rate_control_api, WORD32 i4_scd_in_period)
4083 {
4084 ps_rate_control_api->i4_scd_in_period_2_pass = i4_scd_in_period;
4085 }
4086 /****************************************************************************
4087 Function Name : rc_ba_get_qp_offset_offline_data
4088 Description :
4089 Inputs : ps_rate_control_api
4090
4091 Revision History:
4092 DD MM YYYY Author(s) Changes (Describe the changes made)
4093 *****************************************************************************/
rc_ba_get_qp_offset_offline_data(rate_control_api_t * ps_rate_control_api,WORD32 ai4_offsets[5],float f_hme_sad_per_pixel,WORD32 i4_num_active_pic_type,WORD32 * pi4_complexity_bin)4094 void rc_ba_get_qp_offset_offline_data(
4095 rate_control_api_t *ps_rate_control_api,
4096 WORD32 ai4_offsets[5],
4097 float f_hme_sad_per_pixel,
4098 WORD32 i4_num_active_pic_type,
4099 WORD32 *pi4_complexity_bin)
4100 {
4101 WORD32 i4_ratio;
4102 float f_ratio;
4103
4104 CLIP(f_hme_sad_per_pixel, ps_rate_control_api->f_max_hme_sad_per_pixel, 0.01f);
4105
4106 i4_ratio = (WORD32)(ps_rate_control_api->f_max_hme_sad_per_pixel / f_hme_sad_per_pixel);
4107 f_ratio = ps_rate_control_api->f_max_hme_sad_per_pixel / f_hme_sad_per_pixel;
4108
4109 ba_get_qp_offset_offline_data(
4110 ai4_offsets, i4_ratio, f_ratio, i4_num_active_pic_type, pi4_complexity_bin);
4111 }
4112 /****************************************************************************
4113 Function Name : rc_api_gop_level_averagae_q_scale_without_offset
4114 Description : Find the GOP level average of the Qscale
4115 Inputs : ps_rate_control_api
4116
4117 Revision History:
4118 DD MM YYYY Author(s) Changes (Describe the changes made)
4119 *****************************************************************************/
4120
rc_api_gop_level_averagae_q_scale_without_offset(rate_control_api_t * ps_rate_control_api)4121 float rc_api_gop_level_averagae_q_scale_without_offset(rate_control_api_t *ps_rate_control_api)
4122 {
4123 float f_hbd_qscale =
4124 ba_gop_info_average_qscale_gop_without_offset(ps_rate_control_api->ps_bit_allocation);
4125
4126 return (f_hbd_qscale);
4127 }
4128
4129 /****************************************************************************
4130 Function Name : rc_getprev_ref_pic_type
4131 Description :
4132 Inputs : ps_rate_control_api
4133
4134 Revision History:
4135 DD MM YYYY Author(s) Changes (Describe the changes made)
4136 *****************************************************************************/
rc_getprev_ref_pic_type(rate_control_api_t * ps_rate_control_api)4137 picture_type_e rc_getprev_ref_pic_type(rate_control_api_t *ps_rate_control_api)
4138 {
4139 return (ps_rate_control_api->prev_ref_pic_type);
4140 }
4141 /****************************************************************************
4142 Function Name : rc_get_actual_intra_frame_int
4143 Description :
4144 Inputs : ps_rate_control_api
4145
4146 Revision History:
4147 DD MM YYYY Author(s) Changes (Describe the changes made)
4148 *****************************************************************************/
rc_get_actual_intra_frame_int(rate_control_api_t * ps_rate_control_api)4149 WORD32 rc_get_actual_intra_frame_int(rate_control_api_t *ps_rate_control_api)
4150 {
4151 WORD32 i4_int = pic_type_get_actual_intra_frame_interval(ps_rate_control_api->ps_pic_handling);
4152 return (i4_int);
4153 }
4154 /****************************************************************************
4155 Function Name : rc_get_qscale_max_clip_in_second_pass
4156 Description : Get maximum qscale that is allowed based on average Qp for simple contents
4157 Inputs : ps_rate_control_api
4158
4159 Revision History:
4160 DD MM YYYY Author(s) Changes (Describe the changes made)
4161 *****************************************************************************/
4162
rc_get_qscale_max_clip_in_second_pass(rate_control_api_t * ps_rate_control_api)4163 float rc_get_qscale_max_clip_in_second_pass(rate_control_api_t *ps_rate_control_api)
4164 {
4165 float i4_max_qscale =
4166 ba_get_qscale_max_clip_in_second_pass(ps_rate_control_api->ps_bit_allocation);
4167 return (i4_max_qscale);
4168 }
4169 /****************************************************************************
4170 Function Name : rc_set_2pass_total_frames
4171 Description : Set the total number of frames in the stream
4172 Inputs : ps_rate_control_api
4173
4174 Revision History:
4175 DD MM YYYY Author(s) Changes (Describe the changes made)
4176 *****************************************************************************/
4177
rc_set_2pass_total_frames(rate_control_api_t * ps_rate_control_api,WORD32 i4_total_2pass_frames)4178 void rc_set_2pass_total_frames(rate_control_api_t *ps_rate_control_api, WORD32 i4_total_2pass_frames)
4179 {
4180 bit_alloc_set_2pass_total_frames(ps_rate_control_api->ps_bit_allocation, i4_total_2pass_frames);
4181 }
4182 /****************************************************************************
4183 Function Name : rc_set_2pass_avg_bit_rate
4184 Description : Set the average bit-rate based on consumption so far
4185 Inputs : ps_rate_control_api
4186
4187 Revision History:
4188 DD MM YYYY Author(s) Changes (Describe the changes made)
4189 *****************************************************************************/
4190
rc_set_2pass_avg_bit_rate(rate_control_api_t * ps_rate_control_api,LWORD64 i8_2pass_avg_bit_rate)4191 void rc_set_2pass_avg_bit_rate(
4192 rate_control_api_t *ps_rate_control_api, LWORD64 i8_2pass_avg_bit_rate)
4193 {
4194 ba_set_2pass_avg_bit_rate(ps_rate_control_api->ps_bit_allocation, i8_2pass_avg_bit_rate);
4195 }
4196 /****************************************************************************
4197 Function Name : rc_set_enable_look_ahead
4198 Description :
4199 Inputs : ps_rate_control_api
4200
4201 Revision History:
4202 DD MM YYYY Author(s) Changes (Describe the changes made)
4203 *****************************************************************************/
rc_set_enable_look_ahead(rate_control_api_t * ps_rate_control_api,WORD32 i4_enable_look_ahead)4204 void rc_set_enable_look_ahead(rate_control_api_t *ps_rate_control_api, WORD32 i4_enable_look_ahead)
4205 {
4206 ba_set_enable_look_ahead(ps_rate_control_api->ps_bit_allocation, i4_enable_look_ahead);
4207 }
4208 /****************************************************************************
4209 Function Name : rc_add_est_tot
4210 Description :
4211 Inputs : ps_rate_control_api
4212
4213 Revision History:
4214 DD MM YYYY Author(s) Changes (Describe the changes made)
4215 *****************************************************************************/
rc_add_est_tot(rate_control_api_t * ps_rate_control_api,WORD32 i4_tot_tex_bits)4216 void rc_add_est_tot(rate_control_api_t *ps_rate_control_api, WORD32 i4_tot_tex_bits)
4217 {
4218 rc_modify_est_tot(ps_rate_control_api, i4_tot_tex_bits);
4219 }
4220 /****************************************************************************
4221 Function Name : rc_init_buffer_info
4222 Description :
4223 Inputs : ps_rate_control_api
4224
4225 Revision History:
4226 DD MM YYYY Author(s) Changes (Describe the changes made)
4227 *****************************************************************************/
rc_init_buffer_info(rate_control_api_t * ps_rate_control_api,WORD32 * pi4_vbv_buffer_size,WORD32 * pi4_currEbf,WORD32 * pi4_maxEbf,WORD32 * pi4_drain_rate)4228 void rc_init_buffer_info(
4229 rate_control_api_t *ps_rate_control_api,
4230 WORD32 *pi4_vbv_buffer_size,
4231 WORD32 *pi4_currEbf,
4232 WORD32 *pi4_maxEbf,
4233 WORD32 *pi4_drain_rate)
4234 {
4235 *pi4_vbv_buffer_size = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
4236 *pi4_currEbf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer) +
4237 rc_get_estimate_bit_error(ps_rate_control_api);
4238 *pi4_maxEbf = get_cbr_max_ebf(ps_rate_control_api->ps_cbr_buffer);
4239 *pi4_drain_rate = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
4240 return;
4241 }
4242