Coverage Report

Created: 2025-08-26 06:31

/src/libhevc/encoder/bit_allocation.c
Line
Count
Source (jump to first uncovered line)
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 bit_allocation.c
23
*
24
* \brief
25
*    This file contain bit processing 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 <string.h>
40
#include <stdlib.h>
41
#include <assert.h>
42
#include <stdarg.h>
43
#include <math.h>
44
45
/* User include files */
46
#include "ittiam_datatypes.h"
47
#include "mem_req_and_acq.h"
48
#include "rc_common.h"
49
#include "rc_cntrl_param.h"
50
#include "var_q_operator.h"
51
#include "fixed_point_error_bits.h"
52
#include "cbr_buffer_control.h"
53
#include "rc_rd_model.h"
54
#include "est_sad.h"
55
#include "cbr_buffer_control.h"
56
#include "picture_type.h"
57
#include "bit_allocation.h"
58
#include "trace_support.h"
59
#include "rc_frame_info_collector.h"
60
#include "rate_control_api.h"
61
62
/** Macros **/
63
654k
#define MIN(x, y) ((x) < (y)) ? (x) : (y)
64
0
#define MAX(x, y) ((x) < (y)) ? (y) : (x)
65
66
/* State structure for bit allocation */
67
typedef struct
68
{
69
    WORD32 i4_rem_bits_in_period;
70
    /* Storing inputs */
71
    WORD32 i4_tot_frms_in_gop;
72
    WORD32 i4_num_intra_frm_interval;
73
    WORD32 i4_bits_per_frm;
74
} rem_bit_in_prd_t;
75
76
typedef struct bit_allocation_t
77
{
78
    rem_bit_in_prd_t s_rbip;
79
    WORD32
80
        i2_K[MAX_PIC_TYPE]; /* A universal constant giving the relative complexity between pictures */
81
    WORD32 i4_prev_frm_header_bits[MAX_PIC_TYPE]; /* To get a estimate of the header bits consumed */
82
    WORD32 ai4_prev_frm_tot_bits[MAX_PIC_TYPE];
83
    WORD32 ai4_prev_frm_tot_est_bits[MAX_PIC_TYPE];
84
    WORD32 i4_bits_per_frm;
85
    WORD32 i4_num_gops_in_period;
86
    WORD32
87
    i4_actual_num_gops_in_period; /* Num gops as set by rate control module */
88
    WORD32 i4_saved_bits;
89
    WORD32 i4_max_bits_per_frm[MAX_NUM_DRAIN_RATES];
90
    WORD32 i4_min_bits_per_frm;
91
    /* Error bits module */
92
    error_bits_handle ps_error_bits;
93
    /* Storing frame rate */
94
    WORD32 i4_frame_rate;
95
    WORD32 i4_bit_rate;
96
    WORD32 ai4_peak_bit_rate[MAX_NUM_DRAIN_RATES];
97
    WORD32 i4_max_tex_bits_for_i;
98
    WORD32 i4_pels_in_frame;
99
    /* Errors within GOP and across GOP */
100
    WORD32 i4_gop_level_bit_error;
101
    WORD32 i4_frame_level_bit_error;
102
    WORD32 ai4_cur_frm_est_tex_bits[MAX_NUM_FRAME_PARALLEL];
103
    WORD32 ai4_cur_frm_est_hdr_bits[MAX_NUM_FRAME_PARALLEL];
104
    WORD32 i4_buffer_based_bit_error;
105
    WORD32 i4_bits_from_buffer_in_cur_gop;
106
    WORD32 i4_excess_bits_from_buffer;
107
108
    WORD32 i4_is_hbr;
109
    WORD32 i4_rem_frame_in_period;
110
    /*HEVC_RC : this will be updated by rc_interface.c to have number of SCD in lap window.
111
        Ni will be incremented using this to bring down buffer level and also back to back scd within lap window*/
112
    WORD32 i4_num_scd_in_lap_window;
113
    WORD32 i4_num_frm_b4_scd;
114
    WORD32 i4_num_active_pic_type;
115
    WORD32 i4_lap_window;
116
    WORD32 i4_field_pic;
117
    WORD32 i4_ba_rc_pass;
118
    void *pv_gop_stat;
119
    LWORD64 i8_cur_gop_num;
120
    LWORD64
121
    i8_frm_num_in_gop; /*TBD: For enc loop parallel this variable needs to maintained outside rate control since qp will not be queried in actual bitstream order*/
122
    float af_sum_weigh[MAX_PIC_TYPE][3];
123
    LWORD64 i8_cur_gop_bit_consumption; /*To calculate the deviaiton in 2 pass*/
124
    //LWORD64 i8_2pass_gop_error_accum;
125
    LWORD64
126
    i8_2pass_alloc_per_frm_bits; /*Per frame bits allocated to GOP in 2 pass*/
127
    //LWORD64 i8_2pass_alloc_per_frm_bits_next_gop;
128
129
    float f_min_complexity_cross_peak_rate; /*complexity of gop beyond which it is clipped to peak rate in 2ns pass*/
130
    WORD32 i4_next_sc_i_in_rc_look_ahead;
131
    /*The peak factor is multiplied to get the total bits for a gop based on squashing function*/
132
    float f_cur_peak_factor_2pass;
133
    LWORD64 i8_total_bits_allocated;
134
    WORD32 i4_luma_pels;
135
    WORD32 i4_num_gop;
136
    /*The bitrate will keep changing in 2 pass due to error*/
137
    LWORD64 i8_current_bitrate_2_pass;
138
    /*i4_flag_no_more_set_rbip - once we have reached the end of total number of frames to be encoded in
139
    2nd pass sliding window bit allocation there is no need to set rbip again*/
140
    WORD32 i4_flag_no_more_set_rbip;
141
    /*i8_vbv_based_excess_for_segment will be distributed across the complex segments depending on the
142
    ratio of current complexity to f_sum_complexity_segment_cross_peak*/
143
    float f_sum_complexity_segment_cross_peak;
144
    /*(i8_vbv_based_excess_for_segment)Buffer play excess is calculated for the entire segment of complex
145
    content which may consist of multiple gop's*/
146
    //LWORD64 i8_vbv_based_excess_for_segment;
147
    /*I frame bit allocation during scene cuts is dependent on f_curr_i_to_sum which will signal
148
    the complexity difference between current i to future i's if present in the same default gop*/
149
    float f_curr_i_to_sum;
150
    float f_curr_by_sum_subgop;
151
    WORD32 ai4_pic_types_in_subgop[MAX_PIC_TYPE];
152
    WORD32 i4_use_subgop_bit_alloc_flag;
153
    WORD32 i4_num_frames_since_last_I_frame;
154
    LWORD64 i8_first_pic_bits_pictype[MAX_PIC_TYPE];
155
    LWORD64 i8_avg_bits_pictype[MAX_PIC_TYPE];
156
    WORD32 i4_avg_qscale_gop_first_pass;
157
    WORD32 i4_fp_bit_alloc_in_sp;
158
    WORD32 i4_frame_level_error_ctr_update_rc;
159
    float f_qscale_max_clip_in_second_pass;
160
    float f_average_qscale_1st_pass;
161
    float f_max_average_qscale_1st_pass;
162
    LWORD64 i8_bit_consumption_so_far;
163
    WORD32 i4_total_2pass_frames;
164
    LWORD64 i8_2pass_avg_bit_rate;
165
    WORD32 i4_br_id;
166
} bit_allocation_t;
167
168
static WORD32 get_actual_num_frames_in_gop(pic_handling_handle ps_pic_handling)
169
458k
{
170
458k
    WORD32 i4_tot_frms_in_gop = 0, i;
171
458k
    WORD32 ai4_actual_frms_in_gop[MAX_PIC_TYPE];
172
458k
    memset(ai4_actual_frms_in_gop, 0, MAX_PIC_TYPE * sizeof(WORD32));
173
458k
    pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_actual_frms_in_gop);
174
4.58M
    for(i = 0; i < MAX_PIC_TYPE; i++)
175
4.13M
    {
176
4.13M
        i4_tot_frms_in_gop += ai4_actual_frms_in_gop[i];
177
4.13M
    }
178
458k
    return (i4_tot_frms_in_gop);
179
458k
}
180
181
float get_cur_peak_factor_2pass(bit_allocation_t *ps_bit_allocation)
182
0
{
183
0
    return (ps_bit_allocation->f_cur_peak_factor_2pass);
184
0
}
185
float get_cur_min_complexity_factor_2pass(bit_allocation_t *ps_bit_allocation)
186
0
{
187
0
    return (ps_bit_allocation->f_min_complexity_cross_peak_rate);
188
0
}
189
void set_2pass_total_gops(bit_allocation_t *ps_bit_allocation, WORD32 i4_num_gop)
190
0
{
191
0
    ps_bit_allocation->i4_num_gop = i4_num_gop;
192
0
}
193
#if NON_STEADSTATE_CODE
194
/* Module for accessing remaining bits in period */
195
/*****************************************************************************
196
  Function Name : init_rbip
197
  Description   : Initalises the remaining bits in period structure.
198
  Inputs        : ps_rbip         -remaining bits in period structure
199
                  ps_pic_handling - Pic handling structure
200
                  num_intra_frm_interval - num of I frm intervals in this period
201
                  i4_bits_per_frm - num bits per frm
202
Revision History:
203
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
204
*****************************************************************************/
205
static void init_rbip(
206
    rem_bit_in_prd_t *ps_rbip,
207
    pic_handling_handle ps_pic_handling,
208
    WORD32 i4_bits_per_frm,
209
    WORD32 i4_num_intra_frm_interval)
210
4.39k
{
211
4.39k
    WORD32 i4_tot_frms_in_gop = get_actual_num_frames_in_gop(ps_pic_handling);
212
    /* WORD32 i4_frm_correction_for_open_gop = 0; */
213
    /* If the GOP is open, then we need to subtract the num_b_frames for the first gop */
214
    /*if(!pic_type_is_gop_closed(ps_pic_handling))
215
    {
216
        i4_frm_correction_for_open_gop = pic_type_get_inter_frame_interval(ps_pic_handling)-1;
217
    }*/
218
4.39k
    ps_rbip->i4_rem_bits_in_period =
219
4.39k
        i4_bits_per_frm *
220
4.39k
        (i4_tot_frms_in_gop * i4_num_intra_frm_interval /*- i4_frm_correction_for_open_gop*/);
221
222
    /* Store the total number of frames in GOP value which is
223
     * used from module A */
224
4.39k
    ps_rbip->i4_tot_frms_in_gop = i4_tot_frms_in_gop;
225
4.39k
    ps_rbip->i4_num_intra_frm_interval = i4_num_intra_frm_interval;
226
4.39k
    ps_rbip->i4_bits_per_frm = i4_bits_per_frm;
227
4.39k
}
228
#endif /* #if NON_STEADSTATE_CODE */
229
230
/*****************************************************************************
231
  Function Name : check_update_rbip
232
  Description   : Function for updating rbip.
233
  Inputs        : ps_rbip         -remaining bits in period structure
234
                  ps_pic_handling - Pic handling structure
235
Revision History:
236
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
237
*****************************************************************************/
238
static void check_update_rbip(rem_bit_in_prd_t *ps_rbip, pic_handling_handle ps_pic_handling)
239
454k
{
240
    /* NOTE: Intra frame interval changes aafter the first I frame that is encoded in a GOP */
241
454k
    WORD32 i4_new_tot_frms_in_gop = get_actual_num_frames_in_gop(ps_pic_handling);
242
454k
    if(i4_new_tot_frms_in_gop != ps_rbip->i4_tot_frms_in_gop)
243
0
    {
244
0
        WORD32 i4_num_frames_in_period = ps_rbip->i4_num_intra_frm_interval *
245
0
                                         (i4_new_tot_frms_in_gop - ps_rbip->i4_tot_frms_in_gop);
246
0
        overflow_avoided_summation(
247
0
            &ps_rbip->i4_rem_bits_in_period, (ps_rbip->i4_bits_per_frm * i4_num_frames_in_period));
248
0
    }
249
    /* Updated the new values */
250
454k
    ps_rbip->i4_tot_frms_in_gop = i4_new_tot_frms_in_gop;
251
454k
}
252
/*****************************************************************************
253
  Function Name : ret_rbip_default_preenc
254
  Description   : Function for calculating bits in period.
255
  Inputs        : ps_rbip         -remaining bits in period structure
256
                  ps_pic_handling - Pic handling structure
257
Revision History:
258
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
259
*****************************************************************************/
260
static WORD32
261
    ret_rbip_default_preenc(rem_bit_in_prd_t *ps_rbip, pic_handling_handle ps_pic_handling)
262
671k
{
263
671k
    WORD32 i4_bits_in_period =
264
671k
        pic_type_get_intra_frame_interval(ps_pic_handling) * ps_rbip->i4_bits_per_frm;
265
671k
    return (i4_bits_in_period);
266
671k
}
267
/*****************************************************************************
268
  Function Name : update_rbip
269
  Description   : Function for updating rbip.
270
  Inputs        : ps_rbip         -remaining bits in period structure
271
                  ps_pic_handling - Pic handling structure
272
Revision History:
273
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
274
*****************************************************************************/
275
static WORD32 update_rbip(
276
    rem_bit_in_prd_t *ps_rbip, pic_handling_handle ps_pic_handling, WORD32 i4_num_of_bits)
277
454k
{
278
454k
    check_update_rbip(ps_rbip, ps_pic_handling);
279
454k
    overflow_avoided_summation(&ps_rbip->i4_rem_bits_in_period, i4_num_of_bits);
280
454k
    return (ps_rbip->i4_rem_bits_in_period);
281
454k
}
282
/*****************************************************************************
283
  Function Name : get_rbip_and_num_frames
284
  Description   : Update rbip and get number of frames.
285
  Inputs        : ps_rbip         -remaining bits in period structure
286
                  ps_pic_handling - Pic handling structure
287
                  pi4_num_frames  - pointer to update number of frmes
288
Revision History:
289
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
290
*****************************************************************************/
291
static LWORD64 get_rbip_and_num_frames(
292
    rem_bit_in_prd_t *ps_rbip,
293
    pic_handling_handle ps_pic_handling,
294
    WORD32 i4_num_of_bits,
295
    WORD32 *pi4_num_frames)
296
0
{
297
0
    check_update_rbip(ps_rbip, ps_pic_handling);
298
0
    overflow_avoided_summation(&ps_rbip->i4_rem_bits_in_period, i4_num_of_bits);
299
0
    *pi4_num_frames = ps_rbip->i4_tot_frms_in_gop;
300
0
    return (ps_rbip->i4_rem_bits_in_period);
301
0
}
302
/*****************************************************************************
303
  Function Name : set_rbip
304
  Description   : Set rbip
305
  Inputs        : ps_rbip         -remaining bits in period structure
306
                  i4_error_bits   - Error bits
307
Revision History:
308
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
309
*****************************************************************************/
310
static WORD32 set_rbip(rem_bit_in_prd_t *ps_rbip, WORD32 i4_error_bits)
311
0
{
312
0
    ps_rbip->i4_rem_bits_in_period = (ps_rbip->i4_bits_per_frm * ps_rbip->i4_tot_frms_in_gop *
313
0
                                      ps_rbip->i4_num_intra_frm_interval) +
314
0
                                     i4_error_bits;
315
316
0
    return ps_rbip->i4_rem_bits_in_period;
317
0
}
318
319
/*****************************************************************************
320
  Function Name : multi_pass_set_rbip
321
  Description   : 2 pass set RBIP, since the gop bits shall not depend on bitrate or framerate,
322
                  GOP bits is directly obtained from first pass data
323
  Inputs        : ps_rbip         -remaining bits in period structure
324
                  ps_pic_handling - Pic handling structure
325
                  i4_cur_gop_bits - bits allocated for the curr gop
326
                  i4_tot_frm_in_cur_gop - frames in the gop
327
Revision History:
328
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
329
*****************************************************************************/
330
static void multi_pass_set_rbip(
331
    rem_bit_in_prd_t *ps_rbip,
332
    pic_handling_handle ps_pic_handling,
333
    WORD32 i4_cur_gop_bits,
334
    WORD32 i4_tot_frm_in_cur_gop)
335
0
{
336
0
    WORD32 i4_num_frames_in_gop = get_actual_num_frames_in_gop(ps_pic_handling);
337
0
    ps_rbip->i4_rem_bits_in_period =
338
0
        (WORD32)((LWORD64)i4_cur_gop_bits * i4_num_frames_in_gop / i4_tot_frm_in_cur_gop);
339
0
    ps_rbip->i4_tot_frms_in_gop = i4_num_frames_in_gop;
340
0
    ps_rbip->i4_bits_per_frm = ps_rbip->i4_rem_bits_in_period / i4_num_frames_in_gop;
341
0
}
342
static void change_rbip(
343
    rem_bit_in_prd_t *ps_rbip, WORD32 i4_new_bits_per_frm, WORD32 i4_new_num_intra_frm_interval)
344
4.88k
{
345
4.88k
    if(i4_new_bits_per_frm != ps_rbip->i4_bits_per_frm)
346
4.88k
    {
347
4.88k
        WORD32 i4_rem_frms_in_period =
348
4.88k
            (ps_rbip->i4_num_intra_frm_interval) * ps_rbip->i4_tot_frms_in_gop;
349
4.88k
        overflow_avoided_summation(
350
4.88k
            &ps_rbip->i4_rem_bits_in_period,
351
4.88k
            ((i4_new_bits_per_frm - ps_rbip->i4_bits_per_frm) * i4_rem_frms_in_period));
352
4.88k
    }
353
4.88k
    if(i4_new_num_intra_frm_interval != ps_rbip->i4_num_intra_frm_interval)
354
0
    {
355
0
        overflow_avoided_summation(
356
0
            &ps_rbip->i4_rem_bits_in_period,
357
0
            (i4_new_bits_per_frm * ps_rbip->i4_tot_frms_in_gop *
358
0
             (i4_new_num_intra_frm_interval - ps_rbip->i4_num_intra_frm_interval)));
359
0
    }
360
    /* Update the new value */
361
4.88k
    ps_rbip->i4_num_intra_frm_interval = i4_new_num_intra_frm_interval;
362
4.88k
    ps_rbip->i4_bits_per_frm = i4_new_bits_per_frm;
363
4.88k
}
364
365
#if NON_STEADSTATE_CODE
366
WORD32 bit_allocation_num_fill_use_free_memtab(
367
    bit_allocation_t **pps_bit_allocation, itt_memtab_t *ps_memtab, ITT_FUNC_TYPE_E e_func_type)
368
53.1k
{
369
53.1k
    WORD32 i4_mem_tab_idx = 0;
370
53.1k
    static bit_allocation_t s_bit_allocation_temp;
371
372
    /* Hack for al alloc, during which we dont have any state memory.
373
      Dereferencing can cause issues */
374
53.1k
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
375
45.5k
        (*pps_bit_allocation) = &s_bit_allocation_temp;
376
377
    /*for src rate control state structure*/
378
53.1k
    if(e_func_type != GET_NUM_MEMTAB)
379
22.7k
    {
380
22.7k
        fill_memtab(
381
22.7k
            &ps_memtab[i4_mem_tab_idx],
382
22.7k
            sizeof(bit_allocation_t),
383
22.7k
            MEM_TAB_ALIGNMENT,
384
22.7k
            PERSISTENT,
385
22.7k
            DDR);
386
22.7k
        use_or_fill_base(&ps_memtab[0], (void **)pps_bit_allocation, e_func_type);
387
22.7k
    }
388
53.1k
    i4_mem_tab_idx++;
389
390
53.1k
    i4_mem_tab_idx += error_bits_num_fill_use_free_memtab(
391
53.1k
        &pps_bit_allocation[0]->ps_error_bits, &ps_memtab[i4_mem_tab_idx], e_func_type);
392
393
53.1k
    return (i4_mem_tab_idx);
394
53.1k
}
395
#endif /* #if NON_STEADSTATE_CODE */
396
397
/*****************************************************************************
398
  Function Name : get_bits_based_on_complexity
399
  Description   : function calculates the bits to be allocated for the current
400
                  picture type given the relative complexity between different
401
                  picture types
402
  Inputs        : i4_bits_in_period
403
                  pi4_frms_in_period      - num frames of each pictype in the period
404
                  pvq_complexity_estimate - complexity for each pictype
405
                  e_pic_type              - current picture type
406
                  i4_call_type
407
  Revision History:
408
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
409
*****************************************************************************/
410
static WORD32 get_bits_based_on_complexity(
411
    bit_allocation_t *ps_bit_allocation,
412
    WORD32 i4_bits_in_period,
413
    WORD32 *pi4_frms_in_period,
414
    number_t *pvq_complexity_estimate,
415
    picture_type_e e_pic_type,
416
    WORD32 i4_call_type)
