decodemv.c 68.1 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

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

  assert(l != 0);

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

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

55
56
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
#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
91
92
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
93
  const PREDICTION_MODE y_mode =
94
95
96
#if CONFIG_DAALA_EC
      read_intra_mode_cdf(r, cm->fc->y_mode_cdf[size_group]);
#else
Jingning Han's avatar
Jingning Han committed
97
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
98
#endif
Jingning Han's avatar
Jingning Han committed
99
  FRAME_COUNTS *counts = xd->counts;
100
  if (counts) ++counts->y_mode[size_group][y_mode];
Jingning Han's avatar
Jingning Han committed
101
102
103
  return y_mode;
}

Yaowu Xu's avatar
Yaowu Xu committed
104
105
static PREDICTION_MODE read_intra_mode_uv(AV1_COMMON *cm, MACROBLOCKD *xd,
                                          aom_reader *r,
Jingning Han's avatar
Jingning Han committed
106
                                          PREDICTION_MODE y_mode) {
107
108
  const PREDICTION_MODE uv_mode =
      read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
Jingning Han's avatar
Jingning Han committed
109
  FRAME_COUNTS *counts = xd->counts;
110
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
Jingning Han's avatar
Jingning Han committed
111
112
113
  return uv_mode;
}

114
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
115
116
117
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
118
119
      r, av1_interintra_mode_tree, cm->fc->interintra_mode_prob[size_group],
      ACCT_STR);
120
  FRAME_COUNTS *counts = xd->counts;
121
  if (counts) ++counts->interintra_mode[size_group][ii_mode];
122
123
124
125
  return ii_mode;
}
#endif  // CONFIG_EXT_INTER

Yaowu Xu's avatar
Yaowu Xu committed
126
static PREDICTION_MODE read_inter_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
Yue Chen's avatar
Yue Chen committed
127
128
129
#if CONFIG_REF_MV && CONFIG_EXT_INTER
                                       MB_MODE_INFO *mbmi,
#endif
Yaowu Xu's avatar
Yaowu Xu committed
130
                                       aom_reader *r, int16_t ctx) {
131
132
#if CONFIG_REF_MV
  FRAME_COUNTS *counts = xd->counts;
133
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
Yaowu Xu's avatar
Yaowu Xu committed
134
  aom_prob mode_prob = cm->fc->newmv_prob[mode_ctx];
135

Michael Bebenita's avatar
Michael Bebenita committed
136
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
137
    if (counts) ++counts->newmv_mode[mode_ctx][0];
Yue Chen's avatar
Yue Chen committed
138
139
140
141

#if CONFIG_EXT_INTER
    if (has_second_ref(mbmi)) {
#endif  // CONFIG_EXT_INTER
142
      return NEWMV;
Yue Chen's avatar
Yue Chen committed
143
144
145
#if CONFIG_EXT_INTER
    } else {
      mode_prob = cm->fc->new2mv_prob;
Michael Bebenita's avatar
Michael Bebenita committed
146
      if (aom_read(r, mode_prob, ACCT_STR) == 0) {
147
        if (counts) ++counts->new2mv_mode[0];
Yue Chen's avatar
Yue Chen committed
148
149
        return NEWMV;
      } else {
150
        if (counts) ++counts->new2mv_mode[1];
Yue Chen's avatar
Yue Chen committed
151
152
153
154
        return NEWFROMNEARMV;
      }
    }
#endif  // CONFIG_EXT_INTER
155
  }
156
  if (counts) ++counts->newmv_mode[mode_ctx][1];
157

158
  if (ctx & (1 << ALL_ZERO_FLAG_OFFSET)) return ZEROMV;
159

160
  mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
161
162

  mode_prob = cm->fc->zeromv_prob[mode_ctx];
Michael Bebenita's avatar
Michael Bebenita committed
163
  if (aom_read(r, mode_prob, ACCT_STR) == 0) {
164
    if (counts) ++counts->zeromv_mode[mode_ctx][0];
165
166
    return ZEROMV;
  }
167
  if (counts) ++counts->zeromv_mode[mode_ctx][1];
168

169
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
170

171
172
173
  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;
174

175
  mode_prob = cm->fc->refmv_prob[mode_ctx];
176

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

