decodemv.c 99.9 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 12 13
 */

#include <assert.h>

14 15 16 17 18 19 20
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/entropymv.h"
#include "av1/common/mvref_common.h"
#include "av1/common/pred_common.h"
#include "av1/common/reconinter.h"
hui su's avatar
hui su committed
21 22 23
#if CONFIG_EXT_INTRA
#include "av1/common/reconintra.h"
#endif  // CONFIG_EXT_INTRA
24
#include "av1/common/seg_common.h"
Yue Chen's avatar
Yue Chen committed
25 26 27
#if CONFIG_WARPED_MOTION
#include "av1/common/warped_motion.h"
#endif  // CONFIG_WARPED_MOTION
28 29

#include "av1/decoder/decodeframe.h"
Jingning Han's avatar
Jingning Han committed
30
#include "av1/decoder/decodemv.h"
31

Yaowu Xu's avatar
Yaowu Xu committed
32
#include "aom_dsp/aom_dsp_common.h"
33

Michael Bebenita's avatar
Michael Bebenita committed
34
#define ACCT_STR __func__
35

36
#define DEC_MISMATCH_DEBUG 0
37

38
#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
39
static INLINE int read_uniform(aom_reader *r, int n) {
40 41 42
  const int l = get_unsigned_bits(n);
  const int m = (1 << l) - n;
  const int v = aom_read_literal(r, l - 1, ACCT_STR);
hui su's avatar
hui su committed
43 44 45 46
  assert(l != 0);
  if (v < m)
    return v;
  else
Michael Bebenita's avatar
Michael Bebenita committed
47
    return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
hui su's avatar
hui su committed
48
}
49
#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
hui su's avatar
hui su committed
50

51
static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
52 53 54
  return (PREDICTION_MODE)
      av1_intra_mode_inv[aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR)];
}
Jingning Han's avatar
Jingning Han committed
55

56 57 58 59 60 61 62 63 64
#if CONFIG_DELTA_Q
static int read_delta_qindex(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
                             MB_MODE_INFO *const mbmi, int mi_col, int mi_row) {
  FRAME_COUNTS *counts = xd->counts;
  int sign, abs, reduced_delta_qindex = 0;
  BLOCK_SIZE bsize = mbmi->sb_type;
  const int b_col = mi_col & MAX_MIB_MASK;
  const int b_row = mi_row & MAX_MIB_MASK;
  const int read_delta_q_flag = (b_col == 0 && b_row == 0);
Thomas Davies's avatar
Thomas Davies committed
65 66 67 68 69 70 71 72
  int rem_bits, thr;
  int i, smallval;
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif
73

74
  if ((bsize != BLOCK_LARGEST || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Davies's avatar
Thomas Davies committed
75 76 77 78 79 80 81 82
    abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
    smallval = (abs < DELTA_Q_SMALL);
    if (counts) {
      for (i = 0; i < abs; ++i) counts->delta_q[i][1]++;
      if (smallval) counts->delta_q[abs][0]++;
    }

    if (!smallval) {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      rem_bits = aom_read_literal(r, 3, ACCT_STR);
      thr = (1 << rem_bits) + 1;
      abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
    }

    if (abs) {
      sign = aom_read_bit(r, ACCT_STR);
    } else {
      sign = 1;
    }

    reduced_delta_qindex = sign ? -abs : abs;
  }
  return reduced_delta_qindex;
}
Fangwen Fu's avatar
Fangwen Fu committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#if CONFIG_EXT_DELTA_Q
static int read_delta_lflevel(AV1_COMMON *cm, MACROBLOCKD *xd, aom_reader *r,
                              MB_MODE_INFO *const mbmi, int mi_col,
                              int mi_row) {
  FRAME_COUNTS *counts = xd->counts;
  int sign, abs, reduced_delta_lflevel = 0;
  BLOCK_SIZE bsize = mbmi->sb_type;
  const int b_col = mi_col & MAX_MIB_MASK;
  const int b_row = mi_row & MAX_MIB_MASK;
  const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
  int rem_bits, thr;
  int i, smallval;
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

  if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_lf_flag) {
    abs =
        aom_read_symbol(r, ec_ctx->delta_lf_cdf, DELTA_LF_PROBS + 1, ACCT_STR);
    smallval = (abs < DELTA_LF_SMALL);
    if (counts) {
      for (i = 0; i < abs; ++i) counts->delta_lf[i][1]++;
      if (smallval) counts->delta_lf[abs][0]++;
    }
    if (!smallval) {
      rem_bits = aom_read_literal(r, 3, ACCT_STR);
      thr = (1 << rem_bits) + 1;
      abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
    }

    if (abs) {
      sign = aom_read_bit(r, ACCT_STR);
    } else {
      sign = 1;
    }

    reduced_delta_lflevel = sign ? -abs : abs;
  }
  return reduced_delta_lflevel;
}
#endif
142 143
#endif

144
static PREDICTION_MODE read_intra_mode_y(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
145
                                         aom_reader *r, int size_group) {
Jingning Han's avatar
Jingning Han committed
146
  const PREDICTION_MODE y_mode =
147
      read_intra_mode(r, ec_ctx->y_mode_cdf[size_group]);
Jingning Han's avatar
Jingning Han committed
148
  FRAME_COUNTS *counts = xd->counts;
149
  if (counts) ++counts->y_mode[size_group][y_mode];
Jingning Han's avatar
Jingning Han committed
150 151 152
  return y_mode;
}

