decodemv.c 75.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
#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
36
static INLINE int read_uniform(aom_reader *r, int n) {
hui su's avatar
hui su committed
37 38
  int l = get_unsigned_bits(n);
  int m = (1 << l) - n;
Michael Bebenita's avatar
Michael Bebenita committed
39
  int v = aom_read_literal(r, l - 1, ACCT_STR);
hui su's avatar
hui su committed
40 41 42 43 44 45

  assert(l != 0);

  if (v < m)
    return v;
  else
Michael Bebenita's avatar
Michael Bebenita committed
46
    return (v << 1) - m + aom_read_literal(r, 1, ACCT_STR);
hui su's avatar
hui su committed
47
}
48
#endif  // CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
hui su's avatar
hui su committed
49

50
#if CONFIG_EC_MULTISYMBOL
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)];
}
55
#else
Yaowu Xu's avatar
Yaowu Xu committed
56
static PREDICTION_MODE read_intra_mode(aom_reader *r, const aom_prob *p) {
Michael Bebenita's avatar
Michael Bebenita committed
57
  return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p, ACCT_STR);
Jingning Han's avatar
Jingning Han committed
58
}
59
#endif
Jingning Han's avatar
Jingning Han committed
60

61 62 63 64 65 66 67 68 69
#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
70 71 72 73 74 75 76 77
  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
78 79

  if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_q_flag) {
Thomas Davies's avatar
Thomas Davies committed
80 81
#if !CONFIG_EC_MULTISYMBOL
    int bit = 1;
82 83
    abs = 0;
    while (abs < DELTA_Q_SMALL && bit) {
Thomas Davies's avatar
Thomas Davies committed
84
      bit = aom_read(r, ec_ctx->delta_q_prob[abs], ACCT_STR);
85 86
      abs += bit;
    }
Thomas Davies's avatar
Thomas Davies committed
87 88 89 90 91 92 93 94 95 96
#else
    abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
#endif
    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) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
      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;
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
114 115
static PREDICTION_MODE read_intra_mode_y(AV1_COMMON *cm, MACROBLOCKD *xd,
                                         aom_reader *r, int size_group) {
116 117 118 119 120 121
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#elif CONFIG_EC_MULTISYMBOL
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

Jingning Han's avatar
Jingning Han committed
122
  const PREDICTION_MODE y_mode =
123
#if CONFIG_EC_MULTISYMBOL
124
      read_intra_mode(r, ec_ctx->y_mode_cdf[size_group]);
125
#else
Jingning Han's avatar
Jingning Han committed
126
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
127
#endif
Jingning Han's avatar
Jingning Han committed
128
  FRAME_COUNTS *counts = xd->counts;
129 130 131
#if CONFIG_EC_ADAPT
  (void)cm;
#endif
132
  if (counts) ++counts->y_mode[size_group][y_mode];
Jingning Han's avatar
Jingning Han committed
133 134 135
  return y_mode;
}

Yaowu Xu's avatar
Yaowu Xu committed
136 137
static PREDICTION_MODE read_intra_mode_uv(AV1_COMMON *cm, MACROBLOCKD *xd,
                                          aom_reader *r,
Jingning Han's avatar
Jingning Han committed
138
                                          PREDICTION_MODE y_mode) {
139 140 141 142 143 144
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#elif CONFIG_EC_MULTISYMBOL
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

145
  const PREDICTION_MODE uv_mode =
146
#if CONFIG_EC_MULTISYMBOL
147
      read_intra_mode(r, ec_ctx->uv_mode_cdf[y_mode]);
148
#else
149
      read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
150
#endif
Jingning Han's avatar
Jingning Han committed
151
  FRAME_COUNTS *counts = xd->counts;
152 153 154
#if CONFIG_EC_ADAPT
  (void)cm;
#endif
155
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
Jingning Han's avatar
Jingning Han committed
156 157 158
  return uv_mode;
}

159
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
160 161 162
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
163 164
      r, av1_interintra_mode_tree, cm->fc->interintra_mode_prob[size_group],
      ACCT_STR);
165
  FRAME_COUNTS *counts = xd->counts;
166
  if (counts) ++counts->interintra_mode[size_group][ii_mode];
167 168 169 170
  return ii_mode;
}
#endif  // CONFIG_EXT_INTER

