decodemv.c 28.8 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10 11 12 13
 */

#include <assert.h>

Yaowu Xu's avatar
Yaowu Xu committed
14 15 16 17 18 19 20 21
#include "av1/common/common.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/entropymv.h"
#include "av1/common/mvref_common.h"
#include "av1/common/pred_common.h"
#include "av1/common/reconinter.h"
#include "av1/common/seg_common.h"
Jingning Han's avatar
Jingning Han committed
22

Yaowu Xu's avatar
Yaowu Xu committed
23 24
#include "av1/decoder/decodemv.h"
#include "av1/decoder/decodeframe.h"
Jingning Han's avatar
Jingning Han committed
25

Adrian Grange's avatar
Adrian Grange committed
26
#include "aom_dsp/aom_dsp_common.h"
27

Adrian Grange's avatar
Adrian Grange committed
28
static PREDICTION_MODE read_intra_mode(aom_reader *r, const aom_prob *p) {
29
  return (PREDICTION_MODE)aom_read_tree(r, av1_intra_mode_tree, p);
Jingning Han's avatar
Jingning Han committed
30 31
}

32
static PREDICTION_MODE read_intra_mode_y(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
33
                                         aom_reader *r, int size_group) {
Jingning Han's avatar
Jingning Han committed
34 35 36
  const PREDICTION_MODE y_mode =
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
  FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
37
  if (counts) ++counts->y_mode[size_group][y_mode];
Jingning Han's avatar
Jingning Han committed
38 39 40
  return y_mode;
}

41
static PREDICTION_MODE read_intra_mode_uv(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
42
                                          aom_reader *r,
Jingning Han's avatar
Jingning Han committed
43
                                          PREDICTION_MODE y_mode) {
clang-format's avatar
clang-format committed
44 45
  const PREDICTION_MODE uv_mode =
      read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
Jingning Han's avatar
Jingning Han committed
46
  FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
47
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
Jingning Han's avatar
Jingning Han committed
48 49 50
  return uv_mode;
}

51
static PREDICTION_MODE read_inter_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
52
                                       aom_reader *r, int16_t ctx) {
53 54
#if CONFIG_REF_MV
  FRAME_COUNTS *counts = xd->counts;
55
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
56 57 58 59 60 61 62 63 64 65
  aom_prob mode_prob = cm->fc->newmv_prob[mode_ctx];

  if (aom_read(r, mode_prob) == 0) {
    if (counts)
      ++counts->newmv_mode[mode_ctx][0];
    return NEWMV;
  }
  if (counts)
    ++counts->newmv_mode[mode_ctx][1];

66 67
  if (ctx & (1 << ALL_ZERO_FLAG_OFFSET))
    return ZEROMV;
68

69
  mode_ctx = (ctx >> ZEROMV_OFFSET) & ZEROMV_CTX_MASK;
70 71 72 73 74 75 76 77 78 79

  mode_prob = cm->fc->zeromv_prob[mode_ctx];
  if (aom_read(r, mode_prob) == 0) {
    if (counts)
      ++counts->zeromv_mode[mode_ctx][0];
    return ZEROMV;
  }
  if (counts)
    ++counts->zeromv_mode[mode_ctx][1];

80
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
81 82 83 84 85 86 87 88

  if (ctx & (1 << SKIP_NEARESTMV_OFFSET))
    mode_ctx = 6;
  if (ctx & (1 << SKIP_NEARMV_OFFSET))
    mode_ctx = 7;
  if (ctx & (1 << SKIP_NEARESTMV_SUB8X8_OFFSET))
    mode_ctx = 8;

89 90 91 92 93 94 95 96 97 98 99 100 101 102
  mode_prob = cm->fc->refmv_prob[mode_ctx];
  if (aom_read(r, mode_prob) == 0) {
    if (counts)
      ++counts->refmv_mode[mode_ctx][0];
    return NEARESTMV;
  } else {
    if (counts)
      ++counts->refmv_mode[mode_ctx][1];
    return NEARMV;
  }

  // Invalid prediction mode.
  assert(0);
#else
clang-format's avatar
clang-format committed
103
  const int mode =
104
      aom_read_tree(r, av1_inter_mode_tree, cm->fc->inter_mode_probs[ctx]);
Jingning Han's avatar
Jingning Han committed
105
  FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
106
  if (counts) ++counts->inter_mode[ctx][mode];
Jingning Han's avatar
Jingning Han committed
107 108

  return NEARESTMV + mode;
109
#endif
Jingning Han's avatar
Jingning Han committed
110 111
}

112 113 114 115 116 117 118 119 120
#if CONFIG_REF_MV
static void read_drl_idx(const AV1_COMMON *cm,
                         MACROBLOCKD *xd,
                         MB_MODE_INFO *mbmi,
                         aom_reader *r) {
  uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
  mbmi->ref_mv_idx = 0;

  if (xd->ref_mv_count[ref_frame_type] > 2) {
Jingning Han's avatar
Jingning Han committed
121
    uint8_t drl0_ctx = av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], 1);
