Coverage Report

Created: 2025-07-23 08:18

/src/aom/av1/decoder/decodemv.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include <assert.h>
13
14
#include "av1/common/cfl.h"
15
#include "av1/common/common.h"
16
#include "av1/common/entropy.h"
17
#include "av1/common/entropymode.h"
18
#include "av1/common/entropymv.h"
19
#include "av1/common/mvref_common.h"
20
#include "av1/common/pred_common.h"
21
#include "av1/common/reconinter.h"
22
#include "av1/common/reconintra.h"
23
#include "av1/common/seg_common.h"
24
#include "av1/common/warped_motion.h"
25
26
#include "av1/decoder/decodeframe.h"
27
#include "av1/decoder/decodemv.h"
28
29
#include "aom_dsp/aom_dsp_common.h"
30
31
#define ACCT_STR __func__
32
33
#define DEC_MISMATCH_DEBUG 0
34
35
8.06M
static PREDICTION_MODE read_intra_mode(aom_reader *r, aom_cdf_prob *cdf) {
36
8.06M
  return (PREDICTION_MODE)aom_read_symbol(r, cdf, INTRA_MODES, ACCT_STR);
37
8.06M
}
38
39
8.13M
static void read_cdef(AV1_COMMON *cm, aom_reader *r, MACROBLOCKD *const xd) {
40
8.13M
  const int skip_txfm = xd->mi[0]->skip_txfm;
41
8.13M
  if (cm->features.coded_lossless) return;
42
8.12M
  if (cm->features.allow_intrabc) {
43
684k
    assert(cm->cdef_info.cdef_bits == 0);
44
684k
    return;
45
684k
  }
46
47
  // At the start of a superblock, mark that we haven't yet read CDEF strengths
48
  // for any of the CDEF units contained in this superblock.
49
7.44M
  const int sb_mask = (cm->seq_params->mib_size - 1);
50
7.44M
  const int mi_row_in_sb = (xd->mi_row & sb_mask);
51
7.44M
  const int mi_col_in_sb = (xd->mi_col & sb_mask);
52
7.44M
  if (mi_row_in_sb == 0 && mi_col_in_sb == 0) {
53
481k
    xd->cdef_transmitted[0] = xd->cdef_transmitted[1] =
54
481k
        xd->cdef_transmitted[2] = xd->cdef_transmitted[3] = false;
55
481k
  }
56
57
  // CDEF unit size is 64x64 irrespective of the superblock size.
58
7.44M
  const int cdef_size = 1 << (6 - MI_SIZE_LOG2);
59
60
  // Find index of this CDEF unit in this superblock.
61
7.44M
  const int index_mask = cdef_size;
62
7.44M
  const int cdef_unit_row_in_sb = ((xd->mi_row & index_mask) != 0);
63
7.44M
  const int cdef_unit_col_in_sb = ((xd->mi_col & index_mask) != 0);
64
7.44M
  const int index = (cm->seq_params->sb_size == BLOCK_128X128)
65
7.44M
                        ? cdef_unit_col_in_sb + 2 * cdef_unit_row_in_sb
66
7.44M
                        : 0;
67
68
  // Read CDEF strength from the first non-skip coding block in this CDEF unit.
69
7.44M
  if (!xd->cdef_transmitted[index] && !skip_txfm) {
70
    // CDEF strength for this CDEF unit needs to be read into the MB_MODE_INFO
71
    // of the 1st block in this CDEF unit.
72
344k
    const int first_block_mask = ~(cdef_size - 1);
73
344k
    CommonModeInfoParams *const mi_params = &cm->mi_params;
74
344k
    const int grid_idx =
75
344k
        get_mi_grid_idx(mi_params, xd->mi_row & first_block_mask,
76
344k
                        xd->mi_col & first_block_mask);
77
344k
    MB_MODE_INFO *const mbmi = mi_params->mi_grid_base[grid_idx];
78
344k
    mbmi->cdef_strength =
79
344k
        aom_read_literal(r, cm->cdef_info.cdef_bits, ACCT_STR);
80
344k
    xd->cdef_transmitted[index] = true;
81
344k
  }
82
7.44M
}
83
84
static int read_delta_qindex(AV1_COMMON *cm, const MACROBLOCKD *xd,
85
1.58M
                             aom_reader *r, MB_MODE_INFO *const mbmi) {
86
1.58M
  int sign, abs, reduced_delta_qindex = 0;
87
1.58M
  BLOCK_SIZE bsize = mbmi->bsize;
88
1.58M
  const int b_col = xd->mi_col & (cm->seq_params->mib_size - 1);
89
1.58M
  const int b_row = xd->mi_row & (cm->seq_params->mib_size - 1);
90
1.58M
  const int read_delta_q_flag = (b_col == 0 && b_row == 0);
91
1.58M
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
92
93
1.58M
  if ((bsize != cm->seq_params->sb_size || mbmi->skip_txfm == 0) &&
94
1.58M
      read_delta_q_flag) {
95
111k
    abs = aom_read_symbol(r, ec_ctx->delta_q_cdf, DELTA_Q_PROBS + 1, ACCT_STR);
96
111k
    const int smallval = (abs < DELTA_Q_SMALL);
97
98
111k
    if (!smallval) {
99
16.1k
      const int rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
100
16.1k
      const int thr = (1 << rem_bits) + 1;
101
16.1k
      abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
102
16.1k
    }
103
104
111k
    if (abs) {
105
27.6k
      sign = aom_read_bit(r, ACCT_STR);
106
83.4k
    } else {
107
83.4k
      sign = 1;
108
83.4k
    }
109
110
111k
    reduced_delta_qindex = sign ? -abs : abs;
111
111k
  }
112
1.58M
  return reduced_delta_qindex;
113
1.58M
}
114
static int read_delta_lflevel(const AV1_COMMON *const cm, aom_reader *r,
115
                              aom_cdf_prob *const cdf,
116
                              const MB_MODE_INFO *const mbmi, int mi_col,
117
3.00M
                              int mi_row) {
118
3.00M
  int reduced_delta_lflevel = 0;
119
3.00M
  const BLOCK_SIZE bsize = mbmi->bsize;
120
3.00M
  const int b_col = mi_col & (cm->seq_params->mib_size - 1);
121
3.00M
  const int b_row = mi_row & (cm->seq_params->mib_size - 1);
122
3.00M
  const int read_delta_lf_flag = (b_col == 0 && b_row == 0);
123
124
3.00M
  if ((bsize != cm->seq_params->sb_size || mbmi->skip_txfm == 0) &&
125
3.00M
      read_delta_lf_flag) {
126
235k
    int abs = aom_read_symbol(r, cdf, DELTA_LF_PROBS + 1, ACCT_STR);
127
235k
    const int smallval = (abs < DELTA_LF_SMALL);
128
235k
    if (!smallval) {
129
6.60k
      const int rem_bits = aom_read_literal(r, 3, ACCT_STR) + 1;
130
6.60k
      const int thr = (1 << rem_bits) + 1;
131
6.60k
      abs = aom_read_literal(r, rem_bits, ACCT_STR) + thr;
132
6.60k
    }
133
235k
    const int sign = abs ? aom_read_bit(r, ACCT_STR) : 1;
134
235k
    reduced_delta_lflevel = sign ? -abs : abs;
135
235k
  }
136
3.00M
  return reduced_delta_lflevel;
137
3.00M
}
138
139
static UV_PREDICTION_MODE read_intra_mode_uv(FRAME_CONTEXT *ec_ctx,
140
                                             aom_reader *r,
141
                                             CFL_ALLOWED_TYPE cfl_allowed,
142
7.31M
                                             PREDICTION_MODE y_mode) {
143
7.31M
  const UV_PREDICTION_MODE uv_mode =
144
7.31M
      aom_read_symbol(r, ec_ctx->uv_mode_cdf[cfl_allowed][y_mode],
145
7.31M
                      UV_INTRA_MODES - !cfl_allowed, ACCT_STR);
146
7.31M
  return uv_mode;
147
7.31M
}
148
149
static uint8_t read_cfl_alphas(FRAME_CONTEXT *const ec_ctx, aom_reader *r,
150
1.80M
                               int8_t *signs_out) {
151
1.80M
  const int8_t joint_sign =
152
1.80M
      aom_read_symbol(r, ec_ctx->cfl_sign_cdf, CFL_JOINT_SIGNS, "cfl:signs");
153
1.80M
  uint8_t idx = 0;
154
  // Magnitudes are only coded for nonzero values
155
1.80M
  if (CFL_SIGN_U(joint_sign) != CFL_SIGN_ZERO) {
156
1.68M
    aom_cdf_prob *cdf_u = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_U(joint_sign)];
157
1.68M
    idx = (uint8_t)aom_read_symbol(r, cdf_u, CFL_ALPHABET_SIZE, "cfl:alpha_u")
158
1.68M
          << CFL_ALPHABET_SIZE_LOG2;
159
1.68M
  }
160
1.80M
  if (CFL_SIGN_V(joint_sign) != CFL_SIGN_ZERO) {
161
1.32M
    aom_cdf_prob *cdf_v = ec_ctx->cfl_alpha_cdf[CFL_CONTEXT_V(joint_sign)];
162
1.32M
    idx += (uint8_t)aom_read_symbol(r, cdf_v, CFL_ALPHABET_SIZE, "cfl:alpha_v");
163
1.32M
  }
164
1.80M
  *signs_out = joint_sign;
165
1.80M
  return idx;
