1 /*____________________________________________________________________________
2 
3 	FreeAmp - The Free MP3 Player
4 
5         MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
6         Corp.  http://www.xingtech.com
7 
8 	Portions Copyright (C) 1998-1999 EMusic.com
9 
10 	This program is free software; you can redistribute it and/or modify
11 	it under the terms of the GNU General Public License as published by
12 	the Free Software Foundation; either version 2 of the License, or
13 	(at your option) any later version.
14 
15 	This program is distributed in the hope that it will be useful,
16 	but WITHOUT ANY WARRANTY; without even the implied warranty of
17 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 	GNU General Public License for more details.
19 
20 	You should have received a copy of the GNU General Public License
21 	along with this program; if not, write to the Free Software
22 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 
24 	$Id: isbt.c,v 1.6 2000/10/13 14:29:02 ijr Exp $
25 ____________________________________________________________________________*/
26 
27 /****  isbt.c  ***************************************************
28 
29 MPEG audio decoder, dct and window
30 portable C       integer version of csbt.c
31 
32 mods 11/15/95 for Layer I
33 
34 mods 1/7/97 warnings
35 
36 ******************************************************************/
37 
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <float.h>
41 #include <math.h>
42 #include "itype.h"
43 
44 
45 #ifdef _MSC_VER
46 #pragma warning(disable: 4244)
47 #pragma warning(disable: 4056)
48 #endif
49 
50 
51 
52 /* asm is quick only, c code does not need separate window for right */
53 /* full is opposite of quick */
54 #ifdef  FULL_INTEGER
55 #define i_window_dual_right   i_window_dual
56 #define i_window16_dual_right i_window16_dual
57 #define i_window8_dual_right  i_window8_dual
58 #endif
59 
60 
61 void i_dct32(SAMPLEINT * sample, WININT * vbuf);
62 void i_dct32_dual(SAMPLEINT * sample, WININT * vbuf);
63 void i_dct32_dual_mono(SAMPLEINT * sample, WININT * vbuf);
64 
65 void i_dct16(SAMPLEINT * sample, WININT * vbuf);
66 void i_dct16_dual(SAMPLEINT * sample, WININT * vbuf);
67 void i_dct16_dual_mono(SAMPLEINT * sample, WININT * vbuf);
68 
69 void i_dct8(SAMPLEINT * sample, WININT * vbuf);
70 void i_dct8_dual(SAMPLEINT * sample, WININT * vbuf);
71 void i_dct8_dual_mono(SAMPLEINT * sample, WININT * vbuf);
72 
73 
74 void i_window(WININT * vbuf, int vb_ptr, short *pcm);
75 void i_window_dual(WININT * vbuf, int vb_ptr, short *pcm);
76 void i_window_dual_right(WININT * vbuf, int vb_ptr, short *pcm);
77 
78 void i_window16(WININT * vbuf, int vb_ptr, short *pcm);
79 void i_window16_dual(WININT * vbuf, int vb_ptr, short *pcm);
80 void i_window16_dual_right(WININT * vbuf, int vb_ptr, short *pcm);
81 
82 void i_window8(WININT * vbuf, int vb_ptr, short *pcm);
83 void i_window8_dual(WININT * vbuf, int vb_ptr, short *pcm);
84 void i_window8_dual_right(WININT * vbuf, int vb_ptr, short *pcm);
85 
86 /*--------------------------------------------------------------------*/
87 /*--  floating point window coefs  ---*/
88 /*--  for integer-quick window, table used to generate integer coefs --*/
89 static float wincoef[264] =
90 {
91 #include "tableawd.h"
92 };
93 
94 /* circular window buffers */
95 /* extern windows because of asm */
96 static signed int vb_ptr;
97 
98 // static WININT vbuf[512];
99 //static WININT vbuf2[512];
100 extern WININT vbuf[512];
101 extern WININT vbuf2[512];
102 
103 DCTCOEF *i_dct_coef_addr();
104 
105 /*======================================================================*/
gencoef()106 static void gencoef()		/* gen coef for N=32 */
107 {
108    int p, n, i, k;
109    double t, pi;
110    DCTCOEF *coef32;
111 
112    coef32 = i_dct_coef_addr();
113 
114 
115    pi = 4.0 * atan(1.0);
116    n = 16;
117    k = 0;
118    for (i = 0; i < 5; i++, n = n / 2)
119    {
120       for (p = 0; p < n; p++, k++)
121       {
122 	 t = (pi / (4 * n)) * (2 * p + 1);
123 	 coef32[k] = (1 << DCTBITS) * (0.50 / cos(t)) + 0.5;
124       }
125    }
126 }
127 /*------------------------------------------------------------*/
128 WINCOEF *i_wincoef_addr();
genwincoef_q()129 static void genwincoef_q()	/* gen int window coefs from floating table */
130 {
131    int i, j, k, m;
132    float x;
133    WINCOEF *iwincoef;
134 
135    iwincoef = i_wincoef_addr();
136 
137 
138 /*--- coefs generated inline for quick window ---*/
139 /*-- quick uses only 116 coefs --*/
140 
141    k = 0;
142    m = 0;
143    for (i = 0; i < 16; i++)
144    {
145       k += 5;
146       for (j = 0; j < 7; j++)
147       {
148 	 x = (1 << WINBITS) * wincoef[k++];
149 	 if (x > 0.0)
150 	    x += 0.5;
151 	 else
152 	    x -= 0.5;
153 	 iwincoef[m++] = x;
154       }
155       k += 4;
156    }
157    k++;
158    for (j = 0; j < 4; j++)
159    {
160       x = (1 << WINBITS) * wincoef[k++];
161       if (x > 0.0)
162 	 x += 0.5;
163       else
164 	 x -= 0.5;
165       iwincoef[m++] = x;
166    }
167 }
168 /*------------------------------------------------------------*/
genwincoef()169 static void genwincoef()	/* gen int window coefs from floating table */
170 {
171    int i;
172    float x;
173    WINCOEF *iwincoef;
174    WINCOEF *i_wincoef_addr();
175 
176    iwincoef = i_wincoef_addr();
177 
178    for (i = 0; i < 264; i++)
179    {
180       x = (1 << WINBITS) * wincoef[i];
181       if (x > 0.0)
182 	 x += 0.5;
183       else
184 	 x -= 0.5;
185       iwincoef[i] = x;
186    }
187 }
188 /*------------------------------------------------------------*/
i_sbt_init()189 void i_sbt_init()
190 {
191    int i;
192    static int first_pass = 1;
193 
194 #ifdef FULL_INTEGER
195    static int full_integer = 1;
196 
197 #else
198    static int full_integer = 0;
199 
200 #endif
201 
202    if (first_pass)
203    {
204       gencoef();
205       if (full_integer)
206 	 genwincoef();
207       else
208 	 genwincoef_q();
209       first_pass = 0;
210    }
211 
212 /* clear window vbuf */
213    for (i = 0; i < 512; i++)
214       vbuf[i] = vbuf2[i] = 0;
215    vb_ptr = 0;
216 
217 }
218 /*==============================================================*/
219 /*==============================================================*/
220 /*==============================================================*/
i_sbt_mono(SAMPLEINT * sample,short * pcm,int n)221 void i_sbt_mono(SAMPLEINT * sample, short *pcm, int n)
222 {
223    int i;
224 
225    for (i = 0; i < n; i++)
226    {
227       i_dct32(sample, vbuf + vb_ptr);
228       i_window(vbuf, vb_ptr, pcm);
229       sample += 64;
230       vb_ptr = (vb_ptr - 32) & 511;
231       pcm += 32;
232    }
233 
234 }
235 /*------------------------------------------------------------*/
i_sbt_dual(SAMPLEINT * sample,short * pcm,int n)236 void i_sbt_dual(SAMPLEINT * sample, short *pcm, int n)
237 {
238    int i;
239 
240    for (i = 0; i < n; i++)
241    {
242       i_dct32_dual(sample, vbuf + vb_ptr);
243       i_dct32_dual(sample + 1, vbuf2 + vb_ptr);
244       i_window_dual(vbuf, vb_ptr, pcm);
245       i_window_dual_right(vbuf2, vb_ptr, pcm + 1);
246       sample += 64;
247       vb_ptr = (vb_ptr - 32) & 511;
248       pcm += 64;
249    }
250 }
251 /*------------------------------------------------------------*/
252 /* convert dual to mono */
i_sbt_dual_mono(SAMPLEINT * sample,short * pcm,int n)253 void i_sbt_dual_mono(SAMPLEINT * sample, short *pcm, int n)
254 {
255    int i;
256 
257    for (i = 0; i < n; i++)
258    {
259       i_dct32_dual_mono(sample, vbuf + vb_ptr);
260       i_window(vbuf, vb_ptr, pcm);
261       sample += 64;
262       vb_ptr = (vb_ptr - 32) & 511;
263       pcm += 32;
264    }
265 }
266 /*------------------------------------------------------------*/
267 /* convert dual to left */
i_sbt_dual_left(SAMPLEINT * sample,short * pcm,int n)268 void i_sbt_dual_left(SAMPLEINT * sample, short *pcm, int n)
269 {
270    int i;
271 
272    for (i = 0; i < n; i++)
273    {
274       i_dct32_dual(sample, vbuf + vb_ptr);
275       i_window(vbuf, vb_ptr, pcm);
276       sample += 64;
277       vb_ptr = (vb_ptr - 32) & 511;
278       pcm += 32;
279    }
280 }
281 /*------------------------------------------------------------*/
282 /* convert dual to right */
i_sbt_dual_right(SAMPLEINT * sample,short * pcm,int n)283 void i_sbt_dual_right(SAMPLEINT * sample, short *pcm, int n)
284 {
285    int i;
286 
287    sample++;			/* point to right chan */
288    for (i = 0; i < n; i++)
289    {
290       i_dct32_dual(sample, vbuf + vb_ptr);
291       i_window(vbuf, vb_ptr, pcm);
292       sample += 64;
293       vb_ptr = (vb_ptr - 32) & 511;
294       pcm += 32;
295    }
296 }
297 /*------------------------------------------------------------*/
298 /*---------------- 16 pt sbt's  -------------------------------*/
299 /*------------------------------------------------------------*/
i_sbt16_mono(SAMPLEINT * sample,short * pcm,int n)300 void i_sbt16_mono(SAMPLEINT * sample, short *pcm, int n)
301 {
302    int i;
303 
304    for (i = 0; i < n; i++)
305    {
306       i_dct16(sample, vbuf + vb_ptr);
307       i_window16(vbuf, vb_ptr, pcm);
308       sample += 64;
309       vb_ptr = (vb_ptr - 16) & 255;
310       pcm += 16;
311    }
312 
313 }
314 /*------------------------------------------------------------*/
i_sbt16_dual(SAMPLEINT * sample,short * pcm,int n)315 void i_sbt16_dual(SAMPLEINT * sample, short *pcm, int n)
316 {
317    int i;
318 
319    for (i = 0; i < n; i++)
320    {
321       i_dct16_dual(sample, vbuf + vb_ptr);
322       i_dct16_dual(sample + 1, vbuf2 + vb_ptr);
323       i_window16_dual(vbuf, vb_ptr, pcm);
324       i_window16_dual_right(vbuf2, vb_ptr, pcm + 1);
325       sample += 64;
326       vb_ptr = (vb_ptr - 16) & 255;
327       pcm += 32;
328    }
329 
330 }
331 /*------------------------------------------------------------*/
i_sbt16_dual_mono(SAMPLEINT * sample,short * pcm,int n)332 void i_sbt16_dual_mono(SAMPLEINT * sample, short *pcm, int n)
333 {
334    int i;
335 
336    for (i = 0; i < n; i++)
337    {
338       i_dct16_dual_mono(sample, vbuf + vb_ptr);
339       i_window16(vbuf, vb_ptr, pcm);
340       sample += 64;
341       vb_ptr = (vb_ptr - 16) & 255;
342       pcm += 16;
343    }
344 }
345 /*------------------------------------------------------------*/
i_sbt16_dual_left(SAMPLEINT * sample,short * pcm,int n)346 void i_sbt16_dual_left(SAMPLEINT * sample, short *pcm, int n)
347 {
348    int i;
349 
350    for (i = 0; i < n; i++)
351    {
352       i_dct16_dual(sample, vbuf + vb_ptr);
353       i_window16(vbuf, vb_ptr, pcm);
354       sample += 64;
355       vb_ptr = (vb_ptr - 16) & 255;
356       pcm += 16;
357    }
358 }
359 /*------------------------------------------------------------*/
i_sbt16_dual_right(SAMPLEINT * sample,short * pcm,int n)360 void i_sbt16_dual_right(SAMPLEINT * sample, short *pcm, int n)
361 {
362    int i;
363 
364    sample++;
365    for (i = 0; i < n; i++)
366    {
367       i_dct16_dual(sample, vbuf + vb_ptr);
368       i_window16(vbuf, vb_ptr, pcm);
369       sample += 64;
370       vb_ptr = (vb_ptr - 16) & 255;
371       pcm += 16;
372    }
373 }
374 /*------------------------------------------------------------*/
375 /*---------------- 8 pt sbt's  -------------------------------*/
376 /*------------------------------------------------------------*/
i_sbt8_mono(SAMPLEINT * sample,short * pcm,int n)377 void i_sbt8_mono(SAMPLEINT * sample, short *pcm, int n)
378 {
379    int i;
380 
381    for (i = 0; i < n; i++)
382    {
383       i_dct8(sample, vbuf + vb_ptr);
384       i_window8(vbuf, vb_ptr, pcm);
385       sample += 64;
386       vb_ptr = (vb_ptr - 8) & 127;
387       pcm += 8;
388    }
389 
390 }
391 /*------------------------------------------------------------*/
i_sbt8_dual(SAMPLEINT * sample,short * pcm,int n)392 void i_sbt8_dual(SAMPLEINT * sample, short *pcm, int n)
393 {
394    int i;
395 
396    for (i = 0; i < n; i++)
397    {
398       i_dct8_dual(sample, vbuf + vb_ptr);
399       i_dct8_dual(sample + 1, vbuf2 + vb_ptr);
400       i_window8_dual(vbuf, vb_ptr, pcm);
401       i_window8_dual_right(vbuf2, vb_ptr, pcm + 1);
402       sample += 64;
403       vb_ptr = (vb_ptr - 8) & 127;
404       pcm += 16;
405    }
406 }
407 /*------------------------------------------------------------*/
i_sbt8_dual_mono(SAMPLEINT * sample,short * pcm,int n)408 void i_sbt8_dual_mono(SAMPLEINT * sample, short *pcm, int n)
409 {
410    int i;
411 
412    for (i = 0; i < n; i++)
413    {
414       i_dct8_dual_mono(sample, vbuf + vb_ptr);
415       i_window8(vbuf, vb_ptr, pcm);
416       sample += 64;
417       vb_ptr = (vb_ptr - 8) & 127;
418       pcm += 8;
419    }
420 }
421 /*------------------------------------------------------------*/
i_sbt8_dual_left(SAMPLEINT * sample,short * pcm,int n)422 void i_sbt8_dual_left(SAMPLEINT * sample, short *pcm, int n)
423 {
424    int i;
425 
426    for (i = 0; i < n; i++)
427    {
428       i_dct8_dual(sample, vbuf + vb_ptr);
429       i_window8(vbuf, vb_ptr, pcm);
430       sample += 64;
431       vb_ptr = (vb_ptr - 8) & 127;
432       pcm += 8;
433    }
434 }
435 /*------------------------------------------------------------*/
i_sbt8_dual_right(SAMPLEINT * sample,short * pcm,int n)436 void i_sbt8_dual_right(SAMPLEINT * sample, short *pcm, int n)
437 {
438    int i;
439 
440    sample++;
441    for (i = 0; i < n; i++)
442    {
443       i_dct8_dual(sample, vbuf + vb_ptr);
444       i_window8(vbuf, vb_ptr, pcm);
445       sample += 64;
446       vb_ptr = (vb_ptr - 8) & 127;
447       pcm += 8;
448    }
449 }
450 /*------------------------------------------------------------*/
451 /*--- 8 bit output ----------------*/
452 #include "isbtb.c"
453 /*----------------------------------*/
454 
455 #ifdef _MSC_VER
456 #pragma warning(default: 4244)
457 #pragma warning(default: 4056)
458 #endif
459