Coverage Report

Created: 2025-06-22 08:04

/src/aom/av1/decoder/decodeframe.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
12
#include <assert.h>
13
#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
60.9k
#define AOM_MIN_THREADS_PER_TILE 1
79
94.9k
#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
97.0k
  (((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2) * \
85
97.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
35.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
35.4k
  int bits_before_alignment = 8 - rb->bit_offset % 8;
92
35.4k
  int trailing = aom_rb_read_literal(rb, bits_before_alignment);
93
35.4k
  if (trailing != (1 << (bits_before_alignment - 1))) {
94
111
    pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
95
111
    return -1;
96
111
  }
97
35.3k
  return 0;
98
35.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
27.7k
    int only_chroma) {
104
27.7k
  if (seq_params->use_highbitdepth) {
105
14.8k
    const int val = 1 << (seq_params->bit_depth - 1);
106
52.3k
    for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
107
37.4k
      const int is_uv = plane > 0;
108
37.4k
      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
37.4k
      if (buf->crop_heights[is_uv] > 0) {
112
37.4k
        aom_memset16(base, val, buf->crop_widths[is_uv]);
113
855k
        for (int row_idx = 1; row_idx < buf->crop_heights[is_uv]; row_idx++) {
114
817k
          memcpy(&base[row_idx * buf->strides[is_uv]], base,
115
817k
                 sizeof(*base) * buf->crop_widths[is_uv]);
116
817k
        }
117
37.4k
      }
118
37.4k
    }
119
14.8k
  } else {
120
44.7k
    for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
121
31.8k
      const int is_uv = plane > 0;
122
736k
      for (int row_idx = 0; row_idx < buf->crop_heights[is_uv]; row_idx++) {
123
704k
        memset(&buf->buffers[plane][row_idx * buf->strides[is_uv]], 1 << 7,
124
704k
               buf->crop_widths[is_uv]);
125
704k
      }
126
31.8k
    }
127
12.8k
  }
128
27.7k
}
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
63.9k
static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
136
63.9k
  return len != 0 && len <= (size_t)(end - start);
137
63.9k
}
138
139
static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb,
140
54.7k
                            int coded_lossless) {
141
54.7k
  if (coded_lossless) return ONLY_4X4;
142
46.7k
  return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
143
54.7k
}
144
145
static REFERENCE_MODE read_frame_reference_mode(
146
54.7k
    const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
147
54.7k
  if (frame_is_intra_only(cm)) {
148
34.7k
    return SINGLE_REFERENCE;
149
34.7k
  } else {
150
20.0k
    return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
151
20.0k
  }
152
54.7k
}
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
4.18M
                                           int stride, int reduced_tx_set) {
158
4.18M
  tran_low_t *const dqcoeff = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
159
4.18M
  eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
160
4.18M
  uint16_t scan_line = eob_data->max_scan_line;
161
4.18M
  uint16_t eob = eob_data->eob;
162
4.18M
  av1_inverse_transform_block(&dcb->xd, dqcoeff, plane, tx_type, tx_size, dst,
163
4.18M
                              stride, eob, reduced_tx_set);
164
4.18M
  memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
165
4.18M
}
166
167
static inline void read_coeffs_tx_intra_block(
168
    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
169
31.6M
    const int plane, const int row, const int col, const TX_SIZE tx_size) {
170
31.6M
  MB_MODE_INFO *mbmi = dcb->xd.mi[0];
171
31.6M
  if (!mbmi->skip_txfm) {
172
#if TXCOEFF_TIMER
173
    struct aom_usec_timer timer;
174
    aom_usec_timer_start(&timer);
175
#endif
176
16.3M
    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
16.3M
  }
184
31.6M
}
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
47.5M
                                     const TX_SIZE tx_size) {
191
47.5M
  (void)cm;
192
47.5M
  (void)dcb;
193
47.5M
  (void)r;
194
47.5M
  (void)plane;
195
47.5M
  (void)row;
196
47.5M
  (void)col;
197
47.5M
  (void)tx_size;
198
47.5M
}
199
200
static inline void predict_inter_block_void(AV1_COMMON *const cm,
201
                                            DecoderCodingBlock *dcb,
202
226k
                                            BLOCK_SIZE bsize) {
203
226k
  (void)cm;
204
226k
  (void)dcb;
205
226k
  (void)bsize;
206
226k
}
207
208
static inline void cfl_store_inter_block_void(AV1_COMMON *const cm,
209
226k
                                              MACROBLOCKD *const xd) {
210
226k
  (void)cm;
211
226k
  (void)xd;
212
226k
}
213
214
static inline void predict_and_reconstruct_intra_block(
215
    const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
216
25.9M
    const int plane, const int row, const int col, const TX_SIZE tx_size) {
217
25.9M
  (void)r;
218
25.9M
  MACROBLOCKD *const xd = &dcb->xd;
219
25.9M
  MB_MODE_INFO *mbmi = xd->mi[0];
220
25.9M
  PLANE_TYPE plane_type = get_plane_type(plane);
221
222
25.9M
  av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
223
224
25.9M
  if (!mbmi->skip_txfm) {
225
13.4M
    eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
226
13.4M
    if (eob_data->eob) {
227
4.00M
      const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
228
      // tx_type was read out in av1_read_coeffs_txb.
229
4.00M
      const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
230
4.00M
                                              reduced_tx_set_used);
231
4.00M
      struct macroblockd_plane *const pd = &xd->plane[plane];
232
4.00M
      uint8_t *dst = &pd->dst.buf[(row * pd->dst.stride + col) << MI_SIZE_LOG2];
233
4.00M
      inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
234
4.00M
                              reduced_tx_set_used);
235
4.00M
    }
236
13.4M
  }
237
25.9M
  if (plane == AOM_PLANE_Y && store_cfl_required(cm, xd)) {
238
1.62M
    cfl_store_tx(xd, row, col, tx_size, mbmi->bsize);
239
1.62M
  }
240
25.9M
}
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
173k
    const TX_SIZE tx_size) {
246
173k
  (void)r;
247
173k
  MACROBLOCKD *const xd = &dcb->xd;
248
173k
  PLANE_TYPE plane_type = get_plane_type(plane);
249
173k
  const struct macroblockd_plane *const pd = &xd->plane[plane];
250
173k
  const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
251
  // tx_type was read out in av1_read_coeffs_txb.
252
173k
  const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col,
253
173k
                                          tx_size, reduced_tx_set_used);
254
255
173k
  uint8_t *dst =
256
173k
      &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
257
173k
  inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
258
173k
                          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
173k
}
273
274
static inline void set_cb_buffer_offsets(DecoderCodingBlock *dcb,
275
52.3M
                                         TX_SIZE tx_size, int plane) {
276
52.3M
  dcb->cb_offset[plane] += tx_size_wide[tx_size] * tx_size_high[tx_size];
277
52.3M
  dcb->txb_offset[plane] =
278
52.3M
      dcb->cb_offset[plane] / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
279
52.3M
}
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
415k
                                         TX_SIZE tx_size, int *eob_total) {
287
415k
  DecoderCodingBlock *const dcb = &td->dcb;
288
415k
  MACROBLOCKD *const xd = &dcb->xd;
289
415k
  const struct macroblockd_plane *const pd = &xd->plane[plane];
290
415k
  const TX_SIZE plane_tx_size =
291
415k
      plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
292
221k
                                    pd->subsampling_y)
293
415k
            : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
294
193k
                                                         blk_col)];
295
  // Scale to match transform block unit.
296
415k
  const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
297
415k
  const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
298
299
415k
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
300
301
415k
  if (tx_size == plane_tx_size || plane) {
302
406k
    td->read_coeffs_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
303
406k
                                         tx_size);
304
305
406k
    td->inverse_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
306
406k
                                     tx_size);
307
406k
    eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
308
406k
    *eob_total += eob_data->eob;
309
406k
    set_cb_buffer_offsets(dcb, tx_size, plane);
310
406k
  } else {
311
9.12k
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
312
9.12k
    assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
313
9.12k
    assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
314
9.12k
    const int bsw = tx_size_wide_unit[sub_txs];
315
9.12k
    const int bsh = tx_size_high_unit[sub_txs];
316
9.12k
    const int sub_step = bsw * bsh;
317
9.12k
    const int row_end =
318
9.12k
        AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
319
9.12k
    const int col_end =
320
9.12k
        AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
321
322
9.12k
    assert(bsw > 0 && bsh > 0);
323
324
25.3k
    for (int row = 0; row < row_end; row += bsh) {
325
16.2k
      const int offsetr = blk_row + row;
326
46.3k
      for (int col = 0; col < col_end; col += bsw) {
327
30.1k
        const int offsetc = blk_col + col;
328
329
30.1k
        decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize, offsetr,
330
30.1k
                              offsetc, block, sub_txs, eob_total);
331
30.1k
        block += sub_step;
332
30.1k
      }
333
16.2k
    }
334
9.12k
  }
335
415k
}
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
6.39M
                               int bh, int x_mis, int y_mis) {
340
6.39M
  const int num_planes = av1_num_planes(cm);
341
6.39M
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
342
6.39M
  const TileInfo *const tile = &xd->tile;
343
344
6.39M
  set_mi_offsets(mi_params, xd, mi_row, mi_col);
345
6.39M
  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
6.39M
  assert(x_mis && y_mis);
352
21.3M
  for (int x = 1; x < x_mis; ++x) xd->mi[x] = xd->mi[0];
353
6.39M
  int idx = mi_params->mi_stride;
354
23.9M
  for (int y = 1; y < y_mis; ++y) {
355
17.5M
    memcpy(&xd->mi[idx], &xd->mi[0], x_mis * sizeof(xd->mi[0]));
356
17.5M
    idx += mi_params->mi_stride;
357
17.5M
  }
358
359
6.39M
  set_plane_n4(xd, bw, bh, num_planes);
360
6.39M
  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
6.39M
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
365
6.39M
                 mi_params->mi_cols);
366
367
6.39M
  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
368
6.39M
                       num_planes);
369
6.39M
}
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
6.39M
                                     BLOCK_SIZE bsize) {
376
6.39M
  AV1_COMMON *const cm = &pbi->common;
377
6.39M
  const SequenceHeader *const seq_params = cm->seq_params;
378
6.39M
  const int bw = mi_size_wide[bsize];
379
6.39M
  const int bh = mi_size_high[bsize];
380
6.39M
  const int x_mis = AOMMIN(bw, cm->mi_params.mi_cols - mi_col);
381
6.39M
  const int y_mis = AOMMIN(bh, cm->mi_params.mi_rows - mi_row);
382
6.39M
  MACROBLOCKD *const xd = &dcb->xd;
383
384
#if CONFIG_ACCOUNTING
385
  aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
386
#endif
387
6.39M
  set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
388
6.39M
  xd->mi[0]->partition = partition;
389
6.39M
  av1_read_mode_info(pbi, dcb, r, x_mis, y_mis);
390
6.39M
  if (bsize >= BLOCK_8X8 &&
391
6.39M
      (seq_params->subsampling_x || seq_params->subsampling_y)) {
392
1.74M
    const BLOCK_SIZE uv_subsize =
393
1.74M
        av1_ss_size_lookup[bsize][seq_params->subsampling_x]
394
1.74M
                          [seq_params->subsampling_y];
395
1.74M
    if (uv_subsize == BLOCK_INVALID)
396
0
      aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
397
0
                         "Invalid block size.");
398
1.74M
  }
399
6.39M
}
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
83.0k
                                          int h) {
413
  // Get a pointer to the start of the real data for this row.
414
83.0k
  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
415
83.0k
  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
416
83.0k
  const uint16_t *ref_row = src - x - y * src_stride;
417
418
83.0k
  if (y >= h)
419
6.00k
    ref_row += (h - 1) * src_stride;
420
77.0k
  else if (y > 0)
421
25.8k
    ref_row += y * src_stride;
422
423
1.20M
  do {
424
1.20M
    int right = 0, copy;
425
1.20M
    int left = x < 0 ? -x : 0;
426
427
1.20M
    if (left > b_w) left = b_w;
428
429
1.20M
    if (x + b_w > w) right = x + b_w - w;
430
431
1.20M
    if (right > b_w) right = b_w;
432
433
1.20M
    copy = b_w - left - right;
434
435
1.20M
    if (left) aom_memset16(dst, ref_row[0], left);
436
437
1.20M
    if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
438
439
1.20M
    if (right) aom_memset16(dst + left + copy, ref_row[w - 1], right);
440
441
1.20M
    dst += dst_stride;
442
1.20M
    ++y;
443
444
1.20M
    if (y > 0 && y < h) ref_row += src_stride;
445
1.20M
  } while (--b_h);
446
83.0k
}
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
72.8k
                                   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
72.8k
  const uint8_t *ref_row = src - x - y * src_stride;
454
455
72.8k
  if (y >= h)
456
7.43k
    ref_row += (h - 1) * src_stride;
457
65.3k
  else if (y > 0)
458
20.3k
    ref_row += y * src_stride;
459
460
1.02M
  do {
461
1.02M
    int right = 0, copy;
462
1.02M
    int left = x < 0 ? -x : 0;
463
464
1.02M
    if (left > b_w) left = b_w;
465
466
1.02M
    if (x + b_w > w) right = x + b_w - w;
467
468
1.02M
    if (right > b_w) right = b_w;
469
470
1.02M
    copy = b_w - left - right;
471
472
1.02M
    if (left) memset(dst, ref_row[0], left);
473
474
1.02M
    if (copy) memcpy(dst + left, ref_row + x + left, copy);
475
476
1.02M
    if (right) memset(dst + left + copy, ref_row[w - 1], right);
477
478
1.02M
    dst += dst_stride;
479
1.02M
    ++y;
480
481
1.02M
    if (y > 0 && y < h) ref_row += src_stride;
482
1.02M
  } while (--b_h);
483
72.8k
}
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
513k
    int do_warp, int is_intrabc, int *x_pad, int *y_pad) {
489
513k
  const int is_scaled = av1_is_scaled(sf);
490
  // Get reference width and height.
491
513k
  int frame_width = pre_buf->width;
492
513k
  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
513k
  if ((!is_intrabc) && (!do_warp) &&
497
513k
      (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
498
159k
       (frame_height & 0x7))) {
499
158k
    if (subpel_x_mv || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
500
158k
      block->x0 -= AOM_INTERP_EXTEND - 1;
501
158k
      block->x1 += AOM_INTERP_EXTEND;
502
158k
      *x_pad = 1;
503
158k
    }
504
505
158k
    if (subpel_y_mv || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
506
158k
      block->y0 -= AOM_INTERP_EXTEND - 1;
507
158k
      block->y1 += AOM_INTERP_EXTEND;
508
158k
      *y_pad = 1;
509
158k
    }
510
511
    // Skip border extension if block is inside the frame.
512
158k
    if (block->x0 < 0 || block->x1 > frame_width - 1 || block->y0 < 0 ||
513
158k
        block->y1 > frame_height - 1) {
514
155k
      return 1;
515
155k
    }
516
158k
  }
517
357k
  return 0;
518
513k
}
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
513k
                                    int *src_stride) {
527
513k
  int x_pad = 0, y_pad = 0;
528
513k
  if (update_extend_mc_border_params(sf, pre_buf, scaled_mv, &block,
529
513k
                                     subpel_x_mv, subpel_y_mv, do_warp,
530
513k
                                     is_intrabc, &x_pad, &y_pad)) {
531
    // Get reference block pointer.
532
155k
    const uint8_t *const buf_ptr =
533
155k
        pre_buf->buf0 + block.y0 * pre_buf->stride + block.x0;
534
155k
    int buf_stride = pre_buf->stride;
535
155k
    const int b_w = block.x1 - block.x0;
536
155k
    const int b_h = block.y1 - block.y0;
537
538
155k
#if CONFIG_AV1_HIGHBITDEPTH
539
    // Extend the border.
540
155k
    if (highbd) {
541
83.0k
      highbd_build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0,
542
83.0k
                             block.y0, b_w, b_h, pre_buf->width,
543
83.0k
                             pre_buf->height);
544
83.0k
    } else {
545
72.8k
      build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
546
72.8k
                      b_h, pre_buf->width, pre_buf->height);
547
72.8k
    }
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
155k
    *src_stride = b_w;
554
155k
    *pre = mc_buf + y_pad * (AOM_INTERP_EXTEND - 1) * b_w +
555
155k
           x_pad * (AOM_INTERP_EXTEND - 1);
556
155k
  }
557
513k
}
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
513k
    MV32 *scaled_mv, int *subpel_x_mv, int *subpel_y_mv) {
564
513k
  const struct scale_factors *sf = inter_pred_params->scale_factors;
565
513k
  struct buf_2d *pre_buf = &inter_pred_params->ref_frame_buf;
566
513k
  const int bw = inter_pred_params->block_width;
567
513k
  const int bh = inter_pred_params->block_height;
568
513k
  const int is_scaled = av1_is_scaled(sf);
569
513k
  if (is_scaled) {
570
28.6k
    int ssx = inter_pred_params->subsampling_x;
571
28.6k
    int ssy = inter_pred_params->subsampling_y;
572
28.6k
    int orig_pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
573
28.6k
    orig_pos_y += src_mv->row * (1 << (1 - ssy));
574
28.6k
    int orig_pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
575
28.6k
    orig_pos_x += src_mv->col * (1 << (1 - ssx));
576
28.6k
    int pos_y = av1_scaled_y(orig_pos_y, sf);
577
28.6k
    int pos_x = av1_scaled_x(orig_pos_x, sf);
578
28.6k
    pos_x += SCALE_EXTRA_OFF;
579
28.6k
    pos_y += SCALE_EXTRA_OFF;
580
581
28.6k
    const int top = -AOM_LEFT_TOP_MARGIN_SCALED(ssy);
582
28.6k
    const int left = -AOM_LEFT_TOP_MARGIN_SCALED(ssx);
583
28.6k
    const int bottom = (pre_buf->height + AOM_INTERP_EXTEND)
584
28.6k
                       << SCALE_SUBPEL_BITS;
585
28.6k
    const int right = (pre_buf->width + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
586
28.6k
    pos_y = clamp(pos_y, top, bottom);
587
28.6k
    pos_x = clamp(pos_x, left, right);
588
589
28.6k
    subpel_params->subpel_x = pos_x & SCALE_SUBPEL_MASK;
590
28.6k
    subpel_params->subpel_y = pos_y & SCALE_SUBPEL_MASK;
591
28.6k
    subpel_params->xs = sf->x_step_q4;
592
28.6k
    subpel_params->ys = sf->y_step_q4;
593
594
    // Get reference block top left coordinate.
595
28.6k
    block->x0 = pos_x >> SCALE_SUBPEL_BITS;
596
28.6k
    block->y0 = pos_y >> SCALE_SUBPEL_BITS;
597
598
    // Get reference block bottom right coordinate.
599
28.6k
    block->x1 =
600
28.6k
        ((pos_x + (bw - 1) * subpel_params->xs) >> SCALE_SUBPEL_BITS) + 1;
601
28.6k
    block->y1 =
602
28.6k
        ((pos_y + (bh - 1) * subpel_params->ys) >> SCALE_SUBPEL_BITS) + 1;
603
604
28.6k
    MV temp_mv;
605
28.6k
    temp_mv = clamp_mv_to_umv_border_sb(xd, src_mv, bw, bh,
606
28.6k
                                        inter_pred_params->subsampling_x,
607
28.6k
                                        inter_pred_params->subsampling_y);
608
28.6k
    *scaled_mv = av1_scale_mv(&temp_mv, mi_x, mi_y, sf);
609
28.6k
    scaled_mv->row += SCALE_EXTRA_OFF;
610
28.6k
    scaled_mv->col += SCALE_EXTRA_OFF;
611
612
28.6k
    *subpel_x_mv = scaled_mv->col & SCALE_SUBPEL_MASK;
613
28.6k
    *subpel_y_mv = scaled_mv->row & SCALE_SUBPEL_MASK;
614
484k
  } else {
615
    // Get block position in current frame.
616
484k
    int pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
617
484k
    int pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
618
619
484k
    const MV mv_q4 = clamp_mv_to_umv_border_sb(
620
484k
        xd, src_mv, bw, bh, inter_pred_params->subsampling_x,
621
484k
        inter_pred_params->subsampling_y);
622
484k
    subpel_params->xs = subpel_params->ys = SCALE_SUBPEL_SHIFTS;
623
484k
    subpel_params->subpel_x = (mv_q4.col & SUBPEL_MASK) << SCALE_EXTRA_BITS;
624
484k
    subpel_params->subpel_y = (mv_q4.row & SUBPEL_MASK) << SCALE_EXTRA_BITS;
625
626
    // Get reference block top left coordinate.
627
484k
    pos_x += mv_q4.col;
628
484k
    pos_y += mv_q4.row;
629
484k
    block->x0 = pos_x >> SUBPEL_BITS;
630
484k
    block->y0 = pos_y >> SUBPEL_BITS;
631
632
    // Get reference block bottom right coordinate.
633
484k
    block->x1 = (pos_x >> SUBPEL_BITS) + (bw - 1) + 1;
634
484k
    block->y1 = (pos_y >> SUBPEL_BITS) + (bh - 1) + 1;
635
636
484k
    scaled_mv->row = mv_q4.row;
637
484k
    scaled_mv->col = mv_q4.col;
638
484k
    *subpel_x_mv = scaled_mv->col & SUBPEL_MASK;
639
484k
    *subpel_y_mv = scaled_mv->row & SUBPEL_MASK;
640
484k
  }
641
513k
  *pre = pre_buf->buf0 + block->y0 * pre_buf->stride + block->x0;
642
513k
  *src_stride = pre_buf->stride;
643
513k
}
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
513k
    uint8_t **pre, SubpelParams *subpel_params, int *src_stride) {
649
513k
  PadBlock block;
650
513k
  MV32 scaled_mv;
651
513k
  int subpel_x_mv, subpel_y_mv;
652
513k
  dec_calc_subpel_params(src_mv, inter_pred_params, xd, mi_x, mi_y, pre,
653
513k
                         subpel_params, src_stride, &block, &scaled_mv,
654
513k
                         &subpel_x_mv, &subpel_y_mv);
655
513k
  extend_mc_border(
656
513k
      inter_pred_params->scale_factors, &inter_pred_params->ref_frame_buf,
657
513k
      scaled_mv, block, subpel_x_mv, subpel_y_mv,
658
513k
      inter_pred_params->mode == WARP_PRED, inter_pred_params->is_intrabc,
659
513k
      inter_pred_params->use_hbd_buf, mc_buf[ref], pre, src_stride);
660
513k
}
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
480k
                                       int mi_x, int mi_y) {
671
480k
  build_inter_predictors(cm, &dcb->xd, plane, mi, build_for_obmc, bw, bh, mi_x,
672
480k
                         mi_y, dcb->mc_buf);
673
480k
}
674
675
static inline void dec_build_inter_predictor(const AV1_COMMON *cm,
676
                                             DecoderCodingBlock *dcb,
677
                                             int mi_row, int mi_col,
678
178k
                                             BLOCK_SIZE bsize) {
679
178k
  MACROBLOCKD *const xd = &dcb->xd;
680
178k
  const int num_planes = av1_num_planes(cm);
681
648k
  for (int plane = 0; plane < num_planes; ++plane) {
682
486k
    if (plane && !xd->is_chroma_ref) break;
683
470k
    const int mi_x = mi_col * MI_SIZE;
684
470k
    const int mi_y = mi_row * MI_SIZE;
685
470k
    dec_build_inter_predictors(cm, dcb, plane, xd->mi[0], 0,
686
470k
                               xd->plane[plane].width, xd->plane[plane].height,
687
470k
                               mi_x, mi_y);
688
470k
    if (is_interintra_pred(xd->mi[0])) {
689
4.55k
      BUFFER_SET ctx = { { xd->plane[0].dst.buf, xd->plane[1].dst.buf,
690
4.55k
                           xd->plane[2].dst.buf },
691
4.55k
                         { xd->plane[0].dst.stride, xd->plane[1].dst.stride,
692
4.55k
                           xd->plane[2].dst.stride } };
693
4.55k
      av1_build_interintra_predictor(cm, xd, xd->plane[plane].dst.buf,
694
4.55k
                                     xd->plane[plane].dst.stride, &ctx, plane,
695
4.55k
                                     bsize);
696
4.55k
    }
697
470k
  }
698
178k
}
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
1.50k
    int dir, MB_MODE_INFO *above_mbmi, void *fun_ctxt, const int num_planes) {
703
1.50k
  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
704
1.50k
  const int above_mi_col = xd->mi_col + rel_mi_col;
705
1.50k
  int mi_x, mi_y;
706
1.50k
  MB_MODE_INFO backup_mbmi = *above_mbmi;
707
708
1.50k
  (void)rel_mi_row;
709
1.50k
  (void)dir;
710
711
1.50k
  av1_setup_build_prediction_by_above_pred(xd, rel_mi_col, op_mi_size,
712
1.50k
                                           &backup_mbmi, ctxt, num_planes);
713
1.50k
  mi_x = above_mi_col << MI_SIZE_LOG2;
714
1.50k
  mi_y = xd->mi_row << MI_SIZE_LOG2;
715
716
1.50k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
717
718
5.06k
  for (int j = 0; j < num_planes; ++j) {
719
3.56k
    const struct macroblockd_plane *pd = &xd->plane[j];
720
3.56k
    int bw = (op_mi_size * MI_SIZE) >> pd->subsampling_x;
721
3.56k
    int bh = clamp(block_size_high[bsize] >> (pd->subsampling_y + 1), 4,
722
3.56k
                   block_size_high[BLOCK_64X64] >> (pd->subsampling_y + 1));
723
724
3.56k
    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 0)) continue;
725
3.20k
    dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
726
3.20k
                               &backup_mbmi, 1, bw, bh, mi_x, mi_y);
727
3.20k
  }
728
1.50k
}
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
3.36k
    int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
734
3.36k
  MACROBLOCKD *const xd = &dcb->xd;
735
3.36k
  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
1.40k
  const int this_height = xd->height * MI_SIZE;
741
1.40k
  const int pred_height = AOMMIN(this_height / 2, 32);
742
1.40k
  xd->mb_to_bottom_edge += GET_MV_SUBPEL(this_height - pred_height);
743
1.40k
  struct build_prediction_ctxt ctxt = {
744
1.40k
    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_right_edge, dcb
745
1.40k
  };
746
1.40k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
747
1.40k
  foreach_overlappable_nb_above(cm, xd,
748
1.40k
                                max_neighbor_obmc[mi_size_wide_log2[bsize]],
749
1.40k
                                dec_build_prediction_by_above_pred, &ctxt);
750
751
1.40k
  xd->mb_to_left_edge = -GET_MV_SUBPEL(xd->mi_col * MI_SIZE);
752
1.40k
  xd->mb_to_right_edge = ctxt.mb_to_far_edge;
753
1.40k
  xd->mb_to_bottom_edge -= GET_MV_SUBPEL(this_height - pred_height);
754
1.40k
}
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
2.16k
    int dir, MB_MODE_INFO *left_mbmi, void *fun_ctxt, const int num_planes) {
759
2.16k
  struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
760
2.16k
  const int left_mi_row = xd->mi_row + rel_mi_row;
761
2.16k
  int mi_x, mi_y;
762
2.16k
  MB_MODE_INFO backup_mbmi = *left_mbmi;
763
764
2.16k
  (void)rel_mi_col;
765
2.16k
  (void)dir;
766
767
2.16k
  av1_setup_build_prediction_by_left_pred(xd, rel_mi_row, op_mi_size,
768
2.16k
                                          &backup_mbmi, ctxt, num_planes);
769
2.16k
  mi_x = xd->mi_col << MI_SIZE_LOG2;
770
2.16k
  mi_y = left_mi_row << MI_SIZE_LOG2;
771
2.16k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
772
773
8.64k
  for (int j = 0; j < num_planes; ++j) {
774
6.48k
    const struct macroblockd_plane *pd = &xd->plane[j];
775
6.48k
    int bw = clamp(block_size_wide[bsize] >> (pd->subsampling_x + 1), 4,
776
6.48k
                   block_size_wide[BLOCK_64X64] >> (pd->subsampling_x + 1));
777
6.48k
    int bh = (op_mi_size << MI_SIZE_LOG2) >> pd->subsampling_y;
778
779
6.48k
    if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 1)) continue;
780
6.48k
    dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
781
6.48k
                               &backup_mbmi, 1, bw, bh, mi_x, mi_y);
782
6.48k
  }
783
2.16k
}
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
3.36k
    int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
789
3.36k
  MACROBLOCKD *const xd = &dcb->xd;
790
3.36k
  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
