[FFmpeg-devel] [PATCH v2 162/162] avcodec/mpegaudio_tablegen: Make exponential LUT shared

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Fri Nov 20 09:33:27 EET 2020


Both the fixed as well as the floating point mpegaudio decoders use
LUTs of type int8_t and uint32_t with 32K entries each; these tables
are completely the same, yet they are not shared. This commit makes
them shared. When both fixed as well as floating point decoders are
enabled, this saves 160KiB from the bss segment for a normal build
(translating into 160KiB less memory usage if both a shared as well as
a floating point decoder have actually been used) and 160KiB from the
binary for a build with hardcoded tables.

It also means that the code to create said LUTs is no longer duplicated
(for a normal build).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/Makefile                       |  5 +-
 libavcodec/mpegaudio_tablegen.c           |  2 -
 libavcodec/mpegaudio_tablegen.h           | 22 +------
 libavcodec/mpegaudiodata.h                | 11 ++++
 libavcodec/mpegaudiodec_common.c          |  3 +
 libavcodec/mpegaudiodec_common_tablegen.c | 40 +++++++++++++
 libavcodec/mpegaudiodec_common_tablegen.h | 72 +++++++++++++++++++++++
 libavcodec/mpegaudiodec_template.c        |  4 +-
 8 files changed, 133 insertions(+), 26 deletions(-)
 create mode 100644 libavcodec/mpegaudiodec_common_tablegen.c
 create mode 100644 libavcodec/mpegaudiodec_common_tablegen.h

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index a851a0a3b2..4f6cc22034 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1238,6 +1238,7 @@ HOSTPROGS = aacps_tablegen                                              \
             dv_tablegen                                                 \
             motionpixels_tablegen                                       \
             mpegaudio_tablegen                                          \
+            mpegaudiodec_common_tablegen                                \
             pcm_tablegen                                                \
             qdm2_tablegen                                               \
             sinewin_tablegen                                            \
@@ -1262,7 +1263,8 @@ endif
 
 GEN_HEADERS = cbrt_tables.h cbrt_fixed_tables.h aacps_tables.h aacps_fixed_tables.h \
               dv_tables.h     \
-              sinewin_tables.h sinewin_fixed_tables.h mpegaudio_tables.h motionpixels_tables.h \
+              sinewin_tables.h sinewin_fixed_tables.h mpegaudio_tables.h \
+              mpegaudiodec_common_tables.h motionpixels_tables.h \
               pcm_tables.h qdm2_tables.h
 GEN_HEADERS := $(addprefix $(SUBDIR), $(GEN_HEADERS))
 
@@ -1277,6 +1279,7 @@ $(SUBDIR)aacps_fixed.o: $(SUBDIR)aacps_fixed_tables.h
 $(SUBDIR)aactab_fixed.o: $(SUBDIR)aac_fixed_tables.h
 $(SUBDIR)dvenc.o: $(SUBDIR)dv_tables.h
 $(SUBDIR)motionpixels.o: $(SUBDIR)motionpixels_tables.h
+$(SUBDIR)mpegaudiodec_common.o: $(SUBDIR)mpegaudiodec_common_tables.h
 $(SUBDIR)mpegaudiodec_fixed.o: $(SUBDIR)mpegaudio_tables.h
 $(SUBDIR)mpegaudiodec_float.o: $(SUBDIR)mpegaudio_tables.h
 $(SUBDIR)pcm.o: $(SUBDIR)pcm_tables.h
diff --git a/libavcodec/mpegaudio_tablegen.c b/libavcodec/mpegaudio_tablegen.c
index bff0f031a1..9721ae44e5 100644
--- a/libavcodec/mpegaudio_tablegen.c
+++ b/libavcodec/mpegaudio_tablegen.c
@@ -33,8 +33,6 @@ int main(void)
 
     write_fileheader();
 
-    WRITE_ARRAY("static const", int8_t, table_4_3_exp);
-    WRITE_ARRAY("static const", uint32_t, table_4_3_value);
     WRITE_ARRAY("static const", uint32_t, exp_table_fixed);
     WRITE_ARRAY("static const", float, exp_table_float);
     WRITE_2D_ARRAY("static const", uint32_t, expval_table_fixed);
