blob: 5829b0879d44cc30946189558d487c2f8106ee1d [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"
Soo-Chul Hanf8589862018-01-24 03:13:14 +000023#include "aom_ports/mem_ops.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070024#include "aom_util/aom_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025
26#include "av1/common/alloccommon.h"
27#include "av1/common/frame_buffers.h"
28#include "av1/common/enums.h"
29
30#include "av1/decoder/decoder.h"
31#include "av1/decoder/decodeframe.h"
Tom Finegan13ee28c2018-02-26 19:37:41 -080032#include "av1/decoder/obu.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070033
Yaowu Xuf883b422016-08-30 14:01:10 -070034#include "av1/av1_iface_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070035
Yaowu Xuc27fc142016-08-22 16:08:15 -070036// 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;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -080043 aom_film_grain_t film_grain_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -070044} cache_frame;
45
Yaowu Xuf883b422016-08-30 14:01:10 -070046struct aom_codec_alg_priv {
47 aom_codec_priv_t base;
48 aom_codec_dec_cfg_t cfg;
Ralph Gilesafe71d92017-05-03 13:18:57 -070049 aom_codec_stream_info_t si;
Yaowu Xuc27fc142016-08-22 16:08:15 -070050 int postproc_cfg_set;
Yaowu Xuf883b422016-08-30 14:01:10 -070051 aom_postproc_cfg_t postproc_cfg;
Yaowu Xuf883b422016-08-30 14:01:10 -070052 aom_image_t img;
Yaowu Xuc27fc142016-08-22 16:08:15 -070053 int img_avail;
54 int flushed;
55 int invert_tile_order;
56 int last_show_frame; // Index of last output frame.
57 int byte_alignment;
58 int skip_loop_filter;
59 int decode_tile_row;
60 int decode_tile_col;
Yunqing Wang8ae64a92018-01-12 12:26:44 -080061 unsigned int tile_mode;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -040062 unsigned int is_annexb;
Frank Bossend8f457a2018-04-30 21:44:12 -040063 int operating_point;
Yaowu Xuc27fc142016-08-22 16:08:15 -070064
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];
Andrey Norkin6f1c2f72018-01-15 20:08:52 -080072 aom_image_t *image_with_grain;
Yaowu Xuc27fc142016-08-22 16:08:15 -070073 int frame_cache_write;
74 int frame_cache_read;
75 int num_cache_frames;
76 int need_resync; // wait for key/intra-only frame
77 // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
78 BufferPool *buffer_pool;
79
Yaowu Xuf883b422016-08-30 14:01:10 -070080 // External frame buffer info to save for AV1 common.
Yaowu Xuc27fc142016-08-22 16:08:15 -070081 void *ext_priv; // Private data associated with the external frame buffers.
Yaowu Xuf883b422016-08-30 14:01:10 -070082 aom_get_frame_buffer_cb_fn_t get_ext_fb_cb;
83 aom_release_frame_buffer_cb_fn_t release_ext_fb_cb;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050084
85#if CONFIG_INSPECTION
86 aom_inspect_cb inspect_cb;
87 void *inspect_ctx;
88#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070089};
90
Yaowu Xuf883b422016-08-30 14:01:10 -070091static aom_codec_err_t decoder_init(aom_codec_ctx_t *ctx,
92 aom_codec_priv_enc_mr_cfg_t *data) {
93 // This function only allocates space for the aom_codec_alg_priv_t
Yaowu Xuc27fc142016-08-22 16:08:15 -070094 // structure. More memory may be required at the time the stream
95 // information becomes known.
96 (void)data;
97
98 if (!ctx->priv) {
Yaowu Xuf883b422016-08-30 14:01:10 -070099 aom_codec_alg_priv_t *const priv =
100 (aom_codec_alg_priv_t *)aom_calloc(1, sizeof(*priv));
101 if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102
Yaowu Xuf883b422016-08-30 14:01:10 -0700103 ctx->priv = (aom_codec_priv_t *)priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700104 ctx->priv->init_flags = ctx->init_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105 priv->flushed = 0;
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800106
Thomas Daede85b49002017-07-10 17:43:06 -0700107 // TODO(tdaede): this should not be exposed to the API
108 priv->cfg.allow_lowbitdepth = CONFIG_LOWBITDEPTH;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700109 if (ctx->config.dec) {
110 priv->cfg = *ctx->config.dec;
111 ctx->config.dec = &priv->cfg;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100112 // default values
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -0800113 priv->cfg.cfg.ext_partition = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114 }
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800115 priv->image_with_grain = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700116 }
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);
Yaowu Xu568bf102017-10-17 12:43:27 -0700129 aom_free(frame_worker_data->pbi->common.tpl_mvs);
130 frame_worker_data->pbi->common.tpl_mvs = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700131 av1_remove_common(&frame_worker_data->pbi->common);
Yaowu Xuf883b422016-08-30 14:01:10 -0700132 av1_free_restoration_buffers(&frame_worker_data->pbi->common);
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);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800153 if (ctx->image_with_grain) aom_img_free(ctx->image_with_grain);
Yaowu Xuf883b422016-08-30 14:01:10 -0700154 aom_free(ctx);
155 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700156}
157
Hui Sudf5b2912018-03-16 15:34:01 -0700158static size_t get_obu_length_field_size(const uint8_t *data, size_t data_sz) {
159 const size_t max_bytes = AOMMIN(sizeof(uint64_t), data_sz);
Vignesh Venkatasubramanian03391a22018-03-09 12:25:55 -0800160 size_t length_field_size = 1;
Hui Sudf5b2912018-03-16 15:34:01 -0700161 for (size_t i = 0; i < max_bytes && (data[i] & 0x80); ++i) {
Vignesh Venkatasubramanian03391a22018-03-09 12:25:55 -0800162 ++length_field_size;
163 }
164 return length_field_size;
165}
166
Tom Finegan7ecf8152018-04-26 11:48:47 -0700167static void parse_operating_points(struct aom_read_bit_buffer *rb,
168 int is_reduced_header,
Soo-Chul Han4ea3c5e2018-04-06 10:18:32 -0400169 aom_codec_stream_info_t *si) {
Tom Finegan7ecf8152018-04-26 11:48:47 -0700170 if (is_reduced_header) {
Debargha Mukherjeeacd41f92018-04-11 07:58:34 -0700171 aom_rb_read_literal(rb, LEVEL_BITS); // level
172 } else {
Wan-Teh Chang69582972018-05-15 13:18:52 -0700173 const uint8_t operating_points_cnt_minus_1 =
174 aom_rb_read_literal(rb, OP_POINTS_CNT_MINUS_1_BITS);
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400175 int operating_point_idc0 = 0;
Wan-Teh Chang69582972018-05-15 13:18:52 -0700176 for (int i = 0; i < operating_points_cnt_minus_1 + 1; i++) {
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400177 int operating_point_idc;
178 operating_point_idc = aom_rb_read_literal(rb, OP_POINTS_IDC_BITS);
179 if (i == 0) operating_point_idc0 = operating_point_idc;
180 aom_rb_read_literal(rb, LEVEL_BITS); // level
Debargha Mukherjeeacd41f92018-04-11 07:58:34 -0700181#if !CONFIG_BUFFER_MODEL
182 if (aom_rb_read_literal(rb,
183 1)) { // decoder_rate_model_param_present_flag
184 aom_rb_read_literal(rb, 12); // decode_to_display_rate_ratio
185 aom_rb_read_literal(rb, 24); // initial_display_delay
186 aom_rb_read_literal(rb, 4); // extra_frame_buffers
187 }
188#endif // !CONFIG_BUFFER_MODEL
Debargha Mukherjee112c5822018-04-09 09:42:29 -0700189 }
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400190
191 // derive number of spatial/temporal layers from operating_point_idc0
192 if (operating_point_idc0 == 0) {
193 si->number_temporal_layers = 1;
194 si->number_spatial_layers = 1;
195 } else {
196 si->number_spatial_layers = 0;
197 si->number_temporal_layers = 0;
198 for (int j = 0; j < 8; j++) {
199 si->number_spatial_layers += (operating_point_idc0 >> (j + 8)) & 0x1;
200 si->number_temporal_layers += (operating_point_idc0 >> j) & 0x1;
201 }
202 }
Debargha Mukherjee112c5822018-04-09 09:42:29 -0700203 }
204}
205
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100206static aom_codec_err_t decoder_peek_si_internal(const uint8_t *data,
Tom Finegan21f48252018-05-15 11:31:04 -0700207 size_t data_sz,
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100208 aom_codec_stream_info_t *si,
209 int *is_intra_only) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210 int intra_only_flag = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211
Hui Sudf5b2912018-03-16 15:34:01 -0700212 if (data + data_sz <= data || data_sz < 1) return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213
Tom Fineganf277ca32018-02-26 12:09:52 -0800214 si->w = 0;
Tom Finegan13ee28c2018-02-26 19:37:41 -0800215 si->h = 0;
216 si->is_kf = 0;
217
218 // TODO(tomfinegan): This function needs Sequence and Frame Header OBUs to
219 // operate properly. At present it hard codes the values to 1 for the keyframe
220 // and intra only flags, and assumes the data being parsed is a Sequence
221 // Header OBU.
David Barker5c0e80e2018-04-17 12:54:29 +0100222 // NOTE(david.barker): In addition, this code currently requires the video
223 // stream to begin with an optional OBU_TEMPORAL_DELIMITER followed by an
224 // OBU_SEQUENCE_HEADER.
Tom Fineganf277ca32018-02-26 12:09:52 -0800225 intra_only_flag = 1;
Tom Finegan13ee28c2018-02-26 19:37:41 -0800226 si->is_kf = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227
David Barker5c0e80e2018-04-17 12:54:29 +0100228 ObuHeader obu_header;
229 memset(&obu_header, 0, sizeof(obu_header));
230 size_t payload_size = 0;
231 size_t bytes_read = 0;
232 aom_codec_err_t status = aom_read_obu_header_and_size(
233 data, data_sz, si->is_annexb, &obu_header, &payload_size, &bytes_read);
234 if (status != AOM_CODEC_OK) return status;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400235
David Barker5c0e80e2018-04-17 12:54:29 +0100236 // If the first OBU is a temporal delimiter, skip over it and look at the next
237 // OBU in the bitstream
238 if (obu_header.type == OBU_TEMPORAL_DELIMITER) {
239 // Skip any associated payload (there shouldn't be one, but just in case)
Wan-Teh Changbe668042018-04-24 11:58:21 -0700240 if (data_sz < bytes_read + payload_size) return AOM_CODEC_CORRUPT_FRAME;
David Barker5c0e80e2018-04-17 12:54:29 +0100241 data += bytes_read + payload_size;
Yaowu Xuf7ca9382018-04-29 19:43:32 -0700242 data_sz -= (uint32_t)(bytes_read + payload_size);
Tom Finegan13ee28c2018-02-26 19:37:41 -0800243
David Barker5c0e80e2018-04-17 12:54:29 +0100244 status = aom_read_obu_header_and_size(
245 data, data_sz, si->is_annexb, &obu_header, &payload_size, &bytes_read);
246 if (status != AOM_CODEC_OK) return status;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400247 }
Vignesh Venkatasubramanian03391a22018-03-09 12:25:55 -0800248
David Barker5c0e80e2018-04-17 12:54:29 +0100249 // Check that the selected OBU is a sequence header
250 if (obu_header.type != OBU_SEQUENCE_HEADER) return AOM_CODEC_UNSUP_BITSTREAM;
251
252 // Read a few values from the sequence header payload
253 data += bytes_read;
Yaowu Xuf7ca9382018-04-29 19:43:32 -0700254 data_sz -= (uint32_t)bytes_read;
David Barker5c0e80e2018-04-17 12:54:29 +0100255 struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
Tom Finegan13ee28c2018-02-26 19:37:41 -0800256
Debargha Mukherjee112c5822018-04-09 09:42:29 -0700257 av1_read_profile(&rb); // profile
258 const int still_picture = aom_rb_read_bit(&rb);
259 const int reduced_still_picture_hdr = aom_rb_read_bit(&rb);
260
261 if (!still_picture && reduced_still_picture_hdr) {
262 return AOM_CODEC_UNSUP_BITSTREAM;
263 }
264
Soo-Chul Han4ea3c5e2018-04-06 10:18:32 -0400265 parse_operating_points(&rb, reduced_still_picture_hdr, si);
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000266
Tom Fineganf277ca32018-02-26 12:09:52 -0800267 int num_bits_width = aom_rb_read_literal(&rb, 4) + 1;
268 int num_bits_height = aom_rb_read_literal(&rb, 4) + 1;
269 int max_frame_width = aom_rb_read_literal(&rb, num_bits_width) + 1;
270 int max_frame_height = aom_rb_read_literal(&rb, num_bits_height) + 1;
271 si->w = max_frame_width;
272 si->h = max_frame_height;
Tom Fineganf277ca32018-02-26 12:09:52 -0800273
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274 if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
Yaowu Xuf883b422016-08-30 14:01:10 -0700275 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276}
277
Tom Finegan21f48252018-05-15 11:31:04 -0700278static aom_codec_err_t decoder_peek_si(const uint8_t *data, size_t data_sz,
Yaowu Xuf883b422016-08-30 14:01:10 -0700279 aom_codec_stream_info_t *si) {
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100280 return decoder_peek_si_internal(data, data_sz, si, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281}
282
Yaowu Xuf883b422016-08-30 14:01:10 -0700283static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
284 aom_codec_stream_info_t *si) {
Ralph Gilesafe71d92017-05-03 13:18:57 -0700285 memcpy(si, &ctx->si, sizeof(*si));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286
Yaowu Xuf883b422016-08-30 14:01:10 -0700287 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288}
289
Yaowu Xuf883b422016-08-30 14:01:10 -0700290static void set_error_detail(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700291 const char *const error) {
292 ctx->base.err_detail = error;
293}
294
Yaowu Xuf883b422016-08-30 14:01:10 -0700295static aom_codec_err_t update_error_state(
296 aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297 if (error->error_code)
298 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
299
300 return error->error_code;
301}
302
Yaowu Xuf883b422016-08-30 14:01:10 -0700303static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 int i;
305
306 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700307 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700309 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310 BufferPool *const pool = cm->buffer_pool;
311
312 cm->new_fb_idx = INVALID_IDX;
313 cm->byte_alignment = ctx->byte_alignment;
314 cm->skip_loop_filter = ctx->skip_loop_filter;
315
316 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
317 pool->get_fb_cb = ctx->get_ext_fb_cb;
318 pool->release_fb_cb = ctx->release_ext_fb_cb;
319 pool->cb_priv = ctx->ext_priv;
320 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700321 pool->get_fb_cb = av1_get_frame_buffer;
322 pool->release_fb_cb = av1_release_frame_buffer;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323
Yaowu Xuf883b422016-08-30 14:01:10 -0700324 if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
325 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326 "Failed to initialize internal frame buffers");
327
328 pool->cb_priv = &pool->int_frame_buffers;
329 }
330 }
331}
332
Yaowu Xuf883b422016-08-30 14:01:10 -0700333static void set_default_ppflags(aom_postproc_cfg_t *cfg) {
334 cfg->post_proc_flag = AOM_DEBLOCK | AOM_DEMACROBLOCK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335 cfg->deblocking_level = 4;
336 cfg->noise_level = 0;
337}
338
339static int frame_worker_hook(void *arg1, void *arg2) {
340 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
341 const uint8_t *data = frame_worker_data->data;
342 (void)arg2;
343
Yaowu Xuf883b422016-08-30 14:01:10 -0700344 frame_worker_data->result = av1_receive_compressed_data(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345 frame_worker_data->pbi, frame_worker_data->data_size, &data);
346 frame_worker_data->data_end = data;
347
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800348 if (frame_worker_data->result != 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349 // Check decode result in serial decode.
350 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
351 frame_worker_data->pbi->need_resync = 1;
352 }
353 return !frame_worker_data->result;
354}
355
Yaowu Xuf883b422016-08-30 14:01:10 -0700356static aom_codec_err_t init_decoder(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700357 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -0700358 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700359
360 ctx->last_show_frame = -1;
361 ctx->next_submit_worker_id = 0;
362 ctx->last_submit_worker_id = 0;
363 ctx->next_output_worker_id = 0;
364 ctx->frame_cache_read = 0;
365 ctx->frame_cache_write = 0;
366 ctx->num_cache_frames = 0;
367 ctx->need_resync = 1;
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800368 ctx->num_frame_workers = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700369 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
370 ctx->num_frame_workers = MAX_DECODE_THREADS;
371 ctx->available_threads = ctx->num_frame_workers;
372 ctx->flushed = 0;
373
Yaowu Xuf883b422016-08-30 14:01:10 -0700374 ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
375 if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700376
377#if CONFIG_MULTITHREAD
378 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
379 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700380 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 }
382#endif
383
Yaowu Xuf883b422016-08-30 14:01:10 -0700384 ctx->frame_workers = (AVxWorker *)aom_malloc(ctx->num_frame_workers *
Yaowu Xuc27fc142016-08-22 16:08:15 -0700385 sizeof(*ctx->frame_workers));
386 if (ctx->frame_workers == NULL) {
387 set_error_detail(ctx, "Failed to allocate frame_workers");
Yaowu Xuf883b422016-08-30 14:01:10 -0700388 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389 }
390
391 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700392 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393 FrameWorkerData *frame_worker_data = NULL;
394 winterface->init(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -0700395 worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700396 if (worker->data1 == NULL) {
397 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700398 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700399 }
400 frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700401 frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402 if (frame_worker_data->pbi == NULL) {
403 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700404 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700405 }
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100406 frame_worker_data->pbi->common.options = &ctx->cfg.cfg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700407 frame_worker_data->pbi->frame_worker_owner = worker;
408 frame_worker_data->worker_id = i;
409 frame_worker_data->scratch_buffer = NULL;
410 frame_worker_data->scratch_buffer_size = 0;
411 frame_worker_data->frame_context_ready = 0;
412 frame_worker_data->received_frame = 0;
413#if CONFIG_MULTITHREAD
414 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
415 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700416 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417 }
418
419 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
420 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
Yaowu Xuf883b422016-08-30 14:01:10 -0700421 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700422 }
423#endif
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +0200424 frame_worker_data->pbi->allow_lowbitdepth = ctx->cfg.allow_lowbitdepth;
425
Yaowu Xuc27fc142016-08-22 16:08:15 -0700426 // If decoding in serial mode, FrameWorker thread could create tile worker
427 // thread or loopfilter thread.
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800428 frame_worker_data->pbi->max_threads = ctx->cfg.threads;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700429 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800430 frame_worker_data->pbi->common.large_scale_tile = ctx->tile_mode;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400431 frame_worker_data->pbi->common.is_annexb = ctx->is_annexb;
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800432 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
433 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
Frank Bossend8f457a2018-04-30 21:44:12 -0400434 frame_worker_data->pbi->operating_point = ctx->operating_point;
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,
Tom Finegan21f48252018-05-15 11:31:04 -0700461 const uint8_t **data, size_t data_sz,
Sean DuBois47cc2552018-01-23 07:44:16 +0000462 void *user_priv) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700463 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464
465 // Determine the stream parameters. Note that we rely on peek_si to
466 // validate that we have a buffer that does not wrap around the top
467 // of the heap.
468 if (!ctx->si.h) {
469 int is_intra_only = 0;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400470 ctx->si.is_annexb = ctx->is_annexb;
Tom Finegan21f48252018-05-15 11:31:04 -0700471 const aom_codec_err_t res =
472 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only);
Yaowu Xuf883b422016-08-30 14:01:10 -0700473 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474
Yaowu Xuf883b422016-08-30 14:01:10 -0700475 if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700476 }
477
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800478 AVxWorker *const worker = ctx->frame_workers;
479 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
480 frame_worker_data->data = *data;
481 frame_worker_data->data_size = data_sz;
482 frame_worker_data->user_priv = user_priv;
483 frame_worker_data->received_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484
Nathan E. Egge2cf03b12017-02-22 16:19:59 -0500485#if CONFIG_INSPECTION
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800486 frame_worker_data->pbi->inspect_cb = ctx->inspect_cb;
487 frame_worker_data->pbi->inspect_ctx = ctx->inspect_ctx;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -0500488#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700489
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800490 frame_worker_data->pbi->common.large_scale_tile = ctx->tile_mode;
491 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
492 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
Yunqing Wang2a5575e2018-02-23 16:02:18 -0800493
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400494 frame_worker_data->pbi->common.is_annexb = ctx->is_annexb;
495
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800496 worker->had_error = 0;
497 winterface->execute(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800499 // Update data pointer after decode.
500 *data = frame_worker_data->data_end;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800502 if (worker->had_error)
503 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800505 check_resync(ctx, frame_worker_data->pbi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506
Yaowu Xuf883b422016-08-30 14:01:10 -0700507 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508}
509
Yaowu Xuf883b422016-08-30 14:01:10 -0700510static aom_codec_err_t decoder_decode(aom_codec_alg_priv_t *ctx,
Tom Finegan21f48252018-05-15 11:31:04 -0700511 const uint8_t *data, size_t data_sz,
Sean DuBois47cc2552018-01-23 07:44:16 +0000512 void *user_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513 const uint8_t *data_start = data;
Wan-Teh Chang6e870902018-05-16 09:55:52 -0700514 const uint8_t *data_end = data + data_sz;
Yaowu Xub02d0b12017-12-15 01:32:34 +0000515 aom_codec_err_t res = AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700516
517 if (data == NULL && data_sz == 0) {
518 ctx->flushed = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700519 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520 }
521
522 // Reset flushed when receiving a valid frame.
523 ctx->flushed = 0;
524
525 // Initialize the decoder workers on the first frame.
526 if (ctx->frame_workers == NULL) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700527 res = init_decoder(ctx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700528 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529 }
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400530
Soo-Chul Han8acdbfc2018-04-02 12:55:33 -0400531 if (ctx->is_annexb) {
532 // read the size of this temporal unit
533 size_t length_of_size;
Wan-Teh Chang6e870902018-05-16 09:55:52 -0700534 uint64_t temporal_unit_size;
535 if (aom_uleb_decode(data_start, data_sz, &temporal_unit_size,
536 &length_of_size) != 0) {
Soo-Chul Han8acdbfc2018-04-02 12:55:33 -0400537 return AOM_CODEC_CORRUPT_FRAME;
538 }
539 data_start += length_of_size;
Wan-Teh Chang6e870902018-05-16 09:55:52 -0700540 if (temporal_unit_size > (size_t)(data_end - data_start))
541 return AOM_CODEC_CORRUPT_FRAME;
542 data_end = data_start + temporal_unit_size;
Soo-Chul Han8acdbfc2018-04-02 12:55:33 -0400543 }
544
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800545 // Decode in serial mode.
David Barker0bae60c2018-05-10 14:37:45 +0100546 while (data_start < data_end) {
547 uint64_t frame_size;
548 if (ctx->is_annexb) {
549 // read the size of this frame unit
550 size_t length_of_size;
551 if (aom_uleb_decode(data_start, (size_t)(data_end - data_start),
552 &frame_size, &length_of_size) != 0) {
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800553 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554 }
David Barker0bae60c2018-05-10 14:37:45 +0100555 data_start += length_of_size;
Wan-Teh Chang6e870902018-05-16 09:55:52 -0700556 if (frame_size > (size_t)(data_end - data_start))
557 return AOM_CODEC_CORRUPT_FRAME;
David Barker0bae60c2018-05-10 14:37:45 +0100558 } else {
559 frame_size = (uint64_t)(data_end - data_start);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700560 }
David Barker0bae60c2018-05-10 14:37:45 +0100561
Tom Finegan21f48252018-05-15 11:31:04 -0700562 if (frame_size > UINT32_MAX) return AOM_CODEC_CORRUPT_FRAME;
563
564 res = decode_one(ctx, &data_start, (size_t)frame_size, user_priv);
David Barker0bae60c2018-05-10 14:37:45 +0100565 if (res != AOM_CODEC_OK) return res;
566
567 // Allow extra zero bytes after the frame end
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800568 while (data_start < data_end) {
David Barker0bae60c2018-05-10 14:37:45 +0100569 const uint8_t marker = data_start[0];
570 if (marker) break;
571 ++data_start;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572 }
573 }
574
575 return res;
576}
577
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800578aom_image_t *add_grain_if_needed(aom_image_t *img, aom_image_t *grain_img_buf,
579 aom_film_grain_t *grain_params) {
580 if (!grain_params->apply_grain) return img;
581
582 if (grain_img_buf &&
583 (img->d_w != grain_img_buf->d_w || img->d_h != grain_img_buf->d_h ||
Andrey Norkin4352fed2018-03-01 17:01:49 -0800584 img->fmt != grain_img_buf->fmt || !(img->d_h % 2) || !(img->d_w % 2))) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800585 aom_img_free(grain_img_buf);
586 grain_img_buf = NULL;
587 }
588 if (!grain_img_buf) {
Andrey Norkin4352fed2018-03-01 17:01:49 -0800589 int w_even = img->d_w % 2 ? img->d_w + 1 : img->d_w;
590 int h_even = img->d_h % 2 ? img->d_h + 1 : img->d_h;
591 grain_img_buf = aom_img_alloc(NULL, img->fmt, w_even, h_even, 16);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800592 grain_img_buf->bit_depth = img->bit_depth;
593 }
594
595 av1_add_film_grain(grain_params, img, grain_img_buf);
596
597 return grain_img_buf;
598}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800599
Yaowu Xuf883b422016-08-30 14:01:10 -0700600static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
601 aom_codec_iter_t *iter) {
602 aom_image_t *img = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700603
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 // Output the frames in the cache first.
605 if (ctx->num_cache_frames > 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
607 if (ctx->need_resync) return NULL;
608 img = &ctx->frame_cache[ctx->frame_cache_read].img;
609 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
610 --ctx->num_cache_frames;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800611 return add_grain_if_needed(
612 img, ctx->image_with_grain,
613 &ctx->frame_cache[ctx->frame_cache_read].film_grain_params);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700614 }
615
616 // iter acts as a flip flop, so an image is only returned on the first
617 // call to get_frame.
618 if (*iter == NULL && ctx->frame_workers != NULL) {
619 do {
620 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700621 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
622 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623 FrameWorkerData *const frame_worker_data =
624 (FrameWorkerData *)worker->data1;
625 ctx->next_output_worker_id =
626 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
627 // Wait for the frame from worker thread.
628 if (winterface->sync(worker)) {
629 // Check if worker has received any frames.
630 if (frame_worker_data->received_frame == 1) {
631 ++ctx->available_threads;
632 frame_worker_data->received_frame = 0;
633 check_resync(ctx, frame_worker_data->pbi);
634 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700635 if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
636 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700638 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
639 if (ctx->need_resync) return NULL;
640 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
641
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000642 const int num_planes = av1_num_planes(cm);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700643 if (cm->single_tile_decoding &&
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800644 frame_worker_data->pbi->dec_tile_row >= 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700645 const int tile_row =
Yaowu Xuf883b422016-08-30 14:01:10 -0700646 AOMMIN(frame_worker_data->pbi->dec_tile_row, cm->tile_rows - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647 const int mi_row = tile_row * cm->tile_height;
648 const int ssy = ctx->img.y_chroma_shift;
649 int plane;
650 ctx->img.planes[0] += mi_row * MI_SIZE * ctx->img.stride[0];
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000651 if (num_planes > 1) {
652 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
653 ctx->img.planes[plane] +=
654 mi_row * (MI_SIZE >> ssy) * ctx->img.stride[plane];
655 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700656 }
657 ctx->img.d_h =
Yaowu Xuf883b422016-08-30 14:01:10 -0700658 AOMMIN(cm->tile_height, cm->mi_rows - mi_row) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700659 }
660
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700661 if (cm->single_tile_decoding &&
Yunqing Wangd8cd55f2017-02-27 12:16:00 -0800662 frame_worker_data->pbi->dec_tile_col >= 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700663 const int tile_col =
Yaowu Xuf883b422016-08-30 14:01:10 -0700664 AOMMIN(frame_worker_data->pbi->dec_tile_col, cm->tile_cols - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700665 const int mi_col = tile_col * cm->tile_width;
666 const int ssx = ctx->img.x_chroma_shift;
667 int plane;
668 ctx->img.planes[0] += mi_col * MI_SIZE;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000669 if (num_planes > 1) {
670 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
671 ctx->img.planes[plane] += mi_col * (MI_SIZE >> ssx);
672 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673 }
674 ctx->img.d_w =
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 AOMMIN(cm->tile_width, cm->mi_cols - mi_col) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700677
678 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
679 img = &ctx->img;
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000680 img->temporal_id = cm->temporal_layer_id;
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400681 img->spatial_id = cm->spatial_layer_id;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800682 return add_grain_if_needed(
683 img, ctx->image_with_grain,
684 &frame_worker_data->pbi->common.film_grain_params);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700685 }
686 } else {
687 // Decoding failed. Release the worker thread.
688 frame_worker_data->received_frame = 0;
689 ++ctx->available_threads;
690 ctx->need_resync = 1;
691 if (ctx->flushed != 1) return NULL;
692 }
693 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
694 }
695 return NULL;
696}
697
Yaowu Xuf883b422016-08-30 14:01:10 -0700698static aom_codec_err_t decoder_set_fb_fn(
699 aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
700 aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700701 if (cb_get == NULL || cb_release == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700702 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703 } else if (ctx->frame_workers == NULL) {
704 // If the decoder has already been initialized, do not accept changes to
705 // the frame buffer functions.
706 ctx->get_ext_fb_cb = cb_get;
707 ctx->release_ext_fb_cb = cb_release;
708 ctx->ext_priv = cb_priv;
Yaowu Xuf883b422016-08-30 14:01:10 -0700709 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 }
711
Yaowu Xuf883b422016-08-30 14:01:10 -0700712 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713}
714
Yaowu Xuf883b422016-08-30 14:01:10 -0700715static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716 va_list args) {
Thomas Daede497d1952017-08-08 17:33:06 -0700717 av1_ref_frame_t *const data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719 if (data) {
Thomas Daede497d1952017-08-08 17:33:06 -0700720 av1_ref_frame_t *const frame = data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700722 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
724 image2yuvconfig(&frame->img, &sd);
Thomas Daede497d1952017-08-08 17:33:06 -0700725 return av1_set_reference_dec(&frame_worker_data->pbi->common, frame->idx,
Yunqing Wang4450c762018-04-24 13:49:25 -0700726 frame->use_external_ref, &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700728 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729 }
730}
731
Yaowu Xuf883b422016-08-30 14:01:10 -0700732static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700733 va_list args) {
Thomas Daede497d1952017-08-08 17:33:06 -0700734 const av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
Urvang Joshi77853e52016-07-15 12:47:01 -0700735 if (frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700737 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
739 image2yuvconfig(&frame->img, &sd);
Thomas Daede497d1952017-08-08 17:33:06 -0700740 return av1_copy_reference_dec(frame_worker_data->pbi, frame->idx, &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700742 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 }
744}
745
Yaowu Xuf883b422016-08-30 14:01:10 -0700746static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700747 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700748 av1_ref_frame_t *data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749 if (data) {
750 YV12_BUFFER_CONFIG *fb;
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
753 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700754 if (fb == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700755 yuvconfig2image(&data->img, fb, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700756 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700757 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700758 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759 }
760}
761
Yaowu Xuf883b422016-08-30 14:01:10 -0700762static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700764 aom_image_t *new_img = va_arg(args, aom_image_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 if (new_img) {
766 YV12_BUFFER_CONFIG new_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -0700767 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
769
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 yuvconfig2image(new_img, &new_frame, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700772 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775 }
776 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700777 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 }
779}
780
Yunqing Wang20a336d2018-04-25 10:19:53 -0700781static aom_codec_err_t ctrl_copy_new_frame_image(aom_codec_alg_priv_t *ctx,
782 va_list args) {
783 aom_image_t *img = va_arg(args, aom_image_t *);
784 if (img) {
785 YV12_BUFFER_CONFIG new_frame;
786 AVxWorker *const worker = ctx->frame_workers;
787 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
788
789 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
790 YV12_BUFFER_CONFIG sd;
791 image2yuvconfig(img, &sd);
792 return av1_copy_new_frame_dec(&frame_worker_data->pbi->common, &new_frame,
793 &sd);
794 } else {
795 return AOM_CODEC_ERROR;
796 }
797 } else {
798 return AOM_CODEC_INVALID_PARAM;
799 }
800}
801
Yaowu Xuf883b422016-08-30 14:01:10 -0700802static aom_codec_err_t ctrl_set_postproc(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803 va_list args) {
804 (void)ctx;
805 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700806 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807}
808
Yaowu Xuf883b422016-08-30 14:01:10 -0700809static aom_codec_err_t ctrl_set_dbg_options(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810 va_list args) {
811 (void)ctx;
812 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700813 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814}
815
Yaowu Xuf883b422016-08-30 14:01:10 -0700816static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817 va_list args) {
818 int *const update_info = va_arg(args, int *);
819
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820 if (update_info) {
821 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700822 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823 FrameWorkerData *const frame_worker_data =
824 (FrameWorkerData *)worker->data1;
825 *update_info = frame_worker_data->pbi->refresh_frame_flags;
Yaowu Xuf883b422016-08-30 14:01:10 -0700826 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700827 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700828 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829 }
830 }
831
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833}
834
Peter Boströma1f64322017-01-19 11:35:30 -0500835static aom_codec_err_t ctrl_get_last_quantizer(aom_codec_alg_priv_t *ctx,
836 va_list args) {
837 int *const arg = va_arg(args, int *);
838 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
839 *arg =
840 ((FrameWorkerData *)ctx->frame_workers[0].data1)->pbi->common.base_qindex;
841 return AOM_CODEC_OK;
842}
843
Yaowu Xuf883b422016-08-30 14:01:10 -0700844static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845 va_list args) {
846 int *corrupted = va_arg(args, int *);
847
848 if (corrupted) {
849 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851 FrameWorkerData *const frame_worker_data =
852 (FrameWorkerData *)worker->data1;
853 RefCntBuffer *const frame_bufs =
854 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
855 if (frame_worker_data->pbi->common.frame_to_show == NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857 if (ctx->last_show_frame >= 0)
858 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
Yaowu Xuf883b422016-08-30 14:01:10 -0700859 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700861 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862 }
863 }
864
Yaowu Xuf883b422016-08-30 14:01:10 -0700865 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866}
867
Yaowu Xuf883b422016-08-30 14:01:10 -0700868static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700869 va_list args) {
870 int *const frame_size = va_arg(args, int *);
871
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872 if (frame_size) {
873 if (ctx->frame_workers) {
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 =
876 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878 frame_size[0] = cm->width;
879 frame_size[1] = cm->height;
Yaowu Xuf883b422016-08-30 14:01:10 -0700880 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700882 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883 }
884 }
885
Yaowu Xuf883b422016-08-30 14:01:10 -0700886 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887}
888
Yaowu Xuf883b422016-08-30 14:01:10 -0700889static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700890 va_list args) {
891 int *const render_size = va_arg(args, int *);
892
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893 if (render_size) {
894 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700895 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700896 FrameWorkerData *const frame_worker_data =
897 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700898 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 render_size[0] = cm->render_width;
900 render_size[1] = cm->render_height;
Yaowu Xuf883b422016-08-30 14:01:10 -0700901 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700903 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700904 }
905 }
906
Yaowu Xuf883b422016-08-30 14:01:10 -0700907 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908}
909
Yaowu Xuf883b422016-08-30 14:01:10 -0700910static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 va_list args) {
912 unsigned int *const bit_depth = va_arg(args, unsigned int *);
Yaowu Xuf883b422016-08-30 14:01:10 -0700913 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700914
915 if (bit_depth) {
916 if (worker) {
917 FrameWorkerData *const frame_worker_data =
918 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700919 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700920 *bit_depth = cm->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -0700921 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700923 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700924 }
925 }
926
Yaowu Xuf883b422016-08-30 14:01:10 -0700927 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928}
929
Yaowu Xuf883b422016-08-30 14:01:10 -0700930static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700931 va_list args) {
932 ctx->invert_tile_order = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -0700933 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934}
935
Yaowu Xuf883b422016-08-30 14:01:10 -0700936static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700937 va_list args) {
938 const int legacy_byte_alignment = 0;
939 const int min_byte_alignment = 32;
940 const int max_byte_alignment = 1024;
941 const int byte_alignment = va_arg(args, int);
942
943 if (byte_alignment != legacy_byte_alignment &&
944 (byte_alignment < min_byte_alignment ||
945 byte_alignment > max_byte_alignment ||
946 (byte_alignment & (byte_alignment - 1)) != 0))
Yaowu Xuf883b422016-08-30 14:01:10 -0700947 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700948
949 ctx->byte_alignment = byte_alignment;
950 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700951 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700952 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
953 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
954 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700955 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700956}
957
Yaowu Xuf883b422016-08-30 14:01:10 -0700958static aom_codec_err_t ctrl_set_skip_loop_filter(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700959 va_list args) {
960 ctx->skip_loop_filter = va_arg(args, int);
961
962 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700963 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700964 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
965 frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
966 }
967
Yaowu Xuf883b422016-08-30 14:01:10 -0700968 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700969}
970
Nathan E. Eggec9862e02016-10-05 21:28:36 -0400971static aom_codec_err_t ctrl_get_accounting(aom_codec_alg_priv_t *ctx,
972 va_list args) {
973#if !CONFIG_ACCOUNTING
974 (void)ctx;
975 (void)args;
976 return AOM_CODEC_INCAPABLE;
977#else
978 if (ctx->frame_workers) {
979 AVxWorker *const worker = ctx->frame_workers;
980 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
981 AV1Decoder *pbi = frame_worker_data->pbi;
982 Accounting **acct = va_arg(args, Accounting **);
983 *acct = &pbi->accounting;
984 return AOM_CODEC_OK;
985 }
986 return AOM_CODEC_ERROR;
987#endif
988}
Yaowu Xuf883b422016-08-30 14:01:10 -0700989static aom_codec_err_t ctrl_set_decode_tile_row(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700990 va_list args) {
991 ctx->decode_tile_row = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -0700992 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700993}
994
Yaowu Xuf883b422016-08-30 14:01:10 -0700995static aom_codec_err_t ctrl_set_decode_tile_col(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700996 va_list args) {
997 ctx->decode_tile_col = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -0700998 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700999}
1000
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001001static aom_codec_err_t ctrl_set_tile_mode(aom_codec_alg_priv_t *ctx,
1002 va_list args) {
1003 ctx->tile_mode = va_arg(args, unsigned int);
1004 return AOM_CODEC_OK;
1005}
1006
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04001007static aom_codec_err_t ctrl_set_is_annexb(aom_codec_alg_priv_t *ctx,
1008 va_list args) {
1009 ctx->is_annexb = va_arg(args, unsigned int);
1010 return AOM_CODEC_OK;
1011}
1012
Frank Bossend8f457a2018-04-30 21:44:12 -04001013static aom_codec_err_t ctrl_set_operating_point(aom_codec_alg_priv_t *ctx,
1014 va_list args) {
1015 ctx->operating_point = va_arg(args, int);
1016 return AOM_CODEC_OK;
1017}
1018
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001019static aom_codec_err_t ctrl_set_inspection_callback(aom_codec_alg_priv_t *ctx,
1020 va_list args) {
1021#if !CONFIG_INSPECTION
1022 (void)ctx;
1023 (void)args;
1024 return AOM_CODEC_INCAPABLE;
1025#else
1026 aom_inspect_init *init = va_arg(args, aom_inspect_init *);
1027 ctx->inspect_cb = init->inspect_cb;
1028 ctx->inspect_ctx = init->inspect_ctx;
1029 return AOM_CODEC_OK;
1030#endif
1031}
1032
Yaowu Xuf883b422016-08-30 14:01:10 -07001033static aom_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
Thomas Daede497d1952017-08-08 17:33:06 -07001034 { AV1_COPY_REFERENCE, ctrl_copy_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001035
1036 // Setters
Thomas Daede497d1952017-08-08 17:33:06 -07001037 { AV1_SET_REFERENCE, ctrl_set_reference },
Yaowu Xuf883b422016-08-30 14:01:10 -07001038 { AOM_SET_POSTPROC, ctrl_set_postproc },
1039 { AOM_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options },
1040 { AOM_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options },
1041 { AOM_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options },
1042 { AOM_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options },
1043 { AV1_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
Yaowu Xuf883b422016-08-30 14:01:10 -07001044 { AV1_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
1045 { AV1_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
1046 { AV1_SET_DECODE_TILE_ROW, ctrl_set_decode_tile_row },
1047 { AV1_SET_DECODE_TILE_COL, ctrl_set_decode_tile_col },
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001048 { AV1_SET_TILE_MODE, ctrl_set_tile_mode },
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04001049 { AV1D_SET_IS_ANNEXB, ctrl_set_is_annexb },
Frank Bossend8f457a2018-04-30 21:44:12 -04001050 { AV1D_SET_OPERATING_POINT, ctrl_set_operating_point },
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001051 { AV1_SET_INSPECTION_CALLBACK, ctrl_set_inspection_callback },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001052
1053 // Getters
Yaowu Xuf883b422016-08-30 14:01:10 -07001054 { AOMD_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
Peter Boströma1f64322017-01-19 11:35:30 -05001055 { AOMD_GET_LAST_QUANTIZER, ctrl_get_last_quantizer },
1056 { AOMD_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
Yaowu Xuf883b422016-08-30 14:01:10 -07001057 { AV1D_GET_BIT_DEPTH, ctrl_get_bit_depth },
Peter Boströma1f64322017-01-19 11:35:30 -05001058 { AV1D_GET_DISPLAY_SIZE, ctrl_get_render_size },
Yaowu Xuf883b422016-08-30 14:01:10 -07001059 { AV1D_GET_FRAME_SIZE, ctrl_get_frame_size },
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001060 { AV1_GET_ACCOUNTING, ctrl_get_accounting },
Yaowu Xuf883b422016-08-30 14:01:10 -07001061 { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
Yunqing Wang20a336d2018-04-25 10:19:53 -07001062 { AV1_COPY_NEW_FRAME_IMAGE, ctrl_copy_new_frame_image },
Peter Boströma1f64322017-01-19 11:35:30 -05001063 { AV1_GET_REFERENCE, ctrl_get_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001064
1065 { -1, NULL },
1066};
1067
1068#ifndef VERSION_STRING
1069#define VERSION_STRING
1070#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07001071CODEC_INTERFACE(aom_codec_av1_dx) = {
1072 "AOMedia Project AV1 Decoder" VERSION_STRING,
1073 AOM_CODEC_INTERNAL_ABI_VERSION,
1074 AOM_CODEC_CAP_DECODER |
1075 AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // aom_codec_caps_t
1076 decoder_init, // aom_codec_init_fn_t
1077 decoder_destroy, // aom_codec_destroy_fn_t
1078 decoder_ctrl_maps, // aom_codec_ctrl_fn_map_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001079 {
1080 // NOLINT
Yaowu Xuf883b422016-08-30 14:01:10 -07001081 decoder_peek_si, // aom_codec_peek_si_fn_t
1082 decoder_get_si, // aom_codec_get_si_fn_t
1083 decoder_decode, // aom_codec_decode_fn_t
1084 decoder_get_frame, // aom_codec_frame_get_fn_t
1085 decoder_set_fb_fn, // aom_codec_set_fb_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001086 },
1087 {
1088 // NOLINT
1089 0,
Yaowu Xuf883b422016-08-30 14:01:10 -07001090 NULL, // aom_codec_enc_cfg_map_t
1091 NULL, // aom_codec_encode_fn_t
1092 NULL, // aom_codec_get_cx_data_fn_t
1093 NULL, // aom_codec_enc_config_set_fn_t
1094 NULL, // aom_codec_get_global_headers_fn_t
1095 NULL, // aom_codec_get_preview_frame_fn_t
1096 NULL // aom_codec_enc_mr_get_mem_loc_fn_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07001097 }
1098};