decoder/pcm: round buffer size down to nearest frame size

https://bugs.musicpd.org/view.php?id=4599
This commit is contained in:
Max Kellermann 2016-11-17 21:58:27 +01:00
parent 8bde47280a
commit 7019f6bea4
2 changed files with 34 additions and 12 deletions

1
NEWS
View File

@ -1,6 +1,7 @@
ver 0.19.20 (not yet released) ver 0.19.20 (not yet released)
* decoder * decoder
- ffmpeg: ignore empty packets - ffmpeg: ignore empty packets
- pcm: fix corruption bug with partial frames (after short read)
- sidplay: fix playback speed with libsidplayfp - sidplay: fix playback speed with libsidplayfp
* output * output
- winmm: fix 8 bit playback - winmm: fix 8 bit playback

View File

@ -23,10 +23,28 @@
#include "input/InputStream.hxx" #include "input/InputStream.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/ByteReverse.hxx" #include "util/ByteReverse.hxx"
#include "util/StaticFifoBuffer.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <assert.h>
#include <string.h> #include <string.h>
template<typename B>
static bool
FillBuffer(Decoder &decoder, InputStream &is, B &buffer)
{
buffer.Shift();
auto w = buffer.Write();
assert(!w.IsEmpty());
size_t nbytes = decoder_read(decoder, is, w.data, w.size);
if (nbytes == 0 && is.LockIsEOF())
return false;
buffer.Append(nbytes);
return true;
}
static void static void
pcm_stream_decode(Decoder &decoder, InputStream &is) pcm_stream_decode(Decoder &decoder, InputStream &is)
{ {
@ -50,25 +68,27 @@ pcm_stream_decode(Decoder &decoder, InputStream &is)
decoder_initialized(decoder, audio_format, decoder_initialized(decoder, audio_format,
is.IsSeekable(), total_time); is.IsSeekable(), total_time);
StaticFifoBuffer<uint8_t, 4096> buffer;
DecoderCommand cmd; DecoderCommand cmd;
do { do {
char buffer[4096]; if (!FillBuffer(decoder, is, buffer))
size_t nbytes = decoder_read(decoder, is,
buffer, sizeof(buffer));
if (nbytes == 0 && is.LockIsEOF())
break; break;
auto r = buffer.Read();
/* round down to the nearest frame size, because we
must not pass partial frames to decoder_data() */
r.size -= r.size % frame_size;
buffer.Consume(r.size);
if (reverse_endian) if (reverse_endian)
/* make sure we deliver samples in host byte order */ /* make sure we deliver samples in host byte order */
reverse_bytes_16((uint16_t *)buffer, reverse_bytes_16((uint16_t *)r.data,
(uint16_t *)buffer, (uint16_t *)r.data,
(uint16_t *)(buffer + nbytes)); (uint16_t *)(r.data + r.size));
cmd = nbytes > 0 cmd = !r.IsEmpty()
? decoder_data(decoder, is, ? decoder_data(decoder, is, r.data, r.size, 0)
buffer, nbytes, 0)
: decoder_get_command(decoder); : decoder_get_command(decoder);
if (cmd == DecoderCommand::SEEK) { if (cmd == DecoderCommand::SEEK) {
uint64_t frame = decoder_seek_where_frame(decoder); uint64_t frame = decoder_seek_where_frame(decoder);
@ -76,6 +96,7 @@ pcm_stream_decode(Decoder &decoder, InputStream &is)
Error error; Error error;
if (is.LockSeek(offset, error)) { if (is.LockSeek(offset, error)) {
buffer.Clear();
decoder_command_finished(decoder); decoder_command_finished(decoder);
} else { } else {
LogError(error); LogError(error);