vp9_decodemv.c 21.5 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
}

30
static PREDICTION_MODE read_intra_mode_y(VP9_COMMON *cm, vp9_reader *r,
31
                                            int size_group) {
32
  const PREDICTION_MODE y_mode =
33
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
34 35
  if (!cm->frame_parallel_decoding_mode)
    ++cm->counts.y_mode[size_group][y_mode];
36 37 38
  return y_mode;
}

39 40 41
static PREDICTION_MODE read_intra_mode_uv(VP9_COMMON *cm, vp9_reader *r,
                                          PREDICTION_MODE y_mode) {
  const PREDICTION_MODE uv_mode = read_intra_mode(r,
42
                                         cm->fc->uv_mode_prob[y_mode]);
43 44
  if (!cm->frame_parallel_decoding_mode)
    ++cm->counts.uv_mode[y_mode][uv_mode];
45 46 47
  return uv_mode;
}

48
static PREDICTION_MODE read_inter_mode(VP9_COMMON *cm, vp9_reader *r, int ctx) {
49
  const int mode = vp9_read_tree(r, vp9_inter_mode_tree,
50
                                 cm->fc->inter_mode_probs[ctx]);
51
  if (!cm->frame_parallel_decoding_mode)
52 53 54
    ++cm->counts.inter_mode[ctx][mode];

  return NEARESTMV + mode;
55 56
}

57
static int read_segment_id(vp9_reader *r, const struct segmentation *seg) {
58
  return vp9_read_tree(r, vp9_segment_tree, seg->tree_probs);
Scott LaVarnway's avatar
Scott LaVarnway committed
59
}
60

61
static TX_SIZE read_selected_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
62
                                     TX_SIZE max_tx_size, vp9_reader *r) {
63
  const int ctx = vp9_get_tx_size_context(xd);
64
  const vp9_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
65
  int tx_size = vp9_read(r, tx_probs[0]);
66
  if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
67
    tx_size += vp9_read(r, tx_probs[1]);
68
    if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
69
      tx_size += vp9_read(r, tx_probs[2]);
70 71
  }

72
  if (!cm->frame_parallel_decoding_mode)
73
    ++get_tx_counts(max_tx_size, ctx, &cm->counts.tx)[tx_size];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
74
  return (TX_SIZE)tx_size;
75 76
}

77 78 79 80
static TX_SIZE read_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
                            int allow_select, vp9_reader *r) {
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type;
81 82 83 84 85
  const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
  if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
    return read_selected_tx_size(cm, xd, max_tx_size, r);
  else
    return MIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
86 87
}

88 89 90
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;
91 92
  const int bw = num_8x8_blocks_wide_lookup[bsize];
  const int bh = num_8x8_blocks_high_lookup[bsize];
93 94 95 96 97 98 99 100
  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++)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
      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;
119 120
}

121 122
static int read_intra_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
                                 int mi_row, int mi_col,
123
                                 vp9_reader *r) {
124
  struct segmentation *const seg = &cm->seg;
hkuang's avatar
hkuang committed
125
  const BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type;
126
  int segment_id;
127

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

131 132 133
  if (!seg->update_map) {
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
                    bsize, mi_row, mi_col);
134
    return 0;
135
  }
136

137
  segment_id = read_segment_id(r, seg);
138
  set_segment_id(cm, bsize, mi_row, mi_col, segment_id);
139
  return segment_id;
140 141
}

142 143
static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
                                 int mi_row, int mi_col, vp9_reader *r) {
144
  struct segmentation *const seg = &cm->seg;
hkuang's avatar
hkuang committed
145
  MB_MODE_INFO *const mbmi = &xd->mi[0].src_mi->mbmi;
146 147
  const BLOCK_SIZE bsize = mbmi->sb_type;
  int predicted_segment_id, segment_id;
148 149 150 151

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

152 153 154 155 156 157
  predicted_segment_id = cm->last_frame_seg_map ?
      vp9_get_segment_id(cm, cm->last_frame_seg_map, bsize, mi_row, mi_col) : 0;

  if (!seg->update_map) {
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
                    bsize, mi_row, mi_col);
158
    return predicted_segment_id;
159
  }
