include the arm header files in make dist
[libvpx.git] / vpx / src / vpx_encoder.c
blob5e86835ea05488f7ee5b10aead874384b7488eb9
1 /*
2 * Copyright (c) 2010 The WebM 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.
9 */
12 /*!\file
13 * \brief Provides the high level interface to wrap encoder algorithms.
16 #include <limits.h>
17 #include <string.h>
18 #include "vpx/internal/vpx_codec_internal.h"
19 #include "vpx_config.h"
21 #define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var)
23 vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t *ctx,
24 vpx_codec_iface_t *iface,
25 vpx_codec_enc_cfg_t *cfg,
26 vpx_codec_flags_t flags,
27 int ver)
29 vpx_codec_err_t res;
31 if (ver != VPX_ENCODER_ABI_VERSION)
32 res = VPX_CODEC_ABI_MISMATCH;
33 else if (!ctx || !iface || !cfg)
34 res = VPX_CODEC_INVALID_PARAM;
35 else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
36 res = VPX_CODEC_ABI_MISMATCH;
37 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
38 res = VPX_CODEC_INCAPABLE;
39 else if ((flags & VPX_CODEC_USE_XMA) && !(iface->caps & VPX_CODEC_CAP_XMA))
40 res = VPX_CODEC_INCAPABLE;
41 else if ((flags & VPX_CODEC_USE_PSNR)
42 && !(iface->caps & VPX_CODEC_CAP_PSNR))
43 res = VPX_CODEC_INCAPABLE;
44 else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
45 && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
46 res = VPX_CODEC_INCAPABLE;
47 else
49 ctx->iface = iface;
50 ctx->name = iface->name;
51 ctx->priv = NULL;
52 ctx->init_flags = flags;
53 ctx->config.enc = cfg;
54 res = ctx->iface->init(ctx);
56 if (res)
58 ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
59 vpx_codec_destroy(ctx);
62 if (ctx->priv)
63 ctx->priv->iface = ctx->iface;
66 return SAVE_STATUS(ctx, res);
71 vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface,
72 vpx_codec_enc_cfg_t *cfg,
73 unsigned int usage)
75 vpx_codec_err_t res;
76 vpx_codec_enc_cfg_map_t *map;
78 if (!iface || !cfg || usage > INT_MAX)
79 res = VPX_CODEC_INVALID_PARAM;
80 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
81 res = VPX_CODEC_INCAPABLE;
82 else
84 res = VPX_CODEC_INVALID_PARAM;
86 for (map = iface->enc.cfg_maps; map->usage >= 0; map++)
88 if (map->usage == (int)usage)
90 *cfg = map->cfg;
91 cfg->g_usage = usage;
92 res = VPX_CODEC_OK;
93 break;
98 return res;
102 #if ARCH_X86 || ARCH_X86_64
103 /* On X86, disable the x87 unit's internal 80 bit precision for better
104 * consistency with the SSE unit's 64 bit precision.
106 #include "vpx_ports/x86.h"
107 #define FLOATING_POINT_INIT() do {\
108 unsigned short x87_orig_mode = x87_set_double_precision();
109 #define FLOATING_POINT_RESTORE() \
110 x87_set_control_word(x87_orig_mode); }while(0)
113 #else
114 static void FLOATING_POINT_INIT() {}
115 static void FLOATING_POINT_RESTORE() {}
116 #endif
119 vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx,
120 const vpx_image_t *img,
121 vpx_codec_pts_t pts,
122 unsigned long duration,
123 vpx_enc_frame_flags_t flags,
124 unsigned long deadline)
126 vpx_codec_err_t res;
128 if (!ctx || (img && !duration))
129 res = VPX_CODEC_INVALID_PARAM;
130 else if (!ctx->iface || !ctx->priv)
131 res = VPX_CODEC_ERROR;
132 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
133 res = VPX_CODEC_INCAPABLE;
134 else
136 /* Execute in a normalized floating point environment, if the platform
137 * requires it.
139 FLOATING_POINT_INIT();
140 res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
141 duration, flags, deadline);
142 FLOATING_POINT_RESTORE();
145 return SAVE_STATUS(ctx, res);
149 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
150 vpx_codec_iter_t *iter)
152 const vpx_codec_cx_pkt_t *pkt = NULL;
154 if (ctx)
156 if (!iter)
157 ctx->err = VPX_CODEC_INVALID_PARAM;
158 else if (!ctx->iface || !ctx->priv)
159 ctx->err = VPX_CODEC_ERROR;
160 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
161 ctx->err = VPX_CODEC_INCAPABLE;
162 else
163 pkt = ctx->iface->enc.get_cx_data(ctx->priv->alg_priv, iter);
166 if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT)
168 /* If the application has specified a destination area for the
169 * compressed data, and the codec has not placed the data there,
170 * and it fits, copy it.
172 char *dst_buf = ctx->priv->enc.cx_data_dst_buf.buf;
174 if (dst_buf
175 && pkt->data.raw.buf != dst_buf
176 && pkt->data.raw.sz
177 + ctx->priv->enc.cx_data_pad_before
178 + ctx->priv->enc.cx_data_pad_after
179 <= ctx->priv->enc.cx_data_dst_buf.sz)
181 vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt;
183 memcpy(dst_buf + ctx->priv->enc.cx_data_pad_before,
184 pkt->data.raw.buf, pkt->data.raw.sz);
185 *modified_pkt = *pkt;
186 modified_pkt->data.raw.buf = dst_buf;
187 modified_pkt->data.raw.sz += ctx->priv->enc.cx_data_pad_before
188 + ctx->priv->enc.cx_data_pad_after;
189 pkt = modified_pkt;
192 if (dst_buf == pkt->data.raw.buf)
194 ctx->priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
195 ctx->priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
199 return pkt;
203 vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx,
204 const vpx_fixed_buf_t *buf,
205 unsigned int pad_before,
206 unsigned int pad_after)
208 if (!ctx || !ctx->priv)
209 return VPX_CODEC_INVALID_PARAM;
211 if (buf)
213 ctx->priv->enc.cx_data_dst_buf = *buf;
214 ctx->priv->enc.cx_data_pad_before = pad_before;
215 ctx->priv->enc.cx_data_pad_after = pad_after;
217 else
219 ctx->priv->enc.cx_data_dst_buf.buf = NULL;
220 ctx->priv->enc.cx_data_dst_buf.sz = 0;
221 ctx->priv->enc.cx_data_pad_before = 0;
222 ctx->priv->enc.cx_data_pad_after = 0;
225 return VPX_CODEC_OK;
229 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx)
231 vpx_image_t *img = NULL;
233 if (ctx)
235 if (!ctx->iface || !ctx->priv)
236 ctx->err = VPX_CODEC_ERROR;
237 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
238 ctx->err = VPX_CODEC_INCAPABLE;
239 else if (!ctx->iface->enc.get_preview)
240 ctx->err = VPX_CODEC_INCAPABLE;
241 else
242 img = ctx->iface->enc.get_preview(ctx->priv->alg_priv);
245 return img;
249 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx)
251 vpx_fixed_buf_t *buf = NULL;
253 if (ctx)
255 if (!ctx->iface || !ctx->priv)
256 ctx->err = VPX_CODEC_ERROR;
257 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
258 ctx->err = VPX_CODEC_INCAPABLE;
259 else if (!ctx->iface->enc.get_glob_hdrs)
260 ctx->err = VPX_CODEC_INCAPABLE;
261 else
262 buf = ctx->iface->enc.get_glob_hdrs(ctx->priv->alg_priv);
265 return buf;
269 vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx,
270 const vpx_codec_enc_cfg_t *cfg)
272 vpx_codec_err_t res;
274 if (!ctx || !ctx->iface || !ctx->priv || !cfg)
275 res = VPX_CODEC_INVALID_PARAM;
276 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
277 res = VPX_CODEC_INCAPABLE;
278 else
279 res = ctx->iface->enc.cfg_set(ctx->priv->alg_priv, cfg);
281 return SAVE_STATUS(ctx, res);
285 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
286 const struct vpx_codec_cx_pkt *pkt)
288 if (list->cnt < list->max)
290 list->pkts[list->cnt++] = *pkt;
291 return 0;
294 return 1;
298 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
299 vpx_codec_iter_t *iter)
301 const vpx_codec_cx_pkt_t *pkt;
303 if (!(*iter))
305 *iter = list->pkts;
308 pkt = (const void *) * iter;
310 if ((size_t)(pkt - list->pkts) < list->cnt)
311 *iter = pkt + 1;
312 else
313 pkt = NULL;
315 return pkt;