d3d9: Fix a few ok conditions in the visual tests.
[wine.git] / dlls / winemp3.acm / layer3.c
blob963fc96df7fba103c3f8993442bdbe006964b036
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 "config.h"
24 #include <stdlib.h>
25 #include "wine/debug.h"
26 #include "mpg123.h"
27 #include "mpglib.h"
28 #include "huffman.h"
30 #define MPEG1
31 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
33 static real ispow[8207];
34 static real aa_ca[8],aa_cs[8];
35 static real COS1[12][6];
36 static real win[4][36];
37 static real win1[4][36];
38 static real gainpow2[256+118+4];
39 static real COS9[9];
40 static real COS6_1,COS6_2;
41 static real tfcos36[9];
42 static real tfcos12[3];
44 struct bandInfoStruct {
45 short longIdx[23];
46 short longDiff[22];
47 short shortIdx[14];
48 short shortDiff[13];
51 static int longLimit[9][23];
52 static int shortLimit[9][14];
54 static const struct bandInfoStruct bandInfo[9] = {
56 /* MPEG 1.0 */
57 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
58 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
59 {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},
60 {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
62 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
63 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
64 {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},
65 {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
67 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
68 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
69 {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} ,
70 {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
72 /* MPEG 2.0 */
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,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} ,
76 {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
78 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
79 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
80 {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} ,
81 {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
83 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
84 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
85 {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},
86 {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
87 /* MPEG 2.5 */
88 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
89 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
90 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
91 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
92 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
93 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
94 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
95 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
96 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
97 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
98 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
99 {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
102 static int mapbuf0[9][152];
103 static int mapbuf1[9][156];
104 static int mapbuf2[9][44];
105 static int *map[9][3];
106 static int *mapend[9][3];
108 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
109 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
111 static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
112 static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
115 * init tables for layer-3
117 void init_layer3(int down_sample_sblimit)
119 int i,j,k,l;
121 for(i=-256;i<118+4;i++)
122 gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
124 for(i=0;i<8207;i++)
125 ispow[i] = pow((double)i,(double)4.0/3.0);
127 for (i=0;i<8;i++)
129 static const double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
130 double sq=sqrt(1.0+Ci[i]*Ci[i]);
131 aa_cs[i] = 1.0/sq;
132 aa_ca[i] = Ci[i]/sq;
135 for(i=0;i<18;i++)
137 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 );
138 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 );
140 for(i=0;i<6;i++)
142 win[1][i+18] = 0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 );
143 win[3][i+12] = 0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 );
144 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 );
145 win[1][i+30] = win[3][i] = 0.0;
146 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 );
149 for(i=0;i<9;i++)
150 COS9[i] = cos( M_PI / 18.0 * (double) i);
152 for(i=0;i<9;i++)
153 tfcos36[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 );
154 for(i=0;i<3;i++)
155 tfcos12[i] = 0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 );
157 COS6_1 = cos( M_PI / 6.0 * (double) 1);
158 COS6_2 = cos( M_PI / 6.0 * (double) 2);
160 for(i=0;i<12;i++)
162 win[2][i] = 0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 );
163 for(j=0;j<6;j++)
164 COS1[i][j] = cos( M_PI / 24.0 * (double) ((2*i+7)*(2*j+1)) );
167 for(j=0;j<4;j++) {
168 static const int len[4] = { 36,36,12,36 };
169 for(i=0;i<len[j];i+=2)
170 win1[j][i] = + win[j][i];
171 for(i=1;i<len[j];i+=2)
172 win1[j][i] = - win[j][i];
175 for(i=0;i<16;i++)
177 double t = tan( (double) i * M_PI / 12.0 );
178 tan1_1[i] = t / (1.0+t);
179 tan2_1[i] = 1.0 / (1.0 + t);
180 tan1_2[i] = M_SQRT2 * t / (1.0+t);
181 tan2_2[i] = M_SQRT2 / (1.0 + t);
183 for(j=0;j<2;j++) {
184 double base = pow(2.0,-0.25*(j+1.0));
185 double p1=1.0,p2=1.0;
186 if(i > 0) {
187 if( i & 1 )
188 p1 = pow(base,(i+1.0)*0.5);
189 else
190 p2 = pow(base,i*0.5);
192 pow1_1[j][i] = p1;
193 pow2_1[j][i] = p2;
194 pow1_2[j][i] = M_SQRT2 * p1;
195 pow2_2[j][i] = M_SQRT2 * p2;
199 for(j=0;j<9;j++)
201 const struct bandInfoStruct *bi = &bandInfo[j];
202 int *mp;
203 int cb,lwin;
204 const short *bdf;
206 mp = map[j][0] = mapbuf0[j];
207 bdf = bi->longDiff;
208 for(i=0,cb = 0; cb < 8 ; cb++,i+=*bdf++) {
209 *mp++ = (*bdf) >> 1;
210 *mp++ = i;
211 *mp++ = 3;
212 *mp++ = cb;
214 bdf = bi->shortDiff+3;
215 for(cb=3;cb<13;cb++) {
216 int l = (*bdf++) >> 1;
217 for(lwin=0;lwin<3;lwin++) {
218 *mp++ = l;
219 *mp++ = i + lwin;
220 *mp++ = lwin;
221 *mp++ = cb;
223 i += 6*l;
225 mapend[j][0] = mp;
227 mp = map[j][1] = mapbuf1[j];
228 bdf = bi->shortDiff+0;
229 for(i=0,cb=0;cb<13;cb++) {
230 int l = (*bdf++) >> 1;
231 for(lwin=0;lwin<3;lwin++) {
232 *mp++ = l;
233 *mp++ = i + lwin;
234 *mp++ = lwin;
235 *mp++ = cb;
237 i += 6*l;
239 mapend[j][1] = mp;
241 mp = map[j][2] = mapbuf2[j];
242 bdf = bi->longDiff;
243 for(cb = 0; cb < 22 ; cb++) {
244 *mp++ = (*bdf++) >> 1;
245 *mp++ = cb;
247 mapend[j][2] = mp;
251 for(j=0;j<9;j++) {
252 for(i=0;i<23;i++) {
253 longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
254 if(longLimit[j][i] > (down_sample_sblimit) )
255 longLimit[j][i] = down_sample_sblimit;
257 for(i=0;i<14;i++) {
258 shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
259 if(shortLimit[j][i] > (down_sample_sblimit) )
260 shortLimit[j][i] = down_sample_sblimit;
264 for(i=0;i<5;i++) {
265 for(j=0;j<6;j++) {
266 for(k=0;k<6;k++) {
267 int n = k + j * 6 + i * 36;
268 i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
272 for(i=0;i<4;i++) {
273 for(j=0;j<4;j++) {
274 for(k=0;k<4;k++) {
275 int n = k + j * 4 + i * 16;
276 i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
280 for(i=0;i<4;i++) {
281 for(j=0;j<3;j++) {
282 int n = j + i * 3;
283 i_slen2[n+244] = i|(j<<3) | (5<<12);
284 n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
288 for(i=0;i<5;i++) {
289 for(j=0;j<5;j++) {
290 for(k=0;k<4;k++) {
291 for(l=0;l<4;l++) {
292 int n = l + k * 4 + j * 16 + i * 80;
293 n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
298 for(i=0;i<5;i++) {
299 for(j=0;j<5;j++) {
300 for(k=0;k<4;k++) {
301 int n = k + j * 4 + i * 20;
302 n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
309 * read additional side information
311 #ifdef MPEG1
312 static int III_get_side_info_1(struct III_sideinfo *si,int stereo,
313 int ms_stereo,long sfreq,int single)
315 int ch, gr;
316 int powdiff = (single == 3) ? 4 : 0;
318 si->main_data_begin = getbits(9);
319 if (stereo == 1)
320 si->private_bits = getbits_fast(5);
321 else
322 si->private_bits = getbits_fast(3);
324 for (ch=0; ch<stereo; ch++) {
325 si->ch[ch].gr[0].scfsi = -1;
326 si->ch[ch].gr[1].scfsi = getbits_fast(4);
329 for (gr=0; gr<2; gr++)
331 for (ch=0; ch<stereo; ch++)
333 register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
335 gr_info->part2_3_length = getbits(12);
336 gr_info->big_values = getbits_fast(9);
337 if(gr_info->big_values > 288) {
338 FIXME("big_values (%d) too large!\n", gr_info->big_values);
339 gr_info->big_values = 288;
341 gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
342 if(ms_stereo)
343 gr_info->pow2gain += 2;
344 gr_info->scalefac_compress = getbits_fast(4);
345 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
346 if(get1bit())
348 int i;
349 gr_info->block_type = getbits_fast(2);
350 gr_info->mixed_block_flag = get1bit();
351 gr_info->table_select[0] = getbits_fast(5);
352 gr_info->table_select[1] = getbits_fast(5);
354 * table_select[2] not needed, because there is no region2,
355 * but to satisfy some verifications tools we set it either.
357 gr_info->table_select[2] = 0;
358 for(i=0;i<3;i++)
359 gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
361 if(gr_info->block_type == 0) {
362 FIXME("Blocktype == 0 and window-switching == 1 not allowed.\n");
363 return 0;
365 /* region_count/start parameters are implicit in this case. */
366 gr_info->region1start = 36>>1;
367 gr_info->region2start = 576>>1;
369 else
371 int i,r0c,r1c;
372 for (i=0; i<3; i++)
373 gr_info->table_select[i] = getbits_fast(5);
374 r0c = getbits_fast(4);
375 r1c = getbits_fast(3);
376 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
377 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
378 gr_info->block_type = 0;
379 gr_info->mixed_block_flag = 0;
381 gr_info->preflag = get1bit();
382 gr_info->scalefac_scale = get1bit();
383 gr_info->count1table_select = get1bit();
386 return !0;
388 #endif
391 * Side Info for MPEG 2.0 / LSF
393 static int III_get_side_info_2(struct III_sideinfo *si,int stereo,
394 int ms_stereo,long sfreq,int single)
396 int ch;
397 int powdiff = (single == 3) ? 4 : 0;
399 si->main_data_begin = getbits(8);
400 if (stereo == 1)
401 si->private_bits = get1bit();
402 else
403 si->private_bits = getbits_fast(2);
405 for (ch=0; ch<stereo; ch++)
407 register struct gr_info_s *gr_info = &(si->ch[ch].gr[0]);
409 gr_info->part2_3_length = getbits(12);
410 gr_info->big_values = getbits_fast(9);
411 if(gr_info->big_values > 288) {
412 FIXME("big_values(%d) too large!\n", gr_info->big_values);
413 gr_info->big_values = 288;
415 gr_info->pow2gain = gainpow2+256 - getbits_fast(8) + powdiff;
416 if(ms_stereo)
417 gr_info->pow2gain += 2;
418 gr_info->scalefac_compress = getbits(9);
419 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
420 if(get1bit())
422 int i;
423 gr_info->block_type = getbits_fast(2);
424 gr_info->mixed_block_flag = get1bit();
425 gr_info->table_select[0] = getbits_fast(5);
426 gr_info->table_select[1] = getbits_fast(5);
428 * table_select[2] not needed, because there is no region2,
429 * but to satisfy some verifications tools we set it either.
431 gr_info->table_select[2] = 0;
432 for(i=0;i<3;i++)
433 gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(3)<<3);
435 if(gr_info->block_type == 0) {
436 FIXME("Blocktype == 0 and window-switching == 1 not allowed.\n");
437 return 0;
439 /* region_count/start parameters are implicit in this case. */
440 /* check this again! */
441 if(gr_info->block_type == 2)
442 gr_info->region1start = 36>>1;
443 else if(sfreq == 8)
444 /* check this for 2.5 and sfreq=8 */
445 gr_info->region1start = 108>>1;
446 else
447 gr_info->region1start = 54>>1;
448 gr_info->region2start = 576>>1;
450 else
452 int i,r0c,r1c;
453 for (i=0; i<3; i++)
454 gr_info->table_select[i] = getbits_fast(5);
455 r0c = getbits_fast(4);
456 r1c = getbits_fast(3);
457 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
458 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
459 gr_info->block_type = 0;
460 gr_info->mixed_block_flag = 0;
462 gr_info->scalefac_scale = get1bit();
463 gr_info->count1table_select = get1bit();
465 return !0;
469 * read scalefactors
471 #ifdef MPEG1
472 static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
474 static const unsigned char slen[2][16] = {
475 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
476 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
478 int numbits;
479 int num0 = slen[0][gr_info->scalefac_compress];
480 int num1 = slen[1][gr_info->scalefac_compress];
482 if (gr_info->block_type == 2) {
483 int i=18;
484 numbits = (num0 + num1) * 18;
486 if (gr_info->mixed_block_flag) {
487 for (i=8;i;i--)
488 *scf++ = getbits_fast(num0);
489 i = 9;
490 numbits -= num0; /* num0 * 17 + num1 * 18 */
493 for (;i;i--)
494 *scf++ = getbits_fast(num0);
495 for (i = 18; i; i--)
496 *scf++ = getbits_fast(num1);
497 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
499 else {
500 int i;
501 int scfsi = gr_info->scfsi;
503 if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
504 for(i=11;i;i--)
505 *scf++ = getbits_fast(num0);
506 for(i=10;i;i--)
507 *scf++ = getbits_fast(num1);
508 numbits = (num0 + num1) * 10 + num0;
509 *scf++ = 0;
511 else {
512 numbits = 0;
513 if(!(scfsi & 0x8)) {
514 for (i=0;i<6;i++)
515 *scf++ = getbits_fast(num0);
516 numbits += num0 * 6;
518 else {
519 scf += 6;
522 if(!(scfsi & 0x4)) {
523 for (i=0;i<5;i++)
524 *scf++ = getbits_fast(num0);
525 numbits += num0 * 5;
527 else {
528 scf += 5;
531 if(!(scfsi & 0x2)) {
532 for(i=0;i<5;i++)
533 *scf++ = getbits_fast(num1);
534 numbits += num1 * 5;
536 else {
537 scf += 5;
540 if(!(scfsi & 0x1)) {
541 for (i=0;i<5;i++)
542 *scf++ = getbits_fast(num1);
543 numbits += num1 * 5;
545 else {
546 scf += 5;
548 *scf++ = 0; /* no l[21] in original sources */
551 return numbits;
553 #endif
556 static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
558 const unsigned char *pnt;
559 int i,j;
560 unsigned int slen;
561 int n = 0;
562 int numbits = 0;
564 static const unsigned char stab[3][6][4] = {
565 { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
566 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
567 { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
568 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
569 { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
570 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
572 if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
573 slen = i_slen2[gr_info->scalefac_compress>>1];
574 else
575 slen = n_slen2[gr_info->scalefac_compress];
577 gr_info->preflag = (slen>>15) & 0x1;
579 n = 0;
580 if( gr_info->block_type == 2 ) {
581 n++;
582 if(gr_info->mixed_block_flag)
583 n++;
586 pnt = stab[n][(slen>>12)&0x7];
588 for(i=0;i<4;i++) {
589 int num = slen & 0x7;
590 slen >>= 3;
591 if(num) {
592 for(j=0;j<(int)(pnt[i]);j++)
593 *scf++ = getbits_fast(num);
594 numbits += pnt[i] * num;
596 else {
597 for(j=0;j<(int)(pnt[i]);j++)
598 *scf++ = 0;
602 n = (n << 1) + 1;
603 for(i=0;i<n;i++)
604 *scf++ = 0;
606 return numbits;
609 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};
610 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};
613 * don't forget to apply the same changes to III_dequantize_sample_ms() !!!
615 static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
616 struct gr_info_s *gr_info,int sfreq,int part2bits)
618 int shift = 1 + gr_info->scalefac_scale;
619 real *xrpnt = (real *) xr;
620 int l[3],l3;
621 int part2remain = gr_info->part2_3_length - part2bits;
622 int *me;
625 int bv = gr_info->big_values;
626 int region1 = gr_info->region1start;
627 int region2 = gr_info->region2start;
629 l3 = ((576>>1)-bv)>>1;
631 * we may lose the 'odd' bit here !!
632 * check this later again
634 if(bv <= region1) {
635 l[0] = bv; l[1] = 0; l[2] = 0;
637 else {
638 l[0] = region1;
639 if(bv <= region2) {
640 l[1] = bv - l[0]; l[2] = 0;
642 else {
643 l[1] = region2 - l[0]; l[2] = bv - region2;
648 if(gr_info->block_type == 2) {
650 * decoding with short or mixed mode BandIndex table
652 int i,max[4];
653 int step=0,lwin=0,cb=0;
654 register real v = 0.0;
655 register int *m,mc;
657 if(gr_info->mixed_block_flag) {
658 max[3] = -1;
659 max[0] = max[1] = max[2] = 2;
660 m = map[sfreq][0];
661 me = mapend[sfreq][0];
663 else {
664 max[0] = max[1] = max[2] = max[3] = -1;
665 /* max[3] not really needed in this case */
666 m = map[sfreq][1];
667 me = mapend[sfreq][1];
670 mc = 0;
671 for(i=0;i<2;i++) {
672 int lp = l[i];
673 const struct newhuff *h = ht+gr_info->table_select[i];
674 for(;lp;lp--,mc--) {
675 register int x,y;
676 if( (!mc) ) {
677 mc = *m++;
678 xrpnt = ((real *) xr) + (*m++);
679 lwin = *m++;
680 cb = *m++;
681 if(lwin == 3) {
682 v = gr_info->pow2gain[(*scf++) << shift];
683 step = 1;
685 else {
686 v = gr_info->full_gain[lwin][(*scf++) << shift];
687 step = 3;
691 register const short *val = h->table;
692 while((y=*val++)<0) {
693 if (get1bit())
694 val -= y;
695 part2remain--;
697 x = y >> 4;
698 y &= 0xf;
700 if(x == 15) {
701 max[lwin] = cb;
702 part2remain -= h->linbits+1;
703 x += getbits(h->linbits);
704 if(get1bit())
705 *xrpnt = -ispow[x] * v;
706 else
707 *xrpnt = ispow[x] * v;
709 else if(x) {
710 max[lwin] = cb;
711 if(get1bit())
712 *xrpnt = -ispow[x] * v;
713 else
714 *xrpnt = ispow[x] * v;
715 part2remain--;
717 else
718 *xrpnt = 0.0;
719 xrpnt += step;
720 if(y == 15) {
721 max[lwin] = cb;
722 part2remain -= h->linbits+1;
723 y += getbits(h->linbits);
724 if(get1bit())
725 *xrpnt = -ispow[y] * v;
726 else
727 *xrpnt = ispow[y] * v;
729 else if(y) {
730 max[lwin] = cb;
731 if(get1bit())
732 *xrpnt = -ispow[y] * v;
733 else
734 *xrpnt = ispow[y] * v;
735 part2remain--;
737 else
738 *xrpnt = 0.0;
739 xrpnt += step;
742 for(;l3 && (part2remain > 0);l3--) {
743 const struct newhuff *h = htc+gr_info->count1table_select;
744 const short *val = h->table;
745 short a;
747 while((a=*val++)<0) {
748 part2remain--;
749 if(part2remain < 0) {
750 part2remain++;
751 a = 0;
752 break;
754 if (get1bit())
755 val -= a;
758 for(i=0;i<4;i++) {
759 if(!(i & 1)) {
760 if(!mc) {
761 mc = *m++;
762 xrpnt = ((real *) xr) + (*m++);
763 lwin = *m++;
764 cb = *m++;
765 if(lwin == 3) {
766 v = gr_info->pow2gain[(*scf++) << shift];
767 step = 1;
769 else {
770 v = gr_info->full_gain[lwin][(*scf++) << shift];
771 step = 3;
774 mc--;
776 if( (a & (0x8>>i)) ) {
777 max[lwin] = cb;
778 part2remain--;
779 if(part2remain < 0) {
780 part2remain++;
781 break;
783 if(get1bit())
784 *xrpnt = -v;
785 else
786 *xrpnt = v;
788 else
789 *xrpnt = 0.0;
790 xrpnt += step;
794 while( m < me ) {
795 if(!mc) {
796 mc = *m++;
797 xrpnt = ((real *) xr) + *m++;
798 if( (*m++) == 3)
799 step = 1;
800 else
801 step = 3;
802 m++; /* cb */
804 mc--;
805 *xrpnt = 0.0;
806 xrpnt += step;
807 *xrpnt = 0.0;
808 xrpnt += step;
809 /* we could add a little opt. here:
810 * if we finished a band for window 3 or a long band
811 * further bands could copied in a simple loop without a
812 * special 'map' decoding
816 gr_info->maxband[0] = max[0]+1;
817 gr_info->maxband[1] = max[1]+1;
818 gr_info->maxband[2] = max[2]+1;
819 gr_info->maxbandl = max[3]+1;
822 int rmax = max[0] > max[1] ? max[0] : max[1];
823 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
824 gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
828 else {
830 * decoding with 'long' BandIndex table (block_type != 2)
832 const int *pretab = gr_info->preflag ? pretab1 : pretab2;
833 int i,max = -1;
834 int cb = 0;
835 register int *m = map[sfreq][2];
836 register real v = 0.0;
837 register int mc = 0;
838 #if 0
839 me = mapend[sfreq][2];
840 #endif
843 * long hash table values
845 for(i=0;i<3;i++) {
846 int lp = l[i];
847 const struct newhuff *h = ht+gr_info->table_select[i];
849 for(;lp;lp--,mc--) {
850 int x,y;
852 if(!mc) {
853 mc = *m++;
854 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
855 cb = *m++;
858 register const short *val = h->table;
859 while((y=*val++)<0) {
860 if (get1bit())
861 val -= y;
862 part2remain--;
864 x = y >> 4;
865 y &= 0xf;
867 if (x == 15) {
868 max = cb;
869 part2remain -= h->linbits+1;
870 x += getbits(h->linbits);
871 if(get1bit())
872 *xrpnt++ = -ispow[x] * v;
873 else
874 *xrpnt++ = ispow[x] * v;
876 else if(x) {
877 max = cb;
878 if(get1bit())
879 *xrpnt++ = -ispow[x] * v;
880 else
881 *xrpnt++ = ispow[x] * v;
882 part2remain--;
884 else
885 *xrpnt++ = 0.0;
887 if (y == 15) {
888 max = cb;
889 part2remain -= h->linbits+1;
890 y += getbits(h->linbits);
891 if(get1bit())
892 *xrpnt++ = -ispow[y] * v;
893 else
894 *xrpnt++ = ispow[y] * v;
896 else if(y) {
897 max = cb;
898 if(get1bit())
899 *xrpnt++ = -ispow[y] * v;
900 else
901 *xrpnt++ = ispow[y] * v;
902 part2remain--;
904 else
905 *xrpnt++ = 0.0;
910 * short (count1table) values
912 for(;l3 && (part2remain > 0);l3--) {
913 const struct newhuff *h = htc+gr_info->count1table_select;
914 const short *val = h->table;
915 short a;
917 while((a=*val++)<0) {
918 part2remain--;
919 if(part2remain < 0) {
920 part2remain++;
921 a = 0;
922 break;
924 if (get1bit())
925 val -= a;
928 for(i=0;i<4;i++) {
929 if(!(i & 1)) {
930 if(!mc) {
931 mc = *m++;
932 cb = *m++;
933 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
935 mc--;
937 if ( (a & (0x8>>i)) ) {
938 max = cb;
939 part2remain--;
940 if(part2remain < 0) {
941 part2remain++;
942 break;
944 if(get1bit())
945 *xrpnt++ = -v;
946 else
947 *xrpnt++ = v;
949 else
950 *xrpnt++ = 0.0;
955 * zero part
957 for(i=(&xr[SBLIMIT][0]-xrpnt)>>1;i;i--) {
958 *xrpnt++ = 0.0;
959 *xrpnt++ = 0.0;
962 gr_info->maxbandl = max+1;
963 gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
966 while( part2remain > 16 ) {
967 getbits(16); /* Dismiss stuffing Bits */
968 part2remain -= 16;
970 if(part2remain > 0)
971 getbits(part2remain);
972 else if(part2remain < 0) {
973 FIXME("mpg123: Can't rewind stream by %d bits!\n",-part2remain);
974 return 1; /* -> error */
976 return 0;
979 #if 0
980 static int III_dequantize_sample_ms(real xr[2][SBLIMIT][SSLIMIT],int *scf,
981 struct gr_info_s *gr_info,int sfreq,int part2bits)
983 int shift = 1 + gr_info->scalefac_scale;
984 real *xrpnt = (real *) xr[1];
985 real *xr0pnt = (real *) xr[0];
986 int l[3],l3;
987 int part2remain = gr_info->part2_3_length - part2bits;
988 int *me;
991 int bv = gr_info->big_values;
992 int region1 = gr_info->region1start;
993 int region2 = gr_info->region2start;
995 l3 = ((576>>1)-bv)>>1;
997 * we may lose the 'odd' bit here !!
998 * check this later gain
1000 if(bv <= region1) {
1001 l[0] = bv; l[1] = 0; l[2] = 0;
1003 else {
1004 l[0] = region1;
1005 if(bv <= region2) {
1006 l[1] = bv - l[0]; l[2] = 0;
1008 else {
1009 l[1] = region2 - l[0]; l[2] = bv - region2;
1014 if(gr_info->block_type == 2) {
1015 int i,max[4];
1016 int step=0,lwin=0,cb=0;
1017 register real v = 0.0;
1018 register int *m,mc = 0;
1020 if(gr_info->mixed_block_flag) {
1021 max[3] = -1;
1022 max[0] = max[1] = max[2] = 2;
1023 m = map[sfreq][0];
1024 me = mapend[sfreq][0];
1026 else {
1027 max[0] = max[1] = max[2] = max[3] = -1;
1028 /* max[3] not really needed in this case */
1029 m = map[sfreq][1];
1030 me = mapend[sfreq][1];
1033 for(i=0;i<2;i++) {
1034 int lp = l[i];
1035 struct newhuff *h = ht+gr_info->table_select[i];
1036 for(;lp;lp--,mc--) {
1037 int x,y;
1039 if(!mc) {
1040 mc = *m++;
1041 xrpnt = ((real *) xr[1]) + *m;
1042 xr0pnt = ((real *) xr[0]) + *m++;
1043 lwin = *m++;
1044 cb = *m++;
1045 if(lwin == 3) {
1046 v = gr_info->pow2gain[(*scf++) << shift];
1047 step = 1;
1049 else {
1050 v = gr_info->full_gain[lwin][(*scf++) << shift];
1051 step = 3;
1055 register short *val = h->table;
1056 while((y=*val++)<0) {
1057 if (get1bit())
1058 val -= y;
1059 part2remain--;
1061 x = y >> 4;
1062 y &= 0xf;
1064 if(x == 15) {
1065 max[lwin] = cb;
1066 part2remain -= h->linbits+1;
1067 x += getbits(h->linbits);
1068 if(get1bit()) {
1069 real a = ispow[x] * v;
1070 *xrpnt = *xr0pnt + a;
1071 *xr0pnt -= a;
1073 else {
1074 real a = ispow[x] * v;
1075 *xrpnt = *xr0pnt - a;
1076 *xr0pnt += a;
1079 else if(x) {
1080 max[lwin] = cb;
1081 if(get1bit()) {
1082 real a = ispow[x] * v;
1083 *xrpnt = *xr0pnt + a;
1084 *xr0pnt -= a;
1086 else {
1087 real a = ispow[x] * v;
1088 *xrpnt = *xr0pnt - a;
1089 *xr0pnt += a;
1091 part2remain--;
1093 else
1094 *xrpnt = *xr0pnt;
1095 xrpnt += step;
1096 xr0pnt += step;
1098 if(y == 15) {
1099 max[lwin] = cb;
1100 part2remain -= h->linbits+1;
1101 y += getbits(h->linbits);
1102 if(get1bit()) {
1103 real a = ispow[y] * v;
1104 *xrpnt = *xr0pnt + a;
1105 *xr0pnt -= a;
1107 else {
1108 real a = ispow[y] * v;
1109 *xrpnt = *xr0pnt - a;
1110 *xr0pnt += a;
1113 else if(y) {
1114 max[lwin] = cb;
1115 if(get1bit()) {
1116 real a = ispow[y] * v;
1117 *xrpnt = *xr0pnt + a;
1118 *xr0pnt -= a;
1120 else {
1121 real a = ispow[y] * v;
1122 *xrpnt = *xr0pnt - a;
1123 *xr0pnt += a;
1125 part2remain--;
1127 else
1128 *xrpnt = *xr0pnt;
1129 xrpnt += step;
1130 xr0pnt += step;
1134 for(;l3 && (part2remain > 0);l3--) {
1135 struct newhuff *h = htc+gr_info->count1table_select;
1136 register short *val = h->table,a;
1138 while((a=*val++)<0) {
1139 part2remain--;
1140 if(part2remain < 0) {
1141 part2remain++;
1142 a = 0;
1143 break;
1145 if (get1bit())
1146 val -= a;
1149 for(i=0;i<4;i++) {
1150 if(!(i & 1)) {
1151 if(!mc) {
1152 mc = *m++;
1153 xrpnt = ((real *) xr[1]) + *m;
1154 xr0pnt = ((real *) xr[0]) + *m++;
1155 lwin = *m++;
1156 cb = *m++;
1157 if(lwin == 3) {
1158 v = gr_info->pow2gain[(*scf++) << shift];
1159 step = 1;
1161 else {
1162 v = gr_info->full_gain[lwin][(*scf++) << shift];
1163 step = 3;
1166 mc--;
1168 if( (a & (0x8>>i)) ) {
1169 max[lwin] = cb;
1170 part2remain--;
1171 if(part2remain < 0) {
1172 part2remain++;
1173 break;
1175 if(get1bit()) {
1176 *xrpnt = *xr0pnt + v;
1177 *xr0pnt -= v;
1179 else {
1180 *xrpnt = *xr0pnt - v;
1181 *xr0pnt += v;
1184 else
1185 *xrpnt = *xr0pnt;
1186 xrpnt += step;
1187 xr0pnt += step;
1191 while( m < me ) {
1192 if(!mc) {
1193 mc = *m++;
1194 xrpnt = ((real *) xr[1]) + *m;
1195 xr0pnt = ((real *) xr[0]) + *m++;
1196 if(*m++ == 3)
1197 step = 1;
1198 else
1199 step = 3;
1200 m++; /* cb */
1202 mc--;
1203 *xrpnt = *xr0pnt;
1204 xrpnt += step;
1205 xr0pnt += step;
1206 *xrpnt = *xr0pnt;
1207 xrpnt += step;
1208 xr0pnt += step;
1209 /* we could add a little opt. here:
1210 * if we finished a band for window 3 or a long band
1211 * further bands could copied in a simple loop without a
1212 * special 'map' decoding
1216 gr_info->maxband[0] = max[0]+1;
1217 gr_info->maxband[1] = max[1]+1;
1218 gr_info->maxband[2] = max[2]+1;
1219 gr_info->maxbandl = max[3]+1;
1222 int rmax = max[0] > max[1] ? max[0] : max[1];
1223 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
1224 gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
1227 else {
1228 int *pretab = gr_info->preflag ? pretab1 : pretab2;
1229 int i,max = -1;
1230 int cb = 0;
1231 register int mc=0,*m = map[sfreq][2];
1232 register real v = 0.0;
1233 #if 0
1234 me = mapend[sfreq][2];
1235 #endif
1237 for(i=0;i<3;i++) {
1238 int lp = l[i];
1239 struct newhuff *h = ht+gr_info->table_select[i];
1241 for(;lp;lp--,mc--) {
1242 int x,y;
1243 if(!mc) {
1244 mc = *m++;
1245 cb = *m++;
1246 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1249 register short *val = h->table;
1250 while((y=*val++)<0) {
1251 if (get1bit())
1252 val -= y;
1253 part2remain--;
1255 x = y >> 4;
1256 y &= 0xf;
1258 if (x == 15) {
1259 max = cb;
1260 part2remain -= h->linbits+1;
1261 x += getbits(h->linbits);
1262 if(get1bit()) {
1263 real a = ispow[x] * v;
1264 *xrpnt++ = *xr0pnt + a;
1265 *xr0pnt++ -= a;
1267 else {
1268 real a = ispow[x] * v;
1269 *xrpnt++ = *xr0pnt - a;
1270 *xr0pnt++ += a;
1273 else if(x) {
1274 max = cb;
1275 if(get1bit()) {
1276 real a = ispow[x] * v;
1277 *xrpnt++ = *xr0pnt + a;
1278 *xr0pnt++ -= a;
1280 else {
1281 real a = ispow[x] * v;
1282 *xrpnt++ = *xr0pnt - a;
1283 *xr0pnt++ += a;
1285 part2remain--;
1287 else
1288 *xrpnt++ = *xr0pnt++;
1290 if (y == 15) {
1291 max = cb;
1292 part2remain -= h->linbits+1;
1293 y += getbits(h->linbits);
1294 if(get1bit()) {
1295 real a = ispow[y] * v;
1296 *xrpnt++ = *xr0pnt + a;
1297 *xr0pnt++ -= a;
1299 else {
1300 real a = ispow[y] * v;
1301 *xrpnt++ = *xr0pnt - a;
1302 *xr0pnt++ += a;
1305 else if(y) {
1306 max = cb;
1307 if(get1bit()) {
1308 real a = ispow[y] * v;
1309 *xrpnt++ = *xr0pnt + a;
1310 *xr0pnt++ -= a;
1312 else {
1313 real a = ispow[y] * v;
1314 *xrpnt++ = *xr0pnt - a;
1315 *xr0pnt++ += a;
1317 part2remain--;
1319 else
1320 *xrpnt++ = *xr0pnt++;
1324 for(;l3 && (part2remain > 0);l3--) {
1325 struct newhuff *h = htc+gr_info->count1table_select;
1326 register short *val = h->table,a;
1328 while((a=*val++)<0) {
1329 part2remain--;
1330 if(part2remain < 0) {
1331 part2remain++;
1332 a = 0;
1333 break;
1335 if (get1bit())
1336 val -= a;
1339 for(i=0;i<4;i++) {
1340 if(!(i & 1)) {
1341 if(!mc) {
1342 mc = *m++;
1343 cb = *m++;
1344 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1346 mc--;
1348 if ( (a & (0x8>>i)) ) {
1349 max = cb;
1350 part2remain--;
1351 if(part2remain <= 0) {
1352 part2remain++;
1353 break;
1355 if(get1bit()) {
1356 *xrpnt++ = *xr0pnt + v;
1357 *xr0pnt++ -= v;
1359 else {
1360 *xrpnt++ = *xr0pnt - v;
1361 *xr0pnt++ += v;
1364 else
1365 *xrpnt++ = *xr0pnt++;
1368 for(i=(&xr[1][SBLIMIT][0]-xrpnt)>>1;i;i--) {
1369 *xrpnt++ = *xr0pnt++;
1370 *xrpnt++ = *xr0pnt++;
1373 gr_info->maxbandl = max+1;
1374 gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
1377 while ( part2remain > 16 ) {
1378 getbits(16); /* Dismiss stuffing Bits */
1379 part2remain -= 16;
1381 if(part2remain > 0 )
1382 getbits(part2remain);
1383 else if(part2remain < 0) {
1384 FIXME("mpg123_ms: Can't rewind stream by %d bits!\n",-part2remain);
1385 return 1; /* -> error */
1387 return 0;
1389 #endif
1392 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
1394 static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
1395 struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
1397 real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
1398 const struct bandInfoStruct *bi = &bandInfo[sfreq];
1399 real *tab1,*tab2;
1401 if(lsf) {
1402 int p = gr_info->scalefac_compress & 0x1;
1403 if(ms_stereo) {
1404 tab1 = pow1_2[p]; tab2 = pow2_2[p];
1406 else {
1407 tab1 = pow1_1[p]; tab2 = pow2_1[p];
1410 else {
1411 if(ms_stereo) {
1412 tab1 = tan1_2; tab2 = tan2_2;
1414 else {
1415 tab1 = tan1_1; tab2 = tan2_1;
1419 if (gr_info->block_type == 2)
1421 int lwin,do_l = 0;
1422 if( gr_info->mixed_block_flag )
1423 do_l = 1;
1425 for (lwin=0;lwin<3;lwin++) /* process each window */
1427 /* get first band with zero values */
1428 int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
1429 if(sfb > 3)
1430 do_l = 0;
1432 for(;sfb<12;sfb++)
1434 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1435 if(is_p != 7) {
1436 real t1,t2;
1437 sb = bi->shortDiff[sfb];
1438 idx = bi->shortIdx[sfb] + lwin;
1439 t1 = tab1[is_p]; t2 = tab2[is_p];
1440 for (; sb > 0; sb--,idx+=3)
1442 real v = xr[0][idx];
1443 xr[0][idx] = v * t1;
1444 xr[1][idx] = v * t2;
1449 #if 1
1450 /* in the original: copy 10 to 11 , here: copy 11 to 12
1451 maybe still wrong??? (copy 12 to 13?) */
1452 is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1453 sb = bi->shortDiff[12];
1454 idx = bi->shortIdx[12] + lwin;
1455 #else
1456 is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1457 sb = bi->shortDiff[11];
1458 idx = bi->shortIdx[11] + lwin;
1459 #endif
1460 if(is_p != 7)
1462 real t1,t2;
1463 t1 = tab1[is_p]; t2 = tab2[is_p];
1464 for ( ; sb > 0; sb--,idx+=3 )
1466 real v = xr[0][idx];
1467 xr[0][idx] = v * t1;
1468 xr[1][idx] = v * t2;
1471 } /* end for(lwin; .. ; . ) */
1473 if (do_l)
1475 /* also check l-part, if ALL bands in the three windows are 'empty'
1476 * and mode = mixed_mode
1478 int sfb = gr_info->maxbandl;
1479 int idx = bi->longIdx[sfb];
1481 for ( ; sfb<8; sfb++ )
1483 int sb = bi->longDiff[sfb];
1484 int is_p = scalefac[sfb]; /* scale: 0-15 */
1485 if(is_p != 7) {
1486 real t1,t2;
1487 t1 = tab1[is_p]; t2 = tab2[is_p];
1488 for ( ; sb > 0; sb--,idx++)
1490 real v = xr[0][idx];
1491 xr[0][idx] = v * t1;
1492 xr[1][idx] = v * t2;
1495 else
1496 idx += sb;
1500 else /* ((gr_info->block_type != 2)) */
1502 int sfb = gr_info->maxbandl;
1503 int is_p,idx = bi->longIdx[sfb];
1504 for ( ; sfb<21; sfb++)
1506 int sb = bi->longDiff[sfb];
1507 is_p = scalefac[sfb]; /* scale: 0-15 */
1508 if(is_p != 7) {
1509 real t1,t2;
1510 t1 = tab1[is_p]; t2 = tab2[is_p];
1511 for ( ; sb > 0; sb--,idx++)
1513 real v = xr[0][idx];
1514 xr[0][idx] = v * t1;
1515 xr[1][idx] = v * t2;
1518 else
1519 idx += sb;
1522 is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1523 if(is_p != 7)
1525 int sb;
1526 real t1 = tab1[is_p],t2 = tab2[is_p];
1528 for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1530 real v = xr[0][idx];
1531 xr[0][idx] = v * t1;
1532 xr[1][idx] = v * t2;
1535 } /* ... */
1538 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1540 int sblim;
1542 if(gr_info->block_type == 2)
1544 if(!gr_info->mixed_block_flag)
1545 return;
1546 sblim = 1;
1548 else {
1549 sblim = gr_info->maxb-1;
1552 /* 31 alias-reduction operations between each pair of sub-bands */
1553 /* with 8 butterflies between each pair */
1556 int sb;
1557 real *xr1=(real *) xr[1];
1559 for(sb=sblim;sb;sb--,xr1+=10)
1561 int ss;
1562 real *cs=aa_cs,*ca=aa_ca;
1563 real *xr2 = xr1;
1565 for(ss=7;ss>=0;ss--)
1566 { /* upper and lower butterfly inputs */
1567 register real bu = *--xr2,bd = *xr1;
1568 *xr2 = (bu * (*cs) ) - (bd * (*ca) );
1569 *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
1576 DCT inspired by Jeff Tsay's DCT from the maplay package
1577 this is an optimized version with manual unroll.
1579 References:
1580 [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1581 Mathematics of Computation, Volume 32, Number 141, January 1978,
1582 Pages 175-199
1585 static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1588 register real *in = inbuf;
1590 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1591 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1592 in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8];
1593 in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5];
1594 in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2];
1595 in[2] +=in[1]; in[1] +=in[0];
1597 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1598 in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1];
1603 #define MACRO0(v) { \
1604 real tmp; \
1605 out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1606 out2[8-(v)] = tmp * w[26-(v)]; } \
1607 sum0 -= sum1; \
1608 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1609 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)];
1610 #define MACRO1(v) { \
1611 real sum0,sum1; \
1612 sum0 = tmp1a + tmp2a; \
1613 sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1614 MACRO0(v); }
1615 #define MACRO2(v) { \
1616 real sum0,sum1; \
1617 sum0 = tmp2a - tmp1a; \
1618 sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1619 MACRO0(v); }
1621 register const real *c = COS9;
1622 register real *out2 = o2;
1623 register real *w = wintab;
1624 register real *out1 = o1;
1625 register real *ts = tsbuf;
1627 real ta33,ta66,tb33,tb66;
1629 ta33 = in[2*3+0] * c[3];
1630 ta66 = in[2*6+0] * c[6];
1631 tb33 = in[2*3+1] * c[3];
1632 tb66 = in[2*6+1] * c[6];
1635 real tmp1a,tmp2a,tmp1b,tmp2b;
1636 tmp1a = in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1637 tmp1b = in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1638 tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1639 tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1641 MACRO1(0);
1642 MACRO2(8);
1646 real tmp1a,tmp2a,tmp1b,tmp2b;
1647 tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1648 tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1649 tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1650 tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1652 MACRO1(1);
1653 MACRO2(7);
1657 real tmp1a,tmp2a,tmp1b,tmp2b;
1658 tmp1a = in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1659 tmp1b = in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1660 tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1661 tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1663 MACRO1(2);
1664 MACRO2(6);
1668 real tmp1a,tmp2a,tmp1b,tmp2b;
1669 tmp1a = in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1670 tmp1b = in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1671 tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1672 tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1674 MACRO1(3);
1675 MACRO2(5);
1679 real sum0,sum1;
1680 sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1681 sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1682 MACRO0(4);
1690 * new DCT12
1692 static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1694 #define DCT12_PART1 \
1695 in5 = in[5*3]; \
1696 in5 += (in4 = in[4*3]); \
1697 in4 += (in3 = in[3*3]); \
1698 in3 += (in2 = in[2*3]); \
1699 in2 += (in1 = in[1*3]); \
1700 in1 += (in0 = in[0*3]); \
1702 in5 += in3; in3 += in1; \
1704 in2 *= COS6_1; \
1705 in3 *= COS6_1; \
1707 #define DCT12_PART2 \
1708 in0 += in4 * COS6_2; \
1710 in4 = in0 + in2; \
1711 in0 -= in2; \
1713 in1 += in5 * COS6_2; \
1715 in5 = (in1 + in3) * tfcos12[0]; \
1716 in1 = (in1 - in3) * tfcos12[2]; \
1718 in3 = in4 + in5; \
1719 in4 -= in5; \
1721 in2 = in0 + in1; \
1722 in0 -= in1;
1726 real in0,in1,in2,in3,in4,in5;
1727 register real *out1 = rawout1;
1728 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1729 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1731 DCT12_PART1
1734 real tmp0,tmp1 = (in0 - in4);
1736 real tmp2 = (in1 - in5) * tfcos12[1];
1737 tmp0 = tmp1 + tmp2;
1738 tmp1 -= tmp2;
1740 ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1741 ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1742 ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1743 ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1746 DCT12_PART2
1748 ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1749 ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1750 ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1751 ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1753 ts[(6+0)*SBLIMIT] = out1[6+0] + in0 * wi[0];
1754 ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1755 ts[(6+2)*SBLIMIT] = out1[6+2] + in4 * wi[2];
1756 ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1759 in++;
1762 real in0,in1,in2,in3,in4,in5;
1763 register real *out2 = rawout2;
1765 DCT12_PART1
1768 real tmp0,tmp1 = (in0 - in4);
1770 real tmp2 = (in1 - in5) * tfcos12[1];
1771 tmp0 = tmp1 + tmp2;
1772 tmp1 -= tmp2;
1774 out2[5-1] = tmp0 * wi[11-1];
1775 out2[0+1] = tmp0 * wi[6+1];
1776 ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1777 ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1780 DCT12_PART2
1782 out2[5-0] = in2 * wi[11-0];
1783 out2[0+0] = in2 * wi[6+0];
1784 out2[0+2] = in3 * wi[6+2];
1785 out2[5-2] = in3 * wi[11-2];
1787 ts[(12+0)*SBLIMIT] += in0 * wi[0];
1788 ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1789 ts[(12+2)*SBLIMIT] += in4 * wi[2];
1790 ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1793 in++;
1796 real in0,in1,in2,in3,in4,in5;
1797 register real *out2 = rawout2;
1798 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1800 DCT12_PART1
1803 real tmp0,tmp1 = (in0 - in4);
1805 real tmp2 = (in1 - in5) * tfcos12[1];
1806 tmp0 = tmp1 + tmp2;
1807 tmp1 -= tmp2;
1809 out2[11-1] = tmp0 * wi[11-1];
1810 out2[6 +1] = tmp0 * wi[6+1];
1811 out2[0+1] += tmp1 * wi[1];
1812 out2[5-1] += tmp1 * wi[5-1];
1815 DCT12_PART2
1817 out2[11-0] = in2 * wi[11-0];
1818 out2[6 +0] = in2 * wi[6+0];
1819 out2[6 +2] = in3 * wi[6+2];
1820 out2[11-2] = in3 * wi[11-2];
1822 out2[0+0] += in0 * wi[0];
1823 out2[5-0] += in0 * wi[5-0];
1824 out2[0+2] += in4 * wi[2];
1825 out2[5-2] += in4 * wi[5-2];
1830 * III_hybrid
1832 static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1833 int ch,struct gr_info_s *gr_info,struct mpstr *mp)
1835 real *tspnt = (real *) tsOut;
1836 real (*block)[2][SBLIMIT*SSLIMIT] = mp->hybrid_block;
1837 int *blc = mp->hybrid_blc;
1838 real *rawout1,*rawout2;
1839 int bt;
1840 int sb = 0;
1843 int b = blc[ch];
1844 rawout1=block[b][ch];
1845 b=-b+1;
1846 rawout2=block[b][ch];
1847 blc[ch] = b;
1851 if(gr_info->mixed_block_flag) {
1852 sb = 2;
1853 dct36(fsIn[0],rawout1,rawout2,win[0],tspnt);
1854 dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1855 rawout1 += 36; rawout2 += 36; tspnt += 2;
1858 bt = gr_info->block_type;
1859 if(bt == 2) {
1860 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1861 dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt);
1862 dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1865 else {
1866 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1867 dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1868 dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1872 for(;sb<SBLIMIT;sb++,tspnt++) {
1873 int i;
1874 for(i=0;i<SSLIMIT;i++) {
1875 tspnt[i*SBLIMIT] = *rawout1++;
1876 *rawout2++ = 0.0;
1882 * main layer3 handler
1884 int do_layer3(struct frame *fr,unsigned char *pcm_sample,int *pcm_point)
1886 int gr, ch, ss,clip=0;
1887 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1888 struct III_sideinfo sideinfo;
1889 int stereo = fr->stereo;
1890 int single = fr->single;
1891 int ms_stereo,i_stereo;
1892 int sfreq = fr->sampling_frequency;
1893 int stereo1,granules;
1895 if(stereo == 1) { /* stream is mono */
1896 stereo1 = 1;
1897 single = 0;
1899 else if(single >= 0) /* stream is stereo, but force to mono */
1900 stereo1 = 1;
1901 else
1902 stereo1 = 2;
1904 if(fr->mode == MPG_MD_JOINT_STEREO) {
1905 ms_stereo = fr->mode_ext & 0x2;
1906 i_stereo = fr->mode_ext & 0x1;
1908 else
1909 ms_stereo = i_stereo = 0;
1911 if(fr->lsf) {
1912 granules = 1;
1913 if(!III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single))
1914 return -1;
1916 else {
1917 granules = 2;
1918 #ifdef MPEG1
1919 if(!III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single))
1920 return -1;
1921 #else
1922 FIXME("Not supported\n");
1923 #endif
1926 if(set_pointer(fr->mp,sideinfo.main_data_begin) == MP3_ERR)
1927 return -1;
1929 for (gr=0;gr<granules;gr++)
1931 real hybridIn[2][SBLIMIT][SSLIMIT];
1932 real hybridOut[2][SSLIMIT][SBLIMIT];
1935 struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
1936 long part2bits;
1937 if(fr->lsf)
1938 part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0);
1939 else {
1940 #ifdef MPEG1
1941 part2bits = III_get_scale_factors_1(scalefacs[0],gr_info);
1942 #else
1943 FIXME("Not supported\n");
1944 #endif
1946 if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
1947 return clip;
1949 if(stereo == 2) {
1950 struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
1951 long part2bits;
1952 if(fr->lsf)
1953 part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo);
1954 else {
1955 #ifdef MPEG1
1956 part2bits = III_get_scale_factors_1(scalefacs[1],gr_info);
1957 #else
1958 FIXME("Not supported\n");
1959 #endif
1962 if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
1963 return clip;
1965 if(ms_stereo) {
1966 int i;
1967 for(i=0;i<SBLIMIT*SSLIMIT;i++) {
1968 real tmp0,tmp1;
1969 tmp0 = ((real *) hybridIn[0])[i];
1970 tmp1 = ((real *) hybridIn[1])[i];
1971 ((real *) hybridIn[0])[i] = tmp0 + tmp1;
1972 ((real *) hybridIn[1])[i] = tmp0 - tmp1;
1976 if(i_stereo)
1977 III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
1979 if(ms_stereo || i_stereo || (single == 3) ) {
1980 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
1981 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
1982 else
1983 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
1986 switch(single) {
1987 case 3:
1989 register int i;
1990 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1991 for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++)
1992 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1994 break;
1995 case 1:
1997 register int i;
1998 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1999 for(i=0;i<SSLIMIT*gr_info->maxb;i++)
2000 *in0++ = *in1++;
2002 break;
2006 for(ch=0;ch<stereo1;ch++) {
2007 struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
2008 III_antialias(hybridIn[ch],gr_info);
2009 III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr->mp);
2012 for(ss=0;ss<SSLIMIT;ss++) {
2013 if(single >= 0) {
2014 clip += synth_1to1_mono(fr->mp,hybridOut[0][ss],pcm_sample,pcm_point);
2016 else {
2017 int p1 = *pcm_point;
2018 clip += synth_1to1(fr->mp,hybridOut[0][ss],0,pcm_sample,&p1);
2019 clip += synth_1to1(fr->mp,hybridOut[1][ss],1,pcm_sample,pcm_point);
2024 return clip;