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

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

26
#include "av1/encoder/av1_quantize.h"
27
#include "av1/encoder/encodemb.h"
28
29
30
#if CONFIG_LV_MAP
#include "av1/encoder/encodetxb.h"
#endif
31
32
33
#include "av1/encoder/hybrid_fwd_txfm.h"
#include "av1/encoder/rd.h"
#include "av1/encoder/tokenize.h"
Jingning Han's avatar
Jingning Han committed
34

35
36
37
38
#if CONFIG_CFL
#include "av1/common/cfl.h"
#endif

39
40
41
// 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
42
43
44
45
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) {
46
#if !CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
47
48
  (void)xd;
#endif
Jingning Han's avatar
Jingning Han committed
49

Angie Chiang's avatar
Angie Chiang committed
50
  if (check_subtract_block_size(rows, cols)) {
51
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
52
53
54
    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);
55
56
      return;
    }
57
#endif  // CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
58
59
    aom_subtract_block_c(rows, cols, diff, diff_stride, src8, src_stride, pred8,
                         pred_stride);
60
61
62
63

    return;
  }

64
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
65
66
67
  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
68
69
    return;
  }
70
#endif  // CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
71
72
73
74
  aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
                     pred_stride);
}

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

107
#if !CONFIG_LV_MAP
108
109
110
111
112
113
114
115
116
117
118
// 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);
}

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

124
static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
125
                             int blk_row, int blk_col, int block,
126
                             TX_SIZE tx_size, int ctx, int fast_mode) {
127
128
129
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
130
131
132
133
134
  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));
  assert(eob <= tx_size_2d[tx_size]);
135
136
137
138
139
140
  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);
  const int16_t *const dequant_ptr = pd->dequant;
  const uint8_t *const band_translate = get_band_translate(tx_size);
141
  const TX_TYPE tx_type =
142
      av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
143
  const SCAN_ORDER *const scan_order =
Angie Chiang's avatar
Angie Chiang committed
144
      get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
145
146
  const int16_t *const scan = scan_order->scan;
  const int16_t *const nb = scan_order->neighbors;
147
  const int shift = av1_get_tx_scale(tx_size);
148
149
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
150
  // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
151
152
153
  const qm_val_t *iqmatrix = IS_2D_TRANSFORM(tx_type)
                                 ? pd->seg_iqmatrix[seg_id][tx_size]
                                 : cm->giqmatrix[NUM_QM_LEVELS - 1][0][tx_size];
154
155
156
157
158
159
160
#endif
#if CONFIG_NEW_QUANT
  int dq = get_dq_profile_from_ctx(mb->qindex, ctx, ref, plane_type);
  const dequant_val_type_nuq *dequant_val = pd->dequant_val_nuq[dq];
#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
164
165
166
  int(*head_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] =
      mb->token_head_costs[txsize_sqr_map[tx_size]][plane_type][ref];
  int(*tail_token_costs)[COEFF_CONTEXTS][TAIL_TOKENS] =
      mb->token_tail_costs[txsize_sqr_map[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
212
213
214
215
      // accu_error does not change when x==0
    } else {
      /*  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
221
      const int64_t d0 = (int64_t)dx0 * dx0;
      const int x_a = x - 2 * sz - 1;
      int dqv;
222
#if CONFIG_AOM_QM
223
      int iwt;
224
      dqv = dequant_ptr[rc != 0];
225
226
227
228
      if (iqmatrix != NULL) {
        iwt = iqmatrix[rc];
        dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
      }
229
230
231
232
#else
      dqv = dequant_ptr[rc != 0];
#endif

233
      int dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
234
      dx = signed_shift_right(dx, xd->bd - 8);
235
      const int64_t d2 = (int64_t)dx * dx;
236
237
238
239

      /* compute the distortion for the second candidate
       * x_a = x - 2 * sz + 1;
       */
240
      int64_t d2_a;
241
242
243
      if (x_a != 0) {
#if CONFIG_NEW_QUANT
        dx = av1_dequant_coeff_nuq(x, dqv, dequant_val[band_translate[i]]) -
Yaowu Xu's avatar
Yaowu Xu committed
244
             (coeff[rc] * (1 << shift));
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
245
        dx >>= xd->bd - 8;
246
#else   // CONFIG_NEW_QUANT
Sebastien Alaiwan's avatar
Sebastien Alaiwan committed
247
        dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
248
249
250
251
252
#endif  // CONFIG_NEW_QUANT
        d2_a = (int64_t)dx * dx;
      } else {
        d2_a = d0;
      }
253
      // Computing RD cost
hui su's avatar
hui su committed
254
      int64_t base_bits;
255
256
      // rate cost of x
      base_bits = av1_get_token_cost(x, &t0, cat6_bits);
hui su's avatar
hui su committed
257
258
259
260
      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]);