180
181
    return NEARESTMV;
  } else {
182
    if (counts) ++counts->refmv_mode[mode_ctx][1];
183
184
185
186
187
188
    return NEARMV;
  }

  // Invalid prediction mode.
  assert(0);
#else
Michael Bebenita's avatar
Michael Bebenita committed
189
190
  const int mode = aom_read_tree(r, av1_inter_mode_tree,
                                 cm->fc->inter_mode_probs[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
191
  FRAME_COUNTS *counts = xd->counts;
192
  if (counts) ++counts->inter_mode[ctx][mode];
Jingning Han's avatar
Jingning Han committed
193
194

  return NEARESTMV + mode;
195
#endif
Jingning Han's avatar
Jingning Han committed
196
197
}

198
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
199
200
201
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);
202
203
  mbmi->ref_mv_idx = 0;

204
205
206
207
  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
208
209
        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
210
        if (!aom_read(r, drl_prob, ACCT_STR)) {
211
          mbmi->ref_mv_idx = idx;
212
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
213
214
215
          return;
        }
        mbmi->ref_mv_idx = idx + 1;
216
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
217
218
219
220
      }
    }
  }

221
222
223
224
225
226
227
  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
228
229
        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
230
        if (!aom_read(r, drl_prob, ACCT_STR)) {
231
          mbmi->ref_mv_idx = idx - 1;
232
          if (xd->counts) ++xd->counts->drl_mode[drl_ctx][0];
233
234
          return;
        }
235
        mbmi->ref_mv_idx = idx;
236
        if (xd->counts) ++xd->counts->drl_mode[drl_ctx][1];
237
238
239
240
241
242
      }
    }
  }
}
#endif

243
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
244
245
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
246
247
248
  const int mode =
      aom_read_tree(r, av1_inter_compound_mode_tree,
                    cm->fc->inter_compound_mode_probs[ctx], ACCT_STR);
249
250
  FRAME_COUNTS *counts = xd->counts;

251
  if (counts) ++counts->inter_compound_mode[ctx][mode];
252
253
254
255
256
257

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

Yaowu Xu's avatar
Yaowu Xu committed
258
static int read_segment_id(aom_reader *r,
259
                           const struct segmentation_probs *segp) {
260
#if CONFIG_DAALA_EC
Michael Bebenita's avatar
Michael Bebenita committed
261
  return aom_read_symbol(r, segp->tree_cdf, MAX_SEGMENTS, ACCT_STR);
262
#else
Michael Bebenita's avatar
Michael Bebenita committed
263
  return aom_read_tree(r, av1_segment_tree, segp->tree_probs, ACCT_STR);
264
#endif
Jingning Han's avatar
Jingning Han committed
265
266
}

267
#if CONFIG_VAR_TX
Yaowu Xu's avatar
Yaowu Xu committed
268
static void read_tx_size_vartx(AV1_COMMON *cm, MACROBLOCKD *xd,
269
                               MB_MODE_INFO *mbmi, FRAME_COUNTS *counts,
270
                               TX_SIZE tx_size, int blk_row, int blk_col,
Yaowu Xu's avatar
Yaowu Xu committed
271
                               aom_reader *r) {
272
  int is_split = 0;
273
274
  const int tx_row = blk_row >> 1;
  const int tx_col = blk_col >> 1;
275
276
  int max_blocks_high = num_4x4_blocks_high_lookup[mbmi->sb_type];
  int max_blocks_wide = num_4x4_blocks_wide_lookup[mbmi->sb_type];
277
  int ctx = txfm_partition_context(xd->above_txfm_context + tx_col,
278
                                   xd->left_txfm_context + tx_row, tx_size);
clang-format's avatar
clang-format committed
279
  TX_SIZE(*const inter_tx_size)
280
281
  [MAX_MIB_SIZE] =
      (TX_SIZE(*)[MAX_MIB_SIZE]) & mbmi->inter_tx_size[tx_row][tx_col];
282

283
284
  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;
285

286
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
287

Michael Bebenita's avatar
Michael Bebenita committed
288
  is_split = aom_read(r, cm->fc->txfm_partition_prob[ctx], ACCT_STR);
289
290
291

  if (is_split) {
    BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
292
    int bsl = b_width_log2_lookup[bsize];
293
    int i;
294

295
    if (counts) ++counts->txfm_partition[ctx][1];
296

297
    if (tx_size == TX_8X8) {
298
299
300
301
      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);
302
303
304
305
306
307
      return;
    }

    assert(bsl > 0);
    --bsl;
    for (i = 0; i < 4; ++i) {
308
309
      int offsetr = blk_row + ((i >> 1) << bsl);
      int offsetc = blk_col + ((i & 0x01) << bsl);
310
311
      read_tx_size_vartx(cm, xd, mbmi, counts, tx_size - 1, offsetr, offsetc,
                         r);
312
313
    }
  } else {
314
    int idx, idy;
315
    inter_tx_size[0][0] = tx_size;
316
317
    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)