2.26k
  const int this_width = xd->width * MI_SIZE;
796
2.26k
  const int pred_width = AOMMIN(this_width / 2, 32);
797
2.26k
  xd->mb_to_right_edge += GET_MV_SUBPEL(this_width - pred_width);
798
799
2.26k
  struct build_prediction_ctxt ctxt = {
800
2.26k
    cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_bottom_edge, dcb
801
2.26k
  };
802
2.26k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
803
2.26k
  foreach_overlappable_nb_left(cm, xd,
804
2.26k
                               max_neighbor_obmc[mi_size_high_log2[bsize]],
805
2.26k
                               dec_build_prediction_by_left_pred, &ctxt);
806
807
2.26k
  xd->mb_to_top_edge = -GET_MV_SUBPEL(xd->mi_row * MI_SIZE);
808
2.26k
  xd->mb_to_right_edge -= GET_MV_SUBPEL(this_width - pred_width);
809
2.26k
  xd->mb_to_bottom_edge = ctxt.mb_to_far_edge;
810
2.26k
}
811
812
static inline void dec_build_obmc_inter_predictors_sb(const AV1_COMMON *cm,
813
3.36k
                                                      DecoderCodingBlock *dcb) {
814
3.36k
  const int num_planes = av1_num_planes(cm);
815
3.36k
  uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
816
3.36k
  int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
817
3.36k
  int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
818
3.36k
  int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
819
3.36k
  int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
820
3.36k
  int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
821
3.36k
  int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
822
823
3.36k
  MACROBLOCKD *const xd = &dcb->xd;
824
3.36k
  av1_setup_obmc_dst_bufs(xd, dst_buf1, dst_buf2);
825
826
3.36k
  dec_build_prediction_by_above_preds(cm, dcb, dst_buf1, dst_width1,
827
3.36k
                                      dst_height1, dst_stride1);
828
3.36k
  dec_build_prediction_by_left_preds(cm, dcb, dst_buf2, dst_width2, dst_height2,
829
3.36k
                                     dst_stride2);
830
3.36k
  const int mi_row = xd->mi_row;
831
3.36k
  const int mi_col = xd->mi_col;
832
3.36k
  av1_setup_dst_planes(xd->plane, xd->mi[0]->bsize, &cm->cur_frame->buf, mi_row,
833
3.36k
                       mi_col, 0, num_planes);
834
3.36k
  av1_build_obmc_inter_prediction(cm, xd, dst_buf1, dst_stride1, dst_buf2,
835
3.36k
                                  dst_stride2);
836
3.36k
}
837
838
static inline void cfl_store_inter_block(AV1_COMMON *const cm,
839
178k
                                         MACROBLOCKD *const xd) {
840
178k
  MB_MODE_INFO *mbmi = xd->mi[0];
841
178k
  if (store_cfl_required(cm, xd)) {
842
15.6k
    cfl_store_block(xd, mbmi->bsize, mbmi->tx_size);
843
15.6k
  }
844
178k
}
845
846
static inline void predict_inter_block(AV1_COMMON *const cm,
847
                                       DecoderCodingBlock *dcb,
848
178k
                                       BLOCK_SIZE bsize) {
849
178k
  MACROBLOCKD *const xd = &dcb->xd;
850
178k
  MB_MODE_INFO *mbmi = xd->mi[0];
851
178k
  const int num_planes = av1_num_planes(cm);
852
178k
  const int mi_row = xd->mi_row;
853
178k
  const int mi_col = xd->mi_col;
854
369k
  for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
855
191k
    const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
856
191k
    if (frame < LAST_FRAME) {
857
119k
      assert(is_intrabc_block(mbmi));
858
119k
      assert(frame == INTRA_FRAME);
859
119k
      assert(ref == 0);
860
119k
    } else {
861
71.9k
      const RefCntBuffer *ref_buf = get_ref_frame_buf(cm, frame);
862
71.9k
      const struct scale_factors *ref_scale_factors =
863
71.9k
          get_ref_scale_factors_const(cm, frame);
864
865
71.9k
      xd->block_ref_scale_factors[ref] = ref_scale_factors;
866
71.9k
      av1_setup_pre_planes(xd, ref, &ref_buf->buf, mi_row, mi_col,
867
71.9k
                           ref_scale_factors, num_planes);
868
71.9k
    }
869
191k
  }
870
871
178k
  dec_build_inter_predictor(cm, dcb, mi_row, mi_col, bsize);
872
178k
  if (mbmi->motion_mode == OBMC_CAUSAL) {
873
3.36k
    dec_build_obmc_inter_predictors_sb(cm, dcb);
874
3.36k
  }
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
178k
}
891
892
static inline void set_color_index_map_offset(MACROBLOCKD *const xd, int plane,
893
1.94M
                                              aom_reader *r) {
894
1.94M
  (void)r;
895
1.94M
  Av1ColorMapParam params;
896
1.94M
  const MB_MODE_INFO *const mbmi = xd->mi[0];
897
1.94M
  av1_get_block_dimensions(mbmi->bsize, plane, xd, &params.plane_width,
898
1.94M
                           &params.plane_height, NULL, NULL);
899
1.94M
  xd->color_index_map_offset[plane] += params.plane_width * params.plane_height;
900
1.94M
}
901
902
static inline void decode_token_recon_block(AV1Decoder *const pbi,
903
                                            ThreadData *const td, aom_reader *r,
904
10.3M
                                            BLOCK_SIZE bsize) {
905
10.3M
  AV1_COMMON *const cm = &pbi->common;
906
10.3M
  DecoderCodingBlock *const dcb = &td->dcb;
907
10.3M
  MACROBLOCKD *const xd = &dcb->xd;
908
10.3M
  const int num_planes = av1_num_planes(cm);
909
10.3M
  MB_MODE_INFO *mbmi = xd->mi[0];
910
911
10.3M
  if (!is_inter_block(mbmi)) {
912
9.97M
    int row, col;
913
9.97M
    assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
914
9.97M
                                         xd->plane[0].subsampling_y));
915
9.97M
    const int max_blocks_wide = max_block_wide(xd, bsize, 0);
916
9.97M
    const int max_blocks_high = max_block_high(xd, bsize, 0);
917
9.97M
    const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
918
9.97M
    int mu_blocks_wide = mi_size_wide[max_unit_bsize];
919
9.97M
    int mu_blocks_high = mi_size_high[max_unit_bsize];
920
9.97M
    mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
921
9.97M
    mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
922
923
20.0M
    for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
924
20.1M
      for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
925
38.7M
        for (int plane = 0; plane < num_planes; ++plane) {
926
29.0M
          if (plane && !xd->is_chroma_ref) break;
927
28.6M
          const struct macroblockd_plane *const pd = &xd->plane[plane];
928
28.6M
          const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
929
28.6M
          const int stepr = tx_size_high_unit[tx_size];
930
28.6M
          const int stepc = tx_size_wide_unit[tx_size];
931
932
28.6M
          const int unit_height = ROUND_POWER_OF_TWO(
933
28.6M
              AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
934
28.6M
          const int unit_width = ROUND_POWER_OF_TWO(
935
28.6M
              AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
936
937
62.2M
          for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
938
33.6M
               blk_row += stepr) {
939
85.1M
            for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
940
51.5M
                 blk_col += stepc) {
941
51.5M
              td->read_coeffs_tx_intra_block_visit(cm, dcb, r, plane, blk_row,
942
51.5M
                                                   blk_col, tx_size);
943
51.5M
              td->predict_and_recon_intra_block_visit(
944
51.5M
                  cm, dcb, r, plane, blk_row, blk_col, tx_size);
945
51.5M
              set_cb_buffer_offsets(dcb, tx_size, plane);
946
51.5M
            }
947
33.6M
          }
948
28.6M
        }
949
10.1M
      }
950
10.0M
    }
951
9.97M
  } else {
952
407k
    td->predict_inter_block_visit(cm, dcb, bsize);
953
    // Reconstruction
954
407k
    if (!mbmi->skip_txfm) {
955
115k
      int eobtotal = 0;
956
957
115k
      const int max_blocks_wide = max_block_wide(xd, bsize, 0);
958
115k
      const int max_blocks_high = max_block_high(xd, bsize, 0);
959
115k
      int row, col;
960
961
115k
      const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
962
115k
      assert(max_unit_bsize ==
963
115k
             get_plane_block_size(BLOCK_64X64, xd->plane[0].subsampling_x,
964
115k
                                  xd->plane[0].subsampling_y));
965
115k
      int mu_blocks_wide = mi_size_wide[max_unit_bsize];
966
115k
      int mu_blocks_high = mi_size_high[max_unit_bsize];
967
968
115k
      mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
969
115k
      mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
970
971
232k
      for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
972
236k
        for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
973
415k
          for (int plane = 0; plane < num_planes; ++plane) {
974
302k
            if (plane && !xd->is_chroma_ref) break;
975
296k
            const struct macroblockd_plane *const pd = &xd->plane[plane];
976
296k
            const int ss_x = pd->subsampling_x;
977
296k
            const int ss_y = pd->subsampling_y;
978
296k
            const BLOCK_SIZE plane_bsize =
979
296k
                get_plane_block_size(bsize, ss_x, ss_y);
980
296k
            const TX_SIZE max_tx_size =
981
296k
                get_vartx_max_txsize(xd, plane_bsize, plane);
982
296k
            const int bh_var_tx = tx_size_high_unit[max_tx_size];
983
296k
            const int bw_var_tx = tx_size_wide_unit[max_tx_size];
984
296k
            int block = 0;
985
296k
            int step =
986
296k
                tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
987
296k
            int blk_row, blk_col;
988
296k
            const int unit_height = ROUND_POWER_OF_TWO(
989
296k
                AOMMIN(mu_blocks_high + row, max_blocks_high), ss_y);
990
296k
            const int unit_width = ROUND_POWER_OF_TWO(
991
296k
                AOMMIN(mu_blocks_wide + col, max_blocks_wide), ss_x);
992
993
616k
            for (blk_row = row >> ss_y; blk_row < unit_height;
994
319k
                 blk_row += bh_var_tx) {
995
705k
              for (blk_col = col >> ss_x; blk_col < unit_width;
996
385k
                   blk_col += bw_var_tx) {
997
385k
                decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize,
998
385k
                                      blk_row, blk_col, block, max_tx_size,
999
385k
                                      &eobtotal);
1000
385k
                block += step;
1001
385k
              }
1002
319k
            }
1003
296k
          }
1004
119k
        }
1005
117k
      }
1006
115k
    }
1007
407k
    td->cfl_store_inter_block_visit(cm, xd);
1008
407k
  }
1009
1010
10.3M
  av1_visit_palette(pbi, xd, r, set_color_index_map_offset);
1011
10.3M
}
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
26.3k
                                     int blk_col) {
1017
66.8k
  for (int idy = 0; idy < tx_size_high_unit[split_size];
1018
40.5k
       idy += tx_size_high_unit[min_txs]) {
1019
101k
    for (int idx = 0; idx < tx_size_wide_unit[split_size];
1020
61.4k
         idx += tx_size_wide_unit[min_txs]) {
1021
61.4k
      const int index = (((blk_row + idy) >> tx_h_log2) << stride_log2) +
1022
61.4k
                        ((blk_col + idx) >> tx_w_log2);
1023
61.4k
      mbmi->inter_tx_size[index] = txs;
1024
61.4k
    }
1025
40.5k
  }
1026
26.3k
}
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
30.0k
                                      int blk_col, aom_reader *r) {
1031
30.0k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1032
30.0k
  int is_split = 0;
1033
30.0k
  const BLOCK_SIZE bsize = mbmi->bsize;
1034
30.0k
  const int max_blocks_high = max_block_high(xd, bsize, 0);
1035
30.0k
  const int max_blocks_wide = max_block_wide(xd, bsize, 0);
1036
30.0k
  if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
1037
29.5k
  assert(tx_size > TX_4X4);
1038
29.5k
  TX_SIZE txs = max_txsize_rect_lookup[bsize];
1039
59.1k
  for (int level = 0; level < MAX_VARTX_DEPTH - 1; ++level)
1040
29.5k
    txs = sub_tx_size_map[txs];
1041
29.5k
  const int tx_w_log2 = tx_size_wide_log2[txs] - MI_SIZE_LOG2;
1042
29.5k
  const int tx_h_log2 = tx_size_high_log2[txs] - MI_SIZE_LOG2;
1043
29.5k
  const int bw_log2 = mi_size_wide_log2[bsize];
1044
29.5k
  const int stride_log2 = bw_log2 - tx_w_log2;
1045
1046
29.5k
  if (depth == MAX_VARTX_DEPTH) {
1047
2.76k
    set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1048
2.76k
                      tx_size, blk_row, blk_col);
1049
2.76k
    mbmi->tx_size = tx_size;
1050
2.76k
    txfm_partition_update(xd->above_txfm_context + blk_col,
1051
2.76k
                          xd->left_txfm_context + blk_row, tx_size, tx_size);
1052
2.76k
    return;
1053
2.76k
  }
1054
1055
26.8k
  const int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
1056
26.8k
                                         xd->left_txfm_context + blk_row,
1057
26.8k
                                         mbmi->bsize, tx_size);
1058
26.8k
  is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
1059
1060
26.8k
  if (is_split) {
1061
6.73k
    const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
1062
6.73k
    const int bsw = tx_size_wide_unit[sub_txs];
1063
6.73k
    const int bsh = tx_size_high_unit[sub_txs];
1064
1065
6.73k
    if (sub_txs == TX_4X4) {
1066
3.49k
      set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1067
3.49k
                        sub_txs, blk_row, blk_col);
1068
3.49k
      mbmi->tx_size = sub_txs;
1069
3.49k
      txfm_partition_update(xd->above_txfm_context + blk_col,
1070
3.49k
                            xd->left_txfm_context + blk_row, sub_txs, tx_size);
1071
3.49k
      return;
1072
3.49k
    }
1073
1074
3.23k
    assert(bsw > 0 && bsh > 0);
1075
8.51k
    for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
1076
14.4k
      for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
1077
9.17k
        int offsetr = blk_row + row;
1078
9.17k
        int offsetc = blk_col + col;
1079
9.17k
        read_tx_size_vartx(xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, r);
1080
9.17k
      }
1081
5.27k
    }
1082
20.0k
  } else {
1083
20.0k
    set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1084
20.0k
                      tx_size, blk_row, blk_col);
1085
20.0k
    mbmi->tx_size = tx_size;
1086
20.0k
    txfm_partition_update(xd->above_txfm_context + blk_col,
1087
20.0k
                          xd->left_txfm_context + blk_row, tx_size, tx_size);
1088
20.0k
  }
1089
26.8k
}
1090
1091
static TX_SIZE read_selected_tx_size(const MACROBLOCKD *const xd,
1092
706k
                                     aom_reader *r) {
1093
  // TODO(debargha): Clean up the logic here. This function should only
1094
  // be called for intra.
1095
706k
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1096
706k
  const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
1097
706k
  const int max_depths = bsize_to_max_depth(bsize);
1098
706k
  const int ctx = get_tx_size_context(xd);
1099
706k
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1100
706k
  const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
1101
706k
                                    max_depths + 1, ACCT_STR);
1102
706k
  assert(depth >= 0 && depth <= max_depths);
1103
706k
  const TX_SIZE tx_size = depth_to_tx_size(depth, bsize);
1104
706k
  return tx_size;
1105
706k
}
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
6.37M
                            aom_reader *r) {
1110
6.37M
  const BLOCK_SIZE bsize = xd->mi[0]->bsize;
1111
6.37M
  if (xd->lossless[xd->mi[0]->segment_id]) return TX_4X4;
1112
1113
5.73M
  if (block_signals_txsize(bsize)) {
1114
5.25M
    if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
1115
706k
      const TX_SIZE coded_tx_size = read_selected_tx_size(xd, r);
1116
706k
      return coded_tx_size;
1117
4.54M
    } else {
1118
4.54M
      return tx_size_from_tx_mode(bsize, tx_mode);
1119
4.54M
    }
1120
5.25M
  } else {
1121
476k
    assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
1122
476k
    return max_txsize_rect_lookup[bsize];
1123
476k
  }
1124
5.73M
}
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
6.39M
                                      BLOCK_SIZE bsize) {
1131
6.39M
  DecoderCodingBlock *const dcb = &td->dcb;
1132
6.39M
  MACROBLOCKD *const xd = &dcb->xd;
1133
6.39M
  decode_mbmi_block(pbi, dcb, mi_row, mi_col, r, partition, bsize);
1134
1135
6.39M
  av1_visit_palette(pbi, xd, r, av1_decode_palette_tokens);
1136
1137
6.39M
  AV1_COMMON *cm = &pbi->common;
1138
6.39M
  const int num_planes = av1_num_planes(cm);
1139
6.39M
  MB_MODE_INFO *mbmi = xd->mi[0];
1140
6.39M
  int inter_block_tx = is_inter_block(mbmi) || is_intrabc_block(mbmi);
1141
6.39M
  if (cm->features.tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
1142
6.39M
      !mbmi->skip_txfm && inter_block_tx && !xd->lossless[mbmi->segment_id]) {
1143
20.5k
    const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
1144
20.5k
    const int bh = tx_size_high_unit[max_tx_size];
1145
20.5k
    const int bw = tx_size_wide_unit[max_tx_size];
1146
20.5k
    const int width = mi_size_wide[bsize];
1147
20.5k
    const int height = mi_size_high[bsize];
1148
1149
41.2k
    for (int idy = 0; idy < height; idy += bh)
1150
41.5k
      for (int idx = 0; idx < width; idx += bw)
1151
20.8k
        read_tx_size_vartx(xd, mbmi, max_tx_size, 0, idy, idx, r);
1152
6.37M
  } else {
1153
6.37M
    mbmi->tx_size = read_tx_size(xd, cm->features.tx_mode, inter_block_tx,
1154
6.37M
                                 !mbmi->skip_txfm, r);
1155
6.37M
    if (inter_block_tx)
1156
230k
      memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
1157
6.37M
    set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height,
1158
6.37M
                  mbmi->skip_txfm && is_inter_block(mbmi), xd);
1159
6.37M
  }
1160
1161
6.39M
  if (cm->delta_q_info.delta_q_present_flag) {
1162
17.0M
    for (int i = 0; i < MAX_SEGMENTS; i++) {
1163
15.1M
      const int current_qindex =
1164
15.1M
          av1_get_qindex(&cm->seg, i, xd->current_base_qindex);
1165
15.1M
      const CommonQuantParams *const quant_params = &cm->quant_params;
1166
59.8M
      for (int j = 0; j < num_planes; ++j) {
1167
44.7M
        const int dc_delta_q = j == 0 ? quant_params->y_dc_delta_q
1168
44.7M
                                      : (j == 1 ? quant_params->u_dc_delta_q
1169
29.6M
                                                : quant_params->v_dc_delta_q);
1170
44.7M
        const int ac_delta_q = j == 0 ? 0
1171
44.7M
                                      : (j == 1 ? quant_params->u_ac_delta_q
1172
29.6M
                                                : quant_params->v_ac_delta_q);
1173
44.7M
        xd->plane[j].seg_dequant_QTX[i][0] = av1_dc_quant_QTX(
1174
44.7M
            current_qindex, dc_delta_q, cm->seq_params->bit_depth);
1175
44.7M
        xd->plane[j].seg_dequant_QTX[i][1] = av1_ac_quant_QTX(
1176
44.7M
            current_qindex, ac_delta_q, cm->seq_params->bit_depth);
1177
44.7M
      }
1178
15.1M
    }
1179
1.89M
  }
1180
6.39M
  if (mbmi->skip_txfm) av1_reset_entropy_context(xd, bsize, num_planes);
1181
1182
6.39M
  decode_token_recon_block(pbi, td, r, bsize);
1183
6.39M
}
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
4.09M
                                                  BLOCK_SIZE bsize) {
1189
4.09M
  AV1_COMMON *const cm = &pbi->common;
1190
4.09M
  const CommonModeInfoParams *const mi_params = &cm->mi_params;
1191
4.09M
  DecoderCodingBlock *const dcb = &td->dcb;
1192
4.09M
  MACROBLOCKD *const xd = &dcb->xd;
1193
4.09M
  const int bw = mi_size_wide[bsize];
1194
4.09M
  const int bh = mi_size_high[bsize];
1195
4.09M
  const int num_planes = av1_num_planes(cm);
1196
1197
4.09M
  const int offset = mi_row * mi_params->mi_stride + mi_col;
1198
4.09M
  const TileInfo *const tile = &xd->tile;
1199
1200
4.09M
  xd->mi = mi_params->mi_grid_base + offset;
1201
4.09M
  xd->tx_type_map =
1202
4.09M
      &mi_params->tx_type_map[mi_row * mi_params->mi_stride + mi_col];
1203
4.09M
  xd->tx_type_map_stride = mi_params->mi_stride;
1204
1205
4.09M
  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
4.09M
  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
1210
4.09M
                 mi_params->mi_cols);
1211
1212
4.09M
  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
1213
4.09M
                       num_planes);
1214
4.09M
}
1215
1216
static inline void decode_block(AV1Decoder *const pbi, ThreadData *const td,
1217
                                int mi_row, int mi_col, aom_reader *r,
1218
4.09M
                                PARTITION_TYPE partition, BLOCK_SIZE bsize) {
1219
4.09M
  (void)partition;
1220
4.09M
  set_offsets_for_pred_and_recon(pbi, td, mi_row, mi_col, bsize);
1221
4.09M
  decode_token_recon_block(pbi, td, r, bsize);
1222
4.09M
}
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
4.56M
                                     BLOCK_SIZE bsize) {
1227
4.56M
  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
1228
4.56M
  FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1229
1230
4.56M
  if (!has_rows && !has_cols) return PARTITION_SPLIT;
1231
1232
4.49M
  assert(ctx >= 0);
1233
4.49M
  aom_cdf_prob *partition_cdf = ec_ctx->partition_cdf[ctx];
1234
4.49M
  if (has_rows && has_cols) {
1235
4.07M
    return (PARTITION_TYPE)aom_read_symbol(
1236
4.07M
        r, partition_cdf, partition_cdf_length(bsize), ACCT_STR);
1237
4.07M
  } else if (!has_rows && has_cols) {
1238
101k
    assert(bsize > BLOCK_8X8);
1239
101k
    aom_cdf_prob cdf[2];
1240
101k
    partition_gather_vert_alike(cdf, partition_cdf, bsize);
1241
101k
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
1242
101k
    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
1243
321k
  } else {
1244
321k
    assert(has_rows && !has_cols);
1245
321k
    assert(bsize > BLOCK_8X8);
1246
321k
    aom_cdf_prob cdf[2];
1247
321k
    partition_gather_horz_alike(cdf, partition_cdf, bsize);
1248
321k
    assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
1249
321k
    return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
1250
321k
  }
1251
4.49M
}
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
9.62M
                                    BLOCK_SIZE bsize, int parse_decode_flag) {
1257
9.62M
  assert(bsize < BLOCK_SIZES_ALL);
1258
9.62M
  AV1_COMMON *const cm = &pbi->common;
1259
9.62M
  DecoderCodingBlock *const dcb = &td->dcb;
1260
9.62M
  MACROBLOCKD *const xd = &dcb->xd;
1261
9.62M
  const int bw = mi_size_wide[bsize];
1262
9.62M
  const int hbs = bw >> 1;
1263
9.62M
  PARTITION_TYPE partition;
1264
9.62M
  BLOCK_SIZE subsize;
1265
9.62M
  const int quarter_step = bw / 4;
1266
9.62M
  BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
1267
9.62M
  const int has_rows = (mi_row + hbs) < cm->mi_params.mi_rows;
1268
9.62M
  const int has_cols = (mi_col + hbs) < cm->mi_params.mi_cols;
1269
1270
9.62M
  if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols)
1271
1.04M
    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
8.57M
  static const block_visitor_fn_t block_visit[4] = { NULL, parse_decode_block,
1278
8.57M
                                                     decode_block,
1279
8.57M
                                                     parse_decode_block };
1280
1281
8.57M
  if (parse_decode_flag & 1) {
1282
5.22M
    const int num_planes = av1_num_planes(cm);
1283
20.5M
    for (int plane = 0; plane < num_planes; ++plane) {
1284
15.2M
      int rcol0, rcol1, rrow0, rrow1;
1285
1286
      // Skip some unnecessary work if loop restoration is disabled
1287
15.2M
      if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
1288
1289
1.27M
      if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
1290
1.27M
                                             &rcol0, &rcol1, &rrow0, &rrow1)) {
1291
102k
        const int rstride = cm->rst_info[plane].horz_units;
1292
204k
        for (int rrow = rrow0; rrow < rrow1; ++rrow) {
1293
210k
          for (int rcol = rcol0; rcol < rcol1; ++rcol) {
1294
108k
            const int runit_idx = rcol + rrow * rstride;
1295
108k
            loop_restoration_read_sb_coeffs(cm, xd, reader, plane, runit_idx);
1296
108k
          }
1297
102k
        }
1298
102k
      }
1299
1.27M
    }
1300
1301
5.22M
    partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
1302
5.22M
                                    : read_partition(xd, mi_row, mi_col, reader,
1303
4.56M
                                                     has_rows, has_cols, bsize);
1304
5.22M
  } else {
1305
3.35M
    partition = get_partition(cm, mi_row, mi_col, bsize);
1306
3.35M
  }
1307
8.57M
  subsize = get_partition_subsize(bsize, partition);
1308
8.57M
  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
8.57M
  const struct macroblockd_plane *const pd_u = &xd->plane[1];
1320
8.57M
  if (get_plane_block_size(subsize, pd_u->subsampling_x, pd_u->subsampling_y) ==
1321
8.57M
      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
201
    xd->mi_row = mi_row;
1326
201
    aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1327
201
                       "Block size %dx%d invalid with this subsampling mode",
1328
201
                       block_size_wide[subsize], block_size_high[subsize]);
1329
201
  }
1330
1331
8.57M
#define DEC_BLOCK_STX_ARG
1332
10.4M
#define DEC_BLOCK_EPT_ARG partition,
1333
8.57M
#define DEC_BLOCK(db_r, db_c, db_subsize)                                  \
1334
10.4M
  block_visit[parse_decode_flag](pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), \
1335
10.4M
                                 reader, DEC_BLOCK_EPT_ARG(db_subsize))
1336
8.57M
#define DEC_PARTITION(db_r, db_c, db_subsize)                        \
1337
8.96M
  decode_partition(pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), reader, \
1338
8.96M
                   (db_subsize), parse_decode_flag)
1339
1340
8.57M
  switch (partition) {
1341
3.66M
    case PARTITION_NONE: DEC_BLOCK(mi_row, mi_col, subsize); break;
1342
880k
    case PARTITION_HORZ:
1343
880k
      DEC_BLOCK(mi_row, mi_col, subsize);
1344
880k
      if (has_rows) DEC_BLOCK(mi_row + hbs, mi_col, subsize);
1345
880k
      break;
1346
808k
    case PARTITION_VERT:
1347
808k
      DEC_BLOCK(mi_row, mi_col, subsize);
1348
808k
      if (has_cols) DEC_BLOCK(mi_row, mi_col + hbs, subsize);
1349
808k
      break;
1350
2.24M
    case PARTITION_SPLIT:
1351
2.24M
      DEC_PARTITION(mi_row, mi_col, subsize);
1352
2.24M
      DEC_PARTITION(mi_row, mi_col + hbs, subsize);
1353
2.24M
      DEC_PARTITION(mi_row + hbs, mi_col, subsize);
1354
2.24M
      DEC_PARTITION(mi_row + hbs, mi_col + hbs, subsize);
1355
2.24M
      break;
1356
136k
    case PARTITION_HORZ_A:
1357
136k
      DEC_BLOCK(mi_row, mi_col, bsize2);
1358
136k
      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
1359
136k
      DEC_BLOCK(mi_row + hbs, mi_col, subsize);
1360
136k
      break;
1361
128k
    case PARTITION_HORZ_B:
1362
128k
      DEC_BLOCK(mi_row, mi_col, subsize);
1363
128k
      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
1364
128k
      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
1365
128k
      break;
1366
101k
    case PARTITION_VERT_A:
1367
101k
      DEC_BLOCK(mi_row, mi_col, bsize2);
1368
101k
      DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
1369
101k
      DEC_BLOCK(mi_row, mi_col + hbs, subsize);
1370
101k
      break;
1371
108k
    case PARTITION_VERT_B:
1372
108k
      DEC_BLOCK(mi_row, mi_col, subsize);
1373
108k
      DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
1374
108k
      DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
1375
108k
      break;
1376
281k
    case PARTITION_HORZ_4:
1377
1.39M
      for (int i = 0; i < 4; ++i) {
1378
1.12M
        int this_mi_row = mi_row + i * quarter_step;
1379
1.12M
        if (i > 0 && this_mi_row >= cm->mi_params.mi_rows) break;
1380
1.11M
        DEC_BLOCK(this_mi_row, mi_col, subsize);
1381
1.11M
      }
1382
281k
      break;
1383
290k
    case PARTITION_VERT_4:
1384
1.41M
      for (int i = 0; i < 4; ++i) {
1385
1.16M
        int this_mi_col = mi_col + i * quarter_step;
1386
1.16M
        if (i > 0 && this_mi_col >= cm->mi_params.mi_cols) break;
1387
1.12M
        DEC_BLOCK(mi_row, this_mi_col, subsize);
1388
1.12M
      }
1389
290k
      break;
1390
0
    default: assert(0 && "Invalid partition type");
1391
8.57M
  }
