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.

507 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. # if CONFIG_DCT
  305. case TRANSFORM_DCT:
  306. if (do_inverse)
  307. av_log(NULL, AV_LOG_INFO,"DCT_III");
  308. else
  309. av_log(NULL, AV_LOG_INFO,"DCT_II");
  310. ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
  311. break;
  312. # endif
  313. #endif
  314. default:
  315. av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
  316. return 1;
  317. }
  318. av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
  319. /* generate random data */
  320. for (i = 0; i < fft_size; i++) {
  321. tab1[i].re = frandom(&prng);
  322. tab1[i].im = frandom(&prng);
  323. }
  324. /* checking result */
  325. av_log(NULL, AV_LOG_INFO,"Checking...\n");
  326. switch (transform) {
  327. case TRANSFORM_MDCT:
  328. if (do_inverse) {
  329. imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
  330. m->imdct_calc(m, tab2, (FFTSample *)tab1);
  331. err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
  332. } else {
  333. mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
  334. m->mdct_calc(m, tab2, (FFTSample *)tab1);
  335. err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
  336. }
  337. break;
  338. case TRANSFORM_FFT:
  339. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  340. s->fft_permute(s, tab);
  341. s->fft_calc(s, tab);
  342. fft_ref(tab_ref, tab1, fft_nbits);
  343. err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
  344. break;
  345. #if CONFIG_FFT_FLOAT
  346. case TRANSFORM_RDFT:
  347. fft_size_2 = fft_size >> 1;
  348. if (do_inverse) {
  349. tab1[ 0].im = 0;
  350. tab1[fft_size_2].im = 0;
  351. for (i = 1; i < fft_size_2; i++) {
  352. tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
  353. tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
  354. }
  355. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  356. tab2[1] = tab1[fft_size_2].re;
  357. r->rdft_calc(r, tab2);
  358. fft_ref(tab_ref, tab1, fft_nbits);
  359. for (i = 0; i < fft_size; i++) {
  360. tab[i].re = tab2[i];
  361. tab[i].im = 0;
  362. }
  363. err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
  364. } else {
  365. for (i = 0; i < fft_size; i++) {
  366. tab2[i] = tab1[i].re;
  367. tab1[i].im = 0;
  368. }
  369. r->rdft_calc(r, tab2);
  370. fft_ref(tab_ref, tab1, fft_nbits);
  371. tab_ref[0].im = tab_ref[fft_size_2].re;
  372. err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
  373. }
  374. break;
  375. case TRANSFORM_DCT:
  376. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  377. d->dct_calc(d, (FFTSample *)tab);
  378. if (do_inverse) {
  379. idct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
  380. } else {
  381. dct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
  382. }
  383. err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
  384. break;
  385. #endif
  386. }
  387. /* do a speed test */
  388. if (do_speed) {
  389. int64_t time_start, duration;
  390. int nb_its;
  391. av_log(NULL, AV_LOG_INFO,"Speed test...\n");
  392. /* we measure during about 1 seconds */
  393. nb_its = 1;
  394. for(;;) {
  395. time_start = av_gettime();
  396. for (it = 0; it < nb_its; it++) {
  397. switch (transform) {
  398. case TRANSFORM_MDCT:
  399. if (do_inverse) {
  400. m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
  401. } else {
  402. m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
  403. }
  404. break;
  405. case TRANSFORM_FFT:
  406. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  407. s->fft_calc(s, tab);
  408. break;
  409. #if CONFIG_FFT_FLOAT
  410. case TRANSFORM_RDFT:
  411. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  412. r->rdft_calc(r, tab2);
  413. break;
  414. case TRANSFORM_DCT:
  415. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  416. d->dct_calc(d, tab2);
  417. break;
  418. #endif
  419. }
  420. }
  421. duration = av_gettime() - time_start;
  422. if (duration >= 1000000)
  423. break;
  424. nb_its *= 2;
  425. }
  426. av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
  427. (double)duration / nb_its,
  428. (double)duration / 1000000.0,
  429. nb_its);
  430. }
  431. switch (transform) {
  432. case TRANSFORM_MDCT:
  433. ff_mdct_end(m);
  434. break;
  435. case TRANSFORM_FFT:
  436. ff_fft_end(s);
  437. break;
  438. #if CONFIG_FFT_FLOAT
  439. case TRANSFORM_RDFT:
  440. ff_rdft_end(r);
  441. break;
  442. # if CONFIG_DCT
  443. case TRANSFORM_DCT:
  444. ff_dct_end(d);
  445. break;
  446. # endif
  447. #endif
  448. }
  449. av_free(tab);
  450. av_free(tab1);
  451. av_free(tab2);
  452. av_free(tab_ref);
  453. av_free(exptab);
  454. if (err)
  455. printf("Error: %d.\n", err);
  456. return !!err;
  457. }