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