153 154
static PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
                                          MACROBLOCKD *xd, aom_reader *r,
Jingning Han's avatar
Jingning Han committed
155
                                          PREDICTION_MODE y_mode) {
156
  const PREDICTION_MODE uv_mode =
157
      read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
Jingning Han's avatar
Jingning Han committed
158
  FRAME_COUNTS *counts = xd->counts;
159
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
Jingning Han's avatar
Jingning Han committed
160 161 162
  return uv_mode;
}

Luc Trudeau's avatar
Luc Trudeau committed
163
#if CONFIG_CFL
164
static int read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
165
                           CFL_SIGN_TYPE signs_out[CFL_PRED_PLANES]) {
166 167 168 169 170 171 172 173 174 175 176 177 178
  const int ind =
      aom_read_symbol(r, ec_ctx->cfl_alpha_cdf, CFL_ALPHABET_SIZE, "cfl:alpha");
  // Signs are only coded for nonzero values
  // sign == 0 implies negative alpha
  // sign == 1 implies positive alpha
  signs_out[CFL_PRED_U] = cfl_alpha_codes[ind][CFL_PRED_U]
                              ? aom_read_bit(r, "cfl:sign")
                              : CFL_SIGN_POS;
  signs_out[CFL_PRED_V] = cfl_alpha_codes[ind][CFL_PRED_V]
                              ? aom_read_bit(r, "cfl:sign")
                              : CFL_SIGN_POS;

  return ind;
Luc Trudeau's avatar
Luc Trudeau committed
179 180 181
}
#endif

182
#if CONFIG_EXT_INTER && CONFIG_INTERINTRA
Yaowu Xu's avatar
Yaowu Xu committed
183 184 185
static INTERINTRA_MODE read_interintra_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
                                            aom_reader *r, int size_group) {
  const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_tree(
Michael Bebenita's avatar
Michael Bebenita committed
186 187
      r, av1_interintra_mode_tree, cm->fc->interintra_mode_prob[size_group],
      ACCT_STR);
188
  FRAME_COUNTS *counts = xd->counts;
189
  if (counts) ++counts->interintra_mode[size_group][ii_mode];
190 191
  return ii_mode;
}
192
#endif  // CONFIG_EXT_INTER && CONFIG_INTERINTRA
193

194
static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
195
                                       aom_reader *r, int16_t ctx) {
196
  FRAME_COUNTS *counts = xd->counts;
197
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
198 199 200 201 202 203
  int is_newmv, is_zeromv, is_refmv;
#if CONFIG_NEW_MULTISYMBOL
  is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
#else
  is_newmv = aom_read(r, ec_ctx->newmv_prob[mode_ctx], ACCT_STR) == 0;
#endif
204

205
  if (is_newmv) {
206
    if (counts) ++counts->newmv_mode[mode_ctx][0];
207
    return NEWMV;
208
  }
209
  if (counts) ++counts->newmv_mode[mode_ctx][1];
210

211
  if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
212

213
  mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
214

215 216 217 218 219 220 221
#if CONFIG_NEW_MULTISYMBOL
  is_zeromv =
      aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
#else
  is_zeromv = aom_read(r, ec_ctx->zeromv_prob[mode_ctx], ACCT_STR) == 0;
#endif
  if (is_zeromv) {
222
    if (counts) ++counts->zeromv_mode[mode_ctx][0];
223 224
    return ZEROMV;
  }
225
  if (counts) ++counts->zeromv_mode[mode_ctx][1];
226

227
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
228

229 230 231
  if (ctx & (1 << SKIP_NEARESTMV_OFFSET)) mode_ctx = 6;
  if (ctx & (1 << SKIP_NEARMV_OFFSET)) mode_ctx = 7;
  if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET)) mode_ctx = 8;
232

233 234 235 236 237
#if CONFIG_NEW_MULTISYMBOL
  is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
#else
  is_refmv = aom_read(r, ec_ctx->refmv_prob[mode_ctx], ACCT_STR) == 0;
#endif
238

239
  if (is_refmv) {
240
    if (counts) ++counts->refmv_mode[mode_ctx][0];
241

242 243
    return NEARESTMV;
  } else {
244
    if (counts) ++counts->refmv_mode[mode_ctx][1];
245 246 247 248 249
    return NEARMV;
  }

  // Invalid prediction mode.
  assert(0);
Jingning Han's avatar
Jingning Han committed
250 251
}

252
static void read_drl_idx(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
253 254
                         MB_MODE_INFO *mbmi, aom_reader *r) {
  uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
255 256
  mbmi->ref_mv_idx = 0;

257
#if CONFIG_EXT_INTER
258 259 260 261
#if CONFIG_COMPOUND_SINGLEREF
  if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
      mbmi->mode == SR_NEW_NEWMV) {
#else   // !CONFIG_COMPOUND_SINGLEREF
262
  if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) {
263 264
#endif  // CONFIG_COMPOUND_SINGLEREF
#else   // !CONFIG_EXT_INTER
265
  if (mbmi->mode == NEWMV) {
266
#endif  // CONFIG_EXT_INTER
267 268 269
    int idx;
    for (idx = 0; idx < 2; ++idx) {
      if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xu's avatar
Yaowu Xu committed
270
        uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
271 272 273 274 275 276 277 278
#if CONFIG_NEW_MULTISYMBOL
        int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
#else
        int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
#endif
        mbmi->ref_mv_idx = idx + drl_idx;
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
        if (!drl_idx) return;
279 280 281 282
      }
    }
  }

