aboutsummaryrefslogtreecommitdiff
path: root/encoder/ih264e_cabac.c
blob: 9e9add993269a82419543e3c515a313f7065f791 (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
/******************************************************************************
 *
 * 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_cabac.c
*
* @brief
*  Contains all leaf level functions for CABAC entropy coding.
*
* @author
*  ittiam
*
* @par List of Functions:
*  - ih264e_cabac_UEGk0_binarization
*  - ih264e_get_cabac_context
*  - ih264e_cabac_put_byte
*  - ih264e_cabac_encode_bin
*  - ih264e_encode_decision_bins
*  - ih264e_cabac_encode_terminate
*  - ih264e_cabac_encode_bypass_bin
*  - ih264e_cabac_encode_bypass_bins
*
* @remarks
*  none
*
*******************************************************************************
*/

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

/* System Include Files */
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <string.h>

/* User Include Files */
#include "ih264e_config.h"
#include "ih264_typedefs.h"
#include "iv2.h"
#include "ive2.h"

#include "ih264_debug.h"
#include "ih264_macros.h"
#include "ih264_error.h"
#include "ih264_defs.h"
#include "ih264_mem_fns.h"
#include "ih264_padding.h"
#include "ih264_structs.h"
#include "ih264_trans_quant_itrans_iquant.h"
#include "ih264_inter_pred_filters.h"
#include "ih264_intra_pred_filters.h"
#include "ih264_deblk_edge_filters.h"
#include "ih264_cavlc_tables.h"
#include "ih264_cabac_tables.h"
#include "ih264_platform_macros.h"

#include "ime_defs.h"
#include "ime_distortion_metrics.h"
#include "ime_structs.h"

#include "irc_cntrl_param.h"
#include "irc_frame_info_collector.h"

#include "ih264e_error.h"
#include "ih264e_defs.h"
#include "ih264e_bitstream.h"
#include "ih264e_cabac_structs.h"
#include "ih264e_structs.h"
#include "ih264e_encode_header.h"
#include "ih264e_cabac.h"
#include "ih264e_statistics.h"
#include "ih264e_trace.h"


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

/**
*******************************************************************************
*
* @brief
*  k-th order Exp-Golomb (UEGk) binarization process: Implements concatenated
*  unary/ k-th order Exp-Golomb  (UEGk) binarization process, where k = 0 as
*  defined in 9.3.2.3 of  ITU_T_H264-201402
*
* @param[in] i2_sufs
*  Suffix bit string
*
* @param[in] pi1_bins_len
*  Pointer to length of tthe string
*
* @returns Binarized value
*
* @remarks none
*
*******************************************************************************
*/
UWORD32 ih264e_cabac_UEGk0_binarization(WORD16 i2_sufs, WORD8 *pi1_bins_len)
{
    WORD32 unary_length;
    UWORD32 u4_sufs_shiftk_plus1, u4_egk, u4_unary_bins;

    u4_sufs_shiftk_plus1 = i2_sufs + 1;

    unary_length = (32 - CLZ(u4_sufs_shiftk_plus1) + (0 == u4_sufs_shiftk_plus1));

    /* unary code with (unary_length-1) '1's and terminating '0' bin */
    u4_unary_bins = (1 << unary_length) - 2;

    /* insert the symbol prefix of (unary length - 1)  bins */
    u4_egk = (u4_unary_bins << (unary_length - 1))
                    | (u4_sufs_shiftk_plus1 & ((1 << (unary_length - 1)) - 1));

    /* length of the code = 2 *(unary_length - 1) + 1 + k */
    *pi1_bins_len = (2 * unary_length) - 1;

    return (u4_egk);
}

/**
*******************************************************************************
*
* @brief
*  Get cabac context for the MB :calculates the pointers to Top and left
*  cabac neighbor context depending upon neighbor  availability.
*
* @param[in] ps_ent_ctxt
*  Pointer to entropy context structure
*
* @param[in] u4_mb_type
*  Type of MB
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
void ih264e_get_cabac_context(entropy_ctxt_t *ps_ent_ctxt, WORD32 u4_mb_type)
{
    cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
    mb_info_ctxt_t *ps_ctx_inc_mb_map;
    cab_csbp_t *ps_lft_csbp;
    WORD32 i4_lft_avail, i4_top_avail, i4_is_intra;
    WORD32 i4_mb_x, i4_mb_y;
    UWORD8 *pu1_slice_idx = ps_ent_ctxt->pu1_slice_idx;

    i4_is_intra = ((u4_mb_type == I16x16) || (u4_mb_type == I8x8)
                    || (u4_mb_type == I4x4));

    /* derive neighbor availability */
    i4_mb_x = ps_ent_ctxt->i4_mb_x;
    i4_mb_y = ps_ent_ctxt->i4_mb_y;
    pu1_slice_idx += (i4_mb_y * ps_ent_ctxt->i4_wd_mbs);
    /* left macroblock availability */
    i4_lft_avail = (i4_mb_x == 0
                    || (pu1_slice_idx[i4_mb_x - 1] != pu1_slice_idx[i4_mb_x])) ?
                    0 : 1;
    /* top macroblock availability */
    i4_top_avail = (i4_mb_y == 0
                    || (pu1_slice_idx[i4_mb_x - ps_ent_ctxt->i4_wd_mbs]
                                    != pu1_slice_idx[i4_mb_x])) ? 0 : 1;
    i4_mb_x = ps_ent_ctxt->i4_mb_x;
    ps_ctx_inc_mb_map = ps_cabac_ctxt->ps_mb_map_ctxt_inc;
    ps_cabac_ctxt->ps_curr_ctxt_mb_info = ps_ctx_inc_mb_map + i4_mb_x;
    ps_cabac_ctxt->ps_left_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info;
    ps_cabac_ctxt->ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info;
    ps_lft_csbp = ps_cabac_ctxt->ps_lft_csbp;
    ps_cabac_ctxt->pu1_left_y_ac_csbp = &ps_lft_csbp->u1_y_ac_csbp_top_mb;
    ps_cabac_ctxt->pu1_left_uv_ac_csbp = &ps_lft_csbp->u1_uv_ac_csbp_top_mb;
    ps_cabac_ctxt->pu1_left_yuv_dc_csbp = &ps_lft_csbp->u1_yuv_dc_csbp_top_mb;
    ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc =
                    &ps_cabac_ctxt->i1_left_ref_idx_ctx_inc_arr[0][0];
    ps_cabac_ctxt->pu1_left_mv_ctxt_inc =
                    ps_cabac_ctxt->u1_left_mv_ctxt_inc_arr[0];

    if (i4_lft_avail)
        ps_cabac_ctxt->ps_left_ctxt_mb_info =
                        ps_cabac_ctxt->ps_curr_ctxt_mb_info - 1;
    if (i4_top_avail)
        ps_cabac_ctxt->ps_top_ctxt_mb_info =
                        ps_cabac_ctxt->ps_curr_ctxt_mb_info;

    if (!i4_lft_avail)
    {
        UWORD8 u1_def_csbp = i4_is_intra ? 0xf : 0;
        *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = u1_def_csbp;
        *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = u1_def_csbp;
        *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = u1_def_csbp;
        *((UWORD32 *) ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc) = 0;
        memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16);
    }
    if (!i4_top_avail)
    {
        UWORD8 u1_def_csbp = i4_is_intra ? 0xff : 0;
        ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_ac_csbp = u1_def_csbp;
        ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_dc_csbp = u1_def_csbp;
        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[0] =
        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[1] =
        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[2] =
        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[3] = 0;
        memset(ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv, 0, 16);
    }
}

