26 #include "libavutil/cpu.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/lfg.h"
29 #include "libavutil/log.h"
30 #include "libavutil/time.h"
46 #define MUL16(a,b) ((a) * (b))
48 #define CMAC(pre, pim, are, aim, bre, bim) \
50 pre += (MUL16(are, bre) - MUL16(aim, bim));\
51 pim += (MUL16(are, bim) + MUL16(bre, aim));\
56 # define REF_SCALE(x, bits) (x)
58 #elif CONFIG_FFT_FIXED_32
59 # define RANGE 8388608
60 # define REF_SCALE(x, bits) (x)
64 # define REF_SCALE(x, bits) ((x) / (1<<(bits)))
80 for (i = 0; i < (n/2); i++) {
81 alpha = 2 *
M_PI * (float)i / (
float)
n;
94 double tmp_re, tmp_im,
s,
c;
99 for (i = 0; i <
n; i++) {
103 for (j = 0; j <
n; j++) {
104 k = (i * j) & (n - 1);
112 CMAC(tmp_re, tmp_im, c, s, q->
re, q->
im);
126 for (i = 0; i <
n; i++) {
128 for (k = 0; k < n/2; k++) {
129 a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
130 f = cos(
M_PI * a / (
double)(2 * n));
145 for (k = 0; k < n/2; k++) {
147 for (i = 0; i <
n; i++) {
148 a = (2*
M_PI*(2*i+1+n/2)*(2*k+1) / (4 *
n));
149 s += input[i] * cos(a);
163 for (i = 0; i <
n; i++) {
165 for (k = 1; k <
n; k++) {
166 a =
M_PI*k*(i+0.5) /
n;
167 s += input[k] * cos(a);
169 output[i] = 2 * s /
n;
179 for (k = 0; k <
n; k++) {
181 for (i = 0; i <
n; i++) {
182 a =
M_PI*k*(i+0.5) /
n;
183 s += input[i] * cos(a);
203 for (i = 0; i <
n; i++) {
204 double e = fabsf(tab1[i] - (tab2[i] / scale)) /
RANGE;
207 i, tab1[i], tab2[i]);
221 "-h print this help\n"
226 "-i inverse transform test\n"
227 "-n b set the transform size to 2^b\n"
228 "-f x set scale factor for output data of (I)MDCT to x\n"
243 int main(
int argc,
char **argv)
260 int fft_nbits, fft_size;
267 c =
getopt(argc, argv,
"hsimrdn:f:c:");
306 fft_size = 1 << fft_nbits;
356 for (i = 0; i < fft_size; i++) {
379 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
383 fft_ref(tab_ref, tab1, fft_nbits);
388 fft_size_2 = fft_size >> 1;
391 tab1[fft_size_2].
im = 0;
392 for (i = 1; i < fft_size_2; i++) {
393 tab1[fft_size_2+i].
re = tab1[fft_size_2-i].
re;
394 tab1[fft_size_2+i].
im = -tab1[fft_size_2-i].
im;
397 memcpy(tab2, tab1, fft_size *
sizeof(
FFTSample));
398 tab2[1] = tab1[fft_size_2].
re;
401 fft_ref(tab_ref, tab1, fft_nbits);
402 for (i = 0; i < fft_size; i++) {
406 err =
check_diff((
float *)tab_ref, (
float *)tab, fft_size * 2, 0.5);
408 for (i = 0; i < fft_size; i++) {
409 tab2[i] = tab1[i].
re;
413 fft_ref(tab_ref, tab1, fft_nbits);
414 tab_ref[0].
im = tab_ref[fft_size_2].
re;
415 err =
check_diff((
float *)tab_ref, (
float *)tab2, fft_size, 1.0);
419 memcpy(tab, tab1, fft_size *
sizeof(
FFTComplex));
426 err =
check_diff((
float *)tab_ref, (
float *)tab, fft_size, 1.0);
442 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)
472 av_log(NULL,
AV_LOG_INFO,
"time: %0.1f us/transform [total time=%0.2f s its=%d]\n",
473 (
double)duration / nb_its,
474 (
double)duration / 1000000.0,
504 printf(
"Error: %d.\n", err);
av_cold void ff_rdft_end(RDFTContext *s)
void(* dct_calc)(struct DCTContext *s, FFTSample *data)
void(* mdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
void(* fft_permute)(struct FFTContext *s, FFTComplex *z)
Do the permutation needed BEFORE calling fft_calc().
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
static void idct_ref(FFTSample *output, FFTSample *input, int nbits)
static double alpha(void *priv, double x, double y)
#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(). ...
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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
static FFTSample frandom(AVLFG *prng)
static void dct_ref(FFTSample *output, FFTSample *input, int nbits)
void(* rdft_calc)(struct RDFTContext *s, FFTSample *z)
void(* imdct_calc)(struct FFTContext *s, FFTSample *output, const FFTSample *input)
int64_t av_gettime(void)
Get the current time in microseconds.
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.
static void fft_ref_init(int nbits, int inverse)
#define CMAC(pre, pim, are, aim, bre, bim)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define REF_SCALE(x, bits)
static const int8_t transform[32][32]
#define AV_LOG_INFO
Standard information.
void av_lfg_init(AVLFG *c, unsigned int seed)
static int getopt(int argc, char *argv[], char *opts)
av_cold int ff_dct_init(DCTContext *s, int nbits, enum DCTTransformType inverse)
Set up DCT.
static const uint16_t scale[4]
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)
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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 av_force_cpu_flags(int flags)
Disables cpu detection and forces the specified flags.
av_cold void ff_dct_end(DCTContext *s)
static struct twinvq_data tab
static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
static uint32_t inverse(uint32_t v)
find multiplicative inverse modulo 2 ^ 32
int main(int argc, char **argv)
av_cold int ff_rdft_init(RDFTContext *s, int nbits, enum RDFTransformType trans)
Set up a real FFT.