blob: ab0f98b423fac476f55c30237d6d7a005f360be7 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -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 <stdlib.h>
13#include <string.h>
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
16#include "./aom_version.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
Yaowu Xuf883b422016-08-30 14:01:10 -070018#include "aom/internal/aom_codec_internal.h"
19#include "aom/aomdx.h"
20#include "aom/aom_decoder.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "aom_dsp/bitreader_buffer.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070022#include "aom_dsp/aom_dsp_common.h"
23#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070024
25#include "av1/common/alloccommon.h"
26#include "av1/common/frame_buffers.h"
27#include "av1/common/enums.h"
28
29#include "av1/decoder/decoder.h"
30#include "av1/decoder/decodeframe.h"
31
Yaowu Xuf883b422016-08-30 14:01:10 -070032#include "av1/av1_iface_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033
Yaowu Xuc27fc142016-08-22 16:08:15 -070034// This limit is due to framebuffer numbers.
35// TODO(hkuang): Remove this limit after implementing ondemand framebuffers.
36#define FRAME_CACHE_SIZE 6 // Cache maximum 6 decoded frames.
37
38typedef struct cache_frame {
39 int fb_idx;
Yaowu Xuf883b422016-08-30 14:01:10 -070040 aom_image_t img;
Yaowu Xuc27fc142016-08-22 16:08:15 -070041} cache_frame;
42
Yaowu Xuf883b422016-08-30 14:01:10 -070043struct aom_codec_alg_priv {
44 aom_codec_priv_t base;
45 aom_codec_dec_cfg_t cfg;
Ralph Gilesafe71d92017-05-03 13:18:57 -070046 aom_codec_stream_info_t si;
Yaowu Xuc27fc142016-08-22 16:08:15 -070047 int postproc_cfg_set;
Yaowu Xuf883b422016-08-30 14:01:10 -070048 aom_postproc_cfg_t postproc_cfg;
Yaowu Xuf883b422016-08-30 14:01:10 -070049 aom_image_t img;
Yaowu Xuc27fc142016-08-22 16:08:15 -070050 int img_avail;
51 int flushed;
52 int invert_tile_order;
53 int last_show_frame; // Index of last output frame.
54 int byte_alignment;
55 int skip_loop_filter;
56 int decode_tile_row;
57 int decode_tile_col;
Yunqing Wang8ae64a92018-01-12 12:26:44 -080058 unsigned int tile_mode;
Yaowu Xuc27fc142016-08-22 16:08:15 -070059
60 // Frame parallel related.
61 int frame_parallel_decode; // frame-based threading.
Yaowu Xuf883b422016-08-30 14:01:10 -070062 AVxWorker *frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -070063 int num_frame_workers;
64 int next_submit_worker_id;
65 int last_submit_worker_id;
66 int next_output_worker_id;
67 int available_threads;
68 cache_frame frame_cache[FRAME_CACHE_SIZE];
69 int frame_cache_write;
70 int frame_cache_read;
71 int num_cache_frames;
72 int need_resync; // wait for key/intra-only frame
73 // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
74 BufferPool *buffer_pool;
75
Yaowu Xuf883b422016-08-30 14:01:10 -070076 // External frame buffer info to save for AV1 common.
Yaowu Xuc27fc142016-08-22 16:08:15 -070077 void *ext_priv; // Private data associated with the external frame buffers.
Yaowu Xuf883b422016-08-30 14:01:10 -070078 aom_get_frame_buffer_cb_fn_t get_ext_fb_cb;
79 aom_release_frame_buffer_cb_fn_t release_ext_fb_cb;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050080
81#if CONFIG_INSPECTION
82 aom_inspect_cb inspect_cb;
83 void *inspect_ctx;
84#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070085};
86
Yaowu Xuf883b422016-08-30 14:01:10 -070087static aom_codec_err_t decoder_init(aom_codec_ctx_t *ctx,
88 aom_codec_priv_enc_mr_cfg_t *data) {
89 // This function only allocates space for the aom_codec_alg_priv_t
Yaowu Xuc27fc142016-08-22 16:08:15 -070090 // structure. More memory may be required at the time the stream
91 // information becomes known.
92 (void)data;
93
94 if (!ctx->priv) {
Yaowu Xuf883b422016-08-30 14:01:10 -070095 aom_codec_alg_priv_t *const priv =
96 (aom_codec_alg_priv_t *)aom_calloc(1, sizeof(*priv));
97 if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -070098
Yaowu Xuf883b422016-08-30 14:01:10 -070099 ctx->priv = (aom_codec_priv_t *)priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700100 ctx->priv->init_flags = ctx->init_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101 priv->flushed = 0;
102 // Only do frame parallel decode when threads > 1.
103 priv->frame_parallel_decode =
104 (ctx->config.dec && (ctx->config.dec->threads > 1) &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700105 (ctx->init_flags & AOM_CODEC_USE_FRAME_THREADING))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700106 ? 1
107 : 0;
Thomas Daede85b49002017-07-10 17:43:06 -0700108 // TODO(tdaede): this should not be exposed to the API
109 priv->cfg.allow_lowbitdepth = CONFIG_LOWBITDEPTH;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700110 if (ctx->config.dec) {
111 priv->cfg = *ctx->config.dec;
112 ctx->config.dec = &priv->cfg;
113 }
114 }
115
Yaowu Xuf883b422016-08-30 14:01:10 -0700116 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700117}
118
Yaowu Xuf883b422016-08-30 14:01:10 -0700119static aom_codec_err_t decoder_destroy(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700120 if (ctx->frame_workers != NULL) {
121 int i;
122 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700123 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700124 FrameWorkerData *const frame_worker_data =
125 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700126 aom_get_worker_interface()->end(worker);
Yaowu Xu568bf102017-10-17 12:43:27 -0700127#if CONFIG_MFMV
128 aom_free(frame_worker_data->pbi->common.tpl_mvs);
129 frame_worker_data->pbi->common.tpl_mvs = NULL;
130#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 av1_remove_common(&frame_worker_data->pbi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700133 av1_free_restoration_buffers(&frame_worker_data->pbi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700135 av1_decoder_remove(frame_worker_data->pbi);
136 aom_free(frame_worker_data->scratch_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700137#if CONFIG_MULTITHREAD
138 pthread_mutex_destroy(&frame_worker_data->stats_mutex);
139 pthread_cond_destroy(&frame_worker_data->stats_cond);
140#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 aom_free(frame_worker_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700142 }
143#if CONFIG_MULTITHREAD
144 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
145#endif
146 }
147
148 if (ctx->buffer_pool) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700149 av1_free_ref_frame_buffers(ctx->buffer_pool);
150 av1_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 }
152
Yaowu Xuf883b422016-08-30 14:01:10 -0700153 aom_free(ctx->frame_workers);
154 aom_free(ctx->buffer_pool);
155 aom_free(ctx);
156 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700157}
158
Soo-Chul Han65c00ae12017-09-07 13:12:35 -0400159#if !CONFIG_OBU
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800160static aom_bit_depth_t parse_bitdepth(BITSTREAM_PROFILE profile,
161 struct aom_read_bit_buffer *rb) {
162 aom_bit_depth_t bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_10 : AOM_BITS_8;
163 if (profile < PROFILE_2 || bit_depth == AOM_BITS_8) {
164 return bit_depth;
165 }
166 bit_depth = aom_rb_read_bit(rb) ? AOM_BITS_12 : AOM_BITS_10;
167 return bit_depth;
168}
169
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170static int parse_bitdepth_colorspace_sampling(BITSTREAM_PROFILE profile,
Yaowu Xuf883b422016-08-30 14:01:10 -0700171 struct aom_read_bit_buffer *rb) {
Andrey Norkin9e694632017-12-21 18:50:57 -0800172#if CONFIG_CICP
173 aom_color_primaries_t color_primaries;
174 aom_transfer_characteristics_t transfer_characteristics;
175 aom_matrix_coefficients_t matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800176#else
177 aom_color_space_t color_space = AOM_CS_UNKNOWN;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800178#endif // CONFIG_CICP
anorkin76fb1262017-03-22 15:12:12 -0700179 int subsampling_x = 0;
180 int subsampling_y = 0;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800181 (void)subsampling_x;
182 (void)subsampling_y;
anorkin76fb1262017-03-22 15:12:12 -0700183
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800184 aom_bit_depth_t bit_depth = parse_bitdepth(profile, rb);
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800185#if CONFIG_MONO_VIDEO
186 // Monochrome bit
187 const int is_monochrome = profile != PROFILE_1 ? aom_rb_read_bit(rb) : 0;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800188#else
189 const int is_monochrome = 0;
190#endif // CONFIG_MONO_VIDEO
191
Andrey Norkin9e694632017-12-21 18:50:57 -0800192#if CONFIG_CICP
193 int color_description_present_flag = aom_rb_read_bit(rb);
194 if (color_description_present_flag) {
195 color_primaries = (aom_color_primaries_t)aom_rb_read_literal(rb, 8);
196 transfer_characteristics =
197 (aom_transfer_characteristics_t)aom_rb_read_literal(rb, 8);
198 matrix_coefficients = (aom_matrix_coefficients_t)aom_rb_read_literal(rb, 8);
199 } else {
200 color_primaries = AOM_CICP_CP_UNSPECIFIED;
201 transfer_characteristics = AOM_CICP_TC_UNSPECIFIED;
202 matrix_coefficients = AOM_CICP_MC_UNSPECIFIED;
203 }
204#else
anorkin76fb1262017-03-22 15:12:12 -0700205#if CONFIG_COLORSPACE_HEADERS
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800206 if (!is_monochrome) {
207 color_space = (aom_color_space_t)aom_rb_read_literal(rb, 5);
208 }
anorkin76fb1262017-03-22 15:12:12 -0700209 rb->bit_offset += 5; // Transfer function
210#else
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800211 if (!is_monochrome) {
212 color_space = (aom_color_space_t)aom_rb_read_literal(rb, 4);
213 }
214#endif // CONFIG_COLORSPACE_HEADERS
215#endif // CONFIG_CICP
Debargha Mukherjeee5267692018-01-16 09:41:15 -0800216#if CONFIG_MONO_VIDEO
217 if (is_monochrome) return 1;
218#endif // CONFIG_MONO_VIDEO
Andrey Norkin9e694632017-12-21 18:50:57 -0800219#if CONFIG_CICP
220 if (color_primaries == AOM_CICP_CP_BT_709 &&
221 transfer_characteristics == AOM_CICP_TC_SRGB &&
222 matrix_coefficients == AOM_CICP_MC_IDENTITY) { // it would be better to
223 // remove this dependency
224 // too
225#else
Imdad Sardharwalla317002f2017-12-05 16:24:56 +0000226 if (color_space == AOM_CS_SRGB) {
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800227#endif // CONFIG_CICP
228 if (!(profile == PROFILE_1 ||
229 (profile == PROFILE_2 && bit_depth == AOM_BITS_12))) {
Imdad Sardharwalla317002f2017-12-05 16:24:56 +0000230 return 0;
231 }
Imdad Sardharwalla317002f2017-12-05 16:24:56 +0000232 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700233 rb->bit_offset += 1; // [16,235] (including xvycc) vs [0,255] range.
anorkin76fb1262017-03-22 15:12:12 -0700234
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800235 if (profile == PROFILE_0) {
anorkin76fb1262017-03-22 15:12:12 -0700236 subsampling_x = 1;
237 subsampling_y = 1;
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800238 } else if (profile == PROFILE_1) {
239 subsampling_x = 0;
240 subsampling_y = 0;
241 } else if (profile == PROFILE_2) {
242 if (bit_depth == AOM_BITS_12) {
243 subsampling_x = aom_rb_read_bit(rb);
David Barker0c3545b2018-01-16 17:32:23 +0000244 if (subsampling_x == 0)
245 subsampling_y = 0;
246 else
247 subsampling_y = aom_rb_read_bit(rb);
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800248 } else {
249 subsampling_x = 1;
250 subsampling_y = 0;
251 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700252 }
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800253#if CONFIG_COLORSPACE_HEADERS
anorkin76fb1262017-03-22 15:12:12 -0700254 if (subsampling_x == 1 && subsampling_y == 1) {
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800255 rb->bit_offset += 2; // chroma_sample_position
anorkin76fb1262017-03-22 15:12:12 -0700256 }
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800257#endif // CONFIG_COLORSPACE_HEADERS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 }
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -0800259#if CONFIG_EXT_QM
260 rb->bit_offset += 1; // separate_uv_delta_q
261#endif // CONFIG_EXT_QM
Yaowu Xuc27fc142016-08-22 16:08:15 -0700262 return 1;
263}
Soo-Chul Han65c00ae12017-09-07 13:12:35 -0400264#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100266static aom_codec_err_t decoder_peek_si_internal(const uint8_t *data,
267 unsigned int data_sz,
268 aom_codec_stream_info_t *si,
269 int *is_intra_only) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 int intra_only_flag = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700271
Yaowu Xuf883b422016-08-30 14:01:10 -0700272 if (data + data_sz <= data) return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700273
274 si->is_kf = 0;
275 si->w = si->h = 0;
276
Sebastien Alaiwan6bec6ec2017-08-07 09:37:52 +0200277 // skip a potential superframe index
278 {
279 uint32_t frame_sizes[8];
280 int frame_count;
281 int index_size = 0;
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100282 aom_codec_err_t res = av1_parse_superframe_index(data, data_sz, frame_sizes,
283 &frame_count, &index_size);
Sebastien Alaiwan6bec6ec2017-08-07 09:37:52 +0200284 if (res != AOM_CODEC_OK) return res;
285
286 data += index_size;
287 data_sz -= index_size;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -0400288#if CONFIG_OBU
289 if (data + data_sz <= data) return AOM_CODEC_INVALID_PARAM;
290#endif
Sebastien Alaiwan6bec6ec2017-08-07 09:37:52 +0200291 }
292
Yaowu Xuc27fc142016-08-22 16:08:15 -0700293 {
Soo-Chul Han65c00ae12017-09-07 13:12:35 -0400294#if CONFIG_OBU
295 // Proper fix needed
296 si->is_kf = 1;
297 intra_only_flag = 1;
298 si->h = 1;
Soo-Chul Han65c00ae12017-09-07 13:12:35 -0400299#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700300 int show_frame;
301 int error_resilient;
Yaowu Xuf883b422016-08-30 14:01:10 -0700302 struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
303 const int frame_marker = aom_rb_read_literal(&rb, 2);
304 const BITSTREAM_PROFILE profile = av1_read_profile(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700305
Yaowu Xuf883b422016-08-30 14:01:10 -0700306 if (frame_marker != AOM_FRAME_MARKER) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307
Yaowu Xuf883b422016-08-30 14:01:10 -0700308 if (profile >= MAX_PROFILES) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309
310 if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
Yaowu Xuf883b422016-08-30 14:01:10 -0700311 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700312
Yaowu Xuf883b422016-08-30 14:01:10 -0700313 if (aom_rb_read_bit(&rb)) { // show an existing frame
314 aom_rb_read_literal(&rb, 3); // Frame buffer to show.
315 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316 }
317
Yaowu Xuf883b422016-08-30 14:01:10 -0700318 if (data_sz <= 8) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319
Yaowu Xuf883b422016-08-30 14:01:10 -0700320 si->is_kf = !aom_rb_read_bit(&rb);
321 show_frame = aom_rb_read_bit(&rb);
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700322 if (!si->is_kf) {
323 if (!show_frame) intra_only_flag = show_frame ? 0 : aom_rb_read_bit(&rb);
324 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700325 error_resilient = aom_rb_read_bit(&rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100326#if CONFIG_REFERENCE_BUFFER
Rupert Swarbricke0b15992017-11-09 15:04:32 +0000327 SequenceHeader seq_params;
328 memset(&seq_params, 0, sizeof(seq_params));
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700329 if (si->is_kf) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100330 /* TODO: Move outside frame loop or inside key-frame branch */
David Barker5e70a112017-10-03 14:28:17 +0100331 read_sequence_header(&seq_params, &rb);
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700332 }
333#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +0100334
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700335#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +0100336 if (seq_params.frame_id_numbers_present_flag) {
Frederic Barbiere83fcfe2017-10-13 10:37:50 +0200337 aom_rb_read_literal(&rb, seq_params.frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100338 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700339#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +0100340
341#if CONFIG_FRAME_SIZE
342 int frame_size_override_flag = aom_rb_read_bit(&rb);
343#endif
344
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345 if (si->is_kf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700346 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700347 return AOM_CODEC_UNSUP_BITSTREAM;
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +0100348#if CONFIG_FRAME_SIZE
349 if (frame_size_override_flag) {
350 int num_bits_width = seq_params.num_bits_width;
351 int num_bits_height = seq_params.num_bits_height;
352 av1_read_frame_size(&rb, num_bits_width, num_bits_height, (int *)&si->w,
353 (int *)&si->h);
354 } else {
355 si->w = seq_params.max_frame_width;
356 si->h = seq_params.max_frame_height;
357 }
358#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700359 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +0100360#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700361 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
363
364 if (intra_only_flag) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700365 if (profile > PROFILE_0) {
366 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700367 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700368 }
369 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +0100370#if CONFIG_FRAME_SIZE
371 if (frame_size_override_flag) {
372 int num_bits_width = seq_params.num_bits_width;
373 int num_bits_height = seq_params.num_bits_height;
374 av1_read_frame_size(&rb, num_bits_width, num_bits_height,
375 (int *)&si->w, (int *)&si->h);
376 } else {
377 si->w = seq_params.max_frame_width;
378 si->h = seq_params.max_frame_height;
379 }
380#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700381 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Arild Fuldseth (arilfuld)7193f022017-10-30 12:24:57 +0100382#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383 }
384 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700385#endif // CONFIG_OBU
Yaowu Xuc27fc142016-08-22 16:08:15 -0700386 }
387 if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
Yaowu Xuf883b422016-08-30 14:01:10 -0700388 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389}
390
Yaowu Xuf883b422016-08-30 14:01:10 -0700391static aom_codec_err_t decoder_peek_si(const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392 unsigned int data_sz,
Yaowu Xuf883b422016-08-30 14:01:10 -0700393 aom_codec_stream_info_t *si) {
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100394 return decoder_peek_si_internal(data, data_sz, si, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395}
396
Yaowu Xuf883b422016-08-30 14:01:10 -0700397static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
398 aom_codec_stream_info_t *si) {
Ralph Gilesafe71d92017-05-03 13:18:57 -0700399 memcpy(si, &ctx->si, sizeof(*si));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400
Yaowu Xuf883b422016-08-30 14:01:10 -0700401 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402}
403
Yaowu Xuf883b422016-08-30 14:01:10 -0700404static void set_error_detail(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700405 const char *const error) {
406 ctx->base.err_detail = error;
407}
408
Yaowu Xuf883b422016-08-30 14:01:10 -0700409static aom_codec_err_t update_error_state(
410 aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411 if (error->error_code)
412 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
413
414 return error->error_code;
415}
416
Yaowu Xuf883b422016-08-30 14:01:10 -0700417static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700418 int i;
419
420 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700421 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700422 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700423 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700424 BufferPool *const pool = cm->buffer_pool;
425
426 cm->new_fb_idx = INVALID_IDX;
427 cm->byte_alignment = ctx->byte_alignment;
428 cm->skip_loop_filter = ctx->skip_loop_filter;
429
430 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
431 pool->get_fb_cb = ctx->get_ext_fb_cb;
432 pool->release_fb_cb = ctx->release_ext_fb_cb;
433 pool->cb_priv = ctx->ext_priv;
434 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700435 pool->get_fb_cb = av1_get_frame_buffer;
436 pool->release_fb_cb = av1_release_frame_buffer;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437
Yaowu Xuf883b422016-08-30 14:01:10 -0700438 if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
439 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700440 "Failed to initialize internal frame buffers");
441
442 pool->cb_priv = &pool->int_frame_buffers;
443 }
444 }
445}
446
Yaowu Xuf883b422016-08-30 14:01:10 -0700447static void set_default_ppflags(aom_postproc_cfg_t *cfg) {
448 cfg->post_proc_flag = AOM_DEBLOCK | AOM_DEMACROBLOCK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700449 cfg->deblocking_level = 4;
450 cfg->noise_level = 0;
451}
452
453static int frame_worker_hook(void *arg1, void *arg2) {
454 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
455 const uint8_t *data = frame_worker_data->data;
456 (void)arg2;
457
Yaowu Xuf883b422016-08-30 14:01:10 -0700458 frame_worker_data->result = av1_receive_compressed_data(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700459 frame_worker_data->pbi, frame_worker_data->data_size, &data);
460 frame_worker_data->data_end = data;
461
462 if (frame_worker_data->pbi->common.frame_parallel_decode) {
463 // In frame parallel decoding, a worker thread must successfully decode all
464 // the compressed data.
465 if (frame_worker_data->result != 0 ||
466 frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700467 AVxWorker *const worker = frame_worker_data->pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700468 BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
469 // Signal all the other threads that are waiting for this frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700470 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700471 frame_worker_data->frame_context_ready = 1;
472 lock_buffer_pool(pool);
473 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
474 unlock_buffer_pool(pool);
475 frame_worker_data->pbi->need_resync = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700476 av1_frameworker_signal_stats(worker);
477 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700478 return 0;
479 }
480 } else if (frame_worker_data->result != 0) {
481 // Check decode result in serial decode.
482 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
483 frame_worker_data->pbi->need_resync = 1;
484 }
485 return !frame_worker_data->result;
486}
487
Yaowu Xuf883b422016-08-30 14:01:10 -0700488static aom_codec_err_t init_decoder(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700489 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700490 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700491
492 ctx->last_show_frame = -1;
493 ctx->next_submit_worker_id = 0;
494 ctx->last_submit_worker_id = 0;
495 ctx->next_output_worker_id = 0;
496 ctx->frame_cache_read = 0;
497 ctx->frame_cache_write = 0;
498 ctx->num_cache_frames = 0;
499 ctx->need_resync = 1;
500 ctx->num_frame_workers =
501 (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads : 1;
502 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
503 ctx->num_frame_workers = MAX_DECODE_THREADS;
504 ctx->available_threads = ctx->num_frame_workers;
505 ctx->flushed = 0;
506
Yaowu Xuf883b422016-08-30 14:01:10 -0700507 ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
508 if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509
510#if CONFIG_MULTITHREAD
511 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
512 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700513 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700514 }
515#endif
516
Yaowu Xuf883b422016-08-30 14:01:10 -0700517 ctx->frame_workers = (AVxWorker *)aom_malloc(ctx->num_frame_workers *
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518 sizeof(*ctx->frame_workers));
519 if (ctx->frame_workers == NULL) {
520 set_error_detail(ctx, "Failed to allocate frame_workers");
Yaowu Xuf883b422016-08-30 14:01:10 -0700521 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522 }
523
524 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700525 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700526 FrameWorkerData *frame_worker_data = NULL;
527 winterface->init(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -0700528 worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529 if (worker->data1 == NULL) {
530 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700531 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532 }
533 frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700534 frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535 if (frame_worker_data->pbi == NULL) {
536 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700537 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538 }
539 frame_worker_data->pbi->frame_worker_owner = worker;
540 frame_worker_data->worker_id = i;
541 frame_worker_data->scratch_buffer = NULL;
542 frame_worker_data->scratch_buffer_size = 0;
543 frame_worker_data->frame_context_ready = 0;
544 frame_worker_data->received_frame = 0;
545#if CONFIG_MULTITHREAD
546 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
547 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700548 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549 }
550
551 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
552 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
Yaowu Xuf883b422016-08-30 14:01:10 -0700553 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554 }
555#endif
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +0200556 frame_worker_data->pbi->allow_lowbitdepth = ctx->cfg.allow_lowbitdepth;
557
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558 // If decoding in serial mode, FrameWorker thread could create tile worker
559 // thread or loopfilter thread.
560 frame_worker_data->pbi->max_threads =
561 (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
563 frame_worker_data->pbi->common.frame_parallel_decode =
564 ctx->frame_parallel_decode;
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800565#if CONFIG_EXT_TILE
566 frame_worker_data->pbi->common.large_scale_tile = ctx->tile_mode;
567 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
568 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
569#endif // CONFIG_EXT_TILE
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 worker->hook = (AVxWorkerHook)frame_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700571 if (!winterface->reset(worker)) {
572 set_error_detail(ctx, "Frame Worker thread creation failed");
Yaowu Xuf883b422016-08-30 14:01:10 -0700573 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700574 }
575 }
576
577 // If postprocessing was enabled by the application and a
578 // configuration has not been provided, default it.
Yaowu Xuf883b422016-08-30 14:01:10 -0700579 if (!ctx->postproc_cfg_set && (ctx->base.init_flags & AOM_CODEC_USE_POSTPROC))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700580 set_default_ppflags(&ctx->postproc_cfg);
581
582 init_buffer_callbacks(ctx);
583
Yaowu Xuf883b422016-08-30 14:01:10 -0700584 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585}
586
Yaowu Xuf883b422016-08-30 14:01:10 -0700587static INLINE void check_resync(aom_codec_alg_priv_t *const ctx,
588 const AV1Decoder *const pbi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700589 // Clear resync flag if worker got a key frame or intra only frame.
590 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
591 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
592 ctx->need_resync = 0;
593}
594
Yaowu Xuf883b422016-08-30 14:01:10 -0700595static aom_codec_err_t decode_one(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 const uint8_t **data, unsigned int data_sz,
597 void *user_priv, int64_t deadline) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700598 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700599 (void)deadline;
600
601 // Determine the stream parameters. Note that we rely on peek_si to
602 // validate that we have a buffer that does not wrap around the top
603 // of the heap.
604 if (!ctx->si.h) {
605 int is_intra_only = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700606 const aom_codec_err_t res =
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100607 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only);
Yaowu Xuf883b422016-08-30 14:01:10 -0700608 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609
Yaowu Xuf883b422016-08-30 14:01:10 -0700610 if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700611 }
612
613 if (!ctx->frame_parallel_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700614 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700615 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
616 frame_worker_data->data = *data;
617 frame_worker_data->data_size = data_sz;
618 frame_worker_data->user_priv = user_priv;
619 frame_worker_data->received_frame = 1;
620
Nathan E. Egge2cf03b12017-02-22 16:19:59 -0500621#if CONFIG_INSPECTION
622 frame_worker_data->pbi->inspect_cb = ctx->inspect_cb;
623 frame_worker_data->pbi->inspect_ctx = ctx->inspect_ctx;
624#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626 worker->had_error = 0;
627 winterface->execute(worker);
628
629 // Update data pointer after decode.
630 *data = frame_worker_data->data_end;
631
632 if (worker->had_error)
633 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
634
635 check_resync(ctx, frame_worker_data->pbi);
636 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700637 AVxWorker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
639 // Copy context from last worker thread to next worker thread.
640 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
Yaowu Xuf883b422016-08-30 14:01:10 -0700641 av1_frameworker_copy_context(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700642 &ctx->frame_workers[ctx->next_submit_worker_id],
643 &ctx->frame_workers[ctx->last_submit_worker_id]);
644
645 frame_worker_data->pbi->ready_for_new_data = 0;
646 // Copy the compressed data into worker's internal buffer.
647 // TODO(hkuang): Will all the workers allocate the same size
648 // as the size of the first intra frame be better? This will
649 // avoid too many deallocate and allocate.
650 if (frame_worker_data->scratch_buffer_size < data_sz) {
Alex Converse7f094f12017-02-23 17:29:40 -0800651 aom_free(frame_worker_data->scratch_buffer);
652 frame_worker_data->scratch_buffer = (uint8_t *)aom_malloc(data_sz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653 if (frame_worker_data->scratch_buffer == NULL) {
654 set_error_detail(ctx, "Failed to reallocate scratch buffer");
Yaowu Xuf883b422016-08-30 14:01:10 -0700655 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700656 }
657 frame_worker_data->scratch_buffer_size = data_sz;
658 }
659 frame_worker_data->data_size = data_sz;
660 memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
661
662 frame_worker_data->frame_decoded = 0;
663 frame_worker_data->frame_context_ready = 0;
664 frame_worker_data->received_frame = 1;
665 frame_worker_data->data = frame_worker_data->scratch_buffer;
666 frame_worker_data->user_priv = user_priv;
667
668 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
669 ctx->last_submit_worker_id =
670 (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
671
672 ctx->next_submit_worker_id =
673 (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
674 --ctx->available_threads;
675 worker->had_error = 0;
676 winterface->launch(worker);
677 }
678
Yaowu Xuf883b422016-08-30 14:01:10 -0700679 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700680}
681
Yaowu Xuf883b422016-08-30 14:01:10 -0700682static void wait_worker_and_cache_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700684 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
685 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
687 ctx->next_output_worker_id =
688 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
689 // TODO(hkuang): Add worker error handling here.
690 winterface->sync(worker);
691 frame_worker_data->received_frame = 0;
692 ++ctx->available_threads;
693
694 check_resync(ctx, frame_worker_data->pbi);
695
Yaowu Xuf883b422016-08-30 14:01:10 -0700696 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
697 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
699 ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
700 yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
701 frame_worker_data->user_priv);
702 ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
703 frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
704 ctx->frame_cache_write = (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
705 ++ctx->num_cache_frames;
706 }
707}
708
Yaowu Xuf883b422016-08-30 14:01:10 -0700709static aom_codec_err_t decoder_decode(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 const uint8_t *data, unsigned int data_sz,
711 void *user_priv, long deadline) {
712 const uint8_t *data_start = data;
713 const uint8_t *const data_end = data + data_sz;
Yaowu Xub02d0b12017-12-15 01:32:34 +0000714 aom_codec_err_t res = AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700715 uint32_t frame_sizes[8];
Soo-Chul Han38427e82017-09-27 15:06:13 -0400716 int frame_count = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700717
718 if (data == NULL && data_sz == 0) {
719 ctx->flushed = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700720 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721 }
722
723 // Reset flushed when receiving a valid frame.
724 ctx->flushed = 0;
725
726 // Initialize the decoder workers on the first frame.
727 if (ctx->frame_workers == NULL) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700728 res = init_decoder(ctx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 }
Soo-Chul Han38427e82017-09-27 15:06:13 -0400731#if !CONFIG_OBU
Sebastien Alaiwane4c6fc12017-06-21 16:43:22 +0200732 int index_size = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700733 res = av1_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100734 &index_size);
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736
Sebastien Alaiwane4c6fc12017-06-21 16:43:22 +0200737 data_start += index_size;
Soo-Chul Han38427e82017-09-27 15:06:13 -0400738#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739 if (ctx->frame_parallel_decode) {
740 // Decode in frame parallel mode. When decoding in this mode, the frame
741 // passed to the decoder must be either a normal frame or a superframe with
742 // superframe index so the decoder could get each frame's start position
743 // in the superframe.
744 if (frame_count > 0) {
745 int i;
746
747 for (i = 0; i < frame_count; ++i) {
748 const uint8_t *data_start_copy = data_start;
749 const uint32_t frame_size = frame_sizes[i];
750 if (data_start < data ||
751 frame_size > (uint32_t)(data_end - data_start)) {
752 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700753 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754 }
755
756 if (ctx->available_threads == 0) {
757 // No more threads for decoding. Wait until the next output worker
758 // finishes decoding. Then copy the decoded frame into cache.
759 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
760 wait_worker_and_cache_frame(ctx);
761 } else {
762 // TODO(hkuang): Add unit test to test this path.
763 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700764 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 }
766 }
767
768 res =
769 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 data_start += frame_size;
772 }
773 } else {
774 if (ctx->available_threads == 0) {
775 // No more threads for decoding. Wait until the next output worker
776 // finishes decoding. Then copy the decoded frame into cache.
777 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
778 wait_worker_and_cache_frame(ctx);
779 } else {
780 // TODO(hkuang): Add unit test to test this path.
781 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700782 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783 }
784 }
785
786 res = decode_one(ctx, &data, data_sz, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700787 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788 }
789 } else {
790 // Decode in serial mode.
791 if (frame_count > 0) {
792 int i;
793
794 for (i = 0; i < frame_count; ++i) {
795 const uint8_t *data_start_copy = data_start;
796 const uint32_t frame_size = frame_sizes[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797 if (data_start < data ||
798 frame_size > (uint32_t)(data_end - data_start)) {
799 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700800 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801 }
802
803 res =
804 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700805 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806
807 data_start += frame_size;
808 }
809 } else {
810 while (data_start < data_end) {
811 const uint32_t frame_size = (uint32_t)(data_end - data_start);
Urvang Joshi454280d2016-10-14 16:51:44 -0700812 res = decode_one(ctx, &data_start, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700813 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814
815 // Account for suboptimal termination by the encoder.
816 while (data_start < data_end) {
Sebastien Alaiwan64c23112017-12-21 09:49:23 +0100817 const uint8_t marker = data_start[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 if (marker) break;
819 ++data_start;
820 }
821 }
822 }
823 }
824
825 return res;
826}
827
Yaowu Xuf883b422016-08-30 14:01:10 -0700828static void release_last_output_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829 RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
830 // Decrease reference count of last output frame in frame parallel mode.
831 if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
832 BufferPool *const pool = ctx->buffer_pool;
833 lock_buffer_pool(pool);
834 decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
835 unlock_buffer_pool(pool);
836 }
837}
838
Yaowu Xuf883b422016-08-30 14:01:10 -0700839static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
840 aom_codec_iter_t *iter) {
841 aom_image_t *img = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842
843 // Only return frame when all the cpu are busy or
844 // application fluhsed the decoder in frame parallel decode.
845 if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
846 !ctx->flushed) {
847 return NULL;
848 }
849
850 // Output the frames in the cache first.
851 if (ctx->num_cache_frames > 0) {
852 release_last_output_frame(ctx);
853 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
854 if (ctx->need_resync) return NULL;
855 img = &ctx->frame_cache[ctx->frame_cache_read].img;
856 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
857 --ctx->num_cache_frames;
858 return img;
859 }
860
861 // iter acts as a flip flop, so an image is only returned on the first
862 // call to get_frame.
863 if (*iter == NULL && ctx->frame_workers != NULL) {
864 do {
865 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700866 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
867 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 FrameWorkerData *const frame_worker_data =
869 (FrameWorkerData *)worker->data1;
870 ctx->next_output_worker_id =
871 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
872 // Wait for the frame from worker thread.
873 if (winterface->sync(worker)) {
874 // Check if worker has received any frames.
875 if (frame_worker_data->received_frame == 1) {
876 ++ctx->available_threads;
877 frame_worker_data->received_frame = 0;
878 check_resync(ctx, frame_worker_data->pbi);
879 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700880 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
881 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
883 release_last_output_frame(ctx);
884 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
885 if (ctx->need_resync) return NULL;
886 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
887
888#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700889 if (cm->single_tile_decoding &&
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800890 frame_worker_data->pbi->dec_tile_row >= 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891 const int tile_row =
Yaowu Xuf883b422016-08-30 14:01:10 -0700892 AOMMIN(frame_worker_data->pbi->dec_tile_row, cm->tile_rows - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 const int mi_row = tile_row * cm->tile_height;
894 const int ssy = ctx->img.y_chroma_shift;
895 int plane;
896 ctx->img.planes[0] += mi_row * MI_SIZE * ctx->img.stride[0];
897 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
898 ctx->img.planes[plane] +=
899 mi_row * (MI_SIZE >> ssy) * ctx->img.stride[plane];
900 }
901 ctx->img.d_h =
Yaowu Xuf883b422016-08-30 14:01:10 -0700902 AOMMIN(cm->tile_height, cm->mi_rows - mi_row) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903 }
904
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700905 if (cm->single_tile_decoding &&
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800906 frame_worker_data->pbi->dec_tile_col >= 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700907 const int tile_col =
Yaowu Xuf883b422016-08-30 14:01:10 -0700908 AOMMIN(frame_worker_data->pbi->dec_tile_col, cm->tile_cols - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700909 const int mi_col = tile_col * cm->tile_width;
910 const int ssx = ctx->img.x_chroma_shift;
911 int plane;
912 ctx->img.planes[0] += mi_col * MI_SIZE;
913 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
914 ctx->img.planes[plane] += mi_col * (MI_SIZE >> ssx);
915 }
916 ctx->img.d_w =
Yaowu Xuf883b422016-08-30 14:01:10 -0700917 AOMMIN(cm->tile_width, cm->mi_cols - mi_col) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700918 }
919#endif // CONFIG_EXT_TILE
920
921 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
922 img = &ctx->img;
923 return img;
924 }
925 } else {
926 // Decoding failed. Release the worker thread.
927 frame_worker_data->received_frame = 0;
928 ++ctx->available_threads;
929 ctx->need_resync = 1;
930 if (ctx->flushed != 1) return NULL;
931 }
932 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
933 }
934 return NULL;
935}
936
Yaowu Xuf883b422016-08-30 14:01:10 -0700937static aom_codec_err_t decoder_set_fb_fn(
938 aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
939 aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700940 if (cb_get == NULL || cb_release == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700941 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942 } else if (ctx->frame_workers == NULL) {
943 // If the decoder has already been initialized, do not accept changes to
944 // the frame buffer functions.
945 ctx->get_ext_fb_cb = cb_get;
946 ctx->release_ext_fb_cb = cb_release;
947 ctx->ext_priv = cb_priv;
Yaowu Xuf883b422016-08-30 14:01:10 -0700948 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949 }
950
Yaowu Xuf883b422016-08-30 14:01:10 -0700951 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700952}
953
Yaowu Xuf883b422016-08-30 14:01:10 -0700954static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700955 va_list args) {
Thomas Daede497d1952017-08-08 17:33:06 -0700956 av1_ref_frame_t *const data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957
958 // Only support this function in serial decode.
959 if (ctx->frame_parallel_decode) {
960 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700961 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962 }
963
964 if (data) {
Thomas Daede497d1952017-08-08 17:33:06 -0700965 av1_ref_frame_t *const frame = data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700966 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700967 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700968 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
969 image2yuvconfig(&frame->img, &sd);
Thomas Daede497d1952017-08-08 17:33:06 -0700970 return av1_set_reference_dec(&frame_worker_data->pbi->common, frame->idx,
Yaowu Xuf883b422016-08-30 14:01:10 -0700971 &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700973 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700974 }
975}
976
Yaowu Xuf883b422016-08-30 14:01:10 -0700977static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700978 va_list args) {
Thomas Daede497d1952017-08-08 17:33:06 -0700979 const av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980
981 // Only support this function in serial decode.
982 if (ctx->frame_parallel_decode) {
983 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700984 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700985 }
986
Urvang Joshi77853e52016-07-15 12:47:01 -0700987 if (frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700988 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700989 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700990 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
991 image2yuvconfig(&frame->img, &sd);
Thomas Daede497d1952017-08-08 17:33:06 -0700992 return av1_copy_reference_dec(frame_worker_data->pbi, frame->idx, &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700993 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700994 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995 }
996}
997
Yaowu Xuf883b422016-08-30 14:01:10 -0700998static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700999 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001000 av1_ref_frame_t *data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001001
1002 // Only support this function in serial decode.
1003 if (ctx->frame_parallel_decode) {
1004 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001005 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001006 }
1007
1008 if (data) {
1009 YV12_BUFFER_CONFIG *fb;
Yaowu Xuf883b422016-08-30 14:01:10 -07001010 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001011 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1012 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07001013 if (fb == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001014 yuvconfig2image(&data->img, fb, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07001015 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001017 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001018 }
1019}
1020
Yaowu Xuf883b422016-08-30 14:01:10 -07001021static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001022 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001023 aom_image_t *new_img = va_arg(args, aom_image_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001024
1025 // Only support this function in serial decode.
1026 if (ctx->frame_parallel_decode) {
1027 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001028 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001029 }
1030
1031 if (new_img) {
1032 YV12_BUFFER_CONFIG new_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07001033 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001034 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1035
Yaowu Xuf883b422016-08-30 14:01:10 -07001036 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001037 yuvconfig2image(new_img, &new_frame, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -07001038 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001039 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001040 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001041 }
1042 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001043 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001044 }
1045}
1046
Yaowu Xuf883b422016-08-30 14:01:10 -07001047static aom_codec_err_t ctrl_set_postproc(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001048 va_list args) {
1049 (void)ctx;
1050 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -07001051 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001052}
1053
Yaowu Xuf883b422016-08-30 14:01:10 -07001054static aom_codec_err_t ctrl_set_dbg_options(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001055 va_list args) {
1056 (void)ctx;
1057 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -07001058 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001059}
1060
Yaowu Xuf883b422016-08-30 14:01:10 -07001061static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001062 va_list args) {
1063 int *const update_info = va_arg(args, int *);
1064
1065 // Only support this function in serial decode.
1066 if (ctx->frame_parallel_decode) {
1067 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001068 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001069 }
1070
1071 if (update_info) {
1072 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001073 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001074 FrameWorkerData *const frame_worker_data =
1075 (FrameWorkerData *)worker->data1;
1076 *update_info = frame_worker_data->pbi->refresh_frame_flags;
Yaowu Xuf883b422016-08-30 14:01:10 -07001077 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001078 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001079 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001080 }
1081 }
1082
Yaowu Xuf883b422016-08-30 14:01:10 -07001083 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001084}
1085
Peter Boströma1f64322017-01-19 11:35:30 -05001086static aom_codec_err_t ctrl_get_last_quantizer(aom_codec_alg_priv_t *ctx,
1087 va_list args) {
1088 int *const arg = va_arg(args, int *);
1089 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1090 *arg =
1091 ((FrameWorkerData *)ctx->frame_workers[0].data1)->pbi->common.base_qindex;
1092 return AOM_CODEC_OK;
1093}
1094
Yaowu Xuf883b422016-08-30 14:01:10 -07001095static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001096 va_list args) {
1097 int *corrupted = va_arg(args, int *);
1098
1099 if (corrupted) {
1100 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001101 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001102 FrameWorkerData *const frame_worker_data =
1103 (FrameWorkerData *)worker->data1;
1104 RefCntBuffer *const frame_bufs =
1105 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
1106 if (frame_worker_data->pbi->common.frame_to_show == NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -07001107 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001108 if (ctx->last_show_frame >= 0)
1109 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
Yaowu Xuf883b422016-08-30 14:01:10 -07001110 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001112 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001113 }
1114 }
1115
Yaowu Xuf883b422016-08-30 14:01:10 -07001116 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001117}
1118
Yaowu Xuf883b422016-08-30 14:01:10 -07001119static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001120 va_list args) {
1121 int *const frame_size = va_arg(args, int *);
1122
1123 // Only support this function in serial decode.
1124 if (ctx->frame_parallel_decode) {
1125 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001126 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001127 }
1128
1129 if (frame_size) {
1130 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001131 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001132 FrameWorkerData *const frame_worker_data =
1133 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001134 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001135 frame_size[0] = cm->width;
1136 frame_size[1] = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001137 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001138 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001139 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001140 }
1141 }
1142
Yaowu Xuf883b422016-08-30 14:01:10 -07001143 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001144}
1145
Yaowu Xuf883b422016-08-30 14:01:10 -07001146static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001147 va_list args) {
1148 int *const render_size = va_arg(args, int *);
1149
1150 // Only support this function in serial decode.
1151 if (ctx->frame_parallel_decode) {
1152 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001153 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001154 }
1155
1156 if (render_size) {
1157 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001158 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001159 FrameWorkerData *const frame_worker_data =
1160 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001161 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001162 render_size[0] = cm->render_width;
1163 render_size[1] = cm->render_height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001164 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001165 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001166 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001167 }
1168 }
1169
Yaowu Xuf883b422016-08-30 14:01:10 -07001170 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001171}
1172
Yaowu Xuf883b422016-08-30 14:01:10 -07001173static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001174 va_list args) {
1175 unsigned int *const bit_depth = va_arg(args, unsigned int *);
Yaowu Xuf883b422016-08-30 14:01:10 -07001176 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001177
1178 if (bit_depth) {
1179 if (worker) {
1180 FrameWorkerData *const frame_worker_data =
1181 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001182 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001183 *bit_depth = cm->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -07001184 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001185 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001186 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001187 }
1188 }
1189
Yaowu Xuf883b422016-08-30 14:01:10 -07001190 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001191}
1192
Yaowu Xuf883b422016-08-30 14:01:10 -07001193static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001194 va_list args) {
1195 ctx->invert_tile_order = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001196 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197}
1198
Yaowu Xuf883b422016-08-30 14:01:10 -07001199static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001200 va_list args) {
1201 const int legacy_byte_alignment = 0;
1202 const int min_byte_alignment = 32;
1203 const int max_byte_alignment = 1024;
1204 const int byte_alignment = va_arg(args, int);
1205
1206 if (byte_alignment != legacy_byte_alignment &&
1207 (byte_alignment < min_byte_alignment ||
1208 byte_alignment > max_byte_alignment ||
1209 (byte_alignment & (byte_alignment - 1)) != 0))
Yaowu Xuf883b422016-08-30 14:01:10 -07001210 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001211
1212 ctx->byte_alignment = byte_alignment;
1213 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001214 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001215 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1216 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1217 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001218 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001219}
1220
Yaowu Xuf883b422016-08-30 14:01:10 -07001221static aom_codec_err_t ctrl_set_skip_loop_filter(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001222 va_list args) {
1223 ctx->skip_loop_filter = va_arg(args, int);
1224
1225 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001226 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001227 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1228 frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
1229 }
1230
Yaowu Xuf883b422016-08-30 14:01:10 -07001231 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001232}
1233
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001234static aom_codec_err_t ctrl_get_accounting(aom_codec_alg_priv_t *ctx,
1235 va_list args) {
1236#if !CONFIG_ACCOUNTING
1237 (void)ctx;
1238 (void)args;
1239 return AOM_CODEC_INCAPABLE;
1240#else
1241 if (ctx->frame_workers) {
1242 AVxWorker *const worker = ctx->frame_workers;
1243 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1244 AV1Decoder *pbi = frame_worker_data->pbi;
1245 Accounting **acct = va_arg(args, Accounting **);
1246 *acct = &pbi->accounting;
1247 return AOM_CODEC_OK;
1248 }
1249 return AOM_CODEC_ERROR;
1250#endif
1251}
Yaowu Xuf883b422016-08-30 14:01:10 -07001252static aom_codec_err_t ctrl_set_decode_tile_row(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001253 va_list args) {
1254 ctx->decode_tile_row = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001255 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001256}
1257
Yaowu Xuf883b422016-08-30 14:01:10 -07001258static aom_codec_err_t ctrl_set_decode_tile_col(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259 va_list args) {
1260 ctx->decode_tile_col = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001261 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001262}
1263
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001264static aom_codec_err_t ctrl_set_tile_mode(aom_codec_alg_priv_t *ctx,
1265 va_list args) {
1266 ctx->tile_mode = va_arg(args, unsigned int);
1267 return AOM_CODEC_OK;
1268}
1269
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001270static aom_codec_err_t ctrl_set_inspection_callback(aom_codec_alg_priv_t *ctx,
1271 va_list args) {
1272#if !CONFIG_INSPECTION
1273 (void)ctx;
1274 (void)args;
1275 return AOM_CODEC_INCAPABLE;
1276#else
1277 aom_inspect_init *init = va_arg(args, aom_inspect_init *);
1278 ctx->inspect_cb = init->inspect_cb;
1279 ctx->inspect_ctx = init->inspect_ctx;
1280 return AOM_CODEC_OK;
1281#endif
1282}
1283
Yaowu Xuf883b422016-08-30 14:01:10 -07001284static aom_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
Thomas Daede497d1952017-08-08 17:33:06 -07001285 { AV1_COPY_REFERENCE, ctrl_copy_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001286
1287 // Setters
Thomas Daede497d1952017-08-08 17:33:06 -07001288 { AV1_SET_REFERENCE, ctrl_set_reference },
Yaowu Xuf883b422016-08-30 14:01:10 -07001289 { AOM_SET_POSTPROC, ctrl_set_postproc },
1290 { AOM_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options },
1291 { AOM_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options },
1292 { AOM_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options },
1293 { AOM_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options },
1294 { AV1_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
Yaowu Xuf883b422016-08-30 14:01:10 -07001295 { AV1_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
1296 { AV1_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
1297 { AV1_SET_DECODE_TILE_ROW, ctrl_set_decode_tile_row },
1298 { AV1_SET_DECODE_TILE_COL, ctrl_set_decode_tile_col },
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001299 { AV1_SET_TILE_MODE, ctrl_set_tile_mode },
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001300 { AV1_SET_INSPECTION_CALLBACK, ctrl_set_inspection_callback },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001301
1302 // Getters
Yaowu Xuf883b422016-08-30 14:01:10 -07001303 { AOMD_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
Peter Boströma1f64322017-01-19 11:35:30 -05001304 { AOMD_GET_LAST_QUANTIZER, ctrl_get_last_quantizer },
1305 { AOMD_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
Yaowu Xuf883b422016-08-30 14:01:10 -07001306 { AV1D_GET_BIT_DEPTH, ctrl_get_bit_depth },
Peter Boströma1f64322017-01-19 11:35:30 -05001307 { AV1D_GET_DISPLAY_SIZE, ctrl_get_render_size },
Yaowu Xuf883b422016-08-30 14:01:10 -07001308 { AV1D_GET_FRAME_SIZE, ctrl_get_frame_size },
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001309 { AV1_GET_ACCOUNTING, ctrl_get_accounting },
Yaowu Xuf883b422016-08-30 14:01:10 -07001310 { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
Peter Boströma1f64322017-01-19 11:35:30 -05001311 { AV1_GET_REFERENCE, ctrl_get_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312
1313 { -1, NULL },
1314};
1315
1316#ifndef VERSION_STRING
1317#define VERSION_STRING
1318#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001319CODEC_INTERFACE(aom_codec_av1_dx) = {
1320 "AOMedia Project AV1 Decoder" VERSION_STRING,
1321 AOM_CODEC_INTERNAL_ABI_VERSION,
1322 AOM_CODEC_CAP_DECODER |
1323 AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // aom_codec_caps_t
1324 decoder_init, // aom_codec_init_fn_t
1325 decoder_destroy, // aom_codec_destroy_fn_t
1326 decoder_ctrl_maps, // aom_codec_ctrl_fn_map_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001327 {
1328 // NOLINT
Yaowu Xuf883b422016-08-30 14:01:10 -07001329 decoder_peek_si, // aom_codec_peek_si_fn_t
1330 decoder_get_si, // aom_codec_get_si_fn_t
1331 decoder_decode, // aom_codec_decode_fn_t
1332 decoder_get_frame, // aom_codec_frame_get_fn_t
1333 decoder_set_fb_fn, // aom_codec_set_fb_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001334 },
1335 {
1336 // NOLINT
1337 0,
Yaowu Xuf883b422016-08-30 14:01:10 -07001338 NULL, // aom_codec_enc_cfg_map_t
1339 NULL, // aom_codec_encode_fn_t
1340 NULL, // aom_codec_get_cx_data_fn_t
1341 NULL, // aom_codec_enc_config_set_fn_t
1342 NULL, // aom_codec_get_global_headers_fn_t
1343 NULL, // aom_codec_get_preview_frame_fn_t
1344 NULL // aom_codec_enc_mr_get_mem_loc_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001345 }
1346};