encodemb.c 64.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 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 116 117
#if CONFIG_EC_ADAPT
  { 10, 7 }, { 8, 5 },
#else
118
  { 10, 6 }, { 8, 6 },
Thomas Davies's avatar
Thomas Davies committed
119
#endif
120
};
Jingning Han's avatar
Jingning Han committed
121

122 123 124 125 126
#define UPDATE_RD_COST()                             \
  {                                                  \
    rd_cost0 = RDCOST(rdmult, rddiv, rate0, error0); \
    rd_cost1 = RDCOST(rdmult, rddiv, rate1, error1); \
  }
Jingning Han's avatar
Jingning Han committed
127

128 129 130
static INLINE unsigned int get_token_bit_costs(
    unsigned int token_costs[2][COEFF_CONTEXTS][ENTROPY_TOKENS], int skip_eob,
    int ctx, int token) {
131 132 133 134
  (void)skip_eob;
  return token_costs[token == ZERO_TOKEN || token == EOB_TOKEN][ctx][token];
}

135
#if !CONFIG_LV_MAP
136

137
typedef struct av1_token_state_greedy {
138 139 140
  int16_t token;
  tran_low_t qc;
  tran_low_t dqc;
141
} av1_token_state_greedy;
142

143 144
static int optimize_b_greedy(const AV1_COMMON *cm, MACROBLOCK *mb, int plane,
                             int block, TX_SIZE tx_size, int ctx) {
145 146 147 148
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
149
  av1_token_state_greedy tokens[MAX_TX_SQUARE + 1][2];
150 151 152 153 154 155 156 157 158 159 160 161 162 163
  uint8_t token_cache[MAX_TX_SQUARE];
  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 int eob = p->eobs[block];
  const PLANE_TYPE plane_type = pd->plane_type;
  const int16_t *const dequant_ptr = pd->dequant;
  const uint8_t *const band_translate = get_band_translate(tx_size);
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
  const SCAN_ORDER *const scan_order =
      get_scan(cm, tx_size, tx_type, is_inter_block(&xd->mi[0]->mbmi));
  const int16_t *const scan = scan_order->scan;
  const int16_t *const nb = scan_order->neighbors;
  int dqv;
164
  const int shift = av1_get_tx_scale(tx_size);
165 166
#if CONFIG_AOM_QM
  int seg_id = xd->mi[0]->mbmi.segment_id;
167 168 169 170 171
  // 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];
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
#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
  int sz = 0;
  const int64_t rddiv = mb->rddiv;
  int64_t rd_cost0, rd_cost1;
  int16_t t0, t1;
  int i, final_eob;
  const int cat6_bits = av1_get_cat6_extrabits_size(tx_size, xd->bd);
  unsigned int(*token_costs)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
      mb->token_costs[txsize_sqr_map[tx_size]][plane_type][ref];
  const int default_eob = tx_size_2d[tx_size];

187
  assert(mb->qindex > 0);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

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

  int64_t rdmult = (mb->rdmult * plane_rd_mult[ref][plane_type]) >> 1;

  int64_t rate0, rate1;
  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
    int x = qcoeff[rc];
    t0 = av1_get_token(x);

    tokens[i][0].qc = x;
    tokens[i][0].token = t0;
    tokens[i][0].dqc = dqcoeff[rc];

    token_cache[rc] = av1_pt_energy_class[t0];
  }
  tokens[eob][0].token = EOB_TOKEN;
  tokens[eob][0].qc = 0;
  tokens[eob][0].dqc = 0;
  tokens[eob][1] = tokens[eob][0];

  unsigned int(*token_costs_ptr)[2][COEFF_CONTEXTS][ENTROPY_TOKENS] =
      token_costs;

  final_eob = 0;

  int64_t eob_cost0, eob_cost1;

  const int ctx0 = ctx;
  /* Record the r-d cost */
  int64_t accu_rate = 0;
  int64_t accu_error = 0;

  rate0 = get_token_bit_costs(*(token_costs_ptr + band_translate[0]), 0, ctx0,
                              EOB_TOKEN);
  int64_t best_block_rd_cost = RDCOST(rdmult, rddiv, rate0, accu_error);

  // int64_t best_block_rd_cost_all0 = best_block_rd_cost;

  int x_prev = 1;

  for (i = 0; i < eob; i++) {
    const int rc = scan[i];
    int x = qcoeff[rc];
    sz = -(x < 0);

    int band_cur = band_translate[i];
    int ctx_cur = (i == 0) ? ctx : get_coef_context(nb, token_cache, i);
    int token_tree_sel_cur = (x_prev == 0);

    if (x == 0) {
      // no need to search when x == 0
      rate0 =
          get_token_bit_costs(*(token_costs_ptr + band_cur), token_tree_sel_cur,
                              ctx_cur, tokens[i][0].token);
      accu_rate += rate0;
      x_prev = 0;
      // 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.
253
      int dx0 = abs(coeff[rc]) * (1 << shift);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
#if CONFIG_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
        dx0 >>= xd->bd - 8;
      }
#endif
      int64_t d0 = (int64_t)dx0 * dx0;

      int x_a = x - 2 * sz - 1;
      int64_t d2, d2_a;

      int dx;

#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

      dx = (dqcoeff[rc] - coeff[rc]) * (1 << shift);
#if CONFIG_HIGHBITDEPTH
      if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
277 278 279
        int dx_sign = dx < 0 ? 1 : 0;
        dx = abs(dx) >> (xd->bd - 8);
        if (dx_sign) dx = -dx;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
      }
