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