2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
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.
14 #include "vpx/vpx_decoder.h"
15 #include "vpx/vp8dx.h"
16 #include "vpx/internal/vpx_codec_internal.h"
17 #include "vpx_version.h"
19 #include "onyxd_int.h"
21 #define VP8_CAP_POSTPROC (CONFIG_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
25 ((d&0x000000ff)<<24) | \
26 ((d&0x0000ff00)<<8) | \
27 ((d&0x00ff0000)>>8) | \
30 ((d&0x000000ff)<<8) | \
36 typedef vpx_codec_stream_info_t vp8_stream_info_t;
38 /* Structures for handling memory allocations */
41 VP8_SEG_ALG_PRIV = 256,
44 #define NELEMENTS(x) (sizeof(x)/sizeof(x[0]))
46 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t);
54 unsigned long(*calc_sz)(const vpx_codec_dec_cfg_t *, vpx_codec_flags_t);
57 static const mem_req_t vp8_mem_req_segs[] =
59 {VP8_SEG_ALG_PRIV, 0, 8, VPX_CODEC_MEM_ZERO, vp8_priv_sz},
60 {VP8_SEG_MAX, 0, 0, 0, NULL}
63 struct vpx_codec_alg_priv
65 vpx_codec_priv_t base;
66 vpx_codec_mmap_t mmaps[NELEMENTS(vp8_mem_req_segs)-1];
67 vpx_codec_dec_cfg_t cfg;
73 vp8_postproc_cfg_t postproc_cfg;
79 static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t flags)
81 /* Although this declaration is constant, we can't use it in the requested
82 * segments list because we want to define the requested segments list
83 * before defining the private type (so that the number of memory maps is
87 return sizeof(vpx_codec_alg_priv_t);
91 static void vp8_mmap_dtor(vpx_codec_mmap_t *mmap)
96 static vpx_codec_err_t vp8_mmap_alloc(vpx_codec_mmap_t *mmap)
101 align = mmap->align ? mmap->align - 1 : 0;
103 if (mmap->flags & VPX_CODEC_MEM_ZERO)
104 mmap->priv = calloc(1, mmap->sz + align);
106 mmap->priv = malloc(mmap->sz + align);
108 res = (mmap->priv) ? VPX_CODEC_OK : VPX_CODEC_MEM_ERROR;
109 mmap->base = (void *)((((uintptr_t)mmap->priv) + align) & ~(uintptr_t)align);
110 mmap->dtor = vp8_mmap_dtor;
114 static vpx_codec_err_t vp8_validate_mmaps(const vp8_stream_info_t *si,
115 const vpx_codec_mmap_t *mmaps,
116 vpx_codec_flags_t init_flags)
119 vpx_codec_err_t res = VPX_CODEC_OK;
121 for (i = 0; i < NELEMENTS(vp8_mem_req_segs) - 1; i++)
123 /* Ensure the segment has been allocated */
126 res = VPX_CODEC_MEM_ERROR;
130 /* Verify variable size segment is big enough for the current si. */
131 if (vp8_mem_req_segs[i].calc_sz)
133 vpx_codec_dec_cfg_t cfg;
138 if (mmaps[i].sz < vp8_mem_req_segs[i].calc_sz(&cfg, init_flags))
140 res = VPX_CODEC_MEM_ERROR;
149 static void vp8_init_ctx(vpx_codec_ctx_t *ctx, const vpx_codec_mmap_t *mmap)
153 ctx->priv = mmap->base;
154 ctx->priv->sz = sizeof(*ctx->priv);
155 ctx->priv->iface = ctx->iface;
156 ctx->priv->alg_priv = mmap->base;
158 for (i = 0; i < NELEMENTS(ctx->priv->alg_priv->mmaps); i++)
159 ctx->priv->alg_priv->mmaps[i].id = vp8_mem_req_segs[i].id;
161 ctx->priv->alg_priv->mmaps[0] = *mmap;
162 ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si);
163 ctx->priv->init_flags = ctx->init_flags;
167 /* Update the reference to the config structure to an internal copy. */
168 ctx->priv->alg_priv->cfg = *ctx->config.dec;
169 ctx->config.dec = &ctx->priv->alg_priv->cfg;
173 static void *mmap_lkup(vpx_codec_alg_priv_t *ctx, int id)
177 for (i = 0; i < NELEMENTS(vp8_mem_req_segs); i++)
178 if (ctx->mmaps[i].id == id)
179 return ctx->mmaps[i].base;
183 static void vp8_finalize_mmaps(vpx_codec_alg_priv_t *ctx)
186 ctx->pbi = mmap_lkup(ctx, VP6_SEG_PB_INSTANCE);
187 ctx->pbi->mbi.block_dx_info[0].idct_output_ptr = mmap_lkup(ctx, VP6_SEG_IDCT_BUFFER);
188 ctx->pbi->loop_filtered_block = mmap_lkup(ctx, VP6_SEG_LF_BLOCK);
189 ctx->pbi->huff = mmap_lkup(ctx, VP6_SEG_HUFF);
190 ctx->pbi->mbi.coeffs_base_ptr = mmap_lkup(ctx, VP6_SEG_COEFFS);
191 ctx->pbi->fc.above_y = mmap_lkup(ctx, VP6_SEG_ABOVEY);
192 ctx->pbi->fc.above_u = mmap_lkup(ctx, VP6_SEG_ABOVEU);
193 ctx->pbi->fc.above_v = mmap_lkup(ctx, VP6_SEG_ABOVEV);
194 ctx->pbi->prediction_mode = mmap_lkup(ctx, VP6_SEG_PRED_MODES);
195 ctx->pbi->mbmotion_vector = mmap_lkup(ctx, VP6_SEG_MV_FIELD);
196 ctx->pbi->fb_storage_ptr[0] = mmap_lkup(ctx, VP6_SEG_IMG0_STRG);
197 ctx->pbi->fb_storage_ptr[1] = mmap_lkup(ctx, VP6_SEG_IMG1_STRG);
198 ctx->pbi->fb_storage_ptr[2] = mmap_lkup(ctx, VP6_SEG_IMG2_STRG);
199 #if CONFIG_NEW_TOKENS
200 ctx->pbi->token_graph = mmap_lkup(ctx, VP6_SEG_TOKEN_GRAPH);
203 ctx->pbi->postproc.deblock.fragment_variances = mmap_lkup(ctx, VP6_SEG_DEBLOCKER);
204 ctx->pbi->fb_storage_ptr[3] = mmap_lkup(ctx, VP6_SEG_PP_IMG_STRG);
209 static vpx_codec_err_t vp8_init(vpx_codec_ctx_t *ctx)
211 vpx_codec_err_t res = VPX_CODEC_OK;
213 /* This function only allocates space for the vpx_codec_alg_priv_t
214 * structure. More memory may be required at the time the stream
215 * information becomes known.
219 vpx_codec_mmap_t mmap;
221 mmap.id = vp8_mem_req_segs[0].id;
222 mmap.sz = sizeof(vpx_codec_alg_priv_t);
223 mmap.align = vp8_mem_req_segs[0].align;
224 mmap.flags = vp8_mem_req_segs[0].flags;
226 res = vp8_mmap_alloc(&mmap);
229 vp8_init_ctx(ctx, &mmap);
231 ctx->priv->alg_priv->defer_alloc = 1;
232 /*post processing level initialized to do nothing */
239 static vpx_codec_err_t vp8_destroy(vpx_codec_alg_priv_t *ctx)
243 vp8dx_remove_decompressor(ctx->pbi);
245 for (i = NELEMENTS(ctx->mmaps) - 1; i >= 0; i--)
247 if (ctx->mmaps[i].dtor)
248 ctx->mmaps[i].dtor(&ctx->mmaps[i]);
254 static vpx_codec_err_t vp8_peek_si(const uint8_t *data,
255 unsigned int data_sz,
256 vpx_codec_stream_info_t *si)
259 vpx_codec_err_t res = VPX_CODEC_OK;
261 /* Parse uncompresssed part of key frame header.
262 * 3 bytes:- including version, frame type and an offset
263 * 3 bytes:- sync code (0x9d, 0x01, 0x2a)
264 * 4 bytes:- including image width and height in the lowest 14 bits
265 * of each 2-byte value.
269 if (data_sz >= 10 && !(data[0] & 0x01)) /* I-Frame */
271 const uint8_t *c = data + 3;
275 if (c[0] != 0x9d || c[1] != 0x01 || c[2] != 0x2a)
276 res = VPX_CODEC_UNSUP_BITSTREAM;
278 si->w = swap2(*(const unsigned short *)(c + 3)) & 0x3fff;
279 si->h = swap2(*(const unsigned short *)(c + 5)) & 0x3fff;
281 //printf("w=%d, h=%d\n", si->w, si->h);
282 if (!(si->h | si->w))
283 res = VPX_CODEC_UNSUP_BITSTREAM;
286 res = VPX_CODEC_UNSUP_BITSTREAM;
293 static vpx_codec_err_t vp8_get_si(vpx_codec_alg_priv_t *ctx,
294 vpx_codec_stream_info_t *si)
299 if (si->sz >= sizeof(vp8_stream_info_t))
300 sz = sizeof(vp8_stream_info_t);
302 sz = sizeof(vpx_codec_stream_info_t);
304 memcpy(si, &ctx->si, sz);
311 static vpx_codec_err_t
312 update_error_state(vpx_codec_alg_priv_t *ctx,
313 const struct vpx_internal_error_info *error)
317 if ((res = error->error_code))
318 ctx->base.err_detail = error->has_detail
326 static vpx_codec_err_t vp8_decode(vpx_codec_alg_priv_t *ctx,
328 unsigned int data_sz,
332 vpx_codec_err_t res = VPX_CODEC_OK;
336 /* Determine the stream parameters */
338 res = ctx->base.iface->dec.peek_si(data, data_sz, &ctx->si);
341 /* Perform deferred allocations, if required */
342 if (!res && ctx->defer_alloc)
346 for (i = 1; !res && i < NELEMENTS(ctx->mmaps); i++)
348 vpx_codec_dec_cfg_t cfg;
352 ctx->mmaps[i].id = vp8_mem_req_segs[i].id;
353 ctx->mmaps[i].sz = vp8_mem_req_segs[i].sz;
354 ctx->mmaps[i].align = vp8_mem_req_segs[i].align;
355 ctx->mmaps[i].flags = vp8_mem_req_segs[i].flags;
357 if (!ctx->mmaps[i].sz)
358 ctx->mmaps[i].sz = vp8_mem_req_segs[i].calc_sz(&cfg,
359 ctx->base.init_flags);
361 res = vp8_mmap_alloc(&ctx->mmaps[i]);
365 vp8_finalize_mmaps(ctx);
367 ctx->defer_alloc = 0;
370 /* Initialize the decoder instance on the first frame*/
371 if (!res && !ctx->decoder_init)
373 res = vp8_validate_mmaps(&ctx->si, ctx->mmaps, ctx->base.init_flags);
382 oxcf.Width = ctx->si.w;
383 oxcf.Height = ctx->si.h;
385 oxcf.postprocess = 0;
386 oxcf.max_threads = ctx->cfg.threads;
388 optr = vp8dx_create_decompressor(&oxcf);
390 /* If postprocessing was enabled by the application and a
391 * configuration has not been provided, default it.
393 if (!ctx->postproc_cfg_set
394 && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
396 ctx->postproc_cfg.post_proc_flag =
397 VP8_DEBLOCK | VP8_DEMACROBLOCK;
398 ctx->postproc_cfg.deblocking_level = 4;
399 ctx->postproc_cfg.noise_level = 0;
403 res = VPX_CODEC_ERROR;
408 ctx->decoder_init = 1;
411 if (!res && ctx->pbi)
413 YV12_BUFFER_CONFIG sd;
414 INT64 time_stamp = 0, time_end_stamp = 0;
416 int ppdeblocking = 0;
419 if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
421 ppflag = ctx->postproc_cfg.post_proc_flag;
422 ppdeblocking = ctx->postproc_cfg.deblocking_level;
423 ppnoise = ctx->postproc_cfg.noise_level;
426 if (vp8dx_receive_compressed_data(ctx->pbi, data_sz, data, deadline))
428 VP8D_COMP *pbi = (VP8D_COMP *)ctx->pbi;
429 res = update_error_state(ctx, &pbi->common.error);
432 if (!res && 0 == vp8dx_get_raw_frame(ctx->pbi, &sd, &time_stamp, &time_end_stamp, ppdeblocking, ppnoise, ppflag))
434 /* Align width/height */
435 unsigned int a_w = (sd.y_width + 15) & ~15;
436 unsigned int a_h = (sd.y_height + 15) & ~15;
438 vpx_img_wrap(&ctx->img, VPX_IMG_FMT_I420,
439 a_w + 2 * VP8BORDERINPIXELS,
440 a_h + 2 * VP8BORDERINPIXELS,
443 vpx_img_set_rect(&ctx->img,
444 VP8BORDERINPIXELS, VP8BORDERINPIXELS,
445 sd.y_width, sd.y_height);
454 static vpx_image_t *vp8_get_frame(vpx_codec_alg_priv_t *ctx,
455 vpx_codec_iter_t *iter)
457 vpx_image_t *img = NULL;
461 /* iter acts as a flip flop, so an image is only returned on the first
476 vpx_codec_err_t vp8_xma_get_mmap(const vpx_codec_ctx_t *ctx,
477 vpx_codec_mmap_t *mmap,
478 vpx_codec_iter_t *iter)
481 const mem_req_t *seg_iter = *iter;
483 /* Get address of next segment request */
487 seg_iter = vp8_mem_req_segs;
488 else if (seg_iter->id != VP8_SEG_MAX)
491 *iter = (vpx_codec_iter_t)seg_iter;
493 if (seg_iter->id != VP8_SEG_MAX)
495 mmap->id = seg_iter->id;
496 mmap->sz = seg_iter->sz;
497 mmap->align = seg_iter->align;
498 mmap->flags = seg_iter->flags;
501 mmap->sz = seg_iter->calc_sz(ctx->config.dec, ctx->init_flags);
506 res = VPX_CODEC_LIST_END;
508 while (!mmap->sz && res != VPX_CODEC_LIST_END);
513 static vpx_codec_err_t vp8_xma_set_mmap(vpx_codec_ctx_t *ctx,
514 const vpx_codec_mmap_t *mmap)
516 vpx_codec_err_t res = VPX_CODEC_MEM_ERROR;
521 if (mmap->id == VP8_SEG_ALG_PRIV)
525 vp8_init_ctx(ctx, mmap);
533 if (ctx->priv->alg_priv)
535 for (i = 0; i < NELEMENTS(vp8_mem_req_segs); i++)
537 if (ctx->priv->alg_priv->mmaps[i].id == mmap->id)
538 if (!ctx->priv->alg_priv->mmaps[i].base)
540 ctx->priv->alg_priv->mmaps[i] = *mmap;
544 done &= (ctx->priv->alg_priv->mmaps[i].base != NULL);
550 vp8_finalize_mmaps(ctx->priv->alg_priv);
551 res = ctx->iface->init(ctx);
557 static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img,
558 YV12_BUFFER_CONFIG *yv12)
560 vpx_codec_err_t res = VPX_CODEC_OK;
561 yv12->y_buffer = img->planes[VPX_PLANE_Y];
562 yv12->u_buffer = img->planes[VPX_PLANE_U];
563 yv12->v_buffer = img->planes[VPX_PLANE_V];
565 yv12->y_width = img->d_w;
566 yv12->y_height = img->d_h;
567 yv12->uv_width = yv12->y_width / 2;
568 yv12->uv_height = yv12->y_height / 2;
570 yv12->y_stride = img->stride[VPX_PLANE_Y];
571 yv12->uv_stride = img->stride[VPX_PLANE_U];
573 yv12->border = (img->stride[VPX_PLANE_Y] - img->d_w) / 2;
574 yv12->clrtype = (img->fmt == VPX_IMG_FMT_VPXI420 || img->fmt == VPX_IMG_FMT_VPXYV12);
580 static vpx_codec_err_t vp8_set_reference(vpx_codec_alg_priv_t *ctx,
585 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
589 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
590 YV12_BUFFER_CONFIG sd;
592 image2yuvconfig(&frame->img, &sd);
594 vp8dx_set_reference(ctx->pbi, frame->frame_type, &sd);
598 return VPX_CODEC_INVALID_PARAM;
602 static vpx_codec_err_t vp8_get_reference(vpx_codec_alg_priv_t *ctx,
607 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
611 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
612 YV12_BUFFER_CONFIG sd;
614 image2yuvconfig(&frame->img, &sd);
616 vp8dx_get_reference(ctx->pbi, frame->frame_type, &sd);
620 return VPX_CODEC_INVALID_PARAM;
624 static vpx_codec_err_t vp8_set_postproc(vpx_codec_alg_priv_t *ctx,
628 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
633 ctx->postproc_cfg_set = 1;
634 ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
638 return VPX_CODEC_INVALID_PARAM;
641 return VPX_CODEC_INCAPABLE;
646 vpx_codec_ctrl_fn_map_t vp8_ctf_maps[] =
648 {VP8_SET_REFERENCE, vp8_set_reference},
649 {VP8_COPY_REFERENCE, vp8_get_reference},
650 {VP8_SET_POSTPROC, vp8_set_postproc},
655 #ifndef VERSION_STRING
656 #define VERSION_STRING
658 vpx_codec_iface_t vpx_codec_vp8_dx_algo =
660 "WebM Project VP8 Decoder" VERSION_STRING,
661 VPX_CODEC_INTERNAL_ABI_VERSION,
662 VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC,
663 /* vpx_codec_caps_t caps; */
664 vp8_init, /* vpx_codec_init_fn_t init; */
665 vp8_destroy, /* vpx_codec_destroy_fn_t destroy; */
666 vp8_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
667 vp8_xma_get_mmap, /* vpx_codec_get_mmap_fn_t get_mmap; */
668 vp8_xma_set_mmap, /* vpx_codec_set_mmap_fn_t set_mmap; */
670 vp8_peek_si, /* vpx_codec_peek_si_fn_t peek_si; */
671 vp8_get_si, /* vpx_codec_get_si_fn_t get_si; */
672 vp8_decode, /* vpx_codec_decode_fn_t decode; */
673 vp8_get_frame, /* vpx_codec_frame_get_fn_t frame_get; */
675 {NOT_IMPLEMENTED} /* encoder functions */
679 * BEGIN BACKWARDS COMPATIBILITY SHIM.
681 vpx_codec_iface_t vpx_codec_vp8_algo =
683 "WebM Project VP8 Decoder (Deprecated API)" VERSION_STRING,
684 VPX_CODEC_INTERNAL_ABI_VERSION,
685 VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC,
686 /* vpx_codec_caps_t caps; */
687 vp8_init, /* vpx_codec_init_fn_t init; */
688 vp8_destroy, /* vpx_codec_destroy_fn_t destroy; */
689 vp8_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */
690 vp8_xma_get_mmap, /* vpx_codec_get_mmap_fn_t get_mmap; */
691 vp8_xma_set_mmap, /* vpx_codec_set_mmap_fn_t set_mmap; */
693 vp8_peek_si, /* vpx_codec_peek_si_fn_t peek_si; */
694 vp8_get_si, /* vpx_codec_get_si_fn_t get_si; */
695 vp8_decode, /* vpx_codec_decode_fn_t decode; */
696 vp8_get_frame, /* vpx_codec_frame_get_fn_t frame_get; */
698 {NOT_IMPLEMENTED} /* encoder functions */