Merge commit 'svn/origin' into devel
[cinelerra_cv/ct.git] / libmpeg3 / audio / tables.c
blobe36991d116bd1a39f2fed4afffbfaf1ba6619ec6
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
3 #include "tables.h"
5 #include <math.h>
7 /* Bitrate indexes */
8 int mpeg3_tabsel_123[2][3][16] = {
9 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
10 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
11 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
13 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
14 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
15 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
18 long mpeg3_freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
20 #ifdef USE_3DNOW
21 float mpeg3_decwin[2 * (512 + 32)];
22 float mpeg3_cos64[32], mpeg3_cos32[16], mpeg3_cos16[8], mpeg3_cos8[4], mpeg3_cos4[2];
23 #else
24 float mpeg3_decwin[512 + 32];
25 float mpeg3_cos64[16], mpeg3_cos32[8], mpeg3_cos16[4], mpeg3_cos8[2], mpeg3_cos4[1];
26 #endif
28 float *mpeg3_pnts[] = { mpeg3_cos64, mpeg3_cos32, mpeg3_cos16, mpeg3_cos8, mpeg3_cos4 };
30 int mpeg3_grp_3tab[32 * 3] = { 0, }; /* used: 27 */
31 int mpeg3_grp_5tab[128 * 3] = { 0, }; /* used: 125 */
32 int mpeg3_grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
33 float mpeg3_muls[27][64]; /* also used by layer 1 */
34 float mpeg3_gainpow2[256 + 118 + 4];
35 float mpeg3_ispow[8207];
36 float mpeg3_aa_ca[8], mpeg3_aa_cs[8];
37 float mpeg3_win[4][36];
38 float mpeg3_win1[4][36];
39 float mpeg3_COS1[12][6];
40 float mpeg3_COS9[9];
41 float mpeg3_COS6_1, mpeg3_COS6_2;
42 float mpeg3_tfcos36[9];
43 float mpeg3_tfcos12[3];
44 float mpeg3_cos9[3], mpeg3_cos18[3];
45 float mpeg3_tan1_1[16], mpeg3_tan2_1[16], mpeg3_tan1_2[16], mpeg3_tan2_2[16];
46 float mpeg3_pow1_1[2][16], mpeg3_pow2_1[2][16], mpeg3_pow1_2[2][16], mpeg3_pow2_2[2][16];
49 long mpeg3_intwinbase[] = {
50 0, -1, -1, -1, -1, -1, -1, -2, -2, -2,
51 -2, -3, -3, -4, -4, -5, -5, -6, -7, -7,
52 -8, -9, -10, -11, -13, -14, -16, -17, -19, -21,
53 -24, -26, -29, -31, -35, -38, -41, -45, -49, -53,
54 -58, -63, -68, -73, -79, -85, -91, -97, -104, -111,
55 -117, -125, -132, -139, -147, -154, -161, -169, -176, -183,
56 -190, -196, -202, -208, -213, -218, -222, -225, -227, -228,
57 -228, -227, -224, -221, -215, -208, -200, -189, -177, -163,
58 -146, -127, -106, -83, -57, -29, 2, 36, 72, 111,
59 153, 197, 244, 294, 347, 401, 459, 519, 581, 645,
60 711, 779, 848, 919, 991, 1064, 1137, 1210, 1283, 1356,
61 1428, 1498, 1567, 1634, 1698, 1759, 1817, 1870, 1919, 1962,
62 2001, 2032, 2057, 2075, 2085, 2087, 2080, 2063, 2037, 2000,
63 1952, 1893, 1822, 1739, 1644, 1535, 1414, 1280, 1131, 970,
64 794, 605, 402, 185, -45, -288, -545, -814, -1095, -1388,
65 -1692, -2006, -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788,
66 -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597, -7910, -8209,
67 -8491, -8755, -8998, -9219, -9416, -9585, -9727, -9838, -9916, -9959,
68 -9966, -9935, -9863, -9750, -9592, -9389, -9139, -8840, -8492, -8092,
69 -7640, -7134, -6574, -5959, -5288, -4561, -3776, -2935, -2037, -1082,
70 -70, 998, 2122, 3300, 4533, 5818, 7154, 8540, 9975, 11455,
71 12980, 14548, 16155, 17799, 19478, 21189, 22929, 24694, 26482, 28289,
72 30112, 31947, 33791, 35640, 37489, 39336, 41176, 43006, 44821, 46617,
73 48390, 50137, 51853, 53534, 55178, 56778, 58333, 59838, 61289, 62684,
74 64019, 65290, 66494, 67629, 68692, 69679, 70590, 71420, 72169, 72835,
75 73415, 73908, 74313, 74630, 74856, 74992, 75038 };
77 int mpeg3_longLimit[9][23];
78 int mpeg3_shortLimit[9][14];
80 struct mpeg3_bandInfoStruct mpeg3_bandInfo[9] =
83 /* MPEG 1.0 */
84 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
85 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
86 {0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3},
87 {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
89 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
90 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
91 {0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3},
92 {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
94 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
95 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
96 {0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3} ,
97 {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
99 /* MPEG 2.0 */
100 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
101 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
102 {0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} ,
103 {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
105 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
106 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
107 {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3} ,
108 {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
110 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
111 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
112 {0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3},
113 {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
114 /* MPEG 2.5 */
115 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
116 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
117 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
118 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
119 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
120 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
121 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
122 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
123 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
124 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
125 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
126 {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
129 int mpeg3_mapbuf0[9][152];
130 int mpeg3_mapbuf1[9][156];
131 int mpeg3_mapbuf2[9][44];
132 int *mpeg3_map[9][3];
133 int *mpeg3_mapend[9][3];
135 unsigned int mpeg3_n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
136 unsigned int mpeg3_i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
138 static int init_layer2(mpeg3_layer_t *audio)
140 static double mulmul[27] =
142 0.0 , -2.0/3.0 , 2.0/3.0 ,
143 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
144 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
145 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
146 -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
147 -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0
149 static int base[3][9] =
151 { 1 , 0, 2 , } ,
152 { 17, 18, 0 , 19, 20 , } ,
153 { 21, 1, 22, 23, 0, 24, 25, 2, 26 }
155 static int tablen[3] = { 3, 5, 9 };
156 static int *itable, *tables[3] = {mpeg3_grp_3tab, mpeg3_grp_5tab, mpeg3_grp_9tab};
157 int i, j, k, l, len;
158 float *table;
160 for(i = 0; i < 3; i++)
162 itable = tables[i];
163 len = tablen[i];
164 for(j = 0; j < len; j++)
165 for(k = 0; k < len; k++)
166 for(l = 0; l < len; l++)
168 *itable++ = base[i][l];
169 *itable++ = base[i][k];
170 *itable++ = base[i][j];
174 for(k = 0; k < 27; k++)
176 double m = mulmul[k];
177 table = mpeg3_muls[k];
178 for(j = 3, i = 0; i < 63; i++, j--)
179 *table++ = m * pow(2.0, (double)j / 3.0);
180 *table++ = 0.0;
182 return 0;
185 static int init_layer3(mpeg3_layer_t *audio)
187 int i, j, k, l;
188 int down_sample_sblimit = 32;
190 audio->mp3_block[0][0][0] = 0;
191 audio->mp3_blc[0] = 0;
192 audio->mp3_blc[1] = 0;
194 for(i = -256; i < 118 + 4; i++)
195 mpeg3_gainpow2[i + 256] = pow((double)2.0, -0.25 * (double)(i + 210));
197 for(i = 0; i < 8207; i++)
198 mpeg3_ispow[i] = pow((double)i, (double)4.0 / 3.0);
200 for(i = 0; i < 8; i++)
202 static double Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
203 double sq = sqrt(1.0+Ci[i]*Ci[i]);
204 mpeg3_aa_cs[i] = 1.0/sq;
205 mpeg3_aa_ca[i] = Ci[i]/sq;
208 for(i = 0; i < 18; i++)
210 mpeg3_win[0][i] = mpeg3_win[1][i] = 0.5 * sin( M_PI / 72.0 * (double)(2 * (i + 0) + 1) ) / cos (M_PI * (double)(2 * (i + 0) + 19) / 72.0);
211 mpeg3_win[0][i+18] = mpeg3_win[3][i+18] = 0.5 * sin( M_PI / 72.0 * (double)(2 * (i + 18) + 1) ) / cos (M_PI * (double)(2 * (i + 18) + 19) / 72.0);
213 for(i = 0; i < 6; i++)
215 mpeg3_win[1][i + 18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
216 mpeg3_win[3][i + 12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 );
217 mpeg3_win[1][i + 24] = 0.5 * sin( M_PI / 24.0 * (double)(2 * i + 13) ) / cos (M_PI * (double)(2 * (i + 24)+ 19) / 72.0 );
218 mpeg3_win[1][i + 30] = mpeg3_win[3][i] = 0.0;
219 mpeg3_win[3][i + 6 ] = 0.5 * sin( M_PI / 24.0 * (double)(2 * i + 1) ) / cos (M_PI * (double)(2 * (i + 6 )+ 19) / 72.0 );
222 for(i = 0; i < 9; i++)
223 mpeg3_COS9[i] = cos(M_PI / 18.0 * (double)i);
225 for(i = 0; i < 9; i++)
226 mpeg3_tfcos36[i] = 0.5 / cos (M_PI * (double) (i*2+1) / 36.0);
227 for(i = 0; i < 3; i++)
228 mpeg3_tfcos12[i] = 0.5 / cos (M_PI * (double) (i*2+1) / 12.0);
230 mpeg3_COS6_1 = cos( M_PI / 6.0 * (double) 1);
231 mpeg3_COS6_2 = cos( M_PI / 6.0 * (double) 2);
233 mpeg3_cos9[0] = cos(1.0 * M_PI / 9.0);
234 mpeg3_cos9[1] = cos(5.0 * M_PI / 9.0);
235 mpeg3_cos9[2] = cos(7.0 * M_PI / 9.0);
236 mpeg3_cos18[0] = cos(1.0 * M_PI / 18.0);
237 mpeg3_cos18[1] = cos(11.0 * M_PI / 18.0);
238 mpeg3_cos18[2] = cos(13.0 * M_PI / 18.0);
240 for(i = 0; i < 12; i++)
242 mpeg3_win[2][i] = 0.5 * sin(M_PI / 24.0 * (double) (2 * i + 1)) / cos(M_PI * (double)(2 * i + 7) / 24.0);
243 for(j = 0; j < 6; j++)
244 mpeg3_COS1[i][j] = cos(M_PI / 24.0 * (double) ((2 * i + 7) * (2 * j + 1)));
247 for(j = 0; j < 4; j++)
249 static int len[4] = {36, 36, 12, 36};
250 for(i = 0; i < len[j]; i += 2)
251 mpeg3_win1[j][i] = + mpeg3_win[j][i];
252 for(i = 1; i < len[j]; i += 2)
253 mpeg3_win1[j][i] = - mpeg3_win[j][i];
256 for(i = 0; i < 16; i++)
258 double t = tan( (double) i * M_PI / 12.0 );
259 mpeg3_tan1_1[i] = t / (1.0 + t);
260 mpeg3_tan2_1[i] = 1.0 / (1.0 + t);
261 mpeg3_tan1_2[i] = M_SQRT2 * t / (1.0 + t);
262 mpeg3_tan2_2[i] = M_SQRT2 / (1.0 + t);
264 for(j = 0; j < 2; j++)
266 double base = pow(2.0, -0.25 * (j + 1.0));
267 double p1 = 1.0,p2 = 1.0;
268 if(i > 0)
270 if( i & 1 )
271 p1 = pow(base, (i + 1.0) * 0.5);
272 else
273 p2 = pow(base, i * 0.5);
275 mpeg3_pow1_1[j][i] = p1;
276 mpeg3_pow2_1[j][i] = p2;
277 mpeg3_pow1_2[j][i] = M_SQRT2 * p1;
278 mpeg3_pow2_2[j][i] = M_SQRT2 * p2;
282 for(j = 0; j < 9; j++)
284 struct mpeg3_bandInfoStruct *bi = &mpeg3_bandInfo[j];
285 int *mp;
286 int cb,lwin;
287 int *bdf;
289 mp = mpeg3_map[j][0] = mpeg3_mapbuf0[j];
290 bdf = bi->longDiff;
291 for(i = 0, cb = 0; cb < 8; cb++, i += *bdf++)
293 *mp++ = (*bdf) >> 1;
294 *mp++ = i;
295 *mp++ = 3;
296 *mp++ = cb;
298 bdf = bi->shortDiff + 3;
299 for(cb = 3; cb < 13; cb++)
301 int l = (*bdf++) >> 1;
302 for(lwin = 0; lwin < 3; lwin++)
304 *mp++ = l;
305 *mp++ = i + lwin;
306 *mp++ = lwin;
307 *mp++ = cb;
309 i += 6 * l;
311 mpeg3_mapend[j][0] = mp;
313 mp = mpeg3_map[j][1] = mpeg3_mapbuf1[j];
314 bdf = bi->shortDiff+0;
315 for(i = 0,cb = 0; cb < 13; cb++)
317 int l = (*bdf++) >> 1;
318 for(lwin = 0; lwin < 3; lwin++)
320 *mp++ = l;
321 *mp++ = i + lwin;
322 *mp++ = lwin;
323 *mp++ = cb;
325 i += 6 * l;
327 mpeg3_mapend[j][1] = mp;
329 mp = mpeg3_map[j][2] = mpeg3_mapbuf2[j];
330 bdf = bi->longDiff;
331 for(cb = 0; cb < 22 ; cb++)
333 *mp++ = (*bdf++) >> 1;
334 *mp++ = cb;
336 mpeg3_mapend[j][2] = mp;
339 for(j = 0; j < 9; j++)
341 for(i = 0; i < 23; i++)
343 mpeg3_longLimit[j][i] = (mpeg3_bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
344 if(mpeg3_longLimit[j][i] > (down_sample_sblimit))
345 mpeg3_longLimit[j][i] = down_sample_sblimit;
347 for(i = 0; i < 14; i++)
349 mpeg3_shortLimit[j][i] = (mpeg3_bandInfo[j].shortIdx[i] - 1) / 18 + 1;
350 if(mpeg3_shortLimit[j][i] > (down_sample_sblimit) )
351 mpeg3_shortLimit[j][i] = down_sample_sblimit;
355 for(i = 0; i < 5; i++)
357 for(j = 0; j < 6; j++)
359 for(k = 0; k < 6; k++)
361 int n = k + j * 6 + i * 36;
362 mpeg3_i_slen2[n] = i | (j << 3) | (k << 6) | (3 << 12);
366 for(i = 0; i < 4; i++)
368 for(j = 0; j < 4; j++)
370 for(k = 0; k < 4; k++)
372 int n = k + j * 4 + i * 16;
373 mpeg3_i_slen2[n+180] = i | (j << 3) | (k << 6) | (4 << 12);
377 for(i = 0; i < 4; i++)
379 for(j = 0; j < 3; j++)
381 int n = j + i * 3;
382 mpeg3_i_slen2[n + 244] = i | (j << 3) | (5 << 12);
383 mpeg3_n_slen2[n + 500] = i | (j << 3) | (2 << 12) | (1 << 15);
387 for(i = 0; i < 5; i++)
389 for(j = 0; j < 5; j++)
391 for(k = 0; k < 4; k++)
393 for(l = 0; l < 4; l++)
395 int n = l + k * 4 + j * 16 + i * 80;
396 mpeg3_n_slen2[n] = i | (j << 3) | ( k << 6) | (l << 9) | (0 << 12);
401 for(i = 0; i < 5; i++)
403 for(j = 0; j < 5; j++)
405 for(k = 0; k < 4; k++)
407 int n = k + j * 4 + i * 20;
408 mpeg3_n_slen2[n + 400] = i | (j << 3) | (k << 6) | (1 << 12);
412 return 0;
415 int mpeg3_new_decode_tables(mpeg3_layer_t *audio)
417 int i, j, k, kr, divv;
418 float *costab;
419 int idx;
420 long scaleval = 1;
423 for(i = 0; i < 5; i++)
425 kr = 0x10 >> i;
426 divv = 0x40 >> i;
427 costab = mpeg3_pnts[i];
428 for(k = 0; k < kr; k++)
429 costab[k] = 1.0 / (2.0 * cos(M_PI * ((double)k * 2.0 + 1.0) / (double)divv));
431 #ifdef USE_3DNOW
432 for(k = 0; k < kr; k++)
433 costab[k + kr] = -costab[k];
434 #endif
438 idx = 0;
439 scaleval = -scaleval;
440 for(i = 0, j = 0; i < 256; i++, j++,idx += 32)
442 if(idx < 512 + 16)
443 mpeg3_decwin[idx+16] = mpeg3_decwin[idx] = (double)mpeg3_intwinbase[j] / 65536.0 * (double)scaleval;
445 if(i % 32 == 31)
446 idx -= 1023;
447 if(i % 64 == 63)
448 scaleval = -scaleval;
451 for( ; i < 512; i++, j--, idx += 32)
453 if(idx < 512 + 16)
454 mpeg3_decwin[idx + 16] = mpeg3_decwin[idx] = (double)mpeg3_intwinbase[j] / 65536.0 * (double)scaleval;
456 if(i % 32 == 31)
457 idx -= 1023;
458 if(i % 64 == 63)
459 scaleval = -scaleval;
462 #ifdef USE_3DNOW
463 if(!param.down_sample)
465 for(i = 0; i < 512 + 32; i++)
467 mpeg3_decwin[512 + 31 - i] *= 65536.0; /* allows faster clipping in 3dnow code */
468 mpeg3_decwin[512 + 32 + i] = mpeg3_decwin[512 + 31 - i];
471 #endif
473 /* Initialize AC3 */
474 // mpeg3audio_imdct_init(audio);
475 /* Initialize MPEG */
476 init_layer2(audio); /* inits also shared tables with layer1 */
477 init_layer3(audio);
478 return 0;