vp9_decodemv.c 22 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

Yaowu Xu's avatar
Yaowu Xu committed
25 26
static PREDICTION_MODE read_intra_mode(vpx_reader *r, const vpx_prob *p) {
  return (PREDICTION_MODE)vpx_read_tree(r, vp9_intra_mode_tree, p);
27 28
}

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

Scott LaVarnway's avatar
Scott LaVarnway committed
39
static PREDICTION_MODE read_intra_mode_uv(VP9_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
40
                                          vpx_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]);
Scott LaVarnway's avatar
Scott LaVarnway committed
44 45
  FRAME_COUNTS *counts = xd->counts;
  if (counts)
46
    ++counts->uv_mode[y_mode][uv_mode];
47 48 49
  return uv_mode;
}

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

  return NEARESTMV + mode;
59 60
}

Yaowu Xu's avatar
Yaowu Xu committed
61 62
static int read_segment_id(vpx_reader *r, const struct segmentation *seg) {
  return vpx_read_tree(r, vp9_segment_tree, seg->tree_probs);
Scott LaVarnway's avatar
Scott LaVarnway committed
63
}
64

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

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

82
static TX_SIZE read_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
83
                            int allow_select, vpx_reader *r) {
84
  TX_MODE tx_mode = cm->tx_mode;
85
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
86 87
  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
88
    return read_selected_tx_size(cm, xd, max_tx_size, r);
89 90
  else
    return MIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
91 92
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
static int dec_get_segment_id(const VP9_COMMON *cm, const uint8_t *segment_ids,
                              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++)
      segment_id = MIN(segment_id,
                       segment_ids[mi_offset + y * cm->mi_cols + x]);

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

static void set_segment_id(VP9_COMMON *cm, int mi_offset,
                           int x_mis, int y_mis, int segment_id) {
108 109 110 111
  int x, y;

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

112 113
  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
114 115 116 117 118 119
      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,
120
                           int mi_offset, int x_mis, int y_mis) {
121 122
  int x, y;

123 124
  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
125 126
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =  last_segment_ids ?
          last_segment_ids[mi_offset + y * cm->mi_cols + x] : 0;
127 128
}

129 130
static int read_intra_segment_id(VP9_COMMON *const cm, int mi_offset,
                                 int x_mis, int y_mis,
Yaowu Xu's avatar
Yaowu Xu committed
131
                                 vpx_reader *r) {
132
  struct segmentation *const seg = &cm->seg;
133
  int segment_id;
134

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

138 139
  if (!seg->update_map) {
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
140
                    mi_offset, x_mis, y_mis);
141
    return 0;
142
  }
143

144
  segment_id = read_segment_id(r, seg);
145
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
146
  return segment_id;
147 148
}

149
static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xu's avatar
Yaowu Xu committed
150
                                 int mi_row, int mi_col, vpx_reader *r) {
151
  struct segmentation *const seg = &cm->seg;
152
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
153
  int predicted_segment_id, segment_id;
154 155 156 157 158 159 160
  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 ?????
  const int x_mis = MIN(cm->mi_cols - mi_col, bw);
  const int y_mis = MIN(cm->mi_rows - mi_row, bh);
161 162 163 164

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

165
  predicted_segment_id = cm->last_frame_seg_map ?
166 167
      dec_get_segment_id(cm, cm->last_frame_seg_map, mi_offset, x_mis, y_mis) :
      0;
168 169 170

  if (!seg->update_map) {
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
171
                    mi_offset, x_mis, y_mis);
172
    return predicted_segment_id;
173
  }
174 175

  if (seg->temporal_update) {
Yaowu Xu's avatar
Yaowu Xu committed
176 177
    const vpx_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
    mbmi->seg_id_predicted = vpx_read(r, pred_prob);
178 179
    segment_id = mbmi->seg_id_predicted ? predicted_segment_id
                                        : read_segment_id(r, seg);
180
  } else {
181
    segment_id = read_segment_id(r, seg);
182
  }
