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,
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;
43 else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
44 && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
45 res = VPX_CODEC_INCAPABLE;
48 ctx->name = iface->name;
50 ctx->init_flags = flags;
51 ctx->config.enc = cfg;
52 res = ctx->iface->init(ctx, NULL);
55 ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
56 vpx_codec_destroy(ctx);
60 ctx->priv->iface = ctx->iface;
63 return SAVE_STATUS(ctx, res);
66 vpx_codec_err_t vpx_codec_enc_init_multi_ver(vpx_codec_ctx_t *ctx,
67 vpx_codec_iface_t *iface,
68 vpx_codec_enc_cfg_t *cfg,
70 vpx_codec_flags_t flags,
73 vpx_codec_err_t res = VPX_CODEC_OK;
75 if (ver != VPX_ENCODER_ABI_VERSION)
76 res = VPX_CODEC_ABI_MISMATCH;
77 else if (!ctx || !iface || !cfg || (num_enc > 16 || num_enc < 1))
78 res = VPX_CODEC_INVALID_PARAM;
79 else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
80 res = VPX_CODEC_ABI_MISMATCH;
81 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
82 res = VPX_CODEC_INCAPABLE;
83 else if ((flags & VPX_CODEC_USE_XMA) && !(iface->caps & VPX_CODEC_CAP_XMA))
84 res = VPX_CODEC_INCAPABLE;
85 else if ((flags & VPX_CODEC_USE_PSNR)
86 && !(iface->caps & VPX_CODEC_CAP_PSNR))
87 res = VPX_CODEC_INCAPABLE;
88 else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
89 && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
90 res = VPX_CODEC_INCAPABLE;
95 if (!(res = iface->enc.mr_get_mem_loc(cfg, &mem_loc))) {
96 for (i = 0; i < num_enc; i++) {
97 vpx_codec_priv_enc_mr_cfg_t mr_cfg;
99 /* Validate down-sampling factor. */
100 if (dsf->num < 1 || dsf->num > 4096 || dsf->den < 1 ||
101 dsf->den > dsf->num) {
102 res = VPX_CODEC_INVALID_PARAM;
106 mr_cfg.mr_low_res_mode_info = mem_loc;
107 mr_cfg.mr_total_resolutions = num_enc;
108 mr_cfg.mr_encoder_id = num_enc - 1 - i;
109 mr_cfg.mr_down_sampling_factor.num = dsf->num;
110 mr_cfg.mr_down_sampling_factor.den = dsf->den;
112 /* Force Key-frame synchronization. Namely, encoder at higher
113 * resolution always use the same frame_type chosen by the
114 * lowest-resolution encoder.
116 if (mr_cfg.mr_encoder_id)
117 cfg->kf_mode = VPX_KF_DISABLED;
120 ctx->name = iface->name;
122 ctx->init_flags = flags;
123 ctx->config.enc = cfg;
124 res = ctx->iface->init(ctx, &mr_cfg);
127 const char *error_detail =
128 ctx->priv ? ctx->priv->err_detail : NULL;
129 /* Destroy current ctx */
130 ctx->err_detail = error_detail;
131 vpx_codec_destroy(ctx);
133 /* Destroy already allocated high-level ctx */
136 ctx->err_detail = error_detail;
137 vpx_codec_destroy(ctx);
143 ctx->priv->iface = ctx->iface;
156 return SAVE_STATUS(ctx, res);
160 vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface,
161 vpx_codec_enc_cfg_t *cfg,
162 unsigned int usage) {
164 vpx_codec_enc_cfg_map_t *map;
166 if (!iface || !cfg || usage > INT_MAX)
167 res = VPX_CODEC_INVALID_PARAM;
168 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
169 res = VPX_CODEC_INCAPABLE;
171 res = VPX_CODEC_INVALID_PARAM;
173 for (map = iface->enc.cfg_maps; map->usage >= 0; map++) {
174 if (map->usage == (int)usage) {
176 cfg->g_usage = usage;
187 #if ARCH_X86 || ARCH_X86_64
188 /* On X86, disable the x87 unit's internal 80 bit precision for better
189 * consistency with the SSE unit's 64 bit precision.
191 #include "vpx_ports/x86.h"
192 #define FLOATING_POINT_INIT() do {\
193 unsigned short x87_orig_mode = x87_set_double_precision();
194 #define FLOATING_POINT_RESTORE() \
195 x87_set_control_word(x87_orig_mode); }while(0)
199 static void FLOATING_POINT_INIT() {}
200 static void FLOATING_POINT_RESTORE() {}
204 vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx,
205 const vpx_image_t *img,
207 unsigned long duration,
208 vpx_enc_frame_flags_t flags,
209 unsigned long deadline) {
210 vpx_codec_err_t res = VPX_CODEC_OK;
212 if (!ctx || (img && !duration))
213 res = VPX_CODEC_INVALID_PARAM;
214 else if (!ctx->iface || !ctx->priv)
215 res = VPX_CODEC_ERROR;
216 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
217 res = VPX_CODEC_INCAPABLE;
219 unsigned int num_enc = ctx->priv->enc.total_encoders;
221 /* Execute in a normalized floating point environment, if the platform
224 FLOATING_POINT_INIT();
227 res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
228 duration, flags, deadline);
230 /* Multi-resolution encoding:
231 * Encode multi-levels in reverse order. For example,
232 * if mr_total_resolutions = 3, first encode level 2,
233 * then encode level 1, and finally encode level 0.
238 if (img) img += num_enc - 1;
240 for (i = num_enc - 1; i >= 0; i--) {
241 if ((res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
242 duration, flags, deadline)))
251 FLOATING_POINT_RESTORE();
254 return SAVE_STATUS(ctx, res);
258 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
259 vpx_codec_iter_t *iter) {
260 const vpx_codec_cx_pkt_t *pkt = NULL;
264 ctx->err = VPX_CODEC_INVALID_PARAM;
265 else if (!ctx->iface || !ctx->priv)
266 ctx->err = VPX_CODEC_ERROR;
267 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
268 ctx->err = VPX_CODEC_INCAPABLE;
270 pkt = ctx->iface->enc.get_cx_data(ctx->priv->alg_priv, iter);
273 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) {
286 vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt;
288 memcpy(dst_buf + ctx->priv->enc.cx_data_pad_before,
289 pkt->data.raw.buf, pkt->data.raw.sz);
290 *modified_pkt = *pkt;
291 modified_pkt->data.raw.buf = dst_buf;
292 modified_pkt->data.raw.sz += ctx->priv->enc.cx_data_pad_before
293 + ctx->priv->enc.cx_data_pad_after;
297 if (dst_buf == pkt->data.raw.buf) {
298 ctx->priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
299 ctx->priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
307 vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx,
308 const vpx_fixed_buf_t *buf,
309 unsigned int pad_before,
310 unsigned int pad_after) {
311 if (!ctx || !ctx->priv)
312 return VPX_CODEC_INVALID_PARAM;
315 ctx->priv->enc.cx_data_dst_buf = *buf;
316 ctx->priv->enc.cx_data_pad_before = pad_before;
317 ctx->priv->enc.cx_data_pad_after = pad_after;
319 ctx->priv->enc.cx_data_dst_buf.buf = NULL;
320 ctx->priv->enc.cx_data_dst_buf.sz = 0;
321 ctx->priv->enc.cx_data_pad_before = 0;
322 ctx->priv->enc.cx_data_pad_after = 0;
329 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx) {
330 vpx_image_t *img = NULL;
333 if (!ctx->iface || !ctx->priv)
334 ctx->err = VPX_CODEC_ERROR;
335 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
336 ctx->err = VPX_CODEC_INCAPABLE;
337 else if (!ctx->iface->enc.get_preview)
338 ctx->err = VPX_CODEC_INCAPABLE;
340 img = ctx->iface->enc.get_preview(ctx->priv->alg_priv);
347 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx) {
348 vpx_fixed_buf_t *buf = NULL;
351 if (!ctx->iface || !ctx->priv)
352 ctx->err = VPX_CODEC_ERROR;
353 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
354 ctx->err = VPX_CODEC_INCAPABLE;
355 else if (!ctx->iface->enc.get_glob_hdrs)
356 ctx->err = VPX_CODEC_INCAPABLE;
358 buf = ctx->iface->enc.get_glob_hdrs(ctx->priv->alg_priv);
365 vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx,
366 const vpx_codec_enc_cfg_t *cfg) {
369 if (!ctx || !ctx->iface || !ctx->priv || !cfg)
370 res = VPX_CODEC_INVALID_PARAM;
371 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
372 res = VPX_CODEC_INCAPABLE;
374 res = ctx->iface->enc.cfg_set(ctx->priv->alg_priv, cfg);
376 return SAVE_STATUS(ctx, res);
380 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
381 const struct vpx_codec_cx_pkt *pkt) {
382 if (list->cnt < list->max) {
383 list->pkts[list->cnt++] = *pkt;
391 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
392 vpx_codec_iter_t *iter) {
393 const vpx_codec_cx_pkt_t *pkt;
399 pkt = (const void *) * iter;
401 if ((size_t)(pkt - list->pkts) < list->cnt)