Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
4 : * This source code is subject to the terms of the BSD 2 Clause License and
5 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 : * was not distributed with this source code in the LICENSE file, you can
7 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 : * Media Patent License 1.0 was not distributed with this source code in the
9 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 : */
11 :
12 : #include "./aom_config.h"
13 : #if !CONFIG_PVQ
14 : #include "aom_mem/aom_mem.h"
15 : #include "aom_ports/mem.h"
16 : #endif // !CONFIG_PVQ
17 :
18 : #include "av1/common/blockd.h"
19 :
20 : #define ACCT_STR __func__
21 :
22 : #if !CONFIG_PVQ || CONFIG_VAR_TX
23 : #include "av1/common/common.h"
24 : #include "av1/common/entropy.h"
25 : #include "av1/common/idct.h"
26 : #include "av1/decoder/detokenize.h"
27 :
28 : #define EOB_CONTEXT_NODE 0
29 : #define ZERO_CONTEXT_NODE 1
30 : #define ONE_CONTEXT_NODE 2
31 : #define LOW_VAL_CONTEXT_NODE 0
32 : #define TWO_CONTEXT_NODE 1
33 : #define THREE_CONTEXT_NODE 2
34 : #define HIGH_LOW_CONTEXT_NODE 3
35 : #define CAT_ONE_CONTEXT_NODE 4
36 : #define CAT_THREEFOUR_CONTEXT_NODE 5
37 : #define CAT_THREE_CONTEXT_NODE 6
38 : #define CAT_FIVE_CONTEXT_NODE 7
39 :
40 : #define INCREMENT_COUNT(token) \
41 : do { \
42 : if (counts) ++coef_counts[band][ctx][token]; \
43 : } while (0)
44 :
45 : #if CONFIG_NEW_MULTISYMBOL
46 : #define READ_COEFF(prob_name, cdf_name, num, r) read_coeff(cdf_name, num, r);
47 : static INLINE int read_coeff(const aom_cdf_prob *const *cdf, int n,
48 : aom_reader *r) {
49 : int val = 0;
50 : int i = 0;
51 : int count = 0;
52 : while (count < n) {
53 : const int size = AOMMIN(n - count, 4);
54 : val |= aom_read_cdf(r, cdf[i++], 1 << size, ACCT_STR) << count;
55 : count += size;
56 : }
57 : return val;
58 : }
59 : #else
60 : #define READ_COEFF(prob_name, cdf_name, num, r) read_coeff(prob_name, num, r);
61 0 : static INLINE int read_coeff(const aom_prob *probs, int n, aom_reader *r) {
62 0 : int i, val = 0;
63 0 : for (i = 0; i < n; ++i) val = (val << 1) | aom_read(r, probs[i], ACCT_STR);
64 0 : return val;
65 : }
66 :
67 : #endif
68 :
69 0 : static int token_to_value(aom_reader *const r, int token, TX_SIZE tx_size,
70 : int bit_depth) {
71 : #if !CONFIG_HIGHBITDEPTH
72 : assert(bit_depth == 8);
73 : #endif // !CONFIG_HIGHBITDEPTH
74 :
75 0 : switch (token) {
76 : case ZERO_TOKEN:
77 : case ONE_TOKEN:
78 : case TWO_TOKEN:
79 : case THREE_TOKEN:
80 0 : case FOUR_TOKEN: return token;
81 : case CATEGORY1_TOKEN:
82 0 : return CAT1_MIN_VAL + READ_COEFF(av1_cat1_prob, av1_cat1_cdf, 1, r);
83 : case CATEGORY2_TOKEN:
84 0 : return CAT2_MIN_VAL + READ_COEFF(av1_cat2_prob, av1_cat2_cdf, 2, r);
85 : case CATEGORY3_TOKEN:
86 0 : return CAT3_MIN_VAL + READ_COEFF(av1_cat3_prob, av1_cat3_cdf, 3, r);
87 : case CATEGORY4_TOKEN:
88 0 : return CAT4_MIN_VAL + READ_COEFF(av1_cat4_prob, av1_cat4_cdf, 4, r);
89 : case CATEGORY5_TOKEN:
90 0 : return CAT5_MIN_VAL + READ_COEFF(av1_cat5_prob, av1_cat5_cdf, 5, r);
91 : case CATEGORY6_TOKEN: {
92 0 : const int skip_bits = (int)sizeof(av1_cat6_prob) -
93 0 : av1_get_cat6_extrabits_size(tx_size, bit_depth);
94 0 : return CAT6_MIN_VAL + READ_COEFF(av1_cat6_prob + skip_bits, av1_cat6_cdf,
95 : 18 - skip_bits, r);
96 : }
97 : default:
98 0 : assert(0); // Invalid token.
99 : return -1;
100 : }
101 : }
102 :
103 0 : static int decode_coefs(MACROBLOCKD *xd, PLANE_TYPE type, tran_low_t *dqcoeff,
104 : TX_SIZE tx_size, TX_TYPE tx_type, const int16_t *dq,
105 : #if CONFIG_NEW_QUANT
106 : dequant_val_type_nuq *dq_val,
107 : #endif // CONFIG_NEW_QUANT
108 : #if CONFIG_AOM_QM
109 : const qm_val_t *iqm[2][TX_SIZES_ALL],
110 : #endif // CONFIG_AOM_QM
111 : int ctx, const int16_t *scan, const int16_t *nb,
112 : int16_t *max_scan_line, aom_reader *r) {
113 0 : FRAME_COUNTS *counts = xd->counts;
114 : #if CONFIG_EC_ADAPT
115 0 : FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
116 : #else
117 : FRAME_CONTEXT *const ec_ctx = xd->fc;
118 : #endif
119 0 : const int max_eob = tx_size_2d[tx_size];
120 0 : const int ref = is_inter_block(&xd->mi[0]->mbmi);
121 : #if CONFIG_AOM_QM
122 : const qm_val_t *iqmatrix = iqm[!ref][tx_size];
123 : #endif // CONFIG_AOM_QM
124 0 : int band, c = 0;
125 0 : const int tx_size_ctx = txsize_sqr_map[tx_size];
126 0 : aom_cdf_prob(*coef_head_cdfs)[COEFF_CONTEXTS][CDF_SIZE(ENTROPY_TOKENS)] =
127 : ec_ctx->coef_head_cdfs[tx_size_ctx][type][ref];
128 0 : aom_cdf_prob(*coef_tail_cdfs)[COEFF_CONTEXTS][CDF_SIZE(ENTROPY_TOKENS)] =
129 : ec_ctx->coef_tail_cdfs[tx_size_ctx][type][ref];
130 0 : int val = 0;
131 :
132 : #if !CONFIG_EC_ADAPT
133 : unsigned int *blockz_count;
134 : unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1] = NULL;
135 : unsigned int(*eob_branch_count)[COEFF_CONTEXTS] = NULL;
136 : #endif
137 : uint8_t token_cache[MAX_TX_SQUARE];
138 0 : const uint8_t *band_translate = get_band_translate(tx_size);
139 : int dq_shift;
140 : int v, token;
141 0 : int16_t dqv = dq[0];
142 : #if CONFIG_NEW_QUANT
143 : const tran_low_t *dqv_val = &dq_val[0][0];
144 : #endif // CONFIG_NEW_QUANT
145 : (void)tx_type;
146 :
147 : if (counts) {
148 : #if !CONFIG_EC_ADAPT
149 : coef_counts = counts->coef[tx_size_ctx][type][ref];
150 : eob_branch_count = counts->eob_branch[tx_size_ctx][type][ref];
151 : blockz_count = counts->blockz_count[tx_size_ctx][type][ref][ctx];
152 : #endif
153 : }
154 :
155 0 : dq_shift = av1_get_tx_scale(tx_size);
156 :
157 0 : band = *band_translate++;
158 :
159 0 : int more_data = 1;
160 0 : while (more_data) {
161 : int comb_token;
162 0 : int last_pos = (c + 1 == max_eob);
163 0 : int first_pos = (c == 0);
164 :
165 : #if CONFIG_NEW_QUANT
166 : dqv_val = &dq_val[band][0];
167 : #endif // CONFIG_NEW_QUANT
168 :
169 0 : comb_token = last_pos ? 2 * aom_read_bit(r, ACCT_STR) + 2
170 0 : : aom_read_symbol(r, coef_head_cdfs[band][ctx],
171 : HEAD_TOKENS + first_pos, ACCT_STR) +
172 0 : !first_pos;
173 0 : if (first_pos) {
174 : #if !CONFIG_EC_ADAPT
175 : if (counts) ++blockz_count[comb_token != 0];
176 : #endif
177 0 : if (comb_token == 0) return 0;
178 : }
179 0 : token = comb_token >> 1;
180 :
181 0 : while (!token) {
182 0 : *max_scan_line = AOMMAX(*max_scan_line, scan[c]);
183 0 : token_cache[scan[c]] = 0;
184 : #if !CONFIG_EC_ADAPT
185 : if (counts && !last_pos) {
186 : ++coef_counts[band][ctx][ZERO_TOKEN];
187 : }
188 : #endif
189 0 : ++c;
190 0 : dqv = dq[1];
191 0 : ctx = get_coef_context(nb, token_cache, c);
192 0 : band = *band_translate++;
193 :
194 0 : last_pos = (c + 1 == max_eob);
195 :
196 0 : comb_token = last_pos ? 2 * aom_read_bit(r, ACCT_STR) + 2
197 0 : : aom_read_symbol(r, coef_head_cdfs[band][ctx],
198 : HEAD_TOKENS, ACCT_STR) +
199 : 1;
200 0 : token = comb_token >> 1;
201 : }
202 :
203 0 : more_data = comb_token & 1;
204 : #if !CONFIG_EC_ADAPT
205 : if (counts && !last_pos) {
206 : ++coef_counts[band][ctx][token];
207 : ++eob_branch_count[band][ctx];
208 : if (!more_data) ++coef_counts[band][ctx][EOB_MODEL_TOKEN];
209 : }
210 : #endif
211 :
212 0 : if (token > ONE_TOKEN)
213 0 : token +=
214 0 : aom_read_symbol(r, coef_tail_cdfs[band][ctx], TAIL_TOKENS, ACCT_STR);
215 : #if CONFIG_NEW_QUANT
216 : dqv_val = &dq_val[band][0];
217 : #endif // CONFIG_NEW_QUANT
218 :
219 0 : *max_scan_line = AOMMAX(*max_scan_line, scan[c]);
220 0 : token_cache[scan[c]] = av1_pt_energy_class[token];
221 :
222 0 : val = token_to_value(r, token, tx_size, xd->bd);
223 :
224 : #if CONFIG_NEW_QUANT
225 : v = av1_dequant_abscoeff_nuq(val, dqv, dqv_val);
226 : v = dq_shift ? ROUND_POWER_OF_TWO(v, dq_shift) : v;
227 : #else
228 : #if CONFIG_AOM_QM
229 : dqv = ((iqmatrix[scan[c]] * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >>
230 : AOM_QM_BITS;
231 : #endif
232 0 : v = (val * dqv) >> dq_shift;
233 : #endif
234 :
235 0 : v = aom_read_bit(r, ACCT_STR) ? -v : v;
236 : #if CONFIG_COEFFICIENT_RANGE_CHECKING
237 : check_range(v, xd->bd);
238 : #endif // CONFIG_COEFFICIENT_RANGE_CHECKING
239 :
240 0 : dqcoeff[scan[c]] = v;
241 :
242 0 : ++c;
243 0 : more_data &= (c < max_eob);
244 0 : if (!more_data) break;
245 0 : dqv = dq[1];
246 0 : ctx = get_coef_context(nb, token_cache, c);
247 0 : band = *band_translate++;
248 : }
249 :
250 0 : return c;
251 : }
252 : #endif // !CONFIG_PVQ
253 :
254 : #if CONFIG_PALETTE
255 0 : void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
256 : aom_reader *r) {
257 0 : const MODE_INFO *const mi = xd->mi[0];
258 0 : const MB_MODE_INFO *const mbmi = &mi->mbmi;
259 : uint8_t color_order[PALETTE_MAX_SIZE];
260 0 : const int n = mbmi->palette_mode_info.palette_size[plane];
261 : int i, j;
262 0 : uint8_t *const color_map = xd->plane[plane].color_index_map;
263 : const aom_prob(
264 0 : *const prob)[PALETTE_COLOR_INDEX_CONTEXTS][PALETTE_COLORS - 1] =
265 : plane ? av1_default_palette_uv_color_index_prob
266 0 : : av1_default_palette_y_color_index_prob;
267 : int plane_block_width, plane_block_height, rows, cols;
268 0 : av1_get_block_dimensions(mbmi->sb_type, plane, xd, &plane_block_width,
269 : &plane_block_height, &rows, &cols);
270 0 : assert(plane == 0 || plane == 1);
271 :
272 : #if CONFIG_PALETTE_THROUGHPUT
273 : // Run wavefront on the palette map index decoding.
274 0 : for (i = 1; i < rows + cols - 1; ++i) {
275 0 : for (j = AOMMIN(i, cols - 1); j >= AOMMAX(0, i - rows + 1); --j) {
276 0 : const int color_ctx = av1_get_palette_color_index_context(
277 : color_map, plane_block_width, (i - j), j, n, color_order, NULL);
278 0 : const int color_idx =
279 0 : aom_read_tree(r, av1_palette_color_index_tree[n - 2],
280 : prob[n - 2][color_ctx], ACCT_STR);
281 0 : assert(color_idx >= 0 && color_idx < n);
282 0 : color_map[(i - j) * plane_block_width + j] = color_order[color_idx];
283 : }
284 : }
285 : // Copy last column to extra columns.
286 0 : if (cols < plane_block_width) {
287 0 : for (i = 0; i < plane_block_height; ++i) {
288 0 : memset(color_map + i * plane_block_width + cols,
289 0 : color_map[i * plane_block_width + cols - 1],
290 0 : (plane_block_width - cols));
291 : }
292 : }
293 : #else
294 : for (i = 0; i < rows; ++i) {
295 : for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
296 : const int color_ctx = av1_get_palette_color_index_context(
297 : color_map, plane_block_width, i, j, n, color_order, NULL);
298 : const int color_idx =
299 : aom_read_tree(r, av1_palette_color_index_tree[n - PALETTE_MIN_SIZE],
300 : prob[n - PALETTE_MIN_SIZE][color_ctx], ACCT_STR);
301 : assert(color_idx >= 0 && color_idx < n);
302 : color_map[i * plane_block_width + j] = color_order[color_idx];
303 : }
304 : memset(color_map + i * plane_block_width + cols,
305 : color_map[i * plane_block_width + cols - 1],
306 : (plane_block_width - cols)); // Copy last column to extra columns.
307 : }
308 : #endif // CONFIG_PALETTE_THROUGHPUT
309 : // Copy last row to extra rows.
310 0 : for (i = rows; i < plane_block_height; ++i) {
311 0 : memcpy(color_map + i * plane_block_width,
312 0 : color_map + (rows - 1) * plane_block_width, plane_block_width);
313 : }
314 0 : }
315 : #endif // CONFIG_PALETTE
316 :
317 : #if !CONFIG_PVQ || CONFIG_VAR_TX
318 0 : int av1_decode_block_tokens(AV1_COMMON *cm, MACROBLOCKD *const xd, int plane,
319 : const SCAN_ORDER *sc, int x, int y, TX_SIZE tx_size,
320 : TX_TYPE tx_type, int16_t *max_scan_line,
321 : aom_reader *r, int seg_id) {
322 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
323 0 : const int16_t *const dequant = pd->seg_dequant[seg_id];
324 0 : const int ctx =
325 0 : get_entropy_context(tx_size, pd->above_context + x, pd->left_context + y);
326 : #if CONFIG_NEW_QUANT
327 : const int ref = is_inter_block(&xd->mi[0]->mbmi);
328 : int dq =
329 : get_dq_profile_from_ctx(xd->qindex[seg_id], ctx, ref, pd->plane_type);
330 : #endif // CONFIG_NEW_QUANT
331 :
332 0 : const int eob =
333 0 : decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size, tx_type, dequant,
334 : #if CONFIG_NEW_QUANT
335 : pd->seg_dequant_nuq[seg_id][dq],
336 : #endif // CONFIG_NEW_QUANT
337 : #if CONFIG_AOM_QM
338 : pd->seg_iqmatrix[seg_id],
339 : #endif // CONFIG_AOM_QM
340 : ctx, sc->scan, sc->neighbors, max_scan_line, r);
341 0 : av1_set_contexts(xd, pd, plane, tx_size, eob > 0, x, y);
342 : #if CONFIG_ADAPT_SCAN
343 : if (xd->counts)
344 : av1_update_scan_count_facade(cm, xd->counts, tx_size, tx_type, pd->dqcoeff,
345 : eob);
346 : #else
347 : (void)cm;
348 : #endif
349 0 : return eob;
350 : }
351 : #endif // !CONFIG_PVQ
|