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 "type_aliases.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, UINT8
, vp8_coef_bands_x
[16]) = { 0, 1 * OCB_X
, 2 * OCB_X
, 3 * OCB_X
, 6 * OCB_X
, 4 * OCB_X
, 5 * OCB_X
, 6 * OCB_X
, 6 * OCB_X
, 6 * OCB_X
, 6 * OCB_X
, 6 * OCB_X
, 6 * OCB_X
, 6 * OCB_X
, 6 * OCB_X
, 7 * OCB_X
};
23 #define EOB_CONTEXT_NODE 0
24 #define ZERO_CONTEXT_NODE 1
25 #define ONE_CONTEXT_NODE 2
26 #define LOW_VAL_CONTEXT_NODE 3
27 #define TWO_CONTEXT_NODE 4
28 #define THREE_CONTEXT_NODE 5
29 #define HIGH_LOW_CONTEXT_NODE 6
30 #define CAT_ONE_CONTEXT_NODE 7
31 #define CAT_THREEFOUR_CONTEXT_NODE 8
32 #define CAT_THREE_CONTEXT_NODE 9
33 #define CAT_FIVE_CONTEXT_NODE 10
36 //the definition is put in "onyxd_int.h"
45 DECLARE_ALIGNED(16, static const TOKENEXTRABITS
, vp8d_token_extra_bits2
[MAX_ENTROPY_TOKENS
]) =
47 { 0, -1, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* ZERO_TOKEN */
48 { 1, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* ONE_TOKEN */
49 { 2, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* TWO_TOKEN */
50 { 3, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* THREE_TOKEN */
51 { 4, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* FOUR_TOKEN */
52 { 5, 0, { 159, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY1 */
53 { 7, 1, { 145, 165, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY2 */
54 { 11, 2, { 140, 148, 173, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY3 */
55 { 19, 3, { 135, 140, 155, 176, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY4 */
56 { 35, 4, { 130, 134, 141, 157, 180, 0, 0, 0, 0, 0, 0, 0 } }, /* DCT_VAL_CATEGORY5 */
57 { 67, 10, { 129, 130, 133, 140, 153, 177, 196, 230, 243, 254, 254, 0 } }, /* DCT_VAL_CATEGORY6 */
58 { 0, -1, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, /* EOB TOKEN */
62 void vp8_reset_mb_tokens_context(MACROBLOCKD
*x
)
64 /* Clear entropy contexts for Y2 blocks */
65 if (x
->mode_info_context
->mbmi
.mode
!= B_PRED
&& x
->mode_info_context
->mbmi
.mode
!= SPLITMV
)
67 vpx_memset(x
->above_context
, 0, sizeof(ENTROPY_CONTEXT_PLANES
));
68 vpx_memset(x
->left_context
, 0, sizeof(ENTROPY_CONTEXT_PLANES
));
72 vpx_memset(x
->above_context
, 0, sizeof(ENTROPY_CONTEXT_PLANES
)-1);
73 vpx_memset(x
->left_context
, 0, sizeof(ENTROPY_CONTEXT_PLANES
)-1);
77 #if CONFIG_ARM_ASM_DETOK
78 /* mashup of vp8_block2left and vp8_block2above so we only need one pointer
79 * for the assembly version.
81 DECLARE_ALIGNED(16, const UINT8
, vp8_block2leftabove
[25*2]) =
84 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
86 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8
89 void vp8_init_detokenizer(VP8D_COMP
*dx
)
91 const VP8_COMMON
*const oc
= & dx
->common
;
92 MACROBLOCKD
*x
= & dx
->mb
;
94 dx
->detoken
.vp8_coef_tree_ptr
= vp8_coef_tree
;
95 dx
->detoken
.ptr_block2leftabove
= vp8_block2leftabove
;
96 dx
->detoken
.ptr_coef_bands_x
= vp8_coef_bands_x
;
97 dx
->detoken
.scan
= vp8_default_zig_zag1d
;
98 dx
->detoken
.teb_base_ptr
= vp8d_token_extra_bits2
;
99 dx
->detoken
.qcoeff_start_ptr
= &x
->qcoeff
[0];
101 dx
->detoken
.coef_probs
[0] = (oc
->fc
.coef_probs
[0] [ 0 ] [0]);
102 dx
->detoken
.coef_probs
[1] = (oc
->fc
.coef_probs
[1] [ 0 ] [0]);
103 dx
->detoken
.coef_probs
[2] = (oc
->fc
.coef_probs
[2] [ 0 ] [0]);
104 dx
->detoken
.coef_probs
[3] = (oc
->fc
.coef_probs
[3] [ 0 ] [0]);
108 DECLARE_ALIGNED(16, extern const unsigned char, vp8dx_bitreader_norm
[256]);
111 VP8DX_BOOL_DECODER_FILL(count, value, bufptr, bufend);
114 /*if(range < 0x80)*/ \
116 shift = vp8dx_bitreader_norm[range]; \
122 #define DECODE_AND_APPLYSIGN(value_to_sign) \
123 split = (range + 1) >> 1; \
124 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
126 if ( value < bigsplit ) \
133 range = range-split; \
134 value = value-bigsplit; \
135 v = -value_to_sign; \
141 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \
143 split = 1 + ((( probability*(range-1) ) )>> 8); \
144 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
146 if ( value < bigsplit ) \
153 range = range - split; \
157 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \
159 split = 1 + ((( probability*(range-1) ) ) >> 8); \
160 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
162 if ( value < bigsplit ) \
169 Prob += vp8_coef_bands_x[c]; \
171 } goto BLOCK_FINISHED; /*for malformed input */\
174 range = range - split; \
178 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \
179 DECODE_AND_APPLYSIGN(val) \
180 Prob = coef_probs + (ENTROPY_NODES*2); \
182 qcoeff_ptr [ scan[c] ] = (INT16) v; \
185 qcoeff_ptr [ scan[15] ] = (INT16) v; \
189 #define DECODE_EXTRABIT_AND_ADJUST_VAL(t,bits_count)\
190 split = 1 + (((range-1) * vp8d_token_extra_bits2[t].Probs[bits_count]) >> 8); \
191 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
193 if(value >= bigsplit)\
195 range = range-split;\
196 value = value-bigsplit;\
197 val += ((UINT16)1<<bits_count);\
205 #if CONFIG_ARM_ASM_DETOK
206 int vp8_decode_mb_tokens(VP8D_COMP
*dx
, MACROBLOCKD
*x
)
211 dx
->detoken
.current_bc
= x
->current_bc
;
212 dx
->detoken
.A
= x
->above_context
;
213 dx
->detoken
.L
= x
->left_context
;
217 if (x
->mode_info_context
->mbmi
.mode
!= B_PRED
&& x
->mode_info_context
->mbmi
.mode
!= SPLITMV
)
223 vp8_decode_mb_tokens_v6(&dx
->detoken
, type
);
225 for (i
= 0; i
< 25; i
++)
227 x
->eobs
[i
] = dx
->detoken
.eob
[i
];
228 eobtotal
+= dx
->detoken
.eob
[i
];
234 int vp8_decode_mb_tokens(VP8D_COMP
*dx
, MACROBLOCKD
*x
)
236 ENTROPY_CONTEXT
*A
= (ENTROPY_CONTEXT
*)x
->above_context
;
237 ENTROPY_CONTEXT
*L
= (ENTROPY_CONTEXT
*)x
->left_context
;
238 const VP8_COMMON
*const oc
= & dx
->common
;
240 BOOL_DECODER
*bc
= x
->current_bc
;
242 char *eobs
= x
->eobs
;
252 const BOOL_DATA
*bufptr
;
253 const BOOL_DATA
*bufend
;
254 register unsigned int range
;
257 register unsigned int shift
;
259 VP8_BD_VALUE bigsplit
;
262 const vp8_prob
*coef_probs
;
265 INT16 val
, bits_count
;
268 const vp8_prob
*Prob
;
274 scan
= vp8_default_zig_zag1d
;
275 qcoeff_ptr
= &x
->qcoeff
[0];
277 if (x
->mode_info_context
->mbmi
.mode
!= B_PRED
&& x
->mode_info_context
->mbmi
.mode
!= SPLITMV
)
286 bufend
= bc
->user_buffer_end
;
287 bufptr
= bc
->user_buffer
;
293 coef_probs
= oc
->fc
.coef_probs
[type
] [ 0 ] [0];
296 a
= A
+ vp8_block2above
[i
];
297 l
= L
+ vp8_block2left
[i
];
301 /*Dest = ((A)!=0) + ((B)!=0);*/
302 VP8_COMBINEENTROPYCONTEXTS(v
, *a
, *l
);
304 Prob
+= v
* ENTROPY_NODES
;
307 Prob
+= vp8_coef_bands_x
[c
];
308 DECODE_AND_BRANCH_IF_ZERO(Prob
[EOB_CONTEXT_NODE
], BLOCK_FINISHED
);
311 DECODE_AND_LOOP_IF_ZERO(Prob
[ZERO_CONTEXT_NODE
], CHECK_0_
);
312 DECODE_AND_BRANCH_IF_ZERO(Prob
[ONE_CONTEXT_NODE
], ONE_CONTEXT_NODE_0_
);
313 DECODE_AND_BRANCH_IF_ZERO(Prob
[LOW_VAL_CONTEXT_NODE
], LOW_VAL_CONTEXT_NODE_0_
);
314 DECODE_AND_BRANCH_IF_ZERO(Prob
[HIGH_LOW_CONTEXT_NODE
], HIGH_LOW_CONTEXT_NODE_0_
);
315 DECODE_AND_BRANCH_IF_ZERO(Prob
[CAT_THREEFOUR_CONTEXT_NODE
], CAT_THREEFOUR_CONTEXT_NODE_0_
);
316 DECODE_AND_BRANCH_IF_ZERO(Prob
[CAT_FIVE_CONTEXT_NODE
], CAT_FIVE_CONTEXT_NODE_0_
);
317 val
= vp8d_token_extra_bits2
[DCT_VAL_CATEGORY6
].min_val
;
318 bits_count
= vp8d_token_extra_bits2
[DCT_VAL_CATEGORY6
].Length
;
322 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6
, bits_count
);
325 while (bits_count
>= 0);
327 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val
);
329 CAT_FIVE_CONTEXT_NODE_0_
:
330 val
= vp8d_token_extra_bits2
[DCT_VAL_CATEGORY5
].min_val
;
331 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5
, 4);
332 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5
, 3);
333 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5
, 2);
334 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5
, 1);
335 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY5
, 0);
336 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val
);
338 CAT_THREEFOUR_CONTEXT_NODE_0_
:
339 DECODE_AND_BRANCH_IF_ZERO(Prob
[CAT_THREE_CONTEXT_NODE
], CAT_THREE_CONTEXT_NODE_0_
);
340 val
= vp8d_token_extra_bits2
[DCT_VAL_CATEGORY4
].min_val
;
341 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4
, 3);
342 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4
, 2);
343 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4
, 1);
344 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY4
, 0);
345 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val
);
347 CAT_THREE_CONTEXT_NODE_0_
:
348 val
= vp8d_token_extra_bits2
[DCT_VAL_CATEGORY3
].min_val
;
349 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3
, 2);
350 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3
, 1);
351 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY3
, 0);
352 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val
);
354 HIGH_LOW_CONTEXT_NODE_0_
:
355 DECODE_AND_BRANCH_IF_ZERO(Prob
[CAT_ONE_CONTEXT_NODE
], CAT_ONE_CONTEXT_NODE_0_
);
357 val
= vp8d_token_extra_bits2
[DCT_VAL_CATEGORY2
].min_val
;
358 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2
, 1);
359 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY2
, 0);
360 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val
);
362 CAT_ONE_CONTEXT_NODE_0_
:
363 val
= vp8d_token_extra_bits2
[DCT_VAL_CATEGORY1
].min_val
;
364 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY1
, 0);
365 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val
);
367 LOW_VAL_CONTEXT_NODE_0_
:
368 DECODE_AND_BRANCH_IF_ZERO(Prob
[TWO_CONTEXT_NODE
], TWO_CONTEXT_NODE_0_
);
369 DECODE_AND_BRANCH_IF_ZERO(Prob
[THREE_CONTEXT_NODE
], THREE_CONTEXT_NODE_0_
);
370 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(4);
372 THREE_CONTEXT_NODE_0_
:
373 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3);
376 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2);
379 DECODE_AND_APPLYSIGN(1);
380 Prob
= coef_probs
+ ENTROPY_NODES
;
384 qcoeff_ptr
[ scan
[c
] ] = (INT16
) v
;
389 qcoeff_ptr
[ scan
[15] ] = (INT16
) v
;
391 *a
= *l
= ((eobs
[i
] = c
) != !type
); /* any nonzero data? */
405 coef_probs
= oc
->fc
.coef_probs
[type
] [ 0 ] [0];
406 qcoeff_ptr
-= (24*16 + 16);
413 coef_probs
= oc
->fc
.coef_probs
[type
] [ 0 ] [0];
419 bc
->user_buffer
= bufptr
;
426 #endif /*!CONFIG_ASM_DETOK*/