blob: db338f7e3c441f1df611ce2ff8a5b745cd103a9e [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
Tom Finegan60e653d2018-05-22 11:34:58 -070015#include "config/aom_config.h"
Tom Finegan6791ad12018-05-24 14:30:59 -070016#include "config/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 Xuf883b422016-08-30 14:01:10 -070036struct aom_codec_alg_priv {
37 aom_codec_priv_t base;
38 aom_codec_dec_cfg_t cfg;
Ralph Gilesafe71d92017-05-03 13:18:57 -070039 aom_codec_stream_info_t si;
Yaowu Xuc27fc142016-08-22 16:08:15 -070040 int postproc_cfg_set;
Yaowu Xuf883b422016-08-30 14:01:10 -070041 aom_postproc_cfg_t postproc_cfg;
Yaowu Xuf883b422016-08-30 14:01:10 -070042 aom_image_t img;
Yaowu Xuc27fc142016-08-22 16:08:15 -070043 int img_avail;
44 int flushed;
45 int invert_tile_order;
46 int last_show_frame; // Index of last output frame.
47 int byte_alignment;
48 int skip_loop_filter;
49 int decode_tile_row;
50 int decode_tile_col;
Yunqing Wang8ae64a92018-01-12 12:26:44 -080051 unsigned int tile_mode;
Yunqing Wang93b18f32018-06-08 21:08:29 -070052 unsigned int ext_tile_debug;
53 EXTERNAL_REFERENCES ext_refs;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -040054 unsigned int is_annexb;
Frank Bossend8f457a2018-04-30 21:44:12 -040055 int operating_point;
David Barkerfb356012018-06-07 13:34:44 +010056 int output_all_layers;
Yaowu Xuc27fc142016-08-22 16:08:15 -070057
Yaowu Xuf883b422016-08-30 14:01:10 -070058 AVxWorker *frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -070059 int num_frame_workers;
60 int next_submit_worker_id;
61 int last_submit_worker_id;
62 int next_output_worker_id;
63 int available_threads;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -080064 aom_image_t *image_with_grain;
Yaowu Xuc27fc142016-08-22 16:08:15 -070065 int need_resync; // wait for key/intra-only frame
66 // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
67 BufferPool *buffer_pool;
68
Yaowu Xuf883b422016-08-30 14:01:10 -070069 // External frame buffer info to save for AV1 common.
Yaowu Xuc27fc142016-08-22 16:08:15 -070070 void *ext_priv; // Private data associated with the external frame buffers.
Yaowu Xuf883b422016-08-30 14:01:10 -070071 aom_get_frame_buffer_cb_fn_t get_ext_fb_cb;
72 aom_release_frame_buffer_cb_fn_t release_ext_fb_cb;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -050073
74#if CONFIG_INSPECTION
75 aom_inspect_cb inspect_cb;
76 void *inspect_ctx;
77#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070078};
79
Yaowu Xuf883b422016-08-30 14:01:10 -070080static aom_codec_err_t decoder_init(aom_codec_ctx_t *ctx,
81 aom_codec_priv_enc_mr_cfg_t *data) {
82 // This function only allocates space for the aom_codec_alg_priv_t
Yaowu Xuc27fc142016-08-22 16:08:15 -070083 // structure. More memory may be required at the time the stream
84 // information becomes known.
85 (void)data;
86
87 if (!ctx->priv) {
Yaowu Xuf883b422016-08-30 14:01:10 -070088 aom_codec_alg_priv_t *const priv =
89 (aom_codec_alg_priv_t *)aom_calloc(1, sizeof(*priv));
90 if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -070091
Yaowu Xuf883b422016-08-30 14:01:10 -070092 ctx->priv = (aom_codec_priv_t *)priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070093 ctx->priv->init_flags = ctx->init_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -070094 priv->flushed = 0;
Debargha Mukherjee5521a182018-03-06 12:29:01 -080095
Thomas Daede85b49002017-07-10 17:43:06 -070096 // TODO(tdaede): this should not be exposed to the API
97 priv->cfg.allow_lowbitdepth = CONFIG_LOWBITDEPTH;
Yaowu Xuc27fc142016-08-22 16:08:15 -070098 if (ctx->config.dec) {
99 priv->cfg = *ctx->config.dec;
100 ctx->config.dec = &priv->cfg;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100101 // default values
Debargha Mukherjee2ccf4b92018-02-27 17:30:46 -0800102 priv->cfg.cfg.ext_partition = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103 }
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800104 priv->image_with_grain = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700105 }
106
Yaowu Xuf883b422016-08-30 14:01:10 -0700107 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108}
109
Yaowu Xuf883b422016-08-30 14:01:10 -0700110static aom_codec_err_t decoder_destroy(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700111 if (ctx->frame_workers != NULL) {
112 int i;
113 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700114 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700115 FrameWorkerData *const frame_worker_data =
116 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700117 aom_get_worker_interface()->end(worker);
Yaowu Xu568bf102017-10-17 12:43:27 -0700118 aom_free(frame_worker_data->pbi->common.tpl_mvs);
119 frame_worker_data->pbi->common.tpl_mvs = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700120 av1_remove_common(&frame_worker_data->pbi->common);
Yaowu Xuf883b422016-08-30 14:01:10 -0700121 av1_free_restoration_buffers(&frame_worker_data->pbi->common);
Yaowu Xuf883b422016-08-30 14:01:10 -0700122 av1_decoder_remove(frame_worker_data->pbi);
123 aom_free(frame_worker_data->scratch_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700124#if CONFIG_MULTITHREAD
125 pthread_mutex_destroy(&frame_worker_data->stats_mutex);
126 pthread_cond_destroy(&frame_worker_data->stats_cond);
127#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700128 aom_free(frame_worker_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129 }
130#if CONFIG_MULTITHREAD
131 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
132#endif
133 }
134
135 if (ctx->buffer_pool) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700136 av1_free_ref_frame_buffers(ctx->buffer_pool);
137 av1_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138 }
139
Yaowu Xuf883b422016-08-30 14:01:10 -0700140 aom_free(ctx->frame_workers);
141 aom_free(ctx->buffer_pool);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800142 if (ctx->image_with_grain) aom_img_free(ctx->image_with_grain);
Yaowu Xuf883b422016-08-30 14:01:10 -0700143 aom_free(ctx);
144 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145}
146
Wan-Teh Chang6a26b0c2018-05-24 10:41:55 -0700147// Parses the operating points (including operating_point_idc, seq_level_idx,
148// and seq_tier) and then sets si->number_spatial_layers and
149// si->number_temporal_layers based on operating_point_idc[0].
Tom Finegan38df4dc2018-05-21 11:43:52 -0700150static aom_codec_err_t parse_operating_points(struct aom_read_bit_buffer *rb,
151 int is_reduced_header,
152 aom_codec_stream_info_t *si) {
Wan-Teh Chang6a26b0c2018-05-24 10:41:55 -0700153 int operating_point_idc0 = 0;
154
Tom Finegan7ecf8152018-04-26 11:48:47 -0700155 if (is_reduced_header) {
Debargha Mukherjeeacd41f92018-04-11 07:58:34 -0700156 aom_rb_read_literal(rb, LEVEL_BITS); // level
157 } else {
Wan-Teh Chang69582972018-05-15 13:18:52 -0700158 const uint8_t operating_points_cnt_minus_1 =
159 aom_rb_read_literal(rb, OP_POINTS_CNT_MINUS_1_BITS);
Wan-Teh Chang69582972018-05-15 13:18:52 -0700160 for (int i = 0; i < operating_points_cnt_minus_1 + 1; i++) {
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400161 int operating_point_idc;
162 operating_point_idc = aom_rb_read_literal(rb, OP_POINTS_IDC_BITS);
163 if (i == 0) operating_point_idc0 = operating_point_idc;
Andrey Norkinf481d982018-05-15 12:05:31 -0700164 int seq_level_idx = aom_rb_read_literal(rb, LEVEL_BITS); // level
165 if (seq_level_idx > 7) aom_rb_read_bit(rb); // tier
Debargha Mukherjee112c5822018-04-09 09:42:29 -0700166 }
Wan-Teh Chang6a26b0c2018-05-24 10:41:55 -0700167 }
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400168
Wan-Teh Chang6a26b0c2018-05-24 10:41:55 -0700169 if (aom_get_num_layers_from_operating_point_idc(
170 operating_point_idc0, &si->number_spatial_layers,
171 &si->number_temporal_layers) != AOM_CODEC_OK) {
172 return AOM_CODEC_ERROR;
Debargha Mukherjee112c5822018-04-09 09:42:29 -0700173 }
Tom Finegan38df4dc2018-05-21 11:43:52 -0700174
175 return AOM_CODEC_OK;
Debargha Mukherjee112c5822018-04-09 09:42:29 -0700176}
177
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100178static aom_codec_err_t decoder_peek_si_internal(const uint8_t *data,
Tom Finegan21f48252018-05-15 11:31:04 -0700179 size_t data_sz,
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100180 aom_codec_stream_info_t *si,
181 int *is_intra_only) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 int intra_only_flag = 0;
Thomas Daede1b319b22018-06-19 15:59:51 -0700183 int got_sequence_header = 0;
184 int found_keyframe = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700185
Hui Sudf5b2912018-03-16 15:34:01 -0700186 if (data + data_sz <= data || data_sz < 1) return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187
Tom Fineganf277ca32018-02-26 12:09:52 -0800188 si->w = 0;
Tom Finegan13ee28c2018-02-26 19:37:41 -0800189 si->h = 0;
Thomas Daede1b319b22018-06-19 15:59:51 -0700190 si->is_kf = 0; // is_kf indicates whether the current packet contains a RAP
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191
David Barker5c0e80e2018-04-17 12:54:29 +0100192 ObuHeader obu_header;
193 memset(&obu_header, 0, sizeof(obu_header));
194 size_t payload_size = 0;
195 size_t bytes_read = 0;
Thomas Daede1b319b22018-06-19 15:59:51 -0700196 int reduced_still_picture_hdr = 0;
David Barker5c0e80e2018-04-17 12:54:29 +0100197 aom_codec_err_t status = aom_read_obu_header_and_size(
198 data, data_sz, si->is_annexb, &obu_header, &payload_size, &bytes_read);
199 if (status != AOM_CODEC_OK) return status;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400200
David Barker5c0e80e2018-04-17 12:54:29 +0100201 // If the first OBU is a temporal delimiter, skip over it and look at the next
202 // OBU in the bitstream
203 if (obu_header.type == OBU_TEMPORAL_DELIMITER) {
204 // Skip any associated payload (there shouldn't be one, but just in case)
Wan-Teh Changbe668042018-04-24 11:58:21 -0700205 if (data_sz < bytes_read + payload_size) return AOM_CODEC_CORRUPT_FRAME;
David Barker5c0e80e2018-04-17 12:54:29 +0100206 data += bytes_read + payload_size;
Wan-Teh Chang84b22b82018-05-16 14:56:28 -0700207 data_sz -= bytes_read + payload_size;
Tom Finegan13ee28c2018-02-26 19:37:41 -0800208
David Barker5c0e80e2018-04-17 12:54:29 +0100209 status = aom_read_obu_header_and_size(
210 data, data_sz, si->is_annexb, &obu_header, &payload_size, &bytes_read);
211 if (status != AOM_CODEC_OK) return status;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400212 }
Thomas Daede1b319b22018-06-19 15:59:51 -0700213 while (1) {
214 data += bytes_read;
215 data_sz -= bytes_read;
216 const uint8_t *payload_start = data;
217 // Check that the selected OBU is a sequence header
218 if (obu_header.type == OBU_SEQUENCE_HEADER) {
219 // Sanity check on sequence header size
220 if (data_sz < 2) return AOM_CODEC_CORRUPT_FRAME;
221 // Read a few values from the sequence header payload
222 struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
Vignesh Venkatasubramanian03391a22018-03-09 12:25:55 -0800223
Thomas Daede1b319b22018-06-19 15:59:51 -0700224 av1_read_profile(&rb); // profile
225 const int still_picture = aom_rb_read_bit(&rb);
226 reduced_still_picture_hdr = aom_rb_read_bit(&rb);
David Barker5c0e80e2018-04-17 12:54:29 +0100227
Thomas Daede1b319b22018-06-19 15:59:51 -0700228 if (!still_picture && reduced_still_picture_hdr) {
229 return AOM_CODEC_UNSUP_BITSTREAM;
230 }
Tom Finegan13ee28c2018-02-26 19:37:41 -0800231
Thomas Daede1b319b22018-06-19 15:59:51 -0700232 if (parse_operating_points(&rb, reduced_still_picture_hdr, si) !=
233 AOM_CODEC_OK) {
234 return AOM_CODEC_ERROR;
235 }
Debargha Mukherjee112c5822018-04-09 09:42:29 -0700236
Thomas Daede1b319b22018-06-19 15:59:51 -0700237 int num_bits_width = aom_rb_read_literal(&rb, 4) + 1;
238 int num_bits_height = aom_rb_read_literal(&rb, 4) + 1;
239 int max_frame_width = aom_rb_read_literal(&rb, num_bits_width) + 1;
240 int max_frame_height = aom_rb_read_literal(&rb, num_bits_height) + 1;
241 si->w = max_frame_width;
242 si->h = max_frame_height;
243 got_sequence_header = 1;
244 } else if (obu_header.type == OBU_FRAME_HEADER ||
245 obu_header.type == OBU_FRAME) {
246 if (got_sequence_header && reduced_still_picture_hdr) {
247 found_keyframe = 1;
248 break;
249 } else {
250 // make sure we have enough bits to get the frame type out
251 if (data_sz < 1) return AOM_CODEC_CORRUPT_FRAME;
252 struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
253 const int show_existing_frame = aom_rb_read_bit(&rb);
254 if (!show_existing_frame) {
255 const FRAME_TYPE frame_type = (FRAME_TYPE)aom_rb_read_literal(&rb, 2);
256 if (frame_type == KEY_FRAME) {
257 found_keyframe = 1;
258 break; // Stop here as no further OBUs will change the outcome.
259 }
260 }
261 }
262 }
263 // skip past any unread OBU header data
264 data = payload_start + payload_size;
265 data_sz -= payload_size;
266 if (data_sz <= 0) break; // exit if we're out of OBUs
267 status = aom_read_obu_header_and_size(
268 data, data_sz, si->is_annexb, &obu_header, &payload_size, &bytes_read);
269 if (status != AOM_CODEC_OK) return status;
Debargha Mukherjee112c5822018-04-09 09:42:29 -0700270 }
Thomas Daede1b319b22018-06-19 15:59:51 -0700271 if (got_sequence_header && found_keyframe) si->is_kf = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700272 if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
Yaowu Xuf883b422016-08-30 14:01:10 -0700273 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700274}
275
Tom Finegan21f48252018-05-15 11:31:04 -0700276static aom_codec_err_t decoder_peek_si(const uint8_t *data, size_t data_sz,
Yaowu Xuf883b422016-08-30 14:01:10 -0700277 aom_codec_stream_info_t *si) {
Sebastien Alaiwan2b1ec182017-12-21 09:38:27 +0100278 return decoder_peek_si_internal(data, data_sz, si, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279}
280
Yaowu Xuf883b422016-08-30 14:01:10 -0700281static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
282 aom_codec_stream_info_t *si) {
Ralph Gilesafe71d92017-05-03 13:18:57 -0700283 memcpy(si, &ctx->si, sizeof(*si));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284
Yaowu Xuf883b422016-08-30 14:01:10 -0700285 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286}
287
Yaowu Xuf883b422016-08-30 14:01:10 -0700288static void set_error_detail(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 const char *const error) {
290 ctx->base.err_detail = error;
291}
292
Yaowu Xuf883b422016-08-30 14:01:10 -0700293static aom_codec_err_t update_error_state(
294 aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295 if (error->error_code)
296 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
297
298 return error->error_code;
299}
300
Yaowu Xuf883b422016-08-30 14:01:10 -0700301static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700302 int i;
303
304 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700305 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700307 AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308 BufferPool *const pool = cm->buffer_pool;
309
310 cm->new_fb_idx = INVALID_IDX;
311 cm->byte_alignment = ctx->byte_alignment;
312 cm->skip_loop_filter = ctx->skip_loop_filter;
313
314 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
315 pool->get_fb_cb = ctx->get_ext_fb_cb;
316 pool->release_fb_cb = ctx->release_ext_fb_cb;
317 pool->cb_priv = ctx->ext_priv;
318 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700319 pool->get_fb_cb = av1_get_frame_buffer;
320 pool->release_fb_cb = av1_release_frame_buffer;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700321
Yaowu Xuf883b422016-08-30 14:01:10 -0700322 if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
323 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324 "Failed to initialize internal frame buffers");
325
326 pool->cb_priv = &pool->int_frame_buffers;
327 }
328 }
329}
330
Yaowu Xuf883b422016-08-30 14:01:10 -0700331static void set_default_ppflags(aom_postproc_cfg_t *cfg) {
332 cfg->post_proc_flag = AOM_DEBLOCK | AOM_DEMACROBLOCK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333 cfg->deblocking_level = 4;
334 cfg->noise_level = 0;
335}
336
337static int frame_worker_hook(void *arg1, void *arg2) {
338 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
339 const uint8_t *data = frame_worker_data->data;
340 (void)arg2;
341
Yaowu Xuf883b422016-08-30 14:01:10 -0700342 frame_worker_data->result = av1_receive_compressed_data(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343 frame_worker_data->pbi, frame_worker_data->data_size, &data);
344 frame_worker_data->data_end = data;
345
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800346 if (frame_worker_data->result != 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347 // 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;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 ctx->need_resync = 1;
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800363 ctx->num_frame_workers = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
365 ctx->num_frame_workers = MAX_DECODE_THREADS;
366 ctx->available_threads = ctx->num_frame_workers;
367 ctx->flushed = 0;
368
Yaowu Xuf883b422016-08-30 14:01:10 -0700369 ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
370 if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700371
372#if CONFIG_MULTITHREAD
373 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
374 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700375 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700376 }
377#endif
378
Yaowu Xuf883b422016-08-30 14:01:10 -0700379 ctx->frame_workers = (AVxWorker *)aom_malloc(ctx->num_frame_workers *
Yaowu Xuc27fc142016-08-22 16:08:15 -0700380 sizeof(*ctx->frame_workers));
381 if (ctx->frame_workers == NULL) {
382 set_error_detail(ctx, "Failed to allocate frame_workers");
Yaowu Xuf883b422016-08-30 14:01:10 -0700383 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700384 }
385
386 for (i = 0; i < ctx->num_frame_workers; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700387 AVxWorker *const worker = &ctx->frame_workers[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700388 FrameWorkerData *frame_worker_data = NULL;
389 winterface->init(worker);
Yaowu Xuf883b422016-08-30 14:01:10 -0700390 worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700391 if (worker->data1 == NULL) {
392 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700393 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700394 }
395 frame_worker_data = (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700396 frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397 if (frame_worker_data->pbi == NULL) {
398 set_error_detail(ctx, "Failed to allocate frame_worker_data");
Yaowu Xuf883b422016-08-30 14:01:10 -0700399 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400 }
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100401 frame_worker_data->pbi->common.options = &ctx->cfg.cfg;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402 frame_worker_data->pbi->frame_worker_owner = worker;
403 frame_worker_data->worker_id = i;
404 frame_worker_data->scratch_buffer = NULL;
405 frame_worker_data->scratch_buffer_size = 0;
406 frame_worker_data->frame_context_ready = 0;
407 frame_worker_data->received_frame = 0;
408#if CONFIG_MULTITHREAD
409 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
410 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
Yaowu Xuf883b422016-08-30 14:01:10 -0700411 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700412 }
413
414 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
415 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
Yaowu Xuf883b422016-08-30 14:01:10 -0700416 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417 }
418#endif
Sebastien Alaiwan8b7a4e12017-06-13 11:25:57 +0200419 frame_worker_data->pbi->allow_lowbitdepth = ctx->cfg.allow_lowbitdepth;
420
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421 // If decoding in serial mode, FrameWorker thread could create tile worker
422 // thread or loopfilter thread.
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800423 frame_worker_data->pbi->max_threads = ctx->cfg.threads;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700424 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800425 frame_worker_data->pbi->common.large_scale_tile = ctx->tile_mode;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400426 frame_worker_data->pbi->common.is_annexb = ctx->is_annexb;
Yunqing Wang8ae64a92018-01-12 12:26:44 -0800427 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
428 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
Frank Bossend8f457a2018-04-30 21:44:12 -0400429 frame_worker_data->pbi->operating_point = ctx->operating_point;
David Barkerfb356012018-06-07 13:34:44 +0100430 frame_worker_data->pbi->output_all_layers = ctx->output_all_layers;
Yunqing Wangfcd71662018-06-06 12:08:31 -0700431 frame_worker_data->pbi->ext_tile_debug = ctx->ext_tile_debug;
432
Yaowu Xuf883b422016-08-30 14:01:10 -0700433 worker->hook = (AVxWorkerHook)frame_worker_hook;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700434 if (!winterface->reset(worker)) {
435 set_error_detail(ctx, "Frame Worker thread creation failed");
Yaowu Xuf883b422016-08-30 14:01:10 -0700436 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437 }
438 }
439
440 // If postprocessing was enabled by the application and a
441 // configuration has not been provided, default it.
Yaowu Xuf883b422016-08-30 14:01:10 -0700442 if (!ctx->postproc_cfg_set && (ctx->base.init_flags & AOM_CODEC_USE_POSTPROC))
Yaowu Xuc27fc142016-08-22 16:08:15 -0700443 set_default_ppflags(&ctx->postproc_cfg);
444
445 init_buffer_callbacks(ctx);
446
Yaowu Xuf883b422016-08-30 14:01:10 -0700447 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700448}
449
Yaowu Xuf883b422016-08-30 14:01:10 -0700450static INLINE void check_resync(aom_codec_alg_priv_t *const ctx,
451 const AV1Decoder *const pbi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700452 // Clear resync flag if worker got a key frame or intra only frame.
453 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
454 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
455 ctx->need_resync = 0;
456}
457
Yaowu Xuf883b422016-08-30 14:01:10 -0700458static aom_codec_err_t decode_one(aom_codec_alg_priv_t *ctx,
Tom Finegan21f48252018-05-15 11:31:04 -0700459 const uint8_t **data, size_t data_sz,
Sean DuBois47cc2552018-01-23 07:44:16 +0000460 void *user_priv) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700461 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700462
463 // Determine the stream parameters. Note that we rely on peek_si to
464 // validate that we have a buffer that does not wrap around the top
465 // of the heap.
466 if (!ctx->si.h) {
467 int is_intra_only = 0;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400468 ctx->si.is_annexb = ctx->is_annexb;
Tom Finegan21f48252018-05-15 11:31:04 -0700469 const aom_codec_err_t res =
470 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only);
Yaowu Xuf883b422016-08-30 14:01:10 -0700471 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472
Yaowu Xuf883b422016-08-30 14:01:10 -0700473 if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474 }
475
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800476 AVxWorker *const worker = ctx->frame_workers;
477 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
478 frame_worker_data->data = *data;
479 frame_worker_data->data_size = data_sz;
480 frame_worker_data->user_priv = user_priv;
481 frame_worker_data->received_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700482
Nathan E. Egge2cf03b12017-02-22 16:19:59 -0500483#if CONFIG_INSPECTION
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800484 frame_worker_data->pbi->inspect_cb = ctx->inspect_cb;
485 frame_worker_data->pbi->inspect_ctx = ctx->inspect_ctx;
Nathan E. Egge2cf03b12017-02-22 16:19:59 -0500486#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800488 frame_worker_data->pbi->common.large_scale_tile = ctx->tile_mode;
489 frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
490 frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
Yunqing Wangfcd71662018-06-06 12:08:31 -0700491 frame_worker_data->pbi->ext_tile_debug = ctx->ext_tile_debug;
Yunqing Wang93b18f32018-06-08 21:08:29 -0700492 frame_worker_data->pbi->ext_refs = ctx->ext_refs;
493
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
David Barkerf1c07ce2018-06-06 17:50:18 +0100517 // Release any pending output frames from the previous decoder call.
518 // We need to do this even if the decoder is being flushed
519 if (ctx->frame_workers) {
520 BufferPool *const pool = ctx->buffer_pool;
521 RefCntBuffer *const frame_bufs = pool->frame_bufs;
522 lock_buffer_pool(pool);
523 for (int i = 0; i < ctx->num_frame_workers; ++i) {
524 AVxWorker *const worker = &ctx->frame_workers[i];
525 FrameWorkerData *const frame_worker_data =
526 (FrameWorkerData *)worker->data1;
527 struct AV1Decoder *pbi = frame_worker_data->pbi;
528 for (size_t j = 0; j < pbi->num_output_frames; j++) {
529 decrease_ref_count((int)pbi->output_frame_index[j], frame_bufs, pool);
530 }
531 pbi->num_output_frames = 0;
532 }
533 unlock_buffer_pool(ctx->buffer_pool);
534 }
535
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 if (data == NULL && data_sz == 0) {
537 ctx->flushed = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700538 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700539 }
540
541 // Reset flushed when receiving a valid frame.
542 ctx->flushed = 0;
543
544 // Initialize the decoder workers on the first frame.
545 if (ctx->frame_workers == NULL) {
Urvang Joshi454280d2016-10-14 16:51:44 -0700546 res = init_decoder(ctx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700547 if (res != AOM_CODEC_OK) return res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700548 }
Soo-Chul Han29c46fb2018-03-23 16:02:00 -0400549
Soo-Chul Han8acdbfc2018-04-02 12:55:33 -0400550 if (ctx->is_annexb) {
551 // read the size of this temporal unit
552 size_t length_of_size;
Wan-Teh Chang6e870902018-05-16 09:55:52 -0700553 uint64_t temporal_unit_size;
554 if (aom_uleb_decode(data_start, data_sz, &temporal_unit_size,
555 &length_of_size) != 0) {
Soo-Chul Han8acdbfc2018-04-02 12:55:33 -0400556 return AOM_CODEC_CORRUPT_FRAME;
557 }
558 data_start += length_of_size;
Wan-Teh Chang6e870902018-05-16 09:55:52 -0700559 if (temporal_unit_size > (size_t)(data_end - data_start))
560 return AOM_CODEC_CORRUPT_FRAME;
561 data_end = data_start + temporal_unit_size;
Soo-Chul Han8acdbfc2018-04-02 12:55:33 -0400562 }
563
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800564 // Decode in serial mode.
David Barker0bae60c2018-05-10 14:37:45 +0100565 while (data_start < data_end) {
566 uint64_t frame_size;
567 if (ctx->is_annexb) {
568 // read the size of this frame unit
569 size_t length_of_size;
570 if (aom_uleb_decode(data_start, (size_t)(data_end - data_start),
571 &frame_size, &length_of_size) != 0) {
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800572 return AOM_CODEC_CORRUPT_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700573 }
David Barker0bae60c2018-05-10 14:37:45 +0100574 data_start += length_of_size;
Wan-Teh Chang6e870902018-05-16 09:55:52 -0700575 if (frame_size > (size_t)(data_end - data_start))
576 return AOM_CODEC_CORRUPT_FRAME;
David Barker0bae60c2018-05-10 14:37:45 +0100577 } else {
578 frame_size = (uint64_t)(data_end - data_start);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 }
David Barker0bae60c2018-05-10 14:37:45 +0100580
Tom Finegan21f48252018-05-15 11:31:04 -0700581 res = decode_one(ctx, &data_start, (size_t)frame_size, user_priv);
David Barker0bae60c2018-05-10 14:37:45 +0100582 if (res != AOM_CODEC_OK) return res;
583
584 // Allow extra zero bytes after the frame end
Debargha Mukherjee5521a182018-03-06 12:29:01 -0800585 while (data_start < data_end) {
David Barker0bae60c2018-05-10 14:37:45 +0100586 const uint8_t marker = data_start[0];
587 if (marker) break;
588 ++data_start;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700589 }
590 }
591
592 return res;
593}
594
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800595aom_image_t *add_grain_if_needed(aom_image_t *img, aom_image_t *grain_img_buf,
596 aom_film_grain_t *grain_params) {
597 if (!grain_params->apply_grain) return img;
598
599 if (grain_img_buf &&
600 (img->d_w != grain_img_buf->d_w || img->d_h != grain_img_buf->d_h ||
Andrey Norkin4352fed2018-03-01 17:01:49 -0800601 img->fmt != grain_img_buf->fmt || !(img->d_h % 2) || !(img->d_w % 2))) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800602 aom_img_free(grain_img_buf);
603 grain_img_buf = NULL;
604 }
605 if (!grain_img_buf) {
Andrey Norkin4352fed2018-03-01 17:01:49 -0800606 int w_even = img->d_w % 2 ? img->d_w + 1 : img->d_w;
607 int h_even = img->d_h % 2 ? img->d_h + 1 : img->d_h;
608 grain_img_buf = aom_img_alloc(NULL, img->fmt, w_even, h_even, 16);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800609 grain_img_buf->bit_depth = img->bit_depth;
610 }
611
612 av1_add_film_grain(grain_params, img, grain_img_buf);
613
614 return grain_img_buf;
615}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800616
Yaowu Xuf883b422016-08-30 14:01:10 -0700617static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
618 aom_codec_iter_t *iter) {
619 aom_image_t *img = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700620
David Barkerf1c07ce2018-06-06 17:50:18 +0100621 if (!iter) {
622 return NULL;
623 }
624
625 // To avoid having to allocate any extra storage, treat 'iter' as
626 // simply a pointer to an integer index
627 uintptr_t *index = (uintptr_t *)iter;
628
629 if (ctx->frame_workers != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630 do {
David Barkerf1c07ce2018-06-06 17:50:18 +0100631 YV12_BUFFER_CONFIG *sd;
632 // NOTE(david.barker): This code does not support multiple worker threads
633 // yet. We should probably move the iteration over threads into *iter
634 // instead of using ctx->next_output_worker_id.
Yaowu Xuf883b422016-08-30 14:01:10 -0700635 const AVxWorkerInterface *const winterface = aom_get_worker_interface();
636 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 FrameWorkerData *const frame_worker_data =
638 (FrameWorkerData *)worker->data1;
639 ctx->next_output_worker_id =
640 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
641 // Wait for the frame from worker thread.
642 if (winterface->sync(worker)) {
643 // Check if worker has received any frames.
644 if (frame_worker_data->received_frame == 1) {
645 ++ctx->available_threads;
646 frame_worker_data->received_frame = 0;
647 check_resync(ctx, frame_worker_data->pbi);
648 }
David Barkerf1c07ce2018-06-06 17:50:18 +0100649 aom_film_grain_t *grain_params;
650 if (av1_get_raw_frame(frame_worker_data->pbi, *index, &sd,
651 &grain_params) == 0) {
652 *index += 1; // Advance the iterator to point to the next image
653
Yunqing Wanga2f17282018-06-18 22:43:08 -0700654 AV1Decoder *const pbi = frame_worker_data->pbi;
655 AV1_COMMON *const cm = &pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700656 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
Yunqing Wanga2f17282018-06-18 22:43:08 -0700657 ctx->last_show_frame = cm->new_fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658 if (ctx->need_resync) return NULL;
David Barkerf1c07ce2018-06-06 17:50:18 +0100659 yuvconfig2image(&ctx->img, sd, frame_worker_data->user_priv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660
Yunqing Wanga2f17282018-06-18 22:43:08 -0700661 if (!pbi->ext_tile_debug && cm->large_scale_tile) {
662 img = &ctx->img;
663 img->img_data = pbi->tile_list_output;
664 img->sz = pbi->tile_list_size;
665 return img;
666 }
667
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000668 const int num_planes = av1_num_planes(cm);
Yunqing Wanga2f17282018-06-18 22:43:08 -0700669 if (pbi->ext_tile_debug && cm->single_tile_decoding &&
670 pbi->dec_tile_row >= 0) {
671 const int tile_row = AOMMIN(pbi->dec_tile_row, cm->tile_rows - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700672 const int mi_row = tile_row * cm->tile_height;
673 const int ssy = ctx->img.y_chroma_shift;
674 int plane;
675 ctx->img.planes[0] += mi_row * MI_SIZE * ctx->img.stride[0];
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000676 if (num_planes > 1) {
677 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
678 ctx->img.planes[plane] +=
679 mi_row * (MI_SIZE >> ssy) * ctx->img.stride[plane];
680 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700681 }
682 ctx->img.d_h =
Yaowu Xuf883b422016-08-30 14:01:10 -0700683 AOMMIN(cm->tile_height, cm->mi_rows - mi_row) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700684 }
685
Yunqing Wanga2f17282018-06-18 22:43:08 -0700686 if (pbi->ext_tile_debug && cm->single_tile_decoding &&
687 pbi->dec_tile_col >= 0) {
688 const int tile_col = AOMMIN(pbi->dec_tile_col, cm->tile_cols - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689 const int mi_col = tile_col * cm->tile_width;
690 const int ssx = ctx->img.x_chroma_shift;
691 int plane;
692 ctx->img.planes[0] += mi_col * MI_SIZE;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000693 if (num_planes > 1) {
694 for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
695 ctx->img.planes[plane] += mi_col * (MI_SIZE >> ssx);
696 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697 }
698 ctx->img.d_w =
Yaowu Xuf883b422016-08-30 14:01:10 -0700699 AOMMIN(cm->tile_width, cm->mi_cols - mi_col) * MI_SIZE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700701
702 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
703 img = &ctx->img;
Soo-Chul Hanf8589862018-01-24 03:13:14 +0000704 img->temporal_id = cm->temporal_layer_id;
Soo-Chul Hand2f317c2018-05-08 14:21:24 -0400705 img->spatial_id = cm->spatial_layer_id;
David Barkerf1c07ce2018-06-06 17:50:18 +0100706 return add_grain_if_needed(img, ctx->image_with_grain, grain_params);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700707 }
708 } else {
709 // Decoding failed. Release the worker thread.
710 frame_worker_data->received_frame = 0;
711 ++ctx->available_threads;
712 ctx->need_resync = 1;
713 if (ctx->flushed != 1) return NULL;
714 }
715 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
716 }
717 return NULL;
718}
719
Yaowu Xuf883b422016-08-30 14:01:10 -0700720static aom_codec_err_t decoder_set_fb_fn(
721 aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
722 aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 if (cb_get == NULL || cb_release == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700724 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700725 } else if (ctx->frame_workers == NULL) {
726 // If the decoder has already been initialized, do not accept changes to
727 // the frame buffer functions.
728 ctx->get_ext_fb_cb = cb_get;
729 ctx->release_ext_fb_cb = cb_release;
730 ctx->ext_priv = cb_priv;
Yaowu Xuf883b422016-08-30 14:01:10 -0700731 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 }
733
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735}
736
Yaowu Xuf883b422016-08-30 14:01:10 -0700737static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738 va_list args) {
Thomas Daede497d1952017-08-08 17:33:06 -0700739 av1_ref_frame_t *const data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 if (data) {
Thomas Daede497d1952017-08-08 17:33:06 -0700742 av1_ref_frame_t *const frame = data;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700745 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
746 image2yuvconfig(&frame->img, &sd);
Thomas Daede497d1952017-08-08 17:33:06 -0700747 return av1_set_reference_dec(&frame_worker_data->pbi->common, frame->idx,
Yunqing Wang4450c762018-04-24 13:49:25 -0700748 frame->use_external_ref, &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700750 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700751 }
752}
753
Yaowu Xuf883b422016-08-30 14:01:10 -0700754static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700755 va_list args) {
Thomas Daede497d1952017-08-08 17:33:06 -0700756 const av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
Urvang Joshi77853e52016-07-15 12:47:01 -0700757 if (frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700758 YV12_BUFFER_CONFIG sd;
Yaowu Xuf883b422016-08-30 14:01:10 -0700759 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
761 image2yuvconfig(&frame->img, &sd);
Thomas Daede497d1952017-08-08 17:33:06 -0700762 return av1_copy_reference_dec(frame_worker_data->pbi, frame->idx, &sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700764 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 }
766}
767
Yaowu Xuf883b422016-08-30 14:01:10 -0700768static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700769 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 av1_ref_frame_t *data = va_arg(args, av1_ref_frame_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 if (data) {
772 YV12_BUFFER_CONFIG *fb;
Yaowu Xuf883b422016-08-30 14:01:10 -0700773 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
775 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
Yaowu Xuf883b422016-08-30 14:01:10 -0700776 if (fb == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777 yuvconfig2image(&data->img, fb, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700778 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700779 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700780 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781 }
782}
783
Yaowu Xuf883b422016-08-30 14:01:10 -0700784static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700785 va_list args) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700786 aom_image_t *new_img = va_arg(args, aom_image_t *);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787 if (new_img) {
788 YV12_BUFFER_CONFIG new_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
791
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793 yuvconfig2image(new_img, &new_frame, NULL);
Yaowu Xuf883b422016-08-30 14:01:10 -0700794 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700796 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797 }
798 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 }
801}
802
Yunqing Wang20a336d2018-04-25 10:19:53 -0700803static aom_codec_err_t ctrl_copy_new_frame_image(aom_codec_alg_priv_t *ctx,
804 va_list args) {
805 aom_image_t *img = va_arg(args, aom_image_t *);
806 if (img) {
807 YV12_BUFFER_CONFIG new_frame;
808 AVxWorker *const worker = ctx->frame_workers;
809 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
810
811 if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
812 YV12_BUFFER_CONFIG sd;
813 image2yuvconfig(img, &sd);
814 return av1_copy_new_frame_dec(&frame_worker_data->pbi->common, &new_frame,
815 &sd);
816 } else {
817 return AOM_CODEC_ERROR;
818 }
819 } else {
820 return AOM_CODEC_INVALID_PARAM;
821 }
822}
823
Yaowu Xuf883b422016-08-30 14:01:10 -0700824static aom_codec_err_t ctrl_set_postproc(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700825 va_list args) {
826 (void)ctx;
827 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700828 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700829}
830
Yaowu Xuf883b422016-08-30 14:01:10 -0700831static aom_codec_err_t ctrl_set_dbg_options(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832 va_list args) {
833 (void)ctx;
834 (void)args;
Yaowu Xuf883b422016-08-30 14:01:10 -0700835 return AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700836}
837
Yaowu Xuf883b422016-08-30 14:01:10 -0700838static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 va_list args) {
840 int *const update_info = va_arg(args, int *);
841
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842 if (update_info) {
843 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700844 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845 FrameWorkerData *const frame_worker_data =
846 (FrameWorkerData *)worker->data1;
847 *update_info = frame_worker_data->pbi->refresh_frame_flags;
Yaowu Xuf883b422016-08-30 14:01:10 -0700848 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851 }
852 }
853
Yaowu Xuf883b422016-08-30 14:01:10 -0700854 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855}
856
Peter Boströma1f64322017-01-19 11:35:30 -0500857static aom_codec_err_t ctrl_get_last_quantizer(aom_codec_alg_priv_t *ctx,
858 va_list args) {
859 int *const arg = va_arg(args, int *);
860 if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
861 *arg =
862 ((FrameWorkerData *)ctx->frame_workers[0].data1)->pbi->common.base_qindex;
863 return AOM_CODEC_OK;
864}
865
Yaowu Xuf883b422016-08-30 14:01:10 -0700866static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700867 va_list args) {
868 int *corrupted = va_arg(args, int *);
869
870 if (corrupted) {
871 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700872 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873 FrameWorkerData *const frame_worker_data =
874 (FrameWorkerData *)worker->data1;
Imdad Sardharwalla2af15092018-05-25 14:44:46 +0100875 AV1Decoder *const pbi = frame_worker_data->pbi;
876 RefCntBuffer *const frame_bufs = pbi->common.buffer_pool->frame_bufs;
David Barkerf1c07ce2018-06-06 17:50:18 +0100877 if (pbi->seen_frame_header && pbi->num_output_frames == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -0700878 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700879 if (ctx->last_show_frame >= 0)
880 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
Yaowu Xuf883b422016-08-30 14:01:10 -0700881 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700883 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700884 }
885 }
886
Yaowu Xuf883b422016-08-30 14:01:10 -0700887 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888}
889
Yaowu Xuf883b422016-08-30 14:01:10 -0700890static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891 va_list args) {
892 int *const frame_size = va_arg(args, int *);
893
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894 if (frame_size) {
895 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700896 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897 FrameWorkerData *const frame_worker_data =
898 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700899 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900 frame_size[0] = cm->width;
901 frame_size[1] = cm->height;
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 }
907
Yaowu Xuf883b422016-08-30 14:01:10 -0700908 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700909}
910
Yunqing Wang3f9c1e22018-06-16 16:48:15 -0700911static aom_codec_err_t ctrl_get_frame_header_info(aom_codec_alg_priv_t *ctx,
912 va_list args) {
913 aom_tile_data *const frame_header_info = va_arg(args, aom_tile_data *);
914
915 if (frame_header_info) {
916 if (ctx->frame_workers) {
917 AVxWorker *const worker = ctx->frame_workers;
918 FrameWorkerData *const frame_worker_data =
919 (FrameWorkerData *)worker->data1;
920 const AV1Decoder *pbi = frame_worker_data->pbi;
921 frame_header_info->coded_tile_data_size = pbi->obu_size_hdr.size;
922 frame_header_info->coded_tile_data = pbi->obu_size_hdr.data;
923 frame_header_info->extra_size = pbi->frame_header_size;
924 } else {
925 return AOM_CODEC_ERROR;
926 }
927 }
928
929 return AOM_CODEC_INVALID_PARAM;
930}
931
Yunqing Wang265d15d2018-05-31 14:59:03 -0700932static aom_codec_err_t ctrl_get_tile_data(aom_codec_alg_priv_t *ctx,
933 va_list args) {
934 aom_tile_data *const tile_data = va_arg(args, aom_tile_data *);
935
936 if (tile_data) {
937 if (ctx->frame_workers) {
938 AVxWorker *const worker = ctx->frame_workers;
939 FrameWorkerData *const frame_worker_data =
940 (FrameWorkerData *)worker->data1;
941 const AV1Decoder *pbi = frame_worker_data->pbi;
942 tile_data->coded_tile_data_size =
943 pbi->tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].size;
944 tile_data->coded_tile_data =
Yunqing Wangcd28b202018-06-06 11:53:23 -0700945 pbi->tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].data;
Yunqing Wang265d15d2018-05-31 14:59:03 -0700946 return AOM_CODEC_OK;
947 } else {
948 return AOM_CODEC_ERROR;
949 }
950 }
951
952 return AOM_CODEC_INVALID_PARAM;
953}
954
Yunqing Wang93b18f32018-06-08 21:08:29 -0700955static aom_codec_err_t ctrl_set_ext_ref_ptr(aom_codec_alg_priv_t *ctx,
956 va_list args) {
957 av1_ext_ref_frame_t *const data = va_arg(args, av1_ext_ref_frame_t *);
958
959 if (data) {
960 av1_ext_ref_frame_t *const ext_frames = data;
961 ctx->ext_refs.num = ext_frames->num;
962 for (int i = 0; i < ctx->ext_refs.num; i++) {
963 image2yuvconfig(ext_frames->img++, &ctx->ext_refs.refs[i]);
964 }
965 return AOM_CODEC_OK;
966 } else {
967 return AOM_CODEC_INVALID_PARAM;
968 }
969}
970
Yaowu Xuf883b422016-08-30 14:01:10 -0700971static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972 va_list args) {
973 int *const render_size = va_arg(args, int *);
974
Yaowu Xuc27fc142016-08-22 16:08:15 -0700975 if (render_size) {
976 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700977 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700978 FrameWorkerData *const frame_worker_data =
979 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700980 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700981 render_size[0] = cm->render_width;
982 render_size[1] = cm->render_height;
Yaowu Xuf883b422016-08-30 14:01:10 -0700983 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700984 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700985 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700986 }
987 }
988
Yaowu Xuf883b422016-08-30 14:01:10 -0700989 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700990}
991
Yaowu Xuf883b422016-08-30 14:01:10 -0700992static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700993 va_list args) {
994 unsigned int *const bit_depth = va_arg(args, unsigned int *);
Yaowu Xuf883b422016-08-30 14:01:10 -0700995 AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700996
997 if (bit_depth) {
998 if (worker) {
999 FrameWorkerData *const frame_worker_data =
1000 (FrameWorkerData *)worker->data1;
Yaowu Xuf883b422016-08-30 14:01:10 -07001001 const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001002 *bit_depth = cm->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -07001003 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07001005 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001006 }
1007 }
1008
Yaowu Xuf883b422016-08-30 14:01:10 -07001009 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001010}
1011
Yaowu Xuf883b422016-08-30 14:01:10 -07001012static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001013 va_list args) {
1014 ctx->invert_tile_order = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001015 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016}
1017
Yaowu Xuf883b422016-08-30 14:01:10 -07001018static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001019 va_list args) {
1020 const int legacy_byte_alignment = 0;
1021 const int min_byte_alignment = 32;
1022 const int max_byte_alignment = 1024;
1023 const int byte_alignment = va_arg(args, int);
1024
1025 if (byte_alignment != legacy_byte_alignment &&
1026 (byte_alignment < min_byte_alignment ||
1027 byte_alignment > max_byte_alignment ||
1028 (byte_alignment & (byte_alignment - 1)) != 0))
Yaowu Xuf883b422016-08-30 14:01:10 -07001029 return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001030
1031 ctx->byte_alignment = byte_alignment;
1032 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001033 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001034 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1035 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1036 }
Yaowu Xuf883b422016-08-30 14:01:10 -07001037 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001038}
1039
Yaowu Xuf883b422016-08-30 14:01:10 -07001040static aom_codec_err_t ctrl_set_skip_loop_filter(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001041 va_list args) {
1042 ctx->skip_loop_filter = va_arg(args, int);
1043
1044 if (ctx->frame_workers) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001045 AVxWorker *const worker = ctx->frame_workers;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1047 frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
1048 }
1049
Yaowu Xuf883b422016-08-30 14:01:10 -07001050 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001051}
1052
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001053static aom_codec_err_t ctrl_get_accounting(aom_codec_alg_priv_t *ctx,
1054 va_list args) {
1055#if !CONFIG_ACCOUNTING
1056 (void)ctx;
1057 (void)args;
1058 return AOM_CODEC_INCAPABLE;
1059#else
1060 if (ctx->frame_workers) {
1061 AVxWorker *const worker = ctx->frame_workers;
1062 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1063 AV1Decoder *pbi = frame_worker_data->pbi;
1064 Accounting **acct = va_arg(args, Accounting **);
1065 *acct = &pbi->accounting;
1066 return AOM_CODEC_OK;
1067 }
1068 return AOM_CODEC_ERROR;
1069#endif
1070}
Yaowu Xuf883b422016-08-30 14:01:10 -07001071static aom_codec_err_t ctrl_set_decode_tile_row(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001072 va_list args) {
1073 ctx->decode_tile_row = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001074 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001075}
1076
Yaowu Xuf883b422016-08-30 14:01:10 -07001077static aom_codec_err_t ctrl_set_decode_tile_col(aom_codec_alg_priv_t *ctx,
Yaowu Xuc27fc142016-08-22 16:08:15 -07001078 va_list args) {
1079 ctx->decode_tile_col = va_arg(args, int);
Yaowu Xuf883b422016-08-30 14:01:10 -07001080 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001081}
1082
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001083static aom_codec_err_t ctrl_set_tile_mode(aom_codec_alg_priv_t *ctx,
1084 va_list args) {
1085 ctx->tile_mode = va_arg(args, unsigned int);
1086 return AOM_CODEC_OK;
1087}
1088
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04001089static aom_codec_err_t ctrl_set_is_annexb(aom_codec_alg_priv_t *ctx,
1090 va_list args) {
1091 ctx->is_annexb = va_arg(args, unsigned int);
1092 return AOM_CODEC_OK;
1093}
1094
Frank Bossend8f457a2018-04-30 21:44:12 -04001095static aom_codec_err_t ctrl_set_operating_point(aom_codec_alg_priv_t *ctx,
1096 va_list args) {
1097 ctx->operating_point = va_arg(args, int);
1098 return AOM_CODEC_OK;
1099}
1100
David Barkerfb356012018-06-07 13:34:44 +01001101static aom_codec_err_t ctrl_set_output_all_layers(aom_codec_alg_priv_t *ctx,
1102 va_list args) {
1103 ctx->output_all_layers = va_arg(args, int);
1104 return AOM_CODEC_OK;
1105}
1106
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001107static aom_codec_err_t ctrl_set_inspection_callback(aom_codec_alg_priv_t *ctx,
1108 va_list args) {
1109#if !CONFIG_INSPECTION
1110 (void)ctx;
1111 (void)args;
1112 return AOM_CODEC_INCAPABLE;
1113#else
1114 aom_inspect_init *init = va_arg(args, aom_inspect_init *);
1115 ctx->inspect_cb = init->inspect_cb;
1116 ctx->inspect_ctx = init->inspect_ctx;
1117 return AOM_CODEC_OK;
1118#endif
1119}
1120
Yunqing Wangfcd71662018-06-06 12:08:31 -07001121static aom_codec_err_t ctrl_ext_tile_debug(aom_codec_alg_priv_t *ctx,
1122 va_list args) {
1123 ctx->ext_tile_debug = va_arg(args, int);
1124 return AOM_CODEC_OK;
1125}
1126
Yaowu Xuf883b422016-08-30 14:01:10 -07001127static aom_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
Thomas Daede497d1952017-08-08 17:33:06 -07001128 { AV1_COPY_REFERENCE, ctrl_copy_reference },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001129
1130 // Setters
Thomas Daede497d1952017-08-08 17:33:06 -07001131 { AV1_SET_REFERENCE, ctrl_set_reference },
Yaowu Xuf883b422016-08-30 14:01:10 -07001132 { AOM_SET_POSTPROC, ctrl_set_postproc },
1133 { AOM_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options },
1134 { AOM_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options },
1135 { AOM_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options },
1136 { AOM_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options },
1137 { AV1_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
Yaowu Xuf883b422016-08-30 14:01:10 -07001138 { AV1_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
1139 { AV1_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
1140 { AV1_SET_DECODE_TILE_ROW, ctrl_set_decode_tile_row },
1141 { AV1_SET_DECODE_TILE_COL, ctrl_set_decode_tile_col },
Yunqing Wang8ae64a92018-01-12 12:26:44 -08001142 { AV1_SET_TILE_MODE, ctrl_set_tile_mode },
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04001143 { AV1D_SET_IS_ANNEXB, ctrl_set_is_annexb },
Frank Bossend8f457a2018-04-30 21:44:12 -04001144 { AV1D_SET_OPERATING_POINT, ctrl_set_operating_point },
David Barkerfb356012018-06-07 13:34:44 +01001145 { AV1D_SET_OUTPUT_ALL_LAYERS, ctrl_set_output_all_layers },
Nathan E. Egge2cf03b12017-02-22 16:19:59 -05001146 { AV1_SET_INSPECTION_CALLBACK, ctrl_set_inspection_callback },
Yunqing Wangfcd71662018-06-06 12:08:31 -07001147 { AV1D_EXT_TILE_DEBUG, ctrl_ext_tile_debug },
Yunqing Wang93b18f32018-06-08 21:08:29 -07001148 { AV1D_SET_EXT_REF_PTR, ctrl_set_ext_ref_ptr },
Yaowu Xuc27fc142016-08-22 16:08:15 -07001149
1150 // Getters
Yaowu Xuf883b422016-08-30 14:01:10 -07001151 { AOMD_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
Peter Boströma1f64322017-01-19 11:35:30 -05001152 { AOMD_GET_LAST_QUANTIZER, ctrl_get_last_quantizer },
1153 { AOMD_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
Yaowu Xuf883b422016-08-30 14:01:10 -07001154 { AV1D_GET_BIT_DEPTH, ctrl_get_bit_depth },
Peter Boströma1f64322017-01-19 11:35:30 -05001155 { AV1D_GET_DISPLAY_SIZE, ctrl_get_render_size },
Yaowu Xuf883b422016-08-30 14:01:10 -07001156 { AV1D_GET_FRAME_SIZE, ctrl_get_frame_size },
Nathan E. Eggec9862e02016-10-05 21:28:36 -04001157 { AV1_GET_ACCOUNTING, ctrl_get_accounting },
Yaowu Xuf883b422016-08-30 14:01:10 -07001158 { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
Yunqing Wang20a336d2018-04-25 10:19:53 -07001159 { AV1_COPY_NEW_FRAME_IMAGE, ctrl_copy_new_frame_image },
Peter Boströma1f64322017-01-19 11:35:30 -05001160 { AV1_GET_REFERENCE, ctrl_get_reference },
Yunqing Wang3f9c1e22018-06-16 16:48:15 -07001161 { AV1D_GET_FRAME_HEADER_INFO, ctrl_get_frame_header_info },
Yunqing Wang265d15d2018-05-31 14:59:03 -07001162 { AV1D_GET_TILE_DATA, ctrl_get_tile_data },
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};