vp9_decodemv.c 21.8 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9 10
 */

11 12
#include <assert.h>

13 14
#include "vp9/common/vp9_common.h"
#include "vp9/common/vp9_entropy.h"
15
#include "vp9/common/vp9_entropymode.h"
16 17
#include "vp9/common/vp9_entropymv.h"
#include "vp9/common/vp9_mvref_common.h"
18
#include "vp9/common/vp9_pred_common.h"
19 20 21
#include "vp9/common/vp9_reconinter.h"
#include "vp9/common/vp9_seg_common.h"

22
#include "vp9/decoder/vp9_decodemv.h"
Yaowu Xu's avatar
Yaowu Xu committed
23
#include "vp9/decoder/vp9_decodeframe.h"
24
#include "vp9/decoder/vp9_reader.h"
25

26 27
static PREDICTION_MODE read_intra_mode(vp9_reader *r, const vp9_prob *p) {
  return (PREDICTION_MODE)vp9_read_tree(r, vp9_intra_mode_tree, p);
28 29
}

Scott LaVarnway's avatar
Scott LaVarnway committed
30
static PREDICTION_MODE read_intra_mode_y(VP9_COMMON *cm, MACROBLOCKD *xd,
31
                                         vp9_reader *r, int size_group) {
32
  const PREDICTION_MODE y_mode =
33
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
Scott LaVarnway's avatar
Scott LaVarnway committed
34 35
  FRAME_COUNTS *counts = xd->counts;
  if (counts)
36
    ++counts->y_mode[size_group][y_mode];
37 38 39
  return y_mode;
}

Scott LaVarnway's avatar
Scott LaVarnway committed
40
static PREDICTION_MODE read_intra_mode_uv(VP9_COMMON *cm, MACROBLOCKD *xd,
41
                                          vp9_reader *r,
42 43
                                          PREDICTION_MODE y_mode) {
  const PREDICTION_MODE uv_mode = read_intra_mode(r,
44
                                         cm->fc->uv_mode_prob[y_mode]);
Scott LaVarnway's avatar
Scott LaVarnway committed
45 46
  FRAME_COUNTS *counts = xd->counts;
  if (counts)
47
    ++counts->uv_mode[y_mode][uv_mode];
48 49 50
  return uv_mode;
}

Scott LaVarnway's avatar
Scott LaVarnway committed
51
static PREDICTION_MODE read_inter_mode(VP9_COMMON *cm, MACROBLOCKD *xd,
52
                                       vp9_reader *r, int ctx) {
53
  const int mode = vp9_read_tree(r, vp9_inter_mode_tree,
54
                                 cm->fc->inter_mode_probs[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
55 56
  FRAME_COUNTS *counts = xd->counts;
  if (counts)
57
    ++counts->inter_mode[ctx][mode];
58 59

  return NEARESTMV + mode;
60 61
}

62
static int read_segment_id(vp9_reader *r, const struct segmentation *seg) {
63
  return vp9_read_tree(r, vp9_segment_tree, seg->tree_probs);
Scott LaVarnway's avatar
Scott LaVarnway committed
64
}
65

66
static TX_SIZE read_selected_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
67
                                     TX_SIZE max_tx_size, vp9_reader *r) {
Scott LaVarnway's avatar
Scott LaVarnway committed
68
  FRAME_COUNTS *counts = xd->counts;
69
  const int ctx = get_tx_size_context(xd);
70
  const vp9_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
71
  int tx_size = vp9_read(r, tx_probs[0]);
72
  if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
73
    tx_size += vp9_read(r, tx_probs[1]);
74
    if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
75
      tx_size += vp9_read(r, tx_probs[2]);
76 77
  }

Scott LaVarnway's avatar
Scott LaVarnway committed
78
  if (counts)
79
    ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
80
  return (TX_SIZE)tx_size;
81 82
}

83 84 85
static TX_SIZE read_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
                            int allow_select, vp9_reader *r) {
  TX_MODE tx_mode = cm->tx_mode;
86
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
87 88
  const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
  if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
Scott LaVarnway's avatar
Scott LaVarnway committed
89
    return read_selected_tx_size(cm, xd, max_tx_size, r);
90 91
  else
    return MIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
92 93
}

