decodemv.c 69.5 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
#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"
Yue Chen's avatar
Yue Chen committed
22
23
24
#if CONFIG_WARPED_MOTION
#include "av1/common/warped_motion.h"
#endif  // CONFIG_WARPED_MOTION
25
26

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

Yaowu Xu's avatar
Yaowu Xu committed
29
#include "aom_dsp/aom_dsp_common.h"
30

Michael Bebenita's avatar
Michael Bebenita committed
31
#define ACCT_STR __func__
32
#if CONFIG_EXT_INTRA || CONFIG_FILTER_INTRA || CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
33
static INLINE int read_uniform(aom_reader *r, int n) {
hui su's avatar
hui su committed
34
35
  int l = get_unsigned_bits(n);
  int m = (1 << l) - n;
Michael Bebenita's avatar
Michael Bebenita committed
36
  int v = aom_read_literal(r, l - 1, ACCT_STR);
hui su's avatar
hui su committed
37
38
39
40
41
42

  assert(l != 0);

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

47
#if CONFIG_DAALA_EC
48
static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
49
50
51
  return (PREDICTION_MODE)
      av1_intra_mode_inv[aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR)];
}
52
#else
Yaowu Xu's avatar
Yaowu Xu committed
53
static PREDICTION_MODE read_intra_mode(aom_reader *r, const aom_prob *p) {
Michael Bebenita's avatar
Michael Bebenita committed
54
  return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p, ACCT_STR);
Jingning Han's avatar
Jingning Han committed
55
}
56
#endif
Jingning Han's avatar
Jingning Han committed
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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);
  int rem_bits, thr, bit = 1;

  if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_q_flag) {
    abs = 0;
    while (abs < DELTA_Q_SMALL && bit) {
      bit = aom_read(r, cm->fc->delta_q_prob[abs], ACCT_STR);
      if (counts) counts->delta_q[abs][bit]++;
      abs += bit;
    }
    if (abs == DELTA_Q_SMALL) {
      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
94
95
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
96
  const PREDICTION_MODE y_mode =
97
#if CONFIG_DAALA_EC
98
      read_intra_mode(r, cm->fc->y_mode_cdf[size_group]);
99
#else
Jingning Han's avatar
Jingning Han committed
100
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
101
#endif
Jingning Han's avatar
Jingning Han committed
102
  FRAME_COUNTS *counts = xd->counts;
103
  if (counts) ++counts->y_mode[size_group][y_mode];
Jingning Han's avatar
Jingning Han committed
104
105
106
  return y_mode;
}

Yaowu Xu's avatar
Yaowu Xu committed
107
108
static PREDICTION_MODE read_intra_mode_uv(AV1_COMMON *cm, MACROBLOCKD *xd,
                                          aom_reader *r,
Jingning Han's avatar
Jingning Han committed
109
                                          PREDICTION_MODE y_mode) {
110
  const PREDICTION_MODE uv_mode =
111
112
113
#if CONFIG_DAALA_EC
      read_intra_mode(r, cm->fc->uv_mode_cdf[y_mode]);
#else
114
      read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
115
#endif
Jingning Han's avatar
Jingning Han committed
116
  FRAME_COUNTS *counts = xd->counts;
117
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
Jingning Han's avatar
Jingning Han committed
118
119
120
  return uv_mode;
}

121
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
122
123
124
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
125
126
      r, av1_interintra_mode_tree, cm->fc->interintra_mode_prob[size_group],
      ACCT_STR);
127
  FRAME_COUNTS *counts = xd->counts;
128
  if (counts) ++counts->interintra_mode[size_group][ii_mode];
129
130
131
132
  return ii_mode;
}
#endif  // CONFIG_EXT_INTER

Yaowu Xu's avatar
Yaowu Xu committed
133
static PREDICTION_MODE read_inter_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Yue Chen's avatar
Yue Chen committed
134
135
136
#if CONFIG_REF_MV && CONFIG_EXT_INTER
                                       MB_MODE_INFO *mbmi,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
