Coverage Report

Created: 2026-01-17 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavc/encoder/irc_cbr_buffer_control.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 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 Includes                                                             */
23
/*****************************************************************************/
24
25
/* System include files */
26
#include <stdio.h>
27
28
/* User include files */
29
#include "irc_datatypes.h"
30
#include "irc_cntrl_param.h"
31
#include "irc_common.h"
32
#include "irc_mem_req_and_acq.h"
33
#include "irc_fixed_point_error_bits.h"
34
#include "irc_cbr_buffer_control.h"
35
#include "irc_trace_support.h"
36
37
typedef struct cbr_buffer_t
38
{
39
    /* Buffer size = Delay * Bitrate*/
40
    WORD32 i4_buffer_size;
41
42
    /* Constant drain rate */
43
    WORD32 i4_drain_bits_per_frame[MAX_NUM_DRAIN_RATES];
44
45
    /* Encoder Buffer Fullness */
46
    WORD32 i4_ebf;
47
48
    /* Upper threshold of the Buffer */
49
    WORD32 i4_upr_thr[MAX_PIC_TYPE];
50
51
    /* Lower threshold of the Buffer */
52
    WORD32 i4_low_thr[MAX_PIC_TYPE];
53
54
    /* Stuffing threshold equal to error bits per second in the drain bits
55
     * fixed point computation */
56
    WORD32 i4_stuffing_threshold;
57
58
    /* For error due to bits per frame calculation */
59
    error_bits_handle aps_bpf_error_bits[MAX_NUM_DRAIN_RATES];
60
61
    /* Whether the buffer model is used for CBR or VBR streaming */
62
    WORD32 i4_is_cbr_mode;
63
64
    /* Input parameters stored for initialization */
65
    WORD32 ai4_bit_rate[MAX_NUM_DRAIN_RATES];
66
67
    WORD32 i4_max_delay;
68
69
    WORD32 ai4_num_pics_in_delay_period[MAX_PIC_TYPE];
70
71
    WORD32 i4_tgt_frm_rate;
72
73
    UWORD32 u4_max_vbv_buf_size;
74
75
} cbr_buffer_t;
76
77
WORD32 irc_cbr_buffer_num_fill_use_free_memtab(cbr_buffer_t **pps_cbr_buffer,
78
                                               itt_memtab_t *ps_memtab,
79
                                               ITT_FUNC_TYPE_E e_func_type)
80
120k
{
81
120k
    WORD32 i4_mem_tab_idx = 0, i;
82
120k
    cbr_buffer_t s_cbr_buffer_temp;
83
84
    /*
85
     * Hack for all alloc, during which we don't have any state memory.
86
     * Dereferencing can cause issues
87
     */
88
120k
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
89
100k
        (*pps_cbr_buffer) = &s_cbr_buffer_temp;
90
91
120k
    if(e_func_type != GET_NUM_MEMTAB)
92
60.3k
    {
93
60.3k
        fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(cbr_buffer_t),
94
60.3k
                    ALIGN_128_BYTE, PERSISTENT, DDR);
95
60.3k
        use_or_fill_base(&ps_memtab[0], (void**)pps_cbr_buffer, e_func_type);
96
60.3k
    }
97
120k
    i4_mem_tab_idx++;
98
99
362k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
100
241k
    {
101
241k
        i4_mem_tab_idx += irc_error_bits_num_fill_use_free_memtab(
102
241k
                        &pps_cbr_buffer[0]->aps_bpf_error_bits[i],
103
241k
                        &ps_memtab[i4_mem_tab_idx], e_func_type);
104
241k
    }
105
120k
    return (i4_mem_tab_idx);
106
120k
}
107
108
/******************************************************************************
109
 * @brief Initialize the CBR VBV buffer state.
110
 * This could however be used for VBR streaming VBV also
111
 *
112
 ******************************************************************************/
113
void irc_init_cbr_buffer(cbr_buffer_t *ps_cbr_buffer,
114
                         WORD32 i4_buffer_delay,
115
                         WORD32 i4_tgt_frm_rate,
116
                         WORD32 *i4_bit_rate,
117
                         UWORD32 *u4_num_pics_in_delay_prd,
118
                         UWORD32 u4_vbv_buf_size)
