Coverage Report

Created: 2025-10-28 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vp9/vp9_dx_iface.c
Line
Count
Source
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include <stdlib.h>
12
#include <string.h>
13
14
#include "./vpx_config.h"
15
#include "./vpx_version.h"
16
17
#include "vpx/internal/vpx_codec_internal.h"
18
#include "vpx/vp8dx.h"
19
#include "vpx/vpx_decoder.h"
20
#include "vpx_dsp/bitreader_buffer.h"
21
#include "vpx_dsp/vpx_dsp_common.h"
22
23
#include "vp9/common/vp9_alloccommon.h"
24
#include "vp9/common/vp9_frame_buffers.h"
25
26
#include "vp9/decoder/vp9_decodeframe.h"
27
28
#include "vp9/vp9_dx_iface.h"
29
#include "vp9/vp9_iface_common.h"
30
31
#define VP9_CAP_POSTPROC (CONFIG_VP9_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
32
33
static vpx_codec_err_t decoder_init(vpx_codec_ctx_t *ctx,
34
16.7k
                                    vpx_codec_priv_enc_mr_cfg_t *data) {
35
  // This function only allocates space for the vpx_codec_alg_priv_t
36
  // structure. More memory may be required at the time the stream
37
  // information becomes known.
38
16.7k
  (void)data;
39
40
16.7k
  if (!ctx->priv) {
41
16.7k
    vpx_codec_alg_priv_t *const priv =
42
16.7k
        (vpx_codec_alg_priv_t *)vpx_calloc(1, sizeof(*priv));
43
16.7k
    if (priv == NULL) return VPX_CODEC_MEM_ERROR;
44
45
16.7k
    ctx->priv = (vpx_codec_priv_t *)priv;
46
16.7k
    ctx->priv->init_flags = ctx->init_flags;
47
16.7k
    priv->si.sz = sizeof(priv->si);
48
16.7k
    priv->flushed = 0;
49
16.7k
    if (ctx->config.dec) {
50
16.7k
      priv->cfg = *ctx->config.dec;
51
16.7k
      ctx->config.dec = &priv->cfg;
52
16.7k
    }
53
16.7k
  }
54
55
16.7k
  return VPX_CODEC_OK;
56
16.7k
}
57
58
16.7k
static vpx_codec_err_t decoder_destroy(vpx_codec_alg_priv_t *ctx) {
59
16.7k
  if (ctx->pbi != NULL) {
60
16.6k
    vp9_decoder_remove(ctx->pbi);
61
16.6k
  }
62
63
16.7k
  if (ctx->buffer_pool) {
64
16.6k
    vp9_free_ref_frame_buffers(ctx->buffer_pool);
65
16.6k
    vp9_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
66
16.6k
  }
67
68
16.7k
  vpx_free(ctx->buffer_pool);
69
16.7k
  vpx_free(ctx);
70
16.7k
  return VPX_CODEC_OK;
71
16.7k
}
72
73
static int parse_bitdepth_colorspace_sampling(BITSTREAM_PROFILE profile,
74
57.4k
                                              struct vpx_read_bit_buffer *rb) {
75
57.4k
  vpx_color_space_t color_space;
76
57.4k
  if (profile >= PROFILE_2) rb->bit_offset += 1;  // Bit-depth 10 or 12.
77
57.4k
  color_space = (vpx_color_space_t)vpx_rb_read_literal(rb, 3);
78
57.4k
  if (color_space != VPX_CS_SRGB) {
79
56.9k
    rb->bit_offset += 1;  // [16,235] (including xvycc) vs [0,255] range.
80
56.9k
    if (profile == PROFILE_1 || profile == PROFILE_3) {
81
14.5k
      rb->bit_offset += 2;  // subsampling x/y.
82
14.5k
      rb->bit_offset += 1;  // unused.
83
14.5k
    }
84
56.9k
  } else {
85
428
    if (profile == PROFILE_1 || profile == PROFILE_3) {
86
298
      rb->bit_offset += 1;  // unused
87
298
    } else {
88
      // RGB is only available in version 1.
89
130
      return 0;
90
130
    }
91
428
  }
92
57.2k
  return 1;
93
57.4k
}
94
95
static vpx_codec_err_t decoder_peek_si_internal(
96
    const uint8_t *data, unsigned int data_sz, vpx_codec_stream_info_t *si,
97
189k
    int *is_intra_only, vpx_decrypt_cb decrypt_cb, void *decrypt_state) {
98
189k
  int intra_only_flag = 0;
99
189k
  uint8_t clear_buffer[11];
100
101
189k
  if (data + data_sz <= data) return VPX_CODEC_INVALID_PARAM;
102
103
189k
  si->is_kf = 0;
104
189k
  si->w = si->h = 0;
105
106
189k
  if (decrypt_cb) {
107
0
    data_sz = VPXMIN(sizeof(clear_buffer), data_sz);
108
0
    decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
109
0
    data = clear_buffer;
110
0
  }
111
112
  // A maximum of 6 bits are needed to read the frame marker, profile and
113
  // show_existing_frame.
114
189k
  if (data_sz < 1) return VPX_CODEC_UNSUP_BITSTREAM;
115
116
189k
  {
117
189k
    int show_frame;
118
189k
    int error_resilient;
119
189k
    struct vpx_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
120
189k
    const int frame_marker = vpx_rb_read_literal(&rb, 2);
121
189k
    const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
122
123
189k
    if (frame_marker != VP9_FRAME_MARKER) return VPX_CODEC_UNSUP_BITSTREAM;
124
125
161k
    if (profile >= MAX_PROFILES) return VPX_CODEC_UNSUP_BITSTREAM;
126
127
161k
    if (vpx_rb_read_bit(&rb)) {  // show an existing frame
128
      // If profile is > 2 and show_existing_frame is true, then at least 1 more
129
      // byte (6+3=9 bits) is needed.
130
2.39k
      if (profile > 2 && data_sz < 2) return VPX_CODEC_UNSUP_BITSTREAM;
131
2.11k
      vpx_rb_read_literal(&rb, 3);  // Frame buffer to show.
132
2.11k
      return VPX_CODEC_OK;
133
2.39k
    }
134
135
    // For the rest of the function, a maximum of 9 more bytes are needed
136
    // (computed by taking the maximum possible bits needed in each case). Note
137
    // that this has to be updated if we read any more bits in this function.
138
158k
    if (data_sz < 10) return VPX_CODEC_UNSUP_BITSTREAM;
139
140
158k
    si->is_kf = !vpx_rb_read_bit(&rb);
141
158k
    show_frame = vpx_rb_read_bit(&rb);
142
158k
    error_resilient = vpx_rb_read_bit(&rb);
143
144
158k
    if (si->is_kf) {
145
57.3k
      if (!vp9_read_sync_code(&rb)) return VPX_CODEC_UNSUP_BITSTREAM;
146
147
56.3k
      if (!parse_bitdepth_colorspace_sampling(profile, &rb))
148
118
        return VPX_CODEC_UNSUP_BITSTREAM;
149
56.1k
      vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
150
100k
    } else {
151
100k
      intra_only_flag = show_frame ? 0 : vpx_rb_read_bit(&rb);
152
153
100k
      rb.bit_offset += error_resilient ? 0 : 2;  // reset_frame_context
154
155
100k
      if (intra_only_flag) {
156
54.7k
        if (!vp9_read_sync_code(&rb)) return VPX_CODEC_UNSUP_BITSTREAM;
157
51.2k
        if (profile > PROFILE_0) {
158
1.09k
          if (!parse_bitdepth_colorspace_sampling(profile, &rb))
159
12
            return VPX_CODEC_UNSUP_BITSTREAM;
160
          // The colorspace info may cause vp9_read_frame_size() to need 11
161
          // bytes.
162
1.07k
          if (data_sz < 11) return VPX_CODEC_UNSUP_BITSTREAM;
163
1.07k
        }
164
50.8k
        rb.bit_offset += REF_FRAMES;  // refresh_frame_flags
165
50.8k
        vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
166
50.8k
      }
167
100k
    }
168
158k
  }
169
153k
  if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
170
153k
  return VPX_CODEC_OK;
171
158k
}
172
173
static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
174
                                       unsigned int data_sz,
