comparison decoders/libmpg123/layer3.c @ 562:7e08477b0fc1

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