encodemb.c 57.1 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
39
40
#if CONFIG_PVQ
#include "av1/encoder/encint.h"
#include "av1/common/partition.h"
#include "av1/encoder/pvq_encoder.h"
#endif

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

45
46
47
// 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
48
49
50
51
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) {
52
#if !CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
53
54
  (void)xd;
#endif
Jingning Han's avatar
Jingning Han committed
55

Angie Chiang's avatar
Angie Chiang committed
56
  if (check_subtract_block_size(rows, cols)) {
57
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
58
59
60
    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);
61
62
      return;
    }
63
#endif  // CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
64
65
    aom_subtract_block_c(rows, cols, diff, diff_stride, src8, src_stride, pred8,
                         pred_stride);
66
67
68
69

    return;
  }

70
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
71
72
73
  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
74
75
    return;
  }
76
#endif  // CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
77
78
79
80
  aom_subtract_block(rows, cols, diff, diff_stride, src8, src_stride, pred8,
                     pred_stride);
}

Angie Chiang's avatar
Angie Chiang committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
101
102
103
104
105
106
107
108
109
110
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
111
112
}

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

118
#if !CONFIG_LV_MAP
119
static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
120
121
                             int blk_row, int blk_col, int block,
                             TX_SIZE tx_size, int ctx) {
122
123
124
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
125
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));
  assert(eob <= tx_size_2d[tx_size]);
130
131
132
133
134
135
  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);
136
  const TX_TYPE tx_type =
137
      av1_get_tx_type(plane_type, xd, blk_row, blk_col, block, tx_size);
138
  const SCAN_ORDER *const scan_order =
Angie Chiang's avatar
Angie Chiang committed
139
      get_scan(cm, tx_size, tx_type, &xd->mi[0]->mbmi);
140
141
  const int16_t *const scan = scan_order->scan;
  const int16_t *const nb = scan_order->neighbors;
142
  const int shift = av1_get_tx_scale(tx_size);
143
144
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
145
146
147
148
149
  // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
  const qm_val_t *iqmatrix =
      IS_2D_TRANSFORM(tx_type)
          ? pd->seg_iqmatrix[seg_id][!ref][tx_size]
          : cm->giqmatrix[NUM_QM_LEVELS - 1][0][0][tx_size];
150
151
152
153
154
155
156
#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;
157
  int i, final_eob = 0;
158
  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
hui su's avatar
hui su committed
159
160
161
162
  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];
163
  const int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;
164
  int64_t rate0, rate1;
165
166
167
168
169
  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];
170
171
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
172
    token_cache[rc] = av1_pt_energy_class[av1_get_token(qcoeff[rc])];
173
174
175
176
  }

  /* Record the r-d cost */
  int64_t accu_rate = 0;
177
178
179
  // 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;
180
  rate0 = head_token_costs[0][ctx][0];
181
  int64_t best_block_rd_cost = RDCOST(rdmult, rate0, accu_error);
182
183

  // int64_t best_block_rd_cost_all0 = best_block_rd_cost;
hui su's avatar
hui su committed
184
185
  const int seg_eob =
      av1_get_tx_eob(&cm->seg, xd->mi[0]->mbmi.segment_id, tx_size);
186
187
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
188
189
190
191
    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);
hui su's avatar
hui su committed
192
193
194
    const int eob_val =
        (i + 1 == eob) ? (i + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
    const int is_first = (i == 0);
195
196
197

    if (x == 0) {
      // no need to search when x == 0
198
199
200
      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]);
201
202
203
204
205
206
      // 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.
207
      int dx0 = abs(coeff[rc]) * (1 << shift);
208
209
210
211
212
#if CONFIG_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx0 >>= xd->bd - 8;
      }
#endif
213
214
215
      const int64_t d0 = (int64_t)dx0 * dx0;
      const int x_a = x - 2 * sz - 1;
      int dqv;