/**
*******************************************************************************
*
* @brief
*  flushing at termination: Explained in flowchart 9-12(ITU_T_H264-201402).
*
* @param[in]   ps_cabac_ctxt
*  pointer to cabac context (handle)
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
IH264E_ERROR_T ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt)
{
    bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm;
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
    UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen;
    UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer;
    UWORD32 u4_out_standing_bytes = ps_cab_enc_env->u4_out_standing_bytes;
    IH264E_ERROR_T status = IH264E_SUCCESS;

    /************************************************************************/
    /* Insert the carry (propogated in previous byte) along with            */
    /* outstanding bytes (if any) and flush remaining bits                  */
    /************************************************************************/
    {
        /* carry = 1 => putbit(1); carry propogated due to L renorm */
        WORD32 carry = (u4_low >> (u4_bits_gen + CABAC_BITS)) & 0x1;
        WORD32 last_byte;
        WORD32 bits_left;
        WORD32 rem_bits;

        if (carry)
        {
            /* CORNER CASE: if the previous data is 0x000003, then EPB will be inserted
             and the data will become 0x00000303 and if the carry is present, it will
             be added with the last byte and it will become 0x00000304 which is not correct
             as per standard */
            /* so check for previous four bytes and if it is equal to 0x00000303
             then subtract u4_strm_buf_offset by 1 */
            if (pu1_strm_buf[ps_stream->u4_strm_buf_offset - 1] == 0x03
                            && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 2] == 0x03
                            && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 3] == 0x00
                            && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 4] == 0x00)
            {
                ps_stream->u4_strm_buf_offset -= 1;
            }
            /* previous byte carry add will not result in overflow to        */
            /* u4_strm_buf_offset - 2 as we track 0xff as outstanding bytes  */
            pu1_strm_buf[ps_stream->u4_strm_buf_offset - 1] += carry;
            ps_stream->i4_zero_bytes_run = 0;
        }

        /*        Insert outstanding bytes (if any)         */
        while (u4_out_standing_bytes)
        {
            UWORD8 u1_0_or_ff = carry ? 0 : 0xFF;

            status |= ih264e_put_byte_epb(ps_stream, u1_0_or_ff);
            u4_out_standing_bytes--;
        }

        /*  clear the carry in low */
        u4_low &= ((1 << (u4_bits_gen + CABAC_BITS)) - 1);

        /* extract the remaining bits;                                   */
        /* includes additional msb bit of low as per Figure 9-12      */
        bits_left = u4_bits_gen + 1;
        rem_bits = (u4_low >> (u4_bits_gen + CABAC_BITS - bits_left));

        if (bits_left >= 8)
        {
            last_byte = (rem_bits >> (bits_left - 8)) & 0xFF;
            status |= ih264e_put_byte_epb(ps_stream, last_byte);
            bits_left -= 8;
        }

        /* insert last byte along with rbsp stop bit(1) and 0's in the end */
        last_byte = (rem_bits << (8 - bits_left))
                        | (1 << (7 - bits_left) | (1 << (7 - bits_left - 1)));
        last_byte &= 0xFF;
        status |= ih264e_put_byte_epb(ps_stream, last_byte);

        if (status == IH264E_SUCCESS) {
            /* update the state variables and return success */
            ps_stream->i4_zero_bytes_run = 0;
            /* Default init values for scratch variables of bitstream context */
            ps_stream->u4_cur_word = 0;
            ps_stream->i4_bits_left_in_cw = WORD_SIZE;
        }

    }
    return status;
}

