• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 /*!\file
13  * \brief Provides the high level interface to wrap encoder algorithms.
14  *
15  */
16 #include "config/aom_config.h"
17 
18 #if HAVE_FEXCEPT
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif
22 #include <fenv.h>
23 #endif
24 
25 #include <limits.h>
26 #include <string.h>
27 
28 #include "aom/aom_encoder.h"
29 #include "aom/internal/aom_codec_internal.h"
30 
31 #define SAVE_STATUS(ctx, var) (ctx ? (ctx->err = var) : var)
32 
get_alg_priv(aom_codec_ctx_t * ctx)33 static aom_codec_alg_priv_t *get_alg_priv(aom_codec_ctx_t *ctx) {
34   return (aom_codec_alg_priv_t *)ctx->priv;
35 }
36 
aom_codec_enc_init_ver(aom_codec_ctx_t * ctx,aom_codec_iface_t * iface,const aom_codec_enc_cfg_t * cfg,aom_codec_flags_t flags,int ver)37 aom_codec_err_t aom_codec_enc_init_ver(aom_codec_ctx_t *ctx,
38                                        aom_codec_iface_t *iface,
39                                        const aom_codec_enc_cfg_t *cfg,
40                                        aom_codec_flags_t flags, int ver) {
41   aom_codec_err_t res;
42 
43   if (ver != AOM_ENCODER_ABI_VERSION)
44     res = AOM_CODEC_ABI_MISMATCH;
45   else if (!ctx || !iface || !cfg)
46     res = AOM_CODEC_INVALID_PARAM;
47   else if (iface->abi_version != AOM_CODEC_INTERNAL_ABI_VERSION)
48     res = AOM_CODEC_ABI_MISMATCH;
49   else if (!(iface->caps & AOM_CODEC_CAP_ENCODER))
50     res = AOM_CODEC_INCAPABLE;
51   else if ((flags & AOM_CODEC_USE_PSNR) && !(iface->caps & AOM_CODEC_CAP_PSNR))
52     res = AOM_CODEC_INCAPABLE;
53   else {
54     ctx->iface = iface;
55     ctx->name = iface->name;
56     ctx->priv = NULL;
57     ctx->init_flags = flags;
58     ctx->config.enc = cfg;
59     res = ctx->iface->init(ctx);
60 
61     if (res) {
62       ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
63       aom_codec_destroy(ctx);
64     }
65   }
66 
67   return SAVE_STATUS(ctx, res);
68 }
69 
aom_codec_enc_config_default(aom_codec_iface_t * iface,aom_codec_enc_cfg_t * cfg,unsigned int usage)70 aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface,
71                                              aom_codec_enc_cfg_t *cfg,
72                                              unsigned int usage) {
73   aom_codec_err_t res;
74   int i;
75 
76   if (!iface || !cfg)
77     res = AOM_CODEC_INVALID_PARAM;
78   else if (!(iface->caps & AOM_CODEC_CAP_ENCODER))
79     res = AOM_CODEC_INCAPABLE;
80   else {
81     res = AOM_CODEC_INVALID_PARAM;
82 
83     for (i = 0; i < iface->enc.cfg_count; ++i) {
84       if (iface->enc.cfgs[i].g_usage == usage) {
85         *cfg = iface->enc.cfgs[i];
86         res = AOM_CODEC_OK;
87         break;
88       }
89     }
90   }
91   /* default values */
92   if (cfg) {
93     memset(&cfg->encoder_cfg, 0, sizeof(cfg->encoder_cfg));
94     cfg->encoder_cfg.super_block_size = 0;  // Dynamic
95     cfg->encoder_cfg.max_partition_size = 128;
96     cfg->encoder_cfg.min_partition_size = 4;
97     cfg->encoder_cfg.disable_trellis_quant = 3;
98   }
99   return res;
100 }
101 
102 #if ARCH_X86 || ARCH_X86_64
103 /* On X86, disable the x87 unit's internal 80 bit precision for better
104  * consistency with the SSE unit's 64 bit precision.
105  */
106 #include "aom_ports/x86.h"
107 #define FLOATING_POINT_SET_PRECISION \
108   unsigned short x87_orig_mode = x87_set_double_precision();
109 #define FLOATING_POINT_RESTORE_PRECISION x87_set_control_word(x87_orig_mode);
110 #else
111 #define FLOATING_POINT_SET_PRECISION
112 #define FLOATING_POINT_RESTORE_PRECISION
113 #endif  // ARCH_X86 || ARCH_X86_64
114 
115 #if HAVE_FEXCEPT && CONFIG_DEBUG
116 #define FLOATING_POINT_SET_EXCEPTIONS \
117   const int float_excepts =           \
118       feenableexcept(FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW);
119 #define FLOATING_POINT_RESTORE_EXCEPTIONS \
120   fedisableexcept(FE_ALL_EXCEPT);         \
121   feenableexcept(float_excepts);
122 #else
123 #define FLOATING_POINT_SET_EXCEPTIONS
124 #define FLOATING_POINT_RESTORE_EXCEPTIONS
125 #endif  // HAVE_FEXCEPT && CONFIG_DEBUG
126 
127 /* clang-format off */
128 #define FLOATING_POINT_INIT    \
129   do {                         \
130   FLOATING_POINT_SET_PRECISION \
131   FLOATING_POINT_SET_EXCEPTIONS
132 
133 #define FLOATING_POINT_RESTORE      \
134   FLOATING_POINT_RESTORE_EXCEPTIONS \
135   FLOATING_POINT_RESTORE_PRECISION  \
136   } while (0);
137 /* clang-format on */
138 
aom_codec_encode(aom_codec_ctx_t * ctx,const aom_image_t * img,aom_codec_pts_t pts,unsigned long duration,aom_enc_frame_flags_t flags)139 aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img,
140                                  aom_codec_pts_t pts, unsigned long duration,
141                                  aom_enc_frame_flags_t flags) {
142   aom_codec_err_t res = AOM_CODEC_OK;
143 
144   if (!ctx || (img && !duration))
145     res = AOM_CODEC_INVALID_PARAM;
146   else if (!ctx->iface || !ctx->priv)
147     res = AOM_CODEC_ERROR;
148   else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
149     res = AOM_CODEC_INCAPABLE;
150   else {
151     /* Execute in a normalized floating point environment, if the platform
152      * requires it.
153      */
154     FLOATING_POINT_INIT
155     res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration, flags);
156     FLOATING_POINT_RESTORE
157   }
158 
159   return SAVE_STATUS(ctx, res);
160 }
161 
aom_codec_get_cx_data(aom_codec_ctx_t * ctx,aom_codec_iter_t * iter)162 const aom_codec_cx_pkt_t *aom_codec_get_cx_data(aom_codec_ctx_t *ctx,
163                                                 aom_codec_iter_t *iter) {
164   const aom_codec_cx_pkt_t *pkt = NULL;
165 
166   if (ctx) {
167     if (!iter)
168       ctx->err = AOM_CODEC_INVALID_PARAM;
169     else if (!ctx->iface || !ctx->priv)
170       ctx->err = AOM_CODEC_ERROR;
171     else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
172       ctx->err = AOM_CODEC_INCAPABLE;
173     else
174       pkt = ctx->iface->enc.get_cx_data(get_alg_priv(ctx), iter);
175   }
176 
177   if (pkt && pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
178     // If the application has specified a destination area for the
179     // compressed data, and the codec has not placed the data there,
180     // and it fits, copy it.
181     aom_codec_priv_t *const priv = ctx->priv;
182     char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf;
183 
184     if (dst_buf && pkt->data.raw.buf != dst_buf &&
185         pkt->data.raw.sz + priv->enc.cx_data_pad_before +
186                 priv->enc.cx_data_pad_after <=
187             priv->enc.cx_data_dst_buf.sz) {
188       aom_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt;
189 
190       memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf,
191              pkt->data.raw.sz);
192       *modified_pkt = *pkt;
193       modified_pkt->data.raw.buf = dst_buf;
194       modified_pkt->data.raw.sz +=
195           priv->enc.cx_data_pad_before + priv->enc.cx_data_pad_after;
196       pkt = modified_pkt;
197     }
198 
199     if (dst_buf == pkt->data.raw.buf) {
200       priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
201       priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
202     }
203   }
204 
205   return pkt;
206 }
207 
aom_codec_set_cx_data_buf(aom_codec_ctx_t * ctx,const aom_fixed_buf_t * buf,unsigned int pad_before,unsigned int pad_after)208 aom_codec_err_t aom_codec_set_cx_data_buf(aom_codec_ctx_t *ctx,
209                                           const aom_fixed_buf_t *buf,
210                                           unsigned int pad_before,
211                                           unsigned int pad_after) {
212   if (!ctx || !ctx->priv) return AOM_CODEC_INVALID_PARAM;
213 
214   if (buf) {
215     ctx->priv->enc.cx_data_dst_buf = *buf;
216     ctx->priv->enc.cx_data_pad_before = pad_before;
217     ctx->priv->enc.cx_data_pad_after = pad_after;
218   } else {
219     ctx->priv->enc.cx_data_dst_buf.buf = NULL;
220     ctx->priv->enc.cx_data_dst_buf.sz = 0;
221     ctx->priv->enc.cx_data_pad_before = 0;
222     ctx->priv->enc.cx_data_pad_after = 0;
223   }
224 
225   return AOM_CODEC_OK;
226 }
227 
aom_codec_get_preview_frame(aom_codec_ctx_t * ctx)228 const aom_image_t *aom_codec_get_preview_frame(aom_codec_ctx_t *ctx) {
229   aom_image_t *img = NULL;
230 
231   if (ctx) {
232     if (!ctx->iface || !ctx->priv)
233       ctx->err = AOM_CODEC_ERROR;
234     else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
235       ctx->err = AOM_CODEC_INCAPABLE;
236     else if (!ctx->iface->enc.get_preview)
237       ctx->err = AOM_CODEC_INCAPABLE;
238     else
239       img = ctx->iface->enc.get_preview(get_alg_priv(ctx));
240   }
241 
242   return img;
243 }
244 
aom_codec_get_global_headers(aom_codec_ctx_t * ctx)245 aom_fixed_buf_t *aom_codec_get_global_headers(aom_codec_ctx_t *ctx) {
246   aom_fixed_buf_t *buf = NULL;
247 
248   if (ctx) {
249     if (!ctx->iface || !ctx->priv)
250       ctx->err = AOM_CODEC_ERROR;
251     else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
252       ctx->err = AOM_CODEC_INCAPABLE;
253     else if (!ctx->iface->enc.get_glob_hdrs)
254       ctx->err = AOM_CODEC_INCAPABLE;
255     else
256       buf = ctx->iface->enc.get_glob_hdrs(get_alg_priv(ctx));
257   }
258 
259   return buf;
260 }
261 
aom_codec_enc_config_set(aom_codec_ctx_t * ctx,const aom_codec_enc_cfg_t * cfg)262 aom_codec_err_t aom_codec_enc_config_set(aom_codec_ctx_t *ctx,
263                                          const aom_codec_enc_cfg_t *cfg) {
264   aom_codec_err_t res;
265 
266   if (!ctx || !ctx->iface || !ctx->priv || !cfg)
267     res = AOM_CODEC_INVALID_PARAM;
268   else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
269     res = AOM_CODEC_INCAPABLE;
270   else
271     res = ctx->iface->enc.cfg_set(get_alg_priv(ctx), cfg);
272 
273   return SAVE_STATUS(ctx, res);
274 }
275 
aom_codec_pkt_list_add(struct aom_codec_pkt_list * list,const struct aom_codec_cx_pkt * pkt)276 int aom_codec_pkt_list_add(struct aom_codec_pkt_list *list,
277                            const struct aom_codec_cx_pkt *pkt) {
278   if (list->cnt < list->max) {
279     list->pkts[list->cnt++] = *pkt;
280     return 0;
281   }
282 
283   return 1;
284 }
285 
aom_codec_pkt_list_get(struct aom_codec_pkt_list * list,aom_codec_iter_t * iter)286 const aom_codec_cx_pkt_t *aom_codec_pkt_list_get(
287     struct aom_codec_pkt_list *list, aom_codec_iter_t *iter) {
288   const aom_codec_cx_pkt_t *pkt;
289 
290   if (!(*iter)) {
291     *iter = list->pkts;
292   }
293 
294   pkt = (const aom_codec_cx_pkt_t *)*iter;
295 
296   if ((size_t)(pkt - list->pkts) < list->cnt)
297     *iter = pkt + 1;
298   else
299     pkt = NULL;
300 
301   return pkt;
302 }
303