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.
12 /*!\file vpx_encoder.c
13 * \brief Provides the high level interface to wrap encoder algorithms.
18 #include "vpx/internal/vpx_codec_internal.h"
20 #define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var)
22 vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t *ctx,
23 vpx_codec_iface_t *iface,
24 vpx_codec_enc_cfg_t *cfg,
25 vpx_codec_flags_t flags,
30 if (ver != VPX_ENCODER_ABI_VERSION)
31 res = VPX_CODEC_ABI_MISMATCH;
32 else if (!ctx || !iface || !cfg)
33 res = VPX_CODEC_INVALID_PARAM;
34 else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
35 res = VPX_CODEC_ABI_MISMATCH;
36 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
37 res = VPX_CODEC_INCAPABLE;
38 else if ((flags & VPX_CODEC_USE_XMA) && !(iface->caps & VPX_CODEC_CAP_XMA))
39 res = VPX_CODEC_INCAPABLE;
40 else if ((flags & VPX_CODEC_USE_PSNR)
41 && !(iface->caps & VPX_CODEC_CAP_PSNR))
42 res = VPX_CODEC_INCAPABLE;
46 ctx->name = iface->name;
48 ctx->init_flags = flags;
49 ctx->config.enc = cfg;
50 res = ctx->iface->init(ctx);
54 ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
55 vpx_codec_destroy(ctx);
59 ctx->priv->iface = ctx->iface;
62 return SAVE_STATUS(ctx, res);
67 vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface,
68 vpx_codec_enc_cfg_t *cfg,
72 vpx_codec_enc_cfg_map_t *map;
74 if (!iface || !cfg || usage > INT_MAX)
75 res = VPX_CODEC_INVALID_PARAM;
76 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
77 res = VPX_CODEC_INCAPABLE;
80 res = VPX_CODEC_INVALID_PARAM;
82 for (map = iface->enc.cfg_maps; map->usage >= 0; map++)
84 if (map->usage == (int)usage)
98 #if ARCH_X86 || ARCH_X86_64
99 /* On X86, disable the x87 unit's internal 80 bit precision for better
100 * consistency with the SSE unit's 64 bit precision.
102 #include "vpx_ports/x86.h"
103 #define FLOATING_POINT_INIT() do {\
104 unsigned short x87_orig_mode = x87_set_double_precision();
105 #define FLOATING_POINT_RESTORE() \
106 x87_set_control_word(x87_orig_mode); }while(0)
110 static void FLOATING_POINT_INIT() {}
111 static void FLOATING_POINT_RESTORE() {}
115 vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx,
116 const vpx_image_t *img,
118 unsigned long duration,
119 vpx_enc_frame_flags_t flags,
120 unsigned long deadline)
124 if (!ctx || (img && !duration))
125 res = VPX_CODEC_INVALID_PARAM;
126 else if (!ctx->iface || !ctx->priv)
127 res = VPX_CODEC_ERROR;
128 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
129 res = VPX_CODEC_INCAPABLE;
132 /* Execute in a normalized floating point environment, if the platform
135 FLOATING_POINT_INIT();
136 res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
137 duration, flags, deadline);
138 FLOATING_POINT_RESTORE();
141 return SAVE_STATUS(ctx, res);
145 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
146 vpx_codec_iter_t *iter)
148 const vpx_codec_cx_pkt_t *pkt = NULL;
153 ctx->err = VPX_CODEC_INVALID_PARAM;
154 else if (!ctx->iface || !ctx->priv)
155 ctx->err = VPX_CODEC_ERROR;
156 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
157 ctx->err = VPX_CODEC_INCAPABLE;
159 pkt = ctx->iface->enc.get_cx_data(ctx->priv->alg_priv, iter);
162 if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT)
164 /* If the application has specified a destination area for the
165 * compressed data, and the codec has not placed the data there,
166 * and it fits, copy it.
168 char *dst_buf = ctx->priv->enc.cx_data_dst_buf.buf;
171 && pkt->data.raw.buf != dst_buf
173 + ctx->priv->enc.cx_data_pad_before
174 + ctx->priv->enc.cx_data_pad_after
175 <= ctx->priv->enc.cx_data_dst_buf.sz)
177 vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt;
179 memcpy(dst_buf + ctx->priv->enc.cx_data_pad_before,
180 pkt->data.raw.buf, pkt->data.raw.sz);
181 *modified_pkt = *pkt;
182 modified_pkt->data.raw.buf = dst_buf;
183 modified_pkt->data.raw.sz += ctx->priv->enc.cx_data_pad_before
184 + ctx->priv->enc.cx_data_pad_after;
188 if (dst_buf == pkt->data.raw.buf)
190 ctx->priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
191 ctx->priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
199 vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx,
200 const vpx_fixed_buf_t *buf,
201 unsigned int pad_before,
202 unsigned int pad_after)
204 if (!ctx || !ctx->priv)
205 return VPX_CODEC_INVALID_PARAM;
209 ctx->priv->enc.cx_data_dst_buf = *buf;
210 ctx->priv->enc.cx_data_pad_before = pad_before;
211 ctx->priv->enc.cx_data_pad_after = pad_after;
215 ctx->priv->enc.cx_data_dst_buf.buf = NULL;
216 ctx->priv->enc.cx_data_dst_buf.sz = 0;
217 ctx->priv->enc.cx_data_pad_before = 0;
218 ctx->priv->enc.cx_data_pad_after = 0;
225 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx)
227 vpx_image_t *img = NULL;
231 if (!ctx->iface || !ctx->priv)
232 ctx->err = VPX_CODEC_ERROR;
233 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
234 ctx->err = VPX_CODEC_INCAPABLE;
235 else if (!ctx->iface->enc.get_preview)
236 ctx->err = VPX_CODEC_INCAPABLE;
238 img = ctx->iface->enc.get_preview(ctx->priv->alg_priv);
245 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx)
247 vpx_fixed_buf_t *buf = NULL;
251 if (!ctx->iface || !ctx->priv)
252 ctx->err = VPX_CODEC_ERROR;
253 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
254 ctx->err = VPX_CODEC_INCAPABLE;
255 else if (!ctx->iface->enc.get_glob_hdrs)
256 ctx->err = VPX_CODEC_INCAPABLE;
258 buf = ctx->iface->enc.get_glob_hdrs(ctx->priv->alg_priv);
265 vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx,
266 const vpx_codec_enc_cfg_t *cfg)
270 if (!ctx || !ctx->iface || !ctx->priv || !cfg)
271 res = VPX_CODEC_INVALID_PARAM;
272 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
273 res = VPX_CODEC_INCAPABLE;
275 res = ctx->iface->enc.cfg_set(ctx->priv->alg_priv, cfg);
277 return SAVE_STATUS(ctx, res);
281 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
282 const struct vpx_codec_cx_pkt *pkt)
284 if (list->cnt < list->max)
286 list->pkts[list->cnt++] = *pkt;
294 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
295 vpx_codec_iter_t *iter)
297 const vpx_codec_cx_pkt_t *pkt;
304 pkt = (const void *) * iter;
306 if (pkt - list->pkts < list->cnt)