av1_dx_iface.c 37.3 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
12
13
14
 */

#include <stdlib.h>
#include <string.h>

Adrian Grange's avatar
Adrian Grange committed
15
16
#include "./aom_config.h"
#include "./aom_version.h"
Jingning Han's avatar
Jingning Han committed
17

Adrian Grange's avatar
Adrian Grange committed
18
#include "aom/internal/aom_codec_internal.h"
Adrian Grange's avatar
Adrian Grange committed
19
#include "aom/aomdx.h"
Adrian Grange's avatar
Adrian Grange committed
20
#include "aom/aom_decoder.h"
Yaowu Xu's avatar
Yaowu Xu committed
21
#include "aom_dsp/bitreader_buffer.h"
Adrian Grange's avatar
Adrian Grange committed
22
23
#include "aom_dsp/aom_dsp_common.h"
#include "aom_util/aom_thread.h"
Jingning Han's avatar
Jingning Han committed
24

Yaowu Xu's avatar
Yaowu Xu committed
25
26
#include "av1/common/alloccommon.h"
#include "av1/common/frame_buffers.h"
Jingning Han's avatar
Jingning Han committed
27

Yaowu Xu's avatar
Yaowu Xu committed
28
29
#include "av1/decoder/decoder.h"
#include "av1/decoder/decodeframe.h"
Jingning Han's avatar
Jingning Han committed
30

Yaowu Xu's avatar
Yaowu Xu committed
31
#include "av1/av1_iface_common.h"
Jingning Han's avatar
Jingning Han committed
32

33
typedef aom_codec_stream_info_t av1_stream_info_t;
Jingning Han's avatar
Jingning Han committed
34
35
36

// This limit is due to framebuffer numbers.
// TODO(hkuang): Remove this limit after implementing ondemand framebuffers.
clang-format's avatar
clang-format committed
37
#define FRAME_CACHE_SIZE 6  // Cache maximum 6 decoded frames.
Jingning Han's avatar
Jingning Han committed
38
39
40

typedef struct cache_frame {
  int fb_idx;
Adrian Grange's avatar
Adrian Grange committed
41
  aom_image_t img;
Jingning Han's avatar
Jingning Han committed
42
43
} cache_frame;

Adrian Grange's avatar
Adrian Grange committed
44
45
46
struct aom_codec_alg_priv {
  aom_codec_priv_t base;
  aom_codec_dec_cfg_t cfg;
47
  av1_stream_info_t si;
clang-format's avatar
clang-format committed
48
  int postproc_cfg_set;
Adrian Grange's avatar
Adrian Grange committed
49
  aom_postproc_cfg_t postproc_cfg;
Adrian Grange's avatar
Adrian Grange committed
50
  aom_decrypt_cb decrypt_cb;
clang-format's avatar
clang-format committed
51
  void *decrypt_state;
Adrian Grange's avatar
Adrian Grange committed
52
  aom_image_t img;
clang-format's avatar
clang-format committed
53
54
55
56
57
58
  int img_avail;
  int flushed;
  int invert_tile_order;
  int last_show_frame;  // Index of last output frame.
  int byte_alignment;
  int skip_loop_filter;
Jingning Han's avatar
Jingning Han committed
59
60

  // Frame parallel related.
clang-format's avatar
clang-format committed
61
  int frame_parallel_decode;  // frame-based threading.
Adrian Grange's avatar
Adrian Grange committed
62
  AVxWorker *frame_workers;
clang-format's avatar
clang-format committed
63
64
65
66
67
68
69
70
71
72
  int num_frame_workers;
  int next_submit_worker_id;
  int last_submit_worker_id;
  int next_output_worker_id;
  int available_threads;
  cache_frame frame_cache[FRAME_CACHE_SIZE];
  int frame_cache_write;
  int frame_cache_read;
  int num_cache_frames;
  int need_resync;  // wait for key/intra-only frame
Jingning Han's avatar
Jingning Han committed
73
  // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
clang-format's avatar
clang-format committed
74
  BufferPool *buffer_pool;
Jingning Han's avatar
Jingning Han committed
75

76
  // External frame buffer info to save for AV1 common.
Jingning Han's avatar
Jingning Han committed
77
  void *ext_priv;  // Private data associated with the external frame buffers.
Adrian Grange's avatar
Adrian Grange committed
78
79
  aom_get_frame_buffer_cb_fn_t get_ext_fb_cb;
  aom_release_frame_buffer_cb_fn_t release_ext_fb_cb;
Jingning Han's avatar
Jingning Han committed
80
81
};

Adrian Grange's avatar
Adrian Grange committed
82
83
84
static aom_codec_err_t decoder_init(aom_codec_ctx_t *ctx,
                                    aom_codec_priv_enc_mr_cfg_t *data) {
  // This function only allocates space for the aom_codec_alg_priv_t
Jingning Han's avatar
Jingning Han committed
85
86
87
88
89
  // structure. More memory may be required at the time the stream
  // information becomes known.
  (void)data;

  if (!ctx->priv) {
Adrian Grange's avatar
Adrian Grange committed
90
91
    aom_codec_alg_priv_t *const priv =
        (aom_codec_alg_priv_t *)aom_calloc(1, sizeof(*priv));
Adrian Grange's avatar
Adrian Grange committed
92
    if (priv == NULL) return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
93

Adrian Grange's avatar
Adrian Grange committed
94
    ctx->priv = (aom_codec_priv_t *)priv;
Jingning Han's avatar
Jingning Han committed
95
96
97
98
99
100
    ctx->priv->init_flags = ctx->init_flags;
    priv->si.sz = sizeof(priv->si);
    priv->flushed = 0;
    // Only do frame parallel decode when threads > 1.
    priv->frame_parallel_decode =
        (ctx->config.dec && (ctx->config.dec->threads > 1) &&
Adrian Grange's avatar
Adrian Grange committed
101
         (ctx->init_flags & AOM_CODEC_USE_FRAME_THREADING))
clang-format's avatar
clang-format committed
102
103
            ? 1
            : 0;
Jingning Han's avatar
Jingning Han committed
104
105
106
107
108
109
    if (ctx->config.dec) {
      priv->cfg = *ctx->config.dec;
      ctx->config.dec = &priv->cfg;
    }
  }

Adrian Grange's avatar
Adrian Grange committed
110
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
111
112
}

