Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 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
 *
7
 * - I'm currently working on that .. needs a few more optimizations,
8
 *   though the code is now fast enough to run in realtime on a 100Mhz 486
9
 * - a few personal notes are in german ..
10
 *
11
 * used source:
12
 *   mpeg1_iis package
13
 */
14
 
15
#include "mpg123.h"
16
#include "huffman.h"
17
 
18
#if 0
19
#include "get1bit.h"
20
#endif
21
 
22
static real ispow[8207];
23
static real aa_ca[8],aa_cs[8];
24
static real COS1[12][6];
25
static real win[4][36];
26
static real win1[4][36];
27
static real gainpow2[256+118+4];
28
static real COS9[9];
29
static real COS6_1,COS6_2;
30
static real tfcos36[9];
31
static real tfcos12[3];
32
#ifdef NEW_DCT9
33
static real cos9[3],cos18[3];
34
#endif
35
 
36
struct bandInfoStruct {
37
  int longIdx[23];
38
  int longDiff[22];
39
  int shortIdx[14];
40
  int shortDiff[13];
41
};
42
 
43
int longLimit[7][23];
44
int shortLimit[7][14];
45
 
46
struct bandInfoStruct bandInfo[7] = {
47
 
48
/* MPEG 1.0 */
49
 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
50
   {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
51
   {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},
52
   {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
53
 
54
 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
55
   {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
56
   {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},
57
   {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
58
 
59
 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
60
   {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
61
   {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} ,
62
   {4,4,4,4,6,8,12,16,20,26,34,42,12} }  ,
63
 
64
/* MPEG 2.0 */
65
 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
66
   {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
67
   {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} ,
68
   {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
69
 
70
 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
71
   {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
72
   {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} ,
73
   {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
74
 
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,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3},
78
   {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
79
 
80
/* MPEG 2.5, wrong! table (it's just a copy of MPEG 2.0/44.1kHz) */
81
 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
82
   {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
83
   {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} ,
84
   {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
85
};
86
 
87
static int mapbuf0[7][152];
88
static int mapbuf1[7][156];
89
static int mapbuf2[7][44];
90
static int *map[7][3];
91
static int *mapend[7][3];
92
 
93
static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
94
static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
95
 
96
static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
97
static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
98
 
99
/*
100
 * init tables for layer-3
101
 */
102
void init_layer3(int down_samp)
103
{
104
  int i,j,k,l;
105
 
106
  for(i=-256;i<118+4;i++)
107
    gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
108
 
109
  for(i=0;i<8207;i++)
110
    ispow[i] = pow((double)i,(double)4.0/3.0);
111
 
112
  for (i=0;i<8;i++)
113
  {
114
    static double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
115
    double sq=sqrt(1.0+Ci[i]*Ci[i]);
116
    aa_cs[i] = 1.0/sq;
117
    aa_ca[i] = Ci[i]/sq;
118
  }
119
 
120
  for(i=0;i<18;i++)
121
  {
122
    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 );
123
    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 );
124
  }
125
  for(i=0;i<6;i++)
126
  {
127
    win[1][i+18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
128
    win[3][i+12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 );
129
    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 );
130
    win[1][i+30] = win[3][i] = 0.0;
131
    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 );
132
  }
133
 
134
  for(i=0;i<9;i++)
135
    COS9[i] = cos( M_PI / 18.0 * (double) i);
136
 
137
  for(i=0;i<9;i++)
138
    tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 );
139
  for(i=0;i<3;i++)
140
    tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 );
141
 
142
  COS6_1 = cos( M_PI / 6.0 * (double) 1);
143
  COS6_2 = cos( M_PI / 6.0 * (double) 2);
144
 
145
#ifdef NEW_DCT9
146
  cos9[0] = cos(1.0*M_PI/9.0);
147
  cos9[1] = cos(5.0*M_PI/9.0);
148
  cos9[2] = cos(7.0*M_PI/9.0);
149
  cos18[0] = cos(1.0*M_PI/18.0);
150
  cos18[1] = cos(11.0*M_PI/18.0);
151
  cos18[2] = cos(13.0*M_PI/18.0);
152
#endif
153
 
154
  for(i=0;i<12;i++)
155
  {
156
    win[2][i]  = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 );
157
    for(j=0;j<6;j++)
158
      COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) );
159
  }
160
 
161
  for(j=0;j<4;j++) {
162
    static int len[4] = { 36,36,12,36 };
163
    for(i=0;i<len[j];i+=2)
164
      win1[j][i] = + win[j][i];
165
    for(i=1;i<len[j];i+=2)
166
      win1[j][i] = - win[j][i];
167
  }
168
 
169
  for(i=0;i<16;i++)
170
  {
171
    double t = tan( (double) i * M_PI / 12.0 );
172
    tan1_1[i] = t / (1.0+t);
173
    tan2_1[i] = 1.0 / (1.0 + t);
174
    tan1_2[i] = M_SQRT2 * t / (1.0+t);
175
    tan2_2[i] = M_SQRT2 / (1.0 + t);
176
 
177
    for(j=0;j<2;j++) {
178
      double base = pow(2.0,-0.25*(j+1.0));
179
      double p1=1.0,p2=1.0;
180
      if(i > 0) {
181
        if( i & 1 )
182
          p1 = pow(base,(i+1.0)*0.5);
183
        else
184
          p2 = pow(base,i*0.5);
185
      }
186
      pow1_1[j][i] = p1;
187
      pow2_1[j][i] = p2;
188
      pow1_2[j][i] = M_SQRT2 * p1;
189
      pow2_2[j][i] = M_SQRT2 * p2;
190
    }
191
  }
192
 
193
  for(j=0;j<7;j++)
194
  {
195
   struct bandInfoStruct *bi = &bandInfo[j];
196
   int *mp;
197
   int cb,lwin;
198
   int *bdf;
199
 
200
   mp = map[j][0] = mapbuf0[j];
201
   bdf = bi->longDiff;
202
   for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
203
     *mp++ = (*bdf) >> 1;
204
     *mp++ = i;
205
     *mp++ = 3;
206
     *mp++ = cb;
207
   }
208
   bdf = bi->shortDiff+3;
209
   for(cb=3;cb<13;cb++) {
210
     int l = (*bdf++) >> 1;
211
     for(lwin=0;lwin<3;lwin++) {
212
       *mp++ = l;
213
       *mp++ = i + lwin;
214
       *mp++ = lwin;
215
       *mp++ = cb;
216
     }
217
     i += 6*l;
218
   }
219
   mapend[j][0] = mp;
220
 
221
   mp = map[j][1] = mapbuf1[j];
222
   bdf = bi->shortDiff+0;
223
   for(i=0,cb=0;cb<13;cb++) {
224
     int l = (*bdf++) >> 1;
225
     for(lwin=0;lwin<3;lwin++) {
226
       *mp++ = l;
227
       *mp++ = i + lwin;
228
       *mp++ = lwin;
229
       *mp++ = cb;
230
     }
231
     i += 6*l;
232
   }
233
   mapend[j][1] = mp;
234
 
235
   mp = map[j][2] = mapbuf2[j];
236
   bdf = bi->longDiff;
237
   for(cb = 0; cb < 22 ; cb++) {
238
     *mp++ = (*bdf++) >> 1;
239
     *mp++ = cb;
240
   }
241
   mapend[j][2] = mp;
242
 
243
  }
244
 
245
  for(j=0;j<7;j++) {
246
    for(i=0;i<23;i++) {
247
      longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
248
      if(longLimit[j][i] > (SBLIMIT >> down_samp) )
249
        longLimit[j][i] = SBLIMIT >> down_samp;
250
    }
251
    for(i=0;i<14;i++) {
252
      shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
253
      if(shortLimit[j][i] > (SBLIMIT >> down_samp) )
254
        shortLimit[j][i] = SBLIMIT >> down_samp;
255
    }
256
  }
257
 
258
  for(i=0;i<5;i++) {
259
    for(j=0;j<6;j++) {
260
      for(k=0;k<6;k++) {
261
        int n = k + j * 6 + i * 36;
262
        i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
263
      }
264
    }
265
  }
266
  for(i=0;i<4;i++) {
267
    for(j=0;j<4;j++) {
268
      for(k=0;k<4;k++) {
269
        int n = k + j * 4 + i * 16;
270
        i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
271
      }
272
    }
273
  }
274
  for(i=0;i<4;i++) {
275
    for(j=0;j<3;j++) {
276
      int n = j + i * 3;
277
      i_slen2[n+244] = i|(j<<3) | (5<<12);
278
      n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
279
    }
280
  }
281
 
282
  for(i=0;i<5;i++) {
283
    for(j=0;j<5;j++) {
284
      for(k=0;k<4;k++) {
285
        for(l=0;l<4;l++) {
286
          int n = l + k * 4 + j * 16 + i * 80;
287
          n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
288
        }
289
      }
290
    }
291
  }
292
  for(i=0;i<5;i++) {
293
    for(j=0;j<5;j++) {
294
      for(k=0;k<4;k++) {
295
        int n = k + j * 4 + i * 20;
296
        n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
297
      }
298
    }
299
  }
300
}
301
 
302
/*
303
 * read additional side information
304
 */
305
static void III_get_side_info_1(struct III_sideinfo *si,int stereo,
306
 int ms_stereo,long sfreq,int single)
307
{
308
   int ch, gr;
309
   int powdiff = (single == 3) ? 4 : 0;
310
 
311
   si->main_data_begin = getbits(9);
312
   if (stereo == 1)
313
     si->private_bits = getbits_fast(5);
314
   else
315
     si->private_bits = getbits_fast(3);
316
 
317
   for (ch=0; ch<stereo; ch++) {
318
       si->ch[ch].gr[0].scfsi = -1;
319
       si->ch[ch].gr[1].scfsi = getbits_fast(4);
320
   }
321
 
322
   for (gr=0; gr<2; gr++)
323
   {
324
     for (ch=0; ch<stereo; ch++)
325
     {
326
       register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
327
 
328
       gr_info->part2_3_length = getbits(12);
329
       gr_info->big_values = getbits_fast(9);
330
       gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
331
       if(ms_stereo)
332
         gr_info->pow2gain += 2;
333
       gr_info->scalefac_compress = getbits_fast(4);
334
/* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
335
       if(get1bit())
336
       {
337
         int i;
338
         gr_info->block_type = getbits_fast(2);
339
         gr_info->mixed_block_flag = get1bit();
340
         gr_info->table_select[0] = getbits_fast(5);
341
         gr_info->table_select[1] = getbits_fast(5);
342
         /*
343
          * table_select[2] not needed, because there is no region2,
344
          * but to satisfy some verifications tools we set it either.
345
          */
346
         gr_info->table_select[2] = 0;
347
         for(i=0;i<3;i++)
348
           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
349
 
350
         if(gr_info->block_type == 0) {
351
           fprintf(stderr,"Blocktype == 0 and window-switching == 1 not allowed.\n");
352
           exit(1);
353
         }
354
         /* region_count/start parameters are implicit in this case. */      
355
         gr_info->region1start = 36>>1;
356
         gr_info->region2start = 576>>1;
357
       }
358
       else
359
       {
360
         int i,r0c,r1c;
361
         for (i=0; i<3; i++)
362
           gr_info->table_select[i] = getbits_fast(5);
363
         r0c = getbits_fast(4);
364
         r1c = getbits_fast(3);
365
         gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
366
         gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
367
         gr_info->block_type = 0;
368
         gr_info->mixed_block_flag = 0;
369
       }
370
       gr_info->preflag = get1bit();
371
       gr_info->scalefac_scale = get1bit();
372
       gr_info->count1table_select = get1bit();
373
     }
374
   }
375
}
376
 
