Mercurial > libavcodec.hg
annotate fft.c @ 3572:d5f97ae4f24f libavcodec
keep in sync with dsputil, makes --disable-decoder=vorbis build
author | lu_zero |
---|---|
date | Fri, 11 Aug 2006 02:56:32 +0000 |
parents | 1f8730f62765 |
children | a3d97c60ea07 |
rev | line source |
---|---|
781 | 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 | |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
781 | 18 */ |
1106 | 19 |
20 /** | |
21 * @file fft.c | |
22 * FFT/IFFT transforms. | |
23 */ | |
24 | |
781 | 25 #include "dsputil.h" |
26 | |
27 /** | |
28 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is | |
2967 | 29 * done |
781 | 30 */ |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
31 int ff_fft_init(FFTContext *s, int nbits, int inverse) |
781 | 32 { |
33 int i, j, m, n; | |
34 float alpha, c1, s1, s2; | |
2967 | 35 |
781 | 36 s->nbits = nbits; |
37 n = 1 << nbits; | |
38 | |
39 s->exptab = av_malloc((n / 2) * sizeof(FFTComplex)); | |
40 if (!s->exptab) | |
41 goto fail; | |
42 s->revtab = av_malloc(n * sizeof(uint16_t)); | |
43 if (!s->revtab) | |
44 goto fail; | |
45 s->inverse = inverse; | |
46 | |
47 s2 = inverse ? 1.0 : -1.0; | |
2967 | 48 |
781 | 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 } | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
56 s->fft_calc = ff_fft_calc_c; |
3555 | 57 s->imdct_calc = ff_imdct_calc; |
781 | 58 s->exptab1 = NULL; |
59 | |
60 /* compute constant table for HAVE_SSE version */ | |
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
61 #if (defined(HAVE_MMX) && (defined(HAVE_BUILTIN_VECTOR) || defined(HAVE_MM3DNOW))) \ |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
62 || (defined(HAVE_ALTIVEC) && !defined(ALTIVEC_USE_REFERENCE_C_CODE)) |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
63 { |
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
64 int has_vectors = mm_support(); |
781 | 65 |
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
66 if (has_vectors) { |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
67 #if defined(HAVE_MMX) |
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
68 if (has_vectors & MM_3DNOWEXT) |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
69 s->imdct_calc = ff_imdct_calc_3dn2; |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
70 #ifdef HAVE_MM3DNOW |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
71 if (has_vectors & MM_3DNOWEXT) |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
72 /* 3DNowEx for Athlon(XP) */ |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
73 s->fft_calc = ff_fft_calc_3dn2; |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
74 else if (has_vectors & MM_3DNOW) |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
75 /* 3DNow! for K6-2/3 */ |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
76 s->fft_calc = ff_fft_calc_3dn; |
995
edc10966b081
altivec jumbo patch by (Romain Dolbeau <dolbeaur at club-internet dot fr>)
michaelni
parents:
975
diff
changeset
|
77 #endif |
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
78 #ifdef HAVE_BUILTIN_VECTOR |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
79 if (has_vectors & MM_SSE2) |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
80 /* SSE for P4/K8 */ |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
81 s->fft_calc = ff_fft_calc_sse; |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
82 else if ((has_vectors & MM_SSE) && |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
83 s->fft_calc == ff_fft_calc_c) |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
84 /* SSE for P3 */ |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
85 s->fft_calc = ff_fft_calc_sse; |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
86 #endif |
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
87 #else /* HAVE_MMX */ |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
88 if (has_vectors & MM_ALTIVEC) |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
89 s->fft_calc = ff_fft_calc_altivec; |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
90 #endif |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
91 } |
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
92 if (s->fft_calc != ff_fft_calc_c) { |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
93 int np, nblocks, np2, l; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
94 FFTComplex *q; |
2967 | 95 |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
96 np = 1 << nbits; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
97 nblocks = np >> 3; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
98 np2 = np >> 1; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
99 s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex)); |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
100 if (!s->exptab1) |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
101 goto fail; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
102 q = s->exptab1; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
103 do { |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
104 for(l = 0; l < np2; l += 2 * nblocks) { |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
105 *q++ = s->exptab[l]; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
106 *q++ = s->exptab[l + nblocks]; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
107 |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
108 q->re = -s->exptab[l].im; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
109 q->im = s->exptab[l].re; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
110 q++; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
111 q->re = -s->exptab[l + nblocks].im; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
112 q->im = s->exptab[l + nblocks].re; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
113 q++; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
114 } |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
115 nblocks = nblocks >> 1; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
116 } while (nblocks != 0); |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
117 av_freep(&s->exptab); |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
118 } |
781 | 119 } |
120 #endif | |
121 | |
122 /* compute bit reverse table */ | |
123 | |
124 for(i=0;i<n;i++) { | |
125 m=0; | |
126 for(j=0;j<nbits;j++) { | |
127 m |= ((i >> j) & 1) << (nbits-j-1); | |
128 } | |
129 s->revtab[i]=m; | |
130 } | |
131 return 0; | |
132 fail: | |
133 av_freep(&s->revtab); | |
134 av_freep(&s->exptab); | |
135 av_freep(&s->exptab1); | |
136 return -1; | |
137 } | |
138 | |
139 /* butter fly op */ | |
140 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \ | |
141 {\ | |
142 FFTSample ax, ay, bx, by;\ | |
143 bx=pre1;\ | |
144 by=pim1;\ | |
145 ax=qre1;\ | |
146 ay=qim1;\ | |
147 pre = (bx + ax);\ | |
148 pim = (by + ay);\ | |
149 qre = (bx - ax);\ | |
150 qim = (by - ay);\ | |
151 } | |
152 | |
153 #define MUL16(a,b) ((a) * (b)) | |
154 | |
155 #define CMUL(pre, pim, are, aim, bre, bim) \ | |
156 {\ | |
157 pre = (MUL16(are, bre) - MUL16(aim, bim));\ | |
158 pim = (MUL16(are, bim) + MUL16(bre, aim));\ | |
159 } | |
160 | |
161 /** | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
162 * Do a complex FFT with the parameters defined in ff_fft_init(). The |
781 | 163 * input data must be permuted before with s->revtab table. No |
2967 | 164 * 1.0/sqrt(n) normalization is done. |
781 | 165 */ |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
166 void ff_fft_calc_c(FFTContext *s, FFTComplex *z) |
781 | 167 { |
168 int ln = s->nbits; | |
2979 | 169 int j, np, np2; |
170 int nblocks, nloops; | |
781 | 171 register FFTComplex *p, *q; |
172 FFTComplex *exptab = s->exptab; | |
173 int l; | |
174 FFTSample tmp_re, tmp_im; | |
175 | |
176 np = 1 << ln; | |
177 | |
178 /* pass 0 */ | |
179 | |
180 p=&z[0]; | |
181 j=(np >> 1); | |
182 do { | |
2967 | 183 BF(p[0].re, p[0].im, p[1].re, p[1].im, |
781 | 184 p[0].re, p[0].im, p[1].re, p[1].im); |
185 p+=2; | |
186 } while (--j != 0); | |
187 | |
188 /* pass 1 */ | |
189 | |
2967 | 190 |
781 | 191 p=&z[0]; |
192 j=np >> 2; | |
193 if (s->inverse) { | |
194 do { | |
2967 | 195 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
781 | 196 p[0].re, p[0].im, p[2].re, p[2].im); |
2967 | 197 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
781 | 198 p[1].re, p[1].im, -p[3].im, p[3].re); |
199 p+=4; | |
200 } while (--j != 0); | |
201 } else { | |
202 do { | |
2967 | 203 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
781 | 204 p[0].re, p[0].im, p[2].re, p[2].im); |
2967 | 205 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
781 | 206 p[1].re, p[1].im, p[3].im, -p[3].re); |
207 p+=4; | |
208 } while (--j != 0); | |
209 } | |
210 /* pass 2 .. ln-1 */ | |
211 | |
212 nblocks = np >> 3; | |
213 nloops = 1 << 2; | |
214 np2 = np >> 1; | |
215 do { | |
216 p = z; | |
217 q = z + nloops; | |
218 for (j = 0; j < nblocks; ++j) { | |
219 BF(p->re, p->im, q->re, q->im, | |
220 p->re, p->im, q->re, q->im); | |
2967 | 221 |
781 | 222 p++; |
223 q++; | |
224 for(l = nblocks; l < np2; l += nblocks) { | |
225 CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im); | |
226 BF(p->re, p->im, q->re, q->im, | |
227 p->re, p->im, tmp_re, tmp_im); | |
228 p++; | |
229 q++; | |
230 } | |
231 | |
232 p += nloops; | |
233 q += nloops; | |
234 } | |
235 nblocks = nblocks >> 1; | |
236 nloops = nloops << 1; | |
237 } while (nblocks != 0); | |
238 } | |
239 | |
240 /** | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
241 * Do the permutation needed BEFORE calling ff_fft_calc() |
781 | 242 */ |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
243 void ff_fft_permute(FFTContext *s, FFTComplex *z) |
781 | 244 { |
245 int j, k, np; | |
246 FFTComplex tmp; | |
247 const uint16_t *revtab = s->revtab; | |
2967 | 248 |
781 | 249 /* reverse */ |
250 np = 1 << s->nbits; | |
251 for(j=0;j<np;j++) { | |
252 k = revtab[j]; | |
253 if (k < j) { | |
254 tmp = z[k]; | |
255 z[k] = z[j]; | |
256 z[j] = tmp; | |
257 } | |
258 } | |
259 } | |
260 | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
261 void ff_fft_end(FFTContext *s) |
781 | 262 { |
263 av_freep(&s->revtab); | |
264 av_freep(&s->exptab); | |
265 av_freep(&s->exptab1); | |
266 } | |
267 |