encodemb.c 35.8 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12
13
14
#include "./av1_rtcd.h"
#include "./aom_config.h"
#include "./aom_dsp_rtcd.h"
Jingning Han's avatar
Jingning Han committed
15

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

Angie Chiang's avatar
Angie Chiang committed
21
22
23
24
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
#include "aom_util/debug_util.h"
#endif  // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG

25
26
27
28
#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
29

30
#include "av1/encoder/av1_quantize.h"
31
#include "av1/encoder/encodemb.h"
32
33
34
#if CONFIG_LV_MAP
#include "av1/encoder/encodetxb.h"
#endif
35
#include "av1/encoder/hybrid_fwd_txfm.h"
36
37
38
#if CONFIG_DAALA_TX
#include "av1/common/daala_inv_txfm.h"
#endif
39
#include "av1/encoder/rd.h"
40
#include "av1/encoder/rdopt.h"
Jingning Han's avatar
Jingning Han committed
41

42
43
44
45
#if CONFIG_CFL
#include "av1/common/cfl.h"
#endif

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

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

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

    return;
  }

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

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

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

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

114
#if !CONFIG_LV_MAP
115
116
117
118
119
120
121
122
123
124
125
// Shifting negative values is undefined behaviour in C99,
// and could mislead the optimizer, who might assume the shifted is positive.
// This also avoids ubsan warnings.
// In practise, this gets inlined by the optimizer to a single instruction.
static INLINE int signed_shift_right(int x, int shift) {
  if (x >= 0)
    return x >> shift;
  else
    return -((-x) >> shift);
}

126
127
// These numbers are empirically obtained.
static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
Thomas Davies's avatar
Thomas Davies committed
128
  { 10, 7 }, { 8, 5 },
129
};
Jingning Han's avatar
Jingning Han committed
130

131
static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
132
                             int blk_row, int blk_col, int block,
133
                             TX_SIZE tx_size, int ctx, int fast_mode) {
134
135
136
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
137
138
139
140
  const PLANE_TYPE plane_type = pd->plane_type;
  const int eob = p->eobs[block];
  assert(mb->qindex > 0);
  assert((!plane_type && !plane) || (plane_type && plane));
141
  assert(eob <= av1_get_max_eob(tx_size));
142
143
144
145
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
  const tran_low_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
146
  const int16_t *const dequant_ptr = p->dequant_QTX;
147
  const uint8_t *const band_translate = get_band_translate(tx_size);
148
  const TX_TYPE tx_type =
Luc Trudeau's avatar
Luc Trudeau committed
149
      av1_get_tx_type(plane_type, xd, blk_row, blk_col, tx_size);
150
  const SCAN_ORDER *const scan_order =
Angie Chiang's avatar
Angie Chiang committed
151
      get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
152
153
  const int16_t *const scan = scan_order->scan;
  const int16_t *const nb = scan_order->neighbors;
154
155
156
157
158
159
#if CONFIG_DAALA_TX
  // This is one of the few places where RDO is done on coeffs; it
  // expects the coeffs to be in Q3/D11, so we need to scale them.
  int depth_shift = (TX_COEFF_DEPTH - 11) * 2;
  int depth_round = depth_shift > 1 ? (1 << depth_shift >> 1) : 0;
#else
160
  const int shift = av1_get_tx_scale(tx_size);
161
#endif
162
163
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
164
  const TX_SIZE qm_tx_size = get_qm_tx_size(tx_size);
165
  // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
166
167
168
169
170
  const qm_val_t *iqmatrix =
      IS_2D_TRANSFORM(tx_type)
          ? pd->seg_iqmatrix[seg_id][qm_tx_size]
          : cm->giqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size];
#endif  // CONFIG_AOM_QM
171
#if CONFIG_NEW_QUANT
172
  int dq = get_dq_profile(mb->qindex, ref, plane_type);
173
  const dequant_val_type_nuq *dequant_val = p->dequant_val_nuq_QTX[dq];
174
175
176
#endif  // CONFIG_NEW_QUANT
  int64_t rd_cost0, rd_cost1;
  int16_t t0, t1;
177
  int i, final_eob = 0;
178
  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
hui su's avatar
hui su committed
179
  int(*head_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] =
180
      mb->token_head_costs[get_txsize_entropy_ctx(tx_size)][plane_type][ref];
hui su's avatar
hui su committed
181
  int(*tail_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] =
182
      mb->token_tail_costs[get_txsize_entropy_ctx(tx_size)][plane_type][ref];
Dake He's avatar
Dake He committed
183

184
  const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
185
  int64_t rate0, rate1;