261
      // rate cost of x_a
262
      base_bits = av1_get_token_cost(x_a, &t1, cat6_bits);
hui su's avatar
hui su committed
263
264
265
266
267
268
269
270
      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]);
      }
271

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

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

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

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

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

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

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

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

  assert(final_eob <= eob);
  if (final_eob > 0) {
372
    assert(before_best_eob_qc != 0);
373
374
    i = final_eob - 1;
    int rc = scan[i];
375
376
    qcoeff[rc] = before_best_eob_qc;
    dqcoeff[rc] = before_best_eob_dqc;
377
378
379
380
381
382
383
384
  }

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

385
  p->eobs[block] = final_eob;
386
387
  return final_eob;
}
388
#endif  // !CONFIG_LV_MAP
389

Jingning Han's avatar
Jingning Han committed
390
391
392
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,
393
                   const ENTROPY_CONTEXT *l, int fast_mode) {
394
395
396
397
398
399
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  const int eob = p->eobs[block];
  assert((mb->qindex == 0) ^ (xd->lossless[xd->mi[0]->mbmi.segment_id] == 0));
  if (eob == 0) return eob;
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return eob;
Jingning Han's avatar
Jingning Han committed
400

401
#if !CONFIG_LV_MAP
402
  (void)plane_bsize;
Jingning Han's avatar
Jingning Han committed
403
404
  (void)blk_row;
  (void)blk_col;
405
  int ctx = get_entropy_context(tx_size, a, l);
406
407
  return optimize_b_greedy(cm, mb, plane, blk_row, blk_col, block, tx_size, ctx,
                           fast_mode);
408
409
410
#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
411
  return av1_optimize_txb(cm, mb, plane, blk_row, blk_col, block, tx_size,
412
                          &txb_ctx, fast_mode);
413
#endif  // !CONFIG_LV_MAP
414
}
415

Angie Chiang's avatar
Angie Chiang committed
416
417
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
418
  QUANT_FUNC_HIGHBD = 1,
419
  QUANT_FUNC_TYPES = 2
Angie Chiang's avatar
Angie Chiang committed
420
421
} QUANT_FUNC;

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

Yushin Cho's avatar
Yushin Cho committed
436
#if !CONFIG_TXMG
437
typedef void (*fwdTxfmFunc)(const int16_t *diff, tran_low_t *coeff, int stride,
438
                            TxfmParam *txfm_param);
439
440
static const fwdTxfmFunc fwd_txfm_func[2] = { av1_fwd_txfm,
                                              av1_highbd_fwd_txfm };
Angie Chiang's avatar
Angie Chiang committed
441
#endif
442

Angie Chiang's avatar
Angie Chiang committed
443
444
void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
                     int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
445
446
                     TX_SIZE tx_size, int ctx,
                     AV1_XFORM_QUANT xform_quant_idx) {
Jingning Han's avatar
Jingning Han committed
447
  MACROBLOCKD *const xd = &x->e_mbd;
448
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yushin Cho's avatar
Yushin Cho committed
449
#if !CONFIG_DIST_8X8
Jingning Han's avatar
Jingning Han committed
450
451
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
452
453
454
455
#else
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
#endif
456
  PLANE_TYPE plane_type = get_plane_type(plane);
457
458
  TX_TYPE tx_type =
      av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
Angie Chiang's avatar
Angie Chiang committed
459

460
#if CONFIG_NEW_QUANT
461
  const int is_inter = is_inter_block(mbmi);
Angie Chiang's avatar
Angie Chiang committed
462
463
#endif
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
Jingning Han's avatar
Jingning Han committed
464
465
466
467
  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];
