encodemb.c 55 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
13
14
#include "./av1_rtcd.h"
#include "./aom_config.h"
#include "./aom_dsp_rtcd.h"
Jingning Han's avatar
Jingning Han committed
15

16
#include "aom_dsp/bitwriter.h"
17
#include "aom_dsp/quantize.h"
Yaowu Xu's avatar
Yaowu Xu committed
18
#include "aom_mem/aom_mem.h"
19
#include "aom_ports/mem.h"
Jingning Han's avatar
Jingning Han committed
20

21
22
23
24
#include "av1/common/idct.h"
#include "av1/common/reconinter.h"
#include "av1/common/reconintra.h"
#include "av1/common/scan.h"
Jingning Han's avatar
Jingning Han committed
25

26
#include "av1/encoder/av1_quantize.h"
27
#include "av1/encoder/encodemb.h"
28
29
30
#if CONFIG_LV_MAP
#include "av1/encoder/encodetxb.h"
#endif
31
32
33
#include "av1/encoder/hybrid_fwd_txfm.h"
#include "av1/encoder/rd.h"
#include "av1/encoder/tokenize.h"
Jingning Han's avatar
Jingning Han committed
34

35
36
37
38
39
40
#if CONFIG_PVQ
#include "av1/encoder/encint.h"
#include "av1/common/partition.h"
#include "av1/encoder/pvq_encoder.h"
#endif

41
42
43
// Check if one needs to use c version subtraction.
static int check_subtract_block_size(int w, int h) { return w < 4 || h < 4; }

Angie Chiang's avatar
Angie Chiang committed
44
45
46
47
static void subtract_block(const MACROBLOCKD *xd, int rows, int cols,
                           int16_t *diff, ptrdiff_t diff_stride,
                           const uint8_t *src8, ptrdiff_t src_stride,
                           const uint8_t *pred8, ptrdiff_t pred_stride) {
48
#if !CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
49
50
  (void)xd;
#endif
Jingning Han's avatar
Jingning Han committed
51

Angie Chiang's avatar
Angie Chiang committed
52
  if (check_subtract_block_size(rows, cols)) {
53
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
54
55
56
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      aom_highbd_subtract_block_c(rows, cols, diff, diff_stride, src8,
                                  src_stride, pred8, pred_stride, xd->bd);
57
58
      return;
    }
59
#endif  // CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
60
61
    aom_subtract_block_c(rows, cols, diff, diff_stride, src8, src_stride, pred8,
                         pred_stride);
62
63
64
65

    return;
  }

66
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
67
68
69
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    aom_highbd_subtract_block(rows, cols, diff, diff_stride, src8, src_stride,
                              pred8, pred_stride, xd->bd);
Jingning Han's avatar
Jingning Han committed
70
71
    return;
  }
72
#endif  // CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
73
74
75
76
  aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
                     pred_stride);
}

Angie Chiang's avatar
Angie Chiang committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
void av1_subtract_txb(MACROBLOCK *x, int plane, BLOCK_SIZE plane_bsize,
                      int blk_col, int blk_row, TX_SIZE tx_size) {
  MACROBLOCKD *const xd = &x->e_mbd;
  struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
  const int diff_stride = block_size_wide[plane_bsize];
  const int src_stride = p->src.stride;
  const int dst_stride = pd->dst.stride;
  const int tx1d_width = tx_size_wide[tx_size];
  const int tx1d_height = tx_size_high[tx_size];
  uint8_t *dst =
      &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
  uint8_t *src =
      &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
  int16_t *src_diff =
      &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
  subtract_block(xd, tx1d_height, tx1d_width, src_diff, diff_stride, src,
                 src_stride, dst, dst_stride);
}

Angie Chiang's avatar
Angie Chiang committed
97
98
99
100
101
102
103
104
105
106
void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
  struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &x->e_mbd.plane[plane];
  const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
  const int bw = block_size_wide[plane_bsize];
  const int bh = block_size_high[plane_bsize];
  const MACROBLOCKD *xd = &x->e_mbd;

  subtract_block(xd, bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
                 pd->dst.buf, pd->dst.stride);
Jingning Han's avatar
Jingning Han committed
107
108
}

109
110
// These numbers are empirically obtained.
static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
Thomas Davies's avatar
Thomas Davies committed
111
112
113
#if CONFIG_EC_ADAPT
  { 10, 7 }, { 8, 5 },
#else
114
  { 10, 6 }, { 8, 5 },
Thomas Davies's avatar
Thomas Davies committed
115
#endif
116
};
Jingning Han's avatar
Jingning Han committed
117

118
119
120
121
122
#define UPDATE_RD_COST()                             \
  {                                                  \
    rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0); \
    rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1); \
  }
Jingning Han's avatar
Jingning Han committed
123