417
319k
{
418
319k
    WORD32 i, i4_estimated_bits;
419
319k
    number_t vq_bits_in_period, vq_frms_in_period[MAX_PIC_TYPE], vq_comp_coeff,
420
319k
        vq_est_texture_bits_for_frm;
421
319k
    WORD32 i4_num_scd_in_LAP_window = ps_bit_allocation->i4_num_scd_in_lap_window;
422
319k
    WORD32 i4_active_pic_types = ps_bit_allocation->i4_num_active_pic_type,
423
319k
           i4_field_pic = ps_bit_allocation->i4_field_pic;
424
319k
    float af_sum_weigh[MAX_PIC_TYPE][3];
425
426
319k
    memmove(af_sum_weigh, ps_bit_allocation->af_sum_weigh, ((sizeof(float)) * MAX_PIC_TYPE * 3));
427
428
    /** Increment I frame count if there is scene cut in LAP window*/
429
319k
    i4_num_scd_in_LAP_window = 0;
430
319k
    pi4_frms_in_period[I_PIC] += i4_num_scd_in_LAP_window;
431
    /* Converting inputs to var_q format */
432
319k
    SET_VAR_Q(vq_bits_in_period, i4_bits_in_period, 0);
433
3.19M
    for(i = 0; i < MAX_PIC_TYPE; i++)
434
2.87M
    {
435
2.87M
        SET_VAR_Q(vq_frms_in_period[i], pi4_frms_in_period[i], 0);
436
2.87M
    }
437
    /******************************************************************
438
    Estimated texture bits =
439
            (remaining bits) * (cur frm complexity)
440
            ---------------------------------------
441
    (num_i_frm*i_frm_complexity) + (num_p_frm*pfrm_complexity) + (b_frm * b_frm_cm)
442
    *******************************************************************/
443
    /*Taking the numerator weight into account*/
444
319k
    if(i4_call_type == 1)
445
77.4k
    {
446
77.4k
        trace_printf("1 CUrr / avg %f", af_sum_weigh[e_pic_type][0]);
447
77.4k
    }
448
319k
    if(af_sum_weigh[e_pic_type][0] > 4.0f)
449
0
        af_sum_weigh[e_pic_type][0] = 4.0f;
450
319k
    if(af_sum_weigh[e_pic_type][0] < 0.3f)
451
0
        af_sum_weigh[e_pic_type][0] = 0.3f;
452
319k
    if(i4_call_type == 1)
453
77.4k
    {
454
77.4k
        trace_printf("2 CUrr / avg %f", af_sum_weigh[e_pic_type][0]);
455
77.4k
    }
456
457
319k
    if((ps_bit_allocation->i4_ba_rc_pass != 2) || (i4_call_type == 0) ||
458
319k
       (ps_bit_allocation->i4_fp_bit_alloc_in_sp == 0))
459
319k
    {
460
319k
        convert_float_to_fix(af_sum_weigh[e_pic_type][0], &vq_comp_coeff);
461
319k
        mult32_var_q(vq_bits_in_period, vq_comp_coeff, &vq_bits_in_period);
462
319k
        mult32_var_q(vq_bits_in_period, pvq_complexity_estimate[e_pic_type], &vq_bits_in_period);
463
319k
    }
464
0
    else
465
0
    {
466
0
        WORD32 i4_frame_num = (WORD32)ps_bit_allocation->i8_frm_num_in_gop, i4_offset;
467
0
        gop_level_stat_t *ps_gop;
468
0
        LWORD64 i8_firs_pass_tot_bits;
469
0
        float f_bits_cur_pic, f_offset;
470
0
        ps_gop =
471
0
            (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
472
0
        i8_firs_pass_tot_bits = ps_gop->ai8_tex_bits_consumed[i4_frame_num] +
473
0
                                ps_gop->ai8_head_bits_consumed[i4_frame_num];
474
0
        i4_offset = (ps_gop->ai4_q6_frame_offsets[i4_frame_num] * 1000) >> QSCALE_Q_FAC;
475
0
        f_offset = ((float)i4_offset) / 1000;
476
0
        f_bits_cur_pic =
477
0
            (float)(i8_firs_pass_tot_bits * ps_gop->ai4_first_pass_qscale[i4_frame_num]) /
478
0
            (ps_bit_allocation->i4_avg_qscale_gop_first_pass * f_offset);
479
0
        convert_float_to_fix(f_bits_cur_pic, &vq_comp_coeff);
480
0
        mult32_var_q(vq_bits_in_period, vq_comp_coeff, &vq_bits_in_period);
481
482
0
        for(i = 0; i < MAX_PIC_TYPE; i++)
483
0
        {
484
0
            number_t temp;
485
0
            convert_float_to_fix((float)ps_bit_allocation->i8_avg_bits_pictype[i], &temp);
486
0
            pvq_complexity_estimate[i] = temp;
487
0
        }
488
0
    }
489
490
3.19M
    for(i = 0; i < MAX_PIC_TYPE; i++)
491
2.87M
    {
492
        /*For 2nd pass we will be reducing the num of pictures as they are coded so we dont want to replace 0's*/
493
2.87M
        if(af_sum_weigh[i][1] == 0.0 &&
494
2.87M
           !((i4_call_type == 1) && (ps_bit_allocation->i4_ba_rc_pass == 2)))
495
2.87M
            af_sum_weigh[i][1] = (float)pi4_frms_in_period[i];
496
497
2.87M
        convert_float_to_fix(af_sum_weigh[i][1], &vq_comp_coeff);
498
2.87M
        mult32_var_q(vq_comp_coeff, pvq_complexity_estimate[i], &vq_frms_in_period[i]);
499
2.87M
    }
500
501
    /* changed the index range from active_pic to max_pic*/
502
319k
    if(i4_field_pic)
503
0
    {
504
0
        for(i = 1; i < i4_active_pic_types; i++)
505
0
        {
506
0
            add32_var_q(vq_frms_in_period[I_PIC], vq_frms_in_period[i], &vq_frms_in_period[I_PIC]);
507
0
            add32_var_q(
508
0
                vq_frms_in_period[I_PIC],
509
0
                vq_frms_in_period[i + FIELD_OFFSET],
510
0
                &vq_frms_in_period[I_PIC]);
511
0
        }
512
0
    }
513
319k
    else /*field case*/
514
319k
    {
515
821k
        for(i = 1; i < i4_active_pic_types; i++)
516
501k
        {
517
501k
            add32_var_q(vq_frms_in_period[I_PIC], vq_frms_in_period[i], &vq_frms_in_period[I_PIC]);
518
501k
        }
519
319k
    }
520
521
319k
    div32_var_q(vq_bits_in_period, vq_frms_in_period[I_PIC], &vq_est_texture_bits_for_frm);
522
319k
    number_t_to_word32(vq_est_texture_bits_for_frm, &i4_estimated_bits);
523
524
    /* If the number of frames are zero then return zero */
525
319k
    if(!pi4_frms_in_period[e_pic_type])
526
34.1k
        i4_estimated_bits = 0;
527
319k
    return (i4_estimated_bits);
528
319k
}
529
/*****************************************************************************
530
  Function Name : assign_complexity_coeffs
531
  Description   :
532
  Inputs        : af_sum_weigh
533
  Revision History:
534
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
535
*****************************************************************************/
536
void assign_complexity_coeffs(
537
    bit_allocation_t *ps_bit_allocation, float af_sum_weigh[MAX_PIC_TYPE][3])
538
931k
{
539
931k
    WORD32 i;
540
9.31M
    for(i = 0; i < MAX_PIC_TYPE; i++)
541
8.38M
    {
542
8.38M
        ps_bit_allocation->af_sum_weigh[i][0] = af_sum_weigh[i][0];
543
8.38M
        ps_bit_allocation->af_sum_weigh[i][1] = af_sum_weigh[i][1];
544
8.38M
        ps_bit_allocation->af_sum_weigh[i][2] = af_sum_weigh[i][2];
545
8.38M
    }
546
931k
}
547
/*****************************************************************************
548
  Function Name : ba_get_rbip_and_num_frames
549
  Description   :
550
  Inputs        : pi4_num_frames
551
  Revision History:
552
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
553
*****************************************************************************/
554
LWORD64 ba_get_rbip_and_num_frames(
555
    bit_allocation_t *ps_bit_allocation,
556
    pic_handling_handle ps_pic_handling,
557
    WORD32 *pi4_num_frames)
558
0
{
559
0
    return (
560
0
        get_rbip_and_num_frames(&ps_bit_allocation->s_rbip, ps_pic_handling, 0, pi4_num_frames));
561
0
}
562
/*****************************************************************************
563
  Function Name : init_prev_header_bits
564
  Description   : Intialise header bits for each pic type
565
  Inputs        : ps_bit_allocation
566
                  ps_pic_handling
567
  Revision History:
568
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
569
*****************************************************************************/
570
void init_prev_header_bits(bit_allocation_t *ps_bit_allocation, pic_handling_handle ps_pic_handling)
571
4.64k
{
572
4.64k
    WORD32 i4_rem_bits_in_period, /* ai4_rem_frms_in_period[MAX_PIC_TYPE], */
573
4.64k
        ai4_frms_in_period[MAX_PIC_TYPE], i, j;
574
4.64k
    number_t avq_complexity_estimate[MAX_PIC_TYPE];
575
4.64k
    WORD32 i4_field_pic;
576
4.64k
    i4_field_pic = pic_type_get_field_pic(ps_pic_handling);
577
    /* Assigning Percentages of I, P and B frame header bits based on actual bits allocated for I, P and B frames */
578
    /* Getting the remaining bits in period */
579
4.64k
    i4_rem_bits_in_period = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
580
581
    /* Hardcoding the bit ratios between I, P and B */
582
4.64k
    SET_VAR_Q(
583
4.64k
        avq_complexity_estimate[I_PIC],
584
4.64k
        (I_TO_P_BIT_RATIO * P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
585
4.64k
        0);
586
4.64k
    SET_VAR_Q(
587
4.64k
        avq_complexity_estimate[P_PIC],
588
4.64k
        (P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
589
4.64k
        0);
590
4.64k
    SET_VAR_Q(
591
4.64k
        avq_complexity_estimate[P1_PIC],
592
4.64k
        (P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
593
4.64k
        0);
594
4.64k
    SET_VAR_Q(avq_complexity_estimate[B_PIC], (B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO), 0);
595
4.64k
    SET_VAR_Q(avq_complexity_estimate[BB_PIC], (B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO), 0);
596
4.64k
    SET_VAR_Q(avq_complexity_estimate[B1_PIC], (B1_TO_B2_BIT_RATIO), 0);
597
4.64k
    SET_VAR_Q(
598
4.64k
        avq_complexity_estimate[B11_PIC],
599
4.64k
        (B1_TO_B2_BIT_RATIO),
600
4.64k
        0);  //temporarliy treat ref and non ref as same
601
4.64k
    SET_VAR_Q(avq_complexity_estimate[B2_PIC], 1, 0);
602
4.64k
    SET_VAR_Q(avq_complexity_estimate[B22_PIC], 1, 0);
603
604
    /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
605
    /* pic_type_get_rem_frms_in_gop(ps_pic_handling, ai4_rem_frms_in_period); */
606
4.64k
    pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_period);
607
608
    /* Depending on the number of gops in a period, find the num_frms_in_prd */
609
46.4k
    for(j = 0; j < MAX_PIC_TYPE; j++)
610
41.7k
    {
611
41.7k
        ai4_frms_in_period[j] = (ai4_frms_in_period[j] * ps_bit_allocation->i4_num_gops_in_period);
612
41.7k
    }
613
614
    /* Percentage of header bits in teh overall bits allocated to I, P and B frames
615
    when the data is not known. Since this value is based on bitrate using a equation
616
    to fit the values. Ran the header bit ratio for [1080@30] carnival, ihits and
617
    football at 9, 12 and 16 mbps and based on that deriving a equation using bpp.
618
    Values obtained are:
619
    (bitrate/bpp)   I              P              B
620
    9/2.87          0.358617291155 0.549124350786 0.798772545232
621
    12/3.83         0.288633642796 0.444797334749 0.693933711801
622
    16/5.11         0.284241839623 0.330152764298 0.557999732549
623
    Equation for I:
624
        if bpp > 3.83 hdr = 0.29
625
        else hdr = -0.073*bpp + 0.569
626
    Equation for P: hdr = -0.098*bpp + 0.825
627
    Equation for B: hdr = -0.108*bpp + 1.108
628
    */
629
4.64k
    {
630
52.0k
#define FRAME_HEADER_BITS_Q_FACTOR (10)
631
4.64k
        WORD32 ai4_header_bits_percentage[MAX_PIC_TYPE];
632
633
4.64k
        WORD32 i4_bpp;
634
4.64k
        X_PROD_Y_DIV_Z(
635
4.64k
            ps_bit_allocation->i4_bits_per_frm,
636
4.64k
            (1 << FRAME_HEADER_BITS_Q_FACTOR),
637
4.64k
            ps_bit_allocation->i4_pels_in_frame,
638
4.64k
            i4_bpp);
639
        //ps_bit_allocation->i4_bits_per_frm*(1<<FRAME_HEADER_BITS_Q_FACTOR)/ps_bit_allocation->i4_pels_in_frame;
640
641
4.64k
        if(i4_bpp > 131)
642
3.69k
            ai4_header_bits_percentage[I_PIC] = 297;
643
949
        else
644
949
            ai4_header_bits_percentage[I_PIC] =
645
949
                ((-2238 * i4_bpp) >> FRAME_HEADER_BITS_Q_FACTOR) + 583;
646
4.64k
        ai4_header_bits_percentage[P_PIC] = ((-2990 * i4_bpp) >> FRAME_HEADER_BITS_Q_FACTOR) + 845;
647
648
4.64k
        ai4_header_bits_percentage[B_PIC] = ((-3308 * i4_bpp) >> FRAME_HEADER_BITS_Q_FACTOR) + 1135;
649
650
        /* Changes for 2B subGOP */
651
4.64k
        ai4_header_bits_percentage[P_PIC] = (ai4_header_bits_percentage[P_PIC] * 13) >> 4;
652
4.64k
        ai4_header_bits_percentage[P1_PIC] = (ai4_header_bits_percentage[P_PIC] * 13) >> 4;
653
4.64k
        ai4_header_bits_percentage[B_PIC] = (ai4_header_bits_percentage[B_PIC] * 12) >> 4;
654
4.64k
        ai4_header_bits_percentage[BB_PIC] = (ai4_header_bits_percentage[B_PIC] * 12) >> 4;
655
        /*HEVC_hierarchy: temp change consider same percentage because of insufficient data*/
656
4.64k
        ai4_header_bits_percentage[B1_PIC] = ai4_header_bits_percentage[B_PIC];
657
4.64k
        ai4_header_bits_percentage[B11_PIC] = ai4_header_bits_percentage[B_PIC];
658
4.64k
        ai4_header_bits_percentage[B2_PIC] = ai4_header_bits_percentage[B_PIC];
659
4.64k
        ai4_header_bits_percentage[B22_PIC] = ai4_header_bits_percentage[B_PIC];
660
661
46.4k
        for(i = 0; i < MAX_PIC_TYPE; i++)
662
41.7k
        {
663
41.7k
            ps_bit_allocation->af_sum_weigh[i][0] = 1.0;
664
41.7k
            ps_bit_allocation->af_sum_weigh[i][1] = 0.0;
665
41.7k
            ps_bit_allocation->af_sum_weigh[i][2] = 0.0;
666
41.7k
        }
667
668
46.4k
        for(i = 0; i < MAX_PIC_TYPE; i++)
669
41.7k
        {
670
            /* Getting the total bits allocated for each picture type */
671
41.7k
            WORD32 i4_num_bits_allocated = get_bits_based_on_complexity(
672
41.7k
                ps_bit_allocation,
673
41.7k
                i4_rem_bits_in_period,
674
41.7k
                ai4_frms_in_period,
675
41.7k
                avq_complexity_estimate,
676
41.7k
                (picture_type_e)i,
677
41.7k
                0);
678
679
41.7k
            if(ai4_header_bits_percentage[i] < 0)
680
27.3k
                ai4_header_bits_percentage[i] = 0;
681
682
41.7k
            ps_bit_allocation->i4_prev_frm_header_bits[i] = (WORD32)(
683
41.7k
                ((LWORD64)ai4_header_bits_percentage[i] * i4_num_bits_allocated) >>
684
41.7k
                FRAME_HEADER_BITS_Q_FACTOR);
685
41.7k
        }
686
4.64k
    }
687
4.64k
}
688
689
/*****************************************************************************
690
  Function Name : init_bit_allocation
691
  Description   : Initalises the bit_allocation structure.
692
  Inputs        : intra_frm_interval - num frames between two I frames
693
                  num_intra_frm_interval - num such intervals
694
                  i4_bit_rate - num bits per second
695
                  i4_frm_rate - num frms in 1000 seconds
696
                  i4_num_active_pic_type
697
  Revision History:
698
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
699
*****************************************************************************/
700
#if NON_STEADSTATE_CODE
701
void init_bit_allocation(
702
    bit_allocation_t *ps_bit_allocation,
703
    pic_handling_handle ps_pic_handling,
704
    WORD32 i4_num_intra_frm_interval, /* num such intervals */
705
    WORD32 i4_bit_rate, /* num bits per second */
706
    WORD32 i4_frm_rate, /* num frms in 1000 seconds */
707
    WORD32 *i4_peak_bit_rate,
708
    WORD32 i4_min_bitrate, /* The minimum bit rate that is to be satisfied for a gop */
709
    WORD32 i4_pels_in_frame,
710
    WORD32 i4_is_hbr,
711
    WORD32 i4_num_active_pic_type,
712
    WORD32 i4_lap_window,
713
    WORD32 i4_field_pic,
714
    WORD32 rc_pass,
715
    WORD32 i4_luma_pels,
716
    WORD32 i4_fp_bit_alloc_in_sp)
717
4.39k
{
718
4.39k
    WORD32 i;
719
4.39k
    WORD32 i4_bits_per_frm, i4_max_bits_per_frm[MAX_NUM_DRAIN_RATES];
720
    /* Store the pels in frame value */
721
4.39k
    ps_bit_allocation->i4_pels_in_frame = i4_pels_in_frame;
722
4.39k
    ps_bit_allocation->i4_num_scd_in_lap_window = 0;
723
4.39k
    ps_bit_allocation->i4_num_frm_b4_scd = 0;
724
4.39k
    ps_bit_allocation->i4_num_active_pic_type = i4_num_active_pic_type;
725
4.39k
    ps_bit_allocation->i4_field_pic = i4_field_pic;
726
4.39k
    ps_bit_allocation->i4_ba_rc_pass = rc_pass;
727
4.39k
    ps_bit_allocation->i4_br_id = 0; /* 0 - peak, 1 - average*/
728
4.39k
    ps_bit_allocation->i8_cur_gop_num =
729
4.39k
        0; /*Will be incremented after first frame allocation is done(during init itslef)*/
730
4.39k
    ps_bit_allocation->i8_frm_num_in_gop = 0;
731
4.39k
    ps_bit_allocation->pv_gop_stat =
732
4.39k
        NULL; /*In 2 pass the gop stat pointer is set API parameter call*/
733
4.39k
    ps_bit_allocation->f_min_complexity_cross_peak_rate =
734
4.39k
        1.0; /*In single pass buffer based additional bits movement is disabled, hence set to max complexity
735
                                                                Reset to lower value in case of two pass*/
736
737
4.39k
    ps_bit_allocation->f_cur_peak_factor_2pass = -1.0;
738
4.39k
    ps_bit_allocation->i8_total_bits_allocated = -1;
739
4.39k
    ps_bit_allocation->i4_luma_pels = i4_luma_pels;
740
4.39k
    ps_bit_allocation->i4_num_gop = -1;
741
4.39k
    ps_bit_allocation->f_sum_complexity_segment_cross_peak = 0.0f;
742
    //ps_bit_allocation->i8_vbv_based_excess_for_segment = 0;
743
4.39k
    ps_bit_allocation->i4_flag_no_more_set_rbip = 0;
744
4.39k
    ps_bit_allocation->f_curr_i_to_sum = 1.0f;
745
4.39k
    ps_bit_allocation->i4_fp_bit_alloc_in_sp = i4_fp_bit_alloc_in_sp;
746
747
    /* Calculate the bits per frame */
748
4.39k
    X_PROD_Y_DIV_Z(i4_bit_rate, 1000, i4_frm_rate, i4_bits_per_frm);
749
13.1k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
750
8.78k
    {
751
8.78k
        X_PROD_Y_DIV_Z(i4_peak_bit_rate[i], 1000, i4_frm_rate, i4_max_bits_per_frm[i]);
752
8.78k
    }
753
    /* Initialize the bits_per_frame */
754
4.39k
    ps_bit_allocation->i4_bits_per_frm = i4_bits_per_frm;
755
13.1k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
756
8.78k
    {
757
8.78k
        ps_bit_allocation->i4_max_bits_per_frm[i] = i4_max_bits_per_frm[i];
758
8.78k
    }
759
4.39k
    X_PROD_Y_DIV_Z(i4_min_bitrate, 1000, i4_frm_rate, ps_bit_allocation->i4_min_bits_per_frm);
760
761
    /* Initialise the rem_bits in period
762
       The first gop in case of an OPEN GOP may have fewer B_PICs,
763
       That condition is not taken care of */
764
4.39k
    init_rbip(
765
4.39k
        &ps_bit_allocation->s_rbip, ps_pic_handling, i4_bits_per_frm, i4_num_intra_frm_interval);
766
767
    /* Initialize the num_gops_in_period */
768
4.39k
    ps_bit_allocation->i4_num_gops_in_period = i4_num_intra_frm_interval;
769
4.39k
    ps_bit_allocation->i4_actual_num_gops_in_period = i4_num_intra_frm_interval;
770
771
    /* Relative complexity between I and P frames */
772
4.39k
    ps_bit_allocation->i2_K[I_PIC] = (1 << K_Q);
773
4.39k
    ps_bit_allocation->i2_K[P_PIC] = I_TO_P_RATIO;
774
4.39k
    ps_bit_allocation->i2_K[P1_PIC] = I_TO_P_RATIO;
775
4.39k
    ps_bit_allocation->i2_K[B_PIC] = (P_TO_B_RATIO * I_TO_P_RATIO) >> K_Q;
776
4.39k
    ps_bit_allocation->i2_K[BB_PIC] = (P_TO_B_RATIO * I_TO_P_RATIO) >> K_Q;
777
778
    /*HEVC_hierarchy: force qp offset with one high level of hierarchy*/
779
4.39k
    ps_bit_allocation->i2_K[B1_PIC] = (B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q);
780
4.39k
    ps_bit_allocation->i2_K[B11_PIC] = (B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q);
781
4.39k
    ps_bit_allocation->i2_K[B2_PIC] =
782
4.39k
        (B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q + K_Q);
783
4.39k
    ps_bit_allocation->i2_K[B22_PIC] =
784
4.39k
        (B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q + K_Q);
785
786
    /* Initialize the saved bits to 0*/
787
4.39k
    ps_bit_allocation->i4_saved_bits = 0;
788
789
    /* Update the error bits module with average bits */
790
4.39k
    init_error_bits(ps_bit_allocation->ps_error_bits, i4_frm_rate, i4_bit_rate);
791
    /* Store the input for implementing change in values */
792
4.39k
    ps_bit_allocation->i4_frame_rate = i4_frm_rate;
793
4.39k
    ps_bit_allocation->i4_bit_rate = i4_bit_rate;
794
13.1k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
795
8.78k
        ps_bit_allocation->ai4_peak_bit_rate[i] = i4_peak_bit_rate[i];
796
797
4.39k
    ps_bit_allocation->i4_is_hbr = i4_is_hbr;
798
    /* Initilising the header bits to be used for each picture type */
799
4.39k
    init_prev_header_bits(ps_bit_allocation, ps_pic_handling);
800
801
    /*HEVC_RC*/
802
    /*remember prev frames tot bit consumption. This is required to calcualte error after first sub gop where estimate is not known*/
803
43.9k
    for(i = 0; i < MAX_PIC_TYPE; i++)
804
39.5k
    {
805
39.5k
        ps_bit_allocation->ai4_prev_frm_tot_bits[i] =
806
39.5k
            -1;  //-1 indicates that pic type has not been encoded
807
39.5k
        ps_bit_allocation->ai4_prev_frm_tot_est_bits[i] = -1;
808
39.5k
    }
809
810
    /* #define STATIC_I_TO_P_RATIO ((STATIC_I_TO_B_RATIO)/(STATIC_P_TO_B_RATIO)) */
811
    /* Calcualte the max i frame bits */
812
4.39k
    {
813
4.39k
        WORD32 ai4_frms_in_period[MAX_PIC_TYPE];
814
4.39k
        WORD32 ai4_actual_frms_in_period[MAX_PIC_TYPE], i4_actual_frms_in_period = 0;
815
4.39k
        WORD32 i4_rem_texture_bits, j, i4_tot_header_bits_est = 0;
816
4.39k
        number_t avq_complexity_estimate[MAX_PIC_TYPE];
817
4.39k
        WORD32 i4_total_frms;
818
819
        /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
820
4.39k
        pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_period);
821
4.39k
        pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_actual_frms_in_period);
822
        /* Depending on the number of gops in a period, find the num_frms_in_prd */
823
4.39k
        i4_total_frms = 0;
824
43.9k
        for(j = 0; j < MAX_PIC_TYPE; j++)
825
39.5k
        {
826
39.5k
            ai4_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period;
827
39.5k
            ai4_actual_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period;
828
39.5k
            i4_total_frms += ai4_frms_in_period[j];
829
39.5k
            i4_actual_frms_in_period += ai4_actual_frms_in_period[j];
830
39.5k
        }
831
4.39k
        ps_bit_allocation->i4_rem_frame_in_period = i4_actual_frms_in_period; /*i_only*/
832
833
43.9k
        for(j = 0; j < MAX_PIC_TYPE; j++)
834
39.5k
        {
835
39.5k
            i4_tot_header_bits_est +=
836
39.5k
                ai4_frms_in_period[j] * ps_bit_allocation->i4_prev_frm_header_bits[j];
837
39.5k
        }
838
        /* Remove the header bits from the remaining bits to find how many bits you
839
        can transfer.*/
840
4.39k
        i4_rem_texture_bits =
841
4.39k
            ps_bit_allocation->i4_bits_per_frm * i4_actual_frms_in_period - i4_tot_header_bits_est;
842
843
        /* Set the complexities for static content */
844
4.39k
        SET_VAR_Q(avq_complexity_estimate[I_PIC], STATIC_I_TO_B2_RATIO, 0);
845
4.39k
        SET_VAR_Q(avq_complexity_estimate[P_PIC], STATIC_P_TO_B2_RATIO, 0);
846
4.39k
        SET_VAR_Q(avq_complexity_estimate[P1_PIC], STATIC_P_TO_B2_RATIO, 0);
847
4.39k
        SET_VAR_Q(avq_complexity_estimate[B_PIC], STATIC_B_TO_B2_RATIO, 0);
848
4.39k
        SET_VAR_Q(avq_complexity_estimate[BB_PIC], STATIC_B_TO_B2_RATIO, 0);
849
4.39k
        SET_VAR_Q(avq_complexity_estimate[B1_PIC], STATIC_B1_TO_B2_RATIO, 0);
850
4.39k
        SET_VAR_Q(avq_complexity_estimate[B11_PIC], STATIC_B1_TO_B2_RATIO, 0);
851
4.39k
        SET_VAR_Q(avq_complexity_estimate[B2_PIC], 1, 0);
852
4.39k
        SET_VAR_Q(avq_complexity_estimate[B22_PIC], 1, 0);
853
        /* Get the texture bits required for the current frame */
854
4.39k
        ps_bit_allocation->i4_max_tex_bits_for_i = get_bits_based_on_complexity(
855
4.39k
            ps_bit_allocation,
856
4.39k
            i4_rem_texture_bits,
857
4.39k
            ai4_frms_in_period,
858
4.39k
            avq_complexity_estimate,
859
4.39k
            I_PIC,
860
4.39k
            0);
861
4.39k
    }
862
    /* initialise the GOP and bit errors to zero */
863
4.39k
    ps_bit_allocation->i4_gop_level_bit_error = 0;
864
4.39k
    ps_bit_allocation->i4_frame_level_bit_error = 0;
865
39.5k
    for(i = 0; i < MAX_NUM_FRAME_PARALLEL; i++)
866
35.1k
    {
867
35.1k
        ps_bit_allocation->ai4_cur_frm_est_tex_bits[i] = 0;
868
35.1k
        ps_bit_allocation->ai4_cur_frm_est_hdr_bits[i] = 0;
869
35.1k
    }
870
4.39k
    ps_bit_allocation->i4_buffer_based_bit_error = 0;
871
4.39k
    ps_bit_allocation->i4_bits_from_buffer_in_cur_gop = 0;
872
4.39k
    ps_bit_allocation->i4_excess_bits_from_buffer = 0;
873
4.39k
    ps_bit_allocation->i4_lap_window = i4_lap_window;
874
4.39k
    ps_bit_allocation->i8_cur_gop_bit_consumption = 0;
875
    //ps_bit_allocation->i8_2pass_gop_error_accum = 0;
876
4.39k
    ps_bit_allocation->f_qscale_max_clip_in_second_pass = (float)0x7FFFFFFF;
877
878
    /*Buffer play for single pass*/
879
4.39k
    if(rc_pass != 2)
880
4.39k
    {
881
        /*Find ps_bit_allocation->f_min_complexity_cross_peak_rate*/
882
        /*Find the complexity which maps to peak bit-rate*/
883
4.39k
        {
884
4.39k
            ps_bit_allocation->f_min_complexity_cross_peak_rate = ba_get_min_complexity_for_peak_br(
885
4.39k
                i4_peak_bit_rate[0], i4_bit_rate, 10.0f, 1.0f, 0.0f, rc_pass);
886
4.39k
        }
887
4.39k
    }
888
889
4.39k
    ps_bit_allocation->i4_total_2pass_frames = 0;
890
4.39k
    ps_bit_allocation->i8_2pass_avg_bit_rate = -1;
891
4.39k
}
892
893
/*****************************************************************************
894
  Function Name : ba_init_stat_data
895
  Description   : The parsing of stat file is done at the end of init (by that time
896
                  bit allocation init would have already happened,The memory for gop
897
                  stat data is alocated inside the parse stat file code. Hence the
898
                  pointer has to be updated again.
899
  Inputs        : ps_bit_allocation
900
                  ps_pic_handling
901
                  pv_gop_stat      - pointer to update
902
  Revision History:
903
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
904
*****************************************************************************/
905
void ba_init_stat_data(
906
    bit_allocation_t *ps_bit_allocation,
907
    pic_handling_handle ps_pic_handling,
908
    void *pv_gop_stat,
909
    WORD32 *pi4_pic_dist_in_cur_gop,
910
    WORD32 i4_total_bits_in_period,
911
    WORD32 i4_excess_bits)
912
913
0
{
914
0
    WORD32 i4_tot_frames_in_gop = 0, i;
915
916
0
    ps_bit_allocation->pv_gop_stat = pv_gop_stat;
917
918
    /*Init RBIP*/
919
    /*Get the complexity*/
920
0
    ASSERT(ps_bit_allocation->i8_cur_gop_num == 0);
921
0
    ASSERT(ps_bit_allocation->i8_frm_num_in_gop == 0);
922
923
    /*INit frames of each type*/
924
0
    for(i = 0; i < MAX_PIC_TYPE; i++)
925
0
    {
926
0
        i4_tot_frames_in_gop += pi4_pic_dist_in_cur_gop[i];
927
0
    }
928
929
    /*ASSERT(i4_tot_frames_in_gop == i4_intra_period);*/
930
    /*Also allocate data for first GOP*/
931
    /*Added for getting actual gop structure*/
932
0
    pic_type_update_frms_in_gop(ps_pic_handling, pi4_pic_dist_in_cur_gop);
933
934
0
    multi_pass_set_rbip(
935
0
        &ps_bit_allocation->s_rbip, ps_pic_handling, i4_total_bits_in_period, i4_tot_frames_in_gop);
936
937
0
    ps_bit_allocation->i8_2pass_alloc_per_frm_bits =
938
0
        (i4_total_bits_in_period + (i4_tot_frames_in_gop >> 1)) / i4_tot_frames_in_gop;
939
0
    ps_bit_allocation->i8_bit_consumption_so_far = 0;
940
941
0
    ASSERT(ps_bit_allocation->i4_ba_rc_pass == 2);
942
0
}
943
#endif /* #if NON_STEADSTATE_CODE */
944
945
/*****************************************************************************
946
  Function Name : bit_alloc_get_intra_bits
947
  Description   :
948
  Inputs        : ps_bit_allocation
949
                  ps_pic_handling
950
                  pvq_complexity_estimate
951
                  I_to_avg_rest
952
  Revision History:
953
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
954
*****************************************************************************/
955
WORD32 bit_alloc_get_intra_bits(
956
    bit_allocation_t *ps_bit_allocation,
957
    pic_handling_handle ps_pic_handling,
958
    cbr_buffer_handle ps_cbr_buf_handling,
959
    picture_type_e e_pic_type,
960
    number_t *pvq_complexity_estimate,
961
    WORD32 i4_is_scd,
962
    float I_to_avg_rest,
963
    WORD32 i4_call_type,
964
    WORD32 i4_non_I_scd,
965
    float f_percent_head_bits)
966
767k
{
967
767k
    WORD32 ai4_frms_in_period[MAX_PIC_TYPE], ai4_frm_in_gop[MAX_PIC_TYPE], tot_frms_in_period = 0;
968
767k
    WORD32 i4_field_pic,
969
767k
        i4_safe_margin = 0,
970
767k
        i4_lap_window;  //margin in buffer to handle I frames that can come immediately after encoding huge static I frame
971
    /*obtain buffer size*/
972
767k
    WORD32 i4_buffer_size =
973
767k
        ((get_cbr_buffer_size(ps_cbr_buf_handling)) >> 4) * UPPER_THRESHOLD_EBF_Q4;
974
767k
    WORD32 i4_cur_buf_pos = get_cbr_ebf(ps_cbr_buf_handling), i4_max_buffer_based,
975
767k
           i4_max_buffer_based_I_pic, i, i4_num_scaled_frms = 1;
976
767k
    WORD32 i4_bit_alloc_window =
977
767k
        (ps_bit_allocation->s_rbip.i4_tot_frms_in_gop *
978
767k
         ps_bit_allocation->s_rbip.i4_num_intra_frm_interval);
979
767k
    WORD32 i4_num_buf_frms,
980
767k
        ai4_frms_in_baw[MAX_PIC_TYPE];  //window for which I frame bit allocation is done
981
767k
    WORD32 i4_bits_in_period, i4_frames_in_buf = 0, i4_default_bits_in_period = 0;
982
767k
    WORD32 i4_est_bits_for_I, i4_peak_drain_rate, i4_subgop_size;
983
767k
    rc_type_e rc_type = get_rc_type(ps_cbr_buf_handling);
984
767k
    pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_frm_in_gop);
985
986
7.67M
    for(i = 0; i < MAX_PIC_TYPE; i++)
987
6.90M
    {
988
6.90M
        ai4_frms_in_baw[i] =
989
6.90M
            ai4_frm_in_gop[i] * ps_bit_allocation->s_rbip.i4_num_intra_frm_interval;
990
6.90M
        ai4_frms_in_period[i] =
991
6.90M
            ai4_frm_in_gop[i] * ps_bit_allocation->s_rbip.i4_num_intra_frm_interval;
992
6.90M
        tot_frms_in_period += ai4_frm_in_gop[i];
993
6.90M
    }
994
995
767k
    if(i4_call_type == 1)
996
294k
    {
997
294k
        i4_default_bits_in_period = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
998
294k
        if((i4_default_bits_in_period + ps_bit_allocation->i4_frame_level_bit_error) <
999
294k
           (i4_default_bits_in_period * 0.30))
1000
0
        {
1001
0
            ps_bit_allocation->i4_frame_level_bit_error = 0;  //-(i4_default_bits_in_period * 0.70);
1002
0
        }
1003
294k
        i4_bits_in_period = i4_default_bits_in_period + ps_bit_allocation->i4_frame_level_bit_error;
1004
294k
        if(i4_non_I_scd == 0)
1005
158k
        {
1006
            /*For the first gop unnecessarily the QP is going high in order to prevent this bits corresponding to full gop instead of gop-subgop*/
1007
1008
158k
            WORD32 i4_intra_int = pic_type_get_intra_frame_interval(ps_pic_handling);
1009
158k
            WORD32 i4_inter_int = pic_type_get_inter_frame_interval(ps_pic_handling);
1010
158k
            if((tot_frms_in_period ==
1011
158k
                (i4_intra_int - i4_inter_int + (1 << ps_bit_allocation->i4_field_pic))) &&
1012
158k
               (i4_intra_int != 1))
1013
78.6k
            {
1014
78.6k
                i4_bits_in_period =
1015
78.6k
                    (WORD32)(i4_bits_in_period * ((float)i4_intra_int / tot_frms_in_period));
1016
78.6k
            }
1017
158k
        }
1018
294k
        trace_printf("\nBits in period %d", i4_bits_in_period);
1019
294k
    }
1020
472k
    else
1021
472k
    {
1022
472k
        i4_bits_in_period = ret_rbip_default_preenc(&ps_bit_allocation->s_rbip, ps_pic_handling);
1023
1024
472k
        if(ps_bit_allocation->i4_ba_rc_pass == 2)
1025
0
            i4_default_bits_in_period = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
1026
472k
    }
1027
1028
767k
    i4_peak_drain_rate = get_buf_max_drain_rate(ps_cbr_buf_handling);
1029
767k
    i4_num_buf_frms =
1030
767k
        (get_cbr_buffer_size(ps_cbr_buf_handling) + (ps_bit_allocation->i4_bits_per_frm >> 1)) /
1031
767k
        ps_bit_allocation->i4_bits_per_frm;
1032
    /*In VBR encoder buffer will be drained faster, i4_num_buf_frms should correspond to maximum number of bits that can be drained
1033
      In CBR both peak and average must be same*/
1034
767k
    i4_num_buf_frms = i4_num_buf_frms * i4_peak_drain_rate / ps_bit_allocation->i4_bits_per_frm;
1035
1036
767k
    i4_field_pic = pic_type_get_field_pic(ps_pic_handling);
1037
1038
767k
    i4_subgop_size = pic_type_get_inter_frame_interval(ps_pic_handling);
1039
767k
    if(pvq_complexity_estimate == NULL)
1040
654k
        i4_cur_buf_pos = 0;
1041
1042
767k
    i4_lap_window = ps_bit_allocation->i4_lap_window;
1043
1044
    /*assume minimum lap visibilty.A static I frame is given only the bits of duration for which we have visibility*/
1045
767k
    if(ps_bit_allocation->i4_lap_window < MINIMUM_VISIBILITY_B4_STATIC_I)
1046
767k
    {
1047
767k
        i4_lap_window = MINIMUM_VISIBILITY_B4_STATIC_I;
1048
767k
    }
1049
0
    else
1050
0
    {
1051
0
        i4_lap_window = ps_bit_allocation->i4_lap_window;
1052
        /*clip buffer window to max of lap window or buffer window*/
1053
0
        if((i4_lap_window < i4_num_buf_frms) && (i4_call_type == 1))
1054
0
            i4_num_buf_frms = i4_lap_window + i4_subgop_size;
1055
0
    }
1056
1057
767k
    if(i4_lap_window < MINIMUM_FRM_I_TO_REST_LAP_ENABLED)
1058
0
        i4_lap_window = MINIMUM_FRM_I_TO_REST_LAP_ENABLED;
1059
767k
    if(ps_bit_allocation->i4_ba_rc_pass != 2)
1060
767k
    {
1061
767k
        if(i4_lap_window < i4_num_buf_frms)
1062
767k
            i4_num_buf_frms = i4_lap_window;
1063
767k
    }
1064
1065
767k
    if(i4_num_buf_frms > tot_frms_in_period)
1066
695k
    {
1067
695k
        i4_num_buf_frms = tot_frms_in_period;
1068
695k
        i4_bit_alloc_window = i4_num_buf_frms;
1069
695k
    }
1070
    /*get picture type dist based on bit alloc window*/
1071
767k
    if(i4_num_buf_frms < tot_frms_in_period)
1072
71.5k
    {
1073
143k
        for(i = 1; i < ps_bit_allocation->i4_num_active_pic_type; i++)
1074
71.5k
        {
1075
71.5k
            ai4_frms_in_baw[i] =
1076
71.5k
                (ai4_frms_in_period[i] * i4_num_buf_frms + (tot_frms_in_period >> 1)) /
1077
71.5k
                tot_frms_in_period;
1078
71.5k
            i4_num_scaled_frms += ai4_frms_in_baw[i];
1079
71.5k
            if(ps_bit_allocation->i4_field_pic)
1080
0
            {
1081
0
                ai4_frms_in_baw[i + FIELD_OFFSET] = ai4_frms_in_baw[i];
1082
0
                i4_num_scaled_frms += ai4_frms_in_baw[i];
1083
0
            }
1084
71.5k
        }
1085
71.5k
        if(ps_bit_allocation->i4_field_pic)
1086
0
        {
1087
0
            ai4_frms_in_baw[5]++;
1088
0
            i4_num_scaled_frms++;
1089
0
        }
1090
        //if prorating is not exact account for diff with highest layer pic types
1091
71.5k
        if(!ps_bit_allocation->i4_field_pic)
1092
71.5k
        {
1093
71.5k
            ai4_frms_in_baw[ps_bit_allocation->i4_num_active_pic_type - 1] +=
1094
71.5k
                (i4_num_buf_frms - i4_num_scaled_frms);
1095
71.5k
        }
1096
0
        else
1097
0
        {
1098
0
            ai4_frms_in_baw[ps_bit_allocation->i4_num_active_pic_type - 1] +=
1099
0
                ((i4_num_buf_frms - i4_num_scaled_frms) >> 1);
1100
0
            ai4_frms_in_baw[ps_bit_allocation->i4_num_active_pic_type - 1 + FIELD_OFFSET] +=
1101
0
                ((i4_num_buf_frms - i4_num_scaled_frms) >> 1);
1102
0
        }
1103
71.5k
        i4_bits_in_period =
1104
71.5k
            ((LWORD64)i4_bits_in_period * i4_num_buf_frms + (tot_frms_in_period >> 1)) /
1105
71.5k
            tot_frms_in_period;
1106
71.5k
        i4_bit_alloc_window = i4_num_buf_frms;
1107
71.5k
    }
1108
1109
767k
    i4_safe_margin = (WORD32)(i4_buffer_size * 0.1);
1110
767k
    i4_max_buffer_based = ((LWORD64)i4_buffer_size - i4_cur_buf_pos) /
1111
767k
                          ps_bit_allocation->i4_bits_per_frm * i4_peak_drain_rate;
1112
767k
    i4_max_buffer_based_I_pic = i4_buffer_size - i4_cur_buf_pos;
1113
1114
7.67M
    for(i = 0; i < MAX_PIC_TYPE; i++)
1115
6.90M
    {
1116
6.90M
        i4_frames_in_buf += ai4_frms_in_baw[i];
1117
6.90M
    }
1118
1119
767k
    if((rc_type == VBR_STREAMING) && (i4_call_type == 1))
1120
190k
    {
1121
190k
        WORD32 i4_delay_frames = cbr_get_delay_frames(ps_cbr_buf_handling);
1122
190k
        i4_max_buffer_based =
1123
190k
            (i4_peak_drain_rate *
1124
190k
                 (ps_bit_allocation->s_rbip.i4_tot_frms_in_gop + (WORD32)(i4_delay_frames * 0.8f)) -
1125
190k
             i4_cur_buf_pos);
1126
1127
        /*RBIP is updated once it is restricted for an Intra period */
1128
190k
        if(i4_default_bits_in_period > i4_max_buffer_based)
1129
0
            update_rbip(
1130
0
                &ps_bit_allocation->s_rbip,
1131
0
                ps_pic_handling,
1132
0
                i4_max_buffer_based - i4_default_bits_in_period);
1133
1134
190k
        i4_max_buffer_based =
1135
190k
            (i4_peak_drain_rate * (i4_frames_in_buf + (WORD32)(i4_delay_frames * 0.8f)) -
1136
190k
             i4_cur_buf_pos);
1137
190k
    }
1138
577k
    else
1139
577k
    {
1140
577k
        i4_max_buffer_based =
1141
577k
            ((((LWORD64)i4_buffer_size - i4_cur_buf_pos) / ps_bit_allocation->i4_bits_per_frm) +
1142
577k
             i4_frames_in_buf) *
1143
577k
            i4_peak_drain_rate;
1144
577k
    }
1145
1146
    /*the estimated bits for total period is clipped to buffer limits*/
1147
767k
    if(i4_bits_in_period > i4_max_buffer_based)
1148
0
        i4_bits_in_period = i4_max_buffer_based;
1149
1150
    /*get I pic bits with altered bits in period*/
1151
767k
    if((!i4_is_scd) &&
1152
767k
       (ps_bit_allocation->i4_num_frames_since_last_I_frame <
1153
113k
        (ps_bit_allocation->i4_frame_rate * 2) / 1000) &&
1154
767k
       (ps_bit_allocation->i4_ba_rc_pass != 2))
1155
109k
    {
1156
        /*returns texture bits*/
1157
109k
        LWORD64 i8_header_bits_in_previous_period = 0, i8_total_bits_in_previous_period = 0,
1158
109k
                i4_frames_in_header = 0;
1159
109k
        WORD32 i4_texture_bits = 0;
1160
109k
        float f_percent_header_bits = 0.0f;
1161
        /* Remove the header bits from the remaining bits to find how many bits you
1162
        can transfer.*/
1163
1.09M
        for(i = 0; i < MAX_PIC_TYPE; i++)
1164
988k
        {
1165
988k
            i8_header_bits_in_previous_period +=
1166
988k
                (ps_bit_allocation->i4_prev_frm_header_bits[i] * ai4_frms_in_baw[i]);
1167
988k
            i8_total_bits_in_previous_period +=
1168
988k
                (ps_bit_allocation->ai4_prev_frm_tot_bits[i] * ai4_frms_in_baw[i]);
1169
988k
            i4_frames_in_header += ai4_frms_in_baw[i];
1170
988k
        }
1171
1172
109k
        if((i4_call_type == 1) && (ps_bit_allocation->i4_ba_rc_pass == 2))
1173
0
        {
1174
0
            i4_texture_bits = (WORD32)(i4_bits_in_period * (1 - f_percent_head_bits));
1175
0
        }
1176
109k
        else
1177
109k
        {
1178
109k
            f_percent_header_bits =
1179
109k
                (float)i8_header_bits_in_previous_period / i8_total_bits_in_previous_period;
1180
109k
            i4_texture_bits =
1181
109k
                i4_bits_in_period - (WORD32)(f_percent_header_bits * i4_bits_in_period);
1182
109k
        }
1183
1184
109k
        if(i4_call_type == 1)
1185
24.8k
        {
1186
24.8k
            trace_printf(
1187
24.8k
                "\nHeader Bits in period %d, total_frames %d "
1188
24.8k
                "i4_max_buffer_based %d ",
1189
24.8k
                (WORD32)f_percent_header_bits * i4_bits_in_period,
1190
24.8k
                i4_frames_in_header,
1191
24.8k
                i4_max_buffer_based);
1192
24.8k
        }
1193
109k
        i4_est_bits_for_I = get_bits_based_on_complexity(
1194
109k
            ps_bit_allocation,
1195
109k
            i4_texture_bits,
1196
109k
            ai4_frms_in_baw,
1197
109k
            pvq_complexity_estimate,
1198
109k
            e_pic_type,
1199
109k
            i4_call_type);
1200
        /*twice the bitrate */
1201
109k
        if(i4_est_bits_for_I > ((ps_bit_allocation->i4_bit_rate << 1) -
1202
109k
                                ps_bit_allocation->i4_prev_frm_header_bits[I_PIC]))
1203
0
            i4_est_bits_for_I =
1204
0
                ((ps_bit_allocation->i4_bit_rate << 1) -
1205
0
                 ps_bit_allocation->i4_prev_frm_header_bits[I_PIC]);
1206
1207
109k
        if(i4_est_bits_for_I >
1208
109k
           (i4_max_buffer_based_I_pic - ps_bit_allocation->i4_prev_frm_header_bits[I_PIC]))
1209
1
        {
1210
1
            i4_est_bits_for_I =
1211
1
                (i4_max_buffer_based_I_pic - ps_bit_allocation->i4_prev_frm_header_bits[I_PIC]);
1212
1
        }
1213
109k
    }
1214
657k
    else
1215
657k
    {
1216
        /*returns total bits incase of scene cut*/
1217
657k
        ASSERT(ai4_frms_in_baw[I_PIC] != 0);
1218
657k
        if((i4_non_I_scd == 1) && (i4_call_type == 1) &&
1219
657k
           (ps_bit_allocation->f_curr_i_to_sum != 1.0f))
1220
0
            ai4_frms_in_baw[I_PIC]++;
1221
1222
657k
        i4_est_bits_for_I = (WORD32)(
1223
657k
            (i4_bits_in_period * I_to_avg_rest * ai4_frms_in_baw[I_PIC]) /
1224
657k
            (ai4_frms_in_baw[I_PIC] * I_to_avg_rest +
1225
657k
             (i4_bit_alloc_window - ai4_frms_in_baw[I_PIC])));
1226
1227
657k
        if(i4_call_type == 1)
1228
270k
            i4_est_bits_for_I =
1229
270k
                (WORD32)((float)i4_est_bits_for_I * ps_bit_allocation->f_curr_i_to_sum);
1230
387k
        else
1231
387k
        {
1232
387k
            if(ai4_frms_in_baw[I_PIC] > 0)
1233
387k
                i4_est_bits_for_I = (WORD32)((float)i4_est_bits_for_I / ai4_frms_in_baw[I_PIC]);
1234
387k
        }
1235
1236
657k
        if(i4_call_type == 1)
1237
270k
        {
1238
270k
            trace_printf(
1239
270k
                "bits in period %d I_to_avg_rest %f f_curr_i_to_sum %f i "
1240
270k
                "frames %d i4_non_I_scd %d ",
1241
270k
                i4_bits_in_period,
1242
270k
                I_to_avg_rest,
1243
270k
                ps_bit_allocation->f_curr_i_to_sum,
1244
270k
                ai4_frms_in_baw[I_PIC],
1245
270k
                i4_non_I_scd);
1246
270k
        }
1247
1248
657k
        if(i4_est_bits_for_I > (ps_bit_allocation->i4_bit_rate << 1))
1249
19.3k
            i4_est_bits_for_I = (ps_bit_allocation->i4_bit_rate << 1);
1250
657k
        if(i4_est_bits_for_I > i4_max_buffer_based_I_pic)
1251
22.0k
            i4_est_bits_for_I = i4_max_buffer_based_I_pic;
1252
657k
    }
1253
1254
767k
    return i4_est_bits_for_I;
1255
767k
}
1256
1257
/*****************************************************************************
1258
  Function Name : get_cur_frm_est_texture_bits
1259
  Description   : Based on remaining bits in period and rd_model
1260
                  the number of bits required for the current frame is estimated.
1261
  Inputs        : ps_bit_allocation - bit_allocation structure
1262
                  ps_rd_model - rd model pointer (for all the frame types)
1263
                  e_pic_type - picture type
1264
  Globals       :
1265
  Processing    :
1266
  Outputs       :
1267
  Returns       :
1268
  Issues        :
1269
  Revision History:
1270
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1271
*****************************************************************************/
1272
WORD32 get_cur_frm_est_texture_bits(
1273
    bit_allocation_t *ps_bit_allocation,
1274
    rc_rd_model_handle *pps_rd_model,
1275
    est_sad_handle ps_est_sad,
1276
    pic_handling_handle ps_pic_handling,
1277
    cbr_buffer_handle ps_cbr_buffer,
1278
    picture_type_e e_pic_type,
1279
    WORD32 i4_use_model,
1280
    WORD32 i4_is_scd_frame,
1281
    WORD32 i4_call_type,
1282
    float i_to_avg_ratio,
1283
    WORD32 i4_is_model_valid)