Adrian Grange's avatar
Adrian Grange committed
113
static aom_codec_err_t decoder_destroy(aom_codec_alg_priv_t *ctx) {
Jingning Han's avatar
Jingning Han committed
114
115
116
  if (ctx->frame_workers != NULL) {
    int i;
    for (i = 0; i < ctx->num_frame_workers; ++i) {
Adrian Grange's avatar
Adrian Grange committed
117
      AVxWorker *const worker = &ctx->frame_workers[i];
Jingning Han's avatar
Jingning Han committed
118
119
      FrameWorkerData *const frame_worker_data =
          (FrameWorkerData *)worker->data1;
Adrian Grange's avatar
Adrian Grange committed
120
      aom_get_worker_interface()->end(worker);
121
122
      av1_remove_common(&frame_worker_data->pbi->common);
      av1_decoder_remove(frame_worker_data->pbi);
Adrian Grange's avatar
Adrian Grange committed
123
      aom_free(frame_worker_data->scratch_buffer);
Jingning Han's avatar
Jingning Han committed
124
125
126
127
#if CONFIG_MULTITHREAD
      pthread_mutex_destroy(&frame_worker_data->stats_mutex);
      pthread_cond_destroy(&frame_worker_data->stats_cond);
#endif
Adrian Grange's avatar
Adrian Grange committed
128
      aom_free(frame_worker_data);
Jingning Han's avatar
Jingning Han committed
129
130
131
132
133
134
135
    }
#if CONFIG_MULTITHREAD
    pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
#endif
  }

  if (ctx->buffer_pool) {
136
137
    av1_free_ref_frame_buffers(ctx->buffer_pool);
    av1_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
Jingning Han's avatar
Jingning Han committed
138
139
  }

Adrian Grange's avatar
Adrian Grange committed
140
141
142
  aom_free(ctx->frame_workers);
  aom_free(ctx->buffer_pool);
  aom_free(ctx);
Adrian Grange's avatar
Adrian Grange committed
143
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
144
145
}

clang-format's avatar
clang-format committed
146
static int parse_bitdepth_colorspace_sampling(BITSTREAM_PROFILE profile,
Adrian Grange's avatar
Adrian Grange committed
147
148
                                              struct aom_read_bit_buffer *rb) {
  aom_color_space_t color_space;
clang-format's avatar
clang-format committed
149
  if (profile >= PROFILE_2) rb->bit_offset += 1;  // Bit-depth 10 or 12.
Adrian Grange's avatar
Adrian Grange committed
150
  color_space = (aom_color_space_t)aom_rb_read_literal(rb, 3);
Adrian Grange's avatar
Adrian Grange committed
151
  if (color_space != AOM_CS_SRGB) {
Jingning Han's avatar
Jingning Han committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    rb->bit_offset += 1;  // [16,235] (including xvycc) vs [0,255] range.
    if (profile == PROFILE_1 || profile == PROFILE_3) {
      rb->bit_offset += 2;  // subsampling x/y.
      rb->bit_offset += 1;  // unused.
    }
  } else {
    if (profile == PROFILE_1 || profile == PROFILE_3) {
      rb->bit_offset += 1;  // unused
    } else {
      // RGB is only available in version 1.
      return 0;
    }
  }
  return 1;
}

Adrian Grange's avatar
Adrian Grange committed
168
169
170
static aom_codec_err_t decoder_peek_si_internal(
    const uint8_t *data, unsigned int data_sz, aom_codec_stream_info_t *si,
    int *is_intra_only, aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Jingning Han's avatar
Jingning Han committed
171
172
173
  int intra_only_flag = 0;
  uint8_t clear_buffer[9];

Adrian Grange's avatar
Adrian Grange committed
174
  if (data + data_sz <= data) return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
175
176
177
178
179

  si->is_kf = 0;
  si->w = si->h = 0;

  if (decrypt_cb) {
Adrian Grange's avatar
Adrian Grange committed
180
    data_sz = AOMMIN(sizeof(clear_buffer), data_sz);
Jingning Han's avatar
Jingning Han committed
181
182
183
184
185
186
187
    decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
    data = clear_buffer;
  }

  {
    int show_frame;
    int error_resilient;
Adrian Grange's avatar
Adrian Grange committed
188
189
    struct aom_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
    const int frame_marker = aom_rb_read_literal(&rb, 2);
190
    const BITSTREAM_PROFILE profile = av1_read_profile(&rb);
Jingning Han's avatar
Jingning Han committed
191

Adrian Grange's avatar
Adrian Grange committed
192
    if (frame_marker != AOM_FRAME_MARKER) return AOM_CODEC_UNSUP_BITSTREAM;
Jingning Han's avatar
Jingning Han committed
193

Adrian Grange's avatar
Adrian Grange committed
194
    if (profile >= MAX_PROFILES) return AOM_CODEC_UNSUP_BITSTREAM;
Jingning Han's avatar
Jingning Han committed
195
196

    if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
Adrian Grange's avatar
Adrian Grange committed
197
      return AOM_CODEC_UNSUP_BITSTREAM;
Jingning Han's avatar
Jingning Han committed
198

Adrian Grange's avatar
Adrian Grange committed
199
200
    if (aom_rb_read_bit(&rb)) {     // show an existing frame
      aom_rb_read_literal(&rb, 3);  // Frame buffer to show.
Adrian Grange's avatar
Adrian Grange committed
201
      return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
202
203
    }

Adrian Grange's avatar
Adrian Grange committed
204
    if (data_sz <= 8) return AOM_CODEC_UNSUP_BITSTREAM;
Jingning Han's avatar
Jingning Han committed
205

Adrian Grange's avatar
Adrian Grange committed
206
207
208
    si->is_kf = !aom_rb_read_bit(&rb);
    show_frame = aom_rb_read_bit(&rb);
    error_resilient = aom_rb_read_bit(&rb);
Jingning Han's avatar
Jingning Han committed
209
210

    if (si->is_kf) {
Adrian Grange's avatar
Adrian Grange committed
211
      if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Jingning Han's avatar
Jingning Han committed
212
213

      if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Adrian Grange's avatar
Adrian Grange committed
214
        return AOM_CODEC_UNSUP_BITSTREAM;
215
      av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Jingning Han's avatar
Jingning Han committed
216
    } else {
Adrian Grange's avatar
Adrian Grange committed
217
      intra_only_flag = show_frame ? 0 : aom_rb_read_bit(&rb);
Jingning Han's avatar
Jingning Han committed
218
219
220
221

      rb.bit_offset += error_resilient ? 0 : 2;  // reset_frame_context

      if (intra_only_flag) {
Adrian Grange's avatar
Adrian Grange committed
222
        if (!av1_read_sync_code(&rb)) return AOM_CODEC_UNSUP_BITSTREAM;
Jingning Han's avatar
Jingning Han committed
223
224
        if (profile > PROFILE_0) {
          if (!parse_bitdepth_colorspace_sampling(profile, &rb))
Adrian Grange's avatar
Adrian Grange committed
225
            return AOM_CODEC_UNSUP_BITSTREAM;
Jingning Han's avatar
Jingning Han committed
226
227
        }
        rb.bit_offset += REF_FRAMES;  // refresh_frame_flags
228
        av1_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
Jingning Han's avatar
Jingning Han committed
229
230
231
      }
    }
  }
clang-format's avatar
clang-format committed
232
  if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
Adrian Grange's avatar
Adrian Grange committed
233
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
234
235
}

