summaryrefslogtreecommitdiffstats
path: root/encoder/irc_cbr_buffer_control.c
blob: 9febbc8558c604100da05f22726c8676864b9f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/

/*****************************************************************************/
/* File Includes                                                             */
/*****************************************************************************/

/* System include files */
#include <stdio.h>

/* User include files */
#include "irc_datatypes.h"
#include "irc_cntrl_param.h"
#include "irc_common.h"
#include "irc_mem_req_and_acq.h"
#include "irc_fixed_point_error_bits.h"
#include "irc_cbr_buffer_control.h"
#include "irc_trace_support.h"

typedef struct cbr_buffer_t
{
    /* Buffer size = Delay * Bitrate*/
    WORD32 i4_buffer_size;

    /* Constant drain rate */
    WORD32 i4_drain_bits_per_frame[MAX_NUM_DRAIN_RATES];

    /* Encoder Buffer Fullness */
    WORD32 i4_ebf;

    /* Upper threshold of the Buffer */
    WORD32 i4_upr_thr[MAX_PIC_TYPE];

    /* Lower threshold of the Buffer */
    WORD32 i4_low_thr[MAX_PIC_TYPE];

    /* Stuffing threshold equal to error bits per second in the drain bits
     * fixed point computation */
    WORD32 i4_stuffing_threshold;

    /* For error due to bits per frame calculation */
    error_bits_handle aps_bpf_error_bits[MAX_NUM_DRAIN_RATES];

    /* Whether the buffer model is used for CBR or VBR streaming */
    WORD32 i4_is_cbr_mode;

    /* Input parameters stored for initialization */
    WORD32 ai4_bit_rate[MAX_NUM_DRAIN_RATES];

    WORD32 i4_max_delay;

    WORD32 ai4_num_pics_in_delay_period[MAX_PIC_TYPE];

    WORD32 i4_tgt_frm_rate;

    UWORD32 u4_max_vbv_buf_size;

} cbr_buffer_t;

WORD32 irc_cbr_buffer_num_fill_use_free_memtab(cbr_buffer_t **pps_cbr_buffer,
                                               itt_memtab_t *ps_memtab,
                                               ITT_FUNC_TYPE_E e_func_type)
{
    WORD32 i4_mem_tab_idx = 0, i;
    cbr_buffer_t s_cbr_buffer_temp;

    /*
     * Hack for all alloc, during which we don't have any state memory.
     * Dereferencing can cause issues
     */
    if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
        (*pps_cbr_buffer) = &s_cbr_buffer_temp;

    if(e_func_type != GET_NUM_MEMTAB)
    {
        fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(cbr_buffer_t),
                    ALIGN_128_BYTE, PERSISTENT, DDR);
        use_or_fill_base(&ps_memtab[0], (void**)pps_cbr_buffer, e_func_type);
    }
    i4_mem_tab_idx++;

    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
    {
        i4_mem_tab_idx += irc_error_bits_num_fill_use_free_memtab(
                        &pps_cbr_buffer[0]->aps_bpf_error_bits[i],
                        &ps_memtab[i4_mem_tab_idx], e_func_type);
    }
    return (i4_mem_tab_idx);
}

/******************************************************************************
 * @brief Initialize the CBR VBV buffer state.
 * This could however be used for VBR streaming VBV also
 *
 ******************************************************************************/
