Statistics
| Branch: | Revision:

ffmpeg / libavcodec / fft-test.c @ 86c624af

History | View | Annotate | Download (7.45 KB)

1 04d7f601 Diego Biurrun
/*
2
 * (c) 2002 Fabrice Bellard
3
 *
4 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7 04d7f601 Diego Biurrun
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
10 04d7f601 Diego Biurrun
 *
11 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
12 04d7f601 Diego Biurrun
 * 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.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
18 04d7f601 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21 983e3246 Michael Niedermayer
/**
22
 * @file fft-test.c
23
 * FFT and MDCT tests.
24
 */
25
26 bb6f5690 Fabrice Bellard
#include "dsputil.h"
27
#include <math.h>
28 e366e679 Fabrice Bellard
#include <unistd.h>
29 bb6f5690 Fabrice Bellard
#include <sys/time.h>
30 12807c8d Ronald S. Bultje
#include <stdlib.h>
31
#include <string.h>
32 bb6f5690 Fabrice Bellard
33 28186473 Diego Biurrun
#undef exit
34 cc61f96f Panagiotis Issaris
#undef random
35 28186473 Diego Biurrun
36 bb6f5690 Fabrice Bellard
int mm_flags;
37
38
/* reference fft */
39
40
#define MUL16(a,b) ((a) * (b))
41
42
#define CMAC(pre, pim, are, aim, bre, bim) \
43
{\
44
   pre += (MUL16(are, bre) - MUL16(aim, bim));\
45
   pim += (MUL16(are, bim) + MUL16(bre, aim));\
46
}
47
48
FFTComplex *exptab;
49
50
void fft_ref_init(int nbits, int inverse)
51
{
52
    int n, i;
53
    float c1, s1, alpha;
54
55
    n = 1 << nbits;
56
    exptab = av_malloc((n / 2) * sizeof(FFTComplex));
57
58
    for(i=0;i<(n/2);i++) {
59
        alpha = 2 * M_PI * (float)i / (float)n;
60
        c1 = cos(alpha);
61
        s1 = sin(alpha);
62
        if (!inverse)
63
            s1 = -s1;
64
        exptab[i].re = c1;
65
        exptab[i].im = s1;
66
    }
67
}
68
69
void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
70
{
71
    int n, i, j, k, n2;
72
    float tmp_re, tmp_im, s, c;
73
    FFTComplex *q;
74
75
    n = 1 << nbits;
76
    n2 = n >> 1;
77
    for(i=0;i<n;i++) {
78
        tmp_re = 0;
79
        tmp_im = 0;
80
        q = tab;
81
        for(j=0;j<n;j++) {
82
            k = (i * j) & (n - 1);
83
            if (k >= n2) {
84
                c = -exptab[k - n2].re;
85
                s = -exptab[k - n2].im;
86
            } else {
87
                c = exptab[k].re;
88
                s = exptab[k].im;
89
            }
90
            CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
91
            q++;
92
        }
93
        tabr[i].re = tmp_re;
94
        tabr[i].im = tmp_im;
95
    }
96
}
97
98
void imdct_ref(float *out, float *in, int n)
99
{
100
    int k, i, a;
101
    float sum, f;
102
103
    for(i=0;i<n;i++) {
104
        sum = 0;
105
        for(k=0;k<n/2;k++) {
106
            a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
107
            f = cos(M_PI * a / (double)(2 * n));
108
            sum += f * in[k];
109
        }
110
        out[i] = -sum;
111
    }
112
}
113
114
/* NOTE: no normalisation by 1 / N is done */
115
void mdct_ref(float *output, float *input, int n)
116
{
117
    int k, i;
118
    float a, s;
119
120
    /* do it by hand */
121
    for(k=0;k<n/2;k++) {
122
        s = 0;
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);
126
        }
127
        output[k] = s;
128
    }
