Coverage Report

Created: 2026-04-29 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/decoder/vp9_decodemv.c
Line
Count
Source
1
/*
2
  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  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.
9
 */
10
11
#include <assert.h>
12
13
#include "vp9/common/vp9_common.h"
14
#include "vp9/common/vp9_entropy.h"
15
#include "vp9/common/vp9_entropymode.h"
16
#include "vp9/common/vp9_entropymv.h"
17
#include "vp9/common/vp9_mvref_common.h"
18
#include "vp9/common/vp9_pred_common.h"
19
#include "vp9/common/vp9_reconinter.h"
20
#include "vp9/common/vp9_seg_common.h"
21
22
#include "vp9/decoder/vp9_decodemv.h"
23
#include "vp9/decoder/vp9_decodeframe.h"
24
25
#include "vpx_dsp/vpx_dsp_common.h"
26
27
40.8M
static PREDICTION_MODE read_intra_mode(vpx_reader *r, const vpx_prob *p) {
28
40.8M
  return (PREDICTION_MODE)vpx_read_tree(r, vp9_intra_mode_tree, p);
29
40.8M
}
30
31
static PREDICTION_MODE read_intra_mode_y(VP9_COMMON *cm, MACROBLOCKD *xd,
32
10.0M
                                         vpx_reader *r, int size_group) {
33
10.0M
  const PREDICTION_MODE y_mode =
34
10.0M
      read_intra_mode(r, cm->fc->y_mode_prob[size_group]);
35
10.0M
  FRAME_COUNTS *counts = xd->counts;
36
10.0M
  if (counts) ++counts->y_mode[size_group][y_mode];
37
10.0M
  return y_mode;
38
10.0M
}
39
40
static PREDICTION_MODE read_intra_mode_uv(VP9_COMMON *cm, MACROBLOCKD *xd,
41
                                          vpx_reader *r,
42
8.80M
                                          PREDICTION_MODE y_mode) {
43
8.80M
  const PREDICTION_MODE uv_mode =
44
8.80M
      read_intra_mode(r, cm->fc->uv_mode_prob[y_mode]);
45
8.80M
  FRAME_COUNTS *counts = xd->counts;
46
8.80M
  if (counts) ++counts->uv_mode[y_mode][uv_mode];
47
8.80M
  return uv_mode;
48
8.80M
}
49
50
static PREDICTION_MODE read_inter_mode(VP9_COMMON *cm, MACROBLOCKD *xd,
51
23.3M
                                       vpx_reader *r, int ctx) {
52
23.3M
  const int mode =
53
23.3M
      vpx_read_tree(r, vp9_inter_mode_tree, cm->fc->inter_mode_probs[ctx]);
54
23.3M
  FRAME_COUNTS *counts = xd->counts;
55
23.3M
  if (counts) ++counts->inter_mode[ctx][mode];
56
57
23.3M
  return NEARESTMV + mode;
58
23.3M
}
59
60
7.88M
static int read_segment_id(vpx_reader *r, const struct segmentation *seg) {
61
7.88M
  return vpx_read_tree(r, vp9_segment_tree, seg->tree_probs);
62
7.88M
}
63
64
static TX_SIZE read_selected_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
65
4.55M
                                     TX_SIZE max_tx_size, vpx_reader *r) {
66
4.55M
  FRAME_COUNTS *counts = xd->counts;
67
4.55M
  const int ctx = get_tx_size_context(xd);
68
4.55M
  const vpx_prob *tx_probs = get_tx_probs(max_tx_size, ctx, &cm->fc->tx_probs);
69
4.55M
  int tx_size = vpx_read(r, tx_probs[0]);
70
4.55M
  if (tx_size != TX_4X4 && max_tx_size >= TX_16X16) {
71
1.89M
    tx_size += vpx_read(r, tx_probs[1]);
72
1.89M
    if (tx_size != TX_8X8 && max_tx_size >= TX_32X32)
73
676k
      tx_size += vpx_read(r, tx_probs[2]);
74
1.89M
  }
75
76
4.55M
  if (counts) ++get_tx_counts(max_tx_size, ctx, &counts->tx)[tx_size];
77
4.55M
  return (TX_SIZE)tx_size;
78
4.55M
}
79
80
static INLINE TX_SIZE read_tx_size(VP9_COMMON *cm, MACROBLOCKD *xd,
81
39.5M
                                   int allow_select, vpx_reader *r) {
82
39.5M
  TX_MODE tx_mode = cm->tx_mode;
83
39.5M
  BLOCK_SIZE bsize = xd->mi[0]->sb_type;
84
39.5M
  const TX_SIZE max_tx_size = max_txsize_lookup[bsize];
85
39.5M
  if (allow_select && tx_mode == TX_MODE_SELECT && bsize >= BLOCK_8X8)
86
4.55M
    return read_selected_tx_size(cm, xd, max_tx_size, r);
87
35.0M
  else
88
35.0M
    return VPXMIN(max_tx_size, tx_mode_to_biggest_tx_size[tx_mode]);
89
39.5M
}
90
91
static int dec_get_segment_id(const VP9_COMMON *cm, const uint8_t *segment_ids,
92
7.44M
                              int mi_offset, int x_mis, int y_mis) {
93
7.44M
  int x, y, segment_id = INT_MAX;
94
95
29.5M
  for (y = 0; y < y_mis; y++)
96
132M
    for (x = 0; x < x_mis; x++)
97
109M
      segment_id =
98
109M
          VPXMIN(segment_id, segment_ids[mi_offset + y * cm->mi_cols + x]);
99
100
7.44M
  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
101
7.45M
  return segment_id;
102
7.44M
}
103
104
static void set_segment_id(VP9_COMMON *cm, int mi_offset, int x_mis, int y_mis,
105
7.98M
                           int segment_id) {
106
7.98M
  int x, y;
107
108
7.98M
  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
109
110
40.6M
  for (y = 0; y < y_mis; y++)
111
242M
    for (x = 0; x < x_mis; x++)
112
209M
      cm->current_frame_seg_map[mi_offset + y * cm->mi_cols + x] = segment_id;
113
7.98M
}
114
115
static void copy_segment_id(const VP9_COMMON *cm,
116
                            const uint8_t *last_segment_ids,
117
                            uint8_t *current_segment_ids, int mi_offset,
118
6.64M
                            int x_mis, int y_mis) {
119
6.64M
  int x, y;
120
121
25.0M
  for (y = 0; y < y_mis; y++)
122
106M
    for (x = 0; x < x_mis; x++)
123
87.8M
      current_segment_ids[mi_offset + y * cm->mi_cols + x] =
124
87.8M
          last_segment_ids ? last_segment_ids[mi_offset + y * cm->mi_cols + x]
125
87.8M
                           : 0;
126
6.64M
}
127
128
static int read_intra_segment_id(VP9_COMMON *const cm, int mi_offset, int x_mis,
129
9.54M
                                 int y_mis, vpx_reader *r) {
130
9.54M
  struct segmentation *const seg = &cm->seg;
131
9.54M
  int segment_id;
132
133
9.54M
  if (!seg->enabled) return 0;  // Default for disabled segmentation
134
135
7.17M
  if (!seg->update_map) {
136
1.96M
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
137
1.96M
                    mi_offset, x_mis, y_mis);
138
1.96M
    return 0;
139
1.96M
  }