137
                                       aom_reader *r, int16_t ctx) {
138
139
#if CONFIG_REF_MV
  FRAME_COUNTS *counts = xd->counts;
140
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Yaowu Xu's avatar
Yaowu Xu committed
141
  aom_prob mode_prob = cm->fc->newmv_prob[mode_ctx];
142

Michael Bebenita's avatar
Michael Bebenita committed
143
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
144
    if (counts) ++counts->newmv_mode[mode_ctx][0];
Yue Chen's avatar
Yue Chen committed
145
146
147
148

#if CONFIG_EXT_INTER
    if (has_second_ref(mbmi)) {
#endif  // CONFIG_EXT_INTER
149
      return NEWMV;
Yue Chen's avatar
Yue Chen committed
150
151
152
#if CONFIG_EXT_INTER
    } else {
      mode_prob = cm->fc->new2mv_prob;
Michael Bebenita's avatar
Michael Bebenita committed
153
      if (aom_read(r, mode_prob, ACCT_STR) == 0) {
154
        if (counts) ++counts->new2mv_mode[0];
Yue Chen's avatar
Yue Chen committed
155
156
        return NEWMV;
      } else {
157
        if (counts) ++counts->new2mv_mode[1];
Yue Chen's avatar
Yue Chen committed
158
159
160
161
        return NEWFROMNEARMV;
      }
    }
#endif  // CONFIG_EXT_INTER
162
  }
163
  if (counts) ++counts->newmv_mode[mode_ctx][1];
164

165
  if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
166

167
  mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
168
169

  mode_prob = cm->fc->zeromv_prob[mode_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
170
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
171
    if (counts) ++counts->zeromv_mode[mode_ctx][0];
172
173
    return ZEROMV;
  }
174
  if (counts) ++counts->zeromv_mode[mode_ctx][1];
175

176
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
177

178
179
180
  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;
181

182
  mode_prob = cm->fc->refmv_prob[mode_ctx];
183

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

187
188
    return NEARESTMV;
  } else {
189
    if (counts) ++counts->refmv_mode[mode_ctx][1];
190
191
192
193
194
    return NEARMV;
  }

  // Invalid prediction mode.
  assert(0);
195
196
197
198
#else
#if CONFIG_DAALA_EC
  const int mode = av1_inter_mode_inv[aom_read_symbol(
      r, cm->fc->inter_mode_cdf[ctx], INTER_MODES, ACCT_STR)];
199
#else
Michael Bebenita's avatar
Michael Bebenita committed
200
201
  const int mode = aom_read_tree(r, av1_inter_mode_tree,
                                 cm->fc->inter_mode_probs[ctx], ACCT_STR);
202
#endif
Jingning Han's avatar
Jingning Han committed
203
  FRAME_COUNTS *counts = xd->counts;
204
  if (counts) ++counts->inter_mode[ctx][mode];
Jingning Han's avatar
Jingning Han committed
205
206

  return NEARESTMV + mode;
207
#endif
Jingning Han's avatar
Jingning Han committed
208
209
}

210
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
211
212
213
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);
214
215
  mbmi->ref_mv_idx = 0;

216
217
218
219
  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
220
221
        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
222
        if (!aom_read(r, drl_prob, ACCT_STR)) {
223
          mbmi->ref_mv_idx = idx;
224
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
225
226
227
          return;
        }
        mbmi->ref_mv_idx = idx + 1;
228
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
229
230
231
232
      }
    }
  }

233
234
235
236
237
238
239
  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
240
241
        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
242
        if (!aom_read(r, drl_prob, ACCT_STR)) {
243
          mbmi->ref_mv_idx = idx - 1;
244
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
245
246
          return;
        }
247
        mbmi->ref_mv_idx = idx;
248
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
249
250
251
252
253
254
      }
    }
  }
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
255
256
257
#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
258
259
260
  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
261

Yue Chen's avatar
Yue Chen committed
262
263
264
265
266
267
268
269
  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
270
271
272
273
274
    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
275
#if CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
276
  }
Yue Chen's avatar
Yue Chen committed
277
#endif  // CONFIG_MOTION_VAR && CONFIG_WARPED_MOTION
Yaowu Xu's avatar
Yaowu Xu committed
278
279
280
}
#endif  // CONFIG_MOTION_VAR || CONFIG_WARPED_MOTION

