libavcodec/fft.c
Go to the documentation of this file.
00001 /*
00002  * FFT/IFFT transforms
00003  * Copyright (c) 2008 Loren Merritt
00004  * Copyright (c) 2002 Fabrice Bellard
00005  * Partly based on libdjbfft by D. J. Bernstein
00006  *
00007  * This file is part of FFmpeg.
00008  *
00009  * FFmpeg is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * FFmpeg is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with FFmpeg; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 /* cos(2*pi*x/n) for 0<=x<=n/4, followed by its reverse */
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 #else
00166     if (CONFIG_MDCT)  s->mdct_calcw = ff_mdct_calcw_c;
00167     if (ARCH_ARM)     ff_fft_fixed_init_arm(s);
00168 #endif
00169 
00170     for(j=4; j<=nbits; j++) {
00171         ff_init_ff_cos_tabs(j);
00172     }
00173 
00174     if (s->fft_permutation == FF_FFT_PERM_AVX) {
00175         fft_perm_avx(s);
00176     } else {
00177         for(i=0; i<n; i++) {
00178             int j = i;
00179             if (s->fft_permutation == FF_FFT_PERM_SWAP_LSBS)
00180                 j = (j&~3) | ((j>>1)&1) | ((j<<1)&2);
00181             s->revtab[-split_radix_permutation(i, n, s->inverse) & (n-1)] = j;
00182         }
00183     }
00184 
00185     return 0;
00186  fail:
00187     av_freep(&s->revtab);
00188     av_freep(&s->tmp_buf);
00189     return -1;
00190 }
00191 
00192 static void ff_fft_permute_c(FFTContext *s, FFTComplex *z)
00193 {
00194     int j, np;
00195     const uint16_t *revtab = s->revtab;
00196     np = 1 << s->nbits;
00197     /* TODO: handle split-radix permute in a more optimal way, probably in-place */
00198     for(j=0;j<np;j++) s->tmp_buf[revtab[j]] = z[j];
00199     memcpy(z, s->tmp_buf, np * sizeof(FFTComplex));
00200 }
00201 
00202 av_cold void ff_fft_end(FFTContext *s)
00203 {
00204     av_freep(&s->revtab);
00205     av_freep(&s->tmp_buf);
00206 }
00207 
00208 #define BUTTERFLIES(a0,a1,a2,a3) {\
00209     BF(t3, t5, t5, t1);\
00210     BF(a2.re, a0.re, a0.re, t5);\
00211     BF(a3.im, a1.im, a1.im, t3);\
00212     BF(t4, t6, t2, t6);\
00213     BF(a3.re, a1.re, a1.re, t4);\
00214     BF(a2.im, a0.im, a0.im, t6);\
00215 }
00216 
00217 // force loading all the inputs before storing any.
00218 // this is slightly slower for small data, but avoids store->load aliasing
00219 // for addresses separated by large powers of 2.
00220 #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
00221     FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
00222     BF(t3, t5, t5, t1);\
00223     BF(a2.re, a0.re, r0, t5);\
00224     BF(a3.im, a1.im, i1, t3);\
00225     BF(t4, t6, t2, t6);\
00226     BF(a3.re, a1.re, r1, t4);\
00227     BF(a2.im, a0.im, i0, t6);\
00228 }
00229 
00230 #define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
00231     CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
00232     CMUL(t5, t6, a3.re, a3.im, wre,  wim);\
00233     BUTTERFLIES(a0,a1,a2,a3)\
00234 }
00235 
00236 #define TRANSFORM_ZERO(a0,a1,a2,a3) {\
00237     t1 = a2.re;\
00238     t2 = a2.im;\
00239     t5 = a3.re;\
00240     t6 = a3.im;\
00241     BUTTERFLIES(a0,a1,a2,a3)\
00242 }
00243 
00244 /* z[0...8n-1], w[1...2n-1] */
00245 #define PASS(name)\
00246 static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
00247 {\
00248     FFTDouble t1, t2, t3, t4, t5, t6;\
00249     int o1 = 2*n;\
00250     int o2 = 4*n;\
00251     int o3 = 6*n;\
00252     const FFTSample *wim = wre+o1;\
00253     n--;\
00254 \
00255     TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
00256     TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
00257     do {\
00258         z += 2;\
00259         wre += 2;\
00260         wim -= 2;\
00261         TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
00262         TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
00263     } while(--n);\
00264 }
00265 
00266 PASS(pass)
00267 #undef BUTTERFLIES
00268 #define BUTTERFLIES BUTTERFLIES_BIG
00269 PASS(pass_big)
00270 
00271 #define DECL_FFT(n,n2,n4)\
00272 static void fft##n(FFTComplex *z)\
00273 {\
00274     fft##n2(z);\
00275     fft##n4(z+n4*2);\
00276     fft##n4(z+n4*3);\
00277     pass(z,FFT_NAME(ff_cos_##n),n4/2);\
00278 }
00279 
00280 static void fft4(FFTComplex *z)
00281 {
00282     FFTDouble t1, t2, t3, t4, t5, t6, t7, t8;
00283 
00284     BF(t3, t1, z[0].re, z[1].re);
00285     BF(t8, t6, z[3].re, z[2].re);
00286     BF(z[2].re, z[0].re, t1, t6);
00287     BF(t4, t2, z[0].im, z[1].im);
00288     BF(t7, t5, z[2].im, z[3].im);
00289     BF(z[3].im, z[1].im, t4, t8);
00290     BF(z[3].re, z[1].re, t3, t7);
00291     BF(z[2].im, z[0].im, t2, t5);
00292 }
00293 
00294 static void fft8(FFTComplex *z)
00295 {
00296     FFTDouble t1, t2, t3, t4, t5, t6;
00297 
00298     fft4(z);
00299 
00300     BF(t1, z[5].re, z[4].re, -z[5].re);
00301     BF(t2, z[5].im, z[4].im, -z[5].im);
00302     BF(t5, z[7].re, z[6].re, -z[7].re);
00303     BF(t6, z[7].im, z[6].im, -z[7].im);
00304 
00305     BUTTERFLIES(z[0],z[2],z[4],z[6]);
00306     TRANSFORM(z[1],z[3],z[5],z[7],sqrthalf,sqrthalf);
00307 }
00308 
00309 #if !CONFIG_SMALL
00310 static void fft16(FFTComplex *z)
00311 {
00312     FFTDouble t1, t2, t3, t4, t5, t6;
00313     FFTSample cos_16_1 = FFT_NAME(ff_cos_16)[1];
00314     FFTSample cos_16_3 = FFT_NAME(ff_cos_16)[3];
00315 
00316     fft8(z);
00317     fft4(z+8);
00318     fft4(z+12);
00319 
00320     TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
00321     TRANSFORM(z[2],z[6],z[10],z[14],sqrthalf,sqrthalf);
00322     TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
00323     TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
00324 }
00325 #else
00326 DECL_FFT(16,8,4)
00327 #endif
00328 DECL_FFT(32,16,8)
00329 DECL_FFT(64,32,16)
00330 DECL_FFT(128,64,32)
00331 DECL_FFT(256,128,64)
00332 DECL_FFT(512,256,128)
00333 #if !CONFIG_SMALL
00334 #define pass pass_big
00335 #endif
00336 DECL_FFT(1024,512,256)
00337 DECL_FFT(2048,1024,512)
00338 DECL_FFT(4096,2048,1024)
00339 DECL_FFT(8192,4096,2048)
00340 DECL_FFT(16384,8192,4096)
00341 DECL_FFT(32768,16384,8192)
00342 DECL_FFT(65536,32768,16384)
00343 
00344 static void (* const fft_dispatch[])(FFTComplex*) = {
00345     fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
00346     fft2048, fft4096, fft8192, fft16384, fft32768, fft65536,
00347 };
00348 
00349 static void ff_fft_calc_c(FFTContext *s, FFTComplex *z)
00350 {
00351     fft_dispatch[s->nbits-2](z);
00352 }
00353