Line data Source code
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 "vpx_mem/vpx_mem.h"
12 : #include "vpx_ports/mem.h"
13 :
14 : #include "vp9/common/vp9_blockd.h"
15 : #include "vp9/common/vp9_common.h"
16 : #include "vp9/common/vp9_entropy.h"
17 : #if CONFIG_COEFFICIENT_RANGE_CHECKING
18 : #include "vp9/common/vp9_idct.h"
19 : #endif
20 :
21 : #include "vp9/decoder/vp9_detokenize.h"
22 :
23 : #define EOB_CONTEXT_NODE 0
24 : #define ZERO_CONTEXT_NODE 1
25 : #define ONE_CONTEXT_NODE 2
26 :
27 : #define INCREMENT_COUNT(token) \
28 : do { \
29 : if (counts) ++coef_counts[band][ctx][token]; \
30 : } while (0)
31 :
32 0 : static INLINE int read_bool(vpx_reader *r, int prob, BD_VALUE *value,
33 : int *count, unsigned int *range) {
34 0 : const unsigned int split = (*range * prob + (256 - prob)) >> CHAR_BIT;
35 0 : const BD_VALUE bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
36 :
37 0 : if (*count < 0) {
38 0 : r->value = *value;
39 0 : r->count = *count;
40 0 : vpx_reader_fill(r);
41 0 : *value = r->value;
42 0 : *count = r->count;
43 : }
44 :
45 0 : if (*value >= bigsplit) {
46 0 : *range = *range - split;
47 0 : *value = *value - bigsplit;
48 : {
49 0 : const int shift = vpx_norm[*range];
50 0 : *range <<= shift;
51 0 : *value <<= shift;
52 0 : *count -= shift;
53 : }
54 0 : return 1;
55 : }
56 0 : *range = split;
57 : {
58 0 : const int shift = vpx_norm[*range];
59 0 : *range <<= shift;
60 0 : *value <<= shift;
61 0 : *count -= shift;
62 : }
63 0 : return 0;
64 : }
65 :
66 0 : static INLINE int read_coeff(vpx_reader *r, const vpx_prob *probs, int n,
67 : BD_VALUE *value, int *count, unsigned int *range) {
68 0 : int i, val = 0;
69 0 : for (i = 0; i < n; ++i)
70 0 : val = (val << 1) | read_bool(r, probs[i], value, count, range);
71 0 : return val;
72 : }
73 :
74 0 : static int decode_coefs(const MACROBLOCKD *xd, PLANE_TYPE type,
75 : tran_low_t *dqcoeff, TX_SIZE tx_size, const int16_t *dq,
76 : int ctx, const int16_t *scan, const int16_t *nb,
77 : vpx_reader *r) {
78 0 : FRAME_COUNTS *counts = xd->counts;
79 0 : const int max_eob = 16 << (tx_size << 1);
80 0 : const FRAME_CONTEXT *const fc = xd->fc;
81 0 : const int ref = is_inter_block(xd->mi[0]);
82 0 : int band, c = 0;
83 0 : const vpx_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
84 0 : fc->coef_probs[tx_size][type][ref];
85 : const vpx_prob *prob;
86 : unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
87 : unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
88 : uint8_t token_cache[32 * 32];
89 0 : const uint8_t *band_translate = get_band_translate(tx_size);
90 0 : const int dq_shift = (tx_size == TX_32X32);
91 : int v;
92 0 : int16_t dqv = dq[0];
93 0 : const uint8_t *const cat6_prob =
94 : #if CONFIG_VP9_HIGHBITDEPTH
95 : (xd->bd == VPX_BITS_12)
96 : ? vp9_cat6_prob_high12
97 : : (xd->bd == VPX_BITS_10) ? vp9_cat6_prob_high12 + 2 :
98 : #endif // CONFIG_VP9_HIGHBITDEPTH
99 : vp9_cat6_prob;
100 0 : const int cat6_bits =
101 : #if CONFIG_VP9_HIGHBITDEPTH
102 : (xd->bd == VPX_BITS_12) ? 18 : (xd->bd == VPX_BITS_10) ? 16 :
103 : #endif // CONFIG_VP9_HIGHBITDEPTH
104 : 14;
105 : // Keep value, range, and count as locals. The compiler produces better
106 : // results with the locals than using r directly.
107 0 : BD_VALUE value = r->value;
108 0 : unsigned int range = r->range;
109 0 : int count = r->count;
110 :
111 0 : if (counts) {
112 0 : coef_counts = counts->coef[tx_size][type][ref];
113 0 : eob_branch_count = counts->eob_branch[tx_size][type][ref];
114 : }
115 :
116 0 : while (c < max_eob) {
117 0 : int val = -1;
118 0 : band = *band_translate++;
119 0 : prob = coef_probs[band][ctx];
120 0 : if (counts) ++eob_branch_count[band][ctx];
121 0 : if (!read_bool(r, prob[EOB_CONTEXT_NODE], &value, &count, &range)) {
122 0 : INCREMENT_COUNT(EOB_MODEL_TOKEN);
123 0 : break;
124 : }
125 :
126 0 : while (!read_bool(r, prob[ZERO_CONTEXT_NODE], &value, &count, &range)) {
127 0 : INCREMENT_COUNT(ZERO_TOKEN);
128 0 : dqv = dq[1];
129 0 : token_cache[scan[c]] = 0;
130 0 : ++c;
131 0 : if (c >= max_eob) {
132 0 : r->value = value;
133 0 : r->range = range;
134 0 : r->count = count;
135 0 : return c; // zero tokens at the end (no eob token)
136 : }
137 0 : ctx = get_coef_context(nb, token_cache, c);
138 0 : band = *band_translate++;
139 0 : prob = coef_probs[band][ctx];
140 : }
141 :
142 0 : if (read_bool(r, prob[ONE_CONTEXT_NODE], &value, &count, &range)) {
143 0 : const vpx_prob *p = vp9_pareto8_full[prob[PIVOT_NODE] - 1];
144 0 : INCREMENT_COUNT(TWO_TOKEN);
145 0 : if (read_bool(r, p[0], &value, &count, &range)) {
146 0 : if (read_bool(r, p[3], &value, &count, &range)) {
147 0 : token_cache[scan[c]] = 5;
148 0 : if (read_bool(r, p[5], &value, &count, &range)) {
149 0 : if (read_bool(r, p[7], &value, &count, &range)) {
150 0 : val = CAT6_MIN_VAL +
151 0 : read_coeff(r, cat6_prob, cat6_bits, &value, &count, &range);
152 : } else {
153 0 : val = CAT5_MIN_VAL +
154 0 : read_coeff(r, vp9_cat5_prob, 5, &value, &count, &range);
155 : }
156 0 : } else if (read_bool(r, p[6], &value, &count, &range)) {
157 0 : val = CAT4_MIN_VAL +
158 0 : read_coeff(r, vp9_cat4_prob, 4, &value, &count, &range);
159 : } else {
160 0 : val = CAT3_MIN_VAL +
161 0 : read_coeff(r, vp9_cat3_prob, 3, &value, &count, &range);
162 : }
163 : } else {
164 0 : token_cache[scan[c]] = 4;
165 0 : if (read_bool(r, p[4], &value, &count, &range)) {
166 0 : val = CAT2_MIN_VAL +
167 0 : read_coeff(r, vp9_cat2_prob, 2, &value, &count, &range);
168 : } else {
169 0 : val = CAT1_MIN_VAL +
170 0 : read_coeff(r, vp9_cat1_prob, 1, &value, &count, &range);
171 : }
172 : }
173 : #if CONFIG_VP9_HIGHBITDEPTH
174 : // val may use 18-bits
175 : v = (int)(((int64_t)val * dqv) >> dq_shift);
176 : #else
177 0 : v = (val * dqv) >> dq_shift;
178 : #endif
179 : } else {
180 0 : if (read_bool(r, p[1], &value, &count, &range)) {
181 0 : token_cache[scan[c]] = 3;
182 0 : v = ((3 + read_bool(r, p[2], &value, &count, &range)) * dqv) >>
183 : dq_shift;
184 : } else {
185 0 : token_cache[scan[c]] = 2;
186 0 : v = (2 * dqv) >> dq_shift;
187 : }
188 : }
189 : } else {
190 0 : INCREMENT_COUNT(ONE_TOKEN);
191 0 : token_cache[scan[c]] = 1;
192 0 : v = dqv >> dq_shift;
193 : }
194 : #if CONFIG_COEFFICIENT_RANGE_CHECKING
195 : #if CONFIG_VP9_HIGHBITDEPTH
196 : dqcoeff[scan[c]] = highbd_check_range(
197 : read_bool(r, 128, &value, &count, &range) ? -v : v, xd->bd);
198 : #else
199 : dqcoeff[scan[c]] =
200 : check_range(read_bool(r, 128, &value, &count, &range) ? -v : v);
201 : #endif // CONFIG_VP9_HIGHBITDEPTH
202 : #else
203 0 : if (read_bool(r, 128, &value, &count, &range)) {
204 0 : dqcoeff[scan[c]] = -v;
205 : } else {
206 0 : dqcoeff[scan[c]] = v;
207 : }
208 : #endif // CONFIG_COEFFICIENT_RANGE_CHECKING
209 0 : ++c;
210 0 : ctx = get_coef_context(nb, token_cache, c);
211 0 : dqv = dq[1];
212 : }
213 :
214 0 : r->value = value;
215 0 : r->range = range;
216 0 : r->count = count;
217 0 : return c;
218 : }
219 :
220 0 : static void get_ctx_shift(MACROBLOCKD *xd, int *ctx_shift_a, int *ctx_shift_l,
221 : int x, int y, unsigned int tx_size_in_blocks) {
222 0 : if (xd->max_blocks_wide) {
223 0 : if (tx_size_in_blocks + x > xd->max_blocks_wide)
224 0 : *ctx_shift_a = (tx_size_in_blocks - (xd->max_blocks_wide - x)) * 8;
225 : }
226 0 : if (xd->max_blocks_high) {
227 0 : if (tx_size_in_blocks + y > xd->max_blocks_high)
228 0 : *ctx_shift_l = (tx_size_in_blocks - (xd->max_blocks_high - y)) * 8;
229 : }
230 0 : }
231 :
232 0 : int vp9_decode_block_tokens(TileWorkerData *twd, int plane,
233 : const scan_order *sc, int x, int y, TX_SIZE tx_size,
234 : int seg_id) {
235 0 : vpx_reader *r = &twd->bit_reader;
236 0 : MACROBLOCKD *xd = &twd->xd;
237 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
238 0 : const int16_t *const dequant = pd->seg_dequant[seg_id];
239 : int eob;
240 0 : ENTROPY_CONTEXT *a = pd->above_context + x;
241 0 : ENTROPY_CONTEXT *l = pd->left_context + y;
242 : int ctx;
243 0 : int ctx_shift_a = 0;
244 0 : int ctx_shift_l = 0;
245 :
246 0 : switch (tx_size) {
247 : case TX_4X4:
248 0 : ctx = a[0] != 0;
249 0 : ctx += l[0] != 0;
250 0 : eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
251 : dequant, ctx, sc->scan, sc->neighbors, r);
252 0 : a[0] = l[0] = (eob > 0);
253 0 : break;
254 : case TX_8X8:
255 0 : get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_8X8);
256 0 : ctx = !!*(const uint16_t *)a;
257 0 : ctx += !!*(const uint16_t *)l;
258 0 : eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
259 : dequant, ctx, sc->scan, sc->neighbors, r);
260 0 : *(uint16_t *)a = ((eob > 0) * 0x0101) >> ctx_shift_a;
261 0 : *(uint16_t *)l = ((eob > 0) * 0x0101) >> ctx_shift_l;
262 0 : break;
263 : case TX_16X16:
264 0 : get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_16X16);
265 0 : ctx = !!*(const uint32_t *)a;
266 0 : ctx += !!*(const uint32_t *)l;
267 0 : eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
268 : dequant, ctx, sc->scan, sc->neighbors, r);
269 0 : *(uint32_t *)a = ((eob > 0) * 0x01010101) >> ctx_shift_a;
270 0 : *(uint32_t *)l = ((eob > 0) * 0x01010101) >> ctx_shift_l;
271 0 : break;
272 : case TX_32X32:
273 0 : get_ctx_shift(xd, &ctx_shift_a, &ctx_shift_l, x, y, 1 << TX_32X32);
274 : // NOTE: casting to uint64_t here is safe because the default memory
275 : // alignment is at least 8 bytes and the TX_32X32 is aligned on 8 byte
276 : // boundaries.
277 0 : ctx = !!*(const uint64_t *)a;
278 0 : ctx += !!*(const uint64_t *)l;
279 0 : eob = decode_coefs(xd, get_plane_type(plane), pd->dqcoeff, tx_size,
280 : dequant, ctx, sc->scan, sc->neighbors, r);
281 0 : *(uint64_t *)a = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_a;
282 0 : *(uint64_t *)l = ((eob > 0) * 0x0101010101010101ULL) >> ctx_shift_l;
283 0 : break;
284 : default:
285 0 : assert(0 && "Invalid transform size.");
286 : eob = 0;
287 : break;
288 : }
289 :
290 0 : return eob;
291 : }
|