ih264e_cabac.c revision 0574be65f4b2376e32bdf979650d8e5f168b68b7
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*******************************************************************************
23* @file
24*  ih264e_cabac.c
25*
26* @brief
27*  Contains all leaf level functions for CABAC entropy coding.
28*
29*
30* @author
31* Doney Alex
32*
33* @par List of Functions:
34*
35*
36* @remarks
37*  None
38*
39*******************************************************************************
40*/
41
42/*****************************************************************************/
43/* File Includes                                                             */
44/*****************************************************************************/
45
46/* System include files */
47#include <stdio.h>
48#include <assert.h>
49#include <limits.h>
50#include <string.h>
51
52/* User include files */
53#include "ih264e_config.h"
54#include "ih264_typedefs.h"
55#include "iv2.h"
56#include "ive2.h"
57#include "ih264_debug.h"
58#include "ih264_defs.h"
59#include "ih264e_defs.h"
60#include "ih264_macros.h"
61#include "ih264e_error.h"
62#include "ih264e_bitstream.h"
63#include "ime_distortion_metrics.h"
64#include "ime_defs.h"
65#include "ime_structs.h"
66#include "ih264_error.h"
67#include "ih264_structs.h"
68#include "ih264_trans_quant_itrans_iquant.h"
69#include "ih264_inter_pred_filters.h"
70#include "ih264_mem_fns.h"
71#include "ih264_padding.h"
72#include "ih264_platform_macros.h"
73#include "ih264_intra_pred_filters.h"
74#include "ih264_deblk_edge_filters.h"
75#include "ih264_cabac_tables.h"
76#include "irc_cntrl_param.h"
77#include "irc_frame_info_collector.h"
78#include "ih264e_rate_control.h"
79#include "ih264e_cabac_structs.h"
80#include "ih264e_structs.h"
81#include "ih264e_cabac.h"
82#include "ih264e_encode_header.h"
83#include "ih264_cavlc_tables.h"
84#include "ih264e_statistics.h"
85#include "ih264e_trace.h"
86
87
88/*****************************************************************************/
89/* Function Definitions                                                      */
90/*****************************************************************************/
91
92
93/**
94 *******************************************************************************
95 *
96 * @brief
97 *  k-th order Exp-Golomb (UEGk) binarization process: Implements concatenated
98 *   unary/ k-th order Exp-Golomb  (UEGk) binarization process,
99 *   where k = 0 as defined in 9.3.2.3 of  ITU_T_H264-201402
100 *
101 * @param[in] i2_sufs
102 *  Suffix bit string
103 *
104 * @param[in] pi1_bins_len
105 *  Pointer to length of tthe string
106 *
107 * @returns Binarized value
108 *
109 * @remarks
110 *  None
111 *
112 *******************************************************************************
113 */
114
115UWORD32 ih264e_cabac_UEGk0_binarization(WORD16 i2_sufs, WORD8 *pi1_bins_len)
116{
117    WORD32 unary_length;
118    UWORD32 u4_sufs_shiftk_plus1, u4_egk, u4_unary_bins;
119
120    u4_sufs_shiftk_plus1 = i2_sufs + 1;
121
122    unary_length = (32 - CLZ(u4_sufs_shiftk_plus1) + (0 == u4_sufs_shiftk_plus1));
123
124    /* unary code with (unary_length-1) '1's and terminating '0' bin */
125    u4_unary_bins = (1 << unary_length) - 2;
126
127    /* insert the symbol prefix of (unary length - 1)  bins */
128    u4_egk = (u4_unary_bins << (unary_length - 1))
129                    | (u4_sufs_shiftk_plus1 & ((1 << (unary_length - 1)) - 1));
130
131    /* length of the code = 2 *(unary_length - 1) + 1 + k */
132    *pi1_bins_len = (2 * unary_length) - 1;
133
134    return (u4_egk);
135}
136
137/**
138 *******************************************************************************
139 *
140 * @brief
141 *  Get cabac context for the MB :calculates the pointers to Top and   left
142 *          cabac neighbor context depending upon neighbor  availability.
143 *
144 * @param[in] ps_ent_ctxt
145 *  Pointer to entropy context structure
146 *
147 * @param[in] u4_mb_type
148 *  Type of MB
149 *
150 * @returns
151 *
152 * @remarks
153 *  None
154 *
155 *******************************************************************************
156 */
157void ih264e_get_cabac_context(entropy_ctxt_t *ps_ent_ctxt, WORD32 u4_mb_type)
158{
159
160    /* CABAC context */
161    cabac_ctxt_t *ps_cabac_ctxt = ps_ent_ctxt->ps_cabac;
162    mb_info_ctxt_t *ps_ctx_inc_mb_map;
163    cab_csbp_t *ps_lft_csbp;
164
165    WORD32 i4_lft_avail, i4_top_avail, i4_is_intra;
166    WORD32 i4_mb_x, i4_mb_y;
167    UWORD8 *pu1_slice_idx = ps_ent_ctxt->pu1_slice_idx;
168
169    i4_is_intra = ((u4_mb_type == I16x16) || (u4_mb_type == I8x8)
170                    || (u4_mb_type == I4x4));
171
172    /* derive neighbor availability */
173    i4_mb_x = ps_ent_ctxt->i4_mb_x;
174    i4_mb_y = ps_ent_ctxt->i4_mb_y;
175    pu1_slice_idx += (i4_mb_y * ps_ent_ctxt->i4_wd_mbs);
176    /* left macroblock availability */
177    i4_lft_avail = (i4_mb_x == 0
178                    || (pu1_slice_idx[i4_mb_x - 1] != pu1_slice_idx[i4_mb_x])) ?
179                    0 : 1;
180    /* top macroblock availability */
181    i4_top_avail = (i4_mb_y == 0
182                    || (pu1_slice_idx[i4_mb_x - ps_ent_ctxt->i4_wd_mbs]
183                                    != pu1_slice_idx[i4_mb_x])) ? 0 : 1;
184    i4_mb_x = ps_ent_ctxt->i4_mb_x;
185    ps_ctx_inc_mb_map = ps_cabac_ctxt->ps_mb_map_ctxt_inc;
186    ps_cabac_ctxt->ps_curr_ctxt_mb_info = ps_ctx_inc_mb_map + i4_mb_x;
187    ps_cabac_ctxt->ps_left_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info;
188    ps_cabac_ctxt->ps_top_ctxt_mb_info = ps_cabac_ctxt->ps_def_ctxt_mb_info;
189    ps_lft_csbp = ps_cabac_ctxt->ps_lft_csbp;
190    ps_cabac_ctxt->pu1_left_y_ac_csbp = &ps_lft_csbp->u1_y_ac_csbp_top_mb;
191    ps_cabac_ctxt->pu1_left_uv_ac_csbp = &ps_lft_csbp->u1_uv_ac_csbp_top_mb;
192    ps_cabac_ctxt->pu1_left_yuv_dc_csbp = &ps_lft_csbp->u1_yuv_dc_csbp_top_mb;
193    ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc =
194                    &ps_cabac_ctxt->i1_left_ref_idx_ctx_inc_arr[0][0];
195    ps_cabac_ctxt->pu1_left_mv_ctxt_inc =
196                    ps_cabac_ctxt->u1_left_mv_ctxt_inc_arr[0];
197
198    if (i4_lft_avail)
199        ps_cabac_ctxt->ps_left_ctxt_mb_info =
200                        ps_cabac_ctxt->ps_curr_ctxt_mb_info - 1;
201    if (i4_top_avail)
202        ps_cabac_ctxt->ps_top_ctxt_mb_info =
203                        ps_cabac_ctxt->ps_curr_ctxt_mb_info;
204
205    if (!i4_lft_avail)
206    {
207        UWORD8 u1_def_csbp = i4_is_intra ? 0xf : 0;
208        *(ps_cabac_ctxt->pu1_left_y_ac_csbp) = u1_def_csbp;
209        *(ps_cabac_ctxt->pu1_left_uv_ac_csbp) = u1_def_csbp;
210        *(ps_cabac_ctxt->pu1_left_yuv_dc_csbp) = u1_def_csbp;
211        *((UWORD32 *) ps_cabac_ctxt->pi1_left_ref_idx_ctxt_inc) = 0;
212        memset(ps_cabac_ctxt->pu1_left_mv_ctxt_inc, 0, 16);
213    }
214    if (!i4_top_avail)
215    {
216        UWORD8 u1_def_csbp = i4_is_intra ? 0xff : 0;
217        ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_ac_csbp = u1_def_csbp;
218        ps_cabac_ctxt->ps_top_ctxt_mb_info->u1_yuv_dc_csbp = u1_def_csbp;
219        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[0] =
220        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[1] =
221        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[2] =
222        ps_cabac_ctxt->ps_curr_ctxt_mb_info->i1_ref_idx[3] = 0;
223        memset(ps_cabac_ctxt->ps_curr_ctxt_mb_info->u1_mv, 0, 16);
224    }
225
226}
227
228
229
230/**
231 *******************************************************************************
232 * @brief
233 *  flushing at termination: Explained in flowchart 9-12(ITU_T_H264-201402).
234 *
235 *  @param[in]   ps_cabac_ctxt
236 *  pointer to cabac context (handle)
237 *
238 * @returns  none
239 *
240 * @remarks
241 *  None
242 *
243 *******************************************************************************
244 */
245void ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt)
246{
247
248    /* bit stream ptr */
249    bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm;
250    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
251    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
252    UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen;
253    UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer;
254    UWORD32 u4_strm_buf_offset = ps_stream->u4_strm_buf_offset;
255    WORD32 zero_run = ps_stream->i4_zero_bytes_run;
256    UWORD32 u4_out_standing_bytes = ps_cab_enc_env->u4_out_standing_bytes;
257
258    /************************************************************************/
259    /* Insert the carry (propogated in previous byte) along with            */
260    /* outstanding bytes (if any) and flush remaining bits                  */
261    /************************************************************************/
262    {
263        /* carry = 1 => putbit(1); carry propogated due to L renorm */
264        WORD32 carry = (u4_low >> (u4_bits_gen + CABAC_BITS)) & 0x1;
265        WORD32 last_byte;
266        WORD32 bits_left;
267        WORD32 rem_bits;
268
269        if (carry)
270        {
271            /* CORNER CASE: if the previous data is 0x000003, then EPB will be inserted
272             and the data will become 0x00000303 and if the carry is present, it will
273             be added with the last byte and it will become 0x00000304 which is not correct
274             as per standard */
275            /* so check for previous four bytes and if it is equal to 0x00000303
276             then subtract u4_strm_buf_offset by 1 */
277            if (pu1_strm_buf[u4_strm_buf_offset - 1] == 0x03
278                            && pu1_strm_buf[u4_strm_buf_offset - 2] == 0x03
279                            && pu1_strm_buf[u4_strm_buf_offset - 3] == 0x00
280                            && pu1_strm_buf[u4_strm_buf_offset - 4] == 0x00)
281            {
282                u4_strm_buf_offset -= 1;
283            }
284            /* previous byte carry add will not result in overflow to        */
285            /* u4_strm_buf_offset - 2 as we track 0xff as outstanding bytes  */
286            pu1_strm_buf[u4_strm_buf_offset - 1] += carry;
287            zero_run = 0;
288        }
289
290        /*        Insert outstanding bytes (if any)         */
291        while (u4_out_standing_bytes)
292        {
293            UWORD8 u1_0_or_ff = carry ? 0 : 0xFF;
294
295            PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, u1_0_or_ff, zero_run);
296            u4_out_standing_bytes--;
297        }
298
299        /*  clear the carry in low */
300        u4_low &= ((1 << (u4_bits_gen + CABAC_BITS)) - 1);
301
302        /* extract the remaining bits;                                   */
303        /* includes additional msb bit of low as per Figure 9-12      */
304        bits_left = u4_bits_gen + 1;
305        rem_bits = (u4_low >> (u4_bits_gen + CABAC_BITS - bits_left));
306
307        if (bits_left >= 8)
308        {
309            last_byte = (rem_bits >> (bits_left - 8)) & 0xFF;
310            PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, last_byte, zero_run);
311            bits_left -= 8;
312        }
313
314        /* insert last byte along with rbsp stop bit(1) and 0's in the end */
315        last_byte = (rem_bits << (8 - bits_left))
316                        | (1 << (7 - bits_left) | (1 << (7 - bits_left - 1)));
317        last_byte &= 0xFF;
318        PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, last_byte, zero_run);
319
320        /* update the state variables and return success */
321        ps_stream->u4_strm_buf_offset = u4_strm_buf_offset;
322        ps_stream->i4_zero_bytes_run = 0;
323        /* Default init values for scratch variables of bitstream context */
324        ps_stream->u4_cur_word = 0;
325        ps_stream->i4_bits_left_in_cw = WORD_SIZE;
326
327    }
328}
329
330/**
331 ******************************************************************************
332 *
333 *  @brief Puts new byte (and outstanding bytes) into bitstream after cabac
334 *         renormalization
335 *
336 *  @par   Description
337 *  1. Extract the leading byte of low(L)
338 *  2. If leading byte=0xff increment outstanding bytes and return
339 *     (as the actual bits depend on carry propogation later)
340 *  3. If leading byte is not 0xff check for any carry propogation
341 *  4. Insert the carry (propogated in previous byte) along with outstanding
342 *     bytes (if any) and leading byte
343 *
344 *
345 *  @param[in]   ps_cabac_ctxt
346 *  pointer to cabac context (handle)
347 *
348 *  @return
349 *
350 ******************************************************************************
351 */
352void ih264e_cabac_put_byte(cabac_ctxt_t *ps_cabac_ctxt)
353{
354
355    /* bit stream ptr */
356    bitstrm_t *ps_stream = ps_cabac_ctxt->ps_bitstrm;
357    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac_ctxt->s_cab_enc_env);
358    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
359    UWORD32 u4_bits_gen = ps_cab_enc_env->u4_bits_gen;
360    WORD32 lead_byte = u4_low >> (u4_bits_gen + CABAC_BITS - 8);
361
362    /* Sanity checks */
363    ASSERT((ps_cab_enc_env->u4_code_int_range >= 256)
364                    && (ps_cab_enc_env->u4_code_int_range < 512));
365    ASSERT((u4_bits_gen >= 8));
366
367    /* update bits generated and low after extracting leading byte */
368    u4_bits_gen -= 8;
369    ps_cab_enc_env->u4_code_int_low &= ((1 << (CABAC_BITS + u4_bits_gen)) - 1);
370    ps_cab_enc_env->u4_bits_gen = u4_bits_gen;
371
372    /************************************************************************/
373    /* 1. Extract the leading byte of low(L)                                */
374    /* 2. If leading byte=0xff increment outstanding bytes and return       */
375    /*      (as the actual bits depend on carry propogation later)          */
376    /* 3. If leading byte is not 0xff check for any carry propogation       */
377    /* 4. Insert the carry (propogated in previous byte) along with         */
378    /*    outstanding bytes (if any) and leading byte                       */
379    /************************************************************************/
380    if (lead_byte == 0xff)
381    {
382        /* actual bits depend on carry propogration     */
383        ps_cab_enc_env->u4_out_standing_bytes++;
384        return ;
385    }
386    else
387    {
388        /* carry = 1 => putbit(1); carry propogated due to L renorm */
389        WORD32 carry = (lead_byte >> 8) & 0x1;
390        UWORD8 *pu1_strm_buf = ps_stream->pu1_strm_buffer;
391        UWORD32 u4_strm_buf_offset = ps_stream->u4_strm_buf_offset;
392        WORD32 zero_run = ps_stream->i4_zero_bytes_run;
393        UWORD32 u4_out_standing_bytes = ps_cab_enc_env->u4_out_standing_bytes;
394
395
396        /*********************************************************************/
397        /*        Insert the carry propogated in previous byte               */
398        /*                                                                   */
399        /* Note : Do not worry about corruption into slice header align byte */
400        /*        This is because the first bin cannot result in overflow    */
401        /*********************************************************************/
402        if (carry)
403        {
404            /* CORNER CASE: if the previous data is 0x000003, then EPB will be inserted
405             and the data will become 0x00000303 and if the carry is present, it will
406             be added with the last byte and it will become 0x00000304 which is not correct
407             as per standard */
408            /* so check for previous four bytes and if it is equal to 0x00000303
409             then subtract u4_strm_buf_offset by 1 */
410            if (pu1_strm_buf[u4_strm_buf_offset - 1] == 0x03
411                            && pu1_strm_buf[u4_strm_buf_offset - 2] == 0x03
412                            && pu1_strm_buf[u4_strm_buf_offset - 3] == 0x00
413                            && pu1_strm_buf[u4_strm_buf_offset - 4] == 0x00)
414            {
415                u4_strm_buf_offset -= 1;
416            }
417            /* previous byte carry add will not result in overflow to        */
418            /* u4_strm_buf_offset - 2 as we track 0xff as outstanding bytes  */
419            pu1_strm_buf[u4_strm_buf_offset - 1] += carry;
420            zero_run = 0;
421        }
422
423        /*        Insert outstanding bytes (if any)         */
424        while (u4_out_standing_bytes)
425        {
426            UWORD8 u1_0_or_ff = carry ? 0 : 0xFF;
427
428            PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, u1_0_or_ff, zero_run);
429
430            u4_out_standing_bytes--;
431        }
432        ps_cab_enc_env->u4_out_standing_bytes = 0;
433
434        /*        Insert the leading byte                   */
435        lead_byte &= 0xFF;
436        PUTBYTE_EPB(pu1_strm_buf, u4_strm_buf_offset, lead_byte, zero_run);
437
438        /* update the state variables and return success */
439        ps_stream->u4_strm_buf_offset = u4_strm_buf_offset;
440        ps_stream->i4_zero_bytes_run = zero_run;
441
442    }
443}
444
445
446
447
448 /**
449 ******************************************************************************
450 *
451 *  @brief Codes a bin based on probablilty and mps packed context model
452 *
453 *  @par   Description
454 *  1. Apart from encoding bin, context model is updated as per state transition
455 *  2. Range and Low renormalization is done based on bin and original state
456 *  3. After renorm bistream is updated (if required)
457 *
458 *  @param[in]   ps_cabac
459 *  pointer to cabac context (handle)
460 *
461 *  @param[in]   bin
462 *  bin(boolean) to be encoded
463 *
464 *  @param[in]  pu1_bin_ctxts
465 *  index of cabac context model containing pState[bits 5-0] | MPS[bit6]
466 *
467 *  @return
468 *
469 ******************************************************************************
470  */
471void ih264e_cabac_encode_bin(cabac_ctxt_t *ps_cabac, WORD32 bin,
472                             bin_ctxt_model *pu1_bin_ctxts)
473{
474
475    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
476    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
477    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
478    UWORD32 u4_rlps;
479    UWORD8 state_mps = (*pu1_bin_ctxts) & 0x3F;
480    UWORD8 u1_mps = !!((*pu1_bin_ctxts) & (0x40));
481    WORD32 shift;
482    UWORD32 u4_table_val;
483    /* Sanity checks */
484    ASSERT((bin == 0) || (bin == 1));
485    ASSERT((u4_range >= 256) && (u4_range < 512));
486
487    /* Get the lps range from LUT based on quantized range and state */
488    u4_table_val= gau4_ih264_cabac_table[state_mps][(u4_range >> 6) & 0x3];
489    u4_rlps = u4_table_val & 0xFF;
490    u4_range -= u4_rlps;
491
492    /* check if bin is mps or lps */
493    if (u1_mps ^ bin)
494    {
495        /* lps path;  L= L + R; R = RLPS */
496        u4_low += u4_range;
497        u4_range = u4_rlps;
498        if (state_mps == 0)
499        {
500            /* MPS(CtxIdx) = 1 - MPS(CtxIdx) */
501            u1_mps = 1 - u1_mps;
502        } /* update the context model from state transition LUT */
503
504        state_mps =  (u4_table_val >> 15) & 0x3F;
505    }
506    else
507    { /* update the context model from state transition LUT */
508        state_mps =  (u4_table_val >> 8) & 0x3F;
509    }
510
511    (*pu1_bin_ctxts) = (u1_mps << 6) | state_mps;
512
513        /*****************************************************************/
514        /* Renormalization; calculate bits generated based on range(R)   */
515        /* Note : 6 <= R < 512; R is 2 only for terminating encode       */
516        /*****************************************************************/
517        GETRANGE(shift, u4_range);
518        shift   = 9 - shift;
519        u4_low   <<= shift;
520        u4_range <<= shift;
521
522        /* bits to be inserted in the bitstream */
523        ps_cab_enc_env->u4_bits_gen += shift;
524        ps_cab_enc_env->u4_code_int_range = u4_range;
525        ps_cab_enc_env->u4_code_int_low   = u4_low;
526
527        /* generate stream when a byte is ready */
528        if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
529        {
530            ih264e_cabac_put_byte(ps_cabac);
531        }
532
533}
534
535
536
537
538 /**
539 *******************************************************************************
540 *
541 * @brief
542 *  Encoding process for a binary decision :implements encoding process of a decision
543 *  as defined in 9.3.4.2 . This function encodes multiple bins, of a symbol. Implements
544 *  flowchart Figure 9-7( ITU_T_H264-201402)
545 *
546 * @param[in] u4_bins
547 * array of bin values
548 *
549 * @param[in] i1_bins_len
550 *  Length of bins, maximum 32
551 *
552 * @param[in] u4_ctx_inc
553 *  CtxInc, byte0- bin0, byte1-bin1 ..
554 *
555 * @param[in] i1_valid_len
556 *  valid length of bins, after that CtxInc is constant
557 *
558 * @param[in] pu1_bin_ctxt_type
559 *  Pointer to binary contexts
560
561 * @param[in] ps_cabac
562 *  Pointer to cabac_context_structure
563 *
564 * @returns
565 *
566 * @remarks
567 *  None
568 *
569 *******************************************************************************
570 */
571void ih264e_encode_decision_bins(UWORD32 u4_bins, WORD8 i1_bins_len,
572                                 UWORD32 u4_ctx_inc, WORD8 i1_valid_len,
573                                 bin_ctxt_model *pu1_bin_ctxt_type,
574                                 cabac_ctxt_t *ps_cabac)
575{
576    WORD8 i;
577    UWORD8 u1_ctx_inc, u1_bin;
578
579    for (i = 0; i < i1_bins_len; i++)
580    {
581        u1_bin = (u4_bins & 0x01);
582        u4_bins = u4_bins >> 1;
583        u1_ctx_inc = u4_ctx_inc & 0x0f;
584        if (i < i1_valid_len)
585            u4_ctx_inc = u4_ctx_inc >> 4;
586        /* Encode the bin */
587        ih264e_cabac_encode_bin(ps_cabac, u1_bin,
588                                pu1_bin_ctxt_type + u1_ctx_inc);
589    }
590
591}
592
593
594
595
596
597
598/**
599 *******************************************************************************
600 * @brief
601 *  Encoding process for a binary decision before termination:Encoding process
602 *  of a termination(9.3.4.5 :ITU_T_H264-201402) . Explained in flowchart 9-11.
603 *
604 * @param[in] ps_cabac
605 *  Pointer to cabac structure
606 *
607 * @param[in] term_bin
608 *  Symbol value, end of slice or not, term_bin is binary
609 *
610 * @returns
611 *
612 * @remarks
613 *  None
614 *
615 *******************************************************************************
616 */
617void ih264e_cabac_encode_terminate(cabac_ctxt_t *ps_cabac, WORD32 term_bin)
618{
619
620    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
621
622    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
623    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
624    UWORD32 u4_rlps;
625    WORD32 shift;
626
627    /* Sanity checks */
628    ASSERT((u4_range >= 256) && (u4_range < 512));
629    ASSERT((term_bin == 0) || (term_bin == 1));
630
631    /*  term_bin = 1 has lps range = 2 */
632    u4_rlps = 2;
633    u4_range -= u4_rlps;
634
635    /* if terminate L is incremented by curR and R=2 */
636    if (term_bin)
637    {
638        /* lps path;  L= L + R; R = RLPS */
639        u4_low += u4_range;
640        u4_range = u4_rlps;
641    }
642
643    /*****************************************************************/
644    /* Renormalization; calculate bits generated based on range(R)   */
645    /* Note : 6 <= R < 512; R is 2 only for terminating encode       */
646    /*****************************************************************/
647    GETRANGE(shift, u4_range);
648    shift = 9 - shift;
649    u4_low <<= shift;
650    u4_range <<= shift;
651
652    /* bits to be inserted in the bitstream */
653    ps_cab_enc_env->u4_bits_gen += shift;
654    ps_cab_enc_env->u4_code_int_range = u4_range;
655    ps_cab_enc_env->u4_code_int_low = u4_low;
656
657    /* generate stream when a byte is ready */
658    if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
659    {
660        ih264e_cabac_put_byte(ps_cabac);
661    }
662
663    if (term_bin)
664    {
665        ih264e_cabac_flush(ps_cabac);
666    }
667
668}
669
670
671/**
672 *******************************************************************************
673 * @brief
674 * Bypass encoding process for binary decisions:  Explained (9.3.4.4 :ITU_T_H264-201402)
675 * , flowchart 9-10.
676 *
677 *  @param[ino]  ps_cabac : pointer to cabac context (handle)
678 *
679 *  @param[in]   bin :  bypass bin(0/1) to be encoded
680 *
681 *  @returns
682 *
683 *  @remarks
684 *  None
685 *
686 *******************************************************************************
687 */
688
689void ih264e_cabac_encode_bypass_bin(cabac_ctxt_t *ps_cabac, WORD32 bin)
690{
691
692    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
693
694    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
695    UWORD32 u4_low = ps_cab_enc_env->u4_code_int_low;
696
697    /* Sanity checks */
698    ASSERT((u4_range >= 256) && (u4_range < 512));
699    ASSERT((bin == 0) || (bin == 1));
700
701    u4_low <<= 1;
702    /* add range if bin is 1 */
703    if (bin)
704    {
705        u4_low += u4_range;
706    }
707
708    /* 1 bit to be inserted in the bitstream */
709    ps_cab_enc_env->u4_bits_gen++;
710    ps_cab_enc_env->u4_code_int_low = u4_low;
711
712    /* generate stream when a byte is ready */
713    if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
714    {
715        ih264e_cabac_put_byte(ps_cabac);
716    }
717
718}
719
720
721 /**
722 ******************************************************************************
723 *
724 *  @brief Encodes a series of bypass bins (FLC bypass bins)
725 *
726 *  @par   Description
727 *  This function is more optimal than calling ih264e_cabac_encode_bypass_bin()
728 *  in a loop as cabac low, renorm and generating the stream (8bins at a time)
729 *  can be done in one operation
730 *
731 *  @param[inout]ps_cabac
732 *   pointer to cabac context (handle)
733 *
734 *  @param[in]   u4_bins
735 *   syntax element to be coded (as FLC bins)
736 *
737 *  @param[in]   num_bins
738 *   This is the FLC length for u4_sym
739 *
740 *  @return
741 *
742 ******************************************************************************
743 */
744
745void ih264e_cabac_encode_bypass_bins(cabac_ctxt_t *ps_cabac, UWORD32 u4_bins,
746                                     WORD32 num_bins)
747{
748
749    encoding_envirnoment_t *ps_cab_enc_env = &(ps_cabac->s_cab_enc_env);
750
751    UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
752    WORD32 next_byte;
753
754    /* Sanity checks */
755    ASSERT((num_bins < 33) && (num_bins > 0));
756    ASSERT((u4_range >= 256) && (u4_range < 512));
757
758    /* Compute bit always to populate the trace */
759    /* increment bits generated by num_bins */
760
761    /* Encode 8bins at a time and put in the bit-stream */
762    while (num_bins > 8)
763    {
764        num_bins -= 8;
765
766        next_byte = (u4_bins >> (num_bins)) & 0xff;
767
768        /*  L = (L << 8) +  (R * next_byte) */
769        ps_cab_enc_env->u4_code_int_low <<= 8;
770        ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
771        ps_cab_enc_env->u4_bits_gen += 8;
772
773        if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
774        {
775            /*  insert the leading byte of low into stream */
776            ih264e_cabac_put_byte(ps_cabac);
777        }
778    }
779
780    /* Update low with remaining bins and return */
781    next_byte = (u4_bins & ((1 << num_bins) - 1));
782
783    ps_cab_enc_env->u4_code_int_low <<= num_bins;
784    ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
785    ps_cab_enc_env->u4_bits_gen += num_bins;
786
787    if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
788    {
789        /*  insert the leading byte of low into stream */
790        ih264e_cabac_put_byte(ps_cabac);
791    }
792
793}
794