2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #include "vp8/common/type_aliases.h"
13 #include "vp8/common/blockd.h"
14 #include "onyxd_int.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vpx_ports/mem.h"
17 #include "detokenize.h"
19 #define BOOL_DATA UINT8
21 #define OCB_X PREV_COEF_CONTEXTS * ENTROPY_NODES
22 DECLARE_ALIGNED(16, static const unsigned char, coef_bands_x[16]) =
24 0 * OCB_X, 1 * OCB_X, 2 * OCB_X, 3 * OCB_X,
25 6 * OCB_X, 4 * OCB_X, 5 * OCB_X, 6 * OCB_X,
26 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 6 * OCB_X,
27 6 * OCB_X, 6 * OCB_X, 6 * OCB_X, 7 * OCB_X
29 #define EOB_CONTEXT_NODE 0
30 #define ZERO_CONTEXT_NODE 1
31 #define ONE_CONTEXT_NODE 2
32 #define LOW_VAL_CONTEXT_NODE 3
33 #define TWO_CONTEXT_NODE 4
34 #define THREE_CONTEXT_NODE 5
35 #define HIGH_LOW_CONTEXT_NODE 6
36 #define CAT_ONE_CONTEXT_NODE 7
37 #define CAT_THREEFOUR_CONTEXT_NODE 8
38 #define CAT_THREE_CONTEXT_NODE 9
39 #define CAT_FIVE_CONTEXT_NODE 10
42 //the definition is put in "onyxd_int.h"
51 DECLARE_ALIGNED(16, static const TOKENEXTRABITS, vp8d_token_extra_bits2[MAX_ENTROPY_TOKENS]) =
53 { 0, -1, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* ZERO_TOKEN */
54 { 1, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* ONE_TOKEN */
55 { 2, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* TWO_TOKEN */
56 { 3, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* THREE_TOKEN */
57 { 4, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* FOUR_TOKEN */
58 { 5, 0, { 159, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY1 */
59 { 7, 1, { 145, 165, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY2 */
60 { 11, 2, { 140, 148, 173, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY3 */
61 { 19, 3, { 135, 140, 155, 176, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY4 */
62 { 35, 4, { 130, 134, 141, 157, 180, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY5 */
63 { 67, 10, { 129, 130, 133, 140, 153, 177, 196, 230, 243, 254, 254, 0 } }, /* DCT_VAL_CATEGORY6 */
64 { 0, -1, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* EOB TOKEN */
68 void vp8_reset_mb_tokens_context(MACROBLOCKD *x)
70 /* Clear entropy contexts for Y2 blocks */
71 if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
73 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
74 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
78 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
79 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
83 DECLARE_ALIGNED(16, extern const unsigned char, vp8dx_bitreader_norm[256]);
86 VP8DX_BOOL_DECODER_FILL(count, value, bufptr, bufend);
89 /*if(range < 0x80)*/ \
91 shift = vp8dx_bitreader_norm[range]; \
97 #define DECODE_AND_APPLYSIGN(value_to_sign) \
98 split = (range + 1) >> 1; \
99 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
101 if ( value < bigsplit ) \
108 range = range-split; \
109 value = value-bigsplit; \
110 v = -value_to_sign; \
116 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \
118 split = 1 + ((( probability*(range-1) ) )>> 8); \
119 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
121 if ( value < bigsplit ) \
128 range = range - split; \
132 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \
134 split = 1 + ((( probability*(range-1) ) ) >> 8); \
135 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
137 if ( value < bigsplit ) \
144 Prob += coef_bands_x[c]; \
146 } goto BLOCK_FINISHED; /*for malformed input */\
149 range = range - split; \
153 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \
154 DECODE_AND_APPLYSIGN(val) \
155 Prob = coef_probs + (ENTROPY_NODES*2); \
157 qcoeff_ptr [ scan[c] ] = (INT16) v; \
160 qcoeff_ptr [ scan[15] ] = (INT16) v; \
164 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\
165 split = 1 + (((range-1) * vp8d_token_extra_bits2[t].Probs[bits_count]) >> 8); \
166 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
168 if(value >= bigsplit)\
170 range = range-split;\
171 value = value-bigsplit;\
172 val += ((UINT16)1<<bits_count);\
180 int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x)
182 ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)x->above_context;
183 ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)x->left_context;
184 const FRAME_CONTEXT * const fc = &dx->common.fc;
186 BOOL_DECODER *bc = x->current_bc;
188 char *eobs = x->eobs;
198 const BOOL_DATA *bufptr;
199 const BOOL_DATA *bufend;
200 register unsigned int range;
203 register unsigned int shift;
205 VP8_BD_VALUE bigsplit;
208 const vp8_prob *coef_probs;
211 INT16 val, bits_count;
214 const vp8_prob *Prob;
220 scan = vp8_default_zig_zag1d;
221 qcoeff_ptr = &x->qcoeff[0];
223 if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
232 bufend = bc->user_buffer_end;
233 bufptr = bc->user_buffer;
239 coef_probs = fc->coef_probs [type] [ 0 ] [0];
242 a = A + vp8_block2above[i];
243 l = L + vp8_block2left[i];
247 /*Dest = ((A)!=0) + ((B)!=0);*/
248 VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
250 Prob += v * ENTROPY_NODES;
253 Prob += coef_bands_x[c];
254 DECODE_AND_BRANCH_IF_ZERO(Prob[EOB_CONTEXT_NODE], BLOCK_FINISHED);
257 DECODE_AND_LOOP_IF_ZERO(Prob[ZERO_CONTEXT_NODE], CHECK_0_);
258 DECODE_AND_BRANCH_IF_ZERO(Prob[ONE_CONTEXT_NODE], ONE_CONTEXT_NODE_0_);
259 DECODE_AND_BRANCH_IF_ZERO(Prob[LOW_VAL_CONTEXT_NODE], LOW_VAL_CONTEXT_NODE_0_);
260 DECODE_AND_BRANCH_IF_ZERO(Prob[HIGH_LOW_CONTEXT_NODE], HIGH_LOW_CONTEXT_NODE_0_);
261 DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREEFOUR_CONTEXT_NODE], CAT_THREEFOUR_CONTEXT_NODE_0_);
262 DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_FIVE_CONTEXT_NODE], CAT_FIVE_CONTEXT_NODE_0_);
263 val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY6].min_val;
264 bits_count = vp8d_token_extra_bits2[DCT_VAL_CATEGORY6].Length;
268 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count);
271 while (bits_count >= 0);
273 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
275 CAT_FIVE_CONTEXT_NODE_0_:
276 val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY5].min_val;
277 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 4);
278 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 3);
279 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 2);
280 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 1);
281 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5, 0);
282 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
284 CAT_THREEFOUR_CONTEXT_NODE_0_:
285 DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_THREE_CONTEXT_NODE], CAT_THREE_CONTEXT_NODE_0_);
286 val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY4].min_val;
287 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 3);
288 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 2);
289 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 1);
290 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4, 0);
291 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
293 CAT_THREE_CONTEXT_NODE_0_:
294 val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY3].min_val;
295 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 2);
296 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 1);
297 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3, 0);
298 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
300 HIGH_LOW_CONTEXT_NODE_0_:
301 DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE], CAT_ONE_CONTEXT_NODE_0_);
303 val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY2].min_val;
304 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 1);
305 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2, 0);
306 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
308 CAT_ONE_CONTEXT_NODE_0_:
309 val = vp8d_token_extra_bits2[DCT_VAL_CATEGORY1].min_val;
310 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1, 0);
311 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
313 LOW_VAL_CONTEXT_NODE_0_:
314 DECODE_AND_BRANCH_IF_ZERO(Prob[TWO_CONTEXT_NODE], TWO_CONTEXT_NODE_0_);
315 DECODE_AND_BRANCH_IF_ZERO(Prob[THREE_CONTEXT_NODE], THREE_CONTEXT_NODE_0_);
316 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4);
318 THREE_CONTEXT_NODE_0_:
319 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3);
322 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2);
325 DECODE_AND_APPLYSIGN(1);
326 Prob = coef_probs + ENTROPY_NODES;
330 qcoeff_ptr [ scan[c] ] = (INT16) v;
335 qcoeff_ptr [ scan[15] ] = (INT16) v;
337 *a = *l = ((eobs[i] = c) != !type); /* any nonzero data? */
351 coef_probs = fc->coef_probs [type] [ 0 ] [0];
352 qcoeff_ptr -= (24*16 + 16);
359 coef_probs = fc->coef_probs [type] [ 0 ] [0];
365 bc->user_buffer = bufptr;