Coverage Report

Created: 2026-02-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/decoder/decodeframe.c
Line
Count
Source
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
#include <stdbool.h>
14
#include <stddef.h>
15
16
#include "config/aom_config.h"
17
#include "config/aom_scale_rtcd.h"
18
19
#include "aom/aom_codec.h"
20
#include "aom/aom_image.h"
21
#include "aom/internal/aom_codec_internal.h"
22
#include "aom_dsp/aom_dsp_common.h"
23
#include "aom_dsp/binary_codes_reader.h"
24
#include "aom_dsp/bitreader.h"
25
#include "aom_dsp/bitreader_buffer.h"
26
#include "aom_dsp/txfm_common.h"
27
#include "aom_mem/aom_mem.h"
28
#include "aom_ports/aom_timer.h"
29
#include "aom_ports/mem.h"
30
#include "aom_ports/mem_ops.h"
31
#include "aom_scale/yv12config.h"
32
#include "aom_util/aom_pthread.h"
33
#include "aom_util/aom_thread.h"
34
35
#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
36
#include "aom_util/debug_util.h"
37
#endif  // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
38
39
#include "av1/common/alloccommon.h"
40
#include "av1/common/av1_common_int.h"
41
#include "av1/common/blockd.h"
42
#include "av1/common/cdef.h"
43
#include "av1/common/cfl.h"
44
#include "av1/common/common_data.h"
45
#include "av1/common/common.h"
46
#include "av1/common/entropy.h"
47
#include "av1/common/entropymode.h"
48
#include "av1/common/entropymv.h"
49
#include "av1/common/enums.h"
50
#include "av1/common/frame_buffers.h"
51
#include "av1/common/idct.h"
52
#include "av1/common/mv.h"
53
#include "av1/common/mvref_common.h"
54
#include "av1/common/obmc.h"
55
#include "av1/common/pred_common.h"
56
#include "av1/common/quant_common.h"
57
#include "av1/common/reconinter.h"
58
#include "av1/common/reconintra.h"
59
#include "av1/common/resize.h"
60
#include "av1/common/restoration.h"
61
#include "av1/common/scale.h"
62
#include "av1/common/seg_common.h"
63
#include "av1/common/thread_common.h"
64
#include "av1/common/tile_common.h"
65
#include "av1/common/warped_motion.h"
66
67
#include "av1/decoder/decodeframe.h"
68
#include "av1/decoder/decodemv.h"
69
#include "av1/decoder/decoder.h"
70
#include "av1/decoder/decodetxb.h"
71
#include "av1/decoder/detokenize.h"
72
#if CONFIG_INSPECTION
73
#include "av1/decoder/inspection.h"
74
#endif
75
76
#define ACCT_STR __func__
77
78
41.9k
#define AOM_MIN_THREADS_PER_TILE 1
79
51.1k
#define AOM_MAX_THREADS_PER_TILE 2
80
81
// This is needed by ext_tile related unit tests.
82
#define EXT_TILE_DEBUG 1
83
#define MC_TEMP_BUF_PELS                       \
84
58.0k
  (((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2) * \
85
58.0k
   ((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2))
86
87
// Checks that the remaining bits start with a 1 and ends with 0s.
88
// It consumes an additional byte, if already byte aligned before the check.
89
19.4k
int av1_check_trailing_bits(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
90
  // bit_offset is set to 0 (mod 8) when the reader is already byte aligned
91
19.4k
  int bits_before_alignment = 8 - rb->bit_offset % 8;
92
19.4k
  int trailing = aom_rb_read_literal(rb, bits_before_alignment);
93
19.4k
  if (trailing != (1 << (bits_before_alignment - 1))) {
94
59
    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
95
59
    return -1;
96
59
  }
97
19.3k
  return 0;
98
19.4k
}
99
100
// Use only_chroma = 1 to only set the chroma planes
101
static inline void set_planes_to_neutral_grey(
102
    const SequenceHeader *const seq_params, const YV12_BUFFER_CONFIG *const buf,
103
13.8k
    int only_chroma) {
104
13.8k
  if (seq_params->use_highbitdepth) {
105
8.19k
    const int val = 1 << (seq_params->bit_depth - 1);
106
28.1k
    for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
107
19.9k
      const int is_uv = plane > 0;
108
19.9k
      uint16_t *const base = CONVERT_TO_SHORTPTR(buf->buffers[plane]);
109
      // Set the first row to neutral grey. Then copy the first row to all
110
      // subsequent rows.
111
19.9k
      if (buf->crop_heights[is_uv] > 0) {
112
19.9k
        aom_memset16(base, val, buf->crop_widths[is_uv]);
113
481k
        for (int row_idx = 1; row_idx < buf->crop_heights[is_uv]; row_idx++) {
114
461k
          memcpy(&base[row_idx * buf->strides[is_uv]], base,
115
461k
                 sizeof(*base) * buf->crop_widths[is_uv]);
116
461k
        }
117
19.9k
      }
118
19.9k
    }
119
8.19k
  } else {
120
19.4k
    for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
121
13.7k
      const int is_uv = plane > 0;
122
583k
      for (int row_idx = 0; row_idx < buf->crop_heights[is_uv]; row_idx++) {
123
569k
        memset(&buf->buffers[plane][row_idx * buf->strides[is_uv]], 1 << 7,
124
569k
               buf->crop_widths[is_uv]);
125
569k
      }
126
13.7k
    }
127
5.65k
  }
128
13.8k
}
129
130
static inline void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
131
                                                   MACROBLOCKD *xd,
132
                                                   aom_reader *const r,
133
                                                   int plane, int runit_idx);
134
135
38.3k
static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
136
38.3k
  return len != 0 && len <= (size_t)(end - start);
137
38.3k
}
138
139
static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb,
140
32.0k
                            int coded_lossless) {
141
32.0k
  if (coded_lossless) return ONLY_4X4;
142
27.2k
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
143
32.0k
}
144
145
static REFERENCE_MODE read_frame_reference_mode(
146
32.0k
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
147
32.0k
  if (frame_is_intra_only(cm)) {
148
19.3k
    return SINGLE_REFERENCE;
149
19.3k
  } else {
150
12.6k
    return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
151
12.6k
  }
152
32.0k
}
153
154
static inline void inverse_transform_block(DecoderCodingBlock *dcb, int plane,
155
                                           const TX_TYPE tx_type,
156
                                           const TX_SIZE tx_size, uint8_t *dst,
157
3.18M
                                           int stride, int reduced_tx_set) {
158
3.18M
  tran_low_t *const dqcoeff = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
159
3.18M
  eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
160
3.18M
  uint16_t scan_line = eob_data->max_scan_line;
161
3.18M
  uint16_t eob = eob_data->eob;
162
3.18M
  av1_inverse_transform_block(&dcb->xd, dqcoeff, plane, tx_type, tx_size, dst,
163
3.18M
                              stride, eob, reduced_tx_set);
164
3.18M
  memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
165
3.18M
}
166
167
static inline void read_coeffs_tx_intra_block(
168
    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
169
24.5M
    const int plane, const int row, const int col, const TX_SIZE tx_size) {
170
24.5M
  MB_MODE_INFO *mbmi = dcb->xd.mi[0];
171
24.5M
  if (!mbmi->skip_txfm) {
172
#if TXCOEFF_TIMER
173
    struct aom_usec_timer timer;
174
    aom_usec_timer_start(&timer);
175
#endif
176
11.8M
    av1_read_coeffs_txb(cm, dcb, r, plane, row, col, tx_size);
177
#if TXCOEFF_TIMER
178
    aom_usec_timer_mark(&timer);
179
    const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
180
    cm->txcoeff_timer += elapsed_time;
181
    ++cm->txb_count;
182
#endif
183
11.8M
  }
184
24.5M
}
185
186
static inline void decode_block_void(const AV1_COMMON *const cm,
187
                                     DecoderCodingBlock *dcb,
188
                                     aom_reader *const r, const int plane,
189
                                     const int row, const int col,
190
37.0M
                                     const TX_SIZE tx_size) {
191
37.0M
  (void)cm;
192
37.0M
  (void)dcb;
193
37.0M
  (void)r;
194
37.0M
  (void)plane;
195
37.0M
  (void)row;
196
37.0M
  (void)col;
197
37.0M
  (void)tx_size;
198
37.0M
}
199
200
static inline void predict_inter_block_void(AV1_COMMON *const cm,
201
                                            DecoderCodingBlock *dcb,
202
158k
                                            BLOCK_SIZE bsize) {
203
158k
  (void)cm;
204
158k
  (void)dcb;
205
158k
  (void)bsize;
206
158k
}
207
208
static inline void cfl_store_inter_block_void(AV1_COMMON *const cm,
209
158k
                                              MACROBLOCKD *const xd) {
210
158k
  (void)cm;
211
158k
  (void)xd;
212
158k
}
213
214
static inline void predict_and_reconstruct_intra_block(
215
    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
216
20.0M
    const int plane, const int row, const int col, const TX_SIZE tx_size) {
217
20.0M
  (void)r;
218
20.0M
  MACROBLOCKD *const xd = &dcb->xd;
219
20.0M
  MB_MODE_INFO *mbmi = xd->mi[0];
220
20.0M
  PLANE_TYPE plane_type = get_plane_type(plane);
221
222
20.0M
  av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
223
224
20.0M
  if (!mbmi->skip_txfm) {
225
9.91M
    eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
226
9.91M
    if (eob_data->eob) {
227
3.08M
      const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
228
      // tx_type was read out in av1_read_coeffs_txb.
229
3.08M
      const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
230
3.08M
                                              reduced_tx_set_used);
231
3.08M
      struct macroblockd_plane *const pd = &xd->plane[plane];
232
3.08M
      uint8_t *dst = &pd->dst.buf[(row * pd->dst.stride + col) << MI_SIZE_LOG2];
233
3.08M
      inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
234
3.08M
                              reduced_tx_set_used);
235
3.08M
    }
236
9.91M
  }
237
20.0M
  if (plane == AOM_PLANE_Y && store_cfl_required(cm, xd)) {
238
1.13M
    cfl_store_tx(xd, row, col, tx_size, mbmi->bsize);
239
1.13M
  }
240
20.0M
}
241
242
static inline void inverse_transform_inter_block(
243
    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
244
    const int plane, const int blk_row, const int blk_col,
245
94.2k
    const TX_SIZE tx_size) {
246
94.2k
  (void)r;
247
94.2k
  MACROBLOCKD *const xd = &dcb->xd;
248
94.2k
  PLANE_TYPE plane_type = get_plane_type(plane);
249
94.2k
  const struct macroblockd_plane *const pd = &xd->plane[plane];
250
94.2k
  const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
251
  // tx_type was read out in av1_read_coeffs_txb.
252
94.2k
  const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col,
253
94.2k
                                          tx_size, reduced_tx_set_used);
254
255
94.2k
  uint8_t *dst =
256
94.2k
      &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
257
94.2k
  inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
258
94.2k
                          reduced_tx_set_used);
259
#if CONFIG_MISMATCH_DEBUG
260
  int pixel_c, pixel_r;
261
  BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
262
  int blk_w = block_size_wide[bsize];
263
  int blk_h = block_size_high[bsize];
264
  const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
265
  const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
266
  mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, blk_col, blk_row,
267
                  pd->subsampling_x, pd->subsampling_y);
268
  mismatch_check_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
269
                          plane, pixel_c, pixel_r, blk_w, blk_h,
270
                          xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
271
#endif
272
94.2k
}
273
274
static inline void set_cb_buffer_offsets(DecoderCodingBlock *dcb,
275
40.7M
                                         TX_SIZE tx_size, int plane) {
276
40.7M
  dcb->cb_offset[plane] += tx_size_wide[tx_size] * tx_size_high[tx_size];
277
40.7M
  dcb->txb_offset[plane] =
278
40.7M
      dcb->cb_offset[plane] / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
279
40.7M
}
280
281
static inline void decode_reconstruct_tx(AV1_COMMON *cm, ThreadData *const td,
282
                                         aom_reader *r,
283
                                         MB_MODE_INFO *const mbmi, int plane,
284
                                         BLOCK_SIZE plane_bsize, int blk_row,
285
                                         int blk_col, int block,
286
261k
                                         TX_SIZE tx_size, int *eob_total) {
287
261k
  DecoderCodingBlock *const dcb = &td->dcb;
288
261k
  MACROBLOCKD *const xd = &dcb->xd;
289
261k
  const struct macroblockd_plane *const pd = &xd->plane[plane];
290
261k
  const TX_SIZE plane_tx_size =
291
261k
      plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
292
144k
                                    pd->subsampling_y)
293
261k
            : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
294
117k
                                                         blk_col)];
295
  // Scale to match transform block unit.
296
261k
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
297
261k
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
298
299
261k
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
300
301
261k
  if (tx_size == plane_tx_size || plane) {
302
255k
    td->read_coeffs_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
303
255k
                                         tx_size);
304
305
255k
    td->inverse_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
306
255k
                                     tx_size);
307
255k
    eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
308
255k
    *eob_total += eob_data->eob;
309
255k
    set_cb_buffer_offsets(dcb, tx_size, plane);
310
255k
  } else {
311
5.62k
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
312
5.62k
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
313
5.62k
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
314
5.62k
    const int bsw = tx_size_wide_unit[sub_txs];
315
5.62k
    const int bsh = tx_size_high_unit[sub_txs];
316
5.62k
    const int sub_step = bsw * bsh;
317
5.62k
    const int row_end =
318
5.62k
        AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
319
5.62k
    const int col_end =
320
5.62k
        AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
321
322
5.62k
    assert(bsw > 0 && bsh > 0);
323
324
15.5k
    for (int row = 0; row < row_end; row += bsh) {
325
9.96k
      const int offsetr = blk_row + row;
326
28.5k
      for (int col = 0; col < col_end; col += bsw) {
327
18.5k
        const int offsetc = blk_col + col;
328
329
18.5k
        decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize, offsetr,
330
18.5k
                              offsetc, block, sub_txs, eob_total);
331
18.5k
        block += sub_step;
332
18.5k
      }
333
9.96k
    }
334
5.62k
  }
335
261k
}
336
337
static inline void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
338
                               BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
339
4.57M
                               int bh, int x_mis, int y_mis) {
340
4.57M
  const int num_planes = av1_num_planes(cm);
341
4.57M
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
342
4.57M
  const TileInfo *const tile = &xd->tile;
343
344
4.57M
  set_mi_offsets(mi_params, xd, mi_row, mi_col);
345
4.57M
  xd->mi[0]->bsize = bsize;
346
#if CONFIG_RD_DEBUG
347
  xd->mi[0]->mi_row = mi_row;
348
  xd->mi[0]->mi_col = mi_col;
349
#endif
350
351
4.57M
  assert(x_mis && y_mis);
352
15.7M
  for (int x = 1; x < x_mis; ++x) xd->mi[x] = xd->mi[0];
353
4.57M
  int idx = mi_params->mi_stride;
354
16.7M
  for (int y = 1; y < y_mis; ++y) {
355
12.1M
    memcpy(&xd->mi[idx], &xd->mi[0], x_mis * sizeof(xd->mi[0]));
356
12.1M
    idx += mi_params->mi_stride;
357
12.1M
  }
358
359
4.57M
  set_plane_n4(xd, bw, bh, num_planes);
360
4.57M
  set_entropy_context(xd, mi_row, mi_col, num_planes);
361
362
  // Distance of Mb to the various image edges. These are specified to 8th pel
363
  // as they are always compared to values that are in 1/8th pel units
364
4.57M
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
365
4.57M
                 mi_params->mi_cols);
366
367
4.57M
  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
368
4.57M
                       num_planes);
369
4.57M
}
370
371
static inline void decode_mbmi_block(AV1Decoder *const pbi,
372
                                     DecoderCodingBlock *dcb, int mi_row,
373
                                     int mi_col, aom_reader *r,
374
                                     PARTITION_TYPE partition,
375
4.57M
                                     BLOCK_SIZE bsize) {
376
4.57M
  AV1_COMMON *const cm = &pbi->common;
377
4.57M
  const SequenceHeader *const seq_params = cm->seq_params;
378
4.57M
  const int bw = mi_size_wide[bsize];
379
4.57M
  const int bh = mi_size_high[bsize];
380
4.57M
  const int x_mis = AOMMIN(bw, cm->mi_params.mi_cols - mi_col);
381
4.57M
  const int y_mis = AOMMIN(bh, cm->mi_params.mi_rows - mi_row);
382
4.57M
  MACROBLOCKD *const xd = &dcb->xd;
383
384
#if CONFIG_ACCOUNTING
385
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
386
#endif
387
4.57M
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
388
4.57M
  xd->mi[0]->partition = partition;
389
4.57M
  av1_read_mode_info(pbi, dcb, r, x_mis, y_mis);
390
4.57M
  if (bsize >= BLOCK_8X8 &&
391
3.56M
      (seq_params->subsampling_x || seq_params->subsampling_y)) {
392
1.01M
    const BLOCK_SIZE uv_subsize =
393
1.01M
        av1_ss_size_lookup[bsize][seq_params->subsampling_x]
394
1.01M
                          [seq_params->subsampling_y];
395
1.01M
    if (uv_subsize == BLOCK_INVALID)
396
0
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
397
0
                         "Invalid block size.");
398
1.01M
  }
399
4.57M
}
400
401
typedef struct PadBlock {
402
  int x0;
403
  int x1;
404
  int y0;
405
  int y1;
406
} PadBlock;
407
408
#if CONFIG_AV1_HIGHBITDEPTH
409
static inline void highbd_build_mc_border(const uint8_t *src8, int src_stride,
410
                                          uint8_t *dst8, int dst_stride, int x,
411
                                          int y, int b_w, int b_h, int w,
412
52.3k
                                          int h) {
413
  // Get a pointer to the start of the real data for this row.
414
52.3k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
415
52.3k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
416
52.3k
  const uint16_t *ref_row = src - x - y * src_stride;
417
418
52.3k
  if (y >= h)
419
2.87k
    ref_row += (h - 1) * src_stride;
420
49.4k
  else if (y > 0)
421
17.1k
    ref_row += y * src_stride;
422
423
773k
  do {
424
773k
    int right = 0, copy;
425
773k
    int left = x < 0 ? -x : 0;
426
427
773k
    if (left > b_w) left = b_w;
428
429
773k
    if (x + b_w > w) right = x + b_w - w;
430
431
773k
    if (right > b_w) right = b_w;
432
433
773k
    copy = b_w - left - right;
434
435
773k
    if (left) aom_memset16(dst, ref_row[0], left);
436
437
773k
    if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
438
439
773k
    if (right) aom_memset16(dst + left + copy, ref_row[w - 1], right);
440
441
773k
    dst += dst_stride;
442
773k
    ++y;
443
444
773k
    if (y > 0 && y < h) ref_row += src_stride;
445
773k
  } while (--b_h);
446
52.3k
}
447
#endif  // CONFIG_AV1_HIGHBITDEPTH
448
449
static inline void build_mc_border(const uint8_t *src, int src_stride,
450
                                   uint8_t *dst, int dst_stride, int x, int y,
451
46.5k
                                   int b_w, int b_h, int w, int h) {
452
  // Get a pointer to the start of the real data for this row.
453
46.5k
  const uint8_t *ref_row = src - x - y * src_stride;
454
455
46.5k
  if (y >= h)
456
5.98k
    ref_row += (h - 1) * src_stride;
457
40.5k
  else if (y > 0)
458
7.97k
    ref_row += y * src_stride;
459
460
640k
  do {
461
640k
    int right = 0, copy;
462
640k
    int left = x < 0 ? -x : 0;
463
464
640k
    if (left > b_w) left = b_w;
465
466
640k
    if (x + b_w > w) right = x + b_w - w;
467
468
640k
    if (right > b_w) right = b_w;
469
470
640k
    copy = b_w - left - right;
471
472
640k
    if (left) memset(dst, ref_row[0], left);
473
474
640k
    if (copy) memcpy(dst + left, ref_row + x + left, copy);
475
476
640k
    if (right) memset(dst + left + copy, ref_row[w - 1], right);
477
478
640k
    dst += dst_stride;
479
640k
    ++y;
480
481
640k
    if (y > 0 && y < h) ref_row += src_stride;
482
640k
  } while (--b_h);
483
46.5k
}
484
485
static inline int update_extend_mc_border_params(
486
    const struct scale_factors *const sf, struct buf_2d *const pre_buf,
487
    MV32 scaled_mv, PadBlock *block, int subpel_x_mv, int subpel_y_mv,
488
324k
    int do_warp, int is_intrabc, int *x_pad, int *y_pad) {
489
324k
  const int is_scaled = av1_is_scaled(sf);
490
  // Get reference width and height.
491
324k
  int frame_width = pre_buf->width;
492
324k
  int frame_height = pre_buf->height;
493
494
  // Do border extension if there is motion or
495
  // width/height is not a multiple of 8 pixels.
496
324k
  if ((!is_intrabc) && (!do_warp) &&
497
101k
      (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
498
101k
       (frame_height & 0x7))) {
499
101k
    if (subpel_x_mv || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
500
101k
      block->x0 -= AOM_INTERP_EXTEND - 1;
501
101k
      block->x1 += AOM_INTERP_EXTEND;
502
101k
      *x_pad = 1;
503
101k
    }
504
505
101k
    if (subpel_y_mv || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
506
101k
      block->y0 -= AOM_INTERP_EXTEND - 1;
507
101k
      block->y1 += AOM_INTERP_EXTEND;
508
101k
      *y_pad = 1;
509
101k
    }
510
511
    // Skip border extension if block is inside the frame.
512
101k
    if (block->x0 < 0 || block->x1 > frame_width - 1 || block->y0 < 0 ||
513
98.8k
        block->y1 > frame_height - 1) {
514
98.8k
      return 1;
515
98.8k
    }
516
101k
  }
517
225k
  return 0;
518
324k
}
519
520
static inline void extend_mc_border(const struct scale_factors *const sf,
521
                                    struct buf_2d *const pre_buf,
522
                                    MV32 scaled_mv, PadBlock block,
523
                                    int subpel_x_mv, int subpel_y_mv,
524
                                    int do_warp, int is_intrabc, int highbd,
525
                                    uint8_t *mc_buf, uint8_t **pre,
526
324k
                                    int *src_stride) {
527
324k
  int x_pad = 0, y_pad = 0;
528
324k
  if (update_extend_mc_border_params(sf, pre_buf, scaled_mv, &block,
529
324k
                                     subpel_x_mv, subpel_y_mv, do_warp,
530
324k
                                     is_intrabc, &x_pad, &y_pad)) {
531
    // Get reference block pointer.
532
98.8k
    const uint8_t *const buf_ptr =
533
98.8k
        pre_buf->buf0 + block.y0 * pre_buf->stride + block.x0;
534
98.8k
    int buf_stride = pre_buf->stride;
535
98.8k
    const int b_w = block.x1 - block.x0;
536
98.8k
    const int b_h = block.y1 - block.y0;
537
538
98.8k
#if CONFIG_AV1_HIGHBITDEPTH
539
    // Extend the border.
540
98.8k
    if (highbd) {
541
52.3k
      highbd_build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0,
542
52.3k
                             block.y0, b_w, b_h, pre_buf->width,
543
52.3k
                             pre_buf->height);
544
52.3k
    } else {
545
46.5k
      build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
546
46.5k
                      b_h, pre_buf->width, pre_buf->height);
547
46.5k
    }
548
#else
549
    (void)highbd;
550
    build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
551
                    b_h, pre_buf->width, pre_buf->height);
552
#endif
553
98.8k
    *src_stride = b_w;
554
98.8k
    *pre = mc_buf + y_pad * (AOM_INTERP_EXTEND - 1) * b_w +
555
98.8k
           x_pad * (AOM_INTERP_EXTEND - 1);
556
98.8k
  }
557
324k
}
558
559
static inline void dec_calc_subpel_params(
560
    const MV *const src_mv, InterPredParams *const inter_pred_params,
561
    const MACROBLOCKD *const xd, int mi_x, int mi_y, uint8_t **pre,
562
    SubpelParams *subpel_params, int *src_stride, PadBlock *block,
563
324k
    MV32 *scaled_mv, int *subpel_x_mv, int *subpel_y_mv) {
564
324k
  const struct scale_factors *sf = inter_pred_params->scale_factors;
565
324k
  struct buf_2d *pre_buf = &inter_pred_params->ref_frame_buf;
566
324k
  const int bw = inter_pred_params->block_width;
567
324k
  const int bh = inter_pred_params->block_height;
568
324k
  const int is_scaled = av1_is_scaled(sf);
569
324k
  if (is_scaled) {
570
14.5k
    int ssx = inter_pred_params->subsampling_x;
571
14.5k
    int ssy = inter_pred_params->subsampling_y;
572
14.5k
    int orig_pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
573
14.5k
    orig_pos_y += src_mv->row * (1 << (1 - ssy));
574
14.5k
    int orig_pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
575
14.5k
    orig_pos_x += src_mv->col * (1 << (1 - ssx));
576
14.5k
    int pos_y = av1_scaled_y(orig_pos_y, sf);
577
14.5k
    int pos_x = av1_scaled_x(orig_pos_x, sf);
578
14.5k
    pos_x += SCALE_EXTRA_OFF;
579
14.5k
    pos_y += SCALE_EXTRA_OFF;
580
581
14.5k
    const int top = -AOM_LEFT_TOP_MARGIN_SCALED(ssy);
582
14.5k
    const int left = -AOM_LEFT_TOP_MARGIN_SCALED(ssx);
583
14.5k
    const int bottom = (pre_buf->height + AOM_INTERP_EXTEND)
584
14.5k
                       << SCALE_SUBPEL_BITS;
585
14.5k
    const int right = (pre_buf->width + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
586
14.5k
    pos_y = clamp(pos_y, top, bottom);
587
14.5k
    pos_x = clamp(pos_x, left, right);
588
589
14.5k
    subpel_params->subpel_x = pos_x & SCALE_SUBPEL_MASK;
590
14.5k
    subpel_params->subpel_y = pos_y & SCALE_SUBPEL_MASK;
591
14.5k
    subpel_params->xs = sf->x_step_q4;
592
14.5k
    subpel_params->ys = sf->y_step_q4;
593
594
    // Get reference block top left coordinate.
595
14.5k
    block->x0 = pos_x >> SCALE_SUBPEL_BITS;
596
14.5k
    block->y0 = pos_y >> SCALE_SUBPEL_BITS;
597
598
    // Get reference block bottom right coordinate.
599
14.5k
    block->x1 =
600
14.5k
        ((pos_x + (bw - 1) * subpel_params->xs) >> SCALE_SUBPEL_BITS) + 1;
601
14.5k
    block->y1 =
602
14.5k
        ((pos_y + (bh - 1) * subpel_params->ys) >> SCALE_SUBPEL_BITS) + 1;
603
604
14.5k
    MV temp_mv;
605
14.5k
    temp_mv = clamp_mv_to_umv_border_sb(xd, src_mv, bw, bh,
606
14.5k
                                        inter_pred_params->subsampling_x,
607
14.5k
                                        inter_pred_params->subsampling_y);
608
14.5k
    *scaled_mv = av1_scale_mv(&temp_mv, mi_x, mi_y, sf);
609
14.5k
    scaled_mv->row += SCALE_EXTRA_OFF;
610
14.5k
    scaled_mv->col += SCALE_EXTRA_OFF;
611
612
14.5k
    *subpel_x_mv = scaled_mv->col & SCALE_SUBPEL_MASK;
613
14.5k
    *subpel_y_mv = scaled_mv->row & SCALE_SUBPEL_MASK;
614
309k
  } else {
615
    // Get block position in current frame.
616
309k
    int pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
617
309k
    int pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
618
619
309k
    const MV mv_q4 = clamp_mv_to_umv_border_sb(
620
309k
        xd, src_mv, bw, bh, inter_pred_params->subsampling_x,
621
309k
        inter_pred_params->subsampling_y);
622
309k
    subpel_params->xs = subpel_params->ys = SCALE_SUBPEL_SHIFTS;
623
309k
    subpel_params->subpel_x = (mv_q4.col & SUBPEL_MASK) << SCALE_EXTRA_BITS;
624
309k
    subpel_params->subpel_y = (mv_q4.row & SUBPEL_MASK) << SCALE_EXTRA_BITS;
625
626
    // Get reference block top left coordinate.
627
309k
    pos_x += mv_q4.col;
628
309k
    pos_y += mv_q4.row;
629
309k
    block->x0 = pos_x >> SUBPEL_BITS;
630
309k
    block->y0 = pos_y >> SUBPEL_BITS;
631
632
    // Get reference block bottom right coordinate.
633
309k
    block->x1 = (pos_x >> SUBPEL_BITS) + (bw - 1) + 1;
634
309k
    block->y1 = (pos_y >> SUBPEL_BITS) + (bh - 1) + 1;
635
636
309k
    scaled_mv->row = mv_q4.row;
637
309k
    scaled_mv->col = mv_q4.col;
638
309k
    *subpel_x_mv = scaled_mv->col & SUBPEL_MASK;
639
309k
    *subpel_y_mv = scaled_mv->row & SUBPEL_MASK;
640
309k
  }
641
324k
  *pre = pre_buf->buf0 + block->y0 * pre_buf->stride + block->x0;
642
324k
  *src_stride = pre_buf->stride;
643
324k
}
644
645
static inline void dec_calc_subpel_params_and_extend(
646
    const MV *const src_mv, InterPredParams *const inter_pred_params,
647
    MACROBLOCKD *const xd, int mi_x, int mi_y, int ref, uint8_t **mc_buf,
648
324k
    uint8_t **pre, SubpelParams *subpel_params, int *src_stride) {
649
324k
  PadBlock block;
650
324k
  MV32 scaled_mv;
651
324k
  int subpel_x_mv, subpel_y_mv;
652
324k
  dec_calc_subpel_params(src_mv, inter_pred_params, xd, mi_x, mi_y, pre,
653
324k
                         subpel_params, src_stride, &block, &scaled_mv,
654
324k
                         &subpel_x_mv, &subpel_y_mv);
655
324k
  extend_mc_border(
656
324k
      inter_pred_params->scale_factors, &inter_pred_params->ref_frame_buf,
657
324k
      scaled_mv, block, subpel_x_mv, subpel_y_mv,
658
324k
      inter_pred_params->mode == WARP_PRED, inter_pred_params->is_intrabc,
659
324k
      inter_pred_params->use_hbd_buf, mc_buf[ref], pre, src_stride);
660
324k
}
661
662
#define IS_DEC 1
663
#include "av1/common/reconinter_template.inc"
664
#undef IS_DEC
665
666
static void dec_build_inter_predictors(const AV1_COMMON *cm,
667
                                       DecoderCodingBlock *dcb, int plane,
668
                                       const MB_MODE_INFO *mi,
669
                                       int build_for_obmc, int bw, int bh,
670
301k
                                       int mi_x, int mi_y) {
671
301k
  build_inter_predictors(cm, &dcb->xd, plane, mi, build_for_obmc, bw, bh, mi_x,
672
301k
                         mi_y, dcb->mc_buf);
673
301k
}
674
675
static inline void dec_build_inter_predictor(const AV1_COMMON *cm,
676
                                             DecoderCodingBlock *dcb,
677
                                             int mi_row, int mi_col,
678
106k
                                             BLOCK_SIZE bsize) {
679
106k
  MACROBLOCKD *const xd = &dcb->xd;
680
106k
  const int num_planes = av1_num_planes(cm);
681
401k
  for (int plane = 0; plane < num_planes; ++plane) {
682
303k
    if (plane && !xd->is_chroma_ref) break;
683
294k
    const int mi_x = mi_col * MI_SIZE;
684
294k
    const int mi_y = mi_row * MI_SIZE;
685
294k
    dec_build_inter_predictors(cm, dcb, plane, xd->mi[0], 0,
686
294k
                               xd->plane[plane].width, xd->plane[plane].height,
687
294k
                               mi_x, mi_y);
688
294k
    if (is_interintra_pred(xd->mi[0])) {
689
2.89k
      BUFFER_SET ctx = { { xd->plane[0].dst.buf, xd->plane[1].dst.buf,
690
2.89k
                           xd->plane[2].dst.buf },
691
2.89k
                         { xd->plane[0].dst.stride, xd->plane[1].dst.stride,
692
2.89k
                           xd->plane[2].dst.stride } };
693
2.89k
      av1_build_interintra_predictor(cm, xd, xd->plane[plane].dst.buf,
694
2.89k
                                     xd->plane[plane].dst.stride, &ctx, plane,
695
2.89k
                                     bsize);
696
2.89k
    }
697
294k
  }
698
106k
}
699
700
static inline void dec_build_prediction_by_above_pred(
701
    MACROBLOCKD *const xd, int rel_mi_row, int rel_mi_col, uint8_t op_mi_size,
702
811
    int dir, MB_MODE_INFO *above_mbmi, void *fun_ctxt, const int num_planes) {
703
811
  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
704
811
  const int above_mi_col = xd->mi_col + rel_mi_col;
705
811
  int mi_x, mi_y;
706
811
  MB_MODE_INFO backup_mbmi = *above_mbmi;
707
708
811
  (void)rel_mi_row;
709
811
  (void)dir;
710
711
811
  av1_setup_build_prediction_by_above_pred(xd, rel_mi_col, op_mi_size,
712
811
                                           &backup_mbmi, ctxt, num_planes);
713
811
  mi_x = above_mi_col << MI_SIZE_LOG2;
714
811
  mi_y = xd->mi_row << MI_SIZE_LOG2;
715
716
811
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
717
718
3.24k
  for (int j = 0; j < num_planes; ++j) {
719
2.43k
    const struct macroblockd_plane *pd = &xd->plane[j];
720
2.43k
    int bw = (op_mi_size * MI_SIZE) >> pd->subsampling_x;
721
2.43k
    int bh = clamp(block_size_high[bsize] >> (pd->subsampling_y + 1), 4,
722
2.43k
                   block_size_high[BLOCK_64X64] >> (pd->subsampling_y + 1));
723
724
2.43k
    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 0)) continue;
725
2.04k
    dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
726
2.04k
                               &backup_mbmi, 1, bw, bh, mi_x, mi_y);
727
2.04k
  }
