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 #include "vp8/common/blockd.h"
12 #include "onyxd_int.h"
13 #include "vpx_mem/vpx_mem.h"
14 #include "vpx_ports/mem.h"
15 #include "detokenize.h"
16 
vp8_reset_mb_tokens_context(MACROBLOCKD * x)17 void vp8_reset_mb_tokens_context(MACROBLOCKD *x) {
18   ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context);
19   ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context);
20 
21   memset(a_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES) - 1);
22   memset(l_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES) - 1);
23 
24   /* Clear entropy contexts for Y2 blocks */
25   if (!x->mode_info_context->mbmi.is_4x4) {
26     a_ctx[8] = l_ctx[8] = 0;
27   }
28 }
29 
30 /*
31     ------------------------------------------------------------------------------
32     Residual decoding (Paragraph 13.2 / 13.3)
33 */
34 static const uint8_t kBands[16 + 1] = {
35   0, 1, 2, 3, 6, 4, 5, 6, 6,
36   6, 6, 6, 6, 6, 6, 7, 0 /* extra entry as sentinel */
37 };
38 
39 static const uint8_t kCat3[] = { 173, 148, 140, 0 };
40 static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };
41 static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };
42 static const uint8_t kCat6[] = { 254, 254, 243, 230, 196, 177,
43                                  153, 140, 133, 130, 129, 0 };
44 static const uint8_t *const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };
45 static const uint8_t kZigzag[16] = { 0, 1,  4,  8,  5, 2,  3,  6,
46                                      9, 12, 13, 10, 7, 11, 14, 15 };
47 
48 #define VP8GetBit vp8dx_decode_bool
49 #define NUM_PROBAS 11
50 #define NUM_CTX 3
51 
52 /* for const-casting */
53 typedef const uint8_t (*ProbaArray)[NUM_CTX][NUM_PROBAS];
54 
GetSigned(BOOL_DECODER * br,int value_to_sign)55 static int GetSigned(BOOL_DECODER *br, int value_to_sign) {
56   int split = (br->range + 1) >> 1;
57   VP8_BD_VALUE bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
58   int v;
59 
60   if (br->count < 0) vp8dx_bool_decoder_fill(br);
61 
62   if (br->value < bigsplit) {
63     br->range = split;
64     v = value_to_sign;
65   } else {
66     br->range = br->range - split;
67     br->value = br->value - bigsplit;
68     v = -value_to_sign;
69   }
70   br->range += br->range;
71   br->value += br->value;
72   br->count--;
73 
74   return v;
75 }
76 /*
77    Returns the position of the last non-zero coeff plus one
78    (and 0 if there's no coeff at all)
79 */
GetCoeffs(BOOL_DECODER * br,ProbaArray prob,int ctx,int n,int16_t * out)80 static int GetCoeffs(BOOL_DECODER *br, ProbaArray prob, int ctx, int n,
81                      int16_t *out) {
82   const uint8_t *p = prob[n][ctx];
83   if (!VP8GetBit(br, p[0])) { /* first EOB is more a 'CBP' bit. */
84     return 0;
85   }
86   while (1) {
87     ++n;
88     if (!VP8GetBit(br, p[1])) {
89       p = prob[kBands[n]][0];
90     } else { /* non zero coeff */
91       int v, j;
92       if (!VP8GetBit(br, p[2])) {
93         p = prob[kBands[n]][1];
94         v = 1;
95       } else {
96         if (!VP8GetBit(br, p[3])) {
97           if (!VP8GetBit(br, p[4])) {
98             v = 2;
99           } else {
100             v = 3 + VP8GetBit(br, p[5]);
101           }
102         } else {
103           if (!VP8GetBit(br, p[6])) {
104             if (!VP8GetBit(br, p[7])) {
105               v = 5 + VP8GetBit(br, 159);
106             } else {
107               v = 7 + 2 * VP8GetBit(br, 165);
108               v += VP8GetBit(br, 145);
109             }
110           } else {
111             const uint8_t *tab;
112             const int bit1 = VP8GetBit(br, p[8]);
113             const int bit0 = VP8GetBit(br, p[9 + bit1]);
114             const int cat = 2 * bit1 + bit0;
115             v = 0;
116             for (tab = kCat3456[cat]; *tab; ++tab) {
117               v += v + VP8GetBit(br, *tab);
118             }
119             v += 3 + (8 << cat);
120           }
121         }
122         p = prob[kBands[n]][2];
123       }
124       j = kZigzag[n - 1];
125 
126       out[j] = GetSigned(br, v);
127 
128       if (n == 16 || !VP8GetBit(br, p[0])) { /* EOB */
129         return n;
130       }
131     }
132     if (n == 16) {
133       return 16;
134     }
135   }
136 }
137 
vp8_decode_mb_tokens(VP8D_COMP * dx,MACROBLOCKD * x)138 int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x) {
139   BOOL_DECODER *bc = x->current_bc;
140   const FRAME_CONTEXT *const fc = &dx->common.fc;
141   char *eobs = x->eobs;
142 
143   int i;
144   int nonzeros;
145   int eobtotal = 0;
146 
147   short *qcoeff_ptr;
148   ProbaArray coef_probs;
149   ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context);
150   ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context);
151   ENTROPY_CONTEXT *a;
152   ENTROPY_CONTEXT *l;
153   int skip_dc = 0;
154 
155   qcoeff_ptr = &x->qcoeff[0];
156 
157   if (!x->mode_info_context->mbmi.is_4x4) {
158     a = a_ctx + 8;
159     l = l_ctx + 8;
160 
161     coef_probs = fc->coef_probs[1];
162 
163     nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr + 24 * 16);
164     *a = *l = (nonzeros > 0);
165 
166     eobs[24] = nonzeros;
167     eobtotal += nonzeros - 16;
168 
169     coef_probs = fc->coef_probs[0];
170     skip_dc = 1;
171   } else {
172     coef_probs = fc->coef_probs[3];
173     skip_dc = 0;
174   }
175 
176   for (i = 0; i < 16; ++i) {
177     a = a_ctx + (i & 3);
178     l = l_ctx + ((i & 0xc) >> 2);
179 
180     nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), skip_dc, qcoeff_ptr);
181     *a = *l = (nonzeros > 0);
182 
183     nonzeros += skip_dc;
184     eobs[i] = nonzeros;
185     eobtotal += nonzeros;
186     qcoeff_ptr += 16;
187   }
188 
189   coef_probs = fc->coef_probs[2];
190 
191   a_ctx += 4;
192   l_ctx += 4;
193   for (i = 16; i < 24; ++i) {
194     a = a_ctx + ((i > 19) << 1) + (i & 1);
195     l = l_ctx + ((i > 19) << 1) + ((i & 3) > 1);
196 
197     nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr);
198     *a = *l = (nonzeros > 0);
199 
200     eobs[i] = nonzeros;
201     eobtotal += nonzeros;
202     qcoeff_ptr += 16;
203   }
204 
205   return eobtotal;
206 }
207