160 161

  if (seg->temporal_update) {
162
    const vp9_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
163 164 165
    mbmi->seg_id_predicted = vp9_read(r, pred_prob);
    segment_id = mbmi->seg_id_predicted ? predicted_segment_id
                                        : read_segment_id(r, seg);
166
  } else {
167
    segment_id = read_segment_id(r, seg);
168
  }
169 170
  set_segment_id(cm, bsize, mi_row, mi_col, segment_id);
  return segment_id;
171 172
}

173 174
static int read_skip(VP9_COMMON *cm, const MACROBLOCKD *xd,
                     int segment_id, vp9_reader *r) {
175 176 177
  if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
178
    const int ctx = vp9_get_skip_context(xd);
179
    const int skip = vp9_read(r, cm->fc->skip_probs[ctx]);
180
    if (!cm->frame_parallel_decoding_mode)
Dmitry Kovalev's avatar
Dmitry Kovalev committed
181
      ++cm->counts.skip[ctx][skip];
182
    return skip;
Deb Mukherjee's avatar
Deb Mukherjee committed
183
  }
184
}
John Koleszar's avatar
John Koleszar committed
185

186 187
static void read_intra_frame_mode_info(VP9_COMMON *const cm,
                                       MACROBLOCKD *const xd,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
188
                                       int mi_row, int mi_col, vp9_reader *r) {
hkuang's avatar
hkuang committed
189
  MODE_INFO *const mi = xd->mi[0].src_mi;
190
  MB_MODE_INFO *const mbmi = &mi->mbmi;
hkuang's avatar
hkuang committed
191 192
  const MODE_INFO *above_mi = xd->mi[-cm->mi_stride].src_mi;
  const MODE_INFO *left_mi  = xd->left_available ? xd->mi[-1].src_mi : NULL;
193
  const BLOCK_SIZE bsize = mbmi->sb_type;
194
  int i;
195

196
  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_row, mi_col, r);
197
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
198
  mbmi->tx_size = read_tx_size(cm, xd, 1, r);
199
  mbmi->ref_frame[0] = INTRA_FRAME;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
200
  mbmi->ref_frame[1] = NONE;
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  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
224
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
225

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

229 230 231
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
232
  const int sign = vp9_read(r, mvcomp->sign);
233
  const int mv_class = vp9_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
234
  const int class0 = mv_class == MV_CLASS_0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
235

236
  // Integer part
237
  if (class0) {
238
    d = vp9_read_tree(r, vp9_mv_class0_tree, mvcomp->class0);
239
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
240
    int i;
241
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
Dmitry Kovalev's avatar
Dmitry Kovalev committed
242

243
    d = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
244 245
    for (i = 0; i < n; ++i)
      d |= vp9_read(r, mvcomp->bits[i]) << i;
246 247
  }

248
  // Fractional part
249 250
  fr = vp9_read_tree(r, vp9_mv_fp_tree, class0 ? mvcomp->class0_fp[d]
                                               : mvcomp->fp);
251

252
  // High precision part (if hp is not used, the default value of the hp is 1)
253
  hp = usehp ? vp9_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp)
254
             : 1;
255

256
  // Result
257 258
  mag = vp9_get_mv_mag(mv_class, (d << 3) | (fr << 1) | hp) + 1;
  return sign ? -mag : mag;
259 260
}

261 262
static INLINE void read_mv(vp9_reader *r, MV *mv, const MV *ref,
                           const nmv_context *ctx,
263
                           nmv_context_counts *counts, int allow_hp) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
264 265
  const MV_JOINT_TYPE joint_type =
      (MV_JOINT_TYPE)vp9_read_tree(r, vp9_mv_joint_tree, ctx->joints);
266
  const int use_hp = allow_hp && vp9_use_mv_hp(ref);
267 268
  MV diff = {0, 0};

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

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

275
  vp9_inc_mv(&diff, counts);
276 277 278 279 280

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

281 282 283 284 285
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);
286
    const REFERENCE_MODE mode =
287
        (REFERENCE_MODE)vp9_read(r, cm->fc->comp_inter_prob[ctx]);
288 289 290 291 292 293
    if (!cm->frame_parallel_decoding_mode)
      ++cm->counts.comp_inter[ctx][mode];
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
  } else {
    return cm->reference_mode;
  }
294 295
}

296
// Read the referncence frame
297 298
static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
                            vp9_reader *r,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
