int linear;
enum AVResampleFilterType filter_type;
int kaiser_beta;
- double factor;
void (*set_filter)(void *filter, double *tab, int phase, int tap_count);
void (*resample_one)(struct ResampleContext *c, void *dst0,
int dst_index, const void *src0,
int padding_size;
int initial_padding_filled;
int initial_padding_samples;
+ int final_padding_filled;
+ int final_padding_samples;
};
}
/* Build a polyphase filterbank. */
-static int build_filter(ResampleContext *c)
+static int build_filter(ResampleContext *c, double factor)
{
int ph, i;
- double x, y, w, factor;
+ double x, y, w;
double *tab;
int tap_count = c->filter_length;
int phase_count = 1 << c->phase_shift;
if (!tab)
return AVERROR(ENOMEM);
- /* if upsampling, only need to interpolate, no filter */
- factor = FFMIN(c->factor, 1.0);
-
for (ph = 0; ph < phase_count; ph++) {
double norm = 0;
for (i = 0; i < tap_count; i++) {
c->phase_shift = avr->phase_shift;
c->phase_mask = phase_count - 1;
c->linear = avr->linear_interp;
- c->factor = factor;
c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
c->filter_type = avr->filter_type;
c->kaiser_beta = avr->kaiser_beta;
if (!c->filter_bank)
goto error;
- if (build_filter(c) < 0)
+ if (build_filter(c, factor) < 0)
goto error;
memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
if (ret < 0)
return ret;
- } else if (!in_leftover) {
+ } else if (in_leftover <= c->final_padding_samples) {
/* no remaining samples to flush */
return 0;
- } else {
- /* TODO: pad buffer to flush completely */
}
if (!c->initial_padding_filled) {
int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
int i;
- if (c->buffer->nb_samples < 2 * c->padding_size)
+ if (src && c->buffer->nb_samples < 2 * c->padding_size)
return 0;
for (i = 0; i < c->padding_size; i++)
- for (ch = 0; ch < c->buffer->channels; ch++)
- memcpy(c->buffer->data[ch] + bps * i,
- c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps);
+ for (ch = 0; ch < c->buffer->channels; ch++) {
+ if (c->buffer->nb_samples > 2 * c->padding_size - i) {
+ memcpy(c->buffer->data[ch] + bps * i,
+ c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps);
+ } else {
+ memset(c->buffer->data[ch] + bps * i, 0, bps);
+ }
+ }
c->initial_padding_filled = 1;
}
+ if (!src && !c->final_padding_filled) {
+ int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
+ int i;
+
+ ret = ff_audio_data_realloc(c->buffer, in_samples + c->padding_size);
+ if (ret < 0) {
+ av_log(c->avr, AV_LOG_ERROR, "Error reallocating resampling buffer\n");
+ return AVERROR(ENOMEM);
+ }
+
+ for (i = 0; i < c->padding_size; i++)
+ for (ch = 0; ch < c->buffer->channels; ch++) {
+ if (in_leftover > i) {
+ memcpy(c->buffer->data[ch] + bps * (in_leftover + i),
+ c->buffer->data[ch] + bps * (in_leftover - i - 1),
+ bps);
+ } else {
+ memset(c->buffer->data[ch] + bps * (in_leftover + i),
+ 0, bps);
+ }
+ }
+ c->buffer->nb_samples += c->padding_size;
+ c->final_padding_samples = c->padding_size;
+ c->final_padding_filled = 1;
+ }
+
+
/* calculate output size and reallocate output buffer if needed */
/* TODO: try to calculate this without the dummy resample() run */
if (!dst->read_only && dst->allow_realloc) {