DecoderBuffer: convert functions to methods

This commit is contained in:
Max Kellermann 2014-09-22 08:26:04 +02:00
parent 505e6bec9e
commit 90c228abca
3 changed files with 92 additions and 128 deletions

View File

@ -20,84 +20,52 @@
#include "config.h" #include "config.h"
#include "DecoderBuffer.hxx" #include "DecoderBuffer.hxx"
#include "DecoderAPI.hxx" #include "DecoderAPI.hxx"
#include "util/ConstBuffer.hxx"
#include <assert.h> #include <assert.h>
const InputStream &
decoder_buffer_get_stream(const DecoderBuffer *buffer)
{
return buffer->is;
}
void
decoder_buffer_clear(DecoderBuffer *buffer)
{
buffer->buffer.Clear();
}
bool bool
decoder_buffer_fill(DecoderBuffer *buffer) DecoderBuffer::Fill()
{ {
auto w = buffer->buffer.Write(); auto w = buffer.Write();
if (w.IsEmpty()) if (w.IsEmpty())
/* buffer is full */ /* buffer is full */
return false; return false;
size_t nbytes = decoder_read(buffer->decoder, buffer->is, size_t nbytes = decoder_read(decoder, is,
w.data, w.size); w.data, w.size);
if (nbytes == 0) if (nbytes == 0)
/* end of file, I/O error or decoder command /* end of file, I/O error or decoder command
received */ received */
return false; return false;
buffer->buffer.Append(nbytes); buffer.Append(nbytes);
return true; return true;
} }
size_t
decoder_buffer_available(const DecoderBuffer *buffer)
{
return buffer->buffer.GetAvailable();
}
ConstBuffer<void> ConstBuffer<void>
decoder_buffer_read(const DecoderBuffer *buffer) DecoderBuffer::Need(size_t min_size)
{
auto r = buffer->buffer.Read();
return { r.data, r.size };
}
ConstBuffer<void>
decoder_buffer_need(DecoderBuffer *buffer, size_t min_size)
{ {
while (true) { while (true) {
const auto r = decoder_buffer_read(buffer); const auto r = Read();
if (r.size >= min_size) if (r.size >= min_size)
return r; return r;
if (!decoder_buffer_fill(buffer)) if (!Fill())
return nullptr; return nullptr;
} }
} }
void
decoder_buffer_consume(DecoderBuffer *buffer, size_t nbytes)
{
buffer->buffer.Consume(nbytes);
}
bool bool
decoder_buffer_skip(DecoderBuffer *buffer, size_t nbytes) DecoderBuffer::Skip(size_t nbytes)
{ {
const auto r = buffer->buffer.Read(); const auto r = buffer.Read();
if (r.size >= nbytes) { if (r.size >= nbytes) {
buffer->buffer.Consume(nbytes); buffer.Consume(nbytes);
return true; return true;
} }
buffer->buffer.Clear(); buffer.Clear();
nbytes -= r.size; nbytes -= r.size;
return decoder_skip(buffer->decoder, buffer->is, nbytes); return decoder_skip(decoder, is, nbytes);
} }

View File

