decodemv.c 42.2 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
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>

Yaowu Xu's avatar
Yaowu Xu committed
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"
Jingning Han's avatar
Jingning Han committed
22

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

Adrian Grange's avatar
Adrian Grange committed
26
#include "aom_dsp/aom_dsp_common.h"
27

Michael Bebenita's avatar
Michael Bebenita committed
28
29
#define ACCT_STR __func__

30
#if CONFIG_DAALA_EC
31
static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
32
33
34
  return (PREDICTION_MODE)
      av1_intra_mode_inv[aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR)];
}
35
#else
Adrian Grange's avatar
Adrian Grange committed
36
static PREDICTION_MODE read_intra_mode(aom_reader *r, const aom_prob *p) {
Michael Bebenita's avatar
Michael Bebenita committed
37
  return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p, ACCT_STR);
Jingning Han's avatar
Jingning Han committed
38
}
39
#endif
Jingning Han's avatar
Jingning Han committed
40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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

77
static PREDICTION_MODE read_intra_mode_y(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
78
                                         aom_reader *r, int size_group) {
Jingning Han's avatar
Jingning Han committed
79
  const PREDICTION_MODE y_mode =
80
#if CONFIG_DAALA_EC
81
      read_intra_mode(r, cm->fc->y_mode_cdf[size_group]);
82
#else
Jingning Han's avatar
Jingning Han committed
83
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
84
#endif
Jingning Han's avatar
Jingning Han committed
85
  FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
86
  if (counts) ++counts->y_mode[size_group][y_mode];
Jingning Han's avatar
Jingning Han committed
87
88
89
  return y_mode;
}

90
static PREDICTION_MODE read_intra_mode_uv(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
91
                                          aom_reader *r,
Jingning Han's avatar
Jingning Han committed
92
                                          PREDICTION_MODE y_mode) {
clang-format's avatar
clang-format committed
93
  const PREDICTION_MODE uv_mode =
94
95
96
#if CONFIG_DAALA_EC
      read_intra_mode(r, cm->fc->uv_mode_cdf[y_mode]);
#else
clang-format's avatar
clang-format committed
97
      read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
98
#endif
Jingning Han's avatar
Jingning Han committed
99
  FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
100
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
Jingning Han's avatar
Jingning Han committed
101
102
103
  return uv_mode;
}

104
static PREDICTION_MODE read_inter_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
105
                                       aom_reader *r, int16_t ctx) {
106
107
#if CONFIG_REF_MV
  FRAME_COUNTS *counts = xd->counts;
108
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
109
110
  aom_prob mode_prob = cm->fc->newmv_prob[mode_ctx];

Michael Bebenita's avatar
Michael Bebenita committed
111
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
clang-format's avatar
clang-format committed
112
    if (counts) ++counts->newmv_mode[mode_ctx][0];
113
114
    return NEWMV;
  }
clang-format's avatar
clang-format committed
115
  if (counts) ++counts->newmv_mode[mode_ctx][1];
116

clang-format's avatar
clang-format committed
117
  if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
118

119
  mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
120
121

  mode_prob = cm->fc->zeromv_prob[mode_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
122
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
clang-format's avatar
clang-format committed
123
    if (counts) ++counts->zeromv_mode[mode_ctx][0];
124
125
    return ZEROMV;
  }
clang-format's avatar
clang-format committed
126
  if (counts) ++counts->zeromv_mode[mode_ctx][1];
127

128
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
129

clang-format's avatar
clang-format committed
130
131
132
  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;
133

134
  mode_prob = cm->fc->refmv_prob[mode_ctx];
Jingning Han's avatar
Jingning Han committed
135
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
clang-format's avatar
clang-format committed
136
    if (counts) ++counts->refmv_mode[mode_ctx][0];
137
138
    return NEARESTMV;
  } else {
clang-format's avatar
clang-format committed
139
    if (counts) ++counts->refmv_mode[mode_ctx][1];
140
141
142
143
144
    return NEARMV;
  }

  // Invalid prediction mode.
  assert(0);
145
146
147
148
#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)];
149
#else
Michael Bebenita's avatar
Michael Bebenita committed
150
151
  const int mode = aom_read_tree(r, av1_inter_mode_tree,
                                 cm->fc->inter_mode_probs[ctx], ACCT_STR);
152
#endif
Jingning Han's avatar
Jingning Han committed
153
  FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
154
  if (counts) ++counts->inter_mode[ctx][mode];