377
/*
378
 * Side Info for MPEG 2.0 / LSF
379
 */
380
static void III_get_side_info_2(struct III_sideinfo *si,int stereo,
381
 int ms_stereo,long sfreq,int single)
382
{
383
   int ch;
384
   int powdiff = (single == 3) ? 4 : 0;
385
 
386
   si->main_data_begin = getbits(8);
387
   if (stereo == 1)
388
     si->private_bits = get1bit();
389
   else
390
     si->private_bits = getbits_fast(2);
391
 
392
   for (ch=0; ch<stereo; ch++)
393
   {
394
       register struct gr_info_s *gr_info = &(si->ch[ch].gr[0]);
395
 
396
       gr_info->part2_3_length = getbits(12);
397
       gr_info->big_values = getbits_fast(9);
398
       gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
399
       if(ms_stereo)
400
         gr_info->pow2gain += 2;
401
       gr_info->scalefac_compress = getbits(9);
402
/* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
403
       if(get1bit())
404
       {
405
         int i;
406
         gr_info->block_type = getbits_fast(2);
407
         gr_info->mixed_block_flag = get1bit();
408
         gr_info->table_select[0] = getbits_fast(5);
409
         gr_info->table_select[1] = getbits_fast(5);
410
         /*
411
          * table_select[2] not needed, because there is no region2,
412
          * but to satisfy some verifications tools we set it either.
413
          */
414
         gr_info->table_select[2] = 0;
415
         for(i=0;i<3;i++)
416
           gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
417
 
418
         if(gr_info->block_type == 0) {
419
           fprintf(stderr,"Blocktype == 0 and window-switching == 1 not allowed.\n");
420
           exit(1);
421
         }
422
         /* region_count/start parameters are implicit in this case. */      
423
/* check this again! */
424
         if(gr_info->block_type == 2)
425
           gr_info->region1start = 36>>1;
426
         else {
427
           gr_info->region1start = 54>>1;
428
         }
429
         gr_info->region2start = 576>>1;
430
       }
431
       else
432
       {
433
         int i,r0c,r1c;
434
         for (i=0; i<3; i++)
435
           gr_info->table_select[i] = getbits_fast(5);
436
         r0c = getbits_fast(4);
437
         r1c = getbits_fast(3);
438
         gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
439
         gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
440
         gr_info->block_type = 0;
441
         gr_info->mixed_block_flag = 0;
442
       }
443
       gr_info->scalefac_scale = get1bit();
444
       gr_info->count1table_select = get1bit();
445
   }
446
}
447
 
448
/*
449
 * read scalefactors
450
 */
451
static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
452
{
453
   static unsigned char slen[2][16] = {
454
     {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
455
     {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
456
   };
457
   int numbits;
458
   int num0 = slen[0][gr_info->scalefac_compress];
459
   int num1 = slen[1][gr_info->scalefac_compress];
460
 
461
    if (gr_info->block_type == 2)
462
    {
463
      int i=18;
464
      numbits = (num0 + num1) * 18;
465
 
466
      if (gr_info->mixed_block_flag) {
467
         for (i=8;i;i--)
468
           *scf++ = getbits_fast(num0);
469
         i = 9;
470
         numbits -= num0; /* num0 * 17 + num1 * 18 */
471
      }
472
 
473
      for (;i;i--)
474
        *scf++ = getbits_fast(num0);
475
      for (i = 18; i; i--)
476
        *scf++ = getbits_fast(num1);
477
      *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
478
    }
479
    else
480
    {
481
      int i;
482
      int scfsi = gr_info->scfsi;
483
 
484
      if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
485
         for(i=11;i;i--)
486
           *scf++ = getbits_fast(num0);
487
         for(i=10;i;i--)
488
           *scf++ = getbits_fast(num1);
489
         numbits = (num0 + num1) * 10 + num0;
490
      }
491
      else {
492
        numbits = 0;
493
        if(!(scfsi & 0x8)) {
494
          for (i=6;i;i--)
495
            *scf++ = getbits_fast(num0);
496
          numbits += num0 * 6;
497
        }
498
        else {
499
          *scf++ = 0; *scf++ = 0; *scf++ = 0;  /* set to ZERO necessary? */
500
          *scf++ = 0; *scf++ = 0; *scf++ = 0;
501
        }
502
 
503
        if(!(scfsi & 0x4)) {
504
          for (i=5;i;i--)
505
            *scf++ = getbits_fast(num0);
506
          numbits += num0 * 5;
507
        }
508
        else {
509
          *scf++ = 0; *scf++ = 0; *scf++ = 0;  /* set to ZERO necessary? */
510
          *scf++ = 0; *scf++ = 0;
511
        }
512
 
513
        if(!(scfsi & 0x2)) {
514
          for(i=5;i;i--)
515
            *scf++ = getbits_fast(num1);
516
          numbits += num1 * 5;
517
        }
518
        else {
519
          *scf++ = 0; *scf++ = 0; *scf++ = 0;  /* set to ZERO necessary? */
520
          *scf++ = 0; *scf++ = 0;
521
        }
522
 
523
        if(!(scfsi & 0x1)) {
524
          for (i=5;i;i--)
525
            *scf++ = getbits_fast(num1);
526
          numbits += num1 * 5;
527
        }
528
        else {
529
          *scf++ = 0; *scf++ = 0; *scf++ = 0;  /* set to ZERO necessary? */
530
          *scf++ = 0; *scf++ = 0;
531
        }
532
      }
533
 
534
      *scf++ = 0;  /* no l[21] in original sources */
535
    }
536
    return numbits;
537
}
538
 
539
static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
540
{
541
  unsigned char *pnt;
542
  int i,j;
543
  unsigned int slen;
544
  int n = 0;
545
  int numbits = 0;
546
 
547
  static unsigned char stab[3][6][4] = {
548
   { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
549
     { 7, 7, 7,0 } , { 6, 6, 6,3 } , {  8, 8,5,0} } ,
550
   { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
551
     {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
552
   { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
553
     { 6,15,12,0 } , { 6,12, 9,6 } , {  6,18,9,0} } };
554
 
555
  if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
556
    slen = i_slen2[gr_info->scalefac_compress>>1];
557
  else
558
    slen = n_slen2[gr_info->scalefac_compress];
559
 
560
  gr_info->preflag = (slen>>15) & 0x1;
561
 
562
  n = 0;  
563
  if( gr_info->block_type == 2 ) {
564
    n++;
565
    if(gr_info->mixed_block_flag)
566
      n++;
567
  }
568
 
569
  pnt = stab[n][(slen>>12)&0x7];
570
 
571
  for(i=0;i<4;i++) {
572
    int num = slen & 0x7;
573
    slen >>= 3;
574
    if(num) {
575
      for(j=0;j<pnt[i];j++)
576
        *scf++ = getbits(num);
577
      numbits += pnt[i] * num;
578
    }
579
    else {
580
      for(j=0;j<pnt[i];j++)
581
        *scf++ = 0;
582
    }
583
  }
584
 
585
  n = (n << 1) + 1;
586
  for(i=0;i<n;i++)
587
    *scf++ = 0;
588
 
589
  return numbits;
590
}
591
 
592
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};
593
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};
594
 
595
/*
596
 * don't forget to apply the same changes to III_dequantize_sample_ms() !!!
597
 */
598
static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
599
   struct gr_info_s *gr_info,int sfreq,int part2bits)
