encodemb.c 34.3 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
36
#include "av1/encoder/hybrid_fwd_txfm.h"
#include "av1/encoder/rd.h"
37
#include "av1/encoder/rdopt.h"
Jingning Han's avatar
Jingning Han committed
38

39
40
41
42
#if CONFIG_CFL
#include "av1/common/cfl.h"
#endif

43
44
45
// 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
46
47
48
49
50
51
52
53
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) {
  if (check_subtract_block_size(rows, cols)) {
    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);
54
55
      return;
    }
Angie Chiang's avatar
Angie Chiang committed
56
57
    aom_subtract_block_c(rows, cols, diff, diff_stride, src8, src_stride, pred8,
                         pred_stride);
58
59
60
61

    return;
  }

Angie Chiang's avatar
Angie Chiang committed
62
63
64
  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
65
66
    return;
  }
Angie Chiang's avatar
Angie Chiang committed
67
68
69
70
  aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
                     pred_stride);
}

Angie Chiang's avatar
Angie Chiang committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
91
92
93
94
95
96
97
98
99
100
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
101
102
}

103
#if !CONFIG_LV_MAP
104
105
106
107
108
109
110
111
112
113
114
// 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);
}

115
116
// These numbers are empirically obtained.
static const int plane_rd_mult[REF_TYPES][PLANE_TYPES] = {
Thomas Davies's avatar
Thomas Davies committed
117
  { 10, 7 }, { 8, 5 },
118
};
Jingning Han's avatar
Jingning Han committed
119

120
static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
121
                             int blk_row, int blk_col, int block,
122
                             TX_SIZE tx_size, int ctx, int fast_mode) {
123
124
125
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
126
127
128
129
  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));
130
  assert(eob <= av1_get_max_eob(tx_size));
131
132
133
134
  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);
135
  const int16_t *const dequant_ptr = p->dequant_QTX;
136
  const uint8_t *const band_translate = get_band_translate(tx_size);
137
138
  const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
                                          tx_size, cm->reduced_tx_set_used);
139
  const SCAN_ORDER *const scan_order =
Angie Chiang's avatar
Angie Chiang committed
140
      get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
141
142
  const int16_t *const scan = scan_order->scan;
  const int16_t *const nb = scan_order->neighbors;
143
  const int shift = av1_get_tx_scale(tx_size);
144
145
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
146
  const TX_SIZE qm_tx_size = av1_get_adjusted_tx_size(tx_size);
147
  // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
148
149
150
151
152
  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
153
#if CONFIG_NEW_QUANT
154
  int dq = get_dq_profile(cm->dq_type, mb->qindex, ref, plane_type);
155
#if !CONFIG_AOM_QM
156
  const dequant_val_type_nuq *dequant_val = p->dequant_val_nuq_QTX[dq];
157
#endif  // !CONFIG_AOM_QM
158
159
160
#endif  // CONFIG_NEW_QUANT
  int64_t rd_cost0, rd_cost1;
  int16_t t0, t1;
161
  int i, final_eob = 0;
162
  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
hui su's avatar
hui su committed
163
  int(*head_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] =
164
      mb->token_head_costs[get_txsize_entropy_ctx(tx_size)][plane_type][ref];
hui su's avatar
hui su committed
165
  int(*tail_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] =
166
      mb->token_tail_costs[get_txsize_entropy_ctx(tx_size)][plane_type][ref];
Dake He's avatar
Dake He committed
167

168
  const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
169
  int64_t rate0, rate1;
170
171
172
173
174
  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];
175
176
177
178

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

179
180
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
181
    token_cache[rc] = av1_pt_energy_class[av1_get_token(qcoeff[rc])];
182
183
184
185
  }

  /* Record the r-d cost */
  int64_t accu_rate = 0;
186
187
188
  // 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;
189
  rate0 = head_token_costs[0][ctx][0];
