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