Coverage Report

Created: 2025-09-17 06:42

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
23.8k
#define MAX(x, y) ((x) < (y)) ? (y) : (x)
71
#define CLIP3RC(x, min, max) (((x) > (max)) ? (max) : (((x) < (min)) ? (min) : (x)))
72
73
424k
#define DEV_Q 4 /*Q format(Shift) for Deviation range factor */
74
212k
#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
146k
#define LO_DEV_FCTR_1B 14  //8   /* 0.75*16 */
77
//#define LO_DEV_FCTR_7B     10//8   /* 0.75*16 */
78
65.4k
#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
212k
#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
178k
    ((i4_num_active_pic_types <= B1_PIC)                                                           \
93
178k
         ? ((((WORD32)Qprev) * LO_DEV_FCTR_1B) >> DEV_Q)                                           \
94
178k
         : ((i4_num_active_pic_types == B2_PIC) ? ((((WORD32)Qprev) * LO_DEV_FCTR_3B) >> DEV_Q)    \
95
31.8k
                                                : ((((WORD32)Qprev) * LO_DEV_FCTR_7B) >> DEV_Q)))
96
97
33.5k
#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
58.2k
#define P_TO_I_RATIO_Q_FACTOR (9)
102
773k
#define MULT_FACTOR_SATD (4)
103
#define GET_L0_SATD_BY_ACT_MAX_PER_PIXEL(i4_num_pixel)                                             \
104
386k
    ((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
386k
    (MULT_FACTOR_SATD * normal_satd_act + f_satd_by_Act_norm) /                                    \
107
386k
        (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
49.4k
{
146
49.4k
    WORD32 i4_mem_tab_idx = 0, i;
147
49.4k
    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
49.4k
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
152
42.3k
        (*pps_rate_control_api) = &s_temp_rc_api;
153
154
    /*for src rate control state structure*/
155
49.4k
    if(e_func_type != GET_NUM_MEMTAB)
156
21.1k
    {
157
21.1k
        fill_memtab(
158
21.1k
            &ps_memtab[i4_mem_tab_idx],
159
21.1k
            sizeof(rate_control_api_t),
160
21.1k
            MEM_TAB_ALIGNMENT,
161
21.1k
            PERSISTENT,
162
21.1k
            DDR);
163
21.1k
        use_or_fill_base(&ps_memtab[0], (void **)pps_rate_control_api, e_func_type);
164
21.1k
    }
165
49.4k
    i4_mem_tab_idx++;
166
167
    /* Get the memory requirement of lower modules */
168
49.4k
    i4_mem_tab_idx += bit_allocation_num_fill_use_free_memtab(
169
49.4k
        &pps_rate_control_api[0]->ps_bit_allocation, &ps_memtab[i4_mem_tab_idx], e_func_type);
170
171
49.4k
    i4_mem_tab_idx += cbr_buffer_num_fill_use_free_memtab(
172
49.4k
        &pps_rate_control_api[0]->ps_cbr_buffer, &ps_memtab[i4_mem_tab_idx], e_func_type);
173
174
49.4k
    i4_mem_tab_idx += est_sad_num_fill_use_free_memtab(
175
49.4k
        &pps_rate_control_api[0]->ps_est_sad, &ps_memtab[i4_mem_tab_idx], e_func_type);
176
177
49.4k
    i4_mem_tab_idx += mbrc_num_fill_use_free_memtab(
178
49.4k
        &pps_rate_control_api[0]->ps_mb_rate_control, &ps_memtab[i4_mem_tab_idx], e_func_type);
179
180
49.4k
    i4_mem_tab_idx += vbr_vbv_num_fill_use_free_memtab(
181
49.4k
        &pps_rate_control_api[0]->ps_vbr_storage_vbv, &ps_memtab[i4_mem_tab_idx], e_func_type);
182
183
49.4k
    i4_mem_tab_idx += init_qp_num_fill_use_free_memtab(
184
49.4k
        &pps_rate_control_api[0]->ps_init_qp, &ps_memtab[i4_mem_tab_idx], e_func_type);
185
186
49.4k
    i4_mem_tab_idx += sad_acc_num_fill_use_free_memtab(
187
49.4k
        &pps_rate_control_api[0]->ps_sad_acc, &ps_memtab[i4_mem_tab_idx], e_func_type);
188
189
494k
    for(i = 0; i < MAX_PIC_TYPE; i++)
190
444k
    {
191
444k
        i4_mem_tab_idx += rc_rd_model_num_fill_use_free_memtab(
192
444k
            &pps_rate_control_api[0]->aps_rd_model[i], &ps_memtab[i4_mem_tab_idx], e_func_type);
193
444k
    }
194
49.4k
    i4_mem_tab_idx += pic_handling_num_fill_use_free_memtab(
195
49.4k
        &pps_rate_control_api[0]->ps_pic_handling, &ps_memtab[i4_mem_tab_idx], e_func_type);
196
49.4k
    return (i4_mem_tab_idx);
197
49.4k
}
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
7.06k
{
263
7.06k
    WORD32 i, i4_temp;
264
7.06k
    UWORD32 u4_frms_in_delay_prd = (u4_frame_rate * u4_max_delay) / 1000000;
265
7.06k
    UWORD32 i4_cbr_bit_alloc_period;
266
7.06k
    float f_bit_depth_based_max_qp;
267
7.06k
    UWORD32 u4_bit_depth_based_max_qp;
268
7.06k
    WORD32 i4_pels_in_frame = (3 * (i4_frame_height * i4_frame_width) >> 1);
269
270
7.06k
    if(u4_intra_frame_interval ==
271
7.06k
       1) /*i_only: Set bit allocation period to 15( currently not configurable) for i only mode*/
272
525
    {
273
525
        i4_cbr_bit_alloc_period = u4_frame_rate / 1000; /*changed */
274
525
    }
275
6.53k
    else
276
6.53k
    {
277
6.53k
        i4_cbr_bit_alloc_period = 1;
278
6.53k
    }
279
280
7.06k
    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
7.06k
    else
286
7.06k
    {
287
7.06k
        ps_rate_control_api->i4_is_hbr = 0;
288
7.06k
    }
289
7.06k
    ps_rate_control_api->e_rc_type = e_rate_control_type;
290
7.06k
    ps_rate_control_api->i4_capped_vbr_flag = i4_capped_vbr_flag;
291
7.06k
    ps_rate_control_api->u1_is_mb_level_rc_on = u1_is_mb_level_rc_on;
292
7.06k
    ps_rate_control_api->i4_num_active_pic_type = i4_num_active_pic_type;
293
7.06k
    ps_rate_control_api->i4_quality_preset = i4_quality_preset;
294
7.06k
    ps_rate_control_api->i4_scd_I_frame_estimated_tot_bits = 0;
295
7.06k
    ps_rate_control_api->i4_I_frame_qp_model = 0;
296
7.06k
    ps_rate_control_api->u4_min_scd_hevc_qp = u4_min_scd_hevc_qp;
297
7.06k
    ps_rate_control_api->pf_rc_stat_file = pf_rc_stat_file;
298
7.06k
    ps_rate_control_api->i4_rc_pass = i4_pass_num;
299
7.06k
    ps_rate_control_api->i4_max_frame_height = i4_frame_height;
300
7.06k
    ps_rate_control_api->i4_max_frame_width = i4_frame_width;
301
7.06k
    ps_rate_control_api->i4_underflow_warning = 0;
302
7.06k
    ps_rate_control_api->f_p_to_i_comp_ratio = 1.0f;
303
7.06k
    ps_rate_control_api->i4_scd_in_period_2_pass = 0;
304
7.06k
    ps_rate_control_api->i4_is_infinite_gop = i4_is_infinite_gop;
305
7.06k
    ps_rate_control_api->i4_frames_since_last_scd = 0;
306
7.06k
    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
7.06k
    if(i4_pass_num == 2)
310
0
    {
311
0
        ps_rate_control_api->pv_2pass_gop_summary = pv_gop_stat;
312
0
    }
313
7.06k
    else
314
7.06k
    {
315
7.06k
        ps_rate_control_api->pv_2pass_gop_summary = NULL;
316
7.06k
    }
317
    /*Initialize the call back funcitons for file related operations*/
318
7.06k
    ps_rate_control_api->pv_rc_sys_api = pv_sys_rc_api;
319
320
7.06k
    ps_rate_control_api->u1_bit_depth = u1_bit_depth;
321
322
7.06k
    f_bit_depth_based_max_qp = (float)((51 + (6 * (u1_bit_depth - 8))) - 4) / 6;
323
7.06k
    u4_bit_depth_based_max_qp = (UWORD32)pow(2.0f, f_bit_depth_based_max_qp);
324
325
7.06k
    ps_rate_control_api->u4_bit_depth_based_max_qp = u4_bit_depth_based_max_qp;
326
327
7.06k
    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
70.6k
    for(i = 0; i < MAX_PIC_TYPE; i++)
331
63.5k
    {
332
63.5k
        ps_rate_control_api->au1_avg_bitrate_changed[i] = 0;
333
63.5k
    }
334
335
    /* Initialize the pic_handling module */
336
7.06k
    init_pic_handling(
337
7.06k
        ps_rate_control_api->ps_pic_handling, /*ps_pic_handling*/
338
7.06k
        (WORD32)u4_intra_frame_interval, /*i4_intra_frm_int,*/
339
7.06k
        i4_max_inter_frm_int, /*i4_max_inter_frm_int,*/
340
7.06k
        i4_is_gop_closed,
341
7.06k
        (WORD32)u4_idr_period,
342
7.06k
        ps_rate_control_api->i4_num_active_pic_type,
343
7.06k
        i4_field_pic); /*gop_struct_e*/
344
345
    /* initialise the init Qp module */
346
7.06k
    init_init_qp(
347
7.06k
        ps_rate_control_api->ps_init_qp,
348
7.06k
        pi4_min_max_qp,
349
7.06k
        i4_pels_in_frame,
350
7.06k
        ps_rate_control_api->i4_is_hbr);
351
352
    /*** Initialize the rate control modules  ***/
353
7.06k
    if(ps_rate_control_api->e_rc_type != CONST_QP)
354
4.03k
    {
355
4.03k
        UWORD32 au4_num_pics_in_delay_prd[MAX_PIC_TYPE] = { 0 };
356
357
        /* Initialise the model parameter structures */
358
40.3k
        for(i = 0; i < MAX_PIC_TYPE; i++)
359
36.3k
        {
360
36.3k
            init_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[i], MAX_FRAMES_MODELLED);
361
36.3k
        }
362
363
        /* Initialize the buffer mechanism */
364
4.03k
        if((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
365
4.03k
           (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
4.03k
        else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
381
1.50k
        {
382
1.50k
            UWORD32 u4_avg_bit_rate_copy[MAX_NUM_DRAIN_RATES];
383
4.50k
            for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
384
3.00k
            {
385
3.00k
                u4_avg_bit_rate_copy[i] = u4_avg_bit_rate;
386
3.00k
            }
387
388
1.50k
            init_cbr_buffer(
389
1.50k
                ps_rate_control_api->ps_cbr_buffer,
390
1.50k
                u4_max_delay,
391
1.50k
                u4_frame_rate,
392
1.50k
                u4_avg_bit_rate,
393
1.50k
                au4_num_pics_in_delay_prd,
394
1.50k
                u4_max_vbv_buff_size,
395
1.50k
                u4_intra_frame_interval,
396
1.50k
                ps_rate_control_api->e_rc_type,
397
1.50k
                pu4_peak_bit_rate[0],
398
1.50k
                i4_initial_decoder_delay_frames,
399
1.50k
                f_max_peak_rate_sustain_dur,
400
1.50k
                i8_num_frames_to_encode,
401
1.50k
                i4_max_inter_frm_int,
402
1.50k
                i4_pass_num,
403
1.50k
                0 /*capped vbr off */);
404
1.50k
        }
405
2.53k
        else if(ps_rate_control_api->e_rc_type == VBR_STREAMING)
406
2.53k
        {
407
2.53k
            init_vbv_str_prms(
408
2.53k
                &ps_rate_control_api->s_vbr_str_prms,
409
2.53k
                u4_intra_frame_interval,
410
2.53k
                u4_src_ticks,
411
2.53k
                u4_tgt_ticks,
412
2.53k
                u4_frms_in_delay_prd);
413
414
2.53k
            init_cbr_buffer(
415
2.53k
                ps_rate_control_api->ps_cbr_buffer,
416
2.53k
                u4_max_delay,
417
2.53k
                u4_frame_rate,
418
2.53k
                u4_avg_bit_rate,
419
2.53k
                au4_num_pics_in_delay_prd,
420
2.53k
                u4_max_vbv_buff_size,
421
2.53k
                u4_intra_frame_interval,
422
2.53k
                ps_rate_control_api->e_rc_type,
423
2.53k
                pu4_peak_bit_rate[0],
424
2.53k
                i4_initial_decoder_delay_frames,
425
2.53k
                f_max_peak_rate_sustain_dur,
426
2.53k
                i8_num_frames_to_encode,
427
2.53k
                i4_max_inter_frm_int,
428
2.53k
                i4_pass_num,
429
2.53k
                ps_rate_control_api->i4_capped_vbr_flag);
430
2.53k
        }
431
432
        /* Initalise the SAD estimation module */
433
4.03k
        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
4.03k
        if((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
437
4.03k
           (ps_rate_control_api->e_rc_type == VBR_STREAMING) ||
438
1.50k
           (ps_rate_control_api->e_rc_type == VBR_STORAGE_DVD_COMP))
439
2.53k
        {
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
2.53k
            init_bit_allocation(
443
2.53k
                ps_rate_control_api->ps_bit_allocation,
444
2.53k
                ps_rate_control_api->ps_pic_handling,
445
2.53k
                i4_cbr_bit_alloc_period,
446
2.53k
                u4_avg_bit_rate /*u4_scaled_avg_bit_rate*/,
447
2.53k
                u4_frame_rate,
448
2.53k
                (WORD32 *)pu4_peak_bit_rate,
449
2.53k
                u4_min_bit_rate,
450
2.53k
                i4_pels_in_frame,
451
2.53k
                ps_rate_control_api->i4_is_hbr,
452
2.53k
                ps_rate_control_api->i4_num_active_pic_type,
453
2.53k
                i4_lap_window,
454
2.53k
                i4_field_pic,
455
2.53k
                i4_pass_num,
456
2.53k
                (i4_frame_height * i4_frame_width),
457
2.53k
                i4_fp_bit_alloc_in_sp);
458
2.53k
        }
459
1.50k
        else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
460
1.50k
        {
461
1.50k
            init_bit_allocation(
462
1.50k
                ps_rate_control_api->ps_bit_allocation,
463
1.50k
                ps_rate_control_api->ps_pic_handling,
464
1.50k
                i4_cbr_bit_alloc_period,  //i_onlyCBR_BIT_ALLOC_PERIOD,
465
1.50k
                u4_avg_bit_rate,
466
1.50k
                u4_frame_rate,
467
1.50k
                (WORD32 *)pu4_peak_bit_rate,
468
1.50k
                u4_min_bit_rate,
469
1.50k
                i4_pels_in_frame,
470
1.50k
                ps_rate_control_api->i4_is_hbr,
471
1.50k
                ps_rate_control_api->i4_num_active_pic_type,
472
1.50k
                i4_lap_window,
473
1.50k
                i4_field_pic,
474
1.50k
                i4_pass_num,
475
1.50k
                (i4_frame_height * i4_frame_width),
476
1.50k
                i4_fp_bit_alloc_in_sp);
477
1.50k
        }
478
4.03k
    }
479
3.02k
    else
480
3.02k
    {
481
3.02k
        UWORD32 au4_num_pics_in_delay_prd[MAX_PIC_TYPE];
482
483
30.2k
        for(i = 0; i < MAX_PIC_TYPE; i++)
484
27.2k
            au4_num_pics_in_delay_prd[i] = 0;
485
486
3.02k
        init_cbr_buffer(
487
3.02k
            ps_rate_control_api->ps_cbr_buffer,
488
3.02k
            u4_max_delay,
489
3.02k
            u4_frame_rate,
490
3.02k
            u4_avg_bit_rate,
491
3.02k
            au4_num_pics_in_delay_prd,
492
3.02k
            u4_max_vbv_buff_size,
493
3.02k
            u4_intra_frame_interval,
494
3.02k
            ps_rate_control_api->e_rc_type,
495
3.02k
            pu4_peak_bit_rate[0],
496
3.02k
            i4_initial_decoder_delay_frames,
497
3.02k
            f_max_peak_rate_sustain_dur,
498
3.02k
            i8_num_frames_to_encode,
499
3.02k
            i4_max_inter_frm_int,
500
3.02k
            i4_pass_num,
501
3.02k
            0 /*capped vbr off */);
502
3.02k
    }
503
504
    /* Initialize the init_qp */
505
218k
    for(i4_temp = 0; i4_temp < MAX_SCENE_NUM_RC; i4_temp++)
506
211k
    {
507
2.11M
        for(i = 0; i < MAX_PIC_TYPE; i++)
508
1.90M
        {
509
1.90M
            ps_rate_control_api->ai4_prev_frm_qp[i4_temp][i] = 0x7FFFFFFF;  //pi4_init_qp[i];
510
1.90M
            ps_rate_control_api->ai4_prev_frm_qp_q6[i4_temp][i] =
511
1.90M
                0x7FFFFFFF;  //pi4_init_qp[i] << QSCALE_Q_FAC;
512
1.90M
            ps_rate_control_api->ai4_min_qp[i] = pi4_min_max_qp[(i << 1)];
513
1.90M
            ps_rate_control_api->ai4_max_qp[i] = pi4_min_max_qp[(i << 1) + 1];
514
1.90M
        }
515
211k
    }
516
    /*init min and max qp in qscale*/
517
70.6k
    for(i = 0; i < MAX_PIC_TYPE; i++)
518
63.5k
    {
519
63.5k
        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
63.5k
        ps_rate_control_api->ai4_max_qp_q6[i] = (u4_bit_depth_based_max_qp << QSCALE_Q_FAC);
522
63.5k
    }
523
524
    /* Initialize the is_first_frm_encoded */
525
70.6k
    for(i = 0; i < MAX_PIC_TYPE; i++)
526
63.5k
    {
527
63.5k
        ps_rate_control_api->au1_is_first_frm_coded[i] = 0;
528
63.5k
    }
529
7.06k
    ps_rate_control_api->u1_is_first_frm = 1;
530
7.06k
    ps_rate_control_api->i4_prev_ref_is_scd = 0;
531
532
63.5k
    for(i = 0; i < MAX_NUM_FRAME_PARALLEL; i++)
533
56.5k
    {
534
56.5k
        ps_rate_control_api->ai4_est_tot_bits[i] =
535
56.5k
            get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
536
56.5k
    }
537
538
    /* Control flag for delayed impact after a change in peak bitrate has been made */
539
7.06k
    ps_rate_control_api->u4_frms_in_delay_prd_for_peak_bit_rate_change = 0;
540
21.1k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
541
14.1k
    {
542
14.1k
        ps_rate_control_api->au4_new_peak_bit_rate[i] = pu4_peak_bit_rate[i];
543
14.1k
    }
544
545
    /* initialise the mb level rate control module */
546
7.06k
    init_mb_level_rc(ps_rate_control_api->ps_mb_rate_control);
547
7.06k
    ps_rate_control_api->i4_prev_frm_est_bits = u4_avg_bit_rate / (u4_frame_rate / 1000);
548
549
7.06k
    ps_rate_control_api->prev_ref_pic_type = I_PIC;
550
7.06k
    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
7.06k
    init_sad_acc(ps_rate_control_api->ps_sad_acc);
554
555
7.06k
    rc_get_max_hme_sad_per_pixel(ps_rate_control_api, i4_frame_height * i4_frame_width);
556
7.06k
}
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
386k
{
650
386k
    WORD32 i4_min_bits = 0, i4_max_bits = 0;
651
652
386k
    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
386k
    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
386k
    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
386k
    else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
673
133k
    {
674
133k
        cbr_buffer_constraint_check(
675
133k
            ps_rate_control_api->ps_cbr_buffer, 0, e_pic_type, &i4_max_bits, &i4_min_bits);
676
133k
    }
677
253k
    else /* if(ps_rate_control_api->e_rc_type == VBR_STREAMING) */
678
253k
    {
679
253k
        vbr_stream_buffer_constraint_check(
680
253k
            ps_rate_control_api->ps_cbr_buffer, 0, e_pic_type, &i4_max_bits, &i4_min_bits);
681
253k
    }
682
    /* Fill the min and max bits consumed */
683
386k
    if(1 != ps_rate_control_api->i4_capped_vbr_flag)
684
386k
    {
685
386k
        pi4_min_bits[0] = i4_min_bits;
686
386k
    }
687
0
    else
688
0
    {
689
        /* Capped VBR case */
690
0
        pi4_min_bits[0] = 0;
691
0
    }
692
386k
    pi4_max_bits[0] = i4_max_bits;
693
386k
}
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
886k
{
705
886k
    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
886k
    if(pic_type_get_intra_frame_interval(ps_rate_control_api->ps_pic_handling) == 1)
709
154k
    {
710
154k
        i4_is_first_frame_coded = ps_rate_control_api->au1_is_first_frm_coded[I_PIC];
711
154k
    }
712
732k
    else /*HEVC_hierarchy*/
713
732k
    {
714
732k
        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
732k
        else
726
732k
        {
727
2.71M
            for(i = 0; i < ps_rate_control_api->i4_num_active_pic_type; i++)
728
1.98M
            {
729
1.98M
                i4_is_first_frame_coded &= ps_rate_control_api->au1_is_first_frm_coded[i];
730
1.98M
            }
731
732k
        }
732
732k
    }
733
734
886k
    return i4_is_first_frame_coded;
735
886k
}
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
212k
{
754
212k
    WORD32 prev_qp_q6, prev_I_qp_q6;
755
212k
    WORD32 hi_dev_qp_q6, lo_dev_qp_q6, hi_dev_qp_temp_q6;
756
212k
    WORD32 i4_intra_frm_int, prev_qp_for_high_dev_q6,
757
212k
        use_I_frame_qp_high_dev = 0; /*i_only : to detect i only case*/
758
212k
    float per_pixel_p_hme_sad =
759
212k
        (float)ps_rate_control_api->i8_per_pixel_p_frm_hme_sad_q10 / (1 << 10);
760
761
212k
    i4_intra_frm_int = pic_type_get_intra_frame_interval(ps_rate_control_api->ps_pic_handling);
762
763
    /* Restricting the Quant swing */
764
212k
    prev_qp_q6 = ps_rate_control_api
765
212k
                     ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type];
766
212k
    prev_qp_for_high_dev_q6 = prev_qp_q6;
767
212k
    prev_I_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC];
768
212k
    if(ps_rate_control_api->prev_ref_pic_type != e_pic_type)
769
98.5k
    {
770
98.5k
        if(e_pic_type == I_PIC)
771
51.1k
        {
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
51.1k
            prev_qp_q6 = (ps_rate_control_api->i4_P_to_I_ratio * (LWORD64)prev_qp_q6) >>
776
51.1k
                         P_TO_I_RATIO_Q_FACTOR;
777
51.1k
        }
778
47.4k
        else if(e_pic_type == P_PIC || e_pic_type == P1_PIC)
779
13.8k
        {
780
            /* Constrain P-frame QP to be within specified limit of Kp*prev_ref_qp */
781
13.8k
            prev_qp_q6 = (I_TO_P_RATIO * (LWORD64)prev_qp_q6) >> K_Q;
782
13.8k
            use_I_frame_qp_high_dev = 1;
783
13.8k
        }
784
33.5k
        else if(ps_rate_control_api->prev_ref_pic_type == P_PIC)
785
27.7k
        {
786
            /* current frame is B-pic */
787
            /* Constrain B-frame QP to be within specified limit of prev_ref_qp/Kb */
788
27.7k
            if(!ps_rate_control_api->i4_is_hbr)
789
27.7k
            {
790
27.7k
                prev_qp_q6 = (P_TO_B_RATIO * (LWORD64)prev_qp_q6) >> (K_Q);
791
27.7k
            }
792
0
            else
793
0
            {
794
0
                prev_qp_q6 = (P_TO_B_RATIO_HBR * (LWORD64)prev_qp_q6) >> (K_Q);
795
0
            }
796
27.7k
        }
797
5.84k
        else /* if(ps_rate_control_api->prev_ref_pic_type == I_PIC)  */
798
5.84k
        {
799
            /* current frame is B-pic */
800
            /* Constrain B-frame QP to be within specified limit of prev_ref_qp/Kb */
801
5.84k
            if(!ps_rate_control_api->i4_is_hbr)
802
5.84k
            {
803
5.84k
                prev_qp_q6 = (P_TO_B_RATIO * I_TO_P_RATIO * (LWORD64)prev_qp_q6) >> (K_Q + K_Q);
804
5.84k
            }
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
5.84k
        }
810
98.5k
    }
811
812
    /*if (1)//e_pic_type != B_PIC)*/
813
212k
    {
814
212k
        if(use_I_frame_qp_high_dev)
815
13.8k
        {
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
13.8k
            hi_dev_qp_q6 = GET_HI_DEV_QP_QFAC(prev_qp_for_high_dev_q6);
818
13.8k
        }
819
198k
        else
820
198k
        {
821
198k
            hi_dev_qp_q6 = GET_HI_DEV_QP_QFAC(prev_qp_q6);
822
198k
        }
823
824
212k
        if(e_pic_type == I_PIC || e_pic_type == P_PIC || e_pic_type == P1_PIC)
825
178k
        {
826
178k
            lo_dev_qp_q6 =
827
178k
                GET_LO_DEV_QP_QFAC(prev_qp_q6, ps_rate_control_api->i4_num_active_pic_type);
828
178k
        }
829
33.5k
        else
830
33.5k
        {
831
33.5k
            lo_dev_qp_q6 = GET_LO_DEV_QP_QFAC_B_PIC(prev_qp_q6);
832
33.5k
        }
833
212k
    }
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
212k
    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
212k
    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
212k
    if((per_pixel_p_hme_sad < 0.25f) && (ps_rate_control_api->i4_is_infinite_gop != 1))
850
181k
    {
851
181k
        if(e_pic_type == P_PIC && ps_rate_control_api->i4_I_frame_qp_model)
852
47.3k
        {
853
            /*P is not allowed to get too better compared to previous I in static content*/
854
47.3k
            if(lo_dev_qp_q6<(prev_I_qp_q6 * 14)>> 4)
855
8.78k
                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
47.3k
            if(lo_dev_qp_q6 < prev_I_qp_q6)
858
46.7k
                lo_dev_qp_q6 = prev_I_qp_q6;
859
47.3k
        }
860
181k
    }
861
212k
    if(e_pic_type == I_PIC &&
862
91.7k
       i4_intra_frm_int !=
863
91.7k
           1) /*i_only: In this case P frame Qp will be arbitrary value hence avoiding max_dev_qp to be independent of it*/
864
62.3k
    {
865
        //WORD32 i4_p_qp = ps_rate_control_api->ai4_prev_frm_qp[P_PIC];
866
62.3k
        WORD32 i4_p_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][P_PIC];
867
62.3k
        switch(i4_complexity_bin)
868
62.3k
        {
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
62.3k
        default:
885
62.3k
            hi_dev_qp_temp_q6 = (WORD32)(((LWORD64)i4_p_qp_q6 * P_TO_I_RATIO) >> (K_Q));
886
62.3k
            break;
887
62.3k
        }
888
62.3k
        hi_dev_qp_q6 = (hi_dev_qp_q6 > hi_dev_qp_temp_q6) ? hi_dev_qp_temp_q6 : hi_dev_qp_q6;
889
62.3k
    }
890
212k
    pi4_hi_dev_qp_q6[0] = hi_dev_qp_q6;
891
212k
    pi4_lo_dev_qp_q6[0] = lo_dev_qp_q6;
892
212k
}
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
222k
{
904
222k
    WORD32 min = a;
905
222k
    if(b < min)
906
34.6k
        min = b;
907
222k
    if(c < min)
908
0
        min = c;
909
222k
    if(d < min)
910
152k
        min = d;
911
222k
    return min;
912
222k
}
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
222k
{
924
222k
    WORD32 max = a;
925
222k
    if(b > max)
926
8.13k
        max = b;
927
222k
    if(c > max)
928
33.4k
        max = c;
929
222k
    return max;
930
222k
}
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
76.2k
{
946
76.2k
    WORD32 i4_num_frm_parallel, i;
947
76.2k
    i4_num_frm_parallel = ps_rate_control_api->i4_num_frame_parallel;
948
949
76.2k
    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
76.2k
}
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
375k
{
974
375k
    WORD32 i4_error_bits = 0, i, i4_bits_per_frame;
975
375k
    i4_bits_per_frame = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
976
375k
    if(ps_rate_control_api->i4_num_frame_parallel >
977
375k
       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
375k
    return i4_error_bits;
985
375k
}
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
104k
{
997
104k
    return (get_cur_frm_est_header_bits(ps_rate_control_api->ps_bit_allocation, e_pic_type));
998
104k
}
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
76.2k
{
1010
76.2k
    return (is_model_valid(ps_rate_control_api->aps_rd_model[e_pic_type]));
1011
76.2k
}
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
5.80k
{
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
5.80k
    WORD32 i4_frame_qp, i4_frame_qp_q6 = 0, i4_min_Kp_Kb_factor = 0;
1032
5.80k
    WORD32 Kp_kb_factor = get_Kp_Kb(ps_rate_control_api->ps_bit_allocation, e_pic_type);
1033
5.80k
    WORD32 kp_kb_ref_ref =
1034
5.80k
        get_Kp_Kb(ps_rate_control_api->ps_bit_allocation, ps_rate_control_api->prev_ref_pic_type);
1035
1036
5.80k
    {
1037
5.80k
        WORD32 i4_drain_bits_per_frame = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer),
1038
5.80k
               i4_ebf;
1039
5.80k
        WORD32 i4_delay = cbr_get_delay_frames(ps_rate_control_api->ps_cbr_buffer),
1040
5.80k
               max_buffer_level = 0, rc_type = get_rc_type(ps_rate_control_api->ps_cbr_buffer);
1041
1042
5.80k
        if(rc_type == VBR_STREAMING)
1043
3.61k
            max_buffer_level = i4_drain_bits_per_frame * i4_delay;
1044
2.19k
        else
1045
2.19k
            max_buffer_level = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
1046
1047
5.80k
        i4_ebf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer);
1048
1049
5.80k
        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
5.80k
    }
1071
5.80k
    if((e_pic_type == I_PIC) &&
1072
176
       (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
5.80k
    else if(
1080
5.80k
        (e_pic_type == I_PIC) &&
1081
176
        (ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] != 0x7FFFFFFF))
1082
176
    {
1083
176
        i4_frame_qp = ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC];
1084
176
        i4_frame_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC];
1085
176
    }
1086
5.62k
    else /*! ISlice*/
1087
5.62k
    {
1088
5.62k
        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
5.62k
        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
5.62k
        if(ps_rate_control_api
1099
5.62k
               ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] ==
1100
5.62k
           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
5.62k
        i4_frame_qp_q6 =
1109
5.62k
            ((ps_rate_control_api
1110
5.62k
                  ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] *
1111
5.62k
              Kp_kb_factor) /
1112
5.62k
             kp_kb_ref_ref);
1113
5.62k
    }
1114
5.80k
    return i4_frame_qp_q6;
1115
5.80k
}
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
222k
{
1147
    /* UWORD8 u1_frame_qp; */
1148
222k
    WORD32 i4_frame_qp /*,i4_min_frame_qp = 1,i4_max_frame_qp = MAX_MPEG2_QP*/;
1149
222k
    WORD32 i4_max_frame_qp_q6 = (MAX_MPEG2_QP << QSCALE_Q_FAC),
1150
222k
           i4_min_frame_qp_q6 = MIN_QSCALE_Q6; /*0.707 in q6 corresponds to hevc qp = 1*/
1151
222k
    WORD32 i4_is_first_frame_coded = 1;
1152
222k
    WORD32 i4_is_model_valid = 0;
1153
222k
    WORD32 i4_frame_qp_q6, i4_cur_est_header_bits, i4_frame_qp_q6_based_max_vbv_bits;
1154
222k
    WORD32 i4_bit_alloc_est_tex_bits = 0, i4_bit_alloc_est_tex_bits_for_invalid_model = 0,
1155
222k
           i4_est_tex_bits, i4_qp_based_min_est_tex_bits, i4_qp_based_max_est_tex_bits,
1156
222k
           i4_buf_based_min_bits, i4_buf_based_max_bits;
1157
222k
    UWORD32 u4_estimated_sad;
1158
222k
    WORD32 i4_buffer_based_max_qp_clip_flag = 0;
1159
222k
    WORD32 i4_min_Kp_Kb_factor = 0;
1160
222k
    WORD32 i4_steady_state_texture_case = 0;
1161
1162
222k
    if(i4_call_type == 1)
1163
63.0k
    {
1164
63.0k
        *pi4_maxEbfQP = INVALID_QP;
1165
63.0k
        *pi4_modelQP = INVALID_QP;
1166
63.0k
    }
1167
1168
222k
    if((ps_rate_control_api->e_rc_type != VBR_STORAGE) &&
1169
222k
       (ps_rate_control_api->e_rc_type != VBR_STORAGE_DVD_COMP) &&
1170
222k
       (ps_rate_control_api->e_rc_type != CBR_NLDRC) &&
1171
150k
       (ps_rate_control_api->e_rc_type != CONST_QP) &&
1172
150k
       (ps_rate_control_api->e_rc_type != VBR_STREAMING))
1173
0
    {
1174
0
        return (0);
1175
0
    }
1176
1177
222k
    i4_is_first_frame_coded = is_first_frame_coded(ps_rate_control_api);
1178
1179
222k
    assign_complexity_coeffs(ps_rate_control_api->ps_bit_allocation, af_sum_weigh);
1180
1181
222k
    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
222k
    else
1188
222k
    {
1189
222k
        i4_cur_est_header_bits =
1190
222k
            get_cur_frm_est_header_bits(ps_rate_control_api->ps_bit_allocation, e_pic_type);
1191
222k
        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
222k
        i4_is_model_valid = is_model_valid(ps_rate_control_api->aps_rd_model[e_pic_type]);
1196
1197
222k
        if(i4_is_model_valid == 1)
1198
213k
        {
1199
213k
            i4_bit_alloc_est_tex_bits = get_cur_frm_est_texture_bits(
1200
213k
                ps_rate_control_api->ps_bit_allocation,
1201
213k
                ps_rate_control_api->aps_rd_model,
1202
213k
                ps_rate_control_api->ps_est_sad,
1203
213k
                ps_rate_control_api->ps_pic_handling,
1204
213k
                ps_rate_control_api->ps_cbr_buffer,
1205
213k
                e_pic_type,
1206
213k
                i4_is_first_frame_coded,
1207
213k
                0,
1208
213k
                i4_call_type,
1209
213k
                i_to_avg_ratio,
1210
213k
                i4_is_model_valid);
1211
213k
            if(i4_call_type == 1)
1212
63.0k
            {
1213
63.0k
                *pi4_estimate_to_calc_frm_error =
1214
63.0k
                    i4_bit_alloc_est_tex_bits + i4_cur_est_header_bits;
1215
63.0k
            }
1216
1217
            /* vbv buffer position based error correction to keep away encoder buffer overflow at layer 0 pictures*/
1218
213k
            if(e_pic_type == I_PIC || e_pic_type == P_PIC || e_pic_type == P1_PIC)
1219
179k
            {
1220
179k
                WORD32 i4_cur_ebf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer);
1221
179k
                WORD32 i4_vbv_size = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
1222
179k
                WORD32 i4_max_ebf = (WORD32)(i4_vbv_size * MAX_THRESHOLD_VBV_FRM_ERROR);
1223
179k
                WORD32 i4_drain_rate = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
1224
179k
                WORD32 i4_total_bits_allocted = i4_bit_alloc_est_tex_bits + i4_cur_est_header_bits;
1225
179k
                WORD32 i4_total_bits_to_be_alloc;
1226
179k
                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
179k
                if(i4_expected_ebf > i4_max_ebf)
1230
3
                {
1231
3
                    i4_total_bits_to_be_alloc = MAX(
1232
3
                        i4_drain_rate, (i4_total_bits_allocted - (i4_expected_ebf - i4_max_ebf)));
1233
3
                    i4_bit_alloc_est_tex_bits = i4_total_bits_to_be_alloc - i4_cur_est_header_bits;
1234
3
                }
1235
179k
            }
1236
213k
        }
1237
9.51k
        else
1238
9.51k
        {
1239
9.51k
            i4_bit_alloc_est_tex_bits_for_invalid_model = get_cur_frm_est_texture_bits(
1240
9.51k
                ps_rate_control_api->ps_bit_allocation,
1241
9.51k
                ps_rate_control_api->aps_rd_model,
1242
9.51k
                ps_rate_control_api->ps_est_sad,
1243
9.51k
                ps_rate_control_api->ps_pic_handling,
1244
9.51k
                ps_rate_control_api->ps_cbr_buffer,
1245
9.51k
                e_pic_type,
1246
9.51k
                i4_is_first_frame_coded,
1247
9.51k
                0,
1248
9.51k
                i4_call_type,
1249
9.51k
                i_to_avg_ratio,
1250
9.51k
                i4_is_model_valid);
1251
9.51k
            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
9.51k
        }
1257
1258
222k
#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
222k
        if(i4_bit_alloc_est_tex_bits <= (i4_cur_est_header_bits >> 3))
1262
12.2k
        {
1263
12.2k
            i4_bit_alloc_est_tex_bits = (i4_cur_est_header_bits >> 3);
1264
12.2k
        }
1265
222k
#endif
1266
1267
        /* Step 2: Getting the min and max texture bits based on min and max qp */
1268
222k
        if(i4_is_model_valid && ps_rate_control_api->au1_avg_bitrate_changed[e_pic_type] == 0)
1269
212k
        {
1270
212k
            WORD32 /*i4_min_qp, i4_max_qp,*/ i4_max_qp_q6, i4_min_qp_q6;
1271
212k
            number_t s_lin_coeff_wo_int =
1272
212k
                get_linear_coefficient(ps_rate_control_api->aps_rd_model[e_pic_type]);
1273
1274
212k
            if(s_lin_coeff_wo_int.sm != 0)
1275
212k
            {
1276
                /* Get the min and max qp deviation allowed based on prev frame qp */
1277
212k
                get_min_max_qp(
1278
212k
                    ps_rate_control_api,
1279
212k
                    e_pic_type,
1280
212k
                    &i4_max_qp_q6,
1281
212k
                    &i4_min_qp_q6,
1282
212k
                    i4_complexity_bin,
1283
212k
                    i4_scene_num);
1284
1285
                /* Estimate the max bits based on min qp */
1286
212k
                i4_qp_based_min_est_tex_bits = estimate_bits_for_qp(
1287
212k
                    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
212k
                i4_qp_based_max_est_tex_bits = estimate_bits_for_qp(
1290
212k
                    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
212k
                i4_min_frame_qp_q6 = i4_min_qp_q6;
1293
212k
                i4_max_frame_qp_q6 = i4_max_qp_q6;
1294
212k
                i4_qp_based_max_est_tex_bits = i4_bit_alloc_est_tex_bits;
1295
212k
                i4_qp_based_min_est_tex_bits = i4_bit_alloc_est_tex_bits;
1296
212k
            }
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
212k
        }
1303
10.3k
        else
1304
10.3k
        {
1305
10.3k
            i4_qp_based_min_est_tex_bits = i4_bit_alloc_est_tex_bits_for_invalid_model;
1306
10.3k
            i4_qp_based_max_est_tex_bits = i4_bit_alloc_est_tex_bits_for_invalid_model;
1307
10.3k
            ps_rate_control_api->au1_avg_bitrate_changed[e_pic_type] = 0;
1308
10.3k
        }
1309
1310
        /* Step 3: Getting the min and max texture bits based on buffer fullness */
1311
1312
222k
        if(i4_call_type == 1)
1313
63.0k
        {
1314
63.0k
            WORD32 i4_get_error;
1315
1316
63.0k
            i4_get_error = rc_get_estimate_bit_error(ps_rate_control_api);
1317
1318
63.0k
            get_min_max_bits_based_on_buffer(
1319
63.0k
                ps_rate_control_api,
1320
63.0k
                e_pic_type,
1321
63.0k
                &i4_buf_based_min_bits,
1322
63.0k
                &i4_buf_based_max_bits,
1323
63.0k
                i4_get_error);
1324
1325
            /*In case buffer limitation will come, no need to reduce the QP further because of warning flag*/
1326
63.0k
            if(i4_bit_alloc_est_tex_bits < (i4_buf_based_min_bits - i4_cur_est_header_bits))
1327
33.4k
                ps_rate_control_api->i4_underflow_warning = 0;
1328
1329
63.0k
            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
63.0k
            trace_printf(
1334
63.0k
                "i4_buf_based_min_bits %d i4_buf_based_max_bits %d",
1335
63.0k
                i4_buf_based_min_bits,
1336
63.0k
                i4_buf_based_max_bits);
1337
63.0k
            trace_printf(
1338
63.0k
                "Prev I frame qp q6 %d P frame qp q6 %d",
1339
63.0k
                ps_rate_control_api->ai4_prev_frm_qp_q6[I_PIC],
1340
63.0k
                ps_rate_control_api->ai4_prev_frm_qp_q6[P_PIC]);
1341
63.0k
        }
1342
159k
        else
1343
159k
        {
1344
159k
            i4_buf_based_min_bits = i4_qp_based_min_est_tex_bits;
1345
159k
            i4_buf_based_max_bits = i4_qp_based_max_est_tex_bits;
1346
159k
        }
1347
        /* for I frame the max bits is not restricted based on the user input */
1348
222k
        if(e_pic_type == I_PIC)
1349
92.3k
        {
1350
92.3k
            i4_ud_max_bits = 0x7fffffff; /* i4_bit_alloc_est_tex_bits + i4_cur_est_header_bits; */
1351
92.3k
        }
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
222k
        i4_est_tex_bits = get_max(
1359
222k
            i4_bit_alloc_est_tex_bits,
1360
222k
            i4_qp_based_min_est_tex_bits,
1361
222k
            (i4_buf_based_min_bits - i4_cur_est_header_bits));
1362
222k
        i4_est_tex_bits = get_min(
1363
222k
            i4_est_tex_bits,
1364
222k
            i4_qp_based_max_est_tex_bits,
1365
222k
            (i4_ud_max_bits - i4_cur_est_header_bits),
1366
222k
            (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
222k
        CLIP(
1370
222k
            i4_est_tex_bits,
1371
222k
            i4_buf_based_max_bits - i4_cur_est_header_bits,
1372
222k
            i4_buf_based_min_bits - i4_cur_est_header_bits);
1373
1374
222k
        {
1375
222k
            WORD32 i4_drain_bits_per_frame =
1376
222k
                       get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer),
1377
222k
                   i4_ebf;
1378
222k
            WORD32 i4_delay = cbr_get_delay_frames(ps_rate_control_api->ps_cbr_buffer),
1379
222k
                   max_buffer_level = 0, rc_type = get_rc_type(ps_rate_control_api->ps_cbr_buffer);
1380
1381
222k
            if(rc_type == VBR_STREAMING)
1382
150k
                max_buffer_level = i4_drain_bits_per_frame * i4_delay;
1383
72.3k
            else
1384
72.3k
                max_buffer_level = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
1385
1386
222k
            i4_ebf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer);
1387
1388
222k
            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
222k
        }
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
222k
        if((!i4_is_first_frame_coded /* && ps_rate_control_api->i4_rc_pass == 2*/) ||
1415
204k
           !i4_is_model_valid)  //ELP_RC
1416
19.0k
        {
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
19.0k
            WORD32 Kp_kb_factor = get_Kp_Kb(ps_rate_control_api->ps_bit_allocation, e_pic_type);
1422
19.0k
            WORD32 kp_kb_ref_ref = get_Kp_Kb(
1423
19.0k
                ps_rate_control_api->ps_bit_allocation, ps_rate_control_api->prev_ref_pic_type);
1424
1425
19.0k
            if(e_pic_type == I_PIC &&
1426
5.89k
               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
19.0k
            else if(
1437
19.0k
                e_pic_type == I_PIC &&
1438
5.89k
                ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] != 0x7FFFFFFF)
1439
5.89k
            {
1440
5.89k
                i4_frame_qp = ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC];
1441
5.89k
                i4_frame_qp_q6 = ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC];
1442
5.89k
            }
1443
13.1k
            else /*! ISlice*/
1444
13.1k
            {
1445
13.1k
                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
13.1k
                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
13.1k
                if(ps_rate_control_api
1456
13.1k
                       ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] ==
1457
13.1k
                   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
13.1k
                i4_frame_qp_q6 =
1466
13.1k
                    ((ps_rate_control_api
1467
13.1k
                          ->ai4_prev_frm_qp_q6[i4_scene_num][ps_rate_control_api->prev_ref_pic_type] *
1468
13.1k
                      Kp_kb_factor) /
1469
13.1k
                     kp_kb_ref_ref);
1470
13.1k
            }
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
19.0k
            {
1479
19.0k
                WORD32 i4_bits_per_frame;
1480
19.0k
                i4_bits_per_frame = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
1481
19.0k
                if(i4_call_type == 1)
1482
0
                {
1483
0
                    rc_modify_est_tot(ps_rate_control_api, i4_bits_per_frame);
1484
0
                }
1485
19.0k
            }
1486
19.0k
        }
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
203k
        else if(i4_est_tex_bits > 0)
1492
201k
        {
1493
201k
            if(i4_call_type == 1)
1494
63.0k
            {
1495
63.0k
                rc_modify_est_tot(ps_rate_control_api, (i4_est_tex_bits + i4_cur_est_header_bits));
1496
63.0k
            }
1497
201k
            i4_steady_state_texture_case = 1;
1498
            /* Query the model for the Qp for the corresponding frame*/
1499
201k
            i4_frame_qp_q6_based_max_vbv_bits = find_qp_for_target_bits(
1500
201k
                ps_rate_control_api->aps_rd_model[e_pic_type],
1501
201k
                i4_buf_based_max_bits - i4_cur_est_header_bits,
1502
201k
                u4_estimated_sad,
1503
201k
                (ps_rate_control_api->ai4_max_qp_q6[e_pic_type]),
1504
201k
                (ps_rate_control_api->ai4_min_qp_q6[e_pic_type]));
1505
201k
            if(i4_call_type == 1)
1506
63.0k
            {
1507
63.0k
                *pi4_maxEbfQP = ihevce_rc_get_scaled_hevce_qp_q6(
1508
63.0k
                    i4_frame_qp_q6_based_max_vbv_bits, ps_rate_control_api->u1_bit_depth);
1509
63.0k
            }
1510
            /* Query the model for the Qp for the corresponding frame*/
1511
201k
            i4_frame_qp_q6 = find_qp_for_target_bits(
1512
201k
                ps_rate_control_api->aps_rd_model[e_pic_type],
1513
201k
                i4_est_tex_bits,
1514
201k
                u4_estimated_sad,
1515
201k
                (ps_rate_control_api->ai4_max_qp_q6[e_pic_type]),
1516
201k
                (ps_rate_control_api->ai4_min_qp_q6[e_pic_type]));
1517
201k
            i4_frame_qp = ((i4_frame_qp_q6 + (1 << (QSCALE_Q_FAC - 1))) >> QSCALE_Q_FAC);
1518
201k
        }
1519
2.07k
        else
1520
2.07k
        {
1521
2.07k
            {
1522
2.07k
                WORD32 i4_bits_per_frame;
1523
2.07k
                i4_bits_per_frame = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
1524
2.07k
                if(i4_call_type == 1)
1525
0
                {
1526
0
                    rc_modify_est_tot(ps_rate_control_api, i4_bits_per_frame);
1527
0
                }
1528
2.07k
            }
1529
2.07k
            i4_frame_qp = ps_rate_control_api->ai4_max_qp[e_pic_type];
1530
2.07k
            i4_frame_qp_q6 = ps_rate_control_api->ai4_max_qp_q6[e_pic_type];
1531
2.07k
        }
1532
222k
        if(i4_call_type == 1)
1533
63.0k
        {
1534
63.0k
            *pi4_modelQP =
1535
63.0k
                ihevce_rc_get_scaled_hevce_qp_q6(i4_frame_qp_q6, ps_rate_control_api->u1_bit_depth);
1536
63.0k
        }
1537
222k
        {
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
222k
            WORD32 i4_clip_flag =
1542
222k
                ((i4_call_type == 1) && (i4_is_model_valid == 1) &&
1543
63.0k
                 (ps_rate_control_api->i4_rc_pass == 2) &&
1544
0
                 (i4_buf_based_max_bits > i4_est_tex_bits));
1545
222k
            WORD32 i4_ebf = rc_get_ebf(ps_rate_control_api),
1546
222k
                   i4_max_ebf = i4_ebf + i4_buf_based_max_bits;
1547
222k
            WORD32 i4_inter_frame_interval =
1548
222k
                pic_type_get_inter_frame_interval(ps_rate_control_api->ps_pic_handling);
1549
222k
            float f_buffer_fullness = (float)i4_ebf / i4_max_ebf;
1550
222k
            i4_clip_flag = i4_clip_flag && (ps_rate_control_api->i4_scd_in_period_2_pass == 1);
1551
222k
            i4_clip_flag = i4_clip_flag && (i4_ebf < (i4_max_ebf * 0.5f));
1552
222k
            i4_clip_flag = i4_clip_flag && (ps_rate_control_api->e_rc_type == VBR_STREAMING);
1553
1554
222k
            i4_clip_flag = i4_clip_flag && (ps_rate_control_api->i4_frames_since_last_scd >
1555
0
                                            i4_inter_frame_interval);
1556
1557
222k
            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
222k
            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
222k
        }
1609
1610
        /*swing restriciton based on previous frame qp swing*/
1611
222k
        {
1612
222k
            if(i4_call_type == 1)
1613
63.0k
            {
1614
63.0k
                trace_printf(
1615
63.0k
                    "Before i4_frame_qp_q6 = %d min qp = %d  max_qp = %d    "
1616
63.0k
                    "bufclip %d",
1617
63.0k
                    i4_frame_qp_q6,
1618
63.0k
                    (i4_min_frame_qp_q6),
1619
63.0k
                    (i4_max_frame_qp_q6),
1620
63.0k
                    i4_buffer_based_max_qp_clip_flag);
1621
63.0k
            }
1622
222k
            if(i4_frame_qp_q6 < i4_min_frame_qp_q6)
1623
107k
                i4_frame_qp_q6 = i4_min_frame_qp_q6;
1624
1625
            /*removed low side clipping to avoid HRD compliance issue*/
1626
222k
            if(i4_steady_state_texture_case)
1627
201k
            {
1628
201k
                if(i4_frame_qp_q6 > i4_max_frame_qp_q6)
1629
37.3k
                {
1630
37.3k
                    if(i4_max_frame_qp_q6 > (i4_frame_qp_q6_based_max_vbv_bits))
1631
307
                    {
1632
307
                        i4_frame_qp_q6 = i4_max_frame_qp_q6;
1633
307
                    }
1634
37.0k
                    else
1635
37.0k
                    {
1636
37.0k
                        i4_frame_qp_q6 = i4_frame_qp_q6_based_max_vbv_bits;
1637
37.0k
                    }
1638
37.3k
                }
1639
201k
            }
1640
222k
        }
1641
222k
        if(i4_call_type == 1)
1642
63.0k
        {
1643
63.0k
            trace_printf("After i4_frame_qp_q6 = %d", i4_frame_qp_q6);
1644
63.0k
        }
1645
1646
        /* SS - Following done to restore this after pause to resume detect - 0.25 is for syntax bits */
1647
222k
        ps_rate_control_api->i4_orig_frm_est_bits = (i4_est_tex_bits * 5) >> 2;
1648
222k
        ps_rate_control_api->i4_prev_frm_est_bits = (i4_est_tex_bits + i4_cur_est_header_bits);
1649
222k
        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
222k
        *pi4_is_model_valid = i4_is_model_valid;
1654
1655
222k
        if(0 == i4_is_model_valid)
1656
9.51k
        {
1657
9.51k
            *pi4_tot_bits_estimated =
1658
9.51k
                i4_bit_alloc_est_tex_bits_for_invalid_model;  //(i4_buf_based_max_bits * 0.80);
1659
9.51k
        }
1660
213k
        else
1661
213k
        {
1662
213k
            *pi4_tot_bits_estimated = i4_est_tex_bits + i4_cur_est_header_bits;
1663
213k
        }
1664
1665
        /*For B pics assigning a non-zero value to avoid asser */
1666
222k
        if(*pi4_tot_bits_estimated == 0)
1667
2.34k
        {
1668
2.34k
            *pi4_tot_bits_estimated = 1;
1669
2.34k
        }
1670
222k
        ASSERT(*pi4_tot_bits_estimated != 0);
1671
        /*Underflow prevention*/
1672
222k
        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
222k
    }
