Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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