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