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_dsp_rtcd.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "vpx/vpx_decoder.h"
18 #include "vpx/vp8dx.h"
19 #include "vpx/internal/vpx_codec_internal.h"
20 #include "vpx_version.h"
21 #include "common/alloccommon.h"
22 #include "common/common.h"
23 #include "common/onyxd.h"
24 #include "decoder/onyxd_int.h"
25 #include "vpx_mem/vpx_mem.h"
26 #if CONFIG_ERROR_CONCEALMENT
27 #include "decoder/error_concealment.h"
28 #endif
29 #include "decoder/decoderthreading.h"
30 
31 #define VP8_CAP_POSTPROC (CONFIG_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
32 #define VP8_CAP_ERROR_CONCEALMENT (CONFIG_ERROR_CONCEALMENT ? \
33                                     VPX_CODEC_CAP_ERROR_CONCEALMENT : 0)
34 
35 typedef vpx_codec_stream_info_t  vp8_stream_info_t;
36 
37 /* Structures for handling memory allocations */
38 typedef enum
39 {
40     VP8_SEG_ALG_PRIV     = 256,
41     VP8_SEG_MAX
42 } mem_seg_id_t;
43 #define NELEMENTS(x) ((int)(sizeof(x)/sizeof(x[0])))
44 
45 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t);
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_priv_sz(const vpx_codec_dec_cfg_t * si,vpx_codec_flags_t flags)71 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t flags)
72 {
73     /* Although this declaration is constant, we can't use it in the requested
74      * segments list because we want to define the requested segments list
75      * before defining the private type (so that the number of memory maps is
76      * known)
77      */
78     (void)si;
79     (void)flags;
80     return sizeof(vpx_codec_alg_priv_t);
81 }
82 
vp8_init_ctx(vpx_codec_ctx_t * ctx)83 static void vp8_init_ctx(vpx_codec_ctx_t *ctx)
84 {
85     vpx_codec_alg_priv_t *priv =
86         (vpx_codec_alg_priv_t *)vpx_calloc(1, sizeof(*priv));
87 
88     ctx->priv = (vpx_codec_priv_t *)priv;
89     ctx->priv->init_flags = ctx->init_flags;
90 
91     priv->si.sz = sizeof(priv->si);
92     priv->decrypt_cb = NULL;
93     priv->decrypt_state = NULL;
94 
95     if (ctx->config.dec)
96     {
97         /* Update the reference to the config structure to an internal copy. */
98         priv->cfg = *ctx->config.dec;
99         ctx->config.dec = &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     vpx_codec_alg_priv_t *priv = NULL;
108     (void) data;
109 
110     vp8_rtcd();
111     vpx_dsp_rtcd();
112     vpx_scale_rtcd();
113 
114     /* This function only allocates space for the vpx_codec_alg_priv_t
115      * structure. More memory may be required at the time the stream
116      * information becomes known.
117      */
118     if (!ctx->priv) {
119       vp8_init_ctx(ctx);
120       priv = (vpx_codec_alg_priv_t *)ctx->priv;
121 
122       /* initialize number of fragments to zero */
123       priv->fragments.count = 0;
124       /* is input fragments enabled? */
125       priv->fragments.enabled =
126           (priv->base.init_flags & VPX_CODEC_USE_INPUT_FRAGMENTS);
127 
128       /*post processing level initialized to do nothing */
129     } else {
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       /* 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                 return 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         memset((void*)ctx->fragments.ptrs, 0, sizeof(ctx->fragments.ptrs));
294         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 && (data == NULL && data_sz == 0))
314     {
315         return 0;
316     }
317 
318     if (!ctx->fragments.enabled)
319     {
320         ctx->fragments.ptrs[0] = data;
321         ctx->fragments.sizes[0] = data_sz;
322         ctx->fragments.count = 1;
323     }
324 
325     return 1;
326 }
327 
vp8_decode(vpx_codec_alg_priv_t * ctx,const uint8_t * data,unsigned int data_sz,void * user_priv,long deadline)328 static vpx_codec_err_t vp8_decode(vpx_codec_alg_priv_t  *ctx,
329                                   const uint8_t         *data,
330                                   unsigned int            data_sz,
331                                   void                    *user_priv,
332                                   long                    deadline)
333 {
334     vpx_codec_err_t res = VPX_CODEC_OK;
335     unsigned int resolution_change = 0;
336     unsigned int w, h;
337 
338     if (!ctx->fragments.enabled && (data == NULL && data_sz == 0))
339     {
340         return 0;
341     }
342 
343     /* Update the input fragment data */
344     if(update_fragments(ctx, data, data_sz, &res) <= 0)
345         return res;
346 
347     /* Determine the stream parameters. Note that we rely on peek_si to
348      * validate that we have a buffer that does not wrap around the top
349      * of the heap.
350      */
351     w = ctx->si.w;
352     h = ctx->si.h;
353 
354     res = vp8_peek_si_internal(ctx->fragments.ptrs[0], ctx->fragments.sizes[0],
355                                &ctx->si, ctx->decrypt_cb, ctx->decrypt_state);
356 
357     if((res == VPX_CODEC_UNSUP_BITSTREAM) && !ctx->si.is_kf)
358     {
359         /* the peek function returns an error for non keyframes, however for
360          * this case, it is not an error */
361         res = VPX_CODEC_OK;
362     }
363 
364     if(!ctx->decoder_init && !ctx->si.is_kf)
365         res = VPX_CODEC_UNSUP_BITSTREAM;
366 
367     if ((ctx->si.h != h) || (ctx->si.w != w))
368         resolution_change = 1;
369 
370     /* Initialize the decoder instance on the first frame*/
371     if (!res && !ctx->decoder_init)
372     {
373       VP8D_CONFIG oxcf;
374 
375       oxcf.Width = ctx->si.w;
376       oxcf.Height = ctx->si.h;
377       oxcf.Version = 9;
378       oxcf.postprocess = 0;
379       oxcf.max_threads = ctx->cfg.threads;
380       oxcf.error_concealment =
381           (ctx->base.init_flags & VPX_CODEC_USE_ERROR_CONCEALMENT);
382 
383       /* If postprocessing was enabled by the application and a
384        * configuration has not been provided, default it.
385        */
386        if (!ctx->postproc_cfg_set
387            && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)) {
388          ctx->postproc_cfg.post_proc_flag =
389              VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE;
390          ctx->postproc_cfg.deblocking_level = 4;
391          ctx->postproc_cfg.noise_level = 0;
392        }
393 
394        res = vp8_create_decoder_instances(&ctx->yv12_frame_buffers, &oxcf);
395        ctx->decoder_init = 1;
396     }
397 
398     /* Set these even if already initialized.  The caller may have changed the
399      * decrypt config between frames.
400      */
401     if (ctx->decoder_init) {
402       ctx->yv12_frame_buffers.pbi[0]->decrypt_cb = ctx->decrypt_cb;
403       ctx->yv12_frame_buffers.pbi[0]->decrypt_state = ctx->decrypt_state;
404     }
405 
406     if (!res)
407     {
408         VP8D_COMP *pbi = ctx->yv12_frame_buffers.pbi[0];
409         if (resolution_change)
410         {
411             VP8_COMMON *const pc = & pbi->common;
412             MACROBLOCKD *const xd  = & pbi->mb;
413 #if CONFIG_MULTITHREAD
414             int i;
415 #endif
416             pc->Width = ctx->si.w;
417             pc->Height = ctx->si.h;
418             {
419                 int prev_mb_rows = pc->mb_rows;
420 
421                 if (setjmp(pbi->common.error.jmp))
422                 {
423                     pbi->common.error.setjmp = 0;
424                     vp8_clear_system_state();
425                     /* same return value as used in vp8dx_receive_compressed_data */
426                     return -1;
427                 }
428 
429                 pbi->common.error.setjmp = 1;
430 
431                 if (pc->Width <= 0)
432                 {
433                     pc->Width = w;
434                     vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
435                                        "Invalid frame width");
436                 }
437 
438                 if (pc->Height <= 0)
439                 {
440                     pc->Height = h;
441                     vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
442                                        "Invalid frame height");
443                 }
444 
445                 if (vp8_alloc_frame_buffers(pc, pc->Width, pc->Height))
446                     vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
447                                        "Failed to allocate frame buffers");
448 
449                 xd->pre = pc->yv12_fb[pc->lst_fb_idx];
450                 xd->dst = pc->yv12_fb[pc->new_fb_idx];
451 
452 #if CONFIG_MULTITHREAD
453                 for (i = 0; i < pbi->allocated_decoding_thread_count; i++)
454                 {
455                     pbi->mb_row_di[i].mbd.dst = pc->yv12_fb[pc->new_fb_idx];
456                     vp8_build_block_doffsets(&pbi->mb_row_di[i].mbd);
457                 }
458 #endif
459                 vp8_build_block_doffsets(&pbi->mb);
460 
461                 /* allocate memory for last frame MODE_INFO array */
462 #if CONFIG_ERROR_CONCEALMENT
463 
464                 if (pbi->ec_enabled)
465                 {
466                     /* old prev_mip was released by vp8_de_alloc_frame_buffers()
467                      * called in vp8_alloc_frame_buffers() */
468                     pc->prev_mip = vpx_calloc(
469                                        (pc->mb_cols + 1) * (pc->mb_rows + 1),
470                                        sizeof(MODE_INFO));
471 
472                     if (!pc->prev_mip)
473                     {
474                         vp8_de_alloc_frame_buffers(pc);
475                         vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
476                                            "Failed to allocate"
477                                            "last frame MODE_INFO array");
478                     }
479 
480                     pc->prev_mi = pc->prev_mip + pc->mode_info_stride + 1;
481 
482                     if (vp8_alloc_overlap_lists(pbi))
483                         vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
484                                            "Failed to allocate overlap lists "
485                                            "for error concealment");
486                 }
487 
488 #endif
489 
490 #if CONFIG_MULTITHREAD
491                 if (pbi->b_multithreaded_rd)
492                     vp8mt_alloc_temp_buffers(pbi, pc->Width, prev_mb_rows);
493 #else
494                 (void)prev_mb_rows;
495 #endif
496             }
497 
498             pbi->common.error.setjmp = 0;
499 
500             /* required to get past the first get_free_fb() call */
501             pbi->common.fb_idx_ref_cnt[0] = 0;
502         }
503 
504         /* update the pbi fragment data */
505         pbi->fragments = ctx->fragments;
506 
507         ctx->user_priv = user_priv;
508         if (vp8dx_receive_compressed_data(pbi, data_sz, data, deadline))
509         {
510             res = update_error_state(ctx, &pbi->common.error);
511         }
512 
513         /* get ready for the next series of fragments */
514         ctx->fragments.count = 0;
515     }
516 
517     return res;
518 }
519 
vp8_get_frame(vpx_codec_alg_priv_t * ctx,vpx_codec_iter_t * iter)520 static vpx_image_t *vp8_get_frame(vpx_codec_alg_priv_t  *ctx,
521                                   vpx_codec_iter_t      *iter)
522 {
523     vpx_image_t *img = NULL;
524 
525     /* iter acts as a flip flop, so an image is only returned on the first
526      * call to get_frame.
527      */
528     if (!(*iter) && ctx->yv12_frame_buffers.pbi[0])
529     {
530         YV12_BUFFER_CONFIG sd;
531         int64_t time_stamp = 0, time_end_stamp = 0;
532         vp8_ppflags_t flags = {0};
533 
534         if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
535         {
536             flags.post_proc_flag= ctx->postproc_cfg.post_proc_flag
537 #if CONFIG_POSTPROC_VISUALIZER
538 
539                                 | ((ctx->dbg_color_ref_frame_flag != 0) ? VP8D_DEBUG_CLR_FRM_REF_BLKS : 0)
540                                 | ((ctx->dbg_color_mb_modes_flag != 0) ? VP8D_DEBUG_CLR_BLK_MODES : 0)
541                                 | ((ctx->dbg_color_b_modes_flag != 0) ? VP8D_DEBUG_CLR_BLK_MODES : 0)
542                                 | ((ctx->dbg_display_mv_flag != 0) ? VP8D_DEBUG_DRAW_MV : 0)
543 #endif
544                                 ;
545             flags.deblocking_level      = ctx->postproc_cfg.deblocking_level;
546             flags.noise_level           = ctx->postproc_cfg.noise_level;
547 #if CONFIG_POSTPROC_VISUALIZER
548             flags.display_ref_frame_flag= ctx->dbg_color_ref_frame_flag;
549             flags.display_mb_modes_flag = ctx->dbg_color_mb_modes_flag;
550             flags.display_b_modes_flag  = ctx->dbg_color_b_modes_flag;
551             flags.display_mv_flag       = ctx->dbg_display_mv_flag;
552 #endif
553         }
554 
555         if (0 == vp8dx_get_raw_frame(ctx->yv12_frame_buffers.pbi[0], &sd,
556                                      &time_stamp, &time_end_stamp, &flags))
557         {
558             yuvconfig2image(&ctx->img, &sd, ctx->user_priv);
559 
560             img = &ctx->img;
561             *iter = img;
562         }
563     }
564 
565     return img;
566 }
567 
image2yuvconfig(const vpx_image_t * img,YV12_BUFFER_CONFIG * yv12)568 static vpx_codec_err_t image2yuvconfig(const vpx_image_t   *img,
569                                        YV12_BUFFER_CONFIG  *yv12)
570 {
571     const int y_w = img->d_w;
572     const int y_h = img->d_h;
573     const int uv_w = (img->d_w + 1) / 2;
574     const int uv_h = (img->d_h + 1) / 2;
575     vpx_codec_err_t        res = VPX_CODEC_OK;
576     yv12->y_buffer = img->planes[VPX_PLANE_Y];
577     yv12->u_buffer = img->planes[VPX_PLANE_U];
578     yv12->v_buffer = img->planes[VPX_PLANE_V];
579 
580     yv12->y_crop_width  = y_w;
581     yv12->y_crop_height = y_h;
582     yv12->y_width  = y_w;
583     yv12->y_height = y_h;
584     yv12->uv_crop_width = uv_w;
585     yv12->uv_crop_height = uv_h;
586     yv12->uv_width = uv_w;
587     yv12->uv_height = uv_h;
588 
589     yv12->y_stride = img->stride[VPX_PLANE_Y];
590     yv12->uv_stride = img->stride[VPX_PLANE_U];
591 
592     yv12->border  = (img->stride[VPX_PLANE_Y] - img->d_w) / 2;
593     return res;
594 }
595 
596 
vp8_set_reference(vpx_codec_alg_priv_t * ctx,va_list args)597 static vpx_codec_err_t vp8_set_reference(vpx_codec_alg_priv_t *ctx,
598                                          va_list args)
599 {
600 
601     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
602 
603     if (data && !ctx->yv12_frame_buffers.use_frame_threads)
604     {
605         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
606         YV12_BUFFER_CONFIG sd;
607 
608         image2yuvconfig(&frame->img, &sd);
609 
610         return vp8dx_set_reference(ctx->yv12_frame_buffers.pbi[0],
611                                    frame->frame_type, &sd);
612     }
613     else
614         return VPX_CODEC_INVALID_PARAM;
615 
616 }
617 
vp8_get_reference(vpx_codec_alg_priv_t * ctx,va_list args)618 static vpx_codec_err_t vp8_get_reference(vpx_codec_alg_priv_t *ctx,
619                                          va_list args)
620 {
621 
622     vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
623 
624     if (data && !ctx->yv12_frame_buffers.use_frame_threads)
625     {
626         vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
627         YV12_BUFFER_CONFIG sd;
628 
629         image2yuvconfig(&frame->img, &sd);
630 
631         return vp8dx_get_reference(ctx->yv12_frame_buffers.pbi[0],
632                                    frame->frame_type, &sd);
633     }
634     else
635         return VPX_CODEC_INVALID_PARAM;
636 
637 }
638 
vp8_set_postproc(vpx_codec_alg_priv_t * ctx,va_list args)639 static vpx_codec_err_t vp8_set_postproc(vpx_codec_alg_priv_t *ctx,
640                                         va_list args)
641 {
642 #if CONFIG_POSTPROC
643     vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
644 
645     if (data)
646     {
647         ctx->postproc_cfg_set = 1;
648         ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
649         return VPX_CODEC_OK;
650     }
651     else
652         return VPX_CODEC_INVALID_PARAM;
653 
654 #else
655     (void)ctx;
656     (void)args;
657     return VPX_CODEC_INCAPABLE;
658 #endif
659 }
660 
661 
vp8_set_dbg_color_ref_frame(vpx_codec_alg_priv_t * ctx,va_list args)662 static vpx_codec_err_t vp8_set_dbg_color_ref_frame(vpx_codec_alg_priv_t *ctx,
663                                                    va_list args) {
664 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
665   ctx->dbg_color_ref_frame_flag = va_arg(args, int);
666   return VPX_CODEC_OK;
667 #else
668   (void)ctx;
669   (void)args;
670   return VPX_CODEC_INCAPABLE;
671 #endif
672 }
673 
vp8_set_dbg_color_mb_modes(vpx_codec_alg_priv_t * ctx,va_list args)674 static vpx_codec_err_t vp8_set_dbg_color_mb_modes(vpx_codec_alg_priv_t *ctx,
675                                                   va_list args) {
676 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
677   ctx->dbg_color_mb_modes_flag = va_arg(args, int);
678   return VPX_CODEC_OK;
679 #else
680   (void)ctx;
681   (void)args;
682   return VPX_CODEC_INCAPABLE;
683 #endif
684 }
685 
vp8_set_dbg_color_b_modes(vpx_codec_alg_priv_t * ctx,va_list args)686 static vpx_codec_err_t vp8_set_dbg_color_b_modes(vpx_codec_alg_priv_t *ctx,
687                                                  va_list args) {
688 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
689   ctx->dbg_color_b_modes_flag = va_arg(args, int);
690   return VPX_CODEC_OK;
691 #else
692   (void)ctx;
693   (void)args;
694   return VPX_CODEC_INCAPABLE;
695 #endif
696 }
697 
vp8_set_dbg_display_mv(vpx_codec_alg_priv_t * ctx,va_list args)698 static vpx_codec_err_t vp8_set_dbg_display_mv(vpx_codec_alg_priv_t *ctx,
699                                               va_list args) {
700 #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC
701   ctx->dbg_display_mv_flag = va_arg(args, int);
702   return VPX_CODEC_OK;
703 #else
704   (void)ctx;
705   (void)args;
706   return VPX_CODEC_INCAPABLE;
707 #endif
708 }
709 
vp8_get_last_ref_updates(vpx_codec_alg_priv_t * ctx,va_list args)710 static vpx_codec_err_t vp8_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
711                                                 va_list args)
712 {
713     int *update_info = va_arg(args, int *);
714 
715     if (update_info && !ctx->yv12_frame_buffers.use_frame_threads)
716     {
717         VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
718 
719         *update_info = pbi->common.refresh_alt_ref_frame * (int) VP8_ALTR_FRAME
720             + pbi->common.refresh_golden_frame * (int) VP8_GOLD_FRAME
721             + pbi->common.refresh_last_frame * (int) VP8_LAST_FRAME;
722 
723         return VPX_CODEC_OK;
724     }
725     else
726         return VPX_CODEC_INVALID_PARAM;
727 }
728 
729 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)730 static vpx_codec_err_t vp8_get_last_ref_frame(vpx_codec_alg_priv_t *ctx,
731                                               va_list args)
732 {
733     int *ref_info = va_arg(args, int *);
734 
735     if (ref_info && !ctx->yv12_frame_buffers.use_frame_threads)
736     {
737         VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
738         VP8_COMMON *oci = &pbi->common;
739         *ref_info =
740             (vp8dx_references_buffer( oci, ALTREF_FRAME )?VP8_ALTR_FRAME:0) |
741             (vp8dx_references_buffer( oci, GOLDEN_FRAME )?VP8_GOLD_FRAME:0) |
742             (vp8dx_references_buffer( oci, LAST_FRAME )?VP8_LAST_FRAME:0);
743 
744         return VPX_CODEC_OK;
745     }
746     else
747         return VPX_CODEC_INVALID_PARAM;
748 }
749 
vp8_get_frame_corrupted(vpx_codec_alg_priv_t * ctx,va_list args)750 static vpx_codec_err_t vp8_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
751                                                va_list args)
752 {
753 
754     int *corrupted = va_arg(args, int *);
755     VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0];
756 
757     if (corrupted && pbi)
758     {
759         const YV12_BUFFER_CONFIG *const frame = pbi->common.frame_to_show;
760         if (frame == NULL) return VPX_CODEC_ERROR;
761         *corrupted = frame->corrupted;
762         return VPX_CODEC_OK;
763     }
764     else
765         return VPX_CODEC_INVALID_PARAM;
766 
767 }
768 
vp8_set_decryptor(vpx_codec_alg_priv_t * ctx,va_list args)769 static vpx_codec_err_t vp8_set_decryptor(vpx_codec_alg_priv_t *ctx,
770                                          va_list args)
771 {
772     vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
773 
774     if (init)
775     {
776         ctx->decrypt_cb = init->decrypt_cb;
777         ctx->decrypt_state = init->decrypt_state;
778     }
779     else
780     {
781         ctx->decrypt_cb = NULL;
782         ctx->decrypt_state = NULL;
783     }
784     return VPX_CODEC_OK;
785 }
786 
787 vpx_codec_ctrl_fn_map_t vp8_ctf_maps[] =
788 {
789     {VP8_SET_REFERENCE,             vp8_set_reference},
790     {VP8_COPY_REFERENCE,            vp8_get_reference},
791     {VP8_SET_POSTPROC,              vp8_set_postproc},
792     {VP8_SET_DBG_COLOR_REF_FRAME,   vp8_set_dbg_color_ref_frame},
793     {VP8_SET_DBG_COLOR_MB_MODES,    vp8_set_dbg_color_mb_modes},
794     {VP8_SET_DBG_COLOR_B_MODES,     vp8_set_dbg_color_b_modes},
795     {VP8_SET_DBG_DISPLAY_MV,        vp8_set_dbg_display_mv},
796     {VP8D_GET_LAST_REF_UPDATES,     vp8_get_last_ref_updates},
797     {VP8D_GET_FRAME_CORRUPTED,      vp8_get_frame_corrupted},
798     {VP8D_GET_LAST_REF_USED,        vp8_get_last_ref_frame},
799     {VPXD_SET_DECRYPTOR,            vp8_set_decryptor},
800     { -1, NULL},
801 };
802 
803 
804 #ifndef VERSION_STRING
805 #define VERSION_STRING
806 #endif
807 CODEC_INTERFACE(vpx_codec_vp8_dx) =
808 {
809     "WebM Project VP8 Decoder" VERSION_STRING,
810     VPX_CODEC_INTERNAL_ABI_VERSION,
811     VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC | VP8_CAP_ERROR_CONCEALMENT |
812     VPX_CODEC_CAP_INPUT_FRAGMENTS,
813     /* vpx_codec_caps_t          caps; */
814     vp8_init,         /* vpx_codec_init_fn_t       init; */
815     vp8_destroy,      /* vpx_codec_destroy_fn_t    destroy; */
816     vp8_ctf_maps,     /* vpx_codec_ctrl_fn_map_t  *ctrl_maps; */
817     {
818         vp8_peek_si,      /* vpx_codec_peek_si_fn_t    peek_si; */
819         vp8_get_si,       /* vpx_codec_get_si_fn_t     get_si; */
820         vp8_decode,       /* vpx_codec_decode_fn_t     decode; */
821         vp8_get_frame,    /* vpx_codec_frame_get_fn_t  frame_get; */
822         NULL,
823     },
824     { /* encoder functions */
825         0,
826         NULL,
827         NULL,
828         NULL,
829         NULL,
830         NULL,
831         NULL
832     }
833 };
834