Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2 *
      3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
      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 /**
     19  ******************************************************************************
     20  * @file ihevcd_cabac.c
     21  *
     22  * @brief
     23  *    This file contains function definitions related to CABAC parsing
     24  *
     25  * @author
     26  *    Ittiam
     27  *
     28  *
     29  * List of Functions
     30  *
     31  *   ihevcd_cabac_init()
     32  *   ihevcd_cabac_decode_bin()
     33  *   ihevcd_cabac_decode_bypass_bin()
     34  *   ihevcd_cabac_decode_bypass_bins_tunary()
     35  *   ihevcd_cabac_decode_terminate()
     36  *   ihevcd_cabac_decode_bin_tunary()
     37  *   ihevcd_cabac_decode_bypass_bins()
     38  *   ihevcd_cabac_decode_bypass_bins_egk()
     39  *   ihevcd_cabac_decode_trunc_rice()
     40  *   ihevcd_cabac_flush()
     41  *
     42  ******************************************************************************
     43  */
     44 
     45 /*****************************************************************************/
     46 /* File Includes                                                             */
     47 /*****************************************************************************/
     48 #include <stdio.h>
     49 #include <stddef.h>
     50 #include <assert.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 
     54 #include "ihevc_typedefs.h"
     55 #include "iv.h"
     56 #include "ivd.h"
     57 #include "ihevcd_cxa.h"
     58 
     59 
     60 #include "ihevc_debug.h"
     61 #include "ihevc_macros.h"
     62 #include "ihevc_platform_macros.h"
     63 #include "ihevc_cabac_tables.h"
     64 #include "ihevc_defs.h"
     65 #include "ihevc_structs.h"
     66 #include "ihevc_cabac_tables.h"
     67 
     68 
     69 #include "ihevcd_defs.h"
     70 #include "ihevcd_function_selector.h"
     71 #include "ihevcd_structs.h"
     72 #include "ihevcd_error.h"
     73 #include "ihevcd_bitstream.h"
     74 #include "ihevcd_cabac.h"
     75 #include "ihevcd_trace.h"
     76 
     77 #ifdef TRACE
     78 extern trace_t g_trace;
     79 #endif
     80 #if DEBUG_CABAC_RANGE_OFST
     81 #if FULLRANGE
     82 #define DEBUG_RANGE_OFST(str, m_range, m_ofst )  \
     83 {\
     84     UWORD32 m_clz, m_range_shift, m_ofst_shift;                           \
     85     m_clz = CLZ(m_range);                                                \
     86     m_clz -= (32 - RANGE_NUMBITS);                                      \
     87     m_range_shift = m_range << m_clz;                                    \
     88     m_range_shift = m_range_shift >> RANGE_SHIFT;                                 \
     89     m_ofst_shift = m_ofst << m_clz;                                    \
     90     m_ofst_shift = m_ofst_shift >> RANGE_SHIFT;                                 \
     91     fprintf( g_trace.fp, "%-40s R: %3d O: %3d\n", str, m_range_shift, m_ofst_shift); \
     92 }
     93 
     94 #else
     95 #define DEBUG_RANGE_OFST(str,  m_range, m_ofst) \
     96     fprintf( g_trace.fp, "%-40s R: %3d O: %3d\n", str, m_range, m_ofst);
     97 #endif
     98 #else
     99 #define DEBUG_RANGE_OFST(str, m_range, m_ofst )
    100 #endif
    101 /*****************************************************************************/
    102 /* Function Definitions                                                      */
    103 /*****************************************************************************/
    104 
    105 /**
    106  ******************************************************************************
    107  *
    108  *  @brief Initializes the decoder cabac engine
    109  *
    110  *  @par   Description
    111  *  This routine needs to be called at start of slice/frame decode
    112  *
    113  *  @param[in,out]   ps_cabac_ctxt
    114  *  pointer to cabac context (handle)
    115  *
    116  *  @param[in]   ps_bitstrm
    117  *  pointer to bitstream context (handle)
    118  *
    119  *  @param[in]   qp
    120  *  current slice Qp
    121  *
    122  *  @param[in]   cabac_init_idc
    123  *  current slice init idc (range  [0 - 2])
    124  *
    125  *  @param[in]   pu1_init_ctxt
    126  *  Init cabac context to be used (range  [0 - 2])
    127  *
    128  *  @return      success or failure error code
    129  *
    130  ******************************************************************************
    131  */
    132 IHEVCD_ERROR_T ihevcd_cabac_init(cab_ctxt_t *ps_cabac,
    133                                  bitstrm_t *ps_bitstrm,
    134                                  WORD32 qp,
    135                                  WORD32 cabac_init_idc,
    136                                  const UWORD8 *pu1_init_ctxt)
    137 {
    138     /* Sanity checks */
    139     ASSERT(ps_cabac != NULL);
    140     ASSERT(ps_bitstrm != NULL);
    141     ASSERT((qp >= 0) && (qp < 52));
    142     ASSERT((cabac_init_idc >= 0) && (cabac_init_idc < 3));
    143     UNUSED(qp);
    144     UNUSED(cabac_init_idc);
    145     /* CABAC engine uses 32 bit range instead of 9 bits as specified by
    146      * the spec. This is done to reduce number of renormalizations
    147      */
    148     /* cabac engine initialization */
    149 #if FULLRANGE
    150     ps_cabac->u4_range = (UWORD32)510 << RANGE_SHIFT;
    151     BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    152                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (9 + RANGE_SHIFT));
    153 
    154 #else
    155     ps_cabac->u4_range = (UWORD32)510;
    156     BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    157                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, 9);
    158 
    159 #endif
    160 
    161     /* cabac context initialization based on init idc and slice qp */
    162     memcpy(ps_cabac->au1_ctxt_models,
    163            pu1_init_ctxt,
    164            IHEVC_CAB_CTXT_END);
    165     DEBUG_RANGE_OFST("init", ps_cabac->u4_range, ps_cabac->u4_ofst);
    166     return ((IHEVCD_ERROR_T)IHEVCD_SUCCESS);
    167 }
    168 
    169 IHEVCD_ERROR_T ihevcd_cabac_reset(cab_ctxt_t *ps_cabac,
    170                                   bitstrm_t *ps_bitstrm)
    171 {
    172     /* Sanity checks */
    173     ASSERT(ps_cabac != NULL);
    174     ASSERT(ps_bitstrm != NULL);
    175 
    176     /* CABAC engine uses 32 bit range instead of 9 bits as specified by
    177      * the spec. This is done to reduce number of renormalizations
    178      */
    179     /* cabac engine initialization */
    180 #if FULLRANGE
    181     ps_cabac->u4_range = (UWORD32)510 << RANGE_SHIFT;
    182     BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    183                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (9 + RANGE_SHIFT));
    184 
    185 #else
    186     ps_cabac->u4_range = (UWORD32)510;
    187     BITS_GET(ps_cabac->u4_ofst, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    188                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, 9);
    189 
    190 #endif
    191 
    192     return ((IHEVCD_ERROR_T)IHEVCD_SUCCESS);
    193 }
    194 
    195 /**
    196  ******************************************************************************
    197  *
    198  *  @brief Decodes a bin based on probablilty and mps packed context model
    199  *
    200  *  @par   Description
    201  *  Decodes a bin as per Section : 9.3.3.2.1 and calls renormalization if required
    202  *  as per section 9.3.3.2.2
    203  *  1. Apart from decoding bin, context model is updated as per state transition
    204  *  2. Range and Low renormalization is done based on bin and original state
    205  *  3. After renorm bistream is updated (if required)
    206  *
    207  *  @param[in,out]   ps_cabac
    208  *  pointer to cabac context (handle)
    209  *
    210  *  @param[in]   ctxt_index
    211  *  index of cabac context model containing pState[bits6-1] | MPS[bit0]
    212  *
    213  *  @param[in]   ps_bitstrm
    214  *  Bitstream context
    215  *
    216  *  @return      bin(boolean) to be decoded
    217  *
    218  ******************************************************************************
    219  */
    220 UWORD32 ihevcd_cabac_decode_bin(cab_ctxt_t *ps_cabac,
    221                                 bitstrm_t *ps_bitstrm,
    222                                 WORD32 ctxt_index
    223 
    224                                )
    225 {
    226     UWORD32 u4_range = ps_cabac->u4_range;
    227     UWORD32 u4_ofst = ps_cabac->u4_ofst;
    228     UWORD32 u4_rlps;
    229     UWORD32 u4_bin;
    230     UWORD8 *pu1_ctxt_model = &ps_cabac->au1_ctxt_models[ctxt_index];
    231     WORD32 state_mps = *pu1_ctxt_model;
    232 #if FULLRANGE
    233     WORD32 clz;
    234 #endif
    235     UWORD32 u4_qnt_range;
    236 
    237     /* Sanity checks */
    238     ASSERT(u4_range >= 256);
    239     ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END));
    240     ASSERT(state_mps < 128);
    241 #if FULLRANGE
    242     clz = CLZ(u4_range);
    243     clz -= (32 - RANGE_NUMBITS);
    244     u4_qnt_range = u4_range << clz;
    245     u4_qnt_range = (u4_qnt_range >> (RANGE_SHIFT + 6)) & 0x3;
    246 #else
    247     u4_qnt_range = (u4_range >> 6) & 0x3;
    248 #endif
    249     /* Get the lps range from LUT based on quantized range and state */
    250     u4_rlps = gau1_ihevc_cabac_rlps[state_mps >> 1][u4_qnt_range];
    251 #if FULLRANGE
    252     u4_rlps = u4_rlps << (RANGE_SHIFT - clz);
    253 #endif
    254     u4_range -= u4_rlps;
    255 
    256     u4_bin = state_mps & 1;
    257 
    258     if(u4_ofst >= u4_range)
    259     {
    260         u4_bin = 1 - u4_bin;
    261         u4_ofst -= u4_range;
    262         u4_range = u4_rlps;
    263     }
    264 
    265     *pu1_ctxt_model = gau1_ihevc_next_state[(state_mps << 1) | u4_bin];
    266 
    267     /*****************************************************************/
    268     /* Re-normalization; calculate bits generated based on range(R)  */
    269     /*****************************************************************/
    270     if(u4_range < (1 << 8))
    271     {
    272         UWORD32 u4_bits;
    273         WORD32 numbits;
    274         numbits = CLZ(u4_range);
    275         numbits -= (32 - RANGE_NUMBITS);
    276 #if !FULLRANGE
    277         numbits -= RANGE_SHIFT;
    278 #endif
    279         BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    280                  ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits);
    281 
    282         u4_ofst <<= numbits;
    283         u4_ofst |= u4_bits;
    284         u4_range <<= numbits;
    285 
    286     }
    287     /* Update the cabac context */
    288     ps_cabac->u4_range = u4_range;
    289     ps_cabac->u4_ofst = u4_ofst;
    290     DEBUG_RANGE_OFST("bin", ps_cabac->u4_range, ps_cabac->u4_ofst);
    291 
    292     return (u4_bin);
    293 
    294 
    295 }
    296 
    297 /**
    298  ******************************************************************************
    299  *
    300  *  @brief Decodes a bypass bin (equi-probable 0 / 1)
    301  *
    302  *  @par   Description
    303  *  Decodes a bypss bin as per Section : 9.3.3.2.3
    304  *
    305  *  @param[in,out]  ps_cabac
    306  *  pointer to cabac context (handle)
    307  *
    308  *  @param[in]   ps_bitstrm
    309  *  Bitstream context
    310  *
    311  *  @return      Decoded bypass bin
    312  *
    313  ******************************************************************************
    314  */
    315 UWORD32 ihevcd_cabac_decode_bypass_bin(cab_ctxt_t *ps_cabac,
    316                                        bitstrm_t *ps_bitstrm)
    317 {
    318 
    319     UWORD32 u4_bin;
    320     UWORD32 u4_range = ps_cabac->u4_range;
    321     UWORD32 u4_ofst = ps_cabac->u4_ofst;
    322     UWORD32 u4_bits;
    323 
    324     /* Sanity checks */
    325     ASSERT(u4_range >= 256);
    326 
    327     BIT_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    328             ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word);
    329 
    330     u4_ofst <<= 1;
    331     u4_ofst |= u4_bits;
    332 
    333     u4_bin = 0;
    334     if(u4_ofst >= u4_range)
    335     {
    336         u4_bin = 1;
    337         u4_ofst -= u4_range;
    338     }
    339 
    340     /* Update the cabac context */
    341     ps_cabac->u4_ofst = u4_ofst;
    342     DEBUG_RANGE_OFST("bypass end", ps_cabac->u4_range, ps_cabac->u4_ofst);
    343     return (u4_bin);
    344 }
    345 
    346 /**
    347  ******************************************************************************
    348  *
    349  *  @brief Decodes a terminate bin (1:terminate 0:do not terminate)
    350  *
    351  *  @par   Description
    352  *  Decodes a terminate bin to be called for end_of_slice_flag and pcm_flag
    353  *  as per Section : 9.3.3.2.4
    354  *
    355  *  @param[in,out]  ps_cabac
    356  *  pointer to cabac context (handle)
    357  *
    358  *  @param[in]   ps_bitstrm
    359  *  Bitstream context
    360  *
    361  *  @return    Decoded Bin to indicate whether to terminate or not
    362  *
    363  ******************************************************************************
    364  */
    365 UWORD32 ihevcd_cabac_decode_terminate(cab_ctxt_t *ps_cabac,
    366                                       bitstrm_t *ps_bitstrm)
    367 {
    368     UWORD32 u4_range = ps_cabac->u4_range;
    369     UWORD32 u4_ofst = ps_cabac->u4_ofst;
    370     UWORD32 u4_bin;
    371 #if FULLRANGE
    372     WORD32 clz;
    373 #endif
    374     /* Sanity checks */
    375     ASSERT(u4_range >= 256);
    376 #if FULLRANGE
    377     clz = CLZ(u4_range);
    378     clz -= (32 - RANGE_NUMBITS);
    379     u4_range -= 2 << (RANGE_SHIFT - clz);
    380 #else
    381     u4_range -= 2;
    382 #endif
    383 
    384     if(u4_ofst >= u4_range)
    385     {
    386         u4_bin = 1;
    387 
    388 #if FULLRANGE
    389         /* In case of FULLRANGE extra bits read earlier need to pushed back to the bitstream */
    390         {
    391             WORD32 clz;
    392             WORD32 numbits;
    393             clz = CLZ(ps_cabac->u4_range);
    394 
    395             numbits = (32 - clz);
    396             numbits -= 9;
    397 
    398             ihevcd_bits_seek(ps_bitstrm, -numbits);
    399         }
    400 #endif
    401 
    402     }
    403     else
    404     {
    405         u4_bin = 0;
    406     }
    407     if(0 == u4_bin)
    408     {
    409         UWORD32 u4_bits;
    410         WORD32 numbits;
    411         numbits = CLZ(u4_range);
    412         numbits -= (32 - RANGE_NUMBITS);
    413 #if !FULLRANGE
    414         numbits -= RANGE_SHIFT;
    415 #endif
    416         /* Renormalize if required */
    417         if(numbits)
    418         {
    419             BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    420                      ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits);
    421 
    422             u4_ofst <<= numbits;
    423             u4_ofst |= u4_bits;
    424             u4_range <<= numbits;
    425         }
    426     }
    427     /* bits to be inserted in the bitstream */
    428     ps_cabac->u4_range = u4_range;
    429     ps_cabac->u4_ofst = u4_ofst;
    430     DEBUG_RANGE_OFST("term", ps_cabac->u4_range, ps_cabac->u4_ofst);
    431 
    432     return (u4_bin);
    433 }
    434 
    435 /**
    436  ******************************************************************************
    437  *
    438  *  @brief Decodes a bypass bin (equi-probable 0 / 1)
    439  *
    440  *  @par   Description
    441  *  Decodes a bypss bin as per Section : 9.3.3.2.3
    442  *
    443  *  @param[in,out]  ps_cabac
    444  *  pointer to cabac context (handle)
    445  *
    446  *  @param[in]   ps_bitstrm
    447  *  Bitstream context
    448  *
    449  *  @param[in]   numbins
    450  *  Number of bins to decoded
    451  *
    452  *  @return      Decoded bypass bin
    453  *
    454  *  @remarks     Tested only for numbins less than 17
    455  *
    456  ******************************************************************************
    457  */
    458 
    459 UWORD32 ihevcd_cabac_decode_bypass_bins(cab_ctxt_t *ps_cabac,
    460                                         bitstrm_t *ps_bitstrm,
    461                                         WORD32 numbins)
    462 {
    463     UWORD32 u4_bins;
    464 
    465 
    466     UWORD32 u4_range = ps_cabac->u4_range;
    467     UWORD32 u4_ofst = ps_cabac->u4_ofst;
    468     UWORD32 u4_bits;
    469     ASSERT(u4_range >= 256);
    470     ASSERT(numbins > 0);
    471 
    472     /* Sanity checks */
    473     ASSERT(numbins < 17);
    474 
    475     u4_bins = 0;
    476 
    477     BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    478                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbins);
    479 
    480     do
    481     {
    482         UWORD32 u4_bit;
    483         numbins--;
    484         u4_bit = (u4_bits >> numbins) & 1;
    485         u4_ofst <<= 1;
    486         u4_ofst |= u4_bit;
    487 
    488         u4_bins <<= 1;
    489         if(u4_ofst >= u4_range)
    490         {
    491             u4_bins += 1;
    492             u4_ofst -= u4_range;
    493         }
    494     }while(numbins);
    495 
    496     /* Update the cabac context */
    497     ps_cabac->u4_ofst = u4_ofst;
    498     DEBUG_RANGE_OFST("bypass", ps_cabac->u4_range, ps_cabac->u4_ofst);
    499     return (u4_bins);
    500 }
    501 
    502 /**
    503  ******************************************************************************
    504  *
    505  *  @brief Decodes a truncated unary symbol associated with context model(s)
    506  *
    507  *  @par   Description
    508  *  Decodes symbols coded with TUnary binarization as per sec 9.3.2.2
    509  *  This is used for computing symbols like qp_delta,
    510  *  last_sig_coeff_prefix_x, last_sig_coeff_prefix_y.
    511  *
    512  *  The context models associated with each bin is computed as :
    513  *   current bin context = "base context idx" + (bin_idx >> shift)
    514  *  where
    515  *   1. "base context idx" is the base index for the syntax element
    516  *   2. "bin_idx" is the current bin index of the unary code
    517  *   3. "shift" is the shift factor associated with this syntax element
    518  *
    519  *  @param[in,out] ps_cabac
    520  *   pointer to cabac context (handle)
    521  *
    522  *  @param[in]   ps_bitstrm
    523  *  Bitstream context
    524  *
    525  *  @param[in]   c_max
    526  *   maximum value of sym (required for tunary binarization)
    527  *
    528  *  @param[in]   ctxt_index
    529  *   base context model index for this syntax element
    530  *
    531  *  @param[in]   ctxt_shift
    532  *   shift factor for context increments associated with this syntax element
    533  *
    534  *  @param[in]   ctxt_inc_max
    535  *   max value of context increment beyond which all bins will use same ctxt
    536  *
    537  *  @return     syntax element decoded
    538  *
    539  ******************************************************************************
    540  */
    541 UWORD32 ihevcd_cabac_decode_bins_tunary(cab_ctxt_t *ps_cabac,
    542                                         bitstrm_t *ps_bitstrm,
    543                                         WORD32 c_max,
    544                                         WORD32 ctxt_index,
    545                                         WORD32 ctxt_shift,
    546                                         WORD32 ctxt_inc_max)
    547 {
    548     UWORD32 u4_sym;
    549     WORD32 bin;
    550 
    551     /* Sanity checks */
    552     ASSERT(c_max > 0);
    553     ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END));
    554     ASSERT((ctxt_index + (c_max >> ctxt_shift)) < IHEVC_CAB_CTXT_END);
    555 
    556     u4_sym = 0;
    557     do
    558     {
    559         WORD32 bin_index;
    560         bin_index = ctxt_index + MIN((u4_sym >> ctxt_shift), (UWORD32)ctxt_inc_max);
    561         IHEVCD_CABAC_DECODE_BIN(bin, ps_cabac, ps_bitstrm,  bin_index);
    562         u4_sym++;
    563     }while(((WORD32)u4_sym < c_max) && bin);
    564 
    565     u4_sym = u4_sym - 1 + bin;
    566 
    567     return (u4_sym);
    568 }
    569 
    570 /**
    571  ******************************************************************************
    572  *
    573  *  @brief Decodes a syntax element as truncated unary bypass bins
    574  *
    575  *  @par   Description
    576  *  Decodes symbols coded with TUnary binarization as per sec 9.3.2.2
    577  *  These symbols are coded as bypass bins
    578  *   This is used for computing symbols like merge_idx,
    579  *  mpm_idx etc
    580  *
    581  *  @param[in,out]ps_cabac
    582  *   pointer to cabac context (handle)
    583  *
    584  *  @param[in]   ps_bitstrm
    585  *  Bitstream context
    586  *
    587  *  @param[in]   c_max
    588  *   maximum value of sym (required for tunary binarization)
    589  *
    590  *  @return      syntax element decoded
    591  *
    592  ******************************************************************************
    593  */
    594 UWORD32 ihevcd_cabac_decode_bypass_bins_tunary(cab_ctxt_t *ps_cabac,
    595                                                bitstrm_t *ps_bitstrm,
    596                                                WORD32 c_max)
    597 {
    598 
    599     UWORD32 u4_sym;
    600     WORD32 bin;
    601     UWORD32 u4_ofst = ps_cabac->u4_ofst;
    602     UWORD32 u4_range = ps_cabac->u4_range;
    603     UWORD32 u4_bits;
    604     /* Sanity checks */
    605     ASSERT(c_max > 0);
    606     ASSERT(u4_range >= 256);
    607     u4_sym = 0;
    608     BITS_NXT(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    609                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, (UWORD32)c_max);
    610     u4_bits <<= (32 - c_max);
    611     do
    612     {
    613         u4_ofst <<= 1;
    614         u4_ofst |= (u4_bits >> 31);
    615         u4_bits <<= 1;
    616 
    617         bin = 0;
    618         if(u4_ofst >= u4_range)
    619         {
    620             bin = 1;
    621             u4_ofst -= u4_range;
    622         }
    623         u4_sym++;
    624     }while(((WORD32)u4_sym < c_max) && bin);
    625     BITS_FLUSH(ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,
    626                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, u4_sym);
    627 
    628     u4_sym = u4_sym - 1 + bin;
    629     /* Update the cabac context */
    630     ps_cabac->u4_ofst = u4_ofst;
    631 
    632     return (u4_sym);
    633 }
    634 
    635 /**
    636  ******************************************************************************
    637  *
    638  *  @brief Decodes a syntax element as kth order Exp-Golomb code (EGK)
    639  *
    640  *  @par   Description
    641  *  Decodes a syntax element binarized as kth order Exp-Golomb code (EGK)
    642  *  Elements are coded as bypass bins
    643  *
    644  *  @param[in,out] ps_cabac
    645  *   pointer to cabac context (handle)
    646  *
    647  *  @param[in]   u4_sym
    648  *   syntax element to be coded as EGK
    649  *
    650  *  @param[in]   k
    651  *   order of EGk
    652  *
    653  *  @return      success or failure error code
    654  *
    655  ******************************************************************************
    656  */
    657 UWORD32 ihevcd_cabac_decode_bypass_bins_egk(cab_ctxt_t *ps_cabac,
    658                                             bitstrm_t *ps_bitstrm,
    659                                             WORD32 k)
    660 {
    661 
    662     UWORD32 u4_sym;
    663     WORD32 numones;
    664     WORD32 bin;
    665 
    666     /* Sanity checks */
    667     ASSERT((k >= 0));
    668 
    669     numones = k;
    670     bin = 1;
    671     u4_sym = 0;
    672     while(bin)
    673     {
    674         IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm);
    675         u4_sym += bin << numones++;
    676     }
    677 
    678     numones -= 1;
    679     numones = CLIP3(numones, 0, 16);
    680 
    681     if(numones)
    682     {
    683         UWORD32 u4_suffix;
    684 
    685         IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, numones);
    686         u4_sym += u4_suffix;
    687     }
    688     return (u4_sym);
    689 }
    690 
    691 /**
    692  ******************************************************************************
    693  *
    694  *  @brief Decodes a syntax element as truncated rice code (TR)
    695  *
    696  *  @par   Description
    697  *  Decodes a syntax element as truncated rice code (TR)
    698  *  Elements are coded as bypass bins
    699  *  This function ise used for coeff_abs_level_remaining coding when
    700  *  level is less than c_rice_max
    701  *
    702  *  @param[in,out] ps_cabac
    703  *   pointer to cabac context (handle)
    704  *
    705  *  @param[in]   u4_sym
    706  *   syntax element to be coded as truncated rice code
    707  *
    708  *  @param[in]   c_rice_param
    709  *    shift factor for truncated unary prefix coding of (u4_sym >> c_rice_param)
    710  *
    711  *  @param[in]   c_rice_max
    712  *    max symbol val below which a suffix is coded as (u4_sym%(1<<c_rice_param))
    713  *    This is currently (4 << c_rice_param) for coeff_abs_level_remaining
    714  *
    715  *  @return      success or failure error code
    716  *
    717  ******************************************************************************
    718  */
    719 UWORD32 ihevcd_cabac_decode_bypass_bins_trunc_rice(cab_ctxt_t *ps_cabac,
    720                                                    bitstrm_t *ps_bitstrm,
    721                                                    WORD32 c_rice_param,
    722                                                    WORD32 c_rice_max)
    723 {
    724     UWORD32 u4_sym;
    725     WORD32 bin;
    726     WORD32 c_max;
    727     UWORD32 u4_suffix;
    728     /* Sanity checks */
    729     ASSERT((c_rice_param >= 0));
    730 
    731 
    732     /* Decode prefix coded as TUnary */
    733     c_max = c_rice_max >> c_rice_param;
    734     u4_sym = 0;
    735     do
    736     {
    737         IHEVCD_CABAC_DECODE_BYPASS_BIN(bin, ps_cabac, ps_bitstrm);
    738         u4_sym++;
    739 
    740     }while(((WORD32)u4_sym < c_max) && bin);
    741     u4_sym = u4_sym - 1 + bin;
    742 
    743     /* If suffix is present, then decode c_rice_param number of bins */
    744     if(c_rice_param)
    745     {
    746         IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_suffix, ps_cabac, ps_bitstrm, c_rice_param);
    747 
    748         u4_sym = (u4_sym << c_rice_param) | u4_suffix;
    749     }
    750     return (u4_sym);
    751 }
    752