47e9e062f4845693c7bf3a169bdd800ee1ea8d91
[libav.git] / libavcodec / fft.c
1 /*
2 * FFT/IFFT transforms
3 * Copyright (c) 2002 Fabrice Bellard.
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file fft.c
24 * FFT/IFFT transforms.
25 */
26
27 #include "dsputil.h"
28
29 /**
30 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is
31 * done
32 */
33 int ff_fft_init(FFTContext *s, int nbits, int inverse)
34 {
35 int i, j, m, n;
36 float alpha, c1, s1, s2;
37 int shuffle = 0;
38 int av_unused has_vectors;
39
40 s->nbits = nbits;
41 n = 1 << nbits;
42
43 s->exptab = av_malloc((n / 2) * sizeof(FFTComplex));
44 if (!s->exptab)
45 goto fail;
46 s->revtab = av_malloc(n * sizeof(uint16_t));
47 if (!s->revtab)
48 goto fail;
49 s->inverse = inverse;
50
51 s2 = inverse ? 1.0 : -1.0;
52
53 for(i=0;i<(n/2);i++) {
54 alpha = 2 * M_PI * (float)i / (float)n;
55 c1 = cos(alpha);
56 s1 = sin(alpha) * s2;
57 s->exptab[i].re = c1;
58 s->exptab[i].im = s1;
59 }
60 s->fft_calc = ff_fft_calc_c;
61 s->imdct_calc = ff_imdct_calc;
62 s->imdct_half = ff_imdct_half;
63 s->exptab1 = NULL;
64
65 #ifdef HAVE_MMX
66 has_vectors = mm_support();
67 shuffle = 1;
68 if (has_vectors & MM_3DNOWEXT) {
69 /* 3DNowEx for K7/K8 */
70 s->imdct_calc = ff_imdct_calc_3dn2;
71 s->imdct_half = ff_imdct_half_3dn2;
72 s->fft_calc = ff_fft_calc_3dn2;
73 } else if (has_vectors & MM_3DNOW) {
74 /* 3DNow! for K6-2/3 */
75 s->fft_calc = ff_fft_calc_3dn;
76 } else if (has_vectors & MM_SSE) {
77 /* SSE for P3/P4 */
78 s->imdct_calc = ff_imdct_calc_sse;
79 s->imdct_half = ff_imdct_half_sse;
80 s->fft_calc = ff_fft_calc_sse;
81 } else {
82 shuffle = 0;
83 }
84 #elif defined HAVE_ALTIVEC && !defined ALTIVEC_USE_REFERENCE_C_CODE
85 has_vectors = mm_support();
86 if (has_vectors & MM_ALTIVEC) {
87 s->fft_calc = ff_fft_calc_altivec;
88 shuffle = 1;
89 }
90 #endif
91
92 /* compute constant table for HAVE_SSE version */
93 if (shuffle) {
94 int np, nblocks, np2, l;
95 FFTComplex *q;
96
97 np = 1 << nbits;
98 nblocks = np >> 3;
99 np2 = np >> 1;
100 s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
101 if (!s->exptab1)
102 goto fail;
103 q = s->exptab1;
104 do {
105 for(l = 0; l < np2; l += 2 * nblocks) {
106 *q++ = s->exptab[l];
107 *q++ = s->exptab[l + nblocks];
108
109 q->re = -s->exptab[l].im;
110 q->im = s->exptab[l].re;
111 q++;
112 q->re = -s->exptab[l + nblocks].im;
113 q->im = s->exptab[l + nblocks].re;
114 q++;
115 }
116 nblocks = nblocks >> 1;
117 } while (nblocks != 0);
118 av_freep(&s->exptab);
119 }
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 /**
161 * Do a complex FFT with the parameters defined in ff_fft_init(). The
162 * input data must be permuted before with s->revtab table. No
163 * 1.0/sqrt(n) normalization is done.
164 */
165 void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
166 {
167 int ln = s->nbits;
168 int j, np, np2;
169 int nblocks, nloops;
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 {
182 BF(p[0].re, p[0].im, p[1].re, p[1].im,
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
189
190 p=&z[0];
191 j=np >> 2;
192 if (s->inverse) {
193 do {
194 BF(p[0].re, p[0].im, p[2].re, p[2].im,
195 p[0].re, p[0].im, p[2].re, p[2].im);
196 BF(p[1].re, p[1].im, p[3].re, p[3].im,
197 p[1].re, p[1].im, -p[3].im, p[3].re);
198 p+=4;
199 } while (--j != 0);
200 } else {
201 do {
202 BF(p[0].re, p[0].im, p[2].re, p[2].im,
203 p[0].re, p[0].im, p[2].re, p[2].im);
204 BF(p[1].re, p[1].im, p[3].re, p[3].im,
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);
220
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 /**
240 * Do the permutation needed BEFORE calling ff_fft_calc()
241 */
242 void ff_fft_permute(FFTContext *s, FFTComplex *z)
243 {
244 int j, k, np;
245 FFTComplex tmp;
246 const uint16_t *revtab = s->revtab;
247
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
260 void ff_fft_end(FFTContext *s)
261 {
262 av_freep(&s->revtab);
263 av_freep(&s->exptab);
264 av_freep(&s->exptab1);
265 }
266