00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdlib.h>
00022 #include <math.h>
00023 #include "libavutil/mathematics.h"
00024 #include "rdft.h"
00025
00031
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 static 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 s->fft.fft_permute(&s->fft, (FFTComplex*)data);
00069 s->fft.fft_calc(&s->fft, (FFTComplex*)data);
00070 }
00071
00072
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
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
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 s->fft.fft_permute(&s->fft, (FFTComplex*)data);
00095 s->fft.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 }