122 123 124 125 126 127 128
    aom_prob drl0_prob = cm->fc->drl_prob0[drl0_ctx];
    if (aom_read(r, drl0_prob)) {
      mbmi->ref_mv_idx = 1;
      if (xd->counts)
        ++xd->counts->drl_mode0[drl0_ctx][1];
      if (xd->ref_mv_count[ref_frame_type] > 3) {
        uint8_t drl1_ctx =
Jingning Han's avatar
Jingning Han committed
129
            av1_drl_ctx(xd->ref_mv_stack[ref_frame_type], 2);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        aom_prob drl1_prob = cm->fc->drl_prob1[drl1_ctx];
        if (aom_read(r, drl1_prob)) {
          mbmi->ref_mv_idx = 2;
          if (xd->counts)
            ++xd->counts->drl_mode1[drl1_ctx][1];

          return;
        }

        if (xd->counts)
          ++xd->counts->drl_mode1[drl1_ctx][0];
      }
      return;
    }

    if (xd->counts)
      ++xd->counts->drl_mode0[drl0_ctx][0];
  }
}
#endif

Adrian Grange's avatar
Adrian Grange committed
151
static int read_segment_id(aom_reader *r,
clang-format's avatar
clang-format committed
152
                           const struct segmentation_probs *segp) {
153
  return aom_read_tree(r, av1_segment_tree, segp->tree_probs);
Jingning Han's avatar
Jingning Han committed
154 155
}

156
static TX_SIZE read_selected_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
157
                                     TX_SIZE max_tx_size, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
158 159
  FRAME_COUNTS *counts = xd->counts;
  const int ctx = get_tx_size_context(xd);
Adrian Grange's avatar
Adrian Grange committed
160 161
  const aom_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
  int tx_size = aom_read(r, tx_probs[0]);
Jingning Han's avatar
Jingning Han committed
162
  if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
Adrian Grange's avatar
Adrian Grange committed
163
    tx_size += aom_read(r, tx_probs[1]);
Jingning Han's avatar
Jingning Han committed
164
    if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
Adrian Grange's avatar
Adrian Grange committed
165
      tx_size += aom_read(r, tx_probs[2]);
Jingning Han's avatar
Jingning Han committed
166 167
  }

clang-format's avatar
clang-format committed
168
  if (counts) ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
Jingning Han's avatar
Jingning Han committed
169 170 171
  return (TX_SIZE)tx_size;
}

172
static TX_SIZE read_tx_size(AV1_COMMON *cm, MACROBLOCKD *xd, int allow_select,
Adrian Grange's avatar
Adrian Grange committed
173
                            aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
174 175 176
  TX_MODE tx_mode = cm->tx_mode;
  BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type;
  const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
clang-format's avatar
clang-format committed
177
  if (xd->lossless[xd->mi[0]->mbmi.segment_id]) return TX_4X4;
Jingning Han's avatar
Jingning Han committed
178 179 180
  if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
    return read_selected_tx_size(cm, xd, max_tx_size, r);
  else
Adrian Grange's avatar
Adrian Grange committed
181
    return AOMMIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
Jingning Han's avatar
Jingning Han committed
182 183
}

184
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
Jingning Han's avatar
Jingning Han committed
185 186 187 188 189
                              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++)
190
      segment_id =
Adrian Grange's avatar
Adrian Grange committed
191
          AOMMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
Jingning Han's avatar
Jingning Han committed
192 193 194 195 196

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

197
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
clang-format's avatar
clang-format committed
198
                           int segment_id) {
Jingning Han's avatar
Jingning Han committed
199 200 201 202 203 204 205 206 207
  int x, y;

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

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
      cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
}

208
static int read_intra_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
209
                                 int mi_offset, int x_mis, int y_mis,
Adrian Grange's avatar
Adrian Grange committed
210
                                 aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
211
  struct segmentation *const seg = &cm->seg;
212 213 214 215 216 217
#if CONFIG_MISC_FIXES
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
#else
  struct segmentation_probs *const segp = &cm->segp;
#endif
Jingning Han's avatar
Jingning Han committed
218 219
  int segment_id;

220
#if !CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
221
  (void)xd;
222 223
#endif

clang-format's avatar
clang-format committed
224
  if (!seg->enabled) return 0;  // Default for disabled segmentation
Jingning Han's avatar
Jingning Han committed
225

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

228 229
  segment_id = read_segment_id(r, segp);
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
230
  if (counts) ++counts->seg.tree_total[segment_id];
231
#endif
Jingning Han's avatar
Jingning Han committed
232 233 234 235
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

236
static void copy_segment_id(const AV1_COMMON *cm,
clang-format's avatar
clang-format committed
237 238 239
                            const uint8_t *last_segment_ids,
                            uint8_t *current_segment_ids, int mi_offset,
                            int x_mis, int y_mis) {
240 241 242 243
  int x, y;

  for (y = 0; y < y_mis; y++)
    for (x = 0; x < x_mis; x++)
clang-format's avatar
clang-format committed
244 245 246
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
                           : 0;
247 248
}