#endif  // CONFIG_HIGHBITDEPTH
      d2 = (int64_t)dx * dx;

      /* compute the distortion for the second candidate
       * x_a = x - 2 * sz + 1;
       */
      if (x_a != 0) {
#if CONFIG_NEW_QUANT
        dx = av1_dequant_coeff_nuq(x, dqv, dequant_val[band_translate[i]]) -
             (coeff[rc] << shift);
#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;
      }
      /*  Computing rates and r-d cost
       */

      int best_x, best_eob_x;
      int64_t base_bits, next_bits0, next_bits1;
      int64_t next_eob_bits0, next_eob_bits1;

      // rate cost of x
      base_bits = av1_get_token_cost(x, &t0, cat6_bits);
      rate0 = base_bits + get_token_bit_costs(*(token_costs_ptr + band_cur),
                                              token_tree_sel_cur, ctx_cur, t0);

      base_bits = av1_get_token_cost(x_a, &t1, cat6_bits);
      rate1 = base_bits + get_token_bit_costs(*(token_costs_ptr + band_cur),
                                              token_tree_sel_cur, ctx_cur, t1);

      next_bits0 = 0;
      next_bits1 = 0;
      next_eob_bits0 = 0;
      next_eob_bits1 = 0;

      if (i < default_eob - 1) {
        int ctx_next, token_tree_sel_next;
        int band_next = band_translate[i + 1];

        token_cache[rc] = av1_pt_energy_class[t0];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
        token_tree_sel_next = (x == 0);

        next_bits0 = get_token_bit_costs(*(token_costs_ptr + band_next),
                                         token_tree_sel_next, ctx_next,
                                         tokens[i + 1][0].token);
        next_eob_bits0 =
            get_token_bit_costs(*(token_costs_ptr + band_next),
                                token_tree_sel_next, ctx_next, EOB_TOKEN);

        token_cache[rc] = av1_pt_energy_class[t1];
        ctx_next = get_coef_context(nb, token_cache, i + 1);
        token_tree_sel_next = (x_a == 0);

        next_bits1 = get_token_bit_costs(*(token_costs_ptr + band_next),
                                         token_tree_sel_next, ctx_next,
                                         tokens[i + 1][0].token);

        if (x_a != 0) {
          next_eob_bits1 =
              get_token_bit_costs(*(token_costs_ptr + band_next),
                                  token_tree_sel_next, ctx_next, EOB_TOKEN);
        }
      }

      rd_cost0 = RDCOST(rdmult, rddiv, (rate0 + next_bits0), d2);
      rd_cost1 = RDCOST(rdmult, rddiv, (rate1 + next_bits1), d2_a);

      best_x = (rd_cost1 < rd_cost0);

      eob_cost0 = RDCOST(rdmult, rddiv, (accu_rate + rate0 + next_eob_bits0),
                         (accu_error + d2 - d0));
      eob_cost1 = eob_cost0;
      if (x_a != 0) {
        eob_cost1 = RDCOST(rdmult, rddiv, (accu_rate + rate1 + next_eob_bits1),
                           (accu_error + d2_a - d0));
        best_eob_x = (eob_cost1 < eob_cost0);
      } else {
        best_eob_x = 0;
      }

      int dqc, dqc_a = 0;

      dqc = dqcoeff[rc];
      if (best_x + best_eob_x) {
        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
389 390
          if (x_a < 0)
            dqc_a = -((-x_a * dqv) >> shift);
391
          else
392
            dqc_a = (x_a * dqv) >> shift;
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
#endif  // CONFIG_NEW_QUANT
        } else {
          dqc_a = 0;
        }  // if (x_a != 0)
      }

      // record the better quantized value
      if (best_x) {
        qcoeff[rc] = x_a;
        dqcoeff[rc] = dqc_a;

        accu_rate += rate1;
        accu_error += d2_a - d0;
        assert(d2_a <= d0);

        token_cache[rc] = av1_pt_energy_class[t1];
      } else {
        accu_rate += rate0;
        accu_error += d2 - d0;
        assert(d2 <= d0);

        token_cache[rc] = av1_pt_energy_class[t0];
      }

      x_prev = qcoeff[rc];

      // determine whether to move the eob position to i+1
      int64_t best_eob_cost_i = eob_cost0;

      tokens[i][1].token = t0;
      tokens[i][1].qc = x;
      tokens[i][1].dqc = dqc;

      if ((x_a != 0) && (best_eob_x)) {
        best_eob_cost_i = eob_cost1;

        tokens[i][1].token = t1;
        tokens[i][1].qc = x_a;
        tokens[i][1].dqc = dqc_a;
      }

      if (best_eob_cost_i < best_block_rd_cost) {
        best_block_rd_cost = best_eob_cost_i;
        final_eob = i + 1;
      }
    }  // if (x==0)
  }    // for (i)

  assert(final_eob <= eob);
  if (final_eob > 0) {
    assert(tokens[final_eob - 1][1].qc != 0);
    i = final_eob - 1;
    int rc = scan[i];
    qcoeff[rc] = tokens[i][1].qc;
    dqcoeff[rc] = tokens[i][1].dqc;
  }

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

  mb->plane[plane].eobs[block] = final_eob;
  return final_eob;
}
459
#endif  // !CONFIG_LV_MAP
460 461

