1 /*
2 * (c) 2002 Fabrice Bellard
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "config.h"
27
28 #include <math.h>
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
41
43 #if FFT_FLOAT
46 #endif
47
48 /* reference fft */
49
50 #define MUL16(a, b) ((a) * (b))
51
52 #define CMAC(pre, pim, are, aim, bre, bim) \
53 { \
54 pre += (MUL16(are, bre) - MUL16(aim, bim)); \
55 pim += (MUL16(are, bim) + MUL16(bre, aim)); \
56 }
57
58 #if FFT_FLOAT
60 #define REF_SCALE(x, bits) (x)
62 #elif FFT_FIXED_32
63 #define RANGE 8388608
64 #define REF_SCALE(x, bits) (x)
65 #define FMT "%6d"
66 #else
67 #define RANGE 16384
68 #define REF_SCALE(x, bits) ((x) / (1 << (bits)))
69 #define FMT "%6d"
70 #endif
71
72 static struct {
75
77 {
78 int i,
n = 1 << nbits;
79
83
84 for (i = 0; i < (n / 2); i++) {
85 double alpha = 2 *
M_PI * (float) i / (
float)
n;
86 double c1 = cos(alpha),
s1 = sin(alpha);
87 if (!inverse)
91 }
92 return 0;
93 }
94
96 {
97 int i, j;
99 int n2 = n >> 1;
100
101 for (i = 0; i <
n; i++) {
102 double tmp_re = 0, tmp_im = 0;
104 for (j = 0; j <
n; j++) {
106 int k = (i * j) & (n - 1);
107 if (k >= n2) {
110 } else {
113 }
114 CMAC(tmp_re, tmp_im, c, s, q->
re, q->
im);
115 q++;
116 }
119 }
120 }
121
122 #if CONFIG_MDCT
124 {
125 int i, k,
n = 1 << nbits;
126
127 for (i = 0; i <
n; i++) {
128 double sum = 0;
129 for (k = 0; k < n / 2; k++) {
130 int a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
131 double f = cos(
M_PI * a / (
double) (2 * n));
132 sum += f * in[k];
133 }
135 }
136 }
137
138 /* NOTE: no normalisation by 1 / N is done */
140 {
141 int i, k, n = 1 << nbits;
142
143 /* do it by hand */
144 for (k = 0; k < n / 2; k++) {
146 for (i = 0; i <
n; i++) {
147 double a = (2 *
M_PI * (2 * i + 1 + n / 2) * (2 * k + 1) / (4 *
n));
148 s += input[i] * cos(a);
149 }
151 }
152 }
153 #endif /* CONFIG_MDCT */
154
155 #if FFT_FLOAT
156 #if CONFIG_DCT
158 {
159 int i, k, n = 1 << nbits;
160
161 /* do it by hand */
162 for (i = 0; i <
n; i++) {
163 double s = 0.5 * input[0];
164 for (k = 1; k <
n; k++) {
165 double a =
M_PI * k * (i + 0.5) /
n;
166 s += input[k] * cos(
a);
167 }
168 output[i] = 2 * s /
n;
169 }
170 }
171
173 {
174 int i, k, n = 1 << nbits;
175
176 /* do it by hand */
177 for (k = 0; k <
n; k++) {
179 for (i = 0; i <
n; i++) {
180 double a =
M_PI * k * (i + 0.5) /
n;
181 s += input[i] * cos(
a);
182 }
184 }
185 }
186 #endif /* CONFIG_DCT */
187 #endif /* FFT_FLOAT */
188
190 {
192 }
193
195 {
196 int i, err = 0;
197 double error = 0, max = 0;
198
199 for (i = 0; i <
n; i++) {
200 double e = fabsf(tab1[i] - (tab2[i] / scale)) /
RANGE;
201 if (e >= 1e-3) {
203 i, tab1[i], tab2[i]);
204 err = 1;
205 }
206 error += e * e;
207 if (e > max)
208 max = e;
209 }
211 return err;
212 }
213
215 {
217 "usage: fft-test [-h] [-s] [-i] [-n b]\n"
218 "-h print this help\n"
219 "-s speed test\n"
220 "-m (I)MDCT test\n"
221 "-d (I)DCT test\n"
222 "-r (I)RDFT test\n"
223 "-i inverse transform test\n"
224 "-n b set the transform size to 2^b\n"
225 "-f x set scale factor for output data of (I)MDCT to x\n");
226 }
227
233 };
234
235 #if !HAVE_GETOPT
237 #endif
238
239 int main(
int argc,
char **argv)
240 {
245 #if FFT_FLOAT
248 #endif /* FFT_FLOAT */
249 int it, i, err = 1;
250 int do_speed = 0, do_inverse = 0;
251 int fft_nbits = 9, fft_size;
252 double scale = 1.0;
254
256
257 for (;;) {
258 int c =
getopt(argc, argv,
"hsimrdn:f:c:");
259 if (c == -1)
260 break;
261 switch (c) {
262 case 'h':
264 return 1;
265 case 's':
266 do_speed = 1;
267 break;
268 case 'i':
269 do_inverse = 1;
270 break;
271 case 'm':
273 break;
274 case 'r':
276 break;
277 case 'd':
279 break;
280 case 'n':
282 break;
283 case 'f':
285 break;
286 case 'c':
287 {
289
291 return 1;
292
294 break;
295 }
296 }
297 }
298
299 fft_size = 1 << fft_nbits;
304
305 if (!(tab && tab1 && tab_ref && tab2))
307
308 switch (transform) {
309 #if CONFIG_MDCT
312 if (do_inverse)
314 else
317 break;
318 #endif /* CONFIG_MDCT */
320 if (do_inverse)
322 else
327 break;
328 #if FFT_FLOAT
329 # if CONFIG_RDFT
331 if (do_inverse)
333 else
338 break;
339 # endif /* CONFIG_RDFT */
340 # if CONFIG_DCT
342 if (do_inverse)
344 else
347 break;
348 # endif /* CONFIG_DCT */
349 #endif /* FFT_FLOAT */
350 default:
353 }
355
356 /* generate random data */
357
358 for (i = 0; i < fft_size; i++) {
361 }
362
363 /* checking result */
365
366 switch (transform) {
367 #if CONFIG_MDCT
369 if (do_inverse) {
370 imdct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
373 } else {
374 mdct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
376 err =
check_diff(&tab_ref->
re, tab2, fft_size / 2, scale);
377 }
378 break;
379 #endif /* CONFIG_MDCT */
381 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
384
385 fft_ref(tab_ref, tab1, fft_nbits);
387 break;
388 #if FFT_FLOAT
389 #if CONFIG_RDFT
391 {
392 int fft_size_2 = fft_size >> 1;
393 if (do_inverse) {
395 tab1[fft_size_2].
im = 0;
396 for (i = 1; i < fft_size_2; i++) {
397 tab1[fft_size_2 + i].
re = tab1[fft_size_2 - i].
re;
398 tab1[fft_size_2 + i].
im = -tab1[fft_size_2 - i].
im;
399 }
400
401 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
402 tab2[1] = tab1[fft_size_2].
re;
403
405 fft_ref(tab_ref, tab1, fft_nbits);
406 for (i = 0; i < fft_size; i++) {
409 }
411 } else {
412 for (i = 0; i < fft_size; i++) {
413 tab2[i] = tab1[i].
re;
415 }
417 fft_ref(tab_ref, tab1, fft_nbits);
418 tab_ref[0].
im = tab_ref[fft_size_2].
re;
420 }
421 break;
422 }
423 #endif /* CONFIG_RDFT */
424 #if CONFIG_DCT
426 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
428 if (do_inverse)
429 idct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
430 else
431 dct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
433 break;
434 #endif /* CONFIG_DCT */
435 #endif /* FFT_FLOAT */
436 }
437
438 /* do a speed test */
439
440 if (do_speed) {
442 int nb_its;
443
445 /* we measure during about 1 seconds */
446 nb_its = 1;
447 for (;;) {
449 for (it = 0; it < nb_its; it++) {
450 switch (transform) {
452 if (do_inverse)
454 else
456 break;
458 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
460 break;
461 #if FFT_FLOAT
463 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
465 break;
467 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
469 break;
470 #endif /* FFT_FLOAT */
471 }
472 }
474 if (duration >= 1000000)
475 break;
476 nb_its *= 2;
477 }
479 "time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
480 (double) duration / nb_its,
481 (double) duration / 1000000.0,
482 nb_its);
483 }
484
485 switch (transform) {
486 #if CONFIG_MDCT
489 break;
490 #endif /* CONFIG_MDCT */
493 break;
494 #if FFT_FLOAT
495 # if CONFIG_RDFT
498 break;
499 # endif /* CONFIG_RDFT */
500 # if CONFIG_DCT
503 break;
504 # endif /* CONFIG_DCT */
505 #endif /* FFT_FLOAT */
506 }
507
514
515 if (err)
516 printf("Error: %d.\n", err);
517
518 return !!err;
519 }