1392
1393
8.59M
#undef DEC_PARTITION
1394
8.59M
#undef DEC_BLOCK
1395
8.59M
#undef DEC_BLOCK_EPT_ARG
1396
8.59M
#undef DEC_BLOCK_STX_ARG
1397
1398
8.59M
  if (parse_decode_flag & 1)
1399
5.22M
    update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
1400
8.59M
}
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
57.1k
    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
57.1k
  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
57.1k
  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
57.1k
  r->allow_update_cdf = allow_update_cdf;
1429
57.1k
}
1430
1431
static inline void setup_segmentation(AV1_COMMON *const cm,
1432
54.8k
                                      struct aom_read_bit_buffer *rb) {
1433
54.8k
  struct segmentation *const seg = &cm->seg;
1434
1435
54.8k
  seg->update_map = 0;
1436
54.8k
  seg->update_data = 0;
1437
54.8k
  seg->temporal_update = 0;
1438
1439
54.8k
  seg->enabled = aom_rb_read_bit(rb);
1440
54.8k
  if (!seg->enabled) {
1441
37.3k
    if (cm->cur_frame->seg_map) {
1442
37.3k
      memset(cm->cur_frame->seg_map, 0,
1443
37.3k
             (cm->cur_frame->mi_rows * cm->cur_frame->mi_cols));
1444
37.3k
    }
1445
1446
37.3k
    memset(seg, 0, sizeof(*seg));
1447
37.3k
    segfeatures_copy(&cm->cur_frame->seg, seg);
1448
37.3k
    return;
1449
37.3k
  }
1450
17.5k
  if (cm->seg.enabled && cm->prev_frame &&
1451
17.5k
      (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) &&
1452
17.5k
      (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) {
1453
6.26k
    cm->last_frame_seg_map = cm->prev_frame->seg_map;
1454
11.2k
  } else {
1455
11.2k
    cm->last_frame_seg_map = NULL;
1456
11.2k
  }
1457
  // Read update flags
1458
17.5k
  if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
1459
    // These frames can't use previous frames, so must signal map + features
1460
10.0k
    seg->update_map = 1;
1461
10.0k
    seg->temporal_update = 0;
1462
10.0k
    seg->update_data = 1;
1463
10.0k
  } else {
1464
7.52k
    seg->update_map = aom_rb_read_bit(rb);
1465
7.52k
    if (seg->update_map) {
1466
3.85k
      seg->temporal_update = aom_rb_read_bit(rb);
1467
3.85k
    } else {
1468
3.66k
      seg->temporal_update = 0;
1469
3.66k
    }
1470
7.52k
    seg->update_data = aom_rb_read_bit(rb);
1471
7.52k
  }
1472
1473
  // Segmentation data update
1474
17.5k
  if (seg->update_data) {
1475
14.1k
    av1_clearall_segfeatures(seg);
1476
1477
127k
    for (int i = 0; i < MAX_SEGMENTS; i++) {
1478
1.01M
      for (int j = 0; j < SEG_LVL_MAX; j++) {
1479
903k
        int data = 0;
1480
903k
        const int feature_enabled = aom_rb_read_bit(rb);
1481
903k
        if (feature_enabled) {
1482
372k
          av1_enable_segfeature(seg, i, j);
1483
1484
372k
          const int data_max = av1_seg_feature_data_max(j);
1485
372k
          const int data_min = -data_max;
1486
372k
          const int ubits = get_unsigned_bits(data_max);
1487
1488
372k
          if (av1_is_segfeature_signed(j)) {
1489
237k
            data = aom_rb_read_inv_signed_literal(rb, ubits);
1490
237k
          } else {
1491
134k
            data = aom_rb_read_literal(rb, ubits);
1492
134k
          }
1493
1494
372k
          data = clamp(data, data_min, data_max);
1495
372k
        }
1496
903k
        av1_set_segdata(seg, i, j, data);
1497
903k
      }
1498
112k
    }
1499
14.1k
    av1_calculate_segdata(seg);
1500
14.1k
  } else if (cm->prev_frame) {
1501
3.39k
    segfeatures_copy(seg, &cm->prev_frame->seg);
1502
3.39k
  }
1503
17.5k
  segfeatures_copy(&cm->cur_frame->seg, seg);
1504
17.5k
}
1505
1506
static inline void decode_restoration_mode(AV1_COMMON *cm,
1507
22.1k
                                           struct aom_read_bit_buffer *rb) {
1508
22.1k
  assert(!cm->features.all_lossless);
1509
22.1k
  const int num_planes = av1_num_planes(cm);
1510
22.1k
  if (cm->features.allow_intrabc) return;
1511
20.7k
  int all_none = 1, chroma_none = 1;
1512
73.2k
  for (int p = 0; p < num_planes; ++p) {
1513
52.5k
    RestorationInfo *rsi = &cm->rst_info[p];
1514
52.5k
    if (aom_rb_read_bit(rb)) {
1515
16.1k
      rsi->frame_restoration_type =
1516
16.1k
          aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
1517
36.3k
    } else {
1518
36.3k
      rsi->frame_restoration_type =
1519
36.3k
          aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
1520
36.3k
    }
1521
52.5k
    if (rsi->frame_restoration_type != RESTORE_NONE) {
1522
23.0k
      all_none = 0;
1523
23.0k
      chroma_none &= p == 0;
1524
23.0k
    }
1525
52.5k
  }
1526
20.7k
  if (!all_none) {
1527
13.4k
    assert(cm->seq_params->sb_size == BLOCK_64X64 ||
1528
13.4k
           cm->seq_params->sb_size == BLOCK_128X128);
1529
13.4k
    const int sb_size = cm->seq_params->sb_size == BLOCK_128X128 ? 128 : 64;
1530
1531
47.7k
    for (int p = 0; p < num_planes; ++p)
1532
34.3k
      cm->rst_info[p].restoration_unit_size = sb_size;
1533
1534
13.4k
    RestorationInfo *rsi = &cm->rst_info[0];
1535
1536
13.4k
    if (sb_size == 64) {
1537
6.94k
      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1538
6.94k
    }
1539
13.4k
    if (rsi->restoration_unit_size > 64) {
1540
10.8k
      rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1541
10.8k
    }
1542
13.4k
  } else {
1543
7.27k
    const int size = RESTORATION_UNITSIZE_MAX;
1544
25.4k
    for (int p = 0; p < num_planes; ++p)
1545
18.2k
      cm->rst_info[p].restoration_unit_size = size;
1546
7.27k
  }
1547
1548
20.7k
  if (num_planes > 1) {
1549
15.9k
    int s =
1550
15.9k
        AOMMIN(cm->seq_params->subsampling_x, cm->seq_params->subsampling_y);
1551
15.9k
    if (s && !chroma_none) {
1552
3.88k
      cm->rst_info[1].restoration_unit_size =
1553
3.88k
          cm->rst_info[0].restoration_unit_size >> (aom_rb_read_bit(rb) * s);
1554
12.0k
    } else {
1555
12.0k
      cm->rst_info[1].restoration_unit_size =
1556
12.0k
          cm->rst_info[0].restoration_unit_size;
1557
12.0k
    }
1558
15.9k
    cm->rst_info[2].restoration_unit_size =
1559
15.9k
        cm->rst_info[1].restoration_unit_size;
1560
15.9k
  }
1561
20.7k
}
1562
1563
static inline void read_wiener_filter(int wiener_win, WienerInfo *wiener_info,
1564
                                      WienerInfo *ref_wiener_info,
1565
30.1k
                                      aom_reader *rb) {
1566
30.1k
  memset(wiener_info->vfilter, 0, sizeof(wiener_info->vfilter));
1567
30.1k
  memset(wiener_info->hfilter, 0, sizeof(wiener_info->hfilter));
1568
1569
30.1k
  if (wiener_win == WIENER_WIN)
1570
10.2k
    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
1571
10.2k
        aom_read_primitive_refsubexpfin(
1572
10.2k
            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1573
10.2k
            WIENER_FILT_TAP0_SUBEXP_K,
1574
10.2k
            ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1575
10.2k
        WIENER_FILT_TAP0_MINV;
1576
19.9k
  else
1577
19.9k
    wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] = 0;
1578
30.1k
  wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
1579
30.1k
      aom_read_primitive_refsubexpfin(
1580
30.1k
          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1581
30.1k
          WIENER_FILT_TAP1_SUBEXP_K,
1582
30.1k
          ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
1583
30.1k
      WIENER_FILT_TAP1_MINV;
1584
30.1k
  wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
1585
30.1k
      aom_read_primitive_refsubexpfin(
1586
30.1k
          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1587
30.1k
          WIENER_FILT_TAP2_SUBEXP_K,
1588
30.1k
          ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
1589
30.1k
      WIENER_FILT_TAP2_MINV;
1590
  // The central element has an implicit +WIENER_FILT_STEP
1591
30.1k
  wiener_info->vfilter[WIENER_HALFWIN] =
1592
30.1k
      -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
1593
30.1k
            wiener_info->vfilter[2]);
1594
1595
30.1k
  if (wiener_win == WIENER_WIN)
1596
10.2k
    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
1597
10.2k
        aom_read_primitive_refsubexpfin(
1598
10.2k
            rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1599
10.2k
            WIENER_FILT_TAP0_SUBEXP_K,
1600
10.2k
            ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1601
10.2k
        WIENER_FILT_TAP0_MINV;
1602
19.9k
  else
1603
19.9k
    wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] = 0;
1604
30.1k
  wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
1605
30.1k
      aom_read_primitive_refsubexpfin(
1606
30.1k
          rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1607
30.1k
          WIENER_FILT_TAP1_SUBEXP_K,
1608
30.1k
          ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
1609
30.1k
      WIENER_FILT_TAP1_MINV;
1610
30.1k
  wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
1611
30.1k
      aom_read_primitive_refsubexpfin(
1612
30.1k
          rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1613
30.1k
          WIENER_FILT_TAP2_SUBEXP_K,
1614
30.1k
          ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
1615
30.1k
      WIENER_FILT_TAP2_MINV;
1616
  // The central element has an implicit +WIENER_FILT_STEP
1617
30.1k
  wiener_info->hfilter[WIENER_HALFWIN] =
1618
30.1k
      -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
1619
30.1k
            wiener_info->hfilter[2]);
1620
30.1k
  memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
1621
30.1k
}
1622
1623
static inline void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
1624
                                       SgrprojInfo *ref_sgrproj_info,
1625
34.1k
                                       aom_reader *rb) {
1626
34.1k
  sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
1627
34.1k
  const sgr_params_type *params = &av1_sgr_params[sgrproj_info->ep];
1628
1629
34.1k
  if (params->r[0] == 0) {
1630
7.10k
    sgrproj_info->xqd[0] = 0;
1631
7.10k
    sgrproj_info->xqd[1] =
1632
7.10k
        aom_read_primitive_refsubexpfin(
1633
7.10k
            rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1634
7.10k
            ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1635
7.10k
        SGRPROJ_PRJ_MIN1;
1636
27.0k
  } else if (params->r[1] == 0) {
1637
8.15k
    sgrproj_info->xqd[0] =
1638
8.15k
        aom_read_primitive_refsubexpfin(
1639
8.15k
            rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1640
8.15k
            ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1641
8.15k
        SGRPROJ_PRJ_MIN0;
1642
8.15k
    sgrproj_info->xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - sgrproj_info->xqd[0],
1643
8.15k
                                 SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
1644
18.8k
  } else {
1645
18.8k
    sgrproj_info->xqd[0] =
1646
18.8k
        aom_read_primitive_refsubexpfin(
1647
18.8k
            rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1648
18.8k
            ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1649
18.8k
        SGRPROJ_PRJ_MIN0;
1650
18.8k
    sgrproj_info->xqd[1] =
1651
18.8k
        aom_read_primitive_refsubexpfin(
1652
18.8k
            rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1653
18.8k
            ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1654
18.8k
        SGRPROJ_PRJ_MIN1;
1655
18.8k
  }
1656
1657
34.1k
  memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
1658
34.1k
}
1659
1660
static inline void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
1661
                                                   MACROBLOCKD *xd,
1662
                                                   aom_reader *const r,
1663
108k
                                                   int plane, int runit_idx) {
1664
108k
  const RestorationInfo *rsi = &cm->rst_info[plane];
1665
108k
  RestorationUnitInfo *rui = &rsi->unit_info[runit_idx];
1666
108k
  assert(rsi->frame_restoration_type != RESTORE_NONE);
1667
1668
108k
  assert(!cm->features.all_lossless);
1669
1670
108k
  const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN;
1671
108k
  WienerInfo *wiener_info = xd->wiener_info + plane;
1672
108k
  SgrprojInfo *sgrproj_info = xd->sgrproj_info + plane;
1673
1674
108k
  if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
1675
48.9k
    rui->restoration_type =
1676
48.9k
        aom_read_symbol(r, xd->tile_ctx->switchable_restore_cdf,
1677
48.9k
                        RESTORE_SWITCHABLE_TYPES, ACCT_STR);
1678
48.9k
    switch (rui->restoration_type) {
1679
12.5k
      case RESTORE_WIENER:
1680
12.5k
        read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
1681
12.5k
        break;
1682
22.7k
      case RESTORE_SGRPROJ:
1683
22.7k
        read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
1684
22.7k
        break;
1685
13.5k
      default: assert(rui->restoration_type == RESTORE_NONE); break;
1686
48.9k
    }
1687
59.5k
  } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
1688
31.3k
    if (aom_read_symbol(r, xd->tile_ctx->wiener_restore_cdf, 2, ACCT_STR)) {
1689
17.6k
      rui->restoration_type = RESTORE_WIENER;
1690
17.6k
      read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
1691
17.6k
    } else {
1692
13.7k
      rui->restoration_type = RESTORE_NONE;
1693
13.7k
    }
1694
31.3k
  } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
1695
28.2k
    if (aom_read_symbol(r, xd->tile_ctx->sgrproj_restore_cdf, 2, ACCT_STR)) {
1696
11.3k
      rui->restoration_type = RESTORE_SGRPROJ;
1697
11.3k
      read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
1698
16.8k
    } else {
1699
16.8k
      rui->restoration_type = RESTORE_NONE;
1700
16.8k
    }
1701
28.2k
  }
1702
108k
}
1703
1704
static inline void setup_loopfilter(AV1_COMMON *cm,
1705
54.8k
                                    struct aom_read_bit_buffer *rb) {
1706
54.8k
  const int num_planes = av1_num_planes(cm);
1707
54.8k
  struct loopfilter *lf = &cm->lf;
1708
1709
54.8k
  if (cm->features.allow_intrabc || cm->features.coded_lossless) {
1710
    // write default deltas to frame buffer
1711
11.4k
    av1_set_default_ref_deltas(cm->cur_frame->ref_deltas);
1712
11.4k
    av1_set_default_mode_deltas(cm->cur_frame->mode_deltas);
1713
11.4k
    return;
1714
11.4k
  }
1715
43.4k
  assert(!cm->features.coded_lossless);
1716
43.4k
  if (cm->prev_frame) {
1717
    // write deltas to frame buffer
1718
17.0k
    memcpy(lf->ref_deltas, cm->prev_frame->ref_deltas, REF_FRAMES);
1719
17.0k
    memcpy(lf->mode_deltas, cm->prev_frame->mode_deltas, MAX_MODE_LF_DELTAS);
1720
26.3k
  } else {
1721
26.3k
    av1_set_default_ref_deltas(lf->ref_deltas);
1722
26.3k
    av1_set_default_mode_deltas(lf->mode_deltas);
1723
26.3k
  }
1724
43.4k
  lf->filter_level[0] = aom_rb_read_literal(rb, 6);
1725
43.4k
  lf->filter_level[1] = aom_rb_read_literal(rb, 6);
1726
43.4k
  if (num_planes > 1) {
1727
31.0k
    if (lf->filter_level[0] || lf->filter_level[1]) {
1728
25.5k
      lf->filter_level_u = aom_rb_read_literal(rb, 6);
1729
25.5k
      lf->filter_level_v = aom_rb_read_literal(rb, 6);
1730
25.5k
    }
1731
31.0k
  }
1732
43.4k
  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
43.4k
  lf->mode_ref_delta_update = 0;
1737
1738
43.4k
  lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
1739
43.4k
  if (lf->mode_ref_delta_enabled) {
1740
15.7k
    lf->mode_ref_delta_update = aom_rb_read_bit(rb);
1741
15.7k
    if (lf->mode_ref_delta_update) {
1742
54.3k
      for (int i = 0; i < REF_FRAMES; i++)
1743
48.3k
        if (aom_rb_read_bit(rb))
1744
28.2k
          lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
1745
1746
18.1k
      for (int i = 0; i < MAX_MODE_LF_DELTAS; i++)
1747
12.0k
        if (aom_rb_read_bit(rb))
1748
5.02k
          lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
1749
6.04k
    }
1750
15.7k
  }
1751
1752
  // write deltas to frame buffer
1753
43.4k
  memcpy(cm->cur_frame->ref_deltas, lf->ref_deltas, REF_FRAMES);
1754
43.4k
  memcpy(cm->cur_frame->mode_deltas, lf->mode_deltas, MAX_MODE_LF_DELTAS);
1755
43.4k
}
1756
1757
11.4k
static inline void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
1758
11.4k
  const int num_planes = av1_num_planes(cm);
1759
11.4k
  CdefInfo *const cdef_info = &cm->cdef_info;
1760
1761
11.4k
  if (cm->features.allow_intrabc) return;
1762
10.0k
  cdef_info->cdef_damping = aom_rb_read_literal(rb, 2) + 3;
1763
10.0k
  cdef_info->cdef_bits = aom_rb_read_literal(rb, 2);
1764
10.0k
  cdef_info->nb_cdef_strengths = 1 << cdef_info->cdef_bits;
1765
31.9k
  for (int i = 0; i < cdef_info->nb_cdef_strengths; i++) {
1766
21.8k
    cdef_info->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
1767
21.8k
    cdef_info->cdef_uv_strengths[i] =
1768
21.8k
        num_planes > 1 ? aom_rb_read_literal(rb, CDEF_STRENGTH_BITS) : 0;
1769
21.8k
  }
1770
10.0k
}
1771
1772
140k
static inline int read_delta_q(struct aom_read_bit_buffer *rb) {
1773
140k
  return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
1774
140k
}
1775
1776
static inline void setup_quantization(CommonQuantParams *quant_params,
1777
                                      int num_planes, bool separate_uv_delta_q,
1778
54.8k
                                      struct aom_read_bit_buffer *rb) {
1779
54.8k
  quant_params->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
1780
54.8k
  quant_params->y_dc_delta_q = read_delta_q(rb);
1781
54.8k
  if (num_planes > 1) {
1782
37.3k
    int diff_uv_delta = 0;
1783
37.3k
    if (separate_uv_delta_q) diff_uv_delta = aom_rb_read_bit(rb);
1784
37.3k
    quant_params->u_dc_delta_q = read_delta_q(rb);
1785
37.3k
    quant_params->u_ac_delta_q = read_delta_q(rb);
1786
37.3k
    if (diff_uv_delta) {
1787
5.39k
      quant_params->v_dc_delta_q = read_delta_q(rb);
1788
5.39k
      quant_params->v_ac_delta_q = read_delta_q(rb);
1789
31.9k
    } else {
1790
31.9k
      quant_params->v_dc_delta_q = quant_params->u_dc_delta_q;
1791
31.9k
      quant_params->v_ac_delta_q = quant_params->u_ac_delta_q;
1792
31.9k
    }
1793
37.3k
  } else {
1794
17.5k
    quant_params->u_dc_delta_q = 0;
1795
17.5k
    quant_params->u_ac_delta_q = 0;
1796
17.5k
    quant_params->v_dc_delta_q = 0;
1797
17.5k
    quant_params->v_ac_delta_q = 0;
1798
17.5k
  }
1799
54.8k
  quant_params->using_qmatrix = aom_rb_read_bit(rb);
1800
54.8k
  if (quant_params->using_qmatrix) {
1801
26.5k
    quant_params->qmatrix_level_y = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1802
26.5k
    quant_params->qmatrix_level_u = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1803
26.5k
    if (!separate_uv_delta_q)
1804
21.7k
      quant_params->qmatrix_level_v = quant_params->qmatrix_level_u;
1805
4.74k
    else
1806
4.74k
      quant_params->qmatrix_level_v = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1807
28.3k
  } else {
1808
28.3k
    quant_params->qmatrix_level_y = 0;
1809
28.3k
    quant_params->qmatrix_level_u = 0;
1810
28.3k
    quant_params->qmatrix_level_v = 0;
1811
28.3k
  }
1812
54.8k
}
1813
1814
// Get global dequant matrix.
1815
static const qm_val_t *get_iqmatrix(const CommonQuantParams *quant_params,
1816
10.0M
                                    int qmlevel, int plane, TX_SIZE tx_size) {
1817
10.0M
  assert(quant_params->giqmatrix[qmlevel][plane][tx_size] != NULL ||
1818
10.0M
         qmlevel == NUM_QM_LEVELS - 1);
1819
10.0M
  return quant_params->giqmatrix[qmlevel][plane][tx_size];
1820
10.0M
}
1821
1822
// Build y/uv dequant values based on segmentation.
1823
static inline void setup_segmentation_dequant(AV1_COMMON *const cm,
1824
54.8k
                                              MACROBLOCKD *const xd) {
1825
54.8k
  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
54.8k
  const int max_segments = cm->seg.enabled ? MAX_SEGMENTS : 1;
1829
54.8k
  CommonQuantParams *const quant_params = &cm->quant_params;
1830
232k
  for (int i = 0; i < max_segments; ++i) {
1831
177k
    const int qindex = xd->qindex[i];
1832
177k
    quant_params->y_dequant_QTX[i][0] =
1833
177k
        av1_dc_quant_QTX(qindex, quant_params->y_dc_delta_q, bit_depth);
1834
177k
    quant_params->y_dequant_QTX[i][1] = av1_ac_quant_QTX(qindex, 0, bit_depth);
1835
177k
    quant_params->u_dequant_QTX[i][0] =
1836
177k
        av1_dc_quant_QTX(qindex, quant_params->u_dc_delta_q, bit_depth);
1837
177k
    quant_params->u_dequant_QTX[i][1] =
1838
177k
        av1_ac_quant_QTX(qindex, quant_params->u_ac_delta_q, bit_depth);
1839
177k
    quant_params->v_dequant_QTX[i][0] =
1840
177k
        av1_dc_quant_QTX(qindex, quant_params->v_dc_delta_q, bit_depth);
1841
177k
    quant_params->v_dequant_QTX[i][1] =
1842
177k
        av1_ac_quant_QTX(qindex, quant_params->v_ac_delta_q, bit_depth);
1843
177k
    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
177k
    const int qmlevel_y =
1847
177k
        use_qmatrix ? quant_params->qmatrix_level_y : NUM_QM_LEVELS - 1;
1848
3.54M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1849
3.36M
      quant_params->y_iqmatrix[i][j] =
1850
3.36M
          get_iqmatrix(quant_params, qmlevel_y, AOM_PLANE_Y, j);
1851
3.36M
    }
1852
177k
    const int qmlevel_u =
1853
177k
        use_qmatrix ? quant_params->qmatrix_level_u : NUM_QM_LEVELS - 1;
1854
3.54M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1855
3.36M
      quant_params->u_iqmatrix[i][j] =
1856
3.36M
          get_iqmatrix(quant_params, qmlevel_u, AOM_PLANE_U, j);
1857
3.36M
    }
1858
177k
    const int qmlevel_v =
1859
177k
        use_qmatrix ? quant_params->qmatrix_level_v : NUM_QM_LEVELS - 1;
1860
3.54M
    for (int j = 0; j < TX_SIZES_ALL; ++j) {
1861
3.36M
      quant_params->v_iqmatrix[i][j] =
1862
3.36M
          get_iqmatrix(quant_params, qmlevel_v, AOM_PLANE_V, j);
1863
3.36M
    }
1864
177k
  }
1865
54.8k
}
1866
1867
20.0k
static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
1868
20.0k
  return aom_rb_read_bit(rb) ? SWITCHABLE
1869
20.0k
                             : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
1870
20.0k
}
1871
1872
static void read_frame_size(struct aom_read_bit_buffer *rb, int num_bits_width,
1873
22.9k
                            int num_bits_height, int *width, int *height) {
1874
22.9k
  *width = aom_rb_read_literal(rb, num_bits_width) + 1;
1875
22.9k
  *height = aom_rb_read_literal(rb, num_bits_height) + 1;
1876
22.9k
}
1877
1878
static inline void setup_render_size(AV1_COMMON *cm,
1879
39.4k
                                     struct aom_read_bit_buffer *rb) {
1880
39.4k
  cm->render_width = cm->superres_upscaled_width;
1881
39.4k
  cm->render_height = cm->superres_upscaled_height;
1882
39.4k
  if (aom_rb_read_bit(rb))
1883
15.0k
    read_frame_size(rb, 16, 16, &cm->render_width, &cm->render_height);
1884
39.4k
}
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
54.9k
                                  int *height) {
1890
54.9k
  cm->superres_upscaled_width = *width;
1891
54.9k
  cm->superres_upscaled_height = *height;
1892
1893
54.9k
  const SequenceHeader *const seq_params = cm->seq_params;
1894
54.9k
  if (!seq_params->enable_superres) return;
1895
1896
37.1k
  if (aom_rb_read_bit(rb)) {
1897
12.7k
    cm->superres_scale_denominator =
1898
12.7k
        (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
1899
12.7k
    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
12.7k
    av1_calculate_scaled_superres_size(width, height,
1903
12.7k
                                       cm->superres_scale_denominator);
1904
24.3k
  } else {
1905
    // 1:1 scaling - ie. no scaling, scale not provided
1906
24.3k
    cm->superres_scale_denominator = SCALE_NUMERATOR;
1907
24.3k
  }
1908
37.1k
}
1909
1910
static inline void resize_context_buffers(AV1_COMMON *cm, int width,
1911
54.9k
                                          int height) {
1912
54.9k
#if CONFIG_SIZE_LIMIT
1913
54.9k
  if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1914
4
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1915
4
                       "Dimensions of %dx%d beyond allowed size of %dx%d.",
1916
4
                       width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1917
54.9k
#endif
1918
54.9k
  if (cm->width != width || cm->height != height) {
1919
35.8k
    const int new_mi_rows = CEIL_POWER_OF_TWO(height, MI_SIZE_LOG2);
1920
35.8k
    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
35.8k
    if (new_mi_cols > cm->mi_params.mi_cols ||
1925
35.8k
        new_mi_rows > cm->mi_params.mi_rows) {
1926
31.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
1
        cm->width = 0;
1931
1
        cm->height = 0;
1932
1
        aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
1933
1
                           "Failed to allocate context buffers");
1934
1
      }
1935
31.3k
    } else {
1936
4.51k
      cm->mi_params.set_mb_mi(&cm->mi_params, width, height, BLOCK_4X4);
1937
4.51k
    }
1938
35.8k
    av1_init_mi_buffers(&cm->mi_params);
1939
35.8k
    cm->width = width;
1940
35.8k
    cm->height = height;
1941
35.8k
  }
1942
1943
54.9k
  ensure_mv_buffer(cm->cur_frame, cm);
1944
54.9k
  cm->cur_frame->width = cm->width;
1945
54.9k
  cm->cur_frame->height = cm->height;