728
811
}
729
730
static inline void dec_build_prediction_by_above_preds(
731
    const AV1_COMMON *cm, DecoderCodingBlock *dcb,
732
    uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
733
2.12k
    int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
734
2.12k
  MACROBLOCKD *const xd = &dcb->xd;
735
2.12k
  if (!xd->up_available) return;
736
737
  // Adjust mb_to_bottom_edge to have the correct value for the OBMC
738
  // prediction block. This is half the height of the original block,
739
  // except for 128-wide blocks, where we only use a height of 32.
740
729
  const int this_height = xd->height * MI_SIZE;
741
729
  const int pred_height = AOMMIN(this_height / 2, 32);
742
729
  xd->mb_to_bottom_edge += GET_MV_SUBPEL(this_height - pred_height);
743
729
  struct build_prediction_ctxt ctxt = {
744
729
    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_right_edge, dcb
745
729
  };
746
729
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
747
729
  foreach_overlappable_nb_above(cm, xd,
748
729
                                max_neighbor_obmc[mi_size_wide_log2[bsize]],
749
729
                                dec_build_prediction_by_above_pred, &ctxt);
750
751
729
  xd->mb_to_left_edge = -GET_MV_SUBPEL(xd->mi_col * MI_SIZE);
752
729
  xd->mb_to_right_edge = ctxt.mb_to_far_edge;
753
729
  xd->mb_to_bottom_edge -= GET_MV_SUBPEL(this_height - pred_height);
754
729
}
755
756
static inline void dec_build_prediction_by_left_pred(
757
    MACROBLOCKD *const xd, int rel_mi_row, int rel_mi_col, uint8_t op_mi_size,
758
1.62k
    int dir, MB_MODE_INFO *left_mbmi, void *fun_ctxt, const int num_planes) {
759
1.62k
  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
760
1.62k
  const int left_mi_row = xd->mi_row + rel_mi_row;
761
1.62k
  int mi_x, mi_y;
762
1.62k
  MB_MODE_INFO backup_mbmi = *left_mbmi;
763
764
1.62k
  (void)rel_mi_col;
765
1.62k
  (void)dir;
766
767
1.62k
  av1_setup_build_prediction_by_left_pred(xd, rel_mi_row, op_mi_size,
768
1.62k
                                          &backup_mbmi, ctxt, num_planes);
769
1.62k
  mi_x = xd->mi_col << MI_SIZE_LOG2;
770
1.62k
  mi_y = left_mi_row << MI_SIZE_LOG2;
771
1.62k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
772
773
6.49k
  for (int j = 0; j < num_planes; ++j) {
774
4.86k
    const struct macroblockd_plane *pd = &xd->plane[j];
775
4.86k
    int bw = clamp(block_size_wide[bsize] >> (pd->subsampling_x + 1), 4,
776
4.86k
                   block_size_wide[BLOCK_64X64] >> (pd->subsampling_x + 1));
777
4.86k
    int bh = (op_mi_size << MI_SIZE_LOG2) >> pd->subsampling_y;
778
779
4.86k
    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 1)) continue;
780
4.86k
    dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
781
4.86k
                               &backup_mbmi, 1, bw, bh, mi_x, mi_y);
782
4.86k
  }
783
1.62k
}
784
785
static inline void dec_build_prediction_by_left_preds(
786
    const AV1_COMMON *cm, DecoderCodingBlock *dcb,
787
    uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
788
2.12k
    int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
789
2.12k
  MACROBLOCKD *const xd = &dcb->xd;
790
2.12k
  if (!xd->left_available) return;
791
792
  // Adjust mb_to_right_edge to have the correct value for the OBMC
793
  // prediction block. This is half the width of the original block,
794
  // except for 128-wide blocks, where we only use a width of 32.
795
1.66k
  const int this_width = xd->width * MI_SIZE;
796
1.66k
  const int pred_width = AOMMIN(this_width / 2, 32);
797
1.66k
  xd->mb_to_right_edge += GET_MV_SUBPEL(this_width - pred_width);
798
799
1.66k
  struct build_prediction_ctxt ctxt = {
800
1.66k
    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_bottom_edge, dcb
801
1.66k
  };
802
1.66k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
803
1.66k
  foreach_overlappable_nb_left(cm, xd,
804
1.66k
                               max_neighbor_obmc[mi_size_high_log2[bsize]],
805
1.66k
                               dec_build_prediction_by_left_pred, &ctxt);
806
807
1.66k
  xd->mb_to_top_edge = -GET_MV_SUBPEL(xd->mi_row * MI_SIZE);
808
1.66k
  xd->mb_to_right_edge -= GET_MV_SUBPEL(this_width - pred_width);
809
1.66k
  xd->mb_to_bottom_edge = ctxt.mb_to_far_edge;
810
1.66k
}
811
812
static inline void dec_build_obmc_inter_predictors_sb(const AV1_COMMON *cm,
813
2.12k
                                                      DecoderCodingBlock *dcb) {
814
2.12k
  const int num_planes = av1_num_planes(cm);
815
2.12k
  uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
816
2.12k
  int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
817
2.12k
  int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
818
2.12k
  int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
819
2.12k
  int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
820
2.12k
  int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
821
2.12k
  int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
822
823
2.12k
  MACROBLOCKD *const xd = &dcb->xd;
824
2.12k
  av1_setup_obmc_dst_bufs(xd, dst_buf1, dst_buf2);
825
826
2.12k
  dec_build_prediction_by_above_preds(cm, dcb, dst_buf1, dst_width1,
827
2.12k
                                      dst_height1, dst_stride1);
828
2.12k
  dec_build_prediction_by_left_preds(cm, dcb, dst_buf2, dst_width2, dst_height2,
829
2.12k
                                     dst_stride2);
830
2.12k
  const int mi_row = xd->mi_row;
831
2.12k
  const int mi_col = xd->mi_col;
832
2.12k
  av1_setup_dst_planes(xd->plane, xd->mi[0]->bsize, &cm->cur_frame->buf, mi_row,
833
2.12k
                       mi_col, 0, num_planes);
834
2.12k
  av1_build_obmc_inter_prediction(cm, xd, dst_buf1, dst_stride1, dst_buf2,
835
2.12k
                                  dst_stride2);
836
2.12k
}
837
838
static inline void cfl_store_inter_block(AV1_COMMON *const cm,
839
106k
                                         MACROBLOCKD *const xd) {
840
106k
  MB_MODE_INFO *mbmi = xd->mi[0];
841
106k
  if (store_cfl_required(cm, xd)) {
842
9.26k
    cfl_store_block(xd, mbmi->bsize, mbmi->tx_size);
843
9.26k
  }
844
106k
}
845
846
static inline void predict_inter_block(AV1_COMMON *const cm,
847
                                       DecoderCodingBlock *dcb,
848
106k
                                       BLOCK_SIZE bsize) {
849
106k
  MACROBLOCKD *const xd = &dcb->xd;
850
106k
  MB_MODE_INFO *mbmi = xd->mi[0];
851
106k
  const int num_planes = av1_num_planes(cm);
852
106k
  const int mi_row = xd->mi_row;
853
106k
  const int mi_col = xd->mi_col;
854
221k
  for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
855
114k
    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
856
114k
    if (frame < LAST_FRAME) {
857
73.9k
      assert(is_intrabc_block(mbmi));
858
73.9k
      assert(frame == INTRA_FRAME);
859
73.9k
      assert(ref == 0);
860
73.9k
    } else {
861
40.9k
      const RefCntBuffer *ref_buf = get_ref_frame_buf(cm, frame);
862
40.9k
      const struct scale_factors *ref_scale_factors =
863
40.9k
          get_ref_scale_factors_const(cm, frame);
864
865
40.9k
      xd->block_ref_scale_factors[ref] = ref_scale_factors;
866
40.9k
      av1_setup_pre_planes(xd, ref, &ref_buf->buf, mi_row, mi_col,
867
40.9k
                           ref_scale_factors, num_planes);
868
40.9k
    }
869
114k
  }
870
871
106k
  dec_build_inter_predictor(cm, dcb, mi_row, mi_col, bsize);
872
106k
  if (mbmi->motion_mode == OBMC_CAUSAL) {
873
2.12k
    dec_build_obmc_inter_predictors_sb(cm, dcb);
874
2.12k
  }
875
#if CONFIG_MISMATCH_DEBUG
876
  for (int plane = 0; plane < num_planes; ++plane) {
877
    const struct macroblockd_plane *pd = &xd->plane[plane];
878
    int pixel_c, pixel_r;
879
    mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, 0, 0, pd->subsampling_x,
880
                    pd->subsampling_y);
881
    if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
882
                             pd->subsampling_y))
883
      continue;
884
    mismatch_check_block_pre(pd->dst.buf, pd->dst.stride,
885
                             cm->current_frame.order_hint, plane, pixel_c,
886
                             pixel_r, pd->width, pd->height,
887
                             xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
888
  }
889
#endif
890
106k
}
891
892
static inline void set_color_index_map_offset(MACROBLOCKD *const xd, int plane,
893
1.38M
                                              aom_reader *r) {
894
1.38M
  (void)r;
895
1.38M
  Av1ColorMapParam params;
896
1.38M
  const MB_MODE_INFO *const mbmi = xd->mi[0];
897
1.38M
  av1_get_block_dimensions(mbmi->bsize, plane, xd, &params.plane_width,
898
1.38M
                           &params.plane_height, NULL, NULL);
899
1.38M
  xd->color_index_map_offset[plane] += params.plane_width * params.plane_height;
900
1.38M
}
901
902
static inline void decode_token_recon_block(AV1Decoder *const pbi,
903
                                            ThreadData *const td, aom_reader *r,
904
7.41M
                                            BLOCK_SIZE bsize) {
905
7.41M
  AV1_COMMON *const cm = &pbi->common;
906
7.41M
  DecoderCodingBlock *const dcb = &td->dcb;
907
7.41M
  MACROBLOCKD *const xd = &dcb->xd;
908
7.41M
  const int num_planes = av1_num_planes(cm);
909
7.41M
  MB_MODE_INFO *mbmi = xd->mi[0];
910
911
7.41M
  if (!is_inter_block(mbmi)) {
912
7.14M
    int row, col;
913
7.14M
    assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
914
7.14M
                                         xd->plane[0].subsampling_y));
915
7.14M
    const int max_blocks_wide = max_block_wide(xd, bsize, 0);
916
7.14M
    const int max_blocks_high = max_block_high(xd, bsize, 0);
917
7.14M
    const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
918
7.14M
    int mu_blocks_wide = mi_size_wide[max_unit_bsize];
919
7.14M
    int mu_blocks_high = mi_size_high[max_unit_bsize];
920
7.14M
    mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
921
7.14M
    mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
922
923
14.3M
    for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
924
14.5M
      for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
925
28.2M
        for (int plane = 0; plane < num_planes; ++plane) {
926
21.1M
          if (plane && !xd->is_chroma_ref) break;
927
20.8M
          const struct macroblockd_plane *const pd = &xd->plane[plane];
928
20.8M
          const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
929
20.8M
          const int stepr = tx_size_high_unit[tx_size];
930
20.8M
          const int stepc = tx_size_wide_unit[tx_size];
931
932
20.8M
          const int unit_height = ROUND_POWER_OF_TWO(
933
20.8M
              AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
934
20.8M
          const int unit_width = ROUND_POWER_OF_TWO(
935
20.8M
              AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
936
937
45.8M
          for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
938
24.9M
               blk_row += stepr) {
939
65.5M
            for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
940
40.5M
                 blk_col += stepc) {
941
40.5M
              td->read_coeffs_tx_intra_block_visit(cm, dcb, r, plane, blk_row,
942
40.5M
                                                   blk_col, tx_size);
943
40.5M
              td->predict_and_recon_intra_block_visit(
944
40.5M
                  cm, dcb, r, plane, blk_row, blk_col, tx_size);
945
40.5M
              set_cb_buffer_offsets(dcb, tx_size, plane);
946
40.5M
            }
947
24.9M
          }
948
20.8M
        }
949
7.31M
      }
950
7.21M
    }
951
7.14M
  } else {
952
266k
    td->predict_inter_block_visit(cm, dcb, bsize);
953
    // Reconstruction
954
266k
    if (!mbmi->skip_txfm) {
955
70.7k
      int eobtotal = 0;
956
957
70.7k
      const int max_blocks_wide = max_block_wide(xd, bsize, 0);
958
70.7k
      const int max_blocks_high = max_block_high(xd, bsize, 0);
959
70.7k
      int row, col;
960
961
70.7k
      const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
962
70.7k
      assert(max_unit_bsize ==
963
70.7k
             get_plane_block_size(BLOCK_64X64, xd->plane[0].subsampling_x,
964
70.7k
                                  xd->plane[0].subsampling_y));
965
70.7k
      int mu_blocks_wide = mi_size_wide[max_unit_bsize];
966
70.7k
      int mu_blocks_high = mi_size_high[max_unit_bsize];
967
968
70.7k
      mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
969
70.7k
      mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
970
971
142k
      for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
972
143k
        for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
973
261k
          for (int plane = 0; plane < num_planes; ++plane) {
974
192k
            if (plane && !xd->is_chroma_ref) break;
975
188k
            const struct macroblockd_plane *const pd = &xd->plane[plane];
976
188k
            const int ss_x = pd->subsampling_x;
977
188k
            const int ss_y = pd->subsampling_y;
978
188k
            const BLOCK_SIZE plane_bsize =
979
188k
                get_plane_block_size(bsize, ss_x, ss_y);
980
188k
            const TX_SIZE max_tx_size =
981
188k
                get_vartx_max_txsize(xd, plane_bsize, plane);
982
188k
            const int bh_var_tx = tx_size_high_unit[max_tx_size];
983
188k
            const int bw_var_tx = tx_size_wide_unit[max_tx_size];
984
188k
            int block = 0;
985
188k
            int step =
986
188k
                tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
987
188k
            int blk_row, blk_col;
988
188k
            const int unit_height = ROUND_POWER_OF_TWO(
989
188k
                AOMMIN(mu_blocks_high + row, max_blocks_high), ss_y);
990
188k
            const int unit_width = ROUND_POWER_OF_TWO(
991
188k
                AOMMIN(mu_blocks_wide + col, max_blocks_wide), ss_x);
992
993
392k
            for (blk_row = row >> ss_y; blk_row < unit_height;
994
204k
                 blk_row += bh_var_tx) {
995
446k
              for (blk_col = col >> ss_x; blk_col < unit_width;
996
242k
                   blk_col += bw_var_tx) {
997
242k
                decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize,
998
242k
                                      blk_row, blk_col, block, max_tx_size,
999
242k
                                      &eobtotal);
1000
242k
                block += step;
1001
242k
              }
1002
204k
            }
1003
188k
          }
1004
72.4k
        }
1005
71.2k
      }
1006
70.7k
    }
1007
266k
    td->cfl_store_inter_block_visit(cm, xd);
1008
266k
  }
1009
1010
7.41M
  av1_visit_palette(pbi, xd, r, set_color_index_map_offset);
1011
7.41M
}
1012
1013
static inline void set_inter_tx_size(MB_MODE_INFO *mbmi, int stride_log2,
1014
                                     int tx_w_log2, int tx_h_log2, int min_txs,
1015
                                     int split_size, int txs, int blk_row,
1016
14.9k
                                     int blk_col) {
1017
37.5k
  for (int idy = 0; idy < tx_size_high_unit[split_size];
1018
22.5k
       idy += tx_size_high_unit[min_txs]) {
1019
56.0k
    for (int idx = 0; idx < tx_size_wide_unit[split_size];
1020
33.5k
         idx += tx_size_wide_unit[min_txs]) {
1021
33.5k
      const int index = (((blk_row + idy) >> tx_h_log2) << stride_log2) +
1022
33.5k
                        ((blk_col + idx) >> tx_w_log2);
1023
33.5k
      mbmi->inter_tx_size[index] = txs;
1024
33.5k
    }
1025
22.5k
  }
1026
14.9k
}
1027
1028
static inline void read_tx_size_vartx(MACROBLOCKD *xd, MB_MODE_INFO *mbmi,
1029
                                      TX_SIZE tx_size, int depth, int blk_row,
1030
17.2k
                                      int blk_col, aom_reader *r) {
1031
17.2k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1032
17.2k
  int is_split = 0;
1033
17.2k
  const BLOCK_SIZE bsize = mbmi->bsize;
1034
17.2k
  const int max_blocks_high = max_block_high(xd, bsize, 0);
1035
17.2k
  const int max_blocks_wide = max_block_wide(xd, bsize, 0);
1036
17.2k
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
1037
17.2k
  assert(tx_size > TX_4X4);
1038
17.0k
  TX_SIZE txs = max_txsize_rect_lookup[bsize];
1039
34.0k
  for (int level = 0; level < MAX_VARTX_DEPTH - 1; ++level)
1040
17.0k
    txs = sub_tx_size_map[txs];
1041
17.0k
  const int tx_w_log2 = tx_size_wide_log2[txs] - MI_SIZE_LOG2;
1042
17.0k
  const int tx_h_log2 = tx_size_high_log2[txs] - MI_SIZE_LOG2;
1043
17.0k
  const int bw_log2 = mi_size_wide_log2[bsize];
1044
17.0k
  const int stride_log2 = bw_log2 - tx_w_log2;
1045
1046
17.0k
  if (depth == MAX_VARTX_DEPTH) {
1047
1.71k
    set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1048
1.71k
                      tx_size, blk_row, blk_col);
1049
1.71k
    mbmi->tx_size = tx_size;
1050
1.71k
    txfm_partition_update(xd->above_txfm_context + blk_col,
1051
1.71k
                          xd->left_txfm_context + blk_row, tx_size, tx_size);
1052
1.71k
    return;
1053
1.71k
  }
1054
1055
15.3k
  const int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
1056
15.3k
                                         xd->left_txfm_context + blk_row,
1057
15.3k
                                         mbmi->bsize, tx_size);
1058
15.3k
  is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
1059
1060
15.3k
  if (is_split) {
1061
3.97k
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
1062
3.97k
    const int bsw = tx_size_wide_unit[sub_txs];
1063
3.97k
    const int bsh = tx_size_high_unit[sub_txs];
1064
1065
3.97k
    if (sub_txs == TX_4X4) {
1066
1.90k
      set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1067
1.90k
                        sub_txs, blk_row, blk_col);
1068
1.90k
      mbmi->tx_size = sub_txs;
1069
1.90k
      txfm_partition_update(xd->above_txfm_context + blk_col,
1070
1.90k
                            xd->left_txfm_context + blk_row, sub_txs, tx_size);
1071
1.90k
      return;
1072
1.90k
    }
1073
1074
3.97k
    assert(bsw > 0 && bsh > 0);
1075
5.36k
    for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
1076
9.01k
      for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
1077
5.71k
        int offsetr = blk_row + row;
1078
5.71k
        int offsetc = blk_col + col;
1079
5.71k
        read_tx_size_vartx(xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, r);
1080
5.71k
      }
1081
3.29k
    }
1082
11.3k
  } else {
1083
11.3k
    set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1084
11.3k
                      tx_size, blk_row, blk_col);
1085
11.3k
    mbmi->tx_size = tx_size;
1086
11.3k
    txfm_partition_update(xd->above_txfm_context + blk_col,
1087
11.3k
                          xd->left_txfm_context + blk_row, tx_size, tx_size);
1088
11.3k
  }
1089
15.3k
}
1090
1091
static TX_SIZE read_selected_tx_size(const MACROBLOCKD *const xd,
1092
473k
                                     aom_reader *r) {
1093
  // TODO(debargha): Clean up the logic here. This function should only
1094
  // be called for intra.
1095
473k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1096
473k
  const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
1097
473k
  const int max_depths = bsize_to_max_depth(bsize);
1098
473k
  const int ctx = get_tx_size_context(xd);
1099
473k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1100
473k
  const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
1101
473k
                                    max_depths + 1, ACCT_STR);
1102
473k
  assert(depth >= 0 && depth <= max_depths);
1103
473k
  const TX_SIZE tx_size = depth_to_tx_size(depth, bsize);
1104
473k
  return tx_size;
1105
473k
}
1106
1107
static TX_SIZE read_tx_size(const MACROBLOCKD *const xd, TX_MODE tx_mode,
1108
                            int is_inter, int allow_select_inter,
1109
4.56M
                            aom_reader *r) {
1110
4.56M
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1111
4.56M
  if (xd->lossless[xd->mi[0]->segment_id]) return TX_4X4;
1112
1113
4.05M
  if (block_signals_txsize(bsize)) {
1114
3.74M
    if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
1115
473k
      const TX_SIZE coded_tx_size = read_selected_tx_size(xd, r);
1116
473k
      return coded_tx_size;
1117
3.27M
    } else {
1118
3.27M
      return tx_size_from_tx_mode(bsize, tx_mode);
1119
3.27M
    }
1120
3.74M
  } else {
1121
304k
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
1122
304k
    return max_txsize_rect_lookup[bsize];
1123
304k
  }
1124
4.05M
}
1125
1126
static inline void parse_decode_block(AV1Decoder *const pbi,
1127
                                      ThreadData *const td, int mi_row,
1128
                                      int mi_col, aom_reader *r,
1129
                                      PARTITION_TYPE partition,
1130
4.57M
                                      BLOCK_SIZE bsize) {
1131
4.57M
  DecoderCodingBlock *const dcb = &td->dcb;
1132
4.57M
  MACROBLOCKD *const xd = &dcb->xd;
1133
4.57M
  decode_mbmi_block(pbi, dcb, mi_row, mi_col, r, partition, bsize);
1134
1135
4.57M
  av1_visit_palette(pbi, xd, r, av1_decode_palette_tokens);
1136
1137
4.57M
  AV1_COMMON *cm = &pbi->common;
1138
4.57M
  const int num_planes = av1_num_planes(cm);
1139
4.57M
  MB_MODE_INFO *mbmi = xd->mi[0];
1140
4.57M
  int inter_block_tx = is_inter_block(mbmi) || is_intrabc_block(mbmi);
1141
4.57M
  if (cm->features.tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
1142
544k
      !mbmi->skip_txfm && inter_block_tx && !xd->lossless[mbmi->segment_id]) {
1143
11.3k
    const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
1144
11.3k
    const int bh = tx_size_high_unit[max_tx_size];
1145
11.3k
    const int bw = tx_size_wide_unit[max_tx_size];
1146
11.3k
    const int width = mi_size_wide[bsize];
1147
11.3k
    const int height = mi_size_high[bsize];
1148
1149
22.6k
    for (int idy = 0; idy < height; idy += bh)
1150
22.8k
      for (int idx = 0; idx < width; idx += bw)
1151
11.5k
        read_tx_size_vartx(xd, mbmi, max_tx_size, 0, idy, idx, r);
1152
4.56M
  } else {
1153
4.56M
    mbmi->tx_size = read_tx_size(xd, cm->features.tx_mode, inter_block_tx,
1154
4.56M
                                 !mbmi->skip_txfm, r);
1155
4.56M
    if (inter_block_tx)
1156
152k
      memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
1157
4.56M
    set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height,
1158
4.56M
                  mbmi->skip_txfm && is_inter_block(mbmi), xd);
1159
4.56M
  }
1160
1161
4.57M
  if (cm->delta_q_info.delta_q_present_flag) {
1162
17.2M
    for (int i = 0; i < MAX_SEGMENTS; i++) {
1163
15.3M
      const int current_qindex =
1164
15.3M
          av1_get_qindex(&cm->seg, i, xd->current_base_qindex);
1165
15.3M
      const CommonQuantParams *const quant_params = &cm->quant_params;
1166
61.2M
      for (int j = 0; j < num_planes; ++j) {
1167
45.8M
        const int dc_delta_q = j == 0 ? quant_params->y_dc_delta_q
1168
45.8M
                                      : (j == 1 ? quant_params->u_dc_delta_q
1169
30.5M
                                                : quant_params->v_dc_delta_q);
1170
45.8M
        const int ac_delta_q = j == 0 ? 0
1171
45.8M
                                      : (j == 1 ? quant_params->u_ac_delta_q
1172
30.5M
                                                : quant_params->v_ac_delta_q);
1173
45.8M
        xd->plane[j].seg_dequant_QTX[i][0] = av1_dc_quant_QTX(
1174
45.8M
            current_qindex, dc_delta_q, cm->seq_params->bit_depth);
1175
45.8M
        xd->plane[j].seg_dequant_QTX[i][1] = av1_ac_quant_QTX(
1176
45.8M
            current_qindex, ac_delta_q, cm->seq_params->bit_depth);
1177
45.8M
      }
1178
15.3M
    }
1179
1.91M
  }
1180
4.57M
  if (mbmi->skip_txfm) av1_reset_entropy_context(xd, bsize, num_planes);
1181
1182
4.57M
  decode_token_recon_block(pbi, td, r, bsize);
1183
4.57M
}
1184
1185
static inline void set_offsets_for_pred_and_recon(AV1Decoder *const pbi,
1186
                                                  ThreadData *const td,
1187
                                                  int mi_row, int mi_col,
1188
2.84M
                                                  BLOCK_SIZE bsize) {
1189
2.84M
  AV1_COMMON *const cm = &pbi->common;
1190
2.84M
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
1191
2.84M
  DecoderCodingBlock *const dcb = &td->dcb;
1192
2.84M
  MACROBLOCKD *const xd = &dcb->xd;
1193
2.84M
  const int bw = mi_size_wide[bsize];
1194
2.84M
  const int bh = mi_size_high[bsize];
1195
2.84M
  const int num_planes = av1_num_planes(cm);
1196
1197
2.84M
  const int offset = mi_row * mi_params->mi_stride + mi_col;
1198
2.84M
  const TileInfo *const tile = &xd->tile;
1199
1200
2.84M
  xd->mi = mi_params->mi_grid_base + offset;
1201
2.84M
  xd->tx_type_map =
1202
2.84M
      &mi_params->tx_type_map[mi_row * mi_params->mi_stride + mi_col];
1203
2.84M
  xd->tx_type_map_stride = mi_params->mi_stride;
1204
1205
2.84M
  set_plane_n4(xd, bw, bh, num_planes);
1206
1207
  // Distance of Mb to the various image edges. These are specified to 8th pel
1208
  // as they are always compared to values that are in 1/8th pel units
1209
2.84M
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
1210
2.84M
                 mi_params->mi_cols);
1211
1212
2.84M
  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
1213
2.84M
                       num_planes);
1214
2.84M
}
1215
1216
static inline void decode_block(AV1Decoder *const pbi, ThreadData *const td,
1217
                                int mi_row, int mi_col, aom_reader *r,
1218
2.84M
                                PARTITION_TYPE partition, BLOCK_SIZE bsize) {
1219
2.84M
  (void)partition;
1220
2.84M
  set_offsets_for_pred_and_recon(pbi, td, mi_row, mi_col, bsize);
1221
2.84M
  decode_token_recon_block(pbi, td, r, bsize);
1222
2.84M
}
1223
1224
static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
1225
                                     aom_reader *r, int has_rows, int has_cols,
1226
3.22M
                                     BLOCK_SIZE bsize) {
1227
3.22M
  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1228
3.22M
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1229
1230
3.22M
  if (!has_rows && !has_cols) return PARTITION_SPLIT;
1231
1232
3.22M
  assert(ctx >= 0);
1233
3.17M
  aom_cdf_prob *partition_cdf = ec_ctx->partition_cdf[ctx];
1234
3.17M
  if (has_rows && has_cols) {
1235
2.90M
    return (PARTITION_TYPE)aom_read_symbol(
1236
2.90M
        r, partition_cdf, partition_cdf_length(bsize), ACCT_STR);
1237
2.90M
  } else if (!has_rows && has_cols) {
1238
57.6k
    assert(bsize > BLOCK_8X8);
1239
57.6k
    aom_cdf_prob cdf[2];
1240
57.6k
    partition_gather_vert_alike(cdf, partition_cdf, bsize);
1241
57.6k
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
1242
57.6k
    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
1243
217k
  } else {
1244
217k
    assert(has_rows && !has_cols);
1245
217k
    assert(bsize > BLOCK_8X8);
1246
217k
    aom_cdf_prob cdf[2];
1247
217k
    partition_gather_horz_alike(cdf, partition_cdf, bsize);
1248
217k
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
1249
217k
    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
1250
217k
  }
1251
3.17M
}
1252
1253
// TODO(slavarnway): eliminate bsize and subsize in future commits
1254
static inline void decode_partition(AV1Decoder *const pbi, ThreadData *const td,
1255
                                    int mi_row, int mi_col, aom_reader *reader,
1256
6.71M
                                    BLOCK_SIZE bsize, int parse_decode_flag) {
1257
6.71M
  assert(bsize < BLOCK_SIZES_ALL);
1258
6.71M
  AV1_COMMON *const cm = &pbi->common;
1259
6.71M
  DecoderCodingBlock *const dcb = &td->dcb;
1260
6.71M
  MACROBLOCKD *const xd = &dcb->xd;
1261
6.71M
  const int bw = mi_size_wide[bsize];
1262
6.71M
  const int hbs = bw >> 1;
1263
6.71M
  PARTITION_TYPE partition;
1264
6.71M
  BLOCK_SIZE subsize;
1265
6.71M
  const int quarter_step = bw / 4;
1266
6.71M
  BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
1267
6.71M
  const int has_rows = (mi_row + hbs) < cm->mi_params.mi_rows;
1268
6.71M
  const int has_cols = (mi_col + hbs) < cm->mi_params.mi_cols;
1269
1270
6.71M
  if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols)
1271
679k
    return;
1272
1273
  // parse_decode_flag takes the following values :
1274
  // 01 - do parse only
1275
  // 10 - do decode only
1276
  // 11 - do parse and decode
1277
6.03M
  static const block_visitor_fn_t block_visit[4] = { NULL, parse_decode_block,
1278
6.03M
                                                     decode_block,
1279
6.03M
                                                     parse_decode_block };
1280
1281
6.03M
  if (parse_decode_flag & 1) {
1282
3.67M
    const int num_planes = av1_num_planes(cm);
1283
14.4M
    for (int plane = 0; plane < num_planes; ++plane) {
1284
10.7M
      int rcol0, rcol1, rrow0, rrow1;
1285
1286
      // Skip some unnecessary work if loop restoration is disabled
1287
10.7M
      if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
1288
1289
564k
      if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
1290
564k
                                             &rcol0, &rcol1, &rrow0, &rrow1)) {
1291
49.8k
        const int rstride = cm->rst_info[plane].horz_units;
1292
99.7k
        for (int rrow = rrow0; rrow < rrow1; ++rrow) {
1293
102k
          for (int rcol = rcol0; rcol < rcol1; ++rcol) {
1294
52.6k
            const int runit_idx = rcol + rrow * rstride;
1295
52.6k
            loop_restoration_read_sb_coeffs(cm, xd, reader, plane, runit_idx);
1296
52.6k
          }
1297
49.8k
        }
1298
49.8k
      }
1299
564k
    }
1300
1301
3.67M
    partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
1302
3.67M
                                    : read_partition(xd, mi_row, mi_col, reader,
1303
3.22M
                                                     has_rows, has_cols, bsize);
1304
3.67M
  } else {
1305
2.36M
    partition = get_partition(cm, mi_row, mi_col, bsize);
1306
2.36M
  }
1307
6.03M
  subsize = get_partition_subsize(bsize, partition);
1308
6.03M
  if (subsize == BLOCK_INVALID) {
1309
    // When an internal error occurs ensure that xd->mi_row is set appropriately
1310
    // w.r.t. current tile, which is used to signal processing of current row is
1311
    // done.
1312
0
    xd->mi_row = mi_row;
1313
0
    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1314
0
                       "Partition is invalid for block size %dx%d",
1315
0
                       block_size_wide[bsize], block_size_high[bsize]);
1316
0
  }
1317
  // Check the bitstream is conformant: if there is subsampling on the
1318
  // chroma planes, subsize must subsample to a valid block size.
1319
6.03M
  const struct macroblockd_plane *const pd_u = &xd->plane[1];
1320
6.03M
  if (get_plane_block_size(subsize, pd_u->subsampling_x, pd_u->subsampling_y) ==
1321
6.03M
      BLOCK_INVALID) {
1322
    // When an internal error occurs ensure that xd->mi_row is set appropriately
1323
    // w.r.t. current tile, which is used to signal processing of current row is
1324
    // done.
1325
112
    xd->mi_row = mi_row;
1326
112
    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1327
112
                       "Block size %dx%d invalid with this subsampling mode",
1328
112
                       block_size_wide[subsize], block_size_high[subsize]);
1329
112
  }
1330
1331
6.03M
#define DEC_BLOCK_STX_ARG
1332
7.42M
#define DEC_BLOCK_EPT_ARG partition,
1333
6.03M
#define DEC_BLOCK(db_r, db_c, db_subsize)                                  \
1334
7.42M
  block_visit[parse_decode_flag](pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), \
1335
7.42M
                                 reader, DEC_BLOCK_EPT_ARG(db_subsize))
1336
6.03M
#define DEC_PARTITION(db_r, db_c, db_subsize)                        \
1337
6.25M
  decode_partition(pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), reader, \
1338
6.25M
                   (db_subsize), parse_decode_flag)
1339
1340
6.03M
  switch (partition) {
1341
2.56M
    case PARTITION_NONE: DEC_BLOCK(mi_row, mi_col, subsize); break;
1342
616k
    case PARTITION_HORZ:
1343
616k
      DEC_BLOCK(mi_row, mi_col, subsize);
1344
616k
      if (has_rows) DEC_BLOCK(mi_row + hbs, mi_col, subsize);
1345
616k
      break;
1346
550k
    case PARTITION_VERT:
1347
550k
      DEC_BLOCK(mi_row, mi_col, subsize);
1348
550k
      if (has_cols) DEC_BLOCK(mi_row, mi_col + hbs, subsize);
1349
550k
      break;
1350
1.56M
    case PARTITION_SPLIT:
1351
1.56M
      DEC_PARTITION(mi_row, mi_col, subsize);
1352
1.56M
      DEC_PARTITION(mi_row, mi_col + hbs, subsize);
1353
1.56M
      DEC_PARTITION(mi_row + hbs, mi_col, subsize);
1354
1.56M
      DEC_PARTITION(mi_row + hbs, mi_col + hbs, subsize);
1355
1.56M
      break;
1356
99.5k
    case PARTITION_HORZ_A:
1357
99.5k
      DEC_BLOCK(mi_row, mi_col, bsize2);
1358
99.5k
      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
1359
99.5k
      DEC_BLOCK(mi_row + hbs, mi_col, subsize);
1360
99.5k
      break;
1361
86.8k
    case PARTITION_HORZ_B:
1362
86.8k
      DEC_BLOCK(mi_row, mi_col, subsize);
1363
86.8k
      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
1364
86.8k
      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
1365
86.8k
      break;
1366
68.6k
    case PARTITION_VERT_A:
1367
68.6k
      DEC_BLOCK(mi_row, mi_col, bsize2);
1368
68.6k
      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
1369
68.6k
      DEC_BLOCK(mi_row, mi_col + hbs, subsize);
1370
68.6k
      break;
1371
74.9k
    case PARTITION_VERT_B:
1372
74.9k
      DEC_BLOCK(mi_row, mi_col, subsize);
1373
74.9k
      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
1374
74.9k
      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
1375
74.9k
      break;
1376
207k
    case PARTITION_HORZ_4:
1377
1.03M
      for (int i = 0; i < 4; ++i) {
1378
830k
        int this_mi_row = mi_row + i * quarter_step;
1379
830k
        if (i > 0 && this_mi_row >= cm->mi_params.mi_rows) break;
1380
829k
        DEC_BLOCK(this_mi_row, mi_col, subsize);
1381
829k
      }
1382
207k
      break;
1383
211k
    case PARTITION_VERT_4:
1384
1.05M
      for (int i = 0; i < 4; ++i) {
1385
844k
        int this_mi_col = mi_col + i * quarter_step;
1386
844k
        if (i > 0 && this_mi_col >= cm->mi_params.mi_cols) break;
1387
842k
        DEC_BLOCK(mi_row, this_mi_col, subsize);
1388
842k
      }
1389
211k
      break;
1390
0
    default: assert(0 && "Invalid partition type");
1391
6.03M
  }
