Libav
|
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 <stdlib.h> 00022 #include <math.h> 00023 #include "libavutil/mathematics.h" 00024 #include "fft.h" 00025 00031 /* sin(2*pi*x/n) for 0<=x<n/4, followed by n/2<=x<3n/4 */ 00032 #if !CONFIG_HARDCODED_TABLES 00033 SINTABLE(16); 00034 SINTABLE(32); 00035 SINTABLE(64); 00036 SINTABLE(128); 00037 SINTABLE(256); 00038 SINTABLE(512); 00039 SINTABLE(1024); 00040 SINTABLE(2048); 00041 SINTABLE(4096); 00042 SINTABLE(8192); 00043 SINTABLE(16384); 00044 SINTABLE(32768); 00045 SINTABLE(65536); 00046 #endif 00047 SINTABLE_CONST FFTSample * const ff_sin_tabs[] = { 00048 NULL, NULL, NULL, NULL, 00049 ff_sin_16, ff_sin_32, ff_sin_64, ff_sin_128, ff_sin_256, ff_sin_512, ff_sin_1024, 00050 ff_sin_2048, ff_sin_4096, ff_sin_8192, ff_sin_16384, ff_sin_32768, ff_sin_65536, 00051 }; 00052 00057 static void ff_rdft_calc_c(RDFTContext* s, FFTSample* data) 00058 { 00059 int i, i1, i2; 00060 FFTComplex ev, od; 00061 const int n = 1 << s->nbits; 00062 const float k1 = 0.5; 00063 const float k2 = 0.5 - s->inverse; 00064 const FFTSample *tcos = s->tcos; 00065 const FFTSample *tsin = s->tsin; 00066 00067 if (!s->inverse) { 00068 ff_fft_permute(&s->fft, (FFTComplex*)data); 00069 ff_fft_calc(&s->fft, (FFTComplex*)data); 00070 } 00071 /* i=0 is a special case because of packing, the DC term is real, so we 00072 are going to throw the N/2 term (also real) in with it. */ 00073 ev.re = data[0]; 00074 data[0] = ev.re+data[1]; 00075 data[1] = ev.re-data[1]; 00076 for (i = 1; i < (n>>2); i++) { 00077 i1 = 2*i; 00078 i2 = n-i1; 00079 /* Separate even and odd FFTs */ 00080 ev.re = k1*(data[i1 ]+data[i2 ]); 00081 od.im = -k2*(data[i1 ]-data[i2 ]); 00082 ev.im = k1*(data[i1+1]-data[i2+1]); 00083 od.re = k2*(data[i1+1]+data[i2+1]); 00084 /* Apply twiddle factors to the odd FFT and add to the even FFT */ 00085 data[i1 ] = ev.re + od.re*tcos[i] - od.im*tsin[i]; 00086 data[i1+1] = ev.im + od.im*tcos[i] + od.re*tsin[i]; 00087 data[i2 ] = ev.re - od.re*tcos[i] + od.im*tsin[i]; 00088 data[i2+1] = -ev.im + od.im*tcos[i] + od.re*tsin[i]; 00089 } 00090 data[2*i+1]=s->sign_convention*data[2*i+1]; 00091 if (s->inverse) { 00092 data[0] *= k1; 00093 data[1] *= k1; 00094 ff_fft_permute(&s->fft, (FFTComplex*)data); 00095 ff_fft_calc(&s->fft, (FFTComplex*)data); 00096 } 00097 } 00098 00099 av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans) 00100 { 00101 int n = 1 << nbits; 00102 int i; 00103 const double theta = (trans == DFT_R2C || trans == DFT_C2R ? -1 : 1)*2*M_PI/n; 00104 00105 s->nbits = nbits; 00106 s->inverse = trans == IDFT_C2R || trans == DFT_C2R; 00107 s->sign_convention = trans == IDFT_R2C || trans == DFT_C2R ? 1 : -1; 00108 00109 if (nbits < 4 || nbits > 16) 00110 return -1; 00111 00112 if (ff_fft_init(&s->fft, nbits-1, trans == IDFT_C2R || trans == IDFT_R2C) < 0) 00113 return -1; 00114 00115 ff_init_ff_cos_tabs(nbits); 00116 s->tcos = ff_cos_tabs[nbits]; 00117 s->tsin = ff_sin_tabs[nbits]+(trans == DFT_R2C || trans == DFT_C2R)*(n>>2); 00118 #if !CONFIG_HARDCODED_TABLES 00119 for (i = 0; i < (n>>2); i++) { 00120 s->tsin[i] = sin(i*theta); 00121 } 00122 #endif 00123 s->rdft_calc = ff_rdft_calc_c; 00124 00125 if (ARCH_ARM) ff_rdft_init_arm(s); 00126 00127 return 0; 00128 } 00129 00130 av_cold void ff_rdft_end(RDFTContext *s) 00131 { 00132 ff_fft_end(&s->fft); 00133 }