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