Coverage Report

Created: 2023-06-07 06:31

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