You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

520 lines
13KB

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