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