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"
30 #include "libavutil/time.h"
44 #define MUL16(a,b) ((a) * (b))
46 #define CMAC(pre, pim, are, aim, bre, bim) \
48 pre += (MUL16(are, bre) - MUL16(aim, bim));\
49 pim += (MUL16(are, bim) + MUL16(bre, aim));\
54 # define REF_SCALE(x, bits) (x)
58 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
66 static void fft_ref_init(int nbits, int inverse)
72 exptab = av_malloc((n / 2) * sizeof(*exptab));
74 for (i = 0; i < (n/2); i++) {
75 alpha = 2 * M_PI * (float)i / (float)n;
85 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
88 double tmp_re, tmp_im, s, c;
93 for (i = 0; i < n; i++) {
97 for (j = 0; j < n; j++) {
98 k = (i * j) & (n - 1);
100 c = -exptab[k - n2].re;
101 s = -exptab[k - n2].im;
106 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
109 tabr[i].re = REF_SCALE(tmp_re, nbits);
110 tabr[i].im = REF_SCALE(tmp_im, nbits);
115 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
121 for (i = 0; i < n; i++) {
123 for (k = 0; k < n/2; k++) {
124 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
125 f = cos(M_PI * a / (double)(2 * n));
128 out[i] = REF_SCALE(-sum, nbits - 2);
132 /* NOTE: no normalisation by 1 / N is done */
133 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
140 for (k = 0; k < n/2; k++) {
142 for (i = 0; i < n; i++) {
143 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)
160 for (i = 0; i < n; i++) {
162 for (k = 1; k < n; k++) {
163 a = M_PI*k*(i+0.5) / n;
164 s += input[k] * cos(a);
166 output[i] = 2 * s / n;
169 static void dct_ref(float *output, float *input, int nbits)
176 for (k = 0; k < n; k++) {
178 for (i = 0; i < n; i++) {
179 a = M_PI*k*(i+0.5) / n;
180 s += input[i] * cos(a);
185 #endif /* CONFIG_DCT */
186 #endif /* FFT_FLOAT */
189 static FFTSample frandom(AVLFG *prng)
191 return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
194 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
201 for (i = 0; i < n; i++) {
202 double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
204 av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
205 i, tab1[i], tab2[i]);
211 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
216 static void help(void)
218 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
219 "-h print this help\n"
224 "-i inverse transform test\n"
225 "-n b set the transform size to 2^b\n"
226 "-f x set scale factor for output data of (I)MDCT to x\n"
238 #include "compat/getopt.c"
241 int main(int argc, char **argv)
243 FFTComplex *tab, *tab1, *tab_ref;
249 enum tf_transform transform = TRANSFORM_FFT;
251 FFTContext s1, *s = &s1;
252 FFTContext m1, *m = &m1;
254 RDFTContext r1, *r = &r1;
255 DCTContext d1, *d = &d1;
257 #endif /* FFT_FLOAT */
258 int fft_nbits, fft_size;
261 av_lfg_init(&prng, 1);
265 c = getopt(argc, argv, "hsimrdn:f:c:");
279 transform = TRANSFORM_MDCT;
282 transform = TRANSFORM_RDFT;
285 transform = TRANSFORM_DCT;
288 fft_nbits = atoi(optarg);
291 scale = atof(optarg);
294 cpuflags = av_parse_cpu_flags(optarg);
297 av_set_cpu_flags_mask(cpuflags);
302 fft_size = 1 << fft_nbits;
303 tab = av_malloc(fft_size * sizeof(FFTComplex));
304 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
305 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
306 tab2 = av_malloc(fft_size * sizeof(FFTSample));
311 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
313 av_log(NULL, AV_LOG_INFO,"IMDCT");
315 av_log(NULL, AV_LOG_INFO,"MDCT");
316 ff_mdct_init(m, fft_nbits, do_inverse, scale);
318 #endif /* CONFIG_MDCT */
321 av_log(NULL, AV_LOG_INFO,"IFFT");
323 av_log(NULL, AV_LOG_INFO,"FFT");
324 ff_fft_init(s, fft_nbits, do_inverse);
325 fft_ref_init(fft_nbits, do_inverse);
331 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
333 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
334 ff_rdft_init(r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
335 fft_ref_init(fft_nbits, do_inverse);
337 #endif /* CONFIG_RDFT */
341 av_log(NULL, AV_LOG_INFO,"DCT_III");
343 av_log(NULL, AV_LOG_INFO,"DCT_II");
344 ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
346 #endif /* CONFIG_DCT */
347 #endif /* FFT_FLOAT */
349 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
352 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
354 /* generate random data */
356 for (i = 0; i < fft_size; i++) {
357 tab1[i].re = frandom(&prng);
358 tab1[i].im = frandom(&prng);
361 /* checking result */
362 av_log(NULL, AV_LOG_INFO,"Checking...\n");
368 imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
369 m->imdct_calc(m, tab2, (FFTSample *)tab1);
370 err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
372 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
374 m->mdct_calc(m, tab2, (FFTSample *)tab1);
376 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
379 #endif /* CONFIG_MDCT */
381 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
382 s->fft_permute(s, tab);
385 fft_ref(tab_ref, tab1, fft_nbits);
386 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
391 fft_size_2 = fft_size >> 1;
394 tab1[fft_size_2].im = 0;
395 for (i = 1; i < fft_size_2; i++) {
396 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
397 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
400 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
401 tab2[1] = tab1[fft_size_2].re;
403 r->rdft_calc(r, tab2);
404 fft_ref(tab_ref, tab1, fft_nbits);
405 for (i = 0; i < fft_size; i++) {
409 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
411 for (i = 0; i < fft_size; i++) {
412 tab2[i] = tab1[i].re;
415 r->rdft_calc(r, tab2);
416 fft_ref(tab_ref, tab1, fft_nbits);
417 tab_ref[0].im = tab_ref[fft_size_2].re;
418 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
421 #endif /* CONFIG_RDFT */
424 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
427 idct_ref(tab_ref, tab1, fft_nbits);
429 dct_ref(tab_ref, tab1, fft_nbits);
431 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
433 #endif /* CONFIG_DCT */
434 #endif /* FFT_FLOAT */
437 /* do a speed test */
440 int64_t time_start, duration;
443 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
444 /* we measure during about 1 seconds */
447 time_start = av_gettime();
448 for (it = 0; it < nb_its; it++) {
452 m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
454 m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
458 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
463 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
464 r->rdft_calc(r, tab2);
467 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
468 d->dct_calc(d, tab2);
470 #endif /* FFT_FLOAT */
473 duration = av_gettime() - time_start;
474 if (duration >= 1000000)
478 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
479 (double)duration / nb_its,
480 (double)duration / 1000000.0,
489 #endif /* CONFIG_MDCT */
498 #endif /* CONFIG_RDFT */
503 #endif /* CONFIG_DCT */
504 #endif /* FFT_FLOAT */
514 printf("Error: %d.\n", err);