1392
1393
6.03M
#undef DEC_PARTITION
1394
6.03M
#undef DEC_BLOCK
1395
6.03M
#undef DEC_BLOCK_EPT_ARG
1396
6.03M
#undef DEC_BLOCK_STX_ARG
1397
1398
6.03M
  if (parse_decode_flag & 1)
1399
3.67M
    update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
1400
6.03M
}
1401
1402
static inline void setup_bool_decoder(
1403
    MACROBLOCKD *const xd, const uint8_t *data, const uint8_t *data_end,
1404
    const size_t read_size, struct aom_internal_error_info *error_info,
1405
33.4k
    aom_reader *r, uint8_t allow_update_cdf) {
1406
  // Validate the calculated partition length. If the buffer
1407
  // described by the partition can't be fully read, then restrict
1408
  // it to the portion that can be (for EC mode) or throw an error.
1409
33.4k
  if (!read_is_valid(data, read_size, data_end)) {
1410
    // When internal error occurs ensure that xd->mi_row is set appropriately
1411
    // w.r.t. current tile, which is used to signal processing of current row is
1412
    // done in row-mt decoding.
1413
0
    xd->mi_row = xd->tile.mi_row_start;
1414
1415
0
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
1416
0
                       "Truncated packet or corrupt tile length");
1417
0
  }
1418
33.4k
  if (aom_reader_init(r, data, read_size)) {
1419
    // When internal error occurs ensure that xd->mi_row is set appropriately
1420
    // w.r.t. current tile, which is used to signal processing of current row is
1421
    // done in row-mt decoding.
1422
0
    xd->mi_row = xd->tile.mi_row_start;
1423
1424
0
    aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
1425
0
                       "Failed to allocate bool decoder %d", 1);
1426
0
  }
1427
1428
33.4k
  r->allow_update_cdf = allow_update_cdf;
1429
33.4k
}
1430
1431
static inline void setup_segmentation(AV1_COMMON *const cm,
1432
32.0k
                                      struct aom_read_bit_buffer *rb) {
1433
32.0k
  struct segmentation *const seg = &cm->seg;
1434
1435
32.0k
  seg->update_map = 0;
1436
32.0k
  seg->update_data = 0;
1437
32.0k
  seg->temporal_update = 0;
1438
1439
32.0k
  seg->enabled = aom_rb_read_bit(rb);
1440
32.0k
  if (!seg->enabled) {
1441
20.7k
    if (cm->cur_frame->seg_map) {
1442
20.7k
      memset(cm->cur_frame->seg_map, 0,
1443
20.7k
             (cm->cur_frame->mi_rows * cm->cur_frame->mi_cols));
1444
20.7k
    }
1445
1446
20.7k
    memset(seg, 0, sizeof(*seg));
1447
20.7k
    segfeatures_copy(&cm->cur_frame->seg, seg);
1448
20.7k
    return;
1449
20.7k
  }
1450
11.2k
  if (cm->seg.enabled && cm->prev_frame &&
1451
4.82k
      (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) &&
1452
4.69k
      (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) {
1453
4.07k
    cm->last_frame_seg_map = cm->prev_frame->seg_map;
1454
7.21k
  } else {
1455
7.21k
    cm->last_frame_seg_map = NULL;
1456
7.21k
  }
1457
  // Read update flags
1458
11.2k
  if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
1459
    // These frames can't use previous frames, so must signal map + features
1460
6.46k
    seg->update_map = 1;
1461
6.46k
    seg->temporal_update = 0;
1462
6.46k
    seg->update_data = 1;
1463
6.46k
  } else {
1464
4.82k
    seg->update_map = aom_rb_read_bit(rb);
1465
4.82k
    if (seg->update_map) {
1466
2.37k
      seg->temporal_update = aom_rb_read_bit(rb);
1467
2.45k
    } else {
1468
2.45k
      seg->temporal_update = 0;
1469
2.45k
    }
1470
4.82k
    seg->update_data = aom_rb_read_bit(rb);
1471
4.82k
  }
1472
1473
  // Segmentation data update
1474
11.2k
  if (seg->update_data) {
1475
8.99k
    av1_clearall_segfeatures(seg);
1476
1477
80.8k
    for (int i = 0; i < MAX_SEGMENTS; i++) {
1478
646k
      for (int j = 0; j < SEG_LVL_MAX; j++) {
1479
574k
        int data = 0;
1480
574k
        const int feature_enabled = aom_rb_read_bit(rb);
1481
574k
        if (feature_enabled) {
1482
257k
          av1_enable_segfeature(seg, i, j);
1483
1484
257k
          const int data_max = av1_seg_feature_data_max(j);
1485
257k
          const int data_min = -data_max;
1486
257k
          const int ubits = get_unsigned_bits(data_max);
1487
1488
257k
          if (av1_is_segfeature_signed(j)) {
1489
164k
            data = aom_rb_read_inv_signed_literal(rb, ubits);
1490
164k
          } else {
1491
92.9k
            data = aom_rb_read_literal(rb, ubits);
1492
92.9k
          }
1493
1494
257k
          data = clamp(data, data_min, data_max);
1495
257k
        }
1496
574k
        av1_set_segdata(seg, i, j, data);
1497
574k
      }
1498
71.8k
    }
1499
8.99k
    av1_calculate_segdata(seg);
1500
8.99k
  } else if (cm->prev_frame) {
1501
2.29k
    segfeatures_copy(seg, &cm->prev_frame->seg);
1502
2.29k
  }
1503
11.2k
  segfeatures_copy(&cm->cur_frame->seg, seg);
1504
11.2k
}
1505
1506
static inline void decode_restoration_mode(AV1_COMMON *cm,
1507
8.64k
                                           struct aom_read_bit_buffer *rb) {
1508
8.64k
  assert(!cm->features.all_lossless);
1509
8.64k
  const int num_planes = av1_num_planes(cm);
1510
8.64k
  if (cm->features.allow_intrabc) return;
1511
7.58k
  int all_none = 1, chroma_none = 1;
1512
27.1k
  for (int p = 0; p < num_planes; ++p) {
1513
19.5k
    RestorationInfo *rsi = &cm->rst_info[p];
1514
19.5k
    if (aom_rb_read_bit(rb)) {
1515
5.86k
      rsi->frame_restoration_type =
1516
5.86k
          aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
1517
13.6k
    } else {
1518
13.6k
      rsi->frame_restoration_type =
1519
13.6k
          aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
1520
13.6k
    }
1521
19.5k
    if (rsi->frame_restoration_type != RESTORE_NONE) {
1522
9.34k
      all_none = 0;
1523
9.34k
      chroma_none &= p == 0;
1524
9.34k
    }
1525
19.5k
  }
1526
7.58k
  if (!all_none) {
1527
4.88k
    assert(cm->seq_params->sb_size == BLOCK_64X64 ||
1528
4.88k
           cm->seq_params->sb_size == BLOCK_128X128);
1529
4.88k
    const int sb_size = cm->seq_params->sb_size == BLOCK_128X128 ? 128 : 64;
1530
1531
18.9k
    for (int p = 0; p < num_planes; ++p)
1532
14.0k
      cm->rst_info[p].restoration_unit_size = sb_size;
1533
1534
4.88k
    RestorationInfo *rsi = &cm->rst_info[0];
1535
1536
4.88k
    if (sb_size == 64) {
1537
2.40k
      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1538
2.40k
    }
1539
4.88k
    if (rsi->restoration_unit_size > 64) {
1540
3.82k
      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1541
3.82k
    }
1542
4.88k
  } else {
1543
2.70k
    const int size = RESTORATION_UNITSIZE_MAX;
1544
8.17k
    for (int p = 0; p < num_planes; ++p)
1545
5.47k
      cm->rst_info[p].restoration_unit_size = size;
1546
2.70k
  }
1547
1548
7.58k
  if (num_planes > 1) {
1549
5.98k
    int s =
1550
5.98k
        AOMMIN(cm->seq_params->subsampling_x, cm->seq_params->subsampling_y);
1551
5.98k
    if (s && !chroma_none) {
1552
2.05k
      cm->rst_info[1].restoration_unit_size =
1553
2.05k
          cm->rst_info[0].restoration_unit_size >> (aom_rb_read_bit(rb) * s);
1554
3.93k
    } else {
1555
3.93k
      cm->rst_info[1].restoration_unit_size =
1556
3.93k
          cm->rst_info[0].restoration_unit_size;
1557
3.93k
    }
1558
5.98k
    cm->rst_info[2].restoration_unit_size =
1559
5.98k
        cm->rst_info[1].restoration_unit_size;
1560
5.98k
  }
1561
7.58k
}
1562
1563
static inline void read_wiener_filter(int wiener_win, WienerInfo *wiener_info,
1564
                                      WienerInfo *ref_wiener_info,
1565
15.1k
                                      aom_reader *rb) {
1566
15.1k
  memset(wiener_info->vfilter, 0, sizeof(wiener_info->vfilter));
1567
15.1k
  memset(wiener_info->hfilter, 0, sizeof(wiener_info->hfilter));
1568
1569
15.1k
  if (wiener_win == WIENER_WIN)
1570
5.52k
    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
1571
5.52k
        aom_read_primitive_refsubexpfin(
1572
5.52k
            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1573
5.52k
            WIENER_FILT_TAP0_SUBEXP_K,
1574
5.52k
            ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1575
5.52k
        WIENER_FILT_TAP0_MINV;
1576
9.61k
  else
1577
9.61k
    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] = 0;
1578
15.1k
  wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
1579
15.1k
      aom_read_primitive_refsubexpfin(
1580
15.1k
          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1581
15.1k
          WIENER_FILT_TAP1_SUBEXP_K,
1582
15.1k
          ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
1583
15.1k
      WIENER_FILT_TAP1_MINV;
1584
15.1k
  wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
1585
15.1k
      aom_read_primitive_refsubexpfin(
1586
15.1k
          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1587
15.1k
          WIENER_FILT_TAP2_SUBEXP_K,
1588
15.1k
          ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
1589
15.1k
      WIENER_FILT_TAP2_MINV;
1590
  // The central element has an implicit +WIENER_FILT_STEP
1591
15.1k
  wiener_info->vfilter[WIENER_HALFWIN] =
1592
15.1k
      -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
1593
15.1k
            wiener_info->vfilter[2]);
1594
1595
15.1k
  if (wiener_win == WIENER_WIN)
1596
5.52k
    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
1597
5.52k
        aom_read_primitive_refsubexpfin(
1598
5.52k
            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1599
5.52k
            WIENER_FILT_TAP0_SUBEXP_K,
1600
5.52k
            ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1601
5.52k
        WIENER_FILT_TAP0_MINV;
1602
9.61k
  else
1603
9.61k
    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] = 0;
1604
15.1k
  wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
1605
15.1k
      aom_read_primitive_refsubexpfin(
1606
15.1k
          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1607
15.1k
          WIENER_FILT_TAP1_SUBEXP_K,
1608
15.1k
          ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
1609
15.1k
      WIENER_FILT_TAP1_MINV;
1610
15.1k
  wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
1611
15.1k
      aom_read_primitive_refsubexpfin(
1612
15.1k
          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1613
15.1k
          WIENER_FILT_TAP2_SUBEXP_K,
1614
15.1k
          ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
1615
15.1k
      WIENER_FILT_TAP2_MINV;
1616
  // The central element has an implicit +WIENER_FILT_STEP
1617
15.1k
  wiener_info->hfilter[WIENER_HALFWIN] =
1618
15.1k
      -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
1619
15.1k
            wiener_info->hfilter[2]);
1620
15.1k
  memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
1621
15.1k
}
1622
1623
static inline void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
1624
                                       SgrprojInfo *ref_sgrproj_info,
1625
16.8k
                                       aom_reader *rb) {
1626
16.8k
  sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
1627
16.8k
  const sgr_params_type *params = &av1_sgr_params[sgrproj_info->ep];
1628
1629
16.8k
  if (params->r[0] == 0) {
1630
3.72k
    sgrproj_info->xqd[0] = 0;
1631
3.72k
    sgrproj_info->xqd[1] =
1632
3.72k
        aom_read_primitive_refsubexpfin(
1633
3.72k
            rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1634
3.72k
            ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1635
3.72k
        SGRPROJ_PRJ_MIN1;
1636
13.0k
  } else if (params->r[1] == 0) {
1637
3.97k
    sgrproj_info->xqd[0] =
1638
3.97k
        aom_read_primitive_refsubexpfin(
1639
3.97k
            rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1640
3.97k
            ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1641
3.97k
        SGRPROJ_PRJ_MIN0;
1642
3.97k
    sgrproj_info->xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - sgrproj_info->xqd[0],
1643
3.97k
                                 SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
1644
9.11k
  } else {
1645
9.11k
    sgrproj_info->xqd[0] =
1646
9.11k
        aom_read_primitive_refsubexpfin(
1647
9.11k
            rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1648
9.11k
            ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1649
9.11k
        SGRPROJ_PRJ_MIN0;
1650
9.11k
    sgrproj_info->xqd[1] =
1651
9.11k
        aom_read_primitive_refsubexpfin(
1652
9.11k
            rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1653
9.11k
            ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1654
9.11k
        SGRPROJ_PRJ_MIN1;
1655
9.11k
  }
1656
1657
16.8k
  memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
1658
16.8k
}
1659
1660
static inline void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
1661
                                                   MACROBLOCKD *xd,
1662
                                                   aom_reader *const r,
1663
52.6k
                                                   int plane, int runit_idx) {
1664
52.6k
  const RestorationInfo *rsi = &cm->rst_info[plane];
1665
52.6k
  RestorationUnitInfo *rui = &rsi->unit_info[runit_idx];
1666
52.6k
  assert(rsi->frame_restoration_type != RESTORE_NONE);
1667
1668
52.6k
  assert(!cm->features.all_lossless);
1669
1670
52.6k
  const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN;
1671
52.6k
  WienerInfo *wiener_info = xd->wiener_info + plane;
1672
52.6k
  SgrprojInfo *sgrproj_info = xd->sgrproj_info + plane;
1673
1674
52.6k
  if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
1675
26.2k
    rui->restoration_type =
1676
26.2k
        aom_read_symbol(r, xd->tile_ctx->switchable_restore_cdf,
1677
26.2k
                        RESTORE_SWITCHABLE_TYPES, ACCT_STR);
1678
26.2k
    switch (rui->restoration_type) {
1679
6.65k
      case RESTORE_WIENER:
1680
6.65k
        read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
1681
6.65k
        break;
1682
12.2k
      case RESTORE_SGRPROJ:
1683
12.2k
        read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
1684
12.2k
        break;
1685
7.33k
      default: assert(rui->restoration_type == RESTORE_NONE); break;
1686
26.2k
    }
1687
26.4k
  } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
1688
14.7k
    if (aom_read_symbol(r, xd->tile_ctx->wiener_restore_cdf, 2, ACCT_STR)) {
1689
8.49k
      rui->restoration_type = RESTORE_WIENER;
1690
8.49k
      read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
1691
8.49k
    } else {
1692
6.26k
      rui->restoration_type = RESTORE_NONE;
1693
6.26k
    }
1694
14.7k
  } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
1695
11.6k
    if (aom_read_symbol(r, xd->tile_ctx->sgrproj_restore_cdf, 2, ACCT_STR)) {
1696
4.52k
      rui->restoration_type = RESTORE_SGRPROJ;
1697
4.52k
      read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
1698
7.13k
    } else {
1699
7.13k
      rui->restoration_type = RESTORE_NONE;
1700
7.13k
    }
1701
11.6k
  }
1702
52.6k
}
1703
1704
static inline void setup_loopfilter(AV1_COMMON *cm,
1705
32.0k
                                    struct aom_read_bit_buffer *rb) {
1706
32.0k
  const int num_planes = av1_num_planes(cm);
1707
32.0k
  struct loopfilter *lf = &cm->lf;
1708
1709
32.0k
  if (cm->features.allow_intrabc || cm->features.coded_lossless) {
1710
    // write default deltas to frame buffer
1711
7.18k
    av1_set_default_ref_deltas(cm->cur_frame->ref_deltas);
1712
7.18k
    av1_set_default_mode_deltas(cm->cur_frame->mode_deltas);
1713
7.18k
    return;
1714
7.18k
  }
1715
32.0k
  assert(!cm->features.coded_lossless);
1716
24.8k
  if (cm->prev_frame) {
1717
    // write deltas to frame buffer
1718
11.1k
    memcpy(lf->ref_deltas, cm->prev_frame->ref_deltas, REF_FRAMES);
1719
11.1k
    memcpy(lf->mode_deltas, cm->prev_frame->mode_deltas, MAX_MODE_LF_DELTAS);
1720
13.6k
  } else {
1721
13.6k
    av1_set_default_ref_deltas(lf->ref_deltas);
1722
13.6k
    av1_set_default_mode_deltas(lf->mode_deltas);
1723
13.6k
  }
1724
24.8k
  lf->filter_level[0] = aom_rb_read_literal(rb, 6);
1725
24.8k
  lf->filter_level[1] = aom_rb_read_literal(rb, 6);
1726
24.8k
  if (num_planes > 1) {
1727
18.6k
    if (lf->filter_level[0] || lf->filter_level[1]) {
1728
15.3k
      lf->filter_level_u = aom_rb_read_literal(rb, 6);
1729
15.3k
      lf->filter_level_v = aom_rb_read_literal(rb, 6);
1730
15.3k
    }
1731
18.6k
  }
1732
24.8k
  lf->sharpness_level = aom_rb_read_literal(rb, 3);
1733
1734
  // Read in loop filter deltas applied at the MB level based on mode or ref
1735
  // frame.
1736
24.8k
  lf->mode_ref_delta_update = 0;
1737
1738
24.8k
  lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
1739
24.8k
  if (lf->mode_ref_delta_enabled) {
1740
8.97k
    lf->mode_ref_delta_update = aom_rb_read_bit(rb);
1741
8.97k
    if (lf->mode_ref_delta_update) {
1742
29.7k
      for (int i = 0; i < REF_FRAMES; i++)
1743
26.4k
        if (aom_rb_read_bit(rb))
1744
15.6k
          lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
1745
1746
9.90k
      for (int i = 0; i < MAX_MODE_LF_DELTAS; i++)
1747
6.59k
        if (aom_rb_read_bit(rb))
1748
2.61k
          lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
1749
3.30k
    }
1750
8.97k
  }
1751
1752
  // write deltas to frame buffer
1753
24.8k
  memcpy(cm->cur_frame->ref_deltas, lf->ref_deltas, REF_FRAMES);
1754
24.8k
  memcpy(cm->cur_frame->mode_deltas, lf->mode_deltas, MAX_MODE_LF_DELTAS);
1755
24.8k
}
1756
1757
6.75k
static inline void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
1758
6.75k
  const int num_planes = av1_num_planes(cm);
1759
6.75k
  CdefInfo *const cdef_info = &cm->cdef_info;
1760
1761
6.75k
  if (cm->features.allow_intrabc) return;
1762
5.84k
  cdef_info->cdef_damping = aom_rb_read_literal(rb, 2) + 3;
1763
5.84k
  cdef_info->cdef_bits = aom_rb_read_literal(rb, 2);
1764
5.84k
  cdef_info->nb_cdef_strengths = 1 << cdef_info->cdef_bits;
1765
17.7k
  for (int i = 0; i < cdef_info->nb_cdef_strengths; i++) {
1766
11.8k
    cdef_info->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
1767
11.8k
    cdef_info->cdef_uv_strengths[i] =
1768
11.8k
        num_planes > 1 ? aom_rb_read_literal(rb, CDEF_STRENGTH_BITS) : 0;
1769
11.8k
  }
1770
5.84k
}
1771
1772
83.3k
static inline int read_delta_q(struct aom_read_bit_buffer *rb) {
1773
83.3k
  return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
1774
83.3k
}
1775
1776
static inline void setup_quantization(CommonQuantParams *quant_params,
1777
                                      int num_planes, bool separate_uv_delta_q,
1778
32.0k
                                      struct aom_read_bit_buffer *rb) {
1779
32.0k
  quant_params->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
1780
32.0k
  quant_params->y_dc_delta_q = read_delta_q(rb);
1781
32.0k
  if (num_planes > 1) {
1782
23.2k
    int diff_uv_delta = 0;
1783
23.2k
    if (separate_uv_delta_q) diff_uv_delta = aom_rb_read_bit(rb);
1784
23.2k
    quant_params->u_dc_delta_q = read_delta_q(rb);
1785
23.2k
    quant_params->u_ac_delta_q = read_delta_q(rb);
1786
23.2k
    if (diff_uv_delta) {
1787
2.34k
      quant_params->v_dc_delta_q = read_delta_q(rb);
1788
2.34k
      quant_params->v_ac_delta_q = read_delta_q(rb);
1789
20.9k
    } else {
1790
20.9k
      quant_params->v_dc_delta_q = quant_params->u_dc_delta_q;
1791
20.9k
      quant_params->v_ac_delta_q = quant_params->u_ac_delta_q;
1792
20.9k
    }
1793
23.2k
  } else {
1794
8.74k
    quant_params->u_dc_delta_q = 0;
1795
8.74k
    quant_params->u_ac_delta_q = 0;
1796
8.74k
    quant_params->v_dc_delta_q = 0;
1797
8.74k
    quant_params->v_ac_delta_q = 0;
1798
8.74k
  }
1799
32.0k
  quant_params->using_qmatrix = aom_rb_read_bit(rb);
1800
32.0k
  if (quant_params->using_qmatrix) {
1801
15.7k
    quant_params->qmatrix_level_y = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1802
15.7k
    quant_params->qmatrix_level_u = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1803
15.7k
    if (!separate_uv_delta_q)
1804
13.4k
      quant_params->qmatrix_level_v = quant_params->qmatrix_level_u;
1805
2.31k
    else
1806
2.31k
      quant_params->qmatrix_level_v = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1807
16.2k
  } else {
1808
16.2k
    quant_params->qmatrix_level_y = 0;
1809
16.2k
    quant_params->qmatrix_level_u = 0;
1810
16.2k
    quant_params->qmatrix_level_v = 0;
1811
16.2k
  }
1812
32.0k
}
1813
1814
// Get global dequant matrix.
1815
static const qm_val_t *get_iqmatrix(const CommonQuantParams *quant_params,
1816
6.32M
                                    int qmlevel, int plane, TX_SIZE tx_size) {
1817
6.32M
  assert(quant_params->giqmatrix[qmlevel][plane][tx_size] != NULL ||
1818
6.32M
         qmlevel == NUM_QM_LEVELS - 1);
1819
6.32M
  return quant_params->giqmatrix[qmlevel][plane][tx_size];
1820
6.32M
}
1821
1822
// Build y/uv dequant values based on segmentation.
1823
static inline void setup_segmentation_dequant(AV1_COMMON *const cm,
1824
32.0k
                                              MACROBLOCKD *const xd) {
1825
32.0k
  const int bit_depth = cm->seq_params->bit_depth;
1826
  // When segmentation is disabled, only the first value is used.  The
1827
  // remaining are don't cares.
1828
32.0k
  const int max_segments = cm->seg.enabled ? MAX_SEGMENTS : 1;
1829
32.0k
  CommonQuantParams *const quant_params = &cm->quant_params;
1830
142k
  for (int i = 0; i < max_segments; ++i) {
1831
110k
    const int qindex = xd->qindex[i];
1832
110k
    quant_params->y_dequant_QTX[i][0] =
1833
110k
        av1_dc_quant_QTX(qindex, quant_params->y_dc_delta_q, bit_depth);
1834
110k
    quant_params->y_dequant_QTX[i][1] = av1_ac_quant_QTX(qindex, 0, bit_depth);
1835
110k
    quant_params->u_dequant_QTX[i][0] =
1836
110k
        av1_dc_quant_QTX(qindex, quant_params->u_dc_delta_q, bit_depth);
1837
110k
    quant_params->u_dequant_QTX[i][1] =
1838
110k
        av1_ac_quant_QTX(qindex, quant_params->u_ac_delta_q, bit_depth);
1839
110k
    quant_params->v_dequant_QTX[i][0] =
1840
110k
        av1_dc_quant_QTX(qindex, quant_params->v_dc_delta_q, bit_depth);
1841
110k
    quant_params->v_dequant_QTX[i][1] =
1842
110k
        av1_ac_quant_QTX(qindex, quant_params->v_ac_delta_q, bit_depth);
1843
110k
    const int use_qmatrix = av1_use_qmatrix(quant_params, xd, i);
1844
    // NB: depends on base index so there is only 1 set per frame
1845
    // No quant weighting when lossless or signalled not using QM
1846
110k
    const int qmlevel_y =
1847
110k
        use_qmatrix ? quant_params->qmatrix_level_y : NUM_QM_LEVELS - 1;
1848
2.21M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1849
2.10M
      quant_params->y_iqmatrix[i][j] =
1850
2.10M
          get_iqmatrix(quant_params, qmlevel_y, AOM_PLANE_Y, j);
1851
2.10M
    }
1852
110k
    const int qmlevel_u =
1853
110k
        use_qmatrix ? quant_params->qmatrix_level_u : NUM_QM_LEVELS - 1;
1854
2.21M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1855
2.10M
      quant_params->u_iqmatrix[i][j] =
1856
2.10M
          get_iqmatrix(quant_params, qmlevel_u, AOM_PLANE_U, j);
1857
2.10M
    }
1858
110k
    const int qmlevel_v =
1859
110k
        use_qmatrix ? quant_params->qmatrix_level_v : NUM_QM_LEVELS - 1;
1860
2.21M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1861
2.10M
      quant_params->v_iqmatrix[i][j] =
1862
2.10M
          get_iqmatrix(quant_params, qmlevel_v, AOM_PLANE_V, j);
1863
2.10M
    }
1864
110k
  }
1865
32.0k
}
1866
1867
12.6k
static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
1868
12.6k
  return aom_rb_read_bit(rb) ? SWITCHABLE
1869
12.6k
                             : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
1870
12.6k
}
1871
1872
static void read_frame_size(struct aom_read_bit_buffer *rb, int num_bits_width,
1873
14.7k
                            int num_bits_height, int *width, int *height) {
1874
14.7k
  *width = aom_rb_read_literal(rb, num_bits_width) + 1;
1875
14.7k
  *height = aom_rb_read_literal(rb, num_bits_height) + 1;
1876
14.7k
}
1877
1878
static inline void setup_render_size(AV1_COMMON *cm,
1879
21.8k
                                     struct aom_read_bit_buffer *rb) {
1880
21.8k
  cm->render_width = cm->superres_upscaled_width;
1881
21.8k
  cm->render_height = cm->superres_upscaled_height;
1882
21.8k
  if (aom_rb_read_bit(rb))
1883
10.8k
    read_frame_size(rb, 16, 16, &cm->render_width, &cm->render_height);
1884
21.8k
}
1885
1886
// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
1887
static inline void setup_superres(AV1_COMMON *const cm,
1888
                                  struct aom_read_bit_buffer *rb, int *width,
1889
32.1k
                                  int *height) {
1890
32.1k
  cm->superres_upscaled_width = *width;
1891
32.1k
  cm->superres_upscaled_height = *height;
1892
1893
32.1k
  const SequenceHeader *const seq_params = cm->seq_params;
1894
32.1k
  if (!seq_params->enable_superres) return;
1895
1896
25.3k
  if (aom_rb_read_bit(rb)) {
1897
6.95k
    cm->superres_scale_denominator =
1898
6.95k
        (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
1899
6.95k
    cm->superres_scale_denominator += SUPERRES_SCALE_DENOMINATOR_MIN;
1900
    // Don't edit cm->width or cm->height directly, or the buffers won't get
1901
    // resized correctly
1902
6.95k
    av1_calculate_scaled_superres_size(width, height,
1903
6.95k
                                       cm->superres_scale_denominator);
1904
18.3k
  } else {
1905
    // 1:1 scaling - ie. no scaling, scale not provided
1906
18.3k
    cm->superres_scale_denominator = SCALE_NUMERATOR;
1907
18.3k
  }
1908
25.3k
}
1909
1910
static inline void resize_context_buffers(AV1_COMMON *cm, int width,
1911
32.1k
                                          int height) {
1912
32.1k
#if CONFIG_SIZE_LIMIT
1913
32.1k
  if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1914
3
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1915
3
                       "Dimensions of %dx%d beyond allowed size of %dx%d.",
1916
3
                       width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1917
32.1k
#endif
1918
32.1k
  if (cm->width != width || cm->height != height) {
1919
19.7k
    const int new_mi_rows = CEIL_POWER_OF_TWO(height, MI_SIZE_LOG2);
1920
19.7k
    const int new_mi_cols = CEIL_POWER_OF_TWO(width, MI_SIZE_LOG2);
1921
1922
    // Allocations in av1_alloc_context_buffers() depend on individual
1923
    // dimensions as well as the overall size.
1924
19.7k
    if (new_mi_cols > cm->mi_params.mi_cols ||
1925
17.3k
        new_mi_rows > cm->mi_params.mi_rows) {
1926
17.3k
      if (av1_alloc_context_buffers(cm, width, height, BLOCK_4X4)) {
1927
        // The cm->mi_* values have been cleared and any existing context
1928
        // buffers have been freed. Clear cm->width and cm->height to be
1929
        // consistent and to force a realloc next time.
1930
2
        cm->width = 0;
1931
2
        cm->height = 0;
1932
2
        aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1933
2
                           "Failed to allocate context buffers");
1934
2
      }
1935
17.3k
    } else {
1936
2.47k
      cm->mi_params.set_mb_mi(&cm->mi_params, width, height, BLOCK_4X4);
1937
2.47k
    }
1938
19.7k
    av1_init_mi_buffers(&cm->mi_params);
1939
19.7k
    cm->width = width;
1940
19.7k
    cm->height = height;
1941
19.7k
  }
1942
1943
32.1k
  ensure_mv_buffer(cm->cur_frame, cm);
1944
32.1k
  cm->cur_frame->width = cm->width;
1945
32.1k
  cm->cur_frame->height = cm->height;
1946
32.1k
}
1947
1948
32.0k
static inline void setup_buffer_pool(AV1_COMMON *cm) {
1949
32.0k
  BufferPool *const pool = cm->buffer_pool;
1950
32.0k
  const SequenceHeader *const seq_params = cm->seq_params;
1951
1952
32.0k
  lock_buffer_pool(pool);
1953
32.0k
  if (aom_realloc_frame_buffer(
1954
32.0k
          &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
1955
32.0k
          seq_params->subsampling_y, seq_params->use_highbitdepth,
1956
32.0k
          AOM_DEC_BORDER_IN_PIXELS, cm->features.byte_alignment,
1957
32.0k
          &cm->cur_frame->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv,
1958
32.0k
          false, 0)) {
1959
3
    unlock_buffer_pool(pool);
1960
3
    aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1961
3
                       "Failed to allocate frame buffer");
1962
3
  }
1963
32.0k
  unlock_buffer_pool(pool);
1964
1965
32.0k
  cm->cur_frame->buf.bit_depth = (unsigned int)seq_params->bit_depth;
1966
32.0k
  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
1967
32.0k
  cm->cur_frame->buf.transfer_characteristics =
1968
32.0k
      seq_params->transfer_characteristics;
1969
32.0k
  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
1970
32.0k
  cm->cur_frame->buf.monochrome = seq_params->monochrome;
1971
32.0k
  cm->cur_frame->buf.chroma_sample_position =
1972
32.0k
      seq_params->chroma_sample_position;
1973
32.0k
  cm->cur_frame->buf.color_range = seq_params->color_range;
1974
32.0k
  cm->cur_frame->buf.render_width = cm->render_width;
1975
32.0k
  cm->cur_frame->buf.render_height = cm->render_height;
1976
32.0k
}
1977
1978
static inline void setup_frame_size(AV1_COMMON *cm,
1979
                                    int frame_size_override_flag,
1980
21.6k
                                    struct aom_read_bit_buffer *rb) {
1981
21.6k
  const SequenceHeader *const seq_params = cm->seq_params;
1982
21.6k
  int width, height;
1983
1984
21.6k
  if (frame_size_override_flag) {
1985
3.62k
    int num_bits_width = seq_params->num_bits_width;
1986
3.62k
    int num_bits_height = seq_params->num_bits_height;
1987
3.62k
    read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
1988
3.62k
    if (width > seq_params->max_frame_width ||
1989
3.61k
        height > seq_params->max_frame_height) {
1990
15
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1991
15
                         "Frame dimensions are larger than the maximum values");
1992
15
    }
1993
18.0k
  } else {
1994
18.0k
    width = seq_params->max_frame_width;
1995
18.0k
    height = seq_params->max_frame_height;
1996
18.0k
  }
1997
1998
21.6k
  setup_superres(cm, rb, &width, &height);
1999
21.6k
  resize_context_buffers(cm, width, height);
2000
21.6k
  setup_render_size(cm, rb);
2001
21.6k
  setup_buffer_pool(cm);
2002
21.6k
}
2003
2004
static inline void setup_sb_size(SequenceHeader *seq_params,
2005
19.1k
                                 struct aom_read_bit_buffer *rb) {
2006
19.1k
  set_sb_size(seq_params, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
2007
19.1k
}
2008
2009
static inline int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
2010
                                          int ref_xss, int ref_yss,
2011
                                          aom_bit_depth_t this_bit_depth,
2012
73.2k
                                          int this_xss, int this_yss) {
2013
73.2k
  return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2014
73.1k
         ref_yss == this_yss;
2015
73.2k
}
2016
2017
static inline void setup_frame_size_with_refs(AV1_COMMON *cm,
2018
10.4k
                                              struct aom_read_bit_buffer *rb) {
2019
10.4k
  int width, height;
2020
10.4k
  int found = 0;
2021
10.4k
  int has_valid_ref_frame = 0;
2022
17.1k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2023
16.9k
    if (aom_rb_read_bit(rb)) {
2024
10.2k
      const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
2025
      // This will never be NULL in a normal stream, as streams are required to
2026
      // have a shown keyframe before any inter frames, which would refresh all
2027
      // the reference buffers. However, it might be null if we're starting in
2028
      // the middle of a stream, and static analysis will error if we don't do
2029
      // a null check here.
2030
10.2k
      if (ref_buf == NULL) {
2031
0
        aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2032
0
                           "Invalid condition: invalid reference buffer");
2033
10.2k
      } else {
2034
10.2k
        const YV12_BUFFER_CONFIG *const buf = &ref_buf->buf;
2035
10.2k
        width = buf->y_crop_width;
2036
10.2k
        height = buf->y_crop_height;
2037
10.2k
        cm->render_width = buf->render_width;
2038
10.2k
        cm->render_height = buf->render_height;
2039
10.2k
        setup_superres(cm, rb, &width, &height);
2040
10.2k
        resize_context_buffers(cm, width, height);
2041
10.2k
        found = 1;
2042
10.2k
        break;
2043
10.2k
      }
2044
10.2k
    }
