Merge "Skip calling vp9_block_energy when aq-mode is off"
[platform/upstream/libvpx.git] / 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_version.h"
15
16 #include "vpx/internal/vpx_codec_internal.h"
17 #include "vpx/vp8dx.h"
18 #include "vpx/vpx_decoder.h"
19
20 #include "vp9/common/vp9_frame_buffers.h"
21
22 #include "vp9/decoder/vp9_decoder.h"
23 #include "vp9/decoder/vp9_decodeframe.h"
24 #include "vp9/decoder/vp9_read_bit_buffer.h"
25
26 #include "vp9/vp9_iface_common.h"
27
28 #define VP9_CAP_POSTPROC (CONFIG_VP9_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
29
30 typedef vpx_codec_stream_info_t vp9_stream_info_t;
31
32 struct vpx_codec_alg_priv {
33   vpx_codec_priv_t        base;
34   vpx_codec_dec_cfg_t     cfg;
35   vp9_stream_info_t       si;
36   struct VP9Decoder *pbi;
37   int                     postproc_cfg_set;
38   vp8_postproc_cfg_t      postproc_cfg;
39   vpx_decrypt_cb          decrypt_cb;
40   void                   *decrypt_state;
41   vpx_image_t             img;
42   int                     img_avail;
43   int                     invert_tile_order;
44   int                     frame_parallel_decode;  // frame-based threading.
45
46   // External frame buffer info to save for VP9 common.
47   void *ext_priv;  // Private data associated with the external frame buffers.
48   vpx_get_frame_buffer_cb_fn_t get_ext_fb_cb;
49   vpx_release_frame_buffer_cb_fn_t release_ext_fb_cb;
50 };
51
52 static vpx_codec_err_t decoder_init(vpx_codec_ctx_t *ctx,
53                                     vpx_codec_priv_enc_mr_cfg_t *data) {
54   // This function only allocates space for the vpx_codec_alg_priv_t
55   // structure. More memory may be required at the time the stream
56   // information becomes known.
57   (void)data;
58
59   if (!ctx->priv) {
60     vpx_codec_alg_priv_t *alg_priv = vpx_memalign(32, sizeof(*alg_priv));
61     if (alg_priv == NULL)
62       return VPX_CODEC_MEM_ERROR;
63
64     vp9_zero(*alg_priv);
65
66     ctx->priv = (vpx_codec_priv_t *)alg_priv;
67     ctx->priv->sz = sizeof(*ctx->priv);
68     ctx->priv->iface = ctx->iface;
69     ctx->priv->alg_priv = alg_priv;
70     ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si);
71     ctx->priv->init_flags = ctx->init_flags;
72     ctx->priv->alg_priv->frame_parallel_decode =
73         (ctx->init_flags & VPX_CODEC_USE_FRAME_THREADING);
74
75     // Disable frame parallel decoding for now.
76     ctx->priv->alg_priv->frame_parallel_decode = 0;
77
78     if (ctx->config.dec) {
79       // Update the reference to the config structure to an internal copy.
80       ctx->priv->alg_priv->cfg = *ctx->config.dec;
81       ctx->config.dec = &ctx->priv->alg_priv->cfg;
82     }
83   }
84
85   return VPX_CODEC_OK;
86 }
87
88 static vpx_codec_err_t decoder_destroy(vpx_codec_alg_priv_t *ctx) {
89   if (ctx->pbi) {
90     vp9_decoder_remove(ctx->pbi);
91     ctx->pbi = NULL;
92   }
93
94   vpx_free(ctx);
95
96   return VPX_CODEC_OK;
97 }
98
99 static vpx_codec_err_t decoder_peek_si_internal(const uint8_t *data,
100                                                 unsigned int data_sz,
101                                                 vpx_codec_stream_info_t *si,
102                                                 int *is_intra_only,
103                                                 vpx_decrypt_cb decrypt_cb,
104                                                 void *decrypt_state) {
105   int intra_only_flag = 0;
106   uint8_t clear_buffer[9];
107
108   if (data + data_sz <= data)
109     return VPX_CODEC_INVALID_PARAM;
110
111   si->is_kf = 0;
112   si->w = si->h = 0;
113
114   if (decrypt_cb) {
115     data_sz = MIN(sizeof(clear_buffer), data_sz);
116     decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
117     data = clear_buffer;
118   }
119
120   {
121     int show_frame;
122     int error_resilient;
123     struct vp9_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
124     const int frame_marker = vp9_rb_read_literal(&rb, 2);
125     const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
126
127     if (frame_marker != VP9_FRAME_MARKER)
128       return VPX_CODEC_UNSUP_BITSTREAM;
129
130     if (profile >= MAX_PROFILES) return VPX_CODEC_UNSUP_BITSTREAM;
131
132     if (vp9_rb_read_bit(&rb)) {  // show an existing frame
133       vp9_rb_read_literal(&rb, 3);  // Frame buffer to show.
134       return VPX_CODEC_OK;
135     }
136
137     if (data_sz <= 8)
138       return VPX_CODEC_UNSUP_BITSTREAM;
139
140     si->is_kf = !vp9_rb_read_bit(&rb);
141     show_frame = vp9_rb_read_bit(&rb);
142     error_resilient = vp9_rb_read_bit(&rb);
143
144     if (si->is_kf) {
145       const int sRGB = 7;
146       int colorspace;
147
148       if (!vp9_read_sync_code(&rb))
149         return VPX_CODEC_UNSUP_BITSTREAM;
150
151       if (profile > PROFILE_1)
152         rb.bit_offset += 1;  // Bit-depth 10 or 12
153       colorspace = vp9_rb_read_literal(&rb, 3);
154       if (colorspace != sRGB) {
155         rb.bit_offset += 1;  // [16,235] (including xvycc) vs [0,255] range
156         if (profile == PROFILE_1 || profile == PROFILE_3) {
157           rb.bit_offset += 2;  // subsampling x/y
158           rb.bit_offset += 1;  // unused
159         }
160       } else {
161         if (profile == PROFILE_1 || profile == PROFILE_3) {
162           rb.bit_offset += 1;  // unused
163         } else {
164           // RGB is only available in version 1
165           return VPX_CODEC_UNSUP_BITSTREAM;
166         }
167       }
168       vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
169     } else {
170       intra_only_flag = show_frame ? 0 : vp9_rb_read_bit(&rb);
171       rb.bit_offset += error_resilient ? 0 : 2;  // reset_frame_context
172
173       if (intra_only_flag) {
174         if (!vp9_read_sync_code(&rb))
175           return VPX_CODEC_UNSUP_BITSTREAM;
176         rb.bit_offset += REF_FRAMES;  // refresh_frame_flags
177         vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
178       }
179     }
180   }
181   if (is_intra_only != NULL)
182     *is_intra_only = intra_only_flag;
183   return VPX_CODEC_OK;
184 }
185
186 static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
187                                        unsigned int data_sz,
188                                        vpx_codec_stream_info_t *si) {
189   return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
190 }
191
192 static vpx_codec_err_t decoder_get_si(vpx_codec_alg_priv_t *ctx,
193                                       vpx_codec_stream_info_t *si) {
194   const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
195                        ? sizeof(vp9_stream_info_t)
196                        : sizeof(vpx_codec_stream_info_t);
197   memcpy(si, &ctx->si, sz);
198   si->sz = (unsigned int)sz;
199
200   return VPX_CODEC_OK;
201 }
202
203 static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
204                            const struct vpx_internal_error_info *error) {
205   if (error->error_code)
206     ctx->base.err_detail = error->has_detail ? error->detail : NULL;
207
208   return error->error_code;
209 }
210
211 static void init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
212   VP9_COMMON *const cm = &ctx->pbi->common;
213
214   cm->new_fb_idx = -1;
215
216   if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
217     cm->get_fb_cb = ctx->get_ext_fb_cb;
218     cm->release_fb_cb = ctx->release_ext_fb_cb;
219     cm->cb_priv = ctx->ext_priv;
220   } else {
221     cm->get_fb_cb = vp9_get_frame_buffer;
222     cm->release_fb_cb = vp9_release_frame_buffer;
223
224     if (vp9_alloc_internal_frame_buffers(&cm->int_frame_buffers))
225       vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
226                          "Failed to initialize internal frame buffers");
227
228     cm->cb_priv = &cm->int_frame_buffers;
229   }
230 }
231
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
238 static void set_ppflags(const vpx_codec_alg_priv_t *ctx,
239                         vp9_ppflags_t *flags) {
240   flags->post_proc_flag =
241       ctx->postproc_cfg.post_proc_flag;
242
243   flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
244   flags->noise_level = ctx->postproc_cfg.noise_level;
245 }
246
247 static void init_decoder(vpx_codec_alg_priv_t *ctx) {
248   ctx->pbi = vp9_decoder_create();
249   if (ctx->pbi == NULL)
250     return;
251
252   ctx->pbi->max_threads = ctx->cfg.threads;
253   ctx->pbi->inv_tile_order = ctx->invert_tile_order;
254   ctx->pbi->frame_parallel_decode = ctx->frame_parallel_decode;
255
256   // If postprocessing was enabled by the application and a
257   // configuration has not been provided, default it.
258   if (!ctx->postproc_cfg_set &&
259       (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
260     set_default_ppflags(&ctx->postproc_cfg);
261
262   init_buffer_callbacks(ctx);
263 }
264
265 static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
266                                   const uint8_t **data, unsigned int data_sz,
267                                   void *user_priv, int64_t deadline) {
268   YV12_BUFFER_CONFIG sd;
269   vp9_ppflags_t flags = {0, 0, 0};
270   VP9_COMMON *cm = NULL;
271
272   (void)deadline;
273
274   vp9_zero(sd);
275   ctx->img_avail = 0;
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)
286       return res;
287
288     if (!ctx->si.is_kf && !is_intra_only)
289       return VPX_CODEC_ERROR;
290   }
291
292   // Initialize the decoder instance on the first frame
293   if (ctx->pbi == NULL) {
294     init_decoder(ctx);
295     if (ctx->pbi == NULL)
296       return VPX_CODEC_ERROR;
297   }
298
299   // Set these even if already initialized.  The caller may have changed the
300   // decrypt config between frames.
301   ctx->pbi->decrypt_cb = ctx->decrypt_cb;
302   ctx->pbi->decrypt_state = ctx->decrypt_state;
303
304   cm = &ctx->pbi->common;
305
306   if (vp9_receive_compressed_data(ctx->pbi, data_sz, data))
307     return update_error_state(ctx, &cm->error);
308
309   if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
310     set_ppflags(ctx, &flags);
311
312   if (vp9_get_raw_frame(ctx->pbi, &sd, &flags))
313     return update_error_state(ctx, &cm->error);
314
315   yuvconfig2image(&ctx->img, &sd, user_priv);
316   ctx->img.fb_priv = cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
317   ctx->img_avail = 1;
318
319   return VPX_CODEC_OK;
320 }
321
322 static INLINE uint8_t read_marker(vpx_decrypt_cb decrypt_cb,
323                                   void *decrypt_state,
324                                   const uint8_t *data) {
325   if (decrypt_cb) {
326     uint8_t marker;
327     decrypt_cb(decrypt_state, data, &marker, 1);
328     return marker;
329   }
330   return *data;
331 }
332
333 static vpx_codec_err_t parse_superframe_index(const uint8_t *data,
334                                               size_t data_sz,
335                                               uint32_t sizes[8], int *count,
336                                               vpx_decrypt_cb decrypt_cb,
337                                               void *decrypt_state) {
338   // A chunk ending with a byte matching 0xc0 is an invalid chunk unless
339   // it is a super frame index. If the last byte of real video compression
340   // data is 0xc0 the encoder must add a 0 byte. If we have the marker but
341   // not the associated matching marker byte at the front of the index we have
342   // an invalid bitstream and need to return an error.
343
344   uint8_t marker;
345
346   assert(data_sz);
347   marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
348   *count = 0;
349
350   if ((marker & 0xe0) == 0xc0) {
351     const uint32_t frames = (marker & 0x7) + 1;
352     const uint32_t mag = ((marker >> 3) & 0x3) + 1;
353     const size_t index_sz = 2 + mag * frames;
354
355     // This chunk is marked as having a superframe index but doesn't have
356     // enough data for it, thus it's an invalid superframe index.
357     if (data_sz < index_sz)
358       return VPX_CODEC_CORRUPT_FRAME;
359
360     {
361       const uint8_t marker2 = read_marker(decrypt_cb, decrypt_state,
362                                           data + data_sz - index_sz);
363
364       // This chunk is marked as having a superframe index but doesn't have
365       // the matching marker byte at the front of the index therefore it's an
366       // invalid chunk.
367       if (marker != marker2)
368         return VPX_CODEC_CORRUPT_FRAME;
369     }
370
371     {
372       // Found a valid superframe index.
373       uint32_t i, j;
374       const uint8_t *x = &data[data_sz - index_sz + 1];
375
376       // Frames has a maximum of 8 and mag has a maximum of 4.
377       uint8_t clear_buffer[32];
378       assert(sizeof(clear_buffer) >= frames * mag);
379       if (decrypt_cb) {
380         decrypt_cb(decrypt_state, x, clear_buffer, frames * mag);
381         x = clear_buffer;
382       }
383
384       for (i = 0; i < frames; ++i) {
385         uint32_t this_sz = 0;
386
387         for (j = 0; j < mag; ++j)
388           this_sz |= (*x++) << (j * 8);
389         sizes[i] = this_sz;
390       }
391       *count = frames;
392     }
393   }
394   return VPX_CODEC_OK;
395 }
396
397 static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
398                                       const uint8_t *data, unsigned int data_sz,
399                                       void *user_priv, long deadline) {
400   const uint8_t *data_start = data;
401   const uint8_t * const data_end = data + data_sz;
402   vpx_codec_err_t res;
403   uint32_t frame_sizes[8];
404   int frame_count;
405
406   if (data == NULL || data_sz == 0)
407     return VPX_CODEC_INVALID_PARAM;
408
409   res = parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
410                                ctx->decrypt_cb, ctx->decrypt_state);
411   if (res != VPX_CODEC_OK)
412     return res;
413
414   if (ctx->frame_parallel_decode) {
415     // Decode in frame parallel mode. When decoding in this mode, the frame
416     // passed to the decoder must be either a normal frame or a superframe with
417     // superframe index so the decoder could get each frame's start position
418     // in the superframe.
419     if (frame_count > 0) {
420       int i;
421
422       for (i = 0; i < frame_count; ++i) {
423         const uint8_t *data_start_copy = data_start;
424         const uint32_t frame_size = frame_sizes[i];
425         vpx_codec_err_t res;
426         if (data_start < data
427             || frame_size > (uint32_t) (data_end - data_start)) {
428           ctx->base.err_detail = "Invalid frame size in index";
429           return VPX_CODEC_CORRUPT_FRAME;
430         }
431
432         res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
433                          deadline);
434         if (res != VPX_CODEC_OK)
435           return res;
436
437         data_start += frame_size;
438       }
439     } else {
440       res = decode_one(ctx, &data_start, data_sz, user_priv, deadline);
441       if (res != VPX_CODEC_OK)
442         return res;
443
444       // Extra data detected after the frame.
445       if (data_start < data_end - 1) {
446         ctx->base.err_detail = "Fail to decode frame in parallel mode";
447         return VPX_CODEC_INCAPABLE;
448       }
449     }
450   } else {
451     // Decode in serial mode.
452     if (frame_count > 0) {
453       int i;
454
455       for (i = 0; i < frame_count; ++i) {
456         const uint8_t *data_start_copy = data_start;
457         const uint32_t frame_size = frame_sizes[i];
458         vpx_codec_err_t res;
459         if (data_start < data
460             || frame_size > (uint32_t) (data_end - data_start)) {
461           ctx->base.err_detail = "Invalid frame size in index";
462           return VPX_CODEC_CORRUPT_FRAME;
463         }
464
465         res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
466                          deadline);
467         if (res != VPX_CODEC_OK)
468           return res;
469
470         data_start += frame_size;
471       }
472     } else {
473       while (data_start < data_end) {
474         const uint32_t frame_size = (uint32_t) (data_end - data_start);
475         const vpx_codec_err_t res = decode_one(ctx, &data_start, frame_size,
476                                                user_priv, deadline);
477         if (res != VPX_CODEC_OK)
478           return res;
479
480         // Account for suboptimal termination by the encoder.
481         while (data_start < data_end) {
482           const uint8_t marker = read_marker(ctx->decrypt_cb,
483                                              ctx->decrypt_state, data_start);
484           if (marker)
485             break;
486           ++data_start;
487         }
488       }
489     }
490   }
491
492   return VPX_CODEC_OK;
493 }
494
495 static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
496                                       vpx_codec_iter_t *iter) {
497   vpx_image_t *img = NULL;
498
499   if (ctx->img_avail) {
500     // iter acts as a flip flop, so an image is only returned on the first
501     // call to get_frame.
502     if (!(*iter)) {
503       img = &ctx->img;
504       *iter = img;
505     }
506   }
507   ctx->img_avail = 0;
508
509   return img;
510 }
511
512 static vpx_codec_err_t decoder_set_fb_fn(
513     vpx_codec_alg_priv_t *ctx,
514     vpx_get_frame_buffer_cb_fn_t cb_get,
515     vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
516   if (cb_get == NULL || cb_release == NULL) {
517     return VPX_CODEC_INVALID_PARAM;
518   } else if (ctx->pbi == NULL) {
519     // If the decoder has already been initialized, do not accept changes to
520     // the frame buffer functions.
521     ctx->get_ext_fb_cb = cb_get;
522     ctx->release_ext_fb_cb = cb_release;
523     ctx->ext_priv = cb_priv;
524     return VPX_CODEC_OK;
525   }
526
527   return VPX_CODEC_ERROR;
528 }
529
530 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
531                                           va_list args) {
532   vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
533
534   if (data) {
535     vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
536     YV12_BUFFER_CONFIG sd;
537
538     image2yuvconfig(&frame->img, &sd);
539     return vp9_set_reference_dec(&ctx->pbi->common,
540                                  (VP9_REFFRAME)frame->frame_type, &sd);
541   } else {
542     return VPX_CODEC_INVALID_PARAM;
543   }
544 }
545
546 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
547                                            va_list args) {
548   vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
549
550   if (data) {
551     vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
552     YV12_BUFFER_CONFIG sd;
553
554     image2yuvconfig(&frame->img, &sd);
555
556     return vp9_copy_reference_dec(ctx->pbi,
557                                   (VP9_REFFRAME)frame->frame_type, &sd);
558   } else {
559     return VPX_CODEC_INVALID_PARAM;
560   }
561 }
562
563 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
564                                           va_list args) {
565   vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
566
567   if (data) {
568     YV12_BUFFER_CONFIG* fb;
569
570     vp9_get_reference_dec(ctx->pbi, data->idx, &fb);
571     yuvconfig2image(&data->img, fb, NULL);
572     return VPX_CODEC_OK;
573   } else {
574     return VPX_CODEC_INVALID_PARAM;
575   }
576 }
577
578 static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
579                                          va_list args) {
580 #if CONFIG_VP9_POSTPROC
581   vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
582
583   if (data) {
584     ctx->postproc_cfg_set = 1;
585     ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
586     return VPX_CODEC_OK;
587   } else {
588     return VPX_CODEC_INVALID_PARAM;
589   }
590 #else
591   (void)ctx;
592   (void)args;
593   return VPX_CODEC_INCAPABLE;
594 #endif
595 }
596
597 static vpx_codec_err_t ctrl_set_dbg_options(vpx_codec_alg_priv_t *ctx,
598                                             va_list args) {
599   (void)ctx;
600   (void)args;
601   return VPX_CODEC_INCAPABLE;
602 }
603
604 static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
605                                                  va_list args) {
606   int *const update_info = va_arg(args, int *);
607
608   if (update_info) {
609     if (ctx->pbi)
610       *update_info = ctx->pbi->refresh_frame_flags;
611     else
612       return VPX_CODEC_ERROR;
613     return VPX_CODEC_OK;
614   } else {
615     return VPX_CODEC_INVALID_PARAM;
616   }
617 }
618
619
620 static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
621                                                 va_list args) {
622   int *corrupted = va_arg(args, int *);
623
624   if (corrupted) {
625     if (ctx->pbi)
626       *corrupted = ctx->pbi->common.frame_to_show->corrupted;
627     else
628       return VPX_CODEC_ERROR;
629     return VPX_CODEC_OK;
630   } else {
631     return VPX_CODEC_INVALID_PARAM;
632   }
633 }
634
635 static vpx_codec_err_t ctrl_get_display_size(vpx_codec_alg_priv_t *ctx,
636                                              va_list args) {
637   int *const display_size = va_arg(args, int *);
638
639   if (display_size) {
640     if (ctx->pbi) {
641       const VP9_COMMON *const cm = &ctx->pbi->common;
642       display_size[0] = cm->display_width;
643       display_size[1] = cm->display_height;
644     } else {
645       return VPX_CODEC_ERROR;
646     }
647     return VPX_CODEC_OK;
648   } else {
649     return VPX_CODEC_INVALID_PARAM;
650   }
651 }
652
653 static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
654                                                   va_list args) {
655   ctx->invert_tile_order = va_arg(args, int);
656   return VPX_CODEC_OK;
657 }
658
659 static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
660                                           va_list args) {
661   vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
662   ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
663   ctx->decrypt_state = init ? init->decrypt_state : NULL;
664   return VPX_CODEC_OK;
665 }
666
667 static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
668   {VP8_COPY_REFERENCE,            ctrl_copy_reference},
669
670   // Setters
671   {VP8_SET_REFERENCE,             ctrl_set_reference},
672   {VP8_SET_POSTPROC,              ctrl_set_postproc},
673   {VP8_SET_DBG_COLOR_REF_FRAME,   ctrl_set_dbg_options},
674   {VP8_SET_DBG_COLOR_MB_MODES,    ctrl_set_dbg_options},
675   {VP8_SET_DBG_COLOR_B_MODES,     ctrl_set_dbg_options},
676   {VP8_SET_DBG_DISPLAY_MV,        ctrl_set_dbg_options},
677   {VP9_INVERT_TILE_DECODE_ORDER,  ctrl_set_invert_tile_order},
678   {VPXD_SET_DECRYPTOR,            ctrl_set_decryptor},
679
680   // Getters
681   {VP8D_GET_LAST_REF_UPDATES,     ctrl_get_last_ref_updates},
682   {VP8D_GET_FRAME_CORRUPTED,      ctrl_get_frame_corrupted},
683   {VP9_GET_REFERENCE,             ctrl_get_reference},
684   {VP9D_GET_DISPLAY_SIZE,         ctrl_get_display_size},
685
686   { -1, NULL},
687 };
688
689 #ifndef VERSION_STRING
690 #define VERSION_STRING
691 #endif
692 CODEC_INTERFACE(vpx_codec_vp9_dx) = {
693   "WebM Project VP9 Decoder" VERSION_STRING,
694   VPX_CODEC_INTERNAL_ABI_VERSION,
695   VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
696       VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER,  // vpx_codec_caps_t
697   decoder_init,       // vpx_codec_init_fn_t
698   decoder_destroy,    // vpx_codec_destroy_fn_t
699   decoder_ctrl_maps,  // vpx_codec_ctrl_fn_map_t
700   NOT_IMPLEMENTED,    // vpx_codec_get_mmap_fn_t
701   NOT_IMPLEMENTED,    // vpx_codec_set_mmap_fn_t
702   { // NOLINT
703     decoder_peek_si,    // vpx_codec_peek_si_fn_t
704     decoder_get_si,     // vpx_codec_get_si_fn_t
705     decoder_decode,     // vpx_codec_decode_fn_t
706     decoder_get_frame,  // vpx_codec_frame_get_fn_t
707     decoder_set_fb_fn,  // vpx_codec_set_fb_fn_t
708   },
709   { // NOLINT
710     0,
711     NOT_IMPLEMENTED,  // vpx_codec_enc_cfg_map_t
712     NOT_IMPLEMENTED,  // vpx_codec_encode_fn_t
713     NOT_IMPLEMENTED,  // vpx_codec_get_cx_data_fn_t
714     NOT_IMPLEMENTED,  // vpx_codec_enc_config_set_fn_t
715     NOT_IMPLEMENTED,  // vpx_codec_get_global_headers_fn_t
716     NOT_IMPLEMENTED,  // vpx_codec_get_preview_frame_fn_t
717     NOT_IMPLEMENTED   // vpx_codec_enc_mr_get_mem_loc_fn_t
718   }
719 };