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.

296 lines
7.2KB

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