318
319
        inter_tx_size[idy][idx] = tx_size;
    mbmi->tx_size = tx_size;
320
    if (counts) ++counts->txfm_partition[ctx][0];
321
322
    txfm_partition_update(xd->above_txfm_context + tx_col,
                          xd->left_txfm_context + tx_row, tx_size);
323
324
325
326
  }
}
#endif

Yaowu Xu's avatar
Yaowu Xu committed
327
328
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
329
330
  FRAME_COUNTS *counts = xd->counts;
  const int ctx = get_tx_size_context(xd);
Michael Bebenita's avatar
Michael Bebenita committed
331
332
333
  int tx_size =
      aom_read_tree(r, av1_tx_size_tree[tx_size_cat],
                    cm->fc->tx_size_probs[tx_size_cat][ctx], ACCT_STR);
334
  if (counts) ++counts->tx_size[tx_size_cat][ctx][tx_size];
Jingning Han's avatar
Jingning Han committed
335
336
337
  return (TX_SIZE)tx_size;
}

Yaowu Xu's avatar
Yaowu Xu committed
338
339
static TX_SIZE read_tx_size_intra(AV1_COMMON *cm, MACROBLOCKD *xd,
                                  aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
340
341
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
342
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
343
344
  if (bsize >= BLOCK_8X8) {
    if (tx_mode == TX_MODE_SELECT) {
345
346
347
348
      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;
349
    } else {
350
      return tx_size_from_tx_mode(bsize, cm->tx_mode, 0);
351
352
353
354
355
356
    }
  } else {
    return TX_4X4;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
357
358
static TX_SIZE read_tx_size_inter(AV1_COMMON *cm, MACROBLOCKD *xd,
                                  int allow_select, aom_reader *r) {
359
360
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
361
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
362
363
  if (bsize >= BLOCK_8X8) {
    if (allow_select && tx_mode == TX_MODE_SELECT) {
364
365
      const TX_SIZE coded_tx_size =
          read_selected_tx_size(cm, xd, inter_tx_size_cat_lookup[bsize], r);
366
#if CONFIG_EXT_TX && CONFIG_RECT_TX
367
368
369
370
      if (coded_tx_size > max_txsize_lookup[bsize]) {
        assert(coded_tx_size == max_txsize_lookup[bsize] + 1);
        return max_txsize_rect_lookup[bsize];
      }
371
372
373
#else
      assert(coded_tx_size <= max_txsize_lookup[bsize]);
#endif  // CONFIG_EXT_TX && CONFIG_RECT_TX
374
      return coded_tx_size;
375
    } else {
376
      return tx_size_from_tx_mode(bsize, cm->tx_mode, 1);
377
378
    }
  } else {
379
#if CONFIG_EXT_TX && CONFIG_RECT_TX
Debargha Mukherjee's avatar
Debargha Mukherjee committed
380
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
381
382
383
    return max_txsize_rect_lookup[bsize];
#else
    return TX_4X4;
384
#endif
385
  }
Jingning Han's avatar
Jingning Han committed
386
387
}

Yaowu Xu's avatar
Yaowu Xu committed
388
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Jingning Han's avatar
Jingning Han committed
389
390
391
392
393
                              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++)
394
      segment_id =
Yaowu Xu's avatar
Yaowu Xu committed
395
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
396
397
398
399
400

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

Yaowu Xu's avatar
Yaowu Xu committed
401
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
402
                           int segment_id) {
Jingning Han's avatar
Jingning Han committed
403
404
405
406
407
408
409
410
411
  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
412
static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
413
                                 int mi_offset, int x_mis, int y_mis,
Yaowu Xu's avatar
Yaowu Xu committed
414
                                 aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
415
  struct segmentation *const seg = &cm->seg;
416
417
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
418
419
  int segment_id;

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

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

424
  segment_id = read_segment_id(r, segp);
425
  if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
426
427
428
429
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
430
static void copy_segment_id(const AV1_COMMON *cm,
431
432
433
                            const uint8_t *last_segment_ids,
                            uint8_t *current_segment_ids, int mi_offset,
                            int x_mis, int y_mis) {
434
435
436
437
  int x, y;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
438
439
440
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
                           : 0;
441
442
}

Yaowu Xu's avatar
Yaowu Xu committed
443
444
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
445
  struct segmentation *const seg = &cm->seg;
446
447
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
Jingning Han's avatar
Jingning Han committed
448
449
450
  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
451
452
  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
453
454

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

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

460
461
462
463
  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
464
465
466
467
468
469
470
471

  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
472
473
    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
474
    mbmi->seg_id_predicted = aom_read(r, pred_prob, ACCT_STR);
475
    if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
476
477
478
479
    if (mbmi->seg_id_predicted) {
      segment_id = predicted_segment_id;
    } else {
      segment_id = read_segment_id(r, segp);
480
      if (counts) ++counts->seg.tree_mispred[segment_id];
481
    }
Jingning Han's avatar
Jingning Han committed
482
  } else {
483
    segment_id = read_segment_id(r, segp);
484
    if (counts) ++counts->seg.tree_total[segment_id];
Jingning Han's avatar
Jingning Han committed
485
486
487
488
489
  }
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