600
{
601
  int shift = 1 + gr_info->scalefac_scale;
602
  real *xrpnt = (real *) xr;
603
  int l[3],l3;
604
  int part2remain = gr_info->part2_3_length - part2bits;
605
  int *me;
606
 
607
  {
608
    int bv       = gr_info->big_values;
609
    int region1  = gr_info->region1start;
610
    int region2  = gr_info->region2start;
611
 
612
    l3 = ((576>>1)-bv)>>1;  
613
/*
614
 * we may lose the 'odd' bit here !!
615
 * check this later again
616
 */
617
    if(bv <= region1) {
618
      l[0] = bv; l[1] = 0; l[2] = 0;
619
    }
620
    else {
621
      l[0] = region1;
622
      if(bv <= region2) {
623
        l[1] = bv - l[0];  l[2] = 0;
624
      }
625
      else {
626
        l[1] = region2 - l[0]; l[2] = bv - region2;
627
      }
628
    }
629
  }
630
 
631
  if(gr_info->block_type == 2) {
632
    int i,max[4];
633
    int step=0,lwin=0,cb=0;
634
    register real v = 0.0;
635
    register int *m,mc;
636
 
637
    if(gr_info->mixed_block_flag) {
638
      max[3] = -1;
639
      max[0] = max[1] = max[2] = 2;
640
      m = map[sfreq][0];
641
      me = mapend[sfreq][0];
642
    }
643
    else {
644
      max[0] = max[1] = max[2] = max[3] = -1;
645
      /* max[3] not really needed in this case */
646
      m = map[sfreq][1];
647
      me = mapend[sfreq][1];
648
    }
649
 
650
    mc = 0;
651
    for(i=0;i<2;i++) {
652
      int lp = l[i];
653
      struct newhuff *h = ht+gr_info->table_select[i];
654
      for(;lp;lp--,mc--) {
655
        register int x,y;
656
        if( (!mc) ) {
657
          mc = *m++;
658
          xrpnt = ((real *) xr) + (*m++);
659
          lwin = *m++;
660
          cb = *m++;
661
          if(lwin == 3) {
662
            v = gr_info->pow2gain[(*scf++) << shift];
663
            step = 1;
664
          }
665
          else {
666
            v = gr_info->full_gain[lwin][(*scf++) << shift];
667
            step = 3;
668
          }
669
        }
670
        {
671
          register short *val = h->table;
672
          while((y=*val++)<0) {
673
            if (get1bit())
674
              val -= y;
675
            part2remain--;
676
          }
677
          x = y >> 4;
678
          y &= 0xf;
679
        }
680
        if(x == 15) {
681
          max[lwin] = cb;
682
          part2remain -= h->linbits+1;
683
          x += getbits(h->linbits);
684
          if(get1bit())
685
            *xrpnt = -ispow[x] * v;
686
          else
687
            *xrpnt =  ispow[x] * v;
688
        }
689
        else if(x) {
690
          max[lwin] = cb;
691
          if(get1bit())
692
            *xrpnt = -ispow[x] * v;
693
          else
694
            *xrpnt =  ispow[x] * v;
695
          part2remain--;
696
        }
697
        else
698
          *xrpnt = 0.0;
699
        xrpnt += step;
700
        if(y == 15) {
701
          max[lwin] = cb;
702
          part2remain -= h->linbits+1;
703
          y += getbits(h->linbits);
704
          if(get1bit())
705
            *xrpnt = -ispow[y] * v;
706
          else
707
            *xrpnt =  ispow[y] * v;
708
        }
709
        else if(y) {
710
          max[lwin] = cb;
711
          if(get1bit())
712
            *xrpnt = -ispow[y] * v;
713
          else
714
            *xrpnt =  ispow[y] * v;
715
          part2remain--;
716
        }
717
        else
718
          *xrpnt = 0.0;
719
        xrpnt += step;
720
      }
721
    }
722
    for(;l3 && (part2remain > 0);l3--) {
723
      struct newhuff *h = htc+gr_info->count1table_select;
724
      register short *val = h->table,a;
725
 
726
      while((a=*val++)<0) {
727
        part2remain--;
728
        if(part2remain < 0) {
729
          part2remain++;
730
          a = 0;
731
          break;
732
        }
733
        if (get1bit())
734
          val -= a;
735
      }
736
 
737
      for(i=0;i<4;i++) {
738
        if(!(i & 1)) {
739
          if(!mc) {
740
            mc = *m++;
741
            xrpnt = ((real *) xr) + (*m++);
742
            lwin = *m++;
743
            cb = *m++;
744
            if(lwin == 3) {
745
              v = gr_info->pow2gain[(*scf++) << shift];
746
              step = 1;
747
            }
748
            else {
749
              v = gr_info->full_gain[lwin][(*scf++) << shift];
750
              step = 3;
751
            }
752
          }
753
          mc--;
754
        }
755
        if( (a & (0x8>>i)) ) {
756
          max[lwin] = cb;
757
          part2remain--;
758
          if(part2remain < 0) {
759
            part2remain++;
760
            break;
761
          }
762
          if(get1bit())
763
            *xrpnt = -v;
764
          else
765
            *xrpnt = v;
766
        }
767
        else
768
          *xrpnt = 0.0;
769
        xrpnt += step;
770
      }
771
    }
772
 
773
    while( m < me ) {
774
      if(!mc) {
775
        mc = *m++;
776
        xrpnt = ((real *) xr) + *m++;
777
        if( (*m++) == 3)
778
          step = 1;
779
        else
780
          step = 3;
781
        m++; /* cb */
782
      }
783
      mc--;
784
      *xrpnt = 0.0;
785
      xrpnt += step;
786
      *xrpnt = 0.0;
787
      xrpnt += step;
788
    }
789
 
790
    gr_info->maxband[0] = max[0]+1;
791
    gr_info->maxband[1] = max[1]+1;
792
    gr_info->maxband[2] = max[2]+1;
793
    gr_info->maxbandl = max[3]+1;
794
 
795
    {
796
      int rmax = max[0] > max[1] ? max[0] : max[1];
797
      rmax = (rmax > max[2] ? rmax : max[2]) + 1;
798
      gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
799
    }
800
 
801
  }
802
  else {
803
    int *pretab = gr_info->preflag ? pretab1 : pretab2;
804
    int i,max = -1;
805
    int cb = 0;
806
    register int *m = map[sfreq][2];
807
    register real v = 0.0;
808
    register int mc = 0;
809
    me = mapend[sfreq][2];
810
 
811
    for(i=0;i<3;i++) {
812
      int lp = l[i];
813
      struct newhuff *h = ht+gr_info->table_select[i];
814
 
815
      for(;lp;lp--,mc--) {
816
        int x,y;
817
 
818
        if(!mc) {
819
          mc = *m++;
820
          v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
821
          cb = *m++;
822
        }
823
        {
824
          register short *val = h->table;
825
          while((y=*val++)<0) {
826
            if (get1bit())
827
              val -= y;
828
            part2remain--;
829
          }
830
          x = y >> 4;
831
          y &= 0xf;
832
        }
833
        if (x == 15) {
834
          max = cb;
835
          part2remain -= h->linbits+1;
836
          x += getbits(h->linbits);
837
          if(get1bit())
838
            *xrpnt++ = -ispow[x] * v;
839
          else
840
            *xrpnt++ =  ispow[x] * v;
841
        }
842
        else if(x) {
843
          max = cb;
844
          if(get1bit())
845
            *xrpnt++ = -ispow[x] * v;
846
          else
847
            *xrpnt++ =  ispow[x] * v;
848
          part2remain--;
849
        }
850
        else
851
          *xrpnt++ = 0.0;
852
 
853
        if (y == 15) {
854
          max = cb;
855
          part2remain -= h->linbits+1;
856
          y += getbits(h->linbits);
857
          if(get1bit())
858
            *xrpnt++ = -ispow[y] * v;
859
          else
860
            *xrpnt++ =  ispow[y] * v;
861
        }
862
        else if(y) {
863
          max = cb;
864
          if(get1bit())
865
            *xrpnt++ = -ispow[y] * v;
866
          else
867
            *xrpnt++ =  ispow[y] * v;
868
          part2remain--;
869
        }
870
        else
871
          *xrpnt++ = 0.0;
872
      }
873
    }
874
 
875
    for(;l3 && (part2remain > 0);l3--) {
876
      struct newhuff *h = htc+gr_info->count1table_select;
877
      register short *val = h->table,a;
878
 
879
      while((a=*val++)<0) {
880
        part2remain--;
881
        if(part2remain < 0) {
882
          part2remain++;
883
          a = 0;
884
          break;
885
        }
886
        if (get1bit())
887
          val -= a;
888
      }
889
 
890
      for(i=0;i<4;i++) {
891
        if(!(i & 1)) {
892
          if(!mc) {
893
            mc = *m++;
894
            cb = *m++;
895
            v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
896
          }
897
          mc--;
898
        }
899
        if ( (a & (0x8>>i)) ) {
900
          max = cb;
901
          part2remain--;
902
          if(part2remain < 0) {
903
            part2remain++;
904
            break;
905
          }
906
          if(get1bit())
907
            *xrpnt++ = -v;
908
          else
909
            *xrpnt++ = v;
910
        }
911
        else
912
          *xrpnt++ = 0.0;
913
      }
914
    }
915
    for(i=(&xr[SBLIMIT][SSLIMIT]-xrpnt)>>1;i;i--) {
916
      *xrpnt++ = 0.0;
917
      *xrpnt++ = 0.0;
918
    }
919
 
920
    gr_info->maxbandl = max+1;
921
    gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
922
  }
923
 
924
  while( part2remain > 16 ) {
925
    getbits(16); /* Dismiss stuffing Bits */
926
    part2remain -= 16;
927
  }
928
  if(part2remain > 0)
929
    getbits(part2remain);
930
  else if(part2remain < 0) {
931
    fprintf(stderr,"mpg123: Can't rewind stream by %d bits!\n",-part2remain);
932
    return 1; /* -> error */
933
  }
934
  return 0;
935
}
936
 
