1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11
12 #include "type_aliases.h"
13 #include "blockd.h"
14 #include "onyxd_int.h"
15 #include "vpx_mem/vpx_mem.h"
16 #include "vpx_ports/mem.h"
17 #include "detokenize.h"
18
19 #define BOOL_DATA UINT8
20
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
34
35 /*
36 //the definition is put in "onyxd_int.h"
37 typedef struct
38 {
39 INT16 min_val;
40 INT16 Length;
41 UINT8 Probs[12];
42 } TOKENEXTRABITS;
43 */
44
45 DECLARE_ALIGNED(16, static const TOKENEXTRABITS, vp8d_token_extra_bits2[MAX_ENTROPY_TOKENS]) =
46 {
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 */
59 };
60
61
vp8_reset_mb_tokens_context(MACROBLOCKD * x)62 void vp8_reset_mb_tokens_context(MACROBLOCKD *x)
63 {
64 /* Clear entropy contexts for Y2 blocks */
65 if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
66 {
67 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
68 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
69 }
70 else
71 {
72 vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
73 vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
74 }
75 }
76
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.
80 */
81 DECLARE_ALIGNED(16, const UINT8, vp8_block2leftabove[25*2]) =
82 {
83 /* vp8_block2left */
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,
85 /* vp8_block2above */
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
87 };
88
vp8_init_detokenizer(VP8D_COMP * dx)89 void vp8_init_detokenizer(VP8D_COMP *dx)
90 {
91 const VP8_COMMON *const oc = & dx->common;
92 MACROBLOCKD *x = & dx->mb;
93
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];
100
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]);
105 }
106 #endif
107
108 DECLARE_ALIGNED(16, extern const unsigned char, vp8dx_bitreader_norm[256]);
109 #define FILL \
110 if(count < 0) \
111 VP8DX_BOOL_DECODER_FILL(count, value, bufptr, bufend);
112
113 #define NORMALIZE \
114 /*if(range < 0x80)*/ \
115 { \
116 shift = vp8dx_bitreader_norm[range]; \
117 range <<= shift; \
118 value <<= shift; \
119 count -= shift; \
120 }
121
122 #define DECODE_AND_APPLYSIGN(value_to_sign) \
123 split = (range + 1) >> 1; \
124 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
125 FILL \
126 if ( value < bigsplit ) \
127 { \
128 range = split; \
129 v= value_to_sign; \
130 } \
131 else \
132 { \
133 range = range-split; \
134 value = value-bigsplit; \
135 v = -value_to_sign; \
136 } \
137 range +=range; \
138 value +=value; \
139 count--;
140
141 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \
142 { \
143 split = 1 + ((( probability*(range-1) ) )>> 8); \
144 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
145 FILL \
146 if ( value < bigsplit ) \
147 { \
148 range = split; \
149 NORMALIZE \
150 goto branch; \
151 } \
152 value -= bigsplit; \
153 range = range - split; \
154 NORMALIZE \
155 }
156
157 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \
158 { \
159 split = 1 + ((( probability*(range-1) ) ) >> 8); \
160 bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
161 FILL \
162 if ( value < bigsplit ) \
163 { \
164 range = split; \
165 NORMALIZE \
166 Prob = coef_probs; \
167 if(c<15) {\
168 ++c; \
169 Prob += vp8_coef_bands_x[c]; \
170 goto branch; \
171 } goto BLOCK_FINISHED; /*for malformed input */\
172 } \
173 value -= bigsplit; \
174 range = range - split; \
175 NORMALIZE \
176 }
177
178 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \
179 DECODE_AND_APPLYSIGN(val) \
180 Prob = coef_probs + (ENTROPY_NODES*2); \
181 if(c < 15){\
182 qcoeff_ptr [ scan[c] ] = (INT16) v; \
183 ++c; \
184 goto DO_WHILE; }\
185 qcoeff_ptr [ scan[15] ] = (INT16) v; \
186 goto BLOCK_FINISHED;
187
188
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); \
192 FILL \
193 if(value >= bigsplit)\
194 {\
195 range = range-split;\
196 value = value-bigsplit;\
197 val += ((UINT16)1<<bits_count);\
198 }\
199 else\
200 {\
201 range = split;\
202 }\
203 NORMALIZE
204
205 #if CONFIG_ARM_ASM_DETOK
vp8_decode_mb_tokens(VP8D_COMP * dx,MACROBLOCKD * x)206 int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x)
207 {
208 int eobtotal = 0;
209 int i, type;
210
211 dx->detoken.current_bc = x->current_bc;
212 dx->detoken.A = x->above_context;
213 dx->detoken.L = x->left_context;
214
215 type = 3;
216
217 if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
218 {
219 type = 1;
220 eobtotal -= 16;
221 }
222
223 vp8_decode_mb_tokens_v6(&dx->detoken, type);
224
225 for (i = 0; i < 25; i++)
226 {
227 x->eobs[i] = dx->detoken.eob[i];
228 eobtotal += dx->detoken.eob[i];
229 }
230
231 return eobtotal;
232 }
233 #else
vp8_decode_mb_tokens(VP8D_COMP * dx,MACROBLOCKD * x)234 int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x)
235 {
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;
239
240 BOOL_DECODER *bc = x->current_bc;
241
242 char *eobs = x->eobs;
243
244 ENTROPY_CONTEXT *a;
245 ENTROPY_CONTEXT *l;
246 int i;
247
248 int eobtotal = 0;
249
250 register int count;
251
252 const BOOL_DATA *bufptr;
253 const BOOL_DATA *bufend;
254 register unsigned int range;
255 VP8_BD_VALUE value;
256 const int *scan;
257 register unsigned int shift;
258 UINT32 split;
259 VP8_BD_VALUE bigsplit;
260 INT16 *qcoeff_ptr;
261
262 const vp8_prob *coef_probs;
263 int type;
264 int stop;
265 INT16 val, bits_count;
266 INT16 c;
267 INT16 v;
268 const vp8_prob *Prob;
269
270 type = 3;
271 i = 0;
272 stop = 16;
273
274 scan = vp8_default_zig_zag1d;
275 qcoeff_ptr = &x->qcoeff[0];
276
277 if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
278 {
279 i = 24;
280 stop = 24;
281 type = 1;
282 qcoeff_ptr += 24*16;
283 eobtotal -= 16;
284 }
285
286 bufend = bc->user_buffer_end;
287 bufptr = bc->user_buffer;
288 value = bc->value;
289 count = bc->count;
290 range = bc->range;
291
292
293 coef_probs = oc->fc.coef_probs [type] [ 0 ] [0];
294
295 BLOCK_LOOP:
296 a = A + vp8_block2above[i];
297 l = L + vp8_block2left[i];
298
299 c = (INT16)(!type);
300
301 /*Dest = ((A)!=0) + ((B)!=0);*/
302 VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
303 Prob = coef_probs;
304 Prob += v * ENTROPY_NODES;
305
306 DO_WHILE:
307 Prob += vp8_coef_bands_x[c];
308 DECODE_AND_BRANCH_IF_ZERO(Prob[EOB_CONTEXT_NODE], BLOCK_FINISHED);
309
310 CHECK_0_:
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;
319
320 do
321 {
322 DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count);
323 bits_count -- ;
324 }
325 while (bits_count >= 0);
326
327 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
328
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);
337
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);
346
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);
353
354 HIGH_LOW_CONTEXT_NODE_0_:
355 DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE], CAT_ONE_CONTEXT_NODE_0_);
356
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);
361
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);
366
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);
371
372 THREE_CONTEXT_NODE_0_:
373 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3);
374
375 TWO_CONTEXT_NODE_0_:
376 DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2);
377
378 ONE_CONTEXT_NODE_0_:
379 DECODE_AND_APPLYSIGN(1);
380 Prob = coef_probs + ENTROPY_NODES;
381
382 if (c < 15)
383 {
384 qcoeff_ptr [ scan[c] ] = (INT16) v;
385 ++c;
386 goto DO_WHILE;
387 }
388
389 qcoeff_ptr [ scan[15] ] = (INT16) v;
390 BLOCK_FINISHED:
391 *a = *l = ((eobs[i] = c) != !type); /* any nonzero data? */
392 eobtotal += c;
393 qcoeff_ptr += 16;
394
395 i++;
396
397 if (i < stop)
398 goto BLOCK_LOOP;
399
400 if (i == 25)
401 {
402 type = 0;
403 i = 0;
404 stop = 16;
405 coef_probs = oc->fc.coef_probs [type] [ 0 ] [0];
406 qcoeff_ptr -= (24*16 + 16);
407 goto BLOCK_LOOP;
408 }
409
410 if (i == 16)
411 {
412 type = 2;
413 coef_probs = oc->fc.coef_probs [type] [ 0 ] [0];
414 stop = 24;
415 goto BLOCK_LOOP;
416 }
417
418 FILL
419 bc->user_buffer = bufptr;
420 bc->value = value;
421 bc->count = count;
422 bc->range = range;
423 return eobtotal;
424
425 }
426 #endif /*!CONFIG_ASM_DETOK*/
427