Adrian Grange's avatar
Adrian Grange committed
236
static aom_codec_err_t decoder_peek_si(const uint8_t *data,
Jingning Han's avatar
Jingning Han committed
237
                                       unsigned int data_sz,
Adrian Grange's avatar
Adrian Grange committed
238
                                       aom_codec_stream_info_t *si) {
Jingning Han's avatar
Jingning Han committed
239
240
241
  return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
}

Adrian Grange's avatar
Adrian Grange committed
242
243
static aom_codec_err_t decoder_get_si(aom_codec_alg_priv_t *ctx,
                                      aom_codec_stream_info_t *si) {
244
245
  const size_t sz = (si->sz >= sizeof(av1_stream_info_t))
                        ? sizeof(av1_stream_info_t)
Adrian Grange's avatar
Adrian Grange committed
246
                        : sizeof(aom_codec_stream_info_t);
Jingning Han's avatar
Jingning Han committed
247
248
249
  memcpy(si, &ctx->si, sz);
  si->sz = (unsigned int)sz;

Adrian Grange's avatar
Adrian Grange committed
250
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
251
252
}

Adrian Grange's avatar
Adrian Grange committed
253
static void set_error_detail(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
254
255
256
257
                             const char *const error) {
  ctx->base.err_detail = error;
}

Adrian Grange's avatar
Adrian Grange committed
258
259
static aom_codec_err_t update_error_state(
    aom_codec_alg_priv_t *ctx, const struct aom_internal_error_info *error) {
Jingning Han's avatar
Jingning Han committed
260
261
262
263
264
265
  if (error->error_code)
    set_error_detail(ctx, error->has_detail ? error->detail : NULL);

  return error->error_code;
}

Adrian Grange's avatar
Adrian Grange committed
266
static void init_buffer_callbacks(aom_codec_alg_priv_t *ctx) {
Jingning Han's avatar
Jingning Han committed
267
268
269
  int i;

  for (i = 0; i < ctx->num_frame_workers; ++i) {
Adrian Grange's avatar
Adrian Grange committed
270
    AVxWorker *const worker = &ctx->frame_workers[i];
Jingning Han's avatar
Jingning Han committed
271
    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
272
    AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Jingning Han's avatar
Jingning Han committed
273
274
275
276
277
278
279
280
281
282
283
    BufferPool *const pool = cm->buffer_pool;

    cm->new_fb_idx = INVALID_IDX;
    cm->byte_alignment = ctx->byte_alignment;
    cm->skip_loop_filter = ctx->skip_loop_filter;

    if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
      pool->get_fb_cb = ctx->get_ext_fb_cb;
      pool->release_fb_cb = ctx->release_ext_fb_cb;
      pool->cb_priv = ctx->ext_priv;
    } else {
284
285
      pool->get_fb_cb = av1_get_frame_buffer;
      pool->release_fb_cb = av1_release_frame_buffer;
Jingning Han's avatar
Jingning Han committed
286

287
      if (av1_alloc_internal_frame_buffers(&pool->int_frame_buffers))
Adrian Grange's avatar
Adrian Grange committed
288
        aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Jingning Han's avatar
Jingning Han committed
289
290
291
292
293
294
295
                           "Failed to initialize internal frame buffers");

      pool->cb_priv = &pool->int_frame_buffers;
    }
  }
}

Adrian Grange's avatar
Adrian Grange committed
296
static void set_default_ppflags(aom_postproc_cfg_t *cfg) {
Adrian Grange's avatar
Adrian Grange committed
297
  cfg->post_proc_flag = AOM_DEBLOCK | AOM_DEMACROBLOCK;
Jingning Han's avatar
Jingning Han committed
298
299
300
301
302
303
304
305
306
  cfg->deblocking_level = 4;
  cfg->noise_level = 0;
}

static int frame_worker_hook(void *arg1, void *arg2) {
  FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
  const uint8_t *data = frame_worker_data->data;
  (void)arg2;

307
  frame_worker_data->result = av1_receive_compressed_data(
clang-format's avatar
clang-format committed
308
      frame_worker_data->pbi, frame_worker_data->data_size, &data);
Jingning Han's avatar
Jingning Han committed
309
310
  frame_worker_data->data_end = data;

311
  if (frame_worker_data->pbi->common.frame_parallel_decode) {
Jingning Han's avatar
Jingning Han committed
312
313
314
315
    // In frame parallel decoding, a worker thread must successfully decode all
    // the compressed data.
    if (frame_worker_data->result != 0 ||
        frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
Adrian Grange's avatar
Adrian Grange committed
316
      AVxWorker *const worker = frame_worker_data->pbi->frame_worker_owner;
Jingning Han's avatar
Jingning Han committed
317
318
      BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
      // Signal all the other threads that are waiting for this frame.
319
      av1_frameworker_lock_stats(worker);
Jingning Han's avatar
Jingning Han committed
320
321
322
323
324
      frame_worker_data->frame_context_ready = 1;
      lock_buffer_pool(pool);
      frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
      unlock_buffer_pool(pool);
      frame_worker_data->pbi->need_resync = 1;
325
326
      av1_frameworker_signal_stats(worker);
      av1_frameworker_unlock_stats(worker);
Jingning Han's avatar
Jingning Han committed
327
328
329
330
331
332
333
334
335
336
      return 0;
    }
  } else if (frame_worker_data->result != 0) {
    // Check decode result in serial decode.
    frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
    frame_worker_data->pbi->need_resync = 1;
  }
  return !frame_worker_data->result;
}