Yaowu Xu's avatar
Yaowu Xu committed
490
491
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
                     aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
492
493
494
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
495
    const int ctx = av1_get_skip_context(xd);
Michael Bebenita's avatar
Michael Bebenita committed
496
    const int skip = aom_read(r, cm->fc->skip_probs[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
497
    FRAME_COUNTS *counts = xd->counts;
498
    if (counts) ++counts->skip[ctx][skip];
Jingning Han's avatar
Jingning Han committed
499
500
501
502
    return skip;
  }
}

503
#if CONFIG_PALETTE
Yaowu Xu's avatar
Yaowu Xu committed
504
505
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                   aom_reader *r) {
hui su's avatar
hui su committed
506
507
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
508
  const MODE_INFO *const above_mi = xd->above_mi;
509
  const MODE_INFO *const left_mi = xd->left_mi;
hui su's avatar
hui su committed
510
  const BLOCK_SIZE bsize = mbmi->sb_type;
511
512
513
514
515
516
517
518
  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
519
520
521
    if (aom_read(
            r, av1_default_palette_y_mode_prob[bsize - BLOCK_8X8][palette_ctx],
            ACCT_STR)) {
522
      pmi->palette_size[0] =
Yaowu Xu's avatar
Yaowu Xu committed
523
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
524
525
                        av1_default_palette_y_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
526
          2;
527
528
      n = pmi->palette_size[0];
      for (i = 0; i < n; ++i)
Michael Bebenita's avatar
Michael Bebenita committed
529
        pmi->palette_colors[i] = aom_read_literal(r, cm->bit_depth, ACCT_STR);
hui su's avatar
hui su committed
530

531
532
533
534
      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
535

536
  if (mbmi->uv_mode == DC_PRED) {
Michael Bebenita's avatar
Michael Bebenita committed
537
538
    if (aom_read(r, av1_default_palette_uv_mode_prob[pmi->palette_size[0] > 0],
                 ACCT_STR)) {
539
      pmi->palette_size[1] =
Yaowu Xu's avatar
Yaowu Xu committed
540
          aom_read_tree(r, av1_palette_size_tree,
Michael Bebenita's avatar
Michael Bebenita committed
541
542
                        av1_default_palette_uv_size_prob[bsize - BLOCK_8X8],
                        ACCT_STR) +
543
          2;
544
545
546
      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
547
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
548
        pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
Michael Bebenita's avatar
Michael Bebenita committed
549
            aom_read_literal(r, cm->bit_depth, ACCT_STR);
550
551
552
553
      }
      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
554
555
  }
}
556
#endif  // CONFIG_PALETTE
hui su's avatar
hui su committed
557

558
559
560
#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
561
562
563
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  FRAME_COUNTS *counts = xd->counts;
564
565
  FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
      &mbmi->filter_intra_mode_info;
hui su's avatar
hui su committed
566

567
568
569
570
571
  if (mbmi->mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[0] == 0
#endif  // CONFIG_PALETTE
      ) {
572
573
574
575
    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
576
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
577
    }
578
579
580
581
    if (counts) {
      ++counts->filter_intra[0]
                            [filter_intra_mode_info->use_filter_intra_mode[0]];
    }
hui su's avatar
hui su committed
582
  }
