50 #define MUL16(a, b) ((a) * (b))
52 #define CMAC(pre, pim, are, aim, bre, bim) \
54 pre += (MUL16(are, bre) - MUL16(aim, bim)); \
55 pim += (MUL16(are, bim) + MUL16(bre, aim)); \
60 #define REF_SCALE(x, bits) (x)
64 #define REF_SCALE(x, bits) ((x) / (1 << (bits)))
74 int i, n = 1 << nbits;
80 for (i = 0; i < (n / 2); i++) {
81 double alpha = 2 * M_PI * (float) i / (
float) n;
82 double c1 = cos(alpha), s1 = sin(alpha);
97 for (i = 0; i < n; i++) {
98 double tmp_re = 0, tmp_im = 0;
100 for (j = 0; j < n; j++) {
102 int k = (i * j) & (n - 1);
110 CMAC(tmp_re, tmp_im, c, s, q->
re, q->
im);
121 int i, k, n = 1 << nbits;
123 for (i = 0; i < n; i++) {
125 for (k = 0; k < n / 2; k++) {
126 int a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
127 double f = cos(M_PI * a / (
double) (2 * n));
137 int i, k, n = 1 << nbits;
140 for (k = 0; k < n / 2; k++) {
142 for (i = 0; i < n; i++) {
143 double a = (2 * M_PI * (2 * i + 1 + n / 2) * (2 * k + 1) / (4 * n));
144 s += input[i] * cos(a);
153 static void idct_ref(
float *output,
float *input,
int nbits)
155 int i, k, n = 1 << nbits;
158 for (i = 0; i < n; i++) {
159 double s = 0.5 * input[0];
160 for (k = 1; k < n; k++) {
161 double a = M_PI * k * (i + 0.5) / n;
162 s += input[k] * cos(
a);
164 output[i] = 2 * s / n;
168 static void dct_ref(
float *output,
float *input,
int nbits)
170 int i, k, n = 1 << nbits;
173 for (k = 0; k < n; k++) {
175 for (i = 0; i < n; i++) {
176 double a = M_PI * k * (i + 0.5) / n;
177 s += input[i] * cos(
a);
193 double error = 0, max = 0;
195 for (i = 0; i < n; i++) {
196 double e = fabsf(tab1[i] - (tab2[i] / scale)) /
RANGE;
199 i, tab1[i], tab2[i]);
213 "usage: fft-test [-h] [-s] [-i] [-n b]\n"
214 "-h print this help\n"
219 "-i inverse transform test\n"
220 "-n b set the transform size to 2^b\n"
221 "-f x set scale factor for output data of (I)MDCT to x\n");
235 int main(
int argc,
char **argv)
246 int do_speed = 0, do_inverse = 0;
247 int fft_nbits = 9, fft_size;
254 int c =
getopt(argc, argv,
"hsimrdn:f:c:");
293 fft_size = 1 << fft_nbits;
299 if (!(tab && tab1 && tab_ref && tab2))
352 for (i = 0; i < fft_size; i++) {
364 imdct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
368 mdct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
370 err =
check_diff(&tab_ref->
re, tab2, fft_size / 2, scale);
375 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
379 fft_ref(tab_ref, tab1, fft_nbits);
386 int fft_size_2 = fft_size >> 1;
389 tab1[fft_size_2].
im = 0;
390 for (i = 1; i < fft_size_2; i++) {
391 tab1[fft_size_2 + i].
re = tab1[fft_size_2 - i].
re;
392 tab1[fft_size_2 + i].
im = -tab1[fft_size_2 - i].
im;
395 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
396 tab2[1] = tab1[fft_size_2].
re;
399 fft_ref(tab_ref, tab1, fft_nbits);
400 for (i = 0; i < fft_size; i++) {
406 for (i = 0; i < fft_size; i++) {
407 tab2[i] = tab1[i].
re;
411 fft_ref(tab_ref, tab1, fft_nbits);
412 tab_ref[0].
im = tab_ref[fft_size_2].
re;
420 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
423 idct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
425 dct_ref(&tab_ref->
re, &tab1->
re, fft_nbits);
443 for (it = 0; it < nb_its; it++) {
452 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
457 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
461 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
468 if (duration >= 1000000)
473 "time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
474 (
double) duration / nb_its,
475 (
double) duration / 1000000.0,
510 printf(
"Error: %d.\n", err);
av_cold void ff_rdft_end(RDFTContext *s)
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
void(* dct_calc)(struct DCTContext *s, FFTSample *data)
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
static struct @16 * exptab
void av_set_cpu_flags_mask(int mask)
Set a mask on flags returned by av_get_cpu_flags().
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_dlog(ac->avr,"%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
void(* fft_permute)(struct FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling fft_calc().
static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
int av_parse_cpu_flags(const char *s)
Parse CPU flags from a string.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
static FFTSample frandom(AVLFG *prng)
void av_log(void *avcl, int level, const char *fmt,...)
void(* rdft_calc)(struct RDFTContext *s, FFTSample *z)
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
#define CMAC(pre, pim, are, aim, bre, bim)
int64_t av_gettime(void)
Get the current time in microseconds.
#define REF_SCALE(x, bits)
static const int8_t transform[32][32]
#define AV_LOG_INFO
Standard information.
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.
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
Set up DCT.
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
int main(int argc, char **argv)
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_dlog(ac->avr,"%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-> out
void(* fft_calc)(struct FFTContext *s, FFTComplex *z)
Do a complex FFT with the parameters defined in ff_fft_init().
static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
av_cold void ff_dct_end(DCTContext *s)
static const struct twinvq_data tab
static int fft_ref_init(int nbits, int inverse)
static uint32_t inverse(uint32_t v)
find multiplicative inverse modulo 2 ^ 32
av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans)
Set up a real FFT.