33 double sum = coeff[size-1];
35 for (i = size-2; i >= 0; --i) {
76 static const double p1[] = {
77 -2.2335582639474375249e+15,
78 -5.5050369673018427753e+14,
79 -3.2940087627407749166e+13,
80 -8.4925101247114157499e+11,
81 -1.1912746104985237192e+10,
82 -1.0313066708737980747e+08,
83 -5.9545626019847898221e+05,
84 -2.4125195876041896775e+03,
85 -7.0935347449210549190e+00,
86 -1.5453977791786851041e-02,
87 -2.5172644670688975051e-05,
88 -3.0517226450451067446e-08,
89 -2.6843448573468483278e-11,
90 -1.5982226675653184646e-14,
91 -5.2487866627945699800e-18,
93 static const double q1[] = {
94 -2.2335582639474375245e+15,
95 7.8858692566751002988e+12,
96 -1.2207067397808979846e+10,
97 1.0377081058062166144e+07,
98 -4.8527560179962773045e+03,
101 static const double p2[] = {
102 -2.2210262233306573296e-04,
103 1.3067392038106924055e-02,
104 -4.4700805721174453923e-01,
105 5.5674518371240761397e+00,
106 -2.3517945679239481621e+01,
107 3.1611322818701131207e+01,
108 -9.6090021968656180000e+00,
110 static const double q2[] = {
111 -5.5194330231005480228e-04,
112 3.2547697594819615062e-02,
113 -1.1151759188741312645e+00,
114 1.3982595353892851542e+01,
115 -6.0228002066743340583e+01,
116 8.5539563258012929600e+01,
117 -3.1446690275135491500e+01,
129 y = 1 / x - 1.0 / 15;
131 factor =
exp(x) / sqrt(x);
145 int filter_type,
double kaiser_beta){
147 int ph_nb = phase_count % 2 ? phase_count : phase_count / 2 + 1;
148 double x, y, w, t,
s;
151 const int center= (tap_count-1)/2;
155 if (!tab || !sin_lut)
158 av_assert0(tap_count == 1 || tap_count % 2 == 0);
165 for (ph = 0; ph < ph_nb; ph++)
166 sin_lut[ph] = sin(
M_PI * ph / phase_count) * (center & 1 ? 1 : -1);
168 for(ph = 0; ph < ph_nb; ph++) {
170 for(i=0;i<tap_count;i++) {
171 x =
M_PI * ((double)(i - center) - (double)ph / phase_count) *
factor;
173 else if (factor == 1.0)
180 x = fabs(((
double)(i - center) - (
double)ph / phase_count) * factor);
181 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
182 else y= d*(-4 + 8*x - 5*x*x + x*x*x);
185 w = 2.0*x / (factor*tap_count);
187 y *= 0.3635819 - 0.4891775 * t + 0.1365995 * (2*t*t-1) - 0.0106411 * (4*t*t*t - 3*t);
190 w = 2.0*x / (factor*tap_count*
M_PI);
206 for(i=0;i<tap_count;i++)
207 ((int16_t*)filter)[ph * alloc + i] = av_clip_int16(
lrintf(tab[i] * scale / norm));
208 if (phase_count % 2)
break;
209 for (i = 0; i < tap_count; i++)
210 ((int16_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int16_t*)
filter)[ph * alloc + i];
213 for(i=0;i<tap_count;i++)
215 if (phase_count % 2)
break;
216 for (i = 0; i < tap_count; i++)
217 ((
int32_t*)
filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
int32_t*)filter)[ph * alloc + i];
220 for(i=0;i<tap_count;i++)
221 ((
float*)
filter)[ph * alloc + i] = tab[i] * scale / norm;
222 if (phase_count % 2)
break;
223 for (i = 0; i < tap_count; i++)
224 ((
float*)
filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
float*)filter)[ph * alloc + i];
227 for(i=0;i<tap_count;i++)
228 ((
double*)
filter)[ph * alloc + i] = tab[i] * scale / norm;
229 if (phase_count % 2)
break;
230 for (i = 0; i < tap_count; i++)
231 ((
double*)
filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
double*)filter)[ph * alloc + i];
239 double sine[
LEN + tap_count];
240 double filtered[
LEN];
241 double maxff=-2, minff=2, maxsf=-2, minsf=2;
242 for(i=0; i<
LEN; i++){
243 double ss=0, sf=0, ff=0;
244 for(j=0; j<LEN+tap_count; j++)
245 sine[j]= cos(i*j*
M_PI/LEN);
246 for(j=0; j<
LEN; j++){
249 for(k=0; k<tap_count; k++)
250 sum += filter[ph * tap_count + k] * sine[k+j];
252 ss+= sine[j + center] * sine[j + center];
253 ff+= filtered[j] * filtered[j];
254 sf+= sine[j + center] * filtered[j];
259 maxff=
FFMAX(maxff, ff);
260 minff=
FFMIN(minff, ff);
261 maxsf=
FFMAX(maxsf, sf);
262 minsf=
FFMIN(minsf, sf);
264 av_log(
NULL,
AV_LOG_ERROR,
"i:%4d ss:%f ff:%13.6e-%13.6e sf:%13.6e-%13.6e\n", i, ss, maxff, minff, maxsf, minsf);
289 double precision,
int cheby,
int exact_rational)
291 double cutoff = cutoff0? cutoff0 : 0.97;
292 double factor=
FFMIN(out_rate * cutoff / in_rate, 1.0);
294 int phase_count_compensation = phase_count;
297 if (filter_length > 1)
298 filter_length =
FFALIGN(filter_length, 2);
300 if (exact_rational) {
301 int phase_count_exact, phase_count_exact_den;
303 av_reduce(&phase_count_exact, &phase_count_exact_den, out_rate, in_rate, INT_MAX);
304 if (phase_count_exact <= phase_count) {
305 phase_count_compensation = phase_count_exact * (phase_count / phase_count_exact);
306 phase_count = phase_count_exact;
338 if (filter_size/factor > INT32_MAX/256) {
386 int new_src_incr, new_dst_incr;
396 if (!new_filter_bank)
434 if (compensation_distance && sample_delta) {
441 if (compensation_distance)
461 src_size =
FFMIN(src_size, max_src_size);
470 dst_size =
FFMAX(
FFMIN(dst_size, new_size), 0);
472 for (i = 0; i < dst->
ch_count; i++) {
478 *consumed = c->
index;
489 const void *
src,
int n,
int update_ctx);
491 dst_size =
FFMAX(
FFMIN(dst_size, delta_n), 0);
497 for (i = 0; i < dst->ch_count; i++)
498 *consumed = resample_func(c, dst->ch[i], src->ch[i], dst_size, i+1 == dst->ch_count);
556 for(j=0; j<reflection; j++){
567 int in_count,
int *out_idx,
int *out_sz)
578 for (n = *out_sz; n < num; n++) {
579 for (ch = 0; ch < src->
ch_count; ch++) {
594 for (ch = 0; ch < src->
ch_count; ch++) {
603 while (c->
index < 0) {
610 return FFMAX(res, 0);
static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed)
static int rebuild_filter_bank_with_compensation(ResampleContext *c)
int out_sample_rate
output sample rate
Audio buffer used for intermediate storage between conversion phases.
static int linear(InterplayACMContext *s, unsigned ind, unsigned col)
int ch_count
number of channels
SwrFilterType
Resampling Filter Types.
#define AV_CPU_FLAG_MMX2
SSE integer functions or AMD MMX ext.
int in_buffer_index
cached buffer position
AudioData in_buffer
cached audio data (convert and resample purpose)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
struct ResampleContext * resample
resampling context
static const uint8_t q1[256]
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
struct ResampleContext::@256 dsp
#define av_assert0(cond)
assert() equivalent, that is always enabled.
enum AVSampleFormat format
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
static double bessel(double x)
0th order modified bessel function of the first kind.
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
int swri_realloc_audio(AudioData *a, int count)
int compensation_distance
static int set_compensation(ResampleContext *c, int sample_delta, int compensation_distance)
enum AVResampleFilterType filter_type
struct Resampler const swri_resampler
static int invert_initial_buffer(ResampleContext *c, AudioData *dst, const AudioData *src, int in_count, int *out_idx, int *out_sz)
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
static double eval_poly(const double *coeff, int size, double x)
int in_buffer_count
cached buffer length
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Blackman Nuttall windowed sinc.
static int resample_flush(struct SwrContext *s)
The libswresample context.
simple assert() macros that are a bit more flexible than ISO C assert().
static ResampleContext * resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff0, enum AVSampleFormat format, enum SwrFilterType filter_type, double kaiser_beta, double precision, int cheby, int exact_rational)
int(* resample_linear)(struct ResampleContext *c, void *dst, const void *src, int n, int update_ctx)
int compensation_distance
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
static int64_t get_delay(struct SwrContext *s, int64_t base)
int in_sample_rate
input sample rate
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
static void error(const char *err)
#define FF_ARRAY_ELEMS(a)
static void resample_free(ResampleContext **cc)
AVSampleFormat
Audio sample formats.
int(* resample_common)(struct ResampleContext *c, void *dst, const void *src, int n, int update_ctx)
static const char * format
void swri_resample_dsp_init(ResampleContext *c)
static const int factor[16]
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
static int64_t get_out_samples(struct SwrContext *s, int in_samples)
void(* resample_one)(struct ResampleContext *c, void *dst0, int dst_index, const void *src0, unsigned int index, int frac)
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale, int filter_type, double kaiser_beta)
builds a polyphase filterbank.
int phase_count_compensation
static const double coeff[2][5]
static const struct twinvq_data tab
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
int planar
1 if planar audio, 0 otherwise
#define av_malloc_array(a, b)
uint8_t * ch[SWR_CH_MAX]
samples buffer per channel