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