1686
1687
    /* Clip the frame qp within Min and Max QP */
1688
222k
    if(i4_frame_qp_q6 < ps_rate_control_api->ai4_min_qp_q6[e_pic_type])
1689
27
    {
1690
27
        i4_frame_qp_q6 = ps_rate_control_api->ai4_min_qp_q6[e_pic_type];
1691
27
    }
1692
222k
    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
222k
    if(i4_call_type == 1)
1697
63.0k
    {
1698
63.0k
        *pi4_vbv_buf_max_bits = i4_buf_based_max_bits;
1699
63.0k
        *pi4_est_tex_bits = i4_est_tex_bits;
1700
63.0k
        *pi4_cur_est_header_bits = i4_cur_est_header_bits;
1701
63.0k
    }
1702
222k
    return (i4_frame_qp_q6);
1703
222k
}
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
63.0k
{
1727
63.0k
    UWORD32 u4_estimated_sad;
1728
63.0k
    u4_estimated_sad = get_est_sad(ps_rate_control_api->ps_est_sad, e_pic_type);
1729
63.0k
    {
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
63.0k
        if((*pi4_modelQP != INVALID_QP) && (*pi4_maxEbfQP != INVALID_QP) &&
1732
           /*(*pi4_modelQP >= i4_clipQP) &&*/
1733
63.0k
           (i4_clipQP > *pi4_maxEbfQP))
1734
40.5k
        {
1735
40.5k
            WORD32 i4_loop = 0, i4_error, i4_prev_error = 0x7FFFFFFF;
1736
40.5k
            WORD32 i4_frame_qp_q6_temp;
1737
40.5k
            WORD32 i4_buf_max_text_bits = i4_buf_based_max_bits - i4_cur_est_header_bits;
1738
40.5k
            WORD32 i4_min_bits = i4_est_tex_bits, i4_max_bits = i4_buf_max_text_bits;
1739
40.5k
            WORD32 i4_temp_bits = (i4_min_bits + i4_max_bits) >> 1;
1740
40.5k
            if(*pi4_modelQP == i4_clipQP)
1741
33
            {
1742
33
                *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
33
                return;
1745
33
            }
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
1.25M
            while(i4_loop < 30)
1749
1.21M
            {
1750
1.21M
                i4_frame_qp_q6_temp = find_qp_for_target_bits(
1751
1.21M
                    ps_rate_control_api->aps_rd_model[e_pic_type],
1752
1.21M
                    i4_temp_bits,
1753
1.21M
                    u4_estimated_sad,
1754
1.21M
                    (ps_rate_control_api->ai4_max_qp_q6[e_pic_type]),
1755
1.21M
                    (ps_rate_control_api->ai4_min_qp_q6[e_pic_type]));
1756
1.21M
                i4_error = abs(i4_frame_qp_q6_temp - i4_frame_qp_q6);
1757
1.21M
                if(i4_error < i4_prev_error)
1758
41.9k
                {
1759
41.9k
                    *pi8_bits_from_finalQP = i4_temp_bits + i4_cur_est_header_bits;
1760
41.9k
                    i4_prev_error = i4_error;
1761
                    //printf("*pi8_bits_from_finalQP = %d\n",*pi8_bits_from_finalQP);
1762
41.9k
                }
1763
1.21M
                if(i4_frame_qp_q6_temp < i4_frame_qp_q6)
1764
1.21M
                {
1765
1.21M
                    i4_max_bits = i4_temp_bits;
1766
1.21M
                }
1767
633
                else
1768
633
                {
1769
633
                    i4_min_bits = i4_temp_bits;
1770
633
                }
1771
1.21M
                i4_temp_bits = (i4_min_bits + i4_max_bits) >> 1;
1772
1.21M
                i4_loop++;
1773
1.21M
            }
1774
40.4k
        }
1775
22.5k
        else
1776
22.5k
        {
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
22.5k
            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
22.5k
            else
1784
22.5k
            {
1785
22.5k
                *pi8_bits_from_finalQP = i4_buf_based_max_bits;
1786
22.5k
            }
1787
22.5k
        }
1788
63.0k
    }
1789
63.0k
    return;
1790
63.0k
}
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
106k
{
1810
106k
    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
106k
    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
106k
    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
106k
    else if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
1828
24.7k
    {
1829
24.7k
        e_buf_status = get_cbr_buffer_status(
1830
24.7k
            ps_rate_control_api->ps_cbr_buffer,
1831
24.7k
            i4_total_frame_bits,
1832
24.7k
            pi4_num_bits_to_prevent_vbv_underflow,
1833
24.7k
            e_pic_type,
1834
24.7k
            ps_rate_control_api->e_rc_type);
1835
24.7k
    }
1836
81.3k
    else if(ps_rate_control_api->e_rc_type == VBR_STREAMING)
1837
51.4k
    {
1838
        /* For VBR_streaming the error bits are computed according to peak bitrate*/
1839
51.4k
        e_buf_status = get_cbr_buffer_status(
1840
51.4k
            ps_rate_control_api->ps_cbr_buffer,
1841
51.4k
            i4_total_frame_bits,
1842
51.4k
            pi4_num_bits_to_prevent_vbv_underflow,
1843
51.4k
            e_pic_type,
1844
51.4k
            ps_rate_control_api->e_rc_type);
1845
51.4k
    }
1846
106k
    return e_buf_status;
1847
106k
}
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
106k
{
1952
106k
    UWORD8 u1_num_skips = 0;
1953
106k
    WORD32 i;
1954
    /*picture_type_e  e_orig_pic_type = e_pic_type;*/
1955
106k
    LWORD64 i8_frame_sad = 0; /* Frame level SAD */
1956
106k
    WORD32 i4_tot_texture_bits = 0; /* Total texture bits consumed */
1957
106k
    WORD32 i4_tot_mbs = 0; /* Total number of mbs in frame */
1958
106k
    LWORD64 i8_avg_qp = 0, i8_avg_qp_q6 = 0;
1959
106k
    WORD32 i4_flag_rc_model_update = (i4_is_rc_model_needs_to_be_updated == 1);
1960
106k
    WORD32 i4_gop_correction = 0, i4_new_correction = 0;
1961
1962
106k
    ps_frame_info->i4_flag_rc_model_update = i4_flag_rc_model_update;
1963
106k
    ps_frame_info->i4_num_entries++;
1964
106k
    trace_printf(
1965
106k
        "update pic_type = %d      tbc = %d   hbc = %d\n",
1966
106k
        e_pic_type,
1967
106k
        (i4_total_frame_bits - i4_model_updation_hdr_bits),
1968
106k
        i4_model_updation_hdr_bits);
1969
    /* NOTE KJN: SCD not supported in case of B Frames */
1970
106k
    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
106k
    if(u1_is_scd && i4_is_pause_to_resume)
1977
0
        i4_is_pause_to_resume = 0;
1978
1979
106k
    if(!i4_is_it_a_skip && !i4_is_pic_handling_done)
1980
106k
    {
1981
        /* Update the pic_handling struct */
1982
        /*: do not update pic handling even in case of non-reference B-PIC*/
1983
106k
        update_pic_handling(
1984
106k
            ps_rate_control_api->ps_pic_handling, e_pic_type, i4_suppress_bpic_update, u1_is_scd);
1985
106k
    }
1986
106k
    {
1987
106k
        WORD32 *pi4_qp_array =
1988
106k
            ps_rate_control_api
1989
106k
                ->ai4_prev_frm_qp[(i4_scene_num + HALF_MAX_SCENE_NUM_RC) % MAX_SCENE_NUM_RC];
1990
106k
        WORD32 *pi4_qp_array_q6 =
1991
106k
            ps_rate_control_api
1992
106k
                ->ai4_prev_frm_qp_q6[(i4_scene_num + HALF_MAX_SCENE_NUM_RC) % MAX_SCENE_NUM_RC];
1993
106k
        WORD32 i4_i;
1994
1.06M
        for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
1995
955k
        {
1996
955k
            pi4_qp_array[i4_i] = 0x7FFFFFFF;
1997
955k
            pi4_qp_array_q6[i4_i] = 0x7FFFFFFF;
1998
955k
        }
1999
106k
    }
2000
2001
106k
    if(ps_rate_control_api->e_rc_type == CONST_QP)
2002
29.8k
    {
2003
29.8k
        if(!i4_is_it_a_skip)
2004
29.8k
        {
2005
            /******************************************************************
2006
            Calculate the total values from the individual values
2007
            ******************************************************************/
2008
89.6k
            for(i = 0; i < MAX_MB_TYPE; i++)
2009
59.7k
                i8_frame_sad += pi8_mb_type_sad[i];
2010
89.6k
            for(i = 0; i < MAX_MB_TYPE; i++)
2011
59.7k
                i4_tot_texture_bits += pi4_mb_type_tex_bits[i];
2012
89.6k
            for(i = 0; i < MAX_MB_TYPE; i++)
2013
59.7k
                i8_avg_qp += (pi8_tot_mb_type_qp_q6[i] >> 6);
2014
2015
89.6k
            for(i = 0; i < MAX_MB_TYPE; i++)
2016
59.7k
                i8_avg_qp_q6 += pi8_tot_mb_type_qp_q6[i];
2017
89.6k
            for(i = 0; i < MAX_MB_TYPE; i++)
2018
59.7k
                i4_tot_mbs += pi4_tot_mb_in_type[i];
2019
29.8k
            i8_avg_qp /= i4_tot_mbs; /* Calculate the average QP */
2020
29.8k
            i8_avg_qp_q6 /= i4_tot_mbs;
2021
2022
29.8k
            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
29.8k
            ps_frame_info->i8_frame_num = get_num_frms_encoded(ps_rate_control_api->ps_cbr_buffer);
2034
29.8k
            ps_frame_info->i4_num_entries++;
2035
2036
29.8k
            update_cbr_buffer(
2037
29.8k
                ps_rate_control_api->ps_cbr_buffer,
2038
29.8k
                (i4_total_frame_bits + i4_bits_to_be_stuffed),
2039
29.8k
                e_pic_type);
2040
29.8k
        }
2041
29.8k
    }
2042
2043
106k
    if(ps_rate_control_api->e_rc_type != CONST_QP)
2044
76.2k
    {
2045
        /* For improving CBR streams quality */
2046
76.2k
        WORD32 i4_buffer_based_bit_error = 0;
2047
2048
76.2k
        if(!i4_is_it_a_skip)
2049
76.2k
        {
2050
76.2k
            WORD32 i4_new_period_flag;
2051
            /******************************************************************
2052
            Calculate the total values from the individual values
2053
            ******************************************************************/
2054
228k
            for(i = 0; i < MAX_MB_TYPE; i++)
2055
152k
                i8_frame_sad += pi8_mb_type_sad[i];
2056
228k
            for(i = 0; i < MAX_MB_TYPE; i++)
2057
152k
                i4_tot_texture_bits += pi4_mb_type_tex_bits[i];
2058
228k
            for(i = 0; i < MAX_MB_TYPE; i++)
2059
152k
                i8_avg_qp += (pi8_tot_mb_type_qp_q6[i] >> 6);
2060
2061
228k
            for(i = 0; i < MAX_MB_TYPE; i++)
2062
152k
                i8_avg_qp_q6 += pi8_tot_mb_type_qp_q6[i];
2063
228k
            for(i = 0; i < MAX_MB_TYPE; i++)
2064
152k
                i4_tot_mbs += pi4_tot_mb_in_type[i];
2065
76.2k
            i8_avg_qp /= i4_tot_mbs; /* Calculate the average QP */
2066
76.2k
            i8_avg_qp_q6 /= i4_tot_mbs;
2067
2068
76.2k
            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
76.2k
            ps_frame_info->i8_frame_num = get_num_frms_encoded(ps_rate_control_api->ps_cbr_buffer);
2080
76.2k
            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
76.2k
            i4_new_period_flag = is_last_frame_in_gop(ps_rate_control_api->ps_pic_handling);
2087
2088
76.2k
            update_cur_frm_consumed_bits(
2089
76.2k
                ps_rate_control_api->ps_bit_allocation,
2090
76.2k
                ps_rate_control_api->ps_pic_handling,
2091
76.2k
                ps_rate_control_api->ps_cbr_buffer,
2092
76.2k
                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
76.2k
                i4_model_updation_hdr_bits,
2095
76.2k
                e_pic_type,
2096
76.2k
                u1_is_scd,
2097
76.2k
                i4_is_end_of_period,
2098
76.2k
                i4_lap_based_comp_reset,
2099
76.2k
                i4_suppress_bpic_update,
2100
76.2k
                i4_buffer_based_bit_error,
2101
76.2k
                i4_bits_to_be_stuffed,
2102
76.2k
                i4_lap_window_comp,
2103
76.2k
                ps_rate_control_api->e_rc_type,
2104
76.2k
                ps_rate_control_api->i4_num_gop,
2105
76.2k
                i4_is_pause_to_resume,
2106
76.2k
                i4_est_text_bits_ctr_update_qp,
2107
76.2k
                &i4_gop_correction,
2108
76.2k
                &i4_new_correction);
2109
76.2k
            if(1 == i4_new_period_flag &&
2110
22.4k
               ((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
2111
22.4k
                (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
76.2k
        }
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
76.2k
        if((ps_rate_control_api->e_rc_type == VBR_STORAGE) ||
2126
76.2k
           (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
76.2k
        else if(
2131
76.2k
            ps_rate_control_api->e_rc_type == CBR_NLDRC ||
2132
51.4k
            ps_rate_control_api->e_rc_type == VBR_STREAMING)
2133
76.2k
        {
2134
76.2k
            update_cbr_buffer(
2135
76.2k
                ps_rate_control_api->ps_cbr_buffer,
2136
76.2k
                (i4_total_frame_bits + i4_bits_to_be_stuffed),
2137
76.2k
                e_pic_type);
2138
76.2k
        }
2139
2140
76.2k
        if(e_pic_type != B_PIC || e_pic_type != B1_PIC || e_pic_type != B2_PIC)
2141
76.2k
        {
2142
76.2k
            ps_rate_control_api->i4_prev_ref_is_scd = 0;
2143
76.2k
        }
2144
2145
76.2k
        if(!i4_is_it_a_skip)
2146
76.2k
        {
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
76.2k
            if(u1_is_scd || ps_rate_control_api->u1_is_first_frm)
2154
216
            {
2155
216
                e_pic_type = I_PIC;
2156
2157
                /* Reset the SAD estimation module */
2158
216
                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
216
                ps_rate_control_api->i4_prev_ref_is_scd = 1;
2163
2164
                /* Reset the MB Rate control */
2165
216
                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
216
                ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] = (WORD32)i8_avg_qp;
2170
2171
216
                ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] = (WORD32)i8_avg_qp_q6;
2172
2173
216
                ps_rate_control_api->i4_frames_since_last_scd = 0;
2174
2175
216
                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
216
            }
2179
76.0k
            else if(i4_is_pause_to_resume)
2180
820
            {
2181
820
                reset_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[e_pic_type]);  //ELP_RC
2182
820
            }
2183
76.2k
            if(i8_frame_sad && (!i4_suppress_bpic_update))
2184
68.4k
            {
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
68.4k
                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
68.4k
                if(i4_tot_texture_bits_added_to_model > 0 && (i4_flag_rc_model_update == 1))
2197
66.7k
                {
2198
66.7k
                    add_frame_to_rd_model(
2199
66.7k
                        ps_rate_control_api->aps_rd_model[e_pic_type],
2200
66.7k
                        i4_tot_texture_bits_added_to_model,
2201
66.7k
                        (WORD32)i8_avg_qp_q6,
2202
66.7k
                        i8_frame_sad,
2203
66.7k
                        u1_num_skips);
2204
2205
66.7k
                    {
2206
66.7k
                        number_t temp =
2207
66.7k
                            get_linear_coefficient(ps_rate_control_api->aps_rd_model[e_pic_type]);
2208
66.7k
                        ps_frame_info->model_coeff_a_lin_wo_int.e = temp.e;
2209
66.7k
                        ps_frame_info->model_coeff_a_lin_wo_int.sm = temp.sm;
2210
66.7k
                    }
2211
66.7k
                }
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
68.4k
                update_actual_sad(
2218
68.4k
                    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
68.4k
                ps_rate_control_api->au1_is_first_frm_coded[e_pic_type] = 1;
2230
68.4k
            }
2231
2232
76.2k
            if(i4_avg_activity)
2233
76.2k
            {
2234
                /* Update the mb_level model */
2235
76.2k
                mb_update_frame_level(ps_rate_control_api->ps_mb_rate_control, i4_avg_activity);
2236
76.2k
            }
2237
            /* Update the variable which denotes that a frame has been encountered */
2238
76.2k
            ps_rate_control_api->u1_is_first_frm = 0;
2239
76.2k
            ps_rate_control_api->i4_frames_since_last_scd++;
2240
76.2k
        }
2241
76.2k
    }
2242
106k
    return;
2243
106k
}
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
205k
{
2263
205k
    WORD32 i4_avg_qp = 0, i4_avg_qp_q6 = 0;
2264
2265
205k
    i4_avg_qp = (i4_avg_frame_qp_q6 >> 6);
2266
205k
    i4_avg_qp_q6 = i4_avg_frame_qp_q6;
2267
2268
205k
    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
205k
    if(e_pic_type == I_PIC)
2274
67.0k
    {
2275
67.0k
        ps_rate_control_api->i4_I_frame_qp_model = is_first_frame_coded(ps_rate_control_api);
2276
67.0k
    }
2277
205k
    if((i4_is_scd && i4_is_pause_to_resume))  //KISH
2278
0
        i4_is_pause_to_resume = 0;
2279
2280
205k
    if(i4_is_scd || ps_rate_control_api->u1_is_first_frm)
2281
7.38k
    {
2282
        /* Save previous B-QP since some B-pics may follow detection of SCD */
2283
2284
7.38k
        e_pic_type = I_PIC;
2285
2286
        /* Reset the SAD estimation module */
2287
7.38k
        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
7.38k
        ps_rate_control_api->i4_prev_ref_is_scd = 1;
2292
2293
        /* Reset the MB Rate control */
2294
7.38k
        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
7.38k
        ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] = i4_avg_qp;
2299
2300
7.38k
        ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] = i4_avg_qp_q6;
2301
7.38k
    }
2302
197k
    else if(i4_is_pause_to_resume)
2303
1.57k
    {
2304
        /*pause to resume is guranteed to be P_PIC*/
2305
1.57k
        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
1.57k
        ps_rate_control_api->au1_is_first_frm_coded[e_pic_type] = 0;
2317
1.57k
        ps_rate_control_api->i4_frames_since_last_scd = 0;
2318
2319
1.57k
        {
2320
1.57k
            ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][e_pic_type] = i4_avg_qp;
2321
1.57k
            ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type] = i4_avg_qp_q6;
2322
1.57k
        }
2323
        /*also reset previous I pic Qp since it uses I frame qp for qp determination when model is reset*/
2324
1.57k
        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
1.57k
        else if(e_pic_type == P_PIC || e_pic_type == P1_PIC)
2330
1.29k
        {
2331
1.29k
            ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] =
2332
1.29k
                ((LWORD64)i4_avg_qp * P_TO_I_RATIO) >> K_Q;
2333
1.29k
            ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] =
2334
1.29k
                ((LWORD64)i4_avg_qp_q6 * P_TO_I_RATIO) >> K_Q;
2335
1.29k
        }
