Coverage Report

Created: 2026-03-31 06:08

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
106k
{
81
106k
    WORD32 i4_mem_tab_idx = 0, i;
82
106k
    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
106k
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
89
88.8k
        (*pps_cbr_buffer) = &s_cbr_buffer_temp;
90
91
106k
    if(e_func_type != GET_NUM_MEMTAB)
92
53.3k
    {
93
53.3k
        fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(cbr_buffer_t),
94
53.3k
                    ALIGN_128_BYTE, PERSISTENT, DDR);
95
53.3k
        use_or_fill_base(&ps_memtab[0], (void**)pps_cbr_buffer, e_func_type);
96
53.3k
    }
97
106k
    i4_mem_tab_idx++;
98
99
319k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
100
213k
    {
101
213k
        i4_mem_tab_idx += irc_error_bits_num_fill_use_free_memtab(
102
213k
                        &pps_cbr_buffer[0]->aps_bpf_error_bits[i],
103
213k
                        &ps_memtab[i4_mem_tab_idx], e_func_type);
104
213k
    }
105
106k
    return (i4_mem_tab_idx);
106
106k
}
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
20.9k
{
120
20.9k
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
121
20.9k
    int i;
122
123
62.8k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
124
41.9k
    {
125
41.9k
        X_PROD_Y_DIV_Z(i4_bit_rate[i], 1000, i4_tgt_frm_rate,
126
41.9k
                       i4_bits_per_frm[i]);
127
        /* Drain rate = bitrate/(framerate/1000) */
128
41.9k
        ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
129
        /* Initialize the bits per frame error bits calculation */
130
41.9k
        irc_init_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
131
41.9k
                            i4_tgt_frm_rate, i4_bit_rate[i]);
132
41.9k
    }
133
134
    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
135
    /* This would mean CBR mode */
136
20.9k
    if(i4_bit_rate[0] == i4_bit_rate[1])
137
20.9k
    {
138
20.9k
        X_PROD_Y_DIV_Z(i4_bit_rate[0], i4_buffer_delay, 1000,
139
20.9k
                       ps_cbr_buffer->i4_buffer_size);
140
20.9k
        ps_cbr_buffer->i4_is_cbr_mode = 1;
141
20.9k
    }
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
20.9k
    if(ps_cbr_buffer->i4_buffer_size > (WORD32)u4_vbv_buf_size)
154
2.12k
    {
155
2.12k
        ps_cbr_buffer->i4_buffer_size = u4_vbv_buf_size;
156
2.12k
    }
157
158
    /* Initially Encoder buffer fullness is zero */
159
20.9k
    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
20.9k
    ps_cbr_buffer->i4_stuffing_threshold = (i4_bit_rate[0]
166
20.9k
                    - (i4_bits_per_frm[0] * (i4_tgt_frm_rate / 1000)));
167
168
83.8k
    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
169
62.8k
    {
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
62.8k
        WORD32 i4_index;
180
62.8k
        i4_index = i4_i > 0 ? 1 : 0;
181
62.8k
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
182
62.8k
                        - (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
62.8k
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
189
62.8k
    }
190
191
    /* Storing the input parameters for using it for change functions */
192
62.8k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
193
41.9k
    {
194
41.9k
        ps_cbr_buffer->ai4_bit_rate[i] = i4_bit_rate[i];
195
41.9k
    }
196
197
83.8k
    for(i = 0; i < MAX_PIC_TYPE; i++)
198
62.8k
    {
199
62.8k
        ps_cbr_buffer->ai4_num_pics_in_delay_period[i] =
200
62.8k
                        u4_num_pics_in_delay_prd[i];
201
62.8k
    }
202
20.9k
    ps_cbr_buffer->i4_tgt_frm_rate = i4_tgt_frm_rate;
203
20.9k
    ps_cbr_buffer->i4_max_delay = i4_buffer_delay;
204
20.9k
    ps_cbr_buffer->u4_max_vbv_buf_size = u4_vbv_buf_size;
205
20.9k
}
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
17.3k
{
215
17.3k
    WORD32 i4_max_tgt_bits, i4_min_tgt_bits;
216
17.3k
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
217
6.31k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
218
17.3k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];
219
220
    /* Max tgt bits = Upper threshold - current encoder buffer fullness */
221
17.3k
    i4_max_tgt_bits = ps_cbr_buffer->i4_upr_thr[e_pic_type]
222
17.3k
                    - ps_cbr_buffer->i4_ebf;
223
    /* Max tgt bits cannot be negative */
224
17.3k
    if(i4_max_tgt_bits < 0)
