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: /*!\file michael@0: * \brief Provides the high level interface to wrap decoder algorithms. michael@0: * michael@0: */ michael@0: #include michael@0: #include michael@0: #include "vpx/vpx_integer.h" michael@0: #include "vpx/internal/vpx_codec_internal.h" michael@0: #include "vpx_version.h" michael@0: michael@0: #define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var) michael@0: michael@0: int vpx_codec_version(void) { michael@0: return VERSION_PACKED; michael@0: } michael@0: michael@0: michael@0: const char *vpx_codec_version_str(void) { michael@0: return VERSION_STRING_NOSP; michael@0: } michael@0: michael@0: michael@0: const char *vpx_codec_version_extra_str(void) { michael@0: return VERSION_EXTRA; michael@0: } michael@0: michael@0: michael@0: const char *vpx_codec_iface_name(vpx_codec_iface_t *iface) { michael@0: return iface ? iface->name : ""; michael@0: } michael@0: michael@0: const char *vpx_codec_err_to_string(vpx_codec_err_t err) { michael@0: switch (err) { michael@0: case VPX_CODEC_OK: michael@0: return "Success"; michael@0: case VPX_CODEC_ERROR: michael@0: return "Unspecified internal error"; michael@0: case VPX_CODEC_MEM_ERROR: michael@0: return "Memory allocation error"; michael@0: case VPX_CODEC_ABI_MISMATCH: michael@0: return "ABI version mismatch"; michael@0: case VPX_CODEC_INCAPABLE: michael@0: return "Codec does not implement requested capability"; michael@0: case VPX_CODEC_UNSUP_BITSTREAM: michael@0: return "Bitstream not supported by this decoder"; michael@0: case VPX_CODEC_UNSUP_FEATURE: michael@0: return "Bitstream required feature not supported by this decoder"; michael@0: case VPX_CODEC_CORRUPT_FRAME: michael@0: return "Corrupt frame detected"; michael@0: case VPX_CODEC_INVALID_PARAM: michael@0: return "Invalid parameter"; michael@0: case VPX_CODEC_LIST_END: michael@0: return "End of iterated list"; michael@0: } michael@0: michael@0: return "Unrecognized error code"; michael@0: } michael@0: michael@0: const char *vpx_codec_error(vpx_codec_ctx_t *ctx) { michael@0: return (ctx) ? vpx_codec_err_to_string(ctx->err) michael@0: : vpx_codec_err_to_string(VPX_CODEC_INVALID_PARAM); michael@0: } michael@0: michael@0: const char *vpx_codec_error_detail(vpx_codec_ctx_t *ctx) { michael@0: if (ctx && ctx->err) michael@0: return ctx->priv ? ctx->priv->err_detail : ctx->err_detail; michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: michael@0: vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx) { michael@0: vpx_codec_err_t res; michael@0: michael@0: if (!ctx) michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: else if (!ctx->iface || !ctx->priv) michael@0: res = VPX_CODEC_ERROR; michael@0: else { michael@0: if (ctx->priv->alg_priv) michael@0: ctx->iface->destroy(ctx->priv->alg_priv); michael@0: michael@0: ctx->iface = NULL; michael@0: ctx->name = NULL; michael@0: ctx->priv = NULL; michael@0: res = VPX_CODEC_OK; michael@0: } michael@0: michael@0: return SAVE_STATUS(ctx, res); michael@0: } michael@0: michael@0: michael@0: vpx_codec_caps_t vpx_codec_get_caps(vpx_codec_iface_t *iface) { michael@0: return (iface) ? iface->caps : 0; michael@0: } michael@0: michael@0: michael@0: vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx, michael@0: int ctrl_id, michael@0: ...) { michael@0: vpx_codec_err_t res; michael@0: michael@0: if (!ctx || !ctrl_id) michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: else if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps) michael@0: res = VPX_CODEC_ERROR; michael@0: else { michael@0: vpx_codec_ctrl_fn_map_t *entry; michael@0: michael@0: res = VPX_CODEC_ERROR; michael@0: michael@0: for (entry = ctx->iface->ctrl_maps; entry && entry->fn; entry++) { michael@0: if (!entry->ctrl_id || entry->ctrl_id == ctrl_id) { michael@0: va_list ap; michael@0: michael@0: va_start(ap, ctrl_id); michael@0: res = entry->fn(ctx->priv->alg_priv, ctrl_id, ap); michael@0: va_end(ap); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return SAVE_STATUS(ctx, res); michael@0: } michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // mmap interface michael@0: michael@0: vpx_codec_err_t vpx_mmap_alloc(vpx_codec_mmap_t *mmap) { michael@0: unsigned int align = mmap->align ? mmap->align - 1 : 0; michael@0: michael@0: if (mmap->flags & VPX_CODEC_MEM_ZERO) michael@0: mmap->priv = calloc(1, mmap->sz + align); michael@0: else michael@0: mmap->priv = malloc(mmap->sz + align); michael@0: michael@0: if (mmap->priv == NULL) return VPX_CODEC_MEM_ERROR; michael@0: mmap->base = (void *)((((uintptr_t)mmap->priv) + align) & ~(uintptr_t)align); michael@0: mmap->dtor = vpx_mmap_dtor; michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: void vpx_mmap_dtor(vpx_codec_mmap_t *mmap) { michael@0: free(mmap->priv); michael@0: } michael@0: michael@0: vpx_codec_err_t vpx_validate_mmaps(const vpx_codec_stream_info_t *si, michael@0: const vpx_codec_mmap_t *mmaps, michael@0: const mem_req_t *mem_reqs, int nreqs, michael@0: vpx_codec_flags_t init_flags) { michael@0: int i; michael@0: michael@0: for (i = 0; i < nreqs - 1; ++i) { michael@0: /* Ensure the segment has been allocated */ michael@0: if (mmaps[i].base == NULL) { michael@0: return VPX_CODEC_MEM_ERROR; michael@0: } michael@0: michael@0: /* Verify variable size segment is big enough for the current si. */ michael@0: if (mem_reqs[i].calc_sz != NULL) { michael@0: vpx_codec_dec_cfg_t cfg; michael@0: michael@0: cfg.w = si->w; michael@0: cfg.h = si->h; michael@0: michael@0: if (mmaps[i].sz < mem_reqs[i].calc_sz(&cfg, init_flags)) { michael@0: return VPX_CODEC_MEM_ERROR; michael@0: } michael@0: } michael@0: } michael@0: return VPX_CODEC_OK; michael@0: }