00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029 #include "dsputil.h"
00030
00031
00032 DECLARE_ALIGNED_16(FFTSample, ff_cos_16[8]);
00033 DECLARE_ALIGNED_16(FFTSample, ff_cos_32[16]);
00034 DECLARE_ALIGNED_16(FFTSample, ff_cos_64[32]);
00035 DECLARE_ALIGNED_16(FFTSample, ff_cos_128[64]);
00036 DECLARE_ALIGNED_16(FFTSample, ff_cos_256[128]);
00037 DECLARE_ALIGNED_16(FFTSample, ff_cos_512[256]);
00038 DECLARE_ALIGNED_16(FFTSample, ff_cos_1024[512]);
00039 DECLARE_ALIGNED_16(FFTSample, ff_cos_2048[1024]);
00040 DECLARE_ALIGNED_16(FFTSample, ff_cos_4096[2048]);
00041 DECLARE_ALIGNED_16(FFTSample, ff_cos_8192[4096]);
00042 DECLARE_ALIGNED_16(FFTSample, ff_cos_16384[8192]);
00043 DECLARE_ALIGNED_16(FFTSample, ff_cos_32768[16384]);
00044 DECLARE_ALIGNED_16(FFTSample, ff_cos_65536[32768]);
00045 FFTSample *ff_cos_tabs[] = {
00046 ff_cos_16, ff_cos_32, ff_cos_64, ff_cos_128, ff_cos_256, ff_cos_512, ff_cos_1024,
00047 ff_cos_2048, ff_cos_4096, ff_cos_8192, ff_cos_16384, ff_cos_32768, ff_cos_65536,
00048 };
00049
00050 static int split_radix_permutation(int i, int n, int inverse)
00051 {
00052 int m;
00053 if(n <= 2) return i&1;
00054 m = n >> 1;
00055 if(!(i&m)) return split_radix_permutation(i, m, inverse)*2;
00056 m >>= 1;
00057 if(inverse == !(i&m)) return split_radix_permutation(i, m, inverse)*4 + 1;
00058 else return split_radix_permutation(i, m, inverse)*4 - 1;
00059 }
00060
00061 av_cold int ff_fft_init(FFTContext *s, int nbits, int inverse)
00062 {
00063 int i, j, m, n;
00064 float alpha, c1, s1, s2;
00065 int split_radix = 1;
00066 int av_unused has_vectors;
00067
00068 if (nbits < 2 || nbits > 16)
00069 goto fail;
00070 s->nbits = nbits;
00071 n = 1 << nbits;
00072
00073 s->tmp_buf = NULL;
00074 s->exptab = av_malloc((n / 2) * sizeof(FFTComplex));
00075 if (!s->exptab)
00076 goto fail;
00077 s->revtab = av_malloc(n * sizeof(uint16_t));
00078 if (!s->revtab)
00079 goto fail;
00080 s->inverse = inverse;
00081
00082 s2 = inverse ? 1.0 : -1.0;
00083
00084 s->fft_permute = ff_fft_permute_c;
00085 s->fft_calc = ff_fft_calc_c;
00086 s->imdct_calc = ff_imdct_calc_c;
00087 s->imdct_half = ff_imdct_half_c;
00088 s->exptab1 = NULL;
00089
00090 #if HAVE_MMX && HAVE_YASM
00091 has_vectors = mm_support();
00092 if (has_vectors & FF_MM_SSE && HAVE_SSE) {
00093
00094 s->imdct_calc = ff_imdct_calc_sse;
00095 s->imdct_half = ff_imdct_half_sse;
00096 s->fft_permute = ff_fft_permute_sse;
00097 s->fft_calc = ff_fft_calc_sse;
00098 } else if (has_vectors & FF_MM_3DNOWEXT && HAVE_AMD3DNOWEXT) {
00099
00100 s->imdct_calc = ff_imdct_calc_3dn2;
00101 s->imdct_half = ff_imdct_half_3dn2;
00102 s->fft_calc = ff_fft_calc_3dn2;
00103 } else if (has_vectors & FF_MM_3DNOW && HAVE_AMD3DNOW) {
00104
00105 s->imdct_calc = ff_imdct_calc_3dn;
00106 s->imdct_half = ff_imdct_half_3dn;
00107 s->fft_calc = ff_fft_calc_3dn;
00108 }
00109 #elif HAVE_ALTIVEC && !defined ALTIVEC_USE_REFERENCE_C_CODE
00110 has_vectors = mm_support();
00111 if (has_vectors & FF_MM_ALTIVEC) {
00112 s->fft_calc = ff_fft_calc_altivec;
00113 split_radix = 0;
00114 }
00115 #endif
00116
00117 if (split_radix) {
00118 for(j=4; j<=nbits; j++) {
00119 int m = 1<<j;
00120 double freq = 2*M_PI/m;
00121 FFTSample *tab = ff_cos_tabs[j-4];
00122 for(i=0; i<=m/4; i++)
00123 tab[i] = cos(i*freq);
00124 for(i=1; i<m/4; i++)
00125 tab[m/2-i] = tab[i];
00126 }
00127 for(i=0; i<n; i++)
00128 s->revtab[-split_radix_permutation(i, n, s->inverse) & (n-1)] = i;
00129 s->tmp_buf = av_malloc(n * sizeof(FFTComplex));
00130 } else {
00131 int np, nblocks, np2, l;
00132 FFTComplex *q;
00133
00134 for(i=0; i<(n/2); i++) {
00135 alpha = 2 * M_PI * (float)i / (float)n;
00136 c1 = cos(alpha);
00137 s1 = sin(alpha) * s2;
00138 s->exptab[i].re = c1;
00139 s->exptab[i].im = s1;
00140 }
00141
00142 np = 1 << nbits;
00143 nblocks = np >> 3;
00144 np2 = np >> 1;
00145 s->exptab1 = av_malloc(np * 2 * sizeof(FFTComplex));
00146 if (!s->exptab1)
00147 goto fail;
00148 q = s->exptab1;
00149 do {
00150 for(l = 0; l < np2; l += 2 * nblocks) {
00151 *q++ = s->exptab[l];
00152 *q++ = s->exptab[l + nblocks];
00153
00154 q->re = -s->exptab[l].im;
00155 q->im = s->exptab[l].re;
00156 q++;
00157 q->re = -s->exptab[l + nblocks].im;
00158 q->im = s->exptab[l + nblocks].re;
00159 q++;
00160 }
00161 nblocks = nblocks >> 1;
00162 } while (nblocks != 0);
00163 av_freep(&s->exptab);
00164
00165
00166 for(i=0;i<n;i++) {
00167 m=0;
00168 for(j=0;j<nbits;j++) {
00169 m |= ((i >> j) & 1) << (nbits-j-1);
00170 }
00171 s->revtab[i]=m;
00172 }
00173 }
00174
00175 return 0;
00176 fail:
00177 av_freep(&s->revtab);
00178 av_freep(&s->exptab);
00179 av_freep(&s->exptab1);
00180 av_freep(&s->tmp_buf);
00181 return -1;
00182 }
00183
00184 void ff_fft_permute_c(FFTContext *s, FFTComplex *z)
00185 {
00186 int j, k, np;
00187 FFTComplex tmp;
00188 const uint16_t *revtab = s->revtab;
00189 np = 1 << s->nbits;
00190
00191 if (s->tmp_buf) {
00192
00193 for(j=0;j<np;j++) s->tmp_buf[revtab[j]] = z[j];
00194 memcpy(z, s->tmp_buf, np * sizeof(FFTComplex));
00195 return;
00196 }
00197
00198
00199 for(j=0;j<np;j++) {
00200 k = revtab[j];
00201 if (k < j) {
00202 tmp = z[k];
00203 z[k] = z[j];
00204 z[j] = tmp;
00205 }
00206 }
00207 }
00208
00209 av_cold void ff_fft_end(FFTContext *s)
00210 {
00211 av_freep(&s->revtab);
00212 av_freep(&s->exptab);
00213 av_freep(&s->exptab1);
00214 av_freep(&s->tmp_buf);
00215 }
00216
00217 #define sqrthalf (float)M_SQRT1_2
00218
00219 #define BF(x,y,a,b) {\
00220 x = a - b;\
00221 y = a + b;\
00222 }
00223
00224 #define BUTTERFLIES(a0,a1,a2,a3) {\
00225 BF(t3, t5, t5, t1);\
00226 BF(a2.re, a0.re, a0.re, t5);\
00227 BF(a3.im, a1.im, a1.im, t3);\
00228 BF(t4, t6, t2, t6);\
00229 BF(a3.re, a1.re, a1.re, t4);\
00230 BF(a2.im, a0.im, a0.im, t6);\
00231 }
00232
00233
00234
00235
00236 #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
00237 FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
00238 BF(t3, t5, t5, t1);\
00239 BF(a2.re, a0.re, r0, t5);\
00240 BF(a3.im, a1.im, i1, t3);\
00241 BF(t4, t6, t2, t6);\
00242 BF(a3.re, a1.re, r1, t4);\
00243 BF(a2.im, a0.im, i0, t6);\
00244 }
00245
00246 #define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
00247 t1 = a2.re * wre + a2.im * wim;\
00248 t2 = a2.im * wre - a2.re * wim;\
00249 t5 = a3.re * wre - a3.im * wim;\
00250 t6 = a3.im * wre + a3.re * wim;\
00251 BUTTERFLIES(a0,a1,a2,a3)\
00252 }
00253
00254 #define TRANSFORM_ZERO(a0,a1,a2,a3) {\
00255 t1 = a2.re;\
00256 t2 = a2.im;\
00257 t5 = a3.re;\
00258 t6 = a3.im;\
00259 BUTTERFLIES(a0,a1,a2,a3)\
00260 }
00261
00262
00263 #define PASS(name)\
00264 static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
00265 {\
00266 FFTSample t1, t2, t3, t4, t5, t6;\
00267 int o1 = 2*n;\
00268 int o2 = 4*n;\
00269 int o3 = 6*n;\
00270 const FFTSample *wim = wre+o1;\
00271 n--;\
00272 \
00273 TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
00274 TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
00275 do {\
00276 z += 2;\
00277 wre += 2;\
00278 wim -= 2;\
00279 TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
00280 TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
00281 } while(--n);\
00282 }
00283
00284 PASS(pass)
00285 #undef BUTTERFLIES
00286 #define BUTTERFLIES BUTTERFLIES_BIG
00287 PASS(pass_big)
00288
00289 #define DECL_FFT(n,n2,n4)\
00290 static void fft##n(FFTComplex *z)\
00291 {\
00292 fft##n2(z);\
00293 fft##n4(z+n4*2);\
00294 fft##n4(z+n4*3);\
00295 pass(z,ff_cos_##n,n4/2);\
00296 }
00297
00298 static void fft4(FFTComplex *z)
00299 {
00300 FFTSample t1, t2, t3, t4, t5, t6, t7, t8;
00301
00302 BF(t3, t1, z[0].re, z[1].re);
00303 BF(t8, t6, z[3].re, z[2].re);
00304 BF(z[2].re, z[0].re, t1, t6);
00305 BF(t4, t2, z[0].im, z[1].im);
00306 BF(t7, t5, z[2].im, z[3].im);
00307 BF(z[3].im, z[1].im, t4, t8);
00308 BF(z[3].re, z[1].re, t3, t7);
00309 BF(z[2].im, z[0].im, t2, t5);
00310 }
00311
00312 static void fft8(FFTComplex *z)
00313 {
00314 FFTSample t1, t2, t3, t4, t5, t6, t7, t8;
00315
00316 fft4(z);
00317
00318 BF(t1, z[5].re, z[4].re, -z[5].re);
00319 BF(t2, z[5].im, z[4].im, -z[5].im);
00320 BF(t3, z[7].re, z[6].re, -z[7].re);
00321 BF(t4, z[7].im, z[6].im, -z[7].im);
00322 BF(t8, t1, t3, t1);
00323 BF(t7, t2, t2, t4);
00324 BF(z[4].re, z[0].re, z[0].re, t1);
00325 BF(z[4].im, z[0].im, z[0].im, t2);
00326 BF(z[6].re, z[2].re, z[2].re, t7);
00327 BF(z[6].im, z[2].im, z[2].im, t8);
00328
00329 TRANSFORM(z[1],z[3],z[5],z[7],sqrthalf,sqrthalf);
00330 }
00331
00332 #if !CONFIG_SMALL
00333 static void fft16(FFTComplex *z)
00334 {
00335 FFTSample t1, t2, t3, t4, t5, t6;
00336
00337 fft8(z);
00338 fft4(z+8);
00339 fft4(z+12);
00340
00341 TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
00342 TRANSFORM(z[2],z[6],z[10],z[14],sqrthalf,sqrthalf);
00343 TRANSFORM(z[1],z[5],z[9],z[13],ff_cos_16[1],ff_cos_16[3]);
00344 TRANSFORM(z[3],z[7],z[11],z[15],ff_cos_16[3],ff_cos_16[1]);
00345 }
00346 #else
00347 DECL_FFT(16,8,4)
00348 #endif
00349 DECL_FFT(32,16,8)
00350 DECL_FFT(64,32,16)
00351 DECL_FFT(128,64,32)
00352 DECL_FFT(256,128,64)
00353 DECL_FFT(512,256,128)
00354 #if !CONFIG_SMALL
00355 #define pass pass_big
00356 #endif
00357 DECL_FFT(1024,512,256)
00358 DECL_FFT(2048,1024,512)
00359 DECL_FFT(4096,2048,1024)
00360 DECL_FFT(8192,4096,2048)
00361 DECL_FFT(16384,8192,4096)
00362 DECL_FFT(32768,16384,8192)
00363 DECL_FFT(65536,32768,16384)
00364
00365 static void (*fft_dispatch[])(FFTComplex*) = {
00366 fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
00367 fft2048, fft4096, fft8192, fft16384, fft32768, fft65536,
00368 };
00369
00370 void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
00371 {
00372 fft_dispatch[s->nbits-2](z);
00373 }
00374