2045
16.9k
  }
2046
2047
10.4k
  const SequenceHeader *const seq_params = cm->seq_params;
2048
10.4k
  if (!found) {
2049
265
    int num_bits_width = seq_params->num_bits_width;
2050
265
    int num_bits_height = seq_params->num_bits_height;
2051
2052
265
    read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
2053
265
    setup_superres(cm, rb, &width, &height);
2054
265
    resize_context_buffers(cm, width, height);
2055
265
    setup_render_size(cm, rb);
2056
265
  }
2057
2058
10.4k
  if (width <= 0 || height <= 0)
2059
0
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2060
0
                       "Invalid frame size");
2061
2062
  // Check to make sure at least one of frames that this frame references
2063
  // has valid dimensions.
2064
83.7k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2065
73.2k
    const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
2066
73.2k
    has_valid_ref_frame |=
2067
73.2k
        valid_ref_frame_size(ref_frame->buf.y_crop_width,
2068
73.2k
                             ref_frame->buf.y_crop_height, width, height);
2069
73.2k
  }
2070
10.4k
  if (!has_valid_ref_frame)
2071
7
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2072
7
                       "Referenced frame has invalid size");
2073
83.6k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2074
73.2k
    const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
2075
73.2k
    if (!valid_ref_frame_img_fmt(
2076
73.2k
            ref_frame->buf.bit_depth, ref_frame->buf.subsampling_x,
2077
73.2k
            ref_frame->buf.subsampling_y, seq_params->bit_depth,
2078
73.2k
            seq_params->subsampling_x, seq_params->subsampling_y))
2079
6
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2080
6
                         "Referenced frame has incompatible color format");
2081
73.2k
  }
2082
10.4k
  setup_buffer_pool(cm);
2083
10.4k
}
2084
2085
// Same function as av1_read_uniform but reading from uncompresses header wb
2086
35.9k
static int rb_read_uniform(struct aom_read_bit_buffer *const rb, int n) {
2087
35.9k
  const int l = get_unsigned_bits(n);
2088
35.9k
  const int m = (1 << l) - n;
2089
35.9k
  const int v = aom_rb_read_literal(rb, l - 1);
2090
35.9k
  assert(l != 0);
2091
35.9k
  if (v < m)
2092
33.2k
    return v;
2093
2.64k
  else
2094
2.64k
    return (v << 1) - m + aom_rb_read_bit(rb);
2095
35.9k
}
2096
2097
static inline void read_tile_info_max_tile(
2098
32.0k
    AV1_COMMON *const cm, struct aom_read_bit_buffer *const rb) {
2099
32.0k
  const SequenceHeader *const seq_params = cm->seq_params;
2100
32.0k
  CommonTileParams *const tiles = &cm->tiles;
2101
32.0k
  int width_sb =
2102
32.0k
      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
2103
32.0k
  int height_sb =
2104
32.0k
      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
2105
2106
32.0k
  av1_get_tile_limits(cm);
2107
32.0k
  tiles->uniform_spacing = aom_rb_read_bit(rb);
2108
2109
  // Read tile columns
2110
32.0k
  if (tiles->uniform_spacing) {
2111
16.3k
    tiles->log2_cols = tiles->min_log2_cols;
2112
16.4k
    while (tiles->log2_cols < tiles->max_log2_cols) {
2113
3.75k
      if (!aom_rb_read_bit(rb)) {
2114
3.67k
        break;
2115
3.67k
      }
2116
81
      tiles->log2_cols++;
2117
81
    }
2118
16.3k
  } else {
2119
15.7k
    int i;
2120
15.7k
    int start_sb;
2121
32.5k
    for (i = 0, start_sb = 0; width_sb > 0 && i < MAX_TILE_COLS; i++) {
2122
16.7k
      const int size_sb =
2123
16.7k
          1 + rb_read_uniform(rb, AOMMIN(width_sb, tiles->max_width_sb));
2124
16.7k
      tiles->col_start_sb[i] = start_sb;
2125
16.7k
      start_sb += size_sb;
2126
16.7k
      width_sb -= size_sb;
2127
16.7k
    }
2128
15.7k
    tiles->cols = i;
2129
15.7k
    tiles->col_start_sb[i] = start_sb + width_sb;
2130
15.7k
  }
2131
32.0k
  av1_calculate_tile_cols(seq_params, cm->mi_params.mi_rows,
2132
32.0k
                          cm->mi_params.mi_cols, tiles);
2133
2134
  // Read tile rows
2135
32.0k
  if (tiles->uniform_spacing) {
2136
16.3k
    tiles->log2_rows = tiles->min_log2_rows;
2137
16.4k
    while (tiles->log2_rows < tiles->max_log2_rows) {
2138
5.69k
      if (!aom_rb_read_bit(rb)) {
2139
5.59k
        break;
2140
5.59k
      }
2141
99
      tiles->log2_rows++;
2142
99
    }
2143
16.3k
  } else {
2144
15.7k
    int i;
2145
15.7k
    int start_sb;
2146
34.8k
    for (i = 0, start_sb = 0; height_sb > 0 && i < MAX_TILE_ROWS; i++) {
2147
19.1k
      const int size_sb =
2148
19.1k
          1 + rb_read_uniform(rb, AOMMIN(height_sb, tiles->max_height_sb));
2149
19.1k
      tiles->row_start_sb[i] = start_sb;
2150
19.1k
      start_sb += size_sb;
2151
19.1k
      height_sb -= size_sb;
2152
19.1k
    }
2153
15.7k
    tiles->rows = i;
2154
15.7k
    tiles->row_start_sb[i] = start_sb + height_sb;
2155
15.7k
  }
2156
32.0k
  av1_calculate_tile_rows(seq_params, cm->mi_params.mi_rows, tiles);
2157
32.0k
}
2158
2159
0
void av1_set_single_tile_decoding_mode(AV1_COMMON *const cm) {
2160
0
  cm->tiles.single_tile_decoding = 0;
2161
0
  if (cm->tiles.large_scale) {
2162
0
    struct loopfilter *lf = &cm->lf;
2163
0
    RestorationInfo *const rst_info = cm->rst_info;
2164
0
    const CdefInfo *const cdef_info = &cm->cdef_info;
2165
2166
    // Figure out single_tile_decoding by loopfilter_level.
2167
0
    const int no_loopfilter = !(lf->filter_level[0] || lf->filter_level[1]);
2168
0
    const int no_cdef = cdef_info->cdef_bits == 0 &&
2169
0
                        cdef_info->cdef_strengths[0] == 0 &&
2170
0
                        cdef_info->cdef_uv_strengths[0] == 0;
2171
0
    const int no_restoration =
2172
0
        rst_info[0].frame_restoration_type == RESTORE_NONE &&
2173
0
        rst_info[1].frame_restoration_type == RESTORE_NONE &&
2174
0
        rst_info[2].frame_restoration_type == RESTORE_NONE;
2175
0
    assert(IMPLIES(cm->features.coded_lossless, no_loopfilter && no_cdef));
2176
0
    assert(IMPLIES(cm->features.all_lossless, no_restoration));
2177
0
    cm->tiles.single_tile_decoding = no_loopfilter && no_cdef && no_restoration;
2178
0
  }
2179
0
}
2180
2181
static inline void read_tile_info(AV1Decoder *const pbi,
2182
32.0k
                                  struct aom_read_bit_buffer *const rb) {
2183
32.0k
  AV1_COMMON *const cm = &pbi->common;
2184
2185
32.0k
  read_tile_info_max_tile(cm, rb);
2186
2187
32.0k
  pbi->context_update_tile_id = 0;
2188
32.0k
  if (cm->tiles.rows * cm->tiles.cols > 1) {
2189
    // tile to use for cdf update
2190
1.52k
    pbi->context_update_tile_id =
2191
1.52k
        aom_rb_read_literal(rb, cm->tiles.log2_rows + cm->tiles.log2_cols);
2192
1.52k
    if (pbi->context_update_tile_id >= cm->tiles.rows * cm->tiles.cols) {
2193
19
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2194
19
                         "Invalid context_update_tile_id");
2195
19
    }
2196
    // tile size magnitude
2197
1.52k
    pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2198
1.52k
  }
2199
32.0k
}
2200
2201
#if EXT_TILE_DEBUG
2202
static inline void read_ext_tile_info(AV1Decoder *const pbi,
2203
0
                                      struct aom_read_bit_buffer *const rb) {
2204
0
  AV1_COMMON *const cm = &pbi->common;
2205
2206
  // This information is stored as a separate byte.
2207
0
  int mod = rb->bit_offset % CHAR_BIT;
2208
0
  if (mod > 0) aom_rb_read_literal(rb, CHAR_BIT - mod);
2209
0
  assert(rb->bit_offset % CHAR_BIT == 0);
2210
2211
0
  if (cm->tiles.cols * cm->tiles.rows > 1) {
2212
    // Read the number of bytes used to store tile size
2213
0
    pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2214
0
    pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2215
0
  }
2216
0
}
2217
#endif  // EXT_TILE_DEBUG
2218
2219
4.98k
static size_t mem_get_varsize(const uint8_t *src, int sz) {
2220
4.98k
  switch (sz) {
2221
2.96k
    case 1: return src[0];
2222
1.49k
    case 2: return mem_get_le16(src);
2223
257
    case 3: return mem_get_le24(src);
2224
267
    case 4: return mem_get_le32(src);
2225
0
    default: assert(0 && "Invalid size"); return -1;
2226
4.98k
  }
2227
4.98k
}
2228
2229
#if EXT_TILE_DEBUG
2230
// Reads the next tile returning its size and adjusting '*data' accordingly
2231
// based on 'is_last'. On return, '*data' is updated to point to the end of the
2232
// raw tile buffer in the bit stream.
2233
static inline void get_ls_tile_buffer(
2234
    const uint8_t *const data_end, struct aom_internal_error_info *error_info,
2235
    const uint8_t **data, TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2236
0
    int tile_size_bytes, int col, int row, int tile_copy_mode) {
2237
0
  size_t size;
2238
2239
0
  size_t copy_size = 0;
2240
0
  const uint8_t *copy_data = NULL;
2241
2242
0
  if (!read_is_valid(*data, tile_size_bytes, data_end))
2243
0
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2244
0
                       "Truncated packet or corrupt tile length");
2245
0
  size = mem_get_varsize(*data, tile_size_bytes);
2246
2247
  // If tile_copy_mode = 1, then the top bit of the tile header indicates copy
2248
  // mode.
2249
0
  if (tile_copy_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
2250
    // The remaining bits in the top byte signal the row offset
2251
0
    int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
2252
0
    if (offset > row) {
2253
0
      aom_internal_error(
2254
0
          error_info, AOM_CODEC_CORRUPT_FRAME,
2255
0
          "Invalid row offset in tile copy mode: row=%d offset=%d", row,
2256
0
          offset);
2257
0
    }
2258
2259
    // Currently, only use tiles in same column as reference tiles.
2260
0
    copy_data = tile_buffers[row - offset][col].data;
2261
0
    copy_size = tile_buffers[row - offset][col].size;
2262
0
    size = 0;
2263
0
  } else {
2264
0
    size += AV1_MIN_TILE_SIZE_BYTES;
2265
0
  }
2266
2267
0
  *data += tile_size_bytes;
2268
2269
0
  if (size > (size_t)(data_end - *data))
2270
0
    aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2271
0
                       "Truncated packet or corrupt tile size");
2272
2273
0
  if (size > 0) {
2274
0
    tile_buffers[row][col].data = *data;
2275
0
    tile_buffers[row][col].size = size;
2276
0
  } else {
2277
0
    tile_buffers[row][col].data = copy_data;
2278
0
    tile_buffers[row][col].size = copy_size;
2279
0
  }
2280
2281
0
  *data += size;
2282
0
}
2283
2284
// Returns the end of the last tile buffer
2285
// (tile_buffers[cm->tiles.rows - 1][cm->tiles.cols - 1]).
2286
static const uint8_t *get_ls_tile_buffers(
2287
    AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
2288
0
    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
2289
0
  AV1_COMMON *const cm = &pbi->common;
2290
0
  const int tile_cols = cm->tiles.cols;
2291
0
  const int tile_rows = cm->tiles.rows;
2292
0
  const int have_tiles = tile_cols * tile_rows > 1;
2293
0
  const uint8_t *raw_data_end;  // The end of the last tile buffer
2294
2295
0
  if (!have_tiles) {
2296
0
    const size_t tile_size = data_end - data;
2297
0
    tile_buffers[0][0].data = data;
2298
0
    tile_buffers[0][0].size = tile_size;
2299
0
    raw_data_end = NULL;
2300
0
  } else {
2301
    // We locate only the tile buffers that are required, which are the ones
2302
    // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2303
    // need the last (bottom right) tile buffer, as we need to know where the
2304
    // end of the compressed frame buffer is for proper superframe decoding.
2305
2306
0
    const uint8_t *tile_col_data_end[MAX_TILE_COLS] = { NULL };
2307
0
    const uint8_t *const data_start = data;
2308
2309
0
    const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
2310
0
    const int single_row = pbi->dec_tile_row >= 0;
2311
0
    const int tile_rows_start = single_row ? dec_tile_row : 0;
2312
0
    const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
2313
0
    const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
2314
0
    const int single_col = pbi->dec_tile_col >= 0;
2315
0
    const int tile_cols_start = single_col ? dec_tile_col : 0;
2316
0
    const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2317
2318
0
    const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2319
0
    const int tile_size_bytes = pbi->tile_size_bytes;
2320
0
    int tile_width, tile_height;
2321
0
    if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
2322
0
      aom_internal_error(
2323
0
          &pbi->error, AOM_CODEC_CORRUPT_FRAME,
2324
0
          "Not all the tiles in the tile list have the same size.");
2325
0
    }
2326
0
    const int tile_copy_mode =
2327
0
        ((AOMMAX(tile_width, tile_height) << MI_SIZE_LOG2) <= 256) ? 1 : 0;
2328
    // Read tile column sizes for all columns (we need the last tile buffer)
2329
0
    for (int c = 0; c < tile_cols; ++c) {
2330
0
      const int is_last = c == tile_cols - 1;
2331
0
      size_t tile_col_size;
2332
2333
0
      if (!is_last) {
2334
0
        if (tile_col_size_bytes > data_end - data) {
2335
0
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2336
0
                             "Not enough data to read tile_col_size");
2337
0
        }
2338
0
        tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
2339
0
        data += tile_col_size_bytes;
2340
0
        if (tile_col_size > (size_t)(data_end - data)) {
2341
0
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2342
0
                             "tile_col_data_end[%d] is out of bound", c);
2343
0
        }
2344
0
        tile_col_data_end[c] = data + tile_col_size;
2345
0
      } else {
2346
0
        tile_col_size = data_end - data;
2347
0
        tile_col_data_end[c] = data_end;
2348
0
      }
2349
0
      data += tile_col_size;
2350
0
    }
2351
2352
0
    data = data_start;
2353
2354
    // Read the required tile sizes.
2355
0
    for (int c = tile_cols_start; c < tile_cols_end; ++c) {
2356
0
      const int is_last = c == tile_cols - 1;
2357
2358
0
      if (c > 0) data = tile_col_data_end[c - 1];
2359
2360
0
      if (!is_last) data += tile_col_size_bytes;
2361
2362
      // Get the whole of the last column, otherwise stop at the required tile.
2363
0
      for (int r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
2364
0
        get_ls_tile_buffer(tile_col_data_end[c], &pbi->error, &data,
2365
0
                           tile_buffers, tile_size_bytes, c, r, tile_copy_mode);
2366
0
      }
2367
0
    }
2368
2369
    // If we have not read the last column, then read it to get the last tile.
2370
0
    if (tile_cols_end != tile_cols) {
2371
0
      const int c = tile_cols - 1;
2372
2373
0
      data = tile_col_data_end[c - 1];
2374
2375
0
      for (int r = 0; r < tile_rows; ++r) {
2376
0
        get_ls_tile_buffer(tile_col_data_end[c], &pbi->error, &data,
2377
0
                           tile_buffers, tile_size_bytes, c, r, tile_copy_mode);
2378
0
      }
2379
0
    }
2380
0
    raw_data_end = data;
2381
0
  }
2382
0
  return raw_data_end;
2383
0
}
2384
#endif  // EXT_TILE_DEBUG
2385
2386
static const uint8_t *get_ls_single_tile_buffer(
2387
    AV1Decoder *pbi, const uint8_t *data,
2388
0
    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
2389
0
  assert(pbi->dec_tile_row >= 0 && pbi->dec_tile_col >= 0);
2390
0
  tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].data = data;
2391
0
  tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].size =
2392
0
      (size_t)pbi->coded_tile_data_size;
2393
0
  return data + pbi->coded_tile_data_size;
2394
0
}
2395
2396
// Reads the next tile returning its size and adjusting '*data' accordingly
2397
// based on 'is_last'.
2398
static inline void get_tile_buffer(const uint8_t *const data_end,
2399
                                   const int tile_size_bytes, int is_last,
2400
                                   struct aom_internal_error_info *error_info,
2401
                                   const uint8_t **data,
2402
36.4k
                                   TileBufferDec *const buf) {
2403
36.4k
  size_t size;
2404
2405
36.4k
  if (!is_last) {
2406
4.98k
    if (!read_is_valid(*data, tile_size_bytes, data_end))
2407
3
      aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2408
3
                         "Not enough data to read tile size");
2409
2410
4.98k
    size = mem_get_varsize(*data, tile_size_bytes) + AV1_MIN_TILE_SIZE_BYTES;
2411
4.98k
    *data += tile_size_bytes;
2412
2413
4.98k
    if (size > (size_t)(data_end - *data))
2414
83
      aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2415
83
                         "Truncated packet or corrupt tile size");
2416
31.4k
  } else {
2417
31.4k
    size = data_end - *data;
2418
31.4k
  }
2419
2420
36.4k
  buf->data = *data;
2421
36.4k
  buf->size = size;
2422
2423
36.4k
  *data += size;
2424
36.4k
}
2425
2426
static inline void get_tile_buffers(
2427
    AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
2428
    TileBufferDec (*const tile_buffers)[MAX_TILE_COLS], int start_tile,
2429
31.5k
    int end_tile) {
2430
31.5k
  AV1_COMMON *const cm = &pbi->common;
2431
31.5k
  const int tile_cols = cm->tiles.cols;
2432
31.5k
  const int tile_rows = cm->tiles.rows;
2433
31.5k
  int tc = 0;
2434
2435
65.4k
  for (int r = 0; r < tile_rows; ++r) {
2436
70.3k
    for (int c = 0; c < tile_cols; ++c, ++tc) {
2437
36.4k
      TileBufferDec *const buf = &tile_buffers[r][c];
2438
2439
36.4k
      const int is_last = (tc == end_tile);
2440
36.4k
      const size_t hdr_offset = 0;
2441
2442
36.4k
      if (tc < start_tile || tc > end_tile) continue;
2443
2444
36.4k
      if (data + hdr_offset >= data_end)
2445
8
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2446
8
                           "Data ended before all tiles were read.");
2447
36.4k
      data += hdr_offset;
2448
36.4k
      get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &pbi->error,
2449
36.4k
                      &data, buf);
2450
36.4k
    }
2451
33.8k
  }
2452
31.5k
}
2453
2454
static inline void set_cb_buffer(AV1Decoder *pbi, DecoderCodingBlock *dcb,
2455
                                 CB_BUFFER *cb_buffer_base,
2456
476k
                                 const int num_planes, int mi_row, int mi_col) {
2457
476k
  AV1_COMMON *const cm = &pbi->common;
2458
476k
  int mib_size_log2 = cm->seq_params->mib_size_log2;
2459
476k
  int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
2460
476k
  int offset = (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
2461
476k
  CB_BUFFER *cb_buffer = cb_buffer_base + offset;
2462
2463
1.79M
  for (int plane = 0; plane < num_planes; ++plane) {
2464
1.32M
    dcb->dqcoeff_block[plane] = cb_buffer->dqcoeff[plane];
2465
1.32M
    dcb->eob_data[plane] = cb_buffer->eob_data[plane];
2466
1.32M
    dcb->cb_offset[plane] = 0;
2467
1.32M
    dcb->txb_offset[plane] = 0;
2468
1.32M
  }
2469
476k
  MACROBLOCKD *const xd = &dcb->xd;
2470
476k
  xd->plane[0].color_index_map = cb_buffer->color_index_map[0];
2471
476k
  xd->plane[1].color_index_map = cb_buffer->color_index_map[1];
2472
476k
  xd->color_index_map_offset[0] = 0;
2473
476k
  xd->color_index_map_offset[1] = 0;
2474
476k
}
2475
2476
14.7k
static inline void decoder_alloc_tile_data(AV1Decoder *pbi, const int n_tiles) {
2477
14.7k
  AV1_COMMON *const cm = &pbi->common;
2478
14.7k
  aom_free(pbi->tile_data);
2479
14.7k
  pbi->allocated_tiles = 0;
2480
14.7k
  CHECK_MEM_ERROR(cm, pbi->tile_data,
2481
14.7k
                  aom_memalign(32, n_tiles * sizeof(*pbi->tile_data)));
2482
14.7k
  pbi->allocated_tiles = n_tiles;
2483
32.3k
  for (int i = 0; i < n_tiles; i++) {
2484
17.6k
    TileDataDec *const tile_data = pbi->tile_data + i;
2485
17.6k
    av1_zero(tile_data->dec_row_mt_sync);
2486
17.6k
  }
2487
14.7k
  pbi->allocated_row_mt_sync_rows = 0;
2488
14.7k
}
2489
2490
// Set up nsync by width.
2491
13.1k
static inline int get_sync_range(int width) {
2492
// nsync numbers are picked by testing.
2493
#if 0
2494
  if (width < 640)
2495
    return 1;
2496
  else if (width <= 1280)
2497
    return 2;
2498
  else if (width <= 4096)
2499
    return 4;
2500
  else
2501
    return 8;
2502
#else
2503
13.1k
  (void)width;
2504
13.1k
#endif
2505
13.1k
  return 1;
2506
13.1k
}
2507
2508
// Allocate memory for decoder row synchronization
2509
static inline void dec_row_mt_alloc(AV1DecRowMTSync *dec_row_mt_sync,
2510
13.1k
                                    AV1_COMMON *cm, int rows) {
2511
13.1k
  dec_row_mt_sync->allocated_sb_rows = rows;
2512
13.1k
#if CONFIG_MULTITHREAD
2513
13.1k
  {
2514
13.1k
    int i;
2515
2516
13.1k
    CHECK_MEM_ERROR(cm, dec_row_mt_sync->mutex_,
2517
13.1k
                    aom_malloc(sizeof(*(dec_row_mt_sync->mutex_)) * rows));
2518
13.1k
    if (dec_row_mt_sync->mutex_) {
2519
222k
      for (i = 0; i < rows; ++i) {
2520
209k
        pthread_mutex_init(&dec_row_mt_sync->mutex_[i], NULL);
2521
209k
      }
2522
13.1k
    }
2523
2524
13.1k
    CHECK_MEM_ERROR(cm, dec_row_mt_sync->cond_,
2525
13.1k
                    aom_malloc(sizeof(*(dec_row_mt_sync->cond_)) * rows));
2526
13.1k
    if (dec_row_mt_sync->cond_) {
2527
222k
      for (i = 0; i < rows; ++i) {
2528
209k
        pthread_cond_init(&dec_row_mt_sync->cond_[i], NULL);
2529
209k
      }
2530
13.1k
    }
2531
13.1k
  }
2532
13.1k
#endif  // CONFIG_MULTITHREAD
2533
2534
13.1k
  CHECK_MEM_ERROR(cm, dec_row_mt_sync->cur_sb_col,
2535
13.1k
                  aom_malloc(sizeof(*(dec_row_mt_sync->cur_sb_col)) * rows));
2536
2537
  // Set up nsync.
2538
13.1k
  dec_row_mt_sync->sync_range = get_sync_range(cm->width);
2539
13.1k
}
2540
2541
// Deallocate decoder row synchronization related mutex and data
2542
30.8k
void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync) {
2543
30.8k
  if (dec_row_mt_sync != NULL) {
2544
30.8k
#if CONFIG_MULTITHREAD
2545
30.8k
    int i;
2546
30.8k
    if (dec_row_mt_sync->mutex_ != NULL) {
2547
222k
      for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
2548
209k
        pthread_mutex_destroy(&dec_row_mt_sync->mutex_[i]);
2549
209k
      }
2550
13.1k
      aom_free(dec_row_mt_sync->mutex_);
2551
13.1k
    }
2552
30.8k
    if (dec_row_mt_sync->cond_ != NULL) {
2553
222k
      for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
2554
209k
        pthread_cond_destroy(&dec_row_mt_sync->cond_[i]);
2555
209k
      }
2556
13.1k
      aom_free(dec_row_mt_sync->cond_);
2557
13.1k
    }
2558
30.8k
#endif  // CONFIG_MULTITHREAD
2559
30.8k
    aom_free(dec_row_mt_sync->cur_sb_col);
2560
2561
    // clear the structure as the source of this call may be a resize in which
2562
    // case this call will be followed by an _alloc() which may fail.
2563
30.8k
    av1_zero(*dec_row_mt_sync);
2564
30.8k
  }
2565
30.8k
}
2566
2567
static inline void sync_read(AV1DecRowMTSync *const dec_row_mt_sync, int r,
2568
164k
                             int c) {
2569
164k
#if CONFIG_MULTITHREAD
2570
164k
  const int nsync = dec_row_mt_sync->sync_range;
2571
2572
164k
  if (r && !(c & (nsync - 1))) {
2573
123k
    pthread_mutex_t *const mutex = &dec_row_mt_sync->mutex_[r - 1];
2574
123k
    pthread_mutex_lock(mutex);
2575
2576
140k
    while (c > dec_row_mt_sync->cur_sb_col[r - 1] - nsync -
2577
140k
                   dec_row_mt_sync->intrabc_extra_top_right_sb_delay) {
2578
17.0k
      pthread_cond_wait(&dec_row_mt_sync->cond_[r - 1], mutex);
2579
17.0k
    }
2580
123k
    pthread_mutex_unlock(mutex);
2581
123k
  }
2582
#else
2583
  (void)dec_row_mt_sync;
2584
  (void)r;
2585
  (void)c;
2586
#endif  // CONFIG_MULTITHREAD
2587
164k
}
2588
2589
static inline void sync_write(AV1DecRowMTSync *const dec_row_mt_sync, int r,
2590
164k
                              int c, const int sb_cols) {
2591
164k
#if CONFIG_MULTITHREAD
2592
164k
  const int nsync = dec_row_mt_sync->sync_range;
2593
164k
  int cur;
2594
164k
  int sig = 1;
2595
2596
164k
  if (c < sb_cols - 1) {
2597
101k
    cur = c;
2598
101k
    if (c % nsync) sig = 0;
2599
101k
  } else {
2600
62.6k
    cur = sb_cols + nsync + dec_row_mt_sync->intrabc_extra_top_right_sb_delay;
2601
62.6k
  }
2602
2603
164k
  if (sig) {
2604
164k
    pthread_mutex_lock(&dec_row_mt_sync->mutex_[r]);
2605
2606
164k
    dec_row_mt_sync->cur_sb_col[r] = cur;
2607
2608
164k
    pthread_cond_signal(&dec_row_mt_sync->cond_[r]);
2609
164k
    pthread_mutex_unlock(&dec_row_mt_sync->mutex_[r]);
2610
164k
  }
2611
#else
2612
  (void)dec_row_mt_sync;
2613
  (void)r;
2614
  (void)c;
2615
  (void)sb_cols;
2616
#endif  // CONFIG_MULTITHREAD
2617
164k
}
2618
2619
static inline void signal_decoding_done_for_erroneous_row(
2620
433
    AV1Decoder *const pbi, const MACROBLOCKD *const xd) {
2621
433
  AV1_COMMON *const cm = &pbi->common;
2622
433
  const TileInfo *const tile = &xd->tile;
2623
433
  const int sb_row_in_tile =
2624
433
      ((xd->mi_row - tile->mi_row_start) >> cm->seq_params->mib_size_log2);
2625
433
  const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile);
2626
433
  TileDataDec *const tile_data =
2627
433
      pbi->tile_data + tile->tile_row * cm->tiles.cols + tile->tile_col;
2628
433
  AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
2629
2630
433
  sync_write(dec_row_mt_sync, sb_row_in_tile, sb_cols_in_tile - 1,
2631
433
             sb_cols_in_tile);
2632
433
}
2633
2634
static inline void decode_tile_sb_row(AV1Decoder *pbi, ThreadData *const td,
2635
                                      const TileInfo *tile_info,
2636
62.2k
                                      const int mi_row) {
2637
62.2k
  AV1_COMMON *const cm = &pbi->common;
2638
62.2k
  const int num_planes = av1_num_planes(cm);
2639
62.2k
  TileDataDec *const tile_data = pbi->tile_data +
2640
62.2k
                                 tile_info->tile_row * cm->tiles.cols +
2641
62.2k
                                 tile_info->tile_col;
2642
62.2k
  const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
2643
62.2k
  const int sb_row_in_tile =
2644
62.2k
      (mi_row - tile_info->mi_row_start) >> cm->seq_params->mib_size_log2;
2645
62.2k
  int sb_col_in_tile = 0;
2646
62.2k
  int row_mt_exit = 0;
2647
2648
226k
  for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
2649
164k
       mi_col += cm->seq_params->mib_size, sb_col_in_tile++) {
2650
164k
    set_cb_buffer(pbi, &td->dcb, pbi->cb_buffer_base, num_planes, mi_row,
2651
164k
                  mi_col);
2652
2653
164k
    sync_read(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile);
2654
2655
164k
#if CONFIG_MULTITHREAD
2656
164k
    pthread_mutex_lock(pbi->row_mt_mutex_);
2657
164k
#endif
2658
164k
    row_mt_exit = pbi->frame_row_mt_info.row_mt_exit;
2659
164k
#if CONFIG_MULTITHREAD
2660
164k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
2661
164k
#endif
2662
2663
164k
    if (!row_mt_exit) {
2664
      // Decoding of the super-block
2665
158k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
2666
158k
                       cm->seq_params->sb_size, 0x2);
2667
158k
    }
2668
2669
164k
    sync_write(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile,
2670
164k
               sb_cols_in_tile);
2671
164k
  }
2672
62.2k
}
2673
2674
23.4k
static int check_trailing_bits_after_symbol_coder(aom_reader *r) {
2675
23.4k
  if (aom_reader_has_overflowed(r)) return -1;
2676
2677
23.4k
  uint32_t nb_bits = aom_reader_tell(r);
2678
23.4k
  uint32_t nb_bytes = (nb_bits + 7) >> 3;
2679
23.4k
  const uint8_t *p = aom_reader_find_begin(r) + nb_bytes;
2680
2681
  // aom_reader_tell() returns 1 for a newly initialized decoder, and the
2682
  // return value only increases as values are decoded. So nb_bits > 0, and
2683
  // thus p > p_begin. Therefore accessing p[-1] is safe.
2684
23.4k
  uint8_t last_byte = p[-1];
2685
23.4k
  uint8_t pattern = 128 >> ((nb_bits - 1) & 7);
2686
23.4k
  if ((last_byte & (2 * pattern - 1)) != pattern) return -1;
2687
2688
  // Make sure that all padding bytes are zero as required by the spec.
2689
22.1k
  const uint8_t *p_end = aom_reader_find_end(r);
2690
181k
  while (p < p_end) {
2691
159k
    if (*p != 0) return -1;
2692
159k
    p++;
2693
159k
  }
2694
21.9k
  return 0;
2695
22.1k
}
2696
2697
static inline void set_decode_func_pointers(ThreadData *td,
2698
68.9k
                                            int parse_decode_flag) {
2699
68.9k
  td->read_coeffs_tx_intra_block_visit = decode_block_void;
2700
68.9k
  td->predict_and_recon_intra_block_visit = decode_block_void;
2701
68.9k
  td->read_coeffs_tx_inter_block_visit = decode_block_void;
2702
68.9k
  td->inverse_tx_inter_block_visit = decode_block_void;
2703
68.9k
  td->predict_inter_block_visit = predict_inter_block_void;
2704
68.9k
  td->cfl_store_inter_block_visit = cfl_store_inter_block_void;
2705
2706
68.9k
  if (parse_decode_flag & 0x1) {
2707
40.3k
    td->read_coeffs_tx_intra_block_visit = read_coeffs_tx_intra_block;
2708
40.3k
    td->read_coeffs_tx_inter_block_visit = av1_read_coeffs_txb;
2709
40.3k
  }
2710
68.9k
  if (parse_decode_flag & 0x2) {
2711
33.6k
    td->predict_and_recon_intra_block_visit =
2712
33.6k
        predict_and_reconstruct_intra_block;
2713
33.6k
    td->inverse_tx_inter_block_visit = inverse_transform_inter_block;
2714
33.6k
    td->predict_inter_block_visit = predict_inter_block;
2715
33.6k
    td->cfl_store_inter_block_visit = cfl_store_inter_block;
2716
33.6k
  }
2717
68.9k
}
2718
2719
static inline void decode_tile(AV1Decoder *pbi, ThreadData *const td,
2720
5.05k
                               int tile_row, int tile_col) {
2721
5.05k
  TileInfo tile_info;
2722
2723
5.05k
  AV1_COMMON *const cm = &pbi->common;
2724
5.05k
  const int num_planes = av1_num_planes(cm);
2725
2726
5.05k
  av1_tile_set_row(&tile_info, cm, tile_row);
2727
5.05k
  av1_tile_set_col(&tile_info, cm, tile_col);
2728
5.05k
  DecoderCodingBlock *const dcb = &td->dcb;
2729
5.05k
  MACROBLOCKD *const xd = &dcb->xd;
2730
2731
5.05k
  av1_zero_above_context(cm, xd, tile_info.mi_col_start, tile_info.mi_col_end,
2732
5.05k
                         tile_row);
2733
5.05k
  av1_reset_loop_filter_delta(xd, num_planes);
2734
5.05k
  av1_reset_loop_restoration(xd, num_planes);
2735
2736
52.0k
  for (int mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
2737
51.2k
       mi_row += cm->seq_params->mib_size) {
2738
51.2k
    av1_zero_left_context(xd);
2739
2740
135k
    for (int mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
2741
88.8k
         mi_col += cm->seq_params->mib_size) {
2742
88.8k
      set_cb_buffer(pbi, dcb, &td->cb_buffer_base, num_planes, 0, 0);
2743
2744
      // Bit-stream parsing and decoding of the superblock
2745
88.8k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
2746
88.8k
                       cm->seq_params->sb_size, 0x3);
2747
2748
88.8k
      if (aom_reader_has_overflowed(td->bit_reader)) {
2749
4.25k
        aom_merge_corrupted_flag(&dcb->corrupted, 1);
2750
4.25k
        return;
2751
4.25k
      }
2752
88.8k
    }
2753
51.2k
  }
