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;
153 if (!tab || !sin_lut)
161 for (ph = 0; ph < ph_nb; ph++)
162 sin_lut[ph] = sin(
M_PI * ph / phase_count);
164 for(ph = 0; ph < ph_nb; ph++) {
167 for(i=0;i<=tap_count;i++) {
168 x =
M_PI * ((double)(i - center) - (double)ph / phase_count) *
factor;
170 else if (factor == 1.0)
177 x = fabs(((
double)(i - center) - (
double)ph / phase_count) * factor);
178 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
179 else y= d*(-4 + 8*x - 5*x*x + x*x*x);
182 w = 2.0*x / (factor*tap_count);
184 y *= 0.3635819 - 0.4891775 * t + 0.1365995 * (2*t*t-1) - 0.0106411 * (4*t*t*t - 3*t);
187 w = 2.0*x / (factor*tap_count*
M_PI);
203 for(i=0;i<tap_count;i++)
204 ((int16_t*)filter)[ph * alloc + i] = av_clip_int16(
lrintf(tab[i] * scale / norm));
205 if (phase_count % 2)
break;
206 if (tap_count % 2 == 0 || tap_count == 1) {
207 for (i = 0; i < tap_count; i++)
208 ((int16_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int16_t*)
filter)[ph * alloc + i];
211 for (i = 1; i <= tap_count; i++)
212 ((int16_t*)
filter)[(phase_count-ph) * alloc + tap_count-i] =
213 av_clip_int16(
lrintf(tab[i] * scale / (norm - tab[0] + tab[tap_count])));
217 for(i=0;i<tap_count;i++)
218 ((
int32_t*)filter)[ph * alloc + i] = av_clipl_int32(
llrint(tab[i] * scale / norm));
219 if (phase_count % 2)
break;
220 if (tap_count % 2 == 0 || tap_count == 1) {
221 for (i = 0; i < tap_count; i++)
222 ((
int32_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
int32_t*)
filter)[ph * alloc + i];
225 for (i = 1; i <= tap_count; i++)
227 av_clipl_int32(
llrint(tab[i] * scale / (norm - tab[0] + tab[tap_count])));
231 for(i=0;i<tap_count;i++)
232 ((
float*)filter)[ph * alloc + i] = tab[i] * scale / norm;
233 if (phase_count % 2)
break;
234 if (tap_count % 2 == 0 || tap_count == 1) {
235 for (i = 0; i < tap_count; i++)
236 ((
float*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
float*)
filter)[ph * alloc + i];
239 for (i = 1; i <= tap_count; i++)
240 ((
float*)
filter)[(phase_count-ph) * alloc + tap_count-i] = tab[i] * scale / (norm - tab[0] + tab[tap_count]);
244 for(i=0;i<tap_count;i++)
245 ((
double*)
filter)[ph * alloc + i] = tab[i] * scale / norm;
246 if (phase_count % 2)
break;
247 if (tap_count % 2 == 0 || tap_count == 1) {
248 for (i = 0; i < tap_count; i++)
249 ((
double*)
filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((
double*)filter)[ph * alloc + i];
252 for (i = 1; i <= tap_count; i++)
253 ((
double*)
filter)[(phase_count-ph) * alloc + tap_count-i] = tab[i] * scale / (norm - tab[0] + tab[tap_count]);
262 double sine[
LEN + tap_count];
263 double filtered[
LEN];
264 double maxff=-2, minff=2, maxsf=-2, minsf=2;
265 for(i=0; i<
LEN; i++){
266 double ss=0, sf=0, ff=0;
267 for(j=0; j<LEN+tap_count; j++)
268 sine[j]= cos(i*j*
M_PI/LEN);
269 for(j=0; j<
LEN; j++){
272 for(k=0; k<tap_count; k++)
273 sum += filter[ph * tap_count + k] * sine[k+j];
275 ss+= sine[j + center] * sine[j + center];
276 ff+= filtered[j] * filtered[j];
277 sf+= sine[j + center] * filtered[j];
282 maxff=
FFMAX(maxff, ff);
283 minff=
FFMIN(minff, ff);
284 maxsf=
FFMAX(maxsf, sf);
285 minsf=
FFMIN(minsf, sf);
287 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);
303 double precision,
int cheby,
int exact_rational)
305 double cutoff = cutoff0? cutoff0 : 0.97;
306 double factor=
FFMIN(out_rate * cutoff / in_rate, 1.0);
308 int phase_count_compensation = phase_count;
310 if (exact_rational) {
311 int phase_count_exact, phase_count_exact_den;
313 av_reduce(&phase_count_exact, &phase_count_exact_den, out_rate, in_rate, INT_MAX);
314 if (phase_count_exact <= phase_count) {
315 phase_count_compensation = phase_count_exact * (phase_count / phase_count_exact);
316 phase_count = phase_count_exact;
347 if (filter_size/factor > INT32_MAX/256) {
402 int new_src_incr, new_dst_incr;
412 if (!new_filter_bank)
450 if (compensation_distance) {
457 if (compensation_distance)
470 int src_size,
int dst_size,
int update_ctx)
479 dst_size=
FFMIN(dst_size, new_size);
495 dst_size =
FFMIN(dst_size, delta_n);
497 *consumed = c->
dsp.
resample(c, dst, src, dst_size, update_ctx);
515 src_size =
FFMIN(src_size, max_src_size);
519 consumed, src_size, dst_size, i+1==dst->
ch_count);
583 int in_count,
int *out_idx,
int *out_sz)
594 for (n = *out_sz; n < num; n++) {
595 for (ch = 0; ch < src->
ch_count; ch++) {
610 for (ch = 0; ch < src->
ch_count; ch++) {
619 while (c->
index < 0) {
626 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.
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 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)
struct ResampleContext * resample
resampling context
static const uint8_t q1[256]
#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)
static double eval_poly(const double *coeff, int size, double x)
struct ResampleContext::@225 dsp
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
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
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
#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 * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
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
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...