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