detokenize.c 16.9 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
 */

12
#if !CONFIG_PVQ
Yaowu Xu's avatar
Yaowu Xu committed
13
#include "aom_mem/aom_mem.h"
14
#include "aom_ports/mem.h"
15
16
#endif
#if !CONFIG_PVQ
Alex Converse's avatar
Alex Converse committed
17
18
19
#if CONFIG_ANS
#include "aom_dsp/ans.h"
#endif  // CONFIG_ANS
20
21
22
23
#include "av1/common/blockd.h"
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/idct.h"
Jingning Han's avatar
Jingning Han committed
24

25
#include "av1/decoder/detokenize.h"
Jingning Han's avatar
Jingning Han committed
26

Michael Bebenita's avatar
Michael Bebenita committed
27
28
#define ACCT_STR __func__

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define EOB_CONTEXT_NODE 0
#define ZERO_CONTEXT_NODE 1
#define ONE_CONTEXT_NODE 2
#define LOW_VAL_CONTEXT_NODE 0
#define TWO_CONTEXT_NODE 1
#define THREE_CONTEXT_NODE 2
#define HIGH_LOW_CONTEXT_NODE 3
#define CAT_ONE_CONTEXT_NODE 4
#define CAT_THREEFOUR_CONTEXT_NODE 5
#define CAT_THREE_CONTEXT_NODE 6
#define CAT_FIVE_CONTEXT_NODE 7

#define INCREMENT_COUNT(token)                   \
  do {                                           \
    if (counts) ++coef_counts[band][ctx][token]; \
Jingning Han's avatar
Jingning Han committed
44
45
  } while (0)

