spa: Fix audioconvert overflow when scaling

Add SPA_SCALE32_UP that scales a uint32 without overflow.
Use this for scaling the threshold in ALSA.
Fix the scaling in audioconvert of the buffer size, the scaling was
wrong and it was also causing an overflow resulting in choppy sound in
some cases.

See #2680
This commit is contained in:
Wim Taymans 2023-01-16 18:28:31 +01:00
parent 32a7c85c84
commit 1d9640af5a
3 changed files with 11 additions and 3 deletions

View file

@ -1999,7 +1999,7 @@ static inline void check_position_config(struct state *state)
(state->rate_denom != state->position->clock.rate.denom))) {
state->duration = state->position->clock.duration;
state->rate_denom = state->position->clock.rate.denom;
state->threshold = (state->duration * state->rate + state->rate_denom-1) / state->rate_denom;
state->threshold = SPA_SCALE32_UP(state->duration, state->rate, state->rate_denom);
state->max_error = SPA_MAX(256.0f, state->threshold / 2.0f);
state->resample = ((uint32_t)state->rate != state->rate_denom) || state->matching;
state->alsa_sync = true;
@ -2569,7 +2569,7 @@ int spa_alsa_start(struct state *state)
setup_matching(state);
spa_dll_init(&state->dll);
state->threshold = (state->duration * state->rate + state->rate_denom-1) / state->rate_denom;
state->threshold = SPA_SCALE32_UP(state->duration, state->rate, state->rate_denom);
state->last_threshold = state->threshold;
state->max_error = SPA_MAX(256.0f, state->threshold / 2.0f);

View file

@ -1755,7 +1755,7 @@ impl_node_port_enum_params(void *object, int seq,
size = this->quantum_limit * 2;
/* scale the buffer size when we can. */
if (irate != 0 && orate != 0)
size = size * (irate + orate - 1) / orate;
size = SPA_SCALE32_UP(size, irate, orate);
}
param = spa_pod_builder_add_object(&b,