186
187
188
189
190
  int64_t eob_cost0, eob_cost1;
  tran_low_t before_best_eob_qc = 0;
  tran_low_t before_best_eob_dqc = 0;

  uint8_t token_cache[MAX_TX_SQUARE];
191
192
193
194

  // TODO(debargha): Implement a fast mode. For now just skip.
  if (fast_mode) return eob;

195
196
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
197
    token_cache[rc] = av1_pt_energy_class[av1_get_token(qcoeff[rc])];
198
199
200
201
  }

  /* Record the r-d cost */
  int64_t accu_rate = 0;
202
203
204
  // Initialized to the worst possible error for the largest transform size.
  // This ensures that it never goes negative.
  int64_t accu_error = ((int64_t)1) << 50;
205
  rate0 = head_token_costs[0][ctx][0];
206
  int64_t best_block_rd_cost = RDCOST(rdmult, rate0, accu_error);
207
208

  // int64_t best_block_rd_cost_all0 = best_block_rd_cost;
hui su's avatar
hui su committed
209
210
  const int seg_eob =
      av1_get_tx_eob(&cm->seg, xd->mi[0]->mbmi.segment_id, tx_size);
211
212
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
213
214
215
216
    const int x = qcoeff[rc];
    const int sz = -(x < 0);
    const int band_cur = band_translate[i];
    const int ctx_cur = (i == 0) ? ctx : get_coef_context(nb, token_cache, i);
Yaowu Xu's avatar
Yaowu Xu committed
217
    const int8_t eob_val =