/**
******************************************************************************
*
* @brief Puts new byte (and outstanding bytes) into bitstream after cabac
*  renormalization
*
* @par   Description
*  1. Extract the leading byte of low(L)
*  2. If leading byte=0xff increment outstanding bytes and return
*     (as the actual bits depend on carry propogation later)
*  3. If leading byte is not 0xff check for any carry propogation
*  4. Insert the carry (propogated in previous byte) along with outstanding
*     bytes (if any) and leading byte
*
* @param[in]   ps_cabac_ctxt
*  pointer to cabac context (handle)
*
* @returns none
*
******************************************************************************
*/
IH264E_ERROR_T ih264e_cabac_put_byte(cabac_ctxt_t *ps_cabac_ctxt)
{
    bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm;
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
    UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen;
    UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer;
    WORD32 lead_byte = u4_low >> (u4_bits_gen + CABAC_BITS - 8);
    IH264E_ERROR_T status = IH264E_SUCCESS;

    /* Sanity checks */
    ASSERT((ps_cab_enc_env->u4_code_int_range >= 256)
                    && (ps_cab_enc_env->u4_code_int_range < 512));
    ASSERT((u4_bits_gen >= 8));

    /* update bits generated and low after extracting leading byte */
    u4_bits_gen -= 8;
    ps_cab_enc_env->u4_code_int_low &= ((1 << (CABAC_BITS + u4_bits_gen)) - 1);
    ps_cab_enc_env->u4_bits_gen = u4_bits_gen;

    /************************************************************************/
    /* 1. Extract the leading byte of low(L)                                */
    /* 2. If leading byte=0xff increment outstanding bytes and return       */
    /*      (as the actual bits depend on carry propogation later)          */
    /* 3. If leading byte is not 0xff check for any carry propogation       */
    /* 4. Insert the carry (propogated in previous byte) along with         */
    /*    outstanding bytes (if any) and leading byte                       */
    /************************************************************************/
    if (lead_byte == 0xff)
    {
        /* actual bits depend on carry propogration     */
        ps_cab_enc_env->u4_out_standing_bytes++;
    }
    else
    {
        /* carry = 1 => putbit(1); carry propogated due to L renorm */
        WORD32 carry = (lead_byte >> 8) & 0x1;
        UWORD32 u4_out_standing_bytes = ps_cab_enc_env->u4_out_standing_bytes;


        /*********************************************************************/
        /*        Insert the carry propogated in previous byte               */
        /*                                                                   */
        /* Note : Do not worry about corruption into slice header align byte */
        /*        This is because the first bin cannot result in overflow    */
        /*********************************************************************/
        if (carry)
        {
            /* CORNER CASE: if the previous data is 0x000003, then EPB will be inserted
             and the data will become 0x00000303 and if the carry is present, it will
             be added with the last byte and it will become 0x00000304 which is not correct
             as per standard */
            /* so check for previous four bytes and if it is equal to 0x00000303
             then subtract u4_strm_buf_offset by 1 */
            if (pu1_strm_buf[ps_stream->u4_strm_buf_offset - 1] == 0x03
                            && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 2] == 0x03
                            && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 3] == 0x00
                            && pu1_strm_buf[ps_stream->u4_strm_buf_offset - 4] == 0x00)
            {
                ps_stream->u4_strm_buf_offset -= 1;
            }
            /* previous byte carry add will not result in overflow to        */
            /* u4_strm_buf_offset - 2 as we track 0xff as outstanding bytes  */
            pu1_strm_buf[ps_stream->u4_strm_buf_offset - 1] += carry;
            ps_stream->i4_zero_bytes_run = 0;
        }

        /*        Insert outstanding bytes (if any)         */
        while (u4_out_standing_bytes)
        {
            UWORD8 u1_0_or_ff = carry ? 0 : 0xFF;

            status |= ih264e_put_byte_epb(ps_stream, u1_0_or_ff);

            u4_out_standing_bytes--;
        }
        ps_cab_enc_env->u4_out_standing_bytes = 0;

        /*        Insert the leading byte                   */
        lead_byte &= 0xFF;
        status |= ih264e_put_byte_epb(ps_stream, lead_byte);
    }
    return status;
}

