3 * Copyright (c) 2002 Fabrice Bellard.
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.
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.
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
22 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is
25 int fft_init(FFTContext *s, int nbits, int inverse)
28 float alpha, c1, s1, s2;
33 s->exptab = av_malloc((n / 2) * sizeof(FFTComplex));
36 s->revtab = av_malloc(n * sizeof(uint16_t));
41 s2 = inverse ? 1.0 : -1.0;
43 for(i=0;i<(n/2);i++) {
44 alpha = 2 * M_PI * (float)i / (float)n;
50 s->fft_calc = fft_calc_c;
53 /* compute constant table for HAVE_SSE version */
54 #if defined(HAVE_MMX) && 0
55 if (mm_flags & MM_SSE) {
56 int np, nblocks, np2, l;
62 s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
67 for(l = 0; l < np2; l += 2 * nblocks) {
69 *q++ = s->exptab[l + nblocks];
71 q->re = -s->exptab[l].im;
72 q->im = s->exptab[l].re;
74 q->re = -s->exptab[l + nblocks].im;
75 q->im = s->exptab[l + nblocks].re;
78 nblocks = nblocks >> 1;
79 } while (nblocks != 0);
84 /* compute bit reverse table */
88 for(j=0;j<nbits;j++) {
89 m |= ((i >> j) & 1) << (nbits-j-1);
97 av_freep(&s->exptab1);
102 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \
104 FFTSample ax, ay, bx, by;\
115 #define MUL16(a,b) ((a) * (b))
117 #define CMUL(pre, pim, are, aim, bre, bim) \
119 pre = (MUL16(are, bre) - MUL16(aim, bim));\
120 pim = (MUL16(are, bim) + MUL16(bre, aim));\
124 * Do a complex FFT with the parameters defined in fft_init(). The
125 * input data must be permuted before with s->revtab table. No
126 * 1.0/sqrt(n) normalization is done.
128 void fft_calc_c(FFTContext *s, FFTComplex *z)
133 register FFTComplex *p, *q;
134 FFTComplex *exptab = s->exptab;
136 FFTSample tmp_re, tmp_im;
145 BF(p[0].re, p[0].im, p[1].re, p[1].im,
146 p[0].re, p[0].im, p[1].re, p[1].im);
157 BF(p[0].re, p[0].im, p[2].re, p[2].im,
158 p[0].re, p[0].im, p[2].re, p[2].im);
159 BF(p[1].re, p[1].im, p[3].re, p[3].im,
160 p[1].re, p[1].im, -p[3].im, p[3].re);
165 BF(p[0].re, p[0].im, p[2].re, p[2].im,
166 p[0].re, p[0].im, p[2].re, p[2].im);
167 BF(p[1].re, p[1].im, p[3].re, p[3].im,
168 p[1].re, p[1].im, p[3].im, -p[3].re);
180 for (j = 0; j < nblocks; ++j) {
181 BF(p->re, p->im, q->re, q->im,
182 p->re, p->im, q->re, q->im);
186 for(l = nblocks; l < np2; l += nblocks) {
187 CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im);
188 BF(p->re, p->im, q->re, q->im,
189 p->re, p->im, tmp_re, tmp_im);
197 nblocks = nblocks >> 1;
198 nloops = nloops << 1;
199 } while (nblocks != 0);
203 * Do the permutation needed BEFORE calling fft_calc()
205 void fft_permute(FFTContext *s, FFTComplex *z)
209 const uint16_t *revtab = s->revtab;
223 void fft_end(FFTContext *s)
225 av_freep(&s->revtab);
226 av_freep(&s->exptab);
227 av_freep(&s->exptab1);