1284
276k
{
1285
276k
    WORD32 i, j;
1286
276k
    WORD32 i4_est_texture_bits_for_frm;
1287
276k
    WORD32 i4_rem_texture_bits;
1288
276k
    number_t avq_complexity_estimate[MAX_PIC_TYPE];
1289
276k
    WORD32 ai4_frms_in_period[MAX_PIC_TYPE];
1290
276k
    WORD32 i4_max_consumable_bits, i4_est_tot_head_bits_period = 0, i4_total_bits_prev_gop = 0;
1291
276k
    WORD32 i4_field_pic, i4_inter_frame_int;
1292
276k
    WORD32 complexity_est = 0;
1293
276k
    float f_percent_head_bits = 0.0f;
1294
276k
    WORD32 i4_intra_frm_int;
1295
276k
    i4_intra_frm_int = pic_type_get_actual_intra_frame_interval(ps_pic_handling);
1296
276k
    i4_inter_frame_int = pic_type_get_inter_frame_interval(ps_pic_handling);
1297
276k
    i4_field_pic = pic_type_get_field_pic(ps_pic_handling);
1298
1299
    /* If the complexity estimate is not filled based on
1300
    1) Since not using model
1301
    2) Using the module but one of the estimate values are zero
1302
    Then set the complexity estimate based on default values */
1303
    //  if(!complexity_est)
1304
276k
    {
1305
        /* Hardcoding the bit ratios between I, P and B same as during init time*/
1306
276k
        SET_VAR_Q(
1307
276k
            avq_complexity_estimate[I_PIC],
1308
276k
            (I_TO_P_BIT_RATIO * P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
1309
276k
            0);
1310
276k
        SET_VAR_Q(
1311
276k
            avq_complexity_estimate[P_PIC],
1312
276k
            (P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
1313
276k
            0);
1314
276k
        SET_VAR_Q(
1315
276k
            avq_complexity_estimate[P1_PIC],
1316
276k
            (P_TO_B_BIT_RATIO * B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO),
1317
276k
            0);
1318
276k
        SET_VAR_Q(avq_complexity_estimate[B_PIC], (B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO), 0);
1319
276k
        SET_VAR_Q(avq_complexity_estimate[BB_PIC], (B_TO_B1_BIT_RATO0 * B1_TO_B2_BIT_RATIO), 0);
1320
276k
        SET_VAR_Q(
1321
276k
            avq_complexity_estimate[B1_PIC],
1322
276k
            (B1_TO_B2_BIT_RATIO),
1323
276k
            0);  //temporarliy treat ref and non ref as same
1324
276k
        SET_VAR_Q(avq_complexity_estimate[B11_PIC], (B1_TO_B2_BIT_RATIO), 0);
1325
276k
        SET_VAR_Q(avq_complexity_estimate[B2_PIC], 1, 0);
1326
276k
        SET_VAR_Q(avq_complexity_estimate[B22_PIC], 1, 0);
1327
276k
    }
1328
    /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
1329
    /* pic_type_get_rem_frms_in_gop(ps_pic_handling, ai4_rem_frms_in_period); */
1330
276k
    pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_period);
1331
1332
    /* Depending on the number of gops in a period, find the num_frms_in_prd */
1333
2.76M
    for(j = 0; j < MAX_PIC_TYPE; j++)
1334
2.49M
    {
1335
        /* ai4_rem_frms_in_period[j] += (ai4_frms_in_period[j] * (ps_bit_allocation->i4_num_gops_in_period - 1)); */
1336
2.49M
        ai4_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period;
1337
2.49M
    }
1338
1339
    /* If a frame is detected as SCD and bit allocation is asked for the remaining part of the frame
1340
    we allocate bits assuming that frame as a I frame. So reduce 1 frame from the picture type coming in
1341
    and add that to I frame */
1342
276k
    if(i4_is_scd_frame && e_pic_type != I_PIC)
1343
0
    {
1344
        /* ai4_rem_frms_in_period[0]++;ai4_rem_frms_in_period[e_pic_type]--; */
1345
0
        ai4_frms_in_period[0]++;
1346
0
        ai4_frms_in_period[e_pic_type]--;
1347
0
    }
1348
    /*HEVC_hierarchy: calculate header bits for all frames in period*/
1349
2.76M
    for(j = 0; j < MAX_PIC_TYPE; j++)
1350
2.49M
    {
1351
2.49M
        i4_est_tot_head_bits_period +=
1352
2.49M
            ai4_frms_in_period[j] * ps_bit_allocation->i4_prev_frm_header_bits[j];
1353
2.49M
        i4_total_bits_prev_gop +=
1354
2.49M
            ai4_frms_in_period[j] * ps_bit_allocation->ai4_prev_frm_tot_bits[j];
1355
2.49M
    }
1356
1357
276k
    {
1358
276k
        WORD32 ai4_actual_frms_in_gop[MAX_PIC_TYPE], i, i4_total_frames = 0;
1359
276k
        pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_actual_frms_in_gop);
1360
2.76M
        for(i = 0; i < MAX_PIC_TYPE; i++)
1361
2.49M
        {
1362
2.49M
            i4_total_frames += ai4_actual_frms_in_gop[i];
1363
2.49M
        }
1364
276k
        i4_max_consumable_bits = ps_bit_allocation->i4_max_bits_per_frm[0] * i4_total_frames;
1365
1366
        /* Remove the header bits from the remaining bits to find how many bits you
1367
           can transfer.*/
1368
276k
        if(i4_call_type == 1)
1369
77.4k
        {
1370
77.4k
            if(ps_bit_allocation->i4_ba_rc_pass == 2)
1371
0
            {
1372
0
                WORD32 i4_tot_frm_remain = 0, i4_tot_head_bits_in_gop = 0,
1373
0
                       i4_tot_bits_last_in_gop = 0, i4_use_default_flag = 0;
1374
1375
0
                WORD32 i4_rbip = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
1376
0
                if((i4_rbip + ps_bit_allocation->i4_frame_level_bit_error) < (i4_rbip * 0.30))
1377
0
                {
1378
0
                    ps_bit_allocation->i4_frame_level_bit_error = 0;  //-(i4_rbip * 0.70);
1379
0
                }
1380
0
                i4_rem_texture_bits =
1381
0
                    i4_rbip +
1382
0
                    ps_bit_allocation->i4_frame_level_bit_error /*- i4_est_tot_head_bits_period*/
1383
0
                    ;
1384
1385
0
                i4_est_tot_head_bits_period = 0;
1386
0
                for(j = 0; j < MAX_PIC_TYPE; j++)
1387
0
                {
1388
0
                    if((WORD32)ps_bit_allocation->af_sum_weigh[j][1] > 0)
1389
0
                    {
1390
0
                        i4_tot_frm_remain += (WORD32)ps_bit_allocation->af_sum_weigh[j][1];
1391
0
                        i4_tot_head_bits_in_gop += (WORD32)(
1392
0
                            ps_bit_allocation->i4_prev_frm_header_bits[j] *
1393
0
                            ps_bit_allocation->af_sum_weigh[j][1]);
1394
0
                        i4_tot_bits_last_in_gop += (WORD32)(
1395
0
                            ps_bit_allocation->ai4_prev_frm_tot_bits[j] *
1396
0
                            ps_bit_allocation->af_sum_weigh[j][1]);
1397
0
                        if(ps_bit_allocation->ai4_prev_frm_tot_bits[j] == -1)
1398
0
                        {
1399
0
                            i4_use_default_flag = 1;
1400
0
                        }
1401
0
                    }
1402
0
                }
1403
1404
0
                if(i4_use_default_flag != 1)
1405
0
                {
1406
0
                    f_percent_head_bits = (float)i4_tot_head_bits_in_gop / i4_tot_bits_last_in_gop;
1407
1408
0
                    if(f_percent_head_bits > 0.7f)
1409
0
                        f_percent_head_bits = 0.7f;
1410
1411
                    /*Subtracting a percentage of header bits from the remaining bits in period*/
1412
0
                    i4_rem_texture_bits = (WORD32)(i4_rem_texture_bits * (1 - f_percent_head_bits));
1413
0
                }
1414
0
                else
1415
0
                {
1416
                    /*Assuming 30% of bits will go for header bits in a gop in preenc*/
1417
0
                    i4_rem_texture_bits -= (WORD32)((float)i4_rem_texture_bits * 0.3f);
1418
0
                }
1419
1420
0
                trace_printf(
1421
0
                    "Remaining texture bits %d fbe %d fphb %f thbg %d tblg %d",
1422
0
                    i4_rem_texture_bits,
1423
0
                    ps_bit_allocation->i4_frame_level_bit_error,
1424
0
                    f_percent_head_bits,
1425
0
                    i4_tot_head_bits_in_gop,
1426
0
                    i4_tot_bits_last_in_gop);
1427
0
            }
1428
77.4k
            else
1429
77.4k
            {
1430
                /* Remove the header bits from the remaining bits to find how many bits you
1431
            can transfer.*/
1432
77.4k
                WORD32 i4_rbip = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
1433
77.4k
                if((i4_rbip + ps_bit_allocation->i4_frame_level_bit_error) < (i4_rbip * 0.30))
1434
2
                {
1435
2
                    ps_bit_allocation->i4_frame_level_bit_error = 0;  //-(i4_rbip * 0.70);
1436
2
                }
1437
77.4k
                i4_rem_texture_bits = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0) +
1438
77.4k
                                      ps_bit_allocation->i4_frame_level_bit_error;
1439
1440
77.4k
                i4_est_tot_head_bits_period = (WORD32)(
1441
77.4k
                    ((float)(i4_est_tot_head_bits_period) / (float)i4_total_bits_prev_gop) *
1442
77.4k
                    i4_rem_texture_bits);
1443
1444
77.4k
                if(i4_is_model_valid)
1445
77.4k
                {
1446
77.4k
                    i4_rem_texture_bits = i4_rem_texture_bits - i4_est_tot_head_bits_period;
1447
77.4k
                }
1448
0
                else
1449
0
                {
1450
                    /*inorder to estimate the buffer position for model invalid cases, to control
1451
                    encoder buffer overflow*/
1452
0
                    i4_rem_texture_bits = ((i4_rem_texture_bits * 3) >> 1);
1453
0
                }
1454
1455
77.4k
                trace_printf(
1456
77.4k
                    "Remaining texture bits %d fbe %d ethp %d",
1457
77.4k
                    i4_rem_texture_bits,
1458
77.4k
                    ps_bit_allocation->i4_frame_level_bit_error,
1459
77.4k
                    i4_est_tot_head_bits_period);
1460
77.4k
            }
1461
1462
77.4k
            {
1463
77.4k
                WORD32 i4_drain_bits_per_frame = get_buf_max_drain_rate(ps_cbr_buffer), i4_ebf;
1464
77.4k
                WORD32 i4_delay = cbr_get_delay_frames(ps_cbr_buffer), max_buffer_level = 0,
1465
77.4k
                       rc_type = get_rc_type(ps_cbr_buffer);
1466
1467
77.4k
                if(rc_type == VBR_STREAMING)
1468
51.8k
                    max_buffer_level = i4_drain_bits_per_frame * i4_delay;
1469
25.6k
                else
1470
25.6k
                    max_buffer_level = get_cbr_buffer_size(ps_cbr_buffer);
1471
1472
77.4k
                i4_ebf = get_cbr_ebf(ps_cbr_buffer);
1473
1474
77.4k
                if(i4_ebf > (WORD32)(0.8f * max_buffer_level))
1475
0
                {
1476
0
                    if(ps_bit_allocation->af_sum_weigh[e_pic_type][0] > 1.0f)
1477
0
                        ps_bit_allocation->af_sum_weigh[e_pic_type][0] = 1.0f;
1478
0
                }
1479
77.4k
                if(i4_ebf > (WORD32)(0.6f * max_buffer_level))
1480
0
                {
1481
0
                    if(ps_bit_allocation->af_sum_weigh[e_pic_type][0] > 1.5f)
1482
0
                        ps_bit_allocation->af_sum_weigh[e_pic_type][0] = 1.5f;
1483
0
                }
1484
77.4k
            }
1485
77.4k
        }
1486
199k
        else
1487
199k
        {
1488
199k
            i4_rem_texture_bits =
1489
199k
                ret_rbip_default_preenc(&ps_bit_allocation->s_rbip, ps_pic_handling);
1490
            /*Assuming 30% of bits will go for header bits in a gop in preenc*/
1491
199k
            i4_rem_texture_bits -= (WORD32)(i4_rem_texture_bits * 0.3f);
1492
199k
        }
1493
276k
    }