190
  int64_t best_block_rd_cost = RDCOST(rdmult, rate0, accu_error);
191
192

  // int64_t best_block_rd_cost_all0 = best_block_rd_cost;
hui su's avatar
hui su committed
193
194
  const int seg_eob =
      av1_get_tx_eob(&cm->seg, xd->mi[0]->mbmi.segment_id, tx_size);
195
196
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
197
198
199
200
    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
201
    const int8_t eob_val =
hui su's avatar
hui su committed
202
203
        (i + 1 == eob) ? (i + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
    const int is_first = (i == 0);
204
205
206

    if (x == 0) {
      // no need to search when x == 0
207
208
209
      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]);
210
211
      // accu_error does not change when x==0
    } else {
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
212
213
214
215
      /*  Computing distortion
       */
      // compute the distortion for the first candidate
      // and the distortion for quantizing to 0.
216
      int dx0 = abs(coeff[rc]) * (1 << shift);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
217
218
      dx0 >>= xd->bd - 8;

219
220
      const int64_t d0 = (int64_t)dx0 * dx0;
      const int x_a = x - 2 * sz - 1;
221
      int dqv = dequant_ptr[rc != 0];
222
#if CONFIG_AOM_QM
223
      if (iqmatrix != NULL) {
224
        const qm_val_t iwt = iqmatrix[rc];
225
226
        dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
      }
227
#endif  // CONFIG_AOM_QM
228

229
      int dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
230
      dx = signed_shift_right(dx, xd->bd - 8);
231
      const int64_t d2 = (int64_t)dx * dx;
232
233
234
235

      /* compute the distortion for the second candidate
       * x_a = x - 2 * sz + 1;
       */
236
      int64_t d2_a;
237
238
      if (x_a != 0) {
#if CONFIG_NEW_QUANT
239
240
241
242
#if CONFIG_AOM_QM
        dx = av1_dequant_coeff_nuq(x_a, dqv, dq, rc != 0, 0) -
             (coeff[rc] * (1 << shift));
#else
243
        dx = av1_dequant_coeff_nuq(x_a, dqv, dequant_val[rc != 0], 0) -
Yaowu Xu's avatar
Yaowu Xu committed
244
             (coeff[rc] * (1 << shift));
245
#endif  // CONFIG_AOM_QM
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
246
        dx >>= xd->bd - 8;
247
#else   // CONFIG_NEW_QUANT
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
248
        dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
249
250
251
252
253
#endif  // CONFIG_NEW_QUANT
        d2_a = (int64_t)dx * dx;
      } else {
        d2_a = d0;
      }
254

255
      // Computing RD cost
hui su's avatar
hui su committed
256
      int64_t base_bits;
257
258
      // rate cost of x
      base_bits = av1_get_token_cost(x, &t0, cat6_bits);
hui su's avatar
hui su committed
259
260
261
262
      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]);
263
      // rate cost of x_a
264
      base_bits = av1_get_token_cost(x_a, &t1, cat6_bits);
hui su's avatar
hui su committed
265
266
267
268
269
270
271
272
      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]);
      }
273

hui su's avatar
hui su committed
274
      int64_t next_bits0 = 0, next_bits1 = 0;
275
      if (i < eob - 1) {
hui su's avatar
hui su committed
276
        int ctx_next;
277
278
        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
279
        const int8_t eob_val_next =
hui su's avatar
hui su committed
280
            (i + 2 == eob) ? (i + 2 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
281
282
283

        token_cache[rc] = av1_pt_energy_class[t0];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
284
285
286
        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]);
287
288
289

        token_cache[rc] = av1_pt_energy_class[t1];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
290
291
292
        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]);
293
294
      }

295
296
      rd_cost0 = RDCOST(rdmult, (rate0 + next_bits0), d2);
      rd_cost1 = RDCOST(rdmult, (rate1 + next_bits1), d2_a);