94 95 96
static void set_segment_id(VP9_COMMON *cm, BLOCK_SIZE bsize,
                           int mi_row, int mi_col, int segment_id) {
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
97 98
  const int bw = num_8x8_blocks_wide_lookup[bsize];
  const int bh = num_8x8_blocks_high_lookup[bsize];
99 100 101 102 103 104 105 106
  const int xmis = MIN(cm->mi_cols - mi_col, bw);
  const int ymis = MIN(cm->mi_rows - mi_row, bh);
  int x, y;

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

  for (y = 0; y < ymis; y++)
    for (x = 0; x < xmis; x++)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
}

static void copy_segment_id(const VP9_COMMON *cm,
                           const uint8_t *last_segment_ids,
                           uint8_t *current_segment_ids,
                           BLOCK_SIZE bsize, int mi_row, int mi_col) {
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
  const int bw = num_8x8_blocks_wide_lookup[bsize];
  const int bh = num_8x8_blocks_high_lookup[bsize];
  const int xmis = MIN(cm->mi_cols - mi_col, bw);
  const int ymis = MIN(cm->mi_rows - mi_row, bh);
  int x, y;

  for (y = 0; y < ymis; y++)
    for (x = 0; x < xmis; x++)
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =  last_segment_ids ?
          last_segment_ids[mi_offset + y * cm->mi_cols + x] : 0;
125 126
}

127
static int read_intra_segment_id(VP9_COMMON *const cm, BLOCK_SIZE bsize,
128
                                 int mi_row, int mi_col,
129
                                 vp9_reader *r) {
130
  struct segmentation *const seg = &cm->seg;
131
  int segment_id;
132

133 134 135
  if (!seg->enabled)
    return 0;  // Default for disabled segmentation

136 137 138
  if (!seg->update_map) {
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
                    bsize, mi_row, mi_col);
139
    return 0;
140
  }
141

142
  segment_id = read_segment_id(r, seg);
143
  set_segment_id(cm, bsize, mi_row, mi_col, segment_id);
144
  return segment_id;
145 146
}

147 148
static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
                                 int mi_row, int mi_col, vp9_reader *r) {
149
  struct segmentation *const seg = &cm->seg;
150
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
151 152
  const BLOCK_SIZE bsize = mbmi->sb_type;
  int predicted_segment_id, segment_id;
153 154 155 156

  if (!seg->enabled)
    return 0;  // Default for disabled segmentation

157
  predicted_segment_id = cm->last_frame_seg_map ?
158
      get_segment_id(cm, cm->last_frame_seg_map, bsize, mi_row, mi_col) : 0;
159 160 161 162

  if (!seg->update_map) {
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
                    bsize, mi_row, mi_col);
163
    return predicted_segment_id;
164
  }
165 166

  if (seg->temporal_update) {
167
    const vp9_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
168 169 170
    mbmi->seg_id_predicted = vp9_read(r, pred_prob);
    segment_id = mbmi->seg_id_predicted ? predicted_segment_id
                                        : read_segment_id(r, seg);
171
  } else {
172
    segment_id = read_segment_id(r, seg);
173
  }
174 175
  set_segment_id(cm, bsize, mi_row, mi_col, segment_id);
  return segment_id;
176 177
}

