1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-22 23:53:29 +00:00

audio: preallocate audio buffers on resize

This avoids too many realloc() calls if the caller is appending to an
audo buffer. This case is actually quite noticeable when using something
that buffers a large amount of audio.
This commit is contained in:
wm4 2014-04-18 16:19:46 +02:00
parent 8931bc46ba
commit 5616229dde

View File

@ -173,10 +173,15 @@ void mp_audio_realloc(struct mp_audio *mpa, int samples)
}
// Like mp_audio_realloc(), but only reallocate if the audio grows in size.
// If the buffer is reallocated, also preallocate.
void mp_audio_realloc_min(struct mp_audio *mpa, int samples)
{
if (samples > mp_audio_get_allocated_size(mpa))
mp_audio_realloc(mpa, samples);
if (samples > mp_audio_get_allocated_size(mpa)) {
size_t alloc = ta_calc_prealloc_elems(samples);
if (alloc > INT_MAX)
abort(); // oom
mp_audio_realloc(mpa, alloc);
}
}
/* Get the size allocated for the data, in number of samples. If the allocated