decodemv.c 63.8 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 21 22 23
#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"
#include "av1/common/seg_common.h"

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

Yaowu Xu's avatar
Yaowu Xu committed
26
#include "aom_dsp/aom_dsp_common.h"
27

28
#if CONFIG_EXT_INTRA || CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
29
static INLINE int read_uniform(aom_reader *r, int n) {
hui su's avatar
hui su committed
30 31
  int l = get_unsigned_bits(n);
  int m = (1 << l) - n;
Yaowu Xu's avatar
Yaowu Xu committed
32
  int v = aom_read_literal(r, l - 1);
hui su's avatar
hui su committed
33 34 35 36 37 38

  assert(l != 0);

  if (v < m)
    return v;
  else
Yaowu Xu's avatar
Yaowu Xu committed
39
    return (v << 1) - m + aom_read_literal(r, 1);
hui su's avatar
hui su committed
40
}
41
#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
hui su's avatar
hui su committed
42

Yaowu Xu's avatar
Yaowu Xu committed
43 44
static PREDICTION_MODE read_intra_mode(aom_reader *r, const aom_prob *p) {
  return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p);
Jingning Han's avatar
Jingning Han committed
45 46
}

Yaowu Xu's avatar
Yaowu Xu committed
47 48
static PREDICTION_MODE read_intra_mode_y(AV1_COMMON *cm, MACROBLOCKD *xd,
                                         aom_reader *r, int size_group) {
Jingning Han's avatar
Jingning Han committed
49 50 51
  const PREDICTION_MODE y_mode =
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
  FRAME_COUNTS *counts = xd->counts;
52
  if (counts) ++counts->y_mode[size_group][y_mode];
Jingning Han's avatar
Jingning Han committed
53 54 55
  return y_mode;
}

Yaowu Xu's avatar
Yaowu Xu committed
56 57
static PREDICTION_MODE read_intra_mode_uv(AV1_COMMON *cm, MACROBLOCKD *xd,
                                          aom_reader *r,
Jingning Han's avatar
Jingning Han committed
58
                                          PREDICTION_MODE y_mode) {
59 60
  const PREDICTION_MODE uv_mode =
      read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
Jingning Han's avatar
Jingning Han committed
61
  FRAME_COUNTS *counts = xd->counts;
62
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
Jingning Han's avatar
Jingning Han committed
63 64 65
  return uv_mode;
}

66
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
67 68 69 70
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(
      r, av1_interintra_mode_tree, cm->fc->interintra_mode_prob[size_group]);
71
  FRAME_COUNTS *counts = xd->counts;
72
  if (counts) ++counts->interintra_mode[size_group][ii_mode];
73 74 75 76
  return ii_mode;
}
#endif  // CONFIG_EXT_INTER

Yaowu Xu's avatar
Yaowu Xu committed
77
static PREDICTION_MODE read_inter_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Yue Chen's avatar
Yue Chen committed
78 79 80
#if CONFIG_REF_MV && CONFIG_EXT_INTER
                                       MB_MODE_INFO *mbmi,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
81
                                       aom_reader *r, int16_t ctx) {
82 83
#if CONFIG_REF_MV
  FRAME_COUNTS *counts = xd->counts;
84
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Yaowu Xu's avatar
Yaowu Xu committed
85
  aom_prob mode_prob = cm->fc->newmv_prob[mode_ctx];
86

Yaowu Xu's avatar
Yaowu Xu committed
87
  if (aom_read(r, mode_prob) == 0) {
88
    if (counts) ++counts->newmv_mode[mode_ctx][0];
Yue Chen's avatar
Yue Chen committed
89 90 91 92

#if CONFIG_EXT_INTER
    if (has_second_ref(mbmi)) {
#endif  // CONFIG_EXT_INTER
93
      return NEWMV;
Yue Chen's avatar
Yue Chen committed
94 95 96
#if CONFIG_EXT_INTER
    } else {
      mode_prob = cm->fc->new2mv_prob;
Yaowu Xu's avatar
Yaowu Xu committed
97
      if (aom_read(r, mode_prob) == 0) {
98
        if (counts) ++counts->new2mv_mode[0];
Yue Chen's avatar
Yue Chen committed
99 100
        return NEWMV;
      } else {
101
        if (counts) ++counts->new2mv_mode[1];
Yue Chen's avatar
Yue Chen committed
102 103 104 105
        return NEWFROMNEARMV;
      }
    }
#endif  // CONFIG_EXT_INTER
106
  }
107
  if (counts) ++counts->newmv_mode[mode_ctx][1];
108

109
  if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
110

111
  mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
112 113

  mode_prob = cm->fc->zeromv_prob[mode_ctx];
Yaowu Xu's avatar
Yaowu Xu committed
114
  if (aom_read(r, mode_prob) == 0) {
115
    if (counts) ++counts->zeromv_mode[mode_ctx][0];
116 117
    return ZEROMV;
  }
118
  if (counts) ++counts->zeromv_mode[mode_ctx][1];
119

120
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
121

122 123 124
  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;
125

126
  mode_prob = cm->fc->refmv_prob[mode_ctx];
127

Yaowu Xu's avatar
Yaowu Xu committed
128
  if (aom_read(r, mode_prob) == 0) {
129
    if (counts) ++counts->refmv_mode[mode_ctx][0];
130

131 132
    return NEARESTMV;
  } else {
133
    if (counts) ++counts->refmv_mode[mode_ctx][1];
134 135 136 137 138 139
    return NEARMV;
  }

  // Invalid prediction mode.
  assert(0);
#else
140
  const int mode =
Yaowu Xu's avatar
Yaowu Xu committed
141
      aom_read_tree(r, av1_inter_mode_tree, cm->fc->inter_mode_probs[ctx]);
Jingning Han's avatar
Jingning Han committed
142
  FRAME_COUNTS *counts = xd->counts;
143
  if (counts) ++counts->inter_mode[ctx][mode];
Jingning Han's avatar
Jingning Han committed
144 145

  return NEARESTMV + mode;
146
#endif
Jingning Han's avatar
Jingning Han committed
147 148
}

