~ubuntu-branches/ubuntu/lucid/mpg123/lucid

« back to all changes in this revision

Viewing changes to src/layer3.c

Tags: upstream-0.60
ImportĀ upstreamĀ versionĀ 0.60

Show diffs side-by-side

added added

removed removed

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