583
584
585
586
587
  if (mbmi->uv_mode == DC_PRED
#if CONFIG_PALETTE
      && mbmi->palette_mode_info.palette_size[1] == 0
#endif  // CONFIG_PALETTE
      ) {
588
589
590
591
    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
592
          read_uniform(r, FILTER_INTRA_MODES);
hui su's avatar
hui su committed
593
    }
594
595
596
597
    if (counts) {
      ++counts->filter_intra[1]
                            [filter_intra_mode_info->use_filter_intra_mode[1]];
    }
hui su's avatar
hui su committed
598
599
  }
}
600
#endif  // CONFIG_FILTER_INTRA
601

602
#if CONFIG_EXT_INTRA
Yaowu Xu's avatar
Yaowu Xu committed
603
604
static void read_intra_angle_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                                  aom_reader *r) {
605
606
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
Yaowu Xu's avatar
Yaowu Xu committed
607
  const int ctx = av1_get_pred_context_intra_interp(xd);
608
609
  int p_angle;

610
  if (bsize < BLOCK_8X8) return;
611
612
613
614
615

  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
616
    if (av1_is_intra_filter_switchable(p_angle)) {
617
      FRAME_COUNTS *counts = xd->counts;
Michael Bebenita's avatar
Michael Bebenita committed
618
619
      mbmi->intra_filter = aom_read_tree(
          r, av1_intra_filter_tree, cm->fc->intra_filter_probs[ctx], ACCT_STR);
620
      if (counts) ++counts->intra_filter[ctx][mbmi->intra_filter];
621
622
623
624
625
626
627
628
629
630
    } 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
631
632
#endif  // CONFIG_EXT_INTRA

Yaowu Xu's avatar
Yaowu Xu committed
633
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
634
                                       MACROBLOCKD *const xd, int mi_row,
Yaowu Xu's avatar
Yaowu Xu committed
635
                                       int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
636
637
638
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const MODE_INFO *above_mi = xd->above_mi;
639
  const MODE_INFO *left_mi = xd->left_mi;
Jingning Han's avatar
Jingning Han committed
640
641
642
643
644
645
646
  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
647
648
  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
649

650
  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
Jingning Han's avatar
Jingning Han committed
651
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
652
653
654

#if CONFIG_DELTA_Q
  if (cm->delta_q_present_flag) {
655
656
657
658
    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;
659
660
661
  }
#endif

662
  mbmi->tx_size = read_tx_size_intra(cm, xd, r);
Jingning Han's avatar
Jingning Han committed
663
664
665
666
667
668
669
  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 =
670
671
672
673
#if CONFIG_DAALA_EC
            read_intra_mode_cdf(r,
                                get_y_mode_cdf(cm, mi, above_mi, left_mi, i));
#else
674
            read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, i));
675
#endif
Jingning Han's avatar
Jingning Han committed
676
677
678
679
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
      mi->bmi[0].as_mode = mi->bmi[2].as_mode =
680
681
682
#if CONFIG_DAALA_EC
          read_intra_mode_cdf(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
#else
683
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
684
#endif
Jingning Han's avatar
Jingning Han committed
685
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
686
687
688
#if CONFIG_DAALA_EC
          read_intra_mode_cdf(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 1));
#else
689
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 1));
690
#endif
Jingning Han's avatar
Jingning Han committed
691
692
693
      break;
    case BLOCK_8X4:
      mi->bmi[0].as_mode = mi->bmi[1].as_mode =
694
695
696
#if CONFIG_DAALA_EC
          read_intra_mode_cdf(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
#else
697
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
698
#endif
Jingning Han's avatar
Jingning Han committed
699
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
700
701
702
#if CONFIG_DAALA_EC
          read_intra_mode_cdf(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 2));
