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