149
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
150 151 152
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);
153 154
  mbmi->ref_mv_idx = 0;

155 156 157 158
  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
159 160 161
        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];
        if (!aom_read(r, drl_prob)) {
162
          mbmi->ref_mv_idx = idx;
163
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
164 165 166
          return;
        }
        mbmi->ref_mv_idx = idx + 1;
167
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
168 169 170 171
      }
    }
  }

172 173 174 175 176 177 178
  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
179 180 181
        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];
        if (!aom_read(r, drl_prob)) {
182
          mbmi->ref_mv_idx = idx - 1;
183
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
184 185
          return;
        }
186
        mbmi->ref_mv_idx = idx;
187
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
188 189 190 191 192 193
      }
    }
  }
}
#endif

194
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
195 196 197 198
static PREDICTION_MODE read_inter_compound_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
                                                aom_reader *r, int16_t ctx) {
  const int mode = aom_read_tree(r, av1_inter_compound_mode_tree,
                                 cm->fc->inter_compound_mode_probs[ctx]);
199 200
  FRAME_COUNTS *counts = xd->counts;

201
  if (counts) ++counts->inter_compound_mode[ctx][mode];
202 203 204 205 206 207

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

Yaowu Xu's avatar
Yaowu Xu committed
208
static int read_segment_id(aom_reader *r,
209
                           const struct segmentation_probs *segp) {
Yaowu Xu's avatar
Yaowu Xu committed
210
  return aom_read_tree(r, av1_segment_tree, segp->tree_probs);
Jingning Han's avatar
Jingning Han committed
211 212
}

213
#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
214
static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
215
                               MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
216
                               TX_SIZE tx_size, int blk_row, int blk_col,
Yaowu Xu's avatar
Yaowu Xu committed
217
                               aom_reader *r) {
218
  int is_split = 0;
219 220
  const int tx_row = blk_row >> 1;
  const int tx_col = blk_col >> 1;
221 222
  int max_blocks_high = num_4x4_blocks_high_lookup[mbmi->sb_type];
  int max_blocks_wide = num_4x4_blocks_wide_lookup[mbmi->sb_type];
223
  int ctx = txfm_partition_context(xd->above_txfm_context + tx_col,
224
                                   xd->left_txfm_context + tx_row, tx_size);
clang-format's avatar
clang-format committed
225
  TX_SIZE(*const inter_tx_size)
226 227
  [MAX_MIB_SIZE] =
      (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
228

229 230
  if (xd->mb_to_bottom_edge < 0) max_blocks_high += xd->mb_to_bottom_edge >> 5;
  if (xd->mb_to_right_edge < 0) max_blocks_wide += xd->mb_to_right_edge >> 5;
231

232
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
233

Yaowu Xu's avatar
Yaowu Xu committed
234
  is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx]);