James Zern's avatar
James Zern committed
124
125
126
static INLINE int64_t
get_token_bit_costs(unsigned int token_costs[2][COEFF_CONTEXTS][ENTROPY_TOKENS],
                    int skip_eob, int ctx, int token) {
127
128
129
130
131
132
133
134
#if CONFIG_NEW_TOKENSET
  (void)skip_eob;
  return token_costs[token == ZERO_TOKEN || token == EOB_TOKEN][ctx][token];
#else
  return token_costs[skip_eob][ctx][token];
#endif
}

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#define USE_GREEDY_OPTIMIZE_B 0

#if USE_GREEDY_OPTIMIZE_B

typedef struct av1_token_state {
  int16_t token;
  tran_low_t qc;
  tran_low_t dqc;
} av1_token_state;

int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int block,
                   TX_SIZE tx_size, int ctx) {
#if !CONFIG_PVQ
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
  av1_token_state tokens[MAX_TX_SQUARE + 1][2];
  uint8_t token_cache[MAX_TX_SQUARE];
  const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  const int eob = p->eobs[block];
  const PLANE_TYPE plane_type = pd->plane_type;
  const int16_t *const dequant_ptr = pd->dequant;
  const uint8_t *const band_translate = get_band_translate(tx_size);
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
  const SCAN_ORDER *const scan_order =
      get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
  const int16_t *const scan = scan_order->scan;
  const int16_t *const nb = scan_order->neighbors;
  int dqv;
  const int shift = get_tx_scale(tx_size);
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
  const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!ref][tx_size];
#endif
#if CONFIG_NEW_QUANT
  int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
  const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
#elif !CONFIG_AOM_QM
  const int dq_step[2] = { dequant_ptr[0] >> shift, dequant_ptr[1] >> shift };
#endif  // CONFIG_NEW_QUANT
  int sz = 0;
  const int64_t rddiv = mb->rddiv;
  int64_t rd_cost0, rd_cost1;
  int16_t t0, t1;
  int i, final_eob;
#if CONFIG_HIGHBITDEPTH
  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
#else
  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, 8);
#endif
  unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
      mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref];
  const int default_eob = tx_size_2d[tx_size];

  assert((mb->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));

  assert((!plane_type && !plane) || (plane_type && plane));
  assert(eob <= default_eob);

  int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
/* CpuSpeedTest uses "--min-q=0 --max-q=0" and expects 100dB psnr
* This creates conflict with search for a better EOB position
* The line below is to make sure EOB search is disabled at this corner case.
*/
#if !CONFIG_NEW_QUANT && !CONFIG_AOM_QM
  if (dq_step[1] <= 4) {
    rdmult = 1;
  }
#endif

  int64_t rate0, rate1;
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
    int x = qcoeff[rc];
    t0 = av1_get_token(x);

    tokens[i][0].qc = x;
    tokens[i][0].token = t0;
    tokens[i][0].dqc = dqcoeff[rc];

    token_cache[rc] = av1_pt_energy_class[t0];
  }
  tokens[eob][0].token = EOB_TOKEN;
  tokens[eob][0].qc = 0;
  tokens[eob][0].dqc = 0;
  tokens[eob][1] = tokens[eob][0];

  unsigned int(*token_costs_ptr)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
      token_costs;

  final_eob = 0;

  int64_t eob_cost0, eob_cost1;

  const int ctx0 = ctx;
  /* Record the r-d cost */
  int64_t accu_rate = 0;
  int64_t accu_error = 0;

  rate0 = get_token_bit_costs(*(token_costs_ptr + band_translate[0]), 0, ctx0,
                              EOB_TOKEN);
  int64_t best_block_rd_cost = RDCOST(rdmult, rddiv, rate0, accu_error);

  // int64_t best_block_rd_cost_all0 = best_block_rd_cost;

  int x_prev = 1;

  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
    int x = qcoeff[rc];
    sz = -(x < 0);

    int band_cur = band_translate[i];
    int ctx_cur = (i == 0) ? ctx : get_coef_context(nb, token_cache, i);
    int token_tree_sel_cur = (x_prev == 0);

    if (x == 0) {
      // no need to search when x == 0
      rate0 =
          get_token_bit_costs(*(token_costs_ptr + band_cur), token_tree_sel_cur,
                              ctx_cur, tokens[i][0].token);
      accu_rate += rate0;
      x_prev = 0;
      // accu_error does not change when x==0
    } else {
      /*  Computing distortion
       */
      // compute the distortion for the first candidate
      // and the distortion for quantizing to 0.
      int dx0 = (-coeff[rc]) * (1 << shift);
#if CONFIG_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx0 >>= xd->bd - 8;
      }
#endif
      int64_t d0 = (int64_t)dx0 * dx0;

      int x_a = x - 2 * sz - 1;
      int64_t d2, d2_a;

      int dx;

#if CONFIG_AOM_QM
      int iwt = iqmatrix[rc];
      dqv = dequant_ptr[rc != 0];
      dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
#else
      dqv = dequant_ptr[rc != 0];
#endif

      dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
#if CONFIG_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx >>= xd->bd - 8;
      }