297
      const int best_x = (rd_cost1 < rd_cost0);
298

299
      const int eob_v = (i + 1 == seg_eob) ? LAST_EOB : EARLY_EOB;
hui su's avatar
hui su committed
300
301
302
303
304
305
306
      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));
307
308
      eob_cost1 = eob_cost0;
      if (x_a != 0) {
hui su's avatar
hui su committed
309
310
311
312
        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),
313
314
315
316
317
318
                           (accu_error + d2_a - d0));
        best_eob_x = (eob_cost1 < eob_cost0);
      } else {
        best_eob_x = 0;
      }

319
320
321
      const int dqc = dqcoeff[rc];
      int dqc_a = 0;
      if (best_x || best_eob_x) {
322
323
        if (x_a != 0) {
#if CONFIG_NEW_QUANT
324
325
326
#if CONFIG_AOM_QM
          dqc_a = av1_dequant_abscoeff_nuq(abs(x_a), dqv, dq, rc != 0, shift);
#else
327
328
          dqc_a = av1_dequant_abscoeff_nuq(abs(x_a), dqv, dequant_val[rc != 0],
                                           shift);
329
#endif  // CONFIG_AOM_QM
330
331
          if (sz) dqc_a = -dqc_a;
#else
332
333
          if (x_a < 0)
            dqc_a = -((-x_a * dqv) >> shift);
334
          else
335
            dqc_a = (x_a * dqv) >> shift;
336
337
338
#endif  // CONFIG_NEW_QUANT
        } else {
          dqc_a = 0;
339
        }
340
341
342
343
      }

      // record the better quantized value
      if (best_x) {
344
        assert(d2_a <= d0);
345
346
347
348
349
350
        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 {
351
        assert(d2 <= d0);
352
353
354
355
        accu_rate += rate0;
        accu_error += d2 - d0;
        token_cache[rc] = av1_pt_energy_class[t0];
      }
356
      assert(accu_error >= 0);
357
358

      // determine whether to move the eob position to i+1
359
360
      const int use_a = (x_a != 0) && (best_eob_x);
      const int64_t best_eob_cost_i = use_a ? eob_cost1 : eob_cost0;
361
362
363
      if (best_eob_cost_i < best_block_rd_cost) {
        best_block_rd_cost = best_eob_cost_i;
        final_eob = i + 1;
364
365
366
367
368
369
370
        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;
        }
371
372
373
374
375
376
      }
    }  // if (x==0)
  }    // for (i)

  assert(final_eob <= eob);
  if (final_eob > 0) {
377
    assert(before_best_eob_qc != 0);
378
379
    i = final_eob - 1;
    int rc = scan[i];
380
381
    qcoeff[rc] = before_best_eob_qc;
    dqcoeff[rc] = before_best_eob_dqc;
382
383
384
385
386
387
388
389
  }

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

390
  p->eobs[block] = final_eob;
391
392
  return final_eob;
}
393
#endif  // !CONFIG_LV_MAP
394

395
int av1_optimize_b(const struct AV1_COMP *cpi, MACROBLOCK *mb, int plane,
396
                   int blk_row, int blk_col, int block, BLOCK_SIZE plane_bsize,
Jingning Han's avatar
Jingning Han committed
397
                   TX_SIZE tx_size, const ENTROPY_CONTEXT *a,
Cheng Chen's avatar
Cheng Chen committed
398
                   const ENTROPY_CONTEXT *l, int fast_mode, int *rate_cost) {
399
400
401
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  const int eob = p->eobs[block];
402
403
  if (eob == 0 || !mb->optimize || xd->lossless[xd->mi[0]->mbmi.segment_id])
    return eob;
Jingning Han's avatar
Jingning Han committed
404

405
#if !CONFIG_LV_MAP
406
  (void)plane_bsize;
Jingning Han's avatar
Jingning Han committed
407
408
  (void)blk_row;
  (void)blk_col;
409
  int ctx = get_entropy_context(tx_size, a, l);
410
  const AV1_COMMON *const cm = &cpi->common;
411
412
  return optimize_b_greedy(cm, mb, plane, blk_row, blk_col, block, tx_size, ctx,
                           fast_mode);
413
414
415
#else   // !CONFIG_LV_MAP
  TXB_CTX txb_ctx;
  get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
416
  return av1_optimize_txb(cpi, mb, plane, blk_row, blk_col, block, tx_size,
Cheng Chen's avatar
Cheng Chen committed
417
                          &txb_ctx, fast_mode, rate_cost);
418
#endif  // !CONFIG_LV_MAP
419
}
420

