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"
45 #define MUL16(a,b) ((a) * (b))
47 #define CMAC(pre, pim, are, aim, bre, bim) \
49 pre += (MUL16(are, bre) - MUL16(aim, bim));\
50 pim += (MUL16(are, bim) + MUL16(bre, aim));\
55 # define REF_SCALE(x, bits) (x)
57 #elif CONFIG_FFT_FIXED_32
58 # define RANGE 8388608
59 # define REF_SCALE(x, bits) (x)
63 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
71 static void fft_ref_init(int nbits, int inverse)
77 exptab = av_malloc((n / 2) * sizeof(*exptab));
79 for (i = 0; i < (n/2); i++) {
80 alpha = 2 * M_PI * (float)i / (float)n;
90 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
93 double tmp_re, tmp_im, s, c;
98 for (i = 0; i < n; i++) {
102 for (j = 0; j < n; j++) {
103 k = (i * j) & (n - 1);
105 c = -exptab[k - n2].re;
106 s = -exptab[k - n2].im;
111 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
114 tabr[i].re = REF_SCALE(tmp_re, nbits);
115 tabr[i].im = REF_SCALE(tmp_im, nbits);
119 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
125 for (i = 0; i < n; i++) {
127 for (k = 0; k < n/2; k++) {
128 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
129 f = cos(M_PI * a / (double)(2 * n));
132 out[i] = REF_SCALE(-sum, nbits - 2);
136 /* NOTE: no normalisation by 1 / N is done */
137 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
144 for (k = 0; k < n/2; k++) {
146 for (i = 0; i < n; i++) {
147 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
148 s += input[i] * cos(a);
150 output[k] = REF_SCALE(s, nbits - 1);
155 static void idct_ref(FFTSample *output, FFTSample *input, int nbits)
162 for (i = 0; i < n; i++) {
164 for (k = 1; k < n; k++) {
165 a = M_PI*k*(i+0.5) / n;
166 s += input[k] * cos(a);
168 output[i] = 2 * s / n;
171 static void dct_ref(FFTSample *output, FFTSample *input, int nbits)
178 for (k = 0; k < n; k++) {
180 for (i = 0; i < n; i++) {
181 a = M_PI*k*(i+0.5) / n;
182 s += input[i] * cos(a);
190 static FFTSample frandom(AVLFG *prng)
192 return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
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"
239 #include "compat/getopt.c"
242 int main(int argc, char **argv)
244 FFTComplex *tab, *tab1, *tab_ref;
250 enum tf_transform transform = TRANSFORM_FFT;
252 FFTContext s1, *s = &s1;
253 FFTContext m1, *m = &m1;
255 RDFTContext r1, *r = &r1;
256 DCTContext d1, *d = &d1;
259 int fft_nbits, fft_size;
262 av_lfg_init(&prng, 1);
266 c = getopt(argc, argv, "hsimrdn:f:c:");
280 transform = TRANSFORM_MDCT;
283 transform = TRANSFORM_RDFT;
286 transform = TRANSFORM_DCT;
289 fft_nbits = atoi(optarg);
292 scale = atof(optarg);
295 cpuflags = av_get_cpu_flags();
297 if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
300 av_force_cpu_flags(cpuflags);
305 fft_size = 1 << fft_nbits;
306 tab = av_malloc(fft_size * sizeof(FFTComplex));
307 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
308 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
309 tab2 = av_malloc(fft_size * sizeof(FFTSample));
313 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
315 av_log(NULL, AV_LOG_INFO,"IMDCT");
317 av_log(NULL, AV_LOG_INFO,"MDCT");
318 ff_mdct_init(m, fft_nbits, do_inverse, scale);
322 av_log(NULL, AV_LOG_INFO,"IFFT");
324 av_log(NULL, AV_LOG_INFO,"FFT");
325 ff_fft_init(s, fft_nbits, do_inverse);
326 fft_ref_init(fft_nbits, do_inverse);
331 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
333 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
334 ff_rdft_init(r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
335 fft_ref_init(fft_nbits, do_inverse);
340 av_log(NULL, AV_LOG_INFO,"DCT_III");
342 av_log(NULL, AV_LOG_INFO,"DCT_II");
343 ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
348 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
351 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
353 /* generate random data */
355 for (i = 0; i < fft_size; i++) {
356 tab1[i].re = frandom(&prng);
357 tab1[i].im = frandom(&prng);
360 /* checking result */
361 av_log(NULL, AV_LOG_INFO,"Checking...\n");
366 imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
367 m->imdct_calc(m, tab2, (FFTSample *)tab1);
368 err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
370 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
372 m->mdct_calc(m, tab2, (FFTSample *)tab1);
374 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
378 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
379 s->fft_permute(s, tab);
382 fft_ref(tab_ref, tab1, fft_nbits);
383 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
387 fft_size_2 = fft_size >> 1;
390 tab1[fft_size_2].im = 0;
391 for (i = 1; i < fft_size_2; i++) {
392 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
393 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
396 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
397 tab2[1] = tab1[fft_size_2].re;
399 r->rdft_calc(r, tab2);
400 fft_ref(tab_ref, tab1, fft_nbits);
401 for (i = 0; i < fft_size; i++) {
405 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
407 for (i = 0; i < fft_size; i++) {
408 tab2[i] = tab1[i].re;
411 r->rdft_calc(r, tab2);
412 fft_ref(tab_ref, tab1, fft_nbits);
413 tab_ref[0].im = tab_ref[fft_size_2].re;
414 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
418 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
419 d->dct_calc(d, (FFTSample *)tab);
421 idct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
423 dct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
425 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
430 /* do a speed test */
433 int64_t time_start, duration;
436 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
437 /* we measure during about 1 seconds */
440 time_start = av_gettime();
441 for (it = 0; it < nb_its; it++) {
445 m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
447 m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
451 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
456 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
457 r->rdft_calc(r, tab2);
460 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
461 d->dct_calc(d, tab2);
466 duration = av_gettime() - time_start;
467 if (duration >= 1000000)
471 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
472 (double)duration / nb_its,
473 (double)duration / 1000000.0,