Jingning Han's avatar
Jingning Han committed
155
156

  return NEARESTMV + mode;
157
#endif
Jingning Han's avatar
Jingning Han committed
158
159
}

160
#if CONFIG_REF_MV
clang-format's avatar
clang-format committed
161
162
static void read_drl_idx(const AV1_COMMON *cm, MACROBLOCKD *xd,
                         MB_MODE_INFO *mbmi, aom_reader *r) {
163
164
165
  uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
  mbmi->ref_mv_idx = 0;

166
167
168
169
170
171
  if (mbmi->mode == NEWMV) {
    int idx;
    for (idx = 0; idx < 2; ++idx) {
      if (xd->ref_mv_count[ref_frame_type] > idx + 1) {
        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];
Jingning Han's avatar
Jingning Han committed
172
        if (!aom_read(r, drl_prob, ACCT_STR)) {
173
          mbmi->ref_mv_idx = idx;
clang-format's avatar
clang-format committed
174
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
175
176
177
          return;
        }
        mbmi->ref_mv_idx = idx + 1;
clang-format's avatar
clang-format committed
178
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
179
180
181
182
      }
    }
  }

183
184
185
186
187
188
189
190
191
  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) {
        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];
Jingning Han's avatar
Jingning Han committed
192
        if (!aom_read(r, drl_prob, ACCT_STR)) {
193
          mbmi->ref_mv_idx = idx - 1;
clang-format's avatar
clang-format committed
194
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
195
196
          return;
        }
197
        mbmi->ref_mv_idx = idx;
clang-format's avatar
clang-format committed
198
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
199
200
201
202
203
204
      }
    }
  }
}
#endif

205
206
207
208
209
210
211
#if CONFIG_MOTION_VAR
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;

Michael Bebenita's avatar
Michael Bebenita committed
212
213
214
    motion_mode =
        aom_read_tree(r, av1_motion_mode_tree,
                      cm->fc->motion_mode_prob[mbmi->sb_type], ACCT_STR);
215
216
217
218
219
220
221
222
    if (counts) ++counts->motion_mode[mbmi->sb_type][motion_mode];
    return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
  } else {
    return SIMPLE_TRANSLATION;
  }
}
#endif  // CONFIG_MOTION_VAR

223
static int read_segment_id(aom_reader *r, struct segmentation_probs *segp) {
224
#if CONFIG_DAALA_EC
Michael Bebenita's avatar
Michael Bebenita committed
225
  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
226
#else
Michael Bebenita's avatar
Michael Bebenita committed
227
  return aom_read_tree(r, av1_segment_tree, segp->tree_probs, ACCT_STR);
228
#endif
Jingning Han's avatar
Jingning Han committed
229
230
}

231
static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
232
                                     TX_SIZE max_tx_size, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
233
234
  FRAME_COUNTS *counts = xd->counts;
  const int ctx = get_tx_size_context(xd);
Adrian Grange's avatar
Adrian Grange committed
235
  const aom_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
236
  TX_SIZE tx_size = aom_read(r, tx_probs[TX_4X4], ACCT_STR) ? TX_8X8 : TX_4X4;
Jingning Han's avatar
Jingning Han committed
237
  if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
238
    tx_size += aom_read(r, tx_probs[TX_8X8], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
239
    if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
240
      tx_size += aom_read(r, tx_probs[TX_16X16], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
241
242
  }

clang-format's avatar
clang-format committed
243
  if (counts) ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
244
  return tx_size;
Jingning Han's avatar
Jingning Han committed
245
246
}

247
static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int allow_select,
Adrian Grange's avatar
Adrian Grange committed
248
                            aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
249
250
251
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
  const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
clang-format's avatar
clang-format committed
252
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
Jingning Han's avatar
Jingning Han committed
253
254
255
  if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
    return read_selected_tx_size(cm, xd, max_tx_size, r);
  else
Adrian Grange's avatar
Adrian Grange committed
256
    return AOMMIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
Jingning Han's avatar
Jingning Han committed
257
258
}

259
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Jingning Han's avatar
Jingning Han committed
260
261
262
263
264
                              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++)
265
      segment_id =
Adrian Grange's avatar
Adrian Grange committed
266
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
267
268
269
270
271

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

272
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
clang-format's avatar
clang-format committed
273
                           int segment_id) {
Jingning Han's avatar
Jingning Han committed
274
275
276
277
278
279
280
281
282
  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;
}

283
static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
284
                                 int mi_offset, int x_mis, int y_mis,