119
25.3k
{
120
25.3k
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
121
25.3k
    int i;
122
123
76.0k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
124
50.7k
    {
125
50.7k
        X_PROD_Y_DIV_Z(i4_bit_rate[i], 1000, i4_tgt_frm_rate,
126
50.7k
                       i4_bits_per_frm[i]);
127
        /* Drain rate = bitrate/(framerate/1000) */
128
50.7k
        ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
129
        /* Initialize the bits per frame error bits calculation */
130
50.7k
        irc_init_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
131
50.7k
                            i4_tgt_frm_rate, i4_bit_rate[i]);
132
50.7k
    }
133
134
    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
135
    /* This would mean CBR mode */
136
25.3k
    if(i4_bit_rate[0] == i4_bit_rate[1])
137
25.3k
    {
138
25.3k
        X_PROD_Y_DIV_Z(i4_bit_rate[0], i4_buffer_delay, 1000,
139
25.3k
                       ps_cbr_buffer->i4_buffer_size);
140
25.3k
        ps_cbr_buffer->i4_is_cbr_mode = 1;
141
25.3k
    }
142
0
    else
143
0
    {
144
        /* VBR streaming case which has different drain rates for I and P */
145
0
        ps_cbr_buffer->i4_buffer_size = u4_num_pics_in_delay_prd[0]
146
0
                                        * ps_cbr_buffer->i4_drain_bits_per_frame[0]
147
0
                                        + u4_num_pics_in_delay_prd[1]
148
0
                                        * ps_cbr_buffer->i4_drain_bits_per_frame[1];
149
150
0
        ps_cbr_buffer->i4_is_cbr_mode = 0;
151
0
    }
152
153
25.3k
    if(ps_cbr_buffer->i4_buffer_size > (WORD32)u4_vbv_buf_size)
154
2.64k
    {
155
2.64k
        ps_cbr_buffer->i4_buffer_size = u4_vbv_buf_size;
156
2.64k
    }
157
158
    /* Initially Encoder buffer fullness is zero */
159
25.3k
    ps_cbr_buffer->i4_ebf = 0;
160
161
    /* tgt_frame_rate is divided by 1000 because, an approximate value is fine
162
     * as this is just a threshold below which stuffing is done to avoid buffer
163
     * underflow due to fixed point error in drain rate
164
     */
165
25.3k
    ps_cbr_buffer->i4_stuffing_threshold = (i4_bit_rate[0]
166
25.3k
                    - (i4_bits_per_frm[0] * (i4_tgt_frm_rate / 1000)));
167
168
101k
    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
169
76.0k
    {
170
        /*
171
         * Upper threshold for
172
         * I frame = 1 * bits per frame
173
         * P Frame = 4 * bits per frame.
174
         * The threshold for I frame is only 1 * bits per frame as the threshold
175
         * should only account for error in estimated bits.
176
         * In P frame it should account for difference bets bits consumed by
177
         * I(Scene change) and P frame I to P complexity is assumed to be 5.
178
         */
179
76.0k
        WORD32 i4_index;
180
76.0k
        i4_index = i4_i > 0 ? 1 : 0;
181
76.0k
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
182
76.0k
                        - (ps_cbr_buffer->i4_buffer_size >> 3);
183
184
        /*
185
         * For both I and P frame Lower threshold is equal to drain rate.Even if
186
         * the encoder consumes zero bits it should have enough bits to drain
187
         */
188
76.0k
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
189
76.0k
    }
190
191
    /* Storing the input parameters for using it for change functions */
192
76.0k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
193
50.7k
    {
194
50.7k
        ps_cbr_buffer->ai4_bit_rate[i] = i4_bit_rate[i];
195
50.7k
    }
196
197
101k
    for(i = 0; i < MAX_PIC_TYPE; i++)
198
76.0k
    {
199
76.0k
        ps_cbr_buffer->ai4_num_pics_in_delay_period[i] =
200
76.0k
                        u4_num_pics_in_delay_prd[i];
201
76.0k
    }
