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.
13 * \brief Provides the high level interface to wrap encoder algorithms.
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,
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;
50 ctx->name = iface->name;
52 ctx->init_flags = flags;
53 ctx->config.enc = cfg;
54 res = ctx->iface->init(ctx, NULL);
58 ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
59 vpx_codec_destroy(ctx);
63 ctx->priv->iface = ctx->iface;
66 return SAVE_STATUS(ctx, res);
69 vpx_codec_err_t vpx_codec_enc_init_multi_ver(vpx_codec_ctx_t *ctx,
70 vpx_codec_iface_t *iface,
71 vpx_codec_enc_cfg_t *cfg,
73 vpx_codec_flags_t flags,
77 vpx_codec_err_t res = 0;
79 if (ver != VPX_ENCODER_ABI_VERSION)
80 res = VPX_CODEC_ABI_MISMATCH;
81 else if (!ctx || !iface || !cfg || (num_enc > 16 || num_enc < 1))
82 res = VPX_CODEC_INVALID_PARAM;
83 else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
84 res = VPX_CODEC_ABI_MISMATCH;
85 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
86 res = VPX_CODEC_INCAPABLE;
87 else if ((flags & VPX_CODEC_USE_XMA) && !(iface->caps & VPX_CODEC_CAP_XMA))
88 res = VPX_CODEC_INCAPABLE;
89 else if ((flags & VPX_CODEC_USE_PSNR)
90 && !(iface->caps & VPX_CODEC_CAP_PSNR))
91 res = VPX_CODEC_INCAPABLE;
92 else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
93 && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
94 res = VPX_CODEC_INCAPABLE;
100 if(!(res = iface->enc.mr_get_mem_loc(cfg, &mem_loc)))
102 for (i = 0; i < num_enc; i++)
104 vpx_codec_priv_enc_mr_cfg_t mr_cfg;
106 /* Validate down-sampling factor. */
107 if(dsf->num < 1 || dsf->num > 4096 || dsf->den < 1 ||
110 res = VPX_CODEC_INVALID_PARAM;
114 mr_cfg.mr_low_res_mode_info = mem_loc;
115 mr_cfg.mr_total_resolutions = num_enc;
116 mr_cfg.mr_encoder_id = num_enc-1-i;
117 mr_cfg.mr_down_sampling_factor.num = dsf->num;
118 mr_cfg.mr_down_sampling_factor.den = dsf->den;
121 ctx->name = iface->name;
123 ctx->init_flags = flags;
124 ctx->config.enc = cfg;
125 res = ctx->iface->init(ctx, &mr_cfg);
129 ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
130 vpx_codec_destroy(ctx);
134 ctx->priv->iface = ctx->iface;
146 return SAVE_STATUS(ctx, res);
150 vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface,
151 vpx_codec_enc_cfg_t *cfg,
155 vpx_codec_enc_cfg_map_t *map;
157 if (!iface || !cfg || usage > INT_MAX)
158 res = VPX_CODEC_INVALID_PARAM;
159 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
160 res = VPX_CODEC_INCAPABLE;
163 res = VPX_CODEC_INVALID_PARAM;
165 for (map = iface->enc.cfg_maps; map->usage >= 0; map++)
167 if (map->usage == (int)usage)
170 cfg->g_usage = usage;
181 #if ARCH_X86 || ARCH_X86_64
182 /* On X86, disable the x87 unit's internal 80 bit precision for better
183 * consistency with the SSE unit's 64 bit precision.
185 #include "vpx_ports/x86.h"
186 #define FLOATING_POINT_INIT() do {\
187 unsigned short x87_orig_mode = x87_set_double_precision();
188 #define FLOATING_POINT_RESTORE() \
189 x87_set_control_word(x87_orig_mode); }while(0)
193 static void FLOATING_POINT_INIT() {}
194 static void FLOATING_POINT_RESTORE() {}
198 vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx,
199 const vpx_image_t *img,
201 unsigned long duration,
202 vpx_enc_frame_flags_t flags,
203 unsigned long deadline)
205 vpx_codec_err_t res = 0;
207 if (!ctx || (img && !duration))
208 res = VPX_CODEC_INVALID_PARAM;
209 else if (!ctx->iface || !ctx->priv)
210 res = VPX_CODEC_ERROR;
211 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
212 res = VPX_CODEC_INCAPABLE;
215 /* Execute in a normalized floating point environment, if the platform
218 unsigned int num_enc =ctx->priv->enc.total_encoders;
220 FLOATING_POINT_INIT();
223 res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
224 duration, flags, deadline);
227 /* Multi-resolution encoding:
228 * Encode multi-levels in reverse order. For example,
229 * if mr_total_resolutions = 3, first encode level 2,
230 * then encode level 1, and finally encode level 0.
235 if (img) img += num_enc - 1;
237 for (i = num_enc-1; i >= 0; i--)
239 if ((res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
240 duration, flags, deadline)))
248 FLOATING_POINT_RESTORE();
251 return SAVE_STATUS(ctx, res);
255 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
256 vpx_codec_iter_t *iter)
258 const vpx_codec_cx_pkt_t *pkt = NULL;
263 ctx->err = VPX_CODEC_INVALID_PARAM;
264 else if (!ctx->iface || !ctx->priv)
265 ctx->err = VPX_CODEC_ERROR;
266 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
267 ctx->err = VPX_CODEC_INCAPABLE;
269 pkt = ctx->iface->enc.get_cx_data(ctx->priv->alg_priv, iter);
272 if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT)
274 /* If the application has specified a destination area for the
275 * compressed data, and the codec has not placed the data there,
276 * and it fits, copy it.
278 char *dst_buf = ctx->priv->enc.cx_data_dst_buf.buf;
281 && pkt->data.raw.buf != dst_buf
283 + ctx->priv->enc.cx_data_pad_before
284 + ctx->priv->enc.cx_data_pad_after
285 <= ctx->priv->enc.cx_data_dst_buf.sz)
287 vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt;
289 memcpy(dst_buf + ctx->priv->enc.cx_data_pad_before,
290 pkt->data.raw.buf, pkt->data.raw.sz);
291 *modified_pkt = *pkt;
292 modified_pkt->data.raw.buf = dst_buf;
293 modified_pkt->data.raw.sz += ctx->priv->enc.cx_data_pad_before
294 + ctx->priv->enc.cx_data_pad_after;
298 if (dst_buf == pkt->data.raw.buf)
300 ctx->priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
301 ctx->priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
309 vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx,
310 const vpx_fixed_buf_t *buf,
311 unsigned int pad_before,
312 unsigned int pad_after)
314 if (!ctx || !ctx->priv)
315 return VPX_CODEC_INVALID_PARAM;
319 ctx->priv->enc.cx_data_dst_buf = *buf;
320 ctx->priv->enc.cx_data_pad_before = pad_before;
321 ctx->priv->enc.cx_data_pad_after = pad_after;
325 ctx->priv->enc.cx_data_dst_buf.buf = NULL;
326 ctx->priv->enc.cx_data_dst_buf.sz = 0;
327 ctx->priv->enc.cx_data_pad_before = 0;
328 ctx->priv->enc.cx_data_pad_after = 0;
335 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx)
337 vpx_image_t *img = NULL;
341 if (!ctx->iface || !ctx->priv)
342 ctx->err = VPX_CODEC_ERROR;
343 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
344 ctx->err = VPX_CODEC_INCAPABLE;
345 else if (!ctx->iface->enc.get_preview)
346 ctx->err = VPX_CODEC_INCAPABLE;
348 img = ctx->iface->enc.get_preview(ctx->priv->alg_priv);
355 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx)
357 vpx_fixed_buf_t *buf = NULL;
361 if (!ctx->iface || !ctx->priv)
362 ctx->err = VPX_CODEC_ERROR;
363 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
364 ctx->err = VPX_CODEC_INCAPABLE;
365 else if (!ctx->iface->enc.get_glob_hdrs)
366 ctx->err = VPX_CODEC_INCAPABLE;
368 buf = ctx->iface->enc.get_glob_hdrs(ctx->priv->alg_priv);
375 vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx,
376 const vpx_codec_enc_cfg_t *cfg)
380 if (!ctx || !ctx->iface || !ctx->priv || !cfg)
381 res = VPX_CODEC_INVALID_PARAM;
382 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
383 res = VPX_CODEC_INCAPABLE;
385 res = ctx->iface->enc.cfg_set(ctx->priv->alg_priv, cfg);
387 return SAVE_STATUS(ctx, res);
391 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
392 const struct vpx_codec_cx_pkt *pkt)
394 if (list->cnt < list->max)
396 list->pkts[list->cnt++] = *pkt;
404 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
405 vpx_codec_iter_t *iter)
407 const vpx_codec_cx_pkt_t *pkt;
414 pkt = (const void *) * iter;
416 if ((size_t)(pkt - list->pkts) < list->cnt)