encodemb.c 43.1 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/quantize.h"
Yaowu Xu's avatar
Yaowu Xu committed
17
#include "aom_mem/aom_mem.h"
18
#include "aom_ports/mem.h"
Jingning Han's avatar
Jingning Han committed
19

20 21 22 23
#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
24

25 26 27 28 29
#include "av1/encoder/encodemb.h"
#include "av1/encoder/hybrid_fwd_txfm.h"
#include "av1/encoder/quantize.h"
#include "av1/encoder/rd.h"
#include "av1/encoder/tokenize.h"
Jingning Han's avatar
Jingning Han committed
30

Yaowu Xu's avatar
Yaowu Xu committed
31
void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
Jingning Han's avatar
Jingning Han committed
32 33 34 35 36 37
  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 = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int bh = 4 * num_4x4_blocks_high_lookup[plane_bsize];

Yaowu Xu's avatar
Yaowu Xu committed
38
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
39
  if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Yaowu Xu's avatar
Yaowu Xu committed
40
    aom_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
Jingning Han's avatar
Jingning Han committed
41 42 43 44
                              p->src.stride, pd->dst.buf, pd->dst.stride,
                              x->e_mbd.bd);
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
45 46
#endif  // CONFIG_AOM_HIGHBITDEPTH
  aom_subtract_block(bh, bw, p->src_diff, bw, p->src.buf, p->src.stride,
Jingning Han's avatar
Jingning Han committed
47 48 49
                     pd->dst.buf, pd->dst.stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
50
typedef struct av1_token_state {
51 52 53 54 55 56
  int rate;
  int64_t error;
  int next;
  int16_t token;
  tran_low_t qc;
  tran_low_t dqc;
Yaowu Xu's avatar
Yaowu Xu committed
57
} av1_token_state;
Jingning Han's avatar
Jingning Han committed
58

59 60
// These numbers are empirically obtained.
static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
61
  { 10, 6 }, { 8, 5 },
62
};
Jingning Han's avatar
Jingning Han committed
63

64 65 66 67 68
#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
69

Angie Chiang's avatar
Angie Chiang committed
70 71
int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int block,
                   TX_SIZE tx_size, int ctx) {
Jingning Han's avatar
Jingning Han committed
72 73 74 75
  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
76
  av1_token_state tokens[MAX_TX_SQUARE + 1][2];
77
  unsigned best_index[MAX_TX_SQUARE + 1][2];
78
  uint8_t token_cache[MAX_TX_SQUARE];
Jingning Han's avatar
Jingning Han committed
79 80 81 82
  const tran_low_t *const coeff = BLOCK_OFFSET(mb->plane[plane].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];
83
  const PLANE_TYPE plane_type = pd->plane_type;
84
  const int default_eob = tx_size_2d[tx_size];
85 86
  const int16_t *const dequant_ptr = pd->dequant;
  const uint8_t *const band_translate = get_band_translate(tx_size);
87
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
88
  const SCAN_ORDER *const scan_order =
Angie Chiang's avatar
Angie Chiang committed
89
      get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
90 91
  const int16_t *const scan = scan_order->scan;
  const int16_t *const nb = scan_order->neighbors;
92 93
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
94
  const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][!ref][tx_size];
95
#endif
96
  const int shift = get_tx_scale(xd, tx_type, tx_size);
97
#if CONFIG_NEW_QUANT
98
  int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
99
  const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
100
#else
101
  const int dq_step[2] = { dequant_ptr[0] >> shift, dequant_ptr[1] >> shift };
102
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
103
  int next = eob, sz = 0;
104
  const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
hui su's avatar
hui su committed
105
  const int64_t rddiv = mb->rddiv;
Jingning Han's avatar
Jingning Han committed
106
  int64_t rd_cost0, rd_cost1;
107 108
  int rate0, rate1;
  int64_t error0, error1;
Jingning Han's avatar
Jingning Han committed
109
  int16_t t0, t1;
110 111
  int best, band = (eob < default_eob) ? band_translate[eob]
                                       : band_translate[eob - 1];
112
  int pt, i, final_eob;
