Mercurial > libavcodec.hg
annotate fft.c @ 3591:088be7d7c4fd libavcodec
use ff_fft_calc_3dn2 instead of sse on a K8.
1.5% faster vorbis.
| author | lorenm |
|---|---|
| date | Sat, 19 Aug 2006 02:42:20 +0000 |
| parents | a3d97c60ea07 |
| children | 2ec498208c6a |
| rev | line source |
|---|---|
| 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 | |
|
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2979
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 781 | 18 */ |
| 1106 | 19 |
| 20 /** | |
| 21 * @file fft.c | |
| 22 * FFT/IFFT transforms. | |
| 23 */ | |
| 24 | |
| 781 | 25 #include "dsputil.h" |
| 26 | |
| 27 /** | |
| 28 * The size of the FFT is 2^nbits. If inverse is TRUE, inverse FFT is | |
| 2967 | 29 * done |
| 781 | 30 */ |
|
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
31 int ff_fft_init(FFTContext *s, int nbits, int inverse) |
| 781 | 32 { |
| 33 int i, j, m, n; | |
| 34 float alpha, c1, s1, s2; | |
| 2967 | 35 |
| 781 | 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; | |
| 2967 | 48 |
| 781 | 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 } | |
|
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
56 s->fft_calc = ff_fft_calc_c; |
| 3555 | 57 s->imdct_calc = ff_imdct_calc; |
| 781 | 58 s->exptab1 = NULL; |
| 59 | |
| 60 /* compute constant table for HAVE_SSE version */ | |
|
3590
a3d97c60ea07
ff_fft_calc_3dn/3dn2/sse: convert intrinsics to inline asm.
lorenm
parents:
3567
diff
changeset
|
61 #if defined(HAVE_MMX) \ |
|
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
62 || (defined(HAVE_ALTIVEC) && !defined(ALTIVEC_USE_REFERENCE_C_CODE)) |
|
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
63 { |
|
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
64 int has_vectors = mm_support(); |
| 781 | 65 |
|
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
66 if (has_vectors) { |
|
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
67 #if defined(HAVE_MMX) |
|
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
68 if (has_vectors & MM_3DNOWEXT) |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
69 s->imdct_calc = ff_imdct_calc_3dn2; |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
70 if (has_vectors & MM_3DNOWEXT) |
| 3591 | 71 /* 3DNowEx for K7/K8 */ |
|
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
72 s->fft_calc = ff_fft_calc_3dn2; |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
73 else if (has_vectors & MM_3DNOW) |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
74 /* 3DNow! for K6-2/3 */ |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
75 s->fft_calc = ff_fft_calc_3dn; |
| 3591 | 76 else if (has_vectors & MM_SSE) |
| 77 /* SSE for P3/P4 */ | |
|
3567
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
78 s->fft_calc = ff_fft_calc_sse; |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
79 #else /* HAVE_MMX */ |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
80 if (has_vectors & MM_ALTIVEC) |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
81 s->fft_calc = ff_fft_calc_altivec; |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
82 #endif |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
83 } |
|
1f8730f62765
r5954 broke fft on cpus with 3dnow but without mm3dnow.h
lorenm
parents:
3555
diff
changeset
|
84 if (s->fft_calc != ff_fft_calc_c) { |
|
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
85 int np, nblocks, np2, l; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
86 FFTComplex *q; |
| 2967 | 87 |
|
975
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
88 np = 1 << nbits; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
89 nblocks = np >> 3; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
90 np2 = np >> 1; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
91 s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex)); |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
92 if (!s->exptab1) |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
93 goto fail; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
94 q = s->exptab1; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
95 do { |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
96 for(l = 0; l < np2; l += 2 * nblocks) { |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
97 *q++ = s->exptab[l]; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
98 *q++ = s->exptab[l + nblocks]; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
99 |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
100 q->re = -s->exptab[l].im; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
101 q->im = s->exptab[l].re; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
102 q++; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
103 q->re = -s->exptab[l + nblocks].im; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
104 q->im = s->exptab[l + nblocks].re; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
105 q++; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
106 } |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
107 nblocks = nblocks >> 1; |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
108 } while (nblocks != 0); |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
109 av_freep(&s->exptab); |
|
e05d525505c5
fft altivec by Romain Dolbeau - simplified patch, test it on PPC with fft-test and wma decoding
bellard
parents:
971
diff
changeset
|
110 } |
| 781 | 111 } |
| 112 #endif | |
| 113 | |
| 114 /* compute bit reverse table */ | |
| 115 | |
| 116 for(i=0;i<n;i++) { | |
| 117 m=0; | |
| 118 for(j=0;j<nbits;j++) { | |
| 119 m |= ((i >> j) & 1) << (nbits-j-1); | |
| 120 } | |
| 121 s->revtab[i]=m; | |
| 122 } | |
| 123 return 0; | |
| 124 fail: | |
| 125 av_freep(&s->revtab); | |
| 126 av_freep(&s->exptab); | |
| 127 av_freep(&s->exptab1); | |
| 128 return -1; | |
| 129 } | |
| 130 | |
| 131 /* butter fly op */ | |
| 132 #define BF(pre, pim, qre, qim, pre1, pim1, qre1, qim1) \ | |
| 133 {\ | |
| 134 FFTSample ax, ay, bx, by;\ | |
| 135 bx=pre1;\ | |
| 136 by=pim1;\ | |
| 137 ax=qre1;\ | |
| 138 ay=qim1;\ | |
| 139 pre = (bx + ax);\ | |
| 140 pim = (by + ay);\ | |
| 141 qre = (bx - ax);\ | |
| 142 qim = (by - ay);\ | |
| 143 } | |
| 144 | |
| 145 #define MUL16(a,b) ((a) * (b)) | |
| 146 | |
| 147 #define CMUL(pre, pim, are, aim, bre, bim) \ | |
| 148 {\ | |
| 149 pre = (MUL16(are, bre) - MUL16(aim, bim));\ | |
| 150 pim = (MUL16(are, bim) + MUL16(bre, aim));\ | |
| 151 } | |
| 152 | |
| 153 /** | |
|
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
154 * Do a complex FFT with the parameters defined in ff_fft_init(). The |
| 781 | 155 * input data must be permuted before with s->revtab table. No |
| 2967 | 156 * 1.0/sqrt(n) normalization is done. |
| 781 | 157 */ |
|
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
158 void ff_fft_calc_c(FFTContext *s, FFTComplex *z) |
| 781 | 159 { |
| 160 int ln = s->nbits; | |
| 2979 | 161 int j, np, np2; |
| 162 int nblocks, nloops; | |
| 781 | 163 register FFTComplex *p, *q; |
| 164 FFTComplex *exptab = s->exptab; | |
| 165 int l; | |
| 166 FFTSample tmp_re, tmp_im; | |
| 167 | |
| 168 np = 1 << ln; | |
| 169 | |
| 170 /* pass 0 */ | |
| 171 | |
| 172 p=&z[0]; | |
| 173 j=(np >> 1); | |
| 174 do { | |
| 2967 | 175 BF(p[0].re, p[0].im, p[1].re, p[1].im, |
| 781 | 176 p[0].re, p[0].im, p[1].re, p[1].im); |
| 177 p+=2; | |
| 178 } while (--j != 0); | |
| 179 | |
| 180 /* pass 1 */ | |
| 181 | |
| 2967 | 182 |
| 781 | 183 p=&z[0]; |
| 184 j=np >> 2; | |
| 185 if (s->inverse) { | |
| 186 do { | |
| 2967 | 187 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
| 781 | 188 p[0].re, p[0].im, p[2].re, p[2].im); |
| 2967 | 189 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
| 781 | 190 p[1].re, p[1].im, -p[3].im, p[3].re); |
| 191 p+=4; | |
| 192 } while (--j != 0); | |
| 193 } else { | |
| 194 do { | |
| 2967 | 195 BF(p[0].re, p[0].im, p[2].re, p[2].im, |
| 781 | 196 p[0].re, p[0].im, p[2].re, p[2].im); |
| 2967 | 197 BF(p[1].re, p[1].im, p[3].re, p[3].im, |
| 781 | 198 p[1].re, p[1].im, p[3].im, -p[3].re); |
| 199 p+=4; | |
| 200 } while (--j != 0); | |
| 201 } | |
| 202 /* pass 2 .. ln-1 */ | |
| 203 | |
| 204 nblocks = np >> 3; | |
| 205 nloops = 1 << 2; | |
| 206 np2 = np >> 1; | |
| 207 do { | |
| 208 p = z; | |
| 209 q = z + nloops; | |
| 210 for (j = 0; j < nblocks; ++j) { | |
| 211 BF(p->re, p->im, q->re, q->im, | |
| 212 p->re, p->im, q->re, q->im); | |
| 2967 | 213 |
| 781 | 214 p++; |
| 215 q++; | |
| 216 for(l = nblocks; l < np2; l += nblocks) { | |
| 217 CMUL(tmp_re, tmp_im, exptab[l].re, exptab[l].im, q->re, q->im); | |
| 218 BF(p->re, p->im, q->re, q->im, | |
| 219 p->re, p->im, tmp_re, tmp_im); | |
| 220 p++; | |
| 221 q++; | |
| 222 } | |
| 223 | |
| 224 p += nloops; | |
| 225 q += nloops; | |
| 226 } | |
| 227 nblocks = nblocks >> 1; | |
| 228 nloops = nloops << 1; | |
| 229 } while (nblocks != 0); | |
| 230 } | |
| 231 | |
| 232 /** | |
|
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
233 * Do the permutation needed BEFORE calling ff_fft_calc() |
| 781 | 234 */ |
|
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
235 void ff_fft_permute(FFTContext *s, FFTComplex *z) |
| 781 | 236 { |
| 237 int j, k, np; | |
| 238 FFTComplex tmp; | |
| 239 const uint16_t *revtab = s->revtab; | |
| 2967 | 240 |
| 781 | 241 /* reverse */ |
| 242 np = 1 << s->nbits; | |
| 243 for(j=0;j<np;j++) { | |
| 244 k = revtab[j]; | |
| 245 if (k < j) { | |
| 246 tmp = z[k]; | |
| 247 z[k] = z[j]; | |
| 248 z[j] = tmp; | |
| 249 } | |
| 250 } | |
| 251 } | |
| 252 | |
|
1879
dd63cb7e5080
fft_*() renamed into ff_fft_*() patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
1106
diff
changeset
|
253 void ff_fft_end(FFTContext *s) |
| 781 | 254 { |
| 255 av_freep(&s->revtab); | |
| 256 av_freep(&s->exptab); | |
| 257 av_freep(&s->exptab1); | |
| 258 } | |
| 259 |
