blob: b0499f599d19c5280dab01625ee37b9ec090c1da [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;
Yaowu Xuc27fc142016-08-22 16:08:15 -070083};
84
Yaowu Xuf883b422016-08-30 14:01:10 -070085static aom_codec_err_t decoder_init(aom_codec_ctx_t *ctx,
86 aom_codec_priv_enc_mr_cfg_t *data) {
87 // This function only allocates space for the aom_codec_alg_priv_t
Yaowu Xuc27fc142016-08-22 16:08:15 -070088 // structure. More memory may be required at the time the stream
89 // information becomes known.
90 (void)data;
91
92 if (!ctx->priv) {
Yaowu Xuf883b422016-08-30 14:01:10 -070093 aom_codec_alg_priv_t *const priv =
94 (aom_codec_alg_priv_t *)aom_calloc(1, sizeof(*priv));
95 if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -070096
Yaowu Xuf883b422016-08-30 14:01:10 -070097 ctx->priv = (aom_codec_priv_t *)priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070098 ctx->priv->init_flags = ctx->init_flags;
99 priv->si.sz = sizeof(priv->si);
100 priv->flushed = 0;
101 // Only do frame parallel decode when threads > 1.
102 priv->frame_parallel_decode =
103 (ctx->config.dec && (ctx->config.dec->threads > 1) &&
Yaowu Xuf883b422016-08-30 14:01:10 -0700104 (ctx->init_flags & AOM_CODEC_USE_FRAME_THREADING))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105 ? 1
106 : 0;
107 if (ctx->config.dec) {
108 priv->cfg = *ctx->config.dec;
109 ctx->config.dec = &priv->cfg;
110 }
111 }
112
Yaowu Xuf883b422016-08-30 14:01:10 -0700113 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114}
115
Yaowu Xuf883b422016-08-30 14:01:10 -0700116static aom_codec_err_t decoder_destroy(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700117 if (ctx->frame_workers != NULL) {
118 int i;
119 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700120 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121 FrameWorkerData *const frame_worker_data =
122 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700123 aom_get_worker_interface()->end(worker);
124 av1_remove_common(&frame_worker_data->pbi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700126 av1_free_restoration_buffers(&frame_worker_data->pbi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700127#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700128 av1_decoder_remove(frame_worker_data->pbi);
129 aom_free(frame_worker_data->scratch_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130#if CONFIG_MULTITHREAD
131 pthread_mutex_destroy(&frame_worker_data->stats_mutex);
132 pthread_cond_destroy(&frame_worker_data->stats_cond);
133#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700134 aom_free(frame_worker_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700135 }
136#if CONFIG_MULTITHREAD
137 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
138#endif
139 }
140
141 if (ctx->buffer_pool) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700142 av1_free_ref_frame_buffers(ctx->buffer_pool);
143 av1_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700144 }
145
Yaowu Xuf883b422016-08-30 14:01:10 -0700146 aom_free(ctx->frame_workers);
147 aom_free(ctx->buffer_pool);
148 aom_free(ctx);
149 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700150}
151
152static int parse_bitdepth_colorspace_sampling(BITSTREAM_PROFILE profile,
Yaowu Xuf883b422016-08-30 14:01:10 -0700153 struct aom_read_bit_buffer *rb) {
154 aom_color_space_t color_space;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700155 if (profile >= PROFILE_2) rb->bit_offset += 1; // Bit-depth 10 or 12.
Yaowu Xuf883b422016-08-30 14:01:10 -0700156 color_space = (aom_color_space_t)aom_rb_read_literal(rb, 3);
157 if (color_space != AOM_CS_SRGB) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700158 rb->bit_offset += 1; // [16,235] (including xvycc) vs [0,255] range.
159 if (profile == PROFILE_1 || profile == PROFILE_3) {
160 rb->bit_offset += 2; // subsampling x/y.
161 rb->bit_offset += 1; // unused.
162 }
163 } else {
164 if (profile == PROFILE_1 || profile == PROFILE_3) {
165 rb->bit_offset += 1; // unused
166 } else {
167 // RGB is only available in version 1.
168 return 0;
169 }
170 }
171 return 1;
172}
173
Yaowu Xuf883b422016-08-30 14:01:10 -0700174static aom_codec_err_t decoder_peek_si_internal(
175 const uint8_t *data, unsigned int data_sz, aom_codec_stream_info_t *si,
176 int *is_intra_only, aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177 int intra_only_flag = 0;
178 uint8_t clear_buffer[9];
179
Yaowu Xuf883b422016-08-30 14:01:10 -0700180 if (data + data_sz <= data) return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700181
182 si->is_kf = 0;
183 si->w = si->h = 0;
184
185 if (decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700186 data_sz = AOMMIN(sizeof(clear_buffer), data_sz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187 decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
188 data = clear_buffer;
189 }
190
191 {
192 int show_frame;
193 int error_resilient;
Yaowu Xuf883b422016-08-30 14:01:10 -0700194 struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
195 const int frame_marker = aom_rb_read_literal(&rb, 2);
196 const BITSTREAM_PROFILE profile = av1_read_profile(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197
Yaowu Xuf883b422016-08-30 14:01:10 -0700198 if (frame_marker != AOM_FRAME_MARKER) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199
Yaowu Xuf883b422016-08-30 14:01:10 -0700200 if (profile >= MAX_PROFILES) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700201
202 if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
Yaowu Xuf883b422016-08-30 14:01:10 -0700203 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204
Yaowu Xuf883b422016-08-30 14:01:10 -0700205 if (aom_rb_read_bit(&rb)) { // show an existing frame
206 aom_rb_read_literal(&rb, 3); // Frame buffer to show.
207 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700208 }
209
Yaowu Xuf883b422016-08-30 14:01:10 -0700210 if (data_sz <= 8) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211
Yaowu Xuf883b422016-08-30 14:01:10 -0700212 si->is_kf = !aom_rb_read_bit(&rb);
213 show_frame = aom_rb_read_bit(&rb);
214 error_resilient = aom_rb_read_bit(&rb);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100215#if CONFIG_REFERENCE_BUFFER
216 {
217 /* TODO: Move outside frame loop or inside key-frame branch */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +0100218 int frame_id_len;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100219 SequenceHeader seq_params;
220 read_sequence_header(&seq_params);
221 if (seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +0100222 frame_id_len = seq_params.frame_id_length_minus7 + 7;
223 aom_rb_read_literal(&rb, frame_id_len);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +0100224 }
225 }
226#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227 if (si->is_kf) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700228 if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700229
230 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700231 return AOM_CODEC_UNSUP_BITSTREAM;
232 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700233 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700234 intra_only_flag = show_frame ? 0 : aom_rb_read_bit(&rb);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235
236 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
237
238 if (intra_only_flag) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700239 if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700240 if (profile > PROFILE_0) {
241 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Yaowu Xuf883b422016-08-30 14:01:10 -0700242 return AOM_CODEC_UNSUP_BITSTREAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700243 }
244 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
Yaowu Xuf883b422016-08-30 14:01:10 -0700245 av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246 }
247 }
248 }
249 if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
Yaowu Xuf883b422016-08-30 14:01:10 -0700250 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251}
252
Yaowu Xuf883b422016-08-30 14:01:10 -0700253static aom_codec_err_t decoder_peek_si(const uint8_t *data,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254 unsigned int data_sz,
Yaowu Xuf883b422016-08-30 14:01:10 -0700255 aom_codec_stream_info_t *si) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256 return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
257}
258
Yaowu Xuf883b422016-08-30 14:01:10 -0700259static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
260 aom_codec_stream_info_t *si) {
261 const size_t sz = (si->sz >= sizeof(av1_stream_info_t))
262 ? sizeof(av1_stream_info_t)
263 : sizeof(aom_codec_stream_info_t);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264 memcpy(si, &ctx->si, sz);
265 si->sz = (unsigned int)sz;
266
Yaowu Xuf883b422016-08-30 14:01:10 -0700267 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700268}
269
Yaowu Xuf883b422016-08-30 14:01:10 -0700270static void set_error_detail(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700271 const char *const error) {
272 ctx->base.err_detail = error;
273}
274
Yaowu Xuf883b422016-08-30 14:01:10 -0700275static aom_codec_err_t update_error_state(
276 aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700277 if (error->error_code)
278 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
279
280 return error->error_code;
281}
282
Yaowu Xuf883b422016-08-30 14:01:10 -0700283static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284 int i;
285
286 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700287 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700289 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 BufferPool *const pool = cm->buffer_pool;
291
292 cm->new_fb_idx = INVALID_IDX;
293 cm->byte_alignment = ctx->byte_alignment;
294 cm->skip_loop_filter = ctx->skip_loop_filter;
295
296 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
297 pool->get_fb_cb = ctx->get_ext_fb_cb;
298 pool->release_fb_cb = ctx->release_ext_fb_cb;
299 pool->cb_priv = ctx->ext_priv;
300 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700301 pool->get_fb_cb = av1_get_frame_buffer;
302 pool->release_fb_cb = av1_release_frame_buffer;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303
Yaowu Xuf883b422016-08-30 14:01:10 -0700304 if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
305 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306 "Failed to initialize internal frame buffers");
307
308 pool->cb_priv = &pool->int_frame_buffers;
309 }
310 }
311}
312
Yaowu Xuf883b422016-08-30 14:01:10 -0700313static void set_default_ppflags(aom_postproc_cfg_t *cfg) {
314 cfg->post_proc_flag = AOM_DEBLOCK | AOM_DEMACROBLOCK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315 cfg->deblocking_level = 4;
316 cfg->noise_level = 0;
317}
318
319static int frame_worker_hook(void *arg1, void *arg2) {
320 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
321 const uint8_t *data = frame_worker_data->data;
322 (void)arg2;
323
Yaowu Xuf883b422016-08-30 14:01:10 -0700324 frame_worker_data->result = av1_receive_compressed_data(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700325 frame_worker_data->pbi, frame_worker_data->data_size, &data);
326 frame_worker_data->data_end = data;
327
328 if (frame_worker_data->pbi->common.frame_parallel_decode) {
329 // In frame parallel decoding, a worker thread must successfully decode all
330 // the compressed data.
331 if (frame_worker_data->result != 0 ||
332 frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700333 AVxWorker *const worker = frame_worker_data->pbi->frame_worker_owner;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700334 BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
335 // Signal all the other threads that are waiting for this frame.
Yaowu Xuf883b422016-08-30 14:01:10 -0700336 av1_frameworker_lock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700337 frame_worker_data->frame_context_ready = 1;
338 lock_buffer_pool(pool);
339 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
340 unlock_buffer_pool(pool);
341 frame_worker_data->pbi->need_resync = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700342 av1_frameworker_signal_stats(worker);
343 av1_frameworker_unlock_stats(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344 return 0;
345 }
346 } else if (frame_worker_data->result != 0) {
347 // Check decode result in serial decode.
348 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
349 frame_worker_data->pbi->need_resync = 1;
350 }
351 return !frame_worker_data->result;
352}
353
Yaowu Xuf883b422016-08-30 14:01:10 -0700354static aom_codec_err_t init_decoder(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700355 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700356 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700357
358 ctx->last_show_frame = -1;
359 ctx->next_submit_worker_id = 0;
360 ctx->last_submit_worker_id = 0;
361 ctx->next_output_worker_id = 0;
362 ctx->frame_cache_read = 0;
363 ctx->frame_cache_write = 0;
364 ctx->num_cache_frames = 0;
365 ctx->need_resync = 1;
366 ctx->num_frame_workers =
367 (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads : 1;
368 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
369 ctx->num_frame_workers = MAX_DECODE_THREADS;
370 ctx->available_threads = ctx->num_frame_workers;
371 ctx->flushed = 0;
372
Yaowu Xuf883b422016-08-30 14:01:10 -0700373 ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
374 if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700375
376#if CONFIG_MULTITHREAD
377 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
378 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700379 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700380 }
381#endif
382
Yaowu Xuf883b422016-08-30 14:01:10 -0700383 ctx->frame_workers = (AVxWorker *)aom_malloc(ctx->num_frame_workers *
Yaowu Xuc27fc142016-08-22 16:08:15 -0700384 sizeof(*ctx->frame_workers));
385 if (ctx->frame_workers == NULL) {
386 set_error_detail(ctx, "Failed to allocate frame_workers");
Yaowu Xuf883b422016-08-30 14:01:10 -0700387 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700388 }
389
390 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700391 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392 FrameWorkerData *frame_worker_data = NULL;
393 winterface->init(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -0700394 worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395 if (worker->data1 == NULL) {
396 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700397 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700398 }
399 frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700400 frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401 if (frame_worker_data->pbi == NULL) {
402 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700403 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404 }
405 frame_worker_data->pbi->frame_worker_owner = worker;
406 frame_worker_data->worker_id = i;
407 frame_worker_data->scratch_buffer = NULL;
408 frame_worker_data->scratch_buffer_size = 0;
409 frame_worker_data->frame_context_ready = 0;
410 frame_worker_data->received_frame = 0;
411#if CONFIG_MULTITHREAD
412 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
413 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700414 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700415 }
416
417 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
418 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
Yaowu Xuf883b422016-08-30 14:01:10 -0700419 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700420 }
421#endif
422 // If decoding in serial mode, FrameWorker thread could create tile worker
423 // thread or loopfilter thread.
424 frame_worker_data->pbi->max_threads =
425 (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
426
427 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
428 frame_worker_data->pbi->common.frame_parallel_decode =
429 ctx->frame_parallel_decode;
Yaowu Xuf883b422016-08-30 14:01:10 -0700430 worker->hook = (AVxWorkerHook)frame_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431 if (!winterface->reset(worker)) {
432 set_error_detail(ctx, "Frame Worker thread creation failed");
Yaowu Xuf883b422016-08-30 14:01:10 -0700433 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700434 }
435 }
436
437 // If postprocessing was enabled by the application and a
438 // configuration has not been provided, default it.
Yaowu Xuf883b422016-08-30 14:01:10 -0700439 if (!ctx->postproc_cfg_set && (ctx->base.init_flags & AOM_CODEC_USE_POSTPROC))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700440 set_default_ppflags(&ctx->postproc_cfg);
441
442 init_buffer_callbacks(ctx);
443
Yaowu Xuf883b422016-08-30 14:01:10 -0700444 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445}
446
Yaowu Xuf883b422016-08-30 14:01:10 -0700447static INLINE void check_resync(aom_codec_alg_priv_t *const ctx,
448 const AV1Decoder *const pbi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700449 // Clear resync flag if worker got a key frame or intra only frame.
450 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
451 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
452 ctx->need_resync = 0;
453}
454
Yaowu Xuf883b422016-08-30 14:01:10 -0700455static aom_codec_err_t decode_one(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700456 const uint8_t **data, unsigned int data_sz,
457 void *user_priv, int64_t deadline) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700458 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700459 (void)deadline;
460
461 // Determine the stream parameters. Note that we rely on peek_si to
462 // validate that we have a buffer that does not wrap around the top
463 // of the heap.
464 if (!ctx->si.h) {
465 int is_intra_only = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700466 const aom_codec_err_t res =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700467 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
468 ctx->decrypt_cb, ctx->decrypt_state);
Yaowu Xuf883b422016-08-30 14:01:10 -0700469 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700470
Yaowu Xuf883b422016-08-30 14:01:10 -0700471 if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472 }
473
474 if (!ctx->frame_parallel_decode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700475 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700476 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
477 frame_worker_data->data = *data;
478 frame_worker_data->data_size = data_sz;
479 frame_worker_data->user_priv = user_priv;
480 frame_worker_data->received_frame = 1;
481
482 // Set these even if already initialized. The caller may have changed the
483 // decrypt config between frames.
484 frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
485 frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;
486
487#if CONFIG_EXT_TILE
488 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
489 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
490#endif // CONFIG_EXT_TILE
491
492 worker->had_error = 0;
493 winterface->execute(worker);
494
495 // Update data pointer after decode.
496 *data = frame_worker_data->data_end;
497
498 if (worker->had_error)
499 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
500
501 check_resync(ctx, frame_worker_data->pbi);
502 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700503 AVxWorker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
505 // Copy context from last worker thread to next worker thread.
506 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
Yaowu Xuf883b422016-08-30 14:01:10 -0700507 av1_frameworker_copy_context(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508 &ctx->frame_workers[ctx->next_submit_worker_id],
509 &ctx->frame_workers[ctx->last_submit_worker_id]);
510
511 frame_worker_data->pbi->ready_for_new_data = 0;
512 // Copy the compressed data into worker's internal buffer.
513 // TODO(hkuang): Will all the workers allocate the same size
514 // as the size of the first intra frame be better? This will
515 // avoid too many deallocate and allocate.
516 if (frame_worker_data->scratch_buffer_size < data_sz) {
Alex Converse7f094f12017-02-23 17:29:40 -0800517 aom_free(frame_worker_data->scratch_buffer);
518 frame_worker_data->scratch_buffer = (uint8_t *)aom_malloc(data_sz);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700519 if (frame_worker_data->scratch_buffer == NULL) {
520 set_error_detail(ctx, "Failed to reallocate scratch buffer");
Yaowu Xuf883b422016-08-30 14:01:10 -0700521 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522 }
523 frame_worker_data->scratch_buffer_size = data_sz;
524 }
525 frame_worker_data->data_size = data_sz;
526 memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
527
528 frame_worker_data->frame_decoded = 0;
529 frame_worker_data->frame_context_ready = 0;
530 frame_worker_data->received_frame = 1;
531 frame_worker_data->data = frame_worker_data->scratch_buffer;
532 frame_worker_data->user_priv = user_priv;
533
534 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
535 ctx->last_submit_worker_id =
536 (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
537
538 ctx->next_submit_worker_id =
539 (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
540 --ctx->available_threads;
541 worker->had_error = 0;
542 winterface->launch(worker);
543 }
544
Yaowu Xuf883b422016-08-30 14:01:10 -0700545 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700546}
547
Yaowu Xuf883b422016-08-30 14:01:10 -0700548static void wait_worker_and_cache_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700550 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
551 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
553 ctx->next_output_worker_id =
554 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
555 // TODO(hkuang): Add worker error handling here.
556 winterface->sync(worker);
557 frame_worker_data->received_frame = 0;
558 ++ctx->available_threads;
559
560 check_resync(ctx, frame_worker_data->pbi);
561
Yaowu Xuf883b422016-08-30 14:01:10 -0700562 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
563 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
565 ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
566 yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
567 frame_worker_data->user_priv);
568 ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
569 frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
570 ctx->frame_cache_write = (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
571 ++ctx->num_cache_frames;
572 }
573}
574
Yaowu Xuf883b422016-08-30 14:01:10 -0700575static aom_codec_err_t decoder_decode(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700576 const uint8_t *data, unsigned int data_sz,
577 void *user_priv, long deadline) {
578 const uint8_t *data_start = data;
579 const uint8_t *const data_end = data + data_sz;
Yaowu Xuf883b422016-08-30 14:01:10 -0700580 aom_codec_err_t res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581 uint32_t frame_sizes[8];
582 int frame_count;
583
584 if (data == NULL && data_sz == 0) {
585 ctx->flushed = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700586 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700587 }
588
589 // Reset flushed when receiving a valid frame.
590 ctx->flushed = 0;
591
592 // Initialize the decoder workers on the first frame.
593 if (ctx->frame_workers == NULL) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700594 res = init_decoder(ctx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700595 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596 }
597
Yaowu Xuf883b422016-08-30 14:01:10 -0700598 res = av1_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
599 ctx->decrypt_cb, ctx->decrypt_state);
600 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601
602 if (ctx->frame_parallel_decode) {
603 // Decode in frame parallel mode. When decoding in this mode, the frame
604 // passed to the decoder must be either a normal frame or a superframe with
605 // superframe index so the decoder could get each frame's start position
606 // in the superframe.
607 if (frame_count > 0) {
608 int i;
609
610 for (i = 0; i < frame_count; ++i) {
611 const uint8_t *data_start_copy = data_start;
612 const uint32_t frame_size = frame_sizes[i];
613 if (data_start < data ||
614 frame_size > (uint32_t)(data_end - data_start)) {
615 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700616 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700617 }
618
619 if (ctx->available_threads == 0) {
620 // No more threads for decoding. Wait until the next output worker
621 // finishes decoding. Then copy the decoded frame into cache.
622 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
623 wait_worker_and_cache_frame(ctx);
624 } else {
625 // TODO(hkuang): Add unit test to test this path.
626 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700627 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700628 }
629 }
630
631 res =
632 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700633 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634 data_start += frame_size;
635 }
636 } else {
637 if (ctx->available_threads == 0) {
638 // No more threads for decoding. Wait until the next output worker
639 // finishes decoding. Then copy the decoded frame into cache.
640 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
641 wait_worker_and_cache_frame(ctx);
642 } else {
643 // TODO(hkuang): Add unit test to test this path.
644 set_error_detail(ctx, "Frame output cache is full.");
Yaowu Xuf883b422016-08-30 14:01:10 -0700645 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700646 }
647 }
648
649 res = decode_one(ctx, &data, data_sz, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 }
652 } else {
653 // Decode in serial mode.
654 if (frame_count > 0) {
655 int i;
656
657 for (i = 0; i < frame_count; ++i) {
658 const uint8_t *data_start_copy = data_start;
659 const uint32_t frame_size = frame_sizes[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660 if (data_start < data ||
661 frame_size > (uint32_t)(data_end - data_start)) {
662 set_error_detail(ctx, "Invalid frame size in index");
Yaowu Xuf883b422016-08-30 14:01:10 -0700663 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 }
665
666 res =
667 decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700668 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700669
670 data_start += frame_size;
671 }
672 } else {
673 while (data_start < data_end) {
674 const uint32_t frame_size = (uint32_t)(data_end - data_start);
Urvang Joshi454280d2016-10-14 16:51:44 -0700675 res = decode_one(ctx, &data_start, frame_size, user_priv, deadline);
Yaowu Xuf883b422016-08-30 14:01:10 -0700676 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700677
678 // Account for suboptimal termination by the encoder.
679 while (data_start < data_end) {
680 const uint8_t marker =
681 read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
682 if (marker) break;
683 ++data_start;
684 }
685 }
686 }
687 }
688
689 return res;
690}
691
Yaowu Xuf883b422016-08-30 14:01:10 -0700692static void release_last_output_frame(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693 RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
694 // Decrease reference count of last output frame in frame parallel mode.
695 if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
696 BufferPool *const pool = ctx->buffer_pool;
697 lock_buffer_pool(pool);
698 decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
699 unlock_buffer_pool(pool);
700 }
701}
702
Yaowu Xuf883b422016-08-30 14:01:10 -0700703static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
704 aom_codec_iter_t *iter) {
705 aom_image_t *img = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706
707 // Only return frame when all the cpu are busy or
708 // application fluhsed the decoder in frame parallel decode.
709 if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
710 !ctx->flushed) {
711 return NULL;
712 }
713
714 // Output the frames in the cache first.
715 if (ctx->num_cache_frames > 0) {
716 release_last_output_frame(ctx);
717 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
718 if (ctx->need_resync) return NULL;
719 img = &ctx->frame_cache[ctx->frame_cache_read].img;
720 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
721 --ctx->num_cache_frames;
722 return img;
723 }
724
725 // iter acts as a flip flop, so an image is only returned on the first
726 // call to get_frame.
727 if (*iter == NULL && ctx->frame_workers != NULL) {
728 do {
729 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700730 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
731 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 FrameWorkerData *const frame_worker_data =
733 (FrameWorkerData *)worker->data1;
734 ctx->next_output_worker_id =
735 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
736 // Wait for the frame from worker thread.
737 if (winterface->sync(worker)) {
738 // Check if worker has received any frames.
739 if (frame_worker_data->received_frame == 1) {
740 ++ctx->available_threads;
741 frame_worker_data->received_frame = 0;
742 check_resync(ctx, frame_worker_data->pbi);
743 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
745 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700746 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
747 release_last_output_frame(ctx);
748 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
749 if (ctx->need_resync) return NULL;
750 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
751
752#if CONFIG_EXT_TILE
753 if (frame_worker_data->pbi->dec_tile_row >= 0) {
754 const int tile_row =
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 AOMMIN(frame_worker_data->pbi->dec_tile_row, cm->tile_rows - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756 const int mi_row = tile_row * cm->tile_height;
757 const int ssy = ctx->img.y_chroma_shift;
758 int plane;
759 ctx->img.planes[0] += mi_row * MI_SIZE * ctx->img.stride[0];
760 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
761 ctx->img.planes[plane] +=
762 mi_row * (MI_SIZE >> ssy) * ctx->img.stride[plane];
763 }
764 ctx->img.d_h =
Yaowu Xuf883b422016-08-30 14:01:10 -0700765 AOMMIN(cm->tile_height, cm->mi_rows - mi_row) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766 }
767
768 if (frame_worker_data->pbi->dec_tile_col >= 0) {
769 const int tile_col =
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 AOMMIN(frame_worker_data->pbi->dec_tile_col, cm->tile_cols - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 const int mi_col = tile_col * cm->tile_width;
772 const int ssx = ctx->img.x_chroma_shift;
773 int plane;
774 ctx->img.planes[0] += mi_col * MI_SIZE;
775 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
776 ctx->img.planes[plane] += mi_col * (MI_SIZE >> ssx);
777 }
778 ctx->img.d_w =
Yaowu Xuf883b422016-08-30 14:01:10 -0700779 AOMMIN(cm->tile_width, cm->mi_cols - mi_col) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 }
781#endif // CONFIG_EXT_TILE
782
783 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
784 img = &ctx->img;
785 return img;
786 }
787 } else {
788 // Decoding failed. Release the worker thread.
789 frame_worker_data->received_frame = 0;
790 ++ctx->available_threads;
791 ctx->need_resync = 1;
792 if (ctx->flushed != 1) return NULL;
793 }
794 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
795 }
796 return NULL;
797}
798
Yaowu Xuf883b422016-08-30 14:01:10 -0700799static aom_codec_err_t decoder_set_fb_fn(
800 aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
801 aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 if (cb_get == NULL || cb_release == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700803 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700804 } else if (ctx->frame_workers == NULL) {
805 // If the decoder has already been initialized, do not accept changes to
806 // the frame buffer functions.
807 ctx->get_ext_fb_cb = cb_get;
808 ctx->release_ext_fb_cb = cb_release;
809 ctx->ext_priv = cb_priv;
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811 }
812
Yaowu Xuf883b422016-08-30 14:01:10 -0700813 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814}
815
Yaowu Xuf883b422016-08-30 14:01:10 -0700816static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700818 aom_ref_frame_t *const data = va_arg(args, aom_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700819
820 // Only support this function in serial decode.
821 if (ctx->frame_parallel_decode) {
822 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700823 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824 }
825
826 if (data) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700827 aom_ref_frame_t *const frame = (aom_ref_frame_t *)data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700829 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700830 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
831 image2yuvconfig(&frame->img, &sd);
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 return av1_set_reference_dec(&frame_worker_data->pbi->common,
833 ref_frame_to_av1_reframe(frame->frame_type),
834 &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700836 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837 }
838}
839
Yaowu Xuf883b422016-08-30 14:01:10 -0700840static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700841 va_list args) {
Urvang Joshi77853e52016-07-15 12:47:01 -0700842 const aom_ref_frame_t *const frame = va_arg(args, aom_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843
844 // Only support this function in serial decode.
845 if (ctx->frame_parallel_decode) {
846 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848 }
849
Urvang Joshi77853e52016-07-15 12:47:01 -0700850 if (frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700852 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
854 image2yuvconfig(&frame->img, &sd);
Yaowu Xuf883b422016-08-30 14:01:10 -0700855 return av1_copy_reference_dec(frame_worker_data->pbi,
856 (AOM_REFFRAME)frame->frame_type, &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700858 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859 }
860}
861
Yaowu Xuf883b422016-08-30 14:01:10 -0700862static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700864 av1_ref_frame_t *data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865
866 // Only support this function in serial decode.
867 if (ctx->frame_parallel_decode) {
868 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700869 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870 }
871
872 if (data) {
873 YV12_BUFFER_CONFIG *fb;
Yaowu Xuf883b422016-08-30 14:01:10 -0700874 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
876 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 if (fb == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878 yuvconfig2image(&data->img, fb, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700879 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700881 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 }
883}
884
Yaowu Xuf883b422016-08-30 14:01:10 -0700885static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700886 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700887 aom_image_t *new_img = va_arg(args, aom_image_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888
889 // Only support this function in serial decode.
890 if (ctx->frame_parallel_decode) {
891 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700892 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 }
894
895 if (new_img) {
896 YV12_BUFFER_CONFIG new_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -0700897 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700898 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
899
Yaowu Xuf883b422016-08-30 14:01:10 -0700900 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700901 yuvconfig2image(new_img, &new_frame, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700902 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700903 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700904 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905 }
906 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700907 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908 }
909}
910
Yaowu Xuf883b422016-08-30 14:01:10 -0700911static aom_codec_err_t ctrl_set_postproc(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700912 va_list args) {
913 (void)ctx;
914 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700915 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916}
917
Yaowu Xuf883b422016-08-30 14:01:10 -0700918static aom_codec_err_t ctrl_set_dbg_options(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919 va_list args) {
920 (void)ctx;
921 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700922 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700923}
924
Yaowu Xuf883b422016-08-30 14:01:10 -0700925static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700926 va_list args) {
927 int *const update_info = va_arg(args, int *);
928
929 // Only support this function in serial decode.
930 if (ctx->frame_parallel_decode) {
931 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700932 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700933 }
934
935 if (update_info) {
936 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700937 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700938 FrameWorkerData *const frame_worker_data =
939 (FrameWorkerData *)worker->data1;
940 *update_info = frame_worker_data->pbi->refresh_frame_flags;
Yaowu Xuf883b422016-08-30 14:01:10 -0700941 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700943 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700944 }
945 }
946
Yaowu Xuf883b422016-08-30 14:01:10 -0700947 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700948}
949
Peter Boströma1f64322017-01-19 11:35:30 -0500950static aom_codec_err_t ctrl_get_last_quantizer(aom_codec_alg_priv_t *ctx,
951 va_list args) {
952 int *const arg = va_arg(args, int *);
953 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
954 *arg =
955 ((FrameWorkerData *)ctx->frame_workers[0].data1)->pbi->common.base_qindex;
956 return AOM_CODEC_OK;
957}
958
Yaowu Xuf883b422016-08-30 14:01:10 -0700959static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960 va_list args) {
961 int *corrupted = va_arg(args, int *);
962
963 if (corrupted) {
964 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700965 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700966 FrameWorkerData *const frame_worker_data =
967 (FrameWorkerData *)worker->data1;
968 RefCntBuffer *const frame_bufs =
969 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
970 if (frame_worker_data->pbi->common.frame_to_show == NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -0700971 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972 if (ctx->last_show_frame >= 0)
973 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
Yaowu Xuf883b422016-08-30 14:01:10 -0700974 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700975 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700976 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700977 }
978 }
979
Yaowu Xuf883b422016-08-30 14:01:10 -0700980 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700981}
982
Yaowu Xuf883b422016-08-30 14:01:10 -0700983static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700984 va_list args) {
985 int *const frame_size = va_arg(args, int *);
986
987 // Only support this function in serial decode.
988 if (ctx->frame_parallel_decode) {
989 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -0700990 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700991 }
992
993 if (frame_size) {
994 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700995 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700996 FrameWorkerData *const frame_worker_data =
997 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700998 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700999 frame_size[0] = cm->width;
1000 frame_size[1] = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001001 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001002 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001003 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004 }
1005 }
1006
Yaowu Xuf883b422016-08-30 14:01:10 -07001007 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001008}
1009
Yaowu Xuf883b422016-08-30 14:01:10 -07001010static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001011 va_list args) {
1012 int *const render_size = va_arg(args, int *);
1013
1014 // Only support this function in serial decode.
1015 if (ctx->frame_parallel_decode) {
1016 set_error_detail(ctx, "Not supported in frame parallel decode");
Yaowu Xuf883b422016-08-30 14:01:10 -07001017 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001018 }
1019
1020 if (render_size) {
1021 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001022 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001023 FrameWorkerData *const frame_worker_data =
1024 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001025 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001026 render_size[0] = cm->render_width;
1027 render_size[1] = cm->render_height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001028 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001029 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001030 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001031 }
1032 }
1033
Yaowu Xuf883b422016-08-30 14:01:10 -07001034 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001035}
1036
Yaowu Xuf883b422016-08-30 14:01:10 -07001037static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001038 va_list args) {
1039 unsigned int *const bit_depth = va_arg(args, unsigned int *);
Yaowu Xuf883b422016-08-30 14:01:10 -07001040 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001041
1042 if (bit_depth) {
1043 if (worker) {
1044 FrameWorkerData *const frame_worker_data =
1045 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001046 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001047 *bit_depth = cm->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -07001048 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001049 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001050 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001051 }
1052 }
1053
Yaowu Xuf883b422016-08-30 14:01:10 -07001054 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001055}
1056
Yaowu Xuf883b422016-08-30 14:01:10 -07001057static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001058 va_list args) {
1059 ctx->invert_tile_order = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001060 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001061}
1062
Yaowu Xuf883b422016-08-30 14:01:10 -07001063static aom_codec_err_t ctrl_set_decryptor(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001064 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001065 aom_decrypt_init *init = va_arg(args, aom_decrypt_init *);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001066 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
1067 ctx->decrypt_state = init ? init->decrypt_state : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -07001068 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001069}
1070
Yaowu Xuf883b422016-08-30 14:01:10 -07001071static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001072 va_list args) {
1073 const int legacy_byte_alignment = 0;
1074 const int min_byte_alignment = 32;
1075 const int max_byte_alignment = 1024;
1076 const int byte_alignment = va_arg(args, int);
1077
1078 if (byte_alignment != legacy_byte_alignment &&
1079 (byte_alignment < min_byte_alignment ||
1080 byte_alignment > max_byte_alignment ||
1081 (byte_alignment & (byte_alignment - 1)) != 0))
Yaowu Xuf883b422016-08-30 14:01:10 -07001082 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001083
1084 ctx->byte_alignment = byte_alignment;
1085 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001086 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001087 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1088 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1089 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001090 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001091}
1092
Yaowu Xuf883b422016-08-30 14:01:10 -07001093static aom_codec_err_t ctrl_set_skip_loop_filter(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001094 va_list args) {
1095 ctx->skip_loop_filter = va_arg(args, int);
1096
1097 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001098 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001099 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1100 frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
1101 }
1102
Yaowu Xuf883b422016-08-30 14:01:10 -07001103 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104}
1105
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001106static aom_codec_err_t ctrl_get_accounting(aom_codec_alg_priv_t *ctx,
1107 va_list args) {
1108#if !CONFIG_ACCOUNTING
1109 (void)ctx;
1110 (void)args;
1111 return AOM_CODEC_INCAPABLE;
1112#else
1113 if (ctx->frame_workers) {
1114 AVxWorker *const worker = ctx->frame_workers;
1115 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1116 AV1Decoder *pbi = frame_worker_data->pbi;
1117 Accounting **acct = va_arg(args, Accounting **);
1118 *acct = &pbi->accounting;
1119 return AOM_CODEC_OK;
1120 }
1121 return AOM_CODEC_ERROR;
1122#endif
1123}
Yaowu Xuf883b422016-08-30 14:01:10 -07001124static aom_codec_err_t ctrl_set_decode_tile_row(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001125 va_list args) {
1126 ctx->decode_tile_row = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001127 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001128}
1129
Yaowu Xuf883b422016-08-30 14:01:10 -07001130static aom_codec_err_t ctrl_set_decode_tile_col(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001131 va_list args) {
1132 ctx->decode_tile_col = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001133 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001134}
1135
Yaowu Xuf883b422016-08-30 14:01:10 -07001136static aom_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
1137 { AOM_COPY_REFERENCE, ctrl_copy_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001138
1139 // Setters
Yaowu Xuf883b422016-08-30 14:01:10 -07001140 { AOM_SET_REFERENCE, ctrl_set_reference },
1141 { AOM_SET_POSTPROC, ctrl_set_postproc },
1142 { AOM_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options },
1143 { AOM_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options },
1144 { AOM_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options },
1145 { AOM_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options },
1146 { AV1_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
1147 { AOMD_SET_DECRYPTOR, ctrl_set_decryptor },
1148 { AV1_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
1149 { AV1_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
1150 { AV1_SET_DECODE_TILE_ROW, ctrl_set_decode_tile_row },
1151 { AV1_SET_DECODE_TILE_COL, ctrl_set_decode_tile_col },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001152
1153 // Getters
Yaowu Xuf883b422016-08-30 14:01:10 -07001154 { AOMD_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
Peter Boströma1f64322017-01-19 11:35:30 -05001155 { AOMD_GET_LAST_QUANTIZER, ctrl_get_last_quantizer },
1156 { AOMD_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
Yaowu Xuf883b422016-08-30 14:01:10 -07001157 { AV1D_GET_BIT_DEPTH, ctrl_get_bit_depth },
Peter Boströma1f64322017-01-19 11:35:30 -05001158 { AV1D_GET_DISPLAY_SIZE, ctrl_get_render_size },
Yaowu Xuf883b422016-08-30 14:01:10 -07001159 { AV1D_GET_FRAME_SIZE, ctrl_get_frame_size },
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001160 { AV1_GET_ACCOUNTING, ctrl_get_accounting },
Yaowu Xuf883b422016-08-30 14:01:10 -07001161 { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
Peter Boströma1f64322017-01-19 11:35:30 -05001162 { AV1_GET_REFERENCE, ctrl_get_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001163
1164 { -1, NULL },
1165};
1166
1167#ifndef VERSION_STRING
1168#define VERSION_STRING
1169#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001170CODEC_INTERFACE(aom_codec_av1_dx) = {
1171 "AOMedia Project AV1 Decoder" VERSION_STRING,
1172 AOM_CODEC_INTERNAL_ABI_VERSION,
1173 AOM_CODEC_CAP_DECODER |
1174 AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // aom_codec_caps_t
1175 decoder_init, // aom_codec_init_fn_t
1176 decoder_destroy, // aom_codec_destroy_fn_t
1177 decoder_ctrl_maps, // aom_codec_ctrl_fn_map_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001178 {
1179 // NOLINT
Yaowu Xuf883b422016-08-30 14:01:10 -07001180 decoder_peek_si, // aom_codec_peek_si_fn_t
1181 decoder_get_si, // aom_codec_get_si_fn_t
1182 decoder_decode, // aom_codec_decode_fn_t
1183 decoder_get_frame, // aom_codec_frame_get_fn_t
1184 decoder_set_fb_fn, // aom_codec_set_fb_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001185 },
1186 {
1187 // NOLINT
1188 0,
Yaowu Xuf883b422016-08-30 14:01:10 -07001189 NULL, // aom_codec_enc_cfg_map_t
1190 NULL, // aom_codec_encode_fn_t
1191 NULL, // aom_codec_get_cx_data_fn_t
1192 NULL, // aom_codec_enc_config_set_fn_t
1193 NULL, // aom_codec_get_global_headers_fn_t
1194 NULL, // aom_codec_get_preview_frame_fn_t
1195 NULL // aom_codec_enc_mr_get_mem_loc_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001196 }
1197};