202
25.3k
    ps_cbr_buffer->i4_tgt_frm_rate = i4_tgt_frm_rate;
203
25.3k
    ps_cbr_buffer->i4_max_delay = i4_buffer_delay;
204
25.3k
    ps_cbr_buffer->u4_max_vbv_buf_size = u4_vbv_buf_size;
205
25.3k
}
206
207
/******************************************************************************
208
 * @brief Condition check for constraining the number of bits allocated based on
209
 * bufer size
210
 ******************************************************************************/
211
WORD32 irc_cbr_buffer_constraint_check(cbr_buffer_t *ps_cbr_buffer,
212
                                       WORD32 i4_tgt_bits,
213
                                       picture_type_e e_pic_type)
214
18.4k
{
215
18.4k
    WORD32 i4_max_tgt_bits, i4_min_tgt_bits;
216
18.4k
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
217
6.84k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
218
18.4k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];
219
220
    /* Max tgt bits = Upper threshold - current encoder buffer fullness */
221
18.4k
    i4_max_tgt_bits = ps_cbr_buffer->i4_upr_thr[e_pic_type]
222
18.4k
                    - ps_cbr_buffer->i4_ebf;
223
    /* Max tgt bits cannot be negative */
224
18.4k
    if(i4_max_tgt_bits < 0)
225
2.23k
        i4_max_tgt_bits = 0;
226
227
    /*
228
     * Min tgt bits , least number of bits in the Encoder after
229
     * draining such that it is greater than lower threshold
230
     */
231
18.4k
    i4_min_tgt_bits = ps_cbr_buffer->i4_low_thr[e_pic_type]
232
18.4k
                    - (ps_cbr_buffer->i4_ebf - i4_drain_bits_per_frame);
233
    /* Min tgt bits cannot be negative */
234
18.4k
    if(i4_min_tgt_bits < 0)
235
5.85k
        i4_min_tgt_bits = 0;
236
237
    /* Current tgt bits should be between max and min tgt bits */
238
18.4k
    CLIP(i4_tgt_bits, i4_max_tgt_bits, i4_min_tgt_bits);
239
18.4k
    return i4_tgt_bits;
240
18.4k
}
241
242
/* *****************************************************************************
243
 * @brief constaints the bit allocation based on buffer size
244
 *
245
 ******************************************************************************/
246
WORD32 irc_vbr_stream_buffer_constraint_check(cbr_buffer_t *ps_cbr_buffer,
247
                                              WORD32 i4_tgt_bits,
248
                                              picture_type_e e_pic_type)
249
0
{
250
0
    WORD32 i4_max_tgt_bits;
251
252
    /* Max tgt bits = Upper threshold - current encoder buffer fullness */
253
0
    i4_max_tgt_bits = ps_cbr_buffer->i4_upr_thr[e_pic_type]
254
0
                    - ps_cbr_buffer->i4_ebf;
255
256
    /* Max tgt bits cannot be negative */
257
0
    if(i4_max_tgt_bits < 0)
258
0
        i4_max_tgt_bits = 0;
259
260
0
    if(i4_tgt_bits > i4_max_tgt_bits)
261
0
        i4_tgt_bits = i4_max_tgt_bits;
262
263
0
    return i4_tgt_bits;
264
0
}
265
266
/* *****************************************************************************
267
 * @brief Verifies the buffer state and returns whether it is overflowing,
268
 * underflowing or normal
269
 *
270
 ******************************************************************************/
271
vbv_buf_status_e irc_get_cbr_buffer_status(cbr_buffer_t *ps_cbr_buffer,
272
                                           WORD32 i4_tot_consumed_bits,
273
                                           WORD32 *pi4_num_bits_to_prevent_overflow,
274
                                           picture_type_e e_pic_type)
275
4.02M
{
276
4.02M
    vbv_buf_status_e e_buf_status;
277
4.02M
    WORD32 i4_cur_enc_buf;
278
4.02M
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
279
3.77M
                            irc_get_error_bits(ps_cbr_buffer
280
3.77M
                                               ->aps_bpf_error_bits[0]) :
281
4.02M
                            irc_get_error_bits(ps_cbr_buffer
282
249k
                                               ->aps_bpf_error_bits[1]);
283
284
4.02M
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
285
3.77M
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
286
4.02M
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];
287
288
    /* Add the tot consumed bits to the Encoder Buffer*/
