• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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)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 {
81     WORD32 i4_mem_tab_idx = 0, i;
82     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     if(e_func_type == GET_NUM_MEMTAB || e_func_type == FILL_MEMTAB)
89         (*pps_cbr_buffer) = &s_cbr_buffer_temp;
90 
91     if(e_func_type != GET_NUM_MEMTAB)
92     {
93         fill_memtab(&ps_memtab[i4_mem_tab_idx], sizeof(cbr_buffer_t),
94                     ALIGN_128_BYTE, PERSISTENT, DDR);
95         use_or_fill_base(&ps_memtab[0], (void**)pps_cbr_buffer, e_func_type);
96     }
97     i4_mem_tab_idx++;
98 
99     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
100     {
101         i4_mem_tab_idx += irc_error_bits_num_fill_use_free_memtab(
102                         &pps_cbr_buffer[0]->aps_bpf_error_bits[i],
103                         &ps_memtab[i4_mem_tab_idx], e_func_type);
104     }
105     return (i4_mem_tab_idx);
106 }
107 
108 /******************************************************************************
109  * @brief Initialize the CBR VBV buffer state.
110  * This could however be used for VBR streaming VBV also
111  *
112  ******************************************************************************/
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)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 {
120     WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
121     int i;
122 
123     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
124     {
125         X_PROD_Y_DIV_Z(i4_bit_rate[i], 1000, i4_tgt_frm_rate,
126                        i4_bits_per_frm[i]);
127         /* Drain rate = bitrate/(framerate/1000) */
128         ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
129         /* Initialize the bits per frame error bits calculation */
130         irc_init_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
131                             i4_tgt_frm_rate, i4_bit_rate[i]);
132     }
133 
134     /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
135     /* This would mean CBR mode */
136     if(i4_bit_rate[0] == i4_bit_rate[1])
137     {
138         X_PROD_Y_DIV_Z(i4_bit_rate[0], i4_buffer_delay, 1000,
139                        ps_cbr_buffer->i4_buffer_size);
140         ps_cbr_buffer->i4_is_cbr_mode = 1;
141     }
142     else
143     {
144         /* VBR streaming case which has different drain rates for I and P */
145         ps_cbr_buffer->i4_buffer_size = u4_num_pics_in_delay_prd[0]
146                                         * ps_cbr_buffer->i4_drain_bits_per_frame[0]
147                                         + u4_num_pics_in_delay_prd[1]
148                                         * ps_cbr_buffer->i4_drain_bits_per_frame[1];
149 
150         ps_cbr_buffer->i4_is_cbr_mode = 0;
151     }
152 
153     if(ps_cbr_buffer->i4_buffer_size > (WORD32)u4_vbv_buf_size)
154     {
155         ps_cbr_buffer->i4_buffer_size = u4_vbv_buf_size;
156     }
157 
158     /* Initially Encoder buffer fullness is zero */
159     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     ps_cbr_buffer->i4_stuffing_threshold = (i4_bit_rate[0]
166                     - (i4_bits_per_frm[0] * (i4_tgt_frm_rate / 1000)));
167 
168     for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
169     {
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         WORD32 i4_index;
180         i4_index = i4_i > 0 ? 1 : 0;
181         ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
182                         - (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         ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
189     }
190 
191     /* Storing the input parameters for using it for change functions */
192     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
193     {
194         ps_cbr_buffer->ai4_bit_rate[i] = i4_bit_rate[i];
195     }
196 
197     for(i = 0; i < MAX_PIC_TYPE; i++)
198     {
199         ps_cbr_buffer->ai4_num_pics_in_delay_period[i] =
200                         u4_num_pics_in_delay_prd[i];
201     }
202     ps_cbr_buffer->i4_tgt_frm_rate = i4_tgt_frm_rate;
203     ps_cbr_buffer->i4_max_delay = i4_buffer_delay;
204     ps_cbr_buffer->u4_max_vbv_buf_size = u4_vbv_buf_size;
205 }
206 
207 /******************************************************************************
208  * @brief Condition check for constraining the number of bits allocated based on
209  * bufer size
210  ******************************************************************************/
irc_cbr_buffer_constraint_check(cbr_buffer_t * ps_cbr_buffer,WORD32 i4_tgt_bits,picture_type_e e_pic_type)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 {
215     WORD32 i4_max_tgt_bits, i4_min_tgt_bits;
216     WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
217                                      ps_cbr_buffer->i4_drain_bits_per_frame[0] :
218                                      ps_cbr_buffer->i4_drain_bits_per_frame[1];
219 
220     /* Max tgt bits = Upper threshold - current encoder buffer fullness */
221     i4_max_tgt_bits = ps_cbr_buffer->i4_upr_thr[e_pic_type]
222                     - ps_cbr_buffer->i4_ebf;
223     /* Max tgt bits cannot be negative */
224     if(i4_max_tgt_bits < 0)
225         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     i4_min_tgt_bits = ps_cbr_buffer->i4_low_thr[e_pic_type]
232                     - (ps_cbr_buffer->i4_ebf - i4_drain_bits_per_frame);
233     /* Min tgt bits cannot be negative */
234     if(i4_min_tgt_bits < 0)
235         i4_min_tgt_bits = 0;
236 
237     /* Current tgt bits should be between max and min tgt bits */
238     CLIP(i4_tgt_bits, i4_max_tgt_bits, i4_min_tgt_bits);
239     return i4_tgt_bits;
240 }
241 
242 /* *****************************************************************************
243  * @brief constaints the bit allocation based on buffer size
244  *
245  ******************************************************************************/
irc_vbr_stream_buffer_constraint_check(cbr_buffer_t * ps_cbr_buffer,WORD32 i4_tgt_bits,picture_type_e e_pic_type)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 {
250     WORD32 i4_max_tgt_bits;
251 
252     /* Max tgt bits = Upper threshold - current encoder buffer fullness */
253     i4_max_tgt_bits = ps_cbr_buffer->i4_upr_thr[e_pic_type]
254                     - ps_cbr_buffer->i4_ebf;
255 
256     /* Max tgt bits cannot be negative */
257     if(i4_max_tgt_bits < 0)
258         i4_max_tgt_bits = 0;
259 
260     if(i4_tgt_bits > i4_max_tgt_bits)
261         i4_tgt_bits = i4_max_tgt_bits;
262 
263     return i4_tgt_bits;
264 }
265 
266 /* *****************************************************************************
267  * @brief Verifies the buffer state and returns whether it is overflowing,
268  * underflowing or normal
269  *
270  ******************************************************************************/
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)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 {
276     vbv_buf_status_e e_buf_status;
277     WORD32 i4_cur_enc_buf;
278     WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
279                             irc_get_error_bits(ps_cbr_buffer
280                                                ->aps_bpf_error_bits[0]) :
281                             irc_get_error_bits(ps_cbr_buffer
282                                                ->aps_bpf_error_bits[1]);
283 
284     WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
285                                      ps_cbr_buffer->i4_drain_bits_per_frame[0] :
286                                      ps_cbr_buffer->i4_drain_bits_per_frame[1];
287 
288     /* Add the tot consumed bits to the Encoder Buffer*/
289     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     if(i4_cur_enc_buf > ps_cbr_buffer->i4_buffer_size)
293     {
294         e_buf_status = VBV_OVERFLOW;
295         i4_cur_enc_buf = ps_cbr_buffer->i4_buffer_size;
296     }
297     else
298     {
299         /*
300          * Subtract the constant drain bits and error bits due to fixed point
301          * implementation
302          */
303         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         if(i4_cur_enc_buf < ps_cbr_buffer->i4_stuffing_threshold)
310         {
311             e_buf_status = VBV_UNDERFLOW;
312         }
313         else
314         {
315             e_buf_status = VBV_NORMAL;
316         }
317 
318         if(i4_cur_enc_buf < 0)
319             i4_cur_enc_buf = 0;
320     }
321 
322     /*
323      * The RC lib models the encoder buffer, but the VBV buffer characterizes
324      * the decoder buffer
325      */
326     if(e_buf_status == VBV_OVERFLOW)
327     {
328         e_buf_status = VBV_UNDERFLOW;
329     }
330     else if(e_buf_status == VBV_UNDERFLOW)
331     {
332         e_buf_status = VBV_OVERFLOW;
333     }
334 
335     pi4_num_bits_to_prevent_overflow[0] = (ps_cbr_buffer->i4_buffer_size
336                     - i4_cur_enc_buf);
337 
338     return e_buf_status;
339 }
340 
341 /*******************************************************************************
342  * @brief Based on the bits consumed the buffer model is updated
343  ******************************************************************************/
irc_update_cbr_buffer(cbr_buffer_t * ps_cbr_buffer,WORD32 i4_tot_consumed_bits,picture_type_e e_pic_type)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 {
348     WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
349                            irc_get_error_bits(ps_cbr_buffer->
350                                              aps_bpf_error_bits[0]) :
351                            irc_get_error_bits( ps_cbr_buffer->
352                                               aps_bpf_error_bits[1]);
353 
354     WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
355                                      ps_cbr_buffer->i4_drain_bits_per_frame[0] :
356                                      ps_cbr_buffer->i4_drain_bits_per_frame[1];
357 
358     /* Update the Encoder buffer with the total consumed bits*/
359     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     ps_cbr_buffer->i4_ebf -= (i4_drain_bits_per_frame + i4_error_bits);
366 
367     if(ps_cbr_buffer->i4_ebf < 0)
368         ps_cbr_buffer->i4_ebf = 0;
369 
370     /*SS - Fix for lack of stuffing*/
371     if(ps_cbr_buffer->i4_ebf > ps_cbr_buffer->i4_buffer_size)
372     {
373         trace_printf(
374              (const WORD8*)"Error: Should not be coming here with stuffing\n");
375         ps_cbr_buffer->i4_ebf = ps_cbr_buffer->i4_buffer_size;
376     }
377 }
378 
379 /*******************************************************************************
380  * @brief If the buffer underflows then return the number of bits to prevent
381  * underflow
382  *
383  ******************************************************************************/
irc_get_cbr_bits_to_stuff(cbr_buffer_t * ps_cbr_buffer,WORD32 i4_tot_consumed_bits,picture_type_e e_pic_type)384 WORD32 irc_get_cbr_bits_to_stuff(cbr_buffer_t *ps_cbr_buffer,
385                                  WORD32 i4_tot_consumed_bits,
386                                  picture_type_e e_pic_type)
387 {
388     WORD32 i4_bits_to_stuff;
389     WORD32 i4_error_bits = (e_pic_type == I_PIC) ?
390                             irc_get_error_bits(ps_cbr_buffer
391                                                ->aps_bpf_error_bits[0]) :
392                             irc_get_error_bits(ps_cbr_buffer
393                                                ->aps_bpf_error_bits[1]);
394 
395     WORD32 i4_drain_bits_per_frame = (e_pic_type == I_PIC) ?
396                                      ps_cbr_buffer->i4_drain_bits_per_frame[0] :
397                                      ps_cbr_buffer->i4_drain_bits_per_frame[1];
398 
399     /*
400      * Stuffing bits got from the following equation
401      * Stuffing_threshold = ebf + tcb - drain bits - error bits + stuff_bits
402      */
403     i4_bits_to_stuff = i4_drain_bits_per_frame + i4_error_bits
404                     + ps_cbr_buffer->i4_stuffing_threshold
405                     - (ps_cbr_buffer->i4_ebf + i4_tot_consumed_bits);
406 
407     return i4_bits_to_stuff;
408 }
409 
410 /*******************************************************************************
411  * @brief Update the state for change in number of pics in the delay period
412  *
413  ******************************************************************************/
irc_change_cbr_vbv_num_pics_in_delay_period(cbr_buffer_t * ps_cbr_buffer,UWORD32 * u4_num_pics_in_delay_prd)414 void irc_change_cbr_vbv_num_pics_in_delay_period(cbr_buffer_t *ps_cbr_buffer,
415                                                  UWORD32 *u4_num_pics_in_delay_prd)
416 {
417     WORD32 i;
418 
419     if(!ps_cbr_buffer->i4_is_cbr_mode)
420     {
421         ps_cbr_buffer->i4_buffer_size =
422                         u4_num_pics_in_delay_prd[0]
423                         * ps_cbr_buffer->i4_drain_bits_per_frame[0]
424                         + u4_num_pics_in_delay_prd[1]
425                         * ps_cbr_buffer->i4_drain_bits_per_frame[1];
426 
427         if(ps_cbr_buffer->i4_buffer_size
428                         > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
429         {
430             ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
431         }
432         for(i = 0; i < MAX_PIC_TYPE; i++)
433         {
434             ps_cbr_buffer->i4_upr_thr[i] = ps_cbr_buffer->i4_buffer_size
435                             - (ps_cbr_buffer->i4_buffer_size >> 3);
436         }
437 
438         /* Re-initialize the number of pics in delay period */
439         for(i = 0; i < MAX_PIC_TYPE; i++)
440         {
441             ps_cbr_buffer->ai4_num_pics_in_delay_period[i] =
442                             u4_num_pics_in_delay_prd[i];
443         }
444     }
445 }
446 
447 /******************************************************************************
448  * @brief update the state for change in target frame rate
449  *
450  ******************************************************************************/
irc_change_cbr_vbv_tgt_frame_rate(cbr_buffer_t * ps_cbr_buffer,WORD32 i4_tgt_frm_rate)451 void irc_change_cbr_vbv_tgt_frame_rate(cbr_buffer_t *ps_cbr_buffer,
452                                        WORD32 i4_tgt_frm_rate)
453 {
454     WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
455     int i;
456 
457     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
458     {
459         X_PROD_Y_DIV_Z(ps_cbr_buffer->ai4_bit_rate[i], 1000, i4_tgt_frm_rate,
460                        i4_bits_per_frm[i]);
461         /* Drain rate = bitrate/(framerate/1000) */
462         ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
463         /* Initialize the bits per frame error bits calculation */
464         irc_change_frm_rate_in_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
465                                           i4_tgt_frm_rate);
466     }
467 
468     /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
469     if(!ps_cbr_buffer->i4_is_cbr_mode)
470     {
471         /* VBR streaming case which has different drain rates for I and P */
472         ps_cbr_buffer->i4_buffer_size =
473                         ps_cbr_buffer->ai4_num_pics_in_delay_period[0]
474                       * ps_cbr_buffer->i4_drain_bits_per_frame[0]
475                       + ps_cbr_buffer->ai4_num_pics_in_delay_period[1]
476                       * ps_cbr_buffer->i4_drain_bits_per_frame[1];
477     }
478 
479     if(ps_cbr_buffer->i4_buffer_size
480                     > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
481     {
482         ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
483     }
484 
485     /*
486      * Tgt_frame_rate is divided by 1000 because an approximate value is fine as
487      * this is just a threshold below which stuffing is done to avoid buffer
488      * underflow due to fixed point error in drain rate
489      */
490     ps_cbr_buffer->i4_stuffing_threshold = (ps_cbr_buffer->ai4_bit_rate[0]
491                     - (i4_bits_per_frm[0] * (i4_tgt_frm_rate / 1000)));
492 
493     for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
494     {
495         /*
496          * Upper threshold for
497          * I frame = 1 * bits per frame
498          * P Frame = 4 * bits per frame.
499          * The threshold for I frame is only 1 * bits per frame as the threshold should
500          * only account for error in estimated bits.
501          * In P frame it should account for difference bets bits consumed by I(Scene change)
502          * and P frame I to P complexity is assumed to be 5.
503          */
504         WORD32 i4_index;
505         i4_index = i4_i > 0 ? 1 : 0;
506         ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
507                         - (ps_cbr_buffer->i4_buffer_size >> 3);
508 
509         /*
510          * For both I and P frame Lower threshold is equal to drain rate.
511          * Even if the encoder consumes zero bits it should have enough bits to
512          * drain
513          */
514         ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
515     }
516 
517     /* Storing the input parameters for using it for change functions */
518     ps_cbr_buffer->i4_tgt_frm_rate = i4_tgt_frm_rate;
519 }
520 
521 /*******************************************************************************
522  * @brief Change the state for change in bit rate
523  *
524  ******************************************************************************/
irc_change_cbr_vbv_bit_rate(cbr_buffer_t * ps_cbr_buffer,WORD32 * i4_bit_rate)525 void irc_change_cbr_vbv_bit_rate(cbr_buffer_t *ps_cbr_buffer,
526                                  WORD32 *i4_bit_rate)
527 {
528     WORD32 i4_i, i4_bits_per_frm[MAX_NUM_DRAIN_RATES];
529     int i;
530 
531     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
532     {
533         X_PROD_Y_DIV_Z(i4_bit_rate[i], 1000, ps_cbr_buffer->i4_tgt_frm_rate,
534                        i4_bits_per_frm[i]);
535         /* Drain rate = bitrate/(framerate/1000) */
536         ps_cbr_buffer->i4_drain_bits_per_frame[i] = i4_bits_per_frm[i];
537         /* Initialize the bits per frame error bits calculation */
538         irc_change_bitrate_in_error_bits(ps_cbr_buffer->aps_bpf_error_bits[i],
539                                          i4_bit_rate[i]);
540     }
541 
542     /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
543     if(i4_bit_rate[0] == i4_bit_rate[1]) /* This would mean CBR mode */
544     {
545         X_PROD_Y_DIV_Z(i4_bit_rate[0], ps_cbr_buffer->i4_max_delay, 1000,
546                        ps_cbr_buffer->i4_buffer_size);
547         ps_cbr_buffer->i4_is_cbr_mode = 1;
548     }
549     else
550     {
551         /* VBR streaming case which has different drain rates for I and P */
552         ps_cbr_buffer->i4_buffer_size =
553                         ps_cbr_buffer->ai4_num_pics_in_delay_period[0]
554                       * ps_cbr_buffer->i4_drain_bits_per_frame[0]
555                       + ps_cbr_buffer->ai4_num_pics_in_delay_period[1]
556                       * ps_cbr_buffer->i4_drain_bits_per_frame[1];
557 
558         ps_cbr_buffer->i4_is_cbr_mode = 0;
559     }
560 
561     if(ps_cbr_buffer->i4_buffer_size
562                     > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
563     {
564         ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
565     }
566 
567     /*
568      * tgt_frame_rate is divided by 1000 because
569      * an approximate value is fine as this is just a threshold below which
570      * stuffing is done to avoid buffer underflow due to fixed point
571      * error in drain rate
572      */
573     ps_cbr_buffer->i4_stuffing_threshold = (i4_bit_rate[0]
574                     - (i4_bits_per_frm[0]
575                                     * (ps_cbr_buffer->i4_tgt_frm_rate / 1000)));
576 
577     for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
578     {
579         /*
580          * Upper threshold for
581          * I frame = 1 * bits per frame
582          * P Frame = 4 * bits per frame.
583          * The threshold for I frame is only 1 * bits per frame as the threshold
584          * should only account for error in estimated bits.
585          * In P frame it should account for difference bets bits consumed by
586          * I(Scene change) and P frame I to P complexity is assumed to be 5.
587          */
588 
589         WORD32 i4_index;
590         i4_index = i4_i > 0 ? 1 : 0;
591         ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
592                         - (ps_cbr_buffer->i4_buffer_size >> 3);
593 
594         /* For both I and P frame Lower threshold is equal to drain rate.
595          * Even if the encoder consumes zero bits it should have enough bits to
596          * drain
597          */
598         ps_cbr_buffer->i4_low_thr[i4_i] = i4_bits_per_frm[i4_index];
599     }
600 
601     /* Storing the input parameters for using it for change functions */
602     for(i = 0; i < MAX_NUM_DRAIN_RATES; i++)
603     {
604         ps_cbr_buffer->ai4_bit_rate[i] = i4_bit_rate[i];
605     }
606 }
607 
irc_change_cbr_buffer_delay(cbr_buffer_t * ps_cbr_buffer,WORD32 i4_buffer_delay)608 void irc_change_cbr_buffer_delay(cbr_buffer_t *ps_cbr_buffer,
609                                  WORD32 i4_buffer_delay)
610 {
611     WORD32 i4_i;
612 
613     /* Bitrate * delay = buffer size, divide by 1000 as delay is in ms*/
614     if(ps_cbr_buffer->i4_is_cbr_mode)
615     {
616         X_PROD_Y_DIV_Z(ps_cbr_buffer->ai4_bit_rate[0], i4_buffer_delay, 1000,
617                        ps_cbr_buffer->i4_buffer_size);
618     }
619 
620     if(ps_cbr_buffer->i4_buffer_size
621                     > (WORD32)ps_cbr_buffer->u4_max_vbv_buf_size)
622     {
623         ps_cbr_buffer->i4_buffer_size = ps_cbr_buffer->u4_max_vbv_buf_size;
624     }
625 
626     for(i4_i = 0; i4_i < MAX_PIC_TYPE; i4_i++)
627     {
628         /*
629          * Upper threshold for
630          * I frame = 1 * bits per frame
631          * P Frame = 4 * bits per frame.
632          * The threshold for I frame is only 1 * bits per frame as the threshold
633          * should only account for error in estimated bits.
634          * In P frame it should account for difference bets bits consumed by I
635          * (Scene change) and P frame I to P complexity is assumed to be 5.
636          */
637         ps_cbr_buffer->i4_upr_thr[i4_i] = ps_cbr_buffer->i4_buffer_size
638                         - (ps_cbr_buffer->i4_buffer_size >> 3);
639     }
640 
641     /* Storing the input parameters for using it for change functions */
642     ps_cbr_buffer->i4_max_delay = i4_buffer_delay;
643 }
644 
irc_get_cbr_buffer_delay(cbr_buffer_t * ps_cbr_buffer)645 WORD32 irc_get_cbr_buffer_delay(cbr_buffer_t *ps_cbr_buffer)
646 {
647     return (ps_cbr_buffer->i4_max_delay);
648 }
649 
irc_get_cbr_buffer_size(cbr_buffer_t * ps_cbr_buffer)650 WORD32 irc_get_cbr_buffer_size(cbr_buffer_t *ps_cbr_buffer)
651 {
652     return (ps_cbr_buffer->i4_buffer_size);
653 }
654