#endif  // CONFIG_HIGHBITDEPTH
      d2 = (int64_t)dx * dx;

      /* compute the distortion for the second candidate
       * x_a = x - 2 * sz + 1;
       */
      if (x_a != 0) {
#if CONFIG_NEW_QUANT
        dx = av1_dequant_coeff_nuq(x, dqv, dequant_val[band_translate[i]]) -
             (coeff[rc] << shift);
#if CONFIG_HIGHBITDEPTH
        if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
          dx >>= xd->bd - 8;
        }
#endif  // CONFIG_HIGHBITDEPTH
#else   // CONFIG_NEW_QUANT
#if CONFIG_HIGHBITDEPTH
        if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
          dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
        } else {
          dx -= (dqv + sz) ^ sz;
        }
#else
        dx -= (dqv + sz) ^ sz;
#endif  // CONFIG_HIGHBITDEPTH
#endif  // CONFIG_NEW_QUANT
        d2_a = (int64_t)dx * dx;
      } else {
        d2_a = d0;
      }
      /*  Computing rates and r-d cost
       */

      int best_x, best_eob_x;
      int64_t base_bits, next_bits0, next_bits1;
      int64_t next_eob_bits0, next_eob_bits1;

      // rate cost of x
      base_bits = av1_get_token_cost(x, &t0, cat6_bits);
      rate0 = base_bits + get_token_bit_costs(*(token_costs_ptr + band_cur),
                                              token_tree_sel_cur, ctx_cur, t0);

      base_bits = av1_get_token_cost(x_a, &t1, cat6_bits);
      rate1 = base_bits + get_token_bit_costs(*(token_costs_ptr + band_cur),
                                              token_tree_sel_cur, ctx_cur, t1);

      next_bits0 = 0;
      next_bits1 = 0;
      next_eob_bits0 = 0;
      next_eob_bits1 = 0;

      if (i < default_eob - 1) {
        int ctx_next, token_tree_sel_next;
        int band_next = band_translate[i + 1];

        token_cache[rc] = av1_pt_energy_class[t0];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
        token_tree_sel_next = (x == 0);

        next_bits0 = get_token_bit_costs(*(token_costs_ptr + band_next),
                                         token_tree_sel_next, ctx_next,
                                         tokens[i + 1][0].token);
        next_eob_bits0 =
            get_token_bit_costs(*(token_costs_ptr + band_next),
                                token_tree_sel_next, ctx_next, EOB_TOKEN);

        token_cache[rc] = av1_pt_energy_class[t1];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
        token_tree_sel_next = (x_a == 0);

        next_bits1 = get_token_bit_costs(*(token_costs_ptr + band_next),
                                         token_tree_sel_next, ctx_next,
                                         tokens[i + 1][0].token);

        if (x_a != 0) {
          next_eob_bits1 =
              get_token_bit_costs(*(token_costs_ptr + band_next),
                                  token_tree_sel_next, ctx_next, EOB_TOKEN);
        }
      }

      rd_cost0 = RDCOST(rdmult, rddiv, (rate0 + next_bits0), d2);
      rd_cost1 = RDCOST(rdmult, rddiv, (rate1 + next_bits1), d2_a);

      best_x = (rd_cost1 < rd_cost0);

      eob_cost0 = RDCOST(rdmult, rddiv, (accu_rate + rate0 + next_eob_bits0),
                         (accu_error + d2 - d0));
      eob_cost1 = eob_cost0;
      if (x_a != 0) {
        eob_cost1 = RDCOST(rdmult, rddiv, (accu_rate + rate1 + next_eob_bits1),
                           (accu_error + d2_a - d0));
        best_eob_x = (eob_cost1 < eob_cost0);
      } else {
        best_eob_x = 0;
      }

      int dqc, dqc_a = 0;

      dqc = dqcoeff[rc];
      if (best_x + best_eob_x) {
        if (x_a != 0) {
#if CONFIG_NEW_QUANT
          dqc_a = av1_dequant_abscoeff_nuq(abs(x_a), dqv,
                                           dequant_val[band_translate[i]]);
          dqc_a = shift ? ROUND_POWER_OF_TWO(dqc_a, shift) : dqc_a;
          if (sz) dqc_a = -dqc_a;
#else
// The 32x32 transform coefficient uses half quantization step size.
// Account for the rounding difference in the dequantized coefficeint
// value when the quantization index is dropped from an even number
// to an odd number.

#if CONFIG_AOM_QM
          tran_low_t offset = dqv >> shift;
#else
          tran_low_t offset = dq_step[rc != 0];
#endif
          if (shift & x_a) offset += (dqv & 0x01);

          if (sz == 0)
            dqc_a = dqcoeff[rc] - offset;
          else
            dqc_a = dqcoeff[rc] + offset;
#endif  // CONFIG_NEW_QUANT
        } else {
          dqc_a = 0;
        }  // if (x_a != 0)
      }

      // record the better quantized value
      if (best_x) {
        qcoeff[rc] = x_a;
        dqcoeff[rc] = dqc_a;

        accu_rate += rate1;
        accu_error += d2_a - d0;
        assert(d2_a <= d0);

        token_cache[rc] = av1_pt_energy_class[t1];
      } else {
        accu_rate += rate0;
        accu_error += d2 - d0;
        assert(d2 <= d0);

        token_cache[rc] = av1_pt_energy_class[t0];
      }

      x_prev = qcoeff[rc];

      // determine whether to move the eob position to i+1
      int64_t best_eob_cost_i = eob_cost0;

      tokens[i][1].token = t0;
      tokens[i][1].qc = x;
      tokens[i][1].dqc = dqc;

      if ((x_a != 0) && (best_eob_x)) {
        best_eob_cost_i = eob_cost1;

        tokens[i][1].token = t1;
        tokens[i][1].qc = x_a;
        tokens[i][1].dqc = dqc_a;
      }

      if (best_eob_cost_i < best_block_rd_cost) {
        best_block_rd_cost = best_eob_cost_i;
        final_eob = i + 1;
      }
    }  // if (x==0)
  }    // for (i)

  assert(final_eob <= eob);
  if (final_eob > 0) {
    assert(tokens[final_eob - 1][1].qc != 0);
    i = final_eob - 1;
    int rc = scan[i];
    qcoeff[rc] = tokens[i][1].qc;
    dqcoeff[rc] = tokens[i][1].dqc;
  }

  for (i = final_eob; i < eob; i++) {
    int rc = scan[i];
    qcoeff[rc] = 0;
    dqcoeff[rc] = 0;
  }

  mb->plane[plane].eobs[block] = final_eob;
  return final_eob;

