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 ******************************************************************************
     21 * @file ihevcd_cabac.h
     22 *
     23 * @brief
     24 *  This file contains decoder cabac engine related structures and
     25 *  interface prototypes
     26 *
     27 * @author
     28 *  Ittiam
     29 ******************************************************************************
     30 */
     31 
     32 #ifndef _IHEVCD_CABAC_H_
     33 #define _IHEVCD_CABAC_H_
     34 
     35 #include "ihevc_typedefs.h"
     36 /*****************************************************************************/
     37 /* Constant Macros                                                           */
     38 /*****************************************************************************/
     39 /**
     40 *******************************************************************************
     41 @brief
     42 *******************************************************************************
     43  */
     44 #define CABAC_BITS  9
     45 
     46 /**
     47  * Following definitions control whether cabac functions are inlined as macros or
     48  * are called as functions. Set these to 0 to debug cabac leaf level functions
     49  * Note these macros assume FULLRANGE is 1.
     50  */
     51 #define CABAC_DECODE_BIN            1
     52 #define CABAC_DECODE_BYPASS_BIN     1
     53 #define CABAC_DECODE_BYPASS_BINS    1
     54 
     55 /*****************************************************************************/
     56 /* Function Macros                                                           */
     57 /*****************************************************************************/
     58 #if CABAC_DECODE_BIN
     59 #define IHEVCD_CABAC_DECODE_BIN(u4_bin, ps_cabac, ps_bitstrm, ctxt_index)       \
     60 {                                                                               \
     61     UWORD32 u4_range = ps_cabac->u4_range;                                      \
     62     UWORD32 u4_ofst = ps_cabac->u4_ofst;                                        \
     63     UWORD32 u4_rlps;                                                            \
     64     UWORD8 *pu1_ctxt_model = &ps_cabac->au1_ctxt_models[ctxt_index];            \
     65     WORD32 state_mps = *pu1_ctxt_model;                                         \
     66     WORD32 clz;                                                                 \
     67     UWORD32 u4_qnt_range;                                                       \
     68                                                                                 \
     69     /* Sanity checks */                                                         \
     70     ASSERT(FULLRANGE == 1);                                                     \
     71     ASSERT(u4_range >= 256);                                                    \
     72     ASSERT((ctxt_index >= 0) && (ctxt_index < IHEVC_CAB_CTXT_END));             \
     73     ASSERT(state_mps < 128);                                                    \
     74     clz = CLZ(u4_range);                                                        \
     75     clz -= (32 - RANGE_NUMBITS);                                                \
     76     u4_qnt_range = u4_range << clz;                                             \
     77     u4_qnt_range = (u4_qnt_range >> (RANGE_SHIFT + 6)) & 0x3;                   \
     78     /* Get the lps range from LUT based on quantized range and state */         \
     79     u4_rlps = gau1_ihevc_cabac_rlps[state_mps >> 1][u4_qnt_range];              \
     80     u4_rlps = u4_rlps << (RANGE_SHIFT - clz);                                   \
     81     u4_range -= u4_rlps;                                                        \
     82                                                                                 \
     83     u4_bin = state_mps & 1;                                                     \
     84                                                                                 \
     85     if(u4_ofst >= u4_range)                                                     \
     86     {                                                                           \
     87         u4_bin = 1 - u4_bin;                                                    \
     88         u4_ofst -= u4_range;                                                    \
     89         u4_range = u4_rlps;                                                     \
     90     }                                                                           \
     91                                                                                 \
     92     *pu1_ctxt_model = gau1_ihevc_next_state[(state_mps << 1) | u4_bin];         \
     93                                                                                 \
     94     /*****************************************************************/         \
     95     /* Re-normalization; calculate bits generated based on range(R)  */         \
     96     /*****************************************************************/         \
     97     if(u4_range < (1 << 8))                                                     \
     98     {                                                                           \
     99         UWORD32 u4_bits;                                                        \
    100         WORD32 numbits;                                                         \
    101         numbits = CLZ(u4_range);                                                \
    102         numbits -= (32 - RANGE_NUMBITS);                                        \
    103         BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,         \
    104                  ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbits);    \
    105                                                                                 \
    106         u4_ofst <<= numbits;                                                    \
    107         u4_ofst |= u4_bits;                                                     \
    108         u4_range <<= numbits;                                                   \
    109                                                                                 \
    110     }                                                                           \
    111     /* Update the cabac context */                                              \
    112     ps_cabac->u4_range = u4_range;                                              \
    113     ps_cabac->u4_ofst = u4_ofst;                                                \
    114                                                                                 \
    115 }
    116 #else
    117 #define IHEVCD_CABAC_DECODE_BIN(u4_bin, ps_cabac, ps_bitstrm, ctxt_index)       \
    118         u4_bin = ihevcd_cabac_decode_bin(ps_cabac, ps_bitstrm, ctxt_index);
    119 #endif
    120 
    121 #if CABAC_DECODE_BYPASS_BIN
    122 #define IHEVCD_CABAC_DECODE_BYPASS_BIN(u4_bin, ps_cabac, ps_bitstrm)            \
    123 {                                                                               \
    124                                                                                 \
    125     UWORD32 u4_range = ps_cabac->u4_range;                                      \
    126     UWORD32 u4_ofst = ps_cabac->u4_ofst;                                        \
    127     UWORD32 u4_bits;                                                            \
    128                                                                                 \
    129     /* Sanity checks */                                                         \
    130     ASSERT(FULLRANGE == 1);                                                     \
    131     ASSERT(u4_range >= 256);                                                    \
    132                                                                                 \
    133     BIT_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,              \
    134             ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word);                  \
    135                                                                                 \
    136     u4_ofst <<= 1;                                                              \
    137     u4_ofst |= u4_bits;                                                         \
    138                                                                                 \
    139     u4_bin = 0;                                                                 \
    140     if(u4_ofst >= u4_range)                                                     \
    141     {                                                                           \
    142         u4_bin = 1;                                                             \
    143         u4_ofst -= u4_range;                                                    \
    144     }                                                                           \
    145                                                                                 \
    146     /* Update the cabac context */                                              \
    147     ps_cabac->u4_ofst = u4_ofst;                                                \
    148 }
    149 #else
    150 
    151 #define IHEVCD_CABAC_DECODE_BYPASS_BIN(u4_bin, ps_cabac, ps_bitstrm)            \
    152                 u4_bin = ihevcd_cabac_decode_bypass_bin(ps_cabac, ps_bitstrm);
    153 #endif
    154 
    155 #if CABAC_DECODE_BYPASS_BINS
    156 #define IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_bins, ps_cabac, ps_bitstrm, numbins) \
    157 {                                                                               \
    158     UWORD32 u4_range = ps_cabac->u4_range;                                      \
    159     UWORD32 u4_ofst = ps_cabac->u4_ofst;                                        \
    160     UWORD32 u4_bits;                                                            \
    161     ASSERT(FULLRANGE == 1);                                                     \
    162     ASSERT(u4_range >= 256);                                                    \
    163     ASSERT(numbins > 0);                                                        \
    164     {                                                                           \
    165         WORD32 numbins_tmp = numbins;                                           \
    166         /* Sanity checks */                                                     \
    167         ASSERT(numbins < 17);                                                   \
    168                                                                                 \
    169         u4_bins = 0;                                                            \
    170                                                                                 \
    171         BITS_GET(u4_bits, ps_bitstrm->pu4_buf, ps_bitstrm->u4_bit_ofst,         \
    172                     ps_bitstrm->u4_cur_word, ps_bitstrm->u4_nxt_word, numbins); \
    173         do                                                                      \
    174         {                                                                       \
    175             UWORD32 u4_bit;                                                     \
    176             numbins_tmp--;                                                      \
    177             u4_bit = (u4_bits >> numbins_tmp) & 1;                              \
    178             u4_ofst <<= 1;                                                      \
    179             u4_ofst |= u4_bit;                                                  \
    180                                                                                 \
    181             u4_bins <<= 1;                                                      \
    182             if(u4_ofst >= u4_range)                                             \
    183             {                                                                   \
    184                 u4_bins += 1;                                                   \
    185                 u4_ofst -= u4_range;                                            \
    186             }                                                                   \
    187         }while(numbins_tmp);                                                    \
    188                                                                                 \
    189         /* Update the cabac context */                                          \
    190         ps_cabac->u4_ofst = u4_ofst;                                            \
    191     }                                                                           \
    192 }
    193 
    194 
    195 #else
    196 
    197 #define IHEVCD_CABAC_DECODE_BYPASS_BINS(u4_bins, ps_cabac, ps_bitstrm, numbins) \
    198       u4_bins = ihevcd_cabac_decode_bypass_bins(ps_cabac, ps_bitstrm, numbins);
    199 
    200 #endif
    201 /*****************************************************************************/
    202 /* Structures                                                                */
    203 /*****************************************************************************/
    204 
    205 
    206 
    207 /*****************************************************************************/
    208 /* Extern Function Declarations                                              */
    209 /*****************************************************************************/
    210 IHEVCD_ERROR_T    ihevcd_cabac_init
    211 (
    212                 cab_ctxt_t  *ps_cabac,
    213                 bitstrm_t   *ps_bitstrm,
    214                 WORD32      slice_qp,
    215                 WORD32      cabac_init_idc,
    216                 const UWORD8      *pu1_init_ctxt
    217 );
    218 
    219 
    220 
    221 UWORD32    ihevcd_cabac_decode_bin
    222 (
    223                 cab_ctxt_t  *ps_cabac,
    224                 bitstrm_t *ps_bitstrm,
    225                 WORD32      ctxt_index
    226 );
    227 
    228 UWORD32    ihevcd_cabac_decode_bypass_bin
    229 (
    230                 cab_ctxt_t  *ps_cabac,
    231                 bitstrm_t *ps_bitstrm
    232 );
    233 
    234 UWORD32    ihevcd_cabac_decode_terminate
    235 (
    236                 cab_ctxt_t  *ps_cabac,
    237                 bitstrm_t *ps_bitstrm
    238 );
    239 
    240 UWORD32    ihevcd_cabac_decode_bypass_bins
    241 (
    242                 cab_ctxt_t  *ps_cabac,
    243                 bitstrm_t *ps_bitstrm,
    244                 WORD32       num_bins
    245 );
    246 
    247 UWORD32    ihevcd_cabac_decode_bins_tunary
    248 (
    249                 cab_ctxt_t  *ps_cabac,
    250                 bitstrm_t *ps_bitstrm,
    251                 WORD32       c_max,
    252                 WORD32       ctxt_index,
    253                 WORD32       ctxt_shift,
    254                 WORD32       ctxt_inc_max
    255 
    256 );
    257 
    258 UWORD32    ihevcd_cabac_decode_bypass_bins_tunary
    259 (
    260                 cab_ctxt_t  *ps_cabac,
    261                 bitstrm_t *ps_bitstrm,
    262                 WORD32       c_max
    263 
    264 );
    265 
    266 UWORD32    ihevcd_cabac_decode_bypass_bins_egk
    267 (
    268                 cab_ctxt_t  *ps_cabac,
    269                 bitstrm_t *ps_bitstrm,
    270                 WORD32       k
    271 );
    272 
    273 UWORD32    ihevcd_cabac_decode_bypass_bins_trunc_rice
    274 (
    275                 cab_ctxt_t  *ps_cabac,
    276                 bitstrm_t *ps_bitstrm,
    277                 WORD32       c_rice_param,
    278                 WORD32       c_rice_max
    279 );
    280 
    281 IHEVCD_ERROR_T  ihevcd_cabac_flush(cab_ctxt_t  *ps_cabac);
    282 
    283 IHEVCD_ERROR_T ihevcd_cabac_reset(cab_ctxt_t *ps_cabac,
    284                                   bitstrm_t *ps_bitstrm);
    285 
    286 #endif /* _IHEVCD_CABAC_H_ */
    287