hui su's avatar
hui su committed
218
219
        (i + 1 == eob) ? (i + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
    const int is_first = (i == 0);
220
221
222

    if (x == 0) {
      // no need to search when x == 0
223
224
225
      accu_rate += av1_get_coeff_token_cost(
          ZERO_TOKEN, eob_val, is_first, head_token_costs[band_cur][ctx_cur],
          tail_token_costs[band_cur][ctx_cur]);
226
227
      // accu_error does not change when x==0
    } else {
228
229
230
231
232
233
234
235
/*  Computing distortion
 */
// compute the distortion for the first candidate
// and the distortion for quantizing to 0.
#if CONFIG_DAALA_TX
      int dx0 = coeff[rc];
      const int64_t d0 = ((int64_t)dx0 * dx0 + depth_round) >> depth_shift;
#else
236
      int dx0 = abs(coeff[rc]) * (1 << shift);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
237
238
      dx0 >>= xd->bd - 8;

239
      const int64_t d0 = (int64_t)dx0 * dx0;
240
#endif
241
      const int x_a = x - 2 * sz - 1;
242
      int dqv = dequant_ptr[rc != 0];
243
#if CONFIG_AOM_QM
244
      if (iqmatrix != NULL) {
245
        const qm_val_t iwt = iqmatrix[rc];
246
247
        dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
      }
248
#endif  // CONFIG_AOM_QM
249

250
251
252
253
#if CONFIG_DAALA_TX
      int dx = dqcoeff[rc] - coeff[rc];
      const int64_t d2 = ((int64_t)dx * dx + depth_round) >> depth_shift;
#else
254
      int dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
255
      dx = signed_shift_right(dx, xd->bd - 8);
256
      const int64_t d2 = (int64_t)dx * dx;
257
#endif
258
259
260
261

      /* compute the distortion for the second candidate
       * x_a = x - 2 * sz + 1;
       */
262
      int64_t d2_a;
263
      if (x_a != 0) {
264
265
#if CONFIG_DAALA_TX
#if CONFIG_NEW_QUANT
266
        dx = av1_dequant_coeff_nuq(x_a, dqv, dequant_val[rc != 0]) - coeff[rc];
267
268
269
270
271
#else   // CONFIG_NEW_QUANT
        dx -= (dqv + sz) ^ sz;
#endif  // CONFIG_NEW_QUANT
        d2_a = ((int64_t)dx * dx + depth_round) >> depth_shift;
#else  // CONFIG_DAALA_TX
272
#if CONFIG_NEW_QUANT
273
        dx = av1_dequant_coeff_nuq(x_a, dqv, dequant_val[rc != 0]) -
Yaowu Xu's avatar
Yaowu Xu committed
274
             (coeff[rc] * (1 << shift));
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
275
        dx >>= xd->bd - 8;
276
#else   // CONFIG_NEW_QUANT
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
277
        dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
278
279
#endif  // CONFIG_NEW_QUANT
        d2_a = (int64_t)dx * dx;
280
#endif  // CONFIG_DAALA_TX
281
282
283
      } else {
        d2_a = d0;
      }
284

285
      // Computing RD cost
hui su's avatar
hui su committed
286
      int64_t base_bits;
287
288
      // rate cost of x
      base_bits = av1_get_token_cost(x, &t0, cat6_bits);
hui su's avatar
hui su committed
289
290
291
292
      rate0 = base_bits +
              av1_get_coeff_token_cost(t0, eob_val, is_first,
                                       head_token_costs[band_cur][ctx_cur],
                                       tail_token_costs[band_cur][ctx_cur]);
293
      // rate cost of x_a
294
      base_bits = av1_get_token_cost(x_a, &t1, cat6_bits);
hui su's avatar
hui su committed
295
296
297
298
299
300
301
302
      if (t1 == ZERO_TOKEN && eob_val) {
        rate1 = base_bits;
      } else {
        rate1 = base_bits +
                av1_get_coeff_token_cost(t1, eob_val, is_first,
                                         head_token_costs[band_cur][ctx_cur],
                                         tail_token_costs[band_cur][ctx_cur]);
      }
303

hui su's avatar
hui su committed
304
      int64_t next_bits0 = 0, next_bits1 = 0;
305
      if (i < eob - 1) {
hui su's avatar
hui su committed
306
        int ctx_next;
307
308
        const int band_next = band_translate[i + 1];
        const int token_next = av1_get_token(qcoeff[scan[i + 1]]);
Yaowu Xu's avatar
Yaowu Xu committed
309
        const int8_t eob_val_next =
hui su's avatar
hui su committed
310
            (i + 2 == eob) ? (i + 2 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
311
312
313

        token_cache[rc] = av1_pt_energy_class[t0];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
314
315
316
        next_bits0 = av1_get_coeff_token_cost(
            token_next, eob_val_next, 0, head_token_costs[band_next][ctx_next],
            tail_token_costs[band_next][ctx_next]);
317
318
319

        token_cache[rc] = av1_pt_energy_class[t1];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
320
321
322
        next_bits1 = av1_get_coeff_token_cost(
            token_next, eob_val_next, 0, head_token_costs[band_next][ctx_next],
            tail_token_costs[band_next][ctx_next]);
323
324
      }

325
326
      rd_cost0 = RDCOST(rdmult, (rate0 + next_bits0), d2);
      rd_cost1 = RDCOST(rdmult, (rate1 + next_bits1), d2_a);
327
      const int best_x = (rd_cost1 < rd_cost0);
328

329
      const int eob_v = (i + 1 == seg_eob) ? LAST_EOB : EARLY_EOB;
hui su's avatar
hui su committed
330
331
332
333
334
335
336
      int64_t next_eob_bits0, next_eob_bits1;
      int best_eob_x;
      next_eob_bits0 = av1_get_coeff_token_cost(
          t0, eob_v, is_first, head_token_costs[band_cur][ctx_cur],
          tail_token_costs[band_cur][ctx_cur]);
      eob_cost0 =
          RDCOST(rdmult, (accu_rate + next_eob_bits0), (accu_error + d2 - d0));
337
338
      eob_cost1 = eob_cost0;
      if (x_a != 0) {
hui su's avatar
hui su committed
339
340
341
342
        next_eob_bits1 = av1_get_coeff_token_cost(
            t1, eob_v, is_first, head_token_costs[band_cur][ctx_cur],
            tail_token_costs[band_cur][ctx_cur]);
        eob_cost1 = RDCOST(rdmult, (accu_rate + next_eob_bits1),
343
344
345
346
347
348
                           (accu_error + d2_a - d0));
        best_eob_x = (eob_cost1 < eob_cost0);
      } else {
        best_eob_x = 0;
      }

349
350
351
      const int dqc = dqcoeff[rc];
      int dqc_a = 0;
      if (best_x || best_eob_x) {
352
        if (x_a != 0) {
353
354
#if CONFIG_DAALA_TX
#if CONFIG_NEW_QUANT
355
          dqc_a = av1_dequant_abscoeff_nuq(abs(x_a), dqv, dequant_val[rc != 0]);
356
357
358
359
360
          if (sz) dqc_a = -dqc_a;
#else
          dqc_a = x_a * dqv;
#endif  // CONFIG_NEW_QUANT
#else   // CONFIG_DAALA_TX
361
#if CONFIG_NEW_QUANT
362
          dqc_a = av1_dequant_abscoeff_nuq(abs(x_a), dqv, dequant_val[rc != 0]);
363
364
365
          dqc_a = shift ? ROUND_POWER_OF_TWO(dqc_a, shift) : dqc_a;
          if (sz) dqc_a = -dqc_a;
#else
366
367
          if (x_a < 0)
            dqc_a = -((-x_a * dqv) >> shift);
368
          else
369
            dqc_a = (x_a * dqv) >> shift;
370
#endif  // CONFIG_NEW_QUANT
371
#endif  // CONFIG_DAALA_TX
372
373
        } else {
          dqc_a = 0;
374
        }
375
376
377
378
      }

      // record the better quantized value
      if (best_x) {
379
        assert(d2_a <= d0);
380
381
382
383
384
385
        qcoeff[rc] = x_a;
        dqcoeff[rc] = dqc_a;
        accu_rate += rate1;
        accu_error += d2_a - d0;
        token_cache[rc] = av1_pt_energy_class[t1];
      } else {
386
        assert(d2 <= d0);
387
388
389
390
        accu_rate += rate0;
        accu_error += d2 - d0;
        token_cache[rc] = av1_pt_energy_class[t0];
      }
391
      assert(accu_error >= 0);
392
393

      // determine whether to move the eob position to i+1
394
395
      const int use_a = (x_a != 0) && (best_eob_x);
      const int64_t best_eob_cost_i = use_a ? eob_cost1 : eob_cost0;
396
397
398
      if (best_eob_cost_i < best_block_rd_cost) {
        best_block_rd_cost = best_eob_cost_i;
        final_eob = i + 1;
399
400
401
402
403
404
405
        if (use_a) {
          before_best_eob_qc = x_a;
          before_best_eob_dqc = dqc_a;
        } else {
          before_best_eob_qc = x;
          before_best_eob_dqc = dqc;
        }
406
407
408
409
410
411
      }
    }  // if (x==0)
  }    // for (i)

  assert(final_eob <= eob);
  if (final_eob > 0) {
412
    assert(before_best_eob_qc != 0);
413
414
    i = final_eob - 1;
    int rc = scan[i];
415
416
    qcoeff[rc] = before_best_eob_qc;
    dqcoeff[rc] = before_best_eob_dqc;
417
418
419
420
421
422
423
424
  }

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

425
  p->eobs[block] = final_eob;
426
427
  return final_eob;
}
428
#endif  // !CONFIG_LV_MAP
429

