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.

300 lines
7.2KB

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