From b31bc07555fdd862181d8d6ed551163cea89bc62 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 13 Feb 2024 16:39:56 +1100 Subject: fix (improve?) libtremor strangeness something fucky --- src/codecs/include/vorbis.hpp | 2 +- src/codecs/vorbis.cpp | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) (limited to 'src/codecs') diff --git a/src/codecs/include/vorbis.hpp b/src/codecs/include/vorbis.hpp index 2f93c37e..673b67a0 100644 --- a/src/codecs/include/vorbis.hpp +++ b/src/codecs/include/vorbis.hpp @@ -41,7 +41,7 @@ class TremorVorbisDecoder : public ICodec { private: std::shared_ptr input_; - OggVorbis_File vorbis_; + std::unique_ptr vorbis_; }; } // namespace codecs diff --git a/src/codecs/vorbis.cpp b/src/codecs/vorbis.cpp index 3b3798cb..c373ebf5 100644 --- a/src/codecs/vorbis.cpp +++ b/src/codecs/vorbis.cpp @@ -78,17 +78,21 @@ static const ov_callbacks kCallbacks{ .tell_func = tell_cb, // Not seekable }; -TremorVorbisDecoder::TremorVorbisDecoder() : input_(), vorbis_() {} +TremorVorbisDecoder::TremorVorbisDecoder() + : input_(), + vorbis_(reinterpret_cast( + heap_caps_malloc(sizeof(OggVorbis_File), + MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT))) {} TremorVorbisDecoder::~TremorVorbisDecoder() { - ov_clear(&vorbis_); + ov_clear(vorbis_.get()); } auto TremorVorbisDecoder::OpenStream(std::shared_ptr input) -> cpp::result { - int res = ov_open_callbacks(input.get(), &vorbis_, NULL, 0, kCallbacks); + int res = ov_open_callbacks(input.get(), vorbis_.get(), NULL, 0, kCallbacks); if (res < 0) { - std::pmr::string err; + std::string err; switch (res) { case OV_EREAD: err = "OV_EREAD"; @@ -112,13 +116,13 @@ auto TremorVorbisDecoder::OpenStream(std::shared_ptr input) return cpp::fail(Error::kMalformedData); } - vorbis_info* info = ov_info(&vorbis_, -1); + vorbis_info* info = ov_info(vorbis_.get(), -1); if (info == NULL) { ESP_LOGE(kTag, "failed to get stream info"); return cpp::fail(Error::kMalformedData); } - auto l = ov_pcm_total(&vorbis_, -1); + auto l = ov_pcm_total(vorbis_.get(), -1); std::optional length; if (l > 0) { length = l * info->channels; @@ -133,9 +137,10 @@ auto TremorVorbisDecoder::OpenStream(std::shared_ptr input) auto TremorVorbisDecoder::DecodeTo(cpp::span output) -> cpp::result { - int bitstream = 0; - long bytes_written = ov_read(&vorbis_, reinterpret_cast(output.data()), - output.size_bytes(), &bitstream); + int unused = 0; + long bytes_written = + ov_read(vorbis_.get(), reinterpret_cast(output.data()), + ((output.size() - 1) * sizeof(sample::Sample)), &unused); if (bytes_written == OV_HOLE) { ESP_LOGE(kTag, "got OV_HOLE"); return cpp::fail(Error::kMalformedData); @@ -152,7 +157,7 @@ auto TremorVorbisDecoder::DecodeTo(cpp::span output) } auto TremorVorbisDecoder::SeekTo(size_t target) -> cpp::result { - if (ov_pcm_seek(&vorbis_, target) != 0) { + if (ov_pcm_seek(vorbis_.get(), target) != 0) { return cpp::fail(Error::kInternalError); } return {}; -- cgit v1.2.3 From 7ec0ff2589ffd5774e78f9e6b436ea55be45deb1 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 14 Feb 2024 12:21:33 +1100 Subject: Switch to the lowmem tremor branch in addition to using slightly less memory, this branch also doesn't seem to have the same issues with `-O2` builds that the main branch has. --- src/codecs/include/vorbis.hpp | 4 +--- src/codecs/vorbis.cpp | 25 +++++++------------------ 2 files changed, 8 insertions(+), 21 deletions(-) (limited to 'src/codecs') diff --git a/src/codecs/include/vorbis.hpp b/src/codecs/include/vorbis.hpp index 673b67a0..b96a0407 100644 --- a/src/codecs/include/vorbis.hpp +++ b/src/codecs/include/vorbis.hpp @@ -14,8 +14,6 @@ #include #include "ivorbisfile.h" -#include "ogg/ogg.h" -#include "opus.h" #include "sample.hpp" #include "span.hpp" @@ -41,7 +39,7 @@ class TremorVorbisDecoder : public ICodec { private: std::shared_ptr input_; - std::unique_ptr vorbis_; + std::unique_ptr vorbis_; }; } // namespace codecs diff --git a/src/codecs/vorbis.cpp b/src/codecs/vorbis.cpp index c373ebf5..7fb53f1b 100644 --- a/src/codecs/vorbis.cpp +++ b/src/codecs/vorbis.cpp @@ -4,31 +4,20 @@ * SPDX-License-Identifier: GPL-3.0-only */ -#include "ivorbiscodec.h" -#include "ivorbisfile.h" -#include "ogg/config_types.h" -#include "opus.hpp" - -#include -#include +#include "vorbis.hpp" #include #include #include #include "esp_heap_caps.h" -#include "mad.h" +#include "esp_log.h" +#include "ivorbiscodec.h" +#include "ivorbisfile.h" #include "codec.hpp" -#include "esp_log.h" -#include "ogg/ogg.h" -#include "opus.h" -#include "opus_defines.h" -#include "opus_types.h" -#include "result.hpp" #include "sample.hpp" #include "types.hpp" -#include "vorbis.hpp" namespace codecs { @@ -39,7 +28,7 @@ static size_t read_cb(void* ptr, size_t size, size_t nmemb, void* instance) { return source->Read({reinterpret_cast(ptr), size * nmemb}); } -static int seek_cb(void* instance, ogg_int64_t offset, int whence) { +static int seek_cb(void* instance, tremor_ogg_int64_t offset, int whence) { IStream* source = reinterpret_cast(instance); if (!source->CanSeek()) { return -1; @@ -80,8 +69,8 @@ static const ov_callbacks kCallbacks{ TremorVorbisDecoder::TremorVorbisDecoder() : input_(), - vorbis_(reinterpret_cast( - heap_caps_malloc(sizeof(OggVorbis_File), + vorbis_(reinterpret_cast( + heap_caps_malloc(sizeof(TremorOggVorbis_File), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT))) {} TremorVorbisDecoder::~TremorVorbisDecoder() { -- cgit v1.2.3 From c5917658e6a0fcc77971237c80bb4e47f3e8bf9e Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 14 Feb 2024 17:48:13 +1100 Subject: Cram one of the flac samples buffers into internal ram Can't quite fit the second... yet. Just one is a pretty reasonable speedup, though! Probably bc we're not hammering the spiram cache so hard. --- src/codecs/miniflac.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/codecs') diff --git a/src/codecs/miniflac.cpp b/src/codecs/miniflac.cpp index ace73466..74eafb3b 100644 --- a/src/codecs/miniflac.cpp +++ b/src/codecs/miniflac.cpp @@ -30,9 +30,16 @@ MiniFlacDecoder::MiniFlacDecoder() current_sample_() { miniflac_init(flac_.get(), MINIFLAC_CONTAINER_UNKNOWN); for (int i = 0; i < samples_by_channel_.size(); i++) { - // Full decoded frames too big to fit in internal ram :( + uint32_t caps; + if (i == 0) { + caps = MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL; + } else { + // FIXME: We can *almost* fit two channels into internal ram, but we're a + // few KiB shy of being able to do it safely. + caps = MALLOC_CAP_SPIRAM; + } samples_by_channel_[i] = reinterpret_cast( - heap_caps_malloc(kMaxFrameSize * sizeof(int32_t), MALLOC_CAP_SPIRAM)); + heap_caps_malloc(kMaxFrameSize * sizeof(int32_t), caps)); } } -- cgit v1.2.3