Coverage Report

Created: 2022-08-24 06:11

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