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
|
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 */
|
|
19 #include "dsputil.h"
|
|
20
|
|
21 /**
|
|
22 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is
|
|
23 * done
|
|
24 */
|
|
25 int fft_init(FFTContext *s, int nbits, int inverse)
|
|
26 {
|
|
27 int i, j, m, n;
|
|
28 float alpha, c1, s1, s2;
|
|
29
|
|
30 s->nbits = nbits;
|
|
31 n = 1 << nbits;
|
|
32
|
|
33 s->exptab = av_malloc((n / 2) * sizeof(FFTComplex));
|
|
34 if (!s->exptab)
|
|
35 goto fail;
|
|
36 s->revtab = av_malloc(n * sizeof(uint16_t));
|
|
37 if (!s->revtab)
|
|
38 goto fail;
|
|
39 s->inverse = inverse;
|
|
40
|
|
41 s2 = inverse ? 1.0 : -1.0;
|
|
42
|
|
43 for(i=0;i<(n/2);i++) {
|
|
44 alpha = 2 * M_PI * (float)i / (float)n;
|
|
45 c1 = cos(alpha);
|
|
46 s1 = sin(alpha) * s2;
|
|
47 s->exptab[i].re = c1;
|
|
48 s->exptab[i].im = s1;
|
|
49 }
|
|
50 s->fft_calc = fft_calc_c;
|
|
51 s->exptab1 = NULL;
|
|
52
|
|
53 /* compute constant table for HAVE_SSE version */
|
971
|
54 #if defined(HAVE_MMX) && defined(HAVE_BUILTIN_VECTOR)
|
|
55 if (mm_support() & MM_SSE) {
|
781
|
56 int np, nblocks, np2, l;
|
|
57 FFTComplex *q;
|
971
|
58
|
781
|
59 np = 1 << nbits;
|
|
60 nblocks = np >> 3;
|
|
61 np2 = np >> 1;
|
|
62 s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
|
|
63 if (!s->exptab1)
|
|
64 goto fail;
|
|
65 q = s->exptab1;
|
|
66 do {
|
|
67 for(l = 0; l < np2; l += 2 * nblocks) {
|
|
68 *q++ = s->exptab[l];
|
|
69 *q++ = s->exptab[l + nblocks];
|
|
70
|
|
71 q->re = -s->exptab[l].im;
|
|
72 q->im = s->exptab[l].re;
|
|
73 q++;
|
|
74 q->re = -s->exptab[l + nblocks].im;
|
|
75 q->im = s->exptab[l + nblocks].re;
|
|
76 q++;
|
|
77 }
|
|
78 nblocks = nblocks >> 1;
|
|
79 } while (nblocks != 0);
|
|
80 av_freep(&s->exptab);
|
971
|
81 s->fft_calc = fft_calc_sse;
|
781
|
82 }
|
|
83 #endif
|
|
84
|
|
85 /* compute bit reverse table */
|
|
86
|
|
87 for(i=0;i<n;i++) {
|
|
88 m=0;
|
|
89 for(j=0;j<nbits;j++) {
|
|
90 m |= ((i >> j) & 1) << (nbits-j-1);
|
|
91 }
|
|
92 s->revtab[i]=m;
|
|
93 }
|
|
94 return 0;
|
|
95 fail:
|
|
96 av_freep(&s->revtab);
|
|
97 av_freep(&s->exptab);
|
|
98 av_freep(&s->exptab1);
|
|
99 return -1;
|
|
100 }
|
|
101
|
|
102 /* butter fly op */
|
|
103 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
|
|
104 {\
|
|
105 FFTSample ax, ay, bx, by;\
|
|
106 bx=pre1;\
|
|
107 by=pim1;\
|
|
108 ax=qre1;\
|
|
109 ay=qim1;\
|
|
110 pre = (bx + ax);\
|
|
111 pim = (by + ay);\
|
|
112 qre = (bx - ax);\
|
|
113 qim = (by - ay);\
|
|
114 }
|
|
115
|
|
116 #define MUL16(a,b) ((a) * (b))
|
|
117
|
|
118 #define CMUL(pre, pim, are, aim, bre, bim) \
|
|
119 {\
|
|
120 pre = (MUL16(are, bre) - MUL16(aim, bim));\
|
|
121 pim = (MUL16(are, bim) + MUL16(bre, aim));\
|
|
122 }
|
|
123
|
|
124 /**
|
|
125 * Do a complex FFT with the parameters defined in fft_init(). The
|
|
126 * input data must be permuted before with s->revtab table. No
|
|
127 * 1.0/sqrt(n) normalization is done.
|
|
128 */
|
|
129 void fft_calc_c(FFTContext *s, FFTComplex *z)
|
|
130 {
|
|
131 int ln = s->nbits;
|
|
132 int j, np, np2;
|
|
133 int nblocks, nloops;
|
|
134 register FFTComplex *p, *q;
|
|
135 FFTComplex *exptab = s->exptab;
|
|
136 int l;
|
|
137 FFTSample tmp_re, tmp_im;
|
|
138
|
|
139 np = 1 << ln;
|
|
140
|
|
141 /* pass 0 */
|
|
142
|
|
143 p=&z[0];
|
|
144 j=(np >> 1);
|
|
145 do {
|
|
146 BF(p[0].re, p[0].im, p[1].re, p[1].im,
|
|
147 p[0].re, p[0].im, p[1].re, p[1].im);
|
|
148 p+=2;
|
|
149 } while (--j != 0);
|
|
150
|
|
151 /* pass 1 */
|
|
152
|
|
153
|
|
154 p=&z[0];
|
|
155 j=np >> 2;
|
|
156 if (s->inverse) {
|
|
157 do {
|
|
158 BF(p[0].re, p[0].im, p[2].re, p[2].im,
|
|
159 p[0].re, p[0].im, p[2].re, p[2].im);
|
|
160 BF(p[1].re, p[1].im, p[3].re, p[3].im,
|
|
161 p[1].re, p[1].im, -p[3].im, p[3].re);
|
|
162 p+=4;
|
|
163 } while (--j != 0);
|
|
164 } else {
|
|
165 do {
|
|
166 BF(p[0].re, p[0].im, p[2].re, p[2].im,
|
|
167 p[0].re, p[0].im, p[2].re, p[2].im);
|
|
168 BF(p[1].re, p[1].im, p[3].re, p[3].im,
|
|
169 p[1].re, p[1].im, p[3].im, -p[3].re);
|
|
170 p+=4;
|
|
171 } while (--j != 0);
|
|
172 }
|
|
173 /* pass 2 .. ln-1 */
|
|
174
|
|
175 nblocks = np >> 3;
|
|
176 nloops = 1 << 2;
|
|
177 np2 = np >> 1;
|
|
178 do {
|
|
179 p = z;
|
|
180 q = z + nloops;
|
|
181 for (j = 0; j < nblocks; ++j) {
|
|
182 BF(p->re, p->im, q->re, q->im,
|
|
183 p->re, p->im, q->re, q->im);
|
|
184
|
|
185 p++;
|
|
186 q++;
|
|
187 for(l = nblocks; l < np2; l += nblocks) {
|
|
188 CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
|
|
189 BF(p->re, p->im, q->re, q->im,
|
|
190 p->re, p->im, tmp_re, tmp_im);
|
|
191 p++;
|
|
192 q++;
|
|
193 }
|
|
194
|
|
195 p += nloops;
|
|
196 q += nloops;
|
|
197 }
|
|
198 nblocks = nblocks >> 1;
|
|
199 nloops = nloops << 1;
|
|
200 } while (nblocks != 0);
|
|
201 }
|
|
202
|
|
203 /**
|
|
204 * Do the permutation needed BEFORE calling fft_calc()
|
|
205 */
|
|
206 void fft_permute(FFTContext *s, FFTComplex *z)
|
|
207 {
|
|
208 int j, k, np;
|
|
209 FFTComplex tmp;
|
|
210 const uint16_t *revtab = s->revtab;
|
|
211
|
|
212 /* reverse */
|
|
213 np = 1 << s->nbits;
|
|
214 for(j=0;j<np;j++) {
|
|
215 k = revtab[j];
|
|
216 if (k < j) {
|
|
217 tmp = z[k];
|
|
218 z[k] = z[j];
|
|
219 z[j] = tmp;
|
|
220 }
|
|
221 }
|
|
222 }
|
|
223
|
|
224 void fft_end(FFTContext *s)
|
|
225 {
|
|
226 av_freep(&s->revtab);
|
|
227 av_freep(&s->exptab);
|
|
228 av_freep(&s->exptab1);
|
|
229 }
|
|
230
|