summaryrefslogtreecommitdiffstats
path: root/decoder/ih264d_cabac.h
blob: 6ee3d52da871baf3c4e310cbbee2fa7282efaa8d (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
/******************************************************************************
 *
 * 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 ih264d_cabac.h
 *
 * \brief
 *    This file contains declarations of Binary decoding routines and tables.
 *
 * \date
 *    04/02/2003
 *
 * \author  NS
 ***************************************************************************
 */

#ifndef _IH264D_CABAC_H_
#define _IH264D_CABAC_H_

#include "ih264_typedefs.h"
#include "ih264_macros.h"
#include "ih264_platform_macros.h"
#include "ih264d_bitstrm.h"
#include "ih264d_defs.h"

#define   B_BITS    10

#define   HALF      (1 << (B_BITS-1))
#define   QUARTER   (1 << (B_BITS-2))

#define CTXT_UNUSED   {0,64}
#define NUM_MB_SKIP_CTXT  6
#define NUM_MB_TYPE_CTXT  9
#define NUM_SUBMB_TYPE_CTXT 7
#define NUM_REF_IDX_CTXT  6
#define NUM_MB_QP_DELTA 4
#define NUM_PRED_MODE 6
#define NUM_MB_FIELD    3
#define NUM_CBP 12
#define NUM_CTX_MVD 14

/* Residual block cabac context parameters */
#define NUM_CTX_CAT 6
#define NUM_LUMA_CTX_CAT 3
#define NUM_CTX_CODED_BLOCK 4
/* Luma CtxSigCoeff + CtxLastCoeff = 15 + 15 = 30 */
#define NUM_LUMA_CTX_SIG_COEF 30
/* Chroma DC CtxSigCoeff + CtxLastCoeff = 3 + 3 = 6 */
#define NUM_CTX_CHROMA_DC_SIG_COEF 6
/* Chroma AC CtxSigCoeff + CtxLastCoeff = 14 + 14 = 28 */
#define NUM_CTX_CHROMA_AC_SIG_COEF 28
#define NUM_CTX_ABS_LEVEL 10

#define LUMA_DC_CTXCAT    0
#define LUMA_AC_CTXCAT    1
#define LUMA_4X4_CTXCAT   2
#define CHROMA_DC_CTXCAT  3
#define CHROMA_AC_CTXCAT  4
#define LUMA_8X8_CTXCAT   5

/*****************************************************************************/
/* Constant Macros                                                           */
/*****************************************************************************/
#define NUM_CABAC_CTXTS 460
#define QP_RANGE        52
#define NUM_CAB_INIT_IDC_PLUS_ONE 4
#define LAST_COEFF_CTXT_MINUS_SIG_COEFF_CTXT 61
#define LAST_COEFF_CTXT_MINUS_SIG_COEFF_CTXT_8X8 15

/*bits 0 to 5 :state
 bit 6:mps*/
typedef struct
{
    UWORD8 u1_mps_state; /* state number */
} bin_ctxt_model_t;

typedef struct

{
    /* Neighbour availability Variables needed to get CtxtInc, for CABAC */
    UWORD8 u1_mb_type; /** macroblock type: I/P/B/SI/SP */
    UWORD8 u1_cbp; /** Coded Block Pattern */
    UWORD8 u1_intra_chroma_pred_mode;

    /*************************************************************************/
    /*               Arrangnment of DC CSBP                                  */
    /*        bits:  b7  b6  b5  b4  b3  b2  b1  b0                          */
    /*        CSBP:   x   x   x   x   x  Vdc Udc Ydc                         */
    /*************************************************************************/
    UWORD8 u1_yuv_dc_csbp;
    WORD8 i1_ref_idx[4];
    UWORD8 u1_mv[4][4];
    UWORD8 u1_transform8x8_ctxt;
} ctxt_inc_mb_info_t;

#define ONE_RIGHT_SHIFTED_BY_8 1<<8
#define ONE_RIGHT_SHIFTED_BY_9    1<<9
#define ONE_RIGHT_SHIFTED_BY_14 1<<14
typedef struct
{
    UWORD32 u4_code_int_range;
    UWORD32 u4_code_int_val_ofst;
    const void *cabac_table;
    void * pv_codec_handle; /* For Error Handling */
} decoding_envirnoment_t;

WORD32 ih264d_init_cabac_dec_envirnoment(decoding_envirnoment_t * ps_cab_env,
                                       dec_bit_stream_t *ps_bitstrm);

UWORD32 ih264d_decode_bin(UWORD32 u4_ctx_inc,
                          bin_ctxt_model_t *ps_bin_ctxt,
                          dec_bit_stream_t *ps_bitstrm,
                          decoding_envirnoment_t *ps_cab_env);
UWORD8 ih264d_decode_terminate(decoding_envirnoment_t * ps_cab_env,
                               dec_bit_stream_t * ps_bitstrm);

UWORD32 ih264d_decode_bins_tunary(UWORD8 u1_max_bins,
                                  UWORD32 u4_ctx_inc,
                                  bin_ctxt_model_t *ps_src_bin_ctxt,
                                  dec_bit_stream_t *ps_bitstrm,
                                  decoding_envirnoment_t *ps_cab_env);

UWORD32 ih264d_decode_bins(UWORD8 u1_max_bins,
                           UWORD32 u4_ctx_inc,
                           bin_ctxt_model_t *ps_src_bin_ctxt,
                           dec_bit_stream_t *ps_bitstrm,
                           decoding_envirnoment_t *ps_cab_env);
UWORD32 ih264d_decode_bins_unary(UWORD8 u1_max_bins,
                                 UWORD32 u4_ctx_inc,
                                 bin_ctxt_model_t *ps_src_bin_ctxt,
                                 dec_bit_stream_t *ps_bitstrm,
                                 decoding_envirnoment_t *ps_cab_env);

UWORD32 ih264d_decode_bypass_bins_unary(decoding_envirnoment_t *ps_cab_env,
                                        dec_bit_stream_t *ps_bitstrm);

UWORD32 ih264d_decode_bypass_bins(decoding_envirnoment_t *ps_cab_env,
                                  UWORD8 u1_max_bins,
                                  dec_bit_stream_t *ps_bitstrm);

/*****************************************************************************/
/* Function Macros                                                           */
/*****************************************************************************/

/*****************************************************************************/
/* Defining a macro for renormalization*/
/*****************************************************************************/

/*we renormalize every time the number bits(which are read ahead of time) we have
 consumed in the u4_ofst exceeds 23*/

#define RENORM_RANGE_OFFSET(u4_codeIntRange_m,u4_codeIntValOffset_m,u4_offset_m,pu4_buffer_m) \
  {                                                                                         \
    UWORD32 read_bits_m,u4_clz_m  ;                                                         \
    u4_clz_m = CLZ(u4_codeIntRange_m);                                                  \
    NEXTBITS(read_bits_m,(u4_offset_m+23),pu4_buffer_m,u4_clz_m)                            \
    FLUSHBITS(u4_offset_m,(u4_clz_m))                                                       \
    u4_codeIntRange_m = u4_codeIntRange_m << u4_clz_m;                                      \
    u4_codeIntValOffset_m = (u4_codeIntValOffset_m << u4_clz_m) | read_bits_m;              \
  }

/*****************************************************************************/
/* Defining a macro for checking if the symbol is MPS*/
/*****************************************************************************/

#define CHECK_IF_LPS(u4_codeIntRange_m,u4_codeIntValOffset_m,u4_symbol_m,                   \
                    u4_codeIntRangeLPS_m,u1_mps_state_m,table_lookup_m)                     \
{                                                                                         \
  if(u4_codeIntValOffset_m >= u4_codeIntRange_m)                                            \
  {                                                                                         \
      u4_symbol_m = 1 - u4_symbol_m;                                                        \
      u4_codeIntValOffset_m -= u4_codeIntRange_m;                                           \
      u4_codeIntRange_m = u4_codeIntRangeLPS_m;                                             \
      u1_mps_state_m = (table_lookup_m >> 15) & 0x7F;                                       \
  }                                                                                         \
}