2754
2755
803
  int corrupted =
2756
803
      (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
2757
803
  aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
2758
803
}
2759
2760
static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
2761
                                   const uint8_t *data_end, int start_tile,
2762
5.09k
                                   int end_tile) {
2763
5.09k
  AV1_COMMON *const cm = &pbi->common;
2764
5.09k
  ThreadData *const td = &pbi->td;
2765
5.09k
  CommonTileParams *const tiles = &cm->tiles;
2766
5.09k
  const int tile_cols = tiles->cols;
2767
5.09k
  const int tile_rows = tiles->rows;
2768
5.09k
  const int n_tiles = tile_cols * tile_rows;
2769
5.09k
  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
2770
5.09k
  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
2771
5.09k
  const int single_row = pbi->dec_tile_row >= 0;
2772
5.09k
  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
2773
5.09k
  const int single_col = pbi->dec_tile_col >= 0;
2774
5.09k
  int tile_rows_start;
2775
5.09k
  int tile_rows_end;
2776
5.09k
  int tile_cols_start;
2777
5.09k
  int tile_cols_end;
2778
5.09k
  int inv_col_order;
2779
5.09k
  int inv_row_order;
2780
5.09k
  int tile_row, tile_col;
2781
5.09k
  uint8_t allow_update_cdf;
2782
5.09k
  const uint8_t *raw_data_end = NULL;
2783
2784
5.09k
  if (tiles->large_scale) {
2785
0
    tile_rows_start = single_row ? dec_tile_row : 0;
2786
0
    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
2787
0
    tile_cols_start = single_col ? dec_tile_col : 0;
2788
0
    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2789
0
    inv_col_order = pbi->inv_tile_order && !single_col;
2790
0
    inv_row_order = pbi->inv_tile_order && !single_row;
2791
0
    allow_update_cdf = 0;
2792
5.09k
  } else {
2793
5.09k
    tile_rows_start = 0;
2794
5.09k
    tile_rows_end = tile_rows;
2795
5.09k
    tile_cols_start = 0;
2796
5.09k
    tile_cols_end = tile_cols;
2797
5.09k
    inv_col_order = pbi->inv_tile_order;
2798
5.09k
    inv_row_order = pbi->inv_tile_order;
2799
5.09k
    allow_update_cdf = 1;
2800
5.09k
  }
2801
2802
  // No tiles to decode.
2803
5.09k
  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
2804
      // First tile is larger than end_tile.
2805
5.09k
      tile_rows_start * tiles->cols + tile_cols_start > end_tile ||
2806
      // Last tile is smaller than start_tile.
2807
5.09k
      (tile_rows_end - 1) * tiles->cols + tile_cols_end - 1 < start_tile)
2808
0
    return data;
2809
2810
5.09k
  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
2811
2812
5.09k
  assert(tile_rows <= MAX_TILE_ROWS);
2813
5.09k
  assert(tile_cols <= MAX_TILE_COLS);
2814
2815
5.09k
#if EXT_TILE_DEBUG
2816
5.09k
  if (tiles->large_scale && !pbi->ext_tile_debug)
2817
0
    raw_data_end = get_ls_single_tile_buffer(pbi, data, tile_buffers);
2818
5.09k
  else if (tiles->large_scale && pbi->ext_tile_debug)
2819
0
    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
2820
5.09k
  else
2821
5.09k
#endif  // EXT_TILE_DEBUG
2822
5.09k
    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
2823
2824
5.09k
  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
2825
5.05k
    decoder_alloc_tile_data(pbi, n_tiles);
2826
5.05k
  }
2827
5.09k
  if (pbi->dcb.xd.seg_mask == NULL)
2828
5.09k
    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
2829
5.09k
                    (uint8_t *)aom_memalign(
2830
5.09k
                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
2831
#if CONFIG_ACCOUNTING
2832
  if (pbi->acct_enabled) {
2833
    aom_accounting_reset(&pbi->accounting);
2834
  }
2835
#endif
2836
2837
5.09k
  set_decode_func_pointers(&pbi->td, 0x3);
2838
2839
  // Load all tile information into thread_data.
2840
5.09k
  td->dcb = pbi->dcb;
2841
2842
5.09k
  td->dcb.corrupted = 0;
2843
5.09k
  td->dcb.mc_buf[0] = td->mc_buf[0];
2844
5.09k
  td->dcb.mc_buf[1] = td->mc_buf[1];
2845
5.09k
  td->dcb.xd.tmp_conv_dst = td->tmp_conv_dst;
2846
15.2k
  for (int j = 0; j < 2; ++j) {
2847
10.1k
    td->dcb.xd.tmp_obmc_bufs[j] = td->tmp_obmc_bufs[j];
2848
10.1k
  }
2849
2850
10.1k
  for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2851
5.05k
    const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
2852
2853
10.1k
    for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
2854
5.05k
      const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
2855
5.05k
      TileDataDec *const tile_data = pbi->tile_data + row * tiles->cols + col;
2856
5.05k
      const TileBufferDec *const tile_bs_buf = &tile_buffers[row][col];
2857
2858
5.05k
      if (row * tiles->cols + col < start_tile ||
2859
5.05k
          row * tiles->cols + col > end_tile)
2860
0
        continue;
2861
2862
5.05k
      td->bit_reader = &tile_data->bit_reader;
2863
5.05k
      av1_zero(td->cb_buffer_base.dqcoeff);
2864
5.05k
      av1_tile_init(&td->dcb.xd.tile, cm, row, col);
2865
5.05k
      td->dcb.xd.current_base_qindex = cm->quant_params.base_qindex;
2866
5.05k
      setup_bool_decoder(&td->dcb.xd, tile_bs_buf->data, data_end,
2867
5.05k
                         tile_bs_buf->size, &pbi->error, td->bit_reader,
2868
5.05k
                         allow_update_cdf);
2869
#if CONFIG_ACCOUNTING
2870
      if (pbi->acct_enabled) {
2871
        td->bit_reader->accounting = &pbi->accounting;
2872
        td->bit_reader->accounting->last_tell_frac =
2873
            aom_reader_tell_frac(td->bit_reader);
2874
      } else {
2875
        td->bit_reader->accounting = NULL;
2876
      }
2877
#endif
2878
5.05k
      av1_init_macroblockd(cm, &td->dcb.xd);
2879
5.05k
      av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), row,
2880
5.05k
                             &td->dcb.xd);
2881
2882
      // Initialise the tile context from the frame context
2883
5.05k
      tile_data->tctx = *cm->fc;
2884
5.05k
      td->dcb.xd.tile_ctx = &tile_data->tctx;
2885
2886
      // decode tile
2887
5.05k
      decode_tile(pbi, td, row, col);
2888
5.05k
      aom_merge_corrupted_flag(&pbi->dcb.corrupted, td->dcb.corrupted);
2889
5.05k
      if (pbi->dcb.corrupted)
2890
4.44k
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2891
4.44k
                           "Failed to decode tile data");
2892
5.05k
    }
2893
5.05k
  }
2894
2895
5.09k
  if (tiles->large_scale) {
2896
0
    if (n_tiles == 1) {
2897
      // Find the end of the single tile buffer
2898
0
      return aom_reader_find_end(&pbi->tile_data->bit_reader);
2899
0
    }
2900
    // Return the end of the last tile buffer
2901
0
    return raw_data_end;
2902
0
  }
2903
5.09k
  TileDataDec *const tile_data = pbi->tile_data + end_tile;
2904
2905
5.09k
  return aom_reader_find_end(&tile_data->bit_reader);
2906
5.09k
}
2907
2908
56.9k
static TileJobsDec *get_dec_job_info(AV1DecTileMT *tile_mt_info) {
2909
56.9k
  TileJobsDec *cur_job_info = NULL;
2910
56.9k
#if CONFIG_MULTITHREAD
2911
56.9k
  pthread_mutex_lock(tile_mt_info->job_mutex);
2912
2913
56.9k
  if (tile_mt_info->jobs_dequeued < tile_mt_info->jobs_enqueued) {
2914
28.3k
    cur_job_info = tile_mt_info->job_queue + tile_mt_info->jobs_dequeued;
2915
28.3k
    tile_mt_info->jobs_dequeued++;
2916
28.3k
  }
2917
2918
56.9k
  pthread_mutex_unlock(tile_mt_info->job_mutex);
2919
#else
2920
  (void)tile_mt_info;
2921
#endif
2922
56.9k
  return cur_job_info;
2923
56.9k
}
2924
2925
static inline void tile_worker_hook_init(AV1Decoder *const pbi,
2926
                                         DecWorkerData *const thread_data,
2927
                                         const TileBufferDec *const tile_buffer,
2928
                                         TileDataDec *const tile_data,
2929
28.3k
                                         uint8_t allow_update_cdf) {
2930
28.3k
  AV1_COMMON *cm = &pbi->common;
2931
28.3k
  ThreadData *const td = thread_data->td;
2932
28.3k
  int tile_row = tile_data->tile_info.tile_row;
2933
28.3k
  int tile_col = tile_data->tile_info.tile_col;
2934
2935
28.3k
  td->bit_reader = &tile_data->bit_reader;
2936
28.3k
  av1_zero(td->cb_buffer_base.dqcoeff);
2937
2938
28.3k
  MACROBLOCKD *const xd = &td->dcb.xd;
2939
28.3k
  av1_tile_init(&xd->tile, cm, tile_row, tile_col);
2940
28.3k
  xd->current_base_qindex = cm->quant_params.base_qindex;
2941
2942
28.3k
  setup_bool_decoder(xd, tile_buffer->data, thread_data->data_end,
2943
28.3k
                     tile_buffer->size, &thread_data->error_info,
2944
28.3k
                     td->bit_reader, allow_update_cdf);
2945
#if CONFIG_ACCOUNTING
2946
  if (pbi->acct_enabled) {
2947
    td->bit_reader->accounting = &pbi->accounting;
2948
    td->bit_reader->accounting->last_tell_frac =
2949
        aom_reader_tell_frac(td->bit_reader);
2950
  } else {
2951
    td->bit_reader->accounting = NULL;
2952
  }
2953
#endif
2954
28.3k
  av1_init_macroblockd(cm, xd);
2955
28.3k
  xd->error_info = &thread_data->error_info;
2956
28.3k
  av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row, xd);
2957
2958
  // Initialise the tile context from the frame context
2959
28.3k
  tile_data->tctx = *cm->fc;
2960
28.3k
  xd->tile_ctx = &tile_data->tctx;
2961
#if CONFIG_ACCOUNTING
2962
  if (pbi->acct_enabled) {
2963
    tile_data->bit_reader.accounting->last_tell_frac =
2964
        aom_reader_tell_frac(&tile_data->bit_reader);
2965
  }
2966
#endif
2967
28.3k
}
2968
2969
0
static int tile_worker_hook(void *arg1, void *arg2) {
2970
0
  DecWorkerData *const thread_data = (DecWorkerData *)arg1;
2971
0
  AV1Decoder *const pbi = (AV1Decoder *)arg2;
2972
0
  AV1_COMMON *cm = &pbi->common;
2973
0
  ThreadData *const td = thread_data->td;
2974
0
  uint8_t allow_update_cdf;
2975
2976
  // The jmp_buf is valid only for the duration of the function that calls
2977
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2978
  // before it returns.
2979
0
  if (setjmp(thread_data->error_info.jmp)) {
2980
0
    thread_data->error_info.setjmp = 0;
2981
0
    thread_data->td->dcb.corrupted = 1;
2982
0
    return 0;
2983
0
  }
2984
0
  thread_data->error_info.setjmp = 1;
2985
2986
0
  allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
2987
0
  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
2988
2989
0
  set_decode_func_pointers(td, 0x3);
2990
2991
0
  assert(cm->tiles.cols > 0);
2992
0
  while (!td->dcb.corrupted) {
2993
0
    TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
2994
2995
0
    if (cur_job_info != NULL) {
2996
0
      const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
2997
0
      TileDataDec *const tile_data = cur_job_info->tile_data;
2998
0
      tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
2999
0
                            allow_update_cdf);
3000
      // decode tile
3001
0
      int tile_row = tile_data->tile_info.tile_row;
3002
0
      int tile_col = tile_data->tile_info.tile_col;
3003
0
      decode_tile(pbi, td, tile_row, tile_col);
3004
0
    } else {
3005
0
      break;
3006
0
    }
3007
0
  }
3008
0
  thread_data->error_info.setjmp = 0;
3009
0
  return !td->dcb.corrupted;
3010
0
}
3011
3012
static inline int get_max_row_mt_workers_per_tile(AV1_COMMON *cm,
3013
93.1k
                                                  const TileInfo *tile) {
3014
  // NOTE: Currently value of max workers is calculated based
3015
  // on the parse and decode time. As per the theoretical estimate
3016
  // when percentage of parse time is equal to percentage of decode
3017
  // time, number of workers needed to parse + decode a tile can not
3018
  // exceed more than 2.
3019
  // TODO(any): Modify this value if parsing is optimized in future.
3020
93.1k
  int sb_rows = av1_get_sb_rows_in_tile(cm, tile);
3021
93.1k
  int max_workers =
3022
93.1k
      sb_rows == 1 ? AOM_MIN_THREADS_PER_TILE : AOM_MAX_THREADS_PER_TILE;
3023
93.1k
  return max_workers;
3024
93.1k
}
3025
3026
// The caller must hold pbi->row_mt_mutex_ when calling this function.
3027
// Returns 1 if either the next job is stored in *next_job_info or 1 is stored
3028
// in *end_of_frame.
3029
// NOTE: The caller waits on pbi->row_mt_cond_ if this function returns 0.
3030
// The return value of this function depends on the following variables:
3031
// - frame_row_mt_info->mi_rows_parse_done
3032
// - frame_row_mt_info->mi_rows_decode_started
3033
// - frame_row_mt_info->row_mt_exit
3034
// Therefore we may need to signal or broadcast pbi->row_mt_cond_ if any of
3035
// these variables is modified.
3036
static int get_next_job_info(AV1Decoder *const pbi,
3037
                             AV1DecRowMTJobInfo *next_job_info,
3038
99.5k
                             int *end_of_frame) {
3039
99.5k
  AV1_COMMON *cm = &pbi->common;
3040
99.5k
  TileDataDec *tile_data;
3041
99.5k
  AV1DecRowMTSync *dec_row_mt_sync;
3042
99.5k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3043
99.5k
  const int tile_rows_start = frame_row_mt_info->tile_rows_start;
3044
99.5k
  const int tile_rows_end = frame_row_mt_info->tile_rows_end;
3045
99.5k
  const int tile_cols_start = frame_row_mt_info->tile_cols_start;
3046
99.5k
  const int tile_cols_end = frame_row_mt_info->tile_cols_end;
3047
99.5k
  const int start_tile = frame_row_mt_info->start_tile;
3048
99.5k
  const int end_tile = frame_row_mt_info->end_tile;
3049
99.5k
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
3050
99.5k
  int num_mis_to_decode, num_threads_working;
3051
99.5k
  int num_mis_waiting_for_decode;
3052
99.5k
  int min_threads_working = INT_MAX;
3053
99.5k
  int max_mis_to_decode = 0;
3054
99.5k
  int tile_row_idx, tile_col_idx;
3055
99.5k
  int tile_row = -1;
3056
99.5k
  int tile_col = -1;
3057
3058
99.5k
  memset(next_job_info, 0, sizeof(*next_job_info));
3059
3060
  // Frame decode is completed or error is encountered.
3061
99.5k
  *end_of_frame = (frame_row_mt_info->mi_rows_decode_started ==
3062
99.5k
                   frame_row_mt_info->mi_rows_to_decode) ||
3063
75.0k
                  (frame_row_mt_info->row_mt_exit == 1);
3064
99.5k
  if (*end_of_frame) {
3065
28.5k
    return 1;
3066
28.5k
  }
3067
3068
  // Decoding cannot start as bit-stream parsing is not complete.
3069
99.5k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3070
70.9k
         frame_row_mt_info->mi_rows_decode_started);
3071
70.9k
  if (frame_row_mt_info->mi_rows_parse_done ==
3072
70.9k
      frame_row_mt_info->mi_rows_decode_started)
3073
8.27k
    return 0;
3074
3075
  // Choose the tile to decode.
3076
130k
  for (tile_row_idx = tile_rows_start; tile_row_idx < tile_rows_end;
3077
67.6k
       ++tile_row_idx) {
3078
135k
    for (tile_col_idx = tile_cols_start; tile_col_idx < tile_cols_end;
3079
67.6k
         ++tile_col_idx) {
3080
67.6k
      if (tile_row_idx * cm->tiles.cols + tile_col_idx < start_tile ||
3081
67.6k
          tile_row_idx * cm->tiles.cols + tile_col_idx > end_tile)
3082
0
        continue;
3083
3084
67.6k
      tile_data = pbi->tile_data + tile_row_idx * cm->tiles.cols + tile_col_idx;
3085
67.6k
      dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3086
3087
67.6k
      num_threads_working = dec_row_mt_sync->num_threads_working;
3088
67.6k
      num_mis_waiting_for_decode = (dec_row_mt_sync->mi_rows_parse_done -
3089
67.6k
                                    dec_row_mt_sync->mi_rows_decode_started) *
3090
67.6k
                                   dec_row_mt_sync->mi_cols;
3091
67.6k
      num_mis_to_decode =
3092
67.6k
          (dec_row_mt_sync->mi_rows - dec_row_mt_sync->mi_rows_decode_started) *
3093
67.6k
          dec_row_mt_sync->mi_cols;
3094
3095
67.6k
      assert(num_mis_to_decode >= num_mis_waiting_for_decode);
3096
3097
      // Pick the tile which has minimum number of threads working on it.
3098
67.6k
      if (num_mis_waiting_for_decode > 0) {
3099
63.8k
        if (num_threads_working < min_threads_working) {
3100
62.6k
          min_threads_working = num_threads_working;
3101
62.6k
          max_mis_to_decode = 0;
3102
62.6k
        }
3103
63.8k
        if (num_threads_working == min_threads_working &&
3104
62.9k
            num_mis_to_decode > max_mis_to_decode &&
3105
62.8k
            num_threads_working <
3106
62.8k
                get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info)) {
3107
62.4k
          max_mis_to_decode = num_mis_to_decode;
3108
62.4k
          tile_row = tile_row_idx;
3109
62.4k
          tile_col = tile_col_idx;
3110
62.4k
        }
3111
63.8k
      }
3112
67.6k
    }
3113
67.6k
  }
3114
  // No job found to process
3115
62.6k
  if (tile_row == -1 || tile_col == -1) return 0;
3116
3117
62.2k
  tile_data = pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3118
62.2k
  dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3119
3120
62.2k
  next_job_info->tile_row = tile_row;
3121
62.2k
  next_job_info->tile_col = tile_col;
3122
62.2k
  next_job_info->mi_row = dec_row_mt_sync->mi_rows_decode_started +
3123
62.2k
                          tile_data->tile_info.mi_row_start;
3124
3125
62.2k
  dec_row_mt_sync->num_threads_working++;
3126
62.2k
  dec_row_mt_sync->mi_rows_decode_started += sb_mi_size;
3127
62.2k
  frame_row_mt_info->mi_rows_decode_started += sb_mi_size;
3128
62.2k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3129
62.2k
         frame_row_mt_info->mi_rows_decode_started);
3130
62.2k
#if CONFIG_MULTITHREAD
3131
62.2k
  if (frame_row_mt_info->mi_rows_decode_started ==
3132
62.2k
      frame_row_mt_info->mi_rows_to_decode) {
3133
20.4k
    pthread_cond_broadcast(pbi->row_mt_cond_);
3134
20.4k
  }
3135
62.2k
#endif
3136
3137
62.2k
  return 1;
3138
62.6k
}
3139
3140
static inline void signal_parse_sb_row_done(AV1Decoder *const pbi,
3141
                                            TileDataDec *const tile_data,
3142
84.3k
                                            const int sb_mi_size) {
3143
84.3k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3144
84.3k
#if CONFIG_MULTITHREAD
3145
84.3k
  pthread_mutex_lock(pbi->row_mt_mutex_);
3146
84.3k
#endif
3147
84.3k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3148
84.3k
         frame_row_mt_info->mi_rows_decode_started);
3149
84.3k
  tile_data->dec_row_mt_sync.mi_rows_parse_done += sb_mi_size;
3150
84.3k
  frame_row_mt_info->mi_rows_parse_done += sb_mi_size;
3151
84.3k
#if CONFIG_MULTITHREAD
3152
  // A new decode job is available. Wake up one worker thread to handle the
3153
  // new decode job.
3154
  // NOTE: This assumes we bump mi_rows_parse_done and mi_rows_decode_started
3155
  // by the same increment (sb_mi_size).
3156
84.3k
  pthread_cond_signal(pbi->row_mt_cond_);
3157
84.3k
  pthread_mutex_unlock(pbi->row_mt_mutex_);
3158
84.3k
#endif
3159
84.3k
}
3160
3161
// This function is very similar to decode_tile(). It would be good to figure
3162
// out how to share code.
3163
static inline void parse_tile_row_mt(AV1Decoder *pbi, ThreadData *const td,
3164
28.3k
                                     TileDataDec *const tile_data) {
3165
28.3k
  AV1_COMMON *const cm = &pbi->common;
3166
28.3k
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
3167
28.3k
  const int num_planes = av1_num_planes(cm);
3168
28.3k
  const TileInfo *const tile_info = &tile_data->tile_info;
3169
28.3k
  int tile_row = tile_info->tile_row;
3170
28.3k
  DecoderCodingBlock *const dcb = &td->dcb;
3171
28.3k
  MACROBLOCKD *const xd = &dcb->xd;
3172
3173
28.3k
  av1_zero_above_context(cm, xd, tile_info->mi_col_start, tile_info->mi_col_end,
3174
28.3k
                         tile_row);
3175
28.3k
  av1_reset_loop_filter_delta(xd, num_planes);
3176
28.3k
  av1_reset_loop_restoration(xd, num_planes);
3177
3178
113k
  for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
3179
89.7k
       mi_row += cm->seq_params->mib_size) {
3180
89.7k
    av1_zero_left_context(xd);
3181
3182
308k
    for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
3183
223k
         mi_col += cm->seq_params->mib_size) {
3184
223k
      set_cb_buffer(pbi, dcb, pbi->cb_buffer_base, num_planes, mi_row, mi_col);
3185
3186
      // Bit-stream parsing of the superblock
3187
223k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
3188
223k
                       cm->seq_params->sb_size, 0x1);
3189
3190
223k
      if (aom_reader_has_overflowed(td->bit_reader)) {
3191
4.92k
        aom_merge_corrupted_flag(&dcb->corrupted, 1);
3192
4.92k
        return;
3193
4.92k
      }
3194
223k
    }
3195
84.7k
    signal_parse_sb_row_done(pbi, tile_data, sb_mi_size);
3196
84.7k
  }
3197
3198
23.4k
  int corrupted =
3199
23.4k
      (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
3200
23.4k
  aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
3201
23.4k
}
3202
3203
35.2k
static int row_mt_worker_hook(void *arg1, void *arg2) {
3204
35.2k
  DecWorkerData *const thread_data = (DecWorkerData *)arg1;
3205
35.2k
  AV1Decoder *const pbi = (AV1Decoder *)arg2;
3206
35.2k
  ThreadData *const td = thread_data->td;
3207
35.2k
  uint8_t allow_update_cdf;
3208
35.2k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3209
35.2k
  td->dcb.corrupted = 0;
3210
3211
  // The jmp_buf is valid only for the duration of the function that calls
3212
  // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3213
  // before it returns.
3214
35.2k
  if (setjmp(thread_data->error_info.jmp)) {
3215
433
    thread_data->error_info.setjmp = 0;
3216
433
    thread_data->td->dcb.corrupted = 1;
3217
433
#if CONFIG_MULTITHREAD
3218
433
    pthread_mutex_lock(pbi->row_mt_mutex_);
3219
433
#endif
3220
433
    frame_row_mt_info->row_mt_exit = 1;
3221
433
#if CONFIG_MULTITHREAD
3222
433
    pthread_cond_broadcast(pbi->row_mt_cond_);
3223
433
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3224
433
#endif
3225
    // If any SB row (erroneous row) processed by a thread encounters an
3226
    // internal error, there is a need to indicate other threads that decoding
3227
    // of the erroneous row is complete. This ensures that other threads which
3228
    // wait upon the completion of SB's present in erroneous row are not waiting
3229
    // indefinitely.
3230
433
    signal_decoding_done_for_erroneous_row(pbi, &thread_data->td->dcb.xd);
3231
433
    return 0;
3232
433
  }
3233
34.8k
  thread_data->error_info.setjmp = 1;
3234
3235
34.8k
  AV1_COMMON *cm = &pbi->common;
3236
34.8k
  allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
3237
35.2k
  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
3238
3239
34.8k
  set_decode_func_pointers(td, 0x1);
3240
3241
34.8k
  assert(cm->tiles.cols > 0);
3242
63.2k
  while (!td->dcb.corrupted) {
3243
56.8k
    TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
3244
3245
56.8k
    if (cur_job_info != NULL) {
3246
28.3k
      const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
3247
28.3k
      TileDataDec *const tile_data = cur_job_info->tile_data;
3248
28.3k
      tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
3249
28.3k
                            allow_update_cdf);
3250
28.3k
#if CONFIG_MULTITHREAD
3251
28.3k
      pthread_mutex_lock(pbi->row_mt_mutex_);
3252
28.3k
#endif
3253
28.3k
      tile_data->dec_row_mt_sync.num_threads_working++;
3254
28.3k
#if CONFIG_MULTITHREAD
3255
28.3k
      pthread_mutex_unlock(pbi->row_mt_mutex_);
3256
28.3k
#endif
3257
      // decode tile
3258
28.3k
      parse_tile_row_mt(pbi, td, tile_data);
3259
28.3k
#if CONFIG_MULTITHREAD
3260
28.3k
      pthread_mutex_lock(pbi->row_mt_mutex_);
3261
28.3k
#endif
3262
28.3k
      tile_data->dec_row_mt_sync.num_threads_working--;
3263
28.3k
#if CONFIG_MULTITHREAD
3264
28.3k
      pthread_mutex_unlock(pbi->row_mt_mutex_);
3265
28.3k
#endif
3266
28.5k
    } else {
3267
28.5k
      break;
3268
28.5k
    }
3269
56.8k
  }
3270
3271
34.8k
  if (td->dcb.corrupted) {
3272
6.27k
    thread_data->error_info.setjmp = 0;
3273
6.27k
#if CONFIG_MULTITHREAD
3274
6.27k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3275
6.27k
#endif
3276
6.27k
    frame_row_mt_info->row_mt_exit = 1;
3277
6.27k
#if CONFIG_MULTITHREAD
3278
6.27k
    pthread_cond_broadcast(pbi->row_mt_cond_);
3279
6.27k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3280
6.27k
#endif
3281
6.27k
    return 0;
3282
6.27k
  }
3283
3284
28.5k
  set_decode_func_pointers(td, 0x2);
3285
3286
90.8k
  while (1) {
3287
90.8k
    AV1DecRowMTJobInfo next_job_info;
3288
90.8k
    int end_of_frame = 0;
3289
3290
90.8k
#if CONFIG_MULTITHREAD
3291
90.8k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3292
90.8k
#endif
3293
99.5k
    while (!get_next_job_info(pbi, &next_job_info, &end_of_frame)) {
3294
8.71k
#if CONFIG_MULTITHREAD
3295
8.71k
      pthread_cond_wait(pbi->row_mt_cond_, pbi->row_mt_mutex_);
3296
8.71k
#endif
3297
8.71k
    }
3298
90.8k
#if CONFIG_MULTITHREAD
3299
90.8k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3300
90.8k
#endif
3301
3302
90.8k
    if (end_of_frame) break;
3303
3304
62.2k
    int tile_row = next_job_info.tile_row;
3305
62.2k
    int tile_col = next_job_info.tile_col;
3306
62.2k
    int mi_row = next_job_info.mi_row;
3307
3308
62.2k
    TileDataDec *tile_data =
3309
62.2k
        pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3310
62.2k
    AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3311
3312
62.2k
    av1_tile_init(&td->dcb.xd.tile, cm, tile_row, tile_col);
3313
62.2k
    av1_init_macroblockd(cm, &td->dcb.xd);
3314
62.2k
    td->dcb.xd.error_info = &thread_data->error_info;
3315
3316
62.2k
    decode_tile_sb_row(pbi, td, &tile_data->tile_info, mi_row);
3317
3318
62.2k
#if CONFIG_MULTITHREAD
3319
62.2k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3320
62.2k
#endif
3321
62.2k
    dec_row_mt_sync->num_threads_working--;
3322
62.2k
#if CONFIG_MULTITHREAD
3323
62.2k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3324
62.2k
#endif
3325
62.2k
  }
3326
28.5k
  thread_data->error_info.setjmp = 0;
3327
28.5k
  return !td->dcb.corrupted;
3328
34.8k
}
3329
3330
// sorts in descending order
3331
10.7k
static int compare_tile_buffers(const void *a, const void *b) {
3332
10.7k
  const TileJobsDec *const buf1 = (const TileJobsDec *)a;
3333
10.7k
  const TileJobsDec *const buf2 = (const TileJobsDec *)b;
3334
10.7k
  return (((int)buf2->tile_buffer->size) - ((int)buf1->tile_buffer->size));
3335
10.7k
}
3336
3337
static inline void enqueue_tile_jobs(AV1Decoder *pbi, AV1_COMMON *cm,
3338
                                     int tile_rows_start, int tile_rows_end,
3339
                                     int tile_cols_start, int tile_cols_end,
3340
26.4k
                                     int start_tile, int end_tile) {
3341
26.4k
  AV1DecTileMT *tile_mt_info = &pbi->tile_mt_info;
3342
26.4k
  TileJobsDec *tile_job_queue = tile_mt_info->job_queue;
3343
26.4k
  tile_mt_info->jobs_enqueued = 0;
3344
26.4k
  tile_mt_info->jobs_dequeued = 0;
3345
3346
54.7k
  for (int row = tile_rows_start; row < tile_rows_end; row++) {
3347
58.5k
    for (int col = tile_cols_start; col < tile_cols_end; col++) {
3348
30.2k
      if (row * cm->tiles.cols + col < start_tile ||
3349
30.2k
          row * cm->tiles.cols + col > end_tile)
3350
3
        continue;
3351
30.2k
      tile_job_queue->tile_buffer = &pbi->tile_buffers[row][col];
3352
30.2k
      tile_job_queue->tile_data = pbi->tile_data + row * cm->tiles.cols + col;
3353
30.2k
      tile_job_queue++;
3354
30.2k
      tile_mt_info->jobs_enqueued++;
3355
30.2k
    }
3356
28.3k
  }
3357
26.4k
}
3358
3359
static inline void alloc_dec_jobs(AV1DecTileMT *tile_mt_info, AV1_COMMON *cm,
3360
9.64k
                                  int tile_rows, int tile_cols) {
3361
9.64k
  tile_mt_info->alloc_tile_rows = tile_rows;
3362
9.64k
  tile_mt_info->alloc_tile_cols = tile_cols;
3363
9.64k
  int num_tiles = tile_rows * tile_cols;
3364
9.64k
#if CONFIG_MULTITHREAD
3365
9.64k
  {
3366
9.64k
    CHECK_MEM_ERROR(cm, tile_mt_info->job_mutex,
3367
9.64k
                    aom_malloc(sizeof(*tile_mt_info->job_mutex) * num_tiles));
3368
3369
22.0k
    for (int i = 0; i < num_tiles; i++) {
3370
12.4k
      pthread_mutex_init(&tile_mt_info->job_mutex[i], NULL);
3371
12.4k
    }
3372
9.64k
  }
3373
9.64k
#endif
3374
9.64k
  CHECK_MEM_ERROR(cm, tile_mt_info->job_queue,
3375
9.64k
                  aom_malloc(sizeof(*tile_mt_info->job_queue) * num_tiles));
3376
9.64k
}
3377
3378
648k
void av1_free_mc_tmp_buf(ThreadData *thread_data) {
3379
648k
  int ref;
3380
1.94M
  for (ref = 0; ref < 2; ref++) {
3381
1.29M
    if (thread_data->mc_buf_use_highbd)
3382
360k
      aom_free(CONVERT_TO_SHORTPTR(thread_data->mc_buf[ref]));
3383
935k
    else
3384
935k
      aom_free(thread_data->mc_buf[ref]);
3385
1.29M
    thread_data->mc_buf[ref] = NULL;
3386
1.29M
  }
3387
648k
  thread_data->mc_buf_size = 0;
3388
648k
  thread_data->mc_buf_use_highbd = 0;
3389
3390
648k
  aom_free(thread_data->tmp_conv_dst);
3391
648k
  thread_data->tmp_conv_dst = NULL;
3392
648k
  aom_free(thread_data->seg_mask);
3393
648k
  thread_data->seg_mask = NULL;
3394
1.94M
  for (int i = 0; i < 2; ++i) {
3395
1.29M
    aom_free(thread_data->tmp_obmc_bufs[i]);
3396
1.29M
    thread_data->tmp_obmc_bufs[i] = NULL;
3397
1.29M
  }
3398
648k
}
3399
3400
static inline void allocate_mc_tmp_buf(AV1_COMMON *const cm,
3401
                                       ThreadData *thread_data, int buf_size,
3402
337k
                                       int use_highbd) {
3403
1.01M
  for (int ref = 0; ref < 2; ref++) {
3404
    // The mc_buf/hbd_mc_buf must be zeroed to fix a intermittent valgrind error
3405
    // 'Conditional jump or move depends on uninitialised value' from the loop
3406
    // filter. Uninitialized reads in convolve function (e.g. horiz_4tap path in
3407
    // av1_convolve_2d_sr_avx2()) from mc_buf/hbd_mc_buf are seen to be the
3408
    // potential reason for this issue.
3409
674k
    if (use_highbd) {
3410
360k
      uint16_t *hbd_mc_buf;
3411
360k
      CHECK_MEM_ERROR(cm, hbd_mc_buf, (uint16_t *)aom_memalign(16, buf_size));
3412
360k
      memset(hbd_mc_buf, 0, buf_size);
3413
360k
      thread_data->mc_buf[ref] = CONVERT_TO_BYTEPTR(hbd_mc_buf);
3414
360k
    } else {
3415
313k
      CHECK_MEM_ERROR(cm, thread_data->mc_buf[ref],
3416
313k
                      (uint8_t *)aom_memalign(16, buf_size));
3417
313k
      memset(thread_data->mc_buf[ref], 0, buf_size);
3418
313k
    }
3419
674k
  }
3420
337k
  thread_data->mc_buf_size = buf_size;
3421
337k
  thread_data->mc_buf_use_highbd = use_highbd;
3422
3423
337k
  CHECK_MEM_ERROR(cm, thread_data->tmp_conv_dst,
3424
337k
                  aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
3425
337k
                                       sizeof(*thread_data->tmp_conv_dst)));
