Debian Codegraph @

src/libmpg123/calctables.c

raw
1 /*
2 calctables: compute fixed decoder table values
3
4 copyright ?-2021 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Michael Hipp (as tabinit.c, and parts of layer2.c and layer3.c),
7 then printout added by Thomas Orgis
8
9 This is supposed to compute the supposedly fixed tables that used to be computed
10 live on library startup in mpg123_init().
11 */
12
13 #define CALCTABLES
14 #include "mpg123lib_intern.h"
15 #include "../common/debug.h"
16
17 #define ASIZE(a) (sizeof(a)/sizeof(*a))
18
19 // The variable definitions here are redundant with the runtime part of
20 // the init_* headers, but that is fine, as those headers are generated
21 // by this very program.
22
23 static double cos64[16],cos32[8],cos16[4],cos8[2],cos4[1];
24 #include "init_costabs.h"
25
26 // layer I+II tables
27 static double layer12_table[27][64];
28
29 // Storing only values 0 to 26, so char is fine.
30 // The size of those might be reduced ...
31 static unsigned char grp_3tab[32 * 3] = { 0, }; /* used: 27 */
32 static unsigned char grp_5tab[128 * 3] = { 0, }; /* used: 125 */
33 static unsigned char grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
34
35 #include "init_layer12.h"
36
37 // layer III
38
39 static double ispow[8207]; // scale with SCALE_POW43
40 static double aa_ca[8],aa_cs[8];
41 static double win[4][36];
42 static double win1[4][36];
43 double INT123_COS9[9]; /* INT123_dct36_3dnow wants to use that */
44 static double COS6_1,COS6_2;
45 double INT123_tfcos36[9]; /* INT123_dct36_3dnow wants to use that */
46 static double tfcos12[3];
47 static double cos9[3],cos18[3];
48
49 // scale with SCALE_15
50 static double tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
51 // This used to be [2][32] initially, but already Taihei noticed that
52 // _apparently_ only [2][16] is used for the integer tables. But
53 // this is misleading: Accesses beyond that happen, at least in pathologic
54 // cases. Taihei's fixed-point decoding introduced read-only buffer
55 // overflows:-/ Those are rather harmless, though, as only bad numbers
56 // enter calculation. Nothing about pointers or code.
57 static double pow1_1[2][32],pow2_1[2][32],pow1_2[2][32],pow2_2[2][32];
58
59
60 // That needs special handling for storing the pointers
61 // in map and mapend.
62 // map[j][N] = mapbufN[j];
63 // mapend[j][N] = ... varies let's use
64 // mapbufN[j]+(mapend[j][N]-map[j][N])
65 static short mapbuf0[9][152];
66 static short mapbuf1[9][156];
67 static short mapbuf2[9][44];
68 static short *map[9][3];
69 static short *mapend[9][3];
70
71 static unsigned short n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
72 static unsigned short i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
73
74 static real gainpow2[256+118+4];
75
76 #include "l3bandgain.h"
77 #include "init_layer3.h"
78
79 // helpers
80
81 static void print_char_array( const char *indent, const char *name
82 , size_t count, unsigned char tab[], int calc )
83 {
84 if(calc)
85 {
86 if(name)
87 printf("static unsigned char %s[%zu];\n", name, count);
88 return;
89 }
90 size_t block = 72/4;
91 size_t i = 0;
92 if(name)
93 printf("static const unsigned char %s[%zu] = \n", name, count);
94 printf("%s{\n", indent);
95 while(i<count)
96 {
97 size_t line = block > count-i ? count-i : block;
98 printf("%s", indent);
99 for(size_t j=0; j<line; ++j, ++i)
100 printf("%s%c%3u", i ? "," : "", j ? ' ' : '\t', tab[i]);
101 printf("\n");
102 }
103 printf("%s}%s\n", indent, name ? ";" : "");
104 }
105
106 static void print_value( int fixed, double fixed_scale
107 , const char *name, double val, int calc )
108 {
109 if(calc)
110 {
111 if(name)
112 printf("static real %s;\n", name);
113 return;
114 }
115 if(name)
116 printf("static const real %s = ", name);
117 if(fixed)
118 printf("%ld;\n", (long)(DOUBLE_TO_REAL(fixed_scale*val)));
119 else
120 printf("%15.8ef;\n", val);
121 }
122
123 // I feal uneasy about inf appearing as literal.
124 // Do all C99 implementations support it the same?
125 // An unreasonably big value should also just work.
126 static double limit_val(double val)
127 {
128 if(val > 1e38)
129 return 1e38;
130 if(val < -1e38)
131 return -1e38;
132 return val;
133 }
134
135 static void print_array( int statick, int fixed, double fixed_scale
136 , const char *indent, const char *name
137 , size_t count, double tab[], int calc )
138 {
139 if(calc)
140 {
141 if(!fixed && name)
142 printf( "%sALIGNED(16) real %s[%zu];\n"
143 , statick ? "static " : "", name, count );
144 return;
145 }
146 size_t block = 72/18;
147 size_t i = 0;
148 if(name)
149 printf( "%sconst%s real %s[%zu] = \n", statick ? "static " : ""
150 , fixed ? "" : " ALIGNED(16)", name, count );
151 printf("%s{\n", indent);
152 while(i<count)
153 {
154 size_t line = block > count-i ? count-i : block;
155 printf("%s", indent);
156 if(fixed) for(size_t j=0; j<line; ++j, ++i)
157 printf( "%s%c%11ld", i ? "," : "", j ? ' ' : '\t'
158 , (long)(DOUBLE_TO_REAL(fixed_scale*tab[i])) );
159 else for(size_t j=0; j<line; ++j, ++i)
160 printf("%s%c%15.8ef", i ? "," : "", j ? ' ' : '\t', limit_val(tab[i]));
161 printf("\n");
162 }
163 printf("%s}%s\n", indent, name ? ";" : "");
164 }
165
166 // C99 allows passing VLA with the fast dimensions first.
167 static void print_array2d( int fixed, double fixed_scale
168 , const char *name, size_t x, size_t y
169 , double tab[][y], int calc )
170 {
171 if(calc)
172 {
173 if(!fixed)
174 printf("static ALIGNED(16) real %s[%zu][%zu];\n", name, x, y);
175 return;
176 }
177 printf( "static const%s real %s[%zu][%zu] = \n{\n", fixed ? "" : " ALIGNED(16)"
178 , name, x, y );
179 for(size_t i=0; i<x; ++i)
180 {
181 if(i)
182 printf(",");
183 print_array(1, fixed, fixed_scale, "\t", NULL, y, tab[i], calc);
184 }
185 printf("};\n");
186 }
187
188 static void print_short_array( const char *indent, const char *name
189 , size_t count, short tab[], int calc )
190 {
191 if(calc)
192 {
193 if(name)
194 printf("static short %s[%zu];\n", name, count);
195 return;
196 }
197 size_t block = 72/8;
198 size_t i = 0;
199 if(name)
200 printf("static const short %s[%zu] = \n", name, count);
201 printf("%s{\n", indent);
202 while(i<count)
203 {
204 size_t line = block > count-i ? count-i : block;
205 printf("%s", indent);
206 for(size_t j=0; j<line; ++j, ++i)
207 printf("%s%c%6d", i ? "," : "", j ? ' ' : '\t', tab[i]);
208 printf("\n");
209 }
210 printf("%s}%s\n", indent, name ? ";" : "");
211 }
212
213 static void print_fixed_array( const char *indent, const char *name
214 , size_t count, real tab[] )
215 {
216 size_t block = 72/13;
217 size_t i = 0;
218 if(name)
219 printf("static const real %s[%zu] = \n", name, count);
220 printf("%s{\n", indent);
221 while(i<count)
222 {
223 size_t line = block > count-i ? count-i : block;
224 printf("%s", indent);
225 for(size_t j=0; j<line; ++j, ++i)
226 printf("%s%c%11ld", i ? "," : "", j ? ' ' : '\t', (long)tab[i]);
227 printf("\n");
228 }
229 printf("%s}%s\n", indent, name ? ";" : "");
230 }
231
232 static void print_ushort_array( const char *indent, const char *name
233 , size_t count, unsigned short tab[], int calc )
234 {
235 if(calc)
236 {
237 if(name)
238 printf("static unsigned short %s[%zu];\n", name, count);
239 return;
240 }
241 size_t block = 72/8;
242 size_t i = 0;
243 if(name)
244 printf("static const unsigned short %s[%zu] =\n", name, count);
245 printf("%s{\n", indent);
246 while(i<count)
247 {
248 size_t line = block > count-i ? count-i : block;
249 printf("%s", indent);
250 for(size_t j=0; j<line; ++j, ++i)
251 printf("%s%c%8u", i ? "," : "", j ? ' ' : '\t', tab[i]);
252 printf("\n");
253 }
254 printf("%s}%s\n", indent, name ? ";" : "");
255 }
256
257 // C99 allows passing VLA with the fast dimensions first.
258 static void print_short_array2d( const char *name, size_t x, size_t y
259 , short tab[][y], int calc )
260 {
261 if(calc)
262 {
263 printf("static short %s[%zu][%zu];\n", name, x, y);
264 return;
265 }
266 printf( "static const short %s[%zu][%zu] =\n{\n"
267 , name, x, y );
268 for(size_t i=0; i<x; ++i)
269 {
270 if(i)
271 printf(",");
272 print_short_array("\t", NULL, y, tab[i], calc);
273 }
274 printf("};\n");
275 }
276
277 void printer(int calc, char *arg)
278 {
279 // No fixed point output for run-time calculation.
280 for(int fixed=0; fixed < (calc ? 1 : 2); ++fixed)
281 {
282 printf("\n#ifdef %s\n\n", fixed ? "REAL_IS_FIXED" : "REAL_IS_FLOAT");
283 if(!fixed)
284 printf("// aligned to 16 bytes for vector instructions, e.g. AltiVec\n\n");
285 if(!strcmp("cos", arg))
286 {
287 print_array(1, fixed, 1., "", "cos64", ASIZE(cos64), cos64, calc);
288 print_array(1, fixed, 1., "", "cos32", ASIZE(cos32), cos32, calc);
289 print_array(1, fixed, 1., "", "cos16", ASIZE(cos16), cos16, calc);
290 print_array(1, fixed, 1., "", "cos8", ASIZE(cos8), cos8, calc);
291 print_array(1, fixed, 1., "", "cos4", ASIZE(cos4), cos4, calc);
292 }
293 if(!strcmp("l12", arg))
294 {
295 print_array2d( fixed, SCALE_LAYER12/REAL_FACTOR, "layer12_table"
296 , 27, 64, layer12_table, calc );
297 }
298 if(!strcmp("l3", arg))
299 {
300 print_array( 1, fixed, SCALE_POW43/REAL_FACTOR, "", "ispow"
301 , sizeof(ispow)/sizeof(*ispow), ispow, calc );
302 print_array(1, fixed, 1., "", "aa_ca", ASIZE(aa_ca), aa_ca, calc);
303 print_array(1, fixed, 1., "", "aa_cs", ASIZE(aa_cs), aa_cs, calc);
304 print_array2d(fixed, 1., "win", 4, 36, win, calc);
305 print_array2d(fixed, 1., "win1", 4, 36, win1, calc);
306 print_array(0, fixed, 1., "", "INT123_COS9", ASIZE(INT123_COS9), INT123_COS9, calc);
307 print_value(fixed, 1., "COS6_1", COS6_1, calc);
308 print_value(fixed, 1., "COS6_2", COS6_2, calc);
309 print_array(0, fixed, 1., "", "INT123_tfcos36", ASIZE(INT123_tfcos36), INT123_tfcos36, calc);
310 print_array(1, fixed, 1., "", "tfcos12", ASIZE(tfcos12), tfcos12, calc);
311 print_array(1, fixed, 1., "", "cos9", ASIZE(cos9), cos9, calc);
312 print_array(1, fixed, 1., "", "cos18", ASIZE(cos18), cos18, calc);
313 print_array( 1, fixed, SCALE_15/REAL_FACTOR, ""
314 , "tan1_1", ASIZE(tan1_1), tan1_1, calc );
315 print_array( 1, fixed, SCALE_15/REAL_FACTOR, ""
316 , "tan2_1", ASIZE(tan2_1), tan2_1, calc );
317 print_array( 1, fixed, SCALE_15/REAL_FACTOR, ""
318 , "tan1_2", ASIZE(tan1_2), tan1_2, calc );
319 print_array( 1, fixed, SCALE_15/REAL_FACTOR, ""
320 , "tan2_2", ASIZE(tan2_2), tan2_2, calc );
321 print_array2d( fixed, SCALE_15/REAL_FACTOR
322 , "pow1_1", 2, 32, pow1_1, calc );
323 print_array2d( fixed, SCALE_15/REAL_FACTOR
324 , "pow2_1", 2, 32, pow2_1, calc );
325 print_array2d( fixed, SCALE_15/REAL_FACTOR
326 , "pow1_2", 2, 32, pow1_2, calc );
327 print_array2d( fixed, SCALE_15/REAL_FACTOR
328 , "pow2_2", 2, 32, pow2_2, calc );
329 if(fixed)
330 print_fixed_array("", "gainpow2", ASIZE(gainpow2), gainpow2);
331 }
332 printf("\n#endif\n");
333 }
334 if(!strcmp("l12", arg))
335 {
336 printf("\n");
337 print_char_array("", "grp_3tab", ASIZE(grp_3tab), grp_3tab, calc);
338 printf("\n");
339 print_char_array("", "grp_5tab", ASIZE(grp_5tab), grp_5tab, calc);
340 printf("\n");
341 print_char_array("", "grp_9tab", ASIZE(grp_9tab), grp_9tab, calc);
342 }
343 if(!strcmp("l3", arg))
344 {
345 printf("\n");
346 print_short_array2d("mapbuf0", 9, 152, mapbuf0, calc);
347 print_short_array2d("mapbuf1", 9, 156, mapbuf1, calc);
348 print_short_array2d("mapbuf2", 9, 44, mapbuf2, calc);
349 if(calc)
350 {
351 printf("static short *map[9][3];\n");
352 printf("static short *mapend[9][3];\n");
353 }
354 else
355 {
356 printf("static const short *map[9][3] =\n{\n");
357 for(int i=0; i<9; ++i)
358 printf( "%s\t{ mapbuf0[%d], mapbuf1[%d], mapbuf2[%d] }\n"
359 , (i ? "," : ""), i, i, i );
360 printf("};\n");
361 printf("static const short *mapend[9][3] =\n{\n");
362 for(int i=0; i<9; ++i)
363 printf( "%s\t{ mapbuf0[%d]+%d, mapbuf1[%d]+%d, mapbuf2[%d]+%d }\n"
364 , (i ? "," : "")
365 , i, (int)(mapend[i][0]-map[i][0])
366 , i, (int)(mapend[i][1]-map[i][1])
367 , i, (int)(mapend[i][2]-map[i][2]) );
368 printf("};\n");
369 }
370 print_ushort_array("", "n_slen2", ASIZE(n_slen2), n_slen2, calc);
371 print_ushort_array("", "i_slen2", ASIZE(i_slen2), i_slen2, calc);
372 }
373 }
374
375 int main(int argc, char **argv)
376 {
377 if(argc != 2)
378 {
379 fprintf(stderr, "usage:\n\t%s <cos|l12|l3>\n\n", argv[0]);
380 return 1;
381 }
382 printf("// %s MPEG decoding tables\n", argv[1]);
383 printf("// output of:\n// %s", argv[0]);
384 for(int i=1; i<argc; ++i)
385 printf(" %s", argv[i]);
386 printf("\n");
387
388 INT123_init_costabs();
389 INT123_init_layer12();
390 INT123_init_layer3();
391
392 printf("\n#if defined(RUNTIME_TABLES)\n");
393 printer(1, argv[1]);
394 printf("\n#else\n");
395 printer(0, argv[1]);
396 printf("\n#endif\n");
397
398 return 0;
399 }