Yaowu Xu's avatar
Yaowu Xu committed
113 114
#if CONFIG_AOM_HIGHBITDEPTH
  const int *cat6_high_cost = av1_get_high_cost_table(xd->bd);
Jingning Han's avatar
Jingning Han committed
115
#else
Yaowu Xu's avatar
Yaowu Xu committed
116
  const int *cat6_high_cost = av1_get_high_cost_table(8);
Jingning Han's avatar
Jingning Han committed
117
#endif
118
  unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
119
      mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref];
120 121
  const uint16_t *band_counts = &band_count_table[tx_size][band];
  uint16_t band_left = eob - band_cum_count_table[tx_size][band] + 1;
122 123
  int shortcut = 0;
  int next_shortcut = 0;
124

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

127
  token_costs += band;
Jingning Han's avatar
Jingning Han committed
128

129
  assert((!plane_type && !plane) || (plane_type && plane));
Jingning Han's avatar
Jingning Han committed
130
  assert(eob <= default_eob);
131

Jingning Han's avatar
Jingning Han committed
132 133 134 135 136 137 138 139 140
  /* 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];

141 142
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
Yaowu Xu's avatar
Yaowu Xu committed
143
    tokens[i][0].rate = av1_get_token_cost(qcoeff[rc], &t0, cat6_high_cost);
144
    tokens[i][0].token = t0;
Yaowu Xu's avatar
Yaowu Xu committed
145
    token_cache[rc] = av1_pt_energy_class[t0];
146
  }
Jingning Han's avatar
Jingning Han committed
147 148

  for (i = eob; i-- > 0;) {
149 150
    int base_bits, dx;
    int64_t d2;
Jingning Han's avatar
Jingning Han committed
151
    const int rc = scan[i];
152 153 154
#if CONFIG_AOM_QM
    int iwt = iqmatrix[rc];
#endif
Jingning Han's avatar
Jingning Han committed
155
    int x = qcoeff[rc];
156
    next_shortcut = shortcut;
157

Jingning Han's avatar
Jingning Han committed
158
    /* Only add a trellis state for non-zero coefficients. */
159
    if (UNLIKELY(x)) {
Jingning Han's avatar
Jingning Han committed
160 161 162 163 164
      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;
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      if (next_shortcut) {
        /* Consider both possible successor states. */
        if (next < default_eob) {
          pt = get_coef_context(nb, token_cache, i + 1);
          rate0 += (*token_costs)[0][pt][tokens[next][0].token];
          rate1 += (*token_costs)[0][pt][tokens[next][1].token];
        }
        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);
          rate0 += (*token_costs)[0][pt][tokens[next][0].token];
        }
        best = 0;
Jingning Han's avatar
Jingning Han committed
182
      }
183 184

      dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
Yaowu Xu's avatar
Yaowu Xu committed
185
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
186 187 188
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx >>= xd->bd - 8;
      }
Yaowu Xu's avatar
Yaowu Xu committed
189
#endif  // CONFIG_AOM_HIGHBITDEPTH
190
      d2 = (int64_t)dx * dx;
191
      tokens[i][0].rate += (best ? rate1 : rate0);
Jingning Han's avatar
Jingning Han committed
192 193 194
      tokens[i][0].error = d2 + (best ? error1 : error0);
      tokens[i][0].next = next;
      tokens[i][0].qc = x;
195
      tokens[i][0].dqc = dqcoeff[rc];
Jingning Han's avatar
Jingning Han committed
196 197 198 199 200 201
      best_index[i][0] = best;

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

202
      // The threshold of 3 is empirically obtained.
203
      if (UNLIKELY(abs(x) > 3)) {
204 205
        shortcut = 0;
      } else {
206
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
207 208
        shortcut = ((av1_dequant_abscoeff_nuq(abs(x), dequant_ptr[rc != 0],
                                              dequant_val[band_translate[i]]) >
209
                     (abs(coeff[rc]) << shift)) &&
Yaowu Xu's avatar
Yaowu Xu committed
210 211
                    (av1_dequant_abscoeff_nuq(abs(x) - 1, dequant_ptr[rc != 0],
                                              dequant_val[band_translate[i]]) <
212
                     (abs(coeff[rc]) << shift)));
213 214 215 216 217 218 219 220
#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
221
        if ((abs(x) * dequant_ptr[rc != 0] > (abs(coeff[rc]) << shift)) &&
222 223
            (abs(x) * dequant_ptr[rc != 0] <
             (abs(coeff[rc]) << shift) + dequant_ptr[rc != 0]))
224
#endif  // CONFIG_AOM_QM
225 226 227
          shortcut = 1;
        else
          shortcut = 0;
228
#endif  // CONFIG_NEW_QUANT
229
      }
