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.

315 lines
7.8KB

  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(void)
  115. {
  116. AVLFG prng;
  117. av_lfg_init(&prng, 1);
  118. return (float)((av_lfg_get(&prng) & 0xffff) - 32768) / 32768.0;
  119. }
  120. static int64_t gettime(void)
  121. {
  122. struct timeval tv;
  123. gettimeofday(&tv,NULL);
  124. return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
  125. }
  126. static void check_diff(float *tab1, float *tab2, int n, double scale)
  127. {
  128. int i;
  129. double max= 0;
  130. double error= 0;
  131. for(i=0;i<n;i++) {
  132. double e= fabsf(tab1[i] - (tab2[i] / scale));
  133. if (e >= 1e-3) {
  134. av_log(NULL, AV_LOG_ERROR, "ERROR %d: %f %f\n",
  135. i, tab1[i], tab2[i]);
  136. }
  137. error+= e*e;
  138. if(e>max) max= e;
  139. }
  140. av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
  141. }
  142. static void help(void)
  143. {
  144. av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
  145. "-h print this help\n"
  146. "-s speed test\n"
  147. "-m (I)MDCT test\n"
  148. "-i inverse transform test\n"
  149. "-n b set the transform size to 2^b\n"
  150. "-f x set scale factor for output data of (I)MDCT to x\n"
  151. );
  152. exit(1);
  153. }
  154. int main(int argc, char **argv)
  155. {
  156. FFTComplex *tab, *tab1, *tab_ref;
  157. FFTSample *tab2;
  158. int it, i, c;
  159. int do_speed = 0;
  160. int do_mdct = 0;
  161. int do_inverse = 0;
  162. FFTContext s1, *s = &s1;
  163. MDCTContext m1, *m = &m1;
  164. int fft_nbits, fft_size;
  165. double scale = 1.0;
  166. fft_nbits = 9;
  167. for(;;) {
  168. c = getopt(argc, argv, "hsimn:f:");
  169. if (c == -1)
  170. break;
  171. switch(c) {
  172. case 'h':
  173. help();
  174. break;
  175. case 's':
  176. do_speed = 1;
  177. break;
  178. case 'i':
  179. do_inverse = 1;
  180. break;
  181. case 'm':
  182. do_mdct = 1;
  183. break;
  184. case 'n':
  185. fft_nbits = atoi(optarg);
  186. break;
  187. case 'f':
  188. scale = atof(optarg);
  189. break;
  190. }
  191. }
  192. fft_size = 1 << fft_nbits;
  193. tab = av_malloc(fft_size * sizeof(FFTComplex));
  194. tab1 = av_malloc(fft_size * sizeof(FFTComplex));
  195. tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
  196. tab2 = av_malloc(fft_size * sizeof(FFTSample));
  197. if (do_mdct) {
  198. av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
  199. if (do_inverse)
  200. av_log(NULL, AV_LOG_INFO,"IMDCT");
  201. else
  202. av_log(NULL, AV_LOG_INFO,"MDCT");
  203. ff_mdct_init(m, fft_nbits, do_inverse, scale);
  204. } else {
  205. if (do_inverse)
  206. av_log(NULL, AV_LOG_INFO,"IFFT");
  207. else
  208. av_log(NULL, AV_LOG_INFO,"FFT");
  209. ff_fft_init(s, fft_nbits, do_inverse);
  210. fft_ref_init(fft_nbits, do_inverse);
  211. }
  212. av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
  213. /* generate random data */
  214. for(i=0;i<fft_size;i++) {
  215. tab1[i].re = frandom();
  216. tab1[i].im = frandom();
  217. }
  218. /* checking result */
  219. av_log(NULL, AV_LOG_INFO,"Checking...\n");
  220. if (do_mdct) {
  221. if (do_inverse) {
  222. imdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
  223. ff_imdct_calc(m, tab2, (float *)tab1);
  224. check_diff((float *)tab_ref, tab2, fft_size, scale);
  225. } else {
  226. mdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
  227. ff_mdct_calc(m, tab2, (float *)tab1);
  228. check_diff((float *)tab_ref, tab2, fft_size / 2, scale);
  229. }
  230. } else {
  231. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  232. ff_fft_permute(s, tab);
  233. ff_fft_calc(s, tab);
  234. fft_ref(tab_ref, tab1, fft_nbits);
  235. check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 1.0);
  236. }
  237. /* do a speed test */
  238. if (do_speed) {
  239. int64_t time_start, duration;
  240. int nb_its;
  241. av_log(NULL, AV_LOG_INFO,"Speed test...\n");
  242. /* we measure during about 1 seconds */
  243. nb_its = 1;
  244. for(;;) {
  245. time_start = gettime();
  246. for(it=0;it<nb_its;it++) {
  247. if (do_mdct) {
  248. if (do_inverse) {
  249. ff_imdct_calc(m, (float *)tab, (float *)tab1);
  250. } else {
  251. ff_mdct_calc(m, (float *)tab, (float *)tab1);
  252. }
  253. } else {
  254. memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
  255. ff_fft_calc(s, tab);
  256. }
  257. }
  258. duration = gettime() - time_start;
  259. if (duration >= 1000000)
  260. break;
  261. nb_its *= 2;
  262. }
  263. av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
  264. (double)duration / nb_its,
  265. (double)duration / 1000000.0,
  266. nb_its);
  267. }
  268. if (do_mdct) {
  269. ff_mdct_end(m);
  270. } else {
  271. ff_fft_end(s);
  272. }
  273. return 0;
  274. }