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
;
47 ctx
->name
= iface
->name
;
49 ctx
->init_flags
= flags
;
50 ctx
->config
.enc
= cfg
;
51 res
= ctx
->iface
->init(ctx
);
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
);
68 vpx_codec_err_t
vpx_codec_enc_config_default(vpx_codec_iface_t
*iface
,
69 vpx_codec_enc_cfg_t
*cfg
,
73 vpx_codec_enc_cfg_map_t
*map
;
75 if (!iface
|| !cfg
|| usage
> INT_MAX
)
76 res
= VPX_CODEC_INVALID_PARAM
;
77 else if (!(iface
->caps
& VPX_CODEC_CAP_ENCODER
))
78 res
= VPX_CODEC_INCAPABLE
;
81 res
= VPX_CODEC_INVALID_PARAM
;
83 for (map
= iface
->enc
.cfg_maps
; map
->usage
>= 0; map
++)
85 if (map
->usage
== (int)usage
)
99 #if ARCH_X86 || ARCH_X86_64
100 /* On X86, disable the x87 unit's internal 80 bit precision for better
101 * consistency with the SSE unit's 64 bit precision.
103 #include "vpx_ports/x86.h"
104 #define FLOATING_POINT_INIT() do {\
105 unsigned short x87_orig_mode = x87_set_double_precision();
106 #define FLOATING_POINT_RESTORE() \
107 x87_set_control_word(x87_orig_mode); }while(0)
111 static void FLOATING_POINT_INIT() {}
112 static void FLOATING_POINT_RESTORE() {}
116 vpx_codec_err_t
vpx_codec_encode(vpx_codec_ctx_t
*ctx
,
117 const vpx_image_t
*img
,
119 unsigned long duration
,
120 vpx_enc_frame_flags_t flags
,
121 unsigned long deadline
)
125 if (!ctx
|| (img
&& !duration
))
126 res
= VPX_CODEC_INVALID_PARAM
;
127 else if (!ctx
->iface
|| !ctx
->priv
)
128 res
= VPX_CODEC_ERROR
;
129 else if (!(ctx
->iface
->caps
& VPX_CODEC_CAP_ENCODER
))
130 res
= VPX_CODEC_INCAPABLE
;
133 /* Execute in a normalized floating point environment, if the platform
136 FLOATING_POINT_INIT();
137 res
= ctx
->iface
->enc
.encode(ctx
->priv
->alg_priv
, img
, pts
,
138 duration
, flags
, deadline
);
139 FLOATING_POINT_RESTORE();
142 return SAVE_STATUS(ctx
, res
);
146 const vpx_codec_cx_pkt_t
*vpx_codec_get_cx_data(vpx_codec_ctx_t
*ctx
,
147 vpx_codec_iter_t
*iter
)
149 const vpx_codec_cx_pkt_t
*pkt
= NULL
;
154 ctx
->err
= VPX_CODEC_INVALID_PARAM
;
155 else if (!ctx
->iface
|| !ctx
->priv
)
156 ctx
->err
= VPX_CODEC_ERROR
;
157 else if (!(ctx
->iface
->caps
& VPX_CODEC_CAP_ENCODER
))
158 ctx
->err
= VPX_CODEC_INCAPABLE
;
160 pkt
= ctx
->iface
->enc
.get_cx_data(ctx
->priv
->alg_priv
, iter
);
163 if (pkt
&& pkt
->kind
== VPX_CODEC_CX_FRAME_PKT
)
165 /* If the application has specified a destination area for the
166 * compressed data, and the codec has not placed the data there,
167 * and it fits, copy it.
169 char *dst_buf
= ctx
->priv
->enc
.cx_data_dst_buf
.buf
;
172 && pkt
->data
.raw
.buf
!= dst_buf
174 + ctx
->priv
->enc
.cx_data_pad_before
175 + ctx
->priv
->enc
.cx_data_pad_after
176 <= ctx
->priv
->enc
.cx_data_dst_buf
.sz
)
178 vpx_codec_cx_pkt_t
*modified_pkt
= &ctx
->priv
->enc
.cx_data_pkt
;
180 memcpy(dst_buf
+ ctx
->priv
->enc
.cx_data_pad_before
,
181 pkt
->data
.raw
.buf
, pkt
->data
.raw
.sz
);
182 *modified_pkt
= *pkt
;
183 modified_pkt
->data
.raw
.buf
= dst_buf
;
184 modified_pkt
->data
.raw
.sz
+= ctx
->priv
->enc
.cx_data_pad_before
185 + ctx
->priv
->enc
.cx_data_pad_after
;
189 if (dst_buf
== pkt
->data
.raw
.buf
)
191 ctx
->priv
->enc
.cx_data_dst_buf
.buf
= dst_buf
+ pkt
->data
.raw
.sz
;
192 ctx
->priv
->enc
.cx_data_dst_buf
.sz
-= pkt
->data
.raw
.sz
;
200 vpx_codec_err_t
vpx_codec_set_cx_data_buf(vpx_codec_ctx_t
*ctx
,
201 const vpx_fixed_buf_t
*buf
,
202 unsigned int pad_before
,
203 unsigned int pad_after
)
205 if (!ctx
|| !ctx
->priv
)
206 return VPX_CODEC_INVALID_PARAM
;
210 ctx
->priv
->enc
.cx_data_dst_buf
= *buf
;
211 ctx
->priv
->enc
.cx_data_pad_before
= pad_before
;
212 ctx
->priv
->enc
.cx_data_pad_after
= pad_after
;
216 ctx
->priv
->enc
.cx_data_dst_buf
.buf
= NULL
;
217 ctx
->priv
->enc
.cx_data_dst_buf
.sz
= 0;
218 ctx
->priv
->enc
.cx_data_pad_before
= 0;
219 ctx
->priv
->enc
.cx_data_pad_after
= 0;
226 const vpx_image_t
*vpx_codec_get_preview_frame(vpx_codec_ctx_t
*ctx
)
228 vpx_image_t
*img
= NULL
;
232 if (!ctx
->iface
|| !ctx
->priv
)
233 ctx
->err
= VPX_CODEC_ERROR
;
234 else if (!(ctx
->iface
->caps
& VPX_CODEC_CAP_ENCODER
))
235 ctx
->err
= VPX_CODEC_INCAPABLE
;
236 else if (!ctx
->iface
->enc
.get_preview
)
237 ctx
->err
= VPX_CODEC_INCAPABLE
;
239 img
= ctx
->iface
->enc
.get_preview(ctx
->priv
->alg_priv
);
246 vpx_fixed_buf_t
*vpx_codec_get_global_headers(vpx_codec_ctx_t
*ctx
)
248 vpx_fixed_buf_t
*buf
= NULL
;
252 if (!ctx
->iface
|| !ctx
->priv
)
253 ctx
->err
= VPX_CODEC_ERROR
;
254 else if (!(ctx
->iface
->caps
& VPX_CODEC_CAP_ENCODER
))
255 ctx
->err
= VPX_CODEC_INCAPABLE
;
256 else if (!ctx
->iface
->enc
.get_glob_hdrs
)
257 ctx
->err
= VPX_CODEC_INCAPABLE
;
259 buf
= ctx
->iface
->enc
.get_glob_hdrs(ctx
->priv
->alg_priv
);
266 vpx_codec_err_t
vpx_codec_enc_config_set(vpx_codec_ctx_t
*ctx
,
267 const vpx_codec_enc_cfg_t
*cfg
)
271 if (!ctx
|| !ctx
->iface
|| !ctx
->priv
|| !cfg
)
272 res
= VPX_CODEC_INVALID_PARAM
;
273 else if (!(ctx
->iface
->caps
& VPX_CODEC_CAP_ENCODER
))
274 res
= VPX_CODEC_INCAPABLE
;
276 res
= ctx
->iface
->enc
.cfg_set(ctx
->priv
->alg_priv
, cfg
);
278 return SAVE_STATUS(ctx
, res
);
282 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list
*list
,
283 const struct vpx_codec_cx_pkt
*pkt
)
285 if (list
->cnt
< list
->max
)
287 list
->pkts
[list
->cnt
++] = *pkt
;
295 const vpx_codec_cx_pkt_t
*vpx_codec_pkt_list_get(struct vpx_codec_pkt_list
*list
,
296 vpx_codec_iter_t
*iter
)
298 const vpx_codec_cx_pkt_t
*pkt
;
305 pkt
= (const void *) * iter
;
307 if ((size_t)(pkt
- list
->pkts
) < list
->cnt
)