[vlc-commits] fourcc: preprocess the tables and sort them at build time

Rémi Denis-Courmont git at videolan.org
Thu Jun 11 22:40:10 CEST 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Thu Jun 11 20:49:59 2015 +0300| [9e6b1f9839f671aa38fcc5948c16b978d7780246] | committer: Rémi Denis-Courmont

fourcc: preprocess the tables and sort them at build time

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=9e6b1f9839f671aa38fcc5948c16b978d7780246
---

 include/vlc_es.h       |    1 +
 include/vlc_fourcc.h   |    2 -
 src/.gitignore         |    2 +
 src/Makefile.am        |   14 +++++
 src/misc/fourcc_gen.c  |  156 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/misc/fourcc_list.h |    8 ++-
 6 files changed, 178 insertions(+), 5 deletions(-)

diff --git a/include/vlc_es.h b/include/vlc_es.h
index ff31b63..b041862 100644
--- a/include/vlc_es.h
+++ b/include/vlc_es.h
@@ -24,6 +24,7 @@
 #ifndef VLC_ES_H
 #define VLC_ES_H 1
 
+#include <vlc_common.h>
 #include <vlc_fourcc.h>
 #include <vlc_text_style.h>
 
diff --git a/include/vlc_fourcc.h b/include/vlc_fourcc.h
index 1494ca7..4bb050d 100644
--- a/include/vlc_fourcc.h
+++ b/include/vlc_fourcc.h
@@ -24,8 +24,6 @@
 #ifndef VLC_FOURCC_H
 #define VLC_FOURCC_H 1
 
-#include <vlc_common.h>
-
 /* Video codec */
 #define VLC_CODEC_MPGV            VLC_FOURCC('m','p','g','v')
 #define VLC_CODEC_MP4V            VLC_FOURCC('m','p','4','v')
diff --git a/src/.gitignore b/src/.gitignore
index de9cab7..13f18ff 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -1,4 +1,6 @@
 test_*
+fourcc_gen
+fourcc_tables.h
 libvlc_win32_rc.rc
 revision.c
 revision.txt
diff --git a/src/Makefile.am b/src/Makefile.am
index c96c9fb..e608e4d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -515,6 +515,20 @@ SOURCES_libvlc = \
 	$(SOURCES_libvlc_common) \
 	$(NULL)
 
+# FourCC tables
+BUILT_SOURCES += fourcc_tables.h
+EXTRA_DIST += misc/fourcc_gen.c
+MOSTLYCLEANFILES = fourcc_gen
+
+fourcc_gen: misc/fourcc_gen.c misc/fourcc_list.h ../include/vlc_fourcc.h
+	$(AM_V_at)rm -f -- $@
+	$(AM_V_CC)$(BUILDCC) -I$(srcdir) -o $@ $<
+
+fourcc_tables.h: fourcc_gen
+	$(AM_V_at)rm -f -- $@.tmp
+	$(AM_V_GEN)$(builddir)/fourcc_gen > $@.tmp
+	$(AM_V_at)mv -f -- $@.tmp $@
+
 # Unit/regression tests
 #
 check_PROGRAMS = \