Jingning Han's avatar
Jingning Han committed
230 231 232 233

      if (shortcut) {
        sz = -(x < 0);
        x -= 2 * sz + 1;
234 235 236 237
      } else {
        tokens[i][1] = tokens[i][0];
        best_index[i][1] = best_index[i][0];
        next = i;
238

239
        if (UNLIKELY(!(--band_left))) {
240 241 242 243
          --band_counts;
          band_left = *band_counts;
          --token_costs;
        }
244
        continue;
Jingning Han's avatar
Jingning Han committed
245 246 247 248 249 250 251 252 253
      }

      /* 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;
254
        base_bits = 0;
Jingning Han's avatar
Jingning Han committed
255
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
256
        base_bits = av1_get_token_cost(x, &t0, cat6_high_cost);
Jingning Han's avatar
Jingning Han committed
257 258
        t1 = t0;
      }
259 260

      if (next_shortcut) {
261
        if (LIKELY(next < default_eob)) {
262
          if (t0 != EOB_TOKEN) {
Yaowu Xu's avatar
Yaowu Xu committed
263
            token_cache[rc] = av1_pt_energy_class[t0];
264 265 266 267
            pt = get_coef_context(nb, token_cache, i + 1);
            rate0 += (*token_costs)[!x][pt][tokens[next][0].token];
          }
          if (t1 != EOB_TOKEN) {
Yaowu Xu's avatar
Yaowu Xu committed
268
            token_cache[rc] = av1_pt_energy_class[t1];
269 270 271 272 273 274 275 276 277 278 279
            pt = get_coef_context(nb, token_cache, i + 1);
            rate1 += (*token_costs)[!x][pt][tokens[next][1].token];
          }
        }

        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
280
          token_cache[rc] = av1_pt_energy_class[t0];
Jingning Han's avatar
Jingning Han committed
281
          pt = get_coef_context(nb, token_cache, i + 1);
282
          rate0 += (*token_costs)[!x][pt][tokens[next][0].token];
Jingning Han's avatar
Jingning Han committed
283
        }
284
        best = 0;
Jingning Han's avatar
Jingning Han committed
285 286
      }

287
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
288 289
      dx = av1_dequant_coeff_nuq(x, dequant_ptr[rc != 0],
                                 dequant_val[band_translate[i]]) -
290
           (coeff[rc] << shift);
Yaowu Xu's avatar
Yaowu Xu committed
291
#if CONFIG_AOM_HIGHBITDEPTH
hui su's avatar
hui su committed
292 293 294
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx >>= xd->bd - 8;
      }
Yaowu Xu's avatar
Yaowu Xu committed
295
#endif  // CONFIG_AOM_HIGHBITDEPTH
296
#else   // CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
297
#if CONFIG_AOM_HIGHBITDEPTH
hui su's avatar
hui su committed
298 299 300
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx -= ((dequant_ptr[rc != 0] >> (xd->bd - 8)) + sz) ^ sz;
      } else {
Jingning Han's avatar
Jingning Han committed
301
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
hui su's avatar
hui su committed
302 303 304
      }
#else
      dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
Yaowu Xu's avatar
Yaowu Xu committed
305
#endif  // CONFIG_AOM_HIGHBITDEPTH
306
#endif  // CONFIG_NEW_QUANT
307
      d2 = (int64_t)dx * dx;
hui su's avatar
hui su committed
308

Jingning Han's avatar
Jingning Han committed
309 310 311 312 313
      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;
314 315

      if (x) {
316
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
317
        tokens[i][1].dqc = av1_dequant_abscoeff_nuq(
318
            abs(x), dequant_ptr[rc != 0], dequant_val[band_translate[i]]);
319 320 321
        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;
322
#else
323 324 325 326 327
        tran_low_t offset = dq_step[rc != 0];
        // 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.
328
        if (shift & x) offset += (dequant_ptr[rc != 0] & 0x01);
329 330 331 332 333

        if (sz == 0)
          tokens[i][1].dqc = dqcoeff[rc] - offset;
        else
          tokens[i][1].dqc = dqcoeff[rc] + offset;
334
#endif  // CONFIG_NEW_QUANT
335 336 337 338
      } else {
        tokens[i][1].dqc = 0;
      }

Jingning Han's avatar
Jingning Han committed
339 340 341 342 343 344 345 346 347
      best_index[i][1] = best;
      /* 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;
348
      pt = get_coef_context(nb, token_cache, i + 1);
Jingning Han's avatar
Jingning Han committed
349 350
      /* Update the cost of each path if we're past the EOB token. */
      if (t0 != EOB_TOKEN) {
351
        tokens[next][0].rate += (*token_costs)[1][pt][t0];
Jingning Han's avatar
Jingning Han committed
352 353 354
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != EOB_TOKEN) {
355
        tokens[next][1].rate += (*token_costs)[1][pt][t1];
Jingning Han's avatar
Jingning Han committed
356 357 358
        tokens[next][1].token = ZERO_TOKEN;
      }
      best_index[i][0] = best_index[i][1] = 0;
