1 /*
2 * Mpeg Layer-3 audio decoder
3 * --------------------------
4 * copyright (c) 1995,1996,1997 by Michael Hipp.
5 * All rights reserved. See also 'README'
6 */
7
8 #include <stdlib.h>
9 #include "mpg123.h"
10 #include "mpglib.h"
11 #include "huffman.h"
12
13 #define MPEG1
14
15
16 static real ispow[8207];
17 static real aa_ca[8],aa_cs[8];
18 static real COS1[12][6];
19 static real win[4][36];
20 static real win1[4][36];
21 static real gainpow2[256+118+4];
22 static real COS9[9];
23 static real COS6_1,COS6_2;
24 static real tfcos36[9];
25 static real tfcos12[3];
26
27 struct bandInfoStruct {
28 short longIdx[23];
29 short longDiff[22];
30 short shortIdx[14];
31 short shortDiff[13];
32 };
33
34 int longLimit[9][23];
35 int shortLimit[9][14];
36
37 struct bandInfoStruct bandInfo[9] = {
38
39 /* MPEG 1.0 */
40 { {0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
41 {4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
42 {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},
43 {4,4,4,4,6,8,10,12,14,18,22,30,56} } ,
44
45 { {0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
46 {4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
47 {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},
48 {4,4,4,4,6,6,10,12,14,16,20,26,66} } ,
49
50 { {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576} ,
51 {4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26} ,
52 {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} ,
53 {4,4,4,4,6,8,12,16,20,26,34,42,12} } ,
54
55 /* MPEG 2.0 */
56 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
57 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
58 {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} ,
59 {4,4,4,6,6,8,10,14,18,26,32,42,18 } } ,
60 /* docs: 332. mpg123: 330 */
61 { {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576},
62 {6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,52,64,70,76,36 } ,
63 {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} ,
64 {4,4,4,6,8,10,12,14,18,24,32,44,12 } } ,
65
66 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
67 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
68 {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},
69 {4,4,4,6,8,10,12,14,18,24,30,40,18 } } ,
70 /* MPEG 2.5 */
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,12,24,36,54,78,108,144,186,240,312,402,522,576},
74 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
75 { {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576} ,
76 {6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
77 {0,12,24,36,54,78,108,144,186,240,312,402,522,576},
78 {4,4,4,6,8,10,12,14,18,24,30,40,18} },
79 { {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
80 {12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
81 {0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
82 {8,8,8,12,16,20,24,28,36,2,2,2,26} } ,
83 };
84
85 static int mapbuf0[9][152];
86 static int mapbuf1[9][156];
87 static int mapbuf2[9][44];
88 static int *map[9][3];
89 static int *mapend[9][3];
90
91 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
92 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
93
94 static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
95 static real pow1_1[2][16],pow2_1[2][16],pow1_2[2][16],pow2_2[2][16];
96
get1bit(void)97 static unsigned int get1bit(void)
98 {
99 unsigned char rval;
100 rval = *wordpointer << bitindex;
101
102 bitindex++;
103 wordpointer += (bitindex>>3);
104 bitindex &= 7;
105
106 return rval>>7;
107 }
108
109
110
111
112 /*
113 * init tables for layer-3
114 */
init_layer3(int down_sample_sblimit)115 void init_layer3(int down_sample_sblimit)
116 {
117 int i,j,k,l;
118
119 for(i=-256;i<118+4;i++)
120 gainpow2[i+256] = pow((double)2.0,-0.25 * (double) (i+210) );
121
122 for(i=0;i<8207;i++)
123 ispow[i] = pow((double)i,(double)4.0/3.0);
124
125 for (i=0;i<8;i++)
126 {
127 static 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;
131 }
132
133 for(i=0;i<18;i++)
134 {
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 );
137 }
138 for(i=0;i<6;i++)
139 {
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 );
145 }
146
147 for(i=0;i<9;i++)
148 COS9[i] = cos( M_PI / 18.0 * (double) i);
149
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 );
154
155 COS6_1 = cos( M_PI / 6.0 * (double) 1);
156 COS6_2 = cos( M_PI / 6.0 * (double) 2);
157
158 for(i=0;i<12;i++)
159 {
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)) );
163 }
164
165 for(j=0;j<4;j++) {
166 static 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];
171 }
172
173 for(i=0;i<16;i++)
174 {
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);
180
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);
189 }
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;
194 }
195 }
196
197 for(j=0;j<9;j++)
198 {
199 struct bandInfoStruct *bi = &bandInfo[j];
200 int *mp;
201 int cb,lwin;
202 short *bdf;
203
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;
211 }
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;
220 }
221 i += 6*l;
222 }
223 mapend[j][0] = mp;
224
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;
234 }
235 i += 6*l;
236 }
237 mapend[j][1] = mp;
238
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;
244 }
245 mapend[j][2] = mp;
246
247 }
248
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;
254 }
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;
259 }
260 }
261
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);
267 }
268 }
269 }
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);
275 }
276 }
277 }
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);
283 }
284 }
285
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);
292 }
293 }
294 }
295 }
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);
301 }
302 }
303 }
304 }
305
306 /*
307 * read additional side information
308 */
309 #ifdef MPEG1
III_get_side_info_1(struct III_sideinfo * si,int stereo,int ms_stereo,long sfreq,int single)310 static int III_get_side_info_1(struct III_sideinfo *si,int stereo,
311 int ms_stereo,long sfreq,int single)
312 {
313 int ch, gr;
314 int powdiff = (single == 3) ? 4 : 0;
315
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);
321
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);
325 }
326
327 for (gr=0; gr<2; gr++)
328 {
329 for (ch=0; ch<stereo; ch++)
330 {
331 register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
332
333 gr_info->part2_3_length = getbits(12);
334 gr_info->big_values = getbits_fast(9);
335 if(gr_info->big_values > 288) {
336 gr_info->big_values = 288;
337 }
338 {
339 unsigned int qss = getbits_fast(8);
340 gr_info->pow2gain = gainpow2+256 - qss + powdiff;
341 }
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())
347 {
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);
353
354
355 /*
356 * table_select[2] not needed, because there is no region2,
357 * but to satisfy some verifications tools we set it either.
358 */
359 gr_info->table_select[2] = 0;
360 for(i=0;i<3;i++) {
361 unsigned int sbg = (getbits_fast(3)<<3);
362 gr_info->full_gain[i] = gr_info->pow2gain + sbg;
363 }
364
365
366 if(gr_info->block_type == 0) {
367 return -1;
368 }
369 /* region_count/start parameters are implicit in this case. */
370 gr_info->region1start = 36>>1;
371 gr_info->region2start = 576>>1;
372 }
373 else
374 {
375 int i,r0c,r1c;
376 for (i=0; i<3; i++)
377 gr_info->table_select[i] = getbits_fast(5);
378 r0c = getbits_fast(4);
379 r1c = getbits_fast(3);
380 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
381 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
382 gr_info->block_type = 0;
383 gr_info->mixed_block_flag = 0;
384 }
385 gr_info->preflag = get1bit();
386 gr_info->scalefac_scale = get1bit();
387 gr_info->count1table_select = get1bit();
388 }
389 }
390 return 0;
391 }
392 #endif
393
394 /*
395 * Side Info for MPEG 2.0 / LSF
396 */
III_get_side_info_2(struct III_sideinfo * si,int stereo,int ms_stereo,long sfreq,int single)397 static int III_get_side_info_2(struct III_sideinfo *si,int stereo,
398 int ms_stereo,long sfreq,int single)
399 {
400 int ch;
401 int powdiff = (single == 3) ? 4 : 0;
402
403 si->main_data_begin = getbits(8);
404 if (stereo == 1)
405 si->private_bits = get1bit();
406 else
407 si->private_bits = getbits_fast(2);
408
409 for (ch=0; ch<stereo; ch++)
410 {
411 register struct gr_info_s *gr_info = &(si->ch[ch].gr[0]);
412 unsigned int qss;
413
414 gr_info->part2_3_length = getbits(12);
415 gr_info->big_values = getbits_fast(9);
416 if(gr_info->big_values > 288) {
417 gr_info->big_values = 288;
418 }
419 qss=getbits_fast(8);
420 gr_info->pow2gain = gainpow2+256 - qss + powdiff;
421
422 if(ms_stereo)
423 gr_info->pow2gain += 2;
424 gr_info->scalefac_compress = getbits(9);
425 /* window-switching flag == 1 for block_Type != 0 .. and block-type == 0 -> win-sw-flag = 0 */
426 if(get1bit())
427 {
428 int i;
429 gr_info->block_type = getbits_fast(2);
430 gr_info->mixed_block_flag = get1bit();
431 gr_info->table_select[0] = getbits_fast(5);
432 gr_info->table_select[1] = getbits_fast(5);
433 /*
434 * table_select[2] not needed, because there is no region2,
435 * but to satisfy some verifications tools we set it either.
436 */
437 gr_info->table_select[2] = 0;
438 for(i=0;i<3;i++) {
439 unsigned int sbg = (getbits_fast(3)<<3);
440 gr_info->full_gain[i] = gr_info->pow2gain + sbg;
441 }
442
443 if(gr_info->block_type == 0) {
444 return -1;
445 }
446 /* region_count/start parameters are implicit in this case. */
447 /* check this again! */
448 if(gr_info->block_type == 2)
449 gr_info->region1start = 36>>1;
450 else if(sfreq == 8)
451 /* check this for 2.5 and sfreq=8 */
452 gr_info->region1start = 108>>1;
453 else
454 gr_info->region1start = 54>>1;
455 gr_info->region2start = 576>>1;
456 }
457 else
458 {
459 int i,r0c,r1c;
460 for (i=0; i<3; i++)
461 gr_info->table_select[i] = getbits_fast(5);
462 r0c = getbits_fast(4);
463 r1c = getbits_fast(3);
464 gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
465 gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
466 gr_info->block_type = 0;
467 gr_info->mixed_block_flag = 0;
468 }
469 gr_info->scalefac_scale = get1bit();
470 gr_info->count1table_select = get1bit();
471 }
472 return 0;
473 }
474
475 /*
476 * read scalefactors
477 */
478 #ifdef MPEG1
III_get_scale_factors_1(int * scf,struct gr_info_s * gr_info)479 static int III_get_scale_factors_1(int *scf,struct gr_info_s *gr_info)
480 {
481 static unsigned char slen[2][16] = {
482 {0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
483 {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
484 };
485 int numbits;
486 int num0 = slen[0][gr_info->scalefac_compress];
487 int num1 = slen[1][gr_info->scalefac_compress];
488
489 if (gr_info->block_type == 2)
490 {
491 int i=18;
492 numbits = (num0 + num1) * 18;
493
494 if (gr_info->mixed_block_flag) {
495 for (i=8;i;i--)
496 *scf++ = getbits_fast(num0);
497 i = 9;
498 numbits -= num0; /* num0 * 17 + num1 * 18 */
499 }
500
501 for (;i;i--)
502 *scf++ = getbits_fast(num0);
503 for (i = 18; i; i--)
504 *scf++ = getbits_fast(num1);
505 *scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
506 }
507 else
508 {
509 int i;
510 int scfsi = gr_info->scfsi;
511
512 if(scfsi < 0) { /* scfsi < 0 => granule == 0 */
513 for(i=11;i;i--)
514 *scf++ = getbits_fast(num0);
515 for(i=10;i;i--)
516 *scf++ = getbits_fast(num1);
517 numbits = (num0 + num1) * 10 + num0;
518 }
519 else {
520 numbits = 0;
521 if(!(scfsi & 0x8)) {
522 for (i=6;i;i--)
523 *scf++ = getbits_fast(num0);
524 numbits += num0 * 6;
525 }
526 else {
527 scf += 6;
528 }
529
530 if(!(scfsi & 0x4)) {
531 for (i=5;i;i--)
532 *scf++ = getbits_fast(num0);
533 numbits += num0 * 5;
534 }
535 else {
536 scf += 5;
537 }
538
539 if(!(scfsi & 0x2)) {
540 for(i=5;i;i--)
541 *scf++ = getbits_fast(num1);
542 numbits += num1 * 5;
543 }
544 else {
545 scf += 5;
546 }
547
548 if(!(scfsi & 0x1)) {
549 for (i=5;i;i--)
550 *scf++ = getbits_fast(num1);
551 numbits += num1 * 5;
552 }
553 else {
554 scf += 5;
555 }
556 }
557
558 *scf++ = 0; /* no l[21] in original sources */
559 }
560 return numbits;
561 }
562 #endif
563
III_get_scale_factors_2(int * scf,struct gr_info_s * gr_info,int i_stereo)564 static int III_get_scale_factors_2(int *scf,struct gr_info_s *gr_info,int i_stereo)
565 {
566 unsigned char *pnt;
567 int i,j;
568 unsigned int slen;
569 int n = 0;
570 int numbits = 0;
571
572 static unsigned char stab[3][6][4] = {
573 { { 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0} ,
574 { 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0} } ,
575 { { 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0} ,
576 {12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0} } ,
577 { { 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0} ,
578 { 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0} } };
579
580 if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
581 slen = i_slen2[gr_info->scalefac_compress>>1];
582 else
583 slen = n_slen2[gr_info->scalefac_compress];
584
585 gr_info->preflag = (slen>>15) & 0x1;
586
587 n = 0;
588 if( gr_info->block_type == 2 ) {
589 n++;
590 if(gr_info->mixed_block_flag)
591 n++;
592 }
593
594 pnt = stab[n][(slen>>12)&0x7];
595
596 for(i=0;i<4;i++) {
597 int num = slen & 0x7;
598 slen >>= 3;
599 if(num) {
600 for(j=0;j<(int)(pnt[i]);j++)
601 *scf++ = getbits_fast(num);
602 numbits += pnt[i] * num;
603 }
604 else {
605 for(j=0;j<(int)(pnt[i]);j++)
606 *scf++ = 0;
607 }
608 }
609
610 n = (n << 1) + 1;
611 for(i=0;i<n;i++)
612 *scf++ = 0;
613
614 return numbits;
615 }
616
617 static int pretab1[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
618 static int pretab2[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
619
620 /*
621 * don't forget to apply the same changes to III_dequantize_sample_ms() !!!
622 */
III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int * scf,struct gr_info_s * gr_info,int sfreq,int part2bits)623 static int III_dequantize_sample(real xr[SBLIMIT][SSLIMIT],int *scf,
624 struct gr_info_s *gr_info,int sfreq,int part2bits)
625 {
626 int shift = 1 + gr_info->scalefac_scale;
627 real *xrpnt = (real *) xr;
628 int l[3],l3;
629 int part2remain = gr_info->part2_3_length - part2bits;
630 int *me;
631
632 {
633 int bv = gr_info->big_values;
634 int region1 = gr_info->region1start;
635 int region2 = gr_info->region2start;
636
637 l3 = ((576>>1)-bv)>>1;
638 /*
639 * we may lose the 'odd' bit here !!
640 * check this later again
641 */
642 if(bv <= region1) {
643 l[0] = bv; l[1] = 0; l[2] = 0;
644 }
645 else {
646 l[0] = region1;
647 if(bv <= region2) {
648 l[1] = bv - l[0]; l[2] = 0;
649 }
650 else {
651 l[1] = region2 - l[0]; l[2] = bv - region2;
652 }
653 }
654 }
655
656 if(gr_info->block_type == 2) {
657 /*
658 * decoding with short or mixed mode BandIndex table
659 */
660 int i,max[4];
661 int step=0,lwin=0,cb=0;
662 register real v = 0.0;
663 register int *m,mc;
664
665 if(gr_info->mixed_block_flag) {
666 max[3] = -1;
667 max[0] = max[1] = max[2] = 2;
668 m = map[sfreq][0];
669 me = mapend[sfreq][0];
670 }
671 else {
672 max[0] = max[1] = max[2] = max[3] = -1;
673 /* max[3] not really needed in this case */
674 m = map[sfreq][1];
675 me = mapend[sfreq][1];
676 }
677
678 mc = 0;
679 for(i=0;i<2;i++) {
680 int lp = l[i];
681 struct newhuff *h = ht+gr_info->table_select[i];
682 for(;lp;lp--,mc--) {
683 register int x,y;
684 if( (!mc) ) {
685 mc = *m++;
686 xrpnt = ((real *) xr) + (*m++);
687 lwin = *m++;
688 cb = *m++;
689 if(lwin == 3) {
690 v = gr_info->pow2gain[(*scf++) << shift];
691 step = 1;
692 }
693 else {
694 v = gr_info->full_gain[lwin][(*scf++) << shift];
695 step = 3;
696 }
697 }
698 {
699 register short *val = h->table;
700 while((y=*val++)<0) {
701 if (get1bit())
702 val -= y;
703 part2remain--;
704 }
705 x = y >> 4;
706 y &= 0xf;
707 }
708 if(x == 15) {
709 max[lwin] = cb;
710 part2remain -= h->linbits+1;
711 x += getbits(h->linbits);
712 if(get1bit())
713 *xrpnt = -ispow[x] * v;
714 else
715 *xrpnt = ispow[x] * v;
716 }
717 else if(x) {
718 max[lwin] = cb;
719 if(get1bit())
720 *xrpnt = -ispow[x] * v;
721 else
722 *xrpnt = ispow[x] * v;
723 part2remain--;
724 }
725 else
726 *xrpnt = 0.0;
727 xrpnt += step;
728 if(y == 15) {
729 max[lwin] = cb;
730 part2remain -= h->linbits+1;
731 y += getbits(h->linbits);
732 if(get1bit())
733 *xrpnt = -ispow[y] * v;
734 else
735 *xrpnt = ispow[y] * v;
736 }
737 else if(y) {
738 max[lwin] = cb;
739 if(get1bit())
740 *xrpnt = -ispow[y] * v;
741 else
742 *xrpnt = ispow[y] * v;
743 part2remain--;
744 }
745 else
746 *xrpnt = 0.0;
747 xrpnt += step;
748 }
749 }
750 for(;l3 && (part2remain > 0);l3--) {
751 struct newhuff *h = htc+gr_info->count1table_select;
752 register short *val = h->table,a;
753
754 while((a=*val++)<0) {
755 part2remain--;
756 if(part2remain < 0) {
757 part2remain++;
758 a = 0;
759 break;
760 }
761 if (get1bit())
762 val -= a;
763 }
764 for(i=0;i<4;i++) {
765 if(!(i & 1)) {
766 if(!mc) {
767 mc = *m++;
768 xrpnt = ((real *) xr) + (*m++);
769 lwin = *m++;
770 cb = *m++;
771 if(lwin == 3) {
772 v = gr_info->pow2gain[(*scf++) << shift];
773 step = 1;
774 }
775 else {
776 v = gr_info->full_gain[lwin][(*scf++) << shift];
777 step = 3;
778 }
779 }
780 mc--;
781 }
782 if( (a & (0x8>>i)) ) {
783 max[lwin] = cb;
784 part2remain--;
785 if(part2remain < 0) {
786 part2remain++;
787 break;
788 }
789 if(get1bit())
790 *xrpnt = -v;
791 else
792 *xrpnt = v;
793 }
794 else
795 *xrpnt = 0.0;
796 xrpnt += step;
797 }
798 }
799
800 while( m < me ) {
801 if(!mc) {
802 mc = *m++;
803 xrpnt = ((real *) xr) + *m++;
804 if( (*m++) == 3)
805 step = 1;
806 else
807 step = 3;
808 m++; /* cb */
809 }
810 mc--;
811 *xrpnt = 0.0;
812 xrpnt += step;
813 *xrpnt = 0.0;
814 xrpnt += step;
815 /* we could add a little opt. here:
816 * if we finished a band for window 3 or a long band
817 * further bands could copied in a simple loop without a
818 * special 'map' decoding
819 */
820 }
821
822 gr_info->maxband[0] = max[0]+1;
823 gr_info->maxband[1] = max[1]+1;
824 gr_info->maxband[2] = max[2]+1;
825 gr_info->maxbandl = max[3]+1;
826
827 {
828 int rmax = max[0] > max[1] ? max[0] : max[1];
829 rmax = (rmax > max[2] ? rmax : max[2]) + 1;
830 gr_info->maxb = rmax ? shortLimit[sfreq][rmax] : longLimit[sfreq][max[3]+1];
831 }
832
833 }
834 else {
835 /*
836 * decoding with 'long' BandIndex table (block_type != 2)
837 */
838 int *pretab = gr_info->preflag ? pretab1 : pretab2;
839 int i,max = -1;
840 int cb = 0;
841 register int *m = map[sfreq][2];
842 register real v = 0.0;
843 register int mc = 0;
844 #if 0
845 me = mapend[sfreq][2];
846 #endif
847
848 /*
849 * long hash table values
850 */
851 for(i=0;i<3;i++) {
852 int lp = l[i];
853 struct newhuff *h = ht+gr_info->table_select[i];
854
855 for(;lp;lp--,mc--) {
856 int x,y;
857
858 if(!mc) {
859 mc = *m++;
860 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
861 cb = *m++;
862 }
863 {
864 register short *val = h->table;
865 while((y=*val++)<0) {
866 if (get1bit())
867 val -= y;
868 part2remain--;
869 }
870 x = y >> 4;
871 y &= 0xf;
872 }
873 if (x == 15) {
874 max = cb;
875 part2remain -= h->linbits+1;
876 x += getbits(h->linbits);
877 if(get1bit())
878 *xrpnt++ = -ispow[x] * v;
879 else
880 *xrpnt++ = ispow[x] * v;
881 }
882 else if(x) {
883 max = cb;
884 if(get1bit())
885 *xrpnt++ = -ispow[x] * v;
886 else
887 *xrpnt++ = ispow[x] * v;
888 part2remain--;
889 }
890 else
891 *xrpnt++ = 0.0;
892
893 if (y == 15) {
894 max = cb;
895 part2remain -= h->linbits+1;
896 y += getbits(h->linbits);
897 if(get1bit())
898 *xrpnt++ = -ispow[y] * v;
899 else
900 *xrpnt++ = ispow[y] * v;
901 }
902 else if(y) {
903 max = cb;
904 if(get1bit())
905 *xrpnt++ = -ispow[y] * v;
906 else
907 *xrpnt++ = ispow[y] * v;
908 part2remain--;
909 }
910 else
911 *xrpnt++ = 0.0;
912 }
913 }
914
915 /*
916 * short (count1table) values
917 */
918 for(;l3 && (part2remain > 0);l3--) {
919 struct newhuff *h = htc+gr_info->count1table_select;
920 register short *val = h->table,a;
921
922 while((a=*val++)<0) {
923 part2remain--;
924 if(part2remain < 0) {
925 part2remain++;
926 a = 0;
927 break;
928 }
929 if (get1bit())
930 val -= a;
931 }
932 for(i=0;i<4;i++) {
933 if(!(i & 1)) {
934 if(!mc) {
935 mc = *m++;
936 cb = *m++;
937 v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
938 }
939 mc--;
940 }
941 if ( (a & (0x8>>i)) ) {
942 max = cb;
943 part2remain--;
944 if(part2remain < 0) {
945 part2remain++;
946 break;
947 }
948 if(get1bit())
949 *xrpnt++ = -v;
950 else
951 *xrpnt++ = v;
952 }
953 else
954 *xrpnt++ = 0.0;
955 }
956 }
957
958 /*
959 * zero part
960 */
961 for(i=(&xr[SBLIMIT][0]-xrpnt)>>1;i;i--) {
962 *xrpnt++ = 0.0;
963 *xrpnt++ = 0.0;
964 }
965
966 gr_info->maxbandl = max+1;
967 gr_info->maxb = longLimit[sfreq][gr_info->maxbandl];
968 }
969
970 while( part2remain > 16 ) {
971 getbits(16); /* Dismiss stuffing Bits */
972 part2remain -= 16;
973 }
974 if(part2remain > 0)
975 getbits(part2remain);
976 else if(part2remain < 0) {
977 return 1; /* -> error */
978 }
979 return 0;
980 }
981
982
983 /*
984 * III_stereo: calculate real channel values for Joint-I-Stereo-mode
985 */
III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int * scalefac,struct gr_info_s * gr_info,int sfreq,int ms_stereo,int lsf)986 static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac,
987 struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
988 {
989 real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
990 struct bandInfoStruct *bi = &bandInfo[sfreq];
991 real *tab1,*tab2;
992
993 if(lsf) {
994 int p = gr_info->scalefac_compress & 0x1;
995 if(ms_stereo) {
996 tab1 = pow1_2[p]; tab2 = pow2_2[p];
997 }
998 else {
999 tab1 = pow1_1[p]; tab2 = pow2_1[p];
1000 }
1001 }
1002 else {
1003 if(ms_stereo) {
1004 tab1 = tan1_2; tab2 = tan2_2;
1005 }
1006 else {
1007 tab1 = tan1_1; tab2 = tan2_1;
1008 }
1009 }
1010
1011 if (gr_info->block_type == 2)
1012 {
1013 int lwin,do_l = 0;
1014 if( gr_info->mixed_block_flag )
1015 do_l = 1;
1016
1017 for (lwin=0;lwin<3;lwin++) /* process each window */
1018 {
1019 /* get first band with zero values */
1020 int is_p,sb,idx,sfb = gr_info->maxband[lwin]; /* sfb is minimal 3 for mixed mode */
1021 if(sfb > 3)
1022 do_l = 0;
1023
1024 for(;sfb<12;sfb++)
1025 {
1026 is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1027 if(is_p != 7) {
1028 real t1,t2;
1029 sb = bi->shortDiff[sfb];
1030 idx = bi->shortIdx[sfb] + lwin;
1031 t1 = tab1[is_p]; t2 = tab2[is_p];
1032 for (; sb > 0; sb--,idx+=3)
1033 {
1034 real v = xr[0][idx];
1035 xr[0][idx] = v * t1;
1036 xr[1][idx] = v * t2;
1037 }
1038 }
1039 }
1040
1041 #if 1
1042 /* in the original: copy 10 to 11 , here: copy 11 to 12
1043 maybe still wrong??? (copy 12 to 13?) */
1044 is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1045 sb = bi->shortDiff[12];
1046 idx = bi->shortIdx[12] + lwin;
1047 #else
1048 is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1049 sb = bi->shortDiff[11];
1050 idx = bi->shortIdx[11] + lwin;
1051 #endif
1052 if(is_p != 7)
1053 {
1054 real t1,t2;
1055 t1 = tab1[is_p]; t2 = tab2[is_p];
1056 for ( ; sb > 0; sb--,idx+=3 )
1057 {
1058 real v = xr[0][idx];
1059 xr[0][idx] = v * t1;
1060 xr[1][idx] = v * t2;
1061 }
1062 }
1063 } /* end for(lwin; .. ; . ) */
1064
1065 if (do_l)
1066 {
1067 /* also check l-part, if ALL bands in the three windows are 'empty'
1068 * and mode = mixed_mode
1069 */
1070 int sfb = gr_info->maxbandl;
1071 int idx = bi->longIdx[sfb];
1072
1073 for ( ; sfb<8; sfb++ )
1074 {
1075 int sb = bi->longDiff[sfb];
1076 int is_p = scalefac[sfb]; /* scale: 0-15 */
1077 if(is_p != 7) {
1078 real t1,t2;
1079 t1 = tab1[is_p]; t2 = tab2[is_p];
1080 for ( ; sb > 0; sb--,idx++)
1081 {
1082 real v = xr[0][idx];
1083 xr[0][idx] = v * t1;
1084 xr[1][idx] = v * t2;
1085 }
1086 }
1087 else
1088 idx += sb;
1089 }
1090 }
1091 }
1092 else /* ((gr_info->block_type != 2)) */
1093 {
1094 int sfb = gr_info->maxbandl;
1095 int is_p,idx = bi->longIdx[sfb];
1096 for ( ; sfb<21; sfb++)
1097 {
1098 int sb = bi->longDiff[sfb];
1099 is_p = scalefac[sfb]; /* scale: 0-15 */
1100 if(is_p != 7) {
1101 real t1,t2;
1102 t1 = tab1[is_p]; t2 = tab2[is_p];
1103 for ( ; sb > 0; sb--,idx++)
1104 {
1105 real v = xr[0][idx];
1106 xr[0][idx] = v * t1;
1107 xr[1][idx] = v * t2;
1108 }
1109 }
1110 else
1111 idx += sb;
1112 }
1113
1114 is_p = scalefac[20]; /* copy l-band 20 to l-band 21 */
1115 if(is_p != 7)
1116 {
1117 int sb;
1118 real t1 = tab1[is_p],t2 = tab2[is_p];
1119
1120 for ( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1121 {
1122 real v = xr[0][idx];
1123 xr[0][idx] = v * t1;
1124 xr[1][idx] = v * t2;
1125 }
1126 }
1127 } /* ... */
1128 }
1129
III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s * gr_info)1130 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1131 {
1132 int sblim;
1133
1134 if(gr_info->block_type == 2)
1135 {
1136 if(!gr_info->mixed_block_flag)
1137 return;
1138 sblim = 1;
1139 }
1140 else {
1141 sblim = gr_info->maxb-1;
1142 }
1143
1144 /* 31 alias-reduction operations between each pair of sub-bands */
1145 /* with 8 butterflies between each pair */
1146
1147 {
1148 int sb;
1149 real *xr1=(real *) xr[1];
1150
1151 for(sb=sblim;sb;sb--,xr1+=10)
1152 {
1153 int ss;
1154 real *cs=aa_cs,*ca=aa_ca;
1155 real *xr2 = xr1;
1156
1157 for(ss=7;ss>=0;ss--)
1158 { /* upper and lower butterfly inputs */
1159 register real bu = *--xr2,bd = *xr1;
1160 *xr2 = (bu * (*cs) ) - (bd * (*ca) );
1161 *xr1++ = (bd * (*cs++) ) + (bu * (*ca++) );
1162 }
1163 }
1164 }
1165 }
1166
1167 /*
1168 DCT insipired by Jeff Tsay's DCT from the maplay package
1169 this is an optimized version with manual unroll.
1170
1171 References:
1172 [1] S. Winograd: "On Computing the Discrete Fourier Transform",
1173 Mathematics of Computation, Volume 32, Number 141, January 1978,
1174 Pages 175-199
1175 */
1176
dct36(real * inbuf,real * o1,real * o2,real * wintab,real * tsbuf)1177 static void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1178 {
1179 {
1180 register real *in = inbuf;
1181
1182 in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1183 in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1184 in[11]+=in[10]; in[10]+=in[9]; in[9] +=in[8];
1185 in[8] +=in[7]; in[7] +=in[6]; in[6] +=in[5];
1186 in[5] +=in[4]; in[4] +=in[3]; in[3] +=in[2];
1187 in[2] +=in[1]; in[1] +=in[0];
1188
1189 in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1190 in[9] +=in[7]; in[7] +=in[5]; in[5] +=in[3]; in[3] +=in[1];
1191
1192
1193 {
1194
1195 #define MACRO0(v) { \
1196 real tmp; \
1197 out2[9+(v)] = (tmp = sum0 + sum1) * w[27+(v)]; \
1198 out2[8-(v)] = tmp * w[26-(v)]; } \
1199 sum0 -= sum1; \
1200 ts[SBLIMIT*(8-(v))] = out1[8-(v)] + sum0 * w[8-(v)]; \
1201 ts[SBLIMIT*(9+(v))] = out1[9+(v)] + sum0 * w[9+(v)];
1202 #define MACRO1(v) { \
1203 real sum0,sum1; \
1204 sum0 = tmp1a + tmp2a; \
1205 sum1 = (tmp1b + tmp2b) * tfcos36[(v)]; \
1206 MACRO0(v); }
1207 #define MACRO2(v) { \
1208 real sum0,sum1; \
1209 sum0 = tmp2a - tmp1a; \
1210 sum1 = (tmp2b - tmp1b) * tfcos36[(v)]; \
1211 MACRO0(v); }
1212
1213 register const real *c = COS9;
1214 register real *out2 = o2;
1215 register real *w = wintab;
1216 register real *out1 = o1;
1217 register real *ts = tsbuf;
1218
1219 real ta33,ta66,tb33,tb66;
1220
1221 ta33 = in[2*3+0] * c[3];
1222 ta66 = in[2*6+0] * c[6];
1223 tb33 = in[2*3+1] * c[3];
1224 tb66 = in[2*6+1] * c[6];
1225
1226 {
1227 real tmp1a,tmp2a,tmp1b,tmp2b;
1228 tmp1a = in[2*1+0] * c[1] + ta33 + in[2*5+0] * c[5] + in[2*7+0] * c[7];
1229 tmp1b = in[2*1+1] * c[1] + tb33 + in[2*5+1] * c[5] + in[2*7+1] * c[7];
1230 tmp2a = in[2*0+0] + in[2*2+0] * c[2] + in[2*4+0] * c[4] + ta66 + in[2*8+0] * c[8];
1231 tmp2b = in[2*0+1] + in[2*2+1] * c[2] + in[2*4+1] * c[4] + tb66 + in[2*8+1] * c[8];
1232
1233 MACRO1(0);
1234 MACRO2(8);
1235 }
1236
1237 {
1238 real tmp1a,tmp2a,tmp1b,tmp2b;
1239 tmp1a = ( in[2*1+0] - in[2*5+0] - in[2*7+0] ) * c[3];
1240 tmp1b = ( in[2*1+1] - in[2*5+1] - in[2*7+1] ) * c[3];
1241 tmp2a = ( in[2*2+0] - in[2*4+0] - in[2*8+0] ) * c[6] - in[2*6+0] + in[2*0+0];
1242 tmp2b = ( in[2*2+1] - in[2*4+1] - in[2*8+1] ) * c[6] - in[2*6+1] + in[2*0+1];
1243
1244 MACRO1(1);
1245 MACRO2(7);
1246 }
1247
1248 {
1249 real tmp1a,tmp2a,tmp1b,tmp2b;
1250 tmp1a = in[2*1+0] * c[5] - ta33 - in[2*5+0] * c[7] + in[2*7+0] * c[1];
1251 tmp1b = in[2*1+1] * c[5] - tb33 - in[2*5+1] * c[7] + in[2*7+1] * c[1];
1252 tmp2a = in[2*0+0] - in[2*2+0] * c[8] - in[2*4+0] * c[2] + ta66 + in[2*8+0] * c[4];
1253 tmp2b = in[2*0+1] - in[2*2+1] * c[8] - in[2*4+1] * c[2] + tb66 + in[2*8+1] * c[4];
1254
1255 MACRO1(2);
1256 MACRO2(6);
1257 }
1258
1259 {
1260 real tmp1a,tmp2a,tmp1b,tmp2b;
1261 tmp1a = in[2*1+0] * c[7] - ta33 + in[2*5+0] * c[1] - in[2*7+0] * c[5];
1262 tmp1b = in[2*1+1] * c[7] - tb33 + in[2*5+1] * c[1] - in[2*7+1] * c[5];
1263 tmp2a = in[2*0+0] - in[2*2+0] * c[4] + in[2*4+0] * c[8] + ta66 - in[2*8+0] * c[2];
1264 tmp2b = in[2*0+1] - in[2*2+1] * c[4] + in[2*4+1] * c[8] + tb66 - in[2*8+1] * c[2];
1265
1266 MACRO1(3);
1267 MACRO2(5);
1268 }
1269
1270 {
1271 real sum0,sum1;
1272 sum0 = in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1273 sum1 = (in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ) * tfcos36[4];
1274 MACRO0(4);
1275 }
1276 }
1277
1278 }
1279 }
1280
1281 /*
1282 * new DCT12
1283 */
dct12(real * in,real * rawout1,real * rawout2,register real * wi,register real * ts)1284 static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1285 {
1286 #define DCT12_PART1 \
1287 in5 = in[5*3]; \
1288 in5 += (in4 = in[4*3]); \
1289 in4 += (in3 = in[3*3]); \
1290 in3 += (in2 = in[2*3]); \
1291 in2 += (in1 = in[1*3]); \
1292 in1 += (in0 = in[0*3]); \
1293 \
1294 in5 += in3; in3 += in1; \
1295 \
1296 in2 *= COS6_1; \
1297 in3 *= COS6_1; \
1298
1299 #define DCT12_PART2 \
1300 in0 += in4 * COS6_2; \
1301 \
1302 in4 = in0 + in2; \
1303 in0 -= in2; \
1304 \
1305 in1 += in5 * COS6_2; \
1306 \
1307 in5 = (in1 + in3) * tfcos12[0]; \
1308 in1 = (in1 - in3) * tfcos12[2]; \
1309 \
1310 in3 = in4 + in5; \
1311 in4 -= in5; \
1312 \
1313 in2 = in0 + in1; \
1314 in0 -= in1;
1315
1316
1317 {
1318 real in0,in1,in2,in3,in4,in5;
1319 register real *out1 = rawout1;
1320 ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1321 ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1322
1323 DCT12_PART1
1324
1325 {
1326 real tmp0,tmp1 = (in0 - in4);
1327 {
1328 real tmp2 = (in1 - in5) * tfcos12[1];
1329 tmp0 = tmp1 + tmp2;
1330 tmp1 -= tmp2;
1331 }
1332 ts[(17-1)*SBLIMIT] = out1[17-1] + tmp0 * wi[11-1];
1333 ts[(12+1)*SBLIMIT] = out1[12+1] + tmp0 * wi[6+1];
1334 ts[(6 +1)*SBLIMIT] = out1[6 +1] + tmp1 * wi[1];
1335 ts[(11-1)*SBLIMIT] = out1[11-1] + tmp1 * wi[5-1];
1336 }
1337
1338 DCT12_PART2
1339
1340 ts[(17-0)*SBLIMIT] = out1[17-0] + in2 * wi[11-0];
1341 ts[(12+0)*SBLIMIT] = out1[12+0] + in2 * wi[6+0];
1342 ts[(12+2)*SBLIMIT] = out1[12+2] + in3 * wi[6+2];
1343 ts[(17-2)*SBLIMIT] = out1[17-2] + in3 * wi[11-2];
1344
1345 ts[(6+0)*SBLIMIT] = out1[6+0] + in0 * wi[0];
1346 ts[(11-0)*SBLIMIT] = out1[11-0] + in0 * wi[5-0];
1347 ts[(6+2)*SBLIMIT] = out1[6+2] + in4 * wi[2];
1348 ts[(11-2)*SBLIMIT] = out1[11-2] + in4 * wi[5-2];
1349 }
1350
1351 in++;
1352
1353 {
1354 real in0,in1,in2,in3,in4,in5;
1355 register real *out2 = rawout2;
1356
1357 DCT12_PART1
1358
1359 {
1360 real tmp0,tmp1 = (in0 - in4);
1361 {
1362 real tmp2 = (in1 - in5) * tfcos12[1];
1363 tmp0 = tmp1 + tmp2;
1364 tmp1 -= tmp2;
1365 }
1366 out2[5-1] = tmp0 * wi[11-1];
1367 out2[0+1] = tmp0 * wi[6+1];
1368 ts[(12+1)*SBLIMIT] += tmp1 * wi[1];
1369 ts[(17-1)*SBLIMIT] += tmp1 * wi[5-1];
1370 }
1371
1372 DCT12_PART2
1373
1374 out2[5-0] = in2 * wi[11-0];
1375 out2[0+0] = in2 * wi[6+0];
1376 out2[0+2] = in3 * wi[6+2];
1377 out2[5-2] = in3 * wi[11-2];
1378
1379 ts[(12+0)*SBLIMIT] += in0 * wi[0];
1380 ts[(17-0)*SBLIMIT] += in0 * wi[5-0];
1381 ts[(12+2)*SBLIMIT] += in4 * wi[2];
1382 ts[(17-2)*SBLIMIT] += in4 * wi[5-2];
1383 }
1384
1385 in++;
1386
1387 {
1388 real in0,in1,in2,in3,in4,in5;
1389 register real *out2 = rawout2;
1390 out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1391
1392 DCT12_PART1
1393
1394 {
1395 real tmp0,tmp1 = (in0 - in4);
1396 {
1397 real tmp2 = (in1 - in5) * tfcos12[1];
1398 tmp0 = tmp1 + tmp2;
1399 tmp1 -= tmp2;
1400 }
1401 out2[11-1] = tmp0 * wi[11-1];
1402 out2[6 +1] = tmp0 * wi[6+1];
1403 out2[0+1] += tmp1 * wi[1];
1404 out2[5-1] += tmp1 * wi[5-1];
1405 }
1406
1407 DCT12_PART2
1408
1409 out2[11-0] = in2 * wi[11-0];
1410 out2[6 +0] = in2 * wi[6+0];
1411 out2[6 +2] = in3 * wi[6+2];
1412 out2[11-2] = in3 * wi[11-2];
1413
1414 out2[0+0] += in0 * wi[0];
1415 out2[5-0] += in0 * wi[5-0];
1416 out2[0+2] += in4 * wi[2];
1417 out2[5-2] += in4 * wi[5-2];
1418 }
1419 }
1420
1421 /*
1422 * III_hybrid
1423 */
III_hybrid(void * mp,real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],int ch,struct gr_info_s * gr_info)1424 static void III_hybrid(void *mp,real fsIn[SBLIMIT][SSLIMIT],real tsOut[SSLIMIT][SBLIMIT],
1425 int ch,struct gr_info_s *gr_info)
1426 {
1427 struct mpstr *gmp = mp;
1428
1429 real *tspnt = (real *) tsOut;
1430 real (*block)[2][SBLIMIT*SSLIMIT] = gmp->hybrid_block;
1431 int *blc = gmp->hybrid_blc;
1432 real *rawout1,*rawout2;
1433 int bt;
1434 unsigned int sb = 0;
1435
1436 {
1437 int b = blc[ch];
1438 rawout1=block[b][ch];
1439 b=-b+1;
1440 rawout2=block[b][ch];
1441 blc[ch] = b;
1442 }
1443
1444
1445 if(gr_info->mixed_block_flag) {
1446 sb = 2;
1447 dct36(fsIn[0],rawout1,rawout2,win[0],tspnt);
1448 dct36(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1449 rawout1 += 36; rawout2 += 36; tspnt += 2;
1450 }
1451
1452 bt = gr_info->block_type;
1453 if(bt == 2) {
1454 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1455 dct12(fsIn[sb],rawout1,rawout2,win[2],tspnt);
1456 dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1457 }
1458 }
1459 else {
1460 for (; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36) {
1461 dct36(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1462 dct36(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1463 }
1464 }
1465
1466 for(;sb<SBLIMIT;sb++,tspnt++) {
1467 int i;
1468 for(i=0;i<SSLIMIT;i++) {
1469 tspnt[i*SBLIMIT] = *rawout1++;
1470 *rawout2++ = 0.0;
1471 }
1472 }
1473 }
1474
1475 /*
1476 * main layer3 handler
1477 */
do_layer3(void * mp,struct frame * fr,unsigned char * pcm_sample,int * pcm_point)1478 int do_layer3(void *mp, struct frame *fr, unsigned char *pcm_sample, int *pcm_point)
1479 {
1480 int gr, ch, ss, clip = 0;
1481 int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1482 struct III_sideinfo sideinfo;
1483 int stereo = fr->stereo;
1484 int single = fr->single;
1485 int ms_stereo,i_stereo;
1486 int sfreq = fr->sampling_frequency;
1487 int stereo1,granules;
1488
1489
1490
1491 if(stereo == 1) { /* stream is mono */
1492 stereo1 = 1;
1493 single = 0;
1494 }
1495 else if(single >= 0) /* stream is stereo, but force to mono */
1496 stereo1 = 1;
1497 else
1498 stereo1 = 2;
1499
1500 if(fr->mode == MPG_MD_JOINT_STEREO) {
1501 ms_stereo = fr->mode_ext & 0x2;
1502 i_stereo = fr->mode_ext & 0x1;
1503 }
1504 else
1505 ms_stereo = i_stereo = 0;
1506
1507
1508 if(fr->lsf) {
1509 granules = 1;
1510 if (III_get_side_info_2(&sideinfo,stereo,ms_stereo,sfreq,single))
1511 return -1;
1512 }
1513 else {
1514 granules = 2;
1515 #ifdef MPEG1
1516 if (III_get_side_info_1(&sideinfo,stereo,ms_stereo,sfreq,single))
1517 return -1;
1518 #endif
1519 }
1520
1521 if(almp3_set_pointer(mp,sideinfo.main_data_begin) == MP3_ERR)
1522 return 0;
1523
1524
1525 for (gr=0;gr<granules;gr++)
1526 {
1527 static real hybridIn[2][SBLIMIT][SSLIMIT];
1528 static real hybridOut[2][SSLIMIT][SBLIMIT];
1529
1530 {
1531 struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
1532 long part2bits;
1533 if(fr->lsf)
1534 part2bits = III_get_scale_factors_2(scalefacs[0],gr_info,0);
1535 else {
1536 #ifdef MPEG1
1537 part2bits = III_get_scale_factors_1(scalefacs[0],gr_info);
1538 #endif
1539 }
1540 if(III_dequantize_sample(hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
1541 return 0;
1542 }
1543 if(stereo == 2) {
1544 struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
1545 long part2bits;
1546 if(fr->lsf)
1547 part2bits = III_get_scale_factors_2(scalefacs[1],gr_info,i_stereo);
1548 else {
1549 #ifdef MPEG1
1550 part2bits = III_get_scale_factors_1(scalefacs[1],gr_info);
1551 #endif
1552 }
1553
1554 if(III_dequantize_sample(hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
1555 return 0;
1556
1557 if(ms_stereo) {
1558 int i;
1559 for(i=0;i<SBLIMIT*SSLIMIT;i++) {
1560 real tmp0,tmp1;
1561 tmp0 = ((real *) hybridIn[0])[i];
1562 tmp1 = ((real *) hybridIn[1])[i];
1563 ((real *) hybridIn[1])[i] = tmp0 - tmp1;
1564 ((real *) hybridIn[0])[i] = tmp0 + tmp1;
1565 }
1566 }
1567
1568 if(i_stereo)
1569 III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
1570
1571 if(ms_stereo || i_stereo || (single == 3) ) {
1572 if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
1573 sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
1574 else
1575 gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
1576 }
1577
1578 switch(single) {
1579 case 3:
1580 {
1581 register unsigned int i;
1582 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1583 for(i=0;i<SSLIMIT*gr_info->maxb;i++,in0++)
1584 *in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
1585 }
1586 break;
1587 case 1:
1588 {
1589 register unsigned int i;
1590 register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
1591 for(i=0;i<SSLIMIT*gr_info->maxb;i++)
1592 *in0++ = *in1++;
1593 }
1594 break;
1595 }
1596 }
1597
1598
1599 for(ch=0;ch<stereo1;ch++) {
1600 struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
1601 III_antialias(hybridIn[ch],gr_info);
1602 III_hybrid(mp,hybridIn[ch], hybridOut[ch], ch,gr_info);
1603 }
1604
1605 for(ss=0;ss<SSLIMIT;ss++) {
1606 if(single >= 0) {
1607 clip += synth_1to1_mono(mp,hybridOut[0][ss],pcm_sample,pcm_point);
1608 }
1609 else {
1610 int p1 = *pcm_point;
1611 clip += synth_1to1(mp,hybridOut[0][ss],0,pcm_sample,&p1);
1612 clip += synth_1to1(mp,hybridOut[1][ss],1,pcm_sample,pcm_point);
1613 }
1614 }
1615 }
1616
1617 return 0;
1618 }
1619
1620