140
141
5.20M
  segment_id = read_segment_id(r, seg);
142
5.20M
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
143
5.20M
  return segment_id;
144
7.17M
}
145
146
static int read_inter_segment_id(VP9_COMMON *const cm, MACROBLOCKD *const xd,
147
                                 int mi_row, int mi_col, vpx_reader *r,
148
30.0M
                                 int x_mis, int y_mis) {
149
30.0M
  struct segmentation *const seg = &cm->seg;
150
30.0M
  MODE_INFO *const mi = xd->mi[0];
151
30.0M
  int predicted_segment_id, segment_id;
152
30.0M
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
153
154
30.0M
  if (!seg->enabled) return 0;  // Default for disabled segmentation
155
156
7.44M
  predicted_segment_id = cm->last_frame_seg_map
157
7.44M
                             ? dec_get_segment_id(cm, cm->last_frame_seg_map,
158
7.44M
                                                  mi_offset, x_mis, y_mis)
159
7.44M
                             : 0;
160
161
7.44M
  if (!seg->update_map) {
162
4.67M
    copy_segment_id(cm, cm->last_frame_seg_map, cm->current_frame_seg_map,
163
4.67M
                    mi_offset, x_mis, y_mis);
164
4.67M
    return predicted_segment_id;
165
4.67M
  }
166
167
2.76M
  if (seg->temporal_update) {
168
2.12M
    const vpx_prob pred_prob = vp9_get_pred_prob_seg_id(seg, xd);
169
2.12M
    mi->seg_id_predicted = vpx_read(r, pred_prob);
170
2.12M
    segment_id =
171
2.12M
        mi->seg_id_predicted ? predicted_segment_id : read_segment_id(r, seg);
172
2.12M
  } else {
173
645k
    segment_id = read_segment_id(r, seg);
174
645k
  }
175
2.76M
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
176
2.76M
  return segment_id;
177
7.44M
}
178
179
static int read_skip(VP9_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
180
39.5M
                     vpx_reader *r) {
181
39.5M
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
182
11.4M
    return 1;
183
28.1M
  } else {
184
28.1M
    const int ctx = vp9_get_skip_context(xd);
185
28.1M
    const int skip = vpx_read(r, cm->fc->skip_probs[ctx]);
186
28.1M
    FRAME_COUNTS *counts = xd->counts;
187
28.1M
    if (counts) ++counts->skip[ctx][skip];
188
28.1M
    return skip;
189
28.1M
  }
190
39.5M
}
191
192
static void read_intra_frame_mode_info(VP9_COMMON *const cm,
193
                                       MACROBLOCKD *const xd, int mi_row,
194
                                       int mi_col, vpx_reader *r, int x_mis,
195
9.54M
                                       int y_mis) {
196
9.54M
  MODE_INFO *const mi = xd->mi[0];
197
9.54M
  const MODE_INFO *above_mi = xd->above_mi;
198
9.54M
  const MODE_INFO *left_mi = xd->left_mi;
199
9.54M
  const BLOCK_SIZE bsize = mi->sb_type;
200
9.54M
  int i;
201
9.54M
  const int mi_offset = mi_row * cm->mi_cols + mi_col;
202
203
9.54M
  mi->segment_id = read_intra_segment_id(cm, mi_offset, x_mis, y_mis, r);
204
9.54M
  mi->skip = read_skip(cm, xd, mi->segment_id, r);
205
9.54M
  mi->tx_size = read_tx_size(cm, xd, 1, r);
206
9.54M
  mi->ref_frame[0] = INTRA_FRAME;
207
9.54M
  mi->ref_frame[1] = NO_REF_FRAME;
208
209
9.54M
  switch (bsize) {
210
716k
    case BLOCK_4X4:
211
3.57M
      for (i = 0; i < 4; ++i)
212
2.86M
        mi->bmi[i].as_mode =
213
2.86M
            read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, i));
214
716k
      mi->mode = mi->bmi[3].as_mode;
215
716k
      break;
216
312k
    case BLOCK_4X8:
