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
37 #define MUL16(a,b) ((a) * (b))
39 #define CMAC(pre, pim, are, aim, bre, bim) \
41 pre += (MUL16(are, bre) - MUL16(aim, bim));\
42 pim += (MUL16(are, bim) + MUL16(bre, aim));\
47 void fft_ref_init(int nbits, int inverse)
53 exptab = av_malloc((n / 2) * sizeof(FFTComplex));
55 for(i=0;i<(n/2);i++) {
56 alpha = 2 * M_PI * (float)i / (float)n;
66 void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
69 float tmp_re, tmp_im, s, c;
79 k = (i * j) & (n - 1);
81 c = -exptab[k - n2].re;
82 s = -exptab[k - n2].im;
87 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
95 void imdct_ref(float *out, float *in, int n)
103 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
104 f = cos(M_PI * a / (double)(2 * n));
111 /* NOTE: no normalisation by 1 / N is done */
112 void mdct_ref(float *output, float *input, int n)
121 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
122 s += input[i] * cos(a);
131 return (float)((random() & 0xffff) - 32768) / 32768.0;
134 int64_t gettime(void)
137 gettimeofday(&tv,NULL);
138 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
141 void check_diff(float *tab1, float *tab2, int n)
146 if (fabsf(tab1[i] - tab2[i]) >= 1e-3) {
147 av_log(NULL, AV_LOG_ERROR, "ERROR %d: %f %f\n",
148 i, tab1[i], tab2[i]);
156 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
157 "-h print this help\n"
160 "-i inverse transform test\n"
161 "-n b set the transform size to 2^b\n"
168 int main(int argc, char **argv)
170 FFTComplex *tab, *tab1, *tab_ref;
171 FFTSample *tabtmp, *tab2;
176 FFTContext s1, *s = &s1;
177 MDCTContext m1, *m = &m1;
178 int fft_nbits, fft_size;
183 c = getopt(argc, argv, "hsimn:");
200 fft_nbits = atoi(optarg);
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));
214 av_log(NULL, AV_LOG_INFO,"IMDCT");
216 av_log(NULL, AV_LOG_INFO,"MDCT");
217 ff_mdct_init(m, fft_nbits, do_inverse);
220 av_log(NULL, AV_LOG_INFO,"IFFT");
222 av_log(NULL, AV_LOG_INFO,"FFT");
223 ff_fft_init(s, fft_nbits, do_inverse);
224 fft_ref_init(fft_nbits, do_inverse);
226 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
228 /* generate random data */
230 for(i=0;i<fft_size;i++) {
231 tab1[i].re = frandom();
232 tab1[i].im = frandom();
235 /* checking result */
236 av_log(NULL, AV_LOG_INFO,"Checking...\n");
240 imdct_ref((float *)tab_ref, (float *)tab1, fft_size);
241 ff_imdct_calc(m, tab2, (float *)tab1, tabtmp);
242 check_diff((float *)tab_ref, tab2, fft_size);
244 mdct_ref((float *)tab_ref, (float *)tab1, fft_size);
246 ff_mdct_calc(m, tab2, (float *)tab1, tabtmp);
248 check_diff((float *)tab_ref, tab2, fft_size / 2);
251 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
252 ff_fft_permute(s, tab);
255 fft_ref(tab_ref, tab1, fft_nbits);
256 check_diff((float *)tab_ref, (float *)tab, fft_size * 2);
259 /* do a speed test */
262 int64_t time_start, duration;
265 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
266 /* we measure during about 1 seconds */
269 time_start = gettime();
270 for(it=0;it<nb_its;it++) {
273 ff_imdct_calc(m, (float *)tab, (float *)tab1, tabtmp);
275 ff_mdct_calc(m, (float *)tab, (float *)tab1, tabtmp);
278 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
282 duration = gettime() - time_start;
283 if (duration >= 1000000)
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,
289 (double)duration / 1000000.0,