129
}
130
131
132
float frandom(void)
133
{
134
    return (float)((random() & 0xffff) - 32768) / 32768.0;
135
}
136
137 0c1a9eda Zdenek Kabelac
int64_t gettime(void)
138 bb6f5690 Fabrice Bellard
{
139
    struct timeval tv;
140
    gettimeofday(&tv,NULL);
141 0c1a9eda Zdenek Kabelac
    return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
142 bb6f5690 Fabrice Bellard
}
143
144
void check_diff(float *tab1, float *tab2, int n)
145
{
146
    int i;
147 86c624af Michael Niedermayer
    double max= 0;
148
    double error= 0;
149 bb6f5690 Fabrice Bellard
150
    for(i=0;i<n;i++) {
151 86c624af Michael Niedermayer
        double e= fabsf(tab1[i] - tab2[i]);
152
        if (e >= 1e-3) {
153 115329f1 Diego Biurrun
            av_log(NULL, AV_LOG_ERROR, "ERROR %d: %f %f\n",
154 bb6f5690 Fabrice Bellard
                   i, tab1[i], tab2[i]);
155
        }
156 86c624af Michael Niedermayer
        error+= e*e;
157
        if(e>max) max= e;
158 bb6f5690 Fabrice Bellard
    }
159 86c624af Michael Niedermayer
    av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
160 bb6f5690 Fabrice Bellard
}
161
162
163
void help(void)
164
{
165 09798315 Benjamin Larsson
    av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
166 bb6f5690 Fabrice Bellard
           "-h     print this help\n"
167
           "-s     speed test\n"
168
           "-m     (I)MDCT test\n"
169
           "-i     inverse transform test\n"
170
           "-n b   set the transform size to 2^b\n"
171
           );
172
    exit(1);