Jingning Han's avatar
Jingning Han committed
430
431
432
int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int blk_row,
                   int blk_col, int block, BLOCK_SIZE plane_bsize,
                   TX_SIZE tx_size, const ENTROPY_CONTEXT *a,
433
                   const ENTROPY_CONTEXT *l, int fast_mode) {
434
435
436
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  const int eob = p->eobs[block];
437
438
  if (eob == 0 || !mb->optimize || xd->lossless[xd->mi[0]->mbmi.segment_id])
    return eob;
Jingning Han's avatar
Jingning Han committed
439

440
#if !CONFIG_LV_MAP
441
  (void)plane_bsize;
Jingning Han's avatar
Jingning Han committed
442
443
  (void)blk_row;
  (void)blk_col;
444
  int ctx = get_entropy_context(tx_size, a, l);
445
446
  return optimize_b_greedy(cm, mb, plane, blk_row, blk_col, block, tx_size, ctx,
                           fast_mode);
447
448
449
#else   // !CONFIG_LV_MAP
  TXB_CTX txb_ctx;
  get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
Jingning Han's avatar
Jingning Han committed
450
  return av1_optimize_txb(cm, mb, plane, blk_row, blk_col, block, tx_size,
451
                          &txb_ctx, fast_mode);
452
#endif  // !CONFIG_LV_MAP
453
}
454

Angie Chiang's avatar
Angie Chiang committed
455
456
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
457
  QUANT_FUNC_HIGHBD = 1,
458
  QUANT_FUNC_TYPES = 2
Angie Chiang's avatar
Angie Chiang committed
459
460
} QUANT_FUNC;

461
462
static AV1_QUANT_FACADE
    quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