937
static int III_dequantize_sample_ms(real xr[2][SBLIMIT][SSLIMIT],int *scf,
938
   struct gr_info_s *gr_info,int sfreq,int part2bits)
939
{
940
  int shift = 1 + gr_info->scalefac_scale;
941
  real *xrpnt = (real *) xr[1];
942
  real *xr0pnt = (real *) xr[0];
943
  int l[3],l3;
944
  int part2remain = gr_info->part2_3_length - part2bits;
945
  int *me;
946
 
947
  {
948
    int bv       = gr_info->big_values;
949
    int region1  = gr_info->region1start;
950
    int region2  = gr_info->region2start;
951
 
952
    l3 = ((576>>1)-bv)>>1;  
953
/*
954
 * we may lose the 'odd' bit here !!
955
 * check this later gain
956
 */
957
    if(bv <= region1) {
958
      l[0] = bv; l[1] = 0; l[2] = 0;
959
    }
960
    else {
961
      l[0] = region1;
962
      if(bv <= region2) {
963
        l[1] = bv - l[0];  l[2] = 0;
964
      }
965
      else {
966
        l[1] = region2 - l[0]; l[2] = bv - region2;
967
      }
968
    }
969
  }
970
 
971
  if(gr_info->block_type == 2) {
972
    int i,max[4];
973
    int step=0,lwin=0,cb=0;
974
    register real v = 0.0;
975
    register int *m,mc = 0;
976
 
977
    if(gr_info->mixed_block_flag) {
978
      max[3] = -1;
979
      max[0] = max[1] = max[2] = 2;
980
      m = map[sfreq][0];
981
      me = mapend[sfreq][0];
982
    }
983
    else {
984
      max[0] = max[1] = max[2] = max[3] = -1;
985
      /* max[3] not really needed in this case */
986
      m = map[sfreq][1];
987
      me = mapend[sfreq][1];
988
    }
989
 
990
    for(i=0;i<2;i++) {
991
      int lp = l[i];
992
      struct newhuff *h = ht+gr_info->table_select[i];
993
      for(;lp;lp--,mc--) {
994
        int x,y;
995
 
996
        if(!mc) {
997
          mc = *m++;
998
          xrpnt = ((real *) xr[1]) + *m;
999
          xr0pnt = ((real *) xr[0]) + *m++;
1000
          lwin = *m++;
1001
          cb = *m++;
1002
          if(lwin == 3) {
1003
            v = gr_info->pow2gain[(*scf++) << shift];
1004
            step = 1;
1005
          }
1006
          else {
1007
            v = gr_info->full_gain[lwin][(*scf++) << shift];
1008
            step = 3;
1009
          }
1010
        }
1011
        {
1012
          register short *val = h->table;
1013
          while((y=*val++)<0) {
1014
            if (get1bit())
1015
              val -= y;
1016
            part2remain--;
1017
          }
1018
          x = y >> 4;
1019
          y &= 0xf;
1020
        }
1021
        if(x == 15) {
1022
          max[lwin] = cb;
1023
          part2remain -= h->linbits+1;
1024
          x += getbits(h->linbits);
1025
          if(get1bit()) {
1026
            real a = ispow[x] * v;
1027
            *xrpnt = *xr0pnt + a;
1028
            *xr0pnt -= a;
1029
          }
1030
          else {
1031
            real a = ispow[x] * v;
1032
            *xrpnt = *xr0pnt - a;
1033
            *xr0pnt += a;
1034
          }
1035
        }
1036
        else if(x) {
1037
          max[lwin] = cb;
1038
          if(get1bit()) {
1039
            real a = ispow[x] * v;
1040
            *xrpnt = *xr0pnt + a;
1041
            *xr0pnt -= a;
1042
          }
1043
          else {
1044
            real a = ispow[x] * v;
1045
            *xrpnt = *xr0pnt - a;
1046
            *xr0pnt += a;
1047
          }
1048
          part2remain--;
1049
        }
1050
        else
1051
          *xrpnt = *xr0pnt;
1052
        xrpnt += step;
1053
        xr0pnt += step;
1054
 
1055
        if(y == 15) {
1056
          max[lwin] = cb;
1057
          part2remain -= h->linbits+1;
1058
          y += getbits(h->linbits);
1059
          if(get1bit()) {
1060
            real a = ispow[y] * v;
1061
            *xrpnt = *xr0pnt + a;
1062
            *xr0pnt -= a;
1063
          }
1064
          else {
1065
            real a = ispow[y] * v;
1066
            *xrpnt = *xr0pnt - a;
1067
            *xr0pnt += a;
1068
          }
1069
        }
1070
        else if(y) {
1071
          max[lwin] = cb;
1072
          if(get1bit()) {
1073
            real a = ispow[y] * v;
1074
            *xrpnt = *xr0pnt + a;
1075
            *xr0pnt -= a;
1076
          }
1077
          else {
1078
            real a = ispow[y] * v;
1079
            *xrpnt = *xr0pnt - a;
1080
            *xr0pnt += a;
1081
          }
1082
          part2remain--;
1083
        }
1084
        else
1085
          *xrpnt = *xr0pnt;
1086
        xrpnt += step;
1087
        xr0pnt += step;
1088
      }
1089
    }
1090
 
1091
    for(;l3 && (part2remain > 0);l3--) {
1092
      struct newhuff *h = htc+gr_info->count1table_select;
1093
      register short *val = h->table,a;
1094
 
1095
      while((a=*val++)<0) {
1096
        part2remain--;
1097
        if(part2remain < 0) {
1098
          part2remain++;
1099
          a = 0;
1100
          break;
1101
        }
1102
        if (get1bit())
1103
          val -= a;
1104
      }
1105
 
1106
      for(i=0;i<4;i++) {
1107
        if(!(i & 1)) {
1108
          if(!mc) {
1109
            mc = *m++;
1110
            xrpnt = ((real *) xr[1]) + *m;
1111
            xr0pnt = ((real *) xr[0]) + *m++;
1112
            lwin = *m++;
1113
            cb = *m++;
1114
            if(lwin == 3) {
1115
              v = gr_info->pow2gain[(*scf++) << shift];
1116
              step = 1;
1117
            }
1118
            else {
1119
              v = gr_info->full_gain[lwin][(*scf++) << shift];
1120
              step = 3;
1121
            }
1122
          }
1123
          mc--;
1124
        }
1125
        if( (a & (0x8>>i)) ) {
1126
          max[lwin] = cb;
1127
          part2remain--;
1128
          if(part2remain < 0) {
1129
            part2remain++;
1130
            break;
1131
          }
1132
          if(get1bit()) {
1133
            *xrpnt = *xr0pnt + v;
1134
            *xr0pnt -= v;
1135
          }
1136
          else {
1137
            *xrpnt = *xr0pnt - v;
1138
            *xr0pnt += v;
1139
          }
1140
        }
1141
        else
1142
          *xrpnt = *xr0pnt;
1143
        xrpnt += step;
1144
        xr0pnt += step;
1145
      }
1146
    }
1147
 
1148
    while( m < me ) {
1149
      if(!mc) {
1150
        mc = *m++;
1151
        xrpnt = ((real *) xr) + *m;
1152
        xr0pnt = ((real *) xr) + *m++;
1153
        if(*m++ == 3)
1154
          step = 1;
1155
        else
1156
          step = 3;
1157
        m++; /* cb */
1158
      }
1159
      mc--;
1160
      *xrpnt = *xr0pnt;
1161
      xrpnt += step;
1162
      xr0pnt += step;
1163
      *xrpnt = *xr0pnt;
1164
      xrpnt += step;
1165
      xr0pnt += step;
1166
    }
1167
 
1168
    gr_info->maxband[0] = max[0]+1;
1169
    gr_info->maxband[1] = max[1]+1;
1170
    gr_info->maxband[2] = max[2]+1;
1171
    gr_info->maxbandl = max[3]+1;
1172
 
1173
    {
1174
      int rmax = max[0] > max[1] ? max[0] : max[1];
1175
      rmax = (rmax > max[2] ? rmax : max[2]) + 1;
1176
      gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
1177
    }
1178
  }
1179
  else {
1180
    int *pretab = gr_info->preflag ? pretab1 : pretab2;
1181
    int i,max = -1;
1182
    int cb = 0;
1183
    register int mc=0,*m = map[sfreq][2];
1184
    register real v = 0.0;
1185
    me = mapend[sfreq][2];
1186
 
1187
    for(i=0;i<3;i++) {
1188
      int lp = l[i];
1189
      struct newhuff *h = ht+gr_info->table_select[i];
1190
 
1191
      for(;lp;lp--,mc--) {
1192
        int x,y;
1193
        if(!mc) {
1194
          mc = *m++;
1195
          cb = *m++;
1196
          v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1197
        }
1198
        {
1199
          register short *val = h->table;
1200
          while((y=*val++)<0) {
1201
            if (get1bit())
1202
              val -= y;
1203
            part2remain--;
1204
          }
1205
          x = y >> 4;
1206
          y &= 0xf;
1207
        }
1208
        if (x == 15) {
1209
          max = cb;
1210
          part2remain -= h->linbits+1;
1211
          x += getbits(h->linbits);
1212
          if(get1bit()) {
1213
            real a = ispow[x] * v;
1214
            *xrpnt++ = *xr0pnt + a;
1215
            *xr0pnt++ -= a;
1216
          }
1217
          else {
1218
            real a = ispow[x] * v;
1219
            *xrpnt++ = *xr0pnt - a;
1220
            *xr0pnt++ += a;
1221
          }
1222
        }
1223
        else if(x) {
1224
          max = cb;
1225
          if(get1bit()) {
1226
            real a = ispow[x] * v;
1227
            *xrpnt++ = *xr0pnt + a;
1228
            *xr0pnt++ -= a;
1229
          }
1230
          else {
1231
            real a = ispow[x] * v;
1232
            *xrpnt++ = *xr0pnt - a;
1233
            *xr0pnt++ += a;
1234
          }
1235
          part2remain--;
1236
        }
1237
        else
1238
          *xrpnt++ = *xr0pnt++;
1239
 
1240
        if (y == 15) {
1241
          max = cb;
1242
          part2remain -= h->linbits+1;
1243
          y += getbits(h->linbits);
1244
          if(get1bit()) {
1245
            real a = ispow[y] * v;
1246
            *xrpnt++ = *xr0pnt + a;
1247
            *xr0pnt++ -= a;
1248
          }
1249
          else {
1250
            real a = ispow[y] * v;
1251
            *xrpnt++ = *xr0pnt - a;
1252
            *xr0pnt++ += a;
1253
          }
1254
        }
1255
        else if(y) {
1256
          max = cb;
1257
          if(get1bit()) {
1258
            real a = ispow[y] * v;
1259
            *xrpnt++ = *xr0pnt + a;
1260
            *xr0pnt++ -= a;
1261
          }
1262
          else {
1263
            real a = ispow[y] * v;
1264
            *xrpnt++ = *xr0pnt - a;
1265
            *xr0pnt++ += a;
1266
          }
1267
          part2remain--;
1268
        }
1269
        else
1270
          *xrpnt++ = *xr0pnt++;
1271
      }
1272
    }
1273
 
1274
    for(;l3 && (part2remain > 0);l3--) {
1275
      struct newhuff *h = htc+gr_info->count1table_select;
1276
      register short *val = h->table,a;
1277
 
1278
      while((a=*val++)<0) {
1279
        part2remain--;
1280
        if(part2remain < 0) {
1281
          part2remain++;
1282
          a = 0;
1283
          break;
1284
        }
1285
        if (get1bit())
1286
          val -= a;
1287
      }
1288
 
1289
      for(i=0;i<4;i++) {
1290
        if(!(i & 1)) {
1291
          if(!mc) {
1292
            mc = *m++;
1293
            cb = *m++;
1294
            v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1295
          }
1296
          mc--;
1297
        }
1298
        if ( (a & (0x8>>i)) ) {
1299
          max = cb;
1300
          part2remain--;
1301
          if(part2remain <= 0) {
1302
            part2remain++;
1303
            break;
1304
          }
1305
          if(get1bit()) {
1306
            *xrpnt++ = *xr0pnt + v;
1307
            *xr0pnt++ -= v;
1308
          }
1309
          else {
1310
            *xrpnt++ = *xr0pnt - v;
1311
            *xr0pnt++ += v;
1312
          }
1313
        }
1314
        else
1315
          *xrpnt++ = *xr0pnt++;
1316
      }
1317
    }
1318
    for(i=(&xr[1][SBLIMIT][SSLIMIT]-xrpnt)>>1;i;i--) {
1319
      *xrpnt++ = *xr0pnt++;
1320
      *xrpnt++ = *xr0pnt++;
1321
    }
1322
 
1323
    gr_info->maxbandl = max+1;
1324
    gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
1325
  }
1326
 
1327
  while ( part2remain > 16 ) {
1328
    getbits(16); /* Dismiss stuffing Bits */
1329
    part2remain -= 16;
1330
  }
1331
  if(part2remain > 0 )
1332
    getbits(part2remain);
1333
  else if(part2remain < 0) {
1334
    fprintf(stderr,"mpg123_ms: Can't rewind stream by %d bits!\n",-part2remain);
1335
    return 1; /* -> error */
1336
  }
1337
  return 0;
1338
}
1339
 
