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