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.

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