Adrian Grange's avatar
Adrian Grange committed
337
static aom_codec_err_t init_decoder(aom_codec_alg_priv_t *ctx) {
Jingning Han's avatar
Jingning Han committed
338
  int i;
Adrian Grange's avatar
Adrian Grange committed
339
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Jingning Han's avatar
Jingning Han committed
340
341
342
343
344
345
346
347
348
349

  ctx->last_show_frame = -1;
  ctx->next_submit_worker_id = 0;
  ctx->last_submit_worker_id = 0;
  ctx->next_output_worker_id = 0;
  ctx->frame_cache_read = 0;
  ctx->frame_cache_write = 0;
  ctx->num_cache_frames = 0;
  ctx->need_resync = 1;
  ctx->num_frame_workers =
clang-format's avatar
clang-format committed
350
      (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads : 1;
Jingning Han's avatar
Jingning Han committed
351
352
353
354
355
  if (ctx->num_frame_workers > MAX_DECODE_THREADS)
    ctx->num_frame_workers = MAX_DECODE_THREADS;
  ctx->available_threads = ctx->num_frame_workers;
  ctx->flushed = 0;

Adrian Grange's avatar
Adrian Grange committed
356
  ctx->buffer_pool = (BufferPool *)aom_calloc(1, sizeof(BufferPool));
Adrian Grange's avatar
Adrian Grange committed
357
  if (ctx->buffer_pool == NULL) return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
358
359

#if CONFIG_MULTITHREAD
clang-format's avatar
clang-format committed
360
361
  if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
    set_error_detail(ctx, "Failed to allocate buffer pool mutex");
Adrian Grange's avatar
Adrian Grange committed
362
    return AOM_CODEC_MEM_ERROR;
clang-format's avatar
clang-format committed
363
  }
Jingning Han's avatar
Jingning Han committed
364
365
#endif

Adrian Grange's avatar
Adrian Grange committed
366
  ctx->frame_workers = (AVxWorker *)aom_malloc(ctx->num_frame_workers *
clang-format's avatar
clang-format committed
367
                                               sizeof(*ctx->frame_workers));
Jingning Han's avatar
Jingning Han committed
368
369
  if (ctx->frame_workers == NULL) {
    set_error_detail(ctx, "Failed to allocate frame_workers");
Adrian Grange's avatar
Adrian Grange committed
370
    return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
371
372
373
  }

  for (i = 0; i < ctx->num_frame_workers; ++i) {
Adrian Grange's avatar
Adrian Grange committed
374
    AVxWorker *const worker = &ctx->frame_workers[i];
Jingning Han's avatar
Jingning Han committed
375
376
    FrameWorkerData *frame_worker_data = NULL;
    winterface->init(worker);
Adrian Grange's avatar
Adrian Grange committed
377
    worker->data1 = aom_memalign(32, sizeof(FrameWorkerData));
Jingning Han's avatar
Jingning Han committed
378
379
    if (worker->data1 == NULL) {
      set_error_detail(ctx, "Failed to allocate frame_worker_data");
Adrian Grange's avatar
Adrian Grange committed
380
      return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
381
382
    }
    frame_worker_data = (FrameWorkerData *)worker->data1;
383
    frame_worker_data->pbi = av1_decoder_create(ctx->buffer_pool);
Jingning Han's avatar
Jingning Han committed
384
385
    if (frame_worker_data->pbi == NULL) {
      set_error_detail(ctx, "Failed to allocate frame_worker_data");
Adrian Grange's avatar
Adrian Grange committed
386
      return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
387
388
389
390
391
392
393
394
395
396
    }
    frame_worker_data->pbi->frame_worker_owner = worker;
    frame_worker_data->worker_id = i;
    frame_worker_data->scratch_buffer = NULL;
    frame_worker_data->scratch_buffer_size = 0;
    frame_worker_data->frame_context_ready = 0;
    frame_worker_data->received_frame = 0;
#if CONFIG_MULTITHREAD
    if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
      set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
Adrian Grange's avatar
Adrian Grange committed
397
      return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
398
399
400
401
    }

    if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
      set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
Adrian Grange's avatar
Adrian Grange committed
402
      return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
403
404
405
406
407
408
409
410
411
412
    }
#endif
    // If decoding in serial mode, FrameWorker thread could create tile worker
    // thread or loopfilter thread.
    frame_worker_data->pbi->max_threads =
        (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;

    frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
    frame_worker_data->pbi->common.frame_parallel_decode =
        ctx->frame_parallel_decode;
Adrian Grange's avatar
Adrian Grange committed
413
    worker->hook = (AVxWorkerHook)frame_worker_hook;
Jingning Han's avatar
Jingning Han committed
414
415
    if (!winterface->reset(worker)) {
      set_error_detail(ctx, "Frame Worker thread creation failed");
Adrian Grange's avatar
Adrian Grange committed
416
      return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
417
418
419
420
421
    }
  }

  // If postprocessing was enabled by the application and a
  // configuration has not been provided, default it.
Adrian Grange's avatar
Adrian Grange committed
422
  if (!ctx->postproc_cfg_set && (ctx->base.init_flags & AOM_CODEC_USE_POSTPROC))
Jingning Han's avatar
Jingning Han committed
423
424
425
426
    set_default_ppflags(&ctx->postproc_cfg);

  init_buffer_callbacks(ctx);

Adrian Grange's avatar
Adrian Grange committed
427
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
428
429
}

