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.

330 lines
8.3KB

  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 libavcodec/fft-test.c
  22. * FFT and MDCT tests.
  23. */
  24. #include "libavutil/lfg.h"
  25. #include "dsputil.h"
  26. #include <math.h>
  27. #include <unistd.h>
  28. #include <sys/time.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #undef exit
  32. /* reference fft */
  33. #define MUL16(a,b) ((a) * (b))
  34. #define CMAC(pre, pim, are, aim, bre, bim) \
  35. {\
  36. pre += (MUL16(are, bre) - MUL16(aim, bim));\
  37. pim += (MUL16(are, bim) + MUL16(bre, aim));\
  38. }
  39. FFTComplex *exptab;
  40. static void fft_ref_init(int nbits, int inverse)
  41. {
  42. int n, i;
  43. double c1, s1, alpha;
  44. n = 1 << nbits;
  45. exptab = av_malloc((n / 2) * sizeof(FFTComplex));
  46. for (i = 0; i < (n/2); i++) {
  47. alpha = 2 * M_PI * (float)i / (float)n;
  48. c1 = cos(alpha);
  49. s1 = sin(alpha);
  50. if (!inverse)
  51. s1 = -s1;
  52. exptab[i].re = c1;
  53. exptab[i].im = s1;
  54. }
  55. }
  56. static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
  57. {
  58. int n, i, j, k, n2;
  59. double tmp_re, tmp_im, s, c;
  60. FFTComplex *q;
  61. n = 1 << nbits;
  62. n2 = n >> 1;
  63. for (i = 0; i < n; i++) {
  64. tmp_re = 0;
  65. tmp_im = 0;
  66. q = tab;
  67. for (j = 0; j < n; j++) {
  68. k = (i * j) & (n - 1);
  69. if (k >= n2) {
  70. c = -exptab[k - n2].re;
  71. s = -exptab[k - n2].im;
  72. } else {
  73. c = exptab[k].re;
  74. s = exptab[k].im;
  75. }
  76. CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
  77. q++;
  78. }
  79. tabr[i].re = tmp_re;
  80. tabr[i].im = tmp_im;
  81. }
  82. }
  83. static void imdct_ref(float *out, float *in, int nbits)
  84. {
  85. int n = 1<<nbits;
  86. int k, i, a;
  87. double sum, f;
  88. for (i = 0; i < n; i++) {
  89. sum = 0;
  90. for (k = 0; k < n/2; k++) {
  91. a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
  92. f = cos(M_PI * a / (double)(2 * n));
  93. sum += f * in[k];
  94. }
  95. out[i] = -sum;
  96. }
  97. }
  98. /* NOTE: no normalisation by 1 / N is done */
  99. static void mdct_ref(float *output, float *input, int nbits)
  100. {
  101. int n = 1<<nbits;
  102. int k, i;
  103. double a, s;
  104. /* do it by hand */
  105. for (k = 0; k < n/2; k++) {
  106. s = 0;
  107. for (i = 0; i < n; i++) {
  108. a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
  109. s += input[i] * cos(a);
  110. }
  111. output[k] = s;
  112. }
  113. }
  114. static float frandom(AVLFG *prng)
  115. {
  116. return (int16_t)av_lfg_get(prng) / 32768.0;
  117. }
  118. static int64_t gettime(void)
  119. {
  120. struct timeval tv;
  121. gettimeofday(&tv,NULL);
  122. return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
  123. }
  124. static void check_diff(float *tab1, float *tab2, int n, double scale)
  125. {
  126. int i;
  127. double max= 0;
  128. double error= 0;
  129. for (i = 0; i < n; i++) {
  130. double e= fabsf(tab1[i] - (tab2[i] / scale));
  131. if (e >= 1e-3) {
  132. av_log(NULL, AV_LOG_ERROR, "ERROR %d: %f %f\n",
  133. i, tab1[i], tab2[i]);
  134. }
  135. error+= e*e;
  136. if(e>max) max= e;
  137. }
  138. av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
  139. }
  140. static void help(void)
  141. {
  142. av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
  143. "-h print this help\n"
  144. "-s speed test\n"
  145. "-m (I)MDCT test\n"
  146. "-i inverse transform test\n"
  147. "-n b set the transform size to 2^b\n"
  148. "-f x set scale factor for output data of (I)MDCT to x\n"
  149. );
  150. exit(1);
  151. }
  152. enum tf_transform {
  153. TRANSFORM_FFT,
  154. TRANSFORM_MDCT,
  155. };
  156. int main(int argc, char **argv)
  157. {
  158. FFTComplex *tab, *tab1, *tab_ref;
  159. FFTSample *tab2;
  160. int it, i, c;
  161. int do_speed = 0;
  162. enum tf_transform transform = TRANSFORM_FFT;
  163. int do_inverse = 0;
  164. FFTContext s1, *s = &s1;
  165. FFTContext m1, *m = &m1;
  166. int fft_nbits, fft_size;
  167. double scale = 1.0;
  168. AVLFG prng;
  169. av_lfg_init(&prng, 1);
  170. fft_nbits = 9;
  171. for(;;) {
  172. c = getopt(argc, argv, "hsimn:f:");
  173. if (c == -1)
  174. break;
  175. switch(c) {
  176. case 'h':
  177. help();
  178. break;
  179. case 's':
  180. do_speed = 1;
  181. break;
  182. case 'i':
  183. do_inverse = 1;
  184. break;
  185. case 'm':
  186. transform = TRANSFORM_MDCT;
  187. break;
  188. case 'n':
  189. fft_nbits = atoi(optarg);
  190. break;
  191. case 'f':
  192. scale = atof(optarg);
  193. break;
  194. }
  195. }
  196. fft_size = 1 << fft_nbits;
  197. tab = av_malloc(fft_size * sizeof(FFTComplex));
  198. tab1 = av_malloc(fft_size * sizeof(FFTComplex));
  199. tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
  200. tab2 = av_malloc(fft_size * sizeof(FFTSample));
  201. switch (transform) {
  202. case TRANSFORM_MDCT:
  203. av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
  204. if (do_inverse)
  205. av_log(NULL, AV_LOG_INFO,"IMDCT");
  206. else
  207. av_log(NULL, AV_LOG_INFO,"MDCT");
  208. ff_mdct_init(m, fft_nbits, do_inverse, scale);
  209. break;
  210. case TRANSFORM_FFT:
  211. if (do_inverse)
  212. av_log(NULL, AV_LOG_INFO,"IFFT");
  213. else
  214. av_log(NULL, AV_LOG_INFO,"FFT");
  215. ff_fft_init(s, fft_nbits, do_inverse);
  216. fft_ref_init(fft_nbits, do_inverse);
  217. break;
  218. }
  219. av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
  220. /* generate random data */
  221. for (i = 0; i < fft_size; i++) {
  222. tab1[i].re = frandom(&prng);
  223. tab1[i].im = frandom(&prng);
  224. }
  225. /* checking result */
  226. av_log(NULL, AV_LOG_INFO,"Checking...\n");
  227. switch (transform) {
  228. case TRANSFORM_MDCT:
  229. if (do_inverse) {
  230. imdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
  231. ff_imdct_calc(m, tab2, (float *)tab1);
  232. check_diff((float *)tab_ref, tab2, fft_size, scale);
  233. } else {
  234. mdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
  235. ff_mdct_calc(m, tab2, (float *)tab1);
  236. check_diff((float *)tab_ref, tab2, fft_size / 2, scale);
  237. }
  238. break;
  239. case TRANSFORM_FFT:
  240. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  241. ff_fft_permute(s, tab);
  242. ff_fft_calc(s, tab);
  243. fft_ref(tab_ref, tab1, fft_nbits);
  244. check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 1.0);
  245. break;
  246. }
  247. /* do a speed test */
  248. if (do_speed) {
  249. int64_t time_start, duration;
  250. int nb_its;
  251. av_log(NULL, AV_LOG_INFO,"Speed test...\n");
  252. /* we measure during about 1 seconds */
  253. nb_its = 1;
  254. for(;;) {
  255. time_start = gettime();
  256. for (it = 0; it < nb_its; it++) {
  257. switch (transform) {
  258. case TRANSFORM_MDCT:
  259. if (do_inverse) {
  260. ff_imdct_calc(m, (float *)tab, (float *)tab1);
  261. } else {
  262. ff_mdct_calc(m, (float *)tab, (float *)tab1);
  263. }
  264. break;
  265. case TRANSFORM_FFT:
  266. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  267. ff_fft_calc(s, tab);
  268. break;
  269. }
  270. }
  271. duration = gettime() - time_start;
  272. if (duration >= 1000000)
  273. break;
  274. nb_its *= 2;
  275. }
  276. av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
  277. (double)duration / nb_its,
  278. (double)duration / 1000000.0,
  279. nb_its);
  280. }
  281. switch (transform) {
  282. case TRANSFORM_MDCT:
  283. ff_mdct_end(m);
  284. break;
  285. case TRANSFORM_FFT:
  286. ff_fft_end(s);
  287. break;
  288. }
  289. return 0;
  290. }