2 * (c) 2002 Fabrice Bellard
4 * This file is part of FFmpeg.
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.
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.
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
40 #define MUL16(a,b) ((a) * (b))
42 #define CMAC(pre, pim, are, aim, bre, bim) \
44 pre += (MUL16(are, bre) - MUL16(aim, bim));\
45 pim += (MUL16(are, bim) + MUL16(bre, aim));\
50 void fft_ref_init(int nbits, int inverse)
56 exptab = av_malloc((n / 2) * sizeof(FFTComplex));
58 for(i=0;i<(n/2);i++) {
59 alpha = 2 * M_PI * (float)i / (float)n;
69 void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
72 double tmp_re, tmp_im, s, c;
82 k = (i * j) & (n - 1);
84 c = -exptab[k - n2].re;
85 s = -exptab[k - n2].im;
90 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
98 void imdct_ref(float *out, float *in, int nbits)
107 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
108 f = cos(M_PI * a / (double)(2 * n));
115 /* NOTE: no normalisation by 1 / N is done */
116 void mdct_ref(float *output, float *input, int nbits)
126 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
127 s += input[i] * cos(a);
136 return (float)((random() & 0xffff) - 32768) / 32768.0;
139 int64_t gettime(void)
142 gettimeofday(&tv,NULL);
143 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
146 void check_diff(float *tab1, float *tab2, int n)
153 double e= fabsf(tab1[i] - tab2[i]);
155 av_log(NULL, AV_LOG_ERROR, "ERROR %d: %f %f\n",
156 i, tab1[i], tab2[i]);
161 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
167 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
168 "-h print this help\n"
171 "-i inverse transform test\n"
172 "-n b set the transform size to 2^b\n"
179 int main(int argc, char **argv)
181 FFTComplex *tab, *tab1, *tab_ref;
182 FFTSample *tabtmp, *tab2;
187 FFTContext s1, *s = &s1;
188 MDCTContext m1, *m = &m1;
189 int fft_nbits, fft_size;
194 c = getopt(argc, argv, "hsimn:");
211 fft_nbits = atoi(optarg);
216 fft_size = 1 << fft_nbits;
217 tab = av_malloc(fft_size * sizeof(FFTComplex));
218 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
219 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
220 tabtmp = av_malloc(fft_size / 2 * sizeof(FFTSample));
221 tab2 = av_malloc(fft_size * sizeof(FFTSample));
225 av_log(NULL, AV_LOG_INFO,"IMDCT");
227 av_log(NULL, AV_LOG_INFO,"MDCT");
228 ff_mdct_init(m, fft_nbits, do_inverse);
231 av_log(NULL, AV_LOG_INFO,"IFFT");
233 av_log(NULL, AV_LOG_INFO,"FFT");
234 ff_fft_init(s, fft_nbits, do_inverse);
235 fft_ref_init(fft_nbits, do_inverse);
237 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
239 /* generate random data */
241 for(i=0;i<fft_size;i++) {
242 tab1[i].re = frandom();
243 tab1[i].im = frandom();
246 /* checking result */
247 av_log(NULL, AV_LOG_INFO,"Checking...\n");
251 imdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
252 ff_imdct_calc(m, tab2, (float *)tab1, tabtmp);
253 check_diff((float *)tab_ref, tab2, fft_size);
255 mdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
257 ff_mdct_calc(m, tab2, (float *)tab1, tabtmp);
259 check_diff((float *)tab_ref, tab2, fft_size / 2);
262 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
263 ff_fft_permute(s, tab);
266 fft_ref(tab_ref, tab1, fft_nbits);
267 check_diff((float *)tab_ref, (float *)tab, fft_size * 2);
270 /* do a speed test */
273 int64_t time_start, duration;
276 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
277 /* we measure during about 1 seconds */
280 time_start = gettime();
281 for(it=0;it<nb_its;it++) {
284 ff_imdct_calc(m, (float *)tab, (float *)tab1, tabtmp);
286 ff_mdct_calc(m, (float *)tab, (float *)tab1, tabtmp);
289 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
293 duration = gettime() - time_start;
294 if (duration >= 1000000)
298 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
299 (double)duration / nb_its,
300 (double)duration / 1000000.0,