216
217
218
219
220
221
222
223
#if CONFIG_AOM_QM
      int iwt = iqmatrix[rc];
      dqv = dequant_ptr[rc != 0];
      dqv = ((iwt * (int)dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS;
#else
      dqv = dequant_ptr[rc != 0];
#endif

224
      int dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
225
226
#if CONFIG_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
227
228
229
        int dx_sign = dx < 0 ? 1 : 0;
        dx = abs(dx) >> (xd->bd - 8);
        if (dx_sign) dx = -dx;
230
231
      }
#endif  // CONFIG_HIGHBITDEPTH
232
      const int64_t d2 = (int64_t)dx * dx;
233
234
235
236

      /* compute the distortion for the second candidate
       * x_a = x - 2 * sz + 1;
       */
237
      int64_t d2_a;
238
239
240
      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
241
             (coeff[rc] * (1 << shift));
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#if CONFIG_HIGHBITDEPTH
        if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
          dx >>= xd->bd - 8;
        }
#endif  // CONFIG_HIGHBITDEPTH
#else   // CONFIG_NEW_QUANT
#if CONFIG_HIGHBITDEPTH
        if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
          dx -= ((dqv >> (xd->bd - 8)) + sz) ^ sz;
        } else {
          dx -= (dqv + sz) ^ sz;
        }
#else
        dx -= (dqv + sz) ^ sz;
#endif  // CONFIG_HIGHBITDEPTH
#endif  // CONFIG_NEW_QUANT
        d2_a = (int64_t)dx * dx;
      } else {
        d2_a = d0;
      }
262
      // Computing RD cost
hui su's avatar
hui su committed
263
      int64_t base_bits;
264
265
      // rate cost of x
      base_bits = av1_get_token_cost(x, &t0, cat6_bits);
hui su's avatar
hui su committed
266
267
268
269
      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]);
270
      // rate cost of x_a
271
      base_bits = av1_get_token_cost(x_a, &t1, cat6_bits);
hui su's avatar
hui su committed
272
273
274
275
276
277
278
279
      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]);
      }
280

hui su's avatar
hui su committed
281
      int64_t next_bits0 = 0, next_bits1 = 0;
282
      if (i < eob - 1) {
hui su's avatar
hui su committed
283
        int ctx_next;
284
285
        const int band_next = band_translate[i + 1];
        const int token_next = av1_get_token(qcoeff[scan[i + 1]]);
hui su's avatar
hui su committed
286
287
        const int eob_val_next =
            (i + 2 == eob) ? (i + 2 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
288
289
290

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

        token_cache[rc] = av1_pt_energy_class[t1];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
297
298
299
        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]);
300
301
      }

302
303
      rd_cost0 = RDCOST(rdmult, (rate0 + next_bits0), d2);
      rd_cost1 = RDCOST(rdmult, (rate1 + next_bits1), d2_a);
304
      const int best_x = (rd_cost1 < rd_cost0);
305

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

326
327
328
      const int dqc = dqcoeff[rc];
      int dqc_a = 0;
      if (best_x || best_eob_x) {
329
330
331
332
333
334
335
        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
336
337
          if (x_a < 0)
            dqc_a = -((-x_a * dqv) >> shift);
338
          else
339
            dqc_a = (x_a * dqv) >> shift;
340
341
342
343
344
345
346
347
#endif  // CONFIG_NEW_QUANT
        } else {
          dqc_a = 0;
        }  // if (x_a != 0)
      }

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

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

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

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

394
  p->eobs[block] = final_eob;
395
396
  return final_eob;
}
397
#endif  // !CONFIG_LV_MAP
398

