detokenize.c 17.8 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
 */

Yaowu Xu's avatar
Yaowu Xu committed
12
#include "aom_mem/aom_mem.h"
13
#include "aom_ports/mem.h"
Jingning Han's avatar
Jingning Han committed
14

Alex Converse's avatar
Alex Converse committed
15
16
17
#if CONFIG_ANS
#include "aom_dsp/ans.h"
#endif  // CONFIG_ANS
18
19
20
21
#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
22

23
#include "av1/decoder/detokenize.h"
Jingning Han's avatar
Jingning Han committed
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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
40
41
  } while (0)

Alex Converse's avatar
Alex Converse committed
42
#if !CONFIG_ANS
Yaowu Xu's avatar
Yaowu Xu committed
43
static INLINE int read_coeff(const aom_prob *probs, int n, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
44
  int i, val = 0;
Yaowu Xu's avatar
Yaowu Xu committed
45
  for (i = 0; i < n; ++i) val = (val << 1) | aom_read(r, probs[i]);
Jingning Han's avatar
Jingning Han committed
46
47
48
  return val;
}

49
50
51
52
#if CONFIG_AOM_QM
static int decode_coefs(const MACROBLOCKD *xd, PLANE_TYPE type,
                        tran_low_t *dqcoeff, TX_SIZE tx_size, TX_TYPE tx_type,
                        const int16_t *dq, int ctx, const int16_t *scan,
Yaowu Xu's avatar
Yaowu Xu committed
53
                        const int16_t *nb, aom_reader *r,
54
55
                        const qm_val_t *iqm[2][TX_SIZES])
#else
56
static int decode_coefs(const MACROBLOCKD *xd, PLANE_TYPE type,
Angie Chiang's avatar
Angie Chiang committed
57
58
                        tran_low_t *dqcoeff, TX_SIZE tx_size, TX_TYPE tx_type,
                        const int16_t *dq,
59
60
61
#if CONFIG_NEW_QUANT
                        dequant_val_type_nuq *dq_val,
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
62
                        int ctx, const int16_t *scan, const int16_t *nb,
Yaowu Xu's avatar
Yaowu Xu committed
63
                        aom_reader *r)
64
65
#endif
{
Jingning Han's avatar
Jingning Han committed
66
  FRAME_COUNTS *counts = xd->counts;
67
  const int max_eob = get_tx2d_size(tx_size);
Jingning Han's avatar
Jingning Han committed
68
69
  const FRAME_CONTEXT *const fc = xd->fc;
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
70
71
72
#if CONFIG_AOM_QM
  const qm_val_t *iqmatrix = iqm[!ref][tx_size];
#endif
Jingning Han's avatar
Jingning Han committed
73
  int band, c = 0;
74
  const int tx_size_ctx = txsize_sqr_map[tx_size];
Yaowu Xu's avatar
Yaowu Xu committed
75
  const aom_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
76
      fc->coef_probs[tx_size_ctx][type][ref];
Yaowu Xu's avatar
Yaowu Xu committed
77
  const aom_prob *prob;
78
79
  unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
  unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
80
  uint8_t token_cache[MAX_TX_SQUARE];
Jingning Han's avatar
Jingning Han committed
81
  const uint8_t *band_translate = get_band_translate(tx_size);
82
  int dq_shift;
Jingning Han's avatar
Jingning Han committed
83
84
  int v, token;
  int16_t dqv = dq[0];
85
86
87
#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
88
89
90
91
92
93
94
95
  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;

  if (counts) {
96
97
    coef_counts = counts->coef[tx_size_ctx][type][ref];
    eob_branch_count = counts->eob_branch[tx_size_ctx][type][ref];
Jingning Han's avatar
Jingning Han committed
98
99
  }

Yaowu Xu's avatar
Yaowu Xu committed
100
101
102
103
104
105
106
107
108
#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
109
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
110
111
112
113
114
115
      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
116
117
    }
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
118
119
120
121
122
123
    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
124
125
  }
#else
Yaowu Xu's avatar
Yaowu Xu committed
126
127
128
129
130
131
  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
132
133
#endif

Angie Chiang's avatar
Angie Chiang committed
134
  dq_shift = get_tx_scale(xd, tx_type, tx_size);
135