178 179
static int read_skip(VP9_COMMON *cm, const MACROBLOCKD *xd,
                     int segment_id, vp9_reader *r) {
180
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
181 182
    return 1;
  } else {
183
    const int ctx = vp9_get_skip_context(xd);
184
    const int skip = vp9_read(r, cm->fc->skip_probs[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
185 186
    FRAME_COUNTS *counts = xd->counts;
    if (counts)
187
      ++counts->skip[ctx][skip];
188
    return skip;
Deb Mukherjee's avatar
Deb Mukherjee committed
189
  }
190
}
John Koleszar's avatar
John Koleszar committed
191

192 193
static void read_intra_frame_mode_info(VP9_COMMON *const cm,
                                       MACROBLOCKD *const xd,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
194
                                       int mi_row, int mi_col, vp9_reader *r) {
195
  MODE_INFO *const mi = xd->mi[0];
196
  MB_MODE_INFO *const mbmi = &mi->mbmi;
197 198
  const MODE_INFO *above_mi = xd->above_mi;
  const MODE_INFO *left_mi  = xd->left_mi;
199
  const BLOCK_SIZE bsize = mbmi->sb_type;
200
  int i;
201

202
  mbmi->segment_id = read_intra_segment_id(cm, bsize, mi_row, mi_col, r);
Scott LaVarnway's avatar
Scott LaVarnway committed
203 204
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
  mbmi->tx_size = read_tx_size(cm, xd, 1, r);
205
  mbmi->ref_frame[0] = INTRA_FRAME;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
206
  mbmi->ref_frame[1] = NONE;
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
        mi->bmi[i].as_mode =
            read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, i));
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
      mi->bmi[0].as_mode = mi->bmi[2].as_mode =
          read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
          read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 1));
      break;
    case BLOCK_8X4:
      mi->bmi[0].as_mode = mi->bmi[1].as_mode =
          read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
          read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 2));
      break;
    default:
      mbmi->mode = read_intra_mode(r,
                                   get_y_mode_probs(mi, above_mi, left_mi, 0));
John Koleszar's avatar
John Koleszar committed
230
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
231

232
  mbmi->uv_mode = read_intra_mode(r, vp9_kf_uv_mode_prob[mbmi->mode]);
Scott LaVarnway's avatar
Scott LaVarnway committed
233
}
John Koleszar's avatar
John Koleszar committed
234

235 236 237
static int read_mv_component(vp9_reader *r,
                             const nmv_component *mvcomp, int usehp) {
  int mag, d, fr, hp;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
238
  const int sign = vp9_read(r, mvcomp->sign);
239
  const int mv_class = vp9_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
240
  const int class0 = mv_class == MV_CLASS_0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
241

242
  // Integer part
243
  if (class0) {
244
    d = vp9_read_tree(r, vp9_mv_class0_tree, mvcomp->class0);
245
    mag = 0;
246
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
247
    int i;
248
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
Dmitry Kovalev's avatar
Dmitry Kovalev committed
249

250
    d = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
251 252
    for (i = 0; i < n; ++i)
      d |= vp9_read(r, mvcomp->bits[i]) << i;
253
    mag = CLASS0_SIZE << (mv_class + 2);
254 255
  }

256
  // Fractional part
257 258
  fr = vp9_read_tree(r, vp9_mv_fp_tree, class0 ? mvcomp->class0_fp[d]
                                               : mvcomp->fp);
259

260
  // High precision part (if hp is not used, the default value of the hp is 1)
261
  hp = usehp ? vp9_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp)
262
             : 1;
263

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

269 270
static INLINE void read_mv(vp9_reader *r, MV *mv, const MV *ref,
                           const nmv_context *ctx,
271
                           nmv_context_counts *counts, int allow_hp) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
272 273
  const MV_JOINT_TYPE joint_type =
      (MV_JOINT_TYPE)vp9_read_tree(r, vp9_mv_joint_tree, ctx->joints);
274
  const int use_hp = allow_hp && vp9_use_mv_hp(ref);
275 276
  MV diff = {0, 0};

Dmitry Kovalev's avatar
Dmitry Kovalev committed
277
  if (mv_joint_vertical(joint_type))
278
    diff.row = read_mv_component(r, &ctx->comps[0], use_hp);
279

Dmitry Kovalev's avatar
Dmitry Kovalev committed
280
  if (mv_joint_horizontal(joint_type))
281
    diff.col = read_mv_component(r, &ctx->comps[1], use_hp);
282

283
  vp9_inc_mv(&diff, counts);
284 285 286 287 288

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

