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