463
#if !CONFIG_NEW_QUANT
464
      { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
Yaowu Xu's avatar
Yaowu Xu committed
465
466
      { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
      { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
467
#else   // !CONFIG_NEW_QUANT
468
469
470
      { av1_quantize_fp_nuq_facade, av1_highbd_quantize_fp_nuq_facade },
      { av1_quantize_b_nuq_facade, av1_highbd_quantize_b_nuq_facade },
      { av1_quantize_dc_nuq_facade, av1_highbd_quantize_dc_nuq_facade },
471
#endif  // !CONFIG_NEW_QUANT
472
473
      { NULL, NULL }
    };
474

Yushin Cho's avatar
Yushin Cho committed
475
#if !CONFIG_TXMG
476
typedef void (*fwdTxfmFunc)(const int16_t *diff, tran_low_t *coeff, int stride,
477
                            TxfmParam *txfm_param);
478
479
static const fwdTxfmFunc fwd_txfm_func[2] = { av1_fwd_txfm,
                                              av1_highbd_fwd_txfm };
Angie Chiang's avatar
Angie Chiang committed
480
#endif
481

Angie Chiang's avatar
Angie Chiang committed
482
483
void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
                     int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
484
                     TX_SIZE tx_size, AV1_XFORM_QUANT xform_quant_idx) {
Jingning Han's avatar
Jingning Han committed
485
  MACROBLOCKD *const xd = &x->e_mbd;
486
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yushin Cho's avatar
Yushin Cho committed
487
#if !CONFIG_DIST_8X8
Jingning Han's avatar
Jingning Han committed
488
489
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
490
491
492
493
#else
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
#endif
494
  PLANE_TYPE plane_type = get_plane_type(plane);
Luc Trudeau's avatar
Luc Trudeau committed
495
  TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col, tx_size);
Angie Chiang's avatar
Angie Chiang committed
496

497
#if CONFIG_NEW_QUANT
498
  const int is_inter = is_inter_block(mbmi);
Angie Chiang's avatar
Angie Chiang committed
499
500
#endif
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
Jingning Han's avatar
Jingning Han committed
501
502
503
504
  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];
505
  const int diff_stride = block_size_wide[plane_bsize];
Yushin Cho's avatar
Yushin Cho committed
506
#if CONFIG_AOM_QM
507
  int seg_id = mbmi->segment_id;
508
  const TX_SIZE qm_tx_size = get_qm_tx_size(tx_size);
509
  // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
510
511
512
513
514
515
516
  const qm_val_t *qmatrix =
      IS_2D_TRANSFORM(tx_type) ? pd->seg_qmatrix[seg_id][qm_tx_size]
                               : cm->gqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size];
  const qm_val_t *iqmatrix =
      IS_2D_TRANSFORM(tx_type)
          ? pd->seg_iqmatrix[seg_id][qm_tx_size]
          : cm->giqmatrix[NUM_QM_LEVELS - 1][0][qm_tx_size];
517
#endif  // CONFIG_AOM_QM
Angie Chiang's avatar
Angie Chiang committed
518

519
  TxfmParam txfm_param;
520

Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
521
#if CONFIG_DIST_8X8
Yushin Cho's avatar
Yushin Cho committed
522
523
  uint8_t *dst;
  const int dst_stride = pd->dst.stride;
Yushin Cho's avatar
Yushin Cho committed
524
#if CONFIG_DIST_8X8
Lester Lu's avatar
Lester Lu committed
525
  int16_t *pred;
526
527
  const int txw = tx_size_wide[tx_size];
  const int txh = tx_size_high[tx_size];
Yushin Cho's avatar
Yushin Cho committed
528
529
  int i, j;
#endif
Lester Lu's avatar
Lester Lu committed
530
#endif
Yushin Cho's avatar
Yushin Cho committed
531

532
  QUANT_PARAM qparam;
533
534
  const int16_t *src_diff;

535
536
  src_diff =
      &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
537
538
539
#if CONFIG_DAALA_TX
  qparam.log_scale = 0;
#else
540
  qparam.log_scale = av1_get_tx_scale(tx_size);
541
#endif
542
  qparam.tx_size = tx_size;
543
#if CONFIG_NEW_QUANT
544
  qparam.dq = get_dq_profile(x->qindex, is_inter, plane_type);
545
546
547
548
549
#endif  // CONFIG_NEW_QUANT
#if CONFIG_AOM_QM
  qparam.qmatrix = qmatrix;
  qparam.iqmatrix = iqmatrix;
#endif  // CONFIG_AOM_QM
Yushin Cho's avatar
Yushin Cho committed
550

Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
551
#if CONFIG_DIST_8X8
Yushin Cho's avatar
Yushin Cho committed
552
  dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
553
#endif  // CONFIG_DIST_8X8
554
555

#if CONFIG_DIST_8X8
Yushin Cho's avatar
Yushin Cho committed
556
  if (x->using_dist_8x8) {
557
    pred = &pd->pred[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
558

Thomas Daede's avatar
Thomas Daede committed
559
560
// copy uint8 orig and predicted block to int16 buffer
// in order to use existing VP10 transform functions
561
#if CONFIG_HIGHBITDEPTH
562
563
564
565
566
567
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
      for (j = 0; j < txh; j++)
        for (i = 0; i < txw; i++)
          pred[diff_stride * j + i] =
              CONVERT_TO_SHORTPTR(dst)[dst_stride * j + i];
    } else {
568
#endif  // CONFIG_HIGHBITDEPTH
569
570
571
      for (j = 0; j < txh; j++)
        for (i = 0; i < txw; i++)
          pred[diff_stride * j + i] = dst[dst_stride * j + i];
572
#if CONFIG_HIGHBITDEPTH
573
    }
574
#endif  // CONFIG_HIGHBITDEPTH
575
  }