299
                            int segment_id, MV_REFERENCE_FRAME ref_frame[2]) {
300
  FRAME_CONTEXT *const fc = cm->fc;
301
  FRAME_COUNTS *const counts = &cm->counts;
302

303
  if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
304 305
    ref_frame[0] = (MV_REFERENCE_FRAME)vp9_get_segdata(&cm->seg, segment_id,
                                                       SEG_LVL_REF_FRAME);
306 307
    ref_frame[1] = NONE;
  } else {
308
    const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
309
    // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
310
    if (mode == COMPOUND_REFERENCE) {
311 312 313
      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]);
314
      if (!cm->frame_parallel_decoding_mode)
315 316 317
        ++counts->comp_ref[ctx][bit];
      ref_frame[idx] = cm->comp_fixed_ref;
      ref_frame[!idx] = cm->comp_var_ref[bit];
318
    } else if (mode == SINGLE_REFERENCE) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
319 320
      const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
      const int bit0 = vp9_read(r, fc->single_ref_prob[ctx0][0]);
321 322
      if (!cm->frame_parallel_decoding_mode)
        ++counts->single_ref[ctx0][0][bit0];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
323 324 325
      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]);
326 327
        if (!cm->frame_parallel_decoding_mode)
          ++counts->single_ref[ctx1][1][bit1];
328
        ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
329 330
      } else {
        ref_frame[0] = LAST_FRAME;
John Koleszar's avatar
John Koleszar committed
331
      }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
332 333

      ref_frame[1] = NONE;
334
    } else {
James Zern's avatar
James Zern committed
335
      assert(0 && "Invalid prediction mode.");
336
    }
John Koleszar's avatar
John Koleszar committed
337
  }
338
}
John Koleszar's avatar
John Koleszar committed
339

340

341
static INLINE INTERP_FILTER read_switchable_interp_filter(
342
    VP9_COMMON *const cm, MACROBLOCKD *const xd, vp9_reader *r) {
343
  const int ctx = vp9_get_pred_context_switchable_interp(xd);
344 345
  const INTERP_FILTER type =
      (INTERP_FILTER)vp9_read_tree(r, vp9_switchable_interp_tree,
346
                                   cm->fc->switchable_interp_prob[ctx]);
347 348
  if (!cm->frame_parallel_decoding_mode)
    ++cm->counts.switchable_interp[ctx][type];
349
  return type;
350 351
}

352 353
static void read_intra_block_mode_info(VP9_COMMON *const cm, MODE_INFO *mi,
                                       vp9_reader *r) {
354
  MB_MODE_INFO *const mbmi = &mi->mbmi;
355
  const BLOCK_SIZE bsize = mi->mbmi.sb_type;
356
  int i;
357

Dmitry Kovalev's avatar
Dmitry Kovalev committed
358 359 360
  mbmi->ref_frame[0] = INTRA_FRAME;
  mbmi->ref_frame[1] = NONE;

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
        mi->bmi[i].as_mode = read_intra_mode_y(cm, r, 0);
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
      mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, r, 0);
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
          read_intra_mode_y(cm, r, 0);
      break;
    case BLOCK_8X4:
      mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, r, 0);
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
          read_intra_mode_y(cm, r, 0);
      break;
    default:
      mbmi->mode = read_intra_mode_y(cm, r, size_group_lookup[bsize]);
379 380
  }

381
  mbmi->uv_mode = read_intra_mode_uv(cm, r, mbmi->mode);
382 383
}

384 385 386 387 388
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;
}

389
static INLINE int assign_mv(VP9_COMMON *cm, PREDICTION_MODE mode,
390 391 392
                            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) {
393
  int i;
Yaowu Xu's avatar
Yaowu Xu committed
394
  int ret = 1;
395 396

  switch (mode) {
397 398 399 400
    case NEWMV: {
      nmv_context_counts *const mv_counts = cm->frame_parallel_decoding_mode ?
                                            NULL : &cm->counts.mv;
      for (i = 0; i < 1 + is_compound; ++i) {
401
        read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
402 403
                allow_hp);
        ret = ret && is_mv_valid(&mv[i].as_mv);
404 405 406 407 408
      }
      break;
    }
    case NEARESTMV: {
      mv[0].as_int = nearest_mv[0].as_int;
409 410
      if (is_compound)
        mv[1].as_int = nearest_mv[1].as_int;
411
      break;
412 413
    }
    case NEARMV: {
414
      mv[0].as_int = near_mv[0].as_int;
415 416
      if (is_compound)
        mv[1].as_int = near_mv[1].as_int;
417
      break;
418 419
    }
    case ZEROMV: {
420
      mv[0].as_int = 0;
421 422
      if (is_compound)
        mv[1].as_int = 0;
423
      break;
424 425
    }
    default: {
Yaowu Xu's avatar
Yaowu Xu committed
426
      return 0;
427
    }
428
  }
