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