359
      shortcut = (tokens[next][0].rate != tokens[next][1].rate);
Jingning Han's avatar
Jingning Han committed
360 361
      /* Don't update next, because we didn't add a new node. */
    }
362

363
    if (UNLIKELY(!(--band_left))) {
364 365 366 367
      --band_counts;
      band_left = *band_counts;
      --token_costs;
    }
Jingning Han's avatar
Jingning Han committed
368 369 370 371 372 373 374 375 376
  }

  /* 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;
377 378
  rate0 += (*token_costs)[0][ctx][t0];
  rate1 += (*token_costs)[0][ctx][t1];
Jingning Han's avatar
Jingning Han committed
379 380
  UPDATE_RD_COST();
  best = rd_cost1 < rd_cost0;
381

Jingning Han's avatar
Jingning Han committed
382
  final_eob = -1;
383

Jingning Han's avatar
Jingning Han committed
384 385 386
  for (i = next; i < eob; i = next) {
    const int x = tokens[i][best].qc;
    const int rc = scan[i];
387
    if (x) final_eob = i;
Jingning Han's avatar
Jingning Han committed
388
    qcoeff[rc] = x;
389 390
    dqcoeff[rc] = tokens[i][best].dqc;

Jingning Han's avatar
Jingning Han committed
391 392 393 394 395 396
    next = tokens[i][best].next;
    best = best_index[i][best];
  }
  final_eob++;

  mb->plane[plane].eobs[block] = final_eob;
397
  assert(final_eob <= default_eob);
Jingning Han's avatar
Jingning Han committed
398 399 400
  return final_eob;
}

Yaowu Xu's avatar
Yaowu Xu committed
401
#if CONFIG_AOM_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
402 403
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
404 405
  QUANT_FUNC_HIGHBD = 1,
  QUANT_FUNC_LAST = 2
Angie Chiang's avatar
Angie Chiang committed
406 407
} QUANT_FUNC;

Yaowu Xu's avatar
Yaowu Xu committed
408 409 410 411 412
static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_LAST][QUANT_FUNC_LAST] =
    { { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
      { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
      { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
      { NULL, NULL } };
413

Angie Chiang's avatar
Angie Chiang committed
414 415 416
#else
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
417
  QUANT_FUNC_LAST = 1
Angie Chiang's avatar
Angie Chiang committed
418
} QUANT_FUNC;
Angie Chiang's avatar
Angie Chiang committed
419

clang-format's avatar
clang-format committed
420 421 422 423 424 425 426
static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_LAST]
                                       [QUANT_FUNC_LAST] = {
                                         { av1_quantize_fp_facade },
                                         { av1_quantize_b_facade },
                                         { av1_quantize_dc_facade },
                                         { NULL }
                                       };
Angie Chiang's avatar
Angie Chiang committed
427
#endif
428

Yaowu Xu's avatar
Yaowu Xu committed
429
static FWD_TXFM_OPT fwd_txfm_opt_list[AV1_XFORM_QUANT_LAST] = {
430 431
  FWD_TXFM_OPT_NORMAL, FWD_TXFM_OPT_NORMAL, FWD_TXFM_OPT_DC, FWD_TXFM_OPT_NORMAL
};
432

Angie Chiang's avatar
Angie Chiang committed
433 434 435
void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
                     int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
                     TX_SIZE tx_size, AV1_XFORM_QUANT xform_quant_idx) {
Jingning Han's avatar
Jingning Han committed
436 437 438
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
hui su's avatar
hui su committed
439
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
hui su's avatar
hui su committed
440
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
441
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
Angie Chiang's avatar
Angie Chiang committed
442
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
Jingning Han's avatar
Jingning Han committed
443 444 445 446 447
  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];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
448 449
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
450 451
  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];
452
#endif
Jingning Han's avatar
Jingning Han committed
453
  const int16_t *src_diff;
454
  const int tx2d_size = tx_size_2d[tx_size];
Angie Chiang's avatar
Angie Chiang committed
455 456

  FWD_TXFM_PARAM fwd_txfm_param;
457 458 459
  QUANT_PARAM qparam;

  fwd_txfm_param.tx_type = tx_type;
Angie Chiang's avatar
Angie Chiang committed
460
  fwd_txfm_param.tx_size = tx_size;
461
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[xform_quant_idx];
Angie Chiang's avatar
Angie Chiang committed
462 463 464
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

465
  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];
Jingning Han's avatar
Jingning Han committed
466

467
  qparam.log_scale = get_tx_scale(xd, tx_type, tx_size);
Yaowu Xu's avatar
Yaowu Xu committed
468
#if CONFIG_AOM_HIGHBITDEPTH
469
  fwd_txfm_param.bd = xd->bd;
Jingning Han's avatar
Jingning Han committed
470
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Angie Chiang's avatar
Angie Chiang committed
471
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
Yaowu Xu's avatar
Yaowu Xu committed
472
    if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
473
      if (LIKELY(!x->skip_block)) {
474
        quant_func_list[xform_quant_idx][QUANT_FUNC_HIGHBD](
475 476 477 478 479 480
            coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam
#if CONFIG_AOM_QM
            ,
            qmatrix, iqmatrix
#endif  // CONFIG_AOM_QM
            );
481
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
482
        av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
483 484 485 486
      }
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
487
#endif  // CONFIG_AOM_HIGHBITDEPTH
488 489

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
Yaowu Xu's avatar
Yaowu Xu committed
490
  if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
491
    if (LIKELY(!x->skip_block)) {
492
      quant_func_list[xform_quant_idx][QUANT_FUNC_LOWBD](
493 494 495 496 497 498
          coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam
#if CONFIG_AOM_QM
          ,
          qmatrix, iqmatrix
#endif  // CONFIG_AOM_QM
          );
499
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
500
      av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
Jingning Han's avatar
Jingning Han committed
501 502 503 504
    }
  }
}

505
#if CONFIG_NEW_QUANT
Angie Chiang's avatar
Angie Chiang committed
506 507 508
void av1_xform_quant_nuq(const AV1_COMMON *cm, MACROBLOCK *x, int plane,
                         int block, int blk_row, int blk_col,
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int ctx) {
509 510 511 512 513
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
514
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
Angie Chiang's avatar
Angie Chiang committed
515
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
516 517 518
  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);
519
  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
520 521 522
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
523
  const uint8_t *band = get_band_translate(tx_size);
524 525 526

  FWD_TXFM_PARAM fwd_txfm_param;

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

529 530
  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
531
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_FP];
532 533 534 535 536 537 538
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];

// TODO(sarahparker) add all of these new quant quantize functions
// to quant_func_list, just trying to get this expr to work for now
Yaowu Xu's avatar
Yaowu Xu committed
539
#if CONFIG_AOM_HIGHBITDEPTH
540 541 542
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
543
    if (tx_size == TX_32X32) {
544
      highbd_quantize_32x32_nuq(
545 546
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant, p->quant_shift,
          pd->dequant, (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
547 548
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
549
    } else {
550 551
      highbd_quantize_nuq(coeff, tx_size_2d[tx_size], x->skip_block, p->quant,
                          p->quant_shift, pd->dequant,
552
                          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
553 554
                          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                          qcoeff, dqcoeff, eob, scan_order->scan, band);
555 556 557
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
558
#endif  // CONFIG_AOM_HIGHBITDEPTH
559 560

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
561
  if (tx_size == TX_32X32) {
562 563
    quantize_32x32_nuq(coeff, 1024, x->skip_block, p->quant, p->quant_shift,
                       pd->dequant,
564
                       (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
565 566
                       (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                       qcoeff, dqcoeff, eob, scan_order->scan, band);
567
  } else {
568
    quantize_nuq(coeff, tx_size_2d[tx_size], x->skip_block, p->quant,
569
                 p->quant_shift, pd->dequant,
570
                 (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
571 572
                 (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
                 dqcoeff, eob, scan_order->scan, band);
573 574 575
  }
}

Angie Chiang's avatar
Angie Chiang committed
576 577 578
void av1_xform_quant_fp_nuq(const AV1_COMMON *cm, MACROBLOCK *x, int plane,
                            int block, int blk_row, int blk_col,
                            BLOCK_SIZE plane_bsize, TX_SIZE tx_size, int ctx) {
579 580 581
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
582
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
583 584
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
Angie Chiang's avatar
Angie Chiang committed
585
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
586
  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
587 588 589 590 591 592
  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];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
593
  const uint8_t *band = get_band_translate(tx_size);
594 595 596

  FWD_TXFM_PARAM fwd_txfm_param;

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

599 600
  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
601
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_FP];
602 603 604 605 606 607 608
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];

// TODO(sarahparker) add all of these new quant quantize functions
// to quant_func_list, just trying to get this expr to work for now
Yaowu Xu's avatar
Yaowu Xu committed
609
#if CONFIG_AOM_HIGHBITDEPTH
610 611 612
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
613
    if (tx_size == TX_32X32) {
614
      highbd_quantize_32x32_fp_nuq(
615 616
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp, pd->dequant,
          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
617 618
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
619
    } else {
620
      highbd_quantize_fp_nuq(
621 622
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp, pd->dequant,
          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
623 624
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
625 626 627
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
628
#endif  // CONFIG_AOM_HIGHBITDEPTH
629 630

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
631
  if (tx_size == TX_32X32) {
632
    quantize_32x32_fp_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
633
                          p->quant_fp, pd->dequant,
634 635 636
                          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
                          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                          qcoeff, dqcoeff, eob, scan_order->scan, band);
637
  } else {
638
    quantize_fp_nuq(coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp,
639 640 641 642
                    pd->dequant,
                    (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
                    (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                    qcoeff, dqcoeff, eob, scan_order->scan, band);
643 644 645
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
646 647 648
void av1_xform_quant_dc_nuq(MACROBLOCK *x, int plane, int block, int blk_row,
                            int blk_col, BLOCK_SIZE plane_bsize,
                            TX_SIZE tx_size, int ctx) {
649 650 651 652 653 654 655 656 657 658 659
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
  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];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
660
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
661
  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
662 663 664

  FWD_TXFM_PARAM fwd_txfm_param;

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

667 668
  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
669
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_DC];
670 671 672 673 674 675 676
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];

// TODO(sarahparker) add all of these new quant quantize functions
// to quant_func_list, just trying to get this expr to work for now
Yaowu Xu's avatar
Yaowu Xu committed
677
#if CONFIG_AOM_HIGHBITDEPTH
678 679 680
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
681
    if (tx_size == TX_32X32) {
682
      highbd_quantize_dc_32x32_nuq(
683
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant[0],
684 685
          p->quant_shift[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
          pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
686
    } else {
687
      highbd_quantize_dc_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
688
                             p->quant[0], p->quant_shift[0], pd->dequant[0],
689
                             p->cuml_bins_nuq[dq][0],
690
                             pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
691 692 693
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
694
#endif  // CONFIG_AOM_HIGHBITDEPTH
695 696

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
697
  if (tx_size == TX_32X32) {
698
    quantize_dc_32x32_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
699
                          p->quant[0], p->quant_shift[0], pd->dequant[0],
700
                          p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
701 702
                          qcoeff, dqcoeff, eob);
  } else {
703
    quantize_dc_nuq(coeff, tx_size_2d[tx_size], x->skip_block, p->quant[0],
704 705
                    p->quant_shift[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
                    pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
706 707 708
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
709 710 711
void av1_xform_quant_dc_fp_nuq(MACROBLOCK *x, int plane, int block, int blk_row,
                               int blk_col, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, int ctx) {
712 713 714 715 716 717 718 719 720 721 722
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
  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];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
723
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
724
  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
725 726 727

  FWD_TXFM_PARAM fwd_txfm_param;

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

730 731
  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
732
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_DC];
733 734 735 736 737 738 739
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];

// TODO(sarahparker) add all of these new quant quantize functions
// to quant_func_list, just trying to get this expr to work for now
Yaowu Xu's avatar
Yaowu Xu committed
740
#if CONFIG_AOM_HIGHBITDEPTH
741 742 743
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
744
    if (tx_size == TX_32X32) {
745
      highbd_quantize_dc_32x32_fp_nuq(
746
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp[0],
747 748
          pd->dequant[0], p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
          qcoeff, dqcoeff, eob);
749
    } else {
750
      highbd_quantize_dc_fp_nuq(
751
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp[0],
752 753
          pd->dequant[0], p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
          qcoeff, dqcoeff, eob);
754 755 756
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
757
#endif  // CONFIG_AOM_HIGHBITDEPTH
758 759

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
760
  if (tx_size == TX_32X32) {
761
    quantize_dc_32x32_fp_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
762 763
                             p->quant_fp[0], pd->dequant[0],
                             p->cuml_bins_nuq[dq][0],
764
                             pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
765
  } else {
766
    quantize_dc_fp_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
767 768
                       p->quant_fp[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
                       pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
769 770 771 772
  }
}
#endif  // CONFIG_NEW_QUANT

773
static void encode_block(int plane, int block, int blk_row, int blk_col,
774
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
Jingning Han's avatar
Jingning Han committed
775
  struct encode_b_args *const args = arg;
Angie Chiang's avatar
Angie Chiang committed
776
  AV1_COMMON *cm = args->cm;
Jingning Han's avatar
Jingning Han committed
777 778
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
779
  int ctx;
Jingning Han's avatar
Jingning Han committed
780 781 782 783 784
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint8_t *dst;
  ENTROPY_CONTEXT *a, *l;
Angie Chiang's avatar
Angie Chiang committed
785
  INV_TXFM_PARAM inv_txfm_param;
786 787
#if CONFIG_VAR_TX
  int i;
788
  const int bwl = b_width_log2_lookup[plane_bsize];
789
#endif
790
  dst = &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col];
791 792
  a = &args->ta[blk_col];
  l = &args->tl[blk_row];
793 794 795 796 797
#if CONFIG_VAR_TX
  ctx = get_entropy_context(tx_size, a, l);
#else
  ctx = combine_entropy_contexts(*a, *l);
#endif
Jingning Han's avatar
Jingning Han committed
798

Alex Converse's avatar
Alex Converse committed
799
#if CONFIG_VAR_TX
800
  // Assert not magic number (uninitialized).
801 802
  assert(x->blk_skip[plane][(blk_row << bwl) + blk_col] != 234);

803
  if (x->blk_skip[plane][(blk_row << bwl) + blk_col] == 0) {
804
#else
805
  {
806
#endif
807
#if CONFIG_NEW_QUANT
Angie Chiang's avatar
Angie Chiang committed
808
    av1_xform_quant_fp_nuq(cm, x, plane, block, blk_row, blk_col, plane_bsize,
Yaowu Xu's avatar
Yaowu Xu committed
809
                           tx_size, ctx);
Sarah Parker's avatar