Yushin Cho's avatar
Yushin Cho committed
576
#endif  // CONFIG_DIST_8X8
Yushin Cho's avatar
Yushin Cho committed
577

578
579
580
  txfm_param.tx_type = tx_type;
  txfm_param.tx_size = tx_size;
  txfm_param.lossless = xd->lossless[mbmi->segment_id];
581
582
583
  txfm_param.tx_set_type =
      get_ext_tx_set_type(txfm_param.tx_size, plane_bsize, is_inter_block(mbmi),
                          cm->reduced_tx_set_used);
Angie Chiang's avatar
Angie Chiang committed
584

585
  txfm_param.bd = xd->bd;
586
  txfm_param.is_hbd = get_bitdepth_data_path_index(xd);
Angie Chiang's avatar
Angie Chiang committed
587
588
589
590

#if CONFIG_TXMG
  av1_highbd_fwd_txfm(src_diff, coeff, diff_stride, &txfm_param);
#else   // CONFIG_TXMG
591
  fwd_txfm_func[txfm_param.is_hbd](src_diff, coeff, diff_stride, &txfm_param);
Angie Chiang's avatar
Angie Chiang committed
592
#endif  // CONFIG_TXMG
593

Yaowu Xu's avatar
Yaowu Xu committed
594
  if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
595
    const int n_coeffs = av1_get_max_eob(tx_size);
596
    if (LIKELY(!x->skip_block)) {
597
#if CONFIG_DAALA_TX
598
      quant_func_list[xform_quant_idx][1](coeff, n_coeffs, p, qcoeff, dqcoeff,
599
600
                                          eob, scan_order, &qparam);
#else
601
      quant_func_list[xform_quant_idx][txfm_param.is_hbd](
602
          coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, &qparam);
603
#endif
604
    } else {
605
      av1_quantize_skip(n_coeffs, qcoeff, dqcoeff, eob);
Jingning Han's avatar
Jingning Han committed
606
607
    }
  }
608
609
610
611
#if CONFIG_LV_MAP
  p->txb_entropy_ctx[block] =
      (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
#endif  // CONFIG_LV_MAP
612
  return;
Jingning Han's avatar
Jingning Han committed
613
614
}

615
static void encode_block(int plane, int block, int blk_row, int blk_col,
Angie Chiang's avatar
Angie Chiang committed
616
617
618
619
620
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg,
                         int mi_row, int mi_col, RUN_TYPE dry_run) {
  (void)mi_row;
  (void)mi_col;
  (void)dry_run;
Jingning Han's avatar
Jingning Han committed
621
  struct encode_b_args *const args = arg;
Angie Chiang's avatar
Angie Chiang committed
622
  AV1_COMMON *cm = args->cm;
Jingning Han's avatar
Jingning Han committed
623
624
625
626
627
628
629
  MACROBLOCK *const x = args->x;
  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;
  ENTROPY_CONTEXT *a, *l;
Yushin Cho's avatar
Yushin Cho committed
630

631
  int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
632
633
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
634

635
636
  a = &args->ta[blk_col];
  l = &args->tl[blk_row];
Jingning Han's avatar
Jingning Han committed
637

638
  // Assert not magic number (uninitialized).
639
  assert(x->blk_skip[plane][blk_row * bw + blk_col] != 234);
640

641
  if (x->blk_skip[plane][blk_row * bw + blk_col] == 0) {
Angie Chiang's avatar
Angie Chiang committed
642
    av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
643
                    AV1_XFORM_QUANT_FP);
644
  } else {
645
    p->eobs[block] = 0;
646
  }
647

Jingning Han's avatar
Jingning Han committed
648
  av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size, a,
649
                 l, CONFIG_LV_MAP);
650

651
652
  av1_set_txb_context(x, plane, block, tx_size, a, l);

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

655
  if (p->eobs[block] != 0)
656

657
658
  {
    TX_TYPE tx_type =
Luc Trudeau's avatar
Luc Trudeau committed
659
        av1_get_tx_type(pd->plane_type, xd, blk_row, blk_col, tx_size);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
660
661
662
    av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
                                pd->dst.stride, p->eobs[block],
                                cm->reduced_tx_set_used);
663
  }
664
665
666
667
668
669
670