2336
288
        else if(e_pic_type == B_PIC || e_pic_type == BB_PIC)
2337
144
        {
2338
144
            ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] =
2339
144
                ((LWORD64)i4_avg_qp * P_TO_I_RATIO * P_TO_I_RATIO) >> (K_Q + K_Q);
2340
144
            ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] =
2341
144
                ((LWORD64)i4_avg_qp_q6 * P_TO_I_RATIO * P_TO_I_RATIO) >> (K_Q + K_Q);
2342
144
        }
2343
144
        else if(e_pic_type == B1_PIC || e_pic_type == B11_PIC)
2344
144
        {
2345
144
            ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][I_PIC] =
2346
144
                ((LWORD64)i4_avg_qp * P_TO_I_RATIO * P_TO_I_RATIO * P_TO_I_RATIO) >>
2347
144
                (K_Q + K_Q + K_Q);
2348
144
            ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][I_PIC] =
2349
144
                ((LWORD64)i4_avg_qp_q6 * P_TO_I_RATIO * P_TO_I_RATIO * P_TO_I_RATIO) >>
2350
144
                (K_Q + K_Q + K_Q);
2351
144
        }
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
1.57k
    }
2363
196k
    else
2364
196k
    {
2365
196k
#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
196k
        if(!i4_suppress_bpic_update)
2370
196k
        {
2371
196k
            ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][e_pic_type] = i4_avg_qp;
2372
196k
            ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][e_pic_type] = i4_avg_qp_q6;