281
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
282
283
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
284
285
286
  const int mode =
      aom_read_tree(r, av1_inter_compound_mode_tree,
                    cm->fc->inter_compound_mode_probs[ctx], ACCT_STR);
287
288
  FRAME_COUNTS *counts = xd->counts;

289
  if (counts) ++counts->inter_compound_mode[ctx][mode];
290
291
292
293
294
295

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

296
static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
297
#if CONFIG_DAALA_EC
Michael Bebenita's avatar
Michael Bebenita committed
298
  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
299
#else
Michael Bebenita's avatar
Michael Bebenita committed
300
  return aom_read_tree(r, av1_segment_tree, segp->tree_probs, ACCT_STR);
301
#endif
Jingning Han's avatar
Jingning Han committed
302
303
}

304
#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
305
static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
306
                               MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
307
308
                               TX_SIZE tx_size, int depth, int blk_row,
                               int blk_col, aom_reader *r) {
309
  int is_split = 0;
310
311
  const int tx_row = blk_row >> 1;
  const int tx_col = blk_col >> 1;
312
313
  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);
314
  int ctx = txfm_partition_context(xd->above_txfm_context + tx_col,
315
316
                                   xd->left_txfm_context + tx_row,
                                   mbmi->sb_type, tx_size);
clang-format's avatar
clang-format committed
317
  TX_SIZE(*const inter_tx_size)
318
319
320
  [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;
321

Jingning Han's avatar
Jingning Han committed
322
  if (depth == MAX_VARTX_DEPTH) {
323
324
    int idx, idy;
    inter_tx_size[0][0] = tx_size;
325
326
    for (idy = 0; idy < tx_size_high_unit[tx_size] / 2; ++idy)
      for (idx = 0; idx < tx_size_wide_unit[tx_size] / 2; ++idx)
327
328
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
329
    mbmi->min_tx_size = AOMMIN(mbmi->min_tx_size, get_min_tx_size(tx_size));
330
331
332
333
334
335
    if (counts) ++counts->txfm_partition[ctx][0];
    txfm_partition_update(xd->above_txfm_context + tx_col,
                          xd->left_txfm_context + tx_row, tx_size);
    return;
  }

Michael Bebenita's avatar
Michael Bebenita committed
336
  is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
337
338

  if (is_split) {
339
340
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
    const int bsl = tx_size_wide_unit[sub_txs];
341
    int i;
342

343
    if (counts) ++counts->txfm_partition[ctx][1];
344

345
    if (tx_size == TX_8X8) {
346
347
      inter_tx_size[0][0] = TX_4X4;
      mbmi->tx_size = TX_4X4;
348
      mbmi->min_tx_size = get_min_tx_size(mbmi->tx_size);
349
350
      txfm_partition_update(xd->above_txfm_context + tx_col,
                            xd->left_txfm_context + tx_row, TX_4X4);
351
352
353
354
355
      return;
    }

    assert(bsl > 0);
    for (i = 0; i < 4; ++i) {
356
357
358
      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,
359
                         offsetc, r);
360
361
    }
  } else {
362
    int idx, idy;
363
    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
    if (counts) ++counts->txfm_partition[ctx][0];
370
371
    txfm_partition_update(xd->above_txfm_context + tx_col,
                          xd->left_txfm_context + tx_row, tx_size);
372
373
374
375
  }
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
376
377
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
378
379
  FRAME_COUNTS *counts = xd->counts;
  const int ctx = get_tx_size_context(xd);
380
381
382
  int depth = aom_read_tree(r, av1_tx_size_tree[tx_size_cat],
                            cm->fc->tx_size_probs[tx_size_cat][ctx], ACCT_STR);
  TX_SIZE tx_size = depth_to_tx_size(depth);
383
  if (counts) ++counts->tx_size[tx_size_cat][ctx][depth];
384
  return tx_size;
Jingning Han's avatar
Jingning Han committed
385
386
}