171
static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, MACROBLOCKD *xd,
Yue Chen's avatar
Yue Chen committed
172 173 174
#if CONFIG_REF_MV && CONFIG_EXT_INTER
                                       MB_MODE_INFO *mbmi,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
175
                                       aom_reader *r, int16_t ctx) {
176

177 178
#if CONFIG_REF_MV
  FRAME_COUNTS *counts = xd->counts;
179
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
180
  aom_prob mode_prob = ec_ctx->newmv_prob[mode_ctx];
181

Michael Bebenita's avatar
Michael Bebenita committed
182
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
183
    if (counts) ++counts->newmv_mode[mode_ctx][0];
Yue Chen's avatar
Yue Chen committed
184 185 186 187

#if CONFIG_EXT_INTER
    if (has_second_ref(mbmi)) {
#endif  // CONFIG_EXT_INTER
188
      return NEWMV;
Yue Chen's avatar
Yue Chen committed
189 190
#if CONFIG_EXT_INTER
    } else {
191
      mode_prob = ec_ctx->new2mv_prob;
Michael Bebenita's avatar
Michael Bebenita committed
192
      if (aom_read(r, mode_prob, ACCT_STR) == 0) {
193
        if (counts) ++counts->new2mv_mode[0];
Yue Chen's avatar
Yue Chen committed
194 195
        return NEWMV;
      } else {
196
        if (counts) ++counts->new2mv_mode[1];
Yue Chen's avatar
Yue Chen committed
197 198 199 200
        return NEWFROMNEARMV;
      }
    }
#endif  // CONFIG_EXT_INTER
201
  }
202
  if (counts) ++counts->newmv_mode[mode_ctx][1];
203

204
  if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
205

206
  mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
207

208
  mode_prob = ec_ctx->zeromv_prob[mode_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
209
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
210
    if (counts) ++counts->zeromv_mode[mode_ctx][0];
211 212
    return ZEROMV;
  }
213
  if (counts) ++counts->zeromv_mode[mode_ctx][1];
214

215
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
216

217 218 219
  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;
220

221
  mode_prob = ec_ctx->refmv_prob[mode_ctx];
222

Michael Bebenita's avatar
Michael Bebenita committed
223
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
224
    if (counts) ++counts->refmv_mode[mode_ctx][0];
225

226 227
    return NEARESTMV;
  } else {
228
    if (counts) ++counts->refmv_mode[mode_ctx][1];
229 230 231 232 233
    return NEARMV;
  }

  // Invalid prediction mode.
  assert(0);
234
#else
235
#if CONFIG_EC_MULTISYMBOL
236
  const int mode = av1_inter_mode_inv[aom_read_symbol(
237
      r, ec_ctx->inter_mode_cdf[ctx], INTER_MODES, ACCT_STR)];
238
#else
Michael Bebenita's avatar
Michael Bebenita committed
239
  const int mode = aom_read_tree(r, av1_inter_mode_tree,
240
                                 ec_ctx->inter_mode_probs[ctx], ACCT_STR);
241
#endif
Jingning Han's avatar
Jingning Han committed
242
  FRAME_COUNTS *counts = xd->counts;
243
  if (counts) ++counts->inter_mode[ctx][mode];
Jingning Han's avatar
Jingning Han committed
244 245

  return NEARESTMV + mode;
246
#endif
Jingning Han's avatar
Jingning Han committed
247 248
}

249
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
250 251 252
static void read_drl_idx(const AV1_COMMON *cm, MACROBLOCKD *xd,
                         MB_MODE_INFO *mbmi, aom_reader *r) {
  uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
253 254
  mbmi->ref_mv_idx = 0;

255 256 257 258
  if (mbmi->mode == NEWMV) {
    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
259 260
        uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
        aom_prob drl_prob = cm->fc->drl_prob[drl_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
261
        if (!aom_read(r, drl_prob, ACCT_STR)) {
262
          mbmi->ref_mv_idx = idx;
263
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
264 265 266
          return;
        }
        mbmi->ref_mv_idx = idx + 1;
267
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
268 269 270 271
      }
    }
  }