Jingning Han's avatar
Jingning Han committed
399
400
401
402
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,
                   const ENTROPY_CONTEXT *l) {
403
404
405
406
407
408
  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
409

410
#if CONFIG_PVQ
411
412
  (void)cm;
  (void)tx_size;
413
414
  (void)a;
  (void)l;
415
416
  return eob;
#endif
Jingning Han's avatar
Jingning Han committed
417

418
#if !CONFIG_LV_MAP
419
  (void)plane_bsize;
Jingning Han's avatar
Jingning Han committed
420
421
  (void)blk_row;
  (void)blk_col;
422
423
424
425
#if CONFIG_VAR_TX
  int ctx = get_entropy_context(tx_size, a, l);
#else
  int ctx = combine_entropy_contexts(*a, *l);
426
#endif  // CONFIG_VAR_TX
427
428
  return optimize_b_greedy(cm, mb, plane, blk_row, blk_col, block, tx_size,
                           ctx);
429
430
431
#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
432
433
  return av1_optimize_txb(cm, mb, plane, blk_row, blk_col, block, tx_size,
                          &txb_ctx);
434
#endif  // !CONFIG_LV_MAP
435
}
436

Thomas Daede's avatar
Thomas Daede committed
437
#if !CONFIG_PVQ
Angie Chiang's avatar
Angie Chiang committed
438
439
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
440
  QUANT_FUNC_HIGHBD = 1,
441
  QUANT_FUNC_TYPES = 2
Angie Chiang's avatar
Angie Chiang committed
442
443
} QUANT_FUNC;

444
445
static AV1_QUANT_FACADE
    quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
446
#if !CONFIG_NEW_QUANT
447
      { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
Yaowu Xu's avatar
Yaowu Xu committed
448
449
      { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
      { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
450
#else   // !CONFIG_NEW_QUANT
451
452
453
      { 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 },
454
#endif  // !CONFIG_NEW_QUANT
455
456
      { NULL, NULL }
    };
457
#endif  // !CONFIG_PVQ
458

Angie Chiang's avatar
Angie Chiang committed
459
#if !CONFIG_TXMG
460
typedef void (*fwdTxfmFunc)(const int16_t *diff, tran_low_t *coeff, int stride,
461
                            TxfmParam *txfm_param);
462
463
static const fwdTxfmFunc fwd_txfm_func[2] = { av1_fwd_txfm,
                                              av1_highbd_fwd_txfm };
Angie Chiang's avatar
Angie Chiang committed
464
#endif
465

Angie Chiang's avatar
Angie Chiang committed
466
467
void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
                     int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
468
469
                     TX_SIZE tx_size, int ctx,
                     AV1_XFORM_QUANT xform_quant_idx) {
Jingning Han's avatar
Jingning Han committed
470
  MACROBLOCKD *const xd = &x->e_mbd;
471
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yushin Cho's avatar
Yushin Cho committed
472
#if !(CONFIG_PVQ || CONFIG_DIST_8X8)
Jingning Han's avatar
Jingning Han committed
473
474
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
475
476
477
478
#else
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
#endif
479
  PLANE_TYPE plane_type = get_plane_type(plane);
480
481
  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
482
483

#if CONFIG_AOM_QM || CONFIG_NEW_QUANT
484
  const int is_inter = is_inter_block(mbmi);
Angie Chiang's avatar
Angie Chiang committed
485
486
487
#endif

  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, mbmi);
Jingning Han's avatar
Jingning Han committed
488
489
490
491
  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];
492
  const int diff_stride = block_size_wide[plane_bsize];
493
#if CONFIG_AOM_QM
494
  int seg_id = mbmi->segment_id;
495
496
497
498
499
500
501
502
  // Use a flat matrix (i.e. no weighting) for 1D and Identity transforms
  const qm_val_t *qmatrix =
      IS_2D_TRANSFORM(tx_type) ? pd->seg_qmatrix[seg_id][!is_inter][tx_size]
                               : cm->gqmatrix[NUM_QM_LEVELS - 1][0][0][tx_size];
  const qm_val_t *iqmatrix =
      IS_2D_TRANSFORM(tx_type)
          ? pd->seg_iqmatrix[seg_id][!is_inter][tx_size]
          : cm->giqmatrix[NUM_QM_LEVELS - 1][0][0][tx_size];
503
#endif
Angie Chiang's avatar
Angie Chiang committed
504

505
  TxfmParam txfm_param;
506

