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.

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