vp9_decodemv.c 22.4 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 31
static PREDICTION_MODE read_intra_mode_y(VP9_COMMON *cm, FRAME_COUNTS *counts,
                                         vp9_reader *r, int size_group) {
32
  const PREDICTION_MODE y_mode =
33
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
34
  if (!cm->frame_parallel_decoding_mode)
35
    ++counts->y_mode[size_group][y_mode];
36 37 38
  return y_mode;
}

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

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

  return NEARESTMV + mode;
57 58
}

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

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

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

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

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

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

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

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

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

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

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

155 156 157 158 159 160
  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);
161
    return predicted_segment_id;
162
  }
163 164

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

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

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

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

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  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
229
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
230

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

234 235 236
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
237
  const int sign = vp9_read(r, mvcomp->sign);
238
  const int mv_class = vp9_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
239
  const int class0 = mv_class == MV_CLASS_0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
240

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

248
    d = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
249 250
    for (i = 0; i < n; ++i)
      d |= vp9_read(r, mvcomp->bits[i]) << i;
251 252
  }

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

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

261
  // Result
262 263
  mag = vp9_get_mv_mag(mv_class, (d << 3) | (fr << 1) | hp) + 1;
  return sign ? -mag : mag;
264 265
}

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

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

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

280
  vp9_inc_mv(&diff, counts);
281 282 283 284 285

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

286 287
static REFERENCE_MODE read_block_reference_mode(VP9_COMMON *cm,
                                                const MACROBLOCKD *xd,
288
                                                FRAME_COUNTS *counts,
289 290 291
                                                vp9_reader *r) {
  if (cm->reference_mode == REFERENCE_MODE_SELECT) {
    const int ctx = vp9_get_reference_mode_context(cm, xd);
292
    const REFERENCE_MODE mode =
293
        (REFERENCE_MODE)vp9_read(r, cm->fc->comp_inter_prob[ctx]);
294
    if (!cm->frame_parallel_decoding_mode)
295
      ++counts->comp_inter[ctx][mode];
296 297 298 299
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
  } else {
    return cm->reference_mode;
  }
300 301
}

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

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

      ref_frame[1] = NONE;
339
    } else {
James Zern's avatar
James Zern committed
340
      assert(0 && "Invalid prediction mode.");
341
    }
John Koleszar's avatar
John Koleszar committed
342
  }
343
}
John Koleszar's avatar
John Koleszar committed
344

345

346
static INLINE INTERP_FILTER read_switchable_interp_filter(
347 348
    VP9_COMMON *const cm, MACROBLOCKD *const xd,
    FRAME_COUNTS *counts, vp9_reader *r) {
349
  const int ctx = vp9_get_pred_context_switchable_interp(xd);
350 351
  const INTERP_FILTER type =
      (INTERP_FILTER)vp9_read_tree(r, vp9_switchable_interp_tree,
352
                                   cm->fc->switchable_interp_prob[ctx]);
353
  if (!cm->frame_parallel_decoding_mode)
354
    ++counts->switchable_interp[ctx][type];
355
  return type;
356 357
}

358 359
static void read_intra_block_mode_info(VP9_COMMON *const cm,
                                       FRAME_COUNTS *counts, MODE_INFO *mi,
360
                                       vp9_reader *r) {
361
  MB_MODE_INFO *const mbmi = &mi->mbmi;
362
  const BLOCK_SIZE bsize = mi->mbmi.sb_type;
363
  int i;
364

Dmitry Kovalev's avatar
Dmitry Kovalev committed
365 366 367
  mbmi->ref_frame[0] = INTRA_FRAME;
  mbmi->ref_frame[1] = NONE;

368 369 370
  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
371
        mi->bmi[i].as_mode = read_intra_mode_y(cm, counts, r, 0);
372 373 374
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
375 376
      mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, counts,
                                                                  r, 0);
377
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
378
          read_intra_mode_y(cm, counts, r, 0);
379 380
      break;
    case BLOCK_8X4:
381 382
      mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, counts,
                                                                  r, 0);
383
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
384
          read_intra_mode_y(cm, counts, r, 0);