249
static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
250
                                 int mi_row, int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
251
  struct segmentation *const seg = &cm->seg;
252 253 254 255 256 257
#if CONFIG_MISC_FIXES
  FRAME_COUNTS *counts = xd->counts;
  struct segmentation_probs *const segp = &cm->fc->seg;
#else
  struct segmentation_probs *const segp = &cm->segp;
#endif
Jingning Han's avatar
Jingning Han committed
258 259 260 261 262 263 264
  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
  int predicted_segment_id, segment_id;
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
  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 ?????
Adrian Grange's avatar
Adrian Grange committed
265 266
  const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
  const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Jingning Han's avatar
Jingning Han committed
267

clang-format's avatar
clang-format committed
268
  if (!seg->enabled) return 0;  // Default for disabled segmentation
Jingning Han's avatar
Jingning Han committed
269

clang-format's avatar
clang-format committed
270 271 272 273
  predicted_segment_id = cm->last_frame_seg_map
                             ? dec_get_segment_id(cm, cm->last_frame_seg_map,
                                                  mi_offset, x_mis, y_mis)
                             : 0;
Jingning Han's avatar
Jingning Han committed
274 275 276 277 278 279 280 281

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

  if (seg->temporal_update) {
282
    const int ctx = av1_get_pred_context_seg_id(xd);
Adrian Grange's avatar
Adrian Grange committed
283 284
    const aom_prob pred_prob = segp->pred_probs[ctx];
    mbmi->seg_id_predicted = aom_read(r, pred_prob);
285
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
286
    if (counts) ++counts->seg.pred[ctx][mbmi->seg_id_predicted];
287 288 289 290 291 292
#endif
    if (mbmi->seg_id_predicted) {
      segment_id = predicted_segment_id;
    } else {
      segment_id = read_segment_id(r, segp);
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
293
      if (counts) ++counts->seg.tree_mispred[segment_id];
294 295
#endif
    }
Jingning Han's avatar
Jingning Han committed
296
  } else {
297 298
    segment_id = read_segment_id(r, segp);
#if CONFIG_MISC_FIXES
clang-format's avatar
clang-format committed
299
    if (counts) ++counts->seg.tree_total[segment_id];
300
#endif
Jingning Han's avatar
Jingning Han committed
301 302 303 304 305
  }
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
  return segment_id;
}

306
static int read_skip(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
Adrian Grange's avatar
Adrian Grange committed
307
                     aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
308 309 310
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
    return 1;
  } else {
311
    const int ctx = av1_get_skip_context(xd);
Adrian Grange's avatar
Adrian Grange committed
312
    const int skip = aom_read(r, cm->fc->skip_probs[ctx]);
Jingning Han's avatar
Jingning Han committed
313
    FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
314
    if (counts) ++counts->skip[ctx][skip];
Jingning Han's avatar
Jingning Han committed
315 316 317 318
    return skip;
  }
}

319
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
clang-format's avatar
clang-format committed
320
                                       MACROBLOCKD *const xd, int mi_row,
Adrian Grange's avatar
Adrian Grange committed
321
                                       int mi_col, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
322 323 324
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const MODE_INFO *above_mi = xd->above_mi;
clang-format's avatar
clang-format committed
325
  const MODE_INFO *left_mi = xd->left_mi;
Jingning Han's avatar
Jingning Han committed
326 327 328 329 330 331 332
  const BLOCK_SIZE bsize = mbmi->sb_type;
  int i;
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
  const int bw = xd->plane[0].n4_w >> 1;
  const int bh = xd->plane[0].n4_h >> 1;

  // TODO(slavarnway): move x_mis, y_mis into xd ?????
Adrian Grange's avatar
Adrian Grange committed
333 334
  const int x_mis = AOMMIN(cm->mi_cols - mi_col, bw);
  const int y_mis = AOMMIN(cm->mi_rows - mi_row, bh);
Jingning Han's avatar
Jingning Han committed
335

336
  mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
Jingning Han's avatar
Jingning Han committed
337 338 339 340 341 342 343 344 345
  mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
  mbmi->tx_size = read_tx_size(cm, xd, 1, r);
  mbmi->ref_frame[0] = INTRA_FRAME;
  mbmi->ref_frame[1] = NONE;

  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
        mi->bmi[i].as_mode =
346
            read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, i));
Jingning Han's avatar
Jingning Han committed
347 348 349 350
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
      mi->bmi[0].as_mode = mi->bmi[2].as_mode =
351
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
Jingning Han's avatar
Jingning Han committed
352
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
353
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 1));
Jingning Han's avatar
Jingning Han committed
354 355 356
      break;
    case BLOCK_8X4:
      mi->bmi[0].as_mode = mi->bmi[1].as_mode =
