Mercurial > libavcodec.hg
annotate fft.c @ 7425:ed615bbf4f8d libavcodec
Change slightly the meaning of RA288Context.phase. This
allows to remove two intermediary buffers and avoid a few
memcpy's.
author | vitor |
---|---|
date | Sun, 27 Jul 2008 20:35:32 +0000 |
parents | fc843d00867c |
children | a8a8205a9081 |
rev | line source |
---|---|
781 | 1 /* |
2 * FFT/IFFT transforms | |
3 * Copyright (c) 2002 Fabrice Bellard. | |
4 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3746
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3746
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3746
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
781 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3746
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
781 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3746
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
781 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3746
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
781 | 20 */ |
1106 | 21 |
22 /** | |
23 * @file fft.c | |
24 * FFT/IFFT transforms. | |
25 */ | |
26 | |
781 | 27 #include "dsputil.h" |
28 | |
29 /** | |
30 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is | |
2967 | 31 * done |
781 | 32 */ |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
33 int ff_fft_init(FFTContext *s, int nbits, int inverse) |
781 | 34 { |
35 int i, j, m, n; | |
36 float alpha, c1, s1, s2; | |
6503 | 37 int shuffle = 0; |
38 int av_unused has_vectors; | |
2967 | 39 |
781 | 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; | |
2967 | 52 |
781 | 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 } | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
60 s->fft_calc = ff_fft_calc_c; |
3555 | 61 s->imdct_calc = ff_imdct_calc; |
7263 | 62 s->imdct_half = ff_imdct_half; |
781 | 63 s->exptab1 = NULL; |
64 | |
6503 | 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; | |
7263 | 71 s->imdct_half = ff_imdct_half_3dn2; |
6503 | 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; | |
7263 | 79 s->imdct_half = ff_imdct_half_sse; |
6503 | 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 | |
781 | 91 |
6503 | 92 /* compute constant table for HAVE_SSE version */ |
6504 | 93 if (shuffle) { |
94 int np, nblocks, np2, l; | |
95 FFTComplex *q; | |
2967 | 96 |
6504 | 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]; | |
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
108 |
6504 | 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 } | |
781 | 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 /** | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
161 * Do a complex FFT with the parameters defined in ff_fft_init(). The |
781 | 162 * input data must be permuted before with s->revtab table. No |
2967 | 163 * 1.0/sqrt(n) normalization is done. |
781 | 164 */ |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
165 void ff_fft_calc_c(FFTContext *s, FFTComplex *z) |
781 | 166 { |
167 int ln = s->nbits; | |
2979 | 168 int j, np, np2; |
169 int nblocks, nloops; | |
781 | 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 { | |
2967 | 182 BF(p[0].re, p[0].im, p[1].re, p[1].im, |
781 | 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 | |
2967 | 189 |
781 | 190 p=&z[0]; |
191 j=np >> 2; | |
192 if (s->inverse) { | |
193 do { | |
2967 | 194 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
781 | 195 p[0].re, p[0].im, p[2].re, p[2].im); |
2967 | 196 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
781 | 197 p[1].re, p[1].im, -p[3].im, p[3].re); |
198 p+=4; | |
199 } while (--j != 0); | |
200 } else { | |
201 do { | |
2967 | 202 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
781 | 203 p[0].re, p[0].im, p[2].re, p[2].im); |
2967 | 204 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
781 | 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); | |
2967 | 220 |
781 | 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 /** | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
240 * Do the permutation needed BEFORE calling ff_fft_calc() |
781 | 241 */ |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
242 void ff_fft_permute(FFTContext *s, FFTComplex *z) |
781 | 243 { |
244 int j, k, np; | |
245 FFTComplex tmp; | |
246 const uint16_t *revtab = s->revtab; | |
2967 | 247 |
781 | 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 | |
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
260 void ff_fft_end(FFTContext *s) |
781 | 261 { |
262 av_freep(&s->revtab); | |
263 av_freep(&s->exptab); | |
264 av_freep(&s->exptab1); | |
265 } | |
266 |