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 
33 typedef vpx_codec_stream_info_t  vp8_stream_info_t;
34 
35 /* Structures for handling memory allocations */
36 typedef 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 
43 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t);
44 
45 struct 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 
vp8_priv_sz(const vpx_codec_dec_cfg_t * si,vpx_codec_flags_t flags)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     return sizeof(vpx_codec_alg_priv_t);
79 }
80 
vp8_init_ctx(vpx_codec_ctx_t * ctx)81 static void vp8_init_ctx(vpx_codec_ctx_t *ctx)
82 {
83     ctx->priv =
84         (vpx_codec_priv_t *)vpx_memalign(8, sizeof(vpx_codec_alg_priv_t));
85     vpx_memset(ctx->priv, 0, sizeof(vpx_codec_alg_priv_t));
86     ctx->priv->sz = sizeof(*ctx->priv);
87     ctx->priv->iface = ctx->iface;
88     ctx->priv->alg_priv = (vpx_codec_alg_priv_t *)ctx->priv;
89     ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si);
90     ctx->priv->alg_priv->decrypt_cb = NULL;
91     ctx->priv->alg_priv->decrypt_state = NULL;
92     ctx->priv->alg_priv->flushed = 0;
93     ctx->priv->init_flags = ctx->init_flags;
94 
95     if (ctx->config.dec)
96     {
97         /* Update the reference to the config structure to an internal copy. */
98         ctx->priv->alg_priv->cfg = *ctx->config.dec;
99         ctx->config.dec = &ctx->priv->alg_priv->cfg;
100     }
101 }
102 
vp8_init(vpx_codec_ctx_t * ctx,vpx_codec_priv_enc_mr_cfg_t * data)103 static vpx_codec_err_t vp8_init(vpx_codec_ctx_t *ctx,
104                                 vpx_codec_priv_enc_mr_cfg_t *data)
105 {
106     vpx_codec_err_t        res = VPX_CODEC_OK;
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 
119         /* initialize number of fragments to zero */
120         ctx->priv->alg_priv->fragments.count = 0;
121         /* is input fragments enabled? */
122         ctx->priv->alg_priv->fragments.enabled =
123                 (ctx->priv->alg_priv->base.init_flags &
124                     VPX_CODEC_USE_INPUT_FRAGMENTS);
125 
126         /*post processing level initialized to do nothing */
127     }
128 
129     ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads =
130             (ctx->priv->alg_priv->base.init_flags &
131                     VPX_CODEC_USE_FRAME_THREADING);
132 
133     /* for now, disable frame threading */
134     ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads = 0;
135 
136     if(ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads &&
137             (( ctx->priv->alg_priv->base.init_flags &
138                             VPX_CODEC_USE_ERROR_CONCEALMENT)
139                     || ( ctx->priv->alg_priv->base.init_flags &
140                             VPX_CODEC_USE_INPUT_FRAGMENTS) ) )
141     {
142         /* row-based threading, error concealment, and input fragments will
143          * not be supported when using frame-based threading */
144         res = VPX_CODEC_INVALID_PARAM;
145     }
146 
147     return res;
148 }
149 
vp8_destroy(vpx_codec_alg_priv_t * ctx)150 static vpx_codec_err_t vp8_destroy(vpx_codec_alg_priv_t *ctx)
151 {
152     vp8_remove_decoder_instances(&ctx->yv12_frame_buffers);
153 
154     vpx_free(ctx);
155 
156     return VPX_CODEC_OK;
157 }
158 
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)159 static vpx_codec_err_t vp8_peek_si_internal(const uint8_t *data,
160                                             unsigned int data_sz,
161                                             vpx_codec_stream_info_t *si,
162                                             vpx_decrypt_cb decrypt_cb,
163                                             void *decrypt_state)
164 {
165     vpx_codec_err_t res = VPX_CODEC_OK;
166 
167     if(data + data_sz <= data)
168     {
169         res = VPX_CODEC_INVALID_PARAM;
170     }
171     else
172     {
173         /* Parse uncompresssed part of key frame header.
174          * 3 bytes:- including version, frame type and an offset
175          * 3 bytes:- sync code (0x9d, 0x01, 0x2a)
176          * 4 bytes:- including image width and height in the lowest 14 bits
177          *           of each 2-byte value.
178          */
179         uint8_t clear_buffer[10];
180         const uint8_t *clear = data;
181         if (decrypt_cb)
182         {
183             int n = MIN(sizeof(clear_buffer), data_sz);
184             decrypt_cb(decrypt_state, data, clear_buffer, n);
185             clear = clear_buffer;
186         }
187         si->is_kf = 0;
188 
189         if (data_sz >= 10 && !(clear[0] & 0x01))  /* I-Frame */
190         {
191             si->is_kf = 1;
192 
193             /* vet via sync code */
194             if (clear[3] != 0x9d || clear[4] != 0x01 || clear[5] != 0x2a)
195                 res = VPX_CODEC_UNSUP_BITSTREAM;
196 
197             si->w = (clear[6] | (clear[7] << 8)) & 0x3fff;
198             si->h = (clear[8] | (clear[9] << 8)) & 0x3fff;
199 
200             /*printf("w=%d, h=%d\n", si->w, si->h);*/
201             if (!(si->h | si->w))
202                 res = VPX_CODEC_UNSUP_BITSTREAM;
203         }
204         else
205         {
206             res = VPX_CODEC_UNSUP_BITSTREAM;
207         }
208     }
209 
210     return res;
211 }
212 
vp8_peek_si(const uint8_t * data,unsigned int data_sz,vpx_codec_stream_info_t * si)213 static vpx_codec_err_t vp8_peek_si(const uint8_t *data,
214                                    unsigned int data_sz,
215                                    vpx_codec_stream_info_t *si) {
216     return vp8_peek_si_internal(data, data_sz, si, NULL, NULL);
217 }
218 
vp8_get_si(vpx_codec_alg_priv_t * ctx,vpx_codec_stream_info_t * si)219 static vpx_codec_err_t vp8_get_si(vpx_codec_alg_priv_t    *ctx,
220                                   vpx_codec_stream_info_t *si)
221 {
222 
223     unsigned int sz;
224 
225     if (si->sz >= sizeof(vp8_stream_info_t))
226         sz = sizeof(vp8_stream_info_t);
227     else
228         sz = sizeof(vpx_codec_stream_info_t);
229 
230     memcpy(si, &ctx->si, sz);
231     si->sz = sz;
232 
233     return VPX_CODEC_OK;
234 }
235 
236 
237 static vpx_codec_err_t
update_error_state(vpx_codec_alg_priv_t * ctx,const struct vpx_internal_error_info * error)238 update_error_state(vpx_codec_alg_priv_t                 *ctx,
239                    const struct vpx_internal_error_info *error)
240 {
241     vpx_codec_err_t res;
242 
243     if ((res = error->error_code))
244         ctx->base.err_detail = error->has_detail
245                                ? error->detail
246                                : NULL;
247 
248     return res;
249 }
250 
yuvconfig2image(vpx_image_t * img,const YV12_BUFFER_CONFIG * yv12,void * user_priv)251 static void yuvconfig2image(vpx_image_t               *img,
252                             const YV12_BUFFER_CONFIG  *yv12,
253                             void                      *user_priv)
254 {
255     /** vpx_img_wrap() doesn't allow specifying independent strides for
256       * the Y, U, and V planes, nor other alignment adjustments that
257       * might be representable by a YV12_BUFFER_CONFIG, so we just
258       * initialize all the fields.*/
259     img->fmt = VPX_IMG_FMT_I420;
260     img->w = yv12->y_stride;
261     img->h = (yv12->y_height + 2 * VP8BORDERINPIXELS + 15) & ~15;
262     img->d_w = yv12->y_width;
263     img->d_h = yv12->y_height;
264     img->x_chroma_shift = 1;
265     img->y_chroma_shift = 1;
266     img->planes[VPX_PLANE_Y] = yv12->y_buffer;
267     img->planes[VPX_PLANE_U] = yv12->u_buffer;
268     img->planes[VPX_PLANE_V] = yv12->v_buffer;
269     img->planes[VPX_PLANE_ALPHA] = NULL;
270     img->stride[VPX_PLANE_Y] = yv12->y_stride;
271     img->stride[VPX_PLANE_U] = yv12->uv_stride;
272     img->stride[VPX_PLANE_V] = yv12->uv_stride;
273     img->stride[VPX_PLANE_ALPHA] = yv12->y_stride;
274     img->bit_depth = 8;
275     img->bps = 12;
276     img->user_priv = user_priv;
277     img->img_data = yv12->buffer_alloc;
278     img->img_data_owner = 0;
279     img->self_allocd = 0;
280 }
281 
282 static int
update_fragments(vpx_codec_alg_priv_t * ctx,const uint8_t * data,unsigned int data_sz,vpx_codec_err_t * res)283 update_fragments(vpx_codec_alg_priv_t  *ctx,
284                  const uint8_t         *data,
285                  unsigned int           data_sz,
286                  vpx_codec_err_t       *res)
287 {
288     *res = VPX_CODEC_OK;
289 
290     if (ctx->fragments.count == 0)
291     {
292         /* New frame, reset fragment pointers and sizes */
293         vpx_memset((void*)ctx->fragments.ptrs, 0, sizeof(ctx->fragments.ptrs));
294         vpx_memset(ctx->fragments.sizes, 0, sizeof(ctx->fragments.sizes));
295     }
296     if (ctx->fragments.enabled && !(data == NULL && data_sz == 0))
297     {
298         /* Store a pointer to this fragment and return. We haven't
299          * received the complete frame yet, so we will wait with decoding.
300          */
301         ctx->fragments.ptrs[ctx->fragments.count] = data;
302         ctx->fragments.sizes[ctx->fragments.count] = data_sz;
303         ctx->fragments.count++;
304         if (ctx->fragments.count > (1 << EIGHT_PARTITION) + 1)
305         {
306             ctx->fragments.count = 0;
307             *res = VPX_CODEC_INVALID_PARAM;
308             return -1;
309         }
310         return 0;
311     }
312 
313     if (!ctx->fragments.enabled)
314     {
315         ctx->fragments.ptrs[0] = data;
316         ctx->fragments.sizes[0] = data_sz;
317         ctx->fragments.count = 1;
318     }
319 
320     return 1;
321 }
322 
vp8_decode(vpx_codec_alg_priv_t * ctx,const uint8_t * data,unsigned int data_sz,void * user_priv,long deadline)323 static vpx_codec_err_t vp8_decode(vpx_codec_alg_priv_t  *ctx,
324                                   const uint8_t         *data,
325                                   unsigned int            data_sz,
326                                   void                    *user_priv,
327                                   long                    deadline)
328 {
329     vpx_codec_err_t res = VPX_CODEC_OK;
330     unsigned int resolution_change = 0;
331     unsigned int w, h;
332 
333     if (data == NULL && data_sz == 0) {
334       ctx->flushed = 1;
335       return VPX_CODEC_OK;
336     }
337 
338     /* Reset flushed when receiving a valid frame */
339     ctx->flushed = 0;
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 
vp8_get_frame(vpx_codec_alg_priv_t * ctx,vpx_codec_iter_t * iter)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 
image2yuvconfig(const vpx_image_t * img,YV12_BUFFER_CONFIG * yv12)566 static vpx_codec_err_t image2yuvconfig(const vpx_image_t   *img,
567                                        YV12_BUFFER_CONFIG  *yv12)
568 {
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  = img->d_w;
575     yv12->y_crop_height = img->d_h;
576     yv12->y_width  = img->d_w;
577     yv12->y_height = img->d_h;
578     yv12->uv_width = yv12->y_width / 2;
579     yv12->uv_height = yv12->y_height / 2;
580 
581     yv12->y_stride = img->stride[VPX_PLANE_Y];
582     yv12->uv_stride = img->stride[VPX_PLANE_U];
583 
584     yv12->border  = (img->stride[VPX_PLANE_Y] - img->d_w) / 2;
585     return res;
586 }
587 
588 
vp8_set_reference(vpx_codec_alg_priv_t * ctx,va_list args)589 static vpx_codec_err_t vp8_set_reference(vpx_codec_alg_priv_t *ctx,
590                                          va_list args)
591 {
592 
593     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
594 
595     if (data && !ctx->yv12_frame_buffers.use_frame_threads)
596     {
597         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
598         YV12_BUFFER_CONFIG sd;
599 
600         image2yuvconfig(&frame->img, &sd);
601 
602         return vp8dx_set_reference(ctx->yv12_frame_buffers.pbi[0],
603                                    frame->frame_type, &sd);
604     }
605     else
606         return VPX_CODEC_INVALID_PARAM;
607 
608 }
609 
vp8_get_reference(vpx_codec_alg_priv_t * ctx,va_list args)610 static vpx_codec_err_t vp8_get_reference(vpx_codec_alg_priv_t *ctx,
611                                          va_list args)
612 {
613 
614     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
615 
616     if (data && !ctx->yv12_frame_buffers.use_frame_threads)
617     {
618         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
619         YV12_BUFFER_CONFIG sd;
620 
621         image2yuvconfig(&frame->img, &sd);
622 
623         return vp8dx_get_reference(ctx->yv12_frame_buffers.pbi[0],
624                                    frame->frame_type, &sd);
625     }
626     else
627         return VPX_CODEC_INVALID_PARAM;
628 
629 }
630 
vp8_set_postproc(vpx_codec_alg_priv_t * ctx,va_list args)631 static vpx_codec_err_t vp8_set_postproc(vpx_codec_alg_priv_t *ctx,
632                                         va_list args)
633 {
634 #if CONFIG_POSTPROC
635     vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
636 
637     if (data)
638     {
639         ctx->postproc_cfg_set = 1;
640         ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
641         return VPX_CODEC_OK;
642     }
643     else
644         return VPX_CODEC_INVALID_PARAM;
645 
646 #else
647     return VPX_CODEC_INCAPABLE;
648 #endif
649 }
650 
651 
vp8_set_dbg_color_ref_frame(vpx_codec_alg_priv_t * ctx,va_list args)652 static vpx_codec_err_t vp8_set_dbg_color_ref_frame(vpx_codec_alg_priv_t *ctx,
653                                                    va_list args) {
654 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
655   ctx->dbg_color_ref_frame_flag = va_arg(args, int);
656   return VPX_CODEC_OK;
657 #else
658   (void)ctx;
659   (void)args;
660   return VPX_CODEC_INCAPABLE;
661 #endif
662 }
663 
vp8_set_dbg_color_mb_modes(vpx_codec_alg_priv_t * ctx,va_list args)664 static vpx_codec_err_t vp8_set_dbg_color_mb_modes(vpx_codec_alg_priv_t *ctx,
665                                                   va_list args) {
666 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
667   ctx->dbg_color_mb_modes_flag = va_arg(args, int);
668   return VPX_CODEC_OK;
669 #else
670   (void)ctx;
671   (void)args;
672   return VPX_CODEC_INCAPABLE;
673 #endif
674 }
675 
vp8_set_dbg_color_b_modes(vpx_codec_alg_priv_t * ctx,va_list args)676 static vpx_codec_err_t vp8_set_dbg_color_b_modes(vpx_codec_alg_priv_t *ctx,
677                                                  va_list args) {
678 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
679   ctx->dbg_color_b_modes_flag = va_arg(args, int);
680   return VPX_CODEC_OK;
681 #else
682   (void)ctx;
683   (void)args;
684   return VPX_CODEC_INCAPABLE;
685 #endif
686 }
687 
vp8_set_dbg_display_mv(vpx_codec_alg_priv_t * ctx,va_list args)688 static vpx_codec_err_t vp8_set_dbg_display_mv(vpx_codec_alg_priv_t *ctx,
689                                               va_list args) {
690 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
691   ctx->dbg_display_mv_flag = va_arg(args, int);
692   return VPX_CODEC_OK;
693 #else
694   (void)ctx;
695   (void)args;
696   return VPX_CODEC_INCAPABLE;
697 #endif
698 }
699 
vp8_get_last_ref_updates(vpx_codec_alg_priv_t * ctx,va_list args)700 static vpx_codec_err_t vp8_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
701                                                 va_list args)
702 {
703     int *update_info = va_arg(args, int *);
704 
705     if (update_info && !ctx->yv12_frame_buffers.use_frame_threads)
706     {
707         VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
708 
709         *update_info = pbi->common.refresh_alt_ref_frame * (int) VP8_ALTR_FRAME
710             + pbi->common.refresh_golden_frame * (int) VP8_GOLD_FRAME
711             + pbi->common.refresh_last_frame * (int) VP8_LAST_FRAME;
712 
713         return VPX_CODEC_OK;
714     }
715     else
716         return VPX_CODEC_INVALID_PARAM;
717 }
718 
719 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)720 static vpx_codec_err_t vp8_get_last_ref_frame(vpx_codec_alg_priv_t *ctx,
721                                               va_list args)
722 {
723     int *ref_info = va_arg(args, int *);
724 
725     if (ref_info && !ctx->yv12_frame_buffers.use_frame_threads)
726     {
727         VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
728         VP8_COMMON *oci = &pbi->common;
729         *ref_info =
730             (vp8dx_references_buffer( oci, ALTREF_FRAME )?VP8_ALTR_FRAME:0) |
731             (vp8dx_references_buffer( oci, GOLDEN_FRAME )?VP8_GOLD_FRAME:0) |
732             (vp8dx_references_buffer( oci, LAST_FRAME )?VP8_LAST_FRAME:0);
733 
734         return VPX_CODEC_OK;
735     }
736     else
737         return VPX_CODEC_INVALID_PARAM;
738 }
739 
vp8_get_frame_corrupted(vpx_codec_alg_priv_t * ctx,va_list args)740 static vpx_codec_err_t vp8_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
741                                                va_list args)
742 {
743 
744     int *corrupted = va_arg(args, int *);
745     VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
746 
747     if (corrupted && pbi)
748     {
749         const YV12_BUFFER_CONFIG *const frame = pbi->common.frame_to_show;
750         if (frame == NULL) return VPX_CODEC_ERROR;
751         *corrupted = frame->corrupted;
752         return VPX_CODEC_OK;
753     }
754     else
755         return VPX_CODEC_INVALID_PARAM;
756 
757 }
758 
vp8_set_decryptor(vpx_codec_alg_priv_t * ctx,va_list args)759 static vpx_codec_err_t vp8_set_decryptor(vpx_codec_alg_priv_t *ctx,
760                                          va_list args)
761 {
762     vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
763 
764     if (init)
765     {
766         ctx->decrypt_cb = init->decrypt_cb;
767         ctx->decrypt_state = init->decrypt_state;
768     }
769     else
770     {
771         ctx->decrypt_cb = NULL;
772         ctx->decrypt_state = NULL;
773     }
774     return VPX_CODEC_OK;
775 }
776 
777 vpx_codec_ctrl_fn_map_t vp8_ctf_maps[] =
778 {
779     {VP8_SET_REFERENCE,             vp8_set_reference},
780     {VP8_COPY_REFERENCE,            vp8_get_reference},
781     {VP8_SET_POSTPROC,              vp8_set_postproc},
782     {VP8_SET_DBG_COLOR_REF_FRAME,   vp8_set_dbg_color_ref_frame},
783     {VP8_SET_DBG_COLOR_MB_MODES,    vp8_set_dbg_color_mb_modes},
784     {VP8_SET_DBG_COLOR_B_MODES,     vp8_set_dbg_color_b_modes},
785     {VP8_SET_DBG_DISPLAY_MV,        vp8_set_dbg_display_mv},
786     {VP8D_GET_LAST_REF_UPDATES,     vp8_get_last_ref_updates},
787     {VP8D_GET_FRAME_CORRUPTED,      vp8_get_frame_corrupted},
788     {VP8D_GET_LAST_REF_USED,        vp8_get_last_ref_frame},
789     {VPXD_SET_DECRYPTOR,            vp8_set_decryptor},
790     { -1, NULL},
791 };
792 
793 
794 #ifndef VERSION_STRING
795 #define VERSION_STRING
796 #endif
797 CODEC_INTERFACE(vpx_codec_vp8_dx) =
798 {
799     "WebM Project VP8 Decoder" VERSION_STRING,
800     VPX_CODEC_INTERNAL_ABI_VERSION,
801     VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC | VP8_CAP_ERROR_CONCEALMENT |
802     VPX_CODEC_CAP_INPUT_FRAGMENTS,
803     /* vpx_codec_caps_t          caps; */
804     vp8_init,         /* vpx_codec_init_fn_t       init; */
805     vp8_destroy,      /* vpx_codec_destroy_fn_t    destroy; */
806     vp8_ctf_maps,     /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
807     {
808         vp8_peek_si,      /* vpx_codec_peek_si_fn_t    peek_si; */
809         vp8_get_si,       /* vpx_codec_get_si_fn_t     get_si; */
810         vp8_decode,       /* vpx_codec_decode_fn_t     decode; */
811         vp8_get_frame,    /* vpx_codec_frame_get_fn_t  frame_get; */
812         NOT_IMPLEMENTED,
813     },
814     { /* encoder functions */
815         0,
816         NOT_IMPLEMENTED,
817         NOT_IMPLEMENTED,
818         NOT_IMPLEMENTED,
819         NOT_IMPLEMENTED,
820         NOT_IMPLEMENTED,
821         NOT_IMPLEMENTED
822     }
823 };
824