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.

487 lines
13KB

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