~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpg123_artsplugin/mpg123/layer3.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

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