msi: Update French translation.
[wine] / dlls / winemp3.acm / layer3.c
1 /*
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.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include <stdlib.h>
25 #include "wine/debug.h"
26 #include "mpg123.h"
27 #include "mpglib.h"
28 #include "huffman.h"
29
30 #define MPEG1
31 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
32
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];
39 static real COS9[9];
40 static real COS6_1,COS6_2;
41 static real tfcos36[9];
42 static real tfcos12[3];
43
44 struct bandInfoStruct {
45   short longIdx[23];
46   short longDiff[22];
47   short shortIdx[14];
48   short shortDiff[13];
49 };
50
51 static int longLimit[9][23];
52 static int shortLimit[9][14];
53
54 static const struct bandInfoStruct bandInfo[9] = {
55
56 /* MPEG 1.0 */
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} } ,
61
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} } ,
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} }  ,
71
72 /* MPEG 2.0 */
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 } } ,
77
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 } } ,
82
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 } } ,
87 /* MPEG 2.5 */
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} } ,
100 };
101
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];
107
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 */
110
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];
113
114 /*
115  * init tables for layer-3
116  */
117 void init_layer3(int down_sample_sblimit)
118 {
119   int i,j,k,l;
120
121   for(i=-256;i<118+4;i++)
122     gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
123
124   for(i=0;i<8207;i++)
125     ispow[i] = pow((double)i,(double)4.0/3.0);
126
127   for (i=0;i<8;i++)
128   {
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]);
131     aa_cs[i] = 1.0/sq;
132     aa_ca[i] = Ci[i]/sq;
133   }
134
135   for(i=0;i<18;i++)
136   {
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 );
139   }
140   for(i=0;i<6;i++)
141   {
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 );
147   }
148
149   for(i=0;i<9;i++)
150     COS9[i] = cos( M_PI / 18.0 * (double) i);
151
152   for(i=0;i<9;i++)
153     tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 );
154   for(i=0;i<3;i++)
155     tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 );
156
157   COS6_1 = cos( M_PI / 6.0 * (double) 1);
158   COS6_2 = cos( M_PI / 6.0 * (double) 2);
159
160   for(i=0;i<12;i++)
161   {
162     win[2][i]  = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 );
163     for(j=0;j<6;j++)
164       COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) );
165   }
166
167   for(j=0;j<4;j++) {
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];
173   }
174
175   for(i=0;i<16;i++)
176   {
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);
182
183     for(j=0;j<2;j++) {
184       double base = pow(2.0,-0.25*(j+1.0));
185       double p1=1.0,p2=1.0;
186       if(i > 0) {
187         if( i & 1 )
188           p1 = pow(base,(i+1.0)*0.5);
189         else
190           p2 = pow(base,i*0.5);
191       }
192       pow1_1[j][i] = p1;
193       pow2_1[j][i] = p2;
194       pow1_2[j][i] = M_SQRT2 * p1;
195       pow2_2[j][i] = M_SQRT2 * p2;
196     }
197   }
198
199   for(j=0;j<9;j++)
200   {
201    const struct bandInfoStruct *bi = &bandInfo[j];
202    int *mp;
203    int cb,lwin;
204    const short *bdf;
205
206    mp = map[j][0] = mapbuf0[j];
207    bdf = bi->longDiff;
208    for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
209      *mp++ = (*bdf) >> 1;
210      *mp++ = i;
211      *mp++ = 3;
212      *mp++ = cb;
213    }
214    bdf = bi->shortDiff+3;
215    for(cb=3;cb<13;cb++) {
216      int l = (*bdf++) >> 1;
217      for(lwin=0;lwin<3;lwin++) {
218        *mp++ = l;
219        *mp++ = i + lwin;
220        *mp++ = lwin;
221        *mp++ = cb;
222      }
223      i += 6*l;
224    }
225    mapend[j][0] = mp;
226
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++) {
232        *mp++ = l;
233        *mp++ = i + lwin;
234        *mp++ = lwin;
235        *mp++ = cb;
236      }
237      i += 6*l;
238    }
239    mapend[j][1] = mp;
240
241    mp = map[j][2] = mapbuf2[j];
242    bdf = bi->longDiff;
243    for(cb = 0; cb < 22 ; cb++) {
244      *mp++ = (*bdf++) >> 1;
245      *mp++ = cb;
246    }
247    mapend[j][2] = mp;
248
249   }
250
251   for(j=0;j<9;j++) {
252     for(i=0;i<23;i++) {
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;
256     }
257     for(i=0;i<14;i++) {
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;
261     }
262   }
263
264   for(i=0;i<5;i++) {
265     for(j=0;j<6;j++) {
266       for(k=0;k<6;k++) {
267         int n = k + j * 6 + i * 36;
268         i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
269       }
270     }
271   }
272   for(i=0;i<4;i++) {
273     for(j=0;j<4;j++) {
274       for(k=0;k<4;k++) {
275         int n = k + j * 4 + i * 16;
276         i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
277       }
278     }
279   }
280   for(i=0;i<4;i++) {
281     for(j=0;j<3;j++) {
282       int n = j + i * 3;
283       i_slen2[n+244] = i|(j<<3) | (5<<12);
284       n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
285     }
286   }
287
288   for(i=0;i<5;i++) {
289     for(j=0;j<5;j++) {
290       for(k=0;k<4;k++) {
291         for(l=0;l<4;l++) {
292           int n = l + k * 4 + j * 16 + i * 80;
293           n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
294         }
295       }
296     }
297   }
298   for(i=0;i<5;i++) {
299     for(j=0;j<5;j++) {
300       for(k=0;k<4;k++) {
301         int n = k + j * 4 + i * 20;
302         n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
303       }
304     }
305   }
306 }
307
308 /*
309  * read additional side information
310  */
311 #ifdef MPEG1
312 static int III_get_side_info_1(struct III_sideinfo *si,int stereo,
313  int ms_stereo,long sfreq,int single)
314 {
315    int ch, gr;
316    int powdiff = (single == 3) ? 4 : 0;
317
318    si->main_data_begin = getbits(9);
319    if (stereo == 1)
320      si->private_bits = getbits_fast(5);
321    else
322      si->private_bits = getbits_fast(3);
323
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);
327    }
328
329    for (gr=0; gr<2; gr++)
330    {
331      for (ch=0; ch<stereo; ch++)
332      {
333        register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
334
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;
340        }
341        gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
342        if(ms_stereo)
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 */
346        if(get1bit())
347        {
348          int i;
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);
353          /*
354           * table_select[2] not needed, because there is no region2,
355           * but to satisfy some verifications tools we set it either.
356           */
357          gr_info->table_select[2] = 0;
358          for(i=0;i<3;i++)
359            gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
360
361          if(gr_info->block_type == 0) {
362            FIXME("Blocktype == 0 and window-switching == 1 not allowed.\n");
363            return 0;
364          }
365          /* region_count/start parameters are implicit in this case. */
366          gr_info->region1start = 36>>1;
367          gr_info->region2start = 576>>1;
368        }
369        else
370        {
371          int i,r0c,r1c;
372          for (i=0; i<3; i++)
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;
380        }
381        gr_info->preflag = get1bit();
382        gr_info->scalefac_scale = get1bit();
383        gr_info->count1table_select = get1bit();
384      }
385    }
386    return !0;
387 }
388 #endif
389
390 /*
391  * Side Info for MPEG 2.0 / LSF
392  */
393 static int III_get_side_info_2(struct III_sideinfo *si,int stereo,
394  int ms_stereo,long sfreq,int single)
395 {
396    int ch;
397    int powdiff = (single == 3) ? 4 : 0;
398
399    si->main_data_begin = getbits(8);
400    if (stereo == 1)
401      si->private_bits = get1bit();
402    else
403      si->private_bits = getbits_fast(2);
404
405    for (ch=0; ch<stereo; ch++)
406    {
407        register struct gr_info_s *gr_info = &(si->ch[ch].gr[0]);
408
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;
414        }
415        gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
416        if(ms_stereo)
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 */
420        if(get1bit())
421        {
422          int i;
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);
427          /*
428           * table_select[2] not needed, because there is no region2,
429           * but to satisfy some verifications tools we set it either.
430           */
431          gr_info->table_select[2] = 0;
432          for(i=0;i<3;i++)
433            gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
434
435          if(gr_info->block_type == 0) {
436            FIXME("Blocktype == 0 and window-switching == 1 not allowed.\n");
437            return 0;
438          }
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;
443          else if(sfreq == 8)
444 /* check this for 2.5 and sfreq=8 */
445            gr_info->region1start = 108>>1;
446          else
447            gr_info->region1start = 54>>1;
448          gr_info->region2start = 576>>1;
449        }
450        else
451        {
452          int i,r0c,r1c;
453          for (i=0; i<3; i++)
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;
461        }
462        gr_info->scalefac_scale = get1bit();
463        gr_info->count1table_select = get1bit();
464    }
465    return !0;
466 }
467
468 /*
469  * read scalefactors
470  */
471 #ifdef MPEG1
472 static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
473 {
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}
477    };
478    int numbits;
479    int num0 = slen[0][gr_info->scalefac_compress];
480    int num1 = slen[1][gr_info->scalefac_compress];
481
482     if (gr_info->block_type == 2) {
483       int i=18;
484       numbits = (num0 + num1) * 18;
485
486       if (gr_info->mixed_block_flag) {
487          for (i=8;i;i--)
488            *scf++ = getbits_fast(num0);
489          i = 9;
490          numbits -= num0; /* num0 * 17 + num1 * 18 */
491       }
492
493       for (;i;i--)
494         *scf++ = getbits_fast(num0);
495       for (i = 18; i; i--)
496         *scf++ = getbits_fast(num1);
497       *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
498     }
499     else {
500       int i;
501       int scfsi = gr_info->scfsi;
502
503       if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
504          for(i=11;i;i--)
505            *scf++ = getbits_fast(num0);
506          for(i=10;i;i--)
507            *scf++ = getbits_fast(num1);
508          numbits = (num0 + num1) * 10 + num0;
509          *scf++ = 0;
510       }
511       else {
512         numbits = 0;
513         if(!(scfsi & 0x8)) {
514           for (i=0;i<6;i++)
515             *scf++ = getbits_fast(num0);
516           numbits += num0 * 6;
517         }
518         else {
519           scf += 6;
520         }
521
522         if(!(scfsi & 0x4)) {
523           for (i=0;i<5;i++)
524             *scf++ = getbits_fast(num0);
525           numbits += num0 * 5;
526         }
527         else {
528           scf += 5;
529         }
530
531         if(!(scfsi & 0x2)) {
532           for(i=0;i<5;i++)
533             *scf++ = getbits_fast(num1);
534           numbits += num1 * 5;
535         }
536         else {
537           scf += 5;
538         }
539
540         if(!(scfsi & 0x1)) {
541           for (i=0;i<5;i++)
542             *scf++ = getbits_fast(num1);
543           numbits += num1 * 5;
544         }
545         else {
546            scf += 5;
547         }
548         *scf++ = 0;  /* no l[21] in original sources */
549       }
550     }
551     return numbits;
552 }
553 #endif
554
555
556 static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
557 {
558   const unsigned char *pnt;
559   int i,j;
560   unsigned int slen;
561   int n = 0;
562   int numbits = 0;
563
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} } };
571
572   if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
573     slen = i_slen2[gr_info->scalefac_compress>>1];
574   else
575     slen = n_slen2[gr_info->scalefac_compress];
576
577   gr_info->preflag = (slen>>15) & 0x1;
578
579   n = 0;
580   if( gr_info->block_type == 2 ) {
581     n++;
582     if(gr_info->mixed_block_flag)
583       n++;
584   }
585
586   pnt = stab[n][(slen>>12)&0x7];
587
588   for(i=0;i<4;i++) {
589     int num = slen & 0x7;
590     slen >>= 3;
591     if(num) {
592       for(j=0;j<(int)(pnt[i]);j++)
593         *scf++ = getbits_fast(num);
594       numbits += pnt[i] * num;
595     }
596     else {
597       for(j=0;j<(int)(pnt[i]);j++)
598         *scf++ = 0;
599     }
600   }
601
602   n = (n << 1) + 1;
603   for(i=0;i<n;i++)
604     *scf++ = 0;
605
606   return numbits;
607 }
608
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};
611
612 static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
613    struct gr_info_s *gr_info,int sfreq,int part2bits)
614 {
615   int shift = 1 + gr_info->scalefac_scale;
616   real *xrpnt = (real *) xr;
617   int l[3],l3;
618   int part2remain = gr_info->part2_3_length - part2bits;
619   int *me;
620
621   {
622     int bv       = gr_info->big_values;
623     int region1  = gr_info->region1start;
624     int region2  = gr_info->region2start;
625
626     l3 = ((576>>1)-bv)>>1;
627 /*
628  * we may lose the 'odd' bit here !!
629  * check this later again
630  */
631     if(bv <= region1) {
632       l[0] = bv; l[1] = 0; l[2] = 0;
633     }
634     else {
635       l[0] = region1;
636       if(bv <= region2) {
637         l[1] = bv - l[0];  l[2] = 0;
638       }
639       else {
640         l[1] = region2 - l[0]; l[2] = bv - region2;
641       }
642     }
643   }
644
645   if(gr_info->block_type == 2) {
646     /*
647      * decoding with short or mixed mode BandIndex table
648      */
649     int i,max[4];
650     int step=0,lwin=0,cb=0;
651     register real v = 0.0;
652     register int *m,mc;
653
654     if(gr_info->mixed_block_flag) {
655       max[3] = -1;
656       max[0] = max[1] = max[2] = 2;
657       m = map[sfreq][0];
658       me = mapend[sfreq][0];
659     }
660     else {
661       max[0] = max[1] = max[2] = max[3] = -1;
662       /* max[3] not really needed in this case */
663       m = map[sfreq][1];
664       me = mapend[sfreq][1];
665     }
666
667     mc = 0;
668     for(i=0;i<2;i++) {
669       int lp = l[i];
670       const struct newhuff *h = ht+gr_info->table_select[i];
671       for(;lp;lp--,mc--) {
672         register int x,y;
673         if( (!mc) ) {
674           mc = *m++;
675           xrpnt = ((real *) xr) + (*m++);
676           lwin = *m++;
677           cb = *m++;
678           if(lwin == 3) {
679             v = gr_info->pow2gain[(*scf++) << shift];
680             step = 1;
681           }
682           else {
683             v = gr_info->full_gain[lwin][(*scf++) << shift];
684             step = 3;
685           }
686         }
687         {
688           register const short *val = h->table;
689           while((y=*val++)<0) {
690             if (get1bit())
691               val -= y;
692             part2remain--;
693           }
694           x = y >> 4;
695           y &= 0xf;
696         }
697         if(x == 15) {
698           max[lwin] = cb;
699           part2remain -= h->linbits+1;
700           x += getbits(h->linbits);
701           if(get1bit())
702             *xrpnt = -ispow[x] * v;
703           else
704             *xrpnt =  ispow[x] * v;
705         }
706         else if(x) {
707           max[lwin] = cb;
708           if(get1bit())
709             *xrpnt = -ispow[x] * v;
710           else
711             *xrpnt =  ispow[x] * v;
712           part2remain--;
713         }
714         else
715           *xrpnt = 0.0;
716         xrpnt += step;
717         if(y == 15) {
718           max[lwin] = cb;
719           part2remain -= h->linbits+1;
720           y += getbits(h->linbits);
721           if(get1bit())
722             *xrpnt = -ispow[y] * v;
723           else
724             *xrpnt =  ispow[y] * v;
725         }
726         else if(y) {
727           max[lwin] = cb;
728           if(get1bit())
729             *xrpnt = -ispow[y] * v;
730           else
731             *xrpnt =  ispow[y] * v;
732           part2remain--;
733         }
734         else
735           *xrpnt = 0.0;
736         xrpnt += step;
737       }
738     }
739     for(;l3 && (part2remain > 0);l3--) {
740       const struct newhuff *h = htc+gr_info->count1table_select;
741       const short *val = h->table;
742       short a;
743
744       while((a=*val++)<0) {
745         part2remain--;
746         if(part2remain < 0) {
747           part2remain++;
748           a = 0;
749           break;
750         }
751         if (get1bit())
752           val -= a;
753       }
754
755       for(i=0;i<4;i++) {
756         if(!(i & 1)) {
757           if(!mc) {
758             mc = *m++;
759             xrpnt = ((real *) xr) + (*m++);
760             lwin = *m++;
761             cb = *m++;
762             if(lwin == 3) {
763               v = gr_info->pow2gain[(*scf++) << shift];
764               step = 1;
765             }
766             else {
767               v = gr_info->full_gain[lwin][(*scf++) << shift];
768               step = 3;
769             }
770           }
771           mc--;
772         }
773         if( (a & (0x8>>i)) ) {
774           max[lwin] = cb;
775           part2remain--;
776           if(part2remain < 0) {
777             part2remain++;
778             break;
779           }
780           if(get1bit())
781             *xrpnt = -v;
782           else
783             *xrpnt = v;
784         }
785         else
786           *xrpnt = 0.0;
787         xrpnt += step;
788       }
789     }
790
791     while( m < me ) {
792       if(!mc) {
793         mc = *m++;
794         xrpnt = ((real *) xr) + *m++;
795         if( (*m++) == 3)
796           step = 1;
797         else
798           step = 3;
799         m++; /* cb */
800       }
801       mc--;
802       *xrpnt = 0.0;
803       xrpnt += step;
804       *xrpnt = 0.0;
805       xrpnt += step;
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
810  */
811     }
812
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;
817
818     {
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];
822     }
823
824   }
825   else {
826         /*
827      * decoding with 'long' BandIndex table (block_type != 2)
828      */
829     const int *pretab = gr_info->preflag ? pretab1 : pretab2;
830     int i,max = -1;
831     int cb = 0;
832     register int *m = map[sfreq][2];
833     register real v = 0.0;
834     register int mc = 0;
835 #if 0
836     me = mapend[sfreq][2];
837 #endif
838
839         /*
840      * long hash table values
841      */
842     for(i=0;i<3;i++) {
843       int lp = l[i];
844       const struct newhuff *h = ht+gr_info->table_select[i];
845
846       for(;lp;lp--,mc--) {
847         int x,y;
848
849         if(!mc) {
850           mc = *m++;
851           v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
852           cb = *m++;
853         }
854         {
855           register const short *val = h->table;
856           while((y=*val++)<0) {
857             if (get1bit())
858               val -= y;
859             part2remain--;
860           }
861           x = y >> 4;
862           y &= 0xf;
863         }
864         if (x == 15) {
865           max = cb;
866           part2remain -= h->linbits+1;
867           x += getbits(h->linbits);
868           if(get1bit())
869             *xrpnt++ = -ispow[x] * v;
870           else
871             *xrpnt++ =  ispow[x] * v;
872         }
873         else if(x) {
874           max = cb;
875           if(get1bit())
876             *xrpnt++ = -ispow[x] * v;
877           else
878             *xrpnt++ =  ispow[x] * v;
879           part2remain--;
880         }
881         else
882           *xrpnt++ = 0.0;
883
884         if (y == 15) {
885           max = cb;
886           part2remain -= h->linbits+1;
887           y += getbits(h->linbits);
888           if(get1bit())
889             *xrpnt++ = -ispow[y] * v;
890           else
891             *xrpnt++ =  ispow[y] * v;
892         }
893         else if(y) {
894           max = cb;
895           if(get1bit())
896             *xrpnt++ = -ispow[y] * v;
897           else
898             *xrpnt++ =  ispow[y] * v;
899           part2remain--;
900         }
901         else
902           *xrpnt++ = 0.0;
903       }
904     }
905
906         /*
907      * short (count1table) values
908      */
909     for(;l3 && (part2remain > 0);l3--) {
910       const struct newhuff *h = htc+gr_info->count1table_select;
911       const short *val = h->table;
912       short a;
913
914       while((a=*val++)<0) {
915         part2remain--;
916         if(part2remain < 0) {
917           part2remain++;
918           a = 0;
919           break;
920         }
921         if (get1bit())
922           val -= a;
923       }
924
925       for(i=0;i<4;i++) {
926         if(!(i & 1)) {
927           if(!mc) {
928             mc = *m++;
929             cb = *m++;
930             v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
931           }
932           mc--;
933         }
934         if ( (a & (0x8>>i)) ) {
935           max = cb;
936           part2remain--;
937           if(part2remain < 0) {
938             part2remain++;
939             break;
940           }
941           if(get1bit())
942             *xrpnt++ = -v;
943           else
944             *xrpnt++ = v;
945         }
946         else
947           *xrpnt++ = 0.0;
948       }
949     }
950
951         /*
952      * zero part
953      */
954     for(i=(&xr[SBLIMIT][0]-xrpnt)>>1;i;i--) {
955       *xrpnt++ = 0.0;
956       *xrpnt++ = 0.0;
957     }
958
959     gr_info->maxbandl = max+1;
960     gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
961   }
962
963   while( part2remain > 16 ) {
964     getbits(16); /* Dismiss stuffing Bits */
965     part2remain -= 16;
966   }
967   if(part2remain > 0)
968     getbits(part2remain);
969   else if(part2remain < 0) {
970     FIXME("mpg123: Can't rewind stream by %d bits!\n",-part2remain);
971     return 1; /* -> error */
972   }
973   return 0;
974 }
975
976 /*
977  * III_stereo: calculate real channel values for Joint-I-Stereo-mode
978  */
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)
981 {
982       real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
983       const struct bandInfoStruct *bi = &bandInfo[sfreq];
984       real *tab1,*tab2;
985
986       if(lsf) {
987         int p = gr_info->scalefac_compress & 0x1;
988             if(ms_stereo) {
989           tab1 = pow1_2[p]; tab2 = pow2_2[p];
990         }
991         else {
992           tab1 = pow1_1[p]; tab2 = pow2_1[p];
993         }
994       }
995       else {
996         if(ms_stereo) {
997           tab1 = tan1_2; tab2 = tan2_2;
998         }
999         else {
1000           tab1 = tan1_1; tab2 = tan2_1;
1001         }
1002       }
1003
1004       if (gr_info->block_type == 2)
1005       {
1006          int lwin,do_l = 0;
1007          if( gr_info->mixed_block_flag )
1008            do_l = 1;
1009
1010          for (lwin=0;lwin<3;lwin++) /* process each window */
1011          {
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 */
1014            if(sfb > 3)
1015              do_l = 0;
1016
1017            for(;sfb<12;sfb++)
1018            {
1019              is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1020              if(is_p != 7) {
1021                real t1,t2;
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)
1026                {
1027                  real v = xr[0][idx];
1028                  xr[0][idx] = v * t1;
1029                  xr[1][idx] = v * t2;
1030                }
1031              }
1032            }
1033
1034 #if 1
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;
1040 #else
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;
1044 #endif
1045            if(is_p != 7)
1046            {
1047              real t1,t2;
1048              t1 = tab1[is_p]; t2 = tab2[is_p];
1049              for ( ; sb > 0; sb--,idx+=3 )
1050              {
1051                real v = xr[0][idx];
1052                xr[0][idx] = v * t1;
1053                xr[1][idx] = v * t2;
1054              }
1055            }
1056          } /* end for(lwin; .. ; . ) */
1057
1058          if (do_l)
1059          {
1060 /* also check l-part, if ALL bands in the three windows are 'empty'
1061  * and mode = mixed_mode
1062  */
1063            int sfb = gr_info->maxbandl;
1064            int idx = bi->longIdx[sfb];
1065
1066            for ( ; sfb<8; sfb++ )
1067            {
1068              int sb = bi->longDiff[sfb];
1069              int is_p = scalefac[sfb]; /* scale: 0-15 */
1070              if(is_p != 7) {
1071                real t1,t2;
1072                t1 = tab1[is_p]; t2 = tab2[is_p];
1073                for ( ; sb > 0; sb--,idx++)
1074                {
1075                  real v = xr[0][idx];
1076                  xr[0][idx] = v * t1;
1077                  xr[1][idx] = v * t2;
1078                }
1079              }
1080              else
1081                idx += sb;
1082            }
1083          }
1084       }
1085       else /* ((gr_info->block_type != 2)) */
1086       {
1087         int sfb = gr_info->maxbandl;
1088         int is_p,idx = bi->longIdx[sfb];
1089         for ( ; sfb<21; sfb++)
1090         {
1091           int sb = bi->longDiff[sfb];
1092           is_p = scalefac[sfb]; /* scale: 0-15 */
1093           if(is_p != 7) {
1094             real t1,t2;
1095             t1 = tab1[is_p]; t2 = tab2[is_p];
1096             for ( ; sb > 0; sb--,idx++)
1097             {
1098                real v = xr[0][idx];
1099                xr[0][idx] = v * t1;
1100                xr[1][idx] = v * t2;
1101             }
1102           }
1103           else
1104             idx += sb;
1105         }
1106
1107         is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1108         if(is_p != 7)
1109         {
1110           int sb;
1111           real t1 = tab1[is_p],t2 = tab2[is_p];
1112
1113           for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1114           {
1115             real v = xr[0][idx];
1116             xr[0][idx] = v * t1;
1117             xr[1][idx] = v * t2;
1118           }
1119         }
1120       } /* ... */
1121 }
1122
1123 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1124 {
1125    int sblim;
1126
1127    if(gr_info->block_type == 2)
1128    {
1129       if(!gr_info->mixed_block_flag)
1130         return;
1131       sblim = 1;
1132    }
1133    else {
1134      sblim = gr_info->maxb-1;
1135    }
1136
1137    /* 31 alias-reduction operations between each pair of sub-bands */
1138    /* with 8 butterflies between each pair                         */
1139
1140    {
1141      int sb;
1142      real *xr1=(real *) xr[1];
1143
1144      for(sb=sblim;sb;sb--,xr1+=10)
1145      {
1146        int ss;
1147        real *cs=aa_cs,*ca=aa_ca;
1148        real *xr2 = xr1;
1149
1150        for(ss=7;ss>=0;ss--)
1151        {       /* upper and lower butterfly inputs */
1152          register real bu = *--xr2,bd = *xr1;
1153          *xr2   = (bu * (*cs)   ) - (bd * (*ca)   );
1154          *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
1155        }
1156      }
1157   }
1158 }
1159
1160 /*
1161  DCT inspired by Jeff Tsay's DCT from the maplay package
1162  this is an optimized version with manual unroll.
1163
1164  References:
1165  [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1166      Mathematics of Computation, Volume 32, Number 141, January 1978,
1167      Pages 175-199
1168 */
1169
1170 static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1171 {
1172   {
1173     register real *in = inbuf;
1174
1175     in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1176     in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1177     in[11]+=in[10]; in[10]+=in[9];  in[9] +=in[8];
1178     in[8] +=in[7];  in[7] +=in[6];  in[6] +=in[5];
1179     in[5] +=in[4];  in[4] +=in[3];  in[3] +=in[2];
1180     in[2] +=in[1];  in[1] +=in[0];
1181
1182     in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1183     in[9] +=in[7];  in[7] +=in[5];  in[5] +=in[3];  in[3] +=in[1];
1184
1185
1186   {
1187
1188 #define MACRO0(v) { \
1189     real tmp; \
1190     out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1191     out2[8-(v)] = tmp * w[26-(v)];  } \
1192     sum0 -= sum1; \
1193     ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1194     ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)];
1195 #define MACRO1(v) { \
1196         real sum0,sum1; \
1197     sum0 = tmp1a + tmp2a; \
1198         sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1199         MACRO0(v); }
1200 #define MACRO2(v) { \
1201     real sum0,sum1; \
1202     sum0 = tmp2a - tmp1a; \
1203     sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1204         MACRO0(v); }
1205
1206     register const real *c = COS9;
1207     register real *out2 = o2;
1208         register real *w = wintab;
1209         register real *out1 = o1;
1210         register real *ts = tsbuf;
1211
1212     real ta33,ta66,tb33,tb66;
1213
1214     ta33 = in[2*3+0] * c[3];
1215     ta66 = in[2*6+0] * c[6];
1216     tb33 = in[2*3+1] * c[3];
1217     tb66 = in[2*6+1] * c[6];
1218
1219     {
1220       real tmp1a,tmp2a,tmp1b,tmp2b;
1221       tmp1a =             in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1222       tmp1b =             in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1223       tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1224       tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1225
1226       MACRO1(0);
1227       MACRO2(8);
1228     }
1229
1230     {
1231       real tmp1a,tmp2a,tmp1b,tmp2b;
1232       tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1233       tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1234       tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1235       tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1236
1237       MACRO1(1);
1238       MACRO2(7);
1239     }
1240
1241     {
1242       real tmp1a,tmp2a,tmp1b,tmp2b;
1243       tmp1a =             in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1244       tmp1b =             in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1245       tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1246       tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1247
1248       MACRO1(2);
1249       MACRO2(6);
1250     }
1251
1252     {
1253       real tmp1a,tmp2a,tmp1b,tmp2b;
1254       tmp1a =             in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1255       tmp1b =             in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1256       tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1257       tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1258
1259       MACRO1(3);
1260       MACRO2(5);
1261     }
1262
1263         {
1264                 real sum0,sum1;
1265         sum0 =  in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1266         sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1267                 MACRO0(4);
1268         }
1269   }
1270
1271   }
1272 }
1273
1274 /*
1275  * new DCT12
1276  */
1277 static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1278 {
1279 #define DCT12_PART1 \
1280              in5 = in[5*3];  \
1281      in5 += (in4 = in[4*3]); \
1282      in4 += (in3 = in[3*3]); \
1283      in3 += (in2 = in[2*3]); \
1284      in2 += (in1 = in[1*3]); \
1285      in1 += (in0 = in[0*3]); \
1286                              \
1287      in5 += in3; in3 += in1; \
1288                              \
1289      in2 *= COS6_1; \
1290      in3 *= COS6_1; \
1291
1292 #define DCT12_PART2 \
1293      in0 += in4 * COS6_2; \
1294                           \
1295      in4 = in0 + in2;     \
1296      in0 -= in2;          \
1297                           \
1298      in1 += in5 * COS6_2; \
1299                           \
1300      in5 = (in1 + in3) * tfcos12[0]; \
1301      in1 = (in1 - in3) * tfcos12[2]; \
1302                          \
1303      in3 = in4 + in5;    \
1304      in4 -= in5;         \
1305                          \
1306      in2 = in0 + in1;    \
1307      in0 -= in1;
1308
1309
1310    {
1311      real in0,in1,in2,in3,in4,in5;
1312      register real *out1 = rawout1;
1313      ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1314      ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1315
1316      DCT12_PART1
1317
1318      {
1319        real tmp0,tmp1 = (in0 - in4);
1320        {
1321          real tmp2 = (in1 - in5) * tfcos12[1];
1322          tmp0 = tmp1 + tmp2;
1323          tmp1 -= tmp2;
1324        }
1325        ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1326        ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1327        ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1328        ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1329      }
1330
1331      DCT12_PART2
1332
1333      ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1334      ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1335      ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1336      ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1337
1338      ts[(6+0)*SBLIMIT]  = out1[6+0] + in0 * wi[0];
1339      ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1340      ts[(6+2)*SBLIMIT]  = out1[6+2] + in4 * wi[2];
1341      ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1342   }
1343
1344   in++;
1345
1346   {
1347      real in0,in1,in2,in3,in4,in5;
1348      register real *out2 = rawout2;
1349
1350      DCT12_PART1
1351
1352      {
1353        real tmp0,tmp1 = (in0 - in4);
1354        {
1355          real tmp2 = (in1 - in5) * tfcos12[1];
1356          tmp0 = tmp1 + tmp2;
1357          tmp1 -= tmp2;
1358        }
1359        out2[5-1] = tmp0 * wi[11-1];
1360        out2[0+1] = tmp0 * wi[6+1];
1361        ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1362        ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1363      }
1364
1365      DCT12_PART2
1366
1367      out2[5-0] = in2 * wi[11-0];
1368      out2[0+0] = in2 * wi[6+0];
1369      out2[0+2] = in3 * wi[6+2];
1370      out2[5-2] = in3 * wi[11-2];
1371
1372      ts[(12+0)*SBLIMIT] += in0 * wi[0];
1373      ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1374      ts[(12+2)*SBLIMIT] += in4 * wi[2];
1375      ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1376   }
1377
1378   in++;
1379
1380   {
1381      real in0,in1,in2,in3,in4,in5;
1382      register real *out2 = rawout2;
1383      out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1384
1385      DCT12_PART1
1386
1387      {
1388        real tmp0,tmp1 = (in0 - in4);
1389        {
1390          real tmp2 = (in1 - in5) * tfcos12[1];
1391          tmp0 = tmp1 + tmp2;
1392          tmp1 -= tmp2;
1393        }
1394        out2[11-1] = tmp0 * wi[11-1];
1395        out2[6 +1] = tmp0 * wi[6+1];
1396        out2[0+1] += tmp1 * wi[1];
1397        out2[5-1] += tmp1 * wi[5-1];
1398      }
1399
1400      DCT12_PART2
1401
1402      out2[11-0] = in2 * wi[11-0];
1403      out2[6 +0] = in2 * wi[6+0];
1404      out2[6 +2] = in3 * wi[6+2];
1405      out2[11-2] = in3 * wi[11-2];
1406
1407      out2[0+0] += in0 * wi[0];
1408      out2[5-0] += in0 * wi[5-0];
1409      out2[0+2] += in4 * wi[2];
1410      out2[5-2] += in4 * wi[5-2];
1411   }
1412 }
1413
1414 /*
1415  * III_hybrid
1416  */
1417 static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1418    int ch,struct gr_info_s *gr_info,struct mpstr *mp)
1419 {
1420    real *tspnt = (real *) tsOut;
1421    real (*block)[2][SBLIMIT*SSLIMIT] = mp->hybrid_block;
1422    int *blc = mp->hybrid_blc;
1423    real *rawout1,*rawout2;
1424    int bt;
1425    unsigned int sb = 0;
1426
1427    {
1428      int b = blc[ch];
1429      rawout1=block[b][ch];
1430      b=-b+1;
1431      rawout2=block[b][ch];
1432      blc[ch] = b;
1433    }
1434
1435
1436    if(gr_info->mixed_block_flag) {
1437      sb = 2;
1438      dct36(fsIn[0],rawout1,rawout2,win[0],tspnt);
1439      dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1440      rawout1 += 36; rawout2 += 36; tspnt += 2;
1441    }
1442
1443    bt = gr_info->block_type;
1444    if(bt == 2) {
1445      for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1446        dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt);
1447        dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1448      }
1449    }
1450    else {
1451      for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1452        dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1453        dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1454      }
1455    }
1456
1457    for(;sb<SBLIMIT;sb++,tspnt++) {
1458      int i;
1459      for(i=0;i<SSLIMIT;i++) {
1460        tspnt[i*SBLIMIT] = *rawout1++;
1461        *rawout2++ = 0.0;
1462      }
1463    }
1464 }
1465
1466 /*
1467  * main layer3 handler
1468  */
1469 int do_layer3(struct frame *fr,unsigned char *pcm_sample,int *pcm_point)
1470 {
1471   int gr, ch, ss,clip=0;
1472   int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1473   struct III_sideinfo sideinfo;
1474   int stereo = fr->stereo;
1475   int single = fr->single;
1476   int ms_stereo,i_stereo;
1477   int sfreq = fr->sampling_frequency;
1478   int stereo1,granules;
1479
1480   if(stereo == 1) { /* stream is mono */
1481     stereo1 = 1;
1482     single = 0;
1483   }
1484   else if(single >= 0) /* stream is stereo, but force to mono */
1485     stereo1 = 1;
1486   else
1487     stereo1 = 2;
1488
1489   if(fr->mode == MPG_MD_JOINT_STEREO) {
1490     ms_stereo = fr->mode_ext & 0x2;
1491     i_stereo  = fr->mode_ext & 0x1;
1492   }
1493   else
1494     ms_stereo = i_stereo = 0;
1495
1496   if(fr->lsf) {
1497     granules = 1;
1498     if(!III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single))
1499       return -1;
1500   }
1501   else {
1502     granules = 2;
1503 #ifdef MPEG1
1504     if(!III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single))
1505       return -1;
1506 #else
1507     FIXME("Not supported\n");
1508 #endif
1509   }
1510
1511   if(set_pointer(fr->mp,sideinfo.main_data_begin) == MP3_ERR)
1512     return -1;
1513
1514   for (gr=0;gr<granules;gr++)
1515   {
1516     real hybridIn[2][SBLIMIT][SSLIMIT];
1517     real hybridOut[2][SSLIMIT][SBLIMIT];
1518
1519     {
1520       struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
1521       long part2bits;
1522       if(fr->lsf)
1523         part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0);
1524       else {
1525 #ifdef MPEG1
1526         part2bits = III_get_scale_factors_1(scalefacs[0],gr_info);
1527 #else
1528         FIXME("Not supported\n");
1529 #endif
1530       }
1531       if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
1532         return clip;
1533     }
1534     if(stereo == 2) {
1535       struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
1536       long part2bits;
1537       if(fr->lsf)
1538         part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo);
1539       else {
1540 #ifdef MPEG1
1541         part2bits = III_get_scale_factors_1(scalefacs[1],gr_info);
1542 #else
1543         FIXME("Not supported\n");
1544 #endif
1545       }
1546
1547       if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
1548           return clip;
1549
1550       if(ms_stereo) {
1551         int i;
1552         for(i=0;i<SBLIMIT*SSLIMIT;i++) {
1553           real tmp0,tmp1;
1554           tmp0 = ((real *) hybridIn[0])[i];
1555           tmp1 = ((real *) hybridIn[1])[i];
1556           ((real *) hybridIn[0])[i] = tmp0 + tmp1;
1557           ((real *) hybridIn[1])[i] = tmp0 - tmp1;
1558         }
1559       }
1560
1561       if(i_stereo)
1562         III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
1563
1564       if(ms_stereo || i_stereo || (single == 3) ) {
1565         if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
1566           sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
1567         else
1568           gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
1569       }
1570
1571       switch(single) {
1572         case 3:
1573           {
1574             register unsigned int i;
1575             register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1576             for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++)
1577               *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1578           }
1579           break;
1580         case 1:
1581           {
1582             register unsigned int i;
1583             register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1584             for(i=0;i<SSLIMIT*gr_info->maxb;i++)
1585               *in0++ = *in1++;
1586           }
1587           break;
1588       }
1589     }
1590
1591     for(ch=0;ch<stereo1;ch++) {
1592       struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
1593       III_antialias(hybridIn[ch],gr_info);
1594       III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr->mp);
1595     }
1596
1597     for(ss=0;ss<SSLIMIT;ss++) {
1598       if(single >= 0) {
1599         clip += synth_1to1_mono(fr->mp,hybridOut[0][ss],pcm_sample,pcm_point);
1600       }
1601       else {
1602         int p1 = *pcm_point;
1603         clip += synth_1to1(fr->mp,hybridOut[0][ss],0,pcm_sample,&p1);
1604         clip += synth_1to1(fr->mp,hybridOut[1][ss],1,pcm_sample,pcm_point);
1605       }
1606     }
1607   }
1608
1609   return clip;
1610 }