#if CONFIG_TXK_SEL
  if (plane == 0 && p->eobs[block] == 0) {
#if DISABLE_TRELLISQ_SEARCH
    xd->mi[0]->mbmi.txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col] =
        DCT_DCT;
#else
Angie Chiang's avatar
Angie Chiang committed
671
    assert(xd->mi[0]->mbmi.txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col] ==
672
673
674
675
676
           DCT_DCT);
#endif
  }
#endif  // CONFIG_TXK_SEL

Angie Chiang's avatar
Angie Chiang committed
677
678
679
#if CONFIG_MISMATCH_DEBUG
  if (dry_run == OUTPUT_ENABLED) {
    int pixel_c, pixel_r;
680
681
682
    BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
    int blk_w = block_size_wide[bsize];
    int blk_h = block_size_high[bsize];
Angie Chiang's avatar
Angie Chiang committed
683
684
685
686
687
688
    mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, blk_col, blk_row,
                    pd->subsampling_x, pd->subsampling_y);
    mismatch_record_block_tx(dst, pd->dst.stride, plane, pixel_c, pixel_r,
                             blk_w, blk_h);
  }
#endif
Jingning Han's avatar
Jingning Han committed
689
690
}

691
692
static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
Angie Chiang's avatar
Angie Chiang committed
693
694
695
696
                               void *arg, int mi_row, int mi_col,
                               RUN_TYPE dry_run) {
  (void)mi_row;
  (void)mi_col;
697
698
699
700
  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;
701
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
702
  const struct macroblockd_plane *const pd = &xd->plane[plane];
703
704
  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
705
  TX_SIZE plane_tx_size;
706
707
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
708

709
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
710

711
712
713
  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
714

715
716
717
718
719
  if (tx_size == plane_tx_size
#if DISABLE_VARTX_FOR_CHROMA
      || pd->subsampling_x || pd->subsampling_y
#endif  // DISABLE_VARTX_FOR_CHROMA
      ) {
Angie Chiang's avatar
Angie Chiang committed
720
721
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg,
                 mi_row, mi_col, dry_run);
722
  } else {
723
    assert(tx_size < TX_SIZES_ALL);
724
    const TX_SIZE sub_txs = sub_tx_size_map[1][tx_size];
725
726
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
727
    // This is the square transform block partition entry point.
728
729
730
731
    const int bsw = tx_size_wide_unit[sub_txs];
    const int bsh = tx_size_high_unit[sub_txs];
    const int step = bsh * bsw;
    assert(bsw > 0 && bsh > 0);
732

733
734
735
736
    for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
      for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
        const int offsetr = blk_row + row;
        const int offsetc = blk_col + col;
737

738
        if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
739

740
        encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
Angie Chiang's avatar
Angie Chiang committed
741
                           arg, mi_row, mi_col, dry_run);
742
743
        block += step;
      }
744
745
746
747
    }
  }
}

Angie Chiang's avatar
Angie Chiang committed
748
749
750
751
752
typedef struct encode_block_pass1_args {
  AV1_COMMON *cm;
  MACROBLOCK *x;
} encode_block_pass1_args;

753
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
754
755
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
Angie Chiang's avatar
Angie Chiang committed
756
757
758
  encode_block_pass1_args *args = (encode_block_pass1_args *)arg;
  AV1_COMMON *cm = args->cm;
  MACROBLOCK *const x = args->x;
Jingning Han's avatar
Jingning Han committed
759
760
761
762
  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);
763
  TxfmParam txfm_param;
Jingning Han's avatar
Jingning Han committed
764
  uint8_t *dst;
765
766
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
Jingning Han's avatar
Jingning Han committed
767

Angie Chiang's avatar
Angie Chiang committed
768
  av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
769
                  AV1_XFORM_QUANT_B);
770

Yushin Cho's avatar
Yushin Cho committed
771
  if (p->eobs[block] > 0) {
772
    txfm_param.bd = xd->bd;
773
    txfm_param.is_hbd = get_bitdepth_data_path_index(xd);
774
    txfm_param.tx_type = DCT_DCT;
775
    txfm_param.tx_size = tx_size;
776
777
    txfm_param.eob = p->eobs[block];
    txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
778
779
780
    txfm_param.tx_set_type = get_ext_tx_set_type(
        txfm_param.tx_size, plane_bsize, is_inter_block(&xd->mi[0]->mbmi),
        cm->reduced_tx_set_used);
781
782
783
#if CONFIG_DAALA_TX
    daala_inv_txfm_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
#else
784
#if CONFIG_HIGHBITDEPTH
785
    if (txfm_param.is_hbd) {
786
      av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, pd->dst.stride, &txfm_param);
787
      return;
Jingning Han's avatar
Jingning Han committed
788
    }