Jingning Han's avatar
Jingning Han committed
136
137
138
139
  while (c < max_eob) {
    int val = -1;
    band = *band_translate++;
    prob = coef_probs[band][ctx];
140
    if (counts) ++eob_branch_count[band][ctx];
Yaowu Xu's avatar
Yaowu Xu committed
141
    if (!aom_read(r, prob[EOB_CONTEXT_NODE])) {
Jingning Han's avatar
Jingning Han committed
142
143
144
145
      INCREMENT_COUNT(EOB_MODEL_TOKEN);
      break;
    }

146
147
148
149
#if CONFIG_NEW_QUANT
    dqv_val = &dq_val[band][0];
#endif  // CONFIG_NEW_QUANT

Yaowu Xu's avatar
Yaowu Xu committed
150
    while (!aom_read(r, prob[ZERO_CONTEXT_NODE])) {
Jingning Han's avatar
Jingning Han committed
151
152
153
154
      INCREMENT_COUNT(ZERO_TOKEN);
      dqv = dq[1];
      token_cache[scan[c]] = 0;
      ++c;
155
      if (c >= max_eob) return c;  // zero tokens at the end (no eob token)
Jingning Han's avatar
Jingning Han committed
156
157
158
      ctx = get_coef_context(nb, token_cache, c);
      band = *band_translate++;
      prob = coef_probs[band][ctx];
159
160
161
#if CONFIG_NEW_QUANT
      dqv_val = &dq_val[band][0];
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
162
163
    }

Yaowu Xu's avatar
Yaowu Xu committed
164
    if (!aom_read(r, prob[ONE_CONTEXT_NODE])) {
Jingning Han's avatar
Jingning Han committed
165
166
167
168
169
      INCREMENT_COUNT(ONE_TOKEN);
      token = ONE_TOKEN;
      val = 1;
    } else {
      INCREMENT_COUNT(TWO_TOKEN);
Yaowu Xu's avatar
Yaowu Xu committed
170
171
      token = aom_read_tree(r, av1_coef_con_tree,
                            av1_pareto8_full[prob[PIVOT_NODE] - 1]);
Jingning Han's avatar
Jingning Han committed
172
173
174
      switch (token) {
        case TWO_TOKEN:
        case THREE_TOKEN:
175
        case FOUR_TOKEN: val = token; break;
Jingning Han's avatar
Jingning Han committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
        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;
191
        case CATEGORY6_TOKEN: {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
192
          const int skip_bits = TX_SIZES - 1 - txsize_sqr_up_map[tx_size];
193
          const uint8_t *cat6p = cat6_prob + skip_bits;
Yaowu Xu's avatar
Yaowu Xu committed
194
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
195
          switch (xd->bd) {
Yaowu Xu's avatar
Yaowu Xu committed
196
            case AOM_BITS_8:
197
              val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
Jingning Han's avatar
Jingning Han committed
198
              break;
Yaowu Xu's avatar
Yaowu Xu committed
199
            case AOM_BITS_10:
200
              val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, r);
Jingning Han's avatar
Jingning Han committed
201
              break;
Yaowu Xu's avatar
Yaowu Xu committed
202
            case AOM_BITS_12:
203
              val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, r);
Jingning Han's avatar
Jingning Han committed
204
              break;
205
            default: assert(0); return -1;
Jingning Han's avatar
Jingning Han committed
206
207
          }
#else
208
          val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, r);
Jingning Han's avatar
Jingning Han committed
209
210
#endif
          break;
211
        }
Jingning Han's avatar
Jingning Han committed
212
213
      }
    }
214
#if CONFIG_NEW_QUANT
215

Yaowu Xu's avatar
Yaowu Xu committed
216
    v = av1_dequant_abscoeff_nuq(val, dqv, dqv_val);
217
218
    v = dq_shift ? ROUND_POWER_OF_TWO(v, dq_shift) : v;
#else
219
220
221
222
#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
223
    v = (val * dqv) >> dq_shift;
224
225
#endif  // CONFIG_NEW_QUANT

Jingning Han's avatar
Jingning Han committed
226
#if CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xu's avatar
Yaowu Xu committed
227
228
#if CONFIG_AOM_HIGHBITDEPTH
    dqcoeff[scan[c]] = highbd_check_range((aom_read_bit(r) ? -v : v), xd->bd);
