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 encoder algorithms. michael@0: * michael@0: */ michael@0: #include michael@0: #include michael@0: #include "vpx/internal/vpx_codec_internal.h" michael@0: #include "vpx_config.h" michael@0: michael@0: #define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var) michael@0: michael@0: vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t *ctx, michael@0: vpx_codec_iface_t *iface, michael@0: vpx_codec_enc_cfg_t *cfg, michael@0: vpx_codec_flags_t flags, michael@0: int ver) { michael@0: vpx_codec_err_t res; michael@0: michael@0: if (ver != VPX_ENCODER_ABI_VERSION) michael@0: res = VPX_CODEC_ABI_MISMATCH; michael@0: else if (!ctx || !iface || !cfg) michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION) michael@0: res = VPX_CODEC_ABI_MISMATCH; michael@0: else if (!(iface->caps & VPX_CODEC_CAP_ENCODER)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else if ((flags & VPX_CODEC_USE_XMA) && !(iface->caps & VPX_CODEC_CAP_XMA)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else if ((flags & VPX_CODEC_USE_PSNR) michael@0: && !(iface->caps & VPX_CODEC_CAP_PSNR)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION) michael@0: && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else { michael@0: ctx->iface = iface; michael@0: ctx->name = iface->name; michael@0: ctx->priv = NULL; michael@0: ctx->init_flags = flags; michael@0: ctx->config.enc = cfg; michael@0: res = ctx->iface->init(ctx, NULL); michael@0: michael@0: if (res) { michael@0: ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL; michael@0: vpx_codec_destroy(ctx); michael@0: } michael@0: michael@0: if (ctx->priv) michael@0: ctx->priv->iface = ctx->iface; michael@0: } michael@0: michael@0: return SAVE_STATUS(ctx, res); michael@0: } michael@0: michael@0: vpx_codec_err_t vpx_codec_enc_init_multi_ver(vpx_codec_ctx_t *ctx, michael@0: vpx_codec_iface_t *iface, michael@0: vpx_codec_enc_cfg_t *cfg, michael@0: int num_enc, michael@0: vpx_codec_flags_t flags, michael@0: vpx_rational_t *dsf, michael@0: int ver) { michael@0: vpx_codec_err_t res = 0; michael@0: michael@0: if (ver != VPX_ENCODER_ABI_VERSION) michael@0: res = VPX_CODEC_ABI_MISMATCH; michael@0: else if (!ctx || !iface || !cfg || (num_enc > 16 || num_enc < 1)) michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION) michael@0: res = VPX_CODEC_ABI_MISMATCH; michael@0: else if (!(iface->caps & VPX_CODEC_CAP_ENCODER)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else if ((flags & VPX_CODEC_USE_XMA) && !(iface->caps & VPX_CODEC_CAP_XMA)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else if ((flags & VPX_CODEC_USE_PSNR) michael@0: && !(iface->caps & VPX_CODEC_CAP_PSNR)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION) michael@0: && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else { michael@0: int i; michael@0: void *mem_loc = NULL; michael@0: michael@0: if (!(res = iface->enc.mr_get_mem_loc(cfg, &mem_loc))) { michael@0: for (i = 0; i < num_enc; i++) { michael@0: vpx_codec_priv_enc_mr_cfg_t mr_cfg; michael@0: michael@0: /* Validate down-sampling factor. */ michael@0: if (dsf->num < 1 || dsf->num > 4096 || dsf->den < 1 || michael@0: dsf->den > dsf->num) { michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: break; michael@0: } michael@0: michael@0: mr_cfg.mr_low_res_mode_info = mem_loc; michael@0: mr_cfg.mr_total_resolutions = num_enc; michael@0: mr_cfg.mr_encoder_id = num_enc - 1 - i; michael@0: mr_cfg.mr_down_sampling_factor.num = dsf->num; michael@0: mr_cfg.mr_down_sampling_factor.den = dsf->den; michael@0: michael@0: /* Force Key-frame synchronization. Namely, encoder at higher michael@0: * resolution always use the same frame_type chosen by the michael@0: * lowest-resolution encoder. michael@0: */ michael@0: if (mr_cfg.mr_encoder_id) michael@0: cfg->kf_mode = VPX_KF_DISABLED; michael@0: michael@0: ctx->iface = iface; michael@0: ctx->name = iface->name; michael@0: ctx->priv = NULL; michael@0: ctx->init_flags = flags; michael@0: ctx->config.enc = cfg; michael@0: res = ctx->iface->init(ctx, &mr_cfg); michael@0: michael@0: if (res) { michael@0: const char *error_detail = michael@0: ctx->priv ? ctx->priv->err_detail : NULL; michael@0: /* Destroy current ctx */ michael@0: ctx->err_detail = error_detail; michael@0: vpx_codec_destroy(ctx); michael@0: michael@0: /* Destroy already allocated high-level ctx */ michael@0: while (i) { michael@0: ctx--; michael@0: ctx->err_detail = error_detail; michael@0: vpx_codec_destroy(ctx); michael@0: i--; michael@0: } michael@0: } michael@0: michael@0: if (ctx->priv) michael@0: ctx->priv->iface = ctx->iface; michael@0: michael@0: if (res) michael@0: break; michael@0: michael@0: ctx++; michael@0: cfg++; michael@0: dsf++; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return SAVE_STATUS(ctx, res); michael@0: } michael@0: michael@0: michael@0: vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, michael@0: vpx_codec_enc_cfg_t *cfg, michael@0: unsigned int usage) { michael@0: vpx_codec_err_t res; michael@0: vpx_codec_enc_cfg_map_t *map; michael@0: michael@0: if (!iface || !cfg || usage > INT_MAX) michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: else if (!(iface->caps & VPX_CODEC_CAP_ENCODER)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else { michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: michael@0: for (map = iface->enc.cfg_maps; map->usage >= 0; map++) { michael@0: if (map->usage == (int)usage) { michael@0: *cfg = map->cfg; michael@0: cfg->g_usage = usage; michael@0: res = VPX_CODEC_OK; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: michael@0: #if ARCH_X86 || ARCH_X86_64 michael@0: /* On X86, disable the x87 unit's internal 80 bit precision for better michael@0: * consistency with the SSE unit's 64 bit precision. michael@0: */ michael@0: #include "vpx_ports/x86.h" michael@0: #define FLOATING_POINT_INIT() do {\ michael@0: unsigned short x87_orig_mode = x87_set_double_precision(); michael@0: #define FLOATING_POINT_RESTORE() \ michael@0: x87_set_control_word(x87_orig_mode); }while(0) michael@0: michael@0: michael@0: #else michael@0: static void FLOATING_POINT_INIT() {} michael@0: static void FLOATING_POINT_RESTORE() {} michael@0: #endif michael@0: michael@0: michael@0: vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, michael@0: const vpx_image_t *img, michael@0: vpx_codec_pts_t pts, michael@0: unsigned long duration, michael@0: vpx_enc_frame_flags_t flags, michael@0: unsigned long deadline) { michael@0: vpx_codec_err_t res = 0; michael@0: michael@0: if (!ctx || (img && !duration)) michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: else if (!ctx->iface || !ctx->priv) michael@0: res = VPX_CODEC_ERROR; michael@0: else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else { michael@0: unsigned int num_enc = ctx->priv->enc.total_encoders; michael@0: michael@0: /* Execute in a normalized floating point environment, if the platform michael@0: * requires it. michael@0: */ michael@0: FLOATING_POINT_INIT(); michael@0: michael@0: if (num_enc == 1) michael@0: res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts, michael@0: duration, flags, deadline); michael@0: else { michael@0: /* Multi-resolution encoding: michael@0: * Encode multi-levels in reverse order. For example, michael@0: * if mr_total_resolutions = 3, first encode level 2, michael@0: * then encode level 1, and finally encode level 0. michael@0: */ michael@0: int i; michael@0: michael@0: ctx += num_enc - 1; michael@0: if (img) img += num_enc - 1; michael@0: michael@0: for (i = num_enc - 1; i >= 0; i--) { michael@0: if ((res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts, michael@0: duration, flags, deadline))) michael@0: break; michael@0: michael@0: ctx--; michael@0: if (img) img--; michael@0: } michael@0: ctx++; michael@0: } michael@0: michael@0: FLOATING_POINT_RESTORE(); michael@0: } michael@0: michael@0: return SAVE_STATUS(ctx, res); michael@0: } michael@0: michael@0: michael@0: const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, michael@0: vpx_codec_iter_t *iter) { michael@0: const vpx_codec_cx_pkt_t *pkt = NULL; michael@0: michael@0: if (ctx) { michael@0: if (!iter) michael@0: ctx->err = VPX_CODEC_INVALID_PARAM; michael@0: else if (!ctx->iface || !ctx->priv) michael@0: ctx->err = VPX_CODEC_ERROR; michael@0: else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) michael@0: ctx->err = VPX_CODEC_INCAPABLE; michael@0: else michael@0: pkt = ctx->iface->enc.get_cx_data(ctx->priv->alg_priv, iter); michael@0: } michael@0: michael@0: if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT) { michael@0: /* If the application has specified a destination area for the michael@0: * compressed data, and the codec has not placed the data there, michael@0: * and it fits, copy it. michael@0: */ michael@0: char *dst_buf = ctx->priv->enc.cx_data_dst_buf.buf; michael@0: michael@0: if (dst_buf michael@0: && pkt->data.raw.buf != dst_buf michael@0: && pkt->data.raw.sz michael@0: + ctx->priv->enc.cx_data_pad_before michael@0: + ctx->priv->enc.cx_data_pad_after michael@0: <= ctx->priv->enc.cx_data_dst_buf.sz) { michael@0: vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt; michael@0: michael@0: memcpy(dst_buf + ctx->priv->enc.cx_data_pad_before, michael@0: pkt->data.raw.buf, pkt->data.raw.sz); michael@0: *modified_pkt = *pkt; michael@0: modified_pkt->data.raw.buf = dst_buf; michael@0: modified_pkt->data.raw.sz += ctx->priv->enc.cx_data_pad_before michael@0: + ctx->priv->enc.cx_data_pad_after; michael@0: pkt = modified_pkt; michael@0: } michael@0: michael@0: if (dst_buf == pkt->data.raw.buf) { michael@0: ctx->priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz; michael@0: ctx->priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz; michael@0: } michael@0: } michael@0: michael@0: return pkt; michael@0: } michael@0: michael@0: michael@0: vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx, michael@0: const vpx_fixed_buf_t *buf, michael@0: unsigned int pad_before, michael@0: unsigned int pad_after) { michael@0: if (!ctx || !ctx->priv) michael@0: return VPX_CODEC_INVALID_PARAM; michael@0: michael@0: if (buf) { michael@0: ctx->priv->enc.cx_data_dst_buf = *buf; michael@0: ctx->priv->enc.cx_data_pad_before = pad_before; michael@0: ctx->priv->enc.cx_data_pad_after = pad_after; michael@0: } else { michael@0: ctx->priv->enc.cx_data_dst_buf.buf = NULL; michael@0: ctx->priv->enc.cx_data_dst_buf.sz = 0; michael@0: ctx->priv->enc.cx_data_pad_before = 0; michael@0: ctx->priv->enc.cx_data_pad_after = 0; michael@0: } michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: michael@0: const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx) { michael@0: vpx_image_t *img = NULL; michael@0: michael@0: if (ctx) { michael@0: if (!ctx->iface || !ctx->priv) michael@0: ctx->err = VPX_CODEC_ERROR; michael@0: else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) michael@0: ctx->err = VPX_CODEC_INCAPABLE; michael@0: else if (!ctx->iface->enc.get_preview) michael@0: ctx->err = VPX_CODEC_INCAPABLE; michael@0: else michael@0: img = ctx->iface->enc.get_preview(ctx->priv->alg_priv); michael@0: } michael@0: michael@0: return img; michael@0: } michael@0: michael@0: michael@0: vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx) { michael@0: vpx_fixed_buf_t *buf = NULL; michael@0: michael@0: if (ctx) { michael@0: if (!ctx->iface || !ctx->priv) michael@0: ctx->err = VPX_CODEC_ERROR; michael@0: else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) michael@0: ctx->err = VPX_CODEC_INCAPABLE; michael@0: else if (!ctx->iface->enc.get_glob_hdrs) michael@0: ctx->err = VPX_CODEC_INCAPABLE; michael@0: else michael@0: buf = ctx->iface->enc.get_glob_hdrs(ctx->priv->alg_priv); michael@0: } michael@0: michael@0: return buf; michael@0: } michael@0: michael@0: michael@0: vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx, michael@0: const vpx_codec_enc_cfg_t *cfg) { michael@0: vpx_codec_err_t res; michael@0: michael@0: if (!ctx || !ctx->iface || !ctx->priv || !cfg) michael@0: res = VPX_CODEC_INVALID_PARAM; michael@0: else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) michael@0: res = VPX_CODEC_INCAPABLE; michael@0: else michael@0: res = ctx->iface->enc.cfg_set(ctx->priv->alg_priv, cfg); michael@0: michael@0: return SAVE_STATUS(ctx, res); michael@0: } michael@0: michael@0: michael@0: int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list, michael@0: const struct vpx_codec_cx_pkt *pkt) { michael@0: if (list->cnt < list->max) { michael@0: list->pkts[list->cnt++] = *pkt; michael@0: return 0; michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: michael@0: const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list, michael@0: vpx_codec_iter_t *iter) { michael@0: const vpx_codec_cx_pkt_t *pkt; michael@0: michael@0: if (!(*iter)) { michael@0: *iter = list->pkts; michael@0: } michael@0: michael@0: pkt = (const void *) * iter; michael@0: michael@0: if ((size_t)(pkt - list->pkts) < list->cnt) michael@0: *iter = pkt + 1; michael@0: else michael@0: pkt = NULL; michael@0: michael@0: return pkt; michael@0: }