@ -22,12 +22,12 @@
#include "Compiler.h" #include "Compiler.h"
#include "util/DynamicFifoBuffer.hxx" #include "util/DynamicFifoBuffer.hxx"
#include "util/ConstBuffer.hxx"
#include <stddef.h> #include <stddef.h>
struct Decoder; struct Decoder;
class InputStream; class InputStream;
template<typename T> struct ConstBuffer;
/** /**
* This objects handles buffered reads in decoder plugins easily. You * This objects handles buffered reads in decoder plugins easily. You
@ -51,70 +51,66 @@ struct DecoderBuffer {
DecoderBuffer(Decoder *_decoder, InputStream &_is, DecoderBuffer(Decoder *_decoder, InputStream &_is,
size_t _size) size_t _size)
:decoder(_decoder), is(_is), buffer(_size) {} :decoder(_decoder), is(_is), buffer(_size) {}
const InputStream &GetStream() const {
return is;
}
void Clear() {
buffer.Clear();
}
/**
* Read data from the #InputStream and append it to the buffer.
*
* @return true if data was appended; false if there is no
* data available (yet), end of file, I/O error or a decoder
* command was received
*/
bool Fill();
/**
* How many bytes are stored in the buffer?
*/
gcc_pure
size_t GetAvailable() const {
return buffer.GetAvailable();
}
/**
* Reads data from the buffer. This data is not yet consumed,
* you have to call Consume() to do that. The returned buffer
* becomes invalid after a Fill() or a Consume() call.
*/
ConstBuffer<void> Read() const {
auto r = buffer.Read();
return { r.data, r.size };
}
/**
* Wait until this number of bytes are available. Returns nullptr on
* error.
*/
ConstBuffer<void> Need(size_t min_size);
/**
* Consume (delete, invalidate) a part of the buffer. The
* "nbytes" parameter must not be larger than the length
* returned by Read().
*
* @param nbytes the number of bytes to consume
*/
void Consume(size_t nbytes) {
buffer.Consume(nbytes);
}
/**
* Skips the specified number of bytes, discarding its data.
*
* @param nbytes the number of bytes to skip
* @return true on success, false on error
*/
bool Skip(size_t nbytes);
}; };
gcc_pure
const InputStream &
decoder_buffer_get_stream(const DecoderBuffer *buffer);
void
decoder_buffer_clear(DecoderBuffer *buffer);
/**
* Read data from the input_stream and append it to the buffer.
*
* @return true if data was appended; false if there is no data
* available (yet), end of file, I/O error or a decoder command was
* received
*/
bool
decoder_buffer_fill(DecoderBuffer *buffer);
/**
* How many bytes are stored in the buffer?
*/
gcc_pure
size_t
decoder_buffer_available(const DecoderBuffer *buffer);
/**
* Reads data from the buffer. This data is not yet consumed, you
* have to call decoder_buffer_consume() to do that. The returned
* buffer becomes invalid after a decoder_buffer_fill() or a
* decoder_buffer_consume() call.
*
* @param buffer the decoder_buffer object
*/
gcc_pure
ConstBuffer<void>
decoder_buffer_read(const DecoderBuffer *buffer);
/**
* Wait until this number of bytes are available. Returns nullptr on
* error.
*/
ConstBuffer<void>
decoder_buffer_need(DecoderBuffer *buffer, size_t min_size);
/**
* Consume (delete, invalidate) a part of the buffer. The "nbytes"
* parameter must not be larger than the length returned by
* decoder_buffer_read().
*
* @param buffer the decoder_buffer object
* @param nbytes the number of bytes to consume
*/
void
decoder_buffer_consume(DecoderBuffer *buffer, size_t nbytes);
/**
* Skips the specified number of bytes, discarding its data.
*
* @param buffer the decoder_buffer object
* @param nbytes the number of bytes to skip
* @return true on success, false on error
*/
bool
decoder_buffer_skip(DecoderBuffer *buffer, size_t nbytes);
#endif #endif

View File