Adrian Grange's avatar
Adrian Grange committed
285
                                 aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
286
  struct segmentation *const seg = &cm->seg;
287
288
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
289
290
  int segment_id;

clang-format's avatar
clang-format committed
291
  if (!seg->enabled) return 0;  // Default for disabled segmentation
Jingning Han's avatar
Jingning Han committed
292

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

295
  segment_id = read_segment_id(r, segp);
clang-format's avatar
clang-format committed
296
  if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
297
298
299
300
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

301
static void copy_segment_id(const AV1_COMMON *cm,
clang-format's avatar
clang-format committed
302
303
304
                            const uint8_t *last_segment_ids,
                            uint8_t *current_segment_ids, int mi_offset,
                            int x_mis, int y_mis) {
305
306
307
308
  int x, y;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
clang-format's avatar
clang-format committed
309
310
311
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
                           : 0;
312
313
}

314
static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
315
                                 int mi_row, int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
316
  struct segmentation *const seg = &cm->seg;
317
318
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
319
320
321
322
323
324
325
  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;
  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 ?????
Adrian Grange's avatar
Adrian Grange committed
326
327
  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
328

clang-format's avatar
clang-format committed
329
  if (!seg->enabled) return 0;  // Default for disabled segmentation
Jingning Han's avatar
Jingning Han committed
330

clang-format's avatar
clang-format committed
331
332
333
334
  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
335
336
337
338
339
340
341
342

  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) {
343
    const int ctx = av1_get_pred_context_seg_id(xd);
Adrian Grange's avatar
Adrian Grange committed
344
    const aom_prob pred_prob = segp->pred_probs[ctx];
Michael Bebenita's avatar
Michael Bebenita committed
345
    mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
clang-format's avatar
clang-format committed
346
    if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
347
348
349
350
    if (mbmi->seg_id_predicted) {
      segment_id = predicted_segment_id;
    } else {
      segment_id = read_segment_id(r, segp);
clang-format's avatar
clang-format committed
351
      if (counts) ++counts->seg.tree_mispred[segment_id];
352
    }
Jingning Han's avatar
Jingning Han committed
353
  } else {
354
    segment_id = read_segment_id(r, segp);
clang-format's avatar
clang-format committed
355
    if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
356
357
358
359
360
  }
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

361
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
Adrian Grange's avatar
Adrian Grange committed
362
                     aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
363
364
365
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
366
    const int ctx = av1_get_skip_context(xd);
Michael Bebenita's avatar
Michael Bebenita committed
367
    const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
368
    FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
369
    if (counts) ++counts->skip[ctx][skip];
Jingning Han's avatar
Jingning Han committed
370
371
372
373
    return skip;
  }
}

374
#if CONFIG_EXT_INTRA || CONFIG_PALETTE
hui su's avatar
hui su committed
375
376
377
static INLINE int read_uniform(aom_reader *r, int n) {
  const int l = get_unsigned_bits(n);
  const int m = (1 << l) - n;
Michael Bebenita's avatar
Michael Bebenita committed
378
  const int v = aom_read_literal(r, l - 1, ACCT_STR);
hui su's avatar
hui su committed
379
380

  assert(l != 0);
381
  return (v < m) ? v : ((v << 1) - m + aom_read_literal(r, 1, ACCT_STR));
hui su's avatar
hui su committed
382
}
383
#endif  // CONFIG_EXT_INTRA || CONFIG_PALETTE
hui su's avatar
hui su committed
384

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#if CONFIG_PALETTE
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                   aom_reader *r) {
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const MODE_INFO *const above_mi = xd->above_mi;
  const MODE_INFO *const left_mi = xd->left_mi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
  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);
    if (aom_read(
402
403
            r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx],
            ACCT_STR)) {
404
405
      pmi->palette_size[0] =
          aom_read_tree(r, av1_palette_size_tree,
406
407
                        av1_default_palette_y_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
408
409
410
          2;
      n = pmi->palette_size[0];
      for (i = 0; i < n; ++i)
411
        pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
412
413
414
415
416
      xd->plane[0].color_index_map[0] = read_uniform(r, n);
      assert(xd->plane[0].color_index_map[0] < n);
    }
  }
  if (mbmi->uv_mode == DC_PRED) {
417
418
    if (aom_read(r, av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0],
                 ACCT_STR)) {
419
420
      pmi->palette_size[1] =
          aom_read_tree(r, av1_palette_size_tree,
421
422
                        av1_default_palette_uv_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
423
424
425
426
          2;
      n = pmi->palette_size[1];
      for (i = 0; i < n; ++i) {
        pmi->palette_colors[PALETTE_MAX_SIZE + i] =
427
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
428
        pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
429
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
430
431
432
433
434
435
436
437
438
      }
      xd->plane[1].color_index_map[0] = read_uniform(r, n);
      assert(xd->plane[1].color_index_map[0] < n);
    }
  }
}
#endif  // CONFIG_PALETTE