385 386
      break;
    default:
387
      mbmi->mode = read_intra_mode_y(cm, counts, r, size_group_lookup[bsize]);
388 389
  }

390
  mbmi->uv_mode = read_intra_mode_uv(cm, counts, r, mbmi->mode);
391 392
}

393 394 395 396 397
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;
}

398 399
static INLINE int assign_mv(VP9_COMMON *cm, FRAME_COUNTS *counts,
                            PREDICTION_MODE mode,
400 401 402
                            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) {
403
  int i;
Yaowu Xu's avatar
Yaowu Xu committed
404
  int ret = 1;
405 406

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

442
static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
443
                               FRAME_COUNTS *counts,
444
                               int segment_id, vp9_reader *r) {
445 446
  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
447
           INTRA_FRAME;
448
  } else {
449
    const int ctx = vp9_get_intra_inter_context(xd);
450
    const int is_inter = vp9_read(r, cm->fc->intra_inter_prob[ctx]);
451
    if (!cm->frame_parallel_decoding_mode)
452
      ++counts->intra_inter[ctx][is_inter];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
453
    return is_inter;
454 455 456
  }
}

457 458
static void fpm_sync(void *const data, int mi_row) {
  VP9Decoder *const pbi = (VP9Decoder *)data;
459
  vp9_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
460 461 462 463
                       mi_row << MI_BLOCK_SIZE_LOG2);
}

static void read_inter_block_mode_info(VP9Decoder *const pbi,
464
                                       MACROBLOCKD *const xd,
465
                                       FRAME_COUNTS *counts,
James Zern's avatar
James Zern committed
466
                                       const TileInfo *const tile,
467
                                       MODE_INFO *const mi,
468
                                       int mi_row, int mi_col, vp9_reader *r) {
469
  VP9_COMMON *const cm = &pbi->common;
470
  MB_MODE_INFO *const mbmi = &mi->mbmi;
471
  const BLOCK_SIZE bsize = mbmi->sb_type;
472
  const int allow_hp = cm->allow_high_precision_mv;
473
  int_mv nearestmv[2], nearmv[2];
474
  int inter_mode_ctx, ref, is_compound;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
475

476
  read_ref_frames(cm, xd, counts, r, mbmi->segment_id, mbmi->ref_frame);
477
  is_compound = has_second_ref(mbmi);
478

479 480
  for (ref = 0; ref < 1 + is_compound; ++ref) {
    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
481 482 483
    RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];
    xd->block_refs[ref] = ref_buf;
    if ((!vp9_is_valid_scale(&ref_buf->sf)))
484
      vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
485
                         "Reference frame has invalid dimensions");
486 487
    vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
                         &ref_buf->sf);
488
    vp9_find_mv_refs(cm, xd, tile, mi, frame, mbmi->ref_mvs[frame],
489
                     mi_row, mi_col, fpm_sync, (void *)pbi);
490
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
491

492
  inter_mode_ctx = mbmi->mode_context[mbmi->ref_frame[0]];
493

494
  if (vp9_segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
495
    mbmi->mode = ZEROMV;
496
    if (bsize < BLOCK_8X8) {
497
        vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
498 499 500
                           "Invalid usage of segement feature on small blocks");
        return;
    }
501 502
  } else {
    if (bsize >= BLOCK_8X8)
503
      mbmi->mode = read_inter_mode(cm, counts, r, inter_mode_ctx);
504
  }
505

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

513
  mbmi->interp_filter = (cm->interp_filter == SWITCHABLE)
514
                      ? read_switchable_interp_filter(cm, xd, counts, r)
515
                      : cm->interp_filter;
516

517
  if (bsize < BLOCK_8X8) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
518 519
    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
520
    int idx, idy;
521
    PREDICTION_MODE b_mode;
522
    int_mv nearest_sub8x8[2], near_sub8x8[2];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