Angie Chiang's avatar
Angie Chiang committed
421
422
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
423
  QUANT_FUNC_HIGHBD = 1,
424
  QUANT_FUNC_TYPES = 2
Angie Chiang's avatar
Angie Chiang committed
425
426
} QUANT_FUNC;

427
428
static AV1_QUANT_FACADE
    quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
429
#if !CONFIG_NEW_QUANT
430
      { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
Yaowu Xu's avatar
Yaowu Xu committed
431
432
      { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
      { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
433
#else   // !CONFIG_NEW_QUANT
434
435
436
      { 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 },
437
#endif  // !CONFIG_NEW_QUANT
438
439
      { NULL, NULL }
    };
440

Angie Chiang's avatar
Angie Chiang committed
441
442
void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
                     int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
443
                     TX_SIZE tx_size, AV1_XFORM_QUANT xform_quant_idx) {
Jingning Han's avatar
Jingning Han committed
444
  MACROBLOCKD *const xd = &x->e_mbd;
445
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Jingning Han's avatar
Jingning Han committed
446
447
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
448
  PLANE_TYPE plane_type = get_plane_type(plane);
449
450
  TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col, tx_size,
                                    cm->reduced_tx_set_used);
Angie Chiang's avatar
Angie Chiang committed
451

452
#if CONFIG_NEW_QUANT
453
  const int is_inter = is_inter_block(mbmi);
Angie Chiang's avatar
Angie Chiang committed
454
455
#endif
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
Jingning Han's avatar
Jingning Han committed
456
457
458
459
  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];
460
  const int diff_stride = block_size_wide[plane_bsize];
Yushin Cho's avatar
Yushin Cho committed
461
#if CONFIG_AOM_QM
462
  int seg_id = mbmi->segment_id;
463
  const TX_SIZE qm_tx_size = av1_get_adjusted_tx_size(tx_size);
464
  // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
465
466
467
468
469
470
471
  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];
472
#endif  // CONFIG_AOM_QM
Angie Chiang's avatar
Angie Chiang committed
473

474
  TxfmParam txfm_param;
475
  QUANT_PARAM qparam;
476
477
  const int16_t *src_diff;

478
479
  src_diff =
      &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
480
  qparam.log_scale = av1_get_tx_scale(tx_size);
481
  qparam.tx_size = tx_size;
482
#if CONFIG_NEW_QUANT
483
  qparam.dq = get_dq_profile(cm->dq_type, x->qindex, is_inter, plane_type);
484
  qparam.x0 = get_x0_profile(x->optimize, x->qindex, is_inter, plane_type);
485
486
487
488
489
#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
490

491
492
493
  txfm_param.tx_type = tx_type;
  txfm_param.tx_size = tx_size;
  txfm_param.lossless = xd->lossless[mbmi->segment_id];
494
495
496
  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
497

498
  txfm_param.bd = xd->bd;
499
  txfm_param.is_hbd = get_bitdepth_data_path_index(xd);
Angie Chiang's avatar
Angie Chiang committed
500

501
  av1_highbd_fwd_txfm(src_diff, coeff, diff_stride, &txfm_param);
502

Yaowu Xu's avatar
Yaowu Xu committed
503
  if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
