]> git.sesse.net Git - ffmpeg/blob - libavcodec/fft-test.c
Merge commit 'f50803354c6acb4575379d7c54ca48ec5d36dd61'
[ffmpeg] / libavcodec / fft-test.c
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 "libavutil/cpu.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/lfg.h"
29 #include "libavutil/log.h"
30 #include "libavutil/time.h"
31 #include "fft.h"
32 #if CONFIG_FFT_FLOAT
33 #include "dct.h"
34 #include "rdft.h"
35 #endif
36 #include <math.h>
37 #if HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #include <stdlib.h>
41 #include <string.h>
42
43 /* reference fft */
44
45 #define MUL16(a,b) ((a) * (b))
46
47 #define CMAC(pre, pim, are, aim, bre, bim) \
48 {\
49    pre += (MUL16(are, bre) - MUL16(aim, bim));\
50    pim += (MUL16(are, bim) + MUL16(bre, aim));\
51 }
52
53 #if CONFIG_FFT_FLOAT
54 #   define RANGE 1.0
55 #   define REF_SCALE(x, bits)  (x)
56 #   define FMT "%10.6f"
57 #elif CONFIG_FFT_FIXED_32
58 #   define RANGE 8388608
59 #   define REF_SCALE(x, bits) (x)
60 #   define FMT "%6d"
61 #else
62 #   define RANGE 16384
63 #   define REF_SCALE(x, bits) ((x) / (1<<(bits)))
64 #   define FMT "%6d"
65 #endif
66
67 struct {
68     float re, im;
69 } *exptab;
70
71 static void fft_ref_init(int nbits, int inverse)
72 {
73     int n, i;
74     double c1, s1, alpha;
75
76     n = 1 << nbits;
77     exptab = av_malloc((n / 2) * sizeof(*exptab));
78
79     for (i = 0; i < (n/2); i++) {
80         alpha = 2 * M_PI * (float)i / (float)n;
81         c1 = cos(alpha);
82         s1 = sin(alpha);
83         if (!inverse)
84             s1 = -s1;
85         exptab[i].re = c1;
86         exptab[i].im = s1;
87     }
88 }
89
90 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
91 {
92     int n, i, j, k, n2;
93     double tmp_re, tmp_im, s, c;
94     FFTComplex *q;
95
96     n = 1 << nbits;
97     n2 = n >> 1;
98     for (i = 0; i < n; i++) {
99         tmp_re = 0;
100         tmp_im = 0;
101         q = tab;
102         for (j = 0; j < n; j++) {
103             k = (i * j) & (n - 1);
104             if (k >= n2) {
105                 c = -exptab[k - n2].re;
106                 s = -exptab[k - n2].im;
107             } else {
108                 c = exptab[k].re;
109                 s = exptab[k].im;
110             }
111             CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
112             q++;
113         }
114         tabr[i].re = REF_SCALE(tmp_re, nbits);
115         tabr[i].im = REF_SCALE(tmp_im, nbits);
116     }
117 }
118
119 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
120 {
121     int n = 1<<nbits;
122     int k, i, a;
123     double sum, f;
124
125     for (i = 0; i < n; i++) {
126         sum = 0;
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));
130             sum += f * in[k];
131         }
132         out[i] = REF_SCALE(-sum, nbits - 2);
133     }
134 }
135
136 /* NOTE: no normalisation by 1 / N is done */
137 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
138 {
139     int n = 1<<nbits;
140     int k, i;
141     double a, s;
142
143     /* do it by hand */
144     for (k = 0; k < n/2; k++) {
145         s = 0;
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);
149         }
150         output[k] = REF_SCALE(s, nbits - 1);
151     }
152 }
153
154 #if CONFIG_FFT_FLOAT
155 static void idct_ref(FFTSample *output, FFTSample *input, int nbits)
156 {
157     int n = 1<<nbits;
158     int k, i;
159     double a, s;
160
161     /* do it by hand */
162     for (i = 0; i < n; i++) {
163         s = 0.5 * input[0];
164         for (k = 1; k < n; k++) {
165             a = M_PI*k*(i+0.5) / n;
166             s += input[k] * cos(a);
167         }
168         output[i] = 2 * s / n;
169     }
170 }
171 static void dct_ref(FFTSample *output, FFTSample *input, int nbits)
172 {
173     int n = 1<<nbits;
174     int k, i;
175     double a, s;
176
177     /* do it by hand */
178     for (k = 0; k < n; k++) {
179         s = 0;
180         for (i = 0; i < n; i++) {
181             a = M_PI*k*(i+0.5) / n;
182             s += input[i] * cos(a);
183         }
184         output[k] = s;
185     }
186 }
187 #endif
188
189
190 static FFTSample frandom(AVLFG *prng)
191 {
192     return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
193 }
194
195 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
196 {
197     int i;
198     double max= 0;
199     double error= 0;
200     int err = 0;
201
202     for (i = 0; i < n; i++) {
203         double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
204         if (e >= 1e-3) {
205             av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
206                    i, tab1[i], tab2[i]);
207             err = 1;
208         }
209         error+= e*e;
210         if(e>max) max= e;
211     }
212     av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error/n));
213     return err;
214 }
215
216
217 static void help(void)
218 {
219     av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
220            "-h     print this help\n"
221            "-s     speed test\n"
222            "-m     (I)MDCT test\n"
223            "-d     (I)DCT test\n"
224            "-r     (I)RDFT test\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"
228            );
229 }
230
231 enum tf_transform {
232     TRANSFORM_FFT,
233     TRANSFORM_MDCT,
234     TRANSFORM_RDFT,
235     TRANSFORM_DCT,
236 };
237
238 #if !HAVE_GETOPT
239 #include "compat/getopt.c"
240 #endif
241
242 int main(int argc, char **argv)
243 {
244     FFTComplex *tab, *tab1, *tab_ref;
245     FFTSample *tab2;
246     int it, i, c;
247     int cpuflags;
248     int do_speed = 0;
249     int err = 1;
250     enum tf_transform transform = TRANSFORM_FFT;
251     int do_inverse = 0;
252     FFTContext s1, *s = &s1;
253     FFTContext m1, *m = &m1;
254 #if CONFIG_FFT_FLOAT
255     RDFTContext r1, *r = &r1;
256     DCTContext d1, *d = &d1;
257     int fft_size_2;
258 #endif
259     int fft_nbits, fft_size;
260     double scale = 1.0;
261     AVLFG prng;
262     av_lfg_init(&prng, 1);
263
264     fft_nbits = 9;
265     for(;;) {
266         c = getopt(argc, argv, "hsimrdn:f:c:");
267         if (c == -1)
268             break;
269         switch(c) {
270         case 'h':
271             help();
272             return 1;
273         case 's':
274             do_speed = 1;
275             break;
276         case 'i':
277             do_inverse = 1;
278             break;
279         case 'm':
280             transform = TRANSFORM_MDCT;
281             break;
282         case 'r':
283             transform = TRANSFORM_RDFT;
284             break;
285         case 'd':
286             transform = TRANSFORM_DCT;
287             break;
288         case 'n':
289             fft_nbits = atoi(optarg);
290             break;
291         case 'f':
292             scale = atof(optarg);
293             break;
294         case 'c':
295             cpuflags = av_get_cpu_flags();
296
297             if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
298                 return 1;
299
300             av_force_cpu_flags(cpuflags);
301             break;
302         }
303     }
304
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));
310
311     switch (transform) {
312     case TRANSFORM_MDCT:
313         av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
314         if (do_inverse)
315             av_log(NULL, AV_LOG_INFO,"IMDCT");
316         else
317             av_log(NULL, AV_LOG_INFO,"MDCT");
318         ff_mdct_init(m, fft_nbits, do_inverse, scale);
319         break;
320     case TRANSFORM_FFT:
321         if (do_inverse)
322             av_log(NULL, AV_LOG_INFO,"IFFT");
323         else
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);
327         break;
328 #if CONFIG_FFT_FLOAT
329     case TRANSFORM_RDFT:
330         if (do_inverse)
331             av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
332         else
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);
336         break;
337 #    if CONFIG_DCT
338     case TRANSFORM_DCT:
339         if (do_inverse)
340             av_log(NULL, AV_LOG_INFO,"DCT_III");
341         else
342             av_log(NULL, AV_LOG_INFO,"DCT_II");
343         ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
344         break;
345 #    endif
346 #endif
347     default:
348         av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
349         return 1;
350     }
351     av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
352
353     /* generate random data */
354
355     for (i = 0; i < fft_size; i++) {
356         tab1[i].re = frandom(&prng);
357         tab1[i].im = frandom(&prng);
358     }
359
360     /* checking result */
361     av_log(NULL, AV_LOG_INFO,"Checking...\n");
362
363     switch (transform) {
364     case TRANSFORM_MDCT:
365         if (do_inverse) {
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);
369         } else {
370             mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
371
372             m->mdct_calc(m, tab2, (FFTSample *)tab1);
373
374             err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
375         }
376         break;
377     case TRANSFORM_FFT:
378         memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
379         s->fft_permute(s, tab);
380         s->fft_calc(s, tab);
381
382         fft_ref(tab_ref, tab1, fft_nbits);
383         err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
384         break;
385 #if CONFIG_FFT_FLOAT
386     case TRANSFORM_RDFT:
387         fft_size_2 = fft_size >> 1;
388         if (do_inverse) {
389             tab1[         0].im = 0;
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;
394             }
395
396             memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
397             tab2[1] = tab1[fft_size_2].re;
398
399             r->rdft_calc(r, tab2);
400             fft_ref(tab_ref, tab1, fft_nbits);
401             for (i = 0; i < fft_size; i++) {
402                 tab[i].re = tab2[i];
403                 tab[i].im = 0;
404             }
405             err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
406         } else {
407             for (i = 0; i < fft_size; i++) {
408                 tab2[i]    = tab1[i].re;
409                 tab1[i].im = 0;
410             }
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);
415         }
416         break;
417     case TRANSFORM_DCT:
418         memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
419         d->dct_calc(d, (FFTSample *)tab);
420         if (do_inverse) {
421             idct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
422         } else {
423             dct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
424         }
425         err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
426         break;
427 #endif
428     }
429
430     /* do a speed test */
431
432     if (do_speed) {
433         int64_t time_start, duration;
434         int nb_its;
435
436         av_log(NULL, AV_LOG_INFO,"Speed test...\n");
437         /* we measure during about 1 seconds */
438         nb_its = 1;
439         for(;;) {
440             time_start = av_gettime();
441             for (it = 0; it < nb_its; it++) {
442                 switch (transform) {
443                 case TRANSFORM_MDCT:
444                     if (do_inverse) {
445                         m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
446                     } else {
447                         m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
448                     }
449                     break;
450                 case TRANSFORM_FFT:
451                     memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
452                     s->fft_calc(s, tab);
453                     break;
454 #if CONFIG_FFT_FLOAT
455                 case TRANSFORM_RDFT:
456                     memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
457                     r->rdft_calc(r, tab2);
458                     break;
459                 case TRANSFORM_DCT:
460                     memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
461                     d->dct_calc(d, tab2);
462                     break;
463 #endif
464                 }
465             }
466             duration = av_gettime() - time_start;
467             if (duration >= 1000000)
468                 break;
469             nb_its *= 2;
470         }
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,
474                nb_its);
475     }
476
477     switch (transform) {
478     case TRANSFORM_MDCT:
479         ff_mdct_end(m);
480         break;
481     case TRANSFORM_FFT:
482         ff_fft_end(s);
483         break;
484 #if CONFIG_FFT_FLOAT
485     case TRANSFORM_RDFT:
486         ff_rdft_end(r);
487         break;
488 #    if CONFIG_DCT
489     case TRANSFORM_DCT:
490         ff_dct_end(d);
491         break;
492 #    endif
493 #endif
494     }
495
496     av_free(tab);
497     av_free(tab1);
498     av_free(tab2);
499     av_free(tab_ref);
500     av_free(exptab);
501
502     return err;
503 }