michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include michael@0: #include michael@0: #include "vp8_rtcd.h" michael@0: #include "vpx/vpx_decoder.h" michael@0: #include "vpx/vp8dx.h" michael@0: #include "vpx/internal/vpx_codec_internal.h" michael@0: #include "vpx_version.h" michael@0: #include "common/onyxd.h" michael@0: #include "decoder/onyxd_int.h" michael@0: #include "common/alloccommon.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: #include "decoder/error_concealment.h" michael@0: #endif michael@0: #include "decoder/decoderthreading.h" michael@0: michael@0: #define VP8_CAP_POSTPROC (CONFIG_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0) michael@0: #define VP8_CAP_ERROR_CONCEALMENT (CONFIG_ERROR_CONCEALMENT ? \ michael@0: VPX_CODEC_CAP_ERROR_CONCEALMENT : 0) michael@0: michael@0: typedef vpx_codec_stream_info_t vp8_stream_info_t; michael@0: michael@0: /* Structures for handling memory allocations */ michael@0: typedef enum michael@0: { michael@0: VP8_SEG_ALG_PRIV = 256, michael@0: VP8_SEG_MAX michael@0: } mem_seg_id_t; michael@0: #define NELEMENTS(x) ((int)(sizeof(x)/sizeof(x[0]))) michael@0: michael@0: static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t); michael@0: michael@0: static const mem_req_t vp8_mem_req_segs[] = michael@0: { michael@0: {VP8_SEG_ALG_PRIV, 0, 8, VPX_CODEC_MEM_ZERO, vp8_priv_sz}, michael@0: {VP8_SEG_MAX, 0, 0, 0, NULL} michael@0: }; michael@0: michael@0: struct vpx_codec_alg_priv michael@0: { michael@0: vpx_codec_priv_t base; michael@0: vpx_codec_mmap_t mmaps[NELEMENTS(vp8_mem_req_segs)-1]; michael@0: vpx_codec_dec_cfg_t cfg; michael@0: vp8_stream_info_t si; michael@0: int defer_alloc; michael@0: int decoder_init; michael@0: int postproc_cfg_set; michael@0: vp8_postproc_cfg_t postproc_cfg; michael@0: #if CONFIG_POSTPROC_VISUALIZER michael@0: unsigned int dbg_postproc_flag; michael@0: int dbg_color_ref_frame_flag; michael@0: int dbg_color_mb_modes_flag; michael@0: int dbg_color_b_modes_flag; michael@0: int dbg_display_mv_flag; michael@0: #endif michael@0: vp8_decrypt_cb *decrypt_cb; michael@0: void *decrypt_state; michael@0: vpx_image_t img; michael@0: int img_setup; michael@0: struct frame_buffers yv12_frame_buffers; michael@0: void *user_priv; michael@0: FRAGMENT_DATA fragments; michael@0: }; michael@0: michael@0: static unsigned long vp8_priv_sz(const vpx_codec_dec_cfg_t *si, vpx_codec_flags_t flags) michael@0: { michael@0: /* Although this declaration is constant, we can't use it in the requested michael@0: * segments list because we want to define the requested segments list michael@0: * before defining the private type (so that the number of memory maps is michael@0: * known) michael@0: */ michael@0: (void)si; michael@0: return sizeof(vpx_codec_alg_priv_t); michael@0: } michael@0: michael@0: static void vp8_init_ctx(vpx_codec_ctx_t *ctx, const vpx_codec_mmap_t *mmap) michael@0: { michael@0: int i; michael@0: michael@0: ctx->priv = mmap->base; michael@0: ctx->priv->sz = sizeof(*ctx->priv); michael@0: ctx->priv->iface = ctx->iface; michael@0: ctx->priv->alg_priv = mmap->base; michael@0: michael@0: for (i = 0; i < NELEMENTS(ctx->priv->alg_priv->mmaps); i++) michael@0: ctx->priv->alg_priv->mmaps[i].id = vp8_mem_req_segs[i].id; michael@0: michael@0: ctx->priv->alg_priv->mmaps[0] = *mmap; michael@0: ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si); michael@0: ctx->priv->alg_priv->decrypt_cb = NULL; michael@0: ctx->priv->alg_priv->decrypt_state = NULL; michael@0: ctx->priv->init_flags = ctx->init_flags; michael@0: michael@0: if (ctx->config.dec) michael@0: { michael@0: /* Update the reference to the config structure to an internal copy. */ michael@0: ctx->priv->alg_priv->cfg = *ctx->config.dec; michael@0: ctx->config.dec = &ctx->priv->alg_priv->cfg; michael@0: } michael@0: } michael@0: michael@0: static void vp8_finalize_mmaps(vpx_codec_alg_priv_t *ctx) michael@0: { michael@0: /* nothing to clean up */ michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_init(vpx_codec_ctx_t *ctx, michael@0: vpx_codec_priv_enc_mr_cfg_t *data) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_OK; michael@0: (void) data; michael@0: michael@0: vp8_rtcd(); michael@0: michael@0: /* This function only allocates space for the vpx_codec_alg_priv_t michael@0: * structure. More memory may be required at the time the stream michael@0: * information becomes known. michael@0: */ michael@0: if (!ctx->priv) michael@0: { michael@0: vpx_codec_mmap_t mmap; michael@0: michael@0: mmap.id = vp8_mem_req_segs[0].id; michael@0: mmap.sz = sizeof(vpx_codec_alg_priv_t); michael@0: mmap.align = vp8_mem_req_segs[0].align; michael@0: mmap.flags = vp8_mem_req_segs[0].flags; michael@0: michael@0: res = vpx_mmap_alloc(&mmap); michael@0: if (res != VPX_CODEC_OK) return res; michael@0: michael@0: vp8_init_ctx(ctx, &mmap); michael@0: michael@0: /* initialize number of fragments to zero */ michael@0: ctx->priv->alg_priv->fragments.count = 0; michael@0: /* is input fragments enabled? */ michael@0: ctx->priv->alg_priv->fragments.enabled = michael@0: (ctx->priv->alg_priv->base.init_flags & michael@0: VPX_CODEC_USE_INPUT_FRAGMENTS); michael@0: michael@0: ctx->priv->alg_priv->defer_alloc = 1; michael@0: /*post processing level initialized to do nothing */ michael@0: } michael@0: michael@0: ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads = michael@0: (ctx->priv->alg_priv->base.init_flags & michael@0: VPX_CODEC_USE_FRAME_THREADING); michael@0: michael@0: /* for now, disable frame threading */ michael@0: ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads = 0; michael@0: michael@0: if(ctx->priv->alg_priv->yv12_frame_buffers.use_frame_threads && michael@0: (( ctx->priv->alg_priv->base.init_flags & michael@0: VPX_CODEC_USE_ERROR_CONCEALMENT) michael@0: || ( ctx->priv->alg_priv->base.init_flags & michael@0: VPX_CODEC_USE_INPUT_FRAGMENTS) ) ) michael@0: { michael@0: /* row-based threading, error concealment, and input fragments will michael@0: * not be supported when using frame-based threading */ michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_destroy(vpx_codec_alg_priv_t *ctx) michael@0: { michael@0: int i; michael@0: michael@0: vp8_remove_decoder_instances(&ctx->yv12_frame_buffers); michael@0: michael@0: for (i = NELEMENTS(ctx->mmaps) - 1; i >= 0; i--) michael@0: { michael@0: if (ctx->mmaps[i].dtor) michael@0: ctx->mmaps[i].dtor(&ctx->mmaps[i]); michael@0: } michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_peek_si_internal(const uint8_t *data, michael@0: unsigned int data_sz, michael@0: vpx_codec_stream_info_t *si, michael@0: vp8_decrypt_cb *decrypt_cb, michael@0: void *decrypt_state) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_OK; michael@0: michael@0: if(data + data_sz <= data) michael@0: { michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: else michael@0: { michael@0: /* Parse uncompresssed part of key frame header. michael@0: * 3 bytes:- including version, frame type and an offset michael@0: * 3 bytes:- sync code (0x9d, 0x01, 0x2a) michael@0: * 4 bytes:- including image width and height in the lowest 14 bits michael@0: * of each 2-byte value. michael@0: */ michael@0: uint8_t clear_buffer[10]; michael@0: const uint8_t *clear = data; michael@0: if (decrypt_cb) michael@0: { michael@0: int n = data_sz > 10 ? 10 : data_sz; michael@0: decrypt_cb(decrypt_state, data, clear_buffer, n); michael@0: clear = clear_buffer; michael@0: } michael@0: si->is_kf = 0; michael@0: michael@0: if (data_sz >= 10 && !(clear[0] & 0x01)) /* I-Frame */ michael@0: { michael@0: si->is_kf = 1; michael@0: michael@0: /* vet via sync code */ michael@0: if (clear[3] != 0x9d || clear[4] != 0x01 || clear[5] != 0x2a) michael@0: res = VPX_CODEC_UNSUP_BITSTREAM; michael@0: michael@0: si->w = (clear[6] | (clear[7] << 8)) & 0x3fff; michael@0: si->h = (clear[8] | (clear[9] << 8)) & 0x3fff; michael@0: michael@0: /*printf("w=%d, h=%d\n", si->w, si->h);*/ michael@0: if (!(si->h | si->w)) michael@0: res = VPX_CODEC_UNSUP_BITSTREAM; michael@0: } michael@0: else michael@0: { michael@0: res = VPX_CODEC_UNSUP_BITSTREAM; michael@0: } michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_peek_si(const uint8_t *data, michael@0: unsigned int data_sz, michael@0: vpx_codec_stream_info_t *si) { michael@0: return vp8_peek_si_internal(data, data_sz, si, NULL, NULL); michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_get_si(vpx_codec_alg_priv_t *ctx, michael@0: vpx_codec_stream_info_t *si) michael@0: { michael@0: michael@0: unsigned int sz; michael@0: michael@0: if (si->sz >= sizeof(vp8_stream_info_t)) michael@0: sz = sizeof(vp8_stream_info_t); michael@0: else michael@0: sz = sizeof(vpx_codec_stream_info_t); michael@0: michael@0: memcpy(si, &ctx->si, sz); michael@0: si->sz = sz; michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: michael@0: static vpx_codec_err_t michael@0: update_error_state(vpx_codec_alg_priv_t *ctx, michael@0: const struct vpx_internal_error_info *error) michael@0: { michael@0: vpx_codec_err_t res; michael@0: michael@0: if ((res = error->error_code)) michael@0: ctx->base.err_detail = error->has_detail michael@0: ? error->detail michael@0: : NULL; michael@0: michael@0: return res; michael@0: } michael@0: michael@0: static void yuvconfig2image(vpx_image_t *img, michael@0: const YV12_BUFFER_CONFIG *yv12, michael@0: void *user_priv) michael@0: { michael@0: /** vpx_img_wrap() doesn't allow specifying independent strides for michael@0: * the Y, U, and V planes, nor other alignment adjustments that michael@0: * might be representable by a YV12_BUFFER_CONFIG, so we just michael@0: * initialize all the fields.*/ michael@0: img->fmt = VPX_IMG_FMT_I420; michael@0: img->w = yv12->y_stride; michael@0: img->h = (yv12->y_height + 2 * VP8BORDERINPIXELS + 15) & ~15; michael@0: img->d_w = yv12->y_width; michael@0: img->d_h = yv12->y_height; michael@0: img->x_chroma_shift = 1; michael@0: img->y_chroma_shift = 1; michael@0: img->planes[VPX_PLANE_Y] = yv12->y_buffer; michael@0: img->planes[VPX_PLANE_U] = yv12->u_buffer; michael@0: img->planes[VPX_PLANE_V] = yv12->v_buffer; michael@0: img->planes[VPX_PLANE_ALPHA] = NULL; michael@0: img->stride[VPX_PLANE_Y] = yv12->y_stride; michael@0: img->stride[VPX_PLANE_U] = yv12->uv_stride; michael@0: img->stride[VPX_PLANE_V] = yv12->uv_stride; michael@0: img->stride[VPX_PLANE_ALPHA] = yv12->y_stride; michael@0: img->bps = 12; michael@0: img->user_priv = user_priv; michael@0: img->img_data = yv12->buffer_alloc; michael@0: img->img_data_owner = 0; michael@0: img->self_allocd = 0; michael@0: } michael@0: michael@0: static int michael@0: update_fragments(vpx_codec_alg_priv_t *ctx, michael@0: const uint8_t *data, michael@0: unsigned int data_sz, michael@0: vpx_codec_err_t *res) michael@0: { michael@0: *res = VPX_CODEC_OK; michael@0: michael@0: if (ctx->fragments.count == 0) michael@0: { michael@0: /* New frame, reset fragment pointers and sizes */ michael@0: vpx_memset((void*)ctx->fragments.ptrs, 0, sizeof(ctx->fragments.ptrs)); michael@0: vpx_memset(ctx->fragments.sizes, 0, sizeof(ctx->fragments.sizes)); michael@0: } michael@0: if (ctx->fragments.enabled && !(data == NULL && data_sz == 0)) michael@0: { michael@0: /* Store a pointer to this fragment and return. We haven't michael@0: * received the complete frame yet, so we will wait with decoding. michael@0: */ michael@0: ctx->fragments.ptrs[ctx->fragments.count] = data; michael@0: ctx->fragments.sizes[ctx->fragments.count] = data_sz; michael@0: ctx->fragments.count++; michael@0: if (ctx->fragments.count > (1 << EIGHT_PARTITION) + 1) michael@0: { michael@0: ctx->fragments.count = 0; michael@0: *res = VPX_CODEC_INVALID_PARAM; michael@0: return -1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: if (!ctx->fragments.enabled) michael@0: { michael@0: ctx->fragments.ptrs[0] = data; michael@0: ctx->fragments.sizes[0] = data_sz; michael@0: ctx->fragments.count = 1; michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_decode(vpx_codec_alg_priv_t *ctx, michael@0: const uint8_t *data, michael@0: unsigned int data_sz, michael@0: void *user_priv, michael@0: long deadline) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_OK; michael@0: unsigned int resolution_change = 0; michael@0: unsigned int w, h; michael@0: michael@0: michael@0: /* Update the input fragment data */ michael@0: if(update_fragments(ctx, data, data_sz, &res) <= 0) michael@0: return res; michael@0: michael@0: /* Determine the stream parameters. Note that we rely on peek_si to michael@0: * validate that we have a buffer that does not wrap around the top michael@0: * of the heap. michael@0: */ michael@0: w = ctx->si.w; michael@0: h = ctx->si.h; michael@0: michael@0: res = vp8_peek_si_internal(ctx->fragments.ptrs[0], ctx->fragments.sizes[0], michael@0: &ctx->si, ctx->decrypt_cb, ctx->decrypt_state); michael@0: michael@0: if((res == VPX_CODEC_UNSUP_BITSTREAM) && !ctx->si.is_kf) michael@0: { michael@0: /* the peek function returns an error for non keyframes, however for michael@0: * this case, it is not an error */ michael@0: res = VPX_CODEC_OK; michael@0: } michael@0: michael@0: if(!ctx->decoder_init && !ctx->si.is_kf) michael@0: res = VPX_CODEC_UNSUP_BITSTREAM; michael@0: michael@0: if ((ctx->si.h != h) || (ctx->si.w != w)) michael@0: resolution_change = 1; michael@0: michael@0: /* Perform deferred allocations, if required */ michael@0: if (!res && ctx->defer_alloc) michael@0: { michael@0: int i; michael@0: michael@0: for (i = 1; !res && i < NELEMENTS(ctx->mmaps); i++) michael@0: { michael@0: vpx_codec_dec_cfg_t cfg; michael@0: michael@0: cfg.w = ctx->si.w; michael@0: cfg.h = ctx->si.h; michael@0: ctx->mmaps[i].id = vp8_mem_req_segs[i].id; michael@0: ctx->mmaps[i].sz = vp8_mem_req_segs[i].sz; michael@0: ctx->mmaps[i].align = vp8_mem_req_segs[i].align; michael@0: ctx->mmaps[i].flags = vp8_mem_req_segs[i].flags; michael@0: michael@0: if (!ctx->mmaps[i].sz) michael@0: ctx->mmaps[i].sz = vp8_mem_req_segs[i].calc_sz(&cfg, michael@0: ctx->base.init_flags); michael@0: michael@0: res = vpx_mmap_alloc(&ctx->mmaps[i]); michael@0: } michael@0: michael@0: if (!res) michael@0: vp8_finalize_mmaps(ctx); michael@0: michael@0: ctx->defer_alloc = 0; michael@0: } michael@0: michael@0: /* Initialize the decoder instance on the first frame*/ michael@0: if (!res && !ctx->decoder_init) michael@0: { michael@0: res = vpx_validate_mmaps(&ctx->si, ctx->mmaps, michael@0: vp8_mem_req_segs, NELEMENTS(vp8_mem_req_segs), michael@0: ctx->base.init_flags); michael@0: michael@0: if (!res) michael@0: { michael@0: VP8D_CONFIG oxcf; michael@0: michael@0: oxcf.Width = ctx->si.w; michael@0: oxcf.Height = ctx->si.h; michael@0: oxcf.Version = 9; michael@0: oxcf.postprocess = 0; michael@0: oxcf.max_threads = ctx->cfg.threads; michael@0: oxcf.error_concealment = michael@0: (ctx->base.init_flags & VPX_CODEC_USE_ERROR_CONCEALMENT); michael@0: michael@0: /* If postprocessing was enabled by the application and a michael@0: * configuration has not been provided, default it. michael@0: */ michael@0: if (!ctx->postproc_cfg_set michael@0: && (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)) michael@0: { michael@0: ctx->postproc_cfg.post_proc_flag = michael@0: VP8_DEBLOCK | VP8_DEMACROBLOCK | VP8_MFQE; michael@0: ctx->postproc_cfg.deblocking_level = 4; michael@0: ctx->postproc_cfg.noise_level = 0; michael@0: } michael@0: michael@0: res = vp8_create_decoder_instances(&ctx->yv12_frame_buffers, &oxcf); michael@0: ctx->yv12_frame_buffers.pbi[0]->decrypt_cb = ctx->decrypt_cb; michael@0: ctx->yv12_frame_buffers.pbi[0]->decrypt_state = ctx->decrypt_state; michael@0: } michael@0: michael@0: ctx->decoder_init = 1; michael@0: } michael@0: michael@0: if (!res) michael@0: { michael@0: VP8D_COMP *pbi = ctx->yv12_frame_buffers.pbi[0]; michael@0: if(resolution_change) michael@0: { michael@0: VP8_COMMON *const pc = & pbi->common; michael@0: MACROBLOCKD *const xd = & pbi->mb; michael@0: #if CONFIG_MULTITHREAD michael@0: int i; michael@0: #endif michael@0: pc->Width = ctx->si.w; michael@0: pc->Height = ctx->si.h; michael@0: { michael@0: int prev_mb_rows = pc->mb_rows; michael@0: michael@0: if (setjmp(pbi->common.error.jmp)) michael@0: { michael@0: pbi->common.error.setjmp = 0; michael@0: /* same return value as used in vp8dx_receive_compressed_data */ michael@0: return -1; michael@0: } michael@0: michael@0: pbi->common.error.setjmp = 1; michael@0: michael@0: if (pc->Width <= 0) michael@0: { michael@0: pc->Width = w; michael@0: vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME, michael@0: "Invalid frame width"); michael@0: } michael@0: michael@0: if (pc->Height <= 0) michael@0: { michael@0: pc->Height = h; michael@0: vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME, michael@0: "Invalid frame height"); michael@0: } michael@0: michael@0: if (vp8_alloc_frame_buffers(pc, pc->Width, pc->Height)) michael@0: vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR, michael@0: "Failed to allocate frame buffers"); michael@0: michael@0: xd->pre = pc->yv12_fb[pc->lst_fb_idx]; michael@0: xd->dst = pc->yv12_fb[pc->new_fb_idx]; michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: for (i = 0; i < pbi->allocated_decoding_thread_count; i++) michael@0: { michael@0: pbi->mb_row_di[i].mbd.dst = pc->yv12_fb[pc->new_fb_idx]; michael@0: vp8_build_block_doffsets(&pbi->mb_row_di[i].mbd); michael@0: } michael@0: #endif michael@0: vp8_build_block_doffsets(&pbi->mb); michael@0: michael@0: /* allocate memory for last frame MODE_INFO array */ michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: michael@0: if (pbi->ec_enabled) michael@0: { michael@0: /* old prev_mip was released by vp8_de_alloc_frame_buffers() michael@0: * called in vp8_alloc_frame_buffers() */ michael@0: pc->prev_mip = vpx_calloc( michael@0: (pc->mb_cols + 1) * (pc->mb_rows + 1), michael@0: sizeof(MODE_INFO)); michael@0: michael@0: if (!pc->prev_mip) michael@0: { michael@0: vp8_de_alloc_frame_buffers(pc); michael@0: vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR, michael@0: "Failed to allocate" michael@0: "last frame MODE_INFO array"); michael@0: } michael@0: michael@0: pc->prev_mi = pc->prev_mip + pc->mode_info_stride + 1; michael@0: michael@0: if (vp8_alloc_overlap_lists(pbi)) michael@0: vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR, michael@0: "Failed to allocate overlap lists " michael@0: "for error concealment"); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: if (pbi->b_multithreaded_rd) michael@0: vp8mt_alloc_temp_buffers(pbi, pc->Width, prev_mb_rows); michael@0: #else michael@0: (void)prev_mb_rows; michael@0: #endif michael@0: } michael@0: michael@0: pbi->common.error.setjmp = 0; michael@0: michael@0: /* required to get past the first get_free_fb() call */ michael@0: pbi->common.fb_idx_ref_cnt[0] = 0; michael@0: } michael@0: michael@0: /* update the pbi fragment data */ michael@0: pbi->fragments = ctx->fragments; michael@0: michael@0: ctx->user_priv = user_priv; michael@0: if (vp8dx_receive_compressed_data(pbi, data_sz, data, deadline)) michael@0: { michael@0: res = update_error_state(ctx, &pbi->common.error); michael@0: } michael@0: michael@0: /* get ready for the next series of fragments */ michael@0: ctx->fragments.count = 0; michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: static vpx_image_t *vp8_get_frame(vpx_codec_alg_priv_t *ctx, michael@0: vpx_codec_iter_t *iter) michael@0: { michael@0: vpx_image_t *img = NULL; michael@0: michael@0: /* iter acts as a flip flop, so an image is only returned on the first michael@0: * call to get_frame. michael@0: */ michael@0: if (!(*iter) && ctx->yv12_frame_buffers.pbi[0]) michael@0: { michael@0: YV12_BUFFER_CONFIG sd; michael@0: int64_t time_stamp = 0, time_end_stamp = 0; michael@0: vp8_ppflags_t flags = {0}; michael@0: michael@0: if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC) michael@0: { michael@0: flags.post_proc_flag= ctx->postproc_cfg.post_proc_flag michael@0: #if CONFIG_POSTPROC_VISUALIZER michael@0: michael@0: | ((ctx->dbg_color_ref_frame_flag != 0) ? VP8D_DEBUG_CLR_FRM_REF_BLKS : 0) michael@0: | ((ctx->dbg_color_mb_modes_flag != 0) ? VP8D_DEBUG_CLR_BLK_MODES : 0) michael@0: | ((ctx->dbg_color_b_modes_flag != 0) ? VP8D_DEBUG_CLR_BLK_MODES : 0) michael@0: | ((ctx->dbg_display_mv_flag != 0) ? VP8D_DEBUG_DRAW_MV : 0) michael@0: #endif michael@0: ; michael@0: flags.deblocking_level = ctx->postproc_cfg.deblocking_level; michael@0: flags.noise_level = ctx->postproc_cfg.noise_level; michael@0: #if CONFIG_POSTPROC_VISUALIZER michael@0: flags.display_ref_frame_flag= ctx->dbg_color_ref_frame_flag; michael@0: flags.display_mb_modes_flag = ctx->dbg_color_mb_modes_flag; michael@0: flags.display_b_modes_flag = ctx->dbg_color_b_modes_flag; michael@0: flags.display_mv_flag = ctx->dbg_display_mv_flag; michael@0: #endif michael@0: } michael@0: michael@0: if (0 == vp8dx_get_raw_frame(ctx->yv12_frame_buffers.pbi[0], &sd, michael@0: &time_stamp, &time_end_stamp, &flags)) michael@0: { michael@0: yuvconfig2image(&ctx->img, &sd, ctx->user_priv); michael@0: michael@0: img = &ctx->img; michael@0: *iter = img; michael@0: } michael@0: } michael@0: michael@0: return img; michael@0: } michael@0: michael@0: michael@0: static michael@0: vpx_codec_err_t vp8_xma_get_mmap(const vpx_codec_ctx_t *ctx, michael@0: vpx_codec_mmap_t *mmap, michael@0: vpx_codec_iter_t *iter) michael@0: { michael@0: vpx_codec_err_t res; michael@0: const mem_req_t *seg_iter = *iter; michael@0: michael@0: /* Get address of next segment request */ michael@0: do michael@0: { michael@0: if (!seg_iter) michael@0: seg_iter = vp8_mem_req_segs; michael@0: else if (seg_iter->id != VP8_SEG_MAX) michael@0: seg_iter++; michael@0: michael@0: *iter = (vpx_codec_iter_t)seg_iter; michael@0: michael@0: if (seg_iter->id != VP8_SEG_MAX) michael@0: { michael@0: mmap->id = seg_iter->id; michael@0: mmap->sz = seg_iter->sz; michael@0: mmap->align = seg_iter->align; michael@0: mmap->flags = seg_iter->flags; michael@0: michael@0: if (!seg_iter->sz) michael@0: mmap->sz = seg_iter->calc_sz(ctx->config.dec, ctx->init_flags); michael@0: michael@0: res = VPX_CODEC_OK; michael@0: } michael@0: else michael@0: res = VPX_CODEC_LIST_END; michael@0: } michael@0: while (!mmap->sz && res != VPX_CODEC_LIST_END); michael@0: michael@0: return res; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_xma_set_mmap(vpx_codec_ctx_t *ctx, michael@0: const vpx_codec_mmap_t *mmap) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_MEM_ERROR; michael@0: int i, done; michael@0: michael@0: if (!ctx->priv) michael@0: { michael@0: if (mmap->id == VP8_SEG_ALG_PRIV) michael@0: { michael@0: if (!ctx->priv) michael@0: { michael@0: vp8_init_ctx(ctx, mmap); michael@0: res = VPX_CODEC_OK; michael@0: } michael@0: } michael@0: } michael@0: michael@0: done = 1; michael@0: michael@0: if (!res && ctx->priv->alg_priv) michael@0: { michael@0: for (i = 0; i < NELEMENTS(ctx->priv->alg_priv->mmaps); i++) michael@0: { michael@0: if (ctx->priv->alg_priv->mmaps[i].id == mmap->id) michael@0: if (!ctx->priv->alg_priv->mmaps[i].base) michael@0: { michael@0: ctx->priv->alg_priv->mmaps[i] = *mmap; michael@0: res = VPX_CODEC_OK; michael@0: } michael@0: michael@0: done &= (ctx->priv->alg_priv->mmaps[i].base != NULL); michael@0: } michael@0: } michael@0: michael@0: if (done && !res) michael@0: { michael@0: vp8_finalize_mmaps(ctx->priv->alg_priv); michael@0: res = ctx->iface->init(ctx, NULL); michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: static vpx_codec_err_t image2yuvconfig(const vpx_image_t *img, michael@0: YV12_BUFFER_CONFIG *yv12) michael@0: { michael@0: vpx_codec_err_t res = VPX_CODEC_OK; michael@0: yv12->y_buffer = img->planes[VPX_PLANE_Y]; michael@0: yv12->u_buffer = img->planes[VPX_PLANE_U]; michael@0: yv12->v_buffer = img->planes[VPX_PLANE_V]; michael@0: michael@0: yv12->y_crop_width = img->d_w; michael@0: yv12->y_crop_height = img->d_h; michael@0: yv12->y_width = img->d_w; michael@0: yv12->y_height = img->d_h; michael@0: yv12->uv_width = yv12->y_width / 2; michael@0: yv12->uv_height = yv12->y_height / 2; michael@0: michael@0: yv12->y_stride = img->stride[VPX_PLANE_Y]; michael@0: yv12->uv_stride = img->stride[VPX_PLANE_U]; michael@0: michael@0: yv12->border = (img->stride[VPX_PLANE_Y] - img->d_w) / 2; michael@0: return res; michael@0: } michael@0: michael@0: michael@0: static vpx_codec_err_t vp8_set_reference(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: michael@0: vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *); michael@0: michael@0: if (data && !ctx->yv12_frame_buffers.use_frame_threads) michael@0: { michael@0: vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data; michael@0: YV12_BUFFER_CONFIG sd; michael@0: michael@0: image2yuvconfig(&frame->img, &sd); michael@0: michael@0: return vp8dx_set_reference(ctx->yv12_frame_buffers.pbi[0], michael@0: frame->frame_type, &sd); michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_get_reference(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: michael@0: vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *); michael@0: michael@0: if (data && !ctx->yv12_frame_buffers.use_frame_threads) michael@0: { michael@0: vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data; michael@0: YV12_BUFFER_CONFIG sd; michael@0: michael@0: image2yuvconfig(&frame->img, &sd); michael@0: michael@0: return vp8dx_get_reference(ctx->yv12_frame_buffers.pbi[0], michael@0: frame->frame_type, &sd); michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_set_postproc(vpx_codec_alg_priv_t *ctx, michael@0: int ctr_id, michael@0: va_list args) michael@0: { michael@0: #if CONFIG_POSTPROC michael@0: vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *); michael@0: michael@0: if (data) michael@0: { michael@0: ctx->postproc_cfg_set = 1; michael@0: ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data); michael@0: return VPX_CODEC_OK; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: #else michael@0: return VPX_CODEC_INCAPABLE; michael@0: #endif michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_set_dbg_options(vpx_codec_alg_priv_t *ctx, michael@0: int ctrl_id, michael@0: va_list args) michael@0: { michael@0: #if CONFIG_POSTPROC_VISUALIZER && CONFIG_POSTPROC michael@0: int data = va_arg(args, int); michael@0: michael@0: #define MAP(id, var) case id: var = data; break; michael@0: michael@0: switch (ctrl_id) michael@0: { michael@0: MAP (VP8_SET_DBG_COLOR_REF_FRAME, ctx->dbg_color_ref_frame_flag); michael@0: MAP (VP8_SET_DBG_COLOR_MB_MODES, ctx->dbg_color_mb_modes_flag); michael@0: MAP (VP8_SET_DBG_COLOR_B_MODES, ctx->dbg_color_b_modes_flag); michael@0: MAP (VP8_SET_DBG_DISPLAY_MV, ctx->dbg_display_mv_flag); michael@0: } michael@0: michael@0: return VPX_CODEC_OK; michael@0: #else michael@0: return VPX_CODEC_INCAPABLE; michael@0: #endif michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_get_last_ref_updates(vpx_codec_alg_priv_t *ctx, michael@0: int ctrl_id, michael@0: va_list args) michael@0: { michael@0: int *update_info = va_arg(args, int *); michael@0: michael@0: if (update_info && !ctx->yv12_frame_buffers.use_frame_threads) michael@0: { michael@0: VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0]; michael@0: michael@0: *update_info = pbi->common.refresh_alt_ref_frame * (int) VP8_ALTR_FRAME michael@0: + pbi->common.refresh_golden_frame * (int) VP8_GOLD_FRAME michael@0: + pbi->common.refresh_last_frame * (int) VP8_LAST_FRAME; michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: extern int vp8dx_references_buffer( VP8_COMMON *oci, int ref_frame ); michael@0: static vpx_codec_err_t vp8_get_last_ref_frame(vpx_codec_alg_priv_t *ctx, michael@0: int ctrl_id, michael@0: va_list args) michael@0: { michael@0: int *ref_info = va_arg(args, int *); michael@0: michael@0: if (ref_info && !ctx->yv12_frame_buffers.use_frame_threads) michael@0: { michael@0: VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0]; michael@0: VP8_COMMON *oci = &pbi->common; michael@0: *ref_info = michael@0: (vp8dx_references_buffer( oci, ALTREF_FRAME )?VP8_ALTR_FRAME:0) | michael@0: (vp8dx_references_buffer( oci, GOLDEN_FRAME )?VP8_GOLD_FRAME:0) | michael@0: (vp8dx_references_buffer( oci, LAST_FRAME )?VP8_LAST_FRAME:0); michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_get_frame_corrupted(vpx_codec_alg_priv_t *ctx, michael@0: int ctrl_id, michael@0: va_list args) michael@0: { michael@0: michael@0: int *corrupted = va_arg(args, int *); michael@0: VP8D_COMP *pbi = (VP8D_COMP *)ctx->yv12_frame_buffers.pbi[0]; michael@0: michael@0: if (corrupted && pbi) michael@0: { michael@0: *corrupted = pbi->common.frame_to_show->corrupted; michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: else michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: } michael@0: michael@0: static vpx_codec_err_t vp8_set_decryptor(vpx_codec_alg_priv_t *ctx, michael@0: int ctrl_id, michael@0: va_list args) michael@0: { michael@0: vp8_decrypt_init *init = va_arg(args, vp8_decrypt_init *); michael@0: michael@0: if (init) michael@0: { michael@0: ctx->decrypt_cb = init->decrypt_cb; michael@0: ctx->decrypt_state = init->decrypt_state; michael@0: } michael@0: else michael@0: { michael@0: ctx->decrypt_cb = NULL; michael@0: ctx->decrypt_state = NULL; michael@0: } michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: vpx_codec_ctrl_fn_map_t vp8_ctf_maps[] = michael@0: { michael@0: {VP8_SET_REFERENCE, vp8_set_reference}, michael@0: {VP8_COPY_REFERENCE, vp8_get_reference}, michael@0: {VP8_SET_POSTPROC, vp8_set_postproc}, michael@0: {VP8_SET_DBG_COLOR_REF_FRAME, vp8_set_dbg_options}, michael@0: {VP8_SET_DBG_COLOR_MB_MODES, vp8_set_dbg_options}, michael@0: {VP8_SET_DBG_COLOR_B_MODES, vp8_set_dbg_options}, michael@0: {VP8_SET_DBG_DISPLAY_MV, vp8_set_dbg_options}, michael@0: {VP8D_GET_LAST_REF_UPDATES, vp8_get_last_ref_updates}, michael@0: {VP8D_GET_FRAME_CORRUPTED, vp8_get_frame_corrupted}, michael@0: {VP8D_GET_LAST_REF_USED, vp8_get_last_ref_frame}, michael@0: {VP8D_SET_DECRYPTOR, vp8_set_decryptor}, michael@0: { -1, NULL}, michael@0: }; michael@0: michael@0: michael@0: #ifndef VERSION_STRING michael@0: #define VERSION_STRING michael@0: #endif michael@0: CODEC_INTERFACE(vpx_codec_vp8_dx) = michael@0: { michael@0: "WebM Project VP8 Decoder" VERSION_STRING, michael@0: VPX_CODEC_INTERNAL_ABI_VERSION, michael@0: VPX_CODEC_CAP_DECODER | VP8_CAP_POSTPROC | VP8_CAP_ERROR_CONCEALMENT | michael@0: VPX_CODEC_CAP_INPUT_FRAGMENTS, michael@0: /* vpx_codec_caps_t caps; */ michael@0: vp8_init, /* vpx_codec_init_fn_t init; */ michael@0: vp8_destroy, /* vpx_codec_destroy_fn_t destroy; */ michael@0: vp8_ctf_maps, /* vpx_codec_ctrl_fn_map_t *ctrl_maps; */ michael@0: vp8_xma_get_mmap, /* vpx_codec_get_mmap_fn_t get_mmap; */ michael@0: vp8_xma_set_mmap, /* vpx_codec_set_mmap_fn_t set_mmap; */ michael@0: { michael@0: vp8_peek_si, /* vpx_codec_peek_si_fn_t peek_si; */ michael@0: vp8_get_si, /* vpx_codec_get_si_fn_t get_si; */ michael@0: vp8_decode, /* vpx_codec_decode_fn_t decode; */ michael@0: vp8_get_frame, /* vpx_codec_frame_get_fn_t frame_get; */ michael@0: }, michael@0: { /* encoder functions */ michael@0: NOT_IMPLEMENTED, michael@0: NOT_IMPLEMENTED, michael@0: NOT_IMPLEMENTED, michael@0: NOT_IMPLEMENTED, michael@0: NOT_IMPLEMENTED, michael@0: NOT_IMPLEMENTED michael@0: } michael@0: };