int av1_optimize_b(const AV1_COMMON *cm, MACROBLOCK *mb, int plane, int block,
462 463
                   BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                   const ENTROPY_CONTEXT *a, const ENTROPY_CONTEXT *l) {
464 465 466 467 468 469
  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
470

471
#if CONFIG_PVQ
472 473
  (void)cm;
  (void)tx_size;
474 475
  (void)a;
  (void)l;
476 477
  return eob;
#endif
Jingning Han's avatar
Jingning Han committed
478

479
#if !CONFIG_LV_MAP
480
  (void)plane_bsize;
481 482 483 484
#if CONFIG_VAR_TX
  int ctx = get_entropy_context(tx_size, a, l);
#else
  int ctx = combine_entropy_contexts(*a, *l);
485
#endif  // CONFIG_VAR_TX
486
  return optimize_b_greedy(cm, mb, plane, block, tx_size, ctx);
487 488 489 490 491
#else   // !CONFIG_LV_MAP
  TXB_CTX txb_ctx;
  get_txb_ctx(plane_bsize, tx_size, plane, a, l, &txb_ctx);
  return av1_optimize_txb(cm, mb, plane, block, tx_size, &txb_ctx);
#endif  // !CONFIG_LV_MAP
492
}
493

Thomas Daede's avatar
Thomas Daede committed
494
#if !CONFIG_PVQ
495
#if CONFIG_HIGHBITDEPTH
Angie Chiang's avatar
Angie Chiang committed
496 497
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
498
  QUANT_FUNC_HIGHBD = 1,
499
  QUANT_FUNC_TYPES = 2
Angie Chiang's avatar
Angie Chiang committed
500 501
} QUANT_FUNC;

