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
14KB

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