Skip to content

Commit

Permalink
audio: avoid unnecessary silence padding in read_buffer()
Browse files Browse the repository at this point in the history
Not all callers of read_buffer() require the buffer to be padded with
silence.
  • Loading branch information
t-8ch authored and sfan5 committed Nov 8, 2023
1 parent 0b43b74 commit a96d26e
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions audio/out/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -160,23 +161,25 @@ 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);
return pos;
}

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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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)) {
Expand All @@ -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
}
Expand Down

0 comments on commit a96d26e

Please sign in to comment.