• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * 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.
10  */
11 
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "config/aom_config.h"
16 #include "config/aom_version.h"
17 
18 #include "aom/internal/aom_codec_internal.h"
19 #include "aom/internal/aom_image_internal.h"
20 #include "aom/aomdx.h"
21 #include "aom/aom_decoder.h"
22 #include "aom_dsp/bitreader_buffer.h"
23 #include "aom_dsp/aom_dsp_common.h"
24 #include "aom_ports/mem_ops.h"
25 #include "aom_util/aom_thread.h"
26 
27 #include "av1/common/alloccommon.h"
28 #include "av1/common/frame_buffers.h"
29 #include "av1/common/enums.h"
30 #include "av1/common/obu_util.h"
31 
32 #include "av1/decoder/decoder.h"
33 #include "av1/decoder/decodeframe.h"
34 #include "av1/decoder/grain_synthesis.h"
35 #include "av1/decoder/obu.h"
36 
37 #include "av1/av1_iface_common.h"
38 
39 struct aom_codec_alg_priv {
40   aom_codec_priv_t base;
41   aom_codec_dec_cfg_t cfg;
42   aom_codec_stream_info_t si;
43   aom_image_t img;
44   int img_avail;
45   int flushed;
46   int invert_tile_order;
47   RefCntBuffer *last_show_frame;  // Last output frame buffer
48   int byte_alignment;
49   int skip_loop_filter;
50   int skip_film_grain;
51   int decode_tile_row;
52   int decode_tile_col;
53   unsigned int tile_mode;
54   unsigned int ext_tile_debug;
55   unsigned int row_mt;
56   EXTERNAL_REFERENCES ext_refs;
57   unsigned int is_annexb;
58   int operating_point;
59   int output_all_layers;
60 
61   AVxWorker *frame_worker;
62 
63   aom_image_t image_with_grain;
64   aom_codec_frame_buffer_t grain_image_frame_buffers[MAX_NUM_SPATIAL_LAYERS];
65   size_t num_grain_image_frame_buffers;
66   int need_resync;  // wait for key/intra-only frame
67   // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
68   BufferPool *buffer_pool;
69 
70   // External frame buffer info to save for AV1 common.
71   void *ext_priv;  // Private data associated with the external frame buffers.
72   aom_get_frame_buffer_cb_fn_t get_ext_fb_cb;
73   aom_release_frame_buffer_cb_fn_t release_ext_fb_cb;
74 
75 #if CONFIG_INSPECTION
76   aom_inspect_cb inspect_cb;
77   void *inspect_ctx;
78 #endif
79 };
80 
decoder_init(aom_codec_ctx_t * ctx)81 static aom_codec_err_t decoder_init(aom_codec_ctx_t *ctx) {
82   // This function only allocates space for the aom_codec_alg_priv_t
83   // structure. More memory may be required at the time the stream
84   // information becomes known.
85   if (!ctx->priv) {
86     aom_codec_alg_priv_t *const priv =
87         (aom_codec_alg_priv_t *)aom_calloc(1, sizeof(*priv));
88     if (priv == NULL) return AOM_CODEC_MEM_ERROR;
89 
90     ctx->priv = (aom_codec_priv_t *)priv;
91     ctx->priv->init_flags = ctx->init_flags;
92     priv->flushed = 0;
93 
94     // TODO(tdaede): this should not be exposed to the API
95     priv->cfg.allow_lowbitdepth = !FORCE_HIGHBITDEPTH_DECODING;
96     if (ctx->config.dec) {
97       priv->cfg = *ctx->config.dec;
98       ctx->config.dec = &priv->cfg;
99     }
100     priv->num_grain_image_frame_buffers = 0;
101     // Turn row_mt on by default.
102     priv->row_mt = 1;
103 
104     // Turn on normal tile coding mode by default.
105     // 0 is for normal tile coding mode, and 1 is for large scale tile coding
106     // mode(refer to lightfield example).
107     priv->tile_mode = 0;
108     priv->decode_tile_row = -1;
109     priv->decode_tile_col = -1;
110   }
111 
112   return AOM_CODEC_OK;
113 }
114 
decoder_destroy(aom_codec_alg_priv_t * ctx)115 static aom_codec_err_t decoder_destroy(aom_codec_alg_priv_t *ctx) {
116   if (ctx->frame_worker != NULL) {
117     AVxWorker *const worker = ctx->frame_worker;
118     aom_get_worker_interface()->end(worker);
119     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
120     if (frame_worker_data != NULL && frame_worker_data->pbi != NULL) {
121       AV1Decoder *const pbi = frame_worker_data->pbi;
122       aom_free(pbi->common.tpl_mvs);
123       pbi->common.tpl_mvs = NULL;
124       av1_remove_common(&frame_worker_data->pbi->common);
125       av1_free_cdef_buffers(&pbi->common, &pbi->cdef_worker, &pbi->cdef_sync);
126       av1_free_cdef_sync(&pbi->cdef_sync);
127       av1_free_restoration_buffers(&pbi->common);
128       av1_decoder_remove(pbi);
129     }
130     aom_free(frame_worker_data);
131 #if CONFIG_MULTITHREAD
132     pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
133 #endif
134   }
135 
136   if (ctx->buffer_pool) {
137     for (size_t i = 0; i < ctx->num_grain_image_frame_buffers; i++) {
138       ctx->buffer_pool->release_fb_cb(ctx->buffer_pool->cb_priv,
139                                       &ctx->grain_image_frame_buffers[i]);
140     }
141     av1_free_ref_frame_buffers(ctx->buffer_pool);
142     av1_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
143   }
144 
145   aom_free(ctx->frame_worker);
146   aom_free(ctx->buffer_pool);
147   aom_img_free(&ctx->img);
148   aom_free(ctx);
149   return AOM_CODEC_OK;
150 }
151 
parse_timing_info(struct aom_read_bit_buffer * rb)152 static aom_codec_err_t parse_timing_info(struct aom_read_bit_buffer *rb) {
153   const uint32_t num_units_in_display_tick =
154       aom_rb_read_unsigned_literal(rb, 32);
155   const uint32_t time_scale = aom_rb_read_unsigned_literal(rb, 32);
156   if (num_units_in_display_tick == 0 || time_scale == 0)
157     return AOM_CODEC_UNSUP_BITSTREAM;
158   const uint8_t equal_picture_interval = aom_rb_read_bit(rb);
159   if (equal_picture_interval) {
160     const uint32_t num_ticks_per_picture_minus_1 = aom_rb_read_uvlc(rb);
161     if (num_ticks_per_picture_minus_1 == UINT32_MAX) {
162       // num_ticks_per_picture_minus_1 cannot be (1 << 32) - 1.
163       return AOM_CODEC_UNSUP_BITSTREAM;
164     }
165   }
166   return AOM_CODEC_OK;
167 }
168 
parse_decoder_model_info(struct aom_read_bit_buffer * rb,int * buffer_delay_length_minus_1)169 static aom_codec_err_t parse_decoder_model_info(
170     struct aom_read_bit_buffer *rb, int *buffer_delay_length_minus_1) {
171   *buffer_delay_length_minus_1 = aom_rb_read_literal(rb, 5);
172   const uint32_t num_units_in_decoding_tick =
173       aom_rb_read_unsigned_literal(rb, 32);
174   const uint8_t buffer_removal_time_length_minus_1 = aom_rb_read_literal(rb, 5);
175   const uint8_t frame_presentation_time_length_minus_1 =
176       aom_rb_read_literal(rb, 5);
177   (void)num_units_in_decoding_tick;
178   (void)buffer_removal_time_length_minus_1;
179   (void)frame_presentation_time_length_minus_1;
180   return AOM_CODEC_OK;
181 }
182 
parse_op_parameters_info(struct aom_read_bit_buffer * rb,int buffer_delay_length_minus_1)183 static aom_codec_err_t parse_op_parameters_info(
184     struct aom_read_bit_buffer *rb, int buffer_delay_length_minus_1) {
185   const int n = buffer_delay_length_minus_1 + 1;
186   const uint32_t decoder_buffer_delay = aom_rb_read_unsigned_literal(rb, n);
187   const uint32_t encoder_buffer_delay = aom_rb_read_unsigned_literal(rb, n);
188   const uint8_t low_delay_mode_flag = aom_rb_read_bit(rb);
189   (void)decoder_buffer_delay;
190   (void)encoder_buffer_delay;
191   (void)low_delay_mode_flag;
192   return AOM_CODEC_OK;
193 }
194 
195 // Parses the operating points (including operating_point_idc, seq_level_idx,
196 // and seq_tier) and then sets si->number_spatial_layers and
197 // si->number_temporal_layers based on operating_point_idc[0].
parse_operating_points(struct aom_read_bit_buffer * rb,int is_reduced_header,aom_codec_stream_info_t * si)198 static aom_codec_err_t parse_operating_points(struct aom_read_bit_buffer *rb,
199                                               int is_reduced_header,
200                                               aom_codec_stream_info_t *si) {
201   int operating_point_idc0 = 0;
202   if (is_reduced_header) {
203     aom_rb_read_literal(rb, LEVEL_BITS);  // level
204   } else {
205     uint8_t decoder_model_info_present_flag = 0;
206     int buffer_delay_length_minus_1 = 0;
207     aom_codec_err_t status;
208     const uint8_t timing_info_present_flag = aom_rb_read_bit(rb);
209     if (timing_info_present_flag) {
210       if ((status = parse_timing_info(rb)) != AOM_CODEC_OK) return status;
211       decoder_model_info_present_flag = aom_rb_read_bit(rb);
212       if (decoder_model_info_present_flag) {
213         if ((status = parse_decoder_model_info(
214                  rb, &buffer_delay_length_minus_1)) != AOM_CODEC_OK)
215           return status;
216       }
217     }
218     const uint8_t initial_display_delay_present_flag = aom_rb_read_bit(rb);
219     const uint8_t operating_points_cnt_minus_1 =
220         aom_rb_read_literal(rb, OP_POINTS_CNT_MINUS_1_BITS);
221     for (int i = 0; i < operating_points_cnt_minus_1 + 1; i++) {
222       int operating_point_idc;
223       operating_point_idc = aom_rb_read_literal(rb, OP_POINTS_IDC_BITS);
224       if (i == 0) operating_point_idc0 = operating_point_idc;
225       int seq_level_idx = aom_rb_read_literal(rb, LEVEL_BITS);  // level
226       if (seq_level_idx > 7) aom_rb_read_bit(rb);               // tier
227       if (decoder_model_info_present_flag) {
228         const uint8_t decoder_model_present_for_this_op = aom_rb_read_bit(rb);
229         if (decoder_model_present_for_this_op) {
230           if ((status = parse_op_parameters_info(
231                    rb, buffer_delay_length_minus_1)) != AOM_CODEC_OK)
232             return status;
233         }
234       }
235       if (initial_display_delay_present_flag) {
236         const uint8_t initial_display_delay_present_for_this_op =
237             aom_rb_read_bit(rb);
238         if (initial_display_delay_present_for_this_op)
239           aom_rb_read_literal(rb, 4);  // initial_display_delay_minus_1
240       }
241     }
242   }
243 
244   if (aom_get_num_layers_from_operating_point_idc(
245           operating_point_idc0, &si->number_spatial_layers,
246           &si->number_temporal_layers) != AOM_CODEC_OK) {
247     return AOM_CODEC_ERROR;
248   }
249 
250   return AOM_CODEC_OK;
251 }
252 
decoder_peek_si_internal(const uint8_t * data,size_t data_sz,aom_codec_stream_info_t * si,int * is_intra_only)253 static aom_codec_err_t decoder_peek_si_internal(const uint8_t *data,
254                                                 size_t data_sz,
255                                                 aom_codec_stream_info_t *si,
256                                                 int *is_intra_only) {
257   int intra_only_flag = 0;
258   int got_sequence_header = 0;
259   int found_keyframe = 0;
260 
261   if (data + data_sz <= data || data_sz < 1) return AOM_CODEC_INVALID_PARAM;
262 
263   si->w = 0;
264   si->h = 0;
265   si->is_kf = 0;  // is_kf indicates whether the current packet contains a RAP
266 
267   ObuHeader obu_header;
268   memset(&obu_header, 0, sizeof(obu_header));
269   size_t payload_size = 0;
270   size_t bytes_read = 0;
271   uint8_t reduced_still_picture_hdr = 0;
272   aom_codec_err_t status = aom_read_obu_header_and_size(
273       data, data_sz, si->is_annexb, &obu_header, &payload_size, &bytes_read);
274   if (status != AOM_CODEC_OK) return status;
275 
276   // If the first OBU is a temporal delimiter, skip over it and look at the next
277   // OBU in the bitstream
278   if (obu_header.type == OBU_TEMPORAL_DELIMITER) {
279     // Skip any associated payload (there shouldn't be one, but just in case)
280     if (data_sz < bytes_read + payload_size) return AOM_CODEC_CORRUPT_FRAME;
281     data += bytes_read + payload_size;
282     data_sz -= bytes_read + payload_size;
283 
284     status = aom_read_obu_header_and_size(
285         data, data_sz, si->is_annexb, &obu_header, &payload_size, &bytes_read);
286     if (status != AOM_CODEC_OK) return status;
287   }
288   while (1) {
289     data += bytes_read;
290     data_sz -= bytes_read;
291     if (data_sz < payload_size) return AOM_CODEC_CORRUPT_FRAME;
292     // Check that the selected OBU is a sequence header
293     if (obu_header.type == OBU_SEQUENCE_HEADER) {
294       // Sanity check on sequence header size
295       if (data_sz < 2) return AOM_CODEC_CORRUPT_FRAME;
296       // Read a few values from the sequence header payload
297       struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
298 
299       av1_read_profile(&rb);  // profile
300       const uint8_t still_picture = aom_rb_read_bit(&rb);
301       reduced_still_picture_hdr = aom_rb_read_bit(&rb);
302 
303       if (!still_picture && reduced_still_picture_hdr) {
304         return AOM_CODEC_UNSUP_BITSTREAM;
305       }
306 
307       if (parse_operating_points(&rb, reduced_still_picture_hdr, si) !=
308           AOM_CODEC_OK) {
309         return AOM_CODEC_ERROR;
310       }
311 
312       int num_bits_width = aom_rb_read_literal(&rb, 4) + 1;
313       int num_bits_height = aom_rb_read_literal(&rb, 4) + 1;
314       int max_frame_width = aom_rb_read_literal(&rb, num_bits_width) + 1;
315       int max_frame_height = aom_rb_read_literal(&rb, num_bits_height) + 1;
316       si->w = max_frame_width;
317       si->h = max_frame_height;
318       got_sequence_header = 1;
319     } else if (obu_header.type == OBU_FRAME_HEADER ||
320                obu_header.type == OBU_FRAME) {
321       if (got_sequence_header && reduced_still_picture_hdr) {
322         found_keyframe = 1;
323         break;
324       } else {
325         // make sure we have enough bits to get the frame type out
326         if (data_sz < 1) return AOM_CODEC_CORRUPT_FRAME;
327         struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
328         const int show_existing_frame = aom_rb_read_bit(&rb);
329         if (!show_existing_frame) {
330           const FRAME_TYPE frame_type = (FRAME_TYPE)aom_rb_read_literal(&rb, 2);
331           if (frame_type == KEY_FRAME) {
332             found_keyframe = 1;
333             break;  // Stop here as no further OBUs will change the outcome.
334           } else if (frame_type == INTRA_ONLY_FRAME) {
335             intra_only_flag = 1;
336           }
337         }
338       }
339     }
340     // skip past any unread OBU header data
341     data += payload_size;
342     data_sz -= payload_size;
343     if (data_sz == 0) break;  // exit if we're out of OBUs
344     status = aom_read_obu_header_and_size(
345         data, data_sz, si->is_annexb, &obu_header, &payload_size, &bytes_read);
346     if (status != AOM_CODEC_OK) return status;
347   }
348   if (got_sequence_header && found_keyframe) si->is_kf = 1;
349   if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
350   return AOM_CODEC_OK;
351 }
352 
decoder_peek_si(const uint8_t * data,size_t data_sz,aom_codec_stream_info_t * si)353 static aom_codec_err_t decoder_peek_si(const uint8_t *data, size_t data_sz,
354                                        aom_codec_stream_info_t *si) {
355   return decoder_peek_si_internal(data, data_sz, si, NULL);
356 }
357 
decoder_get_si(aom_codec_alg_priv_t * ctx,aom_codec_stream_info_t * si)358 static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
359                                       aom_codec_stream_info_t *si) {
360   memcpy(si, &ctx->si, sizeof(*si));
361 
362   return AOM_CODEC_OK;
363 }
364 
set_error_detail(aom_codec_alg_priv_t * ctx,const char * const error)365 static void set_error_detail(aom_codec_alg_priv_t *ctx,
366                              const char *const error) {
367   ctx->base.err_detail = error;
368 }
369 
update_error_state(aom_codec_alg_priv_t * ctx,const struct aom_internal_error_info * error)370 static aom_codec_err_t update_error_state(
371     aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
372   if (error->error_code)
373     set_error_detail(ctx, error->has_detail ? error->detail : NULL);
374 
375   return error->error_code;
376 }
377 
init_buffer_callbacks(aom_codec_alg_priv_t * ctx)378 static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
379   AVxWorker *const worker = ctx->frame_worker;
380   FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
381   AV1Decoder *const pbi = frame_worker_data->pbi;
382   AV1_COMMON *const cm = &pbi->common;
383   BufferPool *const pool = cm->buffer_pool;
384 
385   cm->cur_frame = NULL;
386   cm->features.byte_alignment = ctx->byte_alignment;
387   pbi->skip_loop_filter = ctx->skip_loop_filter;
388   pbi->skip_film_grain = ctx->skip_film_grain;
389 
390   if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
391     pool->get_fb_cb = ctx->get_ext_fb_cb;
392     pool->release_fb_cb = ctx->release_ext_fb_cb;
393     pool->cb_priv = ctx->ext_priv;
394   } else {
395     pool->get_fb_cb = av1_get_frame_buffer;
396     pool->release_fb_cb = av1_release_frame_buffer;
397 
398     if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
399       aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
400                          "Failed to initialize internal frame buffers");
401 
402     pool->cb_priv = &pool->int_frame_buffers;
403   }
404 }
405 
frame_worker_hook(void * arg1,void * arg2)406 static int frame_worker_hook(void *arg1, void *arg2) {
407   FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
408   const uint8_t *data = frame_worker_data->data;
409   (void)arg2;
410 
411   int result = av1_receive_compressed_data(frame_worker_data->pbi,
412                                            frame_worker_data->data_size, &data);
413   frame_worker_data->data_end = data;
414 
415   if (result != 0) {
416     // Check decode result in serial decode.
417     frame_worker_data->pbi->need_resync = 1;
418   }
419   return !result;
420 }
421 
init_decoder(aom_codec_alg_priv_t * ctx)422 static aom_codec_err_t init_decoder(aom_codec_alg_priv_t *ctx) {
423   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
424 
425   ctx->last_show_frame = NULL;
426   ctx->need_resync = 1;
427   ctx->flushed = 0;
428 
429   ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
430   if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
431 
432 #if CONFIG_MULTITHREAD
433   if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
434     set_error_detail(ctx, "Failed to allocate buffer pool mutex");
435     return AOM_CODEC_MEM_ERROR;
436   }
437 #endif
438 
439   ctx->frame_worker = (AVxWorker *)aom_malloc(sizeof(*ctx->frame_worker));
440   if (ctx->frame_worker == NULL) {
441     set_error_detail(ctx, "Failed to allocate frame_worker");
442     return AOM_CODEC_MEM_ERROR;
443   }
444 
445   AVxWorker *const worker = ctx->frame_worker;
446   FrameWorkerData *frame_worker_data = NULL;
447   winterface->init(worker);
448   worker->thread_name = "aom frameworker";
449   worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
450   if (worker->data1 == NULL) {
451     set_error_detail(ctx, "Failed to allocate frame_worker_data");
452     return AOM_CODEC_MEM_ERROR;
453   }
454   frame_worker_data = (FrameWorkerData *)worker->data1;
455   frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
456   if (frame_worker_data->pbi == NULL) {
457     set_error_detail(ctx, "Failed to allocate frame_worker_data");
458     return AOM_CODEC_MEM_ERROR;
459   }
460   frame_worker_data->frame_context_ready = 0;
461   frame_worker_data->received_frame = 0;
462   frame_worker_data->pbi->allow_lowbitdepth = ctx->cfg.allow_lowbitdepth;
463 
464   // If decoding in serial mode, FrameWorker thread could create tile worker
465   // thread or loopfilter thread.
466   frame_worker_data->pbi->max_threads = ctx->cfg.threads;
467   frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
468   frame_worker_data->pbi->common.tiles.large_scale = ctx->tile_mode;
469   frame_worker_data->pbi->is_annexb = ctx->is_annexb;
470   frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
471   frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
472   frame_worker_data->pbi->operating_point = ctx->operating_point;
473   frame_worker_data->pbi->output_all_layers = ctx->output_all_layers;
474   frame_worker_data->pbi->ext_tile_debug = ctx->ext_tile_debug;
475   frame_worker_data->pbi->row_mt = ctx->row_mt;
476   frame_worker_data->pbi->is_fwd_kf_present = 0;
477   frame_worker_data->pbi->is_arf_frame_present = 0;
478   worker->hook = frame_worker_hook;
479 
480   init_buffer_callbacks(ctx);
481 
482   return AOM_CODEC_OK;
483 }
484 
check_resync(aom_codec_alg_priv_t * const ctx,const AV1Decoder * const pbi)485 static INLINE void check_resync(aom_codec_alg_priv_t *const ctx,
486                                 const AV1Decoder *const pbi) {
487   // Clear resync flag if worker got a key frame or intra only frame.
488   if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
489       frame_is_intra_only(&pbi->common))
490     ctx->need_resync = 0;
491 }
492 
decode_one(aom_codec_alg_priv_t * ctx,const uint8_t ** data,size_t data_sz,void * user_priv)493 static aom_codec_err_t decode_one(aom_codec_alg_priv_t *ctx,
494                                   const uint8_t **data, size_t data_sz,
495                                   void *user_priv) {
496   const AVxWorkerInterface *const winterface = aom_get_worker_interface();
497 
498   // Determine the stream parameters. Note that we rely on peek_si to
499   // validate that we have a buffer that does not wrap around the top
500   // of the heap.
501   if (!ctx->si.h) {
502     int is_intra_only = 0;
503     ctx->si.is_annexb = ctx->is_annexb;
504     const aom_codec_err_t res =
505         decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only);
506     if (res != AOM_CODEC_OK) return res;
507 
508     if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
509   }
510 
511   AVxWorker *const worker = ctx->frame_worker;
512   FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
513   frame_worker_data->data = *data;
514   frame_worker_data->data_size = data_sz;
515   frame_worker_data->user_priv = user_priv;
516   frame_worker_data->received_frame = 1;
517 
518   frame_worker_data->pbi->common.tiles.large_scale = ctx->tile_mode;
519   frame_worker_data->pbi->dec_tile_row = ctx->decode_tile_row;
520   frame_worker_data->pbi->dec_tile_col = ctx->decode_tile_col;
521   frame_worker_data->pbi->ext_tile_debug = ctx->ext_tile_debug;
522   frame_worker_data->pbi->row_mt = ctx->row_mt;
523   frame_worker_data->pbi->ext_refs = ctx->ext_refs;
524 
525   frame_worker_data->pbi->is_annexb = ctx->is_annexb;
526 
527   worker->had_error = 0;
528   winterface->execute(worker);
529 
530   // Update data pointer after decode.
531   *data = frame_worker_data->data_end;
532 
533   if (worker->had_error)
534     return update_error_state(ctx, &frame_worker_data->pbi->error);
535 
536   check_resync(ctx, frame_worker_data->pbi);
537 
538   return AOM_CODEC_OK;
539 }
540 
release_pending_output_frames(aom_codec_alg_priv_t * ctx)541 static void release_pending_output_frames(aom_codec_alg_priv_t *ctx) {
542   // Release any pending output frames from the previous decoder_decode or
543   // decoder_inspect call. We need to do this even if the decoder is being
544   // flushed or the input arguments are invalid.
545   if (ctx->frame_worker) {
546     BufferPool *const pool = ctx->buffer_pool;
547     lock_buffer_pool(pool);
548     AVxWorker *const worker = ctx->frame_worker;
549     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
550     struct AV1Decoder *pbi = frame_worker_data->pbi;
551     for (size_t j = 0; j < pbi->num_output_frames; j++) {
552       decrease_ref_count(pbi->output_frames[j], pool);
553     }
554     pbi->num_output_frames = 0;
555     unlock_buffer_pool(pool);
556     for (size_t j = 0; j < ctx->num_grain_image_frame_buffers; j++) {
557       pool->release_fb_cb(pool->cb_priv, &ctx->grain_image_frame_buffers[j]);
558       ctx->grain_image_frame_buffers[j].data = NULL;
559       ctx->grain_image_frame_buffers[j].size = 0;
560       ctx->grain_image_frame_buffers[j].priv = NULL;
561     }
562     ctx->num_grain_image_frame_buffers = 0;
563   }
564 }
565 
566 // This function enables the inspector to inspect non visible frames.
decoder_inspect(aom_codec_alg_priv_t * ctx,const uint8_t * data,size_t data_sz,void * user_priv)567 static aom_codec_err_t decoder_inspect(aom_codec_alg_priv_t *ctx,
568                                        const uint8_t *data, size_t data_sz,
569                                        void *user_priv) {
570   aom_codec_err_t res = AOM_CODEC_OK;
571 
572   release_pending_output_frames(ctx);
573 
574   /* Sanity checks */
575   /* NULL data ptr allowed if data_sz is 0 too */
576   if (data == NULL && data_sz == 0) {
577     ctx->flushed = 1;
578     return AOM_CODEC_OK;
579   }
580   if (data == NULL || data_sz == 0) return AOM_CODEC_INVALID_PARAM;
581 
582   // Reset flushed when receiving a valid frame.
583   ctx->flushed = 0;
584 
585   const uint8_t *data_start = data;
586   const uint8_t *data_end = data + data_sz;
587 
588   uint64_t frame_size;
589   if (ctx->is_annexb) {
590     // read the size of this temporal unit
591     size_t length_of_size;
592     uint64_t temporal_unit_size;
593     if (aom_uleb_decode(data_start, data_sz, &temporal_unit_size,
594                         &length_of_size) != 0) {
595       return AOM_CODEC_CORRUPT_FRAME;
596     }
597     data_start += length_of_size;
598     if (temporal_unit_size > (size_t)(data_end - data_start))
599       return AOM_CODEC_CORRUPT_FRAME;
600     data_end = data_start + temporal_unit_size;
601 
602     // read the size of this frame unit
603     if (aom_uleb_decode(data_start, (size_t)(data_end - data_start),
604                         &frame_size, &length_of_size) != 0) {
605       return AOM_CODEC_CORRUPT_FRAME;
606     }
607     data_start += length_of_size;
608     if (frame_size > (size_t)(data_end - data_start))
609       return AOM_CODEC_CORRUPT_FRAME;
610   } else {
611     frame_size = (uint64_t)(data_end - data_start);
612   }
613 
614   if (ctx->frame_worker == NULL) {
615     res = init_decoder(ctx);
616     if (res != AOM_CODEC_OK) return res;
617   }
618   FrameWorkerData *const frame_worker_data =
619       (FrameWorkerData *)ctx->frame_worker->data1;
620   AV1Decoder *const pbi = frame_worker_data->pbi;
621   AV1_COMMON *const cm = &pbi->common;
622 #if CONFIG_INSPECTION
623   frame_worker_data->pbi->inspect_cb = ctx->inspect_cb;
624   frame_worker_data->pbi->inspect_ctx = ctx->inspect_ctx;
625 #endif
626   res = av1_receive_compressed_data(frame_worker_data->pbi, (size_t)frame_size,
627                                     &data_start);
628   check_resync(ctx, frame_worker_data->pbi);
629 
630   if (ctx->frame_worker->had_error)
631     return update_error_state(ctx, &frame_worker_data->pbi->error);
632 
633   // Allow extra zero bytes after the frame end
634   while (data_start < data_end) {
635     const uint8_t marker = data_start[0];
636     if (marker) break;
637     ++data_start;
638   }
639 
640   Av1DecodeReturn *data2 = (Av1DecodeReturn *)user_priv;
641   data2->idx = -1;
642   if (cm->cur_frame) {
643     for (int i = 0; i < REF_FRAMES; ++i)
644       if (cm->ref_frame_map[i] == cm->cur_frame) data2->idx = i;
645   }
646   data2->buf = data_start;
647   data2->show_existing = cm->show_existing_frame;
648   return res;
649 }
650 
decoder_decode(aom_codec_alg_priv_t * ctx,const uint8_t * data,size_t data_sz,void * user_priv)651 static aom_codec_err_t decoder_decode(aom_codec_alg_priv_t *ctx,
652                                       const uint8_t *data, size_t data_sz,
653                                       void *user_priv) {
654   aom_codec_err_t res = AOM_CODEC_OK;
655 
656 #if CONFIG_INSPECTION
657   if (user_priv != 0) {
658     return decoder_inspect(ctx, data, data_sz, user_priv);
659   }
660 #endif
661 
662   release_pending_output_frames(ctx);
663 
664   /* Sanity checks */
665   /* NULL data ptr allowed if data_sz is 0 too */
666   if (data == NULL && data_sz == 0) {
667     ctx->flushed = 1;
668     return AOM_CODEC_OK;
669   }
670   if (data == NULL || data_sz == 0) return AOM_CODEC_INVALID_PARAM;
671 
672   // Reset flushed when receiving a valid frame.
673   ctx->flushed = 0;
674 
675   // Initialize the decoder worker on the first frame.
676   if (ctx->frame_worker == NULL) {
677     res = init_decoder(ctx);
678     if (res != AOM_CODEC_OK) return res;
679   }
680 
681   const uint8_t *data_start = data;
682   const uint8_t *data_end = data + data_sz;
683 
684   if (ctx->is_annexb) {
685     // read the size of this temporal unit
686     size_t length_of_size;
687     uint64_t temporal_unit_size;
688     if (aom_uleb_decode(data_start, data_sz, &temporal_unit_size,
689                         &length_of_size) != 0) {
690       return AOM_CODEC_CORRUPT_FRAME;
691     }
692     data_start += length_of_size;
693     if (temporal_unit_size > (size_t)(data_end - data_start))
694       return AOM_CODEC_CORRUPT_FRAME;
695     data_end = data_start + temporal_unit_size;
696   }
697 
698   // Decode in serial mode.
699   while (data_start < data_end) {
700     uint64_t frame_size;
701     if (ctx->is_annexb) {
702       // read the size of this frame unit
703       size_t length_of_size;
704       if (aom_uleb_decode(data_start, (size_t)(data_end - data_start),
705                           &frame_size, &length_of_size) != 0) {
706         return AOM_CODEC_CORRUPT_FRAME;
707       }
708       data_start += length_of_size;
709       if (frame_size > (size_t)(data_end - data_start))
710         return AOM_CODEC_CORRUPT_FRAME;
711     } else {
712       frame_size = (uint64_t)(data_end - data_start);
713     }
714 
715     res = decode_one(ctx, &data_start, (size_t)frame_size, user_priv);
716     if (res != AOM_CODEC_OK) return res;
717 
718     // Allow extra zero bytes after the frame end
719     while (data_start < data_end) {
720       const uint8_t marker = data_start[0];
721       if (marker) break;
722       ++data_start;
723     }
724   }
725 
726   return res;
727 }
728 
729 typedef struct {
730   BufferPool *pool;
731   aom_codec_frame_buffer_t *fb;
732 } AllocCbParam;
733 
AllocWithGetFrameBufferCb(void * priv,size_t size)734 static void *AllocWithGetFrameBufferCb(void *priv, size_t size) {
735   AllocCbParam *param = (AllocCbParam *)priv;
736   if (param->pool->get_fb_cb(param->pool->cb_priv, size, param->fb) < 0)
737     return NULL;
738   if (param->fb->data == NULL || param->fb->size < size) return NULL;
739   return param->fb->data;
740 }
741 
742 // If grain_params->apply_grain is false, returns img. Otherwise, adds film
743 // grain to img, saves the result in grain_img, and returns grain_img.
add_grain_if_needed(aom_codec_alg_priv_t * ctx,aom_image_t * img,aom_image_t * grain_img,aom_film_grain_t * grain_params)744 static aom_image_t *add_grain_if_needed(aom_codec_alg_priv_t *ctx,
745                                         aom_image_t *img,
746                                         aom_image_t *grain_img,
747                                         aom_film_grain_t *grain_params) {
748   if (!grain_params->apply_grain) return img;
749 
750   const int w_even = ALIGN_POWER_OF_TWO_UNSIGNED(img->d_w, 1);
751   const int h_even = ALIGN_POWER_OF_TWO_UNSIGNED(img->d_h, 1);
752 
753   BufferPool *const pool = ctx->buffer_pool;
754   aom_codec_frame_buffer_t *fb =
755       &ctx->grain_image_frame_buffers[ctx->num_grain_image_frame_buffers];
756   AllocCbParam param;
757   param.pool = pool;
758   param.fb = fb;
759   if (!aom_img_alloc_with_cb(grain_img, img->fmt, w_even, h_even, 16,
760                              AllocWithGetFrameBufferCb, &param)) {
761     return NULL;
762   }
763 
764   grain_img->user_priv = img->user_priv;
765   grain_img->fb_priv = fb->priv;
766   if (av1_add_film_grain(grain_params, img, grain_img)) {
767     pool->release_fb_cb(pool->cb_priv, fb);
768     return NULL;
769   }
770 
771   ctx->num_grain_image_frame_buffers++;
772   return grain_img;
773 }
774 
775 // Copies and clears the metadata from AV1Decoder.
move_decoder_metadata_to_img(AV1Decoder * pbi,aom_image_t * img)776 static void move_decoder_metadata_to_img(AV1Decoder *pbi, aom_image_t *img) {
777   if (pbi->metadata && img) {
778     assert(!img->metadata);
779     img->metadata = pbi->metadata;
780     pbi->metadata = NULL;
781   }
782 }
783 
decoder_get_frame(aom_codec_alg_priv_t * ctx,aom_codec_iter_t * iter)784 static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
785                                       aom_codec_iter_t *iter) {
786   aom_image_t *img = NULL;
787 
788   if (!iter) {
789     return NULL;
790   }
791 
792   // To avoid having to allocate any extra storage, treat 'iter' as
793   // simply a pointer to an integer index
794   uintptr_t *index = (uintptr_t *)iter;
795 
796   if (ctx->frame_worker != NULL) {
797     const AVxWorkerInterface *const winterface = aom_get_worker_interface();
798     AVxWorker *const worker = ctx->frame_worker;
799     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
800     AV1Decoder *const pbi = frame_worker_data->pbi;
801     AV1_COMMON *const cm = &pbi->common;
802     CommonTileParams *const tiles = &cm->tiles;
803     // Wait for the frame from worker thread.
804     if (winterface->sync(worker)) {
805       // Check if worker has received any frames.
806       if (frame_worker_data->received_frame == 1) {
807         frame_worker_data->received_frame = 0;
808         check_resync(ctx, frame_worker_data->pbi);
809       }
810       YV12_BUFFER_CONFIG *sd;
811       aom_film_grain_t *grain_params;
812       if (av1_get_raw_frame(frame_worker_data->pbi, *index, &sd,
813                             &grain_params) == 0) {
814         RefCntBuffer *const output_frame_buf = pbi->output_frames[*index];
815         ctx->last_show_frame = output_frame_buf;
816         if (ctx->need_resync) return NULL;
817         aom_img_remove_metadata(&ctx->img);
818         yuvconfig2image(&ctx->img, sd, frame_worker_data->user_priv);
819         move_decoder_metadata_to_img(pbi, &ctx->img);
820 
821         if (!pbi->ext_tile_debug && tiles->large_scale) {
822           *index += 1;  // Advance the iterator to point to the next image
823           aom_img_remove_metadata(&ctx->img);
824           yuvconfig2image(&ctx->img, &pbi->tile_list_outbuf, NULL);
825           move_decoder_metadata_to_img(pbi, &ctx->img);
826           img = &ctx->img;
827           return img;
828         }
829 
830         const int num_planes = av1_num_planes(cm);
831         if (pbi->ext_tile_debug && tiles->single_tile_decoding &&
832             pbi->dec_tile_row >= 0) {
833           int tile_width, tile_height;
834           av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
835           const int tile_row = AOMMIN(pbi->dec_tile_row, tiles->rows - 1);
836           const int mi_row = tile_row * tile_height;
837           const int ssy = ctx->img.y_chroma_shift;
838           int plane;
839           ctx->img.planes[0] += mi_row * MI_SIZE * ctx->img.stride[0];
840           if (num_planes > 1) {
841             for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
842               ctx->img.planes[plane] +=
843                   mi_row * (MI_SIZE >> ssy) * ctx->img.stride[plane];
844             }
845           }
846           ctx->img.d_h =
847               AOMMIN(tile_height, cm->mi_params.mi_rows - mi_row) * MI_SIZE;
848         }
849 
850         if (pbi->ext_tile_debug && tiles->single_tile_decoding &&
851             pbi->dec_tile_col >= 0) {
852           int tile_width, tile_height;
853           av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
854           const int tile_col = AOMMIN(pbi->dec_tile_col, tiles->cols - 1);
855           const int mi_col = tile_col * tile_width;
856           const int ssx = ctx->img.x_chroma_shift;
857           const int is_hbd = (ctx->img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 1 : 0;
858           int plane;
859           ctx->img.planes[0] += mi_col * MI_SIZE * (1 + is_hbd);
860           if (num_planes > 1) {
861             for (plane = 1; plane < MAX_MB_PLANE; ++plane) {
862               ctx->img.planes[plane] +=
863                   mi_col * (MI_SIZE >> ssx) * (1 + is_hbd);
864             }
865           }
866           ctx->img.d_w =
867               AOMMIN(tile_width, cm->mi_params.mi_cols - mi_col) * MI_SIZE;
868         }
869 
870         ctx->img.fb_priv = output_frame_buf->raw_frame_buffer.priv;
871         img = &ctx->img;
872         img->temporal_id = output_frame_buf->temporal_id;
873         img->spatial_id = output_frame_buf->spatial_id;
874         if (pbi->skip_film_grain) grain_params->apply_grain = 0;
875         aom_image_t *res =
876             add_grain_if_needed(ctx, img, &ctx->image_with_grain, grain_params);
877         if (!res) {
878           aom_internal_error(&pbi->error, AOM_CODEC_CORRUPT_FRAME,
879                              "Grain systhesis failed\n");
880         }
881         *index += 1;  // Advance the iterator to point to the next image
882         return res;
883       }
884     } else {
885       // Decoding failed. Release the worker thread.
886       frame_worker_data->received_frame = 0;
887       ctx->need_resync = 1;
888       if (ctx->flushed != 1) return NULL;
889     }
890   }
891   return NULL;
892 }
893 
decoder_set_fb_fn(aom_codec_alg_priv_t * ctx,aom_get_frame_buffer_cb_fn_t cb_get,aom_release_frame_buffer_cb_fn_t cb_release,void * cb_priv)894 static aom_codec_err_t decoder_set_fb_fn(
895     aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
896     aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
897   if (cb_get == NULL || cb_release == NULL) {
898     return AOM_CODEC_INVALID_PARAM;
899   } else if (ctx->frame_worker == NULL) {
900     // If the decoder has already been initialized, do not accept changes to
901     // the frame buffer functions.
902     ctx->get_ext_fb_cb = cb_get;
903     ctx->release_ext_fb_cb = cb_release;
904     ctx->ext_priv = cb_priv;
905     return AOM_CODEC_OK;
906   }
907 
908   return AOM_CODEC_ERROR;
909 }
910 
ctrl_set_reference(aom_codec_alg_priv_t * ctx,va_list args)911 static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
912                                           va_list args) {
913   av1_ref_frame_t *const data = va_arg(args, av1_ref_frame_t *);
914 
915   if (data) {
916     av1_ref_frame_t *const frame = data;
917     YV12_BUFFER_CONFIG sd;
918     AVxWorker *const worker = ctx->frame_worker;
919     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
920     image2yuvconfig(&frame->img, &sd);
921     return av1_set_reference_dec(&frame_worker_data->pbi->common, frame->idx,
922                                  frame->use_external_ref, &sd);
923   } else {
924     return AOM_CODEC_INVALID_PARAM;
925   }
926 }
927 
ctrl_copy_reference(aom_codec_alg_priv_t * ctx,va_list args)928 static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
929                                            va_list args) {
930   const av1_ref_frame_t *const frame = va_arg(args, av1_ref_frame_t *);
931   if (frame) {
932     YV12_BUFFER_CONFIG sd;
933     AVxWorker *const worker = ctx->frame_worker;
934     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
935     image2yuvconfig(&frame->img, &sd);
936     return av1_copy_reference_dec(frame_worker_data->pbi, frame->idx, &sd);
937   } else {
938     return AOM_CODEC_INVALID_PARAM;
939   }
940 }
941 
ctrl_get_reference(aom_codec_alg_priv_t * ctx,va_list args)942 static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
943                                           va_list args) {
944   av1_ref_frame_t *data = va_arg(args, av1_ref_frame_t *);
945   if (data) {
946     YV12_BUFFER_CONFIG *fb;
947     AVxWorker *const worker = ctx->frame_worker;
948     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
949     fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
950     if (fb == NULL) return AOM_CODEC_ERROR;
951     yuvconfig2image(&data->img, fb, NULL);
952     return AOM_CODEC_OK;
953   } else {
954     return AOM_CODEC_INVALID_PARAM;
955   }
956 }
957 
ctrl_get_new_frame_image(aom_codec_alg_priv_t * ctx,va_list args)958 static aom_codec_err_t ctrl_get_new_frame_image(aom_codec_alg_priv_t *ctx,
959                                                 va_list args) {
960   aom_image_t *new_img = va_arg(args, aom_image_t *);
961   if (new_img) {
962     YV12_BUFFER_CONFIG new_frame;
963     AVxWorker *const worker = ctx->frame_worker;
964     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
965 
966     if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
967       yuvconfig2image(new_img, &new_frame, NULL);
968       return AOM_CODEC_OK;
969     } else {
970       return AOM_CODEC_ERROR;
971     }
972   } else {
973     return AOM_CODEC_INVALID_PARAM;
974   }
975 }
976 
ctrl_copy_new_frame_image(aom_codec_alg_priv_t * ctx,va_list args)977 static aom_codec_err_t ctrl_copy_new_frame_image(aom_codec_alg_priv_t *ctx,
978                                                  va_list args) {
979   aom_image_t *img = va_arg(args, aom_image_t *);
980   if (img) {
981     YV12_BUFFER_CONFIG new_frame;
982     AVxWorker *const worker = ctx->frame_worker;
983     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
984 
985     if (av1_get_frame_to_show(frame_worker_data->pbi, &new_frame) == 0) {
986       YV12_BUFFER_CONFIG sd;
987       image2yuvconfig(img, &sd);
988       return av1_copy_new_frame_dec(&frame_worker_data->pbi->common, &new_frame,
989                                     &sd);
990     } else {
991       return AOM_CODEC_ERROR;
992     }
993   } else {
994     return AOM_CODEC_INVALID_PARAM;
995   }
996 }
997 
ctrl_get_last_ref_updates(aom_codec_alg_priv_t * ctx,va_list args)998 static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
999                                                  va_list args) {
1000   int *const update_info = va_arg(args, int *);
1001 
1002   if (update_info) {
1003     if (ctx->frame_worker) {
1004       AVxWorker *const worker = ctx->frame_worker;
1005       FrameWorkerData *const frame_worker_data =
1006           (FrameWorkerData *)worker->data1;
1007       *update_info =
1008           frame_worker_data->pbi->common.current_frame.refresh_frame_flags;
1009       return AOM_CODEC_OK;
1010     } else {
1011       return AOM_CODEC_ERROR;
1012     }
1013   }
1014 
1015   return AOM_CODEC_INVALID_PARAM;
1016 }
1017 
ctrl_get_last_quantizer(aom_codec_alg_priv_t * ctx,va_list args)1018 static aom_codec_err_t ctrl_get_last_quantizer(aom_codec_alg_priv_t *ctx,
1019                                                va_list args) {
1020   int *const arg = va_arg(args, int *);
1021   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1022   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1023   *arg = ((FrameWorkerData *)ctx->frame_worker->data1)
1024              ->pbi->common.quant_params.base_qindex;
1025   return AOM_CODEC_OK;
1026 }
1027 
ctrl_get_fwd_kf_value(aom_codec_alg_priv_t * ctx,va_list args)1028 static aom_codec_err_t ctrl_get_fwd_kf_value(aom_codec_alg_priv_t *ctx,
1029                                              va_list args) {
1030   int *const arg = va_arg(args, int *);
1031   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1032   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1033   *arg = ((FrameWorkerData *)ctx->frame_worker->data1)->pbi->is_fwd_kf_present;
1034   return AOM_CODEC_OK;
1035 }
1036 
ctrl_get_altref_present(aom_codec_alg_priv_t * ctx,va_list args)1037 static aom_codec_err_t ctrl_get_altref_present(aom_codec_alg_priv_t *ctx,
1038                                                va_list args) {
1039   int *const arg = va_arg(args, int *);
1040   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1041   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1042   *arg =
1043       ((FrameWorkerData *)ctx->frame_worker->data1)->pbi->is_arf_frame_present;
1044   return AOM_CODEC_OK;
1045 }
1046 
ctrl_get_frame_flags(aom_codec_alg_priv_t * ctx,va_list args)1047 static aom_codec_err_t ctrl_get_frame_flags(aom_codec_alg_priv_t *ctx,
1048                                             va_list args) {
1049   int *const arg = va_arg(args, int *);
1050   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1051   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1052   AV1Decoder *pbi = ((FrameWorkerData *)ctx->frame_worker->data1)->pbi;
1053   *arg = 0;
1054   switch (pbi->common.current_frame.frame_type) {
1055     case KEY_FRAME:
1056       *arg |= AOM_FRAME_IS_KEY;
1057       *arg |= AOM_FRAME_IS_INTRAONLY;
1058       if (!pbi->common.show_frame) {
1059         *arg |= AOM_FRAME_IS_DELAYED_RANDOM_ACCESS_POINT;
1060       }
1061       break;
1062     case INTRA_ONLY_FRAME: *arg |= AOM_FRAME_IS_INTRAONLY; break;
1063     case S_FRAME: *arg |= AOM_FRAME_IS_SWITCH; break;
1064   }
1065   if (pbi->common.features.error_resilient_mode) {
1066     *arg |= AOM_FRAME_IS_ERROR_RESILIENT;
1067   }
1068   return AOM_CODEC_OK;
1069 }
1070 
ctrl_get_tile_info(aom_codec_alg_priv_t * ctx,va_list args)1071 static aom_codec_err_t ctrl_get_tile_info(aom_codec_alg_priv_t *ctx,
1072                                           va_list args) {
1073   aom_tile_info *const tile_info = va_arg(args, aom_tile_info *);
1074 
1075   if (tile_info) {
1076     if (ctx->frame_worker) {
1077       AVxWorker *const worker = ctx->frame_worker;
1078       FrameWorkerData *const frame_worker_data =
1079           (FrameWorkerData *)worker->data1;
1080       const AV1Decoder *pbi = frame_worker_data->pbi;
1081       const CommonTileParams *tiles = &pbi->common.tiles;
1082 
1083       int tile_rows = tiles->rows;
1084       int tile_cols = tiles->cols;
1085 
1086       if (tiles->uniform_spacing) {
1087         tile_info->tile_rows = 1 << tiles->log2_rows;
1088         tile_info->tile_columns = 1 << tiles->log2_cols;
1089       } else {
1090         tile_info->tile_rows = tile_rows;
1091         tile_info->tile_columns = tile_cols;
1092       }
1093 
1094       for (int tile_col = 1; tile_col <= tile_cols; tile_col++) {
1095         tile_info->tile_widths[tile_col - 1] =
1096             tiles->col_start_sb[tile_col] - tiles->col_start_sb[tile_col - 1];
1097       }
1098 
1099       for (int tile_row = 1; tile_row <= tile_rows; tile_row++) {
1100         tile_info->tile_heights[tile_row - 1] =
1101             tiles->row_start_sb[tile_row] - tiles->row_start_sb[tile_row - 1];
1102       }
1103       tile_info->num_tile_groups = pbi->num_tile_groups;
1104       return AOM_CODEC_OK;
1105     } else {
1106       return AOM_CODEC_ERROR;
1107     }
1108   }
1109 
1110   return AOM_CODEC_INVALID_PARAM;
1111 }
1112 
ctrl_get_screen_content_tools_info(aom_codec_alg_priv_t * ctx,va_list args)1113 static aom_codec_err_t ctrl_get_screen_content_tools_info(
1114     aom_codec_alg_priv_t *ctx, va_list args) {
1115   aom_screen_content_tools_info *const sc_info =
1116       va_arg(args, aom_screen_content_tools_info *);
1117   if (sc_info) {
1118     if (ctx->frame_worker) {
1119       AVxWorker *const worker = ctx->frame_worker;
1120       FrameWorkerData *const frame_worker_data =
1121           (FrameWorkerData *)worker->data1;
1122       const AV1Decoder *pbi = frame_worker_data->pbi;
1123       sc_info->allow_screen_content_tools =
1124           pbi->common.features.allow_screen_content_tools;
1125       sc_info->allow_intrabc = pbi->common.features.allow_intrabc;
1126       sc_info->force_integer_mv =
1127           (int)pbi->common.features.cur_frame_force_integer_mv;
1128       return AOM_CODEC_OK;
1129     } else {
1130       return AOM_CODEC_ERROR;
1131     }
1132   }
1133   return AOM_CODEC_INVALID_PARAM;
1134 }
1135 
ctrl_get_still_picture(aom_codec_alg_priv_t * ctx,va_list args)1136 static aom_codec_err_t ctrl_get_still_picture(aom_codec_alg_priv_t *ctx,
1137                                               va_list args) {
1138   aom_still_picture_info *const still_picture_info =
1139       va_arg(args, aom_still_picture_info *);
1140   if (still_picture_info) {
1141     if (ctx->frame_worker) {
1142       AVxWorker *const worker = ctx->frame_worker;
1143       FrameWorkerData *const frame_worker_data =
1144           (FrameWorkerData *)worker->data1;
1145       const AV1Decoder *pbi = frame_worker_data->pbi;
1146       still_picture_info->is_still_picture = (int)pbi->seq_params.still_picture;
1147       still_picture_info->is_reduced_still_picture_hdr =
1148           (int)(pbi->seq_params.reduced_still_picture_hdr);
1149       return AOM_CODEC_OK;
1150     } else {
1151       return AOM_CODEC_ERROR;
1152     }
1153   }
1154   return AOM_CODEC_INVALID_PARAM;
1155 }
1156 
ctrl_get_sb_size(aom_codec_alg_priv_t * ctx,va_list args)1157 static aom_codec_err_t ctrl_get_sb_size(aom_codec_alg_priv_t *ctx,
1158                                         va_list args) {
1159   aom_superblock_size_t *const sb_size = va_arg(args, aom_superblock_size_t *);
1160   if (sb_size) {
1161     if (ctx->frame_worker) {
1162       AVxWorker *const worker = ctx->frame_worker;
1163       FrameWorkerData *const frame_worker_data =
1164           (FrameWorkerData *)worker->data1;
1165       const AV1Decoder *pbi = frame_worker_data->pbi;
1166       if (pbi->seq_params.sb_size == BLOCK_128X128) {
1167         *sb_size = AOM_SUPERBLOCK_SIZE_128X128;
1168       } else {
1169         *sb_size = AOM_SUPERBLOCK_SIZE_64X64;
1170       }
1171       return AOM_CODEC_OK;
1172     } else {
1173       return AOM_CODEC_ERROR;
1174     }
1175   }
1176   return AOM_CODEC_INVALID_PARAM;
1177 }
1178 
ctrl_get_show_existing_frame_flag(aom_codec_alg_priv_t * ctx,va_list args)1179 static aom_codec_err_t ctrl_get_show_existing_frame_flag(
1180     aom_codec_alg_priv_t *ctx, va_list args) {
1181   int *const arg = va_arg(args, int *);
1182   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1183   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1184   *arg = ((FrameWorkerData *)ctx->frame_worker->data1)
1185              ->pbi->common.show_existing_frame;
1186   return AOM_CODEC_OK;
1187 }
1188 
ctrl_get_s_frame_info(aom_codec_alg_priv_t * ctx,va_list args)1189 static aom_codec_err_t ctrl_get_s_frame_info(aom_codec_alg_priv_t *ctx,
1190                                              va_list args) {
1191   aom_s_frame_info *const s_frame_info = va_arg(args, aom_s_frame_info *);
1192   if (s_frame_info) {
1193     if (ctx->frame_worker) {
1194       AVxWorker *const worker = ctx->frame_worker;
1195       FrameWorkerData *const frame_worker_data =
1196           (FrameWorkerData *)worker->data1;
1197       const AV1Decoder *pbi = frame_worker_data->pbi;
1198       s_frame_info->is_s_frame = pbi->sframe_info.is_s_frame;
1199       s_frame_info->is_s_frame_at_altref =
1200           pbi->sframe_info.is_s_frame_at_altref;
1201       return AOM_CODEC_OK;
1202     } else {
1203       return AOM_CODEC_ERROR;
1204     }
1205   }
1206   return AOM_CODEC_INVALID_PARAM;
1207 }
1208 
ctrl_get_frame_corrupted(aom_codec_alg_priv_t * ctx,va_list args)1209 static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
1210                                                 va_list args) {
1211   int *corrupted = va_arg(args, int *);
1212 
1213   if (corrupted) {
1214     if (ctx->frame_worker) {
1215       AVxWorker *const worker = ctx->frame_worker;
1216       FrameWorkerData *const frame_worker_data =
1217           (FrameWorkerData *)worker->data1;
1218       AV1Decoder *const pbi = frame_worker_data->pbi;
1219       if (pbi->seen_frame_header && pbi->num_output_frames == 0)
1220         return AOM_CODEC_ERROR;
1221       if (ctx->last_show_frame != NULL)
1222         *corrupted = ctx->last_show_frame->buf.corrupted;
1223       return AOM_CODEC_OK;
1224     } else {
1225       return AOM_CODEC_ERROR;
1226     }
1227   }
1228 
1229   return AOM_CODEC_INVALID_PARAM;
1230 }
1231 
ctrl_get_frame_size(aom_codec_alg_priv_t * ctx,va_list args)1232 static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
1233                                            va_list args) {
1234   int *const frame_size = va_arg(args, int *);
1235 
1236   if (frame_size) {
1237     if (ctx->frame_worker) {
1238       AVxWorker *const worker = ctx->frame_worker;
1239       FrameWorkerData *const frame_worker_data =
1240           (FrameWorkerData *)worker->data1;
1241       const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
1242       frame_size[0] = cm->width;
1243       frame_size[1] = cm->height;
1244       return AOM_CODEC_OK;
1245     } else {
1246       return AOM_CODEC_ERROR;
1247     }
1248   }
1249 
1250   return AOM_CODEC_INVALID_PARAM;
1251 }
1252 
ctrl_get_frame_header_info(aom_codec_alg_priv_t * ctx,va_list args)1253 static aom_codec_err_t ctrl_get_frame_header_info(aom_codec_alg_priv_t *ctx,
1254                                                   va_list args) {
1255   aom_tile_data *const frame_header_info = va_arg(args, aom_tile_data *);
1256 
1257   if (frame_header_info) {
1258     if (ctx->frame_worker) {
1259       AVxWorker *const worker = ctx->frame_worker;
1260       FrameWorkerData *const frame_worker_data =
1261           (FrameWorkerData *)worker->data1;
1262       const AV1Decoder *pbi = frame_worker_data->pbi;
1263       frame_header_info->coded_tile_data_size = pbi->obu_size_hdr.size;
1264       frame_header_info->coded_tile_data = pbi->obu_size_hdr.data;
1265       frame_header_info->extra_size = pbi->frame_header_size;
1266       return AOM_CODEC_OK;
1267     } else {
1268       return AOM_CODEC_ERROR;
1269     }
1270   }
1271 
1272   return AOM_CODEC_INVALID_PARAM;
1273 }
1274 
ctrl_get_tile_data(aom_codec_alg_priv_t * ctx,va_list args)1275 static aom_codec_err_t ctrl_get_tile_data(aom_codec_alg_priv_t *ctx,
1276                                           va_list args) {
1277   aom_tile_data *const tile_data = va_arg(args, aom_tile_data *);
1278 
1279   if (tile_data) {
1280     if (ctx->frame_worker) {
1281       AVxWorker *const worker = ctx->frame_worker;
1282       FrameWorkerData *const frame_worker_data =
1283           (FrameWorkerData *)worker->data1;
1284       const AV1Decoder *pbi = frame_worker_data->pbi;
1285       tile_data->coded_tile_data_size =
1286           pbi->tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].size;
1287       tile_data->coded_tile_data =
1288           pbi->tile_buffers[pbi->dec_tile_row][pbi->dec_tile_col].data;
1289       return AOM_CODEC_OK;
1290     } else {
1291       return AOM_CODEC_ERROR;
1292     }
1293   }
1294 
1295   return AOM_CODEC_INVALID_PARAM;
1296 }
1297 
ctrl_set_ext_ref_ptr(aom_codec_alg_priv_t * ctx,va_list args)1298 static aom_codec_err_t ctrl_set_ext_ref_ptr(aom_codec_alg_priv_t *ctx,
1299                                             va_list args) {
1300   av1_ext_ref_frame_t *const data = va_arg(args, av1_ext_ref_frame_t *);
1301 
1302   if (data) {
1303     av1_ext_ref_frame_t *const ext_frames = data;
1304     ctx->ext_refs.num = ext_frames->num;
1305     for (int i = 0; i < ctx->ext_refs.num; i++) {
1306       image2yuvconfig(ext_frames->img++, &ctx->ext_refs.refs[i]);
1307     }
1308     return AOM_CODEC_OK;
1309   } else {
1310     return AOM_CODEC_INVALID_PARAM;
1311   }
1312 }
1313 
ctrl_get_render_size(aom_codec_alg_priv_t * ctx,va_list args)1314 static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
1315                                             va_list args) {
1316   int *const render_size = va_arg(args, int *);
1317 
1318   if (render_size) {
1319     if (ctx->frame_worker) {
1320       AVxWorker *const worker = ctx->frame_worker;
1321       FrameWorkerData *const frame_worker_data =
1322           (FrameWorkerData *)worker->data1;
1323       const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
1324       render_size[0] = cm->render_width;
1325       render_size[1] = cm->render_height;
1326       return AOM_CODEC_OK;
1327     } else {
1328       return AOM_CODEC_ERROR;
1329     }
1330   }
1331 
1332   return AOM_CODEC_INVALID_PARAM;
1333 }
1334 
ctrl_get_bit_depth(aom_codec_alg_priv_t * ctx,va_list args)1335 static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
1336                                           va_list args) {
1337   unsigned int *const bit_depth = va_arg(args, unsigned int *);
1338   AVxWorker *const worker = ctx->frame_worker;
1339 
1340   if (bit_depth) {
1341     if (worker) {
1342       FrameWorkerData *const frame_worker_data =
1343           (FrameWorkerData *)worker->data1;
1344       const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
1345       *bit_depth = cm->seq_params->bit_depth;
1346       return AOM_CODEC_OK;
1347     } else {
1348       return AOM_CODEC_ERROR;
1349     }
1350   }
1351 
1352   return AOM_CODEC_INVALID_PARAM;
1353 }
1354 
get_img_format(int subsampling_x,int subsampling_y,int use_highbitdepth)1355 static aom_img_fmt_t get_img_format(int subsampling_x, int subsampling_y,
1356                                     int use_highbitdepth) {
1357   aom_img_fmt_t fmt = 0;
1358 
1359   if (subsampling_x == 0 && subsampling_y == 0)
1360     fmt = AOM_IMG_FMT_I444;
1361   else if (subsampling_x == 1 && subsampling_y == 0)
1362     fmt = AOM_IMG_FMT_I422;
1363   else if (subsampling_x == 1 && subsampling_y == 1)
1364     fmt = AOM_IMG_FMT_I420;
1365 
1366   if (use_highbitdepth) fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
1367   return fmt;
1368 }
1369 
ctrl_get_img_format(aom_codec_alg_priv_t * ctx,va_list args)1370 static aom_codec_err_t ctrl_get_img_format(aom_codec_alg_priv_t *ctx,
1371                                            va_list args) {
1372   aom_img_fmt_t *const img_fmt = va_arg(args, aom_img_fmt_t *);
1373   AVxWorker *const worker = ctx->frame_worker;
1374 
1375   if (img_fmt) {
1376     if (worker) {
1377       FrameWorkerData *const frame_worker_data =
1378           (FrameWorkerData *)worker->data1;
1379       const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
1380 
1381       *img_fmt = get_img_format(cm->seq_params->subsampling_x,
1382                                 cm->seq_params->subsampling_y,
1383                                 cm->seq_params->use_highbitdepth);
1384       return AOM_CODEC_OK;
1385     } else {
1386       return AOM_CODEC_ERROR;
1387     }
1388   }
1389 
1390   return AOM_CODEC_INVALID_PARAM;
1391 }
1392 
ctrl_get_tile_size(aom_codec_alg_priv_t * ctx,va_list args)1393 static aom_codec_err_t ctrl_get_tile_size(aom_codec_alg_priv_t *ctx,
1394                                           va_list args) {
1395   unsigned int *const tile_size = va_arg(args, unsigned int *);
1396   AVxWorker *const worker = ctx->frame_worker;
1397 
1398   if (tile_size) {
1399     if (worker) {
1400       FrameWorkerData *const frame_worker_data =
1401           (FrameWorkerData *)worker->data1;
1402       const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
1403       int tile_width, tile_height;
1404       av1_get_uniform_tile_size(cm, &tile_width, &tile_height);
1405       *tile_size = ((tile_width * MI_SIZE) << 16) + tile_height * MI_SIZE;
1406       return AOM_CODEC_OK;
1407     } else {
1408       return AOM_CODEC_ERROR;
1409     }
1410   }
1411   return AOM_CODEC_INVALID_PARAM;
1412 }
1413 
ctrl_get_tile_count(aom_codec_alg_priv_t * ctx,va_list args)1414 static aom_codec_err_t ctrl_get_tile_count(aom_codec_alg_priv_t *ctx,
1415                                            va_list args) {
1416   unsigned int *const tile_count = va_arg(args, unsigned int *);
1417 
1418   if (tile_count) {
1419     AVxWorker *const worker = ctx->frame_worker;
1420     if (worker) {
1421       FrameWorkerData *const frame_worker_data =
1422           (FrameWorkerData *)worker->data1;
1423       *tile_count = frame_worker_data->pbi->tile_count_minus_1 + 1;
1424       return AOM_CODEC_OK;
1425     } else {
1426       return AOM_CODEC_ERROR;
1427     }
1428   }
1429   return AOM_CODEC_INVALID_PARAM;
1430 }
1431 
ctrl_get_base_q_idx(aom_codec_alg_priv_t * ctx,va_list args)1432 static aom_codec_err_t ctrl_get_base_q_idx(aom_codec_alg_priv_t *ctx,
1433                                            va_list args) {
1434   int *const arg = va_arg(args, int *);
1435   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1436   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1437   FrameWorkerData *const frame_worker_data =
1438       (FrameWorkerData *)ctx->frame_worker->data1;
1439   *arg = frame_worker_data->pbi->common.quant_params.base_qindex;
1440   return AOM_CODEC_OK;
1441 }
1442 
ctrl_get_show_frame_flag(aom_codec_alg_priv_t * ctx,va_list args)1443 static aom_codec_err_t ctrl_get_show_frame_flag(aom_codec_alg_priv_t *ctx,
1444                                                 va_list args) {
1445   int *const arg = va_arg(args, int *);
1446   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1447   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1448   FrameWorkerData *const frame_worker_data =
1449       (FrameWorkerData *)ctx->frame_worker->data1;
1450   *arg = frame_worker_data->pbi->common.show_frame;
1451   return AOM_CODEC_OK;
1452 }
1453 
ctrl_get_order_hint(aom_codec_alg_priv_t * ctx,va_list args)1454 static aom_codec_err_t ctrl_get_order_hint(aom_codec_alg_priv_t *ctx,
1455                                            va_list args) {
1456   unsigned int *const arg = va_arg(args, unsigned int *);
1457   if (arg == NULL) return AOM_CODEC_INVALID_PARAM;
1458   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1459   FrameWorkerData *const frame_worker_data =
1460       (FrameWorkerData *)ctx->frame_worker->data1;
1461   *arg = frame_worker_data->pbi->common.current_frame.order_hint;
1462   return AOM_CODEC_OK;
1463 }
1464 
ctrl_get_mi_info(aom_codec_alg_priv_t * ctx,va_list args)1465 static aom_codec_err_t ctrl_get_mi_info(aom_codec_alg_priv_t *ctx,
1466                                         va_list args) {
1467   int mi_row = va_arg(args, int);
1468   int mi_col = va_arg(args, int);
1469   MB_MODE_INFO *mi = va_arg(args, MB_MODE_INFO *);
1470   if (mi == NULL) return AOM_CODEC_INVALID_PARAM;
1471   if (ctx->frame_worker == NULL) return AOM_CODEC_ERROR;
1472   FrameWorkerData *const frame_worker_data =
1473       (FrameWorkerData *)ctx->frame_worker->data1;
1474   if (frame_worker_data == NULL) return AOM_CODEC_ERROR;
1475 
1476   AV1_COMMON *cm = &frame_worker_data->pbi->common;
1477   const int mi_rows = cm->mi_params.mi_rows;
1478   const int mi_cols = cm->mi_params.mi_cols;
1479   const int mi_stride = cm->mi_params.mi_stride;
1480   const int offset = mi_row * mi_stride + mi_col;
1481 
1482   if (mi_row < 0 || mi_row >= mi_rows || mi_col < 0 || mi_col >= mi_cols) {
1483     return AOM_CODEC_INVALID_PARAM;
1484   }
1485 
1486   memcpy(mi, cm->mi_params.mi_grid_base[offset], sizeof(*mi));
1487 
1488   return AOM_CODEC_OK;
1489 }
1490 
ctrl_set_invert_tile_order(aom_codec_alg_priv_t * ctx,va_list args)1491 static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
1492                                                   va_list args) {
1493   ctx->invert_tile_order = va_arg(args, int);
1494   return AOM_CODEC_OK;
1495 }
1496 
ctrl_set_byte_alignment(aom_codec_alg_priv_t * ctx,va_list args)1497 static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
1498                                                va_list args) {
1499   const int legacy_byte_alignment = 0;
1500   const int min_byte_alignment = 32;
1501   const int max_byte_alignment = 1024;
1502   const int byte_alignment = va_arg(args, int);
1503 
1504   if (byte_alignment != legacy_byte_alignment &&
1505       (byte_alignment < min_byte_alignment ||
1506        byte_alignment > max_byte_alignment ||
1507        (byte_alignment & (byte_alignment - 1)) != 0))
1508     return AOM_CODEC_INVALID_PARAM;
1509 
1510   ctx->byte_alignment = byte_alignment;
1511   if (ctx->frame_worker) {
1512     AVxWorker *const worker = ctx->frame_worker;
1513     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1514     frame_worker_data->pbi->common.features.byte_alignment = byte_alignment;
1515   }
1516   return AOM_CODEC_OK;
1517 }
1518 
ctrl_set_skip_loop_filter(aom_codec_alg_priv_t * ctx,va_list args)1519 static aom_codec_err_t ctrl_set_skip_loop_filter(aom_codec_alg_priv_t *ctx,
1520                                                  va_list args) {
1521   ctx->skip_loop_filter = va_arg(args, int);
1522 
1523   if (ctx->frame_worker) {
1524     AVxWorker *const worker = ctx->frame_worker;
1525     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1526     frame_worker_data->pbi->skip_loop_filter = ctx->skip_loop_filter;
1527   }
1528 
1529   return AOM_CODEC_OK;
1530 }
1531 
ctrl_set_skip_film_grain(aom_codec_alg_priv_t * ctx,va_list args)1532 static aom_codec_err_t ctrl_set_skip_film_grain(aom_codec_alg_priv_t *ctx,
1533                                                 va_list args) {
1534   ctx->skip_film_grain = va_arg(args, int);
1535 
1536   if (ctx->frame_worker) {
1537     AVxWorker *const worker = ctx->frame_worker;
1538     FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1539     frame_worker_data->pbi->skip_film_grain = ctx->skip_film_grain;
1540   }
1541 
1542   return AOM_CODEC_OK;
1543 }
1544 
ctrl_get_accounting(aom_codec_alg_priv_t * ctx,va_list args)1545 static aom_codec_err_t ctrl_get_accounting(aom_codec_alg_priv_t *ctx,
1546                                            va_list args) {
1547 #if !CONFIG_ACCOUNTING
1548   (void)ctx;
1549   (void)args;
1550   return AOM_CODEC_INCAPABLE;
1551 #else
1552   Accounting **acct = va_arg(args, Accounting **);
1553 
1554   if (acct) {
1555     if (ctx->frame_worker) {
1556       AVxWorker *const worker = ctx->frame_worker;
1557       FrameWorkerData *const frame_worker_data =
1558           (FrameWorkerData *)worker->data1;
1559       AV1Decoder *pbi = frame_worker_data->pbi;
1560       *acct = &pbi->accounting;
1561       return AOM_CODEC_OK;
1562     } else {
1563       return AOM_CODEC_ERROR;
1564     }
1565   }
1566 
1567   return AOM_CODEC_INVALID_PARAM;
1568 #endif
1569 }
1570 
ctrl_set_decode_tile_row(aom_codec_alg_priv_t * ctx,va_list args)1571 static aom_codec_err_t ctrl_set_decode_tile_row(aom_codec_alg_priv_t *ctx,
1572                                                 va_list args) {
1573   ctx->decode_tile_row = va_arg(args, int);
1574   return AOM_CODEC_OK;
1575 }
1576 
ctrl_set_decode_tile_col(aom_codec_alg_priv_t * ctx,va_list args)1577 static aom_codec_err_t ctrl_set_decode_tile_col(aom_codec_alg_priv_t *ctx,
1578                                                 va_list args) {
1579   ctx->decode_tile_col = va_arg(args, int);
1580   return AOM_CODEC_OK;
1581 }
1582 
ctrl_set_tile_mode(aom_codec_alg_priv_t * ctx,va_list args)1583 static aom_codec_err_t ctrl_set_tile_mode(aom_codec_alg_priv_t *ctx,
1584                                           va_list args) {
1585   ctx->tile_mode = va_arg(args, unsigned int);
1586   return AOM_CODEC_OK;
1587 }
1588 
ctrl_set_is_annexb(aom_codec_alg_priv_t * ctx,va_list args)1589 static aom_codec_err_t ctrl_set_is_annexb(aom_codec_alg_priv_t *ctx,
1590                                           va_list args) {
1591   ctx->is_annexb = va_arg(args, unsigned int);
1592   return AOM_CODEC_OK;
1593 }
1594 
ctrl_set_operating_point(aom_codec_alg_priv_t * ctx,va_list args)1595 static aom_codec_err_t ctrl_set_operating_point(aom_codec_alg_priv_t *ctx,
1596                                                 va_list args) {
1597   ctx->operating_point = va_arg(args, int);
1598   return AOM_CODEC_OK;
1599 }
1600 
ctrl_set_output_all_layers(aom_codec_alg_priv_t * ctx,va_list args)1601 static aom_codec_err_t ctrl_set_output_all_layers(aom_codec_alg_priv_t *ctx,
1602                                                   va_list args) {
1603   ctx->output_all_layers = va_arg(args, int);
1604   return AOM_CODEC_OK;
1605 }
1606 
ctrl_set_inspection_callback(aom_codec_alg_priv_t * ctx,va_list args)1607 static aom_codec_err_t ctrl_set_inspection_callback(aom_codec_alg_priv_t *ctx,
1608                                                     va_list args) {
1609 #if !CONFIG_INSPECTION
1610   (void)ctx;
1611   (void)args;
1612   return AOM_CODEC_INCAPABLE;
1613 #else
1614   aom_inspect_init *init = va_arg(args, aom_inspect_init *);
1615   ctx->inspect_cb = init->inspect_cb;
1616   ctx->inspect_ctx = init->inspect_ctx;
1617   return AOM_CODEC_OK;
1618 #endif
1619 }
1620 
ctrl_ext_tile_debug(aom_codec_alg_priv_t * ctx,va_list args)1621 static aom_codec_err_t ctrl_ext_tile_debug(aom_codec_alg_priv_t *ctx,
1622                                            va_list args) {
1623   ctx->ext_tile_debug = va_arg(args, int);
1624   return AOM_CODEC_OK;
1625 }
1626 
ctrl_set_row_mt(aom_codec_alg_priv_t * ctx,va_list args)1627 static aom_codec_err_t ctrl_set_row_mt(aom_codec_alg_priv_t *ctx,
1628                                        va_list args) {
1629   ctx->row_mt = va_arg(args, unsigned int);
1630   return AOM_CODEC_OK;
1631 }
1632 
1633 static aom_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
1634   { AV1_COPY_REFERENCE, ctrl_copy_reference },
1635 
1636   // Setters
1637   { AV1_SET_REFERENCE, ctrl_set_reference },
1638   { AV1_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
1639   { AV1_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
1640   { AV1_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
1641   { AV1_SET_DECODE_TILE_ROW, ctrl_set_decode_tile_row },
1642   { AV1_SET_DECODE_TILE_COL, ctrl_set_decode_tile_col },
1643   { AV1_SET_TILE_MODE, ctrl_set_tile_mode },
1644   { AV1D_SET_IS_ANNEXB, ctrl_set_is_annexb },
1645   { AV1D_SET_OPERATING_POINT, ctrl_set_operating_point },
1646   { AV1D_SET_OUTPUT_ALL_LAYERS, ctrl_set_output_all_layers },
1647   { AV1_SET_INSPECTION_CALLBACK, ctrl_set_inspection_callback },
1648   { AV1D_EXT_TILE_DEBUG, ctrl_ext_tile_debug },
1649   { AV1D_SET_ROW_MT, ctrl_set_row_mt },
1650   { AV1D_SET_EXT_REF_PTR, ctrl_set_ext_ref_ptr },
1651   { AV1D_SET_SKIP_FILM_GRAIN, ctrl_set_skip_film_grain },
1652 
1653   // Getters
1654   { AOMD_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
1655   { AOMD_GET_LAST_QUANTIZER, ctrl_get_last_quantizer },
1656   { AOMD_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
1657   { AV1D_GET_BIT_DEPTH, ctrl_get_bit_depth },
1658   { AV1D_GET_IMG_FORMAT, ctrl_get_img_format },
1659   { AV1D_GET_TILE_SIZE, ctrl_get_tile_size },
1660   { AV1D_GET_TILE_COUNT, ctrl_get_tile_count },
1661   { AV1D_GET_DISPLAY_SIZE, ctrl_get_render_size },
1662   { AV1D_GET_FRAME_SIZE, ctrl_get_frame_size },
1663   { AV1_GET_ACCOUNTING, ctrl_get_accounting },
1664   { AV1_GET_NEW_FRAME_IMAGE, ctrl_get_new_frame_image },
1665   { AV1_COPY_NEW_FRAME_IMAGE, ctrl_copy_new_frame_image },
1666   { AV1_GET_REFERENCE, ctrl_get_reference },
1667   { AV1D_GET_FRAME_HEADER_INFO, ctrl_get_frame_header_info },
1668   { AV1D_GET_TILE_DATA, ctrl_get_tile_data },
1669   { AOMD_GET_FWD_KF_PRESENT, ctrl_get_fwd_kf_value },
1670   { AOMD_GET_ALTREF_PRESENT, ctrl_get_altref_present },
1671   { AOMD_GET_FRAME_FLAGS, ctrl_get_frame_flags },
1672   { AOMD_GET_TILE_INFO, ctrl_get_tile_info },
1673   { AOMD_GET_SCREEN_CONTENT_TOOLS_INFO, ctrl_get_screen_content_tools_info },
1674   { AOMD_GET_STILL_PICTURE, ctrl_get_still_picture },
1675   { AOMD_GET_SB_SIZE, ctrl_get_sb_size },
1676   { AOMD_GET_SHOW_EXISTING_FRAME_FLAG, ctrl_get_show_existing_frame_flag },
1677   { AOMD_GET_S_FRAME_INFO, ctrl_get_s_frame_info },
1678   { AOMD_GET_SHOW_FRAME_FLAG, ctrl_get_show_frame_flag },
1679   { AOMD_GET_BASE_Q_IDX, ctrl_get_base_q_idx },
1680   { AOMD_GET_ORDER_HINT, ctrl_get_order_hint },
1681   { AV1D_GET_MI_INFO, ctrl_get_mi_info },
1682   CTRL_MAP_END,
1683 };
1684 
1685 // This data structure and function are exported in aom/aomdx.h
1686 #ifndef VERSION_STRING
1687 #define VERSION_STRING
1688 #endif
1689 aom_codec_iface_t aom_codec_av1_dx_algo = {
1690   "AOMedia Project AV1 Decoder" VERSION_STRING,
1691   AOM_CODEC_INTERNAL_ABI_VERSION,
1692   AOM_CODEC_CAP_DECODER |
1693       AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER,  // aom_codec_caps_t
1694   decoder_init,                             // aom_codec_init_fn_t
1695   decoder_destroy,                          // aom_codec_destroy_fn_t
1696   decoder_ctrl_maps,                        // aom_codec_ctrl_fn_map_t
1697   {
1698       // NOLINT
1699       decoder_peek_si,    // aom_codec_peek_si_fn_t
1700       decoder_get_si,     // aom_codec_get_si_fn_t
1701       decoder_decode,     // aom_codec_decode_fn_t
1702       decoder_get_frame,  // aom_codec_get_frame_fn_t
1703       decoder_set_fb_fn,  // aom_codec_set_fb_fn_t
1704   },
1705   {
1706       // NOLINT
1707       0,
1708       NULL,  // aom_codec_enc_cfg_t
1709       NULL,  // aom_codec_encode_fn_t
1710       NULL,  // aom_codec_get_cx_data_fn_t
1711       NULL,  // aom_codec_enc_config_set_fn_t
1712       NULL,  // aom_codec_get_global_headers_fn_t
1713       NULL   // aom_codec_get_preview_frame_fn_t
1714   },
1715   NULL  // aom_codec_set_option_fn_t
1716 };
1717 
1718 // Decoder interface for inspecting frame data. It uses decoder_inspect instead
1719 // of decoder_decode so it only decodes one frame at a time, whether the frame
1720 // is shown or not.
1721 aom_codec_iface_t aom_codec_av1_inspect_algo = {
1722   "AOMedia Project AV1 Decoder Inspector" VERSION_STRING,
1723   AOM_CODEC_INTERNAL_ABI_VERSION,
1724   AOM_CODEC_CAP_DECODER |
1725       AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER,  // aom_codec_caps_t
1726   decoder_init,                             // aom_codec_init_fn_t
1727   decoder_destroy,                          // aom_codec_destroy_fn_t
1728   decoder_ctrl_maps,                        // aom_codec_ctrl_fn_map_t
1729   {
1730       // NOLINT
1731       decoder_peek_si,    // aom_codec_peek_si_fn_t
1732       decoder_get_si,     // aom_codec_get_si_fn_t
1733       decoder_inspect,    // aom_codec_decode_fn_t
1734       decoder_get_frame,  // aom_codec_get_frame_fn_t
1735       decoder_set_fb_fn,  // aom_codec_set_fb_fn_t
1736   },
1737   {
1738       // NOLINT
1739       0,
1740       NULL,  // aom_codec_enc_cfg_t
1741       NULL,  // aom_codec_encode_fn_t
1742       NULL,  // aom_codec_get_cx_data_fn_t
1743       NULL,  // aom_codec_enc_config_set_fn_t
1744       NULL,  // aom_codec_get_global_headers_fn_t
1745       NULL   // aom_codec_get_preview_frame_fn_t
1746   },
1747   NULL  // aom_codec_set_option_fn_t
1748 };
1749 
aom_codec_av1_dx(void)1750 aom_codec_iface_t *aom_codec_av1_dx(void) { return &aom_codec_av1_dx_algo; }
1751