Yaowu Xu's avatar
Yaowu Xu committed
387
388
static TX_SIZE read_tx_size_intra(AV1_COMMON *cm, MACROBLOCKD *xd,
                                  aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
389
390
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
391
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
392
393
  if (bsize >= BLOCK_8X8) {
    if (tx_mode == TX_MODE_SELECT) {
394
395
396
397
      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;
398
    } else {
399
      return tx_size_from_tx_mode(bsize, cm->tx_mode, 0);
400
401
402
403
404
405
    }
  } else {
    return TX_4X4;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
406
407
static TX_SIZE read_tx_size_inter(AV1_COMMON *cm, MACROBLOCKD *xd,
                                  int allow_select, aom_reader *r) {
408
409
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
410
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
411
412
  if (bsize >= BLOCK_8X8) {
    if (allow_select && tx_mode == TX_MODE_SELECT) {
413
414
      const TX_SIZE coded_tx_size =
          read_selected_tx_size(cm, xd, inter_tx_size_cat_lookup[bsize], r);
415
#if CONFIG_EXT_TX && CONFIG_RECT_TX
416
417
418
419
      if (coded_tx_size > max_txsize_lookup[bsize]) {
        assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
        return max_txsize_rect_lookup[bsize];
      }
420
421
422
#else
      assert(coded_tx_size <= max_txsize_lookup[bsize]);
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX
423
      return coded_tx_size;
424
    } else {
425
      return tx_size_from_tx_mode(bsize, cm->tx_mode, 1);
426
427
    }
  } else {
428
#if CONFIG_EXT_TX && CONFIG_RECT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
429
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
430
431
432
    return max_txsize_rect_lookup[bsize];
#else
    return TX_4X4;
433
#endif
434
  }
Jingning Han's avatar
Jingning Han committed
435
436
}

Yaowu Xu's avatar
Yaowu Xu committed
437
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Jingning Han's avatar
Jingning Han committed
438
439
440
441
442
                              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++)
443
      segment_id =
Yaowu Xu's avatar
Yaowu Xu committed
444
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
445
446
447
448
449

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

Yaowu Xu's avatar
Yaowu Xu committed
450
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
451
                           int segment_id) {
Jingning Han's avatar
Jingning Han committed
452
453
454
455
456
457
458
459
460
  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
461
static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
462
                                 int mi_offset, int x_mis, int y_mis,
Yaowu Xu's avatar
Yaowu Xu committed
463
                                 aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
464
  struct segmentation *const seg = &cm->seg;
465
466
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
467
468
  int segment_id;

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

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

473
  segment_id = read_segment_id(r, segp);
474
  if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
475
476
477
478
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
479
static void copy_segment_id(const AV1_COMMON *cm,
480
481
482
                            const uint8_t *last_segment_ids,
                            uint8_t *current_segment_ids, int mi_offset,
                            int x_mis, int y_mis) {
483
484
485
486
  int x, y;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
487
488
489
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
                           : 0;
490
491
}

Yaowu Xu's avatar
Yaowu Xu committed
492
493
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
494
  struct segmentation *const seg = &cm->seg;
495
496
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
497
498
499
  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
500
501
  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
502
503

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

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

509
510
511
512
  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
513
514
515
516
517
518
519
520

  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
521
522
    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
523
    mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
524
    if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
525
526
527
528
    if (mbmi->seg_id_predicted) {
      segment_id = predicted_segment_id;
    } else {
      segment_id = read_segment_id(r, segp);
529
      if (counts) ++counts->seg.tree_mispred[segment_id];
530
    }
Jingning Han's avatar
Jingning Han committed
531
  } else {
532
    segment_id = read_segment_id(r, segp);
533
    if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
534
535
536
537
538
  }
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
539
540
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
                     aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
541
542
543
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
544
    const int ctx = av1_get_skip_context(xd);
Michael Bebenita's avatar
Michael Bebenita committed
545
    const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
546
    FRAME_COUNTS *counts = xd->counts;
547
    if (counts) ++counts->skip[ctx][skip];
Jingning Han's avatar
Jingning Han committed
548
549
550
551
    return skip;
  }
}

