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.

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