Coverage Report

Created: 2026-05-16 07:49

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
15.0k
                                    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
15.0k
  (void)data;
39
40
15.0k
  if (!ctx->priv) {
41
15.0k
    vpx_codec_alg_priv_t *const priv =
42
15.0k
        (vpx_codec_alg_priv_t *)vpx_calloc(1, sizeof(*priv));
43
15.0k
    if (priv == NULL) return VPX_CODEC_MEM_ERROR;
44
45
15.0k
    ctx->priv = (vpx_codec_priv_t *)priv;
46
15.0k
    ctx->priv->init_flags = ctx->init_flags;
47
15.0k
    priv->si.sz = sizeof(priv->si);
48
15.0k
    priv->flushed = 0;
49
15.0k
    if (ctx->config.dec) {
50
15.0k
      priv->cfg = *ctx->config.dec;
51
15.0k
      ctx->config.dec = &priv->cfg;
52
15.0k
    }
53
15.0k
  }
54
55
15.0k
  return VPX_CODEC_OK;
56
15.0k
}
57
58
15.0k
static vpx_codec_err_t decoder_destroy(vpx_codec_alg_priv_t *ctx) {
59
15.0k
  if (ctx->pbi != NULL) {
60
14.9k
    vp9_decoder_remove(ctx->pbi);
61
14.9k
  }
62
63
15.0k
  if (ctx->buffer_pool) {
64
14.9k
    vp9_free_ref_frame_buffers(ctx->buffer_pool);
65
14.9k
    vp9_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
66
14.9k
  }
67
68
15.0k
  vpx_free(ctx->buffer_pool);
69
15.0k
  vpx_free(ctx);
70
15.0k
  return VPX_CODEC_OK;
71
15.0k
}
72
73
static int parse_bitdepth_colorspace_sampling(BITSTREAM_PROFILE profile,
74
14.7k
                                              struct vpx_read_bit_buffer *rb) {
75
14.7k
  vpx_color_space_t color_space;
76
14.7k
  if (profile >= PROFILE_2) rb->bit_offset += 1;  // Bit-depth 10 or 12.
77
14.7k
  color_space = (vpx_color_space_t)vpx_rb_read_literal(rb, 3);
78
14.7k
  if (color_space != VPX_CS_SRGB) {
79
14.1k
    rb->bit_offset += 1;  // [16,235] (including xvycc) vs [0,255] range.
80
14.1k
    if (profile == PROFILE_1 || profile == PROFILE_3) {
81
6.85k
      rb->bit_offset += 2;  // subsampling x/y.
82
6.85k
      rb->bit_offset += 1;  // unused.
83
6.85k
    }
84
14.1k
  } else {
85
662
    if (profile == PROFILE_1 || profile == PROFILE_3) {
86
227
      rb->bit_offset += 1;  // unused
87
435
    } else {
88
      // RGB is only available in version 1.
89
435
      return 0;
90
435
    }
91
662
  }
92
14.3k
  return 1;
93
14.7k
}
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
58.1k
    int *is_intra_only, vpx_decrypt_cb decrypt_cb, void *decrypt_state) {
98
58.1k
  int intra_only_flag = 0;
99
58.1k
  uint8_t clear_buffer[11];
100
101
58.1k
  if (data + data_sz <= data) return VPX_CODEC_INVALID_PARAM;
102
103
57.7k
  si->is_kf = 0;
104
57.7k
  si->w = si->h = 0;
105
106
57.7k
  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
57.7k
  if (data_sz < 1) return VPX_CODEC_UNSUP_BITSTREAM;
115
116
57.7k
  {
117
57.7k
    int show_frame;
118
57.7k
    int error_resilient;
119
57.7k
    struct vpx_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
120
57.7k
    const int frame_marker = vpx_rb_read_literal(&rb, 2);
121
57.7k
    const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
122
123
57.7k
    if (frame_marker != VP9_FRAME_MARKER) return VPX_CODEC_UNSUP_BITSTREAM;
124
125
31.0k
    if (profile >= MAX_PROFILES) return VPX_CODEC_UNSUP_BITSTREAM;
126
127
30.8k
    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
4.40k
      if (profile > 2 && data_sz < 2) return VPX_CODEC_UNSUP_BITSTREAM;
131
1.47k
      vpx_rb_read_literal(&rb, 3);  // Frame buffer to show.
132
1.47k
      return VPX_CODEC_OK;
133
4.40k
    }
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
26.4k
    if (data_sz < 10) return VPX_CODEC_UNSUP_BITSTREAM;
139
140
18.4k
    si->is_kf = !vpx_rb_read_bit(&rb);
141
18.4k
    show_frame = vpx_rb_read_bit(&rb);
142
18.4k
    error_resilient = vpx_rb_read_bit(&rb);
143
144
18.4k
    if (si->is_kf) {
145
12.7k
      if (!vp9_read_sync_code(&rb)) return VPX_CODEC_UNSUP_BITSTREAM;
146
147
12.0k
      if (!parse_bitdepth_colorspace_sampling(profile, &rb))
148
236
        return VPX_CODEC_UNSUP_BITSTREAM;
149
11.8k
      vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
150
11.8k
    } else {
151
5.70k
      intra_only_flag = show_frame ? 0 : vpx_rb_read_bit(&rb);
152
153
5.70k
      rb.bit_offset += error_resilient ? 0 : 2;  // reset_frame_context
154
155
5.70k
      if (intra_only_flag) {
156
5.20k
        if (!vp9_read_sync_code(&rb)) return VPX_CODEC_UNSUP_BITSTREAM;
157
3.44k
        if (profile > PROFILE_0) {
158
2.68k
          if (!parse_bitdepth_colorspace_sampling(profile, &rb))
159
199
            return VPX_CODEC_UNSUP_BITSTREAM;
160
          // The colorspace info may cause vp9_read_frame_size() to need 11
161
          // bytes.
162
2.48k
          if (data_sz < 11) return VPX_CODEC_UNSUP_BITSTREAM;
163
2.48k
        }
164
2.45k
        rb.bit_offset += REF_FRAMES;  // refresh_frame_flags
165
2.45k
        vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
166
2.45k
      }
167
5.70k
    }
168
18.4k
  }
