changeset 10469:23ca6d60184d libavcodec

Add support for hardcoding the mpegaudiodec tables. Reduces .bss size by about 194 kB.
author reimar
date Wed, 28 Oct 2009 18:42:52 +0000
parents fedf07e14bee
children 08be6978ac8e
files Makefile mpegaudio_tablegen.c mpegaudio_tablegen.h mpegaudiodec.c tableprint.c tableprint.h
diffstat 6 files changed, 273 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Oct 28 14:40:05 2009 +0000
+++ b/Makefile	Wed Oct 28 18:42:52 2009 +0000
@@ -687,7 +687,7 @@
 
 DIRS = alpha arm bfin mlib ppc ps2 sh4 sparc x86
 
-CLEANFILES = sin_tables.c cos_tables.c costablegen$(HOSTEXESUF)
+CLEANFILES = sin_tables.c cos_tables.c costablegen$(HOSTEXESUF) *_tables.h *_tablegen$(HOSTEXESUF)
 
 include $(SUBDIR)../subdir.mak
 
@@ -701,3 +701,15 @@
 
 $(SUBDIR)sin_tables.c: $(SUBDIR)costablegen$(HOSTEXESUF)
 	./$< sin > $@
+
+ifeq ($(CONFIG_MPEGAUDIO_HP),yes)
+$(SUBDIR)mpegaudio_tablegen$(HOSTEXESUF): HOSTCFLAGS += -DFRAC_BITS=23
+else
+$(SUBDIR)mpegaudio_tablegen$(HOSTEXESUF): HOSTCFLAGS += -DFRAC_BITS=15
+endif
+$(SUBDIR)%_tablegen$(HOSTEXESUF): $(SUBDIR)%_tablegen.c $(SUBDIR)tableprint.c
+	$(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ $(HOSTLIBS)
+
+$(SUBDIR)mpegaudiodec.c: $(SUBDIR)mpegaudio_tables.h
+$(SUBDIR)%_tables.h: $(SUBDIR)%_tablegen$(HOSTEXESUF)
+	./$< > $@
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpegaudio_tablegen.c	Wed Oct 28 18:42:52 2009 +0000
@@ -0,0 +1,60 @@
+/*
+ * Generate a header file for hardcoded mpegaudiodec tables
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
+ *
+ * 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 "mpegaudio_tablegen.h"
+#include "tableprint.h"
+
+void tableinit(void)
+{
+    mpegaudio_tableinit();
+}
+
+const struct tabledef tables[] = {
+    {
+        "static const int8_t table_4_3_exp[TABLE_4_3_SIZE]",
+        write_int8_array,
+        table_4_3_exp,
+        TABLE_4_3_SIZE
+    },
+    {
+        "static const uint32_t table_4_3_value[TABLE_4_3_SIZE]",
+        write_uint32_array,
+        table_4_3_value,
+        TABLE_4_3_SIZE
+    },
+    {
+        "static const uint32_t exp_table[512]",
+        write_uint32_array,
+        exp_table,
+        512
+    },
+    {
+        "static const uint32_t expval_table[512][16]",
+        write_uint32_2d_array,
+        expval_table,
+        512,
+        16
+    },
+    { NULL }
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mpegaudio_tablegen.h	Wed Oct 28 18:42:52 2009 +0000
@@ -0,0 +1,68 @@
+/*
+ * Header file for hardcoded mpegaudiodec tables
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
+ *
+ * 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 MPEGAUDIO_TABLEGEN_H
+#define MPEGAUDIO_TABLEGEN_H
+
+#include <stdint.h>
+// do not use libavutil/mathematics.h since this is compiled both
+// for the host and the target and config.h is only valid for the target
+#include <math.h>
+
+#define TABLE_4_3_SIZE (8191 + 16)*4
+#if CONFIG_HARDCODED_TABLES
+#define mpegaudio_tableinit()
+#include "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];
+static uint32_t exp_table[512];
+static uint32_t expval_table[512][16];
+
+static void mpegaudio_tableinit(void)
+{
+    int i;
+    for(i=1;i<TABLE_4_3_SIZE;i++) {
+        double value = i/4;
+        double f, fm;
+        int e, m;
+        f = value * cbrtf(value) * pow(2, (i&3)*0.25);
+        fm = frexp(f, &e);
+        m = (uint32_t)(fm*(1LL<<31) + 0.5);
+        e+= FRAC_BITS - 31 + 5 - 100;
+
+        /* normalized to FRAC_BITS */
+        table_4_3_value[i] = m;
+        table_4_3_exp[i] = -e;
+    }
+    for(i=0; i<512*16; i++){
+        double value = i & 15;
+        int exponent= (i>>4);
+        double f= value * cbrtf(value) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
+        expval_table[exponent][i&15]= llrint(f);
+        if((i&15)==1)
+            exp_table[exponent]= llrint(f);
+    }
+}
+#endif /* CONFIG_HARDCODED_TABLES && !TABLEGEN */
+
+#endif /* MPEGAUDIO_TABLEGEN_H */
--- a/mpegaudiodec.c	Wed Oct 28 14:40:05 2009 +0000
+++ b/mpegaudiodec.c	Wed Oct 28 18:42:52 2009 +0000
@@ -92,12 +92,7 @@
 };
 /* computed from band_size_long */
 static uint16_t band_index_long[9][23];