/*!
 **************************************************************************
 * \if Function name : DECODE_ONE_BIN_MACRO \endif
 *
 * \brief
 *    This function implements decoding process of a decision as defined
 *    in 9.3.3.2.2.
 *
 * \return
 *    Returns symbol decoded.
 *
 * \note
 *    It is specified in 9.3.3.2.3.2 that, one of the input to this function
 *    is CtxIdx. CtxIdx is used to identify state and MPS of that context
 *    (Refer Fig 9.11 - Flowchart for encoding a decision). To suffice that
 *    here we pass a pointer bin_ctxt_model_t which contains these values.
 *
 **************************************************************************
 */

#define DECODE_ONE_BIN_MACRO(p_binCtxt_arg ,u4_code_int_range,u4_code_int_val_ofst,       \
                     pu4_table_arg,                                                \
                     p_DecBitStream_arg,u4_symbol)                                           \
{                                                                                       \
    bin_ctxt_model_t *p_binCtxt_m = (bin_ctxt_model_t *) p_binCtxt_arg;                           \
    dec_bit_stream_t *p_DecBitStream_m = (dec_bit_stream_t *) p_DecBitStream_arg;                 \
    const UWORD32 *pu4_table_m = (const UWORD32 *) pu4_table_arg;                         \
                                                                                        \
    UWORD32 u4_quantCodeIntRange_m,u4_codeIntRangeLPS_m;                                    \
    UWORD32 u1_mps_state_m;                                                               \
    UWORD32 table_lookup_m;                                                               \
    UWORD32 u4_clz_m;                                                                     \
                                                                                        \
    u1_mps_state_m = (p_binCtxt_m->u1_mps_state);                                           \
    u4_clz_m = CLZ(u4_code_int_range);                                                  \
    u4_quantCodeIntRange_m = u4_code_int_range << u4_clz_m;                                   \
    u4_quantCodeIntRange_m = (u4_quantCodeIntRange_m >> 29) & 0x3;                          \
    table_lookup_m = pu4_table_m[(u1_mps_state_m << 2)+u4_quantCodeIntRange_m];                 \
    u4_codeIntRangeLPS_m = table_lookup_m & 0xff;                                           \
                                                                                        \
    u4_codeIntRangeLPS_m = u4_codeIntRangeLPS_m << (23 - u4_clz_m);                           \
    u4_code_int_range = u4_code_int_range - u4_codeIntRangeLPS_m;                             \
    u4_symbol = ((u1_mps_state_m>> 6) & 0x1);                                             \
    /*if mps*/                                                                          \
    u1_mps_state_m = (table_lookup_m >> 8) & 0x7F;                                          \
    if(u4_code_int_val_ofst >= u4_code_int_range)                                          \
  {                                                                                     \
                                                                                        \
    u4_symbol = 1 - u4_symbol;                                                          \
    u4_code_int_val_ofst -= u4_code_int_range;                                             \
    u4_code_int_range = u4_codeIntRangeLPS_m;                                               \
    u1_mps_state_m = (table_lookup_m >> 15) & 0x7F;                                         \
  }                                                                                     \
    if(u4_code_int_range < ONE_RIGHT_SHIFTED_BY_8)                                        \
    {                                                                                   \
        UWORD32 *pu4_buffer,u4_offset;                                                  \
        UWORD32 read_bits,u4_clz_m  ;                                                     \
                                                                                        \
        pu4_buffer = p_DecBitStream_m->pu4_buffer;                                         \
        u4_offset = p_DecBitStream_m->u4_ofst;                                          \
        u4_clz_m = CLZ(u4_code_int_range);                                              \
        NEXTBITS(read_bits,(u4_offset+23),pu4_buffer,u4_clz_m)                            \
        FLUSHBITS(u4_offset,(u4_clz_m))                                                   \
        u4_code_int_range = u4_code_int_range << u4_clz_m;                                    \
        u4_code_int_val_ofst= (u4_code_int_val_ofst << u4_clz_m) | read_bits;               \
                                                                                        \
                                                                                        \
        p_DecBitStream_m->u4_ofst = u4_offset;                                          \
    }                                                                                   \
    p_binCtxt_m->u1_mps_state = u1_mps_state_m;                                             \
}

#endif  /*  _IH264D_CABAC_H_ */