217
312k
      mi->bmi[0].as_mode = mi->bmi[2].as_mode =
218
312k
          read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
219
312k
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mi->mode =
220
312k
          read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 1));
221
312k
      break;
222
506k
    case BLOCK_8X4:
223
506k
      mi->bmi[0].as_mode = mi->bmi[1].as_mode =
224
506k
          read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
225
506k
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mi->mode =
226
506k
          read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 2));
227
506k
      break;
228
8.01M
    default:
229
8.01M
      mi->mode = read_intra_mode(r, get_y_mode_probs(mi, above_mi, left_mi, 0));
230
9.54M
  }
231
232
9.54M
  mi->uv_mode = read_intra_mode(r, vp9_kf_uv_mode_prob[mi->mode]);
233
9.54M
}
234
235
static int read_mv_component(vpx_reader *r, const nmv_component *mvcomp,
236
18.6M
                             int usehp) {
237
18.6M
  int mag, d, fr, hp;
238
18.6M
  const int sign = vpx_read(r, mvcomp->sign);
239
18.6M
  const int mv_class = vpx_read_tree(r, vp9_mv_class_tree, mvcomp->classes);
240
18.6M
  const int class0 = mv_class == MV_CLASS_0;
241
242
  // Integer part
243
18.6M
  if (class0) {
244
12.2M
    d = vpx_read(r, mvcomp->class0[0]);
245
12.2M
    mag = 0;
246
12.2M
  } else {
247
6.39M
    int i;
248
6.39M
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
249
250
6.39M
    d = 0;
251
23.3M
    for (i = 0; i < n; ++i) d |= vpx_read(r, mvcomp->bits[i]) << i;
252
6.39M
    mag = CLASS0_SIZE << (mv_class + 2);
253
6.39M
  }
254
255
  // Fractional part
256
18.6M
  fr = vpx_read_tree(r, vp9_mv_fp_tree,
257
18.6M
                     class0 ? mvcomp->class0_fp[d] : mvcomp->fp);
258
259
  // High precision part (if hp is not used, the default value of the hp is 1)
260
18.6M
  hp = usehp ? vpx_read(r, class0 ? mvcomp->class0_hp : mvcomp->hp) : 1;
261
262
  // Result
263
18.6M
  mag += ((d << 3) | (fr << 1) | hp) + 1;
264
18.6M
  return sign ? -mag : mag;
265
18.6M
}
266
267
static INLINE void read_mv(vpx_reader *r, MV *mv, const MV *ref,
268
                           const nmv_context *ctx, nmv_context_counts *counts,
269
15.0M
                           int allow_hp) {
270
15.0M
  const MV_JOINT_TYPE joint_type =
271
15.0M
      (MV_JOINT_TYPE)vpx_read_tree(r, vp9_mv_joint_tree, ctx->joints);
272
15.0M
  const int use_hp = allow_hp && use_mv_hp(ref);
273
15.0M
  MV diff = { 0, 0 };
274
275
15.0M
  if (mv_joint_vertical(joint_type))
276
10.9M
    diff.row = read_mv_component(r, &ctx->comps[0], use_hp);
277
278
15.0M
  if (mv_joint_horizontal(joint_type))
279
7.69M
    diff.col = read_mv_component(r, &ctx->comps[1], use_hp);
280
281
15.0M
  vp9_inc_mv(&diff, counts);
282
283
15.0M
  mv->row = ref->row + diff.row;
284
15.0M
  mv->col = ref->col + diff.col;
285
15.0M
}
286
287
static REFERENCE_MODE read_block_reference_mode(VP9_COMMON *cm,
288
                                                const MACROBLOCKD *xd,
289
20.6M
                                                vpx_reader *r) {
290
20.6M
  if (cm->reference_mode == REFERENCE_MODE_SELECT) {
291
13.0M
    const int ctx = vp9_get_reference_mode_context(cm, xd);
292
13.0M
    const REFERENCE_MODE mode =
293
13.0M
        (REFERENCE_MODE)vpx_read(r, cm->fc->comp_inter_prob[ctx]);
294
13.0M
    FRAME_COUNTS *counts = xd->counts;
295
13.0M
    if (counts) ++counts->comp_inter[ctx][mode];
296
13.0M
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
297
13.0M
  } else {
298
7.62M
    return cm->reference_mode;
299
7.62M
  }
300
20.6M
}
301
302
// Read the reference frame
303
static void read_ref_frames(VP9_COMMON *const cm, MACROBLOCKD *const xd,
304
                            vpx_reader *r, int segment_id,
305
21.2M
                            MV_REFERENCE_FRAME ref_frame[2]) {
306
21.2M
  FRAME_CONTEXT *const fc = cm->fc;
307
21.2M
  FRAME_COUNTS *counts = xd->counts;
308
309
21.2M
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
310
569k
    ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
311
569k
                                                   SEG_LVL_REF_FRAME);
312
569k
    ref_frame[1] = NO_REF_FRAME;