175
170k
                                       vpx_codec_stream_info_t *si) {
176
170k
  return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
177
170k
}
178
179
static vpx_codec_err_t decoder_get_si(vpx_codec_alg_priv_t *ctx,
180
0
                                      vpx_codec_stream_info_t *si) {
181
0
  const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
182
0
                        ? sizeof(vp9_stream_info_t)
183
0
                        : sizeof(vpx_codec_stream_info_t);
184
0
  memcpy(si, &ctx->si, sz);
185
0
  si->sz = (unsigned int)sz;
186
187
0
  return VPX_CODEC_OK;
188
0
}
189
190
static void set_error_detail(vpx_codec_alg_priv_t *ctx,
191
140k
                             const char *const error) {
192
140k
  ctx->base.err_detail = error;
193
140k
}
194
195
static vpx_codec_err_t update_error_state(
196
138k
    vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
197
138k
  if (error->error_code)
198
138k
    set_error_detail(ctx, error->has_detail ? error->detail : NULL);
199
200
138k
  return error->error_code;
201
138k
}
202
203
16.6k
static vpx_codec_err_t init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
204
16.6k
  VP9_COMMON *const cm = &ctx->pbi->common;
205
16.6k
  BufferPool *const pool = cm->buffer_pool;
206
207
16.6k
  cm->new_fb_idx = INVALID_IDX;
