fft-test: Drop unnecessary pointer indirection for context structs
[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 m, s;
232 #if FFT_FLOAT
233 RDFTContext r;
234 DCTContext d;
235 #endif /* FFT_FLOAT */
236 int it, i, err = 1;
237 int do_speed = 0, do_inverse = 0;
238 int fft_nbits = 9, fft_size;
239 double scale = 1.0;
240 AVLFG prng;
241
242 av_lfg_init(&prng, 1);
243
244 for(;;) {
245 int c = getopt(argc, argv, "hsimrdn:f:c:");
246 if (c == -1)
247 break;
248 switch(c) {
249 case 'h':
250 help();
251 return 1;
252 case 's':
253 do_speed = 1;
254 break;
255 case 'i':
256 do_inverse = 1;
257 break;
258 case 'm':
259 transform = TRANSFORM_MDCT;
260 break;
261 case 'r':
262 transform = TRANSFORM_RDFT;
263 break;
264 case 'd':
265 transform = TRANSFORM_DCT;
266 break;
267 case 'n':
268 fft_nbits = atoi(optarg);
269 break;
270 case 'f':
271 scale = atof(optarg);
272 break;
273 case 'c':
274 {
275 int cpuflags = av_parse_cpu_flags(optarg);
276 if (cpuflags < 0)
277 return 1;
278 av_set_cpu_flags_mask(cpuflags);
279 break;
280 }
281 }
282 }
283
284 fft_size = 1 << fft_nbits;
285 tab = av_malloc(fft_size * sizeof(FFTComplex));
286 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
287 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
288 tab2 = av_malloc(fft_size * sizeof(FFTSample));
289
290 switch (transform) {
291 #if CONFIG_MDCT
292 case TRANSFORM_MDCT:
293 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
294 if (do_inverse)
295 av_log(NULL, AV_LOG_INFO,"IMDCT");
296 else
297 av_log(NULL, AV_LOG_INFO,"MDCT");
298 ff_mdct_init(&m, fft_nbits, do_inverse, scale);
299 break;
300 #endif /* CONFIG_MDCT */
301 case TRANSFORM_FFT:
302 if (do_inverse)
303 av_log(NULL, AV_LOG_INFO,"IFFT");
304 else
305 av_log(NULL, AV_LOG_INFO,"FFT");
306 ff_fft_init(&s, fft_nbits, do_inverse);
307 fft_ref_init(fft_nbits, do_inverse);
308 break;
309 #if FFT_FLOAT
310 #if CONFIG_RDFT
311 case TRANSFORM_RDFT:
312 if (do_inverse)
313 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
314 else
315 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
316 ff_rdft_init(&r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
317 fft_ref_init(fft_nbits, do_inverse);
318 break;
319 #endif /* CONFIG_RDFT */
320 #if CONFIG_DCT
321 case TRANSFORM_DCT:
322 if (do_inverse)
323 av_log(NULL, AV_LOG_INFO,"DCT_III");
324 else
325 av_log(NULL, AV_LOG_INFO,"DCT_II");
326 ff_dct_init(&d, fft_nbits, do_inverse ? DCT_III : DCT_II);
327 break;
328 #endif /* CONFIG_DCT */
329 #endif /* FFT_FLOAT */
330 default:
331 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
332 return 1;
333 }
334 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
335
336 /* generate random data */
337
338 for (i = 0; i < fft_size; i++) {
339 tab1[i].re = frandom(&prng);
340 tab1[i].im = frandom(&prng);
341 }
342
343 /* checking result */
344 av_log(NULL, AV_LOG_INFO,"Checking...\n");
345
346 switch (transform) {
347 #if CONFIG_MDCT
348 case TRANSFORM_MDCT:
349 if (do_inverse) {
350 imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
351 m.imdct_calc(&m, tab2, (FFTSample *)tab1);
352 err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
353 } else {
354 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
355
356 m.mdct_calc(&m, tab2, (FFTSample *)tab1);
357
358 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
359 }
360 break;
361 #endif /* CONFIG_MDCT */
362 case TRANSFORM_FFT:
363 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
364 s.fft_permute(&s, tab);
365 s.fft_calc(&s, tab);
366
367 fft_ref(tab_ref, tab1, fft_nbits);
368 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
369 break;
370 #if FFT_FLOAT
371 #if CONFIG_RDFT
372 case TRANSFORM_RDFT:
373 {
374 int fft_size_2 = fft_size >> 1;
375 if (do_inverse) {
376 tab1[ 0].im = 0;
377 tab1[fft_size_2].im = 0;
378 for (i = 1; i < fft_size_2; i++) {
379 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
380 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
381 }
382
383 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
384 tab2[1] = tab1[fft_size_2].re;
385
386 r.rdft_calc(&r, tab2);
387 fft_ref(tab_ref, tab1, fft_nbits);
388 for (i = 0; i < fft_size; i++) {
389 tab[i].re = tab2[i];
390 tab[i].im = 0;
391 }
392 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
393 } else {
394 for (i = 0; i < fft_size; i++) {
395 tab2[i] = tab1[i].re;
396 tab1[i].im = 0;
397 }
398 r.rdft_calc(&r, tab2);
399 fft_ref(tab_ref, tab1, fft_nbits);
400 tab_ref[0].im = tab_ref[fft_size_2].re;
401 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
402 }
403 break;
404 }
405 #endif /* CONFIG_RDFT */
406 #if CONFIG_DCT
407 case TRANSFORM_DCT:
408 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
409 d.dct_calc(&d, tab);
410 if (do_inverse) {
411 idct_ref(tab_ref, tab1, fft_nbits);
412 } else {
413 dct_ref(tab_ref, tab1, fft_nbits);
414 }
415 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
416 break;
417 #endif /* CONFIG_DCT */
418 #endif /* FFT_FLOAT */
419 }
420
421 /* do a speed test */
422
423 if (do_speed) {
424 int64_t time_start, duration;
425 int nb_its;
426
427 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
428 /* we measure during about 1 seconds */
429 nb_its = 1;
430 for(;;) {
431 time_start = av_gettime();
432 for (it = 0; it < nb_its; it++) {
433 switch (transform) {
434 case TRANSFORM_MDCT:
435 if (do_inverse) {
436 m.imdct_calc(&m, (FFTSample *)tab, (FFTSample *)tab1);
437 } else {
438 m.mdct_calc(&m, (FFTSample *)tab, (FFTSample *)tab1);
439 }
440 break;
441 case TRANSFORM_FFT:
442 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
443 s.fft_calc(&s, tab);
444 break;
445 #if FFT_FLOAT
446 case TRANSFORM_RDFT:
447 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
448 r.rdft_calc(&r, tab2);
449 break;
450 case TRANSFORM_DCT:
451 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
452 d.dct_calc(&d, tab2);
453 break;
454 #endif /* FFT_FLOAT */
455 }
456 }
457 duration = av_gettime() - time_start;
458 if (duration >= 1000000)
459 break;
460 nb_its *= 2;
461 }
462 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
463 (double)duration / nb_its,
464 (double)duration / 1000000.0,
465 nb_its);
466 }
467
468 switch (transform) {
469 #if CONFIG_MDCT
470 case TRANSFORM_MDCT:
471 ff_mdct_end(&m);
472 break;
473 #endif /* CONFIG_MDCT */
474 case TRANSFORM_FFT:
475 ff_fft_end(&s);
476 break;
477 #if FFT_FLOAT
478 #if CONFIG_RDFT
479 case TRANSFORM_RDFT:
480 ff_rdft_end(&r);
481 break;
482 #endif /* CONFIG_RDFT */
483 #if CONFIG_DCT
484 case TRANSFORM_DCT:
485 ff_dct_end(&d);
486 break;
487 #endif /* CONFIG_DCT */
488 #endif /* FFT_FLOAT */
489 }
490
491 av_free(tab);
492 av_free(tab1);
493 av_free(tab2);
494 av_free(tab_ref);
495 av_free(exptab);
496
497 if (err)
498 printf("Error: %d.\n", err);
499
500 return !!err;
501 }