272 273 274 275 276 277 278
  if (mbmi->mode == NEARMV) {
    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
279 280
        uint8_t drl_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], idx);
        aom_prob drl_prob = cm->fc->drl_prob[drl_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
281
        if (!aom_read(r, drl_prob, ACCT_STR)) {
282
          mbmi->ref_mv_idx = idx - 1;
283
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
284 285
          return;
        }
286
        mbmi->ref_mv_idx = idx;
287
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
288 289 290 291 292 293
      }
    }
  }
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
294 295 296
#if CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
                                    MB_MODE_INFO *mbmi, aom_reader *r) {
Yue Chen's avatar
Yue Chen committed
297 298 299
  MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(mbmi);
  int motion_mode;
  FRAME_COUNTS *counts = xd->counts;
Yaowu Xu's avatar
Yaowu Xu committed
300

Yue Chen's avatar
Yue Chen committed
301 302 303 304 305 306 307 308
  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
309 310 311 312 313
    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
314
#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
315
  }
Yue Chen's avatar
Yue Chen committed
316
#endif  // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
317 318 319
}
#endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION

320
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
321 322
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
323 324 325
  const int mode =
      aom_read_tree(r, av1_inter_compound_mode_tree,
                    cm->fc->inter_compound_mode_probs[ctx], ACCT_STR);
326 327
  FRAME_COUNTS *counts = xd->counts;

328
  if (counts) ++counts->inter_compound_mode[ctx][mode];
329 330 331 332 333 334

  assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
  return NEAREST_NEARESTMV + mode;
}
#endif  // CONFIG_EXT_INTER

335
static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
336
#if CONFIG_EC_MULTISYMBOL
Michael Bebenita's avatar
Michael Bebenita committed
337
  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
338
#else
Michael Bebenita's avatar
Michael Bebenita committed
339
  return aom_read_tree(r, av1_segment_tree, segp->tree_probs, ACCT_STR);
340
#endif
Jingning Han's avatar
Jingning Han committed
341 342
}

343
#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
344
static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
345
                               MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
346 347
                               TX_SIZE tx_size, int depth, int blk_row,
                               int blk_col, aom_reader *r) {
348
  int is_split = 0;
349 350
  const int tx_row = blk_row >> 1;
  const int tx_col = blk_col >> 1;
351 352
  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);
353
  int ctx = txfm_partition_context(xd->above_txfm_context + tx_col,
354 355
                                   xd->left_txfm_context + tx_row,
                                   mbmi->sb_type, tx_size);
clang-format's avatar
clang-format committed
356
  TX_SIZE(*const inter_tx_size)
357 358 359
  [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;
360

Jingning Han's avatar
Jingning Han committed
361
  if (depth == MAX_VARTX_DEPTH) {
362 363
    int idx, idy;
    inter_tx_size[0][0] = tx_size;
364 365
    for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
      for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
366 367
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
368
    mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
369 370
    if (counts) ++counts->txfm_partition[ctx][0];
    txfm_partition_update(xd->above_txfm_context + tx_col,
371
                          xd->left_txfm_context + tx_row, tx_size, tx_size);
372 373 374
    return;
  }

Michael Bebenita's avatar
Michael Bebenita committed
375
  is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
376 377

  if (is_split) {
378 379
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
    const int bsl = tx_size_wide_unit[sub_txs];
380
    int i;
381

382
    if (counts) ++counts->txfm_partition[ctx][1];
383

384
    if (tx_size == TX_8X8) {
385
      int idx, idy;
386
      inter_tx_size[0][0] = sub_txs;
387 388
      for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
        for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
389
          inter_tx_size[idy][idx] = inter_tx_size[0][0];
390
      mbmi->tx_size = sub_txs;
391
      mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
392
      txfm_partition_update(xd->above_txfm_context + tx_col,
393
                            xd->left_txfm_context + tx_row, sub_txs, tx_size);
394 395 396 397 398
      return;
    }

    assert(bsl > 0);
    for (i = 0; i < 4; ++i) {
399 400 401
      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,
402
                         offsetc, r);
403 404
    }
  } else {
405
    int idx, idy;
406
    inter_tx_size[0][0] = tx_size;
407 408
    for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
      for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
409 410
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
411
    mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
412
    if (counts) ++counts->txfm_partition[ctx][0];
413
    txfm_partition_update(xd->above_txfm_context + tx_col,
414
                          xd->left_txfm_context + tx_row, tx_size, tx_size);
415 416 417 418
  }
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
419 420
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
421 422
  FRAME_COUNTS *counts = xd->counts;
  const int ctx = get_tx_size_context(xd);