1946
54.9k
}
1947
1948
54.9k
static inline void setup_buffer_pool(AV1_COMMON *cm) {
1949
54.9k
  BufferPool *const pool = cm->buffer_pool;
1950
54.9k
  const SequenceHeader *const seq_params = cm->seq_params;
1951
1952
54.9k
  lock_buffer_pool(pool);
1953
54.9k
  if (aom_realloc_frame_buffer(
1954
54.9k
          &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
1955
54.9k
          seq_params->subsampling_y, seq_params->use_highbitdepth,
1956
54.9k
          AOM_DEC_BORDER_IN_PIXELS, cm->features.byte_alignment,
1957
54.9k
          &cm->cur_frame->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv,
1958
54.9k
          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
54.9k
  unlock_buffer_pool(pool);
1964
1965
54.9k
  cm->cur_frame->buf.bit_depth = (unsigned int)seq_params->bit_depth;
1966
54.9k
  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
1967
54.9k
  cm->cur_frame->buf.transfer_characteristics =
1968
54.9k
      seq_params->transfer_characteristics;
1969
54.9k
  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
1970
54.9k
  cm->cur_frame->buf.monochrome = seq_params->monochrome;
1971
54.9k
  cm->cur_frame->buf.chroma_sample_position =
1972
54.9k
      seq_params->chroma_sample_position;
1973
54.9k
  cm->cur_frame->buf.color_range = seq_params->color_range;
1974
54.9k
  cm->cur_frame->buf.render_width = cm->render_width;
1975
54.9k
  cm->cur_frame->buf.render_height = cm->render_height;
1976
54.9k
}
1977
1978
static inline void setup_frame_size(AV1_COMMON *cm,
1979
                                    int frame_size_override_flag,
1980
38.5k
                                    struct aom_read_bit_buffer *rb) {
1981
38.5k
  const SequenceHeader *const seq_params = cm->seq_params;
1982
38.5k
  int width, height;
1983
1984
38.5k
  if (frame_size_override_flag) {
1985
7.00k
    int num_bits_width = seq_params->num_bits_width;
1986
7.00k
    int num_bits_height = seq_params->num_bits_height;
1987
7.00k
    read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
1988
7.00k
    if (width > seq_params->max_frame_width ||
1989
7.00k
        height > seq_params->max_frame_height) {
1990
24
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1991
24
                         "Frame dimensions are larger than the maximum values");
1992
24
    }
1993
31.5k
  } else {
1994
31.5k
    width = seq_params->max_frame_width;
1995
31.5k
    height = seq_params->max_frame_height;
1996
31.5k
  }
1997
1998
38.5k
  setup_superres(cm, rb, &width, &height);
1999
38.5k
  resize_context_buffers(cm, width, height);
2000
38.5k
  setup_render_size(cm, rb);
2001
38.5k
  setup_buffer_pool(cm);
2002
38.5k
}
2003
2004
static inline void setup_sb_size(SequenceHeader *seq_params,
2005
35.0k
                                 struct aom_read_bit_buffer *rb) {
2006
35.0k
  set_sb_size(seq_params, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
2007
35.0k
}
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
115k
                                          int this_xss, int this_yss) {
2013
115k
  return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2014
115k
         ref_yss == this_yss;
2015
115k
}
2016
2017
static inline void setup_frame_size_with_refs(AV1_COMMON *cm,
2018
16.5k
                                              struct aom_read_bit_buffer *rb) {
2019
16.5k
  int width, height;
2020
16.5k
  int found = 0;
2021
16.5k
  int has_valid_ref_frame = 0;
2022
30.3k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2023
29.4k
    if (aom_rb_read_bit(rb)) {
2024
15.5k
      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
15.5k
      if (ref_buf == NULL) {
2031
0
        aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2032
0
                           "Invalid condition: invalid reference buffer");
2033
15.5k
      } else {
2034
15.5k
        const YV12_BUFFER_CONFIG *const buf = &ref_buf->buf;
2035
15.5k
        width = buf->y_crop_width;
2036
15.5k
        height = buf->y_crop_height;
2037
15.5k
        cm->render_width = buf->render_width;
2038
15.5k
        cm->render_height = buf->render_height;
2039
15.5k
        setup_superres(cm, rb, &width, &height);
2040
15.5k
        resize_context_buffers(cm, width, height);
2041
15.5k
        found = 1;
2042
15.5k
        break;
2043
15.5k
      }
2044
15.5k
    }
2045
29.4k
  }
2046
2047
16.5k
  const SequenceHeader *const seq_params = cm->seq_params;
2048
16.5k
  if (!found) {
2049
924
    int num_bits_width = seq_params->num_bits_width;
2050
924
    int num_bits_height = seq_params->num_bits_height;
2051
2052
924
    read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
2053
924
    setup_superres(cm, rb, &width, &height);
2054
924
    resize_context_buffers(cm, width, height);
2055
924
    setup_render_size(cm, rb);
2056
924
  }
2057
2058
16.5k
  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
132k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2065
115k
    const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
2066
115k
    has_valid_ref_frame |=
2067
115k
        valid_ref_frame_size(ref_frame->buf.y_crop_width,
2068
115k
                             ref_frame->buf.y_crop_height, width, height);
2069
115k
  }
2070
16.5k
  if (!has_valid_ref_frame)
2071
13
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2072
13
                       "Referenced frame has invalid size");
2073
131k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2074
115k
    const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
2075
115k
    if (!valid_ref_frame_img_fmt(
2076
115k
            ref_frame->buf.bit_depth, ref_frame->buf.subsampling_x,
2077
115k
            ref_frame->buf.subsampling_y, seq_params->bit_depth,
2078
115k
            seq_params->subsampling_x, seq_params->subsampling_y))
2079
15
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
2080
15
                         "Referenced frame has incompatible color format");
2081
115k
  }
2082
16.5k
  setup_buffer_pool(cm);
2083
16.5k
}
2084
2085
// Same function as av1_read_uniform but reading from uncompresses header wb
2086
57.2k
static int rb_read_uniform(struct aom_read_bit_buffer *const rb, int n) {
2087
57.2k
  const int l = get_unsigned_bits(n);
2088
57.2k
  const int m = (1 << l) - n;
2089
57.2k
  const int v = aom_rb_read_literal(rb, l - 1);
2090
57.2k
  assert(l != 0);
2091
57.2k
  if (v < m)
2092
53.9k
    return v;
2093
3.29k
  else
2094
3.29k
    return (v << 1) - m + aom_rb_read_bit(rb);
2095
57.2k
}
2096
2097
static inline void read_tile_info_max_tile(
2098
54.9k
    AV1_COMMON *const cm, struct aom_read_bit_buffer *const rb) {
2099
54.9k
  const SequenceHeader *const seq_params = cm->seq_params;
2100
54.9k
  CommonTileParams *const tiles = &cm->tiles;
2101
54.9k
  int width_sb =
2102
54.9k
      CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
2103
54.9k
  int height_sb =
2104
54.9k
      CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
2105
2106
54.9k
  av1_get_tile_limits(cm);
2107
54.9k
  tiles->uniform_spacing = aom_rb_read_bit(rb);
2108
2109
  // Read tile columns
2110
54.9k
  if (tiles->uniform_spacing) {
2111
29.6k
    tiles->log2_cols = tiles->min_log2_cols;
2112
29.8k
    while (tiles->log2_cols < tiles->max_log2_cols) {
2113
5.17k
      if (!aom_rb_read_bit(rb)) {
2114
5.04k
        break;
2115
5.04k
      }
2116
122
      tiles->log2_cols++;
2117
122
    }
2118
29.6k
  } else {
2119
25.2k
    int i;
2120
25.2k
    int start_sb;
2121
52.6k
    for (i = 0, start_sb = 0; width_sb > 0 && i < MAX_TILE_COLS; i++) {
2122
27.3k
      const int size_sb =
2123
27.3k
          1 + rb_read_uniform(rb, AOMMIN(width_sb, tiles->max_width_sb));
2124
27.3k
      tiles->col_start_sb[i] = start_sb;
2125
27.3k
      start_sb += size_sb;
2126
27.3k
      width_sb -= size_sb;
2127
27.3k
    }
2128
25.2k
    tiles->cols = i;
2129
25.2k
    tiles->col_start_sb[i] = start_sb + width_sb;
2130
25.2k
  }
2131
54.9k
  av1_calculate_tile_cols(seq_params, cm->mi_params.mi_rows,
2132
54.9k
                          cm->mi_params.mi_cols, tiles);
2133
2134
  // Read tile rows
2135
54.9k
  if (tiles->uniform_spacing) {
2136
29.6k
    tiles->log2_rows = tiles->min_log2_rows;
2137
29.8k
    while (tiles->log2_rows < tiles->max_log2_rows) {
2138
12.0k
      if (!aom_rb_read_bit(rb)) {
2139
11.8k
        break;
2140
11.8k
      }
2141
183
      tiles->log2_rows++;
2142
183
    }
2143
29.6k
  } else {
2144
25.2k
    int i;
2145
25.2k
    int start_sb;
2146
55.1k
    for (i = 0, start_sb = 0; height_sb > 0 && i < MAX_TILE_ROWS; i++) {
2147
29.8k
      const int size_sb =
2148
29.8k
          1 + rb_read_uniform(rb, AOMMIN(height_sb, tiles->max_height_sb));
2149
29.8k
      tiles->row_start_sb[i] = start_sb;
2150
29.8k
      start_sb += size_sb;
2151
29.8k
      height_sb -= size_sb;
2152
29.8k
    }
2153
25.2k
    tiles->rows = i;
2154
25.2k
    tiles->row_start_sb[i] = start_sb + height_sb;
2155
25.2k
  }
2156
54.9k
  av1_calculate_tile_rows(seq_params, cm->mi_params.mi_rows, tiles);
2157
54.9k
}
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
54.9k
                                  struct aom_read_bit_buffer *const rb) {
2183
54.9k
  AV1_COMMON *const cm = &pbi->common;
2184
2185
54.9k
  read_tile_info_max_tile(cm, rb);
2186
2187
54.9k
  pbi->context_update_tile_id = 0;
2188
54.9k
  if (cm->tiles.rows * cm->tiles.cols > 1) {
2189
    // tile to use for cdf update
2190
2.62k
    pbi->context_update_tile_id =
2191
2.62k
        aom_rb_read_literal(rb, cm->tiles.log2_rows + cm->tiles.log2_cols);
2192
2.62k
    if (pbi->context_update_tile_id >= cm->tiles.rows * cm->tiles.cols) {
2193
29
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2194
29
                         "Invalid context_update_tile_id");
2195
29
    }
2196
    // tile size magnitude
2197
2.62k
    pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2198
2.62k
  }
2199
54.9k
}
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
6.84k
static size_t mem_get_varsize(const uint8_t *src, int sz) {
2220
6.84k
  switch (sz) {
2221
3.38k
    case 1: return src[0];
2222
2.74k
    case 2: return mem_get_le16(src);
2223
350
    case 3: return mem_get_le24(src);
2224
370
    case 4: return mem_get_le32(src);
2225
0
    default: assert(0 && "Invalid size"); return -1;
2226
6.84k
  }
2227
6.84k
}
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
60.8k
                                   TileBufferDec *const buf) {
2403
60.8k
  size_t size;
2404
2405
60.8k
  if (!is_last) {
2406
6.85k
    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
6.85k
    size = mem_get_varsize(*data, tile_size_bytes) + AV1_MIN_TILE_SIZE_BYTES;
2411
6.85k
    *data += tile_size_bytes;
2412
2413
6.85k
    if (size > (size_t)(data_end - *data))
2414
110
      aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
2415
110
                         "Truncated packet or corrupt tile size");
2416
54.0k
  } else {
2417
54.0k
    size = data_end - *data;
2418
54.0k
  }
2419
2420
60.8k
  buf->data = *data;
2421
60.8k
  buf->size = size;
2422
2423
60.8k
  *data += size;
2424
60.8k
}
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
54.1k
    int end_tile) {
2430
54.1k
  AV1_COMMON *const cm = &pbi->common;
2431
54.1k
  const int tile_cols = cm->tiles.cols;
2432
54.1k
  const int tile_rows = cm->tiles.rows;
2433
54.1k
  int tc = 0;
2434
2435
111k
  for (int r = 0; r < tile_rows; ++r) {
2436
118k
    for (int c = 0; c < tile_cols; ++c, ++tc) {
2437
60.9k
      TileBufferDec *const buf = &tile_buffers[r][c];
2438
2439
60.9k
      const int is_last = (tc == end_tile);
2440
60.9k
      const size_t hdr_offset = 0;
2441
2442
60.9k
      if (tc < start_tile || tc > end_tile) continue;
2443
2444
60.9k
      if (data + hdr_offset >= data_end)
2445
16
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2446
16
                           "Data ended before all tiles were read.");
2447
60.9k
      data += hdr_offset;
2448
60.9k
      get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &pbi->error,
2449
60.9k
                      &data, buf);
2450
60.9k
    }
2451
57.5k
  }
2452
54.1k
}
2453
2454
static inline void set_cb_buffer(AV1Decoder *pbi, DecoderCodingBlock *dcb,
2455
                                 CB_BUFFER *cb_buffer_base,
2456
739k
                                 const int num_planes, int mi_row, int mi_col) {
2457
739k
  AV1_COMMON *const cm = &pbi->common;
2458
739k
  int mib_size_log2 = cm->seq_params->mib_size_log2;
2459
739k
  int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
2460
739k
  int offset = (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
2461
739k
  CB_BUFFER *cb_buffer = cb_buffer_base + offset;
2462
2463
2.77M
  for (int plane = 0; plane < num_planes; ++plane) {
2464
2.03M
    dcb->dqcoeff_block[plane] = cb_buffer->dqcoeff[plane];
2465
2.03M
    dcb->eob_data[plane] = cb_buffer->eob_data[plane];
2466
2.03M
    dcb->cb_offset[plane] = 0;
2467
2.03M
    dcb->txb_offset[plane] = 0;
2468
2.03M
  }
2469
739k
  MACROBLOCKD *const xd = &dcb->xd;
2470
739k
  xd->plane[0].color_index_map = cb_buffer->color_index_map[0];
2471
739k
  xd->plane[1].color_index_map = cb_buffer->color_index_map[1];
2472
739k
  xd->color_index_map_offset[0] = 0;
2473
739k
  xd->color_index_map_offset[1] = 0;
2474
739k
}
2475
2476
27.0k
static inline void decoder_alloc_tile_data(AV1Decoder *pbi, const int n_tiles) {
2477
27.0k
  AV1_COMMON *const cm = &pbi->common;
2478
27.0k
  aom_free(pbi->tile_data);
2479
27.0k
  pbi->allocated_tiles = 0;
2480
27.0k
  CHECK_MEM_ERROR(cm, pbi->tile_data,
2481
27.0k
                  aom_memalign(32, n_tiles * sizeof(*pbi->tile_data)));
2482
27.0k
  pbi->allocated_tiles = n_tiles;
2483
57.7k
  for (int i = 0; i < n_tiles; i++) {
2484
30.7k
    TileDataDec *const tile_data = pbi->tile_data + i;
2485
30.7k
    av1_zero(tile_data->dec_row_mt_sync);
2486
30.7k
  }
2487
27.0k
  pbi->allocated_row_mt_sync_rows = 0;
2488
27.0k
}
2489
2490
// Set up nsync by width.
2491
21.9k
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
21.9k
  (void)width;
2504
21.9k
#endif
2505
21.9k
  return 1;
2506
21.9k
}
2507
2508
// Allocate memory for decoder row synchronization
2509
static inline void dec_row_mt_alloc(AV1DecRowMTSync *dec_row_mt_sync,
2510
21.9k
                                    AV1_COMMON *cm, int rows) {
2511
21.9k
  dec_row_mt_sync->allocated_sb_rows = rows;
2512
21.9k
#if CONFIG_MULTITHREAD
2513
21.9k
  {
2514
21.9k
    int i;
2515
2516
21.9k
    CHECK_MEM_ERROR(cm, dec_row_mt_sync->mutex_,
2517
21.9k
                    aom_malloc(sizeof(*(dec_row_mt_sync->mutex_)) * rows));
2518
21.9k
    if (dec_row_mt_sync->mutex_) {
2519
389k
      for (i = 0; i < rows; ++i) {
2520
367k
        pthread_mutex_init(&dec_row_mt_sync->mutex_[i], NULL);
2521
367k
      }
2522
21.9k
    }
2523
2524
21.9k
    CHECK_MEM_ERROR(cm, dec_row_mt_sync->cond_,
2525
21.9k
                    aom_malloc(sizeof(*(dec_row_mt_sync->cond_)) * rows));
2526
21.9k
    if (dec_row_mt_sync->cond_) {
2527
389k
      for (i = 0; i < rows; ++i) {
2528
367k
        pthread_cond_init(&dec_row_mt_sync->cond_[i], NULL);
2529
367k
      }
2530
21.9k
    }
2531
21.9k
  }
2532
21.9k
#endif  // CONFIG_MULTITHREAD
2533
2534
21.9k
  CHECK_MEM_ERROR(cm, dec_row_mt_sync->cur_sb_col,
2535
21.9k
                  aom_malloc(sizeof(*(dec_row_mt_sync->cur_sb_col)) * rows));
2536
2537
  // Set up nsync.
2538
21.9k
  dec_row_mt_sync->sync_range = get_sync_range(cm->width);
2539
21.9k
}
2540
2541
// Deallocate decoder row synchronization related mutex and data
2542
52.6k
void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync) {
2543
52.6k
  if (dec_row_mt_sync != NULL) {
2544
52.6k
#if CONFIG_MULTITHREAD
2545
52.6k
    int i;
2546
52.6k
    if (dec_row_mt_sync->mutex_ != NULL) {
2547
389k
      for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
2548
367k
        pthread_mutex_destroy(&dec_row_mt_sync->mutex_[i]);
2549
367k
      }
2550
21.9k
      aom_free(dec_row_mt_sync->mutex_);
2551
21.9k
    }
2552
52.6k
    if (dec_row_mt_sync->cond_ != NULL) {
2553
389k
      for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
2554
367k
        pthread_cond_destroy(&dec_row_mt_sync->cond_[i]);
2555
367k
      }
2556
21.9k
      aom_free(dec_row_mt_sync->cond_);
2557
21.9k
    }
2558
52.6k
#endif  // CONFIG_MULTITHREAD
2559
52.6k
    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
52.6k
    av1_zero(*dec_row_mt_sync);
2564
52.6k
  }
2565
52.6k
}
2566
2567
static inline void sync_read(AV1DecRowMTSync *const dec_row_mt_sync, int r,
2568
257k
                             int c) {
2569
257k
#if CONFIG_MULTITHREAD
2570
257k
  const int nsync = dec_row_mt_sync->sync_range;
2571
2572
257k
  if (r && !(c & (nsync - 1))) {
2573
193k
    pthread_mutex_t *const mutex = &dec_row_mt_sync->mutex_[r - 1];
2574
193k
    pthread_mutex_lock(mutex);
2575
2576
227k
    while (c > dec_row_mt_sync->cur_sb_col[r - 1] - nsync -
2577
227k
                   dec_row_mt_sync->intrabc_extra_top_right_sb_delay) {
2578
33.9k
      pthread_cond_wait(&dec_row_mt_sync->cond_[r - 1], mutex);
2579
33.9k
    }
2580
193k
    pthread_mutex_unlock(mutex);
2581
193k
  }
2582
#else
2583
  (void)dec_row_mt_sync;
2584
  (void)r;
2585
  (void)c;
2586
#endif  // CONFIG_MULTITHREAD
2587
257k
}
2588
2589
static inline void sync_write(AV1DecRowMTSync *const dec_row_mt_sync, int r,
2590
258k
                              int c, const int sb_cols) {
2591
258k
#if CONFIG_MULTITHREAD
2592
258k
  const int nsync = dec_row_mt_sync->sync_range;
2593
258k
  int cur;
2594
258k
  int sig = 1;
2595
2596
258k
  if (c < sb_cols - 1) {
2597
150k
    cur = c;
2598
150k
    if (c % nsync) sig = 0;
2599
150k
  } else {
2600
108k
    cur = sb_cols + nsync + dec_row_mt_sync->intrabc_extra_top_right_sb_delay;
2601
108k
  }
2602
2603
258k
  if (sig) {
2604
258k
    pthread_mutex_lock(&dec_row_mt_sync->mutex_[r]);
2605
2606
258k
    dec_row_mt_sync->cur_sb_col[r] = cur;
2607
2608
258k
    pthread_cond_signal(&dec_row_mt_sync->cond_[r]);
2609
258k
    pthread_mutex_unlock(&dec_row_mt_sync->mutex_[r]);
2610
258k
  }
2611
#else
2612
  (void)dec_row_mt_sync;
2613
  (void)r;
2614
  (void)c;
2615
  (void)sb_cols;
2616
#endif  // CONFIG_MULTITHREAD
2617
258k
}
2618
2619
static inline void signal_decoding_done_for_erroneous_row(
2620
675
    AV1Decoder *const pbi, const MACROBLOCKD *const xd) {
2621
675
  AV1_COMMON *const cm = &pbi->common;
2622
675
  const TileInfo *const tile = &xd->tile;
2623
675
  const int sb_row_in_tile =
2624
675
      ((xd->mi_row - tile->mi_row_start) >> cm->seq_params->mib_size_log2);
2625
675
  const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile);
2626
675
  TileDataDec *const tile_data =
2627
675
      pbi->tile_data + tile->tile_row * cm->tiles.cols + tile->tile_col;
2628
675
  AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
2629
2630
675
  sync_write(dec_row_mt_sync, sb_row_in_tile, sb_cols_in_tile - 1,
2631
675
             sb_cols_in_tile);
2632
675
}
2633
2634
static inline void decode_tile_sb_row(AV1Decoder *pbi, ThreadData *const td,
2635
                                      const TileInfo *tile_info,
2636
107k
                                      const int mi_row) {
2637
107k
  AV1_COMMON *const cm = &pbi->common;
2638
107k
  const int num_planes = av1_num_planes(cm);
2639
107k
  TileDataDec *const tile_data = pbi->tile_data +
2640
107k
                                 tile_info->tile_row * cm->tiles.cols +
2641
107k
                                 tile_info->tile_col;
2642
107k
  const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
2643
107k
  const int sb_row_in_tile =
2644
107k
      (mi_row - tile_info->mi_row_start) >> cm->seq_params->mib_size_log2;
2645
107k
  int sb_col_in_tile = 0;
2646
107k
  int row_mt_exit = 0;
2647
2648
365k
  for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
2649
257k
       mi_col += cm->seq_params->mib_size, sb_col_in_tile++) {
2650
257k
    set_cb_buffer(pbi, &td->dcb, pbi->cb_buffer_base, num_planes, mi_row,
2651
257k
                  mi_col);
2652
2653
257k
    sync_read(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile);
2654
2655
257k
#if CONFIG_MULTITHREAD
2656
257k
    pthread_mutex_lock(pbi->row_mt_mutex_);
2657
257k
#endif
2658
257k
    row_mt_exit = pbi->frame_row_mt_info.row_mt_exit;
2659
257k
#if CONFIG_MULTITHREAD
2660
257k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
2661
257k
#endif
2662
2663
257k
    if (!row_mt_exit) {
2664
      // Decoding of the super-block
2665
249k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
2666
249k
                       cm->seq_params->sb_size, 0x2);
2667
249k
    }
2668
2669
257k
    sync_write(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile,
2670
257k
               sb_cols_in_tile);
2671
257k
  }
2672
107k
}
2673
2674
39.3k
static int check_trailing_bits_after_symbol_coder(aom_reader *r) {
2675
39.3k
  if (aom_reader_has_overflowed(r)) return -1;
2676
2677
39.3k
  uint32_t nb_bits = aom_reader_tell(r);
2678
39.3k
  uint32_t nb_bytes = (nb_bits + 7) >> 3;
2679
39.3k
  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
39.3k
  uint8_t last_byte = p[-1];
2685
39.3k
  uint8_t pattern = 128 >> ((nb_bits - 1) & 7);
2686
39.3k
  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
36.2k
  const uint8_t *p_end = aom_reader_find_end(r);
2690
289k
  while (p < p_end) {
2691
253k
    if (*p != 0) return -1;
2692
253k
    p++;
2693
253k
  }
2694
35.9k
  return 0;
2695
36.2k
}
2696
2697
static inline void set_decode_func_pointers(ThreadData *td,
2698
118k
                                            int parse_decode_flag) {
2699
118k
  td->read_coeffs_tx_intra_block_visit = decode_block_void;
2700
118k
  td->predict_and_recon_intra_block_visit = decode_block_void;
2701
118k
  td->read_coeffs_tx_inter_block_visit = decode_block_void;
2702
118k
  td->inverse_tx_inter_block_visit = decode_block_void;
2703
118k
  td->predict_inter_block_visit = predict_inter_block_void;
2704
118k
  td->cfl_store_inter_block_visit = cfl_store_inter_block_void;
2705
2706
118k
  if (parse_decode_flag & 0x1) {
2707
71.3k
    td->read_coeffs_tx_intra_block_visit = read_coeffs_tx_intra_block;
2708
71.3k
    td->read_coeffs_tx_inter_block_visit = av1_read_coeffs_txb;
2709
71.3k
  }
2710
118k
  if (parse_decode_flag & 0x2) {
2711
58.0k
    td->predict_and_recon_intra_block_visit =
2712
58.0k
        predict_and_reconstruct_intra_block;
2713
58.0k
    td->inverse_tx_inter_block_visit = inverse_transform_inter_block;
2714
58.0k
    td->predict_inter_block_visit = predict_inter_block;
2715
58.0k
    td->cfl_store_inter_block_visit = cfl_store_inter_block;
2716
58.0k
  }
2717
118k
}
2718
2719
static inline void decode_tile(AV1Decoder *pbi, ThreadData *const td,
2720
11.2k
                               int tile_row, int tile_col) {
2721
11.2k
  TileInfo tile_info;
2722
2723
11.2k
  AV1_COMMON *const cm = &pbi->common;
2724
11.2k
  const int num_planes = av1_num_planes(cm);
2725
2726
11.2k
  av1_tile_set_row(&tile_info, cm, tile_row);
2727
11.2k
  av1_tile_set_col(&tile_info, cm, tile_col);
2728
11.2k
  DecoderCodingBlock *const dcb = &td->dcb;
2729
11.2k
  MACROBLOCKD *const xd = &dcb->xd;
2730
2731
11.2k
  av1_zero_above_context(cm, xd, tile_info.mi_col_start, tile_info.mi_col_end,
2732
11.2k
                         tile_row);
2733
11.2k
  av1_reset_loop_filter_delta(xd, num_planes);
2734
11.2k
  av1_reset_loop_restoration(xd, num_planes);
2735
2736
65.8k
  for (int mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
2737
61.0k
       mi_row += cm->seq_params->mib_size) {
2738
61.0k
    av1_zero_left_context(xd);
2739
2740
178k
    for (int mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
2741
124k
         mi_col += cm->seq_params->mib_size) {
2742
124k
      set_cb_buffer(pbi, dcb, &td->cb_buffer_base, num_planes, 0, 0);
2743
2744
      // Bit-stream parsing and decoding of the superblock
2745
124k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
2746
124k
                       cm->seq_params->sb_size, 0x3);
2747
2748
124k
      if (aom_reader_has_overflowed(td->bit_reader)) {
2749
6.41k
        aom_merge_corrupted_flag(&dcb->corrupted, 1);
2750
6.41k
        return;
2751
6.41k
      }
2752
124k
    }
2753
61.0k
  }
2754
2755
4.82k
  int corrupted =
2756
4.82k
      (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
2757
4.82k
  aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
2758
4.82k
}
2759
2760
static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
2761
                                   const uint8_t *data_end, int start_tile,
2762
11.2k
                                   int end_tile) {
2763
11.2k
  AV1_COMMON *const cm = &pbi->common;
2764
11.2k
  ThreadData *const td = &pbi->td;
2765
11.2k
  CommonTileParams *const tiles = &cm->tiles;
2766
11.2k
  const int tile_cols = tiles->cols;
2767
11.2k
  const int tile_rows = tiles->rows;
2768
11.2k
  const int n_tiles = tile_cols * tile_rows;
2769
11.2k
  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
2770
11.2k
  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
2771
11.2k
  const int single_row = pbi->dec_tile_row >= 0;
2772
11.2k
  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
2773
11.2k
  const int single_col = pbi->dec_tile_col >= 0;
2774
11.2k
  int tile_rows_start;
2775
11.2k
  int tile_rows_end;
2776
11.2k
  int tile_cols_start;
2777
11.2k
  int tile_cols_end;
2778
11.2k
  int inv_col_order;
2779
11.2k
  int inv_row_order;
2780
11.2k
  int tile_row, tile_col;
2781
11.2k
  uint8_t allow_update_cdf;
2782
11.2k
  const uint8_t *raw_data_end = NULL;
2783
2784
11.2k
  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
11.2k
  } else {
2793
11.2k
    tile_rows_start = 0;
2794
11.2k
    tile_rows_end = tile_rows;
2795
11.2k
    tile_cols_start = 0;
2796
11.2k
    tile_cols_end = tile_cols;
2797
11.2k
    inv_col_order = pbi->inv_tile_order;
2798
11.2k
    inv_row_order = pbi->inv_tile_order;
2799
11.2k
    allow_update_cdf = 1;
2800
11.2k
  }