Adrian Grange's avatar
Adrian Grange committed
430
static INLINE void check_resync(aom_codec_alg_priv_t *const ctx,
431
                                const AV1Decoder *const pbi) {
Jingning Han's avatar
Jingning Han committed
432
433
434
435
436
437
  // Clear resync flag if worker got a key frame or intra only frame.
  if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
      (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
    ctx->need_resync = 0;
}

Adrian Grange's avatar
Adrian Grange committed
438
static aom_codec_err_t decode_one(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
439
440
                                  const uint8_t **data, unsigned int data_sz,
                                  void *user_priv, int64_t deadline) {
Adrian Grange's avatar
Adrian Grange committed
441
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
Jingning Han's avatar
Jingning Han committed
442
443
444
445
446
447
448
  (void)deadline;

  // Determine the stream parameters. Note that we rely on peek_si to
  // validate that we have a buffer that does not wrap around the top
  // of the heap.
  if (!ctx->si.h) {
    int is_intra_only = 0;
Adrian Grange's avatar
Adrian Grange committed
449
    const aom_codec_err_t res =
Jingning Han's avatar
Jingning Han committed
450
451
        decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
                                 ctx->decrypt_cb, ctx->decrypt_state);
Adrian Grange's avatar
Adrian Grange committed
452
    if (res != AOM_CODEC_OK) return res;
Jingning Han's avatar
Jingning Han committed
453

Adrian Grange's avatar
Adrian Grange committed
454
    if (!ctx->si.is_kf && !is_intra_only) return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
455
456
457
  }

  if (!ctx->frame_parallel_decode) {
Adrian Grange's avatar
Adrian Grange committed
458
    AVxWorker *const worker = ctx->frame_workers;
Jingning Han's avatar
Jingning Han committed
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
    frame_worker_data->data = *data;
    frame_worker_data->data_size = data_sz;
    frame_worker_data->user_priv = user_priv;
    frame_worker_data->received_frame = 1;

    // Set these even if already initialized.  The caller may have changed the
    // decrypt config between frames.
    frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
    frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;

    worker->had_error = 0;
    winterface->execute(worker);

    // Update data pointer after decode.
    *data = frame_worker_data->data_end;

    if (worker->had_error)
      return update_error_state(ctx, &frame_worker_data->pbi->common.error);

    check_resync(ctx, frame_worker_data->pbi);
  } else {
Adrian Grange's avatar
Adrian Grange committed
481
    AVxWorker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
Jingning Han's avatar
Jingning Han committed
482
483
484
    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
    // Copy context from last worker thread to next worker thread.
    if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
485
      av1_frameworker_copy_context(
Jingning Han's avatar
Jingning Han committed
486
487
488
489
490
491
492
493
494
495
          &ctx->frame_workers[ctx->next_submit_worker_id],
          &ctx->frame_workers[ctx->last_submit_worker_id]);

    frame_worker_data->pbi->ready_for_new_data = 0;
    // Copy the compressed data into worker's internal buffer.
    // TODO(hkuang): Will all the workers allocate the same size
    // as the size of the first intra frame be better? This will
    // avoid too many deallocate and allocate.
    if (frame_worker_data->scratch_buffer_size < data_sz) {
      frame_worker_data->scratch_buffer =
Adrian Grange's avatar
Adrian Grange committed
496
          (uint8_t *)aom_realloc(frame_worker_data->scratch_buffer, data_sz);
Jingning Han's avatar
Jingning Han committed
497
498
      if (frame_worker_data->scratch_buffer == NULL) {
        set_error_detail(ctx, "Failed to reallocate scratch buffer");
Adrian Grange's avatar
Adrian Grange committed
499
        return AOM_CODEC_MEM_ERROR;
Jingning Han's avatar
Jingning Han committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
      }
      frame_worker_data->scratch_buffer_size = data_sz;
    }
    frame_worker_data->data_size = data_sz;
    memcpy(frame_worker_data->scratch_buffer, *data, data_sz);

    frame_worker_data->frame_decoded = 0;
    frame_worker_data->frame_context_ready = 0;
    frame_worker_data->received_frame = 1;
    frame_worker_data->data = frame_worker_data->scratch_buffer;
    frame_worker_data->user_priv = user_priv;

    if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
      ctx->last_submit_worker_id =
          (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;

    ctx->next_submit_worker_id =
        (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
    --ctx->available_threads;
    worker->had_error = 0;
    winterface->launch(worker);
  }

Adrian Grange's avatar
Adrian Grange committed
523
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
524
525
}

Adrian Grange's avatar
Adrian Grange committed
526
static void wait_worker_and_cache_frame(aom_codec_alg_priv_t *ctx) {
Jingning Han's avatar
Jingning Han committed
527
  YV12_BUFFER_CONFIG sd;
Adrian Grange's avatar
Adrian Grange committed
528
529
  const AVxWorkerInterface *const winterface = aom_get_worker_interface();
  AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Jingning Han's avatar
Jingning Han committed
530
531
532
533
534
535
536
537
538
539
  FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
  ctx->next_output_worker_id =
      (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
  // TODO(hkuang): Add worker error handling here.
  winterface->sync(worker);
  frame_worker_data->received_frame = 0;
  ++ctx->available_threads;

  check_resync(ctx, frame_worker_data->pbi);

540
541
  if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
    AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Jingning Han's avatar
Jingning Han committed
542
543
544
545
546
547
    RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
    ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
    yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
                    frame_worker_data->user_priv);
    ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
        frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
clang-format's avatar
clang-format committed
548
    ctx->frame_cache_write = (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
Jingning Han's avatar
Jingning Han committed
549
550
551
552
    ++ctx->num_cache_frames;
  }
}

Adrian Grange's avatar
Adrian Grange committed
553
static aom_codec_err_t decoder_decode(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
554
555
556
                                      const uint8_t *data, unsigned int data_sz,
                                      void *user_priv, long deadline) {
  const uint8_t *data_start = data;
clang-format's avatar
clang-format committed
557
  const uint8_t *const data_end = data + data_sz;
Adrian Grange's avatar
Adrian Grange committed
558
  aom_codec_err_t res;
Jingning Han's avatar
Jingning Han committed
559
560
561
562
563
  uint32_t frame_sizes[8];
  int frame_count;

  if (data == NULL && data_sz == 0) {
    ctx->flushed = 1;
Adrian Grange's avatar
Adrian Grange committed
564
    return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
565
566
567
568
569
570
571
  }

  // Reset flushed when receiving a valid frame.
  ctx->flushed = 0;

  // Initialize the decoder workers on the first frame.
  if (ctx->frame_workers == NULL) {
572
    res = init_decoder(ctx);
Adrian Grange's avatar
Adrian Grange committed
573
    if (res != AOM_CODEC_OK) return res;
Jingning Han's avatar
Jingning Han committed
574
575
  }

576
  res = av1_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
clang-format's avatar
clang-format committed
577
                                   ctx->decrypt_cb, ctx->decrypt_state);
Adrian Grange's avatar
Adrian Grange committed
578
  if (res != AOM_CODEC_OK) return res;
Jingning Han's avatar
Jingning Han committed
579
580
581
582
583
584
585
586
587
588
589
590

  if (ctx->frame_parallel_decode) {
    // Decode in frame parallel mode. When decoding in this mode, the frame
    // passed to the decoder must be either a normal frame or a superframe with
    // superframe index so the decoder could get each frame's start position
    // in the superframe.
    if (frame_count > 0) {
      int i;

      for (i = 0; i < frame_count; ++i) {
        const uint8_t *data_start_copy = data_start;
        const uint32_t frame_size = frame_sizes[i];
clang-format's avatar
clang-format committed
591
592
        if (data_start < data ||
            frame_size > (uint32_t)(data_end - data_start)) {
Jingning Han's avatar
Jingning Han committed
593
          set_error_detail(ctx, "Invalid frame size in index");
Adrian Grange's avatar
Adrian Grange committed
594
          return AOM_CODEC_CORRUPT_FRAME;
Jingning Han's avatar
Jingning Han committed
595
596
597
598
599
600
601
602
603
604
        }

        if (ctx->available_threads == 0) {
          // No more threads for decoding. Wait until the next output worker
          // finishes decoding. Then copy the decoded frame into cache.
          if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
            wait_worker_and_cache_frame(ctx);
          } else {
            // TODO(hkuang): Add unit test to test this path.
            set_error_detail(ctx, "Frame output cache is full.");
Adrian Grange's avatar
Adrian Grange committed
605
            return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
606
607
608
          }
        }

clang-format's avatar
clang-format committed
609
610
        res =
            decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Adrian Grange's avatar
Adrian Grange committed
611
        if (res != AOM_CODEC_OK) return res;
Jingning Han's avatar
Jingning Han committed
612
613
614
615
616
617
618
619
620
621
622
        data_start += frame_size;
      }
    } else {
      if (ctx->available_threads == 0) {
        // No more threads for decoding. Wait until the next output worker
        // finishes decoding. Then copy the decoded frame into cache.
        if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
          wait_worker_and_cache_frame(ctx);
        } else {
          // TODO(hkuang): Add unit test to test this path.
          set_error_detail(ctx, "Frame output cache is full.");
Adrian Grange's avatar
Adrian Grange committed
623
          return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
624
625
626
627
        }
      }

      res = decode_one(ctx, &data, data_sz, user_priv, deadline);
Adrian Grange's avatar
Adrian Grange committed
628
      if (res != AOM_CODEC_OK) return res;
Jingning Han's avatar
Jingning Han committed
629
630
631
632
633
634
635
636
637
    }
  } else {
    // Decode in serial mode.
    if (frame_count > 0) {
      int i;

      for (i = 0; i < frame_count; ++i) {
        const uint8_t *data_start_copy = data_start;
        const uint32_t frame_size = frame_sizes[i];
clang-format's avatar
clang-format committed
638
639
        if (data_start < data ||
            frame_size > (uint32_t)(data_end - data_start)) {
Jingning Han's avatar
Jingning Han committed
640
          set_error_detail(ctx, "Invalid frame size in index");
Adrian Grange's avatar
Adrian Grange committed
641
          return AOM_CODEC_CORRUPT_FRAME;
Jingning Han's avatar
Jingning Han committed
642
643
        }

clang-format's avatar
clang-format committed
644
645
        res =
            decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
Adrian Grange's avatar
Adrian Grange committed
646
        if (res != AOM_CODEC_OK) return res;
Jingning Han's avatar
Jingning Han committed
647
648
649
650
651

        data_start += frame_size;
      }
    } else {
      while (data_start < data_end) {
clang-format's avatar
clang-format committed
652
        const uint32_t frame_size = (uint32_t)(data_end - data_start);
653
        res = decode_one(ctx, &data_start, frame_size, user_priv, deadline);
Adrian Grange's avatar
Adrian Grange committed
654
        if (res != AOM_CODEC_OK) return res;
Jingning Han's avatar
Jingning Han committed
655
656
657

        // Account for suboptimal termination by the encoder.
        while (data_start < data_end) {
clang-format's avatar
clang-format committed
658
659
660
          const uint8_t marker =
              read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
          if (marker) break;
Jingning Han's avatar
Jingning Han committed
661
662
663
664
665
666
667
668
669
          ++data_start;
        }
      }
    }
  }

  return res;
}