507
#if CONFIG_PVQ || CONFIG_DIST_8X8 || CONFIG_LGT || CONFIG_MRC_TX
Yushin Cho's avatar
Yushin Cho committed
508
509
  uint8_t *dst;
  const int dst_stride = pd->dst.stride;
Yushin Cho's avatar
Yushin Cho committed
510
#if CONFIG_PVQ || CONFIG_DIST_8X8
Lester Lu's avatar
Lester Lu committed
511
  int16_t *pred;
512
513
  const int txw = tx_size_wide[tx_size];
  const int txh = tx_size_high[tx_size];
Yushin Cho's avatar
Yushin Cho committed
514
515
  int i, j;
#endif
Lester Lu's avatar
Lester Lu committed
516
#endif
Yushin Cho's avatar
Yushin Cho committed
517

518
519
#if !CONFIG_PVQ
  const int tx2d_size = tx_size_2d[tx_size];
520
  QUANT_PARAM qparam;
521
522
  const int16_t *src_diff;

523
524
  src_diff =
      &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
525
  qparam.log_scale = av1_get_tx_scale(tx_size);
526
527
528
529
530
531
532
533
#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
534
535
536
537
#else
  tran_low_t *ref_coeff = BLOCK_OFFSET(pd->pvq_ref_coeff, block);
  int skip = 1;
  PVQ_INFO *pvq_info = NULL;
Yushin Cho's avatar
Yushin Cho committed
538
539
540
  uint8_t *src;
  int16_t *src_int16;
  const int src_stride = p->src.stride;
541

542
  (void)ctx;
543
544
545
546
547
548
549
  (void)scan_order;
  (void)qcoeff;

  if (x->pvq_coded) {
    assert(block < MAX_PVQ_BLOCKS_IN_SB);
    pvq_info = &x->pvq[block][plane];
  }
550
551
552
  src = &p->src.buf[(blk_row * src_stride + blk_col) << tx_size_wide_log2[0]];
  src_int16 =
      &p->src_int16[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
Yushin Cho's avatar
Yushin Cho committed
553

554
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
555
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
556
557
    for (j = 0; j < txh; j++)
      for (i = 0; i < txw; i++)
Thomas Daede's avatar
Thomas Daede committed
558
559
560
        src_int16[diff_stride * j + i] =
            CONVERT_TO_SHORTPTR(src)[src_stride * j + i];
  } else {
561
#endif  // CONFIG_HIGHBITDEPTH
562
563
    for (j = 0; j < txh; j++)
      for (i = 0; i < txw; i++)
Thomas Daede's avatar
Thomas Daede committed
564
        src_int16[diff_stride * j + i] = src[src_stride * j + i];
565
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
566
  }
567
#endif  // CONFIG_HIGHBITDEPTH
Yushin Cho's avatar
Yushin Cho committed
568
569
#endif

570
#if CONFIG_PVQ || CONFIG_DIST_8X8 || CONFIG_LGT || CONFIG_MRC_TX
Yushin Cho's avatar
Yushin Cho committed
571
  dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
Yushin Cho's avatar
Yushin Cho committed
572
#if CONFIG_PVQ || CONFIG_DIST_8X8
573
  pred = &pd->pred[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
574

Thomas Daede's avatar
Thomas Daede committed
575
576
// copy uint8 orig and predicted block to int16 buffer
// in order to use existing VP10 transform functions
577
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
578
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
579
580
    for (j = 0; j < txh; j++)
      for (i = 0; i < txw; i++)
Thomas Daede's avatar
Thomas Daede committed
581
582
583
        pred[diff_stride * j + i] =
            CONVERT_TO_SHORTPTR(dst)[dst_stride * j + i];
  } else {
584
#endif  // CONFIG_HIGHBITDEPTH
585
586
    for (j = 0; j < txh; j++)
      for (i = 0; i < txw; i++)
Thomas Daede's avatar
Thomas Daede committed
587
        pred[diff_stride * j + i] = dst[dst_stride * j + i];
588
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
589
  }