235 236 237

  if (is_split) {
    BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
238
    int bsl = b_width_log2_lookup[bsize];
239
    int i;
240

241
    if (counts) ++counts->txfm_partition[ctx][1];
242

243
    if (tx_size == TX_8X8) {
244 245 246 247
      inter_tx_size[0][0] = TX_4X4;
      mbmi->tx_size = TX_4X4;
      txfm_partition_update(xd->above_txfm_context + tx_col,
                            xd->left_txfm_context + tx_row, TX_4X4);
248 249 250 251 252 253
      return;
    }

    assert(bsl > 0);
    --bsl;
    for (i = 0; i < 4; ++i) {
254 255
      int offsetr = blk_row + ((i >> 1) << bsl);
      int offsetc = blk_col + ((i & 0x01) << bsl);
256 257
      read_tx_size_vartx(cm, xd, mbmi, counts, tx_size - 1, offsetr, offsetc,
                         r);
258 259
    }
  } else {
260
    int idx, idy;
261
    inter_tx_size[0][0] = tx_size;
262 263
    for (idy = 0; idy < num_4x4_blocks_high_txsize_lookup[tx_size] / 2; ++idy)
      for (idx = 0; idx < num_4x4_blocks_wide_txsize_lookup[tx_size] / 2; ++idx)
264 265
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
266
    if (counts) ++counts->txfm_partition[ctx][0];
267 268
    txfm_partition_update(xd->above_txfm_context + tx_col,
                          xd->left_txfm_context + tx_row, tx_size);
269 270 271 272
  }
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
273 274
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
275 276
  FRAME_COUNTS *counts = xd->counts;
  const int ctx = get_tx_size_context(xd);
Yaowu Xu's avatar
Yaowu Xu committed
277 278
  int tx_size = aom_read_tree(r, av1_tx_size_tree[tx_size_cat],
                              cm->fc->tx_size_probs[tx_size_cat][ctx]);
279
  if (counts) ++counts->tx_size[tx_size_cat][ctx][tx_size];
Jingning Han's avatar
Jingning Han committed
280 281 282
  return (TX_SIZE)tx_size;
}

Yaowu Xu's avatar
Yaowu Xu committed
283 284
static TX_SIZE read_tx_size_intra(AV1_COMMON *cm, MACROBLOCKD *xd,
                                  aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
285 286
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
287
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
288 289
  if (bsize >= BLOCK_8X8) {
    if (tx_mode == TX_MODE_SELECT) {
290 291 292 293
      const TX_SIZE tx_size =
          read_selected_tx_size(cm, xd, intra_tx_size_cat_lookup[bsize], r);
      assert(tx_size <= max_txsize_lookup[bsize]);
      return tx_size;
294
    } else {
295
      return tx_size_from_tx_mode(bsize, cm->tx_mode, 0);
296 297 298 299 300 301
    }
  } else {
    return TX_4X4;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
302 303
static TX_SIZE read_tx_size_inter(AV1_COMMON *cm, MACROBLOCKD *xd,
                                  int allow_select, aom_reader *r) {
304 305
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
306
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
307 308
  if (bsize >= BLOCK_8X8) {
    if (allow_select && tx_mode == TX_MODE_SELECT) {
309 310
      const TX_SIZE coded_tx_size =
          read_selected_tx_size(cm, xd, inter_tx_size_cat_lookup[bsize], r);
311
#if CONFIG_EXT_TX && CONFIG_RECT_TX
312 313 314 315
      if (coded_tx_size > max_txsize_lookup[bsize]) {
        assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
        return max_txsize_rect_lookup[bsize];
      }
316 317 318
#else
      assert(coded_tx_size <= max_txsize_lookup[bsize]);
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX
319
      return coded_tx_size;
320
    } else {
321
      return tx_size_from_tx_mode(bsize, cm->tx_mode, 1);
322 323
    }
  } else {
324
#if CONFIG_EXT_TX && CONFIG_RECT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
325
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
326 327 328
    return max_txsize_rect_lookup[bsize];
#else
    return TX_4X4;
329
#endif
330
  }
Jingning Han's avatar
Jingning Han committed
331 332
}

Yaowu Xu's avatar
Yaowu Xu committed
333
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Jingning Han's avatar
Jingning Han committed
334 335 336 337 338
                              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++)
339
      segment_id =
Yaowu Xu's avatar
Yaowu Xu committed
340
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
341 342 343 344 345

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

Yaowu Xu's avatar
Yaowu Xu committed
346
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
347
                           int segment_id) {
Jingning Han's avatar
Jingning Han committed
348 349 350 351 352 353 354 355 356
  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
357
static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
358
                                 int mi_offset, int x_mis, int y_mis,
Yaowu Xu's avatar
Yaowu Xu committed
359
                                 aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
360
  struct segmentation *const seg = &cm->seg;
361 362
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
363 364
  int segment_id;

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

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

369
  segment_id = read_segment_id(r, segp);
370
  if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
371 372 373 374
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
375
static void copy_segment_id(const AV1_COMMON *cm,
376 377 378
                            const uint8_t *last_segment_ids,
                            uint8_t *current_segment_ids, int mi_offset,
                            int x_mis, int y_mis) {
379 380 381 382
  int x, y;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
383 384 385
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
                           : 0;
386 387
}

Yaowu Xu's avatar
Yaowu Xu committed
388 389
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
390
  struct segmentation *const seg = &cm->seg;
391 392
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
393 394 395
  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;
Geza Lore's avatar
Geza Lore committed
396 397
  const int bw = num_8x8_blocks_wide_lookup[mbmi->sb_type];
  const int bh = num_8x8_blocks_high_lookup[mbmi->sb_type];
Jingning Han's avatar
Jingning Han committed
398 399

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

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

405 406 407 408
  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
409 410 411 412 413 414 415 416

  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
417 418 419
    const int ctx = av1_get_pred_context_seg_id(xd);
    const aom_prob pred_prob = segp->pred_probs[ctx];
    mbmi->seg_id_predicted = aom_read(r, pred_prob);
420
    if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
421 422 423 424
    if (mbmi->seg_id_predicted) {
      segment_id = predicted_segment_id;
    } else {
      segment_id = read_segment_id(r, segp);
425
      if (counts) ++counts->seg.tree_mispred[segment_id];
426
    }
Jingning Han's avatar
Jingning Han committed
427
  } else {
428
    segment_id = read_segment_id(r, segp);
429
    if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
430 431 432 433 434
  }
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
435 436
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
                     aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