357
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
Jingning Han's avatar
Jingning Han committed
358
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
359
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 2));
Jingning Han's avatar
Jingning Han committed
360 361
      break;
    default:
clang-format's avatar
clang-format committed
362 363
      mbmi->mode =
          read_intra_mode(r, get_y_mode_probs(cm, mi, above_mi, left_mi, 0));
Jingning Han's avatar
Jingning Han committed
364 365
  }

366
  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
367

clang-format's avatar
clang-format committed
368
  if (mbmi->tx_size < TX_32X32 && cm->base_qindex > 0 && !mbmi->skip &&
369 370 371
      !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
    FRAME_COUNTS *counts = xd->counts;
    TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
clang-format's avatar
clang-format committed
372
    mbmi->tx_type =
373
        aom_read_tree(r, av1_ext_tx_tree,
clang-format's avatar
clang-format committed
374
                      cm->fc->intra_ext_tx_prob[mbmi->tx_size][tx_type_nom]);
375 376 377 378 379
    if (counts)
      ++counts->intra_ext_tx[mbmi->tx_size][tx_type_nom][mbmi->tx_type];
  } else {
    mbmi->tx_type = DCT_DCT;
  }
Jingning Han's avatar
Jingning Han committed
380 381
}

Adrian Grange's avatar
Adrian Grange committed
382
static int read_mv_component(aom_reader *r, const nmv_component *mvcomp,
clang-format's avatar
clang-format committed
383
                             int usehp) {
Jingning Han's avatar
Jingning Han committed
384
  int mag, d, fr, hp;
Adrian Grange's avatar
Adrian Grange committed
385
  const int sign = aom_read(r, mvcomp->sign);
386
  const int mv_class = aom_read_tree(r, av1_mv_class_tree, mvcomp->classes);
Jingning Han's avatar
Jingning Han committed
387 388 389 390
  const int class0 = mv_class == MV_CLASS_0;

  // Integer part
  if (class0) {
391
    d = aom_read_tree(r, av1_mv_class0_tree, mvcomp->class0);
Jingning Han's avatar
Jingning Han committed
392 393 394 395 396 397
    mag = 0;
  } else {
    int i;
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits

    d = 0;
Adrian Grange's avatar
Adrian Grange committed
398
    for (i = 0; i < n; ++i) d |= aom_read(r, mvcomp->bits[i]) << i;
Jingning Han's avatar
Jingning Han committed
399 400 401 402
    mag = CLASS0_SIZE << (mv_class + 2);
  }

  // Fractional part
403
  fr = aom_read_tree(r, av1_mv_fp_tree,
clang-format's avatar
clang-format committed
404
                     class0 ? mvcomp->class0_fp[d] : mvcomp->fp);
Jingning Han's avatar
Jingning Han committed
405 406

  // High precision part (if hp is not used, the default value of the hp is 1)
Adrian Grange's avatar
Adrian Grange committed
407
  hp = usehp ? aom_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp) : 1;
Jingning Han's avatar
Jingning Han committed
408 409 410 411 412 413

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

Adrian Grange's avatar
Adrian Grange committed
414
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
clang-format's avatar
clang-format committed
415 416
                           const nmv_context *ctx, nmv_context_counts *counts,
                           int allow_hp) {
Jingning Han's avatar
Jingning Han committed
417
  const MV_JOINT_TYPE joint_type =
418 419
      (MV_JOINT_TYPE)aom_read_tree(r, av1_mv_joint_tree, ctx->joints);
  const int use_hp = allow_hp && av1_use_mv_hp(ref);
clang-format's avatar
clang-format committed
420
  MV diff = { 0, 0 };
Jingning Han's avatar
Jingning Han committed
421 422 423 424 425 426 427

  if (mv_joint_vertical(joint_type))
    diff.row = read_mv_component(r, &ctx->comps[0], use_hp);

  if (mv_joint_horizontal(joint_type))
    diff.col = read_mv_component(r, &ctx->comps[1], use_hp);

428
  av1_inc_mv(&diff, counts, use_hp);
Jingning Han's avatar
Jingning Han committed
429 430 431 432 433

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

434
static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
Jingning Han's avatar
Jingning Han committed
435
                                                const MACROBLOCKD *xd,
Adrian Grange's avatar
Adrian Grange committed
436
                                                aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
437
  if (cm->reference_mode == REFERENCE_MODE_SELECT) {
438
    const int ctx = av1_get_reference_mode_context(cm, xd);
Jingning Han's avatar
Jingning Han committed
439
    const REFERENCE_MODE mode =
Adrian Grange's avatar
Adrian Grange committed
440
        (REFERENCE_MODE)aom_read(r, cm->fc->comp_inter_prob[ctx]);
Jingning Han's avatar
Jingning Han committed
441
    FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
442
    if (counts) ++counts->comp_inter[ctx][mode];
Jingning Han's avatar
Jingning Han committed
443 444 445 446 447 448 449
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
  } else {
    return cm->reference_mode;
  }
}

