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