summaryrefslogtreecommitdiff
path: root/src/codecs
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-11-23 17:15:06 +1100
committerjacqueline <me@jacqueline.id.au>2022-11-23 17:15:06 +1100
commita7df2855889055976956a58d2a36f23626371ee9 (patch)
tree16e180e57f84474acaeb1893208cc07e278af6f4 /src/codecs
parentdfa9ab6e04689b99267092e016a91d9254f94cd8 (diff)
downloadtangara-fw-a7df2855889055976956a58d2a36f23626371ee9.tar.gz
Mostly done pipeline arch. Now onto cleanup and building.
Diffstat (limited to 'src/codecs')
-rw-r--r--src/codecs/CMakeLists.txt2
-rw-r--r--src/codecs/codec.cpp5
-rw-r--r--src/codecs/include/codec.hpp90
-rw-r--r--src/codecs/include/mad.hpp32
-rw-r--r--src/codecs/include/types.hpp12
-rw-r--r--src/codecs/mad.cpp265
6 files changed, 203 insertions, 203 deletions
diff --git a/src/codecs/CMakeLists.txt b/src/codecs/CMakeLists.txt
index 522aa885..4a8918ae 100644
--- a/src/codecs/CMakeLists.txt
+++ b/src/codecs/CMakeLists.txt
@@ -1,5 +1,5 @@
idf_component_register(
- SRCS "mad.cpp"
+ SRCS "codec.cpp" "mad.cpp"
INCLUDE_DIRS "include")
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
diff --git a/src/codecs/codec.cpp b/src/codecs/codec.cpp
index 70cfe10a..db8d69b9 100644
--- a/src/codecs/codec.cpp
+++ b/src/codecs/codec.cpp
@@ -2,8 +2,9 @@
namespace codecs {
-auto CreateCodecForExtension(std::string extension) -> cpp::result<std::unique_ptr<ICodec>, CreateCodecError> {
+auto CreateCodecForExtension(std::string extension)
+ -> cpp::result<std::unique_ptr<ICodec>, CreateCodecError> {
return cpp::fail(UNKNOWN_EXTENSION);
}
-} // namespace codecs
+} // namespace codecs
diff --git a/src/codecs/include/codec.hpp b/src/codecs/include/codec.hpp
index 99e786d5..8e82bd71 100644
--- a/src/codecs/include/codec.hpp
+++ b/src/codecs/include/codec.hpp
@@ -8,51 +8,53 @@
namespace codecs {
- enum CreateCodecError {
- UNKNOWN_EXTENSION
+enum CreateCodecError { UNKNOWN_EXTENSION };
+
+auto CreateCodecForFile(const std::string& extension)
+ -> cpp::result<std::unique_ptr<ICodec>, CreateCodecError>;
+
+class ICodec {
+ public:
+ virtual ~ICodec() {}
+
+ virtual auto CanHandleFile(const std::string& path) -> bool = 0;
+
+ struct OutputFormat {
+ uint8_t num_channels;
+ uint8_t bits_per_sample;
+ int sample_rate_hz;
};
- auto CreateCodecForExtension(std::string extension) -> cpp::result<std::unique_ptr<ICodec>, CreateCodecError>;
-
- class ICodec {
- public:
- virtual ~ICodec() {}
-
- virtual auto CanHandleExtension(std::string extension) -> bool = 0;
-
- struct OutputFormat {
- uint8_t num_channels;
- uint8_t bits_per_sample;
- int sample_rate_hz;
- };
-
- virtual auto GetOutputFormat() -> OutputFormat = 0;
-
- enum Error {};
-
- struct Result {
- bool need_more_input;
- /*
- * For need_more_input, this is how far we got in the input buffer
- * before we were unable to process more data. Any remaining data in the
- * buffer should be moved to the start before the next call.
- */
- std::size_t input_processed;
-
- bool flush_output;
- /*
- * For flush_output, this is how far we got in the output buffer before
- * we ran out of space for samples. The caller should flush this many
- * bytes downstream.
- */
- std::size_t output_written;
- };
-
- virtual auto Process(
- uint8_t *input,
- std::size_t input_len,
- uint8_t *output,
- std::size_t output_length) -> cpp::result<Result, Error> = 0;
+ virtual auto GetOutputFormat() -> OutputFormat = 0;
+
+ enum ProcessingError {};
+
+ struct Result {
+ bool need_more_input;
+ /*
+ * For need_more_input, this is how far we got in the input buffer
+ * before we were unable to process more data. Any remaining data in the
+ * buffer should be moved to the start before the next call.
+ */
+ std::size_t input_processed;
+
+ bool flush_output;
+ /*
+ * For flush_output, this is how far we got in the output buffer before
+ * we ran out of space for samples. The caller should flush this many
+ * bytes downstream.
+ */
+ std::size_t output_written;
};
-} // namespace codecs
+ virtual auto ResetForNewStream() -> void = 0;
+
+ virtual auto SetInput(uint8_t* buffer, std::size_t length) = 0;
+ virtual auto Process(uint8_t* output, std::size_t output_length)
+ -> cpp::result<size_t, Error> = 0;
+
+ virtual auto GetOutputProcessed() -> std::size_t;
+ = 0;
+};
+
+} // namespace codecs
diff --git a/src/codecs/include/mad.hpp b/src/codecs/include/mad.hpp
index cfe4eab7..241ea6c3 100644
--- a/src/codecs/include/mad.hpp
+++ b/src/codecs/include/mad.hpp
@@ -4,23 +4,25 @@
namespace codecs {
- class MadMp3Decoder : public ICodec {
- public:
- MadMp3Decoder();
- ~MadMp3Decoder();
+class MadMp3Decoder : public ICodec {
+ public:
+ MadMp3Decoder();
+ ~MadMp3Decoder();
- auto ProcessInput(Result *res, uint8_t *input, std::size_t input_len) -> void;
- auto WriteOutputSamples(Result *res, uint8_t *output, std::size_t output_length) -> void;
+ auto ProcessInput(Result* res, uint8_t* input, std::size_t input_len) -> void;
+ auto WriteOutputSamples(Result* res,
+ uint8_t* output,
+ std::size_t output_length) -> void;
- private:
- mad_stream stream_;
- mad_frame frame_;
- mad_synth synth_;
+ private:
+ mad_stream stream_;
+ mad_frame frame_;
+ mad_synth synth_;
- mad_header header_;
- bool has_decoded_header_;
+ mad_header header_;
+ bool has_decoded_header_;
- int current_sample_ = -1;
- };
+ int current_sample_ = -1;
+};
-} // namespace codecs
+} // namespace codecs
diff --git a/src/codecs/include/types.hpp b/src/codecs/include/types.hpp
index 8525a136..a962cf68 100644
--- a/src/codecs/include/types.hpp
+++ b/src/codecs/include/types.hpp
@@ -2,11 +2,11 @@
#include <string>
-namespace codecs {
+namespace codecs {
- enum StreamType {
- STREAM_MP3,
- };
+enum StreamType {
+ STREAM_MP3,
+};
- auto GetStreamTypeFromFilename(std::string filename);
-}
+auto GetStreamTypeFromFilename(std::string filename);
+} // namespace codecs
diff --git a/src/codecs/mad.cpp b/src/codecs/mad.cpp
index f1ed0346..c918e849 100644
--- a/src/codecs/mad.cpp
+++ b/src/codecs/mad.cpp
@@ -5,150 +5,145 @@
namespace codecs {
- static int32_t scaleTo24Bits(mad_fixed_t sample) {
- // Round the bottom bits.
- sample += (1L << (MAD_F_FRACBITS - 24));
-
- // Clip the leftover bits to within range.
- if (sample >= MAD_F_ONE)
- sample = MAD_F_ONE - 1;
- else if (sample < -MAD_F_ONE)
- sample = -MAD_F_ONE;
-
- /* quantize */
- return sample >> (MAD_F_FRACBITS + 1 - 24);
+static int32_t scaleTo24Bits(mad_fixed_t sample) {
+ // Round the bottom bits.
+ sample += (1L << (MAD_F_FRACBITS - 24));
+
+ // Clip the leftover bits to within range.
+ if (sample >= MAD_F_ONE)
+ sample = MAD_F_ONE - 1;
+ else if (sample < -MAD_F_ONE)
+ sample = -MAD_F_ONE;
+
+ /* quantize */
+ return sample >> (MAD_F_FRACBITS + 1 - 24);
+}
+
+MadMp3Decoder::MadMp3Decoder() {
+ mad_stream_init(&stream_);
+ mad_frame_init(&frame_);
+ mad_synth_init(&synth_);
+ mad_header_init(&header_);
+}
+MadMp3Decoder::~MadMp3Decoder() {
+ mad_stream_finish(&stream_);
+ mad_frame_finish(&frame_);
+ mad_synth_finish(&synth_);
+ mad_header_finish(&header_);
+}
+
+auto MadMp3Decoder::CanHandleExtension(std::string extension) -> bool {
+ return extension == "mp3";
+}
+
+auto GetOutputFormat() -> OutputFormat {
+ return OutputFormat{
+ .num_channels = synth_.pcm.channels,
+ .bits_per_sample = 24,
+ .sample_rate_hz = synth_.pcm.samplerate,
+ };
+}
+
+auto MadMp3Decoder::Process(uint8_t* input,
+ std::size_t input_len,
+ uint8_t* output,
+ std::size_t output_length)
+ -> cpp::result<Result, Error> {
+ Result res {
+ .need_more_input = false, .input_processed = 0, .flush_output = false,
+ .output_written = 0,
}
-
- MadMp3Decoder::MadMp3Decoder() {
- mad_stream_init(&stream_);
- mad_frame_init(&frame_);
- mad_synth_init(&synth_);
- mad_header_init(&header_);
- }
- MadMp3Decoder::~MadMp3Decoder() {
- mad_stream_finish(&stream_);
- mad_frame_finish(&frame_);
- mad_synth_finish(&synth_);
- mad_header_finish(&header_);
+ while (true) {
+ // Only process more of the input if we're done sending off the
+ // samples for the previous frame.
+ if (current_sample_ == -1) {
+ ProcessInput(&res, input, input_len);
+ }
+
+ // Write PCM samples to the output buffer. This always needs to be
+ // done, even if we ran out of input, so that we don't keep the last
+ // few samples buffered if the input stream has actually finished.
+ WriteOutputSamples(&res, output, output_length);
+
+ if (res.need_more_input || res.flush_output) {
+ return res;
+ }
}
- auto MadMp3Decoder::CanHandleExtension(std::string extension) -> bool {
- return extension == "mp3";
+ auto MadMp3Decoder::ProcessInput(Result * res, uint8_t * input,
+ std::size_t input_len)
+ ->void {
+ if (input != stream_.buffer) {
+ mad_stream_buffer(&stream_, input, input_len);
+ }
+
+ if (!has_decoded_header_) {
+ // The header of any given frame should be representative of the
+ // entire stream, so only need to read it once.
+ mad_header_decode(&header_, &stream);
+ has_decoded_header_ = true;
+
+ // TODO: Use the info in the header for something. I think the
+ // duration will help with seeking?
+ }
+
+ // Decode the next frame. To signal errors, this returns -1 and
+ // stashes an error code in the stream structure.
+ if (mad_frame_decode(&frame_, &stream_) < 0) {
+ if (MAD_RECOVERABLE(stream_.error)) {
+ // Recoverable errors are usually malformed parts of the stream.
+ // We can recover from them by just retrying the decode.
+ continue;
}
- auto GetOutputFormat() -> OutputFormat {
- return OutputFormat {
- .num_channels = synth_.pcm.channels,
- .bits_per_sample = 24,
- .sample_rate_hz = synth_.pcm.samplerate,
- };
+ if (stream_.error = MAD_ERROR_BUFLEN) {
+ // The decoder ran out of bytes before it completed a frame. We
+ // need to return back to the caller to give us more data. Note
+ // that there might still be some unused data in the input, so we
+ // should calculate that amount and return it.
+ size_t remaining_bytes = stream.bufend - stream_.next_frame;
+ return remaining_bytes;
}
- auto MadMp3Decoder::Process(
- uint8_t *input,
- std::size_t input_len,
- uint8_t *output,
- std::size_t output_length) -> cpp::result<Result, Error> {
-
- Result res {
- .need_more_input = false,
- .input_processed = 0,
- .flush_output = false,
- .output_written = 0,
- }
- while (true) {
- // Only process more of the input if we're done sending off the
- // samples for the previous frame.
- if (current_sample_ == -1) {
- ProcessInput(&res, input, input_len);
- }
-
- // Write PCM samples to the output buffer. This always needs to be
- // done, even if we ran out of input, so that we don't keep the last
- // few samples buffered if the input stream has actually finished.
- WriteOutputSamples(&res, output, output_length);
-
- if (res.need_more_input || res.flush_output) {
- return res;
- }
- }
+ // The error is unrecoverable. Give up.
+ return cpp::fail(MALFORMED_DATA);
+ }
- auto MadMp3Decoder::ProcessInput(
- Result *res, uint8_t *input, std::size_t input_len) -> void {
-
- if (input != stream_.buffer) {
- mad_stream_buffer(&stream_, input, input_len);
- }
-
- if (!has_decoded_header_) {
- // The header of any given frame should be representative of the
- // entire stream, so only need to read it once.
- mad_header_decode(&header_, &stream);
- has_decoded_header_ = true;
-
- // TODO: Use the info in the header for something. I think the
- // duration will help with seeking?
- }
-
-
- // Decode the next frame. To signal errors, this returns -1 and
- // stashes an error code in the stream structure.
- if (mad_frame_decode(&frame_, &stream_) < 0) {
- if (MAD_RECOVERABLE(stream_.error)) {
- // Recoverable errors are usually malformed parts of the stream.
- // We can recover from them by just retrying the decode.
- continue;
- }
-
- if (stream_.error = MAD_ERROR_BUFLEN) {
- // The decoder ran out of bytes before it completed a frame. We
- // need to return back to the caller to give us more data. Note
- // that there might still be some unused data in the input, so we
- // should calculate that amount and return it.
- size_t remaining_bytes = stream.bufend - stream_.next_frame;
- return remaining_bytes;
- }
-
- // The error is unrecoverable. Give up.
- return cpp::fail(MALFORMED_DATA);
- }
-
- // We've successfully decoded a frame!
- // Now we need to synthesize PCM samples based on the frame, and send
- // them downstream.
- mad_synth_frame(&synth_, &frame_);
- up_to_sample = 0;
- }
+ // We've successfully decoded a frame!
+ // Now we need to synthesize PCM samples based on the frame, and send
+ // them downstream.
+ mad_synth_frame(&synth_, &frame_);
+ up_to_sample = 0;
+ }
- auto MadMp3Decoder::WriteOutputSamples(
- Result *res,
- uint8_t *output,
- std::size_t output_length) -> void {
- size_t output_byte = 0;
- // First ensure that we actually have some samples to send off.
- if (current_sample_ < 0) {
- return;
- }
- res->flush_output = true;
-
- while (current_sample_ < synth_.pcm.length) {
- if (output_byte + (3 * synth_.pcm.channels) >= output_length) {
- res->output_written = output_byte;
- return;
- }
-
- for (int channel = 0; channel < synth_.pcm.channels; channel++) {
- uint32_t sample_24 = scaleTo24Bits(synth_.pcm.samples[channel][sample]);
- output[output_byte++] = (sample_24 >> 0) & 0xff;
- output[output_byte++] = (sample_24 >> 8) & 0xff;
- output[output_byte++] = (sample_24 >> 16) & 0xff;
- }
- current_sample_++;
- }
-
- // We wrote everything! Reset, ready for the next frame.
- current_sample_ = -1;
+ auto MadMp3Decoder::WriteOutputSamples(Result * res, uint8_t * output,
+ std::size_t output_length)
+ ->void {
+ size_t output_byte = 0;
+ // First ensure that we actually have some samples to send off.
+ if (current_sample_ < 0) {
+ return;
+ }
+ res->flush_output = true;
+
+ while (current_sample_ < synth_.pcm.length) {
+ if (output_byte + (3 * synth_.pcm.channels) >= output_length) {
res->output_written = output_byte;
+ return;
}
-} // namespace codecs
+ for (int channel = 0; channel < synth_.pcm.channels; channel++) {
+ uint32_t sample_24 = scaleTo24Bits(synth_.pcm.samples[channel][sample]);
+ output[output_byte++] = (sample_24 >> 0) & 0xff;
+ output[output_byte++] = (sample_24 >> 8) & 0xff;
+ output[output_byte++] = (sample_24 >> 16) & 0xff;
+ }
+ current_sample_++;
+ }
+
+ // We wrote everything! Reset, ready for the next frame.
+ current_sample_ = -1;
+ res->output_written = output_byte;
+ }
+
+} // namespace codecs