437 438 439
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
440 441
    const int ctx = av1_get_skip_context(xd);
    const int skip = aom_read(r, cm->fc->skip_probs[ctx]);
Jingning Han's avatar
Jingning Han committed
442
    FRAME_COUNTS *counts = xd->counts;
443
    if (counts) ++counts->skip[ctx][skip];
Jingning Han's avatar
Jingning Han committed
444 445 446 447
    return skip;
  }
}

448
#if CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
449 450
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                   aom_reader *r) {
hui su's avatar
hui su committed
451 452
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
453
  const MODE_INFO *const above_mi = xd->above_mi;
454
  const MODE_INFO *const left_mi = xd->left_mi;
hui su's avatar
hui su committed
455
  const BLOCK_SIZE bsize = mbmi->sb_type;
456 457 458 459 460 461 462 463
  int i, n, palette_ctx = 0;
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;

  if (mbmi->mode == DC_PRED) {
    if (above_mi)
      palette_ctx += (above_mi->mbmi.palette_mode_info.palette_size[0] > 0);
    if (left_mi)
      palette_ctx += (left_mi->mbmi.palette_mode_info.palette_size[0] > 0);
clang-format's avatar
clang-format committed
464 465
    if (aom_read(r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8]
                                                   [palette_ctx])) {
466
      pmi->palette_size[0] =
Yaowu Xu's avatar
Yaowu Xu committed
467 468
          aom_read_tree(r, av1_palette_size_tree,
                        av1_default_palette_y_size_prob[bsize - BLOCK_8X8]) +
469
          2;
470 471
      n = pmi->palette_size[0];
      for (i = 0; i < n; ++i)
Yaowu Xu's avatar
Yaowu Xu committed
472
        pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth);
hui su's avatar
hui su committed
473

474 475 476 477
      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
478

479
  if (mbmi->uv_mode == DC_PRED) {
Yaowu Xu's avatar
Yaowu Xu committed
480 481
    if (aom_read(r,
                 av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0])) {
482
      pmi->palette_size[1] =
Yaowu Xu's avatar
Yaowu Xu committed
483 484
          aom_read_tree(r, av1_palette_size_tree,
                        av1_default_palette_uv_size_prob[bsize - BLOCK_8X8]) +
485
          2;
486 487 488
      n = pmi->palette_size[1];
      for (i = 0; i < n; ++i) {
        pmi->palette_colors[PALETTE_MAX_SIZE + i] =
Yaowu Xu's avatar
Yaowu Xu committed
489
            aom_read_literal(r, cm->bit_depth);
490
        pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Yaowu Xu's avatar
Yaowu Xu committed
491
            aom_read_literal(r, cm->bit_depth);
492 493 494 495
      }
      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
496 497
  }
}
498
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
499

