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
36 #include "libavutil/cpu.h"
37 #include "libavutil/lfg.h"
38 #include "libavutil/log.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/time.h"
50 #define MUL16(a, b) ((a) * (b))
52 #define CMAC(pre, pim, are, aim, bre, bim) \
54 pre += (MUL16(are, bre) - MUL16(aim, bim)); \
55 pim += (MUL16(are, bim) + MUL16(bre, aim)); \
60 #define REF_SCALE(x, bits) (x)
64 #define REF_SCALE(x, bits) ((x) / (1 << (bits)))
72 static int fft_ref_init(int nbits, int inverse)
74 int i, n = 1 << nbits;
76 exptab = av_malloc((n / 2) * sizeof(*exptab));
78 return AVERROR(ENOMEM);
80 for (i = 0; i < (n / 2); i++) {
81 double alpha = 2 * M_PI * (float) i / (float) n;
82 double c1 = cos(alpha), s1 = sin(alpha);
91 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
97 for (i = 0; i < n; i++) {
98 double tmp_re = 0, tmp_im = 0;
100 for (j = 0; j < n; j++) {
102 int k = (i * j) & (n - 1);
104 c = -exptab[k - n2].re;
105 s = -exptab[k - n2].im;
110 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
113 tabr[i].re = REF_SCALE(tmp_re, nbits);
114 tabr[i].im = REF_SCALE(tmp_im, nbits);
119 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
121 int i, k, n = 1 << nbits;
123 for (i = 0; i < n; i++) {
125 for (k = 0; k < n / 2; k++) {
126 int a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
127 double f = cos(M_PI * a / (double) (2 * n));
130 out[i] = REF_SCALE(-sum, nbits - 2);
134 /* NOTE: no normalisation by 1 / N is done */
135 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
137 int i, k, n = 1 << nbits;
140 for (k = 0; k < n / 2; k++) {
142 for (i = 0; i < n; i++) {
143 double a = (2 * M_PI * (2 * i + 1 + n / 2) * (2 * k + 1) / (4 * n));
144 s += input[i] * cos(a);
146 output[k] = REF_SCALE(s, nbits - 1);
149 #endif /* CONFIG_MDCT */
153 static void idct_ref(float *output, float *input, int nbits)
155 int i, k, n = 1 << nbits;
158 for (i = 0; i < n; i++) {
159 double s = 0.5 * input[0];
160 for (k = 1; k < n; k++) {
161 double a = M_PI * k * (i + 0.5) / n;
162 s += input[k] * cos(a);
164 output[i] = 2 * s / n;
168 static void dct_ref(float *output, float *input, int nbits)
170 int i, k, n = 1 << nbits;
173 for (k = 0; k < n; k++) {
175 for (i = 0; i < n; i++) {
176 double a = M_PI * k * (i + 0.5) / n;
177 s += input[i] * cos(a);
182 #endif /* CONFIG_DCT */
183 #endif /* FFT_FLOAT */
185 static FFTSample frandom(AVLFG *prng)
187 return (int16_t) av_lfg_get(prng) / 32768.0 * RANGE;
190 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
193 double error = 0, max = 0;
195 for (i = 0; i < n; i++) {
196 double e = fabs(tab1[i] - (tab2[i] / scale)) / RANGE;
198 av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
199 i, tab1[i], tab2[i]);
206 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error) / n);
210 static void help(void)
212 av_log(NULL, AV_LOG_INFO,
213 "usage: fft-test [-h] [-s] [-i] [-n b]\n"
214 "-h print this help\n"
219 "-i inverse transform test\n"
220 "-n b set the transform size to 2^b\n"
221 "-f x set scale factor for output data of (I)MDCT to x\n");
232 #include "compat/getopt.c"
235 int main(int argc, char **argv)
237 FFTComplex *tab, *tab1, *tab_ref;
239 enum tf_transform transform = TRANSFORM_FFT;
244 #endif /* FFT_FLOAT */
246 int do_speed = 0, do_inverse = 0;
247 int fft_nbits = 9, fft_size;
251 av_lfg_init(&prng, 1);
254 int c = getopt(argc, argv, "hsimrdn:f:c:");
268 transform = TRANSFORM_MDCT;
271 transform = TRANSFORM_RDFT;
274 transform = TRANSFORM_DCT;
277 fft_nbits = atoi(optarg);
280 scale = atof(optarg);
284 int cpuflags = av_parse_cpu_flags(optarg);
287 av_set_cpu_flags_mask(cpuflags);
293 fft_size = 1 << fft_nbits;
294 tab = av_malloc(fft_size * sizeof(FFTComplex));
295 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
296 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
297 tab2 = av_malloc(fft_size * sizeof(FFTSample));
299 if (!(tab && tab1 && tab_ref && tab2))
305 av_log(NULL, AV_LOG_INFO, "Scale factor is set to %f\n", scale);
307 av_log(NULL, AV_LOG_INFO, "IMDCT");
309 av_log(NULL, AV_LOG_INFO, "MDCT");
310 ff_mdct_init(&m, fft_nbits, do_inverse, scale);
312 #endif /* CONFIG_MDCT */
315 av_log(NULL, AV_LOG_INFO, "IFFT");
317 av_log(NULL, AV_LOG_INFO, "FFT");
318 ff_fft_init(&s, fft_nbits, do_inverse);
319 if (err = fft_ref_init(fft_nbits, do_inverse) < 0)
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 if (err = fft_ref_init(fft_nbits, do_inverse) < 0)
333 #endif /* CONFIG_RDFT */
337 av_log(NULL, AV_LOG_INFO, "DCT_III");
339 av_log(NULL, AV_LOG_INFO, "DCT_II");
340 ff_dct_init(&d, fft_nbits, do_inverse ? DCT_III : DCT_II);
342 #endif /* CONFIG_DCT */
343 #endif /* FFT_FLOAT */
345 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
348 av_log(NULL, AV_LOG_INFO, " %d test\n", fft_size);
350 /* generate random data */
352 for (i = 0; i < fft_size; i++) {
353 tab1[i].re = frandom(&prng);
354 tab1[i].im = frandom(&prng);
357 /* checking result */
358 av_log(NULL, AV_LOG_INFO, "Checking...\n");
364 imdct_ref(&tab_ref->re, &tab1->re, fft_nbits);
365 m.imdct_calc(&m, tab2, &tab1->re);
366 err = check_diff(&tab_ref->re, tab2, fft_size, scale);
368 mdct_ref(&tab_ref->re, &tab1->re, fft_nbits);
369 m.mdct_calc(&m, tab2, &tab1->re);
370 err = check_diff(&tab_ref->re, tab2, fft_size / 2, scale);
373 #endif /* CONFIG_MDCT */
375 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
376 s.fft_permute(&s, tab);
379 fft_ref(tab_ref, tab1, fft_nbits);
380 err = check_diff(&tab_ref->re, &tab->re, fft_size * 2, 1.0);
386 int fft_size_2 = fft_size >> 1;
389 tab1[fft_size_2].im = 0;
390 for (i = 1; i < fft_size_2; i++) {
391 tab1[fft_size_2 + i].re = tab1[fft_size_2 - i].re;
392 tab1[fft_size_2 + i].im = -tab1[fft_size_2 - i].im;
395 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
396 tab2[1] = tab1[fft_size_2].re;
398 r.rdft_calc(&r, tab2);
399 fft_ref(tab_ref, tab1, fft_nbits);
400 for (i = 0; i < fft_size; i++) {
404 err = check_diff(&tab_ref->re, &tab->re, fft_size * 2, 0.5);
406 for (i = 0; i < fft_size; i++) {
407 tab2[i] = tab1[i].re;
410 r.rdft_calc(&r, tab2);
411 fft_ref(tab_ref, tab1, fft_nbits);
412 tab_ref[0].im = tab_ref[fft_size_2].re;
413 err = check_diff(&tab_ref->re, tab2, fft_size, 1.0);
417 #endif /* CONFIG_RDFT */
420 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
421 d.dct_calc(&d, &tab->re);
423 idct_ref(&tab_ref->re, &tab1->re, fft_nbits);
425 dct_ref(&tab_ref->re, &tab1->re, fft_nbits);
426 err = check_diff(&tab_ref->re, &tab->re, fft_size, 1.0);
428 #endif /* CONFIG_DCT */
429 #endif /* FFT_FLOAT */
432 /* do a speed test */
435 int64_t time_start, duration;
438 av_log(NULL, AV_LOG_INFO, "Speed test...\n");
439 /* we measure during about 1 seconds */
442 time_start = av_gettime_relative();
443 for (it = 0; it < nb_its; it++) {
447 m.imdct_calc(&m, &tab->re, &tab1->re);
449 m.mdct_calc(&m, &tab->re, &tab1->re);
452 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
457 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
458 r.rdft_calc(&r, tab2);
461 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
462 d.dct_calc(&d, tab2);
464 #endif /* FFT_FLOAT */
467 duration = av_gettime_relative() - time_start;
468 if (duration >= 1000000)
472 av_log(NULL, AV_LOG_INFO,
473 "time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
474 (double) duration / nb_its,
475 (double) duration / 1000000.0,
484 #endif /* CONFIG_MDCT */
493 #endif /* CONFIG_RDFT */
498 #endif /* CONFIG_DCT */
499 #endif /* FFT_FLOAT */
510 printf("Error: %d.\n", err);