10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
|
|
4 **
|
|
5 ** This program is free software; you can redistribute it and/or modify
|
|
6 ** it under the terms of the GNU General Public License as published by
|
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
|
8 ** (at your option) any later version.
|
|
9 **
|
|
10 ** This program is distributed in the hope that it will be useful,
|
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ** GNU General Public License for more details.
|
|
14 **
|
|
15 ** You should have received a copy of the GNU General Public License
|
|
16 ** along with this program; if not, write to the Free Software
|
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
10989
|
25 ** $Id: mdct.c,v 1.28 2003/09/30 12:43:05 menno Exp $
|
10725
|
26 **/
|
|
27
|
|
28 /*
|
|
29 * Fast (I)MDCT Implementation using (I)FFT ((Inverse) Fast Fourier Transform)
|
|
30 * and consists of three steps: pre-(I)FFT complex multiplication, complex
|
|
31 * (I)FFT, post-(I)FFT complex multiplication,
|
|
32 *
|
|
33 * As described in:
|
|
34 * P. Duhamel, Y. Mahieux, and J.P. Petit, "A Fast Algorithm for the
|
|
35 * Implementation of Filter Banks Based on 'Time Domain Aliasing
|
|
36 * Cancellation’," IEEE Proc. on ICASSP‘91, 1991, pp. 2209-2212.
|
|
37 *
|
|
38 *
|
|
39 * As of April 6th 2002 completely rewritten.
|
|
40 * This (I)MDCT can now be used for any data size n, where n is divisible by 8.
|
|
41 *
|
|
42 */
|
|
43
|
|
44 #include "common.h"
|
|
45 #include "structs.h"
|
|
46
|
|
47 #include <stdlib.h>
|
|
48 #ifdef _WIN32_WCE
|
|
49 #define assert(x)
|
|
50 #else
|
|
51 #include <assert.h>
|
|
52 #endif
|
|
53
|
|
54 #include "cfft.h"
|
|
55 #include "mdct.h"
|
|
56
|
|
57 /* const_tab[]:
|
|
58 0: sqrt(2 / N)
|
|
59 1: cos(2 * PI / N)
|
|
60 2: sin(2 * PI / N)
|
|
61 3: cos(2 * PI * (1/8) / N)
|
|
62 4: sin(2 * PI * (1/8) / N)
|
|
63 */
|
|
64 #ifndef FIXED_POINT
|
|
65 #ifdef _MSC_VER
|
|
66 #pragma warning(disable:4305)
|
|
67 #pragma warning(disable:4244)
|
|
68 #endif
|
|
69 real_t const_tab[][5] =
|
|
70 {
|
|
71 { COEF_CONST(0.0312500000), COEF_CONST(0.9999952938), COEF_CONST(0.0030679568),
|
|
72 COEF_CONST(0.9999999265), COEF_CONST(0.0003834952) }, /* 2048 */
|
|
73 { COEF_CONST(0.0322748612), COEF_CONST(0.9999946356), COEF_CONST(0.0032724866),
|
|
74 COEF_CONST(0.9999999404), COEF_CONST(0.0004090615) }, /* 1920 */
|
|
75 { COEF_CONST(0.0441941738), COEF_CONST(0.9999811649), COEF_CONST(0.0061358847),
|
|
76 COEF_CONST(0.9999997020), COEF_CONST(0.0007669903) }, /* 1024 */
|
|
77 { COEF_CONST(0.0456435465), COEF_CONST(0.9999786019), COEF_CONST(0.0065449383),
|
|
78 COEF_CONST(0.9999996424), COEF_CONST(0.0008181230) }, /* 960 */
|
|
79 { COEF_CONST(0.0883883476), COEF_CONST(0.9996988177), COEF_CONST(0.0245412290),
|
|
80 COEF_CONST(0.9999952912), COEF_CONST(0.0030679568) }, /* 256 */
|
|
81 { COEF_CONST(0.0912870929), COEF_CONST(0.9996573329), COEF_CONST(0.0261769500),
|
|
82 COEF_CONST(0.9999946356), COEF_CONST(0.0032724866) } /* 240 */
|
|
83 #ifdef SSR_DEC
|
|
84 ,{ COEF_CONST(0.062500000), COEF_CONST(0.999924702), COEF_CONST(0.012271538),
|
|
85 COEF_CONST(0.999998823), COEF_CONST(0.00153398) }, /* 512 */
|
|
86 { COEF_CONST(0.176776695), COEF_CONST(0.995184727), COEF_CONST(0.09801714),
|
|
87 COEF_CONST(0.999924702), COEF_CONST(0.012271538) } /* 64 */
|
|
88 #endif
|
|
89 };
|
|
90 #else
|
|
91 real_t const_tab[][5] =
|
|
92 {
|
|
93 { COEF_CONST(1), COEF_CONST(0.9999952938), COEF_CONST(0.0030679568),
|
|
94 COEF_CONST(0.9999999265), COEF_CONST(0.0003834952) }, /* 2048 */
|
|
95 { COEF_CONST(/* sqrt(1024/960) */ 1.03279556), COEF_CONST(0.9999946356), COEF_CONST(0.0032724866),
|
|
96 COEF_CONST(0), COEF_CONST(0.0004090615) }, /* 1920 */
|
|
97 { COEF_CONST(1), COEF_CONST(0.9999811649), COEF_CONST(0.0061358847),
|
|
98 COEF_CONST(0.9999997020), COEF_CONST(0.0007669903) }, /* 1024 */
|
|
99 { COEF_CONST(/* sqrt(512/480) */ 1.03279556), COEF_CONST(0.9999786019), COEF_CONST(0.0065449383),
|
|
100 COEF_CONST(0.9999996424), COEF_CONST(0.0008181230) }, /* 960 */
|
|
101 { COEF_CONST(1), COEF_CONST(0.9996988177), COEF_CONST(0.0245412290),
|
|
102 COEF_CONST(0.9999952912), COEF_CONST(0.0030679568) }, /* 256 */
|
|
103 { COEF_CONST(/* sqrt(256/240) */ 1.03279556), COEF_CONST(0.9996573329), COEF_CONST(0.0261769500),
|
|
104 COEF_CONST(0.9999946356), COEF_CONST(0.0032724866) } /* 240 */
|
|
105 #ifdef SSR_DEC
|
|
106 ,{ COEF_CONST(0), COEF_CONST(0.999924702), COEF_CONST(0.012271538),
|
|
107 COEF_CONST(0.999998823), COEF_CONST(0.00153398) }, /* 512 */
|
|
108 { COEF_CONST(0), COEF_CONST(0.995184727), COEF_CONST(0.09801714),
|
|
109 COEF_CONST(0.999924702), COEF_CONST(0.012271538) } /* 64 */
|
|
110 #endif
|
|
111 };
|
|
112 #endif
|
|
113
|
|
114 uint8_t map_N_to_idx(uint16_t N)
|
|
115 {
|
10989
|
116 /* gives an index into const_tab above */
|
|
117 /* for normal AAC deocding (eg. no scalable profile) only */
|
|
118 /* index 0 and 4 will be used */
|
10725
|
119 switch(N)
|
|
120 {
|
|
121 case 2048: return 0;
|
|
122 case 1920: return 1;
|
|
123 case 1024: return 2;
|
|
124 case 960: return 3;
|
|
125 case 256: return 4;
|
|
126 case 240: return 5;
|
|
127 #ifdef SSR_DEC
|
|
128 case 512: return 6;
|
|
129 case 64: return 7;
|
|
130 #endif
|
|
131 }
|
|
132 return 0;
|
|
133 }
|
|
134
|
|
135 mdct_info *faad_mdct_init(uint16_t N)
|
|
136 {
|
|
137 uint16_t k, N_idx;
|
|
138 real_t cangle, sangle, c, s, cold;
|
|
139 real_t scale;
|
|
140
|
|
141 mdct_info *mdct = (mdct_info*)malloc(sizeof(mdct_info));
|
|
142
|
|
143 assert(N % 8 == 0);
|
|
144
|
|
145 mdct->N = N;
|
|
146 mdct->sincos = (complex_t*)malloc(N/4*sizeof(complex_t));
|
|
147 mdct->Z1 = (complex_t*)malloc(N/4*sizeof(complex_t));
|
|
148
|
|
149 N_idx = map_N_to_idx(N);
|
|
150
|
|
151 scale = const_tab[N_idx][0];
|
|
152 cangle = const_tab[N_idx][1];
|
|
153 sangle = const_tab[N_idx][2];
|
|
154 c = const_tab[N_idx][3];
|
|
155 s = const_tab[N_idx][4];
|
|
156
|
10989
|
157 /* (co)sine table build using recurrence relations */
|
|
158 /* this can also be done using static table lookup or */
|
|
159 /* some form of interpolation */
|
10725
|
160 for (k = 0; k < N/4; k++)
|
|
161 {
|
10989
|
162 #if 1
|
10725
|
163 RE(mdct->sincos[k]) = -1*MUL_C_C(c,scale);
|
|
164 IM(mdct->sincos[k]) = -1*MUL_C_C(s,scale);
|
|
165
|
|
166 cold = c;
|
|
167 c = MUL_C_C(c,cangle) - MUL_C_C(s,sangle);
|
|
168 s = MUL_C_C(s,cangle) + MUL_C_C(cold,sangle);
|
10989
|
169 #else
|
|
170 /* no recurrence, just sines */
|
|
171 RE(mdct->sincos[k]) = -scale*cos(2.0*M_PI*(k+1./8.) / (float)N);
|
|
172 IM(mdct->sincos[k]) = -scale*sin(2.0*M_PI*(k+1./8.) / (float)N);
|
|
173 #endif
|
10725
|
174 }
|
|
175
|
|
176 /* initialise fft */
|
|
177 mdct->cfft = cffti(N/4);
|
|
178
|
|
179 return mdct;
|
|
180 }
|
|
181
|
|
182 void faad_mdct_end(mdct_info *mdct)
|
|
183 {
|
|
184 if (mdct != NULL)
|
|
185 {
|
|
186 cfftu(mdct->cfft);
|
|
187
|
|
188 if (mdct->Z1) free(mdct->Z1);
|
|
189 if (mdct->sincos) free(mdct->sincos);
|
|
190
|
|
191 free(mdct);
|
|
192 }
|
|
193 }
|
|
194
|
|
195 void faad_imdct(mdct_info *mdct, real_t *X_in, real_t *X_out)
|
|
196 {
|
|
197 uint16_t k;
|
|
198
|
|
199 complex_t x;
|
|
200 complex_t *Z1 = mdct->Z1;
|
|
201 complex_t *sincos = mdct->sincos;
|
|
202
|
|
203 uint16_t N = mdct->N;
|
|
204 uint16_t N2 = N >> 1;
|
|
205 uint16_t N4 = N >> 2;
|
|
206 uint16_t N8 = N >> 3;
|
|
207
|
|
208 /* pre-IFFT complex multiplication */
|
|
209 for (k = 0; k < N4; k++)
|
|
210 {
|
10989
|
211 RE(Z1[k]) = MUL_R_C(X_in[N2 - 1 - 2*k], RE(sincos[k])) - MUL_R_C(X_in[2*k], IM(sincos[k]));
|
|
212 IM(Z1[k]) = MUL_R_C(X_in[2*k], RE(sincos[k])) + MUL_R_C(X_in[N2 - 1 - 2*k], IM(sincos[k]));
|
10725
|
213 }
|
|
214
|
10989
|
215 /* complex IFFT, any non-scaling FFT can be used here */
|
10725
|
216 cfftb(mdct->cfft, Z1);
|
|
217
|
|
218 /* post-IFFT complex multiplication */
|
|
219 for (k = 0; k < N4; k++)
|
|
220 {
|
|
221 RE(x) = RE(Z1[k]);
|
|
222 IM(x) = IM(Z1[k]);
|
|
223
|
|
224 RE(Z1[k]) = MUL_R_C(RE(x), RE(sincos[k])) - MUL_R_C(IM(x), IM(sincos[k]));
|
|
225 IM(Z1[k]) = MUL_R_C(IM(x), RE(sincos[k])) + MUL_R_C(RE(x), IM(sincos[k]));
|
|
226 }
|
|
227
|
|
228 /* reordering */
|
|
229 for (k = 0; k < N8; k++)
|
|
230 {
|
10989
|
231 X_out[ 2*k] = IM(Z1[N8 + k]);
|
|
232 X_out[ 1 + 2*k] = -RE(Z1[N8 - 1 - k]);
|
|
233 X_out[N4 + 2*k] = RE(Z1[ k]);
|
|
234 X_out[N4 + 1 + 2*k] = -IM(Z1[N4 - 1 - k]);
|
|
235 X_out[N2 + 2*k] = RE(Z1[N8 + k]);
|
|
236 X_out[N2 + 1 + 2*k] = -IM(Z1[N8 - 1 - k]);
|
|
237 X_out[N2 + N4 + 2*k] = -IM(Z1[ k]);
|
|
238 X_out[N2 + N4 + 1 + 2*k] = RE(Z1[N4 - 1 - k]);
|
10725
|
239 }
|
|
240 }
|
|
241
|
|
242 #ifdef LTP_DEC
|
|
243 void faad_mdct(mdct_info *mdct, real_t *X_in, real_t *X_out)
|
|
244 {
|
|
245 uint16_t k;
|
|
246
|
|
247 complex_t x;
|
|
248 complex_t *Z1 = mdct->Z1;
|
|
249 complex_t *sincos = mdct->sincos;
|
|
250
|
|
251 uint16_t N = mdct->N;
|
|
252 uint16_t N2 = N >> 1;
|
|
253 uint16_t N4 = N >> 2;
|
|
254 uint16_t N8 = N >> 3;
|
|
255
|
|
256 real_t scale = REAL_CONST(N);
|
|
257
|
|
258 /* pre-FFT complex multiplication */
|
|
259 for (k = 0; k < N8; k++)
|
|
260 {
|
|
261 uint16_t n = k << 1;
|
|
262 RE(x) = X_in[N - N4 - 1 - n] + X_in[N - N4 + n];
|
|
263 IM(x) = X_in[ N4 + n] - X_in[ N4 - 1 - n];
|
|
264
|
|
265 RE(Z1[k]) = -MUL_R_C(RE(x), RE(sincos[k])) - MUL_R_C(IM(x), IM(sincos[k]));
|
|
266 IM(Z1[k]) = -MUL_R_C(IM(x), RE(sincos[k])) + MUL_R_C(RE(x), IM(sincos[k]));
|
|
267
|
|
268 RE(x) = X_in[N2 - 1 - n] - X_in[ n];
|
|
269 IM(x) = X_in[N2 + n] + X_in[N - 1 - n];
|
|
270
|
|
271 RE(Z1[k + N8]) = -MUL_R_C(RE(x), RE(sincos[k + N8])) - MUL_R_C(IM(x), IM(sincos[k + N8]));
|
|
272 IM(Z1[k + N8]) = -MUL_R_C(IM(x), RE(sincos[k + N8])) + MUL_R_C(RE(x), IM(sincos[k + N8]));
|
|
273 }
|
|
274
|
10989
|
275 /* complex FFT, any non-scaling FFT can be used here */
|
10725
|
276 cfftf(mdct->cfft, Z1);
|
|
277
|
|
278 /* post-FFT complex multiplication */
|
|
279 for (k = 0; k < N4; k++)
|
|
280 {
|
|
281 uint16_t n = k << 1;
|
|
282 RE(x) = MUL(MUL_R_C(RE(Z1[k]), RE(sincos[k])) + MUL_R_C(IM(Z1[k]), IM(sincos[k])), scale);
|
|
283 IM(x) = MUL(MUL_R_C(IM(Z1[k]), RE(sincos[k])) - MUL_R_C(RE(Z1[k]), IM(sincos[k])), scale);
|
|
284
|
|
285 X_out[ n] = RE(x);
|
|
286 X_out[N2 - 1 - n] = -IM(x);
|
|
287 X_out[N2 + n] = IM(x);
|
|
288 X_out[N - 1 - n] = -RE(x);
|
|
289 }
|
|
290 }
|
|
291 #endif
|