208
16.6k
  cm->byte_alignment = ctx->byte_alignment;
209
16.6k
  cm->skip_loop_filter = ctx->skip_loop_filter;
210
211
16.6k
  if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
212
0
    pool->get_fb_cb = ctx->get_ext_fb_cb;
213
0
    pool->release_fb_cb = ctx->release_ext_fb_cb;
214
0
    pool->cb_priv = ctx->ext_priv;
215
16.6k
  } else {
216
16.6k
    pool->get_fb_cb = vp9_get_frame_buffer;
217
16.6k
    pool->release_fb_cb = vp9_release_frame_buffer;
218
219
16.6k
    if (vp9_alloc_internal_frame_buffers(&pool->int_frame_buffers)) {
220
0
      vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
221
0
                         "Failed to initialize internal frame buffers");
222
0
      return VPX_CODEC_MEM_ERROR;
223
0
    }
224
225
16.6k
    pool->cb_priv = &pool->int_frame_buffers;
226
16.6k
  }
227
228
16.6k
  return VPX_CODEC_OK;
229
16.6k
}
230
231
0
static void set_default_ppflags(vp8_postproc_cfg_t *cfg) {
232
0
  cfg->post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
233
0
  cfg->deblocking_level = 4;
234
0
  cfg->noise_level = 0;
235
0
}
236
237
0
static void set_ppflags(const vpx_codec_alg_priv_t *ctx, vp9_ppflags_t *flags) {
238
0
  flags->post_proc_flag = ctx->postproc_cfg.post_proc_flag;
239
240
0
  flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
241
0
  flags->noise_level = ctx->postproc_cfg.noise_level;
242
0
}
243
244
#undef ERROR
245
#define ERROR(str)                  \
246
0
  do {                              \
247
0
    ctx->base.err_detail = str;     \
248
0
    return VPX_CODEC_INVALID_PARAM; \
249
0
  } while (0)
