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