resample: improve buffer size calculations

Scale the buffer size with the rate conversion ratio. Also make sure
that we can at least produce a maximum quantum of samples.

If we have large upconversion (8KH -> 48KHz) and small input
buffers, we would not allocate enough space for the output buffer
and cause xruns in the sink.

Fixes #1809
This commit is contained in:
Wim Taymans 2021-11-16 11:42:14 +01:00
parent 34847f117f
commit f10b872733

View file

@ -48,7 +48,7 @@ static struct spa_log_topic *log_topic = &SPA_LOG_TOPIC(0, "spa.resample");
#define DEFAULT_RATE 48000
#define DEFAULT_CHANNELS 2
#define MAX_SAMPLES 8192
#define MAX_SAMPLES 8192u
#define MAX_ALIGN 16
#define MAX_BUFFERS 32
@ -477,20 +477,26 @@ impl_node_port_enum_params(void *object, int seq,
case SPA_PARAM_Buffers:
{
uint32_t buffers, size;
uint32_t rate;
if (!port->have_format)
if (!port->have_format || !other->have_format)
return -EIO;
if (result.index > 0)
return 0;
if (direction == SPA_DIRECTION_OUTPUT) {
rate = (this->resample.o_rate + this->resample.i_rate - 1) / this->resample.i_rate;
} else {
rate = (this->resample.i_rate + this->resample.o_rate - 1) / this->resample.o_rate;
}
if (other->n_buffers > 0) {
buffers = other->n_buffers;
size = other->size / other->stride * 2;
}
else {
size = (other->size / other->stride) * rate;
} else {
buffers = 1;
size = MAX_SAMPLES*2 * other->stride;
size = MAX_SAMPLES * rate;
}
size = SPA_MAX(size, MAX_SAMPLES) * 2;
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamBuffers, id,