183
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
184
  return segment_id;
185 186
}

187
static int read_skip(VP9_COMMON *cm, const MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
188
                     int segment_id, vpx_reader *r) {
189
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
190 191
    return 1;
  } else {
192
    const int ctx = vp9_get_skip_context(xd);
Yaowu Xu's avatar
Yaowu Xu committed
193
    const int skip = vpx_read(r, cm->fc->skip_probs[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
194 195
    FRAME_COUNTS *counts = xd->counts;
    if (counts)
196
      ++counts->skip[ctx][skip];
197
    return skip;
Deb Mukherjee's avatar
Deb Mukherjee committed
198
  }
199
}
John Koleszar's avatar
John Koleszar committed
200

201 202
static void read_intra_frame_mode_info(VP9_COMMON *const cm,
                                       MACROBLOCKD *const xd,
Yaowu Xu's avatar
Yaowu Xu committed
203
                                       int mi_row, int mi_col, vpx_reader *r) {
204
  MODE_INFO *const mi = xd->mi[0];
205
  MB_MODE_INFO *const mbmi = &mi->mbmi;
206 207
  const MODE_INFO *above_mi = xd->above_mi;
  const MODE_INFO *left_mi  = xd->left_mi;
208
  const BLOCK_SIZE bsize = mbmi->sb_type;
209
  int i;
210 211 212 213 214 215 216
  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 ?????
  const int x_mis = MIN(cm->mi_cols - mi_col, bw);
  const int y_mis = MIN(cm->mi_rows - mi_row, bh);
217

218
  mbmi->segment_id = read_intra_segment_id(cm, mi_offset, x_mis, y_mis, r);
Scott LaVarnway's avatar
Scott LaVarnway committed
219 220
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
  mbmi->tx_size = read_tx_size(cm, xd, 1, r);
221
  mbmi->ref_frame[0] = INTRA_FRAME;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
222
  mbmi->ref_frame[1] = NONE;
223

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  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
246
  }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
247

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

Yaowu Xu's avatar
Yaowu Xu committed
251
static int read_mv_component(vpx_reader *r,
252 253
                             const nmv_component *mvcomp, int usehp) {
  int mag, d, fr, hp;
Yaowu Xu's avatar
Yaowu Xu committed
254 255
  const int sign = vpx_read(r, mvcomp->sign);
  const int mv_class = vpx_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
256
  const int class0 = mv_class == MV_CLASS_0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
257

258
  // Integer part
259
  if (class0) {
Yaowu Xu's avatar
Yaowu Xu committed
260
    d = vpx_read_tree(r, vp9_mv_class0_tree, mvcomp->class0);
261
    mag = 0;
262
  } else {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
263
    int i;
264
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
Dmitry Kovalev's avatar
Dmitry Kovalev committed
265

266
    d = 0;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
267
    for (i = 0; i < n; ++i)
Yaowu Xu's avatar
Yaowu Xu committed
268
      d |= vpx_read(r, mvcomp->bits[i]) << i;
269
    mag = CLASS0_SIZE << (mv_class + 2);
270 271
  }

272
  // Fractional part
Yaowu Xu's avatar
Yaowu Xu committed
273
  fr = vpx_read_tree(r, vp9_mv_fp_tree, class0 ? mvcomp->class0_fp[d]
274
                                               : mvcomp->fp);
275

276
  // High precision part (if hp is not used, the default value of the hp is 1)
Yaowu Xu's avatar
Yaowu Xu committed
277
  hp = usehp ? vpx_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp)
278
             : 1;
279

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

Yaowu Xu's avatar
Yaowu Xu committed
285
static INLINE void read_mv(vpx_reader *r, MV *mv, const MV *ref,
286
                           const nmv_context *ctx,
287
                           nmv_context_counts *counts, int allow_hp) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
288
  const MV_JOINT_TYPE joint_type =
Yaowu Xu's avatar
Yaowu Xu committed
289
      (MV_JOINT_TYPE)vpx_read_tree(r, vp9_mv_joint_tree, ctx->joints);
290
  const int use_hp = allow_hp && vp9_use_mv_hp(ref);
291 292
  MV diff = {0, 0};

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

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

299
  vp9_inc_mv(&diff, counts);
300 301 302 303 304

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

305 306
static REFERENCE_MODE read_block_reference_mode(VP9_COMMON *cm,
                                                const MACROBLOCKD *xd,
Yaowu Xu's avatar
Yaowu Xu committed
307
                                                vpx_reader *r) {
308 309
  if (cm->reference_mode == REFERENCE_MODE_SELECT) {
    const int ctx = vp9_get_reference_mode_context(cm, xd);
310
    const REFERENCE_MODE mode =
Yaowu Xu's avatar
Yaowu Xu committed
311
        (REFERENCE_MODE)vpx_read(r, cm->fc->comp_inter_prob[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
312 313
    FRAME_COUNTS *counts = xd->counts;
    if (counts)
314
      ++counts->comp_inter[ctx][mode];
315 316 317 318
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
  } else {
    return cm->reference_mode;
  }
319 320
}

321
// Read the referncence frame
322
static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xu's avatar
Yaowu Xu committed
323
                            vpx_reader *r,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
324
                            int segment_id, MV_REFERENCE_FRAME ref_frame[2]) {
325
  FRAME_CONTEXT *const fc = cm->fc;
Scott LaVarnway's avatar
Scott LaVarnway committed
326
  FRAME_COUNTS *counts = xd->counts;
327

328
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
Scott LaVarnway's avatar
Scott LaVarnway committed
329 330
    ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
                                                   SEG_LVL_REF_FRAME);
331 332
    ref_frame[1] = NONE;
  } else {
Scott LaVarnway's avatar
Scott LaVarnway committed
333
    const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
334
    // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
335
    if (mode == COMPOUND_REFERENCE) {
336 337
      const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
      const int ctx = vp9_get_pred_context_comp_ref_p(cm, xd);
Yaowu Xu's avatar
Yaowu Xu committed
338
      const int bit = vpx_read(r, fc->comp_ref_prob[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
339
      if (counts)
340 341 342
        ++counts->comp_ref[ctx][bit];
      ref_frame[idx] = cm->comp_fixed_ref;
      ref_frame[!idx] = cm->comp_var_ref[bit];
343
    } else if (mode == SINGLE_REFERENCE) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
344
      const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
Yaowu Xu's avatar
Yaowu Xu committed
345
      const int bit0 = vpx_read(r, fc->single_ref_prob[ctx0][0]);
Scott LaVarnway's avatar
Scott LaVarnway committed
346
      if (counts)
347
        ++counts->single_ref[ctx0][0][bit0];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
348 349
      if (bit0) {
        const int ctx1 = vp9_get_pred_context_single_ref_p2(xd);
Yaowu Xu's avatar
Yaowu Xu committed
350
        const int bit1 = vpx_read(r, fc->single_ref_prob[ctx1][1]);
Scott LaVarnway's avatar
Scott LaVarnway committed
351
        if (counts)
352
          ++counts->single_ref[ctx1][1][bit1];
353
        ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
354 355
      } else {
        ref_frame[0] = LAST_FRAME;
John Koleszar's avatar
John Koleszar committed
356
      }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
357 358

      ref_frame[1] = NONE;
359
    } else {
James Zern's avatar
James Zern committed
360
      assert(0 && "Invalid prediction mode.");
361
    }
John Koleszar's avatar
John Koleszar committed
362
  }
363
}
John Koleszar's avatar
John Koleszar committed
364

365

366
static INLINE INTERP_FILTER read_switchable_interp_filter(
367
    VP9_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xu's avatar
Yaowu Xu committed
368
    vpx_reader *r) {
369
  const int ctx = vp9_get_pred_context_switchable_interp(xd);
370
  const INTERP_FILTER type =
Yaowu Xu's avatar
Yaowu Xu committed
371
      (INTERP_FILTER)vpx_read_tree(r, vp9_switchable_interp_tree,
372
                                   cm->fc->switchable_interp_prob[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
373 374
  FRAME_COUNTS *counts = xd->counts;
  if (counts)
375
    ++counts->switchable_interp[ctx][type];
376
  return type;
377 378
}

379
static void read_intra_block_mode_info(VP9_COMMON *const cm,
Scott LaVarnway's avatar
Scott LaVarnway committed
380
                                       MACROBLOCKD *const xd, MODE_INFO *mi,
Yaowu Xu's avatar
Yaowu Xu committed
381
                                       vpx_reader *r) {
382
  MB_MODE_INFO *const mbmi = &mi->mbmi;
383
  const BLOCK_SIZE bsize = mi->mbmi.sb_type;
384
  int i;
385

Dmitry Kovalev's avatar
Dmitry Kovalev committed
386 387 388
  mbmi->ref_frame[0] = INTRA_FRAME;
  mbmi->ref_frame[1] = NONE;

389 390 391
  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
Scott LaVarnway's avatar
Scott LaVarnway committed
392
        mi->bmi[i].as_mode = read_intra_mode_y(cm, xd, r, 0);
393 394 395
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
Scott LaVarnway's avatar
Scott LaVarnway committed
396
      mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd,
397
                                                                  r, 0);
398
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Scott LaVarnway's avatar
Scott LaVarnway committed
399
          read_intra_mode_y(cm, xd, r, 0);
400 401
      break;
    case BLOCK_8X4:
Scott LaVarnway's avatar
Scott LaVarnway committed
402
      mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd,
403
                                                                  r, 0);
404
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
Scott LaVarnway's avatar
Scott LaVarnway committed
405
          read_intra_mode_y(cm, xd, r, 0);
406 407
      break;
    default:
Scott LaVarnway's avatar
Scott LaVarnway committed
408
      mbmi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
409 410
  }

Scott LaVarnway's avatar
Scott LaVarnway committed
411
  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
412 413
}

