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