166
1.80M
}
167
168
static INTERINTRA_MODE read_interintra_mode(MACROBLOCKD *xd, aom_reader *r,
169
94
                                            int size_group) {
170
94
  const INTERINTRA_MODE ii_mode = (INTERINTRA_MODE)aom_read_symbol(
171
94
      r, xd->tile_ctx->interintra_mode_cdf[size_group], INTERINTRA_MODES,
172
94
      ACCT_STR);
173
94
  return ii_mode;
174
94
}
175
176
static PREDICTION_MODE read_inter_mode(FRAME_CONTEXT *ec_ctx, aom_reader *r,
177
4.28k
                                       int16_t ctx) {
178
4.28k
  int16_t mode_ctx = ctx & NEWMV_CTX_MASK;
179
4.28k
  int is_newmv, is_zeromv, is_refmv;
180
4.28k
  is_newmv = aom_read_symbol(r, ec_ctx->newmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
181
4.28k
  if (is_newmv) return NEWMV;
182
183
1.78k
  mode_ctx = (ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
184
1.78k
  is_zeromv =
185
1.78k
      aom_read_symbol(r, ec_ctx->zeromv_cdf[mode_ctx], 2, ACCT_STR) == 0;
186
1.78k
  if (is_zeromv) return GLOBALMV;
187
188
1.65k
  mode_ctx = (ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
189
1.65k
  is_refmv = aom_read_symbol(r, ec_ctx->refmv_cdf[mode_ctx], 2, ACCT_STR) == 0;
190
1.65k
  if (is_refmv)
191
1.10k
    return NEARESTMV;
192
543
  else
193
543
    return NEARMV;
194
1.65k
}
195
196
static void read_drl_idx(FRAME_CONTEXT *ec_ctx, DecoderCodingBlock *dcb,
197
3.90k
                         MB_MODE_INFO *mbmi, aom_reader *r) {
198
3.90k
  MACROBLOCKD *const xd = &dcb->xd;
199
3.90k
  uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
200
3.90k
  mbmi->ref_mv_idx = 0;
201
3.90k
  if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV) {
202
7.63k
    for (int idx = 0; idx < 2; ++idx) {
203
5.46k
      if (dcb->ref_mv_count[ref_frame_type] > idx + 1) {
204
1.43k
        uint8_t drl_ctx = av1_drl_ctx(xd->weight[ref_frame_type], idx);
205
1.43k
        int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
206
1.43k
        mbmi->ref_mv_idx = idx + drl_idx;
207
1.43k
        if (!drl_idx) return;
208
1.43k
      }
209
5.46k
    }
210
3.26k
  }
211
2.81k
  if (have_nearmv_in_inter_mode(mbmi->mode)) {
212
    // Offset the NEARESTMV mode.
213
    // TODO(jingning): Unify the two syntax decoding loops after the NEARESTMV
214
    // mode is factored in.
215
1.86k
    for (int idx = 1; idx < 3; ++idx) {
216
1.25k
      if (dcb->ref_mv_count[ref_frame_type] > idx + 1) {
217
69
        uint8_t drl_ctx = av1_drl_ctx(xd->weight[ref_frame_type], idx);
218
69
        int drl_idx = aom_read_symbol(r, ec_ctx->drl_cdf[drl_ctx], 2, ACCT_STR);
219
69
        mbmi->ref_mv_idx = idx + drl_idx - 1;
220
69
        if (!drl_idx) return;
221
69
      }
222
1.25k
    }
223
641
  }
224
2.81k
}
225
226
static MOTION_MODE read_motion_mode(AV1_COMMON *cm, MACROBLOCKD *xd,
227
7.10k
                                    MB_MODE_INFO *mbmi, aom_reader *r) {
228
7.10k
  if (cm->features.switchable_motion_mode == 0) return SIMPLE_TRANSLATION;
229
3.76k
  if (mbmi->skip_mode) return SIMPLE_TRANSLATION;
230
231
3.76k
  const MOTION_MODE last_motion_mode_allowed = motion_mode_allowed(
232
3.76k
      xd->global_motion, xd, mbmi, cm->features.allow_warped_motion);
233
3.76k
  int motion_mode;
234
235
3.76k
  if (last_motion_mode_allowed == SIMPLE_TRANSLATION) return SIMPLE_TRANSLATION;
236
237
97
  if (last_motion_mode_allowed == OBMC_CAUSAL) {
238
97
    motion_mode =
239
97
        aom_read_symbol(r, xd->tile_ctx->obmc_cdf[mbmi->bsize], 2, ACCT_STR);
240
97
    return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
241
97
  } else {
242
0
    motion_mode = aom_read_symbol(r, xd->tile_ctx->motion_mode_cdf[mbmi->bsize],
243
0
                                  MOTION_MODES, ACCT_STR);
244
0
    return (MOTION_MODE)(SIMPLE_TRANSLATION + motion_mode);
245
0
  }
246
97
}
247
248
static PREDICTION_MODE read_inter_compound_mode(MACROBLOCKD *xd, aom_reader *r,
249
1.16k
                                                int16_t ctx) {
250
1.16k
  const int mode =
251
1.16k
      aom_read_symbol(r, xd->tile_ctx->inter_compound_mode_cdf[ctx],
252
1.16k
                      INTER_COMPOUND_MODES, ACCT_STR);
253
1.16k
  assert(is_inter_compound_mode(NEAREST_NEARESTMV + mode));
254
1.16k
  return NEAREST_NEARESTMV + mode;
255
1.16k
}
256
257
1.68M
int av1_neg_deinterleave(int diff, int ref, int max) {
258
1.68M
  if (!ref) return diff;
259
1.33M
  if (ref >= (max - 1)) return max - diff - 1;
260
1.15M
  if (2 * ref < max) {
261
688k
    if (diff <= 2 * ref) {
262
591k
      if (diff & 1)
263
82.2k
        return ref + ((diff + 1) >> 1);
264
509k
      else
265
509k
        return ref - (diff >> 1);
266
591k
    }
267
96.4k
    return diff;
268
688k
  } else {
269
471k
    if (diff <= 2 * (max - ref - 1)) {
270
414k
      if (diff & 1)
271
64.2k
        return ref + ((diff + 1) >> 1);
272
350k
      else
273
350k
        return ref - (diff >> 1);
274
414k
    }
275
56.6k
    return max - (diff + 1);
276
471k
  }
277
1.15M
}
278
279
static int read_segment_id(AV1_COMMON *const cm, const MACROBLOCKD *const xd,
280
1.68M
                           aom_reader *r, int skip) {
281
1.68M
  int cdf_num;
282
1.68M
  const int pred = av1_get_spatial_seg_pred(cm, xd, &cdf_num);
283
1.68M
  if (skip) return pred;
284
285
1.68M
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
286
1.68M
  struct segmentation *const seg = &cm->seg;
287
1.68M
  struct segmentation_probs *const segp = &ec_ctx->seg;
288
1.68M
  aom_cdf_prob *pred_cdf = segp->spatial_pred_seg_cdf[cdf_num];
289
1.68M
  const int coded_id = aom_read_symbol(r, pred_cdf, MAX_SEGMENTS, ACCT_STR);
290
1.68M
  const int segment_id =
291
1.68M
      av1_neg_deinterleave(coded_id, pred, seg->last_active_segid + 1);
292
293
1.68M
  if (segment_id < 0 || segment_id > seg->last_active_segid) {
294
131
    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
295
131
                       "Corrupted segment_ids");
296
131
  }
297
1.68M
  return segment_id;
298
1.68M
}
299
300
static int dec_get_segment_id(const AV1_COMMON *cm, const uint8_t *segment_ids,
301
1.06k
                              int mi_offset, int x_mis, int y_mis) {
302
1.06k
  int segment_id = INT_MAX;
303
304
2.30k
  for (int y = 0; y < y_mis; y++)
305
2.84k
    for (int x = 0; x < x_mis; x++)
306
1.60k
      segment_id = AOMMIN(
307
1.06k
          segment_id, segment_ids[mi_offset + y * cm->mi_params.mi_cols + x]);
308
309
1.06k
  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
310
1.06k
  return segment_id;
311
1.06k
}
312
313
static void set_segment_id(AV1_COMMON *cm, int mi_offset, int x_mis, int y_mis,
314
1.68M
                           int segment_id) {
315
1.68M
  assert(segment_id >= 0 && segment_id < MAX_SEGMENTS);
316
317
10.4M
  for (int y = 0; y < y_mis; y++)
318
90.6M
    for (int x = 0; x < x_mis; x++)
319
81.8M
      cm->cur_frame->seg_map[mi_offset + y * cm->mi_params.mi_cols + x] =
320
81.8M
          segment_id;
321
1.68M
}
322
323
static int read_intra_segment_id(AV1_COMMON *const cm,
324
                                 const MACROBLOCKD *const xd, int bsize,
325
8.13M
                                 aom_reader *r, int skip) {
326
8.13M
  struct segmentation *const seg = &cm->seg;
327
8.13M
  if (!seg->enabled) return 0;  // Default for disabled segmentation
328
1.68M
  assert(seg->update_map && !seg->temporal_update);
329
330
1.68M
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
331
1.68M
  const int mi_row = xd->mi_row;
332
1.68M
  const int mi_col = xd->mi_col;
333
1.68M
  const int mi_offset = mi_row * mi_params->mi_cols + mi_col;
334
1.68M
  const int bw = mi_size_wide[bsize];
335
1.68M
  const int bh = mi_size_high[bsize];
336
1.68M
  const int x_mis = AOMMIN(mi_params->mi_cols - mi_col, bw);
337
1.68M
  const int y_mis = AOMMIN(mi_params->mi_rows - mi_row, bh);
338
1.68M
  const int segment_id = read_segment_id(cm, xd, r, skip);
339
1.68M
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
340
1.68M
  return segment_id;
341
1.68M
}
342
343
static void copy_segment_id(const CommonModeInfoParams *const mi_params,
344
                            const uint8_t *last_segment_ids,
345
                            uint8_t *current_segment_ids, int mi_offset,
346
756
                            int x_mis, int y_mis) {
347
1.68k
  for (int y = 0; y < y_mis; y++)
348
2.22k
    for (int x = 0; x < x_mis; x++)
349
1.29k
      current_segment_ids[mi_offset + y * mi_params->mi_cols + x] =
350
1.29k
          last_segment_ids
351
1.29k
              ? last_segment_ids[mi_offset + y * mi_params->mi_cols + x]
352
1.29k
              : 0;
353
756
}
354
355
static int get_predicted_segment_id(AV1_COMMON *const cm, int mi_offset,
356
1.22k
                                    int x_mis, int y_mis) {
357
1.22k
  return cm->last_frame_seg_map ? dec_get_segment_id(cm, cm->last_frame_seg_map,
358
1.06k
                                                     mi_offset, x_mis, y_mis)
359
1.22k
                                : 0;
360
1.22k
}
361
362
static int read_inter_segment_id(AV1_COMMON *const cm, MACROBLOCKD *const xd,
363
12.6k
                                 int preskip, aom_reader *r) {
364
12.6k
  struct segmentation *const seg = &cm->seg;
365
12.6k
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
366
12.6k
  MB_MODE_INFO *const mbmi = xd->mi[0];
367
12.6k
  const int mi_row = xd->mi_row;
368
12.6k
  const int mi_col = xd->mi_col;
369
12.6k
  const int mi_offset = mi_row * mi_params->mi_cols + mi_col;
370
12.6k
  const int bw = mi_size_wide[mbmi->bsize];
371
12.6k
  const int bh = mi_size_high[mbmi->bsize];
372
373
  // TODO(slavarnway): move x_mis, y_mis into xd ?????
374
12.6k
  const int x_mis = AOMMIN(mi_params->mi_cols - mi_col, bw);
375
12.6k
  const int y_mis = AOMMIN(mi_params->mi_rows - mi_row, bh);
376
377
12.6k
  if (!seg->enabled) return 0;  // Default for disabled segmentation
378
379
3.80k
  if (!seg->update_map) {
380
756
    copy_segment_id(mi_params, cm->last_frame_seg_map, cm->cur_frame->seg_map,
381
756
                    mi_offset, x_mis, y_mis);
382
756
    return get_predicted_segment_id(cm, mi_offset, x_mis, y_mis);
383
756
  }
384
385
3.04k
  int segment_id;
386
3.04k
  if (preskip) {
387
2.74k
    if (!seg->segid_preskip) return 0;
388
2.74k
  } else {
389
303
    if (mbmi->skip_txfm) {
390
127
      if (seg->temporal_update) {
391
68
        mbmi->seg_id_predicted = 0;
392
68
      }
393
127
      segment_id = read_segment_id(cm, xd, r, 1);
394
127
      set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
395
127
      return segment_id;
396
127
    }
397
303
  }
398
399
2.61k
  if (seg->temporal_update) {
400
762
    const int ctx = av1_get_pred_context_seg_id(xd);
401
762
    FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
402
762
    struct segmentation_probs *const segp = &ec_ctx->seg;
403
762
    aom_cdf_prob *pred_cdf = segp->pred_cdf[ctx];
404
762
    mbmi->seg_id_predicted = aom_read_symbol(r, pred_cdf, 2, ACCT_STR);
405
762
    if (mbmi->seg_id_predicted) {
406
465
      segment_id = get_predicted_segment_id(cm, mi_offset, x_mis, y_mis);
407
465
    } else {
408
297
      segment_id = read_segment_id(cm, xd, r, 0);
409
297
    }
410
1.85k
  } else {
411
1.85k
    segment_id = read_segment_id(cm, xd, r, 0);
412
1.85k
  }
413
2.61k
  set_segment_id(cm, mi_offset, x_mis, y_mis, segment_id);
414
2.61k
  return segment_id;
415
3.04k
}
416
417
static int read_skip_mode(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
418
7.73k
                          aom_reader *r) {
419
7.73k
  if (!cm->current_frame.skip_mode_info.skip_mode_flag) return 0;
420
421
1.05k
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
422
600
    return 0;
423
600
  }
424
425
454
  if (!is_comp_ref_allowed(xd->mi[0]->bsize)) return 0;
426
427
86
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME) ||
428
86
      segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
