1 /*
2 * (c) 2002 Fabrice Bellard
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * FFT and MDCT tests.
24 */
25
27
28 #ifndef AVFFT
30 #endif
31
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
45
46 #if AVFFT
48 #else
50 #endif
51
52 #if FFT_FLOAT
55 #endif
56
57 /* reference fft */
58
59 #define MUL16(a, b) ((a) * (b))
60
61 #define CMAC(pre, pim, are, aim, bre, bim) \
62 { \
63 pre += (MUL16(are, bre) - MUL16(aim, bim)); \
64 pim += (MUL16(are, bim) + MUL16(bre, aim)); \
65 }
66
67 #if FFT_FLOAT || AVFFT
68 #define RANGE 1.0
69 #define REF_SCALE(x, bits) (x)
70 #define FMT "%10.6f"
71 #elif FFT_FIXED_32
72 #define RANGE 8388608
73 #define REF_SCALE(x, bits) (x)
74 #define FMT "%6d"
75 #else
77 #define REF_SCALE(x, bits) ((x) / (1 << (bits)))
79 #endif
80
81 static struct {
84
86 {
87 int i,
n = 1 << nbits;
88
92
93 for (i = 0; i < (n / 2); i++) {
94 double alpha = 2 *
M_PI * (float) i / (
float)
n;
95 double c1 = cos(alpha),
s1 = sin(alpha);
96 if (!inverse)
100 }
101 return 0;
102 }
103
105 {
106 int i, j;
108 int n2 = n >> 1;
109
110 for (i = 0; i <
n; i++) {
111 double tmp_re = 0, tmp_im = 0;
113 for (j = 0; j <
n; j++) {
115 int k = (i * j) & (n - 1);
116 if (k >= n2) {
119 } else {
122 }
123 CMAC(tmp_re, tmp_im, c, s, q->
re, q->
im);
124 q++;
125 }
128 }
129 }
130
131 #if CONFIG_MDCT
133 {
134 int i, k,
n = 1 << nbits;
135
136 for (i = 0; i <
n; i++) {
137 double sum = 0;
138 for (k = 0; k < n / 2; k++) {
139 int a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
140 double f = cos(
M_PI * a / (
double) (2 * n));
141 sum += f * in[k];
142 }
144 }
145 }
146
147 /* NOTE: no normalisation by 1 / N is done */
149 {
150 int i, k, n = 1 << nbits;
151
152 /* do it by hand */
153 for (k = 0; k < n / 2; k++) {
155 for (i = 0; i <
n; i++) {
156 double a = (2 *
M_PI * (2 * i + 1 + n / 2) * (2 * k + 1) / (4 *
n));
157 s += input[i] * cos(a);
158 }
160 }
161 }
162 #endif /* CONFIG_MDCT */
163
164 #if FFT_FLOAT
165 #if CONFIG_DCT
167 {
168 int i, k, n = 1 << nbits;
169
170 /* do it by hand */
171 for (i = 0; i <
n; i++) {
172 double s = 0.5 * input[0];
173 for (k = 1; k <
n; k++) {
174 double a =
M_PI * k * (i + 0.5) /
n;
175 s += input[k] * cos(
a);
176 }
177 output[i] = 2 * s /
n;
178 }
179 }
180
182 {
183 int i, k, n = 1 << nbits;
184
185 /* do it by hand */
186 for (k = 0; k <
n; k++) {
188 for (i = 0; i <
n; i++) {
189 double a =
M_PI * k * (i + 0.5) /
n;
190 s += input[i] * cos(
a);
191 }
193 }
194 }
195 #endif /* CONFIG_DCT */
196 #endif /* FFT_FLOAT */
197
199 {
201 }
202
204 {
205 int i, err = 0;
206 double error = 0, max = 0;
207
208 for (i = 0; i <
n; i++) {
209 double e = fabs(tab1[i] - (tab2[i] / scale)) /
RANGE;
210 if (e >= 1e-3) {
212 i, tab1[i], tab2[i]);
213 err = 1;
214 }
215 error += e * e;
216 if (e > max)
217 max = e;
218 }
220 return err;
221 }
222
224 {
225 #if AVFFT
227 #else
229 #endif
230 }
231
233 {
234 #if AVFFT
236 #else
238 #endif
239 }
240
242 {
243 #if AVFFT
245 #else
247 #endif
248 }
249
251 {
252 #if AVFFT
254 #else
256 #endif
257 }
258
260 {
261 #if AVFFT
263 #else
265 #endif
266 }
267
269 {
270 #if AVFFT
272 #else
274 #endif
275 }
276
278 {
279 #if AVFFT
281 #else
283 #endif
284 }
285
287 {
288 #if AVFFT
290 #else
292 #endif
293 }
294
295 #if FFT_FLOAT
297 {
298 #if AVFFT
300 #else
302 #endif
303 }
304
306 {
307 #if AVFFT
309 #else
311 #endif
312 }
313
315 {
316 #if AVFFT
318 #else
320 #endif
321 }
322
324 {
325 #if AVFFT
327 #else
329 #endif
330 }
331
333 {
334 #if AVFFT
336 #else
338 #endif
339 }
340
342 {
343 #if AVFFT
345 #else
347 #endif
348 }
349 #endif /* FFT_FLOAT */
350
352 {
354 "usage: fft-test [-h] [-s] [-i] [-n b]\n"
355 "-h print this help\n"
356 "-s speed test\n"
357 "-m (I)MDCT test\n"
358 "-d (I)DCT test\n"
359 "-r (I)RDFT test\n"
360 "-i inverse transform test\n"
361 "-n b set the transform size to 2^b\n"
362 "-f x set scale factor for output data of (I)MDCT to x\n");
363 }
364
370 };
371
372 #if !HAVE_GETOPT
374 #endif
375
376 int main(
int argc,
char **argv)
377 {
382 #if FFT_FLOAT
385 #endif /* FFT_FLOAT */
386 int it, i, err = 1;
387 int do_speed = 0, do_inverse = 0;
388 int fft_nbits = 9, fft_size;
389 double scale = 1.0;
391
392 #if !AVFFT
395 #endif
396
397 #if !AVFFT && FFT_FLOAT
400 #endif
401
403
404 for (;;) {
405 int c =
getopt(argc, argv,
"hsimrdn:f:c:");
406 if (c == -1)
407 break;
408 switch (c) {
409 case 'h':
411 return 1;
412 case 's':
413 do_speed = 1;
414 break;
415 case 'i':
416 do_inverse = 1;
417 break;
418 case 'm':
420 break;
421 case 'r':
423 break;
424 case 'd':
426 break;
427 case 'n':
429 break;
430 case 'f':
432 break;
433 case 'c':
434 {
436
438 return 1;
439
441 break;
442 }
443 }
444 }
445
446 fft_size = 1 << fft_nbits;
451
452 if (!(tab && tab1 && tab_ref && tab2))
454
455 switch (transform) {
456 #if CONFIG_MDCT
459 if (do_inverse)
461 else
463 mdct_init(&m, fft_nbits, do_inverse, scale);
464 break;
465 #endif /* CONFIG_MDCT */
467 if (do_inverse)
469 else
471 fft_init(&s, fft_nbits, do_inverse);
474 break;
475 #if FFT_FLOAT
476 # if CONFIG_RDFT
478 if (do_inverse)
480 else
485 break;
486 # endif /* CONFIG_RDFT */
487 # if CONFIG_DCT
489 if (do_inverse)
491 else
494 break;
495 # endif /* CONFIG_DCT */
496 #endif /* FFT_FLOAT */
497 default:
500 }
502
503 /* generate random data */
504
505 for (i = 0; i < fft_size; i++) {
508 }
509
510 /* checking result */
512
513 switch (transform) {
514 #if CONFIG_MDCT
516 if (do_inverse) {
517 imdct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
520 } else {
521 mdct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
523 err =
check_diff(&tab_ref->
re, tab2, fft_size / 2, scale);
524 }
525 break;
526 #endif /* CONFIG_MDCT */
528 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
531
532 fft_ref(tab_ref, tab1, fft_nbits);
534 break;
535 #if FFT_FLOAT
536 #if CONFIG_RDFT
538 {
539 int fft_size_2 = fft_size >> 1;
540 if (do_inverse) {
542 tab1[fft_size_2].
im = 0;
543 for (i = 1; i < fft_size_2; i++) {
544 tab1[fft_size_2 + i].
re = tab1[fft_size_2 - i].
re;
545 tab1[fft_size_2 + i].
im = -tab1[fft_size_2 - i].
im;
546 }
547
548 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
549 tab2[1] = tab1[fft_size_2].
re;
550
551 rdft_calc(r, tab2);
552 fft_ref(tab_ref, tab1, fft_nbits);
553 for (i = 0; i < fft_size; i++) {
556 }
558 } else {
559 for (i = 0; i < fft_size; i++) {
560 tab2[i] = tab1[i].
re;
562 }
563 rdft_calc(r, tab2);
564 fft_ref(tab_ref, tab1, fft_nbits);
565 tab_ref[0].
im = tab_ref[fft_size_2].
re;
567 }
568 break;
569 }
570 #endif /* CONFIG_RDFT */
571 #if CONFIG_DCT
573 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
574 dct_calc(d, &tab->
re);
575 if (do_inverse)
576 idct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
577 else
578 dct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
580 break;
581 #endif /* CONFIG_DCT */
582 #endif /* FFT_FLOAT */
583 }
584
585 /* do a speed test */
586
587 if (do_speed) {
589 int nb_its;
590
592 /* we measure during about 1 seconds */
593 nb_its = 1;
594 for (;;) {
596 for (it = 0; it < nb_its; it++) {
597 switch (transform) {
599 if (do_inverse)
601 else
603 break;
605 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
607 break;
608 #if FFT_FLOAT
610 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
611 rdft_calc(r, tab2);
612 break;
614 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
615 dct_calc(d, tab2);
616 break;
617 #endif /* FFT_FLOAT */
618 }
619 }
621 if (duration >= 1000000)
622 break;
623 nb_its *= 2;
624 }
626 "time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
627 (double) duration / nb_its,
628 (double) duration / 1000000.0,
629 nb_its);
630 }
631
632 switch (transform) {
633 #if CONFIG_MDCT
636 break;
637 #endif /* CONFIG_MDCT */
640 break;
641 #if FFT_FLOAT
642 # if CONFIG_RDFT
644 rdft_end(r);
645 break;
646 # endif /* CONFIG_RDFT */
647 # if CONFIG_DCT
649 dct_end(d);
650 break;
651 # endif /* CONFIG_DCT */
652 #endif /* FFT_FLOAT */
653 }
654
661
662 #if !AVFFT
665 #endif
666
667 #if !AVFFT && FFT_FLOAT
670 #endif
671
672 if (err)
673 printf("Error: %d.\n", err);
674
675 return !!err;
676 }
av_cold void ff_rdft_end(RDFTContext *s)
static float alpha(float a)
void(* dct_calc)(struct DCTContext *s, FFTSample *data)
ptrdiff_t const GLvoid * data
av_cold void av_fft_end(FFTContext *s)
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
void av_mdct_end(FFTContext *s)
FFTContext * av_mdct_init(int nbits, int inverse, double scale)
DCTContext * av_dct_init(int nbits, enum DCTTransformType type)
Set up DCT.
void(* fft_permute)(struct FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling fft_calc().
void av_fft_permute(FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling ff_fft_calc().
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static void imdct_calc(struct FFTContext *s, FFTSample *output, const FFTSample *input)
static FFTSample frandom(AVLFG *prng)
static void fft_permute(FFTContext *s, FFTComplex *z)
static int fft_ref_init(int nbits, int inverse)
#define CMAC(pre, pim, are, aim, bre, bim)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
FFTContext * av_fft_init(int nbits, int inverse)
Set up a complex FFT.
int av_parse_cpu_caps(unsigned *flags, const char *s)
Parse CPU caps from a string and update the given AV_CPU_* flags based on that.
void(* rdft_calc)(struct RDFTContext *s, FFTSample *z)
void av_rdft_calc(RDFTContext *s, FFTSample *data)
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
#define REF_SCALE(x, bits)
void av_rdft_end(RDFTContext *s)
RDFTContext * av_rdft_init(int nbits, enum RDFTransformType trans)
Set up a real FFT.
static void mdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input)
static void error(const char *err)
static void fft_end(FFTContext *s)
static const int8_t transform[32][32]
#define AV_LOG_INFO
Standard information.
void av_imdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input)
static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static int getopt(int argc, char *argv[], char *opts)
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
void av_dct_end(DCTContext *s)
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
Set up DCT.
static void fft_calc(FFTContext *s, FFTComplex *z)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
static av_cold int dct_init(MpegEncContext *s)
static void mdct_end(FFTContext *s)
void(* fft_calc)(struct FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in ff_fft_init().
static void mdct_init(FFTContext **s, int nbits, int inverse, double scale)
void av_dct_calc(DCTContext *s, FFTSample *data)
int main(int argc, char **argv)
av_cold void ff_dct_end(DCTContext *s)
static const struct twinvq_data tab
void av_mdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input)
static uint32_t inverse(uint32_t v)
find multiplicative inverse modulo 2 ^ 32
#define av_malloc_array(a, b)
void av_force_cpu_flags(int arg)
Disables cpu detection and forces the specified flags.
static void fft_init(FFTContext **s, int nbits, int inverse)
av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans)
Set up a real FFT.
void av_fft_calc(FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in av_fft_init().
static struct @125 * exptab
static av_cold void cleanup(FlashSV2Context *s)