summaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac_enc_cache.cpp
diff options
context:
space:
mode:
authorDaniil Cherednik <[email protected]>2026-06-25 23:48:15 +0200
committerDaniil Cherednik <[email protected]>2026-06-25 23:48:15 +0200
commitbd270db9f2efca6d71959f1e60bc4f3a6867a03a (patch)
treecb95db5a729a9438e5b6a71d30cf7b3bd6cf5bbb /src/atrac/atrac_enc_cache.cpp
parent01234b09a2fb3e7fc6746ace7554383b0caa97e1 (diff)
Add BFU encode cache and use it in ATRAC3 bit allocationatrac3_enc_cache
Introduce a generic TEncCache/TUnit library (src/atrac/atrac_enc_cache.*) that memoizes per-unit quantization results keyed by a user-supplied function. Units are owned via unique_ptr in a flat vector pre-sized to a known key bound; the codec supplies both the unit factory (which also performs the computation, avoiding an extra virtual call on the hot path) and the key packing. Wire it into the ATRAC3 allocator: during the lambda binary search the same (bfu, wordlen) block is quantized repeatedly. TAt3SpecUnit now caches the mantissas plus the CLC/VLC spectrum costs and energy error, so each (bfu, wordlen) is computed once per channel. The cache is reset in TAlloc::Dump (once per channel) so no stale spectrum is reused. Output is bit-exact with the previous encoder; ~36% faster encode.
Diffstat (limited to 'src/atrac/atrac_enc_cache.cpp')
-rw-r--r--src/atrac/atrac_enc_cache.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/atrac/atrac_enc_cache.cpp b/src/atrac/atrac_enc_cache.cpp
new file mode 100644
index 0000000..2dc781c
--- /dev/null
+++ b/src/atrac/atrac_enc_cache.cpp
@@ -0,0 +1,51 @@
+/*
+ * This file is part of AtracDEnc.
+ *
+ * AtracDEnc is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * AtracDEnc is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with AtracDEnc; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "atrac_enc_cache.h"
+
+namespace NAtracDEnc {
+
+TEncCache::TEncCache(size_t numKeys, TProvideUnit provideUnit, TMakeKey makeKey, void* opaque)
+ : UnitBuffers(numKeys)
+ , ProvideUnit(provideUnit)
+ , MakeKey(makeKey)
+ , Opaque(opaque)
+{
+}
+
+TUnit* TEncCache::GetOrCompute(size_t ch, size_t bfu, size_t wordlen, const float* values)
+{
+ const size_t key = MakeKey(ch, bfu, wordlen);
+
+ std::unique_ptr<TUnit>& slot = UnitBuffers[key];
+ if (!slot) {
+ slot.reset(ProvideUnit(ch, bfu, wordlen, values, Opaque));
+ }
+
+ return slot.get();
+}
+
+void TEncCache::Reset()
+{
+ // Keep the vector sized; just drop the cached units for the next frame.
+ for (std::unique_ptr<TUnit>& slot : UnitBuffers) {
+ slot.reset();
+ }
+}
+
+} // namespace NAtracDEnc