169
14.8k
  if (is_intra_only != NULL) *is_intra_only = intra_only_flag;
170
14.8k
  return VPX_CODEC_OK;
171
18.4k
}
172
173
static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
174
                                       unsigned int data_sz,
175
0
                                       vpx_codec_stream_info_t *si) {
176
0
  return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
177
0
}
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
258k
                             const char *const error) {
192
258k
  ctx->base.err_detail = error;
193
258k
}
194
195
static vpx_codec_err_t update_error_state(
196
255k
    vpx_codec_alg_priv_t *ctx, const struct vpx_internal_error_info *error) {
197
255k
  if (error->error_code)
198
255k
    set_error_detail(ctx, error->has_detail ? error->detail : NULL);
199
200
255k
  return error->error_code;
201
255k
}
202
203
14.9k
static vpx_codec_err_t init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
204
14.9k
  VP9_COMMON *const cm = &ctx->pbi->common;
205
14.9k
  BufferPool *const pool = cm->buffer_pool;
206
207
14.9k
  cm->new_fb_idx = INVALID_IDX;
208
14.9k
  cm->byte_alignment = ctx->byte_alignment;
209
14.9k
  cm->skip_loop_filter = ctx->skip_loop_filter;
210
211
14.9k
  if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
212
14.9k
    pool->get_fb_cb = ctx->get_ext_fb_cb;
213
14.9k
    pool->release_fb_cb = ctx->release_ext_fb_cb;
214
14.9k
    pool->cb_priv = ctx->ext_priv;