David Barker's avatar
David Barker committed
283
  if (have_nearmv_in_inter_mode(mbmi->mode)) {
284 285 286 287 288 289
    int idx;
    // Offset the NEARESTMV mode.
    // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
    // mode is factored in.
    for (idx = 1; idx < 3; ++idx) {
      if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
Yaowu Xu's avatar
Yaowu Xu committed
290
        uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
291 292 293 294 295 296 297 298
#if CONFIG_NEW_MULTISYMBOL
        int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
#else
        int drl_idx = aom_read(r, ec_ctx->drl_prob[drl_ctx], ACCT_STR);
#endif
        mbmi->ref_mv_idx = idx + drl_idx - 1;
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][drl_idx];
        if (!drl_idx) return;
299 300 301 302 303
      }
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
304 305
#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
306 307 308 309 310 311 312
                                    MODE_INFO *mi, aom_reader *r) {
  MB_MODE_INFO *mbmi = &mi->mbmi;
  const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
#if CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
      0, xd->global_motion,
#endif  // CONFIG_GLOBAL_MOTION && SEPARATE_GLOBAL_MOTION
      mi);
Yue Chen's avatar
Yue Chen committed
313 314
  int motion_mode;
  FRAME_COUNTS *counts = xd->counts;
Yaowu Xu's avatar
Yaowu Xu committed
315

Yue Chen's avatar
Yue Chen committed
316 317 318 319 320 321 322 323
  if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
  if (last_motion_mode_allowed == OBMC_CAUSAL) {
    motion_mode = aom_read(r, cm->fc->obmc_prob[mbmi->sb_type], ACCT_STR);
    if (counts) ++counts->obmc[mbmi->sb_type][motion_mode];
    return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
  } else {
#endif  // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
324 325 326 327 328
    motion_mode =
        aom_read_tree(r, av1_motion_mode_tree,
                      cm->fc->motion_mode_prob[mbmi->sb_type], ACCT_STR);
    if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
    return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
Yue Chen's avatar
Yue Chen committed
329
#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
330
  }
Yue Chen's avatar
Yue Chen committed
331
#endif  // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
332
}
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

#if CONFIG_NCOBMC_ADAPT_WEIGHT
static void read_ncobmc_mode(AV1_COMMON *cm, MACROBLOCKD *xd, MODE_INFO *mi,
                             NCOBMC_MODE ncobmc_mode[2], aom_reader *r) {
  MB_MODE_INFO *mbmi = &mi->mbmi;
  FRAME_COUNTS *counts = xd->counts;
  ADAPT_OVERLAP_BLOCK ao_block = adapt_overlap_block_lookup[mbmi->sb_type];

  if (ncobmc_mode_allowed(mbmi->sb_type) == NO_OVERLAP ||
      ao_block == ADAPT_OVERLAP_BLOCK_INVALID)
    return;

  ncobmc_mode[0] = aom_read_tree(r, av1_ncobmc_mode_tree,
                                 cm->fc->ncobmc_mode_prob[ao_block], ACCT_STR);
  if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[0]];

  if (mi_size_wide[mbmi->sb_type] != mi_size_high[mbmi->sb_type]) {
    ncobmc_mode[1] = aom_read_tree(
        r, av1_ncobmc_mode_tree, cm->fc->ncobmc_mode_prob[ao_block], ACCT_STR);
    if (counts) ++counts->ncobmc_mode[ao_block][ncobmc_mode[1]];
  }
}
#endif
Yaowu Xu's avatar
Yaowu Xu committed
356 357
#endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION

358
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
359 360
static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
                                                aom_reader *r, int16_t ctx) {
Michael Bebenita's avatar
Michael Bebenita committed
361 362 363
  const int mode =
      aom_read_tree(r, av1_inter_compound_mode_tree,
                    cm->fc->inter_compound_mode_probs[ctx], ACCT_STR);
364 365
  FRAME_COUNTS *counts = xd->counts;

366
  if (counts) ++counts->inter_compound_mode[ctx][mode];
367 368 369 370

  assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
  return NEAREST_NEARESTMV + mode;
}
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

#if CONFIG_COMPOUND_SINGLEREF
static PREDICTION_MODE read_inter_singleref_comp_mode(AV1_COMMON *cm,
                                                      MACROBLOCKD *xd,
                                                      aom_reader *r,
                                                      int16_t ctx) {
  const int mode =
      aom_read_tree(r, av1_inter_singleref_comp_mode_tree,
                    cm->fc->inter_singleref_comp_mode_probs[ctx], ACCT_STR);
  FRAME_COUNTS *counts = xd->counts;

  if (counts) ++counts->inter_singleref_comp_mode[ctx][mode];

  assert(is_inter_singleref_comp_mode(SR_NEAREST_NEARMV + mode));
  return SR_NEAREST_NEARMV + mode;
}
#endif  // CONFIG_COMPOUND_SINGLEREF
388 389
#endif  // CONFIG_EXT_INTER

