summaryrefslogtreecommitdiffstats
path: root/encoder/ih264e_rate_control.c
blob: 1e2fe4f74d8c91218cab1e78840b13c0993f2b6d (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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/******************************************************************************
 *
 * 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
*  ih264e_rate_control.c
*
* @brief
*  Contains api function definitions for h264 rate control
*
* @author
*  ittiam
*
* @par List of Functions:
*  - ih264e_rc_init()
*  - ih264e_rc_get_picture_details()
*  - ih264e_rc_pre_enc()
*  - ih264e_update_rc_mb_info()
*  - ih264e_rc_get_buffer_status()
*  - ih264e_rc_post_enc()
*  - ih264e_update_rc_bits_info()
*
* @remarks
*  None
*
*******************************************************************************
*/

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

/* User include files */
#include "irc_datatypes.h"
#include "iv2.h"
#include "ive2.h"
#include "ih264e.h"
#include "ih264_defs.h"
#include "ih264_macros.h"
#include "ih264_structs.h"
#include "ih264_trans_quant_itrans_iquant.h"
#include "ih264_inter_pred_filters.h"
#include "ih264_mem_fns.h"
#include "ih264_padding.h"
#include "ih264_intra_pred_filters.h"
#include "ih264_deblk_edge_filters.h"
#include "ih264_common_tables.h"
#include "ih264e_defs.h"
#include "ih264e_globals.h"
#include "irc_mem_req_and_acq.h"
#include "irc_cntrl_param.h"
#include "irc_frame_info_collector.h"
#include "irc_rate_control_api.h"
#include "ih264e_time_stamp.h"
#include "ih264e_modify_frm_rate.h"
#include "ih264e_rate_control.h"
#include "ih264e_error.h"
#include "ih264e_bitstream.h"
#include "ime_distortion_metrics.h"
#include "ime_structs.h"
#include "ih264e_structs.h"
#include "ih264e_utils.h"
#include "irc_trace_support.h"


/*****************************************************************************/
/* Function Definitions                                                      */
/*****************************************************************************/

/**
*******************************************************************************
*
* @brief This function does nothing
*
* @par Description
*  This function does nothing
*
* @param[in] variadic function

* @returns none
*
* @remarks This function is used by the rc library for debugging purposes.
*  However this function was not part of rc library. So this is defined here
*  to resolve link issues.
*
*******************************************************************************
*/
int trace_printf(const WORD8 *format, ...)
{
    UNUSED(format);
    return(0);
};

/**
*******************************************************************************
*
* @brief
*  This function initializes rate control context and variables
*
* @par Description
*  This function initializes rate control type, source and target frame rate,
*  average and peak bitrate, intra-inter frame interval and initial
*  quantization parameter
*
* @param[in] pv_rc_api
*  Handle to rate control api
*
* @param[in] pv_frame_time
*  Handle to frame time context
*
* @param[in] pv_time_stamp
*  Handle to time stamp context
*
* @param[in] pv_pd_frm_rate
*  Handle to pull down frame time context
*
* @param[in] u4_max_frm_rate
*  Maximum frame rate
*
* @param[in] u4_src_frm_rate
*  Source frame rate
*
* @param[in] u4_tgt_frm_rate
*  Target frame rate
*
* @param[in] e_rate_control_type
*  Rate control type
*
* @param[in] u4_avg_bit_rate
*  Average bit rate
*
* @param[in] u4_peak_bit_rate
*  Peak bit rate
*
* @param[in] u4_max_delay
*  Maximum delay between frames
*
* @param[in] u4_intra_frame_interval
*  Intra frame interval
*
* @param[in] pu1_init_qp
*  Initial qp
*
* @param[in] i4_max_inter_frm_int
*  Maximum inter frame interval
*
* @param[in] pu1_min_max_qp
*  Array of min/max qp
*
* @param[in] u1_profile_level
*  Encoder profile level
*
* @returns none
*
* @remarks
*
*******************************************************************************
*/
void ih264e_rc_init(void *pv_rc_api,
                    void *pv_frame_time,
                    void *pv_time_stamp,
                    void *pv_pd_frm_rate,
                    UWORD32 u4_max_frm_rate,
                    UWORD32 u4_src_frm_rate,
                    UWORD32 u4_tgt_frm_rate,
                    rc_type_e e_rate_control_type,
                    UWORD32 u4_avg_bit_rate,
                    UWORD32 u4_peak_bit_rate,
                    UWORD32 u4_max_delay,
                    UWORD32 u4_intra_frame_interval,
                    UWORD8 *pu1_init_qp,
                    WORD32 i4_max_inter_frm_int,
                    UWORD8 *pu1_min_max_qp,
                    UWORD8 u1_profile_level)
{
//    UWORD8  u1_is_mb_level_rc_on = 0;
    UWORD32 au4_peak_bit_rate[2] = {0,0};
    UWORD32 u4_min_bit_rate      = 0;
    WORD32  i4_is_gop_closed     = 0;
//    WORD32  i4_use_est_intra_sad = 1;
    UWORD32 u4_src_ticks         = 0;
    UWORD32 u4_tgt_ticks         = 0;
    UWORD8  u1_level_idx         = ih264e_get_lvl_idx(u1_profile_level);
    UWORD32 u4_max_cpb_size      = 1200 * gas_ih264_lvl_tbl[u1_level_idx].u4_max_cpb_size;

    /* Fill the params needed for the RC init */
    if (e_rate_control_type == CBR_NLDRC)
    {
        au4_peak_bit_rate[0] = u4_avg_bit_rate;
        au4_peak_bit_rate[1] = u4_avg_bit_rate;
    }
    else
    {
        au4_peak_bit_rate[0] = u4_peak_bit_rate;
        au4_peak_bit_rate[1] = u4_peak_bit_rate;
    }

    /* Initialize frame time computation module*/
    ih264e_init_frame_time(pv_frame_time,
                           u4_src_frm_rate,  /* u4_src_frm_rate */
                           u4_tgt_frm_rate); /* u4_tgt_frm_rate */

    /* Initialize the pull_down frame rate */
    ih264e_init_pd_frm_rate(pv_pd_frm_rate,
                            u4_src_frm_rate);  /* u4_input_frm_rate */

    /* Initialize time stamp structure */
    ih264e_init_time_stamp(pv_time_stamp,
                           u4_max_frm_rate,    /* u4_max_frm_rate */
                           u4_src_frm_rate);   /* u4_src_frm_rate */

    u4_src_ticks = ih264e_frame_time_get_src_ticks(pv_frame_time);
    u4_tgt_ticks = ih264e_frame_time_get_tgt_ticks(pv_frame_time);

    /* Initialize the rate control */
    irc_initialise_rate_control(pv_rc_api,                  /* RC handle */
                                e_rate_control_type,        /* RC algo type */
                                0,                          /* MB activity on/off */
                                u4_avg_bit_rate,            /* Avg Bitrate */
                                au4_peak_bit_rate,          /* Peak bitrate array[2]:[I][P] */
                                u4_min_bit_rate,            /* Min Bitrate */
                                u4_src_frm_rate,            /* Src frame_rate */
                                u4_max_delay,               /* Max buffer delay */
                                u4_intra_frame_interval,    /* Intra frm_interval */
                                pu1_init_qp,                /* Init QP array[3]:[I][P][B] */
                                u4_max_cpb_size,            /* Max VBV/CPB Buffer Size */
                                i4_max_inter_frm_int,       /* Max inter frm_interval */
                                i4_is_gop_closed,           /* Open/Closed GOP */
                                pu1_min_max_qp,             /* Min-max QP array[6]:[Imax][Imin][Pmax][Pmin][Bmax][Bmin] */
                                0,                          /* How to calc the I-frame estimated_sad */
                                u4_src_ticks,               /* Src_ticks = LCM(src_frm_rate,tgt_frm_rate)/src_frm_rate */
                                u4_tgt_ticks);              /* Tgt_ticks = LCM(src_frm_rate,tgt_frm_rate)/tgt_frm_rate */
}

/**
*******************************************************************************
*
* @brief Function to get picture details
*
* @par   Description
*  This function returns the Picture type(I/P/B)
*
* @param[in] pv_rc_api
*  Handle to Rate control api
*
* @returns
*  Picture type
*
* @remarks none
*
*******************************************************************************
*/
picture_type_e ih264e_rc_get_picture_details(void *pv_rc_api)
{
    WORD32 i4_pic_id = 0;
    WORD32 i4_pic_disp_order_no = 0;
    picture_type_e e_rc_pic_type = P_PIC;

    irc_get_picture_details(pv_rc_api, &i4_pic_id, &i4_pic_disp_order_no,
                            &e_rc_pic_type);

    return (e_rc_pic_type);
}

/**
*******************************************************************************
*
* @brief  Function to get rate control output before encoding
*
* @par Description
*  This function is called before encoding the current frame and gets the qp
*  for the current frame from rate control module
*
* @param[in] ps_rate_control_api
*  Handle to rate control api
*
* @param[in] ps_pd_frm_rate
*  Handle to pull down frm rate context
*
* @param[in] ps_time_stamp
*  Handle to time stamp context
*
* @param[in] ps_frame_time
*  Handle to frame time context
*
* @param[in] i4_delta_time_stamp
*  Time stamp difference between frames
*
* @param[in] i4_total_mb_in_frame
*  Total Macro Blocks in frame
*
* @param[in/out] pe_vop_coding_type
*  Picture coding type(I/P/B)
*
* @param[in/out] pu1_frame_qp
*  QP for current frame
*
* @returns
*  Skip or encode the current frame
*
* @remarks
*
*******************************************************************************
*/
WORD32 ih264e_rc_pre_enc(void * ps_rate_control_api,
                         void * ps_pd_frm_rate,
                         void * ps_time_stamp,
                         void * ps_frame_time,
                         WORD32 i4_delta_time_stamp,
                         WORD32 i4_total_mb_in_frame,
                         picture_type_e *pe_vop_coding_type,
                         UWORD8 *pu1_frame_qp)
{
    WORD8 i4_skip_src = 0, i4_num_app_skips = 0;
    UWORD32 u4_src_not_skipped_for_dts = 0;

    /* Variables for the update_frm_level_info */
    WORD32  ai4_tot_mb_in_type[MAX_MB_TYPE];
    WORD32  ai4_tot_mb_type_qp[MAX_MB_TYPE]    = {0, 0};
    WORD32  ai4_mb_type_sad[MAX_MB_TYPE]       = {0, 0};
    WORD32  ai4_mb_type_tex_bits[MAX_MB_TYPE]  = {0, 0};
    WORD32   i4_total_frame_bits               = 0;
    WORD32   i4_total_hdr_bits                 = 0;
    WORD32   i4_avg_mb_activity                = 0;
    WORD32   i4_intra_frm_cost                 = 0;
    UWORD8   u1_is_scd                         = 0;

    /* Set all the MBs to Intra */
    ai4_tot_mb_in_type[0] = i4_total_mb_in_frame;
    ai4_tot_mb_in_type[1] = 0;

    /* If delta time stamp is greater than 1, do rcupdate that many times */
    for (i4_num_app_skips = 0; (i4_num_app_skips < i4_delta_time_stamp - 1); i4_num_app_skips++)
    {
        /*update the missing frames frm_rate with 0 */
        ih264e_update_pd_frm_rate(ps_pd_frm_rate,0);

        /* Update the time stamp */
        ih264e_update_time_stamp(ps_time_stamp);

        /* Do a pre encode skip update */

        irc_update_frame_level_info(ps_rate_control_api,
                                    (*pe_vop_coding_type),
                                    ai4_mb_type_sad,        /* Frame level SAD for each type of MB[Intra/Inter] */
                                    i4_total_frame_bits,    /* Total frame bits actually consumed */
                                    i4_total_hdr_bits,      /*header bits for model updation*/
                                    ai4_mb_type_tex_bits,   /* Total texture bits consumed for each type of MB[Intra/Inter] used for model */
                                    ai4_tot_mb_type_qp,     /* Total qp of all MBs based on mb type */
                                    ai4_tot_mb_in_type,     /* total number of mbs in each mb type */
                                    i4_avg_mb_activity,     /* Average mb activity in frame */
                                    u1_is_scd,              /* Is a scene change detected at the current frame */
                                    1,                      /* If it's a pre-encode skip */
                                    i4_intra_frm_cost,      /* Sum of Intra cost for each frame */
                                    0);                     /* Is pic handling [irc_update_pic_handling_state] done before update */
    }

    /* Update the time stamp for the current frame */
    ih264e_update_time_stamp(ps_time_stamp);

    /* Check if a src not needs to be skipped */
    i4_skip_src = ih264e_should_src_be_skipped(ps_frame_time,
                                               i4_delta_time_stamp,
                                               &u4_src_not_skipped_for_dts);

    /***********************************************************************
       Based on difference in source and target frame rate frames are skipped
     ***********************************************************************/
    if (i4_skip_src)
    {
        /*update the missing frames frm_rate with 0 */
        ih264e_update_pd_frm_rate(ps_pd_frm_rate,0);

        /* Do a pre encode skip update */
        irc_update_frame_level_info(ps_rate_control_api,
                                    (*pe_vop_coding_type),
                                    ai4_mb_type_sad,        /* Frame level SAD for each type of MB[Intra/Inter] */
                                    i4_total_frame_bits,    /* Total frame bits actually consumed */
                                    i4_total_hdr_bits,      /*header bits for model updation*/
                                    ai4_mb_type_tex_bits,   /* Total texture bits consumed for each type of MB[Intra/Inter] used for model */
                                    ai4_tot_mb_type_qp,     /* Total qp of all MBs based on mb type */
                                    ai4_tot_mb_in_type,     /* total number of mbs in each mb type */
                                    i4_avg_mb_activity,     /* Average mb activity in frame */
                                    u1_is_scd,              /* Is a scene change detected at the current frame */
                                    1,                      /* If it's a pre-encode skip */
                                    i4_intra_frm_cost,      /* Sum of Intra cost for each frame */
                                    0);                     /* Is pic handling [irc_update_pic_handling_state] done before update */

        /* Set the current frame type to NA */
        *pe_vop_coding_type = BUF_PIC;
    }
    else
    {
#define MAX_FRAME_BITS 0x7FFFFFFF
//        WORD32         i4_pic_id;
//        WORD32         i4_pic_disp_order_no;
        WORD32 i4_avg_frm_rate, i4_source_frame_rate;

        i4_source_frame_rate = ih264e_frame_time_get_src_frame_rate(ps_frame_time);

        /* Update the frame rate of the frame present with the tgt_frm_rate */
        /* If the frm was not skipped due to delta_time_stamp, update the
           frame_rate with double the tgt_frame_rate value, so that it makes
           up for one of the frames skipped by the application */
        ih264e_update_pd_frm_rate(ps_pd_frm_rate,
                                  i4_source_frame_rate);

        /* Based on the update get the average frame rate */
        i4_avg_frm_rate = ih264e_get_pd_avg_frm_rate(ps_pd_frm_rate);

        /* Call the RC library function to change the frame_rate to the
           actually achieved frm_rate */
        irc_change_frm_rate_for_bit_alloc(ps_rate_control_api, i4_avg_frm_rate);

        /* --------Rate control related things.  Get pic type and frame Qp---------*/
        /* Add picture to the stack. For IPP encoder we push the variable
           into the stack and get back the variables by requesting RC.
           This interface is designed for IPB encoder */
        irc_add_picture_to_stack(ps_rate_control_api, 1);

        /* Query the picture_type */
        *pe_vop_coding_type = ih264e_rc_get_picture_details(ps_rate_control_api);

        /* Get current frame Qp */
        pu1_frame_qp[0] = (UWORD8)irc_get_frame_level_qp(ps_rate_control_api,
                                                         (picture_type_e)(pe_vop_coding_type[0]),
                                                         MAX_FRAME_BITS);
    }

    return(i4_skip_src);
}

/**
*******************************************************************************
*
* @brief Function to update mb info for rate control context
*
* @par   Description
*  After encoding a mb, information such as mb type, qp used, mb distortion
*  resulted in encoding the block and so on needs to be preserved for modeling
*  RC. This is preserved via this function call.
*
* @param[in] ps_frame_info
*  Handle Frame info context
*
* @param[in] ps_proc
*  Process context
*
* @returns
*
* @remarks
*
*******************************************************************************
*/
void ih264e_update_rc_mb_info(frame_info_t *ps_frame_info, void *pv_proc)
{
    /* proc ctxt */
    process_ctxt_t *ps_proc = pv_proc;

    /* is intra or inter */
    WORD32 mb_type = !ps_proc->u4_is_intra;

    /* distortion */
    ps_frame_info->tot_mb_sad[mb_type] += ps_proc->i4_mb_distortion;

    /* qp */
    ps_frame_info->qp_sum[mb_type] += gau1_h264_to_mpeg2_qmap[ps_proc->u4_mb_qp];

    /* mb cnt */
    ps_frame_info->num_mbs[mb_type]++;

    /* cost */
    if (ps_proc->u4_is_intra)
    {
        ps_frame_info->intra_mb_cost_sum += ps_proc->i4_mb_cost;
    }
}

/**
*******************************************************************************
*
* @brief Function to get rate control buffer status
*
* @par Description
*  This function is used to get buffer status(underflow/overflow) by rate
*  control module
*
* @param[in] pv_rc_api
*  Handle to rate control api context
*
* @param[in] i4_total_frame_bits
*  Total frame bits
*
* @param[in] u1_pic_type
*  Picture type
*
* @param[in] pi4_num_bits_to_prevent_vbv_underflow
*  Number of bits to prevent underflow
*
* @param[out] pu1_is_enc_buf_overflow
*  Buffer overflow indication flag
*
* @param[out] pu1_is_enc_buf_underflow
*  Buffer underflow indication flag
*
* @returns
*
* @remarks
*
*******************************************************************************
*/
void ih264e_rc_get_buffer_status(void *pv_rc_api,
                                 WORD32 i4_total_frame_bits,
                                 picture_type_e e_pic_type,
                                 WORD32 *pi4_num_bits_to_prevent_vbv_underflow,
                                 UWORD8 *pu1_is_enc_buf_overflow,
                                 UWORD8 *pu1_is_enc_buf_underflow)
{
    vbv_buf_status_e e_vbv_buf_status = VBV_NORMAL;

    e_vbv_buf_status = irc_get_buffer_status(pv_rc_api,
                                             i4_total_frame_bits,
                                             e_pic_type,
                                             pi4_num_bits_to_prevent_vbv_underflow);

    if (e_vbv_buf_status == VBV_OVERFLOW)
    {
        *pu1_is_enc_buf_underflow = 1;
        *pu1_is_enc_buf_overflow = 0;
    }
    else if (e_vbv_buf_status == VBV_UNDERFLOW)
    {
        *pu1_is_enc_buf_underflow = 0;
        *pu1_is_enc_buf_overflow = 1;
    }
    else
    {
        *pu1_is_enc_buf_underflow = 0;
        *pu1_is_enc_buf_overflow = 0;
    }
}

/**
*******************************************************************************
*
* @brief Function to update rate control module after encoding
*
* @par Description
*  This function is used to update the rate control module after the current
*  frame encoding is done with details such as bits consumed, SAD for I/P/B,
*  intra cost ,mb type and other
*
* @param[in] ps_rate_control_api
*  Handle to rate control api context
*
* @param[in] ps_frame_info
*  Handle to frame info context
*
* @param[in] ps_pd_frm_rate
*  Handle to pull down frame rate context
*
* @param[in] ps_time_stamp
*  Handle to time stamp context
*
* @param[in] ps_frame_time
*  Handle to frame time context
*
* @param[in] i4_total_mb_in_frame
*  Total mb in frame
*
* @param[in] pe_vop_coding_type
*  Picture coding type
*
* @param[in] i4_is_first_frame
*  Is first frame
*
* @param[in] pi4_is_post_encode_skip
*  Post encoding skip flag
*
* @param[in] u1_frame_qp
*  Frame qp
*
* @param[in] pi4_num_intra_in_prev_frame
*  Numberf of intra mbs in previous frame
*
* @param[in] pi4_avg_activity
*  Average activity
*
* @returns
*
* @remarks
*
*******************************************************************************
*/
WORD32 ih264e_rc_post_enc(void * ps_rate_control_api,
                          frame_info_t *ps_frame_info,
                          void * ps_pd_frm_rate,
                          void * ps_time_stamp,
                          void * ps_frame_time,
                          WORD32   i4_total_mb_in_frame,
                          picture_type_e *pe_vop_coding_type,
                          WORD32 i4_is_first_frame,
                          WORD32 *pi4_is_post_encode_skip,
                          UWORD8 u1_frame_qp,
                          WORD32 *pi4_num_intra_in_prev_frame,
                          WORD32 *pi4_avg_activity)
{
    /* Variables for the update_frm_level_info */
    WORD32  ai4_tot_mb_in_type[MAX_MB_TYPE];
    WORD32  ai4_tot_mb_type_qp[MAX_MB_TYPE]    = {0, 0};
    WORD32  ai4_mb_type_sad[MAX_MB_TYPE]       = {0, 0};
    WORD32  ai4_mb_type_tex_bits[MAX_MB_TYPE]  = {0, 0};
    WORD32   i4_total_frame_bits               = 0;
    WORD32   i4_total_hdr_bits                 = 0;
    WORD32   i4_total_texturebits;
    WORD32   i4_avg_mb_activity                = 0;
    WORD32   i4_intra_frm_cost                 = 0;
    UWORD8   u1_is_scd                         = 0;
    WORD32  i4_cbr_bits_to_stuff               = 0;
    UWORD32   u4_num_intra_in_prev_frame        = *pi4_num_intra_in_prev_frame;
    UNUSED(ps_pd_frm_rate);
    UNUSED(ps_time_stamp);
    UNUSED(ps_frame_time);
    UNUSED(u1_frame_qp);
    /* Accumulate RC stats */
    ai4_tot_mb_in_type[MB_TYPE_INTRA]    = irc_fi_get_total_mb(ps_frame_info,MB_TYPE_INTRA);
    ai4_tot_mb_in_type[MB_TYPE_INTER]    = irc_fi_get_total_mb(ps_frame_info,MB_TYPE_INTER);
    /* ai4_tot_mb_type_qp[MB_TYPE_INTRA]    = 0;
    ai4_tot_mb_type_qp[MB_TYPE_INTER]    = ps_enc->pu1_h264_mpg2quant[u1_frame_qp] * i4_total_mb_in_frame;*/
    ai4_tot_mb_type_qp[MB_TYPE_INTRA]    = irc_fi_get_total_mb_qp(ps_frame_info,MB_TYPE_INTRA);
    ai4_tot_mb_type_qp[MB_TYPE_INTER]    = irc_fi_get_total_mb_qp(ps_frame_info,MB_TYPE_INTER);
    ai4_mb_type_sad[MB_TYPE_INTRA]       = irc_fi_get_total_mb_sad(ps_frame_info,MB_TYPE_INTRA);
    ai4_mb_type_sad[MB_TYPE_INTER]       = irc_fi_get_total_mb_sad(ps_frame_info,MB_TYPE_INTER);
    i4_intra_frm_cost                    = irc_fi_get_total_intra_mb_cost(ps_frame_info);
    i4_avg_mb_activity                   = irc_fi_get_avg_activity(ps_frame_info);
    i4_total_hdr_bits                    = irc_fi_get_total_header_bits(ps_frame_info);
    i4_total_texturebits                 = irc_fi_get_total_mb_texture_bits(ps_frame_info,MB_TYPE_INTRA);
    i4_total_texturebits                 += irc_fi_get_total_mb_texture_bits(ps_frame_info,MB_TYPE_INTER);
    i4_total_frame_bits                  = i4_total_hdr_bits + i4_total_texturebits ;

    *pi4_avg_activity = i4_avg_mb_activity;


    /* Texture bits are not accumulated. Hence subtracting hdr bits from total bits */
    ai4_mb_type_tex_bits[MB_TYPE_INTRA]  = 0;
    ai4_mb_type_tex_bits[MB_TYPE_INTER]  = i4_total_frame_bits - i4_total_hdr_bits;

    /* Set post encode skip to zero */
    pi4_is_post_encode_skip[0]= 0;

    /* For NLDRC, get the buffer status for stuffing or skipping */
    if (irc_get_rc_type(ps_rate_control_api) == CBR_NLDRC)
    {
        WORD32 i4_get_num_bit_to_prevent_vbv_overflow;
        UWORD8 u1_enc_buf_overflow,u1_enc_buf_underflow;

        /* Getting the buffer status */
        ih264e_rc_get_buffer_status(ps_rate_control_api, i4_total_frame_bits,
            pe_vop_coding_type[0],  &i4_get_num_bit_to_prevent_vbv_overflow,
            &u1_enc_buf_overflow,&u1_enc_buf_underflow);

        /* We skip the frame if decoder buffer is underflowing. But we never skip first I frame */
        // if((u1_enc_buf_overflow == 1) && (i4_is_first_frame != 1))
        if ((u1_enc_buf_overflow == 1) && (i4_is_first_frame != 0))
        {
            irc_post_encode_frame_skip(ps_rate_control_api, (picture_type_e)pe_vop_coding_type[0]);
            // i4_total_frame_bits = imp4_write_skip_frame_header(ps_enc);
            i4_total_frame_bits = 0;

            *pi4_is_post_encode_skip = 1;

            /* Adjust the GOP if in case we skipped an I-frame */
            if (*pe_vop_coding_type == I_PIC)
                irc_force_I_frame(ps_rate_control_api);

            /* Since this frame is skipped by writing 7 bytes header, we say this is a P frame */
            // *pe_vop_coding_type = P;

            /* Getting the buffer status again,to check if it underflows  */
            irc_get_buffer_status(ps_rate_control_api, i4_total_frame_bits,
                (picture_type_e)pe_vop_coding_type[0], &i4_get_num_bit_to_prevent_vbv_overflow);

        }

        /* In this case we stuff bytes as buffer is overflowing */
        if (u1_enc_buf_underflow == 1)
        {
            /* The stuffing function is directly pulled out from split controller workspace.
               encode_vop_data() function makes sure alignment data is dumped at the end of a
               frame. Split controller was identifying this alignment byte, overwriting it with
               the stuff data and then finally aligning the buffer. Here every thing is inside
               the DSP. So, ideally encode_vop_data needn't align, and we can start stuffing directly.
               But in that case, it'll break the logic for a normal frame.
               Hence for simplicity, not changing this part since it is ok to align and
               then overwrite since stuffing is not done for every frame */
            i4_cbr_bits_to_stuff = irc_get_bits_to_stuff(ps_rate_control_api, i4_total_frame_bits, pe_vop_coding_type[0]);

            /* Just add extra 32 bits to make sure we don't stuff lesser */
            i4_cbr_bits_to_stuff += 32;

            /* We can not stuff more than the outbuf size. So have a check here */
            /* Add stuffed bits to total bits */
            i4_total_frame_bits += i4_cbr_bits_to_stuff;
        }
    }

#define ENABLE_SCD 1
#if ENABLE_SCD
    /* If number of intra MBs are more than 2/3rd of total MBs, assume it as a scene change */
    if ((ai4_tot_mb_in_type[MB_TYPE_INTRA] > ((2 * i4_total_mb_in_frame) / 3)) &&
       (*pe_vop_coding_type == P_PIC) &&
       (ai4_tot_mb_in_type[MB_TYPE_INTRA] > ((11 * (WORD32)u4_num_intra_in_prev_frame) / 10)))
    {
        u1_is_scd = 1;
    }
#endif

    /* Update num intra mbs of this frame */
    if (pi4_is_post_encode_skip[0] == 0)
    {
        *pi4_num_intra_in_prev_frame = ai4_tot_mb_in_type[MB_TYPE_INTRA];
    }

    /* Reset intra count to zero, if u encounter an I frame */
    if (*pe_vop_coding_type == I_PIC)
    {
        *pi4_num_intra_in_prev_frame = 0;
    }

    /* Do an update of rate control after post encode */
    irc_update_frame_level_info(ps_rate_control_api,        /* RC state */
                                pe_vop_coding_type[0],      /* PIC type */
                                ai4_mb_type_sad,            /* SAD for [Intra/Inter] */
                                i4_total_frame_bits,        /* Total frame bits */
                                i4_total_hdr_bits,          /* header bits for */
                                ai4_mb_type_tex_bits,       /* for MB[Intra/Inter] */
                                ai4_tot_mb_type_qp,         /* for MB[Intra/Inter] */
                                ai4_tot_mb_in_type,         /* for MB[Intra/Inter] */
                                i4_avg_mb_activity,         /* Average mb activity in frame */
                                u1_is_scd,                  /* Is a scene change detected */
                                0,                          /* Pre encode skip  */
                                (WORD32)i4_intra_frm_cost,  /* Intra cost for frame */
                                0);                         /* Not done outside */

    return (i4_cbr_bits_to_stuff >> 3);
}

/**
*******************************************************************************
*
* @brief Function to update bits consumed info to rate control context
*
* @par Description
*  Function to update bits consume info to rate control context
*
* @param[in] ps_frame_info
*  Frame info context
*
* @param[in] ps_entropy
*  Entropy context
*
* @returns
*  total bits consumed by the frame
*
* @remarks
*
*******************************************************************************
*/
void ih264e_update_rc_bits_info(frame_info_t *ps_frame_info, void *pv_entropy)
{
    entropy_ctxt_t *ps_entropy = pv_entropy;

    ps_frame_info->mb_header_bits[MB_TYPE_INTRA] += ps_entropy->u4_header_bits[MB_TYPE_INTRA];

    ps_frame_info->mb_texture_bits[MB_TYPE_INTRA] += ps_entropy->u4_residue_bits[MB_TYPE_INTRA];

    ps_frame_info->mb_header_bits[MB_TYPE_INTER] += ps_entropy->u4_header_bits[MB_TYPE_INTER];

    ps_frame_info->mb_texture_bits[MB_TYPE_INTER] += ps_entropy->u4_residue_bits[MB_TYPE_INTER];

    return;
}