552
#if CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
553
554
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                   aom_reader *r) {
hui su's avatar
hui su committed
555
556
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
557
  const MODE_INFO *const above_mi = xd->above_mi;
558
  const MODE_INFO *const left_mi = xd->left_mi;
hui su's avatar
hui su committed
559
  const BLOCK_SIZE bsize = mbmi->sb_type;
560
561
562
563
564
565
566
567
  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);
Michael Bebenita's avatar
Michael Bebenita committed
568
569
570
    if (aom_read(
            r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx],
            ACCT_STR)) {
571
      pmi->palette_size[0] =
Yaowu Xu's avatar
Yaowu Xu committed
572
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
573
574
                        av1_default_palette_y_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
575
          2;
576
577
      n = pmi->palette_size[0];
      for (i = 0; i < n; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
578
        pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui su's avatar
hui su committed
579

580
581
582
583
      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
584

585
  if (mbmi->uv_mode == DC_PRED) {
Michael Bebenita's avatar
Michael Bebenita committed
586
587
    if (aom_read(r, av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0],
                 ACCT_STR)) {
588
      pmi->palette_size[1] =
Yaowu Xu's avatar
Yaowu Xu committed
589
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
590
591
                        av1_default_palette_uv_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
592
          2;
593
594
595
      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
596
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
597
        pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita's avatar
Michael Bebenita committed
598
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
599
600
601
602
      }
      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
603
604
  }
}
605
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
606

607
608
609
#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
610
611
612
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  FRAME_COUNTS *counts = xd->counts;
613
614
  FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
      &mbmi->filter_intra_mode_info;
hui su's avatar
hui su committed
615

616
617
618
619
620
  if (mbmi->mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[0] == 0
#endif  // CONFIG_PALETTE
      ) {
621
622
623
624
    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
625
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
626
    }
627
628
629
630
    if (counts) {
      ++counts->filter_intra[0]
                            [filter_intra_mode_info->use_filter_intra_mode[0]];
    }
hui su's avatar
hui su committed
631
  }
632
633
634
635
636
  if (mbmi->uv_mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[1] == 0
#endif  // CONFIG_PALETTE
      ) {
637
638
639
640
    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
641
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
642
    }
643
644
645
646
    if (counts) {
      ++counts->filter_intra[1]
                            [filter_intra_mode_info->use_filter_intra_mode[1]];
    }
hui su's avatar
hui su committed
647
648
  }
}
649
#endif  // CONFIG_FILTER_INTRA
650

651
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
652
653
static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                  aom_reader *r) {
654
655
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xu's avatar
Yaowu Xu committed
656
  const int ctx = av1_get_pred_context_intra_interp(xd);
657
658
  int p_angle;

659
  if (bsize < BLOCK_8X8) return;
660
661
662
663
664

  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
665
    if (av1_is_intra_filter_switchable(p_angle)) {
666
      FRAME_COUNTS *counts = xd->counts;
Michael Bebenita's avatar
Michael Bebenita committed
667
668
      mbmi->intra_filter = aom_read_tree(
          r, av1_intra_filter_tree, cm->fc->intra_filter_probs[ctx], ACCT_STR);
669
      if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
670
671
672
673
674
675
676
677
678
679
    } 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
680
681
#endif  // CONFIG_EXT_INTRA

Jingning Han's avatar
Jingning Han committed
682
683
684
685
686
687
688
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);
689
690
691
#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
692
  const TX_SIZE tx_size = mbmi->tx_size;
