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
);
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
);
71 vpx_codec_err_t
vpx_codec_enc_config_default(vpx_codec_iface_t
*iface
,
72 vpx_codec_enc_cfg_t
*cfg
,
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
;
84 res
= VPX_CODEC_INVALID_PARAM
;
86 for (map
= iface
->enc
.cfg_maps
; map
->usage
>= 0; map
++)
88 if (map
->usage
== (int)usage
)
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)
114 static void FLOATING_POINT_INIT() {}
115 static void FLOATING_POINT_RESTORE() {}
119 vpx_codec_err_t
vpx_codec_encode(vpx_codec_ctx_t
*ctx
,
120 const vpx_image_t
*img
,
122 unsigned long duration
,
123 vpx_enc_frame_flags_t flags
,
124 unsigned long deadline
)
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
;
136 /* Execute in a normalized floating point environment, if the platform
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
;
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
;
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
;
175 && pkt
->data
.raw
.buf
!= dst_buf
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
;
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
;
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
;
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
;
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;
229 const vpx_image_t
*vpx_codec_get_preview_frame(vpx_codec_ctx_t
*ctx
)
231 vpx_image_t
*img
= NULL
;
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
;
242 img
= ctx
->iface
->enc
.get_preview(ctx
->priv
->alg_priv
);
249 vpx_fixed_buf_t
*vpx_codec_get_global_headers(vpx_codec_ctx_t
*ctx
)
251 vpx_fixed_buf_t
*buf
= NULL
;
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
;
262 buf
= ctx
->iface
->enc
.get_glob_hdrs(ctx
->priv
->alg_priv
);
269 vpx_codec_err_t
vpx_codec_enc_config_set(vpx_codec_ctx_t
*ctx
,
270 const vpx_codec_enc_cfg_t
*cfg
)
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
;
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
;
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
;
308 pkt
= (const void *) * iter
;
310 if ((size_t)(pkt
- list
->pkts
) < list
->cnt
)