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