1494
1495
276k
    if(i4_use_model)
1496
255k
    {
1497
        /* The bits are then allocated based on the relative complexity of the
1498
        current frame with respect to that of the rest of the frames in period */
1499
2.55M
        for(i = 0; i < MAX_PIC_TYPE; i++)
1500
2.29M
        {
1501
2.29M
            number_t vq_lin_mod_coeff, vq_est_sad, vq_K;
1502
1503
2.29M
            if(ai4_frms_in_period[i] > 0) /*Added for field case */
1504
622k
            {
1505
                /* Getting the linear model coefficient */
1506
622k
                vq_lin_mod_coeff = get_linear_coefficient(pps_rd_model[i]);
1507
                /* Getting the estimated SAD */
1508
622k
                SET_VAR_Q(vq_est_sad, get_est_sad(ps_est_sad, (picture_type_e)i), 0);
1509
                /* Making K factor a var Q format */
1510
622k
                SET_VAR_Q(vq_K, ps_bit_allocation->i2_K[i], K_Q);
1511
                /* Complexity_estimate = [ (lin_mod_coeff * estimated_sad) / K factor ]  */
1512
622k
                mult32_var_q(vq_lin_mod_coeff, vq_est_sad, &vq_lin_mod_coeff);
1513
622k
                div32_var_q(vq_lin_mod_coeff, vq_K, &avq_complexity_estimate[i]);
1514
622k
            }
1515
2.29M
        }
1516
        /*flag to check if complexity estimate is available*/
1517
255k
        complexity_est = 1;
1518
1519
        /*HEVC_hierarchy: If complexity estimate = 0 for any pic type then use default ratios*/
1520
2.55M
        for(i = 0; i < MAX_PIC_TYPE; i++)
1521
2.29M
        {
1522
2.29M
            if(ai4_frms_in_period[i] > 0)
1523
622k
            {
1524
622k
                complexity_est = complexity_est && avq_complexity_estimate[i].sm;
1525
622k
            }
1526
2.29M
        }
1527
255k
    }
1528
1529
    /* Make the picture type of the SCD frame a I_PIC */
1530
276k
    if(i4_is_scd_frame && e_pic_type != I_PIC)
1531
0
        e_pic_type = I_PIC;
1532
1533
276k
    if(e_pic_type == I_PIC)
1534
113k
    {
1535
        /*clip min max values*/
1536
113k
        if(i_to_avg_ratio > I_TO_AVG_REST_GOP_BIT_MAX)
1537
0
            i_to_avg_ratio = I_TO_AVG_REST_GOP_BIT_MAX;
1538
1539
113k
        if(i_to_avg_ratio < I_TO_AVG_REST_GOP_BIT_MIN)
1540
0
            i_to_avg_ratio = I_TO_AVG_REST_GOP_BIT_MIN;
1541
1542
113k
        i4_est_texture_bits_for_frm = bit_alloc_get_intra_bits(
1543
113k
            ps_bit_allocation,
1544
113k
            ps_pic_handling,
1545
113k
            ps_cbr_buffer,
1546
113k
            e_pic_type,
1547
113k
            avq_complexity_estimate,
1548
113k
            0,
1549
113k
            i_to_avg_ratio,
1550
113k
            i4_call_type,
1551
113k
            0,
1552
113k
            f_percent_head_bits);
1553
113k
    }
1554
163k
    else
1555
163k
    {
1556
        /* Get the texture bits required for the current frame */
1557
163k
        i4_est_texture_bits_for_frm = get_bits_based_on_complexity(
1558
163k
            ps_bit_allocation,
1559
163k
            i4_rem_texture_bits,
1560
163k
            ai4_frms_in_period,
1561
163k
            avq_complexity_estimate,
1562
163k
            e_pic_type,
1563
163k
            i4_call_type);
1564
163k
    }