diff --git a/libavcodec/mpegaudio_tablegen.h b/libavcodec/mpegaudio_tablegen.h
index 1a92bb4de6..ed86257767 100644
--- a/libavcodec/mpegaudio_tablegen.h
+++ b/libavcodec/mpegaudio_tablegen.h
@@ -27,14 +27,10 @@
 #include <math.h>
 #include "libavutil/attributes.h"
 
-#define TABLE_4_3_SIZE (8191 + 16)*4
 #if CONFIG_HARDCODED_TABLES
 #define mpegaudio_tableinit()
 #include "libavcodec/mpegaudio_tables.h"
 #else
-static int8_t   table_4_3_exp[TABLE_4_3_SIZE];
-static uint32_t table_4_3_value[TABLE_4_3_SIZE];
-
 #if defined(BOTH_TABLES) || !USE_FLOATS
 #define FIXED_TABLE
 static uint32_t exp_table_fixed[512];
@@ -47,7 +43,6 @@ static float exp_table_float[512];
 static float expval_table_float[512][16];
 #endif
 
-#define FRAC_BITS 23
 #define IMDCT_SCALAR 1.759
 
 static av_cold void mpegaudio_tableinit(void)
@@ -62,25 +57,10 @@ static av_cold void mpegaudio_tableinit(void)
     double pow43_lut[16];
     double exp2_base = 2.11758236813575084767080625169910490512847900390625e-22; // 2^(-72)
     double exp2_val;
-    double pow43_val = 0;
+
     for (i = 0; i < 16; ++i)
         pow43_lut[i] = i * cbrt(i);
 
-    for (i = 1; i < TABLE_4_3_SIZE; i++) {
-        double f, fm;
-        int e, m;
-        double value = i / 4;
-        if ((i & 3) == 0)
-            pow43_val = value / IMDCT_SCALAR * cbrt(value);
-        f  = pow43_val * exp2_lut[i & 3];
-        fm = frexp(f, &e);
-        m  = llrint(fm * (1LL << 31));
-        e += FRAC_BITS - 31 + 5 - 100;
-
-        /* normalized to FRAC_BITS */
-        table_4_3_value[i] =  m;
-        table_4_3_exp[i]   = -e;
-    }
     for (exponent = 0; exponent < 512; exponent++) {
         if (exponent && (exponent & 3) == 0)
             exp2_base *= 2;
diff --git a/libavcodec/mpegaudiodata.h b/libavcodec/mpegaudiodata.h
index 01b1f88cd0..0a425ef6a8 100644
--- a/libavcodec/mpegaudiodata.h
+++ b/libavcodec/mpegaudiodata.h
@@ -29,6 +29,8 @@
 
 #include <stdint.h>
 
+#include "config.h"
+
 #include "internal.h"
 #include "vlc.h"
 
@@ -42,6 +44,15 @@ extern const int ff_mpa_quant_steps[17];
 extern const int ff_mpa_quant_bits[17];
 extern const unsigned char * const ff_mpa_alloc_tables[5];
 
+#define TABLE_4_3_SIZE ((8191 + 16)*4)
+#if CONFIG_HARDCODED_TABLES
+extern const int8_t   ff_table_4_3_exp  [TABLE_4_3_SIZE];
+extern const uint32_t ff_table_4_3_value[TABLE_4_3_SIZE];
+#else
+extern int8_t   ff_table_4_3_exp  [TABLE_4_3_SIZE];
+extern uint32_t ff_table_4_3_value[TABLE_4_3_SIZE];
+#endif
+
 /* VLCs for decoding layer 3 huffman tables */
 extern VLC ff_huff_vlc[16];
 extern VLC ff_huff_quad_vlc[2];
diff --git a/libavcodec/mpegaudiodec_common.c b/libavcodec/mpegaudiodec_common.c
index 4333746e9a..a963f6683a 100644
--- a/libavcodec/mpegaudiodec_common.c
+++ b/libavcodec/mpegaudiodec_common.c
@@ -32,6 +32,8 @@
 
 #include "mpegaudiodata.h"
 
+#include "mpegaudiodec_common_tablegen.h"
+
 uint16_t ff_scale_factor_modshift[64];
 
 static int16_t division_tab3[1 << 6 ];
@@ -469,6 +471,7 @@ static av_cold void mpegaudiodec_common_init_static(void)
             }
         }
     }
+    mpegaudiodec_common_tableinit();
 }
 
 av_cold void ff_mpegaudiodec_common_init_static(void)