289 290 291 292 293
static REFERENCE_MODE read_block_reference_mode(VP9_COMMON *cm,
                                                const MACROBLOCKD *xd,
                                                vp9_reader *r) {
  if (cm->reference_mode == REFERENCE_MODE_SELECT) {
    const int ctx = vp9_get_reference_mode_context(cm, xd);
294
    const REFERENCE_MODE mode =
295
        (REFERENCE_MODE)vp9_read(r, cm->fc->comp_inter_prob[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
296 297
    FRAME_COUNTS *counts = xd->counts;
    if (counts)
298
      ++counts->comp_inter[ctx][mode];
299 300 301 302
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
  } else {
    return cm->reference_mode;
  }
303 304
}

305
// Read the referncence frame
306
static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
Scott LaVarnway's avatar
Scott LaVarnway committed
307
                            vp9_reader *r,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
308
                            int segment_id, MV_REFERENCE_FRAME ref_frame[2]) {
309
  FRAME_CONTEXT *const fc = cm->fc;
Scott LaVarnway's avatar
Scott LaVarnway committed
310
  FRAME_COUNTS *counts = xd->counts;
311

312
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
Scott LaVarnway's avatar
Scott LaVarnway committed
313 314
    ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
                                                   SEG_LVL_REF_FRAME);
315 316
    ref_frame[1] = NONE;
  } else {
Scott LaVarnway's avatar
Scott LaVarnway committed
317
    const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
318
    // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
319
    if (mode == COMPOUND_REFERENCE) {
320 321 322
      const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
      const int ctx = vp9_get_pred_context_comp_ref_p(cm, xd);
      const int bit = vp9_read(r, fc->comp_ref_prob[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
323
      if (counts)
324 325 326
        ++counts->comp_ref[ctx][bit];
      ref_frame[idx] = cm->comp_fixed_ref;
      ref_frame[!idx] = cm->comp_var_ref[bit];
327
    } else if (mode == SINGLE_REFERENCE) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
328 329
      const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
      const int bit0 = vp9_read(r, fc->single_ref_prob[ctx0][0]);
Scott LaVarnway's avatar
Scott LaVarnway committed
330
      if (counts)
331
        ++counts->single_ref[ctx0][0][bit0];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
332 333 334
      if (bit0) {
        const int ctx1 = vp9_get_pred_context_single_ref_p2(xd);
        const int bit1 = vp9_read(r, fc->single_ref_prob[ctx1][1]);
Scott LaVarnway's avatar
Scott LaVarnway committed
335
        if (counts)
336
          ++counts->single_ref[ctx1][1][bit1];
337
        ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
338 339
      } else {
        ref_frame[0] = LAST_FRAME;
John Koleszar's avatar
John Koleszar committed
340
      }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
341 342

      ref_frame[1] = NONE;
343
    } else {
James Zern's avatar
James Zern committed
344
      assert(0 && "Invalid prediction mode.");
345
    }
John Koleszar's avatar
John Koleszar committed
346
  }
347
}
John Koleszar's avatar
John Koleszar committed
348

349

350
static INLINE INTERP_FILTER read_switchable_interp_filter(
351
    VP9_COMMON *const cm, MACROBLOCKD *const xd,
Scott LaVarnway's avatar
Scott LaVarnway committed
352
    vp9_reader *r) {
353
  const int ctx = vp9_get_pred_context_switchable_interp(xd);
354 355
  const INTERP_FILTER type =
      (INTERP_FILTER)vp9_read_tree(r, vp9_switchable_interp_tree,
356
                                   cm->fc->switchable_interp_prob[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
357 358
  FRAME_COUNTS *counts = xd->counts;
  if (counts)
359
    ++counts->switchable_interp[ctx][type];
360
  return type;
361 362
}

363
static void read_intra_block_mode_info(VP9_COMMON *const cm,
Scott LaVarnway's avatar
Scott LaVarnway committed
364
                                       MACROBLOCKD *const xd, MODE_INFO *mi,
365
                                       vp9_reader *r) {
366
  MB_MODE_INFO *const mbmi = &mi->mbmi;
367
  const BLOCK_SIZE bsize = mi->mbmi.sb_type;
368
  int i;
369

Dmitry Kovalev's avatar
Dmitry Kovalev committed
370 371 372
  mbmi->ref_frame[0] = INTRA_FRAME;
  mbmi->ref_frame[1] = NONE;

373 374 375
  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
Scott LaVarnway's avatar
Scott LaVarnway committed
376
        mi->bmi[i].as_mode = read_intra_mode_y(cm, xd, r, 0);
377 378 379
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
Scott LaVarnway's avatar
Scott LaVarnway committed
380
      mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd,
381
                                                                  r, 0);
382
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Scott LaVarnway's avatar
Scott LaVarnway committed
383
          read_intra_mode_y(cm, xd, r, 0);
384 385
      break;
    case BLOCK_8X4:
Scott LaVarnway's avatar
Scott LaVarnway committed
386
      mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd,
387
                                                                  r, 0);
388
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Scott LaVarnway's avatar
Scott LaVarnway committed
389
          read_intra_mode_y(cm, xd, r, 0);
390 391
      break;
    default:
Scott LaVarnway's avatar
Scott LaVarnway committed
392
      mbmi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
393 394
  }

