44 int ph_nb = phase_count % 2 ? phase_count : phase_count / 2 + 1;
48 const int center= (tap_count-1)/2;
55 av_assert0(tap_count == 1 || tap_count % 2 == 0);
62 for (
ph = 0;
ph < ph_nb;
ph++)
63 sin_lut[
ph] = sin(
M_PI *
ph / phase_count) * (center & 1 ? 1 : -1);
65 for(
ph = 0;
ph < ph_nb;
ph++) {
67 for(
i=0;
i<tap_count;
i++) {
77 x =
fabs(((
double)(
i - center) - (
double)
ph / phase_count) *
factor);
78 if(x<1.0) y= 1 - 3*x*x + 2*x*x*x +
d*( -x*x + x*x*x);
79 else y=
d*(-4 + 8*x - 5*x*x + x*x*x);
84 y *= 0.3635819 - 0.4891775 * t + 0.1365995 * (2*t*t-1) - 0.0106411 * (4*t*t*t - 3*t);
103 for(
i=0;
i<tap_count;
i++)
105 if (phase_count % 2)
break;
106 for (
i = 0;
i < tap_count;
i++)
107 ((int16_t*)
filter)[(phase_count-
ph) * alloc + tap_count-1-
i] = ((int16_t*)
filter)[
ph * alloc +
i];
110 for(
i=0;
i<tap_count;
i++)
112 if (phase_count % 2)
break;
113 for (
i = 0;
i < tap_count;
i++)
117 for(
i=0;
i<tap_count;
i++)
119 if (phase_count % 2)
break;
120 for (
i = 0;
i < tap_count;
i++)
121 ((
float*)
filter)[(phase_count-
ph) * alloc + tap_count-1-
i] = ((
float*)
filter)[
ph * alloc +
i];
124 for(
i=0;
i<tap_count;
i++)
126 if (phase_count % 2)
break;
127 for (
i = 0;
i < tap_count;
i++)
128 ((
double*)
filter)[(phase_count-
ph) * alloc + tap_count-1-
i] = ((
double*)
filter)[
ph * alloc +
i];
136 double sine[
LEN + tap_count];
137 double filtered[
LEN];
138 double maxff=-2, minff=2, maxsf=-2, minsf=2;
140 double ss=0, sf=0, ff=0;
141 for(j=0; j<
LEN+tap_count; j++)
143 for(j=0; j<
LEN; j++){
146 for(k=0; k<tap_count; k++)
147 sum +=
filter[
ph * tap_count + k] * sine[k+j];
148 filtered[j]= sum / (1<<FILTER_SHIFT);
149 ss+= sine[j + center] * sine[j + center];
150 ff+= filtered[j] * filtered[j];
151 sf+= sine[j + center] * filtered[j];
156 maxff=
FFMAX(maxff, ff);
157 minff=
FFMIN(minff, ff);
158 maxsf=
FFMAX(maxsf, sf);
159 minsf=
FFMIN(minsf, sf);
186 double precision,
int cheby,
int exact_rational)
188 double cutoff = cutoff0? cutoff0 : 0.97;
189 double factor=
FFMIN(out_rate * cutoff / in_rate, 1.0);
190 int phase_count= 1<<phase_shift;
191 int phase_count_compensation = phase_count;
194 if (filter_length > 1)
195 filter_length =
FFALIGN(filter_length, 2);
197 if (exact_rational) {
198 int phase_count_exact, phase_count_exact_den;
200 av_reduce(&phase_count_exact, &phase_count_exact_den, out_rate, in_rate, INT_MAX);
201 if (phase_count_exact <= phase_count) {
202 phase_count_compensation = phase_count_exact * (phase_count / phase_count_exact);
203 phase_count = phase_count_exact;
207 if (!
c ||
c->phase_count != phase_count ||
c->linear!=
linear ||
c->factor !=
factor
208 ||
c->filter_length != filter_length ||
c->format !=
format
209 ||
c->filter_type != filter_type ||
c->kaiser_beta !=
kaiser_beta) {
221 c->filter_shift = 15;
224 c->filter_shift = 30;
235 if (filter_size/
factor > INT32_MAX/256) {
240 c->phase_count = phase_count;
243 c->filter_length = filter_length;
244 c->filter_alloc =
FFALIGN(
c->filter_length, 8);
245 c->filter_bank =
av_calloc(
c->filter_alloc, (phase_count+1)*
c->felem_size);
246 c->filter_type = filter_type;
248 c->phase_count_compensation = phase_count_compensation;
253 memcpy(
c->filter_bank + (
c->filter_alloc*phase_count+1)*
c->felem_size,
c->filter_bank, (
c->filter_alloc-1)*
c->felem_size);
254 memcpy(
c->filter_bank + (
c->filter_alloc*phase_count )*
c->felem_size,
c->filter_bank + (
c->filter_alloc - 1)*
c->felem_size,
c->felem_size);
257 c->compensation_distance= 0;
258 if(!
av_reduce(&
c->src_incr, &
c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
260 while (
c->dst_incr < (1<<20) &&
c->src_incr < (1<<20)) {
264 c->ideal_dst_incr =
c->dst_incr;
265 c->dst_incr_div =
c->dst_incr /
c->src_incr;
266 c->dst_incr_mod =
c->dst_incr %
c->src_incr;
268 c->index= -phase_count*((
c->filter_length-1)/2);
282 uint8_t *new_filter_bank;
283 int new_src_incr, new_dst_incr;
284 int phase_count =
c->phase_count_compensation;
287 if (phase_count ==
c->phase_count)
292 new_filter_bank =
av_calloc(
c->filter_alloc, (phase_count + 1) *
c->felem_size);
293 if (!new_filter_bank)
297 phase_count, 1 <<
c->filter_shift,
c->filter_type,
c->kaiser_beta);
302 memcpy(new_filter_bank + (
c->filter_alloc*phase_count+1)*
c->felem_size, new_filter_bank, (
c->filter_alloc-1)*
c->felem_size);
303 memcpy(new_filter_bank + (
c->filter_alloc*phase_count )*
c->felem_size, new_filter_bank + (
c->filter_alloc - 1)*
c->felem_size,
c->felem_size);
305 if (!
av_reduce(&new_src_incr, &new_dst_incr,
c->src_incr,
306 c->dst_incr * (int64_t)(phase_count/
c->phase_count), INT32_MAX/2))
312 c->src_incr = new_src_incr;
313 c->dst_incr = new_dst_incr;
314 while (
c->dst_incr < (1<<20) &&
c->src_incr < (1<<20)) {
318 c->ideal_dst_incr =
c->dst_incr;
319 c->dst_incr_div =
c->dst_incr /
c->src_incr;
320 c->dst_incr_mod =
c->dst_incr %
c->src_incr;
321 c->index *= phase_count /
c->phase_count;
322 c->phase_count = phase_count;
324 c->filter_bank = new_filter_bank;
331 if (compensation_distance && sample_delta) {
337 c->compensation_distance= compensation_distance;
338 if (compensation_distance)
339 c->dst_incr =
c->ideal_dst_incr -
c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
341 c->dst_incr =
c->ideal_dst_incr;
343 c->dst_incr_div =
c->dst_incr /
c->src_incr;
344 c->dst_incr_mod =
c->dst_incr %
c->src_incr;
351 int64_t max_src_size = (INT64_MAX/2 /
c->phase_count) /
c->src_incr;
353 if (
c->compensation_distance)
354 dst_size =
FFMIN(dst_size,
c->compensation_distance);
355 src_size =
FFMIN(src_size, max_src_size);
359 if (
c->filter_length == 1 &&
c->phase_count == 1) {
360 int64_t index2= (1LL<<32)*
c->frac/
c->src_incr + (1LL<<32)*
c->index + 1;
361 int64_t incr= (1LL<<32) *
c->dst_incr /
c->src_incr + 1;
362 int new_size = (src_size * (int64_t)
c->src_incr -
c->frac +
c->dst_incr - 1) /
c->dst_incr;
364 dst_size =
FFMAX(
FFMIN(dst_size, new_size), 0);
367 c->dsp.resample_one(dst->
ch[
i],
src->ch[
i], dst_size, index2, incr);
369 c->index += dst_size *
c->dst_incr_div;
370 c->index += (
c->frac + dst_size * (int64_t)
c->dst_incr_mod) /
c->src_incr;
372 *consumed =
c->index;
373 c->frac = (
c->frac + dst_size * (int64_t)
c->dst_incr_mod) %
c->src_incr;
379 int64_t end_index = (1LL + src_size -
c->filter_length) *
c->phase_count;
380 int64_t delta_frac = (end_index -
c->index) *
c->src_incr -
c->frac;
381 int delta_n = (delta_frac +
c->dst_incr - 1) /
c->dst_incr;
383 const void *
src,
int n,
int update_ctx);
385 dst_size =
FFMAX(
FFMIN(dst_size, delta_n), 0);
389 resample_func = (
c->linear && (
c->frac ||
c->dst_incr_mod)) ?
390 c->dsp.resample_linear :
c->dsp.resample_common;
391 for (
i = 0;
i < dst->ch_count;
i++)
392 *consumed = resample_func(
c, dst->ch[
i],
src->ch[
i], dst_size,
i+1 == dst->ch_count);
396 if (
c->compensation_distance) {
397 c->compensation_distance -= dst_size;
398 if (!
c->compensation_distance) {
399 c->dst_incr =
c->ideal_dst_incr;
400 c->dst_incr_div =
c->dst_incr /
c->src_incr;
401 c->dst_incr_mod =
c->dst_incr %
c->src_incr;
410 int64_t num =
s->in_buffer_count - (
c->filter_length-1)/2;
411 num *=
c->phase_count;
415 return av_rescale(num,
base,
s->in_sample_rate*(int64_t)
c->src_incr *
c->phase_count);
423 int64_t num =
s->in_buffer_count + 2LL + in_samples;
424 num *=
c->phase_count;
428 if (
c->compensation_distance) {
432 num =
FFMAX(num, (num *
c->ideal_dst_incr - 1) /
c->dst_incr + 1);
441 int reflection = (
FFMIN(
s->in_buffer_count,
c->filter_length) + 1) / 2;
446 for(
i=0;
i<
a->ch_count;
i++){
447 for(j=0; j<reflection; j++){
448 memcpy(
a->ch[
i] + (
s->in_buffer_index+
s->in_buffer_count+j )*
a->bps,
449 a->ch[
i] + (
s->in_buffer_index+
s->in_buffer_count-j-1)*
a->bps,
a->bps);
452 s->in_buffer_count += reflection;
458 int in_count,
int *out_idx,
int *out_sz)
460 int n, ch, num =
FFMIN(in_count + *out_sz,
c->filter_length + 1), res;
469 for (n = *out_sz; n < num; n++) {
470 for (ch = 0; ch <
src->ch_count; ch++) {
471 memcpy(dst->
ch[ch] + ((
c->filter_length + n) *
c->felem_size),
472 src->ch[ch] + ((n - *out_sz) *
c->felem_size),
c->felem_size);
479 *out_idx =
c->filter_length;
484 for (n = 1; n <=
c->filter_length; n++) {
485 for (ch = 0; ch <
src->ch_count; ch++) {
486 memcpy(dst->
ch[ch] + ((
c->filter_length - n) *
c->felem_size),
487 dst->
ch[ch] + ((
c->filter_length + n) *
c->felem_size),
493 *out_idx =
c->filter_length;
494 while (
c->index < 0) {
496 c->index +=
c->phase_count;
498 *out_sz =
FFMAX(*out_sz +
c->filter_length,
499 1 +
c->filter_length * 2) - *out_idx;
501 return FFMAX(res, 0);