Coverage Report

Created: 2026-03-20 07:15

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