2 * (c) 2002 Fabrice Bellard
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #define MUL16(a,b) ((a) * (b))
35 #define CMAC(pre, pim, are, aim, bre, bim) \
37 pre += (MUL16(are, bre) - MUL16(aim, bim));\
38 pim += (MUL16(are, bim) + MUL16(bre, aim));\
43 void fft_ref_init(int nbits, int inverse)
49 exptab = av_malloc((n / 2) * sizeof(FFTComplex));
51 for(i=0;i<(n/2);i++) {
52 alpha = 2 * M_PI * (float)i / (float)n;
62 void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
65 float tmp_re, tmp_im, s, c;
75 k = (i * j) & (n - 1);
77 c = -exptab[k - n2].re;
78 s = -exptab[k - n2].im;
83 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
91 void imdct_ref(float *out, float *in, int n)
99 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
100 f = cos(M_PI * a / (double)(2 * n));
107 /* NOTE: no normalisation by 1 / N is done */
108 void mdct_ref(float *output, float *input, int n)
117 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
118 s += input[i] * cos(a);
127 return (float)((random() & 0xffff) - 32768) / 32768.0;
130 int64_t gettime(void)
133 gettimeofday(&tv,NULL);
134 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
137 void check_diff(float *tab1, float *tab2, int n)
142 if (fabsf(tab1[i] - tab2[i]) >= 1e-3) {
143 av_log(NULL, AV_LOG_ERROR, "ERROR %d: %f %f\n",
144 i, tab1[i], tab2[i]);
152 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
153 "-h print this help\n"
156 "-i inverse transform test\n"
157 "-n b set the transform size to 2^b\n"
164 int main(int argc, char **argv)
166 FFTComplex *tab, *tab1, *tab_ref;
167 FFTSample *tabtmp, *tab2;
172 FFTContext s1, *s = &s1;
173 MDCTContext m1, *m = &m1;
174 int fft_nbits, fft_size;
179 c = getopt(argc, argv, "hsimn:");
196 fft_nbits = atoi(optarg);
201 fft_size = 1 << fft_nbits;
202 tab = av_malloc(fft_size * sizeof(FFTComplex));
203 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
204 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
205 tabtmp = av_malloc(fft_size / 2 * sizeof(FFTSample));
206 tab2 = av_malloc(fft_size * sizeof(FFTSample));
210 av_log(NULL, AV_LOG_INFO,"IMDCT");
212 av_log(NULL, AV_LOG_INFO,"MDCT");
213 ff_mdct_init(m, fft_nbits, do_inverse);
216 av_log(NULL, AV_LOG_INFO,"IFFT");
218 av_log(NULL, AV_LOG_INFO,"FFT");
219 ff_fft_init(s, fft_nbits, do_inverse);
220 fft_ref_init(fft_nbits, do_inverse);
222 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
224 /* generate random data */
226 for(i=0;i<fft_size;i++) {
227 tab1[i].re = frandom();
228 tab1[i].im = frandom();
231 /* checking result */
232 av_log(NULL, AV_LOG_INFO,"Checking...\n");
236 imdct_ref((float *)tab_ref, (float *)tab1, fft_size);
237 ff_imdct_calc(m, tab2, (float *)tab1, tabtmp);
238 check_diff((float *)tab_ref, tab2, fft_size);
240 mdct_ref((float *)tab_ref, (float *)tab1, fft_size);
242 ff_mdct_calc(m, tab2, (float *)tab1, tabtmp);
244 check_diff((float *)tab_ref, tab2, fft_size / 2);
247 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
248 ff_fft_permute(s, tab);
251 fft_ref(tab_ref, tab1, fft_nbits);
252 check_diff((float *)tab_ref, (float *)tab, fft_size * 2);
255 /* do a speed test */
258 int64_t time_start, duration;
261 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
262 /* we measure during about 1 seconds */
265 time_start = gettime();
266 for(it=0;it<nb_its;it++) {
269 ff_imdct_calc(m, (float *)tab, (float *)tab1, tabtmp);
271 ff_mdct_calc(m, (float *)tab, (float *)tab1, tabtmp);
274 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
278 duration = gettime() - time_start;
279 if (duration >= 1000000)
283 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
284 (double)duration / nb_its,
285 (double)duration / 1000000.0,