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.

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