Adrian Grange's avatar
Adrian Grange committed
670
static void release_last_output_frame(aom_codec_alg_priv_t *ctx) {
Jingning Han's avatar
Jingning Han committed
671
672
673
674
675
676
677
678
679
680
  RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
  // Decrease reference count of last output frame in frame parallel mode.
  if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
    BufferPool *const pool = ctx->buffer_pool;
    lock_buffer_pool(pool);
    decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
    unlock_buffer_pool(pool);
  }
}

Adrian Grange's avatar
Adrian Grange committed
681
682
683
static aom_image_t *decoder_get_frame(aom_codec_alg_priv_t *ctx,
                                      aom_codec_iter_t *iter) {
  aom_image_t *img = NULL;
Jingning Han's avatar
Jingning Han committed
684
685
686
687
688
689
690
691
692
693
694

  // Only return frame when all the cpu are busy or
  // application fluhsed the decoder in frame parallel decode.
  if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
      !ctx->flushed) {
    return NULL;
  }

  // Output the frames in the cache first.
  if (ctx->num_cache_frames > 0) {
    release_last_output_frame(ctx);
clang-format's avatar
clang-format committed
695
696
    ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
    if (ctx->need_resync) return NULL;
Jingning Han's avatar
Jingning Han committed
697
698
699
700
701
702
703
704
705
706
707
    img = &ctx->frame_cache[ctx->frame_cache_read].img;
    ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
    --ctx->num_cache_frames;
    return img;
  }

  // iter acts as a flip flop, so an image is only returned on the first
  // call to get_frame.
  if (*iter == NULL && ctx->frame_workers != NULL) {
    do {
      YV12_BUFFER_CONFIG sd;
Adrian Grange's avatar
Adrian Grange committed
708
709
      const AVxWorkerInterface *const winterface = aom_get_worker_interface();
      AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Jingning Han's avatar
Jingning Han committed
710
711
712
713
714
715
716
717
718
719
720
721
      FrameWorkerData *const frame_worker_data =
          (FrameWorkerData *)worker->data1;
      ctx->next_output_worker_id =
          (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
      // Wait for the frame from worker thread.
      if (winterface->sync(worker)) {
        // Check if worker has received any frames.
        if (frame_worker_data->received_frame == 1) {
          ++ctx->available_threads;
          frame_worker_data->received_frame = 0;
          check_resync(ctx, frame_worker_data->pbi);
        }
722
723
        if (av1_get_raw_frame(frame_worker_data->pbi, &sd) == 0) {
          AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Jingning Han's avatar
Jingning Han committed
724
725
726
          RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
          release_last_output_frame(ctx);
          ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
clang-format's avatar
clang-format committed
727
          if (ctx->need_resync) return NULL;
Jingning Han's avatar
Jingning Han committed
728
729
730
731
732
733
734
735
736
737
          yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
          ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
          img = &ctx->img;
          return img;
        }
      } else {
        // Decoding failed. Release the worker thread.
        frame_worker_data->received_frame = 0;
        ++ctx->available_threads;
        ctx->need_resync = 1;
clang-format's avatar
clang-format committed
738
        if (ctx->flushed != 1) return NULL;
Jingning Han's avatar
Jingning Han committed
739
740
741
742
743
744
      }
    } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
  }
  return NULL;
}

Adrian Grange's avatar
Adrian Grange committed
745
746
747
static aom_codec_err_t decoder_set_fb_fn(
    aom_codec_alg_priv_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
    aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
Jingning Han's avatar
Jingning Han committed
748
  if (cb_get == NULL || cb_release == NULL) {
Adrian Grange's avatar
Adrian Grange committed
749
    return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
750
751
752
753
754
755
  } else if (ctx->frame_workers == NULL) {
    // If the decoder has already been initialized, do not accept changes to
    // the frame buffer functions.
    ctx->get_ext_fb_cb = cb_get;
    ctx->release_ext_fb_cb = cb_release;
    ctx->ext_priv = cb_priv;
Adrian Grange's avatar
Adrian Grange committed
756
    return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
757
758
  }

Adrian Grange's avatar
Adrian Grange committed
759
  return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
760
761
}

