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
22 * @file libavcodec/fft-test.c
26 #include "libavutil/lfg.h"
38 #define MUL16(a,b) ((a) * (b))
40 #define CMAC(pre, pim, are, aim, bre, bim) \
42 pre += (MUL16(are, bre) - MUL16(aim, bim));\
43 pim += (MUL16(are, bim) + MUL16(bre, aim));\
48 static void fft_ref_init(int nbits, int inverse)
54 exptab = av_malloc((n / 2) * sizeof(FFTComplex));
56 for (i = 0; i < (n/2); i++) {
57 alpha = 2 * M_PI * (float)i / (float)n;
67 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
70 double tmp_re, tmp_im, s, c;
75 for (i = 0; i < n; i++) {
79 for (j = 0; j < n; j++) {
80 k = (i * j) & (n - 1);
82 c = -exptab[k - n2].re;
83 s = -exptab[k - n2].im;
88 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
96 static void imdct_ref(float *out, float *in, int nbits)
102 for (i = 0; i < n; i++) {
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));
113 /* NOTE: no normalisation by 1 / N is done */
114 static void mdct_ref(float *output, float *input, int nbits)
121 for (k = 0; k < n/2; k++) {
123 for (i = 0; i < n; i++) {
124 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
125 s += input[i] * cos(a);
132 static float frandom(AVLFG *prng)
134 return (int16_t)av_lfg_get(prng) / 32768.0;
137 static int64_t gettime(void)
140 gettimeofday(&tv,NULL);
141 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
144 static void check_diff(float *tab1, float *tab2, int n, double scale)
150 for (i = 0; i < n; i++) {
151 double e= fabsf(tab1[i] - (tab2[i] / scale));
153 av_log(NULL, AV_LOG_ERROR, "ERROR %d: %f %f\n",
154 i, tab1[i], tab2[i]);
159 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
163 static void help(void)
165 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
166 "-h print this help\n"
169 "-i inverse transform test\n"
170 "-n b set the transform size to 2^b\n"
171 "-f x set scale factor for output data of (I)MDCT to x\n"
182 int main(int argc, char **argv)
184 FFTComplex *tab, *tab1, *tab_ref;
188 enum tf_transform transform = TRANSFORM_FFT;
190 FFTContext s1, *s = &s1;
191 FFTContext m1, *m = &m1;
192 RDFTContext r1, *r = &r1;
193 int fft_nbits, fft_size, fft_size_2;
196 av_lfg_init(&prng, 1);
200 c = getopt(argc, argv, "hsimrn:f:");
214 transform = TRANSFORM_MDCT;
217 transform = TRANSFORM_RDFT;
220 fft_nbits = atoi(optarg);
223 scale = atof(optarg);
228 fft_size = 1 << fft_nbits;
229 fft_size_2 = fft_size >> 1;
230 tab = av_malloc(fft_size * sizeof(FFTComplex));
231 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
232 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
233 tab2 = av_malloc(fft_size * sizeof(FFTSample));
237 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
239 av_log(NULL, AV_LOG_INFO,"IMDCT");
241 av_log(NULL, AV_LOG_INFO,"MDCT");
242 ff_mdct_init(m, fft_nbits, do_inverse, scale);
246 av_log(NULL, AV_LOG_INFO,"IFFT");
248 av_log(NULL, AV_LOG_INFO,"FFT");
249 ff_fft_init(s, fft_nbits, do_inverse);
250 fft_ref_init(fft_nbits, do_inverse);
254 av_log(NULL, AV_LOG_INFO,"IRDFT");
256 av_log(NULL, AV_LOG_INFO,"RDFT");
257 ff_rdft_init(r, fft_nbits, do_inverse ? IRDFT : RDFT);
258 fft_ref_init(fft_nbits, do_inverse);
261 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
263 /* generate random data */
265 for (i = 0; i < fft_size; i++) {
266 tab1[i].re = frandom(&prng);
267 tab1[i].im = frandom(&prng);
270 /* checking result */
271 av_log(NULL, AV_LOG_INFO,"Checking...\n");
276 imdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
277 ff_imdct_calc(m, tab2, (float *)tab1);
278 check_diff((float *)tab_ref, tab2, fft_size, scale);
280 mdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
282 ff_mdct_calc(m, tab2, (float *)tab1);
284 check_diff((float *)tab_ref, tab2, fft_size / 2, scale);
288 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
289 ff_fft_permute(s, tab);
292 fft_ref(tab_ref, tab1, fft_nbits);
293 check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 1.0);
298 tab1[fft_size_2].im = 0;
299 for (i = 1; i < fft_size_2; i++) {
300 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
301 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
304 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
305 tab2[1] = tab1[fft_size_2].re;
307 ff_rdft_calc(r, tab2);
308 fft_ref(tab_ref, tab1, fft_nbits);
309 for (i = 0; i < fft_size; i++) {
313 check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
315 for (i = 0; i < fft_size; i++) {
316 tab2[i] = tab1[i].re;
319 ff_rdft_calc(r, tab2);
320 fft_ref(tab_ref, tab1, fft_nbits);
321 tab_ref[0].im = tab_ref[fft_size_2].re;
322 check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
326 /* do a speed test */
329 int64_t time_start, duration;
332 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
333 /* we measure during about 1 seconds */
336 time_start = gettime();
337 for (it = 0; it < nb_its; it++) {
341 ff_imdct_calc(m, (float *)tab, (float *)tab1);
343 ff_mdct_calc(m, (float *)tab, (float *)tab1);
347 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
351 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
352 ff_rdft_calc(r, tab2);
356 duration = gettime() - time_start;
357 if (duration >= 1000000)
361 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
362 (double)duration / nb_its,
363 (double)duration / 1000000.0,