From a96d26e63a78d6325ed0238a24d8bd4a66519d19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Sun, 5 Nov 2023 09:08:47 +0100 Subject: [PATCH] audio: avoid unnecessary silence padding in read_buffer() Not all callers of read_buffer() require the buffer to be padded with silence. --- audio/out/buffer.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/audio/out/buffer.c b/audio/out/buffer.c index a5520f3a21..71367e47fd 100644 --- a/audio/out/buffer.c +++ b/audio/out/buffer.c @@ -114,7 +114,8 @@ struct mp_async_queue *ao_get_queue(struct ao *ao) } // Special behavior with data==NULL: caller uses p->pending. -static int read_buffer(struct ao *ao, void **data, int samples, bool *eof) +static int read_buffer(struct ao *ao, void **data, int samples, bool *eof, + bool pad_silence) { struct buffer_state *p = ao->buffer_state; int pos = 0; @@ -160,10 +161,12 @@ static int read_buffer(struct ao *ao, void **data, int samples, bool *eof) } // pad with silence (underflow/paused/eof) - for (int n = 0; n < ao->num_planes; n++) { - af_fill_silence((char *)data[n] + pos * ao->sstride, - (samples - pos) * ao->sstride, - ao->format); + if (pad_silence) { + for (int n = 0; n < ao->num_planes; n++) { + af_fill_silence((char *)data[n] + pos * ao->sstride, + (samples - pos) * ao->sstride, + ao->format); + } } ao_post_process_data(ao, data, pos); @@ -171,12 +174,12 @@ static int read_buffer(struct ao *ao, void **data, int samples, bool *eof) } static int ao_read_data_unlocked(struct ao *ao, void **data, int samples, - int64_t out_time_ns) + int64_t out_time_ns, bool pad_silence) { struct buffer_state *p = ao->buffer_state; assert(!ao->driver->write); - int pos = read_buffer(ao, data, samples, &(bool){0}); + int pos = read_buffer(ao, data, samples, &(bool){0}, pad_silence); if (pos > 0) p->end_time_ns = out_time_ns; @@ -205,7 +208,7 @@ int ao_read_data(struct ao *ao, void **data, int samples, int64_t out_time_ns) mp_mutex_lock(&p->lock); - int pos = ao_read_data_unlocked(ao, data, samples, out_time_ns); + int pos = ao_read_data_unlocked(ao, data, samples, out_time_ns, true); mp_mutex_unlock(&p->lock); @@ -221,7 +224,7 @@ int ao_read_data_nonblocking(struct ao *ao, void **data, int samples, int64_t ou if (mp_mutex_trylock(&p->lock)) return 0; - int pos = ao_read_data_unlocked(ao, data, samples, out_time_ns); + int pos = ao_read_data_unlocked(ao, data, samples, out_time_ns, false); mp_mutex_unlock(&p->lock); @@ -623,7 +626,7 @@ static bool ao_play_data(struct ao *ao) bool got_eof = false; if (ao->driver->write_frames) { TA_FREEP(&p->pending); - samples = read_buffer(ao, NULL, 1, &got_eof); + samples = read_buffer(ao, NULL, 1, &got_eof, false); planes = (void **)&p->pending; } else { if (!realloc_buf(ao, space)) { @@ -640,7 +643,7 @@ static bool ao_play_data(struct ao *ao) } if (!samples) { - samples = read_buffer(ao, planes, space, &got_eof); + samples = read_buffer(ao, planes, space, &got_eof, true); if (p->paused || (ao->stream_silence && !p->playing)) samples = space; // read_buffer() sets remainder to silent }