429
    // These features imply single-reference mode, while skip mode implies
430
    // compound reference. Hence, the two are mutually exclusive.
431
    // In other words, skip_mode is implicitly 0 here.
432
7
    return 0;
433
7
  }
434
435
79
  const int ctx = av1_get_skip_mode_context(xd);
436
79
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
437
79
  const int skip_mode =
438
79
      aom_read_symbol(r, ec_ctx->skip_mode_cdfs[ctx], 2, ACCT_STR);
439
79
  return skip_mode;
440
86
}
441
442
static int read_skip_txfm(AV1_COMMON *cm, const MACROBLOCKD *xd, int segment_id,
443
8.13M
                          aom_reader *r) {
444
8.13M
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP)) {
445
798k
    return 1;
446
7.34M
  } else {
447
7.34M
    const int ctx = av1_get_skip_txfm_context(xd);
448
7.34M
    FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
449
7.34M
    const int skip_txfm =
450
7.34M
        aom_read_symbol(r, ec_ctx->skip_txfm_cdfs[ctx], 2, ACCT_STR);
451
7.34M
    return skip_txfm;
452
7.34M
  }
453
8.13M
}
454
455
// Merge the sorted list of cached colors(cached_colors[0...n_cached_colors-1])
456
// and the sorted list of transmitted colors(colors[n_cached_colors...n-1]) into
457
// one single sorted list(colors[...]).
458
static void merge_colors(uint16_t *colors, uint16_t *cached_colors,
459
77.4k
                         int n_colors, int n_cached_colors) {
460
77.4k
  if (n_cached_colors == 0) return;
461
30.9k
  int cache_idx = 0, trans_idx = n_cached_colors;
462
192k
  for (int i = 0; i < n_colors; ++i) {
463
161k
    if (cache_idx < n_cached_colors &&
464
161k
        (trans_idx >= n_colors ||
465
112k
         cached_colors[cache_idx] <= colors[trans_idx])) {
466
63.1k
      colors[i] = cached_colors[cache_idx++];
467
97.9k
    } else {
468
97.9k
      assert(trans_idx < n_colors);
469
97.9k
      colors[i] = colors[trans_idx++];
470
97.9k
    }
471
161k
  }
472
30.9k
}
473
474
static void read_palette_colors_y(MACROBLOCKD *const xd, int bit_depth,
475
64.3k
                                  PALETTE_MODE_INFO *const pmi, aom_reader *r) {
476
64.3k
  uint16_t color_cache[2 * PALETTE_MAX_SIZE];
477
64.3k
  uint16_t cached_colors[PALETTE_MAX_SIZE];
478
64.3k
  const int n_cache = av1_get_palette_cache(xd, 0, color_cache);
479
64.3k
  const int n = pmi->palette_size[0];
480
64.3k
  int idx = 0;
481
244k
  for (int i = 0; i < n_cache && idx < n; ++i)
482
180k
    if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
483
64.3k
  if (idx < n) {
484
52.9k
    const int n_cached_colors = idx;
485
52.9k
    pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
486
52.9k
    if (idx < n) {
487
46.3k
      const int min_bits = bit_depth - 3;
488
46.3k
      int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
489
46.3k
      int range = (1 << bit_depth) - pmi->palette_colors[idx - 1] - 1;
490
194k
      for (; idx < n; ++idx) {
491
148k
        assert(range >= 0);
492
148k
        const int delta = aom_read_literal(r, bits, ACCT_STR) + 1;
493
148k
        pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
494
148k
                                         0, (1 << bit_depth) - 1);
495
148k
        range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
496
148k
        bits = AOMMIN(bits, av1_ceil_log2(range));
497
148k
      }
498
46.3k
    }
499
52.9k
    merge_colors(pmi->palette_colors, cached_colors, n, n_cached_colors);
500
52.9k
  } else {
501
11.3k
    memcpy(pmi->palette_colors, cached_colors, n * sizeof(cached_colors[0]));
502
11.3k
  }
503
64.3k
}
504
505
static void read_palette_colors_uv(MACROBLOCKD *const xd, int bit_depth,
506
                                   PALETTE_MODE_INFO *const pmi,
507
25.3k
                                   aom_reader *r) {
508
25.3k
  const int n = pmi->palette_size[1];
509
  // U channel colors.
510
25.3k
  uint16_t color_cache[2 * PALETTE_MAX_SIZE];
511
25.3k
  uint16_t cached_colors[PALETTE_MAX_SIZE];
512
25.3k
  const int n_cache = av1_get_palette_cache(xd, 1, color_cache);
513
25.3k
  int idx = 0;
514
40.1k
  for (int i = 0; i < n_cache && idx < n; ++i)
515
14.8k
    if (aom_read_bit(r, ACCT_STR)) cached_colors[idx++] = color_cache[i];
516
25.3k
  if (idx < n) {
517
24.4k
    const int n_cached_colors = idx;
518
24.4k
    idx += PALETTE_MAX_SIZE;
519
24.4k
    pmi->palette_colors[idx++] = aom_read_literal(r, bit_depth, ACCT_STR);
520
24.4k
    if (idx < PALETTE_MAX_SIZE + n) {
521
23.5k
      const int min_bits = bit_depth - 3;
522
23.5k
      int bits = min_bits + aom_read_literal(r, 2, ACCT_STR);
523
23.5k
      int range = (1 << bit_depth) - pmi->palette_colors[idx - 1];
524
94.4k
      for (; idx < PALETTE_MAX_SIZE + n; ++idx) {
525
70.8k
        assert(range >= 0);
526
70.8k
        const int delta = aom_read_literal(r, bits, ACCT_STR);
527
70.8k
        pmi->palette_colors[idx] = clamp(pmi->palette_colors[idx - 1] + delta,
528
70.8k
                                         0, (1 << bit_depth) - 1);
529
70.8k
        range -= (pmi->palette_colors[idx] - pmi->palette_colors[idx - 1]);
530
70.8k
        bits = AOMMIN(bits, av1_ceil_log2(range));
531
70.8k
      }
532
23.5k
    }
533
24.4k
    merge_colors(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors, n,
534
24.4k
                 n_cached_colors);
535
24.4k
  } else {
536
961
    memcpy(pmi->palette_colors + PALETTE_MAX_SIZE, cached_colors,
537
961
           n * sizeof(cached_colors[0]));
538
961
  }
539
540
  // V channel colors.
541
25.3k
  if (aom_read_bit(r, ACCT_STR)) {  // Delta encoding.
542
12.9k
    const int min_bits_v = bit_depth - 4;
543
12.9k
    const int max_val = 1 << bit_depth;
544
12.9k
    int bits = min_bits_v + aom_read_literal(r, 2, ACCT_STR);
545
12.9k
    pmi->palette_colors[2 * PALETTE_MAX_SIZE] =
546
12.9k
        aom_read_literal(r, bit_depth, ACCT_STR);
547
52.5k
    for (int i = 1; i < n; ++i) {
548
39.5k
      int delta = aom_read_literal(r, bits, ACCT_STR);
549
39.5k
      if (delta && aom_read_bit(r, ACCT_STR)) delta = -delta;
550
39.5k
      int val = (int)pmi->palette_colors[2 * PALETTE_MAX_SIZE + i - 1] + delta;
551
39.5k
      if (val < 0) val += max_val;
552
39.5k
      if (val >= max_val) val -= max_val;
553
39.5k
      pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] = val;
554
39.5k
    }
555
12.9k
  } else {
556
62.5k
    for (int i = 0; i < n; ++i) {
557
50.1k
      pmi->palette_colors[2 * PALETTE_MAX_SIZE + i] =
558
50.1k
          aom_read_literal(r, bit_depth, ACCT_STR);
559
50.1k
    }
560
12.4k
  }
561
25.3k
}
562
563
static void read_palette_mode_info(AV1_COMMON *const cm, MACROBLOCKD *const xd,
564
3.06M
                                   aom_reader *r) {
565
3.06M
  const int num_planes = av1_num_planes(cm);
566
3.06M
  MB_MODE_INFO *const mbmi = xd->mi[0];
567
3.06M
  const BLOCK_SIZE bsize = mbmi->bsize;
568
3.06M
  assert(av1_allow_palette(cm->features.allow_screen_content_tools, bsize));
569
3.06M
  PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
570
3.06M
  const int bsize_ctx = av1_get_palette_bsize_ctx(bsize);
571
572
3.06M
  if (mbmi->mode == DC_PRED) {
573
1.03M
    const int palette_mode_ctx = av1_get_palette_mode_ctx(xd);
574
1.03M
    const int modev = aom_read_symbol(
575
1.03M
        r, xd->tile_ctx->palette_y_mode_cdf[bsize_ctx][palette_mode_ctx], 2,
576
1.03M
        ACCT_STR);
577
1.03M
    if (modev) {
578
64.3k
      pmi->palette_size[0] =
579
64.3k
          aom_read_symbol(r, xd->tile_ctx->palette_y_size_cdf[bsize_ctx],
580
64.3k
                          PALETTE_SIZES, ACCT_STR) +
581
64.3k
          2;
582
64.3k
      read_palette_colors_y(xd, cm->seq_params->bit_depth, pmi, r);
583
64.3k
    }
584
1.03M
  }
585
3.06M
  if (num_planes > 1 && mbmi->uv_mode == UV_DC_PRED && xd->is_chroma_ref) {
586
664k
    const int palette_uv_mode_ctx = (pmi->palette_size[0] > 0);
587
664k
    const int modev = aom_read_symbol(
588
664k
        r, xd->tile_ctx->palette_uv_mode_cdf[palette_uv_mode_ctx], 2, ACCT_STR);
589
664k
    if (modev) {
590
25.3k
      pmi->palette_size[1] =
591
25.3k
          aom_read_symbol(r, xd->tile_ctx->palette_uv_size_cdf[bsize_ctx],
592
25.3k
                          PALETTE_SIZES, ACCT_STR) +
593
25.3k
          2;
594
25.3k
      read_palette_colors_uv(xd, cm->seq_params->bit_depth, pmi, r);
595
25.3k
    }
596
664k
  }
597
3.06M
}
598
599
3.35M
static int read_angle_delta(aom_reader *r, aom_cdf_prob *cdf) {
600
3.35M
  const int sym = aom_read_symbol(r, cdf, 2 * MAX_ANGLE_DELTA + 1, ACCT_STR);
601
3.35M
  return sym - MAX_ANGLE_DELTA;
602
3.35M
}
603
604
static void read_filter_intra_mode_info(const AV1_COMMON *const cm,
605
8.06M
                                        MACROBLOCKD *const xd, aom_reader *r) {
606
8.06M
  MB_MODE_INFO *const mbmi = xd->mi[0];
607
8.06M
  FILTER_INTRA_MODE_INFO *filter_intra_mode_info =
608
8.06M
      &mbmi->filter_intra_mode_info;
609
610
8.06M
  if (av1_filter_intra_allowed(cm, mbmi)) {
611
2.14M
    filter_intra_mode_info->use_filter_intra = aom_read_symbol(
612
2.14M
        r, xd->tile_ctx->filter_intra_cdfs[mbmi->bsize], 2, ACCT_STR);
613
2.14M
    if (filter_intra_mode_info->use_filter_intra) {
614
1.42M
      filter_intra_mode_info->filter_intra_mode = aom_read_symbol(
615
1.42M
          r, xd->tile_ctx->filter_intra_mode_cdf, FILTER_INTRA_MODES, ACCT_STR);
616
1.42M
    }
617
5.92M
  } else {
618
5.92M
    filter_intra_mode_info->use_filter_intra = 0;
619
5.92M
  }
620
8.06M
}
621
622
void av1_read_tx_type(const AV1_COMMON *const cm, MACROBLOCKD *xd, int blk_row,
623
4.17M
                      int blk_col, TX_SIZE tx_size, aom_reader *r) {
624
4.17M
  MB_MODE_INFO *mbmi = xd->mi[0];
625
4.17M
  uint8_t *tx_type =
626
4.17M
      &xd->tx_type_map[blk_row * xd->tx_type_map_stride + blk_col];
627
4.17M
  *tx_type = DCT_DCT;
628
629
  // No need to read transform type if block is skipped.
630
4.17M
  if (mbmi->skip_txfm ||
631
4.17M
      segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP))