2801
2802
  // No tiles to decode.
2803
11.2k
  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
2804
      // First tile is larger than end_tile.
2805
11.2k
      tile_rows_start * tiles->cols + tile_cols_start > end_tile ||
2806
      // Last tile is smaller than start_tile.
2807
11.2k
      (tile_rows_end - 1) * tiles->cols + tile_cols_end - 1 < start_tile)
2808
0
    return data;
2809
2810
11.2k
  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
2811
2812
11.2k
  assert(tile_rows <= MAX_TILE_ROWS);
2813
11.2k
  assert(tile_cols <= MAX_TILE_COLS);
2814
2815
11.2k
#if EXT_TILE_DEBUG
2816
11.2k
  if (tiles->large_scale && !pbi->ext_tile_debug)
2817
0
    raw_data_end = get_ls_single_tile_buffer(pbi, data, tile_buffers);
2818
11.2k
  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
11.2k
  else
2821
11.2k
#endif  // EXT_TILE_DEBUG
2822
11.2k
    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
2823
2824
11.2k
  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
2825
9.05k
    decoder_alloc_tile_data(pbi, n_tiles);
2826
9.05k
  }
2827
11.2k
  if (pbi->dcb.xd.seg_mask == NULL)
2828
11.2k
    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
2829
11.2k
                    (uint8_t *)aom_memalign(
2830
11.2k
                        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
11.2k
  set_decode_func_pointers(&pbi->td, 0x3);
2838
2839
  // Load all tile information into thread_data.
2840
11.2k
  td->dcb = pbi->dcb;
2841
2842
11.2k
  td->dcb.corrupted = 0;
2843
11.2k
  td->dcb.mc_buf[0] = td->mc_buf[0];
2844
11.2k
  td->dcb.mc_buf[1] = td->mc_buf[1];
2845
11.2k
  td->dcb.xd.tmp_conv_dst = td->tmp_conv_dst;
2846
33.7k
  for (int j = 0; j < 2; ++j) {
2847
22.4k
    td->dcb.xd.tmp_obmc_bufs[j] = td->tmp_obmc_bufs[j];
2848
22.4k
  }
2849
2850
22.5k
  for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2851
11.2k
    const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
2852
2853
22.4k
    for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
2854
11.2k
      const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
2855
11.2k
      TileDataDec *const tile_data = pbi->tile_data + row * tiles->cols + col;
2856
11.2k
      const TileBufferDec *const tile_bs_buf = &tile_buffers[row][col];
2857
2858
11.2k
      if (row * tiles->cols + col < start_tile ||
2859
11.2k
          row * tiles->cols + col > end_tile)
2860
0
        continue;
2861
2862
11.2k
      td->bit_reader = &tile_data->bit_reader;
2863
11.2k
      av1_zero(td->cb_buffer_base.dqcoeff);
2864
11.2k
      av1_tile_init(&td->dcb.xd.tile, cm, row, col);
2865
11.2k
      td->dcb.xd.current_base_qindex = cm->quant_params.base_qindex;
2866
11.2k
      setup_bool_decoder(&td->dcb.xd, tile_bs_buf->data, data_end,
2867
11.2k
                         tile_bs_buf->size, &pbi->error, td->bit_reader,
2868
11.2k
                         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
11.2k
      av1_init_macroblockd(cm, &td->dcb.xd);
2879
11.2k
      av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), row,
2880
11.2k
                             &td->dcb.xd);
2881
2882
      // Initialise the tile context from the frame context
2883
11.2k
      tile_data->tctx = *cm->fc;
2884
11.2k
      td->dcb.xd.tile_ctx = &tile_data->tctx;
2885
2886
      // decode tile
2887
11.2k
      decode_tile(pbi, td, row, col);
2888
11.2k
      aom_merge_corrupted_flag(&pbi->dcb.corrupted, td->dcb.corrupted);
2889
11.2k
      if (pbi->dcb.corrupted)
2890
7.44k
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2891
7.44k
                           "Failed to decode tile data");
2892
11.2k
    }
2893
11.2k
  }
2894
2895
11.2k
  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
11.2k
  TileDataDec *const tile_data = pbi->tile_data + end_tile;
2904
2905
11.2k
  return aom_reader_find_end(&tile_data->bit_reader);
2906
11.2k
}
2907
2908
92.6k
static TileJobsDec *get_dec_job_info(AV1DecTileMT *tile_mt_info) {
2909
92.6k
  TileJobsDec *cur_job_info = NULL;
2910
92.6k
#if CONFIG_MULTITHREAD
2911
92.6k
  pthread_mutex_lock(tile_mt_info->job_mutex);
2912
2913
92.6k
  if (tile_mt_info->jobs_dequeued < tile_mt_info->jobs_enqueued) {
2914
45.9k
    cur_job_info = tile_mt_info->job_queue + tile_mt_info->jobs_dequeued;
2915
45.9k
    tile_mt_info->jobs_dequeued++;
2916
45.9k
  }
2917
2918
92.6k
  pthread_mutex_unlock(tile_mt_info->job_mutex);
2919
#else
2920
  (void)tile_mt_info;
2921
#endif
2922
92.6k
  return cur_job_info;
2923
92.6k
}
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
45.9k
                                         uint8_t allow_update_cdf) {
2930
45.9k
  AV1_COMMON *cm = &pbi->common;
2931
45.9k
  ThreadData *const td = thread_data->td;
2932
45.9k
  int tile_row = tile_data->tile_info.tile_row;
2933
45.9k
  int tile_col = tile_data->tile_info.tile_col;
2934
2935
45.9k
  td->bit_reader = &tile_data->bit_reader;
2936
45.9k
  av1_zero(td->cb_buffer_base.dqcoeff);
2937
2938
45.9k
  MACROBLOCKD *const xd = &td->dcb.xd;
2939
45.9k
  av1_tile_init(&xd->tile, cm, tile_row, tile_col);
2940
45.9k
  xd->current_base_qindex = cm->quant_params.base_qindex;
2941
2942
45.9k
  setup_bool_decoder(xd, tile_buffer->data, thread_data->data_end,
2943
45.9k
                     tile_buffer->size, &thread_data->error_info,
2944
45.9k
                     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
45.9k
  av1_init_macroblockd(cm, xd);
2955
45.9k
  xd->error_info = &thread_data->error_info;
2956
45.9k
  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
45.9k
  tile_data->tctx = *cm->fc;
2960
45.9k
  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
45.9k
}
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
155k
                                                  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
155k
  int sb_rows = av1_get_sb_rows_in_tile(cm, tile);
3021
155k
  int max_workers =
3022
155k
      sb_rows == 1 ? AOM_MIN_THREADS_PER_TILE : AOM_MAX_THREADS_PER_TILE;
3023
155k
  return max_workers;
3024
155k
}
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
174k
                             int *end_of_frame) {
3039
174k
  AV1_COMMON *cm = &pbi->common;
3040
174k
  TileDataDec *tile_data;
3041
174k
  AV1DecRowMTSync *dec_row_mt_sync;
3042
174k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3043
174k
  const int tile_rows_start = frame_row_mt_info->tile_rows_start;
3044
174k
  const int tile_rows_end = frame_row_mt_info->tile_rows_end;
3045
174k
  const int tile_cols_start = frame_row_mt_info->tile_cols_start;
3046
174k
  const int tile_cols_end = frame_row_mt_info->tile_cols_end;
3047
174k
  const int start_tile = frame_row_mt_info->start_tile;
3048
174k
  const int end_tile = frame_row_mt_info->end_tile;
3049
174k
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
3050
174k
  int num_mis_to_decode, num_threads_working;
3051
174k
  int num_mis_waiting_for_decode;
3052
174k
  int min_threads_working = INT_MAX;
3053
174k
  int max_mis_to_decode = 0;
3054
174k
  int tile_row_idx, tile_col_idx;
3055
174k
  int tile_row = -1;
3056
174k
  int tile_col = -1;
3057
3058
174k
  memset(next_job_info, 0, sizeof(*next_job_info));
3059
3060
  // Frame decode is completed or error is encountered.
3061
174k
  *end_of_frame = (frame_row_mt_info->mi_rows_decode_started ==
3062
174k
                   frame_row_mt_info->mi_rows_to_decode) ||
3063
174k
                  (frame_row_mt_info->row_mt_exit == 1);
3064
174k
  if (*end_of_frame) {
3065
46.8k
    return 1;
3066
46.8k
  }
3067
3068
  // Decoding cannot start as bit-stream parsing is not complete.
3069
127k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3070
127k
         frame_row_mt_info->mi_rows_decode_started);
3071
127k
  if (frame_row_mt_info->mi_rows_parse_done ==
3072
127k
      frame_row_mt_info->mi_rows_decode_started)
3073
19.3k
    return 0;
3074
3075
  // Choose the tile to decode.
3076
225k
  for (tile_row_idx = tile_rows_start; tile_row_idx < tile_rows_end;
3077
117k
       ++tile_row_idx) {
3078
234k
    for (tile_col_idx = tile_cols_start; tile_col_idx < tile_cols_end;
3079
117k
         ++tile_col_idx) {
3080
117k
      if (tile_row_idx * cm->tiles.cols + tile_col_idx < start_tile ||
3081
117k
          tile_row_idx * cm->tiles.cols + tile_col_idx > end_tile)
3082
1
        continue;
3083
3084
117k
      tile_data = pbi->tile_data + tile_row_idx * cm->tiles.cols + tile_col_idx;
3085
117k
      dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3086
3087
117k
      num_threads_working = dec_row_mt_sync->num_threads_working;
3088
117k
      num_mis_waiting_for_decode = (dec_row_mt_sync->mi_rows_parse_done -
3089
117k
                                    dec_row_mt_sync->mi_rows_decode_started) *
3090
117k
                                   dec_row_mt_sync->mi_cols;
3091
117k
      num_mis_to_decode =
3092
117k
          (dec_row_mt_sync->mi_rows - dec_row_mt_sync->mi_rows_decode_started) *
3093
117k
          dec_row_mt_sync->mi_cols;
3094
3095
117k
      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
117k
      if (num_mis_waiting_for_decode > 0) {
3099
110k
        if (num_threads_working < min_threads_working) {
3100
108k
          min_threads_working = num_threads_working;
3101
108k
          max_mis_to_decode = 0;
3102
108k
        }
3103
110k
        if (num_threads_working == min_threads_working &&
3104
110k
            num_mis_to_decode > max_mis_to_decode &&
3105
110k
            num_threads_working <
3106
108k
                get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info)) {
3107
107k
          max_mis_to_decode = num_mis_to_decode;
3108
107k
          tile_row = tile_row_idx;
3109
107k
          tile_col = tile_col_idx;
3110
107k
        }
3111
110k
      }
3112
117k
    }
3113
117k
  }
3114
  // No job found to process
3115
108k
  if (tile_row == -1 || tile_col == -1) return 0;
3116
3117
107k
  tile_data = pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3118
107k
  dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3119
3120
107k
  next_job_info->tile_row = tile_row;
3121
107k
  next_job_info->tile_col = tile_col;
3122
107k
  next_job_info->mi_row = dec_row_mt_sync->mi_rows_decode_started +
3123
107k
                          tile_data->tile_info.mi_row_start;
3124
3125
107k
  dec_row_mt_sync->num_threads_working++;
3126
107k
  dec_row_mt_sync->mi_rows_decode_started += sb_mi_size;
3127
107k
  frame_row_mt_info->mi_rows_decode_started += sb_mi_size;
3128
107k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3129
107k
         frame_row_mt_info->mi_rows_decode_started);
3130
107k
#if CONFIG_MULTITHREAD
3131
107k
  if (frame_row_mt_info->mi_rows_decode_started ==
3132
107k
      frame_row_mt_info->mi_rows_to_decode) {
3133
30.4k
    pthread_cond_broadcast(pbi->row_mt_cond_);
3134
30.4k
  }
3135
107k
#endif
3136
3137
107k
  return 1;
3138
108k
}
3139
3140
static inline void signal_parse_sb_row_done(AV1Decoder *const pbi,
3141
                                            TileDataDec *const tile_data,
3142
145k
                                            const int sb_mi_size) {
3143
145k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3144
145k
#if CONFIG_MULTITHREAD
3145
145k
  pthread_mutex_lock(pbi->row_mt_mutex_);
3146
145k
#endif
3147
145k
  assert(frame_row_mt_info->mi_rows_parse_done >=
3148
145k
         frame_row_mt_info->mi_rows_decode_started);
3149
145k
  tile_data->dec_row_mt_sync.mi_rows_parse_done += sb_mi_size;
3150
145k
  frame_row_mt_info->mi_rows_parse_done += sb_mi_size;
3151
145k
#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
145k
  pthread_cond_signal(pbi->row_mt_cond_);
3157
145k
  pthread_mutex_unlock(pbi->row_mt_mutex_);
3158
145k
#endif
3159
145k
}
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
45.9k
                                     TileDataDec *const tile_data) {
3165
45.9k
  AV1_COMMON *const cm = &pbi->common;
3166
45.9k
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
3167
45.9k
  const int num_planes = av1_num_planes(cm);
3168
45.9k
  const TileInfo *const tile_info = &tile_data->tile_info;
3169
45.9k
  int tile_row = tile_info->tile_row;
3170
45.9k
  DecoderCodingBlock *const dcb = &td->dcb;
3171
45.9k
  MACROBLOCKD *const xd = &dcb->xd;
3172
3173
45.9k
  av1_zero_above_context(cm, xd, tile_info->mi_col_start, tile_info->mi_col_end,
3174
45.9k
                         tile_row);
3175
45.9k
  av1_reset_loop_filter_delta(xd, num_planes);
3176
45.9k
  av1_reset_loop_restoration(xd, num_planes);
3177
3178
192k
  for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
3179
156k
       mi_row += cm->seq_params->mib_size) {
3180
156k
    av1_zero_left_context(xd);
3181
3182
504k
    for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
3183
357k
         mi_col += cm->seq_params->mib_size) {
3184
357k
      set_cb_buffer(pbi, dcb, pbi->cb_buffer_base, num_planes, mi_row, mi_col);
3185
3186
      // Bit-stream parsing of the superblock
3187
357k
      decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
3188
357k
                       cm->seq_params->sb_size, 0x1);
3189
3190
357k
      if (aom_reader_has_overflowed(td->bit_reader)) {
3191
10.2k
        aom_merge_corrupted_flag(&dcb->corrupted, 1);
3192
10.2k
        return;
3193
10.2k
      }
3194
357k
    }
3195
146k
    signal_parse_sb_row_done(pbi, tile_data, sb_mi_size);
3196
146k
  }
3197
3198
35.6k
  int corrupted =
3199
35.6k
      (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
3200
35.6k
  aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
3201
35.6k
}
3202
3203
60.1k
static int row_mt_worker_hook(void *arg1, void *arg2) {
3204
60.1k
  DecWorkerData *const thread_data = (DecWorkerData *)arg1;
3205
60.1k
  AV1Decoder *const pbi = (AV1Decoder *)arg2;
3206
60.1k
  ThreadData *const td = thread_data->td;
3207
60.1k
  uint8_t allow_update_cdf;
3208
60.1k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3209
60.1k
  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
60.1k
  if (setjmp(thread_data->error_info.jmp)) {
3215
675
    thread_data->error_info.setjmp = 0;
3216
675
    thread_data->td->dcb.corrupted = 1;
3217
675
#if CONFIG_MULTITHREAD
3218
675
    pthread_mutex_lock(pbi->row_mt_mutex_);
3219
675
#endif
3220
675
    frame_row_mt_info->row_mt_exit = 1;
3221
675
#if CONFIG_MULTITHREAD
3222
675
    pthread_cond_broadcast(pbi->row_mt_cond_);
3223
675
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3224
675
#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
675
    signal_decoding_done_for_erroneous_row(pbi, &thread_data->td->dcb.xd);
3231
675
    return 0;
3232
675
  }
3233
59.4k
  thread_data->error_info.setjmp = 1;
3234
3235
59.4k
  AV1_COMMON *cm = &pbi->common;
3236
59.4k
  allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
3237
60.1k
  allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
3238
3239
59.4k
  set_decode_func_pointers(td, 0x1);
3240
3241
59.4k
  assert(cm->tiles.cols > 0);
3242
105k
  while (!td->dcb.corrupted) {
3243
92.6k
    TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
3244
3245
92.6k
    if (cur_job_info != NULL) {
3246
45.9k
      const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
3247
45.9k
      TileDataDec *const tile_data = cur_job_info->tile_data;
3248
45.9k
      tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
3249
45.9k
                            allow_update_cdf);
3250
45.9k
#if CONFIG_MULTITHREAD
3251
45.9k
      pthread_mutex_lock(pbi->row_mt_mutex_);
3252
45.9k
#endif
3253
45.9k
      tile_data->dec_row_mt_sync.num_threads_working++;
3254
45.9k
#if CONFIG_MULTITHREAD
3255
45.9k
      pthread_mutex_unlock(pbi->row_mt_mutex_);
3256
45.9k
#endif
3257
      // decode tile
3258
45.9k
      parse_tile_row_mt(pbi, td, tile_data);
3259
45.9k
#if CONFIG_MULTITHREAD
3260
45.9k
      pthread_mutex_lock(pbi->row_mt_mutex_);
3261
45.9k
#endif
3262
45.9k
      tile_data->dec_row_mt_sync.num_threads_working--;
3263
45.9k
#if CONFIG_MULTITHREAD
3264
45.9k
      pthread_mutex_unlock(pbi->row_mt_mutex_);
3265
45.9k
#endif
3266
46.7k
    } else {
3267
46.7k
      break;
3268
46.7k
    }
3269
92.6k
  }
3270
3271
59.4k
  if (td->dcb.corrupted) {
3272
12.6k
    thread_data->error_info.setjmp = 0;
3273
12.6k
#if CONFIG_MULTITHREAD
3274
12.6k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3275
12.6k
#endif
3276
12.6k
    frame_row_mt_info->row_mt_exit = 1;
3277
12.6k
#if CONFIG_MULTITHREAD
3278
12.6k
    pthread_cond_broadcast(pbi->row_mt_cond_);
3279
12.6k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3280
12.6k
#endif
3281
12.6k
    return 0;
3282
12.6k
  }
3283
3284
46.7k
  set_decode_func_pointers(td, 0x2);
3285
3286
154k
  while (1) {
3287
154k
    AV1DecRowMTJobInfo next_job_info;
3288
154k
    int end_of_frame = 0;
3289
3290
154k
#if CONFIG_MULTITHREAD
3291
154k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3292
154k
#endif
3293
174k
    while (!get_next_job_info(pbi, &next_job_info, &end_of_frame)) {
3294
20.0k
#if CONFIG_MULTITHREAD
3295
20.0k
      pthread_cond_wait(pbi->row_mt_cond_, pbi->row_mt_mutex_);
3296
20.0k
#endif
3297
20.0k
    }
3298
154k
#if CONFIG_MULTITHREAD
3299
154k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3300
154k
#endif
3301
3302
154k
    if (end_of_frame) break;
3303
3304
107k
    int tile_row = next_job_info.tile_row;
3305
107k
    int tile_col = next_job_info.tile_col;
3306
107k
    int mi_row = next_job_info.mi_row;
3307
3308
107k
    TileDataDec *tile_data =
3309
107k
        pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3310
107k
    AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3311
3312
107k
    av1_tile_init(&td->dcb.xd.tile, cm, tile_row, tile_col);
3313
107k
    av1_init_macroblockd(cm, &td->dcb.xd);
3314
107k
    td->dcb.xd.error_info = &thread_data->error_info;
3315
3316
107k
    decode_tile_sb_row(pbi, td, &tile_data->tile_info, mi_row);
3317
3318
107k
#if CONFIG_MULTITHREAD
3319
107k
    pthread_mutex_lock(pbi->row_mt_mutex_);
3320
107k
#endif
3321
107k
    dec_row_mt_sync->num_threads_working--;
3322
107k
#if CONFIG_MULTITHREAD
3323
107k
    pthread_mutex_unlock(pbi->row_mt_mutex_);
3324
107k
#endif
3325
107k
  }
3326
46.7k
  thread_data->error_info.setjmp = 0;
3327
46.7k
  return !td->dcb.corrupted;
3328
59.4k
}
3329
3330
// sorts in descending order
3331
11.3k
static int compare_tile_buffers(const void *a, const void *b) {
3332
11.3k
  const TileJobsDec *const buf1 = (const TileJobsDec *)a;
3333
11.3k
  const TileJobsDec *const buf2 = (const TileJobsDec *)b;
3334
11.3k
  return (((int)buf2->tile_buffer->size) - ((int)buf1->tile_buffer->size));
3335
11.3k
}
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
42.7k
                                     int start_tile, int end_tile) {
3341
42.7k
  AV1DecTileMT *tile_mt_info = &pbi->tile_mt_info;
3342
42.7k
  TileJobsDec *tile_job_queue = tile_mt_info->job_queue;
3343
42.7k
  tile_mt_info->jobs_enqueued = 0;
3344
42.7k
  tile_mt_info->jobs_dequeued = 0;
3345
3346
88.5k
  for (int row = tile_rows_start; row < tile_rows_end; row++) {
3347
93.3k
    for (int col = tile_cols_start; col < tile_cols_end; col++) {
3348
47.5k
      if (row * cm->tiles.cols + col < start_tile ||
3349
47.5k
          row * cm->tiles.cols + col > end_tile)
3350
8
        continue;
3351
47.5k
      tile_job_queue->tile_buffer = &pbi->tile_buffers[row][col];
3352
47.5k
      tile_job_queue->tile_data = pbi->tile_data + row * cm->tiles.cols + col;
3353
47.5k
      tile_job_queue++;
3354
47.5k
      tile_mt_info->jobs_enqueued++;
3355
47.5k
    }
3356
45.7k
  }
3357
42.7k
}
3358
3359
static inline void alloc_dec_jobs(AV1DecTileMT *tile_mt_info, AV1_COMMON *cm,
3360
17.9k
                                  int tile_rows, int tile_cols) {
3361
17.9k
  tile_mt_info->alloc_tile_rows = tile_rows;
3362
17.9k
  tile_mt_info->alloc_tile_cols = tile_cols;
3363
17.9k
  int num_tiles = tile_rows * tile_cols;
3364
17.9k
#if CONFIG_MULTITHREAD
3365
17.9k
  {
3366
17.9k
    CHECK_MEM_ERROR(cm, tile_mt_info->job_mutex,
3367
17.9k
                    aom_malloc(sizeof(*tile_mt_info->job_mutex) * num_tiles));
3368
3369
38.7k
    for (int i = 0; i < num_tiles; i++) {
3370
20.7k
      pthread_mutex_init(&tile_mt_info->job_mutex[i], NULL);
3371
20.7k
    }
3372
17.9k
  }
3373
17.9k
#endif
3374
17.9k
  CHECK_MEM_ERROR(cm, tile_mt_info->job_queue,
3375
17.9k
                  aom_malloc(sizeof(*tile_mt_info->job_queue) * num_tiles));
3376
17.9k
}
3377
3378
1.19M
void av1_free_mc_tmp_buf(ThreadData *thread_data) {
3379
1.19M
  int ref;
3380
3.59M
  for (ref = 0; ref < 2; ref++) {
3381
2.39M
    if (thread_data->mc_buf_use_highbd)
3382
745k
      aom_free(CONVERT_TO_SHORTPTR(thread_data->mc_buf[ref]));
3383
1.65M
    else
3384
1.65M
      aom_free(thread_data->mc_buf[ref]);
3385
2.39M
    thread_data->mc_buf[ref] = NULL;
3386
2.39M
  }
3387
1.19M
  thread_data->mc_buf_size = 0;
3388
1.19M
  thread_data->mc_buf_use_highbd = 0;
3389
3390
1.19M
  aom_free(thread_data->tmp_conv_dst);
3391
1.19M
  thread_data->tmp_conv_dst = NULL;
3392
1.19M
  aom_free(thread_data->seg_mask);
3393
1.19M
  thread_data->seg_mask = NULL;
3394
3.59M
  for (int i = 0; i < 2; ++i) {
3395
2.39M
    aom_free(thread_data->tmp_obmc_bufs[i]);
3396
2.39M
    thread_data->tmp_obmc_bufs[i] = NULL;
3397
2.39M
  }
3398
1.19M
}
3399
3400
static inline void allocate_mc_tmp_buf(AV1_COMMON *const cm,
3401
                                       ThreadData *thread_data, int buf_size,
3402
621k
                                       int use_highbd) {
3403
1.86M
  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
1.24M
    if (use_highbd) {
3410
745k
      uint16_t *hbd_mc_buf;
3411
745k
      CHECK_MEM_ERROR(cm, hbd_mc_buf, (uint16_t *)aom_memalign(16, buf_size));
3412
745k
      memset(hbd_mc_buf, 0, buf_size);
3413
745k
      thread_data->mc_buf[ref] = CONVERT_TO_BYTEPTR(hbd_mc_buf);
3414
745k
    } else {
3415
497k
      CHECK_MEM_ERROR(cm, thread_data->mc_buf[ref],
3416
497k
                      (uint8_t *)aom_memalign(16, buf_size));
3417
497k
      memset(thread_data->mc_buf[ref], 0, buf_size);
3418
497k
    }
3419
1.24M
  }
3420
621k
  thread_data->mc_buf_size = buf_size;
3421
621k
  thread_data->mc_buf_use_highbd = use_highbd;
3422
3423
621k
  CHECK_MEM_ERROR(cm, thread_data->tmp_conv_dst,
3424
621k
                  aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
3425
621k
                                       sizeof(*thread_data->tmp_conv_dst)));
3426
621k
  CHECK_MEM_ERROR(cm, thread_data->seg_mask,
3427
621k
                  (uint8_t *)aom_memalign(
3428
621k
                      16, 2 * MAX_SB_SQUARE * sizeof(*thread_data->seg_mask)));
3429
3430
1.86M
  for (int i = 0; i < 2; ++i) {
3431
1.24M
    CHECK_MEM_ERROR(
3432
1.24M
        cm, thread_data->tmp_obmc_bufs[i],
3433
1.24M
        aom_memalign(16, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
3434
1.24M
                             sizeof(*thread_data->tmp_obmc_bufs[i])));
3435
1.24M
  }
3436
621k
}
3437
3438
static inline void reset_dec_workers(AV1Decoder *pbi, AVxWorkerHook worker_hook,
3439
42.7k
                                     int num_workers) {
3440
42.7k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3441
3442
  // Reset tile decoding hook
3443
102k
  for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
3444
60.1k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3445
60.1k
    DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3446
60.1k
    thread_data->td->dcb = pbi->dcb;
3447
60.1k
    thread_data->td->dcb.corrupted = 0;
3448
60.1k
    thread_data->td->dcb.mc_buf[0] = thread_data->td->mc_buf[0];
3449
60.1k
    thread_data->td->dcb.mc_buf[1] = thread_data->td->mc_buf[1];
3450
60.1k
    thread_data->td->dcb.xd.tmp_conv_dst = thread_data->td->tmp_conv_dst;
3451
60.1k
    if (worker_idx)
3452
17.3k
      thread_data->td->dcb.xd.seg_mask = thread_data->td->seg_mask;
3453
180k
    for (int j = 0; j < 2; ++j) {
3454
120k
      thread_data->td->dcb.xd.tmp_obmc_bufs[j] =
3455
120k
          thread_data->td->tmp_obmc_bufs[j];
3456
120k
    }
3457
60.1k
    winterface->sync(worker);
3458
3459
60.1k
    worker->hook = worker_hook;
3460
60.1k
    worker->data1 = thread_data;
3461
60.1k
    worker->data2 = pbi;
3462
60.1k
  }
3463
#if CONFIG_ACCOUNTING
3464
  if (pbi->acct_enabled) {
3465
    aom_accounting_reset(&pbi->accounting);
3466
  }
