Commit dfed7302f6 for ffmpeg
commit dfed7302f6aea089dee9e0c37d7953599dad77c1
Author: Nathan Moinvaziri <nathan@solidstatenetworks.com>
Date: Sun Oct 18 12:45:18 2026 -0700
avcodec/aaccoder: look up the reconstruction of escape values
Pricing an escape value called cbrtf() from libm for every coefficient,
which cost a quarter of the encode time with the default coder. A table
of c * cbrtf(c) over the 8192 escape magnitudes, built once, keeps the
output identical and makes encoding about 30% faster.
diff --git a/libavcodec/aaccoder.c b/libavcodec/aaccoder.c
index 4f30a57e5c..bb2e7f9f9e 100644
--- a/libavcodec/aaccoder.c
+++ b/libavcodec/aaccoder.c
@@ -61,6 +61,15 @@
#include "libavcodec/aaccoder_trellis.h"
#include "libavcodec/aaccoder_nmr.h"
+/* c * cbrtf(c), the reconstruction of every escape magnitude c */
+static float esc_pow43[1 << 13];
+
+av_cold void ff_aac_coder_init_tables(void)
+{
+ for (int c = 0; c < FF_ARRAY_ELEMS(esc_pow43); c++)
+ esc_pow43[c] = c * cbrtf(c);
+}
+
typedef float (*quantize_and_encode_band_func)(struct AACEncContext *s, PutBitContext *pb,
const float *in, float *quant, const float *scaled,
int size, int scale_idx, int cb,
@@ -138,7 +147,7 @@ static av_always_inline float quantize_and_encode_band_cost_template(
curbits += 21;
} else {
int c = av_clip_uintp2(quant(t, Q, ROUNDING), 13);
- quantized = c*cbrtf(c)*IQ;
+ quantized = esc_pow43[c]*IQ;
curbits += av_log2(c)*2 - 4 + 1;
}
} else {
diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index d4b4a1e4f9..5b1e3eff0f 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -36,6 +36,7 @@
#include "libavutil/float_dsp.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
+#include "libavutil/thread.h"
#include "avcodec.h"
#include "codec_internal.h"
#include "encode.h"
@@ -1822,6 +1823,7 @@ static av_cold int check_height_ext(AVCodecContext *avctx, AACEncContext *s)
static av_cold int aac_encode_init(AVCodecContext *avctx)
{
+ static AVOnce init_static_once = AV_ONCE_INIT;
AACEncContext *s = avctx->priv_data;
int i, ret = 0;
int chcfg;
@@ -1980,6 +1982,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
// Initialize static tables
ff_aac_float_common_init();
+ ff_thread_once(&init_static_once, ff_aac_coder_init_tables);
if ((ret = dsp_init(avctx, s)) < 0)
return ret;
diff --git a/libavcodec/aacenc.h b/libavcodec/aacenc.h
index 057c2a59d4..a8f69e7051 100644
--- a/libavcodec/aacenc.h
+++ b/libavcodec/aacenc.h
@@ -160,6 +160,8 @@ typedef struct AACCoefficientsEncoder {
extern const AACCoefficientsEncoder ff_aac_coders[];
+void ff_aac_coder_init_tables(void);
+
typedef struct AACQuantizeBandCostCacheEntry {
float rd;
float energy;