289
4.02M
    i4_cur_enc_buf = ps_cbr_buffer->i4_ebf + i4_tot_consumed_bits;
290
291
    /* If the Encoder exceeds the Buffer Size signal an Overflow*/
292
4.02M
    if(i4_cur_enc_buf > ps_cbr_buffer->i4_buffer_size)
293
794k
    {
294
794k
        e_buf_status = VBV_OVERFLOW;
295
794k
        i4_cur_enc_buf = ps_cbr_buffer->i4_buffer_size;
296
794k
    }
297
3.23M
    else
298
3.23M
    {
299
        /*
300
         * Subtract the constant drain bits and error bits due to fixed point
301
         * implementation
302
         */
303
3.23M
        i4_cur_enc_buf -= (i4_drain_bits_per_frame + i4_error_bits);
304
305
        /*
306
         * If the buffer is less than stuffing threshold an Underflow is
307
         * signaled else its NORMAL
308
         */
309
3.23M
        if(i4_cur_enc_buf < ps_cbr_buffer->i4_stuffing_threshold)
310
2.57M
        {
311
2.57M
            e_buf_status = VBV_UNDERFLOW;
312
2.57M
        }
313
653k
        else
314
653k
        {
315
653k
            e_buf_status = VBV_NORMAL;
316
653k
        }
317
318
3.23M
        if(i4_cur_enc_buf < 0)
319
2.57M
            i4_cur_enc_buf = 0;
320
3.23M
    }
321
322
    /*
323
     * The RC lib models the encoder buffer, but the VBV buffer characterizes
324
     * the decoder buffer
325
     */
326
4.02M
    if(e_buf_status == VBV_OVERFLOW)
327
794k
    {
328
794k
        e_buf_status = VBV_UNDERFLOW;
329
794k
    }
330
3.23M
    else if(e_buf_status == VBV_UNDERFLOW)
331
2.57M
    {
332
2.57M
        e_buf_status = VBV_OVERFLOW;
333
2.57M
    }
334
335
4.02M
    pi4_num_bits_to_prevent_overflow[0] = (ps_cbr_buffer->i4_buffer_size
336
4.02M
                    - i4_cur_enc_buf);
337
338
4.02M
    return e_buf_status;
339
4.02M
}
340
341
/*******************************************************************************
342
 * @brief Based on the bits consumed the buffer model is updated
343
 ******************************************************************************/
344
void irc_update_cbr_buffer(cbr_buffer_t *ps_cbr_buffer,
345
                           WORD32 i4_tot_consumed_bits,
346
                           picture_type_e e_pic_type)
347
41.3k
{
348
41.3k
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
349
11.9k
                           irc_get_error_bits(ps_cbr_buffer->
350
11.9k
                                             aps_bpf_error_bits[0]) :
351
41.3k
                           irc_get_error_bits( ps_cbr_buffer->
352
29.3k
                                              aps_bpf_error_bits[1]);
353
354
41.3k
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
355
11.9k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
356
41.3k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];
357
358
    /* Update the Encoder buffer with the total consumed bits*/
359
41.3k
    ps_cbr_buffer->i4_ebf += i4_tot_consumed_bits;
360
361
    /*
362
     * Subtract the drain bits and error bits due to fixed point
363
     * implementation
364
     */
365
41.3k
    ps_cbr_buffer->i4_ebf -= (i4_drain_bits_per_frame + i4_error_bits);
366
367
41.3k
    if(ps_cbr_buffer->i4_ebf < 0)
368
7.70k
        ps_cbr_buffer->i4_ebf = 0;
369
370
    /*SS - Fix for lack of stuffing*/
371
41.3k
    if(ps_cbr_buffer->i4_ebf > ps_cbr_buffer->i4_buffer_size)
372
4.82k
    {
373
4.82k
        TRACE_PRINTF((const WORD8*)"Error: Should not be coming here with stuffing\n");
374
4.82k
        ps_cbr_buffer->i4_ebf = ps_cbr_buffer->i4_buffer_size;
375
4.82k
    }