313
20.6M
  } else {
314
20.6M
    const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
315
    // FIXME(rbultje) I'm pretty sure this breaks segmentation ref frame coding
316
20.6M
    if (mode == COMPOUND_REFERENCE) {
317
10.3M
      const int idx = cm->ref_frame_sign_bias[cm->comp_fixed_ref];
318
10.3M
      const int ctx = vp9_get_pred_context_comp_ref_p(cm, xd);
319
10.3M
      const int bit = vpx_read(r, fc->comp_ref_prob[ctx]);
320
10.3M
      if (counts) ++counts->comp_ref[ctx][bit];
321
10.3M
      ref_frame[idx] = cm->comp_fixed_ref;
322
10.3M
      ref_frame[!idx] = cm->comp_var_ref[bit];
323
10.3M
    } else if (mode == SINGLE_REFERENCE) {
324
10.2M
      const int ctx0 = vp9_get_pred_context_single_ref_p1(xd);
325
10.2M
      const int bit0 = vpx_read(r, fc->single_ref_prob[ctx0][0]);
326
10.2M
      if (counts) ++counts->single_ref[ctx0][0][bit0];
327
10.2M
      if (bit0) {
328
3.93M
        const int ctx1 = vp9_get_pred_context_single_ref_p2(xd);
329
3.93M
        const int bit1 = vpx_read(r, fc->single_ref_prob[ctx1][1]);
330
3.93M
        if (counts) ++counts->single_ref[ctx1][1][bit1];
331
3.93M
        ref_frame[0] = bit1 ? ALTREF_FRAME : GOLDEN_FRAME;
332
6.33M
      } else {
333
6.33M
        ref_frame[0] = LAST_FRAME;
334
6.33M
      }
335
336
10.2M
      ref_frame[1] = NO_REF_FRAME;
337
18.4E
    } else {
338
18.4E
      assert(0 && "Invalid prediction mode.");
339
18.4E
    }
340
20.6M
  }
341
21.2M
}
342
343
static INLINE INTERP_FILTER read_switchable_interp_filter(VP9_COMMON *const cm,
344
                                                          MACROBLOCKD *const xd,
345
3.13M
                                                          vpx_reader *r) {
346
3.13M
  const int ctx = get_pred_context_switchable_interp(xd);
347
3.13M
  const INTERP_FILTER type = (INTERP_FILTER)vpx_read_tree(
348
3.13M
      r, vp9_switchable_interp_tree, cm->fc->switchable_interp_prob[ctx]);
349
3.13M
  FRAME_COUNTS *counts = xd->counts;
350
3.13M
  if (counts) ++counts->switchable_interp[ctx][type];
351
3.13M
  return type;
352
3.13M
}
353
354
static void read_intra_block_mode_info(VP9_COMMON *const cm,
355
                                       MACROBLOCKD *const xd, MODE_INFO *mi,
356
8.80M
                                       vpx_reader *r) {
357
8.80M
  const BLOCK_SIZE bsize = mi->sb_type;
358
8.80M
  int i;
359
360
8.80M
  switch (bsize) {
361
224k
    case BLOCK_4X4:
362
1.12M
      for (i = 0; i < 4; ++i)
363
899k
        mi->bmi[i].as_mode = read_intra_mode_y(cm, xd, r, 0);
364
224k
      mi->mode = mi->bmi[3].as_mode;
365
224k
      break;
366
236k
    case BLOCK_4X8:
367
236k
      mi->bmi[0].as_mode = mi->bmi[2].as_mode = read_intra_mode_y(cm, xd, r, 0);
368
236k
      mi->bmi[1].as_mode = mi->bmi[3].as_mode = mi->mode =
369
236k
          read_intra_mode_y(cm, xd, r, 0);
370
236k
      break;
371
379k
    case BLOCK_8X4:
372
379k
      mi->bmi[0].as_mode = mi->bmi[1].as_mode = read_intra_mode_y(cm, xd, r, 0);
373
379k
      mi->bmi[2].as_mode = mi->bmi[3].as_mode = mi->mode =
374
379k
          read_intra_mode_y(cm, xd, r, 0);
375
379k
      break;
376
7.96M
    default: mi->mode = read_intra_mode_y(cm, xd, r, size_group_lookup[bsize]);
377
8.80M
  }
378
379
8.80M
  mi->uv_mode = read_intra_mode_uv(cm, xd, r, mi->mode);
380
381
  // Initialize interp_filter here so we do not have to check for inter block
382
  // modes in get_pred_context_switchable_interp()
383
8.80M
  mi->interp_filter = SWITCHABLE_FILTERS;
384
385
8.80M
  mi->ref_frame[0] = INTRA_FRAME;
386
8.80M
  mi->ref_frame[1] = NO_REF_FRAME;
387
8.80M
}
388
389
15.0M
static INLINE int is_mv_valid(const MV *mv) {
390
15.0M
  return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
391
15.0M
         mv->col < MV_UPP;
392
15.0M
}
393
394
368M
static INLINE void copy_mv_pair(int_mv *dst, const int_mv *src) {
395
368M
  memcpy(dst, src, sizeof(*dst) * 2);
396
368M
}
397
398
3.03M
static INLINE void zero_mv_pair(int_mv *dst) {
399
3.03M
  memset(dst, 0, sizeof(*dst) * 2);
400
3.03M
}
401
402
static INLINE int assign_mv(VP9_COMMON *cm, MACROBLOCKD *xd,
403
                            PREDICTION_MODE mode, int_mv mv[2],
404
                            int_mv ref_mv[2], int_mv near_nearest_mv[2],
405
24.9M
                            int is_compound, int allow_hp, vpx_reader *r) {
406
24.9M
  int i;
407
24.9M
  int ret = 1;
408
409
24.9M
  switch (mode) {
410
9.53M
    case NEWMV: {
411
9.53M
      FRAME_COUNTS *counts = xd->counts;
412
9.53M
      nmv_context_counts *const mv_counts = counts ? &counts->mv : NULL;
413
24.5M
      for (i = 0; i < 1 + is_compound; ++i) {
414
15.0M
        read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, &cm->fc->nmvc, mv_counts,
415
15.0M
                allow_hp);
416
15.0M
        ret = ret && is_mv_valid(&mv[i].as_mv);
417
15.0M
      }
418
9.53M
      break;
419
0
    }
420
3.15M
    case NEARMV:
421
12.3M
    case NEARESTMV: {
422
12.3M
      copy_mv_pair(mv, near_nearest_mv);
423
12.3M
      break;
424
3.15M
    }
425
3.03M
    case ZEROMV: {
426
3.03M
      zero_mv_pair(mv);
427
3.03M
      break;
428
3.15M
    }
429
0
    default: {
430
0
      return 0;
431
3.15M
    }
432
24.9M
  }