502 503
static AV1_QUANT_FACADE
    quant_func_list[AV1_XFORM_QUANT_TYPES][QUANT_FUNC_TYPES] = {
504
#if !CONFIG_NEW_QUANT
505
      { av1_quantize_fp_facade, av1_highbd_quantize_fp_facade },
Yaowu Xu's avatar
Yaowu Xu committed
506 507
      { av1_quantize_b_facade, av1_highbd_quantize_b_facade },
      { av1_quantize_dc_facade, av1_highbd_quantize_dc_facade },
508
#else   // !CONFIG_NEW_QUANT
509 510 511
      { 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 },
512
#endif  // !CONFIG_NEW_QUANT
513 514
      { NULL, NULL }
    };
515

Thomas Daede's avatar
Thomas Daede committed
516
#else
517

Angie Chiang's avatar
Angie Chiang committed
518 519
typedef enum QUANT_FUNC {
  QUANT_FUNC_LOWBD = 0,
520
  QUANT_FUNC_TYPES = 1
Angie Chiang's avatar
Angie Chiang committed
521
} QUANT_FUNC;
Angie Chiang's avatar
Angie Chiang committed
522

523 524
static AV1_QUANT_FACADE quant_func_list[AV1_XFORM_QUANT_TYPES]
                                       [QUANT_FUNC_TYPES] = {
525
#if !CONFIG_NEW_QUANT
clang-format's avatar
clang-format committed
526 527 528
                                         { av1_quantize_fp_facade },
                                         { av1_quantize_b_facade },
                                         { av1_quantize_dc_facade },
529
#else   // !CONFIG_NEW_QUANT
530 531 532
                                         { av1_quantize_fp_nuq_facade },
                                         { av1_quantize_b_nuq_facade },
                                         { av1_quantize_dc_nuq_facade },
533
#endif  // !CONFIG_NEW_QUANT
clang-format's avatar
clang-format committed
534 535
                                         { NULL }
                                       };
536
#endif  // CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
537
#endif  // CONFIG_PVQ
538

Angie Chiang's avatar
Angie Chiang committed
539 540
void av1_xform_quant(const AV1_COMMON *cm, MACROBLOCK *x, int plane, int block,
                     int blk_row, int blk_col, BLOCK_SIZE plane_bsize,
541 542
                     TX_SIZE tx_size, int ctx,
                     AV1_XFORM_QUANT xform_quant_idx) {
Jingning Han's avatar
Jingning Han committed
543
  MACROBLOCKD *const xd = &x->e_mbd;
544
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yushin Cho's avatar
Yushin Cho committed
545
#if !(CONFIG_PVQ || CONFIG_DAALA_DIST)
Jingning Han's avatar
Jingning Han committed
546 547
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
548 549 550 551
#else
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
#endif
552
  PLANE_TYPE plane_type = get_plane_type(plane);
553
  TX_TYPE tx_type = get_tx_type(plane_type, xd, block, tx_size);
554
  const int is_inter = is_inter_block(mbmi);
Angie Chiang's avatar
Angie Chiang committed
555
  const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, is_inter);
Jingning Han's avatar
Jingning Han committed
556 557 558 559
  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];
560
  const int diff_stride = block_size_wide[plane_bsize];
561
#if CONFIG_AOM_QM
562
  int seg_id = mbmi->segment_id;
563 564 565 566 567 568 569 570
  // 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];
571
#endif
Angie Chiang's avatar
Angie Chiang committed
572 573

  FWD_TXFM_PARAM fwd_txfm_param;
574

Yushin Cho's avatar
Yushin Cho committed
575 576 577 578
#if CONFIG_PVQ || CONFIG_DAALA_DIST
  uint8_t *dst;
  int16_t *pred;
  const int dst_stride = pd->dst.stride;
579 580
  const int txw = tx_size_wide[tx_size];
  const int txh = tx_size_high[tx_size];
Yushin Cho's avatar
Yushin Cho committed
581 582 583
  int i, j;
#endif

584 585
#if !CONFIG_PVQ
  const int tx2d_size = tx_size_2d[tx_size];
586
  QUANT_PARAM qparam;
587 588
  const int16_t *src_diff;