632
0
    return;
633
634
  // No need to read transform type for lossless mode(qindex==0).
635
4.17M
  const int qindex = xd->qindex[mbmi->segment_id];
636
4.17M
  if (qindex == 0) return;
637
638
3.36M
  const int inter_block = is_inter_block(mbmi);
639
3.36M
  if (get_ext_tx_types(tx_size, inter_block, cm->features.reduced_tx_set_used) >
640
3.36M
      1) {
641
2.83M
    const TxSetType tx_set_type = av1_get_ext_tx_set_type(
642
2.83M
        tx_size, inter_block, cm->features.reduced_tx_set_used);
643
2.83M
    const int eset =
644
2.83M
        get_ext_tx_set(tx_size, inter_block, cm->features.reduced_tx_set_used);
645
    // eset == 0 should correspond to a set with only DCT_DCT and
646
    // there is no need to read the tx_type
647
2.83M
    assert(eset != 0);
648
649
2.83M
    const TX_SIZE square_tx_size = txsize_sqr_map[tx_size];
650
2.83M
    FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
651
2.83M
    if (inter_block) {
652
42.4k
      *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
653
42.4k
          r, ec_ctx->inter_ext_tx_cdf[eset][square_tx_size],
654
42.4k
          av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
655
2.78M
    } else {
656
2.78M
      const PREDICTION_MODE intra_mode =
657
2.78M
          mbmi->filter_intra_mode_info.use_filter_intra
658
2.78M
              ? fimode_to_intradir[mbmi->filter_intra_mode_info
659
487k
                                       .filter_intra_mode]
660
2.78M
              : mbmi->mode;
661
2.78M
      *tx_type = av1_ext_tx_inv[tx_set_type][aom_read_symbol(
662
2.78M
          r, ec_ctx->intra_ext_tx_cdf[eset][square_tx_size][intra_mode],
663
2.78M
          av1_num_ext_tx_set[tx_set_type], ACCT_STR)];
664
2.78M
    }
665
2.83M
  }
666
3.36M
}
667
668
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
669
                           nmv_context *ctx, MvSubpelPrecision precision);
670
671
static INLINE int is_mv_valid(const MV *mv);
672
673
static INLINE int assign_dv(AV1_COMMON *cm, MACROBLOCKD *xd, int_mv *mv,
674
                            const int_mv *ref_mv, int mi_row, int mi_col,
675
63.5k
                            BLOCK_SIZE bsize, aom_reader *r) {
676
63.5k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
677
63.5k
  read_mv(r, &mv->as_mv, &ref_mv->as_mv, &ec_ctx->ndvc, MV_SUBPEL_NONE);
678
  // DV should not have sub-pel.
679
63.5k
  assert((mv->as_mv.col & 7) == 0);
680
63.5k
  assert((mv->as_mv.row & 7) == 0);
681
63.5k
  mv->as_mv.col = (mv->as_mv.col >> 3) * 8;
682
63.5k
  mv->as_mv.row = (mv->as_mv.row >> 3) * 8;
683
63.5k
  int valid = is_mv_valid(&mv->as_mv) &&
684
63.5k
              av1_is_dv_valid(mv->as_mv, cm, xd, mi_row, mi_col, bsize,
685
63.5k
                              cm->seq_params->mib_size_log2);
686
63.5k
  return valid;
687
63.5k
}
688
689
static void read_intrabc_info(AV1_COMMON *const cm, DecoderCodingBlock *dcb,
690
686k
                              aom_reader *r) {
691
686k
  MACROBLOCKD *const xd = &dcb->xd;
692
686k
  MB_MODE_INFO *const mbmi = xd->mi[0];
693
686k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
694
686k
  mbmi->use_intrabc = aom_read_symbol(r, ec_ctx->intrabc_cdf, 2, ACCT_STR);
695
686k
  if (mbmi->use_intrabc) {
696
63.5k
    BLOCK_SIZE bsize = mbmi->bsize;
697
63.5k
    mbmi->mode = DC_PRED;
698
63.5k
    mbmi->uv_mode = UV_DC_PRED;
699
63.5k
    mbmi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
700
63.5k
    mbmi->motion_mode = SIMPLE_TRANSLATION;
701
702
63.5k
    int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
703
63.5k
    int_mv ref_mvs[INTRA_FRAME + 1][MAX_MV_REF_CANDIDATES];
704
705
63.5k
    av1_find_mv_refs(cm, xd, mbmi, INTRA_FRAME, dcb->ref_mv_count,
706
63.5k
                     xd->ref_mv_stack, xd->weight, ref_mvs, /*global_mvs=*/NULL,
707
63.5k
                     inter_mode_ctx);
708
709
63.5k
    int_mv nearestmv, nearmv;
710
711
63.5k
    av1_find_best_ref_mvs(0, ref_mvs[INTRA_FRAME], &nearestmv, &nearmv, 0);
712
63.5k
    int_mv dv_ref = nearestmv.as_int == 0 ? nearmv : nearestmv;
713
63.5k
    if (dv_ref.as_int == 0)
714
11.0k
      av1_find_ref_dv(&dv_ref, &xd->tile, cm->seq_params->mib_size, xd->mi_row);
715
    // Ref DV should not have sub-pel.
716
63.5k
    int valid_dv = (dv_ref.as_mv.col & 7) == 0 && (dv_ref.as_mv.row & 7) == 0;
717
63.5k
    dv_ref.as_mv.col = (dv_ref.as_mv.col >> 3) * 8;
718
63.5k
    dv_ref.as_mv.row = (dv_ref.as_mv.row >> 3) * 8;
719
63.5k
    valid_dv = valid_dv && assign_dv(cm, xd, &mbmi->mv[0], &dv_ref, xd->mi_row,
720
63.5k
                                     xd->mi_col, bsize, r);
721
63.5k
    if (!valid_dv) {
722
      // Intra bc motion vectors are not valid - signal corrupt frame
723
810
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
724
810
                         "Invalid intrabc dv");
725
810
    }
726
63.5k
  }
727
686k
}
728
729
// If delta q is present, reads delta_q index.
730
// Also reads delta_q loop filter levels, if present.
731
static void read_delta_q_params(AV1_COMMON *const cm, MACROBLOCKD *const xd,
732
8.13M
                                aom_reader *r) {
733
8.13M
  DeltaQInfo *const delta_q_info = &cm->delta_q_info;
734
735
8.13M
  if (delta_q_info->delta_q_present_flag) {
736
1.58M
    MB_MODE_INFO *const mbmi = xd->mi[0];
737
1.58M
    xd->current_base_qindex +=
738
1.58M
        read_delta_qindex(cm, xd, r, mbmi) * delta_q_info->delta_q_res;
739
    /* Normative: Clamp to [1,MAXQ] to not interfere with lossless mode */
740
1.58M
    xd->current_base_qindex = clamp(xd->current_base_qindex, 1, MAXQ);
741
1.58M
    FRAME_CONTEXT *const ec_ctx = xd->tile_ctx;
742
1.58M
    if (delta_q_info->delta_lf_present_flag) {
743
815k
      const int mi_row = xd->mi_row;
744
815k
      const int mi_col = xd->mi_col;
745
815k
      if (delta_q_info->delta_lf_multi) {
746
729k
        const int frame_lf_count =
747
729k
            av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
748
3.64M
        for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
749
2.91M
          const int tmp_lvl =
750
2.91M
              xd->delta_lf[lf_id] +
751
2.91M
              read_delta_lflevel(cm, r, ec_ctx->delta_lf_multi_cdf[lf_id], mbmi,
752
2.91M
                                 mi_col, mi_row) *
753
2.91M
                  delta_q_info->delta_lf_res;
754
2.91M
          mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id] =
755
2.91M
              clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
756
2.91M
        }
757
729k
      } else {
758
85.1k
        const int tmp_lvl = xd->delta_lf_from_base +
759
85.1k
                            read_delta_lflevel(cm, r, ec_ctx->delta_lf_cdf,
760
85.1k
                                               mbmi, mi_col, mi_row) *
761
85.1k
                                delta_q_info->delta_lf_res;
762
85.1k
        mbmi->delta_lf_from_base = xd->delta_lf_from_base =
763
85.1k
            clamp(tmp_lvl, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
764
85.1k
      }
765
815k
    }
766
1.58M
  }
767
8.13M
}
768
769
static void read_intra_frame_mode_info(AV1_COMMON *const cm,
770
8.13M
                                       DecoderCodingBlock *dcb, aom_reader *r) {
771
8.13M
  MACROBLOCKD *const xd = &dcb->xd;
772
8.13M
  MB_MODE_INFO *const mbmi = xd->mi[0];
773
8.13M
  const MB_MODE_INFO *above_mi = xd->above_mbmi;
774
8.13M
  const MB_MODE_INFO *left_mi = xd->left_mbmi;
775
8.13M
  const BLOCK_SIZE bsize = mbmi->bsize;
776
8.13M
  struct segmentation *const seg = &cm->seg;
777
778
8.13M
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
779
780
8.13M
  if (seg->segid_preskip)
781
1.67M
    mbmi->segment_id = read_intra_segment_id(cm, xd, bsize, r, 0);
782
783
8.13M
  mbmi->skip_txfm = read_skip_txfm(cm, xd, mbmi->segment_id, r);
784
785
8.13M
  if (!seg->segid_preskip)
786
6.45M
    mbmi->segment_id = read_intra_segment_id(cm, xd, bsize, r, mbmi->skip_txfm);
787
788
8.13M
  read_cdef(cm, r, xd);
789
790
8.13M
  read_delta_q_params(cm, xd, r);
791
792
8.13M
  mbmi->current_qindex = xd->current_base_qindex;
793
794
8.13M
  mbmi->ref_frame[0] = INTRA_FRAME;
795
8.13M
  mbmi->ref_frame[1] = NONE_FRAME;
796
8.13M
  mbmi->palette_mode_info.palette_size[0] = 0;
797
8.13M
  mbmi->palette_mode_info.palette_size[1] = 0;
798
8.13M
  mbmi->filter_intra_mode_info.use_filter_intra = 0;
799
800
8.13M
  const int mi_row = xd->mi_row;
801
8.13M
  const int mi_col = xd->mi_col;
802
8.13M
  xd->above_txfm_context = cm->above_contexts.txfm[xd->tile.tile_row] + mi_col;
803
8.13M
  xd->left_txfm_context =
804
8.13M
      xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
805
806
8.13M
  if (av1_allow_intrabc(cm)) {
807
686k
    read_intrabc_info(cm, dcb, r);
808
686k
    if (is_intrabc_block(mbmi)) return;
809
686k
  }
810
811
8.06M
  mbmi->mode = read_intra_mode(r, get_y_mode_cdf(ec_ctx, above_mi, left_mi));
812
813
8.06M
  const int use_angle_delta = av1_use_angle_delta(bsize);
814
8.06M
  mbmi->angle_delta[PLANE_TYPE_Y] =
815
8.06M
      (use_angle_delta && av1_is_directional_mode(mbmi->mode))
816
8.06M
          ? read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED])