hui su's avatar
hui su committed
500
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
501 502
static void read_ext_intra_mode_info(AV1_COMMON *const cm,
                                     MACROBLOCKD *const xd, aom_reader *r) {
hui su's avatar
hui su committed
503 504 505
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  FRAME_COUNTS *counts = xd->counts;
hui su's avatar
hui su committed
506 507 508 509

#if !ALLOW_FILTER_INTRA_MODES
  return;
#endif
510 511 512 513 514
  if (mbmi->mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[0] == 0
#endif  // CONFIG_PALETTE
      ) {
hui su's avatar
hui su committed
515
    mbmi->ext_intra_mode_info.use_ext_intra_mode[0] =
Yaowu Xu's avatar
Yaowu Xu committed
516
        aom_read(r, cm->fc->ext_intra_probs[0]);
hui su's avatar
hui su committed
517
    if (mbmi->ext_intra_mode_info.use_ext_intra_mode[0]) {
hui su's avatar
hui su committed
518 519
      mbmi->ext_intra_mode_info.ext_intra_mode[0] =
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
520 521 522 523
    }
    if (counts)
      ++counts->ext_intra[0][mbmi->ext_intra_mode_info.use_ext_intra_mode[0]];
  }
524 525 526 527 528
  if (mbmi->uv_mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[1] == 0
#endif  // CONFIG_PALETTE
      ) {
hui su's avatar
hui su committed
529
    mbmi->ext_intra_mode_info.use_ext_intra_mode[1] =
Yaowu Xu's avatar
Yaowu Xu committed
530
        aom_read(r, cm->fc->ext_intra_probs[1]);
hui su's avatar
hui su committed
531
    if (mbmi->ext_intra_mode_info.use_ext_intra_mode[1]) {
hui su's avatar
hui su committed
532 533
      mbmi->ext_intra_mode_info.ext_intra_mode[1] =
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
534 535 536 537 538
    }
    if (counts)
      ++counts->ext_intra[1][mbmi->ext_intra_mode_info.use_ext_intra_mode[1]];
  }
}
539

Yaowu Xu's avatar
Yaowu Xu committed
540 541
static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                  aom_reader *r) {
542 543
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xu's avatar
Yaowu Xu committed
544
  const int ctx = av1_get_pred_context_intra_interp(xd);
545 546
  int p_angle;

547
  if (bsize < BLOCK_8X8) return;
548 549 550 551 552

  if (mbmi->mode != DC_PRED && mbmi->mode != TM_PRED) {
    mbmi->angle_delta[0] =
        read_uniform(r, 2 * MAX_ANGLE_DELTAS + 1) - MAX_ANGLE_DELTAS;
    p_angle = mode_to_angle_map[mbmi->mode] + mbmi->angle_delta[0] * ANGLE_STEP;
Yaowu Xu's avatar
Yaowu Xu committed
553
    if (av1_is_intra_filter_switchable(p_angle)) {
554
      FRAME_COUNTS *counts = xd->counts;
Yaowu Xu's avatar
Yaowu Xu committed
555 556
      mbmi->intra_filter = aom_read_tree(r, av1_intra_filter_tree,
                                         cm->fc->intra_filter_probs[ctx]);
557
      if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
558 559 560 561 562 563 564 565 566 567
    } else {
      mbmi->intra_filter = INTRA_FILTER_LINEAR;
    }
  }

  if (mbmi->uv_mode != DC_PRED && mbmi->uv_mode != TM_PRED) {
    mbmi->angle_delta[1] =
        read_uniform(r, 2 * MAX_ANGLE_DELTAS + 1) - MAX_ANGLE_DELTAS;
  }
}
hui su's avatar
hui su committed
568 569
#endif  // CONFIG_EXT_INTRA

Yaowu Xu's avatar
Yaowu Xu committed
570
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
571
                                       MACROBLOCKD *const xd, int mi_row,
Yaowu Xu's avatar
Yaowu Xu committed
572
                                       int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
573 574 575
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const MODE_INFO *above_mi = xd->above_mi;
576
  const MODE_INFO *left_mi = xd->left_mi;
Jingning Han's avatar
Jingning Han committed
577 578 579 580 581 582 583
  const BLOCK_SIZE bsize = mbmi->sb_type;
  int i;
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
  const int bw = xd->plane[0].n4_w >> 1;
  const int bh = xd->plane[0].n4_h >> 1;

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

587
  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
Jingning Han's avatar
Jingning Han committed
588
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
589
  mbmi->tx_size = read_tx_size_intra(cm, xd, r);
Jingning Han's avatar
Jingning Han committed
590 591 592 593 594 595 596
  mbmi->ref_frame[0] = INTRA_FRAME;
  mbmi->ref_frame[1] = NONE;

  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
        mi->bmi[i].as_mode =
597
            read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, i));
Jingning Han's avatar
Jingning Han committed
598 599 600 601
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
      mi->bmi[0].as_mode = mi->bmi[2].as_mode =
602
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
Jingning Han's avatar
Jingning Han committed
603
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
604
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 1));
Jingning Han's avatar
Jingning Han committed
605 606 607
      break;
    case BLOCK_8X4:
      mi->bmi[0].as_mode = mi->bmi[1].as_mode =