250
251
#define RANGE_CHECK(p, memb, lo, hi)                                     \
252
33.3k
  do {                                                                   \
253
33.3k
    if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
254
33.3k
      ERROR(#memb " out of range [" #lo ".." #hi "]");                   \
255
33.3k
  } while (0)
256
257
16.6k
static vpx_codec_err_t init_decoder(vpx_codec_alg_priv_t *ctx) {
258
16.6k
  vpx_codec_err_t res;
259
16.6k
  ctx->last_show_frame = -1;
260
16.6k
  ctx->need_resync = 1;
261
16.6k
  ctx->flushed = 0;
262
263
16.6k
  ctx->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
264
16.6k
  if (ctx->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
265
266
16.6k
  ctx->pbi = vp9_decoder_create(ctx->buffer_pool);
267
16.6k
  if (ctx->pbi == NULL) {
268
0
    vpx_free(ctx->buffer_pool);
269
0
    ctx->buffer_pool = NULL;
270
0
    set_error_detail(ctx, "Failed to allocate decoder");
271
0
    return VPX_CODEC_MEM_ERROR;
272
0
  }
273
16.6k
  ctx->pbi->max_threads = ctx->cfg.threads;
274
16.6k
  ctx->pbi->inv_tile_order = ctx->invert_tile_order;
275
276
16.6k
  RANGE_CHECK(ctx, row_mt, 0, 1);
277
16.6k
  ctx->pbi->row_mt = ctx->row_mt;
278
279
16.6k
  RANGE_CHECK(ctx, lpf_opt, 0, 1);
280
16.6k
  ctx->pbi->lpf_mt_opt = ctx->lpf_opt;
281
282
  // If postprocessing was enabled by the application and a
283
  // configuration has not been provided, default it.
284
16.6k
  if (!ctx->postproc_cfg_set && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
285
0
    set_default_ppflags(&ctx->postproc_cfg);
286
287
16.6k
  res = init_buffer_callbacks(ctx);
288
16.6k
  if (res != VPX_CODEC_OK) {
289
0
    vpx_free(ctx->buffer_pool);
290
0
    ctx->buffer_pool = NULL;
291
0
    vp9_decoder_remove(ctx->pbi);
292
0
    ctx->pbi = NULL;
293
0
  }
294
16.6k
  return res;
295
16.6k
}
296
297
static INLINE void check_resync(vpx_codec_alg_priv_t *const ctx,
298
92.5k
                                const VP9Decoder *const pbi) {
299
  // Clear resync flag if the decoder got a key frame or intra only frame.
300
92.5k
  if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
301
14.8k
      (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
302
14.8k
    ctx->need_resync = 0;
303
92.5k
}
304
305
static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
306
                                  const uint8_t **data, unsigned int data_sz,
307
233k
                                  void *user_priv) {
308
  // Determine the stream parameters. Note that we rely on peek_si to
309
  // validate that we have a buffer that does not wrap around the top
310
  // of the heap.
311
233k
  if (!ctx->si.h) {
312
18.9k
    int is_intra_only = 0;
313
18.9k
    const vpx_codec_err_t res =
314
18.9k
        decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
315
18.9k
                                 ctx->decrypt_cb, ctx->decrypt_state);
316
18.9k
    if (res != VPX_CODEC_OK) return res;
317
318
16.8k
    if (!ctx->si.is_kf && !is_intra_only) return VPX_CODEC_ERROR;
319
16.8k
  }
320
321
231k
  ctx->user_priv = user_priv;
322
323
  // Set these even if already initialized.  The caller may have changed the
324
  // decrypt config between frames.
325
231k
  ctx->pbi->decrypt_cb = ctx->decrypt_cb;
326
231k
  ctx->pbi->decrypt_state = ctx->decrypt_state;
327
328
231k
  if (vp9_receive_compressed_data(ctx->pbi, data_sz, data)) {
329
138k
    ctx->pbi->cur_buf->buf.corrupted = 1;
330
138k
    ctx->pbi->need_resync = 1;
331
138k
    ctx->need_resync = 1;
332
138k
    return update_error_state(ctx, &ctx->pbi->common.error);
333
138k
  }
334
335
92.5k
  check_resync(ctx, ctx->pbi);
336
337
92.5k
  return VPX_CODEC_OK;
338
231k
}
339
340
static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
341
                                      const uint8_t *data, unsigned int data_sz,
342
166k
                                      void *user_priv) {
343
166k
  const uint8_t *data_start = data;
344
166k
  vpx_codec_err_t res;
345
166k
  uint32_t frame_sizes[8];
346
166k
  int frame_count;
347
348
166k
  if (data == NULL && data_sz == 0) {
349
0
    ctx->flushed = 1;
350
0
    return VPX_CODEC_OK;
351
0
  }
352
353
  // Reset flushed when receiving a valid frame.
354
166k
  ctx->flushed = 0;
355
356
  // Initialize the decoder on the first frame.
357
166k
  if (ctx->pbi == NULL) {
358
16.6k
    res = init_decoder(ctx);
359
16.6k
    if (res != VPX_CODEC_OK) return res;
360
16.6k
  }
361
362
166k
  res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
363
166k
                                   ctx->decrypt_cb, ctx->decrypt_state);
364
166k
  if (res != VPX_CODEC_OK) return res;
365
366
166k
  if (ctx->svc_decoding && ctx->svc_spatial_layer < frame_count - 1)
367
0
    frame_count = ctx->svc_spatial_layer + 1;
368
369
  // Decode in serial mode.
370
166k
  if (frame_count > 0) {
371
10.2k
    const uint8_t *const data_end = data + data_sz;
372
10.2k
    int i;
373
374
16.3k
    for (i = 0; i < frame_count; ++i) {
375
13.7k
      const uint8_t *data_start_copy = data_start;
376
13.7k
      const uint32_t frame_size = frame_sizes[i];
377
13.7k
      if (data_start < data || frame_size > (uint32_t)(data_end - data_start)) {
378
1.38k
        set_error_detail(ctx, "Invalid frame size in index");
379
1.38k
        return VPX_CODEC_CORRUPT_FRAME;
380
1.38k
      }
381
382
12.3k
      res = decode_one(ctx, &data_start_copy, frame_size, user_priv);
383
12.3k
      if (res != VPX_CODEC_OK) return res;
384
385
6.10k
      data_start += frame_size;
386
6.10k
    }
387
156k
  } else {
388
156k
    const uint8_t *const data_end = data + data_sz;
389
242k
    while (data_start < data_end) {
390
221k
      const uint32_t frame_size = (uint32_t)(data_end - data_start);
391
221k
      res = decode_one(ctx, &data_start, frame_size, user_priv);
392
221k
      if (res != VPX_CODEC_OK) return res;
393
394
      // Account for suboptimal termination by the encoder.
395
573k
      while (data_start < data_end) {
396
552k
        const uint8_t marker =
397
552k
            read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
398
552k
        if (marker) break;
399
487k
        ++data_start;
400
487k
      }
401
86.4k
    }
402
156k
  }
403
404
23.7k
  return res;
405
166k
}
406
407
static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
408
193k
                                      vpx_codec_iter_t *iter) {
409
193k
  vpx_image_t *img = NULL;
410
411
  // Legacy parameter carried over from VP8. Has no effect for VP9 since we
412
  // always return only 1 frame per decode call.
413
193k
  (void)iter;
414
415
193k
  if (ctx->pbi != NULL) {
416
192k
    YV12_BUFFER_CONFIG sd;
417
192k
    vp9_ppflags_t flags = { 0, 0, 0 };
418
192k
    if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC) set_ppflags(ctx, &flags);
419
192k
    if (vp9_get_raw_frame(ctx->pbi, &sd, &flags) == 0) {
420
22.9k
      VP9_COMMON *const cm = &ctx->pbi->common;
421
22.9k
      RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
422
22.9k
      ctx->last_show_frame = ctx->pbi->common.new_fb_idx;
423
22.9k
      if (ctx->need_resync) return NULL;
424
22.6k
      yuvconfig2image(&ctx->img, &sd, ctx->user_priv);
425
22.6k
      ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
426
22.6k
      img = &ctx->img;
427
22.6k
      return img;
428
22.9k
    }
429
192k
  }
