Coverage Report

Created: 2026-05-16 06:27

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