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