590
#endif  // CONFIG_HIGHBITDEPTH
Yushin Cho's avatar
Yushin Cho committed
591
#endif  // CONFIG_PVQ || CONFIG_DIST_8X8
592
#endif  // CONFIG_PVQ || CONFIG_DIST_8X8 || CONFIG_LGT || CONFIG_MRC_TX
Yushin Cho's avatar
Yushin Cho committed
593

594
  (void)ctx;
595

596
597
598
  txfm_param.tx_type = tx_type;
  txfm_param.tx_size = tx_size;
  txfm_param.lossless = xd->lossless[mbmi->segment_id];
599
#if CONFIG_MRC_TX || CONFIG_LGT
600
  txfm_param.is_inter = is_inter_block(mbmi);
601
602
  txfm_param.dst = dst;
  txfm_param.stride = dst_stride;
603
604
605
#if CONFIG_MRC_TX
  txfm_param.valid_mask = &mbmi->valid_mrc_mask;
#endif  // CONFIG_MRC_TX
606
607
#endif  // CONFIG_MRC_TX || CONFIG_LGT
#if CONFIG_LGT
608
  txfm_param.mode = get_prediction_mode(xd->mi[0], plane, tx_size, block);
Lester Lu's avatar
Lester Lu committed
609
#endif
Angie Chiang's avatar
Angie Chiang committed
610

Thomas Daede's avatar
Thomas Daede committed
611
#if !CONFIG_PVQ
612
  txfm_param.bd = xd->bd;
613
  const int is_hbd = get_bitdepth_data_path_index(xd);
Angie Chiang's avatar
Angie Chiang committed
614
615
616
617

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

Yaowu Xu's avatar
Yaowu Xu committed
621
  if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
622
    if (LIKELY(!x->skip_block)) {
623
      quant_func_list[xform_quant_idx][is_hbd](
624
          coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
625
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
626
      av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
Jingning Han's avatar
Jingning Han committed
627
628
    }
  }
629
630
631
632
#if CONFIG_LV_MAP
  p->txb_entropy_ctx[block] =
      (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
#endif  // CONFIG_LV_MAP
633
634
  return;
#else  // CONFIG_PVQ
Angie Chiang's avatar
Angie Chiang committed
635
  (void)xform_quant_idx;
636
#if CONFIG_HIGHBITDEPTH
637
  txfm_param.bd = xd->bd;
Thomas Daede's avatar
Thomas Daede committed
638
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
639
640
    av1_highbd_fwd_txfm(src_int16, coeff, diff_stride, &txfm_param);
    av1_highbd_fwd_txfm(pred, ref_coeff, diff_stride, &txfm_param);
Thomas Daede's avatar
Thomas Daede committed
641
642
  } else {
#endif
643
644
    av1_fwd_txfm(src_int16, coeff, diff_stride, &txfm_param);
    av1_fwd_txfm(pred, ref_coeff, diff_stride, &txfm_param);
645
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
646
647
  }
#endif
648
649

  // PVQ for inter mode block
650
  if (!x->skip_block) {
ltrudeau's avatar
ltrudeau committed
651
    PVQ_SKIP_TYPE ac_dc_coded =
Thomas Daede's avatar
Thomas Daede committed
652
        av1_pvq_encode_helper(x,
ltrudeau's avatar
ltrudeau committed
653
654
655
656
657
658
659
660
661
662
663
664
                              coeff,        // target original vector
                              ref_coeff,    // reference vector
                              dqcoeff,      // de-quantized vector
                              eob,          // End of Block marker
                              pd->dequant,  // aom's quantizers
                              plane,        // image plane
                              tx_size,      // block size in log_2 - 2
                              tx_type,
                              &x->rate,  // rate measured
                              x->pvq_speed,
                              pvq_info);  // PVQ info for a block
    skip = ac_dc_coded == PVQ_SKIP;
665
  }
666
667
668
669
  x->pvq_skip[plane] = skip;

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