589 590
  src_diff =
      &p->src_diff[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
591
  qparam.log_scale = av1_get_tx_scale(tx_size);
592 593 594 595 596 597 598 599
#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
600 601 602 603
#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
604 605 606
  uint8_t *src;
  int16_t *src_int16;
  const int src_stride = p->src.stride;
607

608
  (void)ctx;
609 610 611 612 613 614 615
  (void)scan_order;
  (void)qcoeff;

  if (x->pvq_coded) {
    assert(block < MAX_PVQ_BLOCKS_IN_SB);
    pvq_info = &x->pvq[block][plane];
  }
616 617 618
  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
619

620
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
621
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
622 623
    for (j = 0; j < txh; j++)
      for (i = 0; i < txw; i++)
Thomas Daede's avatar
Thomas Daede committed
624 625 626
        src_int16[diff_stride * j + i] =
            CONVERT_TO_SHORTPTR(src)[src_stride * j + i];
  } else {
627
#endif  // CONFIG_HIGHBITDEPTH
628 629
    for (j = 0; j < txh; j++)
      for (i = 0; i < txw; i++)
Thomas Daede's avatar
Thomas Daede committed
630
        src_int16[diff_stride * j + i] = src[src_stride * j + i];
631
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
632
  }
633
#endif  // CONFIG_HIGHBITDEPTH
Yushin Cho's avatar
Yushin Cho committed
634 635 636 637
#endif

#if CONFIG_PVQ || CONFIG_DAALA_DIST
  dst = &pd->dst.buf[(blk_row * dst_stride + blk_col) << tx_size_wide_log2[0]];
638
  pred = &pd->pred[(blk_row * diff_stride + blk_col) << tx_size_wide_log2[0]];
639

Thomas Daede's avatar
Thomas Daede committed
640 641
// copy uint8 orig and predicted block to int16 buffer
// in order to use existing VP10 transform functions
642
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
643
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
644 645
    for (j = 0; j < txh; j++)
      for (i = 0; i < txw; i++)
Thomas Daede's avatar
Thomas Daede committed
646 647 648
        pred[diff_stride * j + i] =
            CONVERT_TO_SHORTPTR(dst)[dst_stride * j + i];
  } else {
649
#endif  // CONFIG_HIGHBITDEPTH
650 651
    for (j = 0; j < txh; j++)
      for (i = 0; i < txw; i++)
Thomas Daede's avatar
Thomas Daede committed
652
        pred[diff_stride * j + i] = dst[dst_stride * j + i];
653
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
654
  }
655
#endif  // CONFIG_HIGHBITDEPTH
656
#endif
Yushin Cho's avatar
Yushin Cho committed
657

658
  (void)ctx;
659 660

  fwd_txfm_param.tx_type = tx_type;
Angie Chiang's avatar
Angie Chiang committed
661
  fwd_txfm_param.tx_size = tx_size;
662
  fwd_txfm_param.lossless = xd->lossless[mbmi->segment_id];
Angie Chiang's avatar
Angie Chiang committed
663

Thomas Daede's avatar
Thomas Daede committed
664
#if !CONFIG_PVQ
665
#if CONFIG_HIGHBITDEPTH
666
  fwd_txfm_param.bd = xd->bd;
Jingning Han's avatar
Jingning Han committed
667
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
hui su's avatar
hui su committed
668
    av1_highbd_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
Yaowu Xu's avatar
Yaowu Xu committed
669
    if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
670
      if (LIKELY(!x->skip_block)) {
671
        quant_func_list[xform_quant_idx][QUANT_FUNC_HIGHBD](
672
            coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
673
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
674
        av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
675 676
      }
    }
677 678 679 680
#if CONFIG_LV_MAP
    p->txb_entropy_ctx[block] =
        (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
#endif  // CONFIG_LV_MAP
681 682
    return;
  }
683
#endif  // CONFIG_HIGHBITDEPTH
hui su's avatar
hui su committed
684
  av1_fwd_txfm(src_diff, coeff, diff_stride, &fwd_txfm_param);
