blob: 68d87cbdc741c02304db3083e418f18cbe210a79 [file] [log] [blame]
Jingning Han3ee6db62015-08-05 19:00:31 -07001/*
Adrian Grangea872b062016-03-24 11:38:32 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han3ee6db62015-08-05 19:00:31 -07003 *
Adrian Grangea872b062016-03-24 11:38:32 -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.
Jingning Han3ee6db62015-08-05 19:00:31 -070010 */
11
Yushin Cho09705fe2016-11-04 16:36:56 -070012#if !CONFIG_PVQ
Adrian Grangecebe6f02016-03-25 12:11:05 -070013#include "aom_mem/aom_mem.h"
Yaowu Xubf4202e2016-03-21 15:15:19 -070014#include "aom_ports/mem.h"
Yushin Cho09705fe2016-11-04 16:36:56 -070015#endif
Jingning Han3ee6db62015-08-05 19:00:31 -070016
Yushin Cho09705fe2016-11-04 16:36:56 -070017#if !CONFIG_PVQ
Yaowu Xucfea7dd2016-03-22 09:52:13 -070018#include "av1/common/blockd.h"
19#include "av1/common/common.h"
20#include "av1/common/entropy.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070021#if CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xucfea7dd2016-03-22 09:52:13 -070022#include "av1/common/idct.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070023#endif
24
Yaowu Xucfea7dd2016-03-22 09:52:13 -070025#include "av1/decoder/detokenize.h"
Jingning Han3ee6db62015-08-05 19:00:31 -070026
Michael Bebenitae6b12942016-08-25 14:40:54 -070027#define ACCT_STR __func__
28
clang-format99e28b82016-01-27 12:42:45 -080029#define EOB_CONTEXT_NODE 0
30#define ZERO_CONTEXT_NODE 1
31#define ONE_CONTEXT_NODE 2
32#define LOW_VAL_CONTEXT_NODE 0
33#define TWO_CONTEXT_NODE 1
34#define THREE_CONTEXT_NODE 2
35#define HIGH_LOW_CONTEXT_NODE 3
36#define CAT_ONE_CONTEXT_NODE 4
37#define CAT_THREEFOUR_CONTEXT_NODE 5
38#define CAT_THREE_CONTEXT_NODE 6
39#define CAT_FIVE_CONTEXT_NODE 7
Jingning Han3ee6db62015-08-05 19:00:31 -070040
clang-format99e28b82016-01-27 12:42:45 -080041#define INCREMENT_COUNT(token) \
42 do { \
43 if (counts) ++coef_counts[band][ctx][token]; \
Jingning Han3ee6db62015-08-05 19:00:31 -070044 } while (0)
45
Adrian Grangecebe6f02016-03-25 12:11:05 -070046static INLINE int read_coeff(const aom_prob *probs, int n, aom_reader *r) {
Jingning Han3ee6db62015-08-05 19:00:31 -070047 int i, val = 0;
Michael Bebenitae6b12942016-08-25 14:40:54 -070048 for (i = 0; i < n; ++i) val = (val << 1) | aom_read(r, probs[i], ACCT_STR);
Jingning Han3ee6db62015-08-05 19:00:31 -070049 return val;
50}
51
Thomas1c122c22016-02-19 09:06:12 +000052#if CONFIG_AOM_QM
Thomasb0112df2016-09-23 18:04:17 +010053static int decode_coefs(MACROBLOCKD *xd, PLANE_TYPE type, tran_low_t *dqcoeff,
54 TX_SIZE tx_size, const int16_t *dq, int ctx,
55 const int16_t *scan, const int16_t *nb, aom_reader *r,
56 const qm_val_t *iqm[2][TX_SIZES])
Thomas1c122c22016-02-19 09:06:12 +000057#else
Thomasb0112df2016-09-23 18:04:17 +010058static int decode_coefs(MACROBLOCKD *xd, PLANE_TYPE type, tran_low_t *dqcoeff,
59 TX_SIZE tx_size, const int16_t *dq, int ctx,
60 const int16_t *scan, const int16_t *nb, aom_reader *r)
Thomas1c122c22016-02-19 09:06:12 +000061#endif
62{
Jingning Han3ee6db62015-08-05 19:00:31 -070063 FRAME_COUNTS *counts = xd->counts;
Jingning Han9662fde2016-09-01 12:09:20 -070064 const int max_eob = 1 << (tx_size_1d_log2[tx_size] * 2);
Thomasb0112df2016-09-23 18:04:17 +010065 FRAME_CONTEXT *const fc = xd->fc;
Jingning Han3ee6db62015-08-05 19:00:31 -070066 const int ref = is_inter_block(&xd->mi[0]->mbmi);
Thomas1c122c22016-02-19 09:06:12 +000067#if CONFIG_AOM_QM
68 const qm_val_t *iqmatrix = iqm[!ref][tx_size];
69#endif
Jingning Han3ee6db62015-08-05 19:00:31 -070070 int band, c = 0;
Thomasb0112df2016-09-23 18:04:17 +010071 aom_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
Jingning Han3ee6db62015-08-05 19:00:31 -070072 fc->coef_probs[tx_size][type][ref];
Adrian Grangecebe6f02016-03-25 12:11:05 -070073 const aom_prob *prob;
Alex Conversef2753c32016-10-10 11:08:10 -070074#if CONFIG_EC_MULTISYMBOL
Thomasb0112df2016-09-23 18:04:17 +010075 aom_cdf_prob(*coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
Alex Converse362888b2016-06-23 15:31:42 -070076 fc->coef_cdfs[tx_size][type][ref];
Thomasb0112df2016-09-23 18:04:17 +010077 aom_cdf_prob(*cdf)[ENTROPY_TOKENS];
Alex Conversef2753c32016-10-10 11:08:10 -070078#endif // CONFIG_EC_MULTISYMBOL
clang-format99e28b82016-01-27 12:42:45 -080079 unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
80 unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
Jingning Han3ee6db62015-08-05 19:00:31 -070081 uint8_t token_cache[32 * 32];
82 const uint8_t *band_translate = get_band_translate(tx_size);
83 const int dq_shift = (tx_size == TX_32X32);
84 int v, token;
85 int16_t dqv = dq[0];
86 const uint8_t *cat1_prob;
87 const uint8_t *cat2_prob;
88 const uint8_t *cat3_prob;
89 const uint8_t *cat4_prob;
90 const uint8_t *cat5_prob;
91 const uint8_t *cat6_prob;
92
93 if (counts) {
94 coef_counts = counts->coef[tx_size][type][ref];
95 eob_branch_count = counts->eob_branch[tx_size][type][ref];
96 }
97
Yaowu Xu01dee0b2016-03-25 12:43:01 -070098#if CONFIG_AOM_HIGHBITDEPTH
Adrian Grangeff00fc02016-03-25 12:57:08 -070099 if (xd->bd > AOM_BITS_8) {
100 if (xd->bd == AOM_BITS_10) {
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700101 cat1_prob = av1_cat1_prob_high10;
102 cat2_prob = av1_cat2_prob_high10;
103 cat3_prob = av1_cat3_prob_high10;
104 cat4_prob = av1_cat4_prob_high10;
105 cat5_prob = av1_cat5_prob_high10;
106 cat6_prob = av1_cat6_prob_high10;
Jingning Han3ee6db62015-08-05 19:00:31 -0700107 } else {
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700108 cat1_prob = av1_cat1_prob_high12;
109 cat2_prob = av1_cat2_prob_high12;
110 cat3_prob = av1_cat3_prob_high12;
111 cat4_prob = av1_cat4_prob_high12;
112 cat5_prob = av1_cat5_prob_high12;
113 cat6_prob = av1_cat6_prob_high12;
Jingning Han3ee6db62015-08-05 19:00:31 -0700114 }
115 } else {
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700116 cat1_prob = av1_cat1_prob;
117 cat2_prob = av1_cat2_prob;
118 cat3_prob = av1_cat3_prob;
119 cat4_prob = av1_cat4_prob;
120 cat5_prob = av1_cat5_prob;
121 cat6_prob = av1_cat6_prob;
Jingning Han3ee6db62015-08-05 19:00:31 -0700122 }
123#else
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700124 cat1_prob = av1_cat1_prob;
125 cat2_prob = av1_cat2_prob;
126 cat3_prob = av1_cat3_prob;
127 cat4_prob = av1_cat4_prob;
128 cat5_prob = av1_cat5_prob;
129 cat6_prob = av1_cat6_prob;
Jingning Han3ee6db62015-08-05 19:00:31 -0700130#endif
131
132 while (c < max_eob) {
133 int val = -1;
134 band = *band_translate++;
135 prob = coef_probs[band][ctx];
clang-format99e28b82016-01-27 12:42:45 -0800136 if (counts) ++eob_branch_count[band][ctx];
Michael Bebenitae6b12942016-08-25 14:40:54 -0700137 if (!aom_read(r, prob[EOB_CONTEXT_NODE], ACCT_STR)) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700138 INCREMENT_COUNT(EOB_MODEL_TOKEN);
139 break;
140 }
141
Michael Bebenitae6b12942016-08-25 14:40:54 -0700142 while (!aom_read(r, prob[ZERO_CONTEXT_NODE], ACCT_STR)) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700143 INCREMENT_COUNT(ZERO_TOKEN);
144 dqv = dq[1];
145 token_cache[scan[c]] = 0;
146 ++c;
clang-format99e28b82016-01-27 12:42:45 -0800147 if (c >= max_eob) return c; // zero tokens at the end (no eob token)
Jingning Han3ee6db62015-08-05 19:00:31 -0700148 ctx = get_coef_context(nb, token_cache, c);
149 band = *band_translate++;
150 prob = coef_probs[band][ctx];
151 }
152
Alex Conversef2753c32016-10-10 11:08:10 -0700153#if CONFIG_EC_MULTISYMBOL
Alex Converse362888b2016-06-23 15:31:42 -0700154 cdf = &coef_cdfs[band][ctx];
Michael Bebenitae6b12942016-08-25 14:40:54 -0700155 token = ONE_TOKEN +
156 aom_read_symbol(r, *cdf, CATEGORY6_TOKEN - ONE_TOKEN + 1, ACCT_STR);
Alex Converse362888b2016-06-23 15:31:42 -0700157 INCREMENT_COUNT(ONE_TOKEN + (token > ONE_TOKEN));
158 switch (token) {
159 case ONE_TOKEN:
160 case TWO_TOKEN:
161 case THREE_TOKEN:
162 case FOUR_TOKEN: val = token; break;
163 case CATEGORY1_TOKEN:
164 val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
165 break;
166 case CATEGORY2_TOKEN:
167 val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
168 break;
169 case CATEGORY3_TOKEN:
170 val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
171 break;
172 case CATEGORY4_TOKEN:
173 val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
174 break;
175 case CATEGORY5_TOKEN:
176 val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
177 break;
178 case CATEGORY6_TOKEN: {
Alex Converse362888b2016-06-23 15:31:42 -0700179 const int skip_bits = TX_SIZES - 1 - tx_size;
Alex Converse362888b2016-06-23 15:31:42 -0700180 const uint8_t *cat6p = cat6_prob + skip_bits;
181#if CONFIG_AOM_HIGHBITDEPTH
182 switch (xd->bd) {
183 case AOM_BITS_8:
184 val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
185 break;
186 case AOM_BITS_10:
187 val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, r);
188 break;
189 case AOM_BITS_12:
190 val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, r);
191 break;
192 default: assert(0); return -1;
193 }
194#else
195 val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
196#endif
197 break;
198 }
199 }
Alex Conversef2753c32016-10-10 11:08:10 -0700200#else // CONFIG_EC_MULTISYMBOL
Michael Bebenitae6b12942016-08-25 14:40:54 -0700201 if (!aom_read(r, prob[ONE_CONTEXT_NODE], ACCT_STR)) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700202 INCREMENT_COUNT(ONE_TOKEN);
203 token = ONE_TOKEN;
204 val = 1;
205 } else {
206 INCREMENT_COUNT(TWO_TOKEN);
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700207 token = aom_read_tree(r, av1_coef_con_tree,
Michael Bebenitae6b12942016-08-25 14:40:54 -0700208 av1_pareto8_full[prob[PIVOT_NODE] - 1], ACCT_STR);
Jingning Han3ee6db62015-08-05 19:00:31 -0700209 switch (token) {
210 case TWO_TOKEN:
211 case THREE_TOKEN:
clang-format99e28b82016-01-27 12:42:45 -0800212 case FOUR_TOKEN: val = token; break;
Jingning Han3ee6db62015-08-05 19:00:31 -0700213 case CATEGORY1_TOKEN:
214 val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
215 break;
216 case CATEGORY2_TOKEN:
217 val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
218 break;
219 case CATEGORY3_TOKEN:
220 val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
221 break;
222 case CATEGORY4_TOKEN:
223 val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
224 break;
225 case CATEGORY5_TOKEN:
226 val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
227 break;
Ronald S. Bultje3461e8c2015-09-30 21:37:20 -0400228 case CATEGORY6_TOKEN: {
Ronald S. Bultje3461e8c2015-09-30 21:37:20 -0400229 const int skip_bits = TX_SIZES - 1 - tx_size;
Ronald S. Bultje3461e8c2015-09-30 21:37:20 -0400230 const uint8_t *cat6p = cat6_prob + skip_bits;
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700231#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han3ee6db62015-08-05 19:00:31 -0700232 switch (xd->bd) {
Adrian Grangeff00fc02016-03-25 12:57:08 -0700233 case AOM_BITS_8:
Ronald S. Bultje3461e8c2015-09-30 21:37:20 -0400234 val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
Jingning Han3ee6db62015-08-05 19:00:31 -0700235 break;
Adrian Grangeff00fc02016-03-25 12:57:08 -0700236 case AOM_BITS_10:
Ronald S. Bultje3461e8c2015-09-30 21:37:20 -0400237 val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, r);
Jingning Han3ee6db62015-08-05 19:00:31 -0700238 break;
Adrian Grangeff00fc02016-03-25 12:57:08 -0700239 case AOM_BITS_12:
Ronald S. Bultje3461e8c2015-09-30 21:37:20 -0400240 val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, r);
Jingning Han3ee6db62015-08-05 19:00:31 -0700241 break;
clang-format99e28b82016-01-27 12:42:45 -0800242 default: assert(0); return -1;
Jingning Han3ee6db62015-08-05 19:00:31 -0700243 }
244#else
Ronald S. Bultje3461e8c2015-09-30 21:37:20 -0400245 val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
Jingning Han3ee6db62015-08-05 19:00:31 -0700246#endif
247 break;
Ronald S. Bultje3461e8c2015-09-30 21:37:20 -0400248 }
Jingning Han3ee6db62015-08-05 19:00:31 -0700249 }
250 }
Alex Conversef2753c32016-10-10 11:08:10 -0700251#endif // CONFIG_EC_MULTISYMBOL
Thomas1c122c22016-02-19 09:06:12 +0000252#if CONFIG_AOM_QM
253 dqv = ((iqmatrix[scan[c]] * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >>
254 AOM_QM_BITS;
255#endif
Jingning Han3ee6db62015-08-05 19:00:31 -0700256 v = (val * dqv) >> dq_shift;
257#if CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700258#if CONFIG_AOM_HIGHBITDEPTH
Michael Bebenitae6b12942016-08-25 14:40:54 -0700259 dqcoeff[scan[c]] =
260 highbd_check_range((aom_read_bit(r, ACCT_STR) ? -v : v), xd->bd);
Jingning Han3ee6db62015-08-05 19:00:31 -0700261#else
Michael Bebenitae6b12942016-08-25 14:40:54 -0700262 dqcoeff[scan[c]] = check_range(aom_read_bit(r, ACCT_STR) ? -v : v);
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700263#endif // CONFIG_AOM_HIGHBITDEPTH
Jingning Han3ee6db62015-08-05 19:00:31 -0700264#else
Michael Bebenitae6b12942016-08-25 14:40:54 -0700265 dqcoeff[scan[c]] = aom_read_bit(r, ACCT_STR) ? -v : v;
Jingning Han3ee6db62015-08-05 19:00:31 -0700266#endif // CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700267 token_cache[scan[c]] = av1_pt_energy_class[token];
Jingning Han3ee6db62015-08-05 19:00:31 -0700268 ++c;
269 ctx = get_coef_context(nb, token_cache, c);
270 dqv = dq[1];
271 }
272
273 return c;
274}
275
Urvang Joshi764d1262016-08-24 15:52:38 -0700276#if CONFIG_PALETTE
277void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
278 aom_reader *r) {
Urvang Joshib1c3bb52016-09-07 14:57:49 -0700279 const MODE_INFO *const mi = xd->mi[0];
280 const MB_MODE_INFO *const mbmi = &mi->mbmi;
Urvang Joshi764d1262016-08-24 15:52:38 -0700281 const BLOCK_SIZE bsize = mbmi->sb_type;
282 const int rows = (4 * num_4x4_blocks_high_lookup[bsize]) >>
283 (xd->plane[plane != 0].subsampling_y);
284 const int cols = (4 * num_4x4_blocks_wide_lookup[bsize]) >>
285 (xd->plane[plane != 0].subsampling_x);
Urvang Joshib1c3bb52016-09-07 14:57:49 -0700286 uint8_t color_order[PALETTE_MAX_SIZE];
287 const int n = mbmi->palette_mode_info.palette_size[plane != 0];
Urvang Joshi764d1262016-08-24 15:52:38 -0700288 int i, j;
289 uint8_t *color_map = xd->plane[plane != 0].color_index_map;
clang-formatf9562ae2016-09-17 11:45:12 -0700290 const aom_prob(*const prob)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
Urvang Joshi764d1262016-08-24 15:52:38 -0700291 plane ? av1_default_palette_uv_color_prob
292 : av1_default_palette_y_color_prob;
293
294 for (i = 0; i < rows; ++i) {
295 for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
Urvang Joshib1c3bb52016-09-07 14:57:49 -0700296 const int color_ctx = av1_get_palette_color_context(color_map, cols, i, j,
297 n, color_order, NULL);
298 const int color_idx = aom_read_tree(r, av1_palette_color_tree[n - 2],
299 prob[n - 2][color_ctx], ACCT_STR);
Urvang Joshi764d1262016-08-24 15:52:38 -0700300 assert(color_idx >= 0 && color_idx < n);
301 color_map[i * cols + j] = color_order[color_idx];
302 }
303 }
304}
305#endif // CONFIG_PALETTE
306
Urvang Joshi30abc082016-07-20 15:17:19 -0700307int av1_decode_block_tokens(MACROBLOCKD *xd, int plane, const SCAN_ORDER *sc,
clang-formata2dc61c2016-05-09 23:23:47 -0700308 int x, int y, TX_SIZE tx_size, aom_reader *r,
309 int seg_id) {
Jingning Han3ee6db62015-08-05 19:00:31 -0700310 struct macroblockd_plane *const pd = &xd->plane[plane];
311 const int16_t *const dequant = pd->seg_dequant[seg_id];
clang-format99e28b82016-01-27 12:42:45 -0800312 const int ctx =
313 get_entropy_context(tx_size, pd->above_context + x, pd->left_context + y);
Thomas1c122c22016-02-19 09:06:12 +0000314#if CONFIG_AOM_QM
315 const int eob =
316 decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size, dequant, ctx,
317 sc->scan, sc->neighbors, r, pd->seg_iqmatrix[seg_id]);
318#else
clang-format99e28b82016-01-27 12:42:45 -0800319 const int eob = decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size,
Jingning Han3ee6db62015-08-05 19:00:31 -0700320 dequant, ctx, sc->scan, sc->neighbors, r);
Thomas1c122c22016-02-19 09:06:12 +0000321#endif
Jingning Hanb92bd692016-07-15 08:50:14 -0700322 av1_set_contexts(xd, pd, tx_size, eob > 0, x, y);
Jingning Han3ee6db62015-08-05 19:00:31 -0700323 return eob;
324}
Yushin Cho09705fe2016-11-04 16:36:56 -0700325#endif