#else   // !CONFIG_PVQ
  (void)cm;
  (void)tx_size;
  (void)ctx;
  struct macroblock_plane *const p = &mb->plane[plane];
  return p->eobs[block];
#endif  // !CONFIG_PVQ
}

#else  // USE_GREEDY_OPTIMIZE_B

typedef struct av1_token_state {
  int64_t error;
  int rate;
  int16_t next;
  int16_t token;
  tran_low_t qc;
  tran_low_t dqc;
  uint8_t best_index;
} av1_token_state;

Angie Chiang's avatar
Angie Chiang committed
504
505
int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int block,
                   TX_SIZE tx_size, int ctx) {
506
#if !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
507
508
509
510
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
Yaowu Xu's avatar
Yaowu Xu committed
511
  av1_token_state tokens[MAX_TX_SQUARE + 1][2];
512
  uint8_t token_cache[MAX_TX_SQUARE];
513
  const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
Jingning Han's avatar
Jingning Han committed
514
515
516
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  const int eob = p->eobs[block];
517
  const PLANE_TYPE plane_type = pd->plane_type;
518
  const int default_eob = tx_size_2d[tx_size];
519
520
  const int16_t *const dequant_ptr = pd->dequant;
  const uint8_t *const band_translate = get_band_translate(tx_size);
521
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
522
  const SCAN_ORDER *const scan_order =
Angie Chiang's avatar
Angie Chiang committed
523
      get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
524
525
  const int16_t *const scan = scan_order->scan;
  const int16_t *const nb = scan_order->neighbors;
Thomas Davies's avatar
Thomas Davies committed
526
  int dqv;
527
  const int shift = get_tx_scale(tx_size);
528
529
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
530
  const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!ref][tx_size];
531
#endif
532
#if CONFIG_NEW_QUANT
533
  int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
534
  const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
535
#elif !CONFIG_AOM_QM
536
  const int dq_step[2] = { dequant_ptr[0] >> shift, dequant_ptr[1] >> shift };
537
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
538
  int next = eob, sz = 0;
539
  const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
hui su's avatar
hui su committed
540
  const int64_t rddiv = mb->rddiv;
Jingning Han's avatar
Jingning Han committed
541
  int64_t rd_cost0, rd_cost1;
542
543
  int rate0, rate1;
  int64_t error0, error1;
Jingning Han's avatar
Jingning Han committed
544
  int16_t t0, t1;
545
546
  int best, band = (eob < default_eob) ? band_translate[eob]
                                       : band_translate[eob - 1];
547
  int pt, i, final_eob;
548
#if CONFIG_HIGHBITDEPTH
549
  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
Jingning Han's avatar
Jingning Han committed
550
#else
551
  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, 8);
Jingning Han's avatar
Jingning Han committed
552
#endif
553
  unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
554
      mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref];
555
556
  const uint16_t *band_counts = &band_count_table[tx_size][band];
  uint16_t band_left = eob - band_cum_count_table[tx_size][band] + 1;
557
558
  int shortcut = 0;
  int next_shortcut = 0;
559