Yaowu Xu's avatar
Yaowu Xu committed
429
  return ret;
430 431
}

432 433
static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
                               int segment_id, vp9_reader *r) {
434 435
  if (vp9_segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
    return vp9_get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) !=
Dmitry Kovalev's avatar
Dmitry Kovalev committed
436
           INTRA_FRAME;
437
  } else {
438
    const int ctx = vp9_get_intra_inter_context(xd);
439
    const int is_inter = vp9_read(r, cm->fc->intra_inter_prob[ctx]);
440 441
    if (!cm->frame_parallel_decoding_mode)
      ++cm->counts.intra_inter[ctx][is_inter];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
442
    return is_inter;
443 444 445
  }
}

446 447 448 449 450 451 452
static void fpm_sync(void *const data, int mi_row) {
  VP9Decoder *const pbi = (VP9Decoder *)data;
  vp9_frameworker_wait(pbi->frame_worker_owner, pbi->prev_buf,
                       mi_row << MI_BLOCK_SIZE_LOG2);
}

static void read_inter_block_mode_info(VP9Decoder *const pbi,
453
                                       MACROBLOCKD *const xd,
James Zern's avatar
James Zern committed
454
                                       const TileInfo *const tile,
455
                                       MODE_INFO *const mi,
456
                                       int mi_row, int mi_col, vp9_reader *r) {
457
  VP9_COMMON *const cm = &pbi->common;
458
  MB_MODE_INFO *const mbmi = &mi->mbmi;
459
  const BLOCK_SIZE bsize = mbmi->sb_type;
460
  const int allow_hp = cm->allow_high_precision_mv;
461
  int_mv nearestmv[2], nearmv[2];
462
  int inter_mode_ctx, ref, is_compound;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
463

464
  read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
465
  is_compound = has_second_ref(mbmi);
466

467 468
  for (ref = 0; ref < 1 + is_compound; ++ref) {
    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
469 470 471
    RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
    xd->block_refs[ref] = ref_buf;
    if ((!vp9_is_valid_scale(&ref_buf->sf)))
472
      vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
473
                         "Reference frame has invalid dimensions");
474 475
    vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
                         &ref_buf->sf);
476
    vp9_find_mv_refs(cm, xd, tile, mi, frame, mbmi->ref_mvs[frame],
477
                     mi_row, mi_col, fpm_sync, (void *)pbi);
478
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
479

480
  inter_mode_ctx = mbmi->mode_context[mbmi->ref_frame[0]];
481

482
  if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
483
    mbmi->mode = ZEROMV;
484
    if (bsize < BLOCK_8X8) {
485
        vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
486 487 488
                           "Invalid usage of segement feature on small blocks");
        return;
    }
489 490 491 492
  } else {
    if (bsize >= BLOCK_8X8)
      mbmi->mode = read_inter_mode(cm, r, inter_mode_ctx);
  }
493

494
  if (bsize < BLOCK_8X8 || mbmi->mode != ZEROMV) {
495 496
    for (ref = 0; ref < 1 + is_compound; ++ref) {
      vp9_find_best_ref_mvs(xd, allow_hp, mbmi->ref_mvs[mbmi->ref_frame[ref]],
497
                            &nearestmv[ref], &nearmv[ref]);
498
    }
499
  }
500

501 502 503
  mbmi->interp_filter = (cm->interp_filter == SWITCHABLE)
                      ? read_switchable_interp_filter(cm, xd, r)
                      : cm->interp_filter;
504

505
  if (bsize < BLOCK_8X8) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
506 507
    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
508
    int idx, idy;
509
    PREDICTION_MODE b_mode;
510
    int_mv nearest_sub8x8[2], near_sub8x8[2];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