390
static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
Michael Bebenita's avatar
Michael Bebenita committed
391
  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
Jingning Han's avatar
Jingning Han committed
392 393
}

394
#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
395
static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
396
                               MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
397 398
                               TX_SIZE tx_size, int depth, int blk_row,
                               int blk_col, aom_reader *r) {
399
  int is_split = 0;
400 401
  const int tx_row = blk_row >> 1;
  const int tx_col = blk_col >> 1;
402 403
  const int max_blocks_high = max_block_high(xd, mbmi->sb_type, 0);
  const int max_blocks_wide = max_block_wide(xd, mbmi->sb_type, 0);
404 405
  int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
                                   xd->left_txfm_context + blk_row,
406
                                   mbmi->sb_type, tx_size);
clang-format's avatar
clang-format committed
407
  TX_SIZE(*const inter_tx_size)
408 409 410
  [MAX_MIB_SIZE] =
      (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
411

Jingning Han's avatar
Jingning Han committed
412
  if (depth == MAX_VARTX_DEPTH) {
413 414
    int idx, idy;
    inter_tx_size[0][0] = tx_size;
415 416
    for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
      for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
417 418
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
419
    mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
420
    if (counts) ++counts->txfm_partition[ctx][0];
421 422
    txfm_partition_update(xd->above_txfm_context + blk_col,
                          xd->left_txfm_context + blk_row, tx_size, tx_size);
423 424 425
    return;
  }

Michael Bebenita's avatar
Michael Bebenita committed
426
  is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
427 428

  if (is_split) {
429 430
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
    const int bsl = tx_size_wide_unit[sub_txs];
431
    int i;
432

433
    if (counts) ++counts->txfm_partition[ctx][1];
434

435
    if (tx_size == TX_8X8) {
436
      int idx, idy;
437
      inter_tx_size[0][0] = sub_txs;
438 439
      for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
        for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
440
          inter_tx_size[idy][idx] = inter_tx_size[0][0];
441
      mbmi->tx_size = sub_txs;
442
      mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
443 444
      txfm_partition_update(xd->above_txfm_context + blk_col,
                            xd->left_txfm_context + blk_row, sub_txs, tx_size);
445 446 447 448 449
      return;
    }

    assert(bsl > 0);
    for (i = 0; i < 4; ++i) {
450 451 452
      int offsetr = blk_row + (i >> 1) * bsl;
      int offsetc = blk_col + (i & 0x01) * bsl;
      read_tx_size_vartx(cm, xd, mbmi, counts, sub_txs, depth + 1, offsetr,
453
                         offsetc, r);
454 455
    }
  } else {
456
    int idx, idy;
457
    inter_tx_size[0][0] = tx_size;
458 459
    for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
      for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
460 461
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
462
    mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
463
    if (counts) ++counts->txfm_partition[ctx][0];
464 465
    txfm_partition_update(xd->above_txfm_context + blk_col,
                          xd->left_txfm_context + blk_row, tx_size, tx_size);
466 467 468 469
  }
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
470 471
static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
                                     int tx_size_cat, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
472 473
  FRAME_COUNTS *counts = xd->counts;
  const int ctx = get_tx_size_context(xd);
474 475 476 477 478 479 480
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

481 482
  const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
                                    tx_size_cat + 2, ACCT_STR);
483 484 485 486
  const TX_SIZE tx_size = depth_to_tx_size(depth);
#if CONFIG_RECT_TX
  assert(!is_rect_tx(tx_size));
#endif  // CONFIG_RECT_TX
487
  if (counts) ++counts->tx_size[tx_size_cat][ctx][depth];
488
  return tx_size;
Jingning Han's avatar
Jingning Han committed
489 490
}

491 492 493 494
static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int is_inter,
                            int allow_select_inter, aom_reader *r) {
  const TX_MODE tx_mode = cm->tx_mode;
  const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
495
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
496
#if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_EXT_TX) && CONFIG_RECT_TX
497
  if (bsize > BLOCK_4X4) {
498
#else
499
  if (bsize >= BLOCK_8X8) {
500 501 502 503
#endif  // CONFIG_CB4X4 && CONFIG_VAR_TX
    if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
      const int32_t tx_size_cat = is_inter ? inter_tx_size_cat_lookup[bsize]
                                           : intra_tx_size_cat_lookup[bsize];
504
      const TX_SIZE coded_tx_size =
505
          read_selected_tx_size(cm, xd, tx_size_cat, r);
506
#if CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
507 508
      if (coded_tx_size > max_txsize_lookup[bsize]) {
        assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
509
#if CONFIG_EXT_TX && CONFIG_RECT_TX_EXT
510 511 512 513 514 515 516 517
        if (is_quarter_tx_allowed(xd, &xd->mi[0]->mbmi, is_inter)) {
          int quarter_tx = aom_read(r, cm->fc->quarter_tx_size_prob, ACCT_STR);
          FRAME_COUNTS *counts = xd->counts;

          if (counts) ++counts->quarter_tx_size[quarter_tx];
          return quarter_tx ? quarter_txsize_lookup[bsize]
                            : max_txsize_rect_lookup[bsize];
        }
518
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX_EXT
519

520 521
        return max_txsize_rect_lookup[bsize];
      }
522 523
#else
      assert(coded_tx_size <= max_txsize_lookup[bsize]);
524
#endif  // CONFIG_RECT_TX && (CONFIG_EXT_TX || CONFIG_VAR_TX)
525
      return coded_tx_size;
526
    } else {
527
      return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
528 529
    }
  } else {
530
#if CONFIG_EXT_TX && CONFIG_RECT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
531
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
532 533 534
    return max_txsize_rect_lookup[bsize];
#else
    return TX_4X4;
535
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX
536
  }
Jingning Han's avatar
Jingning Han committed
537 538
}

