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.

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