Fangwen Fu's avatar
Fangwen Fu committed
560
561
562
563
564
565
566
567
568
#if CONFIG_EXT_DELTA_Q
  const int qindex = cm->seg.enabled
                         ? av1_get_qindex(&cm->seg, xd->mi[0]->mbmi.segment_id,
                                          cm->base_qindex)
                         : cm->base_qindex;
  if (qindex == 0) {
    assert((qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
  }
#else
569
  assert((mb->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
Fangwen Fu's avatar
Fangwen Fu committed
570
#endif
571

572
  token_costs += band;
Jingning Han's avatar
Jingning Han committed
573

574
  assert((!plane_type && !plane) || (plane_type && plane));
Jingning Han's avatar
Jingning Han committed
575
  assert(eob <= default_eob);
576

Jingning Han's avatar
Jingning Han committed
577
578
579
580
581
582
583
584
585
  /* Now set up a Viterbi trellis to evaluate alternative roundings. */
  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
  tokens[eob][0].next = default_eob;
  tokens[eob][0].token = EOB_TOKEN;
  tokens[eob][0].qc = 0;
  tokens[eob][1] = tokens[eob][0];

586
587
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
588
    tokens[i][0].rate = av1_get_token_cost(qcoeff[rc], &t0, cat6_bits);
589
    tokens[i][0].token = t0;
Yaowu Xu's avatar
Yaowu Xu committed
590
    token_cache[rc] = av1_pt_energy_class[t0];
591
  }
Jingning Han's avatar
Jingning Han committed
592
593

  for (i = eob; i-- > 0;) {
594
595
    int base_bits, dx;
    int64_t d2;
Jingning Han's avatar
Jingning Han committed
596
    const int rc = scan[i];
Thomas Davies's avatar
Thomas Davies committed
597
    int x = qcoeff[rc];
598
599
#if CONFIG_AOM_QM
    int iwt = iqmatrix[rc];
Thomas Davies's avatar
Thomas Davies committed
600
601
602
603
    dqv = dequant_ptr[rc != 0];
    dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
#else
    dqv = dequant_ptr[rc != 0];
604
#endif
605
    next_shortcut = shortcut;
606

Jingning Han's avatar
Jingning Han committed
607
    /* Only add a trellis state for non-zero coefficients. */
608
    if (UNLIKELY(x)) {
Jingning Han's avatar
Jingning Han committed
609
610
611
612
613
      error0 = tokens[next][0].error;
      error1 = tokens[next][1].error;
      /* Evaluate the first possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;
614

615
616
617
618
      if (next_shortcut) {
        /* Consider both possible successor states. */
        if (next < default_eob) {
          pt = get_coef_context(nb, token_cache, i + 1);
619
620
621
622
          rate0 +=
              get_token_bit_costs(*token_costs, 0, pt, tokens[next][0].token);
          rate1 +=
              get_token_bit_costs(*token_costs, 0, pt, tokens[next][1].token);
623
624
625
626
627
628
629
        }
        UPDATE_RD_COST();
        /* And pick the best. */
        best = rd_cost1 < rd_cost0;
      } else {
        if (next < default_eob) {
          pt = get_coef_context(nb, token_cache, i + 1);
630
631
          rate0 +=
              get_token_bit_costs(*token_costs, 0, pt, tokens[next][0].token);
632
633
        }
        best = 0;
Jingning Han's avatar
Jingning Han committed
634
      }
635
636

      dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
637
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
638
639
640
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx >>= xd->bd - 8;
      }
641
#endif  // CONFIG_HIGHBITDEPTH
642
      d2 = (int64_t)dx * dx;
643
      tokens[i][0].rate += (best ? rate1 : rate0);
Jingning Han's avatar
Jingning Han committed
644
645
646
      tokens[i][0].error = d2 + (best ? error1 : error0);
      tokens[i][0].next = next;
      tokens[i][0].qc = x;
647
      tokens[i][0].dqc = dqcoeff[rc];
648
      tokens[i][0].best_index = best;
Jingning Han's avatar
Jingning Han committed
649
650
651
652
653

      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

654
      // The threshold of 3 is empirically obtained.
655
      if (UNLIKELY(abs(x) > 3)) {
656
657
        shortcut = 0;
      } else {
658
#if CONFIG_NEW_QUANT
Thomas Davies's avatar
Thomas Davies committed
659
        shortcut = ((av1_dequant_abscoeff_nuq(abs(x), dqv,
Yaowu Xu's avatar
Yaowu Xu committed
660
                                              dequant_val[band_translate[i]]) >
661
                     (abs(coeff[rc]) << shift)) &&
Thomas Davies's avatar
Thomas Davies committed
662
                    (av1_dequant_abscoeff_nuq(abs(x) - 1, dqv,
Yaowu Xu's avatar
Yaowu Xu committed
663
                                              dequant_val[band_translate[i]]) <
664
                     (abs(coeff[rc]) << shift)));
665
666
667
668
669
670
671
672
#else  // CONFIG_NEW_QUANT
#if CONFIG_AOM_QM
        if ((abs(x) * dequant_ptr[rc != 0] * iwt >
             ((abs(coeff[rc]) << shift) << AOM_QM_BITS)) &&
            (abs(x) * dequant_ptr[rc != 0] * iwt <
             (((abs(coeff[rc]) << shift) + dequant_ptr[rc != 0])
              << AOM_QM_BITS)))
#else
673
        if ((abs(x) * dequant_ptr[rc != 0] > (abs(coeff[rc]) << shift)) &&
674
675
            (abs(x) * dequant_ptr[rc != 0] <
             (abs(coeff[rc]) << shift) + dequant_ptr[rc != 0]))
676
#endif  // CONFIG_AOM_QM
677
678
679
          shortcut = 1;
        else
          shortcut = 0;
680
#endif  // CONFIG_NEW_QUANT
681
      }
Jingning Han's avatar
Jingning Han committed
682
683
684
685

      if (shortcut) {
        sz = -(x < 0);
        x -= 2 * sz + 1;
686
687
688
      } else {
        tokens[i][1] = tokens[i][0];
        next = i;
689

690
        if (UNLIKELY(!(--band_left))) {
691
692
693
694
          --band_counts;
          band_left = *band_counts;
          --token_costs;
        }
695
        continue;
Jingning Han's avatar
Jingning Han committed
696
697
698
699
700
701
702
703
704
      }

      /* Consider both possible successor states. */
      if (!x) {
        /* If we reduced this coefficient to zero, check to see if
         *  we need to move the EOB back here.
         */
        t0 = tokens[next][0].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
        t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
705
        base_bits = 0;
Jingning Han's avatar
Jingning Han committed
706
      } else {
707
        base_bits = av1_get_token_cost(x, &t0, cat6_bits);
Jingning Han's avatar
Jingning Han committed
708
709
        t1 = t0;
      }
710
711

      if (next_shortcut) {
712
        if (LIKELY(next < default_eob)) {
713
          if (t0 != EOB_TOKEN) {
Yaowu Xu's avatar
Yaowu Xu committed
714
            token_cache[rc] = av1_pt_energy_class[t0];
715
            pt = get_coef_context(nb, token_cache, i + 1);
716
717
            rate0 += get_token_bit_costs(*token_costs, !x, pt,
                                         tokens[next][0].token);
718
719
          }
          if (t1 != EOB_TOKEN) {
Yaowu Xu's avatar
Yaowu Xu committed
720
            token_cache[rc] = av1_pt_energy_class[t1];
721
            pt = get_coef_context(nb, token_cache, i + 1);
722
723
            rate1 += get_token_bit_costs(*token_costs, !x, pt,
                                         tokens[next][1].token);
724
725
726
727
728
729
730
731
732
          }
        }

        UPDATE_RD_COST();
        /* And pick the best. */
        best = rd_cost1 < rd_cost0;
      } else {
        // The two states in next stage are identical.
        if (next < default_eob && t0 != EOB_TOKEN) {
Yaowu Xu's avatar
Yaowu Xu committed
733
          token_cache[rc] = av1_pt_energy_class[t0];
Jingning Han's avatar
Jingning Han committed
734
          pt = get_coef_context(nb, token_cache, i + 1);
735
736
          rate0 +=
              get_token_bit_costs(*token_costs, !x, pt, tokens[next][0].token);
Jingning Han's avatar
Jingning Han committed
737
        }
738
        best = 0;
Jingning Han's avatar
Jingning Han committed
739
740
      }

741
#if CONFIG_NEW_QUANT
Thomas Davies's avatar
Thomas Davies committed
742
      dx = av1_dequant_coeff_nuq(x, dqv, dequant_val[band_translate[i]]) -
743
           (coeff[rc] << shift);
744
#if CONFIG_HIGHBITDEPTH
hui su's avatar
hui su committed
745
746
747
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx >>= xd->bd - 8;
      }
748
#endif  // CONFIG_HIGHBITDEPTH
749
#else   // CONFIG_NEW_QUANT
750
#if CONFIG_HIGHBITDEPTH
hui su's avatar
hui su committed
751
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Thomas Davies's avatar
Thomas Davies committed
752
        dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
hui su's avatar
hui su committed
753
      } else {
Thomas Davies's avatar
Thomas Davies committed
754
        dx -= (dqv + sz) ^ sz;
hui su's avatar
hui su committed
755
756
      }
#else
Thomas Davies's avatar
Thomas Davies committed
757
      dx -= (dqv + sz) ^ sz;
758
#endif  // CONFIG_HIGHBITDEPTH
759
#endif  // CONFIG_NEW_QUANT
760
      d2 = (int64_t)dx * dx;
hui su's avatar
hui su committed
761

Jingning Han's avatar
Jingning Han committed
762
763
764
765
766
      tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
      tokens[i][1].error = d2 + (best ? error1 : error0);
      tokens[i][1].next = next;
      tokens[i][1].token = best ? t1 : t0;
      tokens[i][1].qc = x;
767
768

      if (x) {
769
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
770
        tokens[i][1].dqc = av1_dequant_abscoeff_nuq(
Thomas Davies's avatar
Thomas Davies committed
771
            abs(x), dqv, dequant_val[band_translate[i]]);
772
773
774
        tokens[i][1].dqc = shift ? ROUND_POWER_OF_TWO(tokens[i][1].dqc, shift)
                                 : tokens[i][1].dqc;
        if (sz) tokens[i][1].dqc = -tokens[i][1].dqc;
775
#else
776
777
778
779
// The 32x32 transform coefficient uses half quantization step size.
// Account for the rounding difference in the dequantized coefficeint
// value when the quantization index is dropped from an even number
// to an odd number.
Thomas Davies's avatar
Thomas Davies committed
780
781
782
783
784
785
786

#if CONFIG_AOM_QM
        tran_low_t offset = dqv >> shift;
#else
        tran_low_t offset = dq_step[rc != 0];
#endif
        if (shift & x) offset += (dqv & 0x01);
787
788
789
790
791

        if (sz == 0)
          tokens[i][1].dqc = dqcoeff[rc] - offset;
        else
          tokens[i][1].dqc = dqcoeff[rc] + offset;
792
#endif  // CONFIG_NEW_QUANT
793
794
795
796
      } else {
        tokens[i][1].dqc = 0;
      }

797
      tokens[i][1].best_index = best;
Jingning Han's avatar
Jingning Han committed
798
799
800
801
802
803
804
805
      /* Finally, make this the new head of the trellis. */
      next = i;
    } else {
      /* There's no choice to make for a zero coefficient, so we don't
       *  add a new trellis node, but we do need to update the costs.
       */
      t0 = tokens[next][0].token;
      t1 = tokens[next][1].token;
806
      pt = get_coef_context(nb, token_cache, i + 1);
Jingning Han's avatar
Jingning Han committed
807
808
      /* Update the cost of each path if we're past the EOB token. */
      if (t0 != EOB_TOKEN) {
809
        tokens[next][0].rate += get_token_bit_costs(*token_costs, 1, pt, t0);
Jingning Han's avatar
Jingning Han committed
810
811
812
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != EOB_TOKEN) {
813
        tokens[next][1].rate += get_token_bit_costs(*token_costs, 1, pt, t1);
Jingning Han's avatar
Jingning Han committed
814
815
        tokens[next][1].token = ZERO_TOKEN;
      }
816
      tokens[i][0].best_index = tokens[i][1].best_index = 0;
817
      shortcut = (tokens[next][0].rate != tokens[next][1].rate);
Jingning Han's avatar
Jingning Han committed
818
819
      /* Don't update next, because we didn't add a new node. */
    }
