]> git.sesse.net Git - ffmpeg/commitdiff
avcodec/mpegaudio_tablegen: Make exponential LUT shared
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Wed, 18 Nov 2020 12:13:45 +0000 (13:13 +0100)
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Tue, 8 Dec 2020 16:51:47 +0000 (17:51 +0100)
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@gmail.com>
libavcodec/Makefile
libavcodec/mpegaudio_tablegen.c
libavcodec/mpegaudio_tablegen.h
libavcodec/mpegaudiodata.h
libavcodec/mpegaudiodec_common.c
libavcodec/mpegaudiodec_common_tablegen.c [new file with mode: 0644]
libavcodec/mpegaudiodec_common_tablegen.h [new file with mode: 0644]
libavcodec/mpegaudiodec_template.c

index eed12f09ac7ba0f50a08c2650ee5a0980e520b59..9b370ffc449f9caa14632bce8aa98b3d8ecc06cf 100644 (file)
@@ -1241,6 +1241,7 @@ HOSTPROGS = aacps_tablegen                                              \
             dv_tablegen                                                 \
             motionpixels_tablegen                                       \
             mpegaudio_tablegen                                          \
+            mpegaudiodec_common_tablegen                                \
             pcm_tablegen                                                \
             qdm2_tablegen                                               \
             sinewin_tablegen                                            \
@@ -1265,7 +1266,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))
 
@@ -1280,6 +1282,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
index ec0d51c67d596f27b174f2c9f54be8a26e9370d9..7c598f7774b0bc0b582ec7a7e885914f6fc56582 100644 (file)
@@ -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);
index d9e32b54da7bad9a4ec917e3e1e49362a95edda8..bae6962ac099edeec91f1d1df86fcd46ea80f46e 100644 (file)
 #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(BUILD_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;
index 01b1f88cd00a6e3b56f6f6a6e8fe36b4dec1e053..0a425ef6a83e11688655bc23fc369a0ae75ebe56 100644 (file)
@@ -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];
index 4333746e9a6508a476347426a28079a225774e94..a963f6683a76499dba64940ff6683a1dab6c5761 100644 (file)
@@ -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 (file)
index 0000000..a319df6
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Generate a header file for hardcoded shared mpegaudiodec tables
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
+ * Copyright (c) 2020 Andreas Rheinhardt <andreas.rheinhardt@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 (file)
index 0000000..bf402c9
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Header file for hardcoded shared mpegaudiodec tables
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
+ * Copyright (c) 2020 Andreas Rheinhardt <andreas.rheinhardt@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 */
index 1e8fd6064f2d8374625dedf8b4c8fc198f4a057a..0ceeee2d46f239aa476ea8ef1bdebef18f7683b6 100644 (file)
@@ -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)