Merge "Don't override active_worst_quality in 2 pass"
[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
45     {
46         ctx->iface = iface;
47         ctx->name = iface->name;
48         ctx->priv = NULL;
49         ctx->init_flags = flags;
50         ctx->config.enc = cfg;
51         res = ctx->iface->init(ctx);
52
53         if (res)
54         {
55             ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
56             vpx_codec_destroy(ctx);
57         }
58
59         if (ctx->priv)
60             ctx->priv->iface = ctx->iface;
61     }
62
63     return SAVE_STATUS(ctx, res);
64 }
65
66
67
68 vpx_codec_err_t  vpx_codec_enc_config_default(vpx_codec_iface_t    *iface,
69         vpx_codec_enc_cfg_t  *cfg,
70         unsigned int          usage)
71 {
72     vpx_codec_err_t res;
73     vpx_codec_enc_cfg_map_t *map;
74
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;
79     else
80     {
81         res = VPX_CODEC_INVALID_PARAM;
82
83         for (map = iface->enc.cfg_maps; map->usage >= 0; map++)
84         {
85             if (map->usage == (int)usage)
86             {
87                 *cfg = map->cfg;
88                 cfg->g_usage = usage;
89                 res = VPX_CODEC_OK;
90                 break;
91             }
92         }
93     }
94
95     return res;
96 }
97
98
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.
102  */
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)
108
109
110 #else
111 static void FLOATING_POINT_INIT() {}
112 static void FLOATING_POINT_RESTORE() {}
113 #endif
114
115
116 vpx_codec_err_t  vpx_codec_encode(vpx_codec_ctx_t            *ctx,
117                                   const vpx_image_t          *img,
118                                   vpx_codec_pts_t             pts,
119                                   unsigned long               duration,
120                                   vpx_enc_frame_flags_t       flags,
121                                   unsigned long               deadline)
122 {
123     vpx_codec_err_t res;
124
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;
131     else
132     {
133         /* Execute in a normalized floating point environment, if the platform
134          * requires it.
135          */
136         FLOATING_POINT_INIT();
137         res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
138                                      duration, flags, deadline);
139         FLOATING_POINT_RESTORE();
140     }
141
142     return SAVE_STATUS(ctx, res);
143 }
144
145
146 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t   *ctx,
147         vpx_codec_iter_t  *iter)
148 {
149     const vpx_codec_cx_pkt_t *pkt = NULL;
150
151     if (ctx)
152     {
153         if (!iter)
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;
159         else
160             pkt = ctx->iface->enc.get_cx_data(ctx->priv->alg_priv, iter);
161     }
162
163     if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT)
164     {
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.
168          */
169         char *dst_buf = ctx->priv->enc.cx_data_dst_buf.buf;
170
171         if (dst_buf
172             && pkt->data.raw.buf != dst_buf
173             && pkt->data.raw.sz
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)
177         {
178             vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt;
179
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;
186             pkt = modified_pkt;
187         }
188
189         if (dst_buf == pkt->data.raw.buf)
190         {
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;
193         }
194     }
195
196     return pkt;
197 }
198
199
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)
204 {
205     if (!ctx || !ctx->priv)
206         return VPX_CODEC_INVALID_PARAM;
207
208     if (buf)
209     {
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;
213     }
214     else
215     {
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;
220     }
221
222     return VPX_CODEC_OK;
223 }
224
225
226 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t   *ctx)
227 {
228     vpx_image_t *img = NULL;
229
230     if (ctx)
231     {
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;
238         else
239             img = ctx->iface->enc.get_preview(ctx->priv->alg_priv);
240     }
241
242     return img;
243 }
244
245
246 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t   *ctx)
247 {
248     vpx_fixed_buf_t *buf = NULL;
249
250     if (ctx)
251     {
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;
258         else
259             buf = ctx->iface->enc.get_glob_hdrs(ctx->priv->alg_priv);
260     }
261
262     return buf;
263 }
264
265
266 vpx_codec_err_t  vpx_codec_enc_config_set(vpx_codec_ctx_t            *ctx,
267         const vpx_codec_enc_cfg_t  *cfg)
268 {
269     vpx_codec_err_t res;
270
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;
275     else
276         res = ctx->iface->enc.cfg_set(ctx->priv->alg_priv, cfg);
277
278     return SAVE_STATUS(ctx, res);
279 }
280
281
282 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
283                            const struct vpx_codec_cx_pkt *pkt)
284 {
285     if (list->cnt < list->max)
286     {
287         list->pkts[list->cnt++] = *pkt;
288         return 0;
289     }
290
291     return 1;
292 }
293
294
295 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
296         vpx_codec_iter_t           *iter)
297 {
298     const vpx_codec_cx_pkt_t *pkt;
299
300     if (!(*iter))
301     {
302         *iter = list->pkts;
303     }
304
305     pkt = (const void *) * iter;
306
307     if ((size_t)(pkt - list->pkts) < list->cnt)
308         *iter = pkt + 1;
309     else
310         pkt = NULL;
311
312     return pkt;
313 }