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

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

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

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

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

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

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

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

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
      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
181
      }
182
183

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Jingning Han's avatar
Jingning Han committed
383
384
385
  for (i = next; i < eob; i = next) {
    const int x = tokens[i][best].qc;
    const int rc = scan[i];
386
387
388
389
390
#if CONFIG_AOM_QM
    const int iwt = iqmatrix[rc];
    const int dequant =
        (dequant_ptr[rc != 0] * iwt + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
#endif
391
392

    if (x) final_eob = i;
Jingning Han's avatar
Jingning Han committed
393
    qcoeff[rc] = x;
394
395
    dqcoeff[rc] = tokens[i][best].dqc;

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

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

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

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

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

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

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

Yaowu Xu's avatar
Yaowu Xu committed
438
439
440
void av1_xform_quant(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
441
442
443
  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
444
  PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
hui su's avatar
hui su committed
445
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
446
447
  const scan_order *const scan_order =
      get_scan(tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
Jingning Han's avatar
Jingning Han committed
448
449
450
451
452
  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];
453
454
455
456
457
458
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
  int is_intra = !is_inter_block(&xd->mi[0]->mbmi);
  const qm_val_t *qmatrix = pd->seg_qmatrix[seg_id][is_intra][tx_size];
  const qm_val_t *iqmatrix = pd->seg_iqmatrix[seg_id][is_intra][tx_size];
#endif
Jingning Han's avatar
Jingning Han committed
459
  const int16_t *src_diff;
460
  const int tx2d_size = get_tx2d_size(tx_size);
Angie Chiang's avatar
Angie Chiang committed
461
462

  FWD_TXFM_PARAM fwd_txfm_param;
463
464
465
  QUANT_PARAM qparam;

  fwd_txfm_param.tx_type = tx_type;
Angie Chiang's avatar
Angie Chiang committed
466
  fwd_txfm_param.tx_size = tx_size;
467
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[xform_quant_idx];
Angie Chiang's avatar
Angie Chiang committed
468
469
470
  fwd_txfm_param.rd_transform = x->use_lp32x32fdct;
  fwd_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

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

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

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

511
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
512
513
514
void av1_xform_quant_nuq(MACROBLOCK *x, int plane, int block, int blk_row,
                         int blk_col, BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                         int ctx) {
515
516
517
518
519
  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);
520
521
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
  const scan_order *const scan_order = get_scan(tx_size, tx_type, is_inter);
522
523
524
  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);
525
  int dq = get_dq_profile_from_ctx(ctx, is_inter, plane_type);
526
527
528
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  const int16_t *src_diff;
529
  const uint8_t *band = get_band_translate(tx_size);
530
531
532
533
534

  FWD_TXFM_PARAM fwd_txfm_param;

  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
535
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_FP];
536
537
538
539
540
541
542
  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
543
#if CONFIG_AOM_HIGHBITDEPTH
544
545
546
  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);
547
    if (tx_size == TX_32X32) {
548
549
550
551
552
553
      highbd_quantize_32x32_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant,
          p->quant_shift, 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);
554
555
556
557
    } else {
      highbd_quantize_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
                          p->quant, p->quant_shift, pd->dequant,
                          (const cuml_bins_type_nuq *)p->cuml_bins_nuq[dq],
558
559
                          (const dequant_val_type_nuq *)pd->dequant_val_nuq[dq],
                          qcoeff, dqcoeff, eob, scan_order->scan, band);
560
561
562
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
563
#endif  // CONFIG_AOM_HIGHBITDEPTH
564
565

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

Yaowu Xu's avatar
Yaowu Xu committed
581
582
583
void av1_xform_quant_fp_nuq(MACROBLOCK *x, int plane, int block, int blk_row,
                            int blk_col, BLOCK_SIZE plane_bsize,
                            TX_SIZE tx_size, int ctx) {
584
585
586
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
587
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
588
589
  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);
590
591
  const scan_order *const scan_order = get_scan(tx_size, tx_type, is_inter);
  int dq = get_dq_profile_from_ctx(ctx, is_inter, plane_type);
592
593
594
595
596
597
  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;
598
  const uint8_t *band = get_band_translate(tx_size);
599
600
601
602
603

  FWD_TXFM_PARAM fwd_txfm_param;

  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
604
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_FP];
605
606
607
608
609
610
611
  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
612
#if CONFIG_AOM_HIGHBITDEPTH
613
614
615
  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);
