00001 /* 00002 * (I)RDFT transforms 00003 * Copyright (c) 2009 Alex Converse <alex dot converse at gmail dot com> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 #include <math.h> 00022 #include "dsputil.h" 00023 00029 /* sin(2*pi*x/n) for 0<=x<n/4, followed by n/2<=x<3n/4 */ 00030 DECLARE_ALIGNED_16(FFTSample, ff_sin_16[8]); 00031 DECLARE_ALIGNED_16(FFTSample, ff_sin_32[16]); 00032 DECLARE_ALIGNED_16(FFTSample, ff_sin_64[32]); 00033 DECLARE_ALIGNED_16(FFTSample, ff_sin_128[64]); 00034 DECLARE_ALIGNED_16(FFTSample, ff_sin_256[128]); 00035 DECLARE_ALIGNED_16(FFTSample, ff_sin_512[256]); 00036 DECLARE_ALIGNED_16(FFTSample, ff_sin_1024[512]); 00037 DECLARE_ALIGNED_16(FFTSample, ff_sin_2048[1024]); 00038 DECLARE_ALIGNED_16(FFTSample, ff_sin_4096[2048]); 00039 DECLARE_ALIGNED_16(FFTSample, ff_sin_8192[4096]); 00040 DECLARE_ALIGNED_16(FFTSample, ff_sin_16384[8192]); 00041 DECLARE_ALIGNED_16(FFTSample, ff_sin_32768[16384]); 00042 DECLARE_ALIGNED_16(FFTSample, ff_sin_65536[32768]); 00043 FFTSample *ff_sin_tabs[] = { 00044 ff_sin_16, ff_sin_32, ff_sin_64, ff_sin_128, ff_sin_256, ff_sin_512, ff_sin_1024, 00045 ff_sin_2048, ff_sin_4096, ff_sin_8192, ff_sin_16384, ff_sin_32768, ff_sin_65536, 00046 }; 00047 00048 av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans) 00049 { 00050 int n = 1 << nbits; 00051 int i; 00052 const double theta = (trans == RDFT || trans == IRIDFT ? -1 : 1)*2*M_PI/n; 00053 00054 s->nbits = nbits; 00055 s->inverse = trans == IRDFT || trans == IRIDFT; 00056 s->sign_convention = trans == RIDFT || trans == IRIDFT ? 1 : -1; 00057 00058 if (nbits < 4 || nbits > 16) 00059 return -1; 00060 00061 if (ff_fft_init(&s->fft, nbits-1, trans == IRDFT || trans == RIDFT) < 0) 00062 return -1; 00063 00064 s->tcos = ff_cos_tabs[nbits-4]; 00065 s->tsin = ff_sin_tabs[nbits-4]+(trans == RDFT || trans == IRIDFT)*(n>>2); 00066 for (i = 0; i < (n>>2); i++) { 00067 s->tcos[i] = cos(i*theta); 00068 s->tsin[i] = sin(i*theta); 00069 } 00070 return 0; 00071 } 00072 00077 void ff_rdft_calc_c(RDFTContext* s, FFTSample* data) 00078 { 00079 int i, i1, i2; 00080 FFTComplex ev, od; 00081 const int n = 1 << s->nbits; 00082 const float k1 = 0.5; 00083 const float k2 = 0.5 - s->inverse; 00084 const FFTSample *tcos = s->tcos; 00085 const FFTSample *tsin = s->tsin; 00086 00087 if (!s->inverse) { 00088 ff_fft_permute(&s->fft, (FFTComplex*)data); 00089 ff_fft_calc(&s->fft, (FFTComplex*)data); 00090 } 00091 /* i=0 is a special case because of packing, the DC term is real, so we 00092 are going to throw the N/2 term (also real) in with it. */ 00093 ev.re = data[0]; 00094 data[0] = ev.re+data[1]; 00095 data[1] = ev.re-data[1]; 00096 for (i = 1; i < (n>>2); i++) { 00097 i1 = 2*i; 00098 i2 = n-i1; 00099 /* Separate even and odd FFTs */ 00100 ev.re = k1*(data[i1 ]+data[i2 ]); 00101 od.im = -k2*(data[i1 ]-data[i2 ]); 00102 ev.im = k1*(data[i1+1]-data[i2+1]); 00103 od.re = k2*(data[i1+1]+data[i2+1]); 00104 /* Apply twiddle factors to the odd FFT and add to the even FFT */ 00105 data[i1 ] = ev.re + od.re*tcos[i] - od.im*tsin[i]; 00106 data[i1+1] = ev.im + od.im*tcos[i] + od.re*tsin[i]; 00107 data[i2 ] = ev.re - od.re*tcos[i] + od.im*tsin[i]; 00108 data[i2+1] = -ev.im + od.im*tcos[i] + od.re*tsin[i]; 00109 } 00110 data[2*i+1]=s->sign_convention*data[2*i+1]; 00111 if (s->inverse) { 00112 data[0] *= k1; 00113 data[1] *= k1; 00114 ff_fft_permute(&s->fft, (FFTComplex*)data); 00115 ff_fft_calc(&s->fft, (FFTComplex*)data); 00116 } 00117 } 00118 00119 void ff_rdft_calc(RDFTContext *s, FFTSample *data) 00120 { 00121 ff_rdft_calc_c(s, data); 00122 } 00123 00124 av_cold void ff_rdft_end(RDFTContext *s) 00125 { 00126 ff_fft_end(&s->fft); 00127 }