void irc_init_cbr_buffer(cbr_buffer_t *ps_cbr_buffer,
                         WORD32 i4_buffer_delay,
                         WORD32 i4_tgt_frm_rate,
                         WORD32 *i4_bit_rate,
                         UWORD32 *u4_num_pics_in_delay_prd,
                         UWORD32 u4_vbv_buf_size)
{
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
    int i;

    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
    {
        X_PROD_Y_DIV_Z(i4_bit_rate[i], 1000, i4_tgt_frm_rate,
                       i4_bits_per_frm[i]);
        /* Drain rate = bitrate/(framerate/1000) */
        ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
        /* Initialize the bits per frame error bits calculation */
        irc_init_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
                            i4_tgt_frm_rate, i4_bit_rate[i]);
    }

    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
    /* This would mean CBR mode */
    if(i4_bit_rate[0] == i4_bit_rate[1])
    {
        X_PROD_Y_DIV_Z(i4_bit_rate[0], i4_buffer_delay, 1000,
                       ps_cbr_buffer->i4_buffer_size);
        ps_cbr_buffer->i4_is_cbr_mode = 1;
    }
    else
    {
        /* VBR streaming case which has different drain rates for I and P */
        ps_cbr_buffer->i4_buffer_size = u4_num_pics_in_delay_prd[0]
                                        * ps_cbr_buffer->i4_drain_bits_per_frame[0]
                                        + u4_num_pics_in_delay_prd[1]
                                        * ps_cbr_buffer->i4_drain_bits_per_frame[1];

        ps_cbr_buffer->i4_is_cbr_mode = 0;
    }

    if(ps_cbr_buffer->i4_buffer_size > (WORD32)u4_vbv_buf_size)
    {
        ps_cbr_buffer->i4_buffer_size = u4_vbv_buf_size;
    }

    /* Initially Encoder buffer fullness is zero */
    ps_cbr_buffer->i4_ebf = 0;

    /* tgt_frame_rate is divided by 1000 because, an approximate value is fine
     * as this is just a threshold below which stuffing is done to avoid buffer
     * underflow due to fixed point error in drain rate
     */
    ps_cbr_buffer->i4_stuffing_threshold = (i4_bit_rate[0]
                    - (i4_bits_per_frm[0] * (i4_tgt_frm_rate / 1000)));

    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
    {
        /*
         * Upper threshold for
         * I frame = 1 * bits per frame
         * P Frame = 4 * bits per frame.
         * The threshold for I frame is only 1 * bits per frame as the threshold
         * should only account for error in estimated bits.
         * In P frame it should account for difference bets bits consumed by
         * I(Scene change) and P frame I to P complexity is assumed to be 5.
         */
        WORD32 i4_index;
        i4_index = i4_i > 0 ? 1 : 0;
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
                        - (ps_cbr_buffer->i4_buffer_size >> 3);

        /*
         * For both I and P frame Lower threshold is equal to drain rate.Even if
         * the encoder consumes zero bits it should have enough bits to drain
         */
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
    }

    /* Storing the input parameters for using it for change functions */
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
    {
        ps_cbr_buffer->ai4_bit_rate[i] = i4_bit_rate[i];
    }

    for(i = 0; i < MAX_PIC_TYPE; i++)
    {
        ps_cbr_buffer->ai4_num_pics_in_delay_period[i] =
                        u4_num_pics_in_delay_prd[i];
    }
    ps_cbr_buffer->i4_tgt_frm_rate = i4_tgt_frm_rate;
    ps_cbr_buffer->i4_max_delay = i4_buffer_delay;
    ps_cbr_buffer->u4_max_vbv_buf_size = u4_vbv_buf_size;
}

/******************************************************************************
 * @brief Condition check for constraining the number of bits allocated based on
 * bufer size
 ******************************************************************************/
WORD32 irc_cbr_buffer_constraint_check(cbr_buffer_t *ps_cbr_buffer,
                                       WORD32 i4_tgt_bits,
                                       picture_type_e e_pic_type)
{
    WORD32 i4_max_tgt_bits, i4_min_tgt_bits;
    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];

    /* Max tgt bits = Upper threshold - current encoder buffer fullness */
    i4_max_tgt_bits = ps_cbr_buffer->i4_upr_thr[e_pic_type]
                    - ps_cbr_buffer->i4_ebf;
    /* Max tgt bits cannot be negative */
    if(i4_max_tgt_bits < 0)
        i4_max_tgt_bits = 0;

    /*
     * Min tgt bits , least number of bits in the Encoder after
     * draining such that it is greater than lower threshold
     */
    i4_min_tgt_bits = ps_cbr_buffer->i4_low_thr[e_pic_type]
                    - (ps_cbr_buffer->i4_ebf - i4_drain_bits_per_frame);
    /* Min tgt bits cannot be negative */
    if(i4_min_tgt_bits < 0)
        i4_min_tgt_bits = 0;

    /* Current tgt bits should be between max and min tgt bits */
    CLIP(i4_tgt_bits, i4_max_tgt_bits, i4_min_tgt_bits);
    return i4_tgt_bits;
}

/* *****************************************************************************
 * @brief constaints the bit allocation based on buffer size
 *
 ******************************************************************************/
WORD32 irc_vbr_stream_buffer_constraint_check(cbr_buffer_t *ps_cbr_buffer,
                                              WORD32 i4_tgt_bits,
                                              picture_type_e e_pic_type)
{
    WORD32 i4_max_tgt_bits;

    /* Max tgt bits = Upper threshold - current encoder buffer fullness */
    i4_max_tgt_bits = ps_cbr_buffer->i4_upr_thr[e_pic_type]
                    - ps_cbr_buffer->i4_ebf;

    /* Max tgt bits cannot be negative */
    if(i4_max_tgt_bits < 0)
        i4_max_tgt_bits = 0;

    if(i4_tgt_bits > i4_max_tgt_bits)
        i4_tgt_bits = i4_max_tgt_bits;

    return i4_tgt_bits;
}