672
static void encode_block(int plane, int block, int blk_row, int blk_col,
673
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
Jingning Han's avatar
Jingning Han committed
674
  struct encode_b_args *const args = arg;
Angie Chiang's avatar
Angie Chiang committed
675
  AV1_COMMON *cm = args->cm;
Jingning Han's avatar
Jingning Han committed
676
677
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
678
  int ctx;
Jingning Han's avatar
Jingning Han committed
679
680
681
682
  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;
683
#if !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
684
  ENTROPY_CONTEXT *a, *l;
685
#endif
686
#if CONFIG_VAR_TX
687
  int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
688
#endif
689
690
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
691
692

#if !CONFIG_PVQ
693
694
  a = &args->ta[blk_col];
  l = &args->tl[blk_row];
695
696
697
698
699
#if CONFIG_VAR_TX
  ctx = get_entropy_context(tx_size, a, l);
#else
  ctx = combine_entropy_contexts(*a, *l);
#endif
700
701
702
#else
  ctx = 0;
#endif  // CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
703

Alex Converse's avatar
Alex Converse committed
704
#if CONFIG_VAR_TX
705
  // Assert not magic number (uninitialized).
706
  assert(x->blk_skip[plane][blk_row * bw + blk_col] != 234);
707

708
  if (x->blk_skip[plane][blk_row * bw + blk_col] == 0) {
709
#else
710
  {
711
#endif
Angie Chiang's avatar
Angie Chiang committed
712
    av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
713
                    ctx, AV1_XFORM_QUANT_FP);
Jingning Han's avatar
Jingning Han committed
714
  }
715
716
#if CONFIG_VAR_TX
  else {
717
    p->eobs[block] = 0;
718
719
  }
#endif
720

721
#if !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
722
723
  av1_optimize_b(cm, x, plane, blk_row, blk_col, block, plane_bsize, tx_size, a,
                 l);
724

725
726
  av1_set_txb_context(x, plane, block, tx_size, a, l);

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

729
  if (p->eobs[block] == 0) return;
730
731
732
733
734
735
#else
  (void)ctx;
  if (!x->pvq_skip[plane]) *(args->skip) = 0;

  if (x->pvq_skip[plane]) return;
#endif
736
737
  TX_TYPE tx_type =
      av1_get_tx_type(pd->plane_type, xd, blk_row, blk_col, block, tx_size);
Lester Lu's avatar
Lester Lu committed
738
739
740
741
742
#if CONFIG_LGT
  PREDICTION_MODE mode = get_prediction_mode(xd->mi[0], plane, tx_size, block);
  av1_inverse_transform_block(xd, dqcoeff, mode, tx_type, tx_size, dst,
                              pd->dst.stride, p->eobs[block]);
#else
743
744
  av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst,
                              pd->dst.stride, p->eobs[block]);
Lester Lu's avatar
Lester Lu committed
745
#endif
Jingning Han's avatar
Jingning Han committed
746
747
}

748
749
750
751
752
753
754
755
#if CONFIG_VAR_TX
static void encode_block_inter(int plane, int block, int blk_row, int blk_col,
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
756
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
757
  const struct macroblockd_plane *const pd = &xd->plane[plane];
758
759
  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
760
  TX_SIZE plane_tx_size;
761
762
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
763

764
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
765

766
767
768
  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
769

770
  if (tx_size == plane_tx_size) {
771
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
772
  } else {
773
    assert(tx_size < TX_SIZES_ALL);
Yue Chen's avatar
Yue Chen committed
774
775
776
777
778
#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
779
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
780
    assert(sub_txs < tx_size);
Yue Chen's avatar
Yue Chen committed
781
#endif
782
783
    // This is the square transform block partition entry point.
    int bsl = tx_size_wide_unit[sub_txs];
784
785
    int i;
    assert(bsl > 0);
786

787
    for (i = 0; i < 4; ++i) {
Yue Chen's avatar
Yue Chen committed
788
789
790
791
792
793
794
795
796
#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
797
798
      const int offsetr = blk_row + ((i >> 1) * bsl);
      const int offsetc = blk_col + ((i & 0x01) * bsl);
Yue Chen's avatar
Yue Chen committed
799
#endif
800
      int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
801

802
      if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
803

804
805
806
      encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
                         arg);
      block += step;
807
808
809
810
811
    }
  }
}
#endif