504
    const int n_coeffs = av1_get_max_eob(tx_size);
505
    if (LIKELY(!x->skip_block)) {
506
      quant_func_list[xform_quant_idx][txfm_param.is_hbd](
507
          coeff, n_coeffs, p, qcoeff, dqcoeff, eob, scan_order, &qparam);
508
    } else {
509
      av1_quantize_skip(n_coeffs, qcoeff, dqcoeff, eob);
Jingning Han's avatar
Jingning Han committed
510
511
    }
  }
512
513
514
515
#if CONFIG_LV_MAP
  p->txb_entropy_ctx[block] =
      (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
#endif  // CONFIG_LV_MAP
516
  return;
Jingning Han's avatar
Jingning Han committed
517
518
}

519
static void encode_block(int plane, int block, int blk_row, int blk_col,
Angie Chiang's avatar
Angie Chiang committed
520
521
522
523
524
                         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
525
  struct encode_b_args *const args = arg;
526
  const AV1_COMMON *const cm = &args->cpi->common;
Jingning Han's avatar
Jingning Han committed
527
528
529
530
531
532
533
  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;
Cheng Chen's avatar
Cheng Chen committed
534
  int dummy_rate_cost = 0;
Yushin Cho's avatar
Yushin Cho committed
535

536
  int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
537
538
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
539

540
541
  a = &args->ta[blk_col];
  l = &args->tl[blk_row];
Jingning Han's avatar
Jingning Han committed
542

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

546
  if (x->blk_skip[plane][blk_row * bw + blk_col] == 0) {
547
548
549
    if (args->enable_optimize_b) {
      av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize,
                      tx_size, AV1_XFORM_QUANT_FP);
550
      av1_optimize_b(args->cpi, x, plane, blk_row, blk_col, block, plane_bsize,
Cheng Chen's avatar
Cheng Chen committed
551
                     tx_size, a, l, CONFIG_LV_MAP, &dummy_rate_cost);
552
    } else {
553
554
555
      av1_xform_quant(
          cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
          USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP);
556
    }
557
  } else {
558
    p->eobs[block] = 0;
559
560
561
#if CONFIG_LV_MAP
    p->txb_entropy_ctx[block] = 0;
#endif
562
  }
563

564
565
  av1_set_txb_context(x, plane, block, tx_size, a, l);

566
567
  if (p->eobs[block]) {
    *(args->skip) = 0;
Jingning Han's avatar
Jingning Han committed
568

569
570
    TX_TYPE tx_type = av1_get_tx_type(pd->plane_type, xd, blk_row, blk_col,
                                      tx_size, cm->reduced_tx_set_used);
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
571
572
573
    av1_inverse_transform_block(xd, dqcoeff, plane, tx_type, tx_size, dst,
                                pd->dst.stride, p->eobs[block],
                                cm->reduced_tx_set_used);
574
  }
575
576

#if CONFIG_TXK_SEL
577
578
579
  uint8_t disable_txk_check =
      args->enable_optimize_b || args->cpi->oxcf.aq_mode != NO_AQ;

580
  if (plane == 0 && p->eobs[block] == 0) {
581
    if (disable_txk_check) {
582
583
584
585
586
587
588
      xd->mi[0]->mbmi.txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col] =
          DCT_DCT;
    } else {
      assert(
          xd->mi[0]->mbmi.txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col] ==
          DCT_DCT);
    }
589
590
591
  }
#endif  // CONFIG_TXK_SEL

Angie Chiang's avatar
Angie Chiang committed
592
593
594
#if CONFIG_MISMATCH_DEBUG
  if (dry_run == OUTPUT_ENABLED) {
    int pixel_c, pixel_r;
595
596
597
    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
598
599
600
    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,
601
602
                             blk_w, blk_h,
                             xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
Angie Chiang's avatar
Angie Chiang committed
603
604
  }