423 424 425 426 427 428 429
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
  (void)cm;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

430
  const int depth =
431
#if CONFIG_EC_MULTISYMBOL
432
      aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx], tx_size_cat + 2,
433 434
                      ACCT_STR);
#else
435
      aom_read_tree(r, av1_tx_size_tree[tx_size_cat],
436
                    ec_ctx->tx_size_probs[tx_size_cat][ctx], ACCT_STR);
437
#endif
438 439 440 441
  const TX_SIZE tx_size = depth_to_tx_size(depth);
#if CONFIG_RECT_TX
  assert(!is_rect_tx(tx_size));
#endif  // CONFIG_RECT_TX
442
  if (counts) ++counts->tx_size[tx_size_cat][ctx][depth];
443
  return tx_size;
Jingning Han's avatar
Jingning Han committed
444 445
}

446 447 448 449
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;
450
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
451
#if CONFIG_CB4X4 && (CONFIG_VAR_TX || CONFIG_RECT_TX)
452
#if CONFIG_RECT_TX
453
  if (bsize > BLOCK_4X4) {
454 455 456
#else
  if (bsize >= BLOCK_8X8 || (bsize > BLOCK_4X4 && is_inter)) {
#endif  // CONFIG_RECT_TX
457
#else
458
  if (bsize >= BLOCK_8X8) {
459 460 461 462
#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];
463
      const TX_SIZE coded_tx_size =
464
          read_selected_tx_size(cm, xd, tx_size_cat, r);
465
#if CONFIG_EXT_TX && CONFIG_RECT_TX
466 467 468 469
      if (coded_tx_size > max_txsize_lookup[bsize]) {
        assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
        return max_txsize_rect_lookup[bsize];
      }
470 471 472
#else
      assert(coded_tx_size <= max_txsize_lookup[bsize]);
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX
473
      return coded_tx_size;
474
    } else {
475
      return tx_size_from_tx_mode(bsize, tx_mode, is_inter);
476 477
    }
  } else {
478
#if CONFIG_EXT_TX && CONFIG_RECT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
479
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
480 481 482
    return max_txsize_rect_lookup[bsize];
#else
    return TX_4X4;
483
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX
484
  }
Jingning Han's avatar
Jingning Han committed
485 486
}

Yaowu Xu's avatar
Yaowu Xu committed
487
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Jingning Han's avatar
Jingning Han committed
488 489 490 491 492
                              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++)
493
      segment_id =
Yaowu Xu's avatar
Yaowu Xu committed
494
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
495 496 497 498 499

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

Yaowu Xu's avatar
Yaowu Xu committed
500
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
501
                           int segment_id) {
Jingning Han's avatar
Jingning Han committed
502 503 504 505 506 507 508 509 510
  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
511
static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
512
                                 int mi_offset, int x_mis, int y_mis,
Yaowu Xu's avatar
Yaowu Xu committed
513
                                 aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
514
  struct segmentation *const seg = &cm->seg;
515 516
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
517 518
  int segment_id;

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

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

523
  segment_id = read_segment_id(r, segp);
524
  if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
525 526 527 528
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
529
static void copy_segment_id(const AV1_COMMON *cm,
530 531 532
                            const uint8_t *last_segment_ids,
                            uint8_t *current_segment_ids, int mi_offset,
                            int x_mis, int y_mis) {
533 534 535 536
  int x, y;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
537 538 539
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
                           : 0;
540 541
}

Yaowu Xu's avatar
Yaowu Xu committed
542 543
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
544
  struct segmentation *const seg = &cm->seg;
545 546
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
547 548 549
  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;
550 551
  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
552 553

  // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xu's avatar
Yaowu Xu committed
554 555
  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
556

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

559 560 561 562
  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
563 564 565 566 567 568 569 570

  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
571 572
    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
573
    mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
574
    if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
575 576 577 578
    if (mbmi->seg_id_predicted) {
      segment_id = predicted_segment_id;
    } else {
      segment_id = read_segment_id(r, segp);
579
      if (counts) ++counts->seg.tree_mispred[segment_id];
580
    }
Jingning Han's avatar
Jingning Han committed
581
  } else {
582
    segment_id = read_segment_id(r, segp);
583
    if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
584 585 586 587 588
  }
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
589 590
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
                     aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
