blob: 765c7270380fea88f477446ec81af5e2f425d35f [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
James Zernb7c05bd2024-06-11 19:15:10 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
venkat sanampudi4a469992023-04-10 10:10:58 +053013#include <stdbool.h>
Wan-Teh Chang4eda2842018-05-07 15:03:35 -070014#include <stddef.h>
Yaowu Xuc27fc142016-08-22 16:08:15 -070015
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070017#include "config/aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018
Steinar Midtskogen2fd70ee2016-09-02 10:02:30 +020019#include "aom/aom_codec.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050020#include "aom/aom_image.h"
21#include "aom/internal/aom_codec_internal.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070022#include "aom_dsp/aom_dsp_common.h"
Cheng Chenc7855b12017-09-05 10:49:08 -070023#include "aom_dsp/binary_codes_reader.h"
Jingning Han1aab8182016-06-03 11:09:06 -070024#include "aom_dsp/bitreader.h"
25#include "aom_dsp/bitreader_buffer.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050026#include "aom_dsp/txfm_common.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070027#include "aom_mem/aom_mem.h"
Jingning Han53c08962017-11-16 14:03:41 -080028#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070029#include "aom_ports/mem.h"
30#include "aom_ports/mem_ops.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050031#include "aom_scale/yv12config.h"
Peter Kastingbad2f8d2024-02-12 14:24:41 -080032#include "aom_util/aom_pthread.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070033#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070034
Angie Chiang5b5f4df2017-12-06 10:41:12 -080035#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Fergus Simpson350a9b72017-04-17 15:08:45 -070036#include "aom_util/debug_util.h"
Angie Chiang5b5f4df2017-12-06 10:41:12 -080037#endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Fergus Simpson350a9b72017-04-17 15:08:45 -070038
Yaowu Xuc27fc142016-08-22 16:08:15 -070039#include "av1/common/alloccommon.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050040#include "av1/common/av1_common_int.h"
41#include "av1/common/blockd.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010042#include "av1/common/cdef.h"
Luc Trudeau3ec16a32018-03-01 20:58:09 -050043#include "av1/common/cfl.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050044#include "av1/common/common_data.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/common/common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070046#include "av1/common/entropy.h"
47#include "av1/common/entropymode.h"
Thomas Davies6519beb2016-10-19 14:46:07 +010048#include "av1/common/entropymv.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050049#include "av1/common/enums.h"
Wan-Teh Changc613fd42018-09-06 12:31:17 -070050#include "av1/common/frame_buffers.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070051#include "av1/common/idct.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050052#include "av1/common/mv.h"
Jingning Hanc723b342017-08-24 11:19:46 -070053#include "av1/common/mvref_common.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050054#include "av1/common/obmc.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/common/pred_common.h"
56#include "av1/common/quant_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070057#include "av1/common/reconinter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070058#include "av1/common/reconintra.h"
Fergus Simpsond2bcbb52017-05-22 23:15:05 -070059#include "av1/common/resize.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050060#include "av1/common/restoration.h"
61#include "av1/common/scale.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070062#include "av1/common/seg_common.h"
Jingning Han1aab8182016-06-03 11:09:06 -070063#include "av1/common/thread_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070064#include "av1/common/tile_common.h"
Yunqing Wang1694a4f2018-01-30 16:40:55 -080065#include "av1/common/warped_motion.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050066
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "av1/decoder/decodeframe.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070068#include "av1/decoder/decodemv.h"
69#include "av1/decoder/decoder.h"
Angie Chiang133733c2017-03-17 12:50:20 -070070#include "av1/decoder/decodetxb.h"
Jingning Han1aab8182016-06-03 11:09:06 -070071#include "av1/decoder/detokenize.h"
Jerome Jiang013132e2024-03-05 10:23:12 -050072#if CONFIG_INSPECTION
73#include "av1/decoder/inspection.h"
74#endif
Yue Chen69f18e12016-09-08 14:48:15 -070075
Michael Bebenita6048d052016-08-25 14:40:54 -070076#define ACCT_STR __func__
Yaowu Xuc27fc142016-08-22 16:08:15 -070077
Cherma Rajan A61661692019-01-22 19:35:27 +053078#define AOM_MIN_THREADS_PER_TILE 1
79#define AOM_MAX_THREADS_PER_TILE 2
80
Yunqing Wang3a74b252018-05-03 15:24:07 -070081// This is needed by ext_tile related unit tests.
82#define EXT_TILE_DEBUG 1
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +053083#define MC_TEMP_BUF_PELS \
84 (((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2) * \
85 ((MAX_SB_SIZE)*2 + (AOM_INTERP_EXTEND)*2))
Yunqing Wang3a74b252018-05-03 15:24:07 -070086
Jingning Han8a59bd52018-03-22 21:30:31 -070087// Checks that the remaining bits start with a 1 and ends with 0s.
88// It consumes an additional byte, if already byte aligned before the check.
Jingning Han53995e12018-03-23 08:02:57 -070089int av1_check_trailing_bits(AV1Decoder *pbi, struct aom_read_bit_buffer *rb) {
Jingning Han8a59bd52018-03-22 21:30:31 -070090 // bit_offset is set to 0 (mod 8) when the reader is already byte aligned
91 int bits_before_alignment = 8 - rb->bit_offset % 8;
Jingning Han8a59bd52018-03-22 21:30:31 -070092 int trailing = aom_rb_read_literal(rb, bits_before_alignment);
93 if (trailing != (1 << (bits_before_alignment - 1))) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +053094 pbi->error.error_code = AOM_CODEC_CORRUPT_FRAME;
Wan-Teh Chang4eda2842018-05-07 15:03:35 -070095 return -1;
Jingning Han8a59bd52018-03-22 21:30:31 -070096 }
97 return 0;
98}
Jingning Han8a59bd52018-03-22 21:30:31 -070099
Imdad Sardharwalla714b2682018-03-12 17:20:09 +0000100// Use only_chroma = 1 to only set the chroma planes
Wan-Teh Chang94534432024-08-08 20:29:57 -0700101static inline void set_planes_to_neutral_grey(
Elliott Karpilovsky18fcd6a2019-09-16 15:15:06 -0700102 const SequenceHeader *const seq_params, const YV12_BUFFER_CONFIG *const buf,
103 int only_chroma) {
Wan-Teh Chang6efd3b22018-09-17 12:22:37 -0700104 if (seq_params->use_highbitdepth) {
105 const int val = 1 << (seq_params->bit_depth - 1);
106 for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
107 const int is_uv = plane > 0;
108 uint16_t *const base = CONVERT_TO_SHORTPTR(buf->buffers[plane]);
109 // Set the first row to neutral grey. Then copy the first row to all
110 // subsequent rows.
111 if (buf->crop_heights[is_uv] > 0) {
112 aom_memset16(base, val, buf->crop_widths[is_uv]);
113 for (int row_idx = 1; row_idx < buf->crop_heights[is_uv]; row_idx++) {
114 memcpy(&base[row_idx * buf->strides[is_uv]], base,
115 sizeof(*base) * buf->crop_widths[is_uv]);
Imdad Sardharwalla714b2682018-03-12 17:20:09 +0000116 }
Wan-Teh Chang6efd3b22018-09-17 12:22:37 -0700117 }
118 }
119 } else {
120 for (int plane = only_chroma; plane < MAX_MB_PLANE; plane++) {
121 const int is_uv = plane > 0;
122 for (int row_idx = 0; row_idx < buf->crop_heights[is_uv]; row_idx++) {
Wan-Teh Changbc67b662022-03-29 13:33:27 -0700123 memset(&buf->buffers[plane][row_idx * buf->strides[is_uv]], 1 << 7,
Imdad Sardharwallafea03042018-04-03 13:07:49 +0100124 buf->crop_widths[is_uv]);
Imdad Sardharwalla714b2682018-03-12 17:20:09 +0000125 }
126 }
127 }
128}
129
Wan-Teh Chang94534432024-08-08 20:29:57 -0700130static inline void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
131 MACROBLOCKD *xd,
132 aom_reader *const r,
133 int plane, int runit_idx);
Rupert Swarbrick6c545212017-09-01 17:17:25 +0100134
Yaowu Xuc27fc142016-08-22 16:08:15 -0700135static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
136 return len != 0 && len <= (size_t)(end - start);
137}
138
Urvang Joshi6237b882020-03-26 15:02:26 -0700139static TX_MODE read_tx_mode(struct aom_read_bit_buffer *rb,
140 int coded_lossless) {
141 if (coded_lossless) return ONLY_4X4;
Debargha Mukherjee923b73d2017-10-31 18:11:34 -0700142 return aom_rb_read_bit(rb) ? TX_MODE_SELECT : TX_MODE_LARGEST;
Debargha Mukherjee923b73d2017-10-31 18:11:34 -0700143}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700144
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145static REFERENCE_MODE read_frame_reference_mode(
Yaowu Xuf883b422016-08-30 14:01:10 -0700146 const AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Cheng Chen52091622018-03-12 17:27:13 -0700147 if (frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700148 return SINGLE_REFERENCE;
Cheng Chen52091622018-03-12 17:27:13 -0700149 } else {
150 return aom_rb_read_bit(rb) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 }
152}
153
Wan-Teh Chang94534432024-08-08 20:29:57 -0700154static inline void inverse_transform_block(DecoderCodingBlock *dcb, int plane,
155 const TX_TYPE tx_type,
156 const TX_SIZE tx_size, uint8_t *dst,
157 int stride, int reduced_tx_set) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700158 tran_low_t *const dqcoeff = dcb->dqcoeff_block[plane] + dcb->cb_offset[plane];
159 eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
Deepa K G610c9b82018-06-19 12:47:01 +0530160 uint16_t scan_line = eob_data->max_scan_line;
161 uint16_t eob = eob_data->eob;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700162 av1_inverse_transform_block(&dcb->xd, dqcoeff, plane, tx_type, tx_size, dst,
163 stride, eob, reduced_tx_set);
Jingning Han1be18782016-10-21 11:48:15 -0700164 memset(dqcoeff, 0, (scan_line + 1) * sizeof(dqcoeff[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165}
166
Wan-Teh Chang94534432024-08-08 20:29:57 -0700167static inline void read_coeffs_tx_intra_block(
Urvang Joshibac1dea2020-04-20 11:37:15 -0700168 const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
Elliott Karpilovsky18fcd6a2019-09-16 15:15:06 -0700169 const int plane, const int row, const int col, const TX_SIZE tx_size) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700170 MB_MODE_INFO *mbmi = dcb->xd.mi[0];
chiyotsai8c004e12020-04-17 15:52:08 -0700171 if (!mbmi->skip_txfm) {
Jingning Han53c08962017-11-16 14:03:41 -0800172#if TXCOEFF_TIMER
173 struct aom_usec_timer timer;
174 aom_usec_timer_start(&timer);
175#endif
Urvang Joshibac1dea2020-04-20 11:37:15 -0700176 av1_read_coeffs_txb_facade(cm, dcb, r, plane, row, col, tx_size);
Jingning Han53c08962017-11-16 14:03:41 -0800177#if TXCOEFF_TIMER
178 aom_usec_timer_mark(&timer);
179 const int64_t elapsed_time = aom_usec_timer_elapsed(&timer);
180 cm->txcoeff_timer += elapsed_time;
181 ++cm->txb_count;
182#endif
Deepa K G49607302018-06-18 14:00:57 +0530183 }
184}
185
Wan-Teh Chang94534432024-08-08 20:29:57 -0700186static inline void decode_block_void(const AV1_COMMON *const cm,
187 DecoderCodingBlock *dcb,
188 aom_reader *const r, const int plane,
189 const int row, const int col,
190 const TX_SIZE tx_size) {
Deepa K G3bc1f932018-06-21 14:25:03 +0530191 (void)cm;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700192 (void)dcb;
Deepa K G3bc1f932018-06-21 14:25:03 +0530193 (void)r;
194 (void)plane;
195 (void)row;
196 (void)col;
197 (void)tx_size;
198}
199
Wan-Teh Chang94534432024-08-08 20:29:57 -0700200static inline void predict_inter_block_void(AV1_COMMON *const cm,
201 DecoderCodingBlock *dcb,
202 BLOCK_SIZE bsize) {
Deepa K G3bc1f932018-06-21 14:25:03 +0530203 (void)cm;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700204 (void)dcb;
Deepa K G3bc1f932018-06-21 14:25:03 +0530205 (void)bsize;
206}
207
Wan-Teh Chang94534432024-08-08 20:29:57 -0700208static inline void cfl_store_inter_block_void(AV1_COMMON *const cm,
209 MACROBLOCKD *const xd) {
Deepa K G3bc1f932018-06-21 14:25:03 +0530210 (void)cm;
211 (void)xd;
212}
213
Wan-Teh Chang94534432024-08-08 20:29:57 -0700214static inline void predict_and_reconstruct_intra_block(
Urvang Joshibac1dea2020-04-20 11:37:15 -0700215 const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
Deepa K G1909dee2018-07-03 17:37:56 +0530216 const int plane, const int row, const int col, const TX_SIZE tx_size) {
Deepa K G49607302018-06-18 14:00:57 +0530217 (void)r;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700218 MACROBLOCKD *const xd = &dcb->xd;
Deepa K G49607302018-06-18 14:00:57 +0530219 MB_MODE_INFO *mbmi = xd->mi[0];
220 PLANE_TYPE plane_type = get_plane_type(plane);
221
222 av1_predict_intra_block_facade(cm, xd, plane, col, row, tx_size);
223
chiyotsai8c004e12020-04-17 15:52:08 -0700224 if (!mbmi->skip_txfm) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700225 eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
Deepa K G610c9b82018-06-19 12:47:01 +0530226 if (eob_data->eob) {
Urvang Joshi6237b882020-03-26 15:02:26 -0700227 const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
Hui Su32fdb832019-10-22 10:20:03 -0700228 // tx_type was read out in av1_read_coeffs_txb.
229 const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, row, col, tx_size,
Urvang Joshi6237b882020-03-26 15:02:26 -0700230 reduced_tx_set_used);
Urvang Joshibac1dea2020-04-20 11:37:15 -0700231 struct macroblockd_plane *const pd = &xd->plane[plane];
Hui Suadda5872019-12-09 10:08:49 -0800232 uint8_t *dst = &pd->dst.buf[(row * pd->dst.stride + col) << MI_SIZE_LOG2];
Urvang Joshibac1dea2020-04-20 11:37:15 -0700233 inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
Urvang Joshi6237b882020-03-26 15:02:26 -0700234 reduced_tx_set_used);
Angie Chiang3d005e42017-04-02 16:31:35 -0700235 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700236 }
Luc Trudeaudae377d2018-04-10 13:02:47 -0400237 if (plane == AOM_PLANE_Y && store_cfl_required(cm, xd)) {
chiyotsai0f5cd052020-08-27 14:37:44 -0700238 cfl_store_tx(xd, row, col, tx_size, mbmi->bsize);
Luc Trudeaue3980282017-04-25 23:17:21 -0400239 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240}
241
Wan-Teh Chang94534432024-08-08 20:29:57 -0700242static inline void inverse_transform_inter_block(
Urvang Joshibac1dea2020-04-20 11:37:15 -0700243 const AV1_COMMON *const cm, DecoderCodingBlock *dcb, aom_reader *const r,
Elliott Karpilovsky18fcd6a2019-09-16 15:15:06 -0700244 const int plane, const int blk_row, const int blk_col,
245 const TX_SIZE tx_size) {
Deepa K G49607302018-06-18 14:00:57 +0530246 (void)r;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700247 MACROBLOCKD *const xd = &dcb->xd;
Deepa K G49607302018-06-18 14:00:57 +0530248 PLANE_TYPE plane_type = get_plane_type(plane);
249 const struct macroblockd_plane *const pd = &xd->plane[plane];
Urvang Joshi6237b882020-03-26 15:02:26 -0700250 const bool reduced_tx_set_used = cm->features.reduced_tx_set_used;
Hui Su32fdb832019-10-22 10:20:03 -0700251 // tx_type was read out in av1_read_coeffs_txb.
Urvang Joshi6237b882020-03-26 15:02:26 -0700252 const TX_TYPE tx_type = av1_get_tx_type(xd, plane_type, blk_row, blk_col,
253 tx_size, reduced_tx_set_used);
Deepa K G49607302018-06-18 14:00:57 +0530254
Deepa K G49607302018-06-18 14:00:57 +0530255 uint8_t *dst =
Hui Suadda5872019-12-09 10:08:49 -0800256 &pd->dst.buf[(blk_row * pd->dst.stride + blk_col) << MI_SIZE_LOG2];
Urvang Joshibac1dea2020-04-20 11:37:15 -0700257 inverse_transform_block(dcb, plane, tx_type, tx_size, dst, pd->dst.stride,
Urvang Joshi6237b882020-03-26 15:02:26 -0700258 reduced_tx_set_used);
Deepa K G1909dee2018-07-03 17:37:56 +0530259#if CONFIG_MISMATCH_DEBUG
260 int pixel_c, pixel_r;
261 BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
262 int blk_w = block_size_wide[bsize];
263 int blk_h = block_size_high[bsize];
Urvang Joshibde9c5d2019-07-29 14:33:52 -0700264 const int mi_row = -xd->mb_to_top_edge >> (3 + MI_SIZE_LOG2);
265 const int mi_col = -xd->mb_to_left_edge >> (3 + MI_SIZE_LOG2);
Deepa K G1909dee2018-07-03 17:37:56 +0530266 mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, blk_col, blk_row,
267 pd->subsampling_x, pd->subsampling_y);
David Turnerd2a592e2018-11-16 14:59:31 +0000268 mismatch_check_block_tx(dst, pd->dst.stride, cm->current_frame.order_hint,
269 plane, pixel_c, pixel_r, blk_w, blk_h,
Deepa K G1909dee2018-07-03 17:37:56 +0530270 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
271#endif
Deepa K G610c9b82018-06-19 12:47:01 +0530272}
273
Wan-Teh Chang94534432024-08-08 20:29:57 -0700274static inline void set_cb_buffer_offsets(DecoderCodingBlock *dcb,
275 TX_SIZE tx_size, int plane) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700276 dcb->cb_offset[plane] += tx_size_wide[tx_size] * tx_size_high[tx_size];
277 dcb->txb_offset[plane] =
278 dcb->cb_offset[plane] / (TX_SIZE_W_MIN * TX_SIZE_H_MIN);
Deepa K G49607302018-06-18 14:00:57 +0530279}
280
Wan-Teh Chang94534432024-08-08 20:29:57 -0700281static inline void decode_reconstruct_tx(AV1_COMMON *cm, ThreadData *const td,
282 aom_reader *r,
283 MB_MODE_INFO *const mbmi, int plane,
284 BLOCK_SIZE plane_bsize, int blk_row,
285 int blk_col, int block,
286 TX_SIZE tx_size, int *eob_total) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700287 DecoderCodingBlock *const dcb = &td->dcb;
288 MACROBLOCKD *const xd = &dcb->xd;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 const struct macroblockd_plane *const pd = &xd->plane[plane];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 const TX_SIZE plane_tx_size =
chiyotsai0f5cd052020-08-27 14:37:44 -0700291 plane ? av1_get_max_uv_txsize(mbmi->bsize, pd->subsampling_x,
Cheng Chen8ab1f442018-04-27 18:01:52 -0700292 pd->subsampling_y)
Hui Su7167d952018-02-01 16:33:12 -0800293 : mbmi->inter_tx_size[av1_get_txb_size_index(plane_bsize, blk_row,
294 blk_col)];
Jingning Han5f614262016-10-27 14:27:43 -0700295 // Scale to match transform block unit.
Jingning Hanf64062f2016-11-02 16:22:18 -0700296 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
297 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700298
299 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
300
Debargha Mukherjee6396d3a2017-12-21 00:12:38 -0800301 if (tx_size == plane_tx_size || plane) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700302 td->read_coeffs_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
Deepa K G1909dee2018-07-03 17:37:56 +0530303 tx_size);
Jingning Han53c08962017-11-16 14:03:41 -0800304
Urvang Joshibac1dea2020-04-20 11:37:15 -0700305 td->inverse_tx_inter_block_visit(cm, dcb, r, plane, blk_row, blk_col,
Deepa K G1909dee2018-07-03 17:37:56 +0530306 tx_size);
Urvang Joshibac1dea2020-04-20 11:37:15 -0700307 eob_info *eob_data = dcb->eob_data[plane] + dcb->txb_offset[plane];
Deepa K G610c9b82018-06-19 12:47:01 +0530308 *eob_total += eob_data->eob;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700309 set_cb_buffer_offsets(dcb, tx_size, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310 } else {
Frederic Barbier4b56b102018-03-30 16:09:34 +0200311 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
Urvang Joshidff57e02017-09-29 11:15:48 -0700312 assert(IMPLIES(tx_size <= TX_4X4, sub_txs == tx_size));
313 assert(IMPLIES(tx_size > TX_4X4, sub_txs < tx_size));
Yue Chen0797a202017-10-27 17:24:56 -0700314 const int bsw = tx_size_wide_unit[sub_txs];
315 const int bsh = tx_size_high_unit[sub_txs];
316 const int sub_step = bsw * bsh;
Wan-Teh Chang0cb03d92021-03-05 08:27:21 -0800317 const int row_end =
318 AOMMIN(tx_size_high_unit[tx_size], max_blocks_high - blk_row);
319 const int col_end =
320 AOMMIN(tx_size_wide_unit[tx_size], max_blocks_wide - blk_col);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700321
Yue Chen0797a202017-10-27 17:24:56 -0700322 assert(bsw > 0 && bsh > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323
Wan-Teh Chang0cb03d92021-03-05 08:27:21 -0800324 for (int row = 0; row < row_end; row += bsh) {
325 const int offsetr = blk_row + row;
326 for (int col = 0; col < col_end; col += bsw) {
Yue Chen0797a202017-10-27 17:24:56 -0700327 const int offsetc = blk_col + col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328
Deepa K G1909dee2018-07-03 17:37:56 +0530329 decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize, offsetr,
330 offsetc, block, sub_txs, eob_total);
Yue Chen0797a202017-10-27 17:24:56 -0700331 block += sub_step;
332 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333 }
334 }
335}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700336
Wan-Teh Chang94534432024-08-08 20:29:57 -0700337static inline void set_offsets(AV1_COMMON *const cm, MACROBLOCKD *const xd,
338 BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
339 int bh, int x_mis, int y_mis) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000340 const int num_planes = av1_num_planes(cm);
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700341 const CommonModeInfoParams *const mi_params = &cm->mi_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342 const TileInfo *const tile = &xd->tile;
343
Urvang Joshi2603bfe2020-03-25 13:33:18 -0700344 set_mi_offsets(mi_params, xd, mi_row, mi_col);
chiyotsai0f5cd052020-08-27 14:37:44 -0700345 xd->mi[0]->bsize = bsize;
Angie Chiang394c3372016-11-03 11:13:15 -0700346#if CONFIG_RD_DEBUG
Yue Chen53b53f02018-03-29 14:31:23 -0700347 xd->mi[0]->mi_row = mi_row;
348 xd->mi[0]->mi_col = mi_col;
Angie Chiang394c3372016-11-03 11:13:15 -0700349#endif
Yunqing Wangb90a97a2017-10-24 11:50:15 -0700350
351 assert(x_mis && y_mis);
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +0100352 for (int x = 1; x < x_mis; ++x) xd->mi[x] = xd->mi[0];
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700353 int idx = mi_params->mi_stride;
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +0100354 for (int y = 1; y < y_mis; ++y) {
Yunqing Wangb90a97a2017-10-24 11:50:15 -0700355 memcpy(&xd->mi[idx], &xd->mi[0], x_mis * sizeof(xd->mi[0]));
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700356 idx += mi_params->mi_stride;
Yunqing Wangb90a97a2017-10-24 11:50:15 -0700357 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700358
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000359 set_plane_n4(xd, bw, bh, num_planes);
Urvang Joshi46ff5522020-03-30 15:27:37 -0700360 set_entropy_context(xd, mi_row, mi_col, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700361
Urvang Joshi359dc2b2017-04-27 15:41:47 -0700362 // Distance of Mb to the various image edges. These are specified to 8th pel
363 // as they are always compared to values that are in 1/8th pel units
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700364 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
365 mi_params->mi_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700366
Jack Haughtonddb80602018-11-21 16:41:49 +0000367 av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
368 num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700369}
370
Wan-Teh Chang94534432024-08-08 20:29:57 -0700371static inline void decode_mbmi_block(AV1Decoder *const pbi,
372 DecoderCodingBlock *dcb, int mi_row,
373 int mi_col, aom_reader *r,
374 PARTITION_TYPE partition,
375 BLOCK_SIZE bsize) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700376 AV1_COMMON *const cm = &pbi->common;
Tarundeep Singh4243e622021-04-20 16:10:22 +0530377 const SequenceHeader *const seq_params = cm->seq_params;
Jingning Han85dc03f2016-12-06 16:03:10 -0800378 const int bw = mi_size_wide[bsize];
379 const int bh = mi_size_high[bsize];
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700380 const int x_mis = AOMMIN(bw, cm->mi_params.mi_cols - mi_col);
381 const int y_mis = AOMMIN(bh, cm->mi_params.mi_rows - mi_row);
Urvang Joshibac1dea2020-04-20 11:37:15 -0700382 MACROBLOCKD *const xd = &dcb->xd;
Nathan E. Eggeebbd4792016-10-05 19:30:15 -0400383
Michael Bebenita6048d052016-08-25 14:40:54 -0700384#if CONFIG_ACCOUNTING
385 aom_accounting_set_context(&pbi->accounting, mi_col, mi_row);
386#endif
Yue Chen64550b62017-01-12 12:18:22 -0800387 set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, y_mis);
Yue Chen53b53f02018-03-29 14:31:23 -0700388 xd->mi[0]->partition = partition;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700389 av1_read_mode_info(pbi, dcb, r, x_mis, y_mis);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700390 if (bsize >= BLOCK_8X8 &&
391 (seq_params->subsampling_x || seq_params->subsampling_y)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392 const BLOCK_SIZE uv_subsize =
Ivan Rosales4daac5e2022-11-22 08:46:35 -0800393 av1_ss_size_lookup[bsize][seq_params->subsampling_x]
394 [seq_params->subsampling_y];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395 if (uv_subsize == BLOCK_INVALID)
Yaowu Xuf883b422016-08-30 14:01:10 -0700396 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397 "Invalid block size.");
398 }
Yue Chen64550b62017-01-12 12:18:22 -0800399}
400
Ravi Chaudhary18c5f6f2018-06-06 13:43:07 +0530401typedef struct PadBlock {
402 int x0;
403 int x1;
404 int y0;
405 int y1;
406} PadBlock;
407
Jerome Jiangebba9c72019-09-20 12:23:01 -0700408#if CONFIG_AV1_HIGHBITDEPTH
Wan-Teh Chang94534432024-08-08 20:29:57 -0700409static inline void highbd_build_mc_border(const uint8_t *src8, int src_stride,
410 uint8_t *dst8, int dst_stride, int x,
411 int y, int b_w, int b_h, int w,
412 int h) {
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530413 // Get a pointer to the start of the real data for this row.
414 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
415 uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
416 const uint16_t *ref_row = src - x - y * src_stride;
417
418 if (y >= h)
419 ref_row += (h - 1) * src_stride;
420 else if (y > 0)
421 ref_row += y * src_stride;
422
423 do {
424 int right = 0, copy;
425 int left = x < 0 ? -x : 0;
426
427 if (left > b_w) left = b_w;
428
429 if (x + b_w > w) right = x + b_w - w;
430
431 if (right > b_w) right = b_w;
432
433 copy = b_w - left - right;
434
435 if (left) aom_memset16(dst, ref_row[0], left);
436
437 if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
438
439 if (right) aom_memset16(dst + left + copy, ref_row[w - 1], right);
440
441 dst += dst_stride;
442 ++y;
443
444 if (y > 0 && y < h) ref_row += src_stride;
445 } while (--b_h);
446}
Jerome Jiangebba9c72019-09-20 12:23:01 -0700447#endif // CONFIG_AV1_HIGHBITDEPTH
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530448
Wan-Teh Chang94534432024-08-08 20:29:57 -0700449static inline void build_mc_border(const uint8_t *src, int src_stride,
450 uint8_t *dst, int dst_stride, int x, int y,
451 int b_w, int b_h, int w, int h) {
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530452 // Get a pointer to the start of the real data for this row.
453 const uint8_t *ref_row = src - x - y * src_stride;
454
455 if (y >= h)
456 ref_row += (h - 1) * src_stride;
457 else if (y > 0)
458 ref_row += y * src_stride;
459
460 do {
461 int right = 0, copy;
462 int left = x < 0 ? -x : 0;
463
464 if (left > b_w) left = b_w;
465
466 if (x + b_w > w) right = x + b_w - w;
467
468 if (right > b_w) right = b_w;
469
470 copy = b_w - left - right;
471
472 if (left) memset(dst, ref_row[0], left);
473
474 if (copy) memcpy(dst + left, ref_row + x + left, copy);
475
476 if (right) memset(dst + left + copy, ref_row[w - 1], right);
477
478 dst += dst_stride;
479 ++y;
480
481 if (y > 0 && y < h) ref_row += src_stride;
482 } while (--b_h);
483}
484
Wan-Teh Chang12c64e82024-08-08 16:02:19 -0700485static inline int update_extend_mc_border_params(
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530486 const struct scale_factors *const sf, struct buf_2d *const pre_buf,
487 MV32 scaled_mv, PadBlock *block, int subpel_x_mv, int subpel_y_mv,
488 int do_warp, int is_intrabc, int *x_pad, int *y_pad) {
489 const int is_scaled = av1_is_scaled(sf);
490 // Get reference width and height.
491 int frame_width = pre_buf->width;
492 int frame_height = pre_buf->height;
493
494 // Do border extension if there is motion or
495 // width/height is not a multiple of 8 pixels.
496 if ((!is_intrabc) && (!do_warp) &&
497 (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
498 (frame_height & 0x7))) {
499 if (subpel_x_mv || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
500 block->x0 -= AOM_INTERP_EXTEND - 1;
501 block->x1 += AOM_INTERP_EXTEND;
502 *x_pad = 1;
503 }
504
505 if (subpel_y_mv || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
506 block->y0 -= AOM_INTERP_EXTEND - 1;
507 block->y1 += AOM_INTERP_EXTEND;
508 *y_pad = 1;
509 }
510
511 // Skip border extension if block is inside the frame.
512 if (block->x0 < 0 || block->x1 > frame_width - 1 || block->y0 < 0 ||
513 block->y1 > frame_height - 1) {
514 return 1;
515 }
516 }
517 return 0;
518}
519
Wan-Teh Chang12c64e82024-08-08 16:02:19 -0700520static inline void extend_mc_border(const struct scale_factors *const sf,
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530521 struct buf_2d *const pre_buf,
522 MV32 scaled_mv, PadBlock block,
523 int subpel_x_mv, int subpel_y_mv,
524 int do_warp, int is_intrabc, int highbd,
525 uint8_t *mc_buf, uint8_t **pre,
526 int *src_stride) {
527 int x_pad = 0, y_pad = 0;
528 if (update_extend_mc_border_params(sf, pre_buf, scaled_mv, &block,
529 subpel_x_mv, subpel_y_mv, do_warp,
530 is_intrabc, &x_pad, &y_pad)) {
531 // Get reference block pointer.
532 const uint8_t *const buf_ptr =
533 pre_buf->buf0 + block.y0 * pre_buf->stride + block.x0;
534 int buf_stride = pre_buf->stride;
535 const int b_w = block.x1 - block.x0;
536 const int b_h = block.y1 - block.y0;
537
Jerome Jiangebba9c72019-09-20 12:23:01 -0700538#if CONFIG_AV1_HIGHBITDEPTH
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530539 // Extend the border.
540 if (highbd) {
541 highbd_build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0,
542 block.y0, b_w, b_h, pre_buf->width,
543 pre_buf->height);
544 } else {
545 build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
546 b_h, pre_buf->width, pre_buf->height);
547 }
Jerome Jiangebba9c72019-09-20 12:23:01 -0700548#else
549 (void)highbd;
550 build_mc_border(buf_ptr, buf_stride, mc_buf, b_w, block.x0, block.y0, b_w,
551 b_h, pre_buf->width, pre_buf->height);
552#endif
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530553 *src_stride = b_w;
554 *pre = mc_buf + y_pad * (AOM_INTERP_EXTEND - 1) * b_w +
555 x_pad * (AOM_INTERP_EXTEND - 1);
556 }
557}
558
Wan-Teh Chang94534432024-08-08 20:29:57 -0700559static inline void dec_calc_subpel_params(
chiyotsaif4c53bd2022-11-08 12:55:18 -0800560 const MV *const src_mv, InterPredParams *const inter_pred_params,
561 const MACROBLOCKD *const xd, int mi_x, int mi_y, uint8_t **pre,
562 SubpelParams *subpel_params, int *src_stride, PadBlock *block,
563 MV32 *scaled_mv, int *subpel_x_mv, int *subpel_y_mv) {
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700564 const struct scale_factors *sf = inter_pred_params->scale_factors;
565 struct buf_2d *pre_buf = &inter_pred_params->ref_frame_buf;
566 const int bw = inter_pred_params->block_width;
567 const int bh = inter_pred_params->block_height;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530568 const int is_scaled = av1_is_scaled(sf);
569 if (is_scaled) {
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700570 int ssx = inter_pred_params->subsampling_x;
571 int ssy = inter_pred_params->subsampling_y;
572 int orig_pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
573 orig_pos_y += src_mv->row * (1 << (1 - ssy));
574 int orig_pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
575 orig_pos_x += src_mv->col * (1 << (1 - ssx));
chiyotsaifee3f752022-11-08 15:56:34 -0800576 int pos_y = av1_scaled_y(orig_pos_y, sf);
577 int pos_x = av1_scaled_x(orig_pos_x, sf);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530578 pos_x += SCALE_EXTRA_OFF;
579 pos_y += SCALE_EXTRA_OFF;
580
581 const int top = -AOM_LEFT_TOP_MARGIN_SCALED(ssy);
582 const int left = -AOM_LEFT_TOP_MARGIN_SCALED(ssx);
583 const int bottom = (pre_buf->height + AOM_INTERP_EXTEND)
584 << SCALE_SUBPEL_BITS;
585 const int right = (pre_buf->width + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
586 pos_y = clamp(pos_y, top, bottom);
587 pos_x = clamp(pos_x, left, right);
588
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530589 subpel_params->subpel_x = pos_x & SCALE_SUBPEL_MASK;
590 subpel_params->subpel_y = pos_y & SCALE_SUBPEL_MASK;
591 subpel_params->xs = sf->x_step_q4;
592 subpel_params->ys = sf->y_step_q4;
Ravi Chaudhary18c5f6f2018-06-06 13:43:07 +0530593
594 // Get reference block top left coordinate.
595 block->x0 = pos_x >> SCALE_SUBPEL_BITS;
596 block->y0 = pos_y >> SCALE_SUBPEL_BITS;
597
598 // Get reference block bottom right coordinate.
599 block->x1 =
600 ((pos_x + (bw - 1) * subpel_params->xs) >> SCALE_SUBPEL_BITS) + 1;
601 block->y1 =
602 ((pos_y + (bh - 1) * subpel_params->ys) >> SCALE_SUBPEL_BITS) + 1;
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530603
Remy Foray9333b312018-11-28 09:17:05 +0000604 MV temp_mv;
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700605 temp_mv = clamp_mv_to_umv_border_sb(xd, src_mv, bw, bh,
606 inter_pred_params->subsampling_x,
607 inter_pred_params->subsampling_y);
608 *scaled_mv = av1_scale_mv(&temp_mv, mi_x, mi_y, sf);
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530609 scaled_mv->row += SCALE_EXTRA_OFF;
610 scaled_mv->col += SCALE_EXTRA_OFF;
611
612 *subpel_x_mv = scaled_mv->col & SCALE_SUBPEL_MASK;
613 *subpel_y_mv = scaled_mv->row & SCALE_SUBPEL_MASK;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530614 } else {
Ravi Chaudhary18c5f6f2018-06-06 13:43:07 +0530615 // Get block position in current frame.
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700616 int pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
617 int pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
Ravi Chaudhary18c5f6f2018-06-06 13:43:07 +0530618
Remy Foray9333b312018-11-28 09:17:05 +0000619 const MV mv_q4 = clamp_mv_to_umv_border_sb(
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700620 xd, src_mv, bw, bh, inter_pred_params->subsampling_x,
621 inter_pred_params->subsampling_y);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530622 subpel_params->xs = subpel_params->ys = SCALE_SUBPEL_SHIFTS;
623 subpel_params->subpel_x = (mv_q4.col & SUBPEL_MASK) << SCALE_EXTRA_BITS;
624 subpel_params->subpel_y = (mv_q4.row & SUBPEL_MASK) << SCALE_EXTRA_BITS;
Ravi Chaudhary18c5f6f2018-06-06 13:43:07 +0530625
626 // Get reference block top left coordinate.
627 pos_x += mv_q4.col;
628 pos_y += mv_q4.row;
629 block->x0 = pos_x >> SUBPEL_BITS;
630 block->y0 = pos_y >> SUBPEL_BITS;
631
632 // Get reference block bottom right coordinate.
633 block->x1 = (pos_x >> SUBPEL_BITS) + (bw - 1) + 1;
634 block->y1 = (pos_y >> SUBPEL_BITS) + (bh - 1) + 1;
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +0530635
636 scaled_mv->row = mv_q4.row;
637 scaled_mv->col = mv_q4.col;
638 *subpel_x_mv = scaled_mv->col & SUBPEL_MASK;
639 *subpel_y_mv = scaled_mv->row & SUBPEL_MASK;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530640 }
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700641 *pre = pre_buf->buf0 + block->y0 * pre_buf->stride + block->x0;
642 *src_stride = pre_buf->stride;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530643}
644
Wan-Teh Chang94534432024-08-08 20:29:57 -0700645static inline void dec_calc_subpel_params_and_extend(
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700646 const MV *const src_mv, InterPredParams *const inter_pred_params,
Urvang Joshibac1dea2020-04-20 11:37:15 -0700647 MACROBLOCKD *const xd, int mi_x, int mi_y, int ref, uint8_t **mc_buf,
648 uint8_t **pre, SubpelParams *subpel_params, int *src_stride) {
Urvang Joshi628177a2019-09-26 16:50:36 -0700649 PadBlock block;
650 MV32 scaled_mv;
651 int subpel_x_mv, subpel_y_mv;
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700652 dec_calc_subpel_params(src_mv, inter_pred_params, xd, mi_x, mi_y, pre,
653 subpel_params, src_stride, &block, &scaled_mv,
Urvang Joshi628177a2019-09-26 16:50:36 -0700654 &subpel_x_mv, &subpel_y_mv);
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700655 extend_mc_border(
656 inter_pred_params->scale_factors, &inter_pred_params->ref_frame_buf,
657 scaled_mv, block, subpel_x_mv, subpel_y_mv,
658 inter_pred_params->mode == WARP_PRED, inter_pred_params->is_intrabc,
Urvang Joshibac1dea2020-04-20 11:37:15 -0700659 inter_pred_params->use_hbd_buf, mc_buf[ref], pre, src_stride);
Urvang Joshi63de7bb2020-04-10 15:41:39 -0700660}
661
chiyotsai740d4912022-11-10 14:59:18 -0800662#define IS_DEC 1
chiyotsaif4c53bd2022-11-08 12:55:18 -0800663#include "av1/common/reconinter_template.inc"
chiyotsai740d4912022-11-10 14:59:18 -0800664#undef IS_DEC
chiyotsaif4c53bd2022-11-08 12:55:18 -0800665
Urvang Joshibac1dea2020-04-20 11:37:15 -0700666static void dec_build_inter_predictors(const AV1_COMMON *cm,
667 DecoderCodingBlock *dcb, int plane,
668 const MB_MODE_INFO *mi,
Urvang Joshi628177a2019-09-26 16:50:36 -0700669 int build_for_obmc, int bw, int bh,
670 int mi_x, int mi_y) {
chiyotsai740d4912022-11-10 14:59:18 -0800671 build_inter_predictors(cm, &dcb->xd, plane, mi, build_for_obmc, bw, bh, mi_x,
672 mi_y, dcb->mc_buf);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530673}
674
Wan-Teh Chang94534432024-08-08 20:29:57 -0700675static inline void dec_build_inter_predictor(const AV1_COMMON *cm,
676 DecoderCodingBlock *dcb,
677 int mi_row, int mi_col,
678 BLOCK_SIZE bsize) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700679 MACROBLOCKD *const xd = &dcb->xd;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530680 const int num_planes = av1_num_planes(cm);
Xin Zhaoc420ec02020-04-02 10:53:01 -0700681 for (int plane = 0; plane < num_planes; ++plane) {
682 if (plane && !xd->is_chroma_ref) break;
683 const int mi_x = mi_col * MI_SIZE;
684 const int mi_y = mi_row * MI_SIZE;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700685 dec_build_inter_predictors(cm, dcb, plane, xd->mi[0], 0,
Xin Zhaoc420ec02020-04-02 10:53:01 -0700686 xd->plane[plane].width, xd->plane[plane].height,
687 mi_x, mi_y);
688 if (is_interintra_pred(xd->mi[0])) {
689 BUFFER_SET ctx = { { xd->plane[0].dst.buf, xd->plane[1].dst.buf,
690 xd->plane[2].dst.buf },
691 { xd->plane[0].dst.stride, xd->plane[1].dst.stride,
692 xd->plane[2].dst.stride } };
693 av1_build_interintra_predictor(cm, xd, xd->plane[plane].dst.buf,
694 xd->plane[plane].dst.stride, &ctx, plane,
695 bsize);
696 }
697 }
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530698}
699
Wan-Teh Chang12c64e82024-08-08 16:02:19 -0700700static inline void dec_build_prediction_by_above_pred(
Urvang Joshibac1dea2020-04-20 11:37:15 -0700701 MACROBLOCKD *const xd, int rel_mi_row, int rel_mi_col, uint8_t op_mi_size,
Jingning Hanb726c562019-11-12 15:45:50 -0800702 int dir, MB_MODE_INFO *above_mbmi, void *fun_ctxt, const int num_planes) {
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530703 struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
Hui Sub94cd5e2019-11-06 12:05:47 -0800704 const int above_mi_col = xd->mi_col + rel_mi_col;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530705 int mi_x, mi_y;
706 MB_MODE_INFO backup_mbmi = *above_mbmi;
707
Jingning Hanb726c562019-11-12 15:45:50 -0800708 (void)rel_mi_row;
709 (void)dir;
710
711 av1_setup_build_prediction_by_above_pred(xd, rel_mi_col, op_mi_size,
Deepa K G77e15a42018-06-11 15:40:00 +0530712 &backup_mbmi, ctxt, num_planes);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530713 mi_x = above_mi_col << MI_SIZE_LOG2;
Hui Sub94cd5e2019-11-06 12:05:47 -0800714 mi_y = xd->mi_row << MI_SIZE_LOG2;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530715
chiyotsai0f5cd052020-08-27 14:37:44 -0700716 const BLOCK_SIZE bsize = xd->mi[0]->bsize;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530717
718 for (int j = 0; j < num_planes; ++j) {
719 const struct macroblockd_plane *pd = &xd->plane[j];
Jingning Hanb726c562019-11-12 15:45:50 -0800720 int bw = (op_mi_size * MI_SIZE) >> pd->subsampling_x;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530721 int bh = clamp(block_size_high[bsize] >> (pd->subsampling_y + 1), 4,
722 block_size_high[BLOCK_64X64] >> (pd->subsampling_y + 1));
723
724 if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 0)) continue;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700725 dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
726 &backup_mbmi, 1, bw, bh, mi_x, mi_y);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530727 }
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530728}
729
Wan-Teh Chang94534432024-08-08 20:29:57 -0700730static inline void dec_build_prediction_by_above_preds(
Urvang Joshibac1dea2020-04-20 11:37:15 -0700731 const AV1_COMMON *cm, DecoderCodingBlock *dcb,
732 uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
733 int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
734 MACROBLOCKD *const xd = &dcb->xd;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530735 if (!xd->up_available) return;
736
737 // Adjust mb_to_bottom_edge to have the correct value for the OBMC
738 // prediction block. This is half the height of the original block,
739 // except for 128-wide blocks, where we only use a height of 32.
Urvang Joshi33930b82020-04-06 12:41:27 -0700740 const int this_height = xd->height * MI_SIZE;
741 const int pred_height = AOMMIN(this_height / 2, 32);
chiyotsai87bb8052020-02-12 16:56:33 -0800742 xd->mb_to_bottom_edge += GET_MV_SUBPEL(this_height - pred_height);
Urvang Joshibac1dea2020-04-20 11:37:15 -0700743 struct build_prediction_ctxt ctxt = {
744 cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_right_edge, dcb
745 };
chiyotsai0f5cd052020-08-27 14:37:44 -0700746 const BLOCK_SIZE bsize = xd->mi[0]->bsize;
Hui Sub94cd5e2019-11-06 12:05:47 -0800747 foreach_overlappable_nb_above(cm, xd,
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530748 max_neighbor_obmc[mi_size_wide_log2[bsize]],
749 dec_build_prediction_by_above_pred, &ctxt);
750
chiyotsai87bb8052020-02-12 16:56:33 -0800751 xd->mb_to_left_edge = -GET_MV_SUBPEL(xd->mi_col * MI_SIZE);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530752 xd->mb_to_right_edge = ctxt.mb_to_far_edge;
chiyotsai87bb8052020-02-12 16:56:33 -0800753 xd->mb_to_bottom_edge -= GET_MV_SUBPEL(this_height - pred_height);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530754}
755
Wan-Teh Chang12c64e82024-08-08 16:02:19 -0700756static inline void dec_build_prediction_by_left_pred(
Urvang Joshibac1dea2020-04-20 11:37:15 -0700757 MACROBLOCKD *const xd, int rel_mi_row, int rel_mi_col, uint8_t op_mi_size,
Jingning Hanb726c562019-11-12 15:45:50 -0800758 int dir, MB_MODE_INFO *left_mbmi, void *fun_ctxt, const int num_planes) {
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530759 struct build_prediction_ctxt *ctxt = (struct build_prediction_ctxt *)fun_ctxt;
Hui Sub94cd5e2019-11-06 12:05:47 -0800760 const int left_mi_row = xd->mi_row + rel_mi_row;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530761 int mi_x, mi_y;
762 MB_MODE_INFO backup_mbmi = *left_mbmi;
763
Jingning Hanb726c562019-11-12 15:45:50 -0800764 (void)rel_mi_col;
765 (void)dir;
766
767 av1_setup_build_prediction_by_left_pred(xd, rel_mi_row, op_mi_size,
Deepa K G77e15a42018-06-11 15:40:00 +0530768 &backup_mbmi, ctxt, num_planes);
Hui Sub94cd5e2019-11-06 12:05:47 -0800769 mi_x = xd->mi_col << MI_SIZE_LOG2;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530770 mi_y = left_mi_row << MI_SIZE_LOG2;
chiyotsai0f5cd052020-08-27 14:37:44 -0700771 const BLOCK_SIZE bsize = xd->mi[0]->bsize;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530772
773 for (int j = 0; j < num_planes; ++j) {
774 const struct macroblockd_plane *pd = &xd->plane[j];
775 int bw = clamp(block_size_wide[bsize] >> (pd->subsampling_x + 1), 4,
776 block_size_wide[BLOCK_64X64] >> (pd->subsampling_x + 1));
Jingning Hanb726c562019-11-12 15:45:50 -0800777 int bh = (op_mi_size << MI_SIZE_LOG2) >> pd->subsampling_y;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530778
779 if (av1_skip_u4x4_pred_in_obmc(bsize, pd, 1)) continue;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700780 dec_build_inter_predictors(ctxt->cm, (DecoderCodingBlock *)ctxt->dcb, j,
781 &backup_mbmi, 1, bw, bh, mi_x, mi_y);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530782 }
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530783}
784
Wan-Teh Chang94534432024-08-08 20:29:57 -0700785static inline void dec_build_prediction_by_left_preds(
Urvang Joshibac1dea2020-04-20 11:37:15 -0700786 const AV1_COMMON *cm, DecoderCodingBlock *dcb,
787 uint8_t *tmp_buf[MAX_MB_PLANE], int tmp_width[MAX_MB_PLANE],
788 int tmp_height[MAX_MB_PLANE], int tmp_stride[MAX_MB_PLANE]) {
789 MACROBLOCKD *const xd = &dcb->xd;
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530790 if (!xd->left_available) return;
791
792 // Adjust mb_to_right_edge to have the correct value for the OBMC
793 // prediction block. This is half the width of the original block,
794 // except for 128-wide blocks, where we only use a width of 32.
Urvang Joshi33930b82020-04-06 12:41:27 -0700795 const int this_width = xd->width * MI_SIZE;
796 const int pred_width = AOMMIN(this_width / 2, 32);
chiyotsai87bb8052020-02-12 16:56:33 -0800797 xd->mb_to_right_edge += GET_MV_SUBPEL(this_width - pred_width);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530798
Urvang Joshibac1dea2020-04-20 11:37:15 -0700799 struct build_prediction_ctxt ctxt = {
800 cm, tmp_buf, tmp_width, tmp_height, tmp_stride, xd->mb_to_bottom_edge, dcb
801 };
chiyotsai0f5cd052020-08-27 14:37:44 -0700802 const BLOCK_SIZE bsize = xd->mi[0]->bsize;
Hui Sub94cd5e2019-11-06 12:05:47 -0800803 foreach_overlappable_nb_left(cm, xd,
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530804 max_neighbor_obmc[mi_size_high_log2[bsize]],
805 dec_build_prediction_by_left_pred, &ctxt);
806
chiyotsai87bb8052020-02-12 16:56:33 -0800807 xd->mb_to_top_edge = -GET_MV_SUBPEL(xd->mi_row * MI_SIZE);
808 xd->mb_to_right_edge -= GET_MV_SUBPEL(this_width - pred_width);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530809 xd->mb_to_bottom_edge = ctxt.mb_to_far_edge;
810}
811
Wan-Teh Chang94534432024-08-08 20:29:57 -0700812static inline void dec_build_obmc_inter_predictors_sb(const AV1_COMMON *cm,
813 DecoderCodingBlock *dcb) {
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530814 const int num_planes = av1_num_planes(cm);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530815 uint8_t *dst_buf1[MAX_MB_PLANE], *dst_buf2[MAX_MB_PLANE];
816 int dst_stride1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
817 int dst_stride2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
818 int dst_width1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
819 int dst_width2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
820 int dst_height1[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
821 int dst_height2[MAX_MB_PLANE] = { MAX_SB_SIZE, MAX_SB_SIZE, MAX_SB_SIZE };
822
Urvang Joshibac1dea2020-04-20 11:37:15 -0700823 MACROBLOCKD *const xd = &dcb->xd;
Urvang Joshi7ccbade2020-04-16 12:48:42 -0700824 av1_setup_obmc_dst_bufs(xd, dst_buf1, dst_buf2);
Jerome Jiangebba9c72019-09-20 12:23:01 -0700825
Urvang Joshibac1dea2020-04-20 11:37:15 -0700826 dec_build_prediction_by_above_preds(cm, dcb, dst_buf1, dst_width1,
827 dst_height1, dst_stride1);
828 dec_build_prediction_by_left_preds(cm, dcb, dst_buf2, dst_width2, dst_height2,
Hui Sub94cd5e2019-11-06 12:05:47 -0800829 dst_stride2);
830 const int mi_row = xd->mi_row;
831 const int mi_col = xd->mi_col;
chiyotsai0f5cd052020-08-27 14:37:44 -0700832 av1_setup_dst_planes(xd->plane, xd->mi[0]->bsize, &cm->cur_frame->buf, mi_row,
833 mi_col, 0, num_planes);
Hui Sub94cd5e2019-11-06 12:05:47 -0800834 av1_build_obmc_inter_prediction(cm, xd, dst_buf1, dst_stride1, dst_buf2,
835 dst_stride2);
Ravi Chaudhary6ac4b132018-05-08 16:34:44 +0530836}
837
Wan-Teh Chang94534432024-08-08 20:29:57 -0700838static inline void cfl_store_inter_block(AV1_COMMON *const cm,
839 MACROBLOCKD *const xd) {
Deepa K G49607302018-06-18 14:00:57 +0530840 MB_MODE_INFO *mbmi = xd->mi[0];
841 if (store_cfl_required(cm, xd)) {
chiyotsai0f5cd052020-08-27 14:37:44 -0700842 cfl_store_block(xd, mbmi->bsize, mbmi->tx_size);
Deepa K G49607302018-06-18 14:00:57 +0530843 }
844}
845
Wan-Teh Chang94534432024-08-08 20:29:57 -0700846static inline void predict_inter_block(AV1_COMMON *const cm,
847 DecoderCodingBlock *dcb,
848 BLOCK_SIZE bsize) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700849 MACROBLOCKD *const xd = &dcb->xd;
Deepa K G49607302018-06-18 14:00:57 +0530850 MB_MODE_INFO *mbmi = xd->mi[0];
851 const int num_planes = av1_num_planes(cm);
Hui Sub94cd5e2019-11-06 12:05:47 -0800852 const int mi_row = xd->mi_row;
853 const int mi_col = xd->mi_col;
Deepa K G49607302018-06-18 14:00:57 +0530854 for (int ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
855 const MV_REFERENCE_FRAME frame = mbmi->ref_frame[ref];
856 if (frame < LAST_FRAME) {
857 assert(is_intrabc_block(mbmi));
858 assert(frame == INTRA_FRAME);
859 assert(ref == 0);
860 } else {
David Turnera21966b2018-12-05 14:48:49 +0000861 const RefCntBuffer *ref_buf = get_ref_frame_buf(cm, frame);
862 const struct scale_factors *ref_scale_factors =
863 get_ref_scale_factors_const(cm, frame);
Deepa K G49607302018-06-18 14:00:57 +0530864
David Turnera21966b2018-12-05 14:48:49 +0000865 xd->block_ref_scale_factors[ref] = ref_scale_factors;
866 av1_setup_pre_planes(xd, ref, &ref_buf->buf, mi_row, mi_col,
867 ref_scale_factors, num_planes);
Deepa K G49607302018-06-18 14:00:57 +0530868 }
869 }
870
Urvang Joshibac1dea2020-04-20 11:37:15 -0700871 dec_build_inter_predictor(cm, dcb, mi_row, mi_col, bsize);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700872 if (mbmi->motion_mode == OBMC_CAUSAL) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700873 dec_build_obmc_inter_predictors_sb(cm, dcb);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700874 }
Deepa K G1909dee2018-07-03 17:37:56 +0530875#if CONFIG_MISMATCH_DEBUG
876 for (int plane = 0; plane < num_planes; ++plane) {
877 const struct macroblockd_plane *pd = &xd->plane[plane];
878 int pixel_c, pixel_r;
879 mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, 0, 0, pd->subsampling_x,
880 pd->subsampling_y);
881 if (!is_chroma_reference(mi_row, mi_col, bsize, pd->subsampling_x,
882 pd->subsampling_y))
883 continue;
David Turnerd2a592e2018-11-16 14:59:31 +0000884 mismatch_check_block_pre(pd->dst.buf, pd->dst.stride,
885 cm->current_frame.order_hint, plane, pixel_c,
886 pixel_r, pd->width, pd->height,
Deepa K G1909dee2018-07-03 17:37:56 +0530887 xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
888 }
889#endif
Deepa K G49607302018-06-18 14:00:57 +0530890}
891
Wan-Teh Chang94534432024-08-08 20:29:57 -0700892static inline void set_color_index_map_offset(MACROBLOCKD *const xd, int plane,
893 aom_reader *r) {
Deepa K G99f7ddb2018-06-20 11:21:21 +0530894 (void)r;
895 Av1ColorMapParam params;
896 const MB_MODE_INFO *const mbmi = xd->mi[0];
chiyotsai0f5cd052020-08-27 14:37:44 -0700897 av1_get_block_dimensions(mbmi->bsize, plane, xd, &params.plane_width,
Deepa K G99f7ddb2018-06-20 11:21:21 +0530898 &params.plane_height, NULL, NULL);
899 xd->color_index_map_offset[plane] += params.plane_width * params.plane_height;
900}
901
Wan-Teh Chang94534432024-08-08 20:29:57 -0700902static inline void decode_token_recon_block(AV1Decoder *const pbi,
903 ThreadData *const td, aom_reader *r,
904 BLOCK_SIZE bsize) {
Yue Chen64550b62017-01-12 12:18:22 -0800905 AV1_COMMON *const cm = &pbi->common;
Urvang Joshibac1dea2020-04-20 11:37:15 -0700906 DecoderCodingBlock *const dcb = &td->dcb;
907 MACROBLOCKD *const xd = &dcb->xd;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000908 const int num_planes = av1_num_planes(cm);
Yue Chen53b53f02018-03-29 14:31:23 -0700909 MB_MODE_INFO *mbmi = xd->mi[0];
Yue Chen19e7aa82016-11-30 14:05:39 -0800910
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 if (!is_inter_block(mbmi)) {
Jingning Hanad54a982018-01-12 14:40:29 -0800912 int row, col;
Cheng Chen8ab1f442018-04-27 18:01:52 -0700913 assert(bsize == get_plane_block_size(bsize, xd->plane[0].subsampling_x,
914 xd->plane[0].subsampling_y));
Yushin Choca63a8b2018-04-20 16:46:36 -0700915 const int max_blocks_wide = max_block_wide(xd, bsize, 0);
916 const int max_blocks_high = max_block_high(xd, bsize, 0);
917 const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
Hui Suadda5872019-12-09 10:08:49 -0800918 int mu_blocks_wide = mi_size_wide[max_unit_bsize];
919 int mu_blocks_high = mi_size_high[max_unit_bsize];
Jingning Hanad54a982018-01-12 14:40:29 -0800920 mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
921 mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
Jingning Han5b701742017-07-19 14:39:07 -0700922
Jingning Hanad54a982018-01-12 14:40:29 -0800923 for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
924 for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
925 for (int plane = 0; plane < num_planes; ++plane) {
Hui Su474e1e12020-02-27 15:46:36 -0800926 if (plane && !xd->is_chroma_ref) break;
Jingning Hanad54a982018-01-12 14:40:29 -0800927 const struct macroblockd_plane *const pd = &xd->plane[plane];
Jingning Hanad54a982018-01-12 14:40:29 -0800928 const TX_SIZE tx_size = av1_get_tx_size(plane, xd);
929 const int stepr = tx_size_high_unit[tx_size];
930 const int stepc = tx_size_wide_unit[tx_size];
931
932 const int unit_height = ROUND_POWER_OF_TWO(
933 AOMMIN(mu_blocks_high + row, max_blocks_high), pd->subsampling_y);
934 const int unit_width = ROUND_POWER_OF_TWO(
935 AOMMIN(mu_blocks_wide + col, max_blocks_wide), pd->subsampling_x);
936
937 for (int blk_row = row >> pd->subsampling_y; blk_row < unit_height;
Deepa K G49607302018-06-18 14:00:57 +0530938 blk_row += stepr) {
Jingning Hanad54a982018-01-12 14:40:29 -0800939 for (int blk_col = col >> pd->subsampling_x; blk_col < unit_width;
Deepa K G49607302018-06-18 14:00:57 +0530940 blk_col += stepc) {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700941 td->read_coeffs_tx_intra_block_visit(cm, dcb, r, plane, blk_row,
Deepa K G1909dee2018-07-03 17:37:56 +0530942 blk_col, tx_size);
Urvang Joshibac1dea2020-04-20 11:37:15 -0700943 td->predict_and_recon_intra_block_visit(
944 cm, dcb, r, plane, blk_row, blk_col, tx_size);
945 set_cb_buffer_offsets(dcb, tx_size, plane);
Deepa K G3bc1f932018-06-21 14:25:03 +0530946 }
947 }
948 }
949 }
950 }
951 } else {
Urvang Joshibac1dea2020-04-20 11:37:15 -0700952 td->predict_inter_block_visit(cm, dcb, bsize);
Deepa K G3bc1f932018-06-21 14:25:03 +0530953 // Reconstruction
chiyotsai8c004e12020-04-17 15:52:08 -0700954 if (!mbmi->skip_txfm) {
Deepa K G3bc1f932018-06-21 14:25:03 +0530955 int eobtotal = 0;
956
957 const int max_blocks_wide = max_block_wide(xd, bsize, 0);
958 const int max_blocks_high = max_block_high(xd, bsize, 0);
959 int row, col;
960
961 const BLOCK_SIZE max_unit_bsize = BLOCK_64X64;
962 assert(max_unit_bsize ==
963 get_plane_block_size(BLOCK_64X64, xd->plane[0].subsampling_x,
964 xd->plane[0].subsampling_y));
Hui Suadda5872019-12-09 10:08:49 -0800965 int mu_blocks_wide = mi_size_wide[max_unit_bsize];
966 int mu_blocks_high = mi_size_high[max_unit_bsize];
Deepa K G3bc1f932018-06-21 14:25:03 +0530967
968 mu_blocks_wide = AOMMIN(max_blocks_wide, mu_blocks_wide);
969 mu_blocks_high = AOMMIN(max_blocks_high, mu_blocks_high);
970
971 for (row = 0; row < max_blocks_high; row += mu_blocks_high) {
972 for (col = 0; col < max_blocks_wide; col += mu_blocks_wide) {
973 for (int plane = 0; plane < num_planes; ++plane) {
Hui Su474e1e12020-02-27 15:46:36 -0800974 if (plane && !xd->is_chroma_ref) break;
Deepa K G3bc1f932018-06-21 14:25:03 +0530975 const struct macroblockd_plane *const pd = &xd->plane[plane];
Hui Su56e838f2019-12-04 14:48:30 -0800976 const int ss_x = pd->subsampling_x;
977 const int ss_y = pd->subsampling_y;
Hui Su56e838f2019-12-04 14:48:30 -0800978 const BLOCK_SIZE plane_bsize =
Hui Suaadb0b42019-12-06 15:56:47 -0800979 get_plane_block_size(bsize, ss_x, ss_y);
Deepa K G3bc1f932018-06-21 14:25:03 +0530980 const TX_SIZE max_tx_size =
981 get_vartx_max_txsize(xd, plane_bsize, plane);
982 const int bh_var_tx = tx_size_high_unit[max_tx_size];
983 const int bw_var_tx = tx_size_wide_unit[max_tx_size];
984 int block = 0;
985 int step =
986 tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
987 int blk_row, blk_col;
988 const int unit_height = ROUND_POWER_OF_TWO(
Hui Su56e838f2019-12-04 14:48:30 -0800989 AOMMIN(mu_blocks_high + row, max_blocks_high), ss_y);
Deepa K G3bc1f932018-06-21 14:25:03 +0530990 const int unit_width = ROUND_POWER_OF_TWO(
Hui Su56e838f2019-12-04 14:48:30 -0800991 AOMMIN(mu_blocks_wide + col, max_blocks_wide), ss_x);
Deepa K G3bc1f932018-06-21 14:25:03 +0530992
Hui Su56e838f2019-12-04 14:48:30 -0800993 for (blk_row = row >> ss_y; blk_row < unit_height;
Deepa K G3bc1f932018-06-21 14:25:03 +0530994 blk_row += bh_var_tx) {
Hui Su56e838f2019-12-04 14:48:30 -0800995 for (blk_col = col >> ss_x; blk_col < unit_width;
Deepa K G3bc1f932018-06-21 14:25:03 +0530996 blk_col += bw_var_tx) {
Deepa K G1909dee2018-07-03 17:37:56 +0530997 decode_reconstruct_tx(cm, td, r, mbmi, plane, plane_bsize,
Deepa K G3bc1f932018-06-21 14:25:03 +0530998 blk_row, blk_col, block, max_tx_size,
Deepa K G1909dee2018-07-03 17:37:56 +0530999 &eobtotal);
Deepa K G3bc1f932018-06-21 14:25:03 +05301000 block += step;
1001 }
1002 }
1003 }
1004 }
1005 }
1006 }
Deepa K G1909dee2018-07-03 17:37:56 +05301007 td->cfl_store_inter_block_visit(cm, xd);
Luc Trudeaub05eeae2017-08-18 15:14:30 -04001008 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001009
Hui Su474e1e12020-02-27 15:46:36 -08001010 av1_visit_palette(pbi, xd, r, set_color_index_map_offset);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001011}
1012
Wan-Teh Chang94534432024-08-08 20:29:57 -07001013static inline void set_inter_tx_size(MB_MODE_INFO *mbmi, int stride_log2,
1014 int tx_w_log2, int tx_h_log2, int min_txs,
1015 int split_size, int txs, int blk_row,
1016 int blk_col) {
Jim Bankoski7634c332018-10-14 09:31:47 -07001017 for (int idy = 0; idy < tx_size_high_unit[split_size];
1018 idy += tx_size_high_unit[min_txs]) {
1019 for (int idx = 0; idx < tx_size_wide_unit[split_size];
1020 idx += tx_size_wide_unit[min_txs]) {
1021 const int index = (((blk_row + idy) >> tx_h_log2) << stride_log2) +
1022 ((blk_col + idx) >> tx_w_log2);
1023 mbmi->inter_tx_size[index] = txs;
1024 }
1025 }
1026}
1027
Wan-Teh Chang94534432024-08-08 20:29:57 -07001028static inline void read_tx_size_vartx(MACROBLOCKD *xd, MB_MODE_INFO *mbmi,
1029 TX_SIZE tx_size, int depth, int blk_row,
1030 int blk_col, aom_reader *r) {
Jingning Han088217b2018-02-23 21:55:21 -08001031 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1032 int is_split = 0;
chiyotsai0f5cd052020-08-27 14:37:44 -07001033 const BLOCK_SIZE bsize = mbmi->bsize;
Jingning Han088217b2018-02-23 21:55:21 -08001034 const int max_blocks_high = max_block_high(xd, bsize, 0);
1035 const int max_blocks_wide = max_block_wide(xd, bsize, 0);
1036 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
1037 assert(tx_size > TX_4X4);
Jim Bankoski7634c332018-10-14 09:31:47 -07001038 TX_SIZE txs = max_txsize_rect_lookup[bsize];
1039 for (int level = 0; level < MAX_VARTX_DEPTH - 1; ++level)
1040 txs = sub_tx_size_map[txs];
1041 const int tx_w_log2 = tx_size_wide_log2[txs] - MI_SIZE_LOG2;
1042 const int tx_h_log2 = tx_size_high_log2[txs] - MI_SIZE_LOG2;
1043 const int bw_log2 = mi_size_wide_log2[bsize];
1044 const int stride_log2 = bw_log2 - tx_w_log2;
Jingning Han088217b2018-02-23 21:55:21 -08001045
1046 if (depth == MAX_VARTX_DEPTH) {
Jim Bankoski7634c332018-10-14 09:31:47 -07001047 set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1048 tx_size, blk_row, blk_col);
Jingning Han088217b2018-02-23 21:55:21 -08001049 mbmi->tx_size = tx_size;
Jingning Han088217b2018-02-23 21:55:21 -08001050 txfm_partition_update(xd->above_txfm_context + blk_col,
1051 xd->left_txfm_context + blk_row, tx_size, tx_size);
1052 return;
1053 }
1054
1055 const int ctx = txfm_partition_context(xd->above_txfm_context + blk_col,
1056 xd->left_txfm_context + blk_row,
chiyotsai0f5cd052020-08-27 14:37:44 -07001057 mbmi->bsize, tx_size);
Jingning Han088217b2018-02-23 21:55:21 -08001058 is_split = aom_read_symbol(r, ec_ctx->txfm_partition_cdf[ctx], 2, ACCT_STR);
1059
1060 if (is_split) {
Frederic Barbier4b56b102018-03-30 16:09:34 +02001061 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
Jingning Han088217b2018-02-23 21:55:21 -08001062 const int bsw = tx_size_wide_unit[sub_txs];
1063 const int bsh = tx_size_high_unit[sub_txs];
1064
1065 if (sub_txs == TX_4X4) {
Jim Bankoski7634c332018-10-14 09:31:47 -07001066 set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1067 sub_txs, blk_row, blk_col);
Jingning Han088217b2018-02-23 21:55:21 -08001068 mbmi->tx_size = sub_txs;
Jingning Han088217b2018-02-23 21:55:21 -08001069 txfm_partition_update(xd->above_txfm_context + blk_col,
1070 xd->left_txfm_context + blk_row, sub_txs, tx_size);
1071 return;
1072 }
1073
1074 assert(bsw > 0 && bsh > 0);
1075 for (int row = 0; row < tx_size_high_unit[tx_size]; row += bsh) {
1076 for (int col = 0; col < tx_size_wide_unit[tx_size]; col += bsw) {
1077 int offsetr = blk_row + row;
1078 int offsetc = blk_col + col;
Yannis Guyonee564d22021-06-15 13:25:50 +02001079 read_tx_size_vartx(xd, mbmi, sub_txs, depth + 1, offsetr, offsetc, r);
Jingning Han088217b2018-02-23 21:55:21 -08001080 }
1081 }
1082 } else {
Jim Bankoski7634c332018-10-14 09:31:47 -07001083 set_inter_tx_size(mbmi, stride_log2, tx_w_log2, tx_h_log2, txs, tx_size,
1084 tx_size, blk_row, blk_col);
Jingning Han088217b2018-02-23 21:55:21 -08001085 mbmi->tx_size = tx_size;
Jingning Han088217b2018-02-23 21:55:21 -08001086 txfm_partition_update(xd->above_txfm_context + blk_col,
1087 xd->left_txfm_context + blk_row, tx_size, tx_size);
1088 }
1089}
1090
Urvang Joshi6237b882020-03-26 15:02:26 -07001091static TX_SIZE read_selected_tx_size(const MACROBLOCKD *const xd,
1092 aom_reader *r) {
Jingning Han088217b2018-02-23 21:55:21 -08001093 // TODO(debargha): Clean up the logic here. This function should only
1094 // be called for intra.
chiyotsai0f5cd052020-08-27 14:37:44 -07001095 const BLOCK_SIZE bsize = xd->mi[0]->bsize;
Frederic Barbier4b56b102018-03-30 16:09:34 +02001096 const int32_t tx_size_cat = bsize_to_tx_size_cat(bsize);
1097 const int max_depths = bsize_to_max_depth(bsize);
Jingning Han088217b2018-02-23 21:55:21 -08001098 const int ctx = get_tx_size_context(xd);
1099 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
1100 const int depth = aom_read_symbol(r, ec_ctx->tx_size_cdf[tx_size_cat][ctx],
1101 max_depths + 1, ACCT_STR);
1102 assert(depth >= 0 && depth <= max_depths);
Frederic Barbier4b56b102018-03-30 16:09:34 +02001103 const TX_SIZE tx_size = depth_to_tx_size(depth, bsize);
Jingning Han088217b2018-02-23 21:55:21 -08001104 return tx_size;
1105}
1106
Urvang Joshi6237b882020-03-26 15:02:26 -07001107static TX_SIZE read_tx_size(const MACROBLOCKD *const xd, TX_MODE tx_mode,
1108 int is_inter, int allow_select_inter,
1109 aom_reader *r) {
chiyotsai0f5cd052020-08-27 14:37:44 -07001110 const BLOCK_SIZE bsize = xd->mi[0]->bsize;
Yue Chen53b53f02018-03-29 14:31:23 -07001111 if (xd->lossless[xd->mi[0]->segment_id]) return TX_4X4;
Jingning Han088217b2018-02-23 21:55:21 -08001112
1113 if (block_signals_txsize(bsize)) {
1114 if ((!is_inter || allow_select_inter) && tx_mode == TX_MODE_SELECT) {
Frederic Barbier4b56b102018-03-30 16:09:34 +02001115 const TX_SIZE coded_tx_size = read_selected_tx_size(xd, r);
Jingning Han088217b2018-02-23 21:55:21 -08001116 return coded_tx_size;
1117 } else {
1118 return tx_size_from_tx_mode(bsize, tx_mode);
1119 }
1120 } else {
1121 assert(IMPLIES(tx_mode == ONLY_4X4, bsize == BLOCK_4X4));
Urvang Joshidd0376f2018-05-02 16:37:25 -07001122 return max_txsize_rect_lookup[bsize];
Jingning Han088217b2018-02-23 21:55:21 -08001123 }
1124}
1125
Wan-Teh Chang94534432024-08-08 20:29:57 -07001126static inline void parse_decode_block(AV1Decoder *const pbi,
1127 ThreadData *const td, int mi_row,
1128 int mi_col, aom_reader *r,
1129 PARTITION_TYPE partition,
1130 BLOCK_SIZE bsize) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07001131 DecoderCodingBlock *const dcb = &td->dcb;
1132 MACROBLOCKD *const xd = &dcb->xd;
1133 decode_mbmi_block(pbi, dcb, mi_row, mi_col, r, partition, bsize);
Jingning Han088217b2018-02-23 21:55:21 -08001134
Hui Su474e1e12020-02-27 15:46:36 -08001135 av1_visit_palette(pbi, xd, r, av1_decode_palette_tokens);
Jingning Han088217b2018-02-23 21:55:21 -08001136
1137 AV1_COMMON *cm = &pbi->common;
Deepa K G3bc1f932018-06-21 14:25:03 +05301138 const int num_planes = av1_num_planes(cm);
Yue Chen53b53f02018-03-29 14:31:23 -07001139 MB_MODE_INFO *mbmi = xd->mi[0];
Jingning Han088217b2018-02-23 21:55:21 -08001140 int inter_block_tx = is_inter_block(mbmi) || is_intrabc_block(mbmi);
Urvang Joshi6237b882020-03-26 15:02:26 -07001141 if (cm->features.tx_mode == TX_MODE_SELECT && block_signals_txsize(bsize) &&
chiyotsai8c004e12020-04-17 15:52:08 -07001142 !mbmi->skip_txfm && inter_block_tx && !xd->lossless[mbmi->segment_id]) {
Urvang Joshidd0376f2018-05-02 16:37:25 -07001143 const TX_SIZE max_tx_size = max_txsize_rect_lookup[bsize];
Jingning Han088217b2018-02-23 21:55:21 -08001144 const int bh = tx_size_high_unit[max_tx_size];
1145 const int bw = tx_size_wide_unit[max_tx_size];
Hui Suadda5872019-12-09 10:08:49 -08001146 const int width = mi_size_wide[bsize];
1147 const int height = mi_size_high[bsize];
Jingning Han088217b2018-02-23 21:55:21 -08001148
Jingning Han088217b2018-02-23 21:55:21 -08001149 for (int idy = 0; idy < height; idy += bh)
1150 for (int idx = 0; idx < width; idx += bw)
Yannis Guyonee564d22021-06-15 13:25:50 +02001151 read_tx_size_vartx(xd, mbmi, max_tx_size, 0, idy, idx, r);
Jingning Han088217b2018-02-23 21:55:21 -08001152 } else {
chiyotsai8c004e12020-04-17 15:52:08 -07001153 mbmi->tx_size = read_tx_size(xd, cm->features.tx_mode, inter_block_tx,
1154 !mbmi->skip_txfm, r);
Jingning Han088217b2018-02-23 21:55:21 -08001155 if (inter_block_tx)
1156 memset(mbmi->inter_tx_size, mbmi->tx_size, sizeof(mbmi->inter_tx_size));
Urvang Joshi33930b82020-04-06 12:41:27 -07001157 set_txfm_ctxs(mbmi->tx_size, xd->width, xd->height,
chiyotsai8c004e12020-04-17 15:52:08 -07001158 mbmi->skip_txfm && is_inter_block(mbmi), xd);
Jingning Han088217b2018-02-23 21:55:21 -08001159 }
1160
David Turnerebf96f42018-11-14 16:57:57 +00001161 if (cm->delta_q_info.delta_q_present_flag) {
Deepa K G3bc1f932018-06-21 14:25:03 +05301162 for (int i = 0; i < MAX_SEGMENTS; i++) {
1163 const int current_qindex =
Urvang Joshiacad1ca2020-04-27 17:03:25 -07001164 av1_get_qindex(&cm->seg, i, xd->current_base_qindex);
Urvang Joshi17814622020-03-27 17:26:17 -07001165 const CommonQuantParams *const quant_params = &cm->quant_params;
Deepa K G3bc1f932018-06-21 14:25:03 +05301166 for (int j = 0; j < num_planes; ++j) {
Urvang Joshi17814622020-03-27 17:26:17 -07001167 const int dc_delta_q = j == 0 ? quant_params->y_dc_delta_q
1168 : (j == 1 ? quant_params->u_dc_delta_q
1169 : quant_params->v_dc_delta_q);
1170 const int ac_delta_q = j == 0 ? 0
1171 : (j == 1 ? quant_params->u_ac_delta_q
1172 : quant_params->v_ac_delta_q);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001173 xd->plane[j].seg_dequant_QTX[i][0] = av1_dc_quant_QTX(
Tarundeep Singh4243e622021-04-20 16:10:22 +05301174 current_qindex, dc_delta_q, cm->seq_params->bit_depth);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001175 xd->plane[j].seg_dequant_QTX[i][1] = av1_ac_quant_QTX(
Tarundeep Singh4243e622021-04-20 16:10:22 +05301176 current_qindex, ac_delta_q, cm->seq_params->bit_depth);
Deepa K G3bc1f932018-06-21 14:25:03 +05301177 }
1178 }
1179 }
chiyotsai8c004e12020-04-17 15:52:08 -07001180 if (mbmi->skip_txfm) av1_reset_entropy_context(xd, bsize, num_planes);
Deepa K G3bc1f932018-06-21 14:25:03 +05301181
Hui Sud62a63a2020-02-27 16:59:54 -08001182 decode_token_recon_block(pbi, td, r, bsize);
Deepa K G3bc1f932018-06-21 14:25:03 +05301183}
1184
Wan-Teh Chang94534432024-08-08 20:29:57 -07001185static inline void set_offsets_for_pred_and_recon(AV1Decoder *const pbi,
1186 ThreadData *const td,
1187 int mi_row, int mi_col,
1188 BLOCK_SIZE bsize) {
Deepa K G7d17c382018-07-10 20:07:08 +05301189 AV1_COMMON *const cm = &pbi->common;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001190 const CommonModeInfoParams *const mi_params = &cm->mi_params;
Urvang Joshibac1dea2020-04-20 11:37:15 -07001191 DecoderCodingBlock *const dcb = &td->dcb;
1192 MACROBLOCKD *const xd = &dcb->xd;
Deepa K G7d17c382018-07-10 20:07:08 +05301193 const int bw = mi_size_wide[bsize];
1194 const int bh = mi_size_high[bsize];
1195 const int num_planes = av1_num_planes(cm);
1196
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001197 const int offset = mi_row * mi_params->mi_stride + mi_col;
Deepa K G7d17c382018-07-10 20:07:08 +05301198 const TileInfo *const tile = &xd->tile;
1199
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001200 xd->mi = mi_params->mi_grid_base + offset;
1201 xd->tx_type_map =
1202 &mi_params->tx_type_map[mi_row * mi_params->mi_stride + mi_col];
1203 xd->tx_type_map_stride = mi_params->mi_stride;
Deepa K G7d17c382018-07-10 20:07:08 +05301204
1205 set_plane_n4(xd, bw, bh, num_planes);
1206
1207 // Distance of Mb to the various image edges. These are specified to 8th pel
1208 // as they are always compared to values that are in 1/8th pel units
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001209 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows,
1210 mi_params->mi_cols);
Deepa K G7d17c382018-07-10 20:07:08 +05301211
Jack Haughtonddb80602018-11-21 16:41:49 +00001212 av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
1213 num_planes);
Deepa K G7d17c382018-07-10 20:07:08 +05301214}
1215
Wan-Teh Chang94534432024-08-08 20:29:57 -07001216static inline void decode_block(AV1Decoder *const pbi, ThreadData *const td,
1217 int mi_row, int mi_col, aom_reader *r,
1218 PARTITION_TYPE partition, BLOCK_SIZE bsize) {
Deepa K G3bc1f932018-06-21 14:25:03 +05301219 (void)partition;
Deepa K G7d17c382018-07-10 20:07:08 +05301220 set_offsets_for_pred_and_recon(pbi, td, mi_row, mi_col, bsize);
Hui Sud62a63a2020-02-27 16:59:54 -08001221 decode_token_recon_block(pbi, td, r, bsize);
Yue Chen64550b62017-01-12 12:18:22 -08001222}
1223
Rupert Swarbrickeb123932017-11-22 15:20:47 +00001224static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
1225 aom_reader *r, int has_rows, int has_cols,
Jingning Han1beb0102016-12-07 11:08:30 -08001226 BLOCK_SIZE bsize) {
1227 const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
Thomas Daviesc2ec0e42017-01-11 16:27:27 +00001228 FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001229
Rupert Swarbrickeb123932017-11-22 15:20:47 +00001230 if (!has_rows && !has_cols) return PARTITION_SPLIT;
Jingning Han5fe79db2017-03-27 15:10:30 -07001231
Rupert Swarbrickeb123932017-11-22 15:20:47 +00001232 assert(ctx >= 0);
1233 aom_cdf_prob *partition_cdf = ec_ctx->partition_cdf[ctx];
Rupert Swarbrickb95cf122017-07-31 16:51:12 +01001234 if (has_rows && has_cols) {
Rupert Swarbrickeb123932017-11-22 15:20:47 +00001235 return (PARTITION_TYPE)aom_read_symbol(
1236 r, partition_cdf, partition_cdf_length(bsize), ACCT_STR);
Rupert Swarbrickb95cf122017-07-31 16:51:12 +01001237 } else if (!has_rows && has_cols) {
Stanislav Vitvitskyy8711cf52017-08-18 15:17:57 -07001238 assert(bsize > BLOCK_8X8);
1239 aom_cdf_prob cdf[2];
Rupert Swarbrickeb123932017-11-22 15:20:47 +00001240 partition_gather_vert_alike(cdf, partition_cdf, bsize);
Stanislav Vitvitskyy8711cf52017-08-18 15:17:57 -07001241 assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
Rupert Swarbrickeb123932017-11-22 15:20:47 +00001242 return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_HORZ;
1243 } else {
1244 assert(has_rows && !has_cols);
Stanislav Vitvitskyy8711cf52017-08-18 15:17:57 -07001245 assert(bsize > BLOCK_8X8);
1246 aom_cdf_prob cdf[2];
Rupert Swarbrickeb123932017-11-22 15:20:47 +00001247 partition_gather_horz_alike(cdf, partition_cdf, bsize);
Stanislav Vitvitskyy8711cf52017-08-18 15:17:57 -07001248 assert(cdf[1] == AOM_ICDF(CDF_PROB_TOP));
Rupert Swarbrickeb123932017-11-22 15:20:47 +00001249 return aom_read_cdf(r, cdf, 2, ACCT_STR) ? PARTITION_SPLIT : PARTITION_VERT;
Rupert Swarbrickb95cf122017-07-31 16:51:12 +01001250 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001251}
1252
Yaowu Xuc27fc142016-08-22 16:08:15 -07001253// TODO(slavarnway): eliminate bsize and subsize in future commits
Wan-Teh Chang94534432024-08-08 20:29:57 -07001254static inline void decode_partition(AV1Decoder *const pbi, ThreadData *const td,
1255 int mi_row, int mi_col, aom_reader *reader,
1256 BLOCK_SIZE bsize, int parse_decode_flag) {
kyslov5859dca2019-04-08 12:13:11 -07001257 assert(bsize < BLOCK_SIZES_ALL);
Yaowu Xuf883b422016-08-30 14:01:10 -07001258 AV1_COMMON *const cm = &pbi->common;
Urvang Joshibac1dea2020-04-20 11:37:15 -07001259 DecoderCodingBlock *const dcb = &td->dcb;
1260 MACROBLOCKD *const xd = &dcb->xd;
Yushin Chocee5d202018-03-29 14:06:54 -07001261 const int bw = mi_size_wide[bsize];
1262 const int hbs = bw >> 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001263 PARTITION_TYPE partition;
1264 BLOCK_SIZE subsize;
Yushin Chocee5d202018-03-29 14:06:54 -07001265 const int quarter_step = bw / 4;
Cheng Chen82b4fa12018-05-02 18:43:17 -07001266 BLOCK_SIZE bsize2 = get_partition_subsize(bsize, PARTITION_SPLIT);
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001267 const int has_rows = (mi_row + hbs) < cm->mi_params.mi_rows;
1268 const int has_cols = (mi_col + hbs) < cm->mi_params.mi_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001269
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001270 if (mi_row >= cm->mi_params.mi_rows || mi_col >= cm->mi_params.mi_cols)
1271 return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001272
Deepa K G3bc1f932018-06-21 14:25:03 +05301273 // parse_decode_flag takes the following values :
1274 // 01 - do parse only
1275 // 10 - do decode only
1276 // 11 - do parse and decode
Hien Ho830b8972019-04-04 15:51:14 -07001277 static const block_visitor_fn_t block_visit[4] = { NULL, parse_decode_block,
1278 decode_block,
1279 parse_decode_block };
Deepa K G3bc1f932018-06-21 14:25:03 +05301280
1281 if (parse_decode_flag & 1) {
1282 const int num_planes = av1_num_planes(cm);
1283 for (int plane = 0; plane < num_planes; ++plane) {
1284 int rcol0, rcol1, rrow0, rrow1;
Rachel Barker2b98f1b2023-07-06 19:39:45 +00001285
1286 // Skip some unnecessary work if loop restoration is disabled
1287 if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) continue;
1288
Deepa K G3bc1f932018-06-21 14:25:03 +05301289 if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
1290 &rcol0, &rcol1, &rrow0, &rrow1)) {
Rachel Barker0483cbe2023-07-05 23:31:38 +00001291 const int rstride = cm->rst_info[plane].horz_units;
Deepa K G3bc1f932018-06-21 14:25:03 +05301292 for (int rrow = rrow0; rrow < rrow1; ++rrow) {
1293 for (int rcol = rcol0; rcol < rcol1; ++rcol) {
1294 const int runit_idx = rcol + rrow * rstride;
Wan-Teh Change232afb2018-09-25 16:44:47 -07001295 loop_restoration_read_sb_coeffs(cm, xd, reader, plane, runit_idx);
Deepa K G3bc1f932018-06-21 14:25:03 +05301296 }
Imdad Sardharwalla7d2e5c92018-01-05 18:41:00 +00001297 }
1298 }
Wan-Teh Changd38c8ef2021-06-17 12:13:09 -07001299 }
Yunqing Wang01b18aa2018-06-25 22:05:26 +00001300
Deepa K G3bc1f932018-06-21 14:25:03 +05301301 partition = (bsize < BLOCK_8X8) ? PARTITION_NONE
Wan-Teh Change232afb2018-09-25 16:44:47 -07001302 : read_partition(xd, mi_row, mi_col, reader,
Deepa K G3bc1f932018-06-21 14:25:03 +05301303 has_rows, has_cols, bsize);
1304 } else {
1305 partition = get_partition(cm, mi_row, mi_col, bsize);
1306 }
Cheng Chen82b4fa12018-05-02 18:43:17 -07001307 subsize = get_partition_subsize(bsize, partition);
kyslov6fdf9342019-04-09 14:53:43 -07001308 if (subsize == BLOCK_INVALID) {
venkat sanampudiee9dbcb2022-02-24 21:34:22 +05301309 // When an internal error occurs ensure that xd->mi_row is set appropriately
1310 // w.r.t. current tile, which is used to signal processing of current row is
1311 // done.
1312 xd->mi_row = mi_row;
kyslov6fdf9342019-04-09 14:53:43 -07001313 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
1314 "Partition is invalid for block size %dx%d",
1315 block_size_wide[bsize], block_size_high[bsize]);
1316 }
Rupert Swarbrick415c8f12017-10-09 16:26:23 +01001317 // Check the bitstream is conformant: if there is subsampling on the
1318 // chroma planes, subsize must subsample to a valid block size.
1319 const struct macroblockd_plane *const pd_u = &xd->plane[1];
Cheng Chen8ab1f442018-04-27 18:01:52 -07001320 if (get_plane_block_size(subsize, pd_u->subsampling_x, pd_u->subsampling_y) ==
1321 BLOCK_INVALID) {
venkat sanampudiee9dbcb2022-02-24 21:34:22 +05301322 // When an internal error occurs ensure that xd->mi_row is set appropriately
1323 // w.r.t. current tile, which is used to signal processing of current row is
1324 // done.
1325 xd->mi_row = mi_row;
Yunqing Wang1f68fee2018-07-02 12:39:09 -07001326 aom_internal_error(xd->error_info, AOM_CODEC_CORRUPT_FRAME,
Rupert Swarbrick415c8f12017-10-09 16:26:23 +01001327 "Block size %dx%d invalid with this subsampling mode",
1328 block_size_wide[subsize], block_size_high[subsize]);
1329 }
1330
Rupert Swarbrick668d3d92017-09-06 16:09:51 +01001331#define DEC_BLOCK_STX_ARG
Rupert Swarbrick668d3d92017-09-06 16:09:51 +01001332#define DEC_BLOCK_EPT_ARG partition,
Wan-Teh Change232afb2018-09-25 16:44:47 -07001333#define DEC_BLOCK(db_r, db_c, db_subsize) \
1334 block_visit[parse_decode_flag](pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), \
1335 reader, DEC_BLOCK_EPT_ARG(db_subsize))
1336#define DEC_PARTITION(db_r, db_c, db_subsize) \
1337 decode_partition(pbi, td, DEC_BLOCK_STX_ARG(db_r), (db_c), reader, \
1338 (db_subsize), parse_decode_flag)
Rupert Swarbrick668d3d92017-09-06 16:09:51 +01001339
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001340 switch (partition) {
1341 case PARTITION_NONE: DEC_BLOCK(mi_row, mi_col, subsize); break;
1342 case PARTITION_HORZ:
1343 DEC_BLOCK(mi_row, mi_col, subsize);
1344 if (has_rows) DEC_BLOCK(mi_row + hbs, mi_col, subsize);
1345 break;
1346 case PARTITION_VERT:
1347 DEC_BLOCK(mi_row, mi_col, subsize);
1348 if (has_cols) DEC_BLOCK(mi_row, mi_col + hbs, subsize);
1349 break;
1350 case PARTITION_SPLIT:
1351 DEC_PARTITION(mi_row, mi_col, subsize);
1352 DEC_PARTITION(mi_row, mi_col + hbs, subsize);
1353 DEC_PARTITION(mi_row + hbs, mi_col, subsize);
1354 DEC_PARTITION(mi_row + hbs, mi_col + hbs, subsize);
1355 break;
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001356 case PARTITION_HORZ_A:
1357 DEC_BLOCK(mi_row, mi_col, bsize2);
1358 DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
1359 DEC_BLOCK(mi_row + hbs, mi_col, subsize);
1360 break;
1361 case PARTITION_HORZ_B:
1362 DEC_BLOCK(mi_row, mi_col, subsize);
1363 DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
1364 DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
1365 break;
1366 case PARTITION_VERT_A:
1367 DEC_BLOCK(mi_row, mi_col, bsize2);
1368 DEC_BLOCK(mi_row + hbs, mi_col, bsize2);
1369 DEC_BLOCK(mi_row, mi_col + hbs, subsize);
1370 break;
1371 case PARTITION_VERT_B:
1372 DEC_BLOCK(mi_row, mi_col, subsize);
1373 DEC_BLOCK(mi_row, mi_col + hbs, bsize2);
1374 DEC_BLOCK(mi_row + hbs, mi_col + hbs, bsize2);
1375 break;
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001376 case PARTITION_HORZ_4:
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01001377 for (int i = 0; i < 4; ++i) {
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001378 int this_mi_row = mi_row + i * quarter_step;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001379 if (i > 0 && this_mi_row >= cm->mi_params.mi_rows) break;
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001380 DEC_BLOCK(this_mi_row, mi_col, subsize);
1381 }
1382 break;
1383 case PARTITION_VERT_4:
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01001384 for (int i = 0; i < 4; ++i) {
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001385 int this_mi_col = mi_col + i * quarter_step;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001386 if (i > 0 && this_mi_col >= cm->mi_params.mi_cols) break;
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001387 DEC_BLOCK(mi_row, this_mi_col, subsize);
1388 }
1389 break;
Debargha Mukherjeeedced252017-10-20 00:02:00 -07001390 default: assert(0 && "Invalid partition type");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001391 }
1392
Rupert Swarbrick668d3d92017-09-06 16:09:51 +01001393#undef DEC_PARTITION
1394#undef DEC_BLOCK
1395#undef DEC_BLOCK_EPT_ARG
1396#undef DEC_BLOCK_STX_ARG
1397
Deepa K G3bc1f932018-06-21 14:25:03 +05301398 if (parse_decode_flag & 1)
1399 update_ext_partition_context(xd, mi_row, mi_col, subsize, bsize, partition);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001400}
1401
Wan-Teh Chang94534432024-08-08 20:29:57 -07001402static inline void setup_bool_decoder(
venkat sanampudiee9dbcb2022-02-24 21:34:22 +05301403 MACROBLOCKD *const xd, const uint8_t *data, const uint8_t *data_end,
1404 const size_t read_size, struct aom_internal_error_info *error_info,
1405 aom_reader *r, uint8_t allow_update_cdf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001406 // Validate the calculated partition length. If the buffer
1407 // described by the partition can't be fully read, then restrict
1408 // it to the portion that can be (for EC mode) or throw an error.
venkat sanampudiee9dbcb2022-02-24 21:34:22 +05301409 if (!read_is_valid(data, read_size, data_end)) {
1410 // When internal error occurs ensure that xd->mi_row is set appropriately
1411 // w.r.t. current tile, which is used to signal processing of current row is
1412 // done in row-mt decoding.
1413 xd->mi_row = xd->tile.mi_row_start;
1414
Yaowu Xuf883b422016-08-30 14:01:10 -07001415 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001416 "Truncated packet or corrupt tile length");
venkat sanampudiee9dbcb2022-02-24 21:34:22 +05301417 }
1418 if (aom_reader_init(r, data, read_size)) {
1419 // When internal error occurs ensure that xd->mi_row is set appropriately
1420 // w.r.t. current tile, which is used to signal processing of current row is
1421 // done in row-mt decoding.
1422 xd->mi_row = xd->tile.mi_row_start;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001423
Yaowu Xuf883b422016-08-30 14:01:10 -07001424 aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001425 "Failed to allocate bool decoder %d", 1);
venkat sanampudiee9dbcb2022-02-24 21:34:22 +05301426 }
Yunqing Wang0e141b52017-11-02 15:08:58 -07001427
1428 r->allow_update_cdf = allow_update_cdf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001429}
Yaowu Xuc27fc142016-08-22 16:08:15 -07001430
Wan-Teh Chang94534432024-08-08 20:29:57 -07001431static inline void setup_segmentation(AV1_COMMON *const cm,
1432 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001433 struct segmentation *const seg = &cm->seg;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001434
1435 seg->update_map = 0;
1436 seg->update_data = 0;
Ryandd8df162017-09-27 15:40:13 -07001437 seg->temporal_update = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001438
Yaowu Xuf883b422016-08-30 14:01:10 -07001439 seg->enabled = aom_rb_read_bit(rb);
Jonathan Matthews98c8fcc2018-01-31 15:54:33 +00001440 if (!seg->enabled) {
Urvang Joshi5209fa52020-10-30 09:58:36 -07001441 if (cm->cur_frame->seg_map) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001442 memset(cm->cur_frame->seg_map, 0,
Urvang Joshi5209fa52020-10-30 09:58:36 -07001443 (cm->cur_frame->mi_rows * cm->cur_frame->mi_cols));
1444 }
David Barker190b77a2018-03-16 14:29:46 +00001445
1446 memset(seg, 0, sizeof(*seg));
1447 segfeatures_copy(&cm->cur_frame->seg, seg);
Jonathan Matthews98c8fcc2018-01-31 15:54:33 +00001448 return;
1449 }
Debargha Mukherjee5521a182018-03-06 12:29:01 -08001450 if (cm->seg.enabled && cm->prev_frame &&
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001451 (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) &&
1452 (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) {
Soo-Chul Han2638cb52017-11-22 16:34:47 -05001453 cm->last_frame_seg_map = cm->prev_frame->seg_map;
Jonathan Matthews74e8a992018-02-01 11:31:08 +00001454 } else {
1455 cm->last_frame_seg_map = NULL;
1456 }
David Barker190b77a2018-03-16 14:29:46 +00001457 // Read update flags
Urvang Joshi6237b882020-03-26 15:02:26 -07001458 if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
David Barker190b77a2018-03-16 14:29:46 +00001459 // These frames can't use previous frames, so must signal map + features
Yaowu Xuc27fc142016-08-22 16:08:15 -07001460 seg->update_map = 1;
David Barker190b77a2018-03-16 14:29:46 +00001461 seg->temporal_update = 0;
1462 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001463 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001464 seg->update_map = aom_rb_read_bit(rb);
David Barker190b77a2018-03-16 14:29:46 +00001465 if (seg->update_map) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001466 seg->temporal_update = aom_rb_read_bit(rb);
David Barker190b77a2018-03-16 14:29:46 +00001467 } else {
1468 seg->temporal_update = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469 }
David Barker190b77a2018-03-16 14:29:46 +00001470 seg->update_data = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471 }
1472
1473 // Segmentation data update
Yaowu Xuc27fc142016-08-22 16:08:15 -07001474 if (seg->update_data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001475 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001476
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01001477 for (int i = 0; i < MAX_SEGMENTS; i++) {
1478 for (int j = 0; j < SEG_LVL_MAX; j++) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001479 int data = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07001480 const int feature_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481 if (feature_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001482 av1_enable_segfeature(seg, i, j);
Sebastien Alaiwanca14b472017-12-11 11:46:00 +01001483
1484 const int data_max = av1_seg_feature_data_max(j);
1485 const int data_min = -data_max;
1486 const int ubits = get_unsigned_bits(data_max);
1487
1488 if (av1_is_segfeature_signed(j)) {
1489 data = aom_rb_read_inv_signed_literal(rb, ubits);
1490 } else {
1491 data = aom_rb_read_literal(rb, ubits);
1492 }
1493
1494 data = clamp(data, data_min, data_max);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001495 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001496 av1_set_segdata(seg, i, j, data);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001497 }
1498 }
Yaowu Xu7e450882019-04-30 15:09:18 -07001499 av1_calculate_segdata(seg);
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00001500 } else if (cm->prev_frame) {
1501 segfeatures_copy(seg, &cm->prev_frame->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001502 }
David Barkercab37552018-03-21 11:56:24 +00001503 segfeatures_copy(&cm->cur_frame->seg, seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001504}
1505
Wan-Teh Chang94534432024-08-08 20:29:57 -07001506static inline void decode_restoration_mode(AV1_COMMON *cm,
1507 struct aom_read_bit_buffer *rb) {
Urvang Joshib6409e92020-03-23 11:23:27 -07001508 assert(!cm->features.all_lossless);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001509 const int num_planes = av1_num_planes(cm);
Urvang Joshib6409e92020-03-23 11:23:27 -07001510 if (cm->features.allow_intrabc) return;
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001511 int all_none = 1, chroma_none = 1;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001512 for (int p = 0; p < num_planes; ++p) {
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001513 RestorationInfo *rsi = &cm->rst_info[p];
Debargha Mukherjeed23ceea2017-05-18 20:33:52 -07001514 if (aom_rb_read_bit(rb)) {
1515 rsi->frame_restoration_type =
1516 aom_rb_read_bit(rb) ? RESTORE_SGRPROJ : RESTORE_WIENER;
1517 } else {
Debargha Mukherjeea3d4fe52017-05-19 16:22:54 -07001518 rsi->frame_restoration_type =
1519 aom_rb_read_bit(rb) ? RESTORE_SWITCHABLE : RESTORE_NONE;
Debargha Mukherjeed23ceea2017-05-18 20:33:52 -07001520 }
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001521 if (rsi->frame_restoration_type != RESTORE_NONE) {
1522 all_none = 0;
1523 chroma_none &= p == 0;
1524 }
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001525 }
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001526 if (!all_none) {
Tarundeep Singh4243e622021-04-20 16:10:22 +05301527 assert(cm->seq_params->sb_size == BLOCK_64X64 ||
1528 cm->seq_params->sb_size == BLOCK_128X128);
1529 const int sb_size = cm->seq_params->sb_size == BLOCK_128X128 ? 128 : 64;
Imdad Sardharwallab1dce0a2018-02-12 16:43:59 +00001530
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001531 for (int p = 0; p < num_planes; ++p)
Imdad Sardharwallab1dce0a2018-02-12 16:43:59 +00001532 cm->rst_info[p].restoration_unit_size = sb_size;
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001533
1534 RestorationInfo *rsi = &cm->rst_info[0];
Imdad Sardharwallab1dce0a2018-02-12 16:43:59 +00001535
1536 if (sb_size == 64) {
1537 rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
1538 }
1539 if (rsi->restoration_unit_size > 64) {
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01001540 rsi->restoration_unit_size <<= aom_rb_read_bit(rb);
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001541 }
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001542 } else {
Urvang Joshi813186b2018-03-08 15:38:46 -08001543 const int size = RESTORATION_UNITSIZE_MAX;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001544 for (int p = 0; p < num_planes; ++p)
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001545 cm->rst_info[p].restoration_unit_size = size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08001546 }
Rupert Swarbrick4596deb2017-11-07 18:06:38 +00001547
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001548 if (num_planes > 1) {
Tarundeep Singh4243e622021-04-20 16:10:22 +05301549 int s =
1550 AOMMIN(cm->seq_params->subsampling_x, cm->seq_params->subsampling_y);
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00001551 if (s && !chroma_none) {
1552 cm->rst_info[1].restoration_unit_size =
1553 cm->rst_info[0].restoration_unit_size >> (aom_rb_read_bit(rb) * s);
1554 } else {
1555 cm->rst_info[1].restoration_unit_size =
1556 cm->rst_info[0].restoration_unit_size;
1557 }
1558 cm->rst_info[2].restoration_unit_size =
1559 cm->rst_info[1].restoration_unit_size;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07001560 }
Debargha Mukherjee5cd2ab92016-09-08 15:15:17 -07001561}
1562
Wan-Teh Chang94534432024-08-08 20:29:57 -07001563static inline void read_wiener_filter(int wiener_win, WienerInfo *wiener_info,
1564 WienerInfo *ref_wiener_info,
1565 aom_reader *rb) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001566 memset(wiener_info->vfilter, 0, sizeof(wiener_info->vfilter));
1567 memset(wiener_info->hfilter, 0, sizeof(wiener_info->hfilter));
1568
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -07001569 if (wiener_win == WIENER_WIN)
1570 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] =
1571 aom_read_primitive_refsubexpfin(
1572 rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1573 WIENER_FILT_TAP0_SUBEXP_K,
1574 ref_wiener_info->vfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1575 WIENER_FILT_TAP0_MINV;
1576 else
1577 wiener_info->vfilter[0] = wiener_info->vfilter[WIENER_WIN - 1] = 0;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001578 wiener_info->vfilter[1] = wiener_info->vfilter[WIENER_WIN - 2] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001579 aom_read_primitive_refsubexpfin(
1580 rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1581 WIENER_FILT_TAP1_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001582 ref_wiener_info->vfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001583 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001584 wiener_info->vfilter[2] = wiener_info->vfilter[WIENER_WIN - 3] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001585 aom_read_primitive_refsubexpfin(
1586 rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1587 WIENER_FILT_TAP2_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001588 ref_wiener_info->vfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001589 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00001590 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001591 wiener_info->vfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00001592 -2 * (wiener_info->vfilter[0] + wiener_info->vfilter[1] +
1593 wiener_info->vfilter[2]);
1594
Debargha Mukherjee1cb757c2017-08-21 02:46:31 -07001595 if (wiener_win == WIENER_WIN)
1596 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] =
1597 aom_read_primitive_refsubexpfin(
1598 rb, WIENER_FILT_TAP0_MAXV - WIENER_FILT_TAP0_MINV + 1,
1599 WIENER_FILT_TAP0_SUBEXP_K,
1600 ref_wiener_info->hfilter[0] - WIENER_FILT_TAP0_MINV, ACCT_STR) +
1601 WIENER_FILT_TAP0_MINV;
1602 else
1603 wiener_info->hfilter[0] = wiener_info->hfilter[WIENER_WIN - 1] = 0;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001604 wiener_info->hfilter[1] = wiener_info->hfilter[WIENER_WIN - 2] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001605 aom_read_primitive_refsubexpfin(
1606 rb, WIENER_FILT_TAP1_MAXV - WIENER_FILT_TAP1_MINV + 1,
1607 WIENER_FILT_TAP1_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001608 ref_wiener_info->hfilter[1] - WIENER_FILT_TAP1_MINV, ACCT_STR) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001609 WIENER_FILT_TAP1_MINV;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001610 wiener_info->hfilter[2] = wiener_info->hfilter[WIENER_WIN - 3] =
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001611 aom_read_primitive_refsubexpfin(
1612 rb, WIENER_FILT_TAP2_MAXV - WIENER_FILT_TAP2_MINV + 1,
1613 WIENER_FILT_TAP2_SUBEXP_K,
Debargha Mukherjeee23d5c32017-04-13 15:33:58 -07001614 ref_wiener_info->hfilter[2] - WIENER_FILT_TAP2_MINV, ACCT_STR) +
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001615 WIENER_FILT_TAP2_MINV;
David Barker1e8e6b92017-01-13 13:45:51 +00001616 // The central element has an implicit +WIENER_FILT_STEP
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001617 wiener_info->hfilter[WIENER_HALFWIN] =
David Barker1e8e6b92017-01-13 13:45:51 +00001618 -2 * (wiener_info->hfilter[0] + wiener_info->hfilter[1] +
1619 wiener_info->hfilter[2]);
Debargha Mukherjeecfc12f32017-04-18 07:03:32 -07001620 memcpy(ref_wiener_info, wiener_info, sizeof(*wiener_info));
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001621}
1622
Wan-Teh Chang94534432024-08-08 20:29:57 -07001623static inline void read_sgrproj_filter(SgrprojInfo *sgrproj_info,
1624 SgrprojInfo *ref_sgrproj_info,
1625 aom_reader *rb) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +00001626 sgrproj_info->ep = aom_read_literal(rb, SGRPROJ_PARAMS_BITS, ACCT_STR);
Yaowu Xubf732b82019-04-30 15:21:52 -07001627 const sgr_params_type *params = &av1_sgr_params[sgrproj_info->ep];
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +00001628
Urvang Joshi3715b882018-05-14 20:05:25 -04001629 if (params->r[0] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +00001630 sgrproj_info->xqd[0] = 0;
1631 sgrproj_info->xqd[1] =
1632 aom_read_primitive_refsubexpfin(
1633 rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1634 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1635 SGRPROJ_PRJ_MIN1;
Urvang Joshi3715b882018-05-14 20:05:25 -04001636 } else if (params->r[1] == 0) {
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +00001637 sgrproj_info->xqd[0] =
1638 aom_read_primitive_refsubexpfin(
1639 rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1640 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1641 SGRPROJ_PRJ_MIN0;
Debargha Mukherjee114575d2018-02-23 11:18:37 -08001642 sgrproj_info->xqd[1] = clamp((1 << SGRPROJ_PRJ_BITS) - sgrproj_info->xqd[0],
1643 SGRPROJ_PRJ_MIN1, SGRPROJ_PRJ_MAX1);
Imdad Sardharwallafdeb1162018-02-21 17:38:20 +00001644 } else {
1645 sgrproj_info->xqd[0] =
1646 aom_read_primitive_refsubexpfin(
1647 rb, SGRPROJ_PRJ_MAX0 - SGRPROJ_PRJ_MIN0 + 1, SGRPROJ_PRJ_SUBEXP_K,
1648 ref_sgrproj_info->xqd[0] - SGRPROJ_PRJ_MIN0, ACCT_STR) +
1649 SGRPROJ_PRJ_MIN0;
1650 sgrproj_info->xqd[1] =
1651 aom_read_primitive_refsubexpfin(
1652 rb, SGRPROJ_PRJ_MAX1 - SGRPROJ_PRJ_MIN1 + 1, SGRPROJ_PRJ_SUBEXP_K,
1653 ref_sgrproj_info->xqd[1] - SGRPROJ_PRJ_MIN1, ACCT_STR) +
1654 SGRPROJ_PRJ_MIN1;
1655 }
1656
1657 memcpy(ref_sgrproj_info, sgrproj_info, sizeof(*sgrproj_info));
1658}
Debargha Mukherjee8f209a82016-10-12 10:47:01 -07001659
Wan-Teh Chang94534432024-08-08 20:29:57 -07001660static inline void loop_restoration_read_sb_coeffs(const AV1_COMMON *const cm,
1661 MACROBLOCKD *xd,
1662 aom_reader *const r,
1663 int plane, int runit_idx) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001664 const RestorationInfo *rsi = &cm->rst_info[plane];
Urvang Joshi813186b2018-03-08 15:38:46 -08001665 RestorationUnitInfo *rui = &rsi->unit_info[runit_idx];
Elliott Karpilovsky3c5e16f2020-11-05 18:43:18 -08001666 assert(rsi->frame_restoration_type != RESTORE_NONE);
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001667
Urvang Joshib6409e92020-03-23 11:23:27 -07001668 assert(!cm->features.all_lossless);
Urvang Joshi5ec7b812018-02-28 14:37:06 -08001669
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001670 const int wiener_win = (plane > 0) ? WIENER_WIN_CHROMA : WIENER_WIN;
1671 WienerInfo *wiener_info = xd->wiener_info + plane;
1672 SgrprojInfo *sgrproj_info = xd->sgrproj_info + plane;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001673
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001674 if (rsi->frame_restoration_type == RESTORE_SWITCHABLE) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001675 rui->restoration_type =
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -07001676 aom_read_symbol(r, xd->tile_ctx->switchable_restore_cdf,
1677 RESTORE_SWITCHABLE_TYPES, ACCT_STR);
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001678 switch (rui->restoration_type) {
1679 case RESTORE_WIENER:
1680 read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
1681 break;
1682 case RESTORE_SGRPROJ:
1683 read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
1684 break;
1685 default: assert(rui->restoration_type == RESTORE_NONE); break;
Rupert Swarbrick09b5b162017-08-31 16:32:29 +01001686 }
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001687 } else if (rsi->frame_restoration_type == RESTORE_WIENER) {
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -07001688 if (aom_read_symbol(r, xd->tile_ctx->wiener_restore_cdf, 2, ACCT_STR)) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001689 rui->restoration_type = RESTORE_WIENER;
1690 read_wiener_filter(wiener_win, &rui->wiener_info, wiener_info, r);
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001691 } else {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001692 rui->restoration_type = RESTORE_NONE;
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001693 }
1694 } else if (rsi->frame_restoration_type == RESTORE_SGRPROJ) {
Debargha Mukherjeebc732ef2017-10-12 12:40:25 -07001695 if (aom_read_symbol(r, xd->tile_ctx->sgrproj_restore_cdf, 2, ACCT_STR)) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001696 rui->restoration_type = RESTORE_SGRPROJ;
1697 read_sgrproj_filter(&rui->sgrproj_info, sgrproj_info, r);
Rupert Swarbrick6c545212017-09-01 17:17:25 +01001698 } else {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01001699 rui->restoration_type = RESTORE_NONE;
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -08001700 }
1701 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001702}
Yaowu Xuc27fc142016-08-22 16:08:15 -07001703
Wan-Teh Chang94534432024-08-08 20:29:57 -07001704static inline void setup_loopfilter(AV1_COMMON *cm,
1705 struct aom_read_bit_buffer *rb) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001706 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001707 struct loopfilter *lf = &cm->lf;
Urvang Joshi6237b882020-03-26 15:02:26 -07001708
Urvang Joshib6409e92020-03-23 11:23:27 -07001709 if (cm->features.allow_intrabc || cm->features.coded_lossless) {
Cheng Chen7d089ae2018-03-15 19:11:03 -07001710 // write default deltas to frame buffer
Cheng Chen7d089ae2018-03-15 19:11:03 -07001711 av1_set_default_ref_deltas(cm->cur_frame->ref_deltas);
1712 av1_set_default_mode_deltas(cm->cur_frame->mode_deltas);
Cheng Chen7d089ae2018-03-15 19:11:03 -07001713 return;
1714 }
Urvang Joshib6409e92020-03-23 11:23:27 -07001715 assert(!cm->features.coded_lossless);
Cheng Chen7d089ae2018-03-15 19:11:03 -07001716 if (cm->prev_frame) {
1717 // write deltas to frame buffer
Zoe Liu27deb382018-03-27 15:13:56 -07001718 memcpy(lf->ref_deltas, cm->prev_frame->ref_deltas, REF_FRAMES);
Cheng Chen7d089ae2018-03-15 19:11:03 -07001719 memcpy(lf->mode_deltas, cm->prev_frame->mode_deltas, MAX_MODE_LF_DELTAS);
Debargha Mukherjee5d949692018-03-20 14:51:47 -07001720 } else {
1721 av1_set_default_ref_deltas(lf->ref_deltas);
1722 av1_set_default_mode_deltas(lf->mode_deltas);
Cheng Chen7d089ae2018-03-15 19:11:03 -07001723 }
Cheng Chen76224b02017-12-15 12:21:01 -08001724 lf->filter_level[0] = aom_rb_read_literal(rb, 6);
1725 lf->filter_level[1] = aom_rb_read_literal(rb, 6);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001726 if (num_planes > 1) {
Cheng Chen76224b02017-12-15 12:21:01 -08001727 if (lf->filter_level[0] || lf->filter_level[1]) {
1728 lf->filter_level_u = aom_rb_read_literal(rb, 6);
1729 lf->filter_level_v = aom_rb_read_literal(rb, 6);
Cheng Chen765e34e2017-12-11 11:43:35 -08001730 }
Cheng Chene94df5c2017-07-19 17:25:33 -07001731 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001732 lf->sharpness_level = aom_rb_read_literal(rb, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001733
1734 // Read in loop filter deltas applied at the MB level based on mode or ref
1735 // frame.
1736 lf->mode_ref_delta_update = 0;
1737
Yaowu Xuf883b422016-08-30 14:01:10 -07001738 lf->mode_ref_delta_enabled = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001739 if (lf->mode_ref_delta_enabled) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001740 lf->mode_ref_delta_update = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001741 if (lf->mode_ref_delta_update) {
Zoe Liu27deb382018-03-27 15:13:56 -07001742 for (int i = 0; i < REF_FRAMES; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07001743 if (aom_rb_read_bit(rb))
1744 lf->ref_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001745
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01001746 for (int i = 0; i < MAX_MODE_LF_DELTAS; i++)
Yaowu Xuf883b422016-08-30 14:01:10 -07001747 if (aom_rb_read_bit(rb))
1748 lf->mode_deltas[i] = aom_rb_read_inv_signed_literal(rb, 6);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001749 }
1750 }
Cheng Chen7d089ae2018-03-15 19:11:03 -07001751
1752 // write deltas to frame buffer
Zoe Liu27deb382018-03-27 15:13:56 -07001753 memcpy(cm->cur_frame->ref_deltas, lf->ref_deltas, REF_FRAMES);
Cheng Chen7d089ae2018-03-15 19:11:03 -07001754 memcpy(cm->cur_frame->mode_deltas, lf->mode_deltas, MAX_MODE_LF_DELTAS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001755}
1756
Wan-Teh Chang94534432024-08-08 20:29:57 -07001757static inline void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001758 const int num_planes = av1_num_planes(cm);
David Turnerebf96f42018-11-14 16:57:57 +00001759 CdefInfo *const cdef_info = &cm->cdef_info;
1760
Urvang Joshib6409e92020-03-23 11:23:27 -07001761 if (cm->features.allow_intrabc) return;
Hui Su584ba482019-06-19 11:48:05 -07001762 cdef_info->cdef_damping = aom_rb_read_literal(rb, 2) + 3;
David Turnerebf96f42018-11-14 16:57:57 +00001763 cdef_info->cdef_bits = aom_rb_read_literal(rb, 2);
1764 cdef_info->nb_cdef_strengths = 1 << cdef_info->cdef_bits;
1765 for (int i = 0; i < cdef_info->nb_cdef_strengths; i++) {
1766 cdef_info->cdef_strengths[i] = aom_rb_read_literal(rb, CDEF_STRENGTH_BITS);
1767 cdef_info->cdef_uv_strengths[i] =
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001768 num_planes > 1 ? aom_rb_read_literal(rb, CDEF_STRENGTH_BITS) : 0;
Jean-Marc Valin5f5c1322017-03-21 16:20:21 -04001769 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001770}
Yaowu Xuc27fc142016-08-22 16:08:15 -07001771
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07001772static inline int read_delta_q(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001773 return aom_rb_read_bit(rb) ? aom_rb_read_inv_signed_literal(rb, 6) : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001774}
1775
Wan-Teh Chang94534432024-08-08 20:29:57 -07001776static inline void setup_quantization(CommonQuantParams *quant_params,
1777 int num_planes, bool separate_uv_delta_q,
1778 struct aom_read_bit_buffer *rb) {
Urvang Joshi17814622020-03-27 17:26:17 -07001779 quant_params->base_qindex = aom_rb_read_literal(rb, QINDEX_BITS);
1780 quant_params->y_dc_delta_q = read_delta_q(rb);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00001781 if (num_planes > 1) {
Debargha Mukherjee18f4fb22017-12-14 14:26:27 -08001782 int diff_uv_delta = 0;
Urvang Joshi17814622020-03-27 17:26:17 -07001783 if (separate_uv_delta_q) diff_uv_delta = aom_rb_read_bit(rb);
1784 quant_params->u_dc_delta_q = read_delta_q(rb);
1785 quant_params->u_ac_delta_q = read_delta_q(rb);
Debargha Mukherjee18f4fb22017-12-14 14:26:27 -08001786 if (diff_uv_delta) {
Urvang Joshi17814622020-03-27 17:26:17 -07001787 quant_params->v_dc_delta_q = read_delta_q(rb);
1788 quant_params->v_ac_delta_q = read_delta_q(rb);
Debargha Mukherjee18f4fb22017-12-14 14:26:27 -08001789 } else {
Urvang Joshi17814622020-03-27 17:26:17 -07001790 quant_params->v_dc_delta_q = quant_params->u_dc_delta_q;
1791 quant_params->v_ac_delta_q = quant_params->u_ac_delta_q;
Debargha Mukherjee18f4fb22017-12-14 14:26:27 -08001792 }
Jonathan Matthews292bfe22018-07-26 14:00:53 +01001793 } else {
Urvang Joshi17814622020-03-27 17:26:17 -07001794 quant_params->u_dc_delta_q = 0;
1795 quant_params->u_ac_delta_q = 0;
1796 quant_params->v_dc_delta_q = 0;
1797 quant_params->v_ac_delta_q = 0;
Yaowu Xu6fc47e52017-12-04 15:07:48 -08001798 }
Urvang Joshi17814622020-03-27 17:26:17 -07001799 quant_params->using_qmatrix = aom_rb_read_bit(rb);
1800 if (quant_params->using_qmatrix) {
Urvang Joshi7fdbd052020-03-30 17:50:22 -07001801 quant_params->qmatrix_level_y = aom_rb_read_literal(rb, QM_LEVEL_BITS);
1802 quant_params->qmatrix_level_u = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Urvang Joshi17814622020-03-27 17:26:17 -07001803 if (!separate_uv_delta_q)
Urvang Joshi7fdbd052020-03-30 17:50:22 -07001804 quant_params->qmatrix_level_v = quant_params->qmatrix_level_u;
Yaowu Xuf7a12422018-01-31 15:29:20 -08001805 else
Urvang Joshi7fdbd052020-03-30 17:50:22 -07001806 quant_params->qmatrix_level_v = aom_rb_read_literal(rb, QM_LEVEL_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001807 } else {
Urvang Joshi7fdbd052020-03-30 17:50:22 -07001808 quant_params->qmatrix_level_y = 0;
1809 quant_params->qmatrix_level_u = 0;
1810 quant_params->qmatrix_level_v = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001811 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001812}
1813
James Zernf3cf0302024-09-11 13:43:35 -07001814// Get global dequant matrix.
1815static const qm_val_t *get_iqmatrix(const CommonQuantParams *quant_params,
1816 int qmlevel, int plane, TX_SIZE tx_size) {
1817 assert(quant_params->giqmatrix[qmlevel][plane][tx_size] != NULL ||
1818 qmlevel == NUM_QM_LEVELS - 1);
1819 return quant_params->giqmatrix[qmlevel][plane][tx_size];
1820}
1821
Alex Converse05a3e7d2017-05-16 12:20:07 -07001822// Build y/uv dequant values based on segmentation.
Wan-Teh Chang94534432024-08-08 20:29:57 -07001823static inline void setup_segmentation_dequant(AV1_COMMON *const cm,
1824 MACROBLOCKD *const xd) {
Tarundeep Singh4243e622021-04-20 16:10:22 +05301825 const int bit_depth = cm->seq_params->bit_depth;
Alex Converse05a3e7d2017-05-16 12:20:07 -07001826 // When segmentation is disabled, only the first value is used. The
1827 // remaining are don't cares.
1828 const int max_segments = cm->seg.enabled ? MAX_SEGMENTS : 1;
Urvang Joshi17814622020-03-27 17:26:17 -07001829 CommonQuantParams *const quant_params = &cm->quant_params;
Alex Converse05a3e7d2017-05-16 12:20:07 -07001830 for (int i = 0; i < max_segments; ++i) {
Wan-Teh Chang98c01e02018-10-18 09:48:27 -07001831 const int qindex = xd->qindex[i];
Urvang Joshi17814622020-03-27 17:26:17 -07001832 quant_params->y_dequant_QTX[i][0] =
1833 av1_dc_quant_QTX(qindex, quant_params->y_dc_delta_q, bit_depth);
1834 quant_params->y_dequant_QTX[i][1] = av1_ac_quant_QTX(qindex, 0, bit_depth);
1835 quant_params->u_dequant_QTX[i][0] =
1836 av1_dc_quant_QTX(qindex, quant_params->u_dc_delta_q, bit_depth);
1837 quant_params->u_dequant_QTX[i][1] =
1838 av1_ac_quant_QTX(qindex, quant_params->u_ac_delta_q, bit_depth);
1839 quant_params->v_dequant_QTX[i][0] =
1840 av1_dc_quant_QTX(qindex, quant_params->v_dc_delta_q, bit_depth);
1841 quant_params->v_dequant_QTX[i][1] =
1842 av1_ac_quant_QTX(qindex, quant_params->v_ac_delta_q, bit_depth);
1843 const int use_qmatrix = av1_use_qmatrix(quant_params, xd, i);
Yaowu Xu3b546332018-03-26 12:53:46 -07001844 // NB: depends on base index so there is only 1 set per frame
1845 // No quant weighting when lossless or signalled not using QM
Urvang Joshi7fdbd052020-03-30 17:50:22 -07001846 const int qmlevel_y =
1847 use_qmatrix ? quant_params->qmatrix_level_y : NUM_QM_LEVELS - 1;
Thomas Davies6675adf2017-05-04 17:39:21 +01001848 for (int j = 0; j < TX_SIZES_ALL; ++j) {
Urvang Joshi17814622020-03-27 17:26:17 -07001849 quant_params->y_iqmatrix[i][j] =
James Zernf3cf0302024-09-11 13:43:35 -07001850 get_iqmatrix(quant_params, qmlevel_y, AOM_PLANE_Y, j);
Yaowu Xuf7a12422018-01-31 15:29:20 -08001851 }
Urvang Joshi7fdbd052020-03-30 17:50:22 -07001852 const int qmlevel_u =
1853 use_qmatrix ? quant_params->qmatrix_level_u : NUM_QM_LEVELS - 1;
Yaowu Xuf7a12422018-01-31 15:29:20 -08001854 for (int j = 0; j < TX_SIZES_ALL; ++j) {
Urvang Joshi17814622020-03-27 17:26:17 -07001855 quant_params->u_iqmatrix[i][j] =
James Zernf3cf0302024-09-11 13:43:35 -07001856 get_iqmatrix(quant_params, qmlevel_u, AOM_PLANE_U, j);
Yaowu Xuf7a12422018-01-31 15:29:20 -08001857 }
Urvang Joshi7fdbd052020-03-30 17:50:22 -07001858 const int qmlevel_v =
1859 use_qmatrix ? quant_params->qmatrix_level_v : NUM_QM_LEVELS - 1;
Yaowu Xuf7a12422018-01-31 15:29:20 -08001860 for (int j = 0; j < TX_SIZES_ALL; ++j) {
Urvang Joshi17814622020-03-27 17:26:17 -07001861 quant_params->v_iqmatrix[i][j] =
James Zernf3cf0302024-09-11 13:43:35 -07001862 get_iqmatrix(quant_params, qmlevel_v, AOM_PLANE_V, j);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001863 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001864 }
1865}
1866
Angie Chiang5678ad92016-11-21 09:38:40 -08001867static InterpFilter read_frame_interp_filter(struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001868 return aom_rb_read_bit(rb) ? SWITCHABLE
Angie Chiang6305abe2016-10-24 12:24:44 -07001869 : aom_rb_read_literal(rb, LOG_SWITCHABLE_FILTERS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001870}
1871
Wan-Teh Chang94534432024-08-08 20:29:57 -07001872static inline void setup_render_size(AV1_COMMON *cm,
1873 struct aom_read_bit_buffer *rb) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001874 cm->render_width = cm->superres_upscaled_width;
1875 cm->render_height = cm->superres_upscaled_height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001876 if (aom_rb_read_bit(rb))
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01001877 av1_read_frame_size(rb, 16, 16, &cm->render_width, &cm->render_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001878}
1879
Fergus Simpsone7508412017-03-14 18:14:09 -07001880// TODO(afergs): make "struct aom_read_bit_buffer *const rb"?
Wan-Teh Chang94534432024-08-08 20:29:57 -07001881static inline void setup_superres(AV1_COMMON *const cm,
1882 struct aom_read_bit_buffer *rb, int *width,
1883 int *height) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07001884 cm->superres_upscaled_width = *width;
1885 cm->superres_upscaled_height = *height;
Urvang Joshi2c92b072018-03-19 17:23:31 -07001886
Tarundeep Singh4243e622021-04-20 16:10:22 +05301887 const SequenceHeader *const seq_params = cm->seq_params;
Urvang Joshi2c92b072018-03-19 17:23:31 -07001888 if (!seq_params->enable_superres) return;
1889
Fergus Simpsone7508412017-03-14 18:14:09 -07001890 if (aom_rb_read_bit(rb)) {
Urvang Joshide71d142017-10-05 12:12:15 -07001891 cm->superres_scale_denominator =
Fergus Simpsone7508412017-03-14 18:14:09 -07001892 (uint8_t)aom_rb_read_literal(rb, SUPERRES_SCALE_BITS);
Urvang Joshide71d142017-10-05 12:12:15 -07001893 cm->superres_scale_denominator += SUPERRES_SCALE_DENOMINATOR_MIN;
Fergus Simpson7b2d1442017-05-22 17:18:33 -07001894 // Don't edit cm->width or cm->height directly, or the buffers won't get
1895 // resized correctly
Urvang Joshi69fde2e2017-10-09 15:34:18 -07001896 av1_calculate_scaled_superres_size(width, height,
1897 cm->superres_scale_denominator);
Fergus Simpsone7508412017-03-14 18:14:09 -07001898 } else {
1899 // 1:1 scaling - ie. no scaling, scale not provided
Urvang Joshide71d142017-10-05 12:12:15 -07001900 cm->superres_scale_denominator = SCALE_NUMERATOR;
Fergus Simpsone7508412017-03-14 18:14:09 -07001901 }
1902}
Soo-Chul Han2638cb52017-11-22 16:34:47 -05001903
Wan-Teh Chang94534432024-08-08 20:29:57 -07001904static inline void resize_context_buffers(AV1_COMMON *cm, int width,
1905 int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001906#if CONFIG_SIZE_LIMIT
1907 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05301908 aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001909 "Dimensions of %dx%d beyond allowed size of %dx%d.",
1910 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1911#endif
1912 if (cm->width != width || cm->height != height) {
Mudassir Galagnath77f31602022-04-19 16:28:25 +05301913 const int new_mi_rows = CEIL_POWER_OF_TWO(height, MI_SIZE_LOG2);
1914 const int new_mi_cols = CEIL_POWER_OF_TWO(width, MI_SIZE_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001915
Yaowu Xuf883b422016-08-30 14:01:10 -07001916 // Allocations in av1_alloc_context_buffers() depend on individual
Yaowu Xuc27fc142016-08-22 16:08:15 -07001917 // dimensions as well as the overall size.
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001918 if (new_mi_cols > cm->mi_params.mi_cols ||
1919 new_mi_rows > cm->mi_params.mi_rows) {
Jayasanker Jfb666962022-03-29 18:56:42 +05301920 if (av1_alloc_context_buffers(cm, width, height, BLOCK_4X4)) {
Wan-Teh Changfb0dd0b2018-07-11 15:03:14 -07001921 // The cm->mi_* values have been cleared and any existing context
1922 // buffers have been freed. Clear cm->width and cm->height to be
1923 // consistent and to force a realloc next time.
1924 cm->width = 0;
1925 cm->height = 0;
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05301926 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001927 "Failed to allocate context buffers");
Wan-Teh Changfb0dd0b2018-07-11 15:03:14 -07001928 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001929 } else {
Jayasanker Jfb666962022-03-29 18:56:42 +05301930 cm->mi_params.set_mb_mi(&cm->mi_params, width, height, BLOCK_4X4);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001931 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07001932 av1_init_mi_buffers(&cm->mi_params);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001933 cm->width = width;
1934 cm->height = height;
1935 }
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01001936
1937 ensure_mv_buffer(cm->cur_frame, cm);
1938 cm->cur_frame->width = cm->width;
1939 cm->cur_frame->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001940}
1941
Wan-Teh Chang94534432024-08-08 20:29:57 -07001942static inline void setup_buffer_pool(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001943 BufferPool *const pool = cm->buffer_pool;
Tarundeep Singh4243e622021-04-20 16:10:22 +05301944 const SequenceHeader *const seq_params = cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001945
1946 lock_buffer_pool(pool);
Yaowu Xuf883b422016-08-30 14:01:10 -07001947 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00001948 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
1949 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07001950 AOM_DEC_BORDER_IN_PIXELS, cm->features.byte_alignment,
Rachel Barker72e349e2024-02-13 14:31:10 +00001951 &cm->cur_frame->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv,
1952 false, 0)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001953 unlock_buffer_pool(pool);
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05301954 aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001955 "Failed to allocate frame buffer");
1956 }
1957 unlock_buffer_pool(pool);
1958
Jack Haughtonddb80602018-11-21 16:41:49 +00001959 cm->cur_frame->buf.bit_depth = (unsigned int)seq_params->bit_depth;
1960 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
1961 cm->cur_frame->buf.transfer_characteristics =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001962 seq_params->transfer_characteristics;
Jack Haughtonddb80602018-11-21 16:41:49 +00001963 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
1964 cm->cur_frame->buf.monochrome = seq_params->monochrome;
1965 cm->cur_frame->buf.chroma_sample_position =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001966 seq_params->chroma_sample_position;
Jack Haughtonddb80602018-11-21 16:41:49 +00001967 cm->cur_frame->buf.color_range = seq_params->color_range;
1968 cm->cur_frame->buf.render_width = cm->render_width;
1969 cm->cur_frame->buf.render_height = cm->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001970}
1971
Wan-Teh Chang94534432024-08-08 20:29:57 -07001972static inline void setup_frame_size(AV1_COMMON *cm,
1973 int frame_size_override_flag,
1974 struct aom_read_bit_buffer *rb) {
Tarundeep Singh4243e622021-04-20 16:10:22 +05301975 const SequenceHeader *const seq_params = cm->seq_params;
Urvang Joshic9497b22018-08-03 12:40:02 -07001976 int width, height;
1977
1978 if (frame_size_override_flag) {
1979 int num_bits_width = seq_params->num_bits_width;
1980 int num_bits_height = seq_params->num_bits_height;
1981 av1_read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
1982 if (width > seq_params->max_frame_width ||
1983 height > seq_params->max_frame_height) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05301984 aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
Urvang Joshic9497b22018-08-03 12:40:02 -07001985 "Frame dimensions are larger than the maximum values");
1986 }
1987 } else {
1988 width = seq_params->max_frame_width;
1989 height = seq_params->max_frame_height;
1990 }
1991
1992 setup_superres(cm, rb, &width, &height);
1993 resize_context_buffers(cm, width, height);
1994 setup_render_size(cm, rb);
1995 setup_buffer_pool(cm);
1996}
1997
Wan-Teh Chang94534432024-08-08 20:29:57 -07001998static inline void setup_sb_size(SequenceHeader *seq_params,
1999 struct aom_read_bit_buffer *rb) {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00002000 set_sb_size(seq_params, aom_rb_read_bit(rb) ? BLOCK_128X128 : BLOCK_64X64);
Debargha Mukherjeed2630fa2017-09-22 10:32:51 -07002001}
2002
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07002003static inline int valid_ref_frame_img_fmt(aom_bit_depth_t ref_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002004 int ref_xss, int ref_yss,
Yaowu Xuf883b422016-08-30 14:01:10 -07002005 aom_bit_depth_t this_bit_depth,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002006 int this_xss, int this_yss) {
2007 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
2008 ref_yss == this_yss;
2009}
2010
Wan-Teh Chang94534432024-08-08 20:29:57 -07002011static inline void setup_frame_size_with_refs(AV1_COMMON *cm,
2012 struct aom_read_bit_buffer *rb) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002013 int width, height;
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002014 int found = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002015 int has_valid_ref_frame = 0;
David Turnera21966b2018-12-05 14:48:49 +00002016 for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002017 if (aom_rb_read_bit(rb)) {
David Turnera21966b2018-12-05 14:48:49 +00002018 const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
2019 // This will never be NULL in a normal stream, as streams are required to
2020 // have a shown keyframe before any inter frames, which would refresh all
2021 // the reference buffers. However, it might be null if we're starting in
2022 // the middle of a stream, and static analysis will error if we don't do
2023 // a null check here.
2024 if (ref_buf == NULL) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302025 aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
David Turnera21966b2018-12-05 14:48:49 +00002026 "Invalid condition: invalid reference buffer");
2027 } else {
2028 const YV12_BUFFER_CONFIG *const buf = &ref_buf->buf;
2029 width = buf->y_crop_width;
2030 height = buf->y_crop_height;
2031 cm->render_width = buf->render_width;
2032 cm->render_height = buf->render_height;
2033 setup_superres(cm, rb, &width, &height);
2034 resize_context_buffers(cm, width, height);
2035 found = 1;
2036 break;
2037 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002038 }
2039 }
2040
Tarundeep Singh4243e622021-04-20 16:10:22 +05302041 const SequenceHeader *const seq_params = cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002042 if (!found) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002043 int num_bits_width = seq_params->num_bits_width;
2044 int num_bits_height = seq_params->num_bits_height;
Tom Finegan8ab2bba2018-02-28 07:36:28 -08002045
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01002046 av1_read_frame_size(rb, num_bits_width, num_bits_height, &width, &height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002047 setup_superres(cm, rb, &width, &height);
David Barker6e5ade92017-11-17 13:56:38 +00002048 resize_context_buffers(cm, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002049 setup_render_size(cm, rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002050 }
2051
2052 if (width <= 0 || height <= 0)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302053 aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002054 "Invalid frame size");
2055
2056 // Check to make sure at least one of frames that this frame references
2057 // has valid dimensions.
David Turnera21966b2018-12-05 14:48:49 +00002058 for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2059 const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002060 has_valid_ref_frame |=
David Turnera21966b2018-12-05 14:48:49 +00002061 valid_ref_frame_size(ref_frame->buf.y_crop_width,
2062 ref_frame->buf.y_crop_height, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002063 }
2064 if (!has_valid_ref_frame)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302065 aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002066 "Referenced frame has invalid size");
David Turnera21966b2018-12-05 14:48:49 +00002067 for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2068 const RefCntBuffer *const ref_frame = get_ref_frame_buf(cm, i);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002069 if (!valid_ref_frame_img_fmt(
David Turnera21966b2018-12-05 14:48:49 +00002070 ref_frame->buf.bit_depth, ref_frame->buf.subsampling_x,
2071 ref_frame->buf.subsampling_y, seq_params->bit_depth,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002072 seq_params->subsampling_x, seq_params->subsampling_y))
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302073 aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002074 "Referenced frame has incompatible color format");
2075 }
Urvang Joshic9497b22018-08-03 12:40:02 -07002076 setup_buffer_pool(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002077}
2078
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002079// Same function as av1_read_uniform but reading from uncompresses header wb
2080static int rb_read_uniform(struct aom_read_bit_buffer *const rb, int n) {
2081 const int l = get_unsigned_bits(n);
2082 const int m = (1 << l) - n;
2083 const int v = aom_rb_read_literal(rb, l - 1);
2084 assert(l != 0);
2085 if (v < m)
2086 return v;
2087 else
Wan-Teh Changb6680ff2018-07-27 14:50:25 -07002088 return (v << 1) - m + aom_rb_read_bit(rb);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002089}
2090
Wan-Teh Chang94534432024-08-08 20:29:57 -07002091static inline void read_tile_info_max_tile(
Elliott Karpilovsky18fcd6a2019-09-16 15:15:06 -07002092 AV1_COMMON *const cm, struct aom_read_bit_buffer *const rb) {
Tarundeep Singh4243e622021-04-20 16:10:22 +05302093 const SequenceHeader *const seq_params = cm->seq_params;
Urvang Joshi54ffae72020-03-23 13:37:10 -07002094 CommonTileParams *const tiles = &cm->tiles;
Mudassir Galagnath77f31602022-04-19 16:28:25 +05302095 int width_sb =
2096 CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, seq_params->mib_size_log2);
2097 int height_sb =
2098 CEIL_POWER_OF_TWO(cm->mi_params.mi_rows, seq_params->mib_size_log2);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002099
2100 av1_get_tile_limits(cm);
Urvang Joshi54ffae72020-03-23 13:37:10 -07002101 tiles->uniform_spacing = aom_rb_read_bit(rb);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002102
2103 // Read tile columns
Urvang Joshi54ffae72020-03-23 13:37:10 -07002104 if (tiles->uniform_spacing) {
2105 tiles->log2_cols = tiles->min_log2_cols;
2106 while (tiles->log2_cols < tiles->max_log2_cols) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002107 if (!aom_rb_read_bit(rb)) {
2108 break;
2109 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07002110 tiles->log2_cols++;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002111 }
2112 } else {
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002113 int i;
2114 int start_sb;
Dominic Symesf58f1112017-09-25 12:47:40 +02002115 for (i = 0, start_sb = 0; width_sb > 0 && i < MAX_TILE_COLS; i++) {
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002116 const int size_sb =
Urvang Joshi54ffae72020-03-23 13:37:10 -07002117 1 + rb_read_uniform(rb, AOMMIN(width_sb, tiles->max_width_sb));
2118 tiles->col_start_sb[i] = start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002119 start_sb += size_sb;
2120 width_sb -= size_sb;
2121 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07002122 tiles->cols = i;
2123 tiles->col_start_sb[i] = start_sb + width_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002124 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002125 av1_calculate_tile_cols(seq_params, cm->mi_params.mi_rows,
2126 cm->mi_params.mi_cols, tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002127
2128 // Read tile rows
Urvang Joshi54ffae72020-03-23 13:37:10 -07002129 if (tiles->uniform_spacing) {
2130 tiles->log2_rows = tiles->min_log2_rows;
2131 while (tiles->log2_rows < tiles->max_log2_rows) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002132 if (!aom_rb_read_bit(rb)) {
2133 break;
2134 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07002135 tiles->log2_rows++;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002136 }
2137 } else {
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002138 int i;
2139 int start_sb;
Dominic Symesf58f1112017-09-25 12:47:40 +02002140 for (i = 0, start_sb = 0; height_sb > 0 && i < MAX_TILE_ROWS; i++) {
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002141 const int size_sb =
Urvang Joshi54ffae72020-03-23 13:37:10 -07002142 1 + rb_read_uniform(rb, AOMMIN(height_sb, tiles->max_height_sb));
2143 tiles->row_start_sb[i] = start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002144 start_sb += size_sb;
2145 height_sb -= size_sb;
2146 }
Urvang Joshi54ffae72020-03-23 13:37:10 -07002147 tiles->rows = i;
2148 tiles->row_start_sb[i] = start_sb + height_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002149 }
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002150 av1_calculate_tile_rows(seq_params, cm->mi_params.mi_rows, tiles);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002151}
Dominic Symesdb5d66f2017-08-18 18:11:34 +02002152
Yunqing Wang2f7b7a92018-06-11 10:07:23 -07002153void av1_set_single_tile_decoding_mode(AV1_COMMON *const cm) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07002154 cm->tiles.single_tile_decoding = 0;
2155 if (cm->tiles.large_scale) {
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002156 struct loopfilter *lf = &cm->lf;
David Turnerebf96f42018-11-14 16:57:57 +00002157 RestorationInfo *const rst_info = cm->rst_info;
2158 const CdefInfo *const cdef_info = &cm->cdef_info;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002159
Debargha Mukherjee2382b142018-02-26 14:31:32 -08002160 // Figure out single_tile_decoding by loopfilter_level.
Rupert Swarbrick566155f2017-10-27 11:59:17 +01002161 const int no_loopfilter = !(lf->filter_level[0] || lf->filter_level[1]);
David Turnerebf96f42018-11-14 16:57:57 +00002162 const int no_cdef = cdef_info->cdef_bits == 0 &&
2163 cdef_info->cdef_strengths[0] == 0 &&
2164 cdef_info->cdef_uv_strengths[0] == 0;
Yaowu Xu35ee2342017-11-08 11:50:46 -08002165 const int no_restoration =
David Turnerebf96f42018-11-14 16:57:57 +00002166 rst_info[0].frame_restoration_type == RESTORE_NONE &&
2167 rst_info[1].frame_restoration_type == RESTORE_NONE &&
2168 rst_info[2].frame_restoration_type == RESTORE_NONE;
Urvang Joshib6409e92020-03-23 11:23:27 -07002169 assert(IMPLIES(cm->features.coded_lossless, no_loopfilter && no_cdef));
2170 assert(IMPLIES(cm->features.all_lossless, no_restoration));
Urvang Joshi54ffae72020-03-23 13:37:10 -07002171 cm->tiles.single_tile_decoding = no_loopfilter && no_cdef && no_restoration;
Yunqing Wanga10b46b2018-03-22 15:36:56 -07002172 }
2173}
2174
Wan-Teh Chang94534432024-08-08 20:29:57 -07002175static inline void read_tile_info(AV1Decoder *const pbi,
2176 struct aom_read_bit_buffer *const rb) {
Yunqing Wanga10b46b2018-03-22 15:36:56 -07002177 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002178
James Zern3df52f82018-02-26 12:57:14 -08002179 read_tile_info_max_tile(cm, rb);
Ryan Lei7386eda2016-12-08 21:08:31 -08002180
Urvang Joshi1de67aa2020-03-20 11:21:57 -07002181 pbi->context_update_tile_id = 0;
Urvang Joshi54ffae72020-03-23 13:37:10 -07002182 if (cm->tiles.rows * cm->tiles.cols > 1) {
Dominic Symesa48289a2018-04-03 21:07:59 +02002183 // tile to use for cdf update
Urvang Joshi1de67aa2020-03-20 11:21:57 -07002184 pbi->context_update_tile_id =
Urvang Joshi54ffae72020-03-23 13:37:10 -07002185 aom_rb_read_literal(rb, cm->tiles.log2_rows + cm->tiles.log2_cols);
2186 if (pbi->context_update_tile_id >= cm->tiles.rows * cm->tiles.cols) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302187 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Wan-Teh Chang7c241852018-08-04 15:48:49 -07002188 "Invalid context_update_tile_id");
2189 }
James Zern9e9f7ad2018-03-02 17:38:53 -08002190 // tile size magnitude
2191 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2192 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002193}
2194
Yunqing Wang445739a2018-06-10 12:27:34 -07002195#if EXT_TILE_DEBUG
Wan-Teh Chang94534432024-08-08 20:29:57 -07002196static inline void read_ext_tile_info(AV1Decoder *const pbi,
2197 struct aom_read_bit_buffer *const rb) {
Yunqing Wang445739a2018-06-10 12:27:34 -07002198 AV1_COMMON *const cm = &pbi->common;
2199
Yunqing Wang3f9c1e22018-06-16 16:48:15 -07002200 // This information is stored as a separate byte.
2201 int mod = rb->bit_offset % CHAR_BIT;
2202 if (mod > 0) aom_rb_read_literal(rb, CHAR_BIT - mod);
2203 assert(rb->bit_offset % CHAR_BIT == 0);
2204
Urvang Joshi54ffae72020-03-23 13:37:10 -07002205 if (cm->tiles.cols * cm->tiles.rows > 1) {
Yunqing Wang445739a2018-06-10 12:27:34 -07002206 // Read the number of bytes used to store tile size
2207 pbi->tile_col_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2208 pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
2209 }
Yunqing Wang445739a2018-06-10 12:27:34 -07002210}
2211#endif // EXT_TILE_DEBUG
2212
Hui Su5ab25aa2018-04-25 12:25:05 -07002213static size_t mem_get_varsize(const uint8_t *src, int sz) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002214 switch (sz) {
2215 case 1: return src[0];
2216 case 2: return mem_get_le16(src);
2217 case 3: return mem_get_le24(src);
2218 case 4: return mem_get_le32(src);
James Zern88896732017-06-23 15:55:09 -07002219 default: assert(0 && "Invalid size"); return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002220 }
2221}
2222
Yunqing Wang3a74b252018-05-03 15:24:07 -07002223#if EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07002224// Reads the next tile returning its size and adjusting '*data' accordingly
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002225// based on 'is_last'. On return, '*data' is updated to point to the end of the
2226// raw tile buffer in the bit stream.
Wan-Teh Chang94534432024-08-08 20:29:57 -07002227static inline void get_ls_tile_buffer(
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002228 const uint8_t *const data_end, struct aom_internal_error_info *error_info,
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +01002229 const uint8_t **data, TileBufferDec (*const tile_buffers)[MAX_TILE_COLS],
2230 int tile_size_bytes, int col, int row, int tile_copy_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002231 size_t size;
2232
2233 size_t copy_size = 0;
2234 const uint8_t *copy_data = NULL;
2235
2236 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002237 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002238 "Truncated packet or corrupt tile length");
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +01002239 size = mem_get_varsize(*data, tile_size_bytes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002240
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002241 // If tile_copy_mode = 1, then the top bit of the tile header indicates copy
2242 // mode.
2243 if (tile_copy_mode && (size >> (tile_size_bytes * 8 - 1)) == 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002244 // The remaining bits in the top byte signal the row offset
2245 int offset = (size >> (tile_size_bytes - 1) * 8) & 0x7f;
Wan-Teh Chang4aefb932024-05-28 14:24:57 -07002246 if (offset > row) {
2247 aom_internal_error(
2248 error_info, AOM_CODEC_CORRUPT_FRAME,
2249 "Invalid row offset in tile copy mode: row=%d offset=%d", row,
2250 offset);
2251 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002252
2253 // Currently, only use tiles in same column as reference tiles.
2254 copy_data = tile_buffers[row - offset][col].data;
2255 copy_size = tile_buffers[row - offset][col].size;
2256 size = 0;
James Zern2f22bfd2018-03-10 12:58:22 -08002257 } else {
2258 size += AV1_MIN_TILE_SIZE_BYTES;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259 }
2260
2261 *data += tile_size_bytes;
2262
2263 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002264 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265 "Truncated packet or corrupt tile size");
2266
2267 if (size > 0) {
2268 tile_buffers[row][col].data = *data;
2269 tile_buffers[row][col].size = size;
2270 } else {
2271 tile_buffers[row][col].data = copy_data;
2272 tile_buffers[row][col].size = copy_size;
2273 }
2274
2275 *data += size;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002276}
2277
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002278// Returns the end of the last tile buffer
Urvang Joshi54ffae72020-03-23 13:37:10 -07002279// (tile_buffers[cm->tiles.rows - 1][cm->tiles.cols - 1]).
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002280static const uint8_t *get_ls_tile_buffers(
Yaowu Xuf883b422016-08-30 14:01:10 -07002281 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002282 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002283 AV1_COMMON *const cm = &pbi->common;
Urvang Joshi54ffae72020-03-23 13:37:10 -07002284 const int tile_cols = cm->tiles.cols;
2285 const int tile_rows = cm->tiles.rows;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286 const int have_tiles = tile_cols * tile_rows > 1;
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002287 const uint8_t *raw_data_end; // The end of the last tile buffer
Yaowu Xuc27fc142016-08-22 16:08:15 -07002288
2289 if (!have_tiles) {
Jingning Han99ffce62017-04-25 15:48:41 -07002290 const size_t tile_size = data_end - data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002291 tile_buffers[0][0].data = data;
2292 tile_buffers[0][0].size = tile_size;
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002293 raw_data_end = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002294 } else {
2295 // We locate only the tile buffers that are required, which are the ones
2296 // specified by pbi->dec_tile_col and pbi->dec_tile_row. Also, we always
2297 // need the last (bottom right) tile buffer, as we need to know where the
2298 // end of the compressed frame buffer is for proper superframe decoding.
2299
Yunqing Wangecc07342018-02-26 16:45:38 -08002300 const uint8_t *tile_col_data_end[MAX_TILE_COLS] = { NULL };
Yaowu Xuc27fc142016-08-22 16:08:15 -07002301 const uint8_t *const data_start = data;
2302
Yaowu Xuf883b422016-08-30 14:01:10 -07002303 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002304 const int single_row = pbi->dec_tile_row >= 0;
2305 const int tile_rows_start = single_row ? dec_tile_row : 0;
2306 const int tile_rows_end = single_row ? tile_rows_start + 1 : tile_rows;
Yaowu Xuf883b422016-08-30 14:01:10 -07002307 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002308 const int single_col = pbi->dec_tile_col >= 0;
2309 const int tile_cols_start = single_col ? dec_tile_col : 0;
2310 const int tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2311
2312 const int tile_col_size_bytes = pbi->tile_col_size_bytes;
2313 const int tile_size_bytes = pbi->tile_size_bytes;
Frederic Barbier96621892018-11-29 14:48:01 +01002314 int tile_width, tile_height;
Wan-Teh Changcc715882024-03-04 13:52:25 -08002315 if (!av1_get_uniform_tile_size(cm, &tile_width, &tile_height)) {
2316 aom_internal_error(
2317 &pbi->error, AOM_CODEC_CORRUPT_FRAME,
2318 "Not all the tiles in the tile list have the same size.");
2319 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002320 const int tile_copy_mode =
Frederic Barbier96621892018-11-29 14:48:01 +01002321 ((AOMMAX(tile_width, tile_height) << MI_SIZE_LOG2) <= 256) ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002322 // Read tile column sizes for all columns (we need the last tile buffer)
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002323 for (int c = 0; c < tile_cols; ++c) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002324 const int is_last = c == tile_cols - 1;
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002325 size_t tile_col_size;
2326
Yaowu Xuc27fc142016-08-22 16:08:15 -07002327 if (!is_last) {
Wan-Teh Chang08efc142024-03-04 17:11:12 -08002328 if (tile_col_size_bytes > data_end - data) {
2329 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2330 "Not enough data to read tile_col_size");
2331 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002332 tile_col_size = mem_get_varsize(data, tile_col_size_bytes);
2333 data += tile_col_size_bytes;
Wan-Teh Chang08efc142024-03-04 17:11:12 -08002334 if (tile_col_size > (size_t)(data_end - data)) {
2335 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
2336 "tile_col_data_end[%d] is out of bound", c);
2337 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002338 tile_col_data_end[c] = data + tile_col_size;
2339 } else {
2340 tile_col_size = data_end - data;
2341 tile_col_data_end[c] = data_end;
2342 }
2343 data += tile_col_size;
2344 }
2345
2346 data = data_start;
2347
2348 // Read the required tile sizes.
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002349 for (int c = tile_cols_start; c < tile_cols_end; ++c) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002350 const int is_last = c == tile_cols - 1;
2351
2352 if (c > 0) data = tile_col_data_end[c - 1];
2353
2354 if (!is_last) data += tile_col_size_bytes;
2355
2356 // Get the whole of the last column, otherwise stop at the required tile.
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002357 for (int r = 0; r < (is_last ? tile_rows : tile_rows_end); ++r) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302358 get_ls_tile_buffer(tile_col_data_end[c], &pbi->error, &data,
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +01002359 tile_buffers, tile_size_bytes, c, r, tile_copy_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002360 }
2361 }
2362
2363 // If we have not read the last column, then read it to get the last tile.
2364 if (tile_cols_end != tile_cols) {
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002365 const int c = tile_cols - 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002366
2367 data = tile_col_data_end[c - 1];
2368
Sebastien Alaiwan3e4068e2017-12-01 14:23:45 +01002369 for (int r = 0; r < tile_rows; ++r) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302370 get_ls_tile_buffer(tile_col_data_end[c], &pbi->error, &data,
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +01002371 tile_buffers, tile_size_bytes, c, r, tile_copy_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002372 }
2373 }
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002374 raw_data_end = data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002375 }
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002376 return raw_data_end;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002377}
Yunqing Wang3a74b252018-05-03 15:24:07 -07002378#endif // EXT_TILE_DEBUG
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002379
Yunqing Wangd7546d42018-06-06 15:08:05 -07002380static const uint8_t *get_ls_single_tile_buffer(
2381 AV1Decoder *pbi, const uint8_t *data,
2382 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS]) {
2383 assert(pbi->dec_tile_row >= 0 && pbi->dec_tile_col >= 0);
2384 tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].data = data;
2385 tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].size =
2386 (size_t)pbi->coded_tile_data_size;
2387 return data + pbi->coded_tile_data_size;
2388}
2389
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390// Reads the next tile returning its size and adjusting '*data' accordingly
2391// based on 'is_last'.
Wan-Teh Chang94534432024-08-08 20:29:57 -07002392static inline void get_tile_buffer(const uint8_t *const data_end,
2393 const int tile_size_bytes, int is_last,
2394 struct aom_internal_error_info *error_info,
2395 const uint8_t **data,
2396 TileBufferDec *const buf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397 size_t size;
2398
2399 if (!is_last) {
Yaowu Xu0a79a1b2017-02-17 13:04:54 -08002400 if (!read_is_valid(*data, tile_size_bytes, data_end))
Yaowu Xuf883b422016-08-30 14:01:10 -07002401 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Wan-Teh Changc40acb22019-06-20 09:51:39 -07002402 "Not enough data to read tile size");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002403
James Zern2f22bfd2018-03-10 12:58:22 -08002404 size = mem_get_varsize(*data, tile_size_bytes) + AV1_MIN_TILE_SIZE_BYTES;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002405 *data += tile_size_bytes;
2406
2407 if (size > (size_t)(data_end - *data))
Yaowu Xuf883b422016-08-30 14:01:10 -07002408 aom_internal_error(error_info, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409 "Truncated packet or corrupt tile size");
2410 } else {
2411 size = data_end - *data;
2412 }
2413
2414 buf->data = *data;
2415 buf->size = size;
2416
2417 *data += size;
2418}
2419
Wan-Teh Chang94534432024-08-08 20:29:57 -07002420static inline void get_tile_buffers(
Elliott Karpilovsky18fcd6a2019-09-16 15:15:06 -07002421 AV1Decoder *pbi, const uint8_t *data, const uint8_t *data_end,
2422 TileBufferDec (*const tile_buffers)[MAX_TILE_COLS], int start_tile,
2423 int end_tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002424 AV1_COMMON *const cm = &pbi->common;
Urvang Joshi54ffae72020-03-23 13:37:10 -07002425 const int tile_cols = cm->tiles.cols;
2426 const int tile_rows = cm->tiles.rows;
Thomas Davies80188d12016-10-26 16:08:35 -07002427 int tc = 0;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04002428
Sebastien Alaiwanf518f142017-12-01 14:25:41 +01002429 for (int r = 0; r < tile_rows; ++r) {
2430 for (int c = 0; c < tile_cols; ++c, ++tc) {
Thomas Davies80188d12016-10-26 16:08:35 -07002431 TileBufferDec *const buf = &tile_buffers[r][c];
Tom Fineganff863952017-12-22 11:41:14 -08002432
Cheng Chena98e55e2018-06-12 23:14:19 -07002433 const int is_last = (tc == end_tile);
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04002434 const size_t hdr_offset = 0;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04002435
Cheng Chena98e55e2018-06-12 23:14:19 -07002436 if (tc < start_tile || tc > end_tile) continue;
Thomas Davies80188d12016-10-26 16:08:35 -07002437
Rupert Swarbrickcd757392017-09-01 13:57:53 +01002438 if (data + hdr_offset >= data_end)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302439 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Rupert Swarbrickcd757392017-09-01 13:57:53 +01002440 "Data ended before all tiles were read.");
Thomas Davies80188d12016-10-26 16:08:35 -07002441 data += hdr_offset;
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302442 get_tile_buffer(data_end, pbi->tile_size_bytes, is_last, &pbi->error,
2443 &data, buf);
Thomas Davies80188d12016-10-26 16:08:35 -07002444 }
2445 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002446}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002447
Wan-Teh Chang94534432024-08-08 20:29:57 -07002448static inline void set_cb_buffer(AV1Decoder *pbi, DecoderCodingBlock *dcb,
2449 CB_BUFFER *cb_buffer_base,
2450 const int num_planes, int mi_row, int mi_col) {
Deepa K Gec1987a2018-07-03 14:22:27 +05302451 AV1_COMMON *const cm = &pbi->common;
Tarundeep Singh4243e622021-04-20 16:10:22 +05302452 int mib_size_log2 = cm->seq_params->mib_size_log2;
Urvang Joshi9dc909d2020-03-23 16:07:02 -07002453 int stride = (cm->mi_params.mi_cols >> mib_size_log2) + 1;
Deepa K Gec1987a2018-07-03 14:22:27 +05302454 int offset = (mi_row >> mib_size_log2) * stride + (mi_col >> mib_size_log2);
2455 CB_BUFFER *cb_buffer = cb_buffer_base + offset;
2456
Deepa K G610c9b82018-06-19 12:47:01 +05302457 for (int plane = 0; plane < num_planes; ++plane) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07002458 dcb->dqcoeff_block[plane] = cb_buffer->dqcoeff[plane];
2459 dcb->eob_data[plane] = cb_buffer->eob_data[plane];
2460 dcb->cb_offset[plane] = 0;
2461 dcb->txb_offset[plane] = 0;
Deepa K G610c9b82018-06-19 12:47:01 +05302462 }
Urvang Joshibac1dea2020-04-20 11:37:15 -07002463 MACROBLOCKD *const xd = &dcb->xd;
Deepa K G99f7ddb2018-06-20 11:21:21 +05302464 xd->plane[0].color_index_map = cb_buffer->color_index_map[0];
2465 xd->plane[1].color_index_map = cb_buffer->color_index_map[1];
2466 xd->color_index_map_offset[0] = 0;
2467 xd->color_index_map_offset[1] = 0;
Deepa K G610c9b82018-06-19 12:47:01 +05302468}
2469
Wan-Teh Chang94534432024-08-08 20:29:57 -07002470static inline void decoder_alloc_tile_data(AV1Decoder *pbi, const int n_tiles) {
Deepa K Gcebe0782018-07-05 11:27:40 +05302471 AV1_COMMON *const cm = &pbi->common;
2472 aom_free(pbi->tile_data);
Satheesh Kumarc1279722023-12-20 15:17:10 +05302473 pbi->allocated_tiles = 0;
Deepa K Gcebe0782018-07-05 11:27:40 +05302474 CHECK_MEM_ERROR(cm, pbi->tile_data,
2475 aom_memalign(32, n_tiles * sizeof(*pbi->tile_data)));
2476 pbi->allocated_tiles = n_tiles;
2477 for (int i = 0; i < n_tiles; i++) {
2478 TileDataDec *const tile_data = pbi->tile_data + i;
2479 av1_zero(tile_data->dec_row_mt_sync);
2480 }
2481 pbi->allocated_row_mt_sync_rows = 0;
2482}
2483
2484// Set up nsync by width.
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07002485static inline int get_sync_range(int width) {
Deepa K Gcebe0782018-07-05 11:27:40 +05302486// nsync numbers are picked by testing.
2487#if 0
2488 if (width < 640)
2489 return 1;
2490 else if (width <= 1280)
2491 return 2;
2492 else if (width <= 4096)
2493 return 4;
2494 else
2495 return 8;
2496#else
2497 (void)width;
2498#endif
2499 return 1;
2500}
2501
2502// Allocate memory for decoder row synchronization
Wan-Teh Chang94534432024-08-08 20:29:57 -07002503static inline void dec_row_mt_alloc(AV1DecRowMTSync *dec_row_mt_sync,
2504 AV1_COMMON *cm, int rows) {
Deepa K Gcebe0782018-07-05 11:27:40 +05302505 dec_row_mt_sync->allocated_sb_rows = rows;
2506#if CONFIG_MULTITHREAD
2507 {
2508 int i;
2509
2510 CHECK_MEM_ERROR(cm, dec_row_mt_sync->mutex_,
2511 aom_malloc(sizeof(*(dec_row_mt_sync->mutex_)) * rows));
2512 if (dec_row_mt_sync->mutex_) {
2513 for (i = 0; i < rows; ++i) {
2514 pthread_mutex_init(&dec_row_mt_sync->mutex_[i], NULL);
2515 }
2516 }
2517
2518 CHECK_MEM_ERROR(cm, dec_row_mt_sync->cond_,
2519 aom_malloc(sizeof(*(dec_row_mt_sync->cond_)) * rows));
2520 if (dec_row_mt_sync->cond_) {
2521 for (i = 0; i < rows; ++i) {
2522 pthread_cond_init(&dec_row_mt_sync->cond_[i], NULL);
2523 }
2524 }
2525 }
2526#endif // CONFIG_MULTITHREAD
2527
2528 CHECK_MEM_ERROR(cm, dec_row_mt_sync->cur_sb_col,
2529 aom_malloc(sizeof(*(dec_row_mt_sync->cur_sb_col)) * rows));
2530
2531 // Set up nsync.
2532 dec_row_mt_sync->sync_range = get_sync_range(cm->width);
2533}
2534
2535// Deallocate decoder row synchronization related mutex and data
2536void av1_dec_row_mt_dealloc(AV1DecRowMTSync *dec_row_mt_sync) {
2537 if (dec_row_mt_sync != NULL) {
2538#if CONFIG_MULTITHREAD
2539 int i;
2540 if (dec_row_mt_sync->mutex_ != NULL) {
2541 for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
2542 pthread_mutex_destroy(&dec_row_mt_sync->mutex_[i]);
2543 }
2544 aom_free(dec_row_mt_sync->mutex_);
2545 }
2546 if (dec_row_mt_sync->cond_ != NULL) {
2547 for (i = 0; i < dec_row_mt_sync->allocated_sb_rows; ++i) {
2548 pthread_cond_destroy(&dec_row_mt_sync->cond_[i]);
2549 }
2550 aom_free(dec_row_mt_sync->cond_);
2551 }
2552#endif // CONFIG_MULTITHREAD
2553 aom_free(dec_row_mt_sync->cur_sb_col);
2554
2555 // clear the structure as the source of this call may be a resize in which
2556 // case this call will be followed by an _alloc() which may fail.
2557 av1_zero(*dec_row_mt_sync);
2558 }
2559}
2560
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07002561static inline void sync_read(AV1DecRowMTSync *const dec_row_mt_sync, int r,
Deepa K Gcebe0782018-07-05 11:27:40 +05302562 int c) {
2563#if CONFIG_MULTITHREAD
2564 const int nsync = dec_row_mt_sync->sync_range;
2565
2566 if (r && !(c & (nsync - 1))) {
2567 pthread_mutex_t *const mutex = &dec_row_mt_sync->mutex_[r - 1];
2568 pthread_mutex_lock(mutex);
2569
Jayasanker J34ec50a2022-07-01 19:00:02 +05302570 while (c > dec_row_mt_sync->cur_sb_col[r - 1] - nsync -
2571 dec_row_mt_sync->intrabc_extra_top_right_sb_delay) {
Deepa K Gcebe0782018-07-05 11:27:40 +05302572 pthread_cond_wait(&dec_row_mt_sync->cond_[r - 1], mutex);
2573 }
2574 pthread_mutex_unlock(mutex);
2575 }
2576#else
2577 (void)dec_row_mt_sync;
2578 (void)r;
2579 (void)c;
2580#endif // CONFIG_MULTITHREAD
2581}
2582
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07002583static inline void sync_write(AV1DecRowMTSync *const dec_row_mt_sync, int r,
Deepa K Gcebe0782018-07-05 11:27:40 +05302584 int c, const int sb_cols) {
2585#if CONFIG_MULTITHREAD
2586 const int nsync = dec_row_mt_sync->sync_range;
2587 int cur;
2588 int sig = 1;
2589
2590 if (c < sb_cols - 1) {
2591 cur = c;
2592 if (c % nsync) sig = 0;
2593 } else {
Jayasanker J34ec50a2022-07-01 19:00:02 +05302594 cur = sb_cols + nsync + dec_row_mt_sync->intrabc_extra_top_right_sb_delay;
Deepa K Gcebe0782018-07-05 11:27:40 +05302595 }
2596
2597 if (sig) {
2598 pthread_mutex_lock(&dec_row_mt_sync->mutex_[r]);
2599
2600 dec_row_mt_sync->cur_sb_col[r] = cur;
2601
2602 pthread_cond_signal(&dec_row_mt_sync->cond_[r]);
2603 pthread_mutex_unlock(&dec_row_mt_sync->mutex_[r]);
2604 }
2605#else
2606 (void)dec_row_mt_sync;
2607 (void)r;
2608 (void)c;
2609 (void)sb_cols;
2610#endif // CONFIG_MULTITHREAD
2611}
2612
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07002613static inline void signal_decoding_done_for_erroneous_row(
venkat sanampudidd5066d2022-02-14 21:17:02 +05302614 AV1Decoder *const pbi, const MACROBLOCKD *const xd) {
2615 AV1_COMMON *const cm = &pbi->common;
2616 const TileInfo *const tile = &xd->tile;
2617 const int sb_row_in_tile =
2618 ((xd->mi_row - tile->mi_row_start) >> cm->seq_params->mib_size_log2);
2619 const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile);
2620 TileDataDec *const tile_data =
2621 pbi->tile_data + tile->tile_row * cm->tiles.cols + tile->tile_col;
2622 AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
2623
2624 sync_write(dec_row_mt_sync, sb_row_in_tile, sb_cols_in_tile - 1,
2625 sb_cols_in_tile);
2626}
2627
Wan-Teh Chang94534432024-08-08 20:29:57 -07002628static inline void decode_tile_sb_row(AV1Decoder *pbi, ThreadData *const td,
2629 const TileInfo *tile_info,
2630 const int mi_row) {
Cherma Rajan Accd86512018-03-28 17:24:20 +05302631 AV1_COMMON *const cm = &pbi->common;
Deepa K G610c9b82018-06-19 12:47:01 +05302632 const int num_planes = av1_num_planes(cm);
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08002633 TileDataDec *const tile_data = pbi->tile_data +
2634 tile_info->tile_row * cm->tiles.cols +
2635 tile_info->tile_col;
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05302636 const int sb_cols_in_tile = av1_get_sb_cols_in_tile(cm, tile_info);
Deepa K Gcebe0782018-07-05 11:27:40 +05302637 const int sb_row_in_tile =
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08002638 (mi_row - tile_info->mi_row_start) >> cm->seq_params->mib_size_log2;
Deepa K Gcebe0782018-07-05 11:27:40 +05302639 int sb_col_in_tile = 0;
venkat sanampudi870fa862022-02-21 09:15:53 +05302640 int row_mt_exit = 0;
Cherma Rajan Accd86512018-03-28 17:24:20 +05302641
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08002642 for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
Tarundeep Singh4243e622021-04-20 16:10:22 +05302643 mi_col += cm->seq_params->mib_size, sb_col_in_tile++) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07002644 set_cb_buffer(pbi, &td->dcb, pbi->cb_buffer_base, num_planes, mi_row,
Deepa K Gec1987a2018-07-03 14:22:27 +05302645 mi_col);
Deepa K G610c9b82018-06-19 12:47:01 +05302646
Deepa K Gcebe0782018-07-05 11:27:40 +05302647 sync_read(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile);
2648
venkat sanampudi870fa862022-02-21 09:15:53 +05302649#if CONFIG_MULTITHREAD
2650 pthread_mutex_lock(pbi->row_mt_mutex_);
2651#endif
2652 row_mt_exit = pbi->frame_row_mt_info.row_mt_exit;
2653#if CONFIG_MULTITHREAD
2654 pthread_mutex_unlock(pbi->row_mt_mutex_);
2655#endif
2656
2657 if (!row_mt_exit) {
2658 // Decoding of the super-block
2659 decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
2660 cm->seq_params->sb_size, 0x2);
2661 }
Deepa K Gcebe0782018-07-05 11:27:40 +05302662
2663 sync_write(&tile_data->dec_row_mt_sync, sb_row_in_tile, sb_col_in_tile,
2664 sb_cols_in_tile);
Cherma Rajan Accd86512018-03-28 17:24:20 +05302665 }
Cherma Rajan Accd86512018-03-28 17:24:20 +05302666}
2667
Wan-Teh Chang4eda2842018-05-07 15:03:35 -07002668static int check_trailing_bits_after_symbol_coder(aom_reader *r) {
Wan-Teh Chang47d59ce2018-08-14 10:15:45 -07002669 if (aom_reader_has_overflowed(r)) return -1;
2670
Wan-Teh Chang4eda2842018-05-07 15:03:35 -07002671 uint32_t nb_bits = aom_reader_tell(r);
2672 uint32_t nb_bytes = (nb_bits + 7) >> 3;
Wan-Teh Chang47d59ce2018-08-14 10:15:45 -07002673 const uint8_t *p = aom_reader_find_begin(r) + nb_bytes;
Wan-Teh Chang4eda2842018-05-07 15:03:35 -07002674
2675 // aom_reader_tell() returns 1 for a newly initialized decoder, and the
2676 // return value only increases as values are decoded. So nb_bits > 0, and
2677 // thus p > p_begin. Therefore accessing p[-1] is safe.
2678 uint8_t last_byte = p[-1];
2679 uint8_t pattern = 128 >> ((nb_bits - 1) & 7);
2680 if ((last_byte & (2 * pattern - 1)) != pattern) return -1;
2681
2682 // Make sure that all padding bytes are zero as required by the spec.
Wan-Teh Chang47d59ce2018-08-14 10:15:45 -07002683 const uint8_t *p_end = aom_reader_find_end(r);
Wan-Teh Chang4eda2842018-05-07 15:03:35 -07002684 while (p < p_end) {
2685 if (*p != 0) return -1;
2686 p++;
2687 }
2688 return 0;
2689}
2690
Wan-Teh Chang94534432024-08-08 20:29:57 -07002691static inline void set_decode_func_pointers(ThreadData *td,
2692 int parse_decode_flag) {
Deepa K G1909dee2018-07-03 17:37:56 +05302693 td->read_coeffs_tx_intra_block_visit = decode_block_void;
2694 td->predict_and_recon_intra_block_visit = decode_block_void;
2695 td->read_coeffs_tx_inter_block_visit = decode_block_void;
2696 td->inverse_tx_inter_block_visit = decode_block_void;
2697 td->predict_inter_block_visit = predict_inter_block_void;
2698 td->cfl_store_inter_block_visit = cfl_store_inter_block_void;
2699
2700 if (parse_decode_flag & 0x1) {
2701 td->read_coeffs_tx_intra_block_visit = read_coeffs_tx_intra_block;
2702 td->read_coeffs_tx_inter_block_visit = av1_read_coeffs_txb_facade;
2703 }
2704 if (parse_decode_flag & 0x2) {
2705 td->predict_and_recon_intra_block_visit =
2706 predict_and_reconstruct_intra_block;
2707 td->inverse_tx_inter_block_visit = inverse_transform_inter_block;
2708 td->predict_inter_block_visit = predict_inter_block;
2709 td->cfl_store_inter_block_visit = cfl_store_inter_block;
Deepa K G3bc1f932018-06-21 14:25:03 +05302710 }
2711}
2712
Wan-Teh Chang94534432024-08-08 20:29:57 -07002713static inline void decode_tile(AV1Decoder *pbi, ThreadData *const td,
2714 int tile_row, int tile_col) {
Cherma Rajan Accd86512018-03-28 17:24:20 +05302715 TileInfo tile_info;
2716
2717 AV1_COMMON *const cm = &pbi->common;
2718 const int num_planes = av1_num_planes(cm);
Cherma Rajan Accd86512018-03-28 17:24:20 +05302719
2720 av1_tile_set_row(&tile_info, cm, tile_row);
2721 av1_tile_set_col(&tile_info, cm, tile_col);
Urvang Joshibac1dea2020-04-20 11:37:15 -07002722 DecoderCodingBlock *const dcb = &td->dcb;
2723 MACROBLOCKD *const xd = &dcb->xd;
2724
2725 av1_zero_above_context(cm, xd, tile_info.mi_col_start, tile_info.mi_col_end,
2726 tile_row);
2727 av1_reset_loop_filter_delta(xd, num_planes);
2728 av1_reset_loop_restoration(xd, num_planes);
Cherma Rajan Accd86512018-03-28 17:24:20 +05302729
2730 for (int mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end;
Tarundeep Singh4243e622021-04-20 16:10:22 +05302731 mi_row += cm->seq_params->mib_size) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07002732 av1_zero_left_context(xd);
Deepa K G3bc1f932018-06-21 14:25:03 +05302733
2734 for (int mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end;
Tarundeep Singh4243e622021-04-20 16:10:22 +05302735 mi_col += cm->seq_params->mib_size) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07002736 set_cb_buffer(pbi, dcb, &td->cb_buffer_base, num_planes, 0, 0);
Deepa K G3bc1f932018-06-21 14:25:03 +05302737
2738 // Bit-stream parsing and decoding of the superblock
Deepa K G1909dee2018-07-03 17:37:56 +05302739 decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
Tarundeep Singh4243e622021-04-20 16:10:22 +05302740 cm->seq_params->sb_size, 0x3);
Wan-Teh Chang47d59ce2018-08-14 10:15:45 -07002741
2742 if (aom_reader_has_overflowed(td->bit_reader)) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07002743 aom_merge_corrupted_flag(&dcb->corrupted, 1);
Wan-Teh Chang47d59ce2018-08-14 10:15:45 -07002744 return;
2745 }
Deepa K G3bc1f932018-06-21 14:25:03 +05302746 }
Cherma Rajan Accd86512018-03-28 17:24:20 +05302747 }
Frank Bossendf46b222018-03-27 14:53:51 -04002748
Yunqing Wang789d5282018-05-04 17:18:02 -07002749 int corrupted =
2750 (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
Urvang Joshibac1dea2020-04-20 11:37:15 -07002751 aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
Cherma Rajan Accd86512018-03-28 17:24:20 +05302752}
2753
Yaowu Xuf883b422016-08-30 14:01:10 -07002754static const uint8_t *decode_tiles(AV1Decoder *pbi, const uint8_t *data,
Cheng Chena98e55e2018-06-12 23:14:19 -07002755 const uint8_t *data_end, int start_tile,
2756 int end_tile) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002757 AV1_COMMON *const cm = &pbi->common;
Wan-Teh Changf4fe6612018-05-31 17:42:18 -07002758 ThreadData *const td = &pbi->td;
Urvang Joshi54ffae72020-03-23 13:37:10 -07002759 CommonTileParams *const tiles = &cm->tiles;
2760 const int tile_cols = tiles->cols;
2761 const int tile_rows = tiles->rows;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002762 const int n_tiles = tile_cols * tile_rows;
clang-format67948d32016-09-07 22:40:40 -07002763 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
Yaowu Xuf883b422016-08-30 14:01:10 -07002764 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002765 const int single_row = pbi->dec_tile_row >= 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002766 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002767 const int single_col = pbi->dec_tile_col >= 0;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002768 int tile_rows_start;
2769 int tile_rows_end;
2770 int tile_cols_start;
2771 int tile_cols_end;
2772 int inv_col_order;
2773 int inv_row_order;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002774 int tile_row, tile_col;
Yunqing Wang0e141b52017-11-02 15:08:58 -07002775 uint8_t allow_update_cdf;
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002776 const uint8_t *raw_data_end = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002777
Urvang Joshi54ffae72020-03-23 13:37:10 -07002778 if (tiles->large_scale) {
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002779 tile_rows_start = single_row ? dec_tile_row : 0;
2780 tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
2781 tile_cols_start = single_col ? dec_tile_col : 0;
2782 tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
2783 inv_col_order = pbi->inv_tile_order && !single_col;
2784 inv_row_order = pbi->inv_tile_order && !single_row;
Yunqing Wang0e141b52017-11-02 15:08:58 -07002785 allow_update_cdf = 0;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002786 } else {
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002787 tile_rows_start = 0;
2788 tile_rows_end = tile_rows;
2789 tile_cols_start = 0;
2790 tile_cols_end = tile_cols;
2791 inv_col_order = pbi->inv_tile_order;
2792 inv_row_order = pbi->inv_tile_order;
Yunqing Wang0e141b52017-11-02 15:08:58 -07002793 allow_update_cdf = 1;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002794 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002795
Hui Su4ff78832018-03-05 10:29:34 -08002796 // No tiles to decode.
Hui Su51948b22018-03-30 16:49:08 -07002797 if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
Cheng Chena98e55e2018-06-12 23:14:19 -07002798 // First tile is larger than end_tile.
Urvang Joshi54ffae72020-03-23 13:37:10 -07002799 tile_rows_start * tiles->cols + tile_cols_start > end_tile ||
Cheng Chena98e55e2018-06-12 23:14:19 -07002800 // Last tile is smaller than start_tile.
Urvang Joshi54ffae72020-03-23 13:37:10 -07002801 (tile_rows_end - 1) * tiles->cols + tile_cols_end - 1 < start_tile)
Hui Su4ff78832018-03-05 10:29:34 -08002802 return data;
2803
Urvang Joshib6409e92020-03-23 11:23:27 -07002804 allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
Hui Su483a8452018-02-26 12:28:48 -08002805
Yaowu Xuc27fc142016-08-22 16:08:15 -07002806 assert(tile_rows <= MAX_TILE_ROWS);
2807 assert(tile_cols <= MAX_TILE_COLS);
2808
Yunqing Wang3a74b252018-05-03 15:24:07 -07002809#if EXT_TILE_DEBUG
Urvang Joshi54ffae72020-03-23 13:37:10 -07002810 if (tiles->large_scale && !pbi->ext_tile_debug)
Yunqing Wangd7546d42018-06-06 15:08:05 -07002811 raw_data_end = get_ls_single_tile_buffer(pbi, data, tile_buffers);
Urvang Joshi54ffae72020-03-23 13:37:10 -07002812 else if (tiles->large_scale && pbi->ext_tile_debug)
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002813 raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002814 else
Yunqing Wang3a74b252018-05-03 15:24:07 -07002815#endif // EXT_TILE_DEBUG
Cheng Chena98e55e2018-06-12 23:14:19 -07002816 get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002817
2818 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Deepa K Gcebe0782018-07-05 11:27:40 +05302819 decoder_alloc_tile_data(pbi, n_tiles);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002820 }
Chethan Kumar R Ea494e2b2021-03-18 17:19:44 +05302821 if (pbi->dcb.xd.seg_mask == NULL)
2822 CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
2823 (uint8_t *)aom_memalign(
2824 16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
Michael Bebenita6048d052016-08-25 14:40:54 -07002825#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002826 if (pbi->acct_enabled) {
2827 aom_accounting_reset(&pbi->accounting);
2828 }
Michael Bebenita6048d052016-08-25 14:40:54 -07002829#endif
Deepa K G3bc1f932018-06-21 14:25:03 +05302830
Deepa K G1909dee2018-07-03 17:37:56 +05302831 set_decode_func_pointers(&pbi->td, 0x3);
Deepa K G3bc1f932018-06-21 14:25:03 +05302832
Cherma Rajan Ae4121f62018-04-18 17:30:55 +05302833 // Load all tile information into thread_data.
Urvang Joshibac1dea2020-04-20 11:37:15 -07002834 td->dcb = pbi->dcb;
2835
2836 td->dcb.corrupted = 0;
2837 td->dcb.mc_buf[0] = td->mc_buf[0];
2838 td->dcb.mc_buf[1] = td->mc_buf[1];
2839 td->dcb.xd.tmp_conv_dst = td->tmp_conv_dst;
Urvang Joshie58f6eca2018-09-10 15:10:12 -07002840 for (int j = 0; j < 2; ++j) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07002841 td->dcb.xd.tmp_obmc_bufs[j] = td->tmp_obmc_bufs[j];
Urvang Joshie58f6eca2018-09-10 15:10:12 -07002842 }
2843
Yaowu Xuc27fc142016-08-22 16:08:15 -07002844 for (tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
2845 const int row = inv_row_order ? tile_rows - 1 - tile_row : tile_row;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002846
2847 for (tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
2848 const int col = inv_col_order ? tile_cols - 1 - tile_col : tile_col;
Urvang Joshi54ffae72020-03-23 13:37:10 -07002849 TileDataDec *const tile_data = pbi->tile_data + row * tiles->cols + col;
Cherma Rajan Ae4121f62018-04-18 17:30:55 +05302850 const TileBufferDec *const tile_bs_buf = &tile_buffers[row][col];
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04002851
Urvang Joshi54ffae72020-03-23 13:37:10 -07002852 if (row * tiles->cols + col < start_tile ||
2853 row * tiles->cols + col > end_tile)
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04002854 continue;
2855
Cherma Rajan Ae4121f62018-04-18 17:30:55 +05302856 td->bit_reader = &tile_data->bit_reader;
wenyao.liu00c80e62018-12-28 10:42:26 +08002857 av1_zero(td->cb_buffer_base.dqcoeff);
Urvang Joshibac1dea2020-04-20 11:37:15 -07002858 av1_tile_init(&td->dcb.xd.tile, cm, row, col);
Urvang Joshiacad1ca2020-04-27 17:03:25 -07002859 td->dcb.xd.current_base_qindex = cm->quant_params.base_qindex;
venkat sanampudiee9dbcb2022-02-24 21:34:22 +05302860 setup_bool_decoder(&td->dcb.xd, tile_bs_buf->data, data_end,
2861 tile_bs_buf->size, &pbi->error, td->bit_reader,
2862 allow_update_cdf);
Michael Bebenita6048d052016-08-25 14:40:54 -07002863#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002864 if (pbi->acct_enabled) {
Cherma Rajan Ae4121f62018-04-18 17:30:55 +05302865 td->bit_reader->accounting = &pbi->accounting;
2866 td->bit_reader->accounting->last_tell_frac =
2867 aom_reader_tell_frac(td->bit_reader);
2868 } else {
2869 td->bit_reader->accounting = NULL;
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -04002870 }
Michael Bebenita6048d052016-08-25 14:40:54 -07002871#endif
Urvang Joshibac1dea2020-04-20 11:37:15 -07002872 av1_init_macroblockd(cm, &td->dcb.xd);
Urvang Joshi5c8625a2020-03-30 13:16:37 -07002873 av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), row,
Urvang Joshibac1dea2020-04-20 11:37:15 -07002874 &td->dcb.xd);
Cherma Rajan Ae4121f62018-04-18 17:30:55 +05302875
2876 // Initialise the tile context from the frame context
2877 tile_data->tctx = *cm->fc;
Urvang Joshibac1dea2020-04-20 11:37:15 -07002878 td->dcb.xd.tile_ctx = &tile_data->tctx;
Cherma Rajan Ae4121f62018-04-18 17:30:55 +05302879
2880 // decode tile
Wan-Teh Chang553b0fe2018-06-27 18:02:19 -07002881 decode_tile(pbi, td, row, col);
Urvang Joshibac1dea2020-04-20 11:37:15 -07002882 aom_merge_corrupted_flag(&pbi->dcb.corrupted, td->dcb.corrupted);
2883 if (pbi->dcb.corrupted)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05302884 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Yunqing Wang789d5282018-05-04 17:18:02 -07002885 "Failed to decode tile data");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002886 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887 }
2888
Urvang Joshi54ffae72020-03-23 13:37:10 -07002889 if (tiles->large_scale) {
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002890 if (n_tiles == 1) {
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002891 // Find the end of the single tile buffer
2892 return aom_reader_find_end(&pbi->tile_data->bit_reader);
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002893 }
Sebastien Alaiwan86da7eb2017-12-20 09:44:19 +01002894 // Return the end of the last tile buffer
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07002895 return raw_data_end;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07002896 }
Cheng Chena98e55e2018-06-12 23:14:19 -07002897 TileDataDec *const tile_data = pbi->tile_data + end_tile;
Sebastien Alaiwan86da7eb2017-12-20 09:44:19 +01002898
Cherma Rajan Ae4121f62018-04-18 17:30:55 +05302899 return aom_reader_find_end(&tile_data->bit_reader);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002900}
2901
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05302902static TileJobsDec *get_dec_job_info(AV1DecTileMT *tile_mt_info) {
2903 TileJobsDec *cur_job_info = NULL;
2904#if CONFIG_MULTITHREAD
2905 pthread_mutex_lock(tile_mt_info->job_mutex);
2906
2907 if (tile_mt_info->jobs_dequeued < tile_mt_info->jobs_enqueued) {
2908 cur_job_info = tile_mt_info->job_queue + tile_mt_info->jobs_dequeued;
2909 tile_mt_info->jobs_dequeued++;
2910 }
2911
2912 pthread_mutex_unlock(tile_mt_info->job_mutex);
2913#else
2914 (void)tile_mt_info;
2915#endif
2916 return cur_job_info;
2917}
2918
Wan-Teh Chang94534432024-08-08 20:29:57 -07002919static inline void tile_worker_hook_init(AV1Decoder *const pbi,
2920 DecWorkerData *const thread_data,
2921 const TileBufferDec *const tile_buffer,
2922 TileDataDec *const tile_data,
2923 uint8_t allow_update_cdf) {
Deepa K G984c6eb2018-06-28 16:02:28 +05302924 AV1_COMMON *cm = &pbi->common;
2925 ThreadData *const td = thread_data->td;
Wan-Teh Changa98cd332018-06-29 10:06:29 -07002926 int tile_row = tile_data->tile_info.tile_row;
2927 int tile_col = tile_data->tile_info.tile_col;
Deepa K G984c6eb2018-06-28 16:02:28 +05302928
Deepa K G984c6eb2018-06-28 16:02:28 +05302929 td->bit_reader = &tile_data->bit_reader;
wenyao.liu00c80e62018-12-28 10:42:26 +08002930 av1_zero(td->cb_buffer_base.dqcoeff);
Urvang Joshibac1dea2020-04-20 11:37:15 -07002931
2932 MACROBLOCKD *const xd = &td->dcb.xd;
2933 av1_tile_init(&xd->tile, cm, tile_row, tile_col);
Urvang Joshiacad1ca2020-04-27 17:03:25 -07002934 xd->current_base_qindex = cm->quant_params.base_qindex;
venkat sanampudiee9dbcb2022-02-24 21:34:22 +05302935
2936 setup_bool_decoder(xd, tile_buffer->data, thread_data->data_end,
Yunqing Wangebc3a312018-07-03 13:05:40 -07002937 tile_buffer->size, &thread_data->error_info,
2938 td->bit_reader, allow_update_cdf);
Deepa K G984c6eb2018-06-28 16:02:28 +05302939#if CONFIG_ACCOUNTING
2940 if (pbi->acct_enabled) {
2941 td->bit_reader->accounting = &pbi->accounting;
2942 td->bit_reader->accounting->last_tell_frac =
2943 aom_reader_tell_frac(td->bit_reader);
2944 } else {
2945 td->bit_reader->accounting = NULL;
2946 }
2947#endif
Urvang Joshibac1dea2020-04-20 11:37:15 -07002948 av1_init_macroblockd(cm, xd);
2949 xd->error_info = &thread_data->error_info;
2950 av1_init_above_context(&cm->above_contexts, av1_num_planes(cm), tile_row, xd);
Deepa K G984c6eb2018-06-28 16:02:28 +05302951
2952 // Initialise the tile context from the frame context
2953 tile_data->tctx = *cm->fc;
Urvang Joshibac1dea2020-04-20 11:37:15 -07002954 xd->tile_ctx = &tile_data->tctx;
Deepa K G984c6eb2018-06-28 16:02:28 +05302955#if CONFIG_ACCOUNTING
2956 if (pbi->acct_enabled) {
2957 tile_data->bit_reader.accounting->last_tell_frac =
2958 aom_reader_tell_frac(&tile_data->bit_reader);
2959 }
2960#endif
2961}
2962
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05302963static int tile_worker_hook(void *arg1, void *arg2) {
2964 DecWorkerData *const thread_data = (DecWorkerData *)arg1;
2965 AV1Decoder *const pbi = (AV1Decoder *)arg2;
2966 AV1_COMMON *cm = &pbi->common;
2967 ThreadData *const td = thread_data->td;
2968 uint8_t allow_update_cdf;
2969
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002970 // The jmp_buf is valid only for the duration of the function that calls
2971 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2972 // before it returns.
Wan-Teh Changf3ac6512018-07-18 17:18:53 -07002973 if (setjmp(thread_data->error_info.jmp)) {
2974 thread_data->error_info.setjmp = 0;
Urvang Joshibac1dea2020-04-20 11:37:15 -07002975 thread_data->td->dcb.corrupted = 1;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05302976 return 0;
2977 }
Wan-Teh Changf3ac6512018-07-18 17:18:53 -07002978 thread_data->error_info.setjmp = 1;
Yunqing Wang1f68fee2018-07-02 12:39:09 -07002979
Urvang Joshi54ffae72020-03-23 13:37:10 -07002980 allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
Urvang Joshib6409e92020-03-23 11:23:27 -07002981 allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05302982
Deepa K G1909dee2018-07-03 17:37:56 +05302983 set_decode_func_pointers(td, 0x3);
2984
Urvang Joshi54ffae72020-03-23 13:37:10 -07002985 assert(cm->tiles.cols > 0);
Urvang Joshibac1dea2020-04-20 11:37:15 -07002986 while (!td->dcb.corrupted) {
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05302987 TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05302988
Wan-Teh Chang30e8ddb2018-09-19 18:17:32 -07002989 if (cur_job_info != NULL) {
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05302990 const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
2991 TileDataDec *const tile_data = cur_job_info->tile_data;
Deepa K G984c6eb2018-06-28 16:02:28 +05302992 tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
2993 allow_update_cdf);
2994 // decode tile
Wan-Teh Changa98cd332018-06-29 10:06:29 -07002995 int tile_row = tile_data->tile_info.tile_row;
2996 int tile_col = tile_data->tile_info.tile_col;
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05302997 decode_tile(pbi, td, tile_row, tile_col);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05302998 } else {
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05302999 break;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303000 }
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303001 }
Wan-Teh Chang323a009d2018-07-03 14:20:47 -07003002 thread_data->error_info.setjmp = 0;
Urvang Joshibac1dea2020-04-20 11:37:15 -07003003 return !td->dcb.corrupted;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303004}
3005
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07003006static inline int get_max_row_mt_workers_per_tile(AV1_COMMON *cm,
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003007 const TileInfo *tile) {
Cherma Rajan A61661692019-01-22 19:35:27 +05303008 // NOTE: Currently value of max workers is calculated based
3009 // on the parse and decode time. As per the theoretical estimate
3010 // when percentage of parse time is equal to percentage of decode
3011 // time, number of workers needed to parse + decode a tile can not
3012 // exceed more than 2.
3013 // TODO(any): Modify this value if parsing is optimized in future.
3014 int sb_rows = av1_get_sb_rows_in_tile(cm, tile);
3015 int max_workers =
3016 sb_rows == 1 ? AOM_MIN_THREADS_PER_TILE : AOM_MAX_THREADS_PER_TILE;
3017 return max_workers;
3018}
3019
Wan-Teh Chang516badf2018-09-20 13:54:01 -07003020// The caller must hold pbi->row_mt_mutex_ when calling this function.
3021// Returns 1 if either the next job is stored in *next_job_info or 1 is stored
3022// in *end_of_frame.
3023// NOTE: The caller waits on pbi->row_mt_cond_ if this function returns 0.
3024// The return value of this function depends on the following variables:
3025// - frame_row_mt_info->mi_rows_parse_done
3026// - frame_row_mt_info->mi_rows_decode_started
3027// - frame_row_mt_info->row_mt_exit
3028// Therefore we may need to signal or broadcast pbi->row_mt_cond_ if any of
3029// these variables is modified.
Deepa K G36ff5b12018-07-06 18:22:50 +05303030static int get_next_job_info(AV1Decoder *const pbi,
3031 AV1DecRowMTJobInfo *next_job_info,
3032 int *end_of_frame) {
3033 AV1_COMMON *cm = &pbi->common;
3034 TileDataDec *tile_data;
3035 AV1DecRowMTSync *dec_row_mt_sync;
3036 AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
Deepa K G36ff5b12018-07-06 18:22:50 +05303037 const int tile_rows_start = frame_row_mt_info->tile_rows_start;
3038 const int tile_rows_end = frame_row_mt_info->tile_rows_end;
3039 const int tile_cols_start = frame_row_mt_info->tile_cols_start;
3040 const int tile_cols_end = frame_row_mt_info->tile_cols_end;
3041 const int start_tile = frame_row_mt_info->start_tile;
3042 const int end_tile = frame_row_mt_info->end_tile;
Tarundeep Singh4243e622021-04-20 16:10:22 +05303043 const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
Deepa K G36ff5b12018-07-06 18:22:50 +05303044 int num_mis_to_decode, num_threads_working;
Deepa K G53922352018-07-16 12:07:20 +05303045 int num_mis_waiting_for_decode;
Deepa K G36ff5b12018-07-06 18:22:50 +05303046 int min_threads_working = INT_MAX;
Deepa K G53922352018-07-16 12:07:20 +05303047 int max_mis_to_decode = 0;
Deepa K G36ff5b12018-07-06 18:22:50 +05303048 int tile_row_idx, tile_col_idx;
Cherma Rajan A61661692019-01-22 19:35:27 +05303049 int tile_row = -1;
3050 int tile_col = -1;
Deepa K G36ff5b12018-07-06 18:22:50 +05303051
3052 memset(next_job_info, 0, sizeof(*next_job_info));
3053
3054 // Frame decode is completed or error is encountered.
3055 *end_of_frame = (frame_row_mt_info->mi_rows_decode_started ==
3056 frame_row_mt_info->mi_rows_to_decode) ||
3057 (frame_row_mt_info->row_mt_exit == 1);
3058 if (*end_of_frame) {
3059 return 1;
3060 }
3061
3062 // Decoding cannot start as bit-stream parsing is not complete.
Wan-Teh Chang516badf2018-09-20 13:54:01 -07003063 assert(frame_row_mt_info->mi_rows_parse_done >=
3064 frame_row_mt_info->mi_rows_decode_started);
3065 if (frame_row_mt_info->mi_rows_parse_done ==
3066 frame_row_mt_info->mi_rows_decode_started)
Deepa K G36ff5b12018-07-06 18:22:50 +05303067 return 0;
3068
3069 // Choose the tile to decode.
3070 for (tile_row_idx = tile_rows_start; tile_row_idx < tile_rows_end;
3071 ++tile_row_idx) {
3072 for (tile_col_idx = tile_cols_start; tile_col_idx < tile_cols_end;
3073 ++tile_col_idx) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07003074 if (tile_row_idx * cm->tiles.cols + tile_col_idx < start_tile ||
3075 tile_row_idx * cm->tiles.cols + tile_col_idx > end_tile)
Deepa K G36ff5b12018-07-06 18:22:50 +05303076 continue;
3077
Urvang Joshi54ffae72020-03-23 13:37:10 -07003078 tile_data = pbi->tile_data + tile_row_idx * cm->tiles.cols + tile_col_idx;
Deepa K G36ff5b12018-07-06 18:22:50 +05303079 dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3080
3081 num_threads_working = dec_row_mt_sync->num_threads_working;
Deepa K G53922352018-07-16 12:07:20 +05303082 num_mis_waiting_for_decode = (dec_row_mt_sync->mi_rows_parse_done -
3083 dec_row_mt_sync->mi_rows_decode_started) *
3084 dec_row_mt_sync->mi_cols;
3085 num_mis_to_decode =
3086 (dec_row_mt_sync->mi_rows - dec_row_mt_sync->mi_rows_decode_started) *
3087 dec_row_mt_sync->mi_cols;
3088
3089 assert(num_mis_to_decode >= num_mis_waiting_for_decode);
Deepa K G36ff5b12018-07-06 18:22:50 +05303090
3091 // Pick the tile which has minimum number of threads working on it.
Deepa K G53922352018-07-16 12:07:20 +05303092 if (num_mis_waiting_for_decode > 0) {
3093 if (num_threads_working < min_threads_working) {
3094 min_threads_working = num_threads_working;
3095 max_mis_to_decode = 0;
3096 }
3097 if (num_threads_working == min_threads_working &&
Cherma Rajan A61661692019-01-22 19:35:27 +05303098 num_mis_to_decode > max_mis_to_decode &&
3099 num_threads_working <
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003100 get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info)) {
Deepa K G53922352018-07-16 12:07:20 +05303101 max_mis_to_decode = num_mis_to_decode;
3102 tile_row = tile_row_idx;
3103 tile_col = tile_col_idx;
3104 }
Deepa K G36ff5b12018-07-06 18:22:50 +05303105 }
3106 }
3107 }
Cherma Rajan A61661692019-01-22 19:35:27 +05303108 // No job found to process
3109 if (tile_row == -1 || tile_col == -1) return 0;
Deepa K G36ff5b12018-07-06 18:22:50 +05303110
Urvang Joshi54ffae72020-03-23 13:37:10 -07003111 tile_data = pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
Deepa K G36ff5b12018-07-06 18:22:50 +05303112 dec_row_mt_sync = &tile_data->dec_row_mt_sync;
3113
Deepa K G36ff5b12018-07-06 18:22:50 +05303114 next_job_info->tile_row = tile_row;
3115 next_job_info->tile_col = tile_col;
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003116 next_job_info->mi_row = dec_row_mt_sync->mi_rows_decode_started +
3117 tile_data->tile_info.mi_row_start;
Deepa K G36ff5b12018-07-06 18:22:50 +05303118
3119 dec_row_mt_sync->num_threads_working++;
3120 dec_row_mt_sync->mi_rows_decode_started += sb_mi_size;
3121 frame_row_mt_info->mi_rows_decode_started += sb_mi_size;
Wan-Teh Chang516badf2018-09-20 13:54:01 -07003122 assert(frame_row_mt_info->mi_rows_parse_done >=
3123 frame_row_mt_info->mi_rows_decode_started);
3124#if CONFIG_MULTITHREAD
3125 if (frame_row_mt_info->mi_rows_decode_started ==
3126 frame_row_mt_info->mi_rows_to_decode) {
3127 pthread_cond_broadcast(pbi->row_mt_cond_);
3128 }
3129#endif
Deepa K G36ff5b12018-07-06 18:22:50 +05303130
3131 return 1;
3132}
3133
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07003134static inline void signal_parse_sb_row_done(AV1Decoder *const pbi,
Deepa K G36ff5b12018-07-06 18:22:50 +05303135 TileDataDec *const tile_data,
3136 const int sb_mi_size) {
3137 AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3138#if CONFIG_MULTITHREAD
3139 pthread_mutex_lock(pbi->row_mt_mutex_);
3140#endif
Wan-Teh Chang516badf2018-09-20 13:54:01 -07003141 assert(frame_row_mt_info->mi_rows_parse_done >=
3142 frame_row_mt_info->mi_rows_decode_started);
Deepa K G36ff5b12018-07-06 18:22:50 +05303143 tile_data->dec_row_mt_sync.mi_rows_parse_done += sb_mi_size;
3144 frame_row_mt_info->mi_rows_parse_done += sb_mi_size;
3145#if CONFIG_MULTITHREAD
Wan-Teh Chang516badf2018-09-20 13:54:01 -07003146 // A new decode job is available. Wake up one worker thread to handle the
3147 // new decode job.
3148 // NOTE: This assumes we bump mi_rows_parse_done and mi_rows_decode_started
3149 // by the same increment (sb_mi_size).
3150 pthread_cond_signal(pbi->row_mt_cond_);
Deepa K G36ff5b12018-07-06 18:22:50 +05303151 pthread_mutex_unlock(pbi->row_mt_mutex_);
3152#endif
3153}
3154
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003155// This function is very similar to decode_tile(). It would be good to figure
3156// out how to share code.
Wan-Teh Chang94534432024-08-08 20:29:57 -07003157static inline void parse_tile_row_mt(AV1Decoder *pbi, ThreadData *const td,
3158 TileDataDec *const tile_data) {
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003159 AV1_COMMON *const cm = &pbi->common;
Tarundeep Singh4243e622021-04-20 16:10:22 +05303160 const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003161 const int num_planes = av1_num_planes(cm);
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003162 const TileInfo *const tile_info = &tile_data->tile_info;
3163 int tile_row = tile_info->tile_row;
Urvang Joshibac1dea2020-04-20 11:37:15 -07003164 DecoderCodingBlock *const dcb = &td->dcb;
3165 MACROBLOCKD *const xd = &dcb->xd;
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003166
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003167 av1_zero_above_context(cm, xd, tile_info->mi_col_start, tile_info->mi_col_end,
Urvang Joshibac1dea2020-04-20 11:37:15 -07003168 tile_row);
3169 av1_reset_loop_filter_delta(xd, num_planes);
3170 av1_reset_loop_restoration(xd, num_planes);
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003171
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003172 for (int mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
Tarundeep Singh4243e622021-04-20 16:10:22 +05303173 mi_row += cm->seq_params->mib_size) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07003174 av1_zero_left_context(xd);
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003175
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003176 for (int mi_col = tile_info->mi_col_start; mi_col < tile_info->mi_col_end;
Tarundeep Singh4243e622021-04-20 16:10:22 +05303177 mi_col += cm->seq_params->mib_size) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07003178 set_cb_buffer(pbi, dcb, pbi->cb_buffer_base, num_planes, mi_row, mi_col);
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003179
3180 // Bit-stream parsing of the superblock
3181 decode_partition(pbi, td, mi_row, mi_col, td->bit_reader,
Tarundeep Singh4243e622021-04-20 16:10:22 +05303182 cm->seq_params->sb_size, 0x1);
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003183
3184 if (aom_reader_has_overflowed(td->bit_reader)) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07003185 aom_merge_corrupted_flag(&dcb->corrupted, 1);
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003186 return;
3187 }
3188 }
3189 signal_parse_sb_row_done(pbi, tile_data, sb_mi_size);
3190 }
3191
3192 int corrupted =
3193 (check_trailing_bits_after_symbol_coder(td->bit_reader)) ? 1 : 0;
Urvang Joshibac1dea2020-04-20 11:37:15 -07003194 aom_merge_corrupted_flag(&dcb->corrupted, corrupted);
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003195}
3196
Deepa K Gec1987a2018-07-03 14:22:27 +05303197static int row_mt_worker_hook(void *arg1, void *arg2) {
3198 DecWorkerData *const thread_data = (DecWorkerData *)arg1;
3199 AV1Decoder *const pbi = (AV1Decoder *)arg2;
Deepa K Gec1987a2018-07-03 14:22:27 +05303200 ThreadData *const td = thread_data->td;
3201 uint8_t allow_update_cdf;
Deepa K G36ff5b12018-07-06 18:22:50 +05303202 AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
Urvang Joshibac1dea2020-04-20 11:37:15 -07003203 td->dcb.corrupted = 0;
Deepa K Gec1987a2018-07-03 14:22:27 +05303204
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07003205 // The jmp_buf is valid only for the duration of the function that calls
3206 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
3207 // before it returns.
Wan-Teh Changf3ac6512018-07-18 17:18:53 -07003208 if (setjmp(thread_data->error_info.jmp)) {
3209 thread_data->error_info.setjmp = 0;
Urvang Joshibac1dea2020-04-20 11:37:15 -07003210 thread_data->td->dcb.corrupted = 1;
Deepa K G36ff5b12018-07-06 18:22:50 +05303211#if CONFIG_MULTITHREAD
3212 pthread_mutex_lock(pbi->row_mt_mutex_);
3213#endif
3214 frame_row_mt_info->row_mt_exit = 1;
Mudassir Galaganath4245c192023-12-14 12:36:20 +05303215#if CONFIG_MULTITHREAD
3216 pthread_cond_broadcast(pbi->row_mt_cond_);
3217 pthread_mutex_unlock(pbi->row_mt_mutex_);
3218#endif
venkat sanampudidd5066d2022-02-14 21:17:02 +05303219 // If any SB row (erroneous row) processed by a thread encounters an
3220 // internal error, there is a need to indicate other threads that decoding
3221 // of the erroneous row is complete. This ensures that other threads which
3222 // wait upon the completion of SB's present in erroneous row are not waiting
3223 // indefinitely.
3224 signal_decoding_done_for_erroneous_row(pbi, &thread_data->td->dcb.xd);
Deepa K Gec1987a2018-07-03 14:22:27 +05303225 return 0;
3226 }
Wan-Teh Changf3ac6512018-07-18 17:18:53 -07003227 thread_data->error_info.setjmp = 1;
Deepa K Gec1987a2018-07-03 14:22:27 +05303228
venkat sanampudidd5066d2022-02-14 21:17:02 +05303229 AV1_COMMON *cm = &pbi->common;
Urvang Joshi54ffae72020-03-23 13:37:10 -07003230 allow_update_cdf = cm->tiles.large_scale ? 0 : 1;
Urvang Joshib6409e92020-03-23 11:23:27 -07003231 allow_update_cdf = allow_update_cdf && !cm->features.disable_cdf_update;
Deepa K Gec1987a2018-07-03 14:22:27 +05303232
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003233 set_decode_func_pointers(td, 0x1);
3234
Urvang Joshi54ffae72020-03-23 13:37:10 -07003235 assert(cm->tiles.cols > 0);
Urvang Joshibac1dea2020-04-20 11:37:15 -07003236 while (!td->dcb.corrupted) {
Deepa K Gec1987a2018-07-03 14:22:27 +05303237 TileJobsDec *cur_job_info = get_dec_job_info(&pbi->tile_mt_info);
3238
Wan-Teh Chang30e8ddb2018-09-19 18:17:32 -07003239 if (cur_job_info != NULL) {
Deepa K Gec1987a2018-07-03 14:22:27 +05303240 const TileBufferDec *const tile_buffer = cur_job_info->tile_buffer;
3241 TileDataDec *const tile_data = cur_job_info->tile_data;
3242 tile_worker_hook_init(pbi, thread_data, tile_buffer, tile_data,
3243 allow_update_cdf);
Cherma Rajan A61661692019-01-22 19:35:27 +05303244#if CONFIG_MULTITHREAD
3245 pthread_mutex_lock(pbi->row_mt_mutex_);
3246#endif
3247 tile_data->dec_row_mt_sync.num_threads_working++;
3248#if CONFIG_MULTITHREAD
3249 pthread_mutex_unlock(pbi->row_mt_mutex_);
3250#endif
Deepa K Gec1987a2018-07-03 14:22:27 +05303251 // decode tile
Wan-Teh Changfe9ce8d2018-09-24 18:39:56 -07003252 parse_tile_row_mt(pbi, td, tile_data);
Cherma Rajan A61661692019-01-22 19:35:27 +05303253#if CONFIG_MULTITHREAD
3254 pthread_mutex_lock(pbi->row_mt_mutex_);
3255#endif
3256 tile_data->dec_row_mt_sync.num_threads_working--;
3257#if CONFIG_MULTITHREAD
3258 pthread_mutex_unlock(pbi->row_mt_mutex_);
3259#endif
Deepa K Gec1987a2018-07-03 14:22:27 +05303260 } else {
3261 break;
3262 }
3263 }
Deepa K G36ff5b12018-07-06 18:22:50 +05303264
Urvang Joshibac1dea2020-04-20 11:37:15 -07003265 if (td->dcb.corrupted) {
Wan-Teh Chang82e0ef62018-09-19 15:32:56 -07003266 thread_data->error_info.setjmp = 0;
3267#if CONFIG_MULTITHREAD
3268 pthread_mutex_lock(pbi->row_mt_mutex_);
3269#endif
3270 frame_row_mt_info->row_mt_exit = 1;
3271#if CONFIG_MULTITHREAD
3272 pthread_cond_broadcast(pbi->row_mt_cond_);
3273 pthread_mutex_unlock(pbi->row_mt_mutex_);
3274#endif
3275 return 0;
3276 }
3277
Deepa K G36ff5b12018-07-06 18:22:50 +05303278 set_decode_func_pointers(td, 0x2);
3279
3280 while (1) {
3281 AV1DecRowMTJobInfo next_job_info;
3282 int end_of_frame = 0;
3283
3284#if CONFIG_MULTITHREAD
3285 pthread_mutex_lock(pbi->row_mt_mutex_);
3286#endif
3287 while (!get_next_job_info(pbi, &next_job_info, &end_of_frame)) {
3288#if CONFIG_MULTITHREAD
3289 pthread_cond_wait(pbi->row_mt_cond_, pbi->row_mt_mutex_);
3290#endif
3291 }
3292#if CONFIG_MULTITHREAD
3293 pthread_mutex_unlock(pbi->row_mt_mutex_);
3294#endif
3295
3296 if (end_of_frame) break;
3297
3298 int tile_row = next_job_info.tile_row;
3299 int tile_col = next_job_info.tile_col;
3300 int mi_row = next_job_info.mi_row;
3301
3302 TileDataDec *tile_data =
Urvang Joshi54ffae72020-03-23 13:37:10 -07003303 pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
Deepa K G36ff5b12018-07-06 18:22:50 +05303304 AV1DecRowMTSync *dec_row_mt_sync = &tile_data->dec_row_mt_sync;
Deepa K G36ff5b12018-07-06 18:22:50 +05303305
Urvang Joshibac1dea2020-04-20 11:37:15 -07003306 av1_tile_init(&td->dcb.xd.tile, cm, tile_row, tile_col);
3307 av1_init_macroblockd(cm, &td->dcb.xd);
3308 td->dcb.xd.error_info = &thread_data->error_info;
Deepa K G36ff5b12018-07-06 18:22:50 +05303309
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003310 decode_tile_sb_row(pbi, td, &tile_data->tile_info, mi_row);
Deepa K G36ff5b12018-07-06 18:22:50 +05303311
3312#if CONFIG_MULTITHREAD
3313 pthread_mutex_lock(pbi->row_mt_mutex_);
3314#endif
3315 dec_row_mt_sync->num_threads_working--;
3316#if CONFIG_MULTITHREAD
3317 pthread_mutex_unlock(pbi->row_mt_mutex_);
3318#endif
3319 }
Wan-Teh Chang9f2e3312018-07-06 11:00:36 -07003320 thread_data->error_info.setjmp = 0;
Urvang Joshibac1dea2020-04-20 11:37:15 -07003321 return !td->dcb.corrupted;
Deepa K Gec1987a2018-07-03 14:22:27 +05303322}
3323
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303324// sorts in descending order
3325static int compare_tile_buffers(const void *a, const void *b) {
3326 const TileJobsDec *const buf1 = (const TileJobsDec *)a;
3327 const TileJobsDec *const buf2 = (const TileJobsDec *)b;
Ravi Chaudhary09693ea2018-06-20 19:23:47 +05303328 return (((int)buf2->tile_buffer->size) - ((int)buf1->tile_buffer->size));
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303329}
3330
Wan-Teh Chang94534432024-08-08 20:29:57 -07003331static inline void enqueue_tile_jobs(AV1Decoder *pbi, AV1_COMMON *cm,
3332 int tile_rows_start, int tile_rows_end,
3333 int tile_cols_start, int tile_cols_end,
3334 int start_tile, int end_tile) {
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303335 AV1DecTileMT *tile_mt_info = &pbi->tile_mt_info;
3336 TileJobsDec *tile_job_queue = tile_mt_info->job_queue;
3337 tile_mt_info->jobs_enqueued = 0;
3338 tile_mt_info->jobs_dequeued = 0;
3339
3340 for (int row = tile_rows_start; row < tile_rows_end; row++) {
3341 for (int col = tile_cols_start; col < tile_cols_end; col++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07003342 if (row * cm->tiles.cols + col < start_tile ||
3343 row * cm->tiles.cols + col > end_tile)
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303344 continue;
3345 tile_job_queue->tile_buffer = &pbi->tile_buffers[row][col];
Urvang Joshi54ffae72020-03-23 13:37:10 -07003346 tile_job_queue->tile_data = pbi->tile_data + row * cm->tiles.cols + col;
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303347 tile_job_queue++;
3348 tile_mt_info->jobs_enqueued++;
3349 }
3350 }
3351}
3352
Wan-Teh Chang94534432024-08-08 20:29:57 -07003353static inline void alloc_dec_jobs(AV1DecTileMT *tile_mt_info, AV1_COMMON *cm,
3354 int tile_rows, int tile_cols) {
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303355 tile_mt_info->alloc_tile_rows = tile_rows;
3356 tile_mt_info->alloc_tile_cols = tile_cols;
3357 int num_tiles = tile_rows * tile_cols;
3358#if CONFIG_MULTITHREAD
3359 {
3360 CHECK_MEM_ERROR(cm, tile_mt_info->job_mutex,
3361 aom_malloc(sizeof(*tile_mt_info->job_mutex) * num_tiles));
3362
3363 for (int i = 0; i < num_tiles; i++) {
3364 pthread_mutex_init(&tile_mt_info->job_mutex[i], NULL);
3365 }
3366 }
3367#endif
3368 CHECK_MEM_ERROR(cm, tile_mt_info->job_queue,
3369 aom_malloc(sizeof(*tile_mt_info->job_queue) * num_tiles));
3370}
3371
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -07003372void av1_free_mc_tmp_buf(ThreadData *thread_data) {
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303373 int ref;
3374 for (ref = 0; ref < 2; ref++) {
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -07003375 if (thread_data->mc_buf_use_highbd)
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303376 aom_free(CONVERT_TO_SHORTPTR(thread_data->mc_buf[ref]));
3377 else
3378 aom_free(thread_data->mc_buf[ref]);
3379 thread_data->mc_buf[ref] = NULL;
3380 }
Ravi Chaudhary4bce2292018-06-11 15:03:44 +05303381 thread_data->mc_buf_size = 0;
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -07003382 thread_data->mc_buf_use_highbd = 0;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003383
3384 aom_free(thread_data->tmp_conv_dst);
3385 thread_data->tmp_conv_dst = NULL;
Chethan Kumar R Ea494e2b2021-03-18 17:19:44 +05303386 aom_free(thread_data->seg_mask);
3387 thread_data->seg_mask = NULL;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003388 for (int i = 0; i < 2; ++i) {
3389 aom_free(thread_data->tmp_obmc_bufs[i]);
3390 thread_data->tmp_obmc_bufs[i] = NULL;
3391 }
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303392}
3393
Wan-Teh Chang94534432024-08-08 20:29:57 -07003394static inline void allocate_mc_tmp_buf(AV1_COMMON *const cm,
3395 ThreadData *thread_data, int buf_size,
3396 int use_highbd) {
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303397 for (int ref = 0; ref < 2; ref++) {
Sachin Kumar Gargda739402020-06-05 10:40:07 +05303398 // The mc_buf/hbd_mc_buf must be zeroed to fix a intermittent valgrind error
3399 // 'Conditional jump or move depends on uninitialised value' from the loop
3400 // filter. Uninitialized reads in convolve function (e.g. horiz_4tap path in
3401 // av1_convolve_2d_sr_avx2()) from mc_buf/hbd_mc_buf are seen to be the
3402 // potential reason for this issue.
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303403 if (use_highbd) {
3404 uint16_t *hbd_mc_buf;
3405 CHECK_MEM_ERROR(cm, hbd_mc_buf, (uint16_t *)aom_memalign(16, buf_size));
Sachin Kumar Gargda739402020-06-05 10:40:07 +05303406 memset(hbd_mc_buf, 0, buf_size);
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303407 thread_data->mc_buf[ref] = CONVERT_TO_BYTEPTR(hbd_mc_buf);
3408 } else {
3409 CHECK_MEM_ERROR(cm, thread_data->mc_buf[ref],
3410 (uint8_t *)aom_memalign(16, buf_size));
Sachin Kumar Gargda739402020-06-05 10:40:07 +05303411 memset(thread_data->mc_buf[ref], 0, buf_size);
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303412 }
3413 }
3414 thread_data->mc_buf_size = buf_size;
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -07003415 thread_data->mc_buf_use_highbd = use_highbd;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003416
3417 CHECK_MEM_ERROR(cm, thread_data->tmp_conv_dst,
3418 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
3419 sizeof(*thread_data->tmp_conv_dst)));
Chethan Kumar R Ea494e2b2021-03-18 17:19:44 +05303420 CHECK_MEM_ERROR(cm, thread_data->seg_mask,
3421 (uint8_t *)aom_memalign(
3422 16, 2 * MAX_SB_SQUARE * sizeof(*thread_data->seg_mask)));
3423
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003424 for (int i = 0; i < 2; ++i) {
3425 CHECK_MEM_ERROR(
3426 cm, thread_data->tmp_obmc_bufs[i],
3427 aom_memalign(16, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
3428 sizeof(*thread_data->tmp_obmc_bufs[i])));
3429 }
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303430}
3431
Wan-Teh Chang94534432024-08-08 20:29:57 -07003432static inline void reset_dec_workers(AV1Decoder *pbi, AVxWorkerHook worker_hook,
3433 int num_workers) {
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303434 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3435
3436 // Reset tile decoding hook
3437 for (int worker_idx = 0; worker_idx < num_workers; ++worker_idx) {
3438 AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3439 DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
Urvang Joshibac1dea2020-04-20 11:37:15 -07003440 thread_data->td->dcb = pbi->dcb;
3441 thread_data->td->dcb.corrupted = 0;
3442 thread_data->td->dcb.mc_buf[0] = thread_data->td->mc_buf[0];
3443 thread_data->td->dcb.mc_buf[1] = thread_data->td->mc_buf[1];
3444 thread_data->td->dcb.xd.tmp_conv_dst = thread_data->td->tmp_conv_dst;
Chethan Kumar R Ea494e2b2021-03-18 17:19:44 +05303445 if (worker_idx)
3446 thread_data->td->dcb.xd.seg_mask = thread_data->td->seg_mask;
Urvang Joshie58f6eca2018-09-10 15:10:12 -07003447 for (int j = 0; j < 2; ++j) {
Urvang Joshibac1dea2020-04-20 11:37:15 -07003448 thread_data->td->dcb.xd.tmp_obmc_bufs[j] =
3449 thread_data->td->tmp_obmc_bufs[j];
Urvang Joshie58f6eca2018-09-10 15:10:12 -07003450 }
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303451 winterface->sync(worker);
3452
Deepa K Gec1987a2018-07-03 14:22:27 +05303453 worker->hook = worker_hook;
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303454 worker->data1 = thread_data;
3455 worker->data2 = pbi;
3456 }
3457#if CONFIG_ACCOUNTING
3458 if (pbi->acct_enabled) {
3459 aom_accounting_reset(&pbi->accounting);
3460 }
3461#endif
3462}
3463
Wan-Teh Chang94534432024-08-08 20:29:57 -07003464static inline void launch_dec_workers(AV1Decoder *pbi, const uint8_t *data_end,
3465 int num_workers) {
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303466 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3467
Deepa K G57867742021-01-19 12:52:41 -08003468 for (int worker_idx = num_workers - 1; worker_idx >= 0; --worker_idx) {
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303469 AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3470 DecWorkerData *const thread_data = (DecWorkerData *)worker->data1;
3471
3472 thread_data->data_end = data_end;
3473
3474 worker->had_error = 0;
Deepa K G57867742021-01-19 12:52:41 -08003475 if (worker_idx == 0) {
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303476 winterface->execute(worker);
3477 } else {
3478 winterface->launch(worker);
3479 }
3480 }
3481}
3482
Wan-Teh Chang94534432024-08-08 20:29:57 -07003483static inline void sync_dec_workers(AV1Decoder *pbi, int num_workers) {
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303484 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
3485 int corrupted = 0;
3486
3487 for (int worker_idx = num_workers; worker_idx > 0; --worker_idx) {
3488 AVxWorker *const worker = &pbi->tile_workers[worker_idx - 1];
3489 aom_merge_corrupted_flag(&corrupted, !winterface->sync(worker));
3490 }
3491
Urvang Joshibac1dea2020-04-20 11:37:15 -07003492 pbi->dcb.corrupted = corrupted;
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303493}
3494
Wan-Teh Chang94534432024-08-08 20:29:57 -07003495static inline void decode_mt_init(AV1Decoder *pbi) {
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303496 AV1_COMMON *const cm = &pbi->common;
3497 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303498 int worker_idx;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303499
3500 // Create workers and thread_data
3501 if (pbi->num_workers == 0) {
3502 const int num_threads = pbi->max_threads;
3503 CHECK_MEM_ERROR(cm, pbi->tile_workers,
3504 aom_malloc(num_threads * sizeof(*pbi->tile_workers)));
3505 CHECK_MEM_ERROR(cm, pbi->thread_data,
James Zerna3b61c12022-12-06 15:29:22 -08003506 aom_calloc(num_threads, sizeof(*pbi->thread_data)));
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303507
3508 for (worker_idx = 0; worker_idx < num_threads; ++worker_idx) {
3509 AVxWorker *const worker = &pbi->tile_workers[worker_idx];
3510 DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303511
3512 winterface->init(worker);
Wan-Teh Chang4d29ee82018-09-20 10:07:52 -07003513 worker->thread_name = "aom tile worker";
Deepa K G57867742021-01-19 12:52:41 -08003514 if (worker_idx != 0 && !winterface->reset(worker)) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303515 aom_internal_error(&pbi->error, AOM_CODEC_ERROR,
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303516 "Tile decoder thread creation failed");
3517 }
James Zerna3b61c12022-12-06 15:29:22 -08003518 ++pbi->num_workers;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303519
Deepa K G57867742021-01-19 12:52:41 -08003520 if (worker_idx != 0) {
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303521 // Allocate thread data.
3522 CHECK_MEM_ERROR(cm, thread_data->td,
3523 aom_memalign(32, sizeof(*thread_data->td)));
3524 av1_zero(*thread_data->td);
3525 } else {
3526 // Main thread acts as a worker and uses the thread data in pbi
3527 thread_data->td = &pbi->td;
3528 }
Wan-Teh Chang61f23532018-07-12 18:20:02 -07003529 thread_data->error_info.error_code = AOM_CODEC_OK;
3530 thread_data->error_info.setjmp = 0;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303531 }
3532 }
Tarundeep Singh4243e622021-04-20 16:10:22 +05303533 const int use_highbd = cm->seq_params->use_highbitdepth;
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303534 const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
Deepa K G57867742021-01-19 12:52:41 -08003535 for (worker_idx = 1; worker_idx < pbi->max_threads; ++worker_idx) {
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303536 DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
3537 if (thread_data->td->mc_buf_size != buf_size) {
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -07003538 av1_free_mc_tmp_buf(thread_data->td);
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05303539 allocate_mc_tmp_buf(cm, thread_data->td, buf_size, use_highbd);
3540 }
3541 }
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303542}
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303543
Wan-Teh Chang94534432024-08-08 20:29:57 -07003544static inline void tile_mt_queue(AV1Decoder *pbi, int tile_cols, int tile_rows,
3545 int tile_rows_start, int tile_rows_end,
3546 int tile_cols_start, int tile_cols_end,
3547 int start_tile, int end_tile) {
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303548 AV1_COMMON *const cm = &pbi->common;
3549 if (pbi->tile_mt_info.alloc_tile_cols != tile_cols ||
3550 pbi->tile_mt_info.alloc_tile_rows != tile_rows) {
3551 av1_dealloc_dec_jobs(&pbi->tile_mt_info);
3552 alloc_dec_jobs(&pbi->tile_mt_info, cm, tile_rows, tile_cols);
3553 }
3554 enqueue_tile_jobs(pbi, cm, tile_rows_start, tile_rows_end, tile_cols_start,
3555 tile_cols_end, start_tile, end_tile);
3556 qsort(pbi->tile_mt_info.job_queue, pbi->tile_mt_info.jobs_enqueued,
3557 sizeof(pbi->tile_mt_info.job_queue[0]), compare_tile_buffers);
3558}
3559
3560static const uint8_t *decode_tiles_mt(AV1Decoder *pbi, const uint8_t *data,
3561 const uint8_t *data_end, int start_tile,
3562 int end_tile) {
3563 AV1_COMMON *const cm = &pbi->common;
Urvang Joshi54ffae72020-03-23 13:37:10 -07003564 CommonTileParams *const tiles = &cm->tiles;
3565 const int tile_cols = tiles->cols;
3566 const int tile_rows = tiles->rows;
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303567 const int n_tiles = tile_cols * tile_rows;
3568 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
3569 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
3570 const int single_row = pbi->dec_tile_row >= 0;
3571 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
3572 const int single_col = pbi->dec_tile_col >= 0;
3573 int tile_rows_start;
3574 int tile_rows_end;
3575 int tile_cols_start;
3576 int tile_cols_end;
3577 int tile_count_tg;
3578 int num_workers;
3579 const uint8_t *raw_data_end = NULL;
3580
Urvang Joshi54ffae72020-03-23 13:37:10 -07003581 if (tiles->large_scale) {
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303582 tile_rows_start = single_row ? dec_tile_row : 0;
3583 tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
3584 tile_cols_start = single_col ? dec_tile_col : 0;
3585 tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3586 } else {
3587 tile_rows_start = 0;
3588 tile_rows_end = tile_rows;
3589 tile_cols_start = 0;
3590 tile_cols_end = tile_cols;
3591 }
3592 tile_count_tg = end_tile - start_tile + 1;
3593 num_workers = AOMMIN(pbi->max_threads, tile_count_tg);
3594
3595 // No tiles to decode.
3596 if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
3597 // First tile is larger than end_tile.
3598 tile_rows_start * tile_cols + tile_cols_start > end_tile ||
3599 // Last tile is smaller than start_tile.
3600 (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
3601 return data;
3602
3603 assert(tile_rows <= MAX_TILE_ROWS);
3604 assert(tile_cols <= MAX_TILE_COLS);
3605 assert(tile_count_tg > 0);
3606 assert(num_workers > 0);
3607 assert(start_tile <= end_tile);
3608 assert(start_tile >= 0 && end_tile < n_tiles);
3609
3610 decode_mt_init(pbi);
3611
3612 // get tile size in tile group
Yunqing Wang3a74b252018-05-03 15:24:07 -07003613#if EXT_TILE_DEBUG
Urvang Joshi54ffae72020-03-23 13:37:10 -07003614 if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
3615 if (tiles->large_scale)
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07003616 raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303617 else
Yunqing Wang3a74b252018-05-03 15:24:07 -07003618#endif // EXT_TILE_DEBUG
Cheng Chena98e55e2018-06-12 23:14:19 -07003619 get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303620
3621 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Deepa K Gcebe0782018-07-05 11:27:40 +05303622 decoder_alloc_tile_data(pbi, n_tiles);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303623 }
Chethan Kumar R Ea494e2b2021-03-18 17:19:44 +05303624 if (pbi->dcb.xd.seg_mask == NULL)
3625 CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
3626 (uint8_t *)aom_memalign(
3627 16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303628
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303629 for (int row = 0; row < tile_rows; row++) {
3630 for (int col = 0; col < tile_cols; col++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07003631 TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303632 av1_tile_init(&tile_data->tile_info, cm, row, col);
3633 }
3634 }
3635
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303636 tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
3637 tile_cols_start, tile_cols_end, start_tile, end_tile);
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +05303638
Deepa K Gec1987a2018-07-03 14:22:27 +05303639 reset_dec_workers(pbi, tile_worker_hook, num_workers);
3640 launch_dec_workers(pbi, data_end, num_workers);
3641 sync_dec_workers(pbi, num_workers);
3642
Urvang Joshibac1dea2020-04-20 11:37:15 -07003643 if (pbi->dcb.corrupted)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303644 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Deepa K Gec1987a2018-07-03 14:22:27 +05303645 "Failed to decode tile data");
3646
Urvang Joshi54ffae72020-03-23 13:37:10 -07003647 if (tiles->large_scale) {
Deepa K Gec1987a2018-07-03 14:22:27 +05303648 if (n_tiles == 1) {
3649 // Find the end of the single tile buffer
3650 return aom_reader_find_end(&pbi->tile_data->bit_reader);
3651 }
3652 // Return the end of the last tile buffer
3653 return raw_data_end;
3654 }
3655 TileDataDec *const tile_data = pbi->tile_data + end_tile;
3656
3657 return aom_reader_find_end(&tile_data->bit_reader);
3658}
3659
Wan-Teh Chang94534432024-08-08 20:29:57 -07003660static inline void dec_alloc_cb_buf(AV1Decoder *pbi) {
Deepa K Gec1987a2018-07-03 14:22:27 +05303661 AV1_COMMON *const cm = &pbi->common;
Tarundeep Singh4243e622021-04-20 16:10:22 +05303662 int size = ((cm->mi_params.mi_rows >> cm->seq_params->mib_size_log2) + 1) *
3663 ((cm->mi_params.mi_cols >> cm->seq_params->mib_size_log2) + 1);
Deepa K Gec1987a2018-07-03 14:22:27 +05303664
3665 if (pbi->cb_buffer_alloc_size < size) {
3666 av1_dec_free_cb_buf(pbi);
3667 CHECK_MEM_ERROR(cm, pbi->cb_buffer_base,
3668 aom_memalign(32, sizeof(*pbi->cb_buffer_base) * size));
wenyao.liu00c80e62018-12-28 10:42:26 +08003669 memset(pbi->cb_buffer_base, 0, sizeof(*pbi->cb_buffer_base) * size);
Deepa K Gec1987a2018-07-03 14:22:27 +05303670 pbi->cb_buffer_alloc_size = size;
3671 }
3672}
3673
Wan-Teh Chang94534432024-08-08 20:29:57 -07003674static inline void row_mt_frame_init(AV1Decoder *pbi, int tile_rows_start,
3675 int tile_rows_end, int tile_cols_start,
3676 int tile_cols_end, int start_tile,
3677 int end_tile, int max_sb_rows) {
Deepa K G36ff5b12018-07-06 18:22:50 +05303678 AV1_COMMON *const cm = &pbi->common;
3679 AV1DecRowMTInfo *frame_row_mt_info = &pbi->frame_row_mt_info;
3680
3681 frame_row_mt_info->tile_rows_start = tile_rows_start;
3682 frame_row_mt_info->tile_rows_end = tile_rows_end;
3683 frame_row_mt_info->tile_cols_start = tile_cols_start;
3684 frame_row_mt_info->tile_cols_end = tile_cols_end;
3685 frame_row_mt_info->start_tile = start_tile;
3686 frame_row_mt_info->end_tile = end_tile;
3687 frame_row_mt_info->mi_rows_to_decode = 0;
3688 frame_row_mt_info->mi_rows_parse_done = 0;
3689 frame_row_mt_info->mi_rows_decode_started = 0;
3690 frame_row_mt_info->row_mt_exit = 0;
3691
3692 for (int tile_row = tile_rows_start; tile_row < tile_rows_end; ++tile_row) {
3693 for (int tile_col = tile_cols_start; tile_col < tile_cols_end; ++tile_col) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07003694 if (tile_row * cm->tiles.cols + tile_col < start_tile ||
3695 tile_row * cm->tiles.cols + tile_col > end_tile)
Deepa K G36ff5b12018-07-06 18:22:50 +05303696 continue;
3697
3698 TileDataDec *const tile_data =
Urvang Joshi54ffae72020-03-23 13:37:10 -07003699 pbi->tile_data + tile_row * cm->tiles.cols + tile_col;
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003700 const TileInfo *const tile_info = &tile_data->tile_info;
Deepa K G36ff5b12018-07-06 18:22:50 +05303701
3702 tile_data->dec_row_mt_sync.mi_rows_parse_done = 0;
3703 tile_data->dec_row_mt_sync.mi_rows_decode_started = 0;
3704 tile_data->dec_row_mt_sync.num_threads_working = 0;
3705 tile_data->dec_row_mt_sync.mi_rows =
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003706 ALIGN_POWER_OF_TWO(tile_info->mi_row_end - tile_info->mi_row_start,
Tarundeep Singh4243e622021-04-20 16:10:22 +05303707 cm->seq_params->mib_size_log2);
Deepa K G36ff5b12018-07-06 18:22:50 +05303708 tile_data->dec_row_mt_sync.mi_cols =
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003709 ALIGN_POWER_OF_TWO(tile_info->mi_col_end - tile_info->mi_col_start,
Tarundeep Singh4243e622021-04-20 16:10:22 +05303710 cm->seq_params->mib_size_log2);
Jayasanker J34ec50a2022-07-01 19:00:02 +05303711 tile_data->dec_row_mt_sync.intrabc_extra_top_right_sb_delay =
3712 av1_get_intrabc_extra_top_right_sb_delay(cm);
Deepa K G36ff5b12018-07-06 18:22:50 +05303713
3714 frame_row_mt_info->mi_rows_to_decode +=
3715 tile_data->dec_row_mt_sync.mi_rows;
3716
3717 // Initialize cur_sb_col to -1 for all SB rows.
3718 memset(tile_data->dec_row_mt_sync.cur_sb_col, -1,
3719 sizeof(*tile_data->dec_row_mt_sync.cur_sb_col) * max_sb_rows);
3720 }
3721 }
3722
3723#if CONFIG_MULTITHREAD
3724 if (pbi->row_mt_mutex_ == NULL) {
3725 CHECK_MEM_ERROR(cm, pbi->row_mt_mutex_,
3726 aom_malloc(sizeof(*(pbi->row_mt_mutex_))));
3727 if (pbi->row_mt_mutex_) {
3728 pthread_mutex_init(pbi->row_mt_mutex_, NULL);
3729 }
3730 }
3731
3732 if (pbi->row_mt_cond_ == NULL) {
3733 CHECK_MEM_ERROR(cm, pbi->row_mt_cond_,
3734 aom_malloc(sizeof(*(pbi->row_mt_cond_))));
3735 if (pbi->row_mt_cond_) {
3736 pthread_cond_init(pbi->row_mt_cond_, NULL);
3737 }
3738 }
3739#endif
3740}
3741
Deepa K Gec1987a2018-07-03 14:22:27 +05303742static const uint8_t *decode_tiles_row_mt(AV1Decoder *pbi, const uint8_t *data,
3743 const uint8_t *data_end,
3744 int start_tile, int end_tile) {
3745 AV1_COMMON *const cm = &pbi->common;
Urvang Joshi54ffae72020-03-23 13:37:10 -07003746 CommonTileParams *const tiles = &cm->tiles;
3747 const int tile_cols = tiles->cols;
3748 const int tile_rows = tiles->rows;
Deepa K Gec1987a2018-07-03 14:22:27 +05303749 const int n_tiles = tile_cols * tile_rows;
3750 TileBufferDec(*const tile_buffers)[MAX_TILE_COLS] = pbi->tile_buffers;
3751 const int dec_tile_row = AOMMIN(pbi->dec_tile_row, tile_rows);
3752 const int single_row = pbi->dec_tile_row >= 0;
3753 const int dec_tile_col = AOMMIN(pbi->dec_tile_col, tile_cols);
3754 const int single_col = pbi->dec_tile_col >= 0;
3755 int tile_rows_start;
3756 int tile_rows_end;
3757 int tile_cols_start;
3758 int tile_cols_end;
3759 int tile_count_tg;
Cherma Rajan A61661692019-01-22 19:35:27 +05303760 int num_workers = 0;
3761 int max_threads;
Deepa K Gec1987a2018-07-03 14:22:27 +05303762 const uint8_t *raw_data_end = NULL;
Deepa K Gcebe0782018-07-05 11:27:40 +05303763 int max_sb_rows = 0;
Deepa K Gec1987a2018-07-03 14:22:27 +05303764
Urvang Joshi54ffae72020-03-23 13:37:10 -07003765 if (tiles->large_scale) {
Deepa K Gec1987a2018-07-03 14:22:27 +05303766 tile_rows_start = single_row ? dec_tile_row : 0;
3767 tile_rows_end = single_row ? dec_tile_row + 1 : tile_rows;
3768 tile_cols_start = single_col ? dec_tile_col : 0;
3769 tile_cols_end = single_col ? tile_cols_start + 1 : tile_cols;
3770 } else {
3771 tile_rows_start = 0;
3772 tile_rows_end = tile_rows;
3773 tile_cols_start = 0;
3774 tile_cols_end = tile_cols;
3775 }
3776 tile_count_tg = end_tile - start_tile + 1;
Cherma Rajan A61661692019-01-22 19:35:27 +05303777 max_threads = pbi->max_threads;
Deepa K Gec1987a2018-07-03 14:22:27 +05303778
3779 // No tiles to decode.
3780 if (tile_rows_end <= tile_rows_start || tile_cols_end <= tile_cols_start ||
3781 // First tile is larger than end_tile.
3782 tile_rows_start * tile_cols + tile_cols_start > end_tile ||
3783 // Last tile is smaller than start_tile.
3784 (tile_rows_end - 1) * tile_cols + tile_cols_end - 1 < start_tile)
3785 return data;
3786
3787 assert(tile_rows <= MAX_TILE_ROWS);
3788 assert(tile_cols <= MAX_TILE_COLS);
3789 assert(tile_count_tg > 0);
Cherma Rajan A61661692019-01-22 19:35:27 +05303790 assert(max_threads > 0);
Deepa K Gec1987a2018-07-03 14:22:27 +05303791 assert(start_tile <= end_tile);
3792 assert(start_tile >= 0 && end_tile < n_tiles);
3793
Deepa K G36ff5b12018-07-06 18:22:50 +05303794 (void)tile_count_tg;
3795
Deepa K Gec1987a2018-07-03 14:22:27 +05303796 decode_mt_init(pbi);
3797
3798 // get tile size in tile group
3799#if EXT_TILE_DEBUG
Urvang Joshi54ffae72020-03-23 13:37:10 -07003800 if (tiles->large_scale) assert(pbi->ext_tile_debug == 1);
3801 if (tiles->large_scale)
Deepa K Gec1987a2018-07-03 14:22:27 +05303802 raw_data_end = get_ls_tile_buffers(pbi, data, data_end, tile_buffers);
3803 else
3804#endif // EXT_TILE_DEBUG
3805 get_tile_buffers(pbi, data, data_end, tile_buffers, start_tile, end_tile);
3806
3807 if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
Yaowu Xudbe5aaa2020-04-13 08:59:42 -07003808 if (pbi->tile_data != NULL) {
3809 for (int i = 0; i < pbi->allocated_tiles; i++) {
3810 TileDataDec *const tile_data = pbi->tile_data + i;
3811 av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
3812 }
Deepa K Gcebe0782018-07-05 11:27:40 +05303813 }
3814 decoder_alloc_tile_data(pbi, n_tiles);
Deepa K Gec1987a2018-07-03 14:22:27 +05303815 }
Chethan Kumar R Ea494e2b2021-03-18 17:19:44 +05303816 if (pbi->dcb.xd.seg_mask == NULL)
3817 CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
3818 (uint8_t *)aom_memalign(
3819 16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
Deepa K Gec1987a2018-07-03 14:22:27 +05303820
Deepa K Gec1987a2018-07-03 14:22:27 +05303821 for (int row = 0; row < tile_rows; row++) {
3822 for (int col = 0; col < tile_cols; col++) {
Urvang Joshi54ffae72020-03-23 13:37:10 -07003823 TileDataDec *tile_data = pbi->tile_data + row * tiles->cols + col;
Deepa K Gec1987a2018-07-03 14:22:27 +05303824 av1_tile_init(&tile_data->tile_info, cm, row, col);
Deepa K Gcebe0782018-07-05 11:27:40 +05303825
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +05303826 max_sb_rows = AOMMAX(max_sb_rows,
Wan-Teh Chang5350a5e2022-01-29 11:50:17 -08003827 av1_get_sb_rows_in_tile(cm, &tile_data->tile_info));
3828 num_workers += get_max_row_mt_workers_per_tile(cm, &tile_data->tile_info);
Deepa K Gec1987a2018-07-03 14:22:27 +05303829 }
3830 }
Cherma Rajan A61661692019-01-22 19:35:27 +05303831 num_workers = AOMMIN(num_workers, max_threads);
Deepa K Gec1987a2018-07-03 14:22:27 +05303832
Deepa K Gcebe0782018-07-05 11:27:40 +05303833 if (pbi->allocated_row_mt_sync_rows != max_sb_rows) {
3834 for (int i = 0; i < n_tiles; ++i) {
3835 TileDataDec *const tile_data = pbi->tile_data + i;
3836 av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
3837 dec_row_mt_alloc(&tile_data->dec_row_mt_sync, cm, max_sb_rows);
3838 }
3839 pbi->allocated_row_mt_sync_rows = max_sb_rows;
3840 }
3841
Deepa K Gec1987a2018-07-03 14:22:27 +05303842 tile_mt_queue(pbi, tile_cols, tile_rows, tile_rows_start, tile_rows_end,
3843 tile_cols_start, tile_cols_end, start_tile, end_tile);
3844
3845 dec_alloc_cb_buf(pbi);
3846
Deepa K G36ff5b12018-07-06 18:22:50 +05303847 row_mt_frame_init(pbi, tile_rows_start, tile_rows_end, tile_cols_start,
3848 tile_cols_end, start_tile, end_tile, max_sb_rows);
3849
Deepa K Gec1987a2018-07-03 14:22:27 +05303850 reset_dec_workers(pbi, row_mt_worker_hook, num_workers);
Deepa K Gbf4f07a2018-06-28 15:16:19 +05303851 launch_dec_workers(pbi, data_end, num_workers);
3852 sync_dec_workers(pbi, num_workers);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303853
Urvang Joshibac1dea2020-04-20 11:37:15 -07003854 if (pbi->dcb.corrupted)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303855 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Yunqing Wang789d5282018-05-04 17:18:02 -07003856 "Failed to decode tile data");
3857
Urvang Joshi54ffae72020-03-23 13:37:10 -07003858 if (tiles->large_scale) {
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303859 if (n_tiles == 1) {
3860 // Find the end of the single tile buffer
3861 return aom_reader_find_end(&pbi->tile_data->bit_reader);
3862 }
3863 // Return the end of the last tile buffer
Wan-Teh Chang5bab3e42018-05-29 11:35:04 -07003864 return raw_data_end;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303865 }
Cheng Chena98e55e2018-06-12 23:14:19 -07003866 TileDataDec *const tile_data = pbi->tile_data + end_tile;
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05303867
3868 return aom_reader_find_end(&tile_data->bit_reader);
3869}
3870
Wan-Teh Chang94534432024-08-08 20:29:57 -07003871static inline void error_handler(void *data) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003872 AV1_COMMON *const cm = (AV1_COMMON *)data;
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303873 aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME, "Truncated packet");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003874}
3875
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07003876// Reads the high_bitdepth and twelve_bit fields in color_config() and sets
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003877// seq_params->bit_depth based on the values of those fields and
3878// seq_params->profile. Reports errors by calling rb->error_handler() or
3879// aom_internal_error().
Wan-Teh Chang94534432024-08-08 20:29:57 -07003880static inline void read_bitdepth(struct aom_read_bit_buffer *rb,
3881 SequenceHeader *seq_params,
3882 struct aom_internal_error_info *error_info) {
Debargha Mukherjee9dd77792018-03-30 16:31:36 -07003883 const int high_bitdepth = aom_rb_read_bit(rb);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003884 if (seq_params->profile == PROFILE_2 && high_bitdepth) {
Debargha Mukherjee9dd77792018-03-30 16:31:36 -07003885 const int twelve_bit = aom_rb_read_bit(rb);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003886 seq_params->bit_depth = twelve_bit ? AOM_BITS_12 : AOM_BITS_10;
3887 } else if (seq_params->profile <= PROFILE_2) {
3888 seq_params->bit_depth = high_bitdepth ? AOM_BITS_10 : AOM_BITS_8;
Debargha Mukherjee9dd77792018-03-30 16:31:36 -07003889 } else {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003890 aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
Debargha Mukherjee39c0f632018-04-17 13:02:56 -07003891 "Unsupported profile/bit-depth combination");
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003892 }
Wan-Teh Chang0f036982019-09-05 10:00:41 -07003893#if !CONFIG_AV1_HIGHBITDEPTH
3894 if (seq_params->bit_depth > AOM_BITS_8) {
3895 aom_internal_error(error_info, AOM_CODEC_UNSUP_BITSTREAM,
3896 "Bit-depth %d not supported", seq_params->bit_depth);
3897 }
3898#endif
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08003899}
3900
James Zern0d1a79c2024-03-29 16:34:20 -07003901static void read_film_grain_params(AV1_COMMON *cm,
3902 struct aom_read_bit_buffer *rb) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003903 aom_film_grain_t *pars = &cm->film_grain_params;
Tarundeep Singh4243e622021-04-20 16:10:22 +05303904 const SequenceHeader *const seq_params = cm->seq_params;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003905
3906 pars->apply_grain = aom_rb_read_bit(rb);
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003907 if (!pars->apply_grain) {
3908 memset(pars, 0, sizeof(*pars));
3909 return;
3910 }
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003911
3912 pars->random_seed = aom_rb_read_literal(rb, 16);
David Turnerd2a592e2018-11-16 14:59:31 +00003913 if (cm->current_frame.frame_type == INTER_FRAME)
Andrey Norkin879488f2018-02-28 15:30:26 -08003914 pars->update_parameters = aom_rb_read_bit(rb);
3915 else
3916 pars->update_parameters = 1;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003917
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003918 pars->bit_depth = seq_params->bit_depth;
David Barkera70532c2018-07-03 17:29:28 +01003919
Dominic Symesd4929012018-01-31 17:32:01 +01003920 if (!pars->update_parameters) {
3921 // inherit parameters from a previous reference frame
Dominic Symesd4929012018-01-31 17:32:01 +01003922 int film_grain_params_ref_idx = aom_rb_read_literal(rb, 3);
Wan-Teh Chang0c164bf2019-03-19 16:48:03 -07003923 // Section 6.8.20: It is a requirement of bitstream conformance that
3924 // film_grain_params_ref_idx is equal to ref_frame_idx[ j ] for some value
3925 // of j in the range 0 to REFS_PER_FRAME - 1.
3926 int found = 0;
3927 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3928 if (film_grain_params_ref_idx == cm->remapped_ref_idx[i]) {
3929 found = 1;
3930 break;
3931 }
3932 }
3933 if (!found) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303934 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Wan-Teh Chang0c164bf2019-03-19 16:48:03 -07003935 "Invalid film grain reference idx %d. ref_frame_idx = "
3936 "{%d, %d, %d, %d, %d, %d, %d}",
3937 film_grain_params_ref_idx, cm->remapped_ref_idx[0],
3938 cm->remapped_ref_idx[1], cm->remapped_ref_idx[2],
3939 cm->remapped_ref_idx[3], cm->remapped_ref_idx[4],
3940 cm->remapped_ref_idx[5], cm->remapped_ref_idx[6]);
3941 }
David Turnere7ebf902018-12-04 14:04:55 +00003942 RefCntBuffer *const buf = cm->ref_frame_map[film_grain_params_ref_idx];
3943 if (buf == NULL) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303944 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Debargha Mukherjee9844c8d2018-05-02 11:15:54 -07003945 "Invalid Film grain reference idx");
3946 }
David Turnere7ebf902018-12-04 14:04:55 +00003947 if (!buf->film_grain_params_present) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303948 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Dominic Symesd4929012018-01-31 17:32:01 +01003949 "Film grain reference parameters not available");
3950 }
3951 uint16_t random_seed = pars->random_seed;
David Turnere7ebf902018-12-04 14:04:55 +00003952 *pars = buf->film_grain_params; // inherit paramaters
3953 pars->random_seed = random_seed; // with new random seed
Dominic Symesd4929012018-01-31 17:32:01 +01003954 return;
3955 }
Dominic Symesd4929012018-01-31 17:32:01 +01003956
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003957 // Scaling functions parameters
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003958 pars->num_y_points = aom_rb_read_literal(rb, 4); // max 14
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003959 if (pars->num_y_points > 14)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303960 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003961 "Number of points for film grain luma scaling function "
3962 "exceeds the maximum value.");
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003963 for (int i = 0; i < pars->num_y_points; i++) {
3964 pars->scaling_points_y[i][0] = aom_rb_read_literal(rb, 8);
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003965 if (i && pars->scaling_points_y[i - 1][0] >= pars->scaling_points_y[i][0])
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303966 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
David Barkerfa6dfe52018-04-17 12:53:29 +01003967 "First coordinate of the scaling function points "
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003968 "shall be increasing.");
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003969 pars->scaling_points_y[i][1] = aom_rb_read_literal(rb, 8);
3970 }
3971
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003972 if (!seq_params->monochrome)
Andrey Norkin20be5452018-02-20 17:46:13 -08003973 pars->chroma_scaling_from_luma = aom_rb_read_bit(rb);
Jonathan Matthews080299a2018-07-28 15:54:33 +01003974 else
3975 pars->chroma_scaling_from_luma = 0;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003976
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003977 if (seq_params->monochrome || pars->chroma_scaling_from_luma ||
3978 ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
Andrey Norkin626a1b22018-03-23 21:47:17 -07003979 (pars->num_y_points == 0))) {
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003980 pars->num_cb_points = 0;
3981 pars->num_cr_points = 0;
3982 } else {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003983 pars->num_cb_points = aom_rb_read_literal(rb, 4); // max 10
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003984 if (pars->num_cb_points > 10)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303985 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003986 "Number of points for film grain cb scaling function "
3987 "exceeds the maximum value.");
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003988 for (int i = 0; i < pars->num_cb_points; i++) {
3989 pars->scaling_points_cb[i][0] = aom_rb_read_literal(rb, 8);
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003990 if (i &&
3991 pars->scaling_points_cb[i - 1][0] >= pars->scaling_points_cb[i][0])
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05303992 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003993 "First coordinate of the scaling function points "
3994 "shall be increasing.");
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08003995 pars->scaling_points_cb[i][1] = aom_rb_read_literal(rb, 8);
3996 }
3997
3998 pars->num_cr_points = aom_rb_read_literal(rb, 4); // max 10
Andrey Norkin0c294fa2018-02-16 18:32:12 -08003999 if (pars->num_cr_points > 10)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304000 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Andrey Norkin0c294fa2018-02-16 18:32:12 -08004001 "Number of points for film grain cr scaling function "
4002 "exceeds the maximum value.");
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08004003 for (int i = 0; i < pars->num_cr_points; i++) {
4004 pars->scaling_points_cr[i][0] = aom_rb_read_literal(rb, 8);
Andrey Norkin0c294fa2018-02-16 18:32:12 -08004005 if (i &&
4006 pars->scaling_points_cr[i - 1][0] >= pars->scaling_points_cr[i][0])
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304007 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Andrey Norkin0c294fa2018-02-16 18:32:12 -08004008 "First coordinate of the scaling function points "
4009 "shall be increasing.");
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08004010 pars->scaling_points_cr[i][1] = aom_rb_read_literal(rb, 8);
4011 }
Andrey Norkin626a1b22018-03-23 21:47:17 -07004012
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004013 if ((seq_params->subsampling_x == 1) && (seq_params->subsampling_y == 1) &&
Andrey Norkin626a1b22018-03-23 21:47:17 -07004014 (((pars->num_cb_points == 0) && (pars->num_cr_points != 0)) ||
4015 ((pars->num_cb_points != 0) && (pars->num_cr_points == 0))))
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304016 aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM,
Andrey Norkin626a1b22018-03-23 21:47:17 -07004017 "In YCbCr 4:2:0, film grain shall be applied "
4018 "to both chroma components or neither.");
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08004019 }
4020
4021 pars->scaling_shift = aom_rb_read_literal(rb, 2) + 8; // 8 + value
4022
4023 // AR coefficients
4024 // Only sent if the corresponsing scaling function has
4025 // more than 0 points
4026
4027 pars->ar_coeff_lag = aom_rb_read_literal(rb, 2);
4028
4029 int num_pos_luma = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1);
Andrey Norkin20be5452018-02-20 17:46:13 -08004030 int num_pos_chroma = num_pos_luma;
4031 if (pars->num_y_points > 0) ++num_pos_chroma;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08004032
4033 if (pars->num_y_points)
4034 for (int i = 0; i < num_pos_luma; i++)
4035 pars->ar_coeffs_y[i] = aom_rb_read_literal(rb, 8) - 128;
4036
4037 if (pars->num_cb_points || pars->chroma_scaling_from_luma)
4038 for (int i = 0; i < num_pos_chroma; i++)
4039 pars->ar_coeffs_cb[i] = aom_rb_read_literal(rb, 8) - 128;
4040
4041 if (pars->num_cr_points || pars->chroma_scaling_from_luma)
4042 for (int i = 0; i < num_pos_chroma; i++)
4043 pars->ar_coeffs_cr[i] = aom_rb_read_literal(rb, 8) - 128;
4044
4045 pars->ar_coeff_shift = aom_rb_read_literal(rb, 2) + 6; // 6 + value
4046
Andrey Norkina840cde2018-02-16 15:39:50 -08004047 pars->grain_scale_shift = aom_rb_read_literal(rb, 2);
4048
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08004049 if (pars->num_cb_points) {
4050 pars->cb_mult = aom_rb_read_literal(rb, 8);
4051 pars->cb_luma_mult = aom_rb_read_literal(rb, 8);
4052 pars->cb_offset = aom_rb_read_literal(rb, 9);
4053 }
4054
4055 if (pars->num_cr_points) {
4056 pars->cr_mult = aom_rb_read_literal(rb, 8);
4057 pars->cr_luma_mult = aom_rb_read_literal(rb, 8);
4058 pars->cr_offset = aom_rb_read_literal(rb, 9);
4059 }
4060
4061 pars->overlap_flag = aom_rb_read_bit(rb);
4062
4063 pars->clip_to_restricted_range = aom_rb_read_bit(rb);
4064}
4065
Wan-Teh Chang94534432024-08-08 20:29:57 -07004066static inline void read_film_grain(AV1_COMMON *cm,
4067 struct aom_read_bit_buffer *rb) {
Tarundeep Singh4243e622021-04-20 16:10:22 +05304068 if (cm->seq_params->film_grain_params_present &&
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07004069 (cm->show_frame || cm->showable_frame)) {
James Zern0d1a79c2024-03-29 16:34:20 -07004070 read_film_grain_params(cm, rb);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08004071 } else {
4072 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
4073 }
Tarundeep Singh4243e622021-04-20 16:10:22 +05304074 cm->film_grain_params.bit_depth = cm->seq_params->bit_depth;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08004075 memcpy(&cm->cur_frame->film_grain_params, &cm->film_grain_params,
4076 sizeof(aom_film_grain_t));
4077}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08004078
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004079void av1_read_color_config(struct aom_read_bit_buffer *rb,
4080 int allow_lowbitdepth, SequenceHeader *seq_params,
4081 struct aom_internal_error_info *error_info) {
4082 read_bitdepth(rb, seq_params, error_info);
Sebastien Alaiwan98378132017-01-04 11:23:09 +01004083
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004084 seq_params->use_highbitdepth =
4085 seq_params->bit_depth > AOM_BITS_8 || !allow_lowbitdepth;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08004086 // monochrome bit (not needed for PROFILE_1)
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004087 const int is_monochrome =
4088 seq_params->profile != PROFILE_1 ? aom_rb_read_bit(rb) : 0;
Hui Sud31a6002018-07-09 14:44:35 -07004089 seq_params->monochrome = is_monochrome;
Andrey Norkin9e694632017-12-21 18:50:57 -08004090 int color_description_present_flag = aom_rb_read_bit(rb);
4091 if (color_description_present_flag) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004092 seq_params->color_primaries = aom_rb_read_literal(rb, 8);
4093 seq_params->transfer_characteristics = aom_rb_read_literal(rb, 8);
4094 seq_params->matrix_coefficients = aom_rb_read_literal(rb, 8);
Andrey Norkin9e694632017-12-21 18:50:57 -08004095 } else {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004096 seq_params->color_primaries = AOM_CICP_CP_UNSPECIFIED;
4097 seq_params->transfer_characteristics = AOM_CICP_TC_UNSPECIFIED;
4098 seq_params->matrix_coefficients = AOM_CICP_MC_UNSPECIFIED;
Andrey Norkin9e694632017-12-21 18:50:57 -08004099 }
Debargha Mukherjeee5267692018-01-16 09:41:15 -08004100 if (is_monochrome) {
Debargha Mukherjee085095d2018-03-30 12:39:56 -07004101 // [16,235] (including xvycc) vs [0,255] range
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004102 seq_params->color_range = aom_rb_read_bit(rb);
4103 seq_params->subsampling_y = seq_params->subsampling_x = 1;
4104 seq_params->chroma_sample_position = AOM_CSP_UNKNOWN;
4105 seq_params->separate_uv_delta_q = 0;
Debargha Mukherjeee5267692018-01-16 09:41:15 -08004106 return;
4107 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004108 if (seq_params->color_primaries == AOM_CICP_CP_BT_709 &&
4109 seq_params->transfer_characteristics == AOM_CICP_TC_SRGB &&
4110 seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004111 seq_params->subsampling_y = seq_params->subsampling_x = 0;
4112 seq_params->color_range = 1; // assume full color-range
4113 if (!(seq_params->profile == PROFILE_1 ||
4114 (seq_params->profile == PROFILE_2 &&
4115 seq_params->bit_depth == AOM_BITS_12))) {
Debargha Mukherjee6eacb992018-03-30 13:07:27 -07004116 aom_internal_error(
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004117 error_info, AOM_CODEC_UNSUP_BITSTREAM,
Debargha Mukherjee6eacb992018-03-30 13:07:27 -07004118 "sRGB colorspace not compatible with specified profile");
Imdad Sardharwalla317002f2017-12-05 16:24:56 +00004119 }
Imdad Sardharwalla317002f2017-12-05 16:24:56 +00004120 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004121 // [16,235] (including xvycc) vs [0,255] range
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004122 seq_params->color_range = aom_rb_read_bit(rb);
4123 if (seq_params->profile == PROFILE_0) {
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08004124 // 420 only
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004125 seq_params->subsampling_x = seq_params->subsampling_y = 1;
4126 } else if (seq_params->profile == PROFILE_1) {
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08004127 // 444 only
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004128 seq_params->subsampling_x = seq_params->subsampling_y = 0;
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07004129 } else {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004130 assert(seq_params->profile == PROFILE_2);
4131 if (seq_params->bit_depth == AOM_BITS_12) {
4132 seq_params->subsampling_x = aom_rb_read_bit(rb);
4133 if (seq_params->subsampling_x)
4134 seq_params->subsampling_y = aom_rb_read_bit(rb); // 422 or 420
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07004135 else
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004136 seq_params->subsampling_y = 0; // 444
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08004137 } else {
4138 // 422
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004139 seq_params->subsampling_x = 1;
4140 seq_params->subsampling_y = 0;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08004141 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004142 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004143 if (seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY &&
4144 (seq_params->subsampling_x || seq_params->subsampling_y)) {
Debargha Mukherjeef61c0d12018-03-30 19:29:59 -07004145 aom_internal_error(
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004146 error_info, AOM_CODEC_UNSUP_BITSTREAM,
Debargha Mukherjeef61c0d12018-03-30 19:29:59 -07004147 "Identity CICP Matrix incompatible with non 4:4:4 color sampling");
4148 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004149 if (seq_params->subsampling_x && seq_params->subsampling_y) {
4150 seq_params->chroma_sample_position = aom_rb_read_literal(rb, 2);
anorkin76fb1262017-03-22 15:12:12 -07004151 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004152 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004153 seq_params->separate_uv_delta_q = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004154}
4155
Urvang Joshi450a9a22020-03-31 16:00:22 -07004156void av1_read_timing_info_header(aom_timing_info_t *timing_info,
4157 struct aom_internal_error_info *error,
Andrey Norkin28e9ce22018-01-08 10:11:21 -08004158 struct aom_read_bit_buffer *rb) {
Urvang Joshi450a9a22020-03-31 16:00:22 -07004159 timing_info->num_units_in_display_tick =
4160 aom_rb_read_unsigned_literal(rb,
4161 32); // Number of units in a display tick
4162 timing_info->time_scale = aom_rb_read_unsigned_literal(rb, 32); // Time scale
4163 if (timing_info->num_units_in_display_tick == 0 ||
4164 timing_info->time_scale == 0) {
Wan-Teh Changf250f482018-05-23 12:54:36 -07004165 aom_internal_error(
Urvang Joshi450a9a22020-03-31 16:00:22 -07004166 error, AOM_CODEC_UNSUP_BITSTREAM,
Wan-Teh Changf250f482018-05-23 12:54:36 -07004167 "num_units_in_display_tick and time_scale must be greater than 0.");
4168 }
Urvang Joshi450a9a22020-03-31 16:00:22 -07004169 timing_info->equal_picture_interval =
Wan-Teh Changf250f482018-05-23 12:54:36 -07004170 aom_rb_read_bit(rb); // Equal picture interval bit
Urvang Joshi450a9a22020-03-31 16:00:22 -07004171 if (timing_info->equal_picture_interval) {
Wan-Teh Chang2d7e8312019-03-19 09:32:40 -07004172 const uint32_t num_ticks_per_picture_minus_1 = aom_rb_read_uvlc(rb);
4173 if (num_ticks_per_picture_minus_1 == UINT32_MAX) {
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07004174 aom_internal_error(
Urvang Joshi450a9a22020-03-31 16:00:22 -07004175 error, AOM_CODEC_UNSUP_BITSTREAM,
Wan-Teh Chang854b2762020-12-08 09:59:52 -08004176 "num_ticks_per_picture_minus_1 cannot be (1 << 32) - 1.");
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07004177 }
Urvang Joshi450a9a22020-03-31 16:00:22 -07004178 timing_info->num_ticks_per_picture = num_ticks_per_picture_minus_1 + 1;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08004179 }
4180}
Andrey Norkin28e9ce22018-01-08 10:11:21 -08004181
Urvang Joshi450a9a22020-03-31 16:00:22 -07004182void av1_read_decoder_model_info(aom_dec_model_info_t *decoder_model_info,
Andrey Norkin795ba872018-03-06 13:24:14 -08004183 struct aom_read_bit_buffer *rb) {
Urvang Joshi450a9a22020-03-31 16:00:22 -07004184 decoder_model_info->encoder_decoder_buffer_delay_length =
Andrey Norkin795ba872018-03-06 13:24:14 -08004185 aom_rb_read_literal(rb, 5) + 1;
Urvang Joshi450a9a22020-03-31 16:00:22 -07004186 decoder_model_info->num_units_in_decoding_tick =
4187 aom_rb_read_unsigned_literal(rb,
4188 32); // Number of units in a decoding tick
4189 decoder_model_info->buffer_removal_time_length =
4190 aom_rb_read_literal(rb, 5) + 1;
4191 decoder_model_info->frame_presentation_time_length =
Andrey Norkin795ba872018-03-06 13:24:14 -08004192 aom_rb_read_literal(rb, 5) + 1;
4193}
4194
Urvang Joshi450a9a22020-03-31 16:00:22 -07004195void av1_read_op_parameters_info(aom_dec_model_op_parameters_t *op_params,
4196 int buffer_delay_length,
4197 struct aom_read_bit_buffer *rb) {
4198 op_params->decoder_buffer_delay =
4199 aom_rb_read_unsigned_literal(rb, buffer_delay_length);
4200 op_params->encoder_buffer_delay =
4201 aom_rb_read_unsigned_literal(rb, buffer_delay_length);
4202 op_params->low_delay_mode_flag = aom_rb_read_bit(rb);
Andrey Norkin795ba872018-03-06 13:24:14 -08004203}
4204
Wan-Teh Chang94534432024-08-08 20:29:57 -07004205static inline void read_temporal_point_info(AV1_COMMON *const cm,
4206 struct aom_read_bit_buffer *rb) {
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07004207 cm->frame_presentation_time = aom_rb_read_unsigned_literal(
Tarundeep Singh4243e622021-04-20 16:10:22 +05304208 rb, cm->seq_params->decoder_model_info.frame_presentation_time_length);
Andrey Norkin795ba872018-03-06 13:24:14 -08004209}
Andrey Norkin795ba872018-03-06 13:24:14 -08004210
Hui Sud31a6002018-07-09 14:44:35 -07004211void av1_read_sequence_header(AV1_COMMON *cm, struct aom_read_bit_buffer *rb,
4212 SequenceHeader *seq_params) {
4213 const int num_bits_width = aom_rb_read_literal(rb, 4) + 1;
4214 const int num_bits_height = aom_rb_read_literal(rb, 4) + 1;
4215 const int max_frame_width = aom_rb_read_literal(rb, num_bits_width) + 1;
4216 const int max_frame_height = aom_rb_read_literal(rb, num_bits_height) + 1;
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01004217
4218 seq_params->num_bits_width = num_bits_width;
4219 seq_params->num_bits_height = num_bits_height;
4220 seq_params->max_frame_width = max_frame_width;
4221 seq_params->max_frame_height = max_frame_height;
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01004222
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004223 if (seq_params->reduced_still_picture_hdr) {
4224 seq_params->frame_id_numbers_present_flag = 0;
4225 } else {
4226 seq_params->frame_id_numbers_present_flag = aom_rb_read_bit(rb);
4227 }
David Barker5e70a112017-10-03 14:28:17 +01004228 if (seq_params->frame_id_numbers_present_flag) {
Frederic Barbier4d5d90e2017-10-13 09:22:33 +02004229 // We must always have delta_frame_id_length < frame_id_length,
4230 // in order for a frame to be referenced with a unique delta.
4231 // Avoid wasting bits by using a coding that enforces this restriction.
Frederic Barbiere83fcfe2017-10-13 10:37:50 +02004232 seq_params->delta_frame_id_length = aom_rb_read_literal(rb, 4) + 2;
Frederic Barbier4d5d90e2017-10-13 09:22:33 +02004233 seq_params->frame_id_length =
4234 aom_rb_read_literal(rb, 3) + seq_params->delta_frame_id_length + 1;
Thomas Davies0cbcf792018-03-15 14:10:02 +00004235 if (seq_params->frame_id_length > 16)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304236 aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
Thomas Davies0cbcf792018-03-15 14:10:02 +00004237 "Invalid frame_id_length");
David Barker5e70a112017-10-03 14:28:17 +01004238 }
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00004239
4240 setup_sb_size(seq_params, rb);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00004241
Debargha Mukherjee365eae82018-03-26 19:19:55 -07004242 seq_params->enable_filter_intra = aom_rb_read_bit(rb);
4243 seq_params->enable_intra_edge_filter = aom_rb_read_bit(rb);
4244
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004245 if (seq_params->reduced_still_picture_hdr) {
4246 seq_params->enable_interintra_compound = 0;
4247 seq_params->enable_masked_compound = 0;
4248 seq_params->enable_warped_motion = 0;
4249 seq_params->enable_dual_filter = 0;
David Turnerebf96f42018-11-14 16:57:57 +00004250 seq_params->order_hint_info.enable_order_hint = 0;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08004251 seq_params->order_hint_info.enable_dist_wtd_comp = 0;
David Turnerebf96f42018-11-14 16:57:57 +00004252 seq_params->order_hint_info.enable_ref_frame_mvs = 0;
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07004253 seq_params->force_screen_content_tools = 2; // SELECT_SCREEN_CONTENT_TOOLS
4254 seq_params->force_integer_mv = 2; // SELECT_INTEGER_MV
David Turnerebf96f42018-11-14 16:57:57 +00004255 seq_params->order_hint_info.order_hint_bits_minus_1 = -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004256 } else {
4257 seq_params->enable_interintra_compound = aom_rb_read_bit(rb);
4258 seq_params->enable_masked_compound = aom_rb_read_bit(rb);
4259 seq_params->enable_warped_motion = aom_rb_read_bit(rb);
4260 seq_params->enable_dual_filter = aom_rb_read_bit(rb);
4261
David Turnerebf96f42018-11-14 16:57:57 +00004262 seq_params->order_hint_info.enable_order_hint = aom_rb_read_bit(rb);
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08004263 seq_params->order_hint_info.enable_dist_wtd_comp =
David Turnerebf96f42018-11-14 16:57:57 +00004264 seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
4265 seq_params->order_hint_info.enable_ref_frame_mvs =
4266 seq_params->order_hint_info.enable_order_hint ? aom_rb_read_bit(rb) : 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004267
4268 if (aom_rb_read_bit(rb)) {
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07004269 seq_params->force_screen_content_tools =
4270 2; // SELECT_SCREEN_CONTENT_TOOLS
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004271 } else {
4272 seq_params->force_screen_content_tools = aom_rb_read_bit(rb);
4273 }
4274
4275 if (seq_params->force_screen_content_tools > 0) {
4276 if (aom_rb_read_bit(rb)) {
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07004277 seq_params->force_integer_mv = 2; // SELECT_INTEGER_MV
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004278 } else {
4279 seq_params->force_integer_mv = aom_rb_read_bit(rb);
4280 }
4281 } else {
Wan-Teh Changa19fdce2018-05-22 14:08:16 -07004282 seq_params->force_integer_mv = 2; // SELECT_INTEGER_MV
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004283 }
David Turnerebf96f42018-11-14 16:57:57 +00004284 seq_params->order_hint_info.order_hint_bits_minus_1 =
4285 seq_params->order_hint_info.enable_order_hint
4286 ? aom_rb_read_literal(rb, 3)
4287 : -1;
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00004288 }
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +00004289
Urvang Joshi2c92b072018-03-19 17:23:31 -07004290 seq_params->enable_superres = aom_rb_read_bit(rb);
Debargha Mukherjee98a311c2018-03-25 16:33:11 -07004291 seq_params->enable_cdef = aom_rb_read_bit(rb);
4292 seq_params->enable_restoration = aom_rb_read_bit(rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004293}
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004294
Sarah Parker3e579a62017-08-23 16:53:20 -07004295static int read_global_motion_params(WarpedMotionParams *params,
David Barkerd7c8bd52017-09-25 14:47:29 +01004296 const WarpedMotionParams *ref_params,
Sarah Parker3e579a62017-08-23 16:53:20 -07004297 struct aom_read_bit_buffer *rb,
4298 int allow_hp) {
4299 TransformationType type = aom_rb_read_bit(rb);
4300 if (type != IDENTITY) {
Sarah Parker3e579a62017-08-23 16:53:20 -07004301 if (aom_rb_read_bit(rb))
4302 type = ROTZOOM;
4303 else
4304 type = aom_rb_read_bit(rb) ? TRANSLATION : AFFINE;
Sarah Parker3e579a62017-08-23 16:53:20 -07004305 }
4306
David Barkerd7c8bd52017-09-25 14:47:29 +01004307 *params = default_warp_params;
Sarah Parker3e579a62017-08-23 16:53:20 -07004308 params->wmtype = type;
Sebastien Alaiwan238a6d62017-11-01 11:29:46 +01004309
4310 if (type >= ROTZOOM) {
4311 params->wmmat[2] = aom_rb_read_signed_primitive_refsubexpfin(
4312 rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4313 (ref_params->wmmat[2] >> GM_ALPHA_PREC_DIFF) -
4314 (1 << GM_ALPHA_PREC_BITS)) *
4315 GM_ALPHA_DECODE_FACTOR +
4316 (1 << WARPEDMODEL_PREC_BITS);
4317 params->wmmat[3] = aom_rb_read_signed_primitive_refsubexpfin(
4318 rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4319 (ref_params->wmmat[3] >> GM_ALPHA_PREC_DIFF)) *
4320 GM_ALPHA_DECODE_FACTOR;
Sarah Parker3e579a62017-08-23 16:53:20 -07004321 }
Sebastien Alaiwan238a6d62017-11-01 11:29:46 +01004322
4323 if (type >= AFFINE) {
4324 params->wmmat[4] = aom_rb_read_signed_primitive_refsubexpfin(
4325 rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4326 (ref_params->wmmat[4] >> GM_ALPHA_PREC_DIFF)) *
4327 GM_ALPHA_DECODE_FACTOR;
4328 params->wmmat[5] = aom_rb_read_signed_primitive_refsubexpfin(
4329 rb, GM_ALPHA_MAX + 1, SUBEXPFIN_K,
4330 (ref_params->wmmat[5] >> GM_ALPHA_PREC_DIFF) -
4331 (1 << GM_ALPHA_PREC_BITS)) *
4332 GM_ALPHA_DECODE_FACTOR +
4333 (1 << WARPEDMODEL_PREC_BITS);
4334 } else {
4335 params->wmmat[4] = -params->wmmat[3];
4336 params->wmmat[5] = params->wmmat[2];
4337 }
4338
4339 if (type >= TRANSLATION) {
4340 const int trans_bits = (type == TRANSLATION)
4341 ? GM_ABS_TRANS_ONLY_BITS - !allow_hp
4342 : GM_ABS_TRANS_BITS;
4343 const int trans_dec_factor =
4344 (type == TRANSLATION) ? GM_TRANS_ONLY_DECODE_FACTOR * (1 << !allow_hp)
4345 : GM_TRANS_DECODE_FACTOR;
4346 const int trans_prec_diff = (type == TRANSLATION)
4347 ? GM_TRANS_ONLY_PREC_DIFF + !allow_hp
4348 : GM_TRANS_PREC_DIFF;
4349 params->wmmat[0] = aom_rb_read_signed_primitive_refsubexpfin(
4350 rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
4351 (ref_params->wmmat[0] >> trans_prec_diff)) *
4352 trans_dec_factor;
4353 params->wmmat[1] = aom_rb_read_signed_primitive_refsubexpfin(
4354 rb, (1 << trans_bits) + 1, SUBEXPFIN_K,
4355 (ref_params->wmmat[1] >> trans_prec_diff)) *
4356 trans_dec_factor;
4357 }
4358
Rachel Barker0d9df422023-03-07 20:51:55 +00004359 int good_shear_params = av1_get_shear_params(params);
4360 if (!good_shear_params) return 0;
Sarah Parker3e579a62017-08-23 16:53:20 -07004361
4362 return 1;
4363}
4364
Wan-Teh Chang94534432024-08-08 20:29:57 -07004365static inline void read_global_motion(AV1_COMMON *cm,
4366 struct aom_read_bit_buffer *rb) {
Sebastien Alaiwanbc51d0e2017-12-07 11:13:57 +01004367 for (int frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
David Barkerd7c8bd52017-09-25 14:47:29 +01004368 const WarpedMotionParams *ref_params =
David Barkercc615a82018-03-19 14:38:51 +00004369 cm->prev_frame ? &cm->prev_frame->global_motion[frame]
4370 : &default_warp_params;
Urvang Joshib6409e92020-03-23 11:23:27 -07004371 int good_params =
4372 read_global_motion_params(&cm->global_motion[frame], ref_params, rb,
4373 cm->features.allow_high_precision_mv);
Sebastien Alaiwan8d88b292017-11-03 17:38:30 +01004374 if (!good_params) {
4375#if WARPED_MOTION_DEBUG
4376 printf("Warning: unexpected global motion shear params from aomenc\n");
4377#endif
4378 cm->global_motion[frame].invalid = 1;
4379 }
Sarah Parker3e579a62017-08-23 16:53:20 -07004380
4381 // TODO(sarahparker, debargha): The logic in the commented out code below
4382 // does not work currently and causes mismatches when resize is on. Fix it
4383 // before turning the optimization back on.
4384 /*
4385 YV12_BUFFER_CONFIG *ref_buf = get_ref_frame(cm, frame);
4386 if (cm->width == ref_buf->y_crop_width &&
4387 cm->height == ref_buf->y_crop_height) {
4388 read_global_motion_params(&cm->global_motion[frame],
4389 &cm->prev_frame->global_motion[frame], rb,
Urvang Joshib6409e92020-03-23 11:23:27 -07004390 cm->features.allow_high_precision_mv);
Sarah Parker3e579a62017-08-23 16:53:20 -07004391 } else {
David Barkerd7c8bd52017-09-25 14:47:29 +01004392 cm->global_motion[frame] = default_warp_params;
Sarah Parker3e579a62017-08-23 16:53:20 -07004393 }
4394 */
4395 /*
4396 printf("Dec Ref %d [%d/%d]: %d %d %d %d\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004397 frame, cm->current_frame.frame_number, cm->show_frame,
Sarah Parker3e579a62017-08-23 16:53:20 -07004398 cm->global_motion[frame].wmmat[0],
4399 cm->global_motion[frame].wmmat[1],
4400 cm->global_motion[frame].wmmat[2],
4401 cm->global_motion[frame].wmmat[3]);
4402 */
4403 }
David Barkercba7da72017-09-14 11:24:27 +01004404 memcpy(cm->cur_frame->global_motion, cm->global_motion,
Zoe Liu27deb382018-03-27 15:13:56 -07004405 REF_FRAMES * sizeof(WarpedMotionParams));
Sarah Parker3e579a62017-08-23 16:53:20 -07004406}
Sarah Parker3e579a62017-08-23 16:53:20 -07004407
Wan-Teh Chang38353c82018-09-27 18:46:55 -07004408// Release the references to the frame buffers in cm->ref_frame_map and reset
David Turnere7ebf902018-12-04 14:04:55 +00004409// all elements of cm->ref_frame_map to NULL.
Wan-Teh Chang94534432024-08-08 20:29:57 -07004410static inline void reset_ref_frame_map(AV1_COMMON *const cm) {
Wan-Teh Chang38353c82018-09-27 18:46:55 -07004411 BufferPool *const pool = cm->buffer_pool;
Wan-Teh Chang38353c82018-09-27 18:46:55 -07004412
4413 for (int i = 0; i < REF_FRAMES; i++) {
David Turnere7ebf902018-12-04 14:04:55 +00004414 decrease_ref_count(cm->ref_frame_map[i], pool);
4415 cm->ref_frame_map[i] = NULL;
Wan-Teh Chang38353c82018-09-27 18:46:55 -07004416 }
Wan-Teh Chang38353c82018-09-27 18:46:55 -07004417}
4418
Wan-Teh Chang7c4cb452018-12-03 12:08:20 -08004419// If the refresh_frame_flags bitmask is set, update reference frame id values
4420// and mark frames as valid for reference.
Wan-Teh Chang94534432024-08-08 20:29:57 -07004421static inline void update_ref_frame_id(AV1Decoder *const pbi) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004422 AV1_COMMON *const cm = &pbi->common;
Wan-Teh Chang7c4cb452018-12-03 12:08:20 -08004423 int refresh_frame_flags = cm->current_frame.refresh_frame_flags;
4424 for (int i = 0; i < REF_FRAMES; i++) {
4425 if ((refresh_frame_flags >> i) & 1) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004426 cm->ref_frame_id[i] = cm->current_frame_id;
4427 pbi->valid_for_referencing[i] = 1;
Wan-Teh Chang7c4cb452018-12-03 12:08:20 -08004428 }
4429 }
4430}
4431
Wan-Teh Chang94534432024-08-08 20:29:57 -07004432static inline void show_existing_frame_reset(AV1Decoder *const pbi,
4433 int existing_frame_idx) {
Wan-Teh Changa08349c2018-09-26 10:38:53 -07004434 AV1_COMMON *const cm = &pbi->common;
4435
Bruno Berthierf03d9362018-02-23 15:47:13 +01004436 assert(cm->show_existing_frame);
4437
David Turnerd2a592e2018-11-16 14:59:31 +00004438 cm->current_frame.frame_type = KEY_FRAME;
Zoe Liua7c1b192017-12-19 16:59:38 -08004439
David Turner996b2c12018-12-07 15:52:30 +00004440 cm->current_frame.refresh_frame_flags = (1 << REF_FRAMES) - 1;
Zoe Liua7c1b192017-12-19 16:59:38 -08004441
4442 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
David Turnera21966b2018-12-05 14:48:49 +00004443 cm->remapped_ref_idx[i] = INVALID_IDX;
Zoe Liua7c1b192017-12-19 16:59:38 -08004444 }
4445
4446 if (pbi->need_resync) {
Wan-Teh Chang38353c82018-09-27 18:46:55 -07004447 reset_ref_frame_map(cm);
Zoe Liua7c1b192017-12-19 16:59:38 -08004448 pbi->need_resync = 0;
4449 }
4450
Wan-Teh Chang7c4cb452018-12-03 12:08:20 -08004451 // Note that the displayed frame must be valid for referencing in order to
4452 // have been selected.
Wan-Teh Chang58e4a152019-06-25 13:44:35 -07004453 cm->current_frame_id = cm->ref_frame_id[existing_frame_idx];
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004454 update_ref_frame_id(pbi);
Zoe Liua7c1b192017-12-19 16:59:38 -08004455
Urvang Joshi6237b882020-03-26 15:02:26 -07004456 cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Zoe Liua7c1b192017-12-19 16:59:38 -08004457}
Zoe Liua7c1b192017-12-19 16:59:38 -08004458
Wan-Teh Chang12c64e82024-08-08 16:02:19 -07004459static inline void reset_frame_buffers(AV1_COMMON *cm) {
Yaowu Xu78f49b22018-08-30 12:47:20 -07004460 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
4461 int i;
4462
Yaowu Xu78f49b22018-08-30 12:47:20 -07004463 lock_buffer_pool(cm->buffer_pool);
Wan-Teh Chang0c1b8c12018-10-03 09:56:24 -07004464 reset_ref_frame_map(cm);
Jack Haughtonddb80602018-11-21 16:41:49 +00004465 assert(cm->cur_frame->ref_count == 1);
Apurve Kumar Pandey24f62e82022-12-27 11:55:45 +05304466 for (i = 0; i < cm->buffer_pool->num_frame_bufs; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00004467 // Reset all unreferenced frame buffers. We can also reset cm->cur_frame
4468 // because we are the sole owner of cm->cur_frame.
4469 if (frame_bufs[i].ref_count > 0 && &frame_bufs[i] != cm->cur_frame) {
Wan-Teh Chang0c1b8c12018-10-03 09:56:24 -07004470 continue;
Yaowu Xu78f49b22018-08-30 12:47:20 -07004471 }
David Turner1bcefb32018-11-19 17:54:00 +00004472 frame_bufs[i].order_hint = 0;
4473 av1_zero(frame_bufs[i].ref_order_hints);
Yaowu Xu78f49b22018-08-30 12:47:20 -07004474 }
Wan-Teh Changc613fd42018-09-06 12:31:17 -07004475 av1_zero_unused_internal_frame_buffers(&cm->buffer_pool->int_frame_buffers);
Yaowu Xu78f49b22018-08-30 12:47:20 -07004476 unlock_buffer_pool(cm->buffer_pool);
4477}
4478
Wan-Teh Changbb6192a2018-07-10 11:01:04 -07004479// On success, returns 0. On failure, calls aom_internal_error and does not
4480// return.
Yunqing Wange7142e12018-01-17 11:20:12 -08004481static int read_uncompressed_header(AV1Decoder *pbi,
4482 struct aom_read_bit_buffer *rb) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004483 AV1_COMMON *const cm = &pbi->common;
Tarundeep Singh4243e622021-04-20 16:10:22 +05304484 const SequenceHeader *const seq_params = cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00004485 CurrentFrame *const current_frame = &cm->current_frame;
Urvang Joshib6409e92020-03-23 11:23:27 -07004486 FeatureFlags *const features = &cm->features;
Urvang Joshibac1dea2020-04-20 11:37:15 -07004487 MACROBLOCKD *const xd = &pbi->dcb.xd;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004488 BufferPool *const pool = cm->buffer_pool;
4489 RefCntBuffer *const frame_bufs = pool->frame_bufs;
Aasaipriya Chandrand0588a72020-07-21 13:28:48 +05304490 aom_s_frame_info *sframe_info = &pbi->sframe_info;
4491 sframe_info->is_s_frame = 0;
4492 sframe_info->is_s_frame_at_altref = 0;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004493
David Barker3a7b5c22018-03-21 12:06:16 +00004494 if (!pbi->sequence_header_ready) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304495 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Hui Su224cf962018-03-11 12:28:10 -07004496 "No sequence header");
4497 }
4498
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004499 if (seq_params->reduced_still_picture_hdr) {
David Barkerc9acf932018-04-17 12:54:34 +01004500 cm->show_existing_frame = 0;
4501 cm->show_frame = 1;
David Turnerd2a592e2018-11-16 14:59:31 +00004502 current_frame->frame_type = KEY_FRAME;
Wan-Teh Chang7c924402018-09-29 11:23:39 -07004503 if (pbi->sequence_header_changed) {
4504 // This is the start of a new coded video sequence.
4505 pbi->sequence_header_changed = 0;
4506 pbi->decoding_first_frame = 1;
4507 reset_frame_buffers(cm);
4508 }
Urvang Joshib6409e92020-03-23 11:23:27 -07004509 features->error_resilient_mode = 1;
David Barkerc9acf932018-04-17 12:54:34 +01004510 } else {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004511 cm->show_existing_frame = aom_rb_read_bit(rb);
David Turner475a3132019-01-18 15:17:17 +00004512 pbi->reset_decoder_state = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004513
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004514 if (cm->show_existing_frame) {
Yaowu Xu45a30ac2018-09-04 08:47:05 -07004515 if (pbi->sequence_header_changed) {
4516 aom_internal_error(
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304517 &pbi->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xu45a30ac2018-09-04 08:47:05 -07004518 "New sequence header starts with a show_existing_frame.");
4519 }
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004520 // Show an existing frame directly.
4521 const int existing_frame_idx = aom_rb_read_literal(rb, 3);
David Turnere7ebf902018-12-04 14:04:55 +00004522 RefCntBuffer *const frame_to_show = cm->ref_frame_map[existing_frame_idx];
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004523 if (frame_to_show == NULL) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304524 aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004525 "Buffer does not contain a decoded frame");
4526 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004527 if (seq_params->decoder_model_info_present_flag &&
Urvang Joshi450a9a22020-03-31 16:00:22 -07004528 seq_params->timing_info.equal_picture_interval == 0) {
Yaowu Xu96c0f982019-05-17 09:10:54 -07004529 read_temporal_point_info(cm, rb);
Andrey Norkin795ba872018-03-06 13:24:14 -08004530 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004531 if (seq_params->frame_id_numbers_present_flag) {
4532 int frame_id_length = seq_params->frame_id_length;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004533 int display_frame_id = aom_rb_read_literal(rb, frame_id_length);
4534 /* Compare display_frame_id with ref_frame_id and check valid for
4535 * referencing */
4536 if (display_frame_id != cm->ref_frame_id[existing_frame_idx] ||
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004537 pbi->valid_for_referencing[existing_frame_idx] == 0)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304538 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004539 "Reference buffer frame ID mismatch");
4540 }
4541 lock_buffer_pool(pool);
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004542 assert(frame_to_show->ref_count > 0);
Jack Haughtonddb80602018-11-21 16:41:49 +00004543 // cm->cur_frame should be the buffer referenced by the return value
Wan-Teh Chang6f954e72019-08-30 12:01:26 -07004544 // of the get_free_fb() call in assign_cur_frame_new_fb() (called by
4545 // av1_receive_compressed_data()), so the ref_count should be 1.
Jack Haughtonddb80602018-11-21 16:41:49 +00004546 assert(cm->cur_frame->ref_count == 1);
Wan-Teh Chang8634e612018-12-19 11:41:37 -08004547 // assign_frame_buffer_p() decrements ref_count directly rather than
4548 // call decrease_ref_count(). If cm->cur_frame->raw_frame_buffer has
4549 // already been allocated, it will not be released by
4550 // assign_frame_buffer_p()!
Jack Haughtonddb80602018-11-21 16:41:49 +00004551 assert(!cm->cur_frame->raw_frame_buffer.data);
David Turnere7ebf902018-12-04 14:04:55 +00004552 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner475a3132019-01-18 15:17:17 +00004553 pbi->reset_decoder_state = frame_to_show->frame_type == KEY_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004554 unlock_buffer_pool(pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004555
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004556 cm->lf.filter_level[0] = 0;
4557 cm->lf.filter_level[1] = 0;
4558 cm->show_frame = 1;
Bohan Li15b3c6d2021-07-27 17:27:20 -07004559 current_frame->order_hint = frame_to_show->order_hint;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004560
Wan-Teh Changab5aa982018-12-19 11:17:47 -08004561 // Section 6.8.2: It is a requirement of bitstream conformance that when
4562 // show_existing_frame is used to show a previous frame, that the value
4563 // of showable_frame for the previous frame was equal to 1.
David Turnere7ebf902018-12-04 14:04:55 +00004564 if (!frame_to_show->showable_frame) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304565 aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
Wan-Teh Changab5aa982018-12-19 11:17:47 -08004566 "Buffer does not contain a showable frame");
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004567 }
Wan-Teh Changab5aa982018-12-19 11:17:47 -08004568 // Section 6.8.2: It is a requirement of bitstream conformance that when
4569 // show_existing_frame is used to show a previous frame with
4570 // RefFrameType[ frame_to_show_map_idx ] equal to KEY_FRAME, that the
4571 // frame is output via the show_existing_frame mechanism at most once.
David Turner475a3132019-01-18 15:17:17 +00004572 if (pbi->reset_decoder_state) frame_to_show->showable_frame = 0;
Debargha Mukherjee171f4b32018-06-21 10:06:41 -07004573
David Turnere7ebf902018-12-04 14:04:55 +00004574 cm->film_grain_params = frame_to_show->film_grain_params;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004575
David Turner475a3132019-01-18 15:17:17 +00004576 if (pbi->reset_decoder_state) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004577 show_existing_frame_reset(pbi, existing_frame_idx);
4578 } else {
David Turner996b2c12018-12-07 15:52:30 +00004579 current_frame->refresh_frame_flags = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004580 }
4581
4582 return 0;
4583 }
4584
David Turnerd2a592e2018-11-16 14:59:31 +00004585 current_frame->frame_type = (FRAME_TYPE)aom_rb_read_literal(rb, 2);
Yaowu Xu3ff57a632018-08-29 10:50:57 -07004586 if (pbi->sequence_header_changed) {
David Turnerd2a592e2018-11-16 14:59:31 +00004587 if (current_frame->frame_type == KEY_FRAME) {
Yaowu Xu3ff57a632018-08-29 10:50:57 -07004588 // This is the start of a new coded video sequence.
4589 pbi->sequence_header_changed = 0;
Frederic Barbierd2a1efa2018-09-07 14:28:08 +02004590 pbi->decoding_first_frame = 1;
Wan-Teh Chang7c924402018-09-29 11:23:39 -07004591 reset_frame_buffers(cm);
Yaowu Xu3ff57a632018-08-29 10:50:57 -07004592 } else {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304593 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xu3ff57a632018-08-29 10:50:57 -07004594 "Sequence header has changed without a keyframe.");
4595 }
4596 }
4597
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004598 cm->show_frame = aom_rb_read_bit(rb);
Aasaipriya Chandranc8d7b282020-07-02 12:33:32 +05304599 if (cm->show_frame == 0) pbi->is_arf_frame_present = 1;
Aasaipriya Chandranb2209ce2020-06-18 19:41:22 +05304600 if (cm->show_frame == 0 && cm->current_frame.frame_type == KEY_FRAME)
4601 pbi->is_fwd_kf_present = 1;
Aasaipriya Chandrand0588a72020-07-21 13:28:48 +05304602 if (cm->current_frame.frame_type == S_FRAME) {
4603 sframe_info->is_s_frame = 1;
4604 sframe_info->is_s_frame_at_altref = cm->show_frame ? 0 : 1;
4605 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004606 if (seq_params->still_picture &&
David Turnerd2a592e2018-11-16 14:59:31 +00004607 (current_frame->frame_type != KEY_FRAME || !cm->show_frame)) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304608 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
David Barkerc9acf932018-04-17 12:54:34 +01004609 "Still pictures must be coded as shown keyframes");
4610 }
David Turnerd2a592e2018-11-16 14:59:31 +00004611 cm->showable_frame = current_frame->frame_type != KEY_FRAME;
Andrey Norkin795ba872018-03-06 13:24:14 -08004612 if (cm->show_frame) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004613 if (seq_params->decoder_model_info_present_flag &&
Urvang Joshi450a9a22020-03-31 16:00:22 -07004614 seq_params->timing_info.equal_picture_interval == 0)
Yaowu Xu96c0f982019-05-17 09:10:54 -07004615 read_temporal_point_info(cm, rb);
Andrey Norkin795ba872018-03-06 13:24:14 -08004616 } else {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004617 // See if this frame can be used as show_existing_frame in future
4618 cm->showable_frame = aom_rb_read_bit(rb);
4619 }
4620 cm->cur_frame->showable_frame = cm->showable_frame;
Urvang Joshib6409e92020-03-23 11:23:27 -07004621 features->error_resilient_mode =
David Turnerd2a592e2018-11-16 14:59:31 +00004622 frame_is_sframe(cm) ||
4623 (current_frame->frame_type == KEY_FRAME && cm->show_frame)
Debargha Mukherjeeae0b90c2018-04-10 18:00:41 -07004624 ? 1
4625 : aom_rb_read_bit(rb);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004626 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004627
Wan-Teh Chang58e4a152019-06-25 13:44:35 -07004628 if (current_frame->frame_type == KEY_FRAME && cm->show_frame) {
4629 /* All frames need to be marked as not valid for referencing */
4630 for (int i = 0; i < REF_FRAMES; i++) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004631 pbi->valid_for_referencing[i] = 0;
Wan-Teh Chang58e4a152019-06-25 13:44:35 -07004632 }
4633 }
Urvang Joshib6409e92020-03-23 11:23:27 -07004634 features->disable_cdf_update = aom_rb_read_bit(rb);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004635 if (seq_params->force_screen_content_tools == 2) {
Urvang Joshib6409e92020-03-23 11:23:27 -07004636 features->allow_screen_content_tools = aom_rb_read_bit(rb);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00004637 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07004638 features->allow_screen_content_tools =
4639 seq_params->force_screen_content_tools;
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00004640 }
4641
Urvang Joshib6409e92020-03-23 11:23:27 -07004642 if (features->allow_screen_content_tools) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004643 if (seq_params->force_integer_mv == 2) {
Urvang Joshib6409e92020-03-23 11:23:27 -07004644 features->cur_frame_force_integer_mv = aom_rb_read_bit(rb);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00004645 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07004646 features->cur_frame_force_integer_mv = seq_params->force_integer_mv;
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00004647 }
4648 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07004649 features->cur_frame_force_integer_mv = 0;
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00004650 }
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00004651
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004652 int frame_size_override_flag = 0;
Urvang Joshib6409e92020-03-23 11:23:27 -07004653 features->allow_intrabc = 0;
Urvang Joshi6237b882020-03-26 15:02:26 -07004654 features->primary_ref_frame = PRIMARY_REF_NONE;
Zoe Liu45a8fed2018-03-02 16:51:02 -08004655
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004656 if (!seq_params->reduced_still_picture_hdr) {
4657 if (seq_params->frame_id_numbers_present_flag) {
4658 int frame_id_length = seq_params->frame_id_length;
4659 int diff_len = seq_params->delta_frame_id_length;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004660 int prev_frame_id = 0;
David Turnerd2a592e2018-11-16 14:59:31 +00004661 int have_prev_frame_id =
4662 !pbi->decoding_first_frame &&
4663 !(current_frame->frame_type == KEY_FRAME && cm->show_frame);
David Barkerf6f8fa12018-06-20 14:46:05 +01004664 if (have_prev_frame_id) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004665 prev_frame_id = cm->current_frame_id;
4666 }
4667 cm->current_frame_id = aom_rb_read_literal(rb, frame_id_length);
Zoe Liu45a8fed2018-03-02 16:51:02 -08004668
David Barkerf6f8fa12018-06-20 14:46:05 +01004669 if (have_prev_frame_id) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004670 int diff_frame_id;
4671 if (cm->current_frame_id > prev_frame_id) {
4672 diff_frame_id = cm->current_frame_id - prev_frame_id;
4673 } else {
4674 diff_frame_id =
4675 (1 << frame_id_length) + cm->current_frame_id - prev_frame_id;
4676 }
4677 /* Check current_frame_id for conformance */
4678 if (prev_frame_id == cm->current_frame_id ||
4679 diff_frame_id >= (1 << (frame_id_length - 1))) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304680 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004681 "Invalid value of current_frame_id");
4682 }
4683 }
4684 /* Check if some frames need to be marked as not valid for referencing */
4685 for (int i = 0; i < REF_FRAMES; i++) {
Wan-Teh Chang58e4a152019-06-25 13:44:35 -07004686 if (cm->current_frame_id - (1 << diff_len) > 0) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004687 if (cm->ref_frame_id[i] > cm->current_frame_id ||
4688 cm->ref_frame_id[i] < cm->current_frame_id - (1 << diff_len))
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004689 pbi->valid_for_referencing[i] = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004690 } else {
4691 if (cm->ref_frame_id[i] > cm->current_frame_id &&
4692 cm->ref_frame_id[i] < (1 << frame_id_length) +
4693 cm->current_frame_id - (1 << diff_len))
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004694 pbi->valid_for_referencing[i] = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004695 }
4696 }
4697 }
4698
Wan-Teh Changb6680ff2018-07-27 14:50:25 -07004699 frame_size_override_flag = frame_is_sframe(cm) ? 1 : aom_rb_read_bit(rb);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004700
David Turnerd2a592e2018-11-16 14:59:31 +00004701 current_frame->order_hint = aom_rb_read_literal(
David Turnerebf96f42018-11-14 16:57:57 +00004702 rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
Yunqing Wangb397f7a2022-03-18 15:10:31 -07004703
4704 if (seq_params->order_hint_info.enable_order_hint)
4705 current_frame->frame_number = current_frame->order_hint;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004706
Urvang Joshib6409e92020-03-23 11:23:27 -07004707 if (!features->error_resilient_mode && !frame_is_intra_only(cm)) {
Urvang Joshi6237b882020-03-26 15:02:26 -07004708 features->primary_ref_frame = aom_rb_read_literal(rb, PRIMARY_REF_BITS);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004709 }
Yue Chend90d3432018-03-16 11:28:42 -07004710 }
4711
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004712 if (seq_params->decoder_model_info_present_flag) {
Tarundeep Singhd00b98f2021-04-21 16:13:51 +05304713 pbi->buffer_removal_time_present = aom_rb_read_bit(rb);
4714 if (pbi->buffer_removal_time_present) {
Adrian Grangec56f6ec2018-05-31 14:19:32 -07004715 for (int op_num = 0;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004716 op_num < seq_params->operating_points_cnt_minus_1 + 1; op_num++) {
Urvang Joshi450a9a22020-03-31 16:00:22 -07004717 if (seq_params->op_params[op_num].decoder_model_param_present_flag) {
Wan-Teh Chang62e8d9c2021-05-28 12:30:23 -07004718 if (seq_params->operating_point_idc[op_num] == 0 ||
4719 (((seq_params->operating_point_idc[op_num] >>
Adrian Grangec56f6ec2018-05-31 14:19:32 -07004720 cm->temporal_layer_id) &
4721 0x1) &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004722 ((seq_params->operating_point_idc[op_num] >>
Adrian Grangec56f6ec2018-05-31 14:19:32 -07004723 (cm->spatial_layer_id + 8)) &
Wan-Teh Chang62e8d9c2021-05-28 12:30:23 -07004724 0x1))) {
Urvang Joshi450a9a22020-03-31 16:00:22 -07004725 cm->buffer_removal_times[op_num] = aom_rb_read_unsigned_literal(
4726 rb, seq_params->decoder_model_info.buffer_removal_time_length);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07004727 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07004728 cm->buffer_removal_times[op_num] = 0;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07004729 }
4730 } else {
Urvang Joshi450a9a22020-03-31 16:00:22 -07004731 cm->buffer_removal_times[op_num] = 0;
Andrey Norkin795ba872018-03-06 13:24:14 -08004732 }
4733 }
4734 }
4735 }
David Turnerd2a592e2018-11-16 14:59:31 +00004736 if (current_frame->frame_type == KEY_FRAME) {
David Turner996b2c12018-12-07 15:52:30 +00004737 if (!cm->show_frame) { // unshown keyframe (forward keyframe)
4738 current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
4739 } else { // shown keyframe
4740 current_frame->refresh_frame_flags = (1 << REF_FRAMES) - 1;
4741 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004742
Sebastien Alaiwanf518f142017-12-01 14:25:41 +01004743 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
David Turnera21966b2018-12-05 14:48:49 +00004744 cm->remapped_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004745 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004746 if (pbi->need_resync) {
Wan-Teh Chang38353c82018-09-27 18:46:55 -07004747 reset_ref_frame_map(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004748 pbi->need_resync = 0;
4749 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004750 } else {
David Turner45f416c2018-11-15 11:34:30 +00004751 if (current_frame->frame_type == INTRA_ONLY_FRAME) {
David Turner996b2c12018-12-07 15:52:30 +00004752 current_frame->refresh_frame_flags = aom_rb_read_literal(rb, REF_FRAMES);
4753 if (current_frame->refresh_frame_flags == 0xFF) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304754 aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
Debargha Mukherjee33f42652018-04-12 14:27:32 -07004755 "Intra only frames cannot have refresh flags 0xFF");
4756 }
4757 if (pbi->need_resync) {
Wan-Teh Chang38353c82018-09-27 18:46:55 -07004758 reset_ref_frame_map(cm);
Debargha Mukherjee33f42652018-04-12 14:27:32 -07004759 pbi->need_resync = 0;
4760 }
4761 } else if (pbi->need_resync != 1) { /* Skip if need resync */
David Turner996b2c12018-12-07 15:52:30 +00004762 current_frame->refresh_frame_flags =
Debargha Mukherjee33f42652018-04-12 14:27:32 -07004763 frame_is_sframe(cm) ? 0xFF : aom_rb_read_literal(rb, REF_FRAMES);
Debargha Mukherjee33f42652018-04-12 14:27:32 -07004764 }
4765 }
4766
David Turner996b2c12018-12-07 15:52:30 +00004767 if (!frame_is_intra_only(cm) || current_frame->refresh_frame_flags != 0xFF) {
Imdad Sardharwalla21cc90b2018-03-12 16:39:38 +00004768 // Read all ref frame order hints if error_resilient_mode == 1
Urvang Joshib6409e92020-03-23 11:23:27 -07004769 if (features->error_resilient_mode &&
David Turnerebf96f42018-11-14 16:57:57 +00004770 seq_params->order_hint_info.enable_order_hint) {
Imdad Sardharwalla21cc90b2018-03-12 16:39:38 +00004771 for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
Imdad Sardharwalla21cc90b2018-03-12 16:39:38 +00004772 // Read order hint from bit stream
David Turner1bcefb32018-11-19 17:54:00 +00004773 unsigned int order_hint = aom_rb_read_literal(
David Turnerebf96f42018-11-14 16:57:57 +00004774 rb, seq_params->order_hint_info.order_hint_bits_minus_1 + 1);
David Turnere7ebf902018-12-04 14:04:55 +00004775 // Get buffer
4776 RefCntBuffer *buf = cm->ref_frame_map[ref_idx];
4777 if (buf == NULL || order_hint != buf->order_hint) {
4778 if (buf != NULL) {
Debargha Mukherjeeeeda6d22018-04-02 11:21:13 -07004779 lock_buffer_pool(pool);
David Turnere7ebf902018-12-04 14:04:55 +00004780 decrease_ref_count(buf, pool);
Debargha Mukherjeeeeda6d22018-04-02 11:21:13 -07004781 unlock_buffer_pool(pool);
Wan-Teh Changee70d942019-06-25 18:02:47 -07004782 cm->ref_frame_map[ref_idx] = NULL;
Debargha Mukherjeeeeda6d22018-04-02 11:21:13 -07004783 }
Imdad Sardharwalla714b2682018-03-12 17:20:09 +00004784 // If no corresponding buffer exists, allocate a new buffer with all
4785 // pixels set to neutral grey.
David Turnere7ebf902018-12-04 14:04:55 +00004786 int buf_idx = get_free_fb(cm);
Wan-Teh Chang7b1404ab2018-04-24 17:47:17 -07004787 if (buf_idx == INVALID_IDX) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304788 aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
Wan-Teh Chang7b1404ab2018-04-24 17:47:17 -07004789 "Unable to find free frame buffer");
4790 }
David Turnere7ebf902018-12-04 14:04:55 +00004791 buf = &frame_bufs[buf_idx];
Debargha Mukherjeeeeda6d22018-04-02 11:21:13 -07004792 lock_buffer_pool(pool);
4793 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00004794 &buf->buf, seq_params->max_frame_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004795 seq_params->max_frame_height, seq_params->subsampling_x,
4796 seq_params->subsampling_y, seq_params->use_highbitdepth,
Urvang Joshi6237b882020-03-26 15:02:26 -07004797 AOM_BORDER_IN_PIXELS, features->byte_alignment,
Rachel Barker72e349e2024-02-13 14:31:10 +00004798 &buf->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv, false,
Cherma Rajan Ad3b15712022-04-11 17:02:19 +05304799 0)) {
David Turnere7ebf902018-12-04 14:04:55 +00004800 decrease_ref_count(buf, pool);
Debargha Mukherjeeeeda6d22018-04-02 11:21:13 -07004801 unlock_buffer_pool(pool);
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304802 aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjeeeeda6d22018-04-02 11:21:13 -07004803 "Failed to allocate frame buffer");
4804 }
4805 unlock_buffer_pool(pool);
Yaowu Xuc8a9f552020-03-23 08:41:35 -07004806 // According to the specification, valid bitstreams are required to
4807 // never use missing reference frames so the filling process for
4808 // missing frames is not normatively defined and RefValid for missing
4809 // frames is set to 0.
Imdad Sardharwalla714b2682018-03-12 17:20:09 +00004810
Yaowu Xuc8a9f552020-03-23 08:41:35 -07004811 // To make libaom more robust when the bitstream has been corrupted
4812 // by the loss of some frames of data, this code adds a neutral grey
4813 // buffer in place of missing frames, i.e.
4814 //
4815 set_planes_to_neutral_grey(seq_params, &buf->buf, 0);
4816 //
4817 // and allows the frames to be used for referencing, i.e.
4818 //
4819 pbi->valid_for_referencing[ref_idx] = 1;
4820 //
4821 // Please note such behavior is not normative and other decoders may
4822 // use a different approach.
David Turnere7ebf902018-12-04 14:04:55 +00004823 cm->ref_frame_map[ref_idx] = buf;
4824 buf->order_hint = order_hint;
Imdad Sardharwalla714b2682018-03-12 17:20:09 +00004825 }
Imdad Sardharwalla21cc90b2018-03-12 16:39:38 +00004826 }
4827 }
Debargha Mukherjee33f42652018-04-12 14:27:32 -07004828 }
Imdad Sardharwalla21cc90b2018-03-12 16:39:38 +00004829
David Turnerd2a592e2018-11-16 14:59:31 +00004830 if (current_frame->frame_type == KEY_FRAME) {
Debargha Mukherjee33f42652018-04-12 14:27:32 -07004831 setup_frame_size(cm, frame_size_override_flag, rb);
4832
Urvang Joshib6409e92020-03-23 11:23:27 -07004833 if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
4834 features->allow_intrabc = aom_rb_read_bit(rb);
4835 features->allow_ref_frame_mvs = 0;
Debargha Mukherjee33f42652018-04-12 14:27:32 -07004836 cm->prev_frame = NULL;
4837 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07004838 features->allow_ref_frame_mvs = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004839
David Turner45f416c2018-11-15 11:34:30 +00004840 if (current_frame->frame_type == INTRA_ONLY_FRAME) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07004841 cm->cur_frame->film_grain_params_present =
4842 seq_params->film_grain_params_present;
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01004843 setup_frame_size(cm, frame_size_override_flag, rb);
Urvang Joshib6409e92020-03-23 11:23:27 -07004844 if (features->allow_screen_content_tools && !av1_superres_scaled(cm))
4845 features->allow_intrabc = aom_rb_read_bit(rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004846
Debargha Mukherjee33f42652018-04-12 14:27:32 -07004847 } else if (pbi->need_resync != 1) { /* Skip if need resync */
David Turner45f416c2018-11-15 11:34:30 +00004848 int frame_refs_short_signaling = 0;
Zoe Liu592d73e2018-02-20 14:06:44 -08004849 // Frame refs short signaling is off when error resilient mode is on.
David Turnerebf96f42018-11-14 16:57:57 +00004850 if (seq_params->order_hint_info.enable_order_hint)
David Turner45f416c2018-11-15 11:34:30 +00004851 frame_refs_short_signaling = aom_rb_read_bit(rb);
Zoe Liub57fee02018-02-15 17:41:41 -08004852
David Turner45f416c2018-11-15 11:34:30 +00004853 if (frame_refs_short_signaling) {
Zoe Liub57fee02018-02-15 17:41:41 -08004854 // == LAST_FRAME ==
4855 const int lst_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
David Turnere7ebf902018-12-04 14:04:55 +00004856 const RefCntBuffer *const lst_buf = cm->ref_frame_map[lst_ref];
Zoe Liub57fee02018-02-15 17:41:41 -08004857
4858 // == GOLDEN_FRAME ==
4859 const int gld_ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
David Turnere7ebf902018-12-04 14:04:55 +00004860 const RefCntBuffer *const gld_buf = cm->ref_frame_map[gld_ref];
Rupert Swarbrick5eb471c2017-10-02 16:06:54 +01004861
4862 // Most of the time, streams start with a keyframe. In that case,
4863 // ref_frame_map will have been filled in at that point and will not
David Turnere7ebf902018-12-04 14:04:55 +00004864 // contain any NULLs. However, streams are explicitly allowed to start
Rupert Swarbrick5eb471c2017-10-02 16:06:54 +01004865 // with an intra-only frame, so long as they don't then signal a
4866 // reference to a slot that hasn't been set yet. That's what we are
4867 // checking here.
David Turnere7ebf902018-12-04 14:04:55 +00004868 if (lst_buf == NULL)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304869 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Zoe Liub57fee02018-02-15 17:41:41 -08004870 "Inter frame requests nonexistent reference");
David Turnere7ebf902018-12-04 14:04:55 +00004871 if (gld_buf == NULL)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304872 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Rupert Swarbrick5eb471c2017-10-02 16:06:54 +01004873 "Inter frame requests nonexistent reference");
4874
David Turner73245762019-02-11 16:42:34 +00004875 av1_set_frame_refs(cm, cm->remapped_ref_idx, lst_ref, gld_ref);
Zoe Liub57fee02018-02-15 17:41:41 -08004876 }
Zoe Liub57fee02018-02-15 17:41:41 -08004877
4878 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
4879 int ref = 0;
David Turner45f416c2018-11-15 11:34:30 +00004880 if (!frame_refs_short_signaling) {
Zoe Liub57fee02018-02-15 17:41:41 -08004881 ref = aom_rb_read_literal(rb, REF_FRAMES_LOG2);
Zoe Liub57fee02018-02-15 17:41:41 -08004882
4883 // Most of the time, streams start with a keyframe. In that case,
4884 // ref_frame_map will have been filled in at that point and will not
Wan-Teh Chang62e4bbd2019-03-22 12:52:33 -07004885 // contain any NULLs. However, streams are explicitly allowed to start
Zoe Liub57fee02018-02-15 17:41:41 -08004886 // with an intra-only frame, so long as they don't then signal a
4887 // reference to a slot that hasn't been set yet. That's what we are
4888 // checking here.
David Turnera21966b2018-12-05 14:48:49 +00004889 if (cm->ref_frame_map[ref] == NULL)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304890 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Zoe Liub57fee02018-02-15 17:41:41 -08004891 "Inter frame requests nonexistent reference");
David Turnera21966b2018-12-05 14:48:49 +00004892 cm->remapped_ref_idx[i] = ref;
Zoe Liub57fee02018-02-15 17:41:41 -08004893 } else {
David Turnera21966b2018-12-05 14:48:49 +00004894 ref = cm->remapped_ref_idx[i];
Zoe Liub57fee02018-02-15 17:41:41 -08004895 }
Wan-Teh Chang58e4a152019-06-25 13:44:35 -07004896 // Check valid for referencing
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004897 if (pbi->valid_for_referencing[ref] == 0)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304898 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Wan-Teh Chang58e4a152019-06-25 13:44:35 -07004899 "Reference frame not valid for referencing");
Zoe Liub57fee02018-02-15 17:41:41 -08004900
Zoe Liu17af2742017-10-06 10:36:42 -07004901 cm->ref_frame_sign_bias[LAST_FRAME + i] = 0;
Zoe Liub57fee02018-02-15 17:41:41 -08004902
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004903 if (seq_params->frame_id_numbers_present_flag) {
4904 int frame_id_length = seq_params->frame_id_length;
4905 int diff_len = seq_params->delta_frame_id_length;
Wan-Teh Chang69582972018-05-15 13:18:52 -07004906 int delta_frame_id_minus_1 = aom_rb_read_literal(rb, diff_len);
David Barker5e70a112017-10-03 14:28:17 +01004907 int ref_frame_id =
Wan-Teh Chang69582972018-05-15 13:18:52 -07004908 ((cm->current_frame_id - (delta_frame_id_minus_1 + 1) +
David Barker5e70a112017-10-03 14:28:17 +01004909 (1 << frame_id_length)) %
4910 (1 << frame_id_length));
Wan-Teh Chang69582972018-05-15 13:18:52 -07004911 // Compare values derived from delta_frame_id_minus_1 and
Wan-Teh Chang58e4a152019-06-25 13:44:35 -07004912 // refresh_frame_flags.
4913 if (ref_frame_id != cm->ref_frame_id[ref])
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304914 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
David Barker5e70a112017-10-03 14:28:17 +01004915 "Reference buffer frame ID mismatch");
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004916 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004917 }
4918
Urvang Joshib6409e92020-03-23 11:23:27 -07004919 if (!features->error_resilient_mode && frame_size_override_flag) {
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004920 setup_frame_size_with_refs(cm, rb);
4921 } else {
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01004922 setup_frame_size(cm, frame_size_override_flag, rb);
Arild Fuldseth842e9b02016-09-02 13:00:05 +02004923 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004924
Urvang Joshib6409e92020-03-23 11:23:27 -07004925 if (features->cur_frame_force_integer_mv) {
4926 features->allow_high_precision_mv = 0;
RogerZhou10a03802017-10-26 11:49:48 -07004927 } else {
Urvang Joshib6409e92020-03-23 11:23:27 -07004928 features->allow_high_precision_mv = aom_rb_read_bit(rb);
RogerZhou10a03802017-10-26 11:49:48 -07004929 }
Urvang Joshi6237b882020-03-26 15:02:26 -07004930 features->interp_filter = read_frame_interp_filter(rb);
4931 features->switchable_motion_mode = aom_rb_read_bit(rb);
Yue Chend90d3432018-03-16 11:28:42 -07004932 }
4933
David Turnera21966b2018-12-05 14:48:49 +00004934 cm->prev_frame = get_primary_ref_frame_buf(cm);
Urvang Joshi6237b882020-03-26 15:02:26 -07004935 if (features->primary_ref_frame != PRIMARY_REF_NONE &&
David Turnera21966b2018-12-05 14:48:49 +00004936 get_primary_ref_frame_buf(cm) == NULL) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304937 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
David Barkercc615a82018-03-19 14:38:51 +00004938 "Reference frame containing this frame's initial "
4939 "frame context is unavailable.");
Yue Chend90d3432018-03-16 11:28:42 -07004940 }
4941
David Turner45f416c2018-11-15 11:34:30 +00004942 if (!(current_frame->frame_type == INTRA_ONLY_FRAME) &&
4943 pbi->need_resync != 1) {
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004944 if (frame_might_allow_ref_frame_mvs(cm))
Urvang Joshib6409e92020-03-23 11:23:27 -07004945 features->allow_ref_frame_mvs = aom_rb_read_bit(rb);
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01004946 else
Urvang Joshib6409e92020-03-23 11:23:27 -07004947 features->allow_ref_frame_mvs = 0;
Jingning Hane17ebe92017-11-03 15:25:42 -07004948
David Turnera21966b2018-12-05 14:48:49 +00004949 for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4950 const RefCntBuffer *const ref_buf = get_ref_frame_buf(cm, i);
4951 struct scale_factors *const ref_scale_factors =
4952 get_ref_scale_factors(cm, i);
Yaowu Xuf883b422016-08-30 14:01:10 -07004953 av1_setup_scale_factors_for_frame(
David Turnera21966b2018-12-05 14:48:49 +00004954 ref_scale_factors, ref_buf->buf.y_crop_width,
4955 ref_buf->buf.y_crop_height, cm->width, cm->height);
4956 if ((!av1_is_valid_scale(ref_scale_factors)))
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304957 aom_internal_error(&pbi->error, AOM_CODEC_UNSUP_BITSTREAM,
Debargha Mukherjee4f51ce82018-04-17 07:16:55 -07004958 "Reference frame has invalid dimensions");
Yaowu Xuc27fc142016-08-22 16:08:15 -07004959 }
4960 }
4961 }
Jingning Hanc723b342017-08-24 11:19:46 -07004962
Zoe Liu17af2742017-10-06 10:36:42 -07004963 av1_setup_frame_buf_refs(cm);
4964
Debargha Mukherjeed30cde52018-03-20 17:35:20 -07004965 av1_setup_frame_sign_bias(cm);
Jingning Hanc723b342017-08-24 11:19:46 -07004966
David Turnerd2a592e2018-11-16 14:59:31 +00004967 cm->cur_frame->frame_type = current_frame->frame_type;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004968
Urvang Joshi1de67aa2020-03-20 11:21:57 -07004969 update_ref_frame_id(pbi);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004970
Urvang Joshib6409e92020-03-23 11:23:27 -07004971 const int might_bwd_adapt = !(seq_params->reduced_still_picture_hdr) &&
4972 !(features->disable_cdf_update);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004973 if (might_bwd_adapt) {
Urvang Joshi6237b882020-03-26 15:02:26 -07004974 features->refresh_frame_context = aom_rb_read_bit(rb)
4975 ? REFRESH_FRAME_CONTEXT_DISABLED
4976 : REFRESH_FRAME_CONTEXT_BACKWARD;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004977 } else {
Urvang Joshi6237b882020-03-26 15:02:26 -07004978 features->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07004979 }
4980
Jack Haughtonddb80602018-11-21 16:41:49 +00004981 cm->cur_frame->buf.bit_depth = seq_params->bit_depth;
4982 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
4983 cm->cur_frame->buf.transfer_characteristics =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004984 seq_params->transfer_characteristics;
Jack Haughtonddb80602018-11-21 16:41:49 +00004985 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
4986 cm->cur_frame->buf.monochrome = seq_params->monochrome;
4987 cm->cur_frame->buf.chroma_sample_position =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004988 seq_params->chroma_sample_position;
Jack Haughtonddb80602018-11-21 16:41:49 +00004989 cm->cur_frame->buf.color_range = seq_params->color_range;
4990 cm->cur_frame->buf.render_width = cm->render_width;
4991 cm->cur_frame->buf.render_height = cm->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004992
4993 if (pbi->need_resync) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05304994 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004995 "Keyframe / intra-only frame required to reset decoder"
4996 " state");
4997 }
4998
Urvang Joshib6409e92020-03-23 11:23:27 -07004999 if (features->allow_intrabc) {
Hui Su27df8342017-11-07 15:16:05 -08005000 // Set parameters corresponding to no filtering.
5001 struct loopfilter *lf = &cm->lf;
Hui Su27df8342017-11-07 15:16:05 -08005002 lf->filter_level[0] = 0;
5003 lf->filter_level[1] = 0;
David Turnerebf96f42018-11-14 16:57:57 +00005004 cm->cdef_info.cdef_bits = 0;
5005 cm->cdef_info.cdef_strengths[0] = 0;
5006 cm->cdef_info.nb_cdef_strengths = 1;
5007 cm->cdef_info.cdef_uv_strengths[0] = 0;
Hui Su27df8342017-11-07 15:16:05 -08005008 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5009 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5010 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su27df8342017-11-07 15:16:05 -08005011 }
Hui Su27df8342017-11-07 15:16:05 -08005012
Cyril Concolato3b5afc12017-12-15 12:54:15 -08005013 read_tile_info(pbi, rb);
Yaowu Xu7e450882019-04-30 15:09:18 -07005014 if (!av1_is_min_tile_width_satisfied(cm)) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05305015 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Debargha Mukherjeea082f762019-03-04 15:05:18 -08005016 "Minimum tile width requirement not satisfied");
5017 }
5018
Urvang Joshi17814622020-03-27 17:26:17 -07005019 CommonQuantParams *const quant_params = &cm->quant_params;
5020 setup_quantization(quant_params, av1_num_planes(cm),
Tarundeep Singh4243e622021-04-20 16:10:22 +05305021 cm->seq_params->separate_uv_delta_q, rb);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005022 xd->bd = (int)seq_params->bit_depth;
David Barkercc615a82018-03-19 14:38:51 +00005023
Urvang Joshi5c8625a2020-03-30 13:16:37 -07005024 CommonContexts *const above_contexts = &cm->above_contexts;
5025 if (above_contexts->num_planes < av1_num_planes(cm) ||
5026 above_contexts->num_mi_cols < cm->mi_params.mi_cols ||
5027 above_contexts->num_tile_rows < cm->tiles.rows) {
5028 av1_free_above_context_buffers(above_contexts);
5029 if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows,
5030 cm->mi_params.mi_cols,
5031 av1_num_planes(cm))) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05305032 aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
Cherma Rajan A71d20db2018-04-27 11:15:32 +05305033 "Failed to allocate context buffers");
Urvang Joshi5c8625a2020-03-30 13:16:37 -07005034 }
Cherma Rajan A71d20db2018-04-27 11:15:32 +05305035 }
5036
Urvang Joshi6237b882020-03-26 15:02:26 -07005037 if (features->primary_ref_frame == PRIMARY_REF_NONE) {
David Barkere0d56e62018-03-07 13:56:19 +00005038 av1_setup_past_independence(cm);
David Barkere0d56e62018-03-07 13:56:19 +00005039 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005040
5041 setup_segmentation(cm, rb);
5042
David Turnerebf96f42018-11-14 16:57:57 +00005043 cm->delta_q_info.delta_q_res = 1;
5044 cm->delta_q_info.delta_lf_res = 1;
5045 cm->delta_q_info.delta_lf_present_flag = 0;
5046 cm->delta_q_info.delta_lf_multi = 0;
5047 cm->delta_q_info.delta_q_present_flag =
Urvang Joshi17814622020-03-27 17:26:17 -07005048 quant_params->base_qindex > 0 ? aom_rb_read_bit(rb) : 0;
David Turnerebf96f42018-11-14 16:57:57 +00005049 if (cm->delta_q_info.delta_q_present_flag) {
Urvang Joshiacad1ca2020-04-27 17:03:25 -07005050 xd->current_base_qindex = quant_params->base_qindex;
David Turnerebf96f42018-11-14 16:57:57 +00005051 cm->delta_q_info.delta_q_res = 1 << aom_rb_read_literal(rb, 2);
Urvang Joshib6409e92020-03-23 11:23:27 -07005052 if (!features->allow_intrabc)
David Turnerebf96f42018-11-14 16:57:57 +00005053 cm->delta_q_info.delta_lf_present_flag = aom_rb_read_bit(rb);
5054 if (cm->delta_q_info.delta_lf_present_flag) {
5055 cm->delta_q_info.delta_lf_res = 1 << aom_rb_read_literal(rb, 2);
5056 cm->delta_q_info.delta_lf_multi = aom_rb_read_bit(rb);
Wan-Teh Chang69ac7eb2018-06-08 09:41:37 -07005057 av1_reset_loop_filter_delta(xd, av1_num_planes(cm));
Arild Fuldseth07441162016-08-15 15:07:52 +02005058 }
5059 }
Yue Chen6bec0572018-05-15 09:53:35 -07005060
Urvang Joshib6409e92020-03-23 11:23:27 -07005061 xd->cur_frame_force_integer_mv = features->cur_frame_force_integer_mv;
Thomas Davies3ab20b42017-09-19 10:30:53 +01005062
Sebastien Alaiwanf518f142017-12-01 14:25:41 +01005063 for (int i = 0; i < MAX_SEGMENTS; ++i) {
Urvang Joshi17814622020-03-27 17:26:17 -07005064 const int qindex = av1_get_qindex(&cm->seg, i, quant_params->base_qindex);
5065 xd->lossless[i] =
5066 qindex == 0 && quant_params->y_dc_delta_q == 0 &&
5067 quant_params->u_dc_delta_q == 0 && quant_params->u_ac_delta_q == 0 &&
5068 quant_params->v_dc_delta_q == 0 && quant_params->v_ac_delta_q == 0;
Urvang Joshi454280d2016-10-14 16:51:44 -07005069 xd->qindex[i] = qindex;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005070 }
Urvang Joshib6409e92020-03-23 11:23:27 -07005071 features->coded_lossless = is_coded_lossless(cm, xd);
5072 features->all_lossless = features->coded_lossless && !av1_superres_scaled(cm);
Wan-Teh Chang98c01e02018-10-18 09:48:27 -07005073 setup_segmentation_dequant(cm, xd);
Urvang Joshib6409e92020-03-23 11:23:27 -07005074 if (features->coded_lossless) {
Jonathan Matthewsbfd24542018-03-02 11:20:15 +00005075 cm->lf.filter_level[0] = 0;
5076 cm->lf.filter_level[1] = 0;
Debargha Mukherjee98a311c2018-03-25 16:33:11 -07005077 }
Urvang Joshib6409e92020-03-23 11:23:27 -07005078 if (features->coded_lossless || !seq_params->enable_cdef) {
David Turnerebf96f42018-11-14 16:57:57 +00005079 cm->cdef_info.cdef_bits = 0;
5080 cm->cdef_info.cdef_strengths[0] = 0;
5081 cm->cdef_info.cdef_uv_strengths[0] = 0;
Urvang Joshic8b52d52018-03-23 13:16:51 -07005082 }
Urvang Joshib6409e92020-03-23 11:23:27 -07005083 if (features->all_lossless || !seq_params->enable_restoration) {
Jonathan Matthewsbfd24542018-03-02 11:20:15 +00005084 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5085 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5086 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Debargha Mukherjee63bcfa72018-03-20 15:25:22 -07005087 }
5088 setup_loopfilter(cm, rb);
5089
Urvang Joshib6409e92020-03-23 11:23:27 -07005090 if (!features->coded_lossless && seq_params->enable_cdef) {
Thomas Daedef636d5c2017-06-29 13:48:27 -07005091 setup_cdef(cm, rb);
Urvang Joshic8b52d52018-03-23 13:16:51 -07005092 }
Urvang Joshib6409e92020-03-23 11:23:27 -07005093 if (!features->all_lossless && seq_params->enable_restoration) {
Urvang Joshi5ec7b812018-02-28 14:37:06 -08005094 decode_restoration_mode(cm, rb);
Thomas Daedef636d5c2017-06-29 13:48:27 -07005095 }
Urvang Joshi5ec7b812018-02-28 14:37:06 -08005096
Urvang Joshi6237b882020-03-26 15:02:26 -07005097 features->tx_mode = read_tx_mode(rb, features->coded_lossless);
David Turnerd2a592e2018-11-16 14:59:31 +00005098 current_frame->reference_mode = read_frame_reference_mode(cm, rb);
Zoe Liu4b847e12017-12-07 12:44:45 -08005099
Zoe Liu4b847e12017-12-07 12:44:45 -08005100 av1_setup_skip_mode_allowed(cm);
David Turnerd2a592e2018-11-16 14:59:31 +00005101 current_frame->skip_mode_info.skip_mode_flag =
5102 current_frame->skip_mode_info.skip_mode_allowed ? aom_rb_read_bit(rb) : 0;
Zoe Liu4b847e12017-12-07 12:44:45 -08005103
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005104 if (frame_might_allow_warped_motion(cm))
Urvang Joshib6409e92020-03-23 11:23:27 -07005105 features->allow_warped_motion = aom_rb_read_bit(rb);
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07005106 else
Urvang Joshib6409e92020-03-23 11:23:27 -07005107 features->allow_warped_motion = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005108
Urvang Joshib6409e92020-03-23 11:23:27 -07005109 features->reduced_tx_set_used = aom_rb_read_bit(rb);
Sarah Parkere68a3e42017-02-16 14:03:24 -08005110
Urvang Joshib6409e92020-03-23 11:23:27 -07005111 if (features->allow_ref_frame_mvs && !frame_might_allow_ref_frame_mvs(cm)) {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05305112 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005113 "Frame wrongly requests reference frame MVs");
Pavel Frolov57c36e12017-09-12 15:00:40 +03005114 }
Pavel Frolov57c36e12017-09-12 15:00:40 +03005115
Sarah Parkerf289f9f2017-09-12 18:50:02 -07005116 if (!frame_is_intra_only(cm)) read_global_motion(cm, rb);
Sarah Parker3e579a62017-08-23 16:53:20 -07005117
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07005118 cm->cur_frame->film_grain_params_present =
5119 seq_params->film_grain_params_present;
David Barkere8cedfa2018-04-09 14:54:13 +01005120 read_film_grain(cm, rb);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08005121
Yunqing Wang445739a2018-06-10 12:27:34 -07005122#if EXT_TILE_DEBUG
Urvang Joshi54ffae72020-03-23 13:37:10 -07005123 if (pbi->ext_tile_debug && cm->tiles.large_scale) {
Yunqing Wang2f7b7a92018-06-11 10:07:23 -07005124 read_ext_tile_info(pbi, rb);
5125 av1_set_single_tile_decoding_mode(cm);
5126 }
Yunqing Wang445739a2018-06-10 12:27:34 -07005127#endif // EXT_TILE_DEBUG
Thomas Daviese7154832017-10-03 10:12:17 +01005128 return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005129}
Debargha Mukherjee2eada612017-09-22 15:37:39 -07005130
Sebastien Alaiwane9644be2017-12-19 18:20:12 +01005131struct aom_read_bit_buffer *av1_init_read_bit_buffer(
Yaowu Xuf883b422016-08-30 14:01:10 -07005132 AV1Decoder *pbi, struct aom_read_bit_buffer *rb, const uint8_t *data,
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +01005133 const uint8_t *data_end) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005134 rb->bit_offset = 0;
5135 rb->error_handler = error_handler;
5136 rb->error_handler_data = &pbi->common;
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +01005137 rb->bit_buffer = data;
5138 rb->bit_buffer_end = data_end;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005139 return rb;
5140}
5141
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +01005142void av1_read_frame_size(struct aom_read_bit_buffer *rb, int num_bits_width,
5143 int num_bits_height, int *width, int *height) {
5144 *width = aom_rb_read_literal(rb, num_bits_width) + 1;
5145 *height = aom_rb_read_literal(rb, num_bits_height) + 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005146}
5147
Yaowu Xuf883b422016-08-30 14:01:10 -07005148BITSTREAM_PROFILE av1_read_profile(struct aom_read_bit_buffer *rb) {
Debargha Mukherjee53396fb2018-03-30 12:19:38 -07005149 int profile = aom_rb_read_literal(rb, PROFILE_BITS);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005150 return (BITSTREAM_PROFILE)profile;
5151}
5152
Wan-Teh Chang94534432024-08-08 20:29:57 -07005153static inline void superres_post_decode(AV1Decoder *pbi) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005154 AV1_COMMON *const cm = &pbi->common;
5155 BufferPool *const pool = cm->buffer_pool;
5156
Cheng Chen09c83a52018-06-05 12:27:36 -07005157 if (!av1_superres_scaled(cm)) return;
Urvang Joshib6409e92020-03-23 11:23:27 -07005158 assert(!cm->features.all_lossless);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005159
Rachel Barker674eaa02022-12-22 16:03:44 +00005160 av1_superres_upscale(cm, pool, 0);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005161}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005162
Wan-Teh Changbb6192a2018-07-10 11:01:04 -07005163uint32_t av1_decode_frame_headers_and_setup(AV1Decoder *pbi,
5164 struct aom_read_bit_buffer *rb,
Wan-Teh Changbb6192a2018-07-10 11:01:04 -07005165 int trailing_bits_present) {
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005166 AV1_COMMON *const cm = &pbi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005167 const int num_planes = av1_num_planes(cm);
Urvang Joshibac1dea2020-04-20 11:37:15 -07005168 MACROBLOCKD *const xd = &pbi->dcb.xd;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005169
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005170#if CONFIG_BITSTREAM_DEBUG
Wan-Teh Chang2e1c16e2022-03-30 18:27:20 -07005171 if (cm->seq_params->order_hint_info.enable_order_hint) {
Yunqing Wangb397f7a2022-03-18 15:10:31 -07005172 aom_bitstream_queue_set_frame_read(cm->current_frame.order_hint * 2 +
5173 cm->show_frame);
Wan-Teh Chang2e1c16e2022-03-30 18:27:20 -07005174 } else {
Yunqing Wangb397f7a2022-03-18 15:10:31 -07005175 // This is currently used in RTC encoding. cm->show_frame is always 1.
Wan-Teh Chang2e1c16e2022-03-30 18:27:20 -07005176 assert(cm->show_frame);
Yunqing Wangb397f7a2022-03-18 15:10:31 -07005177 aom_bitstream_queue_set_frame_read(cm->current_frame.frame_number);
Wan-Teh Chang2e1c16e2022-03-30 18:27:20 -07005178 }
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005179#endif
Angie Chiang5b5f4df2017-12-06 10:41:12 -08005180#if CONFIG_MISMATCH_DEBUG
5181 mismatch_move_frame_idx_r();
5182#endif
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005183
Sebastien Alaiwanf518f142017-12-01 14:25:41 +01005184 for (int i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01005185 cm->global_motion[i] = default_warp_params;
5186 cm->cur_frame->global_motion[i] = default_warp_params;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005187 }
5188 xd->global_motion = cm->global_motion;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005189
Yaowu Xuc4071232018-03-30 12:53:22 -07005190 read_uncompressed_header(pbi, rb);
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005191
Vignesh Venkatasubramanianbd7b0e32018-04-10 11:31:59 -07005192 if (trailing_bits_present) av1_check_trailing_bits(pbi, rb);
Jingning Han8a59bd52018-03-22 21:30:31 -07005193
Urvang Joshi54ffae72020-03-23 13:37:10 -07005194 if (!cm->tiles.single_tile_decoding &&
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005195 (pbi->dec_tile_row >= 0 || pbi->dec_tile_col >= 0)) {
5196 pbi->dec_tile_row = -1;
5197 pbi->dec_tile_col = -1;
5198 }
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005199
Wan-Teh Changbb6192a2018-07-10 11:01:04 -07005200 const uint32_t uncomp_hdr_size =
5201 (uint32_t)aom_rb_bytes_read(rb); // Size of the uncompressed header
Jack Haughtonddb80602018-11-21 16:41:49 +00005202 YV12_BUFFER_CONFIG *new_fb = &cm->cur_frame->buf;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005203 xd->cur_buf = new_fb;
Hui Sue8eb3a72018-03-17 17:46:21 -07005204 if (av1_allow_intrabc(cm)) {
Hui Su5ebd8702018-01-08 18:09:20 -08005205 av1_setup_scale_factors_for_frame(
5206 &cm->sf_identity, xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08005207 xd->cur_buf->y_crop_width, xd->cur_buf->y_crop_height);
Hui Su5ebd8702018-01-08 18:09:20 -08005208 }
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005209
Elliott Karpilovsky1d1bc842020-12-21 21:20:43 -08005210 // Showing a frame directly.
Debargha Mukherjee2eada612017-09-22 15:37:39 -07005211 if (cm->show_existing_frame) {
David Turner475a3132019-01-18 15:17:17 +00005212 if (pbi->reset_decoder_state) {
Zoe Liua7c1b192017-12-19 16:59:38 -08005213 // Use the default frame context values.
David Turner1bcefb32018-11-19 17:54:00 +00005214 *cm->fc = *cm->default_frame_context;
Zoe Liua7c1b192017-12-19 16:59:38 -08005215 if (!cm->fc->initialized)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05305216 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Zoe Liua7c1b192017-12-19 16:59:38 -08005217 "Uninitialized entropy context.");
5218 }
Wan-Teh Changbb6192a2018-07-10 11:01:04 -07005219 return uncomp_hdr_size;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005220 }
5221
Urvang Joshi9dc909d2020-03-23 16:07:02 -07005222 cm->mi_params.setup_mi(&cm->mi_params);
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005223
Yunqing Wangfa74d0e2021-07-29 16:41:37 -07005224 av1_calculate_ref_frame_side(cm);
5225 if (cm->features.allow_ref_frame_mvs) av1_setup_motion_field(cm);
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005226
Tarundeep Singh4243e622021-04-20 16:10:22 +05305227 av1_setup_block_planes(xd, cm->seq_params->subsampling_x,
5228 cm->seq_params->subsampling_y, num_planes);
Urvang Joshi6237b882020-03-26 15:02:26 -07005229 if (cm->features.primary_ref_frame == PRIMARY_REF_NONE) {
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005230 // use the default frame context values
David Turner1bcefb32018-11-19 17:54:00 +00005231 *cm->fc = *cm->default_frame_context;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005232 } else {
David Turnera21966b2018-12-05 14:48:49 +00005233 *cm->fc = get_primary_ref_frame_buf(cm)->frame_context;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005234 }
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005235 if (!cm->fc->initialized)
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05305236 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005237 "Uninitialized entropy context.");
5238
Urvang Joshibac1dea2020-04-20 11:37:15 -07005239 pbi->dcb.corrupted = 0;
Wan-Teh Changbb6192a2018-07-10 11:01:04 -07005240 return uncomp_hdr_size;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005241}
5242
David Barker01563082017-10-09 13:59:16 +01005243// Once-per-frame initialization
Wan-Teh Chang94534432024-08-08 20:29:57 -07005244static inline void setup_frame_info(AV1Decoder *pbi) {
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005245 AV1_COMMON *const cm = &pbi->common;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005246
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005247 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5248 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5249 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
venkat sanampudi4a469992023-04-10 10:10:58 +05305250 av1_alloc_restoration_buffers(cm, /*is_sgr_enabled =*/true);
Rachel Barkerc65670f62023-10-25 00:35:23 +00005251 for (int p = 0; p < av1_num_planes(cm); p++) {
5252 av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0);
5253 }
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005254 }
Jerome Jiangf883c112022-04-15 17:52:28 -04005255
Tarundeep Singh4243e622021-04-20 16:10:22 +05305256 const int use_highbd = cm->seq_params->use_highbitdepth;
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05305257 const int buf_size = MC_TEMP_BUF_PELS << use_highbd;
5258 if (pbi->td.mc_buf_size != buf_size) {
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -07005259 av1_free_mc_tmp_buf(&pbi->td);
Ravi Chaudhary4e7b6f02018-06-08 19:36:56 +05305260 allocate_mc_tmp_buf(cm, &pbi->td, buf_size, use_highbd);
5261 }
David Barker01563082017-10-09 13:59:16 +01005262}
5263
5264void av1_decode_tg_tiles_and_wrapup(AV1Decoder *pbi, const uint8_t *data,
5265 const uint8_t *data_end,
Cheng Chena98e55e2018-06-12 23:14:19 -07005266 const uint8_t **p_data_end, int start_tile,
5267 int end_tile, int initialize_flag) {
David Barker01563082017-10-09 13:59:16 +01005268 AV1_COMMON *const cm = &pbi->common;
Urvang Joshi54ffae72020-03-23 13:37:10 -07005269 CommonTileParams *const tiles = &cm->tiles;
Urvang Joshibac1dea2020-04-20 11:37:15 -07005270 MACROBLOCKD *const xd = &pbi->dcb.xd;
Cheng Chena98e55e2018-06-12 23:14:19 -07005271 const int tile_count_tg = end_tile - start_tile + 1;
David Barker01563082017-10-09 13:59:16 +01005272
Mudassir Galaganath24d4b292023-09-16 15:51:13 +05305273 xd->error_info = cm->error;
David Barker01563082017-10-09 13:59:16 +01005274 if (initialize_flag) setup_frame_info(pbi);
Cheng Chenc75fb082018-07-06 19:01:04 -07005275 const int num_planes = av1_num_planes(cm);
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005276
Urvang Joshi54ffae72020-03-23 13:37:10 -07005277 if (pbi->max_threads > 1 && !(tiles->large_scale && !pbi->ext_tile_debug) &&
Deepa K G966c44e2018-06-22 17:41:12 +05305278 pbi->row_mt)
Deepa K Gec1987a2018-07-03 14:22:27 +05305279 *p_data_end =
5280 decode_tiles_row_mt(pbi, data, data_end, start_tile, end_tile);
Deepa K G966c44e2018-06-22 17:41:12 +05305281 else if (pbi->max_threads > 1 && tile_count_tg > 1 &&
Urvang Joshi54ffae72020-03-23 13:37:10 -07005282 !(tiles->large_scale && !pbi->ext_tile_debug))
Cheng Chena98e55e2018-06-12 23:14:19 -07005283 *p_data_end = decode_tiles_mt(pbi, data, data_end, start_tile, end_tile);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +05305284 else
Cheng Chena98e55e2018-06-12 23:14:19 -07005285 *p_data_end = decode_tiles(pbi, data, data_end, start_tile, end_tile);
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005286
Imdad Sardharwalla317002f2017-12-05 16:24:56 +00005287 // If the bit stream is monochrome, set the U and V buffers to a constant.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005288 if (num_planes < 3) {
Tarundeep Singh4243e622021-04-20 16:10:22 +05305289 set_planes_to_neutral_grey(cm->seq_params, xd->cur_buf, 1);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005290 }
Imdad Sardharwallad8397d62017-11-30 15:28:05 +00005291
Urvang Joshi54ffae72020-03-23 13:37:10 -07005292 if (end_tile != tiles->rows * tiles->cols - 1) {
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005293 return;
5294 }
5295
Vishnu Teja Manyamf1949812021-04-08 19:07:51 +05305296 av1_alloc_cdef_buffers(cm, &pbi->cdef_worker, &pbi->cdef_sync,
Nithya V S709ccb32021-08-18 11:05:46 +05305297 pbi->num_workers, 1);
Vishnu Teja Manyamf1949812021-04-08 19:07:51 +05305298 av1_alloc_cdef_sync(cm, &pbi->cdef_sync, pbi->num_workers);
5299
Urvang Joshi54ffae72020-03-23 13:37:10 -07005300 if (!cm->features.allow_intrabc && !tiles->single_tile_decoding) {
Yaowu Xu73a12152018-04-18 12:00:51 -07005301 if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
Yannis Guyon27bdea52021-07-21 10:41:02 +02005302 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, &pbi->dcb.xd, 0,
5303 num_planes, 0, pbi->tile_workers,
5304 pbi->num_workers, &pbi->lf_row_sync, 0);
Yaowu Xu73a12152018-04-18 12:00:51 -07005305 }
5306
Yunqing Wange8025032018-04-10 18:50:29 -07005307 const int do_cdef =
Urvang Joshib6409e92020-03-23 11:23:27 -07005308 !pbi->skip_loop_filter && !cm->features.coded_lossless &&
David Turnerebf96f42018-11-14 16:57:57 +00005309 (cm->cdef_info.cdef_bits || cm->cdef_info.cdef_strengths[0] ||
5310 cm->cdef_info.cdef_uv_strengths[0]);
Cheng Chen09c83a52018-06-05 12:27:36 -07005311 const int do_superres = av1_superres_scaled(cm);
Yunqing Wange8025032018-04-10 18:50:29 -07005312 const int optimized_loop_restoration = !do_cdef && !do_superres;
Jerome Jiang87c414e2020-10-30 19:03:47 -07005313 const int do_loop_restoration =
5314 cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
5315 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
5316 cm->rst_info[2].frame_restoration_type != RESTORE_NONE;
Deepa K Gaeee77c2022-05-28 14:00:56 +05305317 // Frame border extension is not required in the decoder
5318 // as it happens in extend_mc_border().
5319 int do_extend_border_mt = 0;
Yunqing Wang2ff71af2018-04-24 15:10:10 -07005320 if (!optimized_loop_restoration) {
Yunqing Wang883f6442018-04-09 18:11:20 -07005321 if (do_loop_restoration)
Jack Haughtonddb80602018-11-21 16:41:49 +00005322 av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
5323 cm, 0);
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005324
Urvang Joshibac1dea2020-04-20 11:37:15 -07005325 if (do_cdef) {
Vishnu Teja Manyamf1949812021-04-08 19:07:51 +05305326 if (pbi->num_workers > 1) {
5327 av1_cdef_frame_mt(cm, &pbi->dcb.xd, pbi->cdef_worker,
5328 pbi->tile_workers, &pbi->cdef_sync,
Deepa K Gaeee77c2022-05-28 14:00:56 +05305329 pbi->num_workers, av1_cdef_init_fb_row_mt,
5330 do_extend_border_mt);
Vishnu Teja Manyamf1949812021-04-08 19:07:51 +05305331 } else {
5332 av1_cdef_frame(&pbi->common.cur_frame->buf, cm, &pbi->dcb.xd,
5333 av1_cdef_init_fb_row);
5334 }
Urvang Joshibac1dea2020-04-20 11:37:15 -07005335 }
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005336
Yunqing Wang883f6442018-04-09 18:11:20 -07005337 superres_post_decode(pbi);
5338
5339 if (do_loop_restoration) {
Jack Haughtonddb80602018-11-21 16:41:49 +00005340 av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
5341 cm, 1);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05305342 if (pbi->num_workers > 1) {
5343 av1_loop_restoration_filter_frame_mt(
5344 (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
5345 pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
Deepa K Gaeee77c2022-05-28 14:00:56 +05305346 &pbi->lr_ctxt, do_extend_border_mt);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05305347 } else {
5348 av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
5349 cm, optimized_loop_restoration,
5350 &pbi->lr_ctxt);
5351 }
Yunqing Wang883f6442018-04-09 18:11:20 -07005352 }
5353 } else {
Yunqing Wange8025032018-04-10 18:50:29 -07005354 // In no cdef and no superres case. Provide an optimized version of
5355 // loop_restoration_filter.
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05305356 if (do_loop_restoration) {
5357 if (pbi->num_workers > 1) {
5358 av1_loop_restoration_filter_frame_mt(
5359 (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
5360 pbi->tile_workers, pbi->num_workers, &pbi->lr_row_sync,
Deepa K Gaeee77c2022-05-28 14:00:56 +05305361 &pbi->lr_ctxt, do_extend_border_mt);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05305362 } else {
5363 av1_loop_restoration_filter_frame((YV12_BUFFER_CONFIG *)xd->cur_buf,
5364 cm, optimized_loop_restoration,
5365 &pbi->lr_ctxt);
5366 }
5367 }
David Barker218556e2018-02-14 14:23:12 +00005368 }
David Barker218556e2018-02-14 14:23:12 +00005369 }
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005370
Urvang Joshibac1dea2020-04-20 11:37:15 -07005371 if (!pbi->dcb.corrupted) {
Urvang Joshi6237b882020-03-26 15:02:26 -07005372 if (cm->features.refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Urvang Joshi1de67aa2020-03-20 11:21:57 -07005373 assert(pbi->context_update_tile_id < pbi->allocated_tiles);
5374 *cm->fc = pbi->tile_data[pbi->context_update_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07005375 av1_reset_cdf_symbol_counters(cm->fc);
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005376 }
5377 } else {
Tarundeep Singhcfa5fb42021-04-23 18:25:32 +05305378 aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005379 "Decode failed. Frame data is corrupted.");
5380 }
5381
5382#if CONFIG_INSPECTION
5383 if (pbi->inspect_cb != NULL) {
5384 (*pbi->inspect_cb)(pbi, pbi->inspect_ctx);
5385 }
5386#endif
5387
Yaowu Xudd6ef6c2018-03-02 16:43:52 -08005388 // Non frame parallel update frame context here.
Urvang Joshi54ffae72020-03-23 13:37:10 -07005389 if (!tiles->large_scale) {
David Turner1bcefb32018-11-19 17:54:00 +00005390 cm->cur_frame->frame_context = *cm->fc;
David Barker01563082017-10-09 13:59:16 +01005391 }
Yunqing Wangb397f7a2022-03-18 15:10:31 -07005392
5393 if (cm->show_frame && !cm->seq_params->order_hint_info.enable_order_hint) {
5394 ++cm->current_frame.frame_number;
5395 }
Soo-Chul Han65c00ae12017-09-07 13:12:35 -04005396}