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)
58 #elif CONFIG_FFT_FIXED_32
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((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);
120 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
126 for (i = 0; i < n; i++) {
128 for (k = 0; k < n/2; k++) {
129 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
130 f = cos(M_PI * a / (double)(2 * n));
133 out[i] = REF_SCALE(-sum, nbits - 2);
137 /* NOTE: no normalisation by 1 / N is done */
138 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
145 for (k = 0; k < n/2; k++) {
147 for (i = 0; i < n; i++) {
148 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
149 s += input[i] * cos(a);
151 output[k] = REF_SCALE(s, nbits - 1);
156 static void idct_ref(FFTSample *output, FFTSample *input, int nbits)
163 for (i = 0; i < n; i++) {
165 for (k = 1; k < n; k++) {
166 a = M_PI*k*(i+0.5) / n;
167 s += input[k] * cos(a);
169 output[i] = 2 * s / n;
172 static void dct_ref(FFTSample *output, FFTSample *input, int nbits)
179 for (k = 0; k < n; k++) {
181 for (i = 0; i < n; i++) {
182 a = M_PI*k*(i+0.5) / n;
183 s += input[i] * cos(a);
191 static FFTSample frandom(AVLFG *prng)
193 return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
196 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
203 for (i = 0; i < n; i++) {
204 double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
206 av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
207 i, tab1[i], tab2[i]);
213 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error/n));
218 static void help(void)
220 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
221 "-h print this help\n"
226 "-i inverse transform test\n"
227 "-n b set the transform size to 2^b\n"
228 "-f x set scale factor for output data of (I)MDCT to x\n"
240 #include "compat/getopt.c"
243 int main(int argc, char **argv)
245 FFTComplex *tab, *tab1, *tab_ref;
251 enum tf_transform transform = TRANSFORM_FFT;
253 FFTContext s1, *s = &s1;
254 FFTContext m1, *m = &m1;
256 RDFTContext r1, *r = &r1;
257 DCTContext d1, *d = &d1;
260 int fft_nbits, fft_size;
263 av_lfg_init(&prng, 1);
267 c = getopt(argc, argv, "hsimrdn:f:c:");
281 transform = TRANSFORM_MDCT;
284 transform = TRANSFORM_RDFT;
287 transform = TRANSFORM_DCT;
290 fft_nbits = atoi(optarg);
293 scale = atof(optarg);
296 cpuflags = av_get_cpu_flags();
298 if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
301 av_force_cpu_flags(cpuflags);
306 fft_size = 1 << fft_nbits;
307 tab = av_malloc(fft_size * sizeof(FFTComplex));
308 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
309 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
310 tab2 = av_malloc(fft_size * sizeof(FFTSample));
314 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
316 av_log(NULL, AV_LOG_INFO,"IMDCT");
318 av_log(NULL, AV_LOG_INFO,"MDCT");
319 ff_mdct_init(m, fft_nbits, do_inverse, scale);
323 av_log(NULL, AV_LOG_INFO,"IFFT");
325 av_log(NULL, AV_LOG_INFO,"FFT");
326 ff_fft_init(s, fft_nbits, do_inverse);
327 fft_ref_init(fft_nbits, do_inverse);
332 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
334 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
335 ff_rdft_init(r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
336 fft_ref_init(fft_nbits, do_inverse);
341 av_log(NULL, AV_LOG_INFO,"DCT_III");
343 av_log(NULL, AV_LOG_INFO,"DCT_II");
344 ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
349 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
352 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
354 /* generate random data */
356 for (i = 0; i < fft_size; i++) {
357 tab1[i].re = frandom(&prng);
358 tab1[i].im = frandom(&prng);
361 /* checking result */
362 av_log(NULL, AV_LOG_INFO,"Checking...\n");
367 imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
368 m->imdct_calc(m, tab2, (FFTSample *)tab1);
369 err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
371 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
373 m->mdct_calc(m, tab2, (FFTSample *)tab1);
375 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
379 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
380 s->fft_permute(s, tab);
383 fft_ref(tab_ref, tab1, fft_nbits);
384 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
388 fft_size_2 = fft_size >> 1;
391 tab1[fft_size_2].im = 0;
392 for (i = 1; i < fft_size_2; i++) {
393 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
394 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
397 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
398 tab2[1] = tab1[fft_size_2].re;
400 r->rdft_calc(r, tab2);
401 fft_ref(tab_ref, tab1, fft_nbits);
402 for (i = 0; i < fft_size; i++) {
406 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
408 for (i = 0; i < fft_size; i++) {
409 tab2[i] = tab1[i].re;
412 r->rdft_calc(r, tab2);
413 fft_ref(tab_ref, tab1, fft_nbits);
414 tab_ref[0].im = tab_ref[fft_size_2].re;
415 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
419 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
420 d->dct_calc(d, (FFTSample *)tab);
422 idct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
424 dct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
426 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
431 /* do a speed test */
434 int64_t time_start, duration;
437 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
438 /* we measure during about 1 seconds */
441 time_start = av_gettime();
442 for (it = 0; it < nb_its; it++) {
446 m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
448 m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
452 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
457 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
458 r->rdft_calc(r, tab2);
461 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
462 d->dct_calc(d, tab2);
467 duration = av_gettime() - time_start;
468 if (duration >= 1000000)
472 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
473 (double)duration / nb_its,
474 (double)duration / 1000000.0,
504 printf("Error: %d.\n", err);