2373
196k
            trace_printf("Prev frame qp q6 update %d pic type %d", i4_avg_qp_q6, e_pic_type);
2374
196k
        }
2375
196k
#endif
2376
196k
    }
2377
2378
205k
    if(i4_num_frm_enc_in_scene == 1)
2379
6.95k
    {
2380
6.95k
        WORD32 i4_i = 0;
2381
69.5k
        for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
2382
62.5k
        {
2383
62.5k
            if(ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][i4_i] == 0x7FFFFFFF)
2384
55.6k
            {
2385
55.6k
                ps_rate_control_api->ai4_prev_frm_qp[i4_scene_num][i4_i] = i4_avg_qp;
2386
55.6k
                ps_rate_control_api->ai4_prev_frm_qp_q6[i4_scene_num][i4_i] = i4_avg_qp_q6;
2387
55.6k
            }
2388
62.5k
        }
2389
6.95k
    }
2390
2391
205k
    if((!i4_suppress_bpic_update))
2392
205k
    {
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
205k
    }
2399
2400
    /* Update the variable which denotes that a frame has been encountered */
2401
205k
    ps_rate_control_api->u1_is_first_frm = 0;
2402
2403
    /* Store the prev encoded picture type for restricting Qp swing */
2404
205k
    if((e_pic_type == I_PIC) || (e_pic_type == P_PIC))