Scott LaVarnway's avatar
Scott LaVarnway committed
395
  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
396 397
}

398 399 400 401 402
static INLINE int is_mv_valid(const MV *mv) {
  return mv->row > MV_LOW && mv->row < MV_UPP &&
         mv->col > MV_LOW && mv->col < MV_UPP;
}

Scott LaVarnway's avatar
Scott LaVarnway committed
403
static INLINE int assign_mv(VP9_COMMON *cm, MACROBLOCKD *xd,
404
                            PREDICTION_MODE mode,
405 406 407
                            int_mv mv[2], int_mv ref_mv[2],
                            int_mv nearest_mv[2], int_mv near_mv[2],
                            int is_compound, int allow_hp, vp9_reader *r) {
408
  int i;
Yaowu Xu's avatar
Yaowu Xu committed
409
  int ret = 1;
410 411

  switch (mode) {
412
    case NEWMV: {
Scott LaVarnway's avatar
Scott LaVarnway committed
413 414
      FRAME_COUNTS *counts = xd->counts;
      nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
415
      for (i = 0; i < 1 + is_compound; ++i) {
416
        read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
417 418
                allow_hp);
        ret = ret && is_mv_valid(&mv[i].as_mv);
419 420 421 422 423
      }
      break;
    }
    case NEARESTMV: {
      mv[0].as_int = nearest_mv[0].as_int;
424 425
      if (is_compound)
        mv[1].as_int = nearest_mv[1].as_int;
426
      break;
427 428
    }
    case NEARMV: {
429
      mv[0].as_int = near_mv[0].as_int;
430 431
      if (is_compound)
        mv[1].as_int = near_mv[1].as_int;
432
      break;
433 434
    }
    case ZEROMV: {
435
      mv[0].as_int = 0;
436 437
      if (is_compound)
        mv[1].as_int = 0;
438
      break;
439 440
    }
    default: {
Yaowu Xu's avatar
Yaowu Xu committed
441
      return 0;
442
    }
443
  }
Yaowu Xu's avatar
Yaowu Xu committed
444
  return ret;
445 446
}

447 448
static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
                               int segment_id, vp9_reader *r) {
449
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
Scott LaVarnway's avatar
Scott LaVarnway committed
450
    return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
451
  } else {
452
    const int ctx = vp9_get_intra_inter_context(xd);
453
    const int is_inter = vp9_read(r, cm->fc->intra_inter_prob[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
454 455
    FRAME_COUNTS *counts = xd->counts;
    if (counts)
456
      ++counts->intra_inter[ctx][is_inter];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
457
    return is_inter;
458 459 460
  }
}

461 462
static void fpm_sync(void *const data, int mi_row) {
  VP9Decoder *const pbi = (VP9Decoder *)data;
463
  vp9_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
464 465 466 467
                       mi_row << MI_BLOCK_SIZE_LOG2);
}

