1 #include "../libzmpeg3.h"
5 int zaudio_decoder_layer_t::
6 tabsel_123[2][3][16] = {
7 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
8 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
9 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
11 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
12 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
13 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
16 long zaudio_decoder_layer_t::
18 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000
22 float zaudio_decoder_layer_t:: decwin[2 * (512 + 32)];
23 float zaudio_decoder_layer_t:: cos64[32];
24 float zaudio_decoder_layer_t:: cos32[16];
25 float zaudio_decoder_layer_t:: cos16[8];
26 float zaudio_decoder_layer_t:: cos8[4];
27 float zaudio_decoder_layer_t:: cos4[2];
29 float zaudio_decoder_layer_t:: decwin[512 + 32];
30 float zaudio_decoder_layer_t:: cos64[16];
31 float zaudio_decoder_layer_t:: cos32[8];
32 float zaudio_decoder_layer_t:: cos16[4];
33 float zaudio_decoder_layer_t:: cos8[2];
34 float zaudio_decoder_layer_t:: cos4[1];
37 float *zaudio_decoder_layer_t::
39 zaudio_decoder_layer_t:: cos64,
40 zaudio_decoder_layer_t:: cos32,
41 zaudio_decoder_layer_t:: cos16,
42 zaudio_decoder_layer_t:: cos8,
43 zaudio_decoder_layer_t:: cos4
46 int zaudio_decoder_layer_t:: grp_3tab[32 * 3] = { 0, }; /* used: 27 */
47 int zaudio_decoder_layer_t:: grp_5tab[128 * 3] = { 0, }; /* used: 125 */
48 int zaudio_decoder_layer_t:: grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
49 float zaudio_decoder_layer_t:: muls[27][64]; /* also used by layer 1 */
50 float zaudio_decoder_layer_t:: gainpow2[256 + 118 + 4];
51 float zaudio_decoder_layer_t:: ispow[8207];
52 float zaudio_decoder_layer_t:: aa_ca[8];
53 float zaudio_decoder_layer_t:: aa_cs[8];
54 float zaudio_decoder_layer_t:: win[4][36];
55 float zaudio_decoder_layer_t:: win1[4][36];
56 float zaudio_decoder_layer_t:: COS1[12][6];
57 float zaudio_decoder_layer_t:: COS9[9];
58 float zaudio_decoder_layer_t:: COS6_1;
59 float zaudio_decoder_layer_t:: COS6_2;
60 float zaudio_decoder_layer_t:: tfcos36[9];
61 float zaudio_decoder_layer_t:: tfcos12[3];
62 float zaudio_decoder_layer_t:: cos9[3];
63 float zaudio_decoder_layer_t:: cos18[3];
64 float zaudio_decoder_layer_t:: tan1_1[16];
65 float zaudio_decoder_layer_t:: tan2_1[16];
66 float zaudio_decoder_layer_t:: tan1_2[16];
67 float zaudio_decoder_layer_t:: tan2_2[16];
68 float zaudio_decoder_layer_t:: pow1_1[2][16];
69 float zaudio_decoder_layer_t:: pow2_1[2][16];
70 float zaudio_decoder_layer_t:: pow1_2[2][16];
71 float zaudio_decoder_layer_t:: pow2_2[2][16];
73 long zaudio_decoder_layer_t::
75 0, -1, -1, -1, -1, -1, -1, -2, -2, -2,
76 -2, -3, -3, -4, -4, -5, -5, -6, -7, -7,
77 -8, -9, -10, -11, -13, -14, -16, -17, -19, -21,
78 -24, -26, -29, -31, -35, -38, -41, -45, -49, -53,
79 -58, -63, -68, -73, -79, -85, -91, -97, -104, -111,
80 -117, -125, -132, -139, -147, -154, -161, -169, -176, -183,
81 -190, -196, -202, -208, -213, -218, -222, -225, -227, -228,
82 -228, -227, -224, -221, -215, -208, -200, -189, -177, -163,
83 -146, -127, -106, -83, -57, -29, 2, 36, 72, 111,
84 153, 197, 244, 294, 347, 401, 459, 519, 581, 645,
85 711, 779, 848, 919, 991, 1064, 1137, 1210, 1283, 1356,
86 1428, 1498, 1567, 1634, 1698, 1759, 1817, 1870, 1919, 1962,
87 2001, 2032, 2057, 2075, 2085, 2087, 2080, 2063, 2037, 2000,
88 1952, 1893, 1822, 1739, 1644, 1535, 1414, 1280, 1131, 970,
89 794, 605, 402, 185, -45, -288, -545, -814, -1095, -1388,
90 -1692, -2006, -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788,
91 -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597, -7910, -8209,
92 -8491, -8755, -8998, -9219, -9416, -9585, -9727, -9838, -9916, -9959,
93 -9966, -9935, -9863, -9750, -9592, -9389, -9139, -8840, -8492, -8092,
94 -7640, -7134, -6574, -5959, -5288, -4561, -3776, -2935, -2037, -1082,
95 -70, 998, 2122, 3300, 4533, 5818, 7154, 8540, 9975, 11455,
96 12980, 14548, 16155, 17799, 19478, 21189, 22929, 24694, 26482, 28289,
97 30112, 31947, 33791, 35640, 37489, 39336, 41176, 43006, 44821, 46617,
98 48390, 50137, 51853, 53534, 55178, 56778, 58333, 59838, 61289, 62684,
99 64019, 65290, 66494, 67629, 68692, 69679, 70590, 71420, 72169, 72835,
100 73415, 73908, 74313, 74630, 74856, 74992, 75038 };
102 int zaudio_decoder_layer_t::
104 int zaudio_decoder_layer_t::
107 struct zaudio_decoder_layer_t::
108 bandInfoStruct zaudio_decoder_layer_t::
113 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
114 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
115 {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},
116 {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
118 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
119 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
120 {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},
121 {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
123 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
124 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
125 {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} ,
126 {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
129 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
130 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
131 {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} ,
132 {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
134 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
135 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
136 {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} ,
137 {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
139 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
140 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
141 {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},
142 {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
144 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
145 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
146 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
147 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
148 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
149 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
150 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
151 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
152 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
153 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
154 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
155 {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
158 int zaudio_decoder_layer_t:: mapbuf0[9][152];
159 int zaudio_decoder_layer_t:: mapbuf1[9][156];
160 int zaudio_decoder_layer_t:: mapbuf2[9][44];
161 int *zaudio_decoder_layer_t:: map[9][3];
162 int *zaudio_decoder_layer_t:: mapend[9][3];
164 unsigned int zaudio_decoder_layer_t:: n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
165 unsigned int zaudio_decoder_layer_t:: i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
167 int zaudio_decoder_layer_t::
170 static double mulmul[27] = {
171 0.0 , -2.0/3.0 , 2.0/3.0 ,
172 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
173 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
174 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
175 -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
176 -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0
178 static int base[3][9] = {
180 { 17, 18, 0 , 19, 20 , } ,
181 { 21, 1, 22, 23, 0, 24, 25, 2, 26 }
183 static int tablen[3] = { 3, 5, 9 };
184 static int *itable, *tables[3] = { grp_3tab, grp_5tab, grp_9tab };
188 for(i = 0; i < 3; i++)
192 for(j = 0; j < len; j++)
193 for(k = 0; k < len; k++)
194 for(l = 0; l < len; l++)
196 *itable++ = base[i][l];
197 *itable++ = base[i][k];
198 *itable++ = base[i][j];
202 for(k = 0; k < 27; k++)
204 double m = mulmul[k];
206 for(j = 3, i = 0; i < 63; i++, j--)
207 *table++ = m * pow(2.0, (double)j / 3.0);
213 int zaudio_decoder_layer_t::
217 int down_sample_sblimit = 32;
219 mp3_block[0][0][0] = 0;
223 for(i = -256; i < 118 + 4; i++)
224 gainpow2[i + 256] = pow((double)2.0, -0.25 * (double)(i + 210));
226 for(i = 0; i < 8207; i++)
227 ispow[i] = pow((double)i, (double)4.0 / 3.0);
229 for(i = 0; i < 8; i++)
231 static double Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
232 double sq = sqrt(1.0+Ci[i]*Ci[i]);
237 for( i=0; i<18; ++i ) {
239 win[1][i] = 0.5 * sin( M_PI/72.0 * (double)(2*(i+ 0)+1)) /
240 cos( M_PI * (double)(2*(i+ 0)+19) / 72.0);
242 win[3][i + 18] = 0.5 * sin( M_PI/72.0 * (double)(2*(i+18)+1)) /
243 cos( M_PI * (double)(2*(i+18)+19) / 72.0);
245 for( i=0; i<6; ++i ) {
246 win[1][i + 18] = 0.5 / cos( M_PI * (double)(2*(i+18)+19) / 72.0);
247 win[3][i + 12] = 0.5 / cos( M_PI * (double)(2*(i+12)+19) / 72.0);
248 win[1][i + 24] = 0.5 * sin( M_PI/24.0 * (double)(2*(i+ 0)+13)) /
249 cos( M_PI * (double)(2*(i+24)+19) / 72.0);
250 win[1][i + 30] = win[3][i] = 0.0;
251 win[3][i + 6 ] = 0.5 * sin( M_PI/24.0 * (double)(2*(i+ 0)+1)) /
252 cos( M_PI * (double)(2*(i+ 6)+19) / 72.0);
255 for(i = 0; i < 9; i++)
256 COS9[i] = cos(M_PI / 18.0 * (double)i);
258 for(i = 0; i < 9; i++)
259 tfcos36[i] = 0.5 / cos (M_PI * (double) (i*2+1) / 36.0);
260 for(i = 0; i < 3; i++)
261 tfcos12[i] = 0.5 / cos (M_PI * (double) (i*2+1) / 12.0);
263 COS6_1 = cos( M_PI / 6.0 * (double) 1);
264 COS6_2 = cos( M_PI / 6.0 * (double) 2);
266 cos9[0] = cos(1.0 * M_PI / 9.0);
267 cos9[1] = cos(5.0 * M_PI / 9.0);
268 cos9[2] = cos(7.0 * M_PI / 9.0);
269 cos18[0] = cos(1.0 * M_PI / 18.0);
270 cos18[1] = cos(11.0 * M_PI / 18.0);
271 cos18[2] = cos(13.0 * M_PI / 18.0);
273 for( i=0; i < 12; ++i ) {
274 win[2][i] = 0.5 * sin(M_PI / 24.0 * (double)(2*i + 1)) /
275 cos(M_PI * (double)(2*i + 7) / 24.0);
276 for( j=0; j < 6; ++j )
277 COS1[i][j] = cos(M_PI / 24.0 * (double)((2*i + 7) * (2*j + 1)));
280 for( j=0; j < 4; j++) {
281 static int len[4] = {36, 36, 12, 36};
282 for(i = 0; i < len[j]; i+=2 ) win1[j][i] = win[j][i];
283 for(i = 1; i < len[j]; i+=2 ) win1[j][i] = -win[j][i];
286 for( i=0; i < 16; ++i ) {
287 double t = tan( (double) i * M_PI / 12.0 );
288 tan1_1[i] = t / (1.0 + t);
289 tan2_1[i] = 1.0 / (1.0 + t);
290 tan1_2[i] = M_SQRT2 * t / (1.0 + t);
291 tan2_2[i] = M_SQRT2 / (1.0 + t);
293 for( j=0; j < 2; ++j ) {
294 double base = pow(2.0, -0.25 * (j + 1.0));
295 double p1 = 1.0,p2 = 1.0;
298 p1 = pow(base, (i + 1.0) * 0.5);
300 p2 = pow(base, i * 0.5);
304 pow1_2[j][i] = M_SQRT2 * p1;
305 pow2_2[j][i] = M_SQRT2 * p2;
309 for( j=0; j < 9; ++j ) {
310 bandInfoStruct *bi = &bandInfo[j];
312 int *mp = map[j][0] = mapbuf0[j];
313 int *bdf = bi->longDiff;
314 for( i=0, cb=0; cb < 8; ++cb, i+=*bdf++ ) {
320 bdf = bi->shortDiff + 3;
321 for( cb=3; cb < 13; ++cb ) {
322 int l = (*bdf++) >> 1;
323 for( lwin=0; lwin < 3; ++lwin ) {
333 mp = map[j][1] = mapbuf1[j];
334 bdf = bi->shortDiff+0;
335 for( i=0,cb=0; cb < 13; ++cb ) {
336 int l = (*bdf++) >> 1;
337 for( lwin=0; lwin < 3; ++lwin ) {
347 mp = map[j][2] = mapbuf2[j];
349 for( cb=0; cb < 22 ; ++cb ) {
350 *mp++ = (*bdf++) >> 1;
356 for( j=0; j < 9; ++j ) {
357 for( i=0; i < 23; ++i ) {
358 longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
359 if( longLimit[j][i] > down_sample_sblimit )
360 longLimit[j][i] = down_sample_sblimit;
362 for( i=0; i < 14; ++i ) {
363 shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
364 if( shortLimit[j][i] > down_sample_sblimit )
365 shortLimit[j][i] = down_sample_sblimit;
369 for( i=0; i < 5; ++i ) {
370 for( j=0; j < 6; ++j ) {
371 for( k=0; k < 6; ++k ) {
372 int n = k + j * 6 + i * 36;
373 i_slen2[n] = i | (j << 3) | (k << 6) | (3 << 12);
377 for( i=0; i < 4; ++i ) {
378 for( j=0; j < 4; ++j ) {
379 for( k=0; k < 4; ++k ) {
380 int n = k + j * 4 + i * 16;
381 i_slen2[n+180] = i | (j << 3) | (k << 6) | (4 << 12);
385 for( i=0; i < 4; ++i ) {
386 for( j=0; j < 3; ++j ) {
388 i_slen2[n + 244] = i | (j << 3) | (5 << 12);
389 n_slen2[n + 500] = i | (j << 3) | (2 << 12) | (1 << 15);
392 for( i=0; i < 5; ++i ) {
393 for( j=0; j < 5; ++j ) {
394 for( k=0; k < 4; ++k ) {
395 for( l=0; l < 4; ++l ) {
396 int n = l + k * 4 + j * 16 + i * 80;
397 n_slen2[n] = i | (j << 3) | ( k << 6) | (l << 9) | (0 << 12);
402 for( i=0; i < 5; ++i ) {
403 for( j=0; j < 5; ++j ) {
404 for( k=0; k < 4; ++k ) {
405 int n = k + j * 4 + i * 20;
406 n_slen2[n + 400] = i | (j << 3) | (k << 6) | (1 << 12);
413 int zaudio_decoder_layer_t::
416 static int inited = 0;
417 if( inited ) return 0;
419 int i, j, k, kr, divv;
424 for( i=0; i < 5; ++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));
432 for( k=0; k < kr; ++k )
433 costab[k + kr] = -costab[k];
438 scaleval = -scaleval;
439 for( i=0, j=0; i < 256; ++i, ++j, idx+=32 ) {
440 if( idx < 512 + 16 ) decwin[idx+16] = decwin[idx] =
441 (double)intwinbase[j] / 65536.0 * (double)scaleval;
442 if( i % 32 == 31 ) idx -= 1023;
443 if( i % 64 == 63 ) scaleval = -scaleval;
446 for( ; i < 512; ++i, --j, idx+=32 ) {
447 if( idx < 512 + 16 ) decwin[idx + 16] = decwin[idx] =
448 (double)intwinbase[j] / 65536.0 * (double)scaleval;
449 if( i % 32 == 31 ) idx -= 1023;
450 if( i % 64 == 63 ) scaleval = -scaleval;
454 if( !param.down_sample ) {
455 for( i=0; i < 512 + 32; ++i ) {
456 decwin[512 + 31 - i] *= 65536.0; /* allows faster clipping in 3dnow code */
457 decwin[512 + 32 + i] = decwin[512 + 31 - i];
464 /* Initialize MPEG */
465 /* inits also shared tables with layer1 */