#if CONFIG_EXT_INTRA
hui su's avatar
hui su committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
static void read_intra_angle_info(MB_MODE_INFO *const mbmi, aom_reader *r) {
  mbmi->intra_angle_delta[0] = 0;
  mbmi->intra_angle_delta[1] = 0;
  if (mbmi->sb_type < BLOCK_8X8) return;

  if (is_directional_mode(mbmi->mode)) {
    const TX_SIZE max_tx_size = max_txsize_lookup[mbmi->sb_type];
    const int max_angle_delta = av1_max_angle_delta_y[max_tx_size][mbmi->mode];
    mbmi->intra_angle_delta[0] =
        read_uniform(r, 2 * max_angle_delta + 1) - max_angle_delta;
  }

  if (is_directional_mode(mbmi->uv_mode)) {
    mbmi->intra_angle_delta[1] =
        read_uniform(r, 2 * MAX_ANGLE_DELTA_UV + 1) - MAX_ANGLE_DELTA_UV;
  }
}
#endif  // CONFIG_EXT_INTRA

458
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
clang-format's avatar
clang-format committed
459
                                       MACROBLOCKD *const xd, int mi_row,
Adrian Grange's avatar
Adrian Grange committed
460
                                       int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
461
462
463
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const MODE_INFO *above_mi = xd->above_mi;
clang-format's avatar
clang-format committed
464
  const MODE_INFO *left_mi = xd->left_mi;
Jingning Han's avatar
Jingning Han committed
465
466
467
468
469
470
471
  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 ?????
Adrian Grange's avatar
Adrian Grange committed
472
473
  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
474

475
  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
Jingning Han's avatar
Jingning Han committed
476
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
477
478
479

#if CONFIG_DELTA_Q
  if (cm->delta_q_present_flag) {
480
481
482
483
    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;
484
485
486
  }
#endif

Jingning Han's avatar
Jingning Han committed
487
488
489
490
491
492
493
494
  mbmi->tx_size = read_tx_size(cm, xd, 1, r);
  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 =
495
#if CONFIG_DAALA_EC
496
            read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, i));
497
#else
498
            read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, i));
499
#endif
Jingning Han's avatar
Jingning Han committed
500
501
502
503
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
      mi->bmi[0].as_mode = mi->bmi[2].as_mode =
504
#if CONFIG_DAALA_EC
505
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
506
#else
507
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
508
#endif
Jingning Han's avatar
Jingning Han committed
509
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
510
#if CONFIG_DAALA_EC
511
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 1));
512
#else
513
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 1));
514
#endif
Jingning Han's avatar
Jingning Han committed
515
516
517
      break;
    case BLOCK_8X4:
      mi->bmi[0].as_mode = mi->bmi[1].as_mode =
518
#if CONFIG_DAALA_EC
519
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
520
#else
521
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
522
#endif
Jingning Han's avatar
Jingning Han committed
523
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
524
#if CONFIG_DAALA_EC
525
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 2));
526
#else
527
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 2));
528
#endif
Jingning Han's avatar
Jingning Han committed
529
530
      break;
    default:
clang-format's avatar
clang-format committed
531
      mbmi->mode =
532
#if CONFIG_DAALA_EC
533
          read_intra_mode(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
534
#else
clang-format's avatar
clang-format committed
535
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
536
#endif
Jingning Han's avatar
Jingning Han committed
537
538
  }

539
  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
hui su's avatar
hui su committed
540
541
542
#if CONFIG_EXT_INTRA
  read_intra_angle_info(mbmi, r);
#endif  // CONFIG_EXT_INTRA
543

544
545
546
547
548
549
550
#if CONFIG_PALETTE
  mbmi->palette_mode_info.palette_size[0] = 0;
  mbmi->palette_mode_info.palette_size[1] = 0;
  if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
    read_palette_mode_info(cm, xd, r);
#endif  // CONFIG_PALETTE

clang-format's avatar
clang-format committed
551
  if (mbmi->tx_size < TX_32X32 && cm->base_qindex > 0 && !mbmi->skip &&
552
553
554
      !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];