215
14.9k
  } else {
216
0
    pool->get_fb_cb = vp9_get_frame_buffer;
217
0
    pool->release_fb_cb = vp9_release_frame_buffer;
218
219
0
    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
0
    pool->cb_priv = &pool->int_frame_buffers;
226
0
  }
227
228
14.9k
  return VPX_CODEC_OK;
229
14.9k
}
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
29.9k
  do {                                                                   \
253
29.9k
    if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
254
29.9k
      ERROR(#memb " out of range [" #lo ".." #hi "]");                   \
255
29.9k
  } while (0)
256
257
14.9k
static vpx_codec_err_t init_decoder(vpx_codec_alg_priv_t *ctx) {
258
14.9k
  vpx_codec_err_t res;
259
14.9k
  ctx->last_show_frame = -1;
260
14.9k
  ctx->need_resync = 1;
261
14.9k
  ctx->flushed = 0;
262
263
14.9k
  ctx->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
264
14.9k
  if (ctx->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
265
266
14.9k
  ctx->pbi = vp9_decoder_create(ctx->buffer_pool);
267
14.9k
  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
14.9k
  ctx->pbi->max_threads = ctx->cfg.threads;
274
14.9k
  ctx->pbi->inv_tile_order = ctx->invert_tile_order;
275
276
14.9k
  RANGE_CHECK(ctx, row_mt, 0, 1);
277
14.9k
  ctx->pbi->row_mt = ctx->row_mt;
278
279
14.9k
  RANGE_CHECK(ctx, lpf_opt, 0, 1);
280
14.9k
  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
14.9k
  if (!ctx->postproc_cfg_set && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
285
0
    set_default_ppflags(&ctx->postproc_cfg);
286
287
14.9k
  res = init_buffer_callbacks(ctx);
288
14.9k
  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
14.9k
  return res;
295
14.9k
}
296
297
static INLINE void check_resync(vpx_codec_alg_priv_t *const ctx,
298
110k
                                const VP9Decoder *const pbi) {
299
  // Clear resync flag if the decoder got a key frame or intra only frame.
300
110k
  if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
301
39.1k
      (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
302
39.1k
    ctx->need_resync = 0;
303
110k
}
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
409k
                                  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
409k
  if (!ctx->si.h) {
312
58.1k
    int is_intra_only = 0;
313
58.1k
    const vpx_codec_err_t res =
314
58.1k
        decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
315
58.1k
                                 ctx->decrypt_cb, ctx->decrypt_state);
316
58.1k
    if (res != VPX_CODEC_OK) return res;
317
318
16.2k
    if (!ctx->si.is_kf && !is_intra_only) return VPX_CODEC_ERROR;
319
16.2k
  }
320
321
365k
  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
365k
  ctx->pbi->decrypt_cb = ctx->decrypt_cb;
326
365k
  ctx->pbi->decrypt_state = ctx->decrypt_state;
327
328
365k
  if (vp9_receive_compressed_data(ctx->pbi, data_sz, data)) {
329
255k
    ctx->pbi->cur_buf->buf.corrupted = 1;
330
255k
    ctx->pbi->need_resync = 1;
331
255k
    ctx->need_resync = 1;
332
255k
    return update_error_state(ctx, &ctx->pbi->common.error);
333
255k
  }
334
335
110k
  check_resync(ctx, ctx->pbi);
336
337
110k
  return VPX_CODEC_OK;
338
365k
}
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
318k
                                      void *user_priv) {
343
318k
  const uint8_t *data_start = data;
344
318k
  vpx_codec_err_t res;
345
318k
  uint32_t frame_sizes[8];
346
318k
  int frame_count;
347
348
318k
  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
318k
  ctx->flushed = 0;
355
356
  // Initialize the decoder on the first frame.
357
318k
  if (ctx->pbi == NULL) {
358
14.9k
    res = init_decoder(ctx);
359
14.9k
    if (res != VPX_CODEC_OK) return res;
360
14.9k
  }
361
362
318k
  res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
363
318k
                                   ctx->decrypt_cb, ctx->decrypt_state);
364
318k
  if (res != VPX_CODEC_OK) return res;
365
366
314k
  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
314k
  if (frame_count > 0) {
371
5.94k
    const uint8_t *const data_end = data + data_sz;
372
5.94k
    int i;
373
374
6.37k
    for (i = 0; i < frame_count; ++i) {
375
6.17k
      const uint8_t *data_start_copy = data_start;
376
6.17k
      const uint32_t frame_size = frame_sizes[i];
377
6.17k
      if (data_start < data || frame_size > (uint32_t)(data_end - data_start)) {
378
2.83k
        set_error_detail(ctx, "Invalid frame size in index");
379
2.83k
        return VPX_CODEC_CORRUPT_FRAME;
380
2.83k
      }
381
382
3.33k
      res = decode_one(ctx, &data_start_copy, frame_size, user_priv);
383
3.33k
      if (res != VPX_CODEC_OK) return res;
384
385
427
      data_start += frame_size;
386
427
    }
387
308k
  } else {
388
308k
    const uint8_t *const data_end = data + data_sz;
389
418k
    while (data_start < data_end) {
390
406k
      const uint32_t frame_size = (uint32_t)(data_end - data_start);
391
406k
      res = decode_one(ctx, &data_start, frame_size, user_priv);
392
406k
      if (res != VPX_CODEC_OK) return res;
393
394
      // Account for suboptimal termination by the encoder.
395
1.10M
      while (data_start < data_end) {
396
1.09M
        const uint8_t marker =
397
1.09M
            read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
398
1.09M
        if (marker) break;
399
997k
        ++data_start;
400
997k
      }
401
110k
    }
402
308k
  }
403
404
12.6k
  return res;
405
314k
}
406
407
static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
408
12.6k
                                      vpx_codec_iter_t *iter) {
409
12.6k
  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
12.6k
  (void)iter;
414
415
12.6k
  if (ctx->pbi != NULL) {
416
12.6k
    YV12_BUFFER_CONFIG sd;
417
12.6k
    vp9_ppflags_t flags = { 0, 0, 0 };
418
12.6k
    if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC) set_ppflags(ctx, &flags);
419
12.6k
    if (vp9_get_raw_frame(ctx->pbi, &sd, &flags) == 0) {
420
10.7k
      VP9_COMMON *const cm = &ctx->pbi->common;
421
10.7k
      RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
422
10.7k
      ctx->last_show_frame = ctx->pbi->common.new_fb_idx;
423
10.7k
      if (ctx->need_resync) return NULL;
424
8.81k
      yuvconfig2image(&ctx->img, &sd, ctx->user_priv);
425
8.81k
      ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
426
8.81k
      img = &ctx->img;
427
8.81k
      return img;
428
10.7k
    }
429
12.6k
  }