468
  const int diff_stride = block_size_wide[plane_bsize];
Yushin Cho's avatar
Yushin Cho committed
469
#if CONFIG_AOM_QM
470
  int seg_id = mbmi->segment_id;
471
  // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
472
473
474
475
476
477
  const qm_val_t *qmatrix = IS_2D_TRANSFORM(tx_type)
                                ? pd->seg_qmatrix[seg_id][tx_size]
                                : cm->gqmatrix[NUM_QM_LEVELS - 1][0][tx_size];
  const qm_val_t *iqmatrix = IS_2D_TRANSFORM(tx_type)
                                 ? pd->seg_iqmatrix[seg_id][tx_size]
                                 : cm->giqmatrix[NUM_QM_LEVELS - 1][0][tx_size];
478
#endif
Angie Chiang's avatar
Angie Chiang committed
479

480
  TxfmParam txfm_param;
481

Yushin Cho's avatar
Yushin Cho committed
482
#if CONFIG_DIST_8X8 || CONFIG_LGT_FROM_PRED || CONFIG_MRC_TX
Yushin Cho's avatar
Yushin Cho committed
483
484
  uint8_t *dst;
  const int dst_stride = pd->dst.stride;
Yushin Cho's avatar
Yushin Cho committed
485
#if CONFIG_DIST_8X8
Lester Lu's avatar
Lester Lu committed
486
  int16_t *pred;
487
488
  const int txw = tx_size_wide[tx_size];
  const int txh = tx_size_high[tx_size];
Yushin Cho's avatar
Yushin Cho committed
489
490
  int i, j;
#endif
Lester Lu's avatar
Lester Lu committed
491
#endif
Yushin Cho's avatar
Yushin Cho committed
492

493
  const int tx2d_size = tx_size_2d[tx_size];
494
  QUANT_PARAM qparam;
495
496
  const int16_t *src_diff;

497
498
  src_diff =
      &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
499
  qparam.log_scale = av1_get_tx_scale(tx_size);
500
501
502
503
504
505
506
507
#if CONFIG_NEW_QUANT
  qparam.tx_size = tx_size;
  qparam.dq = get_dq_profile_from_ctx(x->qindex, ctx, is_inter, plane_type);
#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
508

Yushin Cho's avatar
Yushin Cho committed
509
#if CONFIG_DIST_8X8 || CONFIG_LGT_FROM_PRED || CONFIG_MRC_TX
Yushin Cho's avatar
Yushin Cho committed
510
  dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
Yushin Cho's avatar
Yushin Cho committed
511
#endif  // CONFIG_DIST_8X8 || CONFIG_LGT_FROM_PRED ||
512
        // CONFIG_MRC_TX
513
514

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

Thomas Daede's avatar
Thomas Daede committed
518
519
// copy uint8 orig and predicted block to int16 buffer
// in order to use existing VP10 transform functions
520
#if CONFIG_HIGHBITDEPTH
521
522
523
524
525
526
    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 {
527
#endif  // CONFIG_HIGHBITDEPTH
528
529
530
      for (j = 0; j < txh; j++)
        for (i = 0; i < txw; i++)
          pred[diff_stride * j + i] = dst[dst_stride * j + i];
531
#if CONFIG_HIGHBITDEPTH
532
    }
533
#endif  // CONFIG_HIGHBITDEPTH
534
  }
Yushin Cho's avatar
Yushin Cho committed
535
#endif  // CONFIG_DIST_8X8
Yushin Cho's avatar
Yushin Cho committed
536

537
  (void)ctx;
538

539
540
541
  txfm_param.tx_type = tx_type;
  txfm_param.tx_size = tx_size;
  txfm_param.lossless = xd->lossless[mbmi->segment_id];
542
543
544
  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);
545
#if CONFIG_MRC_TX || CONFIG_LGT
546
  txfm_param.is_inter = is_inter_block(mbmi);
547
548
#endif
#if CONFIG_MRC_TX || CONFIG_LGT_FROM_PRED
549
550
  txfm_param.dst = dst;
  txfm_param.stride = dst_stride;
