2 * (c) 2002 Fabrice Bellard
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/cpu.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/lfg.h"
29 #include "libavutil/log.h"
43 #define MUL16(a,b) ((a) * (b))
45 #define CMAC(pre, pim, are, aim, bre, bim) \
47 pre += (MUL16(are, bre) - MUL16(aim, bim));\
48 pim += (MUL16(are, bim) + MUL16(bre, aim));\
53 # define REF_SCALE(x, bits) (x)
57 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
65 static void fft_ref_init(int nbits, int inverse)
71 exptab = av_malloc((n / 2) * sizeof(*exptab));
73 for (i = 0; i < (n/2); i++) {
74 alpha = 2 * M_PI * (float)i / (float)n;
84 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
87 double tmp_re, tmp_im, s, c;
92 for (i = 0; i < n; i++) {
96 for (j = 0; j < n; j++) {
97 k = (i * j) & (n - 1);
99 c = -exptab[k - n2].re;
100 s = -exptab[k - n2].im;
105 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
108 tabr[i].re = REF_SCALE(tmp_re, nbits);
109 tabr[i].im = REF_SCALE(tmp_im, nbits);
113 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
119 for (i = 0; i < n; i++) {
121 for (k = 0; k < n/2; k++) {
122 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
123 f = cos(M_PI * a / (double)(2 * n));
126 out[i] = REF_SCALE(-sum, nbits - 2);
130 /* NOTE: no normalisation by 1 / N is done */
131 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
138 for (k = 0; k < n/2; k++) {
140 for (i = 0; i < n; i++) {
141 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
142 s += input[i] * cos(a);
144 output[k] = REF_SCALE(s, nbits - 1);
149 static void idct_ref(float *output, float *input, int nbits)
156 for (i = 0; i < n; i++) {
158 for (k = 1; k < n; k++) {
159 a = M_PI*k*(i+0.5) / n;
160 s += input[k] * cos(a);
162 output[i] = 2 * s / n;
165 static void dct_ref(float *output, float *input, int nbits)
172 for (k = 0; k < n; k++) {
174 for (i = 0; i < n; i++) {
175 a = M_PI*k*(i+0.5) / n;
176 s += input[i] * cos(a);
184 static FFTSample frandom(AVLFG *prng)
186 return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
189 static int64_t gettime(void)
192 gettimeofday(&tv,NULL);
193 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
196 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
203 for (i = 0; i < n; i++) {
204 double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
206 av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
207 i, tab1[i], tab2[i]);
213 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
218 static void help(void)
220 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
221 "-h print this help\n"
226 "-i inverse transform test\n"
227 "-n b set the transform size to 2^b\n"
228 "-f x set scale factor for output data of (I)MDCT to x\n"
239 int main(int argc, char **argv)
241 FFTComplex *tab, *tab1, *tab_ref;
247 enum tf_transform transform = TRANSFORM_FFT;
249 FFTContext s1, *s = &s1;
250 FFTContext m1, *m = &m1;
252 RDFTContext r1, *r = &r1;
253 DCTContext d1, *d = &d1;
256 int fft_nbits, fft_size;
259 av_lfg_init(&prng, 1);
263 c = getopt(argc, argv, "hsimrdn:f:c:");
277 transform = TRANSFORM_MDCT;
280 transform = TRANSFORM_RDFT;
283 transform = TRANSFORM_DCT;
286 fft_nbits = atoi(optarg);
289 scale = atof(optarg);
292 cpuflags = av_parse_cpu_flags(optarg);
295 av_set_cpu_flags_mask(cpuflags);
300 fft_size = 1 << fft_nbits;
301 tab = av_malloc(fft_size * sizeof(FFTComplex));
302 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
303 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
304 tab2 = av_malloc(fft_size * sizeof(FFTSample));
308 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
310 av_log(NULL, AV_LOG_INFO,"IMDCT");
312 av_log(NULL, AV_LOG_INFO,"MDCT");
313 ff_mdct_init(m, fft_nbits, do_inverse, scale);
317 av_log(NULL, AV_LOG_INFO,"IFFT");
319 av_log(NULL, AV_LOG_INFO,"FFT");
320 ff_fft_init(s, fft_nbits, do_inverse);
321 fft_ref_init(fft_nbits, do_inverse);
326 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
328 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
329 ff_rdft_init(r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
330 fft_ref_init(fft_nbits, do_inverse);
334 av_log(NULL, AV_LOG_INFO,"DCT_III");
336 av_log(NULL, AV_LOG_INFO,"DCT_II");
337 ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
341 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
344 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
346 /* generate random data */
348 for (i = 0; i < fft_size; i++) {
349 tab1[i].re = frandom(&prng);
350 tab1[i].im = frandom(&prng);
353 /* checking result */
354 av_log(NULL, AV_LOG_INFO,"Checking...\n");
359 imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
360 m->imdct_calc(m, tab2, (FFTSample *)tab1);
361 err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
363 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
365 m->mdct_calc(m, tab2, (FFTSample *)tab1);
367 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
371 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
372 s->fft_permute(s, tab);
375 fft_ref(tab_ref, tab1, fft_nbits);
376 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
380 fft_size_2 = fft_size >> 1;
383 tab1[fft_size_2].im = 0;
384 for (i = 1; i < fft_size_2; i++) {
385 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
386 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
389 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
390 tab2[1] = tab1[fft_size_2].re;
392 r->rdft_calc(r, tab2);
393 fft_ref(tab_ref, tab1, fft_nbits);
394 for (i = 0; i < fft_size; i++) {
398 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
400 for (i = 0; i < fft_size; i++) {
401 tab2[i] = tab1[i].re;
404 r->rdft_calc(r, tab2);
405 fft_ref(tab_ref, tab1, fft_nbits);
406 tab_ref[0].im = tab_ref[fft_size_2].re;
407 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
411 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
414 idct_ref(tab_ref, tab1, fft_nbits);
416 dct_ref(tab_ref, tab1, fft_nbits);
418 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
423 /* do a speed test */
426 int64_t time_start, duration;
429 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
430 /* we measure during about 1 seconds */
433 time_start = gettime();
434 for (it = 0; it < nb_its; it++) {
438 m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
440 m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
444 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
449 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
450 r->rdft_calc(r, tab2);
453 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
454 d->dct_calc(d, tab2);
459 duration = gettime() - time_start;
460 if (duration >= 1000000)
464 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
465 (double)duration / nb_its,
466 (double)duration / 1000000.0,