1565
1566
276k
    ps_bit_allocation->i4_excess_bits_from_buffer = 0;
1567
1568
    /* If the remaining bits in the period becomes negative then the estimated texture
1569
    bits would also become negative. This would send a feedback to the model which
1570
    may go for a toss. Thus sending the minimum possible value = 0 */
1571
276k
    if(i4_est_texture_bits_for_frm < 0)
1572
331
        i4_est_texture_bits_for_frm = 0;
1573
1574
276k
    return (i4_est_texture_bits_for_frm);
1575
276k
}
1576
1577
/*****************************************************************************
1578
  Function Name : get_cur_frm_est_header_bits
1579
  Description   : Based on remaining bits in period and rd_model
1580
                  the number of bits required for the current frame is estimated.
1581
  Inputs        : ps_bit_allocation - bit_allocation structure
1582
                  ps_rd_model - rd model pointer (for all the frame types)
1583
                  e_pic_type - picture type
1584
  Globals       :
1585
  Processing    :
1586
  Outputs       :
1587
  Returns       :
1588
  Issues        :
1589
  Revision History:
1590
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1591
*****************************************************************************/
1592
WORD32 get_cur_frm_est_header_bits(bit_allocation_t *ps_bit_allocation, picture_type_e e_pic_type)
1593
504k
{
1594
    //ASSERT(ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type] == 0);
1595
504k
    return (ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type]);
1596
504k
}
1597
/*****************************************************************************
1598
  Function Name : get_rem_bits_in_period
1599
  Description   : Get remaining bits in period
1600
  Inputs        : ps_bit_allocation - bit_allocation structure
1601
                  ps_pic_handling
1602
  Revision History:
1603
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1604
*****************************************************************************/
1605
WORD32
1606
    get_rem_bits_in_period(bit_allocation_t *ps_bit_allocation, pic_handling_handle ps_pic_handling)
1607
0
{
1608
0
    return (update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0));
1609
0
}
1610
/*****************************************************************************
1611
  Function Name : get_bits_per_frame
1612
  Description   : Get Bits per frame
1613
  Inputs        : ps_bit_allocation - bit_allocation structure
1614
  Revision History:
1615
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1616
*****************************************************************************/
1617
WORD32 get_bits_per_frame(bit_allocation_t *ps_bit_allocation)
1618
460k
{
1619
460k
    return ((*ps_bit_allocation).i4_bits_per_frm);
1620
460k
}
1621
/*****************************************************************************
1622
  Function Name : ba_get_gop_bits
1623
  Description   :
1624
  Inputs        : ps_bit_allocation - bit_allocation structure
1625
  Revision History:
1626
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1627
*****************************************************************************/
1628
LWORD64 ba_get_gop_bits(bit_allocation_t *ps_bit_allocation)
1629
0
{
1630
0
    gop_level_stat_t *ps_cur_gop_stat;
1631
0
    ps_cur_gop_stat =
1632
0
        (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
1633
0
    return (
1634
0
        ps_cur_gop_stat->i8_bits_allocated_to_gop +
1635
0
        ps_cur_gop_stat->i8_buffer_play_bits_allocated_to_gop);
1636
0
}
1637
/*****************************************************************************
1638
  Function Name : ba_get_gop_sad
1639
  Description   :
1640
  Inputs        : ps_bit_allocation - bit_allocation structure
1641
  Revision History:
1642
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1643
*****************************************************************************/
1644
LWORD64 ba_get_gop_sad(bit_allocation_t *ps_bit_allocation)
1645
0
{
1646
0
    gop_level_stat_t *ps_cur_gop_stat;
1647
0
    ps_cur_gop_stat =
1648
0
        (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
1649
0
    return (ps_cur_gop_stat->i8_acc_gop_sad);
1650
0
}
1651
/*****************************************************************************
1652
  Function Name : ba_get_buffer_play_bits_for_cur_gop
1653
  Description   :
1654
  Inputs        : ps_bit_allocation - bit_allocation structure
1655
  Revision History:
1656
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1657
*****************************************************************************/
1658
LWORD64 ba_get_buffer_play_bits_for_cur_gop(bit_allocation_t *ps_bit_allocation)
1659
0
{
1660
0
    gop_level_stat_t *ps_cur_gop_stat;
1661
0
    ps_cur_gop_stat =
1662
0
        (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
1663
0
    return (ps_cur_gop_stat->i8_buffer_play_bits_allocated_to_gop);
1664
0
}
1665
1666
/*****************************************************************************
1667
  Function Name : update_cur_frm_consumed_bits
1668
  Description   : Based on remaining bits in period and rd_model
1669
                  the number of bits required for the current frame is estimated.
1670
  Inputs        : ps_bit_allocation - bit_allocation structure
1671
                  ps_rd_model - rd model pointer (for all the frame types)
1672
                  e_pic_type - picture type
1673
  Globals       :
1674
  Processing    :
1675
  Outputs       :
1676
  Returns       :
1677
  Issues        :
1678
  Revision History:
1679
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1680
*****************************************************************************/
1681
void update_cur_frm_consumed_bits(
1682
    bit_allocation_t *ps_bit_allocation,
1683
    pic_handling_handle ps_pic_handling,
1684
    cbr_buffer_handle ps_cbr_buf_handle,
1685
    WORD32 i4_total_frame_bits,
1686
    WORD32 i4_model_updation_hdr_bits,
1687
    picture_type_e e_pic_type,
1688
    UWORD8 u1_is_scd,
1689
    WORD32 i4_last_frm_in_period,
1690
    WORD32 i4_lap_comp_bits_reset,
1691
    WORD32 i4_suppress_bpic_update,
1692
    WORD32 i4_buffer_based_bit_error,
1693
    WORD32 i4_stuff_bits,
1694
    WORD32 i4_lap_window_comp,
1695
    rc_type_e e_rc_type,
1696
    WORD32 i4_num_gop,
1697
    WORD32 i4_is_pause_to_resume,
1698
    WORD32 i4_est_text_bits_ctr_update_qp,
1699
    WORD32 *pi4_gop_correction,
1700
    WORD32 *pi4_new_correction)
1701
92.4k
{
1702
92.4k
    WORD32 i4_error_bits = get_error_bits(ps_bit_allocation->ps_error_bits);
1703
92.4k
    WORD32 i4_intra_frm_int, i, i4_flag_no_error_calc = 0; /*i_only*/
1704
92.4k
    WORD32 i4_do_correction = 0;
1705
92.4k
    i4_intra_frm_int = pic_type_get_intra_frame_interval(ps_pic_handling);
1706
92.4k
    ps_bit_allocation->i4_rem_frame_in_period--;
1707
1708
    /*No frame level bit error for top layer pictures*/
1709
1710
92.4k
    i4_flag_no_error_calc = /*((e_pic_type != B1_PIC && e_pic_type != B11_PIC)  && ps_bit_allocation->i4_num_active_pic_type == 4)||
1711
                            ((e_pic_type != B2_PIC && e_pic_type != B22_PIC)  && ps_bit_allocation->i4_num_active_pic_type == 5) &&*/
1712
92.4k
        (i4_is_pause_to_resume == 0);
1713
1714
    /* Update the remaining bits in period */
1715
92.4k
    ps_bit_allocation->i4_bits_from_buffer_in_cur_gop +=
1716
92.4k
        ps_bit_allocation->i4_excess_bits_from_buffer;
1717
92.4k
    ps_bit_allocation->i4_buffer_based_bit_error -= ps_bit_allocation->i4_excess_bits_from_buffer;
1718
92.4k
    ps_bit_allocation->i4_gop_level_bit_error +=
1719
92.4k
        (-(i4_total_frame_bits + i4_stuff_bits) + i4_error_bits +
1720
92.4k
         ps_bit_allocation->i4_bits_per_frm);
1721
92.4k
    ps_bit_allocation->i8_cur_gop_bit_consumption += (i4_total_frame_bits + i4_stuff_bits);
1722
1723
    //if(ps_bit_allocation-> == 2)ASSERT(i4_stuff_bits == 0);//No stuffing in two pass
1724
    //ASSERT(ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type] == 0);
1725
92.4k
    ps_bit_allocation->i4_buffer_based_bit_error += i4_buffer_based_bit_error;
1726
92.4k
    ps_bit_allocation->i8_frm_num_in_gop++;
1727
92.4k
    if(i4_last_frm_in_period && i4_lap_comp_bits_reset)
1728
0
        i4_lap_comp_bits_reset = 0;  //end of period is always I frame boundary.
1729
1730
92.4k
    if(e_pic_type == I_PIC)
1731
32.2k
        ps_bit_allocation->i4_num_frames_since_last_I_frame = 1;
1732
60.1k
    else
1733
60.1k
        ps_bit_allocation->i4_num_frames_since_last_I_frame++;
1734
1735
92.4k
    if((!i4_suppress_bpic_update))
1736
92.4k
    {
1737
        //if(ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_update_qp] > 0)
1738
92.4k
        {
1739
92.4k
            ps_bit_allocation->ai4_prev_frm_tot_est_bits[e_pic_type] =
1740
92.4k
                ps_bit_allocation->ai4_cur_frm_est_hdr_bits[i4_est_text_bits_ctr_update_qp] +
1741
92.4k
                ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_update_qp];
1742
1743
92.4k
            ps_bit_allocation->i4_frame_level_bit_error +=
1744
92.4k
                (ps_bit_allocation->ai4_cur_frm_est_hdr_bits[i4_est_text_bits_ctr_update_qp] +
1745
92.4k
                 ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_update_qp] -
1746
92.4k
                 i4_total_frame_bits);
1747
92.4k
        }
1748
1749
92.4k
        trace_printf(
1750
92.4k
            "Prev frame header %d Total est %d total frame %d",
1751
92.4k
            ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type],
1752
92.4k
            ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_update_qp],
1753
92.4k
            i4_total_frame_bits);
1754
92.4k
    }
1755
1756
92.4k
    trace_printf(
1757
92.4k
        "  rbip = %d  frame lbe = %d    bbbe = %d  bfbicg = %d\n",
1758
92.4k
        update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0),
1759
92.4k
        ps_bit_allocation->i4_frame_level_bit_error,
1760
92.4k
        ps_bit_allocation->i4_buffer_based_bit_error,
1761
92.4k
        ps_bit_allocation->i4_bits_from_buffer_in_cur_gop);
1762
1763
    /* Update the header bits so that it can be used as an estimate to the next frame */
1764
92.4k
    if(u1_is_scd)
1765
251
    {
1766
        /* Initilising the header bits to be used for each picture type */
1767
251
        init_prev_header_bits(ps_bit_allocation, ps_pic_handling);
1768
1769
        /*init tot bits consumed of previous frame*/
1770
2.51k
        for(i = 0; i < MAX_PIC_TYPE; i++)
1771
2.25k
        {
1772
2.25k
            ps_bit_allocation->ai4_prev_frm_tot_bits[i] = -1;
1773
2.25k
            ps_bit_allocation->ai4_prev_frm_tot_est_bits[i] = -1;
1774
2.25k
        }
1775
        /* In case of SCD, eventhough the frame type is P, it is equivalent to a I frame
1776
           and so the coresponding header bits is updated */
1777
        //ASSERT(i4_model_updation_hdr_bits == 0);
1778
251
        ps_bit_allocation->i4_prev_frm_header_bits[I_PIC] = i4_model_updation_hdr_bits;
1779
251
        ps_bit_allocation->ai4_prev_frm_tot_bits[I_PIC] = i4_total_frame_bits;
1780
251
        ps_bit_allocation->ai4_prev_frm_tot_est_bits[I_PIC] = i4_total_frame_bits;
1781
        /*SCD allowed only for I_PIC*/
1782
251
        ASSERT(e_pic_type == I_PIC);
1783
1784
251
#define MAX_NUM_GOPS_IN_PERIOD (5)
1785
251
        if(ps_bit_allocation->i4_num_gops_in_period != 1 &&
1786
251
           ps_bit_allocation->i4_num_gops_in_period < MAX_NUM_GOPS_IN_PERIOD)
1787
0
        {
1788
            /* Whenever there is a scene change increase the number of gops by 2 so that
1789
               the number of bits allocated is not very constrained. */
1790
0
            ps_bit_allocation->i4_num_gops_in_period += 2;
1791
            /* Add the extra bits in GOP to remaining bits in period */
1792
0
            change_rbip(
1793
0
                &ps_bit_allocation->s_rbip,
1794
0
                ps_bit_allocation->i4_bits_per_frm,
1795
0
                ps_bit_allocation->i4_num_gops_in_period);
1796
            /* printf((const WORD8*)"SCD rbp %d, ngp %d\n", update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling,0),
1797
            ps_bit_allocation->i4_num_gops_in_period); */
1798
0
        }
1799
251
    }
1800
92.2k
    else
1801
92.2k
    {
1802
        //ASSERT(i4_model_updation_hdr_bits == 0);
1803
92.2k
        if(!i4_suppress_bpic_update)
1804
92.2k
        {
1805
92.2k
            ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type] = i4_model_updation_hdr_bits;
1806
92.2k
            ps_bit_allocation->ai4_prev_frm_tot_bits[e_pic_type] = i4_total_frame_bits;
1807
92.2k
        }
1808
92.2k
    }
1809
1810
92.4k
    {
1811
        /* Removng the error due to buffer movement from gop level bit error */
1812
92.4k
        WORD32 i4_gop_correction = 0;
1813
92.4k
        WORD32 i4_cur_ebf = get_cbr_ebf(ps_cbr_buf_handle);
1814
92.4k
        WORD32 i4_vbv_size = get_cbr_buffer_size(ps_cbr_buf_handle);
1815
92.4k
        WORD32 i4_min_vbv_size = (WORD32)(i4_vbv_size * MIN_THRESHOLD_VBV_GOP_ERROR);
1816
92.4k
        WORD32 i4_max_vbv_size = (WORD32)(i4_vbv_size * MAX_THRESHOLD_VBV_GOP_ERROR);
1817
        /*get desired buffer level so that bit error can be calculated. desired buf = 1 - lap window complexity*/
1818
92.4k
        if(ps_bit_allocation->i4_ba_rc_pass != 2)
1819
92.4k
        {
1820
92.4k
            WORD32 i4_inter_frame_interval = pic_type_get_inter_frame_interval(ps_pic_handling);
1821
92.4k
            LWORD64 vbv_buffer_based_excess = 0;
1822
92.4k
            WORD32 i4_lap_window_comp_temp = i4_lap_window_comp;
1823
92.4k
            if(ps_bit_allocation->i4_lap_window > i4_inter_frame_interval)
1824
0
            {
1825
0
                if(e_rc_type == VBR_STREAMING)
1826
0
                {
1827
0
                    if(((float)i4_lap_window_comp / 128) >
1828
0
                       ps_bit_allocation->f_min_complexity_cross_peak_rate)
1829
0
                        i4_lap_window_comp_temp =
1830
0
                            (WORD32)(ps_bit_allocation->f_min_complexity_cross_peak_rate * 128);
1831
1832
                    /*Get excess bits if any from vbv buffer*/
1833
0
                    vbv_buffer_based_excess = get_vbv_buffer_based_excess(
1834
0
                        ps_cbr_buf_handle,
1835
0
                        ps_bit_allocation->f_min_complexity_cross_peak_rate,
1836
0
                        ((float)i4_lap_window_comp / 128),
1837
0
                        (i4_intra_frm_int * ps_bit_allocation->s_rbip.i4_num_intra_frm_interval),
1838
0
                        1);
1839
0
                }
1840
1841
0
                i4_do_correction = 1;
1842
0
                i4_gop_correction = get_error_bits_for_desired_buf(
1843
0
                    ps_cbr_buf_handle,
1844
0
                    i4_lap_window_comp_temp,
1845
0
                    (i4_intra_frm_int * ps_bit_allocation->s_rbip.i4_num_intra_frm_interval));
1846
                /*In case of VBR, don't do buffer based correction if gop_correction is less than 0, as it is less than average*/
1847
0
                if((e_rc_type == VBR_STREAMING) && (i4_gop_correction <= 0))
1848
0
                {
1849
0
                    i4_do_correction = 0;
1850
0
                }
1851
1852
                /* vbv buffer position based error correction to keep away encoder buffer overflow at GOP (I to I, not user configured)*/
1853
0
                if(i4_do_correction)
1854
0
                {
1855
0
                    WORD32 i4_buffer_err_bits;
1856
                    /*check if the ebf is greater than max ebf,
1857
                    then account for complete error above max ebf in the current GOP itself*/
1858
0
                    if(i4_cur_ebf > i4_max_vbv_size)
1859
0
                    {
1860
0
                        i4_gop_correction -= (i4_cur_ebf - i4_max_vbv_size);
1861
0
                        *pi4_new_correction -= (i4_cur_ebf - i4_max_vbv_size);
1862
0
                        i4_cur_ebf = i4_max_vbv_size;
1863
0
                    }
1864
                    /* if ebf is above min but less than max, then distribute to the next GOPs*/
1865
0
                    if(i4_cur_ebf > i4_min_vbv_size)
1866
0
                    {
1867
0
                        WORD32 i4_num_gops;
1868
0
                        float f_ebf_percent;
1869
                        /*compute the error bits to be distributed over the next GOPs*/
1870
0
                        i4_buffer_err_bits = (i4_cur_ebf - i4_min_vbv_size);
1871
                        /*compute number fo GOPs the error to be distributed
1872
                            high error -> few GOPs, less error -> more GOPs*/
1873
0
                        f_ebf_percent = ((float)i4_cur_ebf / i4_vbv_size);
1874
0
                        i4_num_gops = (WORD32)((1.0 - f_ebf_percent) * 10) + 2;
1875
                        /*add the error bits to the period*/
1876
0
                        i4_gop_correction -= (WORD32)(i4_buffer_err_bits / i4_num_gops);
1877
0
                        *pi4_new_correction -= (WORD32)(i4_buffer_err_bits / i4_num_gops);
1878
0
                    }
1879
0
                }
1880
0
                *pi4_gop_correction = i4_gop_correction;
1881
0
                set_rbip(
1882
0
                    &ps_bit_allocation->s_rbip,
1883
0
                    (i4_gop_correction + (WORD32)vbv_buffer_based_excess));
1884
1885
0
                update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
1886
0
                ASSERT(ps_bit_allocation->i4_bits_from_buffer_in_cur_gop == 0);
1887
0
                trace_printf("\nRBIP updated ");
1888
0
            }
1889
            /* initialise the GOP and bit errors to zero */
1890
92.4k
            ps_bit_allocation->i4_gop_level_bit_error = 0;
1891
            /*frame level error can't be carried over when it is more than VBV buffer size*/
1892
92.4k
            if(ps_bit_allocation->i4_frame_level_bit_error > i4_max_vbv_size)
1893
13.4k
            {
1894
13.4k
                ps_bit_allocation->i4_frame_level_bit_error = i4_max_vbv_size;
1895
13.4k
            }
1896
92.4k
            if((i4_last_frm_in_period) ||
1897
92.4k
               (i4_intra_frm_int == 1 && ps_bit_allocation->i4_rem_frame_in_period == 0))
1898
17.3k
            { /*For 1st pass set the errors to 0 at end of a gop*/
1899
17.3k
                ps_bit_allocation->i8_cur_gop_bit_consumption = 0;
1900
17.3k
                ps_bit_allocation->i4_frame_level_bit_error = 0;
1901
17.3k
                ps_bit_allocation->i4_bits_from_buffer_in_cur_gop = 0;
1902
17.3k
                ps_bit_allocation->i4_rem_frame_in_period =
1903
17.3k
                    ps_bit_allocation->i4_num_gops_in_period *
1904
17.3k
                    i4_intra_frm_int; /*TBD: I only case*/
1905
17.3k
                ps_bit_allocation->i8_frm_num_in_gop = 0;
1906
17.3k
            }
1907
92.4k
        }
1908
92.4k
    }
1909
1910
92.4k
    if(i4_last_frm_in_period && i4_intra_frm_int != 1)
1911
16.8k
    {
1912
        /* If the number of gops in period has been increased due to scene change, slowly bring in down
1913
           across the gops */
1914
16.8k
        if(ps_bit_allocation->i4_num_gops_in_period >
1915
16.8k
           ps_bit_allocation->i4_actual_num_gops_in_period)
1916
0
        {
1917
0
            ps_bit_allocation->i4_num_gops_in_period--;
1918
0
            change_rbip(
1919
0
                &ps_bit_allocation->s_rbip,
1920
0
                ps_bit_allocation->i4_bits_per_frm,
1921
0
                ps_bit_allocation->i4_num_gops_in_period);
1922
0
        }
1923
16.8k
    }
1924
    /*Check for complexity based bits reset in future with GOP*/
1925
1926
    /* Update the lower modules */
1927
92.4k
    update_error_bits(ps_bit_allocation->ps_error_bits);
1928
92.4k
}
1929
1930
/*****************************************************************************
1931
  Function Name : change_remaining_bits_in_period
1932
  Description   :
1933
  Inputs        : ps_bit_allocation - bit_allocation structure
1934
1935
  Globals       :
1936
  Processing    :
1937
  Outputs       :
1938
  Returns       :
1939
  Issues        :
1940
  Revision History:
1941
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
1942
*****************************************************************************/
1943
void change_remaining_bits_in_period(
1944
    bit_allocation_t *ps_bit_allocation,
1945
    WORD32 i4_bit_rate,
1946
    WORD32 i4_frame_rate,
1947
    WORD32 *i4_peak_bit_rate)