523 524
    for (idy = 0; idy < 2; idy += num_4x4_h) {
      for (idx = 0; idx < 2; idx += num_4x4_w) {
525
        int_mv block[2];
526
        const int j = idy * 2 + idx;
527
        b_mode = read_inter_mode(cm, counts, r, inter_mode_ctx);
528

529 530
        if (b_mode == NEARESTMV || b_mode == NEARMV)
          for (ref = 0; ref < 1 + is_compound; ++ref)
531
            vp9_append_sub8x8_mvs_for_idx(cm, xd, tile, j, ref, mi_row, mi_col,
532 533
                                          &nearest_sub8x8[ref],
                                          &near_sub8x8[ref]);
534

535
        if (!assign_mv(cm, counts, b_mode, block, nearestmv,
536
                       nearest_sub8x8, near_sub8x8,
Yaowu Xu's avatar
Yaowu Xu committed
537 538 539 540 541
                       is_compound, allow_hp, r)) {
          xd->corrupted |= 1;
          break;
        };

542 543
        mi->bmi[j].as_mv[0].as_int = block[0].as_int;
        if (is_compound)
544
          mi->bmi[j].as_mv[1].as_int = block[1].as_int;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
545

Dmitry Kovalev's avatar
Dmitry Kovalev committed
546
        if (num_4x4_h == 2)
547
          mi->bmi[j + 2] = mi->bmi[j];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
548
        if (num_4x4_w == 2)
549
          mi->bmi[j + 1] = mi->bmi[j];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
550
      }
Scott LaVarnway's avatar
Scott LaVarnway committed
551
    }
552

553
    mi->mbmi.mode = b_mode;
554

555 556 557
    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 {
558
    xd->corrupted |= !assign_mv(cm, counts, mbmi->mode, mbmi->mv, nearestmv,
559
                                nearestmv, nearmv, is_compound, allow_hp, r);
John Koleszar's avatar
John Koleszar committed
560
  }
Scott LaVarnway's avatar
Scott LaVarnway committed
561
}
John Koleszar's avatar
John Koleszar committed
562

563
static void read_inter_frame_mode_info(VP9Decoder *const pbi,
564
                                       MACROBLOCKD *const xd,
565
                                       FRAME_COUNTS *counts,
James Zern's avatar
James Zern committed
566
                                       const TileInfo *const tile,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
567
                                       int mi_row, int mi_col, vp9_reader *r) {
568
  VP9_COMMON *const cm = &pbi->common;
569
  MODE_INFO *const mi = xd->mi[0];
570
  MB_MODE_INFO *const mbmi = &mi->mbmi;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
571
  int inter_block;
572

Dmitry Kovalev's avatar
Dmitry Kovalev committed
573 574
  mbmi->mv[0].as_int = 0;
  mbmi->mv[1].as_int = 0;
575
  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
576 577 578
  mbmi->skip = read_skip(cm, xd, counts, mbmi->segment_id, r);
  inter_block = read_is_inter_block(cm, xd, counts, mbmi->segment_id, r);
  mbmi->tx_size = read_tx_size(cm, xd, counts, !mbmi->skip || !inter_block, r);
579

Dmitry Kovalev's avatar
Dmitry Kovalev committed
580
  if (inter_block)
581
    read_inter_block_mode_info(pbi, xd, counts, tile, mi, mi_row, mi_col, r);
582
  else
583
    read_intra_block_mode_info(cm, counts, mi, r);
584 585
}

586
void vp9_read_mode_info(VP9Decoder *const pbi, MACROBLOCKD *xd,
587
                        FRAME_COUNTS *counts,
Johann's avatar
Johann committed
588
                        const TileInfo *const tile,
589
                        int mi_row, int mi_col, vp9_reader *r) {
590
  VP9_COMMON *const cm = &pbi->common;
591
  MODE_INFO *const mi = xd->mi[0];
592 593 594 595 596 597 598
  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;

599
  if (frame_is_intra_only(cm))
600
    read_intra_frame_mode_info(cm, xd, counts, mi_row, mi_col, r);
601
  else
602
    read_inter_frame_mode_info(pbi, xd, counts, tile, mi_row, mi_col, r);
603 604 605 606 607

  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;
608 609 610 611
      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;
612 613
    }
  }
614
}