blob: 70e0b75bcd9c3555e194b41bb6e53ec482e73e4b [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12/*!\file
13 * \brief Provides the high level interface to wrap encoder algorithms.
14 *
15 */
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
James Zern66ee4402017-01-06 17:59:50 -080017
18#if HAVE_FEXCEPT
Tom Anderson4f1fd962018-07-25 16:51:53 -070019#ifndef _GNU_SOURCE
James Zern66ee4402017-01-06 17:59:50 -080020#define _GNU_SOURCE
Tom Anderson4f1fd962018-07-25 16:51:53 -070021#endif
James Zern66ee4402017-01-06 17:59:50 -080022#include <fenv.h>
23#endif
24
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include <limits.h>
26#include <string.h>
Ryan44276df2019-10-29 17:11:45 -070027
28#include "aom/aom_encoder.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070029#include "aom/internal/aom_codec_internal.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070030
31#define SAVE_STATUS(ctx, var) (ctx ? (ctx->err = var) : var)
32
Yaowu Xuf883b422016-08-30 14:01:10 -070033static aom_codec_alg_priv_t *get_alg_priv(aom_codec_ctx_t *ctx) {
34 return (aom_codec_alg_priv_t *)ctx->priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -070035}
36
Yaowu Xuf883b422016-08-30 14:01:10 -070037aom_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;
Wan-Teh Chang2b142a12022-06-02 14:32:38 -070042 // The value of AOM_ENCODER_ABI_VERSION in libaom v3.0.0 and v3.1.0 - v3.1.3.
43 //
44 // We are compatible with these older libaom releases. AOM_ENCODER_ABI_VERSION
45 // was incremented after these releases for two reasons:
46 // 1. AOM_ENCODER_ABI_VERSION takes contribution from
47 // AOM_EXT_PART_ABI_VERSION. The external partition API is still
48 // experimental, so it should not be considered as part of the stable ABI.
49 // fd9ed8366 External partition: Define APIs
50 // https://aomedia-review.googlesource.com/c/aom/+/135663
51 // 2. As a way to detect the presence of speeds 7-9 in all-intra mode. I (wtc)
52 // suggested this change because I misunderstood how
53 // AOM_ENCODER_ABI_VERSION was used.
54 // bbdfa68d1 AllIntra: Redefine all-intra mode speed features for speed 7+
55 // https://aomedia-review.googlesource.com/c/aom/+/140624
56 const int aom_encoder_abi_version_25 = 25;
Yaowu Xuc27fc142016-08-22 16:08:15 -070057
Wan-Teh Chang2b142a12022-06-02 14:32:38 -070058 // TODO(bug aomedia:3228): Remove the check for aom_encoder_abi_version_25 in
59 // libaom v4.0.0.
60 if (ver != AOM_ENCODER_ABI_VERSION && ver != aom_encoder_abi_version_25)
Yaowu Xuf883b422016-08-30 14:01:10 -070061 res = AOM_CODEC_ABI_MISMATCH;
Yaowu Xuc27fc142016-08-22 16:08:15 -070062 else if (!ctx || !iface || !cfg)
Yaowu Xuf883b422016-08-30 14:01:10 -070063 res = AOM_CODEC_INVALID_PARAM;
64 else if (iface->abi_version != AOM_CODEC_INTERNAL_ABI_VERSION)
65 res = AOM_CODEC_ABI_MISMATCH;
66 else if (!(iface->caps & AOM_CODEC_CAP_ENCODER))
67 res = AOM_CODEC_INCAPABLE;
68 else if ((flags & AOM_CODEC_USE_PSNR) && !(iface->caps & AOM_CODEC_CAP_PSNR))
69 res = AOM_CODEC_INCAPABLE;
Wan-Teh Chang96983fe2023-09-22 16:20:01 -070070 else if ((flags & AOM_CODEC_USE_HIGHBITDEPTH) &&
71 !(iface->caps & AOM_CODEC_CAP_HIGHBITDEPTH)) {
72 res = AOM_CODEC_INCAPABLE;
73 } else if (cfg->g_bit_depth > 8 &&
74 (flags & AOM_CODEC_USE_HIGHBITDEPTH) == 0) {
Bohan Lic255bee2021-04-12 10:12:29 -070075 res = AOM_CODEC_INVALID_PARAM;
76 ctx->err_detail =
77 "High bit-depth used without the AOM_CODEC_USE_HIGHBITDEPTH flag.";
78 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -070079 ctx->iface = iface;
80 ctx->name = iface->name;
81 ctx->priv = NULL;
82 ctx->init_flags = flags;
83 ctx->config.enc = cfg;
Elliott Karpilovsky31f92612020-04-26 20:57:37 -070084 res = ctx->iface->init(ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -070085
86 if (res) {
Wan-Teh Changac286102023-03-23 14:50:10 -070087 // IMPORTANT: ctx->priv->err_detail must be null or point to a string
88 // that remains valid after ctx->priv is destroyed, such as a C string
89 // literal. This makes it safe to call aom_codec_error_detail() after
90 // aom_codec_enc_init_ver() failed.
Yaowu Xuc27fc142016-08-22 16:08:15 -070091 ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -070092 aom_codec_destroy(ctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -070093 }
94 }
95
96 return SAVE_STATUS(ctx, res);
97}
98
Yaowu Xuf883b422016-08-30 14:01:10 -070099aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface,
100 aom_codec_enc_cfg_t *cfg,
Wan-Teh Chang7787d5c2020-03-28 17:11:57 -0700101 unsigned int usage) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700102 aom_codec_err_t res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103
Wan-Teh Chang7787d5c2020-03-28 17:11:57 -0700104 if (!iface || !cfg)
Yaowu Xuf883b422016-08-30 14:01:10 -0700105 res = AOM_CODEC_INVALID_PARAM;
106 else if (!(iface->caps & AOM_CODEC_CAP_ENCODER))
107 res = AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108 else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700109 res = AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700110
Wan-Teh Changca0a2252023-04-01 07:34:01 -0700111 for (int i = 0; i < iface->enc.cfg_count; ++i) {
Wan-Teh Chang7787d5c2020-03-28 17:11:57 -0700112 if (iface->enc.cfgs[i].g_usage == usage) {
113 *cfg = iface->enc.cfgs[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700114 res = AOM_CODEC_OK;
Wan-Teh Changca0a2252023-04-01 07:34:01 -0700115 /* default values */
116 memset(&cfg->encoder_cfg, 0, sizeof(cfg->encoder_cfg));
117 cfg->encoder_cfg.super_block_size = 0; // Dynamic
118 cfg->encoder_cfg.max_partition_size = 128;
119 cfg->encoder_cfg.min_partition_size = 4;
120 cfg->encoder_cfg.disable_trellis_quant = 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121 break;
122 }
123 }
124 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 return res;
126}
127
James Zern1fbb25c2023-05-22 10:45:24 -0700128#if AOM_ARCH_X86 || AOM_ARCH_X86_64
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129/* On X86, disable the x87 unit's internal 80 bit precision for better
130 * consistency with the SSE unit's 64 bit precision.
131 */
132#include "aom_ports/x86.h"
James Zern66ee4402017-01-06 17:59:50 -0800133#define FLOATING_POINT_SET_PRECISION \
134 unsigned short x87_orig_mode = x87_set_double_precision();
135#define FLOATING_POINT_RESTORE_PRECISION x87_set_control_word(x87_orig_mode);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700136#else
James Zern66ee4402017-01-06 17:59:50 -0800137#define FLOATING_POINT_SET_PRECISION
138#define FLOATING_POINT_RESTORE_PRECISION
James Zern1fbb25c2023-05-22 10:45:24 -0700139#endif // AOM_ARCH_X86 || AOM_ARCH_X86_64
James Zern66ee4402017-01-06 17:59:50 -0800140
141#if HAVE_FEXCEPT && CONFIG_DEBUG
142#define FLOATING_POINT_SET_EXCEPTIONS \
Yaowu Xu85d00fa2019-05-08 16:03:45 -0700143 const int float_excepts = \
144 feenableexcept(FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW);
Urvang Joshi016454d2019-09-30 11:50:28 -0700145#define FLOATING_POINT_RESTORE_EXCEPTIONS \
Wan-Teh Chang11360fc2023-03-07 11:39:52 -0800146 if (float_excepts != -1) { \
147 fedisableexcept(FE_ALL_EXCEPT); \
148 feenableexcept(float_excepts); \
149 }
James Zern66ee4402017-01-06 17:59:50 -0800150#else
151#define FLOATING_POINT_SET_EXCEPTIONS
152#define FLOATING_POINT_RESTORE_EXCEPTIONS
153#endif // HAVE_FEXCEPT && CONFIG_DEBUG
154
Johann3c30fb42018-02-08 14:33:20 -0800155/* clang-format off */
James Zern66ee4402017-01-06 17:59:50 -0800156#define FLOATING_POINT_INIT \
Johann3c30fb42018-02-08 14:33:20 -0800157 do { \
James Zern66ee4402017-01-06 17:59:50 -0800158 FLOATING_POINT_SET_PRECISION \
159 FLOATING_POINT_SET_EXCEPTIONS
160
161#define FLOATING_POINT_RESTORE \
162 FLOATING_POINT_RESTORE_EXCEPTIONS \
163 FLOATING_POINT_RESTORE_PRECISION \
Johann3c30fb42018-02-08 14:33:20 -0800164 } while (0);
165/* clang-format on */
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166
Yaowu Xuf883b422016-08-30 14:01:10 -0700167aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img,
168 aom_codec_pts_t pts, unsigned long duration,
Sean DuBois47cc2552018-01-23 07:44:16 +0000169 aom_enc_frame_flags_t flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700170 aom_codec_err_t res = AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171
172 if (!ctx || (img && !duration))
Yaowu Xuf883b422016-08-30 14:01:10 -0700173 res = AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174 else if (!ctx->iface || !ctx->priv)
Yaowu Xuf883b422016-08-30 14:01:10 -0700175 res = AOM_CODEC_ERROR;
176 else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
177 res = AOM_CODEC_INCAPABLE;
Wan-Teh Chang96983fe2023-09-22 16:20:01 -0700178 else if (img && ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) != 0) !=
179 ((ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) != 0)) {
180 res = AOM_CODEC_INVALID_PARAM;
181 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 /* Execute in a normalized floating point environment, if the platform
183 * requires it.
184 */
James Zern66ee4402017-01-06 17:59:50 -0800185 FLOATING_POINT_INIT
Elliott Karpilovsky31f92612020-04-26 20:57:37 -0700186 res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration, flags);
James Zern66ee4402017-01-06 17:59:50 -0800187 FLOATING_POINT_RESTORE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 }
189
190 return SAVE_STATUS(ctx, res);
191}
192
Yaowu Xuf883b422016-08-30 14:01:10 -0700193const aom_codec_cx_pkt_t *aom_codec_get_cx_data(aom_codec_ctx_t *ctx,
194 aom_codec_iter_t *iter) {
195 const aom_codec_cx_pkt_t *pkt = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196
197 if (ctx) {
198 if (!iter)
Yaowu Xuf883b422016-08-30 14:01:10 -0700199 ctx->err = AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700200 else if (!ctx->iface || !ctx->priv)
Yaowu Xuf883b422016-08-30 14:01:10 -0700201 ctx->err = AOM_CODEC_ERROR;
202 else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
203 ctx->err = AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204 else
205 pkt = ctx->iface->enc.get_cx_data(get_alg_priv(ctx), iter);
206 }
207
Yaowu Xuf883b422016-08-30 14:01:10 -0700208 if (pkt && pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209 // If the application has specified a destination area for the
210 // compressed data, and the codec has not placed the data there,
211 // and it fits, copy it.
Yaowu Xuf883b422016-08-30 14:01:10 -0700212 aom_codec_priv_t *const priv = ctx->priv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213 char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf;
214
215 if (dst_buf && pkt->data.raw.buf != dst_buf &&
216 pkt->data.raw.sz + priv->enc.cx_data_pad_before +
217 priv->enc.cx_data_pad_after <=
218 priv->enc.cx_data_dst_buf.sz) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700219 aom_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700220
221 memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf,
222 pkt->data.raw.sz);
223 *modified_pkt = *pkt;
224 modified_pkt->data.raw.buf = dst_buf;
225 modified_pkt->data.raw.sz +=
226 priv->enc.cx_data_pad_before + priv->enc.cx_data_pad_after;
227 pkt = modified_pkt;
228 }
229
230 if (dst_buf == pkt->data.raw.buf) {
231 priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
232 priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
233 }
234 }
235
236 return pkt;
237}
238
Yaowu Xuf883b422016-08-30 14:01:10 -0700239aom_codec_err_t aom_codec_set_cx_data_buf(aom_codec_ctx_t *ctx,
240 const aom_fixed_buf_t *buf,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700241 unsigned int pad_before,
242 unsigned int pad_after) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700243 if (!ctx || !ctx->priv) return AOM_CODEC_INVALID_PARAM;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700244
245 if (buf) {
246 ctx->priv->enc.cx_data_dst_buf = *buf;
247 ctx->priv->enc.cx_data_pad_before = pad_before;
248 ctx->priv->enc.cx_data_pad_after = pad_after;
249 } else {
250 ctx->priv->enc.cx_data_dst_buf.buf = NULL;
251 ctx->priv->enc.cx_data_dst_buf.sz = 0;
252 ctx->priv->enc.cx_data_pad_before = 0;
253 ctx->priv->enc.cx_data_pad_after = 0;
254 }
255
Yaowu Xuf883b422016-08-30 14:01:10 -0700256 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257}
258
Yaowu Xuf883b422016-08-30 14:01:10 -0700259const aom_image_t *aom_codec_get_preview_frame(aom_codec_ctx_t *ctx) {
260 aom_image_t *img = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261
262 if (ctx) {
263 if (!ctx->iface || !ctx->priv)
Yaowu Xuf883b422016-08-30 14:01:10 -0700264 ctx->err = AOM_CODEC_ERROR;
265 else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
266 ctx->err = AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700267 else if (!ctx->iface->enc.get_preview)
Yaowu Xuf883b422016-08-30 14:01:10 -0700268 ctx->err = AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 else
270 img = ctx->iface->enc.get_preview(get_alg_priv(ctx));
271 }
272
273 return img;
274}
275
Yaowu Xuf883b422016-08-30 14:01:10 -0700276aom_fixed_buf_t *aom_codec_get_global_headers(aom_codec_ctx_t *ctx) {
277 aom_fixed_buf_t *buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278
279 if (ctx) {
280 if (!ctx->iface || !ctx->priv)
Yaowu Xuf883b422016-08-30 14:01:10 -0700281 ctx->err = AOM_CODEC_ERROR;
282 else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
283 ctx->err = AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700284 else if (!ctx->iface->enc.get_glob_hdrs)
Yaowu Xuf883b422016-08-30 14:01:10 -0700285 ctx->err = AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 else
287 buf = ctx->iface->enc.get_glob_hdrs(get_alg_priv(ctx));
288 }
289
290 return buf;
291}
292
Yaowu Xuf883b422016-08-30 14:01:10 -0700293aom_codec_err_t aom_codec_enc_config_set(aom_codec_ctx_t *ctx,
294 const aom_codec_enc_cfg_t *cfg) {
295 aom_codec_err_t res;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700296
297 if (!ctx || !ctx->iface || !ctx->priv || !cfg)
Yaowu Xuf883b422016-08-30 14:01:10 -0700298 res = AOM_CODEC_INVALID_PARAM;
299 else if (!(ctx->iface->caps & AOM_CODEC_CAP_ENCODER))
300 res = AOM_CODEC_INCAPABLE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700301 else
302 res = ctx->iface->enc.cfg_set(get_alg_priv(ctx), cfg);
303
304 return SAVE_STATUS(ctx, res);
305}
306
Yaowu Xuf883b422016-08-30 14:01:10 -0700307int aom_codec_pkt_list_add(struct aom_codec_pkt_list *list,
308 const struct aom_codec_cx_pkt *pkt) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309 if (list->cnt < list->max) {
310 list->pkts[list->cnt++] = *pkt;
311 return 0;
312 }
313
314 return 1;
315}
316
Yaowu Xuf883b422016-08-30 14:01:10 -0700317const aom_codec_cx_pkt_t *aom_codec_pkt_list_get(
318 struct aom_codec_pkt_list *list, aom_codec_iter_t *iter) {
319 const aom_codec_cx_pkt_t *pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320
321 if (!(*iter)) {
322 *iter = list->pkts;
323 }
324
Yaowu Xuf883b422016-08-30 14:01:10 -0700325 pkt = (const aom_codec_cx_pkt_t *)*iter;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326
327 if ((size_t)(pkt - list->pkts) < list->cnt)
328 *iter = pkt + 1;
329 else
330 pkt = NULL;
331
332 return pkt;
333}