551
552
#if CONFIG_MRC_TX
  txfm_param.valid_mask = &mbmi->valid_mrc_mask;
Sarah Parker's avatar
Sarah Parker committed
553
554
555
#if SIGNAL_ANY_MRC_MASK
  txfm_param.mask = BLOCK_OFFSET(xd->mrc_mask, block);
#endif  // SIGNAL_ANY_MRC_MASK
556
#endif  // CONFIG_MRC_TX
557
#if CONFIG_LGT_FROM_PRED
Lester Lu's avatar
Lester Lu committed
558
  txfm_param.mode = mbmi->mode;
559
560
561
  txfm_param.use_lgt = mbmi->use_lgt;
#endif  // CONFIG_LGT_FROM_PRED
#endif  // CONFIG_MRC_TX || CONFIG_LGT_FROM_PRED
Angie Chiang's avatar
Angie Chiang committed
562

563
  txfm_param.bd = xd->bd;
564
  const int is_hbd = get_bitdepth_data_path_index(xd);
Angie Chiang's avatar
Angie Chiang committed
565
566
567
568

#if CONFIG_TXMG
  av1_highbd_fwd_txfm(src_diff, coeff, diff_stride, &txfm_param);
#else   // CONFIG_TXMG
569
  fwd_txfm_func[is_hbd](src_diff, coeff, diff_stride, &txfm_param);
Angie Chiang's avatar
Angie Chiang committed
570
#endif  // CONFIG_TXMG
571

Yaowu Xu's avatar
Yaowu Xu committed
572
  if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
573
    if (LIKELY(!x->skip_block)) {
574
      quant_func_list[xform_quant_idx][is_hbd](
575
          coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
576
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
577
      av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
Jingning Han's avatar
Jingning Han committed
578
579
    }
  }
580
581
582
583
#if CONFIG_LV_MAP
  p->txb_entropy_ctx[block] =
      (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
#endif  // CONFIG_LV_MAP
584
  return;
Jingning Han's avatar
Jingning Han committed
585
586
}

587
static void encode_block(int plane, int block, int blk_row, int blk_col,
588
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
Jingning Han's avatar
Jingning Han committed
589
  struct encode_b_args *const args = arg;
Angie Chiang's avatar
Angie Chiang committed
590
  AV1_COMMON *cm = args->cm;
Jingning Han's avatar
Jingning Han committed
591
592
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
593
  int ctx;
Jingning Han's avatar
Jingning Han committed
594
595
596
  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);
Sarah Parker's avatar
Sarah Parker committed
597
598
599
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
  uint8_t *mrc_mask = BLOCK_OFFSET(xd->mrc_mask, block);
#endif  // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
Jingning Han's avatar
Jingning Han committed
600
601
  uint8_t *dst;
  ENTROPY_CONTEXT *a, *l;
Yushin Cho's avatar
Yushin Cho committed
602

603
  int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
604
605
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
606

607
608
  a = &args->ta[blk_col];
  l = &args->tl[blk_row];
609
  ctx = get_entropy_context(tx_size, a, l);
Jingning Han's avatar
Jingning Han committed
610

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

614
  if (x->blk_skip[plane][blk_row * bw + blk_col] == 0) {
Angie Chiang's avatar
Angie Chiang committed
615
    av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
616
                    ctx, AV1_XFORM_QUANT_FP);
617
  } else {
618
    p->eobs[block] = 0;
619
  }
620

Jingning Han's avatar
Jingning Han committed
621
  av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size, a,
622
                 l, CONFIG_LV_MAP);
623

624
625
  av1_set_txb_context(x, plane, block, tx_size, a, l);

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

628
  if (p->eobs[block] != 0)
629

630
  {
631
#if CONFIG_LGT_FROM_PRED
632
    PREDICTION_MODE mode = xd->mi[0]->mbmi.mode;
633
#endif  // CONFIG_LGT_FROM_PRED
634
635
636
    TX_TYPE tx_type =
        av1_get_tx_type(pd->plane_type, xd, blk_row, blk_col, block, tx_size);
    av1_inverse_transform_block(xd, dqcoeff,
637
#if CONFIG_LGT_FROM_PRED
638
                                mode,
639
#endif
Sarah Parker's avatar
Sarah Parker committed
640
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
641
                                mrc_mask,
Sarah Parker's avatar
Sarah Parker committed
642
#endif  // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
643
                                plane, tx_type, tx_size, dst, pd->dst.stride,
644
645
                                p->eobs[block]);
  }
