3DNow! & Extended 3DNow! versions of FFT
[libav.git] / libavcodec / fft.c
CommitLineData
bb6f5690
FB
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
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bb6f5690 18 */
983e3246
MN
19
20/**
21 * @file fft.c
22 * FFT/IFFT transforms.
23 */
24
bb6f5690
FB
25#include "dsputil.h"
26
27/**
28 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is
115329f1 29 * done
bb6f5690 30 */
68951ecf 31int ff_fft_init(FFTContext *s, int nbits, int inverse)
bb6f5690
FB
32{
33 int i, j, m, n;
34 float alpha, c1, s1, s2;
115329f1 35
bb6f5690
FB
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;
115329f1 48
bb6f5690
FB
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 }
68951ecf 56 s->fft_calc = ff_fft_calc_c;
bb6f5690
FB
57 s->exptab1 = NULL;
58
59 /* compute constant table for HAVE_SSE version */
82eb4b0f 60#if (defined(HAVE_MMX) && (defined(HAVE_BUILTIN_VECTOR) || defined(HAVE_MM3DNOW))) || defined(HAVE_ALTIVEC)
8d268a7d 61 {
db40a39a 62 int has_vectors = 0;
8d268a7d
FB
63
64#if defined(HAVE_MMX)
82eb4b0f 65 has_vectors = mm_support() & (MM_3DNOW | MM_3DNOWEXT | MM_SSE | MM_SSE2);
e629ab68 66#endif
db40a39a 67#if defined(HAVE_ALTIVEC) && !defined(ALTIVEC_USE_REFERENCE_C_CODE)
e629ab68 68 has_vectors = mm_support() & MM_ALTIVEC;
8d268a7d
FB
69#endif
70 if (has_vectors) {
71 int np, nblocks, np2, l;
72 FFTComplex *q;
115329f1 73
8d268a7d
FB
74 np = 1 << nbits;
75 nblocks = np >> 3;
76 np2 = np >> 1;
77 s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
78 if (!s->exptab1)
79 goto fail;
80 q = s->exptab1;
81 do {
82 for(l = 0; l < np2; l += 2 * nblocks) {
83 *q++ = s->exptab[l];
84 *q++ = s->exptab[l + nblocks];
bb6f5690 85
8d268a7d
FB
86 q->re = -s->exptab[l].im;
87 q->im = s->exptab[l].re;
88 q++;
89 q->re = -s->exptab[l + nblocks].im;
90 q->im = s->exptab[l + nblocks].re;
91 q++;
92 }
93 nblocks = nblocks >> 1;
94 } while (nblocks != 0);
95 av_freep(&s->exptab);
96#if defined(HAVE_MMX)
82eb4b0f
ZM
97#ifdef HAVE_MM3DNOW
98 if (has_vectors & MM_3DNOWEXT)
99 /* 3DNowEx for Athlon(XP) */
100 s->fft_calc = ff_fft_calc_3dn2;
101 else if (has_vectors & MM_3DNOW)
102 /* 3DNow! for K6-2/3 */
103 s->fft_calc = ff_fft_calc_3dn;
104#endif
105#ifdef HAVE_BUILTIN_VECTOR
106 if (has_vectors & MM_SSE2)
107 /* SSE for P4/K8 */
108 s->fft_calc = ff_fft_calc_sse;
109 else if ((has_vectors & MM_SSE) &&
110 s->fft_calc == ff_fft_calc_c)
111 /* SSE for P3 */
112 s->fft_calc = ff_fft_calc_sse;
113#endif
114#else /* HAVE_MMX */
68951ecf 115 s->fft_calc = ff_fft_calc_altivec;
8d268a7d
FB
116#endif
117 }
bb6f5690
FB
118 }
119#endif
120
121 /* compute bit reverse table */
122
123 for(i=0;i<n;i++) {
124 m=0;
125 for(j=0;j<nbits;j++) {
126 m |= ((i >> j) & 1) << (nbits-j-1);
127 }
128 s->revtab[i]=m;
129 }
130 return 0;
131 fail:
132 av_freep(&s->revtab);
133 av_freep(&s->exptab);
134 av_freep(&s->exptab1);
135 return -1;
136}
137
138/* butter fly op */
139#define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
140{\
141 FFTSample ax, ay, bx, by;\
142 bx=pre1;\
143 by=pim1;\
144 ax=qre1;\
145 ay=qim1;\
146 pre = (bx + ax);\
147 pim = (by + ay);\
148 qre = (bx - ax);\
149 qim = (by - ay);\
150}
151
152#define MUL16(a,b) ((a) * (b))
153
154#define CMUL(pre, pim, are, aim, bre, bim) \
155{\
156 pre = (MUL16(are, bre) - MUL16(aim, bim));\
157 pim = (MUL16(are, bim) + MUL16(bre, aim));\
158}
159
160/**
68951ecf 161 * Do a complex FFT with the parameters defined in ff_fft_init(). The
bb6f5690 162 * input data must be permuted before with s->revtab table. No
115329f1 163 * 1.0/sqrt(n) normalization is done.
bb6f5690 164 */
68951ecf 165void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
bb6f5690
FB
166{
167 int ln = s->nbits;
bb270c08
DB
168 int j, np, np2;
169 int nblocks, nloops;
bb6f5690
FB
170 register FFTComplex *p, *q;
171 FFTComplex *exptab = s->exptab;
172 int l;
173 FFTSample tmp_re, tmp_im;
174
175 np = 1 << ln;
176
177 /* pass 0 */
178
179 p=&z[0];
180 j=(np >> 1);
181 do {
115329f1 182 BF(p[0].re, p[0].im, p[1].re, p[1].im,
bb6f5690
FB
183 p[0].re, p[0].im, p[1].re, p[1].im);
184 p+=2;
185 } while (--j != 0);
186
187 /* pass 1 */
188
115329f1 189
bb6f5690
FB
190 p=&z[0];
191 j=np >> 2;
192 if (s->inverse) {
193 do {
115329f1 194 BF(p[0].re, p[0].im, p[2].re, p[2].im,
bb6f5690 195 p[0].re, p[0].im, p[2].re, p[2].im);
115329f1 196 BF(p[1].re, p[1].im, p[3].re, p[3].im,
bb6f5690
FB
197 p[1].re, p[1].im, -p[3].im, p[3].re);
198 p+=4;
199 } while (--j != 0);
200 } else {
201 do {
115329f1 202 BF(p[0].re, p[0].im, p[2].re, p[2].im,
bb6f5690 203 p[0].re, p[0].im, p[2].re, p[2].im);
115329f1 204 BF(p[1].re, p[1].im, p[3].re, p[3].im,
bb6f5690
FB
205 p[1].re, p[1].im, p[3].im, -p[3].re);
206 p+=4;
207 } while (--j != 0);
208 }
209 /* pass 2 .. ln-1 */
210
211 nblocks = np >> 3;
212 nloops = 1 << 2;
213 np2 = np >> 1;
214 do {
215 p = z;
216 q = z + nloops;
217 for (j = 0; j < nblocks; ++j) {
218 BF(p->re, p->im, q->re, q->im,
219 p->re, p->im, q->re, q->im);
115329f1 220
bb6f5690
FB
221 p++;
222 q++;
223 for(l = nblocks; l < np2; l += nblocks) {
224 CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
225 BF(p->re, p->im, q->re, q->im,
226 p->re, p->im, tmp_re, tmp_im);
227 p++;
228 q++;
229 }
230
231 p += nloops;
232 q += nloops;
233 }
234 nblocks = nblocks >> 1;
235 nloops = nloops << 1;
236 } while (nblocks != 0);
237}
238
239/**
68951ecf 240 * Do the permutation needed BEFORE calling ff_fft_calc()
bb6f5690 241 */
68951ecf 242void ff_fft_permute(FFTContext *s, FFTComplex *z)
bb6f5690
FB
243{
244 int j, k, np;
245 FFTComplex tmp;
246 const uint16_t *revtab = s->revtab;
115329f1 247
bb6f5690
FB
248 /* reverse */
249 np = 1 << s->nbits;
250 for(j=0;j<np;j++) {
251 k = revtab[j];
252 if (k < j) {
253 tmp = z[k];
254 z[k] = z[j];
255 z[j] = tmp;
256 }
257 }
258}
259
68951ecf 260void ff_fft_end(FFTContext *s)
bb6f5690
FB
261{
262 av_freep(&s->revtab);
263 av_freep(&s->exptab);
264 av_freep(&s->exptab1);
265}
266