430
1.89k
  return NULL;
431
12.6k
}
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
15.0k
    vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
436
15.0k
  if (cb_get == NULL || cb_release == NULL) {
437
0
    return VPX_CODEC_INVALID_PARAM;
438
15.0k
  } else if (ctx->pbi == NULL) {
439
    // If the decoder has already been initialized, do not accept changes to
440
    // the frame buffer functions.
441
15.0k
    ctx->get_ext_fb_cb = cb_get;
442
15.0k
    ctx->release_ext_fb_cb = cb_release;
443
15.0k
    ctx->ext_priv = cb_priv;
444
15.0k
    return VPX_CODEC_OK;
445
15.0k
  }
446
447
0
  return VPX_CODEC_ERROR;
448
15.0k
}
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
    if (!ctx->pbi) return VPX_CODEC_ERROR;
459
0
    return vp9_set_reference_dec(
460
0
        &ctx->pbi->common, ref_frame_to_vp9_reframe(frame->frame_type), &sd);
461
0
  } else {
462
0
    return VPX_CODEC_INVALID_PARAM;
463
0
  }
464
0
}
465
466
static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
467
0
                                           va_list args) {
468
0
  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
469
470
0
  if (data) {
471
0
    vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
472
0
    YV12_BUFFER_CONFIG sd;
473
0
    image2yuvconfig(&frame->img, &sd);
474
0
    if (!ctx->pbi) return VPX_CODEC_ERROR;
475
0
    return vp9_copy_reference_dec(ctx->pbi, (VP9_REFFRAME)frame->frame_type,
476
0
                                  &sd);
477
0
  } else {
478
0
    return VPX_CODEC_INVALID_PARAM;
479
0
  }
480
0
}
481
482
static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
483
0
                                          va_list args) {
484
0
  vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
485
486
0
  if (data) {
487
0
    if (ctx->pbi) {
488
0
      const int fb_idx = ctx->pbi->common.cur_show_frame_fb_idx;
489
0
      YV12_BUFFER_CONFIG *fb = get_buf_frame(&ctx->pbi->common, fb_idx);
490
0
      if (fb == NULL) return VPX_CODEC_ERROR;
491
0
      yuvconfig2image(&data->img, fb, NULL);
492
0
      return VPX_CODEC_OK;
493
0
    } else {
494
0
      return VPX_CODEC_ERROR;
495
0
    }
496
0
  } else {
497
0
    return VPX_CODEC_INVALID_PARAM;
498
0
  }
499
0
}
500
501
static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
502
0
                                         va_list args) {
503
#if CONFIG_VP9_POSTPROC
504
  vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
505
506
  if (data) {
507
    ctx->postproc_cfg_set = 1;
508
    ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
509
    return VPX_CODEC_OK;
510
  } else {
511
    return VPX_CODEC_INVALID_PARAM;
512
  }
513
#else
514
0
  (void)ctx;
515
0
  (void)args;
516
0
  return VPX_CODEC_INCAPABLE;
517
0
#endif
518
0
}
519
520
static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
521
0
                                          va_list args) {
522
0
  int *const arg = va_arg(args, int *);
523
0
  if (arg == NULL || ctx->pbi == NULL) return VPX_CODEC_INVALID_PARAM;
524
0
  *arg = ctx->pbi->common.base_qindex;
525
0
  return VPX_CODEC_OK;
526
0
}
527
528
static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
529
0
                                                 va_list args) {
530
0
  int *const update_info = va_arg(args, int *);
531
532
0
  if (update_info) {
533
0
    if (ctx->pbi != NULL) {
534
0
      *update_info = ctx->pbi->refresh_frame_flags;
535
0
      return VPX_CODEC_OK;
536
0
    } else {
537
0
      return VPX_CODEC_ERROR;
538
0
    }
539
0
  }
540
541
0
  return VPX_CODEC_INVALID_PARAM;
542
0
}
543
544
static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
545
0
                                                va_list args) {
546
0
  int *corrupted = va_arg(args, int *);
547
548
0
  if (corrupted) {
549
0
    if (ctx->pbi != NULL) {
550
0
      RefCntBuffer *const frame_bufs = ctx->pbi->common.buffer_pool->frame_bufs;
551
0
      if (ctx->pbi->common.frame_to_show == NULL) return VPX_CODEC_ERROR;
552
0
      if (ctx->last_show_frame >= 0)
553
0
        *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
554
0
      return VPX_CODEC_OK;
555
0
    } else {
556
0
      return VPX_CODEC_ERROR;
557
0
    }
558
0
  }
559
560
0
  return VPX_CODEC_INVALID_PARAM;
561
0
}
562
563
static vpx_codec_err_t ctrl_get_frame_size(vpx_codec_alg_priv_t *ctx,
564
0
                                           va_list args) {
565
0
  int *const frame_size = va_arg(args, int *);
566
567
0
  if (frame_size) {
568
0
    if (ctx->pbi != NULL) {
569
0
      const VP9_COMMON *const cm = &ctx->pbi->common;
570
0
      frame_size[0] = cm->width;
571
0
      frame_size[1] = cm->height;
572
0
      return VPX_CODEC_OK;
573
0
    } else {
574
0
      return VPX_CODEC_ERROR;
575
0
    }
576
0
  }
577
578
0
  return VPX_CODEC_INVALID_PARAM;
579
0
}
580
581
static vpx_codec_err_t ctrl_get_render_size(vpx_codec_alg_priv_t *ctx,
582
0
                                            va_list args) {
583
0
  int *const render_size = va_arg(args, int *);
584
585
0
  if (render_size) {
586
0
    if (ctx->pbi != NULL) {
587
0
      const VP9_COMMON *const cm = &ctx->pbi->common;
588
0
      render_size[0] = cm->render_width;
589
0
      render_size[1] = cm->render_height;
590
0
      return VPX_CODEC_OK;
591
0
    } else {
592
0
      return VPX_CODEC_ERROR;
593
0
    }
594
0
  }
595
596
0
  return VPX_CODEC_INVALID_PARAM;
597
0
}
598
599
static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
600
0
                                          va_list args) {
601
0
  unsigned int *const bit_depth = va_arg(args, unsigned int *);
602
603
0
  if (bit_depth) {
604
0
    if (ctx->pbi != NULL) {
605
0
      const VP9_COMMON *const cm = &ctx->pbi->common;
606
0
      *bit_depth = cm->bit_depth;
607
0
      return VPX_CODEC_OK;
608
0
    } else {
609
0
      return VPX_CODEC_ERROR;
610
0
    }
611
0
  }
612
613
0
  return VPX_CODEC_INVALID_PARAM;
614
0
}
615
616
static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
617
0
                                                  va_list args) {
618
0
  ctx->invert_tile_order = va_arg(args, int);
619
0
  return VPX_CODEC_OK;
620
0
}
621
622
static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
623
0
                                          va_list args) {
624
0
  vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
625
0
  ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
626
0
  ctx->decrypt_state = init ? init->decrypt_state : NULL;
627
0
  return VPX_CODEC_OK;
628
0
}
629
630
static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
631
0
                                               va_list args) {
632
0
  const int legacy_byte_alignment = 0;
633
0
  const int min_byte_alignment = 32;
634
0
  const int max_byte_alignment = 1024;
635
0
  const int byte_alignment = va_arg(args, int);
636
637
0
  if (byte_alignment != legacy_byte_alignment &&
638
0
      (byte_alignment < min_byte_alignment ||
639
0
       byte_alignment > max_byte_alignment ||
640
0
       (byte_alignment & (byte_alignment - 1)) != 0))
641
0
    return VPX_CODEC_INVALID_PARAM;
642
643
0
  ctx->byte_alignment = byte_alignment;
644
0
  if (ctx->pbi != NULL) {
645
0
    ctx->pbi->common.byte_alignment = byte_alignment;
646
0
  }
647
0
  return VPX_CODEC_OK;
648
0
}
649
650
static vpx_codec_err_t ctrl_set_skip_loop_filter(vpx_codec_alg_priv_t *ctx,
651
0
                                                 va_list args) {
652
0
  ctx->skip_loop_filter = va_arg(args, int);
653
654
0
  if (ctx->pbi != NULL) {
655
0
    ctx->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
656
0
  }
657
658
0
  return VPX_CODEC_OK;
659
0
}
660
661
static vpx_codec_err_t ctrl_set_spatial_layer_svc(vpx_codec_alg_priv_t *ctx,
662
0
                                                  va_list args) {
663
0
  ctx->svc_decoding = 1;
664
0
  ctx->svc_spatial_layer = va_arg(args, int);
665
0
  if (ctx->svc_spatial_layer < 0)
666
0
    return VPX_CODEC_INVALID_PARAM;
667
0
  else
668
0
    return VPX_CODEC_OK;
669
0
}
670
671
static vpx_codec_err_t ctrl_set_row_mt(vpx_codec_alg_priv_t *ctx,
672
0
                                       va_list args) {
673
0
  ctx->row_mt = va_arg(args, int);
674
675
0
  return VPX_CODEC_OK;
676
0
}
677
678
static vpx_codec_err_t ctrl_enable_lpf_opt(vpx_codec_alg_priv_t *ctx,
679
0
                                           va_list args) {
680
0
  ctx->lpf_opt = va_arg(args, int);
681
682
0
  return VPX_CODEC_OK;
683
0
}
684
685
static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
686
  { VP8_COPY_REFERENCE, ctrl_copy_reference },
