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