608
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
Jingning Han's avatar
Jingning Han committed
609
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
610
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 2));
Jingning Han's avatar
Jingning Han committed
611 612
      break;
    default:
613 614
      mbmi->mode =
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
Jingning Han's avatar
Jingning Han committed
615 616
  }

617
  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
hui su's avatar
hui su committed
618
#if CONFIG_EXT_INTRA
619 620
  read_intra_angle_info(cm, xd, r);
#endif  // CONFIG_EXT_INTRA
621
#if CONFIG_PALETTE
hui su's avatar
hui su committed
622 623
  mbmi->palette_mode_info.palette_size[0] = 0;
  mbmi->palette_mode_info.palette_size[1] = 0;
624
  if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
hui su's avatar
hui su committed
625
    read_palette_mode_info(cm, xd, r);
626
#endif  // CONFIG_PALETTE
627
#if CONFIG_EXT_INTRA
628 629 630
  mbmi->ext_intra_mode_info.use_ext_intra_mode[0] = 0;
  mbmi->ext_intra_mode_info.use_ext_intra_mode[1] = 0;
  if (bsize >= BLOCK_8X8) read_ext_intra_mode_info(cm, xd, r);
631
#endif  // CONFIG_EXT_INTRA
hui su's avatar
hui su committed
632

633
  if (!FIXED_TX_TYPE) {
634
#if CONFIG_EXT_TX
635 636
    if (get_ext_tx_types(mbmi->tx_size, mbmi->sb_type, 0) > 1 &&
        cm->base_qindex > 0 && !mbmi->skip &&
637 638
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) &&
        ALLOW_INTRA_EXT_TX) {
Jingning Han's avatar
Jingning Han committed
639
      FRAME_COUNTS *counts = xd->counts;
640 641
      int eset = get_ext_tx_set(mbmi->tx_size, mbmi->sb_type, 0);
      if (eset > 0) {
Yaowu Xu's avatar
Yaowu Xu committed
642 643
        mbmi->tx_type = aom_read_tree(
            r, av1_ext_tx_intra_tree[eset],
644 645
            cm->fc->intra_ext_tx_prob[eset][mbmi->tx_size][mbmi->mode]);
        if (counts)
clang-format's avatar
clang-format committed
646 647
          ++counts->intra_ext_tx[eset][mbmi->tx_size][mbmi->mode]
                                [mbmi->tx_type];
648
      }
649
    } else {
hui su's avatar
hui su committed
650
      mbmi->tx_type = DCT_DCT;
651
    }
652
#else
653
    if (mbmi->tx_size < TX_32X32 && cm->base_qindex > 0 && !mbmi->skip &&
654 655 656
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
      FRAME_COUNTS *counts = xd->counts;
      TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
657
      mbmi->tx_type =
Yaowu Xu's avatar
Yaowu Xu committed
658 659
          aom_read_tree(r, av1_ext_tx_tree,
                        cm->fc->intra_ext_tx_prob[mbmi->tx_size][tx_type_nom]);
660 661 662 663 664
      if (counts)
        ++counts->intra_ext_tx[mbmi->tx_size][tx_type_nom][mbmi->tx_type];
    } else {
      mbmi->tx_type = DCT_DCT;
    }
665
#endif  // CONFIG_EXT_TX
666
  }
Jingning Han's avatar
Jingning Han committed
667 668
}

Yaowu Xu's avatar
Yaowu Xu committed
669
static int read_mv_component(aom_reader *r, const nmv_component *mvcomp,
670
                             int usehp) {
Jingning Han's avatar
Jingning Han committed
671
  int mag, d, fr, hp;
Yaowu Xu's avatar
Yaowu Xu committed
672 673
  const int sign = aom_read(r, mvcomp->sign);
  const int mv_class = aom_read_tree(r, av1_mv_class_tree, mvcomp->classes);
Jingning Han's avatar
Jingning Han committed
674 675 676 677
  const int class0 = mv_class == MV_CLASS_0;

  // Integer part
  if (class0) {
Yaowu Xu's avatar
Yaowu Xu committed
678
    d = aom_read_tree(r, av1_mv_class0_tree, mvcomp->class0);
Jingning Han's avatar
Jingning Han committed
679 680 681 682 683 684
    mag = 0;
  } else {
    int i;
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits

    d = 0;
Yaowu Xu's avatar
Yaowu Xu committed
685
    for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i]) << i;
Jingning Han's avatar
Jingning Han committed
686 687 688 689
    mag = CLASS0_SIZE << (mv_class + 2);
  }

  // Fractional part