// Read the referncence frame
450
static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
451
                            aom_reader *r, int segment_id,
clang-format's avatar
clang-format committed
452
                            MV_REFERENCE_FRAME ref_frame[2]) {
Jingning Han's avatar
Jingning Han committed
453 454 455 456 457 458 459 460 461 462 463 464
  FRAME_CONTEXT *const fc = cm->fc;
  FRAME_COUNTS *counts = xd->counts;

  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
    ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
                                                   SEG_LVL_REF_FRAME);
    ref_frame[1] = NONE;
  } else {
    const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
    // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
    if (mode == COMPOUND_REFERENCE) {
      const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
465
      const int ctx = av1_get_pred_context_comp_ref_p(cm, xd);
Adrian Grange's avatar
Adrian Grange committed
466
      const int bit = aom_read(r, fc->comp_ref_prob[ctx]);
clang-format's avatar
clang-format committed
467
      if (counts) ++counts->comp_ref[ctx][bit];
Jingning Han's avatar
Jingning Han committed
468 469 470
      ref_frame[idx] = cm->comp_fixed_ref;
      ref_frame[!idx] = cm->comp_var_ref[bit];
    } else if (mode == SINGLE_REFERENCE) {
471
      const int ctx0 = av1_get_pred_context_single_ref_p1(xd);
Adrian Grange's avatar
Adrian Grange committed
472
      const int bit0 = aom_read(r, fc->single_ref_prob[ctx0][0]);
clang-format's avatar
clang-format committed
473
      if (counts) ++counts->single_ref[ctx0][0][bit0];
Jingning Han's avatar
Jingning Han committed
474
      if (bit0) {
475
        const int ctx1 = av1_get_pred_context_single_ref_p2(xd);
Adrian Grange's avatar
Adrian Grange committed
476
        const int bit1 = aom_read(r, fc->single_ref_prob[ctx1][1]);
clang-format's avatar
clang-format committed
477
        if (counts) ++counts->single_ref[ctx1][1][bit1];
Jingning Han's avatar
Jingning Han committed
478 479 480 481 482 483 484 485 486 487 488 489
        ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
      } else {
        ref_frame[0] = LAST_FRAME;
      }

      ref_frame[1] = NONE;
    } else {
      assert(0 && "Invalid prediction mode.");
    }
  }
}

490
static INLINE INTERP_FILTER read_switchable_interp_filter(AV1_COMMON *const cm,
clang-format's avatar
clang-format committed
491
                                                          MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
492
                                                          aom_reader *r) {
493
  const int ctx = av1_get_pred_context_switchable_interp(xd);
Adrian Grange's avatar
Adrian Grange committed
494
  const INTERP_FILTER type = (INTERP_FILTER)aom_read_tree(
495
      r, av1_switchable_interp_tree, cm->fc->switchable_interp_prob[ctx]);
Jingning Han's avatar
Jingning Han committed
496
  FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
497
  if (counts) ++counts->switchable_interp[ctx][type];
Jingning Han's avatar
Jingning Han committed
498 499 500
  return type;
}

501
static void read_intra_block_mode_info(AV1_COMMON *const cm,
Jingning Han's avatar
Jingning Han committed
502
                                       MACROBLOCKD *const xd, MODE_INFO *mi,
Adrian Grange's avatar
Adrian Grange committed
503
                                       aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const BLOCK_SIZE bsize = mi->mbmi.sb_type;
  int i;

  mbmi->ref_frame[0] = INTRA_FRAME;
  mbmi->ref_frame[1] = NONE;

  switch (bsize) {
    case BLOCK_4X4:
      for (i = 0; i < 4; ++i)
        mi->bmi[i].as_mode = read_intra_mode_y(cm, xd, r, 0);
      mbmi->mode = mi->bmi[3].as_mode;
      break;
    case BLOCK_4X8:
clang-format's avatar
clang-format committed
518
      mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd, r, 0);
Jingning Han's avatar
Jingning Han committed
519 520 521 522
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mbmi->mode =
          read_intra_mode_y(cm, xd, r, 0);
      break;
    case BLOCK_8X4:
clang-format's avatar
clang-format committed
523
      mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd, r, 0);
Jingning Han's avatar
Jingning Han committed
524 525 526 527 528 529 530 531 532 533 534
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mbmi->mode =
          read_intra_mode_y(cm, xd, r, 0);
      break;
    default:
      mbmi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
  }

  mbmi->uv_mode = read_intra_mode_uv(cm, xd, r, mbmi->mode);
}

static INLINE int is_mv_valid(const MV *mv) {
clang-format's avatar
clang-format committed
535 536
  return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
         mv->col < MV_UPP;
Jingning Han's avatar
Jingning Han committed
537 538
}

539
static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
540
                            PREDICTION_MODE mode, int block, int_mv mv[2],
clang-format's avatar
clang-format committed
541 542
                            int_mv ref_mv[2], int_mv nearest_mv[2],
                            int_mv near_mv[2], int is_compound, int allow_hp,