789
#endif  //  CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
790
    if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
791
      av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
792
    } else {
793
      av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
794
    }
795
#endif
Jingning Han's avatar
Jingning Han committed
796
797
798
  }
}

Angie Chiang's avatar
Angie Chiang committed
799
800
void av1_encode_sby_pass1(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize) {
  encode_block_pass1_args args = { cm, x };
Yaowu Xu's avatar
Yaowu Xu committed
801
802
  av1_subtract_plane(x, bsize, 0);
  av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
Angie Chiang's avatar
Angie Chiang committed
803
                                         encode_block_pass1, &args);
Jingning Han's avatar
Jingning Han committed
804
805
}

Yaowu Xu's avatar
Yaowu Xu committed
806
void av1_encode_sb(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row,
Angie Chiang's avatar
Angie Chiang committed
807
808
                   int mi_col, RUN_TYPE dry_run) {
  (void)dry_run;
Jingning Han's avatar
Jingning Han committed
809
810
811
  MACROBLOCKD *const xd = &x->e_mbd;
  struct optimize_ctx ctx;
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Angie Chiang's avatar
Angie Chiang committed
812
  struct encode_b_args arg = { cm, x, &ctx, &mbmi->skip, NULL, NULL, 1 };
Jingning Han's avatar
Jingning Han committed
813
814
815
816
  int plane;

  mbmi->skip = 1;

817
  if (x->skip) return;
Jingning Han's avatar
Jingning Han committed
818
819

  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
820
821
822
823
824
    const int subsampling_x = xd->plane[plane].subsampling_x;
    const int subsampling_y = xd->plane[plane].subsampling_y;

    if (!is_chroma_reference(mi_row, mi_col, bsize, subsampling_x,
                             subsampling_y))
825
      continue;
826

827
828
    const BLOCK_SIZE bsizec =
        scale_chroma_bsize(bsize, subsampling_x, subsampling_y);
829

830
831
    // TODO(jingning): Clean this up.
    const struct macroblockd_plane *const pd = &xd->plane[plane];
832
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsizec, pd);
833
834
    const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
    const int mi_height = block_size_high[plane_bsize] >> tx_size_wide_log2[0];
835
    TX_SIZE max_tx_size = get_vartx_max_txsize(
836
        xd, plane_bsize, pd->subsampling_x || pd->subsampling_y);
837
838
839
840
841
842
843
844
845
846

#if DISABLE_VARTX_FOR_CHROMA == 2
    // If the luma transform size is split at least one level, split the chroma
    // by one level. Otherwise use the  largest possible trasnform size for
    // chroma.
    if (plane && (pd->subsampling_x || pd->subsampling_y)) {
      const TX_SIZE l_max_tx_size = get_vartx_max_txsize(xd, bsizec, 0);
      const int is_split =
          (l_max_tx_size != mbmi->inter_tx_size[0][0] && bsize == bsizec &&
           txsize_to_bsize[l_max_tx_size] == bsizec);
847
      if (is_split) max_tx_size = sub_tx_size_map[1][max_tx_size];
848
849
850
    }
#endif  // DISABLE_VARTX_FOR_CHROMA == 2

851
    const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
852
853
    const int bw = block_size_wide[txb_size] >> tx_size_wide_log2[0];
    const int bh = block_size_high[txb_size] >> tx_size_wide_log2[0];
854
855
    int idx, idy;
    int block = 0;
856
    int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
857
    av1_get_entropy_contexts(bsizec, 0, pd, ctx.ta[plane], ctx.tl[plane]);
858

859
    av1_subtract_plane(x, bsizec, plane);
Yushin Cho's avatar
Yushin Cho committed
860

861
862
    arg.ta = ctx.ta[plane];
    arg.tl = ctx.tl[plane];
863

864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
    const BLOCK_SIZE max_unit_bsize = get_plane_block_size(BLOCK_64X64, pd);
    int mu_blocks_wide =
        block_size_wide[max_unit_bsize] >> tx_size_wide_log2[0];
    int mu_blocks_high =
        block_size_high[max_unit_bsize] >> tx_size_high_log2[0];

    mu_blocks_wide = AOMMIN(mi_width, mu_blocks_wide);
    mu_blocks_high = AOMMIN(mi_height, mu_blocks_high);

    for (idy = 0; idy < mi_height; idy += mu_blocks_high) {
      for (idx = 0; idx < mi_width; idx += mu_blocks_wide) {
        int blk_row, blk_col;
        const int unit_height = AOMMIN(mu_blocks_high + idy, mi_height);