#endif
Jingning Han's avatar
Jingning Han committed
605
606
}

607
608
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
609
610
611
612
                               void *arg, int mi_row, int mi_col,
                               RUN_TYPE dry_run) {
  (void)mi_row;
  (void)mi_col;
613
614
615
616
617
  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;
  const struct macroblockd_plane *const pd = &xd->plane[plane];
618
619
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
620

621
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
622

623
  const TX_SIZE plane_tx_size =
624
      plane ? av1_get_uv_tx_size(mbmi, pd->subsampling_x, pd->subsampling_y)
625
626
            : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
                                                         blk_col)];
Debargha Mukherjee's avatar
Debargha Mukherjee committed
627

628
  if (tx_size == plane_tx_size || plane) {
Angie Chiang's avatar
Angie Chiang committed
629
630
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg,
                 mi_row, mi_col, dry_run);
631
  } else {
632
    assert(tx_size < TX_SIZES_ALL);
633
    const TX_SIZE sub_txs = sub_tx_size_map[1][tx_size];
634
635
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
636
    // This is the square transform block partition entry point.
637
638
639
640
    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);
641

642
643
644
645
    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;
646

647
        if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
648

649
        encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
Angie Chiang's avatar
Angie Chiang committed
650
                           arg, mi_row, mi_col, dry_run);
651
652
        block += step;
      }
653
654
655
656
    }
  }
}

Angie Chiang's avatar
Angie Chiang committed
657
658
659
660
661
typedef struct encode_block_pass1_args {
  AV1_COMMON *cm;
  MACROBLOCK *x;
} encode_block_pass1_args;

662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
static void av1_iwht4x4_add(const tran_low_t *input, uint8_t *dest, int stride,
                            const TxfmParam *txfm_param) {
  assert(av1_ext_tx_used[txfm_param->tx_set_type][txfm_param->tx_type]);
  const int eob = txfm_param->eob;
  if (eob > 1)
    aom_iwht4x4_16_add(input, dest, stride);
  else
    aom_iwht4x4_1_add(input, dest, stride);
}

static void av1_idct4x4_add(const tran_low_t *input, uint8_t *dest, int stride,
                            const TxfmParam *txfm_param) {
  assert(av1_ext_tx_used[txfm_param->tx_set_type][txfm_param->tx_type]);
  const int eob = txfm_param->eob;
  if (eob > 1)
    av1_iht4x4_16_add(input, dest, stride, txfm_param);
  else
    aom_idct4x4_1_add(input, dest, stride);
}

682
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
683
684
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
Angie Chiang's avatar
Angie Chiang committed
685
686
687
  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
688
689
690
691
  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);
692
  TxfmParam txfm_param;
Jingning Han's avatar
Jingning Han committed
693
  uint8_t *dst;
694
695
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
Jingning Han's avatar
Jingning Han committed
696

Angie Chiang's avatar
Angie Chiang committed
697
  av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
698
                  AV1_XFORM_QUANT_B);
699

Yushin Cho's avatar
Yushin Cho committed
700
  if (p->eobs[block] > 0) {
701
    txfm_param.bd = xd->bd;
702
    txfm_param.is_hbd = get_bitdepth_data_path_index(xd);
703
    txfm_param.tx_type = DCT_DCT;
704
    txfm_param.tx_size = tx_size;
705
706
    txfm_param.eob = p->eobs[block];
    txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
707
708
709
    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);
710
    if (txfm_param.is_hbd) {
711
      av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, pd->dst.stride, &txfm_param);
712
      return;
Jingning Han's avatar
Jingning Han committed
713
    }
Yaowu Xu's avatar
Yaowu Xu committed
714
    if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
715
      av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
716
    } else {
717
      av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
718
    }
Jingning Han's avatar
Jingning Han committed
719
720
721
  }
}

Angie Chiang's avatar
Angie Chiang committed
722
723
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
724
725
  av1_subtract_plane(x, bsize, 0);
  av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