616
    if (tx_size == TX_32X32) {
617
618
619
620
621
      highbd_quantize_32x32_fp_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp,
          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);
622
    } else {
623
624
625
626
627
      highbd_quantize_fp_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp,
          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);
628
629
630
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
631
#endif  // CONFIG_AOM_HIGHBITDEPTH
632
633

  fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
634
635
636
  if (tx_size == TX_32X32) {
    quantize_32x32_fp_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
                          p->quant_fp, pd->dequant,
637
638
639
                          (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);
640
  } else {
641
642
643
644
645
    quantize_fp_nuq(coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp,
                    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);
646
647
648
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
649
650
651
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) {
652
653
654
655
656
657
658
659
660
661
662
  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;
663
664
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
  int dq = get_dq_profile_from_ctx(ctx, is_inter, plane_type);
665
666
667
668
669

  FWD_TXFM_PARAM fwd_txfm_param;

  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
670
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_DC];
671
672
673
674
675
676
677
  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
678
#if CONFIG_AOM_HIGHBITDEPTH
679
680
681
  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);
682
    if (tx_size == TX_32X32) {
683
684
685
686
      highbd_quantize_dc_32x32_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant[0],
          p->quant_shift[0], pd->dequant[0], p->cuml_bins_nuq[dq][0],
          pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
687
688
    } else {
      highbd_quantize_dc_nuq(coeff, get_tx2d_size(tx_size), x->skip_block,
689
                             p->quant[0], p->quant_shift[0], pd->dequant[0],
690
                             p->cuml_bins_nuq[dq][0],
691
                             pd->dequant_val_nuq[dq][0], qcoeff, dqcoeff, eob);
692
693
694
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
695
#endif  // CONFIG_AOM_HIGHBITDEPTH
696
697

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

Yaowu Xu's avatar
Yaowu Xu committed
710
711
712
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) {
713
714
715
716
717
718
719
720
721
722
723
  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;
724
725
  const int is_inter = is_inter_block(&xd->mi[0]->mbmi);
  int dq = get_dq_profile_from_ctx(ctx, is_inter, plane_type);
726
727
728
729
730

  FWD_TXFM_PARAM fwd_txfm_param;

  fwd_txfm_param.tx_type = tx_type;
  fwd_txfm_param.tx_size = tx_size;
Yaowu Xu's avatar
Yaowu Xu committed
731
  fwd_txfm_param.fwd_txfm_opt = fwd_txfm_opt_list[AV1_XFORM_QUANT_DC];
732
733
734
735
736
737
738
  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
739
#if CONFIG_AOM_HIGHBITDEPTH
740
741
742
  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);
743
    if (tx_size == TX_32X32) {
744
745
746
747
      highbd_quantize_dc_32x32_fp_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp[0],
          pd->dequant[0], p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
          qcoeff, dqcoeff, eob);
748
    } else {
749
750
751
752
      highbd_quantize_dc_fp_nuq(
          coeff, get_tx2d_size(tx_size), x->skip_block, p->quant_fp[0],
          pd->dequant[0], p->cuml_bins_nuq[dq][0], pd->dequant_val_nuq[dq][0],
          qcoeff, dqcoeff, eob);
753
754
755
    }
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
756
#endif  // CONFIG_AOM_HIGHBITDEPTH
757
758

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

