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