Adrian Grange's avatar
Adrian Grange committed
762
static aom_codec_err_t ctrl_set_reference(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
763
                                          va_list args) {
Adrian Grange's avatar
Adrian Grange committed
764
  aom_ref_frame_t *const data = va_arg(args, aom_ref_frame_t *);
Jingning Han's avatar
Jingning Han committed
765
766
767
768

  // Only support this function in serial decode.
  if (ctx->frame_parallel_decode) {
    set_error_detail(ctx, "Not supported in frame parallel decode");
Adrian Grange's avatar
Adrian Grange committed
769
    return AOM_CODEC_INCAPABLE;
Jingning Han's avatar
Jingning Han committed
770
771
772
  }

  if (data) {
Adrian Grange's avatar
Adrian Grange committed
773
    aom_ref_frame_t *const frame = (aom_ref_frame_t *)data;
Jingning Han's avatar
Jingning Han committed
774
    YV12_BUFFER_CONFIG sd;
Adrian Grange's avatar
Adrian Grange committed
775
    AVxWorker *const worker = ctx->frame_workers;
Jingning Han's avatar
Jingning Han committed
776
777
    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
    image2yuvconfig(&frame->img, &sd);
778
    return av1_set_reference_dec(&frame_worker_data->pbi->common,
clang-format's avatar
clang-format committed
779
                                 (AOM_REFFRAME)frame->frame_type, &sd);
Jingning Han's avatar
Jingning Han committed
780
  } else {
Adrian Grange's avatar
Adrian Grange committed
781
    return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
782
783
784
  }
}

Adrian Grange's avatar
Adrian Grange committed
785
static aom_codec_err_t ctrl_copy_reference(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
786
                                           va_list args) {
Urvang Joshi's avatar
Urvang Joshi committed
787
  const aom_ref_frame_t *const frame = va_arg(args, aom_ref_frame_t *);
Jingning Han's avatar
Jingning Han committed
788
789
790
791

  // Only support this function in serial decode.
  if (ctx->frame_parallel_decode) {
    set_error_detail(ctx, "Not supported in frame parallel decode");
Adrian Grange's avatar
Adrian Grange committed
792
    return AOM_CODEC_INCAPABLE;
Jingning Han's avatar
Jingning Han committed
793
794
  }

Urvang Joshi's avatar
Urvang Joshi committed
795
  if (frame) {
Jingning Han's avatar
Jingning Han committed
796
    YV12_BUFFER_CONFIG sd;
Adrian Grange's avatar
Adrian Grange committed
797
    AVxWorker *const worker = ctx->frame_workers;
Jingning Han's avatar
Jingning Han committed
798
799
    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
    image2yuvconfig(&frame->img, &sd);
800
    return av1_copy_reference_dec(frame_worker_data->pbi,
clang-format's avatar
clang-format committed
801
                                  (AOM_REFFRAME)frame->frame_type, &sd);
Jingning Han's avatar
Jingning Han committed
802
  } else {
Adrian Grange's avatar
Adrian Grange committed
803
    return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
804
805
806
  }
}

Adrian Grange's avatar
Adrian Grange committed
807
static aom_codec_err_t ctrl_get_reference(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
808
                                          va_list args) {
Adrian Grange's avatar
Adrian Grange committed
809
  av1_ref_frame_t *data = va_arg(args, av1_ref_frame_t *);
Jingning Han's avatar
Jingning Han committed
810
811
812
813

  // Only support this function in serial decode.
  if (ctx->frame_parallel_decode) {
    set_error_detail(ctx, "Not supported in frame parallel decode");
Adrian Grange's avatar
Adrian Grange committed
814
    return AOM_CODEC_INCAPABLE;
Jingning Han's avatar
Jingning Han committed
815
816
817
  }

  if (data) {
clang-format's avatar
clang-format committed
818
    YV12_BUFFER_CONFIG *fb;
Adrian Grange's avatar
Adrian Grange committed
819
    AVxWorker *const worker = ctx->frame_workers;
Jingning Han's avatar
Jingning Han committed
820
821
    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
    fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
Adrian Grange's avatar
Adrian Grange committed
822
    if (fb == NULL) return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
823
    yuvconfig2image(&data->img, fb, NULL);
Adrian Grange's avatar
Adrian Grange committed
824
    return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
825
  } else {
Adrian Grange's avatar
Adrian Grange committed
826
    return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
827
828
829
  }
}

Adrian Grange's avatar
Adrian Grange committed
830
static aom_codec_err_t ctrl_set_postproc(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
831
832
833
                                         va_list args) {
  (void)ctx;
  (void)args;
Adrian Grange's avatar
Adrian Grange committed
834
  return AOM_CODEC_INCAPABLE;
Jingning Han's avatar
Jingning Han committed
835
836
}

Adrian Grange's avatar
Adrian Grange committed
837
static aom_codec_err_t ctrl_set_dbg_options(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
838
839
840
                                            va_list args) {
  (void)ctx;
  (void)args;
Adrian Grange's avatar
Adrian Grange committed
841
  return AOM_CODEC_INCAPABLE;
Jingning Han's avatar
Jingning Han committed
842
843
}

Adrian Grange's avatar
Adrian Grange committed
844
static aom_codec_err_t ctrl_get_last_ref_updates(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
845
846
847
848
849
850
                                                 va_list args) {
  int *const update_info = va_arg(args, int *);

  // Only support this function in serial decode.
  if (ctx->frame_parallel_decode) {
    set_error_detail(ctx, "Not supported in frame parallel decode");
Adrian Grange's avatar
Adrian Grange committed
851
    return AOM_CODEC_INCAPABLE;
Jingning Han's avatar
Jingning Han committed
852
853
854
855
  }

  if (update_info) {
    if (ctx->frame_workers) {
Adrian Grange's avatar
Adrian Grange committed
856
      AVxWorker *const worker = ctx->frame_workers;
Jingning Han's avatar
Jingning Han committed
857
858
859
      FrameWorkerData *const frame_worker_data =
          (FrameWorkerData *)worker->data1;
      *update_info = frame_worker_data->pbi->refresh_frame_flags;
Adrian Grange's avatar
Adrian Grange committed
860
      return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
861
    } else {
Adrian Grange's avatar
Adrian Grange committed
862
      return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
863
864
865
    }
  }

Adrian Grange's avatar
Adrian Grange committed
866
  return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
867
868
}

