Fix crop filter start_frame() for paletted formats different from
[ffmpeg-lucabe.git] / libavcodec / fft-test.c
blob925711dc76fcb5c5da0a54f3634658904efb6106
1 /*
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
21 /**
22 * @file libavcodec/fft-test.c
23 * FFT and MDCT tests.
26 #include "libavutil/lfg.h"
27 #include "dsputil.h"
28 #include <math.h>
29 #include <unistd.h>
30 #include <sys/time.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #undef exit
36 /* reference fft */
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));\
46 FFTComplex *exptab;
48 static void fft_ref_init(int nbits, int inverse)
50 int n, i;
51 double c1, s1, alpha;
53 n = 1 << nbits;
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;
58 c1 = cos(alpha);
59 s1 = sin(alpha);
60 if (!inverse)
61 s1 = -s1;
62 exptab[i].re = c1;
63 exptab[i].im = s1;
67 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
69 int n, i, j, k, n2;
70 double tmp_re, tmp_im, s, c;
71 FFTComplex *q;
73 n = 1 << nbits;
74 n2 = n >> 1;
75 for(i=0;i<n;i++) {
76 tmp_re = 0;
77 tmp_im = 0;
78 q = tab;
79 for(j=0;j<n;j++) {
80 k = (i * j) & (n - 1);
81 if (k >= n2) {
82 c = -exptab[k - n2].re;
83 s = -exptab[k - n2].im;
84 } else {
85 c = exptab[k].re;
86 s = exptab[k].im;
88 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
89 q++;
91 tabr[i].re = tmp_re;
92 tabr[i].im = tmp_im;
96 static void imdct_ref(float *out, float *in, int nbits)
98 int n = 1<<nbits;
99 int k, i, a;
100 double sum, f;
102 for(i=0;i<n;i++) {
103 sum = 0;
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));
107 sum += f * in[k];
109 out[i] = -sum;
113 /* NOTE: no normalisation by 1 / N is done */
114 static void mdct_ref(float *output, float *input, int nbits)
116 int n = 1<<nbits;
117 int k, i;
118 double a, s;
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);
127 output[k] = s;
132 static float frandom(AVLFG *prng)
134 return (int16_t)av_lfg_get(prng) / 32768.0;
137 static int64_t gettime(void)
139 struct timeval tv;
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)
146 int i;
147 double max= 0;
148 double error= 0;
150 for(i=0;i<n;i++) {
151 double e= fabsf(tab1[i] - (tab2[i] / scale));
152 if (e >= 1e-3) {
153 av_log(NULL, AV_LOG_ERROR, "ERROR %d: %f %f\n",
154 i, tab1[i], tab2[i]);
156 error+= e*e;
157 if(e>max) max= e;
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"
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 "-f x set scale factor for output data of (I)MDCT to x\n"
173 exit(1);
178 int main(int argc, char **argv)
180 FFTComplex *tab, *tab1, *tab_ref;
181 FFTSample *tab2;
182 int it, i, c;
183 int do_speed = 0;
184 int do_mdct = 0;
185 int do_inverse = 0;
186 FFTContext s1, *s = &s1;
187 FFTContext m1, *m = &m1;
188 int fft_nbits, fft_size;
189 double scale = 1.0;
190 AVLFG prng;
191 av_lfg_init(&prng, 1);
193 fft_nbits = 9;
194 for(;;) {
195 c = getopt(argc, argv, "hsimn:f:");
196 if (c == -1)
197 break;
198 switch(c) {
199 case 'h':
200 help();
201 break;
202 case 's':
203 do_speed = 1;
204 break;
205 case 'i':
206 do_inverse = 1;
207 break;
208 case 'm':
209 do_mdct = 1;
210 break;
211 case 'n':
212 fft_nbits = atoi(optarg);
213 break;
214 case 'f':
215 scale = atof(optarg);
216 break;
220 fft_size = 1 << fft_nbits;
221 tab = av_malloc(fft_size * sizeof(FFTComplex));
222 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
223 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
224 tab2 = av_malloc(fft_size * sizeof(FFTSample));
226 if (do_mdct) {
227 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
228 if (do_inverse)
229 av_log(NULL, AV_LOG_INFO,"IMDCT");
230 else
231 av_log(NULL, AV_LOG_INFO,"MDCT");
232 ff_mdct_init(m, fft_nbits, do_inverse, scale);
233 } else {
234 if (do_inverse)
235 av_log(NULL, AV_LOG_INFO,"IFFT");
236 else
237 av_log(NULL, AV_LOG_INFO,"FFT");
238 ff_fft_init(s, fft_nbits, do_inverse);
239 fft_ref_init(fft_nbits, do_inverse);
241 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
243 /* generate random data */
245 for(i=0;i<fft_size;i++) {
246 tab1[i].re = frandom(&prng);
247 tab1[i].im = frandom(&prng);
250 /* checking result */
251 av_log(NULL, AV_LOG_INFO,"Checking...\n");
253 if (do_mdct) {
254 if (do_inverse) {
255 imdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
256 ff_imdct_calc(m, tab2, (float *)tab1);
257 check_diff((float *)tab_ref, tab2, fft_size, scale);
258 } else {
259 mdct_ref((float *)tab_ref, (float *)tab1, fft_nbits);
261 ff_mdct_calc(m, tab2, (float *)tab1);
263 check_diff((float *)tab_ref, tab2, fft_size / 2, scale);
265 } else {
266 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
267 ff_fft_permute(s, tab);
268 ff_fft_calc(s, tab);
270 fft_ref(tab_ref, tab1, fft_nbits);
271 check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 1.0);
274 /* do a speed test */
276 if (do_speed) {
277 int64_t time_start, duration;
278 int nb_its;
280 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
281 /* we measure during about 1 seconds */
282 nb_its = 1;
283 for(;;) {
284 time_start = gettime();
285 for(it=0;it<nb_its;it++) {
286 if (do_mdct) {
287 if (do_inverse) {
288 ff_imdct_calc(m, (float *)tab, (float *)tab1);
289 } else {
290 ff_mdct_calc(m, (float *)tab, (float *)tab1);
292 } else {
293 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
294 ff_fft_calc(s, tab);
297 duration = gettime() - time_start;
298 if (duration >= 1000000)
299 break;
300 nb_its *= 2;
302 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
303 (double)duration / nb_its,
304 (double)duration / 1000000.0,
305 nb_its);
308 if (do_mdct) {
309 ff_mdct_end(m);
310 } else {
311 ff_fft_end(s);
313 return 0;