511 512
    for (idy = 0; idy < 2; idy += num_4x4_h) {
      for (idx = 0; idx < 2; idx += num_4x4_w) {
513
        int_mv block[2];
514
        const int j = idy * 2 + idx;
515
        b_mode = read_inter_mode(cm, r, inter_mode_ctx);
516

517 518
        if (b_mode == NEARESTMV || b_mode == NEARMV)
          for (ref = 0; ref < 1 + is_compound; ++ref)
519
            vp9_append_sub8x8_mvs_for_idx(cm, xd, tile, j, ref, mi_row, mi_col,
520 521
                                          &nearest_sub8x8[ref],
                                          &near_sub8x8[ref]);
522

523 524
        if (!assign_mv(cm, b_mode, block, nearestmv,
                       nearest_sub8x8, near_sub8x8,
Yaowu Xu's avatar
Yaowu Xu committed
525 526 527 528 529
                       is_compound, allow_hp, r)) {
          xd->corrupted |= 1;
          break;
        };

530 531
        mi->bmi[j].as_mv[0].as_int = block[0].as_int;
        if (is_compound)
532
          mi->bmi[j].as_mv[1].as_int = block[1].as_int;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
533

Dmitry Kovalev's avatar
Dmitry Kovalev committed
534
        if (num_4x4_h == 2)
535
          mi->bmi[j + 2] = mi->bmi[j];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
536
        if (num_4x4_w == 2)
537
          mi->bmi[j + 1] = mi->bmi[j];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
538
      }
Scott LaVarnway's avatar
Scott LaVarnway committed
539
    }
540

541
    mi->mbmi.mode = b_mode;
542

543 544 545
    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 {
546 547
    xd->corrupted |= !assign_mv(cm, mbmi->mode, mbmi->mv, nearestmv,
                                nearestmv, nearmv, is_compound, allow_hp, r);
John Koleszar's avatar
John Koleszar committed
548
  }
Scott LaVarnway's avatar
Scott LaVarnway committed
549
}
John Koleszar's avatar
John Koleszar committed
550

551
static void read_inter_frame_mode_info(VP9Decoder *const pbi,
552
                                       MACROBLOCKD *const xd,
James Zern's avatar
James Zern committed
553
                                       const TileInfo *const tile,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
554
                                       int mi_row, int mi_col, vp9_reader *r) {
555
  VP9_COMMON *const cm = &pbi->common;
hkuang's avatar
hkuang committed
556
  MODE_INFO *const mi = xd->mi[0].src_mi;
557
  MB_MODE_INFO *const mbmi = &mi->mbmi;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
558
  int inter_block;
559

Dmitry Kovalev's avatar
Dmitry Kovalev committed
560 561
  mbmi->mv[0].as_int = 0;
  mbmi->mv[1].as_int = 0;
562
  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
563
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
564
  inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
565
  mbmi->tx_size = read_tx_size(cm, xd, !mbmi->skip || !inter_block, r);
566

Dmitry Kovalev's avatar
Dmitry Kovalev committed
567
  if (inter_block)
568
    read_inter_block_mode_info(pbi, xd, tile, mi, mi_row, mi_col, r);
569
  else
570
    read_intra_block_mode_info(cm, mi, r);
571 572
}

573
void vp9_read_mode_info(VP9Decoder *const pbi, MACROBLOCKD *xd,
Johann's avatar
Johann committed
574
                        const TileInfo *const tile,
575
                        int mi_row, int mi_col, vp9_reader *r) {
576
  VP9_COMMON *const cm = &pbi->common;
577 578 579 580 581 582 583 584
  MODE_INFO *const mi = xd->mi[0].src_mi;
  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;

585
  if (frame_is_intra_only(cm))
586
    read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
587
  else
588
    read_inter_frame_mode_info(pbi, xd, tile, mi_row, mi_col, r);
589 590 591 592 593 594 595 596 597 598 599

  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->src_mi->mbmi.ref_frame[0];
      mv->ref_frame[1] = mi->src_mi->mbmi.ref_frame[1];
      mv->mv[0].as_int = mi->src_mi->mbmi.mv[0].as_int;
      mv->mv[1].as_int = mi->src_mi->mbmi.mv[1].as_int;
    }
  }
600
}