Merge "Added 2 temporal patterns with new parameters"
[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, NULL);
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 vpx_codec_err_t vpx_codec_enc_init_multi_ver(vpx_codec_ctx_t      *ctx,
70                                              vpx_codec_iface_t    *iface,
71                                              vpx_codec_enc_cfg_t  *cfg,
72                                              int                   s_lvl,
73                                              int                   num_enc,
74                                              vpx_codec_flags_t     flags,
75                                              vpx_rational_t       *dsf,
76                                              int                   ver)
77 {
78     vpx_codec_err_t res = 0;
79
80     if (ver != VPX_ENCODER_ABI_VERSION)
81         res = VPX_CODEC_ABI_MISMATCH;
82     else if (!ctx || !iface || !cfg || (num_enc > 16 || num_enc < 1))
83         res = VPX_CODEC_INVALID_PARAM;
84     else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
85         res = VPX_CODEC_ABI_MISMATCH;
86     else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
87         res = VPX_CODEC_INCAPABLE;
88     else if ((flags & VPX_CODEC_USE_XMA) && !(iface->caps & VPX_CODEC_CAP_XMA))
89         res = VPX_CODEC_INCAPABLE;
90     else if ((flags & VPX_CODEC_USE_PSNR)
91              && !(iface->caps & VPX_CODEC_CAP_PSNR))
92         res = VPX_CODEC_INCAPABLE;
93     else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION)
94              && !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
95         res = VPX_CODEC_INCAPABLE;
96     else
97     {
98         int i;
99         void *mem_loc = NULL;
100
101         if(!(res = iface->enc.mr_get_mem_loc(cfg, &mem_loc)))
102         {
103             for (i = s_lvl; i < num_enc; i++)
104             {
105                 vpx_codec_priv_enc_mr_cfg_t mr_cfg;
106
107                 /* Validate down-sampling factor. */
108                 if(dsf->num < 1 || dsf->num > 4096 || dsf->den < 1 ||
109                    dsf->den > dsf->num)
110                 {
111                     res = VPX_CODEC_INVALID_PARAM;
112                     break;
113                 }
114
115                 mr_cfg.mr_low_res_mode_info = mem_loc;
116                 mr_cfg.mr_total_resolutions = num_enc - s_lvl;
117                 mr_cfg.mr_encoder_id = num_enc-1-i;
118                 mr_cfg.mr_down_sampling_factor.num = dsf->num;
119                 mr_cfg.mr_down_sampling_factor.den = dsf->den;
120
121                 ctx->iface = iface;
122                 ctx->name = iface->name;
123                 ctx->priv = NULL;
124                 ctx->init_flags = flags;
125                 ctx->config.enc = cfg;
126                 res = ctx->iface->init(ctx, &mr_cfg);
127
128                 if (res)
129                 {
130                     ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
131                     vpx_codec_destroy(ctx);
132                 }
133
134                 if (ctx->priv)
135                     ctx->priv->iface = ctx->iface;
136
137                 if (res)
138                     break;
139
140                 ctx++;
141                 cfg++;
142                 dsf++;
143             }
144         }
145     }
146
147     return SAVE_STATUS(ctx, res);
148 }
149
150
151 vpx_codec_err_t  vpx_codec_enc_config_default(vpx_codec_iface_t    *iface,
152         vpx_codec_enc_cfg_t  *cfg,
153         unsigned int          usage)
154 {
155     vpx_codec_err_t res;
156     vpx_codec_enc_cfg_map_t *map;
157
158     if (!iface || !cfg || usage > INT_MAX)
159         res = VPX_CODEC_INVALID_PARAM;
160     else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
161         res = VPX_CODEC_INCAPABLE;
162     else
163     {
164         res = VPX_CODEC_INVALID_PARAM;
165
166         for (map = iface->enc.cfg_maps; map->usage >= 0; map++)
167         {
168             if (map->usage == (int)usage)
169             {
170                 *cfg = map->cfg;
171                 cfg->g_usage = usage;
172                 res = VPX_CODEC_OK;
173                 break;
174             }
175         }
176     }
177
178     return res;
179 }
180
181
182 #if ARCH_X86 || ARCH_X86_64
183 /* On X86, disable the x87 unit's internal 80 bit precision for better
184  * consistency with the SSE unit's 64 bit precision.
185  */
186 #include "vpx_ports/x86.h"
187 #define FLOATING_POINT_INIT() do {\
188         unsigned short x87_orig_mode = x87_set_double_precision();
189 #define FLOATING_POINT_RESTORE() \
190     x87_set_control_word(x87_orig_mode); }while(0)
191
192
193 #else
194 static void FLOATING_POINT_INIT() {}
195 static void FLOATING_POINT_RESTORE() {}
196 #endif
197
198
199 vpx_codec_err_t  vpx_codec_encode(vpx_codec_ctx_t            *ctx,
200                                   const vpx_image_t          *img,
201                                   vpx_codec_pts_t             pts,
202                                   unsigned long               duration,
203                                   vpx_enc_frame_flags_t       flags,
204                                   unsigned long               deadline)
205 {
206     vpx_codec_err_t res = 0;
207
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;
214     else
215     {
216         /* Execute in a normalized floating point environment, if the platform
217          * requires it.
218          */
219         unsigned int num_enc =ctx->priv->enc.total_encoders;
220
221         FLOATING_POINT_INIT();
222
223         if (num_enc == 1)
224             res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
225                                          duration, flags, deadline);
226         else
227         {
228             /* Multi-resolution encoding:
229              * Encode multi-levels in reverse order. For example,
230              * if mr_total_resolutions = 3, first encode level 2,
231              * then encode level 1, and finally encode level 0.
232              */
233             int i;
234
235             ctx += num_enc - 1;
236             if (img) img += num_enc - 1;
237
238             for (i = num_enc-1; i >= 0; i--)
239             {
240                 if ((res = ctx->iface->enc.encode(ctx->priv->alg_priv, img, pts,
241                                                   duration, flags, deadline)))
242                     break;
243
244                 ctx--;
245                 if (img) img--;
246             }
247             ctx++;
248         }
249
250         FLOATING_POINT_RESTORE();
251     }
252
253     return SAVE_STATUS(ctx, res);
254 }
255
256
257 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t   *ctx,
258         vpx_codec_iter_t  *iter)
259 {
260     const vpx_codec_cx_pkt_t *pkt = NULL;
261
262     if (ctx)
263     {
264         if (!iter)
265             ctx->err = VPX_CODEC_INVALID_PARAM;
266         else if (!ctx->iface || !ctx->priv)
267             ctx->err = VPX_CODEC_ERROR;
268         else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
269             ctx->err = VPX_CODEC_INCAPABLE;
270         else
271             pkt = ctx->iface->enc.get_cx_data(ctx->priv->alg_priv, iter);
272     }
273
274     if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT)
275     {
276         /* If the application has specified a destination area for the
277          * compressed data, and the codec has not placed the data there,
278          * and it fits, copy it.
279          */
280         char *dst_buf = ctx->priv->enc.cx_data_dst_buf.buf;
281
282         if (dst_buf
283             && pkt->data.raw.buf != dst_buf
284             && pkt->data.raw.sz
285             + ctx->priv->enc.cx_data_pad_before
286             + ctx->priv->enc.cx_data_pad_after
287             <= ctx->priv->enc.cx_data_dst_buf.sz)
288         {
289             vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt;
290
291             memcpy(dst_buf + ctx->priv->enc.cx_data_pad_before,
292                    pkt->data.raw.buf, pkt->data.raw.sz);
293             *modified_pkt = *pkt;
294             modified_pkt->data.raw.buf = dst_buf;
295             modified_pkt->data.raw.sz += ctx->priv->enc.cx_data_pad_before
296                                          + ctx->priv->enc.cx_data_pad_after;
297             pkt = modified_pkt;
298         }
299
300         if (dst_buf == pkt->data.raw.buf)
301         {
302             ctx->priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
303             ctx->priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
304         }
305     }
306
307     return pkt;
308 }
309
310
311 vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t       *ctx,
312         const vpx_fixed_buf_t *buf,
313         unsigned int           pad_before,
314         unsigned int           pad_after)
315 {
316     if (!ctx || !ctx->priv)
317         return VPX_CODEC_INVALID_PARAM;
318
319     if (buf)
320     {
321         ctx->priv->enc.cx_data_dst_buf = *buf;
322         ctx->priv->enc.cx_data_pad_before = pad_before;
323         ctx->priv->enc.cx_data_pad_after = pad_after;
324     }
325     else
326     {
327         ctx->priv->enc.cx_data_dst_buf.buf = NULL;
328         ctx->priv->enc.cx_data_dst_buf.sz = 0;
329         ctx->priv->enc.cx_data_pad_before = 0;
330         ctx->priv->enc.cx_data_pad_after = 0;
331     }
332
333     return VPX_CODEC_OK;
334 }
335
336
337 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t   *ctx)
338 {
339     vpx_image_t *img = NULL;
340
341     if (ctx)
342     {
343         if (!ctx->iface || !ctx->priv)
344             ctx->err = VPX_CODEC_ERROR;
345         else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
346             ctx->err = VPX_CODEC_INCAPABLE;
347         else if (!ctx->iface->enc.get_preview)
348             ctx->err = VPX_CODEC_INCAPABLE;
349         else
350             img = ctx->iface->enc.get_preview(ctx->priv->alg_priv);
351     }
352
353     return img;
354 }
355
356
357 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t   *ctx)
358 {
359     vpx_fixed_buf_t *buf = NULL;
360
361     if (ctx)
362     {
363         if (!ctx->iface || !ctx->priv)
364             ctx->err = VPX_CODEC_ERROR;
365         else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
366             ctx->err = VPX_CODEC_INCAPABLE;
367         else if (!ctx->iface->enc.get_glob_hdrs)
368             ctx->err = VPX_CODEC_INCAPABLE;
369         else
370             buf = ctx->iface->enc.get_glob_hdrs(ctx->priv->alg_priv);
371     }
372
373     return buf;
374 }
375
376
377 vpx_codec_err_t  vpx_codec_enc_config_set(vpx_codec_ctx_t            *ctx,
378         const vpx_codec_enc_cfg_t  *cfg)
379 {
380     vpx_codec_err_t res;
381
382     if (!ctx || !ctx->iface || !ctx->priv || !cfg)
383         res = VPX_CODEC_INVALID_PARAM;
384     else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
385         res = VPX_CODEC_INCAPABLE;
386     else
387         res = ctx->iface->enc.cfg_set(ctx->priv->alg_priv, cfg);
388
389     return SAVE_STATUS(ctx, res);
390 }
391
392
393 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
394                            const struct vpx_codec_cx_pkt *pkt)
395 {
396     if (list->cnt < list->max)
397     {
398         list->pkts[list->cnt++] = *pkt;
399         return 0;
400     }
401
402     return 1;
403 }
404
405
406 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(struct vpx_codec_pkt_list *list,
407         vpx_codec_iter_t           *iter)
408 {
409     const vpx_codec_cx_pkt_t *pkt;
410
411     if (!(*iter))
412     {
413         *iter = list->pkts;
414     }
415
416     pkt = (const void *) * iter;
417
418     if ((size_t)(pkt - list->pkts) < list->cnt)
419         *iter = pkt + 1;
420     else
421         pkt = NULL;
422
423     return pkt;
424 }