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

« back to all changes in this revision

Viewing changes to mpglib/layer3.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2004-10-26 23:48:41 UTC
  • Revision ID: james.westby@ubuntu.com-20041026234841-vyigddlv7kb52nxh
Tags: upstream-0.59r
ImportĀ upstreamĀ versionĀ 0.59r

Show diffs side-by-side

added added

removed removed

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