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/mathematics.h"
27 #include "libavutil/lfg.h"
28 #include "libavutil/log.h"
42 #define MUL16(a,b) ((a) * (b))
44 #define CMAC(pre, pim, are, aim, bre, bim) \
46 pre += (MUL16(are, bre) - MUL16(aim, bim));\
47 pim += (MUL16(are, bim) + MUL16(bre, aim));\
52 # define REF_SCALE(x, bits) (x)
56 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
64 static void fft_ref_init(int nbits, int inverse)
70 exptab = av_malloc((n / 2) * sizeof(*exptab));
72 for (i = 0; i < (n/2); i++) {
73 alpha = 2 * M_PI * (float)i / (float)n;
83 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
86 double tmp_re, tmp_im, s, c;
91 for (i = 0; i < n; i++) {
95 for (j = 0; j < n; j++) {
96 k = (i * j) & (n - 1);
98 c = -exptab[k - n2].re;
99 s = -exptab[k - n2].im;
104 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
107 tabr[i].re = REF_SCALE(tmp_re, nbits);
108 tabr[i].im = REF_SCALE(tmp_im, nbits);
112 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
118 for (i = 0; i < n; i++) {
120 for (k = 0; k < n/2; k++) {
121 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
122 f = cos(M_PI * a / (double)(2 * n));
125 out[i] = REF_SCALE(-sum, nbits - 2);
129 /* NOTE: no normalisation by 1 / N is done */
130 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
137 for (k = 0; k < n/2; k++) {
139 for (i = 0; i < n; i++) {
140 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
141 s += input[i] * cos(a);
143 output[k] = REF_SCALE(s, nbits - 1);
148 static void idct_ref(float *output, float *input, int nbits)
155 for (i = 0; i < n; i++) {
157 for (k = 1; k < n; k++) {
158 a = M_PI*k*(i+0.5) / n;
159 s += input[k] * cos(a);
161 output[i] = 2 * s / n;
164 static void dct_ref(float *output, float *input, int nbits)
171 for (k = 0; k < n; k++) {
173 for (i = 0; i < n; i++) {
174 a = M_PI*k*(i+0.5) / n;
175 s += input[i] * cos(a);
183 static FFTSample frandom(AVLFG *prng)
185 return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
188 static int64_t gettime(void)
191 gettimeofday(&tv,NULL);
192 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
195 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
202 for (i = 0; i < n; i++) {
203 double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
205 av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
206 i, tab1[i], tab2[i]);
212 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
217 static void help(void)
219 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
220 "-h print this help\n"
225 "-i inverse transform test\n"
226 "-n b set the transform size to 2^b\n"
227 "-f x set scale factor for output data of (I)MDCT to x\n"
238 int main(int argc, char **argv)
240 FFTComplex *tab, *tab1, *tab_ref;
245 enum tf_transform transform = TRANSFORM_FFT;
247 FFTContext s1, *s = &s1;
248 FFTContext m1, *m = &m1;
250 RDFTContext r1, *r = &r1;
251 DCTContext d1, *d = &d1;
254 int fft_nbits, fft_size;
257 av_lfg_init(&prng, 1);
261 c = getopt(argc, argv, "hsimrdn:f:");
275 transform = TRANSFORM_MDCT;
278 transform = TRANSFORM_RDFT;
281 transform = TRANSFORM_DCT;
284 fft_nbits = atoi(optarg);
287 scale = atof(optarg);
292 fft_size = 1 << fft_nbits;
293 tab = av_malloc(fft_size * sizeof(FFTComplex));
294 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
295 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
296 tab2 = av_malloc(fft_size * sizeof(FFTSample));
300 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
302 av_log(NULL, AV_LOG_INFO,"IMDCT");
304 av_log(NULL, AV_LOG_INFO,"MDCT");
305 ff_mdct_init(m, fft_nbits, do_inverse, scale);
309 av_log(NULL, AV_LOG_INFO,"IFFT");
311 av_log(NULL, AV_LOG_INFO,"FFT");
312 ff_fft_init(s, fft_nbits, do_inverse);
313 fft_ref_init(fft_nbits, do_inverse);
318 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
320 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
321 ff_rdft_init(r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
322 fft_ref_init(fft_nbits, do_inverse);
326 av_log(NULL, AV_LOG_INFO,"DCT_III");
328 av_log(NULL, AV_LOG_INFO,"DCT_II");
329 ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
333 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
336 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
338 /* generate random data */
340 for (i = 0; i < fft_size; i++) {
341 tab1[i].re = frandom(&prng);
342 tab1[i].im = frandom(&prng);
345 /* checking result */
346 av_log(NULL, AV_LOG_INFO,"Checking...\n");
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);
355 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
357 m->mdct_calc(m, tab2, (FFTSample *)tab1);
359 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
363 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
364 s->fft_permute(s, tab);
367 fft_ref(tab_ref, tab1, fft_nbits);
368 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
372 fft_size_2 = fft_size >> 1;
375 tab1[fft_size_2].im = 0;
376 for (i = 1; i < fft_size_2; i++) {
377 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
378 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
381 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
382 tab2[1] = tab1[fft_size_2].re;
384 r->rdft_calc(r, tab2);
385 fft_ref(tab_ref, tab1, fft_nbits);
386 for (i = 0; i < fft_size; i++) {
390 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
392 for (i = 0; i < fft_size; i++) {
393 tab2[i] = tab1[i].re;
396 r->rdft_calc(r, tab2);
397 fft_ref(tab_ref, tab1, fft_nbits);
398 tab_ref[0].im = tab_ref[fft_size_2].re;
399 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
403 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
406 idct_ref(tab_ref, tab1, fft_nbits);
408 dct_ref(tab_ref, tab1, fft_nbits);
410 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
415 /* do a speed test */
418 int64_t time_start, duration;
421 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
422 /* we measure during about 1 seconds */
425 time_start = gettime();
426 for (it = 0; it < nb_its; it++) {
430 m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
432 m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
436 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
441 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
442 r->rdft_calc(r, tab2);
445 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
446 d->dct_calc(d, tab2);
451 duration = gettime() - time_start;
452 if (duration >= 1000000)
456 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
457 (double)duration / nb_its,
458 (double)duration / 1000000.0,