433
24.9M
  return ret;
434
24.9M
}
435
436
static int read_is_inter_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
437
30.0M
                               int segment_id, vpx_reader *r) {
438
30.0M
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
439
4.17M
    return get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME) != INTRA_FRAME;
440
25.8M
  } else {
441
25.8M
    const int ctx = get_intra_inter_context(xd);
442
25.8M
    const int is_inter = vpx_read(r, cm->fc->intra_inter_prob[ctx]);
443
25.8M
    FRAME_COUNTS *counts = xd->counts;
444
25.8M
    if (counts) ++counts->intra_inter[ctx][is_inter];
445
25.8M
    return is_inter;
446
25.8M
  }
447
30.0M
}
448
449
// This macro is used to add a motion vector mv_ref list if it isn't
450
// already in the list.  If it's the second motion vector or early_break
451
// it will also skip all additional processing and jump to Done!
452
#define ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done) \
453
39.2M
  do {                                                         \
454
39.2M
    if (refmv_count) {                                         \
455
10.0M
      if ((mv).as_int != (mv_ref_list)[0].as_int) {            \
456
4.18M
        (mv_ref_list)[(refmv_count)] = (mv);                   \
457
4.18M
        refmv_count++;                                         \
458
4.18M
        goto Done;                                             \
459
4.18M
      }                                                        \
460
29.2M
    } else {                                                   \
461
29.2M
      (mv_ref_list)[(refmv_count)++] = (mv);                   \
462
29.2M
      if (early_break) goto Done;                              \
463
29.2M
    }                                                          \
464
39.2M
  } while (0)
465
466
// If either reference frame is different, not INTRA, and they
467
// are different from each other scale and add the mv to our list.
468
#define IF_DIFF_REF_FRAME_ADD_MV_EB(mbmi, ref_frame, ref_sign_bias,       \
469
                                    refmv_count, mv_ref_list, Done)       \
470
8.63M
  do {                                                                    \
471
8.63M
    if (is_inter_block(mbmi)) {                                           \
472
5.44M
      if ((mbmi)->ref_frame[0] != ref_frame)                              \
473
5.44M
        ADD_MV_REF_LIST_EB(scale_mv((mbmi), 0, ref_frame, ref_sign_bias), \
474
5.44M
                           refmv_count, mv_ref_list, Done);               \
475
5.44M
      if (has_second_ref(mbmi) && (mbmi)->ref_frame[1] != ref_frame &&    \
476
3.51M
          (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int)                   \
477
3.51M
        ADD_MV_REF_LIST_EB(scale_mv((mbmi), 1, ref_frame, ref_sign_bias), \
478
3.51M
                           refmv_count, mv_ref_list, Done);               \
479
3.51M
    }                                                                     \
480
8.63M
  } while (0)
481
482
// This function searches the neighborhood of a given MB/SB
483
// to try and find candidate reference vectors.
484
static int dec_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
485
                            PREDICTION_MODE mode, MV_REFERENCE_FRAME ref_frame,
486
                            const POSITION *const mv_ref_search,
487
                            int_mv *mv_ref_list, int mi_row, int mi_col,
488
29.5M
                            int block) {
489
29.5M
  const int *ref_sign_bias = cm->ref_frame_sign_bias;
490
29.5M
  int i, refmv_count = 0;
491
29.5M
  int different_ref_found = 0;
492
29.5M
  const MV_REF *const prev_frame_mvs =
493
29.5M
      cm->use_prev_frame_mvs
494
29.5M
          ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
495
29.5M
          : NULL;
496
29.5M
  const TileInfo *const tile = &xd->tile;
497
  // If mode is nearestmv or newmv (uses nearestmv as a reference) then stop
498
  // searching after the first mv is found.
499
29.5M
  const int early_break = (mode != NEARMV);
500
501
  // Blank the reference vector list
502
29.5M
  memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
503
504
29.5M
  i = 0;
505
29.5M
  if (block >= 0) {
506
    // If the size < 8x8 we get the mv from the bmi substructure for the
507
    // nearest two blocks.
508
5.64M
    for (i = 0; i < 2; ++i) {
509
4.43M
      const POSITION *const mv_ref = &mv_ref_search[i];
510
4.43M
      if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
511
4.22M
        const MODE_INFO *const candidate_mi =
512
4.22M
            xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
513
4.22M
        different_ref_found = 1;
514
515
4.22M
        if (candidate_mi->ref_frame[0] == ref_frame)
516
1.54M
          ADD_MV_REF_LIST_EB(
517
4.22M
              get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
518
4.22M
              refmv_count, mv_ref_list, Done);
519
2.67M
        else if (candidate_mi->ref_frame[1] == ref_frame)
520
912k
          ADD_MV_REF_LIST_EB(
521
4.22M
              get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
522
4.22M
              refmv_count, mv_ref_list, Done);
523
4.22M
      }
524
4.43M
    }
525
2.59M
  }
526
527
  // Check the rest of the neighbors in much the same way
528
  // as before except we don't need to keep track of sub blocks or
529
  // mode counts.