1340
/*
1341
 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
1342
 */
1343
static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
1344
   struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
1345
{
1346
      real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
1347
      struct bandInfoStruct *bi = &bandInfo[sfreq];
1348
      real *tab1,*tab2;
1349
 
1350
      if(lsf) {
1351
        int p = gr_info->scalefac_compress & 0x1;
1352
            if(ms_stereo) {
1353
          tab1 = pow1_2[p]; tab2 = pow2_2[p];
1354
        }
1355
        else {
1356
          tab1 = pow1_1[p]; tab2 = pow2_1[p];
1357
        }
1358
      }
1359
      else {
1360
        if(ms_stereo) {
1361
          tab1 = tan1_2; tab2 = tan2_2;
1362
        }
1363
        else {
1364
          tab1 = tan1_1; tab2 = tan2_1;
1365
        }
1366
      }
1367
 
1368
      if (gr_info->block_type == 2)
1369
      {
1370
         int lwin,do_l = 0;
1371
         if( gr_info->mixed_block_flag )
1372
           do_l = 1;
1373
 
1374
         for (lwin=0;lwin<3;lwin++) /* process each window */
1375
         {
1376
             /* get first band with zero values */
1377
           int is_p,sb,idx,sfb = gr_info->maxband[lwin];  /* sfb is minimal 3 for mixed mode */
1378
           if(sfb > 3)
1379
             do_l = 0;
1380
 
1381
           for(;sfb<12;sfb++)
1382
           {
1383
             is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1384
             if(is_p != 7) {
1385
               real t1,t2;
1386
               sb = bi->shortDiff[sfb];
1387
               idx = bi->shortIdx[sfb] + lwin;
1388
               t1 = tab1[is_p]; t2 = tab2[is_p];
1389
               for (; sb > 0; sb--,idx+=3)
1390
               {
1391
                 real v = xr[0][idx];
1392
                 xr[0][idx] = v * t1;
1393
                 xr[1][idx] = v * t2;
1394
               }
1395
             }
1396
           }
1397
 
1398
#if 1
1399
/* in the original: copy 10 to 11 , here: copy 11 to 12
1400
maybe still wrong??? (copy 12 to 13?) */
1401
           is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1402
           sb = bi->shortDiff[12];
1403
           idx = bi->shortIdx[12] + lwin;
1404
#else
1405
           is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1406
           sb = bi->shortDiff[11];
1407
           idx = bi->shortIdx[11] + lwin;
1408
#endif
1409
           if(is_p != 7)
1410
           {
1411
             real t1,t2;
1412
             t1 = tab1[is_p]; t2 = tab2[is_p];
1413
             for ( ; sb > 0; sb--,idx+=3 )
1414
             {  
1415
               real v = xr[0][idx];
1416
               xr[0][idx] = v * t1;
1417
               xr[1][idx] = v * t2;
1418
             }
1419
           }
1420
         } /* end for(lwin; .. ; . ) */
1421
 
1422
         if (do_l)
1423
         {
1424
/* also check l-part, if ALL bands in the three windows are 'empty'
1425
 * and mode = mixed_mode
1426
 */
1427
           int sfb = gr_info->maxbandl;
1428
           int idx = bi->longIdx[sfb];
1429
 
1430
           for ( ; sfb<8; sfb++ )
1431
           {
1432
             int sb = bi->longDiff[sfb];
1433
             int is_p = scalefac[sfb]; /* scale: 0-15 */
1434
             if(is_p != 7) {
1435
               real t1,t2;
1436
               t1 = tab1[is_p]; t2 = tab2[is_p];
1437
               for ( ; sb > 0; sb--,idx++)
1438
               {
1439
                 real v = xr[0][idx];
1440
                 xr[0][idx] = v * t1;
1441
                 xr[1][idx] = v * t2;
1442
               }
1443
             }
1444
             else
1445
               idx += sb;
1446
           }
1447
         }    
1448
      }
1449
      else /* ((gr_info->block_type != 2)) */
1450
      {
1451
        int sfb = gr_info->maxbandl;
1452
        int is_p,idx = bi->longIdx[sfb];
1453
        for ( ; sfb<21; sfb++)
1454
        {
1455
          int sb = bi->longDiff[sfb];
1456
          is_p = scalefac[sfb]; /* scale: 0-15 */
1457
          if(is_p != 7) {
1458
            real t1,t2;
1459
            t1 = tab1[is_p]; t2 = tab2[is_p];
1460
            for ( ; sb > 0; sb--,idx++)
1461
            {
1462
               real v = xr[0][idx];
1463
               xr[0][idx] = v * t1;
1464
               xr[1][idx] = v * t2;
1465
            }
1466
          }
1467
          else
1468
            idx += sb;
1469
        }
1470
 
1471
        is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1472
        if(is_p != 7)
1473
        {
1474
          int sb;
1475
          real t1 = tab1[is_p],t2 = tab2[is_p];
1476
 
1477
          for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1478
          {
1479
            real v = xr[0][idx];
1480
            xr[0][idx] = v * t1;
1481
            xr[1][idx] = v * t2;
1482
          }
1483
        }
1484
      } /* ... */
1485
}
1486
 
