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.

524 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 = fabsf(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. int 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((FFTSample *) tab_ref, (FFTSample *) tab1, fft_nbits);
  330. m.imdct_calc(&m, tab2, (FFTSample *) tab1);
  331. err = check_diff((FFTSample *) tab_ref, tab2, fft_size, scale);
  332. } else {
  333. mdct_ref((FFTSample *) tab_ref, (FFTSample *) tab1, fft_nbits);
  334. m.mdct_calc(&m, tab2, (FFTSample *) tab1);
  335. err = check_diff((FFTSample *) tab_ref, 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((FFTSample *) tab_ref, (FFTSample *) tab,
  345. fft_size * 2, 1.0);
  346. break;
  347. #if FFT_FLOAT
  348. #if CONFIG_RDFT
  349. case TRANSFORM_RDFT:
  350. {
  351. int fft_size_2 = fft_size >> 1;
  352. if (do_inverse) {
  353. tab1[0].im = 0;
  354. tab1[fft_size_2].im = 0;
  355. for (i = 1; i < fft_size_2; i++) {
  356. tab1[fft_size_2 + i].re = tab1[fft_size_2 - i].re;
  357. tab1[fft_size_2 + i].im = -tab1[fft_size_2 - i].im;
  358. }
  359. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  360. tab2[1] = tab1[fft_size_2].re;
  361. r.rdft_calc(&r, tab2);
  362. fft_ref(tab_ref, tab1, fft_nbits);
  363. for (i = 0; i < fft_size; i++) {
  364. tab[i].re = tab2[i];
  365. tab[i].im = 0;
  366. }
  367. err = check_diff((float *) tab_ref, (float *) tab,
  368. fft_size * 2, 0.5);
  369. } else {
  370. for (i = 0; i < fft_size; i++) {
  371. tab2[i] = tab1[i].re;
  372. tab1[i].im = 0;
  373. }
  374. r.rdft_calc(&r, tab2);
  375. fft_ref(tab_ref, tab1, fft_nbits);
  376. tab_ref[0].im = tab_ref[fft_size_2].re;
  377. err = check_diff((float *) tab_ref, (float *) tab2, fft_size, 1.0);
  378. }
  379. break;
  380. }
  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->re);
  386. if (do_inverse)
  387. idct_ref(&tab_ref->re, &tab1->re, fft_nbits);
  388. else
  389. dct_ref(&tab_ref->re, &tab1->re, fft_nbits);
  390. err = check_diff((float *) tab_ref, (float *) tab, fft_size, 1.0);
  391. break;
  392. #endif /* CONFIG_DCT */
  393. #endif /* FFT_FLOAT */
  394. }
  395. /* do a speed test */
  396. if (do_speed) {
  397. int64_t time_start, duration;
  398. int nb_its;
  399. av_log(NULL, AV_LOG_INFO, "Speed test...\n");
  400. /* we measure during about 1 seconds */
  401. nb_its = 1;
  402. for (;;) {
  403. time_start = av_gettime_relative();
  404. for (it = 0; it < nb_its; it++) {
  405. switch (transform) {
  406. case TRANSFORM_MDCT:
  407. if (do_inverse)
  408. m.imdct_calc(&m, (FFTSample *) tab, (FFTSample *) tab1);
  409. else
  410. m.mdct_calc(&m, (FFTSample *) tab, (FFTSample *) tab1);
  411. break;
  412. case TRANSFORM_FFT:
  413. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  414. s.fft_calc(&s, tab);
  415. break;
  416. #if FFT_FLOAT
  417. case TRANSFORM_RDFT:
  418. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  419. r.rdft_calc(&r, tab2);
  420. break;
  421. case TRANSFORM_DCT:
  422. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  423. d.dct_calc(&d, tab2);
  424. break;
  425. #endif /* FFT_FLOAT */
  426. }
  427. }
  428. duration = av_gettime_relative() - time_start;
  429. if (duration >= 1000000)
  430. break;
  431. nb_its *= 2;
  432. }
  433. av_log(NULL, AV_LOG_INFO,
  434. "time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
  435. (double) duration / nb_its,
  436. (double) duration / 1000000.0,
  437. nb_its);
  438. }
  439. switch (transform) {
  440. #if CONFIG_MDCT
  441. case TRANSFORM_MDCT:
  442. ff_mdct_end(&m);
  443. break;
  444. #endif /* CONFIG_MDCT */
  445. case TRANSFORM_FFT:
  446. ff_fft_end(&s);
  447. break;
  448. #if FFT_FLOAT
  449. # if CONFIG_RDFT
  450. case TRANSFORM_RDFT:
  451. ff_rdft_end(&r);
  452. break;
  453. # endif /* CONFIG_RDFT */
  454. # if CONFIG_DCT
  455. case TRANSFORM_DCT:
  456. ff_dct_end(&d);
  457. break;
  458. # endif /* CONFIG_DCT */
  459. #endif /* FFT_FLOAT */
  460. }
  461. cleanup:
  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. }