817
8.06M
          : 0;
818
819
8.06M
  if (!cm->seq_params->monochrome && xd->is_chroma_ref) {
820
7.31M
    mbmi->uv_mode =
821
7.31M
        read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode);
822
7.31M
    if (mbmi->uv_mode == UV_CFL_PRED) {
823
1.80M
      mbmi->cfl_alpha_idx = read_cfl_alphas(ec_ctx, r, &mbmi->cfl_alpha_signs);
824
1.80M
    }
825
7.31M
    mbmi->angle_delta[PLANE_TYPE_UV] =
826
7.31M
        (use_angle_delta && av1_is_directional_mode(get_uv_mode(mbmi->uv_mode)))
827
7.31M
            ? read_angle_delta(r,
828
1.42M
                               ec_ctx->angle_delta_cdf[mbmi->uv_mode - V_PRED])
829
7.31M
            : 0;
830
7.31M
  } else {
831
    // Avoid decoding angle_info if there is is no chroma prediction
832
752k
    mbmi->uv_mode = UV_DC_PRED;
833
752k
  }
834
8.06M
  xd->cfl.store_y = store_cfl_required(cm, xd);
835
836
8.06M
  if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize))
837
3.06M
    read_palette_mode_info(cm, xd, r);
838
839
8.06M
  read_filter_intra_mode_info(cm, xd, r);
840
8.06M
}
841
842
static int read_mv_component(aom_reader *r, nmv_component *mvcomp,
843
80.1k
                             int use_subpel, int usehp) {
844
80.1k
  int mag, d, fr, hp;
845
80.1k
  const int sign = aom_read_symbol(r, mvcomp->sign_cdf, 2, ACCT_STR);
846
80.1k
  const int mv_class =
847
80.1k
      aom_read_symbol(r, mvcomp->classes_cdf, MV_CLASSES, ACCT_STR);
848
80.1k
  const int class0 = mv_class == MV_CLASS_0;
849
850
  // Integer part
851
80.1k
  if (class0) {
852
69.8k
    d = aom_read_symbol(r, mvcomp->class0_cdf, CLASS0_SIZE, ACCT_STR);
853
69.8k
    mag = 0;
854
69.8k
  } else {
855
10.3k
    const int n = mv_class + CLASS0_BITS - 1;  // number of bits
856
10.3k
    d = 0;
857
30.0k
    for (int i = 0; i < n; ++i)
858
19.7k
      d |= aom_read_symbol(r, mvcomp->bits_cdf[i], 2, ACCT_STR) << i;
859
10.3k
    mag = CLASS0_SIZE << (mv_class + 2);
860
10.3k
  }
861
862
80.1k
  if (use_subpel) {
863
    // Fractional part
864
3.83k
    fr = aom_read_symbol(r, class0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
865
3.83k
                         MV_FP_SIZE, ACCT_STR);
866
867
    // High precision part (if hp is not used, the default value of the hp is 1)
868
3.83k
    hp = usehp ? aom_read_symbol(
869
3.83k
                     r, class0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf, 2,
870
3.83k
                     ACCT_STR)
871
3.83k
               : 1;
872
76.3k
  } else {
873
76.3k
    fr = 3;
874
76.3k
    hp = 1;
875
76.3k
  }
876
877
  // Result
878
80.1k
  mag += ((d << 3) | (fr << 1) | hp) + 1;
879
80.1k
  return sign ? -mag : mag;
880
80.1k
}
881
882
static INLINE void read_mv(aom_reader *r, MV *mv, const MV *ref,
883
67.6k
                           nmv_context *ctx, MvSubpelPrecision precision) {
884
67.6k
  MV diff = kZeroMv;
885
67.6k
  const MV_JOINT_TYPE joint_type =
886
67.6k
      (MV_JOINT_TYPE)aom_read_symbol(r, ctx->joints_cdf, MV_JOINTS, ACCT_STR);
887
888
67.6k
  if (mv_joint_vertical(joint_type))
889
38.8k
    diff.row = read_mv_component(r, &ctx->comps[0], precision > MV_SUBPEL_NONE,
890
38.8k
                                 precision > MV_SUBPEL_LOW_PRECISION);
891
892
67.6k
  if (mv_joint_horizontal(joint_type))
893
41.3k
    diff.col = read_mv_component(r, &ctx->comps[1], precision > MV_SUBPEL_NONE,
894
41.3k
                                 precision > MV_SUBPEL_LOW_PRECISION);
895
896
67.6k
  mv->row = ref->row + diff.row;
897
67.6k
  mv->col = ref->col + diff.col;
898
67.6k
}
899
900
static REFERENCE_MODE read_block_reference_mode(AV1_COMMON *cm,
901
                                                const MACROBLOCKD *xd,
902
5.36k
                                                aom_reader *r) {
903
5.36k
  if (!is_comp_ref_allowed(xd->mi[0]->bsize)) return SINGLE_REFERENCE;
904
1.52k
  if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) {
905
1.36k
    const int ctx = av1_get_reference_mode_context(xd);
906
1.36k
    const REFERENCE_MODE mode = (REFERENCE_MODE)aom_read_symbol(
907
1.36k
        r, xd->tile_ctx->comp_inter_cdf[ctx], 2, ACCT_STR);
908
1.36k
    return mode;  // SINGLE_REFERENCE or COMPOUND_REFERENCE
909
1.36k
  } else {
910
158
    assert(cm->current_frame.reference_mode == SINGLE_REFERENCE);
911
158
    return cm->current_frame.reference_mode;
912
158
  }
913
1.52k
}
914
915
#define READ_REF_BIT(pname) \
916
15.1k
  aom_read_symbol(r, av1_get_pred_cdf_##pname(xd), 2, ACCT_STR)
917
918
static COMP_REFERENCE_TYPE read_comp_reference_type(const MACROBLOCKD *xd,
919
1.16k
                                                    aom_reader *r) {
920
1.16k
  const int ctx = av1_get_comp_reference_type_context(xd);
921
1.16k
  const COMP_REFERENCE_TYPE comp_ref_type =
922
1.16k
      (COMP_REFERENCE_TYPE)aom_read_symbol(
923
1.16k
          r, xd->tile_ctx->comp_ref_type_cdf[ctx], 2, ACCT_STR);
924
1.16k
  return comp_ref_type;  // UNIDIR_COMP_REFERENCE or BIDIR_COMP_REFERENCE
925
1.16k
}
926
927
static void set_ref_frames_for_skip_mode(AV1_COMMON *const cm,
928
5
                                         MV_REFERENCE_FRAME ref_frame[2]) {
929
5
  ref_frame[0] = LAST_FRAME + cm->current_frame.skip_mode_info.ref_frame_idx_0;
930
5
  ref_frame[1] = LAST_FRAME + cm->current_frame.skip_mode_info.ref_frame_idx_1;
931
5
}
932
933
// Read the referncence frame
934
static void read_ref_frames(AV1_COMMON *const cm, MACROBLOCKD *const xd,
935
                            aom_reader *r, int segment_id,
936
7.19k
                            MV_REFERENCE_FRAME ref_frame[2]) {
937
7.19k
  if (xd->mi[0]->skip_mode) {
938
5
    set_ref_frames_for_skip_mode(cm, ref_frame);
939
5
    return;
940
5
  }
941
942
7.19k
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
943
1.03k
    ref_frame[0] = (MV_REFERENCE_FRAME)get_segdata(&cm->seg, segment_id,
944
1.03k
                                                   SEG_LVL_REF_FRAME);
945
1.03k
    ref_frame[1] = NONE_FRAME;
946
6.15k
  } else if (segfeature_active(&cm->seg, segment_id, SEG_LVL_SKIP) ||
947
6.15k
             segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
948
790
    ref_frame[0] = LAST_FRAME;
949
790
    ref_frame[1] = NONE_FRAME;
950
5.36k
  } else {
951
5.36k
    const REFERENCE_MODE mode = read_block_reference_mode(cm, xd, r);
952
953
5.36k
    if (mode == COMPOUND_REFERENCE) {
954
1.16k
      const COMP_REFERENCE_TYPE comp_ref_type = read_comp_reference_type(xd, r);
955
956
1.16k
      if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
957
177
        const int bit = READ_REF_BIT(uni_comp_ref_p);
958
177
        if (bit) {
959
35
          ref_frame[0] = BWDREF_FRAME;
960
35
          ref_frame[1] = ALTREF_FRAME;
961
142
        } else {
962
142
          const int bit1 = READ_REF_BIT(uni_comp_ref_p1);
963
142
          if (bit1) {
964
105
            const int bit2 = READ_REF_BIT(uni_comp_ref_p2);
965
105
            if (bit2) {
966
38
              ref_frame[0] = LAST_FRAME;
967
38
              ref_frame[1] = GOLDEN_FRAME;
968
67
            } else {
969
67
              ref_frame[0] = LAST_FRAME;
970
67
              ref_frame[1] = LAST3_FRAME;
971
67
            }
972
105
          } else {
973
37
            ref_frame[0] = LAST_FRAME;
974
37
            ref_frame[1] = LAST2_FRAME;
975
37
          }
976
142
        }
977
978
177
        return;
979
177
      }
980
981
991
      assert(comp_ref_type == BIDIR_COMP_REFERENCE);
982
983
991
      const int idx = 1;
984
991
      const int bit = READ_REF_BIT(comp_ref_p);
985
      // Decode forward references.
986
991
      if (!bit) {
987
794
        const int bit1 = READ_REF_BIT(comp_ref_p1);
988
794
        ref_frame[!idx] = bit1 ? LAST2_FRAME : LAST_FRAME;
989
794
      } else {
990
197
        const int bit2 = READ_REF_BIT(comp_ref_p2);
991
197
        ref_frame[!idx] = bit2 ? GOLDEN_FRAME : LAST3_FRAME;
992
197
      }
993
994
      // Decode backward references.
995
991
      const int bit_bwd = READ_REF_BIT(comp_bwdref_p);
996
991
      if (!bit_bwd) {
997
823
        const int bit1_bwd = READ_REF_BIT(comp_bwdref_p1);
998
823
        ref_frame[idx] = bit1_bwd ? ALTREF2_FRAME : BWDREF_FRAME;
999
823
      } else {
1000
168
        ref_frame[idx] = ALTREF_FRAME;
1001
168
      }
1002
4.20k
    } else if (mode == SINGLE_REFERENCE) {
1003
4.20k
      const int bit0 = READ_REF_BIT(single_ref_p1);
1004
4.20k
      if (bit0) {
1005
2.41k
        const int bit1 = READ_REF_BIT(single_ref_p2);
1006
2.41k
        if (!bit1) {
1007
699
          const int bit5 = READ_REF_BIT(single_ref_p6);
1008
699
          ref_frame[0] = bit5 ? ALTREF2_FRAME : BWDREF_FRAME;
1009
1.71k
        } else {
1010
1.71k
          ref_frame[0] = ALTREF_FRAME;
1011
1.71k
        }
1012
2.41k
      } else {
1013
1.78k
        const int bit2 = READ_REF_BIT(single_ref_p3);
1014
1.78k
        if (bit2) {
1015
592
          const int bit4 = READ_REF_BIT(single_ref_p5);
1016
592
          ref_frame[0] = bit4 ? GOLDEN_FRAME : LAST3_FRAME;
1017
1.19k
        } else {
1018
1.19k
          const int bit3 = READ_REF_BIT(single_ref_p4);
1019
1.19k
          ref_frame[0] = bit3 ? LAST2_FRAME : LAST_FRAME;
1020
1.19k
        }
1021
1.78k
      }
1022
1023
4.20k
      ref_frame[1] = NONE_FRAME;
1024
4.20k
    } else {
1025
0
      assert(0 && "Invalid prediction mode.");
1026
0
    }
1027
5.36k
  }