diff --git a/libavcodec/mpegaudiodec_common_tablegen.c b/libavcodec/mpegaudiodec_common_tablegen.c
new file mode 100644
index 0000000000..a319df65ab
--- /dev/null
+++ b/libavcodec/mpegaudiodec_common_tablegen.c
@@ -0,0 +1,40 @@
+/*
+ * Generate a header file for hardcoded shared mpegaudiodec tables
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger at gmx.de>
+ * Copyright (c) 2020 Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdlib.h>
+#define CONFIG_HARDCODED_TABLES 0
+#include "libavutil/tablegen.h"
+#include "mpegaudiodec_common_tablegen.h"
+#include "tableprint.h"
+
+int main(void)
+{
+    mpegaudiodec_common_tableinit();
+
+    write_fileheader();
+
+    WRITE_ARRAY("const", int8_t, ff_table_4_3_exp);
+    WRITE_ARRAY("const", uint32_t, ff_table_4_3_value);
+
+    return 0;
+}
diff --git a/libavcodec/mpegaudiodec_common_tablegen.h b/libavcodec/mpegaudiodec_common_tablegen.h
new file mode 100644
index 0000000000..bf402c9d84
--- /dev/null
+++ b/libavcodec/mpegaudiodec_common_tablegen.h
@@ -0,0 +1,72 @@
+/*
+ * Header file for hardcoded shared mpegaudiodec tables
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger at gmx.de>
+ * Copyright (c) 2020 Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_MPEGAUDIODEC_COMMON_TABLEGEN_H
+#define AVCODEC_MPEGAUDIODEC_COMMON_TABLEGEN_H
+
+#include <stdint.h>
+
+#define TABLE_4_3_SIZE ((8191 + 16)*4)
+
+#if CONFIG_HARDCODED_TABLES
+#define mpegaudiodec_common_tableinit()
+#include "libavcodec/mpegaudiodec_common_tables.h"
+#else
+#include <math.h>
+#include "libavutil/attributes.h"
+
+int8_t   ff_table_4_3_exp  [TABLE_4_3_SIZE];
+uint32_t ff_table_4_3_value[TABLE_4_3_SIZE];
+
+#define FRAC_BITS 23
+#define IMDCT_SCALAR 1.759
+
+static av_cold void mpegaudiodec_common_tableinit(void)
+{
+    static const double exp2_lut[4] = {
+        1.00000000000000000000, /* 2 ^ (0 * 0.25) */
+        1.18920711500272106672, /* 2 ^ (1 * 0.25) */
+        M_SQRT2               , /* 2 ^ (2 * 0.25) */
+        1.68179283050742908606, /* 2 ^ (3 * 0.25) */
+    };
+    double pow43_val = 0;
+
+    for (int i = 1; i < TABLE_4_3_SIZE; i++) {
+        double f, fm;
+        int e, m;
+        double value = i / 4;
+        if ((i & 3) == 0)
+            pow43_val = value / IMDCT_SCALAR * cbrt(value);
+        f  = pow43_val * exp2_lut[i & 3];
+        fm = frexp(f, &e);
+        m  = llrint(fm * (1LL << 31));
+        e += FRAC_BITS - 31 + 5 - 100;
+
+        /* normalized to FRAC_BITS */
+        ff_table_4_3_value[i] =  m;
+        ff_table_4_3_exp  [i] = -e;
+    }
+}
+
+#endif /* CONFIG_HARDCODED_TABLES */
+#endif /* AVCODEC_MPEGAUDIODEC_COMMON_TABLEGEN_H */
diff --git a/libavcodec/mpegaudiodec_template.c b/libavcodec/mpegaudiodec_template.c
index 1e8fd6064f..0ceeee2d46 100644
--- a/libavcodec/mpegaudiodec_template.c
+++ b/libavcodec/mpegaudiodec_template.c
@@ -219,8 +219,8 @@ static inline int l3_unscale(int value, int exponent)
     unsigned int m;
     int e;
 
-    e  = table_4_3_exp  [4 * value + (exponent & 3)];
-    m  = table_4_3_value[4 * value + (exponent & 3)];
+    e  = ff_table_4_3_exp  [4 * value + (exponent & 3)];
+    m  = ff_table_4_3_value[4 * value + (exponent & 3)];
     e -= exponent >> 2;
 #ifdef DEBUG
     if(e < 1)
-- 
2.25.1



More information about the ffmpeg-devel mailing list