comparison decoders/mpglib/layer3.c @ 261:9b6e82f7c853

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