1487
static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1488
{
1489
   int sblim;
1490
 
1491
   if(gr_info->block_type == 2)
1492
   {
1493
      if(!gr_info->mixed_block_flag)
1494
        return;
1495
      sblim = 1;
1496
   }
1497
   else {
1498
     sblim = gr_info->maxb-1;
1499
   }
1500
 
1501
   /* 31 alias-reduction operations between each pair of sub-bands */
1502
   /* with 8 butterflies between each pair                         */
1503
 
1504
   {
1505
     int sb;
1506
     real *xr1=(real *) xr[1];
1507
 
1508
     for(sb=sblim;sb;sb--,xr1+=10)
1509
     {
1510
       int ss;
1511
       real *cs=aa_cs,*ca=aa_ca;
1512
       real *xr2 = xr1;
1513
 
1514
       for(ss=7;ss>=0;ss--)
1515
       {       /* upper and lower butterfly inputs */
1516
         register real bu = *--xr2,bd = *xr1;
1517
         *xr2   = (bu * (*cs)   ) - (bd * (*ca)   );
1518
         *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
1519
       }
1520
     }
1521
  }
1522
}
1523
 
1524
/*
1525
// This is an optimized DCT from Jeff Tsay's maplay 1.2+ package.
1526
// Saved one multiplication by doing the 'twiddle factor' stuff
1527
// together with the window mul. (MH)
1528
//
1529
// This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the
1530
// 9 point IDCT needs to be reduced further. Unfortunately, I don't
1531
// know how to do that, because 9 is not an even number. - Jeff.
1532
//
1533
//////////////////////////////////////////////////////////////////
1534
//
1535
// 9 Point Inverse Discrete Cosine Transform
1536
//
1537
// This piece of code is Copyright 1997 Mikko Tommila and is freely usable
1538
// by anybody. The algorithm itself is of course in the public domain.
1539
//
1540
// Again derived heuristically from the 9-point WFTA.
1541
//
1542
// The algorithm is optimized (?) for speed, not for small rounding errors or
1543
// good readability.
1544
//
1545
// 36 additions, 11 multiplications
1546
//
1547
// Again this is very likely sub-optimal.
1548
//
1549
// The code is optimized to use a minimum number of temporary variables,
1550
// so it should compile quite well even on 8-register Intel x86 processors.
1551
// This makes the code quite obfuscated and very difficult to understand.
1552
//
1553
// References:
1554
// [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1555
//     Mathematics of Computation, Volume 32, Number 141, January 1978,
1556
//     Pages 175-199
1557
*/
1558
 
1559
/*------------------------------------------------------------------*/
1560
/*                                                                  */
1561
/*    Function: Calculation of the inverse MDCT                     */
1562
/*                                                                  */
1563
/*------------------------------------------------------------------*/
1564
 
