Merge svn changes up to r29412
[mplayer.git] / mp3lib / layer3.c
blobb89bc9844dc882e50087d508559c56b89746dc7c
1 /*
2 * Modified for use with MPlayer, for details see the changelog at
3 * http://svn.mplayerhq.hu/mplayer/trunk/
4 * $Id$
5 */
7 /*
8 * Mpeg Layer-3 audio decoder
9 * --------------------------
10 * copyright (c) 1995-1999 by Michael Hipp.
11 * All rights reserved. See also 'README'
13 * Optimize-TODO: put short bands into the band-field without the stride
14 * of 3 reals
15 * Length-optimze: unify long and short band code where it is possible
18 #if 0
19 #define L3_DEBUG 1
20 #endif
22 #if 0
23 #define CUT_HF
24 #endif
26 # define REAL_MUL(x, y) ((x) * (y))
28 static real ispow[8207];
29 static real aa_ca[8],aa_cs[8];
30 static real COS1[12][6];
31 static real win[4][36];
32 static real win1[4][36];
33 static real gainpow2[256+118+4];
35 /* non static for external 3dnow functions */
36 real COS9[9];
37 static real COS6_1,COS6_2;
38 real tfcos36[9];
40 static real tfcos12[3];
41 #define NEW_DCT9
42 #ifdef NEW_DCT9
43 static real cos9[3],cos18[3];
44 #endif
46 struct bandInfoStruct {
47 uint16_t longIdx[23];
48 uint8_t longDiff[22];
49 uint16_t shortIdx[14];
50 uint8_t shortDiff[13];
53 static int longLimit[9][23];
54 static int shortLimit[9][14];
56 static const struct bandInfoStruct bandInfo[9] = {
58 /* MPEG 1.0 */
59 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
60 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
61 {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},
62 {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
64 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
65 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
66 {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},
67 {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
69 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
70 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
71 {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} ,
72 {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
74 /* MPEG 2.0 */
75 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
76 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
77 {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} ,
78 {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
79 /* changed 19th value fropm 330 to 332 */
80 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576},
81 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 } ,
82 {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} ,
83 {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
85 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
86 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
87 {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},
88 {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
89 /* MPEG 2.5 */
90 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
91 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
92 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
93 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
94 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
95 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
96 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
97 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
98 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
99 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
100 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
101 {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
104 static int mapbuf0[9][152];
105 static int mapbuf1[9][156];
106 static int mapbuf2[9][44];
107 static int *map[9][3];
108 static int *mapend[9][3];
110 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
111 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
113 static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
114 static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
117 * init tables for layer-3
119 static void init_layer3(int down_sample_sblimit)
121 int i,j,k,l;
123 for(i=-256;i<118+4;i++)
125 if(_has_mmx)
126 gainpow2[i+256] = 16384.0 * pow((double)2.0,-0.25 * (double) (i+210) );
127 else
128 gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
130 for(i=0;i<8207;i++)
131 ispow[i] = pow((double)i,(double)4.0/3.0);
133 for (i=0;i<8;i++)
135 static const double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
136 double sq=sqrt(1.0+Ci[i]*Ci[i]);
137 aa_cs[i] = 1.0/sq;
138 aa_ca[i] = Ci[i]/sq;
141 for(i=0;i<18;i++)
143 win[0][i] = 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 );
144 win[0][i+18] = 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 );
146 for(i=0;i<6;i++)
148 win[1][i+18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
149 win[3][i+12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 );
150 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 );
151 win[1][i+30] = win[3][i] = 0.0;
152 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 );
155 for(i=0;i<9;i++)
156 COS9[i] = cos( M_PI / 18.0 * (double) i);
158 for(i=0;i<9;i++)
159 tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 );
160 for(i=0;i<3;i++)
161 tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 );
163 COS6_1 = cos( M_PI / 6.0 * (double) 1);
164 COS6_2 = cos( M_PI / 6.0 * (double) 2);
166 #ifdef NEW_DCT9
167 cos9[0] = cos(1.0*M_PI/9.0);
168 cos9[1] = cos(5.0*M_PI/9.0);
169 cos9[2] = cos(7.0*M_PI/9.0);
170 cos18[0] = cos(1.0*M_PI/18.0);
171 cos18[1] = cos(11.0*M_PI/18.0);
172 cos18[2] = cos(13.0*M_PI/18.0);
173 #endif
175 for(i=0;i<12;i++)
177 win[2][i] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 );
178 for(j=0;j<6;j++)
179 COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) );
182 for(j=0;j<4;j++) {
183 static const int len[4] = { 36,36,12,36 };
184 for(i=0;i<len[j];i+=2)
185 win1[j][i] = + win[j][i];
186 for(i=1;i<len[j];i+=2)
187 win1[j][i] = - win[j][i];
190 for(i=0;i<16;i++)
192 double t = tan( (double) i * M_PI / 12.0 );
193 tan1_1[i] = t / (1.0+t);
194 tan2_1[i] = 1.0 / (1.0 + t);
195 tan1_2[i] = M_SQRT2 * t / (1.0+t);
196 tan2_2[i] = M_SQRT2 / (1.0 + t);
198 for(j=0;j<2;j++) {
199 double base = pow(2.0,-0.25*(j+1.0));
200 double p1=1.0,p2=1.0;
201 if(i > 0) {
202 if( i & 1 )
203 p1 = pow(base,(i+1.0)*0.5);
204 else
205 p2 = pow(base,i*0.5);
207 pow1_1[j][i] = p1;
208 pow2_1[j][i] = p2;
209 pow1_2[j][i] = M_SQRT2 * p1;
210 pow2_2[j][i] = M_SQRT2 * p2;
214 for(j=0;j<9;j++)
216 const struct bandInfoStruct *bi = &bandInfo[j];
217 int *mp;
218 int cb,lwin;
219 const uint8_t *bdf;
221 mp = map[j][0] = mapbuf0[j];
222 bdf = bi->longDiff;
223 for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
224 *mp++ = (*bdf) >> 1;
225 *mp++ = i;
226 *mp++ = 3;
227 *mp++ = cb;
229 bdf = bi->shortDiff+3;
230 for(cb=3;cb<13;cb++) {
231 int l = (*bdf++) >> 1;
232 for(lwin=0;lwin<3;lwin++) {
233 *mp++ = l;
234 *mp++ = i + lwin;
235 *mp++ = lwin;
236 *mp++ = cb;
238 i += 6*l;
240 mapend[j][0] = mp;
242 mp = map[j][1] = mapbuf1[j];
243 bdf = bi->shortDiff+0;
244 for(i=0,cb=0;cb<13;cb++) {
245 int l = (*bdf++) >> 1;
246 for(lwin=0;lwin<3;lwin++) {
247 *mp++ = l;
248 *mp++ = i + lwin;
249 *mp++ = lwin;
250 *mp++ = cb;
252 i += 6*l;
254 mapend[j][1] = mp;
256 mp = map[j][2] = mapbuf2[j];
257 bdf = bi->longDiff;
258 for(cb = 0; cb < 22 ; cb++) {
259 *mp++ = (*bdf++) >> 1;
260 *mp++ = cb;
262 mapend[j][2] = mp;
266 for(j=0;j<9;j++) {
267 for(i=0;i<23;i++) {
268 longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
269 if(longLimit[j][i] > (down_sample_sblimit) )
270 longLimit[j][i] = down_sample_sblimit;
272 for(i=0;i<14;i++) {
273 shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
274 if(shortLimit[j][i] > (down_sample_sblimit) )
275 shortLimit[j][i] = down_sample_sblimit;
279 for(i=0;i<5;i++) {
280 for(j=0;j<6;j++) {
281 for(k=0;k<6;k++) {
282 int n = k + j * 6 + i * 36;
283 i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
287 for(i=0;i<4;i++) {
288 for(j=0;j<4;j++) {
289 for(k=0;k<4;k++) {
290 int n = k + j * 4 + i * 16;
291 i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
295 for(i=0;i<4;i++) {
296 for(j=0;j<3;j++) {
297 int n = j + i * 3;
298 i_slen2[n+244] = i|(j<<3) | (5<<12);
299 n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
303 for(i=0;i<5;i++) {
304 for(j=0;j<5;j++) {
305 for(k=0;k<4;k++) {
306 for(l=0;l<4;l++) {
307 int n = l + k * 4 + j * 16 + i * 80;
308 n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
313 for(i=0;i<5;i++) {
314 for(j=0;j<5;j++) {
315 for(k=0;k<4;k++) {
316 int n = k + j * 4 + i * 20;
317 n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
324 * read additional side information (for MPEG 1 and MPEG 2)
326 static int III_get_side_info(struct III_sideinfo *si,int stereo,
327 int ms_stereo,int sfreq,int single,int lsf)
329 int ch, gr;
330 int powdiff = (single == 3) ? 4 : 0;
332 static const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } };
333 const int *tab = tabs[lsf];
335 si->main_data_begin = getbits(tab[1]);
336 if (stereo == 1)
337 si->private_bits = getbits_fast(tab[2]);
338 else
339 si->private_bits = getbits_fast(tab[3]);
341 if(!lsf) {
342 for (ch=0; ch<stereo; ch++) {
343 si->ch[ch].gr[0].scfsi = -1;
344 si->ch[ch].gr[1].scfsi = getbits_fast(4);
348 for (gr=0; gr<tab[0]; gr++) {
349 for (ch=0; ch<stereo; ch++) {
350 register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
352 gr_info->part2_3_length = getbits(12);
353 gr_info->big_values = getbits(9);
354 if(gr_info->big_values > 288) {
355 fprintf(stderr,"big_values too large!\n");
356 gr_info->big_values = 288;
358 gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
359 if(ms_stereo)
360 gr_info->pow2gain += 2;
361 gr_info->scalefac_compress = getbits(tab[4]);
363 if(get1bit()) { /* window switch flag */
364 int i;
365 #ifdef L3_DEBUG
366 if(2*gr_info->big_values > bandInfo[sfreq].shortIdx[12])
367 fprintf(stderr,"L3: BigValues too large, doesn't make sense %d %d\n",2*gr_info->big_values,bandInfo[sfreq].shortIdx[12]);
368 #endif
370 gr_info->block_type = getbits_fast(2);
371 gr_info->mixed_block_flag = get1bit();
372 gr_info->table_select[0] = getbits_fast(5);
373 gr_info->table_select[1] = getbits_fast(5);
375 * table_select[2] not needed, because there is no region2,
376 * but to satisfy some verifications tools we set it either.
378 gr_info->table_select[2] = 0;
379 for(i=0;i<3;i++)
380 gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
382 if(gr_info->block_type == 0) {
383 fprintf(stderr,"Blocktype == 0 and window-switching == 1 not allowed.\n");
384 return 0;
387 /* region_count/start parameters are implicit in this case. */
388 if(!lsf || gr_info->block_type == 2)
389 gr_info->region1start = 36>>1;
390 else {
391 /* check this again for 2.5 and sfreq=8 */
392 if(sfreq == 8)
393 gr_info->region1start = 108>>1;
394 else
395 gr_info->region1start = 54>>1;
397 gr_info->region2start = 576>>1;
399 else {
400 int i,r0c,r1c;
401 #ifdef L3_DEBUG
402 if(2*gr_info->big_values > bandInfo[sfreq].longIdx[21])
403 fprintf(stderr,"L3: BigValues too large, doesn't make sense %d %d\n",2*gr_info->big_values,bandInfo[sfreq].longIdx[21]);
404 #endif
405 for (i=0; i<3; i++)
406 gr_info->table_select[i] = getbits_fast(5);
407 r0c = getbits_fast(4);
408 r1c = getbits_fast(3);
409 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
410 if(r0c + r1c + 2 > 22)
411 gr_info->region2start = 576>>1;
412 else
413 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
414 gr_info->block_type = 0;
415 gr_info->mixed_block_flag = 0;
417 if(!lsf)
418 gr_info->preflag = get1bit();
419 gr_info->scalefac_scale = get1bit();
420 gr_info->count1table_select = get1bit();
424 return !0;
428 * read scalefactors
430 static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
432 static const unsigned char slen[2][16] = {
433 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
434 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
436 int numbits;
437 int num0 = slen[0][gr_info->scalefac_compress];
438 int num1 = slen[1][gr_info->scalefac_compress];
440 if (gr_info->block_type == 2) {
441 int i=18;
442 numbits = (num0 + num1) * 18;
444 if (gr_info->mixed_block_flag) {
445 for (i=8;i;i--)
446 *scf++ = getbits_fast(num0);
447 i = 9;
448 numbits -= num0; /* num0 * 17 + num1 * 18 */
451 for (;i;i--)
452 *scf++ = getbits_fast(num0);
453 for (i = 18; i; i--)
454 *scf++ = getbits_fast(num1);
455 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
457 else {
458 int i;
459 int scfsi = gr_info->scfsi;
461 if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
462 for(i=11;i;i--)
463 *scf++ = getbits_fast(num0);
464 for(i=10;i;i--)
465 *scf++ = getbits_fast(num1);
466 numbits = (num0 + num1) * 10 + num0;
467 *scf++ = 0;
469 else {
470 numbits = 0;
471 if(!(scfsi & 0x8)) {
472 for (i=0;i<6;i++)
473 *scf++ = getbits_fast(num0);
474 numbits += num0 * 6;
476 else {
477 scf += 6;
480 if(!(scfsi & 0x4)) {
481 for (i=0;i<5;i++)
482 *scf++ = getbits_fast(num0);
483 numbits += num0 * 5;
485 else {
486 scf += 5;
489 if(!(scfsi & 0x2)) {
490 for(i=0;i<5;i++)
491 *scf++ = getbits_fast(num1);
492 numbits += num1 * 5;
494 else {
495 scf += 5;
498 if(!(scfsi & 0x1)) {
499 for (i=0;i<5;i++)
500 *scf++ = getbits_fast(num1);
501 numbits += num1 * 5;
503 else {
504 scf += 5;
506 *scf++ = 0; /* no l[21] in original sources */
509 return numbits;
512 static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
514 unsigned char *pnt;
515 int i,j;
516 unsigned int slen;
517 int n = 0;
518 int numbits = 0;
520 static unsigned char stab[3][6][4] = {
521 { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
522 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
523 { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
524 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
525 { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
526 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
528 if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
529 slen = i_slen2[gr_info->scalefac_compress>>1];
530 else
531 slen = n_slen2[gr_info->scalefac_compress];
533 gr_info->preflag = (slen>>15) & 0x1;
535 n = 0;
536 if( gr_info->block_type == 2 ) {
537 n++;
538 if(gr_info->mixed_block_flag) n++;
541 pnt = stab[n][(slen>>12)&0x7];
543 for(i=0;i<4;i++) {
544 int num = slen & 0x7;
545 slen >>= 3;
546 if(num) {
547 for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(num);
548 numbits += pnt[i] * num;
550 else {
551 for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0;
555 n = (n << 1) + 1;
556 for(i=0;i<n;i++) *scf++ = 0;
558 return numbits;
561 static int pretab1[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
562 static int pretab2[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
564 #define getbitoffset() ((-bitindex)&0x7)
565 #define getbyte() (*wordpointer++)
568 * Dequantize samples (includes huffman decoding)
570 /* 24 is enough because tab13 has max. a 19 bit huffvector */
571 #define BITSHIFT ((sizeof(long)-1)*8)
572 #define REFRESH_MASK \
573 while(num < BITSHIFT) { \
574 mask |= ((unsigned long)getbyte())<<(BITSHIFT-num); \
575 num += 8; \
576 part2remain -= 8; }
578 static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
579 struct gr_info_s *gr_info,int sfreq,int part2bits)
581 int shift = 1 + gr_info->scalefac_scale;
582 real *xrpnt = (real *) xr;
583 int l[3],l3;
584 int part2remain = gr_info->part2_3_length - part2bits;
585 int *me;
587 int num=getbitoffset();
588 long mask;
589 /* we must split this, because for num==0 the shift is undefined if you do it in one step */
590 mask = ((unsigned long) getbits(num))<<BITSHIFT;
591 mask <<= 8-num;
592 part2remain -= num;
595 int bv = gr_info->big_values;
596 int region1 = gr_info->region1start;
597 int region2 = gr_info->region2start;
599 l3 = ((576>>1)-bv)>>1;
601 * we may lose the 'odd' bit here !!
602 * check this later again
604 if(bv <= region1) {
605 l[0] = bv; l[1] = l[2] = 0;
607 else {
608 l[0] = region1;
609 if(bv <= region2) {
610 l[1] = bv - l[0]; l[2] = 0;
612 else {
613 l[1] = region2 - l[0]; l[2] = bv - region2;
618 if(gr_info->block_type == 2) {
620 * decoding with short or mixed mode BandIndex table
622 int i,max[4];
623 int step=0,lwin=3,cb=0;
624 register real v = 0.0;
625 register int *m,mc;
627 if(gr_info->mixed_block_flag) {
628 max[3] = -1;
629 max[0] = max[1] = max[2] = 2;
630 m = map[sfreq][0];
631 me = mapend[sfreq][0];
633 else {
634 max[0] = max[1] = max[2] = max[3] = -1;
635 /* max[3] not really needed in this case */
636 m = map[sfreq][1];
637 me = mapend[sfreq][1];
640 mc = 0;
641 for(i=0;i<2;i++) {
642 int lp = l[i];
643 struct newhuff *h = ht+gr_info->table_select[i];
644 for(;lp;lp--,mc--) {
645 register int x,y;
646 if( (!mc) ) {
647 mc = *m++;
648 xrpnt = ((real *) xr) + (*m++);
649 lwin = *m++;
650 cb = *m++;
651 if(lwin == 3) {
652 v = gr_info->pow2gain[(*scf++) << shift];
653 step = 1;
655 else {
656 v = gr_info->full_gain[lwin][(*scf++) << shift];
657 step = 3;
661 register short *val = h->table;
662 REFRESH_MASK;
663 while((y=*val++)<0) {
664 if (mask < 0)
665 val -= y;
666 num--;
667 mask <<= 1;
669 x = y >> 4;
670 y &= 0xf;
672 if(x == 15 && h->linbits) {
673 max[lwin] = cb;
674 REFRESH_MASK;
675 x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
676 num -= h->linbits+1;
677 mask <<= h->linbits;
678 if(mask < 0)
679 *xrpnt = REAL_MUL(-ispow[x], v);
680 else
681 *xrpnt = REAL_MUL(ispow[x], v);
682 mask <<= 1;
684 else if(x) {
685 max[lwin] = cb;
686 if(mask < 0)
687 *xrpnt = REAL_MUL(-ispow[x], v);
688 else
689 *xrpnt = REAL_MUL(ispow[x], v);
690 num--;
691 mask <<= 1;
693 else
694 *xrpnt = 0.0;
695 xrpnt += step;
696 if(y == 15 && h->linbits) {
697 max[lwin] = cb;
698 REFRESH_MASK;
699 y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
700 num -= h->linbits+1;
701 mask <<= h->linbits;
702 if(mask < 0)
703 *xrpnt = REAL_MUL(-ispow[y], v);
704 else
705 *xrpnt = REAL_MUL(ispow[y], v);
706 mask <<= 1;
708 else if(y) {
709 max[lwin] = cb;
710 if(mask < 0)
711 *xrpnt = REAL_MUL(-ispow[y], v);
712 else
713 *xrpnt = REAL_MUL(ispow[y], v);
714 num--;
715 mask <<= 1;
717 else
718 *xrpnt = 0.0;
719 xrpnt += step;
723 for(;l3 && (part2remain+num > 0);l3--) {
724 struct newhuff *h = htc+gr_info->count1table_select;
725 register short *val = h->table,a;
727 REFRESH_MASK;
728 while((a=*val++)<0) {
729 if (mask < 0)
730 val -= a;
731 num--;
732 mask <<= 1;
734 if(part2remain+num <= 0) {
735 num -= part2remain+num;
736 break;
739 for(i=0;i<4;i++) {
740 if(!(i & 1)) {
741 if(!mc) {
742 mc = *m++;
743 xrpnt = ((real *) xr) + (*m++);
744 lwin = *m++;
745 cb = *m++;
746 if(lwin == 3) {
747 v = gr_info->pow2gain[(*scf++) << shift];
748 step = 1;
750 else {
751 v = gr_info->full_gain[lwin][(*scf++) << shift];
752 step = 3;
755 mc--;
757 if( (a & (0x8>>i)) ) {
758 max[lwin] = cb;
759 if(part2remain+num <= 0) {
760 break;
762 if(mask < 0)
763 *xrpnt = -v;
764 else
765 *xrpnt = v;
766 num--;
767 mask <<= 1;
769 else
770 *xrpnt = 0.0;
771 xrpnt += step;
775 if(lwin < 3) { /* short band? */
776 while(1) {
777 for(;mc > 0;mc--) {
778 *xrpnt = 0.0; xrpnt += 3; /* short band -> step=3 */
779 *xrpnt = 0.0; xrpnt += 3;
781 if(m >= me)
782 break;
783 mc = *m++;
784 xrpnt = ((real *) xr) + *m++;
785 if(*m++ == 0)
786 break; /* optimize: field will be set to zero at the end of the function */
787 m++; /* cb */
791 gr_info->maxband[0] = max[0]+1;
792 gr_info->maxband[1] = max[1]+1;
793 gr_info->maxband[2] = max[2]+1;
794 gr_info->maxbandl = max[3]+1;
797 int rmax = max[0] > max[1] ? max[0] : max[1];
798 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
799 gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
803 else {
805 * decoding with 'long' BandIndex table (block_type != 2)
807 int *pretab = gr_info->preflag ? pretab1 : pretab2;
808 int i,max = -1;
809 int cb = 0;
810 int *m = map[sfreq][2];
811 register real v = 0.0;
812 int mc = 0;
815 * long hash table values
817 for(i=0;i<3;i++) {
818 int lp = l[i];
819 struct newhuff *h = ht+gr_info->table_select[i];
821 for(;lp;lp--,mc--) {
822 int x,y;
824 if(!mc) {
825 mc = *m++;
826 cb = *m++;
827 #ifdef CUT_HF
828 if(cb == 21) {
829 fprintf(stderr,"c");
830 v = 0.0;
832 else
833 #endif
834 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
838 register short *val = h->table;
839 REFRESH_MASK;
840 while((y=*val++)<0) {
841 if (mask < 0)
842 val -= y;
843 num--;
844 mask <<= 1;
846 x = y >> 4;
847 y &= 0xf;
850 if (x == 15 && h->linbits) {
851 max = cb;
852 REFRESH_MASK;
853 x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
854 num -= h->linbits+1;
855 mask <<= h->linbits;
856 if(mask < 0)
857 *xrpnt++ = REAL_MUL(-ispow[x], v);
858 else
859 *xrpnt++ = REAL_MUL(ispow[x], v);
860 mask <<= 1;
862 else if(x) {
863 max = cb;
864 if(mask < 0)
865 *xrpnt++ = REAL_MUL(-ispow[x], v);
866 else
867 *xrpnt++ = REAL_MUL(ispow[x], v);
868 num--;
869 mask <<= 1;
871 else
872 *xrpnt++ = 0.0;
874 if (y == 15 && h->linbits) {
875 max = cb;
876 REFRESH_MASK;
877 y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
878 num -= h->linbits+1;
879 mask <<= h->linbits;
880 if(mask < 0)
881 *xrpnt++ = REAL_MUL(-ispow[y], v);
882 else
883 *xrpnt++ = REAL_MUL(ispow[y], v);
884 mask <<= 1;
886 else if(y) {
887 max = cb;
888 if(mask < 0)
889 *xrpnt++ = REAL_MUL(-ispow[y], v);
890 else
891 *xrpnt++ = REAL_MUL(ispow[y], v);
892 num--;
893 mask <<= 1;
895 else
896 *xrpnt++ = 0.0;
901 * short (count1table) values
903 for(;l3 && (part2remain+num > 0);l3--) {
904 struct newhuff *h = htc+gr_info->count1table_select;
905 register short *val = h->table,a;
907 REFRESH_MASK;
908 while((a=*val++)<0) {
909 if (mask < 0)
910 val -= a;
911 num--;
912 mask <<= 1;
914 if(part2remain+num <= 0) {
915 num -= part2remain+num;
916 break;
919 for(i=0;i<4;i++) {
920 if(!(i & 1)) {
921 if(!mc) {
922 mc = *m++;
923 cb = *m++;
924 #ifdef CUT_HF
925 if(cb == 21) {
926 fprintf(stderr,"c");
927 v = 0.0;
929 else
930 #endif
931 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
933 mc--;
935 if ( (a & (0x8>>i)) ) {
936 max = cb;
937 if(part2remain+num <= 0) {
938 break;
940 if(mask < 0)
941 *xrpnt++ = -v;
942 else
943 *xrpnt++ = v;
944 num--;
945 mask <<= 1;
947 else
948 *xrpnt++ = 0.0;
952 gr_info->maxbandl = max+1;
953 gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
956 part2remain += num;
957 // backbits(num);
958 bitindex -= num; wordpointer += (bitindex>>3); bitindex &= 0x7;
959 num = 0;
961 while(xrpnt < &xr[SBLIMIT][0])
962 *xrpnt++ = 0.0;
964 while( part2remain > 16 ) {
965 getbits(16); /* Dismiss stuffing Bits */
966 part2remain -= 16;
968 if(part2remain > 0)
969 getbits(part2remain);
970 else if(part2remain < 0) {
971 fprintf(stderr,"mpg123: Can't rewind stream by %d bits!\n",-part2remain);
972 return 1; /* -> error */
974 return 0;
981 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
983 static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
984 struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
986 real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
987 const struct bandInfoStruct *bi = &bandInfo[sfreq];
989 const real *tab1,*tab2;
991 int tab;
992 static const real *tabs[3][2][2] = {
993 { { tan1_1,tan2_1 } , { tan1_2,tan2_2 } },
994 { { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } } ,
995 { { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } }
998 tab = lsf + (gr_info->scalefac_compress & lsf);
999 tab1 = tabs[tab][ms_stereo][0];
1000 tab2 = tabs[tab][ms_stereo][1];
1001 #if 0
1002 if(lsf) {
1003 int p = gr_info->scalefac_compress & 0x1;
1004 if(ms_stereo) {
1005 tab1 = pow1_2[p]; tab2 = pow2_2[p];
1007 else {
1008 tab1 = pow1_1[p]; tab2 = pow2_1[p];
1011 else {
1012 if(ms_stereo) {
1013 tab1 = tan1_2; tab2 = tan2_2;
1015 else {
1016 tab1 = tan1_1; tab2 = tan2_1;
1019 #endif
1021 // printf("III_i_st: tab1=%p tab2=%p tab=%d ms=%d \n", tab1, tab2, tab, ms_stereo);
1023 if (gr_info->block_type == 2) {
1024 int lwin,do_l = 0;
1025 if( gr_info->mixed_block_flag )
1026 do_l = 1;
1028 for (lwin=0;lwin<3;lwin++) { /* process each window */
1029 /* get first band with zero values */
1030 int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
1031 if(sfb > 3)
1032 do_l = 0;
1034 for(;sfb<12;sfb++) {
1035 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1036 if(is_p != 7) {
1037 real t1,t2;
1038 sb = bi->shortDiff[sfb];
1039 idx = bi->shortIdx[sfb] + lwin;
1040 t1 = tab1[is_p]; t2 = tab2[is_p];
1041 for (; sb > 0; sb--,idx+=3) {
1042 real v = xr[0][idx];
1043 xr[0][idx] = REAL_MUL(v, t1);
1044 xr[1][idx] = REAL_MUL(v, t2);
1049 #if 1
1050 /* in the original: copy 10 to 11 , here: copy 11 to 12
1051 maybe still wrong??? (copy 12 to 13?) */
1052 is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1053 sb = bi->shortDiff[12];
1054 idx = bi->shortIdx[12] + lwin;
1055 #else
1056 is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1057 sb = bi->shortDiff[11];
1058 idx = bi->shortIdx[11] + lwin;
1059 #endif
1060 if(is_p != 7) {
1061 real t1,t2;
1062 t1 = tab1[is_p]; t2 = tab2[is_p];
1063 for ( ; sb > 0; sb--,idx+=3 ) {
1064 real v = xr[0][idx];
1065 xr[0][idx] = REAL_MUL(v, t1);
1066 xr[1][idx] = REAL_MUL(v, t2);
1069 } /* end for(lwin; .. ; . ) */
1071 /* also check l-part, if ALL bands in the three windows are 'empty'
1072 * and mode = mixed_mode
1074 if (do_l) {
1075 int sfb = gr_info->maxbandl;
1076 int idx = bi->longIdx[sfb];
1078 for ( ; sfb<8; sfb++ ) {
1079 int sb = bi->longDiff[sfb];
1080 int is_p = scalefac[sfb]; /* scale: 0-15 */
1081 if(is_p != 7) {
1082 real t1,t2;
1083 t1 = tab1[is_p]; t2 = tab2[is_p];
1084 for ( ; sb > 0; sb--,idx++) {
1085 real v = xr[0][idx];
1086 xr[0][idx] = REAL_MUL(v, t1);
1087 xr[1][idx] = REAL_MUL(v, t2);
1090 else
1091 idx += sb;
1095 else { /* ((gr_info->block_type != 2)) */
1096 int sfb = gr_info->maxbandl;
1097 int is_p,idx = bi->longIdx[sfb];
1099 /* hmm ... maybe the maxbandl stuff for i-stereo is buggy? */
1100 if(sfb <= 21) {
1101 for ( ; sfb<21; sfb++) {
1102 int sb = bi->longDiff[sfb];
1103 is_p = scalefac[sfb]; /* scale: 0-15 */
1104 if(is_p != 7) {
1105 real t1,t2;
1106 t1 = tab1[is_p]; t2 = tab2[is_p];
1107 for ( ; sb > 0; sb--,idx++) {
1108 real v = xr[0][idx];
1109 xr[0][idx] = REAL_MUL(v, t1);
1110 xr[1][idx] = REAL_MUL(v, t2);
1113 else
1114 idx += sb;
1117 is_p = scalefac[20];
1118 if(is_p != 7) { /* copy l-band 20 to l-band 21 */
1119 int sb;
1120 real t1 = tab1[is_p],t2 = tab2[is_p];
1122 for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ ) {
1123 real v = xr[0][idx];
1124 xr[0][idx] = REAL_MUL(v, t1);
1125 xr[1][idx] = REAL_MUL(v, t2);
1128 } /* end: if(sfb <= 21) */
1129 } /* ... */
1132 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info) {
1133 int sblim;
1135 if(gr_info->block_type == 2) {
1136 if(!gr_info->mixed_block_flag)
1137 return;
1138 sblim = 1;
1140 else {
1141 sblim = gr_info->maxb-1;
1144 /* 31 alias-reduction operations between each pair of sub-bands */
1145 /* with 8 butterflies between each pair */
1148 int sb;
1149 real *xr1=(real *) xr[1];
1151 for(sb=sblim;sb;sb--,xr1+=10) {
1152 int ss;
1153 real *cs=aa_cs,*ca=aa_ca;
1154 real *xr2 = xr1;
1156 for(ss=7;ss>=0;ss--) { /* upper and lower butterfly inputs */
1157 register real bu = *--xr2,bd = *xr1;
1158 *xr2 = (bu * (*cs) ) - (bd * (*ca) );
1159 *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
1166 #include "dct64.c"
1167 #include "dct36.c"
1168 #include "dct12.c"
1170 #include "decod386.c"
1173 * III_hybrid
1176 static dct36_func_t dct36_func;
1178 static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1179 int ch,struct gr_info_s *gr_info)
1181 real *tspnt = (real *) tsOut;
1182 static real block[2][2][SBLIMIT*SSLIMIT] = { { { 0, } } };
1183 static int blc[2]={0,0};
1184 real *rawout1,*rawout2;
1185 int bt;
1186 int sb = 0;
1189 int b = blc[ch];
1190 rawout1=block[b][ch];
1191 b=-b+1;
1192 rawout2=block[b][ch];
1193 blc[ch] = b;
1196 if(gr_info->mixed_block_flag) {
1197 sb = 2;
1198 (*dct36_func)(fsIn[0],rawout1,rawout2,win[0],tspnt);
1199 (*dct36_func)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1200 rawout1 += 36; rawout2 += 36; tspnt += 2;
1203 bt = gr_info->block_type;
1204 if(bt == 2) {
1205 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1206 dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt);
1207 dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1210 else {
1211 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1212 (*dct36_func)(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1213 (*dct36_func)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1217 for(;sb<SBLIMIT;sb++,tspnt++) {
1218 int i;
1219 for(i=0;i<SSLIMIT;i++) {
1220 tspnt[i*SBLIMIT] = *rawout1++;
1221 *rawout2++ = 0.0;
1227 * main layer3 handler
1229 /* int do_layer3(struct frame *fr,int outmode,struct audio_info_struct *ai) */
1230 static int do_layer3(struct frame *fr,int single){
1231 int gr, ch, ss,clip=0;
1232 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1233 struct III_sideinfo sideinfo;
1234 int stereo = fr->stereo;
1235 int ms_stereo,i_stereo;
1236 int sfreq = fr->sampling_frequency;
1237 int stereo1,granules;
1239 // if (fr->error_protection) getbits(16); /* skip crc */
1241 if(stereo == 1) { /* stream is mono */
1242 stereo1 = 1;
1243 single = 0;
1244 } else
1245 if(single >= 0) /* stream is stereo, but force to mono */
1246 stereo1 = 1;
1247 else
1248 stereo1 = 2;
1250 if(fr->mode == MPG_MD_JOINT_STEREO) {
1251 ms_stereo = (fr->mode_ext & 0x2)>>1;
1252 i_stereo = fr->mode_ext & 0x1;
1253 } else
1254 ms_stereo = i_stereo = 0;
1256 if(!III_get_side_info(&sideinfo,stereo,ms_stereo,sfreq,single,fr->lsf))
1257 return -1;
1259 set_pointer(sideinfo.main_data_begin);
1261 granules = (fr->lsf) ? 1 : 2;
1262 for (gr=0;gr<granules;gr++){
1263 DECLARE_ALIGNED(16, real, hybridIn[2][SBLIMIT][SSLIMIT]);
1264 DECLARE_ALIGNED(16, real, hybridOut[2][SSLIMIT][SBLIMIT]);
1266 { struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
1267 int part2bits;
1268 if(fr->lsf)
1269 part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0);
1270 else
1271 part2bits = III_get_scale_factors_1(scalefacs[0],gr_info);
1272 if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
1273 return clip;
1276 if(stereo == 2) {
1277 struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
1279 int part2bits;
1280 if(fr->lsf)
1281 part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo);
1282 else
1283 part2bits = III_get_scale_factors_1(scalefacs[1],gr_info);
1285 if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
1286 return clip;
1288 if(ms_stereo) {
1289 int i;
1290 int maxb = sideinfo.ch[0].gr[gr].maxb;
1291 if(sideinfo.ch[1].gr[gr].maxb > maxb)
1292 maxb = sideinfo.ch[1].gr[gr].maxb;
1293 for(i=0;i<SSLIMIT*maxb;i++) {
1294 real tmp0 = ((real *)hybridIn[0])[i];
1295 real tmp1 = ((real *)hybridIn[1])[i];
1296 ((real *)hybridIn[0])[i] = tmp0 + tmp1;
1297 ((real *)hybridIn[1])[i] = tmp0 - tmp1;
1301 if(i_stereo)
1302 III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
1304 if(ms_stereo || i_stereo || (single == 3) ) {
1305 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
1306 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
1307 else
1308 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
1311 switch(single) {
1312 case 3: {
1313 register int i;
1314 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1315 for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++)
1316 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1317 break; }
1318 case 1: {
1319 register int i;
1320 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1321 for(i=0;i<SSLIMIT*gr_info->maxb;i++)
1322 *in0++ = *in1++;
1323 break; }
1326 } // if(stereo == 2)
1328 for(ch=0;ch<stereo1;ch++) {
1329 struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
1330 III_antialias(hybridIn[ch],gr_info);
1331 III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info);
1334 for(ss=0;ss<SSLIMIT;ss++) {
1335 if(single >= 0) {
1336 clip += (fr->synth_mono)(hybridOut[0][ss],pcm_sample,&pcm_point);
1337 } else {
1338 int p1 = pcm_point;
1339 clip += (fr->synth)(hybridOut[0][ss],0,pcm_sample,&p1);
1340 clip += (fr->synth)(hybridOut[1][ss],1,pcm_sample,&pcm_point);
1346 return clip;