Angie Chiang's avatar
Angie Chiang committed
812
813
814
815
816
typedef struct encode_block_pass1_args {
  AV1_COMMON *cm;
  MACROBLOCK *x;
} encode_block_pass1_args;

817
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
818
819
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
Angie Chiang's avatar
Angie Chiang committed
820
821
822
  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
823
824
825
826
  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);
827
  TxfmParam txfm_param;
Jingning Han's avatar
Jingning Han committed
828
  uint8_t *dst;
829
  int ctx = 0;
830
831
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
Jingning Han's avatar
Jingning Han committed
832

Angie Chiang's avatar
Angie Chiang committed
833
  av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
834
                  ctx, AV1_XFORM_QUANT_B);
835
#if !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
836
  if (p->eobs[block] > 0) {
837
838
839
840
841
842
843
844
#else
  if (!x->pvq_skip[plane]) {
    {
      int tx_blk_size;
      int i, j;
      // transform block size in pixels
      tx_blk_size = tx_size_wide[tx_size];

Thomas Daede's avatar
Thomas Daede committed
845
846
847
848
// Since av1 does not have separate function which does inverse transform
// but av1_inv_txfm_add_*x*() also does addition of predicted image to
// inverse transformed image,
// pass blank dummy image to av1_inv_txfm_add_*x*(), i.e. set dst as zeros
849
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
850
851
852
853
854
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        for (j = 0; j < tx_blk_size; j++)
          for (i = 0; i < tx_blk_size; i++)
            CONVERT_TO_SHORTPTR(dst)[j * pd->dst.stride + i] = 0;
      } else {
855
#endif  // CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
856
857
        for (j = 0; j < tx_blk_size; j++)
          for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
858
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
859
      }
860
#endif  // CONFIG_HIGHBITDEPTH
861
    }
Jingning Han's avatar
Jingning Han committed
862
#endif  // !CONFIG_PVQ
863
864
865
866
    txfm_param.bd = xd->bd;
    txfm_param.tx_type = DCT_DCT;
    txfm_param.eob = p->eobs[block];
    txfm_param.lossless = xd->lossless[xd->mi[0]->mbmi.segment_id];
867
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
868
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
869
      av1_highbd_inv_txfm_add_4x4(dqcoeff, dst, pd->dst.stride, &txfm_param);
870
      return;
Jingning Han's avatar
Jingning Han committed
871
    }
872
#endif  //  CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
873
    if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
874
      av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
875
    } else {
876
      av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, &txfm_param);
877
    }
Jingning Han's avatar
Jingning Han committed
878
879
880
  }
}

Angie Chiang's avatar
Angie Chiang committed
881
882
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
883
884
  av1_subtract_plane(x, bsize, 0);
  av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
Angie Chiang's avatar
Angie Chiang committed
885
                                         encode_block_pass1, &args);
Jingning Han's avatar
Jingning Han committed
886
887
}

Yaowu Xu's avatar
Yaowu Xu committed
888
889
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
890
891
892
  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
893
  struct encode_b_args arg = { cm, x, &ctx, &mbmi->skip, NULL, NULL, 1 };
Jingning Han's avatar
Jingning Han committed
894
895
896
897
  int plane;

  mbmi->skip = 1;

898
  if (x->skip) return;
Jingning Han's avatar
Jingning Han committed
899
900

  for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
Jingning Han's avatar
Jingning Han committed
901
#if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
902
903
904
905
906
    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))
907
      continue;
908
909

    bsize = scale_chroma_bsize(bsize, subsampling_x, subsampling_y);
910
911
912
913
914
#else
    (void)mi_row;
    (void)mi_col;
#endif

915
916
917
918