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_config.h"
19 #include "vpx/internal/vpx_codec_internal.h"
21 #define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var)
23 static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) {
24 return (vpx_codec_alg_priv_t *)ctx->priv;
27 vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t *ctx,
28 vpx_codec_iface_t *iface,
29 const vpx_codec_enc_cfg_t *cfg,
30 vpx_codec_flags_t flags,
34 if (ver != VPX_ENCODER_ABI_VERSION)
35 res = VPX_CODEC_ABI_MISMATCH;
36 else if (!ctx || !iface || !cfg)
37 res = VPX_CODEC_INVALID_PARAM;
38 else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
39 res = VPX_CODEC_ABI_MISMATCH;
40 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
41 res = VPX_CODEC_INCAPABLE;
42 else if ((flags & VPX_CODEC_USE_PSNR)
43 && !(iface->caps & VPX_CODEC_CAP_PSNR))
44 res = VPX_CODEC_INCAPABLE;
45 else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
46 && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
47 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);
57 ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
58 vpx_codec_destroy(ctx);
62 return SAVE_STATUS(ctx, res);
65 vpx_codec_err_t vpx_codec_enc_init_multi_ver(vpx_codec_ctx_t *ctx,
66 vpx_codec_iface_t *iface,
67 vpx_codec_enc_cfg_t *cfg,
69 vpx_codec_flags_t flags,
72 vpx_codec_err_t res = VPX_CODEC_OK;
74 if (ver != VPX_ENCODER_ABI_VERSION)
75 res = VPX_CODEC_ABI_MISMATCH;
76 else if (!ctx || !iface || !cfg || (num_enc > 16 || num_enc < 1))
77 res = VPX_CODEC_INVALID_PARAM;
78 else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
79 res = VPX_CODEC_ABI_MISMATCH;
80 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
81 res = VPX_CODEC_INCAPABLE;
82 else if ((flags & VPX_CODEC_USE_PSNR)
83 && !(iface->caps & VPX_CODEC_CAP_PSNR))
84 res = VPX_CODEC_INCAPABLE;
85 else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
86 && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
87 res = VPX_CODEC_INCAPABLE;
92 if (!(res = iface->enc.mr_get_mem_loc(cfg, &mem_loc))) {
93 for (i = 0; i < num_enc; i++) {
94 vpx_codec_priv_enc_mr_cfg_t mr_cfg;
96 /* Validate down-sampling factor. */
97 if (dsf->num < 1 || dsf->num > 4096 || dsf->den < 1 ||
98 dsf->den > dsf->num) {
99 res = VPX_CODEC_INVALID_PARAM;
103 mr_cfg.mr_low_res_mode_info = mem_loc;
104 mr_cfg.mr_total_resolutions = num_enc;
105 mr_cfg.mr_encoder_id = num_enc - 1 - i;
106 mr_cfg.mr_down_sampling_factor.num = dsf->num;
107 mr_cfg.mr_down_sampling_factor.den = dsf->den;
109 /* Force Key-frame synchronization. Namely, encoder at higher
110 * resolution always use the same frame_type chosen by the
111 * lowest-resolution encoder.
113 if (mr_cfg.mr_encoder_id)
114 cfg->kf_mode = VPX_KF_DISABLED;
117 ctx->name = iface->name;
119 ctx->init_flags = flags;
120 ctx->config.enc = cfg;
121 res = ctx->iface->init(ctx, &mr_cfg);
124 const char *error_detail =
125 ctx->priv ? ctx->priv->err_detail : NULL;
126 /* Destroy current ctx */
127 ctx->err_detail = error_detail;
128 vpx_codec_destroy(ctx);
130 /* Destroy already allocated high-level ctx */
133 ctx->err_detail = error_detail;
134 vpx_codec_destroy(ctx);
150 return SAVE_STATUS(ctx, res);
154 vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface,
155 vpx_codec_enc_cfg_t *cfg,
156 unsigned int usage) {
158 vpx_codec_enc_cfg_map_t *map;
161 if (!iface || !cfg || usage > INT_MAX)
162 res = VPX_CODEC_INVALID_PARAM;
163 else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
164 res = VPX_CODEC_INCAPABLE;
166 res = VPX_CODEC_INVALID_PARAM;
168 for (i = 0; i < iface->enc.cfg_map_count; ++i) {
169 map = iface->enc.cfg_maps + i;
170 if (map->usage == (int)usage) {
172 cfg->g_usage = usage;
183 #if ARCH_X86 || ARCH_X86_64
184 /* On X86, disable the x87 unit's internal 80 bit precision for better
185 * consistency with the SSE unit's 64 bit precision.
187 #include "vpx_ports/x86.h"
188 #define FLOATING_POINT_INIT() do {\
189 unsigned short x87_orig_mode = x87_set_double_precision();
190 #define FLOATING_POINT_RESTORE() \
191 x87_set_control_word(x87_orig_mode); }while(0)
195 static void FLOATING_POINT_INIT() {}
196 static void FLOATING_POINT_RESTORE() {}
200 vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx,
201 const vpx_image_t *img,
203 unsigned long duration,
204 vpx_enc_frame_flags_t flags,
205 unsigned long deadline) {
206 vpx_codec_err_t res = VPX_CODEC_OK;
208 if (!ctx || (img && !duration))
209 res = VPX_CODEC_INVALID_PARAM;
210 else if (!ctx->iface || !ctx->priv)
211 res = VPX_CODEC_ERROR;
212 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
213 res = VPX_CODEC_INCAPABLE;
215 unsigned int num_enc = ctx->priv->enc.total_encoders;
217 /* Execute in a normalized floating point environment, if the platform
220 FLOATING_POINT_INIT();
223 res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts,
224 duration, flags, deadline);
226 /* Multi-resolution encoding:
227 * Encode multi-levels in reverse order. For example,
228 * if mr_total_resolutions = 3, first encode level 2,
229 * then encode level 1, and finally encode level 0.
234 if (img) img += num_enc - 1;
236 for (i = num_enc - 1; i >= 0; i--) {
237 if ((res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts,
238 duration, flags, deadline)))
247 FLOATING_POINT_RESTORE();
250 return SAVE_STATUS(ctx, res);
254 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
255 vpx_codec_iter_t *iter) {
256 const vpx_codec_cx_pkt_t *pkt = NULL;
260 ctx->err = VPX_CODEC_INVALID_PARAM;
261 else if (!ctx->iface || !ctx->priv)
262 ctx->err = VPX_CODEC_ERROR;
263 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
264 ctx->err = VPX_CODEC_INCAPABLE;
266 pkt = ctx->iface->enc.get_cx_data(get_alg_priv(ctx), iter);
269 if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
270 // If the application has specified a destination area for the
271 // compressed data, and the codec has not placed the data there,
272 // and it fits, copy it.
273 vpx_codec_priv_t *const priv = ctx->priv;
274 char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf;
277 pkt->data.raw.buf != dst_buf &&
278 pkt->data.raw.sz + priv->enc.cx_data_pad_before +
279 priv->enc.cx_data_pad_after <= priv->enc.cx_data_dst_buf.sz) {
280 vpx_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt;
282 memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf,
284 *modified_pkt = *pkt;
285 modified_pkt->data.raw.buf = dst_buf;
286 modified_pkt->data.raw.sz += priv->enc.cx_data_pad_before +
287 priv->enc.cx_data_pad_after;
291 if (dst_buf == pkt->data.raw.buf) {
292 priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
293 priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
301 vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx,
302 const vpx_fixed_buf_t *buf,
303 unsigned int pad_before,
304 unsigned int pad_after) {
305 if (!ctx || !ctx->priv)
306 return VPX_CODEC_INVALID_PARAM;
309 ctx->priv->enc.cx_data_dst_buf = *buf;
310 ctx->priv->enc.cx_data_pad_before = pad_before;
311 ctx->priv->enc.cx_data_pad_after = pad_after;
313 ctx->priv->enc.cx_data_dst_buf.buf = NULL;
314 ctx->priv->enc.cx_data_dst_buf.sz = 0;
315 ctx->priv->enc.cx_data_pad_before = 0;
316 ctx->priv->enc.cx_data_pad_after = 0;
323 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx) {
324 vpx_image_t *img = NULL;
327 if (!ctx->iface || !ctx->priv)
328 ctx->err = VPX_CODEC_ERROR;
329 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
330 ctx->err = VPX_CODEC_INCAPABLE;
331 else if (!ctx->iface->enc.get_preview)
332 ctx->err = VPX_CODEC_INCAPABLE;
334 img = ctx->iface->enc.get_preview(get_alg_priv(ctx));
341 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx) {
342 vpx_fixed_buf_t *buf = NULL;
345 if (!ctx->iface || !ctx->priv)
346 ctx->err = VPX_CODEC_ERROR;
347 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
348 ctx->err = VPX_CODEC_INCAPABLE;
349 else if (!ctx->iface->enc.get_glob_hdrs)
350 ctx->err = VPX_CODEC_INCAPABLE;
352 buf = ctx->iface->enc.get_glob_hdrs(get_alg_priv(ctx));
359 vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx,
360 const vpx_codec_enc_cfg_t *cfg) {
363 if (!ctx || !ctx->iface || !ctx->priv || !cfg)
364 res = VPX_CODEC_INVALID_PARAM;
365 else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
366 res = VPX_CODEC_INCAPABLE;
368 res = ctx->iface->enc.cfg_set(get_alg_priv(ctx), cfg);
370 return SAVE_STATUS(ctx, res);
374 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
375 const struct vpx_codec_cx_pkt *pkt) {
376 if (list->cnt < list->max) {
377 list->pkts[list->cnt++] = *pkt;
385 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
386 vpx_codec_iter_t *iter) {
387 const vpx_codec_cx_pkt_t *pkt;
393 pkt = (const vpx_codec_cx_pkt_t *)*iter;
395 if ((size_t)(pkt - list->pkts) < list->cnt)