/**
******************************************************************************
*
* @brief Codes a bin based on probablilty and mps packed context model
*
* @par   Description
*  1. Apart from encoding bin, context model is updated as per state transition
*  2. Range and Low renormalization is done based on bin and original state
*  3. After renorm bistream is updated (if required)
*
* @param[in]   ps_cabac
*  pointer to cabac context (handle)
*
* @param[in]   bin
*  bin(boolean) to be encoded
*
* @param[in]  pu1_bin_ctxts
*  index of cabac context model containing pState[bits 5-0] | MPS[bit6]
*
*  @return none
*
******************************************************************************
*/
void ih264e_cabac_encode_bin(cabac_ctxt_t *ps_cabac, WORD32 bin,
                             bin_ctxt_model *pu1_bin_ctxts)
{
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
    UWORD32 u4_rlps;
    UWORD8 state_mps = (*pu1_bin_ctxts) & 0x3F;
    UWORD8 u1_mps = !!((*pu1_bin_ctxts) & (0x40));
    WORD32 shift;
    UWORD32 u4_table_val;

    /* Sanity checks */
    ASSERT((bin == 0) || (bin == 1));
    ASSERT((u4_range >= 256) && (u4_range < 512));

    /* Get the lps range from LUT based on quantized range and state */
    u4_table_val= gau4_ih264_cabac_table[state_mps][(u4_range >> 6) & 0x3];
    u4_rlps = u4_table_val & 0xFF;
    u4_range -= u4_rlps;

    /* check if bin is mps or lps */
    if (u1_mps ^ bin)
    {
        /* lps path;  L= L + R; R = RLPS */
        u4_low += u4_range;
        u4_range = u4_rlps;
        if (state_mps == 0)
        {
            /* MPS(CtxIdx) = 1 - MPS(CtxIdx) */
            u1_mps = 1 - u1_mps;
        } /* update the context model from state transition LUT */

        state_mps =  (u4_table_val >> 15) & 0x3F;
    }
    else
    { /* update the context model from state transition LUT */
        state_mps =  (u4_table_val >> 8) & 0x3F;
    }

    (*pu1_bin_ctxts) = (u1_mps << 6) | state_mps;

    /*****************************************************************/
    /* Renormalization; calculate bits generated based on range(R)   */
    /* Note : 6 <= R < 512; R is 2 only for terminating encode       */
    /*****************************************************************/
    GETRANGE(shift, u4_range);
    shift   = 9 - shift;
    u4_low   <<= shift;
    u4_range <<= shift;

    /* bits to be inserted in the bitstream */
    ps_cab_enc_env->u4_bits_gen += shift;
    ps_cab_enc_env->u4_code_int_range = u4_range;
    ps_cab_enc_env->u4_code_int_low   = u4_low;

    /* generate stream when a byte is ready */
    if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
    {
        ih264e_cabac_put_byte(ps_cabac);
    }
}

