codec: jpeg: fix sanity checks
[vlc.git] / src / misc / fourcc_gen.c
blob5f917836903cc27bde96e83fc00e2f9950aba29e
1 /*****************************************************************************
2 * fourcc_gen.c: FourCC preprocessor
3 *****************************************************************************
4 * Copyright © 2015 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 /* DO NOT include "config.h" here */
23 #include <assert.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #define VLC_API
32 #define VLC_USED
33 typedef uint32_t vlc_fourcc_t;
34 typedef struct { unsigned num, den; } vlc_rational_t;
35 #include "../include/vlc_fourcc.h"
37 #define VLC_FOURCC(a,b,c,d) { a, b, c, d }
38 #define A(sfcc) E(sfcc, NULL)
39 #define B(fcc,dsc) { true, fcc, dsc }
40 #define E(sfcc,dsc) { false, sfcc, dsc }
42 typedef struct
44 bool klass;
45 char fourcc[4];
46 const char *description;
47 } staticentry_t;
49 #include "misc/fourcc_list.h"
51 struct entry
53 char fourcc[4];
54 char alias[4];
55 const char *desc;
58 static int cmp_entry(const void *a, const void *b)
60 const struct entry *ea = a, *eb = b;
61 int d = memcmp(ea->alias, eb->alias, 4);
62 if (d == 0)
63 d = memcmp(ea->fourcc, eb->fourcc, 4);
64 return d;
67 static void process_list(const char *name, const staticentry_t *list, size_t n)
69 struct entry *entries = malloc(sizeof (*entries) * n);
70 if (entries == NULL)
71 abort();
73 const staticentry_t *klass = NULL;
75 for (size_t i = 0; i < n; i++)
77 if (list[i].klass)
78 klass = &list[i];
80 if (klass == NULL)
82 fprintf(stderr, "Error: FourCC \"%.4s\" not mapped!\n",
83 list[i].fourcc);
84 exit(1);
87 memcpy(entries[i].fourcc, klass->fourcc, 4);
88 memcpy(entries[i].alias, list[i].fourcc, 4);
89 entries[i].desc = list[i].description;
92 qsort(entries, n, sizeof (*entries), cmp_entry);
94 size_t dups = 0;
95 for (size_t i = 1; i < n; i++)
96 if (!memcmp(entries[i - 1].alias, entries[i].alias, 4)
97 && memcmp(entries[i - 1].fourcc, entries[i].fourcc, 4))
99 fprintf(stderr, "Error: FourCC alias \"%.4s\" conflict: "
100 "\"%.4s\" and \"%.4s\"\n", entries[i].alias,
101 entries[i - 1].fourcc, entries[i].fourcc);
102 dups++;
105 if (dups > 0)
106 exit(1);
108 printf("static const struct fourcc_mapping mapping_%s[] = {\n", name);
109 for (size_t i = 0; i < n; i++)
111 if (!memcmp(entries[i].fourcc, entries[i].alias, 4))
112 continue;
113 printf(" { { { 0x%02hhx, 0x%02hhx, 0x%02hhx, 0x%02hhx } }, "
114 "{ { 0x%02hhx, 0x%02hhx, 0x%02hhx, 0x%02hhx } } },\n",
115 entries[i].alias[0], entries[i].alias[1], entries[i].alias[2],
116 entries[i].alias[3], entries[i].fourcc[0], entries[i].fourcc[1],
117 entries[i].fourcc[2], entries[i].fourcc[3]);
119 puts("};");
120 printf("static const struct fourcc_desc desc_%s[] = {\n", name);
121 for (size_t i = 0; i < n; i++)
123 if (entries[i].desc == NULL)
124 continue;
125 printf(" { { { 0x%02hhx, 0x%02hhx, 0x%02hhx, 0x%02hhx } }, "
126 "\"%s\" },\n", entries[i].alias[0], entries[i].alias[1],
127 entries[i].alias[2], entries[i].alias[3], entries[i].desc);
129 puts("};");
131 free(entries);
132 fprintf(stderr, "%s: %zu entries\n", name, n);
135 int main(void)
137 puts("/* This file is generated automatically. DO NOT EDIT! */");
138 puts("struct fourcc_mapping {");
139 puts(" union { unsigned char alias_str[4]; vlc_fourcc_t alias; };");
140 puts(" union { unsigned char fourcc_str[4]; vlc_fourcc_t fourcc; };");
141 puts("};");
142 puts("struct fourcc_desc {");
143 puts(" union { unsigned char alias_str[4]; vlc_fourcc_t alias; };");
144 puts(" const char desc[52];");
145 puts("};");
147 #define p(t) \
148 process_list(#t, p_list_##t, \
149 sizeof (p_list_##t) / sizeof ((p_list_##t)[0]))
150 p(video);
151 p(audio);
152 p(spu);
153 return 0;