1948
4.88k
{
1949
4.88k
    WORD32 i4_new_avg_bits_per_frm, i4_new_peak_bits_per_frm[MAX_NUM_DRAIN_RATES];
1950
4.88k
    int i;
1951
1952
    /* Calculate the new per frame bits */
1953
4.88k
    X_PROD_Y_DIV_Z(i4_bit_rate, 1000, i4_frame_rate, i4_new_avg_bits_per_frm);
1954
1955
14.6k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
1956
9.76k
    {
1957
9.76k
        X_PROD_Y_DIV_Z(i4_peak_bit_rate[i], 1000, i4_frame_rate, i4_new_peak_bits_per_frm[i]);
1958
9.76k
    }
1959
1960
14.6k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
1961
9.76k
    {
1962
9.76k
        ps_bit_allocation->i4_max_bits_per_frm[i] = i4_new_peak_bits_per_frm[i];
1963
9.76k
    }
1964
1965
    /* Get the rem_frms_in_prd & the frms_in_prd from the pic_type state struct */
1966
    /* pic_type_get_rem_frms_in_gop(ps_pic_handling, i4_rem_frms_in_period); */
1967
1968
    /* If the difference > 0(/ <0), the remaining bits in period needs to be increased(/decreased)
1969
    based on the remaining number of frames */
1970
4.88k
    change_rbip(
1971
4.88k
        &ps_bit_allocation->s_rbip,
1972
4.88k
        i4_new_avg_bits_per_frm,
1973
4.88k
        ps_bit_allocation->i4_num_gops_in_period);
1974
1975
    /* Update the new average bits per frame */
1976
4.88k
    ps_bit_allocation->i4_bits_per_frm = i4_new_avg_bits_per_frm;
1977
1978
    /*change max_bits_per_frame*/
1979
    //ps_bit_allocation->i4_max_bits_per_frm[0]=i4_new_avg_bits_per_frm;
1980
    //ps_bit_allocation->i4_max_bits_per_frm[1]=i4_new_avg_bits_per_frm;
1981
4.88k
    ps_bit_allocation->i4_min_bits_per_frm =
1982
4.88k
        i4_new_avg_bits_per_frm; /*VBR storage related parameter so this variable is currently not in use*/
1983
    /* change the lower modules state */
1984
    /*#ifdef DYNAMIC_RC*/
1985
4.88k
    if(i4_bit_rate != ps_bit_allocation->i4_bit_rate)
1986
4.88k
    {
1987
4.88k
        X_PROD_Y_DIV_Z(
1988
4.88k
            ps_bit_allocation->i4_max_tex_bits_for_i,
1989
4.88k
            i4_bit_rate,
1990
4.88k
            ps_bit_allocation->i4_bit_rate,
1991
4.88k
            ps_bit_allocation->i4_max_tex_bits_for_i);
1992
4.88k
    }
1993
    /*#endif*/
1994
1995
4.88k
    change_bitrate_in_error_bits(ps_bit_allocation->ps_error_bits, i4_bit_rate);
1996
4.88k
    change_frm_rate_in_error_bits(ps_bit_allocation->ps_error_bits, i4_frame_rate);
1997
1998
    /* Store the modified frame_rate */
1999
4.88k
    ps_bit_allocation->i4_frame_rate = i4_frame_rate;
2000
4.88k
    ps_bit_allocation->i4_bit_rate = i4_bit_rate;
2001
14.6k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
2002
9.76k
        ps_bit_allocation->ai4_peak_bit_rate[i] = i4_peak_bit_rate[i];
2003
4.88k
}
2004
/*****************************************************************************
2005
  Function Name : change_ba_peak_bit_rate
2006
  Description   :
2007
  Inputs        : ps_bit_allocation - bit_allocation structure
2008
                  ai4_peak_bit_rate
2009
  Globals       :
2010
  Processing    :
2011
  Outputs       :
2012
  Returns       :
2013
  Issues        :
2014
  Revision History:
2015
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2016
*****************************************************************************/
2017
void change_ba_peak_bit_rate(bit_allocation_t *ps_bit_allocation, WORD32 *ai4_peak_bit_rate)
2018
0
{
2019
0
    WORD32 i;
2020
    /* Calculate the bits per frame */
2021
0
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
2022
0
    {
2023
0
        X_PROD_Y_DIV_Z(
2024
0
            ai4_peak_bit_rate[i],
2025
0
            1000,
2026
0
            ps_bit_allocation->i4_frame_rate,
2027
0
            ps_bit_allocation->i4_max_bits_per_frm[i]);
2028
0
        ps_bit_allocation->ai4_peak_bit_rate[i] = ai4_peak_bit_rate[i];
2029
0
    }
2030
0
}
2031
/*****************************************************************************
2032
  Function Name : check_and_update_bit_allocation
2033
  Description   :
2034
  Inputs        : ps_bit_allocation - bit_allocation structure
2035
                  ps_pic_handling
2036
                  i4_max_bits_inflow_per_frm
2037
  Globals       :
2038
  Processing    :
2039
  Outputs       :
2040
  Returns       :
2041
  Issues        :
2042
  Revision History:
2043
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2044
*****************************************************************************/
2045
void check_and_update_bit_allocation(
2046
    bit_allocation_t *ps_bit_allocation,
2047
    pic_handling_handle ps_pic_handling,
2048
    WORD32 i4_max_bits_inflow_per_frm)
2049
0
{
2050
0
    WORD32 i4_max_drain_bits, i4_extra_bits, i4_less_bits, i4_allocated_saved_bits,
2051
0
        i4_min_bits_for_period;
2052
0
    WORD32 i4_num_frms_in_period = get_actual_num_frames_in_gop(ps_pic_handling);
2053
0
    WORD32 i4_rem_bits_in_period = update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, 0);
2054
2055
    /* If the remaining bits is greater than what can be drained in that period
2056
            Clip the remaining bits in period to the maximum it can drain in that pariod
2057
            with the error of current buffer size.Accumulate the saved bits if any.
2058
       else if the remaining bits is lesser than the minimum bit rate promissed in that period
2059
            Add the excess bits to remaining bits in period and reduce it from the saved bits
2060
       Else
2061
            Provide the extra bits from the "saved bits pool".*/
2062
2063
0
    i4_max_drain_bits = ps_bit_allocation->i4_num_gops_in_period * i4_num_frms_in_period *
2064
0
                        i4_max_bits_inflow_per_frm;
2065
2066
    /* Practical DBF = middle of the buffer */
2067
    /* NITT TO BE VERIFIED
2068
    MAx drain bits becomes negative if the buffer underflows
2069
    i4_max_drain_bits += (i4_cur_buf_size + i4_max_bits_inflow_per_frm - i4_tot_frame_bits); */
2070
2071
0
    i4_min_bits_for_period = ps_bit_allocation->i4_num_gops_in_period * i4_num_frms_in_period *
2072
0
                             ps_bit_allocation->i4_min_bits_per_frm;
2073
2074
    /* printf((const WORD8*)" mdb %d, mbfp %d, rbip %d, sb %d \n",i4_max_drain_bits,
2075
        i4_min_bits_for_period, ps_bit_allocation->i4_rem_bits_in_period, ps_bit_allocation->i4_saved_bits); */
2076
0
    if(i4_rem_bits_in_period > i4_max_drain_bits)
2077
0
    {
2078
0
        i4_extra_bits = (i4_rem_bits_in_period - i4_max_drain_bits);
2079
0
        update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, -1 * i4_extra_bits);
2080
0
        overflow_avoided_summation(&ps_bit_allocation->i4_saved_bits, i4_extra_bits);
2081
0
    }
2082
0
    else if(i4_rem_bits_in_period < i4_min_bits_for_period)
2083
0
    {
2084
0
        i4_extra_bits = (i4_min_bits_for_period - i4_rem_bits_in_period);
2085
0
        update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, i4_extra_bits);
2086
0
        overflow_avoided_summation(&ps_bit_allocation->i4_saved_bits, -1 * i4_extra_bits);
2087
0
    }
2088
0
    else if(ps_bit_allocation->i4_saved_bits > 0)
2089
0
    {
2090
0
        i4_less_bits = i4_max_drain_bits - i4_rem_bits_in_period;
2091
0
        i4_allocated_saved_bits = MIN(i4_less_bits, ps_bit_allocation->i4_saved_bits);
2092
0
        update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handling, i4_allocated_saved_bits);
2093
0
        ps_bit_allocation->i4_saved_bits -= i4_allocated_saved_bits;
2094
0
    }
2095
0
    return;
2096
0
}
2097
/*****************************************************************************
2098
  Function Name : ba_get_frame_rate
2099
  Description   :
2100
  Inputs        : ps_bit_allocation - bit_allocation structure
2101
  Revision History:
2102
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2103
*****************************************************************************/
2104
WORD32 ba_get_frame_rate(bit_allocation_t *ps_bit_allocation)
2105
4.88k
{
2106
4.88k
    return (ps_bit_allocation->i4_frame_rate);
2107
4.88k
}
2108
/*****************************************************************************
2109
  Function Name : ba_get_bit_rate
2110
  Description   :
2111
  Inputs        : ps_bit_allocation - bit_allocation structure
2112
  Revision History:
2113
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2114
*****************************************************************************/
2115
WORD32 ba_get_bit_rate(bit_allocation_t *ps_bit_allocation)
2116
4.88k
{
2117
4.88k
    return (ps_bit_allocation->i4_bit_rate);
2118
4.88k
}
2119
/*****************************************************************************
2120
  Function Name : ba_get_2pass_avg_bit_rate
2121
  Description   :
2122
  Inputs        : ps_bit_allocation - bit_allocation structure
2123
  Revision History:
2124
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2125
*****************************************************************************/
2126
LWORD64 ba_get_2pass_avg_bit_rate(bit_allocation_t *ps_bit_allocation)
2127
0
{
2128
0
    return (ps_bit_allocation->i8_2pass_avg_bit_rate);
2129
0
}
2130
/*****************************************************************************
2131
  Function Name : ba_set_2pass_avg_bit_rate
2132
  Description   :
2133
  Inputs        : ps_bit_allocation - bit_allocation structure
2134
  Revision History:
2135
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2136
*****************************************************************************/
2137
void ba_set_2pass_avg_bit_rate(bit_allocation_t *ps_bit_allocation, LWORD64 i8_2pass_avg_bit_rate)
2138
0
{
2139
0
    ps_bit_allocation->i8_2pass_avg_bit_rate = i8_2pass_avg_bit_rate;
2140
0
}
2141
/*****************************************************************************
2142
  Function Name : ba_get_peak_bit_rate
2143
  Description   :
2144
  Inputs        : ps_bit_allocation - bit_allocation structure
2145
  Revision History:
2146
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2147
*****************************************************************************/
2148
void ba_get_peak_bit_rate(bit_allocation_t *ps_bit_allocation, WORD32 *pi4_peak_bit_rate)
2149
0
{
2150
0
    WORD32 i;
2151
0
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
2152
0
    {
2153
0
        pi4_peak_bit_rate[i] = ps_bit_allocation->ai4_peak_bit_rate[i];
2154
0
    }
2155
0
}
2156
/*****************************************************************************
2157
  Function Name : init_intra_header_bits
2158
  Description   :
2159
  Inputs        : ps_bit_allocation - bit_allocation structure
2160
  Revision History:
2161
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2162
*****************************************************************************/
2163
void init_intra_header_bits(bit_allocation_t *ps_bit_allocation, WORD32 i4_intra_header_bits)
2164
0
{
2165
    //ASSERT(i4_intra_header_bits == 0);
2166
0
    ps_bit_allocation->i4_prev_frm_header_bits[0] = i4_intra_header_bits;
2167
0
}
2168
/*****************************************************************************
2169
  Function Name : get_prev_header_bits
2170
  Description   :
2171
  Inputs        : ps_bit_allocation - bit_allocation structure
2172
  Revision History:
2173
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2174
*****************************************************************************/
2175
WORD32 get_prev_header_bits(bit_allocation_t *ps_bit_allocation, WORD32 pic_type)
2176
0
{
2177
    //ASSERT(ps_bit_allocation->i4_prev_frm_header_bits[pic_type] == 0);
2178
0
    return (ps_bit_allocation->i4_prev_frm_header_bits[pic_type]);
2179
0
}
2180
2181
0
#define I_TO_P_RATIO_HI_MO (16)
2182
0
#define P_TO_B_RATIO_HI_MO (18)
2183
0
#define P_TO_B_RATIO_HI_MO_HBR (16)
2184
/*****************************************************************************
2185
  Function Name : set_Kp_Kb_for_hi_motion
2186
  Description   :
2187
  Inputs        : ps_bit_allocation - bit_allocation structure
2188
  Revision History:
2189
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2190
*****************************************************************************/
2191
void set_Kp_Kb_for_hi_motion(bit_allocation_t *ps_bit_allocation)
2192
0
{
2193
0
    ps_bit_allocation->i2_K[I_PIC] = (1 << K_Q);
2194
0
    ps_bit_allocation->i2_K[P_PIC] = I_TO_P_RATIO_HI_MO;
2195
2196
0
    if(ps_bit_allocation->i4_is_hbr)
2197
0
    {
2198
0
        ps_bit_allocation->i2_K[B_PIC] = (P_TO_B_RATIO_HI_MO * I_TO_P_RATIO_HI_MO) >> K_Q;
2199
0
    }
2200
0
    else
2201
0
    {
2202
0
        ps_bit_allocation->i2_K[B_PIC] = (P_TO_B_RATIO_HI_MO_HBR * I_TO_P_RATIO_HI_MO) >> K_Q;
2203
0
    }
2204
0
}
2205
/*****************************************************************************
2206
  Function Name : reset_Kp_Kb
2207
  Description   : I_P_B_B1_B2 QP offset calculation based on hme sad
2208
  Inputs        :
2209
  Globals       :
2210
  Processing    :
2211
  Outputs       :
2212
  Returns       :
2213
  Issues        :
2214
  Revision History:
2215
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2216
*****************************************************************************/
2217
2218
void reset_Kp_Kb(
2219
    bit_allocation_t *ps_bit_allocation,
2220
    float f_i_to_avg_ratio,
2221
    WORD32 i4_num_active_pic_type,
2222
    float f_hme_sad_per_pixel,
2223
    float f_max_hme_sad_per_pixel,
2224
    WORD32 *pi4_complexity_bin,
2225
    WORD32 i4_rc_pass)