static void read_inter_block_mode_info(VP9Decoder *const pbi,
468 469
                                       MACROBLOCKD *const xd,
                                       MODE_INFO *const mi,
470
                                       int mi_row, int mi_col, vp9_reader *r) {
471
  VP9_COMMON *const cm = &pbi->common;
472
  MB_MODE_INFO *const mbmi = &mi->mbmi;
473
  const BLOCK_SIZE bsize = mbmi->sb_type;
474
  const int allow_hp = cm->allow_high_precision_mv;
475
  int_mv nearestmv[2], nearmv[2];
476 477 478
  int_mv ref_mvs[MAX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
  int ref, is_compound;
  uint8_t inter_mode_ctx[MAX_REF_FRAMES];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
479

Scott LaVarnway's avatar
Scott LaVarnway committed
480
  read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
481
  is_compound = has_second_ref(mbmi);
482

483 484
  for (ref = 0; ref < 1 + is_compound; ++ref) {
    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
485
    RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
Scott LaVarnway's avatar
Scott LaVarnway committed
486

487 488
    xd->block_refs[ref] = ref_buf;
    if ((!vp9_is_valid_scale(&ref_buf->sf)))
489
      vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
490
                         "Reference frame has invalid dimensions");
491 492
    vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
                         &ref_buf->sf);
Scott LaVarnway's avatar
Scott LaVarnway committed
493
    vp9_find_mv_refs(cm, xd, mi, frame, ref_mvs[frame],
494
                     mi_row, mi_col, fpm_sync, (void *)pbi, inter_mode_ctx);
495
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
496

497
  if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
498
    mbmi->mode = ZEROMV;
499
    if (bsize < BLOCK_8X8) {
500
        vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
501 502 503
                           "Invalid usage of segement feature on small blocks");
        return;
    }
504 505
  } else {
    if (bsize >= BLOCK_8X8)
506 507
      mbmi->mode = read_inter_mode(cm, xd, r,
                                   inter_mode_ctx[mbmi->ref_frame[0]]);
508
  }
509

510
  if (bsize < BLOCK_8X8 || mbmi->mode != ZEROMV) {
511
    for (ref = 0; ref < 1 + is_compound; ++ref) {
512
      vp9_find_best_ref_mvs(xd, allow_hp, ref_mvs[mbmi->ref_frame[ref]],
513
                            &nearestmv[ref], &nearmv[ref]);
514
    }
515
  }
516

517
  mbmi->interp_filter = (cm->interp_filter == SWITCHABLE)
Scott LaVarnway's avatar
Scott LaVarnway committed
518
                      ? read_switchable_interp_filter(cm, xd, r)
519
                      : cm->interp_filter;
520

521
  if (bsize < BLOCK_8X8) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
522 523
    const int num_4x4_w = num_4x4_blocks_wide_lookup[bsize];  // 1 or 2
    const int num_4x4_h = num_4x4_blocks_high_lookup[bsize];  // 1 or 2
524
    int idx, idy;
525
    PREDICTION_MODE b_mode;
526
    int_mv nearest_sub8x8[2], near_sub8x8[2];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
527 528
    for (idy = 0; idy < 2; idy += num_4x4_h) {
      for (idx = 0; idx < 2; idx += num_4x4_w) {
529
        int_mv block[2];
530
        const int j = idy * 2 + idx;
531
        b_mode = read_inter_mode(cm, xd, r, inter_mode_ctx[mbmi->ref_frame[0]]);
532

533 534
        if (b_mode == NEARESTMV || b_mode == NEARMV) {
          uint8_t dummy_mode_ctx[MAX_REF_FRAMES];
535
          for (ref = 0; ref < 1 + is_compound; ++ref)
Scott LaVarnway's avatar
Scott LaVarnway committed
536
            vp9_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
537
                                          &nearest_sub8x8[ref],
538 539 540
                                          &near_sub8x8[ref],
                                          dummy_mode_ctx);
        }
541

Scott LaVarnway's avatar
Scott LaVarnway committed
542
        if (!assign_mv(cm, xd, b_mode, block, nearestmv,
543
                       nearest_sub8x8, near_sub8x8,
Yaowu Xu's avatar
Yaowu Xu committed
544 545 546 547 548
                       is_compound, allow_hp, r)) {
          xd->corrupted |= 1;
          break;
        };

549 550
        mi->bmi[j].as_mv[0].as_int = block[0].as_int;
        if (is_compound)
551
          mi->bmi[j].as_mv[1].as_int = block[1].as_int;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
552

Dmitry Kovalev's avatar
Dmitry Kovalev committed
553
        if (num_4x4_h == 2)
554
          mi->bmi[j + 2] = mi->bmi[j];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
555
        if (num_4x4_w == 2)
556
          mi->bmi[j + 1] = mi->bmi[j];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
557
      }