Jingning Han's avatar
Jingning Han committed
229
#else
Yaowu Xu's avatar
Yaowu Xu committed
230
231
    dqcoeff[scan[c]] = check_range(aom_read_bit(r) ? -v : v);
#endif  // CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
232
#else
Yaowu Xu's avatar
Yaowu Xu committed
233
    dqcoeff[scan[c]] = aom_read_bit(r) ? -v : v;
Jingning Han's avatar
Jingning Han committed
234
#endif  // CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xu's avatar
Yaowu Xu committed
235
    token_cache[scan[c]] = av1_pt_energy_class[token];
Jingning Han's avatar
Jingning Han committed
236
237
238
239
240
241
242
    ++c;
    ctx = get_coef_context(nb, token_cache, c);
    dqv = dq[1];
  }

  return c;
}
Alex Converse's avatar
Alex Converse committed
243
#else  // !CONFIG_ANS
Yaowu Xu's avatar
Yaowu Xu committed
244
static INLINE int read_coeff(const aom_prob *const probs, int n,
Alex Converse's avatar
Alex Converse committed
245
246
                             struct AnsDecoder *const ans) {
  int i, val = 0;
247
  for (i = 0; i < n; ++i) val = (val << 1) | uabs_read(ans, probs[i]);
Alex Converse's avatar
Alex Converse committed
248
249
250
  return val;
}

251
static int decode_coefs_ans(const MACROBLOCKD *const xd, PLANE_TYPE type,
Alex Converse's avatar
Alex Converse committed
252
                            tran_low_t *dqcoeff, TX_SIZE tx_size,
253
                            TX_TYPE tx_type, const int16_t *dq,
254
255
256
#if CONFIG_NEW_QUANT
                            dequant_val_type_nuq *dq_val,
#endif  // CONFIG_NEW_QUANT
Alex Converse's avatar
Alex Converse committed
257
258
259
                            int ctx, const int16_t *scan, const int16_t *nb,
                            struct AnsDecoder *const ans) {
  FRAME_COUNTS *counts = xd->counts;
260
  const int max_eob = get_tx2d_size(tx_size);
Alex Converse's avatar
Alex Converse committed
261
262
263
  const FRAME_CONTEXT *const fc = xd->fc;
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
  int band, c = 0;
264
  int skip_eob = 0;
265
  const int tx_size_ctx = txsize_sqr_map[tx_size];
Yaowu Xu's avatar
Yaowu Xu committed
266
  const aom_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
267
      fc->coef_probs[tx_size_ctx][type][ref];
268
  const rans_lut(*coef_cdfs)[COEFF_CONTEXTS] =
269
      fc->coef_cdfs[tx_size_ctx][type][ref];
Yaowu Xu's avatar
Yaowu Xu committed
270
  const aom_prob *prob;
271
  const rans_lut *cdf;
272
273
  unsigned int(*coef_counts)[COEFF_CONTEXTS][UNCONSTRAINED_NODES + 1];
  unsigned int(*eob_branch_count)[COEFF_CONTEXTS];
274
  uint8_t token_cache[MAX_TX_SQUARE];
Alex Converse's avatar
Alex Converse committed
275
  const uint8_t *band_translate = get_band_translate(tx_size);
276
  int dq_shift;
Alex Converse's avatar
Alex Converse committed
277
278
  int v, token;
  int16_t dqv = dq[0];
279
280
281
#if CONFIG_NEW_QUANT
  const tran_low_t *dqv_val = &dq_val[0][0];
#endif  // CONFIG_NEW_QUANT
Alex Converse's avatar
Alex Converse committed
282
283
284
285
286
287
288
  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;

Angie Chiang's avatar
Angie Chiang committed
289
  dq_shift = get_tx_scale(xd, tx_type, tx_size);
290

Alex Converse's avatar
Alex Converse committed
291
  if (counts) {
292
293
    coef_counts = counts->coef[tx_size_ctx][type][ref];
    eob_branch_count = counts->eob_branch[tx_size_ctx][type][ref];
Alex Converse's avatar
Alex Converse committed
294
295
  }

Yaowu Xu's avatar
Yaowu Xu committed
296
297
298
299
300
301
302
303
304
#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;
Alex Converse's avatar
Alex Converse committed
305
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
306
307
308
309
310
311
      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;
Alex Converse's avatar
Alex Converse committed
312
313
    }
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
314
315
316
317
318
319
    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;
