summaryrefslogtreecommitdiffstats
path: root/audio
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-04-18 16:19:46 +0200
committerwm4 <wm4@nowhere>2014-04-18 16:19:46 +0200
commit5616229dde2c6da86ca3a5818c636493c32f6c06 (patch)
tree6fd65e0fad464edbdaa7ca8eb39c14b8106ad900 /audio
parent8931bc46ba2fa7619c0bb4f8ffab40ac4458bc7a (diff)
downloadmpv-5616229dde2c6da86ca3a5818c636493c32f6c06.tar.bz2
mpv-5616229dde2c6da86ca3a5818c636493c32f6c06.tar.xz
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.
Diffstat (limited to 'audio')
-rw-r--r--audio/audio.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/audio/audio.c b/audio/audio.c
index e8ff96ddc3..57096eaa38 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -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