555
556
557
558
559
#if CONFIG_DAALA_EC
    mbmi->tx_type = av1_ext_tx_inv[aom_read_symbol(
        r, cm->fc->intra_ext_tx_cdf[mbmi->tx_size][tx_type_nom], TX_TYPES,
        ACCT_STR)];
#else
Michael Bebenita's avatar
Michael Bebenita committed
560
561
562
    mbmi->tx_type = aom_read_tree(
        r, av1_ext_tx_tree,
        cm->fc->intra_ext_tx_prob[mbmi->tx_size][tx_type_nom], ACCT_STR);
563
#endif
564
565
566
567
568
    if (counts)
      ++counts->intra_ext_tx[mbmi->tx_size][tx_type_nom][mbmi->tx_type];
  } else {
    mbmi->tx_type = DCT_DCT;
  }
Jingning Han's avatar
Jingning Han committed
569
570
}

571
static int read_mv_component(aom_reader *r, nmv_component *mvcomp, int usehp) {
Jingning Han's avatar
Jingning Han committed
572
  int mag, d, fr, hp;
Michael Bebenita's avatar
Michael Bebenita committed
573
574
  const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
  const int mv_class =
575
#if CONFIG_EC_MULTISYMBOL
576
577
      aom_read_symbol(r, mvcomp->class_cdf, MV_CLASSES, ACCT_STR);
#else
Michael Bebenita's avatar
Michael Bebenita committed
578
      aom_read_tree(r, av1_mv_class_tree, mvcomp->classes, ACCT_STR);
579
#endif
Jingning Han's avatar
Jingning Han committed
580
581
582
583
  const int class0 = mv_class == MV_CLASS_0;

  // Integer part
  if (class0) {
584
    d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
585
586
587
588
589
590
    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
591
    for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Jingning Han's avatar
Jingning Han committed
592
593
594
    mag = CLASS0_SIZE << (mv_class + 2);
  }

595
// Fractional part
596
#if CONFIG_EC_MULTISYMBOL
597
598
599
  fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
                       MV_FP_SIZE, ACCT_STR);
#else
600
  fr = aom_read_tree(r, av1_mv_fp_tree,
Michael Bebenita's avatar
Michael Bebenita committed
601
                     class0 ? mvcomp->class0_fp[d] : mvcomp->fp, ACCT_STR);
602
#endif
Jingning Han's avatar
Jingning Han committed
603
604

  // High precision part (if hp is not used, the default value of the hp is 1)
Michael Bebenita's avatar
Michael Bebenita committed
605
606
  hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
             : 1;
Jingning Han's avatar
Jingning Han committed
607
608
609
610
611
612

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

Adrian Grange's avatar
Adrian Grange committed
613
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
614
                           nmv_context *ctx, nmv_context_counts *counts,