/* *****************************************************************************
 * @brief Verifies the buffer state and returns whether it is overflowing,
 * underflowing or normal
 *
 ******************************************************************************/
vbv_buf_status_e irc_get_cbr_buffer_status(cbr_buffer_t *ps_cbr_buffer,
                                           WORD32 i4_tot_consumed_bits,
                                           WORD32 *pi4_num_bits_to_prevent_overflow,
                                           picture_type_e e_pic_type)
{
    vbv_buf_status_e e_buf_status;
    WORD32 i4_cur_enc_buf;
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
                            irc_get_error_bits(ps_cbr_buffer
                                               ->aps_bpf_error_bits[0]) :
                            irc_get_error_bits(ps_cbr_buffer
                                               ->aps_bpf_error_bits[1]);

    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];

    /* Add the tot consumed bits to the Encoder Buffer*/
    i4_cur_enc_buf = ps_cbr_buffer->i4_ebf + i4_tot_consumed_bits;

    /* If the Encoder exceeds the Buffer Size signal an Overflow*/
    if(i4_cur_enc_buf > ps_cbr_buffer->i4_buffer_size)
    {
        e_buf_status = VBV_OVERFLOW;
        i4_cur_enc_buf = ps_cbr_buffer->i4_buffer_size;
    }
    else
    {
        /*
         * Subtract the constant drain bits and error bits due to fixed point
         * implementation
         */
        i4_cur_enc_buf -= (i4_drain_bits_per_frame + i4_error_bits);

        /*
         * If the buffer is less than stuffing threshold an Underflow is
         * signaled else its NORMAL
         */
        if(i4_cur_enc_buf < ps_cbr_buffer->i4_stuffing_threshold)
        {
            e_buf_status = VBV_UNDERFLOW;
        }
        else
        {
            e_buf_status = VBV_NORMAL;
        }

        if(i4_cur_enc_buf < 0)
            i4_cur_enc_buf = 0;
    }

    /*
     * The RC lib models the encoder buffer, but the VBV buffer characterizes
     * the decoder buffer
     */
    if(e_buf_status == VBV_OVERFLOW)
    {
        e_buf_status = VBV_UNDERFLOW;
    }
    else if(e_buf_status == VBV_UNDERFLOW)
    {
        e_buf_status = VBV_OVERFLOW;
    }

    pi4_num_bits_to_prevent_overflow[0] = (ps_cbr_buffer->i4_buffer_size
                    - i4_cur_enc_buf);

    return e_buf_status;
}

/*******************************************************************************
 * @brief Based on the bits consumed the buffer model is updated
 ******************************************************************************/
void irc_update_cbr_buffer(cbr_buffer_t *ps_cbr_buffer,
                           WORD32 i4_tot_consumed_bits,
                           picture_type_e e_pic_type)
{
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
                           irc_get_error_bits(ps_cbr_buffer->
                                             aps_bpf_error_bits[0]) :
                           irc_get_error_bits( ps_cbr_buffer->
                                              aps_bpf_error_bits[1]);

    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];

    /* Update the Encoder buffer with the total consumed bits*/
    ps_cbr_buffer->i4_ebf += i4_tot_consumed_bits;

    /*
     * Subtract the drain bits and error bits due to fixed point
     * implementation
     */
    ps_cbr_buffer->i4_ebf -= (i4_drain_bits_per_frame + i4_error_bits);

    if(ps_cbr_buffer->i4_ebf < 0)
        ps_cbr_buffer->i4_ebf = 0;

    /*SS - Fix for lack of stuffing*/
    if(ps_cbr_buffer->i4_ebf > ps_cbr_buffer->i4_buffer_size)
    {
        trace_printf(
             (const WORD8*)"Error: Should not be coming here with stuffing\n");
        ps_cbr_buffer->i4_ebf = ps_cbr_buffer->i4_buffer_size;
    }
}

/*******************************************************************************
 * @brief If the buffer underflows then return the number of bits to prevent
 * underflow
 *
 ******************************************************************************/