591 592 593
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
594
    const int ctx = av1_get_skip_context(xd);
Michael Bebenita's avatar
Michael Bebenita committed
595
    const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
596
    FRAME_COUNTS *counts = xd->counts;
597
    if (counts) ++counts->skip[ctx][skip];
Jingning Han's avatar
Jingning Han committed
598 599 600 601
    return skip;
  }
}

602
#if CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
603 604
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                   aom_reader *r) {
hui su's avatar
hui su committed
605 606
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
607
  const MODE_INFO *const above_mi = xd->above_mi;
608
  const MODE_INFO *const left_mi = xd->left_mi;
hui su's avatar
hui su committed
609
  const BLOCK_SIZE bsize = mbmi->sb_type;
610
  int i, n;
611 612 613
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;

  if (mbmi->mode == DC_PRED) {
614
    int palette_y_mode_ctx = 0;
615
    if (above_mi)
616 617
      palette_y_mode_ctx +=
          (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
618
    if (left_mi)
619 620 621 622 623
      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)) {
624
      pmi->palette_size[0] =
Yaowu Xu's avatar
Yaowu Xu committed
625
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
626 627
                        av1_default_palette_y_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
628
          2;
629 630
      n = pmi->palette_size[0];
      for (i = 0; i < n; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
631
        pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui su's avatar
hui su committed
632

633 634 635 636
      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
637

638
  if (mbmi->uv_mode == DC_PRED) {
639 640
    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
641
                 ACCT_STR)) {
642
      pmi->palette_size[1] =
Yaowu Xu's avatar
Yaowu Xu committed
643
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
644 645
                        av1_default_palette_uv_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
646
          2;
647 648 649
      n = pmi->palette_size[1];
      for (i = 0; i < n; ++i) {
        pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Michael Bebenita's avatar
Michael Bebenita committed
650
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
651
        pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita's avatar
Michael Bebenita committed
652
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
653 654 655 656
      }
      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
657 658
  }
}
659
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
660

661 662 663
#if CONFIG_FILTER_INTRA
static void read_filter_intra_mode_info(AV1_COMMON *const cm,
                                        MACROBLOCKD *const xd, aom_reader *r) {
hui su's avatar
hui su committed
664 665 666
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  FRAME_COUNTS *counts = xd->counts;
667 668
  FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
      &mbmi->filter_intra_mode_info;
hui su's avatar
hui su committed
669

670 671 672 673 674
  if (mbmi->mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[0] == 0
#endif  // CONFIG_PALETTE
      ) {
675 676 677 678
    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
679
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
680
    }
681
    if (counts) {
clang-format's avatar
clang-format committed
682 683
      ++counts
            ->filter_intra[0][filter_intra_mode_info->use_filter_intra_mode[0]];
684
    }
hui su's avatar
hui su committed
685
  }
686 687 688 689 690
  if (mbmi->uv_mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[1] == 0
#endif  // CONFIG_PALETTE
      ) {
691 692 693 694
    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
695
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
696
    }
697
    if (counts) {
clang-format's avatar
clang-format committed
698 699
      ++counts
            ->filter_intra[1][filter_intra_mode_info->use_filter_intra_mode[1]];
700
    }
hui su's avatar
hui su committed
701 702
  }
}
703
#endif  // CONFIG_FILTER_INTRA
704

