encodemb.c 54.7 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

31 32 33 34 35 36
#if CONFIG_PVQ
#include "av1/encoder/encint.h"
#include "av1/common/partition.h"
#include "av1/encoder/pvq_encoder.h"
#endif

Yaowu Xu's avatar
Yaowu Xu committed
37
void av1_subtract_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
Jingning Han's avatar
Jingning Han committed
38 39 40 41 42 43
  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
44
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
45
  if (x->e_mbd.cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Yaowu Xu's avatar
Yaowu Xu committed
46
    aom_highbd_subtract_block(bh, bw, p->src_diff, bw, p->src.buf,
Jingning Han's avatar
Jingning Han committed
47 48 49 50
                              p->src.stride, pd->dst.buf, pd->dst.stride,
                              x->e_mbd.bd);
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
51 52
#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
53 54 55
                     pd->dst.buf, pd->dst.stride);
}

Yaowu Xu's avatar
Yaowu Xu committed
56
typedef struct av1_token_state {
57 58 59 60 61 62
  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
63
} av1_token_state;
Jingning Han's avatar
Jingning Han committed
64

65 66
// These numbers are empirically obtained.
static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
67
  { 10, 6 }, { 8, 5 },
68
};
Jingning Han's avatar
Jingning Han committed
69

70 71 72 73 74
#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
75

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

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

133
  token_costs += band;
Jingning Han's avatar
Jingning Han committed
134

135
  assert((!plane_type && !plane) || (plane_type && plane));
Jingning Han's avatar
Jingning Han committed
136
  assert(eob <= default_eob);
137

Jingning Han's avatar
Jingning Han committed
138 139 140 141 142 143 144 145 146
  /* 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];

147 148
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
Yaowu Xu's avatar
Yaowu Xu committed
149
    tokens[i][0].rate = av1_get_token_cost(qcoeff[rc], &t0, cat6_high_cost);
150
    tokens[i][0].token = t0;
Yaowu Xu's avatar
Yaowu Xu committed
151
    token_cache[rc] = av1_pt_energy_class[t0];
152
  }
Jingning Han's avatar
Jingning Han committed
153 154

  for (i = eob; i-- > 0;) {
155 156
    int base_bits, dx;
    int64_t d2;
Jingning Han's avatar
Jingning Han committed
157
    const int rc = scan[i];
158 159 160
#if CONFIG_AOM_QM
    int iwt = iqmatrix[rc];
#endif
Jingning Han's avatar
Jingning Han committed
161
    int x = qcoeff[rc];
162
    next_shortcut = shortcut;
163

Jingning Han's avatar
Jingning Han committed
164
    /* Only add a trellis state for non-zero coefficients. */
165
    if (UNLIKELY(x)) {
Jingning Han's avatar
Jingning Han committed
166 167 168 169 170
      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;
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
      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
188
      }
