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;
154 if (!tab || !sin_lut)
162 for (ph = 0; ph < ph_nb; ph++)
163 sin_lut[ph] = sin(
M_PI * ph / phase_count);
165 for(ph = 0; ph < ph_nb; ph++) {
168 for(i=0;i<=tap_count;i++) {
169 x =
M_PI * ((double)(i - center) - (double)ph / phase_count) *
factor;
171 else if (factor == 1.0)
178 x = fabs(((
double)(i - center) - (
double)ph / phase_count) * factor);
179 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
180 else y= d*(-4 + 8*x - 5*x*x + x*x*x);
183 w = 2.0*x / (factor*tap_count);
185 y *= 0.3635819 - 0.4891775 * t + 0.1365995 * (2*t*t-1) - 0.0106411 * (4*t*t*t - 3*t);
188 w = 2.0*x / (factor*tap_count*
M_PI);
204 for(i=0;i<tap_count;i++)
205 ((int16_t*)filter)[ph * alloc + i] = av_clip_int16(
lrintf(tab[i] * scale / norm));
206 if (phase_count % 2)
break;
207 if (tap_count % 2 == 0 || tap_count == 1) {
208 for (i = 0; i < tap_count; i++)
209 ((int16_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int16_t*)
filter)[ph * alloc + i];
212 for (i = 1; i <= tap_count; i++)
213 ((int16_t*)
filter)[(phase_count-ph) * alloc + tap_count-i] =
214 av_clip_int16(
lrintf(tab[i] * scale / (norm - tab[0] + tab[tap_count])));
218 for(i=0;i<tap_count;i++)
219 ((
int32_t*)filter)[ph * alloc + i] = av_clipl_int32(
llrint(tab[i] * scale / norm));
220 if (phase_count % 2)
break;
221 if (tap_count % 2 == 0 || tap_count == 1) {
222 for (i = 0; i < tap_count; i++)
223 ((
int32_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
int32_t*)
filter)[ph * alloc + i];
226 for (i = 1; i <= tap_count; i++)
228 av_clipl_int32(
llrint(tab[i] * scale / (norm - tab[0] + tab[tap_count])));
232 for(i=0;i<tap_count;i++)
233 ((
float*)filter)[ph * alloc + i] = tab[i] * scale / norm;
234 if (phase_count % 2)
break;
235 if (tap_count % 2 == 0 || tap_count == 1) {
236 for (i = 0; i < tap_count; i++)
237 ((
float*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
float*)
filter)[ph * alloc + i];
240 for (i = 1; i <= tap_count; i++)
241 ((
float*)
filter)[(phase_count-ph) * alloc + tap_count-i] = tab[i] * scale / (norm - tab[0] + tab[tap_count]);
245 for(i=0;i<tap_count;i++)
246 ((
double*)
filter)[ph * alloc + i] = tab[i] * scale / norm;
247 if (phase_count % 2)
break;
248 if (tap_count % 2 == 0 || tap_count == 1) {
249 for (i = 0; i < tap_count; i++)
250 ((
double*)
filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
double*)filter)[ph * alloc + i];
253 for (i = 1; i <= tap_count; i++)
254 ((
double*)
filter)[(phase_count-ph) * alloc + tap_count-i] = tab[i] * scale / (norm - tab[0] + tab[tap_count]);
263 double sine[
LEN + tap_count];
264 double filtered[
LEN];
265 double maxff=-2, minff=2, maxsf=-2, minsf=2;
266 for(i=0; i<
LEN; i++){
267 double ss=0, sf=0, ff=0;
268 for(j=0; j<LEN+tap_count; j++)
269 sine[j]= cos(i*j*
M_PI/LEN);
270 for(j=0; j<
LEN; j++){
273 for(k=0; k<tap_count; k++)
274 sum += filter[ph * tap_count + k] * sine[k+j];
276 ss+= sine[j + center] * sine[j + center];
277 ff+= filtered[j] * filtered[j];
278 sf+= sine[j + center] * filtered[j];
283 maxff=
FFMAX(maxff, ff);
284 minff=
FFMIN(minff, ff);
285 maxsf=
FFMAX(maxsf, sf);
286 minsf=
FFMIN(minsf, sf);
288 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);
305 double precision,
int cheby,
int exact_rational)
307 double cutoff = cutoff0? cutoff0 : 0.97;
308 double factor=
FFMIN(out_rate * cutoff / in_rate, 1.0);
310 int phase_count_compensation = phase_count;
312 if (exact_rational) {
313 int phase_count_exact, phase_count_exact_den;
315 av_reduce(&phase_count_exact, &phase_count_exact_den, out_rate, in_rate, INT_MAX);
316 if (phase_count_exact <= phase_count) {
317 phase_count_compensation = phase_count_exact * (phase_count / phase_count_exact);
318 phase_count = phase_count_exact;
349 if (filter_size/factor > INT32_MAX/256) {
404 int new_src_incr, new_dst_incr;
414 if (!new_filter_bank)
452 if (compensation_distance) {
459 if (compensation_distance)
472 int src_size,
int dst_size,
int update_ctx)
481 dst_size=
FFMIN(dst_size, new_size);
497 dst_size =
FFMIN(dst_size, delta_n);
499 *consumed = c->
dsp.
resample(c, dst, src, dst_size, update_ctx);
517 src_size =
FFMIN(src_size, max_src_size);
521 consumed, src_size, dst_size, i+1==dst->
ch_count);
585 int in_count,
int *out_idx,
int *out_sz)
596 for (n = *out_sz; n < num; n++) {
597 for (ch = 0; ch < src->
ch_count; ch++) {
612 for (ch = 0; ch < src->
ch_count; ch++) {
621 while (c->
index < 0) {
628 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]
struct ResampleContext::@243 dsp
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
#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.
static int swri_resample(ResampleContext *c, uint8_t *dst, const uint8_t *src, int *consumed, int src_size, int dst_size, int update_ctx)
static void resample_free(ResampleContext **c)
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 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.
#define FF_ARRAY_ELEMS(a)
AVSampleFormat
Audio sample formats.
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.
int(* resample)(struct ResampleContext *c, void *dst, const void *src, int n, int update_ctx)
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
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_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MT_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_YASM &&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 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