Jingning Han's avatar
Jingning Han committed
646
647
}

648
649
650
651
652
653
654
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;
655
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
656
  const struct macroblockd_plane *const pd = &xd->plane[plane];
657
658
  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
659
  TX_SIZE plane_tx_size;
660
661
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
662

663
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
664

665
666
667
  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
668

669
  if (tx_size == plane_tx_size) {
670
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
671
  } else {
672
    assert(tx_size < TX_SIZES_ALL);
Yue Chen's avatar
Yue Chen committed
673
674
675
676
677
#if CONFIG_RECT_TX_EXT
    int is_qttx = plane_tx_size == quarter_txsize_lookup[plane_bsize];
    const TX_SIZE sub_txs = is_qttx ? plane_tx_size : sub_tx_size_map[tx_size];
    if (is_qttx) assert(blk_row == 0 && blk_col == 0 && block == 0);
#else
678
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
679
680
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
Yue Chen's avatar
Yue Chen committed
681
#endif
682
683
    // This is the square transform block partition entry point.
    int bsl = tx_size_wide_unit[sub_txs];
684
685
    int i;
    assert(bsl > 0);
686

687
    for (i = 0; i < 4; ++i) {
Yue Chen's avatar
Yue Chen committed
688
689
690
691
692
693
694
695
696
#if CONFIG_RECT_TX_EXT
      int is_wide_tx = tx_size_wide_unit[sub_txs] > tx_size_high_unit[sub_txs];
      const int offsetr =
          is_qttx ? (is_wide_tx ? i * tx_size_high_unit[sub_txs] : 0)
                  : blk_row + ((i >> 1) * bsl);
      const int offsetc =
          is_qttx ? (is_wide_tx ? 0 : i * tx_size_wide_unit[sub_txs])
                  : blk_col + ((i & 0x01) * bsl);
#else
697
698
      const int offsetr = blk_row + ((i >> 1) * bsl);
      const int offsetc = blk_col + ((i & 0x01) * bsl);
Yue Chen's avatar
Yue Chen committed
699
#endif
700
      int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
701

702
      if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
703

704
705
706
      encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
                         arg);
      block += step;
707
708
709
710
    }
  }
}

Angie Chiang's avatar
Angie Chiang committed
711
712
713
714
715
typedef struct encode_block_pass1_args {
  AV1_COMMON *cm;
  MACROBLOCK *x;
} encode_block_pass1_args;

716
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
717
718
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
Angie Chiang's avatar
Angie Chiang committed
719
720
721
  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
722
723
724
725
  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);
726
  TxfmParam txfm_param;
Jingning Han's avatar
Jingning Han committed
727
  uint8_t *dst;
728
  int ctx = 0;
729
730
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
Jingning Han's avatar
Jingning Han committed
731

Angie Chiang's avatar
Angie Chiang committed
732
  av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
733
                  ctx, AV1_XFORM_QUANT_B);
734

Yushin Cho's avatar
Yushin Cho committed
735
  if (p->eobs[block] > 0) {
736
737
    txfm_param.bd = xd->bd;
    txfm_param.tx_type = DCT_DCT;
738
    txfm_param.tx_size = tx_size;
739
740
    txfm_param.eob = p->eobs[block];
    txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
741
742
743
    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);
744
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
745
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
746
      av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, pd->dst.stride, &txfm_param);
747
      return;
Jingning Han's avatar
Jingning Han committed
748
    }
749
#endif  //  CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
750
    if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
751
      av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
752
    } else {
753
      av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
754
    }
Jingning Han's avatar
Jingning Han committed
755
756
757
  }
}

Angie Chiang's avatar
Angie Chiang committed
758
759
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
760
761
  av1_subtract_plane(x, bsize, 0);
  av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
Angie Chiang's avatar
Angie Chiang committed
762
                                         encode_block_pass1, &args);