189 190

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

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

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

      if (shortcut) {
        sz = -(x < 0);
        x -= 2 * sz + 1;
240 241 242 243
      } else {
        tokens[i][1] = tokens[i][0];
        best_index[i][1] = best_index[i][0];
        next = i;
244

245
        if (UNLIKELY(!(--band_left))) {
246 247 248 249
          --band_counts;
          band_left = *band_counts;
          --token_costs;
        }
250
        continue;
Jingning Han's avatar
Jingning Han committed
251 252 253 254 255 256 257 258 259
      }

      /* 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;
260
        base_bits = 0;
Jingning Han's avatar
Jingning Han committed
261
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
262
        base_bits = av1_get_token_cost(x, &t0, cat6_high_cost);
Jingning Han's avatar
Jingning Han committed
263 264
        t1 = t0;
      }
265 266

      if (next_shortcut) {
267
        if (LIKELY(next < default_eob)) {
268
          if (t0 != EOB_TOKEN) {
Yaowu Xu's avatar
Yaowu Xu committed
269
            token_cache[rc] = av1_pt_energy_class[t0];
270 271 272 273
            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
274
            token_cache[rc] = av1_pt_energy_class[t1];
275 276 277 278 279 280 281 282 283 284 285
            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
286
          token_cache[rc] = av1_pt_energy_class[t0];
Jingning Han's avatar
Jingning Han committed
287
          pt = get_coef_context(nb, token_cache, i + 1);
288
          rate0 += (*token_costs)[!x][pt][tokens[next][0].token];
Jingning Han's avatar
Jingning Han committed
289
        }
290
        best = 0;
Jingning Han's avatar
Jingning Han committed
291 292
      }

293
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
294 295
      dx = av1_dequant_coeff_nuq(x, dequant_ptr[rc != 0],
                                 dequant_val[band_translate[i]]) -
296
           (coeff[rc] << shift);
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 >>= xd->bd - 8;
      }
Yaowu Xu's avatar
Yaowu Xu committed
301
#endif  // CONFIG_AOM_HIGHBITDEPTH
302
#else   // CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
303
#if CONFIG_AOM_HIGHBITDEPTH
hui su's avatar
hui su committed
304 305 306
      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
307
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
hui su's avatar
hui su committed
308 309 310
      }
#else
      dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
Yaowu Xu's avatar
Yaowu Xu committed
311
#endif  // CONFIG_AOM_HIGHBITDEPTH
312
#endif  // CONFIG_NEW_QUANT
313
      d2 = (int64_t)dx * dx;
hui su's avatar
hui su committed
314

Jingning Han's avatar
Jingning Han committed
315 316 317 318 319
      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;
320 321

      if (x) {
322
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
323
        tokens[i][1].dqc = av1_dequant_abscoeff_nuq(
324
            abs(x), dequant_ptr[rc != 0], dequant_val[band_translate[i]]);
325 326 327
        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;
328
#else
329 330 331 332 333
        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.
334
        if (shift & x) offset += (dequant_ptr[rc != 0] & 0x01);
335 336 337 338 339

        if (sz == 0)
          tokens[i][1].dqc = dqcoeff[rc] - offset;
        else
          tokens[i][1].dqc = dqcoeff[rc] + offset;
340
#endif  // CONFIG_NEW_QUANT
341 342 343 344
      } else {
        tokens[i][1].dqc = 0;
      }

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

369
    if (UNLIKELY(!(--band_left))) {
370 371 372 373
      --band_counts;
      band_left = *band_counts;
      --token_costs;
    }
Jingning Han's avatar
Jingning Han committed
374 375 376 377 378 379 380 381 382
  }

  /* 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;
383 384
  rate0 += (*token_costs)[0][ctx][t0];
  rate1 += (*token_costs)[0][ctx][t1];
Jingning Han's avatar
Jingning Han committed
385 386
  UPDATE_RD_COST();
  best = rd_cost1 < rd_cost0;
387

Jingning Han's avatar
Jingning Han committed
388
  final_eob = -1;
389

Jingning Han's avatar
Jingning Han committed
390 391 392
  for (i = next; i < eob; i = next) {
    const int x = tokens[i][best].qc;
    const int rc = scan[i];
393
    if (x) final_eob = i;
Jingning Han's avatar
Jingning Han committed
394
    qcoeff[rc] = x;
395 396
    dqcoeff[rc] = tokens[i][best].dqc;

Jingning Han's avatar
Jingning Han committed
397 398 399 400 401 402
    next = tokens[i][best].next;
    best = best_index[i][best];
  }
  final_eob++;

  mb->plane[plane].eobs[block] = final_eob;
403
  assert(final_eob <= default_eob);
Jingning Han's avatar
Jingning Han committed
404 405 406
  return final_eob;
}

Yaowu Xu's avatar
Yaowu Xu committed
407
#if CONFIG_AOM_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
408 409
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
410 411
  QUANT_FUNC_HIGHBD = 1,
  QUANT_FUNC_LAST = 2
Angie Chiang's avatar
Angie Chiang committed
412 413
} QUANT_FUNC;

Yaowu Xu's avatar
Yaowu Xu committed
414 415 416 417 418
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 } };
419

Angie Chiang's avatar
Angie Chiang committed
420 421 422
#else
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
423
  QUANT_FUNC_LAST = 1
Angie Chiang's avatar
Angie Chiang committed
424
} QUANT_FUNC;
Angie Chiang's avatar
Angie Chiang committed
425

clang-format's avatar
clang-format committed
426 427 428 429 430 431 432
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
433
#endif
434

Yaowu Xu's avatar
Yaowu Xu committed
435
static FWD_TXFM_OPT fwd_txfm_opt_list[AV1_XFORM_QUANT_LAST] = {
436 437
  FWD_TXFM_OPT_NORMAL, FWD_TXFM_OPT_NORMAL, FWD_TXFM_OPT_DC, FWD_TXFM_OPT_NORMAL
};
438

Angie Chiang's avatar
Angie Chiang committed
439 440 441
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
442
  MACROBLOCKD *const xd = &x->e_mbd;
443
#if !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
444 445
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
446 447 448 449
#else
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
#endif
hui su's avatar
hui su committed
450
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
hui su's avatar
hui su committed
451
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
452
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
Angie Chiang's avatar
Angie Chiang committed
453
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
Jingning Han's avatar
Jingning Han committed
454 455 456 457 458
  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];
459 460
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
461 462
  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];
463
#endif
Angie Chiang's avatar
Angie Chiang committed
464 465

  FWD_TXFM_PARAM fwd_txfm_param;
466 467 468

#if !CONFIG_PVQ
  const int tx2d_size = tx_size_2d[tx_size];
469
  QUANT_PARAM qparam;
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 504 505 506 507 508
  const int16_t *src_diff;

  src_diff = &p->src_diff[4 * (blk_row * diff_stride + blk_col)];
  qparam.log_scale = get_tx_scale(xd, tx_type, tx_size);
#else
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
  tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block);
  uint8_t *src, *dst;
  int16_t *src_int16, *pred;
  const int src_stride = p->src.stride;
  const int dst_stride = pd->dst.stride;
  int tx_blk_size;
  int i, j;
  int skip = 1;
  PVQ_INFO *pvq_info = NULL;

  (void)scan_order;
  (void)qcoeff;

  if (x->pvq_coded) {
    assert(block < MAX_PVQ_BLOCKS_IN_SB);
    pvq_info = &x->pvq[block][plane];
  }
  dst = &pd->dst.buf[4 * (blk_row * dst_stride + blk_col)];
  src = &p->src.buf[4 * (blk_row * src_stride + blk_col)];
  src_int16 = &p->src_int16[4 * (blk_row * diff_stride + blk_col)];
  pred = &pd->pred[4 * (blk_row * diff_stride + blk_col)];

  // transform block size in pixels
  tx_blk_size = tx_size_wide[tx_size];

  // copy uint8 orig and predicted block to int16 buffer
  // in order to use existing VP10 transform functions
  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];
      pred[diff_stride * j + i] = dst[dst_stride * j + i];
    }
#endif
509 510

  fwd_txfm_param.tx_type = tx_type;
Angie Chiang's avatar
Angie Chiang committed
511
  fwd_txfm_param.tx_size = tx_size;
512
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[xform_quant_idx];
Angie Chiang's avatar
Angie Chiang committed
513 514 515
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

Yaowu Xu's avatar
Yaowu Xu committed
516
#if CONFIG_AOM_HIGHBITDEPTH
517
  fwd_txfm_param.bd = xd->bd;
Jingning Han's avatar
Jingning Han committed
518
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Angie Chiang's avatar
Angie Chiang committed
519
    highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
Yaowu Xu's avatar
Yaowu Xu committed
520
    if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
521
      if (LIKELY(!x->skip_block)) {
522
        quant_func_list[xform_quant_idx][QUANT_FUNC_HIGHBD](
523 524 525 526 527 528
            coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam
#if CONFIG_AOM_QM
            ,
            qmatrix, iqmatrix
#endif  // CONFIG_AOM_QM
            );
529
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
530
        av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
531 532 533 534
      }
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
535
#endif  // CONFIG_AOM_HIGHBITDEPTH
536

537
#if !CONFIG_PVQ
538
  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
Yaowu Xu's avatar
Yaowu Xu committed
539
  if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
540
    if (LIKELY(!x->skip_block)) {
541
      quant_func_list[xform_quant_idx][QUANT_FUNC_LOWBD](
542 543 544 545 546 547
          coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam
#if CONFIG_AOM_QM
          ,
          qmatrix, iqmatrix
#endif  // CONFIG_AOM_QM
          );
548
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
549
      av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
Jingning Han's avatar
Jingning Han committed
550 551
    }
  }
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
#else   // #if !CONFIG_PVQ
  fwd_txfm_param.rd_transform = 0;

  fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param);
  fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param);

  // PVQ for inter mode block
  if (!x->skip_block)
    skip = av1_pvq_encode_helper(&x->daala_enc,
                                 coeff,        // target original vector
                                 ref_coeff,    // reference vector
                                 dqcoeff,      // de-quantized vector
                                 eob,          // End of Block marker
                                 pd->dequant,  // aom's quantizers
                                 plane,        // image plane
                                 tx_size,      // block size in log_2 - 2
                                 tx_type,
                                 &x->rate,  // rate measured
                                 x->pvq_speed,
                                 pvq_info);  // PVQ info for a block

  x->pvq_skip[plane] = skip;

  if (!skip) mbmi->skip = 0;
#endif  // #if !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
577 578
}

579
#if CONFIG_NEW_QUANT
Angie Chiang's avatar
Angie Chiang committed
580 581 582
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) {
583 584 585 586 587
  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);
588
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
Angie Chiang's avatar
Angie Chiang committed
589
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
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);
593
  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
594 595 596
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
597
  const uint8_t *band = get_band_translate(tx_size);
598 599 600

  FWD_TXFM_PARAM fwd_txfm_param;

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

603 604
  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
605
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_FP];
606 607 608 609 610 611 612
  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
613
#if CONFIG_AOM_HIGHBITDEPTH
614 615 616
  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);
617
    if (tx_size == TX_32X32) {
618
      highbd_quantize_32x32_nuq(
619 620
          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],
621 622
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
623
    } else {
624 625
      highbd_quantize_nuq(coeff, tx_size_2d[tx_size], x->skip_block, p->quant,
                          p->quant_shift, pd->dequant,
626
                          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
627 628
                          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                          qcoeff, dqcoeff, eob, scan_order->scan, band);
629 630 631
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
632
#endif  // CONFIG_AOM_HIGHBITDEPTH
633 634

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
635
  if (tx_size == TX_32X32) {
636 637
    quantize_32x32_nuq(coeff, 1024, x->skip_block, p->quant, p->quant_shift,
                       pd->dequant,
638
                       (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
639 640
                       (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                       qcoeff, dqcoeff, eob, scan_order->scan, band);
641
  } else {
642
    quantize_nuq(coeff, tx_size_2d[tx_size], x->skip_block, p->quant,
643
                 p->quant_shift, pd->dequant,
644
                 (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
645 646
                 (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
                 dqcoeff, eob, scan_order->scan, band);
647 648 649
  }
}

Angie Chiang's avatar
Angie Chiang committed
650 651 652
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) {
653 654 655
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
656
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
657 658
  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
659
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
660
  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
661 662 663 664 665 666
  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;
667
  const uint8_t *band = get_band_translate(tx_size);
668 669 670

  FWD_TXFM_PARAM fwd_txfm_param;

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

673 674
  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
675
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_FP];
676 677 678 679 680 681 682
  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
683
#if CONFIG_AOM_HIGHBITDEPTH
684 685 686
  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);
687
    if (tx_size == TX_32X32) {
688
      highbd_quantize_32x32_fp_nuq(
689 690
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp, pd->dequant,
          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
691 692
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
693
    } else {
694
      highbd_quantize_fp_nuq(
695 696
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp, pd->dequant,
          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
697 698
          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq], qcoeff,
          dqcoeff, eob, scan_order->scan, band);
699 700 701
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
702
#endif  // CONFIG_AOM_HIGHBITDEPTH
703 704

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
705
  if (tx_size == TX_32X32) {
706
    quantize_32x32_fp_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
707
                          p->quant_fp, pd->dequant,
708 709 710
                          (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);
711
  } else {
712
    quantize_fp_nuq(coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp,
713 714 715 716
                    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);
717 718 719
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
720 721 722
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) {
723 724 725 726 727 728 729 730 731 732 733
  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;
734
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
735
  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
736 737 738

  FWD_TXFM_PARAM fwd_txfm_param;

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

741 742
  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
743
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_DC];
744 745 746 747 748 749 750
  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
751
#if CONFIG_AOM_HIGHBITDEPTH
752 753 754
  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);
755
    if (tx_size == TX_32X32) {
756
      highbd_quantize_dc_32x32_nuq(
757
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant[0],
758 759
          p->quant_shift[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
          pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
760
    } else {
761
      highbd_quantize_dc_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
762
                             p->quant[0], p->quant_shift[0], pd->dequant[0],
763
                             p->cuml_bins_nuq[dq][0],
764
                             pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
765 766 767
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
768
#endif  // CONFIG_AOM_HIGHBITDEPTH
769 770

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
771
  if (tx_size == TX_32X32) {
772
    quantize_dc_32x32_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
773
                          p->quant[0], p->quant_shift[0], pd->dequant[0],
774
                          p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
775 776
                          qcoeff, dqcoeff, eob);
  } else {
777
    quantize_dc_nuq(coeff, tx_size_2d[tx_size], x->skip_block, p->quant[0],
778 779
                    p->quant_shift[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
                    pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
780 781 782
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
783 784 785
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) {
786 787 788 789 790 791 792 793 794 795 796
  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;
797
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
798
  int dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
799 800 801

  FWD_TXFM_PARAM fwd_txfm_param;

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

804 805
  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
806
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_DC];
807 808 809 810 811 812 813
  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
814
#if CONFIG_AOM_HIGHBITDEPTH
815 816 817
  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);
818
    if (tx_size == TX_32X32) {
819
      highbd_quantize_dc_32x32_fp_nuq(
820
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp[0],
821 822
          pd->dequant[0], p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
          qcoeff, dqcoeff, eob);
823
    } else {
824
      highbd_quantize_dc_fp_nuq(
825
          coeff, tx_size_2d[tx_size], x->skip_block, p->quant_fp[0],
826 827
          pd->dequant[0], p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
          qcoeff, dqcoeff, eob);
828 829 830
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
831
#endif  // CONFIG_AOM_HIGHBITDEPTH
832 833

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
834
  if (tx_size == TX_32X32) {
835
    quantize_dc_32x32_fp_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
836 837
                             p->quant_fp[0], pd->dequant[0],
                             p->cuml_bins_nuq[dq][0],
838
                             pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
839
  } else {
840
    quantize_dc_fp_nuq(coeff, tx_size_2d[tx_size], x->skip_block,
841 842
                       p->quant_fp[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
                       pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
843 844 845 846
  }
}
#endif  // CONFIG_NEW_QUANT

847
static void encode_block(int plane, int block, int blk_row, int blk_col,
848
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
Jingning Han's avatar
Jingning Han committed
849
  struct encode_b_args *const args = arg;
Angie Chiang's avatar
Angie Chiang committed
850
  AV1_COMMON *cm = args->cm;
Jingning Han's avatar
Jingning Han committed
851 852
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
853
  int ctx;