Adrian Grange's avatar
Adrian Grange committed
869
static aom_codec_err_t ctrl_get_frame_corrupted(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
870
871
872
873
874
                                                va_list args) {
  int *corrupted = va_arg(args, int *);

  if (corrupted) {
    if (ctx->frame_workers) {
Adrian Grange's avatar
Adrian Grange committed
875
      AVxWorker *const worker = ctx->frame_workers;
Jingning Han's avatar
Jingning Han committed
876
877
878
879
880
      FrameWorkerData *const frame_worker_data =
          (FrameWorkerData *)worker->data1;
      RefCntBuffer *const frame_bufs =
          frame_worker_data->pbi->common.buffer_pool->frame_bufs;
      if (frame_worker_data->pbi->common.frame_to_show == NULL)
Adrian Grange's avatar
Adrian Grange committed
881
        return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
882
883
      if (ctx->last_show_frame >= 0)
        *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
Adrian Grange's avatar
Adrian Grange committed
884
      return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
885
    } else {
Adrian Grange's avatar
Adrian Grange committed
886
      return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
887
888
889
    }
  }

Adrian Grange's avatar
Adrian Grange committed
890
  return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
891
892
}

Adrian Grange's avatar
Adrian Grange committed
893
static aom_codec_err_t ctrl_get_frame_size(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
894
895
896
897
898
899
                                           va_list args) {
  int *const frame_size = va_arg(args, int *);

  // Only support this function in serial decode.
  if (ctx->frame_parallel_decode) {
    set_error_detail(ctx, "Not supported in frame parallel decode");
Adrian Grange's avatar
Adrian Grange committed
900
    return AOM_CODEC_INCAPABLE;
Jingning Han's avatar
Jingning Han committed
901
902
903
904
  }

  if (frame_size) {
    if (ctx->frame_workers) {
Adrian Grange's avatar
Adrian Grange committed
905
      AVxWorker *const worker = ctx->frame_workers;
Jingning Han's avatar
Jingning Han committed
906
907
      FrameWorkerData *const frame_worker_data =
          (FrameWorkerData *)worker->data1;
908
      const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Jingning Han's avatar
Jingning Han committed
909
910
      frame_size[0] = cm->width;
      frame_size[1] = cm->height;
Adrian Grange's avatar
Adrian Grange committed
911
      return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
912
    } else {
Adrian Grange's avatar
Adrian Grange committed
913
      return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
914
915
916
    }
  }

Adrian Grange's avatar
Adrian Grange committed
917
  return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
918
919
}

Adrian Grange's avatar
Adrian Grange committed
920
static aom_codec_err_t ctrl_get_render_size(aom_codec_alg_priv_t *ctx,
921
922
                                            va_list args) {
  int *const render_size = va_arg(args, int *);
Jingning Han's avatar
Jingning Han committed
923
924
925
926

  // Only support this function in serial decode.
  if (ctx->frame_parallel_decode) {
    set_error_detail(ctx, "Not supported in frame parallel decode");
Adrian Grange's avatar
Adrian Grange committed
927
    return AOM_CODEC_INCAPABLE;
Jingning Han's avatar
Jingning Han committed
928
929
  }

930
  if (render_size) {
Jingning Han's avatar
Jingning Han committed
931
    if (ctx->frame_workers) {
Adrian Grange's avatar
Adrian Grange committed
932
      AVxWorker *const worker = ctx->frame_workers;
Jingning Han's avatar
Jingning Han committed
933
934
      FrameWorkerData *const frame_worker_data =
          (FrameWorkerData *)worker->data1;
935
      const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
936
937
      render_size[0] = cm->render_width;
      render_size[1] = cm->render_height;
Adrian Grange's avatar
Adrian Grange committed
938
      return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
939
    } else {
Adrian Grange's avatar
Adrian Grange committed
940
      return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
941
942
943
    }
  }

Adrian Grange's avatar
Adrian Grange committed
944
  return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
945
946
}

Adrian Grange's avatar
Adrian Grange committed
947
static aom_codec_err_t ctrl_get_bit_depth(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
948
949
                                          va_list args) {
  unsigned int *const bit_depth = va_arg(args, unsigned int *);
Adrian Grange's avatar
Adrian Grange committed
950
  AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
Jingning Han's avatar
Jingning Han committed
951
952
953
954
955

  if (bit_depth) {
    if (worker) {
      FrameWorkerData *const frame_worker_data =
          (FrameWorkerData *)worker->data1;
956
      const AV1_COMMON *const cm = &frame_worker_data->pbi->common;
Jingning Han's avatar
Jingning Han committed
957
      *bit_depth = cm->bit_depth;
Adrian Grange's avatar
Adrian Grange committed
958
      return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
959
    } else {
Adrian Grange's avatar
Adrian Grange committed
960
      return AOM_CODEC_ERROR;
Jingning Han's avatar
Jingning Han committed
961
962
963
    }
  }

Adrian Grange's avatar
Adrian Grange committed
964
  return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
965
966
}

Adrian Grange's avatar
Adrian Grange committed
967
static aom_codec_err_t ctrl_set_invert_tile_order(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
968
969
                                                  va_list args) {
  ctx->invert_tile_order = va_arg(args, int);
Adrian Grange's avatar
Adrian Grange committed
970
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
971
972
}

Adrian Grange's avatar
Adrian Grange committed
973
static aom_codec_err_t ctrl_set_decryptor(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
974
                                          va_list args) {
Adrian Grange's avatar
Adrian Grange committed
975
  aom_decrypt_init *init = va_arg(args, aom_decrypt_init *);
Jingning Han's avatar
Jingning Han committed
976
977
  ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
  ctx->decrypt_state = init ? init->decrypt_state : NULL;
Adrian Grange's avatar
Adrian Grange committed
978
  return AOM_CODEC_OK;
Jingning Han's avatar
Jingning Han committed
979
980
}

Adrian Grange's avatar
Adrian Grange committed
981
static aom_codec_err_t ctrl_set_byte_alignment(aom_codec_alg_priv_t *ctx,
Jingning Han's avatar
Jingning Han committed
982
983
984
985
986
987
988
989
990
991
                                               va_list args) {
  const int legacy_byte_alignment = 0;
  const int min_byte_alignment = 32;
  const int max_byte_alignment = 1024;
  const int byte_alignment = va_arg(args, int);

  if (byte_alignment != legacy_byte_alignment &&
      (byte_alignment < min_byte_alignment ||
       byte_alignment > max_byte_alignment ||
       (byte_alignment & (byte_alignment - 1)) != 0))
Adrian Grange's avatar
Adrian Grange committed
992
    return AOM_CODEC_INVALID_PARAM;
Jingning Han's avatar
Jingning Han committed
993
994
995

  ctx->byte_alignment = byte_alignment;
  if (ctx->frame_workers) {
Adrian Grange's avatar
Adrian Grange committed
996
    AVxWorker *const worker = ctx->frame_workers;
clang-format's avatar
clang-format committed
997
    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
Jingning Han's avatar
Jingning Han committed
998
999
    frame_worker_data->pbi->common.byte_alignment = byte_alignment;
  }
Adrian Grange's avatar
Adrian Grange committed
1000
  return AOM_CODEC_OK;
For faster browsing, not all history is shown. View entire blame