414 415 416 417 418
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
419
static INLINE int assign_mv(VP9_COMMON *cm, MACROBLOCKD *xd,
420
                            PREDICTION_MODE mode,
421 422
                            int_mv mv[2], int_mv ref_mv[2],
                            int_mv nearest_mv[2], int_mv near_mv[2],
Yaowu Xu's avatar
Yaowu Xu committed
423
                            int is_compound, int allow_hp, vpx_reader *r) {
424
  int i;
Yaowu Xu's avatar
Yaowu Xu committed
425
  int ret = 1;
426 427

  switch (mode) {
428
    case NEWMV: {
Scott LaVarnway's avatar
Scott LaVarnway committed
429 430
      FRAME_COUNTS *counts = xd->counts;
      nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
431
      for (i = 0; i < 1 + is_compound; ++i) {
432
        read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
433 434
                allow_hp);
        ret = ret && is_mv_valid(&mv[i].as_mv);
435 436 437 438 439
      }
      break;
    }
    case NEARESTMV: {
      mv[0].as_int = nearest_mv[0].as_int;
440 441
      if (is_compound)
        mv[1].as_int = nearest_mv[1].as_int;
442
      break;
443 444
    }
    case NEARMV: {
445
      mv[0].as_int = near_mv[0].as_int;
446 447
      if (is_compound)
        mv[1].as_int = near_mv[1].as_int;
448
      break;
449 450
    }
    case ZEROMV: {
451
      mv[0].as_int = 0;
452 453
      if (is_compound)
        mv[1].as_int = 0;
454
      break;
455 456
    }
    default: {
Yaowu Xu's avatar
Yaowu Xu committed
457
      return 0;
458
    }
459
  }