173
}
174
175
176
177
int main(int argc, char **argv)
178
{
179
    FFTComplex *tab, *tab1, *tab_ref;
180
    FFTSample *tabtmp, *tab2;
181
    int it, i, c;
182
    int do_speed = 0;
183
    int do_mdct = 0;
184
    int do_inverse = 0;
185
    FFTContext s1, *s = &s1;
186
    MDCTContext m1, *m = &m1;
187
    int fft_nbits, fft_size;
188
189
    mm_flags = 0;
190
    fft_nbits = 9;
191
    for(;;) {
192
        c = getopt(argc, argv, "hsimn:");
193
        if (c == -1)
194
            break;
195
        switch(c) {
196
        case 'h':
197
            help();
198
            break;
199
        case 's':
200
            do_speed = 1;
201
            break;
202
        case 'i':
203
            do_inverse = 1;
204
            break;
205
        case 'm':
206
            do_mdct = 1;
207
            break;
208
        case 'n':
209
            fft_nbits = atoi(optarg);
210
            break;
211
        }
212
    }
213
214
    fft_size = 1 << fft_nbits;
215
    tab = av_malloc(fft_size * sizeof(FFTComplex));
216
    tab1 = av_malloc(fft_size * sizeof(FFTComplex));
217
    tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
218
    tabtmp = av_malloc(fft_size / 2 * sizeof(FFTSample));
219
    tab2 = av_malloc(fft_size * sizeof(FFTSample));
220
221
    if (do_mdct) {
222
        if (do_inverse)
223 09798315 Benjamin Larsson
            av_log(NULL, AV_LOG_INFO,"IMDCT");
224 bb6f5690 Fabrice Bellard
        else
225 09798315 Benjamin Larsson
            av_log(NULL, AV_LOG_INFO,"MDCT");
226 ea0f841a Fabrice Bellard
        ff_mdct_init(m, fft_nbits, do_inverse);
227 bb6f5690 Fabrice Bellard
    } else {
228
        if (do_inverse)
229 09798315 Benjamin Larsson
            av_log(NULL, AV_LOG_INFO,"IFFT");
230 bb6f5690 Fabrice Bellard
        else
231 09798315 Benjamin Larsson
            av_log(NULL, AV_LOG_INFO,"FFT");
232 68951ecf Gildas Bazin
        ff_fft_init(s, fft_nbits, do_inverse);
233 bb6f5690 Fabrice Bellard
        fft_ref_init(fft_nbits, do_inverse);
234
    }
235 09798315 Benjamin Larsson
    av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
236 bb6f5690 Fabrice Bellard
237
    /* generate random data */
238
239
    for(i=0;i<fft_size;i++) {
240
        tab1[i].re = frandom();
241
        tab1[i].im = frandom();
242
    }
243
244
    /* checking result */
245 09798315 Benjamin Larsson
    av_log(NULL, AV_LOG_INFO,"Checking...\n");
246 bb6f5690 Fabrice Bellard
247
    if (do_mdct) {
248
        if (do_inverse) {
249
            imdct_ref((float *)tab_ref, (float *)tab1, fft_size);
250 ea0f841a Fabrice Bellard
            ff_imdct_calc(m, tab2, (float *)tab1, tabtmp);
251 bb6f5690 Fabrice Bellard
            check_diff((float *)tab_ref, tab2, fft_size);
252
        } else {
253
            mdct_ref((float *)tab_ref, (float *)tab1, fft_size);
254 115329f1 Diego Biurrun
255 ea0f841a Fabrice Bellard
            ff_mdct_calc(m, tab2, (float *)tab1, tabtmp);
256 bb6f5690 Fabrice Bellard
257
            check_diff((float *)tab_ref, tab2, fft_size / 2);
258
        }
259
    } else {
260
        memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
261 68951ecf Gildas Bazin
        ff_fft_permute(s, tab);
262
        ff_fft_calc(s, tab);
263 115329f1 Diego Biurrun
264 bb6f5690 Fabrice Bellard
        fft_ref(tab_ref, tab1, fft_nbits);
265
        check_diff((float *)tab_ref, (float *)tab, fft_size * 2);
266
    }
267
268
    /* do a speed test */
269
270
    if (do_speed) {
271 0c1a9eda Zdenek Kabelac
        int64_t time_start, duration;
272 bb6f5690 Fabrice Bellard
        int nb_its;
273
274 09798315 Benjamin Larsson
        av_log(NULL, AV_LOG_INFO,"Speed test...\n");
275 bb6f5690 Fabrice Bellard
        /* we measure during about 1 seconds */
276
        nb_its = 1;
277
        for(;;) {
278
            time_start = gettime();
279
            for(it=0;it<nb_its;it++) {
280
                if (do_mdct) {
281
                    if (do_inverse) {
282 ea0f841a Fabrice Bellard
                        ff_imdct_calc(m, (float *)tab, (float *)tab1, tabtmp);
283 bb6f5690 Fabrice Bellard
                    } else {
284 ea0f841a Fabrice Bellard
                        ff_mdct_calc(m, (float *)tab, (float *)tab1, tabtmp);
285 bb6f5690 Fabrice Bellard
                    }
286
                } else {
287
                    memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
288 68951ecf Gildas Bazin
                    ff_fft_calc(s, tab);
289 bb6f5690 Fabrice Bellard
                }
290
            }
291
            duration = gettime() - time_start;
292
            if (duration >= 1000000)
293
                break;
294
            nb_its *= 2;
295
        }
296 115329f1 Diego Biurrun
        av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
297
               (double)duration / nb_its,
298 bb6f5690 Fabrice Bellard
               (double)duration / 1000000.0,
299
               nb_its);
300
    }
301 115329f1 Diego Biurrun
302 bb6f5690 Fabrice Bellard
    if (do_mdct) {
303 ea0f841a Fabrice Bellard
        ff_mdct_end(m);
304 bb6f5690 Fabrice Bellard
    } else {
305 68951ecf Gildas Bazin
        ff_fft_end(s);
306 bb6f5690 Fabrice Bellard
    }
307
    return 0;
308
}