summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--decoder.h28
-rw-r--r--decoders/mpg123_decoder.cpp8
-rw-r--r--decoders/mpg123_decoder.h8
-rw-r--r--encoder.h34
-rw-r--r--encoders/lame_encoder.cpp14
-rw-r--r--encoders/lame_encoder.h10
6 files changed, 77 insertions, 25 deletions
diff --git a/decoder.h b/decoder.h
index 9faabc1..d58dcf1 100644
--- a/decoder.h
+++ b/decoder.h
@@ -4,21 +4,39 @@
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/shared_ptr.hpp>
+#include <boost/function.hpp>
#include <string>
class DecoderBase {
+ friend class DecoderFilter;
+
+ protected:
+ typedef boost::function<std::size_t (char*, std::size_t)> ReadFunc;
+
+ virtual size_t decode(ReadFunc read, uint8_t *output, size_t output_size) = 0;
+
public:
typedef boost::shared_ptr<DecoderBase> p;
- virtual size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0;
virtual ~DecoderBase() {}
-
};
//! Input filter to hold a decoder in a filter chain.
class DecoderFilter : public boost::iostreams::multichar_input_filter {
private:
DecoderBase::p decoder;
+
+ //! Functor binding a source to a read function.
+ template<class T>
+ struct ReadFunc {
+ T& s;
+
+ ReadFunc(T& s_) : s(s_) {}
+
+ std::size_t operator()(char* buf, std::size_t buf_size) {
+ return boost::iostreams::read(s, buf, buf_size);
+ }
+ };
public:
typedef boost::shared_ptr<DecoderFilter> p;
@@ -26,11 +44,7 @@ class DecoderFilter : public boost::iostreams::multichar_input_filter {
template<typename Source>
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);
+ return decoder->decode(ReadFunc<Source>(src), (uint8_t*)s, n);
}
};
diff --git a/decoders/mpg123_decoder.cpp b/decoders/mpg123_decoder.cpp
index 0c2629f..0618320 100644
--- a/decoders/mpg123_decoder.cpp
+++ b/decoders/mpg123_decoder.cpp
@@ -48,3 +48,11 @@ size_t DecoderMpg123::decode(const uint8_t *input, size_t input_size, uint8_t *o
return output_written;
}
+
+size_t DecoderMpg123::decode(ReadFunc read, uint8_t *output, size_t output_size) {
+ char src_data[0x2000];
+ std::streamsize src_read = read(src_data, 0x2000);
+ if(src_read < 0)
+ src_read = 0;
+ return decode((const uint8_t*)src_data, src_read, (uint8_t*)output, output_size);
+}
diff --git a/decoders/mpg123_decoder.h b/decoders/mpg123_decoder.h
index 46f9e4c..5a637af 100644
--- a/decoders/mpg123_decoder.h
+++ b/decoders/mpg123_decoder.h
@@ -8,11 +8,15 @@
class DecoderMpg123 : public DecoderBase {
private:
mpg123_handle *handle;
-
+
+ size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size);
+
+ protected:
+ virtual size_t decode(ReadFunc read, uint8_t *output, size_t output_size);
+
public:
DecoderMpg123();
~DecoderMpg123();
- size_t decode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size);
};
#endif
diff --git a/encoder.h b/encoder.h
index f68d51c..d000ffc 100644
--- a/encoder.h
+++ b/encoder.h
@@ -4,23 +4,40 @@
#include <boost/iostreams/concepts.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/shared_ptr.hpp>
+#include <boost/function.hpp>
#include <iostream>
#include <string>
class EncoderBase {
+ friend class EncoderFilter;
+
+ protected:
+ typedef boost::function<std::size_t (char*, std::size_t)> ReadFunc;
+
+ virtual size_t encode(ReadFunc read, uint8_t *output, size_t output_size) = 0;
+
public:
typedef boost::shared_ptr<EncoderBase> p;
virtual ~EncoderBase() {}
-
- virtual size_t encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size) = 0;
- virtual size_t flush(uint8_t *output, size_t output_size) = 0;
};
//! Input filter to hold an encoder in a filter chain.
class EncoderFilter : public boost::iostreams::multichar_input_filter {
private:
EncoderBase::p encoder;
+
+ //! Functor binding a source to a read function.
+ template<class T>
+ struct ReadFunc {
+ T& s;
+
+ ReadFunc(T& s_) : s(s_) {}
+
+ std::size_t operator()(char* buf, std::size_t buf_size) {
+ return boost::iostreams::read(s, buf, buf_size);
+ }
+ };
public:
typedef boost::shared_ptr<EncoderFilter> p;
@@ -28,16 +45,7 @@ class EncoderFilter : public boost::iostreams::multichar_input_filter {
template<typename Source>
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;
- std::streamsize size = encoder->encode((const uint8_t*)src_data, src_read, (uint8_t*)s, n);
- // no more data, flush encoder
- if(src_read == 0 && size == 0) {
- size = encoder->flush((uint8_t*)s, n);
- }
- return size;
+ return encoder->encode(ReadFunc<Source>(src), (uint8_t*)s, n);
};
};
diff --git a/encoders/lame_encoder.cpp b/encoders/lame_encoder.cpp
index 125ee5e..7bf2ebc 100644
--- a/encoders/lame_encoder.cpp
+++ b/encoders/lame_encoder.cpp
@@ -22,3 +22,17 @@ size_t EncoderLame::encode(const uint8_t *input, size_t input_size, uint8_t *out
size_t EncoderLame::flush(uint8_t *output, size_t output_size) {
return lame_encode_flush(gfp, output, output_size);
}
+
+size_t EncoderLame::encode(ReadFunc read, uint8_t *output, size_t output_size) {
+ char src_data[0x2000];
+ std::streamsize src_read = read(src_data, 0x2000);
+ if(src_read < 0)
+ src_read = 0;
+ std::streamsize size = encode((const uint8_t*)src_data, src_read, (uint8_t*)output, output_size);
+ // no more data, flush encoder
+ if(src_read == 0 && size == 0) {
+ size = flush((uint8_t*)output, output_size);
+ }
+ return size;
+
+} \ No newline at end of file
diff --git a/encoders/lame_encoder.h b/encoders/lame_encoder.h
index 1843361..98ebc9f 100644
--- a/encoders/lame_encoder.h
+++ b/encoders/lame_encoder.h
@@ -8,12 +8,16 @@
class EncoderLame : public EncoderBase {
private:
lame_global_flags *gfp;
-
+
+ size_t encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size);
+ size_t flush(uint8_t *output, size_t output_size);
+
+ protected:
+ virtual size_t encode(ReadFunc read, uint8_t *output, size_t output_size);
+
public:
EncoderLame();
~EncoderLame();
- size_t encode(const uint8_t *input, size_t input_size, uint8_t *output, size_t output_size);
- size_t flush(uint8_t *output, size_t output_size);
};
#endif