376
41.3k
}
377
378
/*******************************************************************************
379
 * @brief If the buffer underflows then return the number of bits to prevent
380
 * underflow
381
 *
382
 ******************************************************************************/
383
WORD32 irc_get_cbr_bits_to_stuff(cbr_buffer_t *ps_cbr_buffer,
384
                                 WORD32 i4_tot_consumed_bits,
385
                                 picture_type_e e_pic_type)
386
4.07k
{
387
4.07k
    WORD32 i4_bits_to_stuff;
388
4.07k
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
389
2.78k
                            irc_get_error_bits(ps_cbr_buffer
390
2.78k
                                               ->aps_bpf_error_bits[0]) :
391
4.07k
                            irc_get_error_bits(ps_cbr_buffer
392
1.29k
                                               ->aps_bpf_error_bits[1]);
393
394
4.07k
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
395
2.78k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
396
4.07k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];
397
398
    /*
399
     * Stuffing bits got from the following equation
400
     * Stuffing_threshold = ebf + tcb - drain bits - error bits + stuff_bits
401
     */
402
4.07k
    i4_bits_to_stuff = i4_drain_bits_per_frame + i4_error_bits
403
4.07k
                    + ps_cbr_buffer->i4_stuffing_threshold
404
4.07k
                    - (ps_cbr_buffer->i4_ebf + i4_tot_consumed_bits);
405
406
4.07k
    return i4_bits_to_stuff;
407
4.07k
}
408
409
/*******************************************************************************
410
 * @brief Update the state for change in number of pics in the delay period
411
 *
412
 ******************************************************************************/
413
void irc_change_cbr_vbv_num_pics_in_delay_period(cbr_buffer_t *ps_cbr_buffer,
414
                                                 UWORD32 *u4_num_pics_in_delay_prd)
