Proposing an extension to the encoder and decoder interfaces.
[profile/ivi/libvpx.git] / vpx / src / vpx_encoder.c
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10
11
12 /*!\file
13  * \brief Provides the high level interface to wrap encoder algorithms.
14  *
15  */
16 #include <limits.h>
17 #include <string.h>
18 #include "vpx/internal/vpx_codec_internal.h"
19 #include "vpx_config.h"
20
21 #define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var)
22
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,
27                                        int                   ver)
28 {
29     vpx_codec_err_t res;
30
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;
47     else
48     {
49         ctx->iface = iface;
50         ctx->name = iface->name;
51         ctx->priv = NULL;
52         ctx->init_flags = flags;
53         ctx->config.enc = cfg;
54         res = ctx->iface->init(ctx);
55
56         if (res)
57         {
58             ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
59             vpx_codec_destroy(ctx);
60         }
61
62         if (ctx->priv)
63             ctx->priv->iface = ctx->iface;
64     }
65
66     return SAVE_STATUS(ctx, res);
67 }
68
69
70
71 vpx_codec_err_t  vpx_codec_enc_config_default(vpx_codec_iface_t    *iface,
72         vpx_codec_enc_cfg_t  *cfg,
73         unsigned int          usage)
74 {
75     vpx_codec_err_t res;
76     vpx_codec_enc_cfg_map_t *map;
77
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;
82     else
83     {
84         res = VPX_CODEC_INVALID_PARAM;
85
86         for (map = iface->enc.cfg_maps; map->usage >= 0; map++)
87         {
88             if (map->usage == (int)usage)
89             {
90                 *cfg = map->cfg;
91                 cfg->g_usage = usage;
92                 res = VPX_CODEC_OK;
93                 break;
94             }
95         }
96     }
97
98     return res;
99 }
100
101
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.
105  */
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)
111
112
113 #else
114 static void FLOATING_POINT_INIT() {}
115 static void FLOATING_POINT_RESTORE() {}
116 #endif
117
118
119 vpx_codec_err_t  vpx_codec_encode(vpx_codec_ctx_t            *ctx,
120                                   const vpx_image_t          *img,
121                                   vpx_codec_pts_t             pts,
122                                   unsigned long               duration,
123                                   vpx_enc_frame_flags_t       flags,
124                                   unsigned long               deadline)
125 {
126     vpx_codec_err_t res;
127
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;
134     else
135     {
136         /* Execute in a normalized floating point environment, if the platform
137          * requires it.
138          */
139         FLOATING_POINT_INIT();
140         res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
141                                      duration, flags, deadline);
142         FLOATING_POINT_RESTORE();
143     }
144
145     return SAVE_STATUS(ctx, res);
146 }
147
148
149 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t   *ctx,
150         vpx_codec_iter_t  *iter)
151 {
152     const vpx_codec_cx_pkt_t *pkt = NULL;
153
154     if (ctx)
155     {
156         if (!iter)
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;
162         else
163             pkt = ctx->iface->enc.get_cx_data(ctx->priv->alg_priv, iter);
164     }
165
166     if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT)
167     {
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.
171          */
172         char *dst_buf = ctx->priv->enc.cx_data_dst_buf.buf;
173
174         if (dst_buf
175             && pkt->data.raw.buf != dst_buf
176             && pkt->data.raw.sz
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)
180         {
181             vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt;
182
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;
189             pkt = modified_pkt;
190         }
191
192         if (dst_buf == pkt->data.raw.buf)
193         {
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;
196         }
197     }
198
199     return pkt;
200 }
201
202
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)
207 {
208     if (!ctx || !ctx->priv)
209         return VPX_CODEC_INVALID_PARAM;
210
211     if (buf)
212     {
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;
216     }
217     else
218     {
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;
223     }
224
225     return VPX_CODEC_OK;
226 }
227
228
229 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t   *ctx)
230 {
231     vpx_image_t *img = NULL;
232
233     if (ctx)
234     {
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;
241         else
242             img = ctx->iface->enc.get_preview(ctx->priv->alg_priv);
243     }
244
245     return img;
246 }
247
248
249 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t   *ctx)
250 {
251     vpx_fixed_buf_t *buf = NULL;
252
253     if (ctx)
254     {
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;
261         else
262             buf = ctx->iface->enc.get_glob_hdrs(ctx->priv->alg_priv);
263     }
264
265     return buf;
266 }
267
268
269 vpx_codec_err_t  vpx_codec_enc_config_set(vpx_codec_ctx_t            *ctx,
270         const vpx_codec_enc_cfg_t  *cfg)
271 {
272     vpx_codec_err_t res;
273
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;
278     else
279         res = ctx->iface->enc.cfg_set(ctx->priv->alg_priv, cfg);
280
281     return SAVE_STATUS(ctx, res);
282 }
283
284
285 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
286                            const struct vpx_codec_cx_pkt *pkt)
287 {
288     if (list->cnt < list->max)
289     {
290         list->pkts[list->cnt++] = *pkt;
291         return 0;
292     }
293
294     return 1;
295 }
296
297
298 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
299         vpx_codec_iter_t           *iter)
300 {
301     const vpx_codec_cx_pkt_t *pkt;
302
303     if (!(*iter))
304     {
305         *iter = list->pkts;
306     }
307
308     pkt = (const void *) * iter;
309
310     if ((size_t)(pkt - list->pkts) < list->cnt)
311         *iter = pkt + 1;
312     else
313         pkt = NULL;
314
315     return pkt;
316 }