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 vpx_codec_err_t 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 return VPX_CODEC_MEM_ERROR;
224 }
225
226 pool->cb_priv = &pool->int_frame_buffers;
227 }
228
229 return VPX_CODEC_OK;
230 }
231
set_default_ppflags(vp8_postproc_cfg_t * cfg)232 static void set_default_ppflags(vp8_postproc_cfg_t *cfg) {
233 cfg->post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
234 cfg->deblocking_level = 4;
235 cfg->noise_level = 0;
236 }
237
set_ppflags(const vpx_codec_alg_priv_t * ctx,vp9_ppflags_t * flags)238 static void set_ppflags(const vpx_codec_alg_priv_t *ctx, vp9_ppflags_t *flags) {
239 flags->post_proc_flag = ctx->postproc_cfg.post_proc_flag;
240
241 flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
242 flags->noise_level = ctx->postproc_cfg.noise_level;
243 }
244
245 #undef ERROR
246 #define ERROR(str) \
247 do { \
248 ctx->base.err_detail = str; \
249 return VPX_CODEC_INVALID_PARAM; \
250 } while (0)
251
252 #define RANGE_CHECK(p, memb, lo, hi) \
253 do { \
254 if (!(((p)->memb == (lo) || (p)->memb > (lo)) && (p)->memb <= (hi))) \
255 ERROR(#memb " out of range [" #lo ".." #hi "]"); \
256 } while (0)
257
init_decoder(vpx_codec_alg_priv_t * ctx)258 static vpx_codec_err_t init_decoder(vpx_codec_alg_priv_t *ctx) {
259 vpx_codec_err_t res;
260 ctx->last_show_frame = -1;
261 ctx->need_resync = 1;
262 ctx->flushed = 0;
263
264 ctx->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
265 if (ctx->buffer_pool == NULL) return VPX_CODEC_MEM_ERROR;
266
267 ctx->pbi = vp9_decoder_create(ctx->buffer_pool);
268 if (ctx->pbi == NULL) {
269 vpx_free(ctx->buffer_pool);
270 ctx->buffer_pool = NULL;
271 set_error_detail(ctx, "Failed to allocate decoder");
272 return VPX_CODEC_MEM_ERROR;
273 }
274 ctx->pbi->max_threads = ctx->cfg.threads;
275 ctx->pbi->inv_tile_order = ctx->invert_tile_order;
276
277 RANGE_CHECK(ctx, row_mt, 0, 1);
278 ctx->pbi->row_mt = ctx->row_mt;
279
280 RANGE_CHECK(ctx, lpf_opt, 0, 1);
281 ctx->pbi->lpf_mt_opt = ctx->lpf_opt;
282
283 // If postprocessing was enabled by the application and a
284 // configuration has not been provided, default it.
285 if (!ctx->postproc_cfg_set && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
286 set_default_ppflags(&ctx->postproc_cfg);
287
288 res = init_buffer_callbacks(ctx);
289 if (res != VPX_CODEC_OK) {
290 vpx_free(ctx->buffer_pool);
291 ctx->buffer_pool = NULL;
292 vp9_decoder_remove(ctx->pbi);
293 ctx->pbi = NULL;
294 }
295 return res;
296 }
297
check_resync(vpx_codec_alg_priv_t * const ctx,const VP9Decoder * const pbi)298 static INLINE void check_resync(vpx_codec_alg_priv_t *const ctx,
299 const VP9Decoder *const pbi) {
300 // Clear resync flag if the decoder got a key frame or intra only frame.
301 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
302 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
303 ctx->need_resync = 0;
304 }
305
decode_one(vpx_codec_alg_priv_t * ctx,const uint8_t ** data,unsigned int data_sz,void * user_priv,int64_t deadline)306 static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
307 const uint8_t **data, unsigned int data_sz,
308 void *user_priv, int64_t deadline) {
309 (void)deadline;
310
311 // Determine the stream parameters. Note that we rely on peek_si to
312 // validate that we have a buffer that does not wrap around the top
313 // of the heap.
314 if (!ctx->si.h) {
315 int is_intra_only = 0;
316 const vpx_codec_err_t res =
317 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
318 ctx->decrypt_cb, ctx->decrypt_state);
319 if (res != VPX_CODEC_OK) return res;
320
321 if (!ctx->si.is_kf && !is_intra_only) return VPX_CODEC_ERROR;
322 }
323
324 ctx->user_priv = user_priv;
325
326 // Set these even if already initialized. The caller may have changed the
327 // decrypt config between frames.
328 ctx->pbi->decrypt_cb = ctx->decrypt_cb;
329 ctx->pbi->decrypt_state = ctx->decrypt_state;
330
331 if (vp9_receive_compressed_data(ctx->pbi, data_sz, data)) {
332 ctx->pbi->cur_buf->buf.corrupted = 1;
333 ctx->pbi->need_resync = 1;
334 ctx->need_resync = 1;
335 return update_error_state(ctx, &ctx->pbi->common.error);
336 }
337
338 check_resync(ctx, ctx->pbi);
339
340 return VPX_CODEC_OK;
341 }
342
decoder_decode(vpx_codec_alg_priv_t * ctx,const uint8_t * data,unsigned int data_sz,void * user_priv,long deadline)343 static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
344 const uint8_t *data, unsigned int data_sz,
345 void *user_priv, long deadline) {
346 const uint8_t *data_start = data;
347 vpx_codec_err_t res;
348 uint32_t frame_sizes[8];
349 int frame_count;
350
351 if (data == NULL && data_sz == 0) {
352 ctx->flushed = 1;
353 return VPX_CODEC_OK;
354 }
355
356 // Reset flushed when receiving a valid frame.
357 ctx->flushed = 0;
358
359 // Initialize the decoder on the first frame.
360 if (ctx->pbi == NULL) {
361 res = init_decoder(ctx);
362 if (res != VPX_CODEC_OK) return res;
363 }
364
365 res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
366 ctx->decrypt_cb, ctx->decrypt_state);
367 if (res != VPX_CODEC_OK) return res;
368
369 if (ctx->svc_decoding && ctx->svc_spatial_layer < frame_count - 1)
370 frame_count = ctx->svc_spatial_layer + 1;
371
372 // Decode in serial mode.
373 if (frame_count > 0) {
374 const uint8_t *const data_end = data + data_sz;
375 int i;
376
377 for (i = 0; i < frame_count; ++i) {
378 const uint8_t *data_start_copy = data_start;
379 const uint32_t frame_size = frame_sizes[i];
380 if (data_start < data || frame_size > (uint32_t)(data_end - data_start)) {
381 set_error_detail(ctx, "Invalid frame size in index");
382 return VPX_CODEC_CORRUPT_FRAME;
383 }
384
385 res = decode_one(ctx, &data_start_copy, frame_size, user_priv, deadline);
386 if (res != VPX_CODEC_OK) return res;
387
388 data_start += frame_size;
389 }
390 } else {
391 const uint8_t *const data_end = data + data_sz;
392 while (data_start < data_end) {
393 const uint32_t frame_size = (uint32_t)(data_end - data_start);
394 res = decode_one(ctx, &data_start, frame_size, user_priv, deadline);
395 if (res != VPX_CODEC_OK) return res;
396
397 // Account for suboptimal termination by the encoder.
398 while (data_start < data_end) {
399 const uint8_t marker =
400 read_marker(ctx->decrypt_cb, ctx->decrypt_state, data_start);
401 if (marker) break;
402 ++data_start;
403 }
404 }
405 }
406
407 return res;
408 }
409
decoder_get_frame(vpx_codec_alg_priv_t * ctx,vpx_codec_iter_t * iter)410 static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
411 vpx_codec_iter_t *iter) {
412 vpx_image_t *img = NULL;
413
414 // Legacy parameter carried over from VP8. Has no effect for VP9 since we
415 // always return only 1 frame per decode call.
416 (void)iter;
417
418 if (ctx->pbi != NULL) {
419 YV12_BUFFER_CONFIG sd;
420 vp9_ppflags_t flags = { 0, 0, 0 };
421 if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC) set_ppflags(ctx, &flags);
422 if (vp9_get_raw_frame(ctx->pbi, &sd, &flags) == 0) {
423 VP9_COMMON *const cm = &ctx->pbi->common;
424 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
425 ctx->last_show_frame = ctx->pbi->common.new_fb_idx;
426 if (ctx->need_resync) return NULL;
427 yuvconfig2image(&ctx->img, &sd, ctx->user_priv);
428 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
429 img = &ctx->img;
430 return img;
431 }
432 }
433 return NULL;
434 }
435
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)436 static vpx_codec_err_t decoder_set_fb_fn(
437 vpx_codec_alg_priv_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get,
438 vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
439 if (cb_get == NULL || cb_release == NULL) {
440 return VPX_CODEC_INVALID_PARAM;
441 } else if (ctx->pbi == NULL) {
442 // If the decoder has already been initialized, do not accept changes to
443 // the frame buffer functions.
444 ctx->get_ext_fb_cb = cb_get;
445 ctx->release_ext_fb_cb = cb_release;
446 ctx->ext_priv = cb_priv;
447 return VPX_CODEC_OK;
448 }
449
450 return VPX_CODEC_ERROR;
451 }
452
ctrl_set_reference(vpx_codec_alg_priv_t * ctx,va_list args)453 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
454 va_list args) {
455 vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
456
457 if (data) {
458 vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
459 YV12_BUFFER_CONFIG sd;
460 image2yuvconfig(&frame->img, &sd);
461 return vp9_set_reference_dec(
462 &ctx->pbi->common, ref_frame_to_vp9_reframe(frame->frame_type), &sd);
463 } else {
464 return VPX_CODEC_INVALID_PARAM;
465 }
466 }
467
ctrl_copy_reference(vpx_codec_alg_priv_t * ctx,va_list args)468 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
469 va_list args) {
470 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
471
472 if (data) {
473 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
474 YV12_BUFFER_CONFIG sd;
475 image2yuvconfig(&frame->img, &sd);
476 return vp9_copy_reference_dec(ctx->pbi, (VP9_REFFRAME)frame->frame_type,
477 &sd);
478 } else {
479 return VPX_CODEC_INVALID_PARAM;
480 }
481 }
482
ctrl_get_reference(vpx_codec_alg_priv_t * ctx,va_list args)483 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
484 va_list args) {
485 vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
486
487 if (data) {
488 if (ctx->pbi) {
489 const int fb_idx = ctx->pbi->common.cur_show_frame_fb_idx;
490 YV12_BUFFER_CONFIG *fb = get_buf_frame(&ctx->pbi->common, fb_idx);
491 if (fb == NULL) return VPX_CODEC_ERROR;
492 yuvconfig2image(&data->img, fb, NULL);
493 return VPX_CODEC_OK;
494 } else {
495 return VPX_CODEC_ERROR;
496 }
497 } else {
498 return VPX_CODEC_INVALID_PARAM;
499 }
500 }
501
ctrl_set_postproc(vpx_codec_alg_priv_t * ctx,va_list args)502 static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
503 va_list args) {
504 #if CONFIG_VP9_POSTPROC
505 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
506
507 if (data) {
508 ctx->postproc_cfg_set = 1;
509 ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
510 return VPX_CODEC_OK;
511 } else {
512 return VPX_CODEC_INVALID_PARAM;
513 }
514 #else
515 (void)ctx;
516 (void)args;
517 return VPX_CODEC_INCAPABLE;
518 #endif
519 }
520
ctrl_get_quantizer(vpx_codec_alg_priv_t * ctx,va_list args)521 static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
522 va_list args) {
523 int *const arg = va_arg(args, int *);
524 if (arg == NULL || ctx->pbi == NULL) return VPX_CODEC_INVALID_PARAM;
525 *arg = ctx->pbi->common.base_qindex;
526 return VPX_CODEC_OK;
527 }
528
ctrl_get_last_ref_updates(vpx_codec_alg_priv_t * ctx,va_list args)529 static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
530 va_list args) {
531 int *const update_info = va_arg(args, int *);
532
533 if (update_info) {
534 if (ctx->pbi != NULL) {
535 *update_info = ctx->pbi->refresh_frame_flags;
536 return VPX_CODEC_OK;
537 } else {
538 return VPX_CODEC_ERROR;
539 }
540 }
541
542 return VPX_CODEC_INVALID_PARAM;
543 }
544
ctrl_get_frame_corrupted(vpx_codec_alg_priv_t * ctx,va_list args)545 static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
546 va_list args) {
547 int *corrupted = va_arg(args, int *);
548
549 if (corrupted) {
550 if (ctx->pbi != NULL) {
551 RefCntBuffer *const frame_bufs = ctx->pbi->common.buffer_pool->frame_bufs;
552 if (ctx->pbi->common.frame_to_show == NULL) return VPX_CODEC_ERROR;
553 if (ctx->last_show_frame >= 0)
554 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
555 return VPX_CODEC_OK;
556 } else {
557 return VPX_CODEC_ERROR;
558 }
559 }
560
561 return VPX_CODEC_INVALID_PARAM;
562 }
563
ctrl_get_frame_size(vpx_codec_alg_priv_t * ctx,va_list args)564 static vpx_codec_err_t ctrl_get_frame_size(vpx_codec_alg_priv_t *ctx,
565 va_list args) {
566 int *const frame_size = va_arg(args, int *);
567
568 if (frame_size) {
569 if (ctx->pbi != NULL) {
570 const VP9_COMMON *const cm = &ctx->pbi->common;
571 frame_size[0] = cm->width;
572 frame_size[1] = cm->height;
573 return VPX_CODEC_OK;
574 } else {
575 return VPX_CODEC_ERROR;
576 }
577 }
578
579 return VPX_CODEC_INVALID_PARAM;
580 }
581
ctrl_get_render_size(vpx_codec_alg_priv_t * ctx,va_list args)582 static vpx_codec_err_t ctrl_get_render_size(vpx_codec_alg_priv_t *ctx,
583 va_list args) {
584 int *const render_size = va_arg(args, int *);
585
586 if (render_size) {
587 if (ctx->pbi != NULL) {
588 const VP9_COMMON *const cm = &ctx->pbi->common;
589 render_size[0] = cm->render_width;
590 render_size[1] = cm->render_height;
591 return VPX_CODEC_OK;
592 } else {
593 return VPX_CODEC_ERROR;
594 }
595 }
596
597 return VPX_CODEC_INVALID_PARAM;
598 }
599
ctrl_get_bit_depth(vpx_codec_alg_priv_t * ctx,va_list args)600 static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
601 va_list args) {
602 unsigned int *const bit_depth = va_arg(args, unsigned int *);
603
604 if (bit_depth) {
605 if (ctx->pbi != NULL) {
606 const VP9_COMMON *const cm = &ctx->pbi->common;
607 *bit_depth = cm->bit_depth;
608 return VPX_CODEC_OK;
609 } else {
610 return VPX_CODEC_ERROR;
611 }
612 }
613
614 return VPX_CODEC_INVALID_PARAM;
615 }
616
ctrl_set_invert_tile_order(vpx_codec_alg_priv_t * ctx,va_list args)617 static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
618 va_list args) {
619 ctx->invert_tile_order = va_arg(args, int);
620 return VPX_CODEC_OK;
621 }
622
ctrl_set_decryptor(vpx_codec_alg_priv_t * ctx,va_list args)623 static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
624 va_list args) {
625 vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
626 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
627 ctx->decrypt_state = init ? init->decrypt_state : NULL;
628 return VPX_CODEC_OK;
629 }
630
ctrl_set_byte_alignment(vpx_codec_alg_priv_t * ctx,va_list args)631 static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
632 va_list args) {
633 const int legacy_byte_alignment = 0;
634 const int min_byte_alignment = 32;
635 const int max_byte_alignment = 1024;
636 const int byte_alignment = va_arg(args, int);
637
638 if (byte_alignment != legacy_byte_alignment &&
639 (byte_alignment < min_byte_alignment ||
640 byte_alignment > max_byte_alignment ||
641 (byte_alignment & (byte_alignment - 1)) != 0))
642 return VPX_CODEC_INVALID_PARAM;
643
644 ctx->byte_alignment = byte_alignment;
645 if (ctx->pbi != NULL) {
646 ctx->pbi->common.byte_alignment = byte_alignment;
647 }
648 return VPX_CODEC_OK;
649 }
650
ctrl_set_skip_loop_filter(vpx_codec_alg_priv_t * ctx,va_list args)651 static vpx_codec_err_t ctrl_set_skip_loop_filter(vpx_codec_alg_priv_t *ctx,
652 va_list args) {
653 ctx->skip_loop_filter = va_arg(args, int);
654
655 if (ctx->pbi != NULL) {
656 ctx->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
657 }
658
659 return VPX_CODEC_OK;
660 }
661
ctrl_set_spatial_layer_svc(vpx_codec_alg_priv_t * ctx,va_list args)662 static vpx_codec_err_t ctrl_set_spatial_layer_svc(vpx_codec_alg_priv_t *ctx,
663 va_list args) {
664 ctx->svc_decoding = 1;
665 ctx->svc_spatial_layer = va_arg(args, int);
666 if (ctx->svc_spatial_layer < 0)
667 return VPX_CODEC_INVALID_PARAM;
668 else
669 return VPX_CODEC_OK;
670 }
671
ctrl_set_row_mt(vpx_codec_alg_priv_t * ctx,va_list args)672 static vpx_codec_err_t ctrl_set_row_mt(vpx_codec_alg_priv_t *ctx,
673 va_list args) {
674 ctx->row_mt = va_arg(args, int);
675
676 return VPX_CODEC_OK;
677 }
678
ctrl_enable_lpf_opt(vpx_codec_alg_priv_t * ctx,va_list args)679 static vpx_codec_err_t ctrl_enable_lpf_opt(vpx_codec_alg_priv_t *ctx,
680 va_list args) {
681 ctx->lpf_opt = va_arg(args, int);
682
683 return VPX_CODEC_OK;
684 }
685
686 static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
687 { VP8_COPY_REFERENCE, ctrl_copy_reference },
688
689 // Setters
690 { VP8_SET_REFERENCE, ctrl_set_reference },
691 { VP8_SET_POSTPROC, ctrl_set_postproc },
692 { VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
693 { VPXD_SET_DECRYPTOR, ctrl_set_decryptor },
694 { VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
695 { VP9_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
696 { VP9_DECODE_SVC_SPATIAL_LAYER, ctrl_set_spatial_layer_svc },
697 { VP9D_SET_ROW_MT, ctrl_set_row_mt },
698 { VP9D_SET_LOOP_FILTER_OPT, ctrl_enable_lpf_opt },
699
700 // Getters
701 { VPXD_GET_LAST_QUANTIZER, ctrl_get_quantizer },
702 { VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
703 { VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
704 { VP9_GET_REFERENCE, ctrl_get_reference },
705 { VP9D_GET_DISPLAY_SIZE, ctrl_get_render_size },
706 { VP9D_GET_BIT_DEPTH, ctrl_get_bit_depth },
707 { VP9D_GET_FRAME_SIZE, ctrl_get_frame_size },
708
709 { -1, NULL },
710 };
711
712 #ifndef VERSION_STRING
713 #define VERSION_STRING
714 #endif
715 CODEC_INTERFACE(vpx_codec_vp9_dx) = {
716 "WebM Project VP9 Decoder" VERSION_STRING,
717 VPX_CODEC_INTERNAL_ABI_VERSION,
718 #if CONFIG_VP9_HIGHBITDEPTH
719 VPX_CODEC_CAP_HIGHBITDEPTH |
720 #endif
721 VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
722 VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // vpx_codec_caps_t
723 decoder_init, // vpx_codec_init_fn_t
724 decoder_destroy, // vpx_codec_destroy_fn_t
725 decoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
726 {
727 // NOLINT
728 decoder_peek_si, // vpx_codec_peek_si_fn_t
729 decoder_get_si, // vpx_codec_get_si_fn_t
730 decoder_decode, // vpx_codec_decode_fn_t
731 decoder_get_frame, // vpx_codec_frame_get_fn_t
732 decoder_set_fb_fn, // vpx_codec_set_fb_fn_t
733 },
734 {
735 // NOLINT
736 0,
737 NULL, // vpx_codec_enc_cfg_map_t
738 NULL, // vpx_codec_encode_fn_t
739 NULL, // vpx_codec_get_cx_data_fn_t
740 NULL, // vpx_codec_enc_config_set_fn_t
741 NULL, // vpx_codec_get_global_headers_fn_t
742 NULL, // vpx_codec_get_preview_frame_fn_t
743 NULL // vpx_codec_enc_mr_get_mem_loc_fn_t
744 }
745 };
746