705
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
706 707
static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                  aom_reader *r) {
708 709
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
hui su's avatar
hui su committed
710
#if CONFIG_INTRA_INTERP
Yaowu Xu's avatar
Yaowu Xu committed
711
  const int ctx = av1_get_pred_context_intra_interp(xd);
712
  int p_angle;
hui su's avatar
hui su committed
713
#endif  // CONFIG_INTRA_INTERP
714

hui su's avatar
hui su committed
715
  (void)cm;
716
  if (bsize < BLOCK_8X8) return;
717

hui su's avatar
hui su committed
718 719
  if (av1_is_directional_mode(mbmi->mode, bsize)) {
    const int max_angle_delta = av1_get_max_angle_delta(mbmi->sb_type, 0);
720
    mbmi->angle_delta[0] =
hui su's avatar
hui su committed
721
        read_uniform(r, 2 * max_angle_delta + 1) - max_angle_delta;
hui su's avatar
hui su committed
722
#if CONFIG_INTRA_INTERP
hui su's avatar
hui su committed
723 724
    p_angle = mode_to_angle_map[mbmi->mode] +
              mbmi->angle_delta[0] * av1_get_angle_step(mbmi->sb_type, 0);
Yaowu Xu's avatar
Yaowu Xu committed
725
    if (av1_is_intra_filter_switchable(p_angle)) {
726
      FRAME_COUNTS *counts = xd->counts;
Michael Bebenita's avatar
Michael Bebenita committed
727 728
      mbmi->intra_filter = aom_read_tree(
          r, av1_intra_filter_tree, cm->fc->intra_filter_probs[ctx], ACCT_STR);
729
      if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
730 731 732
    } else {
      mbmi->intra_filter = INTRA_FILTER_LINEAR;
    }
hui su's avatar
hui su committed
733
#endif  // CONFIG_INTRA_INTERP
734 735
  }

hui su's avatar
hui su committed
736
  if (av1_is_directional_mode(mbmi->uv_mode, bsize)) {
737
    mbmi->angle_delta[1] =
hui su's avatar
hui su committed
738
        read_uniform(r, 2 * MAX_ANGLE_DELTA_UV + 1) - MAX_ANGLE_DELTA_UV;
739 740
  }
}
hui su's avatar
hui su committed
741 742
#endif  // CONFIG_EXT_INTRA

Jingning Han's avatar
Jingning Han committed
743 744 745 746 747 748 749
static void read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd,
                         MB_MODE_INFO *mbmi,
#if CONFIG_SUPERTX
                         int supertx_enabled,
