2 * Modified for use with MPlayer, for details see the changelog at
3 * http://svn.mplayerhq.hu/mplayer/trunk/
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
15 * Length-optimze: unify long and short band code where it is possible
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 */
37 static real COS6_1
,COS6_2
;
40 static real tfcos12
[3];
43 static real cos9
[3],cos18
[3];
46 struct bandInfoStruct
{
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] = {
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} } ,
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 } } ,
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
)
123 for(i
=-256;i
<118+4;i
++)
126 gainpow2
[i
+256] = 16384.0 * pow((double)2.0,-0.25 * (double) (i
+210) );
128 gainpow2
[i
+256] = pow((double)2.0,-0.25 * (double) (i
+210) );
131 ispow
[i
] = pow((double)i
,(double)4.0/3.0);
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
]);
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 );
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 );
156 COS9
[i
] = cos( M_PI
/ 18.0 * (double) i
);
159 tfcos36
[i
] = 0.5 / cos ( M_PI
* (double) (i
*2+1) / 36.0 );
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);
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);
177 win
[2][i
] = 0.5 * sin( M_PI
/ 24.0 * (double) (2*i
+1) ) / cos ( M_PI
* (double) (2*i
+7) / 24.0 );
179 COS1
[i
][j
] = cos( M_PI
/ 24.0 * (double) ((2*i
+7)*(2*j
+1)) );
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
];
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
);
199 double base
= pow(2.0,-0.25*(j
+1.0));
200 double p1
=1.0,p2
=1.0;
203 p1
= pow(base
,(i
+1.0)*0.5);
205 p2
= pow(base
,i
*0.5);
209 pow1_2
[j
][i
] = M_SQRT2
* p1
;
210 pow2_2
[j
][i
] = M_SQRT2
* p2
;
216 const struct bandInfoStruct
*bi
= &bandInfo
[j
];
221 mp
= map
[j
][0] = mapbuf0
[j
];
223 for(i
=0,cb
= 0; cb
< 8 ; cb
++,i
+=*bdf
++) {
229 bdf
= bi
->shortDiff
+3;
230 for(cb
=3;cb
<13;cb
++) {
231 int l
= (*bdf
++) >> 1;
232 for(lwin
=0;lwin
<3;lwin
++) {
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
++) {
256 mp
= map
[j
][2] = mapbuf2
[j
];
258 for(cb
= 0; cb
< 22 ; cb
++) {
259 *mp
++ = (*bdf
++) >> 1;
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
;
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
;
282 int n
= k
+ j
* 6 + i
* 36;
283 i_slen2
[n
] = i
|(j
<<3)|(k
<<6)|(3<<12);
290 int n
= k
+ j
* 4 + i
* 16;
291 i_slen2
[n
+180] = i
|(j
<<3)|(k
<<6)|(4<<12);
298 i_slen2
[n
+244] = i
|(j
<<3) | (5<<12);
299 n_slen2
[n
+500] = i
|(j
<<3) | (2<<12) | (1<<15);
307 int n
= l
+ k
* 4 + j
* 16 + i
* 80;
308 n_slen2
[n
] = i
|(j
<<3)|(k
<<6)|(l
<<9)|(0<<12);
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
)
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]);
337 si
->private_bits
= getbits_fast(tab
[2]);
339 si
->private_bits
= getbits_fast(tab
[3]);
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
;
360 gr_info
->pow2gain
+= 2;
361 gr_info
->scalefac_compress
= getbits(tab
[4]);
363 if(get1bit()) { /* window switch flag */
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]);
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;
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");
387 /* region_count/start parameters are implicit in this case. */
388 if(!lsf
|| gr_info
->block_type
== 2)
389 gr_info
->region1start
= 36>>1;
391 /* check this again for 2.5 and sfreq=8 */
393 gr_info
->region1start
= 108>>1;
395 gr_info
->region1start
= 54>>1;
397 gr_info
->region2start
= 576>>1;
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]);
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;
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;
418 gr_info
->preflag
= get1bit();
419 gr_info
->scalefac_scale
= get1bit();
420 gr_info
->count1table_select
= get1bit();
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}
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) {
442 numbits
= (num0
+ num1
) * 18;
444 if (gr_info
->mixed_block_flag
) {
446 *scf
++ = getbits_fast(num0
);
448 numbits
-= num0
; /* num0 * 17 + num1 * 18 */
452 *scf
++ = getbits_fast(num0
);
454 *scf
++ = getbits_fast(num1
);
455 *scf
++ = 0; *scf
++ = 0; *scf
++ = 0; /* short[13][0..2] = 0 */
459 int scfsi
= gr_info
->scfsi
;
461 if(scfsi
< 0) { /* scfsi < 0 => granule == 0 */
463 *scf
++ = getbits_fast(num0
);
465 *scf
++ = getbits_fast(num1
);
466 numbits
= (num0
+ num1
) * 10 + num0
;
473 *scf
++ = getbits_fast(num0
);
482 *scf
++ = getbits_fast(num0
);
491 *scf
++ = getbits_fast(num1
);
500 *scf
++ = getbits_fast(num1
);
506 *scf
++ = 0; /* no l[21] in original sources */
512 static int III_get_scale_factors_2(int *scf
,struct gr_info_s
*gr_info
,int i_stereo
)
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];
531 slen
= n_slen2
[gr_info
->scalefac_compress
];
533 gr_info
->preflag
= (slen
>>15) & 0x1;
536 if( gr_info
->block_type
== 2 ) {
538 if(gr_info
->mixed_block_flag
) n
++;
541 pnt
= stab
[n
][(slen
>>12)&0x7];
544 int num
= slen
& 0x7;
547 for(j
=0;j
<(int)(pnt
[i
]);j
++) *scf
++ = getbits_fast(num
);
548 numbits
+= pnt
[i
] * num
;
551 for(j
=0;j
<(int)(pnt
[i
]);j
++) *scf
++ = 0;
556 for(i
=0;i
<n
;i
++) *scf
++ = 0;
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); \
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
;
584 int part2remain
= gr_info
->part2_3_length
- part2bits
;
587 int num
=getbitoffset();
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
;
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
605 l
[0] = bv
; l
[1] = l
[2] = 0;
610 l
[1] = bv
- l
[0]; l
[2] = 0;
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
623 int step
=0,lwin
=3,cb
=0;
624 register real v
= 0.0;
627 if(gr_info
->mixed_block_flag
) {
629 max
[0] = max
[1] = max
[2] = 2;
631 me
= mapend
[sfreq
][0];
634 max
[0] = max
[1] = max
[2] = max
[3] = -1;
635 /* max[3] not really needed in this case */
637 me
= mapend
[sfreq
][1];
643 struct newhuff
*h
= ht
+gr_info
->table_select
[i
];
648 xrpnt
= ((real
*) xr
) + (*m
++);
652 v
= gr_info
->pow2gain
[(*scf
++) << shift
];
656 v
= gr_info
->full_gain
[lwin
][(*scf
++) << shift
];
661 register short *val
= h
->table
;
663 while((y
=*val
++)<0) {
672 if(x
== 15 && h
->linbits
) {
675 x
+= ((unsigned long) mask
) >> (BITSHIFT
+8-h
->linbits
);
679 *xrpnt
= REAL_MUL(-ispow
[x
], v
);
681 *xrpnt
= REAL_MUL(ispow
[x
], v
);
687 *xrpnt
= REAL_MUL(-ispow
[x
], v
);
689 *xrpnt
= REAL_MUL(ispow
[x
], v
);
696 if(y
== 15 && h
->linbits
) {
699 y
+= ((unsigned long) mask
) >> (BITSHIFT
+8-h
->linbits
);
703 *xrpnt
= REAL_MUL(-ispow
[y
], v
);
705 *xrpnt
= REAL_MUL(ispow
[y
], v
);
711 *xrpnt
= REAL_MUL(-ispow
[y
], v
);
713 *xrpnt
= REAL_MUL(ispow
[y
], v
);
723 for(;l3
&& (part2remain
+num
> 0);l3
--) {
724 struct newhuff
*h
= htc
+gr_info
->count1table_select
;
725 register short *val
= h
->table
,a
;
728 while((a
=*val
++)<0) {
734 if(part2remain
+num
<= 0) {
735 num
-= part2remain
+num
;
743 xrpnt
= ((real
*) xr
) + (*m
++);
747 v
= gr_info
->pow2gain
[(*scf
++) << shift
];
751 v
= gr_info
->full_gain
[lwin
][(*scf
++) << shift
];
757 if( (a
& (0x8>>i
)) ) {
759 if(part2remain
+num
<= 0) {
775 if(lwin
< 3) { /* short band? */
778 *xrpnt
= 0.0; xrpnt
+= 3; /* short band -> step=3 */
779 *xrpnt
= 0.0; xrpnt
+= 3;
784 xrpnt
= ((real
*) xr
) + *m
++;
786 break; /* optimize: field will be set to zero at the end of the function */
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];
805 * decoding with 'long' BandIndex table (block_type != 2)
807 int *pretab
= gr_info
->preflag
? pretab1
: pretab2
;
810 int *m
= map
[sfreq
][2];
811 register real v
= 0.0;
815 * long hash table values
819 struct newhuff
*h
= ht
+gr_info
->table_select
[i
];
834 v
= gr_info
->pow2gain
[((*scf
++) + (*pretab
++)) << shift
];
838 register short *val
= h
->table
;
840 while((y
=*val
++)<0) {
850 if (x
== 15 && h
->linbits
) {
853 x
+= ((unsigned long) mask
) >> (BITSHIFT
+8-h
->linbits
);
857 *xrpnt
++ = REAL_MUL(-ispow
[x
], v
);
859 *xrpnt
++ = REAL_MUL(ispow
[x
], v
);
865 *xrpnt
++ = REAL_MUL(-ispow
[x
], v
);
867 *xrpnt
++ = REAL_MUL(ispow
[x
], v
);
874 if (y
== 15 && h
->linbits
) {
877 y
+= ((unsigned long) mask
) >> (BITSHIFT
+8-h
->linbits
);
881 *xrpnt
++ = REAL_MUL(-ispow
[y
], v
);
883 *xrpnt
++ = REAL_MUL(ispow
[y
], v
);
889 *xrpnt
++ = REAL_MUL(-ispow
[y
], v
);
891 *xrpnt
++ = REAL_MUL(ispow
[y
], v
);
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
;
908 while((a
=*val
++)<0) {
914 if(part2remain
+num
<= 0) {
915 num
-= part2remain
+num
;
931 v
= gr_info
->pow2gain
[((*scf
++) + (*pretab
++)) << shift
];
935 if ( (a
& (0x8>>i
)) ) {
937 if(part2remain
+num
<= 0) {
952 gr_info
->maxbandl
= max
+1;
953 gr_info
->maxb
= longLimit
[sfreq
][gr_info
->maxbandl
];
958 bitindex
-= num
; wordpointer
+= (bitindex
>>3); bitindex
&= 0x7;
961 while(xrpnt
< &xr
[SBLIMIT
][0])
964 while( part2remain
> 16 ) {
965 getbits(16); /* Dismiss stuffing Bits */
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 */
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
;
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];
1003 int p
= gr_info
->scalefac_compress
& 0x1;
1005 tab1
= pow1_2
[p
]; tab2
= pow2_2
[p
];
1008 tab1
= pow1_1
[p
]; tab2
= pow2_1
[p
];
1013 tab1
= tan1_2
; tab2
= tan2_2
;
1016 tab1
= tan1_1
; tab2
= tan2_1
;
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) {
1025 if( gr_info
->mixed_block_flag
)
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 */
1034 for(;sfb
<12;sfb
++) {
1035 is_p
= scalefac
[sfb
*3+lwin
-gr_info
->mixed_block_flag
]; /* scale: 0-15 */
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
);
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
;
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
;
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
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 */
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
);
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? */
1101 for ( ; sfb
<21; sfb
++) {
1102 int sb
= bi
->longDiff
[sfb
];
1103 is_p
= scalefac
[sfb
]; /* scale: 0-15 */
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
);
1117 is_p
= scalefac
[20];
1118 if(is_p
!= 7) { /* copy l-band 20 to l-band 21 */
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) */
1132 static void III_antialias(real xr
[SBLIMIT
][SSLIMIT
],struct gr_info_s
*gr_info
) {
1135 if(gr_info
->block_type
== 2) {
1136 if(!gr_info
->mixed_block_flag
)
1141 sblim
= gr_info
->maxb
-1;
1144 /* 31 alias-reduction operations between each pair of sub-bands */
1145 /* with 8 butterflies between each pair */
1149 real
*xr1
=(real
*) xr
[1];
1151 for(sb
=sblim
;sb
;sb
--,xr1
+=10) {
1153 real
*cs
=aa_cs
,*ca
=aa_ca
;
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
++) );
1170 #include "decod386.c"
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
;
1190 rawout1
=block
[b
][ch
];
1192 rawout2
=block
[b
][ch
];
1196 if(gr_info
->mixed_block_flag
) {
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
;
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);
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
++) {
1219 for(i
=0;i
<SSLIMIT
;i
++) {
1220 tspnt
[i
*SBLIMIT
] = *rawout1
++;
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 */
1245 if(single
>= 0) /* stream is stereo, but force to mono */
1250 if(fr
->mode
== MPG_MD_JOINT_STEREO
) {
1251 ms_stereo
= (fr
->mode_ext
& 0x2)>>1;
1252 i_stereo
= fr
->mode_ext
& 0x1;
1254 ms_stereo
= i_stereo
= 0;
1256 if(!III_get_side_info(&sideinfo
,stereo
,ms_stereo
,sfreq
,single
,fr
->lsf
))
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
]);
1269 part2bits
= III_get_scale_factors_2(scalefacs
[0],gr_info
,0);
1271 part2bits
= III_get_scale_factors_1(scalefacs
[0],gr_info
);
1272 if(III_dequantize_sample(hybridIn
[0], scalefacs
[0],gr_info
,sfreq
,part2bits
))
1277 struct gr_info_s
*gr_info
= &(sideinfo
.ch
[1].gr
[gr
]);
1281 part2bits
= III_get_scale_factors_2(scalefacs
[1],gr_info
,i_stereo
);
1283 part2bits
= III_get_scale_factors_1(scalefacs
[1],gr_info
);
1285 if(III_dequantize_sample(hybridIn
[1],scalefacs
[1],gr_info
,sfreq
,part2bits
))
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
;
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
;
1308 gr_info
->maxb
= sideinfo
.ch
[0].gr
[gr
].maxb
;
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 */
1320 register real
*in0
= (real
*) hybridIn
[0],*in1
= (real
*) hybridIn
[1];
1321 for(i
=0;i
<SSLIMIT
*gr_info
->maxb
;i
++)
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
++) {
1336 clip
+= (fr
->synth_mono
)(hybridOut
[0][ss
],pcm_sample
,&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
);