/**
*******************************************************************************
*
* @brief Encoding process for a binary decision: implements encoding process of
*  a decision as defined in 9.3.4.2. This function encodes multiple bins, of a
*  symbol. Implements flowchart Figure 9-7( ITU_T_H264-201402)
*
* @param[in] u4_bins
*  array of bin values
*
* @param[in] i1_bins_len
*  Length of bins, maximum 32
*
* @param[in] u4_ctx_inc
*  CtxInc, byte0- bin0, byte1-bin1 ..
*
* @param[in] i1_valid_len
*  valid length of bins, after that CtxInc is constant
*
* @param[in] pu1_bin_ctxt_type
*  Pointer to binary contexts
*
* @param[in] ps_cabac
*  Pointer to cabac_context_structure
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
void ih264e_encode_decision_bins(UWORD32 u4_bins, WORD8 i1_bins_len,
                                 UWORD32 u4_ctx_inc, WORD8 i1_valid_len,
                                 bin_ctxt_model *pu1_bin_ctxt_type,
                                 cabac_ctxt_t *ps_cabac)
{
    WORD8 i;
    UWORD8 u1_ctx_inc, u1_bin;

    for (i = 0; i < i1_bins_len; i++)
    {
        u1_bin = (u4_bins & 0x01);
        u4_bins = u4_bins >> 1;
        u1_ctx_inc = u4_ctx_inc & 0x0f;
        if (i < i1_valid_len)
            u4_ctx_inc = u4_ctx_inc >> 4;
        /* Encode the bin */
        ih264e_cabac_encode_bin(ps_cabac, u1_bin,
                                pu1_bin_ctxt_type + u1_ctx_inc);
    }
}

