diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2025-05-31 22:39:38 +0200 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2025-05-31 22:39:38 +0200 |
commit | 1c7f2f821fb965af468cdf2a14df3ff75cc1c352 (patch) | |
tree | 1bc92237122b75c67afc326af207cf3cc9eb3d6c /src/lib/bs_encode/encode_ut.cpp | |
parent | 272af27a3d148bd13e8f15640e53ca70c64ccb9b (diff) | |
parent | 6dfc60e9d4791c3385908c61ad75c4a0093ea1eb (diff) | |
download | atracdenc-1c7f2f821fb965af468cdf2a14df3ff75cc1c352.tar.gz |
Merge branch 'at3plus-dev'
It looks like we are able to encode ATRAC3PLUS compatible bitstream so we can merge at3p development branch in to the main branch.
Current limitation for AT3P mode:
- Only 352 Kbps (proper bit allocation and some psychoacoustic must be implemented)
- GHA sometime works with error (but huge bitrate hide it)
- No VLC, VQ, delta encoding
- No noise substitution
- No gain control
- No window shape switching
Diffstat (limited to 'src/lib/bs_encode/encode_ut.cpp')
-rw-r--r-- | src/lib/bs_encode/encode_ut.cpp | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/src/lib/bs_encode/encode_ut.cpp b/src/lib/bs_encode/encode_ut.cpp new file mode 100644 index 0000000..39f0ff1 --- /dev/null +++ b/src/lib/bs_encode/encode_ut.cpp @@ -0,0 +1,178 @@ +/* + * 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 "encode.h" +#include <gtest/gtest.h> +#include <cmath> +#include <memory> +#include <bitstream/bitstream.h> + +using namespace NAtracDEnc; + +static size_t SomeBitFn1(float lambda) { + return sqrt(lambda * (-1.0f)) * 300; +} + +static size_t SomeBitFn2(float lambda) { + return 1 + (SomeBitFn1(lambda) & (~(size_t)7)); +} + +class TPartEncoder1 : public IBitStreamPartEncoder { +public: + explicit TPartEncoder1(size_t expCalls) + : ExpCalls(expCalls) + {} + + EStatus Encode(void* frameData, TBitAllocHandler& ba) override { + EncCalls++; + ba.Start(1000, -15, -1); + return EStatus::Ok; + } + + void Dump(NBitStream::TBitStream& bs) override { + EXPECT_EQ(EncCalls, ExpCalls); + } + + uint32_t GetConsumption() const noexcept override { + return 0; + } +private: + const size_t ExpCalls; + size_t EncCalls = 0; +}; + +template<size_t (*F)(float)> +class TPartEncoder2 : public IBitStreamPartEncoder { +public: + explicit TPartEncoder2(size_t expCalls) + : ExpCalls(expCalls) + {} + + EStatus Encode(void* frameData, TBitAllocHandler& ba) override { + EncCalls++; + auto lambda = ba.Continue(); + auto bits = F(lambda); + ba.Submit(bits); + Bits = bits; + return EStatus::Ok; + } + + void Dump(NBitStream::TBitStream& bs) override { + EXPECT_EQ(EncCalls, ExpCalls); + for (size_t i = 0; i < Bits; i++) { + bs.Write(1, 1); + } + } + + uint32_t GetConsumption() const noexcept override { + return 1 * Bits; + } +private: + const size_t ExpCalls; + size_t EncCalls = 0; + size_t Bits = 0; +}; + +class TPartEncoder3 : public IBitStreamPartEncoder { +public: + explicit TPartEncoder3(size_t expCalls) + : ExpCalls(expCalls) + {} + + EStatus Encode(void* frameData, TBitAllocHandler& ba) override { + EncCalls++; + return EStatus::Ok; + } + + void Dump(NBitStream::TBitStream& bs) override { + EXPECT_EQ(EncCalls, ExpCalls); + } + + uint32_t GetConsumption() const noexcept override { + return 0; + } +private: + const size_t ExpCalls; + size_t EncCalls = 0; +}; + +class TPartEncoder4 : public IBitStreamPartEncoder { +public: + TPartEncoder4() = default; + + EStatus Encode(void* frameData, TBitAllocHandler& ba) override { + if (EncCalls == 0) { + EncCalls++; + return EStatus::Repeat; + } + + return EStatus::Ok; + } + + void Dump(NBitStream::TBitStream& bs) override { + EXPECT_EQ(EncCalls, 1); + } + + uint32_t GetConsumption() const noexcept override { + return 0; + } +private: + size_t EncCalls = 0; +}; + +TEST(BsEncode, SimpleAlloc) { + std::vector<IBitStreamPartEncoder::TPtr> encoders; + encoders.emplace_back(std::make_unique<TPartEncoder1>(1)); + encoders.emplace_back(std::make_unique<TPartEncoder2<SomeBitFn1>>(8)); + encoders.emplace_back(std::make_unique<TPartEncoder3>(1)); + + NBitStream::TBitStream bs; + TBitStreamEncoder encoder(std::move(encoders)); + encoder.Do(nullptr, bs); + + EXPECT_EQ(bs.GetSizeInBits(), 1000); +} + +TEST(BsEncode, AllocWithRepeat) { + std::vector<IBitStreamPartEncoder::TPtr> encoders; + encoders.emplace_back(std::make_unique<TPartEncoder1>(2)); + encoders.emplace_back(std::make_unique<TPartEncoder2<SomeBitFn1>>(16)); + encoders.emplace_back(std::make_unique<TPartEncoder3>(2)); + encoders.emplace_back(std::make_unique<TPartEncoder4>()); + + NBitStream::TBitStream bs; + TBitStreamEncoder encoder(std::move(encoders)); + encoder.Do(nullptr, bs); + + EXPECT_EQ(bs.GetSizeInBits(), 1000); +} + +TEST(BsEncode, NotExactAlloc) { + std::vector<IBitStreamPartEncoder::TPtr> encoders; + encoders.emplace_back(std::make_unique<TPartEncoder1>(1)); + encoders.emplace_back(std::make_unique<TPartEncoder2<SomeBitFn2>>(11)); + encoders.emplace_back(std::make_unique<TPartEncoder3>(1)); + + NBitStream::TBitStream bs; + TBitStreamEncoder encoder(std::move(encoders)); + encoder.Do(nullptr, bs); + + EXPECT_EQ(bs.GetSizeInBits(), 993); +} + + |