Yaowu Xu's avatar
Yaowu Xu committed
539
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Jingning Han's avatar
Jingning Han committed
540 541 542 543 544
                              int mi_offset, int x_mis, int y_mis) {
  int x, y, segment_id = INT_MAX;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
545
      segment_id =
Yaowu Xu's avatar
Yaowu Xu committed
546
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
547 548 549 550 551

  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
552
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
553
                           int segment_id) {
Jingning Han's avatar
Jingning Han committed
554 555 556 557 558 559 560 561 562
  int x, y;

  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
      cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
563
static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
564
                                 int mi_offset, int x_mis, int y_mis,
Yaowu Xu's avatar
Yaowu Xu committed
565
                                 aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
566
  struct segmentation *const seg = &cm->seg;
567 568
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
569 570
  int segment_id;

571
  if (!seg->enabled) return 0;  // Default for disabled segmentation
Jingning Han's avatar
Jingning Han committed
572

573
  assert(seg->update_map && !seg->temporal_update);
Jingning Han's avatar
Jingning Han committed
574

575
  segment_id = read_segment_id(r, segp);
576
  if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
577 578 579 580
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
581
static void copy_segment_id(const AV1_COMMON *cm,
582 583 584
                            const uint8_t *last_segment_ids,
                            uint8_t *current_segment_ids, int mi_offset,
                            int x_mis, int y_mis) {
585 586 587 588
  int x, y;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
589 590 591
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
                           : 0;
592 593
}

Yaowu Xu's avatar
Yaowu Xu committed
594 595
static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                 int mi_row, int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
596
  struct segmentation *const seg = &cm->seg;
597 598
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
599 600 601
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  int predicted_segment_id, segment_id;
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
602 603
  const int bw = mi_size_wide[mbmi->sb_type];
  const int bh = mi_size_high[mbmi->sb_type];
Jingning Han's avatar
Jingning Han committed
604 605

  // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xu's avatar
Yaowu Xu committed
606 607
  const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
  const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Jingning Han's avatar
Jingning Han committed
608

609
  if (!seg->enabled) return 0;  // Default for disabled segmentation
Jingning Han's avatar
Jingning Han committed
610

611 612 613 614
  predicted_segment_id = cm->last_frame_seg_map
                             ? dec_get_segment_id(cm, cm->last_frame_seg_map,
                                                  mi_offset, x_mis, y_mis)
                             : 0;
Jingning Han's avatar
Jingning Han committed
615 616 617 618 619 620 621 622

  if (!seg->update_map) {
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
                    mi_offset, x_mis, y_mis);
    return predicted_segment_id;
  }

  if (seg->temporal_update) {
Yaowu Xu's avatar
Yaowu Xu committed
623 624
    const int ctx = av1_get_pred_context_seg_id(xd);
    const aom_prob pred_prob = segp->pred_probs[ctx];
Michael Bebenita's avatar
Michael Bebenita committed
625
    mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
626
    if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
627 628 629 630
    if (mbmi->seg_id_predicted) {
      segment_id = predicted_segment_id;
    } else {
      segment_id = read_segment_id(r, segp);
631
      if (counts) ++counts->seg.tree_mispred[segment_id];
632
    }
Jingning Han's avatar
Jingning Han committed
633
  } else {
634
    segment_id = read_segment_id(r, segp);
635
    if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
636 637 638 639 640
  }
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
641 642
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
                     aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
643 644 645
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
646
    const int ctx = av1_get_skip_context(xd);
647 648 649 650
#if CONFIG_NEW_MULTISYMBOL
    FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
    const int skip = aom_read_symbol(r, ec_ctx->skip_cdfs[ctx], 2, ACCT_STR);
#else
Michael Bebenita's avatar
Michael Bebenita committed
651
    const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
652
#endif
Jingning Han's avatar
Jingning Han committed
653
    FRAME_COUNTS *counts = xd->counts;
654
    if (counts) ++counts->skip[ctx][skip];
Jingning Han's avatar
Jingning Han committed
655 656 657 658
    return skip;
  }
}

659
#if CONFIG_PALETTE
660 661
#if CONFIG_PALETTE_DELTA_ENCODING
static int uint16_compare(const void *a, const void *b) {
662 663
  const uint16_t va = *(const uint16_t *)a;
  const uint16_t vb = *(const uint16_t *)b;
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
  return va - vb;
}