Angie Chiang's avatar
Angie Chiang committed
726
                                         encode_block_pass1, &args);
Jingning Han's avatar
Jingning Han committed
727
728
}

729
void av1_encode_sb(const struct AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
730
                   int mi_row, int mi_col, RUN_TYPE dry_run) {
Angie Chiang's avatar
Angie Chiang committed
731
  (void)dry_run;
732
733
  const AV1_COMMON *const cm = &cpi->common;
  const int num_planes = av1_num_planes(cm);
Jingning Han's avatar
Jingning Han committed
734
735
736
  MACROBLOCKD *const xd = &x->e_mbd;
  struct optimize_ctx ctx;
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
737
738
  struct encode_b_args arg = { cpi,  x,    &ctx,       &mbmi->skip,
                               NULL, NULL, x->optimize };
Jingning Han's avatar
Jingning Han committed
739
740
741
742
  int plane;

  mbmi->skip = 1;

743
  if (x->skip) return;
Jingning Han's avatar
Jingning Han committed
744

745
  for (plane = 0; plane < num_planes; ++plane) {
746
747
748
749
750
    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))
751
      continue;
752

753
754
    const BLOCK_SIZE bsizec =
        scale_chroma_bsize(bsize, subsampling_x, subsampling_y);
755

756
757
    // TODO(jingning): Clean this up.
    const struct macroblockd_plane *const pd = &xd->plane[plane];
758
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsizec, pd);
759
760
    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];
761
    TX_SIZE max_tx_size = get_vartx_max_txsize(
762
        xd, plane_bsize, pd->subsampling_x || pd->subsampling_y);
763

764
    const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
765
766
    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];
767
768
    int idx, idy;
    int block = 0;
769
    int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
770
    av1_get_entropy_contexts(bsizec, 0, pd, ctx.ta[plane], ctx.tl[plane]);
771

772
    av1_subtract_plane(x, bsizec, plane);
Yushin Cho's avatar
Yushin Cho committed
773

774
775
    arg.ta = ctx.ta[plane];
    arg.tl = ctx.tl[plane];
776

777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
    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);
        const int unit_width = AOMMIN(mu_blocks_wide + idx, mi_width);
        for (blk_row = idy; blk_row < unit_height; blk_row += bh) {
          for (blk_col = idx; blk_col < unit_width; blk_col += bw) {
            encode_block_inter(plane, block, blk_row, blk_col, plane_bsize,
Angie Chiang's avatar
Angie Chiang committed
794
                               max_tx_size, &arg, mi_row, mi_col, dry_run);
795
796
797
            block += step;
          }
        }
798
799
      }
    }
Jingning Han's avatar
Jingning Han committed
800
801
802
  }
}

803
void av1_set_txb_context(MACROBLOCK *x, int plane, int block, TX_SIZE tx_size,
804
805
806
                         ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) {
  (void)tx_size;
  struct macroblock_plane *p = &x->plane[plane];
807

808
#if !CONFIG_LV_MAP
809
  *a = *l = p->eobs[block] > 0;
810
811
812
#else   // !CONFIG_LV_MAP
  *a = *l = p->txb_entropy_ctx[block];
#endif  // !CONFIG_LV_MAP
813
814
815
816
817

  int i;
  for (i = 0; i < tx_size_wide_unit[tx_size]; ++i) a[i] = a[0];

  for (i = 0; i < tx_size_high_unit[tx_size]; ++i) l[i] = l[0];
818
819
820
821
822
823
824
825
}

