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.

508 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. #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 m, s;
  208. #if FFT_FLOAT
  209. RDFTContext r;
  210. DCTContext d;
  211. #endif /* FFT_FLOAT */
  212. int it, i, err = 1;
  213. int do_speed = 0, do_inverse = 0;
  214. int fft_nbits = 9, fft_size;
  215. double scale = 1.0;
  216. AVLFG prng;
  217. av_lfg_init(&prng, 1);
  218. for(;;) {
  219. int c = getopt(argc, argv, "hsimrdn:f:c:");
  220. if (c == -1)
  221. break;
  222. switch(c) {
  223. case 'h':
  224. help();
  225. return 1;
  226. case 's':
  227. do_speed = 1;
  228. break;
  229. case 'i':
  230. do_inverse = 1;
  231. break;
  232. case 'm':
  233. transform = TRANSFORM_MDCT;
  234. break;
  235. case 'r':
  236. transform = TRANSFORM_RDFT;
  237. break;
  238. case 'd':
  239. transform = TRANSFORM_DCT;
  240. break;
  241. case 'n':
  242. fft_nbits = atoi(optarg);
  243. break;
  244. case 'f':
  245. scale = atof(optarg);
  246. break;
  247. case 'c':
  248. {
  249. int cpuflags = av_get_cpu_flags();
  250. if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
  251. return 1;
  252. av_force_cpu_flags(cpuflags);
  253. break;
  254. }
  255. }
  256. }
  257. fft_size = 1 << fft_nbits;
  258. tab = av_malloc_array(fft_size, sizeof(FFTComplex));
  259. tab1 = av_malloc_array(fft_size, sizeof(FFTComplex));
  260. tab_ref = av_malloc_array(fft_size, sizeof(FFTComplex));
  261. tab2 = av_malloc_array(fft_size, sizeof(FFTSample));
  262. switch (transform) {
  263. #if CONFIG_MDCT
  264. case TRANSFORM_MDCT:
  265. av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
  266. if (do_inverse)
  267. av_log(NULL, AV_LOG_INFO,"IMDCT");
  268. else
  269. av_log(NULL, AV_LOG_INFO,"MDCT");
  270. ff_mdct_init(&m, fft_nbits, do_inverse, scale);
  271. break;
  272. #endif /* CONFIG_MDCT */
  273. case TRANSFORM_FFT:
  274. if (do_inverse)
  275. av_log(NULL, AV_LOG_INFO,"IFFT");
  276. else
  277. av_log(NULL, AV_LOG_INFO,"FFT");
  278. ff_fft_init(&s, fft_nbits, do_inverse);
  279. fft_ref_init(fft_nbits, do_inverse);
  280. break;
  281. #if FFT_FLOAT
  282. # if CONFIG_RDFT
  283. case TRANSFORM_RDFT:
  284. if (do_inverse)
  285. av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
  286. else
  287. av_log(NULL, AV_LOG_INFO,"DFT_R2C");
  288. ff_rdft_init(&r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
  289. fft_ref_init(fft_nbits, do_inverse);
  290. break;
  291. # endif /* CONFIG_RDFT */
  292. # if CONFIG_DCT
  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 /* CONFIG_DCT */
  301. #endif /* FFT_FLOAT */
  302. default:
  303. av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
  304. return 1;
  305. }
  306. av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
  307. /* generate random data */
  308. for (i = 0; i < fft_size; i++) {
  309. tab1[i].re = frandom(&prng);
  310. tab1[i].im = frandom(&prng);
  311. }
  312. /* checking result */
  313. av_log(NULL, AV_LOG_INFO,"Checking...\n");
  314. switch (transform) {
  315. #if CONFIG_MDCT
  316. case TRANSFORM_MDCT:
  317. if (do_inverse) {
  318. imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
  319. m.imdct_calc(&m, tab2, (FFTSample *)tab1);
  320. err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
  321. } else {
  322. mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
  323. m.mdct_calc(&m, tab2, (FFTSample *)tab1);
  324. err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
  325. }
  326. break;
  327. #endif /* CONFIG_MDCT */
  328. case TRANSFORM_FFT:
  329. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  330. s.fft_permute(&s, tab);
  331. s.fft_calc(&s, tab);
  332. fft_ref(tab_ref, tab1, fft_nbits);
  333. err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
  334. break;
  335. #if FFT_FLOAT
  336. #if CONFIG_RDFT
  337. case TRANSFORM_RDFT:
  338. {
  339. int fft_size_2 = fft_size >> 1;
  340. if (do_inverse) {
  341. tab1[ 0].im = 0;
  342. tab1[fft_size_2].im = 0;
  343. for (i = 1; i < fft_size_2; i++) {
  344. tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
  345. tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
  346. }
  347. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  348. tab2[1] = tab1[fft_size_2].re;
  349. r.rdft_calc(&r, tab2);
  350. fft_ref(tab_ref, tab1, fft_nbits);
  351. for (i = 0; i < fft_size; i++) {
  352. tab[i].re = tab2[i];
  353. tab[i].im = 0;
  354. }
  355. err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
  356. } else {
  357. for (i = 0; i < fft_size; i++) {
  358. tab2[i] = tab1[i].re;
  359. tab1[i].im = 0;
  360. }
  361. r.rdft_calc(&r, tab2);
  362. fft_ref(tab_ref, tab1, fft_nbits);
  363. tab_ref[0].im = tab_ref[fft_size_2].re;
  364. err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
  365. }
  366. break;
  367. }
  368. #endif /* CONFIG_RDFT */
  369. #if CONFIG_DCT
  370. case TRANSFORM_DCT:
  371. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  372. d.dct_calc(&d, (FFTSample *)tab);
  373. if (do_inverse) {
  374. idct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
  375. } else {
  376. dct_ref((FFTSample*)tab_ref, (FFTSample *)tab1, fft_nbits);
  377. }
  378. err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
  379. break;
  380. #endif /* CONFIG_DCT */
  381. #endif /* FFT_FLOAT */
  382. }
  383. /* do a speed test */
  384. if (do_speed) {
  385. int64_t time_start, duration;
  386. int nb_its;
  387. av_log(NULL, AV_LOG_INFO,"Speed test...\n");
  388. /* we measure during about 1 seconds */
  389. nb_its = 1;
  390. for(;;) {
  391. time_start = av_gettime_relative();
  392. for (it = 0; it < nb_its; it++) {
  393. switch (transform) {
  394. case TRANSFORM_MDCT:
  395. if (do_inverse) {
  396. m.imdct_calc(&m, (FFTSample *)tab, (FFTSample *)tab1);
  397. } else {
  398. m.mdct_calc(&m, (FFTSample *)tab, (FFTSample *)tab1);
  399. }
  400. break;
  401. case TRANSFORM_FFT:
  402. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  403. s.fft_calc(&s, tab);
  404. break;
  405. #if FFT_FLOAT
  406. case TRANSFORM_RDFT:
  407. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  408. r.rdft_calc(&r, tab2);
  409. break;
  410. case TRANSFORM_DCT:
  411. memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
  412. d.dct_calc(&d, tab2);
  413. break;
  414. #endif /* FFT_FLOAT */
  415. }
  416. }
  417. duration = av_gettime_relative() - time_start;
  418. if (duration >= 1000000)
  419. break;
  420. nb_its *= 2;
  421. }
  422. av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
  423. (double)duration / nb_its,
  424. (double)duration / 1000000.0,
  425. nb_its);
  426. }
  427. switch (transform) {
  428. #if CONFIG_MDCT
  429. case TRANSFORM_MDCT:
  430. ff_mdct_end(&m);
  431. break;
  432. #endif /* CONFIG_MDCT */
  433. case TRANSFORM_FFT:
  434. ff_fft_end(&s);
  435. break;
  436. #if FFT_FLOAT
  437. # if CONFIG_RDFT
  438. case TRANSFORM_RDFT:
  439. ff_rdft_end(&r);
  440. break;
  441. # endif /* CONFIG_RDFT */
  442. # if CONFIG_DCT
  443. case TRANSFORM_DCT:
  444. ff_dct_end(&d);
  445. break;
  446. # endif /* CONFIG_DCT */
  447. #endif /* FFT_FLOAT */
  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. }