WORD32 irc_get_cbr_bits_to_stuff(cbr_buffer_t *ps_cbr_buffer,
                                 WORD32 i4_tot_consumed_bits,
                                 picture_type_e e_pic_type)
{
    WORD32 i4_bits_to_stuff;
    WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
                            irc_get_error_bits(ps_cbr_buffer
                                               ->aps_bpf_error_bits[0]) :
                            irc_get_error_bits(ps_cbr_buffer
                                               ->aps_bpf_error_bits[1]);

    WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
                                     ps_cbr_buffer->i4_drain_bits_per_frame[0] :
                                     ps_cbr_buffer->i4_drain_bits_per_frame[1];

    /*
     * Stuffing bits got from the following equation
     * Stuffing_threshold = ebf + tcb - drain bits - error bits + stuff_bits
     */
    i4_bits_to_stuff = i4_drain_bits_per_frame + i4_error_bits
                    + ps_cbr_buffer->i4_stuffing_threshold
                    - (ps_cbr_buffer->i4_ebf + i4_tot_consumed_bits);

    return i4_bits_to_stuff;
}

/*******************************************************************************
 * @brief Update the state for change in number of pics in the delay period
 *
 ******************************************************************************/
void irc_change_cbr_vbv_num_pics_in_delay_period(cbr_buffer_t *ps_cbr_buffer,
                                                 UWORD32 *u4_num_pics_in_delay_prd)
{
    WORD32 i;

    if(!ps_cbr_buffer->i4_is_cbr_mode)
    {
        ps_cbr_buffer->i4_buffer_size =
                        u4_num_pics_in_delay_prd[0]
                        * ps_cbr_buffer->i4_drain_bits_per_frame[0]
                        + u4_num_pics_in_delay_prd[1]
                        * ps_cbr_buffer->i4_drain_bits_per_frame[1];

        if(ps_cbr_buffer->i4_buffer_size
                        > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
        {
            ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
        }
        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            ps_cbr_buffer->i4_upr_thr[i] = ps_cbr_buffer->i4_buffer_size
                            - (ps_cbr_buffer->i4_buffer_size >> 3);
        }

        /* Re-initialize the number of pics in delay period */
        for(i = 0; i < MAX_PIC_TYPE; i++)
        {
            ps_cbr_buffer->ai4_num_pics_in_delay_period[i] =
                            u4_num_pics_in_delay_prd[i];
        }
    }
}

/******************************************************************************
 * @brief update the state for change in target frame rate
 *
 ******************************************************************************/
void irc_change_cbr_vbv_tgt_frame_rate(cbr_buffer_t *ps_cbr_buffer,
                                       WORD32 i4_tgt_frm_rate)
{
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
    int i;

    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
    {
        X_PROD_Y_DIV_Z(ps_cbr_buffer->ai4_bit_rate[i], 1000, i4_tgt_frm_rate,
                       i4_bits_per_frm[i]);
        /* Drain rate = bitrate/(framerate/1000) */
        ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
        /* Initialize the bits per frame error bits calculation */
        irc_change_frm_rate_in_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
                                          i4_tgt_frm_rate);
    }

    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
    if(!ps_cbr_buffer->i4_is_cbr_mode)
    {
        /* VBR streaming case which has different drain rates for I and P */
        ps_cbr_buffer->i4_buffer_size =
                        ps_cbr_buffer->ai4_num_pics_in_delay_period[0]
                      * ps_cbr_buffer->i4_drain_bits_per_frame[0]
                      + ps_cbr_buffer->ai4_num_pics_in_delay_period[1]
                      * ps_cbr_buffer->i4_drain_bits_per_frame[1];
    }

    if(ps_cbr_buffer->i4_buffer_size
                    > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
    {
        ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
    }

    /*
     * Tgt_frame_rate is divided by 1000 because an approximate value is fine as
     * this is just a threshold below which stuffing is done to avoid buffer
     * underflow due to fixed point error in drain rate
     */
    ps_cbr_buffer->i4_stuffing_threshold = (ps_cbr_buffer->ai4_bit_rate[0]
                    - (i4_bits_per_frm[0] * (i4_tgt_frm_rate / 1000)));

    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
    {
        /*
         * Upper threshold for
         * I frame = 1 * bits per frame
         * P Frame = 4 * bits per frame.
         * The threshold for I frame is only 1 * bits per frame as the threshold should
         * only account for error in estimated bits.
         * In P frame it should account for difference bets bits consumed by I(Scene change)
         * and P frame I to P complexity is assumed to be 5.
         */
        WORD32 i4_index;
        i4_index = i4_i > 0 ? 1 : 0;
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
                        - (ps_cbr_buffer->i4_buffer_size >> 3);

        /*
         * For both I and P frame Lower threshold is equal to drain rate.
         * Even if the encoder consumes zero bits it should have enough bits to
         * drain
         */
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
    }

    /* Storing the input parameters for using it for change functions */
    ps_cbr_buffer->i4_tgt_frm_rate = i4_tgt_frm_rate;
}