3426
337k
  CHECK_MEM_ERROR(cm, thread_data->seg_mask,
3427
337k
                  (uint8_t *)aom_memalign(
3428
337k
                      16, 2 * MAX_SB_SQUARE * sizeof(*thread_data->seg_mask)));
3429
3430
1.01M
  for (int i = 0; i < 2; ++i) {
3431
674k
    CHECK_MEM_ERROR(
3432
674k
        cm, thread_data->tmp_obmc_bufs[i],
3433
674k
        aom_memalign(16, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
3434
674k
                             sizeof(*thread_data->tmp_obmc_bufs[i])));
3435
674k
  }
3436
337k
}
3437
3438
static inline void reset_dec_workers(AV1Decoder *pbi, AVxWorkerHook worker_hook,
3439
26.4k
                                     int num_workers) {
3440
26.4k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3441
3442
  // Reset tile decoding hook
3443
61.7k
  for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
3444
35.3k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3445
35.3k
    DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3446
35.3k
    thread_data->td->dcb = pbi->dcb;
3447
35.3k
    thread_data->td->dcb.corrupted = 0;
3448
35.3k
    thread_data->td->dcb.mc_buf[0] = thread_data->td->mc_buf[0];
3449
35.3k
    thread_data->td->dcb.mc_buf[1] = thread_data->td->mc_buf[1];
3450
35.3k
    thread_data->td->dcb.xd.tmp_conv_dst = thread_data->td->tmp_conv_dst;
3451
35.3k
    if (worker_idx)
3452
8.87k
      thread_data->td->dcb.xd.seg_mask = thread_data->td->seg_mask;
3453
105k
    for (int j = 0; j < 2; ++j) {
3454
70.6k
      thread_data->td->dcb.xd.tmp_obmc_bufs[j] =
3455
70.6k
          thread_data->td->tmp_obmc_bufs[j];
3456
70.6k
    }
3457
35.3k
    winterface->sync(worker);
3458
3459
35.3k
    worker->hook = worker_hook;
3460
35.3k
    worker->data1 = thread_data;
3461
35.3k
    worker->data2 = pbi;
3462
35.3k
  }
3463
#if CONFIG_ACCOUNTING
3464
  if (pbi->acct_enabled) {
3465
    aom_accounting_reset(&pbi->accounting);
3466
  }
3467
#endif
3468
26.4k
}
3469
3470
static inline void launch_dec_workers(AV1Decoder *pbi, const uint8_t *data_end,
3471
26.4k
                                      int num_workers) {
3472
26.4k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3473
3474
61.7k
  for (int worker_idx = num_workers - 1; worker_idx >= 0; --worker_idx) {
3475
35.3k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3476
35.3k
    DecWorkerData *const thread_data = (DecWorkerData *)worker->data1;
3477
3478
35.3k
    thread_data->data_end = data_end;
3479
3480
35.3k
    worker->had_error = 0;
3481
35.3k
    if (worker_idx == 0) {
3482
26.4k
      winterface->execute(worker);
3483
26.4k
    } else {
3484
8.87k
      winterface->launch(worker);
3485
8.87k
    }
3486
35.3k
  }
3487
26.4k
}
3488
3489
26.4k
static inline void sync_dec_workers(AV1Decoder *pbi, int num_workers) {
3490
26.4k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3491
26.4k
  int corrupted = 0;
3492
3493
61.7k
  for (int worker_idx = num_workers; worker_idx > 0; --worker_idx) {
3494
35.3k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx - 1];
3495
35.3k
    aom_merge_corrupted_flag(&corrupted, !winterface->sync(worker));
3496
35.3k
  }
3497
3498
26.4k
  pbi->dcb.corrupted = corrupted;
3499
26.4k
}
3500
3501
26.4k
static inline void decode_mt_init(AV1Decoder *pbi) {
3502
26.4k
  AV1_COMMON *const cm = &pbi->common;
3503
26.4k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3504
26.4k
  int worker_idx;
3505
3506
  // Create workers and thread_data
3507
26.4k
  if (pbi->num_workers == 0) {
3508
9.52k
    const int num_threads = pbi->max_threads;
3509
9.52k
    CHECK_MEM_ERROR(cm, pbi->tile_workers,
3510
9.52k
                    aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
3511
9.52k
    CHECK_MEM_ERROR(cm, pbi->thread_data,
3512
9.52k
                    aom_calloc(num_threads, sizeof(*pbi->thread_data)));
3513
3514
314k
    for (worker_idx = 0; worker_idx < num_threads; ++worker_idx) {
3515
304k
      AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3516
304k
      DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3517
3518
304k
      winterface->init(worker);
3519
304k
      worker->thread_name = "aom tile worker";
3520
304k
      if (worker_idx != 0 && !winterface->reset(worker)) {
3521
0
        aom_internal_error(&pbi->error, AOM_CODEC_ERROR,
3522
0
                           "Tile decoder thread creation failed");
3523
0
      }
3524
304k
      ++pbi->num_workers;
3525
3526
304k
      if (worker_idx != 0) {
3527
        // Allocate thread data.
3528
295k
        CHECK_MEM_ERROR(cm, thread_data->td,
3529
295k
                        aom_memalign(32, sizeof(*thread_data->td)));
3530
295k
        av1_zero(*thread_data->td);
3531
295k
      } else {
3532
        // Main thread acts as a worker and uses the thread data in pbi
3533
9.52k
        thread_data->td = &pbi->td;
3534
9.52k
      }
3535
304k
      thread_data->error_info.error_code = AOM_CODEC_OK;
3536
304k
      thread_data->error_info.setjmp = 0;
3537
304k
    }
3538
9.52k
  }
3539
26.4k
  const int use_highbd = cm->seq_params->use_highbitdepth;
3540
26.4k
  const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
3541
847k
  for (worker_idx = 1; worker_idx < pbi->max_threads; ++worker_idx) {
3542
821k
    DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3543
821k
    if (thread_data->td->mc_buf_size != buf_size) {
3544
321k
      av1_free_mc_tmp_buf(thread_data->td);
3545
321k
      allocate_mc_tmp_buf(cm, thread_data->td, buf_size, use_highbd);
3546
321k
    }
3547
821k
  }
3548
26.4k
}
3549
3550
static inline void tile_mt_queue(AV1Decoder *pbi, int tile_cols, int tile_rows,
3551
                                 int tile_rows_start, int tile_rows_end,
3552
                                 int tile_cols_start, int tile_cols_end,
3553
26.4k
                                 int start_tile, int end_tile) {
3554
26.4k
  AV1_COMMON *const cm = &pbi->common;
3555
26.4k
  if (pbi->tile_mt_info.alloc_tile_cols != tile_cols ||
3556
16.9k
      pbi->tile_mt_info.alloc_tile_rows != tile_rows) {
3557
9.64k
    av1_dealloc_dec_jobs(&pbi->tile_mt_info);
3558
9.64k
    alloc_dec_jobs(&pbi->tile_mt_info, cm, tile_rows, tile_cols);
3559
9.64k
  }
3560
26.4k
  enqueue_tile_jobs(pbi, cm, tile_rows_start, tile_rows_end, tile_cols_start,
3561
26.4k
                    tile_cols_end, start_tile, end_tile);
3562
26.4k
  qsort(pbi->tile_mt_info.job_queue, pbi->tile_mt_info.jobs_enqueued,
3563
26.4k
        sizeof(pbi->tile_mt_info.job_queue[0]), compare_tile_buffers);
3564
26.4k
}
3565
3566
static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
3567
                                      const uint8_t *data_end, int start_tile,
3568
0
                                      int end_tile) {
3569
0
  AV1_COMMON *const cm = &pbi->common;
3570
0
  CommonTileParams *const tiles = &cm->tiles;
3571
0
  const int tile_cols = tiles->cols;
3572
0
  const int tile_rows = tiles->rows;
3573
0
  const int n_tiles = tile_cols * tile_rows;
3574
0
  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
3575
0
  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
3576
0
  const int single_row = pbi->dec_tile_row >= 0;
3577
0
  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
3578
0
  const int single_col = pbi->dec_tile_col >= 0;
3579
0
  int tile_rows_start;
3580
0
  int tile_rows_end;
3581
0
  int tile_cols_start;
3582
0
  int tile_cols_end;
3583
0
  int tile_count_tg;
3584
0
  int num_workers;
3585
0
  const uint8_t *raw_data_end = NULL;
3586
3587
0
  if (tiles->large_scale) {
3588
0
    tile_rows_start = single_row ? dec_tile_row : 0;
3589
0
    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
3590
0
    tile_cols_start = single_col ? dec_tile_col : 0;
3591
0
    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3592
0
  } else {
3593
0
    tile_rows_start = 0;
3594
0
    tile_rows_end = tile_rows;
3595
0
    tile_cols_start = 0;
3596
0
    tile_cols_end = tile_cols;
3597
0
  }
3598
0
  tile_count_tg = end_tile - start_tile + 1;
3599
0
  num_workers = AOMMIN(pbi->max_threads, tile_count_tg);
3600
3601
  // No tiles to decode.
3602
0
  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
3603
      // First tile is larger than end_tile.
3604
0
      tile_rows_start * tile_cols + tile_cols_start > end_tile ||
3605
      // Last tile is smaller than start_tile.
3606
0
      (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
3607
0
    return data;
3608
3609
0
  assert(tile_rows <= MAX_TILE_ROWS);
3610
0
  assert(tile_cols <= MAX_TILE_COLS);
3611
0
  assert(tile_count_tg > 0);
3612
0
  assert(num_workers > 0);
3613
0
  assert(start_tile <= end_tile);
3614
0
  assert(start_tile >= 0 && end_tile < n_tiles);
3615
3616
0
  decode_mt_init(pbi);
3617
3618
  // get tile size in tile group
3619
0
#if EXT_TILE_DEBUG
3620
0
  if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
3621
0
  if (tiles->large_scale)
3622
0
    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
3623
0
  else
3624
0
#endif  // EXT_TILE_DEBUG
3625
0
    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
3626
3627
0
  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
3628
0
    decoder_alloc_tile_data(pbi, n_tiles);
3629
0
  }
3630
0
  if (pbi->dcb.xd.seg_mask == NULL)
3631
0
    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
3632
0
                    (uint8_t *)aom_memalign(
3633
0
                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
3634
3635
0
  for (int row = 0; row < tile_rows; row++) {
3636
0
    for (int col = 0; col < tile_cols; col++) {
3637
0
      TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
3638
0
      av1_tile_init(&tile_data->tile_info, cm, row, col);
3639
0
    }
3640
0
  }
3641
3642
0
  tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
3643
0
                tile_cols_start, tile_cols_end, start_tile, end_tile);
3644
3645
0
  reset_dec_workers(pbi, tile_worker_hook, num_workers);
3646
0
  launch_dec_workers(pbi, data_end, num_workers);
3647
0
  sync_dec_workers(pbi, num_workers);
3648
3649
0
  if (pbi->dcb.corrupted)
3650
0
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
3651
0
                       "Failed to decode tile data");
3652
3653
0
  if (tiles->large_scale) {
3654
0
    if (n_tiles == 1) {
3655
      // Find the end of the single tile buffer
3656
0
      return aom_reader_find_end(&pbi->tile_data->bit_reader);
3657
0
    }
3658
    // Return the end of the last tile buffer
3659
0
    return raw_data_end;
3660
0
  }
3661
0
  TileDataDec *const tile_data = pbi->tile_data + end_tile;
3662
3663
0
  return aom_reader_find_end(&tile_data->bit_reader);
3664
0
}
3665
3666
26.4k
static inline void dec_alloc_cb_buf(AV1Decoder *pbi) {
3667
26.4k
  AV1_COMMON *const cm = &pbi->common;
3668
26.4k
  int size = ((cm->mi_params.mi_rows >> cm->seq_params->mib_size_log2) + 1) *
3669
26.4k
             ((cm->mi_params.mi_cols >> cm->seq_params->mib_size_log2) + 1);
3670
3671
26.4k
  if (pbi->cb_buffer_alloc_size < size) {
3672
10.4k
    av1_dec_free_cb_buf(pbi);
3673
10.4k
    CHECK_MEM_ERROR(cm, pbi->cb_buffer_base,
3674
10.4k
                    aom_memalign(32, sizeof(*pbi->cb_buffer_base) * size));
3675
10.4k
    memset(pbi->cb_buffer_base, 0, sizeof(*pbi->cb_buffer_base) * size);
3676
10.4k
    pbi->cb_buffer_alloc_size = size;
3677
10.4k
  }
3678
26.4k
}
3679
3680
static inline void row_mt_frame_init(AV1Decoder *pbi, int tile_rows_start,
3681
                                     int tile_rows_end, int tile_cols_start,
3682
                                     int tile_cols_end, int start_tile,
3683
26.4k
                                     int end_tile, int max_sb_rows) {
3684
26.4k
  AV1_COMMON *const cm = &pbi->common;
3685
26.4k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3686
3687
26.4k
  frame_row_mt_info->tile_rows_start = tile_rows_start;
3688
26.4k
  frame_row_mt_info->tile_rows_end = tile_rows_end;
3689
26.4k
  frame_row_mt_info->tile_cols_start = tile_cols_start;
3690
26.4k
  frame_row_mt_info->tile_cols_end = tile_cols_end;
3691
26.4k
  frame_row_mt_info->start_tile = start_tile;
3692
26.4k
  frame_row_mt_info->end_tile = end_tile;
3693
26.4k
  frame_row_mt_info->mi_rows_to_decode = 0;
3694
26.4k
  frame_row_mt_info->mi_rows_parse_done = 0;
3695
26.4k
  frame_row_mt_info->mi_rows_decode_started = 0;
3696
26.4k
  frame_row_mt_info->row_mt_exit = 0;
3697
3698
54.7k
  for (int tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3699
58.5k
    for (int tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3700
30.2k
      if (tile_row * cm->tiles.cols + tile_col < start_tile ||
3701
30.2k
          tile_row * cm->tiles.cols + tile_col > end_tile)
3702
3
        continue;
3703
3704
30.2k
      TileDataDec *const tile_data =
3705
30.2k
          pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3706
30.2k
      const TileInfo *const tile_info = &tile_data->tile_info;
3707
3708
30.2k
      tile_data->dec_row_mt_sync.mi_rows_parse_done = 0;
3709
30.2k
      tile_data->dec_row_mt_sync.mi_rows_decode_started = 0;
3710
30.2k
      tile_data->dec_row_mt_sync.num_threads_working = 0;
3711
30.2k
      tile_data->dec_row_mt_sync.mi_rows =
3712
30.2k
          ALIGN_POWER_OF_TWO(tile_info->mi_row_end - tile_info->mi_row_start,
3713
30.2k
                             cm->seq_params->mib_size_log2);
3714
30.2k
      tile_data->dec_row_mt_sync.mi_cols =
3715
30.2k
          ALIGN_POWER_OF_TWO(tile_info->mi_col_end - tile_info->mi_col_start,
3716
30.2k
                             cm->seq_params->mib_size_log2);
3717
30.2k
      tile_data->dec_row_mt_sync.intrabc_extra_top_right_sb_delay =
3718
30.2k
          av1_get_intrabc_extra_top_right_sb_delay(cm);
3719
3720
30.2k
      frame_row_mt_info->mi_rows_to_decode +=
3721
30.2k
          tile_data->dec_row_mt_sync.mi_rows;
3722
3723
      // Initialize cur_sb_col to -1 for all SB rows.
3724
30.2k
      memset(tile_data->dec_row_mt_sync.cur_sb_col, -1,
3725
30.2k
             sizeof(*tile_data->dec_row_mt_sync.cur_sb_col) * max_sb_rows);
3726
30.2k
    }
3727
28.3k
  }
3728
3729
26.4k
#if CONFIG_MULTITHREAD
3730
26.4k
  if (pbi->row_mt_mutex_ == NULL) {
3731
9.48k
    CHECK_MEM_ERROR(cm, pbi->row_mt_mutex_,
3732
9.48k
                    aom_malloc(sizeof(*(pbi->row_mt_mutex_))));
3733
9.48k
    if (pbi->row_mt_mutex_) {
3734
9.48k
      pthread_mutex_init(pbi->row_mt_mutex_, NULL);
3735
9.48k
    }
3736
9.48k
  }
3737
3738
26.4k
  if (pbi->row_mt_cond_ == NULL) {
3739
9.48k
    CHECK_MEM_ERROR(cm, pbi->row_mt_cond_,
3740
9.48k
                    aom_malloc(sizeof(*(pbi->row_mt_cond_))));
3741
9.48k
    if (pbi->row_mt_cond_) {
3742
9.48k
      pthread_cond_init(pbi->row_mt_cond_, NULL);
3743
9.48k
    }
3744
9.48k
  }
3745
26.4k
#endif
3746
26.4k
}
3747
3748
static const uint8_t *decode_tiles_row_mt(AV1Decoder *pbi, const uint8_t *data,
3749
                                          const uint8_t *data_end,
3750
26.4k
                                          int start_tile, int end_tile) {
3751
26.4k
  AV1_COMMON *const cm = &pbi->common;
3752
26.4k
  CommonTileParams *const tiles = &cm->tiles;
3753
26.4k
  const int tile_cols = tiles->cols;
3754
26.4k
  const int tile_rows = tiles->rows;
3755
26.4k
  const int n_tiles = tile_cols * tile_rows;
3756
26.4k
  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
3757
26.4k
  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
3758
26.4k
  const int single_row = pbi->dec_tile_row >= 0;
3759
26.4k
  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
3760
26.4k
  const int single_col = pbi->dec_tile_col >= 0;
3761
26.4k
  int tile_rows_start;
3762
26.4k
  int tile_rows_end;
3763
26.4k
  int tile_cols_start;
3764
26.4k
  int tile_cols_end;
3765
26.4k
  int tile_count_tg;
3766
26.4k
  int num_workers = 0;
3767
26.4k
  int max_threads;
3768
26.4k
  const uint8_t *raw_data_end = NULL;
3769
26.4k
  int max_sb_rows = 0;
3770
3771
26.4k
  if (tiles->large_scale) {
3772
0
    tile_rows_start = single_row ? dec_tile_row : 0;
3773
0
    tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
3774
0
    tile_cols_start = single_col ? dec_tile_col : 0;
3775
0
    tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3776
26.4k
  } else {
3777
26.4k
    tile_rows_start = 0;
3778
26.4k
    tile_rows_end = tile_rows;
3779
26.4k
    tile_cols_start = 0;
3780
26.4k
    tile_cols_end = tile_cols;
3781
26.4k
  }
3782
26.4k
  tile_count_tg = end_tile - start_tile + 1;
3783
26.4k
  max_threads = pbi->max_threads;
3784
3785
  // No tiles to decode.
3786
26.4k
  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
3787
      // First tile is larger than end_tile.
3788
26.4k
      tile_rows_start * tile_cols + tile_cols_start > end_tile ||
3789
      // Last tile is smaller than start_tile.
3790
26.4k
      (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
3791
0
    return data;
3792
3793
26.4k
  assert(tile_rows <= MAX_TILE_ROWS);
3794
26.4k
  assert(tile_cols <= MAX_TILE_COLS);
3795
26.4k
  assert(tile_count_tg > 0);
3796
26.4k
  assert(max_threads > 0);
3797
26.4k
  assert(start_tile <= end_tile);
3798
26.4k
  assert(start_tile >= 0 && end_tile < n_tiles);
3799
3800
26.4k
  (void)tile_count_tg;
3801
3802
26.4k
  decode_mt_init(pbi);
3803
3804
  // get tile size in tile group
3805
26.4k
#if EXT_TILE_DEBUG
3806
26.4k
  if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
3807
26.4k
  if (tiles->large_scale)
3808
0
    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
3809
26.4k
  else
3810
26.4k
#endif  // EXT_TILE_DEBUG
3811
26.4k
    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
3812
3813
26.4k
  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
3814
9.64k
    if (pbi->tile_data != NULL) {
3815
362
      for (int i = 0; i < pbi->allocated_tiles; i++) {
3816
201
        TileDataDec *const tile_data = pbi->tile_data + i;
3817
201
        av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
3818
201
      }
3819
161
    }
3820
9.64k
    decoder_alloc_tile_data(pbi, n_tiles);
3821
9.64k
  }
3822
26.4k
  if (pbi->dcb.xd.seg_mask == NULL)
3823
26.4k
    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
3824
26.4k
                    (uint8_t *)aom_memalign(
3825
26.4k
                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
3826
3827
54.8k
  for (int row = 0; row < tile_rows; row++) {
3828
58.5k
    for (int col = 0; col < tile_cols; col++) {
3829
30.2k
      TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
3830
30.2k
      av1_tile_init(&tile_data->tile_info, cm, row, col);
3831
3832
30.2k
      max_sb_rows = AOMMAX(max_sb_rows,
3833
30.2k
                           av1_get_sb_rows_in_tile(cm, &tile_data->tile_info));
3834
30.2k
      num_workers += get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info);
3835
30.2k
    }
3836
28.3k
  }
3837
26.4k
  num_workers = AOMMIN(num_workers, max_threads);
3838
3839
26.4k
  if (pbi->allocated_row_mt_sync_rows != max_sb_rows) {
3840
23.5k
    for (int i = 0; i < n_tiles; ++i) {
3841
13.1k
      TileDataDec *const tile_data = pbi->tile_data + i;
3842
13.1k
      av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
3843
13.1k
      dec_row_mt_alloc(&tile_data->dec_row_mt_sync, cm, max_sb_rows);
3844
13.1k
    }
3845
10.3k
    pbi->allocated_row_mt_sync_rows = max_sb_rows;
3846
10.3k
  }
3847
3848
26.4k
  tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
3849
26.4k
                tile_cols_start, tile_cols_end, start_tile, end_tile);
3850
3851
26.4k
  dec_alloc_cb_buf(pbi);
3852
3853
26.4k
  row_mt_frame_init(pbi, tile_rows_start, tile_rows_end, tile_cols_start,
3854
26.4k
                    tile_cols_end, start_tile, end_tile, max_sb_rows);
3855
3856
26.4k
  reset_dec_workers(pbi, row_mt_worker_hook, num_workers);
3857
26.4k
  launch_dec_workers(pbi, data_end, num_workers);
3858
26.4k
  sync_dec_workers(pbi, num_workers);
3859
3860
26.4k
  if (pbi->dcb.corrupted)
3861
5.94k
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
3862
5.94k
                       "Failed to decode tile data");
3863
3864
26.4k
  if (tiles->large_scale) {
3865
0
    if (n_tiles == 1) {
3866
      // Find the end of the single tile buffer
3867
0
      return aom_reader_find_end(&pbi->tile_data->bit_reader);
3868
0
    }
3869
    // Return the end of the last tile buffer
3870
0
    return raw_data_end;
3871
0
  }
3872
26.4k
  TileDataDec *const tile_data = pbi->tile_data + end_tile;
3873
3874
26.4k
  return aom_reader_find_end(&tile_data->bit_reader);
3875
26.4k
}
3876
3877
222
static inline void error_handler(void *data) {
3878
222
  AV1_COMMON *const cm = (AV1_COMMON *)data;
3879
222
  aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
3880
222
}
3881
3882
// Reads the high_bitdepth and twelve_bit fields in color_config() and sets
3883
// seq_params->bit_depth based on the values of those fields and
3884
// seq_params->profile. Reports errors by calling rb->error_handler() or
3885
// aom_internal_error().
3886
static inline void read_bitdepth(struct aom_read_bit_buffer *rb,
3887
                                 SequenceHeader *seq_params,
3888
19.1k
                                 struct aom_internal_error_info *error_info) {
3889
19.1k
  const int high_bitdepth = aom_rb_read_bit(rb);
3890
19.1k
  if (seq_params->profile == PROFILE_2 && high_bitdepth) {
3891
2.95k
    const int twelve_bit = aom_rb_read_bit(rb);
3892
2.95k
    seq_params->bit_depth = twelve_bit ? AOM_BITS_12 : AOM_BITS_10;
3893
16.1k
  } else if (seq_params->profile <= PROFILE_2) {
3894
16.1k
    seq_params->bit_depth = high_bitdepth ? AOM_BITS_10 : AOM_BITS_8;
3895
16.1k
  } else {
3896
3
    aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
3897
3
                       "Unsupported profile/bit-depth combination");
3898
3
  }
3899
#if !CONFIG_AV1_HIGHBITDEPTH
3900
  if (seq_params->bit_depth > AOM_BITS_8) {
3901
    aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
3902
                       "Bit-depth %d not supported", seq_params->bit_depth);
3903
  }
3904
#endif
3905
19.1k
}
3906
3907
static void read_film_grain_params(AV1_COMMON *cm,
3908
13.6k
                                   struct aom_read_bit_buffer *rb) {
3909
13.6k
  aom_film_grain_t *pars = &cm->film_grain_params;
3910
13.6k
  const SequenceHeader *const seq_params = cm->seq_params;
3911
3912
13.6k
  pars->apply_grain = aom_rb_read_bit(rb);
3913
13.6k
  if (!pars->apply_grain) {
3914
10.3k
    memset(pars, 0, sizeof(*pars));
3915
10.3k
    return;
3916
10.3k
  }
3917
3918
3.31k
  pars->random_seed = aom_rb_read_literal(rb, 16);
3919
3.31k
  if (cm->current_frame.frame_type == INTER_FRAME)
3920
289
    pars->update_parameters = aom_rb_read_bit(rb);
3921
3.02k
  else
3922
3.02k
    pars->update_parameters = 1;
3923
3924
3.31k
  pars->bit_depth = seq_params->bit_depth;
3925
3926
3.31k
  if (!pars->update_parameters) {
3927
    // inherit parameters from a previous reference frame
3928
184
    int film_grain_params_ref_idx = aom_rb_read_literal(rb, 3);
3929
    // Section 6.8.20: It is a requirement of bitstream conformance that
3930
    // film_grain_params_ref_idx is equal to ref_frame_idx[ j ] for some value
3931
    // of j in the range 0 to REFS_PER_FRAME - 1.
3932
184
    int found = 0;
3933
562
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3934
550
      if (film_grain_params_ref_idx == cm->remapped_ref_idx[i]) {
3935
172
        found = 1;
3936
172
        break;
3937
172
      }
3938
550
    }
3939
184
    if (!found) {
3940
12
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3941
12
                         "Invalid film grain reference idx %d. ref_frame_idx = "
3942
12
                         "{%d, %d, %d, %d, %d, %d, %d}",
3943
12
                         film_grain_params_ref_idx, cm->remapped_ref_idx[0],
3944
12
                         cm->remapped_ref_idx[1], cm->remapped_ref_idx[2],
3945
12
                         cm->remapped_ref_idx[3], cm->remapped_ref_idx[4],
3946
12
                         cm->remapped_ref_idx[5], cm->remapped_ref_idx[6]);
3947
12
    }
3948
184
    RefCntBuffer *const buf = cm->ref_frame_map[film_grain_params_ref_idx];
3949
184
    if (buf == NULL) {
3950
0
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3951
0
                         "Invalid Film grain reference idx");
3952
0
    }
3953
184
    if (!buf->film_grain_params_present) {
3954
5
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3955
5
                         "Film grain reference parameters not available");
3956
5
    }
3957
184
    uint16_t random_seed = pars->random_seed;
3958
184
    *pars = buf->film_grain_params;   // inherit paramaters
3959
184
    pars->random_seed = random_seed;  // with new random seed
3960
184
    return;
3961
184
  }
3962
3963
  // Scaling functions parameters
3964
3.13k
  pars->num_y_points = aom_rb_read_literal(rb, 4);  // max 14
3965
3.13k
  if (pars->num_y_points > 14)
3966
12
    aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3967
12
                       "Number of points for film grain luma scaling function "
3968
12
                       "exceeds the maximum value.");
3969
3.71k
  for (int i = 0; i < pars->num_y_points; i++) {
3970
584
    pars->scaling_points_y[i][0] = aom_rb_read_literal(rb, 8);
3971
584
    if (i && pars->scaling_points_y[i - 1][0] >= pars->scaling_points_y[i][0])
3972
40
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3973
40
                         "First coordinate of the scaling function points "
3974
40
                         "shall be increasing.");
3975
584
    pars->scaling_points_y[i][1] = aom_rb_read_literal(rb, 8);
3976
584
  }
3977
3978
3.13k
  if (!seq_params->monochrome)
3979
2.77k
    pars->chroma_scaling_from_luma = aom_rb_read_bit(rb);
3980
354
  else
3981
354
    pars->chroma_scaling_from_luma = 0;
3982
3983
3.13k
  if (seq_params->monochrome || pars->chroma_scaling_from_luma ||
3984
2.53k
      ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
3985
2.51k
       (pars->num_y_points == 0))) {
3986
2.51k
    pars->num_cb_points = 0;
3987
2.51k
    pars->num_cr_points = 0;
3988
2.51k
  } else {
3989
618
    pars->num_cb_points = aom_rb_read_literal(rb, 4);  // max 10
3990
618
    if (pars->num_cb_points > 10)
3991
2
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3992
2
                         "Number of points for film grain cb scaling function "
3993
2
                         "exceeds the maximum value.");
3994
811
    for (int i = 0; i < pars->num_cb_points; i++) {
3995
193
      pars->scaling_points_cb[i][0] = aom_rb_read_literal(rb, 8);
3996
193
      if (i &&
3997
93
          pars->scaling_points_cb[i - 1][0] >= pars->scaling_points_cb[i][0])
3998
8
        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3999
8
                           "First coordinate of the scaling function points "
4000
8
                           "shall be increasing.");
4001
193
      pars->scaling_points_cb[i][1] = aom_rb_read_literal(rb, 8);
4002
193
    }
4003
4004
618
    pars->num_cr_points = aom_rb_read_literal(rb, 4);  // max 10
4005
618
    if (pars->num_cr_points > 10)
4006
2
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4007
2
                         "Number of points for film grain cr scaling function "
4008
2
                         "exceeds the maximum value.");
4009
762
    for (int i = 0; i < pars->num_cr_points; i++) {
4010
144
      pars->scaling_points_cr[i][0] = aom_rb_read_literal(rb, 8);
4011
144
      if (i &&
4012
65
          pars->scaling_points_cr[i - 1][0] >= pars->scaling_points_cr[i][0])
4013
9
        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4014
9
                           "First coordinate of the scaling function points "
4015
9
                           "shall be increasing.");
4016
144
      pars->scaling_points_cr[i][1] = aom_rb_read_literal(rb, 8);
4017
144
    }
4018
4019
618
    if ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
4020
20
        (((pars->num_cb_points == 0) && (pars->num_cr_points != 0)) ||
4021
16
         ((pars->num_cb_points != 0) && (pars->num_cr_points == 0))))
4022
5
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4023
5
                         "In YCbCr 4:2:0, film grain shall be applied "
4024
5
                         "to both chroma components or neither.");
4025
618
  }
4026
4027
3.13k
  pars->scaling_shift = aom_rb_read_literal(rb, 2) + 8;  // 8 + value
4028
4029
  // AR coefficients
4030
  // Only sent if the corresponsing scaling function has
4031
  // more than 0 points
4032
4033
3.13k
  pars->ar_coeff_lag = aom_rb_read_literal(rb, 2);
4034
4035
3.13k
  int num_pos_luma = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1);
4036
3.13k
  int num_pos_chroma = num_pos_luma;
4037
3.13k
  if (pars->num_y_points > 0) ++num_pos_chroma;
4038
4039
3.13k
  if (pars->num_y_points)
4040
1.61k
    for (int i = 0; i < num_pos_luma; i++)
4041
1.32k
      pars->ar_coeffs_y[i] = aom_rb_read_literal(rb, 8) - 128;
4042
4043
3.13k
  if (pars->num_cb_points || pars->chroma_scaling_from_luma)
4044
2.80k
    for (int i = 0; i < num_pos_chroma; i++)
4045
2.48k
      pars->ar_coeffs_cb[i] = aom_rb_read_literal(rb, 8) - 128;
4046
4047
3.13k
  if (pars->num_cr_points || pars->chroma_scaling_from_luma)
4048
2.73k
    for (int i = 0; i < num_pos_chroma; i++)
4049
2.43k
      pars->ar_coeffs_cr[i] = aom_rb_read_literal(rb, 8) - 128;
4050
4051
3.13k
  pars->ar_coeff_shift = aom_rb_read_literal(rb, 2) + 6;  // 6 + value
4052
4053
3.13k
  pars->grain_scale_shift = aom_rb_read_literal(rb, 2);
4054
4055
3.13k
  if (pars->num_cb_points) {
4056
80
    pars->cb_mult = aom_rb_read_literal(rb, 8);
4057
80
    pars->cb_luma_mult = aom_rb_read_literal(rb, 8);
4058
80
    pars->cb_offset = aom_rb_read_literal(rb, 9);
4059
80
  }
4060
4061
3.13k
  if (pars->num_cr_points) {
4062
61
    pars->cr_mult = aom_rb_read_literal(rb, 8);
4063
61
    pars->cr_luma_mult = aom_rb_read_literal(rb, 8);
4064
61
    pars->cr_offset = aom_rb_read_literal(rb, 9);
4065
61
  }
4066
4067
3.13k
  pars->overlap_flag = aom_rb_read_bit(rb);
4068
4069
3.13k
  pars->clip_to_restricted_range = aom_rb_read_bit(rb);