Yaowu Xu's avatar
Yaowu Xu committed
46
static INLINE int read_coeff(const aom_prob *probs, int n, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
47
  int i, val = 0;
Michael Bebenita's avatar
Michael Bebenita committed
48
  for (i = 0; i < n; ++i) val = (val << 1) | aom_read(r, probs[i], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
49
50
51
  return val;
}

52
#if CONFIG_AOM_QM
53
54
static int decode_coefs(MACROBLOCKD *xd, PLANE_TYPE type, tran_low_t *dqcoeff,
                        TX_SIZE tx_size, TX_TYPE tx_type, const int16_t *dq,
55
56
57
#if CONFIG_NEW_QUANT
                        dequant_val_type_nuq *dq_val,
#endif  // CONFIG_NEW_QUANT
58
                        int ctx, const int16_t *scan, const int16_t *nb,
Yaowu Xu's avatar
Yaowu Xu committed
59
60
                        int16_t *max_scan_line, aom_reader *r,
                        const qm_val_t *iqm[2][TX_SIZES])
61
#else
62
63
static int decode_coefs(MACROBLOCKD *xd, PLANE_TYPE type, tran_low_t *dqcoeff,
                        TX_SIZE tx_size, TX_TYPE tx_type, const int16_t *dq,
64
65
66
#if CONFIG_NEW_QUANT
                        dequant_val_type_nuq *dq_val,
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
67
                        int ctx, const int16_t *scan, const int16_t *nb,
68
                        int16_t *max_scan_line, aom_reader *r)
69
#endif  // CONFIG_AOM_QM
70
{
Jingning Han's avatar
Jingning Han committed
71
  FRAME_COUNTS *counts = xd->counts;
72
73
74
75
76
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#else
  FRAME_CONTEXT *const ec_ctx = xd->fc;
#endif
77
  const int max_eob = tx_size_2d[tx_size];
Jingning Han's avatar
Jingning Han committed
78
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
79
80
#if CONFIG_AOM_QM
  const qm_val_t *iqmatrix = iqm[!ref][tx_size];
81
#endif  // CONFIG_AOM_QM
Jingning Han's avatar
Jingning Han committed
82
  int band, c = 0;
83
  const int tx_size_ctx = txsize_sqr_map[tx_size];
84
#if CONFIG_NEW_TOKENSET
85
  aom_cdf_prob(*coef_head_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
86
      ec_ctx->coef_head_cdfs[tx_size_ctx][type][ref];
87
  aom_cdf_prob(*coef_tail_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
88
      ec_ctx->coef_tail_cdfs[tx_size_ctx][type][ref];
89
90
  aom_cdf_prob(*cdf_head)[ENTROPY_TOKENS];
  aom_cdf_prob(*cdf_tail)[ENTROPY_TOKENS];
91
92
  int val = 0;
  unsigned int *blockz_count;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#else
  aom_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
      ec_ctx->coef_probs[tx_size_ctx][type][ref];
  const aom_prob *prob;
#if CONFIG_EC_ADAPT
  aom_cdf_prob(*coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
      ec_ctx->coef_cdfs[tx_size][type][ref];
  aom_cdf_prob(*cdf)[ENTROPY_TOKENS];
#elif CONFIG_EC_MULTISYMBOL
  aom_cdf_prob(*coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
      ec_ctx->coef_cdfs[tx_size_ctx][type][ref];
  aom_cdf_prob(*cdf)[ENTROPY_TOKENS];
#endif  // CONFIG_EC_ADAPT
#endif  // CONFIG_NEW_TOKENSET
Thomas Davies's avatar
Thomas Davies committed
107
  unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1] = NULL;
108
  unsigned int(*eob_branch_count)[COEFF_CONTEXTS] = NULL;
109
  uint8_t token_cache[MAX_TX_SQUARE];
Jingning Han's avatar
Jingning Han committed
110
  const uint8_t *band_translate = get_band_translate(tx_size);
111
  int dq_shift;
Jingning Han's avatar
Jingning Han committed
112
113
  int v, token;
  int16_t dqv = dq[0];
114
115
116
#if CONFIG_NEW_QUANT
  const tran_low_t *dqv_val = &dq_val[0][0];
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
117
118
119
120
121
122
  const uint8_t *cat1_prob;
  const uint8_t *cat2_prob;
  const uint8_t *cat3_prob;
  const uint8_t *cat4_prob;
  const uint8_t *cat5_prob;
  const uint8_t *cat6_prob;
123
  (void)tx_type;
124
125
126
#if CONFIG_AOM_QM
  (void)iqmatrix;
#endif  // CONFIG_AOM_QM
Jingning Han's avatar
Jingning Han committed
127
128

  if (counts) {
129
130
    coef_counts = counts->coef[tx_size_ctx][type][ref];
    eob_branch_count = counts->eob_branch[tx_size_ctx][type][ref];
131
#if CONFIG_NEW_TOKENSET
132
133
    blockz_count = counts->blockz_count[tx_size_ctx][type][ref][ctx];
#endif
Jingning Han's avatar
Jingning Han committed
134
135
  }

Yaowu Xu's avatar
Yaowu Xu committed
136
137
138
139
140
141
142
143
144
#if CONFIG_AOM_HIGHBITDEPTH
  if (xd->bd > AOM_BITS_8) {
    if (xd->bd == AOM_BITS_10) {
      cat1_prob = av1_cat1_prob_high10;
      cat2_prob = av1_cat2_prob_high10;
      cat3_prob = av1_cat3_prob_high10;
      cat4_prob = av1_cat4_prob_high10;
      cat5_prob = av1_cat5_prob_high10;
      cat6_prob = av1_cat6_prob_high10;
Jingning Han's avatar
Jingning Han committed
145
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
146
147
148
149
150
151
      cat1_prob = av1_cat1_prob_high12;
      cat2_prob = av1_cat2_prob_high12;
      cat3_prob = av1_cat3_prob_high12;
      cat4_prob = av1_cat4_prob_high12;
      cat5_prob = av1_cat5_prob_high12;
      cat6_prob = av1_cat6_prob_high12;
Jingning Han's avatar
Jingning Han committed
152
153
    }
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
154
155
156
157
158
159
    cat1_prob = av1_cat1_prob;
    cat2_prob = av1_cat2_prob;
    cat3_prob = av1_cat3_prob;
    cat4_prob = av1_cat4_prob;
    cat5_prob = av1_cat5_prob;
    cat6_prob = av1_cat6_prob;
Jingning Han's avatar
Jingning Han committed
160
161
  }
#else
Yaowu Xu's avatar
Yaowu Xu committed
162
163
164
165
166
167
  cat1_prob = av1_cat1_prob;
  cat2_prob = av1_cat2_prob;
  cat3_prob = av1_cat3_prob;
  cat4_prob = av1_cat4_prob;
  cat5_prob = av1_cat5_prob;
  cat6_prob = av1_cat6_prob;
Jingning Han's avatar
Jingning Han committed
168
169
#endif

170
  dq_shift = get_tx_scale(tx_size);
171

172
#if CONFIG_NEW_TOKENSET
173
174
  band = *band_translate++;

Jingning Han's avatar
Jingning Han committed
175
  while (c < max_eob) {
176
177
    int more_data;
    int comb_token;
Jingning Han's avatar
Jingning Han committed
178

179
180
181
182
#if CONFIG_NEW_QUANT
    dqv_val = &dq_val[band][0];
#endif  // CONFIG_NEW_QUANT

183
184
    cdf_head = &coef_head_cdfs[band][ctx];
    cdf_tail = &coef_tail_cdfs[band][ctx];
185
186
187
188
189
190
191
192
    comb_token = aom_read_symbol(r, *cdf_head, 6, ACCT_STR);
    if (c == 0) {
      if (counts) ++blockz_count[comb_token != 0];
      if (comb_token == 0) return 0;
    }
    token = comb_token >> 1;
    more_data = !token || ((comb_token & 1) == 1);

193
194
    if (token > ONE_TOKEN)
      token += aom_read_symbol(r, *cdf_tail, CATEGORY6_TOKEN + 1 - 2, ACCT_STR);
195
    INCREMENT_COUNT(ZERO_TOKEN + (token > ZERO_TOKEN) + (token > ONE_TOKEN));
196
#if CONFIG_NEW_QUANT
197
    dqv_val = &dq_val[band][0];
198
#endif  // CONFIG_NEW_QUANT
199
200
201

    *max_scan_line = AOMMAX(*max_scan_line, scan[c]);

202
203
204
205
206
207
    if (token) {
      if (counts) ++eob_branch_count[band][ctx];
      if (!more_data) {
        if (counts) ++coef_counts[band][ctx][EOB_MODEL_TOKEN];
      }
    }
208
    token_cache[scan[c]] = av1_pt_energy_class[token];
209

Alex Converse's avatar
Alex Converse committed
210
    switch (token) {
211
      case ZERO_TOKEN:
Alex Converse's avatar
Alex Converse committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
      case ONE_TOKEN:
      case TWO_TOKEN:
      case THREE_TOKEN:
      case FOUR_TOKEN: val = token; break;
      case CATEGORY1_TOKEN:
        val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
        break;
      case CATEGORY2_TOKEN:
        val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
        break;
      case CATEGORY3_TOKEN:
        val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
        break;
      case CATEGORY4_TOKEN:
        val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
        break;
      case CATEGORY5_TOKEN:
        val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
        break;
      case CATEGORY6_TOKEN: {
        const int skip_bits = TX_SIZES - 1 - txsize_sqr_up_map[tx_size];
        const uint8_t *cat6p = cat6_prob + skip_bits;
#if CONFIG_AOM_HIGHBITDEPTH
        switch (xd->bd) {
          case AOM_BITS_8:
            val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
            break;
          case AOM_BITS_10:
            val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, r);
            break;
          case AOM_BITS_12:
            val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, r);
            break;
          default: assert(0); return -1;
        }
#else
        val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
#endif
      } break;
    }
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

#if CONFIG_NEW_QUANT
    v = av1_dequant_abscoeff_nuq(val, dqv, dqv_val);
    v = dq_shift ? ROUND_POWER_OF_TWO(v, dq_shift) : v;
#else
#if CONFIG_AOM_QM
    dqv = ((iqmatrix[scan[c]] * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >>
          AOM_QM_BITS;
#endif
    v = (val * dqv) >> dq_shift;
#endif
#if CONFIG_COEFFICIENT_RANGE_CHECKING
#if CONFIG_AOM_HIGHBITDEPTH
    if (v)
      dqcoeff[scan[c]] =
          highbd_check_range((aom_read_bit(r, ACCT_STR) ? -v : v), xd->bd);
#else
    if (v) dqcoeff[scan[c]] = check_range(aom_read_bit(r, ACCT_STR) ? -v : v);
#endif  // CONFIG_AOM_HIGHBITDEPTH
#else
    if (v) dqcoeff[scan[c]] = aom_read_bit(r, ACCT_STR) ? -v : v;
#endif  // CONFIG_COEFFICIENT_RANGE_CHECKING

    ++c;
276
277
    more_data &= (c < max_eob);
    if (!more_data) break;
278
279
    dqv = dq[1];
    ctx = get_coef_context(nb, token_cache, c);
280
    band = *band_translate++;
281

282
#else  // CONFIG_NEW_TOKENSET
283
284
285
286
287
288
289
290
291
292
293
294
295
  while (c < max_eob) {
    int val = -1;
    band = *band_translate++;
    prob = coef_probs[band][ctx];
    if (counts) ++eob_branch_count[band][ctx];
    if (!aom_read(r, prob[EOB_CONTEXT_NODE], ACCT_STR)) {
      INCREMENT_COUNT(EOB_MODEL_TOKEN);
      break;
    }

#if CONFIG_NEW_QUANT
    dqv_val = &dq_val[band][0];
#endif  // CONFIG_NEW_QUANT
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

    while (!aom_read(r, prob[ZERO_CONTEXT_NODE], ACCT_STR)) {
      INCREMENT_COUNT(ZERO_TOKEN);
      dqv = dq[1];
      token_cache[scan[c]] = 0;
      ++c;
      if (c >= max_eob) return c;  // zero tokens at the end (no eob token)
      ctx = get_coef_context(nb, token_cache, c);
      band = *band_translate++;
      prob = coef_probs[band][ctx];
#if CONFIG_NEW_QUANT
      dqv_val = &dq_val[band][0];
#endif  // CONFIG_NEW_QUANT
    }

    *max_scan_line = AOMMAX(*max_scan_line, scan[c]);

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#if CONFIG_EC_MULTISYMBOL
    cdf = &coef_cdfs[band][ctx];
    token = ONE_TOKEN +
            aom_read_symbol(r, *cdf, CATEGORY6_TOKEN - ONE_TOKEN + 1, ACCT_STR);
    INCREMENT_COUNT(ONE_TOKEN + (token > ONE_TOKEN));
    switch (token) {
      case ONE_TOKEN:
      case TWO_TOKEN:
      case THREE_TOKEN:
      case FOUR_TOKEN: val = token; break;
      case CATEGORY1_TOKEN:
        val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
        break;
      case CATEGORY2_TOKEN:
        val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
        break;
      case CATEGORY3_TOKEN:
        val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
        break;
      case CATEGORY4_TOKEN:
        val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
        break;
      case CATEGORY5_TOKEN:
        val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
        break;
      case CATEGORY6_TOKEN: {
        const int skip_bits = TX_SIZES - 1 - txsize_sqr_up_map[tx_size];
        const uint8_t *cat6p = cat6_prob + skip_bits;
#if CONFIG_AOM_HIGHBITDEPTH
        switch (xd->bd) {
          case AOM_BITS_8:
            val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
            break;
          case AOM_BITS_10:
            val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, r);
            break;
          case AOM_BITS_12:
            val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, r);
            break;
          default: assert(0); return -1;
        }
#else
        val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
#endif
      } break;
    }
#else  // CONFIG_EC_MULTISYMBOL
Michael Bebenita's avatar
Michael Bebenita committed
360
    if (!aom_read(r, prob[ONE_CONTEXT_NODE], ACCT_STR)) {
Jingning Han's avatar
Jingning Han committed
361
362
363
364
365
      INCREMENT_COUNT(ONE_TOKEN);
      token = ONE_TOKEN;
      val = 1;
    } else {
      INCREMENT_COUNT(TWO_TOKEN);
Yaowu Xu's avatar
Yaowu Xu committed
366
      token = aom_read_tree(r, av1_coef_con_tree,
Michael Bebenita's avatar
Michael Bebenita committed
367
                            av1_pareto8_full[prob[PIVOT_NODE] - 1], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
368
369
370
      switch (token) {
        case TWO_TOKEN:
        case THREE_TOKEN:
371
        case FOUR_TOKEN: val = token; break;
Jingning Han's avatar
Jingning Han committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
        case CATEGORY1_TOKEN:
          val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, r);
          break;
        case CATEGORY2_TOKEN:
          val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, r);
          break;
        case CATEGORY3_TOKEN:
          val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, r);
          break;
        case CATEGORY4_TOKEN:
          val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, r);
          break;
        case CATEGORY5_TOKEN:
          val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, r);
          break;
387
        case CATEGORY6_TOKEN: {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
388
          const int skip_bits = TX_SIZES - 1 - txsize_sqr_up_map[tx_size];
389
          const uint8_t *cat6p = cat6_prob + skip_bits;
Yaowu Xu's avatar
Yaowu Xu committed
390
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
391
          switch (xd->bd) {
Yaowu Xu's avatar
Yaowu Xu committed
392
            case AOM_BITS_8:
393
              val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
Jingning Han's avatar
Jingning Han committed
394
              break;
Yaowu Xu's avatar
Yaowu Xu committed
395
            case AOM_BITS_10:
396
              val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, r);
Jingning Han's avatar
Jingning Han committed
397
              break;
Yaowu Xu's avatar
Yaowu Xu committed
398
            case AOM_BITS_12:
399
              val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, r);
Jingning Han's avatar
Jingning Han committed
400
              break;
401
            default: assert(0); return -1;
Jingning Han's avatar
Jingning Han committed
402
403
          }
#else
404
          val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
Jingning Han's avatar
Jingning Han committed
405
406
#endif
          break;
407
        }
Jingning Han's avatar
Jingning Han committed
408
409
      }
    }
