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