00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/mathematics.h"
00027 #include "libavutil/lfg.h"
00028 #include "libavutil/log.h"
00029 #include "fft.h"
00030 #if CONFIG_FFT_FLOAT
00031 #include "dct.h"
00032 #include "rdft.h"
00033 #endif
00034 #include <math.h>
00035 #include <unistd.h>
00036 #include <sys/time.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039
00040
00041
00042 #define MUL16(a,b) ((a) * (b))
00043
00044 #define CMAC(pre, pim, are, aim, bre, bim) \
00045 {\
00046 pre += (MUL16(are, bre) - MUL16(aim, bim));\
00047 pim += (MUL16(are, bim) + MUL16(bre, aim));\
00048 }
00049
00050 #if CONFIG_FFT_FLOAT
00051 # define RANGE 1.0
00052 # define REF_SCALE(x, bits) (x)
00053 # define FMT "%10.6f"
00054 #else
00055 # define RANGE 16384
00056 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
00057 # define FMT "%6d"
00058 #endif
00059
00060 struct {
00061 float re, im;
00062 } *exptab;
00063
00064 static void fft_ref_init(int nbits, int inverse)
00065 {
00066 int n, i;
00067 double c1, s1, alpha;
00068
00069 n = 1 << nbits;
00070 exptab = av_malloc((n / 2) * sizeof(*exptab));
00071
00072 for (i = 0; i < (n/2); i++) {
00073 alpha = 2 * M_PI * (float)i / (float)n;
00074 c1 = cos(alpha);
00075 s1 = sin(alpha);
00076 if (!inverse)
00077 s1 = -s1;
00078 exptab[i].re = c1;
00079 exptab[i].im = s1;
00080 }
00081 }
00082
00083 static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
00084 {
00085 int n, i, j, k, n2;
00086 double tmp_re, tmp_im, s, c;
00087 FFTComplex *q;
00088
00089 n = 1 << nbits;
00090 n2 = n >> 1;
00091 for (i = 0; i < n; i++) {
00092 tmp_re = 0;
00093 tmp_im = 0;
00094 q = tab;
00095 for (j = 0; j < n; j++) {
00096 k = (i * j) & (n - 1);
00097 if (k >= n2) {
00098 c = -exptab[k - n2].re;
00099 s = -exptab[k - n2].im;
00100 } else {
00101 c = exptab[k].re;
00102 s = exptab[k].im;
00103 }
00104 CMAC(tmp_re, tmp_im, c, s, q->re, q->im);
00105 q++;
00106 }
00107 tabr[i].re = REF_SCALE(tmp_re, nbits);
00108 tabr[i].im = REF_SCALE(tmp_im, nbits);
00109 }
00110 }
00111
00112 static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
00113 {
00114 int n = 1<<nbits;
00115 int k, i, a;
00116 double sum, f;
00117
00118 for (i = 0; i < n; i++) {
00119 sum = 0;
00120 for (k = 0; k < n/2; k++) {
00121 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
00122 f = cos(M_PI * a / (double)(2 * n));
00123 sum += f * in[k];
00124 }
00125 out[i] = REF_SCALE(-sum, nbits - 2);
00126 }
00127 }
00128
00129
00130 static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
00131 {
00132 int n = 1<<nbits;
00133 int k, i;
00134 double a, s;
00135
00136
00137 for (k = 0; k < n/2; k++) {
00138 s = 0;
00139 for (i = 0; i < n; i++) {
00140 a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n));
00141 s += input[i] * cos(a);
00142 }
00143 output[k] = REF_SCALE(s, nbits - 1);
00144 }
00145 }
00146
00147 #if CONFIG_FFT_FLOAT
00148 static void idct_ref(float *output, float *input, int nbits)
00149 {
00150 int n = 1<<nbits;
00151 int k, i;
00152 double a, s;
00153
00154
00155 for (i = 0; i < n; i++) {
00156 s = 0.5 * input[0];
00157 for (k = 1; k < n; k++) {
00158 a = M_PI*k*(i+0.5) / n;
00159 s += input[k] * cos(a);
00160 }
00161 output[i] = 2 * s / n;
00162 }
00163 }
00164 static void dct_ref(float *output, float *input, int nbits)
00165 {
00166 int n = 1<<nbits;
00167 int k, i;
00168 double a, s;
00169
00170
00171 for (k = 0; k < n; k++) {
00172 s = 0;
00173 for (i = 0; i < n; i++) {
00174 a = M_PI*k*(i+0.5) / n;
00175 s += input[i] * cos(a);
00176 }
00177 output[k] = s;
00178 }
00179 }
00180 #endif
00181
00182
00183 static FFTSample frandom(AVLFG *prng)
00184 {
00185 return (int16_t)av_lfg_get(prng) / 32768.0 * RANGE;
00186 }
00187
00188 static int64_t gettime(void)
00189 {
00190 struct timeval tv;
00191 gettimeofday(&tv,NULL);
00192 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
00193 }
00194
00195 static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
00196 {
00197 int i;
00198 double max= 0;
00199 double error= 0;
00200 int err = 0;
00201
00202 for (i = 0; i < n; i++) {
00203 double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
00204 if (e >= 1e-3) {
00205 av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n",
00206 i, tab1[i], tab2[i]);
00207 err = 1;
00208 }
00209 error+= e*e;
00210 if(e>max) max= e;
00211 }
00212 av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error)/n);
00213 return err;
00214 }
00215
00216
00217 static void help(void)
00218 {
00219 av_log(NULL, AV_LOG_INFO,"usage: fft-test [-h] [-s] [-i] [-n b]\n"
00220 "-h print this help\n"
00221 "-s speed test\n"
00222 "-m (I)MDCT test\n"
00223 "-d (I)DCT test\n"
00224 "-r (I)RDFT test\n"
00225 "-i inverse transform test\n"
00226 "-n b set the transform size to 2^b\n"
00227 "-f x set scale factor for output data of (I)MDCT to x\n"
00228 );
00229 }
00230
00231 enum tf_transform {
00232 TRANSFORM_FFT,
00233 TRANSFORM_MDCT,
00234 TRANSFORM_RDFT,
00235 TRANSFORM_DCT,
00236 };
00237
00238 int main(int argc, char **argv)
00239 {
00240 FFTComplex *tab, *tab1, *tab_ref;
00241 FFTSample *tab2;
00242 int it, i, c;
00243 int do_speed = 0;
00244 int err = 1;
00245 enum tf_transform transform = TRANSFORM_FFT;
00246 int do_inverse = 0;
00247 FFTContext s1, *s = &s1;
00248 FFTContext m1, *m = &m1;
00249 #if CONFIG_FFT_FLOAT
00250 RDFTContext r1, *r = &r1;
00251 DCTContext d1, *d = &d1;
00252 int fft_size_2;
00253 #endif
00254 int fft_nbits, fft_size;
00255 double scale = 1.0;
00256 AVLFG prng;
00257 av_lfg_init(&prng, 1);
00258
00259 fft_nbits = 9;
00260 for(;;) {
00261 c = getopt(argc, argv, "hsimrdn:f:");
00262 if (c == -1)
00263 break;
00264 switch(c) {
00265 case 'h':
00266 help();
00267 return 1;
00268 case 's':
00269 do_speed = 1;
00270 break;
00271 case 'i':
00272 do_inverse = 1;
00273 break;
00274 case 'm':
00275 transform = TRANSFORM_MDCT;
00276 break;
00277 case 'r':
00278 transform = TRANSFORM_RDFT;
00279 break;
00280 case 'd':
00281 transform = TRANSFORM_DCT;
00282 break;
00283 case 'n':
00284 fft_nbits = atoi(optarg);
00285 break;
00286 case 'f':
00287 scale = atof(optarg);
00288 break;
00289 }
00290 }
00291
00292 fft_size = 1 << fft_nbits;
00293 tab = av_malloc(fft_size * sizeof(FFTComplex));
00294 tab1 = av_malloc(fft_size * sizeof(FFTComplex));
00295 tab_ref = av_malloc(fft_size * sizeof(FFTComplex));
00296 tab2 = av_malloc(fft_size * sizeof(FFTSample));
00297
00298 switch (transform) {
00299 case TRANSFORM_MDCT:
00300 av_log(NULL, AV_LOG_INFO,"Scale factor is set to %f\n", scale);
00301 if (do_inverse)
00302 av_log(NULL, AV_LOG_INFO,"IMDCT");
00303 else
00304 av_log(NULL, AV_LOG_INFO,"MDCT");
00305 ff_mdct_init(m, fft_nbits, do_inverse, scale);
00306 break;
00307 case TRANSFORM_FFT:
00308 if (do_inverse)
00309 av_log(NULL, AV_LOG_INFO,"IFFT");
00310 else
00311 av_log(NULL, AV_LOG_INFO,"FFT");
00312 ff_fft_init(s, fft_nbits, do_inverse);
00313 fft_ref_init(fft_nbits, do_inverse);
00314 break;
00315 #if CONFIG_FFT_FLOAT
00316 case TRANSFORM_RDFT:
00317 if (do_inverse)
00318 av_log(NULL, AV_LOG_INFO,"IDFT_C2R");
00319 else
00320 av_log(NULL, AV_LOG_INFO,"DFT_R2C");
00321 ff_rdft_init(r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C);
00322 fft_ref_init(fft_nbits, do_inverse);
00323 break;
00324 case TRANSFORM_DCT:
00325 if (do_inverse)
00326 av_log(NULL, AV_LOG_INFO,"DCT_III");
00327 else
00328 av_log(NULL, AV_LOG_INFO,"DCT_II");
00329 ff_dct_init(d, fft_nbits, do_inverse ? DCT_III : DCT_II);
00330 break;
00331 #endif
00332 default:
00333 av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n");
00334 return 1;
00335 }
00336 av_log(NULL, AV_LOG_INFO," %d test\n", fft_size);
00337
00338
00339
00340 for (i = 0; i < fft_size; i++) {
00341 tab1[i].re = frandom(&prng);
00342 tab1[i].im = frandom(&prng);
00343 }
00344
00345
00346 av_log(NULL, AV_LOG_INFO,"Checking...\n");
00347
00348 switch (transform) {
00349 case TRANSFORM_MDCT:
00350 if (do_inverse) {
00351 imdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
00352 m->imdct_calc(m, tab2, (FFTSample *)tab1);
00353 err = check_diff((FFTSample *)tab_ref, tab2, fft_size, scale);
00354 } else {
00355 mdct_ref((FFTSample *)tab_ref, (FFTSample *)tab1, fft_nbits);
00356
00357 m->mdct_calc(m, tab2, (FFTSample *)tab1);
00358
00359 err = check_diff((FFTSample *)tab_ref, tab2, fft_size / 2, scale);
00360 }
00361 break;
00362 case TRANSFORM_FFT:
00363 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
00364 s->fft_permute(s, tab);
00365 s->fft_calc(s, tab);
00366
00367 fft_ref(tab_ref, tab1, fft_nbits);
00368 err = check_diff((FFTSample *)tab_ref, (FFTSample *)tab, fft_size * 2, 1.0);
00369 break;
00370 #if CONFIG_FFT_FLOAT
00371 case TRANSFORM_RDFT:
00372 fft_size_2 = fft_size >> 1;
00373 if (do_inverse) {
00374 tab1[ 0].im = 0;
00375 tab1[fft_size_2].im = 0;
00376 for (i = 1; i < fft_size_2; i++) {
00377 tab1[fft_size_2+i].re = tab1[fft_size_2-i].re;
00378 tab1[fft_size_2+i].im = -tab1[fft_size_2-i].im;
00379 }
00380
00381 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
00382 tab2[1] = tab1[fft_size_2].re;
00383
00384 r->rdft_calc(r, tab2);
00385 fft_ref(tab_ref, tab1, fft_nbits);
00386 for (i = 0; i < fft_size; i++) {
00387 tab[i].re = tab2[i];
00388 tab[i].im = 0;
00389 }
00390 err = check_diff((float *)tab_ref, (float *)tab, fft_size * 2, 0.5);
00391 } else {
00392 for (i = 0; i < fft_size; i++) {
00393 tab2[i] = tab1[i].re;
00394 tab1[i].im = 0;
00395 }
00396 r->rdft_calc(r, tab2);
00397 fft_ref(tab_ref, tab1, fft_nbits);
00398 tab_ref[0].im = tab_ref[fft_size_2].re;
00399 err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
00400 }
00401 break;
00402 case TRANSFORM_DCT:
00403 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
00404 d->dct_calc(d, tab);
00405 if (do_inverse) {
00406 idct_ref(tab_ref, tab1, fft_nbits);
00407 } else {
00408 dct_ref(tab_ref, tab1, fft_nbits);
00409 }
00410 err = check_diff((float *)tab_ref, (float *)tab, fft_size, 1.0);
00411 break;
00412 #endif
00413 }
00414
00415
00416
00417 if (do_speed) {
00418 int64_t time_start, duration;
00419 int nb_its;
00420
00421 av_log(NULL, AV_LOG_INFO,"Speed test...\n");
00422
00423 nb_its = 1;
00424 for(;;) {
00425 time_start = gettime();
00426 for (it = 0; it < nb_its; it++) {
00427 switch (transform) {
00428 case TRANSFORM_MDCT:
00429 if (do_inverse) {
00430 m->imdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
00431 } else {
00432 m->mdct_calc(m, (FFTSample *)tab, (FFTSample *)tab1);
00433 }
00434 break;
00435 case TRANSFORM_FFT:
00436 memcpy(tab, tab1, fft_size * sizeof(FFTComplex));
00437 s->fft_calc(s, tab);
00438 break;
00439 #if CONFIG_FFT_FLOAT
00440 case TRANSFORM_RDFT:
00441 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
00442 r->rdft_calc(r, tab2);
00443 break;
00444 case TRANSFORM_DCT:
00445 memcpy(tab2, tab1, fft_size * sizeof(FFTSample));
00446 d->dct_calc(d, tab2);
00447 break;
00448 #endif
00449 }
00450 }
00451 duration = gettime() - time_start;
00452 if (duration >= 1000000)
00453 break;
00454 nb_its *= 2;
00455 }
00456 av_log(NULL, AV_LOG_INFO,"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
00457 (double)duration / nb_its,
00458 (double)duration / 1000000.0,
00459 nb_its);
00460 }
00461
00462 switch (transform) {
00463 case TRANSFORM_MDCT:
00464 ff_mdct_end(m);
00465 break;
00466 case TRANSFORM_FFT:
00467 ff_fft_end(s);
00468 break;
00469 #if CONFIG_FFT_FLOAT
00470 case TRANSFORM_RDFT:
00471 ff_rdft_end(r);
00472 break;
00473 case TRANSFORM_DCT:
00474 ff_dct_end(d);
00475 break;
00476 #endif
00477 }
00478
00479 av_free(tab);
00480 av_free(tab1);
00481 av_free(tab2);
00482 av_free(tab_ref);
00483 av_free(exptab);
00484
00485 return err;
00486 }