Yaowu Xu's avatar
Yaowu Xu committed
460
  return ret;
461 462
}

463
static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
Yaowu Xu's avatar
Yaowu Xu committed
464
                               int segment_id, vpx_reader *r) {
465
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
Scott LaVarnway's avatar
Scott LaVarnway committed
466
    return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
467
  } else {
468
    const int ctx = vp9_get_intra_inter_context(xd);
Yaowu Xu's avatar
Yaowu Xu committed
469
    const int is_inter = vpx_read(r, cm->fc->intra_inter_prob[ctx]);
Scott LaVarnway's avatar
Scott LaVarnway committed
470 471
    FRAME_COUNTS *counts = xd->counts;
    if (counts)
472
      ++counts->intra_inter[ctx][is_inter];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
473
    return is_inter;
474 475 476
  }
}

477 478
static void fpm_sync(void *const data, int mi_row) {
  VP9Decoder *const pbi = (VP9Decoder *)data;
479
  vp9_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
480 481 482 483
                       mi_row << MI_BLOCK_SIZE_LOG2);
}

static void read_inter_block_mode_info(VP9Decoder *const pbi,
484 485
                                       MACROBLOCKD *const xd,
                                       MODE_INFO *const mi,
Yaowu Xu's avatar
Yaowu Xu committed
486
                                       int mi_row, int mi_col, vpx_reader *r) {
487
  VP9_COMMON *const cm = &pbi->common;
488
  MB_MODE_INFO *const mbmi = &mi->mbmi;
489
  const BLOCK_SIZE bsize = mbmi->sb_type;
490
  const int allow_hp = cm->allow_high_precision_mv;
491
  int_mv nearestmv[2], nearmv[2];
492 493 494
  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
495

Scott LaVarnway's avatar
Scott LaVarnway committed
496
  read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
497
  is_compound = has_second_ref(mbmi);
498

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

503 504
    xd->block_refs[ref] = ref_buf;
    if ((!vp9_is_valid_scale(&ref_buf->sf)))
505
      vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
506
                         "Reference frame has invalid dimensions");
