2 * Mpeg Layer-3 audio decoder
3 * --------------------------
4 * copyright (c) 1995,1996,1997 by Michael Hipp.
5 * All rights reserved. See also 'README'
6 * This file has been copied from mpglib.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
33 static real ispow[8207];
34 static real aa_ca[8],aa_cs[8];
35 static real COS1[12][6];
36 static real win[4][36];
37 static real win1[4][36];
38 static real gainpow2[256+118+4];
40 static real COS6_1,COS6_2;
41 static real tfcos36[9];
42 static real tfcos12[3];
44 struct bandInfoStruct {
51 static int longLimit[9][23];
52 static int shortLimit[9][14];
54 static const struct bandInfoStruct bandInfo[9] = {
57 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
58 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
59 {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},
60 {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
62 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
63 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
64 {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},
65 {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
67 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
68 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
69 {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} ,
70 {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
73 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
74 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
75 {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} ,
76 {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
78 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
79 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
80 {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} ,
81 {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
83 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
84 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
85 {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},
86 {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
88 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
89 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
90 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
91 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
92 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
93 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
94 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
95 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
96 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
97 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
98 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
99 {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
102 static int mapbuf0[9][152];
103 static int mapbuf1[9][156];
104 static int mapbuf2[9][44];
105 static int *map[9][3];
106 static int *mapend[9][3];
108 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
109 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
111 static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
112 static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
115 * init tables for layer-3
117 void init_layer3(int down_sample_sblimit)
121 for(i=-256;i<118+4;i++)
122 gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
125 ispow[i] = pow((double)i,(double)4.0/3.0);
129 static const double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
130 double sq=sqrt(1.0+Ci[i]*Ci[i]);
137 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 );
138 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 );
142 win[1][i+18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
143 win[3][i+12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 );
144 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 );
145 win[1][i+30] = win[3][i] = 0.0;
146 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 );
150 COS9[i] = cos( M_PI / 18.0 * (double) i);
153 tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 );
155 tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 );
157 COS6_1 = cos( M_PI / 6.0 * (double) 1);
158 COS6_2 = cos( M_PI / 6.0 * (double) 2);
162 win[2][i] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 );
164 COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) );
168 static const int len[4] = { 36,36,12,36 };
169 for(i=0;i<len[j];i+=2)
170 win1[j][i] = + win[j][i];
171 for(i=1;i<len[j];i+=2)
172 win1[j][i] = - win[j][i];
177 double t = tan( (double) i * M_PI / 12.0 );
178 tan1_1[i] = t / (1.0+t);
179 tan2_1[i] = 1.0 / (1.0 + t);
180 tan1_2[i] = M_SQRT2 * t / (1.0+t);
181 tan2_2[i] = M_SQRT2 / (1.0 + t);
184 double base = pow(2.0,-0.25*(j+1.0));
185 double p1=1.0,p2=1.0;
188 p1 = pow(base,(i+1.0)*0.5);
190 p2 = pow(base,i*0.5);
194 pow1_2[j][i] = M_SQRT2 * p1;
195 pow2_2[j][i] = M_SQRT2 * p2;
201 const struct bandInfoStruct *bi = &bandInfo[j];
206 mp = map[j][0] = mapbuf0[j];
208 for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
214 bdf = bi->shortDiff+3;
215 for(cb=3;cb<13;cb++) {
216 int l = (*bdf++) >> 1;
217 for(lwin=0;lwin<3;lwin++) {
227 mp = map[j][1] = mapbuf1[j];
228 bdf = bi->shortDiff+0;
229 for(i=0,cb=0;cb<13;cb++) {
230 int l = (*bdf++) >> 1;
231 for(lwin=0;lwin<3;lwin++) {
241 mp = map[j][2] = mapbuf2[j];
243 for(cb = 0; cb < 22 ; cb++) {
244 *mp++ = (*bdf++) >> 1;
253 longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
254 if(longLimit[j][i] > (down_sample_sblimit) )
255 longLimit[j][i] = down_sample_sblimit;
258 shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
259 if(shortLimit[j][i] > (down_sample_sblimit) )
260 shortLimit[j][i] = down_sample_sblimit;
267 int n = k + j * 6 + i * 36;
268 i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
275 int n = k + j * 4 + i * 16;
276 i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
283 i_slen2[n+244] = i|(j<<3) | (5<<12);
284 n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
292 int n = l + k * 4 + j * 16 + i * 80;
293 n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
301 int n = k + j * 4 + i * 20;
302 n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
309 * read additional side information
312 static int III_get_side_info_1(struct III_sideinfo *si,int stereo,
313 int ms_stereo,long sfreq,int single)
316 int powdiff = (single == 3) ? 4 : 0;
318 si->main_data_begin = getbits(9);
320 si->private_bits = getbits_fast(5);
322 si->private_bits = getbits_fast(3);
324 for (ch=0; ch<stereo; ch++) {
325 si->ch[ch].gr[0].scfsi = -1;
326 si->ch[ch].gr[1].scfsi = getbits_fast(4);
329 for (gr=0; gr<2; gr++)
331 for (ch=0; ch<stereo; ch++)
333 register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
335 gr_info->part2_3_length = getbits(12);
336 gr_info->big_values = getbits_fast(9);
337 if(gr_info->big_values > 288) {
338 FIXME("big_values (%d) too large!\n", gr_info->big_values);
339 gr_info->big_values = 288;
341 gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
343 gr_info->pow2gain += 2;
344 gr_info->scalefac_compress = getbits_fast(4);
345 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
349 gr_info->block_type = getbits_fast(2);
350 gr_info->mixed_block_flag = get1bit();
351 gr_info->table_select[0] = getbits_fast(5);
352 gr_info->table_select[1] = getbits_fast(5);
354 * table_select[2] not needed, because there is no region2,
355 * but to satisfy some verifications tools we set it either.
357 gr_info->table_select[2] = 0;
359 gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
361 if(gr_info->block_type == 0) {
362 FIXME("Blocktype == 0 and window-switching == 1 not allowed.\n");
365 /* region_count/start parameters are implicit in this case. */
366 gr_info->region1start = 36>>1;
367 gr_info->region2start = 576>>1;
373 gr_info->table_select[i] = getbits_fast(5);
374 r0c = getbits_fast(4);
375 r1c = getbits_fast(3);
376 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
377 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
378 gr_info->block_type = 0;
379 gr_info->mixed_block_flag = 0;
381 gr_info->preflag = get1bit();
382 gr_info->scalefac_scale = get1bit();
383 gr_info->count1table_select = get1bit();
391 * Side Info for MPEG 2.0 / LSF
393 static int III_get_side_info_2(struct III_sideinfo *si,int stereo,
394 int ms_stereo,long sfreq,int single)
397 int powdiff = (single == 3) ? 4 : 0;
399 si->main_data_begin = getbits(8);
401 si->private_bits = get1bit();
403 si->private_bits = getbits_fast(2);
405 for (ch=0; ch<stereo; ch++)
407 register struct gr_info_s *gr_info = &(si->ch[ch].gr[0]);
409 gr_info->part2_3_length = getbits(12);
410 gr_info->big_values = getbits_fast(9);
411 if(gr_info->big_values > 288) {
412 FIXME("big_values(%d) too large!\n", gr_info->big_values);
413 gr_info->big_values = 288;
415 gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
417 gr_info->pow2gain += 2;
418 gr_info->scalefac_compress = getbits(9);
419 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
423 gr_info->block_type = getbits_fast(2);
424 gr_info->mixed_block_flag = get1bit();
425 gr_info->table_select[0] = getbits_fast(5);
426 gr_info->table_select[1] = getbits_fast(5);
428 * table_select[2] not needed, because there is no region2,
429 * but to satisfy some verifications tools we set it either.
431 gr_info->table_select[2] = 0;
433 gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
435 if(gr_info->block_type == 0) {
436 FIXME("Blocktype == 0 and window-switching == 1 not allowed.\n");
439 /* region_count/start parameters are implicit in this case. */
440 /* check this again! */
441 if(gr_info->block_type == 2)
442 gr_info->region1start = 36>>1;
444 /* check this for 2.5 and sfreq=8 */
445 gr_info->region1start = 108>>1;
447 gr_info->region1start = 54>>1;
448 gr_info->region2start = 576>>1;
454 gr_info->table_select[i] = getbits_fast(5);
455 r0c = getbits_fast(4);
456 r1c = getbits_fast(3);
457 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
458 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
459 gr_info->block_type = 0;
460 gr_info->mixed_block_flag = 0;
462 gr_info->scalefac_scale = get1bit();
463 gr_info->count1table_select = get1bit();
472 static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
474 static const unsigned char slen[2][16] = {
475 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
476 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
479 int num0 = slen[0][gr_info->scalefac_compress];
480 int num1 = slen[1][gr_info->scalefac_compress];
482 if (gr_info->block_type == 2) {
484 numbits = (num0 + num1) * 18;
486 if (gr_info->mixed_block_flag) {
488 *scf++ = getbits_fast(num0);
490 numbits -= num0; /* num0 * 17 + num1 * 18 */
494 *scf++ = getbits_fast(num0);
496 *scf++ = getbits_fast(num1);
497 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
501 int scfsi = gr_info->scfsi;
503 if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
505 *scf++ = getbits_fast(num0);
507 *scf++ = getbits_fast(num1);
508 numbits = (num0 + num1) * 10 + num0;
515 *scf++ = getbits_fast(num0);
524 *scf++ = getbits_fast(num0);
533 *scf++ = getbits_fast(num1);
542 *scf++ = getbits_fast(num1);
548 *scf++ = 0; /* no l[21] in original sources */
556 static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
558 const unsigned char *pnt;
564 static const unsigned char stab[3][6][4] = {
565 { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
566 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
567 { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
568 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
569 { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
570 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
572 if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
573 slen = i_slen2[gr_info->scalefac_compress>>1];
575 slen = n_slen2[gr_info->scalefac_compress];
577 gr_info->preflag = (slen>>15) & 0x1;
580 if( gr_info->block_type == 2 ) {
582 if(gr_info->mixed_block_flag)
586 pnt = stab[n][(slen>>12)&0x7];
589 int num = slen & 0x7;
592 for(j=0;j<(int)(pnt[i]);j++)
593 *scf++ = getbits_fast(num);
594 numbits += pnt[i] * num;
597 for(j=0;j<(int)(pnt[i]);j++)
609 static const 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};
610 static const 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};
612 static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
613 struct gr_info_s *gr_info,int sfreq,int part2bits)
615 int shift = 1 + gr_info->scalefac_scale;
616 real *xrpnt = (real *) xr;
618 int part2remain = gr_info->part2_3_length - part2bits;
622 int bv = gr_info->big_values;
623 int region1 = gr_info->region1start;
624 int region2 = gr_info->region2start;
626 l3 = ((576>>1)-bv)>>1;
628 * we may lose the 'odd' bit here !!
629 * check this later again
632 l[0] = bv; l[1] = 0; l[2] = 0;
637 l[1] = bv - l[0]; l[2] = 0;
640 l[1] = region2 - l[0]; l[2] = bv - region2;
645 if(gr_info->block_type == 2) {
647 * decoding with short or mixed mode BandIndex table
650 int step=0,lwin=0,cb=0;
651 register real v = 0.0;
654 if(gr_info->mixed_block_flag) {
656 max[0] = max[1] = max[2] = 2;
658 me = mapend[sfreq][0];
661 max[0] = max[1] = max[2] = max[3] = -1;
662 /* max[3] not really needed in this case */
664 me = mapend[sfreq][1];
670 const struct newhuff *h = ht+gr_info->table_select[i];
675 xrpnt = ((real *) xr) + (*m++);
679 v = gr_info->pow2gain[(*scf++) << shift];
683 v = gr_info->full_gain[lwin][(*scf++) << shift];
688 register const short *val = h->table;
689 while((y=*val++)<0) {
699 part2remain -= h->linbits+1;
700 x += getbits(h->linbits);
702 *xrpnt = -ispow[x] * v;
704 *xrpnt = ispow[x] * v;
709 *xrpnt = -ispow[x] * v;
711 *xrpnt = ispow[x] * v;
719 part2remain -= h->linbits+1;
720 y += getbits(h->linbits);
722 *xrpnt = -ispow[y] * v;
724 *xrpnt = ispow[y] * v;
729 *xrpnt = -ispow[y] * v;
731 *xrpnt = ispow[y] * v;
739 for(;l3 && (part2remain > 0);l3--) {
740 const struct newhuff *h = htc+gr_info->count1table_select;
741 const short *val = h->table;
744 while((a=*val++)<0) {
746 if(part2remain < 0) {
759 xrpnt = ((real *) xr) + (*m++);
763 v = gr_info->pow2gain[(*scf++) << shift];
767 v = gr_info->full_gain[lwin][(*scf++) << shift];
773 if( (a & (0x8>>i)) ) {
776 if(part2remain < 0) {
794 xrpnt = ((real *) xr) + *m++;
806 /* we could add a little opt. here:
807 * if we finished a band for window 3 or a long band
808 * further bands could copied in a simple loop without a
809 * special 'map' decoding
813 gr_info->maxband[0] = max[0]+1;
814 gr_info->maxband[1] = max[1]+1;
815 gr_info->maxband[2] = max[2]+1;
816 gr_info->maxbandl = max[3]+1;
819 int rmax = max[0] > max[1] ? max[0] : max[1];
820 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
821 gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
827 * decoding with 'long' BandIndex table (block_type != 2)
829 const int *pretab = gr_info->preflag ? pretab1 : pretab2;
832 register int *m = map[sfreq][2];
833 register real v = 0.0;
836 me = mapend[sfreq][2];
840 * long hash table values
844 const struct newhuff *h = ht+gr_info->table_select[i];
851 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
855 register const short *val = h->table;
856 while((y=*val++)<0) {
866 part2remain -= h->linbits+1;
867 x += getbits(h->linbits);
869 *xrpnt++ = -ispow[x] * v;
871 *xrpnt++ = ispow[x] * v;
876 *xrpnt++ = -ispow[x] * v;
878 *xrpnt++ = ispow[x] * v;
886 part2remain -= h->linbits+1;
887 y += getbits(h->linbits);
889 *xrpnt++ = -ispow[y] * v;
891 *xrpnt++ = ispow[y] * v;
896 *xrpnt++ = -ispow[y] * v;
898 *xrpnt++ = ispow[y] * v;
907 * short (count1table) values
909 for(;l3 && (part2remain > 0);l3--) {
910 const struct newhuff *h = htc+gr_info->count1table_select;
911 const short *val = h->table;
914 while((a=*val++)<0) {
916 if(part2remain < 0) {
930 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
934 if ( (a & (0x8>>i)) ) {
937 if(part2remain < 0) {
954 for(i=(&xr[SBLIMIT][0]-xrpnt)>>1;i;i--) {
959 gr_info->maxbandl = max+1;
960 gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
963 while( part2remain > 16 ) {
964 getbits(16); /* Dismiss stuffing Bits */
968 getbits(part2remain);
969 else if(part2remain < 0) {
970 FIXME("mpg123: Can't rewind stream by %d bits!\n",-part2remain);
971 return 1; /* -> error */
977 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
979 static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
980 struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
982 real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
983 const struct bandInfoStruct *bi = &bandInfo[sfreq];
987 int p = gr_info->scalefac_compress & 0x1;
989 tab1 = pow1_2[p]; tab2 = pow2_2[p];
992 tab1 = pow1_1[p]; tab2 = pow2_1[p];
997 tab1 = tan1_2; tab2 = tan2_2;
1000 tab1 = tan1_1; tab2 = tan2_1;
1004 if (gr_info->block_type == 2)
1007 if( gr_info->mixed_block_flag )
1010 for (lwin=0;lwin<3;lwin++) /* process each window */
1012 /* get first band with zero values */
1013 int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
1019 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1022 sb = bi->shortDiff[sfb];
1023 idx = bi->shortIdx[sfb] + lwin;
1024 t1 = tab1[is_p]; t2 = tab2[is_p];
1025 for (; sb > 0; sb--,idx+=3)
1027 real v = xr[0][idx];
1028 xr[0][idx] = v * t1;
1029 xr[1][idx] = v * t2;
1035 /* in the original: copy 10 to 11 , here: copy 11 to 12
1036 maybe still wrong??? (copy 12 to 13?) */
1037 is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1038 sb = bi->shortDiff[12];
1039 idx = bi->shortIdx[12] + lwin;
1041 is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1042 sb = bi->shortDiff[11];
1043 idx = bi->shortIdx[11] + lwin;
1048 t1 = tab1[is_p]; t2 = tab2[is_p];
1049 for ( ; sb > 0; sb--,idx+=3 )
1051 real v = xr[0][idx];
1052 xr[0][idx] = v * t1;
1053 xr[1][idx] = v * t2;
1056 } /* end for(lwin; .. ; . ) */
1060 /* also check l-part, if ALL bands in the three windows are 'empty'
1061 * and mode = mixed_mode
1063 int sfb = gr_info->maxbandl;
1065 if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */
1066 idx = bi->longIdx[sfb];
1067 for ( ; sfb<8; sfb++ )
1069 int sb = bi->longDiff[sfb];
1070 int is_p = scalefac[sfb]; /* scale: 0-15 */
1073 t1 = tab1[is_p]; t2 = tab2[is_p];
1074 for ( ; sb > 0; sb--,idx++)
1076 real v = xr[0][idx];
1077 xr[0][idx] = v * t1;
1078 xr[1][idx] = v * t2;
1086 else /* ((gr_info->block_type != 2)) */
1088 int sfb = gr_info->maxbandl;
1090 if (sfb > 21) return; /* tightened fix for CVE-2006-1655 */
1091 idx = bi->longIdx[sfb];
1092 for ( ; sfb<21; sfb++)
1094 int sb = bi->longDiff[sfb];
1095 is_p = scalefac[sfb]; /* scale: 0-15 */
1098 t1 = tab1[is_p]; t2 = tab2[is_p];
1099 for ( ; sb > 0; sb--,idx++)
1101 real v = xr[0][idx];
1102 xr[0][idx] = v * t1;
1103 xr[1][idx] = v * t2;
1110 is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1114 real t1 = tab1[is_p],t2 = tab2[is_p];
1116 for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1118 real v = xr[0][idx];
1119 xr[0][idx] = v * t1;
1120 xr[1][idx] = v * t2;
1126 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1130 if(gr_info->block_type == 2)
1132 if(!gr_info->mixed_block_flag)
1137 sblim = gr_info->maxb-1;
1140 /* 31 alias-reduction operations between each pair of sub-bands */
1141 /* with 8 butterflies between each pair */
1145 real *xr1=(real *) xr[1];
1147 for(sb=sblim;sb;sb--,xr1+=10)
1150 real *cs=aa_cs,*ca=aa_ca;
1153 for(ss=7;ss>=0;ss--)
1154 { /* upper and lower butterfly inputs */
1155 register real bu = *--xr2,bd = *xr1;
1156 *xr2 = (bu * (*cs) ) - (bd * (*ca) );
1157 *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
1164 DCT inspired by Jeff Tsay's DCT from the maplay package
1165 this is an optimized version with manual unroll.
1168 [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1169 Mathematics of Computation, Volume 32, Number 141, January 1978,
1173 static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1176 register real *in = inbuf;
1178 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1179 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1180 in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8];
1181 in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5];
1182 in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2];
1183 in[2] +=in[1]; in[1] +=in[0];
1185 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1186 in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1];
1191 #define MACRO0(v) { \
1193 out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1194 out2[8-(v)] = tmp * w[26-(v)]; } \
1196 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1197 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)];
1198 #define MACRO1(v) { \
1200 sum0 = tmp1a + tmp2a; \
1201 sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1203 #define MACRO2(v) { \
1205 sum0 = tmp2a - tmp1a; \
1206 sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1209 register const real *c = COS9;
1210 register real *out2 = o2;
1211 register real *w = wintab;
1212 register real *out1 = o1;
1213 register real *ts = tsbuf;
1215 real ta33,ta66,tb33,tb66;
1217 ta33 = in[2*3+0] * c[3];
1218 ta66 = in[2*6+0] * c[6];
1219 tb33 = in[2*3+1] * c[3];
1220 tb66 = in[2*6+1] * c[6];
1223 real tmp1a,tmp2a,tmp1b,tmp2b;
1224 tmp1a = in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1225 tmp1b = in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1226 tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1227 tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1234 real tmp1a,tmp2a,tmp1b,tmp2b;
1235 tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1236 tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1237 tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1238 tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1245 real tmp1a,tmp2a,tmp1b,tmp2b;
1246 tmp1a = in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1247 tmp1b = in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1248 tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1249 tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1256 real tmp1a,tmp2a,tmp1b,tmp2b;
1257 tmp1a = in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1258 tmp1b = in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1259 tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1260 tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1268 sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1269 sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1280 static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1282 #define DCT12_PART1 \
1284 in5 += (in4 = in[4*3]); \
1285 in4 += (in3 = in[3*3]); \
1286 in3 += (in2 = in[2*3]); \
1287 in2 += (in1 = in[1*3]); \
1288 in1 += (in0 = in[0*3]); \
1290 in5 += in3; in3 += in1; \
1295 #define DCT12_PART2 \
1296 in0 += in4 * COS6_2; \
1301 in1 += in5 * COS6_2; \
1303 in5 = (in1 + in3) * tfcos12[0]; \
1304 in1 = (in1 - in3) * tfcos12[2]; \
1314 real in0,in1,in2,in3,in4,in5;
1315 register real *out1 = rawout1;
1316 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1317 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1322 real tmp0,tmp1 = (in0 - in4);
1324 real tmp2 = (in1 - in5) * tfcos12[1];
1328 ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1329 ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1330 ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1331 ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1336 ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1337 ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1338 ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1339 ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1341 ts[(6+0)*SBLIMIT] = out1[6+0] + in0 * wi[0];
1342 ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1343 ts[(6+2)*SBLIMIT] = out1[6+2] + in4 * wi[2];
1344 ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1350 real in0,in1,in2,in3,in4,in5;
1351 register real *out2 = rawout2;
1356 real tmp0,tmp1 = (in0 - in4);
1358 real tmp2 = (in1 - in5) * tfcos12[1];
1362 out2[5-1] = tmp0 * wi[11-1];
1363 out2[0+1] = tmp0 * wi[6+1];
1364 ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1365 ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1370 out2[5-0] = in2 * wi[11-0];
1371 out2[0+0] = in2 * wi[6+0];
1372 out2[0+2] = in3 * wi[6+2];
1373 out2[5-2] = in3 * wi[11-2];
1375 ts[(12+0)*SBLIMIT] += in0 * wi[0];
1376 ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1377 ts[(12+2)*SBLIMIT] += in4 * wi[2];
1378 ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1384 real in0,in1,in2,in3,in4,in5;
1385 register real *out2 = rawout2;
1386 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1391 real tmp0,tmp1 = (in0 - in4);
1393 real tmp2 = (in1 - in5) * tfcos12[1];
1397 out2[11-1] = tmp0 * wi[11-1];
1398 out2[6 +1] = tmp0 * wi[6+1];
1399 out2[0+1] += tmp1 * wi[1];
1400 out2[5-1] += tmp1 * wi[5-1];
1405 out2[11-0] = in2 * wi[11-0];
1406 out2[6 +0] = in2 * wi[6+0];
1407 out2[6 +2] = in3 * wi[6+2];
1408 out2[11-2] = in3 * wi[11-2];
1410 out2[0+0] += in0 * wi[0];
1411 out2[5-0] += in0 * wi[5-0];
1412 out2[0+2] += in4 * wi[2];
1413 out2[5-2] += in4 * wi[5-2];
1420 static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1421 int ch,struct gr_info_s *gr_info,struct mpstr *mp)
1423 real *tspnt = (real *) tsOut;
1424 real (*block)[2][SBLIMIT*SSLIMIT] = mp->hybrid_block;
1425 int *blc = mp->hybrid_blc;
1426 real *rawout1,*rawout2;
1428 unsigned int sb = 0;
1432 rawout1=block[b][ch];
1434 rawout2=block[b][ch];
1439 if(gr_info->mixed_block_flag) {
1441 dct36(fsIn[0],rawout1,rawout2,win[0],tspnt);
1442 dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1443 rawout1 += 36; rawout2 += 36; tspnt += 2;
1446 bt = gr_info->block_type;
1448 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1449 dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt);
1450 dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1454 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1455 dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1456 dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1460 for(;sb<SBLIMIT;sb++,tspnt++) {
1462 for(i=0;i<SSLIMIT;i++) {
1463 tspnt[i*SBLIMIT] = *rawout1++;
1470 * main layer3 handler
1472 int do_layer3(struct frame *fr,unsigned char *pcm_sample,int *pcm_point)
1474 int gr, ch, ss,clip=0;
1475 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1476 struct III_sideinfo sideinfo;
1477 int stereo = fr->stereo;
1478 int single = fr->single;
1479 int ms_stereo,i_stereo;
1480 int sfreq = fr->sampling_frequency;
1481 int stereo1,granules;
1483 if(stereo == 1) { /* stream is mono */
1487 else if(single >= 0) /* stream is stereo, but force to mono */
1492 if(fr->mode == MPG_MD_JOINT_STEREO) {
1493 ms_stereo = fr->mode_ext & 0x2;
1494 i_stereo = fr->mode_ext & 0x1;
1497 ms_stereo = i_stereo = 0;
1501 if(!III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single))
1507 if(!III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single))
1510 FIXME("Not supported\n");
1514 if(set_pointer(fr->mp,sideinfo.main_data_begin) == MP3_ERR)
1517 for (gr=0;gr<granules;gr++)
1519 real hybridIn[2][SBLIMIT][SSLIMIT];
1520 real hybridOut[2][SSLIMIT][SBLIMIT];
1523 struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
1526 part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0);
1529 part2bits = III_get_scale_factors_1(scalefacs[0],gr_info);
1531 FIXME("Not supported\n");
1534 if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
1538 struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
1541 part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo);
1544 part2bits = III_get_scale_factors_1(scalefacs[1],gr_info);
1546 FIXME("Not supported\n");
1550 if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
1555 for(i=0;i<SBLIMIT*SSLIMIT;i++) {
1557 tmp0 = ((real *) hybridIn[0])[i];
1558 tmp1 = ((real *) hybridIn[1])[i];
1559 ((real *) hybridIn[0])[i] = tmp0 + tmp1;
1560 ((real *) hybridIn[1])[i] = tmp0 - tmp1;
1565 III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
1567 if(ms_stereo || i_stereo || (single == 3) ) {
1568 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
1569 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
1571 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
1577 register unsigned int i;
1578 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1579 for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++)
1580 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1585 register unsigned int i;
1586 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1587 for(i=0;i<SSLIMIT*gr_info->maxb;i++)
1594 for(ch=0;ch<stereo1;ch++) {
1595 struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
1596 III_antialias(hybridIn[ch],gr_info);
1597 III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr->mp);
1600 for(ss=0;ss<SSLIMIT;ss++) {
1602 clip += synth_1to1_mono(fr->mp,hybridOut[0][ss],pcm_sample,pcm_point);
1605 int p1 = *pcm_point;
1606 clip += synth_1to1(fr->mp,hybridOut[0][ss],0,pcm_sample,&p1);
1607 clip += synth_1to1(fr->mp,hybridOut[1][ss],1,pcm_sample,pcm_point);