410
#endif  // CONFIG_EC_MULTISYMBOL
411
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
412
    v = av1_dequant_abscoeff_nuq(val, dqv, dqv_val);
413
414
    v = dq_shift ? ROUND_POWER_OF_TWO(v, dq_shift) : v;
#else
415
416
417
418
#if CONFIG_AOM_QM
    dqv = ((iqmatrix[scan[c]] * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >>
          AOM_QM_BITS;
#endif
Jingning Han's avatar
Jingning Han committed
419
    v = (val * dqv) >> dq_shift;
420
421
#endif  // CONFIG_NEW_QUANT

Jingning Han's avatar
Jingning Han committed
422
#if CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xu's avatar
Yaowu Xu committed
423
#if CONFIG_AOM_HIGHBITDEPTH
Michael Bebenita's avatar
Michael Bebenita committed
424
425
    dqcoeff[scan[c]] =
        highbd_check_range((aom_read_bit(r, ACCT_STR) ? -v : v), xd->bd);
Jingning Han's avatar
Jingning Han committed
426
#else
Michael Bebenita's avatar
Michael Bebenita committed
427
    dqcoeff[scan[c]] = check_range(aom_read_bit(r, ACCT_STR) ? -v : v);
Yaowu Xu's avatar
Yaowu Xu committed
428
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
429
#else
Michael Bebenita's avatar
Michael Bebenita committed
430
    dqcoeff[scan[c]] = aom_read_bit(r, ACCT_STR) ? -v : v;
Jingning Han's avatar
Jingning Han committed
431
#endif  // CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xu's avatar
Yaowu Xu committed
432
    token_cache[scan[c]] = av1_pt_energy_class[token];
Jingning Han's avatar
Jingning Han committed
433
434
435
    ++c;
    ctx = get_coef_context(nb, token_cache, c);
    dqv = dq[1];
436
#endif  // CONFIG_NEW_TOKENSET
Jingning Han's avatar
Jingning Han committed
437
438
439
440
441
  }

  return c;
}