687
688
  // Setters
689
  { VP8_SET_REFERENCE, ctrl_set_reference },
690
  { VP8_SET_POSTPROC, ctrl_set_postproc },
691
  { VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
692
  { VPXD_SET_DECRYPTOR, ctrl_set_decryptor },
693
  { VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
694
  { VP9_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
695
  { VP9_DECODE_SVC_SPATIAL_LAYER, ctrl_set_spatial_layer_svc },
696
  { VP9D_SET_ROW_MT, ctrl_set_row_mt },
697
  { VP9D_SET_LOOP_FILTER_OPT, ctrl_enable_lpf_opt },
698
699
  // Getters
700
  { VPXD_GET_LAST_QUANTIZER, ctrl_get_quantizer },
701
  { VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
702
  { VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
703
  { VP9_GET_REFERENCE, ctrl_get_reference },
704
  { VP9D_GET_DISPLAY_SIZE, ctrl_get_render_size },
705
  { VP9D_GET_BIT_DEPTH, ctrl_get_bit_depth },
706
  { VP9D_GET_FRAME_SIZE, ctrl_get_frame_size },
707
708
  { -1, NULL },
709
};
710
711
#ifndef VERSION_STRING
712
#define VERSION_STRING
713
#endif
714
CODEC_INTERFACE(vpx_codec_vp9_dx) = {
715
  "WebM Project VP9 Decoder" VERSION_STRING,
716
  VPX_CODEC_INTERNAL_ABI_VERSION,
717
#if CONFIG_VP9_HIGHBITDEPTH
718
  VPX_CODEC_CAP_HIGHBITDEPTH |
719
#endif
720
      VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
721
      VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER,  // vpx_codec_caps_t
722
  decoder_init,                             // vpx_codec_init_fn_t
723
  decoder_destroy,                          // vpx_codec_destroy_fn_t
724
  decoder_ctrl_maps,                        // vpx_codec_ctrl_fn_map_t
725
  {
726
      // NOLINT
727
      decoder_peek_si,    // vpx_codec_peek_si_fn_t
728
      decoder_get_si,     // vpx_codec_get_si_fn_t
729
      decoder_decode,     // vpx_codec_decode_fn_t
730
      decoder_get_frame,  // vpx_codec_frame_get_fn_t
731
      decoder_set_fb_fn,  // vpx_codec_set_fb_fn_t
732
  },
733
  {
734
      // NOLINT
735
      0,
736
      NULL,  // vpx_codec_enc_cfg_map_t
737
      NULL,  // vpx_codec_encode_fn_t
738
      NULL,  // vpx_codec_get_cx_data_fn_t
739
      NULL,  // vpx_codec_enc_config_set_fn_t
740
      NULL,  // vpx_codec_get_global_headers_fn_t
741
      NULL,  // vpx_codec_get_preview_frame_fn_t
742
      NULL,  // vpx_codec_enc_mr_get_mem_loc_fn_t
743
      NULL   // vpx_codec_enc_mr_free_mem_loc_fn_t
744
  }
745
};