Jingning Han's avatar
Jingning Han committed
763
764
}

Yaowu Xu's avatar
Yaowu Xu committed
765
766
void av1_encode_sb(AV1_COMMON *cm, MACROBLOCK *x, BLOCK_SIZE bsize, int mi_row,
                   int mi_col) {
Jingning Han's avatar
Jingning Han committed
767
768
769
  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
770
  struct encode_b_args arg = { cm, x, &ctx, &mbmi->skip, NULL, NULL, 1 };
Jingning Han's avatar
Jingning Han committed
771
772
773
774
  int plane;

  mbmi->skip = 1;

775
  if (x->skip) return;
Jingning Han's avatar
Jingning Han committed
776
777

  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
778
779
780
781
782
    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))
783
      continue;
784
785

    bsize = scale_chroma_bsize(bsize, subsampling_x, subsampling_y);
786

787
788
789
    // TODO(jingning): Clean this up.
    const struct macroblockd_plane *const pd = &xd->plane[plane];
    const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
790
791
    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];
792
793
    const TX_SIZE max_tx_size = get_vartx_max_txsize(
        mbmi, plane_bsize, pd->subsampling_x || pd->subsampling_y);
794
    const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
795
796
    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];
797
798
    int idx, idy;
    int block = 0;
799
    int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
800
    av1_get_entropy_contexts(bsize, 0, pd, ctx.ta[plane], ctx.tl[plane]);
801

Yaowu Xu's avatar
Yaowu Xu committed
802
    av1_subtract_plane(x, bsize, plane);
Yushin Cho's avatar
Yushin Cho committed
803

804
805
    arg.ta = ctx.ta[plane];
    arg.tl = ctx.tl[plane];
806

807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
    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,
                               max_tx_size, &arg);
            block += step;
          }
        }
828
829
      }
    }
Jingning Han's avatar
Jingning Han committed
830
831
832
  }
}

833
void av1_set_txb_context(MACROBLOCK *x, int plane, int block, TX_SIZE tx_size,
834
835
836
                         ENTROPY_CONTEXT *a, ENTROPY_CONTEXT *l) {
  (void)tx_size;
  struct macroblock_plane *p = &x->plane[plane];
837

838
#if !CONFIG_LV_MAP
839
  *a = *l = p->eobs[block] > 0;
840
841
842
#else   // !CONFIG_LV_MAP
  *a = *l = p->txb_entropy_ctx[block];
#endif  // !CONFIG_LV_MAP
843
844
845
846
847

  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];
848
849
850
851
852
853
854
855
}

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
856

857
858
859
860
  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];
861
  av1_set_txb_context(x, plane, block, tx_size, a, l);
862
863
}

Yaowu Xu's avatar
Yaowu Xu committed
864
865
866
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) {
867
  struct encode_b_args *const args = arg;
Angie Chiang's avatar
Angie Chiang committed
868
  AV1_COMMON *cm = args->cm;
Jingning Han's avatar
Jingning Han committed
869
870
871
872
873
  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);
Sarah Parker's avatar
Sarah Parker committed
874
875
876
#if CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
  uint8_t *mrc_mask = BLOCK_OFFSET(xd->mrc_mask, block);
#endif  // CONFIG_MRC_TX && SIGNAL_ANY_MRC_MASK
877
  PLANE_TYPE plane_type = get_plane_type(plane);
878
879
  const TX_TYPE tx_type =
      av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
Jingning Han's avatar
Jingning Han committed
880
881
  uint16_t *eob = &p->eobs[block];
  const int dst_stride = pd->dst.stride;
Angie Chiang's avatar
Angie Chiang committed
882
883
  uint8_t *dst =
      &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
884

885
886
  av1_predict_intra_block_facade(cm, xd, plane, block, blk_col, blk_row,
                                 tx_size);
hui su's avatar
hui su committed
887

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

890
891
  const ENTROPY_CONTEXT *a = &args->ta[blk_col];
  const ENTROPY_CONTEXT *l = &args->tl[blk_row];
892
  int ctx = combine_entropy_contexts(*a, *l);
893
  if (args->enable_optimize_b) {
Angie Chiang's avatar
Angie Chiang committed
894
    av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,