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