blob: d180af505d630ea57020550f612ca574c6a60646 [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 *
Krishna Rapaka7319db52021-09-28 20:35:29 -07004 * This source code is subject to the terms of the BSD 3-Clause Clear License and the
5 * Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear License was
6 * not distributed with this source code in the LICENSE file, you can obtain it
7 * at aomedia.org/license/software-license/bsd-3-c-c/. If the Alliance for Open Media Patent
8 * License 1.0 was not distributed with this source code in the PATENTS file, you
9 * can obtain it at aomedia.org/license/patent-license/.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <limits.h>
14#include <stdio.h>
15
Tom Finegan44702c82018-05-22 13:00:39 -070016#include "config/av1_rtcd.h"
17#include "config/aom_dsp_rtcd.h"
18#include "config/aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019
Wan-Teh Change9bfe122018-10-02 11:50:07 -070020#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070021#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070023#include "aom_ports/aom_once.h"
24#include "aom_ports/aom_timer.h"
25#include "aom_scale/aom_scale.h"
26#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027
28#include "av1/common/alloccommon.h"
Wan-Teh Changf2d15ee2020-03-10 09:24:43 -070029#include "av1/common/av1_common_int.h"
Tom Finegan17ce8b12017-02-08 12:46:31 -080030#include "av1/common/av1_loopfilter.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031#include "av1/common/quant_common.h"
32#include "av1/common/reconinter.h"
33#include "av1/common/reconintra.h"
34
35#include "av1/decoder/decodeframe.h"
36#include "av1/decoder/decoder.h"
37#include "av1/decoder/detokenize.h"
Sebastien Alaiwane9644be2017-12-19 18:20:12 +010038#include "av1/decoder/obu.h"
Sebastien Alaiwane9644be2017-12-19 18:20:12 +010039
Yaowu Xuc27fc142016-08-22 16:08:15 -070040static void initialize_dec(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -070041 av1_rtcd();
42 aom_dsp_rtcd();
43 aom_scale_rtcd();
44 av1_init_intra_predictors();
45 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -070046}
47
Vishnu Teja Manyamc1686892020-12-01 20:00:52 +053048static void update_subgop_stats(const AV1_COMMON *const cm,
49 SubGOPStatsDec *const subgop_stats,
50 unsigned int display_order_hint,
51 unsigned int enable_subgop_stats) {
52 if (!enable_subgop_stats) return;
53 // Update subgop related frame data.
54 subgop_stats->disp_frame_idx[subgop_stats->stat_count] = display_order_hint;
55 subgop_stats->show_existing_frame[subgop_stats->stat_count] =
56 cm->show_existing_frame;
57 subgop_stats->show_frame[subgop_stats->stat_count] = cm->show_frame;
Vishnu Teja Manyam4d1db462020-12-02 09:07:12 +053058 subgop_stats->qindex[subgop_stats->stat_count] = cm->quant_params.base_qindex;
Vishnu Teja Manyamd42d5622020-12-02 10:17:29 +053059 subgop_stats->refresh_frame_flags[subgop_stats->stat_count] =
60 cm->current_frame.refresh_frame_flags;
61
62 for (MV_REFERENCE_FRAME ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame)
63 subgop_stats->ref_frame_map[subgop_stats->stat_count][ref_frame] =
64 cm->ref_frame_map[ref_frame]->order_hint;
65
Vishnu Teja Manyamc1686892020-12-01 20:00:52 +053066 assert(subgop_stats->stat_count < MAX_SUBGOP_STATS_SIZE);
67 subgop_stats->stat_count++;
68}
69
Urvang Joshi9dc909d2020-03-23 16:07:02 -070070static void dec_set_mb_mi(CommonModeInfoParams *mi_params, int width,
71 int height) {
chiyotsaia7091f12019-08-09 16:48:27 -070072 // Ensure that the decoded width and height are both multiples of
73 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
74 // subsampling is used).
75 // This simplifies the implementation of various experiments,
76 // eg. cdef, which operates on units of 8x8 luma pixels.
77 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
78 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
79
Urvang Joshi9dc909d2020-03-23 16:07:02 -070080 mi_params->mi_cols = aligned_width >> MI_SIZE_LOG2;
81 mi_params->mi_rows = aligned_height >> MI_SIZE_LOG2;
82 mi_params->mi_stride = calc_mi_size(mi_params->mi_cols);
chiyotsaia7091f12019-08-09 16:48:27 -070083
Urvang Joshi9dc909d2020-03-23 16:07:02 -070084 mi_params->mb_cols = (mi_params->mi_cols + 2) >> 2;
85 mi_params->mb_rows = (mi_params->mi_rows + 2) >> 2;
86 mi_params->MBs = mi_params->mb_rows * mi_params->mb_cols;
chiyotsaia7091f12019-08-09 16:48:27 -070087
Urvang Joshi9dc909d2020-03-23 16:07:02 -070088 mi_params->mi_alloc_bsize = BLOCK_4X4;
Urvang Joshi9dc909d2020-03-23 16:07:02 -070089 mi_params->mi_alloc_stride = mi_params->mi_stride;
chiyotsaia7091f12019-08-09 16:48:27 -070090
Urvang Joshi9dc909d2020-03-23 16:07:02 -070091 assert(mi_size_wide[mi_params->mi_alloc_bsize] ==
92 mi_size_high[mi_params->mi_alloc_bsize]);
chiyotsaia7091f12019-08-09 16:48:27 -070093
94#if CONFIG_LPF_MASK
Urvang Joshi9dc909d2020-03-23 16:07:02 -070095 av1_alloc_loop_filter_mask(mi_params);
chiyotsaia7091f12019-08-09 16:48:27 -070096#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070097}
98
Urvang Joshi9dc909d2020-03-23 16:07:02 -070099static void dec_setup_mi(CommonModeInfoParams *mi_params) {
100 const int mi_grid_size =
101 mi_params->mi_stride * calc_mi_size(mi_params->mi_rows);
102 memset(mi_params->mi_grid_base, 0,
103 mi_grid_size * sizeof(*mi_params->mi_grid_base));
chiyotsaia7091f12019-08-09 16:48:27 -0700104}
105
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700106static void dec_free_mi(CommonModeInfoParams *mi_params) {
Urvang Joshid2998cd2020-03-26 02:27:48 -0700107 aom_free(mi_params->mi_alloc);
108 mi_params->mi_alloc = NULL;
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700109 aom_free(mi_params->mi_grid_base);
110 mi_params->mi_grid_base = NULL;
111 mi_params->mi_alloc_size = 0;
112 aom_free(mi_params->tx_type_map);
113 mi_params->tx_type_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114}
115
Yaowu Xuf883b422016-08-30 14:01:10 -0700116AV1Decoder *av1_decoder_create(BufferPool *const pool) {
117 AV1Decoder *volatile const pbi = aom_memalign(32, sizeof(*pbi));
Wan-Teh Changabff6712018-09-26 12:10:38 -0700118 if (!pbi) return NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700119 av1_zero(*pbi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700120
Wan-Teh Changabff6712018-09-26 12:10:38 -0700121 AV1_COMMON *volatile const cm = &pbi->common;
122
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -0700123 // The jmp_buf is valid only for the duration of the function that calls
124 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
125 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126 if (setjmp(cm->error.jmp)) {
127 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700128 av1_decoder_remove(pbi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129 return NULL;
130 }
131
132 cm->error.setjmp = 1;
133
Angie Chianga5d96c42016-10-21 16:16:56 -0700134 CHECK_MEM_ERROR(cm, cm->fc,
135 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +0000136 CHECK_MEM_ERROR(
137 cm, cm->default_frame_context,
138 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -0700139 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +0000140 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700141
142 pbi->need_resync = 1;
Wan-Teh Chang5396c552018-06-29 10:33:50 -0700143 aom_once(initialize_dec);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700144
145 // Initialize the references to not point to any frame buffers.
David Turnere7ebf902018-12-04 14:04:55 +0000146 for (int i = 0; i < REF_FRAMES; i++) {
147 cm->ref_frame_map[i] = NULL;
David Turnere7ebf902018-12-04 14:04:55 +0000148 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149
David Turnerd2a592e2018-11-16 14:59:31 +0000150 cm->current_frame.frame_number = 0;
David Barkerf6f8fa12018-06-20 14:46:05 +0100151 pbi->decoding_first_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152 pbi->common.buffer_pool = pool;
153
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700154 cm->seq_params.bit_depth = AOM_BITS_8;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700156 cm->mi_params.free_mi = dec_free_mi;
157 cm->mi_params.setup_mi = dec_setup_mi;
158 cm->mi_params.set_mb_mi = dec_set_mb_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700159
Yaowu Xuf883b422016-08-30 14:01:10 -0700160 av1_loop_filter_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700161
Urvang Joshi17814622020-03-27 17:26:17 -0700162 av1_qm_init(&cm->quant_params, av1_num_planes(cm));
Yaowu Xuf883b422016-08-30 14:01:10 -0700163 av1_loop_restoration_precal();
Michael Bebenita6048d052016-08-25 14:40:54 -0700164#if CONFIG_ACCOUNTING
Nathan E. Eggeeb64fc22016-10-05 19:33:48 -0400165 pbi->acct_enabled = 1;
Michael Bebenita6048d052016-08-25 14:40:54 -0700166 aom_accounting_init(&pbi->accounting);
167#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700168
169 cm->error.setjmp = 0;
170
Yaowu Xuf883b422016-08-30 14:01:10 -0700171 aom_get_worker_interface()->init(&pbi->lf_worker);
Wan-Teh Chang4d29ee82018-09-20 10:07:52 -0700172 pbi->lf_worker.thread_name = "aom lf worker";
Yaowu Xuc27fc142016-08-22 16:08:15 -0700173
Ryan Leiccc6ea72021-01-06 11:43:56 -0800174#if DEBUG_EXTQUANT
175 cm->fDecCoeffLog = fopen("DecCoeffLog.txt", "wt");
176#endif
177
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178 return pbi;
179}
180
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +0530181void av1_dealloc_dec_jobs(struct AV1DecTileMTData *tile_mt_info) {
182 if (tile_mt_info != NULL) {
183#if CONFIG_MULTITHREAD
184 if (tile_mt_info->job_mutex != NULL) {
185 pthread_mutex_destroy(tile_mt_info->job_mutex);
186 aom_free(tile_mt_info->job_mutex);
187 }
188#endif
189 aom_free(tile_mt_info->job_queue);
190 // clear the structure as the source of this call may be a resize in which
191 // case this call will be followed by an _alloc() which may fail.
192 av1_zero(*tile_mt_info);
193 }
194}
195
Deepa K Gec1987a2018-07-03 14:22:27 +0530196void av1_dec_free_cb_buf(AV1Decoder *pbi) {
197 aom_free(pbi->cb_buffer_base);
198 pbi->cb_buffer_base = NULL;
199 pbi->cb_buffer_alloc_size = 0;
200}
201
Yaowu Xuf883b422016-08-30 14:01:10 -0700202void av1_decoder_remove(AV1Decoder *pbi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 int i;
204
205 if (!pbi) return;
206
Yunqing Wanga2f17282018-06-18 22:43:08 -0700207 // Free the tile list output buffer.
Yunqing Wang6ff48092018-11-13 14:10:48 -0800208 aom_free_frame_buffer(&pbi->tile_list_outbuf);
Yunqing Wanga2f17282018-06-18 22:43:08 -0700209
Yaowu Xuf883b422016-08-30 14:01:10 -0700210 aom_get_worker_interface()->end(&pbi->lf_worker);
211 aom_free(pbi->lf_worker.data1);
Cherma Rajan Ae4121f62018-04-18 17:30:55 +0530212
Cherma Rajan Aa91ed822018-04-23 14:45:49 +0530213 if (pbi->thread_data) {
214 for (int worker_idx = 0; worker_idx < pbi->max_threads - 1; worker_idx++) {
215 DecWorkerData *const thread_data = pbi->thread_data + worker_idx;
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -0700216 av1_free_mc_tmp_buf(thread_data->td);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +0530217 aom_free(thread_data->td);
218 }
219 aom_free(pbi->thread_data);
220 }
221
222 for (i = 0; i < pbi->num_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700223 AVxWorker *const worker = &pbi->tile_workers[i];
224 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225 }
Deepa K G36ff5b12018-07-06 18:22:50 +0530226#if CONFIG_MULTITHREAD
227 if (pbi->row_mt_mutex_ != NULL) {
228 pthread_mutex_destroy(pbi->row_mt_mutex_);
229 aom_free(pbi->row_mt_mutex_);
230 }
231 if (pbi->row_mt_cond_ != NULL) {
232 pthread_cond_destroy(pbi->row_mt_cond_);
233 aom_free(pbi->row_mt_cond_);
234 }
235#endif
Deepa K Gcebe0782018-07-05 11:27:40 +0530236 for (i = 0; i < pbi->allocated_tiles; i++) {
237 TileDataDec *const tile_data = pbi->tile_data + i;
238 av1_dec_row_mt_dealloc(&tile_data->dec_row_mt_sync);
239 }
Cherma Rajan Ae4121f62018-04-18 17:30:55 +0530240 aom_free(pbi->tile_data);
Yaowu Xuf883b422016-08-30 14:01:10 -0700241 aom_free(pbi->tile_workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242
Deepa K G964e72e2018-05-16 16:56:01 +0530243 if (pbi->num_workers > 0) {
244 av1_loop_filter_dealloc(&pbi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +0530245 av1_loop_restoration_dealloc(&pbi->lr_row_sync, pbi->num_workers);
Ravi Chaudhary2ad23c32018-06-13 15:45:48 +0530246 av1_dealloc_dec_jobs(&pbi->tile_mt_info);
Deepa K G964e72e2018-05-16 16:56:01 +0530247 }
248
Deepa K Gec1987a2018-07-03 14:22:27 +0530249 av1_dec_free_cb_buf(pbi);
Michael Bebenita6048d052016-08-25 14:40:54 -0700250#if CONFIG_ACCOUNTING
251 aom_accounting_clear(&pbi->accounting);
252#endif
Wan-Teh Chang8d728cc2018-08-30 15:34:47 -0700253 av1_free_mc_tmp_buf(&pbi->td);
Daniel Max Valenzuelacc6cf052019-12-17 11:13:15 -0800254 aom_img_metadata_array_free(pbi->metadata);
Ryan Leiccc6ea72021-01-06 11:43:56 -0800255
256#if DEBUG_EXTQUANT
257 if (pbi->common.fDecCoeffLog != NULL) {
258 fclose(pbi->common.fDecCoeffLog);
259 }
260#endif
261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262 aom_free(pbi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263}
264
Hui Su95e9c0c2019-12-09 15:21:35 -0800265void av1_visit_palette(AV1Decoder *const pbi, MACROBLOCKD *const xd,
Hui Su474e1e12020-02-27 15:46:36 -0800266 aom_reader *r, palette_visitor_fn_t visit) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800267#if CONFIG_SDP
leolzhao3ab59842021-05-11 10:07:48 -0700268 if (!is_inter_block(xd->mi[0], xd->tree_type)) {
leolzhao3db7cca2021-01-26 16:53:07 -0800269 const int plane_start = (xd->tree_type == CHROMA_PART);
270 const int plane_end =
271 (xd->tree_type == LUMA_PART ? 1
272 : AOMMIN(2, av1_num_planes(&pbi->common)));
273 for (int plane = plane_start; plane < plane_end; ++plane) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800274#else
leolzhao3ab59842021-05-11 10:07:48 -0700275 if (!is_inter_block(xd->mi[0])) {
Deepa K G99f7ddb2018-06-20 11:21:21 +0530276 for (int plane = 0; plane < AOMMIN(2, av1_num_planes(&pbi->common));
277 ++plane) {
liang zhaoc6f775a2020-12-17 11:54:58 -0800278#endif
Hui Su474e1e12020-02-27 15:46:36 -0800279 if (plane == 0 || xd->is_chroma_ref) {
Deepa K G99f7ddb2018-06-20 11:21:21 +0530280 if (xd->mi[0]->palette_mode_info.palette_size[plane])
281 visit(xd, plane, r);
282 } else {
283 assert(xd->mi[0]->palette_mode_info.palette_size[plane] == 0);
284 }
285 }
286 }
287}
288
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289static int equal_dimensions(const YV12_BUFFER_CONFIG *a,
290 const YV12_BUFFER_CONFIG *b) {
291 return a->y_height == b->y_height && a->y_width == b->y_width &&
292 a->uv_height == b->uv_height && a->uv_width == b->uv_width;
293}
294
Thomas Daede497d1952017-08-08 17:33:06 -0700295aom_codec_err_t av1_copy_reference_dec(AV1Decoder *pbi, int idx,
Yaowu Xuf883b422016-08-30 14:01:10 -0700296 YV12_BUFFER_CONFIG *sd) {
297 AV1_COMMON *cm = &pbi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000298 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700299
Thomas Daede497d1952017-08-08 17:33:06 -0700300 const YV12_BUFFER_CONFIG *const cfg = get_ref_frame(cm, idx);
301 if (cfg == NULL) {
302 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "No reference frame");
303 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 }
Thomas Daede497d1952017-08-08 17:33:06 -0700305 if (!equal_dimensions(cfg, sd))
306 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
307 "Incorrect buffer dimensions");
308 else
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000309 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310
311 return cm->error.error_code;
312}
313
Yunqing Wang4450c762018-04-24 13:49:25 -0700314static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
315 const YV12_BUFFER_CONFIG *b) {
316 return a->y_height == b->y_height && a->y_width == b->y_width &&
317 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
318 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
319 a->border == b->border &&
320 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
321 (b->flags & YV12_FLAG_HIGHBITDEPTH);
322}
323
Thomas Daede497d1952017-08-08 17:33:06 -0700324aom_codec_err_t av1_set_reference_dec(AV1_COMMON *cm, int idx,
Yunqing Wang4450c762018-04-24 13:49:25 -0700325 int use_external_ref,
Yaowu Xuf883b422016-08-30 14:01:10 -0700326 YV12_BUFFER_CONFIG *sd) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000327 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328 YV12_BUFFER_CONFIG *ref_buf = NULL;
329
Yaowu Xuc27fc142016-08-22 16:08:15 -0700330 // Get the destination reference buffer.
Thomas Daede497d1952017-08-08 17:33:06 -0700331 ref_buf = get_ref_frame(cm, idx);
332
333 if (ref_buf == NULL) {
334 aom_internal_error(&cm->error, AOM_CODEC_ERROR, "No reference frame");
335 return AOM_CODEC_ERROR;
336 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700337
Yunqing Wang4450c762018-04-24 13:49:25 -0700338 if (!use_external_ref) {
339 if (!equal_dimensions(ref_buf, sd)) {
340 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
341 "Incorrect buffer dimensions");
342 } else {
343 // Overwrite the reference frame buffer.
344 aom_yv12_copy_frame(sd, ref_buf, num_planes);
345 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700346 } else {
Yunqing Wang4450c762018-04-24 13:49:25 -0700347 if (!equal_dimensions_and_border(ref_buf, sd)) {
348 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
349 "Incorrect buffer dimensions");
350 } else {
351 // Overwrite the reference frame buffer pointers.
352 // Once we no longer need the external reference buffer, these pointers
353 // are restored.
354 ref_buf->store_buf_adr[0] = ref_buf->y_buffer;
355 ref_buf->store_buf_adr[1] = ref_buf->u_buffer;
356 ref_buf->store_buf_adr[2] = ref_buf->v_buffer;
357 ref_buf->y_buffer = sd->y_buffer;
358 ref_buf->u_buffer = sd->u_buffer;
359 ref_buf->v_buffer = sd->v_buffer;
Frederic Barbier870462d2018-06-27 16:53:40 +0200360 ref_buf->use_external_reference_buffers = 1;
Yunqing Wang4450c762018-04-24 13:49:25 -0700361 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 }
363
364 return cm->error.error_code;
365}
366
Yunqing Wang20a336d2018-04-25 10:19:53 -0700367aom_codec_err_t av1_copy_new_frame_dec(AV1_COMMON *cm,
368 YV12_BUFFER_CONFIG *new_frame,
369 YV12_BUFFER_CONFIG *sd) {
370 const int num_planes = av1_num_planes(cm);
371
372 if (!equal_dimensions_and_border(new_frame, sd))
373 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
374 "Incorrect buffer dimensions");
375 else
376 aom_yv12_copy_frame(new_frame, sd, num_planes);
377
378 return cm->error.error_code;
379}
380
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700381static void release_current_frame(AV1Decoder *pbi) {
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700382 AV1_COMMON *const cm = &pbi->common;
383 BufferPool *const pool = cm->buffer_pool;
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700384
Wan-Teh Changc4824892018-11-20 17:22:41 -0800385 cm->cur_frame->buf.corrupted = 1;
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700386 lock_buffer_pool(pool);
David Turnere7ebf902018-12-04 14:04:55 +0000387 decrease_ref_count(cm->cur_frame, pool);
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700388 unlock_buffer_pool(pool);
Wan-Teh Changc4824892018-11-20 17:22:41 -0800389 cm->cur_frame = NULL;
Wan-Teh Chang16c9aff2018-09-28 16:08:21 -0700390}
391
Wan-Teh Change9bfe122018-10-02 11:50:07 -0700392// If any buffer updating is signaled it should be done here.
David Turnere7ebf902018-12-04 14:04:55 +0000393// Consumes a reference to cm->cur_frame.
Wan-Teh Change9bfe122018-10-02 11:50:07 -0700394//
395// This functions returns void. It reports failure by setting
396// cm->error.error_code.
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700397static void update_frame_buffers(AV1Decoder *pbi, int frame_decoded) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700398 int ref_index = 0, mask;
Yaowu Xuf883b422016-08-30 14:01:10 -0700399 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401
David Barker6cc60e22018-05-29 17:38:47 +0100402 if (frame_decoded) {
Frank Bossend8f457a2018-04-30 21:44:12 -0400403 lock_buffer_pool(pool);
Yunqing Wangd7546d42018-06-06 15:08:05 -0700404
405 // In ext-tile decoding, the camera frame header is only decoded once. So,
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700406 // we don't update the references here.
Yunqing Wangd7546d42018-06-06 15:08:05 -0700407 if (!pbi->camera_frame_header_ready) {
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700408 // The following for loop needs to release the reference stored in
409 // cm->ref_frame_map[ref_index] before storing a reference to
410 // cm->cur_frame in cm->ref_frame_map[ref_index].
David Turner996b2c12018-12-07 15:52:30 +0000411 for (mask = cm->current_frame.refresh_frame_flags; mask; mask >>= 1) {
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700412 if (mask & 1) {
413 decrease_ref_count(cm->ref_frame_map[ref_index], pool);
414 cm->ref_frame_map[ref_index] = cm->cur_frame;
415 ++cm->cur_frame->ref_count;
416 }
Yunqing Wangd7546d42018-06-06 15:08:05 -0700417 ++ref_index;
418 }
Vishnu Teja Manyamc1686892020-12-01 20:00:52 +0530419 update_subgop_stats(cm, &pbi->subgop_stats, cm->cur_frame->order_hint,
420 pbi->enable_subgop_stats);
Frank Bossend8f457a2018-04-30 21:44:12 -0400421 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700422
David Barkere8df7ba2018-06-06 16:22:11 +0100423 if (cm->show_existing_frame || cm->show_frame) {
David Barkerf1c07ce2018-06-06 17:50:18 +0100424 if (pbi->output_all_layers) {
425 // Append this frame to the output queue
426 if (pbi->num_output_frames >= MAX_NUM_SPATIAL_LAYERS) {
427 // We can't store the new frame anywhere, so drop it and return an
428 // error
Wan-Teh Changc4824892018-11-20 17:22:41 -0800429 cm->cur_frame->buf.corrupted = 1;
David Turnere7ebf902018-12-04 14:04:55 +0000430 decrease_ref_count(cm->cur_frame, pool);
David Barkerf1c07ce2018-06-06 17:50:18 +0100431 cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
432 } else {
David Turnere7ebf902018-12-04 14:04:55 +0000433 pbi->output_frames[pbi->num_output_frames] = cm->cur_frame;
David Barkerf1c07ce2018-06-06 17:50:18 +0100434 pbi->num_output_frames++;
435 }
436 } else {
437 // Replace any existing output frame
438 assert(pbi->num_output_frames == 0 || pbi->num_output_frames == 1);
439 if (pbi->num_output_frames > 0) {
David Turnere7ebf902018-12-04 14:04:55 +0000440 decrease_ref_count(pbi->output_frames[0], pool);
David Barkerf1c07ce2018-06-06 17:50:18 +0100441 }
David Turnere7ebf902018-12-04 14:04:55 +0000442 pbi->output_frames[0] = cm->cur_frame;
David Barkerf1c07ce2018-06-06 17:50:18 +0100443 pbi->num_output_frames = 1;
David Barkere8df7ba2018-06-06 16:22:11 +0100444 }
David Barkere8df7ba2018-06-06 16:22:11 +0100445 } else {
David Turnere7ebf902018-12-04 14:04:55 +0000446 decrease_ref_count(cm->cur_frame, pool);
David Barkere8df7ba2018-06-06 16:22:11 +0100447 }
448
Frank Bossend8f457a2018-04-30 21:44:12 -0400449 unlock_buffer_pool(pool);
David Barkere8df7ba2018-06-06 16:22:11 +0100450 } else {
451 // Nothing was decoded, so just drop this frame buffer
452 lock_buffer_pool(pool);
David Turnere7ebf902018-12-04 14:04:55 +0000453 decrease_ref_count(cm->cur_frame, pool);
David Barkere8df7ba2018-06-06 16:22:11 +0100454 unlock_buffer_pool(pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700455 }
Wan-Teh Changc4824892018-11-20 17:22:41 -0800456 cm->cur_frame = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700457
Yunqing Wangd7546d42018-06-06 15:08:05 -0700458 if (!pbi->camera_frame_header_ready) {
Yunqing Wangd7546d42018-06-06 15:08:05 -0700459 // Invalidate these references until the next frame starts.
460 for (ref_index = 0; ref_index < INTER_REFS_PER_FRAME; ref_index++) {
David Turnera21966b2018-12-05 14:48:49 +0000461 cm->remapped_ref_idx[ref_index] = INVALID_IDX;
Yunqing Wangd7546d42018-06-06 15:08:05 -0700462 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700463 }
464}
465
Yaowu Xuf883b422016-08-30 14:01:10 -0700466int av1_receive_compressed_data(AV1Decoder *pbi, size_t size,
467 const uint8_t **psource) {
468 AV1_COMMON *volatile const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469 const uint8_t *source = *psource;
Yaowu Xuf883b422016-08-30 14:01:10 -0700470 cm->error.error_code = AOM_CODEC_OK;
Wan-Teh Chang97129d12018-10-01 15:56:38 -0700471 cm->error.has_detail = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472
473 if (size == 0) {
474 // This is used to signal that we are missing frames.
475 // We do not know if the missing frame(s) was supposed to update
476 // any of the reference buffers, but we act conservative and
477 // mark only the last buffer as corrupted.
478 //
479 // TODO(jkoleszar): Error concealment is undefined and non-normative
480 // at this point, but if it becomes so, [0] may not always be the correct
481 // thing to do here.
David Turnera21966b2018-12-05 14:48:49 +0000482 RefCntBuffer *ref_buf = get_ref_frame_buf(cm, LAST_FRAME);
483 if (ref_buf != NULL) ref_buf->buf.corrupted = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484 }
485
David Turner0308a5a2019-01-07 10:36:16 +0000486 if (assign_cur_frame_new_fb(cm) == NULL) {
Wan-Teh Chang332b2ae2018-07-09 14:30:20 -0700487 cm->error.error_code = AOM_CODEC_MEM_ERROR;
488 return 1;
489 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -0700491 // The jmp_buf is valid only for the duration of the function that calls
492 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
493 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700494 if (setjmp(cm->error.jmp)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700495 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700496 int i;
497
498 cm->error.setjmp = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700499
500 // Synchronize all threads immediately as a subsequent decode call may
501 // cause a resize invalidating some allocations.
502 winterface->sync(&pbi->lf_worker);
Cherma Rajan Aa91ed822018-04-23 14:45:49 +0530503 for (i = 0; i < pbi->num_workers; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504 winterface->sync(&pbi->tile_workers[i]);
505 }
506
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700507 release_current_frame(pbi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700508 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509 return -1;
510 }
511
512 cm->error.setjmp = 1;
Soo-Chul Han65c00ae2017-09-07 13:12:35 -0400513
David Barker6cc60e22018-05-29 17:38:47 +0100514 int frame_decoded =
515 aom_decode_frame_from_obus(pbi, source, source + size, psource);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516
Wan-Teh Changf0f76ca2018-09-26 11:36:52 -0700517 if (frame_decoded < 0) {
518 assert(cm->error.error_code != AOM_CODEC_OK);
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700519 release_current_frame(pbi);
Wan-Teh Chang88e4b0a2018-07-03 14:39:50 -0700520 cm->error.setjmp = 0;
Hui Sub390df92018-07-03 10:59:07 -0700521 return 1;
522 }
Hui Sub4d6b1c2018-01-08 13:43:12 -0800523
Jingning Han53c08962017-11-16 14:03:41 -0800524#if TXCOEFF_TIMER
525 cm->cum_txcoeff_timer += cm->txcoeff_timer;
526 fprintf(stderr,
527 "txb coeff block number: %d, frame time: %ld, cum time %ld in us\n",
528 cm->txb_count, cm->txcoeff_timer, cm->cum_txcoeff_timer);
529 cm->txcoeff_timer = 0;
530 cm->txb_count = 0;
531#endif
532
David Turnere7ebf902018-12-04 14:04:55 +0000533 // Note: At this point, this function holds a reference to cm->cur_frame
Wan-Teh Chang6f954e72019-08-30 12:01:26 -0700534 // in the buffer pool. This reference is consumed by update_frame_buffers().
535 update_frame_buffers(pbi, frame_decoded);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536
David Barkerf6f8fa12018-06-20 14:46:05 +0100537 if (frame_decoded) {
538 pbi->decoding_first_frame = 0;
539 }
540
Wan-Teh Chang88e4b0a2018-07-03 14:39:50 -0700541 if (cm->error.error_code != AOM_CODEC_OK) {
542 cm->error.setjmp = 0;
543 return 1;
544 }
David Barkerf1c07ce2018-06-06 17:50:18 +0100545
Yaowu Xuf883b422016-08-30 14:01:10 -0700546 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547
548 if (!cm->show_existing_frame) {
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800549 if (cm->seg.enabled) {
Urvang Joshi9dc909d2020-03-23 16:07:02 -0700550 if (cm->prev_frame &&
551 (cm->mi_params.mi_rows == cm->prev_frame->mi_rows) &&
552 (cm->mi_params.mi_cols == cm->prev_frame->mi_cols)) {
Jonathan Matthews74e8a992018-02-01 11:31:08 +0000553 cm->last_frame_seg_map = cm->prev_frame->seg_map;
554 } else {
555 cm->last_frame_seg_map = NULL;
556 }
Jonathan Matthews74e8a992018-02-01 11:31:08 +0000557 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558 }
559
560 // Update progress in frame parallel decode.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561 cm->error.setjmp = 0;
Frank Bossend8f457a2018-04-30 21:44:12 -0400562
Sebastien Alaiwan56fcf7c2018-01-02 11:21:54 +0100563 return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564}
565
David Barkerf1c07ce2018-06-06 17:50:18 +0100566// Get the frame at a particular index in the output queue
567int av1_get_raw_frame(AV1Decoder *pbi, size_t index, YV12_BUFFER_CONFIG **sd,
568 aom_film_grain_t **grain_params) {
David Barkerf1c07ce2018-06-06 17:50:18 +0100569 if (index >= pbi->num_output_frames) return -1;
David Turnere7ebf902018-12-04 14:04:55 +0000570 *sd = &pbi->output_frames[index]->buf;
571 *grain_params = &pbi->output_frames[index]->film_grain_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700572 aom_clear_system_state();
Sebastien Alaiwan56fcf7c2018-01-02 11:21:54 +0100573 return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700574}
575
David Barkerf1c07ce2018-06-06 17:50:18 +0100576// Get the highest-spatial-layer output
577// TODO(david.barker): What should this do?
Yaowu Xuf883b422016-08-30 14:01:10 -0700578int av1_get_frame_to_show(AV1Decoder *pbi, YV12_BUFFER_CONFIG *frame) {
David Barkerf1c07ce2018-06-06 17:50:18 +0100579 if (pbi->num_output_frames == 0) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700580
David Turnere7ebf902018-12-04 14:04:55 +0000581 *frame = pbi->output_frames[pbi->num_output_frames - 1]->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582 return 0;
583}