772
static void encode_block(int plane, int block, int blk_row, int blk_col,
773
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
Jingning Han's avatar
Jingning Han committed
774
775
776
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
777
  int ctx;
Jingning Han's avatar
Jingning Han committed
778
779
780
781
782
  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
783
  INV_TXFM_PARAM inv_txfm_param;
784
785
#if CONFIG_VAR_TX
  int i;
786
  const int bwl = b_width_log2_lookup[plane_bsize];
787
#endif
788
  dst = &pd->dst.buf[4 * blk_row * pd->dst.stride + 4 * blk_col];
789
790
  a = &args->ta[blk_col];
  l = &args->tl[blk_row];
791
792
793
794
795
#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
796

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

801
  if (x->blk_skip[plane][(blk_row << bwl) + blk_col] == 0) {
802
#else
803
  {
804
#endif
805
#if CONFIG_NEW_QUANT
Yaowu Xu's avatar
Yaowu Xu committed
806
807
    av1_xform_quant_fp_nuq(x, plane, block, blk_row, blk_col, plane_bsize,
                           tx_size, ctx);
808
#else
Yaowu Xu's avatar
Yaowu Xu committed
809
810
    av1_xform_quant(x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
                    AV1_XFORM_QUANT_FP);
811
#endif  // CONFIG_NEW_QUANT
Jingning Han's avatar
Jingning Han committed
812
  }
813
814
#if CONFIG_VAR_TX
  else {
815
    p->eobs[block] = 0;
816
817
  }
#endif
Jingning Han's avatar
Jingning Han committed
818

819
  if (p->eobs[block]) {
Yaowu Xu's avatar
Yaowu Xu committed
820
    *a = *l = av1_optimize_b(x, plane, block, tx_size, ctx) > 0;
Jingning Han's avatar
Jingning Han committed
821
822
823
824
  } else {
    *a = *l = p->eobs[block] > 0;
  }

825
#if CONFIG_VAR_TX
826
  for (i = 0; i < num_4x4_blocks_wide_txsize_lookup[tx_size]; ++i) {
827
    a[i] = a[0];
828
829
  }
  for (i = 0; i < num_4x4_blocks_high_txsize_lookup[tx_size]; ++i) {
830
831
832
833
    l[i] = l[0];
  }
#endif

834
  if (p->eobs[block]) *(args->skip) = 0;
Jingning Han's avatar
Jingning Han committed
835

836
  if (p->eobs[block] == 0) return;
Angie Chiang's avatar
Angie Chiang committed
837
838
839
840
841
842
843

  // inverse transform parameters
  inv_txfm_param.tx_type = get_tx_type(pd->plane_type, xd, block, tx_size);
  inv_txfm_param.tx_size = tx_size;
  inv_txfm_param.eob = p->eobs[block];
  inv_txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];

Yaowu Xu's avatar
Yaowu Xu committed
844
#if CONFIG_AOM_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
845
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Angie Chiang's avatar
Angie Chiang committed
846
847
    inv_txfm_param.bd = xd->bd;
    highbd_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &inv_txfm_param);
Jingning Han's avatar
Jingning Han committed
848
849
    return;
  }
Yaowu Xu's avatar
Yaowu Xu committed
850
#endif  // CONFIG_AOM_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
851
  inv_txfm_add(dqcoeff, dst, pd->dst.stride, &inv_txfm_param);
Jingning Han's avatar
Jingning Han committed
852
853
}

854
855
856
857
858
859
860
861
#if CONFIG_VAR_TX
static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
862
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
863
  const struct macroblockd_plane *const pd = &xd->plane[plane];
864
865
  const int tx_row = blk_row >> (1 - pd->subsampling_y);
  const int tx_col = blk_col >> (1 - pd->subsampling_x);
Debargha Mukherjee's avatar
Debargha Mukherjee committed
866
  TX_SIZE plane_tx_size;
867
868
869
870
871
872
873
874
875

  int max_blocks_high = num_4x4_blocks_high_lookup[plane_bsize];
  int max_blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize];

  if (xd->mb_to_bottom_edge < 0)
    max_blocks_high += xd->mb_to_bottom_edge >> (5 + pd->subsampling_y);
  if (xd->mb_to_right_edge < 0)
    max_blocks_wide += xd->mb_to_right_edge >> (5 + pd->subsampling_x);

876
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
877

878
879
880
  plane_tx_size =
      plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
            : mbmi->inter_tx_size[tx_row][tx_col];
Debargha Mukherjee's avatar
Debargha Mukherjee committed
881

882
  if (tx_size == plane_tx_size) {
883
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
884
885
886
887
888
889
890
  } else {
    int bsl = b_width_log2_lookup[bsize];
    int i;

    assert(bsl > 0);
    --bsl;

891
892
893
894
#if CONFIG_EXT_TX
    assert(tx_size < TX_SIZES);
#endif  // CONFIG_EXT_TX

895
896
897
    for (i = 0; i < 4; ++i) {
      const int offsetr = blk_row + ((i >> 1) << bsl);
      const int offsetc = blk_col + ((i & 0x01) << bsl);
898
      int step = num_4x4_blocks_txsize_lookup[tx_size - 1];
899

900
      if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
901

902
903
      encode_block_inter(plane, block + i * step, offsetr, offsetc, plane_bsize,
                         tx_size - 1, arg);
904
905
906
907
908
    }
  }
}
#endif

909
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
910
911
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
Jingning Han's avatar
Jingning Han committed
912
913
914
915
916
917
  MACROBLOCK *const x = (MACROBLOCK *)arg;
  MACROBLOCKD *const xd = &x->e_mbd;
  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;