blob: a63be5fba8b9ea232945e5d6ee3ea6b5839a569b [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;
49 aom_decrypt_cb decrypt_cb;
Yaowu Xuc27fc142016-08-22 16:08:15 -070050 void *decrypt_state;
Yaowu Xuf883b422016-08-30 14:01:10 -070051 aom_image_t img;
Yaowu Xuc27fc142016-08-22 16:08:15 -070052 int img_avail;
53 int flushed;
54 int invert_tile_order;
55 int last_show_frame; // Index of last output frame.
56 int byte_alignment;
57 int skip_loop_filter;
58 int decode_tile_row;
59 int decode_tile_col;
60
61 // Frame parallel related.
62 int frame_parallel_decode; // frame-based threading.
Yaowu Xuf883b422016-08-30 14:01:10 -070063 AVxWorker *frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -070064 int num_frame_workers;
65 int next_submit_worker_id;
66 int last_submit_worker_id;
67 int next_output_worker_id;
68 int available_threads;
69 cache_frame frame_cache[FRAME_CACHE_SIZE];
70 int frame_cache_write;
71 int frame_cache_read;
72 int num_cache_frames;
73 int need_resync; // wait for key/intra-only frame
74 // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
75 BufferPool *buffer_pool;
76
Yaowu Xuf883b422016-08-30 14:01:10 -070077 // External frame buffer info to save for AV1 common.
Yaowu Xuc27fc142016-08-22 16:08:15 -070078 void *ext_priv; // Private data associated with the external frame buffers.
Yaowu Xuf883b422016-08-30 14:01:10 -070079 aom_get_frame_buffer_cb_fn_t get_ext_fb_cb;
80 aom_release_frame_buffer_cb_fn_t release_ext_fb_cb;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050081
82#if CONFIG_INSPECTION
83 aom_inspect_cb inspect_cb;
84 void *inspect_ctx;
85#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070086};
87
Yaowu Xuf883b422016-08-30 14:01:10 -070088static aom_codec_err_t decoder_init(aom_codec_ctx_t *ctx,
89 aom_codec_priv_enc_mr_cfg_t *data) {
90 // This function only allocates space for the aom_codec_alg_priv_t
Yaowu Xuc27fc142016-08-22 16:08:15 -070091 // structure. More memory may be required at the time the stream
92 // information becomes known.
93 (void)data;
94
95 if (!ctx->priv) {
Yaowu Xuf883b422016-08-30 14:01:10 -070096 aom_codec_alg_priv_t *const priv =
97 (aom_codec_alg_priv_t *)aom_calloc(1, sizeof(*priv));
98 if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -070099
Yaowu Xuf883b422016-08-30 14:01:10 -0700100 ctx->priv = (aom_codec_priv_t *)priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101 ctx->priv->init_flags = ctx->init_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102 priv->flushed = 0;
103 // Only do frame parallel decode when threads > 1.
104 priv->frame_parallel_decode =
105 (ctx->config.dec && (ctx->config.dec->threads > 1) &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700106 (ctx->init_flags & AOM_CODEC_USE_FRAME_THREADING))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700107 ? 1
108 : 0;
Thomas Daede85b49002017-07-10 17:43:06 -0700109 // TODO(tdaede): this should not be exposed to the API
110 priv->cfg.allow_lowbitdepth = CONFIG_LOWBITDEPTH;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700111 if (ctx->config.dec) {
112 priv->cfg = *ctx->config.dec;
113 ctx->config.dec = &priv->cfg;
114 }
115 }
116
Yaowu Xuf883b422016-08-30 14:01:10 -0700117 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700118}
119
Yaowu Xuf883b422016-08-30 14:01:10 -0700120static aom_codec_err_t decoder_destroy(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121 if (ctx->frame_workers != NULL) {
122 int i;
123 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700124 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 FrameWorkerData *const frame_worker_data =
126 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700127 aom_get_worker_interface()->end(worker);
128 av1_remove_common(&frame_worker_data->pbi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700130 av1_free_restoration_buffers(&frame_worker_data->pbi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700131#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700132 av1_decoder_remove(frame_worker_data->pbi);
133 aom_free(frame_worker_data->scratch_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134#if CONFIG_MULTITHREAD
135 pthread_mutex_destroy(&frame_worker_data->stats_mutex);
136 pthread_cond_destroy(&frame_worker_data->stats_cond);
137#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700138 aom_free(frame_worker_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700139 }
140#if CONFIG_MULTITHREAD
141 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
142#endif
143 }
144
145 if (ctx->buffer_pool) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700146 av1_free_ref_frame_buffers(ctx->buffer_pool);
147 av1_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700148 }
149
Yaowu Xuf883b422016-08-30 14:01:10 -0700150 aom_free(ctx->frame_workers);
151 aom_free(ctx->buffer_pool);
152 aom_free(ctx);
153 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700154}
155
156static int parse_bitdepth_colorspace_sampling(BITSTREAM_PROFILE profile,
Yaowu Xuf883b422016-08-30 14:01:10 -0700157 struct aom_read_bit_buffer *rb) {
158 aom_color_space_t color_space;
anorkin76fb1262017-03-22 15:12:12 -0700159#if CONFIG_COLORSPACE_HEADERS
160 int subsampling_x = 0;
161 int subsampling_y = 0;
162#endif
163
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164 if (profile >= PROFILE_2) rb->bit_offset += 1; // Bit-depth 10 or 12.
anorkin76fb1262017-03-22 15:12:12 -0700165#if CONFIG_COLORSPACE_HEADERS
166 color_space = (aom_color_space_t)aom_rb_read_literal(rb, 5);
167 rb->bit_offset += 5; // Transfer function
168#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700169 color_space = (aom_color_space_t)aom_rb_read_literal(rb, 3);
anorkin76fb1262017-03-22 15:12:12 -0700170#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700171 if (color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172 rb->bit_offset += 1; // [16,235] (including xvycc) vs [0,255] range.
anorkin76fb1262017-03-22 15:12:12 -0700173
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174 if (profile == PROFILE_1 || profile == PROFILE_3) {
anorkin76fb1262017-03-22 15:12:12 -0700175#if CONFIG_COLORSPACE_HEADERS
176 subsampling_x = aom_rb_read_bit(rb);
177 subsampling_y = aom_rb_read_bit(rb);
178#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179 rb->bit_offset += 2; // subsampling x/y.
anorkin76fb1262017-03-22 15:12:12 -0700180#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181 rb->bit_offset += 1; // unused.
anorkin76fb1262017-03-22 15:12:12 -0700182#if CONFIG_COLORSPACE_HEADERS
183 } else {
184 subsampling_x = 1;
185 subsampling_y = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 }
anorkin76fb1262017-03-22 15:12:12 -0700187 if (subsampling_x == 1 && subsampling_y == 1) {
188 rb->bit_offset += 2;
189 }
190#else
191 }
192#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 } else {
194 if (profile == PROFILE_1 || profile == PROFILE_3) {
195 rb->bit_offset += 1; // unused
196 } else {
197 // RGB is only available in version 1.
198 return 0;
199 }
200 }
201 return 1;
202}
203
Yaowu Xuf883b422016-08-30 14:01:10 -0700204static aom_codec_err_t decoder_peek_si_internal(
205 const uint8_t *data, unsigned int data_sz, aom_codec_stream_info_t *si,
206 int *is_intra_only, aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700207 int intra_only_flag = 0;
208 uint8_t clear_buffer[9];
209
Yaowu Xuf883b422016-08-30 14:01:10 -0700210 if (data + data_sz <= data) return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211
212 si->is_kf = 0;
213 si->w = si->h = 0;
214
215 if (decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700216 data_sz = AOMMIN(sizeof(clear_buffer), data_sz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217 decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
218 data = clear_buffer;
219 }
220
Sebastien Alaiwan6bec6ec2017-08-07 09:37:52 +0200221 // skip a potential superframe index
222 {
223 uint32_t frame_sizes[8];
224 int frame_count;
225 int index_size = 0;
226 aom_codec_err_t res = av1_parse_superframe_index(
227 data, data_sz, frame_sizes, &frame_count, &index_size, NULL, NULL);
228 if (res != AOM_CODEC_OK) return res;
229
230 data += index_size;
231 data_sz -= index_size;
232 }
233
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234 {
235 int show_frame;
236 int error_resilient;
Yaowu Xuf883b422016-08-30 14:01:10 -0700237 struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
238 const int frame_marker = aom_rb_read_literal(&rb, 2);
239 const BITSTREAM_PROFILE profile = av1_read_profile(&rb);
Yunqing Wangc2502b52017-07-19 17:44:18 -0700240#if CONFIG_EXT_TILE
241 unsigned int large_scale_tile;
242#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243
Yaowu Xuf883b422016-08-30 14:01:10 -0700244 if (frame_marker != AOM_FRAME_MARKER) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245
Yaowu Xuf883b422016-08-30 14:01:10 -0700246 if (profile >= MAX_PROFILES) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700247
248 if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
Yaowu Xuf883b422016-08-30 14:01:10 -0700249 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250
Yunqing Wangc2502b52017-07-19 17:44:18 -0700251#if CONFIG_EXT_TILE
252 large_scale_tile = aom_rb_read_literal(&rb, 1);
253#endif // CONFIG_EXT_TILE
254
Yaowu Xuf883b422016-08-30 14:01:10 -0700255 if (aom_rb_read_bit(&rb)) { // show an existing frame
256 aom_rb_read_literal(&rb, 3); // Frame buffer to show.
257 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 }
259
Yaowu Xuf883b422016-08-30 14:01:10 -0700260 if (data_sz <= 8) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262 si->is_kf = !aom_rb_read_bit(&rb);
263 show_frame = aom_rb_read_bit(&rb);
264 error_resilient = aom_rb_read_bit(&rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100265#if CONFIG_REFERENCE_BUFFER
266 {
267 /* TODO: Move outside frame loop or inside key-frame branch */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +0100268 int frame_id_len;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100269 SequenceHeader seq_params;
270 read_sequence_header(&seq_params);
Yunqing Wangc2502b52017-07-19 17:44:18 -0700271#if CONFIG_EXT_TILE
272 if (large_scale_tile) seq_params.frame_id_numbers_present_flag = 0;
273#endif // CONFIG_EXT_TILE
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100274 if (seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +0100275 frame_id_len = seq_params.frame_id_length_minus7 + 7;
276 aom_rb_read_literal(&rb, frame_id_len);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100277 }
278 }
279#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280 if (si->is_kf) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700281 if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700282
283 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700284 return AOM_CODEC_UNSUP_BITSTREAM;
285 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700287 intra_only_flag = show_frame ? 0 : aom_rb_read_bit(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288
289 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
290
291 if (intra_only_flag) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700292 if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700293 if (profile > PROFILE_0) {
294 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700295 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700296 }
297 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
Yaowu Xuf883b422016-08-30 14:01:10 -0700298 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700299 }
300 }
301 }
302 if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
Yaowu Xuf883b422016-08-30 14:01:10 -0700303 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304}
305
Yaowu Xuf883b422016-08-30 14:01:10 -0700306static aom_codec_err_t decoder_peek_si(const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307 unsigned int data_sz,
Yaowu Xuf883b422016-08-30 14:01:10 -0700308 aom_codec_stream_info_t *si) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309 return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
310}
311
Yaowu Xuf883b422016-08-30 14:01:10 -0700312static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
313 aom_codec_stream_info_t *si) {
Ralph Gilesafe71d92017-05-03 13:18:57 -0700314 memcpy(si, &ctx->si, sizeof(*si));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315
Yaowu Xuf883b422016-08-30 14:01:10 -0700316 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700317}
318
Yaowu Xuf883b422016-08-30 14:01:10 -0700319static void set_error_detail(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320 const char *const error) {
321 ctx->base.err_detail = error;
322}
323
Yaowu Xuf883b422016-08-30 14:01:10 -0700324static aom_codec_err_t update_error_state(
325 aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326 if (error->error_code)
327 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
328
329 return error->error_code;
330}
331
Yaowu Xuf883b422016-08-30 14:01:10 -0700332static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333 int i;
334
335 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700336 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700337 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700338 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339 BufferPool *const pool = cm->buffer_pool;
340
341 cm->new_fb_idx = INVALID_IDX;
342 cm->byte_alignment = ctx->byte_alignment;
343 cm->skip_loop_filter = ctx->skip_loop_filter;
344
345 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
346 pool->get_fb_cb = ctx->get_ext_fb_cb;
347 pool->release_fb_cb = ctx->release_ext_fb_cb;
348 pool->cb_priv = ctx->ext_priv;
349 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700350 pool->get_fb_cb = av1_get_frame_buffer;
351 pool->release_fb_cb = av1_release_frame_buffer;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700352
Yaowu Xuf883b422016-08-30 14:01:10 -0700353 if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
354 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700355 "Failed to initialize internal frame buffers");
356
357 pool->cb_priv = &pool->int_frame_buffers;
358 }
359 }
360}
361
Yaowu Xuf883b422016-08-30 14:01:10 -0700362static void set_default_ppflags(aom_postproc_cfg_t *cfg) {
363 cfg->post_proc_flag = AOM_DEBLOCK | AOM_DEMACROBLOCK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364 cfg->deblocking_level = 4;
365 cfg->noise_level = 0;
366}
367
368static int frame_worker_hook(void *arg1, void *arg2) {
369 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
370 const uint8_t *data = frame_worker_data->data;
371 (void)arg2;
372
Yaowu Xuf883b422016-08-30 14:01:10 -0700373 frame_worker_data->result = av1_receive_compressed_data(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700374 frame_worker_data->pbi, frame_worker_data->data_size, &data);
375 frame_worker_data->data_end = data;
376
377 if (frame_worker_data->pbi->common.frame_parallel_decode) {
378 // In frame parallel decoding, a worker thread must successfully decode all
379 // the compressed data.
380 if (frame_worker_data->result != 0 ||
381 frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700382 AVxWorker *const worker = frame_worker_data->pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383 BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
384 // Signal all the other threads that are waiting for this frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700385 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700386 frame_worker_data->frame_context_ready = 1;
387 lock_buffer_pool(pool);
388 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
389 unlock_buffer_pool(pool);
390 frame_worker_data->pbi->need_resync = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700391 av1_frameworker_signal_stats(worker);
392 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393 return 0;
394 }
395 } else if (frame_worker_data->result != 0) {
396 // Check decode result in serial decode.
397 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
398 frame_worker_data->pbi->need_resync = 1;
399 }
400 return !frame_worker_data->result;
401}
402
Yaowu Xuf883b422016-08-30 14:01:10 -0700403static aom_codec_err_t init_decoder(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700405 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700406
407 ctx->last_show_frame = -1;
408 ctx->next_submit_worker_id = 0;
409 ctx->last_submit_worker_id = 0;
410 ctx->next_output_worker_id = 0;
411 ctx->frame_cache_read = 0;
412 ctx->frame_cache_write = 0;
413 ctx->num_cache_frames = 0;
414 ctx->need_resync = 1;
415 ctx->num_frame_workers =
416 (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads : 1;
417 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
418 ctx->num_frame_workers = MAX_DECODE_THREADS;
419 ctx->available_threads = ctx->num_frame_workers;
420 ctx->flushed = 0;
421
Yaowu Xuf883b422016-08-30 14:01:10 -0700422 ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
423 if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700424
425#if CONFIG_MULTITHREAD
426 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
427 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700428 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700429 }
430#endif
431
Yaowu Xuf883b422016-08-30 14:01:10 -0700432 ctx->frame_workers = (AVxWorker *)aom_malloc(ctx->num_frame_workers *
Yaowu Xuc27fc142016-08-22 16:08:15 -0700433 sizeof(*ctx->frame_workers));
434 if (ctx->frame_workers == NULL) {
435 set_error_detail(ctx, "Failed to allocate frame_workers");
Yaowu Xuf883b422016-08-30 14:01:10 -0700436 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437 }
438
439 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700440 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700441 FrameWorkerData *frame_worker_data = NULL;
442 winterface->init(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -0700443 worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700444 if (worker->data1 == NULL) {
445 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700446 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700447 }
448 frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700449 frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700450 if (frame_worker_data->pbi == NULL) {
451 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700452 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453 }
454 frame_worker_data->pbi->frame_worker_owner = worker;
455 frame_worker_data->worker_id = i;
456 frame_worker_data->scratch_buffer = NULL;
457 frame_worker_data->scratch_buffer_size = 0;
458 frame_worker_data->frame_context_ready = 0;
459 frame_worker_data->received_frame = 0;
460#if CONFIG_MULTITHREAD
461 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
462 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700463 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464 }
465
466 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
467 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
Yaowu Xuf883b422016-08-30 14:01:10 -0700468 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469 }
470#endif
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +0200471 frame_worker_data->pbi->allow_lowbitdepth = ctx->cfg.allow_lowbitdepth;
472
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473 // If decoding in serial mode, FrameWorker thread could create tile worker
474 // thread or loopfilter thread.
475 frame_worker_data->pbi->max_threads =
476 (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
477
478 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
479 frame_worker_data->pbi->common.frame_parallel_decode =
480 ctx->frame_parallel_decode;
Yaowu Xuf883b422016-08-30 14:01:10 -0700481 worker->hook = (AVxWorkerHook)frame_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700482 if (!winterface->reset(worker)) {
483 set_error_detail(ctx, "Frame Worker thread creation failed");
Yaowu Xuf883b422016-08-30 14:01:10 -0700484 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700485 }
486 }
487
488 // If postprocessing was enabled by the application and a
489 // configuration has not been provided, default it.
Yaowu Xuf883b422016-08-30 14:01:10 -0700490 if (!ctx->postproc_cfg_set && (ctx->base.init_flags & AOM_CODEC_USE_POSTPROC))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700491 set_default_ppflags(&ctx->postproc_cfg);
492
493 init_buffer_callbacks(ctx);
494
Yaowu Xuf883b422016-08-30 14:01:10 -0700495 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700496}
497
Yaowu Xuf883b422016-08-30 14:01:10 -0700498static INLINE void check_resync(aom_codec_alg_priv_t *const ctx,
499 const AV1Decoder *const pbi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700500 // Clear resync flag if worker got a key frame or intra only frame.
501 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
502 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
503 ctx->need_resync = 0;
504}
505
Yaowu Xuf883b422016-08-30 14:01:10 -0700506static aom_codec_err_t decode_one(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507 const uint8_t **data, unsigned int data_sz,
508 void *user_priv, int64_t deadline) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700509 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510 (void)deadline;
511
512 // Determine the stream parameters. Note that we rely on peek_si to
513 // validate that we have a buffer that does not wrap around the top
514 // of the heap.
515 if (!ctx->si.h) {
516 int is_intra_only = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700517 const aom_codec_err_t res =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
519 ctx->decrypt_cb, ctx->decrypt_state);
Yaowu Xuf883b422016-08-30 14:01:10 -0700520 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521
Yaowu Xuf883b422016-08-30 14:01:10 -0700522 if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700523 }
524
525 if (!ctx->frame_parallel_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700526 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700527 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
528 frame_worker_data->data = *data;
529 frame_worker_data->data_size = data_sz;
530 frame_worker_data->user_priv = user_priv;
531 frame_worker_data->received_frame = 1;
532
533 // Set these even if already initialized. The caller may have changed the
534 // decrypt config between frames.
535 frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
536 frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -0500537#if CONFIG_INSPECTION
538 frame_worker_data->pbi->inspect_cb = ctx->inspect_cb;
539 frame_worker_data->pbi->inspect_ctx = ctx->inspect_ctx;
540#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541
542#if CONFIG_EXT_TILE
543 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
544 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
545#endif // CONFIG_EXT_TILE
546
547 worker->had_error = 0;
548 winterface->execute(worker);
549
550 // Update data pointer after decode.
551 *data = frame_worker_data->data_end;
552
553 if (worker->had_error)
554 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
555
556 check_resync(ctx, frame_worker_data->pbi);
557 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700558 AVxWorker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700559 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
560 // Copy context from last worker thread to next worker thread.
561 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
Yaowu Xuf883b422016-08-30 14:01:10 -0700562 av1_frameworker_copy_context(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563 &ctx->frame_workers[ctx->next_submit_worker_id],
564 &ctx->frame_workers[ctx->last_submit_worker_id]);
565
566 frame_worker_data->pbi->ready_for_new_data = 0;
567 // Copy the compressed data into worker's internal buffer.
568 // TODO(hkuang): Will all the workers allocate the same size
569 // as the size of the first intra frame be better? This will
570 // avoid too many deallocate and allocate.
571 if (frame_worker_data->scratch_buffer_size < data_sz) {
Alex Converse7f094f12017-02-23 17:29:40 -0800572 aom_free(frame_worker_data->scratch_buffer);
573 frame_worker_data->scratch_buffer = (uint8_t *)aom_malloc(data_sz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700574 if (frame_worker_data->scratch_buffer == NULL) {
575 set_error_detail(ctx, "Failed to reallocate scratch buffer");
Yaowu Xuf883b422016-08-30 14:01:10 -0700576 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700577 }
578 frame_worker_data->scratch_buffer_size = data_sz;
579 }
580 frame_worker_data->data_size = data_sz;
581 memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
582
583 frame_worker_data->frame_decoded = 0;
584 frame_worker_data->frame_context_ready = 0;
585 frame_worker_data->received_frame = 1;
586 frame_worker_data->data = frame_worker_data->scratch_buffer;
587 frame_worker_data->user_priv = user_priv;
588
589 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
590 ctx->last_submit_worker_id =
591 (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
592
593 ctx->next_submit_worker_id =
594 (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
595 --ctx->available_threads;
596 worker->had_error = 0;
597 winterface->launch(worker);
598 }
599
Yaowu Xuf883b422016-08-30 14:01:10 -0700600 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601}
602
Yaowu Xuf883b422016-08-30 14:01:10 -0700603static void wait_worker_and_cache_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700605 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
606 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
608 ctx->next_output_worker_id =
609 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
610 // TODO(hkuang): Add worker error handling here.
611 winterface->sync(worker);
612 frame_worker_data->received_frame = 0;
613 ++ctx->available_threads;
614
615 check_resync(ctx, frame_worker_data->pbi);
616
Yaowu Xuf883b422016-08-30 14:01:10 -0700617 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
618 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
620 ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
621 yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
622 frame_worker_data->user_priv);
623 ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
624 frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
625 ctx->frame_cache_write = (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
626 ++ctx->num_cache_frames;
627 }
628}
629
Yaowu Xuf883b422016-08-30 14:01:10 -0700630static aom_codec_err_t decoder_decode(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700631 const uint8_t *data, unsigned int data_sz,
632 void *user_priv, long deadline) {
633 const uint8_t *data_start = data;
634 const uint8_t *const data_end = data + data_sz;
Yaowu Xuf883b422016-08-30 14:01:10 -0700635 aom_codec_err_t res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636 uint32_t frame_sizes[8];
637 int frame_count;
638
639 if (data == NULL && data_sz == 0) {
640 ctx->flushed = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700641 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700642 }
643
644 // Reset flushed when receiving a valid frame.
645 ctx->flushed = 0;
646
647 // Initialize the decoder workers on the first frame.
648 if (ctx->frame_workers == NULL) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700649 res = init_decoder(ctx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 }
652
Sebastien Alaiwane4c6fc12017-06-21 16:43:22 +0200653 int index_size = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 res = av1_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
Sebastien Alaiwane4c6fc12017-06-21 16:43:22 +0200655 &index_size, ctx->decrypt_cb,
656 ctx->decrypt_state);
Yaowu Xuf883b422016-08-30 14:01:10 -0700657 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658
Sebastien Alaiwane4c6fc12017-06-21 16:43:22 +0200659 data_start += index_size;
660
Yaowu Xuc27fc142016-08-22 16:08:15 -0700661 if (ctx->frame_parallel_decode) {
662 // Decode in frame parallel mode. When decoding in this mode, the frame
663 // passed to the decoder must be either a normal frame or a superframe with
664 // superframe index so the decoder could get each frame's start position
665 // in the superframe.
666 if (frame_count > 0) {
667 int i;
668
669 for (i = 0; i < frame_count; ++i) {
670 const uint8_t *data_start_copy = data_start;
671 const uint32_t frame_size = frame_sizes[i];
672 if (data_start < data ||
673 frame_size > (uint32_t)(data_end - data_start)) {
674 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676 }
677
678 if (ctx->available_threads == 0) {
679 // No more threads for decoding. Wait until the next output worker
680 // finishes decoding. Then copy the decoded frame into cache.
681 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
682 wait_worker_and_cache_frame(ctx);
683 } else {
684 // TODO(hkuang): Add unit test to test this path.
685 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700686 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 }
688 }
689
690 res =
691 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693 data_start += frame_size;
694 }
695 } else {
696 if (ctx->available_threads == 0) {
697 // No more threads for decoding. Wait until the next output worker
698 // finishes decoding. Then copy the decoded frame into cache.
699 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
700 wait_worker_and_cache_frame(ctx);
701 } else {
702 // TODO(hkuang): Add unit test to test this path.
703 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700705 }
706 }
707
708 res = decode_one(ctx, &data, data_sz, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700709 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 }
711 } else {
712 // Decode in serial mode.
713 if (frame_count > 0) {
714 int i;
715
716 for (i = 0; i < frame_count; ++i) {
717 const uint8_t *data_start_copy = data_start;
718 const uint32_t frame_size = frame_sizes[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719 if (data_start < data ||
720 frame_size > (uint32_t)(data_end - data_start)) {
721 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700722 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 }
724
725 res =
726 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700727 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728
729 data_start += frame_size;
730 }
731 } else {
732 while (data_start < data_end) {
733 const uint32_t frame_size = (uint32_t)(data_end - data_start);
Urvang Joshi454280d2016-10-14 16:51:44 -0700734 res = decode_one(ctx, &data_start, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736
737 // Account for suboptimal termination by the encoder.
738 while (data_start < data_end) {
739 const uint8_t marker =
740 read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
741 if (marker) break;
742 ++data_start;
743 }
744 }
745 }
746 }
747
748 return res;
749}
750
Yaowu Xuf883b422016-08-30 14:01:10 -0700751static void release_last_output_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
753 // Decrease reference count of last output frame in frame parallel mode.
754 if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
755 BufferPool *const pool = ctx->buffer_pool;
756 lock_buffer_pool(pool);
757 decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
758 unlock_buffer_pool(pool);
759 }
760}
761
Yaowu Xuf883b422016-08-30 14:01:10 -0700762static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
763 aom_codec_iter_t *iter) {
764 aom_image_t *img = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765
766 // Only return frame when all the cpu are busy or
767 // application fluhsed the decoder in frame parallel decode.
768 if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
769 !ctx->flushed) {
770 return NULL;
771 }
772
773 // Output the frames in the cache first.
774 if (ctx->num_cache_frames > 0) {
775 release_last_output_frame(ctx);
776 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
777 if (ctx->need_resync) return NULL;
778 img = &ctx->frame_cache[ctx->frame_cache_read].img;
779 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
780 --ctx->num_cache_frames;
781 return img;
782 }
783
784 // iter acts as a flip flop, so an image is only returned on the first
785 // call to get_frame.
786 if (*iter == NULL && ctx->frame_workers != NULL) {
787 do {
788 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
790 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 FrameWorkerData *const frame_worker_data =
792 (FrameWorkerData *)worker->data1;
793 ctx->next_output_worker_id =
794 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
795 // Wait for the frame from worker thread.
796 if (winterface->sync(worker)) {
797 // Check if worker has received any frames.
798 if (frame_worker_data->received_frame == 1) {
799 ++ctx->available_threads;
800 frame_worker_data->received_frame = 0;
801 check_resync(ctx, frame_worker_data->pbi);
802 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700803 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
804 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
806 release_last_output_frame(ctx);
807 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
808 if (ctx->need_resync) return NULL;
809 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
810
811#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700812 if (cm->single_tile_decoding &&
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800813 frame_worker_data->pbi->dec_tile_row >= 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814 const int tile_row =
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 AOMMIN(frame_worker_data->pbi->dec_tile_row, cm->tile_rows - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816 const int mi_row = tile_row * cm->tile_height;
817 const int ssy = ctx->img.y_chroma_shift;
818 int plane;
819 ctx->img.planes[0] += mi_row * MI_SIZE * ctx->img.stride[0];
820 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
821 ctx->img.planes[plane] +=
822 mi_row * (MI_SIZE >> ssy) * ctx->img.stride[plane];
823 }
824 ctx->img.d_h =
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 AOMMIN(cm->tile_height, cm->mi_rows - mi_row) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 }
827
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700828 if (cm->single_tile_decoding &&
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800829 frame_worker_data->pbi->dec_tile_col >= 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700830 const int tile_col =
Yaowu Xuf883b422016-08-30 14:01:10 -0700831 AOMMIN(frame_worker_data->pbi->dec_tile_col, cm->tile_cols - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832 const int mi_col = tile_col * cm->tile_width;
833 const int ssx = ctx->img.x_chroma_shift;
834 int plane;
835 ctx->img.planes[0] += mi_col * MI_SIZE;
836 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
837 ctx->img.planes[plane] += mi_col * (MI_SIZE >> ssx);
838 }
839 ctx->img.d_w =
Yaowu Xuf883b422016-08-30 14:01:10 -0700840 AOMMIN(cm->tile_width, cm->mi_cols - mi_col) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700841 }
842#endif // CONFIG_EXT_TILE
843
844 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
845 img = &ctx->img;
846 return img;
847 }
848 } else {
849 // Decoding failed. Release the worker thread.
850 frame_worker_data->received_frame = 0;
851 ++ctx->available_threads;
852 ctx->need_resync = 1;
853 if (ctx->flushed != 1) return NULL;
854 }
855 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
856 }
857 return NULL;
858}
859
Yaowu Xuf883b422016-08-30 14:01:10 -0700860static aom_codec_err_t decoder_set_fb_fn(
861 aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
862 aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863 if (cb_get == NULL || cb_release == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700864 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865 } else if (ctx->frame_workers == NULL) {
866 // If the decoder has already been initialized, do not accept changes to
867 // the frame buffer functions.
868 ctx->get_ext_fb_cb = cb_get;
869 ctx->release_ext_fb_cb = cb_release;
870 ctx->ext_priv = cb_priv;
Yaowu Xuf883b422016-08-30 14:01:10 -0700871 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872 }
873
Yaowu Xuf883b422016-08-30 14:01:10 -0700874 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875}
876
Yaowu Xuf883b422016-08-30 14:01:10 -0700877static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878 va_list args) {
Thomas Daede497d1952017-08-08 17:33:06 -0700879 av1_ref_frame_t *const data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880
881 // Only support this function in serial decode.
882 if (ctx->frame_parallel_decode) {
883 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700884 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700885 }
886
887 if (data) {
Thomas Daede497d1952017-08-08 17:33:06 -0700888 av1_ref_frame_t *const frame = data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700890 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
892 image2yuvconfig(&frame->img, &sd);
Thomas Daede497d1952017-08-08 17:33:06 -0700893 return av1_set_reference_dec(&frame_worker_data->pbi->common, frame->idx,
Yaowu Xuf883b422016-08-30 14:01:10 -0700894 &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897 }
898}
899
Yaowu Xuf883b422016-08-30 14:01:10 -0700900static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700901 va_list args) {
Thomas Daede497d1952017-08-08 17:33:06 -0700902 const av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903
904 // Only support this function in serial decode.
905 if (ctx->frame_parallel_decode) {
906 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700907 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908 }
909
Urvang Joshi77853e52016-07-15 12:47:01 -0700910 if (frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700912 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700913 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
914 image2yuvconfig(&frame->img, &sd);
Thomas Daede497d1952017-08-08 17:33:06 -0700915 return av1_copy_reference_dec(frame_worker_data->pbi, frame->idx, &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700917 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700918 }
919}
920
Yaowu Xuf883b422016-08-30 14:01:10 -0700921static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700923 av1_ref_frame_t *data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924
925 // Only support this function in serial decode.
926 if (ctx->frame_parallel_decode) {
927 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700928 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929 }
930
931 if (data) {
932 YV12_BUFFER_CONFIG *fb;
Yaowu Xuf883b422016-08-30 14:01:10 -0700933 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
935 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700936 if (fb == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700937 yuvconfig2image(&data->img, fb, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700938 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700939 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700940 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941 }
942}
943
Yaowu Xuf883b422016-08-30 14:01:10 -0700944static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700946 aom_image_t *new_img = va_arg(args, aom_image_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700947
948 // Only support this function in serial decode.
949 if (ctx->frame_parallel_decode) {
950 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700951 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700952 }
953
954 if (new_img) {
955 YV12_BUFFER_CONFIG new_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -0700956 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
958
Yaowu Xuf883b422016-08-30 14:01:10 -0700959 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960 yuvconfig2image(new_img, &new_frame, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700961 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700963 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700964 }
965 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700966 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700967 }
968}
969
Yaowu Xuf883b422016-08-30 14:01:10 -0700970static aom_codec_err_t ctrl_set_postproc(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700971 va_list args) {
972 (void)ctx;
973 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700974 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700975}
976
Yaowu Xuf883b422016-08-30 14:01:10 -0700977static aom_codec_err_t ctrl_set_dbg_options(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700978 va_list args) {
979 (void)ctx;
980 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700981 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700982}
983
Yaowu Xuf883b422016-08-30 14:01:10 -0700984static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700985 va_list args) {
986 int *const update_info = va_arg(args, int *);
987
988 // Only support this function in serial decode.
989 if (ctx->frame_parallel_decode) {
990 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700991 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700992 }
993
994 if (update_info) {
995 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700996 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700997 FrameWorkerData *const frame_worker_data =
998 (FrameWorkerData *)worker->data1;
999 *update_info = frame_worker_data->pbi->refresh_frame_flags;
Yaowu Xuf883b422016-08-30 14:01:10 -07001000 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001001 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001002 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001003 }
1004 }
1005
Yaowu Xuf883b422016-08-30 14:01:10 -07001006 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001007}
1008
Peter Boströma1f64322017-01-19 11:35:30 -05001009static aom_codec_err_t ctrl_get_last_quantizer(aom_codec_alg_priv_t *ctx,
1010 va_list args) {
1011 int *const arg = va_arg(args, int *);
1012 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1013 *arg =
1014 ((FrameWorkerData *)ctx->frame_workers[0].data1)->pbi->common.base_qindex;
1015 return AOM_CODEC_OK;
1016}
1017
Yaowu Xuf883b422016-08-30 14:01:10 -07001018static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001019 va_list args) {
1020 int *corrupted = va_arg(args, int *);
1021
1022 if (corrupted) {
1023 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001024 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001025 FrameWorkerData *const frame_worker_data =
1026 (FrameWorkerData *)worker->data1;
1027 RefCntBuffer *const frame_bufs =
1028 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
1029 if (frame_worker_data->pbi->common.frame_to_show == NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -07001030 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001031 if (ctx->last_show_frame >= 0)
1032 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
Yaowu Xuf883b422016-08-30 14:01:10 -07001033 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001034 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001035 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001036 }
1037 }
1038
Yaowu Xuf883b422016-08-30 14:01:10 -07001039 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001040}
1041
Yaowu Xuf883b422016-08-30 14:01:10 -07001042static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001043 va_list args) {
1044 int *const frame_size = va_arg(args, int *);
1045
1046 // Only support this function in serial decode.
1047 if (ctx->frame_parallel_decode) {
1048 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001049 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001050 }
1051
1052 if (frame_size) {
1053 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001054 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001055 FrameWorkerData *const frame_worker_data =
1056 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001057 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001058 frame_size[0] = cm->width;
1059 frame_size[1] = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001060 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001061 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001062 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001063 }
1064 }
1065
Yaowu Xuf883b422016-08-30 14:01:10 -07001066 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001067}
1068
Yaowu Xuf883b422016-08-30 14:01:10 -07001069static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001070 va_list args) {
1071 int *const render_size = va_arg(args, int *);
1072
1073 // Only support this function in serial decode.
1074 if (ctx->frame_parallel_decode) {
1075 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001076 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001077 }
1078
1079 if (render_size) {
1080 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001081 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001082 FrameWorkerData *const frame_worker_data =
1083 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001084 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001085 render_size[0] = cm->render_width;
1086 render_size[1] = cm->render_height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001087 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001088 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001089 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001090 }
1091 }
1092
Yaowu Xuf883b422016-08-30 14:01:10 -07001093 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001094}
1095
Yaowu Xuf883b422016-08-30 14:01:10 -07001096static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001097 va_list args) {
1098 unsigned int *const bit_depth = va_arg(args, unsigned int *);
Yaowu Xuf883b422016-08-30 14:01:10 -07001099 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001100
1101 if (bit_depth) {
1102 if (worker) {
1103 FrameWorkerData *const frame_worker_data =
1104 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001105 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001106 *bit_depth = cm->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -07001107 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001108 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001109 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001110 }
1111 }
1112
Yaowu Xuf883b422016-08-30 14:01:10 -07001113 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001114}
1115
Yaowu Xuf883b422016-08-30 14:01:10 -07001116static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001117 va_list args) {
1118 ctx->invert_tile_order = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001119 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001120}
1121
Yaowu Xuf883b422016-08-30 14:01:10 -07001122static aom_codec_err_t ctrl_set_decryptor(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001123 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001124 aom_decrypt_init *init = va_arg(args, aom_decrypt_init *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001125 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
1126 ctx->decrypt_state = init ? init->decrypt_state : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07001127 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001128}
1129
Yaowu Xuf883b422016-08-30 14:01:10 -07001130static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001131 va_list args) {
1132 const int legacy_byte_alignment = 0;
1133 const int min_byte_alignment = 32;
1134 const int max_byte_alignment = 1024;
1135 const int byte_alignment = va_arg(args, int);
1136
1137 if (byte_alignment != legacy_byte_alignment &&
1138 (byte_alignment < min_byte_alignment ||
1139 byte_alignment > max_byte_alignment ||
1140 (byte_alignment & (byte_alignment - 1)) != 0))
Yaowu Xuf883b422016-08-30 14:01:10 -07001141 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001142
1143 ctx->byte_alignment = byte_alignment;
1144 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001145 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001146 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1147 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1148 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001149 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001150}
1151
Yaowu Xuf883b422016-08-30 14:01:10 -07001152static aom_codec_err_t ctrl_set_skip_loop_filter(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001153 va_list args) {
1154 ctx->skip_loop_filter = va_arg(args, int);
1155
1156 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001157 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001158 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1159 frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
1160 }
1161
Yaowu Xuf883b422016-08-30 14:01:10 -07001162 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001163}
1164
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001165static aom_codec_err_t ctrl_get_accounting(aom_codec_alg_priv_t *ctx,
1166 va_list args) {
1167#if !CONFIG_ACCOUNTING
1168 (void)ctx;
1169 (void)args;
1170 return AOM_CODEC_INCAPABLE;
1171#else
1172 if (ctx->frame_workers) {
1173 AVxWorker *const worker = ctx->frame_workers;
1174 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1175 AV1Decoder *pbi = frame_worker_data->pbi;
1176 Accounting **acct = va_arg(args, Accounting **);
1177 *acct = &pbi->accounting;
1178 return AOM_CODEC_OK;
1179 }
1180 return AOM_CODEC_ERROR;
1181#endif
1182}
Yaowu Xuf883b422016-08-30 14:01:10 -07001183static aom_codec_err_t ctrl_set_decode_tile_row(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001184 va_list args) {
1185 ctx->decode_tile_row = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001186 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001187}
1188
Yaowu Xuf883b422016-08-30 14:01:10 -07001189static aom_codec_err_t ctrl_set_decode_tile_col(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001190 va_list args) {
1191 ctx->decode_tile_col = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001192 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001193}
1194
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001195static aom_codec_err_t ctrl_set_inspection_callback(aom_codec_alg_priv_t *ctx,
1196 va_list args) {
1197#if !CONFIG_INSPECTION
1198 (void)ctx;
1199 (void)args;
1200 return AOM_CODEC_INCAPABLE;
1201#else
1202 aom_inspect_init *init = va_arg(args, aom_inspect_init *);
1203 ctx->inspect_cb = init->inspect_cb;
1204 ctx->inspect_ctx = init->inspect_ctx;
1205 return AOM_CODEC_OK;
1206#endif
1207}
1208
Yaowu Xuf883b422016-08-30 14:01:10 -07001209static aom_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
Thomas Daede497d1952017-08-08 17:33:06 -07001210 { AV1_COPY_REFERENCE, ctrl_copy_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001211
1212 // Setters
Thomas Daede497d1952017-08-08 17:33:06 -07001213 { AV1_SET_REFERENCE, ctrl_set_reference },
Yaowu Xuf883b422016-08-30 14:01:10 -07001214 { AOM_SET_POSTPROC, ctrl_set_postproc },
1215 { AOM_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options },
1216 { AOM_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options },
1217 { AOM_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options },
1218 { AOM_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options },
1219 { AV1_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
1220 { AOMD_SET_DECRYPTOR, ctrl_set_decryptor },
1221 { AV1_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
1222 { AV1_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
1223 { AV1_SET_DECODE_TILE_ROW, ctrl_set_decode_tile_row },
1224 { AV1_SET_DECODE_TILE_COL, ctrl_set_decode_tile_col },
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001225 { AV1_SET_INSPECTION_CALLBACK, ctrl_set_inspection_callback },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001226
1227 // Getters
Yaowu Xuf883b422016-08-30 14:01:10 -07001228 { AOMD_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
Peter Boströma1f64322017-01-19 11:35:30 -05001229 { AOMD_GET_LAST_QUANTIZER, ctrl_get_last_quantizer },
1230 { AOMD_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
Yaowu Xuf883b422016-08-30 14:01:10 -07001231 { AV1D_GET_BIT_DEPTH, ctrl_get_bit_depth },
Peter Boströma1f64322017-01-19 11:35:30 -05001232 { AV1D_GET_DISPLAY_SIZE, ctrl_get_render_size },
Yaowu Xuf883b422016-08-30 14:01:10 -07001233 { AV1D_GET_FRAME_SIZE, ctrl_get_frame_size },
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001234 { AV1_GET_ACCOUNTING, ctrl_get_accounting },
Yaowu Xuf883b422016-08-30 14:01:10 -07001235 { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
Peter Boströma1f64322017-01-19 11:35:30 -05001236 { AV1_GET_REFERENCE, ctrl_get_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001237
1238 { -1, NULL },
1239};
1240
1241#ifndef VERSION_STRING
1242#define VERSION_STRING
1243#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001244CODEC_INTERFACE(aom_codec_av1_dx) = {
1245 "AOMedia Project AV1 Decoder" VERSION_STRING,
1246 AOM_CODEC_INTERNAL_ABI_VERSION,
1247 AOM_CODEC_CAP_DECODER |
1248 AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // aom_codec_caps_t
1249 decoder_init, // aom_codec_init_fn_t
1250 decoder_destroy, // aom_codec_destroy_fn_t
1251 decoder_ctrl_maps, // aom_codec_ctrl_fn_map_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001252 {
1253 // NOLINT
Yaowu Xuf883b422016-08-30 14:01:10 -07001254 decoder_peek_si, // aom_codec_peek_si_fn_t
1255 decoder_get_si, // aom_codec_get_si_fn_t
1256 decoder_decode, // aom_codec_decode_fn_t
1257 decoder_get_frame, // aom_codec_frame_get_fn_t
1258 decoder_set_fb_fn, // aom_codec_set_fb_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259 },
1260 {
1261 // NOLINT
1262 0,
Yaowu Xuf883b422016-08-30 14:01:10 -07001263 NULL, // aom_codec_enc_cfg_map_t
1264 NULL, // aom_codec_encode_fn_t
1265 NULL, // aom_codec_get_cx_data_fn_t
1266 NULL, // aom_codec_enc_config_set_fn_t
1267 NULL, // aom_codec_get_global_headers_fn_t
1268 NULL, // aom_codec_get_preview_frame_fn_t
1269 NULL // aom_codec_enc_mr_get_mem_loc_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001270 }
1271};