Mercurial > audlegacy
comparison Plugins/Input/wma/libffwma/fft.c @ 137:b8d4c1faa6d7 trunk
[svn] Import WMA decoder into SVN.
author | nenolod |
---|---|
date | Thu, 10 Nov 2005 14:56:35 -0800 |
parents | |
children | 12004b385a96 |
comparison
equal
deleted
inserted
replaced
136:54218989d664 | 137:b8d4c1faa6d7 |
---|---|
1 /* | |
2 * FFT/IFFT transforms | |
3 * Copyright (c) 2002 Fabrice Bellard. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library 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 GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /** | |
21 * @file fft.c | |
22 * FFT/IFFT transforms. | |
23 */ | |
24 | |
25 #include "dsputil.h" | |
26 | |
27 /** | |
28 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is | |
29 * done | |
30 */ | |
31 int fft_inits(FFTContext *s, int nbits, int inverse) | |
32 { | |
33 int i, j, m, n; | |
34 float alpha, c1, s1, s2; | |
35 | |
36 s->nbits = nbits; | |
37 n = 1 << nbits; | |
38 | |
39 s->exptab = malloc((n / 2) * sizeof(FFTComplex)); | |
40 if (!s->exptab) | |
41 goto fail; | |
42 s->revtab = malloc(n * sizeof(uint16_t)); | |
43 if (!s->revtab) | |
44 goto fail; | |
45 s->inverse = inverse; | |
46 | |
47 s2 = inverse ? 1.0 : -1.0; | |
48 | |
49 for(i=0;i<(n/2);i++) { | |
50 alpha = 2 * M_PI * (float)i / (float)n; | |
51 c1 = cos(alpha); | |
52 s1 = sin(alpha) * s2; | |
53 s->exptab[i].re = c1; | |
54 s->exptab[i].im = s1; | |
55 } | |
56 s->fft_calc = fft_calc_c; | |
57 s->exptab1 = NULL; | |
58 /* compute constant table for HAVE_SSE version */ | |
59 #if (defined(HAVE_MMX) && defined(HAVE_BUILTIN_VECTOR)) || defined(HAVE_ALTIVEC) | |
60 { | |
61 int has_vectors = 0; | |
62 | |
63 #if defined(HAVE_MMX) | |
64 has_vectors = mm_support() & MM_SSE; | |
65 #endif | |
66 #if defined(HAVE_ALTIVEC) && !defined(ALTIVEC_USE_REFERENCE_C_CODE) | |
67 has_vectors = mm_support() & MM_ALTIVEC; | |
68 #endif | |
69 if (has_vectors) { | |
70 int np, nblocks, np2, l; | |
71 FFTComplex *q; | |
72 | |
73 np = 1 << nbits; | |
74 nblocks = np >> 3; | |
75 np2 = np >> 1; | |
76 s->exptab1 = malloc(np * 2 * sizeof(FFTComplex)); | |
77 if (!s->exptab1) | |
78 goto fail; | |
79 q = s->exptab1; | |
80 do { | |
81 for(l = 0; l < np2; l += 2 * nblocks) { | |
82 *q++ = s->exptab[l]; | |
83 *q++ = s->exptab[l + nblocks]; | |
84 | |
85 q->re = -s->exptab[l].im; | |
86 q->im = s->exptab[l].re; | |
87 q++; | |
88 q->re = -s->exptab[l + nblocks].im; | |
89 q->im = s->exptab[l + nblocks].re; | |
90 q++; | |
91 } | |
92 nblocks = nblocks >> 1; | |
93 } while (nblocks != 0); | |
94 av_freep(&s->exptab); | |
95 #if defined(HAVE_MMX) | |
96 s->fft_calc = fft_calc_sse; | |
97 #else | |
98 s->fft_calc = fft_calc_altivec; | |
99 #endif | |
100 } | |
101 } | |
102 #endif | |
103 | |
104 /* compute bit reverse table */ | |
105 | |
106 for(i=0;i<n;i++) { | |
107 m=0; | |
108 for(j=0;j<nbits;j++) { | |
109 m |= ((i >> j) & 1) << (nbits-j-1); | |
110 } | |
111 s->revtab[i]=m; | |
112 } | |
113 return 0; | |
114 fail: | |
115 av_freep(&s->revtab); | |
116 av_freep(&s->exptab); | |
117 av_freep(&s->exptab1); | |
118 return -1; | |
119 } | |
120 | |
121 /* butter fly op */ | |
122 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \ | |
123 {\ | |
124 FFTSample ax, ay, bx, by;\ | |
125 bx=pre1;\ | |
126 by=pim1;\ | |
127 ax=qre1;\ | |
128 ay=qim1;\ | |
129 pre = (bx + ax);\ | |
130 pim = (by + ay);\ | |
131 qre = (bx - ax);\ | |
132 qim = (by - ay);\ | |
133 } | |
134 | |
135 #define MUL16(a,b) ((a) * (b)) | |
136 | |
137 #define CMUL(pre, pim, are, aim, bre, bim) \ | |
138 {\ | |
139 pre = (MUL16(are, bre) - MUL16(aim, bim));\ | |
140 pim = (MUL16(are, bim) + MUL16(bre, aim));\ | |
141 } | |
142 | |
143 /** | |
144 * Do a complex FFT with the parameters defined in fft_init(). The | |
145 * input data must be permuted before with s->revtab table. No | |
146 * 1.0/sqrt(n) normalization is done. | |
147 */ | |
148 void fft_calc_c(FFTContext *s, FFTComplex *z) | |
149 { | |
150 int ln = s->nbits; | |
151 int j, np, np2; | |
152 int nblocks, nloops; | |
153 register FFTComplex *p, *q; | |
154 FFTComplex *exptab = s->exptab; | |
155 int l; | |
156 FFTSample tmp_re, tmp_im; | |
157 | |
158 np = 1 << ln; | |
159 | |
160 /* pass 0 */ | |
161 | |
162 p=&z[0]; | |
163 j=(np >> 1); | |
164 do { | |
165 BF(p[0].re, p[0].im, p[1].re, p[1].im, | |
166 p[0].re, p[0].im, p[1].re, p[1].im); | |
167 p+=2; | |
168 } while (--j != 0); | |
169 | |
170 /* pass 1 */ | |
171 | |
172 | |
173 p=&z[0]; | |
174 j=np >> 2; | |
175 if (s->inverse) { | |
176 do { | |
177 BF(p[0].re, p[0].im, p[2].re, p[2].im, | |
178 p[0].re, p[0].im, p[2].re, p[2].im); | |
179 BF(p[1].re, p[1].im, p[3].re, p[3].im, | |
180 p[1].re, p[1].im, -p[3].im, p[3].re); | |
181 p+=4; | |
182 } while (--j != 0); | |
183 } else { | |
184 do { | |
185 BF(p[0].re, p[0].im, p[2].re, p[2].im, | |
186 p[0].re, p[0].im, p[2].re, p[2].im); | |
187 BF(p[1].re, p[1].im, p[3].re, p[3].im, | |
188 p[1].re, p[1].im, p[3].im, -p[3].re); | |
189 p+=4; | |
190 } while (--j != 0); | |
191 } | |
192 /* pass 2 .. ln-1 */ | |
193 | |
194 nblocks = np >> 3; | |
195 nloops = 1 << 2; | |
196 np2 = np >> 1; | |
197 do { | |
198 p = z; | |
199 q = z + nloops; | |
200 for (j = 0; j < nblocks; ++j) { | |
201 BF(p->re, p->im, q->re, q->im, | |
202 p->re, p->im, q->re, q->im); | |
203 | |
204 p++; | |
205 q++; | |
206 for(l = nblocks; l < np2; l += nblocks) { | |
207 CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im); | |
208 BF(p->re, p->im, q->re, q->im, | |
209 p->re, p->im, tmp_re, tmp_im); | |
210 p++; | |
211 q++; | |
212 } | |
213 | |
214 p += nloops; | |
215 q += nloops; | |
216 } | |
217 nblocks = nblocks >> 1; | |
218 nloops = nloops << 1; | |
219 } while (nblocks != 0); | |
220 } | |
221 | |
222 /** | |
223 * Do the permutation needed BEFORE calling fft_calc() | |
224 */ | |
225 void fft_permute(FFTContext *s, FFTComplex *z) | |
226 { | |
227 int j, k, np; | |
228 FFTComplex tmp; | |
229 const uint16_t *revtab = s->revtab; | |
230 | |
231 /* reverse */ | |
232 np = 1 << s->nbits; | |
233 for(j=0;j<np;j++) { | |
234 k = revtab[j]; | |
235 if (k < j) { | |
236 tmp = z[k]; | |
237 z[k] = z[j]; | |
238 z[j] = tmp; | |
239 } | |
240 } | |
241 } | |
242 | |
243 void fft_end(FFTContext *s) | |
244 { | |
245 av_freep(&s->revtab); | |
246 av_freep(&s->exptab); | |
247 av_freep(&s->exptab1); | |
248 } | |
249 |