2226
0
{
2227
0
    WORD32 i, i4_ratio = (WORD32)(f_max_hme_sad_per_pixel / f_hme_sad_per_pixel);
2228
0
    WORD32 ai4_offsets[5] = { 0 };
2229
0
    float f_ratio = f_max_hme_sad_per_pixel / f_hme_sad_per_pixel;
2230
2231
    /*Filling out the offfset array for QP offset 0 - 7*/
2232
0
    const WORD32 ai4_offset_qp[8] = {
2233
0
        (1 << K_Q),
2234
0
        I_TO_P_RATIO,
2235
0
        ((P_TO_B_RATIO * I_TO_P_RATIO) >> K_Q),
2236
0
        (B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q),
2237
0
        (B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >> (K_Q + K_Q + K_Q),
2238
0
        (B1_TO_B2_RATIO * B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO * I_TO_P_RATIO) >>
2239
0
            (K_Q + K_Q + K_Q + K_Q),
2240
0
        (B1_TO_B2_RATIO * B1_TO_B2_RATIO * B1_TO_B2_RATIO * B_TO_B1_RATIO * P_TO_B_RATIO *
2241
0
         I_TO_P_RATIO) >>
2242
0
            (K_Q + K_Q + K_Q + K_Q + K_Q),
2243
0
        (B1_TO_B2_RATIO * B1_TO_B2_RATIO * B1_TO_B2_RATIO * B1_TO_B2_RATIO * B_TO_B1_RATIO *
2244
0
         P_TO_B_RATIO * I_TO_P_RATIO) >>
2245
0
            (K_Q + K_Q + K_Q + K_Q + K_Q + K_Q)
2246
0
    };
2247
2248
0
    ba_get_qp_offset_offline_data(
2249
0
        ai4_offsets, i4_ratio, f_ratio, i4_num_active_pic_type, pi4_complexity_bin);
2250
0
    for(i = 0; i < 5; i++)
2251
0
    {
2252
0
        ASSERT((ai4_offsets[i] >= 0) && (ai4_offsets[i] <= 7));
2253
0
        ps_bit_allocation->i2_K[i] = ai4_offset_qp[ai4_offsets[i]];
2254
2255
        /*For interlaced also we are filling out the offsets*/
2256
0
        if(i > 0)
2257
0
            ps_bit_allocation->i2_K[i + 4] = ai4_offset_qp[ai4_offsets[i]];
2258
0
    }
2259
0
}
2260
2261
/*****************************************************************************
2262
  Function Name : ba_get_qp_offset_offline_data
2263
  Description   : Offline model for qp offset calculation
2264
  Inputs        : ai4_offsets
2265
                  i4_ratio
2266
                  f_ratio
2267
                  i4_num_active_pic_type
2268
                  pi4_complexity_bin
2269
  Revision History:
2270
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2271
*****************************************************************************/
2272
void ba_get_qp_offset_offline_data(
2273
    WORD32 ai4_offsets[5],
2274
    WORD32 i4_ratio,
2275
    float f_ratio,
2276
    WORD32 i4_num_active_pic_type,
2277
    WORD32 *pi4_complexity_bin)
2278
0
{
2279
0
    WORD32 i4_bin;
2280
    /*Desired QP offset's for different complexity bins depending on number of temporal layers*/
2281
    /*There are 6 complexity bins
2282
    Max_compl - Max_compl*3/4,
2283
    Max_compl*3/4 - Max_compl*1/2,
2284
    Max_compl*1/2 - Max_compl*1/4,
2285
    Max_compl*1/4 - Max_compl*1/8,
2286
    Max_compl*1/8 - Max_compl*1/16
2287
    <Max_compl*1/16*/
2288
    /*The kids_rain content was run on different resolutions and the max value for different temporal configs is the max value used*/
2289
2290
    /*First index for complexity bin, second index for pic_types (P,B,B1,B2)*/
2291
0
    const WORD32 ai4_offset_values_7B[7][4] = { { 0, 1, 1, 2 }, { 1, 1, 2, 3 }, { 1, 2, 3, 3 },
2292
0
                                                { 1, 2, 3, 4 }, { 2, 2, 3, 4 }, { 2, 3, 4, 5 },
2293
0
                                                { 3, 4, 5, 6 } };
2294
0
    const WORD32 ai4_offset_values_3B[7][3] = { { 0, 1, 2 }, { 1, 2, 2 }, { 1, 2, 3 }, { 2, 2, 3 },
2295
0
                                                { 2, 3, 4 }, { 2, 4, 5 }, { 3, 4, 5 } };
2296
0
    const WORD32 ai4_offset_values_1B[7][2] = { { 1, 1 }, { 1, 2 }, { 1, 2 }, { 1, 3 },
2297
0
                                                { 2, 3 }, { 3, 4 }, { 3, 5 } };
2298
0
    const WORD32 ai4_offset_values_0B[7][1] = { { 0 }, { 1 }, { 2 }, { 2 }, { 3 }, { 3 }, { 4 } };
2299
2300
    /*The ratio is clipped between 16 and 2 to put it into bins*/
2301
2302
0
    CLIP(i4_ratio, 16, 2);
2303
2304
0
    for(i4_bin = 1; i4_bin < 5; i4_bin++)
2305
0
    {
2306
0
        if((i4_ratio >> i4_bin) == 1)
2307
0
        {
2308
0
            break;
2309
0
        }
2310
0
    }
2311
0
    switch(i4_bin)
2312
0
    {
2313
0
    case(1):
2314
0
        (f_ratio > 2.0f) ? (i4_bin = 3) : ((f_ratio > 1.33f) ? (i4_bin = 2) : (i4_bin = 1));
2315
0
        break;
2316
0
    case(2):
2317
0
        i4_bin = 4;
2318
0
        break;
2319
0
    case(3):
2320
0
        (f_ratio > 12.0f) ? (i4_bin = 6) : (i4_bin = 5);
2321
0
        break;
2322
0
    case(4):
2323
0
        i4_bin = 7;
2324
0
        break;
2325
0
    }
2326
2327
    /*For the i4_bin == 1, actual ratio could be >2.0,>1.33 or lesser hence putting them into different bins*/
2328
2329
0
    trace_printf("1 bin %d", i4_bin);
2330
2331
    /*Total 7 bins hence the clip*/
2332
0
    CLIP(i4_bin, 7, 1);
2333
2334
0
    *pi4_complexity_bin = i4_bin - 1;
2335
2336
0
    switch(i4_num_active_pic_type)
2337
0
    {
2338
0
    case 5:
2339
0
        memmove(
2340
0
            &ai4_offsets[1],
2341
0
            ai4_offset_values_7B[i4_bin - 1],
2342
0
            sizeof(ai4_offset_values_7B[i4_bin - 1]));
2343
0
        break;
2344
0
    case 4:
2345
0
        memmove(
2346
0
            &ai4_offsets[1],
2347
0
            ai4_offset_values_3B[i4_bin - 1],
2348
0
            sizeof(ai4_offset_values_3B[i4_bin - 1]));
2349
0
        break;
2350
0
    case 3:
2351
0
        memmove(
2352
0
            &ai4_offsets[1],
2353
0
            ai4_offset_values_1B[i4_bin - 1],
2354
0
            sizeof(ai4_offset_values_1B[i4_bin - 1]));
2355
0
        break;
2356
0
    case 2:
2357
0
        memmove(
2358
0
            &ai4_offsets[1],
2359
0
            ai4_offset_values_0B[i4_bin - 1],
2360
0
            sizeof(ai4_offset_values_0B[i4_bin - 1]));
2361
0
        break;
2362
0
    default:
2363
0
        memmove(
2364
0
            &ai4_offsets[1],
2365
0
            ai4_offset_values_0B[i4_bin - 1],
2366
0
            sizeof(ai4_offset_values_0B[i4_bin - 1]));
2367
0
        break;
2368
0
    }
2369
2370
0
    trace_printf(
2371
0
        "Enc %d,%d,%d,%d,%d offsets",
2372
0
        ai4_offsets[0],
2373
0
        ai4_offsets[1],
2374
0
        ai4_offsets[2],
2375
0
        ai4_offsets[3],
2376
0
        ai4_offsets[4]);
2377
0
}
2378
2379
/*****************************************************************************
2380
  Function Name : get_Kp_Kb
2381
  Description   : Get the operating Kp and Kp so that scene cut sub gop can go
2382
                  with similar qp offset
2383
  Inputs        : ps_bit_allocation
2384
                  e_pic_type
2385
  Revision History:
2386
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2387
*****************************************************************************/
2388
2389
WORD32 get_Kp_Kb(bit_allocation_t *ps_bit_allocation, picture_type_e e_pic_type)
2390
59.0k
{
2391
59.0k
    return ps_bit_allocation->i2_K[e_pic_type];
2392
59.0k
}
2393
/*****************************************************************************
2394
  Function Name : get_scene_change_tot_frm_bits
2395
  Description   : Based on remaining bits in period and default I_TO_B complexity
2396
                  total bit budget for scene cut frame is obtained.
2397
  Inputs        : ps_bit_allocation - bit_allocation structure
2398
                  ps_rd_model - rd model pointer (for all the frame types)
2399
                  e_pic_type - picture type
2400
  Globals       :
2401
  Processing    :
2402
  Outputs       :
2403
  Returns       :
2404
  Issues        :
2405
  Revision History:
2406
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2407
*****************************************************************************/
2408
WORD32 get_scene_change_tot_frm_bits(
2409
    bit_allocation_t *ps_bit_allocation,
2410
    pic_handling_handle ps_pic_handling,
2411
    cbr_buffer_handle ps_cbr_buf_handling,
2412
    WORD32 i4_num_pixels,
2413
    WORD32 i4_f_sim_lap,
2414
    float i_to_avg_rest,
2415
    WORD32 i4_call_type,
2416
    WORD32 i4_non_I_scd,
2417
    WORD32 i4_is_infinite_gop)
2418
654k
{
2419
654k
    WORD32 j;
2420
654k
    WORD32 i4_tot_bits_for_scd_frame;
2421
654k
    WORD32 i4_total_bits_in_period;
2422
    //number_t avq_complexity_estimate[MAX_PIC_TYPE];
2423
654k
    WORD32 /* ai4_rem_frms_in_period[MAX_PIC_TYPE], */
2424
654k
        ai4_frms_in_period[MAX_PIC_TYPE];
2425
654k
    WORD32 i4_max_consumable_bits;
2426
654k
    WORD32 i4_intra_frm_int;
2427
654k
    WORD32 ai4_actual_frms_in_gop[MAX_PIC_TYPE], i, i4_total_frames = 0;
2428
654k
    float final_ratio, f_sim = (float)i4_f_sim_lap / 128;
2429
2430
654k
    i4_intra_frm_int = pic_type_get_intra_frame_interval(ps_pic_handling);
2431
2432
    /* Get the rem_frms_in_gop & the frms_in_gop from the pic_type state struct */
2433
    /* pic_type_get_rem_frms_in_gop(ps_pic_handling, ai4_rem_frms_in_period); */
2434
654k
    pic_type_get_frms_in_gop(ps_pic_handling, ai4_frms_in_period);
2435
2436
    /* Depending on the number of gops in a period, find the num_frms_in_prd */
2437
6.54M
    for(j = 0; j < MAX_PIC_TYPE; j++)
2438
5.88M
    {
2439
        /* ai4_rem_frms_in_period[j] += (ai4_frms_in_period[j] * (ps_bit_allocation->i4_num_gops_in_period - 1)); */
2440
5.88M
        ai4_frms_in_period[j] *= ps_bit_allocation->i4_num_gops_in_period;
2441
5.88M
    }
2442
2443
    /* Remove the header bits from the remaining bits to find how many bits you
2444
       can transfer.*/
2445
654k
    {
2446
654k
        i4_total_bits_in_period = ps_bit_allocation->s_rbip.i4_bits_per_frm *
2447
654k
                                  ps_bit_allocation->s_rbip.i4_tot_frms_in_gop;
2448
        //trace_printf(" SCD_rbip = %d",i4_total_bits_in_period);
2449
654k
    }
2450
    //since this marks end of previous GOP it is better to consider actual error than ps_bit_allocation->i4_frame_level_bit_error;
2451
2452
654k
    {
2453
654k
        pic_type_get_actual_frms_in_gop(ps_pic_handling, ai4_actual_frms_in_gop);
2454
6.54M
        for(i = 0; i < MAX_PIC_TYPE; i++)
2455
5.88M
        {
2456
5.88M
            i4_total_frames += ai4_frms_in_period[i];
2457
5.88M
        }
2458
654k
        i4_max_consumable_bits = ps_bit_allocation->i4_max_bits_per_frm[0] * i4_total_frames;
2459
654k
    }
2460
654k
    if(i4_total_bits_in_period > 0)
2461
654k
    {
2462
654k
        i4_total_bits_in_period = MIN(i4_total_bits_in_period, i4_max_consumable_bits);
2463
654k
    }
2464
654k
    final_ratio = i_to_avg_rest;
2465
    /*If FSIM says the content is static (> 126 is assured to be static*/
2466
    /*Very low FSIM safety check*/
2467
654k
    if(f_sim < 0.50 && final_ratio > 8)
2468
0
        final_ratio = 8;
2469
    /*Do not apply safety limits if second pass as data is reliable*/
2470
654k
    if(ps_bit_allocation->i4_ba_rc_pass != 2)
2471
654k
    {
2472
        /*clip min max values*/
2473
654k
        if((i4_is_infinite_gop == 1) && (final_ratio > I_TO_AVG_REST_GOP_BIT_MAX_INFINITE))
2474
3.01k
        {
2475
3.01k
            final_ratio = I_TO_AVG_REST_GOP_BIT_MAX_INFINITE;
2476
3.01k
        }
2477
651k
        else
2478
651k
        {
2479
651k
            if(final_ratio > I_TO_AVG_REST_GOP_BIT_MAX)
2480
33.0k
                final_ratio = I_TO_AVG_REST_GOP_BIT_MAX;
2481
651k
        }
2482
654k
        if(final_ratio < I_TO_AVG_REST_GOP_BIT_MIN)
2483
7
            final_ratio = I_TO_AVG_REST_GOP_BIT_MIN;
2484
654k
    }
2485
0
    else
2486
0
    {
2487
0
        if(final_ratio > I_TO_AVG_REST_GOP_BIT_MAX_2_PASS)
2488
0
            final_ratio = I_TO_AVG_REST_GOP_BIT_MAX_2_PASS;
2489
2490
0
        if(final_ratio < I_TO_AVG_REST_GOP_BIT_MIN_2_PASS)
2491
0
            final_ratio = I_TO_AVG_REST_GOP_BIT_MIN_2_PASS;
2492
0
    }
2493
2494
    /*based on offline runs to find I_BITS/(AVERAGE_CONSUMPTION_OF_REST_GOP)*/
2495
    /*  BITS FOR I
2496
        BITS = I_TO_AVG_REST_GOP * total_bits_period
2497
                -------------------------------------
2498
                N - (num_I_in_period) + (I_TO_AVG_REST_GOP * num_I_in_period)
2499
                */
2500
654k
    i4_tot_bits_for_scd_frame = bit_alloc_get_intra_bits(
2501
654k
        ps_bit_allocation,
2502
654k
        ps_pic_handling,
2503
654k
        ps_cbr_buf_handling,
2504
654k
        I_PIC,
2505
654k
        NULL,
2506
654k
        1,
2507
654k
        final_ratio,
2508
654k
        i4_call_type,
2509
654k
        i4_non_I_scd,
2510
654k
        0.0f);
2511
654k
    ps_bit_allocation->i4_excess_bits_from_buffer = 0;
2512
2513
654k
    if(i4_call_type == 1)
2514
270k
    {
2515
270k
        trace_printf("I_TO_AVG_REST_GOP_BIT used = %f\n", final_ratio);
2516
270k
        trace_printf(" SCD DETECTED   bits allocated = %d", i4_tot_bits_for_scd_frame);
2517
270k
    }
2518
2519
    /* If the remaining bits in the period becomes negative then the estimated texture
2520
    bits would also become negative. This would send a feedback to the model which
2521
    may go for a toss. Thus sending the minimum possible value = 0 */
2522
654k
    if(i4_tot_bits_for_scd_frame < 0)
2523
0
        i4_tot_bits_for_scd_frame = 0;
2524
2525
654k
    return (i4_tot_bits_for_scd_frame);
2526
654k
}
2527
2528
/*****************************************************************************
2529
  Function Name : update_estimate_status
2530
  Description   : Est texture bits in case of scene cut is obtained form offline
2531
                  model. Update bit alloc
2532
  Inputs        : ps_bit_allocation
2533
                  e_pic_type
2534
  Revision History:
2535
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2536
*****************************************************************************/
2537
2538
void update_estimate_status(
2539
    bit_allocation_t *ps_bit_allocation,
2540
    WORD32 i4_est_texture_bits,
2541
    WORD32 i4_hdr_bits,
2542
    WORD32 i4_est_text_bits_ctr_get_qp)
2543
92.4k
{
2544
92.4k
    ps_bit_allocation->ai4_cur_frm_est_tex_bits[i4_est_text_bits_ctr_get_qp] = i4_est_texture_bits;
2545
92.4k
    ps_bit_allocation->ai4_cur_frm_est_hdr_bits[i4_est_text_bits_ctr_get_qp] = i4_hdr_bits;
2546
92.4k
}
2547
2548
/*****************************************************************************
2549
  Function Name : bit_allocation_set_num_scd_lap_window
2550
  Description   :
2551
  Inputs        : ps_bit_allocation
2552
                  i4_num_scd_in_lap_window
2553
  Revision History:
2554
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2555
*****************************************************************************/
2556
void bit_allocation_set_num_scd_lap_window(
2557
    bit_allocation_t *ps_bit_allocation,
2558
    WORD32 i4_num_scd_in_lap_window,
2559
    WORD32 i4_num_frames_b4_Scd)
2560
199k
{
2561
199k
    ps_bit_allocation->i4_num_scd_in_lap_window = i4_num_scd_in_lap_window;
2562
199k
    ps_bit_allocation->i4_num_frm_b4_scd = i4_num_frames_b4_Scd;
2563
    /*To avoid trashing I frame badly due to back to back scene cut limit the increment in Ni*/
2564
199k
    if(ps_bit_allocation->i4_num_scd_in_lap_window > 2)
2565
0
        ps_bit_allocation->i4_num_scd_in_lap_window = 2;
2566
199k
}
2567
/*****************************************************************************
2568
  Function Name : bit_allocation_set_sc_i_in_rc_look_ahead
2569
  Description   :
2570
  Inputs        : ps_bit_allocation
2571
                  i4_next_sc_i_in_rc_look_ahead
2572
  Revision History:
2573
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2574
*****************************************************************************/
2575
void bit_allocation_set_sc_i_in_rc_look_ahead(
2576
    bit_allocation_t *ps_bit_allocation, WORD32 i4_next_sc_i_in_rc_look_ahead)
2577
0
{
2578
0
    ps_bit_allocation->i4_next_sc_i_in_rc_look_ahead = i4_next_sc_i_in_rc_look_ahead;
2579
0
}
2580
/*****************************************************************************
2581
  Function Name : bit_allocation_update_gop_level_bit_error
2582
  Description   :
2583
  Inputs        : ps_bit_allocation
2584
                  i4_error_bits
2585
  Revision History:
2586
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2587
*****************************************************************************/
2588
void bit_allocation_update_gop_level_bit_error(
2589
    bit_allocation_t *ps_bit_allocation, WORD32 i4_error_bits)
2590
120k
{
2591
120k
    ps_bit_allocation->i4_gop_level_bit_error += i4_error_bits;
2592
120k
    ps_bit_allocation->i4_frame_level_bit_error += i4_error_bits;
2593
    /*Error is (rdopt - entropy) Hence for total bit consumption subtract error*/
2594
120k
    ps_bit_allocation->i8_cur_gop_bit_consumption -= i4_error_bits;
2595
120k
}
2596
2597
/******************************************************************************
2598
  Function Name   : rc_update_bit_distribution_gop_level_2pass
2599
  Description     : This function distributes the bits to all the gops depending
2600
                    on the complexities and the error bits accumulated until now
2601
  Arguments       : ps_rate_control_api - rate control api handle
2602
                    i4_start_gop_number : GOP number from which distribution should happen
2603
  Return Values   :
2604
  Revision History:
2605
2606
2607
 Assumptions    -
2608
2609
 Checks     -
2610
*****************************************************************************/
2611
void rc_update_bit_distribution_gop_level_2pass(
2612
    bit_allocation_t *ps_bit_allocation,
2613
    pic_handling_handle ps_pic_handle,
2614
    void *pv_gop_stat,
2615
    rc_type_e e_rc_type,
2616
    WORD32 i4_num_gop,
2617
    WORD32 i4_start_gop_number,
2618
    float f_avg_qscale_first_pass,
2619
    WORD32 i4_max_ebf,
2620
    WORD32 i4_ebf,
2621
    LWORD64 i8_tot_bits_sequence,
2622
    WORD32 i4_comp_error)
2623
0
{
2624
0
    float cur_peak_factor, f_bits_per_frame;
2625
0
    LWORD64 total_nbp_bits_allocated = 0;
2626
0
    LWORD64 total_bp_bits_allocated = 0;
2627
0
    LWORD64 total_bits_allocated = 0, prev_total_bits_allocated = -1;
2628
0
    WORD32
2629
0
    i4_num_loop_inter_GOP_alloc = 0, ai4_peak_bitrate[MAX_NUM_DRAIN_RATES] = { 0 },
2630
0
    temp_i; /*Loop 20 times to meet precise bitrate, after that exit the loop and distribute remaining bits equally for all GOP*/
2631
0
    gop_level_stat_t *ps_cur_gop;
2632
0
    WORD32 i4_num_frames_in_gop, i4_cur_gop_num, i4_num_frm_with_rmax, i4_num_frm_with_rmin;
2633
0
    LWORD64 i8_max_bit_for_gop, /*i8_min_bit_for_gop,*/ i8_peak_bitrate, i8_frame_rate,
2634
0
        i8_current_bitrate = (LWORD64)ba_get_2pass_avg_bit_rate(ps_bit_allocation);
2635
0
    LWORD64 i8_actual_avg_bit_rate = ba_get_bit_rate(ps_bit_allocation);
2636
0
    LWORD64 i8_num_frame_remaining = 0, i8_excess_bits = 0;
2637
0
    float min_complexity_beyond_peak /*,f_max_complexity = 1.0f,f_min_complexity = 0.0f*/
2638
0
        ;  //The minimum complexity for which bit allocation exceeds peak rate but
2639
0
    float f_avg_bits_complexity_based;
2640
0
    WORD32 i4_num_gop_not_rmax;
2641
0
    LWORD64 i8_bits_for_this_gop;
2642
2643
0
#define MAX_LOOP_INTER_GOP_ALLOC                                                                   \
2644
0
    20 /*The below loop shall run maximum of this macro once it exits allocate the difference bits equally for all the GOPS*/
2645
2646
0
    i4_ebf = MAX(i4_ebf, 0);
2647
    //i4_ebf = 0;
2648
0
    if(i4_start_gop_number == 0)
2649
0
    {
2650
0
        cur_peak_factor = 7.0;
2651
0
    }
2652
0
    else
2653
0
    {
2654
0
        cur_peak_factor = ps_bit_allocation->f_cur_peak_factor_2pass;
2655
0
    }
2656
    /*Parsing of entire log file is done and summary of GOP level data has been updated in the temp,
2657
    Iteratively allocate the bits to make it meet bitrate*/
2658
0
    for(temp_i = i4_start_gop_number; temp_i < i4_num_gop; temp_i++)
2659
0
    {
2660
0
        ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + temp_i);
2661
0
    }
2662
0
    i8_frame_rate = ba_get_frame_rate(ps_bit_allocation);
2663
0
    ba_get_peak_bit_rate(ps_bit_allocation, &ai4_peak_bitrate[0]);
2664
0
    i8_peak_bitrate = (LWORD64)ai4_peak_bitrate[0];
2665
2666
    /*Modify the bitrate depending on the error bits and total bits*/
2667
    //i8_current_bitrate = (LWORD64)((float)i8_tot_bits_sequence*i8_frame_rate/(1000*i8_num_frame_remaining));
2668
2669
0
    f_bits_per_frame = (float)i8_current_bitrate / i8_frame_rate * 1000;
2670
0
    ps_bit_allocation->i8_current_bitrate_2_pass = i8_current_bitrate;
2671
    //printf("\n%d current bitrate",i8_current_bitrate);
2672
2673
0
    do
2674
0
    {
2675
        /*Get gop level stat*/
2676
        /*recalculate the bits based on new scaling factor*/
2677
0
        total_bits_allocated = 0;
2678
0
        total_bp_bits_allocated = 0;
2679
0
        total_nbp_bits_allocated = 0;
2680
0
        min_complexity_beyond_peak =
2681
0
            (float)ps_bit_allocation->ai4_peak_bit_rate[0] / i8_current_bitrate;
2682
2683
        /*min_complexity_beyond_peak = ba_get_min_complexity_for_peak_br(ps_bit_allocation->ai4_peak_bit_rate[0],
2684
                                            (WORD32)i8_current_bitrate,
2685
                                            cur_peak_factor,
2686
                                            f_max_complexity,
2687
                                            f_min_complexity,
2688
                                            ps_bit_allocation->i4_ba_rc_pass);*/
2689
2690
0
        for(i4_cur_gop_num = i4_start_gop_number; i4_cur_gop_num < i4_num_gop; i4_cur_gop_num++)
2691
0
        {
2692
0
            ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + i4_cur_gop_num);
2693
0
            ps_cur_gop->f_bits_complexity_l1_based_peak_factor =
2694
0
                ps_cur_gop->f_bits_complexity_l1_based * cur_peak_factor;
2695
0
        }
2696
0
        i4_num_frm_with_rmax = 0;
2697
0
        i4_num_frm_with_rmin = 0;
2698
0
        f_avg_bits_complexity_based = 0.0;
2699
0
        i4_num_gop_not_rmax = 0;
2700
0
        i8_num_frame_remaining = 0;
2701
0
        for(i4_cur_gop_num = i4_start_gop_number; i4_cur_gop_num < i4_num_gop; i4_cur_gop_num++)
2702
0
        {
2703
0
            ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + i4_cur_gop_num);
2704
0
            if(!ps_cur_gop->i4_peak_br_clip)
2705
0
            {
2706
0
                f_avg_bits_complexity_based +=
2707
0
                    (ps_cur_gop->f_bits_complexity_l1_based * ps_cur_gop->i4_tot_frm_in_gop);
2708
0
                i8_num_frame_remaining += ps_cur_gop->i4_tot_frm_in_gop;
2709
0
                i4_num_gop_not_rmax++;
2710
0
            }
2711
0
        }
2712
0
        f_avg_bits_complexity_based = (f_avg_bits_complexity_based / i8_num_frame_remaining);
2713
0
        for(i4_cur_gop_num = i4_start_gop_number; i4_cur_gop_num < i4_num_gop; i4_cur_gop_num++)
2714
0
        {
2715
            /*Parse through all the GOP*/
2716
            /*get current gop data*/
2717
            //i4_num_frames_in_gop = 0;
2718
0
            LWORD64 i8_avg_bit_rate_bits;
2719
0
            LWORD64 i8_curr_bit_rate_bits;
2720
0
            ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + i4_cur_gop_num);
2721
2722
0
            if(ps_cur_gop->i4_peak_br_clip)
2723
0
            {
2724
0
                i4_num_frm_with_rmax++;
2725
0
                total_nbp_bits_allocated += ps_cur_gop->i8_bits_allocated_to_gop;
2726
0
                continue;
2727
0
            }
2728
0
            ps_cur_gop->f_buffer_play_complexity = 0.;
2729
            //ps_cur_gop->f_gop_level_complexity_sum = -1;
2730
            //ps_cur_gop->i8_buffer_play_bits = 0;
2731
0
            ps_cur_gop->i8_buffer_play_bits_allocated_to_gop = 0;
2732
0
            i4_num_frames_in_gop = ps_cur_gop->i4_tot_frm_in_gop;
2733
2734
0
            if(i4_num_gop_not_rmax == i4_num_gop)
2735
0
            {
2736
0
                i8_bits_for_this_gop =
2737
0
                    (LWORD64)((i8_current_bitrate * i4_num_frames_in_gop * 1000) / i8_frame_rate);
2738
0
                if(e_rc_type == VBR_STREAMING)
2739
0
                {
2740
0
                    ps_cur_gop->i8_bits_allocated_to_gop = (LWORD64)(
2741
0
                        (ps_cur_gop->f_bits_complexity_l1_based / (f_avg_bits_complexity_based)) *
2742
0
                        i8_bits_for_this_gop);
2743
0
                }
2744
0
                else
2745
0
                {
2746
0
                    ps_cur_gop->i8_bits_allocated_to_gop =
2747
0
                        (LWORD64)(i8_current_bitrate * i4_num_frames_in_gop / i8_frame_rate * 1000);
2748
0
                }
2749
0
            }
2750
0
            else
2751
0
            {
2752
                //i8_bits_for_this_gop = (LWORD64)((i8_excess_bits * i4_num_frames_in_gop * 1000)/(i8_frame_rate*i4_num_gop_not_rmax));
2753
0
                i8_bits_for_this_gop =
2754
0
                    (LWORD64)((i8_excess_bits * i4_num_frames_in_gop) / (i8_num_frame_remaining));
2755
0
                if(e_rc_type == VBR_STREAMING)
2756
0
                {
2757
0
                    ps_cur_gop->i8_bits_allocated_to_gop += (LWORD64)(
2758
0
                        (ps_cur_gop->f_bits_complexity_l1_based / (f_avg_bits_complexity_based)) *
2759
0
                        i8_bits_for_this_gop);
2760
0
                }
2761
0
                else
2762
0
                {
2763
0
                    ASSERT(0);
2764
0
                }
2765
0
            }
2766
0
            ps_cur_gop->i8_actual_bits_allocated_to_gop = ps_cur_gop->i8_bits_allocated_to_gop;
2767
            /*clip based on peak rate*/
2768
0
            i8_max_bit_for_gop = i8_peak_bitrate * i4_num_frames_in_gop * 1000 / i8_frame_rate;
2769
0
            ps_cur_gop->i8_max_bit_for_gop = i8_max_bit_for_gop;
2770
0
            ps_cur_gop->i4_peak_br_clip = 0;
2771
0
            if(ps_cur_gop->i8_bits_allocated_to_gop > i8_max_bit_for_gop)
2772
0
            {
2773
0
                ps_cur_gop->i8_bits_allocated_to_gop = i8_max_bit_for_gop;
2774
0
                ps_cur_gop->i4_peak_br_clip = 1;
2775
0
                i4_num_frm_with_rmax++;
2776
                /*if(ps_cur_gop->f_bits_complexity_l1_based < min_complexity_beyond_peak)
2777
                    min_complexity_beyond_peak = ps_cur_gop->f_bits_complexity_l1_based;*/
2778
0
            }
2779
0
            i8_curr_bit_rate_bits =
2780
0
                (LWORD64)(i8_current_bitrate * i4_num_frames_in_gop / i8_frame_rate * 1000);
2781
0
            i8_avg_bit_rate_bits =
2782
0
                (LWORD64)(i8_actual_avg_bit_rate * i4_num_frames_in_gop / i8_frame_rate * 1000);
