2 * (c) 2002 Fabrice Bellard
4 * This file is part of FFmpeg.
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.
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.
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
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"
46 #define MUL16(a,b) ((a) * (b))
48 #define CMAC(pre, pim, are, aim, bre, bim) \
50 pre += (MUL16(are, bre) - MUL16(aim, bim));\
51 pim += (MUL16(are, bim) + MUL16(bre, aim));\
56 # define REF_SCALE(x, bits) (x)
59 # define RANGE 8388608
60 # define REF_SCALE(x, bits) (x)
64 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
72 static void fft_ref_init(int nbits, int inverse)
78 exptab = av_malloc_array((n / 2), sizeof(*exptab));
80 for (i = 0; i < (n/2); i++) {
81 alpha = 2 * M_PI * (float)i / (float)n;
91 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
94 double tmp_re, tmp_im, s, c;
99 for (i = 0; i < n; i++) {
103 for (j = 0; j < n; j++) {
104 k = (i * j) & (n - 1);
106 c = -exptab[k - n2].re;
107 s = -exptab[k - n2].im;
112 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
115 tabr[i].re = REF_SCALE(tmp_re, nbits);
116 tabr[i].im = REF_SCALE(tmp_im, nbits);
121 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
127 for (i = 0; i < n; i++) {
129 for (k = 0; k < n/2; k++) {
130 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
131 f = cos(M_PI * a / (double)(2 * n));
134 out[i] = REF_SCALE(-sum, nbits - 2);
138 /* NOTE: no normalisation by 1 / N is done */
139 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
146 for (k = 0; k < n/2; k++) {
148 for (i = 0; i < n; i++) {
149 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
150 s += input[i] * cos(a);
152 output[k] = REF_SCALE(s, nbits - 1);
155 #endif /* CONFIG_MDCT */
159 static void idct_ref(FFTSample *output, FFTSample *input, int nbits)
166 for (i = 0; i < n; i++) {
168 for (k = 1; k < n; k++) {
169 a = M_PI*k*(i+0.5) / n;
170 s += input[k] * cos(a);
172 output[i] = 2 * s / n;
175 static void dct_ref(FFTSample *output, FFTSample *input, int nbits)
182 for (k = 0; k < n; k++) {
184 for (i = 0; i < n; i++) {
185 a = M_PI*k*(i+0.5) / n;
186 s += input[i] * cos(a);
191 #endif /* CONFIG_DCT */
195 static FFTSample frandom(AVLFG *prng)
197 return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
200 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
207 for (i = 0; i < n; i++) {
208 double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
210 av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
211 i, tab1[i], tab2[i]);
217 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error/n));
222 static void help(void)
224 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
225 "-h print this help\n"
230 "-i inverse transform test\n"
231 "-n b set the transform size to 2^b\n"
232 "-f x set scale factor for output data of (I)MDCT to x\n"
244 #include "compat/getopt.c"
247 int main(int argc, char **argv)
249 FFTComplex *tab, *tab1, *tab_ref;
255 enum tf_transform transform = TRANSFORM_FFT;
257 FFTContext s1, *s = &s1;
258 FFTContext m1, *m = &m1;
260 RDFTContext r1, *r = &r1;
261 DCTContext d1, *d = &d1;
264 int fft_nbits, fft_size;
267 av_lfg_init(&prng, 1);
271 c = getopt(argc, argv, "hsimrdn:f:c:");
285 transform = TRANSFORM_MDCT;
288 transform = TRANSFORM_RDFT;
291 transform = TRANSFORM_DCT;
294 fft_nbits = atoi(optarg);
297 scale = atof(optarg);
300 cpuflags = av_get_cpu_flags();
302 if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
305 av_force_cpu_flags(cpuflags);
310 fft_size = 1 << fft_nbits;
311 tab = av_malloc_array(fft_size, sizeof(FFTComplex));
312 tab1 = av_malloc_array(fft_size, sizeof(FFTComplex));
313 tab_ref = av_malloc_array(fft_size, sizeof(FFTComplex));
314 tab2 = av_malloc_array(fft_size, sizeof(FFTSample));
319 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
321 av_log(NULL, AV_LOG_INFO,"IMDCT");
323 av_log(NULL, AV_LOG_INFO,"MDCT");
324 ff_mdct_init(m, fft_nbits, do_inverse, scale);
326 #endif /* CONFIG_MDCT */
329 av_log(NULL, AV_LOG_INFO,"IFFT");
331 av_log(NULL, AV_LOG_INFO,"FFT");
332 ff_fft_init(s, fft_nbits, do_inverse);
333 fft_ref_init(fft_nbits, do_inverse);
339 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
341 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
342 ff_rdft_init(r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
343 fft_ref_init(fft_nbits, do_inverse);
345 # endif /* CONFIG_RDFT */
349 av_log(NULL, AV_LOG_INFO,"DCT_III");
351 av_log(NULL, AV_LOG_INFO,"DCT_II");
352 ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
354 # endif /* CONFIG_DCT */
357 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
360 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
362 /* generate random data */
364 for (i = 0; i < fft_size; i++) {
365 tab1[i].re = frandom(&prng);
366 tab1[i].im = frandom(&prng);
369 /* checking result */
370 av_log(NULL, AV_LOG_INFO,"Checking...\n");
376 imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
377 m->imdct_calc(m, tab2, (FFTSample *)tab1);
378 err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
380 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
382 m->mdct_calc(m, tab2, (FFTSample *)tab1);
384 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
387 #endif /* CONFIG_MDCT */
389 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
390 s->fft_permute(s, tab);
393 fft_ref(tab_ref, tab1, fft_nbits);
394 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
399 fft_size_2 = fft_size >> 1;
402 tab1[fft_size_2].im = 0;
403 for (i = 1; i < fft_size_2; i++) {
404 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
405 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
408 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
409 tab2[1] = tab1[fft_size_2].re;
411 r->rdft_calc(r, tab2);
412 fft_ref(tab_ref, tab1, fft_nbits);
413 for (i = 0; i < fft_size; i++) {
417 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
419 for (i = 0; i < fft_size; i++) {
420 tab2[i] = tab1[i].re;
423 r->rdft_calc(r, tab2);
424 fft_ref(tab_ref, tab1, fft_nbits);
425 tab_ref[0].im = tab_ref[fft_size_2].re;
426 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
429 #endif /* CONFIG_RDFT */
432 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
433 d->dct_calc(d, (FFTSample *)tab);
435 idct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
437 dct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
439 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
441 #endif /* CONFIG_DCT */
445 /* do a speed test */
448 int64_t time_start, duration;
451 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
452 /* we measure during about 1 seconds */
455 time_start = av_gettime_relative();
456 for (it = 0; it < nb_its; it++) {
460 m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
462 m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
466 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
471 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
472 r->rdft_calc(r, tab2);
475 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
476 d->dct_calc(d, tab2);
481 duration = av_gettime_relative() - time_start;
482 if (duration >= 1000000)
486 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
487 (double)duration / nb_its,
488 (double)duration / 1000000.0,
497 #endif /* CONFIG_MDCT */
506 # endif /* CONFIG_RDFT */
511 # endif /* CONFIG_DCT */
522 printf("Error: %d.\n", err);