2
|
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 **
|
|
25 ** $Id: mdct.c,v 1.34 2003/11/12 20:47:58 menno Exp $
|
|
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 #ifdef FIXED_POINT
|
|
65 real_t const_tab[][5] =
|
|
66 {
|
|
67 { /* 2048 */
|
|
68 COEF_CONST(1),
|
|
69 FRAC_CONST(0.99999529380957619),
|
|
70 FRAC_CONST(0.0030679567629659761),
|
|
71 FRAC_CONST(0.99999992646571789),
|
|
72 FRAC_CONST(0.00038349518757139556)
|
|
73 }, { /* 1920 */
|
|
74 COEF_CONST(/* sqrt(1024/960) */ 1.0327955589886444),
|
|
75 FRAC_CONST(0.99999464540169647),
|
|
76 FRAC_CONST(0.0032724865065266251),
|
|
77 FRAC_CONST(0.99999991633432805),
|
|
78 FRAC_CONST(0.00040906153202803459)
|
|
79 }, { /* 1024 */
|
|
80 COEF_CONST(1),
|
|
81 FRAC_CONST(0.99998117528260111),
|
|
82 FRAC_CONST(0.0061358846491544753),
|
|
83 FRAC_CONST(0.99999970586288223),
|
|
84 FRAC_CONST(0.00076699031874270449)
|
|
85 }, { /* 960 */
|
|
86 COEF_CONST(/* sqrt(512/480) */ 1.0327955589886444),
|
|
87 FRAC_CONST(0.99997858166412923),
|
|
88 FRAC_CONST(0.0065449379673518581),
|
|
89 FRAC_CONST(0.99999966533732598),
|
|
90 FRAC_CONST(0.00081812299560725323)
|
|
91 }, { /* 256 */
|
|
92 COEF_CONST(1),
|
|
93 FRAC_CONST(0.99969881869620425),
|
|
94 FRAC_CONST(0.024541228522912288),
|
|
95 FRAC_CONST(0.99999529380957619),
|
|
96 FRAC_CONST(0.0030679567629659761)
|
|
97 }, { /* 240 */
|
|
98 COEF_CONST(/* sqrt(256/240) */ 1.0327955589886444),
|
|
99 FRAC_CONST(0.99965732497555726),
|
|
100 FRAC_CONST(0.026176948307873149),
|
|
101 FRAC_CONST(0.99999464540169647),
|
|
102 FRAC_CONST(0.0032724865065266251)
|
|
103 }
|
|
104 #ifdef SSR_DEC
|
|
105 ,{ /* 512 */
|
|
106 COEF_CONST(1),
|
|
107 FRAC_CONST(0.9999247018391445),
|
|
108 FRAC_CONST(0.012271538285719925),
|
|
109 FRAC_CONST(0.99999882345170188),
|
|
110 FRAC_CONST(0.0015339801862847655)
|
|
111 }, { /* 64 */
|
|
112 COEF_CONST(1),
|
|
113 FRAC_CONST(0.99518472667219693),
|
|
114 FRAC_CONST(0.098017140329560604),
|
|
115 FRAC_CONST(0.9999247018391445),
|
|
116 FRAC_CONST(0.012271538285719925)
|
|
117 }
|
|
118 #endif
|
|
119 };
|
|
120 #endif
|
|
121
|
|
122 uint8_t map_N_to_idx(uint16_t N)
|
|
123 {
|
|
124 /* gives an index into const_tab above */
|
|
125 /* for normal AAC deocding (eg. no scalable profile) only */
|
|
126 /* index 0 and 4 will be used */
|
|
127 switch(N)
|
|
128 {
|
|
129 case 2048: return 0;
|
|
130 case 1920: return 1;
|
|
131 case 1024: return 2;
|
|
132 case 960: return 3;
|
|
133 case 256: return 4;
|
|
134 case 240: return 5;
|
|
135 #ifdef SSR_DEC
|
|
136 case 512: return 6;
|
|
137 case 64: return 7;
|
|
138 #endif
|
|
139 }
|
|
140 return 0;
|
|
141 }
|
|
142
|
|
143 mdct_info *faad_mdct_init(uint16_t N)
|
|
144 {
|
|
145 uint16_t k;
|
|
146 #ifdef FIXED_POINT
|
|
147 uint16_t N_idx;
|
|
148 real_t cangle, sangle, c, s, cold;
|
|
149 #endif
|
|
150 real_t scale;
|
|
151
|
|
152 mdct_info *mdct = (mdct_info*)malloc(sizeof(mdct_info));
|
|
153
|
|
154 assert(N % 8 == 0);
|
|
155
|
|
156 mdct->N = N;
|
|
157 mdct->sincos = (complex_t*)malloc(N/4*sizeof(complex_t));
|
|
158
|
|
159 #ifdef FIXED_POINT
|
|
160 N_idx = map_N_to_idx(N);
|
|
161
|
|
162 scale = const_tab[N_idx][0];
|
|
163 cangle = const_tab[N_idx][1];
|
|
164 sangle = const_tab[N_idx][2];
|
|
165 c = const_tab[N_idx][3];
|
|
166 s = const_tab[N_idx][4];
|
|
167 #else
|
|
168 scale = (real_t)sqrt(2.0 / (real_t)N);
|
|
169 #endif
|
|
170
|
|
171 /* (co)sine table build using recurrence relations */
|
|
172 /* this can also be done using static table lookup or */
|
|
173 /* some form of interpolation */
|
|
174 for (k = 0; k < N/4; k++)
|
|
175 {
|
|
176 #ifdef FIXED_POINT
|
|
177 RE(mdct->sincos[k]) = c; //MUL_C_C(c,scale);
|
|
178 IM(mdct->sincos[k]) = s; //MUL_C_C(s,scale);
|
|
179
|
|
180 cold = c;
|
|
181 c = MUL_F(c,cangle) - MUL_F(s,sangle);
|
|
182 s = MUL_F(s,cangle) + MUL_F(cold,sangle);
|
|
183 #else
|
|
184 /* no recurrence, just sines */
|
|
185 RE(mdct->sincos[k]) = scale*(real_t)(cos(2.0*M_PI*(k+1./8.) / (real_t)N));
|
|
186 IM(mdct->sincos[k]) = scale*(real_t)(sin(2.0*M_PI*(k+1./8.) / (real_t)N));
|
|
187 #endif
|
|
188 }
|
|
189
|
|
190 /* initialise fft */
|
|
191 mdct->cfft = cffti(N/4);
|
|
192
|
|
193 return mdct;
|
|
194 }
|
|
195
|
|
196 void faad_mdct_end(mdct_info *mdct)
|
|
197 {
|
|
198 if (mdct != NULL)
|
|
199 {
|
|
200 cfftu(mdct->cfft);
|
|
201
|
|
202 if (mdct->sincos) free(mdct->sincos);
|
|
203
|
|
204 free(mdct);
|
|
205 }
|
|
206 }
|
|
207
|
|
208 void faad_imdct(mdct_info *mdct, real_t *X_in, real_t *X_out)
|
|
209 {
|
|
210 uint16_t k;
|
|
211
|
|
212 complex_t x;
|
|
213 complex_t Z1[512];
|
|
214 complex_t *sincos = mdct->sincos;
|
|
215
|
|
216 uint16_t N = mdct->N;
|
|
217 uint16_t N2 = N >> 1;
|
|
218 uint16_t N4 = N >> 2;
|
|
219 uint16_t N8 = N >> 3;
|
|
220
|
|
221 /* pre-IFFT complex multiplication */
|
|
222 for (k = 0; k < N4; k++)
|
|
223 {
|
|
224 ComplexMult(&IM(Z1[k]), &RE(Z1[k]),
|
|
225 X_in[2*k], X_in[N2 - 1 - 2*k], RE(sincos[k]), IM(sincos[k]));
|
|
226 }
|
|
227
|
|
228 /* complex IFFT, any non-scaling FFT can be used here */
|
|
229 cfftb(mdct->cfft, Z1);
|
|
230
|
|
231 /* post-IFFT complex multiplication */
|
|
232 for (k = 0; k < N4; k++)
|
|
233 {
|
|
234 RE(x) = RE(Z1[k]);
|
|
235 IM(x) = IM(Z1[k]);
|
|
236 ComplexMult(&IM(Z1[k]), &RE(Z1[k]),
|
|
237 IM(x), RE(x), RE(sincos[k]), IM(sincos[k]));
|
|
238
|
|
239 #ifdef FIXED_POINT
|
|
240 #if (REAL_BITS == 16)
|
|
241 if (abs(RE(Z1[k])) > REAL_CONST(16383.5))
|
|
242 {
|
|
243 if (RE(Z1[k]) > 0) RE(Z1[k]) = REAL_CONST(32767.0);
|
|
244 else RE(Z1[k]) = REAL_CONST(-32767.0);
|
|
245 } else {
|
|
246 RE(Z1[k]) *= 2;
|
|
247 }
|
|
248 if (abs(IM(Z1[k])) > REAL_CONST(16383.5))
|
|
249 {
|
|
250 if (IM(Z1[k]) > 0) IM(Z1[k]) = REAL_CONST(32767.0);
|
|
251 else IM(Z1[k]) = REAL_CONST(-32767.0);
|
|
252 } else {
|
|
253 IM(Z1[k]) *= 2;
|
|
254 }
|
|
255 #endif
|
|
256 #endif
|
|
257 }
|
|
258
|
|
259 /* reordering */
|
|
260 for (k = 0; k < N8; k++)
|
|
261 {
|
|
262 X_out[ 2*k] = IM(Z1[N8 + k]);
|
|
263 X_out[ 1 + 2*k] = -RE(Z1[N8 - 1 - k]);
|
|
264 X_out[N4 + 2*k] = RE(Z1[ k]);
|
|
265 X_out[N4 + 1 + 2*k] = -IM(Z1[N4 - 1 - k]);
|
|
266 X_out[N2 + 2*k] = RE(Z1[N8 + k]);
|
|
267 X_out[N2 + 1 + 2*k] = -IM(Z1[N8 - 1 - k]);
|
|
268 X_out[N2 + N4 + 2*k] = -IM(Z1[ k]);
|
|
269 X_out[N2 + N4 + 1 + 2*k] = RE(Z1[N4 - 1 - k]);
|
|
270 }
|
|
271 }
|
|
272
|
|
273 #ifdef LTP_DEC
|
|
274 void faad_mdct(mdct_info *mdct, real_t *X_in, real_t *X_out)
|
|
275 {
|
|
276 uint16_t k;
|
|
277
|
|
278 complex_t x;
|
|
279 complex_t Z1[512];
|
|
280 complex_t *sincos = mdct->sincos;
|
|
281
|
|
282 uint16_t N = mdct->N;
|
|
283 uint16_t N2 = N >> 1;
|
|
284 uint16_t N4 = N >> 2;
|
|
285 uint16_t N8 = N >> 3;
|
|
286
|
|
287 #ifndef FIXED_POINT
|
|
288 real_t scale = REAL_CONST(N);
|
|
289 #else
|
|
290 real_t scale = REAL_CONST(4.0/N);
|
|
291 #endif
|
|
292
|
|
293 /* pre-FFT complex multiplication */
|
|
294 for (k = 0; k < N8; k++)
|
|
295 {
|
|
296 uint16_t n = k << 1;
|
|
297 RE(x) = X_in[N - N4 - 1 - n] + X_in[N - N4 + n];
|
|
298 IM(x) = X_in[ N4 + n] - X_in[ N4 - 1 - n];
|
|
299
|
|
300 ComplexMult(&RE(Z1[k]), &IM(Z1[k]),
|
|
301 RE(x), IM(x), RE(sincos[k]), IM(sincos[k]));
|
|
302
|
|
303 RE(Z1[k]) = MUL_R(RE(Z1[k]), scale);
|
|
304 IM(Z1[k]) = MUL_R(IM(Z1[k]), scale);
|
|
305
|
|
306 RE(x) = X_in[N2 - 1 - n] - X_in[ n];
|
|
307 IM(x) = X_in[N2 + n] + X_in[N - 1 - n];
|
|
308
|
|
309 ComplexMult(&RE(Z1[k + N8]), &IM(Z1[k + N8]),
|
|
310 RE(x), IM(x), RE(sincos[k + N8]), IM(sincos[k + N8]));
|
|
311
|
|
312 RE(Z1[k + N8]) = MUL_R(RE(Z1[k + N8]), scale);
|
|
313 IM(Z1[k + N8]) = MUL_R(IM(Z1[k + N8]), scale);
|
|
314 }
|
|
315
|
|
316 /* complex FFT, any non-scaling FFT can be used here */
|
|
317 cfftf(mdct->cfft, Z1);
|
|
318
|
|
319 /* post-FFT complex multiplication */
|
|
320 for (k = 0; k < N4; k++)
|
|
321 {
|
|
322 uint16_t n = k << 1;
|
|
323 ComplexMult(&RE(x), &IM(x),
|
|
324 RE(Z1[k]), IM(Z1[k]), RE(sincos[k]), IM(sincos[k]));
|
|
325
|
|
326 X_out[ n] = -RE(x);
|
|
327 X_out[N2 - 1 - n] = IM(x);
|
|
328 X_out[N2 + n] = -IM(x);
|
|
329 X_out[N - 1 - n] = RE(x);
|
|
330 }
|
|
331 }
|
|
332 #endif
|