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