• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
18 
19 #define BOOL_DATA UINT8
20 
21 #define OCB_X PREV_COEF_CONTEXTS * ENTROPY_NODES
22 DECLARE_ALIGNED(16, static const unsigned char, coef_bands_x[16]) =
23 {
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
28 };
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
40 
41 /*
42 //the definition is put in "onyxd_int.h"
43 typedef struct
44 {
45     INT16         min_val;
46     INT16         Length;
47     UINT8 Probs[12];
48 } TOKENEXTRABITS;
49 */
50 
51 DECLARE_ALIGNED(16, static const TOKENEXTRABITS, vp8d_token_extra_bits2[MAX_ENTROPY_TOKENS]) =
52 {
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 */
65 };
66 
67 
vp8_reset_mb_tokens_context(MACROBLOCKD * x)68 void vp8_reset_mb_tokens_context(MACROBLOCKD *x)
69 {
70     /* Clear entropy contexts for Y2 blocks */
71     if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
72     {
73         vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
74         vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
75     }
76     else
77     {
78         vpx_memset(x->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
79         vpx_memset(x->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
80     }
81 }
82 
83 DECLARE_ALIGNED(16, extern const unsigned char, vp8dx_bitreader_norm[256]);
84 #define FILL \
85     if(count < 0) \
86         VP8DX_BOOL_DECODER_FILL(count, value, bufptr, bufend);
87 
88 #define NORMALIZE \
89     /*if(range < 0x80)*/                            \
90     { \
91         shift = vp8dx_bitreader_norm[range]; \
92         range <<= shift; \
93         value <<= shift; \
94         count -= shift; \
95     }
96 
97 #define DECODE_AND_APPLYSIGN(value_to_sign) \
98     split = (range + 1) >> 1; \
99     bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
100     FILL \
101     if ( value < bigsplit ) \
102     { \
103         range = split; \
104         v= value_to_sign; \
105     } \
106     else \
107     { \
108         range = range-split; \
109         value = value-bigsplit; \
110         v = -value_to_sign; \
111     } \
112     range +=range;                   \
113     value +=value;                   \
114     count--;
115 
116 #define DECODE_AND_BRANCH_IF_ZERO(probability,branch) \
117     { \
118         split = 1 +  ((( probability*(range-1) ) )>> 8); \
119         bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
120         FILL \
121         if ( value < bigsplit ) \
122         { \
123             range = split; \
124             NORMALIZE \
125             goto branch; \
126         } \
127         value -= bigsplit; \
128         range = range - split; \
129         NORMALIZE \
130     }
131 
132 #define DECODE_AND_LOOP_IF_ZERO(probability,branch) \
133     { \
134         split = 1 + ((( probability*(range-1) ) ) >> 8); \
135         bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8); \
136         FILL \
137         if ( value < bigsplit ) \
138         { \
139             range = split; \
140             NORMALIZE \
141             Prob = coef_probs; \
142             if(c<15) {\
143             ++c; \
144             Prob += coef_bands_x[c]; \
145             goto branch; \
146             } goto BLOCK_FINISHED; /*for malformed input */\
147         } \
148         value -= bigsplit; \
149         range = range - split; \
150         NORMALIZE \
151     }
152 
153 #define DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val) \
154     DECODE_AND_APPLYSIGN(val) \
155     Prob = coef_probs + (ENTROPY_NODES*2); \
156     if(c < 15){\
157         qcoeff_ptr [ scan[c] ] = (INT16) v; \
158         ++c; \
159         goto DO_WHILE; }\
160     qcoeff_ptr [ scan[15] ] = (INT16) v; \
161     goto BLOCK_FINISHED;
162 
163 
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); \
167     FILL \
168     if(value >= bigsplit)\
169     {\
170         range = range-split;\
171         value = value-bigsplit;\
172         val += ((UINT16)1<<bits_count);\
173     }\
174     else\
175     {\
176         range = split;\
177     }\
178     NORMALIZE
179 
vp8_decode_mb_tokens(VP8D_COMP * dx,MACROBLOCKD * x)180 int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x)
181 {
182     ENTROPY_CONTEXT *A = (ENTROPY_CONTEXT *)x->above_context;
183     ENTROPY_CONTEXT *L = (ENTROPY_CONTEXT *)x->left_context;
184     const VP8_COMMON *const oc = & dx->common;
185 
186     BOOL_DECODER *bc = x->current_bc;
187 
188     char *eobs = x->eobs;
189 
190     ENTROPY_CONTEXT *a;
191     ENTROPY_CONTEXT *l;
192     int i;
193 
194     int eobtotal = 0;
195 
196     register int count;
197 
198     const BOOL_DATA *bufptr;
199     const BOOL_DATA *bufend;
200     register unsigned int range;
201     VP8_BD_VALUE value;
202     const int *scan;
203     register unsigned int shift;
204     UINT32 split;
205     VP8_BD_VALUE bigsplit;
206     INT16 *qcoeff_ptr;
207 
208     const vp8_prob *coef_probs;
209     int type;
210     int stop;
211     INT16 val, bits_count;
212     INT16 c;
213     INT16 v;
214     const vp8_prob *Prob;
215 
216     type = 3;
217     i = 0;
218     stop = 16;
219 
220     scan = vp8_default_zig_zag1d;
221     qcoeff_ptr = &x->qcoeff[0];
222 
223     if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
224     {
225         i = 24;
226         stop = 24;
227         type = 1;
228         qcoeff_ptr += 24*16;
229         eobtotal -= 16;
230     }
231 
232     bufend  = bc->user_buffer_end;
233     bufptr  = bc->user_buffer;
234     value   = bc->value;
235     count   = bc->count;
236     range   = bc->range;
237 
238 
239     coef_probs = oc->fc.coef_probs [type] [ 0 ] [0];
240 
241 BLOCK_LOOP:
242     a = A + vp8_block2above[i];
243     l = L + vp8_block2left[i];
244 
245     c = (INT16)(!type);
246 
247     /*Dest = ((A)!=0) + ((B)!=0);*/
248     VP8_COMBINEENTROPYCONTEXTS(v, *a, *l);
249     Prob = coef_probs;
250     Prob += v * ENTROPY_NODES;
251 
252 DO_WHILE:
253     Prob += coef_bands_x[c];
254     DECODE_AND_BRANCH_IF_ZERO(Prob[EOB_CONTEXT_NODE], BLOCK_FINISHED);
255 
256 CHECK_0_:
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;
265 
266     do
267     {
268         DECODE_EXTRABIT_AND_ADJUST_VAL(DCT_VAL_CATEGORY6, bits_count);
269         bits_count -- ;
270     }
271     while (bits_count >= 0);
272 
273     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(val);
274 
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);
283 
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);
292 
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);
299 
300 HIGH_LOW_CONTEXT_NODE_0_:
301     DECODE_AND_BRANCH_IF_ZERO(Prob[CAT_ONE_CONTEXT_NODE], CAT_ONE_CONTEXT_NODE_0_);
302 
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);
307 
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);
312 
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);
317 
318 THREE_CONTEXT_NODE_0_:
319     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(3);
320 
321 TWO_CONTEXT_NODE_0_:
322     DECODE_SIGN_WRITE_COEFF_AND_CHECK_EXIT(2);
323 
324 ONE_CONTEXT_NODE_0_:
325     DECODE_AND_APPLYSIGN(1);
326     Prob = coef_probs + ENTROPY_NODES;
327 
328     if (c < 15)
329     {
330         qcoeff_ptr [ scan[c] ] = (INT16) v;
331         ++c;
332         goto DO_WHILE;
333     }
334 
335     qcoeff_ptr [ scan[15] ] = (INT16) v;
336 BLOCK_FINISHED:
337     *a = *l = ((eobs[i] = c) != !type);   /* any nonzero data? */
338     eobtotal += c;
339     qcoeff_ptr += 16;
340 
341     i++;
342 
343     if (i < stop)
344         goto BLOCK_LOOP;
345 
346     if (i == 25)
347     {
348         type = 0;
349         i = 0;
350         stop = 16;
351         coef_probs = oc->fc.coef_probs [type] [ 0 ] [0];
352         qcoeff_ptr -= (24*16 + 16);
353         goto BLOCK_LOOP;
354     }
355 
356     if (i == 16)
357     {
358         type = 2;
359         coef_probs = oc->fc.coef_probs [type] [ 0 ] [0];
360         stop = 24;
361         goto BLOCK_LOOP;
362     }
363 
364     FILL
365     bc->user_buffer = bufptr;
366     bc->value = value;
367     bc->count = count;
368     bc->range = range;
369     return eobtotal;
370 
371 }
372