#endif
                         aom_reader *r) {
  const int inter_block = is_inter_block(mbmi);
750 751 752
#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
753
  const TX_SIZE tx_size = mbmi->tx_size;
754
#endif
755 756 757 758 759 760
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#else
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif

Jingning Han's avatar
Jingning Han committed
761 762
  if (!FIXED_TX_TYPE) {
#if CONFIG_EXT_TX
763
    const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
764 765
    if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block,
                         cm->reduced_tx_set_used) > 1 &&
766 767 768
        ((!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
769 770 771 772
#if CONFIG_SUPERTX
        !supertx_enabled &&
#endif  // CONFIG_SUPERTX
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
773 774
      const int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block,
                                      cm->reduced_tx_set_used);
Jingning Han's avatar
Jingning Han committed
775 776 777 778 779 780
      FRAME_COUNTS *counts = xd->counts;

      if (inter_block) {
        if (eset > 0) {
          mbmi->tx_type = aom_read_tree(
              r, av1_ext_tx_inter_tree[eset],
781
              ec_ctx->inter_ext_tx_prob[eset][square_tx_size], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
782
          if (counts)
783
            ++counts->inter_ext_tx[eset][square_tx_size][mbmi->tx_type];
Jingning Han's avatar
Jingning Han committed
784 785 786 787 788
        }
      } else if (ALLOW_INTRA_EXT_TX) {
        if (eset > 0) {
          mbmi->tx_type = aom_read_tree(
              r, av1_ext_tx_intra_tree[eset],
789
              ec_ctx->intra_ext_tx_prob[eset][square_tx_size][mbmi->mode],
790
              ACCT_STR);
Jingning Han's avatar
Jingning Han committed
791
          if (counts)
792 793
            ++counts->intra_ext_tx[eset][square_tx_size][mbmi->mode]
                                  [mbmi->tx_type];
Jingning Han's avatar
Jingning Han committed
794 795 796 797 798 799
        }
      }
    } else {
      mbmi->tx_type = DCT_DCT;
    }
#else
800 801 802 803 804

    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
805 806 807 808 809
#if CONFIG_SUPERTX
        !supertx_enabled &&
#endif  // CONFIG_SUPERTX
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
      FRAME_COUNTS *counts = xd->counts;
810

Jingning Han's avatar
Jingning Han committed
811
      if (inter_block) {
812
#if CONFIG_EC_MULTISYMBOL
Jingning Han's avatar
Jingning Han committed
813
        mbmi->tx_type = av1_ext_tx_inv[aom_read_symbol(
814
            r, ec_ctx->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
Jingning Han's avatar
Jingning Han committed
815 816
#else
        mbmi->tx_type = aom_read_tree(
817
            r, av1_ext_tx_tree, ec_ctx->inter_ext_tx_prob[tx_size], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
818 819 820 821
#endif
        if (counts) ++counts->inter_ext_tx[tx_size][mbmi->tx_type];
      } else {
        const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
822
#if CONFIG_EC_MULTISYMBOL
Jingning Han's avatar
Jingning Han committed
823
        mbmi->tx_type = av1_ext_tx_inv[aom_read_symbol(
824
            r, ec_ctx->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
Jingning Han's avatar
Jingning Han committed
825 826 827
            ACCT_STR)];
#else
        mbmi->tx_type = aom_read_tree(
828
            r, av1_ext_tx_tree, ec_ctx->intra_ext_tx_prob[tx_size][tx_type_nom],
Jingning Han's avatar
Jingning Han committed
829 830 831 832 833 834 835 836 837 838 839
            ACCT_STR);
#endif
        if (counts) ++counts->intra_ext_tx[tx_size][tx_type_nom][mbmi->tx_type];
      }
    } else {
      mbmi->tx_type = DCT_DCT;
    }
#endif  // CONFIG_EXT_TX
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
840
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
841
                                       MACROBLOCKD *const xd, int mi_row,
Yaowu Xu's avatar
Yaowu Xu committed
842
                                       int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
843 844 845
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const MODE_INFO *above_mi = xd->above_mi;
846
  const MODE_INFO *left_mi = xd->left_mi;
Jingning Han's avatar
Jingning Han committed
847 848 849
  const BLOCK_SIZE bsize = mbmi->sb_type;
  int i;
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
850 851
  const int bw = mi_size_wide[bsize];
  const int bh = mi_size_high[bsize];
Jingning Han's avatar
Jingning Han committed
852 853

  // TODO(slavarnway): move x_mis, y_mis into xd ?????
Yaowu Xu's avatar
Yaowu Xu committed
854 855
  const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
  const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
856 857 858 859 860
#if CONFIG_EC_ADAPT
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
#elif CONFIG_EC_MULTISYMBOL
  FRAME_CONTEXT *ec_ctx = cm->fc;
#endif
Jingning Han's avatar
Jingning Han committed
861

862
  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
Jingning Han's avatar
Jingning Han committed
863
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
864 865 866

#if CONFIG_DELTA_Q
  if (cm->delta_q_present_flag) {
867 868 869 870
    xd->current_qindex =
        xd->prev_qindex +
        read_delta_qindex(cm, xd, r, mbmi, mi_col, mi_row) * cm->delta_q_res;
    xd->prev_qindex = xd->current_qindex;
871 872 873
  }
#endif

874
  mbmi->tx_size = read_tx_size(cm, xd, 0, 1, r);
Jingning Han's avatar
Jingning Han committed
875
  mbmi->ref_frame[0] = INTRA_FRAME;
Emil Keyder's avatar
Emil Keyder committed
876
  mbmi->ref_frame[1] = NONE_FRAME;
Jingning Han's avatar
Jingning Han committed
877

878 879 880
#if CONFIG_CB4X4
  (void)i;
  mbmi->mode =
881
#if CONFIG_EC_MULTISYMBOL
882
      read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
883 884
#else
      read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
885
#endif
886
#else
Jingning Han's avatar
Jingning Han committed
887 888 889 890
  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
        mi->bmi[i].as_mode =
891
#if CONFIG_EC_MULTISYMBOL
892 893
            read_intra_mode(r,
                            get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, i));
894
#else
895
            read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, i));
896
#endif
Jingning Han's avatar
Jingning Han committed
897 898 899 900
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
      mi->bmi[0].as_mode = mi->bmi[2].as_mode =
901
#if CONFIG_EC_MULTISYMBOL
902
          read_intra_mode(r, get_y_mode_cdf(ec_ctx, mi, above_mi, left_mi, 0));
903
#else
904
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));