#else
703
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 2));
704
#endif
Jingning Han's avatar
Jingning Han committed
705
706
      break;
    default:
707
      mbmi->mode =
708
709
710
#if CONFIG_DAALA_EC
          read_intra_mode_cdf(r, get_y_mode_cdf(cm, mi, above_mi, left_mi, 0));
#else
711
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
712
#endif
Jingning Han's avatar
Jingning Han committed
713
714
  }

715
  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
hui su's avatar
hui su committed
716
#if CONFIG_EXT_INTRA
717
718
  read_intra_angle_info(cm, xd, r);
#endif  // CONFIG_EXT_INTRA
719
#if CONFIG_PALETTE
hui su's avatar
hui su committed
720
721
  mbmi->palette_mode_info.palette_size[0] = 0;
  mbmi->palette_mode_info.palette_size[1] = 0;
722
  if (bsize >= BLOCK_8X8 && cm->allow_screen_content_tools)
hui su's avatar
hui su committed
723
    read_palette_mode_info(cm, xd, r);
724
#endif  // CONFIG_PALETTE
725
726
727
728
729
#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
730

731
  if (!FIXED_TX_TYPE) {
732
#if CONFIG_EXT_TX
733
734
    if (get_ext_tx_types(mbmi->tx_size, mbmi->sb_type, 0) > 1 &&
        cm->base_qindex > 0 && !mbmi->skip &&
735
736
        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) &&
        ALLOW_INTRA_EXT_TX) {
Jingning Han's avatar
Jingning Han committed
737
      FRAME_COUNTS *counts = xd->counts;
738
739
      int eset = get_ext_tx_set(mbmi->tx_size, mbmi->sb_type, 0);
      if (eset > 0) {
Yaowu Xu's avatar
Yaowu Xu committed
740
741
        mbmi->tx_type = aom_read_tree(
            r, av1_ext_tx_intra_tree[eset],
Michael Bebenita's avatar
Michael Bebenita committed
742
743
            cm->fc->intra_ext_tx_prob[eset][mbmi->tx_size][mbmi->mode],
            ACCT_STR);
744
        if (counts)
clang-format's avatar
clang-format committed
745
746
          ++counts->intra_ext_tx[eset][mbmi->tx_size][mbmi->mode]
                                [mbmi->tx_type];
747
      }
748
    } else {
hui su's avatar
hui su committed
749
      mbmi->tx_type = DCT_DCT;
750
    }
751
#else
752
    if (mbmi->tx_size < TX_32X32 && cm->base_qindex > 0 && !mbmi->skip &&
753
754
755
        !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];
756
757
758
759
760
#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
761
762
763
      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);
764
#endif
765
766
767
768
769
      if (counts)
        ++counts->intra_ext_tx[mbmi->tx_size][tx_type_nom][mbmi->tx_type];
    } else {
      mbmi->tx_type = DCT_DCT;
    }
770
#endif  // CONFIG_EXT_TX
771
  }
Jingning Han's avatar
Jingning Han committed
772
773
}

Yaowu Xu's avatar
Yaowu Xu committed
774
static int read_mv_component(aom_reader *r, const nmv_component *mvcomp,
775
                             int usehp) {
Jingning Han's avatar
Jingning Han committed
776
  int mag, d, fr, hp;
Michael Bebenita's avatar
Michael Bebenita committed
777
778
779
  const int sign = aom_read(r, mvcomp->sign, ACCT_STR);
  const int mv_class =
      aom_read_tree(r, av1_mv_class_tree, mvcomp->classes, ACCT_STR);
Jingning Han's avatar
Jingning Han committed
780
781
782
783
  const int class0 = mv_class == MV_CLASS_0;

  // Integer part
  if (class0) {
784
    d = aom_read(r, mvcomp->class0[0], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
785
786
787
788
789
790
    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
791
    for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i], ACCT_STR) << i;
Jingning Han's avatar
Jingning Han committed
792
793
794
795
    mag = CLASS0_SIZE << (mv_class + 2);
  }

  // Fractional part
Yaowu Xu's avatar
Yaowu Xu committed
796
  fr = aom_read_tree(r, av1_mv_fp_tree,
Michael Bebenita's avatar
Michael Bebenita committed
797
                     class0 ? mvcomp->class0_fp[d] : mvcomp->fp, ACCT_STR);
