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