blob: 4faab1cfe1db20f9348b98f4096baf92e7ec9865 [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
221 {
222 int show_frame;
223 int error_resilient;
Yaowu Xuf883b422016-08-30 14:01:10 -0700224 struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
225 const int frame_marker = aom_rb_read_literal(&rb, 2);
226 const BITSTREAM_PROFILE profile = av1_read_profile(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227
Yaowu Xuf883b422016-08-30 14:01:10 -0700228 if (frame_marker != AOM_FRAME_MARKER) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700229
Yaowu Xuf883b422016-08-30 14:01:10 -0700230 if (profile >= MAX_PROFILES) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231
232 if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
Yaowu Xuf883b422016-08-30 14:01:10 -0700233 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234
Yaowu Xuf883b422016-08-30 14:01:10 -0700235 if (aom_rb_read_bit(&rb)) { // show an existing frame
236 aom_rb_read_literal(&rb, 3); // Frame buffer to show.
237 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238 }
239
Yaowu Xuf883b422016-08-30 14:01:10 -0700240 if (data_sz <= 8) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700241
Yaowu Xuf883b422016-08-30 14:01:10 -0700242 si->is_kf = !aom_rb_read_bit(&rb);
243 show_frame = aom_rb_read_bit(&rb);
244 error_resilient = aom_rb_read_bit(&rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100245#if CONFIG_REFERENCE_BUFFER
246 {
247 /* TODO: Move outside frame loop or inside key-frame branch */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +0100248 int frame_id_len;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100249 SequenceHeader seq_params;
250 read_sequence_header(&seq_params);
251 if (seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +0100252 frame_id_len = seq_params.frame_id_length_minus7 + 7;
253 aom_rb_read_literal(&rb, frame_id_len);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100254 }
255 }
256#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257 if (si->is_kf) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700258 if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259
260 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700261 return AOM_CODEC_UNSUP_BITSTREAM;
262 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700264 intra_only_flag = show_frame ? 0 : aom_rb_read_bit(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265
266 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
267
268 if (intra_only_flag) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700269 if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 if (profile > PROFILE_0) {
271 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700272 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700273 }
274 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
Yaowu Xuf883b422016-08-30 14:01:10 -0700275 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276 }
277 }
278 }
279 if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
Yaowu Xuf883b422016-08-30 14:01:10 -0700280 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281}
282
Yaowu Xuf883b422016-08-30 14:01:10 -0700283static aom_codec_err_t decoder_peek_si(const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284 unsigned int data_sz,
Yaowu Xuf883b422016-08-30 14:01:10 -0700285 aom_codec_stream_info_t *si) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
287}
288
Yaowu Xuf883b422016-08-30 14:01:10 -0700289static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
290 aom_codec_stream_info_t *si) {
Ralph Gilesafe71d92017-05-03 13:18:57 -0700291 memcpy(si, &ctx->si, sizeof(*si));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292
Yaowu Xuf883b422016-08-30 14:01:10 -0700293 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294}
295
Yaowu Xuf883b422016-08-30 14:01:10 -0700296static void set_error_detail(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297 const char *const error) {
298 ctx->base.err_detail = error;
299}
300
Yaowu Xuf883b422016-08-30 14:01:10 -0700301static aom_codec_err_t update_error_state(
302 aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303 if (error->error_code)
304 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
305
306 return error->error_code;
307}
308
Yaowu Xuf883b422016-08-30 14:01:10 -0700309static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310 int i;
311
312 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700313 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700314 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700315 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316 BufferPool *const pool = cm->buffer_pool;
317
318 cm->new_fb_idx = INVALID_IDX;
319 cm->byte_alignment = ctx->byte_alignment;
320 cm->skip_loop_filter = ctx->skip_loop_filter;
321
322 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
323 pool->get_fb_cb = ctx->get_ext_fb_cb;
324 pool->release_fb_cb = ctx->release_ext_fb_cb;
325 pool->cb_priv = ctx->ext_priv;
326 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700327 pool->get_fb_cb = av1_get_frame_buffer;
328 pool->release_fb_cb = av1_release_frame_buffer;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700329
Yaowu Xuf883b422016-08-30 14:01:10 -0700330 if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
331 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700332 "Failed to initialize internal frame buffers");
333
334 pool->cb_priv = &pool->int_frame_buffers;
335 }
336 }
337}
338
Yaowu Xuf883b422016-08-30 14:01:10 -0700339static void set_default_ppflags(aom_postproc_cfg_t *cfg) {
340 cfg->post_proc_flag = AOM_DEBLOCK | AOM_DEMACROBLOCK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341 cfg->deblocking_level = 4;
342 cfg->noise_level = 0;
343}
344
345static int frame_worker_hook(void *arg1, void *arg2) {
346 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
347 const uint8_t *data = frame_worker_data->data;
348 (void)arg2;
349
Yaowu Xuf883b422016-08-30 14:01:10 -0700350 frame_worker_data->result = av1_receive_compressed_data(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700351 frame_worker_data->pbi, frame_worker_data->data_size, &data);
352 frame_worker_data->data_end = data;
353
354 if (frame_worker_data->pbi->common.frame_parallel_decode) {
355 // In frame parallel decoding, a worker thread must successfully decode all
356 // the compressed data.
357 if (frame_worker_data->result != 0 ||
358 frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700359 AVxWorker *const worker = frame_worker_data->pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700360 BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
361 // Signal all the other threads that are waiting for this frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700362 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700363 frame_worker_data->frame_context_ready = 1;
364 lock_buffer_pool(pool);
365 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
366 unlock_buffer_pool(pool);
367 frame_worker_data->pbi->need_resync = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700368 av1_frameworker_signal_stats(worker);
369 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700370 return 0;
371 }
372 } else if (frame_worker_data->result != 0) {
373 // Check decode result in serial decode.
374 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
375 frame_worker_data->pbi->need_resync = 1;
376 }
377 return !frame_worker_data->result;
378}
379
Yaowu Xuf883b422016-08-30 14:01:10 -0700380static aom_codec_err_t init_decoder(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700382 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383
384 ctx->last_show_frame = -1;
385 ctx->next_submit_worker_id = 0;
386 ctx->last_submit_worker_id = 0;
387 ctx->next_output_worker_id = 0;
388 ctx->frame_cache_read = 0;
389 ctx->frame_cache_write = 0;
390 ctx->num_cache_frames = 0;
391 ctx->need_resync = 1;
392 ctx->num_frame_workers =
393 (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads : 1;
394 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
395 ctx->num_frame_workers = MAX_DECODE_THREADS;
396 ctx->available_threads = ctx->num_frame_workers;
397 ctx->flushed = 0;
398
Yaowu Xuf883b422016-08-30 14:01:10 -0700399 ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
400 if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401
402#if CONFIG_MULTITHREAD
403 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
404 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700405 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700406 }
407#endif
408
Yaowu Xuf883b422016-08-30 14:01:10 -0700409 ctx->frame_workers = (AVxWorker *)aom_malloc(ctx->num_frame_workers *
Yaowu Xuc27fc142016-08-22 16:08:15 -0700410 sizeof(*ctx->frame_workers));
411 if (ctx->frame_workers == NULL) {
412 set_error_detail(ctx, "Failed to allocate frame_workers");
Yaowu Xuf883b422016-08-30 14:01:10 -0700413 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700414 }
415
416 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700417 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700418 FrameWorkerData *frame_worker_data = NULL;
419 winterface->init(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -0700420 worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421 if (worker->data1 == NULL) {
422 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700423 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700424 }
425 frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700426 frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700427 if (frame_worker_data->pbi == NULL) {
428 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700429 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700430 }
431 frame_worker_data->pbi->frame_worker_owner = worker;
432 frame_worker_data->worker_id = i;
433 frame_worker_data->scratch_buffer = NULL;
434 frame_worker_data->scratch_buffer_size = 0;
435 frame_worker_data->frame_context_ready = 0;
436 frame_worker_data->received_frame = 0;
437#if CONFIG_MULTITHREAD
438 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
439 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700440 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700441 }
442
443 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
444 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
Yaowu Xuf883b422016-08-30 14:01:10 -0700445 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700446 }
447#endif
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +0200448 frame_worker_data->pbi->allow_lowbitdepth = ctx->cfg.allow_lowbitdepth;
449
Yaowu Xuc27fc142016-08-22 16:08:15 -0700450 // If decoding in serial mode, FrameWorker thread could create tile worker
451 // thread or loopfilter thread.
452 frame_worker_data->pbi->max_threads =
453 (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
454
455 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
456 frame_worker_data->pbi->common.frame_parallel_decode =
457 ctx->frame_parallel_decode;
Yaowu Xuf883b422016-08-30 14:01:10 -0700458 worker->hook = (AVxWorkerHook)frame_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700459 if (!winterface->reset(worker)) {
460 set_error_detail(ctx, "Frame Worker thread creation failed");
Yaowu Xuf883b422016-08-30 14:01:10 -0700461 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700462 }
463 }
464
465 // If postprocessing was enabled by the application and a
466 // configuration has not been provided, default it.
Yaowu Xuf883b422016-08-30 14:01:10 -0700467 if (!ctx->postproc_cfg_set && (ctx->base.init_flags & AOM_CODEC_USE_POSTPROC))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700468 set_default_ppflags(&ctx->postproc_cfg);
469
470 init_buffer_callbacks(ctx);
471
Yaowu Xuf883b422016-08-30 14:01:10 -0700472 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473}
474
Yaowu Xuf883b422016-08-30 14:01:10 -0700475static INLINE void check_resync(aom_codec_alg_priv_t *const ctx,
476 const AV1Decoder *const pbi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477 // Clear resync flag if worker got a key frame or intra only frame.
478 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
479 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
480 ctx->need_resync = 0;
481}
482
Yaowu Xuf883b422016-08-30 14:01:10 -0700483static aom_codec_err_t decode_one(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484 const uint8_t **data, unsigned int data_sz,
485 void *user_priv, int64_t deadline) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487 (void)deadline;
488
489 // Determine the stream parameters. Note that we rely on peek_si to
490 // validate that we have a buffer that does not wrap around the top
491 // of the heap.
492 if (!ctx->si.h) {
493 int is_intra_only = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700494 const aom_codec_err_t res =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700495 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
496 ctx->decrypt_cb, ctx->decrypt_state);
Yaowu Xuf883b422016-08-30 14:01:10 -0700497 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498
Yaowu Xuf883b422016-08-30 14:01:10 -0700499 if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700500 }
501
502 if (!ctx->frame_parallel_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700503 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
505 frame_worker_data->data = *data;
506 frame_worker_data->data_size = data_sz;
507 frame_worker_data->user_priv = user_priv;
508 frame_worker_data->received_frame = 1;
509
510 // Set these even if already initialized. The caller may have changed the
511 // decrypt config between frames.
512 frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
513 frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -0500514#if CONFIG_INSPECTION
515 frame_worker_data->pbi->inspect_cb = ctx->inspect_cb;
516 frame_worker_data->pbi->inspect_ctx = ctx->inspect_ctx;
517#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518
519#if CONFIG_EXT_TILE
520 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
521 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
522#endif // CONFIG_EXT_TILE
523
524 worker->had_error = 0;
525 winterface->execute(worker);
526
527 // Update data pointer after decode.
528 *data = frame_worker_data->data_end;
529
530 if (worker->had_error)
531 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
532
533 check_resync(ctx, frame_worker_data->pbi);
534 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700535 AVxWorker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
537 // Copy context from last worker thread to next worker thread.
538 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
Yaowu Xuf883b422016-08-30 14:01:10 -0700539 av1_frameworker_copy_context(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 &ctx->frame_workers[ctx->next_submit_worker_id],
541 &ctx->frame_workers[ctx->last_submit_worker_id]);
542
543 frame_worker_data->pbi->ready_for_new_data = 0;
544 // Copy the compressed data into worker's internal buffer.
545 // TODO(hkuang): Will all the workers allocate the same size
546 // as the size of the first intra frame be better? This will
547 // avoid too many deallocate and allocate.
548 if (frame_worker_data->scratch_buffer_size < data_sz) {
Alex Converse7f094f12017-02-23 17:29:40 -0800549 aom_free(frame_worker_data->scratch_buffer);
550 frame_worker_data->scratch_buffer = (uint8_t *)aom_malloc(data_sz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 if (frame_worker_data->scratch_buffer == NULL) {
552 set_error_detail(ctx, "Failed to reallocate scratch buffer");
Yaowu Xuf883b422016-08-30 14:01:10 -0700553 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554 }
555 frame_worker_data->scratch_buffer_size = data_sz;
556 }
557 frame_worker_data->data_size = data_sz;
558 memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
559
560 frame_worker_data->frame_decoded = 0;
561 frame_worker_data->frame_context_ready = 0;
562 frame_worker_data->received_frame = 1;
563 frame_worker_data->data = frame_worker_data->scratch_buffer;
564 frame_worker_data->user_priv = user_priv;
565
566 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
567 ctx->last_submit_worker_id =
568 (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
569
570 ctx->next_submit_worker_id =
571 (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
572 --ctx->available_threads;
573 worker->had_error = 0;
574 winterface->launch(worker);
575 }
576
Yaowu Xuf883b422016-08-30 14:01:10 -0700577 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700578}
579
Yaowu Xuf883b422016-08-30 14:01:10 -0700580static void wait_worker_and_cache_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700582 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
583 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
585 ctx->next_output_worker_id =
586 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
587 // TODO(hkuang): Add worker error handling here.
588 winterface->sync(worker);
589 frame_worker_data->received_frame = 0;
590 ++ctx->available_threads;
591
592 check_resync(ctx, frame_worker_data->pbi);
593
Yaowu Xuf883b422016-08-30 14:01:10 -0700594 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
595 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
597 ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
598 yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
599 frame_worker_data->user_priv);
600 ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
601 frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
602 ctx->frame_cache_write = (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
603 ++ctx->num_cache_frames;
604 }
605}
606
Yaowu Xuf883b422016-08-30 14:01:10 -0700607static aom_codec_err_t decoder_decode(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608 const uint8_t *data, unsigned int data_sz,
609 void *user_priv, long deadline) {
610 const uint8_t *data_start = data;
611 const uint8_t *const data_end = data + data_sz;
Yaowu Xuf883b422016-08-30 14:01:10 -0700612 aom_codec_err_t res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700613 uint32_t frame_sizes[8];
614 int frame_count;
615
616 if (data == NULL && data_sz == 0) {
617 ctx->flushed = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700618 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619 }
620
621 // Reset flushed when receiving a valid frame.
622 ctx->flushed = 0;
623
624 // Initialize the decoder workers on the first frame.
625 if (ctx->frame_workers == NULL) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700626 res = init_decoder(ctx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700627 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700628 }
629
Sebastien Alaiwane4c6fc12017-06-21 16:43:22 +0200630 int index_size = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700631 res = av1_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
Sebastien Alaiwane4c6fc12017-06-21 16:43:22 +0200632 &index_size, ctx->decrypt_cb,
633 ctx->decrypt_state);
Yaowu Xuf883b422016-08-30 14:01:10 -0700634 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635
Sebastien Alaiwane4c6fc12017-06-21 16:43:22 +0200636 data_start += index_size;
637
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638 if (ctx->frame_parallel_decode) {
639 // Decode in frame parallel mode. When decoding in this mode, the frame
640 // passed to the decoder must be either a normal frame or a superframe with
641 // superframe index so the decoder could get each frame's start position
642 // in the superframe.
643 if (frame_count > 0) {
644 int i;
645
646 for (i = 0; i < frame_count; ++i) {
647 const uint8_t *data_start_copy = data_start;
648 const uint32_t frame_size = frame_sizes[i];
649 if (data_start < data ||
650 frame_size > (uint32_t)(data_end - data_start)) {
651 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700652 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653 }
654
655 if (ctx->available_threads == 0) {
656 // No more threads for decoding. Wait until the next output worker
657 // finishes decoding. Then copy the decoded frame into cache.
658 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
659 wait_worker_and_cache_frame(ctx);
660 } else {
661 // TODO(hkuang): Add unit test to test this path.
662 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700663 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 }
665 }
666
667 res =
668 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700669 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700670 data_start += frame_size;
671 }
672 } else {
673 if (ctx->available_threads == 0) {
674 // No more threads for decoding. Wait until the next output worker
675 // finishes decoding. Then copy the decoded frame into cache.
676 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
677 wait_worker_and_cache_frame(ctx);
678 } else {
679 // TODO(hkuang): Add unit test to test this path.
680 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700681 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700682 }
683 }
684
685 res = decode_one(ctx, &data, data_sz, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700686 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 }
688 } else {
689 // Decode in serial mode.
690 if (frame_count > 0) {
691 int i;
692
693 for (i = 0; i < frame_count; ++i) {
694 const uint8_t *data_start_copy = data_start;
695 const uint32_t frame_size = frame_sizes[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 if (data_start < data ||
697 frame_size > (uint32_t)(data_end - data_start)) {
698 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700699 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 }
701
702 res =
703 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700705
706 data_start += frame_size;
707 }
708 } else {
709 while (data_start < data_end) {
710 const uint32_t frame_size = (uint32_t)(data_end - data_start);
Urvang Joshi454280d2016-10-14 16:51:44 -0700711 res = decode_one(ctx, &data_start, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700712 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713
714 // Account for suboptimal termination by the encoder.
715 while (data_start < data_end) {
716 const uint8_t marker =
717 read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
718 if (marker) break;
719 ++data_start;
720 }
721 }
722 }
723 }
724
725 return res;
726}
727
Yaowu Xuf883b422016-08-30 14:01:10 -0700728static void release_last_output_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729 RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
730 // Decrease reference count of last output frame in frame parallel mode.
731 if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
732 BufferPool *const pool = ctx->buffer_pool;
733 lock_buffer_pool(pool);
734 decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
735 unlock_buffer_pool(pool);
736 }
737}
738
Yaowu Xuf883b422016-08-30 14:01:10 -0700739static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
740 aom_codec_iter_t *iter) {
741 aom_image_t *img = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700742
743 // Only return frame when all the cpu are busy or
744 // application fluhsed the decoder in frame parallel decode.
745 if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
746 !ctx->flushed) {
747 return NULL;
748 }
749
750 // Output the frames in the cache first.
751 if (ctx->num_cache_frames > 0) {
752 release_last_output_frame(ctx);
753 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
754 if (ctx->need_resync) return NULL;
755 img = &ctx->frame_cache[ctx->frame_cache_read].img;
756 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
757 --ctx->num_cache_frames;
758 return img;
759 }
760
761 // iter acts as a flip flop, so an image is only returned on the first
762 // call to get_frame.
763 if (*iter == NULL && ctx->frame_workers != NULL) {
764 do {
765 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700766 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
767 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768 FrameWorkerData *const frame_worker_data =
769 (FrameWorkerData *)worker->data1;
770 ctx->next_output_worker_id =
771 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
772 // Wait for the frame from worker thread.
773 if (winterface->sync(worker)) {
774 // Check if worker has received any frames.
775 if (frame_worker_data->received_frame == 1) {
776 ++ctx->available_threads;
777 frame_worker_data->received_frame = 0;
778 check_resync(ctx, frame_worker_data->pbi);
779 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700780 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
781 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
783 release_last_output_frame(ctx);
784 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
785 if (ctx->need_resync) return NULL;
786 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
787
788#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700789 if (cm->single_tile_decoding &&
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800790 frame_worker_data->pbi->dec_tile_row >= 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 const int tile_row =
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 AOMMIN(frame_worker_data->pbi->dec_tile_row, cm->tile_rows - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793 const int mi_row = tile_row * cm->tile_height;
794 const int ssy = ctx->img.y_chroma_shift;
795 int plane;
796 ctx->img.planes[0] += mi_row * MI_SIZE * ctx->img.stride[0];
797 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
798 ctx->img.planes[plane] +=
799 mi_row * (MI_SIZE >> ssy) * ctx->img.stride[plane];
800 }
801 ctx->img.d_h =
Yaowu Xuf883b422016-08-30 14:01:10 -0700802 AOMMIN(cm->tile_height, cm->mi_rows - mi_row) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803 }
804
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700805 if (cm->single_tile_decoding &&
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800806 frame_worker_data->pbi->dec_tile_col >= 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807 const int tile_col =
Yaowu Xuf883b422016-08-30 14:01:10 -0700808 AOMMIN(frame_worker_data->pbi->dec_tile_col, cm->tile_cols - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 const int mi_col = tile_col * cm->tile_width;
810 const int ssx = ctx->img.x_chroma_shift;
811 int plane;
812 ctx->img.planes[0] += mi_col * MI_SIZE;
813 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
814 ctx->img.planes[plane] += mi_col * (MI_SIZE >> ssx);
815 }
816 ctx->img.d_w =
Yaowu Xuf883b422016-08-30 14:01:10 -0700817 AOMMIN(cm->tile_width, cm->mi_cols - mi_col) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 }
819#endif // CONFIG_EXT_TILE
820
821 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
822 img = &ctx->img;
823 return img;
824 }
825 } else {
826 // Decoding failed. Release the worker thread.
827 frame_worker_data->received_frame = 0;
828 ++ctx->available_threads;
829 ctx->need_resync = 1;
830 if (ctx->flushed != 1) return NULL;
831 }
832 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
833 }
834 return NULL;
835}
836
Yaowu Xuf883b422016-08-30 14:01:10 -0700837static aom_codec_err_t decoder_set_fb_fn(
838 aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
839 aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840 if (cb_get == NULL || cb_release == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842 } else if (ctx->frame_workers == NULL) {
843 // If the decoder has already been initialized, do not accept changes to
844 // the frame buffer functions.
845 ctx->get_ext_fb_cb = cb_get;
846 ctx->release_ext_fb_cb = cb_release;
847 ctx->ext_priv = cb_priv;
Yaowu Xuf883b422016-08-30 14:01:10 -0700848 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849 }
850
Yaowu Xuf883b422016-08-30 14:01:10 -0700851 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852}
853
Yaowu Xuf883b422016-08-30 14:01:10 -0700854static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 aom_ref_frame_t *const data = va_arg(args, aom_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857
858 // Only support this function in serial decode.
859 if (ctx->frame_parallel_decode) {
860 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700861 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862 }
863
864 if (data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700865 aom_ref_frame_t *const frame = (aom_ref_frame_t *)data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700867 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
869 image2yuvconfig(&frame->img, &sd);
Yaowu Xuf883b422016-08-30 14:01:10 -0700870 return av1_set_reference_dec(&frame_worker_data->pbi->common,
871 ref_frame_to_av1_reframe(frame->frame_type),
872 &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700874 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 }
876}
877
Yaowu Xuf883b422016-08-30 14:01:10 -0700878static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700879 va_list args) {
Urvang Joshi77853e52016-07-15 12:47:01 -0700880 const aom_ref_frame_t *const frame = va_arg(args, aom_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881
882 // Only support this function in serial decode.
883 if (ctx->frame_parallel_decode) {
884 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700885 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700886 }
887
Urvang Joshi77853e52016-07-15 12:47:01 -0700888 if (frame) {
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);
Yaowu Xuf883b422016-08-30 14:01:10 -0700893 return av1_copy_reference_dec(frame_worker_data->pbi,
894 (AOM_REFFRAME)frame->frame_type, &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_get_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700901 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700902 av1_ref_frame_t *data = 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
910 if (data) {
911 YV12_BUFFER_CONFIG *fb;
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 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700915 if (fb == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916 yuvconfig2image(&data->img, fb, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700917 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700918 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700919 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700920 }
921}
922
Yaowu Xuf883b422016-08-30 14:01:10 -0700923static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700925 aom_image_t *new_img = va_arg(args, aom_image_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700926
927 // Only support this function in serial decode.
928 if (ctx->frame_parallel_decode) {
929 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700930 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700931 }
932
933 if (new_img) {
934 YV12_BUFFER_CONFIG new_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -0700935 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
937
Yaowu Xuf883b422016-08-30 14:01:10 -0700938 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700939 yuvconfig2image(new_img, &new_frame, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700940 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700942 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700943 }
944 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700945 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700946 }
947}
948
Yaowu Xuf883b422016-08-30 14:01:10 -0700949static aom_codec_err_t ctrl_set_postproc(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700950 va_list args) {
951 (void)ctx;
952 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700953 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700954}
955
Yaowu Xuf883b422016-08-30 14:01:10 -0700956static aom_codec_err_t ctrl_set_dbg_options(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957 va_list args) {
958 (void)ctx;
959 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700960 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700961}
962
Yaowu Xuf883b422016-08-30 14:01:10 -0700963static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700964 va_list args) {
965 int *const update_info = va_arg(args, int *);
966
967 // Only support this function in serial decode.
968 if (ctx->frame_parallel_decode) {
969 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700970 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700971 }
972
973 if (update_info) {
974 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700975 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700976 FrameWorkerData *const frame_worker_data =
977 (FrameWorkerData *)worker->data1;
978 *update_info = frame_worker_data->pbi->refresh_frame_flags;
Yaowu Xuf883b422016-08-30 14:01:10 -0700979 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700981 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700982 }
983 }
984
Yaowu Xuf883b422016-08-30 14:01:10 -0700985 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700986}
987
Peter Boströma1f64322017-01-19 11:35:30 -0500988static aom_codec_err_t ctrl_get_last_quantizer(aom_codec_alg_priv_t *ctx,
989 va_list args) {
990 int *const arg = va_arg(args, int *);
991 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
992 *arg =
993 ((FrameWorkerData *)ctx->frame_workers[0].data1)->pbi->common.base_qindex;
994 return AOM_CODEC_OK;
995}
996
Yaowu Xuf883b422016-08-30 14:01:10 -0700997static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700998 va_list args) {
999 int *corrupted = va_arg(args, int *);
1000
1001 if (corrupted) {
1002 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001003 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004 FrameWorkerData *const frame_worker_data =
1005 (FrameWorkerData *)worker->data1;
1006 RefCntBuffer *const frame_bufs =
1007 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
1008 if (frame_worker_data->pbi->common.frame_to_show == NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -07001009 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001010 if (ctx->last_show_frame >= 0)
1011 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
Yaowu Xuf883b422016-08-30 14:01:10 -07001012 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001013 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001014 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001015 }
1016 }
1017
Yaowu Xuf883b422016-08-30 14:01:10 -07001018 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001019}
1020
Yaowu Xuf883b422016-08-30 14:01:10 -07001021static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001022 va_list args) {
1023 int *const frame_size = va_arg(args, int *);
1024
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 (frame_size) {
1032 if (ctx->frame_workers) {
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 =
1035 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001036 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001037 frame_size[0] = cm->width;
1038 frame_size[1] = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001039 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001040 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001041 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001042 }
1043 }
1044
Yaowu Xuf883b422016-08-30 14:01:10 -07001045 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046}
1047
Yaowu Xuf883b422016-08-30 14:01:10 -07001048static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001049 va_list args) {
1050 int *const render_size = va_arg(args, int *);
1051
1052 // Only support this function in serial decode.
1053 if (ctx->frame_parallel_decode) {
1054 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001055 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001056 }
1057
1058 if (render_size) {
1059 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001060 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001061 FrameWorkerData *const frame_worker_data =
1062 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001063 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001064 render_size[0] = cm->render_width;
1065 render_size[1] = cm->render_height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001066 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001067 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001068 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001069 }
1070 }
1071
Yaowu Xuf883b422016-08-30 14:01:10 -07001072 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001073}
1074
Yaowu Xuf883b422016-08-30 14:01:10 -07001075static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001076 va_list args) {
1077 unsigned int *const bit_depth = va_arg(args, unsigned int *);
Yaowu Xuf883b422016-08-30 14:01:10 -07001078 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001079
1080 if (bit_depth) {
1081 if (worker) {
1082 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 *bit_depth = cm->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -07001086 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001087 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001088 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001089 }
1090 }
1091
Yaowu Xuf883b422016-08-30 14:01:10 -07001092 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001093}
1094
Yaowu Xuf883b422016-08-30 14:01:10 -07001095static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001096 va_list args) {
1097 ctx->invert_tile_order = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001098 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001099}
1100
Yaowu Xuf883b422016-08-30 14:01:10 -07001101static aom_codec_err_t ctrl_set_decryptor(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001102 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001103 aom_decrypt_init *init = va_arg(args, aom_decrypt_init *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
1105 ctx->decrypt_state = init ? init->decrypt_state : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07001106 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001107}
1108
Yaowu Xuf883b422016-08-30 14:01:10 -07001109static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001110 va_list args) {
1111 const int legacy_byte_alignment = 0;
1112 const int min_byte_alignment = 32;
1113 const int max_byte_alignment = 1024;
1114 const int byte_alignment = va_arg(args, int);
1115
1116 if (byte_alignment != legacy_byte_alignment &&
1117 (byte_alignment < min_byte_alignment ||
1118 byte_alignment > max_byte_alignment ||
1119 (byte_alignment & (byte_alignment - 1)) != 0))
Yaowu Xuf883b422016-08-30 14:01:10 -07001120 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001121
1122 ctx->byte_alignment = byte_alignment;
1123 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001124 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001125 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1126 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1127 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001128 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001129}
1130
Yaowu Xuf883b422016-08-30 14:01:10 -07001131static aom_codec_err_t ctrl_set_skip_loop_filter(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001132 va_list args) {
1133 ctx->skip_loop_filter = va_arg(args, int);
1134
1135 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001136 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001137 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1138 frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
1139 }
1140
Yaowu Xuf883b422016-08-30 14:01:10 -07001141 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001142}
1143
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001144static aom_codec_err_t ctrl_get_accounting(aom_codec_alg_priv_t *ctx,
1145 va_list args) {
1146#if !CONFIG_ACCOUNTING
1147 (void)ctx;
1148 (void)args;
1149 return AOM_CODEC_INCAPABLE;
1150#else
1151 if (ctx->frame_workers) {
1152 AVxWorker *const worker = ctx->frame_workers;
1153 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1154 AV1Decoder *pbi = frame_worker_data->pbi;
1155 Accounting **acct = va_arg(args, Accounting **);
1156 *acct = &pbi->accounting;
1157 return AOM_CODEC_OK;
1158 }
1159 return AOM_CODEC_ERROR;
1160#endif
1161}
Yaowu Xuf883b422016-08-30 14:01:10 -07001162static aom_codec_err_t ctrl_set_decode_tile_row(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001163 va_list args) {
1164 ctx->decode_tile_row = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001165 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166}
1167
Yaowu Xuf883b422016-08-30 14:01:10 -07001168static aom_codec_err_t ctrl_set_decode_tile_col(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001169 va_list args) {
1170 ctx->decode_tile_col = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001171 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001172}
1173
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001174static aom_codec_err_t ctrl_set_inspection_callback(aom_codec_alg_priv_t *ctx,
1175 va_list args) {
1176#if !CONFIG_INSPECTION
1177 (void)ctx;
1178 (void)args;
1179 return AOM_CODEC_INCAPABLE;
1180#else
1181 aom_inspect_init *init = va_arg(args, aom_inspect_init *);
1182 ctx->inspect_cb = init->inspect_cb;
1183 ctx->inspect_ctx = init->inspect_ctx;
1184 return AOM_CODEC_OK;
1185#endif
1186}
1187
Yaowu Xuf883b422016-08-30 14:01:10 -07001188static aom_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
1189 { AOM_COPY_REFERENCE, ctrl_copy_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001190
1191 // Setters
Yaowu Xuf883b422016-08-30 14:01:10 -07001192 { AOM_SET_REFERENCE, ctrl_set_reference },
1193 { AOM_SET_POSTPROC, ctrl_set_postproc },
1194 { AOM_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options },
1195 { AOM_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options },
1196 { AOM_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options },
1197 { AOM_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options },
1198 { AV1_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
1199 { AOMD_SET_DECRYPTOR, ctrl_set_decryptor },
1200 { AV1_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
1201 { AV1_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
1202 { AV1_SET_DECODE_TILE_ROW, ctrl_set_decode_tile_row },
1203 { AV1_SET_DECODE_TILE_COL, ctrl_set_decode_tile_col },
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001204 { AV1_SET_INSPECTION_CALLBACK, ctrl_set_inspection_callback },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001205
1206 // Getters
Yaowu Xuf883b422016-08-30 14:01:10 -07001207 { AOMD_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
Peter Boströma1f64322017-01-19 11:35:30 -05001208 { AOMD_GET_LAST_QUANTIZER, ctrl_get_last_quantizer },
1209 { AOMD_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
Yaowu Xuf883b422016-08-30 14:01:10 -07001210 { AV1D_GET_BIT_DEPTH, ctrl_get_bit_depth },
Peter Boströma1f64322017-01-19 11:35:30 -05001211 { AV1D_GET_DISPLAY_SIZE, ctrl_get_render_size },
Yaowu Xuf883b422016-08-30 14:01:10 -07001212 { AV1D_GET_FRAME_SIZE, ctrl_get_frame_size },
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001213 { AV1_GET_ACCOUNTING, ctrl_get_accounting },
Yaowu Xuf883b422016-08-30 14:01:10 -07001214 { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
Peter Boströma1f64322017-01-19 11:35:30 -05001215 { AV1_GET_REFERENCE, ctrl_get_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001216
1217 { -1, NULL },
1218};
1219
1220#ifndef VERSION_STRING
1221#define VERSION_STRING
1222#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001223CODEC_INTERFACE(aom_codec_av1_dx) = {
1224 "AOMedia Project AV1 Decoder" VERSION_STRING,
1225 AOM_CODEC_INTERNAL_ABI_VERSION,
1226 AOM_CODEC_CAP_DECODER |
1227 AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // aom_codec_caps_t
1228 decoder_init, // aom_codec_init_fn_t
1229 decoder_destroy, // aom_codec_destroy_fn_t
1230 decoder_ctrl_maps, // aom_codec_ctrl_fn_map_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001231 {
1232 // NOLINT
Yaowu Xuf883b422016-08-30 14:01:10 -07001233 decoder_peek_si, // aom_codec_peek_si_fn_t
1234 decoder_get_si, // aom_codec_get_si_fn_t
1235 decoder_decode, // aom_codec_decode_fn_t
1236 decoder_get_frame, // aom_codec_frame_get_fn_t
1237 decoder_set_fb_fn, // aom_codec_set_fb_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001238 },
1239 {
1240 // NOLINT
1241 0,
Yaowu Xuf883b422016-08-30 14:01:10 -07001242 NULL, // aom_codec_enc_cfg_map_t
1243 NULL, // aom_codec_encode_fn_t
1244 NULL, // aom_codec_get_cx_data_fn_t
1245 NULL, // aom_codec_enc_config_set_fn_t
1246 NULL, // aom_codec_get_global_headers_fn_t
1247 NULL, // aom_codec_get_preview_frame_fn_t
1248 NULL // aom_codec_enc_mr_get_mem_loc_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001249 }
1250};