Libav 0.7.1
|
00001 /* 00002 * MDCT/IMDCT transforms 00003 * Copyright (c) 2002 Fabrice Bellard 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 #include "fft-internal.h" 00028 00034 #if CONFIG_FFT_FLOAT 00035 # define RSCALE(x) (x) 00036 #else 00037 # define RSCALE(x) ((x) >> 1) 00038 #endif 00039 00043 av_cold int ff_mdct_init(FFTContext *s, int nbits, int inverse, double scale) 00044 { 00045 int n, n4, i; 00046 double alpha, theta; 00047 int tstep; 00048 00049 memset(s, 0, sizeof(*s)); 00050 n = 1 << nbits; 00051 s->mdct_bits = nbits; 00052 s->mdct_size = n; 00053 n4 = n >> 2; 00054 s->mdct_permutation = FF_MDCT_PERM_NONE; 00055 00056 if (ff_fft_init(s, s->mdct_bits - 2, inverse) < 0) 00057 goto fail; 00058 00059 s->tcos = av_malloc(n/2 * sizeof(FFTSample)); 00060 if (!s->tcos) 00061 goto fail; 00062 00063 switch (s->mdct_permutation) { 00064 case FF_MDCT_PERM_NONE: 00065 s->tsin = s->tcos + n4; 00066 tstep = 1; 00067 break; 00068 case FF_MDCT_PERM_INTERLEAVE: 00069 s->tsin = s->tcos + 1; 00070 tstep = 2; 00071 break; 00072 default: 00073 goto fail; 00074 } 00075 00076 theta = 1.0 / 8.0 + (scale < 0 ? n4 : 0); 00077 scale = sqrt(fabs(scale)); 00078 for(i=0;i<n4;i++) { 00079 alpha = 2 * M_PI * (i + theta) / n; 00080 s->tcos[i*tstep] = FIX15(-cos(alpha) * scale); 00081 s->tsin[i*tstep] = FIX15(-sin(alpha) * scale); 00082 } 00083 return 0; 00084 fail: 00085 ff_mdct_end(s); 00086 return -1; 00087 } 00088 00095 void ff_imdct_half_c(FFTContext *s, FFTSample *output, const FFTSample *input) 00096 { 00097 int k, n8, n4, n2, n, j; 00098 const uint16_t *revtab = s->revtab; 00099 const FFTSample *tcos = s->tcos; 00100 const FFTSample *tsin = s->tsin; 00101 const FFTSample *in1, *in2; 00102 FFTComplex *z = (FFTComplex *)output; 00103 00104 n = 1 << s->mdct_bits; 00105 n2 = n >> 1; 00106 n4 = n >> 2; 00107 n8 = n >> 3; 00108 00109 /* pre rotation */ 00110 in1 = input; 00111 in2 = input + n2 - 1; 00112 for(k = 0; k < n4; k++) { 00113 j=revtab[k]; 00114 CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]); 00115 in1 += 2; 00116 in2 -= 2; 00117 } 00118 s->fft_calc(s, z); 00119 00120 /* post rotation + reordering */ 00121 for(k = 0; k < n8; k++) { 00122 FFTSample r0, i0, r1, i1; 00123 CMUL(r0, i1, z[n8-k-1].im, z[n8-k-1].re, tsin[n8-k-1], tcos[n8-k-1]); 00124 CMUL(r1, i0, z[n8+k ].im, z[n8+k ].re, tsin[n8+k ], tcos[n8+k ]); 00125 z[n8-k-1].re = r0; 00126 z[n8-k-1].im = i0; 00127 z[n8+k ].re = r1; 00128 z[n8+k ].im = i1; 00129 } 00130 } 00131 00137 void ff_imdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input) 00138 { 00139 int k; 00140 int n = 1 << s->mdct_bits; 00141 int n2 = n >> 1; 00142 int n4 = n >> 2; 00143 00144 ff_imdct_half_c(s, output+n4, input); 00145 00146 for(k = 0; k < n4; k++) { 00147 output[k] = -output[n2-k-1]; 00148 output[n-k-1] = output[n2+k]; 00149 } 00150 } 00151 00157 void ff_mdct_calc_c(FFTContext *s, FFTSample *out, const FFTSample *input) 00158 { 00159 int i, j, n, n8, n4, n2, n3; 00160 FFTDouble re, im; 00161 const uint16_t *revtab = s->revtab; 00162 const FFTSample *tcos = s->tcos; 00163 const FFTSample *tsin = s->tsin; 00164 FFTComplex *x = (FFTComplex *)out; 00165 00166 n = 1 << s->mdct_bits; 00167 n2 = n >> 1; 00168 n4 = n >> 2; 00169 n8 = n >> 3; 00170 n3 = 3 * n4; 00171 00172 /* pre rotation */ 00173 for(i=0;i<n8;i++) { 00174 re = RSCALE(-input[2*i+n3] - input[n3-1-2*i]); 00175 im = RSCALE(-input[n4+2*i] + input[n4-1-2*i]); 00176 j = revtab[i]; 00177 CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]); 00178 00179 re = RSCALE( input[2*i] - input[n2-1-2*i]); 00180 im = RSCALE(-input[n2+2*i] - input[ n-1-2*i]); 00181 j = revtab[n8 + i]; 00182 CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]); 00183 } 00184 00185 s->fft_calc(s, x); 00186 00187 /* post rotation */ 00188 for(i=0;i<n8;i++) { 00189 FFTSample r0, i0, r1, i1; 00190 CMUL(i1, r0, x[n8-i-1].re, x[n8-i-1].im, -tsin[n8-i-1], -tcos[n8-i-1]); 00191 CMUL(i0, r1, x[n8+i ].re, x[n8+i ].im, -tsin[n8+i ], -tcos[n8+i ]); 00192 x[n8-i-1].re = r0; 00193 x[n8-i-1].im = i0; 00194 x[n8+i ].re = r1; 00195 x[n8+i ].im = i1; 00196 } 00197 } 00198 00199 av_cold void ff_mdct_end(FFTContext *s) 00200 { 00201 av_freep(&s->tcos); 00202 ff_fft_end(s); 00203 }