Hamsalekha S | 8d3d303 | 2015-03-13 21:24:58 +0530 | [diff] [blame] | 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 | * \file ih264d_cabac.h |
| 23 | * |
| 24 | * \brief |
| 25 | * This file contains declarations of Binary decoding routines and tables. |
| 26 | * |
| 27 | * \date |
| 28 | * 04/02/2003 |
| 29 | * |
| 30 | * \author NS |
| 31 | *************************************************************************** |
| 32 | */ |
| 33 | |
| 34 | #ifndef _IH264D_CABAC_H_ |
| 35 | #define _IH264D_CABAC_H_ |
| 36 | |
| 37 | #include "ih264_typedefs.h" |
| 38 | #include "ih264_macros.h" |
| 39 | #include "ih264_platform_macros.h" |
| 40 | #include "ih264d_bitstrm.h" |
| 41 | #include "ih264d_defs.h" |
| 42 | |
| 43 | #define B_BITS 10 |
| 44 | |
| 45 | #define HALF (1 << (B_BITS-1)) |
| 46 | #define QUARTER (1 << (B_BITS-2)) |
| 47 | |
| 48 | #define CTXT_UNUSED {0,64} |
| 49 | #define NUM_MB_SKIP_CTXT 6 |
| 50 | #define NUM_MB_TYPE_CTXT 9 |
| 51 | #define NUM_SUBMB_TYPE_CTXT 7 |
| 52 | #define NUM_REF_IDX_CTXT 6 |
| 53 | #define NUM_MB_QP_DELTA 4 |
| 54 | #define NUM_PRED_MODE 6 |
| 55 | #define NUM_MB_FIELD 3 |
| 56 | #define NUM_CBP 12 |
| 57 | #define NUM_CTX_MVD 14 |
| 58 | |
| 59 | /* Residual block cabac context parameters */ |
| 60 | #define NUM_CTX_CAT 6 |
| 61 | #define NUM_LUMA_CTX_CAT 3 |
| 62 | #define NUM_CTX_CODED_BLOCK 4 |
| 63 | /* Luma CtxSigCoeff + CtxLastCoeff = 15 + 15 = 30 */ |
| 64 | #define NUM_LUMA_CTX_SIG_COEF 30 |
| 65 | /* Chroma DC CtxSigCoeff + CtxLastCoeff = 3 + 3 = 6 */ |
| 66 | #define NUM_CTX_CHROMA_DC_SIG_COEF 6 |
| 67 | /* Chroma AC CtxSigCoeff + CtxLastCoeff = 14 + 14 = 28 */ |
| 68 | #define NUM_CTX_CHROMA_AC_SIG_COEF 28 |
| 69 | #define NUM_CTX_ABS_LEVEL 10 |
| 70 | |
| 71 | #define LUMA_DC_CTXCAT 0 |
| 72 | #define LUMA_AC_CTXCAT 1 |
| 73 | #define LUMA_4X4_CTXCAT 2 |
| 74 | #define CHROMA_DC_CTXCAT 3 |
| 75 | #define CHROMA_AC_CTXCAT 4 |
| 76 | #define LUMA_8X8_CTXCAT 5 |
| 77 | |
| 78 | /*****************************************************************************/ |
| 79 | /* Constant Macros */ |
| 80 | /*****************************************************************************/ |
| 81 | #define NUM_CABAC_CTXTS 460 |
| 82 | #define QP_RANGE 52 |
| 83 | #define NUM_CAB_INIT_IDC_PLUS_ONE 4 |
| 84 | #define LAST_COEFF_CTXT_MINUS_SIG_COEFF_CTXT 61 |
| 85 | #define LAST_COEFF_CTXT_MINUS_SIG_COEFF_CTXT_8X8 15 |
| 86 | |
| 87 | /*bits 0 to 5 :state |
| 88 | bit 6:mps*/ |
| 89 | typedef struct |
| 90 | { |
| 91 | UWORD8 u1_mps_state; /* state number */ |
| 92 | } bin_ctxt_model_t; |
| 93 | |
| 94 | typedef struct |
| 95 | |
| 96 | { |
| 97 | /* Neighbour availability Variables needed to get CtxtInc, for CABAC */ |
| 98 | UWORD8 u1_mb_type; /** macroblock type: I/P/B/SI/SP */ |
| 99 | UWORD8 u1_cbp; /** Coded Block Pattern */ |
| 100 | UWORD8 u1_intra_chroma_pred_mode; |
| 101 | |
| 102 | /*************************************************************************/ |
| 103 | /* Arrangnment of DC CSBP */ |
| 104 | /* bits: b7 b6 b5 b4 b3 b2 b1 b0 */ |
| 105 | /* CSBP: x x x x x Vdc Udc Ydc */ |
| 106 | /*************************************************************************/ |
| 107 | UWORD8 u1_yuv_dc_csbp; |
| 108 | WORD8 i1_ref_idx[4]; |
| 109 | UWORD8 u1_mv[4][4]; |
| 110 | UWORD8 u1_transform8x8_ctxt; |
| 111 | } ctxt_inc_mb_info_t; |
| 112 | |
| 113 | #define ONE_RIGHT_SHIFTED_BY_8 1<<8 |
| 114 | #define ONE_RIGHT_SHIFTED_BY_9 1<<9 |
| 115 | #define ONE_RIGHT_SHIFTED_BY_14 1<<14 |
| 116 | typedef struct |
| 117 | { |
| 118 | UWORD32 u4_code_int_range; |
| 119 | UWORD32 u4_code_int_val_ofst; |
| 120 | const void *cabac_table; |
| 121 | void * pv_codec_handle; /* For Error Handling */ |
| 122 | } decoding_envirnoment_t; |
| 123 | |
| 124 | WORD32 ih264d_init_cabac_dec_envirnoment(decoding_envirnoment_t * ps_cab_env, |
| 125 | dec_bit_stream_t *ps_bitstrm); |
| 126 | |
| 127 | UWORD32 ih264d_decode_bin(UWORD32 u4_ctx_inc, |
| 128 | bin_ctxt_model_t *ps_bin_ctxt, |
| 129 | dec_bit_stream_t *ps_bitstrm, |
| 130 | decoding_envirnoment_t *ps_cab_env); |
| 131 | UWORD8 ih264d_decode_terminate(decoding_envirnoment_t * ps_cab_env, |
| 132 | dec_bit_stream_t * ps_bitstrm); |
| 133 | |
| 134 | UWORD32 ih264d_decode_bins_tunary(UWORD8 u1_max_bins, |
| 135 | UWORD32 u4_ctx_inc, |
| 136 | bin_ctxt_model_t *ps_src_bin_ctxt, |
| 137 | dec_bit_stream_t *ps_bitstrm, |
| 138 | decoding_envirnoment_t *ps_cab_env); |
| 139 | |
| 140 | UWORD32 ih264d_decode_bins(UWORD8 u1_max_bins, |
| 141 | UWORD32 u4_ctx_inc, |
| 142 | bin_ctxt_model_t *ps_src_bin_ctxt, |
| 143 | dec_bit_stream_t *ps_bitstrm, |
| 144 | decoding_envirnoment_t *ps_cab_env); |
| 145 | UWORD32 ih264d_decode_bins_unary(UWORD8 u1_max_bins, |
| 146 | UWORD32 u4_ctx_inc, |
| 147 | bin_ctxt_model_t *ps_src_bin_ctxt, |
| 148 | dec_bit_stream_t *ps_bitstrm, |
| 149 | decoding_envirnoment_t *ps_cab_env); |
| 150 | |
| 151 | UWORD32 ih264d_decode_bypass_bins_unary(decoding_envirnoment_t *ps_cab_env, |
| 152 | dec_bit_stream_t *ps_bitstrm); |
| 153 | |
| 154 | UWORD32 ih264d_decode_bypass_bins(decoding_envirnoment_t *ps_cab_env, |
| 155 | UWORD8 u1_max_bins, |
| 156 | dec_bit_stream_t *ps_bitstrm); |
| 157 | |
| 158 | /*****************************************************************************/ |
| 159 | /* Function Macros */ |
| 160 | /*****************************************************************************/ |
| 161 | |
| 162 | /*****************************************************************************/ |
| 163 | /* Defining a macro for renormalization*/ |
| 164 | /*****************************************************************************/ |
| 165 | |
| 166 | /*we renormalize every time the number bits(which are read ahead of time) we have |
| 167 | consumed in the u4_ofst exceeds 23*/ |
| 168 | |
| 169 | #define RENORM_RANGE_OFFSET(u4_codeIntRange_m,u4_codeIntValOffset_m,u4_offset_m,pu4_buffer_m) \ |
| 170 | { \ |
| 171 | UWORD32 read_bits_m,u4_clz_m ; \ |
| 172 | u4_clz_m = CLZ(u4_codeIntRange_m); \ |
| 173 | NEXTBITS(read_bits_m,(u4_offset_m+23),pu4_buffer_m,u4_clz_m) \ |
| 174 | FLUSHBITS(u4_offset_m,(u4_clz_m)) \ |
| 175 | u4_codeIntRange_m = u4_codeIntRange_m << u4_clz_m; \ |
| 176 | u4_codeIntValOffset_m = (u4_codeIntValOffset_m << u4_clz_m) | read_bits_m; \ |
| 177 | } |
| 178 | |
| 179 | /*****************************************************************************/ |
| 180 | /* Defining a macro for checking if the symbol is MPS*/ |
| 181 | /*****************************************************************************/ |
| 182 | |
| 183 | #define CHECK_IF_LPS(u4_codeIntRange_m,u4_codeIntValOffset_m,u4_symbol_m, \ |
| 184 | u4_codeIntRangeLPS_m,u1_mps_state_m,table_lookup_m) \ |
| 185 | { \ |
| 186 | if(u4_codeIntValOffset_m >= u4_codeIntRange_m) \ |
| 187 | { \ |
| 188 | u4_symbol_m = 1 - u4_symbol_m; \ |
| 189 | u4_codeIntValOffset_m -= u4_codeIntRange_m; \ |
| 190 | u4_codeIntRange_m = u4_codeIntRangeLPS_m; \ |
| 191 | u1_mps_state_m = (table_lookup_m >> 15) & 0x7F; \ |
| 192 | } \ |
| 193 | } |
| 194 | |
| 195 | /*! |
| 196 | ************************************************************************** |
| 197 | * \if Function name : DECODE_ONE_BIN_MACRO \endif |
| 198 | * |
| 199 | * \brief |
| 200 | * This function implements decoding process of a decision as defined |
| 201 | * in 9.3.3.2.2. |
| 202 | * |
| 203 | * \return |
| 204 | * Returns symbol decoded. |
| 205 | * |
| 206 | * \note |
| 207 | * It is specified in 9.3.3.2.3.2 that, one of the input to this function |
| 208 | * is CtxIdx. CtxIdx is used to identify state and MPS of that context |
| 209 | * (Refer Fig 9.11 - Flowchart for encoding a decision). To suffice that |
| 210 | * here we pass a pointer bin_ctxt_model_t which contains these values. |
| 211 | * |
| 212 | ************************************************************************** |
| 213 | */ |
| 214 | |
| 215 | #define DECODE_ONE_BIN_MACRO(p_binCtxt_arg ,u4_code_int_range,u4_code_int_val_ofst, \ |
| 216 | pu4_table_arg, \ |
| 217 | p_DecBitStream_arg,u4_symbol) \ |
| 218 | { \ |
| 219 | bin_ctxt_model_t *p_binCtxt_m = (bin_ctxt_model_t *) p_binCtxt_arg; \ |
| 220 | dec_bit_stream_t *p_DecBitStream_m = (dec_bit_stream_t *) p_DecBitStream_arg; \ |
| 221 | const UWORD32 *pu4_table_m = (const UWORD32 *) pu4_table_arg; \ |
| 222 | \ |
| 223 | UWORD32 u4_quantCodeIntRange_m,u4_codeIntRangeLPS_m; \ |
| 224 | UWORD32 u1_mps_state_m; \ |
| 225 | UWORD32 table_lookup_m; \ |
| 226 | UWORD32 u4_clz_m; \ |
| 227 | \ |
| 228 | u1_mps_state_m = (p_binCtxt_m->u1_mps_state); \ |
| 229 | u4_clz_m = CLZ(u4_code_int_range); \ |
| 230 | u4_quantCodeIntRange_m = u4_code_int_range << u4_clz_m; \ |
| 231 | u4_quantCodeIntRange_m = (u4_quantCodeIntRange_m >> 29) & 0x3; \ |
| 232 | table_lookup_m = pu4_table_m[(u1_mps_state_m << 2)+u4_quantCodeIntRange_m]; \ |
| 233 | u4_codeIntRangeLPS_m = table_lookup_m & 0xff; \ |
| 234 | \ |
| 235 | u4_codeIntRangeLPS_m = u4_codeIntRangeLPS_m << (23 - u4_clz_m); \ |
| 236 | u4_code_int_range = u4_code_int_range - u4_codeIntRangeLPS_m; \ |
| 237 | u4_symbol = ((u1_mps_state_m>> 6) & 0x1); \ |
| 238 | /*if mps*/ \ |
| 239 | u1_mps_state_m = (table_lookup_m >> 8) & 0x7F; \ |
| 240 | if(u4_code_int_val_ofst >= u4_code_int_range) \ |
| 241 | { \ |
| 242 | \ |
| 243 | u4_symbol = 1 - u4_symbol; \ |
| 244 | u4_code_int_val_ofst -= u4_code_int_range; \ |
| 245 | u4_code_int_range = u4_codeIntRangeLPS_m; \ |
| 246 | u1_mps_state_m = (table_lookup_m >> 15) & 0x7F; \ |
| 247 | } \ |
| 248 | if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_8) \ |
| 249 | { \ |
| 250 | UWORD32 *pu4_buffer,u4_offset; \ |
| 251 | UWORD32 read_bits,u4_clz_m ; \ |
| 252 | \ |
| 253 | pu4_buffer = p_DecBitStream_m->pu4_buffer; \ |
| 254 | u4_offset = p_DecBitStream_m->u4_ofst; \ |
| 255 | u4_clz_m = CLZ(u4_code_int_range); \ |
| 256 | NEXTBITS(read_bits,(u4_offset+23),pu4_buffer,u4_clz_m) \ |
| 257 | FLUSHBITS(u4_offset,(u4_clz_m)) \ |
| 258 | u4_code_int_range = u4_code_int_range << u4_clz_m; \ |
| 259 | u4_code_int_val_ofst= (u4_code_int_val_ofst << u4_clz_m) | read_bits; \ |
| 260 | \ |
| 261 | \ |
| 262 | p_DecBitStream_m->u4_ofst = u4_offset; \ |
| 263 | } \ |
| 264 | p_binCtxt_m->u1_mps_state = u1_mps_state_m; \ |
| 265 | } |
| 266 | |
| 267 | #endif /* _IH264D_CABAC_H_ */ |