2
leyer3.c: the layer 3 decoder
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
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
11
The int-vs-pointer situation has to be cleaned up.
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];
32
static real COS6_1,COS6_2;
36
static real COS6_1,COS6_2;
37
static real tfcos36[9];
39
static real tfcos12[3];
42
static real cos9[3],cos18[3];
45
struct bandInfoStruct {
53
int shortLimit[9][14];
55
struct bandInfoStruct bandInfo[9] = {
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} } ,
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} } ,
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} } ,
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 } } ,
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 } } ,
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 } } ,
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} } ,
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];
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 */
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];
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 */
123
/* input in bytes already */
124
void layer3_gapless_init(unsigned long b, unsigned long e)
130
debug2("layer3_gapless_init: from %lu to %lu samples", begin, end);
133
void layer3_gapless_set_position(unsigned long frames, struct frame* fr, struct audio_info_struct *ai)
135
position = samples_to_bytes(frames*spf(fr), fr, ai);
136
debug1("set; position now %lu", position);
139
void layer3_gapless_bytify(struct frame *fr, struct audio_info_struct *ai)
143
begin = samples_to_bytes(begin, fr, ai);
144
end = samples_to_bytes(end, fr, ai);
146
debug2("bytified: begin=%lu; end=%5lu", begin, end);
151
take the (partially or fully) filled and remove stuff for gapless mode if needed
152
pcm_point may then be smaller than before...
154
void layer3_gapless_buffercheck()
156
/* pcm_point bytes added since last position... */
157
unsigned long new_pos = position + pcm_point;
158
if(begin && (position < begin))
160
debug4("new_pos %lu (old: %lu), begin %lu, pcm_point %i", new_pos, position, begin, pcm_point);
161
if(new_pos < begin) pcm_point = 0; /* full of padding/delay */
164
/* we need to shift the memory to the left... */
165
debug3("old pcm_point: %i, begin %lu; good bytes: %i", pcm_point, begin, (int)(new_pos-begin));
166
pcm_point -= begin-position;
167
debug3("shifting %i bytes from %p to %p", pcm_point, pcm_sample+(int)(begin-position), pcm_sample);
168
memmove(pcm_sample, pcm_sample+(int)(begin-position), pcm_point);
171
/* I don't cover the case with both end and begin in chunk! */
172
else if(end && (new_pos > end))
174
/* either end in current chunk or chunk totally out */
175
debug2("ending at position %lu / point %i", new_pos, pcm_point);
176
if(position < end) pcm_point -= new_pos-end;
178
debug1("set pcm_point to %i", pcm_point);
185
* init tables for layer-3
187
void init_layer3(int down_sample_sblimit)
191
for(i=-256;i<118+4;i++)
193
if(!param.down_sample)
194
gainpow2[i+256] = 16384.0 * pow((double)2.0,-0.25 * (double) (i+210) );
197
gainpow2[i+256] = DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210)));
200
ispow[i] = DOUBLE_TO_REAL(pow((double)i,(double)4.0/3.0));
203
static double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
204
double sq=sqrt(1.0+Ci[i]*Ci[i]);
205
aa_cs[i] = DOUBLE_TO_REAL(1.0/sq);
206
aa_ca[i] = DOUBLE_TO_REAL(Ci[i]/sq);
210
win[0][i] = win[1][i] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 72.0 * (double) (2*(i+0) +1) ) / cos ( M_PI * (double) (2*(i+0) +19) / 72.0 ));
211
win[0][i+18] = win[3][i+18] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 72.0 * (double) (2*(i+18)+1) ) / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 ));
214
win[1][i+18] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 ));
215
win[3][i+12] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 ));
216
win[1][i+24] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 ));
217
win[1][i+30] = win[3][i] = DOUBLE_TO_REAL(0.0);
218
win[3][i+6 ] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 ));
222
COS9[i] = DOUBLE_TO_REAL(cos( M_PI / 18.0 * (double) i));
225
tfcos36[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 ));
227
tfcos12[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 ));
229
COS6_1 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 1));
230
COS6_2 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 2));
233
cos9[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/9.0));
234
cos9[1] = DOUBLE_TO_REAL(cos(5.0*M_PI/9.0));
235
cos9[2] = DOUBLE_TO_REAL(cos(7.0*M_PI/9.0));
236
cos18[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/18.0));
237
cos18[1] = DOUBLE_TO_REAL(cos(11.0*M_PI/18.0));
238
cos18[2] = DOUBLE_TO_REAL(cos(13.0*M_PI/18.0));
242
win[2][i] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 ));
244
COS1[i][j] = DOUBLE_TO_REAL(cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) ));
248
static int len[4] = { 36,36,12,36 };
249
for(i=0;i<len[j];i+=2)
250
win1[j][i] = + win[j][i];
251
for(i=1;i<len[j];i+=2)
252
win1[j][i] = - win[j][i];
256
double t = tan( (double) i * M_PI / 12.0 );
257
tan1_1[i] = DOUBLE_TO_REAL(t / (1.0+t));
258
tan2_1[i] = DOUBLE_TO_REAL(1.0 / (1.0 + t));
259
tan1_2[i] = DOUBLE_TO_REAL(M_SQRT2 * t / (1.0+t));
260
tan2_2[i] = DOUBLE_TO_REAL(M_SQRT2 / (1.0 + t));
263
double base = pow(2.0,-0.25*(j+1.0));
264
double p1=1.0,p2=1.0;
267
p1 = pow(base,(i+1.0)*0.5);
269
p2 = pow(base,i*0.5);
271
pow1_1[j][i] = DOUBLE_TO_REAL(p1);
272
pow2_1[j][i] = DOUBLE_TO_REAL(p2);
273
pow1_2[j][i] = DOUBLE_TO_REAL(M_SQRT2 * p1);
274
pow2_2[j][i] = DOUBLE_TO_REAL(M_SQRT2 * p2);
279
struct bandInfoStruct *bi = &bandInfo[j];
284
mp = map[j][0] = mapbuf0[j];
286
for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
292
bdf = bi->shortDiff+3;
293
for(cb=3;cb<13;cb++) {
294
int l = (*bdf++) >> 1;
295
for(lwin=0;lwin<3;lwin++) {
305
mp = map[j][1] = mapbuf1[j];
306
bdf = bi->shortDiff+0;
307
for(i=0,cb=0;cb<13;cb++) {
308
int l = (*bdf++) >> 1;
309
for(lwin=0;lwin<3;lwin++) {
319
mp = map[j][2] = mapbuf2[j];
321
for(cb = 0; cb < 22 ; cb++) {
322
*mp++ = (*bdf++) >> 1;
331
longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
332
if(longLimit[j][i] > (down_sample_sblimit) )
333
longLimit[j][i] = down_sample_sblimit;
336
shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
337
if(shortLimit[j][i] > (down_sample_sblimit) )
338
shortLimit[j][i] = down_sample_sblimit;
345
int n = k + j * 6 + i * 36;
346
i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
353
int n = k + j * 4 + i * 16;
354
i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
361
i_slen2[n+244] = i|(j<<3) | (5<<12);
362
n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
370
int n = l + k * 4 + j * 16 + i * 80;
371
n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
379
int n = k + j * 4 + i * 20;
380
n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
387
* read additional side information (for MPEG 1 and MPEG 2)
389
static int III_get_side_info(struct III_sideinfo *si,int stereo,
390
int ms_stereo,long sfreq,int single,int lsf)
393
int powdiff = (single == 3) ? 4 : 0;
395
static const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } };
396
const int *tab = tabs[lsf];
398
si->main_data_begin = getbits(tab[1]);
400
si->private_bits = getbits_fast(tab[2]);
402
si->private_bits = getbits_fast(tab[3]);
405
for (ch=0; ch<stereo; ch++) {
406
si->ch[ch].gr[0].scfsi = -1;
407
si->ch[ch].gr[1].scfsi = getbits_fast(4);
411
for (gr=0; gr<tab[0]; gr++) {
412
for (ch=0; ch<stereo; ch++) {
413
register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
415
gr_info->part2_3_length = getbits(12);
416
gr_info->big_values = getbits(9);
417
if(gr_info->big_values > 288) {
418
error("big_values too large!");
419
gr_info->big_values = 288;
421
gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
423
gr_info->pow2gain += 2;
424
gr_info->scalefac_compress = getbits(tab[4]);
426
if(get1bit()) { /* window switch flag */
428
gr_info->block_type = getbits_fast(2);
429
gr_info->mixed_block_flag = get1bit();
430
gr_info->table_select[0] = getbits_fast(5);
431
gr_info->table_select[1] = getbits_fast(5);
433
* table_select[2] not needed, because there is no region2,
434
* but to satisfy some verifications tools we set it either.
436
gr_info->table_select[2] = 0;
438
gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
440
if(gr_info->block_type == 0) {
441
error("Blocktype == 0 and window-switching == 1 not allowed.");
446
/* region_count/start parameters are implicit in this case. */
447
if(!lsf || gr_info->block_type == 2)
448
gr_info->region1start = 36>>1;
450
/* check this again for 2.5 and sfreq=8 */
452
gr_info->region1start = 108>>1;
454
gr_info->region1start = 54>>1;
456
gr_info->region2start = 576>>1;
461
gr_info->table_select[i] = getbits_fast(5);
462
r0c = getbits_fast(4);
463
r1c = getbits_fast(3);
464
gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
465
gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
466
gr_info->block_type = 0;
467
gr_info->mixed_block_flag = 0;
470
gr_info->preflag = get1bit();
471
gr_info->scalefac_scale = get1bit();
472
gr_info->count1table_select = get1bit();
481
static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info,int ch,int gr)
483
static const unsigned char slen[2][16] = {
484
{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
485
{0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
488
int num0 = slen[0][gr_info->scalefac_compress];
489
int num1 = slen[1][gr_info->scalefac_compress];
491
if (gr_info->block_type == 2) {
493
numbits = (num0 + num1) * 18;
495
if (gr_info->mixed_block_flag) {
497
*scf++ = getbits_fast(num0);
499
numbits -= num0; /* num0 * 17 + num1 * 18 */
503
*scf++ = getbits_fast(num0);
505
*scf++ = getbits_fast(num1);
506
*scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
510
int scfsi = gr_info->scfsi;
512
if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
514
*scf++ = getbits_fast(num0);
516
*scf++ = getbits_fast(num1);
517
numbits = (num0 + num1) * 10 + num0;
524
*scf++ = getbits_fast(num0);
533
*scf++ = getbits_fast(num0);
542
*scf++ = getbits_fast(num1);
551
*scf++ = getbits_fast(num1);
557
*scf++ = 0; /* no l[21] in original sources */
563
static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
566
int i,j,n=0,numbits=0;
569
static unsigned char stab[3][6][4] = {
570
{ { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
571
{ 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
572
{ { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
573
{12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
574
{ { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
575
{ 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
577
if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
578
slen = i_slen2[gr_info->scalefac_compress>>1];
580
slen = n_slen2[gr_info->scalefac_compress];
582
gr_info->preflag = (slen>>15) & 0x1;
585
if( gr_info->block_type == 2 ) {
587
if(gr_info->mixed_block_flag)
591
pnt = stab[n][(slen>>12)&0x7];
594
int num = slen & 0x7;
597
for(j=0;j<(int)(pnt[i]);j++)
598
*scf++ = getbits_fast(num);
599
numbits += pnt[i] * num;
602
for(j=0;j<(int)(pnt[i]);j++)
614
static int pretab1[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
615
static int pretab2[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
618
* Dequantize samples (includes huffman decoding)
620
/* 24 is enough because tab13 has max. a 19 bit huffvector */
621
#define BITSHIFT ((sizeof(long)-1)*8)
622
#define REFRESH_MASK \
623
while(num < BITSHIFT) { \
624
mask |= ((unsigned long)getbyte())<<(BITSHIFT-num); \
628
static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
629
struct gr_info_s *gr_info,int sfreq,int part2bits)
631
int shift = 1 + gr_info->scalefac_scale;
632
real *xrpnt = (real *) xr;
634
int part2remain = gr_info->part2_3_length - part2bits;
637
/* mhipp tree has this split up a bit... */
638
int num=getbitoffset();
639
long mask = (long) getbits(num)<<(BITSHIFT+8-num);
643
int bv = gr_info->big_values;
644
int region1 = gr_info->region1start;
645
int region2 = gr_info->region2start;
646
if(region1 > region2)
648
error("You got some really nasty file there... region1>region2!");
651
l3 = ((576>>1)-bv)>>1;
653
* we may lose the 'odd' bit here !!
654
* check this later again
657
l[0] = bv; l[1] = 0; l[2] = 0;
662
l[1] = bv - l[0]; l[2] = 0;
665
l[1] = region2 - l[0]; l[2] = bv - region2;
670
if(gr_info->block_type == 2) {
672
* decoding with short or mixed mode BandIndex table
675
int step=0,lwin=3,cb=0;
676
register real v = 0.0;
679
if(gr_info->mixed_block_flag) {
681
max[0] = max[1] = max[2] = 2;
683
me = mapend[sfreq][0];
686
max[0] = max[1] = max[2] = max[3] = -1;
687
/* max[3] not really needed in this case */
689
me = mapend[sfreq][1];
695
struct newhuff *h = ht+gr_info->table_select[i];
700
xrpnt = ((real *) xr) + (*m++);
704
v = gr_info->pow2gain[(*scf++) << shift];
708
v = gr_info->full_gain[lwin][(*scf++) << shift];
713
register short *val = h->table;
715
while((y=*val++)<0) {
724
if(x == 15 && h->linbits) {
727
x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
731
*xrpnt = REAL_MUL(-ispow[x], v);
733
*xrpnt = REAL_MUL(ispow[x], v);
739
*xrpnt = REAL_MUL(-ispow[x], v);
741
*xrpnt = REAL_MUL(ispow[x], v);
746
*xrpnt = DOUBLE_TO_REAL(0.0);
748
if(y == 15 && h->linbits) {
751
y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
755
*xrpnt = REAL_MUL(-ispow[y], v);
757
*xrpnt = REAL_MUL(ispow[y], v);
763
*xrpnt = REAL_MUL(-ispow[y], v);
765
*xrpnt = REAL_MUL(ispow[y], v);
770
*xrpnt = DOUBLE_TO_REAL(0.0);
775
for(;l3 && (part2remain+num > 0);l3--) {
776
/* not mixing code and declarations to keep C89 happy */
780
/* This is only a humble hack to prevent a special segfault. */
781
/* More insight into the real workings is still needed. */
782
/* especially why there are (valid?) files that make xrpnt exceed the array with 4 bytes without segfaulting, more seems to be really bad, though. */
784
if(!(xrpnt < &xr[SBLIMIT][0]))
786
if(param.verbose) debug2("attempted soft xrpnt overflow (%p !< %p) ?", (void*) xrpnt, (void*) &xr[SBLIMIT][0]);
789
if(!(xrpnt < &xr[SBLIMIT][0]+5))
791
error2("attempted xrpnt overflow (%p !< %p)", (void*) xrpnt, (void*) &xr[SBLIMIT][0]);
794
h = htc+gr_info->count1table_select;
798
while((a=*val++)<0) {
804
if(part2remain+num <= 0) {
805
num -= part2remain+num;
813
xrpnt = ((real *) xr) + (*m++);
817
v = gr_info->pow2gain[(*scf++) << shift];
821
v = gr_info->full_gain[lwin][(*scf++) << shift];
827
if( (a & (0x8>>i)) ) {
829
if(part2remain+num <= 0) {
840
*xrpnt = DOUBLE_TO_REAL(0.0);
845
if(lwin < 3) { /* short band? */
848
*xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; /* short band -> step=3 */
849
*xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3;
854
xrpnt = ((real *) xr) + *m++;
856
break; /* optimize: field will be set to zero at the end of the function */
861
gr_info->maxband[0] = max[0]+1;
862
gr_info->maxband[1] = max[1]+1;
863
gr_info->maxband[2] = max[2]+1;
864
gr_info->maxbandl = max[3]+1;
867
int rmax = max[0] > max[1] ? max[0] : max[1];
868
rmax = (rmax > max[2] ? rmax : max[2]) + 1;
869
gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
875
* decoding with 'long' BandIndex table (block_type != 2)
877
int *pretab = gr_info->preflag ? pretab1 : pretab2;
880
int *m = map[sfreq][2];
881
register real v = 0.0;
885
* long hash table values
889
struct newhuff *h = ht+gr_info->table_select[i];
899
v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
903
register short *val = h->table;
905
while((y=*val++)<0) {
915
if (x == 15 && h->linbits) {
918
x += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
922
*xrpnt++ = REAL_MUL(-ispow[x], v);
924
*xrpnt++ = REAL_MUL(ispow[x], v);
930
*xrpnt++ = REAL_MUL(-ispow[x], v);
932
*xrpnt++ = REAL_MUL(ispow[x], v);
937
*xrpnt++ = DOUBLE_TO_REAL(0.0);
939
if (y == 15 && h->linbits) {
942
y += ((unsigned long) mask) >> (BITSHIFT+8-h->linbits);
946
*xrpnt++ = REAL_MUL(-ispow[y], v);
948
*xrpnt++ = REAL_MUL(ispow[y], v);
954
*xrpnt++ = REAL_MUL(-ispow[y], v);
956
*xrpnt++ = REAL_MUL(ispow[y], v);
961
*xrpnt++ = DOUBLE_TO_REAL(0.0);
966
* short (count1table) values
968
for(;l3 && (part2remain+num > 0);l3--) {
969
struct newhuff *h = htc+gr_info->count1table_select;
970
register short *val = h->table,a;
973
while((a=*val++)<0) {
979
if(part2remain+num <= 0) {
980
num -= part2remain+num;
992
v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
996
if ( (a & (0x8>>i)) ) {
998
if(part2remain+num <= 0) {
1009
*xrpnt++ = DOUBLE_TO_REAL(0.0);
1013
gr_info->maxbandl = max+1;
1014
gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
1021
while(xrpnt < &xr[SBLIMIT][0])
1022
*xrpnt++ = DOUBLE_TO_REAL(0.0);
1024
while( part2remain > 16 ) {
1025
getbits(16); /* Dismiss stuffing Bits */
1029
getbits(part2remain);
1030
else if(part2remain < 0) {
1031
debug1("Can't rewind stream by %d bits!",-part2remain);
1032
return 1; /* -> error */
1038
* III_stereo: calculate real channel values for Joint-I-Stereo-mode
1040
static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
1041
struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
1043
real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
1044
struct bandInfoStruct *bi = &bandInfo[sfreq];
1046
const real *tab1,*tab2;
1050
/* TODO: optimize as static */
1051
static const real *tabs[3][2][2] = {
1052
{ { tan1_1,tan2_1 } , { tan1_2,tan2_2 } },
1053
{ { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } } ,
1054
{ { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } }
1057
tab = lsf + (gr_info->scalefac_compress & lsf);
1058
tab1 = tabs[tab][ms_stereo][0];
1059
tab2 = tabs[tab][ms_stereo][1];
1062
int p = gr_info->scalefac_compress & 0x1;
1064
tab1 = pow1_2[p]; tab2 = pow2_2[p];
1067
tab1 = pow1_1[p]; tab2 = pow2_1[p];
1072
tab1 = tan1_2; tab2 = tan2_2;
1075
tab1 = tan1_1; tab2 = tan2_1;
1080
if (gr_info->block_type == 2) {
1082
if( gr_info->mixed_block_flag )
1085
for (lwin=0;lwin<3;lwin++) { /* process each window */
1086
/* get first band with zero values */
1087
int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
1091
for(;sfb<12;sfb++) {
1092
is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1095
sb = bi->shortDiff[sfb];
1096
idx = bi->shortIdx[sfb] + lwin;
1097
t1 = tab1[is_p]; t2 = tab2[is_p];
1098
for (; sb > 0; sb--,idx+=3) {
1099
real v = xr[0][idx];
1100
xr[0][idx] = REAL_MUL(v, t1);
1101
xr[1][idx] = REAL_MUL(v, t2);
1107
/* in the original: copy 10 to 11 , here: copy 11 to 12
1108
maybe still wrong??? (copy 12 to 13?) */
1109
is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1110
sb = bi->shortDiff[12];
1111
idx = bi->shortIdx[12] + lwin;
1113
is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1114
sb = bi->shortDiff[11];
1115
idx = bi->shortIdx[11] + lwin;
1119
t1 = tab1[is_p]; t2 = tab2[is_p];
1120
for ( ; sb > 0; sb--,idx+=3 ) {
1121
real v = xr[0][idx];
1122
xr[0][idx] = REAL_MUL(v, t1);
1123
xr[1][idx] = REAL_MUL(v, t2);
1126
} /* end for(lwin; .. ; . ) */
1128
/* also check l-part, if ALL bands in the three windows are 'empty'
1129
* and mode = mixed_mode
1132
int sfb = gr_info->maxbandl;
1133
int idx = bi->longIdx[sfb];
1135
for ( ; sfb<8; sfb++ ) {
1136
int sb = bi->longDiff[sfb];
1137
int is_p = scalefac[sfb]; /* scale: 0-15 */
1140
t1 = tab1[is_p]; t2 = tab2[is_p];
1141
for ( ; sb > 0; sb--,idx++) {
1142
real v = xr[0][idx];
1143
xr[0][idx] = REAL_MUL(v, t1);
1144
xr[1][idx] = REAL_MUL(v, t2);
1152
else { /* ((gr_info->block_type != 2)) */
1153
int sfb = gr_info->maxbandl;
1155
if(sfb > 21) return; /* tightened fix for CVE-2006-1655 */
1156
idx = bi->longIdx[sfb];
1157
for ( ; sfb<21; sfb++) {
1158
int sb = bi->longDiff[sfb];
1159
is_p = scalefac[sfb]; /* scale: 0-15 */
1162
t1 = tab1[is_p]; t2 = tab2[is_p];
1163
for ( ; sb > 0; sb--,idx++) {
1164
real v = xr[0][idx];
1165
xr[0][idx] = REAL_MUL(v, t1);
1166
xr[1][idx] = REAL_MUL(v, t2);
1173
is_p = scalefac[20];
1174
if(is_p != 7) { /* copy l-band 20 to l-band 21 */
1176
real t1 = tab1[is_p],t2 = tab2[is_p];
1178
for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ ) {
1179
real v = xr[0][idx];
1180
xr[0][idx] = REAL_MUL(v, t1);
1181
xr[1][idx] = REAL_MUL(v, t2);
1187
static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info) {
1190
if(gr_info->block_type == 2) {
1191
if(!gr_info->mixed_block_flag)
1196
sblim = gr_info->maxb-1;
1199
/* 31 alias-reduction operations between each pair of sub-bands */
1200
/* with 8 butterflies between each pair */
1204
real *xr1=(real *) xr[1];
1206
for(sb=sblim;sb;sb--,xr1+=10) {
1208
real *cs=aa_cs,*ca=aa_ca;
1211
for(ss=7;ss>=0;ss--)
1212
{ /* upper and lower butterfly inputs */
1213
register real bu = *--xr2,bd = *xr1;
1214
*xr2 = REAL_MUL(bu, *cs) - REAL_MUL(bd, *ca);
1215
*xr1++ = REAL_MUL(bd, *cs++) + REAL_MUL(bu, *ca++);
1222
// This is an optimized DCT from Jeff Tsay's maplay 1.2+ package.
1223
// Saved one multiplication by doing the 'twiddle factor' stuff
1224
// together with the window mul. (MH)
1226
// This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the
1227
// 9 point IDCT needs to be reduced further. Unfortunately, I don't
1228
// know how to do that, because 9 is not an even number. - Jeff.
1230
//////////////////////////////////////////////////////////////////
1232
// 9 Point Inverse Discrete Cosine Transform
1234
// This piece of code is Copyright 1997 Mikko Tommila and is freely usable
1235
// by anybody. The algorithm itself is of course in the public domain.
1237
// Again derived heuristically from the 9-point WFTA.
1239
// The algorithm is optimized (?) for speed, not for small rounding errors or
1240
// good readability.
1242
// 36 additions, 11 multiplications
1244
// Again this is very likely sub-optimal.
1246
// The code is optimized to use a minimum number of temporary variables,
1247
// so it should compile quite well even on 8-register Intel x86 processors.
1248
// This makes the code quite obfuscated and very difficult to understand.
1251
// [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1252
// Mathematics of Computation, Volume 32, Number 141, January 1978,
1256
/*------------------------------------------------------------------*/
1258
/* Function: Calculation of the inverse MDCT */
1260
/*------------------------------------------------------------------*/
1262
void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1264
static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1272
register real *in = inbuf;
1274
in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1275
in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1276
in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8];
1277
in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5];
1278
in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2];
1279
in[2] +=in[1]; in[1] +=in[0];
1281
in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1282
in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1];
1292
t0 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
1293
t1 = REAL_MUL(COS6_2, in[12]);
1297
tmp[1] = tmp[7] = t2 - t0;
1298
tmp[4] = t2 + t0 + t0;
1301
t2 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
1308
t0 = REAL_MUL(cos9[0], (in[4] + in[8] ));
1309
t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
1310
t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
1312
tmp[2] = tmp[6] = t3 - t0 - t2;
1313
tmp[0] = tmp[8] = t3 + t0 + t1;
1314
tmp[3] = tmp[5] = t3 - t1 + t2;
1320
t1 = REAL_MUL(cos18[0], (in[2] + in[10]));
1321
t2 = REAL_MUL(cos18[1], (in[10] - in[14]));
1322
t3 = REAL_MUL(COS6_1, in[6]);
1325
real t0 = t1 + t2 + t3;
1333
t3 = REAL_MUL(cos18[2], (in[2] + in[14]));
1346
real t0, t1, t2, t3, t4, t5, t6, t7;
1348
t1 = REAL_MUL(COS6_2, in[12]);
1349
t2 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
1352
t4 = in[0] - t1 - t1;
1354
tmp[4] = t4 + t2 + t2;
1356
t0 = REAL_MUL(cos9[0], (in[4] + in[8]));
1357
t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
1359
t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
1365
t2 = REAL_MUL(cos18[0], (in[2] + in[10]));
1366
t4 = REAL_MUL(cos18[1], (in[10] - in[14]));
1367
t7 = REAL_MUL(COS6_1, in[6]);
1372
t1 = REAL_MUL(cos18[2], (in[2] + in[14]));
1376
t0 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
1389
real t0, t1, t2, t3, t4, t5, t6, t7;
1391
t1 = REAL_MUL(COS6_2, in[13]);
1392
t2 = REAL_MUL(COS6_2, (in[9] + in[17] - in[5]));
1395
t4 = in[1] - t1 - t1;
1398
t0 = REAL_MUL(cos9[0], (in[5] + in[9]));
1399
t1 = REAL_MUL(cos9[1], (in[9] - in[17]));
1401
tmp[13] = REAL_MUL((t4 + t2 + t2), tfcos36[17-13]);
1402
t2 = REAL_MUL(cos9[2], (in[5] + in[17]));
1408
t2 = REAL_MUL(cos18[0], (in[3] + in[11]));
1409
t4 = REAL_MUL(cos18[1], (in[11] - in[15]));
1410
t7 = REAL_MUL(COS6_1, in[7]);
1413
tmp[17] = REAL_MUL((t0 + t1), tfcos36[17-17]);
1414
tmp[9] = REAL_MUL((t0 - t1), tfcos36[17-9]);
1415
t1 = REAL_MUL(cos18[2], (in[3] + in[15]));
1418
tmp[14] = REAL_MUL((t3 + t2), tfcos36[17-14]);
1419
t0 = REAL_MUL(COS6_1, (in[11] + in[15] - in[3]));
1420
tmp[12] = REAL_MUL((t3 - t2), tfcos36[17-12]);
1424
tmp[16] = REAL_MUL((t5 - t0), tfcos36[17-16]);
1425
tmp[10] = REAL_MUL((t5 + t0), tfcos36[17-10]);
1426
tmp[15] = REAL_MUL((t6 + t4), tfcos36[17-15]);
1427
tmp[11] = REAL_MUL((t6 - t4), tfcos36[17-11]);
1430
#define MACRO(v) { \
1432
tmpval = tmp[(v)] + tmp[17-(v)]; \
1433
out2[9+(v)] = REAL_MUL(tmpval, w[27+(v)]); \
1434
out2[8-(v)] = REAL_MUL(tmpval, w[26-(v)]); \
1435
tmpval = tmp[(v)] - tmp[17-(v)]; \
1436
ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(tmpval, w[8-(v)]); \
1437
ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(tmpval, w[9+(v)]); }
1440
register real *out2 = o2;
1441
register real *w = wintab;
1442
register real *out1 = o1;
1443
register real *ts = tsbuf;
1460
#define MACRO0(v) { \
1462
out2[9+(v)] = REAL_MUL((tmp = sum0 + sum1), w[27+(v)]); \
1463
out2[8-(v)] = REAL_MUL(tmp, w[26-(v)]); } \
1465
ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(sum0, w[8-(v)]); \
1466
ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(sum0, w[9+(v)]);
1467
#define MACRO1(v) { \
1469
sum0 = tmp1a + tmp2a; \
1470
sum1 = REAL_MUL((tmp1b + tmp2b), tfcos36[(v)]); \
1472
#define MACRO2(v) { \
1474
sum0 = tmp2a - tmp1a; \
1475
sum1 = REAL_MUL((tmp2b - tmp1b), tfcos36[(v)]); \
1478
register const real *c = COS9;
1479
register real *out2 = o2;
1480
register real *w = wintab;
1481
register real *out1 = o1;
1482
register real *ts = tsbuf;
1484
real ta33,ta66,tb33,tb66;
1486
ta33 = REAL_MUL(in[2*3+0], c[3]);
1487
ta66 = REAL_MUL(in[2*6+0], c[6]);
1488
tb33 = REAL_MUL(in[2*3+1], c[3]);
1489
tb66 = REAL_MUL(in[2*6+1], c[6]);
1492
real tmp1a,tmp2a,tmp1b,tmp2b;
1493
tmp1a = REAL_MUL(in[2*1+0], c[1]) + ta33 + REAL_MUL(in[2*5+0], c[5]) + REAL_MUL(in[2*7+0], c[7]);
1494
tmp1b = REAL_MUL(in[2*1+1], c[1]) + tb33 + REAL_MUL(in[2*5+1], c[5]) + REAL_MUL(in[2*7+1], c[7]);
1495
tmp2a = REAL_MUL(in[2*2+0], c[2]) + REAL_MUL(in[2*4+0], c[4]) + ta66 + REAL_MUL(in[2*8+0], c[8]);
1496
tmp2b = REAL_MUL(in[2*2+1], c[2]) + REAL_MUL(in[2*4+1], c[4]) + tb66 + REAL_MUL(in[2*8+1], c[8]);
1503
real tmp1a,tmp2a,tmp1b,tmp2b;
1504
tmp1a = REAL_MUL(( in[2*1+0] - in[2*5+0] - in[2*7+0] ), c[3]);
1505
tmp1b = REAL_MUL(( in[2*1+1] - in[2*5+1] - in[2*7+1] ), c[3]);
1506
tmp2a = REAL_MUL(( in[2*2+0] - in[2*4+0] - in[2*8+0] ), c[6]) - in[2*6+0] + in[2*0+0];
1507
tmp2b = REAL_MUL(( in[2*2+1] - in[2*4+1] - in[2*8+1] ), c[6]) - in[2*6+1] + in[2*0+1];
1514
real tmp1a,tmp2a,tmp1b,tmp2b;
1515
tmp1a = REAL_MUL(in[2*1+0], c[5]) - ta33 - REAL_MUL(in[2*5+0], c[7]) + REAL_MUL(in[2*7+0], c[1]);
1516
tmp1b = REAL_MUL(in[2*1+1], c[5]) - tb33 - REAL_MUL(in[2*5+1], c[7]) + REAL_MUL(in[2*7+1], c[1]);
1517
tmp2a = - REAL_MUL(in[2*2+0], c[8]) - REAL_MUL(in[2*4+0], c[2]) + ta66 + REAL_MUL(in[2*8+0], c[4]);
1518
tmp2b = - REAL_MUL(in[2*2+1], c[8]) - REAL_MUL(in[2*4+1], c[2]) + tb66 + REAL_MUL(in[2*8+1], c[4]);
1525
real tmp1a,tmp2a,tmp1b,tmp2b;
1526
tmp1a = REAL_MUL(in[2*1+0], c[7]) - ta33 + REAL_MUL(in[2*5+0], c[1]) - REAL_MUL(in[2*7+0], c[5]);
1527
tmp1b = REAL_MUL(in[2*1+1], c[7]) - tb33 + REAL_MUL(in[2*5+1], c[1]) - REAL_MUL(in[2*7+1], c[5]);
1528
tmp2a = - REAL_MUL(in[2*2+0], c[4]) + REAL_MUL(in[2*4+0], c[8]) + ta66 - REAL_MUL(in[2*8+0], c[2]);
1529
tmp2b = - REAL_MUL(in[2*2+1], c[4]) + REAL_MUL(in[2*4+1], c[8]) + tb66 - REAL_MUL(in[2*8+1], c[2]);
1537
sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1538
sum1 = REAL_MUL((in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ), tfcos36[4]);
1550
static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1552
#define DCT12_PART1 \
1554
in5 += (in4 = in[4*3]); \
1555
in4 += (in3 = in[3*3]); \
1556
in3 += (in2 = in[2*3]); \
1557
in2 += (in1 = in[1*3]); \
1558
in1 += (in0 = in[0*3]); \
1560
in5 += in3; in3 += in1; \
1562
in2 = REAL_MUL(in2, COS6_1); \
1563
in3 = REAL_MUL(in3, COS6_1); \
1565
#define DCT12_PART2 \
1566
in0 += REAL_MUL(in4, COS6_2); \
1571
in1 += REAL_MUL(in5, COS6_2); \
1573
in5 = REAL_MUL((in1 + in3), tfcos12[0]); \
1574
in1 = REAL_MUL((in1 - in3), tfcos12[2]); \
1584
real in0,in1,in2,in3,in4,in5;
1585
register real *out1 = rawout1;
1586
ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1587
ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1592
real tmp0,tmp1 = (in0 - in4);
1594
real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1598
ts[(17-1)*SBLIMIT] = out1[17-1] + REAL_MUL(tmp0, wi[11-1]);
1599
ts[(12+1)*SBLIMIT] = out1[12+1] + REAL_MUL(tmp0, wi[6+1]);
1600
ts[(6 +1)*SBLIMIT] = out1[6 +1] + REAL_MUL(tmp1, wi[1]);
1601
ts[(11-1)*SBLIMIT] = out1[11-1] + REAL_MUL(tmp1, wi[5-1]);
1606
ts[(17-0)*SBLIMIT] = out1[17-0] + REAL_MUL(in2, wi[11-0]);
1607
ts[(12+0)*SBLIMIT] = out1[12+0] + REAL_MUL(in2, wi[6+0]);
1608
ts[(12+2)*SBLIMIT] = out1[12+2] + REAL_MUL(in3, wi[6+2]);
1609
ts[(17-2)*SBLIMIT] = out1[17-2] + REAL_MUL(in3, wi[11-2]);
1611
ts[(6 +0)*SBLIMIT] = out1[6+0] + REAL_MUL(in0, wi[0]);
1612
ts[(11-0)*SBLIMIT] = out1[11-0] + REAL_MUL(in0, wi[5-0]);
1613
ts[(6 +2)*SBLIMIT] = out1[6+2] + REAL_MUL(in4, wi[2]);
1614
ts[(11-2)*SBLIMIT] = out1[11-2] + REAL_MUL(in4, wi[5-2]);
1620
real in0,in1,in2,in3,in4,in5;
1621
register real *out2 = rawout2;
1626
real tmp0,tmp1 = (in0 - in4);
1628
real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1632
out2[5-1] = REAL_MUL(tmp0, wi[11-1]);
1633
out2[0+1] = REAL_MUL(tmp0, wi[6+1]);
1634
ts[(12+1)*SBLIMIT] += REAL_MUL(tmp1, wi[1]);
1635
ts[(17-1)*SBLIMIT] += REAL_MUL(tmp1, wi[5-1]);
1640
out2[5-0] = REAL_MUL(in2, wi[11-0]);
1641
out2[0+0] = REAL_MUL(in2, wi[6+0]);
1642
out2[0+2] = REAL_MUL(in3, wi[6+2]);
1643
out2[5-2] = REAL_MUL(in3, wi[11-2]);
1645
ts[(12+0)*SBLIMIT] += REAL_MUL(in0, wi[0]);
1646
ts[(17-0)*SBLIMIT] += REAL_MUL(in0, wi[5-0]);
1647
ts[(12+2)*SBLIMIT] += REAL_MUL(in4, wi[2]);
1648
ts[(17-2)*SBLIMIT] += REAL_MUL(in4, wi[5-2]);
1654
real in0,in1,in2,in3,in4,in5;
1655
register real *out2 = rawout2;
1656
out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1661
real tmp0,tmp1 = (in0 - in4);
1663
real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1667
out2[11-1] = REAL_MUL(tmp0, wi[11-1]);
1668
out2[6 +1] = REAL_MUL(tmp0, wi[6+1]);
1669
out2[0+1] += REAL_MUL(tmp1, wi[1]);
1670
out2[5-1] += REAL_MUL(tmp1, wi[5-1]);
1675
out2[11-0] = REAL_MUL(in2, wi[11-0]);
1676
out2[6 +0] = REAL_MUL(in2, wi[6+0]);
1677
out2[6 +2] = REAL_MUL(in3, wi[6+2]);
1678
out2[11-2] = REAL_MUL(in3, wi[11-2]);
1680
out2[0+0] += REAL_MUL(in0, wi[0]);
1681
out2[5-0] += REAL_MUL(in0, wi[5-0]);
1682
out2[0+2] += REAL_MUL(in4, wi[2]);
1683
out2[5-2] += REAL_MUL(in4, wi[5-2]);
1691
static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],int ch,struct gr_info_s *gr_info,struct frame *fr)
1693
static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1694
int ch,struct gr_info_s *gr_info)
1697
static real block[2][2][SBLIMIT*SSLIMIT] = { { { 0, } } };
1698
static int blc[2]={0,0};
1700
real *tspnt = (real *) tsOut;
1701
real *rawout1,*rawout2;
1706
rawout1=block[b][ch];
1708
rawout2=block[b][ch];
1712
if(gr_info->mixed_block_flag) {
1715
(fr->dct36)(fsIn[0],rawout1,rawout2,win[0],tspnt);
1716
(fr->dct36)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1718
dct36(fsIn[0],rawout1,rawout2,win[0],tspnt);
1719
dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1721
rawout1 += 36; rawout2 += 36; tspnt += 2;
1724
bt = gr_info->block_type;
1726
for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1727
dct12(fsIn[sb] ,rawout1 ,rawout2 ,win[2] ,tspnt);
1728
dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1732
for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1734
(fr->dct36)(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1735
(fr->dct36)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1737
dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1738
dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1743
for(;sb<SBLIMIT;sb++,tspnt++) {
1745
for(i=0;i<SSLIMIT;i++) {
1746
tspnt[i*SBLIMIT] = *rawout1++;
1747
*rawout2++ = DOUBLE_TO_REAL(0.0);
1754
* main layer3 handler
1756
int do_layer3(struct frame *fr,int outmode,struct audio_info_struct *ai)
1758
int gr, ch, ss,clip=0;
1759
int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1760
struct III_sideinfo sideinfo;
1761
int stereo = fr->stereo;
1762
int single = fr->single;
1763
int ms_stereo,i_stereo;
1764
int sfreq = fr->sampling_frequency;
1765
int stereo1,granules;
1767
if(stereo == 1) { /* stream is mono */
1771
else if(single >= 0) /* stream is stereo, but force to mono */
1776
if(fr->mode == MPG_MD_JOINT_STEREO) {
1777
ms_stereo = (fr->mode_ext & 0x2)>>1;
1778
i_stereo = fr->mode_ext & 0x1;
1781
ms_stereo = i_stereo = 0;
1786
III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single);
1792
/* quick hack to keep the music playing */
1793
/* after having seen this nasty test file... */
1794
if(III_get_side_info(&sideinfo,stereo,ms_stereo,sfreq,single,fr->lsf))
1796
error("bad frame - unable to get valid sideinfo");
1800
set_pointer(sideinfo.main_data_begin);
1802
for (gr=0;gr<granules;gr++) {
1803
real hybridIn [2][SBLIMIT][SSLIMIT];
1804
real hybridOut[2][SSLIMIT][SBLIMIT];
1807
struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
1810
part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0);
1812
part2bits = III_get_scale_factors_1(scalefacs[0],gr_info,0,gr);
1814
if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
1819
struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
1822
part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo);
1824
part2bits = III_get_scale_factors_1(scalefacs[1],gr_info,1,gr);
1826
if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
1831
int maxb = sideinfo.ch[0].gr[gr].maxb;
1832
if(sideinfo.ch[1].gr[gr].maxb > maxb)
1833
maxb = sideinfo.ch[1].gr[gr].maxb;
1834
for(i=0;i<SSLIMIT*maxb;i++) {
1835
real tmp0 = ((real *)hybridIn[0])[i];
1836
real tmp1 = ((real *)hybridIn[1])[i];
1837
((real *)hybridIn[0])[i] = tmp0 + tmp1;
1838
((real *)hybridIn[1])[i] = tmp0 - tmp1;
1843
III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
1845
if(ms_stereo || i_stereo || (single == 3) ) {
1846
if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
1847
sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
1849
gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
1856
register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1857
for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++)
1858
*in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1864
register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1865
for(i=0;i<SSLIMIT*gr_info->maxb;i++)
1872
for(ch=0;ch<stereo1;ch++) {
1873
struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
1874
III_antialias(hybridIn[ch],gr_info);
1876
III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info,fr);
1878
III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info);
1883
if (fr->synth != synth_1to1 || single >= 0) {
1885
for(ss=0;ss<SSLIMIT;ss++) {
1887
clip += (fr->synth_mono)(hybridOut[0][ss],pcm_sample,&pcm_point);
1891
clip += (fr->synth)(hybridOut[0][ss],0,pcm_sample,&p1);
1892
clip += (fr->synth)(hybridOut[1][ss],1,pcm_sample,&pcm_point);
1895
#ifdef VARMODESUPPORT
1896
if (playlimit < 128) {
1897
pcm_point -= playlimit >> 1;
1903
if(pcm_point >= audiobufsize) audio_flush(outmode,ai);
1907
/* Only stereo, 16 bits benefit from the 486 optimization. */
1909
while (ss < SSLIMIT) {
1911
n=(audiobufsize - pcm_point) / (2*2*32);
1912
if (n > (SSLIMIT-ss)) n=SSLIMIT-ss;
1914
synth_1to1_486(hybridOut[0][ss],0,pcm_sample+pcm_point,n);
1915
synth_1to1_486(hybridOut[1][ss],1,pcm_sample+pcm_point,n);
1917
pcm_point+=(2*2*32)*n;
1919
if(pcm_point >= audiobufsize) audio_flush(outmode,ai);