blob: 8cd40a38fdf6f7e6ed276ebe136f976305be1559 [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 Xuf883b422016-08-30 14:01:10 -070034typedef aom_codec_stream_info_t av1_stream_info_t;
Yaowu Xuc27fc142016-08-22 16:08:15 -070035
36// This limit is due to framebuffer numbers.
37// TODO(hkuang): Remove this limit after implementing ondemand framebuffers.
38#define FRAME_CACHE_SIZE 6 // Cache maximum 6 decoded frames.
39
40typedef struct cache_frame {
41 int fb_idx;
Yaowu Xuf883b422016-08-30 14:01:10 -070042 aom_image_t img;
Yaowu Xuc27fc142016-08-22 16:08:15 -070043} cache_frame;
44
Yaowu Xuf883b422016-08-30 14:01:10 -070045struct aom_codec_alg_priv {
46 aom_codec_priv_t base;
47 aom_codec_dec_cfg_t cfg;
48 av1_stream_info_t si;
Yaowu Xuc27fc142016-08-22 16:08:15 -070049 int postproc_cfg_set;
Yaowu Xuf883b422016-08-30 14:01:10 -070050 aom_postproc_cfg_t postproc_cfg;
51 aom_decrypt_cb decrypt_cb;
Yaowu Xuc27fc142016-08-22 16:08:15 -070052 void *decrypt_state;
Yaowu Xuf883b422016-08-30 14:01:10 -070053 aom_image_t img;
Yaowu Xuc27fc142016-08-22 16:08:15 -070054 int img_avail;
55 int flushed;
56 int invert_tile_order;
57 int last_show_frame; // Index of last output frame.
58 int byte_alignment;
59 int skip_loop_filter;
60 int decode_tile_row;
61 int decode_tile_col;
62
63 // Frame parallel related.
64 int frame_parallel_decode; // frame-based threading.
Yaowu Xuf883b422016-08-30 14:01:10 -070065 AVxWorker *frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -070066 int num_frame_workers;
67 int next_submit_worker_id;
68 int last_submit_worker_id;
69 int next_output_worker_id;
70 int available_threads;
71 cache_frame frame_cache[FRAME_CACHE_SIZE];
72 int frame_cache_write;
73 int frame_cache_read;
74 int num_cache_frames;
75 int need_resync; // wait for key/intra-only frame
76 // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
77 BufferPool *buffer_pool;
78
Yaowu Xuf883b422016-08-30 14:01:10 -070079 // External frame buffer info to save for AV1 common.
Yaowu Xuc27fc142016-08-22 16:08:15 -070080 void *ext_priv; // Private data associated with the external frame buffers.
Yaowu Xuf883b422016-08-30 14:01:10 -070081 aom_get_frame_buffer_cb_fn_t get_ext_fb_cb;
82 aom_release_frame_buffer_cb_fn_t release_ext_fb_cb;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050083
84#if CONFIG_INSPECTION
85 aom_inspect_cb inspect_cb;
86 void *inspect_ctx;
87#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070088};
89
Yaowu Xuf883b422016-08-30 14:01:10 -070090static aom_codec_err_t decoder_init(aom_codec_ctx_t *ctx,
91 aom_codec_priv_enc_mr_cfg_t *data) {
92 // This function only allocates space for the aom_codec_alg_priv_t
Yaowu Xuc27fc142016-08-22 16:08:15 -070093 // structure. More memory may be required at the time the stream
94 // information becomes known.
95 (void)data;
96
97 if (!ctx->priv) {
Yaowu Xuf883b422016-08-30 14:01:10 -070098 aom_codec_alg_priv_t *const priv =
99 (aom_codec_alg_priv_t *)aom_calloc(1, sizeof(*priv));
100 if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101
Yaowu Xuf883b422016-08-30 14:01:10 -0700102 ctx->priv = (aom_codec_priv_t *)priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103 ctx->priv->init_flags = ctx->init_flags;
104 priv->si.sz = sizeof(priv->si);
105 priv->flushed = 0;
106 // Only do frame parallel decode when threads > 1.
107 priv->frame_parallel_decode =
108 (ctx->config.dec && (ctx->config.dec->threads > 1) &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700109 (ctx->init_flags & AOM_CODEC_USE_FRAME_THREADING))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700110 ? 1
111 : 0;
112 if (ctx->config.dec) {
113 priv->cfg = *ctx->config.dec;
114 ctx->config.dec = &priv->cfg;
115 }
116 }
117
Yaowu Xuf883b422016-08-30 14:01:10 -0700118 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119}
120
Yaowu Xuf883b422016-08-30 14:01:10 -0700121static aom_codec_err_t decoder_destroy(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700122 if (ctx->frame_workers != NULL) {
123 int i;
124 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700125 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700126 FrameWorkerData *const frame_worker_data =
127 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700128 aom_get_worker_interface()->end(worker);
129 av1_remove_common(&frame_worker_data->pbi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 av1_free_restoration_buffers(&frame_worker_data->pbi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700133 av1_decoder_remove(frame_worker_data->pbi);
134 aom_free(frame_worker_data->scratch_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700135#if CONFIG_MULTITHREAD
136 pthread_mutex_destroy(&frame_worker_data->stats_mutex);
137 pthread_cond_destroy(&frame_worker_data->stats_cond);
138#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700139 aom_free(frame_worker_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140 }
141#if CONFIG_MULTITHREAD
142 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
143#endif
144 }
145
146 if (ctx->buffer_pool) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700147 av1_free_ref_frame_buffers(ctx->buffer_pool);
148 av1_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149 }
150
Yaowu Xuf883b422016-08-30 14:01:10 -0700151 aom_free(ctx->frame_workers);
152 aom_free(ctx->buffer_pool);
153 aom_free(ctx);
154 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155}
156
157static int parse_bitdepth_colorspace_sampling(BITSTREAM_PROFILE profile,
Yaowu Xuf883b422016-08-30 14:01:10 -0700158 struct aom_read_bit_buffer *rb) {
159 aom_color_space_t color_space;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 if (profile >= PROFILE_2) rb->bit_offset += 1; // Bit-depth 10 or 12.
Yaowu Xuf883b422016-08-30 14:01:10 -0700161 color_space = (aom_color_space_t)aom_rb_read_literal(rb, 3);
162 if (color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700163 rb->bit_offset += 1; // [16,235] (including xvycc) vs [0,255] range.
164 if (profile == PROFILE_1 || profile == PROFILE_3) {
165 rb->bit_offset += 2; // subsampling x/y.
166 rb->bit_offset += 1; // unused.
167 }
168 } else {
169 if (profile == PROFILE_1 || profile == PROFILE_3) {
170 rb->bit_offset += 1; // unused
171 } else {
172 // RGB is only available in version 1.
173 return 0;
174 }
175 }
176 return 1;
177}
178
Yaowu Xuf883b422016-08-30 14:01:10 -0700179static aom_codec_err_t decoder_peek_si_internal(
180 const uint8_t *data, unsigned int data_sz, aom_codec_stream_info_t *si,
181 int *is_intra_only, aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 int intra_only_flag = 0;
183 uint8_t clear_buffer[9];
184
Yaowu Xuf883b422016-08-30 14:01:10 -0700185 if (data + data_sz <= data) return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186
187 si->is_kf = 0;
188 si->w = si->h = 0;
189
190 if (decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700191 data_sz = AOMMIN(sizeof(clear_buffer), data_sz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192 decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
193 data = clear_buffer;
194 }
195
196 {
197 int show_frame;
198 int error_resilient;
Yaowu Xuf883b422016-08-30 14:01:10 -0700199 struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
200 const int frame_marker = aom_rb_read_literal(&rb, 2);
201 const BITSTREAM_PROFILE profile = av1_read_profile(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202
Yaowu Xuf883b422016-08-30 14:01:10 -0700203 if (frame_marker != AOM_FRAME_MARKER) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204
Yaowu Xuf883b422016-08-30 14:01:10 -0700205 if (profile >= MAX_PROFILES) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700206
207 if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
Yaowu Xuf883b422016-08-30 14:01:10 -0700208 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209
Yaowu Xuf883b422016-08-30 14:01:10 -0700210 if (aom_rb_read_bit(&rb)) { // show an existing frame
211 aom_rb_read_literal(&rb, 3); // Frame buffer to show.
212 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213 }
214
Yaowu Xuf883b422016-08-30 14:01:10 -0700215 if (data_sz <= 8) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700216
Yaowu Xuf883b422016-08-30 14:01:10 -0700217 si->is_kf = !aom_rb_read_bit(&rb);
218 show_frame = aom_rb_read_bit(&rb);
219 error_resilient = aom_rb_read_bit(&rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100220#if CONFIG_REFERENCE_BUFFER
221 {
222 /* TODO: Move outside frame loop or inside key-frame branch */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +0100223 int frame_id_len;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100224 SequenceHeader seq_params;
225 read_sequence_header(&seq_params);
226 if (seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +0100227 frame_id_len = seq_params.frame_id_length_minus7 + 7;
228 aom_rb_read_literal(&rb, frame_id_len);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100229 }
230 }
231#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700232 if (si->is_kf) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700233 if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234
235 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700236 return AOM_CODEC_UNSUP_BITSTREAM;
237 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700239 intra_only_flag = show_frame ? 0 : aom_rb_read_bit(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240
241 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
242
243 if (intra_only_flag) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700244 if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700245 if (profile > PROFILE_0) {
246 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700247 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700248 }
249 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
Yaowu Xuf883b422016-08-30 14:01:10 -0700250 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251 }
252 }
253 }
254 if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
Yaowu Xuf883b422016-08-30 14:01:10 -0700255 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256}
257
Yaowu Xuf883b422016-08-30 14:01:10 -0700258static aom_codec_err_t decoder_peek_si(const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259 unsigned int data_sz,
Yaowu Xuf883b422016-08-30 14:01:10 -0700260 aom_codec_stream_info_t *si) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261 return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
262}
263
Yaowu Xuf883b422016-08-30 14:01:10 -0700264static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
265 aom_codec_stream_info_t *si) {
266 const size_t sz = (si->sz >= sizeof(av1_stream_info_t))
267 ? sizeof(av1_stream_info_t)
268 : sizeof(aom_codec_stream_info_t);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 memcpy(si, &ctx->si, sz);
270 si->sz = (unsigned int)sz;
271
Yaowu Xuf883b422016-08-30 14:01:10 -0700272 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700273}
274
Yaowu Xuf883b422016-08-30 14:01:10 -0700275static void set_error_detail(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276 const char *const error) {
277 ctx->base.err_detail = error;
278}
279
Yaowu Xuf883b422016-08-30 14:01:10 -0700280static aom_codec_err_t update_error_state(
281 aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700282 if (error->error_code)
283 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
284
285 return error->error_code;
286}
287
Yaowu Xuf883b422016-08-30 14:01:10 -0700288static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 int i;
290
291 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700292 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700293 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700294 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295 BufferPool *const pool = cm->buffer_pool;
296
297 cm->new_fb_idx = INVALID_IDX;
298 cm->byte_alignment = ctx->byte_alignment;
299 cm->skip_loop_filter = ctx->skip_loop_filter;
300
301 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
302 pool->get_fb_cb = ctx->get_ext_fb_cb;
303 pool->release_fb_cb = ctx->release_ext_fb_cb;
304 pool->cb_priv = ctx->ext_priv;
305 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700306 pool->get_fb_cb = av1_get_frame_buffer;
307 pool->release_fb_cb = av1_release_frame_buffer;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308
Yaowu Xuf883b422016-08-30 14:01:10 -0700309 if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
310 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700311 "Failed to initialize internal frame buffers");
312
313 pool->cb_priv = &pool->int_frame_buffers;
314 }
315 }
316}
317
Yaowu Xuf883b422016-08-30 14:01:10 -0700318static void set_default_ppflags(aom_postproc_cfg_t *cfg) {
319 cfg->post_proc_flag = AOM_DEBLOCK | AOM_DEMACROBLOCK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320 cfg->deblocking_level = 4;
321 cfg->noise_level = 0;
322}
323
324static int frame_worker_hook(void *arg1, void *arg2) {
325 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
326 const uint8_t *data = frame_worker_data->data;
327 (void)arg2;
328
Yaowu Xuf883b422016-08-30 14:01:10 -0700329 frame_worker_data->result = av1_receive_compressed_data(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700330 frame_worker_data->pbi, frame_worker_data->data_size, &data);
331 frame_worker_data->data_end = data;
332
333 if (frame_worker_data->pbi->common.frame_parallel_decode) {
334 // In frame parallel decoding, a worker thread must successfully decode all
335 // the compressed data.
336 if (frame_worker_data->result != 0 ||
337 frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700338 AVxWorker *const worker = frame_worker_data->pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339 BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
340 // Signal all the other threads that are waiting for this frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700341 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342 frame_worker_data->frame_context_ready = 1;
343 lock_buffer_pool(pool);
344 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
345 unlock_buffer_pool(pool);
346 frame_worker_data->pbi->need_resync = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700347 av1_frameworker_signal_stats(worker);
348 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349 return 0;
350 }
351 } else if (frame_worker_data->result != 0) {
352 // Check decode result in serial decode.
353 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
354 frame_worker_data->pbi->need_resync = 1;
355 }
356 return !frame_worker_data->result;
357}
358
Yaowu Xuf883b422016-08-30 14:01:10 -0700359static aom_codec_err_t init_decoder(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700360 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700361 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362
363 ctx->last_show_frame = -1;
364 ctx->next_submit_worker_id = 0;
365 ctx->last_submit_worker_id = 0;
366 ctx->next_output_worker_id = 0;
367 ctx->frame_cache_read = 0;
368 ctx->frame_cache_write = 0;
369 ctx->num_cache_frames = 0;
370 ctx->need_resync = 1;
371 ctx->num_frame_workers =
372 (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads : 1;
373 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
374 ctx->num_frame_workers = MAX_DECODE_THREADS;
375 ctx->available_threads = ctx->num_frame_workers;
376 ctx->flushed = 0;
377
Yaowu Xuf883b422016-08-30 14:01:10 -0700378 ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
379 if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700380
381#if CONFIG_MULTITHREAD
382 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
383 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700384 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700385 }
386#endif
387
Yaowu Xuf883b422016-08-30 14:01:10 -0700388 ctx->frame_workers = (AVxWorker *)aom_malloc(ctx->num_frame_workers *
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389 sizeof(*ctx->frame_workers));
390 if (ctx->frame_workers == NULL) {
391 set_error_detail(ctx, "Failed to allocate frame_workers");
Yaowu Xuf883b422016-08-30 14:01:10 -0700392 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393 }
394
395 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700396 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397 FrameWorkerData *frame_worker_data = NULL;
398 winterface->init(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -0700399 worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400 if (worker->data1 == NULL) {
401 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700402 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700403 }
404 frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700405 frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700406 if (frame_worker_data->pbi == NULL) {
407 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700408 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700409 }
410 frame_worker_data->pbi->frame_worker_owner = worker;
411 frame_worker_data->worker_id = i;
412 frame_worker_data->scratch_buffer = NULL;
413 frame_worker_data->scratch_buffer_size = 0;
414 frame_worker_data->frame_context_ready = 0;
415 frame_worker_data->received_frame = 0;
416#if CONFIG_MULTITHREAD
417 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
418 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700419 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700420 }
421
422 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
423 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
Yaowu Xuf883b422016-08-30 14:01:10 -0700424 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700425 }
426#endif
427 // If decoding in serial mode, FrameWorker thread could create tile worker
428 // thread or loopfilter thread.
429 frame_worker_data->pbi->max_threads =
430 (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
431
432 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
433 frame_worker_data->pbi->common.frame_parallel_decode =
434 ctx->frame_parallel_decode;
Yaowu Xuf883b422016-08-30 14:01:10 -0700435 worker->hook = (AVxWorkerHook)frame_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700436 if (!winterface->reset(worker)) {
437 set_error_detail(ctx, "Frame Worker thread creation failed");
Yaowu Xuf883b422016-08-30 14:01:10 -0700438 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700439 }
440 }
441
442 // If postprocessing was enabled by the application and a
443 // configuration has not been provided, default it.
Yaowu Xuf883b422016-08-30 14:01:10 -0700444 if (!ctx->postproc_cfg_set && (ctx->base.init_flags & AOM_CODEC_USE_POSTPROC))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445 set_default_ppflags(&ctx->postproc_cfg);
446
447 init_buffer_callbacks(ctx);
448
Yaowu Xuf883b422016-08-30 14:01:10 -0700449 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700450}
451
Yaowu Xuf883b422016-08-30 14:01:10 -0700452static INLINE void check_resync(aom_codec_alg_priv_t *const ctx,
453 const AV1Decoder *const pbi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700454 // Clear resync flag if worker got a key frame or intra only frame.
455 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
456 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
457 ctx->need_resync = 0;
458}
459
Yaowu Xuf883b422016-08-30 14:01:10 -0700460static aom_codec_err_t decode_one(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700461 const uint8_t **data, unsigned int data_sz,
462 void *user_priv, int64_t deadline) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700463 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464 (void)deadline;
465
466 // Determine the stream parameters. Note that we rely on peek_si to
467 // validate that we have a buffer that does not wrap around the top
468 // of the heap.
469 if (!ctx->si.h) {
470 int is_intra_only = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700471 const aom_codec_err_t res =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
473 ctx->decrypt_cb, ctx->decrypt_state);
Yaowu Xuf883b422016-08-30 14:01:10 -0700474 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475
Yaowu Xuf883b422016-08-30 14:01:10 -0700476 if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477 }
478
479 if (!ctx->frame_parallel_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700480 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
482 frame_worker_data->data = *data;
483 frame_worker_data->data_size = data_sz;
484 frame_worker_data->user_priv = user_priv;
485 frame_worker_data->received_frame = 1;
486
487 // Set these even if already initialized. The caller may have changed the
488 // decrypt config between frames.
489 frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
490 frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -0500491#if CONFIG_INSPECTION
492 frame_worker_data->pbi->inspect_cb = ctx->inspect_cb;
493 frame_worker_data->pbi->inspect_ctx = ctx->inspect_ctx;
494#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700495
496#if CONFIG_EXT_TILE
497 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
498 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
499#endif // CONFIG_EXT_TILE
500
501 worker->had_error = 0;
502 winterface->execute(worker);
503
504 // Update data pointer after decode.
505 *data = frame_worker_data->data_end;
506
507 if (worker->had_error)
508 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
509
510 check_resync(ctx, frame_worker_data->pbi);
511 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700512 AVxWorker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
514 // Copy context from last worker thread to next worker thread.
515 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
Yaowu Xuf883b422016-08-30 14:01:10 -0700516 av1_frameworker_copy_context(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700517 &ctx->frame_workers[ctx->next_submit_worker_id],
518 &ctx->frame_workers[ctx->last_submit_worker_id]);
519
520 frame_worker_data->pbi->ready_for_new_data = 0;
521 // Copy the compressed data into worker's internal buffer.
522 // TODO(hkuang): Will all the workers allocate the same size
523 // as the size of the first intra frame be better? This will
524 // avoid too many deallocate and allocate.
525 if (frame_worker_data->scratch_buffer_size < data_sz) {
Alex Converse7f094f12017-02-23 17:29:40 -0800526 aom_free(frame_worker_data->scratch_buffer);
527 frame_worker_data->scratch_buffer = (uint8_t *)aom_malloc(data_sz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528 if (frame_worker_data->scratch_buffer == NULL) {
529 set_error_detail(ctx, "Failed to reallocate scratch buffer");
Yaowu Xuf883b422016-08-30 14:01:10 -0700530 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531 }
532 frame_worker_data->scratch_buffer_size = data_sz;
533 }
534 frame_worker_data->data_size = data_sz;
535 memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
536
537 frame_worker_data->frame_decoded = 0;
538 frame_worker_data->frame_context_ready = 0;
539 frame_worker_data->received_frame = 1;
540 frame_worker_data->data = frame_worker_data->scratch_buffer;
541 frame_worker_data->user_priv = user_priv;
542
543 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
544 ctx->last_submit_worker_id =
545 (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
546
547 ctx->next_submit_worker_id =
548 (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
549 --ctx->available_threads;
550 worker->had_error = 0;
551 winterface->launch(worker);
552 }
553
Yaowu Xuf883b422016-08-30 14:01:10 -0700554 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700555}
556
Yaowu Xuf883b422016-08-30 14:01:10 -0700557static void wait_worker_and_cache_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700559 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
560 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
562 ctx->next_output_worker_id =
563 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
564 // TODO(hkuang): Add worker error handling here.
565 winterface->sync(worker);
566 frame_worker_data->received_frame = 0;
567 ++ctx->available_threads;
568
569 check_resync(ctx, frame_worker_data->pbi);
570
Yaowu Xuf883b422016-08-30 14:01:10 -0700571 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
572 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700573 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
574 ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
575 yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
576 frame_worker_data->user_priv);
577 ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
578 frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
579 ctx->frame_cache_write = (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
580 ++ctx->num_cache_frames;
581 }
582}
583
Yaowu Xuf883b422016-08-30 14:01:10 -0700584static aom_codec_err_t decoder_decode(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585 const uint8_t *data, unsigned int data_sz,
586 void *user_priv, long deadline) {
587 const uint8_t *data_start = data;
588 const uint8_t *const data_end = data + data_sz;
Yaowu Xuf883b422016-08-30 14:01:10 -0700589 aom_codec_err_t res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590 uint32_t frame_sizes[8];
591 int frame_count;
592
593 if (data == NULL && data_sz == 0) {
594 ctx->flushed = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700595 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 }
597
598 // Reset flushed when receiving a valid frame.
599 ctx->flushed = 0;
600
601 // Initialize the decoder workers on the first frame.
602 if (ctx->frame_workers == NULL) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700603 res = init_decoder(ctx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700604 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700605 }
606
Yaowu Xuf883b422016-08-30 14:01:10 -0700607 res = av1_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
608 ctx->decrypt_cb, ctx->decrypt_state);
609 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610
611 if (ctx->frame_parallel_decode) {
612 // Decode in frame parallel mode. When decoding in this mode, the frame
613 // passed to the decoder must be either a normal frame or a superframe with
614 // superframe index so the decoder could get each frame's start position
615 // in the superframe.
616 if (frame_count > 0) {
617 int i;
618
619 for (i = 0; i < frame_count; ++i) {
620 const uint8_t *data_start_copy = data_start;
621 const uint32_t frame_size = frame_sizes[i];
622 if (data_start < data ||
623 frame_size > (uint32_t)(data_end - data_start)) {
624 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700625 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626 }
627
628 if (ctx->available_threads == 0) {
629 // No more threads for decoding. Wait until the next output worker
630 // finishes decoding. Then copy the decoded frame into cache.
631 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
632 wait_worker_and_cache_frame(ctx);
633 } else {
634 // TODO(hkuang): Add unit test to test this path.
635 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700636 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 }
638 }
639
640 res =
641 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700642 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700643 data_start += frame_size;
644 }
645 } else {
646 if (ctx->available_threads == 0) {
647 // No more threads for decoding. Wait until the next output worker
648 // finishes decoding. Then copy the decoded frame into cache.
649 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
650 wait_worker_and_cache_frame(ctx);
651 } else {
652 // TODO(hkuang): Add unit test to test this path.
653 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 }
656 }
657
658 res = decode_one(ctx, &data, data_sz, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700659 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660 }
661 } else {
662 // Decode in serial mode.
663 if (frame_count > 0) {
664 int i;
665
666 for (i = 0; i < frame_count; ++i) {
667 const uint8_t *data_start_copy = data_start;
668 const uint32_t frame_size = frame_sizes[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700669 if (data_start < data ||
670 frame_size > (uint32_t)(data_end - data_start)) {
671 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673 }
674
675 res =
676 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700677 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700678
679 data_start += frame_size;
680 }
681 } else {
682 while (data_start < data_end) {
683 const uint32_t frame_size = (uint32_t)(data_end - data_start);
Urvang Joshi454280d2016-10-14 16:51:44 -0700684 res = decode_one(ctx, &data_start, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700685 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686
687 // Account for suboptimal termination by the encoder.
688 while (data_start < data_end) {
689 const uint8_t marker =
690 read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
691 if (marker) break;
692 ++data_start;
693 }
694 }
695 }
696 }
697
698 return res;
699}
700
Yaowu Xuf883b422016-08-30 14:01:10 -0700701static void release_last_output_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
703 // Decrease reference count of last output frame in frame parallel mode.
704 if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
705 BufferPool *const pool = ctx->buffer_pool;
706 lock_buffer_pool(pool);
707 decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
708 unlock_buffer_pool(pool);
709 }
710}
711
Yaowu Xuf883b422016-08-30 14:01:10 -0700712static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
713 aom_codec_iter_t *iter) {
714 aom_image_t *img = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700715
716 // Only return frame when all the cpu are busy or
717 // application fluhsed the decoder in frame parallel decode.
718 if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
719 !ctx->flushed) {
720 return NULL;
721 }
722
723 // Output the frames in the cache first.
724 if (ctx->num_cache_frames > 0) {
725 release_last_output_frame(ctx);
726 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
727 if (ctx->need_resync) return NULL;
728 img = &ctx->frame_cache[ctx->frame_cache_read].img;
729 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
730 --ctx->num_cache_frames;
731 return img;
732 }
733
734 // iter acts as a flip flop, so an image is only returned on the first
735 // call to get_frame.
736 if (*iter == NULL && ctx->frame_workers != NULL) {
737 do {
738 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
740 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 FrameWorkerData *const frame_worker_data =
742 (FrameWorkerData *)worker->data1;
743 ctx->next_output_worker_id =
744 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
745 // Wait for the frame from worker thread.
746 if (winterface->sync(worker)) {
747 // Check if worker has received any frames.
748 if (frame_worker_data->received_frame == 1) {
749 ++ctx->available_threads;
750 frame_worker_data->received_frame = 0;
751 check_resync(ctx, frame_worker_data->pbi);
752 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700753 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
754 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700755 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
756 release_last_output_frame(ctx);
757 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
758 if (ctx->need_resync) return NULL;
759 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
760
761#if CONFIG_EXT_TILE
762 if (frame_worker_data->pbi->dec_tile_row >= 0) {
763 const int tile_row =
Yaowu Xuf883b422016-08-30 14:01:10 -0700764 AOMMIN(frame_worker_data->pbi->dec_tile_row, cm->tile_rows - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 const int mi_row = tile_row * cm->tile_height;
766 const int ssy = ctx->img.y_chroma_shift;
767 int plane;
768 ctx->img.planes[0] += mi_row * MI_SIZE * ctx->img.stride[0];
769 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
770 ctx->img.planes[plane] +=
771 mi_row * (MI_SIZE >> ssy) * ctx->img.stride[plane];
772 }
773 ctx->img.d_h =
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 AOMMIN(cm->tile_height, cm->mi_rows - mi_row) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775 }
776
777 if (frame_worker_data->pbi->dec_tile_col >= 0) {
778 const int tile_col =
Yaowu Xuf883b422016-08-30 14:01:10 -0700779 AOMMIN(frame_worker_data->pbi->dec_tile_col, cm->tile_cols - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 const int mi_col = tile_col * cm->tile_width;
781 const int ssx = ctx->img.x_chroma_shift;
782 int plane;
783 ctx->img.planes[0] += mi_col * MI_SIZE;
784 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
785 ctx->img.planes[plane] += mi_col * (MI_SIZE >> ssx);
786 }
787 ctx->img.d_w =
Yaowu Xuf883b422016-08-30 14:01:10 -0700788 AOMMIN(cm->tile_width, cm->mi_cols - mi_col) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700789 }
790#endif // CONFIG_EXT_TILE
791
792 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
793 img = &ctx->img;
794 return img;
795 }
796 } else {
797 // Decoding failed. Release the worker thread.
798 frame_worker_data->received_frame = 0;
799 ++ctx->available_threads;
800 ctx->need_resync = 1;
801 if (ctx->flushed != 1) return NULL;
802 }
803 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
804 }
805 return NULL;
806}
807
Yaowu Xuf883b422016-08-30 14:01:10 -0700808static aom_codec_err_t decoder_set_fb_fn(
809 aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
810 aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811 if (cb_get == NULL || cb_release == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700812 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813 } else if (ctx->frame_workers == NULL) {
814 // If the decoder has already been initialized, do not accept changes to
815 // the frame buffer functions.
816 ctx->get_ext_fb_cb = cb_get;
817 ctx->release_ext_fb_cb = cb_release;
818 ctx->ext_priv = cb_priv;
Yaowu Xuf883b422016-08-30 14:01:10 -0700819 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820 }
821
Yaowu Xuf883b422016-08-30 14:01:10 -0700822 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823}
824
Yaowu Xuf883b422016-08-30 14:01:10 -0700825static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700827 aom_ref_frame_t *const data = va_arg(args, aom_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828
829 // Only support this function in serial decode.
830 if (ctx->frame_parallel_decode) {
831 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 }
834
835 if (data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700836 aom_ref_frame_t *const frame = (aom_ref_frame_t *)data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700838 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
840 image2yuvconfig(&frame->img, &sd);
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 return av1_set_reference_dec(&frame_worker_data->pbi->common,
842 ref_frame_to_av1_reframe(frame->frame_type),
843 &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700844 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700845 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846 }
847}
848
Yaowu Xuf883b422016-08-30 14:01:10 -0700849static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700850 va_list args) {
Urvang Joshi77853e52016-07-15 12:47:01 -0700851 const aom_ref_frame_t *const frame = va_arg(args, aom_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852
853 // Only support this function in serial decode.
854 if (ctx->frame_parallel_decode) {
855 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857 }
858
Urvang Joshi77853e52016-07-15 12:47:01 -0700859 if (frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700861 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
863 image2yuvconfig(&frame->img, &sd);
Yaowu Xuf883b422016-08-30 14:01:10 -0700864 return av1_copy_reference_dec(frame_worker_data->pbi,
865 (AOM_REFFRAME)frame->frame_type, &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700867 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 }
869}
870
Yaowu Xuf883b422016-08-30 14:01:10 -0700871static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700873 av1_ref_frame_t *data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874
875 // Only support this function in serial decode.
876 if (ctx->frame_parallel_decode) {
877 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700878 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700879 }
880
881 if (data) {
882 YV12_BUFFER_CONFIG *fb;
Yaowu Xuf883b422016-08-30 14:01:10 -0700883 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700884 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
885 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700886 if (fb == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887 yuvconfig2image(&data->img, fb, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700888 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700890 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891 }
892}
893
Yaowu Xuf883b422016-08-30 14:01:10 -0700894static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700895 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 aom_image_t *new_img = va_arg(args, aom_image_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897
898 // Only support this function in serial decode.
899 if (ctx->frame_parallel_decode) {
900 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700901 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902 }
903
904 if (new_img) {
905 YV12_BUFFER_CONFIG new_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -0700906 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700907 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
908
Yaowu Xuf883b422016-08-30 14:01:10 -0700909 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700910 yuvconfig2image(new_img, &new_frame, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700911 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700912 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700913 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700914 }
915 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700916 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700917 }
918}
919
Yaowu Xuf883b422016-08-30 14:01:10 -0700920static aom_codec_err_t ctrl_set_postproc(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700921 va_list args) {
922 (void)ctx;
923 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700924 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700925}
926
Yaowu Xuf883b422016-08-30 14:01:10 -0700927static aom_codec_err_t ctrl_set_dbg_options(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928 va_list args) {
929 (void)ctx;
930 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700931 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700932}
933
Yaowu Xuf883b422016-08-30 14:01:10 -0700934static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935 va_list args) {
936 int *const update_info = va_arg(args, int *);
937
938 // Only support this function in serial decode.
939 if (ctx->frame_parallel_decode) {
940 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700941 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942 }
943
944 if (update_info) {
945 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700946 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700947 FrameWorkerData *const frame_worker_data =
948 (FrameWorkerData *)worker->data1;
949 *update_info = frame_worker_data->pbi->refresh_frame_flags;
Yaowu Xuf883b422016-08-30 14:01:10 -0700950 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700952 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700953 }
954 }
955
Yaowu Xuf883b422016-08-30 14:01:10 -0700956 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957}
958
Peter Boströma1f64322017-01-19 11:35:30 -0500959static aom_codec_err_t ctrl_get_last_quantizer(aom_codec_alg_priv_t *ctx,
960 va_list args) {
961 int *const arg = va_arg(args, int *);
962 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
963 *arg =
964 ((FrameWorkerData *)ctx->frame_workers[0].data1)->pbi->common.base_qindex;
965 return AOM_CODEC_OK;
966}
967
Yaowu Xuf883b422016-08-30 14:01:10 -0700968static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700969 va_list args) {
970 int *corrupted = va_arg(args, int *);
971
972 if (corrupted) {
973 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700974 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700975 FrameWorkerData *const frame_worker_data =
976 (FrameWorkerData *)worker->data1;
977 RefCntBuffer *const frame_bufs =
978 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
979 if (frame_worker_data->pbi->common.frame_to_show == NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -0700980 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700981 if (ctx->last_show_frame >= 0)
982 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
Yaowu Xuf883b422016-08-30 14:01:10 -0700983 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700984 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700985 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700986 }
987 }
988
Yaowu Xuf883b422016-08-30 14:01:10 -0700989 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700990}
991
Yaowu Xuf883b422016-08-30 14:01:10 -0700992static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700993 va_list args) {
994 int *const frame_size = va_arg(args, int *);
995
996 // Only support this function in serial decode.
997 if (ctx->frame_parallel_decode) {
998 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700999 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001000 }
1001
1002 if (frame_size) {
1003 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001004 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001005 FrameWorkerData *const frame_worker_data =
1006 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001007 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001008 frame_size[0] = cm->width;
1009 frame_size[1] = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001010 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001011 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001012 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001013 }
1014 }
1015
Yaowu Xuf883b422016-08-30 14:01:10 -07001016 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001017}
1018
Yaowu Xuf883b422016-08-30 14:01:10 -07001019static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001020 va_list args) {
1021 int *const render_size = va_arg(args, int *);
1022
1023 // Only support this function in serial decode.
1024 if (ctx->frame_parallel_decode) {
1025 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001026 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001027 }
1028
1029 if (render_size) {
1030 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001031 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001032 FrameWorkerData *const frame_worker_data =
1033 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001034 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001035 render_size[0] = cm->render_width;
1036 render_size[1] = cm->render_height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001037 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001038 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001039 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001040 }
1041 }
1042
Yaowu Xuf883b422016-08-30 14:01:10 -07001043 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001044}
1045
Yaowu Xuf883b422016-08-30 14:01:10 -07001046static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001047 va_list args) {
1048 unsigned int *const bit_depth = va_arg(args, unsigned int *);
Yaowu Xuf883b422016-08-30 14:01:10 -07001049 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001050
1051 if (bit_depth) {
1052 if (worker) {
1053 FrameWorkerData *const frame_worker_data =
1054 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001055 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001056 *bit_depth = cm->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -07001057 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001058 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001059 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001060 }
1061 }
1062
Yaowu Xuf883b422016-08-30 14:01:10 -07001063 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001064}
1065
Yaowu Xuf883b422016-08-30 14:01:10 -07001066static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001067 va_list args) {
1068 ctx->invert_tile_order = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001069 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001070}
1071
Yaowu Xuf883b422016-08-30 14:01:10 -07001072static aom_codec_err_t ctrl_set_decryptor(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001073 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001074 aom_decrypt_init *init = va_arg(args, aom_decrypt_init *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001075 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
1076 ctx->decrypt_state = init ? init->decrypt_state : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07001077 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001078}
1079
Yaowu Xuf883b422016-08-30 14:01:10 -07001080static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001081 va_list args) {
1082 const int legacy_byte_alignment = 0;
1083 const int min_byte_alignment = 32;
1084 const int max_byte_alignment = 1024;
1085 const int byte_alignment = va_arg(args, int);
1086
1087 if (byte_alignment != legacy_byte_alignment &&
1088 (byte_alignment < min_byte_alignment ||
1089 byte_alignment > max_byte_alignment ||
1090 (byte_alignment & (byte_alignment - 1)) != 0))
Yaowu Xuf883b422016-08-30 14:01:10 -07001091 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001092
1093 ctx->byte_alignment = byte_alignment;
1094 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001095 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001096 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1097 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1098 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001099 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001100}
1101
Yaowu Xuf883b422016-08-30 14:01:10 -07001102static aom_codec_err_t ctrl_set_skip_loop_filter(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001103 va_list args) {
1104 ctx->skip_loop_filter = va_arg(args, int);
1105
1106 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001107 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001108 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1109 frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
1110 }
1111
Yaowu Xuf883b422016-08-30 14:01:10 -07001112 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001113}
1114
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001115static aom_codec_err_t ctrl_get_accounting(aom_codec_alg_priv_t *ctx,
1116 va_list args) {
1117#if !CONFIG_ACCOUNTING
1118 (void)ctx;
1119 (void)args;
1120 return AOM_CODEC_INCAPABLE;
1121#else
1122 if (ctx->frame_workers) {
1123 AVxWorker *const worker = ctx->frame_workers;
1124 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1125 AV1Decoder *pbi = frame_worker_data->pbi;
1126 Accounting **acct = va_arg(args, Accounting **);
1127 *acct = &pbi->accounting;
1128 return AOM_CODEC_OK;
1129 }
1130 return AOM_CODEC_ERROR;
1131#endif
1132}
Yaowu Xuf883b422016-08-30 14:01:10 -07001133static aom_codec_err_t ctrl_set_decode_tile_row(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001134 va_list args) {
1135 ctx->decode_tile_row = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001136 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001137}
1138
Yaowu Xuf883b422016-08-30 14:01:10 -07001139static aom_codec_err_t ctrl_set_decode_tile_col(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001140 va_list args) {
1141 ctx->decode_tile_col = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001142 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001143}
1144
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001145static aom_codec_err_t ctrl_set_inspection_callback(aom_codec_alg_priv_t *ctx,
1146 va_list args) {
1147#if !CONFIG_INSPECTION
1148 (void)ctx;
1149 (void)args;
1150 return AOM_CODEC_INCAPABLE;
1151#else
1152 aom_inspect_init *init = va_arg(args, aom_inspect_init *);
1153 ctx->inspect_cb = init->inspect_cb;
1154 ctx->inspect_ctx = init->inspect_ctx;
1155 return AOM_CODEC_OK;
1156#endif
1157}
1158
Yaowu Xuf883b422016-08-30 14:01:10 -07001159static aom_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
1160 { AOM_COPY_REFERENCE, ctrl_copy_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001161
1162 // Setters
Yaowu Xuf883b422016-08-30 14:01:10 -07001163 { AOM_SET_REFERENCE, ctrl_set_reference },
1164 { AOM_SET_POSTPROC, ctrl_set_postproc },
1165 { AOM_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options },
1166 { AOM_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options },
1167 { AOM_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options },
1168 { AOM_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options },
1169 { AV1_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
1170 { AOMD_SET_DECRYPTOR, ctrl_set_decryptor },
1171 { AV1_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
1172 { AV1_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
1173 { AV1_SET_DECODE_TILE_ROW, ctrl_set_decode_tile_row },
1174 { AV1_SET_DECODE_TILE_COL, ctrl_set_decode_tile_col },
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001175 { AV1_SET_INSPECTION_CALLBACK, ctrl_set_inspection_callback },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001176
1177 // Getters
Yaowu Xuf883b422016-08-30 14:01:10 -07001178 { AOMD_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
Peter Boströma1f64322017-01-19 11:35:30 -05001179 { AOMD_GET_LAST_QUANTIZER, ctrl_get_last_quantizer },
1180 { AOMD_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
Yaowu Xuf883b422016-08-30 14:01:10 -07001181 { AV1D_GET_BIT_DEPTH, ctrl_get_bit_depth },
Peter Boströma1f64322017-01-19 11:35:30 -05001182 { AV1D_GET_DISPLAY_SIZE, ctrl_get_render_size },
Yaowu Xuf883b422016-08-30 14:01:10 -07001183 { AV1D_GET_FRAME_SIZE, ctrl_get_frame_size },
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001184 { AV1_GET_ACCOUNTING, ctrl_get_accounting },
Yaowu Xuf883b422016-08-30 14:01:10 -07001185 { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
Peter Boströma1f64322017-01-19 11:35:30 -05001186 { AV1_GET_REFERENCE, ctrl_get_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001187
1188 { -1, NULL },
1189};
1190
1191#ifndef VERSION_STRING
1192#define VERSION_STRING
1193#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001194CODEC_INTERFACE(aom_codec_av1_dx) = {
1195 "AOMedia Project AV1 Decoder" VERSION_STRING,
1196 AOM_CODEC_INTERNAL_ABI_VERSION,
1197 AOM_CODEC_CAP_DECODER |
1198 AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // aom_codec_caps_t
1199 decoder_init, // aom_codec_init_fn_t
1200 decoder_destroy, // aom_codec_destroy_fn_t
1201 decoder_ctrl_maps, // aom_codec_ctrl_fn_map_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001202 {
1203 // NOLINT
Yaowu Xuf883b422016-08-30 14:01:10 -07001204 decoder_peek_si, // aom_codec_peek_si_fn_t
1205 decoder_get_si, // aom_codec_get_si_fn_t
1206 decoder_decode, // aom_codec_decode_fn_t
1207 decoder_get_frame, // aom_codec_frame_get_fn_t
1208 decoder_set_fb_fn, // aom_codec_set_fb_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001209 },
1210 {
1211 // NOLINT
1212 0,
Yaowu Xuf883b422016-08-30 14:01:10 -07001213 NULL, // aom_codec_enc_cfg_map_t
1214 NULL, // aom_codec_encode_fn_t
1215 NULL, // aom_codec_get_cx_data_fn_t
1216 NULL, // aom_codec_enc_config_set_fn_t
1217 NULL, // aom_codec_get_global_headers_fn_t
1218 NULL, // aom_codec_get_preview_frame_fn_t
1219 NULL // aom_codec_enc_mr_get_mem_loc_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220 }
1221};