4070
3.13k
}
4071
4072
static inline void read_film_grain(AV1_COMMON *cm,
4073
31.9k
                                   struct aom_read_bit_buffer *rb) {
4074
31.9k
  if (cm->seq_params->film_grain_params_present &&
4075
16.7k
      (cm->show_frame || cm->showable_frame)) {
4076
13.6k
    read_film_grain_params(cm, rb);
4077
18.3k
  } else {
4078
18.3k
    memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
4079
18.3k
  }
4080
31.9k
  cm->film_grain_params.bit_depth = cm->seq_params->bit_depth;
4081
31.9k
  memcpy(&cm->cur_frame->film_grain_params, &cm->film_grain_params,
4082
31.9k
         sizeof(aom_film_grain_t));
4083
31.9k
}
4084
4085
void av1_read_color_config(struct aom_read_bit_buffer *rb,
4086
                           int allow_lowbitdepth, SequenceHeader *seq_params,
4087
19.1k
                           struct aom_internal_error_info *error_info) {
4088
19.1k
  read_bitdepth(rb, seq_params, error_info);
4089
4090
19.1k
  seq_params->use_highbitdepth =
4091
19.1k
      seq_params->bit_depth > AOM_BITS_8 || !allow_lowbitdepth;
4092
  // monochrome bit (not needed for PROFILE_1)
4093
19.1k
  const int is_monochrome =
4094
19.1k
      seq_params->profile != PROFILE_1 ? aom_rb_read_bit(rb) : 0;
4095
19.1k
  seq_params->monochrome = is_monochrome;
4096
19.1k
  int color_description_present_flag = aom_rb_read_bit(rb);
4097
19.1k
  if (color_description_present_flag) {
4098
2.36k
    seq_params->color_primaries = aom_rb_read_literal(rb, 8);
4099
2.36k
    seq_params->transfer_characteristics = aom_rb_read_literal(rb, 8);
4100
2.36k
    seq_params->matrix_coefficients = aom_rb_read_literal(rb, 8);
4101
16.7k
  } else {
4102
16.7k
    seq_params->color_primaries = AOM_CICP_CP_UNSPECIFIED;
4103
16.7k
    seq_params->transfer_characteristics = AOM_CICP_TC_UNSPECIFIED;
4104
16.7k
    seq_params->matrix_coefficients = AOM_CICP_MC_UNSPECIFIED;
4105
16.7k
  }
4106
19.1k
  if (is_monochrome) {
4107
    // [16,235] (including xvycc) vs [0,255] range
4108
3.23k
    seq_params->color_range = aom_rb_read_bit(rb);
4109
3.23k
    seq_params->subsampling_y = seq_params->subsampling_x = 1;
4110
3.23k
    seq_params->chroma_sample_position = AOM_CSP_UNKNOWN;
4111
3.23k
    seq_params->separate_uv_delta_q = 0;
4112
3.23k
    return;
4113
3.23k
  }
4114
15.9k
  if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
4115
1.20k
      seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
4116
1.10k
      seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
4117
696
    seq_params->subsampling_y = seq_params->subsampling_x = 0;
4118
696
    seq_params->color_range = 1;  // assume full color-range
4119
696
    if (!(seq_params->profile == PROFILE_1 ||
4120
41
          (seq_params->profile == PROFILE_2 &&
4121
40
           seq_params->bit_depth == AOM_BITS_12))) {
4122
2
      aom_internal_error(
4123
2
          error_info, AOM_CODEC_UNSUP_BITSTREAM,
4124
2
          "sRGB colorspace not compatible with specified profile");
4125
2
    }
4126
15.2k
  } else {
4127
    // [16,235] (including xvycc) vs [0,255] range
4128
15.2k
    seq_params->color_range = aom_rb_read_bit(rb);
4129
15.2k
    if (seq_params->profile == PROFILE_0) {
4130
      // 420 only
4131
6.99k
      seq_params->subsampling_x = seq_params->subsampling_y = 1;
4132
8.23k
    } else if (seq_params->profile == PROFILE_1) {
4133
      // 444 only
4134
4.59k
      seq_params->subsampling_x = seq_params->subsampling_y = 0;
4135
4.59k
    } else {
4136
3.64k
      assert(seq_params->profile == PROFILE_2);
4137
3.64k
      if (seq_params->bit_depth == AOM_BITS_12) {
4138
2.14k
        seq_params->subsampling_x = aom_rb_read_bit(rb);
4139
2.14k
        if (seq_params->subsampling_x)
4140
289
          seq_params->subsampling_y = aom_rb_read_bit(rb);  // 422 or 420
4141
1.85k
        else
4142
1.85k
          seq_params->subsampling_y = 0;  // 444
4143
2.14k
      } else {
4144
        // 422
4145
1.49k
        seq_params->subsampling_x = 1;
4146
1.49k
        seq_params->subsampling_y = 0;
4147
1.49k
      }
4148
3.64k
    }
4149
15.2k
    if (seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY &&
4150
219
        (seq_params->subsampling_x || seq_params->subsampling_y)) {
4151
3
      aom_internal_error(
4152
3
          error_info, AOM_CODEC_UNSUP_BITSTREAM,
4153
3
          "Identity CICP Matrix incompatible with non 4:4:4 color sampling");
4154
3
    }
4155
15.2k
    if (seq_params->subsampling_x && seq_params->subsampling_y) {
4156
7.03k
      seq_params->chroma_sample_position = aom_rb_read_literal(rb, 2);
4157
7.03k
    }
4158
15.2k
  }
4159
15.9k
  seq_params->separate_uv_delta_q = aom_rb_read_bit(rb);
4160
15.9k
}
4161
4162
void av1_read_timing_info_header(aom_timing_info_t *timing_info,
4163
                                 struct aom_internal_error_info *error,
4164
385
                                 struct aom_read_bit_buffer *rb) {
4165
385
  timing_info->num_units_in_display_tick =
4166
385
      aom_rb_read_unsigned_literal(rb,
4167
385
                                   32);  // Number of units in a display tick
4168
385
  timing_info->time_scale = aom_rb_read_unsigned_literal(rb, 32);  // Time scale
4169
385
  if (timing_info->num_units_in_display_tick == 0 ||
4170
379
      timing_info->time_scale == 0) {
4171
2
    aom_internal_error(
4172
2
        error, AOM_CODEC_UNSUP_BITSTREAM,
4173
2
        "num_units_in_display_tick and time_scale must be greater than 0.");
4174
2
  }
4175
385
  timing_info->equal_picture_interval =
4176
385
      aom_rb_read_bit(rb);  // Equal picture interval bit
4177
385
  if (timing_info->equal_picture_interval) {
4178
51
    const uint32_t num_ticks_per_picture_minus_1 = aom_rb_read_uvlc(rb);
4179
51
    if (num_ticks_per_picture_minus_1 == UINT32_MAX) {
4180
1
      aom_internal_error(
4181
1
          error, AOM_CODEC_UNSUP_BITSTREAM,
4182
1
          "num_ticks_per_picture_minus_1 cannot be (1 << 32) - 1.");
4183
1
    }
4184
51
    timing_info->num_ticks_per_picture = num_ticks_per_picture_minus_1 + 1;
4185
51
  }
4186
385
}
4187
4188
void av1_read_decoder_model_info(aom_dec_model_info_t *decoder_model_info,
4189
352
                                 struct aom_read_bit_buffer *rb) {
4190
352
  decoder_model_info->encoder_decoder_buffer_delay_length =
4191
352
      aom_rb_read_literal(rb, 5) + 1;
4192
352
  decoder_model_info->num_units_in_decoding_tick =
4193
352
      aom_rb_read_unsigned_literal(rb,
4194
352
                                   32);  // Number of units in a decoding tick
4195
352
  decoder_model_info->buffer_removal_time_length =
4196
352
      aom_rb_read_literal(rb, 5) + 1;
4197
352
  decoder_model_info->frame_presentation_time_length =
4198
352
      aom_rb_read_literal(rb, 5) + 1;
4199
352
}
4200
4201
void av1_read_op_parameters_info(aom_dec_model_op_parameters_t *op_params,
4202
                                 int buffer_delay_length,
4203
492
                                 struct aom_read_bit_buffer *rb) {
4204
492
  op_params->decoder_buffer_delay =
4205
492
      aom_rb_read_unsigned_literal(rb, buffer_delay_length);
4206
492
  op_params->encoder_buffer_delay =
4207
492
      aom_rb_read_unsigned_literal(rb, buffer_delay_length);
4208
492
  op_params->low_delay_mode_flag = aom_rb_read_bit(rb);
4209
492
}
4210
4211
static inline void read_temporal_point_info(AV1_COMMON *const cm,
4212
15
                                            struct aom_read_bit_buffer *rb) {
4213
15
  cm->frame_presentation_time = aom_rb_read_unsigned_literal(
4214
15
      rb, cm->seq_params->decoder_model_info.frame_presentation_time_length);
4215
15
}
4216
4217
void av1_read_sequence_header(AV1_COMMON *cm, struct aom_read_bit_buffer *rb,
4218
19.1k
                              SequenceHeader *seq_params) {
4219
19.1k
  const int num_bits_width = aom_rb_read_literal(rb, 4) + 1;
4220
19.1k
  const int num_bits_height = aom_rb_read_literal(rb, 4) + 1;
4221
19.1k
  const int max_frame_width = aom_rb_read_literal(rb, num_bits_width) + 1;
4222
19.1k
  const int max_frame_height = aom_rb_read_literal(rb, num_bits_height) + 1;
4223
4224
19.1k
  seq_params->num_bits_width = num_bits_width;
4225
19.1k
  seq_params->num_bits_height = num_bits_height;
4226
19.1k
  seq_params->max_frame_width = max_frame_width;
4227
19.1k
  seq_params->max_frame_height = max_frame_height;
4228
4229
19.1k
  if (seq_params->reduced_still_picture_hdr) {
4230
12.8k
    seq_params->frame_id_numbers_present_flag = 0;
4231
12.8k
  } else {
4232
6.31k
    seq_params->frame_id_numbers_present_flag = aom_rb_read_bit(rb);
4233
6.31k
  }
4234
19.1k
  if (seq_params->frame_id_numbers_present_flag) {
4235
    // We must always have delta_frame_id_length < frame_id_length,
4236
    // in order for a frame to be referenced with a unique delta.
4237
    // Avoid wasting bits by using a coding that enforces this restriction.
4238
321
    seq_params->delta_frame_id_length = aom_rb_read_literal(rb, 4) + 2;
4239
321
    seq_params->frame_id_length =
4240
321
        aom_rb_read_literal(rb, 3) + seq_params->delta_frame_id_length + 1;
4241
321
    if (seq_params->frame_id_length > 16)
4242
8
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
4243
8
                         "Invalid frame_id_length");
4244
321
  }
4245
4246
19.1k
  setup_sb_size(seq_params, rb);
4247
4248
19.1k
  seq_params->enable_filter_intra = aom_rb_read_bit(rb);
4249
19.1k
  seq_params->enable_intra_edge_filter = aom_rb_read_bit(rb);
4250
4251
19.1k
  if (seq_params->reduced_still_picture_hdr) {
4252
12.8k
    seq_params->enable_interintra_compound = 0;
4253
12.8k
    seq_params->enable_masked_compound = 0;
4254
12.8k
    seq_params->enable_warped_motion = 0;
4255
12.8k
    seq_params->enable_dual_filter = 0;
4256
12.8k
    seq_params->order_hint_info.enable_order_hint = 0;
4257
12.8k
    seq_params->order_hint_info.enable_dist_wtd_comp = 0;
4258
12.8k
    seq_params->order_hint_info.enable_ref_frame_mvs = 0;
4259
12.8k
    seq_params->force_screen_content_tools = 2;  // SELECT_SCREEN_CONTENT_TOOLS
4260
12.8k
    seq_params->force_integer_mv = 2;            // SELECT_INTEGER_MV
4261
12.8k
    seq_params->order_hint_info.order_hint_bits_minus_1 = -1;
4262
12.8k
  } else {
4263
6.31k
    seq_params->enable_interintra_compound = aom_rb_read_bit(rb);
4264
6.31k
    seq_params->enable_masked_compound = aom_rb_read_bit(rb);
4265
6.31k
    seq_params->enable_warped_motion = aom_rb_read_bit(rb);
4266
6.31k
    seq_params->enable_dual_filter = aom_rb_read_bit(rb);
4267
4268
6.31k
    seq_params->order_hint_info.enable_order_hint = aom_rb_read_bit(rb);
4269
6.31k
    seq_params->order_hint_info.enable_dist_wtd_comp =
4270
6.31k
        seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
4271
6.31k
    seq_params->order_hint_info.enable_ref_frame_mvs =
4272
6.31k
        seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
4273
4274
6.31k
    if (aom_rb_read_bit(rb)) {
4275
2.20k
      seq_params->force_screen_content_tools =
4276
2.20k
          2;  // SELECT_SCREEN_CONTENT_TOOLS
4277
4.11k
    } else {
4278
4.11k
      seq_params->force_screen_content_tools = aom_rb_read_bit(rb);
4279
4.11k
    }
4280
4281
6.31k
    if (seq_params->force_screen_content_tools > 0) {
4282
2.79k
      if (aom_rb_read_bit(rb)) {
4283
1.91k
        seq_params->force_integer_mv = 2;  // SELECT_INTEGER_MV
4284
1.91k
      } else {
4285
875
        seq_params->force_integer_mv = aom_rb_read_bit(rb);
4286
875
      }
4287
3.52k
    } else {
4288
3.52k
      seq_params->force_integer_mv = 2;  // SELECT_INTEGER_MV
4289
3.52k
    }
4290
6.31k
    seq_params->order_hint_info.order_hint_bits_minus_1 =
4291
6.31k
        seq_params->order_hint_info.enable_order_hint
4292
6.31k
            ? aom_rb_read_literal(rb, 3)
4293
6.31k
            : -1;
4294
6.31k
  }
4295
4296
19.1k
  seq_params->enable_superres = aom_rb_read_bit(rb);
4297
19.1k
  seq_params->enable_cdef = aom_rb_read_bit(rb);
4298
19.1k
  seq_params->enable_restoration = aom_rb_read_bit(rb);
4299
19.1k
}
4300
4301
static int read_global_motion_params(WarpedMotionParams *params,
4302
                                     const WarpedMotionParams *ref_params,
4303
                                     struct aom_read_bit_buffer *rb,
4304
88.2k
                                     int allow_hp) {
4305
88.2k
  TransformationType type = aom_rb_read_bit(rb);
4306
88.2k
  if (type != IDENTITY) {
4307
32.8k
    if (aom_rb_read_bit(rb))
4308
17.2k
      type = ROTZOOM;
4309
15.5k
    else
4310
15.5k
      type = aom_rb_read_bit(rb) ? TRANSLATION : AFFINE;
4311
32.8k
  }
4312
4313
88.2k
  *params = default_warp_params;
4314
88.2k
  params->wmtype = type;
4315
4316
88.2k
  if (type >= ROTZOOM) {
4317
27.0k
    params->wmmat[2] = aom_rb_read_signed_primitive_refsubexpfin(
4318
27.0k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4319
27.0k
                           (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
4320
27.0k
                               (1 << GM_ALPHA_PREC_BITS)) *
4321
27.0k
                           GM_ALPHA_DECODE_FACTOR +
4322
27.0k
                       (1 << WARPEDMODEL_PREC_BITS);
4323
27.0k
    params->wmmat[3] = aom_rb_read_signed_primitive_refsubexpfin(
4324
27.0k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4325
27.0k
                           (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
4326
27.0k
                       GM_ALPHA_DECODE_FACTOR;
4327
27.0k
  }
4328
4329
88.2k
  if (type >= AFFINE) {
4330
9.83k
    params->wmmat[4] = aom_rb_read_signed_primitive_refsubexpfin(
4331
9.83k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4332
9.83k
                           (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
4333
9.83k
                       GM_ALPHA_DECODE_FACTOR;
4334
9.83k
    params->wmmat[5] = aom_rb_read_signed_primitive_refsubexpfin(
4335
9.83k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4336
9.83k
                           (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
4337
9.83k
                               (1 << GM_ALPHA_PREC_BITS)) *
4338
9.83k
                           GM_ALPHA_DECODE_FACTOR +
4339
9.83k
                       (1 << WARPEDMODEL_PREC_BITS);
4340
78.4k
  } else {
4341
78.4k
    params->wmmat[4] = -params->wmmat[3];
4342
78.4k
    params->wmmat[5] = params->wmmat[2];
4343
78.4k
  }
4344
4345
88.2k
  if (type >= TRANSLATION) {
4346
32.8k
    const int trans_bits = (type == TRANSLATION)
4347
32.8k
                               ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4348
32.8k
                               : GM_ABS_TRANS_BITS;
4349
32.8k
    const int trans_dec_factor =
4350
32.8k
        (type == TRANSLATION) ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4351
32.8k
                              : GM_TRANS_DECODE_FACTOR;
4352
32.8k
    const int trans_prec_diff = (type == TRANSLATION)
4353
32.8k
                                    ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
4354
32.8k
                                    : GM_TRANS_PREC_DIFF;
4355
32.8k
    params->wmmat[0] = aom_rb_read_signed_primitive_refsubexpfin(
4356
32.8k
                           rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
4357
32.8k
                           (ref_params->wmmat[0] >> trans_prec_diff)) *
4358
32.8k
                       trans_dec_factor;
4359
32.8k
    params->wmmat[1] = aom_rb_read_signed_primitive_refsubexpfin(
4360
32.8k
                           rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
4361
32.8k
                           (ref_params->wmmat[1] >> trans_prec_diff)) *
4362
32.8k
                       trans_dec_factor;
4363
32.8k
  }
4364
4365
88.2k
  int good_shear_params = av1_get_shear_params(params);
4366
88.2k
  if (!good_shear_params) return 0;
4367
4368
87.4k
  return 1;
4369
88.2k
}
4370
4371
static inline void read_global_motion(AV1_COMMON *cm,
4372
12.6k
                                      struct aom_read_bit_buffer *rb) {
4373
100k
  for (int frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4374
88.2k
    const WarpedMotionParams *ref_params =
4375
88.2k
        cm->prev_frame ? &cm->prev_frame->global_motion[frame]
4376
88.2k
                       : &default_warp_params;
4377
88.2k
    int good_params =
4378
88.2k
        read_global_motion_params(&cm->global_motion[frame], ref_params, rb,
4379
88.2k
                                  cm->features.allow_high_precision_mv);
4380
88.2k
    if (!good_params) {
4381
#if WARPED_MOTION_DEBUG
4382
      printf("Warning: unexpected global motion shear params from aomenc\n");
4383
#endif
4384
760
      cm->global_motion[frame].invalid = 1;
4385
760
    }
4386
4387
    // TODO(sarahparker, debargha): The logic in the commented out code below
4388
    // does not work currently and causes mismatches when resize is on. Fix it
4389
    // before turning the optimization back on.
4390
    /*
4391
    YV12_BUFFER_CONFIG *ref_buf = get_ref_frame(cm, frame);
4392
    if (cm->width == ref_buf->y_crop_width &&
4393
        cm->height == ref_buf->y_crop_height) {
4394
      read_global_motion_params(&cm->global_motion[frame],
4395
                                &cm->prev_frame->global_motion[frame], rb,
4396
                                cm->features.allow_high_precision_mv);
4397
    } else {
4398
      cm->global_motion[frame] = default_warp_params;
4399
    }
4400
    */
4401
    /*
4402
    printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
4403
           frame, cm->current_frame.frame_number, cm->show_frame,
4404
           cm->global_motion[frame].wmmat[0],
4405
           cm->global_motion[frame].wmmat[1],
4406
           cm->global_motion[frame].wmmat[2],
4407
           cm->global_motion[frame].wmmat[3]);
4408
           */
4409
88.2k
  }
4410
12.6k
  memcpy(cm->cur_frame->global_motion, cm->global_motion,
4411
12.6k
         REF_FRAMES * sizeof(WarpedMotionParams));
4412
12.6k
}
4413
4414
// Release the references to the frame buffers in cm->ref_frame_map and reset
4415
// all elements of cm->ref_frame_map to NULL.
4416
18.1k
static inline void reset_ref_frame_map(AV1_COMMON *const cm) {
4417
18.1k
  BufferPool *const pool = cm->buffer_pool;
4418
4419
162k
  for (int i = 0; i < REF_FRAMES; i++) {
4420
144k
    decrease_ref_count(cm->ref_frame_map[i], pool);
4421
144k
    cm->ref_frame_map[i] = NULL;
4422
144k
  }
4423
18.1k
}
4424
4425
// If the refresh_frame_flags bitmask is set, update reference frame id values
4426
// and mark frames as valid for reference.
4427
32.1k
static inline void update_ref_frame_id(AV1Decoder *const pbi) {
4428
32.1k
  AV1_COMMON *const cm = &pbi->common;
4429
32.1k
  int refresh_frame_flags = cm->current_frame.refresh_frame_flags;
4430
288k
  for (int i = 0; i < REF_FRAMES; i++) {
4431
256k
    if ((refresh_frame_flags >> i) & 1) {
4432
192k
      cm->ref_frame_id[i] = cm->current_frame_id;
4433
192k
      pbi->valid_for_referencing[i] = 1;
4434
192k
    }
4435
256k
  }
4436
32.1k
}
4437
4438
static inline void show_existing_frame_reset(AV1Decoder *const pbi,
4439
39
                                             int existing_frame_idx) {
4440
39
  AV1_COMMON *const cm = &pbi->common;
4441
4442
39
  assert(cm->show_existing_frame);
4443
4444
39
  cm->current_frame.frame_type = KEY_FRAME;
4445
4446
39
  cm->current_frame.refresh_frame_flags = (1 << REF_FRAMES) - 1;
4447
4448
312
  for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4449
273
    cm->remapped_ref_idx[i] = INVALID_IDX;
4450
273
  }
4451
4452
39
  if (pbi->need_resync) {
4453
0
    reset_ref_frame_map(cm);
4454
0
    pbi->need_resync = 0;
4455
0
  }
4456
4457
  // Note that the displayed frame must be valid for referencing in order to
4458
  // have been selected.
4459
39
  cm->current_frame_id = cm->ref_frame_id[existing_frame_idx];
4460
39
  update_ref_frame_id(pbi);
4461
4462
39
  cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
4463
39
}
4464
4465
3.25k
static inline void reset_frame_buffers(AV1_COMMON *cm) {
4466
3.25k
  RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
4467
3.25k
  int i;
4468
4469
3.25k
  lock_buffer_pool(cm->buffer_pool);
4470
3.25k
  reset_ref_frame_map(cm);
4471
3.25k
  assert(cm->cur_frame->ref_count == 1);
4472
55.3k
  for (i = 0; i < cm->buffer_pool->num_frame_bufs; ++i) {
4473
    // Reset all unreferenced frame buffers. We can also reset cm->cur_frame
4474
    // because we are the sole owner of cm->cur_frame.
4475
52.0k
    if (frame_bufs[i].ref_count > 0 && &frame_bufs[i] != cm->cur_frame) {
4476
1.81k
      continue;
4477
1.81k
    }
4478
50.2k
    frame_bufs[i].order_hint = 0;
4479
50.2k
    av1_zero(frame_bufs[i].ref_order_hints);
4480
50.2k
  }
4481
3.25k
  av1_zero_unused_internal_frame_buffers(&cm->buffer_pool->int_frame_buffers);
4482
3.25k
  unlock_buffer_pool(cm->buffer_pool);
4483
3.25k
}
4484
4485
// On success, returns 0. On failure, calls aom_internal_error and does not
4486
// return.
4487
static int read_uncompressed_header(AV1Decoder *pbi,
4488
32.4k
                                    struct aom_read_bit_buffer *rb) {
4489
32.4k
  AV1_COMMON *const cm = &pbi->common;
4490
32.4k
  const SequenceHeader *const seq_params = cm->seq_params;
4491
32.4k
  CurrentFrame *const current_frame = &cm->current_frame;
4492
32.4k
  FeatureFlags *const features = &cm->features;
4493
32.4k
  MACROBLOCKD *const xd = &pbi->dcb.xd;
4494
32.4k
  BufferPool *const pool = cm->buffer_pool;
4495
32.4k
  RefCntBuffer *const frame_bufs = pool->frame_bufs;
4496
32.4k
  aom_s_frame_info *sframe_info = &pbi->sframe_info;
4497
32.4k
  sframe_info->is_s_frame = 0;
4498
32.4k
  sframe_info->is_s_frame_at_altref = 0;
4499
4500
32.4k
  if (!pbi->sequence_header_ready) {
4501
2
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4502
2
                       "No sequence header");
4503
2
  }
4504
4505
32.4k
  if (seq_params->reduced_still_picture_hdr) {
4506
12.8k
    cm->show_existing_frame = 0;
4507
12.8k
    cm->show_frame = 1;
4508
12.8k
    current_frame->frame_type = KEY_FRAME;
4509
12.8k
    if (pbi->sequence_header_changed) {
4510
      // This is the start of a new coded video sequence.
4511
1.70k
      pbi->sequence_header_changed = 0;
4512
1.70k
      pbi->decoding_first_frame = 1;
4513
1.70k
      reset_frame_buffers(cm);
4514
1.70k
    }
4515
12.8k
    features->error_resilient_mode = 1;
4516
19.6k
  } else {
4517
19.6k
    cm->show_existing_frame = aom_rb_read_bit(rb);
4518
19.6k
    pbi->reset_decoder_state = 0;
4519
4520
19.6k
    if (cm->show_existing_frame) {
4521
274
      if (pbi->sequence_header_changed) {
4522
2
        aom_internal_error(
4523
2
            &pbi->error, AOM_CODEC_CORRUPT_FRAME,
4524
2
            "New sequence header starts with a show_existing_frame.");
4525
2
      }
4526
      // Show an existing frame directly.
4527
274
      const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4528
274
      RefCntBuffer *const frame_to_show = cm->ref_frame_map[existing_frame_idx];
4529
274
      if (frame_to_show == NULL) {
4530
1
        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4531
1
                           "Buffer does not contain a decoded frame");
4532
1
      }
4533
274
      if (seq_params->decoder_model_info_present_flag &&
4534
2
          seq_params->timing_info.equal_picture_interval == 0) {
4535
2
        read_temporal_point_info(cm, rb);
4536
2
      }
4537
274
      if (seq_params->frame_id_numbers_present_flag) {
4538
2
        int frame_id_length = seq_params->frame_id_length;
4539
2
        int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
4540
        /* Compare display_frame_id with ref_frame_id and check valid for
4541
         * referencing */
4542
2
        if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
4543
1
            pbi->valid_for_referencing[existing_frame_idx] == 0)
4544
1
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4545
1
                             "Reference buffer frame ID mismatch");
4546
2
      }
4547
274
      lock_buffer_pool(pool);
4548
274
      assert(frame_to_show->ref_count > 0);
4549
      // cm->cur_frame should be the buffer referenced by the return value
4550
      // of the get_free_fb() call in assign_cur_frame_new_fb() (called by
4551
      // av1_receive_compressed_data()), so the ref_count should be 1.
4552
274
      assert(cm->cur_frame->ref_count == 1);
4553
      // assign_frame_buffer_p() decrements ref_count directly rather than
4554
      // call decrease_ref_count(). If cm->cur_frame->raw_frame_buffer has
4555
      // already been allocated, it will not be released by
4556
      // assign_frame_buffer_p()!
4557
274
      assert(!cm->cur_frame->raw_frame_buffer.data);
4558
274
      assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
4559
274
      pbi->reset_decoder_state = frame_to_show->frame_type == KEY_FRAME;
4560
274
      unlock_buffer_pool(pool);
4561
4562
274
      cm->lf.filter_level[0] = 0;
4563
274
      cm->lf.filter_level[1] = 0;
4564
274
      cm->show_frame = 1;
4565
274
      current_frame->order_hint = frame_to_show->order_hint;
4566
4567
      // Section 6.8.2: It is a requirement of bitstream conformance that when
4568
      // show_existing_frame is used to show a previous frame, that the value
4569
      // of showable_frame for the previous frame was equal to 1.
4570
274
      if (!frame_to_show->showable_frame) {
4571
5
        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4572
5
                           "Buffer does not contain a showable frame");
4573
5
      }
4574
      // Section 6.8.2: It is a requirement of bitstream conformance that when
4575
      // show_existing_frame is used to show a previous frame with
4576
      // RefFrameType[ frame_to_show_map_idx ] equal to KEY_FRAME, that the
4577
      // frame is output via the show_existing_frame mechanism at most once.
4578
274
      if (pbi->reset_decoder_state) frame_to_show->showable_frame = 0;
4579
4580
274
      cm->film_grain_params = frame_to_show->film_grain_params;
4581
4582
274
      if (pbi->reset_decoder_state) {
4583
39
        show_existing_frame_reset(pbi, existing_frame_idx);
4584
235
      } else {
4585
235
        current_frame->refresh_frame_flags = 0;
4586
235
      }
4587
4588
274
      return 0;
4589
274
    }
4590
4591
19.3k
    current_frame->frame_type = (FRAME_TYPE)aom_rb_read_literal(rb, 2);
4592
19.3k
    if (pbi->sequence_header_changed) {
4593
1.55k
      if (current_frame->frame_type == KEY_FRAME) {
4594
        // This is the start of a new coded video sequence.
4595
1.55k
        pbi->sequence_header_changed = 0;
4596
1.55k
        pbi->decoding_first_frame = 1;
4597
1.55k
        reset_frame_buffers(cm);
4598
1.55k
      } else {
4599
1
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4600
1
                           "Sequence header has changed without a keyframe.");
4601
1
      }
4602
1.55k
    }
4603
4604
19.3k
    cm->show_frame = aom_rb_read_bit(rb);
4605
19.3k
    if (cm->show_frame == 0) pbi->is_arf_frame_present = 1;
4606
19.3k
    if (cm->show_frame == 0 && cm->current_frame.frame_type == KEY_FRAME)
4607
3.56k
      pbi->is_fwd_kf_present = 1;
4608
19.3k
    if (cm->current_frame.frame_type == S_FRAME) {
4609
133
      sframe_info->is_s_frame = 1;
4610
133
      sframe_info->is_s_frame_at_altref = cm->show_frame ? 0 : 1;
4611
133
    }
4612
19.3k
    if (seq_params->still_picture &&
4613
71
        (current_frame->frame_type != KEY_FRAME || !cm->show_frame)) {
4614
3
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4615
3
                         "Still pictures must be coded as shown keyframes");
4616
3
    }
4617
19.3k
    cm->showable_frame = current_frame->frame_type != KEY_FRAME;
4618
19.3k
    if (cm->show_frame) {
4619
12.8k
      if (seq_params->decoder_model_info_present_flag &&
4620
16
          seq_params->timing_info.equal_picture_interval == 0)
4621
13
        read_temporal_point_info(cm, rb);
4622
12.8k
    } else {
4623
      // See if this frame can be used as show_existing_frame in future
4624
6.50k
      cm->showable_frame = aom_rb_read_bit(rb);
4625
6.50k
    }
4626
19.3k
    cm->cur_frame->showable_frame = cm->showable_frame;
4627
19.3k
    features->error_resilient_mode =
4628
19.3k
        frame_is_sframe(cm) ||
4629
19.2k
                (current_frame->frame_type == KEY_FRAME && cm->show_frame)
4630
19.3k
            ? 1
4631
19.3k
            : aom_rb_read_bit(rb);
4632
19.3k
  }
4633
4634
32.1k
  if (current_frame->frame_type == KEY_FRAME && cm->show_frame) {
4635
    /* All frames need to be marked as not valid for referencing */
4636
141k
    for (int i = 0; i < REF_FRAMES; i++) {
4637
125k
      pbi->valid_for_referencing[i] = 0;
4638
125k
    }
4639
15.6k
  }
4640
32.1k
  features->disable_cdf_update = aom_rb_read_bit(rb);
4641
32.1k
  if (seq_params->force_screen_content_tools == 2) {
4642
17.1k
    features->allow_screen_content_tools = aom_rb_read_bit(rb);
4643
17.1k
  } else {
4644
15.0k
    features->allow_screen_content_tools =
4645
15.0k
        seq_params->force_screen_content_tools;
4646
15.0k
  }
4647
4648
32.1k
  if (features->allow_screen_content_tools) {
4649
12.5k
    if (seq_params->force_integer_mv == 2) {
4650
11.3k
      features->cur_frame_force_integer_mv = aom_rb_read_bit(rb);
4651
11.3k
    } else {
4652
1.14k
      features->cur_frame_force_integer_mv = seq_params->force_integer_mv;
4653
1.14k
    }
4654
19.6k
  } else {
4655
19.6k
    features->cur_frame_force_integer_mv = 0;
4656
19.6k
  }
4657
4658
32.1k
  int frame_size_override_flag = 0;
4659
32.1k
  features->allow_intrabc = 0;
4660
32.1k
  features->primary_ref_frame = PRIMARY_REF_NONE;
4661
4662
32.1k
  if (!seq_params->reduced_still_picture_hdr) {
4663
19.3k
    if (seq_params->frame_id_numbers_present_flag) {
4664
342
      int frame_id_length = seq_params->frame_id_length;
4665
342
      int diff_len = seq_params->delta_frame_id_length;
4666
342
      int prev_frame_id = 0;
4667
342
      int have_prev_frame_id =
4668
342
          !pbi->decoding_first_frame &&
4669
44
          !(current_frame->frame_type == KEY_FRAME && cm->show_frame);
4670
342
      if (have_prev_frame_id) {
4671
42
        prev_frame_id = cm->current_frame_id;
4672
42
      }
4673
342
      cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
4674
4675
342
      if (have_prev_frame_id) {
4676
42
        int diff_frame_id;
4677
42
        if (cm->current_frame_id > prev_frame_id) {
4678
32
          diff_frame_id = cm->current_frame_id - prev_frame_id;
4679
32
        } else {
4680
10
          diff_frame_id =
4681
10
              (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
4682
10
        }
4683
        /* Check current_frame_id for conformance */
4684
42
        if (prev_frame_id == cm->current_frame_id ||
4685
41
            diff_frame_id >= (1 << (frame_id_length - 1))) {
4686
2
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4687
2
                             "Invalid value of current_frame_id");
4688
2
        }
4689
42
      }
4690
      /* Check if some frames need to be marked as not valid for referencing */
4691
3.06k
      for (int i = 0; i < REF_FRAMES; i++) {
4692
2.72k
        if (cm->current_frame_id - (1 << diff_len) > 0) {
4693
1.63k
          if (cm->ref_frame_id[i] > cm->current_frame_id ||
4694
1.16k
              cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
4695
1.36k
            pbi->valid_for_referencing[i] = 0;
4696
1.63k
        } else {
4697
1.08k
          if (cm->ref_frame_id[i] > cm->current_frame_id &&
4698
549
              cm->ref_frame_id[i] < (1 << frame_id_length) +
4699
549
                                        cm->current_frame_id - (1 << diff_len))
4700
298
            pbi->valid_for_referencing[i] = 0;
4701
1.08k
        }
4702
2.72k
      }
4703
342
    }
4704
4705
19.3k
    frame_size_override_flag = frame_is_sframe(cm) ? 1 : aom_rb_read_bit(rb);
4706
4707
19.3k
    current_frame->order_hint = aom_rb_read_literal(
4708
19.3k
        rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
4709
4710
19.3k
    if (seq_params->order_hint_info.enable_order_hint)
4711
12.1k
      current_frame->frame_number = current_frame->order_hint;
4712
4713
19.3k
    if (!features->error_resilient_mode && !frame_is_intra_only(cm)) {
4714
12.0k
      features->primary_ref_frame = aom_rb_read_literal(rb, PRIMARY_REF_BITS);
4715
12.0k
    }
4716
19.3k
  }
4717
4718
32.1k
  if (seq_params->decoder_model_info_present_flag) {
4719
335
    pbi->buffer_removal_time_present = aom_rb_read_bit(rb);
4720
335
    if (pbi->buffer_removal_time_present) {
4721
187
      for (int op_num = 0;
4722
1.11k
           op_num < seq_params->operating_points_cnt_minus_1 + 1; op_num++) {
4723
928
        if (seq_params->op_params[op_num].decoder_model_param_present_flag) {
4724
240
          if (seq_params->operating_point_idc[op_num] == 0 ||
4725
233
              (((seq_params->operating_point_idc[op_num] >>
4726
233
                 cm->temporal_layer_id) &
4727
233
                0x1) &&
4728
167
               ((seq_params->operating_point_idc[op_num] >>
4729
167
                 (cm->spatial_layer_id + 8)) &
4730
167
                0x1))) {
4731
15
            cm->buffer_removal_times[op_num] = aom_rb_read_unsigned_literal(
4732
15
                rb, seq_params->decoder_model_info.buffer_removal_time_length);
4733
225
          } else {
4734
225
            cm->buffer_removal_times[op_num] = 0;
4735
225
          }
4736
688
        } else {
4737
688
          cm->buffer_removal_times[op_num] = 0;
4738
688
        }
4739
928
      }
4740
187
    }
4741
335
  }
4742
32.1k
  if (current_frame->frame_type == KEY_FRAME) {
4743
19.2k
    if (!cm->show_frame) {  // unshown keyframe (forward keyframe)
4744
3.56k
      current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
4745
15.6k
    } else {  // shown keyframe
4746
15.6k
      current_frame->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4747
15.6k
    }
4748
4749
153k
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4750
134k
      cm->remapped_ref_idx[i] = INVALID_IDX;
4751
134k
    }
4752
19.2k
    if (pbi->need_resync) {
4753
14.7k
      reset_ref_frame_map(cm);
4754
14.7k
      pbi->need_resync = 0;
4755
14.7k
    }
4756
19.2k
  } else {
4757
12.9k
    if (current_frame->frame_type == INTRA_ONLY_FRAME) {
4758
244
      current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
4759
244
      if (current_frame->refresh_frame_flags == 0xFF) {
4760
1
        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4761
1
                           "Intra only frames cannot have refresh flags 0xFF");
4762
1
      }
4763
244
      if (pbi->need_resync) {
4764
71
        reset_ref_frame_map(cm);
4765
71
        pbi->need_resync = 0;
4766
71
      }
4767
12.7k
    } else if (pbi->need_resync != 1) { /* Skip if need resync */
4768
12.6k
      current_frame->refresh_frame_flags =
4769
12.6k
          frame_is_sframe(cm) ? 0xFF : aom_rb_read_literal(rb, REF_FRAMES);
4770
12.6k
    }
4771
12.9k
  }