225
2.13k
        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
17.3k
    i4_min_tgt_bits = ps_cbr_buffer->i4_low_thr[e_pic_type]
232
17.3k
                    - (ps_cbr_buffer->i4_ebf - i4_drain_bits_per_frame);
233
    /* Min tgt bits cannot be negative */
234
17.3k
    if(i4_min_tgt_bits < 0)
235
5.01k
        i4_min_tgt_bits = 0;
236
237
    /* Current tgt bits should be between max and min tgt bits */
238
17.3k
    CLIP(i4_tgt_bits, i4_max_tgt_bits, i4_min_tgt_bits);
239
17.3k
    return i4_tgt_bits;
240
17.3k
}
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
3.30M
{
276
3.30M
    vbv_buf_status_e e_buf_status;
277
3.30M
    WORD32 i4_cur_enc_buf;
278
3.30M
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
279
3.10M
                            irc_get_error_bits(ps_cbr_buffer
280
3.10M
                                               ->aps_bpf_error_bits[0]) :
281
3.30M
                            irc_get_error_bits(ps_cbr_buffer
282
198k
                                               ->aps_bpf_error_bits[1]);
283
284
3.30M
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
285
3.10M
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
286
3.30M
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];
287
288
    /* Add the tot consumed bits to the Encoder Buffer*/
289
3.30M
    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
3.30M
    if(i4_cur_enc_buf > ps_cbr_buffer->i4_buffer_size)
293
663k
    {
294
663k
        e_buf_status = VBV_OVERFLOW;
295
663k
        i4_cur_enc_buf = ps_cbr_buffer->i4_buffer_size;
296
663k
    }
297
2.63M
    else
298
2.63M
    {
299
        /*
300
         * Subtract the constant drain bits and error bits due to fixed point
301
         * implementation
302
         */
303
2.63M
        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
2.63M
        if(i4_cur_enc_buf < ps_cbr_buffer->i4_stuffing_threshold)
310
2.07M
        {
311
2.07M
            e_buf_status = VBV_UNDERFLOW;
312
2.07M
        }
313
565k
        else
314
565k
        {
315
565k
            e_buf_status = VBV_NORMAL;
316
565k
        }
317
318
2.63M
        if(i4_cur_enc_buf < 0)
319
2.07M
            i4_cur_enc_buf = 0;
320
2.63M
    }
321
322
    /*
323
     * The RC lib models the encoder buffer, but the VBV buffer characterizes
324
     * the decoder buffer
325
     */
326
3.30M
    if(e_buf_status == VBV_OVERFLOW)
327
663k
    {
328
663k
        e_buf_status = VBV_UNDERFLOW;
329
663k
    }
330
2.63M
    else if(e_buf_status == VBV_UNDERFLOW)
331
2.07M
    {
332
2.07M
        e_buf_status = VBV_OVERFLOW;
333
2.07M
    }
334
335
3.30M
    pi4_num_bits_to_prevent_overflow[0] = (ps_cbr_buffer->i4_buffer_size
336
3.30M
                    - i4_cur_enc_buf);
337
338
3.30M
    return e_buf_status;
339
3.30M
}
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
37.1k
{
348
37.1k
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
349
11.2k
                           irc_get_error_bits(ps_cbr_buffer->
350
11.2k
                                             aps_bpf_error_bits[0]) :
351
37.1k
                           irc_get_error_bits( ps_cbr_buffer->
352
25.9k
                                              aps_bpf_error_bits[1]);
353
354
37.1k
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
355
11.2k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
356
37.1k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];
357
358
    /* Update the Encoder buffer with the total consumed bits*/
359
37.1k
    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
37.1k
    ps_cbr_buffer->i4_ebf -= (i4_drain_bits_per_frame + i4_error_bits);
366
367
37.1k
    if(ps_cbr_buffer->i4_ebf < 0)
368
8.59k
        ps_cbr_buffer->i4_ebf = 0;
369
370
    /*SS - Fix for lack of stuffing*/
371
37.1k
    if(ps_cbr_buffer->i4_ebf > ps_cbr_buffer->i4_buffer_size)
372
4.65k
    {
373
4.65k
        TRACE_PRINTF((const WORD8*)"Error: Should not be coming here with stuffing\n");
374
4.65k
        ps_cbr_buffer->i4_ebf = ps_cbr_buffer->i4_buffer_size;
375
4.65k
    }
