blob: e7798b25de3d21d7d1342eccb886bb79fba74983 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Krishna Rapaka7319db52021-09-28 20:35:29 -07002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Vibhoothi41c6dd72021-10-12 18:48:26 +00004 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * aomedia.org/license/patent-license/.
Yaowu Xuc27fc142016-08-22 16:08:15 -070011 */
12
13#include <assert.h>
14#include <limits.h>
15#include <stdio.h>
16
Tom Finegan44702c82018-05-22 13:00:39 -070017#include "config/av1_rtcd.h"
18#include "config/aom_dsp_rtcd.h"
19#include "config/aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020
Wan-Teh Change9bfe122018-10-02 11:50:07 -070021#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070022#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070023#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070024#include "aom_ports/aom_once.h"
25#include "aom_ports/aom_timer.h"
26#include "aom_scale/aom_scale.h"
27#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070028
29#include "av1/common/alloccommon.h"
Wan-Teh Changf2d15ee2020-03-10 09:24:43 -070030#include "av1/common/av1_common_int.h"
Tom Finegan17ce8b12017-02-08 12:46:31 -080031#include "av1/common/av1_loopfilter.h"
Lester Lu54673072022-03-18 19:17:51 +000032#include "av1/common/pred_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033#include "av1/common/quant_common.h"
34#include "av1/common/reconinter.h"
35#include "av1/common/reconintra.h"
36
37#include "av1/decoder/decodeframe.h"
38#include "av1/decoder/decoder.h"
39#include "av1/decoder/detokenize.h"
Sebastien Alaiwane9644be2017-12-19 18:20:12 +010040#include "av1/decoder/obu.h"
Sebastien Alaiwane9644be2017-12-19 18:20:12 +010041
Yaowu Xuc27fc142016-08-22 16:08:15 -070042static void initialize_dec(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -070043 av1_rtcd();
44 aom_dsp_rtcd();
45 aom_scale_rtcd();
46 av1_init_intra_predictors();
47 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -070048}
49
Vishnu Teja Manyamc1686892020-12-01 20:00:52 +053050static void update_subgop_stats(const AV1_COMMON *const cm,
51 SubGOPStatsDec *const subgop_stats,
52 unsigned int display_order_hint,
53 unsigned int enable_subgop_stats) {
54 if (!enable_subgop_stats) return;
55 // Update subgop related frame data.
56 subgop_stats->disp_frame_idx[subgop_stats->stat_count] = display_order_hint;
57 subgop_stats->show_existing_frame[subgop_stats->stat_count] =
58 cm->show_existing_frame;
59 subgop_stats->show_frame[subgop_stats->stat_count] = cm->show_frame;
Vishnu Teja Manyam4d1db462020-12-02 09:07:12 +053060 subgop_stats->qindex[subgop_stats->stat_count] = cm->quant_params.base_qindex;
Vishnu Teja Manyamd42d5622020-12-02 10:17:29 +053061 subgop_stats->refresh_frame_flags[subgop_stats->stat_count] =
62 cm->current_frame.refresh_frame_flags;
63
64 for (MV_REFERENCE_FRAME ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame)
65 subgop_stats->ref_frame_map[subgop_stats->stat_count][ref_frame] =
66 cm->ref_frame_map[ref_frame]->order_hint;
67
Vishnu Teja Manyamc1686892020-12-01 20:00:52 +053068 assert(subgop_stats->stat_count < MAX_SUBGOP_STATS_SIZE);
69 subgop_stats->stat_count++;
70}
71
Urvang Joshi9dc909d2020-03-23 16:07:02 -070072static void dec_set_mb_mi(CommonModeInfoParams *mi_params, int width,
73 int height) {
chiyotsaia7091f12019-08-09 16:48:27 -070074 // Ensure that the decoded width and height are both multiples of
75 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
76 // subsampling is used).
77 // This simplifies the implementation of various experiments,
78 // eg. cdef, which operates on units of 8x8 luma pixels.
79 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
80 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
81
Urvang Joshi9dc909d2020-03-23 16:07:02 -070082 mi_params->mi_cols = aligned_width >> MI_SIZE_LOG2;
83 mi_params->mi_rows = aligned_height >> MI_SIZE_LOG2;
84 mi_params->mi_stride = calc_mi_size(mi_params->mi_cols);
chiyotsaia7091f12019-08-09 16:48:27 -070085
Urvang Joshi9dc909d2020-03-23 16:07:02 -070086 mi_params->mb_cols = (mi_params->mi_cols + 2) >> 2;
87 mi_params->mb_rows = (mi_params->mi_rows + 2) >> 2;
88 mi_params->MBs = mi_params->mb_rows * mi_params->mb_cols;
chiyotsaia7091f12019-08-09 16:48:27 -070089
Urvang Joshi9dc909d2020-03-23 16:07:02 -070090 mi_params->mi_alloc_bsize = BLOCK_4X4;
Urvang Joshi9dc909d2020-03-23 16:07:02 -070091 mi_params->mi_alloc_stride = mi_params->mi_stride;
chiyotsaia7091f12019-08-09 16:48:27 -070092
Urvang Joshi9dc909d2020-03-23 16:07:02 -070093 assert(mi_size_wide[mi_params->mi_alloc_bsize] ==
94 mi_size_high[mi_params->mi_alloc_bsize]);
chiyotsaia7091f12019-08-09 16:48:27 -070095
96#if CONFIG_LPF_MASK
Urvang Joshi9dc909d2020-03-23 16:07:02 -070097 av1_alloc_loop_filter_mask(mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -070098#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070099}
100
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700101static void dec_setup_mi(CommonModeInfoParams *mi_params) {
102 const int mi_grid_size =
103 mi_params->mi_stride * calc_mi_size(mi_params->mi_rows);
104 memset(mi_params->mi_grid_base, 0,
105 mi_grid_size * sizeof(*mi_params->mi_grid_base));
chiyotsaia7091f12019-08-09 16:48:27 -0700106}
107
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700108static void dec_free_mi(CommonModeInfoParams *mi_params) {
Urvang Joshid2998cd2020-03-26 02:27:48 -0700109 aom_free(mi_params->mi_alloc);
110 mi_params->mi_alloc = NULL;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700111 aom_free(mi_params->mi_grid_base);
112 mi_params->mi_grid_base = NULL;
113 mi_params->mi_alloc_size = 0;
114 aom_free(mi_params->tx_type_map);
115 mi_params->tx_type_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700116}
117
Yeqing Wuecfcdea2022-04-13 05:57:39 +0000118#if CONFIG_TIP
119static INLINE void dec_init_tip_ref_frame(AV1_COMMON *const cm) {
120 TIP *tip_ref = &cm->tip_ref;
121 tip_ref->tip_frame = aom_calloc(1, sizeof(*tip_ref->tip_frame));
122}
123
124static INLINE void dec_free_tip_ref_frame(AV1_COMMON *const cm) {
125 aom_free(cm->tip_ref.available_flag);
126 cm->tip_ref.available_flag = NULL;
127 aom_free(cm->tip_ref.mf_need_clamp);
128 cm->tip_ref.mf_need_clamp = NULL;
129
130 aom_free_frame_buffer(&cm->tip_ref.tip_frame->buf);
131 aom_free(cm->tip_ref.tip_frame);
132 cm->tip_ref.tip_frame = NULL;
133}
134#endif // CONFIG_TIP
135
Yaowu Xuf883b422016-08-30 14:01:10 -0700136AV1Decoder *av1_decoder_create(BufferPool *const pool) {
137 AV1Decoder *volatile const pbi = aom_memalign(32, sizeof(*pbi));
Wan-Teh Changabff6712018-09-26 12:10:38 -0700138 if (!pbi) return NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700139 av1_zero(*pbi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140
Wan-Teh Changabff6712018-09-26 12:10:38 -0700141 AV1_COMMON *volatile const cm = &pbi->common;
142
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -0700143 // The jmp_buf is valid only for the duration of the function that calls
144 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
145 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700146 if (setjmp(cm->error.jmp)) {
147 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700148 av1_decoder_remove(pbi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149 return NULL;
150 }
151
152 cm->error.setjmp = 1;
153
Angie Chianga5d96c42016-10-21 16:16:56 -0700154 CHECK_MEM_ERROR(cm, cm->fc,
155 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +0000156 CHECK_MEM_ERROR(
157 cm, cm->default_frame_context,
158 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -0700159 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +0000160 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161
162 pbi->need_resync = 1;
Wan-Teh Chang5396c552018-06-29 10:33:50 -0700163 aom_once(initialize_dec);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164
165 // Initialize the references to not point to any frame buffers.
David Turnere7ebf902018-12-04 14:04:55 +0000166 for (int i = 0; i < REF_FRAMES; i++) {
167 cm->ref_frame_map[i] = NULL;
David Turnere7ebf902018-12-04 14:04:55 +0000168 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169
David Turnerd2a592e2018-11-16 14:59:31 +0000170 cm->current_frame.frame_number = 0;
David Barkerf6f8fa12018-06-20 14:46:05 +0100171 pbi->decoding_first_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172 pbi->common.buffer_pool = pool;
173
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700174 cm->seq_params.bit_depth = AOM_BITS_8;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700176 cm->mi_params.free_mi = dec_free_mi;
177 cm->mi_params.setup_mi = dec_setup_mi;
178 cm->mi_params.set_mb_mi = dec_set_mb_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179
Yaowu Xuf883b422016-08-30 14:01:10 -0700180 av1_loop_filter_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181
Urvang Joshi17814622020-03-27 17:26:17 -0700182 av1_qm_init(&cm->quant_params, av1_num_planes(cm));
Yaowu Xuf883b422016-08-30 14:01:10 -0700183 av1_loop_restoration_precal();
Michael Bebenita6048d052016-08-25 14:40:54 -0700184#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -0400185 pbi->acct_enabled = 1;
Michael Bebenita6048d052016-08-25 14:40:54 -0700186 aom_accounting_init(&pbi->accounting);
187#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188
Yeqing Wuecfcdea2022-04-13 05:57:39 +0000189#if CONFIG_TIP
190 dec_init_tip_ref_frame(cm);
191#endif // CONFIG_TIP
192
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 cm->error.setjmp = 0;
194
Yaowu Xuf883b422016-08-30 14:01:10 -0700195 aom_get_worker_interface()->init(&pbi->lf_worker);
Wan-Teh Chang4d29ee82018-09-20 10:07:52 -0700196 pbi->lf_worker.thread_name = "aom lf worker";
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197
Ryan Leiccc6ea72021-01-06 11:43:56 -0800198#if DEBUG_EXTQUANT
199 cm->fDecCoeffLog = fopen("DecCoeffLog.txt", "wt");
200#endif
201
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202 return pbi;
203}
204
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +0530205void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info) {
206 if (tile_mt_info != NULL) {
207#if CONFIG_MULTITHREAD
208 if (tile_mt_info->job_mutex != NULL) {
209 pthread_mutex_destroy(tile_mt_info->job_mutex);
210 aom_free(tile_mt_info->job_mutex);
211 }
212#endif
213 aom_free(tile_mt_info->job_queue);
214 // clear the structure as the source of this call may be a resize in which
215 // case this call will be followed by an _alloc() which may fail.
216 av1_zero(*tile_mt_info);
217 }
218}
219
Deepa K Gec1987a2018-07-03 14:22:27 +0530220void av1_dec_free_cb_buf(AV1Decoder *pbi) {
221 aom_free(pbi->cb_buffer_base);
222 pbi->cb_buffer_base = NULL;
223 pbi->cb_buffer_alloc_size = 0;
224}
225
Yaowu Xuf883b422016-08-30 14:01:10 -0700226void av1_decoder_remove(AV1Decoder *pbi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227 int i;
228
229 if (!pbi) return;
230
Yunqing Wanga2f17282018-06-18 22:43:08 -0700231 // Free the tile list output buffer.
Yunqing Wang6ff48092018-11-13 14:10:48 -0800232 aom_free_frame_buffer(&pbi->tile_list_outbuf);
Yunqing Wanga2f17282018-06-18 22:43:08 -0700233
Yaowu Xuf883b422016-08-30 14:01:10 -0700234 aom_get_worker_interface()->end(&pbi->lf_worker);
235 aom_free(pbi->lf_worker.data1);
Cherma Rajan Ae4121f62018-04-18 17:30:55 +0530236
Cherma Rajan Aa91ed822018-04-23 14:45:49 +0530237 if (pbi->thread_data) {
238 for (int worker_idx = 0; worker_idx < pbi->max_threads - 1; worker_idx++) {
239 DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -0700240 av1_free_mc_tmp_buf(thread_data->td);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +0530241 aom_free(thread_data->td);
242 }
243 aom_free(pbi->thread_data);
244 }
245
246 for (i = 0; i < pbi->num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700247 AVxWorker *const worker = &pbi->tile_workers[i];
248 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249 }
Deepa K G36ff5b12018-07-06 18:22:50 +0530250#if CONFIG_MULTITHREAD
251 if (pbi->row_mt_mutex_ != NULL) {
252 pthread_mutex_destroy(pbi->row_mt_mutex_);
253 aom_free(pbi->row_mt_mutex_);
254 }
255 if (pbi->row_mt_cond_ != NULL) {
256 pthread_cond_destroy(pbi->row_mt_cond_);
257 aom_free(pbi->row_mt_cond_);
258 }
259#endif
Deepa K Gcebe0782018-07-05 11:27:40 +0530260 for (i = 0; i < pbi->allocated_tiles; i++) {
261 TileDataDec *const tile_data = pbi->tile_data + i;
262 av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
263 }
Cherma Rajan Ae4121f62018-04-18 17:30:55 +0530264 aom_free(pbi->tile_data);
Yaowu Xuf883b422016-08-30 14:01:10 -0700265 aom_free(pbi->tile_workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700266
Deepa K G964e72e2018-05-16 16:56:01 +0530267 if (pbi->num_workers > 0) {
268 av1_loop_filter_dealloc(&pbi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +0530269 av1_loop_restoration_dealloc(&pbi->lr_row_sync, pbi->num_workers);
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +0530270 av1_dealloc_dec_jobs(&pbi->tile_mt_info);
Deepa K G964e72e2018-05-16 16:56:01 +0530271 }
272
Yeqing Wuecfcdea2022-04-13 05:57:39 +0000273#if CONFIG_TIP
274 dec_free_tip_ref_frame(&pbi->common);
275#endif // CONFIG_TIP
276
Deepa K Gec1987a2018-07-03 14:22:27 +0530277 av1_dec_free_cb_buf(pbi);
Michael Bebenita6048d052016-08-25 14:40:54 -0700278#if CONFIG_ACCOUNTING
279 aom_accounting_clear(&pbi->accounting);
280#endif
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -0700281 av1_free_mc_tmp_buf(&pbi->td);
Daniel Max Valenzuelacc6cf052019-12-17 11:13:15 -0800282 aom_img_metadata_array_free(pbi->metadata);
Ryan Leiccc6ea72021-01-06 11:43:56 -0800283
284#if DEBUG_EXTQUANT
285 if (pbi->common.fDecCoeffLog != NULL) {
286 fclose(pbi->common.fDecCoeffLog);
287 }
288#endif
289
Yaowu Xuf883b422016-08-30 14:01:10 -0700290 aom_free(pbi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291}
292
Hui Su95e9c0c2019-12-09 15:21:35 -0800293void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Hui Su474e1e12020-02-27 15:46:36 -0800294 aom_reader *r, palette_visitor_fn_t visit) {
leolzhao3ab59842021-05-11 10:07:48 -0700295 if (!is_inter_block(xd->mi[0], xd->tree_type)) {
Leo (Liang) Zhao03c65e82022-03-09 22:23:31 +0000296 const int plane_start = get_partition_plane_start(xd->tree_type);
297 const int plane_end = get_partition_plane_end(
298 xd->tree_type, AOMMIN(2, av1_num_planes(&pbi->common)));
leolzhao3db7cca2021-01-26 16:53:07 -0800299 for (int plane = plane_start; plane < plane_end; ++plane) {
Hui Su474e1e12020-02-27 15:46:36 -0800300 if (plane == 0 || xd->is_chroma_ref) {
Deepa K G99f7ddb2018-06-20 11:21:21 +0530301 if (xd->mi[0]->palette_mode_info.palette_size[plane])
302 visit(xd, plane, r);
303 } else {
304 assert(xd->mi[0]->palette_mode_info.palette_size[plane] == 0);
305 }
306 }
307 }
308}
309
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310static int equal_dimensions(const YV12_BUFFER_CONFIG *a,
311 const YV12_BUFFER_CONFIG *b) {
312 return a->y_height == b->y_height && a->y_width == b->y_width &&
313 a->uv_height == b->uv_height && a->uv_width == b->uv_width;
314}
315
Thomas Daede497d1952017-08-08 17:33:06 -0700316aom_codec_err_t av1_copy_reference_dec(AV1Decoder *pbi, int idx,
Yaowu Xuf883b422016-08-30 14:01:10 -0700317 YV12_BUFFER_CONFIG *sd) {
318 AV1_COMMON *cm = &pbi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000319 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320
Thomas Daede497d1952017-08-08 17:33:06 -0700321 const YV12_BUFFER_CONFIG *const cfg = get_ref_frame(cm, idx);
322 if (cfg == NULL) {
323 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "No reference frame");
324 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700325 }
Thomas Daede497d1952017-08-08 17:33:06 -0700326 if (!equal_dimensions(cfg, sd))
327 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
328 "Incorrect buffer dimensions");
329 else
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000330 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700331
332 return cm->error.error_code;
333}
334
Yunqing Wang4450c762018-04-24 13:49:25 -0700335static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
336 const YV12_BUFFER_CONFIG *b) {
337 return a->y_height == b->y_height && a->y_width == b->y_width &&
338 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
339 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
340 a->border == b->border &&
341 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
342 (b->flags & YV12_FLAG_HIGHBITDEPTH);
343}
344
Thomas Daede497d1952017-08-08 17:33:06 -0700345aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
Yunqing Wang4450c762018-04-24 13:49:25 -0700346 int use_external_ref,
Yaowu Xuf883b422016-08-30 14:01:10 -0700347 YV12_BUFFER_CONFIG *sd) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000348 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349 YV12_BUFFER_CONFIG *ref_buf = NULL;
350
Yaowu Xuc27fc142016-08-22 16:08:15 -0700351 // Get the destination reference buffer.
Thomas Daede497d1952017-08-08 17:33:06 -0700352 ref_buf = get_ref_frame(cm, idx);
353
354 if (ref_buf == NULL) {
355 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "No reference frame");
356 return AOM_CODEC_ERROR;
357 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700358
Yunqing Wang4450c762018-04-24 13:49:25 -0700359 if (!use_external_ref) {
360 if (!equal_dimensions(ref_buf, sd)) {
361 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
362 "Incorrect buffer dimensions");
363 } else {
364 // Overwrite the reference frame buffer.
365 aom_yv12_copy_frame(sd, ref_buf, num_planes);
366 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700367 } else {
Yunqing Wang4450c762018-04-24 13:49:25 -0700368 if (!equal_dimensions_and_border(ref_buf, sd)) {
369 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
370 "Incorrect buffer dimensions");
371 } else {
372 // Overwrite the reference frame buffer pointers.
373 // Once we no longer need the external reference buffer, these pointers
374 // are restored.
375 ref_buf->store_buf_adr[0] = ref_buf->y_buffer;
376 ref_buf->store_buf_adr[1] = ref_buf->u_buffer;
377 ref_buf->store_buf_adr[2] = ref_buf->v_buffer;
378 ref_buf->y_buffer = sd->y_buffer;
379 ref_buf->u_buffer = sd->u_buffer;
380 ref_buf->v_buffer = sd->v_buffer;
Frederic Barbier870462d2018-06-27 16:53:40 +0200381 ref_buf->use_external_reference_buffers = 1;
Yunqing Wang4450c762018-04-24 13:49:25 -0700382 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383 }
384
385 return cm->error.error_code;
386}
387
Yunqing Wang20a336d2018-04-25 10:19:53 -0700388aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
389 YV12_BUFFER_CONFIG *new_frame,
390 YV12_BUFFER_CONFIG *sd) {
391 const int num_planes = av1_num_planes(cm);
392
393 if (!equal_dimensions_and_border(new_frame, sd))
394 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
395 "Incorrect buffer dimensions");
396 else
397 aom_yv12_copy_frame(new_frame, sd, num_planes);
398
399 return cm->error.error_code;
400}
401
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700402static void release_current_frame(AV1Decoder *pbi) {
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700403 AV1_COMMON *const cm = &pbi->common;
404 BufferPool *const pool = cm->buffer_pool;
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700405
Wan-Teh Changc4824892018-11-20 17:22:41 -0800406 cm->cur_frame->buf.corrupted = 1;
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700407 lock_buffer_pool(pool);
David Turnere7ebf902018-12-04 14:04:55 +0000408 decrease_ref_count(cm->cur_frame, pool);
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700409 unlock_buffer_pool(pool);
Wan-Teh Changc4824892018-11-20 17:22:41 -0800410 cm->cur_frame = NULL;
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700411}
412
Wan-Teh Change9bfe122018-10-02 11:50:07 -0700413// If any buffer updating is signaled it should be done here.
David Turnere7ebf902018-12-04 14:04:55 +0000414// Consumes a reference to cm->cur_frame.
Wan-Teh Change9bfe122018-10-02 11:50:07 -0700415//
416// This functions returns void. It reports failure by setting
417// cm->error.error_code.
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700418static void update_frame_buffers(AV1Decoder *pbi, int frame_decoded) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700419 int ref_index = 0, mask;
Yaowu Xuf883b422016-08-30 14:01:10 -0700420 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700422
David Barker6cc60e22018-05-29 17:38:47 +0100423 if (frame_decoded) {
Frank Bossend8f457a2018-04-30 21:44:12 -0400424 lock_buffer_pool(pool);
Yunqing Wangd7546d42018-06-06 15:08:05 -0700425
426 // In ext-tile decoding, the camera frame header is only decoded once. So,
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700427 // we don't update the references here.
Yunqing Wangd7546d42018-06-06 15:08:05 -0700428 if (!pbi->camera_frame_header_ready) {
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700429 // The following for loop needs to release the reference stored in
430 // cm->ref_frame_map[ref_index] before storing a reference to
431 // cm->cur_frame in cm->ref_frame_map[ref_index].
David Turner996b2c12018-12-07 15:52:30 +0000432 for (mask = cm->current_frame.refresh_frame_flags; mask; mask >>= 1) {
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700433 if (mask & 1) {
434 decrease_ref_count(cm->ref_frame_map[ref_index], pool);
435 cm->ref_frame_map[ref_index] = cm->cur_frame;
436 ++cm->cur_frame->ref_count;
437 }
Yunqing Wangd7546d42018-06-06 15:08:05 -0700438 ++ref_index;
439 }
Vishnu Teja Manyamc1686892020-12-01 20:00:52 +0530440 update_subgop_stats(cm, &pbi->subgop_stats, cm->cur_frame->order_hint,
441 pbi->enable_subgop_stats);
Frank Bossend8f457a2018-04-30 21:44:12 -0400442 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700443
David Barkere8df7ba2018-06-06 16:22:11 +0100444 if (cm->show_existing_frame || cm->show_frame) {
David Barkerf1c07ce2018-06-06 17:50:18 +0100445 if (pbi->output_all_layers) {
446 // Append this frame to the output queue
447 if (pbi->num_output_frames >= MAX_NUM_SPATIAL_LAYERS) {
448 // We can't store the new frame anywhere, so drop it and return an
449 // error
Wan-Teh Changc4824892018-11-20 17:22:41 -0800450 cm->cur_frame->buf.corrupted = 1;
David Turnere7ebf902018-12-04 14:04:55 +0000451 decrease_ref_count(cm->cur_frame, pool);
David Barkerf1c07ce2018-06-06 17:50:18 +0100452 cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
453 } else {
David Turnere7ebf902018-12-04 14:04:55 +0000454 pbi->output_frames[pbi->num_output_frames] = cm->cur_frame;
David Barkerf1c07ce2018-06-06 17:50:18 +0100455 pbi->num_output_frames++;
456 }
457 } else {
458 // Replace any existing output frame
459 assert(pbi->num_output_frames == 0 || pbi->num_output_frames == 1);
460 if (pbi->num_output_frames > 0) {
David Turnere7ebf902018-12-04 14:04:55 +0000461 decrease_ref_count(pbi->output_frames[0], pool);
David Barkerf1c07ce2018-06-06 17:50:18 +0100462 }
David Turnere7ebf902018-12-04 14:04:55 +0000463 pbi->output_frames[0] = cm->cur_frame;
David Barkerf1c07ce2018-06-06 17:50:18 +0100464 pbi->num_output_frames = 1;
David Barkere8df7ba2018-06-06 16:22:11 +0100465 }
David Barkere8df7ba2018-06-06 16:22:11 +0100466 } else {
David Turnere7ebf902018-12-04 14:04:55 +0000467 decrease_ref_count(cm->cur_frame, pool);
David Barkere8df7ba2018-06-06 16:22:11 +0100468 }
469
Frank Bossend8f457a2018-04-30 21:44:12 -0400470 unlock_buffer_pool(pool);
David Barkere8df7ba2018-06-06 16:22:11 +0100471 } else {
472 // Nothing was decoded, so just drop this frame buffer
473 lock_buffer_pool(pool);
David Turnere7ebf902018-12-04 14:04:55 +0000474 decrease_ref_count(cm->cur_frame, pool);
David Barkere8df7ba2018-06-06 16:22:11 +0100475 unlock_buffer_pool(pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700476 }
Wan-Teh Changc4824892018-11-20 17:22:41 -0800477 cm->cur_frame = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700478
Yunqing Wangd7546d42018-06-06 15:08:05 -0700479 if (!pbi->camera_frame_header_ready) {
Yunqing Wangd7546d42018-06-06 15:08:05 -0700480 // Invalidate these references until the next frame starts.
481 for (ref_index = 0; ref_index < INTER_REFS_PER_FRAME; ref_index++) {
David Turnera21966b2018-12-05 14:48:49 +0000482 cm->remapped_ref_idx[ref_index] = INVALID_IDX;
Yunqing Wangd7546d42018-06-06 15:08:05 -0700483 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484 }
485}
486
Yaowu Xuf883b422016-08-30 14:01:10 -0700487int av1_receive_compressed_data(AV1Decoder *pbi, size_t size,
488 const uint8_t **psource) {
489 AV1_COMMON *volatile const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490 const uint8_t *source = *psource;
Yaowu Xuf883b422016-08-30 14:01:10 -0700491 cm->error.error_code = AOM_CODEC_OK;
Wan-Teh Chang97129d12018-10-01 15:56:38 -0700492 cm->error.has_detail = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700493
494 if (size == 0) {
495 // This is used to signal that we are missing frames.
496 // We do not know if the missing frame(s) was supposed to update
497 // any of the reference buffers, but we act conservative and
498 // mark only the last buffer as corrupted.
499 //
500 // TODO(jkoleszar): Error concealment is undefined and non-normative
501 // at this point, but if it becomes so, [0] may not always be the correct
502 // thing to do here.
Lester Lu54673072022-03-18 19:17:51 +0000503#if CONFIG_NEW_REF_SIGNALING
504 const int last_frame = get_closest_pastcur_ref_index(cm);
505 RefCntBuffer *ref_buf = get_ref_frame_buf(cm, last_frame);
506#else
David Turnera21966b2018-12-05 14:48:49 +0000507 RefCntBuffer *ref_buf = get_ref_frame_buf(cm, LAST_FRAME);
Lester Lu54673072022-03-18 19:17:51 +0000508#endif // CONFIG_NEW_REF_SIGNALING
David Turnera21966b2018-12-05 14:48:49 +0000509 if (ref_buf != NULL) ref_buf->buf.corrupted = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510 }
511
David Turner0308a5a2019-01-07 10:36:16 +0000512 if (assign_cur_frame_new_fb(cm) == NULL) {
Wan-Teh Chang332b2ae2018-07-09 14:30:20 -0700513 cm->error.error_code = AOM_CODEC_MEM_ERROR;
514 return 1;
515 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -0700517 // The jmp_buf is valid only for the duration of the function that calls
518 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
519 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520 if (setjmp(cm->error.jmp)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700521 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522 int i;
523
524 cm->error.setjmp = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700525
526 // Synchronize all threads immediately as a subsequent decode call may
527 // cause a resize invalidating some allocations.
528 winterface->sync(&pbi->lf_worker);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +0530529 for (i = 0; i < pbi->num_workers; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700530 winterface->sync(&pbi->tile_workers[i]);
531 }
532
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700533 release_current_frame(pbi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700534 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535 return -1;
536 }
537
538 cm->error.setjmp = 1;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -0400539
David Barker6cc60e22018-05-29 17:38:47 +0100540 int frame_decoded =
541 aom_decode_frame_from_obus(pbi, source, source + size, psource);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542
Wan-Teh Changf0f76ca2018-09-26 11:36:52 -0700543 if (frame_decoded < 0) {
544 assert(cm->error.error_code != AOM_CODEC_OK);
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700545 release_current_frame(pbi);
Wan-Teh Chang88e4b0a2018-07-03 14:39:50 -0700546 cm->error.setjmp = 0;
Hui Sub390df92018-07-03 10:59:07 -0700547 return 1;
548 }
Hui Sub4d6b1c2018-01-08 13:43:12 -0800549
Jingning Han53c08962017-11-16 14:03:41 -0800550#if TXCOEFF_TIMER
551 cm->cum_txcoeff_timer += cm->txcoeff_timer;
552 fprintf(stderr,
553 "txb coeff block number: %d, frame time: %ld, cum time %ld in us\n",
554 cm->txb_count, cm->txcoeff_timer, cm->cum_txcoeff_timer);
555 cm->txcoeff_timer = 0;
556 cm->txb_count = 0;
557#endif
558
David Turnere7ebf902018-12-04 14:04:55 +0000559 // Note: At this point, this function holds a reference to cm->cur_frame
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700560 // in the buffer pool. This reference is consumed by update_frame_buffers().
561 update_frame_buffers(pbi, frame_decoded);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562
David Barkerf6f8fa12018-06-20 14:46:05 +0100563 if (frame_decoded) {
564 pbi->decoding_first_frame = 0;
565 }
566
Wan-Teh Chang88e4b0a2018-07-03 14:39:50 -0700567 if (cm->error.error_code != AOM_CODEC_OK) {
568 cm->error.setjmp = 0;
569 return 1;
570 }
David Barkerf1c07ce2018-06-06 17:50:18 +0100571
Yaowu Xuf883b422016-08-30 14:01:10 -0700572 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700573
574 if (!cm->show_existing_frame) {
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800575 if (cm->seg.enabled) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700576 if (cm->prev_frame &&
577 (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) &&
578 (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) {
Jonathan Matthews74e8a992018-02-01 11:31:08 +0000579 cm->last_frame_seg_map = cm->prev_frame->seg_map;
580 } else {
581 cm->last_frame_seg_map = NULL;
582 }
Jonathan Matthews74e8a992018-02-01 11:31:08 +0000583 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584 }
585
586 // Update progress in frame parallel decode.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700587 cm->error.setjmp = 0;
Frank Bossend8f457a2018-04-30 21:44:12 -0400588
Sebastien Alaiwan56fcf7c2018-01-02 11:21:54 +0100589 return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590}
591
David Barkerf1c07ce2018-06-06 17:50:18 +0100592// Get the frame at a particular index in the output queue
593int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
594 aom_film_grain_t **grain_params) {
David Barkerf1c07ce2018-06-06 17:50:18 +0100595 if (index >= pbi->num_output_frames) return -1;
David Turnere7ebf902018-12-04 14:04:55 +0000596 *sd = &pbi->output_frames[index]->buf;
597 *grain_params = &pbi->output_frames[index]->film_grain_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700598 aom_clear_system_state();
Sebastien Alaiwan56fcf7c2018-01-02 11:21:54 +0100599 return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700600}
601
David Barkerf1c07ce2018-06-06 17:50:18 +0100602// Get the highest-spatial-layer output
Rachel Barker8bde1452021-12-16 18:13:47 +0000603// TODO(rachelbarker): What should this do?
Yaowu Xuf883b422016-08-30 14:01:10 -0700604int av1_get_frame_to_show(AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame) {
David Barkerf1c07ce2018-06-06 17:50:18 +0100605 if (pbi->num_output_frames == 0) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606
David Turnere7ebf902018-12-04 14:04:55 +0000607 *frame = pbi->output_frames[pbi->num_output_frames - 1]->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608 return 0;
609}