820

821
    if (UNLIKELY(!(--band_left))) {
822
823
824
825
      --band_counts;
      band_left = *band_counts;
      --token_costs;
    }
Jingning Han's avatar
Jingning Han committed
826
827
828
829
830
831
832
833
834
  }

  /* Now pick the best path through the whole trellis. */
  rate0 = tokens[next][0].rate;
  rate1 = tokens[next][1].rate;
  error0 = tokens[next][0].error;
  error1 = tokens[next][1].error;
  t0 = tokens[next][0].token;
  t1 = tokens[next][1].token;
835
836
  rate0 += get_token_bit_costs(*token_costs, 0, ctx, t0);
  rate1 += get_token_bit_costs(*token_costs, 0, ctx, t1);
Jingning Han's avatar
Jingning Han committed
837
838
  UPDATE_RD_COST();
  best = rd_cost1 < rd_cost0;
839

Jingning Han's avatar
Jingning Han committed
840
  final_eob = -1;
841

Jingning Han's avatar
Jingning Han committed
842
843
844
  for (i = next; i < eob; i = next) {
    const int x = tokens[i][best].qc;
    const int rc = scan[i];
845
    if (x) final_eob = i;
Jingning Han's avatar
Jingning Han committed
846
    qcoeff[rc] = x;
847
848
    dqcoeff[rc] = tokens[i][best].dqc;

Jingning Han's avatar
Jingning Han committed
849
    next = tokens[i][best].next;
850
    best = tokens[i][best].best_index;
Jingning Han's avatar
Jingning Han committed
851
852
853
854
  }
  final_eob++;

  mb->plane[plane].eobs[block] = final_eob;