430
170k
  return NULL;
431
193k
}
432
433
static vpx_codec_err_t decoder_set_fb_fn(
434
    vpx_codec_alg_priv_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get,
435
0
    vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
436
0
  if (cb_get == NULL || cb_release == NULL) {
437
0
    return VPX_CODEC_INVALID_PARAM;
438
0
  } else if (ctx->pbi == NULL) {
439
    // If the decoder has already been initialized, do not accept changes to
440
    // the frame buffer functions.
441
0
    ctx->get_ext_fb_cb = cb_get;
442
0
    ctx->release_ext_fb_cb = cb_release;
443
0
    ctx->ext_priv = cb_priv;
444
0
    return VPX_CODEC_OK;
445
0
  }
446
447
0
  return VPX_CODEC_ERROR;
448
0
}
449
450
static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
451
0
                                          va_list args) {
452
0
  vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
453
454
0
  if (data) {
455
0
    vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
456
0
    YV12_BUFFER_CONFIG sd;
457
0
    image2yuvconfig(&frame->img, &sd);
458
0
    return vp9_set_reference_dec(
459
0
        &ctx->pbi->common, ref_frame_to_vp9_reframe(frame->frame_type), &sd);
460
0
  } else {
461
0
    return VPX_CODEC_INVALID_PARAM;
462
0
  }
463
0
}
464
465
static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
466
0
                                           va_list args) {
467
0
  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
468
469
0
  if (data) {
470
0
    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
471
0
    YV12_BUFFER_CONFIG sd;
472
0
    image2yuvconfig(&frame->img, &sd);
473
0
    return vp9_copy_reference_dec(ctx->pbi, (VP9_REFFRAME)frame->frame_type,
474
0
                                  &sd);
475
0
  } else {
476
0
    return VPX_CODEC_INVALID_PARAM;
477
0
  }
478
0
}
479
480
static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
481
0
                                          va_list args) {
482
0
  vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
483
484
0
  if (data) {
485
0
    if (ctx->pbi) {
486
0
      const int fb_idx = ctx->pbi->common.cur_show_frame_fb_idx;
487
0
      YV12_BUFFER_CONFIG *fb = get_buf_frame(&ctx->pbi->common, fb_idx);
488
0
      if (fb == NULL) return VPX_CODEC_ERROR;
489
0
      yuvconfig2image(&data->img, fb, NULL);
490
0
      return VPX_CODEC_OK;
491
0
    } else {
492
0
      return VPX_CODEC_ERROR;
493
0
    }
494
0
  } else {
495
0
    return VPX_CODEC_INVALID_PARAM;
496
0
  }
497
0
}
498
499
static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
500
0
                                         va_list args) {
501
#if CONFIG_VP9_POSTPROC
502
  vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
503
504
  if (data) {
505
    ctx->postproc_cfg_set = 1;
506
    ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
507
    return VPX_CODEC_OK;
508
  } else {
509
    return VPX_CODEC_INVALID_PARAM;
510
  }
511
#else
512
0
  (void)ctx;
513
0
  (void)args;
514
0
  return VPX_CODEC_INCAPABLE;
515
0
#endif
516
0
}
517
518
static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
519
0
                                          va_list args) {
520
0
  int *const arg = va_arg(args, int *);
521
0
  if (arg == NULL || ctx->pbi == NULL) return VPX_CODEC_INVALID_PARAM;
522
0
  *arg = ctx->pbi->common.base_qindex;
523
0
  return VPX_CODEC_OK;
524
0
}
525
526
static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
527
0
                                                 va_list args) {
528
0
  int *const update_info = va_arg(args, int *);
529
530
0
  if (update_info) {
531
0
    if (ctx->pbi != NULL) {
532
0
      *update_info = ctx->pbi->refresh_frame_flags;
533
0
      return VPX_CODEC_OK;
534
0
    } else {
535
0
      return VPX_CODEC_ERROR;
536
0
    }
537
0
  }
538
539
0
  return VPX_CODEC_INVALID_PARAM;
540
0
}
541
542
static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
543
0
                                                va_list args) {
544
0
  int *corrupted = va_arg(args, int *);
545
546
0
  if (corrupted) {
547
0
    if (ctx->pbi != NULL) {
548
0
      RefCntBuffer *const frame_bufs = ctx->pbi->common.buffer_pool->frame_bufs;
549
0
      if (ctx->pbi->common.frame_to_show == NULL) return VPX_CODEC_ERROR;
550
0
      if (ctx->last_show_frame >= 0)
551
0
        *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
552
0
      return VPX_CODEC_OK;
553
0
    } else {
554
0
      return VPX_CODEC_ERROR;
555
0
    }
556
0
  }
557
558
0
  return VPX_CODEC_INVALID_PARAM;
559
0
}
560
561
static vpx_codec_err_t ctrl_get_frame_size(vpx_codec_alg_priv_t *ctx,
562
0
                                           va_list args) {
563
0
  int *const frame_size = va_arg(args, int *);
564
565
0
  if (frame_size) {
566
0
    if (ctx->pbi != NULL) {
567
0
      const VP9_COMMON *const cm = &ctx->pbi->common;
568
0
      frame_size[0] = cm->width;
569
0
      frame_size[1] = cm->height;
570
0
      return VPX_CODEC_OK;
571
0
    } else {
572
0
      return VPX_CODEC_ERROR;
573
0
    }
574
0
  }
575
576
0
  return VPX_CODEC_INVALID_PARAM;
577
0
}
578
579
static vpx_codec_err_t ctrl_get_render_size(vpx_codec_alg_priv_t *ctx,
580
0
                                            va_list args) {
581
0
  int *const render_size = va_arg(args, int *);
582
583
0
  if (render_size) {
584
0
    if (ctx->pbi != NULL) {
585
0
      const VP9_COMMON *const cm = &ctx->pbi->common;
586
0
      render_size[0] = cm->render_width;
587
0
      render_size[1] = cm->render_height;
588
0
      return VPX_CODEC_OK;
589
0
    } else {
590
0
      return VPX_CODEC_ERROR;
591
0
    }
592
0
  }
593
594
0
  return VPX_CODEC_INVALID_PARAM;
595
0
}
596
597
static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
598
0
                                          va_list args) {
599
0
  unsigned int *const bit_depth = va_arg(args, unsigned int *);
600
601
0
  if (bit_depth) {
602
0
    if (ctx->pbi != NULL) {
603
0
      const VP9_COMMON *const cm = &ctx->pbi->common;
604
0
      *bit_depth = cm->bit_depth;
605
0
      return VPX_CODEC_OK;
606
0
    } else {
607
0
      return VPX_CODEC_ERROR;
608
0
    }
609
0
  }
610
611
0
  return VPX_CODEC_INVALID_PARAM;
612
0
}
613
614
static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
615
0
                                                  va_list args) {
616
0
  ctx->invert_tile_order = va_arg(args, int);
617
0
  return VPX_CODEC_OK;
618
0
}
619
620
static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
621
0
                                          va_list args) {
622
0
  vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
623
0
  ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
624
0
  ctx->decrypt_state = init ? init->decrypt_state : NULL;
625
0
  return VPX_CODEC_OK;
626
0
}
627
628
static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
629
0
                                               va_list args) {
630
0
  const int legacy_byte_alignment = 0;
631
0
  const int min_byte_alignment = 32;
632
0
  const int max_byte_alignment = 1024;
633
0
  const int byte_alignment = va_arg(args, int);
634
635
0
  if (byte_alignment != legacy_byte_alignment &&
636
0
      (byte_alignment < min_byte_alignment ||
637
0
       byte_alignment > max_byte_alignment ||
638
0
       (byte_alignment & (byte_alignment - 1)) != 0))
639
0
    return VPX_CODEC_INVALID_PARAM;
640
641
0
  ctx->byte_alignment = byte_alignment;
642
0
  if (ctx->pbi != NULL) {
643
0
    ctx->pbi->common.byte_alignment = byte_alignment;
644
0
  }
645
0
  return VPX_CODEC_OK;
646
0
}
647
648
static vpx_codec_err_t ctrl_set_skip_loop_filter(vpx_codec_alg_priv_t *ctx,
649
0
                                                 va_list args) {
650
0
  ctx->skip_loop_filter = va_arg(args, int);
651
652
0
  if (ctx->pbi != NULL) {
653
0
    ctx->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
654
0
  }
655
656
0
  return VPX_CODEC_OK;
657
0
}
658
659
static vpx_codec_err_t ctrl_set_spatial_layer_svc(vpx_codec_alg_priv_t *ctx,
660
0
                                                  va_list args) {
661
0
  ctx->svc_decoding = 1;
662
0
  ctx->svc_spatial_layer = va_arg(args, int);
663
0
  if (ctx->svc_spatial_layer < 0)
664
0
    return VPX_CODEC_INVALID_PARAM;
665
0
  else
666
0
    return VPX_CODEC_OK;
667
0
}
668
669
static vpx_codec_err_t ctrl_set_row_mt(vpx_codec_alg_priv_t *ctx,
670
0
                                       va_list args) {
671
0
  ctx->row_mt = va_arg(args, int);
672
673
0
  return VPX_CODEC_OK;
674
0
}
675
676
static vpx_codec_err_t ctrl_enable_lpf_opt(vpx_codec_alg_priv_t *ctx,
677
12.1k
                                           va_list args) {
678
12.1k
  ctx->lpf_opt = va_arg(args, int);
679
680
12.1k
  return VPX_CODEC_OK;
681
12.1k
}
682
683
static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
684
  { VP8_COPY_REFERENCE, ctrl_copy_reference },