Alex Converse's avatar
Alex Converse committed
320
321
  }
#else
Yaowu Xu's avatar
Yaowu Xu committed
322
323
324
325
326
327
  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;
Alex Converse's avatar
Alex Converse committed
328
329
330
331
332
333
#endif

  while (c < max_eob) {
    int val = -1;
    band = *band_translate++;
    prob = coef_probs[band][ctx];
334
    if (!skip_eob) {
335
      if (counts) ++eob_branch_count[band][ctx];
336
337
338
339
      if (!uabs_read(ans, prob[EOB_CONTEXT_NODE])) {
        INCREMENT_COUNT(EOB_MODEL_TOKEN);
        break;
      }
Alex Converse's avatar
Alex Converse committed
340
341
    }

342
343
344
345
#if CONFIG_NEW_QUANT
    dqv_val = &dq_val[band][0];
#endif  // CONFIG_NEW_QUANT

346
347
348
    cdf = &coef_cdfs[band][ctx];
    token = ZERO_TOKEN + rans_read(ans, *cdf);
    if (token == ZERO_TOKEN) {
Alex Converse's avatar
Alex Converse committed
349
350
      INCREMENT_COUNT(ZERO_TOKEN);
      token_cache[scan[c]] = 0;
351
352
353
354
355
356
357
      skip_eob = 1;
    } else {
      INCREMENT_COUNT(ONE_TOKEN + (token > ONE_TOKEN));
      switch (token) {
        case ONE_TOKEN:
        case TWO_TOKEN:
        case THREE_TOKEN:
358
        case FOUR_TOKEN: val = token; break;
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
        case CATEGORY1_TOKEN:
          val = CAT1_MIN_VAL + read_coeff(cat1_prob, 1, ans);
          break;
        case CATEGORY2_TOKEN:
          val = CAT2_MIN_VAL + read_coeff(cat2_prob, 2, ans);
          break;
        case CATEGORY3_TOKEN:
          val = CAT3_MIN_VAL + read_coeff(cat3_prob, 3, ans);
          break;
        case CATEGORY4_TOKEN:
          val = CAT4_MIN_VAL + read_coeff(cat4_prob, 4, ans);
          break;
        case CATEGORY5_TOKEN:
          val = CAT5_MIN_VAL + read_coeff(cat5_prob, 5, ans);
          break;
        case CATEGORY6_TOKEN: {
Debargha Mukherjee's avatar
Debargha Mukherjee committed
375
          const int skip_bits = TX_SIZES - 1 - txsize_sqr_up_map[tx_size];
Alex Converse's avatar
Alex Converse committed
376
          const uint8_t *cat6p = cat6_prob + skip_bits;
Yaowu Xu's avatar
Yaowu Xu committed
377
#if CONFIG_AOM_HIGHBITDEPTH
378
          switch (xd->bd) {
Yaowu Xu's avatar
Yaowu Xu committed
379
            case AOM_BITS_8:
380
381
              val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, ans);
              break;
Yaowu Xu's avatar
Yaowu Xu committed
382
            case AOM_BITS_10:
383
384
              val = CAT6_MIN_VAL + read_coeff(cat6p, 16 - skip_bits, ans);
              break;
Yaowu Xu's avatar
Yaowu Xu committed
385
            case AOM_BITS_12:
386
387
              val = CAT6_MIN_VAL + read_coeff(cat6p, 18 - skip_bits, ans);
              break;
388
            default: assert(0); return -1;
389
          }
Alex Converse's avatar
Alex Converse committed
390
#else
391
          val = CAT6_MIN_VAL + read_coeff(cat6p, 14 - skip_bits, ans);
Alex Converse's avatar
Alex Converse committed
392
#endif
393
394
        } break;
      }
395
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
396
      v = av1_dequant_abscoeff_nuq(val, dqv, dqv_val);
397
      v = dq_shift ? ROUND_POWER_OF_TWO(v, dq_shift) : v;
398
#else
399
      v = (val * dqv) >> dq_shift;
400
401
#endif  // CONFIG_NEW_QUANT

Alex Converse's avatar
Alex Converse committed
402
#if CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xu's avatar
Yaowu Xu committed
403
#if CONFIG_AOM_HIGHBITDEPTH
404
405
      dqcoeff[scan[c]] =
          highbd_check_range((uabs_read_bit(ans) ? -v : v), xd->bd);
Alex Converse's avatar
Alex Converse committed
406
#else
407
      dqcoeff[scan[c]] = check_range(uabs_read_bit(ans) ? -v : v);
Yaowu Xu's avatar
Yaowu Xu committed
408
#endif  // CONFIG_AOM_HIGHBITDEPTH
Alex Converse's avatar
Alex Converse committed
409
#else
410
      dqcoeff[scan[c]] = uabs_read_bit(ans) ? -v : v;
Alex Converse's avatar
Alex Converse committed
411
#endif  // CONFIG_COEFFICIENT_RANGE_CHECKING
Yaowu Xu's avatar
Yaowu Xu committed
412
      token_cache[scan[c]] = av1_pt_energy_class[token];
413
414
      skip_eob = 0;
    }
