crypt32: Don't use off_t for memory offsets.
[wine/multimedia.git] / dlls / winemp3.acm / layer3.c
blob129d82151ac504627cfa1e676e9700118a180cf7
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 * This file has been copied from mpglib.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdlib.h>
24 #include "mpg123.h"
25 #include "mpglib.h"
26 #include "huffman.h"
28 #define MPEG1
31 static real ispow[8207];
32 static real aa_ca[8],aa_cs[8];
33 static real COS1[12][6];
34 static real win[4][36];
35 static real win1[4][36];
36 static real gainpow2[256+118+4];
37 static real COS9[9];
38 static real COS6_1,COS6_2;
39 static real tfcos36[9];
40 static real tfcos12[3];
42 struct bandInfoStruct {
43 short longIdx[23];
44 short longDiff[22];
45 short shortIdx[14];
46 short shortDiff[13];
49 static int longLimit[9][23];
50 static int shortLimit[9][14];
52 static const struct bandInfoStruct bandInfo[9] = {
54 /* MPEG 1.0 */
55 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
56 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
57 {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},
58 {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
60 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
61 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
62 {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},
63 {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
65 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
66 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
67 {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} ,
68 {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
70 /* MPEG 2.0 */
71 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
72 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
73 {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} ,
74 {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
76 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
77 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
78 {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} ,
79 {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
81 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
82 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
83 {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},
84 {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
85 /* MPEG 2.5 */
86 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
87 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
88 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
89 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
90 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
91 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
92 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
93 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
94 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
95 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
96 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
97 {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
100 static int mapbuf0[9][152];
101 static int mapbuf1[9][156];
102 static int mapbuf2[9][44];
103 static int *map[9][3];
104 static int *mapend[9][3];
106 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
107 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
109 static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
110 static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
113 * init tables for layer-3
115 void init_layer3(int down_sample_sblimit)
117 int i,j,k,l;
119 for(i=-256;i<118+4;i++)
120 gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
122 for(i=0;i<8207;i++)
123 ispow[i] = pow((double)i,(double)4.0/3.0);
125 for (i=0;i<8;i++)
127 static const double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
128 double sq=sqrt(1.0+Ci[i]*Ci[i]);
129 aa_cs[i] = 1.0/sq;
130 aa_ca[i] = Ci[i]/sq;
133 for(i=0;i<18;i++)
135 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 );
136 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 );
138 for(i=0;i<6;i++)
140 win[1][i+18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
141 win[3][i+12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 );
142 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 );
143 win[1][i+30] = win[3][i] = 0.0;
144 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 );
147 for(i=0;i<9;i++)
148 COS9[i] = cos( M_PI / 18.0 * (double) i);
150 for(i=0;i<9;i++)
151 tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 );
152 for(i=0;i<3;i++)
153 tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 );
155 COS6_1 = cos( M_PI / 6.0 * (double) 1);
156 COS6_2 = cos( M_PI / 6.0 * (double) 2);
158 for(i=0;i<12;i++)
160 win[2][i] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 );
161 for(j=0;j<6;j++)
162 COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) );
165 for(j=0;j<4;j++) {
166 static const int len[4] = { 36,36,12,36 };
167 for(i=0;i<len[j];i+=2)
168 win1[j][i] = + win[j][i];
169 for(i=1;i<len[j];i+=2)
170 win1[j][i] = - win[j][i];
173 for(i=0;i<16;i++)
175 double t = tan( (double) i * M_PI / 12.0 );
176 tan1_1[i] = t / (1.0+t);
177 tan2_1[i] = 1.0 / (1.0 + t);
178 tan1_2[i] = M_SQRT2 * t / (1.0+t);
179 tan2_2[i] = M_SQRT2 / (1.0 + t);
181 for(j=0;j<2;j++) {
182 double base = pow(2.0,-0.25*(j+1.0));
183 double p1=1.0,p2=1.0;
184 if(i > 0) {
185 if( i & 1 )
186 p1 = pow(base,(i+1.0)*0.5);
187 else
188 p2 = pow(base,i*0.5);
190 pow1_1[j][i] = p1;
191 pow2_1[j][i] = p2;
192 pow1_2[j][i] = M_SQRT2 * p1;
193 pow2_2[j][i] = M_SQRT2 * p2;
197 for(j=0;j<9;j++)
199 const struct bandInfoStruct *bi = &bandInfo[j];
200 int *mp;
201 int cb,lwin;
202 const short *bdf;
204 mp = map[j][0] = mapbuf0[j];
205 bdf = bi->longDiff;
206 for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
207 *mp++ = (*bdf) >> 1;
208 *mp++ = i;
209 *mp++ = 3;
210 *mp++ = cb;
212 bdf = bi->shortDiff+3;
213 for(cb=3;cb<13;cb++) {
214 int l = (*bdf++) >> 1;
215 for(lwin=0;lwin<3;lwin++) {
216 *mp++ = l;
217 *mp++ = i + lwin;
218 *mp++ = lwin;
219 *mp++ = cb;
221 i += 6*l;
223 mapend[j][0] = mp;
225 mp = map[j][1] = mapbuf1[j];
226 bdf = bi->shortDiff+0;
227 for(i=0,cb=0;cb<13;cb++) {
228 int l = (*bdf++) >> 1;
229 for(lwin=0;lwin<3;lwin++) {
230 *mp++ = l;
231 *mp++ = i + lwin;
232 *mp++ = lwin;
233 *mp++ = cb;
235 i += 6*l;
237 mapend[j][1] = mp;
239 mp = map[j][2] = mapbuf2[j];
240 bdf = bi->longDiff;
241 for(cb = 0; cb < 22 ; cb++) {
242 *mp++ = (*bdf++) >> 1;
243 *mp++ = cb;
245 mapend[j][2] = mp;
249 for(j=0;j<9;j++) {
250 for(i=0;i<23;i++) {
251 longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
252 if(longLimit[j][i] > (down_sample_sblimit) )
253 longLimit[j][i] = down_sample_sblimit;
255 for(i=0;i<14;i++) {
256 shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
257 if(shortLimit[j][i] > (down_sample_sblimit) )
258 shortLimit[j][i] = down_sample_sblimit;
262 for(i=0;i<5;i++) {
263 for(j=0;j<6;j++) {
264 for(k=0;k<6;k++) {
265 int n = k + j * 6 + i * 36;
266 i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
270 for(i=0;i<4;i++) {
271 for(j=0;j<4;j++) {
272 for(k=0;k<4;k++) {
273 int n = k + j * 4 + i * 16;
274 i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
278 for(i=0;i<4;i++) {
279 for(j=0;j<3;j++) {
280 int n = j + i * 3;
281 i_slen2[n+244] = i|(j<<3) | (5<<12);
282 n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
286 for(i=0;i<5;i++) {
287 for(j=0;j<5;j++) {
288 for(k=0;k<4;k++) {
289 for(l=0;l<4;l++) {
290 int n = l + k * 4 + j * 16 + i * 80;
291 n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
296 for(i=0;i<5;i++) {
297 for(j=0;j<5;j++) {
298 for(k=0;k<4;k++) {
299 int n = k + j * 4 + i * 20;
300 n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
307 * read additional side information
309 #ifdef MPEG1
310 static int III_get_side_info_1(struct III_sideinfo *si,int stereo,
311 int ms_stereo,long sfreq,int single)
313 int ch, gr;
314 int powdiff = (single == 3) ? 4 : 0;
316 si->main_data_begin = getbits(9);
317 if (stereo == 1)
318 si->private_bits = getbits_fast(5);
319 else
320 si->private_bits = getbits_fast(3);
322 for (ch=0; ch<stereo; ch++) {
323 si->ch[ch].gr[0].scfsi = -1;
324 si->ch[ch].gr[1].scfsi = getbits_fast(4);
327 for (gr=0; gr<2; gr++)
329 for (ch=0; ch<stereo; ch++)
331 register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
333 gr_info->part2_3_length = getbits(12);
334 gr_info->big_values = getbits_fast(9);
335 if(gr_info->big_values > 288) {
336 fprintf(stderr,"big_values too large!\n");
337 gr_info->big_values = 288;
339 gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
340 if(ms_stereo)
341 gr_info->pow2gain += 2;
342 gr_info->scalefac_compress = getbits_fast(4);
343 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
344 if(get1bit())
346 int i;
347 gr_info->block_type = getbits_fast(2);
348 gr_info->mixed_block_flag = get1bit();
349 gr_info->table_select[0] = getbits_fast(5);
350 gr_info->table_select[1] = getbits_fast(5);
352 * table_select[2] not needed, because there is no region2,
353 * but to satisfy some verifications tools we set it either.
355 gr_info->table_select[2] = 0;
356 for(i=0;i<3;i++)
357 gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
359 if(gr_info->block_type == 0) {
360 fprintf(stderr,"Blocktype == 0 and window-switching == 1 not allowed.\n");
361 return 0;
363 /* region_count/start parameters are implicit in this case. */
364 gr_info->region1start = 36>>1;
365 gr_info->region2start = 576>>1;
367 else
369 int i,r0c,r1c;
370 for (i=0; i<3; i++)
371 gr_info->table_select[i] = getbits_fast(5);
372 r0c = getbits_fast(4);
373 r1c = getbits_fast(3);
374 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
375 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
376 gr_info->block_type = 0;
377 gr_info->mixed_block_flag = 0;
379 gr_info->preflag = get1bit();
380 gr_info->scalefac_scale = get1bit();
381 gr_info->count1table_select = get1bit();
384 return !0;
386 #endif
389 * Side Info for MPEG 2.0 / LSF
391 static int III_get_side_info_2(struct III_sideinfo *si,int stereo,
392 int ms_stereo,long sfreq,int single)
394 int ch;
395 int powdiff = (single == 3) ? 4 : 0;
397 si->main_data_begin = getbits(8);
398 if (stereo == 1)
399 si->private_bits = get1bit();
400 else
401 si->private_bits = getbits_fast(2);
403 for (ch=0; ch<stereo; ch++)
405 register struct gr_info_s *gr_info = &(si->ch[ch].gr[0]);
407 gr_info->part2_3_length = getbits(12);
408 gr_info->big_values = getbits_fast(9);
409 if(gr_info->big_values > 288) {
410 fprintf(stderr,"big_values too large!\n");
411 gr_info->big_values = 288;
413 gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
414 if(ms_stereo)
415 gr_info->pow2gain += 2;
416 gr_info->scalefac_compress = getbits(9);
417 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
418 if(get1bit())
420 int i;
421 gr_info->block_type = getbits_fast(2);
422 gr_info->mixed_block_flag = get1bit();
423 gr_info->table_select[0] = getbits_fast(5);
424 gr_info->table_select[1] = getbits_fast(5);
426 * table_select[2] not needed, because there is no region2,
427 * but to satisfy some verifications tools we set it either.
429 gr_info->table_select[2] = 0;
430 for(i=0;i<3;i++)
431 gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
433 if(gr_info->block_type == 0) {
434 fprintf(stderr,"Blocktype == 0 and window-switching == 1 not allowed.\n");
435 return 0;
437 /* region_count/start parameters are implicit in this case. */
438 /* check this again! */
439 if(gr_info->block_type == 2)
440 gr_info->region1start = 36>>1;
441 else if(sfreq == 8)
442 /* check this for 2.5 and sfreq=8 */
443 gr_info->region1start = 108>>1;
444 else
445 gr_info->region1start = 54>>1;
446 gr_info->region2start = 576>>1;
448 else
450 int i,r0c,r1c;
451 for (i=0; i<3; i++)
452 gr_info->table_select[i] = getbits_fast(5);
453 r0c = getbits_fast(4);
454 r1c = getbits_fast(3);
455 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
456 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
457 gr_info->block_type = 0;
458 gr_info->mixed_block_flag = 0;
460 gr_info->scalefac_scale = get1bit();
461 gr_info->count1table_select = get1bit();
463 return !0;
467 * read scalefactors
469 #ifdef MPEG1
470 static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
472 static const unsigned char slen[2][16] = {
473 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
474 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
476 int numbits;
477 int num0 = slen[0][gr_info->scalefac_compress];
478 int num1 = slen[1][gr_info->scalefac_compress];
480 if (gr_info->block_type == 2) {
481 int i=18;
482 numbits = (num0 + num1) * 18;
484 if (gr_info->mixed_block_flag) {
485 for (i=8;i;i--)
486 *scf++ = getbits_fast(num0);
487 i = 9;
488 numbits -= num0; /* num0 * 17 + num1 * 18 */
491 for (;i;i--)
492 *scf++ = getbits_fast(num0);
493 for (i = 18; i; i--)
494 *scf++ = getbits_fast(num1);
495 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
497 else {
498 int i;
499 int scfsi = gr_info->scfsi;
501 if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
502 for(i=11;i;i--)
503 *scf++ = getbits_fast(num0);
504 for(i=10;i;i--)
505 *scf++ = getbits_fast(num1);
506 numbits = (num0 + num1) * 10 + num0;
507 *scf++ = 0;
509 else {
510 numbits = 0;
511 if(!(scfsi & 0x8)) {
512 for (i=0;i<6;i++)
513 *scf++ = getbits_fast(num0);
514 numbits += num0 * 6;
516 else {
517 scf += 6;
520 if(!(scfsi & 0x4)) {
521 for (i=0;i<5;i++)
522 *scf++ = getbits_fast(num0);
523 numbits += num0 * 5;
525 else {
526 scf += 5;
529 if(!(scfsi & 0x2)) {
530 for(i=0;i<5;i++)
531 *scf++ = getbits_fast(num1);
532 numbits += num1 * 5;
534 else {
535 scf += 5;
538 if(!(scfsi & 0x1)) {
539 for (i=0;i<5;i++)
540 *scf++ = getbits_fast(num1);
541 numbits += num1 * 5;
543 else {
544 scf += 5;
546 *scf++ = 0; /* no l[21] in original sources */
549 return numbits;
551 #endif
554 static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
556 const unsigned char *pnt;
557 int i,j;
558 unsigned int slen;
559 int n = 0;
560 int numbits = 0;
562 static const unsigned char stab[3][6][4] = {
563 { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
564 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
565 { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
566 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
567 { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
568 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
570 if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
571 slen = i_slen2[gr_info->scalefac_compress>>1];
572 else
573 slen = n_slen2[gr_info->scalefac_compress];
575 gr_info->preflag = (slen>>15) & 0x1;
577 n = 0;
578 if( gr_info->block_type == 2 ) {
579 n++;
580 if(gr_info->mixed_block_flag)
581 n++;
584 pnt = stab[n][(slen>>12)&0x7];
586 for(i=0;i<4;i++) {
587 int num = slen & 0x7;
588 slen >>= 3;
589 if(num) {
590 for(j=0;j<(int)(pnt[i]);j++)
591 *scf++ = getbits_fast(num);
592 numbits += pnt[i] * num;
594 else {
595 for(j=0;j<(int)(pnt[i]);j++)
596 *scf++ = 0;
600 n = (n << 1) + 1;
601 for(i=0;i<n;i++)
602 *scf++ = 0;
604 return numbits;
607 static const int pretab1[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
608 static const int pretab2[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
611 * don't forget to apply the same changes to III_dequantize_sample_ms() !!!
613 static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
614 struct gr_info_s *gr_info,int sfreq,int part2bits)
616 int shift = 1 + gr_info->scalefac_scale;
617 real *xrpnt = (real *) xr;
618 int l[3],l3;
619 int part2remain = gr_info->part2_3_length - part2bits;
620 int *me;
623 int bv = gr_info->big_values;
624 int region1 = gr_info->region1start;
625 int region2 = gr_info->region2start;
627 l3 = ((576>>1)-bv)>>1;
629 * we may lose the 'odd' bit here !!
630 * check this later again
632 if(bv <= region1) {
633 l[0] = bv; l[1] = 0; l[2] = 0;
635 else {
636 l[0] = region1;
637 if(bv <= region2) {
638 l[1] = bv - l[0]; l[2] = 0;
640 else {
641 l[1] = region2 - l[0]; l[2] = bv - region2;
646 if(gr_info->block_type == 2) {
648 * decoding with short or mixed mode BandIndex table
650 int i,max[4];
651 int step=0,lwin=0,cb=0;
652 register real v = 0.0;
653 register int *m,mc;
655 if(gr_info->mixed_block_flag) {
656 max[3] = -1;
657 max[0] = max[1] = max[2] = 2;
658 m = map[sfreq][0];
659 me = mapend[sfreq][0];
661 else {
662 max[0] = max[1] = max[2] = max[3] = -1;
663 /* max[3] not really needed in this case */
664 m = map[sfreq][1];
665 me = mapend[sfreq][1];
668 mc = 0;
669 for(i=0;i<2;i++) {
670 int lp = l[i];
671 const struct newhuff *h = ht+gr_info->table_select[i];
672 for(;lp;lp--,mc--) {
673 register int x,y;
674 if( (!mc) ) {
675 mc = *m++;
676 xrpnt = ((real *) xr) + (*m++);
677 lwin = *m++;
678 cb = *m++;
679 if(lwin == 3) {
680 v = gr_info->pow2gain[(*scf++) << shift];
681 step = 1;
683 else {
684 v = gr_info->full_gain[lwin][(*scf++) << shift];
685 step = 3;
689 register const short *val = h->table;
690 while((y=*val++)<0) {
691 if (get1bit())
692 val -= y;
693 part2remain--;
695 x = y >> 4;
696 y &= 0xf;
698 if(x == 15) {
699 max[lwin] = cb;
700 part2remain -= h->linbits+1;
701 x += getbits(h->linbits);
702 if(get1bit())
703 *xrpnt = -ispow[x] * v;
704 else
705 *xrpnt = ispow[x] * v;
707 else if(x) {
708 max[lwin] = cb;
709 if(get1bit())
710 *xrpnt = -ispow[x] * v;
711 else
712 *xrpnt = ispow[x] * v;
713 part2remain--;
715 else
716 *xrpnt = 0.0;
717 xrpnt += step;
718 if(y == 15) {
719 max[lwin] = cb;
720 part2remain -= h->linbits+1;
721 y += getbits(h->linbits);
722 if(get1bit())
723 *xrpnt = -ispow[y] * v;
724 else
725 *xrpnt = ispow[y] * v;
727 else if(y) {
728 max[lwin] = cb;
729 if(get1bit())
730 *xrpnt = -ispow[y] * v;
731 else
732 *xrpnt = ispow[y] * v;
733 part2remain--;
735 else
736 *xrpnt = 0.0;
737 xrpnt += step;
740 for(;l3 && (part2remain > 0);l3--) {
741 const struct newhuff *h = htc+gr_info->count1table_select;
742 const short *val = h->table;
743 short a;
745 while((a=*val++)<0) {
746 part2remain--;
747 if(part2remain < 0) {
748 part2remain++;
749 a = 0;
750 break;
752 if (get1bit())
753 val -= a;
756 for(i=0;i<4;i++) {
757 if(!(i & 1)) {
758 if(!mc) {
759 mc = *m++;
760 xrpnt = ((real *) xr) + (*m++);
761 lwin = *m++;
762 cb = *m++;
763 if(lwin == 3) {
764 v = gr_info->pow2gain[(*scf++) << shift];
765 step = 1;
767 else {
768 v = gr_info->full_gain[lwin][(*scf++) << shift];
769 step = 3;
772 mc--;
774 if( (a & (0x8>>i)) ) {
775 max[lwin] = cb;
776 part2remain--;
777 if(part2remain < 0) {
778 part2remain++;
779 break;
781 if(get1bit())
782 *xrpnt = -v;
783 else
784 *xrpnt = v;
786 else
787 *xrpnt = 0.0;
788 xrpnt += step;
792 while( m < me ) {
793 if(!mc) {
794 mc = *m++;
795 xrpnt = ((real *) xr) + *m++;
796 if( (*m++) == 3)
797 step = 1;
798 else
799 step = 3;
800 m++; /* cb */
802 mc--;
803 *xrpnt = 0.0;
804 xrpnt += step;
805 *xrpnt = 0.0;
806 xrpnt += step;
807 /* we could add a little opt. here:
808 * if we finished a band for window 3 or a long band
809 * further bands could copied in a simple loop without a
810 * special 'map' decoding
814 gr_info->maxband[0] = max[0]+1;
815 gr_info->maxband[1] = max[1]+1;
816 gr_info->maxband[2] = max[2]+1;
817 gr_info->maxbandl = max[3]+1;
820 int rmax = max[0] > max[1] ? max[0] : max[1];
821 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
822 gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
826 else {
828 * decoding with 'long' BandIndex table (block_type != 2)
830 const int *pretab = gr_info->preflag ? pretab1 : pretab2;
831 int i,max = -1;
832 int cb = 0;
833 register int *m = map[sfreq][2];
834 register real v = 0.0;
835 register int mc = 0;
836 #if 0
837 me = mapend[sfreq][2];
838 #endif
841 * long hash table values
843 for(i=0;i<3;i++) {
844 int lp = l[i];
845 const struct newhuff *h = ht+gr_info->table_select[i];
847 for(;lp;lp--,mc--) {
848 int x,y;
850 if(!mc) {
851 mc = *m++;
852 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
853 cb = *m++;
856 register const short *val = h->table;
857 while((y=*val++)<0) {
858 if (get1bit())
859 val -= y;
860 part2remain--;
862 x = y >> 4;
863 y &= 0xf;
865 if (x == 15) {
866 max = cb;
867 part2remain -= h->linbits+1;
868 x += getbits(h->linbits);
869 if(get1bit())
870 *xrpnt++ = -ispow[x] * v;
871 else
872 *xrpnt++ = ispow[x] * v;
874 else if(x) {
875 max = cb;
876 if(get1bit())
877 *xrpnt++ = -ispow[x] * v;
878 else
879 *xrpnt++ = ispow[x] * v;
880 part2remain--;
882 else
883 *xrpnt++ = 0.0;
885 if (y == 15) {
886 max = cb;
887 part2remain -= h->linbits+1;
888 y += getbits(h->linbits);
889 if(get1bit())
890 *xrpnt++ = -ispow[y] * v;
891 else
892 *xrpnt++ = ispow[y] * v;
894 else if(y) {
895 max = cb;
896 if(get1bit())
897 *xrpnt++ = -ispow[y] * v;
898 else
899 *xrpnt++ = ispow[y] * v;
900 part2remain--;
902 else
903 *xrpnt++ = 0.0;
908 * short (count1table) values
910 for(;l3 && (part2remain > 0);l3--) {
911 const struct newhuff *h = htc+gr_info->count1table_select;
912 const short *val = h->table;
913 short a;
915 while((a=*val++)<0) {
916 part2remain--;
917 if(part2remain < 0) {
918 part2remain++;
919 a = 0;
920 break;
922 if (get1bit())
923 val -= a;
926 for(i=0;i<4;i++) {
927 if(!(i & 1)) {
928 if(!mc) {
929 mc = *m++;
930 cb = *m++;
931 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
933 mc--;
935 if ( (a & (0x8>>i)) ) {
936 max = cb;
937 part2remain--;
938 if(part2remain < 0) {
939 part2remain++;
940 break;
942 if(get1bit())
943 *xrpnt++ = -v;
944 else
945 *xrpnt++ = v;
947 else
948 *xrpnt++ = 0.0;
953 * zero part
955 for(i=(&xr[SBLIMIT][0]-xrpnt)>>1;i;i--) {
956 *xrpnt++ = 0.0;
957 *xrpnt++ = 0.0;
960 gr_info->maxbandl = max+1;
961 gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
964 while( part2remain > 16 ) {
965 getbits(16); /* Dismiss stuffing Bits */
966 part2remain -= 16;
968 if(part2remain > 0)
969 getbits(part2remain);
970 else if(part2remain < 0) {
971 fprintf(stderr,"mpg123: Can't rewind stream by %d bits!\n",-part2remain);
972 return 1; /* -> error */
974 return 0;
977 #if 0
978 static int III_dequantize_sample_ms(real xr[2][SBLIMIT][SSLIMIT],int *scf,
979 struct gr_info_s *gr_info,int sfreq,int part2bits)
981 int shift = 1 + gr_info->scalefac_scale;
982 real *xrpnt = (real *) xr[1];
983 real *xr0pnt = (real *) xr[0];
984 int l[3],l3;
985 int part2remain = gr_info->part2_3_length - part2bits;
986 int *me;
989 int bv = gr_info->big_values;
990 int region1 = gr_info->region1start;
991 int region2 = gr_info->region2start;
993 l3 = ((576>>1)-bv)>>1;
995 * we may lose the 'odd' bit here !!
996 * check this later gain
998 if(bv <= region1) {
999 l[0] = bv; l[1] = 0; l[2] = 0;
1001 else {
1002 l[0] = region1;
1003 if(bv <= region2) {
1004 l[1] = bv - l[0]; l[2] = 0;
1006 else {
1007 l[1] = region2 - l[0]; l[2] = bv - region2;
1012 if(gr_info->block_type == 2) {
1013 int i,max[4];
1014 int step=0,lwin=0,cb=0;
1015 register real v = 0.0;
1016 register int *m,mc = 0;
1018 if(gr_info->mixed_block_flag) {
1019 max[3] = -1;
1020 max[0] = max[1] = max[2] = 2;
1021 m = map[sfreq][0];
1022 me = mapend[sfreq][0];
1024 else {
1025 max[0] = max[1] = max[2] = max[3] = -1;
1026 /* max[3] not really needed in this case */
1027 m = map[sfreq][1];
1028 me = mapend[sfreq][1];
1031 for(i=0;i<2;i++) {
1032 int lp = l[i];
1033 struct newhuff *h = ht+gr_info->table_select[i];
1034 for(;lp;lp--,mc--) {
1035 int x,y;
1037 if(!mc) {
1038 mc = *m++;
1039 xrpnt = ((real *) xr[1]) + *m;
1040 xr0pnt = ((real *) xr[0]) + *m++;
1041 lwin = *m++;
1042 cb = *m++;
1043 if(lwin == 3) {
1044 v = gr_info->pow2gain[(*scf++) << shift];
1045 step = 1;
1047 else {
1048 v = gr_info->full_gain[lwin][(*scf++) << shift];
1049 step = 3;
1053 register short *val = h->table;
1054 while((y=*val++)<0) {
1055 if (get1bit())
1056 val -= y;
1057 part2remain--;
1059 x = y >> 4;
1060 y &= 0xf;
1062 if(x == 15) {
1063 max[lwin] = cb;
1064 part2remain -= h->linbits+1;
1065 x += getbits(h->linbits);
1066 if(get1bit()) {
1067 real a = ispow[x] * v;
1068 *xrpnt = *xr0pnt + a;
1069 *xr0pnt -= a;
1071 else {
1072 real a = ispow[x] * v;
1073 *xrpnt = *xr0pnt - a;
1074 *xr0pnt += a;
1077 else if(x) {
1078 max[lwin] = cb;
1079 if(get1bit()) {
1080 real a = ispow[x] * v;
1081 *xrpnt = *xr0pnt + a;
1082 *xr0pnt -= a;
1084 else {
1085 real a = ispow[x] * v;
1086 *xrpnt = *xr0pnt - a;
1087 *xr0pnt += a;
1089 part2remain--;
1091 else
1092 *xrpnt = *xr0pnt;
1093 xrpnt += step;
1094 xr0pnt += step;
1096 if(y == 15) {
1097 max[lwin] = cb;
1098 part2remain -= h->linbits+1;
1099 y += getbits(h->linbits);
1100 if(get1bit()) {
1101 real a = ispow[y] * v;
1102 *xrpnt = *xr0pnt + a;
1103 *xr0pnt -= a;
1105 else {
1106 real a = ispow[y] * v;
1107 *xrpnt = *xr0pnt - a;
1108 *xr0pnt += a;
1111 else if(y) {
1112 max[lwin] = cb;
1113 if(get1bit()) {
1114 real a = ispow[y] * v;
1115 *xrpnt = *xr0pnt + a;
1116 *xr0pnt -= a;
1118 else {
1119 real a = ispow[y] * v;
1120 *xrpnt = *xr0pnt - a;
1121 *xr0pnt += a;
1123 part2remain--;
1125 else
1126 *xrpnt = *xr0pnt;
1127 xrpnt += step;
1128 xr0pnt += step;
1132 for(;l3 && (part2remain > 0);l3--) {
1133 struct newhuff *h = htc+gr_info->count1table_select;
1134 register short *val = h->table,a;
1136 while((a=*val++)<0) {
1137 part2remain--;
1138 if(part2remain < 0) {
1139 part2remain++;
1140 a = 0;
1141 break;
1143 if (get1bit())
1144 val -= a;
1147 for(i=0;i<4;i++) {
1148 if(!(i & 1)) {
1149 if(!mc) {
1150 mc = *m++;
1151 xrpnt = ((real *) xr[1]) + *m;
1152 xr0pnt = ((real *) xr[0]) + *m++;
1153 lwin = *m++;
1154 cb = *m++;
1155 if(lwin == 3) {
1156 v = gr_info->pow2gain[(*scf++) << shift];
1157 step = 1;
1159 else {
1160 v = gr_info->full_gain[lwin][(*scf++) << shift];
1161 step = 3;
1164 mc--;
1166 if( (a & (0x8>>i)) ) {
1167 max[lwin] = cb;
1168 part2remain--;
1169 if(part2remain < 0) {
1170 part2remain++;
1171 break;
1173 if(get1bit()) {
1174 *xrpnt = *xr0pnt + v;
1175 *xr0pnt -= v;
1177 else {
1178 *xrpnt = *xr0pnt - v;
1179 *xr0pnt += v;
1182 else
1183 *xrpnt = *xr0pnt;
1184 xrpnt += step;
1185 xr0pnt += step;
1189 while( m < me ) {
1190 if(!mc) {
1191 mc = *m++;
1192 xrpnt = ((real *) xr[1]) + *m;
1193 xr0pnt = ((real *) xr[0]) + *m++;
1194 if(*m++ == 3)
1195 step = 1;
1196 else
1197 step = 3;
1198 m++; /* cb */
1200 mc--;
1201 *xrpnt = *xr0pnt;
1202 xrpnt += step;
1203 xr0pnt += step;
1204 *xrpnt = *xr0pnt;
1205 xrpnt += step;
1206 xr0pnt += step;
1207 /* we could add a little opt. here:
1208 * if we finished a band for window 3 or a long band
1209 * further bands could copied in a simple loop without a
1210 * special 'map' decoding
1214 gr_info->maxband[0] = max[0]+1;
1215 gr_info->maxband[1] = max[1]+1;
1216 gr_info->maxband[2] = max[2]+1;
1217 gr_info->maxbandl = max[3]+1;
1220 int rmax = max[0] > max[1] ? max[0] : max[1];
1221 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
1222 gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
1225 else {
1226 int *pretab = gr_info->preflag ? pretab1 : pretab2;
1227 int i,max = -1;
1228 int cb = 0;
1229 register int mc=0,*m = map[sfreq][2];
1230 register real v = 0.0;
1231 #if 0
1232 me = mapend[sfreq][2];
1233 #endif
1235 for(i=0;i<3;i++) {
1236 int lp = l[i];
1237 struct newhuff *h = ht+gr_info->table_select[i];
1239 for(;lp;lp--,mc--) {
1240 int x,y;
1241 if(!mc) {
1242 mc = *m++;
1243 cb = *m++;
1244 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1247 register short *val = h->table;
1248 while((y=*val++)<0) {
1249 if (get1bit())
1250 val -= y;
1251 part2remain--;
1253 x = y >> 4;
1254 y &= 0xf;
1256 if (x == 15) {
1257 max = cb;
1258 part2remain -= h->linbits+1;
1259 x += getbits(h->linbits);
1260 if(get1bit()) {
1261 real a = ispow[x] * v;
1262 *xrpnt++ = *xr0pnt + a;
1263 *xr0pnt++ -= a;
1265 else {
1266 real a = ispow[x] * v;
1267 *xrpnt++ = *xr0pnt - a;
1268 *xr0pnt++ += a;
1271 else if(x) {
1272 max = cb;
1273 if(get1bit()) {
1274 real a = ispow[x] * v;
1275 *xrpnt++ = *xr0pnt + a;
1276 *xr0pnt++ -= a;
1278 else {
1279 real a = ispow[x] * v;
1280 *xrpnt++ = *xr0pnt - a;
1281 *xr0pnt++ += a;
1283 part2remain--;
1285 else
1286 *xrpnt++ = *xr0pnt++;
1288 if (y == 15) {
1289 max = cb;
1290 part2remain -= h->linbits+1;
1291 y += getbits(h->linbits);
1292 if(get1bit()) {
1293 real a = ispow[y] * v;
1294 *xrpnt++ = *xr0pnt + a;
1295 *xr0pnt++ -= a;
1297 else {
1298 real a = ispow[y] * v;
1299 *xrpnt++ = *xr0pnt - a;
1300 *xr0pnt++ += a;
1303 else if(y) {
1304 max = cb;
1305 if(get1bit()) {
1306 real a = ispow[y] * v;
1307 *xrpnt++ = *xr0pnt + a;
1308 *xr0pnt++ -= a;
1310 else {
1311 real a = ispow[y] * v;
1312 *xrpnt++ = *xr0pnt - a;
1313 *xr0pnt++ += a;
1315 part2remain--;
1317 else
1318 *xrpnt++ = *xr0pnt++;
1322 for(;l3 && (part2remain > 0);l3--) {
1323 struct newhuff *h = htc+gr_info->count1table_select;
1324 register short *val = h->table,a;
1326 while((a=*val++)<0) {
1327 part2remain--;
1328 if(part2remain < 0) {
1329 part2remain++;
1330 a = 0;
1331 break;
1333 if (get1bit())
1334 val -= a;
1337 for(i=0;i<4;i++) {
1338 if(!(i & 1)) {
1339 if(!mc) {
1340 mc = *m++;
1341 cb = *m++;
1342 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1344 mc--;
1346 if ( (a & (0x8>>i)) ) {
1347 max = cb;
1348 part2remain--;
1349 if(part2remain <= 0) {
1350 part2remain++;
1351 break;
1353 if(get1bit()) {
1354 *xrpnt++ = *xr0pnt + v;
1355 *xr0pnt++ -= v;
1357 else {
1358 *xrpnt++ = *xr0pnt - v;
1359 *xr0pnt++ += v;
1362 else
1363 *xrpnt++ = *xr0pnt++;
1366 for(i=(&xr[1][SBLIMIT][0]-xrpnt)>>1;i;i--) {
1367 *xrpnt++ = *xr0pnt++;
1368 *xrpnt++ = *xr0pnt++;
1371 gr_info->maxbandl = max+1;
1372 gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
1375 while ( part2remain > 16 ) {
1376 getbits(16); /* Dismiss stuffing Bits */
1377 part2remain -= 16;
1379 if(part2remain > 0 )
1380 getbits(part2remain);
1381 else if(part2remain < 0) {
1382 fprintf(stderr,"mpg123_ms: Can't rewind stream by %d bits!\n",-part2remain);
1383 return 1; /* -> error */
1385 return 0;
1387 #endif
1390 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
1392 static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
1393 struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
1395 real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
1396 const struct bandInfoStruct *bi = &bandInfo[sfreq];
1397 real *tab1,*tab2;
1399 if(lsf) {
1400 int p = gr_info->scalefac_compress & 0x1;
1401 if(ms_stereo) {
1402 tab1 = pow1_2[p]; tab2 = pow2_2[p];
1404 else {
1405 tab1 = pow1_1[p]; tab2 = pow2_1[p];
1408 else {
1409 if(ms_stereo) {
1410 tab1 = tan1_2; tab2 = tan2_2;
1412 else {
1413 tab1 = tan1_1; tab2 = tan2_1;
1417 if (gr_info->block_type == 2)
1419 int lwin,do_l = 0;
1420 if( gr_info->mixed_block_flag )
1421 do_l = 1;
1423 for (lwin=0;lwin<3;lwin++) /* process each window */
1425 /* get first band with zero values */
1426 int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
1427 if(sfb > 3)
1428 do_l = 0;
1430 for(;sfb<12;sfb++)
1432 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1433 if(is_p != 7) {
1434 real t1,t2;
1435 sb = bi->shortDiff[sfb];
1436 idx = bi->shortIdx[sfb] + lwin;
1437 t1 = tab1[is_p]; t2 = tab2[is_p];
1438 for (; sb > 0; sb--,idx+=3)
1440 real v = xr[0][idx];
1441 xr[0][idx] = v * t1;
1442 xr[1][idx] = v * t2;
1447 #if 1
1448 /* in the original: copy 10 to 11 , here: copy 11 to 12
1449 maybe still wrong??? (copy 12 to 13?) */
1450 is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1451 sb = bi->shortDiff[12];
1452 idx = bi->shortIdx[12] + lwin;
1453 #else
1454 is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1455 sb = bi->shortDiff[11];
1456 idx = bi->shortIdx[11] + lwin;
1457 #endif
1458 if(is_p != 7)
1460 real t1,t2;
1461 t1 = tab1[is_p]; t2 = tab2[is_p];
1462 for ( ; sb > 0; sb--,idx+=3 )
1464 real v = xr[0][idx];
1465 xr[0][idx] = v * t1;
1466 xr[1][idx] = v * t2;
1469 } /* end for(lwin; .. ; . ) */
1471 if (do_l)
1473 /* also check l-part, if ALL bands in the three windows are 'empty'
1474 * and mode = mixed_mode
1476 int sfb = gr_info->maxbandl;
1477 int idx = bi->longIdx[sfb];
1479 for ( ; sfb<8; sfb++ )
1481 int sb = bi->longDiff[sfb];
1482 int is_p = scalefac[sfb]; /* scale: 0-15 */
1483 if(is_p != 7) {
1484 real t1,t2;
1485 t1 = tab1[is_p]; t2 = tab2[is_p];
1486 for ( ; sb > 0; sb--,idx++)
1488 real v = xr[0][idx];
1489 xr[0][idx] = v * t1;
1490 xr[1][idx] = v * t2;
1493 else
1494 idx += sb;
1498 else /* ((gr_info->block_type != 2)) */
1500 int sfb = gr_info->maxbandl;
1501 int is_p,idx = bi->longIdx[sfb];
1502 for ( ; sfb<21; sfb++)
1504 int sb = bi->longDiff[sfb];
1505 is_p = scalefac[sfb]; /* scale: 0-15 */
1506 if(is_p != 7) {
1507 real t1,t2;
1508 t1 = tab1[is_p]; t2 = tab2[is_p];
1509 for ( ; sb > 0; sb--,idx++)
1511 real v = xr[0][idx];
1512 xr[0][idx] = v * t1;
1513 xr[1][idx] = v * t2;
1516 else
1517 idx += sb;
1520 is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1521 if(is_p != 7)
1523 int sb;
1524 real t1 = tab1[is_p],t2 = tab2[is_p];
1526 for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1528 real v = xr[0][idx];
1529 xr[0][idx] = v * t1;
1530 xr[1][idx] = v * t2;
1533 } /* ... */
1536 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1538 int sblim;
1540 if(gr_info->block_type == 2)
1542 if(!gr_info->mixed_block_flag)
1543 return;
1544 sblim = 1;
1546 else {
1547 sblim = gr_info->maxb-1;
1550 /* 31 alias-reduction operations between each pair of sub-bands */
1551 /* with 8 butterflies between each pair */
1554 int sb;
1555 real *xr1=(real *) xr[1];
1557 for(sb=sblim;sb;sb--,xr1+=10)
1559 int ss;
1560 real *cs=aa_cs,*ca=aa_ca;
1561 real *xr2 = xr1;
1563 for(ss=7;ss>=0;ss--)
1564 { /* upper and lower butterfly inputs */
1565 register real bu = *--xr2,bd = *xr1;
1566 *xr2 = (bu * (*cs) ) - (bd * (*ca) );
1567 *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
1574 DCT insipired by Jeff Tsay's DCT from the maplay package
1575 this is an optimized version with manual unroll.
1577 References:
1578 [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1579 Mathematics of Computation, Volume 32, Number 141, January 1978,
1580 Pages 175-199
1583 static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1586 register real *in = inbuf;
1588 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1589 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1590 in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8];
1591 in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5];
1592 in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2];
1593 in[2] +=in[1]; in[1] +=in[0];
1595 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1596 in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1];
1601 #define MACRO0(v) { \
1602 real tmp; \
1603 out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1604 out2[8-(v)] = tmp * w[26-(v)]; } \
1605 sum0 -= sum1; \
1606 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1607 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)];
1608 #define MACRO1(v) { \
1609 real sum0,sum1; \
1610 sum0 = tmp1a + tmp2a; \
1611 sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1612 MACRO0(v); }
1613 #define MACRO2(v) { \
1614 real sum0,sum1; \
1615 sum0 = tmp2a - tmp1a; \
1616 sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1617 MACRO0(v); }
1619 register const real *c = COS9;
1620 register real *out2 = o2;
1621 register real *w = wintab;
1622 register real *out1 = o1;
1623 register real *ts = tsbuf;
1625 real ta33,ta66,tb33,tb66;
1627 ta33 = in[2*3+0] * c[3];
1628 ta66 = in[2*6+0] * c[6];
1629 tb33 = in[2*3+1] * c[3];
1630 tb66 = in[2*6+1] * c[6];
1633 real tmp1a,tmp2a,tmp1b,tmp2b;
1634 tmp1a = in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1635 tmp1b = in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1636 tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1637 tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1639 MACRO1(0);
1640 MACRO2(8);
1644 real tmp1a,tmp2a,tmp1b,tmp2b;
1645 tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1646 tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1647 tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1648 tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1650 MACRO1(1);
1651 MACRO2(7);
1655 real tmp1a,tmp2a,tmp1b,tmp2b;
1656 tmp1a = in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1657 tmp1b = in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1658 tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1659 tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1661 MACRO1(2);
1662 MACRO2(6);
1666 real tmp1a,tmp2a,tmp1b,tmp2b;
1667 tmp1a = in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1668 tmp1b = in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1669 tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1670 tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1672 MACRO1(3);
1673 MACRO2(5);
1677 real sum0,sum1;
1678 sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1679 sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1680 MACRO0(4);
1688 * new DCT12
1690 static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1692 #define DCT12_PART1 \
1693 in5 = in[5*3]; \
1694 in5 += (in4 = in[4*3]); \
1695 in4 += (in3 = in[3*3]); \
1696 in3 += (in2 = in[2*3]); \
1697 in2 += (in1 = in[1*3]); \
1698 in1 += (in0 = in[0*3]); \
1700 in5 += in3; in3 += in1; \
1702 in2 *= COS6_1; \
1703 in3 *= COS6_1; \
1705 #define DCT12_PART2 \
1706 in0 += in4 * COS6_2; \
1708 in4 = in0 + in2; \
1709 in0 -= in2; \
1711 in1 += in5 * COS6_2; \
1713 in5 = (in1 + in3) * tfcos12[0]; \
1714 in1 = (in1 - in3) * tfcos12[2]; \
1716 in3 = in4 + in5; \
1717 in4 -= in5; \
1719 in2 = in0 + in1; \
1720 in0 -= in1;
1724 real in0,in1,in2,in3,in4,in5;
1725 register real *out1 = rawout1;
1726 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1727 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1729 DCT12_PART1
1732 real tmp0,tmp1 = (in0 - in4);
1734 real tmp2 = (in1 - in5) * tfcos12[1];
1735 tmp0 = tmp1 + tmp2;
1736 tmp1 -= tmp2;
1738 ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1739 ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1740 ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1741 ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1744 DCT12_PART2
1746 ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1747 ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1748 ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1749 ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1751 ts[(6+0)*SBLIMIT] = out1[6+0] + in0 * wi[0];
1752 ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1753 ts[(6+2)*SBLIMIT] = out1[6+2] + in4 * wi[2];
1754 ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1757 in++;
1760 real in0,in1,in2,in3,in4,in5;
1761 register real *out2 = rawout2;
1763 DCT12_PART1
1766 real tmp0,tmp1 = (in0 - in4);
1768 real tmp2 = (in1 - in5) * tfcos12[1];
1769 tmp0 = tmp1 + tmp2;
1770 tmp1 -= tmp2;
1772 out2[5-1] = tmp0 * wi[11-1];
1773 out2[0+1] = tmp0 * wi[6+1];
1774 ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1775 ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1778 DCT12_PART2
1780 out2[5-0] = in2 * wi[11-0];
1781 out2[0+0] = in2 * wi[6+0];
1782 out2[0+2] = in3 * wi[6+2];
1783 out2[5-2] = in3 * wi[11-2];
1785 ts[(12+0)*SBLIMIT] += in0 * wi[0];
1786 ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1787 ts[(12+2)*SBLIMIT] += in4 * wi[2];
1788 ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1791 in++;
1794 real in0,in1,in2,in3,in4,in5;
1795 register real *out2 = rawout2;
1796 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1798 DCT12_PART1
1801 real tmp0,tmp1 = (in0 - in4);
1803 real tmp2 = (in1 - in5) * tfcos12[1];
1804 tmp0 = tmp1 + tmp2;
1805 tmp1 -= tmp2;
1807 out2[11-1] = tmp0 * wi[11-1];
1808 out2[6 +1] = tmp0 * wi[6+1];
1809 out2[0+1] += tmp1 * wi[1];
1810 out2[5-1] += tmp1 * wi[5-1];
1813 DCT12_PART2
1815 out2[11-0] = in2 * wi[11-0];
1816 out2[6 +0] = in2 * wi[6+0];
1817 out2[6 +2] = in3 * wi[6+2];
1818 out2[11-2] = in3 * wi[11-2];
1820 out2[0+0] += in0 * wi[0];
1821 out2[5-0] += in0 * wi[5-0];
1822 out2[0+2] += in4 * wi[2];
1823 out2[5-2] += in4 * wi[5-2];
1828 * III_hybrid
1830 static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1831 int ch,struct gr_info_s *gr_info,struct mpstr *mp)
1833 real *tspnt = (real *) tsOut;
1834 real (*block)[2][SBLIMIT*SSLIMIT] = mp->hybrid_block;
1835 int *blc = mp->hybrid_blc;
1836 real *rawout1,*rawout2;
1837 int bt;
1838 int sb = 0;
1841 int b = blc[ch];
1842 rawout1=block[b][ch];
1843 b=-b+1;
1844 rawout2=block[b][ch];
1845 blc[ch] = b;
1849 if(gr_info->mixed_block_flag) {
1850 sb = 2;
1851 dct36(fsIn[0],rawout1,rawout2,win[0],tspnt);
1852 dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1853 rawout1 += 36; rawout2 += 36; tspnt += 2;
1856 bt = gr_info->block_type;
1857 if(bt == 2) {
1858 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1859 dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt);
1860 dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1863 else {
1864 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1865 dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1866 dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1870 for(;sb<SBLIMIT;sb++,tspnt++) {
1871 int i;
1872 for(i=0;i<SSLIMIT;i++) {
1873 tspnt[i*SBLIMIT] = *rawout1++;
1874 *rawout2++ = 0.0;
1880 * main layer3 handler
1882 int do_layer3(struct frame *fr,unsigned char *pcm_sample,int *pcm_point)
1884 int gr, ch, ss,clip=0;
1885 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1886 struct III_sideinfo sideinfo;
1887 int stereo = fr->stereo;
1888 int single = fr->single;
1889 int ms_stereo,i_stereo;
1890 int sfreq = fr->sampling_frequency;
1891 int stereo1,granules;
1893 if(stereo == 1) { /* stream is mono */
1894 stereo1 = 1;
1895 single = 0;
1897 else if(single >= 0) /* stream is stereo, but force to mono */
1898 stereo1 = 1;
1899 else
1900 stereo1 = 2;
1902 if(fr->mode == MPG_MD_JOINT_STEREO) {
1903 ms_stereo = fr->mode_ext & 0x2;
1904 i_stereo = fr->mode_ext & 0x1;
1906 else
1907 ms_stereo = i_stereo = 0;
1909 if(fr->lsf) {
1910 granules = 1;
1911 if(!III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single))
1912 return -1;
1914 else {
1915 granules = 2;
1916 #ifdef MPEG1
1917 if(!III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single))
1918 return -1;
1919 #else
1920 fprintf(stderr,"Not supported\n");
1921 #endif
1924 if(set_pointer(fr->mp,sideinfo.main_data_begin) == MP3_ERR)
1925 return -1;
1927 for (gr=0;gr<granules;gr++)
1929 real hybridIn[2][SBLIMIT][SSLIMIT];
1930 real hybridOut[2][SSLIMIT][SBLIMIT];
1933 struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
1934 long part2bits;
1935 if(fr->lsf)
1936 part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0);
1937 else {
1938 #ifdef MPEG1
1939 part2bits = III_get_scale_factors_1(scalefacs[0],gr_info);
1940 #else
1941 fprintf(stderr,"Not supported\n");
1942 #endif
1944 if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
1945 return clip;
1947 if(stereo == 2) {
1948 struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
1949 long part2bits;
1950 if(fr->lsf)
1951 part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo);
1952 else {
1953 #ifdef MPEG1
1954 part2bits = III_get_scale_factors_1(scalefacs[1],gr_info);
1955 #else
1956 fprintf(stderr,"Not supported\n");
1957 #endif
1960 if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
1961 return clip;
1963 if(ms_stereo) {
1964 int i;
1965 for(i=0;i<SBLIMIT*SSLIMIT;i++) {
1966 real tmp0,tmp1;
1967 tmp0 = ((real *) hybridIn[0])[i];
1968 tmp1 = ((real *) hybridIn[1])[i];
1969 ((real *) hybridIn[0])[i] = tmp0 + tmp1;
1970 ((real *) hybridIn[1])[i] = tmp0 - tmp1;
1974 if(i_stereo)
1975 III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
1977 if(ms_stereo || i_stereo || (single == 3) ) {
1978 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
1979 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
1980 else
1981 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
1984 switch(single) {
1985 case 3:
1987 register int i;
1988 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1989 for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++)
1990 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1992 break;
1993 case 1:
1995 register int i;
1996 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1997 for(i=0;i<SSLIMIT*gr_info->maxb;i++)
1998 *in0++ = *in1++;
2000 break;
2004 for(ch=0;ch<stereo1;ch++) {
2005 struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
2006 III_antialias(hybridIn[ch],gr_info);
2007 III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr->mp);
2010 for(ss=0;ss<SSLIMIT;ss++) {
2011 if(single >= 0) {
2012 clip += synth_1to1_mono(fr->mp,hybridOut[0][ss],pcm_sample,pcm_point);
2014 else {
2015 int p1 = *pcm_point;
2016 clip += synth_1to1(fr->mp,hybridOut[0][ss],0,pcm_sample,&p1);
2017 clip += synth_1to1(fr->mp,hybridOut[1][ss],1,pcm_sample,pcm_point);
2022 return clip;