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.

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