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.

677 lines
16KB

  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. * @file
  22. * FFT and MDCT tests.
  23. */
  24. #include "config.h"
  25. #ifndef AVFFT
  26. #define AVFFT 0
  27. #endif
  28. #include <math.h>
  29. #if HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "libavutil/cpu.h"
  36. #include "libavutil/lfg.h"
  37. #include "libavutil/log.h"
  38. #include "libavutil/mathematics.h"
  39. #include "libavutil/time.h"
  40. #if AVFFT
  41. #include "libavcodec/avfft.h"
  42. #else
  43. #include "libavcodec/fft.h"
  44. #endif
  45. #if FFT_FLOAT
  46. #include "libavcodec/dct.h"
  47. #include "libavcodec/rdft.h"
  48. #endif
  49. /* reference fft */
  50. #define MUL16(a, b) ((a) * (b))
  51. #define CMAC(pre, pim, are, aim, bre, bim) \
  52. { \
  53. pre += (MUL16(are, bre) - MUL16(aim, bim)); \
  54. pim += (MUL16(are, bim) + MUL16(bre, aim)); \
  55. }
  56. #if FFT_FLOAT || AVFFT
  57. #define RANGE 1.0
  58. #define REF_SCALE(x, bits) (x)
  59. #define FMT "%10.6f"
  60. #elif FFT_FIXED_32
  61. #define RANGE 8388608
  62. #define REF_SCALE(x, bits) (x)
  63. #define FMT "%6d"
  64. #else
  65. #define RANGE 16384
  66. #define REF_SCALE(x, bits) ((x) / (1 << (bits)))
  67. #define FMT "%6d"
  68. #endif
  69. static struct {
  70. float re, im;
  71. } *exptab;
  72. static int fft_ref_init(int nbits, int inverse)
  73. {
  74. int i, n = 1 << nbits;
  75. exptab = av_malloc_array((n / 2), sizeof(*exptab));
  76. if (!exptab)
  77. return AVERROR(ENOMEM);
  78. for (i = 0; i < (n / 2); i++) {
  79. double alpha = 2 * M_PI * (float) i / (float) n;
  80. double c1 = cos(alpha), s1 = sin(alpha);
  81. if (!inverse)
  82. s1 = -s1;
  83. exptab[i].re = c1;
  84. exptab[i].im = s1;
  85. }
  86. return 0;
  87. }
  88. static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
  89. {
  90. int i, j;
  91. int n = 1 << nbits;
  92. int n2 = n >> 1;
  93. for (i = 0; i < n; i++) {
  94. double tmp_re = 0, tmp_im = 0;
  95. FFTComplex *q = tab;
  96. for (j = 0; j < n; j++) {
  97. double s, c;
  98. int k = (i * j) & (n - 1);
  99. if (k >= n2) {
  100. c = -exptab[k - n2].re;
  101. s = -exptab[k - n2].im;
  102. } else {
  103. c = exptab[k].re;
  104. s = exptab[k].im;
  105. }
  106. CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
  107. q++;
  108. }
  109. tabr[i].re = REF_SCALE(tmp_re, nbits);
  110. tabr[i].im = REF_SCALE(tmp_im, nbits);
  111. }
  112. }
  113. #if CONFIG_MDCT
  114. static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
  115. {
  116. int i, k, n = 1 << nbits;
  117. for (i = 0; i < n; i++) {
  118. double sum = 0;
  119. for (k = 0; k < n / 2; k++) {
  120. int a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
  121. double f = cos(M_PI * a / (double) (2 * n));
  122. sum += f * in[k];
  123. }
  124. out[i] = REF_SCALE(-sum, nbits - 2);
  125. }
  126. }
  127. /* NOTE: no normalisation by 1 / N is done */
  128. static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
  129. {
  130. int i, k, n = 1 << nbits;
  131. /* do it by hand */
  132. for (k = 0; k < n / 2; k++) {
  133. double s = 0;
  134. for (i = 0; i < n; i++) {
  135. double a = (2 * M_PI * (2 * i + 1 + n / 2) * (2 * k + 1) / (4 * n));
  136. s += input[i] * cos(a);
  137. }
  138. output[k] = REF_SCALE(s, nbits - 1);
  139. }
  140. }
  141. #endif /* CONFIG_MDCT */
  142. #if FFT_FLOAT
  143. #if CONFIG_DCT
  144. static void idct_ref(FFTSample *output, FFTSample *input, int nbits)
  145. {
  146. int i, k, n = 1 << nbits;
  147. /* do it by hand */
  148. for (i = 0; i < n; i++) {
  149. double s = 0.5 * input[0];
  150. for (k = 1; k < n; k++) {
  151. double a = M_PI * k * (i + 0.5) / n;
  152. s += input[k] * cos(a);
  153. }
  154. output[i] = 2 * s / n;
  155. }
  156. }
  157. static void dct_ref(FFTSample *output, FFTSample *input, int nbits)
  158. {
  159. int i, k, n = 1 << nbits;
  160. /* do it by hand */
  161. for (k = 0; k < n; k++) {
  162. double s = 0;
  163. for (i = 0; i < n; i++) {
  164. double a = M_PI * k * (i + 0.5) / n;
  165. s += input[i] * cos(a);
  166. }
  167. output[k] = s;
  168. }
  169. }
  170. #endif /* CONFIG_DCT */
  171. #endif /* FFT_FLOAT */
  172. static FFTSample frandom(AVLFG *prng)
  173. {
  174. return (int16_t) av_lfg_get(prng) / 32768.0 * RANGE;
  175. }
  176. static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
  177. {
  178. int i, err = 0;
  179. double error = 0, max = 0;
  180. for (i = 0; i < n; i++) {
  181. double e = fabs(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)
  189. max = e;
  190. }
  191. av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error / n));
  192. return err;
  193. }
  194. static inline void fft_init(FFTContext **s, int nbits, int inverse)
  195. {
  196. #if AVFFT
  197. *s = av_fft_init(nbits, inverse);
  198. #else
  199. ff_fft_init(*s, nbits, inverse);
  200. #endif
  201. }
  202. static inline void mdct_init(FFTContext **s, int nbits, int inverse, double scale)
  203. {
  204. #if AVFFT
  205. *s = av_mdct_init(nbits, inverse, scale);
  206. #else
  207. ff_mdct_init(*s, nbits, inverse, scale);
  208. #endif
  209. }
  210. static inline void mdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input)
  211. {
  212. #if AVFFT
  213. av_mdct_calc(s, output, input);
  214. #else
  215. s->mdct_calc(s, output, input);
  216. #endif
  217. }
  218. static inline void imdct_calc(struct FFTContext *s, FFTSample *output, const FFTSample *input)
  219. {
  220. #if AVFFT
  221. av_imdct_calc(s, output, input);
  222. #else
  223. s->imdct_calc(s, output, input);
  224. #endif
  225. }
  226. static inline void fft_permute(FFTContext *s, FFTComplex *z)
  227. {
  228. #if AVFFT
  229. av_fft_permute(s, z);
  230. #else
  231. s->fft_permute(s, z);
  232. #endif
  233. }
  234. static inline void fft_calc(FFTContext *s, FFTComplex *z)
  235. {
  236. #if AVFFT
  237. av_fft_calc(s, z);
  238. #else
  239. s->fft_calc(s, z);
  240. #endif
  241. }
  242. static inline void mdct_end(FFTContext *s)
  243. {
  244. #if AVFFT
  245. av_mdct_end(s);
  246. #else
  247. ff_mdct_end(s);
  248. #endif
  249. }
  250. static inline void fft_end(FFTContext *s)
  251. {
  252. #if AVFFT
  253. av_fft_end(s);
  254. #else
  255. ff_fft_end(s);
  256. #endif
  257. }
  258. #if FFT_FLOAT
  259. static inline void rdft_init(RDFTContext **r, int nbits, enum RDFTransformType trans)
  260. {
  261. #if AVFFT
  262. *r = av_rdft_init(nbits, trans);
  263. #else
  264. ff_rdft_init(*r, nbits, trans);
  265. #endif
  266. }
  267. static inline void dct_init(DCTContext **d, int nbits, enum DCTTransformType trans)
  268. {
  269. #if AVFFT
  270. *d = av_dct_init(nbits, trans);
  271. #else
  272. ff_dct_init(*d, nbits, trans);
  273. #endif
  274. }
  275. static inline void rdft_calc(RDFTContext *r, FFTSample *tab)
  276. {
  277. #if AVFFT
  278. av_rdft_calc(r, tab);
  279. #else
  280. r->rdft_calc(r, tab);
  281. #endif
  282. }
  283. static inline void dct_calc(DCTContext *d, FFTSample *data)
  284. {
  285. #if AVFFT
  286. av_dct_calc(d, data);
  287. #else
  288. d->dct_calc(d, data);
  289. #endif
  290. }
  291. static inline void rdft_end(RDFTContext *r)
  292. {
  293. #if AVFFT
  294. av_rdft_end(r);
  295. #else
  296. ff_rdft_end(r);
  297. #endif
  298. }
  299. static inline void dct_end(DCTContext *d)
  300. {
  301. #if AVFFT
  302. av_dct_end(d);
  303. #else
  304. ff_dct_end(d);
  305. #endif
  306. }
  307. #endif /* FFT_FLOAT */
  308. static void help(void)
  309. {
  310. av_log(NULL, AV_LOG_INFO,
  311. "usage: fft-test [-h] [-s] [-i] [-n b]\n"
  312. "-h print this help\n"
  313. "-s speed test\n"
  314. "-m (I)MDCT test\n"
  315. "-d (I)DCT test\n"
  316. "-r (I)RDFT test\n"
  317. "-i inverse transform test\n"
  318. "-n b set the transform size to 2^b\n"
  319. "-f x set scale factor for output data of (I)MDCT to x\n");
  320. }
  321. enum tf_transform {
  322. TRANSFORM_FFT,
  323. TRANSFORM_MDCT,
  324. TRANSFORM_RDFT,
  325. TRANSFORM_DCT,
  326. };
  327. #if !HAVE_GETOPT
  328. #include "compat/getopt.c"
  329. #endif
  330. int main(int argc, char **argv)
  331. {
  332. FFTComplex *tab, *tab1, *tab_ref;
  333. FFTSample *tab2;
  334. enum tf_transform transform = TRANSFORM_FFT;
  335. FFTContext *m, *s;
  336. #if FFT_FLOAT
  337. RDFTContext *r;
  338. DCTContext *d;
  339. #endif /* FFT_FLOAT */
  340. int it, i, err = 1;
  341. int do_speed = 0, do_inverse = 0;
  342. int fft_nbits = 9, fft_size;
  343. double scale = 1.0;
  344. AVLFG prng;
  345. #if !AVFFT
  346. s = av_mallocz(sizeof(*s));
  347. m = av_mallocz(sizeof(*m));
  348. #endif
  349. #if !AVFFT && FFT_FLOAT
  350. r = av_mallocz(sizeof(*r));
  351. d = av_mallocz(sizeof(*d));
  352. #endif
  353. av_lfg_init(&prng, 1);
  354. for (;;) {
  355. int c = getopt(argc, argv, "hsimrdn:f:c:");
  356. if (c == -1)
  357. break;
  358. switch (c) {
  359. case 'h':
  360. help();
  361. return 1;
  362. case 's':
  363. do_speed = 1;
  364. break;
  365. case 'i':
  366. do_inverse = 1;
  367. break;
  368. case 'm':
  369. transform = TRANSFORM_MDCT;
  370. break;
  371. case 'r':
  372. transform = TRANSFORM_RDFT;
  373. break;
  374. case 'd':
  375. transform = TRANSFORM_DCT;
  376. break;
  377. case 'n':
  378. fft_nbits = atoi(optarg);
  379. break;
  380. case 'f':
  381. scale = atof(optarg);
  382. break;
  383. case 'c':
  384. {
  385. unsigned cpuflags = av_get_cpu_flags();
  386. if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
  387. return 1;
  388. av_force_cpu_flags(cpuflags);
  389. break;
  390. }
  391. }
  392. }
  393. fft_size = 1 << fft_nbits;
  394. tab = av_malloc_array(fft_size, sizeof(FFTComplex));
  395. tab1 = av_malloc_array(fft_size, sizeof(FFTComplex));
  396. tab_ref = av_malloc_array(fft_size, sizeof(FFTComplex));
  397. tab2 = av_malloc_array(fft_size, sizeof(FFTSample));
  398. if (!(tab && tab1 && tab_ref && tab2))
  399. goto cleanup;
  400. switch (transform) {
  401. #if CONFIG_MDCT
  402. case TRANSFORM_MDCT:
  403. av_log(NULL, AV_LOG_INFO, "Scale factor is set to %f\n", scale);
  404. if (do_inverse)
  405. av_log(NULL, AV_LOG_INFO, "IMDCT");
  406. else
  407. av_log(NULL, AV_LOG_INFO, "MDCT");
  408. mdct_init(&m, fft_nbits, do_inverse, scale);
  409. break;
  410. #endif /* CONFIG_MDCT */
  411. case TRANSFORM_FFT:
  412. if (do_inverse)
  413. av_log(NULL, AV_LOG_INFO, "IFFT");
  414. else
  415. av_log(NULL, AV_LOG_INFO, "FFT");
  416. fft_init(&s, fft_nbits, do_inverse);
  417. if ((err = fft_ref_init(fft_nbits, do_inverse)) < 0)
  418. goto cleanup;
  419. break;
  420. #if FFT_FLOAT
  421. # if CONFIG_RDFT
  422. case TRANSFORM_RDFT:
  423. if (do_inverse)
  424. av_log(NULL, AV_LOG_INFO, "IDFT_C2R");
  425. else
  426. av_log(NULL, AV_LOG_INFO, "DFT_R2C");
  427. rdft_init(&r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
  428. if ((err = fft_ref_init(fft_nbits, do_inverse)) < 0)
  429. goto cleanup;
  430. break;
  431. # endif /* CONFIG_RDFT */
  432. # if CONFIG_DCT
  433. case TRANSFORM_DCT:
  434. if (do_inverse)
  435. av_log(NULL, AV_LOG_INFO, "DCT_III");
  436. else
  437. av_log(NULL, AV_LOG_INFO, "DCT_II");
  438. dct_init(&d, fft_nbits, do_inverse ? DCT_III : DCT_II);
  439. break;
  440. # endif /* CONFIG_DCT */
  441. #endif /* FFT_FLOAT */
  442. default:
  443. av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
  444. goto cleanup;
  445. }
  446. av_log(NULL, AV_LOG_INFO, " %d test\n", fft_size);
  447. /* generate random data */
  448. for (i = 0; i < fft_size; i++) {
  449. tab1[i].re = frandom(&prng);
  450. tab1[i].im = frandom(&prng);
  451. }
  452. /* checking result */
  453. av_log(NULL, AV_LOG_INFO, "Checking...\n");
  454. switch (transform) {
  455. #if CONFIG_MDCT
  456. case TRANSFORM_MDCT:
  457. if (do_inverse) {
  458. imdct_ref(&tab_ref->re, &tab1->re, fft_nbits);
  459. imdct_calc(m, tab2, &tab1->re);
  460. err = check_diff(&tab_ref->re, tab2, fft_size, scale);
  461. } else {
  462. mdct_ref(&tab_ref->re, &tab1->re, fft_nbits);
  463. mdct_calc(m, tab2, &tab1->re);
  464. err = check_diff(&tab_ref->re, tab2, fft_size / 2, scale);
  465. }
  466. break;
  467. #endif /* CONFIG_MDCT */
  468. case TRANSFORM_FFT:
  469. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  470. fft_permute(s, tab);
  471. fft_calc(s, tab);
  472. fft_ref(tab_ref, tab1, fft_nbits);
  473. err = check_diff(&tab_ref->re, &tab->re, fft_size * 2, 1.0);
  474. break;
  475. #if FFT_FLOAT
  476. #if CONFIG_RDFT
  477. case TRANSFORM_RDFT:
  478. {
  479. int fft_size_2 = fft_size >> 1;
  480. if (do_inverse) {
  481. tab1[0].im = 0;
  482. tab1[fft_size_2].im = 0;
  483. for (i = 1; i < fft_size_2; i++) {
  484. tab1[fft_size_2 + i].re = tab1[fft_size_2 - i].re;
  485. tab1[fft_size_2 + i].im = -tab1[fft_size_2 - i].im;
  486. }
  487. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  488. tab2[1] = tab1[fft_size_2].re;
  489. rdft_calc(r, tab2);
  490. fft_ref(tab_ref, tab1, fft_nbits);
  491. for (i = 0; i < fft_size; i++) {
  492. tab[i].re = tab2[i];
  493. tab[i].im = 0;
  494. }
  495. err = check_diff(&tab_ref->re, &tab->re, fft_size * 2, 0.5);
  496. } else {
  497. for (i = 0; i < fft_size; i++) {
  498. tab2[i] = tab1[i].re;
  499. tab1[i].im = 0;
  500. }
  501. rdft_calc(r, tab2);
  502. fft_ref(tab_ref, tab1, fft_nbits);
  503. tab_ref[0].im = tab_ref[fft_size_2].re;
  504. err = check_diff(&tab_ref->re, tab2, fft_size, 1.0);
  505. }
  506. break;
  507. }
  508. #endif /* CONFIG_RDFT */
  509. #if CONFIG_DCT
  510. case TRANSFORM_DCT:
  511. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  512. dct_calc(d, &tab->re);
  513. if (do_inverse)
  514. idct_ref(&tab_ref->re, &tab1->re, fft_nbits);
  515. else
  516. dct_ref(&tab_ref->re, &tab1->re, fft_nbits);
  517. err = check_diff(&tab_ref->re, &tab->re, fft_size, 1.0);
  518. break;
  519. #endif /* CONFIG_DCT */
  520. #endif /* FFT_FLOAT */
  521. }
  522. /* do a speed test */
  523. if (do_speed) {
  524. int64_t time_start, duration;
  525. int nb_its;
  526. av_log(NULL, AV_LOG_INFO, "Speed test...\n");
  527. /* we measure during about 1 seconds */
  528. nb_its = 1;
  529. for (;;) {
  530. time_start = av_gettime_relative();
  531. for (it = 0; it < nb_its; it++) {
  532. switch (transform) {
  533. case TRANSFORM_MDCT:
  534. if (do_inverse)
  535. imdct_calc(m, &tab->re, &tab1->re);
  536. else
  537. mdct_calc(m, &tab->re, &tab1->re);
  538. break;
  539. case TRANSFORM_FFT:
  540. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  541. fft_calc(s, tab);
  542. break;
  543. #if FFT_FLOAT
  544. case TRANSFORM_RDFT:
  545. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  546. rdft_calc(r, tab2);
  547. break;
  548. case TRANSFORM_DCT:
  549. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  550. dct_calc(d, tab2);
  551. break;
  552. #endif /* FFT_FLOAT */
  553. }
  554. }
  555. duration = av_gettime_relative() - time_start;
  556. if (duration >= 1000000)
  557. break;
  558. nb_its *= 2;
  559. }
  560. av_log(NULL, AV_LOG_INFO,
  561. "time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
  562. (double) duration / nb_its,
  563. (double) duration / 1000000.0,
  564. nb_its);
  565. }
  566. switch (transform) {
  567. #if CONFIG_MDCT
  568. case TRANSFORM_MDCT:
  569. mdct_end(m);
  570. break;
  571. #endif /* CONFIG_MDCT */
  572. case TRANSFORM_FFT:
  573. fft_end(s);
  574. break;
  575. #if FFT_FLOAT
  576. # if CONFIG_RDFT
  577. case TRANSFORM_RDFT:
  578. rdft_end(r);
  579. break;
  580. # endif /* CONFIG_RDFT */
  581. # if CONFIG_DCT
  582. case TRANSFORM_DCT:
  583. dct_end(d);
  584. break;
  585. # endif /* CONFIG_DCT */
  586. #endif /* FFT_FLOAT */
  587. }
  588. cleanup:
  589. av_free(tab);
  590. av_free(tab1);
  591. av_free(tab2);
  592. av_free(tab_ref);
  593. av_free(exptab);
  594. #if !AVFFT
  595. av_free(s);
  596. av_free(m);
  597. #endif
  598. #if !AVFFT && FFT_FLOAT
  599. av_free(r);
  600. av_free(d);
  601. #endif
  602. if (err)
  603. printf("Error: %d.\n", err);
  604. return !!err;
  605. }