685
686
  // Setters
687
  { VP8_SET_REFERENCE, ctrl_set_reference },
688
  { VP8_SET_POSTPROC, ctrl_set_postproc },
689
  { VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
690
  { VPXD_SET_DECRYPTOR, ctrl_set_decryptor },
691
  { VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
692
  { VP9_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
693
  { VP9_DECODE_SVC_SPATIAL_LAYER, ctrl_set_spatial_layer_svc },
694
  { VP9D_SET_ROW_MT, ctrl_set_row_mt },
695
  { VP9D_SET_LOOP_FILTER_OPT, ctrl_enable_lpf_opt },
696
697
  // Getters
698
  { VPXD_GET_LAST_QUANTIZER, ctrl_get_quantizer },
699
  { VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
700
  { VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
701
  { VP9_GET_REFERENCE, ctrl_get_reference },
702
  { VP9D_GET_DISPLAY_SIZE, ctrl_get_render_size },
703
  { VP9D_GET_BIT_DEPTH, ctrl_get_bit_depth },
704
  { VP9D_GET_FRAME_SIZE, ctrl_get_frame_size },
705
706
  { -1, NULL },
707
};
708
709
#ifndef VERSION_STRING
710
#define VERSION_STRING
711
#endif
712
CODEC_INTERFACE(vpx_codec_vp9_dx) = {
713
  "WebM Project VP9 Decoder" VERSION_STRING,
714
  VPX_CODEC_INTERNAL_ABI_VERSION,
715
#if CONFIG_VP9_HIGHBITDEPTH
716
  VPX_CODEC_CAP_HIGHBITDEPTH |
717
#endif
718
      VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
719
      VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER,  // vpx_codec_caps_t
720
  decoder_init,                             // vpx_codec_init_fn_t
721
  decoder_destroy,                          // vpx_codec_destroy_fn_t
722
  decoder_ctrl_maps,                        // vpx_codec_ctrl_fn_map_t
723
  {
724
      // NOLINT
725
      decoder_peek_si,    // vpx_codec_peek_si_fn_t
726
      decoder_get_si,     // vpx_codec_get_si_fn_t
727
      decoder_decode,     // vpx_codec_decode_fn_t
728
      decoder_get_frame,  // vpx_codec_frame_get_fn_t
729
      decoder_set_fb_fn,  // vpx_codec_set_fb_fn_t
730
  },
731
  {
732
      // NOLINT
733
      0,
734
      NULL,  // vpx_codec_enc_cfg_map_t
735
      NULL,  // vpx_codec_encode_fn_t
736
      NULL,  // vpx_codec_get_cx_data_fn_t
737
      NULL,  // vpx_codec_enc_config_set_fn_t
738
      NULL,  // vpx_codec_get_global_headers_fn_t
739
      NULL,  // vpx_codec_get_preview_frame_fn_t
740
      NULL,  // vpx_codec_enc_mr_get_mem_loc_fn_t
741
      NULL   // vpx_codec_enc_mr_free_mem_loc_fn_t
742
  }
743
};