Adrian Grange's avatar
Adrian Grange committed
543
                            aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
544 545 546
  int i;
  int ret = 1;

547 548 549 550 551 552 553 554 555
#if CONFIG_REF_MV
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
  BLOCK_SIZE bsize = mbmi->sb_type;
  int_mv *pred_mv = (bsize >= BLOCK_8X8) ?
      mbmi->pred_mv : xd->mi[0]->bmi[block].pred_mv;
#else
  (void)block;
#endif

Jingning Han's avatar
Jingning Han committed
556 557 558 559 560 561 562 563
  switch (mode) {
    case NEWMV: {
      FRAME_COUNTS *counts = xd->counts;
      nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
      for (i = 0; i < 1 + is_compound; ++i) {
        read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
                allow_hp);
        ret = ret && is_mv_valid(&mv[i].as_mv);
564 565 566
#if CONFIG_REF_MV
        pred_mv[i].as_int = ref_mv[i].as_int;
#endif
Jingning Han's avatar
Jingning Han committed
567 568 569 570 571
      }
      break;
    }
    case NEARESTMV: {
      mv[0].as_int = nearest_mv[0].as_int;
clang-format's avatar
clang-format committed
572
      if (is_compound) mv[1].as_int = nearest_mv[1].as_int;
573 574 575 576 577
#if CONFIG_REF_MV
      pred_mv[0].as_int = nearest_mv[0].as_int;
      if (is_compound)
        pred_mv[1].as_int = nearest_mv[1].as_int;
#endif
Jingning Han's avatar
Jingning Han committed
578 579 580 581
      break;
    }
    case NEARMV: {
      mv[0].as_int = near_mv[0].as_int;
clang-format's avatar
clang-format committed
582
      if (is_compound) mv[1].as_int = near_mv[1].as_int;
583 584 585 586 587
#if CONFIG_REF_MV
      pred_mv[0].as_int = near_mv[0].as_int;
      if (is_compound)
        pred_mv[1].as_int = near_mv[1].as_int;
#endif
Jingning Han's avatar
Jingning Han committed
588 589 590 591
      break;
    }
    case ZEROMV: {
      mv[0].as_int = 0;
clang-format's avatar
clang-format committed
592
      if (is_compound) mv[1].as_int = 0;
593 594 595 596 597
#if CONFIG_REF_MV
      pred_mv[0].as_int = 0;
      if (is_compound)
        pred_mv[1].as_int = 0;
#endif
Jingning Han's avatar
Jingning Han committed
598 599
      break;
    }
clang-format's avatar
clang-format committed
600
    default: { return 0; }
Jingning Han's avatar
Jingning Han committed
601 602 603 604
  }
  return ret;
}

605
static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
Adrian Grange's avatar
Adrian Grange committed
606
                               int segment_id, aom_reader *r) {
Jingning Han's avatar
Jingning Han committed
607 608 609
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
    return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
  } else {
610
    const int ctx = av1_get_intra_inter_context(xd);
Adrian Grange's avatar
Adrian Grange committed
611
    const int is_inter = aom_read(r, cm->fc->intra_inter_prob[ctx]);
Jingning Han's avatar
Jingning Han committed
612
    FRAME_COUNTS *counts = xd->counts;
clang-format's avatar
clang-format committed
613
    if (counts) ++counts->intra_inter[ctx][is_inter];
Jingning Han's avatar
Jingning Han committed
614 615 616 617 618
    return is_inter;
  }
}

static void fpm_sync(void *const data, int mi_row) {
619 620
  AV1Decoder *const pbi = (AV1Decoder *)data;
  av1_frameworker_wait(pbi->frame_worker_owner, pbi->common.prev_frame,
clang-format's avatar
clang-format committed
621
                        mi_row << MI_BLOCK_SIZE_LOG2);
Jingning Han's avatar
Jingning Han committed
622 623
}

624
static void read_inter_block_mode_info(AV1Decoder *const pbi,
Jingning Han's avatar
Jingning Han committed
625
                                       MACROBLOCKD *const xd,
clang-format's avatar
clang-format committed
626
                                       MODE_INFO *const mi, int mi_row,
Adrian Grange's avatar
Adrian Grange committed
627
                                       int mi_col, aom_reader *r) {
628
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
629 630 631 632
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  const BLOCK_SIZE bsize = mbmi->sb_type;
  const int allow_hp = cm->allow_high_precision_mv;
  int_mv nearestmv[2], nearmv[2];
633
  int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
Jingning Han's avatar
Jingning Han committed
634
  int ref, is_compound;
635
  int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
636
  int16_t mode_ctx = 0;
637
  MV_REFERENCE_FRAME ref_frame;
Jingning Han's avatar
Jingning Han committed
638 639 640 641 642 643 644 645 646

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

  for (ref = 0; ref < 1 + is_compound; ++ref) {
    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
    RefBuffer *ref_buf = &cm->frame_refs[frame - LAST_FRAME];

    xd->block_refs[ref] = ref_buf;
647
    if ((!av1_is_valid_scale(&ref_buf->sf)))
Adrian Grange's avatar
Adrian Grange committed
648
      aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
Jingning Han's avatar
Jingning Han committed
649
                         "Reference frame has invalid dimensions");
650
    av1_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col, &ref_buf->sf);