442
#if CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
443
444
void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
                               aom_reader *r) {
445
446
447
  const MODE_INFO *const mi = xd->mi[0];
  const MB_MODE_INFO *const mbmi = &mi->mbmi;
  uint8_t color_order[PALETTE_MAX_SIZE];
448
  const int n = mbmi->palette_mode_info.palette_size[plane];
hui su's avatar
hui su committed
449
  int i, j;
450
  uint8_t *const color_map = xd->plane[plane].color_index_map;
451
452
453
454
  const aom_prob(*const prob)[PALETTE_COLOR_INDEX_CONTEXTS]
                             [PALETTE_COLORS - 1] =
                                 plane ? av1_default_palette_uv_color_index_prob
                                       : av1_default_palette_y_color_index_prob;
455
456
457
458
  int plane_block_width, plane_block_height, rows, cols;
  av1_get_block_dimensions(mbmi->sb_type, plane, xd, &plane_block_width,
                           &plane_block_height, &rows, &cols);
  assert(plane == 0 || plane == 1);
hui su's avatar
hui su committed
459
460
461

  for (i = 0; i < rows; ++i) {
    for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
462
      const int color_ctx = av1_get_palette_color_index_context(
463
          color_map, plane_block_width, i, j, n, color_order, NULL);
464
465
466
      const int color_idx =
          aom_read_tree(r, av1_palette_color_index_tree[n - 2],
                        prob[n - 2][color_ctx], ACCT_STR);
hui su's avatar
hui su committed
467
      assert(color_idx >= 0 && color_idx < n);
468
      color_map[i * plane_block_width + j] = color_order[color_idx];
hui su's avatar
hui su committed
469
    }
470
471
472
473
474
475
476
477
    memset(color_map + i * plane_block_width + cols,
           color_map[i * plane_block_width + cols - 1],
           (plane_block_width - cols));  // Copy last column to extra columns.
  }
  // Copy last row to extra rows.
  for (i = rows; i < plane_block_height; ++i) {
    memcpy(color_map + i * plane_block_width,
           color_map + (rows - 1) * plane_block_width, plane_block_width);
hui su's avatar
hui su committed
478
479
  }
}
480
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
481

