2016-02-23 14:08:26 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!\file
|
|
|
|
* \brief Provides the high level interface to wrap encoder algorithms.
|
|
|
|
*
|
|
|
|
*/
|
2019-01-17 20:16:34 +00:00
|
|
|
#include <assert.h>
|
2016-02-23 14:08:26 -05:00
|
|
|
#include <limits.h>
|
2019-01-17 20:16:34 +00:00
|
|
|
#include <stdlib.h>
|
2016-02-23 14:08:26 -05:00
|
|
|
#include <string.h>
|
2019-01-17 20:16:34 +00:00
|
|
|
#include "vp8/common/blockd.h"
|
2016-02-23 14:08:26 -05:00
|
|
|
#include "vpx_config.h"
|
|
|
|
#include "vpx/internal/vpx_codec_internal.h"
|
|
|
|
|
2019-09-11 15:51:47 +00:00
|
|
|
#define SAVE_STATUS(ctx, var) ((ctx) ? ((ctx)->err = (var)) : (var))
|
2016-02-23 14:08:26 -05:00
|
|
|
|
|
|
|
static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) {
|
|
|
|
return (vpx_codec_alg_priv_t *)ctx->priv;
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t *ctx,
|
|
|
|
vpx_codec_iface_t *iface,
|
2016-02-23 14:08:26 -05:00
|
|
|
const vpx_codec_enc_cfg_t *cfg,
|
2016-09-26 14:31:51 -04:00
|
|
|
vpx_codec_flags_t flags, int ver) {
|
2016-02-23 14:08:26 -05:00
|
|
|
vpx_codec_err_t res;
|
|
|
|
|
|
|
|
if (ver != VPX_ENCODER_ABI_VERSION)
|
|
|
|
res = VPX_CODEC_ABI_MISMATCH;
|
|
|
|
else if (!ctx || !iface || !cfg)
|
|
|
|
res = VPX_CODEC_INVALID_PARAM;
|
|
|
|
else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
|
|
|
|
res = VPX_CODEC_ABI_MISMATCH;
|
|
|
|
else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
|
|
|
|
res = VPX_CODEC_INCAPABLE;
|
2016-09-26 14:31:51 -04:00
|
|
|
else if ((flags & VPX_CODEC_USE_PSNR) && !(iface->caps & VPX_CODEC_CAP_PSNR))
|
2016-02-23 14:08:26 -05:00
|
|
|
res = VPX_CODEC_INCAPABLE;
|
2016-09-26 14:31:51 -04:00
|
|
|
else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION) &&
|
|
|
|
!(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
|
2016-02-23 14:08:26 -05:00
|
|
|
res = VPX_CODEC_INCAPABLE;
|
|
|
|
else {
|
|
|
|
ctx->iface = iface;
|
|
|
|
ctx->name = iface->name;
|
|
|
|
ctx->priv = NULL;
|
|
|
|
ctx->init_flags = flags;
|
|
|
|
ctx->config.enc = cfg;
|
|
|
|
res = ctx->iface->init(ctx, NULL);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
|
|
|
|
vpx_codec_destroy(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SAVE_STATUS(ctx, res);
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
vpx_codec_err_t vpx_codec_enc_init_multi_ver(
|
|
|
|
vpx_codec_ctx_t *ctx, vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg,
|
|
|
|
int num_enc, vpx_codec_flags_t flags, vpx_rational_t *dsf, int ver) {
|
2016-02-23 14:08:26 -05:00
|
|
|
vpx_codec_err_t res = VPX_CODEC_OK;
|
|
|
|
|
|
|
|
if (ver != VPX_ENCODER_ABI_VERSION)
|
|
|
|
res = VPX_CODEC_ABI_MISMATCH;
|
|
|
|
else if (!ctx || !iface || !cfg || (num_enc > 16 || num_enc < 1))
|
|
|
|
res = VPX_CODEC_INVALID_PARAM;
|
|
|
|
else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
|
|
|
|
res = VPX_CODEC_ABI_MISMATCH;
|
|
|
|
else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
|
|
|
|
res = VPX_CODEC_INCAPABLE;
|
2016-09-26 14:31:51 -04:00
|
|
|
else if ((flags & VPX_CODEC_USE_PSNR) && !(iface->caps & VPX_CODEC_CAP_PSNR))
|
2016-02-23 14:08:26 -05:00
|
|
|
res = VPX_CODEC_INCAPABLE;
|
2016-09-26 14:31:51 -04:00
|
|
|
else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION) &&
|
|
|
|
!(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
|
2016-02-23 14:08:26 -05:00
|
|
|
res = VPX_CODEC_INCAPABLE;
|
|
|
|
else {
|
|
|
|
int i;
|
2019-09-11 15:51:47 +00:00
|
|
|
#if CONFIG_MULTI_RES_ENCODING
|
|
|
|
int mem_loc_owned = 0;
|
|
|
|
#endif
|
2016-02-23 14:08:26 -05:00
|
|
|
void *mem_loc = NULL;
|
|
|
|
|
2019-01-17 20:16:34 +00:00
|
|
|
if (iface->enc.mr_get_mem_loc == NULL) return VPX_CODEC_INCAPABLE;
|
|
|
|
|
2016-02-23 14:08:26 -05:00
|
|
|
if (!(res = iface->enc.mr_get_mem_loc(cfg, &mem_loc))) {
|
|
|
|
for (i = 0; i < num_enc; i++) {
|
|
|
|
vpx_codec_priv_enc_mr_cfg_t mr_cfg;
|
|
|
|
|
|
|
|
/* Validate down-sampling factor. */
|
|
|
|
if (dsf->num < 1 || dsf->num > 4096 || dsf->den < 1 ||
|
|
|
|
dsf->den > dsf->num) {
|
|
|
|
res = VPX_CODEC_INVALID_PARAM;
|
2019-01-17 20:16:34 +00:00
|
|
|
} else {
|
|
|
|
mr_cfg.mr_low_res_mode_info = mem_loc;
|
|
|
|
mr_cfg.mr_total_resolutions = num_enc;
|
|
|
|
mr_cfg.mr_encoder_id = num_enc - 1 - i;
|
|
|
|
mr_cfg.mr_down_sampling_factor.num = dsf->num;
|
|
|
|
mr_cfg.mr_down_sampling_factor.den = dsf->den;
|
|
|
|
|
|
|
|
ctx->iface = iface;
|
|
|
|
ctx->name = iface->name;
|
|
|
|
ctx->priv = NULL;
|
|
|
|
ctx->init_flags = flags;
|
|
|
|
ctx->config.enc = cfg;
|
|
|
|
res = ctx->iface->init(ctx, &mr_cfg);
|
2016-02-23 14:08:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res) {
|
2016-09-26 14:31:51 -04:00
|
|
|
const char *error_detail = ctx->priv ? ctx->priv->err_detail : NULL;
|
2016-02-23 14:08:26 -05:00
|
|
|
/* Destroy current ctx */
|
|
|
|
ctx->err_detail = error_detail;
|
|
|
|
vpx_codec_destroy(ctx);
|
|
|
|
|
|
|
|
/* Destroy already allocated high-level ctx */
|
|
|
|
while (i) {
|
|
|
|
ctx--;
|
|
|
|
ctx->err_detail = error_detail;
|
|
|
|
vpx_codec_destroy(ctx);
|
|
|
|
i--;
|
|
|
|
}
|
2019-01-17 20:16:34 +00:00
|
|
|
#if CONFIG_MULTI_RES_ENCODING
|
2019-09-11 15:51:47 +00:00
|
|
|
if (!mem_loc_owned) {
|
|
|
|
assert(mem_loc);
|
|
|
|
free(((LOWER_RES_FRAME_INFO *)mem_loc)->mb_info);
|
|
|
|
free(mem_loc);
|
|
|
|
}
|
2019-01-17 20:16:34 +00:00
|
|
|
#endif
|
|
|
|
return SAVE_STATUS(ctx, res);
|
2016-02-23 14:08:26 -05:00
|
|
|
}
|
2019-09-11 15:51:47 +00:00
|
|
|
#if CONFIG_MULTI_RES_ENCODING
|
|
|
|
mem_loc_owned = 1;
|
|
|
|
#endif
|
2016-02-23 14:08:26 -05:00
|
|
|
ctx++;
|
|
|
|
cfg++;
|
|
|
|
dsf++;
|
|
|
|
}
|
|
|
|
ctx--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SAVE_STATUS(ctx, res);
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface,
|
|
|
|
vpx_codec_enc_cfg_t *cfg,
|
|
|
|
unsigned int usage) {
|
2016-02-23 14:08:26 -05:00
|
|
|
vpx_codec_err_t res;
|
|
|
|
vpx_codec_enc_cfg_map_t *map;
|
|
|
|
int i;
|
|
|
|
|
2019-09-11 15:51:47 +00:00
|
|
|
if (!iface || !cfg || usage != 0)
|
2016-02-23 14:08:26 -05:00
|
|
|
res = VPX_CODEC_INVALID_PARAM;
|
|
|
|
else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
|
|
|
|
res = VPX_CODEC_INCAPABLE;
|
|
|
|
else {
|
|
|
|
res = VPX_CODEC_INVALID_PARAM;
|
|
|
|
|
|
|
|
for (i = 0; i < iface->enc.cfg_map_count; ++i) {
|
|
|
|
map = iface->enc.cfg_maps + i;
|
2019-09-11 15:51:47 +00:00
|
|
|
*cfg = map->cfg;
|
|
|
|
res = VPX_CODEC_OK;
|
|
|
|
break;
|
2016-02-23 14:08:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ARCH_X86 || ARCH_X86_64
|
|
|
|
/* On X86, disable the x87 unit's internal 80 bit precision for better
|
|
|
|
* consistency with the SSE unit's 64 bit precision.
|
|
|
|
*/
|
|
|
|
#include "vpx_ports/x86.h"
|
2016-09-26 14:31:51 -04:00
|
|
|
#define FLOATING_POINT_INIT() \
|
|
|
|
do { \
|
2016-02-23 14:08:26 -05:00
|
|
|
unsigned short x87_orig_mode = x87_set_double_precision();
|
2016-09-26 14:31:51 -04:00
|
|
|
#define FLOATING_POINT_RESTORE() \
|
|
|
|
x87_set_control_word(x87_orig_mode); \
|
|
|
|
} \
|
|
|
|
while (0)
|
2016-02-23 14:08:26 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
static void FLOATING_POINT_INIT() {}
|
|
|
|
static void FLOATING_POINT_RESTORE() {}
|
|
|
|
#endif
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img,
|
|
|
|
vpx_codec_pts_t pts, unsigned long duration,
|
|
|
|
vpx_enc_frame_flags_t flags,
|
|
|
|
unsigned long deadline) {
|
2016-02-23 14:08:26 -05:00
|
|
|
vpx_codec_err_t res = VPX_CODEC_OK;
|
|
|
|
|
|
|
|
if (!ctx || (img && !duration))
|
|
|
|
res = VPX_CODEC_INVALID_PARAM;
|
|
|
|
else if (!ctx->iface || !ctx->priv)
|
|
|
|
res = VPX_CODEC_ERROR;
|
|
|
|
else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
|
|
|
|
res = VPX_CODEC_INCAPABLE;
|
|
|
|
else {
|
|
|
|
unsigned int num_enc = ctx->priv->enc.total_encoders;
|
|
|
|
|
|
|
|
/* Execute in a normalized floating point environment, if the platform
|
|
|
|
* requires it.
|
|
|
|
*/
|
|
|
|
FLOATING_POINT_INIT();
|
|
|
|
|
|
|
|
if (num_enc == 1)
|
2016-09-26 14:31:51 -04:00
|
|
|
res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration, flags,
|
|
|
|
deadline);
|
2016-02-23 14:08:26 -05:00
|
|
|
else {
|
|
|
|
/* Multi-resolution encoding:
|
|
|
|
* Encode multi-levels in reverse order. For example,
|
|
|
|
* if mr_total_resolutions = 3, first encode level 2,
|
|
|
|
* then encode level 1, and finally encode level 0.
|
|
|
|
*/
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ctx += num_enc - 1;
|
|
|
|
if (img) img += num_enc - 1;
|
|
|
|
|
|
|
|
for (i = num_enc - 1; i >= 0; i--) {
|
2016-09-26 14:31:51 -04:00
|
|
|
if ((res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration,
|
|
|
|
flags, deadline)))
|
2016-02-23 14:08:26 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
ctx--;
|
|
|
|
if (img) img--;
|
|
|
|
}
|
|
|
|
ctx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLOATING_POINT_RESTORE();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SAVE_STATUS(ctx, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
|
|
|
|
vpx_codec_iter_t *iter) {
|
|
|
|
const vpx_codec_cx_pkt_t *pkt = NULL;
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
if (!iter)
|
|
|
|
ctx->err = VPX_CODEC_INVALID_PARAM;
|
|
|
|
else if (!ctx->iface || !ctx->priv)
|
|
|
|
ctx->err = VPX_CODEC_ERROR;
|
|
|
|
else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
|
|
|
|
ctx->err = VPX_CODEC_INCAPABLE;
|
|
|
|
else
|
|
|
|
pkt = ctx->iface->enc.get_cx_data(get_alg_priv(ctx), iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
|
|
|
|
// If the application has specified a destination area for the
|
|
|
|
// compressed data, and the codec has not placed the data there,
|
|
|
|
// and it fits, copy it.
|
|
|
|
vpx_codec_priv_t *const priv = ctx->priv;
|
|
|
|
char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf;
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
if (dst_buf && pkt->data.raw.buf != dst_buf &&
|
2016-02-23 14:08:26 -05:00
|
|
|
pkt->data.raw.sz + priv->enc.cx_data_pad_before +
|
2016-09-26 14:31:51 -04:00
|
|
|
priv->enc.cx_data_pad_after <=
|
|
|
|
priv->enc.cx_data_dst_buf.sz) {
|
2016-02-23 14:08:26 -05:00
|
|
|
vpx_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt;
|
|
|
|
|
|
|
|
memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf,
|
|
|
|
pkt->data.raw.sz);
|
|
|
|
*modified_pkt = *pkt;
|
|
|
|
modified_pkt->data.raw.buf = dst_buf;
|
2016-09-26 14:31:51 -04:00
|
|
|
modified_pkt->data.raw.sz +=
|
|
|
|
priv->enc.cx_data_pad_before + priv->enc.cx_data_pad_after;
|
2016-02-23 14:08:26 -05:00
|
|
|
pkt = modified_pkt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst_buf == pkt->data.raw.buf) {
|
|
|
|
priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
|
|
|
|
priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkt;
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx,
|
2016-02-23 14:08:26 -05:00
|
|
|
const vpx_fixed_buf_t *buf,
|
2016-09-26 14:31:51 -04:00
|
|
|
unsigned int pad_before,
|
|
|
|
unsigned int pad_after) {
|
|
|
|
if (!ctx || !ctx->priv) return VPX_CODEC_INVALID_PARAM;
|
2016-02-23 14:08:26 -05:00
|
|
|
|
|
|
|
if (buf) {
|
|
|
|
ctx->priv->enc.cx_data_dst_buf = *buf;
|
|
|
|
ctx->priv->enc.cx_data_pad_before = pad_before;
|
|
|
|
ctx->priv->enc.cx_data_pad_after = pad_after;
|
|
|
|
} else {
|
|
|
|
ctx->priv->enc.cx_data_dst_buf.buf = NULL;
|
|
|
|
ctx->priv->enc.cx_data_dst_buf.sz = 0;
|
|
|
|
ctx->priv->enc.cx_data_pad_before = 0;
|
|
|
|
ctx->priv->enc.cx_data_pad_after = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return VPX_CODEC_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx) {
|
2016-02-23 14:08:26 -05:00
|
|
|
vpx_image_t *img = NULL;
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
if (!ctx->iface || !ctx->priv)
|
|
|
|
ctx->err = VPX_CODEC_ERROR;
|
|
|
|
else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
|
|
|
|
ctx->err = VPX_CODEC_INCAPABLE;
|
|
|
|
else if (!ctx->iface->enc.get_preview)
|
|
|
|
ctx->err = VPX_CODEC_INCAPABLE;
|
|
|
|
else
|
|
|
|
img = ctx->iface->enc.get_preview(get_alg_priv(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx) {
|
2016-02-23 14:08:26 -05:00
|
|
|
vpx_fixed_buf_t *buf = NULL;
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
if (!ctx->iface || !ctx->priv)
|
|
|
|
ctx->err = VPX_CODEC_ERROR;
|
|
|
|
else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
|
|
|
|
ctx->err = VPX_CODEC_INCAPABLE;
|
|
|
|
else if (!ctx->iface->enc.get_glob_hdrs)
|
|
|
|
ctx->err = VPX_CODEC_INCAPABLE;
|
|
|
|
else
|
|
|
|
buf = ctx->iface->enc.get_glob_hdrs(get_alg_priv(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx,
|
|
|
|
const vpx_codec_enc_cfg_t *cfg) {
|
2016-02-23 14:08:26 -05:00
|
|
|
vpx_codec_err_t res;
|
|
|
|
|
|
|
|
if (!ctx || !ctx->iface || !ctx->priv || !cfg)
|
|
|
|
res = VPX_CODEC_INVALID_PARAM;
|
|
|
|
else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
|
|
|
|
res = VPX_CODEC_INCAPABLE;
|
|
|
|
else
|
|
|
|
res = ctx->iface->enc.cfg_set(get_alg_priv(ctx), cfg);
|
|
|
|
|
|
|
|
return SAVE_STATUS(ctx, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
|
|
|
|
const struct vpx_codec_cx_pkt *pkt) {
|
|
|
|
if (list->cnt < list->max) {
|
|
|
|
list->pkts[list->cnt++] = *pkt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:31:51 -04:00
|
|
|
const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(
|
|
|
|
struct vpx_codec_pkt_list *list, vpx_codec_iter_t *iter) {
|
2016-02-23 14:08:26 -05:00
|
|
|
const vpx_codec_cx_pkt_t *pkt;
|
|
|
|
|
|
|
|
if (!(*iter)) {
|
|
|
|
*iter = list->pkts;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt = (const vpx_codec_cx_pkt_t *)*iter;
|
|
|
|
|
|
|
|
if ((size_t)(pkt - list->pkts) < list->cnt)
|
|
|
|
*iter = pkt + 1;
|
|
|
|
else
|
|
|
|
pkt = NULL;
|
|
|
|
|
|
|
|
return pkt;
|
|
|
|
}
|