fft-test.c
Go to the documentation of this file.
1 /*
2  * (c) 2002 Fabrice Bellard
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20 
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 #else
58 # define RANGE 16384
59 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
60 # define FMT "%6d"
61 #endif
62 
63 struct {
64  float re, im;
65 } *exptab;
66 
67 static void fft_ref_init(int nbits, int inverse)
68 {
69  int n, i;
70  double c1, s1, alpha;
71 
72  n = 1 << nbits;
73  exptab = av_malloc((n / 2) * sizeof(*exptab));
74 
75  for (i = 0; i < (n/2); i++) {
76  alpha = 2 * M_PI * (float)i / (float)n;
77  c1 = cos(alpha);
78  s1 = sin(alpha);
79  if (!inverse)
80  s1 = -s1;
81  exptab[i].re = c1;
82  exptab[i].im = s1;
83  }
84 }
85 
86 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
87 {
88  int n, i, j, k, n2;
89  double tmp_re, tmp_im, s, c;
90  FFTComplex *q;
91 
92  n = 1 << nbits;
93  n2 = n >> 1;
94  for (i = 0; i < n; i++) {
95  tmp_re = 0;
96  tmp_im = 0;
97  q = tab;
98  for (j = 0; j < n; j++) {
99  k = (i * j) & (n - 1);
100  if (k >= n2) {
101  c = -exptab[k - n2].re;
102  s = -exptab[k - n2].im;
103  } else {
104  c = exptab[k].re;
105  s = exptab[k].im;
106  }
107  CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
108  q++;
109  }
110  tabr[i].re = REF_SCALE(tmp_re, nbits);
111  tabr[i].im = REF_SCALE(tmp_im, nbits);
112  }
113 }
114 
115 #if CONFIG_MDCT
116 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
117 {
118  int n = 1<<nbits;
119  int k, i, a;
120  double sum, f;
121 
122  for (i = 0; i < n; i++) {
123  sum = 0;
124  for (k = 0; k < n/2; k++) {
125  a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
126  f = cos(M_PI * a / (double)(2 * n));
127  sum += f * in[k];
128  }
129  out[i] = REF_SCALE(-sum, nbits - 2);
130  }
131 }
132 
133 /* NOTE: no normalisation by 1 / N is done */
134 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
135 {
136  int n = 1<<nbits;
137  int k, i;
138  double a, s;
139 
140  /* do it by hand */
141  for (k = 0; k < n/2; k++) {
142  s = 0;
143  for (i = 0; i < n; i++) {
144  a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
145  s += input[i] * cos(a);
146  }
147  output[k] = REF_SCALE(s, nbits - 1);
148  }
149 }
150 #endif /* CONFIG_MDCT */
151 
152 #if CONFIG_FFT_FLOAT
153 #if CONFIG_DCT
154 static void idct_ref(float *output, float *input, int nbits)
155 {
156  int n = 1<<nbits;
157  int k, i;
158  double a, s;
159 
160  /* do it by hand */
161  for (i = 0; i < n; i++) {
162  s = 0.5 * input[0];
163  for (k = 1; k < n; k++) {
164  a = M_PI*k*(i+0.5) / n;
165  s += input[k] * cos(a);
166  }
167  output[i] = 2 * s / n;
168  }
169 }
170 static void dct_ref(float *output, float *input, int nbits)
171 {
172  int n = 1<<nbits;
173  int k, i;
174  double a, s;
175 
176  /* do it by hand */
177  for (k = 0; k < n; k++) {
178  s = 0;
179  for (i = 0; i < n; i++) {
180  a = M_PI*k*(i+0.5) / n;
181  s += input[i] * cos(a);
182  }
183  output[k] = s;
184  }
185 }
186 #endif /* CONFIG_DCT */
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 
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_parse_cpu_flags(optarg);
296  if (cpuflags < 0)
297  return 1;
298  av_set_cpu_flags_mask(cpuflags);
299  break;
300  }
301  }
302 
303  fft_size = 1 << fft_nbits;
304  tab = av_malloc(fft_size * sizeof(FFTComplex));
305  tab1 = av_malloc(fft_size * sizeof(FFTComplex));
306  tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
307  tab2 = av_malloc(fft_size * sizeof(FFTSample));
308 
309  switch (transform) {
310 #if CONFIG_MDCT
311  case TRANSFORM_MDCT:
312  av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
313  if (do_inverse)
314  av_log(NULL, AV_LOG_INFO,"IMDCT");
315  else
316  av_log(NULL, AV_LOG_INFO,"MDCT");
317  ff_mdct_init(m, fft_nbits, do_inverse, scale);
318  break;
319 #endif /* CONFIG_MDCT */
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 #if CONFIG_RDFT
330  case TRANSFORM_RDFT:
331  if (do_inverse)
332  av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
333  else
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);
337  break;
338 #endif /* CONFIG_RDFT */
339 #if CONFIG_DCT
340  case TRANSFORM_DCT:
341  if (do_inverse)
342  av_log(NULL, AV_LOG_INFO,"DCT_III");
343  else
344  av_log(NULL, AV_LOG_INFO,"DCT_II");
345  ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
346  break;
347 #endif /* CONFIG_DCT */
348 #endif
349  default:
350  av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
351  return 1;
352  }
353  av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
354 
355  /* generate random data */
356 
357  for (i = 0; i < fft_size; i++) {
358  tab1[i].re = frandom(&prng);
359  tab1[i].im = frandom(&prng);
360  }
361 
362  /* checking result */
363  av_log(NULL, AV_LOG_INFO,"Checking...\n");
364 
365  switch (transform) {
366 #if CONFIG_MDCT
367  case TRANSFORM_MDCT:
368  if (do_inverse) {
369  imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
370  m->imdct_calc(m, tab2, (FFTSample *)tab1);
371  err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
372  } else {
373  mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
374 
375  m->mdct_calc(m, tab2, (FFTSample *)tab1);
376 
377  err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
378  }
379  break;
380 #endif /* CONFIG_MDCT */
381  case TRANSFORM_FFT:
382  memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
383  s->fft_permute(s, tab);
384  s->fft_calc(s, tab);
385 
386  fft_ref(tab_ref, tab1, fft_nbits);
387  err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
388  break;
389 #if CONFIG_FFT_FLOAT
390 #if CONFIG_RDFT
391  case TRANSFORM_RDFT:
392  fft_size_2 = fft_size >> 1;
393  if (do_inverse) {
394  tab1[ 0].im = 0;
395  tab1[fft_size_2].im = 0;
396  for (i = 1; i < fft_size_2; i++) {
397  tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
398  tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
399  }
400 
401  memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
402  tab2[1] = tab1[fft_size_2].re;
403 
404  r->rdft_calc(r, tab2);
405  fft_ref(tab_ref, tab1, fft_nbits);
406  for (i = 0; i < fft_size; i++) {
407  tab[i].re = tab2[i];
408  tab[i].im = 0;
409  }
410  err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
411  } else {
412  for (i = 0; i < fft_size; i++) {
413  tab2[i] = tab1[i].re;
414  tab1[i].im = 0;
415  }
416  r->rdft_calc(r, tab2);
417  fft_ref(tab_ref, tab1, fft_nbits);
418  tab_ref[0].im = tab_ref[fft_size_2].re;
419  err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
420  }
421  break;
422 #endif /* CONFIG_RDFT */
423 #if CONFIG_DCT
424  case TRANSFORM_DCT:
425  memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
426  d->dct_calc(d, tab);
427  if (do_inverse) {
428  idct_ref(tab_ref, tab1, fft_nbits);
429  } else {
430  dct_ref(tab_ref, tab1, fft_nbits);
431  }
432  err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
433  break;
434 #endif /* CONFIG_DCT */
435 #endif
436  }
437 
438  /* do a speed test */
439 
440  if (do_speed) {
441  int64_t time_start, duration;
442  int nb_its;
443 
444  av_log(NULL, AV_LOG_INFO,"Speed test...\n");
445  /* we measure during about 1 seconds */
446  nb_its = 1;
447  for(;;) {
448  time_start = av_gettime();
449  for (it = 0; it < nb_its; it++) {
450  switch (transform) {
451  case TRANSFORM_MDCT:
452  if (do_inverse) {
453  m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
454  } else {
455  m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
456  }
457  break;
458  case TRANSFORM_FFT:
459  memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
460  s->fft_calc(s, tab);
461  break;
462 #if CONFIG_FFT_FLOAT
463  case TRANSFORM_RDFT:
464  memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
465  r->rdft_calc(r, tab2);
466  break;
467  case TRANSFORM_DCT:
468  memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
469  d->dct_calc(d, tab2);
470  break;
471 #endif
472  }
473  }
474  duration = av_gettime() - time_start;
475  if (duration >= 1000000)
476  break;
477  nb_its *= 2;
478  }
479  av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
480  (double)duration / nb_its,
481  (double)duration / 1000000.0,
482  nb_its);
483  }
484 
485  switch (transform) {
486 #if CONFIG_MDCT
487  case TRANSFORM_MDCT:
488  ff_mdct_end(m);
489  break;
490 #endif /* CONFIG_MDCT */
491  case TRANSFORM_FFT:
492  ff_fft_end(s);
493  break;
494 #if CONFIG_FFT_FLOAT
495 #if CONFIG_RDFT
496  case TRANSFORM_RDFT:
497  ff_rdft_end(r);
498  break;
499 #endif /* CONFIG_RDFT */
500 #if CONFIG_DCT
501  case TRANSFORM_DCT:
502  ff_dct_end(d);
503  break;
504 #endif /* CONFIG_DCT */
505 #endif
506  }
507 
508  av_free(tab);
509  av_free(tab1);
510  av_free(tab2);
511  av_free(tab_ref);
512  av_free(exptab);
513 
514  return err;
515 }
Definition: lfg.h:25
av_cold void ff_rdft_end(RDFTContext *s)
Definition: rdft.c:130
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
void(* dct_calc)(struct DCTContext *s, FFTSample *data)
Definition: dct.h:35
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:83
Definition: avfft.h:95
void av_set_cpu_flags_mask(int mask)
Set a mask on flags returned by av_get_cpu_flags().
Definition: cpu.c:42
void(* fft_permute)(struct FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling fft_calc().
Definition: fft.h:75
FFTSample re
Definition: avfft.h:38
static int64_t duration
Definition: avplay.c:249
tf_transform
Definition: fft-test.c:231
static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
Definition: fft-test.c:86
int av_parse_cpu_flags(const char *s)
Parse CPU flags from a string.
Definition: cpu.c:48
static void help(void)
Definition: fft-test.c:217
#define r
Definition: input.c:51
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
struct @17 * exptab
static FFTSample frandom(AVLFG *prng)
Definition: fft-test.c:190
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
Definition: avfft.h:73
#define ff_mdct_init
Definition: fft.h:146
float FFTSample
Definition: avfft.h:35
void(* rdft_calc)(struct RDFTContext *s, FFTSample *z)
Definition: rdft.h:60
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
Definition: fft.h:81
Definition: fft.h:62
static void fft_ref_init(int nbits, int inverse)
Definition: fft-test.c:67
#define CMAC(pre, pim, are, aim, bre, bim)
Definition: fft-test.c:47
#define ff_fft_init
Definition: fft.h:126
Definition: dct.h:29
Definition: avfft.h:72
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:37
#define REF_SCALE(x, bits)
Definition: fft-test.c:55
NULL
Definition: eval.c:52
float im
Definition: fft-test.c:64
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:43
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
const int16_t * tab1
Definition: mace.c:144
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
Set up DCT.
Definition: dct.c:177
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
#define s1
Definition: regdef.h:38
#define FMT
Definition: fft-test.c:56
static const uint16_t scale[4]
#define RANGE
Definition: fft-test.c:54
int main(int argc, char **argv)
Definition: fft-test.c:242
FFTSample im
Definition: avfft.h:38
#define ff_mdct_end
Definition: fft.h:147
#define ff_fft_end
Definition: fft.h:127
void(* fft_calc)(struct FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in ff_fft_init().
Definition: fft.h:80
Definition: avfft.h:94
static char * optarg
Definition: getopt.c:39
static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
Definition: fft-test.c:195
float re
Definition: fft-test.c:64
av_cold void ff_dct_end(DCTContext *s)
Definition: dct.c:218
static const struct twinvq_data tab
static uint32_t inverse(uint32_t v)
find multiplicative inverse modulo 2 ^ 32
Definition: asfcrypt.c:35
av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans)
Set up a real FFT.
Definition: rdft.c:99
const int16_t * tab2
Definition: mace.c:144
#define c1
Definition: idct_sh4.c:27