Yaowu Xu's avatar
Yaowu Xu committed
685
  if (xform_quant_idx != AV1_XFORM_QUANT_SKIP_QUANT) {
686
    if (LIKELY(!x->skip_block)) {
687
      quant_func_list[xform_quant_idx][QUANT_FUNC_LOWBD](
688
          coeff, tx2d_size, p, qcoeff, pd, dqcoeff, eob, scan_order, &qparam);
689
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
690
      av1_quantize_skip(tx2d_size, qcoeff, dqcoeff, eob);
Jingning Han's avatar
Jingning Han committed
691 692
    }
  }
693 694 695 696 697
#if CONFIG_LV_MAP
  p->txb_entropy_ctx[block] =
      (uint8_t)av1_get_txb_entropy_context(qcoeff, scan_order, *eob);
#endif  // CONFIG_LV_MAP
#else   // #if !CONFIG_PVQ
Angie Chiang's avatar
Angie Chiang committed
698
  (void)xform_quant_idx;
699
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
700 701
  fwd_txfm_param.bd = xd->bd;
  if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
hui su's avatar
hui su committed
702 703
    av1_highbd_fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param);
    av1_highbd_fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param);
Thomas Daede's avatar
Thomas Daede committed
704 705
  } else {
#endif
hui su's avatar
hui su committed
706 707
    av1_fwd_txfm(src_int16, coeff, diff_stride, &fwd_txfm_param);
    av1_fwd_txfm(pred, ref_coeff, diff_stride, &fwd_txfm_param);
708
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
709 710
  }
#endif
711 712

  // PVQ for inter mode block
