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