Coverage Report

Created: 2026-09-14 06:39

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