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;
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;
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);
206 for(
i=0;
i<tap_count;
i++)
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++)
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;
243 double ss=0, sf=0, ff=0;
244 for(j=0; j<
LEN+tap_count; j++)
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];
251 filtered[j]= sum / (1<<FILTER_SHIFT);
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);
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);
293 int phase_count= 1<<phase_shift;
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;
310 if (!
c ||
c->phase_count != phase_count ||
c->linear!=
linear ||
c->factor !=
factor
311 ||
c->filter_length != filter_length ||
c->format !=
format
312 ||
c->filter_type != filter_type ||
c->kaiser_beta !=
kaiser_beta) {
324 c->filter_shift = 15;
327 c->filter_shift = 30;
338 if (filter_size/
factor > INT32_MAX/256) {
343 c->phase_count = phase_count;
346 c->filter_length = filter_length;
347 c->filter_alloc =
FFALIGN(
c->filter_length, 8);
348 c->filter_bank =
av_calloc(
c->filter_alloc, (phase_count+1)*
c->felem_size);
349 c->filter_type = filter_type;
351 c->phase_count_compensation = phase_count_compensation;
356 memcpy(
c->filter_bank + (
c->filter_alloc*phase_count+1)*
c->felem_size,
c->filter_bank, (
c->filter_alloc-1)*
c->felem_size);
357 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);
360 c->compensation_distance= 0;
361 if(!
av_reduce(&
c->src_incr, &
c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
363 while (
c->dst_incr < (1<<20) &&
c->src_incr < (1<<20)) {
367 c->ideal_dst_incr =
c->dst_incr;
368 c->dst_incr_div =
c->dst_incr /
c->src_incr;
369 c->dst_incr_mod =
c->dst_incr %
c->src_incr;
371 c->index= -phase_count*((
c->filter_length-1)/2);
386 int new_src_incr, new_dst_incr;
387 int phase_count =
c->phase_count_compensation;
390 if (phase_count ==
c->phase_count)
395 new_filter_bank =
av_calloc(
c->filter_alloc, (phase_count + 1) *
c->felem_size);
396 if (!new_filter_bank)
400 phase_count, 1 <<
c->filter_shift,
c->filter_type,
c->kaiser_beta);
405 memcpy(new_filter_bank + (
c->filter_alloc*phase_count+1)*
c->felem_size, new_filter_bank, (
c->filter_alloc-1)*
c->felem_size);
406 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);
408 if (!
av_reduce(&new_src_incr, &new_dst_incr,
c->src_incr,
409 c->dst_incr * (int64_t)(phase_count/
c->phase_count), INT32_MAX/2))
415 c->src_incr = new_src_incr;
416 c->dst_incr = new_dst_incr;
417 while (
c->dst_incr < (1<<20) &&
c->src_incr < (1<<20)) {
421 c->ideal_dst_incr =
c->dst_incr;
422 c->dst_incr_div =
c->dst_incr /
c->src_incr;
423 c->dst_incr_mod =
c->dst_incr %
c->src_incr;
424 c->index *= phase_count /
c->phase_count;
425 c->phase_count = phase_count;
427 c->filter_bank = new_filter_bank;
434 if (compensation_distance && sample_delta) {
440 c->compensation_distance= compensation_distance;
441 if (compensation_distance)
442 c->dst_incr =
c->ideal_dst_incr -
c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
444 c->dst_incr =
c->ideal_dst_incr;
446 c->dst_incr_div =
c->dst_incr /
c->src_incr;
447 c->dst_incr_mod =
c->dst_incr %
c->src_incr;
457 int64_t max_src_size = (INT64_MAX/2 /
c->phase_count) /
c->src_incr;
459 if (
c->compensation_distance)
460 dst_size =
FFMIN(dst_size,
c->compensation_distance);
461 src_size =
FFMIN(src_size, max_src_size);
465 if (
c->filter_length == 1 &&
c->phase_count == 1) {
466 int64_t index2= (1LL<<32)*
c->frac/
c->src_incr + (1LL<<32)*
c->index;
467 int64_t incr= (1LL<<32) *
c->dst_incr /
c->src_incr;
468 int new_size = (src_size * (int64_t)
c->src_incr -
c->frac +
c->dst_incr - 1) /
c->dst_incr;
470 dst_size =
FFMAX(
FFMIN(dst_size, new_size), 0);
473 c->dsp.resample_one(dst->
ch[
i],
src->ch[
i], dst_size, index2, incr);
475 c->index += dst_size *
c->dst_incr_div;
476 c->index += (
c->frac + dst_size * (int64_t)
c->dst_incr_mod) /
c->src_incr;
478 *consumed =
c->index;
479 c->frac = (
c->frac + dst_size * (int64_t)
c->dst_incr_mod) %
c->src_incr;
485 int64_t end_index = (1LL + src_size -
c->filter_length) *
c->phase_count;
486 int64_t delta_frac = (end_index -
c->index) *
c->src_incr -
c->frac;
487 int delta_n = (delta_frac +
c->dst_incr - 1) /
c->dst_incr;
489 const void *
src,
int n,
int update_ctx);
491 dst_size =
FFMAX(
FFMIN(dst_size, delta_n), 0);
495 resample_func = (
c->linear && (
c->frac ||
c->dst_incr_mod)) ?
496 c->dsp.resample_linear :
c->dsp.resample_common;
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);
505 if (
c->compensation_distance) {
506 c->compensation_distance -= dst_size;
507 if (!
c->compensation_distance) {
508 c->dst_incr =
c->ideal_dst_incr;
509 c->dst_incr_div =
c->dst_incr /
c->src_incr;
510 c->dst_incr_mod =
c->dst_incr %
c->src_incr;
519 int64_t num =
s->in_buffer_count - (
c->filter_length-1)/2;
520 num *=
c->phase_count;
524 return av_rescale(num,
base,
s->in_sample_rate*(int64_t)
c->src_incr *
c->phase_count);
532 int64_t num =
s->in_buffer_count + 2LL + in_samples;
533 num *=
c->phase_count;
537 if (
c->compensation_distance) {
541 num =
FFMAX(num, (num *
c->ideal_dst_incr - 1) /
c->dst_incr + 1);
550 int reflection = (
FFMIN(
s->in_buffer_count,
c->filter_length) + 1) / 2;
555 for(
i=0;
i<
a->ch_count;
i++){
556 for(j=0; j<reflection; j++){
557 memcpy(
a->ch[
i] + (
s->in_buffer_index+
s->in_buffer_count+j )*
a->bps,
558 a->ch[
i] + (
s->in_buffer_index+
s->in_buffer_count-j-1)*
a->bps,
a->bps);
561 s->in_buffer_count += reflection;
567 int in_count,
int *out_idx,
int *out_sz)
569 int n, ch, num =
FFMIN(in_count + *out_sz,
c->filter_length + 1), res;
578 for (n = *out_sz; n < num; n++) {
579 for (ch = 0; ch <
src->ch_count; ch++) {
580 memcpy(dst->
ch[ch] + ((
c->filter_length + n) *
c->felem_size),
581 src->ch[ch] + ((n - *out_sz) *
c->felem_size),
c->felem_size);
588 *out_idx =
c->filter_length;
593 for (n = 1; n <=
c->filter_length; n++) {
594 for (ch = 0; ch <
src->ch_count; ch++) {
595 memcpy(dst->
ch[ch] + ((
c->filter_length - n) *
c->felem_size),
596 dst->
ch[ch] + ((
c->filter_length + n) *
c->felem_size),
602 *out_idx =
c->filter_length;
603 while (
c->index < 0) {
605 c->index +=
c->phase_count;
607 *out_sz =
FFMAX(*out_sz +
c->filter_length,
608 1 +
c->filter_length * 2) - *out_idx;
610 return FFMAX(res, 0);