3467
#endif
3468
42.7k
}
3469
3470
static inline void launch_dec_workers(AV1Decoder *pbi, const uint8_t *data_end,
3471
42.7k
                                      int num_workers) {
3472
42.7k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3473
3474
102k
  for (int worker_idx = num_workers - 1; worker_idx >= 0; --worker_idx) {
3475
60.1k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3476
60.1k
    DecWorkerData *const thread_data = (DecWorkerData *)worker->data1;
3477
3478
60.1k
    thread_data->data_end = data_end;
3479
3480
60.1k
    worker->had_error = 0;
3481
60.1k
    if (worker_idx == 0) {
3482
42.7k
      winterface->execute(worker);
3483
42.7k
    } else {
3484
17.3k
      winterface->launch(worker);
3485
17.3k
    }
3486
60.1k
  }
3487
42.7k
}
3488
3489
42.7k
static inline void sync_dec_workers(AV1Decoder *pbi, int num_workers) {
3490
42.7k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3491
42.7k
  int corrupted = 0;
3492
3493
102k
  for (int worker_idx = num_workers; worker_idx > 0; --worker_idx) {
3494
60.1k
    AVxWorker *const worker = &pbi->tile_workers[worker_idx - 1];
3495
60.1k
    aom_merge_corrupted_flag(&corrupted, !winterface->sync(worker));
3496
60.1k
  }
3497
3498
42.7k
  pbi->dcb.corrupted = corrupted;
3499
42.7k
}
3500
3501
42.8k
static inline void decode_mt_init(AV1Decoder *pbi) {
3502
42.8k
  AV1_COMMON *const cm = &pbi->common;
3503
42.8k
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3504
42.8k
  int worker_idx;
3505
3506
  // Create workers and thread_data
3507
42.8k
  if (pbi->num_workers == 0) {
3508
17.7k
    const int num_threads = pbi->max_threads;
3509
17.7k
    CHECK_MEM_ERROR(cm, pbi->tile_workers,
3510
17.7k
                    aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
3511
17.7k
    CHECK_MEM_ERROR(cm, pbi->thread_data,
3512
17.7k
                    aom_calloc(num_threads, sizeof(*pbi->thread_data)));
3513
3514
585k
    for (worker_idx = 0; worker_idx < num_threads; ++worker_idx) {
3515
567k
      AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3516
567k
      DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3517
3518
567k
      winterface->init(worker);
3519
567k
      worker->thread_name = "aom tile worker";
3520
567k
      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
567k
      ++pbi->num_workers;
3525
3526
567k
      if (worker_idx != 0) {
3527
        // Allocate thread data.
3528
550k
        CHECK_MEM_ERROR(cm, thread_data->td,
3529
550k
                        aom_memalign(32, sizeof(*thread_data->td)));
3530
550k
        av1_zero(*thread_data->td);
3531
550k
      } else {
3532
        // Main thread acts as a worker and uses the thread data in pbi
3533
17.7k
        thread_data->td = &pbi->td;
3534
17.7k
      }
3535
567k
      thread_data->error_info.error_code = AOM_CODEC_OK;
3536
567k
      thread_data->error_info.setjmp = 0;
3537
567k
    }
3538
17.7k
  }
3539
42.8k
  const int use_highbd = cm->seq_params->use_highbitdepth;
3540
42.8k
  const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
3541
1.37M
  for (worker_idx = 1; worker_idx < pbi->max_threads; ++worker_idx) {
3542
1.32M
    DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3543
1.32M
    if (thread_data->td->mc_buf_size != buf_size) {
3544
592k
      av1_free_mc_tmp_buf(thread_data->td);
3545
592k
      allocate_mc_tmp_buf(cm, thread_data->td, buf_size, use_highbd);
3546
592k
    }
3547
1.32M
  }
3548
42.8k
}
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
42.7k
                                 int start_tile, int end_tile) {
3554
42.7k
  AV1_COMMON *const cm = &pbi->common;
3555
42.7k
  if (pbi->tile_mt_info.alloc_tile_cols != tile_cols ||
3556
42.7k
      pbi->tile_mt_info.alloc_tile_rows != tile_rows) {
3557
17.9k
    av1_dealloc_dec_jobs(&pbi->tile_mt_info);
3558
17.9k
    alloc_dec_jobs(&pbi->tile_mt_info, cm, tile_rows, tile_cols);
3559
17.9k
  }
3560
42.7k
  enqueue_tile_jobs(pbi, cm, tile_rows_start, tile_rows_end, tile_cols_start,
3561
42.7k
                    tile_cols_end, start_tile, end_tile);
3562
42.7k
  qsort(pbi->tile_mt_info.job_queue, pbi->tile_mt_info.jobs_enqueued,
3563
42.7k
        sizeof(pbi->tile_mt_info.job_queue[0]), compare_tile_buffers);
3564
42.7k
}
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
42.7k
static inline void dec_alloc_cb_buf(AV1Decoder *pbi) {
3667
42.7k
  AV1_COMMON *const cm = &pbi->common;
3668
42.7k
  int size = ((cm->mi_params.mi_rows >> cm->seq_params->mib_size_log2) + 1) *
3669
42.7k
             ((cm->mi_params.mi_cols >> cm->seq_params->mib_size_log2) + 1);
3670
3671
42.7k
  if (pbi->cb_buffer_alloc_size < size) {
3672
19.1k
    av1_dec_free_cb_buf(pbi);
3673
19.1k
    CHECK_MEM_ERROR(cm, pbi->cb_buffer_base,
3674
19.1k
                    aom_memalign(32, sizeof(*pbi->cb_buffer_base) * size));
3675
19.1k
    memset(pbi->cb_buffer_base, 0, sizeof(*pbi->cb_buffer_base) * size);
3676
19.1k
    pbi->cb_buffer_alloc_size = size;
3677
19.1k
  }
3678
42.7k
}
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
42.7k
                                     int end_tile, int max_sb_rows) {
3684
42.7k
  AV1_COMMON *const cm = &pbi->common;
3685
42.7k
  AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3686
3687
42.7k
  frame_row_mt_info->tile_rows_start = tile_rows_start;
3688
42.7k
  frame_row_mt_info->tile_rows_end = tile_rows_end;
3689
42.7k
  frame_row_mt_info->tile_cols_start = tile_cols_start;
3690
42.7k
  frame_row_mt_info->tile_cols_end = tile_cols_end;
3691
42.7k
  frame_row_mt_info->start_tile = start_tile;
3692
42.7k
  frame_row_mt_info->end_tile = end_tile;
3693
42.7k
  frame_row_mt_info->mi_rows_to_decode = 0;
3694
42.7k
  frame_row_mt_info->mi_rows_parse_done = 0;
3695
42.7k
  frame_row_mt_info->mi_rows_decode_started = 0;
3696
42.7k
  frame_row_mt_info->row_mt_exit = 0;
3697
3698
88.5k
  for (int tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3699
93.2k
    for (int tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
3700
47.5k
      if (tile_row * cm->tiles.cols + tile_col < start_tile ||
3701
47.5k
          tile_row * cm->tiles.cols + tile_col > end_tile)
3702
8
        continue;
3703
3704
47.5k
      TileDataDec *const tile_data =
3705
47.5k
          pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
3706
47.5k
      const TileInfo *const tile_info = &tile_data->tile_info;
3707
3708
47.5k
      tile_data->dec_row_mt_sync.mi_rows_parse_done = 0;
3709
47.5k
      tile_data->dec_row_mt_sync.mi_rows_decode_started = 0;
3710
47.5k
      tile_data->dec_row_mt_sync.num_threads_working = 0;
3711
47.5k
      tile_data->dec_row_mt_sync.mi_rows =
3712
47.5k
          ALIGN_POWER_OF_TWO(tile_info->mi_row_end - tile_info->mi_row_start,
3713
47.5k
                             cm->seq_params->mib_size_log2);
3714
47.5k
      tile_data->dec_row_mt_sync.mi_cols =
3715
47.5k
          ALIGN_POWER_OF_TWO(tile_info->mi_col_end - tile_info->mi_col_start,
3716
47.5k
                             cm->seq_params->mib_size_log2);
3717
47.5k
      tile_data->dec_row_mt_sync.intrabc_extra_top_right_sb_delay =
3718
47.5k
          av1_get_intrabc_extra_top_right_sb_delay(cm);
3719
3720
47.5k
      frame_row_mt_info->mi_rows_to_decode +=
3721
47.5k
          tile_data->dec_row_mt_sync.mi_rows;
3722
3723
      // Initialize cur_sb_col to -1 for all SB rows.
3724
47.5k
      memset(tile_data->dec_row_mt_sync.cur_sb_col, -1,
3725
47.5k
             sizeof(*tile_data->dec_row_mt_sync.cur_sb_col) * max_sb_rows);
3726
47.5k
    }
3727
45.7k
  }
3728
3729
42.7k
#if CONFIG_MULTITHREAD
3730
42.7k
  if (pbi->row_mt_mutex_ == NULL) {
3731
17.6k
    CHECK_MEM_ERROR(cm, pbi->row_mt_mutex_,
3732
17.6k
                    aom_malloc(sizeof(*(pbi->row_mt_mutex_))));
3733
17.6k
    if (pbi->row_mt_mutex_) {
3734
17.6k
      pthread_mutex_init(pbi->row_mt_mutex_, NULL);
3735
17.6k
    }
3736
17.6k
  }
3737
3738
42.7k
  if (pbi->row_mt_cond_ == NULL) {
3739
17.6k
    CHECK_MEM_ERROR(cm, pbi->row_mt_cond_,
3740
17.6k
                    aom_malloc(sizeof(*(pbi->row_mt_cond_))));
3741
17.6k
    if (pbi->row_mt_cond_) {
3742
17.6k
      pthread_cond_init(pbi->row_mt_cond_, NULL);
3743
17.6k
    }
3744
17.6k
  }
3745
42.7k
#endif
3746
42.7k
}
3747
3748
static const uint8_t *decode_tiles_row_mt(AV1Decoder *pbi, const uint8_t *data,
3749
                                          const uint8_t *data_end,
3750
42.8k
                                          int start_tile, int end_tile) {
3751
42.8k
  AV1_COMMON *const cm = &pbi->common;
3752
42.8k
  CommonTileParams *const tiles = &cm->tiles;
3753
42.8k
  const int tile_cols = tiles->cols;
3754
42.8k
  const int tile_rows = tiles->rows;
3755
42.8k
  const int n_tiles = tile_cols * tile_rows;
3756
42.8k
  TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
3757
42.8k
  const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
3758
42.8k
  const int single_row = pbi->dec_tile_row >= 0;
3759
42.8k
  const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
3760
42.8k
  const int single_col = pbi->dec_tile_col >= 0;
3761
42.8k
  int tile_rows_start;
3762
42.8k
  int tile_rows_end;
3763
42.8k
  int tile_cols_start;
3764
42.8k
  int tile_cols_end;
3765
42.8k
  int tile_count_tg;
3766
42.8k
  int num_workers = 0;
3767
42.8k
  int max_threads;
3768
42.8k
  const uint8_t *raw_data_end = NULL;
3769
42.8k
  int max_sb_rows = 0;
3770
3771
42.8k
  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
42.8k
  } else {
3777
42.8k
    tile_rows_start = 0;
3778
42.8k
    tile_rows_end = tile_rows;
3779
42.8k
    tile_cols_start = 0;
3780
42.8k
    tile_cols_end = tile_cols;
3781
42.8k
  }
3782
42.8k
  tile_count_tg = end_tile - start_tile + 1;
3783
42.8k
  max_threads = pbi->max_threads;
3784
3785
  // No tiles to decode.
3786
42.8k
  if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
3787
      // First tile is larger than end_tile.
3788
42.8k
      tile_rows_start * tile_cols + tile_cols_start > end_tile ||
3789
      // Last tile is smaller than start_tile.
3790
42.8k
      (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
3791
0
    return data;
3792
3793
42.8k
  assert(tile_rows <= MAX_TILE_ROWS);
3794
42.8k
  assert(tile_cols <= MAX_TILE_COLS);
3795
42.8k
  assert(tile_count_tg > 0);
3796
42.8k
  assert(max_threads > 0);
3797
42.8k
  assert(start_tile <= end_tile);
3798
42.8k
  assert(start_tile >= 0 && end_tile < n_tiles);
3799
3800
42.8k
  (void)tile_count_tg;
3801
3802
42.8k
  decode_mt_init(pbi);
3803
3804
  // get tile size in tile group
3805
42.8k
#if EXT_TILE_DEBUG
3806
42.8k
  if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
3807
42.8k
  if (tiles->large_scale)
3808
0
    raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
3809
42.8k
  else
3810
42.8k
#endif  // EXT_TILE_DEBUG
3811
42.8k
    get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
3812
3813
42.8k
  if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
3814
17.9k
    if (pbi->tile_data != NULL) {
3815
613
      for (int i = 0; i < pbi->allocated_tiles; i++) {
3816
344
        TileDataDec *const tile_data = pbi->tile_data + i;
3817
344
        av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
3818
344
      }
3819
269
    }
3820
17.9k
    decoder_alloc_tile_data(pbi, n_tiles);
3821
17.9k
  }
3822
42.8k
  if (pbi->dcb.xd.seg_mask == NULL)
3823
42.8k
    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
3824
42.8k
                    (uint8_t *)aom_memalign(
3825
42.8k
                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
3826
3827
88.6k
  for (int row = 0; row < tile_rows; row++) {
3828
93.3k
    for (int col = 0; col < tile_cols; col++) {
3829
47.5k
      TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
3830
47.5k
      av1_tile_init(&tile_data->tile_info, cm, row, col);
3831
3832
47.5k
      max_sb_rows = AOMMAX(max_sb_rows,
3833
47.5k
                           av1_get_sb_rows_in_tile(cm, &tile_data->tile_info));
3834
47.5k
      num_workers += get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info);
3835
47.5k
    }
3836
45.7k
  }
3837
42.8k
  num_workers = AOMMIN(num_workers, max_threads);
3838
3839
42.8k
  if (pbi->allocated_row_mt_sync_rows != max_sb_rows) {
3840
41.1k
    for (int i = 0; i < n_tiles; ++i) {
3841
21.9k
      TileDataDec *const tile_data = pbi->tile_data + i;
3842
21.9k
      av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
3843
21.9k
      dec_row_mt_alloc(&tile_data->dec_row_mt_sync, cm, max_sb_rows);
3844
21.9k
    }
3845
19.1k
    pbi->allocated_row_mt_sync_rows = max_sb_rows;
3846
19.1k
  }
3847
3848
42.8k
  tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
3849
42.8k
                tile_cols_start, tile_cols_end, start_tile, end_tile);
3850
3851
42.8k
  dec_alloc_cb_buf(pbi);
3852
3853
42.8k
  row_mt_frame_init(pbi, tile_rows_start, tile_rows_end, tile_cols_start,
3854
42.8k
                    tile_cols_end, start_tile, end_tile, max_sb_rows);
3855
3856
42.8k
  reset_dec_workers(pbi, row_mt_worker_hook, num_workers);
3857
42.8k
  launch_dec_workers(pbi, data_end, num_workers);
3858
42.8k
  sync_dec_workers(pbi, num_workers);
3859
3860
42.8k
  if (pbi->dcb.corrupted)
3861
12.3k
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
3862
12.3k
                       "Failed to decode tile data");
3863
3864
42.8k
  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
42.8k
  TileDataDec *const tile_data = pbi->tile_data + end_tile;
3873
3874
42.8k
  return aom_reader_find_end(&tile_data->bit_reader);
3875
42.8k
}
3876
3877
443
static inline void error_handler(void *data) {
3878
443
  AV1_COMMON *const cm = (AV1_COMMON *)data;
3879
443
  aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
3880
443
}
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
35.0k
                                 struct aom_internal_error_info *error_info) {
3889
35.0k
  const int high_bitdepth = aom_rb_read_bit(rb);
3890
35.0k
  if (seq_params->profile == PROFILE_2 && high_bitdepth) {
3891
6.46k
    const int twelve_bit = aom_rb_read_bit(rb);
3892
6.46k
    seq_params->bit_depth = twelve_bit ? AOM_BITS_12 : AOM_BITS_10;
3893
28.5k
  } else if (seq_params->profile <= PROFILE_2) {
3894
28.5k
    seq_params->bit_depth = high_bitdepth ? AOM_BITS_10 : AOM_BITS_8;
3895
28.5k
  } else {
3896
0
    aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
3897
0
                       "Unsupported profile/bit-depth combination");
3898
0
  }
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
35.0k
}
3906
3907
static void read_film_grain_params(AV1_COMMON *cm,
3908
18.7k
                                   struct aom_read_bit_buffer *rb) {
3909
18.7k
  aom_film_grain_t *pars = &cm->film_grain_params;
3910
18.7k
  const SequenceHeader *const seq_params = cm->seq_params;
3911
3912
18.7k
  pars->apply_grain = aom_rb_read_bit(rb);
3913
18.7k
  if (!pars->apply_grain) {
3914
15.5k
    memset(pars, 0, sizeof(*pars));
3915
15.5k
    return;
3916
15.5k
  }
3917
3918
3.25k
  pars->random_seed = aom_rb_read_literal(rb, 16);
3919
3.25k
  if (cm->current_frame.frame_type == INTER_FRAME)
3920
459
    pars->update_parameters = aom_rb_read_bit(rb);
3921
2.79k
  else
3922
2.79k
    pars->update_parameters = 1;
3923
3924
3.25k
  pars->bit_depth = seq_params->bit_depth;
3925
3926
3.25k
  if (!pars->update_parameters) {
3927
    // inherit parameters from a previous reference frame
3928
268
    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
268
    int found = 0;
3933
848
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3934
837
      if (film_grain_params_ref_idx == cm->remapped_ref_idx[i]) {
3935
257
        found = 1;
3936
257
        break;
3937
257
      }
3938
837
    }
3939
268
    if (!found) {
3940
11
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3941
11
                         "Invalid film grain reference idx %d. ref_frame_idx = "
3942
11
                         "{%d, %d, %d, %d, %d, %d, %d}",
3943
11
                         film_grain_params_ref_idx, cm->remapped_ref_idx[0],
3944
11
                         cm->remapped_ref_idx[1], cm->remapped_ref_idx[2],
3945
11
                         cm->remapped_ref_idx[3], cm->remapped_ref_idx[4],
3946
11
                         cm->remapped_ref_idx[5], cm->remapped_ref_idx[6]);
3947
11
    }
3948
268
    RefCntBuffer *const buf = cm->ref_frame_map[film_grain_params_ref_idx];
3949
268
    if (buf == NULL) {
3950
0
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3951
0
                         "Invalid Film grain reference idx");
3952
0
    }
3953
268
    if (!buf->film_grain_params_present) {
3954
8
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3955
8
                         "Film grain reference parameters not available");
3956
8
    }
3957
268
    uint16_t random_seed = pars->random_seed;
3958
268
    *pars = buf->film_grain_params;   // inherit paramaters
3959
268
    pars->random_seed = random_seed;  // with new random seed
3960
268
    return;
3961
268
  }
3962
3963
  // Scaling functions parameters
3964
2.98k
  pars->num_y_points = aom_rb_read_literal(rb, 4);  // max 14
3965
2.98k
  if (pars->num_y_points > 14)
3966
9
    aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3967
9
                       "Number of points for film grain luma scaling function "
3968
9
                       "exceeds the maximum value.");
3969
3.67k
  for (int i = 0; i < pars->num_y_points; i++) {
3970
685
    pars->scaling_points_y[i][0] = aom_rb_read_literal(rb, 8);
3971
685
    if (i && pars->scaling_points_y[i - 1][0] >= pars->scaling_points_y[i][0])
3972
34
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3973
34
                         "First coordinate of the scaling function points "
3974
34
                         "shall be increasing.");
3975
685
    pars->scaling_points_y[i][1] = aom_rb_read_literal(rb, 8);
3976
685
  }
3977
3978
2.98k
  if (!seq_params->monochrome)
3979
2.39k
    pars->chroma_scaling_from_luma = aom_rb_read_bit(rb);
3980
590
  else
3981
590
    pars->chroma_scaling_from_luma = 0;
3982
3983
2.98k
  if (seq_params->monochrome || pars->chroma_scaling_from_luma ||
3984
2.98k
      ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
3985
2.03k
       (pars->num_y_points == 0))) {
3986
2.03k
    pars->num_cb_points = 0;
3987
2.03k
    pars->num_cr_points = 0;
3988
2.03k
  } else {
3989
958
    pars->num_cb_points = aom_rb_read_literal(rb, 4);  // max 10
3990
958
    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
1.26k
    for (int i = 0; i < pars->num_cb_points; i++) {
3995
303
      pars->scaling_points_cb[i][0] = aom_rb_read_literal(rb, 8);
3996
303
      if (i &&
3997
303
          pars->scaling_points_cb[i - 1][0] >= pars->scaling_points_cb[i][0])
3998
9
        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
3999
9
                           "First coordinate of the scaling function points "
4000
9
                           "shall be increasing.");
4001
303
      pars->scaling_points_cb[i][1] = aom_rb_read_literal(rb, 8);
4002
303
    }
4003
4004
958
    pars->num_cr_points = aom_rb_read_literal(rb, 4);  // max 10
4005
958
    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
1.16k
    for (int i = 0; i < pars->num_cr_points; i++) {
4010
205
      pars->scaling_points_cr[i][0] = aom_rb_read_literal(rb, 8);
4011
205
      if (i &&
4012
205
          pars->scaling_points_cr[i - 1][0] >= pars->scaling_points_cr[i][0])
4013
11
        aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4014
11
                           "First coordinate of the scaling function points "
4015
11
                           "shall be increasing.");
4016
205
      pars->scaling_points_cr[i][1] = aom_rb_read_literal(rb, 8);
4017
205
    }
4018
4019
958
    if ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
4020
958
        (((pars->num_cb_points == 0) && (pars->num_cr_points != 0)) ||
4021
19
         ((pars->num_cb_points != 0) && (pars->num_cr_points == 0))))
4022
4
      aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4023
4
                         "In YCbCr 4:2:0, film grain shall be applied "
4024
4
                         "to both chroma components or neither.");
4025
958
  }
4026
4027
2.98k
  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
2.98k
  pars->ar_coeff_lag = aom_rb_read_literal(rb, 2);
4034
4035
2.98k
  int num_pos_luma = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1);
4036
2.98k
  int num_pos_chroma = num_pos_luma;
4037
2.98k
  if (pars->num_y_points > 0) ++num_pos_chroma;
4038
4039
2.98k
  if (pars->num_y_points)
4040
2.31k
    for (int i = 0; i < num_pos_luma; i++)
4041
1.94k
      pars->ar_coeffs_y[i] = aom_rb_read_literal(rb, 8) - 128;
4042
4043
2.98k
  if (pars->num_cb_points || pars->chroma_scaling_from_luma)
4044
5.95k
    for (int i = 0; i < num_pos_chroma; i++)
4045
5.42k
      pars->ar_coeffs_cb[i] = aom_rb_read_literal(rb, 8) - 128;
4046
4047
2.98k
  if (pars->num_cr_points || pars->chroma_scaling_from_luma)
4048
5.77k
    for (int i = 0; i < num_pos_chroma; i++)
4049
5.29k
      pars->ar_coeffs_cr[i] = aom_rb_read_literal(rb, 8) - 128;
4050
4051
2.98k
  pars->ar_coeff_shift = aom_rb_read_literal(rb, 2) + 6;  // 6 + value
4052
4053
2.98k
  pars->grain_scale_shift = aom_rb_read_literal(rb, 2);
4054
4055
2.98k
  if (pars->num_cb_points) {
4056
134
    pars->cb_mult = aom_rb_read_literal(rb, 8);
4057
134
    pars->cb_luma_mult = aom_rb_read_literal(rb, 8);
4058
134
    pars->cb_offset = aom_rb_read_literal(rb, 9);
4059
134
  }
4060
4061
2.98k
  if (pars->num_cr_points) {
4062
94
    pars->cr_mult = aom_rb_read_literal(rb, 8);
4063
94
    pars->cr_luma_mult = aom_rb_read_literal(rb, 8);
4064
94
    pars->cr_offset = aom_rb_read_literal(rb, 9);
4065
94
  }
4066
4067
2.98k
  pars->overlap_flag = aom_rb_read_bit(rb);
4068
4069
2.98k
  pars->clip_to_restricted_range = aom_rb_read_bit(rb);
