1
/*____________________________________________________________________________
3
FreeAmp - The Free MP3 Player
5
MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
6
Corp. http://www.xingtech.com
8
Portions Copyright (C) 1998 EMusic.com
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.
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.
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.
24
$Id: icdct.c,v 1.1 1999/12/07 05:46:01 markster Exp $
25
____________________________________________________________________________*/
27
/**** icdct.c ***************************************************
30
MPEG audio decoder, dct
31
portable C integer dct
35
******************************************************************/
43
/*-------------------------------------------------------------------*/
44
static DCTCOEF coef32[32]; /* 32 pt dct coefs */
49
#define forward_bf idx_forward_bf
50
/*--- #define forward_bf ptr_forward_bf ---*/
51
/*------------------------------------------------------------*/
52
DCTCOEF *i_dct_coef_addr()
56
/*------------------------------------------------------------*/
57
static void idx_forward_bf(int m, int n, INT32 x[], INT32 f[], DCTCOEF coef[])
64
for (i = 0; i < m; i++, p0 += n)
69
for (j = 0; j < n2; j++, p++, q--, k++)
72
f[n2 + p] = ((x[p] - x[q]) * coef[k]) >> DCTBITS;
76
/*------------------------------------------------------------*/
78
static void ptr_forward_bf(int m, int n, INT32 x[], INT32 f[], DCTCOEF coef[])
90
*((f++)+n2) = ( (*x++ - *y) * (*c++) ) >> DCTBITS;
97
/*------------------------------------------------------------*/
98
static void forward_bfm(int m, INT32 x[], INT32 f[])
103
/*--- special case last fwd stage ----*/
104
for (p = 0, i = 0; i < m; i++, p += 2)
106
f[p] = x[p] + x[p + 1];
107
f[p + 1] = ((x[p] - x[p + 1]) * coef32[30]) >> DCTBITS;
110
/*------------------------------------------------------------*/
111
static void back_bf(int m, int n, INT32 x[], INT32 f[])
119
for (i = 0; i < m; i++, p0 += n)
123
for (j = 0; j < n2; j++, p += 2, q++)
126
for (j = 0; j < n21; j++, p += 2, q++)
127
f[p] = x[q] + x[q + 1];
131
/*------------------------------------------------------------*/
132
static void back_bf0(int n, INT32 x[], WININT f[])
138
for (p = 0, q = 0; p < n; p += 2, q++)
143
else if (tmp < -32768)
147
for (p = 1; q < n; p += 2, q++)
149
tmp = x[q] + x[q + 1];
152
else if (tmp < -32768)
159
else if (tmp < -32768)
163
for (p = 0, q = 0; p < n; p += 2, q++)
165
for (p = 1; q < n; p += 2, q++)
166
f[p] = x[q] + x[q + 1];
171
/*------------------------------------------------------------*/
172
void i_dct32(SAMPLEINT x[], WININT c[])
174
INT32 a[32]; /* ping pong buffers */
178
/* special first stage */
179
for (p = 0, q = 31; p < 16; p++, q--)
181
a[p] = (INT32) x[p] + x[q];
182
a[16 + p] = (coef32[p] * ((INT32) x[p] - x[q])) >> DCTBITS;
185
forward_bf(2, 16, a, b, coef32 + 16);
186
forward_bf(4, 8, b, a, coef32 + 16 + 8);
187
forward_bf(8, 4, a, b, coef32 + 16 + 8 + 4);
188
forward_bfm(16, b, a);
191
back_bf(2, 16, a, b);
194
/*------------------------------------------------------------*/
195
void i_dct32_dual(SAMPLEINT x[], WININT c[])
197
INT32 a[32]; /* ping pong buffers */
201
/* special first stage for dual chan (interleaved x) */
204
for (p = 0; p < 16; p++, pp += 2, qq -= 2)
206
a[p] = (INT32) x[pp] + x[qq];
207
a[16 + p] = (coef32[p] * ((INT32) x[pp] - x[qq])) >> DCTBITS;
209
forward_bf(2, 16, a, b, coef32 + 16);
210
forward_bf(4, 8, b, a, coef32 + 16 + 8);
211
forward_bf(8, 4, a, b, coef32 + 16 + 8 + 4);
212
forward_bfm(16, b, a);
215
back_bf(2, 16, a, b);
218
/*---------------convert dual to mono------------------------------*/
219
void i_dct32_dual_mono(SAMPLEINT x[], WININT c[])
221
INT32 a[32]; /* ping pong buffers */
226
/* special first stage */
229
for (p = 0; p < 16; p++, pp += 2, qq -= 2)
231
t1 = ((INT32) x[pp] + x[pp + 1]);
232
t2 = ((INT32) x[qq] + x[qq + 1]);
233
a[p] = (t1 + t2) >> 1;
234
a[16 + p] = coef32[p] * (t1 - t2) >> (DCTBITS + 1);
236
forward_bf(2, 16, a, b, coef32 + 16);
237
forward_bf(4, 8, b, a, coef32 + 16 + 8);
238
forward_bf(8, 4, a, b, coef32 + 16 + 8 + 4);
239
forward_bfm(16, b, a);
242
back_bf(2, 16, a, b);
245
/*------------------------------------------------------------*/
246
/*---------------- 16 pt dct -------------------------------*/
247
void i_dct16(SAMPLEINT x[], WININT c[])
249
INT32 a[16]; /* ping pong buffers */
253
/* special first stage (drop highest sb) */
255
a[8] = (a[0] * coef32[16]) >> DCTBITS;
256
for (p = 1, q = 14; p < 8; p++, q--)
258
a[p] = (INT32) x[p] + x[q];
259
a[8 + p] = (((INT32) x[p] - x[q]) * coef32[16 + p]) >> DCTBITS;
261
forward_bf(2, 8, a, b, coef32 + 16 + 8);
262
forward_bf(4, 4, b, a, coef32 + 16 + 8 + 4);
263
forward_bfm(8, a, b);
268
/*------------------------------------------------------------*/
269
/*---------------- 16 pt dct dual chan---------------------*/
270
void i_dct16_dual(SAMPLEINT x[], WININT c[])
273
INT32 a[16]; /* ping pong buffers */
276
/* special first stage for interleaved input */
278
a[8] = (coef32[16] * a[0]) >> DCTBITS;
281
for (p = 1; p < 8; p++, pp += 2, qq -= 2)
283
a[p] = (INT32) x[pp] + x[qq];
284
a[8 + p] = (coef32[16 + p] * ((INT32) x[pp] - x[qq])) >> DCTBITS;
286
forward_bf(2, 8, a, b, coef32 + 16 + 8);
287
forward_bf(4, 4, b, a, coef32 + 16 + 8 + 4);
288
forward_bfm(8, a, b);
293
/*------------------------------------------------------------*/
294
/*---------------- 16 pt dct dual to mono-------------------*/
295
void i_dct16_dual_mono(SAMPLEINT x[], WININT c[])
297
INT32 a[16]; /* ping pong buffers */
302
/* special first stage */
303
a[0] = ((INT32) x[0] + x[1]) >> 1;
304
a[8] = (coef32[16] * a[0]) >> DCTBITS;
307
for (p = 1; p < 8; p++, pp += 2, qq -= 2)
309
t1 = (INT32) x[pp] + x[pp + 1];
310
t2 = (INT32) x[qq] + x[qq + 1];
311
a[p] = (t1 + t2) >> 1;
312
a[8 + p] = (coef32[16 + p] * (t1 - t2)) >> (DCTBITS + 1);
314
forward_bf(2, 8, a, b, coef32 + 16 + 8);
315
forward_bf(4, 4, b, a, coef32 + 16 + 8 + 4);
316
forward_bfm(8, a, b);
321
/*------------------------------------------------------------*/
322
/*---------------- 8 pt dct -------------------------------*/
323
void i_dct8(SAMPLEINT x[], WININT c[])
326
INT32 a[8]; /* ping pong buffers */
329
/* special first stage */
331
for (p = 0, q = 7; p < 4; p++, q--)
333
b[p] = (INT32) x[p] + x[q];
334
b[4 + p] = (coef32[16 + 8 + p] * ((INT32) x[p] - x[q])) >> DCTBITS;
337
forward_bf(2, 4, b, a, coef32 + 16 + 8 + 4);
338
forward_bfm(4, a, b);
342
/*------------------------------------------------------------*/
343
/*---------------- 8 pt dct dual chan---------------------*/
344
void i_dct8_dual(SAMPLEINT x[], WININT c[])
347
INT32 a[8]; /* ping pong buffers */
350
/* special first stage for interleaved input */
351
for (p = 0, pp = 0, qq = 14; p < 4; p++, pp += 2, qq -= 2)
353
b[p] = (INT32) x[pp] + x[qq];
354
b[4 + p] = (coef32[16 + 8 + p] * ((INT32) x[pp] - x[qq])) >> DCTBITS;
356
forward_bf(2, 4, b, a, coef32 + 16 + 8 + 4);
357
forward_bfm(4, a, b);
361
/*------------------------------------------------------------*/
362
/*---------------- 8 pt dct dual to mono---------------------*/
363
void i_dct8_dual_mono(SAMPLEINT x[], WININT c[])
366
INT32 a[8]; /* ping pong buffers */
370
/* special first stage */
371
for (p = 0, pp = 0, qq = 14; p < 4; p++, pp += 2, qq -= 2)
373
t1 = (INT32) x[pp] + x[pp + 1];
374
t2 = (INT32) x[qq] + x[qq + 1];
375
b[p] = (t1 + t2) >> 1;
376
b[4 + p] = (coef32[16 + 8 + p] * (t1 - t2)) >> (DCTBITS + 1);
378
forward_bf(2, 4, b, a, coef32 + 16 + 8 + 4);
379
forward_bfm(4, a, b);
383
/*------------------------------------------------------------*/