2405
177k
    {
2406
177k
        ps_rate_control_api->prev_ref_pic_type = e_pic_type;
2407
177k
    }
2408
2409
205k
    return;
2410
205k
}
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
20.0k
{
2424
20.0k
    update_prev_frame_intra_sad(ps_rate_control_api->ps_est_sad, i4_intra_frame_sad);
2425
20.0k
}
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
20.0k
{
2436
20.0k
    return get_prev_frame_intra_sad(ps_rate_control_api->ps_est_sad);
2437
20.0k
}
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
43.0k
{
2450
43.0k
    update_prev_frame_sad(ps_rate_control_api->ps_est_sad, i4_frame_sad, e_pic_type);
2451
43.0k
}
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
43.0k
{
2462
43.0k
    return get_prev_frame_sad(ps_rate_control_api->ps_est_sad, e_pic_type);
2463
43.0k
}
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
24.7k
{
2554
24.7k
    WORD32 i4_bits_to_stuff;
2555
    /* Get the CBR bits to stuff*/
2556
24.7k
    i4_bits_to_stuff =
2557
24.7k
        get_cbr_bits_to_stuff(ps_rate_control_api->ps_cbr_buffer, i4_tot_consumed_bits, e_pic_type);
2558
24.7k
    return i4_bits_to_stuff;
2559
24.7k
}
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
106k
{
2676
106k
    sad_acc_put_sad(ps_rate_control_api->ps_sad_acc, i4_cur_intra_sad, i4_cur_sad, i4_cur_pic_type);
2677
106k
}
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
177k
{
2688
177k
    sad_acc_get_sad(ps_rate_control_api->ps_sad_acc, pi4_sad);
2689
177k
}
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
4.48k
{
2722
4.48k
    int i;
2723
2724
4.48k
    if(ps_rate_control_api->e_rc_type != CONST_QP)
2725
4.48k
    {
2726
4.48k
        if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
2727
1.72k
        {
2728
1.72k
            ps_rate_control_api->au4_new_peak_bit_rate[0] = u4_average_bit_rate;
2729
1.72k
            ps_rate_control_api->au4_new_peak_bit_rate[1] = u4_average_bit_rate;
2730
1.72k
        }
2731
2.76k
        else
2732
2.76k
        {
2733
2.76k
            ps_rate_control_api->au4_new_peak_bit_rate[0] = u4_peak_bit_rate;
2734
2.76k
            ps_rate_control_api->au4_new_peak_bit_rate[1] = u4_peak_bit_rate;
2735
2.76k
        }
2736
        /* Bit Allocation Module: distribute the excess/deficit bits between the
2737
        old and the new frame rate to all the remaining frames */
2738
4.48k
        change_remaining_bits_in_period(
2739
4.48k
            ps_rate_control_api->ps_bit_allocation,
2740
4.48k
            u4_average_bit_rate,
2741
4.48k
            ba_get_frame_rate(ps_rate_control_api->ps_bit_allocation),
2742
4.48k
            (WORD32 *)(ps_rate_control_api->au4_new_peak_bit_rate));
2743
4.48k
    }
2744
    //if(ps_rate_control_api->e_rc_type == CBR_NLDRC)
2745
4.48k
    {
2746
4.48k
        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
13.4k
        for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
2751
8.97k
        {
2752
8.97k
            u4_average_bit_rate_copy[i] = u4_average_bit_rate;
2753
8.97k
        }
2754
4.48k
        change_cbr_vbv_bit_rate(
2755
4.48k
            ps_rate_control_api->ps_cbr_buffer,
2756
4.48k
            (WORD32 *)(u4_average_bit_rate_copy),
2757
4.48k
            (WORD32)ps_rate_control_api->au4_new_peak_bit_rate[0]);
2758
4.48k
    }
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
4.48k
    if(ps_rate_control_api->u1_is_first_frm == 0)
2768
542
    {
2769
5.42k
        for(i = 0; i < MAX_PIC_TYPE; i++)
2770
4.87k
        {
2771
            /*This also makes sure the qp swing restrictions wont be applied at boundary of bitrate change*/
2772
4.87k
            ps_rate_control_api->au1_avg_bitrate_changed[i] = 1;
2773
4.87k
        }
2774
542
    }
2775
4.48k
}
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
4.48k
{
2939
4.48k
    return (ba_get_bit_rate(ps_rate_control_api->ps_bit_allocation));
2940
4.48k
}
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
242k
{
2963
242k
    return (pic_type_get_intra_frame_interval(ps_rate_control_api->ps_pic_handling));
2964
242k
}
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
136k
{
2975
136k
    return (pic_type_get_inter_frame_interval(ps_rate_control_api->ps_pic_handling));
2976
136k
}
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
4.48k
{
3103
4.48k
    return (get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer));
