summaryrefslogtreecommitdiff
path: root/src/codecs
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-12-03 11:10:06 +1100
committerjacqueline <me@jacqueline.id.au>2022-12-03 11:10:06 +1100
commit16d5d29049c08e21f57f7928ceedf40586a2d294 (patch)
treea4647f951f90b036c58c879ae186fa7e452ed950 /src/codecs
parent00d4883d3a683eaf9cfaefacdd81434e0974ad13 (diff)
downloadtangara-fw-16d5d29049c08e21f57f7928ceedf40586a2d294.tar.gz
Use std::span (backported) and std::byte to make our buffers safer
Diffstat (limited to 'src/codecs')
-rw-r--r--src/codecs/CMakeLists.txt2
-rw-r--r--src/codecs/include/codec.hpp5
-rw-r--r--src/codecs/include/mad.hpp5
-rw-r--r--src/codecs/mad.cpp17
4 files changed, 16 insertions, 13 deletions
diff --git a/src/codecs/CMakeLists.txt b/src/codecs/CMakeLists.txt
index 4f89f370..9677e459 100644
--- a/src/codecs/CMakeLists.txt
+++ b/src/codecs/CMakeLists.txt
@@ -1,7 +1,7 @@
idf_component_register(
SRCS "codec.cpp" "mad.cpp"
INCLUDE_DIRS "include"
- REQUIRES "result")
+ REQUIRES "result" "span")
add_dependencies("${COMPONENT_LIB}" libmad)
target_compile_options("${COMPONENT_LIB}" PRIVATE ${EXTRA_WARNINGS})
diff --git a/src/codecs/include/codec.hpp b/src/codecs/include/codec.hpp
index 764b63fc..bbb621f5 100644
--- a/src/codecs/include/codec.hpp
+++ b/src/codecs/include/codec.hpp
@@ -9,6 +9,7 @@
#include <utility>
#include "result.hpp"
+#include "span.hpp"
namespace codecs {
@@ -30,7 +31,7 @@ class ICodec {
virtual auto ResetForNewStream() -> void = 0;
- virtual auto SetInput(uint8_t* buffer, std::size_t length) -> void = 0;
+ virtual auto SetInput(cpp::span<std::byte> input) -> void = 0;
/*
* Returns the codec's next read position within the input buffer. If the
@@ -56,7 +57,7 @@ class ICodec {
* written. If this returns false, then this method should be called again
* after flushing the output buffer.
*/
- virtual auto WriteOutputSamples(uint8_t* output, std::size_t output_length)
+ virtual auto WriteOutputSamples(cpp::span<std::byte> output)
-> std::pair<std::size_t, bool> = 0;
};
diff --git a/src/codecs/include/mad.hpp b/src/codecs/include/mad.hpp
index aa24f3c9..6077314c 100644
--- a/src/codecs/include/mad.hpp
+++ b/src/codecs/include/mad.hpp
@@ -5,6 +5,7 @@
#include <string>
#include "mad.h"
+#include "span.hpp"
#include "codec.hpp"
@@ -18,10 +19,10 @@ class MadMp3Decoder : public ICodec {
auto CanHandleFile(const std::string& path) -> bool override;
auto GetOutputFormat() -> OutputFormat override;
auto ResetForNewStream() -> void override;
- auto SetInput(uint8_t* buffer, std::size_t length) -> void override;
+ auto SetInput(cpp::span<std::byte> input) -> void override;
auto GetInputPosition() -> std::size_t override;
auto ProcessNextFrame() -> cpp::result<bool, ProcessingError> override;
- auto WriteOutputSamples(uint8_t* output, std::size_t output_length)
+ auto WriteOutputSamples(cpp::span<std::byte> output)
-> std::pair<std::size_t, bool> override;
private:
diff --git a/src/codecs/mad.cpp b/src/codecs/mad.cpp
index 4afc9a77..a8c2fb11 100644
--- a/src/codecs/mad.cpp
+++ b/src/codecs/mad.cpp
@@ -51,8 +51,10 @@ auto MadMp3Decoder::ResetForNewStream() -> void {
has_decoded_header_ = false;
}
-auto MadMp3Decoder::SetInput(uint8_t* buffer, std::size_t length) -> void {
- mad_stream_buffer(&stream_, buffer, length);
+auto MadMp3Decoder::SetInput(cpp::span<std::byte> input) -> void {
+ mad_stream_buffer(&stream_,
+ reinterpret_cast<const unsigned char*>(input.data()),
+ input.size());
}
auto MadMp3Decoder::GetInputPosition() -> std::size_t {
@@ -101,8 +103,7 @@ auto MadMp3Decoder::ProcessNextFrame() -> cpp::result<bool, ProcessingError> {
return false;
}
-auto MadMp3Decoder::WriteOutputSamples(uint8_t* output,
- std::size_t output_length)
+auto MadMp3Decoder::WriteOutputSamples(cpp::span<std::byte> output)
-> std::pair<std::size_t, bool> {
size_t output_byte = 0;
// First ensure that we actually have some samples to send off.
@@ -111,16 +112,16 @@ auto MadMp3Decoder::WriteOutputSamples(uint8_t* output,
}
while (current_sample_ < synth_.pcm.length) {
- if (output_byte + (3 * synth_.pcm.channels) >= output_length) {
+ if (output_byte + (3 * synth_.pcm.channels) >= output.size()) {
return std::make_pair(output_byte, false);
}
for (int channel = 0; channel < synth_.pcm.channels; channel++) {
uint32_t sample_24 =
scaleTo24Bits(synth_.pcm.samples[channel][current_sample_]);
- output[output_byte++] = (sample_24 >> 0) & 0xff;
- output[output_byte++] = (sample_24 >> 8) & 0xff;
- output[output_byte++] = (sample_24 >> 16) & 0xff;
+ output[output_byte++] = static_cast<std::byte>(sample_24 >> 0);
+ output[output_byte++] = static_cast<std::byte>(sample_24 >> 8);
+ output[output_byte++] = static_cast<std::byte>(sample_24 >> 16);
}
current_sample_++;
}