4070
2.98k
}
4071
4072
static inline void read_film_grain(AV1_COMMON *cm,
4073
54.7k
                                   struct aom_read_bit_buffer *rb) {
4074
54.7k
  if (cm->seq_params->film_grain_params_present &&
4075
54.7k
      (cm->show_frame || cm->showable_frame)) {
4076
18.7k
    read_film_grain_params(cm, rb);
4077
35.9k
  } else {
4078
35.9k
    memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
4079
35.9k
  }
4080
54.7k
  cm->film_grain_params.bit_depth = cm->seq_params->bit_depth;
4081
54.7k
  memcpy(&cm->cur_frame->film_grain_params, &cm->film_grain_params,
4082
54.7k
         sizeof(aom_film_grain_t));
4083
54.7k
}
4084
4085
void av1_read_color_config(struct aom_read_bit_buffer *rb,
4086
                           int allow_lowbitdepth, SequenceHeader *seq_params,
4087
35.0k
                           struct aom_internal_error_info *error_info) {
4088
35.0k
  read_bitdepth(rb, seq_params, error_info);
4089
4090
35.0k
  seq_params->use_highbitdepth =
4091
35.0k
      seq_params->bit_depth > AOM_BITS_8 || !allow_lowbitdepth;
4092
  // monochrome bit (not needed for PROFILE_1)
4093
35.0k
  const int is_monochrome =
4094
35.0k
      seq_params->profile != PROFILE_1 ? aom_rb_read_bit(rb) : 0;
4095
35.0k
  seq_params->monochrome = is_monochrome;
4096
35.0k
  int color_description_present_flag = aom_rb_read_bit(rb);
4097
35.0k
  if (color_description_present_flag) {
4098
6.75k
    seq_params->color_primaries = aom_rb_read_literal(rb, 8);
4099
6.75k
    seq_params->transfer_characteristics = aom_rb_read_literal(rb, 8);
4100
6.75k
    seq_params->matrix_coefficients = aom_rb_read_literal(rb, 8);
4101
28.2k
  } else {
4102
28.2k
    seq_params->color_primaries = AOM_CICP_CP_UNSPECIFIED;
4103
28.2k
    seq_params->transfer_characteristics = AOM_CICP_TC_UNSPECIFIED;
4104
28.2k
    seq_params->matrix_coefficients = AOM_CICP_MC_UNSPECIFIED;
4105
28.2k
  }
4106
35.0k
  if (is_monochrome) {
4107
    // [16,235] (including xvycc) vs [0,255] range
4108
8.42k
    seq_params->color_range = aom_rb_read_bit(rb);
4109
8.42k
    seq_params->subsampling_y = seq_params->subsampling_x = 1;
4110
8.42k
    seq_params->chroma_sample_position = AOM_CSP_UNKNOWN;
4111
8.42k
    seq_params->separate_uv_delta_q = 0;
4112
8.42k
    return;
4113
8.42k
  }
4114
26.6k
  if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
4115
26.6k
      seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
4116
26.6k
      seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
4117
1.01k
    seq_params->subsampling_y = seq_params->subsampling_x = 0;
4118
1.01k
    seq_params->color_range = 1;  // assume full color-range
4119
1.01k
    if (!(seq_params->profile == PROFILE_1 ||
4120
1.01k
          (seq_params->profile == PROFILE_2 &&
4121
44
           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
25.6k
  } else {
4127
    // [16,235] (including xvycc) vs [0,255] range
4128
25.6k
    seq_params->color_range = aom_rb_read_bit(rb);
4129
25.6k
    if (seq_params->profile == PROFILE_0) {
4130
      // 420 only
4131
9.59k
      seq_params->subsampling_x = seq_params->subsampling_y = 1;
4132
16.0k
    } else if (seq_params->profile == PROFILE_1) {
4133
      // 444 only
4134
8.43k
      seq_params->subsampling_x = seq_params->subsampling_y = 0;
4135
8.43k
    } else {
4136
7.58k
      assert(seq_params->profile == PROFILE_2);
4137
7.58k
      if (seq_params->bit_depth == AOM_BITS_12) {
4138
5.45k
        seq_params->subsampling_x = aom_rb_read_bit(rb);
4139
5.45k
        if (seq_params->subsampling_x)
4140
450
          seq_params->subsampling_y = aom_rb_read_bit(rb);  // 422 or 420
4141
5.00k
        else
4142
5.00k
          seq_params->subsampling_y = 0;  // 444
4143
5.45k
      } else {
4144
        // 422
4145
2.13k
        seq_params->subsampling_x = 1;
4146
2.13k
        seq_params->subsampling_y = 0;
4147
2.13k
      }
4148
7.58k
    }
4149
25.6k
    if (seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY &&
4150
25.6k
        (seq_params->subsampling_x || seq_params->subsampling_y)) {
4151
2
      aom_internal_error(
4152
2
          error_info, AOM_CODEC_UNSUP_BITSTREAM,
4153
2
          "Identity CICP Matrix incompatible with non 4:4:4 color sampling");
4154
2
    }
4155
25.6k
    if (seq_params->subsampling_x && seq_params->subsampling_y) {
4156
9.64k
      seq_params->chroma_sample_position = aom_rb_read_literal(rb, 2);
4157
9.64k
    }
4158
25.6k
  }
4159
26.6k
  seq_params->separate_uv_delta_q = aom_rb_read_bit(rb);
4160
26.6k
}
4161
4162
void av1_read_timing_info_header(aom_timing_info_t *timing_info,
4163
                                 struct aom_internal_error_info *error,
4164
729
                                 struct aom_read_bit_buffer *rb) {
4165
729
  timing_info->num_units_in_display_tick =
4166
729
      aom_rb_read_unsigned_literal(rb,
4167
729
                                   32);  // Number of units in a display tick
4168
729
  timing_info->time_scale = aom_rb_read_unsigned_literal(rb, 32);  // Time scale
4169
729
  if (timing_info->num_units_in_display_tick == 0 ||
4170
729
      timing_info->time_scale == 0) {
4171
4
    aom_internal_error(
4172
4
        error, AOM_CODEC_UNSUP_BITSTREAM,
4173
4
        "num_units_in_display_tick and time_scale must be greater than 0.");
4174
4
  }
4175
729
  timing_info->equal_picture_interval =
4176
729
      aom_rb_read_bit(rb);  // Equal picture interval bit
4177
729
  if (timing_info->equal_picture_interval) {
4178
77
    const uint32_t num_ticks_per_picture_minus_1 = aom_rb_read_uvlc(rb);
4179
77
    if (num_ticks_per_picture_minus_1 == UINT32_MAX) {
4180
2
      aom_internal_error(
4181
2
          error, AOM_CODEC_UNSUP_BITSTREAM,
4182
2
          "num_ticks_per_picture_minus_1 cannot be (1 << 32) - 1.");
4183
2
    }
4184
77
    timing_info->num_ticks_per_picture = num_ticks_per_picture_minus_1 + 1;
4185
77
  }
4186
729
}
4187
4188
void av1_read_decoder_model_info(aom_dec_model_info_t *decoder_model_info,
4189
650
                                 struct aom_read_bit_buffer *rb) {
4190
650
  decoder_model_info->encoder_decoder_buffer_delay_length =
4191
650
      aom_rb_read_literal(rb, 5) + 1;
4192
650
  decoder_model_info->num_units_in_decoding_tick =
4193
650
      aom_rb_read_unsigned_literal(rb,
4194
650
                                   32);  // Number of units in a decoding tick
4195
650
  decoder_model_info->buffer_removal_time_length =
4196
650
      aom_rb_read_literal(rb, 5) + 1;
4197
650
  decoder_model_info->frame_presentation_time_length =
4198
650
      aom_rb_read_literal(rb, 5) + 1;
4199
650
}
4200
4201
void av1_read_op_parameters_info(aom_dec_model_op_parameters_t *op_params,
4202
                                 int buffer_delay_length,
4203
820
                                 struct aom_read_bit_buffer *rb) {
4204
820
  op_params->decoder_buffer_delay =
4205
820
      aom_rb_read_unsigned_literal(rb, buffer_delay_length);
4206
820
  op_params->encoder_buffer_delay =
4207
820
      aom_rb_read_unsigned_literal(rb, buffer_delay_length);
4208
820
  op_params->low_delay_mode_flag = aom_rb_read_bit(rb);
4209
820
}
4210
4211
static inline void read_temporal_point_info(AV1_COMMON *const cm,
4212
17
                                            struct aom_read_bit_buffer *rb) {
4213
17
  cm->frame_presentation_time = aom_rb_read_unsigned_literal(
4214
17
      rb, cm->seq_params->decoder_model_info.frame_presentation_time_length);
4215
17
}
4216
4217
void av1_read_sequence_header(AV1_COMMON *cm, struct aom_read_bit_buffer *rb,
4218
35.0k
                              SequenceHeader *seq_params) {
4219
35.0k
  const int num_bits_width = aom_rb_read_literal(rb, 4) + 1;
4220
35.0k
  const int num_bits_height = aom_rb_read_literal(rb, 4) + 1;
4221
35.0k
  const int max_frame_width = aom_rb_read_literal(rb, num_bits_width) + 1;
4222
35.0k
  const int max_frame_height = aom_rb_read_literal(rb, num_bits_height) + 1;
4223
4224
35.0k
  seq_params->num_bits_width = num_bits_width;
4225
35.0k
  seq_params->num_bits_height = num_bits_height;
4226
35.0k
  seq_params->max_frame_width = max_frame_width;
4227
35.0k
  seq_params->max_frame_height = max_frame_height;
4228
4229
35.0k
  if (seq_params->reduced_still_picture_hdr) {
4230
22.0k
    seq_params->frame_id_numbers_present_flag = 0;
4231
22.0k
  } else {
4232
13.0k
    seq_params->frame_id_numbers_present_flag = aom_rb_read_bit(rb);
4233
13.0k
  }
4234
35.0k
  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
609
    seq_params->delta_frame_id_length = aom_rb_read_literal(rb, 4) + 2;
4239
609
    seq_params->frame_id_length =
4240
609
        aom_rb_read_literal(rb, 3) + seq_params->delta_frame_id_length + 1;
4241
609
    if (seq_params->frame_id_length > 16)
4242
11
      aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
4243
11
                         "Invalid frame_id_length");
4244
609
  }
4245
4246
35.0k
  setup_sb_size(seq_params, rb);
4247
4248
35.0k
  seq_params->enable_filter_intra = aom_rb_read_bit(rb);
4249
35.0k
  seq_params->enable_intra_edge_filter = aom_rb_read_bit(rb);
4250
4251
35.0k
  if (seq_params->reduced_still_picture_hdr) {
4252
22.0k
    seq_params->enable_interintra_compound = 0;
4253
22.0k
    seq_params->enable_masked_compound = 0;
4254
22.0k
    seq_params->enable_warped_motion = 0;
4255
22.0k
    seq_params->enable_dual_filter = 0;
4256
22.0k
    seq_params->order_hint_info.enable_order_hint = 0;
4257
22.0k
    seq_params->order_hint_info.enable_dist_wtd_comp = 0;
4258
22.0k
    seq_params->order_hint_info.enable_ref_frame_mvs = 0;
4259
22.0k
    seq_params->force_screen_content_tools = 2;  // SELECT_SCREEN_CONTENT_TOOLS
4260
22.0k
    seq_params->force_integer_mv = 2;            // SELECT_INTEGER_MV
4261
22.0k
    seq_params->order_hint_info.order_hint_bits_minus_1 = -1;
4262
22.0k
  } else {
4263
13.0k
    seq_params->enable_interintra_compound = aom_rb_read_bit(rb);
4264
13.0k
    seq_params->enable_masked_compound = aom_rb_read_bit(rb);
4265
13.0k
    seq_params->enable_warped_motion = aom_rb_read_bit(rb);
4266
13.0k
    seq_params->enable_dual_filter = aom_rb_read_bit(rb);
4267
4268
13.0k
    seq_params->order_hint_info.enable_order_hint = aom_rb_read_bit(rb);
4269
13.0k
    seq_params->order_hint_info.enable_dist_wtd_comp =
4270
13.0k
        seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
4271
13.0k
    seq_params->order_hint_info.enable_ref_frame_mvs =
4272
13.0k
        seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
4273
4274
13.0k
    if (aom_rb_read_bit(rb)) {
4275
5.13k
      seq_params->force_screen_content_tools =
4276
5.13k
          2;  // SELECT_SCREEN_CONTENT_TOOLS
4277
7.88k
    } else {
4278
7.88k
      seq_params->force_screen_content_tools = aom_rb_read_bit(rb);
4279
7.88k
    }
4280
4281
13.0k
    if (seq_params->force_screen_content_tools > 0) {
4282
7.69k
      if (aom_rb_read_bit(rb)) {
4283
4.79k
        seq_params->force_integer_mv = 2;  // SELECT_INTEGER_MV
4284
4.79k
      } else {
4285
2.90k
        seq_params->force_integer_mv = aom_rb_read_bit(rb);
4286
2.90k
      }
4287
7.69k
    } else {
4288
5.32k
      seq_params->force_integer_mv = 2;  // SELECT_INTEGER_MV
4289
5.32k
    }
4290
13.0k
    seq_params->order_hint_info.order_hint_bits_minus_1 =
4291
13.0k
        seq_params->order_hint_info.enable_order_hint
4292
13.0k
            ? aom_rb_read_literal(rb, 3)
4293
13.0k
            : -1;
4294
13.0k
  }
4295
4296
35.0k
  seq_params->enable_superres = aom_rb_read_bit(rb);
4297
35.0k
  seq_params->enable_cdef = aom_rb_read_bit(rb);
4298
35.0k
  seq_params->enable_restoration = aom_rb_read_bit(rb);
4299
35.0k
}
4300
4301
static int read_global_motion_params(WarpedMotionParams *params,
4302
                                     const WarpedMotionParams *ref_params,
4303
                                     struct aom_read_bit_buffer *rb,
4304
139k
                                     int allow_hp) {
4305
139k
  TransformationType type = aom_rb_read_bit(rb);
4306
139k
  if (type != IDENTITY) {
4307
51.6k
    if (aom_rb_read_bit(rb))
4308
27.6k
      type = ROTZOOM;
4309
24.0k
    else
4310
24.0k
      type = aom_rb_read_bit(rb) ? TRANSLATION : AFFINE;
4311
51.6k
  }
4312
4313
139k
  *params = default_warp_params;
4314
139k
  params->wmtype = type;
4315
4316
139k
  if (type >= ROTZOOM) {
4317
42.4k
    params->wmmat[2] = aom_rb_read_signed_primitive_refsubexpfin(
4318
42.4k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4319
42.4k
                           (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
4320
42.4k
                               (1 << GM_ALPHA_PREC_BITS)) *
4321
42.4k
                           GM_ALPHA_DECODE_FACTOR +
4322
42.4k
                       (1 << WARPEDMODEL_PREC_BITS);
4323
42.4k
    params->wmmat[3] = aom_rb_read_signed_primitive_refsubexpfin(
4324
42.4k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4325
42.4k
                           (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
4326
42.4k
                       GM_ALPHA_DECODE_FACTOR;
4327
42.4k
  }
4328
4329
139k
  if (type >= AFFINE) {
4330
14.7k
    params->wmmat[4] = aom_rb_read_signed_primitive_refsubexpfin(
4331
14.7k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4332
14.7k
                           (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
4333
14.7k
                       GM_ALPHA_DECODE_FACTOR;
4334
14.7k
    params->wmmat[5] = aom_rb_read_signed_primitive_refsubexpfin(
4335
14.7k
                           rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4336
14.7k
                           (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
4337
14.7k
                               (1 << GM_ALPHA_PREC_BITS)) *
4338
14.7k
                           GM_ALPHA_DECODE_FACTOR +
4339
14.7k
                       (1 << WARPEDMODEL_PREC_BITS);
4340
125k
  } else {
4341
125k
    params->wmmat[4] = -params->wmmat[3];
4342
125k
    params->wmmat[5] = params->wmmat[2];
4343
125k
  }
4344
4345
139k
  if (type >= TRANSLATION) {
4346
51.6k
    const int trans_bits = (type == TRANSLATION)
4347
51.6k
                               ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4348
51.6k
                               : GM_ABS_TRANS_BITS;
4349
51.6k
    const int trans_dec_factor =
4350
51.6k
        (type == TRANSLATION) ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4351
51.6k
                              : GM_TRANS_DECODE_FACTOR;
4352
51.6k
    const int trans_prec_diff = (type == TRANSLATION)
4353
51.6k
                                    ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
4354
51.6k
                                    : GM_TRANS_PREC_DIFF;
4355
51.6k
    params->wmmat[0] = aom_rb_read_signed_primitive_refsubexpfin(
4356
51.6k
                           rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
4357
51.6k
                           (ref_params->wmmat[0] >> trans_prec_diff)) *
4358
51.6k
                       trans_dec_factor;
4359
51.6k
    params->wmmat[1] = aom_rb_read_signed_primitive_refsubexpfin(
4360
51.6k
                           rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
4361
51.6k
                           (ref_params->wmmat[1] >> trans_prec_diff)) *
4362
51.6k
                       trans_dec_factor;
4363
51.6k
  }
4364
4365
139k
  int good_shear_params = av1_get_shear_params(params);
4366
139k
  if (!good_shear_params) return 0;
4367
4368
138k
  return 1;
4369
139k
}
4370
4371
static inline void read_global_motion(AV1_COMMON *cm,
4372
20.0k
                                      struct aom_read_bit_buffer *rb) {
4373
159k
  for (int frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
4374
139k
    const WarpedMotionParams *ref_params =
4375
139k
        cm->prev_frame ? &cm->prev_frame->global_motion[frame]
4376
139k
                       : &default_warp_params;
4377
139k
    int good_params =
4378
139k
        read_global_motion_params(&cm->global_motion[frame], ref_params, rb,
4379
139k
                                  cm->features.allow_high_precision_mv);
4380
139k
    if (!good_params) {
4381
#if WARPED_MOTION_DEBUG
4382
      printf("Warning: unexpected global motion shear params from aomenc\n");
4383
#endif
4384
1.22k
      cm->global_motion[frame].invalid = 1;
4385
1.22k
    }
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
139k
  }
4410
20.0k
  memcpy(cm->cur_frame->global_motion, cm->global_motion,
4411
20.0k
         REF_FRAMES * sizeof(WarpedMotionParams));
4412
20.0k
}
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
33.0k
static inline void reset_ref_frame_map(AV1_COMMON *const cm) {
4417
33.0k
  BufferPool *const pool = cm->buffer_pool;
4418
4419
297k
  for (int i = 0; i < REF_FRAMES; i++) {
4420
264k
    decrease_ref_count(cm->ref_frame_map[i], pool);
4421
264k
    cm->ref_frame_map[i] = NULL;
4422
264k
  }
4423
33.0k
}
4424
4425
// If the refresh_frame_flags bitmask is set, update reference frame id values
4426
// and mark frames as valid for reference.
4427
54.9k
static inline void update_ref_frame_id(AV1Decoder *const pbi) {
4428
54.9k
  AV1_COMMON *const cm = &pbi->common;
4429
54.9k
  int refresh_frame_flags = cm->current_frame.refresh_frame_flags;
4430
494k
  for (int i = 0; i < REF_FRAMES; i++) {
4431
439k
    if ((refresh_frame_flags >> i) & 1) {
4432
330k
      cm->ref_frame_id[i] = cm->current_frame_id;
4433
330k
      pbi->valid_for_referencing[i] = 1;
4434
330k
    }
4435
439k
  }
4436
54.9k
}
4437
4438
static inline void show_existing_frame_reset(AV1Decoder *const pbi,
4439
56
                                             int existing_frame_idx) {
4440
56
  AV1_COMMON *const cm = &pbi->common;
4441
4442
56
  assert(cm->show_existing_frame);
4443
4444
56
  cm->current_frame.frame_type = KEY_FRAME;
4445
4446
56
  cm->current_frame.refresh_frame_flags = (1 << REF_FRAMES) - 1;
4447
4448
448
  for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4449
392
    cm->remapped_ref_idx[i] = INVALID_IDX;
4450
392
  }
4451
4452
56
  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
56
  cm->current_frame_id = cm->ref_frame_id[existing_frame_idx];
4460
56
  update_ref_frame_id(pbi);
4461
4462
56
  cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
4463
56
}
4464
4465
5.91k
static inline void reset_frame_buffers(AV1_COMMON *cm) {
4466
5.91k
  RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
4467
5.91k
  int i;
4468
4469
5.91k
  lock_buffer_pool(cm->buffer_pool);
4470
5.91k
  reset_ref_frame_map(cm);
4471
5.91k
  assert(cm->cur_frame->ref_count == 1);
4472
100k
  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
94.6k
    if (frame_bufs[i].ref_count > 0 && &frame_bufs[i] != cm->cur_frame) {
4476
2.51k
      continue;
4477
2.51k
    }
4478
92.1k
    frame_bufs[i].order_hint = 0;
4479
92.1k
    av1_zero(frame_bufs[i].ref_order_hints);
4480
92.1k
  }
4481
5.91k
  av1_zero_unused_internal_frame_buffers(&cm->buffer_pool->int_frame_buffers);
4482
5.91k
  unlock_buffer_pool(cm->buffer_pool);
4483
5.91k
}
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
55.5k
                                    struct aom_read_bit_buffer *rb) {
4489
55.5k
  AV1_COMMON *const cm = &pbi->common;
4490
55.5k
  const SequenceHeader *const seq_params = cm->seq_params;
4491
55.5k
  CurrentFrame *const current_frame = &cm->current_frame;
4492
55.5k
  FeatureFlags *const features = &cm->features;
4493
55.5k
  MACROBLOCKD *const xd = &pbi->dcb.xd;
4494
55.5k
  BufferPool *const pool = cm->buffer_pool;
4495
55.5k
  RefCntBuffer *const frame_bufs = pool->frame_bufs;
4496
55.5k
  aom_s_frame_info *sframe_info = &pbi->sframe_info;
4497
55.5k
  sframe_info->is_s_frame = 0;
4498
55.5k
  sframe_info->is_s_frame_at_altref = 0;
4499
4500
55.5k
  if (!pbi->sequence_header_ready) {
4501
3
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4502
3
                       "No sequence header");
4503
3
  }
4504
4505
55.5k
  if (seq_params->reduced_still_picture_hdr) {
4506
21.9k
    cm->show_existing_frame = 0;
4507
21.9k
    cm->show_frame = 1;
4508
21.9k
    current_frame->frame_type = KEY_FRAME;
4509
21.9k
    if (pbi->sequence_header_changed) {
4510
      // This is the start of a new coded video sequence.
4511
3.53k
      pbi->sequence_header_changed = 0;
4512
3.53k
      pbi->decoding_first_frame = 1;
4513
3.53k
      reset_frame_buffers(cm);
4514
3.53k
    }
4515
21.9k
    features->error_resilient_mode = 1;
4516
33.5k
  } else {
4517
33.5k
    cm->show_existing_frame = aom_rb_read_bit(rb);
4518
33.5k
    pbi->reset_decoder_state = 0;
4519
4520
33.5k
    if (cm->show_existing_frame) {
4521
378
      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
378
      const int existing_frame_idx = aom_rb_read_literal(rb, 3);
4528
378
      RefCntBuffer *const frame_to_show = cm->ref_frame_map[existing_frame_idx];
4529
378
      if (frame_to_show == NULL) {
4530
3
        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4531
3
                           "Buffer does not contain a decoded frame");
4532
3
      }
4533
378
      if (seq_params->decoder_model_info_present_flag &&
4534
378
          seq_params->timing_info.equal_picture_interval == 0) {
4535
2
        read_temporal_point_info(cm, rb);
4536
2
      }
4537
378
      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
2
            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
378
      lock_buffer_pool(pool);
4548
378
      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
378
      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
378
      assert(!cm->cur_frame->raw_frame_buffer.data);
4558
378
      assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
4559
378
      pbi->reset_decoder_state = frame_to_show->frame_type == KEY_FRAME;
4560
378
      unlock_buffer_pool(pool);
4561
4562
378
      cm->lf.filter_level[0] = 0;
4563
378
      cm->lf.filter_level[1] = 0;
4564
378
      cm->show_frame = 1;
4565
378
      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
378
      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
378
      if (pbi->reset_decoder_state) frame_to_show->showable_frame = 0;
4579
4580
378
      cm->film_grain_params = frame_to_show->film_grain_params;
4581
4582
378
      if (pbi->reset_decoder_state) {
4583
56
        show_existing_frame_reset(pbi, existing_frame_idx);
4584
322
      } else {
4585
322
        current_frame->refresh_frame_flags = 0;
4586
322
      }
4587
4588
378
      return 0;
4589
378
    }
4590
4591
33.1k
    current_frame->frame_type = (FRAME_TYPE)aom_rb_read_literal(rb, 2);
4592
33.1k
    if (pbi->sequence_header_changed) {
4593
2.37k
      if (current_frame->frame_type == KEY_FRAME) {
4594
        // This is the start of a new coded video sequence.
4595
2.37k
        pbi->sequence_header_changed = 0;
4596
2.37k
        pbi->decoding_first_frame = 1;
4597
2.37k
        reset_frame_buffers(cm);
4598
2.37k
      } else {
4599
2
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4600
2
                           "Sequence header has changed without a keyframe.");
4601
2
      }
4602
2.37k
    }
4603
4604
33.1k
    cm->show_frame = aom_rb_read_bit(rb);
4605
33.1k
    if (cm->show_frame == 0) pbi->is_arf_frame_present = 1;
4606
33.1k
    if (cm->show_frame == 0 && cm->current_frame.frame_type == KEY_FRAME)
4607
8.37k
      pbi->is_fwd_kf_present = 1;
4608
33.1k
    if (cm->current_frame.frame_type == S_FRAME) {
4609
255
      sframe_info->is_s_frame = 1;
4610
255
      sframe_info->is_s_frame_at_altref = cm->show_frame ? 0 : 1;
4611
255
    }
4612
33.1k
    if (seq_params->still_picture &&
4613
33.1k
        (current_frame->frame_type != KEY_FRAME || !cm->show_frame)) {
4614
6
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4615
6
                         "Still pictures must be coded as shown keyframes");
4616
6
    }
4617
33.1k
    cm->showable_frame = current_frame->frame_type != KEY_FRAME;
4618
33.1k
    if (cm->show_frame) {
4619
19.1k
      if (seq_params->decoder_model_info_present_flag &&
4620
19.1k
          seq_params->timing_info.equal_picture_interval == 0)
4621
15
        read_temporal_point_info(cm, rb);
4622
19.1k
    } else {
4623
      // See if this frame can be used as show_existing_frame in future
4624
14.0k
      cm->showable_frame = aom_rb_read_bit(rb);
4625
14.0k
    }
4626
33.1k
    cm->cur_frame->showable_frame = cm->showable_frame;
4627
33.1k
    features->error_resilient_mode =
4628
33.1k
        frame_is_sframe(cm) ||
4629
33.1k
                (current_frame->frame_type == KEY_FRAME && cm->show_frame)
4630
33.1k
            ? 1
4631
33.1k
            : aom_rb_read_bit(rb);
4632
33.1k
  }
4633
4634
55.1k
  if (current_frame->frame_type == KEY_FRAME && cm->show_frame) {
4635
    /* All frames need to be marked as not valid for referencing */
4636
235k
    for (int i = 0; i < REF_FRAMES; i++) {
4637
209k
      pbi->valid_for_referencing[i] = 0;
4638
209k
    }
4639
26.1k
  }
4640
55.1k
  features->disable_cdf_update = aom_rb_read_bit(rb);
4641
55.1k
  if (seq_params->force_screen_content_tools == 2) {
4642
29.9k
    features->allow_screen_content_tools = aom_rb_read_bit(rb);
4643
29.9k
  } else {
4644
25.2k
    features->allow_screen_content_tools =
4645
25.2k
        seq_params->force_screen_content_tools;
4646
25.2k
  }
4647
4648
55.1k
  if (features->allow_screen_content_tools) {
4649
20.7k
    if (seq_params->force_integer_mv == 2) {
4650
15.9k
      features->cur_frame_force_integer_mv = aom_rb_read_bit(rb);
4651
15.9k
    } else {
4652
4.81k
      features->cur_frame_force_integer_mv = seq_params->force_integer_mv;
4653
4.81k
    }
4654
34.4k
  } else {
4655
34.4k
    features->cur_frame_force_integer_mv = 0;
4656
34.4k
  }
4657
4658
55.1k
  int frame_size_override_flag = 0;
4659
55.1k
  features->allow_intrabc = 0;
4660
55.1k
  features->primary_ref_frame = PRIMARY_REF_NONE;
4661
4662
55.1k
  if (!seq_params->reduced_still_picture_hdr) {
4663
33.1k
    if (seq_params->frame_id_numbers_present_flag) {
4664
665
      int frame_id_length = seq_params->frame_id_length;
4665
665
      int diff_len = seq_params->delta_frame_id_length;
4666
665
      int prev_frame_id = 0;
4667
665
      int have_prev_frame_id =
4668
665
          !pbi->decoding_first_frame &&
4669
665
          !(current_frame->frame_type == KEY_FRAME && cm->show_frame);
4670
665
      if (have_prev_frame_id) {
4671
84
        prev_frame_id = cm->current_frame_id;
4672
84
      }
4673
665
      cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
4674
4675
665
      if (have_prev_frame_id) {
4676
84
        int diff_frame_id;
4677
84
        if (cm->current_frame_id > prev_frame_id) {
4678
68
          diff_frame_id = cm->current_frame_id - prev_frame_id;
4679
68
        } else {
4680
16
          diff_frame_id =
4681
16
              (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
4682
16
        }
4683
        /* Check current_frame_id for conformance */
4684
84
        if (prev_frame_id == cm->current_frame_id ||
4685
84
            diff_frame_id >= (1 << (frame_id_length - 1))) {
4686
6
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4687
6
                             "Invalid value of current_frame_id");
4688
6
        }
4689
84
      }
4690
      /* Check if some frames need to be marked as not valid for referencing */
4691
5.93k
      for (int i = 0; i < REF_FRAMES; i++) {
4692
5.27k
        if (cm->current_frame_id - (1 << diff_len) > 0) {
4693
2.79k
          if (cm->ref_frame_id[i] > cm->current_frame_id ||
4694
2.79k
              cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
4695
2.38k
            pbi->valid_for_referencing[i] = 0;
4696
2.79k
        } else {
4697
2.48k
          if (cm->ref_frame_id[i] > cm->current_frame_id &&
4698
2.48k
              cm->ref_frame_id[i] < (1 << frame_id_length) +
4699
1.21k
                                        cm->current_frame_id - (1 << diff_len))
4700
598
            pbi->valid_for_referencing[i] = 0;
4701
2.48k
        }
4702
5.27k
      }
4703
665
    }
4704
4705
33.1k
    frame_size_override_flag = frame_is_sframe(cm) ? 1 : aom_rb_read_bit(rb);
4706
4707
33.1k
    current_frame->order_hint = aom_rb_read_literal(
4708
33.1k
        rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
4709
4710
33.1k
    if (seq_params->order_hint_info.enable_order_hint)
4711
22.2k
      current_frame->frame_number = current_frame->order_hint;
4712
4713
33.1k
    if (!features->error_resilient_mode && !frame_is_intra_only(cm)) {
4714
18.6k
      features->primary_ref_frame = aom_rb_read_literal(rb, PRIMARY_REF_BITS);
4715
18.6k
    }
4716
33.1k
  }
4717
4718
55.1k
  if (seq_params->decoder_model_info_present_flag) {
4719
650
    pbi->buffer_removal_time_present = aom_rb_read_bit(rb);
4720
650
    if (pbi->buffer_removal_time_present) {
4721
372
      for (int op_num = 0;
4722
2.22k
           op_num < seq_params->operating_points_cnt_minus_1 + 1; op_num++) {
4723
1.85k
        if (seq_params->op_params[op_num].decoder_model_param_present_flag) {
4724
479
          if (seq_params->operating_point_idc[op_num] == 0 ||
4725
479
              (((seq_params->operating_point_idc[op_num] >>
4726
472
                 cm->temporal_layer_id) &
4727
472
                0x1) &&
4728
472
               ((seq_params->operating_point_idc[op_num] >>
4729
348
                 (cm->spatial_layer_id + 8)) &
4730
348
                0x1))) {
4731
20
            cm->buffer_removal_times[op_num] = aom_rb_read_unsigned_literal(
4732
20
                rb, seq_params->decoder_model_info.buffer_removal_time_length);
4733
459
          } else {
4734
459
            cm->buffer_removal_times[op_num] = 0;
4735
459
          }
4736
1.37k
        } else {
4737
1.37k
          cm->buffer_removal_times[op_num] = 0;
4738
1.37k
        }
4739
1.85k
      }
4740
372
    }
4741
650
  }
4742
55.1k
  if (current_frame->frame_type == KEY_FRAME) {
4743
34.5k
    if (!cm->show_frame) {  // unshown keyframe (forward keyframe)
4744
8.36k
      current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
4745
26.1k
    } else {  // shown keyframe
4746
26.1k
      current_frame->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4747
26.1k
    }
4748
4749
276k
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4750
241k
      cm->remapped_ref_idx[i] = INVALID_IDX;
4751
241k
    }
4752
34.5k
    if (pbi->need_resync) {
4753
27.0k
      reset_ref_frame_map(cm);
4754
27.0k
      pbi->need_resync = 0;
4755
27.0k
    }
4756
34.5k
  } else {
4757
20.6k
    if (current_frame->frame_type == INTRA_ONLY_FRAME) {
4758
433
      current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
4759
433
      if (current_frame->refresh_frame_flags == 0xFF) {
4760
2
        aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4761
2
                           "Intra only frames cannot have refresh flags 0xFF");
4762
2
      }
4763
433
      if (pbi->need_resync) {
4764
130
        reset_ref_frame_map(cm);
4765
130
        pbi->need_resync = 0;
4766
130
      }
4767
20.2k
    } else if (pbi->need_resync != 1) { /* Skip if need resync */
4768
20.1k
      current_frame->refresh_frame_flags =
4769
20.1k
          frame_is_sframe(cm) ? 0xFF : aom_rb_read_literal(rb, REF_FRAMES);
4770
20.1k
    }
4771
20.6k
  }
