02096a4917b6ebd71d782cfabdfbcde3e4734bf8
[libav.git] / libavcodec / fft-test.c
1 /*
2 * (c) 2002 Fabrice Bellard
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * FFT and MDCT tests.
24 */
25
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"
31 #include "fft.h"
32 #include "dct.h"
33 #include "rdft.h"
34 #include <math.h>
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 /* reference fft */
43
44 #define MUL16(a,b) ((a) * (b))
45
46 #define CMAC(pre, pim, are, aim, bre, bim) \
47 {\
48 pre += (MUL16(are, bre) - MUL16(aim, bim));\
49 pim += (MUL16(are, bim) + MUL16(bre, aim));\
50 }
51
52 #if FFT_FLOAT
53 # define RANGE 1.0
54 # define REF_SCALE(x, bits) (x)
55 # define FMT "%10.6f"
56 #else
57 # define RANGE 16384
58 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
59 # define FMT "%6d"
60 #endif
61
62 static struct {
63 float re, im;
64 } *exptab;
65
66 static void fft_ref_init(int nbits, int inverse)
67 {
68 int i, n = 1 << nbits;
69
70 exptab = av_malloc((n / 2) * sizeof(*exptab));
71
72 for (i = 0; i < (n/2); i++) {
73 double alpha = 2 * M_PI * (float)i / (float)n;
74 double c1 = cos(alpha), s1 = sin(alpha);
75 if (!inverse)
76 s1 = -s1;
77 exptab[i].re = c1;
78 exptab[i].im = s1;
79 }
80 }
81
82 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
83 {
84 int i, j;
85 int n = 1 << nbits;
86 int n2 = n >> 1;
87
88 for (i = 0; i < n; i++) {
89 double tmp_re = 0, tmp_im = 0;
90 FFTComplex *q = tab;
91 for (j = 0; j < n; j++) {
92 double s, c;
93 int k = (i * j) & (n - 1);
94 if (k >= n2) {
95 c = -exptab[k - n2].re;
96 s = -exptab[k - n2].im;
97 } else {
98 c = exptab[k].re;
99 s = exptab[k].im;
100 }
101 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
102 q++;
103 }
104 tabr[i].re = REF_SCALE(tmp_re, nbits);
105 tabr[i].im = REF_SCALE(tmp_im, nbits);
106 }
107 }
108
109 #if CONFIG_MDCT
110 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
111 {
112 int i, k, n = 1 << nbits;
113
114 for (i = 0; i < n; i++) {
115 double sum = 0;
116 for (k = 0; k < n/2; k++) {
117 int a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
118 double f = cos(M_PI * a / (double)(2 * n));
119 sum += f * in[k];
120 }
121 out[i] = REF_SCALE(-sum, nbits - 2);
122 }
123 }
124
125 /* NOTE: no normalisation by 1 / N is done */
126 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
127 {
128 int i, k, n = 1 << nbits;
129
130 /* do it by hand */
131 for (k = 0; k < n/2; k++) {
132 double s = 0;
133 for (i = 0; i < n; i++) {
134 double a = (2 * M_PI * (2 * i + 1 + n / 2) * (2 * k + 1) / (4 * n));
135 s += input[i] * cos(a);
136 }
137 output[k] = REF_SCALE(s, nbits - 1);
138 }
139 }
140 #endif /* CONFIG_MDCT */
141
142 #if FFT_FLOAT
143 #if CONFIG_DCT
144 static void idct_ref(float *output, float *input, int nbits)
145 {
146 int i, k, n = 1 << nbits;
147
148 /* do it by hand */
149 for (i = 0; i < n; i++) {
150 double s = 0.5 * input[0];
151 for (k = 1; k < n; k++) {
152 double a = M_PI * k * (i + 0.5) / n;
153 s += input[k] * cos(a);
154 }
155 output[i] = 2 * s / n;
156 }
157 }
158 static void dct_ref(float *output, float *input, int nbits)
159 {
160 int i, k, n = 1 << nbits;
161
162 /* do it by hand */
163 for (k = 0; k < n; k++) {
164 double s = 0;
165 for (i = 0; i < n; i++) {
166 double a = M_PI * k * (i + 0.5) / n;
167 s += input[i] * cos(a);
168 }
169 output[k] = s;
170 }
171 }
172 #endif /* CONFIG_DCT */
173 #endif /* FFT_FLOAT */
174
175
176 static FFTSample frandom(AVLFG *prng)
177 {
178 return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
179 }
180
181 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
182 {
183 int i, err = 0;
184 double error = 0, max = 0;
185
186 for (i = 0; i < n; i++) {
187 double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
188 if (e >= 1e-3) {
189 av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
190 i, tab1[i], tab2[i]);
191 err = 1;
192 }
193 error+= e*e;
194 if(e>max) max= e;
195 }
196 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
197 return err;
198 }
199
200
201 static void help(void)
202 {
203 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
204 "-h print this help\n"
205 "-s speed test\n"
206 "-m (I)MDCT test\n"
207 "-d (I)DCT test\n"
208 "-r (I)RDFT test\n"
209 "-i inverse transform test\n"
210 "-n b set the transform size to 2^b\n"
211 "-f x set scale factor for output data of (I)MDCT to x\n"
212 );
213 }
214
215 enum tf_transform {
216 TRANSFORM_FFT,
217 TRANSFORM_MDCT,
218 TRANSFORM_RDFT,
219 TRANSFORM_DCT,
220 };
221
222 #if !HAVE_GETOPT
223 #include "compat/getopt.c"
224 #endif
225
226 int main(int argc, char **argv)
227 {
228 FFTComplex *tab, *tab1, *tab_ref;
229 FFTSample *tab2;
230 enum tf_transform transform = TRANSFORM_FFT;
231 FFTContext s1, *s = &s1;
232 FFTContext m1, *m = &m1;
233 #if FFT_FLOAT
234 RDFTContext r1, *r = &r1;
235 DCTContext d1, *d = &d1;
236 #endif /* FFT_FLOAT */
237 int it, i, err = 1;
238 int do_speed = 0, do_inverse = 0;
239 int fft_nbits = 9, fft_size;
240 double scale = 1.0;
241 AVLFG prng;
242
243 av_lfg_init(&prng, 1);
244
245 for(;;) {
246 int c = getopt(argc, argv, "hsimrdn:f:c:");
247 if (c == -1)
248 break;
249 switch(c) {
250 case 'h':
251 help();
252 return 1;
253 case 's':
254 do_speed = 1;
255 break;
256 case 'i':
257 do_inverse = 1;
258 break;
259 case 'm':
260 transform = TRANSFORM_MDCT;
261 break;
262 case 'r':
263 transform = TRANSFORM_RDFT;
264 break;
265 case 'd':
266 transform = TRANSFORM_DCT;
267 break;
268 case 'n':
269 fft_nbits = atoi(optarg);
270 break;
271 case 'f':
272 scale = atof(optarg);
273 break;
274 case 'c':
275 {
276 int cpuflags = av_parse_cpu_flags(optarg);
277 if (cpuflags < 0)
278 return 1;
279 av_set_cpu_flags_mask(cpuflags);
280 break;
281 }
282 }
283 }
284
285 fft_size = 1 << fft_nbits;
286 tab = av_malloc(fft_size * sizeof(FFTComplex));
287 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
288 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
289 tab2 = av_malloc(fft_size * sizeof(FFTSample));
290
291 switch (transform) {
292 #if CONFIG_MDCT
293 case TRANSFORM_MDCT:
294 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
295 if (do_inverse)
296 av_log(NULL, AV_LOG_INFO,"IMDCT");
297 else
298 av_log(NULL, AV_LOG_INFO,"MDCT");
299 ff_mdct_init(m, fft_nbits, do_inverse, scale);
300 break;
301 #endif /* CONFIG_MDCT */
302 case TRANSFORM_FFT:
303 if (do_inverse)
304 av_log(NULL, AV_LOG_INFO,"IFFT");
305 else
306 av_log(NULL, AV_LOG_INFO,"FFT");
307 ff_fft_init(s, fft_nbits, do_inverse);
308 fft_ref_init(fft_nbits, do_inverse);
309 break;
310 #if FFT_FLOAT
311 #if CONFIG_RDFT
312 case TRANSFORM_RDFT:
313 if (do_inverse)
314 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
315 else
316 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
317 ff_rdft_init(r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
318 fft_ref_init(fft_nbits, do_inverse);
319 break;
320 #endif /* CONFIG_RDFT */
321 #if CONFIG_DCT
322 case TRANSFORM_DCT:
323 if (do_inverse)
324 av_log(NULL, AV_LOG_INFO,"DCT_III");
325 else
326 av_log(NULL, AV_LOG_INFO,"DCT_II");
327 ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
328 break;
329 #endif /* CONFIG_DCT */
330 #endif /* FFT_FLOAT */
331 default:
332 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
333 return 1;
334 }
335 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
336
337 /* generate random data */
338
339 for (i = 0; i < fft_size; i++) {
340 tab1[i].re = frandom(&prng);
341 tab1[i].im = frandom(&prng);
342 }
343
344 /* checking result */
345 av_log(NULL, AV_LOG_INFO,"Checking...\n");
346
347 switch (transform) {
348 #if CONFIG_MDCT
349 case TRANSFORM_MDCT:
350 if (do_inverse) {
351 imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
352 m->imdct_calc(m, tab2, (FFTSample *)tab1);
353 err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
354 } else {
355 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
356
357 m->mdct_calc(m, tab2, (FFTSample *)tab1);
358
359 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
360 }
361 break;
362 #endif /* CONFIG_MDCT */
363 case TRANSFORM_FFT:
364 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
365 s->fft_permute(s, tab);
366 s->fft_calc(s, tab);
367
368 fft_ref(tab_ref, tab1, fft_nbits);
369 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
370 break;
371 #if FFT_FLOAT
372 #if CONFIG_RDFT
373 case TRANSFORM_RDFT:
374 {
375 int fft_size_2 = fft_size >> 1;
376 if (do_inverse) {
377 tab1[ 0].im = 0;
378 tab1[fft_size_2].im = 0;
379 for (i = 1; i < fft_size_2; i++) {
380 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
381 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
382 }
383
384 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
385 tab2[1] = tab1[fft_size_2].re;
386
387 r->rdft_calc(r, tab2);
388 fft_ref(tab_ref, tab1, fft_nbits);
389 for (i = 0; i < fft_size; i++) {
390 tab[i].re = tab2[i];
391 tab[i].im = 0;
392 }
393 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
394 } else {
395 for (i = 0; i < fft_size; i++) {
396 tab2[i] = tab1[i].re;
397 tab1[i].im = 0;
398 }
399 r->rdft_calc(r, tab2);
400 fft_ref(tab_ref, tab1, fft_nbits);
401 tab_ref[0].im = tab_ref[fft_size_2].re;
402 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
403 }
404 break;
405 }
406 #endif /* CONFIG_RDFT */
407 #if CONFIG_DCT
408 case TRANSFORM_DCT:
409 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
410 d->dct_calc(d, tab);
411 if (do_inverse) {
412 idct_ref(tab_ref, tab1, fft_nbits);
413 } else {
414 dct_ref(tab_ref, tab1, fft_nbits);
415 }
416 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
417 break;
418 #endif /* CONFIG_DCT */
419 #endif /* FFT_FLOAT */
420 }
421
422 /* do a speed test */
423
424 if (do_speed) {
425 int64_t time_start, duration;
426 int nb_its;
427
428 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
429 /* we measure during about 1 seconds */
430 nb_its = 1;
431 for(;;) {
432 time_start = av_gettime();
433 for (it = 0; it < nb_its; it++) {
434 switch (transform) {
435 case TRANSFORM_MDCT:
436 if (do_inverse) {
437 m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
438 } else {
439 m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
440 }
441 break;
442 case TRANSFORM_FFT:
443 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
444 s->fft_calc(s, tab);
445 break;
446 #if FFT_FLOAT
447 case TRANSFORM_RDFT:
448 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
449 r->rdft_calc(r, tab2);
450 break;
451 case TRANSFORM_DCT:
452 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
453 d->dct_calc(d, tab2);
454 break;
455 #endif /* FFT_FLOAT */
456 }
457 }
458 duration = av_gettime() - time_start;
459 if (duration >= 1000000)
460 break;
461 nb_its *= 2;
462 }
463 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
464 (double)duration / nb_its,
465 (double)duration / 1000000.0,
466 nb_its);
467 }
468
469 switch (transform) {
470 #if CONFIG_MDCT
471 case TRANSFORM_MDCT:
472 ff_mdct_end(m);
473 break;
474 #endif /* CONFIG_MDCT */
475 case TRANSFORM_FFT:
476 ff_fft_end(s);
477 break;
478 #if FFT_FLOAT
479 #if CONFIG_RDFT
480 case TRANSFORM_RDFT:
481 ff_rdft_end(r);
482 break;
483 #endif /* CONFIG_RDFT */
484 #if CONFIG_DCT
485 case TRANSFORM_DCT:
486 ff_dct_end(d);
487 break;
488 #endif /* CONFIG_DCT */
489 #endif /* FFT_FLOAT */
490 }
491
492 av_free(tab);
493 av_free(tab1);
494 av_free(tab2);
495 av_free(tab_ref);
496 av_free(exptab);
497
498 if (err)
499 printf("Error: %d.\n", err);
500
501 return !!err;
502 }