Yaowu Xu's avatar
Yaowu Xu committed
690 691
  fr = aom_read_tree(r, av1_mv_fp_tree,
                     class0 ? mvcomp->class0_fp[d] : mvcomp->fp);
Jingning Han's avatar
Jingning Han committed
692 693

  // High precision part (if hp is not used, the default value of the hp is 1)
Yaowu Xu's avatar
Yaowu Xu committed
694
  hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp) : 1;
Jingning Han's avatar
Jingning Han committed
695 696 697 698 699 700

  // Result
  mag += ((d << 3) | (fr << 1) | hp) + 1;
  return sign ? -mag : mag;
}

Yaowu Xu's avatar
Yaowu Xu committed
701
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
702 703
                           const nmv_context *ctx, nmv_context_counts *counts,
                           int allow_hp) {
704
  MV_JOINT_TYPE joint_type;
Yaowu Xu's avatar
Yaowu Xu committed
705
  const int use_hp = allow_hp && av1_use_mv_hp(ref);
706
  MV diff = { 0, 0 };
Yaowu Xu's avatar
Yaowu Xu committed
707
  joint_type = (MV_JOINT_TYPE)aom_read_tree(r, av1_mv_joint_tree, ctx->joints);
708

Jingning Han's avatar
Jingning Han committed
709 710 711 712 713 714
  if (mv_joint_vertical(joint_type))
    diff.row = read_mv_component(r, &ctx->comps[0], use_hp);

  if (mv_joint_horizontal(joint_type))
    diff.col = read_mv_component(r, &ctx->comps[1], use_hp);

Yaowu Xu's avatar
Yaowu Xu committed
715
  av1_inc_mv(&diff, counts, use_hp);
Jingning Han's avatar
Jingning Han committed
716 717 718 719 720

  mv->row = ref->row + diff.row;
  mv->col = ref->col + diff.col;
}

Yaowu Xu's avatar
Yaowu Xu committed
721
static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Jingning Han's avatar
Jingning Han committed
722
                                                const MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
723
                                                aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
724
  if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xu's avatar
Yaowu Xu committed
725
    const int ctx = av1_get_reference_mode_context(cm, xd);
Jingning Han's avatar
Jingning Han committed
726
    const REFERENCE_MODE mode =
Yaowu Xu's avatar
Yaowu Xu committed
727
        (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx]);
Jingning Han's avatar
Jingning Han committed
728
    FRAME_COUNTS *counts = xd->counts;
729
    if (counts) ++counts->comp_inter[ctx][mode];
Jingning Han's avatar
Jingning Han committed
730 731 732 733 734 735 736
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
  } else {
    return cm->reference_mode;
  }
}

// Read the referncence frame
Yaowu Xu's avatar
Yaowu Xu committed
737 738
static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                            aom_reader *r, int segment_id,
739
                            MV_REFERENCE_FRAME ref_frame[2]) {
Jingning Han's avatar
Jingning Han committed
740 741 742 743 744 745 746 747 748 749 750
  FRAME_CONTEXT *const fc = cm->fc;
  FRAME_COUNTS *counts = xd->counts;

  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
    ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
                                                   SEG_LVL_REF_FRAME);
    ref_frame[1] = NONE;
  } else {
    const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
    // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
    if (mode == COMPOUND_REFERENCE) {
751
#if CONFIG_EXT_REFS
752 753
      const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
#else
Jingning Han's avatar
Jingning Han committed
754
      const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
755
#endif  // CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
756 757
      const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
      const int bit = aom_read(r, fc->comp_ref_prob[ctx][0]);
758

759
      if (counts) ++counts->comp_ref[ctx][0][bit];
760

761
#if CONFIG_EXT_REFS
762
      // Decode forward references.
763
      if (!bit) {
Yaowu Xu's avatar
Yaowu Xu committed
764 765
        const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
        const int bit1 = aom_read(r, fc->comp_ref_prob[ctx1][1]);
766
        if (counts) ++counts->comp_ref[ctx1][1][bit1];
767
        ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
768
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
769 770
        const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
        const int bit2 = aom_read(r, fc->comp_ref_prob[ctx2][2]);
771
        if (counts) ++counts->comp_ref[ctx2][2][bit2];
772
        ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];
773
      }
774 775 776

      // Decode backward references.
      {
Yaowu Xu's avatar
Yaowu Xu committed
777 778
        const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
        const int bit_bwd = aom_read(r, fc->comp_bwdref_prob[ctx_bwd][0]);
779
        if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
780 781 782
        ref_frame[idx] = cm->comp_bwd_ref[bit_bwd];
      }
#else
Jingning Han's avatar
Jingning Han committed
783
      ref_frame[!idx] = cm->comp_var_ref[bit];
784
      ref_frame[idx] = cm->comp_fixed_ref;
785
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
786
    } else if (mode == SINGLE_REFERENCE) {
787
#if CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
788 789
      const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0]);
