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