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