clang-format's avatar
clang-format committed
615
                           int allow_hp) {
Jingning Han's avatar
Jingning Han committed
616
  const MV_JOINT_TYPE joint_type =
617
#if CONFIG_EC_MULTISYMBOL
618
619
      (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joint_cdf, MV_JOINTS, ACCT_STR);
#else
Michael Bebenita's avatar
Michael Bebenita committed
620
      (MV_JOINT_TYPE)aom_read_tree(r, av1_mv_joint_tree, ctx->joints, ACCT_STR);
621
#endif
clang-format's avatar
clang-format committed
622
  MV diff = { 0, 0 };
Jingning Han's avatar
Jingning Han committed
623
624

  if (mv_joint_vertical(joint_type))
Alex Converse's avatar
Alex Converse committed
625
    diff.row = read_mv_component(r, &ctx->comps[0], allow_hp);
Jingning Han's avatar
Jingning Han committed
626
627

  if (mv_joint_horizontal(joint_type))
Alex Converse's avatar
Alex Converse committed
628
    diff.col = read_mv_component(r, &ctx->comps[1], allow_hp);
Jingning Han's avatar
Jingning Han committed
629

Alex Converse's avatar
Alex Converse committed
630
  av1_inc_mv(&diff, counts, allow_hp);
Jingning Han's avatar
Jingning Han committed
631
632
633
634
635

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

636
static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Jingning Han's avatar
Jingning Han committed
637
                                                const MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
638
                                                aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
639
  if (cm->reference_mode == REFERENCE_MODE_SELECT) {
640
    const int ctx = av1_get_reference_mode_context(cm, xd);
Jingning Han's avatar
Jingning Han committed
641
    const REFERENCE_MODE mode =
Michael Bebenita's avatar
Michael Bebenita committed
642
        (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
643
    FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
644
    if (counts) ++counts->comp_inter[ctx][mode];
Jingning Han's avatar
Jingning Han committed
645
646
647
648
649
650
651
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
  } else {
    return cm->reference_mode;
  }
}

// Read the referncence frame
652
static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
653
                            aom_reader *r, int segment_id,
clang-format's avatar
clang-format committed
654
                            MV_REFERENCE_FRAME ref_frame[2]) {
Jingning Han's avatar
Jingning Han committed
655
656
657
658
659
660
661
662
663
664
665
  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) {
666
667
#if CONFIG_EXT_REFS
      const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
668
      // Read forward references.
669
      const int ctx_fwd = av1_get_pred_context_comp_fwdref_p(cm, xd);
Michael Bebenita's avatar
Michael Bebenita committed
670
671
      const int bit_fwd =
          aom_read(r, fc->comp_fwdref_prob[ctx_fwd][0], ACCT_STR);
672
673
674
      if (counts) ++counts->comp_fwdref[ctx_fwd][0][bit_fwd];
      if (!bit_fwd) {
        const int ctx_fwd1 = av1_get_pred_context_comp_fwdref_p1(cm, xd);
Michael Bebenita's avatar
Michael Bebenita committed
675
676
        const int bit_fwd1 =
            aom_read(r, fc->comp_fwdref_prob[ctx_fwd1][1], ACCT_STR);
677
678
679
680
        if (counts) ++counts->comp_fwdref[ctx_fwd1][1][bit_fwd1];
        ref_frame[!idx] = cm->comp_fwd_ref[bit_fwd1 ? 0 : 1];
      } else {
        const int ctx_fwd2 = av1_get_pred_context_comp_fwdref_p2(cm, xd);
Michael Bebenita's avatar
Michael Bebenita committed
681
682
        const int bit_fwd2 =
            aom_read(r, fc->comp_fwdref_prob[ctx_fwd2][2], ACCT_STR);
683
684
685
        if (counts) ++counts->comp_fwdref[ctx_fwd2][2][bit_fwd2];
        ref_frame[!idx] = cm->comp_fwd_ref[bit_fwd2 ? 3 : 2];
      }
686
687
688
      // Read backward references.
      {
        const int ctx_bwd = av1_get_pred_context_comp_bwdref_p(cm, xd);
Michael Bebenita's avatar
Michael Bebenita committed
689
690
        const int bit_bwd =
            aom_read(r, fc->comp_bwdref_prob[ctx_bwd][0], ACCT_STR);
691
692
693
        if (counts) ++counts->comp_bwdref[ctx_bwd][0][bit_bwd];
        ref_frame[idx] = cm->comp_bwd_ref[bit_bwd];
      }
694
#else
Jingning Han's avatar
Jingning Han committed
695
      const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
696
      const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
Michael Bebenita's avatar
Michael Bebenita committed
697
      const int bit = aom_read(r, fc->comp_ref_prob[ctx], ACCT_STR);
clang-format's avatar
clang-format committed
698
      if (counts) ++counts->comp_ref[ctx][bit];
Jingning Han's avatar
Jingning Han committed
699
700
      ref_frame[idx] = cm->comp_fixed_ref;
      ref_frame[!idx] = cm->comp_var_ref[bit];
701
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
702
    } else if (mode == SINGLE_REFERENCE) {
703
704
#if CONFIG_EXT_REFS
      const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Michael Bebenita's avatar
Michael Bebenita committed
705
      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0], ACCT_STR);
706
707
708
      if (counts) ++counts->single_ref[ctx0][0][bit0];
      if (bit0) {
        const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Michael Bebenita's avatar
Michael Bebenita committed
709
        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1], ACCT_STR);
