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.

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