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.

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