530
78.7M
  for (; i < MVREF_NEIGHBOURS; ++i) {
531
75.4M
    const POSITION *const mv_ref = &mv_ref_search[i];
532
75.4M
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
533
70.9M
      const MODE_INFO *const candidate =
534
70.9M
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
535
70.9M
      different_ref_found = 1;
536
537
70.9M
      if (candidate->ref_frame[0] == ref_frame)
538
21.3M
        ADD_MV_REF_LIST_EB(candidate->mv[0], refmv_count, mv_ref_list, Done);
539
49.6M
      else if (candidate->ref_frame[1] == ref_frame)
540
10.8M
        ADD_MV_REF_LIST_EB(candidate->mv[1], refmv_count, mv_ref_list, Done);
541
70.9M
    }
542
75.4M
  }
543
544
  // Check the last frame's mode and mv info.
545
3.25M
  if (prev_frame_mvs) {
546
433k
    if (prev_frame_mvs->ref_frame[0] == ref_frame) {
547
173k
      ADD_MV_REF_LIST_EB(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, Done);
548
259k
    } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
549
66.2k
      ADD_MV_REF_LIST_EB(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, Done);
550
66.2k
    }
551
433k
  }
552
553
  // Since we couldn't find 2 mvs from the same reference frame
554
  // go back through the neighbors and find motion vectors from
555
  // different reference frames.
556
3.10M
  if (different_ref_found) {
557
11.2M
    for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
558
10.3M
      const POSITION *mv_ref = &mv_ref_search[i];
559
10.3M
      if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
560
8.63M
        const MODE_INFO *const candidate =
561
8.63M
            xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
562
563
        // If the candidate is INTRA we don't want to consider its mv.
564
8.63M
        IF_DIFF_REF_FRAME_ADD_MV_EB(candidate, ref_frame, ref_sign_bias,
565
8.63M
                                    refmv_count, mv_ref_list, Done);
566
8.63M
      }
567
10.3M
    }
568
3.07M
  }
569
570
  // Since we still don't have a candidate we'll try the last frame.
571
911k
  if (prev_frame_mvs) {
572
142k
    if (prev_frame_mvs->ref_frame[0] != ref_frame &&
573
102k
        prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
574
68.1k
      int_mv mv = prev_frame_mvs->mv[0];
575
68.1k
      if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
576
68.1k
          ref_sign_bias[ref_frame]) {
577
53.4k
        mv.as_mv.row *= -1;
578
53.4k
        mv.as_mv.col *= -1;
579
53.4k
      }
580
68.1k
      ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done);
581
68.1k
    }
582
583
120k
    if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
584
76.1k
        prev_frame_mvs->ref_frame[1] != ref_frame &&
585
46.1k
        prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int) {
586
5.55k
      int_mv mv = prev_frame_mvs->mv[1];
587
5.55k
      if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
588
5.55k
          ref_sign_bias[ref_frame]) {
589
3.61k
        mv.as_mv.row *= -1;
590
3.61k
        mv.as_mv.col *= -1;
591
3.61k
      }
592
5.55k
      ADD_MV_REF_LIST_EB(mv, refmv_count, mv_ref_list, Done);
593
5.55k
    }
594
120k
  }
595
596
884k
  if (mode == NEARMV)
597
603k
    refmv_count = MAX_MV_REF_CANDIDATES;
598
281k
  else
599
    // we only care about the nearestmv for the remaining modes
600
281k
    refmv_count = 1;
601
602
29.5M
Done:
603
  // Clamp vectors
604
63.8M
  for (i = 0; i < refmv_count; ++i) clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
605
606
29.5M
  return refmv_count;
607
884k
}
608
609
static void append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
610
                                      const POSITION *const mv_ref_search,
611
                                      PREDICTION_MODE b_mode, int block,
612
                                      int ref, int mi_row, int mi_col,
613
4.75M
                                      int_mv *best_sub8x8) {
614
4.75M
  int_mv mv_list[MAX_MV_REF_CANDIDATES];
615
4.75M
  MODE_INFO *const mi = xd->mi[0];
616
4.75M
  b_mode_info *bmi = mi->bmi;
617
4.75M
  int n;
618
4.75M
  int refmv_count;
619
620
4.75M
  assert(MAX_MV_REF_CANDIDATES == 2);
621
622
4.75M
  switch (block) {
623
1.85M
    case 0:
624
1.85M
      refmv_count =
625
1.85M
          dec_find_mv_refs(cm, xd, b_mode, mi->ref_frame[ref], mv_ref_search,
626
1.85M
                           mv_list, mi_row, mi_col, block);
627
1.85M
      best_sub8x8->as_int = mv_list[refmv_count - 1].as_int;
628
1.85M
      break;
629
1.03M
    case 1:
630
2.39M
    case 2:
631
2.39M
      if (b_mode == NEARESTMV) {
632
1.68M
        best_sub8x8->as_int = bmi[0].as_mv[ref].as_int;
633
1.68M
      } else {
634
702k
        dec_find_mv_refs(cm, xd, b_mode, mi->ref_frame[ref], mv_ref_search,
635
702k
                         mv_list, mi_row, mi_col, block);
636
702k
        best_sub8x8->as_int = 0;
637
1.08M
        for (n = 0; n < 2; ++n)
638
1.00M
          if (bmi[0].as_mv[ref].as_int != mv_list[n].as_int) {
639
625k
            best_sub8x8->as_int = mv_list[n].as_int;
640
625k
            break;
641
625k
          }
642
702k
      }
643
2.39M
      break;
644
504k
    case 3:
645
504k
      if (b_mode == NEARESTMV) {
646
356k
        best_sub8x8->as_int = bmi[2].as_mv[ref].as_int;
647
356k
      } else {
648
147k
        best_sub8x8->as_int = 0;
649
147k
        if (bmi[2].as_mv[ref].as_int != bmi[1].as_mv[ref].as_int) {
650
97.2k
          best_sub8x8->as_int = bmi[1].as_mv[ref].as_int;
651
97.2k
          break;
652
97.2k
        }
653
50.6k
        if (bmi[2].as_mv[ref].as_int != bmi[0].as_mv[ref].as_int) {
654
12.7k
          best_sub8x8->as_int = bmi[0].as_mv[ref].as_int;
655
12.7k
          break;
656
12.7k
        }
657
37.9k
        dec_find_mv_refs(cm, xd, b_mode, mi->ref_frame[ref], mv_ref_search,
658
37.9k
                         mv_list, mi_row, mi_col, block);
659
81.3k
        for (n = 0; n < 2; ++n)
660
64.6k
          if (bmi[2].as_mv[ref].as_int != mv_list[n].as_int) {
661
21.2k
            best_sub8x8->as_int = mv_list[n].as_int;
662
21.2k
            break;
663
21.2k
          }
664
37.9k
      }
665
394k
      break;
666
394k
    default: assert(0 && "Invalid block index.");
667
4.75M
  }
668
4.75M
}
669
670
static uint8_t get_mode_context(const VP9_COMMON *cm, const MACROBLOCKD *xd,
671
                                const POSITION *const mv_ref_search, int mi_row,
672
21.2M
                                int mi_col) {
673
21.2M
  int i;
674
21.2M
  int context_counter = 0;
675
21.2M
  const TileInfo *const tile = &xd->tile;
676
677
  // Get mode count from nearest 2 blocks
678
63.5M
  for (i = 0; i < 2; ++i) {
679
42.3M
    const POSITION *const mv_ref = &mv_ref_search[i];
680
42.3M
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
681
40.6M
      const MODE_INFO *const candidate =
682
40.6M
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
683
      // Keep counts for entropy encoding.
684
40.6M
      context_counter += mode_2_counter[candidate->mode];
685
40.6M
    }
686
42.3M
  }