2783
0
            ps_cur_gop->i4_is_below_avg_rate_gop_frame = 0;
2784
0
            if(ps_cur_gop->i8_bits_allocated_to_gop <
2785
0
               (MIN(i8_curr_bit_rate_bits, ps_cur_gop->i8_minimum_gop_bits)))
2786
0
            {
2787
0
                ps_cur_gop->i4_is_below_avg_rate_gop_frame = 1;
2788
0
                ps_cur_gop->i8_bits_allocated_to_gop =
2789
0
                    MIN(i8_curr_bit_rate_bits, ps_cur_gop->i8_minimum_gop_bits);
2790
0
                i4_num_frm_with_rmin++;
2791
0
            }
2792
0
            total_nbp_bits_allocated += ps_cur_gop->i8_bits_allocated_to_gop;
2793
0
        }
2794
0
        i4_num_loop_inter_GOP_alloc++;
2795
        /*check for tolerance of 0.5% in terms of meeting bitrate, terminate the loop when bitrate is met*/
2796
0
        total_bits_allocated = total_nbp_bits_allocated + total_bp_bits_allocated;
2797
0
        if((total_bits_allocated < (1.005 * i8_tot_bits_sequence) &&
2798
0
            total_bits_allocated > (0.995 * i8_tot_bits_sequence)) ||
2799
0
           (i4_num_loop_inter_GOP_alloc > MAX_LOOP_INTER_GOP_ALLOC) /*|| (cur_peak_factor <= 1 )*/)
2800
0
        {
2801
0
            float error_bits = ((float)i8_tot_bits_sequence - total_bits_allocated);
2802
0
            WORD32 temp_i;
2803
0
            float f_per_frm_bits = ((float)(i8_current_bitrate)) / (i8_frame_rate / 1000);
2804
            //cur_peak_factor *= (float)i8_tot_bits_sequence/total_bits_allocated;
2805
0
            if((i4_comp_error == 1) || ((i4_comp_error == 0) && (error_bits < 0)))
2806
0
            {
2807
0
                for(temp_i = i4_start_gop_number; temp_i < i4_num_gop; temp_i++)
2808
0
                {
2809
0
                    ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + temp_i);
2810
0
                    ps_cur_gop->i8_bits_allocated_to_gop += (LWORD64)(
2811
0
                        (error_bits * ps_cur_gop->i8_bits_allocated_to_gop / total_bits_allocated));
2812
0
                }
2813
0
            }
2814
0
            for(temp_i = i4_start_gop_number; temp_i < i4_num_gop; temp_i++)
2815
0
            {
2816
0
                ps_cur_gop = (gop_level_stat_t *)((gop_level_stat_t *)pv_gop_stat + temp_i);
2817
0
                ps_cur_gop->f_avg_complexity_factor = (ps_cur_gop->f_bits_complexity_l1_based /
2818
0
                                                       ps_cur_gop->i8_bits_allocated_to_gop) *
2819
0
                                                      (f_per_frm_bits) *
2820
0
                                                      (ps_cur_gop->i4_tot_frm_in_gop);
2821
0
            }
2822
0
            break;
2823
0
        }
2824
0
        else
2825
0
        {
2826
            /*Go for next iteration*/
2827
0
            cur_peak_factor *= (float)i8_tot_bits_sequence / total_bits_allocated;
2828
            //cur_peak_factor = MAX(cur_peak_factor,1);
2829
0
            prev_total_bits_allocated = total_bits_allocated;
2830
0
            i8_excess_bits = i8_tot_bits_sequence - total_bits_allocated;
2831
0
        }
2832
2833
0
    } while(1);
2834
0
    ps_bit_allocation->f_cur_peak_factor_2pass = cur_peak_factor;
2835
0
    ps_bit_allocation->i8_total_bits_allocated = total_bits_allocated;
2836
2837
    /*Store complexity beyond which bits are clipped to peak rate*/
2838
    /*if(i4_start_gop_number == 0)*/
2839
0
    {
2840
0
        ps_bit_allocation->f_min_complexity_cross_peak_rate = /*min_complexity_beyond_peak*/
2841
0
            (float)ps_bit_allocation->ai4_peak_bit_rate[0] / i8_current_bitrate;
2842
        //ba_get_min_complexity_for_peak_br(ps_bit_allocation->ai4_peak_bit_rate[0],ps_bit_allocation->i4_bit_rate,cur_peak_factor,f_max_complexity,f_min_complexity,ps_bit_allocation->i4_ba_rc_pass);
2843
0
    }
2844
0
}
2845
2846
/*****************************************************************************
2847
  Function Name : get_prev_frame_total_header_bits
2848
  Description   :
2849
  Inputs        : ps_bit_allocation
2850
                  e_pic_type
2851
  Revision History:
2852
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2853
*****************************************************************************/
2854
void get_prev_frame_total_header_bits(
2855
    bit_allocation_t *ps_bit_allocation,
2856
    WORD32 *pi4_prev_frame_total_bits,
2857
    WORD32 *pi4_prev_frame_header_bits,
2858
    picture_type_e e_pic_type)
2859
0
{
2860
0
    *pi4_prev_frame_total_bits = ps_bit_allocation->ai4_prev_frm_tot_bits[e_pic_type];
2861
0
    *pi4_prev_frame_header_bits = ps_bit_allocation->i4_prev_frm_header_bits[e_pic_type];
2862
0
}
2863
2864
/*****************************************************************************
2865
  Function Name : bit_alloc_get_gop_num
2866
  Description   :
2867
  Inputs        : ps_bit_allocation
2868
2869
  Revision History:
2870
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2871
*****************************************************************************/
2872
LWORD64 bit_alloc_get_gop_num(bit_allocation_t *ps_bit_allocation)
2873
0
{
2874
0
    return (ps_bit_allocation->i8_cur_gop_num);
2875
0
}
2876
/*****************************************************************************
2877
  Function Name : ba_get_min_bits_per_frame
2878
  Description   :
2879
  Inputs        : ps_bit_allocation
2880
2881
  Revision History:
2882
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2883
*****************************************************************************/
2884
WORD32 ba_get_min_bits_per_frame(bit_allocation_t *ps_bit_allocation)
2885
0
{
2886
0
    return (ps_bit_allocation->i4_min_bits_per_frm);
2887
0
}
2888
/*****************************************************************************
2889
  Function Name : set_bit_allocation_i_frames
2890
  Description   :
2891
  Inputs        : ps_bit_allocation
2892
2893
  Revision History:
2894
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2895
*****************************************************************************/
2896
void set_bit_allocation_i_frames(
2897
    bit_allocation_t *ps_bit_allocation,
2898
    cbr_buffer_handle ps_cbr_buffer,
2899
    pic_handling_handle ps_pic_handle,
2900
    WORD32 i4_lap_window_comp,
2901
    WORD32 i4_num_frames)
2902
0
{
2903
0
    LWORD64 vbv_buffer_based_excess = 0;
2904
0
    WORD32 i4_gop_correction;
2905
0
    WORD32 i4_lap_window_comp_temp = i4_lap_window_comp;
2906
0
    rc_type_e e_rc_type = get_rc_type(ps_cbr_buffer);
2907
0
    if(e_rc_type == VBR_STREAMING)
2908
0
    {
2909
0
        if(((float)i4_lap_window_comp / 128) > ps_bit_allocation->f_min_complexity_cross_peak_rate)
2910
0
            i4_lap_window_comp_temp =
2911
0
                (WORD32)(ps_bit_allocation->f_min_complexity_cross_peak_rate * 128);
2912
2913
        /*Get excess bits if any from vbv buffer*/
2914
0
        vbv_buffer_based_excess = get_vbv_buffer_based_excess(
2915
0
            ps_cbr_buffer,
2916
0
            ps_bit_allocation->f_min_complexity_cross_peak_rate,
2917
0
            ((float)i4_lap_window_comp / 128),
2918
0
            i4_num_frames,
2919
0
            1);
2920
0
    }
2921
0
    i4_gop_correction =
2922
0
        get_error_bits_for_desired_buf(ps_cbr_buffer, i4_lap_window_comp_temp, i4_num_frames);
2923
2924
0
    update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handle, 0);
2925
2926
0
    set_rbip(&ps_bit_allocation->s_rbip, (i4_gop_correction + (WORD32)vbv_buffer_based_excess));
2927
2928
0
    update_rbip(&ps_bit_allocation->s_rbip, ps_pic_handle, 0);
2929
0
}
2930
2931
/*****************************************************************************
2932
  Function Name : bit_alloc_set_curr_i_to_sum_i
2933
  Description   :
2934
  Inputs        : ps_bit_allocation
2935
2936
  Revision History:
2937
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2938
*****************************************************************************/
2939
void bit_alloc_set_curr_i_to_sum_i(bit_allocation_t *ps_bit_allocation, float f_curr_i_to_sum)
2940
0
{
2941
0
    ps_bit_allocation->f_curr_i_to_sum = f_curr_i_to_sum;
2942
0
}
2943
2944
/*****************************************************************************
2945
  Function Name : ba_set_gop_stat_in_bit_alloc
2946
  Description   :
2947
  Inputs        : ps_bit_allocation
2948
2949
  Revision History:
2950
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2951
*****************************************************************************/
2952
void ba_set_gop_stat_in_bit_alloc(bit_allocation_t *ps_bit_allocation, void *pv_gop_stat_summary)
2953
0
{
2954
0
    ps_bit_allocation->pv_gop_stat = pv_gop_stat_summary;
2955
0
}
2956
/*****************************************************************************
2957
  Function Name : ba_get_luma_pels
2958
  Description   :
2959
  Inputs        : ps_bit_allocation
2960
2961
  Revision History:
2962
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2963
*****************************************************************************/
2964
WORD32 ba_get_luma_pels(bit_allocation_t *ps_bit_allocation)
2965
0
{
2966
0
    return (ps_bit_allocation->i4_luma_pels);
2967
0
}
2968
/*****************************************************************************
2969
  Function Name : overflow_avoided_summation
2970
  Description   :
2971
  Inputs        : ps_bit_allocation
2972
2973
  Revision History:
2974
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2975
*****************************************************************************/
2976
void overflow_avoided_summation(WORD32 *pi4_accumulator, WORD32 i4_input)
2977
459k
{
2978
459k
    if((pi4_accumulator[0] > 0) && (((int)0x7fffffff - pi4_accumulator[0]) < i4_input))
2979
0
        pi4_accumulator[0] = 0x7fffffff;
2980
459k
    else if((pi4_accumulator[0] < 0) && (((int)0x80000000 - pi4_accumulator[0]) > i4_input))
2981
0
        pi4_accumulator[0] = 0x80000000;
2982
459k
    else
2983
459k
        pi4_accumulator[0] += i4_input;
2984
459k
}
2985
/*****************************************************************************
2986
  Function Name : ba_get_sum_complexity_segment_cross_peak
2987
  Description   :
2988
  Inputs        : ps_bit_allocation
2989
2990
  Revision History:
2991
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
2992
*****************************************************************************/
2993
float ba_get_sum_complexity_segment_cross_peak(bit_allocation_t *ps_bit_allocation)
2994
0
{
2995
0
    return (ps_bit_allocation->f_sum_complexity_segment_cross_peak);
2996
0
}
2997
/*****************************************************************************
2998
  Function Name : ba_get_prev_frame_tot_est_bits
2999
  Description   :
3000
  Inputs        : ps_bit_allocation
3001
3002
  Revision History:
3003
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3004
*****************************************************************************/
3005
WORD32 ba_get_prev_frame_tot_est_bits(bit_allocation_t *ps_bit_allocation, WORD32 i4_pic)
3006
0
{
3007
0
    return (ps_bit_allocation->ai4_prev_frm_tot_est_bits[i4_pic]);
3008
0
}
3009
/*****************************************************************************
3010
  Function Name : ba_get_prev_frame_tot_bits
3011
  Description   :
3012
  Inputs        : ps_bit_allocation
3013
3014
  Revision History:
3015
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3016
*****************************************************************************/
3017
WORD32 ba_get_prev_frame_tot_bits(bit_allocation_t *ps_bit_allocation, WORD32 i4_pic)
3018
0
{
3019
0
    return (ps_bit_allocation->ai4_prev_frm_tot_bits[i4_pic]);
3020
0
}
3021
/*****************************************************************************
3022
  Function Name : ba_gop_info_average_qscale_gop_without_offset
3023
  Description   :
3024
  Inputs        : ps_bit_allocation
3025
3026
  Revision History:
3027
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3028
*****************************************************************************/
3029
float ba_gop_info_average_qscale_gop_without_offset(bit_allocation_t *ps_bit_allocation)
3030
0
{
3031
0
    gop_level_stat_t *ps_gop_level_stat =
3032
0
        (gop_level_stat_t *)ps_bit_allocation->pv_gop_stat + ps_bit_allocation->i8_cur_gop_num;
3033
3034
0
    return (ps_gop_level_stat->f_hbd_avg_q_scale_gop_without_offset);
3035
0
}
3036
/*****************************************************************************
3037
  Function Name : ba_get_min_complexity_for_peak_br
3038
  Description   : compute min complexity above which peak rate needs to be given
3039
  Inputs        : i4_peak_bit_rate
3040
3041
  Revision History:
3042
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3043
*****************************************************************************/
3044
float ba_get_min_complexity_for_peak_br(
3045
    WORD32 i4_peak_bit_rate,
3046
    WORD32 i4_bit_rate,
3047
    float f_peak_rate_factor,
3048
    float f_max_val,
3049
    float f_min_val,
3050
    WORD32 i4_pass)
3051
4.39k
{
3052
4.39k
    float f_target_bits_ratio = (float)i4_peak_bit_rate / i4_bit_rate;
3053
4.39k
    float f_at_min_val;
3054
4.39k
    float f_at_max_val;
3055
4.39k
    float f_avg_val, f_at_avg_val;
3056
4.39k
    WORD32 i4_iter = 0, i4_max_iter = 25;
3057
3058
4.39k
    f_avg_val = (f_max_val + f_min_val) / 2;
3059
    /*i4_target_bits_ratio = (-1.7561*(X*X*X*X) + ( 2.5547 * X * X * X) - 0.3408 * (X * X) + (0.5343 * X) - 0.003) * 10;*/
3060
4.39k
    if(i4_pass != 2)
3061
4.39k
    {
3062
4.39k
        f_at_min_val = COMP_TO_BITS_MAP(f_min_val, f_peak_rate_factor);
3063
4.39k
        f_at_max_val = COMP_TO_BITS_MAP(f_max_val, f_peak_rate_factor);
3064
4.39k
        f_at_avg_val = COMP_TO_BITS_MAP(f_avg_val, f_peak_rate_factor);
3065
4.39k
    }
3066
0
    else
3067
0
    {
3068
0
        f_at_min_val = COMP_TO_BITS_MAP_2_PASS(f_min_val, f_peak_rate_factor);
3069
0
        f_at_max_val = COMP_TO_BITS_MAP_2_PASS(f_max_val, f_peak_rate_factor);
3070
0
        f_at_avg_val = COMP_TO_BITS_MAP_2_PASS(f_avg_val, f_peak_rate_factor);
3071
0
    }
3072
3073
4.39k
    do
3074
61.2k
    {
3075
61.2k
        if((f_at_min_val < f_target_bits_ratio) && (f_target_bits_ratio < f_at_avg_val))
3076
38.0k
        {
3077
38.0k
            f_max_val = f_avg_val;
3078
38.0k
        }
3079
23.1k
        else
3080
23.1k
        {
3081
23.1k
            f_min_val = f_avg_val;
3082
23.1k
        }
3083
61.2k
        f_avg_val = (f_max_val + f_min_val) / 2;
3084
3085
        /*i4_target_bits_ratio = (-1.7561*(X*X*X*X) + ( 2.5547 * X * X * X) - 0.3408 * (X * X) + (0.5343 * X) - 0.003) * 10;*/
3086
61.2k
        if(i4_pass != 2)
3087
61.2k
        {
3088
61.2k
            f_at_min_val = COMP_TO_BITS_MAP(f_min_val, f_peak_rate_factor);
3089
61.2k
            f_at_max_val = COMP_TO_BITS_MAP(f_max_val, f_peak_rate_factor);
3090
61.2k
            f_at_avg_val = COMP_TO_BITS_MAP(f_avg_val, f_peak_rate_factor);
3091
61.2k
        }
3092
0
        else
3093
0
        {
3094
0
            f_at_min_val = COMP_TO_BITS_MAP_2_PASS(f_min_val, f_peak_rate_factor);
3095
0
            f_at_max_val = COMP_TO_BITS_MAP_2_PASS(f_max_val, f_peak_rate_factor);
3096
0
            f_at_avg_val = COMP_TO_BITS_MAP_2_PASS(f_avg_val, f_peak_rate_factor);
3097
0
        }
3098
3099
61.2k
        if(((fabs((float)(f_at_avg_val - f_target_bits_ratio))) <= .0001f) ||
3100
61.2k
           (i4_iter >= i4_max_iter))
3101
4.39k
        {
3102
4.39k
            break;
3103
4.39k
        }
3104
56.8k
        i4_iter++;
3105
56.8k
    } while(1);
3106
3107
    /*f_min_complexity_across_which pk br is given is unmapped value for 1 pass and mapped value for 2 pass*/
3108
4.39k
    if(i4_pass != 2)
3109
4.39k
        return (f_avg_val);
3110
0
    else
3111
0
        return (f_at_avg_val);
3112
4.39k
}
3113
/*****************************************************************************
3114
  Function Name : get_f_curr_by_sum_subgop
3115
  Description   :
3116
  Inputs        : ps_bit_allocation
3117
3118
  Revision History:
3119
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3120
*****************************************************************************/
3121
float get_f_curr_by_sum_subgop(bit_allocation_t *ps_bit_allocation)
3122
0
{
3123
0
    return (ps_bit_allocation->f_curr_by_sum_subgop);
3124
0
}
3125
/*****************************************************************************
3126
  Function Name : ba_get_frame_number_in_gop
3127
  Description   :
3128
  Inputs        : ps_bit_allocation
3129
3130
  Revision History:
3131
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3132
*****************************************************************************/
3133
WORD32 ba_get_frame_number_in_gop(bit_allocation_t *ps_bit_allocation)
3134
0
{
3135
0
    return ((WORD32)(ps_bit_allocation->i8_frm_num_in_gop));
3136
0
}
3137
/*****************************************************************************
3138
  Function Name : ba_get_qscale_max_clip_in_second_pass
3139
  Description   :
3140
  Inputs        : ps_bit_allocation
3141
3142
  Revision History:
3143
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3144
*****************************************************************************/
3145
float ba_get_qscale_max_clip_in_second_pass(bit_allocation_t *ps_bit_allocation)
3146
0
{
3147
0
    return (ps_bit_allocation->f_qscale_max_clip_in_second_pass);
3148
0
}
3149
/*****************************************************************************
3150
  Function Name : ba_set_avg_qscale_first_pass
3151
  Description   :
3152
  Inputs        : ps_bit_allocation
3153
3154
  Revision History:
3155
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3156
*****************************************************************************/
3157
void ba_set_avg_qscale_first_pass(
3158
    bit_allocation_t *ps_bit_allocation, float f_average_qscale_1st_pass)
3159
0
{
3160
0
    ps_bit_allocation->f_average_qscale_1st_pass = f_average_qscale_1st_pass;
3161
0
}
3162
/*****************************************************************************
3163
  Function Name : ba_set_max_avg_qscale_first_pass
3164
  Description   :
3165
  Inputs        : ps_bit_allocation
3166
3167
  Revision History:
3168
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3169
*****************************************************************************/
3170
void ba_set_max_avg_qscale_first_pass(
3171
    bit_allocation_t *ps_bit_allocation, float f_average_qscale_1st_pass)
3172
0
{
3173
0
    ps_bit_allocation->f_max_average_qscale_1st_pass = f_average_qscale_1st_pass;
3174
0
}
3175
/*****************************************************************************
3176
  Function Name : ba_get_avg_qscale_first_pass
3177
  Description   :
3178
  Inputs        : ps_bit_allocation
3179
3180
  Revision History:
3181
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3182
*****************************************************************************/
3183
float ba_get_avg_qscale_first_pass(bit_allocation_t *ps_bit_allocation)
3184
0
{
3185
0
    return (ps_bit_allocation->f_average_qscale_1st_pass);
3186
0
}
3187
/*****************************************************************************
3188
  Function Name : ba_get_max_avg_qscale_first_pass
3189
  Description   :
3190
  Inputs        : ps_bit_allocation
3191
3192
  Revision History:
3193
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3194
*****************************************************************************/
3195
float ba_get_max_avg_qscale_first_pass(bit_allocation_t *ps_bit_allocation)
3196
0
{
3197
0
    return (ps_bit_allocation->f_max_average_qscale_1st_pass);
3198
0
}
3199
/*****************************************************************************
3200
  Function Name : bit_alloc_set_2pass_total_frames
3201
  Description   :
3202
  Inputs        : ps_bit_allocation
3203
3204
  Revision History:
3205
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3206
*****************************************************************************/
3207
void bit_alloc_set_2pass_total_frames(
3208
    bit_allocation_t *ps_bit_allocation, WORD32 i4_total_2pass_frames)
3209
0
{
3210
0
    ps_bit_allocation->i4_total_2pass_frames = i4_total_2pass_frames;
3211
0
}
3212
/*****************************************************************************
3213
  Function Name : ba_get_2pass_total_frames
3214
  Description   :
3215
  Inputs        : ps_bit_allocation
3216
3217
  Revision History:
3218
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3219
*****************************************************************************/
3220
WORD32 ba_get_2pass_total_frames(bit_allocation_t *ps_bit_allocation)
3221
0
{
3222
0
    return (ps_bit_allocation->i4_total_2pass_frames);
3223
0
}
3224
/*****************************************************************************
3225
  Function Name : ba_set_enable_look_ahead
3226
  Description   :
3227
  Inputs        : ps_bit_allocation
3228
3229
  Revision History:
3230
         DD MM YYYY   Author(s)       Changes (Describe the changes made)
3231
*****************************************************************************/
3232
void ba_set_enable_look_ahead(bit_allocation_t *ps_bit_allocation, WORD32 i4_fp_bit_alloc_in_sp)
3233
0
{
3234
0
    ps_bit_allocation->i4_fp_bit_alloc_in_sp = i4_fp_bit_alloc_in_sp;
3235
0
}