• 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 if (cfg->g_bit_depth > 8 && (flags & AOM_CODEC_USE_HIGHBITDEPTH) == 0) {
54     res = AOM_CODEC_INVALID_PARAM;
55     ctx->err_detail =
56         "High bit-depth used without the AOM_CODEC_USE_HIGHBITDEPTH flag.";
57   } else {
58     ctx->iface = iface;
59     ctx->name = iface->name;
60     ctx->priv = NULL;
61     ctx->init_flags = flags;
62     ctx->config.enc = cfg;
63     res = ctx->iface->init(ctx);
64 
65     if (res) {
66       ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
67       aom_codec_destroy(ctx);
68     }
69   }
70 
71   return SAVE_STATUS(ctx, res);
72 }
73 
aom_codec_enc_config_default(aom_codec_iface_t * iface,aom_codec_enc_cfg_t * cfg,unsigned int usage)74 aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface,
75                                              aom_codec_enc_cfg_t *cfg,
76                                              unsigned int usage) {
77   aom_codec_err_t res;
78   int i;
79 
80   if (!iface || !cfg)
81     res = AOM_CODEC_INVALID_PARAM;
82   else if (!(iface->caps & AOM_CODEC_CAP_ENCODER))
83     res = AOM_CODEC_INCAPABLE;
84   else {
85     res = AOM_CODEC_INVALID_PARAM;
86 
87     for (i = 0; i < iface->enc.cfg_count; ++i) {
88       if (iface->enc.cfgs[i].g_usage == usage) {
89         *cfg = iface->enc.cfgs[i];
90         res = AOM_CODEC_OK;
91         break;
92       }
93     }
94   }
95   /* default values */
96   if (cfg) {
97     memset(&cfg->encoder_cfg, 0, sizeof(cfg->encoder_cfg));
98     cfg->encoder_cfg.super_block_size = 0;  // Dynamic
99     cfg->encoder_cfg.max_partition_size = 128;
100     cfg->encoder_cfg.min_partition_size = 4;
101     cfg->encoder_cfg.disable_trellis_quant = 3;
102   }
103   return res;
104 }
105 
106 #if ARCH_X86 || ARCH_X86_64
107 /* On X86, disable the x87 unit's internal 80 bit precision for better
108  * consistency with the SSE unit's 64 bit precision.
109  */
110 #include "aom_ports/x86.h"
111 #define FLOATING_POINT_SET_PRECISION \
112   unsigned short x87_orig_mode = x87_set_double_precision();
113 #define FLOATING_POINT_RESTORE_PRECISION x87_set_control_word(x87_orig_mode);
114 #else
115 #define FLOATING_POINT_SET_PRECISION
116 #define FLOATING_POINT_RESTORE_PRECISION
117 #endif  // ARCH_X86 || ARCH_X86_64
118 
119 #if HAVE_FEXCEPT && CONFIG_DEBUG
120 #define FLOATING_POINT_SET_EXCEPTIONS \
121   const int float_excepts =           \
122       feenableexcept(FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW);
123 #define FLOATING_POINT_RESTORE_EXCEPTIONS \
124   fedisableexcept(FE_ALL_EXCEPT);         \
125   feenableexcept(float_excepts);
126 #else
127 #define FLOATING_POINT_SET_EXCEPTIONS
128 #define FLOATING_POINT_RESTORE_EXCEPTIONS
129 #endif  // HAVE_FEXCEPT && CONFIG_DEBUG
130 
131 /* clang-format off */
132 #define FLOATING_POINT_INIT    \
133   do {                         \
134   FLOATING_POINT_SET_PRECISION \
135   FLOATING_POINT_SET_EXCEPTIONS
136 
137 #define FLOATING_POINT_RESTORE      \
138   FLOATING_POINT_RESTORE_EXCEPTIONS \
139   FLOATING_POINT_RESTORE_PRECISION  \
140   } while (0);
141 /* clang-format on */
142 
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)143 aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img,
144                                  aom_codec_pts_t pts, unsigned long duration,
145                                  aom_enc_frame_flags_t flags) {
146   aom_codec_err_t res = AOM_CODEC_OK;
147 
148   if (!ctx || (img && !duration))
149     res = AOM_CODEC_INVALID_PARAM;
150   else if (!ctx->iface || !ctx->priv)
151     res = AOM_CODEC_ERROR;
152   else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
153     res = AOM_CODEC_INCAPABLE;
154   else {
155     /* Execute in a normalized floating point environment, if the platform
156      * requires it.
157      */
158     FLOATING_POINT_INIT
159     res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration, flags);
160     FLOATING_POINT_RESTORE
161   }
162 
163   return SAVE_STATUS(ctx, res);
164 }
165 
aom_codec_get_cx_data(aom_codec_ctx_t * ctx,aom_codec_iter_t * iter)166 const aom_codec_cx_pkt_t *aom_codec_get_cx_data(aom_codec_ctx_t *ctx,
167                                                 aom_codec_iter_t *iter) {
168   const aom_codec_cx_pkt_t *pkt = NULL;
169 
170   if (ctx) {
171     if (!iter)
172       ctx->err = AOM_CODEC_INVALID_PARAM;
173     else if (!ctx->iface || !ctx->priv)
174       ctx->err = AOM_CODEC_ERROR;
175     else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
176       ctx->err = AOM_CODEC_INCAPABLE;
177     else
178       pkt = ctx->iface->enc.get_cx_data(get_alg_priv(ctx), iter);
179   }
180 
181   if (pkt && pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
182     // If the application has specified a destination area for the
183     // compressed data, and the codec has not placed the data there,
184     // and it fits, copy it.
185     aom_codec_priv_t *const priv = ctx->priv;
186     char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf;
187 
188     if (dst_buf && pkt->data.raw.buf != dst_buf &&
189         pkt->data.raw.sz + priv->enc.cx_data_pad_before +
190                 priv->enc.cx_data_pad_after <=
191             priv->enc.cx_data_dst_buf.sz) {
192       aom_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt;
193 
194       memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf,
195              pkt->data.raw.sz);
196       *modified_pkt = *pkt;
197       modified_pkt->data.raw.buf = dst_buf;
198       modified_pkt->data.raw.sz +=
199           priv->enc.cx_data_pad_before + priv->enc.cx_data_pad_after;
200       pkt = modified_pkt;
201     }
202 
203     if (dst_buf == pkt->data.raw.buf) {
204       priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
205       priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
206     }
207   }
208 
209   return pkt;
210 }
211 
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)212 aom_codec_err_t aom_codec_set_cx_data_buf(aom_codec_ctx_t *ctx,
213                                           const aom_fixed_buf_t *buf,
214                                           unsigned int pad_before,
215                                           unsigned int pad_after) {
216   if (!ctx || !ctx->priv) return AOM_CODEC_INVALID_PARAM;
217 
218   if (buf) {
219     ctx->priv->enc.cx_data_dst_buf = *buf;
220     ctx->priv->enc.cx_data_pad_before = pad_before;
221     ctx->priv->enc.cx_data_pad_after = pad_after;
222   } else {
223     ctx->priv->enc.cx_data_dst_buf.buf = NULL;
224     ctx->priv->enc.cx_data_dst_buf.sz = 0;
225     ctx->priv->enc.cx_data_pad_before = 0;
226     ctx->priv->enc.cx_data_pad_after = 0;
227   }
228 
229   return AOM_CODEC_OK;
230 }
231 
aom_codec_get_preview_frame(aom_codec_ctx_t * ctx)232 const aom_image_t *aom_codec_get_preview_frame(aom_codec_ctx_t *ctx) {
233   aom_image_t *img = NULL;
234 
235   if (ctx) {
236     if (!ctx->iface || !ctx->priv)
237       ctx->err = AOM_CODEC_ERROR;
238     else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
239       ctx->err = AOM_CODEC_INCAPABLE;
240     else if (!ctx->iface->enc.get_preview)
241       ctx->err = AOM_CODEC_INCAPABLE;
242     else
243       img = ctx->iface->enc.get_preview(get_alg_priv(ctx));
244   }
245 
246   return img;
247 }
248 
aom_codec_get_global_headers(aom_codec_ctx_t * ctx)249 aom_fixed_buf_t *aom_codec_get_global_headers(aom_codec_ctx_t *ctx) {
250   aom_fixed_buf_t *buf = NULL;
251 
252   if (ctx) {
253     if (!ctx->iface || !ctx->priv)
254       ctx->err = AOM_CODEC_ERROR;
255     else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
256       ctx->err = AOM_CODEC_INCAPABLE;
257     else if (!ctx->iface->enc.get_glob_hdrs)
258       ctx->err = AOM_CODEC_INCAPABLE;
259     else
260       buf = ctx->iface->enc.get_glob_hdrs(get_alg_priv(ctx));
261   }
262 
263   return buf;
264 }
265 
aom_codec_enc_config_set(aom_codec_ctx_t * ctx,const aom_codec_enc_cfg_t * cfg)266 aom_codec_err_t aom_codec_enc_config_set(aom_codec_ctx_t *ctx,
267                                          const aom_codec_enc_cfg_t *cfg) {
268   aom_codec_err_t res;
269 
270   if (!ctx || !ctx->iface || !ctx->priv || !cfg)
271     res = AOM_CODEC_INVALID_PARAM;
272   else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
273     res = AOM_CODEC_INCAPABLE;
274   else
275     res = ctx->iface->enc.cfg_set(get_alg_priv(ctx), cfg);
276 
277   return SAVE_STATUS(ctx, res);
278 }
279 
aom_codec_pkt_list_add(struct aom_codec_pkt_list * list,const struct aom_codec_cx_pkt * pkt)280 int aom_codec_pkt_list_add(struct aom_codec_pkt_list *list,
281                            const struct aom_codec_cx_pkt *pkt) {
282   if (list->cnt < list->max) {
283     list->pkts[list->cnt++] = *pkt;
284     return 0;
285   }
286 
287   return 1;
288 }
289 
aom_codec_pkt_list_get(struct aom_codec_pkt_list * list,aom_codec_iter_t * iter)290 const aom_codec_cx_pkt_t *aom_codec_pkt_list_get(
291     struct aom_codec_pkt_list *list, aom_codec_iter_t *iter) {
292   const aom_codec_cx_pkt_t *pkt;
293 
294   if (!(*iter)) {
295     *iter = list->pkts;
296   }
297 
298   pkt = (const aom_codec_cx_pkt_t *)*iter;
299 
300   if ((size_t)(pkt - list->pkts) < list->cnt)
301     *iter = pkt + 1;
302   else
303     pkt = NULL;
304 
305   return pkt;
306 }
307