710
711
712
713
        if (counts) ++counts->single_ref[ctx1][1][bit1];
        ref_frame[0] = bit1 ? ALTREF_FRAME : BWDREF_FRAME;
      } else {
        const int ctx2 = av1_get_pred_context_single_ref_p3(xd);
Michael Bebenita's avatar
Michael Bebenita committed
714
        const int bit2 = aom_read(r, fc->single_ref_prob[ctx2][2], ACCT_STR);
715
        if (counts) ++counts->single_ref[ctx2][2][bit2];
716
        if (!bit2) {
717
          const int ctx3 = av1_get_pred_context_single_ref_p4(xd);
Michael Bebenita's avatar
Michael Bebenita committed
718
          const int bit3 = aom_read(r, fc->single_ref_prob[ctx3][3], ACCT_STR);
719
720
          if (counts) ++counts->single_ref[ctx3][3][bit3];
          ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
721
722
        } else {
          const int ctx4 = av1_get_pred_context_single_ref_p5(xd);
Michael Bebenita's avatar
Michael Bebenita committed
723
          const int bit4 = aom_read(r, fc->single_ref_prob[ctx4][4], ACCT_STR);
724
725
          if (counts) ++counts->single_ref[ctx4][4][bit4];
          ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
726
727
728
        }
      }
#else
729
      const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Michael Bebenita's avatar
Michael Bebenita committed
730
      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0], ACCT_STR);
clang-format's avatar
clang-format committed
731
      if (counts) ++counts->single_ref[ctx0][0][bit0];
Jingning Han's avatar
Jingning Han committed
732
      if (bit0) {
733
        const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Michael Bebenita's avatar
Michael Bebenita committed
734
        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1], ACCT_STR);
clang-format's avatar
clang-format committed
735
        if (counts) ++counts->single_ref[ctx1][1][bit1];
Jingning Han's avatar
Jingning Han committed
736
737
738
739
        ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
      } else {
        ref_frame[0] = LAST_FRAME;
      }
740
#endif  // CONFIG_EXT_REFS
Jingning Han's avatar
Jingning Han committed
741
742
743
744
745
746
747
748

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

James Zern's avatar
James Zern committed
749
750
751
static INLINE InterpFilter read_switchable_interp_filter(AV1_COMMON *const cm,
                                                         MACROBLOCKD *const xd,
                                                         aom_reader *r) {
752
753
754
755
756
757
  if (cm->interp_filter == SWITCHABLE) {
#if CONFIG_EXT_INTERP
    if (is_interp_needed(xd))
#endif
    {
      const int ctx = av1_get_pred_context_switchable_interp(xd);
758
759
#if CONFIG_DAALA_EC
      const InterpFilter type =
760
          (InterpFilter)av1_switchable_interp_inv[aom_read_symbol(
Michael Bebenita's avatar
Michael Bebenita committed
761
762
              r, cm->fc->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS,
              ACCT_STR)];
763
#else
764
      const InterpFilter type = (InterpFilter)aom_read_tree(
Michael Bebenita's avatar
Michael Bebenita committed
765
766
          r, av1_switchable_interp_tree, cm->fc->switchable_interp_prob[ctx],
          ACCT_STR);
767
#endif
768
769
770
771
772
773
774
775
      FRAME_COUNTS *counts = xd->counts;
      if (counts) ++counts->switchable_interp[ctx][type];
      return type;
    }
    return EIGHTTAP;
  } else {
    return cm->interp_filter;
  }
Jingning Han's avatar
Jingning Han committed
776
777
}

778
static void read_intra_block_mode_info(AV1_COMMON *const cm,
Jingning Han's avatar
Jingning Han committed
779
                                       MACROBLOCKD *const xd, MODE_INFO *mi,
Adrian Grange's avatar
Adrian Grange committed
780
                                       aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
781
782
783
784
785
786
787
788
789
790
791
792
793
794
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const BLOCK_SIZE bsize = mi->mbmi.sb_type;
  int i;

  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 = read_intra_mode_y(cm, xd, r, 0);
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
clang-format's avatar
clang-format committed
795
      mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd, r, 0);
Jingning Han's avatar
Jingning Han committed
796
797
798
799
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
          read_intra_mode_y(cm, xd, r, 0);
      break;
    case BLOCK_8X4:
clang-format's avatar
clang-format committed
800
      mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd, r, 0);
Jingning Han's avatar
Jingning Han committed
801
802
803
804
805
806
807
808
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
          read_intra_mode_y(cm, xd, r, 0);
      break;
    default:
      mbmi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
  }

  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
809
810
811
812
813
814
815
#if CONFIG_PALETTE
  mbmi->palette_mode_info.palette_size[0] = 0;
  mbmi->palette_mode_info.palette_size[1] = 0;
  if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
    read_palette_mode_info(cm, xd, r);
#endif  // CONFIG_PALETTE

hui su's avatar
hui su committed
816
817
818
#if CONFIG_EXT_INTRA
  read_intra_angle_info(mbmi, r);