static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
                                  PALETTE_MODE_INFO *const pmi, aom_reader *r) {
  uint16_t color_cache[2 * PALETTE_MAX_SIZE];
  const MODE_INFO *const above_mi = xd->above_mi;
  const MODE_INFO *const left_mi = xd->left_mi;
  const int n_cache = av1_get_palette_cache(above_mi, left_mi, 0, color_cache);
  const int n = pmi->palette_size[0];
  int idx = 0;
  for (int i = 0; i < n_cache && idx < n; ++i)
    if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
  if (idx < n) {
    pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
    if (idx < n) {
      const int min_bits = bit_depth - 3;
      int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
      int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
      for (; idx < n; ++idx) {
        const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
        pmi->palette_colors[idx] = pmi->palette_colors[idx - 1] + delta;
        range -= delta;
        bits = AOMMIN(bits, av1_ceil_log2(range));
      }
    }
  }
  qsort(pmi->palette_colors, n, sizeof(pmi->palette_colors[0]), uint16_compare);
}

static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
                                   PALETTE_MODE_INFO *const pmi,
                                   aom_reader *r) {
  const int n = pmi->palette_size[1];
  // U channel colors.
  uint16_t color_cache[2 * PALETTE_MAX_SIZE];
  const MODE_INFO *const above_mi = xd->above_mi;
  const MODE_INFO *const left_mi = xd->left_mi;
  const int n_cache = av1_get_palette_cache(above_mi, left_mi, 1, color_cache);
  int idx = PALETTE_MAX_SIZE;
  for (int i = 0; i < n_cache && idx < PALETTE_MAX_SIZE + n; ++i)
    if (aom_read_bit(r, ACCT_STR)) pmi->palette_colors[idx++] = color_cache[i];
  if (idx < PALETTE_MAX_SIZE + n) {
    pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
    if (idx < PALETTE_MAX_SIZE + n) {
      const int min_bits = bit_depth - 3;
      int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
      int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
      for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
        const int delta = aom_read_literal(r, bits, ACCT_STR);
        pmi->palette_colors[idx] = pmi->palette_colors[idx - 1] + delta;
        range -= delta;
        bits = AOMMIN(bits, av1_ceil_log2(range));
      }
    }
  }
  qsort(pmi->palette_colors + PALETTE_MAX_SIZE, n,
        sizeof(pmi->palette_colors[0]), uint16_compare);

  // V channel colors.
  if (aom_read_bit(r, ACCT_STR)) {  // Delta encoding.
    const int min_bits_v = bit_depth - 4;
    const int max_val = 1 << bit_depth;
    int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
    pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
        aom_read_literal(r, bit_depth, ACCT_STR);
    for (int i = 1; i < n; ++i) {
      int delta = aom_read_literal(r, bits, ACCT_STR);
      if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
      int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
      if (val < 0) val += max_val;
      if (val >= max_val) val -= max_val;
      pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
    }
  } else {
    for (int i = 0; i < n; ++i) {
      pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
          aom_read_literal(r, bit_depth, ACCT_STR);
    }
  }
}
#endif  // CONFIG_PALETTE_DELTA_ENCODING

