• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 bit_allocation.c
23 *
24 * \brief
25 *    This file contain bit processing 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 <string.h>
40 #include <stdlib.h>
41 #include <assert.h>
42 #include <stdarg.h>
43 #include <math.h>
44 
45 /* User include files */
46 #include "ittiam_datatypes.h"
47 #include "mem_req_and_acq.h"
48 #include "rc_common.h"
49 #include "rc_cntrl_param.h"
50 #include "var_q_operator.h"
51 #include "fixed_point_error_bits.h"
52 #include "cbr_buffer_control.h"
53 #include "rc_rd_model.h"
54 #include "est_sad.h"
55 #include "cbr_buffer_control.h"
56 #include "picture_type.h"
57 #include "bit_allocation.h"
58 #include "trace_support.h"
59 #include "rc_frame_info_collector.h"
60 #include "rate_control_api.h"
61 
62 /** Macros **/
63 #define MIN(x, y) ((x) < (y)) ? (x) : (y)
64 #define MAX(x, y) ((x) < (y)) ? (y) : (x)
65 
66 /* State structure for bit allocation */
67 typedef struct
68 {
69     WORD32 i4_rem_bits_in_period;
70     /* Storing inputs */
71     WORD32 i4_tot_frms_in_gop;
72     WORD32 i4_num_intra_frm_interval;
73     WORD32 i4_bits_per_frm;
74 } rem_bit_in_prd_t;
75 
76 typedef struct bit_allocation_t
77 {
78     rem_bit_in_prd_t s_rbip;
79     WORD32
80         i2_K[MAX_PIC_TYPE]; /* A universal constant giving the relative complexity between pictures */
81     WORD32 i4_prev_frm_header_bits[MAX_PIC_TYPE]; /* To get a estimate of the header bits consumed */
82     WORD32 ai4_prev_frm_tot_bits[MAX_PIC_TYPE];
83     WORD32 ai4_prev_frm_tot_est_bits[MAX_PIC_TYPE];
84     WORD32 i4_bits_per_frm;
85     WORD32 i4_num_gops_in_period;
86     WORD32
87     i4_actual_num_gops_in_period; /* Num gops as set by rate control module */
88     WORD32 i4_saved_bits;
89     WORD32 i4_max_bits_per_frm[MAX_NUM_DRAIN_RATES];
90     WORD32 i4_min_bits_per_frm;
91     /* Error bits module */
92     error_bits_handle ps_error_bits;
93     /* Storing frame rate */
94     WORD32 i4_frame_rate;
95     WORD32 i4_bit_rate;
96     WORD32 ai4_peak_bit_rate[MAX_NUM_DRAIN_RATES];
97     WORD32 i4_max_tex_bits_for_i;
98     WORD32 i4_pels_in_frame;
99     /* Errors within GOP and across GOP */
100     WORD32 i4_gop_level_bit_error;
101     WORD32 i4_frame_level_bit_error;
102     WORD32 ai4_cur_frm_est_tex_bits[MAX_NUM_FRAME_PARALLEL];
103     WORD32 ai4_cur_frm_est_hdr_bits[MAX_NUM_FRAME_PARALLEL];
104     WORD32 i4_buffer_based_bit_error;
105     WORD32 i4_bits_from_buffer_in_cur_gop;
106     WORD32 i4_excess_bits_from_buffer;
107 
108     WORD32 i4_is_hbr;
109     WORD32 i4_rem_frame_in_period;
110     /*HEVC_RC : this will be updated by rc_interface.c to have number of SCD in lap window.
111         Ni will be incremented using this to bring down buffer level and also back to back scd within lap window*/
112     WORD32 i4_num_scd_in_lap_window;
113     WORD32 i4_num_frm_b4_scd;
114     WORD32 i4_num_active_pic_type;
115     WORD32 i4_lap_window;
116     WORD32 i4_field_pic;
117     WORD32 i4_ba_rc_pass;
118     void *pv_gop_stat;
119     LWORD64 i8_cur_gop_num;
120     LWORD64
121     i8_frm_num_in_gop; /*TBD: For enc loop parallel this variable needs to maintained outside rate control since qp will not be queried in actual bitstream order*/
122     float af_sum_weigh[MAX_PIC_TYPE][3];
123     LWORD64 i8_cur_gop_bit_consumption; /*To calculate the deviaiton in 2 pass*/
124     //LWORD64 i8_2pass_gop_error_accum;
125     LWORD64
126     i8_2pass_alloc_per_frm_bits; /*Per frame bits allocated to GOP in 2 pass*/
127     //LWORD64 i8_2pass_alloc_per_frm_bits_next_gop;
128 
129     float f_min_complexity_cross_peak_rate; /*complexity of gop beyond which it is clipped to peak rate in 2ns pass*/
130     WORD32 i4_next_sc_i_in_rc_look_ahead;
131     /*The peak factor is multiplied to get the total bits for a gop based on squashing function*/
132     float f_cur_peak_factor_2pass;
133     LWORD64 i8_total_bits_allocated;
134     WORD32 i4_luma_pels;
135     WORD32 i4_num_gop;
136     /*The bitrate will keep changing in 2 pass due to error*/
137     LWORD64 i8_current_bitrate_2_pass;
138     /*i4_flag_no_more_set_rbip - once we have reached the end of total number of frames to be encoded in
139     2nd pass sliding window bit allocation there is no need to set rbip again*/
140     WORD32 i4_flag_no_more_set_rbip;
141     /*i8_vbv_based_excess_for_segment will be distributed across the complex segments depending on the
142     ratio of current complexity to f_sum_complexity_segment_cross_peak*/
143     float f_sum_complexity_segment_cross_peak;
144     /*(i8_vbv_based_excess_for_segment)Buffer play excess is calculated for the entire segment of complex
145     content which may consist of multiple gop's*/
146     //LWORD64 i8_vbv_based_excess_for_segment;
147     /*I frame bit allocation during scene cuts is dependent on f_curr_i_to_sum which will signal
148     the complexity difference between current i to future i's if present in the same default gop*/
149     float f_curr_i_to_sum;
150     float f_curr_by_sum_subgop;
151     WORD32 ai4_pic_types_in_subgop[MAX_PIC_TYPE];
152     WORD32 i4_use_subgop_bit_alloc_flag;
153     WORD32 i4_num_frames_since_last_I_frame;
154     LWORD64 i8_first_pic_bits_pictype[MAX_PIC_TYPE];
155     LWORD64 i8_avg_bits_pictype[MAX_PIC_TYPE];
156     WORD32 i4_avg_qscale_gop_first_pass;
157     WORD32 i4_fp_bit_alloc_in_sp;
158     WORD32 i4_frame_level_error_ctr_update_rc;
159     float f_qscale_max_clip_in_second_pass;
160     float f_average_qscale_1st_pass;
161     float f_max_average_qscale_1st_pass;
162     LWORD64 i8_bit_consumption_so_far;
163     WORD32 i4_total_2pass_frames;
164     LWORD64 i8_2pass_avg_bit_rate;
165     WORD32 i4_br_id;
166 } bit_allocation_t;
167 
get_actual_num_frames_in_gop(pic_handling_handle ps_pic_handling)168 static WORD32 get_actual_num_frames_in_gop(pic_handling_handle ps_pic_handling)
169 {
170     WORD32 i4_tot_frms_in_gop = 0, i;
171     WORD32 ai4_actual_frms_in_gop[MAX_PIC_TYPE];
172     memset(ai4_actual_frms_in_gop, 0, MAX_PIC_TYPE * sizeof(WORD32));
173     pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_actual_frms_in_gop);
174     for(i = 0; i < MAX_PIC_TYPE; i++)
175     {
176         i4_tot_frms_in_gop += ai4_actual_frms_in_gop[i];
177     }
178     return (i4_tot_frms_in_gop);
179 }
180 
get_cur_peak_factor_2pass(bit_allocation_t * ps_bit_allocation)181 float get_cur_peak_factor_2pass(bit_allocation_t *ps_bit_allocation)
182 {
183     return (ps_bit_allocation->f_cur_peak_factor_2pass);
184 }
get_cur_min_complexity_factor_2pass(bit_allocation_t * ps_bit_allocation)185 float get_cur_min_complexity_factor_2pass(bit_allocation_t *ps_bit_allocation)
186 {
187     return (ps_bit_allocation->f_min_complexity_cross_peak_rate);
188 }
set_2pass_total_gops(bit_allocation_t * ps_bit_allocation,WORD32 i4_num_gop)189 void set_2pass_total_gops(bit_allocation_t *ps_bit_allocation, WORD32 i4_num_gop)
190 {
191     ps_bit_allocation->i4_num_gop = i4_num_gop;
192 }
193 #if NON_STEADSTATE_CODE
194 /* Module for accessing remaining bits in period */
195 /*****************************************************************************
196   Function Name : init_rbip
197   Description   : Initalises the remaining bits in period structure.
198   Inputs        : ps_rbip         -remaining bits in period structure
199                   ps_pic_handling - Pic handling structure
200                   num_intra_frm_interval - num of I frm intervals in this period
201                   i4_bits_per_frm - num bits per frm
202 Revision History:
203          DD MM YYYY   Author(s)       Changes (Describe the changes made)
204 *****************************************************************************/
init_rbip(rem_bit_in_prd_t * ps_rbip,pic_handling_handle ps_pic_handling,WORD32 i4_bits_per_frm,WORD32 i4_num_intra_frm_interval)205 static void init_rbip(
206     rem_bit_in_prd_t *ps_rbip,
207     pic_handling_handle ps_pic_handling,
208     WORD32 i4_bits_per_frm,
209     WORD32 i4_num_intra_frm_interval)
210 {
211     WORD32 i4_tot_frms_in_gop = get_actual_num_frames_in_gop(ps_pic_handling);
212     /* WORD32 i4_frm_correction_for_open_gop = 0; */
213     /* If the GOP is open, then we need to subtract the num_b_frames for the first gop */
214     /*if(!pic_type_is_gop_closed(ps_pic_handling))
215     {
216         i4_frm_correction_for_open_gop = pic_type_get_inter_frame_interval(ps_pic_handling)-1;
217     }*/
218     ps_rbip->i4_rem_bits_in_period =
219         i4_bits_per_frm *
220         (i4_tot_frms_in_gop * i4_num_intra_frm_interval /*- i4_frm_correction_for_open_gop*/);
221 
222     /* Store the total number of frames in GOP value which is
223      * used from module A */
224     ps_rbip->i4_tot_frms_in_gop = i4_tot_frms_in_gop;
225     ps_rbip->i4_num_intra_frm_interval = i4_num_intra_frm_interval;
226     ps_rbip->i4_bits_per_frm = i4_bits_per_frm;
227 }
228 #endif /* #if NON_STEADSTATE_CODE */
229 
230 /*****************************************************************************
231   Function Name : check_update_rbip
232   Description   : Function for updating rbip.
233   Inputs        : ps_rbip         -remaining bits in period structure
234                   ps_pic_handling - Pic handling structure
235 Revision History:
236          DD MM YYYY   Author(s)       Changes (Describe the changes made)
237 *****************************************************************************/
check_update_rbip(rem_bit_in_prd_t * ps_rbip,pic_handling_handle ps_pic_handling)238 static void check_update_rbip(rem_bit_in_prd_t *ps_rbip, pic_handling_handle ps_pic_handling)
239 {
240     /* NOTE: Intra frame interval changes aafter the first I frame that is encoded in a GOP */
241     WORD32 i4_new_tot_frms_in_gop = get_actual_num_frames_in_gop(ps_pic_handling);
242     if(i4_new_tot_frms_in_gop != ps_rbip->i4_tot_frms_in_gop)
243     {
244         WORD32 i4_num_frames_in_period = ps_rbip->i4_num_intra_frm_interval *
245                                          (i4_new_tot_frms_in_gop - ps_rbip->i4_tot_frms_in_gop);
246         overflow_avoided_summation(
247             &ps_rbip->i4_rem_bits_in_period, (ps_rbip->i4_bits_per_frm * i4_num_frames_in_period));
248     }
249     /* Updated the new values */
250     ps_rbip->i4_tot_frms_in_gop = i4_new_tot_frms_in_gop;
251 }
252 /*****************************************************************************
253   Function Name : ret_rbip_default_preenc
254   Description   : Function for calculating bits in period.
255   Inputs        : ps_rbip         -remaining bits in period structure
256                   ps_pic_handling - Pic handling structure
257 Revision History:
258          DD MM YYYY   Author(s)       Changes (Describe the changes made)
259 *****************************************************************************/
260 static WORD32
ret_rbip_default_preenc(rem_bit_in_prd_t * ps_rbip,pic_handling_handle ps_pic_handling)261     ret_rbip_default_preenc(rem_bit_in_prd_t *ps_rbip, pic_handling_handle ps_pic_handling)
262 {
263     WORD32 i4_bits_in_period =
264         pic_type_get_intra_frame_interval(ps_pic_handling) * ps_rbip->i4_bits_per_frm;
265     return (i4_bits_in_period);
266 }
267 /*****************************************************************************
268   Function Name : update_rbip
269   Description   : Function for updating rbip.
270   Inputs        : ps_rbip         -remaining bits in period structure
271                   ps_pic_handling - Pic handling structure
272 Revision History:
273          DD MM YYYY   Author(s)       Changes (Describe the changes made)
274 *****************************************************************************/
update_rbip(rem_bit_in_prd_t * ps_rbip,pic_handling_handle ps_pic_handling,WORD32 i4_num_of_bits)275 static WORD32 update_rbip(
276     rem_bit_in_prd_t *ps_rbip, pic_handling_handle ps_pic_handling, WORD32 i4_num_of_bits)
277 {
278     check_update_rbip(ps_rbip, ps_pic_handling);
279     overflow_avoided_summation(&ps_rbip->i4_rem_bits_in_period, i4_num_of_bits);
280     return (ps_rbip->i4_rem_bits_in_period);
281 }
282 /*****************************************************************************
283   Function Name : get_rbip_and_num_frames
284   Description   : Update rbip and get number of frames.
285   Inputs        : ps_rbip         -remaining bits in period structure
286                   ps_pic_handling - Pic handling structure
287                   pi4_num_frames  - pointer to update number of frmes
288 Revision History:
289          DD MM YYYY   Author(s)       Changes (Describe the changes made)
290 *****************************************************************************/
get_rbip_and_num_frames(rem_bit_in_prd_t * ps_rbip,pic_handling_handle ps_pic_handling,WORD32 i4_num_of_bits,WORD32 * pi4_num_frames)291 static LWORD64 get_rbip_and_num_frames(
292     rem_bit_in_prd_t *ps_rbip,
293     pic_handling_handle ps_pic_handling,
294     WORD32 i4_num_of_bits,
295     WORD32 *pi4_num_frames)
296 {
297     check_update_rbip(ps_rbip, ps_pic_handling);
298     overflow_avoided_summation(&ps_rbip->i4_rem_bits_in_period, i4_num_of_bits);
299     *pi4_num_frames = ps_rbip->i4_tot_frms_in_gop;
300     return (ps_rbip->i4_rem_bits_in_period);
301 }
302 /*****************************************************************************
303   Function Name : set_rbip
304   Description   : Set rbip
305   Inputs        : ps_rbip         -remaining bits in period structure
306                   i4_error_bits   - Error bits
307 Revision History:
308          DD MM YYYY   Author(s)       Changes (Describe the changes made)
309 *****************************************************************************/
set_rbip(rem_bit_in_prd_t * ps_rbip,WORD32 i4_error_bits)310 static WORD32 set_rbip(rem_bit_in_prd_t *ps_rbip, WORD32 i4_error_bits)
311 {
312     ps_rbip->i4_rem_bits_in_period = (ps_rbip->i4_bits_per_frm * ps_rbip->i4_tot_frms_in_gop *
313                                       ps_rbip->i4_num_intra_frm_interval) +
314                                      i4_error_bits;
315 
316     return ps_rbip->i4_rem_bits_in_period;
317 }
318 
319 /*****************************************************************************
320   Function Name : multi_pass_set_rbip
321   Description   : 2 pass set RBIP, since the gop bits shall not depend on bitrate or framerate,
322                   GOP bits is directly obtained from first pass data
323   Inputs        : ps_rbip         -remaining bits in period structure
324                   ps_pic_handling - Pic handling structure
325                   i4_cur_gop_bits - bits allocated for the curr gop
326                   i4_tot_frm_in_cur_gop - frames in the gop
327 Revision History:
328          DD MM YYYY   Author(s)       Changes (Describe the changes made)
329 *****************************************************************************/
multi_pass_set_rbip(rem_bit_in_prd_t * ps_rbip,pic_handling_handle ps_pic_handling,WORD32 i4_cur_gop_bits,WORD32 i4_tot_frm_in_cur_gop)330 static void multi_pass_set_rbip(
331     rem_bit_in_prd_t *ps_rbip,
332     pic_handling_handle ps_pic_handling,
333     WORD32 i4_cur_gop_bits,
334     WORD32 i4_tot_frm_in_cur_gop)
335 {
336     WORD32 i4_num_frames_in_gop = get_actual_num_frames_in_gop(ps_pic_handling);
337     ps_rbip->i4_rem_bits_in_period =
338         (WORD32)((LWORD64)i4_cur_gop_bits * i4_num_frames_in_gop / i4_tot_frm_in_cur_gop);
339     ps_rbip->i4_tot_frms_in_gop = i4_num_frames_in_gop;
340     ps_rbip->i4_bits_per_frm = ps_rbip->i4_rem_bits_in_period / i4_num_frames_in_gop;
341 }
change_rbip(rem_bit_in_prd_t * ps_rbip,WORD32 i4_new_bits_per_frm,WORD32 i4_new_num_intra_frm_interval)342 static void change_rbip(
343     rem_bit_in_prd_t *ps_rbip, WORD32 i4_new_bits_per_frm, WORD32 i4_new_num_intra_frm_interval)
344 {
345     if(i4_new_bits_per_frm != ps_rbip->i4_bits_per_frm)
346     {
347         WORD32 i4_rem_frms_in_period =
348             (ps_rbip->i4_num_intra_frm_interval) * ps_rbip->i4_tot_frms_in_gop;
349         overflow_avoided_summation(
350             &ps_rbip->i4_rem_bits_in_period,
351             ((i4_new_bits_per_frm - ps_rbip->i4_bits_per_frm) * i4_rem_frms_in_period));
352     }
353     if(i4_new_num_intra_frm_interval != ps_rbip->i4_num_intra_frm_interval)
354     {
355         overflow_avoided_summation(
356             &ps_rbip->i4_rem_bits_in_period,
357             (i4_new_bits_per_frm * ps_rbip->i4_tot_frms_in_gop *
358              (i4_new_num_intra_frm_interval - ps_rbip->i4_num_intra_frm_interval)));
359     }
360     /* Update the new value */
361     ps_rbip->i4_num_intra_frm_interval = i4_new_num_intra_frm_interval;
362     ps_rbip->i4_bits_per_frm = i4_new_bits_per_frm;
363 }
364 
365 #if NON_STEADSTATE_CODE
bit_allocation_num_fill_use_free_memtab(bit_allocation_t ** pps_bit_allocation,itt_memtab_t * ps_memtab,ITT_FUNC_TYPE_E e_func_type)366 WORD32 bit_allocation_num_fill_use_free_memtab(
367     bit_allocation_t **pps_bit_allocation, itt_memtab_t *ps_memtab, ITT_FUNC_TYPE_E e_func_type)
368 {
369     WORD32 i4_mem_tab_idx = 0;
370     static bit_allocation_t s_bit_allocation_temp;
371 
372     /* Hack for al alloc, during which we dont have any state memory.
373       Dereferencing can cause issues */
374     if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
375         (*pps_bit_allocation) = &s_bit_allocation_temp;
376 
377     /*for src rate control state structure*/
378     if(e_func_type != GET_NUM_MEMTAB)
379     {
380         fill_memtab(
381             &ps_memtab[i4_mem_tab_idx],
382             sizeof(bit_allocation_t),
383             MEM_TAB_ALIGNMENT,
384             PERSISTENT,
385             DDR);
386         use_or_fill_base(&ps_memtab[0], (void **)pps_bit_allocation, e_func_type);
387     }
388     i4_mem_tab_idx++;
389 
390     i4_mem_tab_idx += error_bits_num_fill_use_free_memtab(
391         &pps_bit_allocation[0]->ps_error_bits, &ps_memtab[i4_mem_tab_idx], e_func_type);
392 
393     return (i4_mem_tab_idx);
394 }
395 #endif /* #if NON_STEADSTATE_CODE */
396 
397 /*****************************************************************************
398   Function Name : get_bits_based_on_complexity
399   Description   : function calculates the bits to be allocated for the current
400                   picture type given the relative complexity between different
401                   picture types
402   Inputs        : i4_bits_in_period
403                   pi4_frms_in_period      - num frames of each pictype in the period
404                   pvq_complexity_estimate - complexity for each pictype
405                   e_pic_type              - current picture type
406                   i4_call_type
407   Revision History:
408          DD MM YYYY   Author(s)       Changes (Describe the changes made)
409 *****************************************************************************/
get_bits_based_on_complexity(bit_allocation_t * ps_bit_allocation,WORD32 i4_bits_in_period,WORD32 * pi4_frms_in_period,number_t * pvq_complexity_estimate,picture_type_e e_pic_type,WORD32 i4_call_type)410 static WORD32 get_bits_based_on_complexity(
411     bit_allocation_t *ps_bit_allocation,
412     WORD32 i4_bits_in_period,
413     WORD32 *pi4_frms_in_period,
414     number_t *pvq_complexity_estimate,
415     picture_type_e e_pic_type,
416     WORD32 i4_call_type)
417 {
418     WORD32 i, i4_estimated_bits;
419     number_t vq_bits_in_period, vq_frms_in_period[MAX_PIC_TYPE], vq_comp_coeff,
420         vq_est_texture_bits_for_frm;
421     WORD32 i4_num_scd_in_LAP_window = ps_bit_allocation->i4_num_scd_in_lap_window;
422     WORD32 i4_active_pic_types = ps_bit_allocation->i4_num_active_pic_type,
423            i4_field_pic = ps_bit_allocation->i4_field_pic;
424     float af_sum_weigh[MAX_PIC_TYPE][3];
425 
426     memmove(af_sum_weigh, ps_bit_allocation->af_sum_weigh, ((sizeof(float)) * MAX_PIC_TYPE * 3));
427 
428     /** Increment I frame count if there is scene cut in LAP window*/
429     i4_num_scd_in_LAP_window = 0;
430     pi4_frms_in_period[I_PIC] += i4_num_scd_in_LAP_window;
431     /* Converting inputs to var_q format */
432     SET_VAR_Q(vq_bits_in_period, i4_bits_in_period, 0);
433     for(i = 0; i < MAX_PIC_TYPE; i++)
434     {
435         SET_VAR_Q(vq_frms_in_period[i], pi4_frms_in_period[i], 0);
436     }
437     /******************************************************************
438     Estimated texture bits =
439             (remaining bits) * (cur frm complexity)
440             ---------------------------------------
441     (num_i_frm*i_frm_complexity) + (num_p_frm*pfrm_complexity) + (b_frm * b_frm_cm)
442     *******************************************************************/
443     /*Taking the numerator weight into account*/
444     if(i4_call_type == 1)
445     {
446         trace_printf("1 CUrr / avg %f", af_sum_weigh[e_pic_type][0]);
447     }
448     if(af_sum_weigh[e_pic_type][0] > 4.0f)
449         af_sum_weigh[e_pic_type][0] = 4.0f;
450     if(af_sum_weigh[e_pic_type][0] < 0.3f)
451         af_sum_weigh[e_pic_type][0] = 0.3f;
452     if(i4_call_type == 1)
453     {
454         trace_printf("2 CUrr / avg %f", af_sum_weigh[e_pic_type][0]);
455     }
456 
457     if((ps_bit_allocation->i4_ba_rc_pass != 2) || (i4_call_type == 0) ||
458        (ps_bit_allocation->i4_fp_bit_alloc_in_sp == 0))
459     {
460         convert_float_to_fix(af_sum_weigh[e_pic_type][0], &vq_comp_coeff);
461         mult32_var_q(vq_bits_in_period, vq_comp_coeff, &vq_bits_in_period);
462         mult32_var_q(vq_bits_in_period, pvq_complexity_estimate[e_pic_type], &vq_bits_in_period);
463     }
464     else
465     {
466         WORD32 i4_frame_num = (WORD32)ps_bit_allocation->i8_frm_num_in_gop, i4_offset;
467         gop_level_stat_t *ps_gop;
468         LWORD64 i8_firs_pass_tot_bits;
469         float f_bits_cur_pic, f_offset;
470         ps_gop =
471             (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
472         i8_firs_pass_tot_bits = ps_gop->ai8_tex_bits_consumed[i4_frame_num] +
473                                 ps_gop->ai8_head_bits_consumed[i4_frame_num];
474         i4_offset = (ps_gop->ai4_q6_frame_offsets[i4_frame_num] * 1000) >> QSCALE_Q_FAC;
475         f_offset = ((float)i4_offset) / 1000;
476         f_bits_cur_pic =
477             (float)(i8_firs_pass_tot_bits * ps_gop->ai4_first_pass_qscale[i4_frame_num]) /
478             (ps_bit_allocation->i4_avg_qscale_gop_first_pass * f_offset);
479         convert_float_to_fix(f_bits_cur_pic, &vq_comp_coeff);
480         mult32_var_q(vq_bits_in_period, vq_comp_coeff, &vq_bits_in_period);
481 
482         for(i = 0; i < MAX_PIC_TYPE; i++)
483         {
484             number_t temp;
485             convert_float_to_fix((float)ps_bit_allocation->i8_avg_bits_pictype[i], &temp);
486             pvq_complexity_estimate[i] = temp;
487         }
488     }
489 
490     for(i = 0; i < MAX_PIC_TYPE; i++)
491     {
492         /*For 2nd pass we will be reducing the num of pictures as they are coded so we dont want to replace 0's*/
493         if(af_sum_weigh[i][1] == 0.0 &&
494            !((i4_call_type == 1) && (ps_bit_allocation->i4_ba_rc_pass == 2)))
495             af_sum_weigh[i][1] = (float)pi4_frms_in_period[i];
496 
497         convert_float_to_fix(af_sum_weigh[i][1], &vq_comp_coeff);
498         mult32_var_q(vq_comp_coeff, pvq_complexity_estimate[i], &vq_frms_in_period[i]);
499     }
500 
501     /* changed the index range from active_pic to max_pic*/
502     if(i4_field_pic)
503     {
504         for(i = 1; i < i4_active_pic_types; i++)
505         {
506             add32_var_q(vq_frms_in_period[I_PIC], vq_frms_in_period[i], &vq_frms_in_period[I_PIC]);
507             add32_var_q(
508                 vq_frms_in_period[I_PIC],
509                 vq_frms_in_period[i + FIELD_OFFSET],
510                 &vq_frms_in_period[I_PIC]);
511         }
512     }
513     else /*field case*/
514     {
515         for(i = 1; i < i4_active_pic_types; i++)
516         {
517             add32_var_q(vq_frms_in_period[I_PIC], vq_frms_in_period[i], &vq_frms_in_period[I_PIC]);
518         }
519     }
520 
521     div32_var_q(vq_bits_in_period, vq_frms_in_period[I_PIC], &vq_est_texture_bits_for_frm);
522     number_t_to_word32(vq_est_texture_bits_for_frm, &i4_estimated_bits);
523 
524     /* If the number of frames are zero then return zero */
525     if(!pi4_frms_in_period[e_pic_type])
526         i4_estimated_bits = 0;
527     return (i4_estimated_bits);
528 }
529 /*****************************************************************************
530   Function Name : assign_complexity_coeffs
531   Description   :
532   Inputs        : af_sum_weigh
533   Revision History:
534          DD MM YYYY   Author(s)       Changes (Describe the changes made)
535 *****************************************************************************/
assign_complexity_coeffs(bit_allocation_t * ps_bit_allocation,float af_sum_weigh[MAX_PIC_TYPE][3])536 void assign_complexity_coeffs(
537     bit_allocation_t *ps_bit_allocation, float af_sum_weigh[MAX_PIC_TYPE][3])
538 {
539     WORD32 i;
540     for(i = 0; i < MAX_PIC_TYPE; i++)
541     {
542         ps_bit_allocation->af_sum_weigh[i][0] = af_sum_weigh[i][0];
543         ps_bit_allocation->af_sum_weigh[i][1] = af_sum_weigh[i][1];
544         ps_bit_allocation->af_sum_weigh[i][2] = af_sum_weigh[i][2];
545     }
546 }
547 /*****************************************************************************
548   Function Name : ba_get_rbip_and_num_frames
549   Description   :
550   Inputs        : pi4_num_frames
551   Revision History:
552          DD MM YYYY   Author(s)       Changes (Describe the changes made)
553 *****************************************************************************/
ba_get_rbip_and_num_frames(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling,WORD32 * pi4_num_frames)554 LWORD64 ba_get_rbip_and_num_frames(
555     bit_allocation_t *ps_bit_allocation,
556     pic_handling_handle ps_pic_handling,
557     WORD32 *pi4_num_frames)
558 {
559     return (
560         get_rbip_and_num_frames(&ps_bit_allocation->s_rbip, ps_pic_handling, 0, pi4_num_frames));
561 }
562 /*****************************************************************************
563   Function Name : init_prev_header_bits
564   Description   : Intialise header bits for each pic type
565   Inputs        : ps_bit_allocation
566                   ps_pic_handling
567   Revision History:
568          DD MM YYYY   Author(s)       Changes (Describe the changes made)
569 *****************************************************************************/
init_prev_header_bits(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling)570 void init_prev_header_bits(bit_allocation_t *ps_bit_allocation, pic_handling_handle ps_pic_handling)
571 {
572     WORD32 i4_rem_bits_in_period, /* ai4_rem_frms_in_period[MAX_PIC_TYPE], */
573         ai4_frms_in_period[MAX_PIC_TYPE], i, j;
574     number_t avq_complexity_estimate[MAX_PIC_TYPE];
575     WORD32 i4_field_pic;
576     i4_field_pic = pic_type_get_field_pic(ps_pic_handling);
577     /* Assigning Percentages of I, P and B frame header bits based on actual bits allocated for I, P and B frames */
578     /* Getting the remaining bits in period */
579     i4_rem_bits_in_period = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
580 
581     /* Hardcoding the bit ratios between I, P and B */
582     SET_VAR_Q(
583         avq_complexity_estimate[I_PIC],
584         (I_TO_P_BIT_RATIO * P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
585         0);
586     SET_VAR_Q(
587         avq_complexity_estimate[P_PIC],
588         (P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
589         0);
590     SET_VAR_Q(
591         avq_complexity_estimate[P1_PIC],
592         (P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
593         0);
594     SET_VAR_Q(avq_complexity_estimate[B_PIC], (B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO), 0);
595     SET_VAR_Q(avq_complexity_estimate[BB_PIC], (B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO), 0);
596     SET_VAR_Q(avq_complexity_estimate[B1_PIC], (B1_TO_B2_BIT_RATIO), 0);
597     SET_VAR_Q(
598         avq_complexity_estimate[B11_PIC],
599         (B1_TO_B2_BIT_RATIO),
600         0);  //temporarliy treat ref and non ref as same
601     SET_VAR_Q(avq_complexity_estimate[B2_PIC], 1, 0);
602     SET_VAR_Q(avq_complexity_estimate[B22_PIC], 1, 0);
603 
604     /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
605     /* pic_type_get_rem_frms_in_gop(ps_pic_handling, ai4_rem_frms_in_period); */
606     pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_period);
607 
608     /* Depending on the number of gops in a period, find the num_frms_in_prd */
609     for(j = 0; j < MAX_PIC_TYPE; j++)
610     {
611         ai4_frms_in_period[j] = (ai4_frms_in_period[j] * ps_bit_allocation->i4_num_gops_in_period);
612     }
613 
614     /* Percentage of header bits in teh overall bits allocated to I, P and B frames
615     when the data is not known. Since this value is based on bitrate using a equation
616     to fit the values. Ran the header bit ratio for [1080@30] carnival, ihits and
617     football at 9, 12 and 16 mbps and based on that deriving a equation using bpp.
618     Values obtained are:
619     (bitrate/bpp)   I              P              B
620     9/2.87          0.358617291155 0.549124350786 0.798772545232
621     12/3.83         0.288633642796 0.444797334749 0.693933711801
622     16/5.11         0.284241839623 0.330152764298 0.557999732549
623     Equation for I:
624         if bpp > 3.83 hdr = 0.29
625         else hdr = -0.073*bpp + 0.569
626     Equation for P: hdr = -0.098*bpp + 0.825
627     Equation for B: hdr = -0.108*bpp + 1.108
628     */
629     {
630 #define FRAME_HEADER_BITS_Q_FACTOR (10)
631         WORD32 ai4_header_bits_percentage[MAX_PIC_TYPE];
632 
633         WORD32 i4_bpp;
634         X_PROD_Y_DIV_Z(
635             ps_bit_allocation->i4_bits_per_frm,
636             (1 << FRAME_HEADER_BITS_Q_FACTOR),
637             ps_bit_allocation->i4_pels_in_frame,
638             i4_bpp);
639         //ps_bit_allocation->i4_bits_per_frm*(1<<FRAME_HEADER_BITS_Q_FACTOR)/ps_bit_allocation->i4_pels_in_frame;
640 
641         if(i4_bpp > 131)
642             ai4_header_bits_percentage[I_PIC] = 297;
643         else
644             ai4_header_bits_percentage[I_PIC] =
645                 ((-2238 * i4_bpp) >> FRAME_HEADER_BITS_Q_FACTOR) + 583;
646         ai4_header_bits_percentage[P_PIC] = ((-2990 * i4_bpp) >> FRAME_HEADER_BITS_Q_FACTOR) + 845;
647 
648         ai4_header_bits_percentage[B_PIC] = ((-3308 * i4_bpp) >> FRAME_HEADER_BITS_Q_FACTOR) + 1135;
649 
650         /* Changes for 2B subGOP */
651         ai4_header_bits_percentage[P_PIC] = (ai4_header_bits_percentage[P_PIC] * 13) >> 4;
652         ai4_header_bits_percentage[P1_PIC] = (ai4_header_bits_percentage[P_PIC] * 13) >> 4;
653         ai4_header_bits_percentage[B_PIC] = (ai4_header_bits_percentage[B_PIC] * 12) >> 4;
654         ai4_header_bits_percentage[BB_PIC] = (ai4_header_bits_percentage[B_PIC] * 12) >> 4;
655         /*HEVC_hierarchy: temp change consider same percentage because of insufficient data*/
656         ai4_header_bits_percentage[B1_PIC] = ai4_header_bits_percentage[B_PIC];
657         ai4_header_bits_percentage[B11_PIC] = ai4_header_bits_percentage[B_PIC];
658         ai4_header_bits_percentage[B2_PIC] = ai4_header_bits_percentage[B_PIC];
659         ai4_header_bits_percentage[B22_PIC] = ai4_header_bits_percentage[B_PIC];
660 
661         for(i = 0; i < MAX_PIC_TYPE; i++)
662         {
663             ps_bit_allocation->af_sum_weigh[i][0] = 1.0;
664             ps_bit_allocation->af_sum_weigh[i][1] = 0.0;
665             ps_bit_allocation->af_sum_weigh[i][2] = 0.0;
666         }
667 
668         for(i = 0; i < MAX_PIC_TYPE; i++)
669         {
670             /* Getting the total bits allocated for each picture type */
671             WORD32 i4_num_bits_allocated = get_bits_based_on_complexity(
672                 ps_bit_allocation,
673                 i4_rem_bits_in_period,
674                 ai4_frms_in_period,
675                 avq_complexity_estimate,
676                 (picture_type_e)i,
677                 0);
678 
679             if(ai4_header_bits_percentage[i] < 0)
680                 ai4_header_bits_percentage[i] = 0;
681 
682             ps_bit_allocation->i4_prev_frm_header_bits[i] = (WORD32)(
683                 ((LWORD64)ai4_header_bits_percentage[i] * i4_num_bits_allocated) >>
684                 FRAME_HEADER_BITS_Q_FACTOR);
685         }
686     }
687 }
688 
689 /*****************************************************************************
690   Function Name : init_bit_allocation
691   Description   : Initalises the bit_allocation structure.
692   Inputs        : intra_frm_interval - num frames between two I frames
693                   num_intra_frm_interval - num such intervals
694                   i4_bit_rate - num bits per second
695                   i4_frm_rate - num frms in 1000 seconds
696                   i4_num_active_pic_type
697   Revision History:
698          DD MM YYYY   Author(s)       Changes (Describe the changes made)
699 *****************************************************************************/
700 #if NON_STEADSTATE_CODE
init_bit_allocation(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling,WORD32 i4_num_intra_frm_interval,WORD32 i4_bit_rate,WORD32 i4_frm_rate,WORD32 * i4_peak_bit_rate,WORD32 i4_min_bitrate,WORD32 i4_pels_in_frame,WORD32 i4_is_hbr,WORD32 i4_num_active_pic_type,WORD32 i4_lap_window,WORD32 i4_field_pic,WORD32 rc_pass,WORD32 i4_luma_pels,WORD32 i4_fp_bit_alloc_in_sp)701 void init_bit_allocation(
702     bit_allocation_t *ps_bit_allocation,
703     pic_handling_handle ps_pic_handling,
704     WORD32 i4_num_intra_frm_interval, /* num such intervals */
705     WORD32 i4_bit_rate, /* num bits per second */
706     WORD32 i4_frm_rate, /* num frms in 1000 seconds */
707     WORD32 *i4_peak_bit_rate,
708     WORD32 i4_min_bitrate, /* The minimum bit rate that is to be satisfied for a gop */
709     WORD32 i4_pels_in_frame,
710     WORD32 i4_is_hbr,
711     WORD32 i4_num_active_pic_type,
712     WORD32 i4_lap_window,
713     WORD32 i4_field_pic,
714     WORD32 rc_pass,
715     WORD32 i4_luma_pels,
716     WORD32 i4_fp_bit_alloc_in_sp)
717 {
718     WORD32 i;
719     WORD32 i4_bits_per_frm, i4_max_bits_per_frm[MAX_NUM_DRAIN_RATES];
720     /* Store the pels in frame value */
721     ps_bit_allocation->i4_pels_in_frame = i4_pels_in_frame;
722     ps_bit_allocation->i4_num_scd_in_lap_window = 0;
723     ps_bit_allocation->i4_num_frm_b4_scd = 0;
724     ps_bit_allocation->i4_num_active_pic_type = i4_num_active_pic_type;
725     ps_bit_allocation->i4_field_pic = i4_field_pic;
726     ps_bit_allocation->i4_ba_rc_pass = rc_pass;
727     ps_bit_allocation->i4_br_id = 0; /* 0 - peak, 1 - average*/
728     ps_bit_allocation->i8_cur_gop_num =
729         0; /*Will be incremented after first frame allocation is done(during init itslef)*/
730     ps_bit_allocation->i8_frm_num_in_gop = 0;
731     ps_bit_allocation->pv_gop_stat =
732         NULL; /*In 2 pass the gop stat pointer is set API parameter call*/
733     ps_bit_allocation->f_min_complexity_cross_peak_rate =
734         1.0; /*In single pass buffer based additional bits movement is disabled, hence set to max complexity
735                                                                 Reset to lower value in case of two pass*/
736 
737     ps_bit_allocation->f_cur_peak_factor_2pass = -1.0;
738     ps_bit_allocation->i8_total_bits_allocated = -1;
739     ps_bit_allocation->i4_luma_pels = i4_luma_pels;
740     ps_bit_allocation->i4_num_gop = -1;
741     ps_bit_allocation->f_sum_complexity_segment_cross_peak = 0.0f;
742     //ps_bit_allocation->i8_vbv_based_excess_for_segment = 0;
743     ps_bit_allocation->i4_flag_no_more_set_rbip = 0;
744     ps_bit_allocation->f_curr_i_to_sum = 1.0f;
745     ps_bit_allocation->i4_fp_bit_alloc_in_sp = i4_fp_bit_alloc_in_sp;
746 
747     /* Calculate the bits per frame */
748     X_PROD_Y_DIV_Z(i4_bit_rate, 1000, i4_frm_rate, i4_bits_per_frm);
749     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
750     {
751         X_PROD_Y_DIV_Z(i4_peak_bit_rate[i], 1000, i4_frm_rate, i4_max_bits_per_frm[i]);
752     }
753     /* Initialize the bits_per_frame */
754     ps_bit_allocation->i4_bits_per_frm = i4_bits_per_frm;
755     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
756     {
757         ps_bit_allocation->i4_max_bits_per_frm[i] = i4_max_bits_per_frm[i];
758     }
759     X_PROD_Y_DIV_Z(i4_min_bitrate, 1000, i4_frm_rate, ps_bit_allocation->i4_min_bits_per_frm);
760 
761     /* Initialise the rem_bits in period
762        The first gop in case of an OPEN GOP may have fewer B_PICs,
763        That condition is not taken care of */
764     init_rbip(
765         &ps_bit_allocation->s_rbip, ps_pic_handling, i4_bits_per_frm, i4_num_intra_frm_interval);
766 
767     /* Initialize the num_gops_in_period */
768     ps_bit_allocation->i4_num_gops_in_period = i4_num_intra_frm_interval;
769     ps_bit_allocation->i4_actual_num_gops_in_period = i4_num_intra_frm_interval;
770 
771     /* Relative complexity between I and P frames */
772     ps_bit_allocation->i2_K[I_PIC] = (1 << K_Q);
773     ps_bit_allocation->i2_K[P_PIC] = I_TO_P_RATIO;
774     ps_bit_allocation->i2_K[P1_PIC] = I_TO_P_RATIO;
775     ps_bit_allocation->i2_K[B_PIC] = (P_TO_B_RATIO * I_TO_P_RATIO) >> K_Q;
776     ps_bit_allocation->i2_K[BB_PIC] = (P_TO_B_RATIO * I_TO_P_RATIO) >> K_Q;
777 
778     /*HEVC_hierarchy: force qp offset with one high level of hierarchy*/
779     ps_bit_allocation->i2_K[B1_PIC] = (B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q);
780     ps_bit_allocation->i2_K[B11_PIC] = (B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q);
781     ps_bit_allocation->i2_K[B2_PIC] =
782         (B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q + K_Q);
783     ps_bit_allocation->i2_K[B22_PIC] =
784         (B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q + K_Q);
785 
786     /* Initialize the saved bits to 0*/
787     ps_bit_allocation->i4_saved_bits = 0;
788 
789     /* Update the error bits module with average bits */
790     init_error_bits(ps_bit_allocation->ps_error_bits, i4_frm_rate, i4_bit_rate);
791     /* Store the input for implementing change in values */
792     ps_bit_allocation->i4_frame_rate = i4_frm_rate;
793     ps_bit_allocation->i4_bit_rate = i4_bit_rate;
794     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
795         ps_bit_allocation->ai4_peak_bit_rate[i] = i4_peak_bit_rate[i];
796 
797     ps_bit_allocation->i4_is_hbr = i4_is_hbr;
798     /* Initilising the header bits to be used for each picture type */
799     init_prev_header_bits(ps_bit_allocation, ps_pic_handling);
800 
801     /*HEVC_RC*/
802     /*remember prev frames tot bit consumption. This is required to calcualte error after first sub gop where estimate is not known*/
803     for(i = 0; i < MAX_PIC_TYPE; i++)
804     {
805         ps_bit_allocation->ai4_prev_frm_tot_bits[i] =
806             -1;  //-1 indicates that pic type has not been encoded
807         ps_bit_allocation->ai4_prev_frm_tot_est_bits[i] = -1;
808     }
809 
810     /* #define STATIC_I_TO_P_RATIO ((STATIC_I_TO_B_RATIO)/(STATIC_P_TO_B_RATIO)) */
811     /* Calcualte the max i frame bits */
812     {
813         WORD32 ai4_frms_in_period[MAX_PIC_TYPE];
814         WORD32 ai4_actual_frms_in_period[MAX_PIC_TYPE], i4_actual_frms_in_period = 0;
815         WORD32 i4_rem_texture_bits, j, i4_tot_header_bits_est = 0;
816         number_t avq_complexity_estimate[MAX_PIC_TYPE];
817         WORD32 i4_total_frms;
818 
819         /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
820         pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_period);
821         pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_actual_frms_in_period);
822         /* Depending on the number of gops in a period, find the num_frms_in_prd */
823         i4_total_frms = 0;
824         for(j = 0; j < MAX_PIC_TYPE; j++)
825         {
826             ai4_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period;
827             ai4_actual_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period;
828             i4_total_frms += ai4_frms_in_period[j];
829             i4_actual_frms_in_period += ai4_actual_frms_in_period[j];
830         }
831         ps_bit_allocation->i4_rem_frame_in_period = i4_actual_frms_in_period; /*i_only*/
832 
833         for(j = 0; j < MAX_PIC_TYPE; j++)
834         {
835             i4_tot_header_bits_est +=
836                 ai4_frms_in_period[j] * ps_bit_allocation->i4_prev_frm_header_bits[j];
837         }
838         /* Remove the header bits from the remaining bits to find how many bits you
839         can transfer.*/
840         i4_rem_texture_bits =
841             ps_bit_allocation->i4_bits_per_frm * i4_actual_frms_in_period - i4_tot_header_bits_est;
842 
843         /* Set the complexities for static content */
844         SET_VAR_Q(avq_complexity_estimate[I_PIC], STATIC_I_TO_B2_RATIO, 0);
845         SET_VAR_Q(avq_complexity_estimate[P_PIC], STATIC_P_TO_B2_RATIO, 0);
846         SET_VAR_Q(avq_complexity_estimate[P1_PIC], STATIC_P_TO_B2_RATIO, 0);
847         SET_VAR_Q(avq_complexity_estimate[B_PIC], STATIC_B_TO_B2_RATIO, 0);
848         SET_VAR_Q(avq_complexity_estimate[BB_PIC], STATIC_B_TO_B2_RATIO, 0);
849         SET_VAR_Q(avq_complexity_estimate[B1_PIC], STATIC_B1_TO_B2_RATIO, 0);
850         SET_VAR_Q(avq_complexity_estimate[B11_PIC], STATIC_B1_TO_B2_RATIO, 0);
851         SET_VAR_Q(avq_complexity_estimate[B2_PIC], 1, 0);
852         SET_VAR_Q(avq_complexity_estimate[B22_PIC], 1, 0);
853         /* Get the texture bits required for the current frame */
854         ps_bit_allocation->i4_max_tex_bits_for_i = get_bits_based_on_complexity(
855             ps_bit_allocation,
856             i4_rem_texture_bits,
857             ai4_frms_in_period,
858             avq_complexity_estimate,
859             I_PIC,
860             0);
861     }
862     /* initialise the GOP and bit errors to zero */
863     ps_bit_allocation->i4_gop_level_bit_error = 0;
864     ps_bit_allocation->i4_frame_level_bit_error = 0;
865     for(i = 0; i < MAX_NUM_FRAME_PARALLEL; i++)
866     {
867         ps_bit_allocation->ai4_cur_frm_est_tex_bits[i] = 0;
868         ps_bit_allocation->ai4_cur_frm_est_hdr_bits[i] = 0;
869     }
870     ps_bit_allocation->i4_buffer_based_bit_error = 0;
871     ps_bit_allocation->i4_bits_from_buffer_in_cur_gop = 0;
872     ps_bit_allocation->i4_excess_bits_from_buffer = 0;
873     ps_bit_allocation->i4_lap_window = i4_lap_window;
874     ps_bit_allocation->i8_cur_gop_bit_consumption = 0;
875     //ps_bit_allocation->i8_2pass_gop_error_accum = 0;
876     ps_bit_allocation->f_qscale_max_clip_in_second_pass = (float)0x7FFFFFFF;
877 
878     /*Buffer play for single pass*/
879     if(rc_pass != 2)
880     {
881         /*Find ps_bit_allocation->f_min_complexity_cross_peak_rate*/
882         /*Find the complexity which maps to peak bit-rate*/
883         {
884             ps_bit_allocation->f_min_complexity_cross_peak_rate = ba_get_min_complexity_for_peak_br(
885                 i4_peak_bit_rate[0], i4_bit_rate, 10.0f, 1.0f, 0.0f, rc_pass);
886         }
887     }
888 
889     ps_bit_allocation->i4_total_2pass_frames = 0;
890     ps_bit_allocation->i8_2pass_avg_bit_rate = -1;
891 }
892 
893 /*****************************************************************************
894   Function Name : ba_init_stat_data
895   Description   : The parsing of stat file is done at the end of init (by that time
896                   bit allocation init would have already happened,The memory for gop
897                   stat data is alocated inside the parse stat file code. Hence the
898                   pointer has to be updated again.
899   Inputs        : ps_bit_allocation
900                   ps_pic_handling
901                   pv_gop_stat      - pointer to update
902   Revision History:
903          DD MM YYYY   Author(s)       Changes (Describe the changes made)
904 *****************************************************************************/
ba_init_stat_data(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling,void * pv_gop_stat,WORD32 * pi4_pic_dist_in_cur_gop,WORD32 i4_total_bits_in_period,WORD32 i4_excess_bits)905 void ba_init_stat_data(
906     bit_allocation_t *ps_bit_allocation,
907     pic_handling_handle ps_pic_handling,
908     void *pv_gop_stat,
909     WORD32 *pi4_pic_dist_in_cur_gop,
910     WORD32 i4_total_bits_in_period,
911     WORD32 i4_excess_bits)
912 
913 {
914     WORD32 i4_tot_frames_in_gop = 0, i;
915 
916     ps_bit_allocation->pv_gop_stat = pv_gop_stat;
917 
918     /*Init RBIP*/
919     /*Get the complexity*/
920     ASSERT(ps_bit_allocation->i8_cur_gop_num == 0);
921     ASSERT(ps_bit_allocation->i8_frm_num_in_gop == 0);
922 
923     /*INit frames of each type*/
924     for(i = 0; i < MAX_PIC_TYPE; i++)
925     {
926         i4_tot_frames_in_gop += pi4_pic_dist_in_cur_gop[i];
927     }
928 
929     /*ASSERT(i4_tot_frames_in_gop == i4_intra_period);*/
930     /*Also allocate data for first GOP*/
931     /*Added for getting actual gop structure*/
932     pic_type_update_frms_in_gop(ps_pic_handling, pi4_pic_dist_in_cur_gop);
933 
934     multi_pass_set_rbip(
935         &ps_bit_allocation->s_rbip, ps_pic_handling, i4_total_bits_in_period, i4_tot_frames_in_gop);
936 
937     ps_bit_allocation->i8_2pass_alloc_per_frm_bits =
938         (i4_total_bits_in_period + (i4_tot_frames_in_gop >> 1)) / i4_tot_frames_in_gop;
939     ps_bit_allocation->i8_bit_consumption_so_far = 0;
940 
941     ASSERT(ps_bit_allocation->i4_ba_rc_pass == 2);
942 }
943 #endif /* #if NON_STEADSTATE_CODE */
944 
945 /*****************************************************************************
946   Function Name : bit_alloc_get_intra_bits
947   Description   :
948   Inputs        : ps_bit_allocation
949                   ps_pic_handling
950                   pvq_complexity_estimate
951                   I_to_avg_rest
952   Revision History:
953          DD MM YYYY   Author(s)       Changes (Describe the changes made)
954 *****************************************************************************/
bit_alloc_get_intra_bits(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling,cbr_buffer_handle ps_cbr_buf_handling,picture_type_e e_pic_type,number_t * pvq_complexity_estimate,WORD32 i4_is_scd,float I_to_avg_rest,WORD32 i4_call_type,WORD32 i4_non_I_scd,float f_percent_head_bits)955 WORD32 bit_alloc_get_intra_bits(
956     bit_allocation_t *ps_bit_allocation,
957     pic_handling_handle ps_pic_handling,
958     cbr_buffer_handle ps_cbr_buf_handling,
959     picture_type_e e_pic_type,
960     number_t *pvq_complexity_estimate,
961     WORD32 i4_is_scd,
962     float I_to_avg_rest,
963     WORD32 i4_call_type,
964     WORD32 i4_non_I_scd,
965     float f_percent_head_bits)
966 {
967     WORD32 ai4_frms_in_period[MAX_PIC_TYPE], ai4_frm_in_gop[MAX_PIC_TYPE], tot_frms_in_period = 0;
968     WORD32 i4_field_pic,
969         i4_safe_margin = 0,
970         i4_lap_window;  //margin in buffer to handle I frames that can come immediately after encoding huge static I frame
971     /*obtain buffer size*/
972     WORD32 i4_buffer_size =
973         ((get_cbr_buffer_size(ps_cbr_buf_handling)) >> 4) * UPPER_THRESHOLD_EBF_Q4;
974     WORD32 i4_cur_buf_pos = get_cbr_ebf(ps_cbr_buf_handling), i4_max_buffer_based,
975            i4_max_buffer_based_I_pic, i, i4_num_scaled_frms = 1;
976     WORD32 i4_bit_alloc_window =
977         (ps_bit_allocation->s_rbip.i4_tot_frms_in_gop *
978          ps_bit_allocation->s_rbip.i4_num_intra_frm_interval);
979     WORD32 i4_num_buf_frms,
980         ai4_frms_in_baw[MAX_PIC_TYPE];  //window for which I frame bit allocation is done
981     WORD32 i4_bits_in_period, i4_frames_in_buf = 0, i4_default_bits_in_period = 0;
982     WORD32 i4_est_bits_for_I, i4_peak_drain_rate, i4_subgop_size;
983     rc_type_e rc_type = get_rc_type(ps_cbr_buf_handling);
984     pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_frm_in_gop);
985 
986     for(i = 0; i < MAX_PIC_TYPE; i++)
987     {
988         ai4_frms_in_baw[i] =
989             ai4_frm_in_gop[i] * ps_bit_allocation->s_rbip.i4_num_intra_frm_interval;
990         ai4_frms_in_period[i] =
991             ai4_frm_in_gop[i] * ps_bit_allocation->s_rbip.i4_num_intra_frm_interval;
992         tot_frms_in_period += ai4_frm_in_gop[i];
993     }
994 
995     if(i4_call_type == 1)
996     {
997         i4_default_bits_in_period = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
998         if((i4_default_bits_in_period + ps_bit_allocation->i4_frame_level_bit_error) <
999            (i4_default_bits_in_period * 0.30))
1000         {
1001             ps_bit_allocation->i4_frame_level_bit_error = 0;  //-(i4_default_bits_in_period * 0.70);
1002         }
1003         i4_bits_in_period = i4_default_bits_in_period + ps_bit_allocation->i4_frame_level_bit_error;
1004         if(i4_non_I_scd == 0)
1005         {
1006             /*For the first gop unnecessarily the QP is going high in order to prevent this bits corresponding to full gop instead of gop-subgop*/
1007 
1008             WORD32 i4_intra_int = pic_type_get_intra_frame_interval(ps_pic_handling);
1009             WORD32 i4_inter_int = pic_type_get_inter_frame_interval(ps_pic_handling);
1010             if((tot_frms_in_period ==
1011                 (i4_intra_int - i4_inter_int + (1 << ps_bit_allocation->i4_field_pic))) &&
1012                (i4_intra_int != 1))
1013             {
1014                 i4_bits_in_period =
1015                     (WORD32)(i4_bits_in_period * ((float)i4_intra_int / tot_frms_in_period));
1016             }
1017         }
1018         trace_printf("\nBits in period %d", i4_bits_in_period);
1019     }
1020     else
1021     {
1022         i4_bits_in_period = ret_rbip_default_preenc(&ps_bit_allocation->s_rbip, ps_pic_handling);
1023 
1024         if(ps_bit_allocation->i4_ba_rc_pass == 2)
1025             i4_default_bits_in_period = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
1026     }
1027 
1028     i4_peak_drain_rate = get_buf_max_drain_rate(ps_cbr_buf_handling);
1029     i4_num_buf_frms =
1030         (get_cbr_buffer_size(ps_cbr_buf_handling) + (ps_bit_allocation->i4_bits_per_frm >> 1)) /
1031         ps_bit_allocation->i4_bits_per_frm;
1032     /*In VBR encoder buffer will be drained faster, i4_num_buf_frms should correspond to maximum number of bits that can be drained
1033       In CBR both peak and average must be same*/
1034     i4_num_buf_frms = i4_num_buf_frms * i4_peak_drain_rate / ps_bit_allocation->i4_bits_per_frm;
1035 
1036     i4_field_pic = pic_type_get_field_pic(ps_pic_handling);
1037 
1038     i4_subgop_size = pic_type_get_inter_frame_interval(ps_pic_handling);
1039     if(pvq_complexity_estimate == NULL)
1040         i4_cur_buf_pos = 0;
1041 
1042     i4_lap_window = ps_bit_allocation->i4_lap_window;
1043 
1044     /*assume minimum lap visibilty.A static I frame is given only the bits of duration for which we have visibility*/
1045     if(ps_bit_allocation->i4_lap_window < MINIMUM_VISIBILITY_B4_STATIC_I)
1046     {
1047         i4_lap_window = MINIMUM_VISIBILITY_B4_STATIC_I;
1048     }
1049     else
1050     {
1051         i4_lap_window = ps_bit_allocation->i4_lap_window;
1052         /*clip buffer window to max of lap window or buffer window*/
1053         if((i4_lap_window < i4_num_buf_frms) && (i4_call_type == 1))
1054             i4_num_buf_frms = i4_lap_window + i4_subgop_size;
1055     }
1056 
1057     if(i4_lap_window < MINIMUM_FRM_I_TO_REST_LAP_ENABLED)
1058         i4_lap_window = MINIMUM_FRM_I_TO_REST_LAP_ENABLED;
1059     if(ps_bit_allocation->i4_ba_rc_pass != 2)
1060     {
1061         if(i4_lap_window < i4_num_buf_frms)
1062             i4_num_buf_frms = i4_lap_window;
1063     }
1064 
1065     if(i4_num_buf_frms > tot_frms_in_period)
1066     {
1067         i4_num_buf_frms = tot_frms_in_period;
1068         i4_bit_alloc_window = i4_num_buf_frms;
1069     }
1070     /*get picture type dist based on bit alloc window*/
1071     if(i4_num_buf_frms < tot_frms_in_period)
1072     {
1073         for(i = 1; i < ps_bit_allocation->i4_num_active_pic_type; i++)
1074         {
1075             ai4_frms_in_baw[i] =
1076                 (ai4_frms_in_period[i] * i4_num_buf_frms + (tot_frms_in_period >> 1)) /
1077                 tot_frms_in_period;
1078             i4_num_scaled_frms += ai4_frms_in_baw[i];
1079             if(ps_bit_allocation->i4_field_pic)
1080             {
1081                 ai4_frms_in_baw[i + FIELD_OFFSET] = ai4_frms_in_baw[i];
1082                 i4_num_scaled_frms += ai4_frms_in_baw[i];
1083             }
1084         }
1085         if(ps_bit_allocation->i4_field_pic)
1086         {
1087             ai4_frms_in_baw[5]++;
1088             i4_num_scaled_frms++;
1089         }
1090         //if prorating is not exact account for diff with highest layer pic types
1091         if(!ps_bit_allocation->i4_field_pic)
1092         {
1093             ai4_frms_in_baw[ps_bit_allocation->i4_num_active_pic_type - 1] +=
1094                 (i4_num_buf_frms - i4_num_scaled_frms);
1095         }
1096         else
1097         {
1098             ai4_frms_in_baw[ps_bit_allocation->i4_num_active_pic_type - 1] +=
1099                 ((i4_num_buf_frms - i4_num_scaled_frms) >> 1);
1100             ai4_frms_in_baw[ps_bit_allocation->i4_num_active_pic_type - 1 + FIELD_OFFSET] +=
1101                 ((i4_num_buf_frms - i4_num_scaled_frms) >> 1);
1102         }
1103         i4_bits_in_period =
1104             ((LWORD64)i4_bits_in_period * i4_num_buf_frms + (tot_frms_in_period >> 1)) /
1105             tot_frms_in_period;
1106         i4_bit_alloc_window = i4_num_buf_frms;
1107     }
1108 
1109     i4_safe_margin = (WORD32)(i4_buffer_size * 0.1);
1110     i4_max_buffer_based = ((LWORD64)i4_buffer_size - i4_cur_buf_pos) /
1111                           ps_bit_allocation->i4_bits_per_frm * i4_peak_drain_rate;
1112     i4_max_buffer_based_I_pic = i4_buffer_size - i4_cur_buf_pos;
1113 
1114     for(i = 0; i < MAX_PIC_TYPE; i++)
1115     {
1116         i4_frames_in_buf += ai4_frms_in_baw[i];
1117     }
1118 
1119     if((rc_type == VBR_STREAMING) && (i4_call_type == 1))
1120     {
1121         WORD32 i4_delay_frames = cbr_get_delay_frames(ps_cbr_buf_handling);
1122         i4_max_buffer_based =
1123             (i4_peak_drain_rate *
1124                  (ps_bit_allocation->s_rbip.i4_tot_frms_in_gop + (WORD32)(i4_delay_frames * 0.8f)) -
1125              i4_cur_buf_pos);
1126 
1127         /*RBIP is updated once it is restricted for an Intra period */
1128         if(i4_default_bits_in_period > i4_max_buffer_based)
1129             update_rbip(
1130                 &ps_bit_allocation->s_rbip,
1131                 ps_pic_handling,
1132                 i4_max_buffer_based - i4_default_bits_in_period);
1133 
1134         i4_max_buffer_based =
1135             (i4_peak_drain_rate * (i4_frames_in_buf + (WORD32)(i4_delay_frames * 0.8f)) -
1136              i4_cur_buf_pos);
1137     }
1138     else
1139     {
1140         i4_max_buffer_based =
1141             ((((LWORD64)i4_buffer_size - i4_cur_buf_pos) / ps_bit_allocation->i4_bits_per_frm) +
1142              i4_frames_in_buf) *
1143             i4_peak_drain_rate;
1144     }
1145 
1146     /*the estimated bits for total period is clipped to buffer limits*/
1147     if(i4_bits_in_period > i4_max_buffer_based)
1148         i4_bits_in_period = i4_max_buffer_based;
1149 
1150     /*get I pic bits with altered bits in period*/
1151     if((!i4_is_scd) &&
1152        (ps_bit_allocation->i4_num_frames_since_last_I_frame <
1153         (ps_bit_allocation->i4_frame_rate * 2) / 1000) &&
1154        (ps_bit_allocation->i4_ba_rc_pass != 2))
1155     {
1156         /*returns texture bits*/
1157         LWORD64 i8_header_bits_in_previous_period = 0, i8_total_bits_in_previous_period = 0,
1158                 i4_frames_in_header = 0;
1159         WORD32 i4_texture_bits = 0;
1160         float f_percent_header_bits = 0.0f;
1161         /* Remove the header bits from the remaining bits to find how many bits you
1162         can transfer.*/
1163         for(i = 0; i < MAX_PIC_TYPE; i++)
1164         {
1165             i8_header_bits_in_previous_period +=
1166                 (ps_bit_allocation->i4_prev_frm_header_bits[i] * ai4_frms_in_baw[i]);
1167             i8_total_bits_in_previous_period +=
1168                 (ps_bit_allocation->ai4_prev_frm_tot_bits[i] * ai4_frms_in_baw[i]);
1169             i4_frames_in_header += ai4_frms_in_baw[i];
1170         }
1171 
1172         if((i4_call_type == 1) && (ps_bit_allocation->i4_ba_rc_pass == 2))
1173         {
1174             i4_texture_bits = (WORD32)(i4_bits_in_period * (1 - f_percent_head_bits));
1175         }
1176         else
1177         {
1178             f_percent_header_bits =
1179                 (float)i8_header_bits_in_previous_period / i8_total_bits_in_previous_period;
1180             i4_texture_bits =
1181                 i4_bits_in_period - (WORD32)(f_percent_header_bits * i4_bits_in_period);
1182         }
1183 
1184         if(i4_call_type == 1)
1185         {
1186             trace_printf(
1187                 "\nHeader Bits in period %d, total_frames %d "
1188                 "i4_max_buffer_based %d ",
1189                 (WORD32)f_percent_header_bits * i4_bits_in_period,
1190                 i4_frames_in_header,
1191                 i4_max_buffer_based);
1192         }
1193         i4_est_bits_for_I = get_bits_based_on_complexity(
1194             ps_bit_allocation,
1195             i4_texture_bits,
1196             ai4_frms_in_baw,
1197             pvq_complexity_estimate,
1198             e_pic_type,
1199             i4_call_type);
1200         /*twice the bitrate */
1201         if(i4_est_bits_for_I > ((ps_bit_allocation->i4_bit_rate << 1) -
1202                                 ps_bit_allocation->i4_prev_frm_header_bits[I_PIC]))
1203             i4_est_bits_for_I =
1204                 ((ps_bit_allocation->i4_bit_rate << 1) -
1205                  ps_bit_allocation->i4_prev_frm_header_bits[I_PIC]);
1206 
1207         if(i4_est_bits_for_I >
1208            (i4_max_buffer_based_I_pic - ps_bit_allocation->i4_prev_frm_header_bits[I_PIC]))
1209         {
1210             i4_est_bits_for_I =
1211                 (i4_max_buffer_based_I_pic - ps_bit_allocation->i4_prev_frm_header_bits[I_PIC]);
1212         }
1213     }
1214     else
1215     {
1216         /*returns total bits incase of scene cut*/
1217         ASSERT(ai4_frms_in_baw[I_PIC] != 0);
1218         if((i4_non_I_scd == 1) && (i4_call_type == 1) &&
1219            (ps_bit_allocation->f_curr_i_to_sum != 1.0f))
1220             ai4_frms_in_baw[I_PIC]++;
1221 
1222         i4_est_bits_for_I = (WORD32)(
1223             (i4_bits_in_period * I_to_avg_rest * ai4_frms_in_baw[I_PIC]) /
1224             (ai4_frms_in_baw[I_PIC] * I_to_avg_rest +
1225              (i4_bit_alloc_window - ai4_frms_in_baw[I_PIC])));
1226 
1227         if(i4_call_type == 1)
1228             i4_est_bits_for_I =
1229                 (WORD32)((float)i4_est_bits_for_I * ps_bit_allocation->f_curr_i_to_sum);
1230         else
1231         {
1232             if(ai4_frms_in_baw[I_PIC] > 0)
1233                 i4_est_bits_for_I = (WORD32)((float)i4_est_bits_for_I / ai4_frms_in_baw[I_PIC]);
1234         }
1235 
1236         if(i4_call_type == 1)
1237         {
1238             trace_printf(
1239                 "bits in period %d I_to_avg_rest %f f_curr_i_to_sum %f i "
1240                 "frames %d i4_non_I_scd %d ",
1241                 i4_bits_in_period,
1242                 I_to_avg_rest,
1243                 ps_bit_allocation->f_curr_i_to_sum,
1244                 ai4_frms_in_baw[I_PIC],
1245                 i4_non_I_scd);
1246         }
1247 
1248         if(i4_est_bits_for_I > (ps_bit_allocation->i4_bit_rate << 1))
1249             i4_est_bits_for_I = (ps_bit_allocation->i4_bit_rate << 1);
1250         if(i4_est_bits_for_I > i4_max_buffer_based_I_pic)
1251             i4_est_bits_for_I = i4_max_buffer_based_I_pic;
1252     }
1253 
1254     return i4_est_bits_for_I;
1255 }
1256 
1257 /*****************************************************************************
1258   Function Name : get_cur_frm_est_texture_bits
1259   Description   : Based on remaining bits in period and rd_model
1260                   the number of bits required for the current frame is estimated.
1261   Inputs        : ps_bit_allocation - bit_allocation structure
1262                   ps_rd_model - rd model pointer (for all the frame types)
1263                   e_pic_type - picture type
1264   Globals       :
1265   Processing    :
1266   Outputs       :
1267   Returns       :
1268   Issues        :
1269   Revision History:
1270          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1271 *****************************************************************************/
get_cur_frm_est_texture_bits(bit_allocation_t * ps_bit_allocation,rc_rd_model_handle * pps_rd_model,est_sad_handle ps_est_sad,pic_handling_handle ps_pic_handling,cbr_buffer_handle ps_cbr_buffer,picture_type_e e_pic_type,WORD32 i4_use_model,WORD32 i4_is_scd_frame,WORD32 i4_call_type,float i_to_avg_ratio,WORD32 i4_is_model_valid)1272 WORD32 get_cur_frm_est_texture_bits(
1273     bit_allocation_t *ps_bit_allocation,
1274     rc_rd_model_handle *pps_rd_model,
1275     est_sad_handle ps_est_sad,
1276     pic_handling_handle ps_pic_handling,
1277     cbr_buffer_handle ps_cbr_buffer,
1278     picture_type_e e_pic_type,
1279     WORD32 i4_use_model,
1280     WORD32 i4_is_scd_frame,
1281     WORD32 i4_call_type,
1282     float i_to_avg_ratio,
1283     WORD32 i4_is_model_valid)
1284 {
1285     WORD32 i, j;
1286     WORD32 i4_est_texture_bits_for_frm;
1287     WORD32 i4_rem_texture_bits;
1288     number_t avq_complexity_estimate[MAX_PIC_TYPE];
1289     WORD32 ai4_frms_in_period[MAX_PIC_TYPE];
1290     WORD32 i4_max_consumable_bits, i4_est_tot_head_bits_period = 0, i4_total_bits_prev_gop = 0;
1291     WORD32 i4_field_pic, i4_inter_frame_int;
1292     WORD32 complexity_est = 0;
1293     float f_percent_head_bits = 0.0f;
1294     WORD32 i4_intra_frm_int;
1295     i4_intra_frm_int = pic_type_get_actual_intra_frame_interval(ps_pic_handling);
1296     i4_inter_frame_int = pic_type_get_inter_frame_interval(ps_pic_handling);
1297     i4_field_pic = pic_type_get_field_pic(ps_pic_handling);
1298 
1299     /* If the complexity estimate is not filled based on
1300     1) Since not using model
1301     2) Using the module but one of the estimate values are zero
1302     Then set the complexity estimate based on default values */
1303     //  if(!complexity_est)
1304     {
1305         /* Hardcoding the bit ratios between I, P and B same as during init time*/
1306         SET_VAR_Q(
1307             avq_complexity_estimate[I_PIC],
1308             (I_TO_P_BIT_RATIO * P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
1309             0);
1310         SET_VAR_Q(
1311             avq_complexity_estimate[P_PIC],
1312             (P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
1313             0);
1314         SET_VAR_Q(
1315             avq_complexity_estimate[P1_PIC],
1316             (P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
1317             0);
1318         SET_VAR_Q(avq_complexity_estimate[B_PIC], (B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO), 0);
1319         SET_VAR_Q(avq_complexity_estimate[BB_PIC], (B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO), 0);
1320         SET_VAR_Q(
1321             avq_complexity_estimate[B1_PIC],
1322             (B1_TO_B2_BIT_RATIO),
1323             0);  //temporarliy treat ref and non ref as same
1324         SET_VAR_Q(avq_complexity_estimate[B11_PIC], (B1_TO_B2_BIT_RATIO), 0);
1325         SET_VAR_Q(avq_complexity_estimate[B2_PIC], 1, 0);
1326         SET_VAR_Q(avq_complexity_estimate[B22_PIC], 1, 0);
1327     }
1328     /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
1329     /* pic_type_get_rem_frms_in_gop(ps_pic_handling, ai4_rem_frms_in_period); */
1330     pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_period);
1331 
1332     /* Depending on the number of gops in a period, find the num_frms_in_prd */
1333     for(j = 0; j < MAX_PIC_TYPE; j++)
1334     {
1335         /* ai4_rem_frms_in_period[j] += (ai4_frms_in_period[j] * (ps_bit_allocation->i4_num_gops_in_period - 1)); */
1336         ai4_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period;
1337     }
1338 
1339     /* If a frame is detected as SCD and bit allocation is asked for the remaining part of the frame
1340     we allocate bits assuming that frame as a I frame. So reduce 1 frame from the picture type coming in
1341     and add that to I frame */
1342     if(i4_is_scd_frame && e_pic_type != I_PIC)
1343     {
1344         /* ai4_rem_frms_in_period[0]++;ai4_rem_frms_in_period[e_pic_type]--; */
1345         ai4_frms_in_period[0]++;
1346         ai4_frms_in_period[e_pic_type]--;
1347     }
1348     /*HEVC_hierarchy: calculate header bits for all frames in period*/
1349     for(j = 0; j < MAX_PIC_TYPE; j++)
1350     {
1351         i4_est_tot_head_bits_period +=
1352             ai4_frms_in_period[j] * ps_bit_allocation->i4_prev_frm_header_bits[j];
1353         i4_total_bits_prev_gop +=
1354             ai4_frms_in_period[j] * ps_bit_allocation->ai4_prev_frm_tot_bits[j];
1355     }
1356 
1357     {
1358         WORD32 ai4_actual_frms_in_gop[MAX_PIC_TYPE], i, i4_total_frames = 0;
1359         pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_actual_frms_in_gop);
1360         for(i = 0; i < MAX_PIC_TYPE; i++)
1361         {
1362             i4_total_frames += ai4_actual_frms_in_gop[i];
1363         }
1364         i4_max_consumable_bits = ps_bit_allocation->i4_max_bits_per_frm[0] * i4_total_frames;
1365 
1366         /* Remove the header bits from the remaining bits to find how many bits you
1367            can transfer.*/
1368         if(i4_call_type == 1)
1369         {
1370             if(ps_bit_allocation->i4_ba_rc_pass == 2)
1371             {
1372                 WORD32 i4_tot_frm_remain = 0, i4_tot_head_bits_in_gop = 0,
1373                        i4_tot_bits_last_in_gop = 0, i4_use_default_flag = 0;
1374 
1375                 WORD32 i4_rbip = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
1376                 if((i4_rbip + ps_bit_allocation->i4_frame_level_bit_error) < (i4_rbip * 0.30))
1377                 {
1378                     ps_bit_allocation->i4_frame_level_bit_error = 0;  //-(i4_rbip * 0.70);
1379                 }
1380                 i4_rem_texture_bits =
1381                     i4_rbip +
1382                     ps_bit_allocation->i4_frame_level_bit_error /*- i4_est_tot_head_bits_period*/
1383                     ;
1384 
1385                 i4_est_tot_head_bits_period = 0;
1386                 for(j = 0; j < MAX_PIC_TYPE; j++)
1387                 {
1388                     if((WORD32)ps_bit_allocation->af_sum_weigh[j][1] > 0)
1389                     {
1390                         i4_tot_frm_remain += (WORD32)ps_bit_allocation->af_sum_weigh[j][1];
1391                         i4_tot_head_bits_in_gop += (WORD32)(
1392                             ps_bit_allocation->i4_prev_frm_header_bits[j] *
1393                             ps_bit_allocation->af_sum_weigh[j][1]);
1394                         i4_tot_bits_last_in_gop += (WORD32)(
1395                             ps_bit_allocation->ai4_prev_frm_tot_bits[j] *
1396                             ps_bit_allocation->af_sum_weigh[j][1]);
1397                         if(ps_bit_allocation->ai4_prev_frm_tot_bits[j] == -1)
1398                         {
1399                             i4_use_default_flag = 1;
1400                         }
1401                     }
1402                 }
1403 
1404                 if(i4_use_default_flag != 1)
1405                 {
1406                     f_percent_head_bits = (float)i4_tot_head_bits_in_gop / i4_tot_bits_last_in_gop;
1407 
1408                     if(f_percent_head_bits > 0.7f)
1409                         f_percent_head_bits = 0.7f;
1410 
1411                     /*Subtracting a percentage of header bits from the remaining bits in period*/
1412                     i4_rem_texture_bits = (WORD32)(i4_rem_texture_bits * (1 - f_percent_head_bits));
1413                 }
1414                 else
1415                 {
1416                     /*Assuming 30% of bits will go for header bits in a gop in preenc*/
1417                     i4_rem_texture_bits -= (WORD32)((float)i4_rem_texture_bits * 0.3f);
1418                 }
1419 
1420                 trace_printf(
1421                     "Remaining texture bits %d fbe %d fphb %f thbg %d tblg %d",
1422                     i4_rem_texture_bits,
1423                     ps_bit_allocation->i4_frame_level_bit_error,
1424                     f_percent_head_bits,
1425                     i4_tot_head_bits_in_gop,
1426                     i4_tot_bits_last_in_gop);
1427             }
1428             else
1429             {
1430                 /* Remove the header bits from the remaining bits to find how many bits you
1431             can transfer.*/
1432                 WORD32 i4_rbip = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
1433                 if((i4_rbip + ps_bit_allocation->i4_frame_level_bit_error) < (i4_rbip * 0.30))
1434                 {
1435                     ps_bit_allocation->i4_frame_level_bit_error = 0;  //-(i4_rbip * 0.70);
1436                 }
1437                 i4_rem_texture_bits = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0) +
1438                                       ps_bit_allocation->i4_frame_level_bit_error;
1439 
1440                 i4_est_tot_head_bits_period = (WORD32)(
1441                     ((float)(i4_est_tot_head_bits_period) / (float)i4_total_bits_prev_gop) *
1442                     i4_rem_texture_bits);
1443 
1444                 if(i4_is_model_valid)
1445                 {
1446                     i4_rem_texture_bits = i4_rem_texture_bits - i4_est_tot_head_bits_period;
1447                 }
1448                 else
1449                 {
1450                     /*inorder to estimate the buffer position for model invalid cases, to control
1451                     encoder buffer overflow*/
1452                     i4_rem_texture_bits = ((i4_rem_texture_bits * 3) >> 1);
1453                 }
1454 
1455                 trace_printf(
1456                     "Remaining texture bits %d fbe %d ethp %d",
1457                     i4_rem_texture_bits,
1458                     ps_bit_allocation->i4_frame_level_bit_error,
1459                     i4_est_tot_head_bits_period);
1460             }
1461 
1462             {
1463                 WORD32 i4_drain_bits_per_frame = get_buf_max_drain_rate(ps_cbr_buffer), i4_ebf;
1464                 WORD32 i4_delay = cbr_get_delay_frames(ps_cbr_buffer), max_buffer_level = 0,
1465                        rc_type = get_rc_type(ps_cbr_buffer);
1466 
1467                 if(rc_type == VBR_STREAMING)
1468                     max_buffer_level = i4_drain_bits_per_frame * i4_delay;
1469                 else
1470                     max_buffer_level = get_cbr_buffer_size(ps_cbr_buffer);
1471 
1472                 i4_ebf = get_cbr_ebf(ps_cbr_buffer);
1473 
1474                 if(i4_ebf > (WORD32)(0.8f * max_buffer_level))
1475                 {
1476                     if(ps_bit_allocation->af_sum_weigh[e_pic_type][0] > 1.0f)
1477                         ps_bit_allocation->af_sum_weigh[e_pic_type][0] = 1.0f;
1478                 }
1479                 if(i4_ebf > (WORD32)(0.6f * max_buffer_level))
1480                 {
1481                     if(ps_bit_allocation->af_sum_weigh[e_pic_type][0] > 1.5f)
1482                         ps_bit_allocation->af_sum_weigh[e_pic_type][0] = 1.5f;
1483                 }
1484             }
1485         }
1486         else
1487         {
1488             i4_rem_texture_bits =
1489                 ret_rbip_default_preenc(&ps_bit_allocation->s_rbip, ps_pic_handling);
1490             /*Assuming 30% of bits will go for header bits in a gop in preenc*/
1491             i4_rem_texture_bits -= (WORD32)(i4_rem_texture_bits * 0.3f);
1492         }
1493     }
1494 
1495     if(i4_use_model)
1496     {
1497         /* The bits are then allocated based on the relative complexity of the
1498         current frame with respect to that of the rest of the frames in period */
1499         for(i = 0; i < MAX_PIC_TYPE; i++)
1500         {
1501             number_t vq_lin_mod_coeff, vq_est_sad, vq_K;
1502 
1503             if(ai4_frms_in_period[i] > 0) /*Added for field case */
1504             {
1505                 /* Getting the linear model coefficient */
1506                 vq_lin_mod_coeff = get_linear_coefficient(pps_rd_model[i]);
1507                 /* Getting the estimated SAD */
1508                 SET_VAR_Q(vq_est_sad, get_est_sad(ps_est_sad, (picture_type_e)i), 0);
1509                 /* Making K factor a var Q format */
1510                 SET_VAR_Q(vq_K, ps_bit_allocation->i2_K[i], K_Q);
1511                 /* Complexity_estimate = [ (lin_mod_coeff * estimated_sad) / K factor ]  */
1512                 mult32_var_q(vq_lin_mod_coeff, vq_est_sad, &vq_lin_mod_coeff);
1513                 div32_var_q(vq_lin_mod_coeff, vq_K, &avq_complexity_estimate[i]);
1514             }
1515         }
1516         /*flag to check if complexity estimate is available*/
1517         complexity_est = 1;
1518 
1519         /*HEVC_hierarchy: If complexity estimate = 0 for any pic type then use default ratios*/
1520         for(i = 0; i < MAX_PIC_TYPE; i++)
1521         {
1522             if(ai4_frms_in_period[i] > 0)
1523             {
1524                 complexity_est = complexity_est && avq_complexity_estimate[i].sm;
1525             }
1526         }
1527     }
1528 
1529     /* Make the picture type of the SCD frame a I_PIC */
1530     if(i4_is_scd_frame && e_pic_type != I_PIC)
1531         e_pic_type = I_PIC;
1532 
1533     if(e_pic_type == I_PIC)
1534     {
1535         /*clip min max values*/
1536         if(i_to_avg_ratio > I_TO_AVG_REST_GOP_BIT_MAX)
1537             i_to_avg_ratio = I_TO_AVG_REST_GOP_BIT_MAX;
1538 
1539         if(i_to_avg_ratio < I_TO_AVG_REST_GOP_BIT_MIN)
1540             i_to_avg_ratio = I_TO_AVG_REST_GOP_BIT_MIN;
1541 
1542         i4_est_texture_bits_for_frm = bit_alloc_get_intra_bits(
1543             ps_bit_allocation,
1544             ps_pic_handling,
1545             ps_cbr_buffer,
1546             e_pic_type,
1547             avq_complexity_estimate,
1548             0,
1549             i_to_avg_ratio,
1550             i4_call_type,
1551             0,
1552             f_percent_head_bits);
1553     }
1554     else
1555     {
1556         /* Get the texture bits required for the current frame */
1557         i4_est_texture_bits_for_frm = get_bits_based_on_complexity(
1558             ps_bit_allocation,
1559             i4_rem_texture_bits,
1560             ai4_frms_in_period,
1561             avq_complexity_estimate,
1562             e_pic_type,
1563             i4_call_type);
1564     }
1565 
1566     ps_bit_allocation->i4_excess_bits_from_buffer = 0;
1567 
1568     /* If the remaining bits in the period becomes negative then the estimated texture
1569     bits would also become negative. This would send a feedback to the model which
1570     may go for a toss. Thus sending the minimum possible value = 0 */
1571     if(i4_est_texture_bits_for_frm < 0)
1572         i4_est_texture_bits_for_frm = 0;
1573 
1574     return (i4_est_texture_bits_for_frm);
1575 }
1576 
1577 /*****************************************************************************
1578   Function Name : get_cur_frm_est_header_bits
1579   Description   : Based on remaining bits in period and rd_model
1580                   the number of bits required for the current frame is estimated.
1581   Inputs        : ps_bit_allocation - bit_allocation structure
1582                   ps_rd_model - rd model pointer (for all the frame types)
1583                   e_pic_type - picture type
1584   Globals       :
1585   Processing    :
1586   Outputs       :
1587   Returns       :
1588   Issues        :
1589   Revision History:
1590          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1591 *****************************************************************************/
get_cur_frm_est_header_bits(bit_allocation_t * ps_bit_allocation,picture_type_e e_pic_type)1592 WORD32 get_cur_frm_est_header_bits(bit_allocation_t *ps_bit_allocation, picture_type_e e_pic_type)
1593 {
1594     //ASSERT(ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type] == 0);
1595     return (ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type]);
1596 }
1597 /*****************************************************************************
1598   Function Name : get_rem_bits_in_period
1599   Description   : Get remaining bits in period
1600   Inputs        : ps_bit_allocation - bit_allocation structure
1601                   ps_pic_handling
1602   Revision History:
1603          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1604 *****************************************************************************/
1605 WORD32
get_rem_bits_in_period(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling)1606     get_rem_bits_in_period(bit_allocation_t *ps_bit_allocation, pic_handling_handle ps_pic_handling)
1607 {
1608     return (update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0));
1609 }
1610 /*****************************************************************************
1611   Function Name : get_bits_per_frame
1612   Description   : Get Bits per frame
1613   Inputs        : ps_bit_allocation - bit_allocation structure
1614   Revision History:
1615          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1616 *****************************************************************************/
get_bits_per_frame(bit_allocation_t * ps_bit_allocation)1617 WORD32 get_bits_per_frame(bit_allocation_t *ps_bit_allocation)
1618 {
1619     return ((*ps_bit_allocation).i4_bits_per_frm);
1620 }
1621 /*****************************************************************************
1622   Function Name : ba_get_gop_bits
1623   Description   :
1624   Inputs        : ps_bit_allocation - bit_allocation structure
1625   Revision History:
1626          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1627 *****************************************************************************/
ba_get_gop_bits(bit_allocation_t * ps_bit_allocation)1628 LWORD64 ba_get_gop_bits(bit_allocation_t *ps_bit_allocation)
1629 {
1630     gop_level_stat_t *ps_cur_gop_stat;
1631     ps_cur_gop_stat =
1632         (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
1633     return (
1634         ps_cur_gop_stat->i8_bits_allocated_to_gop +
1635         ps_cur_gop_stat->i8_buffer_play_bits_allocated_to_gop);
1636 }
1637 /*****************************************************************************
1638   Function Name : ba_get_gop_sad
1639   Description   :
1640   Inputs        : ps_bit_allocation - bit_allocation structure
1641   Revision History:
1642          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1643 *****************************************************************************/
ba_get_gop_sad(bit_allocation_t * ps_bit_allocation)1644 LWORD64 ba_get_gop_sad(bit_allocation_t *ps_bit_allocation)
1645 {
1646     gop_level_stat_t *ps_cur_gop_stat;
1647     ps_cur_gop_stat =
1648         (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
1649     return (ps_cur_gop_stat->i8_acc_gop_sad);
1650 }
1651 /*****************************************************************************
1652   Function Name : ba_get_buffer_play_bits_for_cur_gop
1653   Description   :
1654   Inputs        : ps_bit_allocation - bit_allocation structure
1655   Revision History:
1656          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1657 *****************************************************************************/
ba_get_buffer_play_bits_for_cur_gop(bit_allocation_t * ps_bit_allocation)1658 LWORD64 ba_get_buffer_play_bits_for_cur_gop(bit_allocation_t *ps_bit_allocation)
1659 {
1660     gop_level_stat_t *ps_cur_gop_stat;
1661     ps_cur_gop_stat =
1662         (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
1663     return (ps_cur_gop_stat->i8_buffer_play_bits_allocated_to_gop);
1664 }
1665 
1666 /*****************************************************************************
1667   Function Name : update_cur_frm_consumed_bits
1668   Description   : Based on remaining bits in period and rd_model
1669                   the number of bits required for the current frame is estimated.
1670   Inputs        : ps_bit_allocation - bit_allocation structure
1671                   ps_rd_model - rd model pointer (for all the frame types)
1672                   e_pic_type - picture type
1673   Globals       :
1674   Processing    :
1675   Outputs       :
1676   Returns       :
1677   Issues        :
1678   Revision History:
1679          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1680 *****************************************************************************/
update_cur_frm_consumed_bits(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling,cbr_buffer_handle ps_cbr_buf_handle,WORD32 i4_total_frame_bits,WORD32 i4_model_updation_hdr_bits,picture_type_e e_pic_type,UWORD8 u1_is_scd,WORD32 i4_last_frm_in_period,WORD32 i4_lap_comp_bits_reset,WORD32 i4_suppress_bpic_update,WORD32 i4_buffer_based_bit_error,WORD32 i4_stuff_bits,WORD32 i4_lap_window_comp,rc_type_e e_rc_type,WORD32 i4_num_gop,WORD32 i4_is_pause_to_resume,WORD32 i4_est_text_bits_ctr_update_qp,WORD32 * pi4_gop_correction,WORD32 * pi4_new_correction)1681 void update_cur_frm_consumed_bits(
1682     bit_allocation_t *ps_bit_allocation,
1683     pic_handling_handle ps_pic_handling,
1684     cbr_buffer_handle ps_cbr_buf_handle,
1685     WORD32 i4_total_frame_bits,
1686     WORD32 i4_model_updation_hdr_bits,
1687     picture_type_e e_pic_type,
1688     UWORD8 u1_is_scd,
1689     WORD32 i4_last_frm_in_period,
1690     WORD32 i4_lap_comp_bits_reset,
1691     WORD32 i4_suppress_bpic_update,
1692     WORD32 i4_buffer_based_bit_error,
1693     WORD32 i4_stuff_bits,
1694     WORD32 i4_lap_window_comp,
1695     rc_type_e e_rc_type,
1696     WORD32 i4_num_gop,
1697     WORD32 i4_is_pause_to_resume,
1698     WORD32 i4_est_text_bits_ctr_update_qp,
1699     WORD32 *pi4_gop_correction,
1700     WORD32 *pi4_new_correction)
1701 {
1702     WORD32 i4_error_bits = get_error_bits(ps_bit_allocation->ps_error_bits);
1703     WORD32 i4_intra_frm_int, i, i4_flag_no_error_calc = 0; /*i_only*/
1704     WORD32 i4_do_correction = 0;
1705     i4_intra_frm_int = pic_type_get_intra_frame_interval(ps_pic_handling);
1706     ps_bit_allocation->i4_rem_frame_in_period--;
1707 
1708     /*No frame level bit error for top layer pictures*/
1709 
1710     i4_flag_no_error_calc = /*((e_pic_type != B1_PIC && e_pic_type != B11_PIC)  && ps_bit_allocation->i4_num_active_pic_type == 4)||
1711                             ((e_pic_type != B2_PIC && e_pic_type != B22_PIC)  && ps_bit_allocation->i4_num_active_pic_type == 5) &&*/
1712         (i4_is_pause_to_resume == 0);
1713 
1714     /* Update the remaining bits in period */
1715     ps_bit_allocation->i4_bits_from_buffer_in_cur_gop +=
1716         ps_bit_allocation->i4_excess_bits_from_buffer;
1717     ps_bit_allocation->i4_buffer_based_bit_error -= ps_bit_allocation->i4_excess_bits_from_buffer;
1718     ps_bit_allocation->i4_gop_level_bit_error +=
1719         (-(i4_total_frame_bits + i4_stuff_bits) + i4_error_bits +
1720          ps_bit_allocation->i4_bits_per_frm);
1721     ps_bit_allocation->i8_cur_gop_bit_consumption += (i4_total_frame_bits + i4_stuff_bits);
1722 
1723     //if(ps_bit_allocation-> == 2)ASSERT(i4_stuff_bits == 0);//No stuffing in two pass
1724     //ASSERT(ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type] == 0);
1725     ps_bit_allocation->i4_buffer_based_bit_error += i4_buffer_based_bit_error;
1726     ps_bit_allocation->i8_frm_num_in_gop++;
1727     if(i4_last_frm_in_period && i4_lap_comp_bits_reset)
1728         i4_lap_comp_bits_reset = 0;  //end of period is always I frame boundary.
1729 
1730     if(e_pic_type == I_PIC)
1731         ps_bit_allocation->i4_num_frames_since_last_I_frame = 1;
1732     else
1733         ps_bit_allocation->i4_num_frames_since_last_I_frame++;
1734 
1735     if((!i4_suppress_bpic_update))
1736     {
1737         //if(ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_update_qp] > 0)
1738         {
1739             ps_bit_allocation->ai4_prev_frm_tot_est_bits[e_pic_type] =
1740                 ps_bit_allocation->ai4_cur_frm_est_hdr_bits[i4_est_text_bits_ctr_update_qp] +
1741                 ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_update_qp];
1742 
1743             ps_bit_allocation->i4_frame_level_bit_error +=
1744                 (ps_bit_allocation->ai4_cur_frm_est_hdr_bits[i4_est_text_bits_ctr_update_qp] +
1745                  ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_update_qp] -
1746                  i4_total_frame_bits);
1747         }
1748 
1749         trace_printf(
1750             "Prev frame header %d Total est %d total frame %d",
1751             ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type],
1752             ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_update_qp],
1753             i4_total_frame_bits);
1754     }
1755 
1756     trace_printf(
1757         "  rbip = %d  frame lbe = %d    bbbe = %d  bfbicg = %d\n",
1758         update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0),
1759         ps_bit_allocation->i4_frame_level_bit_error,
1760         ps_bit_allocation->i4_buffer_based_bit_error,
1761         ps_bit_allocation->i4_bits_from_buffer_in_cur_gop);
1762 
1763     /* Update the header bits so that it can be used as an estimate to the next frame */
1764     if(u1_is_scd)
1765     {
1766         /* Initilising the header bits to be used for each picture type */
1767         init_prev_header_bits(ps_bit_allocation, ps_pic_handling);
1768 
1769         /*init tot bits consumed of previous frame*/
1770         for(i = 0; i < MAX_PIC_TYPE; i++)
1771         {
1772             ps_bit_allocation->ai4_prev_frm_tot_bits[i] = -1;
1773             ps_bit_allocation->ai4_prev_frm_tot_est_bits[i] = -1;
1774         }
1775         /* In case of SCD, eventhough the frame type is P, it is equivalent to a I frame
1776            and so the coresponding header bits is updated */
1777         //ASSERT(i4_model_updation_hdr_bits == 0);
1778         ps_bit_allocation->i4_prev_frm_header_bits[I_PIC] = i4_model_updation_hdr_bits;
1779         ps_bit_allocation->ai4_prev_frm_tot_bits[I_PIC] = i4_total_frame_bits;
1780         ps_bit_allocation->ai4_prev_frm_tot_est_bits[I_PIC] = i4_total_frame_bits;
1781         /*SCD allowed only for I_PIC*/
1782         ASSERT(e_pic_type == I_PIC);
1783 
1784 #define MAX_NUM_GOPS_IN_PERIOD (5)
1785         if(ps_bit_allocation->i4_num_gops_in_period != 1 &&
1786            ps_bit_allocation->i4_num_gops_in_period < MAX_NUM_GOPS_IN_PERIOD)
1787         {
1788             /* Whenever there is a scene change increase the number of gops by 2 so that
1789                the number of bits allocated is not very constrained. */
1790             ps_bit_allocation->i4_num_gops_in_period += 2;
1791             /* Add the extra bits in GOP to remaining bits in period */
1792             change_rbip(
1793                 &ps_bit_allocation->s_rbip,
1794                 ps_bit_allocation->i4_bits_per_frm,
1795                 ps_bit_allocation->i4_num_gops_in_period);
1796             /* printf((const WORD8*)"SCD rbp %d, ngp %d\n", update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling,0),
1797             ps_bit_allocation->i4_num_gops_in_period); */
1798         }
1799     }
1800     else
1801     {
1802         //ASSERT(i4_model_updation_hdr_bits == 0);
1803         if(!i4_suppress_bpic_update)
1804         {
1805             ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type] = i4_model_updation_hdr_bits;
1806             ps_bit_allocation->ai4_prev_frm_tot_bits[e_pic_type] = i4_total_frame_bits;
1807         }
1808     }
1809 
1810     {
1811         /* Removng the error due to buffer movement from gop level bit error */
1812         WORD32 i4_gop_correction = 0;
1813         WORD32 i4_cur_ebf = get_cbr_ebf(ps_cbr_buf_handle);
1814         WORD32 i4_vbv_size = get_cbr_buffer_size(ps_cbr_buf_handle);
1815         WORD32 i4_min_vbv_size = (WORD32)(i4_vbv_size * MIN_THRESHOLD_VBV_GOP_ERROR);
1816         WORD32 i4_max_vbv_size = (WORD32)(i4_vbv_size * MAX_THRESHOLD_VBV_GOP_ERROR);
1817         /*get desired buffer level so that bit error can be calculated. desired buf = 1 - lap window complexity*/
1818         if(ps_bit_allocation->i4_ba_rc_pass != 2)
1819         {
1820             WORD32 i4_inter_frame_interval = pic_type_get_inter_frame_interval(ps_pic_handling);
1821             LWORD64 vbv_buffer_based_excess = 0;
1822             WORD32 i4_lap_window_comp_temp = i4_lap_window_comp;
1823             if(ps_bit_allocation->i4_lap_window > i4_inter_frame_interval)
1824             {
1825                 if(e_rc_type == VBR_STREAMING)
1826                 {
1827                     if(((float)i4_lap_window_comp / 128) >
1828                        ps_bit_allocation->f_min_complexity_cross_peak_rate)
1829                         i4_lap_window_comp_temp =
1830                             (WORD32)(ps_bit_allocation->f_min_complexity_cross_peak_rate * 128);
1831 
1832                     /*Get excess bits if any from vbv buffer*/
1833                     vbv_buffer_based_excess = get_vbv_buffer_based_excess(
1834                         ps_cbr_buf_handle,
1835                         ps_bit_allocation->f_min_complexity_cross_peak_rate,
1836                         ((float)i4_lap_window_comp / 128),
1837                         (i4_intra_frm_int * ps_bit_allocation->s_rbip.i4_num_intra_frm_interval),
1838                         1);
1839                 }
1840 
1841                 i4_do_correction = 1;
1842                 i4_gop_correction = get_error_bits_for_desired_buf(
1843                     ps_cbr_buf_handle,
1844                     i4_lap_window_comp_temp,
1845                     (i4_intra_frm_int * ps_bit_allocation->s_rbip.i4_num_intra_frm_interval));
1846                 /*In case of VBR, don't do buffer based correction if gop_correction is less than 0, as it is less than average*/
1847                 if((e_rc_type == VBR_STREAMING) && (i4_gop_correction <= 0))
1848                 {
1849                     i4_do_correction = 0;
1850                 }
1851 
1852                 /* vbv buffer position based error correction to keep away encoder buffer overflow at GOP (I to I, not user configured)*/
1853                 if(i4_do_correction)
1854                 {
1855                     WORD32 i4_buffer_err_bits;
1856                     /*check if the ebf is greater than max ebf,
1857                     then account for complete error above max ebf in the current GOP itself*/
1858                     if(i4_cur_ebf > i4_max_vbv_size)
1859                     {
1860                         i4_gop_correction -= (i4_cur_ebf - i4_max_vbv_size);
1861                         *pi4_new_correction -= (i4_cur_ebf - i4_max_vbv_size);
1862                         i4_cur_ebf = i4_max_vbv_size;
1863                     }
1864                     /* if ebf is above min but less than max, then distribute to the next GOPs*/
1865                     if(i4_cur_ebf > i4_min_vbv_size)
1866                     {
1867                         WORD32 i4_num_gops;
1868                         float f_ebf_percent;
1869                         /*compute the error bits to be distributed over the next GOPs*/
1870                         i4_buffer_err_bits = (i4_cur_ebf - i4_min_vbv_size);
1871                         /*compute number fo GOPs the error to be distributed
1872                             high error -> few GOPs, less error -> more GOPs*/
1873                         f_ebf_percent = ((float)i4_cur_ebf / i4_vbv_size);
1874                         i4_num_gops = (WORD32)((1.0 - f_ebf_percent) * 10) + 2;
1875                         /*add the error bits to the period*/
1876                         i4_gop_correction -= (WORD32)(i4_buffer_err_bits / i4_num_gops);
1877                         *pi4_new_correction -= (WORD32)(i4_buffer_err_bits / i4_num_gops);
1878                     }
1879                 }
1880                 *pi4_gop_correction = i4_gop_correction;
1881                 set_rbip(
1882                     &ps_bit_allocation->s_rbip,
1883                     (i4_gop_correction + (WORD32)vbv_buffer_based_excess));
1884 
1885                 update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
1886                 ASSERT(ps_bit_allocation->i4_bits_from_buffer_in_cur_gop == 0);
1887                 trace_printf("\nRBIP updated ");
1888             }
1889             /* initialise the GOP and bit errors to zero */
1890             ps_bit_allocation->i4_gop_level_bit_error = 0;
1891             /*frame level error can't be carried over when it is more than VBV buffer size*/
1892             if(ps_bit_allocation->i4_frame_level_bit_error > i4_max_vbv_size)
1893             {
1894                 ps_bit_allocation->i4_frame_level_bit_error = i4_max_vbv_size;
1895             }
1896             if((i4_last_frm_in_period) ||
1897                (i4_intra_frm_int == 1 && ps_bit_allocation->i4_rem_frame_in_period == 0))
1898             { /*For 1st pass set the errors to 0 at end of a gop*/
1899                 ps_bit_allocation->i8_cur_gop_bit_consumption = 0;
1900                 ps_bit_allocation->i4_frame_level_bit_error = 0;
1901                 ps_bit_allocation->i4_bits_from_buffer_in_cur_gop = 0;
1902                 ps_bit_allocation->i4_rem_frame_in_period =
1903                     ps_bit_allocation->i4_num_gops_in_period *
1904                     i4_intra_frm_int; /*TBD: I only case*/
1905                 ps_bit_allocation->i8_frm_num_in_gop = 0;
1906             }
1907         }
1908     }
1909 
1910     if(i4_last_frm_in_period && i4_intra_frm_int != 1)
1911     {
1912         /* If the number of gops in period has been increased due to scene change, slowly bring in down
1913            across the gops */
1914         if(ps_bit_allocation->i4_num_gops_in_period >
1915            ps_bit_allocation->i4_actual_num_gops_in_period)
1916         {
1917             ps_bit_allocation->i4_num_gops_in_period--;
1918             change_rbip(
1919                 &ps_bit_allocation->s_rbip,
1920                 ps_bit_allocation->i4_bits_per_frm,
1921                 ps_bit_allocation->i4_num_gops_in_period);
1922         }
1923     }
1924     /*Check for complexity based bits reset in future with GOP*/
1925 
1926     /* Update the lower modules */
1927     update_error_bits(ps_bit_allocation->ps_error_bits);
1928 }
1929 
1930 /*****************************************************************************
1931   Function Name : change_remaining_bits_in_period
1932   Description   :
1933   Inputs        : ps_bit_allocation - bit_allocation structure
1934 
1935   Globals       :
1936   Processing    :
1937   Outputs       :
1938   Returns       :
1939   Issues        :
1940   Revision History:
1941          DD MM YYYY   Author(s)       Changes (Describe the changes made)
1942 *****************************************************************************/
change_remaining_bits_in_period(bit_allocation_t * ps_bit_allocation,WORD32 i4_bit_rate,WORD32 i4_frame_rate,WORD32 * i4_peak_bit_rate)1943 void change_remaining_bits_in_period(
1944     bit_allocation_t *ps_bit_allocation,
1945     WORD32 i4_bit_rate,
1946     WORD32 i4_frame_rate,
1947     WORD32 *i4_peak_bit_rate)
1948 {
1949     WORD32 i4_new_avg_bits_per_frm, i4_new_peak_bits_per_frm[MAX_NUM_DRAIN_RATES];
1950     int i;
1951 
1952     /* Calculate the new per frame bits */
1953     X_PROD_Y_DIV_Z(i4_bit_rate, 1000, i4_frame_rate, i4_new_avg_bits_per_frm);
1954 
1955     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
1956     {
1957         X_PROD_Y_DIV_Z(i4_peak_bit_rate[i], 1000, i4_frame_rate, i4_new_peak_bits_per_frm[i]);
1958     }
1959 
1960     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
1961     {
1962         ps_bit_allocation->i4_max_bits_per_frm[i] = i4_new_peak_bits_per_frm[i];
1963     }
1964 
1965     /* Get the rem_frms_in_prd & the frms_in_prd from the pic_type state struct */
1966     /* pic_type_get_rem_frms_in_gop(ps_pic_handling, i4_rem_frms_in_period); */
1967 
1968     /* If the difference > 0(/ <0), the remaining bits in period needs to be increased(/decreased)
1969     based on the remaining number of frames */
1970     change_rbip(
1971         &ps_bit_allocation->s_rbip,
1972         i4_new_avg_bits_per_frm,
1973         ps_bit_allocation->i4_num_gops_in_period);
1974 
1975     /* Update the new average bits per frame */
1976     ps_bit_allocation->i4_bits_per_frm = i4_new_avg_bits_per_frm;
1977 
1978     /*change max_bits_per_frame*/
1979     //ps_bit_allocation->i4_max_bits_per_frm[0]=i4_new_avg_bits_per_frm;
1980     //ps_bit_allocation->i4_max_bits_per_frm[1]=i4_new_avg_bits_per_frm;
1981     ps_bit_allocation->i4_min_bits_per_frm =
1982         i4_new_avg_bits_per_frm; /*VBR storage related parameter so this variable is currently not in use*/
1983     /* change the lower modules state */
1984     /*#ifdef DYNAMIC_RC*/
1985     if(i4_bit_rate != ps_bit_allocation->i4_bit_rate)
1986     {
1987         X_PROD_Y_DIV_Z(
1988             ps_bit_allocation->i4_max_tex_bits_for_i,
1989             i4_bit_rate,
1990             ps_bit_allocation->i4_bit_rate,
1991             ps_bit_allocation->i4_max_tex_bits_for_i);
1992     }
1993     /*#endif*/
1994 
1995     change_bitrate_in_error_bits(ps_bit_allocation->ps_error_bits, i4_bit_rate);
1996     change_frm_rate_in_error_bits(ps_bit_allocation->ps_error_bits, i4_frame_rate);
1997 
1998     /* Store the modified frame_rate */
1999     ps_bit_allocation->i4_frame_rate = i4_frame_rate;
2000     ps_bit_allocation->i4_bit_rate = i4_bit_rate;
2001     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
2002         ps_bit_allocation->ai4_peak_bit_rate[i] = i4_peak_bit_rate[i];
2003 }
2004 /*****************************************************************************
2005   Function Name : change_ba_peak_bit_rate
2006   Description   :
2007   Inputs        : ps_bit_allocation - bit_allocation structure
2008                   ai4_peak_bit_rate
2009   Globals       :
2010   Processing    :
2011   Outputs       :
2012   Returns       :
2013   Issues        :
2014   Revision History:
2015          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2016 *****************************************************************************/
change_ba_peak_bit_rate(bit_allocation_t * ps_bit_allocation,WORD32 * ai4_peak_bit_rate)2017 void change_ba_peak_bit_rate(bit_allocation_t *ps_bit_allocation, WORD32 *ai4_peak_bit_rate)
2018 {
2019     WORD32 i;
2020     /* Calculate the bits per frame */
2021     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
2022     {
2023         X_PROD_Y_DIV_Z(
2024             ai4_peak_bit_rate[i],
2025             1000,
2026             ps_bit_allocation->i4_frame_rate,
2027             ps_bit_allocation->i4_max_bits_per_frm[i]);
2028         ps_bit_allocation->ai4_peak_bit_rate[i] = ai4_peak_bit_rate[i];
2029     }
2030 }
2031 /*****************************************************************************
2032   Function Name : check_and_update_bit_allocation
2033   Description   :
2034   Inputs        : ps_bit_allocation - bit_allocation structure
2035                   ps_pic_handling
2036                   i4_max_bits_inflow_per_frm
2037   Globals       :
2038   Processing    :
2039   Outputs       :
2040   Returns       :
2041   Issues        :
2042   Revision History:
2043          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2044 *****************************************************************************/
check_and_update_bit_allocation(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling,WORD32 i4_max_bits_inflow_per_frm)2045 void check_and_update_bit_allocation(
2046     bit_allocation_t *ps_bit_allocation,
2047     pic_handling_handle ps_pic_handling,
2048     WORD32 i4_max_bits_inflow_per_frm)
2049 {
2050     WORD32 i4_max_drain_bits, i4_extra_bits, i4_less_bits, i4_allocated_saved_bits,
2051         i4_min_bits_for_period;
2052     WORD32 i4_num_frms_in_period = get_actual_num_frames_in_gop(ps_pic_handling);
2053     WORD32 i4_rem_bits_in_period = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
2054 
2055     /* If the remaining bits is greater than what can be drained in that period
2056             Clip the remaining bits in period to the maximum it can drain in that pariod
2057             with the error of current buffer size.Accumulate the saved bits if any.
2058        else if the remaining bits is lesser than the minimum bit rate promissed in that period
2059             Add the excess bits to remaining bits in period and reduce it from the saved bits
2060        Else
2061             Provide the extra bits from the "saved bits pool".*/
2062 
2063     i4_max_drain_bits = ps_bit_allocation->i4_num_gops_in_period * i4_num_frms_in_period *
2064                         i4_max_bits_inflow_per_frm;
2065 
2066     /* Practical DBF = middle of the buffer */
2067     /* NITT TO BE VERIFIED
2068     MAx drain bits becomes negative if the buffer underflows
2069     i4_max_drain_bits += (i4_cur_buf_size + i4_max_bits_inflow_per_frm - i4_tot_frame_bits); */
2070 
2071     i4_min_bits_for_period = ps_bit_allocation->i4_num_gops_in_period * i4_num_frms_in_period *
2072                              ps_bit_allocation->i4_min_bits_per_frm;
2073 
2074     /* printf((const WORD8*)" mdb %d, mbfp %d, rbip %d, sb %d \n",i4_max_drain_bits,
2075         i4_min_bits_for_period, ps_bit_allocation->i4_rem_bits_in_period, ps_bit_allocation->i4_saved_bits); */
2076     if(i4_rem_bits_in_period > i4_max_drain_bits)
2077     {
2078         i4_extra_bits = (i4_rem_bits_in_period - i4_max_drain_bits);
2079         update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, -1 * i4_extra_bits);
2080         overflow_avoided_summation(&ps_bit_allocation->i4_saved_bits, i4_extra_bits);
2081     }
2082     else if(i4_rem_bits_in_period < i4_min_bits_for_period)
2083     {
2084         i4_extra_bits = (i4_min_bits_for_period - i4_rem_bits_in_period);
2085         update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, i4_extra_bits);
2086         overflow_avoided_summation(&ps_bit_allocation->i4_saved_bits, -1 * i4_extra_bits);
2087     }
2088     else if(ps_bit_allocation->i4_saved_bits > 0)
2089     {
2090         i4_less_bits = i4_max_drain_bits - i4_rem_bits_in_period;
2091         i4_allocated_saved_bits = MIN(i4_less_bits, ps_bit_allocation->i4_saved_bits);
2092         update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, i4_allocated_saved_bits);
2093         ps_bit_allocation->i4_saved_bits -= i4_allocated_saved_bits;
2094     }
2095     return;
2096 }
2097 /*****************************************************************************
2098   Function Name : ba_get_frame_rate
2099   Description   :
2100   Inputs        : ps_bit_allocation - bit_allocation structure
2101   Revision History:
2102          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2103 *****************************************************************************/
ba_get_frame_rate(bit_allocation_t * ps_bit_allocation)2104 WORD32 ba_get_frame_rate(bit_allocation_t *ps_bit_allocation)
2105 {
2106     return (ps_bit_allocation->i4_frame_rate);
2107 }
2108 /*****************************************************************************
2109   Function Name : ba_get_bit_rate
2110   Description   :
2111   Inputs        : ps_bit_allocation - bit_allocation structure
2112   Revision History:
2113          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2114 *****************************************************************************/
ba_get_bit_rate(bit_allocation_t * ps_bit_allocation)2115 WORD32 ba_get_bit_rate(bit_allocation_t *ps_bit_allocation)
2116 {
2117     return (ps_bit_allocation->i4_bit_rate);
2118 }
2119 /*****************************************************************************
2120   Function Name : ba_get_2pass_avg_bit_rate
2121   Description   :
2122   Inputs        : ps_bit_allocation - bit_allocation structure
2123   Revision History:
2124          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2125 *****************************************************************************/
ba_get_2pass_avg_bit_rate(bit_allocation_t * ps_bit_allocation)2126 LWORD64 ba_get_2pass_avg_bit_rate(bit_allocation_t *ps_bit_allocation)
2127 {
2128     return (ps_bit_allocation->i8_2pass_avg_bit_rate);
2129 }
2130 /*****************************************************************************
2131   Function Name : ba_set_2pass_avg_bit_rate
2132   Description   :
2133   Inputs        : ps_bit_allocation - bit_allocation structure
2134   Revision History:
2135          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2136 *****************************************************************************/
ba_set_2pass_avg_bit_rate(bit_allocation_t * ps_bit_allocation,LWORD64 i8_2pass_avg_bit_rate)2137 void ba_set_2pass_avg_bit_rate(bit_allocation_t *ps_bit_allocation, LWORD64 i8_2pass_avg_bit_rate)
2138 {
2139     ps_bit_allocation->i8_2pass_avg_bit_rate = i8_2pass_avg_bit_rate;
2140 }
2141 /*****************************************************************************
2142   Function Name : ba_get_peak_bit_rate
2143   Description   :
2144   Inputs        : ps_bit_allocation - bit_allocation structure
2145   Revision History:
2146          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2147 *****************************************************************************/
ba_get_peak_bit_rate(bit_allocation_t * ps_bit_allocation,WORD32 * pi4_peak_bit_rate)2148 void ba_get_peak_bit_rate(bit_allocation_t *ps_bit_allocation, WORD32 *pi4_peak_bit_rate)
2149 {
2150     WORD32 i;
2151     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
2152     {
2153         pi4_peak_bit_rate[i] = ps_bit_allocation->ai4_peak_bit_rate[i];
2154     }
2155 }
2156 /*****************************************************************************
2157   Function Name : init_intra_header_bits
2158   Description   :
2159   Inputs        : ps_bit_allocation - bit_allocation structure
2160   Revision History:
2161          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2162 *****************************************************************************/
init_intra_header_bits(bit_allocation_t * ps_bit_allocation,WORD32 i4_intra_header_bits)2163 void init_intra_header_bits(bit_allocation_t *ps_bit_allocation, WORD32 i4_intra_header_bits)
2164 {
2165     //ASSERT(i4_intra_header_bits == 0);
2166     ps_bit_allocation->i4_prev_frm_header_bits[0] = i4_intra_header_bits;
2167 }
2168 /*****************************************************************************
2169   Function Name : get_prev_header_bits
2170   Description   :
2171   Inputs        : ps_bit_allocation - bit_allocation structure
2172   Revision History:
2173          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2174 *****************************************************************************/
get_prev_header_bits(bit_allocation_t * ps_bit_allocation,WORD32 pic_type)2175 WORD32 get_prev_header_bits(bit_allocation_t *ps_bit_allocation, WORD32 pic_type)
2176 {
2177     //ASSERT(ps_bit_allocation->i4_prev_frm_header_bits[pic_type] == 0);
2178     return (ps_bit_allocation->i4_prev_frm_header_bits[pic_type]);
2179 }
2180 
2181 #define I_TO_P_RATIO_HI_MO (16)
2182 #define P_TO_B_RATIO_HI_MO (18)
2183 #define P_TO_B_RATIO_HI_MO_HBR (16)
2184 /*****************************************************************************
2185   Function Name : set_Kp_Kb_for_hi_motion
2186   Description   :
2187   Inputs        : ps_bit_allocation - bit_allocation structure
2188   Revision History:
2189          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2190 *****************************************************************************/
set_Kp_Kb_for_hi_motion(bit_allocation_t * ps_bit_allocation)2191 void set_Kp_Kb_for_hi_motion(bit_allocation_t *ps_bit_allocation)
2192 {
2193     ps_bit_allocation->i2_K[I_PIC] = (1 << K_Q);
2194     ps_bit_allocation->i2_K[P_PIC] = I_TO_P_RATIO_HI_MO;
2195 
2196     if(ps_bit_allocation->i4_is_hbr)
2197     {
2198         ps_bit_allocation->i2_K[B_PIC] = (P_TO_B_RATIO_HI_MO * I_TO_P_RATIO_HI_MO) >> K_Q;
2199     }
2200     else
2201     {
2202         ps_bit_allocation->i2_K[B_PIC] = (P_TO_B_RATIO_HI_MO_HBR * I_TO_P_RATIO_HI_MO) >> K_Q;
2203     }
2204 }
2205 /*****************************************************************************
2206   Function Name : reset_Kp_Kb
2207   Description   : I_P_B_B1_B2 QP offset calculation based on hme sad
2208   Inputs        :
2209   Globals       :
2210   Processing    :
2211   Outputs       :
2212   Returns       :
2213   Issues        :
2214   Revision History:
2215          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2216 *****************************************************************************/
2217 
reset_Kp_Kb(bit_allocation_t * ps_bit_allocation,float f_i_to_avg_ratio,WORD32 i4_num_active_pic_type,float f_hme_sad_per_pixel,float f_max_hme_sad_per_pixel,WORD32 * pi4_complexity_bin,WORD32 i4_rc_pass)2218 void reset_Kp_Kb(
2219     bit_allocation_t *ps_bit_allocation,
2220     float f_i_to_avg_ratio,
2221     WORD32 i4_num_active_pic_type,
2222     float f_hme_sad_per_pixel,
2223     float f_max_hme_sad_per_pixel,
2224     WORD32 *pi4_complexity_bin,
2225     WORD32 i4_rc_pass)
2226 {
2227     WORD32 i, i4_ratio = (WORD32)(f_max_hme_sad_per_pixel / f_hme_sad_per_pixel);
2228     WORD32 ai4_offsets[5] = { 0 };
2229     float f_ratio = f_max_hme_sad_per_pixel / f_hme_sad_per_pixel;
2230 
2231     /*Filling out the offfset array for QP offset 0 - 7*/
2232     const WORD32 ai4_offset_qp[8] = {
2233         (1 << K_Q),
2234         I_TO_P_RATIO,
2235         ((P_TO_B_RATIO * I_TO_P_RATIO) >> K_Q),
2236         (B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q),
2237         (B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q + K_Q),
2238         (B1_TO_B2_RATIO * B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >>
2239             (K_Q + K_Q + K_Q + K_Q),
2240         (B1_TO_B2_RATIO * B1_TO_B2_RATIO * B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO *
2241          I_TO_P_RATIO) >>
2242             (K_Q + K_Q + K_Q + K_Q + K_Q),
2243         (B1_TO_B2_RATIO * B1_TO_B2_RATIO * B1_TO_B2_RATIO * B1_TO_B2_RATIO * B_TO_B1_RATIO *
2244          P_TO_B_RATIO * I_TO_P_RATIO) >>
2245             (K_Q + K_Q + K_Q + K_Q + K_Q + K_Q)
2246     };
2247 
2248     ba_get_qp_offset_offline_data(
2249         ai4_offsets, i4_ratio, f_ratio, i4_num_active_pic_type, pi4_complexity_bin);
2250     for(i = 0; i < 5; i++)
2251     {
2252         ASSERT((ai4_offsets[i] >= 0) && (ai4_offsets[i] <= 7));
2253         ps_bit_allocation->i2_K[i] = ai4_offset_qp[ai4_offsets[i]];
2254 
2255         /*For interlaced also we are filling out the offsets*/
2256         if(i > 0)
2257             ps_bit_allocation->i2_K[i + 4] = ai4_offset_qp[ai4_offsets[i]];
2258     }
2259 }
2260 
2261 /*****************************************************************************
2262   Function Name : ba_get_qp_offset_offline_data
2263   Description   : Offline model for qp offset calculation
2264   Inputs        : ai4_offsets
2265                   i4_ratio
2266                   f_ratio
2267                   i4_num_active_pic_type
2268                   pi4_complexity_bin
2269   Revision History:
2270          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2271 *****************************************************************************/
ba_get_qp_offset_offline_data(WORD32 ai4_offsets[5],WORD32 i4_ratio,float f_ratio,WORD32 i4_num_active_pic_type,WORD32 * pi4_complexity_bin)2272 void ba_get_qp_offset_offline_data(
2273     WORD32 ai4_offsets[5],
2274     WORD32 i4_ratio,
2275     float f_ratio,
2276     WORD32 i4_num_active_pic_type,
2277     WORD32 *pi4_complexity_bin)
2278 {
2279     WORD32 i4_bin;
2280     /*Desired QP offset's for different complexity bins depending on number of temporal layers*/
2281     /*There are 6 complexity bins
2282     Max_compl - Max_compl*3/4,
2283     Max_compl*3/4 - Max_compl*1/2,
2284     Max_compl*1/2 - Max_compl*1/4,
2285     Max_compl*1/4 - Max_compl*1/8,
2286     Max_compl*1/8 - Max_compl*1/16
2287     <Max_compl*1/16*/
2288     /*The kids_rain content was run on different resolutions and the max value for different temporal configs is the max value used*/
2289 
2290     /*First index for complexity bin, second index for pic_types (P,B,B1,B2)*/
2291     const WORD32 ai4_offset_values_7B[7][4] = { { 0, 1, 1, 2 }, { 1, 1, 2, 3 }, { 1, 2, 3, 3 },
2292                                                 { 1, 2, 3, 4 }, { 2, 2, 3, 4 }, { 2, 3, 4, 5 },
2293                                                 { 3, 4, 5, 6 } };
2294     const WORD32 ai4_offset_values_3B[7][3] = { { 0, 1, 2 }, { 1, 2, 2 }, { 1, 2, 3 }, { 2, 2, 3 },
2295                                                 { 2, 3, 4 }, { 2, 4, 5 }, { 3, 4, 5 } };
2296     const WORD32 ai4_offset_values_1B[7][2] = { { 1, 1 }, { 1, 2 }, { 1, 2 }, { 1, 3 },
2297                                                 { 2, 3 }, { 3, 4 }, { 3, 5 } };
2298     const WORD32 ai4_offset_values_0B[7][1] = { { 0 }, { 1 }, { 2 }, { 2 }, { 3 }, { 3 }, { 4 } };
2299 
2300     /*The ratio is clipped between 16 and 2 to put it into bins*/
2301 
2302     CLIP(i4_ratio, 16, 2);
2303 
2304     for(i4_bin = 1; i4_bin < 5; i4_bin++)
2305     {
2306         if((i4_ratio >> i4_bin) == 1)
2307         {
2308             break;
2309         }
2310     }
2311     switch(i4_bin)
2312     {
2313     case(1):
2314         (f_ratio > 2.0f) ? (i4_bin = 3) : ((f_ratio > 1.33f) ? (i4_bin = 2) : (i4_bin = 1));
2315         break;
2316     case(2):
2317         i4_bin = 4;
2318         break;
2319     case(3):
2320         (f_ratio > 12.0f) ? (i4_bin = 6) : (i4_bin = 5);
2321         break;
2322     case(4):
2323         i4_bin = 7;
2324         break;
2325     }
2326 
2327     /*For the i4_bin == 1, actual ratio could be >2.0,>1.33 or lesser hence putting them into different bins*/
2328 
2329     trace_printf("1 bin %d", i4_bin);
2330 
2331     /*Total 7 bins hence the clip*/
2332     CLIP(i4_bin, 7, 1);
2333 
2334     *pi4_complexity_bin = i4_bin - 1;
2335 
2336     switch(i4_num_active_pic_type)
2337     {
2338     case 5:
2339         memmove(
2340             &ai4_offsets[1],
2341             ai4_offset_values_7B[i4_bin - 1],
2342             sizeof(ai4_offset_values_7B[i4_bin - 1]));
2343         break;
2344     case 4:
2345         memmove(
2346             &ai4_offsets[1],
2347             ai4_offset_values_3B[i4_bin - 1],
2348             sizeof(ai4_offset_values_3B[i4_bin - 1]));
2349         break;
2350     case 3:
2351         memmove(
2352             &ai4_offsets[1],
2353             ai4_offset_values_1B[i4_bin - 1],
2354             sizeof(ai4_offset_values_1B[i4_bin - 1]));
2355         break;
2356     case 2:
2357         memmove(
2358             &ai4_offsets[1],
2359             ai4_offset_values_0B[i4_bin - 1],
2360             sizeof(ai4_offset_values_0B[i4_bin - 1]));
2361         break;
2362     default:
2363         memmove(
2364             &ai4_offsets[1],
2365             ai4_offset_values_0B[i4_bin - 1],
2366             sizeof(ai4_offset_values_0B[i4_bin - 1]));
2367         break;
2368     }
2369 
2370     trace_printf(
2371         "Enc %d,%d,%d,%d,%d offsets",
2372         ai4_offsets[0],
2373         ai4_offsets[1],
2374         ai4_offsets[2],
2375         ai4_offsets[3],
2376         ai4_offsets[4]);
2377 }
2378 
2379 /*****************************************************************************
2380   Function Name : get_Kp_Kb
2381   Description   : Get the operating Kp and Kp so that scene cut sub gop can go
2382                   with similar qp offset
2383   Inputs        : ps_bit_allocation
2384                   e_pic_type
2385   Revision History:
2386          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2387 *****************************************************************************/
2388 
get_Kp_Kb(bit_allocation_t * ps_bit_allocation,picture_type_e e_pic_type)2389 WORD32 get_Kp_Kb(bit_allocation_t *ps_bit_allocation, picture_type_e e_pic_type)
2390 {
2391     return ps_bit_allocation->i2_K[e_pic_type];
2392 }
2393 /*****************************************************************************
2394   Function Name : get_scene_change_tot_frm_bits
2395   Description   : Based on remaining bits in period and default I_TO_B complexity
2396                   total bit budget for scene cut frame is obtained.
2397   Inputs        : ps_bit_allocation - bit_allocation structure
2398                   ps_rd_model - rd model pointer (for all the frame types)
2399                   e_pic_type - picture type
2400   Globals       :
2401   Processing    :
2402   Outputs       :
2403   Returns       :
2404   Issues        :
2405   Revision History:
2406          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2407 *****************************************************************************/
get_scene_change_tot_frm_bits(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handling,cbr_buffer_handle ps_cbr_buf_handling,WORD32 i4_num_pixels,WORD32 i4_f_sim_lap,float i_to_avg_rest,WORD32 i4_call_type,WORD32 i4_non_I_scd,WORD32 i4_is_infinite_gop)2408 WORD32 get_scene_change_tot_frm_bits(
2409     bit_allocation_t *ps_bit_allocation,
2410     pic_handling_handle ps_pic_handling,
2411     cbr_buffer_handle ps_cbr_buf_handling,
2412     WORD32 i4_num_pixels,
2413     WORD32 i4_f_sim_lap,
2414     float i_to_avg_rest,
2415     WORD32 i4_call_type,
2416     WORD32 i4_non_I_scd,
2417     WORD32 i4_is_infinite_gop)
2418 {
2419     WORD32 j;
2420     WORD32 i4_tot_bits_for_scd_frame;
2421     WORD32 i4_total_bits_in_period;
2422     //number_t avq_complexity_estimate[MAX_PIC_TYPE];
2423     WORD32 /* ai4_rem_frms_in_period[MAX_PIC_TYPE], */
2424         ai4_frms_in_period[MAX_PIC_TYPE];
2425     WORD32 i4_max_consumable_bits;
2426     WORD32 i4_intra_frm_int;
2427     WORD32 ai4_actual_frms_in_gop[MAX_PIC_TYPE], i, i4_total_frames = 0;
2428     float final_ratio, f_sim = (float)i4_f_sim_lap / 128;
2429 
2430     i4_intra_frm_int = pic_type_get_intra_frame_interval(ps_pic_handling);
2431 
2432     /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
2433     /* pic_type_get_rem_frms_in_gop(ps_pic_handling, ai4_rem_frms_in_period); */
2434     pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_period);
2435 
2436     /* Depending on the number of gops in a period, find the num_frms_in_prd */
2437     for(j = 0; j < MAX_PIC_TYPE; j++)
2438     {
2439         /* ai4_rem_frms_in_period[j] += (ai4_frms_in_period[j] * (ps_bit_allocation->i4_num_gops_in_period - 1)); */
2440         ai4_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period;
2441     }
2442 
2443     /* Remove the header bits from the remaining bits to find how many bits you
2444        can transfer.*/
2445     {
2446         i4_total_bits_in_period = ps_bit_allocation->s_rbip.i4_bits_per_frm *
2447                                   ps_bit_allocation->s_rbip.i4_tot_frms_in_gop;
2448         //trace_printf(" SCD_rbip = %d",i4_total_bits_in_period);
2449     }
2450     //since this marks end of previous GOP it is better to consider actual error than ps_bit_allocation->i4_frame_level_bit_error;
2451 
2452     {
2453         pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_actual_frms_in_gop);
2454         for(i = 0; i < MAX_PIC_TYPE; i++)
2455         {
2456             i4_total_frames += ai4_frms_in_period[i];
2457         }
2458         i4_max_consumable_bits = ps_bit_allocation->i4_max_bits_per_frm[0] * i4_total_frames;
2459     }
2460     if(i4_total_bits_in_period > 0)
2461     {
2462         i4_total_bits_in_period = MIN(i4_total_bits_in_period, i4_max_consumable_bits);
2463     }
2464     final_ratio = i_to_avg_rest;
2465     /*If FSIM says the content is static (> 126 is assured to be static*/
2466     /*Very low FSIM safety check*/
2467     if(f_sim < 0.50 && final_ratio > 8)
2468         final_ratio = 8;
2469     /*Do not apply safety limits if second pass as data is reliable*/
2470     if(ps_bit_allocation->i4_ba_rc_pass != 2)
2471     {
2472         /*clip min max values*/
2473         if((i4_is_infinite_gop == 1) && (final_ratio > I_TO_AVG_REST_GOP_BIT_MAX_INFINITE))
2474         {
2475             final_ratio = I_TO_AVG_REST_GOP_BIT_MAX_INFINITE;
2476         }
2477         else
2478         {
2479             if(final_ratio > I_TO_AVG_REST_GOP_BIT_MAX)
2480                 final_ratio = I_TO_AVG_REST_GOP_BIT_MAX;
2481         }
2482         if(final_ratio < I_TO_AVG_REST_GOP_BIT_MIN)
2483             final_ratio = I_TO_AVG_REST_GOP_BIT_MIN;
2484     }
2485     else
2486     {
2487         if(final_ratio > I_TO_AVG_REST_GOP_BIT_MAX_2_PASS)
2488             final_ratio = I_TO_AVG_REST_GOP_BIT_MAX_2_PASS;
2489 
2490         if(final_ratio < I_TO_AVG_REST_GOP_BIT_MIN_2_PASS)
2491             final_ratio = I_TO_AVG_REST_GOP_BIT_MIN_2_PASS;
2492     }
2493 
2494     /*based on offline runs to find I_BITS/(AVERAGE_CONSUMPTION_OF_REST_GOP)*/
2495     /*  BITS FOR I
2496         BITS = I_TO_AVG_REST_GOP * total_bits_period
2497                 -------------------------------------
2498                 N - (num_I_in_period) + (I_TO_AVG_REST_GOP * num_I_in_period)
2499                 */
2500     i4_tot_bits_for_scd_frame = bit_alloc_get_intra_bits(
2501         ps_bit_allocation,
2502         ps_pic_handling,
2503         ps_cbr_buf_handling,
2504         I_PIC,
2505         NULL,
2506         1,
2507         final_ratio,
2508         i4_call_type,
2509         i4_non_I_scd,
2510         0.0f);
2511     ps_bit_allocation->i4_excess_bits_from_buffer = 0;
2512 
2513     if(i4_call_type == 1)
2514     {
2515         trace_printf("I_TO_AVG_REST_GOP_BIT used = %f\n", final_ratio);
2516         trace_printf(" SCD DETECTED   bits allocated = %d", i4_tot_bits_for_scd_frame);
2517     }
2518 
2519     /* If the remaining bits in the period becomes negative then the estimated texture
2520     bits would also become negative. This would send a feedback to the model which
2521     may go for a toss. Thus sending the minimum possible value = 0 */
2522     if(i4_tot_bits_for_scd_frame < 0)
2523         i4_tot_bits_for_scd_frame = 0;
2524 
2525     return (i4_tot_bits_for_scd_frame);
2526 }
2527 
2528 /*****************************************************************************
2529   Function Name : update_estimate_status
2530   Description   : Est texture bits in case of scene cut is obtained form offline
2531                   model. Update bit alloc
2532   Inputs        : ps_bit_allocation
2533                   e_pic_type
2534   Revision History:
2535          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2536 *****************************************************************************/
2537 
update_estimate_status(bit_allocation_t * ps_bit_allocation,WORD32 i4_est_texture_bits,WORD32 i4_hdr_bits,WORD32 i4_est_text_bits_ctr_get_qp)2538 void update_estimate_status(
2539     bit_allocation_t *ps_bit_allocation,
2540     WORD32 i4_est_texture_bits,
2541     WORD32 i4_hdr_bits,
2542     WORD32 i4_est_text_bits_ctr_get_qp)
2543 {
2544     ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_get_qp] = i4_est_texture_bits;
2545     ps_bit_allocation->ai4_cur_frm_est_hdr_bits[i4_est_text_bits_ctr_get_qp] = i4_hdr_bits;
2546 }
2547 
2548 /*****************************************************************************
2549   Function Name : bit_allocation_set_num_scd_lap_window
2550   Description   :
2551   Inputs        : ps_bit_allocation
2552                   i4_num_scd_in_lap_window
2553   Revision History:
2554          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2555 *****************************************************************************/
bit_allocation_set_num_scd_lap_window(bit_allocation_t * ps_bit_allocation,WORD32 i4_num_scd_in_lap_window,WORD32 i4_num_frames_b4_Scd)2556 void bit_allocation_set_num_scd_lap_window(
2557     bit_allocation_t *ps_bit_allocation,
2558     WORD32 i4_num_scd_in_lap_window,
2559     WORD32 i4_num_frames_b4_Scd)
2560 {
2561     ps_bit_allocation->i4_num_scd_in_lap_window = i4_num_scd_in_lap_window;
2562     ps_bit_allocation->i4_num_frm_b4_scd = i4_num_frames_b4_Scd;
2563     /*To avoid trashing I frame badly due to back to back scene cut limit the increment in Ni*/
2564     if(ps_bit_allocation->i4_num_scd_in_lap_window > 2)
2565         ps_bit_allocation->i4_num_scd_in_lap_window = 2;
2566 }
2567 /*****************************************************************************
2568   Function Name : bit_allocation_set_sc_i_in_rc_look_ahead
2569   Description   :
2570   Inputs        : ps_bit_allocation
2571                   i4_next_sc_i_in_rc_look_ahead
2572   Revision History:
2573          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2574 *****************************************************************************/
bit_allocation_set_sc_i_in_rc_look_ahead(bit_allocation_t * ps_bit_allocation,WORD32 i4_next_sc_i_in_rc_look_ahead)2575 void bit_allocation_set_sc_i_in_rc_look_ahead(
2576     bit_allocation_t *ps_bit_allocation, WORD32 i4_next_sc_i_in_rc_look_ahead)
2577 {
2578     ps_bit_allocation->i4_next_sc_i_in_rc_look_ahead = i4_next_sc_i_in_rc_look_ahead;
2579 }
2580 /*****************************************************************************
2581   Function Name : bit_allocation_update_gop_level_bit_error
2582   Description   :
2583   Inputs        : ps_bit_allocation
2584                   i4_error_bits
2585   Revision History:
2586          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2587 *****************************************************************************/
bit_allocation_update_gop_level_bit_error(bit_allocation_t * ps_bit_allocation,WORD32 i4_error_bits)2588 void bit_allocation_update_gop_level_bit_error(
2589     bit_allocation_t *ps_bit_allocation, WORD32 i4_error_bits)
2590 {
2591     ps_bit_allocation->i4_gop_level_bit_error += i4_error_bits;
2592     ps_bit_allocation->i4_frame_level_bit_error += i4_error_bits;
2593     /*Error is (rdopt - entropy) Hence for total bit consumption subtract error*/
2594     ps_bit_allocation->i8_cur_gop_bit_consumption -= i4_error_bits;
2595 }
2596 
2597 /******************************************************************************
2598   Function Name   : rc_update_bit_distribution_gop_level_2pass
2599   Description     : This function distributes the bits to all the gops depending
2600                     on the complexities and the error bits accumulated until now
2601   Arguments       : ps_rate_control_api - rate control api handle
2602                     i4_start_gop_number : GOP number from which distribution should happen
2603   Return Values   :
2604   Revision History:
2605 
2606 
2607  Assumptions    -
2608 
2609  Checks     -
2610 *****************************************************************************/
rc_update_bit_distribution_gop_level_2pass(bit_allocation_t * ps_bit_allocation,pic_handling_handle ps_pic_handle,void * pv_gop_stat,rc_type_e e_rc_type,WORD32 i4_num_gop,WORD32 i4_start_gop_number,float f_avg_qscale_first_pass,WORD32 i4_max_ebf,WORD32 i4_ebf,LWORD64 i8_tot_bits_sequence,WORD32 i4_comp_error)2611 void rc_update_bit_distribution_gop_level_2pass(
2612     bit_allocation_t *ps_bit_allocation,
2613     pic_handling_handle ps_pic_handle,
2614     void *pv_gop_stat,
2615     rc_type_e e_rc_type,
2616     WORD32 i4_num_gop,
2617     WORD32 i4_start_gop_number,
2618     float f_avg_qscale_first_pass,
2619     WORD32 i4_max_ebf,
2620     WORD32 i4_ebf,
2621     LWORD64 i8_tot_bits_sequence,
2622     WORD32 i4_comp_error)
2623 {
2624     float cur_peak_factor, f_bits_per_frame;
2625     LWORD64 total_nbp_bits_allocated = 0;
2626     LWORD64 total_bp_bits_allocated = 0;
2627     LWORD64 total_bits_allocated = 0, prev_total_bits_allocated = -1;
2628     WORD32
2629     i4_num_loop_inter_GOP_alloc = 0, ai4_peak_bitrate[MAX_NUM_DRAIN_RATES] = { 0 },
2630     temp_i; /*Loop 20 times to meet precise bitrate, after that exit the loop and distribute remaining bits equally for all GOP*/
2631     gop_level_stat_t *ps_cur_gop;
2632     WORD32 i4_num_frames_in_gop, i4_cur_gop_num, i4_num_frm_with_rmax, i4_num_frm_with_rmin;
2633     LWORD64 i8_max_bit_for_gop, /*i8_min_bit_for_gop,*/ i8_peak_bitrate, i8_frame_rate,
2634         i8_current_bitrate = (LWORD64)ba_get_2pass_avg_bit_rate(ps_bit_allocation);
2635     LWORD64 i8_actual_avg_bit_rate = ba_get_bit_rate(ps_bit_allocation);
2636     LWORD64 i8_num_frame_remaining = 0, i8_excess_bits = 0;
2637     float min_complexity_beyond_peak /*,f_max_complexity = 1.0f,f_min_complexity = 0.0f*/
2638         ;  //The minimum complexity for which bit allocation exceeds peak rate but
2639     float f_avg_bits_complexity_based;
2640     WORD32 i4_num_gop_not_rmax;
2641     LWORD64 i8_bits_for_this_gop;
2642 
2643 #define MAX_LOOP_INTER_GOP_ALLOC                                                                   \
2644     20 /*The below loop shall run maximum of this macro once it exits allocate the difference bits equally for all the GOPS*/
2645 
2646     i4_ebf = MAX(i4_ebf, 0);
2647     //i4_ebf = 0;
2648     if(i4_start_gop_number == 0)
2649     {
2650         cur_peak_factor = 7.0;
2651     }
2652     else
2653     {
2654         cur_peak_factor = ps_bit_allocation->f_cur_peak_factor_2pass;
2655     }
2656     /*Parsing of entire log file is done and summary of GOP level data has been updated in the temp,
2657     Iteratively allocate the bits to make it meet bitrate*/
2658     for(temp_i = i4_start_gop_number; temp_i < i4_num_gop; temp_i++)
2659     {
2660         ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + temp_i);
2661     }
2662     i8_frame_rate = ba_get_frame_rate(ps_bit_allocation);
2663     ba_get_peak_bit_rate(ps_bit_allocation, &ai4_peak_bitrate[0]);
2664     i8_peak_bitrate = (LWORD64)ai4_peak_bitrate[0];
2665 
2666     /*Modify the bitrate depending on the error bits and total bits*/
2667     //i8_current_bitrate = (LWORD64)((float)i8_tot_bits_sequence*i8_frame_rate/(1000*i8_num_frame_remaining));
2668 
2669     f_bits_per_frame = (float)i8_current_bitrate / i8_frame_rate * 1000;
2670     ps_bit_allocation->i8_current_bitrate_2_pass = i8_current_bitrate;
2671     //printf("\n%d current bitrate",i8_current_bitrate);
2672 
2673     do
2674     {
2675         /*Get gop level stat*/
2676         /*recalculate the bits based on new scaling factor*/
2677         total_bits_allocated = 0;
2678         total_bp_bits_allocated = 0;
2679         total_nbp_bits_allocated = 0;
2680         min_complexity_beyond_peak =
2681             (float)ps_bit_allocation->ai4_peak_bit_rate[0] / i8_current_bitrate;
2682 
2683         /*min_complexity_beyond_peak = ba_get_min_complexity_for_peak_br(ps_bit_allocation->ai4_peak_bit_rate[0],
2684                                             (WORD32)i8_current_bitrate,
2685                                             cur_peak_factor,
2686                                             f_max_complexity,
2687                                             f_min_complexity,
2688                                             ps_bit_allocation->i4_ba_rc_pass);*/
2689 
2690         for(i4_cur_gop_num = i4_start_gop_number; i4_cur_gop_num < i4_num_gop; i4_cur_gop_num++)
2691         {
2692             ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + i4_cur_gop_num);
2693             ps_cur_gop->f_bits_complexity_l1_based_peak_factor =
2694                 ps_cur_gop->f_bits_complexity_l1_based * cur_peak_factor;
2695         }
2696         i4_num_frm_with_rmax = 0;
2697         i4_num_frm_with_rmin = 0;
2698         f_avg_bits_complexity_based = 0.0;
2699         i4_num_gop_not_rmax = 0;
2700         i8_num_frame_remaining = 0;
2701         for(i4_cur_gop_num = i4_start_gop_number; i4_cur_gop_num < i4_num_gop; i4_cur_gop_num++)
2702         {
2703             ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + i4_cur_gop_num);
2704             if(!ps_cur_gop->i4_peak_br_clip)
2705             {
2706                 f_avg_bits_complexity_based +=
2707                     (ps_cur_gop->f_bits_complexity_l1_based * ps_cur_gop->i4_tot_frm_in_gop);
2708                 i8_num_frame_remaining += ps_cur_gop->i4_tot_frm_in_gop;
2709                 i4_num_gop_not_rmax++;
2710             }
2711         }
2712         f_avg_bits_complexity_based = (f_avg_bits_complexity_based / i8_num_frame_remaining);
2713         for(i4_cur_gop_num = i4_start_gop_number; i4_cur_gop_num < i4_num_gop; i4_cur_gop_num++)
2714         {
2715             /*Parse through all the GOP*/
2716             /*get current gop data*/
2717             //i4_num_frames_in_gop = 0;
2718             LWORD64 i8_avg_bit_rate_bits;
2719             LWORD64 i8_curr_bit_rate_bits;
2720             ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + i4_cur_gop_num);
2721 
2722             if(ps_cur_gop->i4_peak_br_clip)
2723             {
2724                 i4_num_frm_with_rmax++;
2725                 total_nbp_bits_allocated += ps_cur_gop->i8_bits_allocated_to_gop;
2726                 continue;
2727             }
2728             ps_cur_gop->f_buffer_play_complexity = 0.;
2729             //ps_cur_gop->f_gop_level_complexity_sum = -1;
2730             //ps_cur_gop->i8_buffer_play_bits = 0;
2731             ps_cur_gop->i8_buffer_play_bits_allocated_to_gop = 0;
2732             i4_num_frames_in_gop = ps_cur_gop->i4_tot_frm_in_gop;
2733 
2734             if(i4_num_gop_not_rmax == i4_num_gop)
2735             {
2736                 i8_bits_for_this_gop =
2737                     (LWORD64)((i8_current_bitrate * i4_num_frames_in_gop * 1000) / i8_frame_rate);
2738                 if(e_rc_type == VBR_STREAMING)
2739                 {
2740                     ps_cur_gop->i8_bits_allocated_to_gop = (LWORD64)(
2741                         (ps_cur_gop->f_bits_complexity_l1_based / (f_avg_bits_complexity_based)) *
2742                         i8_bits_for_this_gop);
2743                 }
2744                 else
2745                 {
2746                     ps_cur_gop->i8_bits_allocated_to_gop =
2747                         (LWORD64)(i8_current_bitrate * i4_num_frames_in_gop / i8_frame_rate * 1000);
2748                 }
2749             }
2750             else
2751             {
2752                 //i8_bits_for_this_gop = (LWORD64)((i8_excess_bits * i4_num_frames_in_gop * 1000)/(i8_frame_rate*i4_num_gop_not_rmax));
2753                 i8_bits_for_this_gop =
2754                     (LWORD64)((i8_excess_bits * i4_num_frames_in_gop) / (i8_num_frame_remaining));
2755                 if(e_rc_type == VBR_STREAMING)
2756                 {
2757                     ps_cur_gop->i8_bits_allocated_to_gop += (LWORD64)(
2758                         (ps_cur_gop->f_bits_complexity_l1_based / (f_avg_bits_complexity_based)) *
2759                         i8_bits_for_this_gop);
2760                 }
2761                 else
2762                 {
2763                     ASSERT(0);
2764                 }
2765             }
2766             ps_cur_gop->i8_actual_bits_allocated_to_gop = ps_cur_gop->i8_bits_allocated_to_gop;
2767             /*clip based on peak rate*/
2768             i8_max_bit_for_gop = i8_peak_bitrate * i4_num_frames_in_gop * 1000 / i8_frame_rate;
2769             ps_cur_gop->i8_max_bit_for_gop = i8_max_bit_for_gop;
2770             ps_cur_gop->i4_peak_br_clip = 0;
2771             if(ps_cur_gop->i8_bits_allocated_to_gop > i8_max_bit_for_gop)
2772             {
2773                 ps_cur_gop->i8_bits_allocated_to_gop = i8_max_bit_for_gop;
2774                 ps_cur_gop->i4_peak_br_clip = 1;
2775                 i4_num_frm_with_rmax++;
2776                 /*if(ps_cur_gop->f_bits_complexity_l1_based < min_complexity_beyond_peak)
2777                     min_complexity_beyond_peak = ps_cur_gop->f_bits_complexity_l1_based;*/
2778             }
2779             i8_curr_bit_rate_bits =
2780                 (LWORD64)(i8_current_bitrate * i4_num_frames_in_gop / i8_frame_rate * 1000);
2781             i8_avg_bit_rate_bits =
2782                 (LWORD64)(i8_actual_avg_bit_rate * i4_num_frames_in_gop / i8_frame_rate * 1000);
2783             ps_cur_gop->i4_is_below_avg_rate_gop_frame = 0;
2784             if(ps_cur_gop->i8_bits_allocated_to_gop <
2785                (MIN(i8_curr_bit_rate_bits, ps_cur_gop->i8_minimum_gop_bits)))
2786             {
2787                 ps_cur_gop->i4_is_below_avg_rate_gop_frame = 1;
2788                 ps_cur_gop->i8_bits_allocated_to_gop =
2789                     MIN(i8_curr_bit_rate_bits, ps_cur_gop->i8_minimum_gop_bits);
2790                 i4_num_frm_with_rmin++;
2791             }
2792             total_nbp_bits_allocated += ps_cur_gop->i8_bits_allocated_to_gop;
2793         }
2794         i4_num_loop_inter_GOP_alloc++;
2795         /*check for tolerance of 0.5% in terms of meeting bitrate, terminate the loop when bitrate is met*/
2796         total_bits_allocated = total_nbp_bits_allocated + total_bp_bits_allocated;
2797         if((total_bits_allocated < (1.005 * i8_tot_bits_sequence) &&
2798             total_bits_allocated > (0.995 * i8_tot_bits_sequence)) ||
2799            (i4_num_loop_inter_GOP_alloc > MAX_LOOP_INTER_GOP_ALLOC) /*|| (cur_peak_factor <= 1 )*/)
2800         {
2801             float error_bits = ((float)i8_tot_bits_sequence - total_bits_allocated);
2802             WORD32 temp_i;
2803             float f_per_frm_bits = ((float)(i8_current_bitrate)) / (i8_frame_rate / 1000);
2804             //cur_peak_factor *= (float)i8_tot_bits_sequence/total_bits_allocated;
2805             if((i4_comp_error == 1) || ((i4_comp_error == 0) && (error_bits < 0)))
2806             {
2807                 for(temp_i = i4_start_gop_number; temp_i < i4_num_gop; temp_i++)
2808                 {
2809                     ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + temp_i);
2810                     ps_cur_gop->i8_bits_allocated_to_gop += (LWORD64)(
2811                         (error_bits * ps_cur_gop->i8_bits_allocated_to_gop / total_bits_allocated));
2812                 }
2813             }
2814             for(temp_i = i4_start_gop_number; temp_i < i4_num_gop; temp_i++)
2815             {
2816                 ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + temp_i);
2817                 ps_cur_gop->f_avg_complexity_factor = (ps_cur_gop->f_bits_complexity_l1_based /
2818                                                        ps_cur_gop->i8_bits_allocated_to_gop) *
2819                                                       (f_per_frm_bits) *
2820                                                       (ps_cur_gop->i4_tot_frm_in_gop);
2821             }
2822             break;
2823         }
2824         else
2825         {
2826             /*Go for next iteration*/
2827             cur_peak_factor *= (float)i8_tot_bits_sequence / total_bits_allocated;
2828             //cur_peak_factor = MAX(cur_peak_factor,1);
2829             prev_total_bits_allocated = total_bits_allocated;
2830             i8_excess_bits = i8_tot_bits_sequence - total_bits_allocated;
2831         }
2832 
2833     } while(1);
2834     ps_bit_allocation->f_cur_peak_factor_2pass = cur_peak_factor;
2835     ps_bit_allocation->i8_total_bits_allocated = total_bits_allocated;
2836 
2837     /*Store complexity beyond which bits are clipped to peak rate*/
2838     /*if(i4_start_gop_number == 0)*/
2839     {
2840         ps_bit_allocation->f_min_complexity_cross_peak_rate = /*min_complexity_beyond_peak*/
2841             (float)ps_bit_allocation->ai4_peak_bit_rate[0] / i8_current_bitrate;
2842         //ba_get_min_complexity_for_peak_br(ps_bit_allocation->ai4_peak_bit_rate[0],ps_bit_allocation->i4_bit_rate,cur_peak_factor,f_max_complexity,f_min_complexity,ps_bit_allocation->i4_ba_rc_pass);
2843     }
2844 }
2845 
2846 /*****************************************************************************
2847   Function Name : get_prev_frame_total_header_bits
2848   Description   :
2849   Inputs        : ps_bit_allocation
2850                   e_pic_type
2851   Revision History:
2852          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2853 *****************************************************************************/
get_prev_frame_total_header_bits(bit_allocation_t * ps_bit_allocation,WORD32 * pi4_prev_frame_total_bits,WORD32 * pi4_prev_frame_header_bits,picture_type_e e_pic_type)2854 void get_prev_frame_total_header_bits(
2855     bit_allocation_t *ps_bit_allocation,
2856     WORD32 *pi4_prev_frame_total_bits,
2857     WORD32 *pi4_prev_frame_header_bits,
2858     picture_type_e e_pic_type)
2859 {
2860     *pi4_prev_frame_total_bits = ps_bit_allocation->ai4_prev_frm_tot_bits[e_pic_type];
2861     *pi4_prev_frame_header_bits = ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type];
2862 }
2863 
2864 /*****************************************************************************
2865   Function Name : bit_alloc_get_gop_num
2866   Description   :
2867   Inputs        : ps_bit_allocation
2868 
2869   Revision History:
2870          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2871 *****************************************************************************/
bit_alloc_get_gop_num(bit_allocation_t * ps_bit_allocation)2872 LWORD64 bit_alloc_get_gop_num(bit_allocation_t *ps_bit_allocation)
2873 {
2874     return (ps_bit_allocation->i8_cur_gop_num);
2875 }
2876 /*****************************************************************************
2877   Function Name : ba_get_min_bits_per_frame
2878   Description   :
2879   Inputs        : ps_bit_allocation
2880 
2881   Revision History:
2882          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2883 *****************************************************************************/
ba_get_min_bits_per_frame(bit_allocation_t * ps_bit_allocation)2884 WORD32 ba_get_min_bits_per_frame(bit_allocation_t *ps_bit_allocation)
2885 {
2886     return (ps_bit_allocation->i4_min_bits_per_frm);
2887 }
2888 /*****************************************************************************
2889   Function Name : set_bit_allocation_i_frames
2890   Description   :
2891   Inputs        : ps_bit_allocation
2892 
2893   Revision History:
2894          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2895 *****************************************************************************/
set_bit_allocation_i_frames(bit_allocation_t * ps_bit_allocation,cbr_buffer_handle ps_cbr_buffer,pic_handling_handle ps_pic_handle,WORD32 i4_lap_window_comp,WORD32 i4_num_frames)2896 void set_bit_allocation_i_frames(
2897     bit_allocation_t *ps_bit_allocation,
2898     cbr_buffer_handle ps_cbr_buffer,
2899     pic_handling_handle ps_pic_handle,
2900     WORD32 i4_lap_window_comp,
2901     WORD32 i4_num_frames)
2902 {
2903     LWORD64 vbv_buffer_based_excess = 0;
2904     WORD32 i4_gop_correction;
2905     WORD32 i4_lap_window_comp_temp = i4_lap_window_comp;
2906     rc_type_e e_rc_type = get_rc_type(ps_cbr_buffer);
2907     if(e_rc_type == VBR_STREAMING)
2908     {
2909         if(((float)i4_lap_window_comp / 128) > ps_bit_allocation->f_min_complexity_cross_peak_rate)
2910             i4_lap_window_comp_temp =
2911                 (WORD32)(ps_bit_allocation->f_min_complexity_cross_peak_rate * 128);
2912 
2913         /*Get excess bits if any from vbv buffer*/
2914         vbv_buffer_based_excess = get_vbv_buffer_based_excess(
2915             ps_cbr_buffer,
2916             ps_bit_allocation->f_min_complexity_cross_peak_rate,
2917             ((float)i4_lap_window_comp / 128),
2918             i4_num_frames,
2919             1);
2920     }
2921     i4_gop_correction =
2922         get_error_bits_for_desired_buf(ps_cbr_buffer, i4_lap_window_comp_temp, i4_num_frames);
2923 
2924     update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handle, 0);
2925 
2926     set_rbip(&ps_bit_allocation->s_rbip, (i4_gop_correction + (WORD32)vbv_buffer_based_excess));
2927 
2928     update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handle, 0);
2929 }
2930 
2931 /*****************************************************************************
2932   Function Name : bit_alloc_set_curr_i_to_sum_i
2933   Description   :
2934   Inputs        : ps_bit_allocation
2935 
2936   Revision History:
2937          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2938 *****************************************************************************/
bit_alloc_set_curr_i_to_sum_i(bit_allocation_t * ps_bit_allocation,float f_curr_i_to_sum)2939 void bit_alloc_set_curr_i_to_sum_i(bit_allocation_t *ps_bit_allocation, float f_curr_i_to_sum)
2940 {
2941     ps_bit_allocation->f_curr_i_to_sum = f_curr_i_to_sum;
2942 }
2943 
2944 /*****************************************************************************
2945   Function Name : ba_set_gop_stat_in_bit_alloc
2946   Description   :
2947   Inputs        : ps_bit_allocation
2948 
2949   Revision History:
2950          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2951 *****************************************************************************/
ba_set_gop_stat_in_bit_alloc(bit_allocation_t * ps_bit_allocation,void * pv_gop_stat_summary)2952 void ba_set_gop_stat_in_bit_alloc(bit_allocation_t *ps_bit_allocation, void *pv_gop_stat_summary)
2953 {
2954     ps_bit_allocation->pv_gop_stat = pv_gop_stat_summary;
2955 }
2956 /*****************************************************************************
2957   Function Name : ba_get_luma_pels
2958   Description   :
2959   Inputs        : ps_bit_allocation
2960 
2961   Revision History:
2962          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2963 *****************************************************************************/
ba_get_luma_pels(bit_allocation_t * ps_bit_allocation)2964 WORD32 ba_get_luma_pels(bit_allocation_t *ps_bit_allocation)
2965 {
2966     return (ps_bit_allocation->i4_luma_pels);
2967 }
2968 /*****************************************************************************
2969   Function Name : overflow_avoided_summation
2970   Description   :
2971   Inputs        : ps_bit_allocation
2972 
2973   Revision History:
2974          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2975 *****************************************************************************/
overflow_avoided_summation(WORD32 * pi4_accumulator,WORD32 i4_input)2976 void overflow_avoided_summation(WORD32 *pi4_accumulator, WORD32 i4_input)
2977 {
2978     if((pi4_accumulator[0] > 0) && (((int)0x7fffffff - pi4_accumulator[0]) < i4_input))
2979         pi4_accumulator[0] = 0x7fffffff;
2980     else if((pi4_accumulator[0] < 0) && (((int)0x80000000 - pi4_accumulator[0]) > i4_input))
2981         pi4_accumulator[0] = 0x80000000;
2982     else
2983         pi4_accumulator[0] += i4_input;
2984 }
2985 /*****************************************************************************
2986   Function Name : ba_get_sum_complexity_segment_cross_peak
2987   Description   :
2988   Inputs        : ps_bit_allocation
2989 
2990   Revision History:
2991          DD MM YYYY   Author(s)       Changes (Describe the changes made)
2992 *****************************************************************************/
ba_get_sum_complexity_segment_cross_peak(bit_allocation_t * ps_bit_allocation)2993 float ba_get_sum_complexity_segment_cross_peak(bit_allocation_t *ps_bit_allocation)
2994 {
2995     return (ps_bit_allocation->f_sum_complexity_segment_cross_peak);
2996 }
2997 /*****************************************************************************
2998   Function Name : ba_get_prev_frame_tot_est_bits
2999   Description   :
3000   Inputs        : ps_bit_allocation
3001 
3002   Revision History:
3003          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3004 *****************************************************************************/
ba_get_prev_frame_tot_est_bits(bit_allocation_t * ps_bit_allocation,WORD32 i4_pic)3005 WORD32 ba_get_prev_frame_tot_est_bits(bit_allocation_t *ps_bit_allocation, WORD32 i4_pic)
3006 {
3007     return (ps_bit_allocation->ai4_prev_frm_tot_est_bits[i4_pic]);
3008 }
3009 /*****************************************************************************
3010   Function Name : ba_get_prev_frame_tot_bits
3011   Description   :
3012   Inputs        : ps_bit_allocation
3013 
3014   Revision History:
3015          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3016 *****************************************************************************/
ba_get_prev_frame_tot_bits(bit_allocation_t * ps_bit_allocation,WORD32 i4_pic)3017 WORD32 ba_get_prev_frame_tot_bits(bit_allocation_t *ps_bit_allocation, WORD32 i4_pic)
3018 {
3019     return (ps_bit_allocation->ai4_prev_frm_tot_bits[i4_pic]);
3020 }
3021 /*****************************************************************************
3022   Function Name : ba_gop_info_average_qscale_gop_without_offset
3023   Description   :
3024   Inputs        : ps_bit_allocation
3025 
3026   Revision History:
3027          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3028 *****************************************************************************/
ba_gop_info_average_qscale_gop_without_offset(bit_allocation_t * ps_bit_allocation)3029 float ba_gop_info_average_qscale_gop_without_offset(bit_allocation_t *ps_bit_allocation)
3030 {
3031     gop_level_stat_t *ps_gop_level_stat =
3032         (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
3033 
3034     return (ps_gop_level_stat->f_hbd_avg_q_scale_gop_without_offset);
3035 }
3036 /*****************************************************************************
3037   Function Name : ba_get_min_complexity_for_peak_br
3038   Description   : compute min complexity above which peak rate needs to be given
3039   Inputs        : i4_peak_bit_rate
3040 
3041   Revision History:
3042          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3043 *****************************************************************************/
ba_get_min_complexity_for_peak_br(WORD32 i4_peak_bit_rate,WORD32 i4_bit_rate,float f_peak_rate_factor,float f_max_val,float f_min_val,WORD32 i4_pass)3044 float ba_get_min_complexity_for_peak_br(
3045     WORD32 i4_peak_bit_rate,
3046     WORD32 i4_bit_rate,
3047     float f_peak_rate_factor,
3048     float f_max_val,
3049     float f_min_val,
3050     WORD32 i4_pass)
3051 {
3052     float f_target_bits_ratio = (float)i4_peak_bit_rate / i4_bit_rate;
3053     float f_at_min_val;
3054     float f_at_max_val;
3055     float f_avg_val, f_at_avg_val;
3056     WORD32 i4_iter = 0, i4_max_iter = 25;
3057 
3058     f_avg_val = (f_max_val + f_min_val) / 2;
3059     /*i4_target_bits_ratio = (-1.7561*(X*X*X*X) + ( 2.5547 * X * X * X) - 0.3408 * (X * X) + (0.5343 * X) - 0.003) * 10;*/
3060     if(i4_pass != 2)
3061     {
3062         f_at_min_val = COMP_TO_BITS_MAP(f_min_val, f_peak_rate_factor);
3063         f_at_max_val = COMP_TO_BITS_MAP(f_max_val, f_peak_rate_factor);
3064         f_at_avg_val = COMP_TO_BITS_MAP(f_avg_val, f_peak_rate_factor);
3065     }
3066     else
3067     {
3068         f_at_min_val = COMP_TO_BITS_MAP_2_PASS(f_min_val, f_peak_rate_factor);
3069         f_at_max_val = COMP_TO_BITS_MAP_2_PASS(f_max_val, f_peak_rate_factor);
3070         f_at_avg_val = COMP_TO_BITS_MAP_2_PASS(f_avg_val, f_peak_rate_factor);
3071     }
3072 
3073     do
3074     {
3075         if((f_at_min_val < f_target_bits_ratio) && (f_target_bits_ratio < f_at_avg_val))
3076         {
3077             f_max_val = f_avg_val;
3078         }
3079         else
3080         {
3081             f_min_val = f_avg_val;
3082         }
3083         f_avg_val = (f_max_val + f_min_val) / 2;
3084 
3085         /*i4_target_bits_ratio = (-1.7561*(X*X*X*X) + ( 2.5547 * X * X * X) - 0.3408 * (X * X) + (0.5343 * X) - 0.003) * 10;*/
3086         if(i4_pass != 2)
3087         {
3088             f_at_min_val = COMP_TO_BITS_MAP(f_min_val, f_peak_rate_factor);
3089             f_at_max_val = COMP_TO_BITS_MAP(f_max_val, f_peak_rate_factor);
3090             f_at_avg_val = COMP_TO_BITS_MAP(f_avg_val, f_peak_rate_factor);
3091         }
3092         else
3093         {
3094             f_at_min_val = COMP_TO_BITS_MAP_2_PASS(f_min_val, f_peak_rate_factor);
3095             f_at_max_val = COMP_TO_BITS_MAP_2_PASS(f_max_val, f_peak_rate_factor);
3096             f_at_avg_val = COMP_TO_BITS_MAP_2_PASS(f_avg_val, f_peak_rate_factor);
3097         }
3098 
3099         if(((fabs((float)(f_at_avg_val - f_target_bits_ratio))) <= .0001f) ||
3100            (i4_iter >= i4_max_iter))
3101         {
3102             break;
3103         }
3104         i4_iter++;
3105     } while(1);
3106 
3107     /*f_min_complexity_across_which pk br is given is unmapped value for 1 pass and mapped value for 2 pass*/
3108     if(i4_pass != 2)
3109         return (f_avg_val);
3110     else
3111         return (f_at_avg_val);
3112 }
3113 /*****************************************************************************
3114   Function Name : get_f_curr_by_sum_subgop
3115   Description   :
3116   Inputs        : ps_bit_allocation
3117 
3118   Revision History:
3119          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3120 *****************************************************************************/
get_f_curr_by_sum_subgop(bit_allocation_t * ps_bit_allocation)3121 float get_f_curr_by_sum_subgop(bit_allocation_t *ps_bit_allocation)
3122 {
3123     return (ps_bit_allocation->f_curr_by_sum_subgop);
3124 }
3125 /*****************************************************************************
3126   Function Name : ba_get_frame_number_in_gop
3127   Description   :
3128   Inputs        : ps_bit_allocation
3129 
3130   Revision History:
3131          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3132 *****************************************************************************/
ba_get_frame_number_in_gop(bit_allocation_t * ps_bit_allocation)3133 WORD32 ba_get_frame_number_in_gop(bit_allocation_t *ps_bit_allocation)
3134 {
3135     return ((WORD32)(ps_bit_allocation->i8_frm_num_in_gop));
3136 }
3137 /*****************************************************************************
3138   Function Name : ba_get_qscale_max_clip_in_second_pass
3139   Description   :
3140   Inputs        : ps_bit_allocation
3141 
3142   Revision History:
3143          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3144 *****************************************************************************/
ba_get_qscale_max_clip_in_second_pass(bit_allocation_t * ps_bit_allocation)3145 float ba_get_qscale_max_clip_in_second_pass(bit_allocation_t *ps_bit_allocation)
3146 {
3147     return (ps_bit_allocation->f_qscale_max_clip_in_second_pass);
3148 }
3149 /*****************************************************************************
3150   Function Name : ba_set_avg_qscale_first_pass
3151   Description   :
3152   Inputs        : ps_bit_allocation
3153 
3154   Revision History:
3155          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3156 *****************************************************************************/
ba_set_avg_qscale_first_pass(bit_allocation_t * ps_bit_allocation,float f_average_qscale_1st_pass)3157 void ba_set_avg_qscale_first_pass(
3158     bit_allocation_t *ps_bit_allocation, float f_average_qscale_1st_pass)
3159 {
3160     ps_bit_allocation->f_average_qscale_1st_pass = f_average_qscale_1st_pass;
3161 }
3162 /*****************************************************************************
3163   Function Name : ba_set_max_avg_qscale_first_pass
3164   Description   :
3165   Inputs        : ps_bit_allocation
3166 
3167   Revision History:
3168          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3169 *****************************************************************************/
ba_set_max_avg_qscale_first_pass(bit_allocation_t * ps_bit_allocation,float f_average_qscale_1st_pass)3170 void ba_set_max_avg_qscale_first_pass(
3171     bit_allocation_t *ps_bit_allocation, float f_average_qscale_1st_pass)
3172 {
3173     ps_bit_allocation->f_max_average_qscale_1st_pass = f_average_qscale_1st_pass;
3174 }
3175 /*****************************************************************************
3176   Function Name : ba_get_avg_qscale_first_pass
3177   Description   :
3178   Inputs        : ps_bit_allocation
3179 
3180   Revision History:
3181          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3182 *****************************************************************************/
ba_get_avg_qscale_first_pass(bit_allocation_t * ps_bit_allocation)3183 float ba_get_avg_qscale_first_pass(bit_allocation_t *ps_bit_allocation)
3184 {
3185     return (ps_bit_allocation->f_average_qscale_1st_pass);
3186 }
3187 /*****************************************************************************
3188   Function Name : ba_get_max_avg_qscale_first_pass
3189   Description   :
3190   Inputs        : ps_bit_allocation
3191 
3192   Revision History:
3193          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3194 *****************************************************************************/
ba_get_max_avg_qscale_first_pass(bit_allocation_t * ps_bit_allocation)3195 float ba_get_max_avg_qscale_first_pass(bit_allocation_t *ps_bit_allocation)
3196 {
3197     return (ps_bit_allocation->f_max_average_qscale_1st_pass);
3198 }
3199 /*****************************************************************************
3200   Function Name : bit_alloc_set_2pass_total_frames
3201   Description   :
3202   Inputs        : ps_bit_allocation
3203 
3204   Revision History:
3205          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3206 *****************************************************************************/
bit_alloc_set_2pass_total_frames(bit_allocation_t * ps_bit_allocation,WORD32 i4_total_2pass_frames)3207 void bit_alloc_set_2pass_total_frames(
3208     bit_allocation_t *ps_bit_allocation, WORD32 i4_total_2pass_frames)
3209 {
3210     ps_bit_allocation->i4_total_2pass_frames = i4_total_2pass_frames;
3211 }
3212 /*****************************************************************************
3213   Function Name : ba_get_2pass_total_frames
3214   Description   :
3215   Inputs        : ps_bit_allocation
3216 
3217   Revision History:
3218          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3219 *****************************************************************************/
ba_get_2pass_total_frames(bit_allocation_t * ps_bit_allocation)3220 WORD32 ba_get_2pass_total_frames(bit_allocation_t *ps_bit_allocation)
3221 {
3222     return (ps_bit_allocation->i4_total_2pass_frames);
3223 }
3224 /*****************************************************************************
3225   Function Name : ba_set_enable_look_ahead
3226   Description   :
3227   Inputs        : ps_bit_allocation
3228 
3229   Revision History:
3230          DD MM YYYY   Author(s)       Changes (Describe the changes made)
3231 *****************************************************************************/
ba_set_enable_look_ahead(bit_allocation_t * ps_bit_allocation,WORD32 i4_fp_bit_alloc_in_sp)3232 void ba_set_enable_look_ahead(bit_allocation_t *ps_bit_allocation, WORD32 i4_fp_bit_alloc_in_sp)
3233 {
3234     ps_bit_allocation->i4_fp_bit_alloc_in_sp = i4_fp_bit_alloc_in_sp;
3235 }
3236