Coverage Report

Created: 2025-11-16 07:09

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