651 652
  }

653
  for (ref_frame = LAST_FRAME; ref_frame < MODE_CTX_REF_FRAMES; ++ref_frame) {
654
    av1_find_mv_refs(cm, xd, mi, ref_frame,
655
#if CONFIG_REF_MV
656 657
                     &xd->ref_mv_count[ref_frame],
                     xd->ref_mv_stack[ref_frame],
658
#endif
659
                     ref_mvs[ref_frame], mi_row, mi_col,
660
                     fpm_sync, (void *)pbi, inter_mode_ctx);
Jingning Han's avatar
Jingning Han committed
661 662
  }

663
#if CONFIG_REF_MV
664 665
  mode_ctx = av1_mode_context_analyzer(inter_mode_ctx,
                                       mbmi->ref_frame, bsize, -1);
666
  mbmi->ref_mv_idx = 0;
667 668
#else
  mode_ctx = inter_mode_ctx[mbmi->ref_frame[0]];
669 670
#endif

Jingning Han's avatar
Jingning Han committed
671 672 673
  if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
    mbmi->mode = ZEROMV;
    if (bsize < BLOCK_8X8) {
Adrian Grange's avatar
Adrian Grange committed
674
      aom_internal_error(xd->error_info, AOM_CODEC_UNSUP_BITSTREAM,
clang-format's avatar
clang-format committed
675 676
                         "Invalid usage of segement feature on small blocks");
      return;
Jingning Han's avatar
Jingning Han committed
677 678
    }
  } else {
679 680 681
    if (bsize >= BLOCK_8X8) {
      mbmi->mode = read_inter_mode(cm, xd, r, mode_ctx);
#if CONFIG_REF_MV
682 683
      if (mbmi->mode == NEARMV)
        read_drl_idx(cm, xd, mbmi, r);
684 685
#endif
    }
Jingning Han's avatar
Jingning Han committed
686 687 688 689
  }

  if (bsize < BLOCK_8X8 || mbmi->mode != ZEROMV) {
    for (ref = 0; ref < 1 + is_compound; ++ref) {
690
      av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[ref]],
691
                             &nearestmv[ref], &nearmv[ref]);
Jingning Han's avatar
Jingning Han committed
692 693 694
    }
  }

695
#if CONFIG_REF_MV
696 697 698
  if (mbmi->ref_mv_idx > 0) {
    int_mv cur_mv =
        xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
699 700 701 702
    lower_mv_precision(&cur_mv.as_mv, cm->allow_high_precision_mv);
    nearmv[0] = cur_mv;
  }

703 704 705 706 707 708 709 710 711
  if (is_compound && bsize >= BLOCK_8X8 && mbmi->mode != NEWMV &&
      mbmi->mode != ZEROMV) {
    uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);

    if (xd->ref_mv_count[ref_frame_type] == 1 && mbmi->mode == NEARESTMV) {
      int i;
      nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
      nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;

712
      for (i = 0; i < 2; ++i)
713 714 715 716 717
        lower_mv_precision(&nearestmv[i].as_mv, allow_hp);
    }

    if (xd->ref_mv_count[ref_frame_type] > 1) {
      int i;
718
      const int ref_mv_idx = 1 + mbmi->ref_mv_idx;
719 720
      nearestmv[0] = xd->ref_mv_stack[ref_frame_type][0].this_mv;
      nearestmv[1] = xd->ref_mv_stack[ref_frame_type][0].comp_mv;
721 722
      nearmv[0] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].this_mv;
      nearmv[1] = xd->ref_mv_stack[ref_frame_type][ref_mv_idx].comp_mv;
723

724
      for (i = 0; i < 2; ++i) {
725 726 727 728 729 730 731
        lower_mv_precision(&nearestmv[i].as_mv, allow_hp);
        lower_mv_precision(&nearmv[i].as_mv, allow_hp);
      }
    }
  }
#endif

Jingning Han's avatar
Jingning Han committed
732
  mbmi->interp_filter = (cm->interp_filter == SWITCHABLE)
clang-format's avatar
clang-format committed
733 734
                            ? read_switchable_interp_filter(cm, xd, r)
                            : cm->interp_filter;