693
#endif
Jingning Han's avatar
Jingning Han committed
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
  if (!FIXED_TX_TYPE) {
#if CONFIG_EXT_TX
    if (get_ext_tx_types(tx_size, mbmi->sb_type, inter_block) > 1 &&
        cm->base_qindex > 0 && !mbmi->skip &&
#if CONFIG_SUPERTX
        !supertx_enabled &&
#endif  // CONFIG_SUPERTX
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
      int eset = get_ext_tx_set(tx_size, mbmi->sb_type, inter_block);
      FRAME_COUNTS *counts = xd->counts;

      if (inter_block) {
        if (eset > 0) {
          mbmi->tx_type = aom_read_tree(
              r, av1_ext_tx_inter_tree[eset],
              cm->fc->inter_ext_tx_prob[eset][txsize_sqr_map[tx_size]],
              ACCT_STR);
          if (counts)
            ++counts->inter_ext_tx[eset][txsize_sqr_map[tx_size]]
                                  [mbmi->tx_type];
        }
      } else if (ALLOW_INTRA_EXT_TX) {
        if (eset > 0) {
          mbmi->tx_type = aom_read_tree(
              r, av1_ext_tx_intra_tree[eset],
              cm->fc->intra_ext_tx_prob[eset][tx_size][mbmi->mode], ACCT_STR);
          if (counts)
            ++counts->intra_ext_tx[eset][tx_size][mbmi->mode][mbmi->tx_type];
        }
      }
    } else {
      mbmi->tx_type = DCT_DCT;
    }
#else
    if (tx_size < TX_32X32 && cm->base_qindex > 0 && !mbmi->skip &&
#if CONFIG_SUPERTX
        !supertx_enabled &&
#endif  // CONFIG_SUPERTX
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
      FRAME_COUNTS *counts = xd->counts;
      if (inter_block) {
#if CONFIG_DAALA_EC
        mbmi->tx_type = av1_ext_tx_inv[aom_read_symbol(
            r, cm->fc->inter_ext_tx_cdf[tx_size], TX_TYPES, ACCT_STR)];
#else
        mbmi->tx_type = aom_read_tree(
            r, av1_ext_tx_tree, cm->fc->inter_ext_tx_prob[tx_size], ACCT_STR);
#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];
#if CONFIG_DAALA_EC
        mbmi->tx_type = av1_ext_tx_inv[aom_read_symbol(
            r, cm->fc->intra_ext_tx_cdf[tx_size][tx_type_nom], TX_TYPES,
            ACCT_STR)];
#else
        mbmi->tx_type = aom_read_tree(
            r, av1_ext_tx_tree, cm->fc->intra_ext_tx_prob[tx_size][tx_type_nom],
            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
763
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
764
                                       MACROBLOCKD *const xd, int mi_row,
Yaowu Xu's avatar
Yaowu Xu committed
765
                                       int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
766
767
768
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const MODE_INFO *above_mi = xd->above_mi;
769
  const MODE_INFO *left_mi = xd->left_mi;
Jingning Han's avatar
Jingning Han committed
770
771
772
773
774
775
776
  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
777
778
  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
779

780
  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
Jingning Han's avatar
Jingning Han committed
781
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
782
783
784

#if CONFIG_DELTA_Q
  if (cm->delta_q_present_flag) {
785
786
787
788
    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;
789
790
791
  }
#endif

792
  mbmi->tx_size = read_tx_size_intra(cm, xd, r);
Jingning Han's avatar
Jingning Han committed
793
794
795
796
797
798
799
  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 =
800
#if CONFIG_DAALA_EC
801
            read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, i));
802
#else
803
            read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, i));
804
#endif
Jingning Han's avatar
Jingning Han committed
805
806
807
808
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
      mi->bmi[0].as_mode = mi->bmi[2].as_mode =
809
#if CONFIG_DAALA_EC
810
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
811
#else
812
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
813
#endif
Jingning Han's avatar
Jingning Han committed
814
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
815
#if CONFIG_DAALA_EC
816
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 1));
817
#else
818
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 1));
819
#endif
Jingning Han's avatar
Jingning Han committed
820
821
822
      break;
    case BLOCK_8X4:
      mi->bmi[0].as_mode = mi->bmi[1].as_mode =
823
#if CONFIG_DAALA_EC
824
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
825
#else
826
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
827
#endif
Jingning Han's avatar
Jingning Han committed
828
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
829
#if CONFIG_DAALA_EC
830
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 2));
831
#else
832
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 2));
833
#endif
Jingning Han's avatar
Jingning Han committed
834
835
      break;
    default:
836
      mbmi->mode =