Alex Converse's avatar
Alex Converse committed
415
416
417
418
419
420
421
422
    ++c;
    ctx = get_coef_context(nb, token_cache, c);
    dqv = dq[1];
  }

  return c;
}
#endif  // !CONFIG_ANS
Jingning Han's avatar
Jingning Han committed
423

Yaowu Xu's avatar
Yaowu Xu committed
424
425
// TODO(slavarnway): Decode version of av1_set_context.  Modify
// av1_set_context
Jingning Han's avatar
Jingning Han committed
426
// after testing is complete, then delete this version.
427
428
429
static void dec_set_contexts(const MACROBLOCKD *xd,
                             struct macroblockd_plane *pd, TX_SIZE tx_size,
                             int has_eob, int aoff, int loff) {
Jingning Han's avatar
Jingning Han committed
430
431
  ENTROPY_CONTEXT *const a = pd->above_context + aoff;
  ENTROPY_CONTEXT *const l = pd->left_context + loff;
432
433
  const int tx_w_in_blocks = num_4x4_blocks_wide_txsize_lookup[tx_size];
  const int tx_h_in_blocks = num_4x4_blocks_high_txsize_lookup[tx_size];
Jingning Han's avatar
Jingning Han committed
434
435
436
437

  // above
  if (has_eob && xd->mb_to_right_edge < 0) {
    int i;
438
439
    const int blocks_wide =
        pd->n4_w + (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
440
    int above_contexts = tx_w_in_blocks;
Jingning Han's avatar
Jingning Han committed
441
442
443
    if (above_contexts + aoff > blocks_wide)
      above_contexts = blocks_wide - aoff;

444
445
    for (i = 0; i < above_contexts; ++i) a[i] = has_eob;
    for (i = above_contexts; i < tx_w_in_blocks; ++i) a[i] = 0;
Jingning Han's avatar
Jingning Han committed
446
  } else {
447
    memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_w_in_blocks);
Jingning Han's avatar
Jingning Han committed
448
449
450
451
452
  }

  // left
  if (has_eob && xd->mb_to_bottom_edge < 0) {
    int i;
453
454
    const int blocks_high =
        pd->n4_h + (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
455
    int left_contexts = tx_h_in_blocks;
456
    if (left_contexts + loff > blocks_high) left_contexts = blocks_high - loff;
Jingning Han's avatar
Jingning Han committed
457

458
459
    for (i = 0; i < left_contexts; ++i) l[i] = has_eob;
    for (i = left_contexts; i < tx_h_in_blocks; ++i) l[i] = 0;
Jingning Han's avatar
Jingning Han committed
460
  } else {
461
    memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_h_in_blocks);
Jingning Han's avatar
Jingning Han committed
462
463
464
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
465
466
void av1_decode_palette_tokens(MACROBLOCKD *const xd, int plane,
                               aom_reader *r) {
hui su's avatar
hui su committed
467
468
469
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
470
  const int rows = (4 * num_4x4_blocks_high_lookup[bsize]) >>
471
                   (xd->plane[plane != 0].subsampling_y);
472
  const int cols = (4 * num_4x4_blocks_wide_lookup[bsize]) >>
473
                   (xd->plane[plane != 0].subsampling_x);
hui su's avatar
hui su committed
474
475
476
  int color_idx, color_ctx, color_order[PALETTE_MAX_SIZE];
  int n = mbmi->palette_mode_info.palette_size[plane != 0];
  int i, j;
477
  uint8_t *color_map = xd->plane[plane != 0].color_index_map;
clang-format's avatar
clang-format committed
478
  const aom_prob(*const prob)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
Yaowu Xu's avatar
Yaowu Xu committed
479
480
      plane ? av1_default_palette_uv_color_prob
            : av1_default_palette_y_color_prob;
hui su's avatar
hui su committed
481
482
483

  for (i = 0; i < rows; ++i) {
    for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
484
      color_ctx =
Yaowu Xu's avatar
Yaowu Xu committed
485
486
487
          av1_get_palette_color_context(color_map, cols, i, j, n, color_order);
      color_idx = aom_read_tree(r, av1_palette_color_tree[n - 2],
                                prob[n - 2][color_ctx]);
hui su's avatar
hui su committed
488
489
490
491
492
493
      assert(color_idx >= 0 && color_idx < n);
      color_map[i * cols + j] = color_order[color_idx];
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
494
495
496
int av1_decode_block_tokens(MACROBLOCKD *const xd, int plane,
                            const scan_order *sc, int x, int y, TX_SIZE tx_size,
                            TX_TYPE tx_type,
Alex Converse's avatar
Alex Converse committed
497
#if CONFIG_ANS
Yaowu Xu's avatar
Yaowu Xu committed
498
                            struct AnsDecoder *const r,
Alex Converse's avatar
Alex Converse committed
499
#else
Yaowu Xu's avatar
Yaowu Xu committed
500
                            aom_reader *r,
Alex Converse's avatar
Alex Converse committed
501
#endif  // CONFIG_ANS
Yaowu Xu's avatar
Yaowu Xu committed
502
                            int seg_id) {
Jingning Han's avatar
Jingning Han committed
503
504
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int16_t *const dequant = pd->seg_dequant[seg_id];
505
506
  const int ctx =
      get_entropy_context(tx_size, pd->above_context + x, pd->left_context + y);
507
#if CONFIG_NEW_QUANT
508
509
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
  int dq = get_dq_profile_from_ctx(ctx, ref, pd->plane_type);
510
511
#endif  //  CONFIG_NEW_QUANT

Alex Converse's avatar
Alex Converse committed
512
#if !CONFIG_ANS
513
514
515
516
517
#if CONFIG_AOM_QM
  const int eob =
      decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size, tx_type, dequant,
                   ctx, sc->scan, sc->neighbors, r, pd->seg_iqmatrix[seg_id]);
#else
518
519
  const int eob =
      decode_coefs(xd, pd->plane_type, pd->dqcoeff, tx_size, tx_type, dequant,
520
#if CONFIG_NEW_QUANT
521
                   pd->seg_dequant_nuq[seg_id][dq],
522
#endif  // CONFIG_NEW_QUANT
523
                   ctx, sc->scan, sc->neighbors, r);
524
#endif  // CONFIG_AOM_QM
Alex Converse's avatar
Alex Converse committed
525
#else
526
527
  const int eob = decode_coefs_ans(xd, pd->plane_type, pd->dqcoeff, tx_size,
                                   tx_type, dequant,
528
#if CONFIG_NEW_QUANT
529
                                   pd->seg_dequant_nuq[seg_id][dq],
530
531
#endif  // CONFIG_NEW_QUANT
                                   ctx, sc->scan, sc->neighbors, r);
Alex Converse's avatar
Alex Converse committed
532
#endif  // !CONFIG_ANS
Jingning Han's avatar
Jingning Han committed
533
  dec_set_contexts(xd, pd, tx_size, eob > 0, x, y);
534
  /*
Yaowu Xu's avatar
Yaowu Xu committed
535
  av1_set_contexts(xd, pd,
536
537
538
                    get_plane_block_size(xd->mi[0]->mbmi.sb_type, pd),
                    tx_size, eob > 0, x, y);
                    */
Jingning Han's avatar
Jingning Han committed
539
540
  return eob;
}