Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / 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
12 #include <stdlib.h>
13 #include <string.h>
14 #include "vpx/vpx_decoder.h"
15 #include "vpx/vp8dx.h"
16 #include "vpx/internal/vpx_codec_internal.h"
17 #include "./vpx_version.h"
18 #include "vp9/common/vp9_frame_buffers.h"
19 #include "vp9/decoder/vp9_onyxd.h"
20 #include "vp9/decoder/vp9_onyxd_int.h"
21 #include "vp9/decoder/vp9_read_bit_buffer.h"
22 #include "vp9/vp9_iface_common.h"
23
24 #define VP9_CAP_POSTPROC (CONFIG_VP9_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
25 typedef vpx_codec_stream_info_t  vp9_stream_info_t;
26
27 /* Structures for handling memory allocations */
28 typedef enum {
29   VP9_SEG_ALG_PRIV = 256,
30   VP9_SEG_MAX
31 } mem_seg_id_t;
32 #define NELEMENTS(x) ((int)(sizeof(x)/sizeof(x[0])))
33
34 static unsigned long priv_sz(const vpx_codec_dec_cfg_t *si,
35                              vpx_codec_flags_t flags);
36
37 static const mem_req_t vp9_mem_req_segs[] = {
38   {VP9_SEG_ALG_PRIV, 0, 8, VPX_CODEC_MEM_ZERO, priv_sz},
39   {VP9_SEG_MAX, 0, 0, 0, NULL}
40 };
41
42 struct vpx_codec_alg_priv {
43   vpx_codec_priv_t        base;
44   vpx_codec_mmap_t        mmaps[NELEMENTS(vp9_mem_req_segs) - 1];
45   vpx_codec_dec_cfg_t     cfg;
46   vp9_stream_info_t       si;
47   int                     defer_alloc;
48   int                     decoder_init;
49   VP9D_PTR                pbi;
50   int                     postproc_cfg_set;
51   vp8_postproc_cfg_t      postproc_cfg;
52 #if CONFIG_POSTPROC_VISUALIZER
53   unsigned int            dbg_postproc_flag;
54   int                     dbg_color_ref_frame_flag;
55   int                     dbg_color_mb_modes_flag;
56   int                     dbg_color_b_modes_flag;
57   int                     dbg_display_mv_flag;
58 #endif
59   vpx_image_t             img;
60   int                     img_setup;
61   int                     img_avail;
62   int                     invert_tile_order;
63
64   // External frame buffer info to save for VP9 common.
65   void *ext_priv;  // Private data associated with the external frame buffers.
66   vpx_get_frame_buffer_cb_fn_t get_ext_fb_cb;
67   vpx_release_frame_buffer_cb_fn_t release_ext_fb_cb;
68 };
69
70 static unsigned long priv_sz(const vpx_codec_dec_cfg_t *si,
71                              vpx_codec_flags_t flags) {
72   /* Although this declaration is constant, we can't use it in the requested
73    * segments list because we want to define the requested segments list
74    * before defining the private type (so that the number of memory maps is
75    * known)
76    */
77   (void)si;
78   return sizeof(vpx_codec_alg_priv_t);
79 }
80
81 static void vp9_init_ctx(vpx_codec_ctx_t *ctx, const vpx_codec_mmap_t *mmap) {
82   int i;
83
84   ctx->priv = mmap->base;
85   ctx->priv->sz = sizeof(*ctx->priv);
86   ctx->priv->iface = ctx->iface;
87   ctx->priv->alg_priv = mmap->base;
88
89   for (i = 0; i < NELEMENTS(ctx->priv->alg_priv->mmaps); i++)
90     ctx->priv->alg_priv->mmaps[i].id = vp9_mem_req_segs[i].id;
91
92   ctx->priv->alg_priv->mmaps[0] = *mmap;
93   ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si);
94   ctx->priv->init_flags = ctx->init_flags;
95
96   if (ctx->config.dec) {
97     /* Update the reference to the config structure to an internal copy. */
98     ctx->priv->alg_priv->cfg = *ctx->config.dec;
99     ctx->config.dec = &ctx->priv->alg_priv->cfg;
100   }
101 }
102
103 static void vp9_finalize_mmaps(vpx_codec_alg_priv_t *ctx) {
104   /* nothing to clean up */
105 }
106
107 static vpx_codec_err_t vp9_init(vpx_codec_ctx_t *ctx,
108                                 vpx_codec_priv_enc_mr_cfg_t *data) {
109   vpx_codec_err_t res = VPX_CODEC_OK;
110
111   // This function only allocates space for the vpx_codec_alg_priv_t
112   // structure. More memory may be required at the time the stream
113   // information becomes known.
114   if (!ctx->priv) {
115     vpx_codec_mmap_t mmap;
116
117     mmap.id = vp9_mem_req_segs[0].id;
118     mmap.sz = sizeof(vpx_codec_alg_priv_t);
119     mmap.align = vp9_mem_req_segs[0].align;
120     mmap.flags = vp9_mem_req_segs[0].flags;
121
122     res = vpx_mmap_alloc(&mmap);
123     if (!res) {
124       vp9_init_ctx(ctx, &mmap);
125
126       ctx->priv->alg_priv->defer_alloc = 1;
127     }
128   }
129
130   return res;
131 }
132
133 static vpx_codec_err_t vp9_destroy(vpx_codec_alg_priv_t *ctx) {
134   int i;
135
136   vp9_remove_decompressor(ctx->pbi);
137
138   for (i = NELEMENTS(ctx->mmaps) - 1; i >= 0; i--) {
139     if (ctx->mmaps[i].dtor)
140       ctx->mmaps[i].dtor(&ctx->mmaps[i]);
141   }
142
143   return VPX_CODEC_OK;
144 }
145
146 static vpx_codec_err_t vp9_peek_si(const uint8_t *data, unsigned int data_sz,
147                                    vpx_codec_stream_info_t *si) {
148   if (data_sz <= 8) return VPX_CODEC_UNSUP_BITSTREAM;
149   if (data + data_sz <= data) return VPX_CODEC_INVALID_PARAM;
150
151   si->is_kf = 0;
152   si->w = si->h = 0;
153
154   {
155     struct vp9_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
156     const int frame_marker = vp9_rb_read_literal(&rb, 2);
157     const int version = vp9_rb_read_bit(&rb);
158     (void) vp9_rb_read_bit(&rb);  // unused version bit
159
160     if (frame_marker != VP9_FRAME_MARKER)
161       return VPX_CODEC_UNSUP_BITSTREAM;
162 #if CONFIG_NON420
163     if (version > 1) return VPX_CODEC_UNSUP_BITSTREAM;
164 #else
165     if (version != 0) return VPX_CODEC_UNSUP_BITSTREAM;
166 #endif
167
168     if (vp9_rb_read_bit(&rb)) {  // show an existing frame
169       return VPX_CODEC_OK;
170     }
171
172     si->is_kf = !vp9_rb_read_bit(&rb);
173     if (si->is_kf) {
174       const int sRGB = 7;
175       int colorspace;
176
177       rb.bit_offset += 1;  // show frame
178       rb.bit_offset += 1;  // error resilient
179
180       if (vp9_rb_read_literal(&rb, 8) != VP9_SYNC_CODE_0 ||
181           vp9_rb_read_literal(&rb, 8) != VP9_SYNC_CODE_1 ||
182           vp9_rb_read_literal(&rb, 8) != VP9_SYNC_CODE_2) {
183         return VPX_CODEC_UNSUP_BITSTREAM;
184       }
185
186       colorspace = vp9_rb_read_literal(&rb, 3);
187       if (colorspace != sRGB) {
188         rb.bit_offset += 1;  // [16,235] (including xvycc) vs [0,255] range
189         if (version == 1) {
190           rb.bit_offset += 2;  // subsampling x/y
191           rb.bit_offset += 1;  // has extra plane
192         }
193       } else {
194         if (version == 1) {
195           rb.bit_offset += 1;  // has extra plane
196         } else {
197           // RGB is only available in version 1
198           return VPX_CODEC_UNSUP_BITSTREAM;
199         }
200       }
201
202       // TODO(jzern): these are available on non-keyframes in intra only mode.
203       si->w = vp9_rb_read_literal(&rb, 16) + 1;
204       si->h = vp9_rb_read_literal(&rb, 16) + 1;
205     }
206   }
207
208   return VPX_CODEC_OK;
209 }
210
211 static vpx_codec_err_t vp9_get_si(vpx_codec_alg_priv_t    *ctx,
212                                   vpx_codec_stream_info_t *si) {
213   const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
214                        ? sizeof(vp9_stream_info_t)
215                        : sizeof(vpx_codec_stream_info_t);
216   memcpy(si, &ctx->si, sz);
217   si->sz = (unsigned int)sz;
218
219   return VPX_CODEC_OK;
220 }
221
222
223 static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
224                            const struct vpx_internal_error_info *error) {
225   if (error->error_code)
226     ctx->base.err_detail = error->has_detail ? error->detail : NULL;
227
228   return error->error_code;
229 }
230
231 static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
232                                   const uint8_t **data, unsigned int data_sz,
233                                   void *user_priv, int64_t deadline) {
234   vpx_codec_err_t res = VPX_CODEC_OK;
235
236   ctx->img_avail = 0;
237
238   /* Determine the stream parameters. Note that we rely on peek_si to
239    * validate that we have a buffer that does not wrap around the top
240    * of the heap.
241    */
242   if (!ctx->si.h)
243     res = ctx->base.iface->dec.peek_si(*data, data_sz, &ctx->si);
244
245
246   /* Perform deferred allocations, if required */
247   if (!res && ctx->defer_alloc) {
248     int i;
249
250     for (i = 1; !res && i < NELEMENTS(ctx->mmaps); i++) {
251       vpx_codec_dec_cfg_t cfg;
252
253       cfg.w = ctx->si.w;
254       cfg.h = ctx->si.h;
255       ctx->mmaps[i].id = vp9_mem_req_segs[i].id;
256       ctx->mmaps[i].sz = vp9_mem_req_segs[i].sz;
257       ctx->mmaps[i].align = vp9_mem_req_segs[i].align;
258       ctx->mmaps[i].flags = vp9_mem_req_segs[i].flags;
259
260       if (!ctx->mmaps[i].sz)
261         ctx->mmaps[i].sz = vp9_mem_req_segs[i].calc_sz(&cfg,
262                                                        ctx->base.init_flags);
263
264       res = vpx_mmap_alloc(&ctx->mmaps[i]);
265     }
266
267     if (!res)
268       vp9_finalize_mmaps(ctx);
269
270     ctx->defer_alloc = 0;
271   }
272
273   /* Initialize the decoder instance on the first frame*/
274   if (!res && !ctx->decoder_init) {
275     res = vpx_validate_mmaps(&ctx->si, ctx->mmaps,
276                              vp9_mem_req_segs, NELEMENTS(vp9_mem_req_segs),
277                              ctx->base.init_flags);
278
279     if (!res) {
280       VP9D_CONFIG oxcf;
281       VP9D_PTR optr;
282
283       vp9_initialize_dec();
284
285       oxcf.width = ctx->si.w;
286       oxcf.height = ctx->si.h;
287       oxcf.version = 9;
288       oxcf.postprocess = 0;
289       oxcf.max_threads = ctx->cfg.threads;
290       oxcf.inv_tile_order = ctx->invert_tile_order;
291       optr = vp9_create_decompressor(&oxcf);
292
293       // If postprocessing was enabled by the application and a
294       // configuration has not been provided, default it.
295       if (!ctx->postproc_cfg_set &&
296           (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)) {
297         ctx->postproc_cfg.post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
298         ctx->postproc_cfg.deblocking_level = 4;
299         ctx->postproc_cfg.noise_level = 0;
300       }
301
302       if (!optr) {
303         res = VPX_CODEC_ERROR;
304       } else {
305         VP9D_COMP *const pbi = (VP9D_COMP*)optr;
306         VP9_COMMON *const cm = &pbi->common;
307
308         // Set index to not initialized.
309         cm->new_fb_idx = -1;
310
311         if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
312           cm->get_fb_cb = ctx->get_ext_fb_cb;
313           cm->release_fb_cb = ctx->release_ext_fb_cb;
314           cm->cb_priv = ctx->ext_priv;
315         } else {
316           cm->get_fb_cb = vp9_get_frame_buffer;
317           cm->release_fb_cb = vp9_release_frame_buffer;
318
319           if (vp9_alloc_internal_frame_buffers(&cm->int_frame_buffers))
320             vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
321                                "Failed to initialize internal frame buffers");
322           cm->cb_priv = &cm->int_frame_buffers;
323         }
324
325         ctx->pbi = optr;
326       }
327     }
328
329     ctx->decoder_init = 1;
330   }
331
332   if (!res && ctx->pbi) {
333     YV12_BUFFER_CONFIG sd;
334     int64_t time_stamp = 0, time_end_stamp = 0;
335     vp9_ppflags_t flags = {0};
336
337     if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC) {
338       flags.post_proc_flag =
339 #if CONFIG_POSTPROC_VISUALIZER
340           (ctx->dbg_color_ref_frame_flag ? VP9D_DEBUG_CLR_FRM_REF_BLKS : 0) |
341           (ctx->dbg_color_mb_modes_flag ? VP9D_DEBUG_CLR_BLK_MODES : 0) |
342           (ctx->dbg_color_b_modes_flag ? VP9D_DEBUG_CLR_BLK_MODES : 0) |
343           (ctx->dbg_display_mv_flag ? VP9D_DEBUG_DRAW_MV : 0) |
344 #endif
345           ctx->postproc_cfg.post_proc_flag;
346
347       flags.deblocking_level = ctx->postproc_cfg.deblocking_level;
348       flags.noise_level = ctx->postproc_cfg.noise_level;
349 #if CONFIG_POSTPROC_VISUALIZER
350       flags.display_ref_frame_flag = ctx->dbg_color_ref_frame_flag;
351       flags.display_mb_modes_flag = ctx->dbg_color_mb_modes_flag;
352       flags.display_b_modes_flag = ctx->dbg_color_b_modes_flag;
353       flags.display_mv_flag = ctx->dbg_display_mv_flag;
354 #endif
355     }
356
357     if (vp9_receive_compressed_data(ctx->pbi, data_sz, data, deadline)) {
358       VP9D_COMP *pbi = (VP9D_COMP*)ctx->pbi;
359       res = update_error_state(ctx, &pbi->common.error);
360     }
361
362     if (!res && 0 == vp9_get_raw_frame(ctx->pbi, &sd, &time_stamp,
363                                        &time_end_stamp, &flags)) {
364       VP9D_COMP *const pbi = (VP9D_COMP*)ctx->pbi;
365       VP9_COMMON *const cm = &pbi->common;
366       yuvconfig2image(&ctx->img, &sd, user_priv);
367
368       ctx->img.fb_priv = cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
369       ctx->img_avail = 1;
370     }
371   }
372
373   return res;
374 }
375
376 static void parse_superframe_index(const uint8_t *data, size_t data_sz,
377                                    uint32_t sizes[8], int *count) {
378   uint8_t marker;
379
380   assert(data_sz);
381   marker = data[data_sz - 1];
382   *count = 0;
383
384   if ((marker & 0xe0) == 0xc0) {
385     const uint32_t frames = (marker & 0x7) + 1;
386     const uint32_t mag = ((marker >> 3) & 0x3) + 1;
387     const size_t index_sz = 2 + mag * frames;
388
389     if (data_sz >= index_sz && data[data_sz - index_sz] == marker) {
390       // found a valid superframe index
391       uint32_t i, j;
392       const uint8_t *x = data + data_sz - index_sz + 1;
393
394       for (i = 0; i < frames; i++) {
395         uint32_t this_sz = 0;
396
397         for (j = 0; j < mag; j++)
398           this_sz |= (*x++) << (j * 8);
399         sizes[i] = this_sz;
400       }
401
402       *count = frames;
403     }
404   }
405 }
406
407 static vpx_codec_err_t vp9_decode(vpx_codec_alg_priv_t  *ctx,
408                                   const uint8_t         *data,
409                                   unsigned int           data_sz,
410                                   void                  *user_priv,
411                                   long                   deadline) {
412   const uint8_t *data_start = data;
413   const uint8_t *data_end = data + data_sz;
414   vpx_codec_err_t res = 0;
415   uint32_t sizes[8];
416   int frames_this_pts, frame_count = 0;
417
418   if (data == NULL || data_sz == 0) return VPX_CODEC_INVALID_PARAM;
419
420   parse_superframe_index(data, data_sz, sizes, &frames_this_pts);
421
422   do {
423     // Skip over the superframe index, if present
424     if (data_sz && (*data_start & 0xe0) == 0xc0) {
425       const uint8_t marker = *data_start;
426       const uint32_t frames = (marker & 0x7) + 1;
427       const uint32_t mag = ((marker >> 3) & 0x3) + 1;
428       const uint32_t index_sz = 2 + mag * frames;
429
430       if (data_sz >= index_sz && data_start[index_sz - 1] == marker) {
431         data_start += index_sz;
432         data_sz -= index_sz;
433         if (data_start < data_end)
434           continue;
435         else
436           break;
437       }
438     }
439
440     // Use the correct size for this frame, if an index is present.
441     if (frames_this_pts) {
442       uint32_t this_sz = sizes[frame_count];
443
444       if (data_sz < this_sz) {
445         ctx->base.err_detail = "Invalid frame size in index";
446         return VPX_CODEC_CORRUPT_FRAME;
447       }
448
449       data_sz = this_sz;
450       frame_count++;
451     }
452
453     res = decode_one(ctx, &data_start, data_sz, user_priv, deadline);
454     assert(data_start >= data);
455     assert(data_start <= data_end);
456
457     /* Early exit if there was a decode error */
458     if (res)
459       break;
460
461     /* Account for suboptimal termination by the encoder. */
462     while (data_start < data_end && *data_start == 0)
463       data_start++;
464
465     data_sz = data_end - data_start;
466   } while (data_start < data_end);
467   return res;
468 }
469
470 static vpx_image_t *vp9_get_frame(vpx_codec_alg_priv_t  *ctx,
471                                   vpx_codec_iter_t      *iter) {
472   vpx_image_t *img = NULL;
473
474   if (ctx->img_avail) {
475     /* iter acts as a flip flop, so an image is only returned on the first
476      * call to get_frame.
477      */
478     if (!(*iter)) {
479       img = &ctx->img;
480       *iter = img;
481     }
482   }
483   ctx->img_avail = 0;
484
485   return img;
486 }
487
488 static vpx_codec_err_t vp9_set_fb_fn(
489     vpx_codec_alg_priv_t *ctx,
490     vpx_get_frame_buffer_cb_fn_t cb_get,
491     vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
492   if (cb_get == NULL || cb_release == NULL) {
493     return VPX_CODEC_INVALID_PARAM;
494   } else if (ctx->pbi == NULL) {
495     // If the decoder has already been initialized, do not accept changes to
496     // the frame buffer functions.
497     ctx->get_ext_fb_cb = cb_get;
498     ctx->release_ext_fb_cb = cb_release;
499     ctx->ext_priv = cb_priv;
500     return VPX_CODEC_OK;
501   }
502
503   return VPX_CODEC_ERROR;
504 }
505
506 static vpx_codec_err_t vp9_xma_get_mmap(const vpx_codec_ctx_t *ctx,
507                                         vpx_codec_mmap_t *mmap,
508                                         vpx_codec_iter_t *iter) {
509   vpx_codec_err_t res;
510   const mem_req_t *seg_iter = *iter;
511
512   /* Get address of next segment request */
513   do {
514     if (!seg_iter)
515       seg_iter = vp9_mem_req_segs;
516     else if (seg_iter->id != VP9_SEG_MAX)
517       seg_iter++;
518
519     *iter = (vpx_codec_iter_t)seg_iter;
520
521     if (seg_iter->id != VP9_SEG_MAX) {
522       mmap->id = seg_iter->id;
523       mmap->sz = seg_iter->sz;
524       mmap->align = seg_iter->align;
525       mmap->flags = seg_iter->flags;
526
527       if (!seg_iter->sz)
528         mmap->sz = seg_iter->calc_sz(ctx->config.dec, ctx->init_flags);
529
530       res = VPX_CODEC_OK;
531     } else {
532       res = VPX_CODEC_LIST_END;
533     }
534   } while (!mmap->sz && res != VPX_CODEC_LIST_END);
535
536   return res;
537 }
538
539 static vpx_codec_err_t vp9_xma_set_mmap(vpx_codec_ctx_t *ctx,
540                                         const vpx_codec_mmap_t  *mmap) {
541   vpx_codec_err_t res = VPX_CODEC_MEM_ERROR;
542   int i, done;
543
544   if (!ctx->priv) {
545     if (mmap->id == VP9_SEG_ALG_PRIV) {
546       if (!ctx->priv) {
547         vp9_init_ctx(ctx, mmap);
548         res = VPX_CODEC_OK;
549       }
550     }
551   }
552
553   done = 1;
554
555   if (!res && ctx->priv->alg_priv) {
556     for (i = 0; i < NELEMENTS(ctx->priv->alg_priv->mmaps); i++) {
557       if (ctx->priv->alg_priv->mmaps[i].id == mmap->id)
558         if (!ctx->priv->alg_priv->mmaps[i].base) {
559           ctx->priv->alg_priv->mmaps[i] = *mmap;
560           res = VPX_CODEC_OK;
561         }
562
563       done &= (ctx->priv->alg_priv->mmaps[i].base != NULL);
564     }
565   }
566
567   if (done && !res) {
568     vp9_finalize_mmaps(ctx->priv->alg_priv);
569     res = ctx->iface->init(ctx, NULL);
570   }
571
572   return res;
573 }
574
575 static vpx_codec_err_t set_reference(vpx_codec_alg_priv_t *ctx, int ctr_id,
576                                      va_list args) {
577   vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
578
579   if (data) {
580     vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
581     YV12_BUFFER_CONFIG sd;
582
583     image2yuvconfig(&frame->img, &sd);
584     return vp9_set_reference_dec(ctx->pbi,
585                                  (VP9_REFFRAME)frame->frame_type, &sd);
586   } else {
587     return VPX_CODEC_INVALID_PARAM;
588   }
589 }
590
591 static vpx_codec_err_t copy_reference(vpx_codec_alg_priv_t *ctx, int ctr_id,
592                                       va_list args) {
593   vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
594
595   if (data) {
596     vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
597     YV12_BUFFER_CONFIG sd;
598
599     image2yuvconfig(&frame->img, &sd);
600
601     return vp9_copy_reference_dec(ctx->pbi,
602                                   (VP9_REFFRAME)frame->frame_type, &sd);
603   } else {
604     return VPX_CODEC_INVALID_PARAM;
605   }
606 }
607
608 static vpx_codec_err_t get_reference(vpx_codec_alg_priv_t *ctx, int ctr_id,
609                                      va_list args) {
610   vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
611
612   if (data) {
613     YV12_BUFFER_CONFIG* fb;
614
615     vp9_get_reference_dec(ctx->pbi, data->idx, &fb);
616     yuvconfig2image(&data->img, fb, NULL);
617     return VPX_CODEC_OK;
618   } else {
619     return VPX_CODEC_INVALID_PARAM;
620   }
621 }
622
623 static vpx_codec_err_t set_postproc(vpx_codec_alg_priv_t *ctx, int ctr_id,
624                                     va_list args) {
625 #if CONFIG_VP9_POSTPROC
626   vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
627
628   if (data) {
629     ctx->postproc_cfg_set = 1;
630     ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
631     return VPX_CODEC_OK;
632   } else {
633     return VPX_CODEC_INVALID_PARAM;
634   }
635 #else
636   return VPX_CODEC_INCAPABLE;
637 #endif
638 }
639
640 static vpx_codec_err_t set_dbg_options(vpx_codec_alg_priv_t *ctx, int ctrl_id,
641                                        va_list args) {
642 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
643   int data = va_arg(args, int);
644
645 #define MAP(id, var) case id: var = data; break;
646
647   switch (ctrl_id) {
648       MAP(VP8_SET_DBG_COLOR_REF_FRAME,   ctx->dbg_color_ref_frame_flag);
649       MAP(VP8_SET_DBG_COLOR_MB_MODES,    ctx->dbg_color_mb_modes_flag);
650       MAP(VP8_SET_DBG_COLOR_B_MODES,     ctx->dbg_color_b_modes_flag);
651       MAP(VP8_SET_DBG_DISPLAY_MV,        ctx->dbg_display_mv_flag);
652   }
653
654   return VPX_CODEC_OK;
655 #else
656   return VPX_CODEC_INCAPABLE;
657 #endif
658 }
659
660 static vpx_codec_err_t get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
661                                             int ctrl_id, va_list args) {
662   int *update_info = va_arg(args, int *);
663   VP9D_COMP *pbi = (VP9D_COMP*)ctx->pbi;
664
665   if (update_info) {
666     *update_info = pbi->refresh_frame_flags;
667
668     return VPX_CODEC_OK;
669   } else {
670     return VPX_CODEC_INVALID_PARAM;
671   }
672 }
673
674
675 static vpx_codec_err_t get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
676                                            int ctrl_id, va_list args) {
677   int *corrupted = va_arg(args, int *);
678
679   if (corrupted) {
680     VP9D_COMP *pbi = (VP9D_COMP*)ctx->pbi;
681     if (pbi)
682       *corrupted = pbi->common.frame_to_show->corrupted;
683     else
684       return VPX_CODEC_ERROR;
685     return VPX_CODEC_OK;
686   } else {
687     return VPX_CODEC_INVALID_PARAM;
688   }
689 }
690
691 static vpx_codec_err_t get_display_size(vpx_codec_alg_priv_t *ctx,
692                                         int ctrl_id, va_list args) {
693   int *const display_size = va_arg(args, int *);
694
695   if (display_size) {
696     const VP9D_COMP *const pbi = (VP9D_COMP*)ctx->pbi;
697     if (pbi) {
698       display_size[0] = pbi->common.display_width;
699       display_size[1] = pbi->common.display_height;
700     } else {
701       return VPX_CODEC_ERROR;
702     }
703     return VPX_CODEC_OK;
704   } else {
705     return VPX_CODEC_INVALID_PARAM;
706   }
707 }
708
709 static vpx_codec_err_t set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
710                                              int ctr_id,
711                                              va_list args) {
712   ctx->invert_tile_order = va_arg(args, int);
713   return VPX_CODEC_OK;
714 }
715
716 static vpx_codec_ctrl_fn_map_t ctf_maps[] = {
717   {VP8_SET_REFERENCE,             set_reference},
718   {VP8_COPY_REFERENCE,            copy_reference},
719   {VP8_SET_POSTPROC,              set_postproc},
720   {VP8_SET_DBG_COLOR_REF_FRAME,   set_dbg_options},
721   {VP8_SET_DBG_COLOR_MB_MODES,    set_dbg_options},
722   {VP8_SET_DBG_COLOR_B_MODES,     set_dbg_options},
723   {VP8_SET_DBG_DISPLAY_MV,        set_dbg_options},
724   {VP8D_GET_LAST_REF_UPDATES,     get_last_ref_updates},
725   {VP8D_GET_FRAME_CORRUPTED,      get_frame_corrupted},
726   {VP9_GET_REFERENCE,             get_reference},
727   {VP9D_GET_DISPLAY_SIZE,         get_display_size},
728   {VP9_INVERT_TILE_DECODE_ORDER,  set_invert_tile_order},
729   { -1, NULL},
730 };
731
732
733 #ifndef VERSION_STRING
734 #define VERSION_STRING
735 #endif
736 CODEC_INTERFACE(vpx_codec_vp9_dx) = {
737   "WebM Project VP9 Decoder" VERSION_STRING,
738   VPX_CODEC_INTERNAL_ABI_VERSION,
739   VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
740       VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER,
741   /* vpx_codec_caps_t          caps; */
742   vp9_init,         /* vpx_codec_init_fn_t       init; */
743   vp9_destroy,      /* vpx_codec_destroy_fn_t    destroy; */
744   ctf_maps,         /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
745   vp9_xma_get_mmap, /* vpx_codec_get_mmap_fn_t   get_mmap; */
746   vp9_xma_set_mmap, /* vpx_codec_set_mmap_fn_t   set_mmap; */
747   { // NOLINT
748     vp9_peek_si,      /* vpx_codec_peek_si_fn_t    peek_si; */
749     vp9_get_si,       /* vpx_codec_get_si_fn_t     get_si; */
750     vp9_decode,       /* vpx_codec_decode_fn_t     decode; */
751     vp9_get_frame,    /* vpx_codec_frame_get_fn_t  frame_get; */
752     vp9_set_fb_fn,    /* vpx_codec_set_fb_fn_t     set_fb_fn; */
753   },
754   { // NOLINT
755     /* encoder functions */
756     NOT_IMPLEMENTED,
757     NOT_IMPLEMENTED,
758     NOT_IMPLEMENTED,
759     NOT_IMPLEMENTED,
760     NOT_IMPLEMENTED,
761     NOT_IMPLEMENTED
762   }
763 };