Yaowu Xu's avatar
Yaowu Xu committed
482
int av1_decode_block_tokens(MACROBLOCKD *const xd, int plane,
483
                            const SCAN_ORDER *sc, int x, int y, TX_SIZE tx_size,
484
                            TX_TYPE tx_type, int16_t *max_scan_line,
485
                            aom_reader *r, int seg_id) {
Jingning Han's avatar
Jingning Han committed
486
487
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int16_t *const dequant = pd->seg_dequant[seg_id];
488
489
  const int ctx =
      get_entropy_context(tx_size, pd->above_context + x, pd->left_context + y);
490
#if CONFIG_NEW_QUANT
491
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
492
493
  int dq =
      get_dq_profile_from_ctx(xd->qindex[seg_id], ctx, ref, pd->plane_type);
494
495
#endif  //  CONFIG_NEW_QUANT

496
#if CONFIG_AOM_QM
497
498
499
500
501
502
  const int eob = decode_coefs(
      xd, pd->plane_type, pd->dqcoeff, tx_size, tx_type, dequant,
#if CONFIG_NEW_QUANT
      pd->seg_dequant_nuq[seg_id][dq],
#endif  // CONFIG_NEW_QUANT
      ctx, sc->scan, sc->neighbors, max_scan_line, r, pd->seg_iqmatrix[seg_id]);
503
#else
504
505
  const int eob =
      decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size, tx_type, dequant,
506
#if CONFIG_NEW_QUANT
507
                   pd->seg_dequant_nuq[seg_id][dq],
508
#endif  // CONFIG_NEW_QUANT
509
                   ctx, sc->scan, sc->neighbors, max_scan_line, r);
510
#endif  // CONFIG_AOM_QM
511
  av1_set_contexts(xd, pd, plane, tx_size, eob > 0, x, y);
Jingning Han's avatar
Jingning Han committed
512
513
  return eob;
}
514
#endif