00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include "libavutil/common.h"
00025 #include "libavutil/mathematics.h"
00026 #include "fft.h"
00027
00036 av_cold int ff_mdct_init(FFTContext *s, int nbits, int inverse, double scale)
00037 {
00038 int n, n4, i;
00039 double alpha, theta;
00040 int tstep;
00041
00042 memset(s, 0, sizeof(*s));
00043 n = 1 << nbits;
00044 s->mdct_bits = nbits;
00045 s->mdct_size = n;
00046 n4 = n >> 2;
00047 s->mdct_permutation = FF_MDCT_PERM_NONE;
00048
00049 if (ff_fft_init(s, s->mdct_bits - 2, inverse) < 0)
00050 goto fail;
00051
00052 s->tcos = av_malloc(n/2 * sizeof(FFTSample));
00053 if (!s->tcos)
00054 goto fail;
00055
00056 switch (s->mdct_permutation) {
00057 case FF_MDCT_PERM_NONE:
00058 s->tsin = s->tcos + n4;
00059 tstep = 1;
00060 break;
00061 case FF_MDCT_PERM_INTERLEAVE:
00062 s->tsin = s->tcos + 1;
00063 tstep = 2;
00064 break;
00065 default:
00066 goto fail;
00067 }
00068
00069 theta = 1.0 / 8.0 + (scale < 0 ? n4 : 0);
00070 scale = sqrt(fabs(scale));
00071 for(i=0;i<n4;i++) {
00072 alpha = 2 * M_PI * (i + theta) / n;
00073 s->tcos[i*tstep] = -cos(alpha) * scale;
00074 s->tsin[i*tstep] = -sin(alpha) * scale;
00075 }
00076 return 0;
00077 fail:
00078 ff_mdct_end(s);
00079 return -1;
00080 }
00081
00082
00083 #define CMUL(pre, pim, are, aim, bre, bim) \
00084 {\
00085 FFTSample _are = (are);\
00086 FFTSample _aim = (aim);\
00087 FFTSample _bre = (bre);\
00088 FFTSample _bim = (bim);\
00089 (pre) = _are * _bre - _aim * _bim;\
00090 (pim) = _are * _bim + _aim * _bre;\
00091 }
00092
00099 void ff_imdct_half_c(FFTContext *s, FFTSample *output, const FFTSample *input)
00100 {
00101 int k, n8, n4, n2, n, j;
00102 const uint16_t *revtab = s->revtab;
00103 const FFTSample *tcos = s->tcos;
00104 const FFTSample *tsin = s->tsin;
00105 const FFTSample *in1, *in2;
00106 FFTComplex *z = (FFTComplex *)output;
00107
00108 n = 1 << s->mdct_bits;
00109 n2 = n >> 1;
00110 n4 = n >> 2;
00111 n8 = n >> 3;
00112
00113
00114 in1 = input;
00115 in2 = input + n2 - 1;
00116 for(k = 0; k < n4; k++) {
00117 j=revtab[k];
00118 CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
00119 in1 += 2;
00120 in2 -= 2;
00121 }
00122 s->fft_calc(s, z);
00123
00124
00125 for(k = 0; k < n8; k++) {
00126 FFTSample r0, i0, r1, i1;
00127 CMUL(r0, i1, z[n8-k-1].im, z[n8-k-1].re, tsin[n8-k-1], tcos[n8-k-1]);
00128 CMUL(r1, i0, z[n8+k ].im, z[n8+k ].re, tsin[n8+k ], tcos[n8+k ]);
00129 z[n8-k-1].re = r0;
00130 z[n8-k-1].im = i0;
00131 z[n8+k ].re = r1;
00132 z[n8+k ].im = i1;
00133 }
00134 }
00135
00141 void ff_imdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input)
00142 {
00143 int k;
00144 int n = 1 << s->mdct_bits;
00145 int n2 = n >> 1;
00146 int n4 = n >> 2;
00147
00148 ff_imdct_half_c(s, output+n4, input);
00149
00150 for(k = 0; k < n4; k++) {
00151 output[k] = -output[n2-k-1];
00152 output[n-k-1] = output[n2+k];
00153 }
00154 }
00155
00161 void ff_mdct_calc_c(FFTContext *s, FFTSample *out, const FFTSample *input)
00162 {
00163 int i, j, n, n8, n4, n2, n3;
00164 FFTSample re, im;
00165 const uint16_t *revtab = s->revtab;
00166 const FFTSample *tcos = s->tcos;
00167 const FFTSample *tsin = s->tsin;
00168 FFTComplex *x = (FFTComplex *)out;
00169
00170 n = 1 << s->mdct_bits;
00171 n2 = n >> 1;
00172 n4 = n >> 2;
00173 n8 = n >> 3;
00174 n3 = 3 * n4;
00175
00176
00177 for(i=0;i<n8;i++) {
00178 re = -input[2*i+n3] - input[n3-1-2*i];
00179 im = -input[n4+2*i] + input[n4-1-2*i];
00180 j = revtab[i];
00181 CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
00182
00183 re = input[2*i] - input[n2-1-2*i];
00184 im = -(input[n2+2*i] + input[n-1-2*i]);
00185 j = revtab[n8 + i];
00186 CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
00187 }
00188
00189 s->fft_calc(s, x);
00190
00191
00192 for(i=0;i<n8;i++) {
00193 FFTSample r0, i0, r1, i1;
00194 CMUL(i1, r0, x[n8-i-1].re, x[n8-i-1].im, -tsin[n8-i-1], -tcos[n8-i-1]);
00195 CMUL(i0, r1, x[n8+i ].re, x[n8+i ].im, -tsin[n8+i ], -tcos[n8+i ]);
00196 x[n8-i-1].re = r0;
00197 x[n8-i-1].im = i0;
00198 x[n8+i ].re = r1;
00199 x[n8+i ].im = i1;
00200 }
00201 }
00202
00203 av_cold void ff_mdct_end(FFTContext *s)
00204 {
00205 av_freep(&s->tcos);
00206 ff_fft_end(s);
00207 }