Jingning Han's avatar
Jingning Han committed
798
799

  // High precision part (if hp is not used, the default value of the hp is 1)
Michael Bebenita's avatar
Michael Bebenita committed
800
801
  hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp, ACCT_STR)
             : 1;
Jingning Han's avatar
Jingning Han committed
802
803
804
805
806
807

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

Yaowu Xu's avatar
Yaowu Xu committed
808
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
809
810
                           const nmv_context *ctx, nmv_context_counts *counts,
                           int allow_hp) {
811
  MV_JOINT_TYPE joint_type;
Yaowu Xu's avatar
Yaowu Xu committed
812
  const int use_hp = allow_hp && av1_use_mv_hp(ref);
813
  MV diff = { 0, 0 };
Michael Bebenita's avatar
Michael Bebenita committed
814
815
  joint_type =
      (MV_JOINT_TYPE)aom_read_tree(r, av1_mv_joint_tree, ctx->joints, ACCT_STR);
816

Jingning Han's avatar
Jingning Han committed
817
818
819
820
821
822
  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
823
  av1_inc_mv(&diff, counts, use_hp);
Jingning Han's avatar
Jingning Han committed
824
825
826
827
828

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

Yaowu Xu's avatar
Yaowu Xu committed
829
static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Jingning Han's avatar
Jingning Han committed
830
                                                const MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
831
                                                aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
832
  if (cm->reference_mode == REFERENCE_MODE_SELECT) {
Yaowu Xu's avatar
Yaowu Xu committed
833
    const int ctx = av1_get_reference_mode_context(cm, xd);
Jingning Han's avatar
Jingning Han committed
834
    const REFERENCE_MODE mode =
Michael Bebenita's avatar
Michael Bebenita committed
835
        (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx], ACCT_STR);
Jingning Han's avatar
Jingning Han committed
836
    FRAME_COUNTS *counts = xd->counts;
837
    if (counts) ++counts->comp_inter[ctx][mode];
Jingning Han's avatar
Jingning Han committed
838
839
840
841
842
843
844
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
  } else {
    return cm->reference_mode;
  }
}

// Read the referncence frame
Yaowu Xu's avatar
Yaowu Xu committed
845
846
static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
                            aom_reader *r, int segment_id,
847
                            MV_REFERENCE_FRAME ref_frame[2]) {
Jingning Han's avatar
Jingning Han committed
848
849
850
851
852
853
854
855
856
857
858
  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) {
859
#if CONFIG_EXT_REFS
860
861
      const int idx = cm->ref_frame_sign_bias[cm->comp_bwd_ref[0]];
#else
Jingning Han's avatar
Jingning Han committed
862
      const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
863
#endif  // CONFIG_EXT_REFS
Yaowu Xu's avatar
Yaowu Xu committed
864
      const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
Michael Bebenita's avatar
Michael Bebenita committed
865
      const int bit = aom_read(r, fc->comp_ref_prob[ctx][0], ACCT_STR);
866

867
      if (counts) ++counts->comp_ref[ctx][0][bit];
868

869
#if CONFIG_EXT_REFS
870
      // Decode forward references.
871
      if (!bit) {
Yaowu Xu's avatar
Yaowu Xu committed
872
        const int ctx1 = av1_get_pred_context_comp_ref_p1(cm, xd);
Michael Bebenita's avatar
Michael Bebenita committed
873
        const int bit1 = aom_read(r, fc->comp_ref_prob[ctx1][1], ACCT_STR);
874
        if (counts) ++counts->comp_ref[ctx1][1][bit1];
875
        ref_frame[!idx] = cm->comp_fwd_ref[bit1 ? 0 : 1];
876
      } else {
Yaowu Xu's avatar
Yaowu Xu committed
877
        const int ctx2 = av1_get_pred_context_comp_ref_p2(cm, xd);
Michael Bebenita's avatar
Michael Bebenita committed
878
        const int bit2 = aom_read(r, fc->comp_ref_prob[ctx2][2], ACCT_STR);
879
        if (counts) ++counts->comp_ref[ctx2][2][bit2];
880
        ref_frame[!idx] = cm->comp_fwd_ref[bit2 ? 3 : 2];