1565
static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1566
{
1567
#ifdef NEW_DCT9
1568
  real tmp[18];
1569
#endif
1570
 
1571
  {
1572
    register real *in = inbuf;
1573
 
1574
    in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1575
    in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1576
    in[11]+=in[10]; in[10]+=in[9];  in[9] +=in[8];
1577
    in[8] +=in[7];  in[7] +=in[6];  in[6] +=in[5];
1578
    in[5] +=in[4];  in[4] +=in[3];  in[3] +=in[2];
1579
    in[2] +=in[1];  in[1] +=in[0];
1580
 
1581
    in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1582
    in[9] +=in[7];  in[7] +=in[5];  in[5] +=in[3];  in[3] +=in[1];
1583
 
1584
 
1585
#ifdef NEW_DCT9
1586
    {
1587
      real t0, t1, t2, t3, t4, t5, t6, t7;
1588
 
1589
      t1 = COS6_2 * in[12];
1590
      t2 = COS6_2 * (in[8] + in[16] - in[4]);
1591
 
1592
      t3 = in[0] + t1;
1593
      t4 = in[0] - t1 - t1;
1594
      t5 = t4 - t2;
1595
 
1596
      t0 = cos9[0] * (in[4] + in[8]);
1597
      t1 = cos9[1] * (in[8] - in[16]);
1598
 
1599
      tmp[4] = t4 + t2 + t2;
1600
      t2 = cos9[2] * (in[4] + in[16]);
1601
 
1602
      t6 = t3 - t0 - t2;
1603
      t0 += t3 + t1;
1604
      t3 += t2 - t1;
1605
 
1606
      t2 = cos18[0] * (in[2]  + in[10]);
1607
      t4 = cos18[1] * (in[10] - in[14]);
1608
      t7 = COS6_1 * in[6];
1609
 
1610
      t1 = t2 + t4 + t7;
1611
      tmp[0] = t0 + t1;
1612
      tmp[8] = t0 - t1;
1613
      t1 = cos18[2] * (in[2] + in[14]);
1614
      t2 += t1 - t7;
1615
 
1616
      tmp[3] = t3 + t2;
1617
      t0 = COS6_1 * (in[10] + in[14] - in[2]);
1618
      tmp[5] = t3 - t2;
1619
 
1620
      t4 -= t1 + t7;
1621
 
1622
      tmp[1] = t5 - t0;
1623
      tmp[7] = t5 + t0;
1624
      tmp[2] = t6 + t4;
1625
      tmp[6] = t6 - t4;
1626
    }
1627
 
1628
    {
1629
      real t0, t1, t2, t3, t4, t5, t6, t7;
1630
 
1631
      t1 = COS6_2 * in[13];
1632
      t2 = COS6_2 * (in[9] + in[17] - in[5]);
1633
 
1634
      t3 = in[1] + t1;
1635
      t4 = in[1] - t1 - t1;
1636
      t5 = t4 - t2;
1637
 
1638
      t0 = cos9[0] * (in[5] + in[9]);
1639
      t1 = cos9[1] * (in[9] - in[17]);
1640
 
1641
      tmp[13] = (t4 + t2 + t2) * tfcos36[17-13];
1642
      t2 = cos9[2] * (in[5] + in[17]);
1643
 
1644
      t6 = t3 - t0 - t2;
1645
      t0 += t3 + t1;
1646
      t3 += t2 - t1;
1647
 
1648
      t2 = cos18[0] * (in[3]  + in[11]);
1649
      t4 = cos18[1] * (in[11] - in[15]);
1650
      t7 = COS6_1 * in[7];
1651
 
1652
      t1 = t2 + t4 + t7;
1653
      tmp[17] = (t0 + t1) * tfcos36[17-17];
1654
      tmp[9]  = (t0 - t1) * tfcos36[17-9];
1655
      t1 = cos18[2] * (in[3] + in[15]);
1656
      t2 += t1 - t7;
1657
 
1658
      tmp[14] = (t3 + t2) * tfcos36[17-14];
1659
      t0 = COS6_1 * (in[11] + in[15] - in[3]);
1660
      tmp[12] = (t3 - t2) * tfcos36[17-12];
1661
 
1662
      t4 -= t1 + t7;
1663
 
1664
      tmp[16] = (t5 - t0) * tfcos36[17-16];
1665
      tmp[10] = (t5 + t0) * tfcos36[17-10];
1666
      tmp[15] = (t6 + t4) * tfcos36[17-15];
1667
      tmp[11] = (t6 - t4) * tfcos36[17-11];
1668
   }
1669
 
1670
#define MACRO(v) { \
1671
    real tmpval; \
1672
    real sum0 = tmp[(v)]; \
1673
    real sum1 = tmp[17-(v)]; \
1674
    out2[9+(v)] = (tmpval = sum0 + sum1) * w[27+(v)]; \
1675
    out2[8-(v)] = tmpval * w[26-(v)]; \
1676
    sum0 -= sum1; \
1677
    ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1678
    ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)]; }
1679
 
1680
{
1681
   register real *out2 = o2;
1682
   register real *w = wintab;
1683
   register real *out1 = o1;
1684
   register real *ts = tsbuf;
1685
 
1686
   MACRO(0);
1687
   MACRO(1);
1688
   MACRO(2);
1689
   MACRO(3);
1690
   MACRO(4);
1691
   MACRO(5);
1692
   MACRO(6);
1693
   MACRO(7);
1694
   MACRO(8);
1695
}
1696
 
1697
#else
1698
 
1699
  {
1700
 
1701
#define MACRO0(v) { \
1702
    real tmp; \
1703
    out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1704
    out2[8-(v)] = tmp * w[26-(v)];  } \
1705
    sum0 -= sum1; \
1706
    ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1707
    ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)]; 
1708
#define MACRO1(v) { \
1709
        real sum0,sum1; \
1710
    sum0 = tmp1a + tmp2a; \
1711
        sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1712
        MACRO0(v); }
1713
#define MACRO2(v) { \
1714
    real sum0,sum1; \
1715
    sum0 = tmp2a - tmp1a; \
1716
    sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1717
        MACRO0(v); }
1718
 
1719
    register const real *c = COS9;
1720
    register real *out2 = o2;
1721
        register real *w = wintab;
1722
        register real *out1 = o1;
1723
        register real *ts = tsbuf;
1724
 
1725
    real ta33,ta66,tb33,tb66;
1726
 
1727
    ta33 = in[2*3+0] * c[3];
1728
    ta66 = in[2*6+0] * c[6];
1729
    tb33 = in[2*3+1] * c[3];
1730
    tb66 = in[2*6+1] * c[6];
1731
 
1732
    {
1733
      real tmp1a,tmp2a,tmp1b,tmp2b;
1734
      tmp1a =             in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1735
      tmp1b =             in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1736
      tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1737
      tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1738
 
1739
      MACRO1(0);
1740
      MACRO2(8);
1741
    }
1742
 
1743
    {
1744
      real tmp1a,tmp2a,tmp1b,tmp2b;
1745
      tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1746
      tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1747
      tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1748
      tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1749
 
1750
      MACRO1(1);
1751
      MACRO2(7);
1752
    }
1753
 
1754
    {
1755
      real tmp1a,tmp2a,tmp1b,tmp2b;
1756
      tmp1a =             in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1757
      tmp1b =             in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1758
      tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1759
      tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1760
 
1761
      MACRO1(2);
1762
      MACRO2(6);
1763
    }
1764
 
1765
    {
1766
      real tmp1a,tmp2a,tmp1b,tmp2b;
1767
      tmp1a =             in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1768
      tmp1b =             in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1769
      tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1770
      tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1771
 
1772
      MACRO1(3);
1773
      MACRO2(5);
1774
    }
1775
 
1776
        {
1777
                real sum0,sum1;
1778
        sum0 =  in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1779
        sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1780
                MACRO0(4);
1781
        }
1782
  }
1783
#endif
1784
 
1785
  }
1786
}
1787
 
1788
/*
1789
 * new DCT12
1790
 */
