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.

311 lines
7.5KB

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