713
  if (!x->skip_block) {
ltrudeau's avatar
ltrudeau committed
714
    PVQ_SKIP_TYPE ac_dc_coded =
Thomas Daede's avatar
Thomas Daede committed
715
        av1_pvq_encode_helper(x,
ltrudeau's avatar
ltrudeau committed
716 717 718 719 720 721 722 723 724 725 726 727
                              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;
728
  }
729 730 731 732
  x->pvq_skip[plane] = skip;

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

735
static void encode_block(int plane, int block, int blk_row, int blk_col,
736
                         BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
Jingning Han's avatar
Jingning Han committed
737
  struct encode_b_args *const args = arg;
Angie Chiang's avatar
Angie Chiang committed
738
  AV1_COMMON *cm = args->cm;
Jingning Han's avatar
Jingning Han committed
739 740
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
741
  int ctx;
Jingning Han's avatar
Jingning Han committed
742 743 744 745
  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;
746
#if !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
747
  ENTROPY_CONTEXT *a, *l;
748
#endif
749
#if CONFIG_VAR_TX
750
  int bw = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
751
#endif
752 753
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
754 755

#if !CONFIG_PVQ
756 757
  a = &args->ta[blk_col];
  l = &args->tl[blk_row];
758 759 760 761 762
#if CONFIG_VAR_TX
  ctx = get_entropy_context(tx_size, a, l);
#else
  ctx = combine_entropy_contexts(*a, *l);
#endif
763 764 765
#else
  ctx = 0;
#endif  // CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
766

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

771
  if (x->blk_skip[plane][blk_row * bw + blk_col] == 0) {
772
#else
773
  {
774
#endif
Angie Chiang's avatar
Angie Chiang committed
775
    av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
776
                    ctx, AV1_XFORM_QUANT_FP);
Jingning Han's avatar
Jingning Han committed
777
  }
778 779
#if CONFIG_VAR_TX
  else {
780
    p->eobs[block] = 0;
781 782
  }
#endif
783

784
#if !CONFIG_PVQ
785
  av1_optimize_b(cm, x, plane, block, plane_bsize, tx_size, a, l);
786

787 788
  av1_set_txb_context(x, plane, block, tx_size, a, l);

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

791
  if (p->eobs[block] == 0) return;
792 793 794 795 796 797
#else
  (void)ctx;
  if (!x->pvq_skip[plane]) *(args->skip) = 0;

  if (x->pvq_skip[plane]) return;
#endif
798
  TX_TYPE tx_type = get_tx_type(pd->plane_type, xd, block, tx_size);
799 800
  av1_inverse_transform_block(xd, dqcoeff, tx_type, tx_size, dst,
                              pd->dst.stride, p->eobs[block]);
Jingning Han's avatar
Jingning Han committed
801 802
}

803 804 805 806 807 808 809 810
#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;
811
  const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
812
  const struct macroblockd_plane *const pd = &xd->plane[plane];
813 814
  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
815
  TX_SIZE plane_tx_size;
816 817
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
818

819
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
820

821 822 823
  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
824

825
  if (tx_size == plane_tx_size) {
826
    encode_block(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
827
  } else {
828
    assert(tx_size < TX_SIZES_ALL);
829
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
830
    assert(sub_txs < tx_size);
831 832
    // This is the square transform block partition entry point.
    int bsl = tx_size_wide_unit[sub_txs];
833 834
    int i;
    assert(bsl > 0);
835

836
    for (i = 0; i < 4; ++i) {
837 838 839
      const int offsetr = blk_row + ((i >> 1) * bsl);
      const int offsetc = blk_col + ((i & 0x01) * bsl);
      int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
840

841
      if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
842

843 844 845
      encode_block_inter(plane, block, offsetr, offsetc, plane_bsize, sub_txs,
                         arg);
      block += step;
846 847 848 849 850
    }
  }
}
#endif

Angie Chiang's avatar
Angie Chiang committed
851 852 853 854 855
typedef struct encode_block_pass1_args {
  AV1_COMMON *cm;
  MACROBLOCK *x;
} encode_block_pass1_args;

856
static void encode_block_pass1(int plane, int block, int blk_row, int blk_col,
857 858
                               BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
                               void *arg) {
Angie Chiang's avatar
Angie Chiang committed
859 860 861
  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
862 863 864 865 866
  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;
867
  int ctx = 0;
868 869
  dst = &pd->dst
             .buf[(blk_row * pd->dst.stride + blk_col) << tx_size_wide_log2[0]];
Jingning Han's avatar
Jingning Han committed
870

Angie Chiang's avatar
Angie Chiang committed
871
  av1_xform_quant(cm, x, plane, block, blk_row, blk_col, plane_bsize, tx_size,
872
                  ctx, AV1_XFORM_QUANT_B);
873
#if !CONFIG_PVQ
Jingning Han's avatar
Jingning Han committed
874
  if (p->eobs[block] > 0) {
875 876 877 878 879 880 881 882
#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
883 884 885 886
// 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
887
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
888 889 890 891 892
      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 {
893
#endif  // CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
894 895
        for (j = 0; j < tx_blk_size; j++)
          for (i = 0; i < tx_blk_size; i++) dst[j * pd->dst.stride + i] = 0;
896
#if CONFIG_HIGHBITDEPTH
Thomas Daede's avatar
Thomas Daede committed
897
      }
898
#endif  // CONFIG_HIGHBITDEPTH
899
    }
Jingning Han's avatar
Jingning Han committed
900
#endif  // !CONFIG_PVQ
901
#if CONFIG_HIGHBITDEPTH
Jingning Han's avatar
Jingning Han committed
902
    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
Yaowu Xu's avatar
Yaowu Xu committed
903
      if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
Yaowu Xu's avatar
Yaowu Xu committed
904 905
        av1_highbd_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block],
                               xd->bd);
906
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
907 908
        av1_highbd_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block],
                               xd->bd);
909 910
      }
      return;
Jingning Han's avatar
Jingning Han committed
911
    }
912
#endif  //  CONFIG_HIGHBITDEPTH
Yaowu Xu's avatar
Yaowu Xu committed
913
    if (xd->lossless[xd->mi[0]->mbmi.segment_id]) {
Yaowu Xu's avatar
Yaowu Xu committed
914
      av1_iwht4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
915
    } else {
Yaowu Xu's avatar
Yaowu Xu committed
916
      av1_idct4x4_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
917
    }
Jingning Han's avatar
Jingning Han committed
918 919 920
  }
}

Angie Chiang's avatar
Angie Chiang committed
921 922
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
923 924
  av1_subtract_plane(x, bsize, 0);
  av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, 0,
Angie Chiang's avatar
Angie Chiang committed
925
                                         encode_block_pass1, &args);
Jingning Han's avatar
Jingning Han committed
926 927
}

Yaowu Xu's avatar
Yaowu Xu committed
928 929
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
930 931 932
  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
933
  struct encode_b_args arg = { cm, x, &ctx, &mbmi->skip, NULL, NULL, 1 };
Jingning Han's avatar