4772
4773
32.1k
  if (!frame_is_intra_only(cm) || current_frame->refresh_frame_flags != 0xFF) {
4774
    // Read all ref frame order hints if error_resilient_mode == 1
4775
16.1k
    if (features->error_resilient_mode &&
4776
1.24k
        seq_params->order_hint_info.enable_order_hint) {
4777
9.19k
      for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
4778
        // Read order hint from bit stream
4779
8.16k
        unsigned int order_hint = aom_rb_read_literal(
4780
8.16k
            rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
4781
        // Get buffer
4782
8.16k
        RefCntBuffer *buf = cm->ref_frame_map[ref_idx];
4783
8.16k
        if (buf == NULL || order_hint != buf->order_hint) {
4784
6.05k
          if (buf != NULL) {
4785
2.79k
            lock_buffer_pool(pool);
4786
2.79k
            decrease_ref_count(buf, pool);
4787
2.79k
            unlock_buffer_pool(pool);
4788
2.79k
            cm->ref_frame_map[ref_idx] = NULL;
4789
2.79k
          }
4790
          // If no corresponding buffer exists, allocate a new buffer with all
4791
          // pixels set to neutral grey.
4792
6.05k
          int buf_idx = get_free_fb(cm);
4793
6.05k
          if (buf_idx == INVALID_IDX) {
4794
0
            aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
4795
0
                               "Unable to find free frame buffer");
4796
0
          }
4797
6.05k
          buf = &frame_bufs[buf_idx];
4798
6.05k
          lock_buffer_pool(pool);
4799
6.05k
#if CONFIG_SIZE_LIMIT
4800
6.05k
          if (seq_params->max_frame_width > DECODE_WIDTH_LIMIT ||
4801
6.05k
              seq_params->max_frame_height > DECODE_HEIGHT_LIMIT) {
4802
0
            decrease_ref_count(buf, pool);
4803
0
            unlock_buffer_pool(pool);
4804
0
            aom_internal_error(
4805
0
                cm->error, AOM_CODEC_CORRUPT_FRAME,
4806
0
                "Dimensions of %dx%d beyond allowed size of %dx%d.",
4807
0
                seq_params->max_frame_width, seq_params->max_frame_height,
4808
0
                DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
4809
0
          }
4810
6.05k
#endif
4811
6.05k
          if (aom_realloc_frame_buffer(
4812
6.05k
                  &buf->buf, seq_params->max_frame_width,
4813
6.05k
                  seq_params->max_frame_height, seq_params->subsampling_x,
4814
6.05k
                  seq_params->subsampling_y, seq_params->use_highbitdepth,
4815
6.05k
                  AOM_BORDER_IN_PIXELS, features->byte_alignment,
4816
6.05k
                  &buf->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv, false,
4817
6.05k
                  0)) {
4818
0
            decrease_ref_count(buf, pool);
4819
0
            unlock_buffer_pool(pool);
4820
0
            aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
4821
0
                               "Failed to allocate frame buffer");
4822
0
          }
4823
6.05k
          unlock_buffer_pool(pool);
4824
          // According to the specification, valid bitstreams are required to
4825
          // never use missing reference frames so the filling process for
4826
          // missing frames is not normatively defined and RefValid for missing
4827
          // frames is set to 0.
4828
4829
          // To make libaom more robust when the bitstream has been corrupted
4830
          // by the loss of some frames of data, this code adds a neutral grey
4831
          // buffer in place of missing frames, i.e.
4832
          //
4833
6.05k
          set_planes_to_neutral_grey(seq_params, &buf->buf, 0);
4834
          //
4835
          // and allows the frames to be used for referencing, i.e.
4836
          //
4837
6.05k
          pbi->valid_for_referencing[ref_idx] = 1;
4838
          //
4839
          // Please note such behavior is not normative and other decoders may
4840
          // use a different approach.
4841
6.05k
          cm->ref_frame_map[ref_idx] = buf;
4842
6.05k
          buf->order_hint = order_hint;
4843
6.05k
        }
4844
8.16k
      }
4845
1.02k
    }
4846
16.1k
  }
4847
4848
32.1k
  if (current_frame->frame_type == KEY_FRAME) {
4849
19.2k
    setup_frame_size(cm, frame_size_override_flag, rb);
4850
4851
19.2k
    if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
4852
9.79k
      features->allow_intrabc = aom_rb_read_bit(rb);
4853
19.2k
    features->allow_ref_frame_mvs = 0;
4854
19.2k
    cm->prev_frame = NULL;
4855
19.2k
  } else {
4856
12.9k
    features->allow_ref_frame_mvs = 0;
4857
4858
12.9k
    if (current_frame->frame_type == INTRA_ONLY_FRAME) {
4859
243
      cm->cur_frame->film_grain_params_present =
4860
243
          seq_params->film_grain_params_present;
4861
243
      setup_frame_size(cm, frame_size_override_flag, rb);
4862
243
      if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
4863
33
        features->allow_intrabc = aom_rb_read_bit(rb);
4864
4865
12.7k
    } else if (pbi->need_resync != 1) { /* Skip if need resync */
4866
12.6k
      int frame_refs_short_signaling = 0;
4867
      // Frame refs short signaling is off when error resilient mode is on.
4868
12.6k
      if (seq_params->order_hint_info.enable_order_hint)
4869
7.05k
        frame_refs_short_signaling = aom_rb_read_bit(rb);
4870
4871
12.6k
      if (frame_refs_short_signaling) {
4872
        // == LAST_FRAME ==
4873
1.93k
        const int lst_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
4874
1.93k
        const RefCntBuffer *const lst_buf = cm->ref_frame_map[lst_ref];
4875
4876
        // == GOLDEN_FRAME ==
4877
1.93k
        const int gld_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
4878
1.93k
        const RefCntBuffer *const gld_buf = cm->ref_frame_map[gld_ref];
4879
4880
        // Most of the time, streams start with a keyframe. In that case,
4881
        // ref_frame_map will have been filled in at that point and will not
4882
        // contain any NULLs. However, streams are explicitly allowed to start
4883
        // with an intra-only frame, so long as they don't then signal a
4884
        // reference to a slot that hasn't been set yet. That's what we are
4885
        // checking here.
4886
1.93k
        if (lst_buf == NULL)
4887
1
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4888
1
                             "Inter frame requests nonexistent reference");
4889
1.93k
        if (gld_buf == NULL)
4890
1
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4891
1
                             "Inter frame requests nonexistent reference");
4892
4893
1.93k
        av1_set_frame_refs(cm, cm->remapped_ref_idx, lst_ref, gld_ref);
4894
1.93k
      }
4895
4896
101k
      for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4897
88.5k
        int ref = 0;
4898
88.5k
        if (!frame_refs_short_signaling) {
4899
75.1k
          ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
4900
4901
          // Most of the time, streams start with a keyframe. In that case,
4902
          // ref_frame_map will have been filled in at that point and will not
4903
          // contain any NULLs. However, streams are explicitly allowed to start
4904
          // with an intra-only frame, so long as they don't then signal a
4905
          // reference to a slot that hasn't been set yet. That's what we are
4906
          // checking here.
4907
75.1k
          if (cm->ref_frame_map[ref] == NULL)
4908
4
            aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4909
4
                               "Inter frame requests nonexistent reference");
4910
75.1k
          cm->remapped_ref_idx[i] = ref;
4911
75.1k
        } else {
4912
13.3k
          ref = cm->remapped_ref_idx[i];
4913
13.3k
        }
4914
        // Check valid for referencing
4915
88.5k
        if (pbi->valid_for_referencing[ref] == 0)
4916
2
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4917
2
                             "Reference frame not valid for referencing");
4918
4919
88.5k
        cm->ref_frame_sign_bias[LAST_FRAME + i] = 0;
4920
4921
88.5k
        if (seq_params->frame_id_numbers_present_flag) {
4922
20
          int frame_id_length = seq_params->frame_id_length;
4923
20
          int diff_len = seq_params->delta_frame_id_length;
4924
20
          int delta_frame_id_minus_1 = aom_rb_read_literal(rb, diff_len);
4925
20
          int ref_frame_id =
4926
20
              ((cm->current_frame_id - (delta_frame_id_minus_1 + 1) +
4927
20
                (1 << frame_id_length)) %
4928
20
               (1 << frame_id_length));
4929
          // Compare values derived from delta_frame_id_minus_1 and
4930
          // refresh_frame_flags.
4931
20
          if (ref_frame_id != cm->ref_frame_id[ref])
4932
7
            aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4933
7
                               "Reference buffer frame ID mismatch");
4934
20
        }
4935
88.5k
      }
4936
4937
12.6k
      if (!features->error_resilient_mode && frame_size_override_flag) {
4938
10.4k
        setup_frame_size_with_refs(cm, rb);
4939
10.4k
      } else {
4940
2.21k
        setup_frame_size(cm, frame_size_override_flag, rb);
4941
2.21k
      }
4942
4943
12.6k
      if (features->cur_frame_force_integer_mv) {
4944
911
        features->allow_high_precision_mv = 0;
4945
11.7k
      } else {
4946
11.7k
        features->allow_high_precision_mv = aom_rb_read_bit(rb);
4947
11.7k
      }
4948
12.6k
      features->interp_filter = read_frame_interp_filter(rb);
4949
12.6k
      features->switchable_motion_mode = aom_rb_read_bit(rb);
4950
12.6k
    }
4951
4952
12.9k
    cm->prev_frame = get_primary_ref_frame_buf(cm);
4953
12.9k
    if (features->primary_ref_frame != PRIMARY_REF_NONE &&
4954
11.6k
        get_primary_ref_frame_buf(cm) == NULL) {
4955
1
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4956
1
                         "Reference frame containing this frame's initial "
4957
1
                         "frame context is unavailable.");
4958
1
    }
4959
4960
12.9k
    if (!(current_frame->frame_type == INTRA_ONLY_FRAME) &&
4961
12.6k
        pbi->need_resync != 1) {
4962
12.6k
      if (frame_might_allow_ref_frame_mvs(cm))
4963
5.11k
        features->allow_ref_frame_mvs = aom_rb_read_bit(rb);
4964
7.52k
      else
4965
7.52k
        features->allow_ref_frame_mvs = 0;
4966
4967
101k
      for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4968
88.3k
        const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
4969
88.3k
        struct scale_factors *const ref_scale_factors =
4970
88.3k
            get_ref_scale_factors(cm, i);
4971
88.3k
        av1_setup_scale_factors_for_frame(
4972
88.3k
            ref_scale_factors, ref_buf->buf.y_crop_width,
4973
88.3k
            ref_buf->buf.y_crop_height, cm->width, cm->height);
4974
88.3k
        if ((!av1_is_valid_scale(ref_scale_factors)))
4975
11
          aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4976
11
                             "Reference frame has invalid dimensions");
4977
88.3k
      }
4978
12.6k
    }
4979
12.9k
  }
4980
4981
32.1k
  av1_setup_frame_buf_refs(cm);
4982
4983
32.1k
  av1_setup_frame_sign_bias(cm);
4984
4985
32.1k
  cm->cur_frame->frame_type = current_frame->frame_type;
4986
4987
32.1k
  update_ref_frame_id(pbi);
4988
4989
32.1k
  const int might_bwd_adapt = !(seq_params->reduced_still_picture_hdr) &&
4990
19.2k
                              !(features->disable_cdf_update);
4991
32.1k
  if (might_bwd_adapt) {
4992
12.1k
    features->refresh_frame_context = aom_rb_read_bit(rb)
4993
12.1k
                                          ? REFRESH_FRAME_CONTEXT_DISABLED
4994
12.1k
                                          : REFRESH_FRAME_CONTEXT_BACKWARD;
4995
20.0k
  } else {
4996
20.0k
    features->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
4997
20.0k
  }
4998
4999
32.1k
  cm->cur_frame->buf.bit_depth = seq_params->bit_depth;
5000
32.1k
  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5001
32.1k
  cm->cur_frame->buf.transfer_characteristics =
5002
32.1k
      seq_params->transfer_characteristics;
5003
32.1k
  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5004
32.1k
  cm->cur_frame->buf.monochrome = seq_params->monochrome;
5005
32.1k
  cm->cur_frame->buf.chroma_sample_position =
5006
32.1k
      seq_params->chroma_sample_position;
5007
32.1k
  cm->cur_frame->buf.color_range = seq_params->color_range;
5008
32.1k
  cm->cur_frame->buf.render_width = cm->render_width;
5009
32.1k
  cm->cur_frame->buf.render_height = cm->render_height;
5010
5011
32.1k
  if (pbi->need_resync) {
5012
1
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5013
1
                       "Keyframe / intra-only frame required to reset decoder"
5014
1
                       " state");
5015
1
  }
5016
5017
32.1k
  if (features->allow_intrabc) {
5018
    // Set parameters corresponding to no filtering.
5019
3.09k
    struct loopfilter *lf = &cm->lf;
5020
3.09k
    lf->filter_level[0] = 0;
5021
3.09k
    lf->filter_level[1] = 0;
5022
3.09k
    cm->cdef_info.cdef_bits = 0;
5023
3.09k
    cm->cdef_info.cdef_strengths[0] = 0;
5024
3.09k
    cm->cdef_info.nb_cdef_strengths = 1;
5025
3.09k
    cm->cdef_info.cdef_uv_strengths[0] = 0;
5026
3.09k
    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5027
3.09k
    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5028
3.09k
    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5029
3.09k
  }
5030
5031
32.1k
  read_tile_info(pbi, rb);
5032
32.1k
  if (!av1_is_min_tile_width_satisfied(cm)) {
5033
5
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5034
5
                       "Minimum tile width requirement not satisfied");
5035
5
  }
5036
5037
32.1k
  CommonQuantParams *const quant_params = &cm->quant_params;
5038
32.1k
  setup_quantization(quant_params, av1_num_planes(cm),
5039
32.1k
                     cm->seq_params->separate_uv_delta_q, rb);
5040
32.1k
  xd->bd = (int)seq_params->bit_depth;
5041
5042
32.1k
  CommonContexts *const above_contexts = &cm->above_contexts;
5043
32.1k
  if (above_contexts->num_planes < av1_num_planes(cm) ||
5044
15.9k
      above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
5045
16.4k
      above_contexts->num_tile_rows < cm->tiles.rows) {
5046
16.4k
    av1_free_above_context_buffers(above_contexts);
5047
16.4k
    if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
5048
16.4k
                                        cm->mi_params.mi_cols,
5049
16.4k
                                        av1_num_planes(cm))) {
5050
0
      aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
5051
0
                         "Failed to allocate context buffers");
5052
0
    }
5053
16.4k
  }
5054
5055
32.1k
  if (features->primary_ref_frame == PRIMARY_REF_NONE) {
5056
20.4k
    av1_setup_past_independence(cm);
5057
20.4k
  }
5058
5059
32.1k
  setup_segmentation(cm, rb);
5060
5061
32.1k
  cm->delta_q_info.delta_q_res = 1;
5062
32.1k
  cm->delta_q_info.delta_lf_res = 1;
5063
32.1k
  cm->delta_q_info.delta_lf_present_flag = 0;
5064
32.1k
  cm->delta_q_info.delta_lf_multi = 0;
5065
32.1k
  cm->delta_q_info.delta_q_present_flag =
5066
32.1k
      quant_params->base_qindex > 0 ? aom_rb_read_bit(rb) : 0;
5067
32.1k
  if (cm->delta_q_info.delta_q_present_flag) {
5068
8.89k
    xd->current_base_qindex = quant_params->base_qindex;
5069
8.89k
    cm->delta_q_info.delta_q_res = 1 << aom_rb_read_literal(rb, 2);
5070
8.89k
    if (!features->allow_intrabc)
5071
8.48k
      cm->delta_q_info.delta_lf_present_flag = aom_rb_read_bit(rb);
5072
8.89k
    if (cm->delta_q_info.delta_lf_present_flag) {
5073
6.16k
      cm->delta_q_info.delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
5074
6.16k
      cm->delta_q_info.delta_lf_multi = aom_rb_read_bit(rb);
5075
6.16k
      av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
5076
6.16k
    }
5077
8.89k
  }
5078
5079
32.1k
  xd->cur_frame_force_integer_mv = features->cur_frame_force_integer_mv;
5080
5081
288k
  for (int i = 0; i < MAX_SEGMENTS; ++i) {
5082
256k
    const int qindex = av1_get_qindex(&cm->seg, i, quant_params->base_qindex);
5083
256k
    xd->lossless[i] =
5084
256k
        qindex == 0 && quant_params->y_dc_delta_q == 0 &&
5085
42.0k
        quant_params->u_dc_delta_q == 0 && quant_params->u_ac_delta_q == 0 &&
5086
39.4k
        quant_params->v_dc_delta_q == 0 && quant_params->v_ac_delta_q == 0;
5087
256k
    xd->qindex[i] = qindex;
5088
256k
  }
5089
32.1k
  features->coded_lossless = is_coded_lossless(cm, xd);
5090
32.1k
  features->all_lossless = features->coded_lossless && !av1_superres_scaled(cm);
5091
32.1k
  setup_segmentation_dequant(cm, xd);
5092
32.1k
  if (features->coded_lossless) {
5093
4.71k
    cm->lf.filter_level[0] = 0;
5094
4.71k
    cm->lf.filter_level[1] = 0;
5095
4.71k
  }
5096
32.1k
  if (features->coded_lossless || !seq_params->enable_cdef) {
5097
25.2k
    cm->cdef_info.cdef_bits = 0;
5098
25.2k
    cm->cdef_info.cdef_strengths[0] = 0;
5099
25.2k
    cm->cdef_info.cdef_uv_strengths[0] = 0;
5100
25.2k
  }
5101
32.1k
  if (features->all_lossless || !seq_params->enable_restoration) {
5102
23.3k
    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5103
23.3k
    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5104
23.3k
    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5105
23.3k
  }
5106
32.1k
  setup_loopfilter(cm, rb);
5107
5108
32.1k
  if (!features->coded_lossless && seq_params->enable_cdef) {
5109
6.75k
    setup_cdef(cm, rb);
5110
6.75k
  }
5111
32.1k
  if (!features->all_lossless && seq_params->enable_restoration) {
5112
8.64k
    decode_restoration_mode(cm, rb);
5113
8.64k
  }
5114
5115
32.1k
  features->tx_mode = read_tx_mode(rb, features->coded_lossless);
5116
32.1k
  current_frame->reference_mode = read_frame_reference_mode(cm, rb);
5117
5118
32.1k
  av1_setup_skip_mode_allowed(cm);
5119
32.1k
  current_frame->skip_mode_info.skip_mode_flag =
5120
32.1k
      current_frame->skip_mode_info.skip_mode_allowed ? aom_rb_read_bit(rb) : 0;
5121
5122
32.1k
  if (frame_might_allow_warped_motion(cm))
5123
8.10k
    features->allow_warped_motion = aom_rb_read_bit(rb);
5124
24.0k
  else
5125
24.0k
    features->allow_warped_motion = 0;
5126
5127
32.1k
  features->reduced_tx_set_used = aom_rb_read_bit(rb);
5128
5129
32.1k
  if (features->allow_ref_frame_mvs && !frame_might_allow_ref_frame_mvs(cm)) {
5130
0
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5131
0
                       "Frame wrongly requests reference frame MVs");
5132
0
  }
5133
5134
32.1k
  if (!frame_is_intra_only(cm)) read_global_motion(cm, rb);
5135
5136
32.1k
  cm->cur_frame->film_grain_params_present =
5137
32.1k
      seq_params->film_grain_params_present;
5138
32.1k
  read_film_grain(cm, rb);
5139
5140
32.1k
#if EXT_TILE_DEBUG
5141
32.1k
  if (pbi->ext_tile_debug && cm->tiles.large_scale) {
5142
0
    read_ext_tile_info(pbi, rb);
5143
0
    av1_set_single_tile_decoding_mode(cm);
5144
0
  }
5145
32.1k
#endif  // EXT_TILE_DEBUG
5146
32.1k
  return 0;
5147
32.4k
}
5148
5149
struct aom_read_bit_buffer *av1_init_read_bit_buffer(
5150
    AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
5151
61.1k
    const uint8_t *data_end) {
5152
61.1k
  rb->bit_offset = 0;
5153
61.1k
  rb->error_handler = error_handler;
5154
61.1k
  rb->error_handler_data = &pbi->common;
5155
61.1k
  rb->bit_buffer = data;
5156
61.1k
  rb->bit_buffer_end = data_end;
5157
61.1k
  return rb;
5158
61.1k
}
5159
5160
38.7k
BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
5161
38.7k
  int profile = aom_rb_read_literal(rb, PROFILE_BITS);
5162
38.7k
  return (BITSTREAM_PROFILE)profile;
5163
38.7k
}
5164
5165
4.19k
static inline void superres_post_decode(AV1Decoder *pbi) {
5166
4.19k
  AV1_COMMON *const cm = &pbi->common;
5167
4.19k
  BufferPool *const pool = cm->buffer_pool;
5168
5169
4.19k
  if (!av1_superres_scaled(cm)) return;
5170
4.19k
  assert(!cm->features.all_lossless);
5171
5172
2.24k
  av1_superres_upscale(cm, pool, 0);
5173
2.24k
}
5174
5175
uint32_t av1_decode_frame_headers_and_setup(AV1Decoder *pbi,
5176
                                            struct aom_read_bit_buffer *rb,
5177
32.4k
                                            int trailing_bits_present) {
5178
32.4k
  AV1_COMMON *const cm = &pbi->common;
5179
32.4k
  const int num_planes = av1_num_planes(cm);
5180
32.4k
  MACROBLOCKD *const xd = &pbi->dcb.xd;
5181
5182
#if CONFIG_BITSTREAM_DEBUG
5183
  if (cm->seq_params->order_hint_info.enable_order_hint) {
5184
    aom_bitstream_queue_set_frame_read(cm->current_frame.order_hint * 2 +
5185
                                       cm->show_frame);
5186
  } else {
5187
    // This is currently used in RTC encoding. cm->show_frame is always 1.
5188
    assert(cm->show_frame);
5189
    aom_bitstream_queue_set_frame_read(cm->current_frame.frame_number);
5190
  }
5191
#endif
5192
#if CONFIG_MISMATCH_DEBUG
5193
  mismatch_move_frame_idx_r();
5194
#endif
5195
5196
259k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
5197
227k
    cm->global_motion[i] = default_warp_params;
5198
227k
    cm->cur_frame->global_motion[i] = default_warp_params;
5199
227k
  }
5200
32.4k
  xd->global_motion = cm->global_motion;
5201
5202
32.4k
  read_uncompressed_header(pbi, rb);
5203
5204
32.4k
  if (trailing_bits_present) av1_check_trailing_bits(pbi, rb);
5205
5206
32.4k
  if (!cm->tiles.single_tile_decoding &&
5207
32.1k
      (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
5208
0
    pbi->dec_tile_row = -1;
5209
0
    pbi->dec_tile_col = -1;
5210
0
  }
5211
5212
32.4k
  const uint32_t uncomp_hdr_size =
5213
32.4k
      (uint32_t)aom_rb_bytes_read(rb);  // Size of the uncompressed header
5214
32.4k
  YV12_BUFFER_CONFIG *new_fb = &cm->cur_frame->buf;
5215
32.4k
  xd->cur_buf = new_fb;
5216
32.4k
  if (av1_allow_intrabc(cm)) {
5217
3.06k
    av1_setup_scale_factors_for_frame(
5218
3.06k
        &cm->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
5219
3.06k
        xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height);
5220
3.06k
  }
5221
5222
  // Showing a frame directly.
5223
32.4k
  if (cm->show_existing_frame) {
5224
265
    if (pbi->reset_decoder_state) {
5225
      // Use the default frame context values.
5226
39
      *cm->fc = *cm->default_frame_context;
5227
39
      if (!cm->fc->initialized)
5228
0
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5229
0
                           "Uninitialized entropy context.");
5230
39
    }
5231
265
    return uncomp_hdr_size;
5232
265
  }
5233
5234
32.1k
  cm->mi_params.setup_mi(&cm->mi_params);
5235
5236
32.1k
  av1_calculate_ref_frame_side(cm);
5237
32.1k
  if (cm->features.allow_ref_frame_mvs) av1_setup_motion_field(cm);
5238
5239
32.1k
  av1_setup_block_planes(xd, cm->seq_params->subsampling_x,
5240
32.1k
                         cm->seq_params->subsampling_y, num_planes);
5241
32.1k
  if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
5242
    // use the default frame context values
5243
20.2k
    *cm->fc = *cm->default_frame_context;
5244
20.2k
  } else {
5245
11.9k
    *cm->fc = get_primary_ref_frame_buf(cm)->frame_context;
5246
11.9k
  }
5247
32.1k
  if (!cm->fc->initialized)
5248
14
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5249
14
                       "Uninitialized entropy context.");
5250
5251
32.1k
  pbi->dcb.corrupted = 0;
5252
32.1k
  return uncomp_hdr_size;
5253
32.4k
}
5254
5255
// Once-per-frame initialization
5256
31.5k
static inline void setup_frame_info(AV1Decoder *pbi) {
5257
31.5k
  AV1_COMMON *const cm = &pbi->common;
5258
5259
31.5k
  if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5260
28.8k
      cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5261
27.1k
      cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
5262
4.79k
    av1_alloc_restoration_buffers(cm, /*is_sgr_enabled =*/true);
5263
18.6k
    for (int p = 0; p < av1_num_planes(cm); p++) {
5264
13.8k
      av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0);
5265
13.8k
    }
5266
4.79k
  }
5267
5268
31.5k
  const int use_highbd = cm->seq_params->use_highbitdepth;
5269
31.5k
  const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
5270
31.5k
  if (pbi->td.mc_buf_size != buf_size) {
5271
15.4k
    av1_free_mc_tmp_buf(&pbi->td);
5272
15.4k
    allocate_mc_tmp_buf(cm, &pbi->td, buf_size, use_highbd);
5273
15.4k
  }
5274
31.5k
}
5275
5276
void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,
5277
                                    const uint8_t *data_end,
5278
                                    const uint8_t **p_data_end, int start_tile,
5279
31.5k
                                    int end_tile, int initialize_flag) {
5280
31.5k
  AV1_COMMON *const cm = &pbi->common;
5281
31.5k
  CommonTileParams *const tiles = &cm->tiles;
5282
31.5k
  MACROBLOCKD *const xd = &pbi->dcb.xd;
5283
31.5k
  const int tile_count_tg = end_tile - start_tile + 1;
5284
5285
31.5k
  xd->error_info = cm->error;
5286
31.5k
  if (initialize_flag) setup_frame_info(pbi);
5287
31.5k
  const int num_planes = av1_num_planes(cm);
5288
5289
31.5k
  if (pbi->max_threads > 1 && !(tiles->large_scale && !pbi->ext_tile_debug) &&
5290
26.4k
      pbi->row_mt)
5291
26.4k
    *p_data_end =
5292
26.4k
        decode_tiles_row_mt(pbi, data, data_end, start_tile, end_tile);
5293
5.09k
  else if (pbi->max_threads > 1 && tile_count_tg > 1 &&
5294
0
           !(tiles->large_scale && !pbi->ext_tile_debug))
5295
0
    *p_data_end = decode_tiles_mt(pbi, data, data_end, start_tile, end_tile);
5296
5.09k
  else
5297
5.09k
    *p_data_end = decode_tiles(pbi, data, data_end, start_tile, end_tile);
5298
5299
  // If the bit stream is monochrome, set the U and V buffers to a constant.
5300
31.5k
  if (num_planes < 3) {
5301
7.79k
    set_planes_to_neutral_grey(cm->seq_params, xd->cur_buf, 1);
5302
7.79k
  }
5303
5304
31.5k
  if (end_tile != tiles->rows * tiles->cols - 1) {
5305
0
    return;
5306
0
  }
5307
5308
31.5k
  av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
5309
31.5k
                         pbi->num_workers, 1);
5310
31.5k
  av1_alloc_cdef_sync(cm, &pbi->cdef_sync, pbi->num_workers);
5311
5312
31.5k
  if (!cm->features.allow_intrabc && !tiles->single_tile_decoding) {
5313
18.9k
    if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
5314
13.0k
      av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, &pbi->dcb.xd, 0,
5315
13.0k
                               num_planes, 0, pbi->tile_workers,
5316
13.0k
                               pbi->num_workers, &pbi->lf_row_sync, 0);
5317
13.0k
    }
5318
5319
18.9k
    const int do_cdef =
5320
18.9k
        !pbi->skip_loop_filter && !cm->features.coded_lossless &&
5321
15.7k
        (cm->cdef_info.cdef_bits || cm->cdef_info.cdef_strengths[0] ||
5322
12.6k
         cm->cdef_info.cdef_uv_strengths[0]);
5323
18.9k
    const int do_superres = av1_superres_scaled(cm);
5324
18.9k
    const int optimized_loop_restoration = !do_cdef && !do_superres;
5325
18.9k
    const int do_loop_restoration =
5326
18.9k
        cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5327
17.2k
        cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5328
15.9k
        cm->rst_info[2].frame_restoration_type != RESTORE_NONE;
5329
    // Frame border extension is not required in the decoder
5330
    // as it happens in extend_mc_border().
5331
18.9k
    int do_extend_border_mt = 0;
5332
18.9k
    if (!optimized_loop_restoration) {
5333
4.19k
      if (do_loop_restoration)
5334
2.94k
        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
5335
2.94k
                                                 cm, 0);
5336
5337
4.19k
      if (do_cdef) {
5338
3.46k
        if (pbi->num_workers > 1) {
5339
3.41k
          av1_cdef_frame_mt(cm, &pbi->dcb.xd, pbi->cdef_worker,
5340
3.41k
                            pbi->tile_workers, &pbi->cdef_sync,
5341
3.41k
                            pbi->num_workers, av1_cdef_init_fb_row_mt,
5342
3.41k
                            do_extend_border_mt);
5343
3.41k
        } else {
5344
51
          av1_cdef_frame(&pbi->common.cur_frame->buf, cm, &pbi->dcb.xd,
5345
51
                         av1_cdef_init_fb_row);
5346
51
        }
5347
3.46k
      }
5348
5349
4.19k
      superres_post_decode(pbi);
5350
5351
4.19k
      if (do_loop_restoration) {
5352
2.94k
        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
5353
2.94k
                                                 cm, 1);
5354
2.94k
        if (pbi->num_workers > 1) {
5355
2.94k
          av1_loop_restoration_filter_frame_mt(
5356
2.94k
              (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
5357
2.94k
              pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
5358
2.94k
              &pbi->lr_ctxt, do_extend_border_mt);
5359
2.94k
        } else {
5360
0
          av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
5361
0
                                            cm, optimized_loop_restoration,
5362
0
                                            &pbi->lr_ctxt);
5363
0
        }
5364
2.94k
      }
5365
14.7k
    } else {
5366
      // In no cdef and no superres case. Provide an optimized version of
5367
      // loop_restoration_filter.
5368
14.7k
      if (do_loop_restoration) {
5369
291
        if (pbi->num_workers > 1) {
5370
281
          av1_loop_restoration_filter_frame_mt(
5371
281
              (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
5372
281
              pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
5373
281
              &pbi->lr_ctxt, do_extend_border_mt);
5374
281
        } else {
5375
10
          av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
5376
10
                                            cm, optimized_loop_restoration,
5377
10
                                            &pbi->lr_ctxt);
5378
10
        }
5379
291
      }
5380
14.7k
    }
5381
18.9k
  }
5382
5383
31.5k
  if (!pbi->dcb.corrupted) {
5384
20.7k
    if (cm->features.refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
5385
6.55k
      assert(pbi->context_update_tile_id < pbi->allocated_tiles);
5386
6.55k
      *cm->fc = pbi->tile_data[pbi->context_update_tile_id].tctx;
5387
6.55k
      av1_reset_cdf_symbol_counters(cm->fc);
5388
6.55k
    }
5389
20.7k
  } else {
5390
10.7k
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5391
10.7k
                       "Decode failed. Frame data is corrupted.");
5392
10.7k
  }
5393
5394
#if CONFIG_INSPECTION
5395
  if (pbi->inspect_cb != NULL) {
5396
    (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5397
  }
5398
#endif
5399
5400
  // Non frame parallel update frame context here.
5401
31.5k
  if (!tiles->large_scale) {
5402
20.7k
    cm->cur_frame->frame_context = *cm->fc;
5403
20.7k
  }
5404
5405
31.5k
  if (cm->show_frame && !cm->seq_params->order_hint_info.enable_order_hint) {
5406
10.0k
    ++cm->current_frame.frame_number;
5407
10.0k
  }
5408
31.5k
}