687
688
21.2M
  return counter_to_context[context_counter];
689
21.2M
}
690
691
static void read_inter_block_mode_info(VP9Decoder *const pbi,
692
                                       MACROBLOCKD *const xd,
693
                                       MODE_INFO *const mi, int mi_row,
694
21.2M
                                       int mi_col, vpx_reader *r) {
695
21.2M
  VP9_COMMON *const cm = &pbi->common;
696
21.2M
  const BLOCK_SIZE bsize = mi->sb_type;
697
21.2M
  const int allow_hp = cm->allow_high_precision_mv;
698
21.2M
  int_mv best_ref_mvs[2] = { { 0 }, { 0 } };
699
21.2M
  int ref, is_compound;
700
21.2M
  uint8_t inter_mode_ctx;
701
21.2M
  const POSITION *const mv_ref_search = mv_ref_blocks[bsize];
702
703
21.2M
  read_ref_frames(cm, xd, r, mi->segment_id, mi->ref_frame);
704
21.2M
  is_compound = has_second_ref(mi);
705
21.2M
  inter_mode_ctx = get_mode_context(cm, xd, mv_ref_search, mi_row, mi_col);
706
707
21.2M
  if (segfeature_active(&cm->seg, mi->segment_id, SEG_LVL_SKIP)) {
708
1.44M
    mi->mode = ZEROMV;
709
1.44M
    if (bsize < BLOCK_8X8) {
710
240
      vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
711
240
                         "Invalid usage of segment feature on small blocks");
712
240
      return;
713
240
    }
714
19.7M
  } else {
715
19.7M
    if (bsize >= BLOCK_8X8)
716
17.3M
      mi->mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
717
19.7M
  }
718
719
21.2M
  mi->interp_filter = (cm->interp_filter == SWITCHABLE)
720
21.2M
                          ? read_switchable_interp_filter(cm, xd, r)
721
21.2M
                          : cm->interp_filter;
722
723
21.2M
  if (bsize < BLOCK_8X8) {
724
2.37M
    const int num_4x4_w = 1 << xd->bmode_blocks_wl;
725
2.37M
    const int num_4x4_h = 1 << xd->bmode_blocks_hl;
726
2.37M
    int idx, idy;
727
2.37M
    PREDICTION_MODE b_mode;
728
2.37M
    int got_mv_refs_for_new = 0;
729
2.37M
    int_mv best_sub8x8[2];
730
2.37M
    const uint32_t invalid_mv = 0x80008000;
731
    // Initialize the 2nd element as even though it won't be used meaningfully
732
    // if is_compound is false, copying/clamping it may trigger a MSan warning.
733
2.37M
    best_sub8x8[1].as_int = invalid_mv;
734
6.44M
    for (idy = 0; idy < 2; idy += num_4x4_h) {
735
10.0M
      for (idx = 0; idx < 2; idx += num_4x4_w) {
736
6.02M
        const int j = idy * 2 + idx;
737
6.02M
        b_mode = read_inter_mode(cm, xd, r, inter_mode_ctx);
738
739
6.02M
        if (b_mode == NEARESTMV || b_mode == NEARMV) {
740
7.79M
          for (ref = 0; ref < 1 + is_compound; ++ref)
741
4.75M
            append_sub8x8_mvs_for_idx(cm, xd, mv_ref_search, b_mode, j, ref,
742
4.75M
                                      mi_row, mi_col, &best_sub8x8[ref]);
743
3.04M
        } else if (b_mode == NEWMV && !got_mv_refs_for_new) {
744
4.30M
          for (ref = 0; ref < 1 + is_compound; ++ref) {
745
2.65M
            int_mv tmp_mvs[MAX_MV_REF_CANDIDATES];
746
2.65M
            const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
747
748
2.65M
            dec_find_mv_refs(cm, xd, NEWMV, frame, mv_ref_search, tmp_mvs,
749
2.65M
                             mi_row, mi_col, -1);
750
751
2.65M
            lower_mv_precision(&tmp_mvs[0].as_mv, allow_hp);
752
2.65M
            best_ref_mvs[ref] = tmp_mvs[0];
753
2.65M
            got_mv_refs_for_new = 1;
754
2.65M
          }
755
1.64M
        }
756
757
6.02M
        if (!assign_mv(cm, xd, b_mode, mi->bmi[j].as_mv, best_ref_mvs,
758
6.02M
                       best_sub8x8, is_compound, allow_hp, r)) {
759
5.70k
          xd->corrupted |= 1;
760
5.70k
          return;
761
5.70k
        }
762
763
6.01M
        if (num_4x4_h == 2) mi->bmi[j + 2] = mi->bmi[j];
764
6.01M
        if (num_4x4_w == 2) mi->bmi[j + 1] = mi->bmi[j];
765
6.01M
      }
766
4.07M
    }
767
768
2.36M
    mi->mode = b_mode;
769
770
2.36M
    copy_mv_pair(mi->mv, mi->bmi[3].as_mv);
771
18.8M
  } else {
772
18.8M
    if (mi->mode != ZEROMV) {
773
40.4M
      for (ref = 0; ref < 1 + is_compound; ++ref) {
774
24.2M
        int_mv tmp_mvs[MAX_MV_REF_CANDIDATES];
775
24.2M
        const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
776
24.2M
        int refmv_count =
777
24.2M
            dec_find_mv_refs(cm, xd, mi->mode, frame, mv_ref_search, tmp_mvs,
778
24.2M
                             mi_row, mi_col, -1);
779
24.2M
        lower_mv_precision(&tmp_mvs[refmv_count - 1].as_mv, allow_hp);
780
24.2M
        best_ref_mvs[ref] = tmp_mvs[refmv_count - 1];
781
24.2M
      }
782
16.2M
    }
783
18.8M
    xd->corrupted |= !assign_mv(cm, xd, mi->mode, mi->mv, best_ref_mvs,
784
18.8M
                                best_ref_mvs, is_compound, allow_hp, r);
785
18.8M
  }
