Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / 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 "vp8_rtcd.h"
15 #include "vpx/vpx_decoder.h"
16 #include "vpx/vp8dx.h"
17 #include "vpx/internal/vpx_codec_internal.h"
18 #include "vpx_version.h"
19 #include "common/alloccommon.h"
20 #include "common/common.h"
21 #include "common/onyxd.h"
22 #include "decoder/onyxd_int.h"
23 #include "vpx_mem/vpx_mem.h"
24 #if CONFIG_ERROR_CONCEALMENT
25 #include "decoder/error_concealment.h"
26 #endif
27 #include "decoder/decoderthreading.h"
28
29 #define VP8_CAP_POSTPROC (CONFIG_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
30 #define VP8_CAP_ERROR_CONCEALMENT (CONFIG_ERROR_CONCEALMENT ? \
31                                     VPX_CODEC_CAP_ERROR_CONCEALMENT : 0)
32
33 typedef vpx_codec_stream_info_t  vp8_stream_info_t;
34
35 /* Structures for handling memory allocations */
36 typedef enum
37 {
38     VP8_SEG_ALG_PRIV     = 256,
39     VP8_SEG_MAX
40 } mem_seg_id_t;
41 #define NELEMENTS(x) ((int)(sizeof(x)/sizeof(x[0])))
42
43 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t);
44
45 struct vpx_codec_alg_priv
46 {
47     vpx_codec_priv_t        base;
48     vpx_codec_dec_cfg_t     cfg;
49     vp8_stream_info_t       si;
50     int                     decoder_init;
51     int                     postproc_cfg_set;
52     vp8_postproc_cfg_t      postproc_cfg;
53 #if CONFIG_POSTPROC_VISUALIZER
54     unsigned int            dbg_postproc_flag;
55     int                     dbg_color_ref_frame_flag;
56     int                     dbg_color_mb_modes_flag;
57     int                     dbg_color_b_modes_flag;
58     int                     dbg_display_mv_flag;
59 #endif
60     vpx_decrypt_cb          decrypt_cb;
61     void                    *decrypt_state;
62     vpx_image_t             img;
63     int                     img_setup;
64     struct frame_buffers    yv12_frame_buffers;
65     void                    *user_priv;
66     FRAGMENT_DATA           fragments;
67 };
68
69 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t flags)
70 {
71     /* Although this declaration is constant, we can't use it in the requested
72      * segments list because we want to define the requested segments list
73      * before defining the private type (so that the number of memory maps is
74      * known)
75      */
76     (void)si;
77     return sizeof(vpx_codec_alg_priv_t);
78 }
79
80 static void vp8_init_ctx(vpx_codec_ctx_t *ctx)
81 {
82     ctx->priv =
83         (vpx_codec_priv_t *)vpx_memalign(8, sizeof(vpx_codec_alg_priv_t));
84     vpx_memset(ctx->priv, 0, sizeof(vpx_codec_alg_priv_t));
85     ctx->priv->sz = sizeof(*ctx->priv);
86     ctx->priv->iface = ctx->iface;
87     ctx->priv->alg_priv = (vpx_codec_alg_priv_t *)ctx->priv;
88     ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si);
89     ctx->priv->alg_priv->decrypt_cb = NULL;
90     ctx->priv->alg_priv->decrypt_state = NULL;
91     ctx->priv->init_flags = ctx->init_flags;
92
93     if (ctx->config.dec)
94     {
95         /* Update the reference to the config structure to an internal copy. */
96         ctx->priv->alg_priv->cfg = *ctx->config.dec;
97         ctx->config.dec = &ctx->priv->alg_priv->cfg;
98     }
99 }
100
101 static vpx_codec_err_t vp8_init(vpx_codec_ctx_t *ctx,
102                                 vpx_codec_priv_enc_mr_cfg_t *data)
103 {
104     vpx_codec_err_t        res = VPX_CODEC_OK;
105     (void) data;
106
107     vp8_rtcd();
108
109     /* This function only allocates space for the vpx_codec_alg_priv_t
110      * structure. More memory may be required at the time the stream
111      * information becomes known.
112      */
113     if (!ctx->priv)
114     {
115         vp8_init_ctx(ctx);
116
117         /* initialize number of fragments to zero */
118         ctx->priv->alg_priv->fragments.count = 0;
119         /* is input fragments enabled? */
120         ctx->priv->alg_priv->fragments.enabled =
121                 (ctx->priv->alg_priv->base.init_flags &
122                     VPX_CODEC_USE_INPUT_FRAGMENTS);
123
124         /*post processing level initialized to do nothing */
125     }
126
127     ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads =
128             (ctx->priv->alg_priv->base.init_flags &
129                     VPX_CODEC_USE_FRAME_THREADING);
130
131     /* for now, disable frame threading */
132     ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads = 0;
133
134     if(ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads &&
135             (( ctx->priv->alg_priv->base.init_flags &
136                             VPX_CODEC_USE_ERROR_CONCEALMENT)
137                     || ( ctx->priv->alg_priv->base.init_flags &
138                             VPX_CODEC_USE_INPUT_FRAGMENTS) ) )
139     {
140         /* row-based threading, error concealment, and input fragments will
141          * not be supported when using frame-based threading */
142         res = VPX_CODEC_INVALID_PARAM;
143     }
144
145     return res;
146 }
147
148 static vpx_codec_err_t vp8_destroy(vpx_codec_alg_priv_t *ctx)
149 {
150     vp8_remove_decoder_instances(&ctx->yv12_frame_buffers);
151
152     vpx_free(ctx);
153
154     return VPX_CODEC_OK;
155 }
156
157 static vpx_codec_err_t vp8_peek_si_internal(const uint8_t *data,
158                                             unsigned int data_sz,
159                                             vpx_codec_stream_info_t *si,
160                                             vpx_decrypt_cb decrypt_cb,
161                                             void *decrypt_state)
162 {
163     vpx_codec_err_t res = VPX_CODEC_OK;
164
165     if(data + data_sz <= data)
166     {
167         res = VPX_CODEC_INVALID_PARAM;
168     }
169     else
170     {
171         /* Parse uncompresssed part of key frame header.
172          * 3 bytes:- including version, frame type and an offset
173          * 3 bytes:- sync code (0x9d, 0x01, 0x2a)
174          * 4 bytes:- including image width and height in the lowest 14 bits
175          *           of each 2-byte value.
176          */
177         uint8_t clear_buffer[10];
178         const uint8_t *clear = data;
179         if (decrypt_cb)
180         {
181             int n = MIN(sizeof(clear_buffer), data_sz);
182             decrypt_cb(decrypt_state, data, clear_buffer, n);
183             clear = clear_buffer;
184         }
185         si->is_kf = 0;
186
187         if (data_sz >= 10 && !(clear[0] & 0x01))  /* I-Frame */
188         {
189             si->is_kf = 1;
190
191             /* vet via sync code */
192             if (clear[3] != 0x9d || clear[4] != 0x01 || clear[5] != 0x2a)
193                 res = VPX_CODEC_UNSUP_BITSTREAM;
194
195             si->w = (clear[6] | (clear[7] << 8)) & 0x3fff;
196             si->h = (clear[8] | (clear[9] << 8)) & 0x3fff;
197
198             /*printf("w=%d, h=%d\n", si->w, si->h);*/
199             if (!(si->h | si->w))
200                 res = VPX_CODEC_UNSUP_BITSTREAM;
201         }
202         else
203         {
204             res = VPX_CODEC_UNSUP_BITSTREAM;
205         }
206     }
207
208     return res;
209 }
210
211 static vpx_codec_err_t vp8_peek_si(const uint8_t *data,
212                                    unsigned int data_sz,
213                                    vpx_codec_stream_info_t *si) {
214     return vp8_peek_si_internal(data, data_sz, si, NULL, NULL);
215 }
216
217 static vpx_codec_err_t vp8_get_si(vpx_codec_alg_priv_t    *ctx,
218                                   vpx_codec_stream_info_t *si)
219 {
220
221     unsigned int sz;
222
223     if (si->sz >= sizeof(vp8_stream_info_t))
224         sz = sizeof(vp8_stream_info_t);
225     else
226         sz = sizeof(vpx_codec_stream_info_t);
227
228     memcpy(si, &ctx->si, sz);
229     si->sz = sz;
230
231     return VPX_CODEC_OK;
232 }
233
234
235 static vpx_codec_err_t
236 update_error_state(vpx_codec_alg_priv_t                 *ctx,
237                    const struct vpx_internal_error_info *error)
238 {
239     vpx_codec_err_t res;
240
241     if ((res = error->error_code))
242         ctx->base.err_detail = error->has_detail
243                                ? error->detail
244                                : NULL;
245
246     return res;
247 }
248
249 static void yuvconfig2image(vpx_image_t               *img,
250                             const YV12_BUFFER_CONFIG  *yv12,
251                             void                      *user_priv)
252 {
253     /** vpx_img_wrap() doesn't allow specifying independent strides for
254       * the Y, U, and V planes, nor other alignment adjustments that
255       * might be representable by a YV12_BUFFER_CONFIG, so we just
256       * initialize all the fields.*/
257     img->fmt = VPX_IMG_FMT_I420;
258     img->w = yv12->y_stride;
259     img->h = (yv12->y_height + 2 * VP8BORDERINPIXELS + 15) & ~15;
260     img->d_w = yv12->y_width;
261     img->d_h = yv12->y_height;
262     img->x_chroma_shift = 1;
263     img->y_chroma_shift = 1;
264     img->planes[VPX_PLANE_Y] = yv12->y_buffer;
265     img->planes[VPX_PLANE_U] = yv12->u_buffer;
266     img->planes[VPX_PLANE_V] = yv12->v_buffer;
267     img->planes[VPX_PLANE_ALPHA] = NULL;
268     img->stride[VPX_PLANE_Y] = yv12->y_stride;
269     img->stride[VPX_PLANE_U] = yv12->uv_stride;
270     img->stride[VPX_PLANE_V] = yv12->uv_stride;
271     img->stride[VPX_PLANE_ALPHA] = yv12->y_stride;
272     img->bit_depth = 8;
273     img->bps = 12;
274     img->user_priv = user_priv;
275     img->img_data = yv12->buffer_alloc;
276     img->img_data_owner = 0;
277     img->self_allocd = 0;
278 }
279
280 static int
281 update_fragments(vpx_codec_alg_priv_t  *ctx,
282                  const uint8_t         *data,
283                  unsigned int           data_sz,
284                  vpx_codec_err_t       *res)
285 {
286     *res = VPX_CODEC_OK;
287
288     if (ctx->fragments.count == 0)
289     {
290         /* New frame, reset fragment pointers and sizes */
291         vpx_memset((void*)ctx->fragments.ptrs, 0, sizeof(ctx->fragments.ptrs));
292         vpx_memset(ctx->fragments.sizes, 0, sizeof(ctx->fragments.sizes));
293     }
294     if (ctx->fragments.enabled && !(data == NULL && data_sz == 0))
295     {
296         /* Store a pointer to this fragment and return. We haven't
297          * received the complete frame yet, so we will wait with decoding.
298          */
299         ctx->fragments.ptrs[ctx->fragments.count] = data;
300         ctx->fragments.sizes[ctx->fragments.count] = data_sz;
301         ctx->fragments.count++;
302         if (ctx->fragments.count > (1 << EIGHT_PARTITION) + 1)
303         {
304             ctx->fragments.count = 0;
305             *res = VPX_CODEC_INVALID_PARAM;
306             return -1;
307         }
308         return 0;
309     }
310
311     if (!ctx->fragments.enabled)
312     {
313         ctx->fragments.ptrs[0] = data;
314         ctx->fragments.sizes[0] = data_sz;
315         ctx->fragments.count = 1;
316     }
317
318     return 1;
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     unsigned int resolution_change = 0;
329     unsigned int w, h;
330
331
332     /* Update the input fragment data */
333     if(update_fragments(ctx, data, data_sz, &res) <= 0)
334         return res;
335
336     /* Determine the stream parameters. Note that we rely on peek_si to
337      * validate that we have a buffer that does not wrap around the top
338      * of the heap.
339      */
340     w = ctx->si.w;
341     h = ctx->si.h;
342
343     res = vp8_peek_si_internal(ctx->fragments.ptrs[0], ctx->fragments.sizes[0],
344                                &ctx->si, ctx->decrypt_cb, ctx->decrypt_state);
345
346     if((res == VPX_CODEC_UNSUP_BITSTREAM) && !ctx->si.is_kf)
347     {
348         /* the peek function returns an error for non keyframes, however for
349          * this case, it is not an error */
350         res = VPX_CODEC_OK;
351     }
352
353     if(!ctx->decoder_init && !ctx->si.is_kf)
354         res = VPX_CODEC_UNSUP_BITSTREAM;
355
356     if ((ctx->si.h != h) || (ctx->si.w != w))
357         resolution_change = 1;
358
359     /* Initialize the decoder instance on the first frame*/
360     if (!res && !ctx->decoder_init)
361     {
362       VP8D_CONFIG oxcf;
363
364       oxcf.Width = ctx->si.w;
365       oxcf.Height = ctx->si.h;
366       oxcf.Version = 9;
367       oxcf.postprocess = 0;
368       oxcf.max_threads = ctx->cfg.threads;
369       oxcf.error_concealment =
370           (ctx->base.init_flags & VPX_CODEC_USE_ERROR_CONCEALMENT);
371
372       /* If postprocessing was enabled by the application and a
373        * configuration has not been provided, default it.
374        */
375        if (!ctx->postproc_cfg_set
376            && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)) {
377          ctx->postproc_cfg.post_proc_flag =
378              VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE;
379          ctx->postproc_cfg.deblocking_level = 4;
380          ctx->postproc_cfg.noise_level = 0;
381        }
382
383        res = vp8_create_decoder_instances(&ctx->yv12_frame_buffers, &oxcf);
384        ctx->decoder_init = 1;
385     }
386
387     /* Set these even if already initialized.  The caller may have changed the
388      * decrypt config between frames.
389      */
390     if (ctx->decoder_init) {
391       ctx->yv12_frame_buffers.pbi[0]->decrypt_cb = ctx->decrypt_cb;
392       ctx->yv12_frame_buffers.pbi[0]->decrypt_state = ctx->decrypt_state;
393     }
394
395     if (!res)
396     {
397         VP8D_COMP *pbi = ctx->yv12_frame_buffers.pbi[0];
398         if(resolution_change)
399         {
400             VP8_COMMON *const pc = & pbi->common;
401             MACROBLOCKD *const xd  = & pbi->mb;
402 #if CONFIG_MULTITHREAD
403             int i;
404 #endif
405             pc->Width = ctx->si.w;
406             pc->Height = ctx->si.h;
407             {
408                 int prev_mb_rows = pc->mb_rows;
409
410                 if (setjmp(pbi->common.error.jmp))
411                 {
412                     pbi->common.error.setjmp = 0;
413                     vp8_clear_system_state();
414                     /* same return value as used in vp8dx_receive_compressed_data */
415                     return -1;
416                 }
417
418                 pbi->common.error.setjmp = 1;
419
420                 if (pc->Width <= 0)
421                 {
422                     pc->Width = w;
423                     vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
424                                        "Invalid frame width");
425                 }
426
427                 if (pc->Height <= 0)
428                 {
429                     pc->Height = h;
430                     vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
431                                        "Invalid frame height");
432                 }
433
434                 if (vp8_alloc_frame_buffers(pc, pc->Width, pc->Height))
435                     vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
436                                        "Failed to allocate frame buffers");
437
438                 xd->pre = pc->yv12_fb[pc->lst_fb_idx];
439                 xd->dst = pc->yv12_fb[pc->new_fb_idx];
440
441 #if CONFIG_MULTITHREAD
442                 for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
443                 {
444                     pbi->mb_row_di[i].mbd.dst = pc->yv12_fb[pc->new_fb_idx];
445                     vp8_build_block_doffsets(&pbi->mb_row_di[i].mbd);
446                 }
447 #endif
448                 vp8_build_block_doffsets(&pbi->mb);
449
450                 /* allocate memory for last frame MODE_INFO array */
451 #if CONFIG_ERROR_CONCEALMENT
452
453                 if (pbi->ec_enabled)
454                 {
455                     /* old prev_mip was released by vp8_de_alloc_frame_buffers()
456                      * called in vp8_alloc_frame_buffers() */
457                     pc->prev_mip = vpx_calloc(
458                                        (pc->mb_cols + 1) * (pc->mb_rows + 1),
459                                        sizeof(MODE_INFO));
460
461                     if (!pc->prev_mip)
462                     {
463                         vp8_de_alloc_frame_buffers(pc);
464                         vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
465                                            "Failed to allocate"
466                                            "last frame MODE_INFO array");
467                     }
468
469                     pc->prev_mi = pc->prev_mip + pc->mode_info_stride + 1;
470
471                     if (vp8_alloc_overlap_lists(pbi))
472                         vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
473                                            "Failed to allocate overlap lists "
474                                            "for error concealment");
475                 }
476
477 #endif
478
479 #if CONFIG_MULTITHREAD
480                 if (pbi->b_multithreaded_rd)
481                     vp8mt_alloc_temp_buffers(pbi, pc->Width, prev_mb_rows);
482 #else
483                 (void)prev_mb_rows;
484 #endif
485             }
486
487             pbi->common.error.setjmp = 0;
488
489             /* required to get past the first get_free_fb() call */
490             pbi->common.fb_idx_ref_cnt[0] = 0;
491         }
492
493         /* update the pbi fragment data */
494         pbi->fragments = ctx->fragments;
495
496         ctx->user_priv = user_priv;
497         if (vp8dx_receive_compressed_data(pbi, data_sz, data, deadline))
498         {
499             res = update_error_state(ctx, &pbi->common.error);
500         }
501
502         /* get ready for the next series of fragments */
503         ctx->fragments.count = 0;
504     }
505
506     return res;
507 }
508
509 static vpx_image_t *vp8_get_frame(vpx_codec_alg_priv_t  *ctx,
510                                   vpx_codec_iter_t      *iter)
511 {
512     vpx_image_t *img = NULL;
513
514     /* iter acts as a flip flop, so an image is only returned on the first
515      * call to get_frame.
516      */
517     if (!(*iter) && ctx->yv12_frame_buffers.pbi[0])
518     {
519         YV12_BUFFER_CONFIG sd;
520         int64_t time_stamp = 0, time_end_stamp = 0;
521         vp8_ppflags_t flags = {0};
522
523         if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
524         {
525             flags.post_proc_flag= ctx->postproc_cfg.post_proc_flag
526 #if CONFIG_POSTPROC_VISUALIZER
527
528                                 | ((ctx->dbg_color_ref_frame_flag != 0) ? VP8D_DEBUG_CLR_FRM_REF_BLKS : 0)
529                                 | ((ctx->dbg_color_mb_modes_flag != 0) ? VP8D_DEBUG_CLR_BLK_MODES : 0)
530                                 | ((ctx->dbg_color_b_modes_flag != 0) ? VP8D_DEBUG_CLR_BLK_MODES : 0)
531                                 | ((ctx->dbg_display_mv_flag != 0) ? VP8D_DEBUG_DRAW_MV : 0)
532 #endif
533                                 ;
534             flags.deblocking_level      = ctx->postproc_cfg.deblocking_level;
535             flags.noise_level           = ctx->postproc_cfg.noise_level;
536 #if CONFIG_POSTPROC_VISUALIZER
537             flags.display_ref_frame_flag= ctx->dbg_color_ref_frame_flag;
538             flags.display_mb_modes_flag = ctx->dbg_color_mb_modes_flag;
539             flags.display_b_modes_flag  = ctx->dbg_color_b_modes_flag;
540             flags.display_mv_flag       = ctx->dbg_display_mv_flag;
541 #endif
542         }
543
544         if (0 == vp8dx_get_raw_frame(ctx->yv12_frame_buffers.pbi[0], &sd,
545                                      &time_stamp, &time_end_stamp, &flags))
546         {
547             yuvconfig2image(&ctx->img, &sd, ctx->user_priv);
548
549             img = &ctx->img;
550             *iter = img;
551         }
552     }
553
554     return img;
555 }
556
557 static vpx_codec_err_t image2yuvconfig(const vpx_image_t   *img,
558                                        YV12_BUFFER_CONFIG  *yv12)
559 {
560     vpx_codec_err_t        res = VPX_CODEC_OK;
561     yv12->y_buffer = img->planes[VPX_PLANE_Y];
562     yv12->u_buffer = img->planes[VPX_PLANE_U];
563     yv12->v_buffer = img->planes[VPX_PLANE_V];
564
565     yv12->y_crop_width  = img->d_w;
566     yv12->y_crop_height = img->d_h;
567     yv12->y_width  = img->d_w;
568     yv12->y_height = img->d_h;
569     yv12->uv_width = yv12->y_width / 2;
570     yv12->uv_height = yv12->y_height / 2;
571
572     yv12->y_stride = img->stride[VPX_PLANE_Y];
573     yv12->uv_stride = img->stride[VPX_PLANE_U];
574
575     yv12->border  = (img->stride[VPX_PLANE_Y] - img->d_w) / 2;
576     return res;
577 }
578
579
580 static vpx_codec_err_t vp8_set_reference(vpx_codec_alg_priv_t *ctx,
581                                          va_list args)
582 {
583
584     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
585
586     if (data && !ctx->yv12_frame_buffers.use_frame_threads)
587     {
588         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
589         YV12_BUFFER_CONFIG sd;
590
591         image2yuvconfig(&frame->img, &sd);
592
593         return vp8dx_set_reference(ctx->yv12_frame_buffers.pbi[0],
594                                    frame->frame_type, &sd);
595     }
596     else
597         return VPX_CODEC_INVALID_PARAM;
598
599 }
600
601 static vpx_codec_err_t vp8_get_reference(vpx_codec_alg_priv_t *ctx,
602                                          va_list args)
603 {
604
605     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
606
607     if (data && !ctx->yv12_frame_buffers.use_frame_threads)
608     {
609         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
610         YV12_BUFFER_CONFIG sd;
611
612         image2yuvconfig(&frame->img, &sd);
613
614         return vp8dx_get_reference(ctx->yv12_frame_buffers.pbi[0],
615                                    frame->frame_type, &sd);
616     }
617     else
618         return VPX_CODEC_INVALID_PARAM;
619
620 }
621
622 static vpx_codec_err_t vp8_set_postproc(vpx_codec_alg_priv_t *ctx,
623                                         va_list args)
624 {
625 #if CONFIG_POSTPROC
626     vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
627
628     if (data)
629     {
630         ctx->postproc_cfg_set = 1;
631         ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
632         return VPX_CODEC_OK;
633     }
634     else
635         return VPX_CODEC_INVALID_PARAM;
636
637 #else
638     return VPX_CODEC_INCAPABLE;
639 #endif
640 }
641
642
643 static vpx_codec_err_t vp8_set_dbg_color_ref_frame(vpx_codec_alg_priv_t *ctx,
644                                                    va_list args) {
645 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
646   ctx->dbg_color_ref_frame_flag = va_arg(args, int);
647   return VPX_CODEC_OK;
648 #else
649   (void)ctx;
650   (void)args;
651   return VPX_CODEC_INCAPABLE;
652 #endif
653 }
654
655 static vpx_codec_err_t vp8_set_dbg_color_mb_modes(vpx_codec_alg_priv_t *ctx,
656                                                   va_list args) {
657 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
658   ctx->dbg_color_mb_modes_flag = va_arg(args, int);
659   return VPX_CODEC_OK;
660 #else
661   (void)ctx;
662   (void)args;
663   return VPX_CODEC_INCAPABLE;
664 #endif
665 }
666
667 static vpx_codec_err_t vp8_set_dbg_color_b_modes(vpx_codec_alg_priv_t *ctx,
668                                                  va_list args) {
669 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
670   ctx->dbg_color_b_modes_flag = va_arg(args, int);
671   return VPX_CODEC_OK;
672 #else
673   (void)ctx;
674   (void)args;
675   return VPX_CODEC_INCAPABLE;
676 #endif
677 }
678
679 static vpx_codec_err_t vp8_set_dbg_display_mv(vpx_codec_alg_priv_t *ctx,
680                                               va_list args) {
681 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
682   ctx->dbg_display_mv_flag = va_arg(args, int);
683   return VPX_CODEC_OK;
684 #else
685   (void)ctx;
686   (void)args;
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                                                 va_list args)
693 {
694     int *update_info = va_arg(args, int *);
695
696     if (update_info && !ctx->yv12_frame_buffers.use_frame_threads)
697     {
698         VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
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 extern int vp8dx_references_buffer( VP8_COMMON *oci, int ref_frame );
711 static vpx_codec_err_t vp8_get_last_ref_frame(vpx_codec_alg_priv_t *ctx,
712                                               va_list args)
713 {
714     int *ref_info = va_arg(args, int *);
715
716     if (ref_info && !ctx->yv12_frame_buffers.use_frame_threads)
717     {
718         VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
719         VP8_COMMON *oci = &pbi->common;
720         *ref_info =
721             (vp8dx_references_buffer( oci, ALTREF_FRAME )?VP8_ALTR_FRAME:0) |
722             (vp8dx_references_buffer( oci, GOLDEN_FRAME )?VP8_GOLD_FRAME:0) |
723             (vp8dx_references_buffer( oci, LAST_FRAME )?VP8_LAST_FRAME:0);
724
725         return VPX_CODEC_OK;
726     }
727     else
728         return VPX_CODEC_INVALID_PARAM;
729 }
730
731 static vpx_codec_err_t vp8_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
732                                                va_list args)
733 {
734
735     int *corrupted = va_arg(args, int *);
736     VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
737
738     if (corrupted && pbi)
739     {
740         *corrupted = pbi->common.frame_to_show->corrupted;
741
742         return VPX_CODEC_OK;
743     }
744     else
745         return VPX_CODEC_INVALID_PARAM;
746
747 }
748
749 static vpx_codec_err_t vp8_set_decryptor(vpx_codec_alg_priv_t *ctx,
750                                          va_list args)
751 {
752     vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
753
754     if (init)
755     {
756         ctx->decrypt_cb = init->decrypt_cb;
757         ctx->decrypt_state = init->decrypt_state;
758     }
759     else
760     {
761         ctx->decrypt_cb = NULL;
762         ctx->decrypt_state = NULL;
763     }
764     return VPX_CODEC_OK;
765 }
766
767 vpx_codec_ctrl_fn_map_t vp8_ctf_maps[] =
768 {
769     {VP8_SET_REFERENCE,             vp8_set_reference},
770     {VP8_COPY_REFERENCE,            vp8_get_reference},
771     {VP8_SET_POSTPROC,              vp8_set_postproc},
772     {VP8_SET_DBG_COLOR_REF_FRAME,   vp8_set_dbg_color_ref_frame},
773     {VP8_SET_DBG_COLOR_MB_MODES,    vp8_set_dbg_color_mb_modes},
774     {VP8_SET_DBG_COLOR_B_MODES,     vp8_set_dbg_color_b_modes},
775     {VP8_SET_DBG_DISPLAY_MV,        vp8_set_dbg_display_mv},
776     {VP8D_GET_LAST_REF_UPDATES,     vp8_get_last_ref_updates},
777     {VP8D_GET_FRAME_CORRUPTED,      vp8_get_frame_corrupted},
778     {VP8D_GET_LAST_REF_USED,        vp8_get_last_ref_frame},
779     {VPXD_SET_DECRYPTOR,            vp8_set_decryptor},
780     { -1, NULL},
781 };
782
783
784 #ifndef VERSION_STRING
785 #define VERSION_STRING
786 #endif
787 CODEC_INTERFACE(vpx_codec_vp8_dx) =
788 {
789     "WebM Project VP8 Decoder" VERSION_STRING,
790     VPX_CODEC_INTERNAL_ABI_VERSION,
791     VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC | VP8_CAP_ERROR_CONCEALMENT |
792     VPX_CODEC_CAP_INPUT_FRAGMENTS,
793     /* vpx_codec_caps_t          caps; */
794     vp8_init,         /* vpx_codec_init_fn_t       init; */
795     vp8_destroy,      /* vpx_codec_destroy_fn_t    destroy; */
796     vp8_ctf_maps,     /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
797     NOT_IMPLEMENTED,  /* vpx_codec_get_mmap_fn_t   get_mmap; */
798     NOT_IMPLEMENTED,  /* vpx_codec_set_mmap_fn_t   set_mmap; */
799     {
800         vp8_peek_si,      /* vpx_codec_peek_si_fn_t    peek_si; */
801         vp8_get_si,       /* vpx_codec_get_si_fn_t     get_si; */
802         vp8_decode,       /* vpx_codec_decode_fn_t     decode; */
803         vp8_get_frame,    /* vpx_codec_frame_get_fn_t  frame_get; */
804         NOT_IMPLEMENTED,
805     },
806     { /* encoder functions */
807         NOT_IMPLEMENTED,
808         NOT_IMPLEMENTED,
809         NOT_IMPLEMENTED,
810         NOT_IMPLEMENTED,
811         NOT_IMPLEMENTED,
812         NOT_IMPLEMENTED
813     }
814 };