Mercurial > libavcodec.hg
annotate fft.c @ 3555:5ea82888103e libavcodec
3dnow2 implementation of imdct.
6% faster vorbis and wma.
author | lorenm |
---|---|
date | Tue, 08 Aug 2006 04:01:04 +0000 |
parents | 769d68b12d26 |
children | 1f8730f62765 |
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 */ | |
3175 | 61 #if (defined(HAVE_MMX) && (defined(HAVE_BUILTIN_VECTOR) || defined(HAVE_MM3DNOW))) || defined(HAVE_ALTIVEC) |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
62 { |
1009
3b7cc8e4b83f
AltiVec perf (take 2), plus a couple AltiVec functions by (Romain Dolbeau <dolbeau at irisa dot fr>)
michaelni
parents:
995
diff
changeset
|
63 int has_vectors = 0; |
781 | 64 |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
65 #if defined(HAVE_MMX) |
3175 | 66 has_vectors = mm_support() & (MM_3DNOW | MM_3DNOWEXT | MM_SSE | MM_SSE2); |
995
edc10966b081
altivec jumbo patch by (Romain Dolbeau <dolbeaur at club-internet dot fr>)
michaelni
parents:
975
diff
changeset
|
67 #endif |
1009
3b7cc8e4b83f
AltiVec perf (take 2), plus a couple AltiVec functions by (Romain Dolbeau <dolbeau at irisa dot fr>)
michaelni
parents:
995
diff
changeset
|
68 #if defined(HAVE_ALTIVEC) && !defined(ALTIVEC_USE_REFERENCE_C_CODE) |
995
edc10966b081
altivec jumbo patch by (Romain Dolbeau <dolbeaur at club-internet dot fr>)
michaelni
parents:
975
diff
changeset
|
69 has_vectors = mm_support() & MM_ALTIVEC; |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
70 #endif |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
71 if (has_vectors) { |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
72 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
|
73 FFTComplex *q; |
2967 | 74 |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 do { |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
83 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
|
84 *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
|
85 *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
|
86 |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
87 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
|
88 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
|
89 q++; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
90 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
|
91 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
|
92 q++; |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
93 } |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
94 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
|
95 } 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
|
96 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
|
97 #if defined(HAVE_MMX) |
3555 | 98 if (has_vectors & MM_3DNOWEXT) |
99 s->imdct_calc = ff_imdct_calc_3dn2; | |
3175 | 100 #ifdef HAVE_MM3DNOW |
101 if (has_vectors & MM_3DNOWEXT) | |
102 /* 3DNowEx for Athlon(XP) */ | |
103 s->fft_calc = ff_fft_calc_3dn2; | |
104 else if (has_vectors & MM_3DNOW) | |
105 /* 3DNow! for K6-2/3 */ | |
106 s->fft_calc = ff_fft_calc_3dn; | |
107 #endif | |
108 #ifdef HAVE_BUILTIN_VECTOR | |
109 if (has_vectors & MM_SSE2) | |
110 /* SSE for P4/K8 */ | |
111 s->fft_calc = ff_fft_calc_sse; | |
112 else if ((has_vectors & MM_SSE) && | |
113 s->fft_calc == ff_fft_calc_c) | |
114 /* SSE for P3 */ | |
115 s->fft_calc = ff_fft_calc_sse; | |
116 #endif | |
117 #else /* HAVE_MMX */ | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
118 s->fft_calc = ff_fft_calc_altivec; |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
119 #endif |
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
120 } |
781 | 121 } |
122 #endif | |
123 | |
124 /* compute bit reverse table */ | |
125 | |
126 for(i=0;i<n;i++) { | |
127 m=0; | |
128 for(j=0;j<nbits;j++) { | |
129 m |= ((i >> j) & 1) << (nbits-j-1); | |
130 } | |
131 s->revtab[i]=m; | |
132 } | |
133 return 0; | |
134 fail: | |
135 av_freep(&s->revtab); | |
136 av_freep(&s->exptab); | |
137 av_freep(&s->exptab1); | |
138 return -1; | |
139 } | |
140 | |
141 /* butter fly op */ | |
142 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \ | |
143 {\ | |
144 FFTSample ax, ay, bx, by;\ | |
145 bx=pre1;\ | |
146 by=pim1;\ | |
147 ax=qre1;\ | |
148 ay=qim1;\ | |
149 pre = (bx + ax);\ | |
150 pim = (by + ay);\ | |
151 qre = (bx - ax);\ | |
152 qim = (by - ay);\ | |
153 } | |
154 | |
155 #define MUL16(a,b) ((a) * (b)) | |
156 | |
157 #define CMUL(pre, pim, are, aim, bre, bim) \ | |
158 {\ | |
159 pre = (MUL16(are, bre) - MUL16(aim, bim));\ | |
160 pim = (MUL16(are, bim) + MUL16(bre, aim));\ | |
161 } | |
162 | |
163 /** | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
164 * Do a complex FFT with the parameters defined in ff_fft_init(). The |
781 | 165 * input data must be permuted before with s->revtab table. No |
2967 | 166 * 1.0/sqrt(n) normalization is done. |
781 | 167 */ |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
168 void ff_fft_calc_c(FFTContext *s, FFTComplex *z) |
781 | 169 { |
170 int ln = s->nbits; | |
2979 | 171 int j, np, np2; |
172 int nblocks, nloops; | |
781 | 173 register FFTComplex *p, *q; |
174 FFTComplex *exptab = s->exptab; | |
175 int l; | |
176 FFTSample tmp_re, tmp_im; | |
177 | |
178 np = 1 << ln; | |
179 | |
180 /* pass 0 */ | |
181 | |
182 p=&z[0]; | |
183 j=(np >> 1); | |
184 do { | |
2967 | 185 BF(p[0].re, p[0].im, p[1].re, p[1].im, |
781 | 186 p[0].re, p[0].im, p[1].re, p[1].im); |
187 p+=2; | |
188 } while (--j != 0); | |
189 | |
190 /* pass 1 */ | |
191 | |
2967 | 192 |
781 | 193 p=&z[0]; |
194 j=np >> 2; | |
195 if (s->inverse) { | |
196 do { | |
2967 | 197 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
781 | 198 p[0].re, p[0].im, p[2].re, p[2].im); |
2967 | 199 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
781 | 200 p[1].re, p[1].im, -p[3].im, p[3].re); |
201 p+=4; | |
202 } while (--j != 0); | |
203 } else { | |
204 do { | |
2967 | 205 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
781 | 206 p[0].re, p[0].im, p[2].re, p[2].im); |
2967 | 207 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
781 | 208 p[1].re, p[1].im, p[3].im, -p[3].re); |
209 p+=4; | |
210 } while (--j != 0); | |
211 } | |
212 /* pass 2 .. ln-1 */ | |
213 | |
214 nblocks = np >> 3; | |
215 nloops = 1 << 2; | |
216 np2 = np >> 1; | |
217 do { | |
218 p = z; | |
219 q = z + nloops; | |
220 for (j = 0; j < nblocks; ++j) { | |
221 BF(p->re, p->im, q->re, q->im, | |
222 p->re, p->im, q->re, q->im); | |
2967 | 223 |
781 | 224 p++; |
225 q++; | |
226 for(l = nblocks; l < np2; l += nblocks) { | |
227 CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im); | |
228 BF(p->re, p->im, q->re, q->im, | |
229 p->re, p->im, tmp_re, tmp_im); | |
230 p++; | |
231 q++; | |
232 } | |
233 | |
234 p += nloops; | |
235 q += nloops; | |
236 } | |
237 nblocks = nblocks >> 1; | |
238 nloops = nloops << 1; | |
239 } while (nblocks != 0); | |
240 } | |
241 | |
242 /** | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
243 * Do the permutation needed BEFORE calling ff_fft_calc() |
781 | 244 */ |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
245 void ff_fft_permute(FFTContext *s, FFTComplex *z) |
781 | 246 { |
247 int j, k, np; | |
248 FFTComplex tmp; | |
249 const uint16_t *revtab = s->revtab; | |
2967 | 250 |
781 | 251 /* reverse */ |
252 np = 1 << s->nbits; | |
253 for(j=0;j<np;j++) { | |
254 k = revtab[j]; | |
255 if (k < j) { | |
256 tmp = z[k]; | |
257 z[k] = z[j]; | |
258 z[j] = tmp; | |
259 } | |
260 } | |
261 } | |
262 | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
263 void ff_fft_end(FFTContext *s) |
781 | 264 { |
265 av_freep(&s->revtab); | |
266 av_freep(&s->exptab); | |
267 av_freep(&s->exptab1); | |
268 } | |
269 |