Merge "Use endian-neutral bitstream packing/unpacking"
[profile/ivi/libvpx.git] / vp8 / vp8_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 "common/onyxd.h"
19 #include "decoder/onyxd_int.h"
20
21 #define VP8_CAP_POSTPROC (CONFIG_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
22
23 typedef vpx_codec_stream_info_t  vp8_stream_info_t;
24
25 /* Structures for handling memory allocations */
26 typedef enum
27 {
28     VP8_SEG_ALG_PRIV     = 256,
29     VP8_SEG_MAX
30 } mem_seg_id_t;
31 #define NELEMENTS(x) ((int)(sizeof(x)/sizeof(x[0])))
32
33 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t);
34
35 typedef struct
36 {
37     unsigned int   id;
38     unsigned long  sz;
39     unsigned int   align;
40     unsigned int   flags;
41     unsigned long(*calc_sz)(const vpx_codec_dec_cfg_t *, vpx_codec_flags_t);
42 } mem_req_t;
43
44 static const mem_req_t vp8_mem_req_segs[] =
45 {
46     {VP8_SEG_ALG_PRIV,    0, 8, VPX_CODEC_MEM_ZERO, vp8_priv_sz},
47     {VP8_SEG_MAX, 0, 0, 0, NULL}
48 };
49
50 struct vpx_codec_alg_priv
51 {
52     vpx_codec_priv_t        base;
53     vpx_codec_mmap_t        mmaps[NELEMENTS(vp8_mem_req_segs)-1];
54     vpx_codec_dec_cfg_t     cfg;
55     vp8_stream_info_t       si;
56     int                     defer_alloc;
57     int                     decoder_init;
58     VP8D_PTR                pbi;
59     int                     postproc_cfg_set;
60     vp8_postproc_cfg_t      postproc_cfg;
61 #if CONFIG_POSTPROC_VISUALIZER
62     unsigned int            dbg_postproc_flag;
63     int                     dbg_color_ref_frame_flag;
64     int                     dbg_color_mb_modes_flag;
65     int                     dbg_color_b_modes_flag;
66     int                     dbg_display_mv_flag;
67 #endif
68     vpx_image_t             img;
69     int                     img_setup;
70     int                     img_avail;
71 };
72
73 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t flags)
74 {
75     /* Although this declaration is constant, we can't use it in the requested
76      * segments list because we want to define the requested segments list
77      * before defining the private type (so that the number of memory maps is
78      * known)
79      */
80     (void)si;
81     return sizeof(vpx_codec_alg_priv_t);
82 }
83
84
85 static void vp8_mmap_dtor(vpx_codec_mmap_t *mmap)
86 {
87     free(mmap->priv);
88 }
89
90 static vpx_codec_err_t vp8_mmap_alloc(vpx_codec_mmap_t *mmap)
91 {
92     vpx_codec_err_t  res;
93     unsigned int   align;
94
95     align = mmap->align ? mmap->align - 1 : 0;
96
97     if (mmap->flags & VPX_CODEC_MEM_ZERO)
98         mmap->priv = calloc(1, mmap->sz + align);
99     else
100         mmap->priv = malloc(mmap->sz + align);
101
102     res = (mmap->priv) ? VPX_CODEC_OK : VPX_CODEC_MEM_ERROR;
103     mmap->base = (void *)((((uintptr_t)mmap->priv) + align) & ~(uintptr_t)align);
104     mmap->dtor = vp8_mmap_dtor;
105     return res;
106 }
107
108 static vpx_codec_err_t vp8_validate_mmaps(const vp8_stream_info_t *si,
109         const vpx_codec_mmap_t        *mmaps,
110         vpx_codec_flags_t              init_flags)
111 {
112     int i;
113     vpx_codec_err_t res = VPX_CODEC_OK;
114
115     for (i = 0; i < NELEMENTS(vp8_mem_req_segs) - 1; i++)
116     {
117         /* Ensure the segment has been allocated */
118         if (!mmaps[i].base)
119         {
120             res = VPX_CODEC_MEM_ERROR;
121             break;
122         }
123
124         /* Verify variable size segment is big enough for the current si. */
125         if (vp8_mem_req_segs[i].calc_sz)
126         {
127             vpx_codec_dec_cfg_t cfg;
128
129             cfg.w = si->w;
130             cfg.h = si->h;
131
132             if (mmaps[i].sz < vp8_mem_req_segs[i].calc_sz(&cfg, init_flags))
133             {
134                 res = VPX_CODEC_MEM_ERROR;
135                 break;
136             }
137         }
138     }
139
140     return res;
141 }
142
143 static void vp8_init_ctx(vpx_codec_ctx_t *ctx, const vpx_codec_mmap_t *mmap)
144 {
145     int i;
146
147     ctx->priv = mmap->base;
148     ctx->priv->sz = sizeof(*ctx->priv);
149     ctx->priv->iface = ctx->iface;
150     ctx->priv->alg_priv = mmap->base;
151
152     for (i = 0; i < NELEMENTS(ctx->priv->alg_priv->mmaps); i++)
153         ctx->priv->alg_priv->mmaps[i].id = vp8_mem_req_segs[i].id;
154
155     ctx->priv->alg_priv->mmaps[0] = *mmap;
156     ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si);
157     ctx->priv->init_flags = ctx->init_flags;
158
159     if (ctx->config.dec)
160     {
161         /* Update the reference to the config structure to an internal copy. */
162         ctx->priv->alg_priv->cfg = *ctx->config.dec;
163         ctx->config.dec = &ctx->priv->alg_priv->cfg;
164     }
165 }
166
167 static void *mmap_lkup(vpx_codec_alg_priv_t *ctx, unsigned int id)
168 {
169     int i;
170
171     for (i = 0; i < NELEMENTS(vp8_mem_req_segs); i++)
172         if (ctx->mmaps[i].id == id)
173             return ctx->mmaps[i].base;
174
175     return NULL;
176 }
177 static void vp8_finalize_mmaps(vpx_codec_alg_priv_t *ctx)
178 {
179     /*
180     ctx->pbi = mmap_lkup(ctx, VP6_SEG_PB_INSTANCE);
181     ctx->pbi->mbi.block_dx_info[0].idct_output_ptr = mmap_lkup(ctx, VP6_SEG_IDCT_BUFFER);
182     ctx->pbi->loop_filtered_block = mmap_lkup(ctx, VP6_SEG_LF_BLOCK);
183     ctx->pbi->huff = mmap_lkup(ctx, VP6_SEG_HUFF);
184     ctx->pbi->mbi.coeffs_base_ptr = mmap_lkup(ctx, VP6_SEG_COEFFS);
185     ctx->pbi->fc.above_y = mmap_lkup(ctx, VP6_SEG_ABOVEY);
186     ctx->pbi->fc.above_u = mmap_lkup(ctx, VP6_SEG_ABOVEU);
187     ctx->pbi->fc.above_v = mmap_lkup(ctx, VP6_SEG_ABOVEV);
188     ctx->pbi->prediction_mode = mmap_lkup(ctx, VP6_SEG_PRED_MODES);
189     ctx->pbi->mbmotion_vector = mmap_lkup(ctx, VP6_SEG_MV_FIELD);
190     ctx->pbi->fb_storage_ptr[0] = mmap_lkup(ctx, VP6_SEG_IMG0_STRG);
191     ctx->pbi->fb_storage_ptr[1] = mmap_lkup(ctx, VP6_SEG_IMG1_STRG);
192     ctx->pbi->fb_storage_ptr[2] = mmap_lkup(ctx, VP6_SEG_IMG2_STRG);
193     #if CONFIG_POSTPROC
194     ctx->pbi->postproc.deblock.fragment_variances = mmap_lkup(ctx, VP6_SEG_DEBLOCKER);
195     ctx->pbi->fb_storage_ptr[3] = mmap_lkup(ctx, VP6_SEG_PP_IMG_STRG);
196     #endif
197     */
198 }
199
200 static vpx_codec_err_t vp8_init(vpx_codec_ctx_t *ctx)
201 {
202     vpx_codec_err_t        res = VPX_CODEC_OK;
203
204     /* This function only allocates space for the vpx_codec_alg_priv_t
205      * structure. More memory may be required at the time the stream
206      * information becomes known.
207      */
208     if (!ctx->priv)
209     {
210         vpx_codec_mmap_t mmap;
211
212         mmap.id = vp8_mem_req_segs[0].id;
213         mmap.sz = sizeof(vpx_codec_alg_priv_t);
214         mmap.align = vp8_mem_req_segs[0].align;
215         mmap.flags = vp8_mem_req_segs[0].flags;
216
217         res = vp8_mmap_alloc(&mmap);
218
219         if (!res)
220         {
221             vp8_init_ctx(ctx, &mmap);
222
223             ctx->priv->alg_priv->defer_alloc = 1;
224             /*post processing level initialized to do nothing */
225         }
226     }
227
228     return res;
229 }
230
231 static vpx_codec_err_t vp8_destroy(vpx_codec_alg_priv_t *ctx)
232 {
233     int i;
234
235     vp8dx_remove_decompressor(ctx->pbi);
236
237     for (i = NELEMENTS(ctx->mmaps) - 1; i >= 0; i--)
238     {
239         if (ctx->mmaps[i].dtor)
240             ctx->mmaps[i].dtor(&ctx->mmaps[i]);
241     }
242
243     return VPX_CODEC_OK;
244 }
245
246 static vpx_codec_err_t vp8_peek_si(const uint8_t         *data,
247                                    unsigned int           data_sz,
248                                    vpx_codec_stream_info_t *si)
249 {
250     vpx_codec_err_t res = VPX_CODEC_OK;
251
252     if(data + data_sz <= data)
253         res = VPX_CODEC_INVALID_PARAM;
254     else
255     {
256         /* Parse uncompresssed part of key frame header.
257          * 3 bytes:- including version, frame type and an offset
258          * 3 bytes:- sync code (0x9d, 0x01, 0x2a)
259          * 4 bytes:- including image width and height in the lowest 14 bits
260          *           of each 2-byte value.
261          */
262         si->is_kf = 0;
263
264         if (data_sz >= 10 && !(data[0] & 0x01))  /* I-Frame */
265         {
266             const uint8_t *c = data + 3;
267             si->is_kf = 1;
268
269             /* vet via sync code */
270             if (c[0] != 0x9d || c[1] != 0x01 || c[2] != 0x2a)
271                 res = VPX_CODEC_UNSUP_BITSTREAM;
272
273             si->w = (c[3] | (c[4] << 8)) & 0x3fff;
274             si->h = (c[5] | (c[6] << 8)) & 0x3fff;
275
276             /*printf("w=%d, h=%d\n", si->w, si->h);*/
277             if (!(si->h | si->w))
278                 res = VPX_CODEC_UNSUP_BITSTREAM;
279         }
280         else
281             res = VPX_CODEC_UNSUP_BITSTREAM;
282     }
283
284     return res;
285
286 }
287
288 static vpx_codec_err_t vp8_get_si(vpx_codec_alg_priv_t    *ctx,
289                                   vpx_codec_stream_info_t *si)
290 {
291
292     unsigned int sz;
293
294     if (si->sz >= sizeof(vp8_stream_info_t))
295         sz = sizeof(vp8_stream_info_t);
296     else
297         sz = sizeof(vpx_codec_stream_info_t);
298
299     memcpy(si, &ctx->si, sz);
300     si->sz = sz;
301
302     return VPX_CODEC_OK;
303 }
304
305
306 static vpx_codec_err_t
307 update_error_state(vpx_codec_alg_priv_t                 *ctx,
308                    const struct vpx_internal_error_info *error)
309 {
310     vpx_codec_err_t res;
311
312     if ((res = error->error_code))
313         ctx->base.err_detail = error->has_detail
314                                ? error->detail
315                                : NULL;
316
317     return res;
318 }
319
320
321 static vpx_codec_err_t vp8_decode(vpx_codec_alg_priv_t  *ctx,
322                                   const uint8_t         *data,
323                                   unsigned int            data_sz,
324                                   void                    *user_priv,
325                                   long                    deadline)
326 {
327     vpx_codec_err_t res = VPX_CODEC_OK;
328
329     ctx->img_avail = 0;
330
331     /* Determine the stream parameters. Note that we rely on peek_si to
332      * validate that we have a buffer that does not wrap around the top
333      * of the heap.
334      */
335     if (!ctx->si.h)
336         res = ctx->base.iface->dec.peek_si(data, data_sz, &ctx->si);
337
338
339     /* Perform deferred allocations, if required */
340     if (!res && ctx->defer_alloc)
341     {
342         int i;
343
344         for (i = 1; !res && i < NELEMENTS(ctx->mmaps); i++)
345         {
346             vpx_codec_dec_cfg_t cfg;
347
348             cfg.w = ctx->si.w;
349             cfg.h = ctx->si.h;
350             ctx->mmaps[i].id = vp8_mem_req_segs[i].id;
351             ctx->mmaps[i].sz = vp8_mem_req_segs[i].sz;
352             ctx->mmaps[i].align = vp8_mem_req_segs[i].align;
353             ctx->mmaps[i].flags = vp8_mem_req_segs[i].flags;
354
355             if (!ctx->mmaps[i].sz)
356                 ctx->mmaps[i].sz = vp8_mem_req_segs[i].calc_sz(&cfg,
357                                    ctx->base.init_flags);
358
359             res = vp8_mmap_alloc(&ctx->mmaps[i]);
360         }
361
362         if (!res)
363             vp8_finalize_mmaps(ctx);
364
365         ctx->defer_alloc = 0;
366     }
367
368     /* Initialize the decoder instance on the first frame*/
369     if (!res && !ctx->decoder_init)
370     {
371         res = vp8_validate_mmaps(&ctx->si, ctx->mmaps, ctx->base.init_flags);
372
373         if (!res)
374         {
375             VP8D_CONFIG oxcf;
376             VP8D_PTR optr;
377
378             vp8dx_initialize();
379
380             oxcf.Width = ctx->si.w;
381             oxcf.Height = ctx->si.h;
382             oxcf.Version = 9;
383             oxcf.postprocess = 0;
384             oxcf.max_threads = ctx->cfg.threads;
385
386             optr = vp8dx_create_decompressor(&oxcf);
387
388             /* If postprocessing was enabled by the application and a
389              * configuration has not been provided, default it.
390              */
391             if (!ctx->postproc_cfg_set
392                 && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
393             {
394                 ctx->postproc_cfg.post_proc_flag =
395                     VP8_DEBLOCK | VP8_DEMACROBLOCK;
396                 ctx->postproc_cfg.deblocking_level = 4;
397                 ctx->postproc_cfg.noise_level = 0;
398             }
399
400             if (!optr)
401                 res = VPX_CODEC_ERROR;
402             else
403                 ctx->pbi = optr;
404         }
405
406         ctx->decoder_init = 1;
407     }
408
409     if (!res && ctx->pbi)
410     {
411         YV12_BUFFER_CONFIG sd;
412         INT64 time_stamp = 0, time_end_stamp = 0;
413         vp8_ppflags_t flags = {0};
414
415         if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
416         {
417             flags.post_proc_flag= ctx->postproc_cfg.post_proc_flag
418 #if CONFIG_POSTPROC_VISUALIZER
419
420                                 | ((ctx->dbg_color_ref_frame_flag != 0) ? VP8D_DEBUG_CLR_FRM_REF_BLKS : 0)
421                                 | ((ctx->dbg_color_mb_modes_flag != 0) ? VP8D_DEBUG_CLR_BLK_MODES : 0)
422                                 | ((ctx->dbg_color_b_modes_flag != 0) ? VP8D_DEBUG_CLR_BLK_MODES : 0)
423                                 | ((ctx->dbg_display_mv_flag != 0) ? VP8D_DEBUG_DRAW_MV : 0)
424 #endif
425                                 ;
426             flags.deblocking_level      = ctx->postproc_cfg.deblocking_level;
427             flags.noise_level           = ctx->postproc_cfg.noise_level;
428 #if CONFIG_POSTPROC_VISUALIZER
429             flags.display_ref_frame_flag= ctx->dbg_color_ref_frame_flag;
430             flags.display_mb_modes_flag = ctx->dbg_color_mb_modes_flag;
431             flags.display_b_modes_flag  = ctx->dbg_color_b_modes_flag;
432             flags.display_mv_flag       = ctx->dbg_display_mv_flag;
433 #endif
434         }
435
436         if (vp8dx_receive_compressed_data(ctx->pbi, data_sz, data, deadline))
437         {
438             VP8D_COMP *pbi = (VP8D_COMP *)ctx->pbi;
439             res = update_error_state(ctx, &pbi->common.error);
440         }
441
442         if (!res && 0 == vp8dx_get_raw_frame(ctx->pbi, &sd, &time_stamp, &time_end_stamp, &flags))
443         {
444             /* Align width/height */
445             unsigned int a_w = (sd.y_width + 15) & ~15;
446             unsigned int a_h = (sd.y_height + 15) & ~15;
447
448             vpx_img_wrap(&ctx->img, VPX_IMG_FMT_I420,
449                          a_w + 2 * VP8BORDERINPIXELS,
450                          a_h + 2 * VP8BORDERINPIXELS,
451                          1,
452                          sd.buffer_alloc);
453             vpx_img_set_rect(&ctx->img,
454                              VP8BORDERINPIXELS, VP8BORDERINPIXELS,
455                              sd.y_width, sd.y_height);
456             ctx->img.user_priv = user_priv;
457             ctx->img_avail = 1;
458
459         }
460     }
461
462     return res;
463 }
464
465 static vpx_image_t *vp8_get_frame(vpx_codec_alg_priv_t  *ctx,
466                                   vpx_codec_iter_t      *iter)
467 {
468     vpx_image_t *img = NULL;
469
470     if (ctx->img_avail)
471     {
472         /* iter acts as a flip flop, so an image is only returned on the first
473          * call to get_frame.
474          */
475         if (!(*iter))
476         {
477             img = &ctx->img;
478             *iter = img;
479         }
480     }
481
482     return img;
483 }
484
485
486 static
487 vpx_codec_err_t vp8_xma_get_mmap(const vpx_codec_ctx_t      *ctx,
488                                  vpx_codec_mmap_t           *mmap,
489                                  vpx_codec_iter_t           *iter)
490 {
491     vpx_codec_err_t     res;
492     const mem_req_t  *seg_iter = *iter;
493
494     /* Get address of next segment request */
495     do
496     {
497         if (!seg_iter)
498             seg_iter = vp8_mem_req_segs;
499         else if (seg_iter->id != VP8_SEG_MAX)
500             seg_iter++;
501
502         *iter = (vpx_codec_iter_t)seg_iter;
503
504         if (seg_iter->id != VP8_SEG_MAX)
505         {
506             mmap->id = seg_iter->id;
507             mmap->sz = seg_iter->sz;
508             mmap->align = seg_iter->align;
509             mmap->flags = seg_iter->flags;
510
511             if (!seg_iter->sz)
512                 mmap->sz = seg_iter->calc_sz(ctx->config.dec, ctx->init_flags);
513
514             res = VPX_CODEC_OK;
515         }
516         else
517             res = VPX_CODEC_LIST_END;
518     }
519     while (!mmap->sz && res != VPX_CODEC_LIST_END);
520
521     return res;
522 }
523
524 static vpx_codec_err_t vp8_xma_set_mmap(vpx_codec_ctx_t         *ctx,
525                                         const vpx_codec_mmap_t  *mmap)
526 {
527     vpx_codec_err_t res = VPX_CODEC_MEM_ERROR;
528     int i, done;
529
530     if (!ctx->priv)
531     {
532         if (mmap->id == VP8_SEG_ALG_PRIV)
533         {
534             if (!ctx->priv)
535             {
536                 vp8_init_ctx(ctx, mmap);
537                 res = VPX_CODEC_OK;
538             }
539         }
540     }
541
542     done = 1;
543
544     if (!res && ctx->priv->alg_priv)
545     {
546         for (i = 0; i < NELEMENTS(vp8_mem_req_segs); i++)
547         {
548             if (ctx->priv->alg_priv->mmaps[i].id == mmap->id)
549                 if (!ctx->priv->alg_priv->mmaps[i].base)
550                 {
551                     ctx->priv->alg_priv->mmaps[i] = *mmap;
552                     res = VPX_CODEC_OK;
553                 }
554
555             done &= (ctx->priv->alg_priv->mmaps[i].base != NULL);
556         }
557     }
558
559     if (done && !res)
560     {
561         vp8_finalize_mmaps(ctx->priv->alg_priv);
562         res = ctx->iface->init(ctx);
563     }
564
565     return res;
566 }
567
568 static vpx_codec_err_t image2yuvconfig(const vpx_image_t   *img,
569                                        YV12_BUFFER_CONFIG  *yv12)
570 {
571     vpx_codec_err_t        res = VPX_CODEC_OK;
572     yv12->y_buffer = img->planes[VPX_PLANE_Y];
573     yv12->u_buffer = img->planes[VPX_PLANE_U];
574     yv12->v_buffer = img->planes[VPX_PLANE_V];
575
576     yv12->y_width  = img->d_w;
577     yv12->y_height = img->d_h;
578     yv12->uv_width = yv12->y_width / 2;
579     yv12->uv_height = yv12->y_height / 2;
580
581     yv12->y_stride = img->stride[VPX_PLANE_Y];
582     yv12->uv_stride = img->stride[VPX_PLANE_U];
583
584     yv12->border  = (img->stride[VPX_PLANE_Y] - img->d_w) / 2;
585     yv12->clrtype = (img->fmt == VPX_IMG_FMT_VPXI420 || img->fmt == VPX_IMG_FMT_VPXYV12);
586
587     return res;
588 }
589
590
591 static vpx_codec_err_t vp8_set_reference(vpx_codec_alg_priv_t *ctx,
592         int ctr_id,
593         va_list args)
594 {
595
596     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
597
598     if (data)
599     {
600         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
601         YV12_BUFFER_CONFIG sd;
602
603         image2yuvconfig(&frame->img, &sd);
604
605         vp8dx_set_reference(ctx->pbi, frame->frame_type, &sd);
606         return VPX_CODEC_OK;
607     }
608     else
609         return VPX_CODEC_INVALID_PARAM;
610
611 }
612
613 static vpx_codec_err_t vp8_get_reference(vpx_codec_alg_priv_t *ctx,
614         int ctr_id,
615         va_list args)
616 {
617
618     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
619
620     if (data)
621     {
622         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
623         YV12_BUFFER_CONFIG sd;
624
625         image2yuvconfig(&frame->img, &sd);
626
627         vp8dx_get_reference(ctx->pbi, frame->frame_type, &sd);
628         return VPX_CODEC_OK;
629     }
630     else
631         return VPX_CODEC_INVALID_PARAM;
632
633 }
634
635 static vpx_codec_err_t vp8_set_postproc(vpx_codec_alg_priv_t *ctx,
636                                         int ctr_id,
637                                         va_list args)
638 {
639     vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
640 #if CONFIG_POSTPROC
641
642     if (data)
643     {
644         ctx->postproc_cfg_set = 1;
645         ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
646         return VPX_CODEC_OK;
647     }
648     else
649         return VPX_CODEC_INVALID_PARAM;
650
651 #else
652     return VPX_CODEC_INCAPABLE;
653 #endif
654 }
655
656 static vpx_codec_err_t vp8_set_dbg_options(vpx_codec_alg_priv_t *ctx,
657                                         int ctrl_id,
658                                         va_list args)
659 {
660 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
661     int data = va_arg(args, int);
662
663 #define MAP(id, var) case id: var = data; break;
664
665     switch (ctrl_id)
666     {
667         MAP (VP8_SET_DBG_COLOR_REF_FRAME,   ctx->dbg_color_ref_frame_flag);
668         MAP (VP8_SET_DBG_COLOR_MB_MODES,    ctx->dbg_color_mb_modes_flag);
669         MAP (VP8_SET_DBG_COLOR_B_MODES,     ctx->dbg_color_b_modes_flag);
670         MAP (VP8_SET_DBG_DISPLAY_MV,        ctx->dbg_display_mv_flag);
671     }
672
673     return VPX_CODEC_OK;
674 #else
675     return VPX_CODEC_INCAPABLE;
676 #endif
677 }
678
679 static vpx_codec_err_t vp8_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
680                                                 int ctrl_id,
681                                                 va_list args)
682 {
683     int *update_info = va_arg(args, int *);
684     VP8D_COMP *pbi = (VP8D_COMP *)ctx->pbi;
685
686     if (update_info)
687     {
688         *update_info = pbi->common.refresh_alt_ref_frame * (int) VP8_ALTR_FRAME
689             + pbi->common.refresh_golden_frame * (int) VP8_GOLD_FRAME
690             + pbi->common.refresh_last_frame * (int) VP8_LAST_FRAME;
691
692         return VPX_CODEC_OK;
693     }
694     else
695         return VPX_CODEC_INVALID_PARAM;
696 }
697
698
699 static vpx_codec_err_t vp8_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
700                                                int ctrl_id,
701                                                va_list args)
702 {
703
704     int *corrupted = va_arg(args, int *);
705
706     if (corrupted)
707     {
708         VP8D_COMP *pbi = (VP8D_COMP *)ctx->pbi;
709         *corrupted = pbi->common.frame_to_show->corrupted;
710
711         return VPX_CODEC_OK;
712     }
713     else
714         return VPX_CODEC_INVALID_PARAM;
715
716 }
717
718 vpx_codec_ctrl_fn_map_t vp8_ctf_maps[] =
719 {
720     {VP8_SET_REFERENCE,             vp8_set_reference},
721     {VP8_COPY_REFERENCE,            vp8_get_reference},
722     {VP8_SET_POSTPROC,              vp8_set_postproc},
723     {VP8_SET_DBG_COLOR_REF_FRAME,   vp8_set_dbg_options},
724     {VP8_SET_DBG_COLOR_MB_MODES,    vp8_set_dbg_options},
725     {VP8_SET_DBG_COLOR_B_MODES,     vp8_set_dbg_options},
726     {VP8_SET_DBG_DISPLAY_MV,        vp8_set_dbg_options},
727     {VP8D_GET_LAST_REF_UPDATES,     vp8_get_last_ref_updates},
728     {VP8D_GET_FRAME_CORRUPTED,      vp8_get_frame_corrupted},
729     { -1, NULL},
730 };
731
732
733 #ifndef VERSION_STRING
734 #define VERSION_STRING
735 #endif
736 CODEC_INTERFACE(vpx_codec_vp8_dx) =
737 {
738     "WebM Project VP8 Decoder" VERSION_STRING,
739     VPX_CODEC_INTERNAL_ABI_VERSION,
740     VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC,
741     /* vpx_codec_caps_t          caps; */
742     vp8_init,         /* vpx_codec_init_fn_t       init; */
743     vp8_destroy,      /* vpx_codec_destroy_fn_t    destroy; */
744     vp8_ctf_maps,     /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
745     vp8_xma_get_mmap, /* vpx_codec_get_mmap_fn_t   get_mmap; */
746     vp8_xma_set_mmap, /* vpx_codec_set_mmap_fn_t   set_mmap; */
747     {
748         vp8_peek_si,      /* vpx_codec_peek_si_fn_t    peek_si; */
749         vp8_get_si,       /* vpx_codec_get_si_fn_t     get_si; */
750         vp8_decode,       /* vpx_codec_decode_fn_t     decode; */
751         vp8_get_frame,    /* vpx_codec_frame_get_fn_t  frame_get; */
752     },
753     { /* encoder functions */
754         NOT_IMPLEMENTED,
755         NOT_IMPLEMENTED,
756         NOT_IMPLEMENTED,
757         NOT_IMPLEMENTED,
758         NOT_IMPLEMENTED,
759         NOT_IMPLEMENTED
760     }
761 };
762
763 /*
764  * BEGIN BACKWARDS COMPATIBILITY SHIM.
765  */
766 vpx_codec_iface_t vpx_codec_vp8_algo =
767 {
768     "WebM Project VP8 Decoder (Deprecated API)" VERSION_STRING,
769     VPX_CODEC_INTERNAL_ABI_VERSION,
770     VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC,
771     /* vpx_codec_caps_t          caps; */
772     vp8_init,         /* vpx_codec_init_fn_t       init; */
773     vp8_destroy,      /* vpx_codec_destroy_fn_t    destroy; */
774     vp8_ctf_maps,     /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
775     vp8_xma_get_mmap, /* vpx_codec_get_mmap_fn_t   get_mmap; */
776     vp8_xma_set_mmap, /* vpx_codec_set_mmap_fn_t   set_mmap; */
777     {
778         vp8_peek_si,      /* vpx_codec_peek_si_fn_t    peek_si; */
779         vp8_get_si,       /* vpx_codec_get_si_fn_t     get_si; */
780         vp8_decode,       /* vpx_codec_decode_fn_t     decode; */
781         vp8_get_frame,    /* vpx_codec_frame_get_fn_t  frame_get; */
782     },
783     { /* encoder functions */
784         NOT_IMPLEMENTED,
785         NOT_IMPLEMENTED,
786         NOT_IMPLEMENTED,
787         NOT_IMPLEMENTED,
788         NOT_IMPLEMENTED,
789         NOT_IMPLEMENTED
790     }
791 };