31 #define CONFIG_RESAMPLE_DBL
33 #undef CONFIG_RESAMPLE_DBL
36 #define CONFIG_RESAMPLE_FLT
38 #undef CONFIG_RESAMPLE_FLT
41 #define CONFIG_RESAMPLE_S32
43 #undef CONFIG_RESAMPLE_S32
58 for (
i = 1; v != lastv;
i++) {
72 int tap_count =
c->filter_length;
73 int phase_count = 1 <<
c->phase_shift;
74 const int center = (tap_count - 1) / 2;
80 for (ph = 0; ph < phase_count; ph++) {
82 for (
i = 0;
i < tap_count;
i++) {
83 x =
M_PI * ((double)(
i - center) - (double)ph / phase_count) *
factor;
86 switch (
c->filter_type) {
89 x =
fabs(((
double)(
i - center) - (
double)ph / phase_count) *
factor);
90 if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x +
d * ( -x*x + x*x*x);
91 else y =
d * (-4 + 8 * x - 5 * x*x + x*x*x);
96 y *= 0.3635819 - 0.4891775 * cos(
w) +
97 0.1365995 * cos(2 *
w) -
98 0.0106411 * cos(3 *
w);
110 for (
i = 0;
i < tap_count;
i++)
113 c->set_filter(
c->filter_bank,
tab, ph, tap_count);
144 c->phase_mask = phase_count - 1;
152 c->resample_one =
c->linear ? resample_linear_dbl : resample_one_dbl;
153 c->resample_nearest = resample_nearest_dbl;
154 c->set_filter = set_filter_dbl;
157 c->resample_one =
c->linear ? resample_linear_flt : resample_one_flt;
158 c->resample_nearest = resample_nearest_flt;
159 c->set_filter = set_filter_flt;
162 c->resample_one =
c->linear ? resample_linear_s32 : resample_one_s32;
163 c->resample_nearest = resample_nearest_s32;
164 c->set_filter = set_filter_s32;
167 c->resample_one =
c->linear ? resample_linear_s16 : resample_one_s16;
168 c->resample_nearest = resample_nearest_s16;
169 c->set_filter = set_filter_s16;
179 c->filter_bank =
av_mallocz(
c->filter_length * (phase_count + 1) * felem_size);
186 memcpy(&
c->filter_bank[(
c->filter_length * phase_count + 1) * felem_size],
187 c->filter_bank, (
c->filter_length - 1) * felem_size);
188 memcpy(&
c->filter_bank[
c->filter_length * phase_count * felem_size],
189 &
c->filter_bank[(
c->filter_length - 1) * felem_size], felem_size);
191 c->compensation_distance = 0;
192 if (!
av_reduce(&
c->src_incr, &
c->dst_incr, out_rate,
193 in_rate * (int64_t)phase_count, INT32_MAX / 2))
195 c->ideal_dst_incr =
c->dst_incr;
197 c->padding_size = (
c->filter_length - 1) / 2;
198 c->initial_padding_filled = 0;
208 c->buffer->nb_samples =
c->padding_size;
209 c->initial_padding_samples =
c->padding_size;
234 int compensation_distance)
238 if (compensation_distance < 0)
240 if (!compensation_distance && sample_delta)
248 c->compensation_distance = compensation_distance;
249 if (compensation_distance) {
250 c->dst_incr =
c->ideal_dst_incr -
c->ideal_dst_incr *
251 (int64_t)sample_delta / compensation_distance;
253 c->dst_incr =
c->ideal_dst_incr;
260 int *consumed,
int src_size,
int dst_size,
int update_ctx,
261 int nearest_neighbour)
264 unsigned int index =
c->index;
266 int dst_incr_frac =
c->dst_incr %
c->src_incr;
267 int dst_incr =
c->dst_incr /
c->src_incr;
268 int compensation_distance =
c->compensation_distance;
273 if (nearest_neighbour) {
274 uint64_t index2 = ((uint64_t)
index) << 32;
275 int64_t incr = (1LL << 32) *
c->dst_incr /
c->src_incr;
276 dst_size =
FFMIN(dst_size,
277 (src_size-1-
index) * (int64_t)
c->src_incr /
281 for(dst_index = 0; dst_index < dst_size; dst_index++) {
282 c->resample_nearest(dst, dst_index,
src, index2 >> 32);
286 dst_index = dst_size;
288 index += dst_index * dst_incr;
289 index += (frac + dst_index * (int64_t)dst_incr_frac) /
c->src_incr;
290 frac = (frac + dst_index * (int64_t)dst_incr_frac) %
c->src_incr;
292 for (dst_index = 0; dst_index < dst_size; dst_index++) {
293 int sample_index =
index >>
c->phase_shift;
295 if (sample_index +
c->filter_length > src_size)
299 c->resample_one(
c, dst, dst_index,
src,
index, frac);
301 frac += dst_incr_frac;
303 if (frac >=
c->src_incr) {
307 if (dst_index + 1 == compensation_distance) {
308 compensation_distance = 0;
309 dst_incr_frac =
c->ideal_dst_incr %
c->src_incr;
310 dst_incr =
c->ideal_dst_incr /
c->src_incr;
315 *consumed =
index >>
c->phase_shift;
320 if (compensation_distance) {
321 compensation_distance -= dst_index;
322 if (compensation_distance <= 0)
327 c->dst_incr = dst_incr_frac +
c->src_incr*dst_incr;
328 c->compensation_distance = compensation_distance;
336 int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
338 int nearest_neighbour = (
c->compensation_distance == 0 &&
339 c->filter_length == 1 &&
340 c->phase_shift == 0);
342 in_samples =
src ?
src->nb_samples : 0;
343 in_leftover =
c->buffer->nb_samples;
350 }
else if (in_leftover <= c->final_padding_samples) {
355 if (!
c->initial_padding_filled) {
359 if (
src &&
c->buffer->nb_samples < 2 *
c->padding_size)
362 for (
i = 0;
i <
c->padding_size;
i++)
363 for (ch = 0; ch <
c->buffer->channels; ch++) {
364 if (
c->buffer->nb_samples > 2 *
c->padding_size -
i) {
365 memcpy(
c->buffer->data[ch] +
bps *
i,
366 c->buffer->data[ch] +
bps * (2 *
c->padding_size -
i),
bps);
368 memset(
c->buffer->data[ch] +
bps *
i, 0,
bps);
371 c->initial_padding_filled = 1;
374 if (!
src && !
c->final_padding_filled) {
379 FFMAX(in_samples, in_leftover) +
386 for (
i = 0;
i <
c->padding_size;
i++)
387 for (ch = 0; ch <
c->buffer->channels; ch++) {
388 if (in_leftover >
i) {
389 memcpy(
c->buffer->data[ch] +
bps * (in_leftover +
i),
390 c->buffer->data[ch] +
bps * (in_leftover -
i - 1),
393 memset(
c->buffer->data[ch] +
bps * (in_leftover +
i),
397 c->buffer->nb_samples +=
c->padding_size;
398 c->final_padding_samples =
c->padding_size;
399 c->final_padding_filled = 1;
407 INT_MAX, 0, nearest_neighbour);
416 for (ch = 0; ch <
c->buffer->channels; ch++) {
418 (
const void *)
c->buffer->data[ch], &consumed,
420 ch + 1 ==
c->buffer->channels, nearest_neighbour);
422 if (out_samples < 0) {
429 c->initial_padding_samples =
FFMAX(
c->initial_padding_samples - consumed, 0);
432 in_samples, in_leftover, out_samples,
c->buffer->nb_samples);
445 return FFMAX(
c->buffer->nb_samples -
c->padding_size, 0);