790
      if (counts) ++counts->single_ref[ctx0][0][bit0];
791

792
      if (bit0) {
Yaowu Xu's avatar
Yaowu Xu committed
793 794
        const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1]);
795
        if (counts) ++counts->single_ref[ctx1][1][bit1];
796
        ref_frame[0] = bit1 ? ALTREF_FRAME : BWDREF_FRAME;
797
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
798 799
        const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
        const int bit2 = aom_read(r, fc->single_ref_prob[ctx2][2]);
800
        if (counts) ++counts->single_ref[ctx2][2][bit2];
801
        if (bit2) {
Yaowu Xu's avatar
Yaowu Xu committed
802 803
          const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
          const int bit4 = aom_read(r, fc->single_ref_prob[ctx4][4]);
804
          if (counts) ++counts->single_ref[ctx4][4][bit4];
805
          ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
806
        } else {
Yaowu Xu's avatar
Yaowu Xu committed
807 808
          const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
          const int bit3 = aom_read(r, fc->single_ref_prob[ctx3][3]);
809
          if (counts) ++counts->single_ref[ctx3][3][bit3];
810 811 812
          ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
        }
      }
813
#else
Yaowu Xu's avatar
Yaowu Xu committed
814 815
      const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0]);
816
      if (counts) ++counts->single_ref[ctx0][0][bit0];
817

Jingning Han's avatar
Jingning Han committed
818
      if (bit0) {
Yaowu Xu's avatar
Yaowu Xu committed
819 820
        const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1]);
821
        if (counts) ++counts->single_ref[ctx1][1][bit1];
Jingning Han's avatar
Jingning Han committed
822 823 824 825
        ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
      } else {
        ref_frame[0] = LAST_FRAME;
      }
826
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
827 828 829 830 831 832 833 834

      ref_frame[1] = NONE;
    } else {
      assert(0 && "Invalid prediction mode.");
    }
  }
}

Yue Chen's avatar
Yue Chen committed
835 836 837 838 839 840
#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) {
  if (is_motion_variation_allowed(mbmi)) {
    int motion_mode;
    FRAME_COUNTS *counts = xd->counts;
841

Yue Chen's avatar
Yue Chen committed
842 843 844 845
    motion_mode = aom_read_tree(r, av1_motion_mode_tree,
                                cm->fc->motion_mode_prob[mbmi->sb_type]);
    if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
    return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
846
  } else {
847
    return SIMPLE_TRANSLATION;
848 849
  }
}
Yue Chen's avatar
Yue Chen committed
850
#endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION
851

James Zern's avatar
James Zern committed
852 853
static INLINE InterpFilter read_interp_filter(AV1_COMMON *const cm,
                                              MACROBLOCKD *const xd,
854
#if CONFIG_DUAL_FILTER
James Zern's avatar
James Zern committed
855
                                              int dir,
856
#endif
James Zern's avatar
James Zern committed
857
                                              aom_reader *r) {
858
#if CONFIG_EXT_INTERP
Yaowu Xu's avatar
Yaowu Xu committed
859
  if (!av1_is_interp_needed(xd)) return EIGHTTAP_REGULAR;
860
#endif
Geza Lore's avatar
Geza Lore committed
861 862 863
  if (cm->interp_filter != SWITCHABLE) {
    return cm->interp_filter;
  } else {
864
#if CONFIG_DUAL_FILTER
Yaowu Xu's avatar
Yaowu Xu committed
865
    const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
866
#else
Yaowu Xu's avatar
Yaowu Xu committed
867
    const int ctx = av1_get_pred_context_switchable_interp(xd);
868
#endif
Geza Lore's avatar
Geza Lore committed
869
    FRAME_COUNTS *counts = xd->counts;
870 871 872 873 874
#if CONFIG_DAALA_EC
    const InterpFilter type =
        (InterpFilter)av1_switchable_interp_inv[aom_read_tree_cdf(
            r, cm->fc->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS)];
#else
James Zern's avatar
James Zern committed
875
    const InterpFilter type = (InterpFilter)aom_read_tree(
Yaowu Xu's avatar
Yaowu Xu committed
876
        r, av1_switchable_interp_tree, cm->fc->switchable_interp_prob[ctx]);
877
#endif
878
    if (counts) ++counts->switchable_interp[ctx][type];
Geza Lore's avatar
Geza Lore committed
879 880
    return type;
  }
Jingning Han's avatar
Jingning Han committed
881 882
}

Yaowu Xu's avatar
Yaowu Xu committed
883
static void read_intra_block_mode_info(AV1_COMMON *const cm,