786
21.2M
}
787
788
static void read_inter_frame_mode_info(VP9Decoder *const pbi,
789
                                       MACROBLOCKD *const xd, int mi_row,
790
                                       int mi_col, vpx_reader *r, int x_mis,
791
30.0M
                                       int y_mis) {
792
30.0M
  VP9_COMMON *const cm = &pbi->common;
793
30.0M
  MODE_INFO *const mi = xd->mi[0];
794
30.0M
  int inter_block;
795
796
30.0M
  mi->segment_id =
797
30.0M
      read_inter_segment_id(cm, xd, mi_row, mi_col, r, x_mis, y_mis);
798
30.0M
  mi->skip = read_skip(cm, xd, mi->segment_id, r);
799
30.0M
  inter_block = read_is_inter_block(cm, xd, mi->segment_id, r);
800
30.0M
  mi->tx_size = read_tx_size(cm, xd, !mi->skip || !inter_block, r);
801
802
30.0M
  if (inter_block)
803
21.2M
    read_inter_block_mode_info(pbi, xd, mi, mi_row, mi_col, r);
804
8.82M
  else
805
8.82M
    read_intra_block_mode_info(cm, xd, mi, r);
806
30.0M
}
807
808
static INLINE void copy_ref_frame_pair(MV_REFERENCE_FRAME *dst,
809
353M
                                       const MV_REFERENCE_FRAME *src) {
810
353M
  memcpy(dst, src, sizeof(*dst) * 2);
811
353M
}
812
813
void vp9_read_mode_info(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
814
39.6M
                        int mi_col, int x_mis, int y_mis) {
815
39.6M
  vpx_reader *r = &twd->bit_reader;
816
39.6M
  MACROBLOCKD *const xd = &twd->xd;
817
39.6M
  VP9_COMMON *const cm = &pbi->common;
818
39.6M
  MODE_INFO *const mi = xd->mi[0];
819
39.6M
  MV_REF *frame_mvs = cm->cur_frame->mvs + mi_row * cm->mi_cols + mi_col;
820
39.6M
  int w, h;
821
822
39.6M
  if (frame_is_intra_only(cm)) {
823
9.54M
    read_intra_frame_mode_info(cm, xd, mi_row, mi_col, r, x_mis, y_mis);
824
30.0M
  } else {
825
    // Cache mi->ref_frame and mi->mv so that the compiler can prove that they
826
    // are constant for the duration of the loop and avoids reloading them.
827
30.0M
    MV_REFERENCE_FRAME mi_ref_frame[2];
828
30.0M
    int_mv mi_mv[2];
829
830
30.0M
    read_inter_frame_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
831
832
30.0M
    copy_ref_frame_pair(mi_ref_frame, mi->ref_frame);
833
30.0M
    copy_mv_pair(mi_mv, mi->mv);
834
835
104M
    for (h = 0; h < y_mis; ++h) {
836
397M
      for (w = 0; w < x_mis; ++w) {
837
323M
        MV_REF *const mv = frame_mvs + w;
838
323M
        copy_ref_frame_pair(mv->ref_frame, mi_ref_frame);
839
323M
        copy_mv_pair(mv->mv, mi_mv);
840
323M
      }
841
74.2M
      frame_mvs += cm->mi_cols;
842
74.2M
    }
843
30.0M
  }
844
#if 0   // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
845
  if ((xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) &&
846
      (xd->above_mi == NULL || xd->left_mi == NULL) &&
847
      !is_inter_block(mi) && need_top_left[mi->uv_mode])
848
    assert(0);
849
#endif  // CONFIG_BETTER_HW_COMPATIBILITY && CONFIG_VP9_HIGHBITDEPTH
850
39.6M
}