507 508
    vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
                         &ref_buf->sf);
Scott LaVarnway's avatar
Scott LaVarnway committed
509
    vp9_find_mv_refs(cm, xd, mi, frame, ref_mvs[frame],
510
                     mi_row, mi_col, fpm_sync, (void *)pbi, inter_mode_ctx);
511
  }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
512

513
  if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
514
    mbmi->mode = ZEROMV;
515
    if (bsize < BLOCK_8X8) {
516
        vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
517 518 519
                           "Invalid usage of segement feature on small blocks");
        return;
    }
520 521
  } else {
    if (bsize >= BLOCK_8X8)
522 523
      mbmi->mode = read_inter_mode(cm, xd, r,
                                   inter_mode_ctx[mbmi->ref_frame[0]]);
524
  }
525

526
  if (bsize < BLOCK_8X8 || mbmi->mode != ZEROMV) {
527
    for (ref = 0; ref < 1 + is_compound; ++ref) {
528
      vp9_find_best_ref_mvs(xd, allow_hp, ref_mvs[mbmi->ref_frame[ref]],
529
                            &nearestmv[ref], &nearmv[ref]);
530
    }
531
  }
532

533
  mbmi->interp_filter = (cm->interp_filter == SWITCHABLE)
Scott LaVarnway's avatar
Scott LaVarnway committed
534
                      ? read_switchable_interp_filter(cm, xd, r)
535
                      : cm->interp_filter;
536