Yaowu Xu's avatar
Yaowu Xu committed
747 748
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                   aom_reader *r) {
hui su's avatar
hui su committed
749 750
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
751
  const MODE_INFO *const above_mi = xd->above_mi;
752
  const MODE_INFO *const left_mi = xd->left_mi;
hui su's avatar
hui su committed
753
  const BLOCK_SIZE bsize = mbmi->sb_type;
754
  int n;
755 756 757
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;

  if (mbmi->mode == DC_PRED) {
758
    int palette_y_mode_ctx = 0;
759
    if (above_mi)
760 761
      palette_y_mode_ctx +=
          (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
762
    if (left_mi)
763 764 765 766 767
      palette_y_mode_ctx +=
          (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
    if (aom_read(r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8]
                                                   [palette_y_mode_ctx],
                 ACCT_STR)) {
768
      pmi->palette_size[0] =
Yaowu Xu's avatar
Yaowu Xu committed
769
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
770 771
                        av1_default_palette_y_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
772
          2;
773
      n = pmi->palette_size[0];
hui su's avatar
hui su committed
774
#if CONFIG_PALETTE_DELTA_ENCODING
775
      read_palette_colors_y(xd, cm->bit_depth, pmi, r);
hui su's avatar
hui su committed
776
#else
777
      int i;
778
      for (i = 0; i < n; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
779
        pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui su's avatar
hui su committed
780
#endif  // CONFIG_PALETTE_DELTA_ENCODING
781 782 783 784
      xd->plane[0].color_index_map[0] = read_uniform(r, n);
      assert(xd->plane[0].color_index_map[0] < n);
    }
  }
hui su's avatar
hui su committed
785

786
  if (mbmi->uv_mode == DC_PRED) {
787 788
    const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
    if (aom_read(r, av1_default_palette_uv_mode_prob[palette_uv_mode_ctx],
Michael Bebenita's avatar
Michael Bebenita committed
789
                 ACCT_STR)) {
790
      pmi->palette_size[1] =
Yaowu Xu's avatar
Yaowu Xu committed
791
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
792 793
                        av1_default_palette_uv_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
794
          2;
795
      n = pmi->palette_size[1];
hui su's avatar
hui su committed
796
#if CONFIG_PALETTE_DELTA_ENCODING
797
      read_palette_colors_uv(xd, cm->bit_depth, pmi, r);
hui su's avatar
hui su committed
798
#else
799
      int i;
800 801
      for (i = 0; i < n; ++i) {
        pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita's avatar
Michael Bebenita committed
802
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
803
        pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita's avatar
Michael Bebenita committed
804
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
805
      }
hui su's avatar
hui su committed
806
#endif  // CONFIG_PALETTE_DELTA_ENCODING
807 808 809
      xd->plane[1].color_index_map[0] = read_uniform(r, n);
      assert(xd->plane[1].color_index_map[0] < n);
    }
hui su's avatar
hui su committed
810 811
  }
}
812
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
813

814 815
#if CONFIG_FILTER_INTRA
static void read_filter_intra_mode_info(AV1_COMMON *const cm,
816 817
                                        MACROBLOCKD *const xd, int mi_row,
                                        int mi_col, aom_reader *r) {
hui su's avatar
hui su committed
818 819 820
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  FRAME_COUNTS *counts = xd->counts;
821 822
  FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
      &mbmi->filter_intra_mode_info;
hui su's avatar
hui su committed
823

824 825 826 827 828
  if (mbmi->mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[0] == 0
#endif  // CONFIG_PALETTE
      ) {
829 830 831 832
    filter_intra_mode_info->use_filter_intra_mode[0] =
        aom_read(r, cm->fc->filter_intra_probs[0], ACCT_STR);
    if (filter_intra_mode_info->use_filter_intra_mode[0]) {
      filter_intra_mode_info->filter_intra_mode[0] =
hui su's avatar
hui su committed
833
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
834
    }
835
    if (counts) {
clang-format's avatar
clang-format committed
836 837
      ++counts
            ->filter_intra[0][filter_intra_mode_info->use_filter_intra_mode[0]];
838
    }
hui su's avatar
hui su committed
839
  }
840 841 842 843 844 845

#if CONFIG_CB4X4
  if (!is_chroma_reference(mi_row, mi_col, mbmi->sb_type,
                           xd->plane[1].subsampling_x,
                           xd->plane[1].subsampling_y))
    return;
846 847 848 849
#else
  (void)mi_row;
  (void)mi_col;
#endif  // CONFIG_CB4X4
850

851 852 853 854 855
  if (mbmi->uv_mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[1] == 0
#endif  // CONFIG_PALETTE
      ) {
856 857 858 859
    filter_intra_mode_info->use_filter_intra_mode[1] =
        aom_read(r, cm->fc->filter_intra_probs[1], ACCT_STR);
    if (filter_intra_mode_info->use_filter_intra_mode[1]) {
      filter_intra_mode_info->filter_intra_mode[1] =
hui su's avatar
hui su committed
860
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
861
    }
862
    if (counts) {
clang-format's avatar
clang-format committed
863 864
      ++counts
            ->filter_intra[1][filter_intra_mode_info->use_filter_intra_mode[1]];
865
    }
hui su's avatar
hui su committed
866 867
  }
}
868
#endif  // CONFIG_FILTER_INTRA
869

870
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
871 872
static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                  aom_reader *r) {
873 874
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
hui su's avatar
hui su committed
875
#if CONFIG_INTRA_INTERP
876 877 878 879 880
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
#else
  FRAME_CONTEXT *const ec_ctx = cm->fc;
#endif  // CONFIG_EC_ADAPT
Yaowu Xu's avatar
Yaowu Xu committed
881
  const int ctx = av1_get_pred_context_intra_interp(xd);
882
  int p_angle;
hui su's avatar
hui su committed
883
#endif  // CONFIG_INTRA_INTERP
884

hui su's avatar
hui su committed
885
  (void)cm;
886 887 888 889 890

  mbmi->angle_delta[0] = 0;
  mbmi->angle_delta[1] = 0;

  if (!av1_use_angle_delta(bsize)) return;
891

hui su's avatar
hui su committed
892
  if (av1_is_directional_mode(mbmi->mode, bsize)) {
893
    mbmi->angle_delta[0] =
hui su's avatar
hui su committed
894
        read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
hui su's avatar
hui su committed
895
#if CONFIG_INTRA_INTERP
hui su's avatar
hui su committed
896
    p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xu's avatar
Yaowu Xu committed
897
    if (av1_is_intra_filter_switchable(p_angle)) {
898
      FRAME_COUNTS *counts = xd->counts;
899 900
      mbmi->intra_filter = aom_read_symbol(r, ec_ctx->intra_filter_cdf[ctx],
                                           INTRA_FILTERS, ACCT_STR);
901
      if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
902 903 904
    } else {
      mbmi->intra_filter = INTRA_FILTER_LINEAR;
    }
hui su's avatar
hui su committed
905
#endif  // CONFIG_INTRA_INTERP
906 907
  }

hui su's avatar
hui su committed
908
  if (av1_is_directional_mode(mbmi->uv_mode, bsize)) {
909
    mbmi->angle_delta[1] =
hui su's avatar
hui su committed
910
        read_uniform(r, 2 * MAX_ANGLE_DELTA + 1) - MAX_ANGLE_DELTA;
911 912
  }
}
hui su's avatar
hui su committed
913 914
#endif  // CONFIG_EXT_INTRA

915
void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
916
#if CONFIG_SUPERTX
917
                      int supertx_enabled,
Jingning Han's avatar
Jingning Han committed
918
#endif
Angie Chiang's avatar
Angie Chiang committed
919
#if CONFIG_TXK_SEL
920
                      int block, int plane, TX_SIZE tx_size,
921 922 923
#endif
                      aom_reader *r) {
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
Jingning Han's avatar
Jingning Han committed
924
  const int inter_block = is_inter_block(mbmi);
925
#if !CONFIG_TXK_SEL
926 927 928
#if CONFIG_VAR_TX
  const TX_SIZE tx_size = inter_block ? mbmi->min_tx_size : mbmi->tx_size;
#else
Jingning Han's avatar
Jingning Han committed
929
  const TX_SIZE tx_size = mbmi->tx_size;
930
#endif
931
#endif  // !CONFIG_TXK_SEL
932 933 934 935 936 937
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

Angie Chiang's avatar
Angie Chiang committed
938
#if !CONFIG_TXK_SEL
939 940
  TX_TYPE *tx_type = &mbmi->tx_type;
#else
941 942
  // only y plane's tx_type is transmitted
  if (plane > 0) return;
943 944 945
  TX_TYPE *tx_type = &mbmi->txk_type[block];
#endif

Jingning Han's avatar
Jingning Han committed
946 947
  if (!FIXED_TX_TYPE) {
#if CONFIG_EXT_TX
948
    const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
949 950
    if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
                         cm->reduced_tx_set_used) > 1 &&
951 952 953
        ((!cm->seg.enabled && cm->base_qindex > 0) ||
         (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
        !mbmi->skip &&
Jingning Han's avatar
Jingning Han committed
954 955 956 957
#if CONFIG_SUPERTX
        !supertx_enabled &&
#endif  // CONFIG_SUPERTX
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
958 959
      const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
                                      cm->reduced_tx_set_used);
960 961 962
      // eset == 0 should correspond to a set with only DCT_DCT and
      // there is no need to read the tx_type
      assert(eset != 0);
Jingning Han's avatar
Jingning Han committed
963 964 965
      FRAME_COUNTS *counts = xd->counts;

      if (inter_block) {
966 967 968 969
        *tx_type = av1_ext_tx_inter_inv[eset][aom_read_symbol(
            r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
            ext_tx_cnt_inter[eset], ACCT_STR)];
        if (counts) ++counts->inter_ext_tx[eset][square_tx_size][*tx_type];
Jingning Han's avatar
Jingning Han committed
970
      } else if (ALLOW_INTRA_EXT_TX) {
971 972 973 974 975
        *tx_type = av1_ext_tx_intra_inv[eset][aom_read_symbol(
            r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][mbmi->mode],
            ext_tx_cnt_intra[eset], ACCT_STR)];
        if (counts)
          ++counts->intra_ext_tx[eset][square_tx_size][mbmi->mode][*tx_type];
Jingning Han's avatar
Jingning Han committed
976 977
      }
    } else {
978
      *tx_type = DCT_DCT;
Jingning Han's avatar
Jingning Han committed
979 980
    }
