]> granicus.if.org Git - libvpx/blob - vp9/vp9_dx_iface.c
Merge "Bug fix: fadst4() in vp9/encoder/vp9_dct.c"
[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   // iter acts as a flip flop, so an image is only returned on the first
382   // call to get_frame.
383   if (*iter == NULL && ctx->pbi != NULL) {
384     YV12_BUFFER_CONFIG sd;
385     vp9_ppflags_t flags = { 0, 0, 0 };
386     if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC) set_ppflags(ctx, &flags);
387     if (vp9_get_raw_frame(ctx->pbi, &sd, &flags) == 0) {
388       VP9_COMMON *const cm = &ctx->pbi->common;
389       RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
390       ctx->last_show_frame = ctx->pbi->common.new_fb_idx;
391       if (ctx->need_resync) return NULL;
392       yuvconfig2image(&ctx->img, &sd, ctx->user_priv);
393       ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
394       img = &ctx->img;
395       return img;
396     }
397   }
398   return NULL;
399 }
400
401 static vpx_codec_err_t decoder_set_fb_fn(
402     vpx_codec_alg_priv_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get,
403     vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
404   if (cb_get == NULL || cb_release == NULL) {
405     return VPX_CODEC_INVALID_PARAM;
406   } else if (ctx->pbi == NULL) {
407     // If the decoder has already been initialized, do not accept changes to
408     // the frame buffer functions.
409     ctx->get_ext_fb_cb = cb_get;
410     ctx->release_ext_fb_cb = cb_release;
411     ctx->ext_priv = cb_priv;
412     return VPX_CODEC_OK;
413   }
414
415   return VPX_CODEC_ERROR;
416 }
417
418 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
419                                           va_list args) {
420   vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
421
422   if (data) {
423     vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
424     YV12_BUFFER_CONFIG sd;
425     image2yuvconfig(&frame->img, &sd);
426     return vp9_set_reference_dec(
427         &ctx->pbi->common, ref_frame_to_vp9_reframe(frame->frame_type), &sd);
428   } else {
429     return VPX_CODEC_INVALID_PARAM;
430   }
431 }
432
433 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
434                                            va_list args) {
435   vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
436
437   if (data) {
438     vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
439     YV12_BUFFER_CONFIG sd;
440     image2yuvconfig(&frame->img, &sd);
441     return vp9_copy_reference_dec(ctx->pbi, (VP9_REFFRAME)frame->frame_type,
442                                   &sd);
443   } else {
444     return VPX_CODEC_INVALID_PARAM;
445   }
446 }
447
448 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
449                                           va_list args) {
450   vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
451
452   if (data) {
453     YV12_BUFFER_CONFIG *fb;
454     fb = get_ref_frame(&ctx->pbi->common, data->idx);
455     if (fb == NULL) return VPX_CODEC_ERROR;
456     yuvconfig2image(&data->img, fb, NULL);
457     return VPX_CODEC_OK;
458   } else {
459     return VPX_CODEC_INVALID_PARAM;
460   }
461 }
462
463 static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
464                                          va_list args) {
465 #if CONFIG_VP9_POSTPROC
466   vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
467
468   if (data) {
469     ctx->postproc_cfg_set = 1;
470     ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
471     return VPX_CODEC_OK;
472   } else {
473     return VPX_CODEC_INVALID_PARAM;
474   }
475 #else
476   (void)ctx;
477   (void)args;
478   return VPX_CODEC_INCAPABLE;
479 #endif
480 }
481
482 static vpx_codec_err_t ctrl_get_quantizer(vpx_codec_alg_priv_t *ctx,
483                                           va_list args) {
484   int *const arg = va_arg(args, int *);
485   if (arg == NULL || ctx->pbi == NULL) return VPX_CODEC_INVALID_PARAM;
486   *arg = ctx->pbi->common.base_qindex;
487   return VPX_CODEC_OK;
488 }
489
490 static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
491                                                  va_list args) {
492   int *const update_info = va_arg(args, int *);
493
494   if (update_info) {
495     if (ctx->pbi != NULL) {
496       *update_info = ctx->pbi->refresh_frame_flags;
497       return VPX_CODEC_OK;
498     } else {
499       return VPX_CODEC_ERROR;
500     }
501   }
502
503   return VPX_CODEC_INVALID_PARAM;
504 }
505
506 static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
507                                                 va_list args) {
508   int *corrupted = va_arg(args, int *);
509
510   if (corrupted) {
511     if (ctx->pbi != NULL) {
512       RefCntBuffer *const frame_bufs = ctx->pbi->common.buffer_pool->frame_bufs;
513       if (ctx->pbi->common.frame_to_show == NULL) return VPX_CODEC_ERROR;
514       if (ctx->last_show_frame >= 0)
515         *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
516       return VPX_CODEC_OK;
517     } else {
518       return VPX_CODEC_ERROR;
519     }
520   }
521
522   return VPX_CODEC_INVALID_PARAM;
523 }
524
525 static vpx_codec_err_t ctrl_get_frame_size(vpx_codec_alg_priv_t *ctx,
526                                            va_list args) {
527   int *const frame_size = va_arg(args, int *);
528
529   if (frame_size) {
530     if (ctx->pbi != NULL) {
531       const VP9_COMMON *const cm = &ctx->pbi->common;
532       frame_size[0] = cm->width;
533       frame_size[1] = cm->height;
534       return VPX_CODEC_OK;
535     } else {
536       return VPX_CODEC_ERROR;
537     }
538   }
539
540   return VPX_CODEC_INVALID_PARAM;
541 }
542
543 static vpx_codec_err_t ctrl_get_render_size(vpx_codec_alg_priv_t *ctx,
544                                             va_list args) {
545   int *const render_size = va_arg(args, int *);
546
547   if (render_size) {
548     if (ctx->pbi != NULL) {
549       const VP9_COMMON *const cm = &ctx->pbi->common;
550       render_size[0] = cm->render_width;
551       render_size[1] = cm->render_height;
552       return VPX_CODEC_OK;
553     } else {
554       return VPX_CODEC_ERROR;
555     }
556   }
557
558   return VPX_CODEC_INVALID_PARAM;
559 }
560
561 static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
562                                           va_list args) {
563   unsigned int *const bit_depth = va_arg(args, unsigned int *);
564
565   if (bit_depth) {
566     if (ctx->pbi != NULL) {
567       const VP9_COMMON *const cm = &ctx->pbi->common;
568       *bit_depth = cm->bit_depth;
569       return VPX_CODEC_OK;
570     } else {
571       return VPX_CODEC_ERROR;
572     }
573   }
574
575   return VPX_CODEC_INVALID_PARAM;
576 }
577
578 static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
579                                                   va_list args) {
580   ctx->invert_tile_order = va_arg(args, int);
581   return VPX_CODEC_OK;
582 }
583
584 static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
585                                           va_list args) {
586   vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
587   ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
588   ctx->decrypt_state = init ? init->decrypt_state : NULL;
589   return VPX_CODEC_OK;
590 }
591
592 static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
593                                                va_list args) {
594   const int legacy_byte_alignment = 0;
595   const int min_byte_alignment = 32;
596   const int max_byte_alignment = 1024;
597   const int byte_alignment = va_arg(args, int);
598
599   if (byte_alignment != legacy_byte_alignment &&
600       (byte_alignment < min_byte_alignment ||
601        byte_alignment > max_byte_alignment ||
602        (byte_alignment & (byte_alignment - 1)) != 0))
603     return VPX_CODEC_INVALID_PARAM;
604
605   ctx->byte_alignment = byte_alignment;
606   if (ctx->pbi != NULL) {
607     ctx->pbi->common.byte_alignment = byte_alignment;
608   }
609   return VPX_CODEC_OK;
610 }
611
612 static vpx_codec_err_t ctrl_set_skip_loop_filter(vpx_codec_alg_priv_t *ctx,
613                                                  va_list args) {
614   ctx->skip_loop_filter = va_arg(args, int);
615
616   if (ctx->pbi != NULL) {
617     ctx->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
618   }
619
620   return VPX_CODEC_OK;
621 }
622
623 static vpx_codec_err_t ctrl_set_spatial_layer_svc(vpx_codec_alg_priv_t *ctx,
624                                                   va_list args) {
625   ctx->svc_decoding = 1;
626   ctx->svc_spatial_layer = va_arg(args, int);
627   if (ctx->svc_spatial_layer < 0)
628     return VPX_CODEC_INVALID_PARAM;
629   else
630     return VPX_CODEC_OK;
631 }
632
633 static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
634   { VP8_COPY_REFERENCE, ctrl_copy_reference },
635
636   // Setters
637   { VP8_SET_REFERENCE, ctrl_set_reference },
638   { VP8_SET_POSTPROC, ctrl_set_postproc },
639   { VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order },
640   { VPXD_SET_DECRYPTOR, ctrl_set_decryptor },
641   { VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment },
642   { VP9_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter },
643   { VP9_DECODE_SVC_SPATIAL_LAYER, ctrl_set_spatial_layer_svc },
644
645   // Getters
646   { VPXD_GET_LAST_QUANTIZER, ctrl_get_quantizer },
647   { VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates },
648   { VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted },
649   { VP9_GET_REFERENCE, ctrl_get_reference },
650   { VP9D_GET_DISPLAY_SIZE, ctrl_get_render_size },
651   { VP9D_GET_BIT_DEPTH, ctrl_get_bit_depth },
652   { VP9D_GET_FRAME_SIZE, ctrl_get_frame_size },
653
654   { -1, NULL },
655 };
656
657 #ifndef VERSION_STRING
658 #define VERSION_STRING
659 #endif
660 CODEC_INTERFACE(vpx_codec_vp9_dx) = {
661   "WebM Project VP9 Decoder" VERSION_STRING,
662   VPX_CODEC_INTERNAL_ABI_VERSION,
663 #if CONFIG_VP9_HIGHBITDEPTH
664   VPX_CODEC_CAP_HIGHBITDEPTH |
665 #endif
666       VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
667       VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER,  // vpx_codec_caps_t
668   decoder_init,                             // vpx_codec_init_fn_t
669   decoder_destroy,                          // vpx_codec_destroy_fn_t
670   decoder_ctrl_maps,                        // vpx_codec_ctrl_fn_map_t
671   {
672       // NOLINT
673       decoder_peek_si,    // vpx_codec_peek_si_fn_t
674       decoder_get_si,     // vpx_codec_get_si_fn_t
675       decoder_decode,     // vpx_codec_decode_fn_t
676       decoder_get_frame,  // vpx_codec_frame_get_fn_t
677       decoder_set_fb_fn,  // vpx_codec_set_fb_fn_t
678   },
679   {
680       // NOLINT
681       0,
682       NULL,  // vpx_codec_enc_cfg_map_t
683       NULL,  // vpx_codec_encode_fn_t
684       NULL,  // vpx_codec_get_cx_data_fn_t
685       NULL,  // vpx_codec_enc_config_set_fn_t
686       NULL,  // vpx_codec_get_global_headers_fn_t
687       NULL,  // vpx_codec_get_preview_frame_fn_t
688       NULL   // vpx_codec_enc_mr_get_mem_loc_fn_t
689   }
690 };