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