@ -66,7 +66,7 @@ static size_t
adts_find_frame(DecoderBuffer &buffer) adts_find_frame(DecoderBuffer &buffer)
{ {
while (true) { while (true) {
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_need(&buffer, 8)); auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(8));
if (data.IsNull()) if (data.IsNull())
/* failed */ /* failed */
return 0; return 0;
@ -76,13 +76,13 @@ adts_find_frame(DecoderBuffer &buffer)
memchr(data.data, 0xff, data.size); memchr(data.data, 0xff, data.size);
if (p == nullptr) { if (p == nullptr) {
/* no marker - discard the buffer */ /* no marker - discard the buffer */
decoder_buffer_clear(&buffer); buffer.Clear();
continue; continue;
} }
if (p > data.data) { if (p > data.data) {
/* discard data before 0xff */ /* discard data before 0xff */
decoder_buffer_consume(&buffer, p - data.data); buffer.Consume(p - data.data);
continue; continue;
} }
@ -91,14 +91,14 @@ adts_find_frame(DecoderBuffer &buffer)
if (frame_length == 0) { if (frame_length == 0) {
/* it's just some random 0xff byte; discard it /* it's just some random 0xff byte; discard it
and continue searching */ and continue searching */
decoder_buffer_consume(&buffer, 1); buffer.Consume(1);
continue; continue;
} }
if (decoder_buffer_need(&buffer, frame_length).IsNull()) { if (buffer.Need(frame_length).IsNull()) {
/* not enough data; discard this frame to /* not enough data; discard this frame to
prevent a possible buffer overflow */ prevent a possible buffer overflow */
decoder_buffer_clear(&buffer); buffer.Clear();
continue; continue;
} }
@ -110,7 +110,7 @@ adts_find_frame(DecoderBuffer &buffer)
static SignedSongTime static SignedSongTime
adts_song_duration(DecoderBuffer &buffer) adts_song_duration(DecoderBuffer &buffer)
{ {
const InputStream &is = decoder_buffer_get_stream(&buffer); const InputStream &is = buffer.GetStream();
const bool estimate = !is.CheapSeeking(); const bool estimate = !is.CheapSeeking();
if (estimate && !is.KnownSize()) if (estimate && !is.KnownSize())
return SignedSongTime::Negative(); return SignedSongTime::Negative();
@ -125,7 +125,7 @@ adts_song_duration(DecoderBuffer &buffer)
break; break;
if (frames == 0) { if (frames == 0) {
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(&buffer)); auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
assert(!data.IsEmpty()); assert(!data.IsEmpty());
assert(frame_length <= data.size); assert(frame_length <= data.size);
@ -134,7 +134,7 @@ adts_song_duration(DecoderBuffer &buffer)
break; break;
} }
decoder_buffer_consume(&buffer, frame_length); buffer.Consume(frame_length);
if (estimate && frames == 128) { if (estimate && frames == 128) {
/* if this is a remote file, don't slurp the /* if this is a remote file, don't slurp the
@ -144,7 +144,7 @@ adts_song_duration(DecoderBuffer &buffer)
have until now */ have until now */
const auto offset = is.GetOffset() const auto offset = is.GetOffset()
- decoder_buffer_available(&buffer); - buffer.GetAvailable();
if (offset <= 0) if (offset <= 0)
return SignedSongTime::Negative(); return SignedSongTime::Negative();
@ -164,7 +164,7 @@ adts_song_duration(DecoderBuffer &buffer)
static SignedSongTime static SignedSongTime
faad_song_duration(DecoderBuffer &buffer, InputStream &is) faad_song_duration(DecoderBuffer &buffer, InputStream &is)
{ {
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_need(&buffer, 5)); auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(5));
if (data.IsNull()) if (data.IsNull())
return SignedSongTime::Negative(); return SignedSongTime::Negative();
@ -177,10 +177,10 @@ faad_song_duration(DecoderBuffer &buffer, InputStream &is)
tagsize += 10; tagsize += 10;
if (!decoder_buffer_skip(&buffer, tagsize)) if (!buffer.Skip(tagsize))
return SignedSongTime::Negative(); return SignedSongTime::Negative();
data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_need(&buffer, 5)); data = ConstBuffer<uint8_t>::FromVoid(buffer.Need(5));
if (data.IsNull()) if (data.IsNull())
return SignedSongTime::Negative(); return SignedSongTime::Negative();
} }
@ -195,7 +195,7 @@ faad_song_duration(DecoderBuffer &buffer, InputStream &is)
is.LockSeek(tagsize, IgnoreError()); is.LockSeek(tagsize, IgnoreError());
decoder_buffer_clear(&buffer); buffer.Clear();
return song_length; return song_length;
} else if (data.size >= 5 && memcmp(data.data, "ADIF", 4) == 0) { } else if (data.size >= 5 && memcmp(data.data, "ADIF", 4) == 0) {
@ -248,7 +248,7 @@ static bool
faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer, faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer,
AudioFormat &audio_format, Error &error) AudioFormat &audio_format, Error &error)
{ {
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(&buffer)); auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
if (data.IsEmpty()) { if (data.IsEmpty()) {
error.Set(faad_decoder_domain, "Empty file"); error.Set(faad_decoder_domain, "Empty file");
return false; return false;
@ -274,7 +274,7 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer &buffer,
return false; return false;
} }
decoder_buffer_consume(&buffer, nbytes); buffer.Consume(nbytes);
return audio_format_init_checked(audio_format, sample_rate, return audio_format_init_checked(audio_format, sample_rate,
SampleFormat::S16, channels, error); SampleFormat::S16, channels, error);
@ -288,7 +288,7 @@ static const void *
faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer &buffer, faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer &buffer,
NeAACDecFrameInfo *frame_info) NeAACDecFrameInfo *frame_info)
{ {
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(&buffer)); auto data = ConstBuffer<uint8_t>::FromVoid(buffer.Read());
if (data.IsEmpty()) if (data.IsEmpty())
return nullptr; return nullptr;
@ -315,7 +315,7 @@ faad_get_file_time(InputStream &is)
if (!recognized) { if (!recognized) {
NeAACDecHandle decoder = faad_decoder_new(); NeAACDecHandle decoder = faad_decoder_new();
decoder_buffer_fill(&buffer); buffer.Fill();
AudioFormat audio_format; AudioFormat audio_format;
if (faad_decoder_init(decoder, buffer, audio_format, if (faad_decoder_init(decoder, buffer, audio_format,
@ -390,7 +390,7 @@ faad_stream_decode(Decoder &mpd_decoder, InputStream &is,
break; break;
} }
decoder_buffer_consume(&buffer, frame_info.bytesconsumed); buffer.Consume(frame_info.bytesconsumed);
/* update bit rate and position */ /* update bit rate and position */