From b5452bc9bc09e66121aae35adbd5c504a3f90466 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 6 Jan 2011 02:12:25 +0100 Subject: Let decoders read a dynamic amount through a Source object. --- decoder.h | 26 ++++++++++++++++++++------ decoders/mpg123_decoder.cpp | 9 +++++++-- decoders/mpg123_decoder.h | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/decoder.h b/decoder.h index 9faabc1..31986f7 100644 --- a/decoder.h +++ b/decoder.h @@ -7,12 +7,29 @@ #include +class Source { + public: + virtual std::streamsize read(char* buf, std::streamsize buf_size) = 0; +}; + +template +class StreamSource : public Source { + private: + T& s; + + public: + StreamSource(T& s_) : s(s_) {} + std::streamsize read(char* buf, std::streamsize buf_size) { + return boost::iostreams::read(s, buf, buf_size); + } +}; + class DecoderBase { public: typedef boost::shared_ptr p; - virtual size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0; virtual ~DecoderBase() {} + virtual size_t decode(Source& input, uint8_t *output, size_t output_size) = 0; }; //! Input filter to hold a decoder in a filter chain. @@ -26,11 +43,8 @@ class DecoderFilter : public boost::iostreams::multichar_input_filter { template std::streamsize read(Source& src, char *s, std::streamsize n) { - char src_data[0x2000]; - std::streamsize src_read = boost::iostreams::read(src, src_data, 0x2000); - if(src_read < 0) - src_read = 0; - return decoder->decode((const uint8_t*)src_data, src_read, (uint8_t*)s, n); + StreamSource src_f(src); + return decoder->decode(src_f, (uint8_t*)s, n); } }; diff --git a/decoders/mpg123_decoder.cpp b/decoders/mpg123_decoder.cpp index 0c2629f..a195c58 100644 --- a/decoders/mpg123_decoder.cpp +++ b/decoders/mpg123_decoder.cpp @@ -26,8 +26,13 @@ DecoderMpg123::~DecoderMpg123() { mpg123_delete(handle); } -size_t DecoderMpg123::decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) { - if(mpg123_feed(handle, input, input_size) != MPG123_OK) { +size_t DecoderMpg123::decode(Source& input, uint8_t *output, size_t output_size) { + char src_data[0x2000]; + std::streamsize src_read = input.read(src_data, 0x2000); + if(src_read < 0) + src_read = 0; + + if(mpg123_feed(handle, (uint8_t*)src_data, src_read) != MPG123_OK) { throw std::runtime_error(mpg123_strerror(handle)); } diff --git a/decoders/mpg123_decoder.h b/decoders/mpg123_decoder.h index 46f9e4c..9e2bcdf 100644 --- a/decoders/mpg123_decoder.h +++ b/decoders/mpg123_decoder.h @@ -12,7 +12,7 @@ class DecoderMpg123 : public DecoderBase { public: DecoderMpg123(); ~DecoderMpg123(); - size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size); + size_t decode(Source& input, uint8_t *output, size_t output_size); }; #endif -- cgit v1.2.3