/*******************************************************************************
 * @brief Change the state for change in bit rate
 *
 ******************************************************************************/
void irc_change_cbr_vbv_bit_rate(cbr_buffer_t *ps_cbr_buffer,
                                 WORD32 *i4_bit_rate)
{
    WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
    int i;

    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
    {
        X_PROD_Y_DIV_Z(i4_bit_rate[i], 1000, ps_cbr_buffer->i4_tgt_frm_rate,
                       i4_bits_per_frm[i]);
        /* Drain rate = bitrate/(framerate/1000) */
        ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
        /* Initialize the bits per frame error bits calculation */
        irc_change_bitrate_in_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
                                         i4_bit_rate[i]);
    }

    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
    if(i4_bit_rate[0] == i4_bit_rate[1]) /* This would mean CBR mode */
    {
        X_PROD_Y_DIV_Z(i4_bit_rate[0], ps_cbr_buffer->i4_max_delay, 1000,
                       ps_cbr_buffer->i4_buffer_size);
        ps_cbr_buffer->i4_is_cbr_mode = 1;
    }
    else
    {
        /* VBR streaming case which has different drain rates for I and P */
        ps_cbr_buffer->i4_buffer_size =
                        ps_cbr_buffer->ai4_num_pics_in_delay_period[0]
                      * ps_cbr_buffer->i4_drain_bits_per_frame[0]
                      + ps_cbr_buffer->ai4_num_pics_in_delay_period[1]
                      * ps_cbr_buffer->i4_drain_bits_per_frame[1];

        ps_cbr_buffer->i4_is_cbr_mode = 0;
    }

    if(ps_cbr_buffer->i4_buffer_size
                    > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
    {
        ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
    }

    /*
     * tgt_frame_rate is divided by 1000 because
     * an approximate value is fine as this is just a threshold below which
     * stuffing is done to avoid buffer underflow due to fixed point
     * error in drain rate
     */
    ps_cbr_buffer->i4_stuffing_threshold = (i4_bit_rate[0]
                    - (i4_bits_per_frm[0]
                                    * (ps_cbr_buffer->i4_tgt_frm_rate / 1000)));

    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
    {
        /*
         * Upper threshold for
         * I frame = 1 * bits per frame
         * P Frame = 4 * bits per frame.
         * The threshold for I frame is only 1 * bits per frame as the threshold
         * should only account for error in estimated bits.
         * In P frame it should account for difference bets bits consumed by
         * I(Scene change) and P frame I to P complexity is assumed to be 5.
         */

        WORD32 i4_index;
        i4_index = i4_i > 0 ? 1 : 0;
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
                        - (ps_cbr_buffer->i4_buffer_size >> 3);

        /* For both I and P frame Lower threshold is equal to drain rate.
         * Even if the encoder consumes zero bits it should have enough bits to
         * drain
         */
        ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
    }

    /* Storing the input parameters for using it for change functions */
    for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
    {
        ps_cbr_buffer->ai4_bit_rate[i] = i4_bit_rate[i];
    }
}

void irc_change_cbr_buffer_delay(cbr_buffer_t *ps_cbr_buffer,
                                 WORD32 i4_buffer_delay)
{
    WORD32 i4_i;

    /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
    if(ps_cbr_buffer->i4_is_cbr_mode)
    {
        X_PROD_Y_DIV_Z(ps_cbr_buffer->ai4_bit_rate[0], i4_buffer_delay, 1000,
                       ps_cbr_buffer->i4_buffer_size);
    }

    if(ps_cbr_buffer->i4_buffer_size
                    > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
    {
        ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
    }

    for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
    {
        /*
         * Upper threshold for
         * I frame = 1 * bits per frame
         * P Frame = 4 * bits per frame.
         * The threshold for I frame is only 1 * bits per frame as the threshold
         * should only account for error in estimated bits.
         * In P frame it should account for difference bets bits consumed by I
         * (Scene change) and P frame I to P complexity is assumed to be 5.
         */
        ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
                        - (ps_cbr_buffer->i4_buffer_size >> 3);
    }

    /* Storing the input parameters for using it for change functions */
    ps_cbr_buffer->i4_max_delay = i4_buffer_delay;
}

WORD32 irc_get_cbr_buffer_delay(cbr_buffer_t *ps_cbr_buffer)
{
    return (ps_cbr_buffer->i4_max_delay);
}

WORD32 irc_get_cbr_buffer_size(cbr_buffer_t *ps_cbr_buffer)
{
    return (ps_cbr_buffer->i4_buffer_size);
}