blob: ec2b341bc84210654fa2cef08cdf28279dc77c51 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070013#include "aom_ports/mem.h"
14
Alex Converse1ac1ae72016-09-17 15:11:16 -070015#if CONFIG_ANS
16#include "aom_dsp/ans.h"
17#endif // CONFIG_ANS
Yaowu Xuc27fc142016-08-22 16:08:15 -070018#include "av1/common/blockd.h"
19#include "av1/common/common.h"
20#include "av1/common/entropy.h"
21#include "av1/common/idct.h"
22
23#include "av1/decoder/detokenize.h"
24
Michael Bebenita6048d052016-08-25 14:40:54 -070025#define ACCT_STR __func__
26
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#define EOB_CONTEXT_NODE 0
28#define ZERO_CONTEXT_NODE 1
29#define ONE_CONTEXT_NODE 2
30#define LOW_VAL_CONTEXT_NODE 0
31#define TWO_CONTEXT_NODE 1
32#define THREE_CONTEXT_NODE 2
33#define HIGH_LOW_CONTEXT_NODE 3
34#define CAT_ONE_CONTEXT_NODE 4
35#define CAT_THREEFOUR_CONTEXT_NODE 5
36#define CAT_THREE_CONTEXT_NODE 6
37#define CAT_FIVE_CONTEXT_NODE 7
38
39#define INCREMENT_COUNT(token) \
40 do { \
41 if (counts) ++coef_counts[band][ctx][token]; \
42 } while (0)
43
Yaowu Xuf883b422016-08-30 14:01:10 -070044static INLINE int read_coeff(const aom_prob *probs, int n, aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070045 int i, val = 0;
Michael Bebenita6048d052016-08-25 14:40:54 -070046 for (i = 0; i < n; ++i) val = (val << 1) | aom_read(r, probs[i], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -070047 return val;
48}
49
50#if CONFIG_AOM_QM
Thomas9ac55082016-09-23 18:04:17 +010051static int decode_coefs(MACROBLOCKD *xd, PLANE_TYPE type, tran_low_t *dqcoeff,
52 TX_SIZE tx_size, TX_TYPE tx_type, const int16_t *dq,
53 int ctx, const int16_t *scan, const int16_t *nb,
54 aom_reader *r, const qm_val_t *iqm[2][TX_SIZES])
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#else
Thomas9ac55082016-09-23 18:04:17 +010056static int decode_coefs(MACROBLOCKD *xd, PLANE_TYPE type, tran_low_t *dqcoeff,
57 TX_SIZE tx_size, TX_TYPE tx_type, const int16_t *dq,
Yaowu Xuc27fc142016-08-22 16:08:15 -070058#if CONFIG_NEW_QUANT
59 dequant_val_type_nuq *dq_val,
60#endif // CONFIG_NEW_QUANT
61 int ctx, const int16_t *scan, const int16_t *nb,
Jingning Han1be18782016-10-21 11:48:15 -070062 int16_t *max_scan_line, aom_reader *r)
Yaowu Xuc27fc142016-08-22 16:08:15 -070063#endif
64{
65 FRAME_COUNTS *counts = xd->counts;
66 const int max_eob = get_tx2d_size(tx_size);
Thomas9ac55082016-09-23 18:04:17 +010067 FRAME_CONTEXT *const fc = xd->fc;
Yaowu Xuc27fc142016-08-22 16:08:15 -070068 const int ref = is_inter_block(&xd->mi[0]->mbmi);
69#if CONFIG_AOM_QM
70 const qm_val_t *iqmatrix = iqm[!ref][tx_size];
71#endif
72 int band, c = 0;
73 const int tx_size_ctx = txsize_sqr_map[tx_size];
Thomas9ac55082016-09-23 18:04:17 +010074 aom_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
Yaowu Xuc27fc142016-08-22 16:08:15 -070075 fc->coef_probs[tx_size_ctx][type][ref];
Yaowu Xuf883b422016-08-30 14:01:10 -070076 const aom_prob *prob;
Alex Converseaca9feb2016-10-10 11:08:10 -070077#if CONFIG_EC_MULTISYMBOL
Thomas9ac55082016-09-23 18:04:17 +010078 aom_cdf_prob(*coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
Alex Converse5e4d00c2016-10-12 13:23:33 -070079 fc->coef_cdfs[tx_size_ctx][type][ref];
Thomas9ac55082016-09-23 18:04:17 +010080 aom_cdf_prob(*cdf)[ENTROPY_TOKENS];
Alex Converseaca9feb2016-10-10 11:08:10 -070081#endif // CONFIG_EC_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -070082 unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
83 unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
84 uint8_t token_cache[MAX_TX_SQUARE];
85 const uint8_t *band_translate = get_band_translate(tx_size);
86 int dq_shift;
87 int v, token;
88 int16_t dqv = dq[0];
89#if CONFIG_NEW_QUANT
90 const tran_low_t *dqv_val = &dq_val[0][0];
91#endif // CONFIG_NEW_QUANT
92 const uint8_t *cat1_prob;
93 const uint8_t *cat2_prob;
94 const uint8_t *cat3_prob;
95 const uint8_t *cat4_prob;
96 const uint8_t *cat5_prob;
97 const uint8_t *cat6_prob;
98
99 if (counts) {
100 coef_counts = counts->coef[tx_size_ctx][type][ref];
101 eob_branch_count = counts->eob_branch[tx_size_ctx][type][ref];
102 }
103
Yaowu Xuf883b422016-08-30 14:01:10 -0700104#if CONFIG_AOM_HIGHBITDEPTH
105 if (xd->bd > AOM_BITS_8) {
106 if (xd->bd == AOM_BITS_10) {
107 cat1_prob = av1_cat1_prob_high10;
108 cat2_prob = av1_cat2_prob_high10;
109 cat3_prob = av1_cat3_prob_high10;
110 cat4_prob = av1_cat4_prob_high10;
111 cat5_prob = av1_cat5_prob_high10;
112 cat6_prob = av1_cat6_prob_high10;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700113 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700114 cat1_prob = av1_cat1_prob_high12;
115 cat2_prob = av1_cat2_prob_high12;
116 cat3_prob = av1_cat3_prob_high12;
117 cat4_prob = av1_cat4_prob_high12;
118 cat5_prob = av1_cat5_prob_high12;
119 cat6_prob = av1_cat6_prob_high12;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700120 }
121 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700122 cat1_prob = av1_cat1_prob;
123 cat2_prob = av1_cat2_prob;
124 cat3_prob = av1_cat3_prob;
125 cat4_prob = av1_cat4_prob;
126 cat5_prob = av1_cat5_prob;
127 cat6_prob = av1_cat6_prob;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 }
129#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700130 cat1_prob = av1_cat1_prob;
131 cat2_prob = av1_cat2_prob;
132 cat3_prob = av1_cat3_prob;
133 cat4_prob = av1_cat4_prob;
134 cat5_prob = av1_cat5_prob;
135 cat6_prob = av1_cat6_prob;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700136#endif
137
138 dq_shift = get_tx_scale(xd, tx_type, tx_size);
139
140 while (c < max_eob) {
141 int val = -1;
142 band = *band_translate++;
143 prob = coef_probs[band][ctx];
144 if (counts) ++eob_branch_count[band][ctx];
Michael Bebenita6048d052016-08-25 14:40:54 -0700145 if (!aom_read(r, prob[EOB_CONTEXT_NODE], ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700146 INCREMENT_COUNT(EOB_MODEL_TOKEN);
147 break;
148 }
149
150#if CONFIG_NEW_QUANT
151 dqv_val = &dq_val[band][0];
152#endif // CONFIG_NEW_QUANT
153
Michael Bebenita6048d052016-08-25 14:40:54 -0700154 while (!aom_read(r, prob[ZERO_CONTEXT_NODE], ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155 INCREMENT_COUNT(ZERO_TOKEN);
156 dqv = dq[1];
157 token_cache[scan[c]] = 0;
158 ++c;
159 if (c >= max_eob) return c; // zero tokens at the end (no eob token)
160 ctx = get_coef_context(nb, token_cache, c);
161 band = *band_translate++;
162 prob = coef_probs[band][ctx];
163#if CONFIG_NEW_QUANT
164 dqv_val = &dq_val[band][0];
165#endif // CONFIG_NEW_QUANT
166 }
Jingning Han1be18782016-10-21 11:48:15 -0700167
168 *max_scan_line = AOMMAX(*max_scan_line, scan[c]);
169
Alex Converseaca9feb2016-10-10 11:08:10 -0700170#if CONFIG_EC_MULTISYMBOL
Alex Converse5e4d00c2016-10-12 13:23:33 -0700171 cdf = &coef_cdfs[band][ctx];
Michael Bebenita6048d052016-08-25 14:40:54 -0700172 token = ONE_TOKEN +
173 aom_read_symbol(r, *cdf, CATEGORY6_TOKEN - ONE_TOKEN + 1, ACCT_STR);
Alex Converse5e4d00c2016-10-12 13:23:33 -0700174 INCREMENT_COUNT(ONE_TOKEN + (token > ONE_TOKEN));
175 switch (token) {
176 case ONE_TOKEN:
177 case TWO_TOKEN:
178 case THREE_TOKEN:
179 case FOUR_TOKEN: val = token; break;
180 case CATEGORY1_TOKEN:
181 val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
182 break;
183 case CATEGORY2_TOKEN:
184 val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
185 break;
186 case CATEGORY3_TOKEN:
187 val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
188 break;
189 case CATEGORY4_TOKEN:
190 val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
191 break;
192 case CATEGORY5_TOKEN:
193 val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
194 break;
195 case CATEGORY6_TOKEN: {
196 const int skip_bits = TX_SIZES - 1 - txsize_sqr_up_map[tx_size];
197 const uint8_t *cat6p = cat6_prob + skip_bits;
198#if CONFIG_AOM_HIGHBITDEPTH
199 switch (xd->bd) {
200 case AOM_BITS_8:
201 val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
202 break;
203 case AOM_BITS_10:
204 val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, r);
205 break;
206 case AOM_BITS_12:
207 val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, r);
208 break;
209 default: assert(0); return -1;
210 }
211#else
212 val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
213#endif
214 } break;
215 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700216#else // CONFIG_EC_MULTISYMBOL
Michael Bebenita6048d052016-08-25 14:40:54 -0700217 if (!aom_read(r, prob[ONE_CONTEXT_NODE], ACCT_STR)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218 INCREMENT_COUNT(ONE_TOKEN);
219 token = ONE_TOKEN;
220 val = 1;
221 } else {
222 INCREMENT_COUNT(TWO_TOKEN);
Yaowu Xuf883b422016-08-30 14:01:10 -0700223 token = aom_read_tree(r, av1_coef_con_tree,
Michael Bebenita6048d052016-08-25 14:40:54 -0700224 av1_pareto8_full[prob[PIVOT_NODE] - 1], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225 switch (token) {
226 case TWO_TOKEN:
227 case THREE_TOKEN:
228 case FOUR_TOKEN: val = token; break;
229 case CATEGORY1_TOKEN:
230 val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
231 break;
232 case CATEGORY2_TOKEN:
233 val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
234 break;
235 case CATEGORY3_TOKEN:
236 val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
237 break;
238 case CATEGORY4_TOKEN:
239 val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
240 break;
241 case CATEGORY5_TOKEN:
242 val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
243 break;
244 case CATEGORY6_TOKEN: {
245 const int skip_bits = TX_SIZES - 1 - txsize_sqr_up_map[tx_size];
246 const uint8_t *cat6p = cat6_prob + skip_bits;
Yaowu Xuf883b422016-08-30 14:01:10 -0700247#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700248 switch (xd->bd) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700249 case AOM_BITS_8:
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
251 break;
Yaowu Xuf883b422016-08-30 14:01:10 -0700252 case AOM_BITS_10:
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253 val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, r);
254 break;
Yaowu Xuf883b422016-08-30 14:01:10 -0700255 case AOM_BITS_12:
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256 val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, r);
257 break;
258 default: assert(0); return -1;
259 }
260#else
261 val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
262#endif
263 break;
264 }
265 }
266 }
Alex Converseaca9feb2016-10-10 11:08:10 -0700267#endif // CONFIG_EC_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700268#if CONFIG_NEW_QUANT
Yaowu Xuf883b422016-08-30 14:01:10 -0700269 v = av1_dequant_abscoeff_nuq(val, dqv, dqv_val);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 v = dq_shift ? ROUND_POWER_OF_TWO(v, dq_shift) : v;
271#else
272#if CONFIG_AOM_QM
273 dqv = ((iqmatrix[scan[c]] * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >>
274 AOM_QM_BITS;
275#endif
276 v = (val * dqv) >> dq_shift;
277#endif // CONFIG_NEW_QUANT
278
279#if CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xuf883b422016-08-30 14:01:10 -0700280#if CONFIG_AOM_HIGHBITDEPTH
Michael Bebenita6048d052016-08-25 14:40:54 -0700281 dqcoeff[scan[c]] =
282 highbd_check_range((aom_read_bit(r, ACCT_STR) ? -v : v), xd->bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700283#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700284 dqcoeff[scan[c]] = check_range(aom_read_bit(r, ACCT_STR) ? -v : v);
Yaowu Xuf883b422016-08-30 14:01:10 -0700285#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286#else
Michael Bebenita6048d052016-08-25 14:40:54 -0700287 dqcoeff[scan[c]] = aom_read_bit(r, ACCT_STR) ? -v : v;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288#endif // CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xuf883b422016-08-30 14:01:10 -0700289 token_cache[scan[c]] = av1_pt_energy_class[token];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 ++c;
291 ctx = get_coef_context(nb, token_cache, c);
292 dqv = dq[1];
293 }
294
295 return c;
296}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297
Urvang Joshib100db72016-10-12 16:28:56 -0700298#if CONFIG_PALETTE
Yaowu Xuf883b422016-08-30 14:01:10 -0700299void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
300 aom_reader *r) {
Urvang Joshi967ff392016-09-07 14:57:49 -0700301 const MODE_INFO *const mi = xd->mi[0];
302 const MB_MODE_INFO *const mbmi = &mi->mbmi;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303 const BLOCK_SIZE bsize = mbmi->sb_type;
304 const int rows = (4 * num_4x4_blocks_high_lookup[bsize]) >>
305 (xd->plane[plane != 0].subsampling_y);
306 const int cols = (4 * num_4x4_blocks_wide_lookup[bsize]) >>
307 (xd->plane[plane != 0].subsampling_x);
Urvang Joshi967ff392016-09-07 14:57:49 -0700308 uint8_t color_order[PALETTE_MAX_SIZE];
309 const int n = mbmi->palette_mode_info.palette_size[plane != 0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310 int i, j;
311 uint8_t *color_map = xd->plane[plane != 0].color_index_map;
clang-format67948d32016-09-07 22:40:40 -0700312 const aom_prob(*const prob)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700313 plane ? av1_default_palette_uv_color_prob
314 : av1_default_palette_y_color_prob;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315
316 for (i = 0; i < rows; ++i) {
317 for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
Urvang Joshi967ff392016-09-07 14:57:49 -0700318 const int color_ctx = av1_get_palette_color_context(color_map, cols, i, j,
319 n, color_order, NULL);
320 const int color_idx = aom_read_tree(r, av1_palette_color_tree[n - 2],
321 prob[n - 2][color_ctx], ACCT_STR);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700322 assert(color_idx >= 0 && color_idx < n);
323 color_map[i * cols + j] = color_order[color_idx];
324 }
325 }
326}
Urvang Joshib100db72016-10-12 16:28:56 -0700327#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328
Yaowu Xuf883b422016-08-30 14:01:10 -0700329int av1_decode_block_tokens(MACROBLOCKD *const xd, int plane,
Urvang Joshi03f6fdc2016-10-14 15:53:39 -0700330 const SCAN_ORDER *sc, int x, int y, TX_SIZE tx_size,
Jingning Han1be18782016-10-21 11:48:15 -0700331 TX_TYPE tx_type, int16_t *max_scan_line,
332 aom_reader *r, int seg_id) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333 struct macroblockd_plane *const pd = &xd->plane[plane];
334 const int16_t *const dequant = pd->seg_dequant[seg_id];
335 const int ctx =
336 get_entropy_context(tx_size, pd->above_context + x, pd->left_context + y);
337#if CONFIG_NEW_QUANT
Debargha Mukherjee9324d382016-09-23 10:52:13 -0700338 const int ref = is_inter_block(&xd->mi[0]->mbmi);
Debargha Mukherjee3c42c092016-09-29 09:17:36 -0700339 int dq =
340 get_dq_profile_from_ctx(xd->qindex[seg_id], ctx, ref, pd->plane_type);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341#endif // CONFIG_NEW_QUANT
342
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343#if CONFIG_AOM_QM
Jingning Han1be18782016-10-21 11:48:15 -0700344 const int eob = decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size,
345 tx_type, dequant, ctx, sc->scan, sc->neighbors,
346 &sc->max_scan_line, r, pd->seg_iqmatrix[seg_id]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347#else
348 const int eob =
349 decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size, tx_type, dequant,
350#if CONFIG_NEW_QUANT
351 pd->seg_dequant_nuq[seg_id][dq],
352#endif // CONFIG_NEW_QUANT
Jingning Han1be18782016-10-21 11:48:15 -0700353 ctx, sc->scan, sc->neighbors, max_scan_line, r);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700354#endif // CONFIG_AOM_QM
Jingning Hana6923f72016-07-15 08:50:14 -0700355 av1_set_contexts(xd, pd, tx_size, eob > 0, x, y);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700356 return eob;
357}