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 <stdlib.h>
00030 #include <string.h>
00031 #include "libavutil/mathematics.h"
00032 #include "fft.h"
00033 #include "fft-internal.h"
00034
00035
00036 #if !CONFIG_HARDCODED_TABLES
00037 COSTABLE(16);
00038 COSTABLE(32);
00039 COSTABLE(64);
00040 COSTABLE(128);
00041 COSTABLE(256);
00042 COSTABLE(512);
00043 COSTABLE(1024);
00044 COSTABLE(2048);
00045 COSTABLE(4096);
00046 COSTABLE(8192);
00047 COSTABLE(16384);
00048 COSTABLE(32768);
00049 COSTABLE(65536);
00050 #endif
00051 COSTABLE_CONST FFTSample * const FFT_NAME(ff_cos_tabs)[] = {
00052 NULL, NULL, NULL, NULL,
00053 FFT_NAME(ff_cos_16),
00054 FFT_NAME(ff_cos_32),
00055 FFT_NAME(ff_cos_64),
00056 FFT_NAME(ff_cos_128),
00057 FFT_NAME(ff_cos_256),
00058 FFT_NAME(ff_cos_512),
00059 FFT_NAME(ff_cos_1024),
00060 FFT_NAME(ff_cos_2048),
00061 FFT_NAME(ff_cos_4096),
00062 FFT_NAME(ff_cos_8192),
00063 FFT_NAME(ff_cos_16384),
00064 FFT_NAME(ff_cos_32768),
00065 FFT_NAME(ff_cos_65536),
00066 };
00067
00068 static void ff_fft_permute_c(FFTContext *s, FFTComplex *z);
00069 static void ff_fft_calc_c(FFTContext *s, FFTComplex *z);
00070
00071 static int split_radix_permutation(int i, int n, int inverse)
00072 {
00073 int m;
00074 if(n <= 2) return i&1;
00075 m = n >> 1;
00076 if(!(i&m)) return split_radix_permutation(i, m, inverse)*2;
00077 m >>= 1;
00078 if(inverse == !(i&m)) return split_radix_permutation(i, m, inverse)*4 + 1;
00079 else return split_radix_permutation(i, m, inverse)*4 - 1;
00080 }
00081
00082 av_cold void ff_init_ff_cos_tabs(int index)
00083 {
00084 #if !CONFIG_HARDCODED_TABLES
00085 int i;
00086 int m = 1<<index;
00087 double freq = 2*M_PI/m;
00088 FFTSample *tab = FFT_NAME(ff_cos_tabs)[index];
00089 for(i=0; i<=m/4; i++)
00090 tab[i] = FIX15(cos(i*freq));
00091 for(i=1; i<m/4; i++)
00092 tab[m/2-i] = tab[i];
00093 #endif
00094 }
00095
00096 static const int avx_tab[] = {
00097 0, 4, 1, 5, 8, 12, 9, 13, 2, 6, 3, 7, 10, 14, 11, 15
00098 };
00099
00100 static int is_second_half_of_fft32(int i, int n)
00101 {
00102 if (n <= 32)
00103 return i >= 16;
00104 else if (i < n/2)
00105 return is_second_half_of_fft32(i, n/2);
00106 else if (i < 3*n/4)
00107 return is_second_half_of_fft32(i - n/2, n/4);
00108 else
00109 return is_second_half_of_fft32(i - 3*n/4, n/4);
00110 }
00111
00112 static av_cold void fft_perm_avx(FFTContext *s)
00113 {
00114 int i;
00115 int n = 1 << s->nbits;
00116
00117 for (i = 0; i < n; i += 16) {
00118 int k;
00119 if (is_second_half_of_fft32(i, n)) {
00120 for (k = 0; k < 16; k++)
00121 s->revtab[-split_radix_permutation(i + k, n, s->inverse) & (n - 1)] =
00122 i + avx_tab[k];
00123
00124 } else {
00125 for (k = 0; k < 16; k++) {
00126 int j = i + k;
00127 j = (j & ~7) | ((j >> 1) & 3) | ((j << 2) & 4);
00128 s->revtab[-split_radix_permutation(i + k, n, s->inverse) & (n - 1)] = j;
00129 }
00130 }
00131 }
00132 }
00133
00134 av_cold int ff_fft_init(FFTContext *s, int nbits, int inverse)
00135 {
00136 int i, j, n;
00137
00138 if (nbits < 2 || nbits > 16)
00139 goto fail;
00140 s->nbits = nbits;
00141 n = 1 << nbits;
00142
00143 s->revtab = av_malloc(n * sizeof(uint16_t));
00144 if (!s->revtab)
00145 goto fail;
00146 s->tmp_buf = av_malloc(n * sizeof(FFTComplex));
00147 if (!s->tmp_buf)
00148 goto fail;
00149 s->inverse = inverse;
00150 s->fft_permutation = FF_FFT_PERM_DEFAULT;
00151
00152 s->fft_permute = ff_fft_permute_c;
00153 s->fft_calc = ff_fft_calc_c;
00154 #if CONFIG_MDCT
00155 s->imdct_calc = ff_imdct_calc_c;
00156 s->imdct_half = ff_imdct_half_c;
00157 s->mdct_calc = ff_mdct_calc_c;
00158 #endif
00159
00160 #if CONFIG_FFT_FLOAT
00161 if (ARCH_ARM) ff_fft_init_arm(s);
00162 if (HAVE_ALTIVEC) ff_fft_init_altivec(s);
00163 if (HAVE_MMX) ff_fft_init_mmx(s);
00164 if (CONFIG_MDCT) s->mdct_calcw = s->mdct_calc;
00165 if (HAVE_MIPSFPU) ff_fft_init_mips(s);
00166 #else
00167 if (CONFIG_MDCT) s->mdct_calcw = ff_mdct_calcw_c;
00168 if (ARCH_ARM) ff_fft_fixed_init_arm(s);
00169 #endif
00170
00171 for(j=4; j<=nbits; j++) {
00172 ff_init_ff_cos_tabs(j);
00173 }
00174
00175 if (s->fft_permutation == FF_FFT_PERM_AVX) {
00176 fft_perm_avx(s);
00177 } else {
00178 for(i=0; i<n; i++) {
00179 int j = i;
00180 if (s->fft_permutation == FF_FFT_PERM_SWAP_LSBS)
00181 j = (j&~3) | ((j>>1)&1) | ((j<<1)&2);
00182 s->revtab[-split_radix_permutation(i, n, s->inverse) & (n-1)] = j;
00183 }
00184 }
00185
00186 return 0;
00187 fail:
00188 av_freep(&s->revtab);
00189 av_freep(&s->tmp_buf);
00190 return -1;
00191 }
00192
00193 static void ff_fft_permute_c(FFTContext *s, FFTComplex *z)
00194 {
00195 int j, np;
00196 const uint16_t *revtab = s->revtab;
00197 np = 1 << s->nbits;
00198
00199 for(j=0;j<np;j++) s->tmp_buf[revtab[j]] = z[j];
00200 memcpy(z, s->tmp_buf, np * sizeof(FFTComplex));
00201 }
00202
00203 av_cold void ff_fft_end(FFTContext *s)
00204 {
00205 av_freep(&s->revtab);
00206 av_freep(&s->tmp_buf);
00207 }
00208
00209 #define BUTTERFLIES(a0,a1,a2,a3) {\
00210 BF(t3, t5, t5, t1);\
00211 BF(a2.re, a0.re, a0.re, t5);\
00212 BF(a3.im, a1.im, a1.im, t3);\
00213 BF(t4, t6, t2, t6);\
00214 BF(a3.re, a1.re, a1.re, t4);\
00215 BF(a2.im, a0.im, a0.im, t6);\
00216 }
00217
00218
00219
00220
00221 #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
00222 FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
00223 BF(t3, t5, t5, t1);\
00224 BF(a2.re, a0.re, r0, t5);\
00225 BF(a3.im, a1.im, i1, t3);\
00226 BF(t4, t6, t2, t6);\
00227 BF(a3.re, a1.re, r1, t4);\
00228 BF(a2.im, a0.im, i0, t6);\
00229 }
00230
00231 #define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
00232 CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
00233 CMUL(t5, t6, a3.re, a3.im, wre, wim);\
00234 BUTTERFLIES(a0,a1,a2,a3)\
00235 }
00236
00237 #define TRANSFORM_ZERO(a0,a1,a2,a3) {\
00238 t1 = a2.re;\
00239 t2 = a2.im;\
00240 t5 = a3.re;\
00241 t6 = a3.im;\
00242 BUTTERFLIES(a0,a1,a2,a3)\
00243 }
00244
00245
00246 #define PASS(name)\
00247 static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
00248 {\
00249 FFTDouble t1, t2, t3, t4, t5, t6;\
00250 int o1 = 2*n;\
00251 int o2 = 4*n;\
00252 int o3 = 6*n;\
00253 const FFTSample *wim = wre+o1;\
00254 n--;\
00255 \
00256 TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
00257 TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
00258 do {\
00259 z += 2;\
00260 wre += 2;\
00261 wim -= 2;\
00262 TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
00263 TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
00264 } while(--n);\
00265 }
00266
00267 PASS(pass)
00268 #undef BUTTERFLIES
00269 #define BUTTERFLIES BUTTERFLIES_BIG
00270 PASS(pass_big)
00271
00272 #define DECL_FFT(n,n2,n4)\
00273 static void fft##n(FFTComplex *z)\
00274 {\
00275 fft##n2(z);\
00276 fft##n4(z+n4*2);\
00277 fft##n4(z+n4*3);\
00278 pass(z,FFT_NAME(ff_cos_##n),n4/2);\
00279 }
00280
00281 static void fft4(FFTComplex *z)
00282 {
00283 FFTDouble t1, t2, t3, t4, t5, t6, t7, t8;
00284
00285 BF(t3, t1, z[0].re, z[1].re);
00286 BF(t8, t6, z[3].re, z[2].re);
00287 BF(z[2].re, z[0].re, t1, t6);
00288 BF(t4, t2, z[0].im, z[1].im);
00289 BF(t7, t5, z[2].im, z[3].im);
00290 BF(z[3].im, z[1].im, t4, t8);
00291 BF(z[3].re, z[1].re, t3, t7);
00292 BF(z[2].im, z[0].im, t2, t5);
00293 }
00294
00295 static void fft8(FFTComplex *z)
00296 {
00297 FFTDouble t1, t2, t3, t4, t5, t6;
00298
00299 fft4(z);
00300
00301 BF(t1, z[5].re, z[4].re, -z[5].re);
00302 BF(t2, z[5].im, z[4].im, -z[5].im);
00303 BF(t5, z[7].re, z[6].re, -z[7].re);
00304 BF(t6, z[7].im, z[6].im, -z[7].im);
00305
00306 BUTTERFLIES(z[0],z[2],z[4],z[6]);
00307 TRANSFORM(z[1],z[3],z[5],z[7],sqrthalf,sqrthalf);
00308 }
00309
00310 #if !CONFIG_SMALL
00311 static void fft16(FFTComplex *z)
00312 {
00313 FFTDouble t1, t2, t3, t4, t5, t6;
00314 FFTSample cos_16_1 = FFT_NAME(ff_cos_16)[1];
00315 FFTSample cos_16_3 = FFT_NAME(ff_cos_16)[3];
00316
00317 fft8(z);
00318 fft4(z+8);
00319 fft4(z+12);
00320
00321 TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
00322 TRANSFORM(z[2],z[6],z[10],z[14],sqrthalf,sqrthalf);
00323 TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
00324 TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
00325 }
00326 #else
00327 DECL_FFT(16,8,4)
00328 #endif
00329 DECL_FFT(32,16,8)
00330 DECL_FFT(64,32,16)
00331 DECL_FFT(128,64,32)
00332 DECL_FFT(256,128,64)
00333 DECL_FFT(512,256,128)
00334 #if !CONFIG_SMALL
00335 #define pass pass_big
00336 #endif
00337 DECL_FFT(1024,512,256)
00338 DECL_FFT(2048,1024,512)
00339 DECL_FFT(4096,2048,1024)
00340 DECL_FFT(8192,4096,2048)
00341 DECL_FFT(16384,8192,4096)
00342 DECL_FFT(32768,16384,8192)
00343 DECL_FFT(65536,32768,16384)
00344
00345 static void (* const fft_dispatch[])(FFTComplex*) = {
00346 fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
00347 fft2048, fft4096, fft8192, fft16384, fft32768, fft65536,
00348 };
00349
00350 static void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
00351 {
00352 fft_dispatch[s->nbits-2](z);
00353 }