-/* XXX: free when all decoders are closed */
-#define TABLE_4_3_SIZE (8191 + 16)*4
-static int8_t  table_4_3_exp[TABLE_4_3_SIZE];
-static uint32_t table_4_3_value[TABLE_4_3_SIZE];
-static uint32_t exp_table[512];
-static uint32_t expval_table[512][16];
+#include "mpegaudio_tablegen.h"
 /* intensity stereo coef table */
 static int32_t is_table[2][16];
 static int32_t is_table_lsf[2][2][16];
@@ -407,27 +402,7 @@
         /* compute n ^ (4/3) and store it in mantissa/exp format */
 
         int_pow_init();
-        for(i=1;i<TABLE_4_3_SIZE;i++) {
-            double value = i/4;
-            double f, fm;
-            int e, m;
-            f = value * cbrtf(value) * pow(2, (i&3)*0.25);
-            fm = frexp(f, &e);
-            m = (uint32_t)(fm*(1LL<<31) + 0.5);
-            e+= FRAC_BITS - 31 + 5 - 100;
-
-            /* normalized to FRAC_BITS */
-            table_4_3_value[i] = m;
-            table_4_3_exp[i] = -e;
-        }
-        for(i=0; i<512*16; i++){
-            double value = i & 15;
-            int exponent= (i>>4);
-            double f= value * cbrtf(value) * pow(2, (exponent-400)*0.25 + FRAC_BITS + 5);
-            expval_table[exponent][i&15]= llrint(f);
-            if((i&15)==1)
-                exp_table[exponent]= llrint(f);
-        }
+        mpegaudio_tableinit();
 
         for(i=0;i<7;i++) {
             float f;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tableprint.c	Wed Oct 28 18:42:52 2009 +0000
@@ -0,0 +1,72 @@
+/*
+ * Generate a file for hardcoded tables
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
+ *
+ * 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 <stdio.h>
+#include <inttypes.h>
+#include "tableprint.h"
+
+#define WRITE_1D_FUNC(name, type, fmtstr, linebrk)\
+void write_##name##_array(const void *arg, int len, int dummy)\
+{\
+    const type *data = arg;\
+    int i;\
+    printf("   ");\
+    for (i = 0; i < len - 1; i++) {\
+       printf(" "fmtstr",", data[i]);\
+       if ((i & linebrk) == linebrk) printf("\n   ");\
+    }\
+    printf(" "fmtstr"\n", data[i]);\
+}
+
+WRITE_1D_FUNC(int8,   int8_t,   "%3"PRIi8, 15)
+WRITE_1D_FUNC(uint32, uint32_t, "0x%08x",   7)
+
+#define WRITE_2D_FUNC(name, type)\
+void write_##name##_2d_array(const void *arg, int len, int len2)\
+{\
+    const type *data = arg;\
+    int i;\
+    printf("    {");\
+    for (i = 0; i < len; i++) {\
+        write_##name##_array(data + i * len2, len2, 0);\
+        printf(i == len - 1 ? "    }\n" : "    }, {\n");\
+    }\
+}
+
+WRITE_2D_FUNC(uint32, uint32_t)
+
+int main(int argc, char *argv[])
+{
+    int i;
+
+    printf("/* This file was generated by libavcodec/tableprint */\n");
+    printf("#include <stdint.h>\n");
+    tableinit();
+
+    for (i = 0; tables[i].declaration; i++) {
+        printf(tables[i].declaration);
+        printf(" = {\n");
+        tables[i].printfunc(tables[i].data, tables[i].size, tables[i].size2);
+        printf("};\n");
+    }
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tableprint.h	Wed Oct 28 18:42:52 2009 +0000
@@ -0,0 +1,58 @@
+/*
+ * Generate a file for hardcoded tables
+ *
+ * Copyright (c) 2009 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
+ *
+ * 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_TABLEPRINT_H
+#define AVCODEC_TABLEPRINT_H
+
+#include <stdint.h>
+
+/**
+ * \defgroup printfuncs Predefined functions for printing tables
+ *
+ * \{
+ */
+void write_int8_array     (const void *, int, int);
+void write_uint32_array   (const void *, int, int);
+void write_uint32_2d_array(const void *, int, int);
+/** \} */ // end of printfuncs group
+
+struct tabledef {
+    /** String that declares the array. Adding " = { ..." after it should
+     * make a valid initializer, adding "extern" before and ";" if possible
+     * should make a valid extern declaration. */
+    const char *declaration;
+    /** Function used to print the table data (i.e. the part in {}).
+     * Should be one of the predefined write_*_array functions. */
+    void (*printfunc)(const void *, int, int);
+    /** Pointer passed to the printfunc, usually a pointer to the start
+     * of the array to be printed. */
+    const void *data;
+    int size;   ///< size of the first dimension of the array
+    int size2;  ///< size of the second dimension of the array if any
+};
+
+/** Initializes all the tables described in the tables array */
+void tableinit(void);
+/** Describes the tables that should be printed */
+extern const struct tabledef tables[];
+
+#endif /* AVCODEC_TABLEPRINT_H */