1791
static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1792
{
1793
#define DCT12_PART1 \
1794
             in5 = in[5*3];  \
1795
     in5 += (in4 = in[4*3]); \
1796
     in4 += (in3 = in[3*3]); \
1797
     in3 += (in2 = in[2*3]); \
1798
     in2 += (in1 = in[1*3]); \
1799
     in1 += (in0 = in[0*3]); \
1800
                             \
1801
     in5 += in3; in3 += in1; \
1802
                             \
1803
     in2 *= COS6_1; \
1804
     in3 *= COS6_1; \
1805
 
1806
#define DCT12_PART2 \
1807
     in0 += in4 * COS6_2; \
1808
                          \
1809
     in4 = in0 + in2;     \
1810
     in0 -= in2;          \
1811
                          \
1812
     in1 += in5 * COS6_2; \
1813
                          \
1814
     in5 = (in1 + in3) * tfcos12[0]; \
1815
     in1 = (in1 - in3) * tfcos12[2]; \
1816
                         \
1817
     in3 = in4 + in5;    \
1818
     in4 -= in5;         \
1819
                         \
1820
     in2 = in0 + in1;    \
1821
     in0 -= in1;
1822
 
1823
 
1824
   {
1825
     real in0,in1,in2,in3,in4,in5;
1826
     register real *out1 = rawout1;
1827
     ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1828
     ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1829
 
1830
     DCT12_PART1
1831
 
1832
     {
1833
       real tmp0,tmp1 = (in0 - in4);
1834
       {
1835
         real tmp2 = (in1 - in5) * tfcos12[1];
1836
         tmp0 = tmp1 + tmp2;
1837
         tmp1 -= tmp2;
1838
       }
1839
       ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1840
       ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1841
       ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1842
       ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1843
     }
1844
 
1845
     DCT12_PART2
1846
 
1847
     ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1848
     ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1849
     ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1850
     ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1851
 
1852
     ts[(6+0)*SBLIMIT]  = out1[6+0] + in0 * wi[0];
1853
     ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1854
     ts[(6+2)*SBLIMIT]  = out1[6+2] + in4 * wi[2];
1855
     ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1856
  }
1857
 
1858
  in++;
1859
 
1860
  {
1861
     real in0,in1,in2,in3,in4,in5;
1862
     register real *out2 = rawout2;
1863
 
1864
     DCT12_PART1
1865
 
1866
     {
1867
       real tmp0,tmp1 = (in0 - in4);
1868
       {
1869
         real tmp2 = (in1 - in5) * tfcos12[1];
1870
         tmp0 = tmp1 + tmp2;
1871
         tmp1 -= tmp2;
1872
       }
1873
       out2[5-1] = tmp0 * wi[11-1];
1874
       out2[0+1] = tmp0 * wi[6+1];
1875
       ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1876
       ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1877
     }
1878
 
1879
     DCT12_PART2
1880
 
1881
     out2[5-0] = in2 * wi[11-0];
1882
     out2[0+0] = in2 * wi[6+0];
1883
     out2[0+2] = in3 * wi[6+2];
1884
     out2[5-2] = in3 * wi[11-2];
1885
 
1886
     ts[(12+0)*SBLIMIT] += in0 * wi[0];
1887
     ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1888
     ts[(12+2)*SBLIMIT] += in4 * wi[2];
1889
     ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1890
  }
1891
 
1892
  in++;
1893
 
1894
  {
1895
     real in0,in1,in2,in3,in4,in5;
1896
     register real *out2 = rawout2;
1897
     out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1898
 
1899
     DCT12_PART1
1900
 
1901
     {
1902
       real tmp0,tmp1 = (in0 - in4);
1903
       {
1904
         real tmp2 = (in1 - in5) * tfcos12[1];
1905
         tmp0 = tmp1 + tmp2;
1906
         tmp1 -= tmp2;
1907
       }
1908
       out2[11-1] = tmp0 * wi[11-1];
1909
       out2[6 +1] = tmp0 * wi[6+1];
1910
       out2[0+1] += tmp1 * wi[1];
1911
       out2[5-1] += tmp1 * wi[5-1];
1912
     }
1913
 
1914
     DCT12_PART2
1915
 
1916
     out2[11-0] = in2 * wi[11-0];
1917
     out2[6 +0] = in2 * wi[6+0];
1918
     out2[6 +2] = in3 * wi[6+2];
1919
     out2[11-2] = in3 * wi[11-2];
1920
 
1921
     out2[0+0] += in0 * wi[0];
1922
     out2[5-0] += in0 * wi[5-0];
1923
     out2[0+2] += in4 * wi[2];
1924
     out2[5-2] += in4 * wi[5-2];
1925
  }
1926
}
1927
 
1928
/*
1929
 * III_hybrid
1930
 */
1931
static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1932
   int ch,struct gr_info_s *gr_info)
1933
{
1934
   real *tspnt = (real *) tsOut;
1935
   static real block[2][2][SBLIMIT*SSLIMIT] = { { { 0, } } };
1936
   static int blc[2]={0,0};
1937
   real *rawout1,*rawout2;
1938
   int bt;
1939
   int sb = 0;
1940
 
1941
   {
1942
     int b = blc[ch];
1943
     rawout1=block[b][ch];
1944
     b=-b+1;
1945
     rawout2=block[b][ch];
1946
     blc[ch] = b;
1947
   }
1948
 
1949
 
1950
   if(gr_info->mixed_block_flag) {
1951
     sb = 2;
1952
     dct36(fsIn[0],rawout1,rawout2,win[0],tspnt);
1953
     dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1954
     rawout1 += 36; rawout2 += 36; tspnt += 2;
1955
   }
1956
 
1957
   bt = gr_info->block_type;
1958
   if(bt == 2) {
1959
     for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1960
       dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt);
1961
       dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1962
     }
1963
   }
1964
   else {
1965
     for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1966
       dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1967
       dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1968
     }
1969
   }
1970
 
1971
   for(;sb<SBLIMIT;sb++,tspnt++) {
1972
     int i;
1973
     for(i=0;i<SSLIMIT;i++) {
1974
       tspnt[i*SBLIMIT] = *rawout1++;
1975
       *rawout2++ = 0.0;
1976
     }
1977
   }
1978
}
1979
 
1980
/*
1981
 * main layer3 handler
1982
 */
1983
int do_layer3(struct frame *fr,int outmode,struct audio_info_struct *ai)
1984
{
1985
  int gr, ch, ss,clip=0;
1986
  int scalefacs[39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1987
  struct III_sideinfo sideinfo;
1988
  int stereo = fr->stereo;
1989
  int single = fr->single;
1990
  int ms_stereo,i_stereo;
1991
  int sfreq = fr->sampling_frequency;
1992
  int stereo1,granules;
1993
 
1994
  if(stereo == 1) {
1995
    stereo1 = 1;
1996
    single = 0;
1997
  }
1998
  else if(single >= 0)
1999
    stereo1 = 1;
2000
  else
2001
    stereo1 = 2;
2002
 
2003
  ms_stereo = (fr->mode == MPG_MD_JOINT_STEREO) && (fr->mode_ext & 0x2);
2004
  i_stereo = (fr->mode == MPG_MD_JOINT_STEREO) && (fr->mode_ext & 0x1);
2005
 
2006
  if(fr->lsf) {
2007
    granules = 1;
2008
    III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single);
2009
  }
2010
  else {
2011
    granules = 2;
2012
    III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single);
2013
  }
2014
 
2015
  set_pointer(sideinfo.main_data_begin);
2016
 
2017
  for (gr=0;gr<granules;gr++)
2018
  {
2019
    static real hybridIn[2][SBLIMIT][SSLIMIT];
2020
    static real hybridOut[2][SSLIMIT][SBLIMIT];
2021
 
2022
    {
2023
      struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
2024
      long part2bits;
2025
      if(fr->lsf)
2026
        part2bits = III_get_scale_factors_2(scalefacs,gr_info,0);
2027
      else
2028
        part2bits = III_get_scale_factors_1(scalefacs,gr_info);
2029
      if(III_dequantize_sample(hybridIn[0], scalefacs,gr_info,sfreq,part2bits))
2030
        return clip;
2031
    }
2032
    if(stereo == 2) {
2033
      struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
2034
      long part2bits;
2035
      if(fr->lsf)
2036
        part2bits = III_get_scale_factors_2(scalefacs,gr_info,i_stereo);
2037
      else
2038
        part2bits = III_get_scale_factors_1(scalefacs,gr_info);
2039
      if(ms_stereo) {
2040
        if(III_dequantize_sample_ms(hybridIn,scalefacs,gr_info,sfreq,part2bits))
2041
          return clip;
2042
      }
2043
      else {
2044
        if(III_dequantize_sample(hybridIn[1],scalefacs,gr_info,sfreq,part2bits))
2045
          return clip;
2046
      }
2047
 
2048
      if(i_stereo)
2049
        III_i_stereo(hybridIn,scalefacs,gr_info,sfreq,ms_stereo,fr->lsf);
2050
 
2051
 
2052
      if(ms_stereo || i_stereo || (single == 3) ) {
2053
        if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
2054
          sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
2055
        else
2056
          gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
2057
      }
2058
 
2059
      switch(single) {
2060
        case 3:
2061
          {
2062
            register int i;
2063
            register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
2064
            for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++)
2065
              *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
2066
          }
2067
          break;
2068
        case 1:
2069
          {
2070
            register int i;
2071
            register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
2072
            for(i=0;i<SSLIMIT*gr_info->maxb;i++)
2073
              *in0++ = *in1++;
2074
          }
2075
          break;
2076
      }
2077
    }
2078
 
2079
    for(ch=0;ch<stereo1;ch++) {
2080
      struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
2081
      III_antialias(hybridIn[ch],gr_info);
2082
      III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info);
2083
    }
2084
 
2085
    for(ss=0;ss<SSLIMIT;ss++) {
2086
      if(single >= 0) {
2087
        clip += (fr->synth_mono)(hybridOut[0][ss],pcm_sample+pcm_point);
2088
      }
2089
      else {
2090
        clip += (fr->synth)(hybridOut[0][ss],0,pcm_sample+pcm_point);
2091
        clip += (fr->synth)(hybridOut[1][ss],1,pcm_sample+pcm_point);
2092
      }
2093
      pcm_point += fr->block_size;
2094
 
2095
#ifdef VARMODESUPPORT
2096
      if (playlimit < 128) {
2097
        pcm_point -= playlimit >> 1;
2098
        playlimit = 0;
2099
      }
2100
      else
2101
        playlimit -= 128;
2102
#endif
2103
 
2104
      if(pcm_point >= audiobufsize)
2105
        audio_flush(outmode,ai);
2106
    }
2107
  }
2108
 
2109
  return clip;
2110
}
2111
 
2112