/**
*******************************************************************************
* @brief
*  Encoding process for a binary decision before termination:Encoding process
*  of a termination(9.3.4.5:ITU_T_H264-201402). Explained in flowchart 9-11.
*
* @param[in] ps_cabac
*  Pointer to cabac structure
*
* @param[in] term_bin
*  Symbol value, end of slice or not, term_bin is binary
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
void ih264e_cabac_encode_terminate(cabac_ctxt_t *ps_cabac, WORD32 term_bin)
{
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
    UWORD32 u4_rlps;
    WORD32 shift;

    /* Sanity checks */
    ASSERT((u4_range >= 256) && (u4_range < 512));
    ASSERT((term_bin == 0) || (term_bin == 1));

    /*  term_bin = 1 has lps range = 2 */
    u4_rlps = 2;
    u4_range -= u4_rlps;

    /* if terminate L is incremented by curR and R=2 */
    if (term_bin)
    {
        /* lps path;  L= L + R; R = RLPS */
        u4_low += u4_range;
        u4_range = u4_rlps;
    }

    /*****************************************************************/
    /* Renormalization; calculate bits generated based on range(R)   */
    /* Note : 6 <= R < 512; R is 2 only for terminating encode       */
    /*****************************************************************/
    GETRANGE(shift, u4_range);
    shift = 9 - shift;
    u4_low <<= shift;
    u4_range <<= shift;

    /* bits to be inserted in the bitstream */
    ps_cab_enc_env->u4_bits_gen += shift;
    ps_cab_enc_env->u4_code_int_range = u4_range;
    ps_cab_enc_env->u4_code_int_low = u4_low;

    /* generate stream when a byte is ready */
    if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
    {
        ih264e_cabac_put_byte(ps_cabac);
    }

    if (term_bin)
    {
        ih264e_cabac_flush(ps_cabac);
    }
}

/**
*******************************************************************************
* @brief Bypass encoding process for binary decisions.
*  Explained (9.3.4.4 :ITU_T_H264-201402), flowchart 9-10.
*
* @param[ino]  ps_cabac
*  pointer to cabac context (handle)
*
* @param[in]  bin
*  bypass bin(0/1) to be encoded
*
* @returns none
*
* @remarks none
*
*******************************************************************************
*/
void ih264e_cabac_encode_bypass_bin(cabac_ctxt_t *ps_cabac, WORD32 bin)
{
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;

    /* Sanity checks */
    ASSERT((u4_range >= 256) && (u4_range < 512));
    ASSERT((bin == 0) || (bin == 1));

    u4_low <<= 1;
    /* add range if bin is 1 */
    if (bin)
    {
        u4_low += u4_range;
    }

    /* 1 bit to be inserted in the bitstream */
    ps_cab_enc_env->u4_bits_gen++;
    ps_cab_enc_env->u4_code_int_low = u4_low;

    /* generate stream when a byte is ready */
    if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
    {
        ih264e_cabac_put_byte(ps_cabac);
    }
}

/**
******************************************************************************
*
* @brief Encodes a series of bypass bins (FLC bypass bins)
*
* @par   Description
*  This function is more optimal than calling ih264e_cabac_encode_bypass_bin()
*  in a loop as cabac low, renorm and generating the stream (8bins at a time)
*  can be done in one operation
*
* @param[inout]ps_cabac
*  pointer to cabac context (handle)
*
* @param[in]   u4_bins
*  syntax element to be coded (as FLC bins)
*
* @param[in]   num_bins
*  This is the FLC length for u4_sym
*
* @return none
*
******************************************************************************
*/
void ih264e_cabac_encode_bypass_bins(cabac_ctxt_t *ps_cabac, UWORD32 u4_bins,
                                     WORD32 num_bins)
{
    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
    WORD32 next_byte;

    /* Sanity checks */
    ASSERT((num_bins < 33) && (num_bins > 0));
    ASSERT((u4_range >= 256) && (u4_range < 512));

    /* Compute bit always to populate the trace */
    /* increment bits generated by num_bins */

    /* Encode 8bins at a time and put in the bit-stream */
    while (num_bins > 8)
    {
        num_bins -= 8;

        next_byte = (u4_bins >> (num_bins)) & 0xff;

        /*  L = (L << 8) +  (R * next_byte) */
        ps_cab_enc_env->u4_code_int_low <<= 8;
        ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
        ps_cab_enc_env->u4_bits_gen += 8;

        if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
        {
            /*  insert the leading byte of low into stream */
            ih264e_cabac_put_byte(ps_cabac);
        }
    }

    /* Update low with remaining bins and return */
    next_byte = (u4_bins & ((1 << num_bins) - 1));

    ps_cab_enc_env->u4_code_int_low <<= num_bins;
    ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
    ps_cab_enc_env->u4_bits_gen += num_bins;

    if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
    {
        /*  insert the leading byte of low into stream */
        ih264e_cabac_put_byte(ps_cabac);
    }
}