static void encode_block_intra_and_set_context(int plane, int block,
                                               int blk_row, int blk_col,
                                               BLOCK_SIZE plane_bsize,
                                               TX_SIZE tx_size, void *arg) {
  av1_encode_block_intra(plane, block, blk_row, blk_col, plane_bsize, tx_size,
                         arg);
Yushin Cho's avatar
Yushin Cho committed
826

827
828
829
830
  struct encode_b_args *const args = arg;
  MACROBLOCK *x = args->x;
  ENTROPY_CONTEXT *a = &args->ta[blk_col];
  ENTROPY_CONTEXT *l = &args->tl[blk_row];
831
  av1_set_txb_context(x, plane, block, tx_size, a, l);
832
833
}

Yaowu Xu's avatar
Yaowu Xu committed
834
835
836
void av1_encode_block_intra(int plane, int block, int blk_row, int blk_col,
                            BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                            void *arg) {
837
  struct encode_b_args *const args = arg;
838
  const AV1_COMMON *const cm = &args->cpi->common;
Jingning Han's avatar
Jingning Han committed
839
840
841
842
843
  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 *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
844
  PLANE_TYPE plane_type = get_plane_type(plane);
845
846
  const TX_TYPE tx_type = av1_get_tx_type(plane_type, xd, blk_row, blk_col,
                                          tx_size, cm->reduced_tx_set_used);
Jingning Han's avatar
Jingning Han committed
847
848
  uint16_t *eob = &p->eobs[block];
  const int dst_stride = pd->dst.stride;
Angie Chiang's avatar
Angie Chiang committed
849
850
  uint8_t *dst =
      &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
Cheng Chen's avatar
Cheng Chen committed
851
  int dummy_rate_cost = 0;
852

Luc Trudeau's avatar
Luc Trudeau committed
853
  av1_predict_intra_block_facade(cm, xd, plane, blk_col, blk_row, tx_size);
hui su's avatar
hui su committed
854

855
856
857
858
#if CONFIG_TXK_SEL
  const int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
  if (x->blk_skip[plane][blk_row * bw + blk_col] && plane == 0) {
    *eob = 0;
859
    p->txb_entropy_ctx[block] = 0;
860
861
862
    *(args->skip) = 0;
    assert(xd->mi[0]->mbmi.txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col] ==
           DCT_DCT);
863
864
865
866
867
868
869

#if CONFIG_CFL
    if (plane == AOM_PLANE_Y && xd->cfl.store_y &&
        is_cfl_allowed(&xd->mi[0]->mbmi)) {
      cfl_store_tx(xd, blk_row, blk_col, tx_size, plane_bsize);
    }
#endif  // CONFIG_CFL
870
871
872
873
    return;
  }
#endif

Angie Chiang's avatar
Angie Chiang committed
874
  av1_subtract_txb(x, plane, plane_bsize, blk_col, blk_row, tx_size);
Jingning Han's avatar
Jingning Han committed
875

876
877
  const ENTROPY_CONTEXT *a = &args->ta[blk_col];
  const ENTROPY_CONTEXT *l = &args->tl[blk_row];
878
  if (args->enable_optimize_b) {
Angie Chiang's avatar
Angie Chiang committed
879
    av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
880
                    AV1_XFORM_QUANT_FP);
881
    av1_optimize_b(args->cpi, x, plane, blk_row, blk_col, block, plane_bsize,
Cheng Chen's avatar
Cheng Chen committed
882
                   tx_size, a, l, CONFIG_LV_MAP, &dummy_rate_cost);
883
884
885

#if CONFIG_TXK_SEL
    if (plane == 0 && p->eobs[block] == 0) {
Angie Chiang's avatar
Angie Chiang committed
886
887
888
      assert(
          xd->mi[0]->mbmi.txk_type[(blk_row << MAX_MIB_SIZE_LOG2) + blk_col] ==
          DCT_DCT);
889
890
    }
#endif  // CONFIG_TXK_SEL
891
  } else {
892
893
894
    av1_xform_quant(
        cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
        USE_B_QUANT_NO_TRELLIS ? AV1_XFORM_QUANT_B : AV1_XFORM_QUANT_FP);
hui su's avatar
hui su committed
895
896