1028
7.19k
}
1029
1030
static INLINE void read_mb_interp_filter(const MACROBLOCKD *const xd,
1031
                                         InterpFilter interp_filter,
1032
                                         bool enable_dual_filter,
1033
                                         MB_MODE_INFO *const mbmi,
1034
7.19k
                                         aom_reader *r) {
1035
7.19k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1036
1037
7.19k
  if (!av1_is_interp_needed(xd)) {
1038
1.04k
    set_default_interp_filters(mbmi, interp_filter);
1039
1.04k
    return;
1040
1.04k
  }
1041
1042
6.15k
  if (interp_filter != SWITCHABLE) {
1043
2.42k
    mbmi->interp_filters = av1_broadcast_interp_filter(interp_filter);
1044
3.72k
  } else {
1045
3.72k
    InterpFilter ref0_filter[2] = { EIGHTTAP_REGULAR, EIGHTTAP_REGULAR };
1046
9.95k
    for (int dir = 0; dir < 2; ++dir) {
1047
6.83k
      const int ctx = av1_get_pred_context_switchable_interp(xd, dir);
1048
6.83k
      ref0_filter[dir] = (InterpFilter)aom_read_symbol(
1049
6.83k
          r, ec_ctx->switchable_interp_cdf[ctx], SWITCHABLE_FILTERS, ACCT_STR);
1050
6.83k
      if (!enable_dual_filter) {
1051
608
        ref0_filter[1] = ref0_filter[0];
1052
608
        break;
1053
608
      }
1054
6.83k
    }
1055
    // The index system works as: (0, 1) -> (vertical, horizontal) filter types
1056
3.72k
    mbmi->interp_filters.as_filters.x_filter = ref0_filter[1];
1057
3.72k
    mbmi->interp_filters.as_filters.y_filter = ref0_filter[0];
1058
3.72k
  }
1059
6.15k
}
1060
1061
static void read_intra_block_mode_info(AV1_COMMON *const cm,
1062
                                       MACROBLOCKD *const xd,
1063
                                       MB_MODE_INFO *const mbmi,
1064
517
                                       aom_reader *r) {
1065
517
  const BLOCK_SIZE bsize = mbmi->bsize;
1066
517
  const int use_angle_delta = av1_use_angle_delta(bsize);
1067
1068
517
  mbmi->ref_frame[0] = INTRA_FRAME;
1069
517
  mbmi->ref_frame[1] = NONE_FRAME;
1070
1071
517
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1072
1073
517
  mbmi->mode = read_intra_mode(r, ec_ctx->y_mode_cdf[size_group_lookup[bsize]]);
1074
1075
517
  mbmi->angle_delta[PLANE_TYPE_Y] =
1076
517
      use_angle_delta && av1_is_directional_mode(mbmi->mode)
1077
517
          ? read_angle_delta(r, ec_ctx->angle_delta_cdf[mbmi->mode - V_PRED])
1078
517
          : 0;
1079
517
  if (!cm->seq_params->monochrome && xd->is_chroma_ref) {
1080
146
    mbmi->uv_mode =
1081
146
        read_intra_mode_uv(ec_ctx, r, is_cfl_allowed(xd), mbmi->mode);
1082
146
    if (mbmi->uv_mode == UV_CFL_PRED) {
1083
16
      mbmi->cfl_alpha_idx =
1084
16
          read_cfl_alphas(xd->tile_ctx, r, &mbmi->cfl_alpha_signs);
1085
16
    }
1086
146
    mbmi->angle_delta[PLANE_TYPE_UV] =
1087
146
        use_angle_delta && av1_is_directional_mode(get_uv_mode(mbmi->uv_mode))
1088
146
            ? read_angle_delta(r,
1089
13
                               ec_ctx->angle_delta_cdf[mbmi->uv_mode - V_PRED])
1090
146
            : 0;
1091
371
  } else {
1092
    // Avoid decoding angle_info if there is is no chroma prediction
1093
371
    mbmi->uv_mode = UV_DC_PRED;
1094
371
  }
1095
517
  xd->cfl.store_y = store_cfl_required(cm, xd);
1096
1097
517
  mbmi->palette_mode_info.palette_size[0] = 0;
1098
517
  mbmi->palette_mode_info.palette_size[1] = 0;
1099
517
  if (av1_allow_palette(cm->features.allow_screen_content_tools, bsize))
1100
60
    read_palette_mode_info(cm, xd, r);
1101
1102
517
  read_filter_intra_mode_info(cm, xd, r);
1103
517
}
1104
1105
71.8k
static INLINE int is_mv_valid(const MV *mv) {
1106
71.8k
  return mv->row > MV_LOW && mv->row < MV_UPP && mv->col > MV_LOW &&
1107
71.8k
         mv->col < MV_UPP;
1108
71.8k
}
1109
1110
static INLINE int assign_mv(AV1_COMMON *cm, MACROBLOCKD *xd,
1111
                            PREDICTION_MODE mode,
1112
                            MV_REFERENCE_FRAME ref_frame[2], int_mv mv[2],
1113
                            int_mv ref_mv[2], int_mv nearest_mv[2],
1114
                            int_mv near_mv[2], int is_compound, int allow_hp,
1115
7.19k
                            aom_reader *r) {
1116
7.19k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1117
7.19k
  MB_MODE_INFO *mbmi = xd->mi[0];
1118
7.19k
  BLOCK_SIZE bsize = mbmi->bsize;
1119
7.19k
  FeatureFlags *const features = &cm->features;
1120
7.19k
  if (features->cur_frame_force_integer_mv) {
1121
1.11k
    allow_hp = MV_SUBPEL_NONE;
1122
1.11k
  }
1123
7.19k
  switch (mode) {
1124
2.50k
    case NEWMV: {
1125
2.50k
      nmv_context *const nmvc = &ec_ctx->nmvc;
1126
2.50k
      read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
1127
2.50k
      break;
1128
0
    }
1129
1.10k
    case NEARESTMV: {
1130
1.10k
      mv[0].as_int = nearest_mv[0].as_int;
1131
1.10k
      break;
1132
0
    }
1133
543
    case NEARMV: {
1134
543
      mv[0].as_int = near_mv[0].as_int;
1135
543
      break;
1136
0
    }
1137
1.86k
    case GLOBALMV: {
1138
1.86k
      mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
1139
1.86k
                                          features->allow_high_precision_mv,
1140
1.86k
                                          bsize, xd->mi_col, xd->mi_row,
1141
1.86k
                                          features->cur_frame_force_integer_mv)
1142
1.86k
                         .as_int;
1143
1.86k
      break;
1144
0
    }
1145
755
    case NEW_NEWMV: {
1146
755
      assert(is_compound);
1147
2.26k
      for (int i = 0; i < 2; ++i) {
1148
1.51k
        nmv_context *const nmvc = &ec_ctx->nmvc;
1149
1.51k
        read_mv(r, &mv[i].as_mv, &ref_mv[i].as_mv, nmvc, allow_hp);
1150
1.51k
      }
1151
755
      break;
1152
755
    }
1153
88
    case NEAREST_NEARESTMV: {
1154
88
      assert(is_compound);
1155
88
      mv[0].as_int = nearest_mv[0].as_int;
1156
88
      mv[1].as_int = nearest_mv[1].as_int;
1157
88
      break;
1158
88
    }
1159
39
    case NEAR_NEARMV: {
1160
39
      assert(is_compound);
1161
39
      mv[0].as_int = near_mv[0].as_int;
1162
39
      mv[1].as_int = near_mv[1].as_int;
1163
39
      break;
1164
39
    }
1165
62
    case NEW_NEARESTMV: {
1166
62
      nmv_context *const nmvc = &ec_ctx->nmvc;
1167
62
      read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
1168
62
      assert(is_compound);
1169
62
      mv[1].as_int = nearest_mv[1].as_int;
1170
62
      break;
1171
62
    }
1172
24
    case NEAREST_NEWMV: {
1173
24
      nmv_context *const nmvc = &ec_ctx->nmvc;
1174
24
      mv[0].as_int = nearest_mv[0].as_int;
1175
24
      read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, allow_hp);
1176
24
      assert(is_compound);
1177
24
      break;
1178
24
    }
1179
30
    case NEAR_NEWMV: {
1180
30
      nmv_context *const nmvc = &ec_ctx->nmvc;
1181
30
      mv[0].as_int = near_mv[0].as_int;
1182
30
      read_mv(r, &mv[1].as_mv, &ref_mv[1].as_mv, nmvc, allow_hp);
1183
30
      assert(is_compound);
1184
30
      break;
1185
30
    }
1186
30
    case NEW_NEARMV: {
1187
29
      nmv_context *const nmvc = &ec_ctx->nmvc;
1188
29
      read_mv(r, &mv[0].as_mv, &ref_mv[0].as_mv, nmvc, allow_hp);
1189
29
      assert(is_compound);
1190
29
      mv[1].as_int = near_mv[1].as_int;
1191
29
      break;
1192
29
    }
1193
146
    case GLOBAL_GLOBALMV: {
1194
146
      assert(is_compound);
1195
146
      mv[0].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[0]],
1196
146
                                          features->allow_high_precision_mv,
1197
146
                                          bsize, xd->mi_col, xd->mi_row,
1198
146
                                          features->cur_frame_force_integer_mv)
1199
146
                         .as_int;
1200
146
      mv[1].as_int = gm_get_motion_vector(&cm->global_motion[ref_frame[1]],
1201
146
                                          features->allow_high_precision_mv,
1202
146
                                          bsize, xd->mi_col, xd->mi_row,
1203
146
                                          features->cur_frame_force_integer_mv)
1204
146
                         .as_int;
1205
146
      break;
1206
146
    }
1207
0
    default: { return 0; }
1208
7.19k
  }
1209
1210
7.19k
  int ret = is_mv_valid(&mv[0].as_mv);
1211
7.19k
  if (is_compound) {
1212
1.17k
    ret = ret && is_mv_valid(&mv[1].as_mv);
1213
1.17k
  }
1214
7.19k
  return ret;
