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.

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