Scott LaVarnway's avatar
Scott LaVarnway committed
558
    }
559

560
    mi->mbmi.mode = b_mode;
561

562 563 564
    mbmi->mv[0].as_int = mi->bmi[3].as_mv[0].as_int;
    mbmi->mv[1].as_int = mi->bmi[3].as_mv[1].as_int;
  } else {
Scott LaVarnway's avatar
Scott LaVarnway committed
565
    xd->corrupted |= !assign_mv(cm, xd, mbmi->mode, mbmi->mv, nearestmv,
566
                                nearestmv, nearmv, is_compound, allow_hp, r);
John Koleszar's avatar
John Koleszar committed
567
  }
Scott LaVarnway's avatar
Scott LaVarnway committed
568
}
John Koleszar's avatar
John Koleszar committed
569

570
static void read_inter_frame_mode_info(VP9Decoder *const pbi,
571
                                       MACROBLOCKD *const xd,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
572
                                       int mi_row, int mi_col, vp9_reader *r) {
573
  VP9_COMMON *const cm = &pbi->common;
574
  MODE_INFO *const mi = xd->mi[0];
575
  MB_MODE_INFO *const mbmi = &mi->mbmi;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
576
  int inter_block;
577

Dmitry Kovalev's avatar
Dmitry Kovalev committed
578 579
  mbmi->mv[0].as_int = 0;
  mbmi->mv[1].as_int = 0;
580
  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
Scott LaVarnway's avatar
Scott LaVarnway committed
581 582 583
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
  inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
  mbmi->tx_size = read_tx_size(cm, xd, !mbmi->skip || !inter_block, r);
584

Dmitry Kovalev's avatar
Dmitry Kovalev committed
585
  if (inter_block)
Scott LaVarnway's avatar
Scott LaVarnway committed
586
    read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
587
  else
Scott LaVarnway's avatar
Scott LaVarnway committed
588
    read_intra_block_mode_info(cm, xd, mi, r);
589 590
}

591
void vp9_read_mode_info(VP9Decoder *const pbi, MACROBLOCKD *xd,
592
                        int mi_row, int mi_col, vp9_reader *r) {
593
  VP9_COMMON *const cm = &pbi->common;
594
  MODE_INFO *const mi = xd->mi[0];
595 596 597 598 599 600 601
  const int bw = num_8x8_blocks_wide_lookup[mi->mbmi.sb_type];
  const int bh = num_8x8_blocks_high_lookup[mi->mbmi.sb_type];
  const int x_mis = MIN(bw, cm->mi_cols - mi_col);
  const int y_mis = MIN(bh, cm->mi_rows - mi_row);
  MV_REF* frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  int w, h;

602
  if (frame_is_intra_only(cm)) {
Scott LaVarnway's avatar
Scott LaVarnway committed
603
    read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
604
  } else {
Scott LaVarnway's avatar
Scott LaVarnway committed
605
    read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r);
606

607 608 609 610 611 612 613 614 615
    for (h = 0; h < y_mis; ++h) {
      MV_REF *const frame_mv = frame_mvs + h * cm->mi_cols;
      for (w = 0; w < x_mis; ++w) {
        MV_REF *const mv = frame_mv + w;
        mv->ref_frame[0] = mi->mbmi.ref_frame[0];
        mv->ref_frame[1] = mi->mbmi.ref_frame[1];
        mv->mv[0].as_int = mi->mbmi.mv[0].as_int;
        mv->mv[1].as_int = mi->mbmi.mv[1].as_int;
      }
616 617
    }
  }
618
}