376
37.1k
}
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
3.42k
{
387
3.42k
    WORD32 i4_bits_to_stuff;
388
3.42k
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
389
2.31k
                            irc_get_error_bits(ps_cbr_buffer
390
2.31k
                                               ->aps_bpf_error_bits[0]) :
391
3.42k
                            irc_get_error_bits(ps_cbr_buffer
392
1.11k
                                               ->aps_bpf_error_bits[1]);
393
394
3.42k
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
395
2.31k
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
396
3.42k
                                     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
3.42k
    i4_bits_to_stuff = i4_drain_bits_per_frame + i4_error_bits
403
3.42k
                    + ps_cbr_buffer->i4_stuffing_threshold
404
3.42k
                    - (ps_cbr_buffer->i4_ebf + i4_tot_consumed_bits);
405
406
3.42k
    return i4_bits_to_stuff;
407
3.42k
}
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
9.80k
{
453
9.80k
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
454
9.80k
    int i;
455
456
29.4k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
457
19.6k
    {
458
19.6k
        X_PROD_Y_DIV_Z(ps_cbr_buffer->ai4_bit_rate[i], 1000, i4_tgt_frm_rate,
459
19.6k
                       i4_bits_per_frm[i]);
460
        /* Drain rate = bitrate/(framerate/1000) */
461
19.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
19.6k
        irc_change_frm_rate_in_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
464
19.6k
                                          i4_tgt_frm_rate);
465
19.6k
    }
466
467
    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
468
9.80k
    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
9.80k
    if(ps_cbr_buffer->i4_buffer_size
479
9.80k
                    > (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
9.80k
    ps_cbr_buffer->i4_stuffing_threshold = (ps_cbr_buffer->ai4_bit_rate[0]
490
9.80k
                    - (i4_bits_per_frm[0] * (i4_tgt_frm_rate / 1000)));
491
492
39.2k
    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
493
29.4k
    {
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
29.4k
        WORD32 i4_index;
504
29.4k
        i4_index = i4_i > 0 ? 1 : 0;
505
29.4k
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
506
29.4k
                        - (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
29.4k
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
514
29.4k
    }
515
516
    /* Storing the input parameters for using it for change functions */
517
9.80k
    ps_cbr_buffer->i4_tgt_frm_rate = i4_tgt_frm_rate;
518
9.80k
}
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
5.62k
{
527
5.62k
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
528
5.62k
    int i;
529
530
16.8k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
531
11.2k
    {
532
11.2k
        X_PROD_Y_DIV_Z(i4_bit_rate[i], 1000, ps_cbr_buffer->i4_tgt_frm_rate,
533
11.2k
                       i4_bits_per_frm[i]);
534
        /* Drain rate = bitrate/(framerate/1000) */
535
11.2k
        ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
536
        /* Initialize the bits per frame error bits calculation */
537
11.2k
        irc_change_bitrate_in_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
538
11.2k
                                         i4_bit_rate[i]);
539
11.2k
    }
540
541
    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
542
5.62k
    if(i4_bit_rate[0] == i4_bit_rate[1]) /* This would mean CBR mode */
543
5.62k
    {
544
5.62k
        X_PROD_Y_DIV_Z(i4_bit_rate[0], ps_cbr_buffer->i4_max_delay, 1000,
545
5.62k
                       ps_cbr_buffer->i4_buffer_size);
546
5.62k
        ps_cbr_buffer->i4_is_cbr_mode = 1;
547
5.62k
    }
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
5.62k
    if(ps_cbr_buffer->i4_buffer_size
561
5.62k
                    > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
562
655
    {
563
655
        ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
564
655
    }
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
5.62k
    ps_cbr_buffer->i4_stuffing_threshold = (i4_bit_rate[0]
573
5.62k
                    - (i4_bits_per_frm[0]
574
5.62k
                                    * (ps_cbr_buffer->i4_tgt_frm_rate / 1000)));
575
576
22.4k
    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
577
16.8k
    {
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
16.8k
        WORD32 i4_index;
589
16.8k
        i4_index = i4_i > 0 ? 1 : 0;
590
16.8k
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
591
16.8k
                        - (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
16.8k
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
598
16.8k
    }
599
600
    /* Storing the input parameters for using it for change functions */
601
16.8k
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
602
11.2k
    {
603
11.2k
        ps_cbr_buffer->ai4_bit_rate[i] = i4_bit_rate[i];
604
11.2k
    }
605
5.62k
}
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
16.6k
{
646
16.6k
    return (ps_cbr_buffer->i4_max_delay);
647
16.6k
}
648
649
WORD32 irc_get_cbr_buffer_size(cbr_buffer_t *ps_cbr_buffer)
650
63.0k
{
651
63.0k
    return (ps_cbr_buffer->i4_buffer_size);
652
63.0k
}