537
  if (bsize < BLOCK_8X8) {
538 539
    const int num_4x4_w = 1 << xd->bmode_blocks_wl;
    const int num_4x4_h = 1 << xd->bmode_blocks_hl;
540
    int idx, idy;
541
    PREDICTION_MODE b_mode;
542
    int_mv nearest_sub8x8[2], near_sub8x8[2];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
543 544
    for (idy = 0; idy < 2; idy += num_4x4_h) {
      for (idx = 0; idx < 2; idx += num_4x4_w) {
545
        int_mv block[2];
546
        const int j = idy * 2 + idx;
547
        b_mode = read_inter_mode(cm, xd, r, inter_mode_ctx[mbmi->ref_frame[0]]);
548

549 550
        if (b_mode == NEARESTMV || b_mode == NEARMV) {
          uint8_t dummy_mode_ctx[MAX_REF_FRAMES];
551
          for (ref = 0; ref < 1 + is_compound; ++ref)
Scott LaVarnway's avatar
Scott LaVarnway committed
552
            vp9_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
553
                                          &nearest_sub8x8[ref],
554 555 556
                                          &near_sub8x8[ref],
                                          dummy_mode_ctx);
        }
557

Scott LaVarnway's avatar
Scott LaVarnway committed
558
        if (!assign_mv(cm, xd, b_mode, block, nearestmv,
559
                       nearest_sub8x8, near_sub8x8,
Yaowu Xu's avatar
Yaowu Xu committed
560 561 562
                       is_compound, allow_hp, r)) {
          xd->corrupted |= 1;
          break;
Jingning Han's avatar
Jingning Han committed
563
        }
Yaowu Xu's avatar
Yaowu Xu committed
564

565 566
        mi->bmi[j].as_mv[0].as_int = block[0].as_int;
        if (is_compound)
567
          mi->bmi[j].as_mv[1].as_int = block[1].as_int;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
568

Dmitry Kovalev's avatar
Dmitry Kovalev committed
569
        if (num_4x4_h == 2)
570
          mi->bmi[j + 2] = mi->bmi[j];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
571
        if (num_4x4_w == 2)
572
          mi->bmi[j + 1] = mi->bmi[j];
Ronald S. Bultje's avatar
Ronald S. Bultje committed
573
      }
Scott LaVarnway's avatar
Scott LaVarnway committed
574
    }
575

576
    mi->mbmi.mode = b_mode;
577

578 579 580
    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
581
    xd->corrupted |= !assign_mv(cm, xd, mbmi->mode, mbmi->mv, nearestmv,
582
                                nearestmv, nearmv, is_compound, allow_hp, r);
John Koleszar's avatar
John Koleszar committed
583
  }
Scott LaVarnway's avatar
Scott LaVarnway committed
584
}
John Koleszar's avatar
John Koleszar committed
585

586
static void read_inter_frame_mode_info(VP9Decoder *const pbi,
587
                                       MACROBLOCKD *const xd,
Yaowu Xu's avatar
Yaowu Xu committed
588
                                       int mi_row, int mi_col, vpx_reader *r) {
589
  VP9_COMMON *const cm = &pbi->common;
590
  MODE_INFO *const mi = xd->mi[0];
591
  MB_MODE_INFO *const mbmi = &mi->mbmi;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
592
  int inter_block;
593

Dmitry Kovalev's avatar
Dmitry Kovalev committed
594 595
  mbmi->mv[0].as_int = 0;
  mbmi->mv[1].as_int = 0;
596
  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
Scott LaVarnway's avatar
Scott LaVarnway committed
597 598 599
  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);
600

Dmitry Kovalev's avatar
Dmitry Kovalev committed
601
  if (inter_block)
Scott LaVarnway's avatar
Scott LaVarnway committed
602
    read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
603
  else
Scott LaVarnway's avatar
Scott LaVarnway committed
604
    read_intra_block_mode_info(cm, xd, mi, r);
605 606
}

Yaowu Xu's avatar
Yaowu Xu committed
607 608
void vpx_read_mode_info(VP9Decoder *const pbi, MACROBLOCKD *xd,
                        int mi_row, int mi_col, vpx_reader *r,
609
                        int x_mis, int y_mis) {
610
  VP9_COMMON *const cm = &pbi->common;
611
  MODE_INFO *const mi = xd->mi[0];
612 613 614
  MV_REF* frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
  int w, h;

615
  if (frame_is_intra_only(cm)) {
Scott LaVarnway's avatar
Scott LaVarnway committed
616
    read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
617
  } else {
Scott LaVarnway's avatar
Scott LaVarnway committed
618
    read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r);
619

620 621 622 623 624 625 626 627 628
    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;
      }
629 630
    }
  }
631
}