diff --git a/src/misc/fourcc_gen.c b/src/misc/fourcc_gen.c
new file mode 100644
index 0000000..d8903ac
--- /dev/null
+++ b/src/misc/fourcc_gen.c
@@ -0,0 +1,156 @@
+/*****************************************************************************
+ * fourcc_gen.c: FourCC preprocessor
+ *****************************************************************************
+ * Copyright © 2015 Rémi Denis-Courmont
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/* DO NOT include "config.h" here */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define VLC_API
+#define VLC_USED
+typedef uint32_t vlc_fourcc_t;
+#include "../include/vlc_fourcc.h"
+
+#define VLC_FOURCC(a,b,c,d) { a, b, c, d }
+#define A(sfcc) E(sfcc, NULL)
+#define B(fcc,dsc) { true, fcc, dsc }
+#define E(sfcc,dsc) { false, sfcc, dsc }
+
+typedef struct
+{
+    bool klass;
+    char fourcc[4];
+    const char *description;
+} staticentry_t;
+
+#include "misc/fourcc_list.h"
+
+struct entry
+{
+    char fourcc[4];
+    char alias[4];
+    const char *desc;
+};
+
+static int cmp_entry(const void *a, const void *b)
+{
+    const struct entry *ea = a, *eb = b;
+
+    return memcmp(ea->alias, eb->alias, 4);
+}
+
+static void process_list(const char *name, const staticentry_t *list, size_t n)
+{
+    assert(n > 0);
+    n--; /* discard final nul entry  */
+
+    struct entry *entries = malloc(sizeof (*entries) * n);
+    if (entries == NULL)
+        abort();
+
+    const staticentry_t *klass = NULL;
+
+    for (size_t i = 0; i < n; i++)
+    {
+        if (list[i].klass)
+            klass = &list[i];
+
+        if (klass == NULL)
+        {
+            fprintf(stderr, "Error: FourCC \"%.4s\" not mapped!\n",
+                    list[i].fourcc);
+            exit(1);
+        }
+
+        memcpy(entries[i].fourcc, klass->fourcc, 4);
+        memcpy(entries[i].alias, list[i].fourcc, 4);
+        entries[i].desc = list[i].description;
+    }
+
+    qsort(entries, n, sizeof (*entries), cmp_entry);
+
+    size_t dups = 0;
+    for (size_t i = 1; i < n; i++)
+    {
+        if (!memcmp(entries[i].fourcc, entries[i].alias, 4))
+            continue;
+        if (!memcmp(entries[i - 1].alias, entries[i].alias, 4))
+        {
+            fprintf(stderr, "Error: FourCC \"%.4s\" (alias of \"%.4s\") "
+                    "duplicated!\n", entries[i].alias, entries[i].fourcc);
+            dups++;
+        }
+    }
+
+    if (dups > 0)
+        exit(1);
+
+    printf("static const struct fourcc_mapping mapping_%s[] = {\n", name);
+    for (size_t i = 0; i < n; i++)
+    {
+        if (!memcmp(entries[i].fourcc, entries[i].alias, 4))
+            continue;
+        printf("    { { { 0x%02hhx, 0x%02hhx, 0x%02hhx, 0x%02hhx } }, "
+               "{ { 0x%02hhx, 0x%02hhx, 0x%02hhx, 0x%02hhx } } },\n",
+               entries[i].alias[0], entries[i].alias[1], entries[i].alias[2],
+               entries[i].alias[3], entries[i].fourcc[0], entries[i].fourcc[1],
+               entries[i].fourcc[2], entries[i].fourcc[3]);
+    }
+    puts("};");
+    printf("static const struct fourcc_desc desc_%s[] = {\n", name);
+    for (size_t i = 0; i < n; i++)
+    {
+        if (entries[i].desc == NULL)
+            continue;
+        printf("    { { { 0x%02hhx, 0x%02hhx, 0x%02hhx, 0x%02hhx } }, "
+               "\"%s\" },\n", entries[i].alias[0], entries[i].alias[1],
+               entries[i].alias[2], entries[i].alias[3], entries[i].desc);
+    }
+    puts("};");
+
+    free(entries);
+    fprintf(stderr, "%s: %zu entries\n", name, n);
+}
+
+int main(void)
+{
+    puts("/* This file is generated automatically. DO NOT EDIT! */");
+    puts("struct fourcc_mapping {");
+    puts("    union { unsigned char alias_str[4]; vlc_fourcc_t alias; };");
+    puts("    union { unsigned char fourcc_str[4]; vlc_fourcc_t fourcc; };");
+    puts("};");
+    puts("struct fourcc_desc {");
+    puts("    union { unsigned char alias_str[4]; vlc_fourcc_t alias; };");
+    puts("    const char desc[52];");
+    puts("};");
+
+#define p(t) \
+    process_list(#t, p_list_##t, \
+                 sizeof (p_list_##t) / sizeof ((p_list_##t)[0]))
+    p(video);
+    p(audio);
+    p(spu);
+    return 0;
+}
diff --git a/src/misc/fourcc_list.h b/src/misc/fourcc_list.h
index 29ca3eb..42513c2 100644
--- a/src/misc/fourcc_list.h
+++ b/src/misc/fourcc_list.h
@@ -1076,8 +1076,9 @@ static const staticentry_t p_list_video[] = {
 
     B(VLC_CODEC_HNM4_VIDEO, "Cryo Interactive Entertainment HNM4"),
 
-    B(0, "")
+    B(VLC_FOURCC(0,0,0,0), "")
 };
+
 static const staticentry_t p_list_audio[] = {
 
     /* Windows Media Audio 1 */
@@ -1482,8 +1483,9 @@ static const staticentry_t p_list_audio[] = {
 
     B(VLC_CODEC_ADPCM_IMA_APC, "ADPCM APC"),
 
-    B(0, "")
+    B(VLC_FOURCC(0,0,0,0), "")
 };
+
 static const staticentry_t p_list_spu[] = {
 
     B(VLC_CODEC_SPU, "DVD Subtitles"),
@@ -1553,5 +1555,5 @@ static const staticentry_t p_list_spu[] = {
     B(VLC_CODEC_TTML, "TTML subtitles"),
         A("ttml"),
 
-    B(0, "")
+    B(VLC_FOURCC(0,0,0,0), "")
 };



More information about the vlc-commits mailing list