From fc28e4af4d77a2aa29857471f0de1dc38996cc20 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 24 Jul 2014 15:26:43 +0200 Subject: [PATCH] audio: adjust format change code Execute the format change based on whether we logically detected EOF (after filters), instead of when the decode buffer was drained. It's slightly cleaner. (The requirement of len>0 existed before.) --- audio/decode/dec_audio.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/audio/decode/dec_audio.c b/audio/decode/dec_audio.c index 218063a699..a090f9a707 100644 --- a/audio/decode/dec_audio.c +++ b/audio/decode/dec_audio.c @@ -242,17 +242,18 @@ int audio_init_filters(struct dec_audio *d_audio, int in_samplerate, static int filter_n_bytes(struct dec_audio *da, struct mp_audio_buffer *outbuf, int len) { + bool format_change = false; int error = 0; + assert(len > 0); // would break EOF logic below + while (mp_audio_buffer_samples(da->decode_buffer) < len) { // Check for a format change struct mp_audio config; mp_audio_buffer_get_format(da->decode_buffer, &config); - if (!mp_audio_config_equals(&da->decoded, &config)) { - // If there are still samples left in the buffer, let them drain - // first, and don't signal a format change to the caller yet. - if (mp_audio_buffer_samples(da->decode_buffer) == 0) - error = AD_NEW_FMT; + format_change = !mp_audio_config_equals(&da->decoded, &config); + if (format_change) { + error = AD_EOF; // drain remaining data left in the current buffer break; } if (da->decoded.samples > 0) { @@ -286,9 +287,9 @@ static int filter_n_bytes(struct dec_audio *da, struct mp_audio_buffer *outbuf, // remove processed data from decoder buffer: mp_audio_buffer_skip(da->decode_buffer, len); - // Assume the filter chain is drained from old data at this point. - // (If not, the remaining old data is discarded.) - if (error == AD_NEW_FMT) { + // if format was changed, and all data was drained, execute the format change + if (format_change && eof) { + error = AD_NEW_FMT; if (!reinit_audio_buffer(da)) error = AD_ERR; // switch to invalid format }