1215
7.19k
}
1216
1217
static int read_is_inter_block(AV1_COMMON *const cm, MACROBLOCKD *const xd,
1218
7.70k
                               int segment_id, aom_reader *r) {
1219
7.70k
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_REF_FRAME)) {
1220
1.13k
    const int frame = get_segdata(&cm->seg, segment_id, SEG_LVL_REF_FRAME);
1221
1.13k
    if (frame < LAST_FRAME) return 0;
1222
1.03k
    return frame != INTRA_FRAME;
1223
1.13k
  }
1224
6.57k
  if (segfeature_active(&cm->seg, segment_id, SEG_LVL_GLOBALMV)) {
1225
538
    return 1;
1226
538
  }
1227
6.03k
  const int ctx = av1_get_intra_inter_context(xd);
1228
6.03k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1229
6.03k
  const int is_inter =
1230
6.03k
      aom_read_symbol(r, ec_ctx->intra_inter_cdf[ctx], 2, ACCT_STR);
1231
6.03k
  return is_inter;
1232
6.57k
}
1233
1234
#if DEC_MISMATCH_DEBUG
1235
static void dec_dump_logs(AV1_COMMON *cm, MB_MODE_INFO *const mbmi, int mi_row,
1236
                          int mi_col, int16_t mode_ctx) {
1237
  int_mv mv[2] = { { 0 } };
1238
  for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref)
1239
    mv[ref].as_mv = mbmi->mv[ref].as_mv;
1240
1241
  const int16_t newmv_ctx = mode_ctx & NEWMV_CTX_MASK;
1242
  int16_t zeromv_ctx = -1;
1243
  int16_t refmv_ctx = -1;
1244
  if (mbmi->mode != NEWMV) {
1245
    zeromv_ctx = (mode_ctx >> GLOBALMV_OFFSET) & GLOBALMV_CTX_MASK;
1246
    if (mbmi->mode != GLOBALMV)
1247
      refmv_ctx = (mode_ctx >> REFMV_OFFSET) & REFMV_CTX_MASK;
1248
  }
1249
1250
#define FRAME_TO_CHECK 11
1251
  if (cm->current_frame.frame_number == FRAME_TO_CHECK && cm->show_frame == 1) {
1252
    printf(
1253
        "=== DECODER ===: "
1254
        "Frame=%d, (mi_row,mi_col)=(%d,%d), skip_mode=%d, mode=%d, bsize=%d, "
1255
        "show_frame=%d, mv[0]=(%d,%d), mv[1]=(%d,%d), ref[0]=%d, "
1256
        "ref[1]=%d, motion_mode=%d, mode_ctx=%d, "
1257
        "newmv_ctx=%d, zeromv_ctx=%d, refmv_ctx=%d, tx_size=%d\n",
1258
        cm->current_frame.frame_number, mi_row, mi_col, mbmi->skip_mode,
1259
        mbmi->mode, mbmi->sb_type, cm->show_frame, mv[0].as_mv.row,
1260
        mv[0].as_mv.col, mv[1].as_mv.row, mv[1].as_mv.col, mbmi->ref_frame[0],
1261
        mbmi->ref_frame[1], mbmi->motion_mode, mode_ctx, newmv_ctx, zeromv_ctx,
1262
        refmv_ctx, mbmi->tx_size);
1263
  }
1264
}
1265
#endif  // DEC_MISMATCH_DEBUG
1266
1267
static void read_inter_block_mode_info(AV1Decoder *const pbi,
1268
                                       DecoderCodingBlock *dcb,
1269
                                       MB_MODE_INFO *const mbmi,
1270
7.19k
                                       aom_reader *r) {
1271
7.19k
  AV1_COMMON *const cm = &pbi->common;
1272
7.19k
  FeatureFlags *const features = &cm->features;
1273
7.19k
  const BLOCK_SIZE bsize = mbmi->bsize;
1274
7.19k
  const int allow_hp = features->allow_high_precision_mv;
1275
7.19k
  int_mv nearestmv[2], nearmv[2];
1276
7.19k
  int_mv ref_mvs[MODE_CTX_REF_FRAMES][MAX_MV_REF_CANDIDATES] = { { { 0 } } };
1277
7.19k
  int16_t inter_mode_ctx[MODE_CTX_REF_FRAMES];
1278
7.19k
  int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
1279
7.19k
  MACROBLOCKD *const xd = &dcb->xd;
1280
7.19k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1281
1282
7.19k
  mbmi->uv_mode = UV_DC_PRED;
1283
7.19k
  mbmi->palette_mode_info.palette_size[0] = 0;
1284
7.19k
  mbmi->palette_mode_info.palette_size[1] = 0;
1285
1286
7.19k
  av1_collect_neighbors_ref_counts(xd);
1287
1288
7.19k
  read_ref_frames(cm, xd, r, mbmi->segment_id, mbmi->ref_frame);
1289
7.19k
  const int is_compound = has_second_ref(mbmi);
1290
1291
7.19k
  const MV_REFERENCE_FRAME ref_frame = av1_ref_frame_type(mbmi->ref_frame);
1292
7.19k
  av1_find_mv_refs(cm, xd, mbmi, ref_frame, dcb->ref_mv_count, xd->ref_mv_stack,
1293
7.19k
                   xd->weight, ref_mvs, /*global_mvs=*/NULL, inter_mode_ctx);
1294
1295
7.19k
  mbmi->ref_mv_idx = 0;
1296
1297
7.19k
  if (mbmi->skip_mode) {
1298
5
    assert(is_compound);
1299
5
    mbmi->mode = NEAREST_NEARESTMV;
1300
7.19k
  } else {
1301
7.19k
    if (segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP) ||
1302
7.19k
        segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_GLOBALMV)) {
1303
1.73k
      mbmi->mode = GLOBALMV;
1304
5.45k
    } else {
1305
5.45k
      const int mode_ctx =
1306
5.45k
          av1_mode_context_analyzer(inter_mode_ctx, mbmi->ref_frame);
1307
5.45k
      if (is_compound)
1308
1.16k
        mbmi->mode = read_inter_compound_mode(xd, r, mode_ctx);
1309
4.28k
      else
1310
4.28k
        mbmi->mode = read_inter_mode(ec_ctx, r, mode_ctx);
1311
5.45k
      if (mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV ||
1312
5.45k
          have_nearmv_in_inter_mode(mbmi->mode))
1313
3.90k
        read_drl_idx(ec_ctx, dcb, mbmi, r);
1314
5.45k
    }
1315
7.19k
  }
1316
1317
7.19k
  if (is_compound != is_inter_compound_mode(mbmi->mode)) {
1318
0
    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1319
0
                       "Prediction mode %d invalid with ref frame %d %d",
1320
0
                       mbmi->mode, mbmi->ref_frame[0], mbmi->ref_frame[1]);
1321
0
  }
1322
1323
7.19k
  if (!is_compound && mbmi->mode != GLOBALMV) {
1324
4.15k
    av1_find_best_ref_mvs(allow_hp, ref_mvs[mbmi->ref_frame[0]], &nearestmv[0],
1325
4.15k
                          &nearmv[0], features->cur_frame_force_integer_mv);
1326
4.15k
  }
1327
1328
7.19k
  if (is_compound && mbmi->mode != GLOBAL_GLOBALMV) {
1329
1.02k
    const int ref_mv_idx = mbmi->ref_mv_idx + 1;
1330
1.02k
    nearestmv[0] = xd->ref_mv_stack[ref_frame][0].this_mv;
1331
1.02k
    nearestmv[1] = xd->ref_mv_stack[ref_frame][0].comp_mv;
1332
1.02k
    nearmv[0] = xd->ref_mv_stack[ref_frame][ref_mv_idx].this_mv;
1333
1.02k
    nearmv[1] = xd->ref_mv_stack[ref_frame][ref_mv_idx].comp_mv;
1334
1.02k
    lower_mv_precision(&nearestmv[0].as_mv, allow_hp,
1335
1.02k
                       features->cur_frame_force_integer_mv);
1336
1.02k
    lower_mv_precision(&nearestmv[1].as_mv, allow_hp,
1337
1.02k
                       features->cur_frame_force_integer_mv);
1338
1.02k
    lower_mv_precision(&nearmv[0].as_mv, allow_hp,
1339
1.02k
                       features->cur_frame_force_integer_mv);
1340
1.02k
    lower_mv_precision(&nearmv[1].as_mv, allow_hp,
1341
1.02k
                       features->cur_frame_force_integer_mv);
1342
6.16k
  } else if (mbmi->ref_mv_idx > 0 && mbmi->mode == NEARMV) {
1343
32
    nearmv[0] =
1344
32
        xd->ref_mv_stack[mbmi->ref_frame[0]][1 + mbmi->ref_mv_idx].this_mv;
1345
32
  }
1346
1347
7.19k
  int_mv ref_mv[2] = { nearestmv[0], nearestmv[1] };
1348
1349
7.19k
  if (is_compound) {
1350
1.17k
    int ref_mv_idx = mbmi->ref_mv_idx;
1351
    // Special case: NEAR_NEWMV and NEW_NEARMV modes use
1352
    // 1 + mbmi->ref_mv_idx (like NEARMV) instead of
1353
    // mbmi->ref_mv_idx (like NEWMV)
1354
1.17k
    if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV)
1355
59
      ref_mv_idx = 1 + mbmi->ref_mv_idx;
1356
1357
    // TODO(jingning, yunqing): Do we need a lower_mv_precision() call here?
1358
1.17k
    if (compound_ref0_mode(mbmi->mode) == NEWMV)
1359
846
      ref_mv[0] = xd->ref_mv_stack[ref_frame][ref_mv_idx].this_mv;
1360
1361
1.17k
    if (compound_ref1_mode(mbmi->mode) == NEWMV)
1362
809
      ref_mv[1] = xd->ref_mv_stack[ref_frame][ref_mv_idx].comp_mv;
1363
6.02k
  } else {
1364
6.02k
    if (mbmi->mode == NEWMV) {
1365
2.50k
      if (dcb->ref_mv_count[ref_frame] > 1)
1366
631
        ref_mv[0] = xd->ref_mv_stack[ref_frame][mbmi->ref_mv_idx].this_mv;
1367
2.50k
    }
1368
6.02k
  }
1369
1370
7.19k
  if (mbmi->skip_mode) assert(mbmi->mode == NEAREST_NEARESTMV);
1371
1372
7.19k
  const int mv_corrupted_flag =
1373
7.19k
      !assign_mv(cm, xd, mbmi->mode, mbmi->ref_frame, mbmi->mv, ref_mv,
1374
7.19k
                 nearestmv, nearmv, is_compound, allow_hp, r);
1375
7.19k
  aom_merge_corrupted_flag(&dcb->corrupted, mv_corrupted_flag);
1376
1377
7.19k
  mbmi->use_wedge_interintra = 0;