4772
4773
55.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
28.3k
    if (features->error_resilient_mode &&
4776
28.3k
        seq_params->order_hint_info.enable_order_hint) {
4777
19.2k
      for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
4778
        // Read order hint from bit stream
4779
17.0k
        unsigned int order_hint = aom_rb_read_literal(
4780
17.0k
            rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
4781
        // Get buffer
4782
17.0k
        RefCntBuffer *buf = cm->ref_frame_map[ref_idx];
4783
17.0k
        if (buf == NULL || order_hint != buf->order_hint) {
4784
13.8k
          if (buf != NULL) {
4785
7.12k
            lock_buffer_pool(pool);
4786
7.12k
            decrease_ref_count(buf, pool);
4787
7.12k
            unlock_buffer_pool(pool);
4788
7.12k
            cm->ref_frame_map[ref_idx] = NULL;
4789
7.12k
          }
4790
          // If no corresponding buffer exists, allocate a new buffer with all
4791
          // pixels set to neutral grey.
4792
13.8k
          int buf_idx = get_free_fb(cm);
4793
13.8k
          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
13.8k
          buf = &frame_bufs[buf_idx];
4798
13.8k
          lock_buffer_pool(pool);
4799
13.8k
#if CONFIG_SIZE_LIMIT
4800
13.8k
          if (seq_params->max_frame_width > DECODE_WIDTH_LIMIT ||
4801
13.8k
              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
13.8k
#endif
4811
13.8k
          if (aom_realloc_frame_buffer(
4812
13.8k
                  &buf->buf, seq_params->max_frame_width,
4813
13.8k
                  seq_params->max_frame_height, seq_params->subsampling_x,
4814
13.8k
                  seq_params->subsampling_y, seq_params->use_highbitdepth,
4815
13.8k
                  AOM_BORDER_IN_PIXELS, features->byte_alignment,
4816
13.8k
                  &buf->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv, false,
4817
13.8k
                  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
13.8k
          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
13.8k
          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
13.8k
          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
13.8k
          cm->ref_frame_map[ref_idx] = buf;
4842
13.8k
          buf->order_hint = order_hint;
4843
13.8k
        }
4844
17.0k
      }
4845
2.15k
    }
4846
28.3k
  }
4847
4848
55.1k
  if (current_frame->frame_type == KEY_FRAME) {
4849
34.4k
    setup_frame_size(cm, frame_size_override_flag, rb);
4850
4851
34.4k
    if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
4852
13.5k
      features->allow_intrabc = aom_rb_read_bit(rb);
4853
34.4k
    features->allow_ref_frame_mvs = 0;
4854
34.4k
    cm->prev_frame = NULL;
4855
34.4k
  } else {
4856
20.6k
    features->allow_ref_frame_mvs = 0;
4857
4858
20.6k
    if (current_frame->frame_type == INTRA_ONLY_FRAME) {
4859
430
      cm->cur_frame->film_grain_params_present =
4860
430
          seq_params->film_grain_params_present;
4861
430
      setup_frame_size(cm, frame_size_override_flag, rb);
4862
430
      if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
4863
78
        features->allow_intrabc = aom_rb_read_bit(rb);
4864
4865
20.2k
    } else if (pbi->need_resync != 1) { /* Skip if need resync */
4866
20.1k
      int frame_refs_short_signaling = 0;
4867
      // Frame refs short signaling is off when error resilient mode is on.
4868
20.1k
      if (seq_params->order_hint_info.enable_order_hint)
4869
11.7k
        frame_refs_short_signaling = aom_rb_read_bit(rb);
4870
4871
20.1k
      if (frame_refs_short_signaling) {
4872
        // == LAST_FRAME ==
4873
3.60k
        const int lst_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
4874
3.60k
        const RefCntBuffer *const lst_buf = cm->ref_frame_map[lst_ref];
4875
4876
        // == GOLDEN_FRAME ==
4877
3.60k
        const int gld_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
4878
3.60k
        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
3.60k
        if (lst_buf == NULL)
4887
5
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4888
5
                             "Inter frame requests nonexistent reference");
4889
3.60k
        if (gld_buf == NULL)
4890
4
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4891
4
                             "Inter frame requests nonexistent reference");
4892
4893
3.60k
        av1_set_frame_refs(cm, cm->remapped_ref_idx, lst_ref, gld_ref);
4894
3.60k
      }
4895
4896
160k
      for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4897
140k
        int ref = 0;
4898
140k
        if (!frame_refs_short_signaling) {
4899
115k
          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
115k
          if (cm->ref_frame_map[ref] == NULL)
4908
8
            aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4909
8
                               "Inter frame requests nonexistent reference");
4910
115k
          cm->remapped_ref_idx[i] = ref;
4911
115k
        } else {
4912
24.9k
          ref = cm->remapped_ref_idx[i];
4913
24.9k
        }
4914
        // Check valid for referencing
4915
140k
        if (pbi->valid_for_referencing[ref] == 0)
4916
3
          aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4917
3
                             "Reference frame not valid for referencing");
4918
4919
140k
        cm->ref_frame_sign_bias[LAST_FRAME + i] = 0;
4920
4921
140k
        if (seq_params->frame_id_numbers_present_flag) {
4922
22
          int frame_id_length = seq_params->frame_id_length;
4923
22
          int diff_len = seq_params->delta_frame_id_length;
4924
22
          int delta_frame_id_minus_1 = aom_rb_read_literal(rb, diff_len);
4925
22
          int ref_frame_id =
4926
22
              ((cm->current_frame_id - (delta_frame_id_minus_1 + 1) +
4927
22
                (1 << frame_id_length)) %
4928
22
               (1 << frame_id_length));
4929
          // Compare values derived from delta_frame_id_minus_1 and
4930
          // refresh_frame_flags.
4931
22
          if (ref_frame_id != cm->ref_frame_id[ref])
4932
9
            aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4933
9
                               "Reference buffer frame ID mismatch");
4934
22
        }
4935
140k
      }
4936
4937
20.1k
      if (!features->error_resilient_mode && frame_size_override_flag) {
4938
16.5k
        setup_frame_size_with_refs(cm, rb);
4939
16.5k
      } else {
4940
3.64k
        setup_frame_size(cm, frame_size_override_flag, rb);
4941
3.64k
      }
4942
4943
20.1k
      if (features->cur_frame_force_integer_mv) {
4944
1.64k
        features->allow_high_precision_mv = 0;
4945
18.5k
      } else {
4946
18.5k
        features->allow_high_precision_mv = aom_rb_read_bit(rb);
4947
18.5k
      }
4948
20.1k
      features->interp_filter = read_frame_interp_filter(rb);
4949
20.1k
      features->switchable_motion_mode = aom_rb_read_bit(rb);
4950
20.1k
    }
4951
4952
20.6k
    cm->prev_frame = get_primary_ref_frame_buf(cm);
4953
20.6k
    if (features->primary_ref_frame != PRIMARY_REF_NONE &&
4954
20.6k
        get_primary_ref_frame_buf(cm) == NULL) {
4955
3
      aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
4956
3
                         "Reference frame containing this frame's initial "
4957
3
                         "frame context is unavailable.");
4958
3
    }
4959
4960
20.6k
    if (!(current_frame->frame_type == INTRA_ONLY_FRAME) &&
4961
20.6k
        pbi->need_resync != 1) {
4962
20.0k
      if (frame_might_allow_ref_frame_mvs(cm))
4963
7.98k
        features->allow_ref_frame_mvs = aom_rb_read_bit(rb);
4964
12.0k
      else
4965
12.0k
        features->allow_ref_frame_mvs = 0;
4966
4967
160k
      for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4968
140k
        const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
4969
140k
        struct scale_factors *const ref_scale_factors =
4970
140k
            get_ref_scale_factors(cm, i);
4971
140k
        av1_setup_scale_factors_for_frame(
4972
140k
            ref_scale_factors, ref_buf->buf.y_crop_width,
4973
140k
            ref_buf->buf.y_crop_height, cm->width, cm->height);
4974
140k
        if ((!av1_is_valid_scale(ref_scale_factors)))
4975
21
          aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
4976
21
                             "Reference frame has invalid dimensions");
4977
140k
      }
4978
20.0k
    }
4979
20.6k
  }
4980
4981
55.1k
  av1_setup_frame_buf_refs(cm);
4982
4983
55.1k
  av1_setup_frame_sign_bias(cm);
4984
4985
55.1k
  cm->cur_frame->frame_type = current_frame->frame_type;
4986
4987
55.1k
  update_ref_frame_id(pbi);
4988
4989
55.1k
  const int might_bwd_adapt = !(seq_params->reduced_still_picture_hdr) &&
4990
55.1k
                              !(features->disable_cdf_update);
4991
55.1k
  if (might_bwd_adapt) {
4992
22.3k
    features->refresh_frame_context = aom_rb_read_bit(rb)
4993
22.3k
                                          ? REFRESH_FRAME_CONTEXT_DISABLED
4994
22.3k
                                          : REFRESH_FRAME_CONTEXT_BACKWARD;
4995
32.7k
  } else {
4996
32.7k
    features->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
4997
32.7k
  }
4998
4999
55.1k
  cm->cur_frame->buf.bit_depth = seq_params->bit_depth;
5000
55.1k
  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5001
55.1k
  cm->cur_frame->buf.transfer_characteristics =
5002
55.1k
      seq_params->transfer_characteristics;
5003
55.1k
  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5004
55.1k
  cm->cur_frame->buf.monochrome = seq_params->monochrome;
5005
55.1k
  cm->cur_frame->buf.chroma_sample_position =
5006
55.1k
      seq_params->chroma_sample_position;
5007
55.1k
  cm->cur_frame->buf.color_range = seq_params->color_range;
5008
55.1k
  cm->cur_frame->buf.render_width = cm->render_width;
5009
55.1k
  cm->cur_frame->buf.render_height = cm->render_height;
5010
5011
55.1k
  if (pbi->need_resync) {
5012
2
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5013
2
                       "Keyframe / intra-only frame required to reset decoder"
5014
2
                       " state");
5015
2
  }
5016
5017
55.1k
  if (features->allow_intrabc) {
5018
    // Set parameters corresponding to no filtering.
5019
4.26k
    struct loopfilter *lf = &cm->lf;
5020
4.26k
    lf->filter_level[0] = 0;
5021
4.26k
    lf->filter_level[1] = 0;
5022
4.26k
    cm->cdef_info.cdef_bits = 0;
5023
4.26k
    cm->cdef_info.cdef_strengths[0] = 0;
5024
4.26k
    cm->cdef_info.nb_cdef_strengths = 1;
5025
4.26k
    cm->cdef_info.cdef_uv_strengths[0] = 0;
5026
4.26k
    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5027
4.26k
    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5028
4.26k
    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5029
4.26k
  }
5030
5031
55.1k
  read_tile_info(pbi, rb);
5032
55.1k
  if (!av1_is_min_tile_width_satisfied(cm)) {
5033
9
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5034
9
                       "Minimum tile width requirement not satisfied");
5035
9
  }
5036
5037
55.1k
  CommonQuantParams *const quant_params = &cm->quant_params;
5038
55.1k
  setup_quantization(quant_params, av1_num_planes(cm),
5039
55.1k
                     cm->seq_params->separate_uv_delta_q, rb);
5040
55.1k
  xd->bd = (int)seq_params->bit_depth;
5041
5042
55.1k
  CommonContexts *const above_contexts = &cm->above_contexts;
5043
55.1k
  if (above_contexts->num_planes < av1_num_planes(cm) ||
5044
55.1k
      above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
5045
55.1k
      above_contexts->num_tile_rows < cm->tiles.rows) {
5046
29.7k
    av1_free_above_context_buffers(above_contexts);
5047
29.7k
    if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
5048
29.7k
                                        cm->mi_params.mi_cols,
5049
29.7k
                                        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
29.7k
  }
5054
5055
55.1k
  if (features->primary_ref_frame == PRIMARY_REF_NONE) {
5056
37.0k
    av1_setup_past_independence(cm);
5057
37.0k
  }
5058
5059
55.1k
  setup_segmentation(cm, rb);
5060
5061
55.1k
  cm->delta_q_info.delta_q_res = 1;
5062
55.1k
  cm->delta_q_info.delta_lf_res = 1;
5063
55.1k
  cm->delta_q_info.delta_lf_present_flag = 0;
5064
55.1k
  cm->delta_q_info.delta_lf_multi = 0;
5065
55.1k
  cm->delta_q_info.delta_q_present_flag =
5066
55.1k
      quant_params->base_qindex > 0 ? aom_rb_read_bit(rb) : 0;
5067
55.1k
  if (cm->delta_q_info.delta_q_present_flag) {
5068
15.0k
    xd->current_base_qindex = quant_params->base_qindex;
5069
15.0k
    cm->delta_q_info.delta_q_res = 1 << aom_rb_read_literal(rb, 2);
5070
15.0k
    if (!features->allow_intrabc)
5071
14.6k
      cm->delta_q_info.delta_lf_present_flag = aom_rb_read_bit(rb);
5072
15.0k
    if (cm->delta_q_info.delta_lf_present_flag) {
5073
7.82k
      cm->delta_q_info.delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
5074
7.82k
      cm->delta_q_info.delta_lf_multi = aom_rb_read_bit(rb);
5075
7.82k
      av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
5076
7.82k
    }
5077
15.0k
  }
5078
5079
55.1k
  xd->cur_frame_force_integer_mv = features->cur_frame_force_integer_mv;
5080
5081
493k
  for (int i = 0; i < MAX_SEGMENTS; ++i) {
5082
438k
    const int qindex = av1_get_qindex(&cm->seg, i, quant_params->base_qindex);
5083
438k
    xd->lossless[i] =
5084
438k
        qindex == 0 && quant_params->y_dc_delta_q == 0 &&
5085
438k
        quant_params->u_dc_delta_q == 0 && quant_params->u_ac_delta_q == 0 &&
5086
438k
        quant_params->v_dc_delta_q == 0 && quant_params->v_ac_delta_q == 0;
5087
438k
    xd->qindex[i] = qindex;
5088
438k
  }
5089
55.1k
  features->coded_lossless = is_coded_lossless(cm, xd);
5090
55.1k
  features->all_lossless = features->coded_lossless && !av1_superres_scaled(cm);
5091
55.1k
  setup_segmentation_dequant(cm, xd);
5092
55.1k
  if (features->coded_lossless) {
5093
8.00k
    cm->lf.filter_level[0] = 0;
5094
8.00k
    cm->lf.filter_level[1] = 0;
5095
8.00k
  }
5096
55.1k
  if (features->coded_lossless || !seq_params->enable_cdef) {
5097
43.3k
    cm->cdef_info.cdef_bits = 0;
5098
43.3k
    cm->cdef_info.cdef_strengths[0] = 0;
5099
43.3k
    cm->cdef_info.cdef_uv_strengths[0] = 0;
5100
43.3k
  }
5101
55.1k
  if (features->all_lossless || !seq_params->enable_restoration) {
5102
32.6k
    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5103
32.6k
    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5104
32.6k
    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5105
32.6k
  }
5106
55.1k
  setup_loopfilter(cm, rb);
5107
5108
55.1k
  if (!features->coded_lossless && seq_params->enable_cdef) {
5109
11.4k
    setup_cdef(cm, rb);
5110
11.4k
  }
5111
55.1k
  if (!features->all_lossless && seq_params->enable_restoration) {
5112
22.1k
    decode_restoration_mode(cm, rb);
5113
22.1k
  }
5114
5115
55.1k
  features->tx_mode = read_tx_mode(rb, features->coded_lossless);
5116
55.1k
  current_frame->reference_mode = read_frame_reference_mode(cm, rb);
5117
5118
55.1k
  av1_setup_skip_mode_allowed(cm);
5119
55.1k
  current_frame->skip_mode_info.skip_mode_flag =
5120
55.1k
      current_frame->skip_mode_info.skip_mode_allowed ? aom_rb_read_bit(rb) : 0;
5121
5122
55.1k
  if (frame_might_allow_warped_motion(cm))
5123
11.6k
    features->allow_warped_motion = aom_rb_read_bit(rb);
5124
43.4k
  else
5125
43.4k
    features->allow_warped_motion = 0;
5126
5127
55.1k
  features->reduced_tx_set_used = aom_rb_read_bit(rb);
5128
5129
55.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
55.1k
  if (!frame_is_intra_only(cm)) read_global_motion(cm, rb);
5135
5136
55.1k
  cm->cur_frame->film_grain_params_present =
5137
55.1k
      seq_params->film_grain_params_present;
5138
55.1k
  read_film_grain(cm, rb);
5139
5140
55.1k
#if EXT_TILE_DEBUG
5141
55.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
55.1k
#endif  // EXT_TILE_DEBUG
5146
55.1k
  return 0;
5147
55.5k
}
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
99.9k
    const uint8_t *data_end) {
5152
99.9k
  rb->bit_offset = 0;
5153
99.9k
  rb->error_handler = error_handler;
5154
99.9k
  rb->error_handler_data = &pbi->common;
5155
99.9k
  rb->bit_buffer = data;
5156
99.9k
  rb->bit_buffer_end = data_end;
5157
99.9k
  return rb;
5158
99.9k
}
5159
5160
69.7k
BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
5161
69.7k
  int profile = aom_rb_read_literal(rb, PROFILE_BITS);
5162
69.7k
  return (BITSTREAM_PROFILE)profile;
5163
69.7k
}
5164
5165
6.94k
static inline void superres_post_decode(AV1Decoder *pbi) {
5166
6.94k
  AV1_COMMON *const cm = &pbi->common;
5167
6.94k
  BufferPool *const pool = cm->buffer_pool;
5168
5169
6.94k
  if (!av1_superres_scaled(cm)) return;
5170
4.26k
  assert(!cm->features.all_lossless);
5171
5172
4.26k
  av1_superres_upscale(cm, pool, 0);
5173
4.26k
}
5174
5175
uint32_t av1_decode_frame_headers_and_setup(AV1Decoder *pbi,
5176
                                            struct aom_read_bit_buffer *rb,
5177
55.5k
                                            int trailing_bits_present) {
5178
55.5k
  AV1_COMMON *const cm = &pbi->common;
5179
55.5k
  const int num_planes = av1_num_planes(cm);
5180
55.5k
  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
444k
  for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
5197
388k
    cm->global_motion[i] = default_warp_params;
5198
388k
    cm->cur_frame->global_motion[i] = default_warp_params;
5199
388k
  }
5200
55.5k
  xd->global_motion = cm->global_motion;
5201
5202
55.5k
  read_uncompressed_header(pbi, rb);
5203
5204
55.5k
  if (trailing_bits_present) av1_check_trailing_bits(pbi, rb);
5205
5206
55.5k
  if (!cm->tiles.single_tile_decoding &&
5207
55.5k
      (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
55.5k
  const uint32_t uncomp_hdr_size =
5213
55.5k
      (uint32_t)aom_rb_bytes_read(rb);  // Size of the uncompressed header
5214
55.5k
  YV12_BUFFER_CONFIG *new_fb = &cm->cur_frame->buf;
5215
55.5k
  xd->cur_buf = new_fb;
5216
55.5k
  if (av1_allow_intrabc(cm)) {
5217
4.24k
    av1_setup_scale_factors_for_frame(
5218
4.24k
        &cm->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
5219
4.24k
        xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height);
5220
4.24k
  }
5221
5222
  // Showing a frame directly.
5223
55.5k
  if (cm->show_existing_frame) {
5224
367
    if (pbi->reset_decoder_state) {
5225
      // Use the default frame context values.
5226
56
      *cm->fc = *cm->default_frame_context;
5227
56
      if (!cm->fc->initialized)
5228
0
        aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5229
0
                           "Uninitialized entropy context.");
5230
56
    }
5231
367
    return uncomp_hdr_size;
5232
367
  }
5233
5234
55.1k
  cm->mi_params.setup_mi(&cm->mi_params);
5235
5236
55.1k
  av1_calculate_ref_frame_side(cm);
5237
55.1k
  if (cm->features.allow_ref_frame_mvs) av1_setup_motion_field(cm);
5238
5239
55.1k
  av1_setup_block_planes(xd, cm->seq_params->subsampling_x,
5240
55.1k
                         cm->seq_params->subsampling_y, num_planes);
5241
55.1k
  if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
5242
    // use the default frame context values
5243
36.8k
    *cm->fc = *cm->default_frame_context;
5244
36.8k
  } else {
5245
18.3k
    *cm->fc = get_primary_ref_frame_buf(cm)->frame_context;
5246
18.3k
  }
5247
55.1k
  if (!cm->fc->initialized)
5248
23
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5249
23
                       "Uninitialized entropy context.");
5250
5251
55.1k
  pbi->dcb.corrupted = 0;
5252
55.1k
  return uncomp_hdr_size;
5253
55.5k
}
5254
5255
// Once-per-frame initialization
5256
54.1k
static inline void setup_frame_info(AV1Decoder *pbi) {
5257
54.1k
  AV1_COMMON *const cm = &pbi->common;
5258
5259
54.1k
  if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5260
54.1k
      cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5261
54.1k
      cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
5262
13.3k
    av1_alloc_restoration_buffers(cm, /*is_sgr_enabled =*/true);
5263
47.3k
    for (int p = 0; p < av1_num_planes(cm); p++) {
5264
34.0k
      av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0);
5265
34.0k
    }
5266
13.3k
  }
5267
5268
54.1k
  const int use_highbd = cm->seq_params->use_highbitdepth;
5269
54.1k
  const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
5270
54.1k
  if (pbi->td.mc_buf_size != buf_size) {
5271
28.5k
    av1_free_mc_tmp_buf(&pbi->td);
5272
28.5k
    allocate_mc_tmp_buf(cm, &pbi->td, buf_size, use_highbd);
5273
28.5k
  }
5274
54.1k
}
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
54.1k
                                    int end_tile, int initialize_flag) {
5280
54.1k
  AV1_COMMON *const cm = &pbi->common;
5281
54.1k
  CommonTileParams *const tiles = &cm->tiles;
5282
54.1k
  MACROBLOCKD *const xd = &pbi->dcb.xd;
5283
54.1k
  const int tile_count_tg = end_tile - start_tile + 1;
5284
5285
54.1k
  xd->error_info = cm->error;
5286
54.1k
  if (initialize_flag) setup_frame_info(pbi);
5287
54.1k
  const int num_planes = av1_num_planes(cm);
5288
5289
54.1k
  if (pbi->max_threads > 1 && !(tiles->large_scale && !pbi->ext_tile_debug) &&
5290
54.1k
      pbi->row_mt)
5291
42.8k
    *p_data_end =
5292
42.8k
        decode_tiles_row_mt(pbi, data, data_end, start_tile, end_tile);
5293
11.2k
  else if (pbi->max_threads > 1 && tile_count_tg > 1 &&
5294
11.2k
           !(tiles->large_scale && !pbi->ext_tile_debug))
5295
0
    *p_data_end = decode_tiles_mt(pbi, data, data_end, start_tile, end_tile);
5296
11.2k
  else
5297
11.2k
    *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
54.1k
  if (num_planes < 3) {
5301
13.9k
    set_planes_to_neutral_grey(cm->seq_params, xd->cur_buf, 1);
5302
13.9k
  }
5303
5304
54.1k
  if (end_tile != tiles->rows * tiles->cols - 1) {
5305
0
    return;
5306
0
  }
5307
5308
54.1k
  av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
5309
54.1k
                         pbi->num_workers, 1);
5310
54.1k
  av1_alloc_cdef_sync(cm, &pbi->cdef_sync, pbi->num_workers);
5311
5312
54.1k
  if (!cm->features.allow_intrabc && !tiles->single_tile_decoding) {
5313
31.2k
    if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
5314
21.5k
      av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, &pbi->dcb.xd, 0,
5315
21.5k
                               num_planes, 0, pbi->tile_workers,
5316
21.5k
                               pbi->num_workers, &pbi->lf_row_sync, 0);
5317
21.5k
    }
5318
5319
31.2k
    const int do_cdef =
5320
31.2k
        !pbi->skip_loop_filter && !cm->features.coded_lossless &&
5321
31.2k
        (cm->cdef_info.cdef_bits || cm->cdef_info.cdef_strengths[0] ||
5322
25.3k
         cm->cdef_info.cdef_uv_strengths[0]);
5323
31.2k
    const int do_superres = av1_superres_scaled(cm);
5324
31.2k
    const int optimized_loop_restoration = !do_cdef && !do_superres;
5325
31.2k
    const int do_loop_restoration =
5326
31.2k
        cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5327
31.2k
        cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5328
31.2k
        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
31.2k
    int do_extend_border_mt = 0;
5332
31.2k
    if (!optimized_loop_restoration) {
5333
6.94k
      if (do_loop_restoration)
5334
4.70k
        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
5335
4.70k
                                                 cm, 0);
5336
5337
6.94k
      if (do_cdef) {
5338
5.55k
        if (pbi->num_workers > 1) {
5339
5.29k
          av1_cdef_frame_mt(cm, &pbi->dcb.xd, pbi->cdef_worker,
5340
5.29k
                            pbi->tile_workers, &pbi->cdef_sync,
5341
5.29k
                            pbi->num_workers, av1_cdef_init_fb_row_mt,
5342
5.29k
                            do_extend_border_mt);
5343
5.29k
        } else {
5344
260
          av1_cdef_frame(&pbi->common.cur_frame->buf, cm, &pbi->dcb.xd,
5345
260
                         av1_cdef_init_fb_row);
5346
260
        }
5347
5.55k
      }
5348
5349
6.94k
      superres_post_decode(pbi);
5350
5351
6.94k
      if (do_loop_restoration) {
5352
4.70k
        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
5353
4.70k
                                                 cm, 1);
5354
4.70k
        if (pbi->num_workers > 1) {
5355
4.51k
          av1_loop_restoration_filter_frame_mt(
5356
4.51k
              (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
5357
4.51k
              pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
5358
4.51k
              &pbi->lr_ctxt, do_extend_border_mt);
5359
4.51k
        } else {
5360
195
          av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
5361
195
                                            cm, optimized_loop_restoration,
5362
195
                                            &pbi->lr_ctxt);
5363
195
        }
5364
4.70k
      }
5365
24.3k
    } else {
5366
      // In no cdef and no superres case. Provide an optimized version of
5367
      // loop_restoration_filter.
5368
24.3k
      if (do_loop_restoration) {
5369
2.06k
        if (pbi->num_workers > 1) {
5370
407
          av1_loop_restoration_filter_frame_mt(
5371
407
              (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
5372
407
              pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
5373
407
              &pbi->lr_ctxt, do_extend_border_mt);
5374
1.65k
        } else {
5375
1.65k
          av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
5376
1.65k
                                            cm, optimized_loop_restoration,
5377
1.65k
                                            &pbi->lr_ctxt);
5378
1.65k
        }
5379
2.06k
      }
5380
24.3k
    }
5381
31.2k
  }
5382
5383
54.1k
  if (!pbi->dcb.corrupted) {
5384
33.7k
    if (cm->features.refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
5385
12.3k
      assert(pbi->context_update_tile_id < pbi->allocated_tiles);
5386
12.3k
      *cm->fc = pbi->tile_data[pbi->context_update_tile_id].tctx;
5387
12.3k
      av1_reset_cdf_symbol_counters(cm->fc);
5388
12.3k
    }
5389
33.7k
  } else {
5390
20.3k
    aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
5391
20.3k
                       "Decode failed. Frame data is corrupted.");
5392
20.3k
  }
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
54.1k
  if (!tiles->large_scale) {
5402
33.7k
    cm->cur_frame->frame_context = *cm->fc;
5403
33.7k
  }
5404
5405
54.1k
  if (cm->show_frame && !cm->seq_params->order_hint_info.enable_order_hint) {
5406
15.5k
    ++cm->current_frame.frame_number;
5407
15.5k
  }
5408
54.1k
}