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