1378
7.19k
  if (cm->seq_params->enable_interintra_compound && !mbmi->skip_mode &&
1379
7.19k
      is_interintra_allowed(mbmi)) {
1380
278
    const int bsize_group = size_group_lookup[bsize];
1381
278
    const int interintra =
1382
278
        aom_read_symbol(r, ec_ctx->interintra_cdf[bsize_group], 2, ACCT_STR);
1383
278
    assert(mbmi->ref_frame[1] == NONE_FRAME);
1384
278
    if (interintra) {
1385
94
      const INTERINTRA_MODE interintra_mode =
1386
94
          read_interintra_mode(xd, r, bsize_group);
1387
94
      mbmi->ref_frame[1] = INTRA_FRAME;
1388
94
      mbmi->interintra_mode = interintra_mode;
1389
94
      mbmi->angle_delta[PLANE_TYPE_Y] = 0;
1390
94
      mbmi->angle_delta[PLANE_TYPE_UV] = 0;
1391
94
      mbmi->filter_intra_mode_info.use_filter_intra = 0;
1392
94
      if (av1_is_wedge_used(bsize)) {
1393
94
        mbmi->use_wedge_interintra = aom_read_symbol(
1394
94
            r, ec_ctx->wedge_interintra_cdf[bsize], 2, ACCT_STR);
1395
94
        if (mbmi->use_wedge_interintra) {
1396
53
          mbmi->interintra_wedge_index = (int8_t)aom_read_symbol(
1397
53
              r, ec_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES, ACCT_STR);
1398
53
        }
1399
94
      }
1400
94
    }
1401
278
  }
1402
1403
15.5k
  for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
1404
8.36k
    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
1405
8.36k
    xd->block_ref_scale_factors[ref] = get_ref_scale_factors_const(cm, frame);
1406
8.36k
  }
1407
1408
7.19k
  mbmi->motion_mode = SIMPLE_TRANSLATION;
1409
7.19k
  if (is_motion_variation_allowed_bsize(mbmi->bsize) && !mbmi->skip_mode &&
1410
7.19k
      !has_second_ref(mbmi)) {
1411
1.29k
    mbmi->num_proj_ref = av1_findSamples(cm, xd, pts, pts_inref);
1412
1.29k
  }
1413
7.19k
  av1_count_overlappable_neighbors(cm, xd);
1414
1415
7.19k
  if (mbmi->ref_frame[1] != INTRA_FRAME)
1416
7.10k
    mbmi->motion_mode = read_motion_mode(cm, xd, mbmi, r);
1417
1418
  // init
1419
7.19k
  mbmi->comp_group_idx = 0;
1420
7.19k
  mbmi->compound_idx = 1;
1421
7.19k
  mbmi->interinter_comp.type = COMPOUND_AVERAGE;
1422
1423
7.19k
  if (has_second_ref(mbmi) && !mbmi->skip_mode) {
1424
    // Read idx to indicate current compound inter prediction mode group
1425
1.16k
    const int masked_compound_used = is_any_masked_compound_used(bsize) &&
1426
1.16k
                                     cm->seq_params->enable_masked_compound;
1427
1428
1.16k
    if (masked_compound_used) {
1429
178
      const int ctx_comp_group_idx = get_comp_group_idx_context(xd);
1430
178
      mbmi->comp_group_idx = (uint8_t)aom_read_symbol(
1431
178
          r, ec_ctx->comp_group_idx_cdf[ctx_comp_group_idx], 2, ACCT_STR);
1432
178
    }
1433
1434
1.16k
    if (mbmi->comp_group_idx == 0) {
1435
1.07k
      if (cm->seq_params->order_hint_info.enable_dist_wtd_comp) {
1436
767
        const int comp_index_ctx = get_comp_index_context(cm, xd);
1437
767
        mbmi->compound_idx = (uint8_t)aom_read_symbol(
1438
767
            r, ec_ctx->compound_index_cdf[comp_index_ctx], 2, ACCT_STR);
1439
767
        mbmi->interinter_comp.type =
1440
767
            mbmi->compound_idx ? COMPOUND_AVERAGE : COMPOUND_DISTWTD;
1441
767
      } else {
1442
        // Distance-weighted compound is disabled, so always use average
1443
308
        mbmi->compound_idx = 1;
1444
308
        mbmi->interinter_comp.type = COMPOUND_AVERAGE;
1445
308
      }
1446
1.07k
    } else {
1447
93
      assert(cm->current_frame.reference_mode != SINGLE_REFERENCE &&
1448
93
             is_inter_compound_mode(mbmi->mode) &&
1449
93
             mbmi->motion_mode == SIMPLE_TRANSLATION);
1450
93
      assert(masked_compound_used);
1451
1452
      // compound_diffwtd, wedge
1453
93
      if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) {
1454
93
        mbmi->interinter_comp.type =
1455
93
            COMPOUND_WEDGE + aom_read_symbol(r,
1456
93
                                             ec_ctx->compound_type_cdf[bsize],
1457
93
                                             MASKED_COMPOUND_TYPES, ACCT_STR);
1458
93
      } else {
1459
0
        mbmi->interinter_comp.type = COMPOUND_DIFFWTD;
1460
0
      }
1461
1462
93
      if (mbmi->interinter_comp.type == COMPOUND_WEDGE) {
1463
63
        assert(is_interinter_compound_used(COMPOUND_WEDGE, bsize));
1464
63
        mbmi->interinter_comp.wedge_index = (int8_t)aom_read_symbol(
1465
63
            r, ec_ctx->wedge_idx_cdf[bsize], MAX_WEDGE_TYPES, ACCT_STR);
1466
63
        mbmi->interinter_comp.wedge_sign = (int8_t)aom_read_bit(r, ACCT_STR);
1467
63
      } else {
1468
30
        assert(mbmi->interinter_comp.type == COMPOUND_DIFFWTD);
1469
30
        mbmi->interinter_comp.mask_type =
1470
30
            aom_read_literal(r, MAX_DIFFWTD_MASK_BITS, ACCT_STR);
1471
30
      }
1472
93
    }
1473
1.16k
  }
1474
1475
7.19k
  read_mb_interp_filter(xd, features->interp_filter,
1476
7.19k
                        cm->seq_params->enable_dual_filter, mbmi, r);
1477
1478
7.19k
#if !CONFIG_REALTIME_ONLY
1479
7.19k
  if (mbmi->motion_mode == WARPED_CAUSAL) {
1480
0
    const int mi_row = xd->mi_row;
1481
0
    const int mi_col = xd->mi_col;
1482
0
    mbmi->wm_params.wmtype = DEFAULT_WMTYPE;
1483
0
    mbmi->wm_params.invalid = 0;
1484
1485
0
    if (mbmi->num_proj_ref > 1) {
1486
0
      mbmi->num_proj_ref = av1_selectSamples(&mbmi->mv[0].as_mv, pts, pts_inref,
1487
0
                                             mbmi->num_proj_ref, bsize);
1488
0
    }
1489
1490
0
    if (av1_find_projection(mbmi->num_proj_ref, pts, pts_inref, bsize,
1491
0
                            mbmi->mv[0].as_mv.row, mbmi->mv[0].as_mv.col,
1492
0
                            &mbmi->wm_params, mi_row, mi_col)) {
1493
#if WARPED_MOTION_DEBUG
1494
      printf("Warning: unexpected warped model from aomenc\n");
1495
#endif
1496
0
      mbmi->wm_params.invalid = 1;
1497
0
    }
1498
0
  }
1499
7.19k
#endif
1500
1501
7.19k
  xd->cfl.store_y = store_cfl_required(cm, xd);
1502
1503
#if DEC_MISMATCH_DEBUG
1504
  dec_dump_logs(cm, mi, mi_row, mi_col, mode_ctx);
1505
#endif  // DEC_MISMATCH_DEBUG
1506
7.19k
}
1507
1508
static void read_inter_frame_mode_info(AV1Decoder *const pbi,
1509
7.75k
                                       DecoderCodingBlock *dcb, aom_reader *r) {
1510
7.75k
  AV1_COMMON *const cm = &pbi->common;
1511
7.75k
  MACROBLOCKD *const xd = &dcb->xd;
1512
7.75k
  MB_MODE_INFO *const mbmi = xd->mi[0];
1513
7.75k
  int inter_block = 1;
1514
1515
7.75k
  mbmi->mv[0].as_int = 0;
1516
7.75k
  mbmi->mv[1].as_int = 0;
1517
7.75k
  mbmi->segment_id = read_inter_segment_id(cm, xd, 1, r);
1518
1519
7.75k
  mbmi->skip_mode = read_skip_mode(cm, xd, mbmi->segment_id, r);
1520
1521
7.75k
  if (mbmi->skip_mode)
1522
5
    mbmi->skip_txfm = 1;
1523
7.74k
  else
1524
7.74k
    mbmi->skip_txfm = read_skip_txfm(cm, xd, mbmi->segment_id, r);
1525
1526
7.75k
  if (!cm->seg.segid_preskip)
1527
4.87k
    mbmi->segment_id = read_inter_segment_id(cm, xd, 0, r);
1528
1529
7.75k
  read_cdef(cm, r, xd);
1530
1531
7.75k
  read_delta_q_params(cm, xd, r);
1532
1533
7.75k
  if (!mbmi->skip_mode)
1534
7.70k
    inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
1535
1536
7.75k
  mbmi->current_qindex = xd->current_base_qindex;
1537
1538
7.75k
  xd->above_txfm_context =
1539
7.75k
      cm->above_contexts.txfm[xd->tile.tile_row] + xd->mi_col;
1540
7.75k
  xd->left_txfm_context =
1541
7.75k
      xd->left_txfm_context_buffer + (xd->mi_row & MAX_MIB_MASK);
1542
1543
7.75k
  if (inter_block)
1544
7.19k
    read_inter_block_mode_info(pbi, dcb, mbmi, r);
1545
557
  else
1546
557
    read_intra_block_mode_info(cm, xd, mbmi, r);
1547
7.75k
}
1548
1549
static void intra_copy_frame_mvs(AV1_COMMON *const cm, int mi_row, int mi_col,
1550
9.27k
                                 int x_mis, int y_mis) {
1551
9.27k
  const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_params.mi_cols, 1);
1552
9.27k
  MV_REF *frame_mvs =
1553
9.27k
      cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1);
1554
9.27k
  x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
1555
9.27k
  y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
1556
1557
22.3k
  for (int h = 0; h < y_mis; h++) {
1558
13.0k
    MV_REF *mv = frame_mvs;
1559
43.9k
    for (int w = 0; w < x_mis; w++) {
1560
30.8k
      mv->ref_frame = NONE_FRAME;
1561
30.8k
      mv++;
1562
30.8k
    }
1563
13.0k
    frame_mvs += frame_mvs_stride;
1564
13.0k
  }
1565
9.27k
}
1566
1567
void av1_read_mode_info(AV1Decoder *const pbi, DecoderCodingBlock *dcb,
1568
8.13M
                        aom_reader *r, int x_mis, int y_mis) {
1569
8.13M
  AV1_COMMON *const cm = &pbi->common;
1570
8.13M
  MACROBLOCKD *const xd = &dcb->xd;
1571
8.13M
  MB_MODE_INFO *const mi = xd->mi[0];
1572
8.13M
  mi->use_intrabc = 0;
1573
1574
8.13M
  if (frame_is_intra_only(cm)) {
1575
8.13M
    read_intra_frame_mode_info(cm, dcb, r);
1576
8.13M
    if (cm->seq_params->order_hint_info.enable_ref_frame_mvs)
1577
9.27k
      intra_copy_frame_mvs(cm, xd->mi_row, xd->mi_col, x_mis, y_mis);
1578
8.13M
  } else {
1579
7.75k
    read_inter_frame_mode_info(pbi, dcb, r);
1580
7.75k
    if (cm->seq_params->order_hint_info.enable_ref_frame_mvs)
1581
4.96k
      av1_copy_frame_mvs(cm, mi, xd->mi_row, xd->mi_col, x_mis, y_mis);
1582
7.75k
  }
1583
8.13M
}