#endif  // CONFIG_EXT_INTRA
Jingning Han's avatar
Jingning Han committed
819
820
821
}

static INLINE int is_mv_valid(const MV *mv) {
clang-format's avatar
clang-format committed
822
823
  return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
         mv->col < MV_UPP;
Jingning Han's avatar
Jingning Han committed
824
825
}

826
static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
827
                            PREDICTION_MODE mode, int block, int_mv mv[2],
clang-format's avatar
clang-format committed
828
829
                            int_mv ref_mv[2], int_mv nearest_mv[2],
                            int_mv near_mv[2], int is_compound, int allow_hp,
Adrian Grange's avatar
Adrian Grange committed
830
                            aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
831
832
833
  int i;
  int ret = 1;

834
835
836
#if CONFIG_REF_MV
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
  BLOCK_SIZE bsize = mbmi->sb_type;
clang-format's avatar
clang-format committed
837
838
  int_mv *pred_mv =
      (bsize >= BLOCK_8X8) ? mbmi->pred_mv : xd->mi[0]->bmi[block].pred_mv;
839
840
841
842
#else
  (void)block;
#endif

Jingning Han's avatar
Jingning Han committed
843
844
845
  switch (mode) {
    case NEWMV: {
      FRAME_COUNTS *counts = xd->counts;
846
#if !CONFIG_REF_MV
Jingning Han's avatar
Jingning Han committed
847
      nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
848
#endif
Jingning Han's avatar
Jingning Han committed
849
      for (i = 0; i < 1 + is_compound; ++i) {
850
#if CONFIG_REF_MV
851
852
853
854
        int8_t rf_type = av1_ref_frame_type(mbmi->ref_frame);
        int nmv_ctx =
            av1_nmv_ctx(xd->ref_mv_count[rf_type], xd->ref_mv_stack[rf_type], i,
                        mbmi->ref_mv_idx);
855
856
857
858
859
        nmv_context_counts *const mv_counts =
            counts ? &counts->mv[nmv_ctx] : NULL;
        read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc[nmv_ctx],
                mv_counts, allow_hp);
#else
Jingning Han's avatar
Jingning Han committed
860
861
        read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
                allow_hp);
862
#endif
Jingning Han's avatar
Jingning Han committed
863
        ret = ret && is_mv_valid(&mv[i].as_mv);
864
865
866
#if CONFIG_REF_MV
        pred_mv[i].as_int = ref_mv[i].as_int;
#endif
Jingning Han's avatar
Jingning Han committed
867
868
869
870
871
      }
      break;
    }
    case NEARESTMV: {
      mv[0].as_int = nearest_mv[0].as_int;
clang-format's avatar
clang-format committed
872
      if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
873
874
#if CONFIG_REF_MV
      pred_mv[0].as_int = nearest_mv[0].as_int;
clang-format's avatar
clang-format committed
875
      if (is_compound) pred_mv[1].as_int = nearest_mv[1].as_int;
876
#endif
Jingning Han's avatar
Jingning Han committed
877
878
879
880
      break;
    }
    case NEARMV: {
      mv[0].as_int = near_mv[0].as_int;
clang-format's avatar
clang-format committed
881
      if (is_compound) mv[1].as_int = near_mv[1].as_int;
882
883
#if CONFIG_REF_MV
      pred_mv[0].as_int = near_mv[0].as_int;
clang-format's avatar
clang-format committed
884
      if (is_compound) pred_mv[1].as_int = near_mv[1].as_int;
885
#endif
Jingning Han's avatar
Jingning Han committed
886
887
888
889
      break;
    }
    case ZEROMV: {
      mv[0].as_int = 0;
clang-format's avatar
clang-format committed
890
      if (is_compound) mv[1].as_int = 0;
891
892
#if CONFIG_REF_MV
      pred_mv[0].as_int = 0;
clang-format's avatar
clang-format committed
893
      if (is_compound) pred_mv[1].as_int = 0;
894
#endif
Jingning Han's avatar
Jingning Han committed
895
896
      break;
    }
clang-format's avatar
clang-format committed
897
    default: { return 0; }
Jingning Han's avatar
Jingning Han committed
898
899
900
901
  }
  return ret;
}

902
static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
903
                               int segment_id, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
904
905
906
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
    return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
  } else {
907
    const int ctx = av1_get_intra_inter_context(xd);
Michael Bebenita's avatar
Michael Bebenita committed
908
    const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
909
    FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
910
    if (counts) ++counts->intra_inter[ctx][is_inter];