855
  assert(final_eob <= default_eob);
Jingning Han's avatar
Jingning Han committed
856
  return final_eob;
857
858
859
860
861
862
863
#else   // !CONFIG_PVQ
  (void)cm;
  (void)tx_size;
  (void)ctx;
  struct macroblock_plane *const p = &mb->plane[plane];
  return p->eobs[block];
#endif  // !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
864
865
}

866
867
#endif  // USE_GREEDY_OPTIMIZE_B

Thomas Daede's avatar
Thomas Daede committed
868
#if !CONFIG_PVQ
869
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
870
871
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
872
  QUANT_FUNC_HIGHBD = 1,
873
  QUANT_FUNC_TYPES = 2
Angie Chiang's avatar
Angie Chiang committed
874
875
} QUANT_FUNC;

876
877
static AV1_QUANT_FACADE
    quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
878
#if !CONFIG_NEW_QUANT
879
      { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
Yaowu Xu's avatar
Yaowu Xu committed
880
881
      { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
      { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
882
#else   // !CONFIG_NEW_QUANT
883
884
885
      { av1_quantize_fp_nuq_facade, av1_highbd_quantize_fp_nuq_facade },
      { av1_quantize_b_nuq_facade, av1_highbd_quantize_b_nuq_facade },
      { av1_quantize_dc_nuq_facade, av1_highbd_quantize_dc_nuq_facade },
886
#endif  // !CONFIG_NEW_QUANT
887
888
      { NULL, NULL }
    };
889

Thomas Daede's avatar
Thomas Daede committed
890
#else
891

Angie Chiang's avatar
Angie Chiang committed
892
893
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
894
  QUANT_FUNC_TYPES = 1
Angie Chiang's avatar
Angie Chiang committed
895
} QUANT_FUNC;
Angie Chiang's avatar
Angie Chiang committed
896

897
898
static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_TYPES]
                                       [QUANT_FUNC_TYPES] = {
899
#if !CONFIG_NEW_QUANT
clang-format's avatar
clang-format committed
900
901
902
                                         { av1_quantize_fp_facade },
                                         { av1_quantize_b_facade },
                                         { av1_quantize_dc_facade },
903
#else   // !CONFIG_NEW_QUANT
904
905
906
                                         { av1_quantize_fp_nuq_facade },
                                         { av1_quantize_b_nuq_facade },
                                         { av1_quantize_dc_nuq_facade },
907
#endif  // !CONFIG_NEW_QUANT
clang-format's avatar
clang-format committed
908
909
                                         { NULL }
                                       };
