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.

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