3104
4.48k
}
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
7.06k
{
3128
7.06k
    set_cbr_ebf(ps_rate_control_api->ps_cbr_buffer, i32_init_ebf);
3129
7.06k
}
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
386k
{
3150
386k
    float f_trial_q_scale;
3151
386k
    WORD32 i4_tex_bits = 0, i4_header_bits = 0;
3152
386k
    WORD32 error = 0, min_error = 0x7FFFFFFF, i4_is_high_bitrate = 0;
3153
386k
    double *model_coeff, min_error_q_scale = (double)127;
3154
386k
    double min_scd_qscale, max_scd_q_scale;
3155
386k
    WORD32 i4_QP, i4_max_Qp, i4_min_Qp, i4_qp_selection_flag = 0;
3156
386k
    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
386k
    float coeff_a, coeff_b, coeff_c, coeff_d, X, tex_bpp;
3163
386k
    float min_qp_qscale_multiplier =
3164
386k
        1; /*For fade-in fade-out case where scene starts with blank frame have higher min frame qp*/
3165
    //float head_per;
3166
386k
    float normal_satd_act;
3167
386k
    float bpp = (float)get_bits_per_frame(ps_rate_control_api->ps_bit_allocation) / i4_num_pixel;
3168
3169
386k
    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
386k
    else
3179
386k
    {
3180
386k
        if(bpp > 0.16) /*10mbps 1080 30p*/
3181
385k
            i4_is_high_bitrate = 1;
3182
1.43k
        else if(bpp > 0.08)
3183
1.09k
            i4_is_high_bitrate = 2;
3184
343
        else if(bpp > 0.04)
3185
171
            i4_is_high_bitrate = 3;
3186
386k
    }
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
386k
    if(f_i_to_average_rest < I_TO_REST_VVFAST &&
3193
16.0k
       (i4_is_high_bitrate !=
3194
16.0k
        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
386k
    min_scd_qscale = pow(2, (double)(ps_rate_control_api->u4_min_scd_hevc_qp - 4) / 6) *
3223
386k
                     min_qp_qscale_multiplier;
3224
386k
    max_scd_q_scale = pow(2, (double)(SCD_MAX_HEVC_QP - 4) / 6);
3225
386k
    i4_max_Qp = MAX_HEVC_QP;
3226
386k
    i4_min_Qp = ps_rate_control_api->u4_min_scd_hevc_qp;
3227
386k
    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
386k
    normal_satd_act = (float)i8_satd_by_act_accum / i4_num_pixel;
3236
3237
386k
    {
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
386k
        float f_satd_by_Act_norm = GET_L0_SATD_BY_ACT_MAX_PER_PIXEL(i4_num_pixel);
3247
386k
        float f_weigh_factor = 0.0f;
3248
386k
        f_satd_by_Act_norm = f_satd_by_Act_norm * 0.75f;
3249
386k
        f_weigh_factor = GET_WEIGH_FACTOR_FOR_MIN_SCD_Q_SCALE(normal_satd_act, f_satd_by_Act_norm);
3250
386k
        CLIP(f_weigh_factor, 1.0f, 1.0f / MULT_FACTOR_SATD);
3251
386k
        min_scd_qscale = min_scd_qscale * f_weigh_factor;
3252
386k
        CLIP(min_scd_qscale, max_scd_q_scale, 1);
3253
386k
    }
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
386k
    model_coeff = (double *)offline_model_coeff;
3288
386k
    coeff_a = (float)model_coeff[0];
3289
386k
    coeff_b = (float)model_coeff[1];
3290
386k
    coeff_c = (float)model_coeff[2];
3291
386k
    coeff_d = (float)model_coeff[3];
3292
15.7M
    for(i4_QP = i4_min_Qp; i4_QP < i4_max_Qp; i4_QP++)
3293
15.3M
    {
3294
        /*needs to use the array for qp to qscale */
3295
3296
15.3M
        f_trial_q_scale = (float)(pow(2.0, (i4_QP - 4.0) / 6.0));
3297
        /* curve fit for texture bits*/
3298
15.3M
        X = (float)normal_satd_act / f_trial_q_scale;
3299
15.3M
        tex_bpp = ((coeff_a * X * X * X) + (coeff_b * X * X) + (coeff_c * X) + coeff_d);
3300
15.3M
        if(tex_bpp < (float)((1 << 30)) / i4_num_pixel)
3301
15.3M
            i4_tex_bits = (tex_bpp * i4_num_pixel);
3302
1.89k
        else
3303
1.89k
            i4_tex_bits = (1 << 30);
3304
15.3M
        i4_header_bits = 0;
3305
15.3M
        if(i4_tex_bits > 0)
3306
5.99M
        {
3307
            /*QP increase can't cause increase in bits*/
3308
5.99M
            if(i4_prev_best != -1 && (i4_tex_bits > i4_prev_best))
3309
56.0k
            {
3310
56.0k
                min_error = 0x7FFFFFFF;
3311
56.0k
                i4_qp_selection_flag = 0;
3312
56.0k
            }
3313
            /*consider texture bits to get header bits using obtained header percentage. Using header bits on overall bits targetted might not be correct*/
3314
5.99M
            error = i4_total_bits - (i4_tex_bits + i4_header_bits);
3315
5.99M
            if(abs(error) < abs(min_error))
3316
397k
            {
3317
397k
                min_error = error;
3318
397k
                min_error_q_scale = f_trial_q_scale;
3319
397k
                i4_qp_selection_flag = 1;
3320
397k
                i4_prev_best = i4_tex_bits;
3321
397k
            }
3322
5.99M
        }
3323
15.3M
    }
3324
386k
    if(!i4_qp_selection_flag)
3325
55.5k
    {
3326
55.5k
        min_error_q_scale = (WORD32)(min_scd_qscale + 0.5);
3327
55.5k
    }
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
386k
    CLIP(min_error_q_scale, (WORD32)(max_scd_q_scale + 0.5), (WORD32)(min_scd_qscale + .5));
3333
386k
    return ((WORD32)(min_error_q_scale * (1 << QSCALE_Q_FAC_3)));
3334
386k
}
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
386k
{
3368
386k
    WORD32 i4_qs_q3, i4_buf_based_min_bits, i4_buf_based_max_bits, i4_cur_est_tot_bits,
3369
386k
        i4_est_texture_bits, i4_get_error = 0;
3370
386k
    float f_bits_ratio;
3371
3372
386k
    assign_complexity_coeffs(ps_rate_control_api->ps_bit_allocation, af_sum_weigh);
3373
3374
386k
    i4_cur_est_tot_bits = get_scene_change_tot_frm_bits(
3375
386k
        ps_rate_control_api->ps_bit_allocation,
3376
386k
        ps_rate_control_api->ps_pic_handling,
3377
386k
        ps_rate_control_api->ps_cbr_buffer,
3378
386k
        i4_num_pels_in_frame,
3379
386k
        i4_f_sim_lap_avg,
3380
386k
        i_to_avg_ratio,
3381
386k
        i4_call_type,
3382
386k
        i4_is_not_an_I_pic,
3383
386k
        ps_rate_control_api->i4_is_infinite_gop);
3384
386k
    if(i4_call_type == 1)
3385
236k
    {
3386
236k
        *pi4_estimate_to_calc_frm_error = i4_cur_est_tot_bits;
3387
236k
    }
3388
3389
    /* vbv buffer position based error correction to keep away encoder buffer overflow at layer 0 pictures*/
3390
386k
    if(e_pic_type == I_PIC || e_pic_type == P_PIC || e_pic_type == P1_PIC)
3391
386k
    {
3392
386k
        WORD32 i4_cur_ebf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer);
3393
386k
        WORD32 i4_vbv_size = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
3394
386k
        WORD32 i4_max_ebf = (WORD32)(i4_vbv_size * MAX_THRESHOLD_VBV_FRM_ERROR);
3395
386k
        WORD32 i4_drain_rate = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
3396
386k
        WORD32 i4_total_bits_allocted = i4_cur_est_tot_bits;
3397
386k
        WORD32 i4_total_bits_to_be_alloc;
3398
386k
        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
386k
        if(i4_expected_ebf > i4_max_ebf)
3402
23.8k
        {
3403
23.8k
            i4_total_bits_to_be_alloc =
3404
23.8k
                MAX(i4_drain_rate, (i4_total_bits_allocted - (i4_expected_ebf - i4_max_ebf)));
3405
23.8k
            i4_cur_est_tot_bits = i4_total_bits_to_be_alloc;
3406
23.8k
        }
3407
386k
    }
3408
386k
    if(i4_call_type == 1)
3409
236k
    {
3410
236k
        i4_get_error = rc_get_estimate_bit_error(ps_rate_control_api);
3411
236k
    }
3412
386k
    if(i4_est_I_pic_head_bits != -1)
3413
    /*get constraints from buffer*/
3414
319k
    {
3415
319k
        get_min_max_bits_based_on_buffer(
3416
319k
            ps_rate_control_api,
3417
319k
            e_pic_type,
3418
319k
            &i4_buf_based_min_bits,
3419
319k
            &i4_buf_based_max_bits,
3420
319k
            i4_get_error);
3421
319k
        if(i4_cur_est_tot_bits > i4_buf_based_max_bits)
3422
0
            i4_cur_est_tot_bits = i4_buf_based_max_bits;
3423
319k
        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
319k
    }
3426
386k
    if(i4_est_I_pic_head_bits <
3427
386k
       0)  //indicates header bits data is not available. Assume default ratio
3428
67.4k
    {
3429
67.4k
        i4_est_texture_bits = (i4_cur_est_tot_bits * DEFAULT_TEX_PERCENTAGE_Q5) >> 5;
3430
67.4k
        i4_est_I_pic_head_bits = i4_cur_est_tot_bits - i4_est_texture_bits;
3431
67.4k
    }
3432
386k
    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
386k
    *pi4_tot_bits_estimated = i4_cur_est_tot_bits;
3436
3437
386k
    if(i4_true_scd)
3438
236k
    {
3439
        /*texture bits should be atleast 25% of header bits*/
3440
236k
        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
236k
        ps_rate_control_api->i4_scd_I_frame_estimated_tot_bits = i4_cur_est_tot_bits;
3444
236k
    }
3445
3446
    /* Get qp for scene cut frame based on offline generated data*/
3447
3448
386k
    i4_qs_q3 = rc_get_qp_scene_change_bits(
3449
386k
        ps_rate_control_api,
3450
386k
        (i4_cur_est_tot_bits - i4_est_I_pic_head_bits),
3451
386k
        i8_satd_act_accum,
3452
386k
        i4_num_pels_in_frame,
3453
386k
        offline_model_coeff,
3454
386k
        i_to_avg_ratio,
3455
386k
        i4_call_type);
3456
3457
386k
    if(i4_call_type)
3458
236k
        trace_printf(
3459
236k
            "i4_qp %d, i8_satd_act_accum %I64d,i_to_avg_ratio %f, "
3460
236k
            "i4_est_I_pic_head_bits %d i4_cur_est_tot_bits %d\n",
3461
236k
            i4_qp,
3462
236k
            i8_satd_act_accum,
3463
236k
            i_to_avg_ratio,
3464
236k
            i4_est_I_pic_head_bits,
3465
236k
            i4_cur_est_tot_bits);
3466
3467
386k
    *pi4_cur_est_tot_bits = i4_cur_est_tot_bits;
3468
3469
386k
    return (i4_qs_q3);
3470
386k
}
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
159k
{
3485
159k
    bit_allocation_set_num_scd_lap_window(
3486
159k
        ps_rate_control_api->ps_bit_allocation, i4_num_scd_in_lap_window, i4_num_frames_b4_scd);
3487
159k
}
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
99.1k
{
3510
99.1k
    bit_allocation_update_gop_level_bit_error(
3511
99.1k
        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
99.1k
    update_cbr_buf_mismatch_bit(ps_rate_control_api->ps_cbr_buffer, i4_error_bits);
3515
99.1k
}
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
76.2k
{
3530
76.2k
    update_estimate_status(
3531
76.2k
        ps_rate_control_api->ps_bit_allocation,
3532
76.2k
        i4_tex_bits,
3533
76.2k
        i4_hdr_bits,
3534
76.2k
        i4_est_text_bits_ctr_get_qp);
3535
3536
76.2k
    return i4_tex_bits;
3537
76.2k
}
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
85.1k
{
3554
85.1k
    WORD32 i4_cur_est_texture_bits, i4_cur_est_header_bits, i4_qp, i4_tot_bits,
3555
85.1k
        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
85.1k
    assign_complexity_coeffs(ps_rate_control_api->ps_bit_allocation, af_sum_weigh);
3563
85.1k
    i4_cur_est_header_bits =
3564
85.1k
        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
85.1k
    i4_tot_bits = get_scene_change_tot_frm_bits(
3568
85.1k
        ps_rate_control_api->ps_bit_allocation,
3569
85.1k
        ps_rate_control_api->ps_pic_handling,
3570
85.1k
        ps_rate_control_api->ps_cbr_buffer,
3571
85.1k
        i4_num_pels_in_frame,
3572
85.1k
        i4_f_sim_lap,
3573
85.1k
        (float)8.00,
3574
85.1k
        0,
3575
85.1k
        0,
3576
85.1k
        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
85.1k
    if(i4_call_type == 1)
3581
4.03k
    {
3582
4.03k
        get_min_max_bits_based_on_buffer(
3583
4.03k
            ps_rate_control_api, e_pic_type, &i4_buf_based_min_bits, &i4_buf_based_max_bits, 0);
3584
4.03k
        if(i4_tot_bits > i4_buf_based_max_bits)
3585
0
            i4_tot_bits = i4_buf_based_max_bits;
3586
4.03k
        if(i4_tot_bits < i4_buf_based_min_bits)
3587
661
            i4_tot_bits = i4_buf_based_min_bits;
3588
4.03k
    }
3589
    /*Assume 30 percent header bits*/
3590
85.1k
    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
85.1k
    i4_cur_est_header_bits = i4_tot_bits - i4_cur_est_texture_bits;
3594
3595
85.1k
    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
85.1k
    i4_qp = get_init_qp_using_pels_bits_per_frame(
3600
85.1k
        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
85.1k
    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
85.1k
    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
85.1k
    return (i4_qp);
3612
85.1k
}
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
2.36k
{
3623
2.36k
    reset_frm_rc_rd_model(ps_rate_control_api->aps_rd_model[pic_type]);
3624
2.36k
}
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
3.18k
{
3636
3.18k
    ps_rate_control_api->au1_is_first_frm_coded[pic_type] = 0;
3637
3.18k
}
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
82.6k
{
3653
82.6k
    WORD32 i4_est_tot_bits;
3654
3655
82.6k
    assign_complexity_coeffs(ps_rate_control_api->ps_bit_allocation, af_sum_weigh);
3656
3657
82.6k
    i4_est_tot_bits = get_scene_change_tot_frm_bits(
3658
82.6k
        ps_rate_control_api->ps_bit_allocation,
3659
82.6k
        ps_rate_control_api->ps_pic_handling,
3660
82.6k
        ps_rate_control_api->ps_cbr_buffer,
3661
82.6k
        i4_num_pixels,
3662
82.6k
        i4_fsim_lap,
3663
82.6k
        i_to_avg_ratio,
3664
82.6k
        0,
3665
82.6k
        0,
3666
82.6k
        ps_rate_control_api->i4_is_infinite_gop);
3667
    /*return header bits based on default percentage*/
3668
82.6k
    return (i4_est_tot_bits - ((i4_est_tot_bits * DEFAULT_TEX_PERCENTAGE_Q5) >> 5));
3669
82.6k
}
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
310k
{
3700
310k
    pic_type_get_frms_in_gop(ps_rate_control_api->ps_pic_handling, ai4_pic_type);
3701
310k
}
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
227k
{
3763
227k
    return (get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer));
3764
227k
}
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
7.06k
{
3905
7.06k
    WORD32 i, i4_error = 0x7FFFFFFF, i4_temp_error, i4_res_index = 0, i4_br_index = 0;
3906
7.06k
    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
7.06k
    float af_offline_hme_sad_per_pixel_480p[4][2] = {
3909
7.06k
        { 2.94f, 2.63f }, { 2.96f, 2.44f }, { 2.72f, 1.94f }, { 2.70f, 2.04f }
3910
7.06k
    };
3911
7.06k
    float af_offline_hme_sad_per_pixel_720p[4][2] = {
3912
7.06k
        { 3.37f, 2.97f }, { 3.35f, 2.77f }, { 3.18f, 2.40f }, { 2.94f, 1.83f }
3913
7.06k
    };
3914
7.06k
    float af_offline_hme_sad_per_pixel_1080p[4][2] = {
3915
7.06k
        { 3.24f, 2.78f }, { 3.17f, 2.46f }, { 2.91f, 1.98f }, { 2.75f, 1.65f }
3916
7.06k
    };
3917
7.06k
    float af_offline_hme_sad_per_pixel_2160p[4][2] = {
3918
7.06k
        { 2.56f, 2.11f }, { 2.47f, 1.92f }, { 2.19f, 1.46f }, { 2.00f, 1.21f }
3919
7.06k
    };
3920
3921
    /*Low BR or HBR is decided by comparing the bpp values as below*/
3922
7.06k
    float af_offline_bpp[4][2] = {
3923
7.06k
        { 0.30f, 0.09f }, { 0.25f, 0.06f }, { 0.16f, 0.04f }, { 0.12f, 0.02f }
3924
7.06k
    };
3925
3926
    /*Number of pixels in the picture for picking the closest resolution*/
3927
7.06k
    WORD32 ai4_pixels_res[4] = { 307200, 921600, 2073600, 8294400 };
3928
3929
7.06k
    float f_bpp =
3930
7.06k
        (float)get_bits_per_frame(ps_rate_control_api->ps_bit_allocation) / i4_total_pixels;
3931
7.06k
    float f_max_hme_sad_per_pixel;
3932
3933
7.06k
    i4_num_temporal_layers = ps_rate_control_api->i4_num_active_pic_type - 2;
3934
3935
7.06k
    CLIP(i4_num_temporal_layers, 3, 0);
3936
3937
    /*Pick the closest resolution based on error*/
3938
35.3k
    for(i = 0; i < 4; i++)
3939
28.2k
    {
3940
28.2k
        i4_temp_error = abs(i4_total_pixels - ai4_pixels_res[i]);
3941
3942
28.2k
        if(i4_temp_error < i4_error)
3943
7.23k
        {
3944
7.23k
            i4_error = i4_temp_error;
3945
7.23k
            i4_res_index = i;
3946
7.23k
        }
3947
28.2k
    }
3948
3949
    /*Decide whether LBR or HBR*/
3950
7.06k
    if((fabs(af_offline_bpp[i4_res_index][0] - f_bpp)) >
3951
7.06k
       (fabs(af_offline_bpp[i4_res_index][1] - f_bpp)))
3952
3.89k
    {
3953
3.89k
        i4_br_index = 1;
3954
3.89k
    }
3955
3.17k
    else
3956
3.17k
    {
3957
3.17k
        i4_br_index = 0;
3958
3.17k
    }
3959
3960
    /*After that pick the max hme sad*/
3961
7.06k
    switch(i4_res_index)
3962
7.06k
    {
3963
6.92k
    case 0:
3964
6.92k
        f_max_hme_sad_per_pixel =
3965
6.92k
            af_offline_hme_sad_per_pixel_480p[i4_num_temporal_layers][i4_br_index];
3966
6.92k
        break;
3967
108
    case 1:
3968
108
        f_max_hme_sad_per_pixel =
3969
108
            af_offline_hme_sad_per_pixel_720p[i4_num_temporal_layers][i4_br_index];
3970
108
        break;
3971
30
    case 2:
3972
30
        f_max_hme_sad_per_pixel =
3973
30
            af_offline_hme_sad_per_pixel_1080p[i4_num_temporal_layers][i4_br_index];
3974
30
        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
7.06k
    }
3984
3985
7.06k
    ps_rate_control_api->f_max_hme_sad_per_pixel = f_max_hme_sad_per_pixel;
3986
7.06k
}
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
2.57k
{
4139
2.57k
    return (ps_rate_control_api->prev_ref_pic_type);
4140
2.57k
}
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
13.1k
{
4218
13.1k
    rc_modify_est_tot(ps_rate_control_api, i4_tot_tex_bits);
4219
13.1k
}
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
76.2k
{
4235
76.2k
    *pi4_vbv_buffer_size = get_cbr_buffer_size(ps_rate_control_api->ps_cbr_buffer);
4236
76.2k
    *pi4_currEbf = get_cbr_ebf(ps_rate_control_api->ps_cbr_buffer) +
4237
76.2k
                   rc_get_estimate_bit_error(ps_rate_control_api);
4238
76.2k
    *pi4_maxEbf = get_cbr_max_ebf(ps_rate_control_api->ps_cbr_buffer);
4239
76.2k
    *pi4_drain_rate = get_buf_max_drain_rate(ps_rate_control_api->ps_cbr_buffer);
4240
76.2k
    return;
4241
76.2k
}