910
#endif  // CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
911
#endif  // CONFIG_PVQ
912

Angie Chiang's avatar
Angie Chiang committed
913
914
void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
                     int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
915
916
                     TX_SIZE tx_size, int ctx,
                     AV1_XFORM_QUANT xform_quant_idx) {
Jingning Han's avatar
Jingning Han committed
917
  MACROBLOCKD *const xd = &x->e_mbd;
918
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yushin Cho's avatar
Yushin Cho committed
919
#if !(CONFIG_PVQ || CONFIG_DAALA_DIST)
Jingning Han's avatar
Jingning Han committed
920
921
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
922
923
924
925
#else
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
#endif
926
  PLANE_TYPE plane_type = get_plane_type(plane);
927
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
928
  const int is_inter = is_inter_block(mbmi);
Angie Chiang's avatar
Angie Chiang committed
929
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
Jingning Han's avatar
Jingning Han committed
930
931
932
933
  tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint16_t *const eob = &p->eobs[block];
934
  const int diff_stride = block_size_wide[plane_bsize];
935
#if CONFIG_AOM_QM
936
  int seg_id = mbmi->segment_id;
937
938
  const qm_val_t *qmatrix = pd->seg_qmatrix[seg_id][!is_inter][tx_size];
  const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!is_inter][tx_size];
939
#endif
Angie Chiang's avatar
Angie Chiang committed
940
941

  FWD_TXFM_PARAM fwd_txfm_param;
942

Yushin Cho's avatar
Yushin Cho committed
943
944
945
946
947
948
949
950
#if CONFIG_PVQ || CONFIG_DAALA_DIST
  uint8_t *dst;
  int16_t *pred;
  const int dst_stride = pd->dst.stride;
  int tx_blk_size;
  int i, j;
#endif

951
952
#if !CONFIG_PVQ
  const int tx2d_size = tx_size_2d[tx_size];
953
  QUANT_PARAM qparam;
954
955
  const int16_t *src_diff;

956
957
  src_diff =
      &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
958
  qparam.log_scale = get_tx_scale(tx_size);
959
960
961
962
963
964
965
966
#if CONFIG_NEW_QUANT
  qparam.tx_size = tx_size;
  qparam.dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
#endif  // CONFIG_NEW_QUANT
#if CONFIG_AOM_QM
  qparam.qmatrix = qmatrix;
  qparam.iqmatrix = iqmatrix;
#endif  // CONFIG_AOM_QM
967
968
969
970
#else
  tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block);
  int skip = 1;
  PVQ_INFO *pvq_info = NULL;
Yushin Cho's avatar
Yushin Cho committed
971
972
973
  uint8_t *src;
  int16_t *src_int16;
  const int src_stride = p->src.stride;
974

975
  (void)ctx;
976
977
978
979
980
981
982
  (void)scan_order;
  (void)qcoeff;

  if (x->pvq_coded) {
    assert(block < MAX_PVQ_BLOCKS_IN_SB);
    pvq_info = &x->pvq[block][plane];
  }
983
984
985
  src = &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
  src_int16 =
      &p->src_int16[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
Yushin Cho's avatar
Yushin Cho committed
986
987
988

  // transform block size in pixels
  tx_blk_size = tx_size_wide[tx_size];
989
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
990
991
992
993
994
995
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    for (j = 0; j < tx_blk_size; j++)
      for (i = 0; i < tx_blk_size; i++)
        src_int16[diff_stride * j + i] =
            CONVERT_TO_SHORTPTR(src)[src_stride * j + i];
  } else {
996
#endif  // CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
997
998
999
    for (j = 0; j < tx_blk_size; j++)
      for (i = 0; i < tx_blk_size; i++)
        src_int16[diff_stride * j + i] = src[src_stride * j + i];
1000
#if CONFIG_HIGHBITDEPTH