Jingning Han's avatar
Jingning Han committed
735 736 737 738 739 740 741 742 743 744 745

  if (bsize < BLOCK_8X8) {
    const int num_4x4_w = 1 << xd->bmode_blocks_wl;
    const int num_4x4_h = 1 << xd->bmode_blocks_hl;
    int idx, idy;
    PREDICTION_MODE b_mode;
    int_mv nearest_sub8x8[2], near_sub8x8[2];
    for (idy = 0; idy < 2; idy += num_4x4_h) {
      for (idx = 0; idx < 2; idx += num_4x4_w) {
        int_mv block[2];
        const int j = idy * 2 + idx;
746 747 748 749 750
#if CONFIG_REF_MV
        mode_ctx = av1_mode_context_analyzer(inter_mode_ctx,
                                             mbmi->ref_frame,
                                             bsize, j);
#endif
751
        b_mode = read_inter_mode(cm, xd, r, mode_ctx);
Jingning Han's avatar
Jingning Han committed
752 753 754

        if (b_mode == NEARESTMV || b_mode == NEARMV) {
          for (ref = 0; ref < 1 + is_compound; ++ref)
755
            av1_append_sub8x8_mvs_for_idx(cm, xd, j, ref, mi_row, mi_col,
clang-format's avatar
clang-format committed
756
                                           &nearest_sub8x8[ref],
757
                                           &near_sub8x8[ref]);
Jingning Han's avatar
Jingning Han committed
758 759
        }

760
        if (!assign_mv(cm, xd, b_mode, j, block, nearestmv, nearest_sub8x8,
clang-format's avatar
clang-format committed
761
                       near_sub8x8, is_compound, allow_hp, r)) {
Jingning Han's avatar
Jingning Han committed
762 763 764 765 766
          xd->corrupted |= 1;
          break;
        };

        mi->bmi[j].as_mv[0].as_int = block[0].as_int;
clang-format's avatar
clang-format committed
767
        if (is_compound) mi->bmi[j].as_mv[1].as_int = block[1].as_int;
Jingning Han's avatar
Jingning Han committed
768

clang-format's avatar
clang-format committed
769 770
        if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
        if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
Jingning Han's avatar
Jingning Han committed
771 772 773 774 775 776 777 778
      }
    }

    mi->mbmi.mode = b_mode;

    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 {
779
    xd->corrupted |= !assign_mv(cm, xd, mbmi->mode, 0, mbmi->mv, nearestmv,
Jingning Han's avatar
Jingning Han committed
780 781 782 783
                                nearestmv, nearmv, is_compound, allow_hp, r);
  }
}

784
static void read_inter_frame_mode_info(AV1Decoder *const pbi,
clang-format's avatar
clang-format committed
785
                                       MACROBLOCKD *const xd, int mi_row,
Adrian Grange's avatar
Adrian Grange committed
786
                                       int mi_col, aom_reader *r) {
787
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
  MODE_INFO *const mi = xd->mi[0];
  MB_MODE_INFO *const mbmi = &mi->mbmi;
  int inter_block;

  mbmi->mv[0].as_int = 0;
  mbmi->mv[1].as_int = 0;
  mbmi->segment_id = read_inter_segment_id(cm, xd, mi_row, mi_col, r);
  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);

  if (inter_block)
    read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
  else
    read_intra_block_mode_info(cm, xd, mi, r);
803

clang-format's avatar
clang-format committed
804
  if (mbmi->tx_size < TX_32X32 && cm->base_qindex > 0 && !mbmi->skip &&
805 806 807
      !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
    FRAME_COUNTS *counts = xd->counts;
    if (inter_block) {
808
      mbmi->tx_type = aom_read_tree(r, av1_ext_tx_tree,
clang-format's avatar
clang-format committed
809 810
                                    cm->fc->inter_ext_tx_prob[mbmi->tx_size]);
      if (counts) ++counts->inter_ext_tx[mbmi->tx_size][mbmi->tx_type];
811 812
    } else {
      const TX_TYPE tx_type_nom = intra_mode_to_tx_type_context[mbmi->mode];
clang-format's avatar
clang-format committed
813
      mbmi->tx_type =
814
          aom_read_tree(r, av1_ext_tx_tree,
clang-format's avatar
clang-format committed
815
                        cm->fc->intra_ext_tx_prob[mbmi->tx_size][tx_type_nom]);
816 817 818 819 820 821
      if (counts)
        ++counts->intra_ext_tx[mbmi->tx_size][tx_type_nom][mbmi->tx_type];
    }
  } else {
    mbmi->tx_type = DCT_DCT;
  }
Jingning Han's avatar
Jingning Han committed
822 823
}

824
void av1_read_mode_info(AV1Decoder *const pbi, MACROBLOCKD *xd, int mi_row,
Adrian Grange's avatar
Adrian Grange committed
825
                         int mi_col, aom_reader *r, int x_mis, int y_mis) {
826
  AV1_COMMON *const cm = &pbi->common;
Jingning Han's avatar
Jingning Han committed
827
  MODE_INFO *const mi = xd->mi[0];
clang-format's avatar
clang-format committed
828
  MV_REF *frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
Jingning Han's avatar
Jingning Han committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
  int w, h;

  if (frame_is_intra_only(cm)) {
    read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r);
  } else {
    read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r);

    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;
      }
    }
  }
}