#else
981 982 983 984 985

    if (tx_size < TX_32X32 &&
        ((!cm->seg.enabled && cm->base_qindex > 0) ||
         (cm->seg.enabled && xd->qindex[mbmi->segment_id] > 0)) &&
        !mbmi->skip &&
Jingning Han's avatar
Jingning Han committed
986 987 988 989 990
#if CONFIG_SUPERTX
        !supertx_enabled &&
#endif  // CONFIG_SUPERTX
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
      FRAME_COUNTS *counts = xd->counts;
991

Jingning Han's avatar
Jingning Han committed
992
      if (inter_block) {
993
        *tx_type = av1_ext_tx_inv[aom_read_symbol(
994
            r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
995
        if (counts) ++counts->inter_ext_tx[tx_size][*tx_type];
Jingning Han's avatar
Jingning Han committed
996 997
      } else {
        const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
998
        *tx_type = av1_ext_tx_inv[aom_read_symbol(
999
            r, ec_ctx->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
Jingning Han's avatar
Jingning Han committed
1000
            ACCT_STR)];
1001
        if (counts) ++counts->intra_ext_tx[tx_size][tx_type_nom][*tx_type];
Jingning Han's avatar
Jingning Han committed
1002 1003
      }
    } else {
1004
      *tx_type = DCT_DCT;
Jingning Han's avatar
Jingning Han committed
1005 1006 1007 1008 1009
    }
#endif  // CONFIG_EXT_TX
  }
}

Alex Converse's avatar
Alex Converse committed
1010 1011 1012
#if CONFIG_INTRABC
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
                           nmv_context *ctx, nmv_context_counts *counts,
Alex Converse's avatar
Alex Converse committed
1013
                           MvSubpelPrecision precision);
Alex Converse's avatar
Alex Converse committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

static INLINE int is_mv_valid(const MV *mv);

static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
                            const int_mv *ref_mv, int mi_row, int mi_col,
                            BLOCK_SIZE bsize, aom_reader *r) {
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif
  FRAME_COUNTS *counts = xd->counts;
  nmv_context_counts *const dv_counts = counts ? &counts->dv : NULL;
Alex Converse's avatar
Alex Converse committed
1028 1029
  read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, dv_counts,
          MV_SUBPEL_NONE);