837
#if CONFIG_DAALA_EC
838
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
839
#else
840
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
841
#endif
Jingning Han's avatar
Jingning Han committed
842
843
  }

844
  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
hui su's avatar
hui su committed
845
#if CONFIG_EXT_INTRA
846
847
  read_intra_angle_info(cm, xd, r);
#endif  // CONFIG_EXT_INTRA
848
#if CONFIG_PALETTE
hui su's avatar
hui su committed
849
850
  mbmi->palette_mode_info.palette_size[0] = 0;
  mbmi->palette_mode_info.palette_size[1] = 0;
851
  if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
hui su's avatar
hui su committed
852
    read_palette_mode_info(cm, xd, r);
853
#endif  // CONFIG_PALETTE
854
855
856
857
858
#if CONFIG_FILTER_INTRA
  mbmi->filter_intra_mode_info.use_filter_intra_mode[0] = 0;
  mbmi->filter_intra_mode_info.use_filter_intra_mode[1] = 0;
  if (bsize >= BLOCK_8X8) read_filter_intra_mode_info(cm, xd, r);
#endif  // CONFIG_FILTER_INTRA
hui su's avatar
hui su committed
859

Jingning Han's avatar
Jingning Han committed
860
861
862
  read_tx_type(cm, xd, mbmi,
#if CONFIG_SUPERTX
               0,
863
#endif
Jingning Han's avatar
Jingning Han committed
864
               r);
Jingning Han's avatar
Jingning Han committed
865
866
}

867
static int read_mv_component(aom_reader *r, nmv_component *mvcomp, int usehp) {
Jingning Han's avatar
Jingning Han committed
868
  int mag, d, fr, hp;
Michael Bebenita's avatar
Michael Bebenita committed
869
870
  const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
  const int mv_class =
871
#if CONFIG_EC_MULTISYMBOL
872
873
      aom_read_symbol(r, mvcomp->class_cdf, MV_CLASSES, ACCT_STR);
#else
Michael Bebenita's avatar
Michael Bebenita committed
874
      aom_read_tree(r, av1_mv_class_tree, mvcomp->classes, ACCT_STR);
875
#endif
Jingning Han's avatar
Jingning Han committed
876
877
878
879
  const int class0 = mv_class == MV_CLASS_0;

  // Integer part
  if (class0) {
880
    d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
881
882
883
884
885
886
    mag = 0;
  } else {
    int i;
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits

    d = 0;
Michael Bebenita's avatar
Michael Bebenita committed
887
    for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Jingning Han's avatar
Jingning Han committed
888
889
890
    mag = CLASS0_SIZE << (mv_class + 2);
  }

891
// Fractional part
892
#if CONFIG_EC_MULTISYMBOL
893
894
895
  fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
                       MV_FP_SIZE, ACCT_STR);
#else
Yaowu Xu's avatar
Yaowu Xu committed
896
  fr = aom_read_tree(r, av1_mv_fp_tree,
Michael Bebenita's avatar
Michael Bebenita committed
897
                     class0 ? mvcomp->class0_fp[d] : mvcomp->fp, ACCT_STR);
898
#endif
Jingning Han's avatar
Jingning Han committed
899
900

  // High precision part (if hp is not used, the default value of the hp is 1)
Michael Bebenita's avatar
Michael Bebenita committed
901
902
  hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
             : 1;
Jingning Han's avatar
Jingning Han committed
903
904
905
906
907
908

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

Yaowu Xu's avatar
Yaowu Xu committed
909
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
910
                           nmv_context *ctx, nmv_context_counts *counts,
911
                           int allow_hp) {
912
  MV_JOINT_TYPE joint_type;
913
  MV diff = { 0, 0 };
Michael Bebenita's avatar
Michael Bebenita committed
914
  joint_type =
915
#if CONFIG_EC_MULTISYMBOL
916
917
      (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joint_cdf, MV_JOINTS, ACCT_STR);
#else
Michael Bebenita's avatar
Michael Bebenita committed
918
      (MV_JOINT_TYPE)aom_read_tree(r, av1_mv_joint_tree, ctx->joints, ACCT_STR);
919
#endif
920