415
0
{
416
0
    WORD32 i;
417
418
0
    if(!ps_cbr_buffer->i4_is_cbr_mode)
419
0
    {
420
0
        ps_cbr_buffer->i4_buffer_size =
421
0
                        u4_num_pics_in_delay_prd[0]
422
0
                        * ps_cbr_buffer->i4_drain_bits_per_frame[0]
423
0
                        + u4_num_pics_in_delay_prd[1]
424
0
                        * ps_cbr_buffer->i4_drain_bits_per_frame[1];
425
426
0
        if(ps_cbr_buffer->i4_buffer_size
427
0
                        > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
428
0
        {
429
0
            ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
430
0
        }
431
0
        for(i = 0; i < MAX_PIC_TYPE; i++)
432
0
        {
433
0
            ps_cbr_buffer->i4_upr_thr[i] = ps_cbr_buffer->i4_buffer_size
434
0
                            - (ps_cbr_buffer->i4_buffer_size >> 3);
435
0
        }
436
437
        /* Re-initialize the number of pics in delay period */
438
0
        for(i = 0; i < MAX_PIC_TYPE; i++)
439
0
        {
440
0
            ps_cbr_buffer->ai4_num_pics_in_delay_period[i] =
441
0
                            u4_num_pics_in_delay_prd[i];
442
0
        }
443
0
    }
444
0
}
445
446
/******************************************************************************
447
 * @brief update the state for change in target frame rate
448
 *
449
 ******************************************************************************/
450
void irc_change_cbr_vbv_tgt_frame_rate(cbr_buffer_t *ps_cbr_buffer,
451
                                       WORD32 i4_tgt_frm_rate)
452
11.8k
{
453
11.8k
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
454
11.8k
    int i;
455
456
35.5k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
457
23.6k
    {
458
23.6k
        X_PROD_Y_DIV_Z(ps_cbr_buffer->ai4_bit_rate[i], 1000, i4_tgt_frm_rate,
459
23.6k
                       i4_bits_per_frm[i]);
460
        /* Drain rate = bitrate/(framerate/1000) */
461
23.6k
        ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
462
        /* Initialize the bits per frame error bits calculation */
463
23.6k
        irc_change_frm_rate_in_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
464
23.6k
                                          i4_tgt_frm_rate);
465
23.6k
    }
466
467
    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
468
11.8k
    if(!ps_cbr_buffer->i4_is_cbr_mode)
469
0
    {
470
        /* VBR streaming case which has different drain rates for I and P */
471
0
        ps_cbr_buffer->i4_buffer_size =
472
0
                        ps_cbr_buffer->ai4_num_pics_in_delay_period[0]
473
0
                      * ps_cbr_buffer->i4_drain_bits_per_frame[0]
474
0
                      + ps_cbr_buffer->ai4_num_pics_in_delay_period[1]
475
0
                      * ps_cbr_buffer->i4_drain_bits_per_frame[1];
476
0
    }
477
478
11.8k
    if(ps_cbr_buffer->i4_buffer_size
479
11.8k
                    > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
480
0
    {
481
0
        ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
482
0
    }
483
484
    /*
485
     * Tgt_frame_rate is divided by 1000 because an approximate value is fine as
486
     * this is just a threshold below which stuffing is done to avoid buffer
487
     * underflow due to fixed point error in drain rate
488
     */
489
11.8k
    ps_cbr_buffer->i4_stuffing_threshold = (ps_cbr_buffer->ai4_bit_rate[0]
490
11.8k
                    - (i4_bits_per_frm[0] * (i4_tgt_frm_rate / 1000)));
491
492
47.3k
    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
493
35.5k
    {
494
        /*
495
         * Upper threshold for
496
         * I frame = 1 * bits per frame
497
         * P Frame = 4 * bits per frame.
498
         * The threshold for I frame is only 1 * bits per frame as the threshold should
499
         * only account for error in estimated bits.
500
         * In P frame it should account for difference bets bits consumed by I(Scene change)
501
         * and P frame I to P complexity is assumed to be 5.
502
         */
503
35.5k
        WORD32 i4_index;
504
35.5k
        i4_index = i4_i > 0 ? 1 : 0;
505
35.5k
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
506
35.5k
                        - (ps_cbr_buffer->i4_buffer_size >> 3);
507
508
        /*
509
         * For both I and P frame Lower threshold is equal to drain rate.
510
         * Even if the encoder consumes zero bits it should have enough bits to
511
         * drain
512
         */
513
35.5k
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
514
35.5k
    }
515
516
    /* Storing the input parameters for using it for change functions */
517
11.8k
    ps_cbr_buffer->i4_tgt_frm_rate = i4_tgt_frm_rate;
518
11.8k
}
519
520
/*******************************************************************************
521
 * @brief Change the state for change in bit rate
522
 *
523
 ******************************************************************************/
524
void irc_change_cbr_vbv_bit_rate(cbr_buffer_t *ps_cbr_buffer,
525
                                 WORD32 *i4_bit_rate)
526
6.74k
{
527
6.74k
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
528
6.74k
    int i;
529
530
20.2k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
531
13.4k
    {
532
13.4k
        X_PROD_Y_DIV_Z(i4_bit_rate[i], 1000, ps_cbr_buffer->i4_tgt_frm_rate,
533
13.4k
                       i4_bits_per_frm[i]);
534
        /* Drain rate = bitrate/(framerate/1000) */
535
13.4k
        ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
536
        /* Initialize the bits per frame error bits calculation */
537
13.4k
        irc_change_bitrate_in_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
538
13.4k
                                         i4_bit_rate[i]);
539
13.4k
    }
540
541
    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
542
6.74k
    if(i4_bit_rate[0] == i4_bit_rate[1]) /* This would mean CBR mode */
543
6.74k
    {
544
6.74k
        X_PROD_Y_DIV_Z(i4_bit_rate[0], ps_cbr_buffer->i4_max_delay, 1000,
545
6.74k
                       ps_cbr_buffer->i4_buffer_size);
546
6.74k
        ps_cbr_buffer->i4_is_cbr_mode = 1;
547
6.74k
    }
548
0
    else
549
0
    {
550
        /* VBR streaming case which has different drain rates for I and P */
551
0
        ps_cbr_buffer->i4_buffer_size =
552
0
                        ps_cbr_buffer->ai4_num_pics_in_delay_period[0]
553
0
                      * ps_cbr_buffer->i4_drain_bits_per_frame[0]
554
0
                      + ps_cbr_buffer->ai4_num_pics_in_delay_period[1]
555
0
                      * ps_cbr_buffer->i4_drain_bits_per_frame[1];
556
557
0
        ps_cbr_buffer->i4_is_cbr_mode = 0;
558
0
    }
559
560
6.74k
    if(ps_cbr_buffer->i4_buffer_size
561
6.74k
                    > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
562
765
    {
563
765
        ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
564
765
    }
565
566
    /*
567
     * tgt_frame_rate is divided by 1000 because
568
     * an approximate value is fine as this is just a threshold below which
569
     * stuffing is done to avoid buffer underflow due to fixed point
570
     * error in drain rate
571
     */
572
6.74k
    ps_cbr_buffer->i4_stuffing_threshold = (i4_bit_rate[0]
573
6.74k
                    - (i4_bits_per_frm[0]
574
6.74k
                                    * (ps_cbr_buffer->i4_tgt_frm_rate / 1000)));
575
576
26.9k
    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
577
20.2k
    {
578
        /*
579
         * Upper threshold for
580
         * I frame = 1 * bits per frame
581
         * P Frame = 4 * bits per frame.
582
         * The threshold for I frame is only 1 * bits per frame as the threshold
583
         * should only account for error in estimated bits.
584
         * In P frame it should account for difference bets bits consumed by
585
         * I(Scene change) and P frame I to P complexity is assumed to be 5.
586
         */
587
588
20.2k
        WORD32 i4_index;
589
20.2k
        i4_index = i4_i > 0 ? 1 : 0;
590
20.2k
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
591
20.2k
                        - (ps_cbr_buffer->i4_buffer_size >> 3);
592
593
        /* For both I and P frame Lower threshold is equal to drain rate.
594
         * Even if the encoder consumes zero bits it should have enough bits to
595
         * drain
596
         */
597
20.2k
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
598
20.2k
    }
599
600
    /* Storing the input parameters for using it for change functions */
601
20.2k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
602
13.4k
    {
603
13.4k
        ps_cbr_buffer->ai4_bit_rate[i] = i4_bit_rate[i];
604
13.4k
    }
605
6.74k
}
606
607
void irc_change_cbr_buffer_delay(cbr_buffer_t *ps_cbr_buffer,
608
                                 WORD32 i4_buffer_delay)
609
0
{
610
0
    WORD32 i4_i;
611
612
    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
613
0
    if(ps_cbr_buffer->i4_is_cbr_mode)
614
0
    {
615
0
        X_PROD_Y_DIV_Z(ps_cbr_buffer->ai4_bit_rate[0], i4_buffer_delay, 1000,
616
0
                       ps_cbr_buffer->i4_buffer_size);
617
0
    }
618
619
0
    if(ps_cbr_buffer->i4_buffer_size
620
0
                    > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
621
0
    {
622
0
        ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
623
0
    }
624
625
0
    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
626
0
    {
627
        /*
628
         * Upper threshold for
629
         * I frame = 1 * bits per frame
630
         * P Frame = 4 * bits per frame.
631
         * The threshold for I frame is only 1 * bits per frame as the threshold
632
         * should only account for error in estimated bits.
633
         * In P frame it should account for difference bets bits consumed by I
634
         * (Scene change) and P frame I to P complexity is assumed to be 5.
635
         */
636
0
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
637
0
                        - (ps_cbr_buffer->i4_buffer_size >> 3);
638
0
    }
639
640
    /* Storing the input parameters for using it for change functions */
641
0
    ps_cbr_buffer->i4_max_delay = i4_buffer_delay;
642
0
}
643
644
WORD32 irc_get_cbr_buffer_delay(cbr_buffer_t *ps_cbr_buffer)
645
18.9k
{
646
18.9k
    return (ps_cbr_buffer->i4_max_delay);
647
18.9k
}
648
649
WORD32 irc_get_cbr_buffer_size(cbr_buffer_t *ps_cbr_buffer)
650
76.8k
{
651
76.8k
    return (ps_cbr_buffer->i4_buffer_size);
652
76.8k
}