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.

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