diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2016-06-19 02:58:23 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2016-06-19 03:31:55 +0300 |
commit | 1151d5831f19a9f24dd0c545a4968606712a62d2 (patch) | |
tree | c978c1b9a3fc86fef531dd412fe6b7668b7c0567 /src | |
parent | 8d65a0bd0774e03b3d10354e15f2f3361a2ce26a (diff) | |
download | atracdenc-1151d5831f19a9f24dd0c545a4968606712a62d2.tar.gz |
some improvements of ATRAC3 implementation:atrac3
- simple (ATRAC1 like) psychoacoustic added
- possibility to encode tonal components
- simple tonal component extractor
- refactoring
Diffstat (limited to 'src')
39 files changed, 1454 insertions, 533 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7a30576..c32401b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +#add_definitions( "-Wall -O2 -g -Rpass-analysis=loop-vectorize" ) +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address -fno-omit-frame-pointer") + add_definitions( "-Wall -O2 -g" ) set(CMAKE_CXX_STANDARD 11) @@ -20,11 +23,12 @@ set(SOURCE_EXE wav.cpp aea.cpp transient_detector.cpp - atracdenc.cpp + atrac1denc.cpp bitstream/bitstream.cpp atrac/atrac1.cpp atrac/atrac1_dequantiser.cpp atrac/atrac_scale.cpp + atrac/atrac_psy_common.cpp atrac/atrac1_bitalloc.cpp oma.cpp atrac3denc.cpp @@ -48,5 +48,4 @@ public: long long GetLengthInSamples() const override; }; -typedef std::unique_ptr<IAtrac1IO> TAeaPtr; diff --git a/src/atrac/atrac1.cpp b/src/atrac/atrac1.cpp index 26d8218..b71e5ae 100644 --- a/src/atrac/atrac1.cpp +++ b/src/atrac/atrac1.cpp @@ -1,10 +1,15 @@ #include "atrac1.h" -constexpr uint32_t TAtrac1Data::BlocksPerBand[QMF_BANDS + 1]; -constexpr uint32_t TAtrac1Data::SpecsPerBlock[MAX_BFUS]; -constexpr uint32_t TAtrac1Data::SpecsStartLong[MAX_BFUS]; -constexpr uint32_t TAtrac1Data::SpecsStartShort[MAX_BFUS]; +namespace NAtracDEnc { +namespace NAtrac1 { + +constexpr uint32_t TAtrac1Data::BlocksPerBand[NumQMF + 1]; +constexpr uint32_t TAtrac1Data::SpecsPerBlock[MaxBfus]; +constexpr uint32_t TAtrac1Data::SpecsStartLong[MaxBfus]; +constexpr uint32_t TAtrac1Data::SpecsStartShort[MaxBfus]; constexpr uint32_t TAtrac1Data::BfuAmountTab[8]; double TAtrac1Data::ScaleTable[64] = {0}; double TAtrac1Data::SineWindow[32] = {0}; +} //namespace NAtrac1 +} //namespace NAtracDEnc diff --git a/src/atrac/atrac1.h b/src/atrac/atrac1.h index cb0df1f..9736ea7 100644 --- a/src/atrac/atrac1.h +++ b/src/atrac/atrac1.h @@ -4,23 +4,52 @@ #include <map> #include <math.h> #include "../bitstream/bitstream.h" -const int QMF_BANDS = 3; -const int MAX_BFUS = 52; +namespace NAtracDEnc { +namespace NAtrac1 { + +class TAtrac1EncodeSettings { +public: + enum class EWindowMode { + EWM_NOTRANSIENT, + EWM_AUTO + }; +private: + const uint32_t BfuIdxConst = 0; + const bool FastBfuNumSearch = false; + EWindowMode WindowMode = EWindowMode::EWM_AUTO; + const uint32_t WindowMask = 0; +public: + TAtrac1EncodeSettings() + {} + TAtrac1EncodeSettings(uint32_t bfuIdxConst, bool fastBfuNumSearch, EWindowMode windowMode, uint32_t windowMask) + : BfuIdxConst(bfuIdxConst) + , FastBfuNumSearch(fastBfuNumSearch) + , WindowMode(windowMode) + , WindowMask(windowMask) + {} + uint32_t GetBfuIdxConst() const { return BfuIdxConst; } + bool GetFastBfuNumSearch() const { return FastBfuNumSearch; } + EWindowMode GetWindowMode() const {return WindowMode; } + uint32_t GetWindowMask() const {return WindowMask; } +}; class TAtrac1Data { +public: + static constexpr uint8_t MaxBfus = 52; + static constexpr uint8_t NumQMF = 3; protected: - static constexpr uint32_t SpecsPerBlock[MAX_BFUS] = { + static constexpr uint32_t SpecsPerBlock[MaxBfus] = { 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 6, 6, 6, 6, 6, 6, 6, 6, // low band 6, 6, 6, 6, 7, 7, 7, 7, 9, 9, 9, 9, 10, 10, 10, 10, // middle band 12, 12, 12, 12, 12, 12, 12, 12, 20, 20, 20, 20, 20, 20, 20, 20 // high band }; - static constexpr uint32_t BlocksPerBand[QMF_BANDS + 1] = {0, 20, 36, 52}; - static constexpr uint32_t SpecsStartLong[MAX_BFUS] = { + static constexpr uint32_t BlocksPerBand[NumQMF + 1] = {0, 20, 36, 52}; + static constexpr uint32_t SpecsStartLong[MaxBfus] = { 0, 8, 16, 24, 32, 36, 40, 44, 48, 56, 64, 72, 80, 86, 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 159, 166, 173, 180, 189, 198, 207, 216, 226, 236, 246, 256, 268, 280, 292, 304, 316, 328, 340, 352, 372, 392, 412, 432, 452, 472, 492, }; - static constexpr uint32_t SpecsStartShort[MAX_BFUS] = { + static constexpr uint32_t SpecsStartShort[MaxBfus] = { 0, 32, 64, 96, 8, 40, 72, 104, 12, 44, 76, 108, 20, 52, 84, 116, 26, 58, 90, 122, 128, 160, 192, 224, 134, 166, 198, 230, 141, 173, 205, 237, 150, 182, 214, 246, 256, 288, 320, 352, 384, 416, 448, 480, 268, 300, 332, 364, 396, 428, 460, 492 @@ -30,8 +59,6 @@ protected: static const uint32_t BitsPerBfuAmountTabIdx = 3; static const uint32_t BitsPerIDWL = 4; static const uint32_t BitsPerIDSF = 6; - static const uint32_t NumSamples = 512; - static const uint8_t NumQMF = QMF_BANDS; static double ScaleTable[64]; static double SineWindow[32]; @@ -43,6 +70,7 @@ protected: return 2; } public: + static const uint32_t NumSamples = 512; TAtrac1Data() { if (ScaleTable[0] == 0) { for (uint32_t i = 0; i < 64; i++) { @@ -57,30 +85,5 @@ public: } }; -class TBlockSize { - static std::array<int, QMF_BANDS> Parse(NBitStream::TBitStream* stream) { - std::array<int,QMF_BANDS> tmp; - tmp[0] = 2 - stream->Read(2); - tmp[1] = 2 - stream->Read(2); - tmp[2] = 3 - stream->Read(2); - stream->Read(2); //skip unused 2 bits - return tmp; - } - static std::array<int,QMF_BANDS> Create(bool lowShort, bool midShort, bool hiShort) { - std::array<int,QMF_BANDS> tmp; - tmp[0] = lowShort ? 2 : 0; - tmp[1] = midShort ? 2 : 0; - tmp[2] = hiShort ? 3 : 0; - return tmp; - } -public: - TBlockSize(NBitStream::TBitStream* stream) - : LogCount(Parse(stream)) - {} - TBlockSize(bool lowShort, bool midShort, bool hiShort) - : LogCount(Create(lowShort, midShort, hiShort)) - {} - const std::array<int,QMF_BANDS> LogCount; -}; - - +} //namespace NAtrac1 +} //namespace NAtracDEnc diff --git a/src/atrac/atrac1_bitalloc.cpp b/src/atrac/atrac1_bitalloc.cpp index 68af205..11cbc4f 100644 --- a/src/atrac/atrac1_bitalloc.cpp +++ b/src/atrac/atrac1_bitalloc.cpp @@ -1,9 +1,12 @@ #include "atrac1_bitalloc.h" +#include "atrac_psy_common.h" #include "atrac_scale.h" #include "atrac1.h" #include <math.h> #include <cassert> #include "../bitstream/bitstream.h" + +namespace NAtracDEnc { namespace NAtrac1 { using std::vector; @@ -11,47 +14,26 @@ using std::cerr; using std::endl; using std::pair; -static const uint32_t FixedBitAllocTableLong[MAX_BFUS] = { +static const uint32_t FixedBitAllocTableLong[TAtrac1BitStreamWriter::MaxBfus] = { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1, 1, 0, 0, 0 }; -static const uint32_t FixedBitAllocTableShort[MAX_BFUS] = { +static const uint32_t FixedBitAllocTableShort[TAtrac1BitStreamWriter::MaxBfus] = { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 }; -static const uint32_t BitBoostMask[MAX_BFUS] = { +static const uint32_t BitBoostMask[TAtrac1BitStreamWriter::MaxBfus] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -//returns 1 for tone-like, 0 - noise-like -static double AnalizeSpread(const std::vector<TScaledBlock>& scaledBlocks) { - double s = 0.0; - for (int i = 0; i < scaledBlocks.size(); ++i) { - s += scaledBlocks[i].ScaleFactorIndex; - } - s /= scaledBlocks.size(); - double sigma = 0.0; - double xxx = 0.0; - for (int i = 0; i < scaledBlocks.size(); ++i) { - xxx = (scaledBlocks[i].ScaleFactorIndex - s); - xxx *= xxx; - sigma += xxx; - } - sigma /= scaledBlocks.size(); - sigma = sqrt(sigma); - if (sigma > 14.0) - sigma = 14.0; - return sigma/14.0; -} - TBitsBooster::TBitsBooster() { - for (uint32_t i = 0; i < MAX_BFUS; ++i) { + for (uint32_t i = 0; i < MaxBfus; ++i) { if (BitBoostMask[i] == 0) continue; const uint32_t nBits = SpecsPerBlock[i]; @@ -68,7 +50,6 @@ uint32_t TBitsBooster::ApplyBoost(std::vector<uint32_t>* bitsPerEachBlock, uint3 //the key too low if (maxIt == BitsBoostMap.begin()) return surplus; - //std::cout << "key: " << key << " min key: " << MinKey << " it pos: " << maxIt->first << endl; while (surplus >= MinKey) { bool done = true; @@ -76,7 +57,6 @@ uint32_t TBitsBooster::ApplyBoost(std::vector<uint32_t>* bitsPerEachBlock, uint3 const uint32_t curBits = it->first; const uint32_t curPos = it->second; - //std::cout << "key: " << key << " curBits: " << curBits << endl; assert(key >= curBits); if (curPos >= bitsPerEachBlock->size()) break; @@ -90,23 +70,25 @@ uint32_t TBitsBooster::ApplyBoost(std::vector<uint32_t>* bitsPerEachBlock, uint3 (*bitsPerEachBlock)[curPos] += nBitsPerSpec; surplus -= curBits * nBitsPerSpec; - //std::cout << "added: " << curPos << " " << nBitsPerSpec << " got: " << (*bitsPerEachBlock)[curPos] << endl; done = false; } if (done) break; } - //std::cout << "boost: " << surplus << " was " << target - cur << endl; return surplus; } -vector<uint32_t> TAtrac1SimpleBitAlloc::CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, const uint32_t bfuNum, const double spread, const double shift, const TBlockSize& blockSize) { +vector<uint32_t> TAtrac1SimpleBitAlloc::CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, + const uint32_t bfuNum, + const TFloat spread, + const TFloat shift, + const TBlockSize& blockSize) { vector<uint32_t> bitsPerEachBlock(bfuNum); for (int i = 0; i < bitsPerEachBlock.size(); ++i) { const uint32_t fix = blockSize.LogCount[BfuToBand(i)] ? FixedBitAllocTableShort[i] : FixedBitAllocTableLong[i]; - int tmp = spread * ( (double)scaledBlocks[i].ScaleFactorIndex/3.2) + (1.0 - spread) * fix - shift; + int tmp = spread * ( (TFloat)scaledBlocks[i].ScaleFactorIndex/3.2) + (1.0 - spread) * fix - shift; if (tmp > 16) { bitsPerEachBlock[i] = 16; } else if (tmp < 2) { @@ -143,7 +125,8 @@ uint32_t TAtrac1SimpleBitAlloc::GetMaxUsedBfuId(const vector<uint32_t>& bitsPerE return idx; } -uint32_t TAtrac1SimpleBitAlloc::CheckBfuUsage(bool* changed, uint32_t curBfuId, const vector<uint32_t>& bitsPerEachBlock) { +uint32_t TAtrac1SimpleBitAlloc::CheckBfuUsage(bool* changed, + uint32_t curBfuId, const vector<uint32_t>& bitsPerEachBlock) { uint32_t usedBfuId = GetMaxUsedBfuId(bitsPerEachBlock); if (usedBfuId < curBfuId) { *changed = true; @@ -151,32 +134,34 @@ uint32_t TAtrac1SimpleBitAlloc::CheckBfuUsage(bool* changed, uint32_t curBfuId, } return curBfuId; } + uint32_t TAtrac1SimpleBitAlloc::Write(const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize) { uint32_t bfuIdx = BfuIdxConst ? BfuIdxConst - 1 : 7; bool autoBfu = !BfuIdxConst; - double spread = AnalizeSpread(scaledBlocks); + TFloat spread = AnalizeScaleFactorSpread(scaledBlocks); vector<uint32_t> bitsPerEachBlock(BfuAmountTab[bfuIdx]); uint32_t targetBitsPerBfus; uint32_t curBitsPerBfus; for (;;) { bitsPerEachBlock.resize(BfuAmountTab[bfuIdx]); - const uint32_t bitsAvaliablePerBfus = SoundUnitSize * 8 - BitsPerBfuAmountTabIdx - 32 - 2 - 3 - bitsPerEachBlock.size() * (BitsPerIDWL + BitsPerIDSF); - double maxShift = 15; - double minShift = -3; - double shift = 3.0; + const uint32_t bitsAvaliablePerBfus = SoundUnitSize * 8 - BitsPerBfuAmountTabIdx - 32 - 2 - 3 - + bitsPerEachBlock.size() * (BitsPerIDWL + BitsPerIDSF); + TFloat maxShift = 15; + TFloat minShift = -3; + TFloat shift = 3.0; const uint32_t maxBits = bitsAvaliablePerBfus; const uint32_t minBits = bitsAvaliablePerBfus - 110; bool bfuNumChanged = false; for (;;) { - const vector<uint32_t>& tmpAlloc = CalcBitsAllocation(scaledBlocks, BfuAmountTab[bfuIdx], spread, shift, blockSize); + const vector<uint32_t>& tmpAlloc = CalcBitsAllocation(scaledBlocks, BfuAmountTab[bfuIdx], + spread, shift, blockSize); uint32_t bitsUsed = 0; for (int i = 0; i < tmpAlloc.size(); i++) { bitsUsed += SpecsPerBlock[i] * tmpAlloc[i]; } - //std::cout << spread << " bitsUsed: " << bitsUsed << " min " << minBits << " max " << maxBits << " " << maxShift << " " << minShift << " " << endl; if (bitsUsed < minBits) { if (maxShift - minShift < 0.1) { if (autoBfu) { @@ -214,7 +199,10 @@ uint32_t TAtrac1SimpleBitAlloc::Write(const std::vector<TScaledBlock>& scaledBlo return BfuAmountTab[bfuIdx]; } -void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachBlock, const std::vector<TScaledBlock>& scaledBlocks, uint32_t bfuAmountIdx, const TBlockSize& blockSize) { +void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachBlock, + const std::vector<TScaledBlock>& scaledBlocks, + uint32_t bfuAmountIdx, + const TBlockSize& blockSize) { NBitStream::TBitStream bitStream; int bitUsed = 0; if (bfuAmountIdx >= (1 << BitsPerBfuAmountTabIdx)) { @@ -252,8 +240,8 @@ void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachB if (wordLength == 0 || wordLength == 1) continue; - const double multiple = ((1 << (wordLength - 1)) - 1); - for (const double val : scaledBlocks[i].Values) { + const TFloat multiple = ((1 << (wordLength - 1)) - 1); + for (const TFloat val : scaledBlocks[i].Values) { const int tmp = round(val * multiple); const int testwl = bitsPerEachBlock[i] ? (bitsPerEachBlock[i] - 1) : 0; const int a = !!testwl + testwl; @@ -280,4 +268,5 @@ void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachB Container->WriteFrame(bitStream.GetBytes()); } -} +} //namespace NAtrac1 +} //namespace NAtracDEnc diff --git a/src/atrac/atrac1_bitalloc.h b/src/atrac/atrac1_bitalloc.h index ce7b6fb..c5c4ad2 100644 --- a/src/atrac/atrac1_bitalloc.h +++ b/src/atrac/atrac1_bitalloc.h @@ -7,7 +7,9 @@ #include <map> #include <cstdint> +namespace NAtracDEnc { namespace NAtrac1 { + using NAtracDEnc::TScaledBlock; class IAtrac1BitAlloc { @@ -32,11 +34,13 @@ public: explicit TAtrac1BitStreamWriter(TAea* container) : Container(container) {}; - void WriteBitStream(const std::vector<uint32_t>& bitsPerEachBlock, const std::vector<TScaledBlock>& scaledBlocks, uint32_t bfuAmountIdx, const TBlockSize& blockSize); + void WriteBitStream(const std::vector<uint32_t>& bitsPerEachBlock, const std::vector<TScaledBlock>& scaledBlocks, + uint32_t bfuAmountIdx, const TBlockSize& blockSize); }; class TAtrac1SimpleBitAlloc : public TAtrac1BitStreamWriter, public TBitsBooster, public virtual IAtrac1BitAlloc { - std::vector<uint32_t> CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, const uint32_t bfuNum, const double spread, const double shift, const TBlockSize& blockSize); + std::vector<uint32_t> CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, const uint32_t bfuNum, + const TFloat spread, const TFloat shift, const TBlockSize& blockSize); const uint32_t BfuIdxConst; const bool FastBfuNumSearch; uint32_t GetMaxUsedBfuId(const std::vector<uint32_t>& bitsPerEachBlock); @@ -51,4 +55,5 @@ public: uint32_t Write(const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize) override; }; -} +} //namespace NAtrac1 +} //namespace NAtracDEnc diff --git a/src/atrac/atrac1_dequantiser.cpp b/src/atrac/atrac1_dequantiser.cpp index 8229822..83abc76 100644 --- a/src/atrac/atrac1_dequantiser.cpp +++ b/src/atrac/atrac1_dequantiser.cpp @@ -1,15 +1,16 @@ #include "atrac1_dequantiser.h" #include <string.h> - +namespace NAtracDEnc { namespace NAtrac1 { + using namespace NBitStream; TAtrac1Dequantiser::TAtrac1Dequantiser() { } -void TAtrac1Dequantiser::Dequant(TBitStream* stream, const TBlockSize& bs, double specs[512]) { - uint32_t wordLens[MAX_BFUS]; - uint32_t idScaleFactors[MAX_BFUS]; +void TAtrac1Dequantiser::Dequant(TBitStream* stream, const TBlockSize& bs, TFloat specs[512]) { + uint32_t wordLens[MaxBfus]; + uint32_t idScaleFactors[MaxBfus]; const uint32_t numBFUs = BfuAmountTab[stream->Read(3)]; stream->Read(2); stream->Read(3); @@ -21,28 +22,29 @@ void TAtrac1Dequantiser::Dequant(TBitStream* stream, const TBlockSize& bs, doubl for (uint32_t i = 0; i < numBFUs; i++) { idScaleFactors[i] = stream->Read(6); } - for (uint32_t i = numBFUs; i < MAX_BFUS; i++) { + for (uint32_t i = numBFUs; i < MaxBfus; i++) { wordLens[i] = idScaleFactors[i] = 0; } - for (uint32_t bandNum = 0; bandNum < QMF_BANDS; bandNum++) { + for (uint32_t bandNum = 0; bandNum < NumQMF; bandNum++) { for (uint32_t bfuNum = BlocksPerBand[bandNum]; bfuNum < BlocksPerBand[bandNum + 1]; bfuNum++) { const uint32_t numSpecs = SpecsPerBlock[bfuNum]; const uint32_t wordLen = !!wordLens[bfuNum] + wordLens[bfuNum]; - const double scaleFactor = ScaleTable[idScaleFactors[bfuNum]]; + const TFloat scaleFactor = ScaleTable[idScaleFactors[bfuNum]]; const uint32_t startPos = bs.LogCount[bandNum] ? SpecsStartShort[bfuNum] : SpecsStartLong[bfuNum]; if (wordLen) { - double maxQuant = 1.0 / (double)((1 << (wordLen - 1)) - 1); + TFloat maxQuant = 1.0 / (TFloat)((1 << (wordLen - 1)) - 1); //cout << "BFU ("<< bfuNum << ") :" << "wordLen " << wordLen << " maxQuant " << maxQuant << " scaleFactor " << scaleFactor << " id " << idScaleFactors[bfuNum] << " num Specs " << numSpecs << " short: "<< (int)bs.LogCount[bandNum] << endl; for (uint32_t i = 0; i < numSpecs; i++ ) { specs[startPos + i] = scaleFactor * maxQuant * MakeSign(stream->Read(wordLen), wordLen); } } else { - memset(&specs[startPos], 0, numSpecs * sizeof(double)); + memset(&specs[startPos], 0, numSpecs * sizeof(TFloat)); } } } } -} +} //namespace NAtrac1 +} //namespace NAtracDEnc diff --git a/src/atrac/atrac1_dequantiser.h b/src/atrac/atrac1_dequantiser.h index 112fc8b..8b2a8b4 100644 --- a/src/atrac/atrac1_dequantiser.h +++ b/src/atrac/atrac1_dequantiser.h @@ -1,12 +1,16 @@ #pragma once #include "atrac1.h" +#include "atrac_scale.h" +namespace NAtracDEnc { namespace NAtrac1 { class TAtrac1Dequantiser : public TAtrac1Data { public: TAtrac1Dequantiser(); - void Dequant(NBitStream::TBitStream* stream, const TBlockSize& bs, double specs[512]); + void Dequant(NBitStream::TBitStream* stream, const TBlockSize& bs, TFloat specs[512]); }; -} + +} //namespace NAtrac1 +} //namespace NAtracDEnc diff --git a/src/atrac/atrac1_qmf.h b/src/atrac/atrac1_qmf.h index 8550932..37d0bba 100644 --- a/src/atrac/atrac1_qmf.h +++ b/src/atrac/atrac1_qmf.h @@ -2,24 +2,26 @@ #include "../qmf/qmf.h" +namespace NAtracDEnc { + template<class TIn> class Atrac1SplitFilterBank { const static int nInSamples = 512; const static int delayComp = 39; TQmf<TIn, nInSamples> Qmf1; TQmf<TIn, nInSamples / 2> Qmf2; - std::vector<double> MidLowTmp; - std::vector<double> DelayBuf; + std::vector<TFloat> MidLowTmp; + std::vector<TFloat> DelayBuf; public: Atrac1SplitFilterBank() { MidLowTmp.resize(512); DelayBuf.resize(delayComp + 512); } - void Split(TIn* pcm, double* low, double* mid, double* hi) { - memcpy(&DelayBuf[0], &DelayBuf[256], sizeof(double) * delayComp); + void Split(TIn* pcm, TFloat* low, TFloat* mid, TFloat* hi) { + memcpy(&DelayBuf[0], &DelayBuf[256], sizeof(TFloat) * delayComp); Qmf1.Split(pcm, &MidLowTmp[0], &DelayBuf[delayComp]); Qmf2.Split(&MidLowTmp[0], low, mid); - memcpy(hi, &DelayBuf[0], sizeof(double) * 256); + memcpy(hi, &DelayBuf[0], sizeof(TFloat) * 256); } }; @@ -29,19 +31,19 @@ class Atrac1SynthesisFilterBank { const static int delayComp = 39; TQmf<TOut, nInSamples> Qmf1; TQmf<TOut, nInSamples / 2> Qmf2; - std::vector<double> MidLowTmp; - std::vector<double> DelayBuf; + std::vector<TFloat> MidLowTmp; + std::vector<TFloat> DelayBuf; public: Atrac1SynthesisFilterBank() { MidLowTmp.resize(512); DelayBuf.resize(delayComp + 512); } - void Synthesis(TOut* pcm, double* low, double* mid, double* hi) { - memcpy(&DelayBuf[0], &DelayBuf[256], sizeof(double) * delayComp); - memcpy(&DelayBuf[delayComp], hi, sizeof(double) * 256); + void Synthesis(TOut* pcm, TFloat* low, TFloat* mid, TFloat* hi) { + memcpy(&DelayBuf[0], &DelayBuf[256], sizeof(TFloat) * delayComp); + memcpy(&DelayBuf[delayComp], hi, sizeof(TFloat) * 256); Qmf2.Merge(&MidLowTmp[0], &low[0], &mid[0]); Qmf1.Merge(&pcm[0], &MidLowTmp[0], &DelayBuf[0]); } }; - +} //namespace NAtracDEnc diff --git a/src/atrac/atrac3.cpp b/src/atrac/atrac3.cpp index cdf6b3f..e587d2c 100644 --- a/src/atrac/atrac3.cpp +++ b/src/atrac/atrac3.cpp @@ -1,5 +1,9 @@ #include "atrac3.h" #include <algorithm> + +namespace NAtracDEnc { +namespace NAtrac3 { + constexpr uint32_t TAtrac3Data::BlockSizeTab[33]; constexpr uint32_t TAtrac3Data::ClcLengthTab[8]; constexpr double TAtrac3Data::MaxQuant[8]; @@ -24,3 +28,6 @@ const TContainerParams* TAtrac3Data::GetContainerParamsForBitrate(uint32_t bitra std::cout << bitrate << std::endl; return std::lower_bound(ContainerParams, ContainerParams+8, bitrate); } + +} // namespace NAtrac3 +} // namespace NAtracDEnc diff --git a/src/atrac/atrac3.h b/src/atrac/atrac3.h index 02e694b..e65e71a 100644 --- a/src/atrac/atrac3.h +++ b/src/atrac/atrac3.h @@ -5,6 +5,8 @@ #include <cassert> #include <iostream> +namespace NAtracDEnc { +namespace NAtrac3 { struct TContainerParams { const uint32_t Bitrate; @@ -30,6 +32,9 @@ inline bool operator> (const TContainerParams& x, const unsigned int y) } class TAtrac3Data { +public: + static constexpr uint8_t MaxBfus = 32; + static constexpr uint32_t NumSamples = 1024; protected: static const uint32_t MDCTSz = 512; static double ScaleTable[64]; @@ -42,7 +47,7 @@ protected: static constexpr int32_t LocSz = 1 << LocScale; static constexpr int32_t GainInterpolationPosShift = 15; - static const uint32_t NumSamples=1024; + static constexpr uint32_t NumSpecs = NumSamples; static const uint32_t frameSz = 152; static constexpr double MaxQuant[8] = { 0.0, 1.5, 2.5, 3.5, @@ -59,8 +64,9 @@ protected: static constexpr uint32_t const * const SpecsStartLong = &BlockSizeTab[0]; static constexpr uint32_t ClcLengthTab[8] = { 0, 4, 3, 3, 4, 4, 5, 6 }; - static const int NumQMF = 4; - static const uint32_t MAX_SPECS = 1024; + static constexpr int NumQMF = 4; + static constexpr uint32_t MaxSpecs = NumSamples; //1024 + static constexpr uint32_t MaxSpecsPerBlock = 128; static constexpr uint32_t BlocksPerBand[NumQMF + 1] = {0, 18, 26, 30, 32}; static constexpr uint32_t SpecsPerBlock[33] = { @@ -114,13 +120,16 @@ protected: static constexpr THuffEntry HuffTable7[HuffTable7Sz] = { { 0x0, 3 }, //{ 0x2, 4 }, { 0x3, 4 }, - { 0x8, 5 }, { 0x9, 5 }, { 0xA, 5}, { 0xB, 5 }, { 0xC, 5 }, { 0xD, 5 }, { 0xE, 5}, { 0xF, 5 }, { 0x10, 5 }, { 0x11, 5 }, + { 0x8, 5 }, { 0x9, 5 }, { 0xA, 5}, { 0xB, 5 }, { 0xC, 5 }, { 0xD, 5 }, { 0xE, 5}, { 0xF, 5 }, { 0x10, 5 }, + { 0x11, 5 }, { 0x24, 6 }, { 0x25, 6 }, { 0x26, 6 }, { 0x27, 6 }, { 0x28, 6 }, { 0x29, 6 }, { 0x2A, 6 }, { 0x2B, 6 }, { 0x2C, 6 }, { 0x2D, 6 }, { 0x2E, 6 }, { 0x2F, 6 }, { 0x30, 6 }, { 0x31, 6 }, { 0x32, 6 }, { 0x33, 6 }, { 0x68, 7 }, { 0x69, 7 }, { 0x6A, 7 }, { 0x6B, 7 }, { 0x6C, 7 }, { 0x6D, 7 }, { 0x6E, 7 }, { 0x6F, 7 }, { 0x70, 7 }, { 0x71, 7 }, { 0x72, 7 }, { 0x73, 7 }, { 0x74, 7 }, { 0x75, 7 }, - { 0xEC, 8 }, { 0xED, 8 }, { 0xEE, 8 }, { 0xEF, 8 }, { 0xF0, 8 }, { 0xF1, 8 }, { 0xF2, 8 }, { 0xF3, 8 }, { 0xF4, 8 }, { 0xF5, 8 }, - { 0xF6, 8 }, { 0xF7, 8 }, { 0xF8, 8 }, { 0xF9, 8 }, { 0xFA, 8 }, { 0xFB, 8 }, { 0xFC, 8 }, { 0xFD, 8 }, { 0xFE, 8 }, { 0xFF, 8 }, + { 0xEC, 8 }, { 0xED, 8 }, { 0xEE, 8 }, { 0xEF, 8 }, { 0xF0, 8 }, { 0xF1, 8 }, { 0xF2, 8 }, { 0xF3, 8 }, + { 0xF4, 8 }, { 0xF5, 8 }, + { 0xF6, 8 }, { 0xF7, 8 }, { 0xF8, 8 }, { 0xF9, 8 }, { 0xFA, 8 }, { 0xFB, 8 }, { 0xFC, 8 }, { 0xFD, 8 }, + { 0xFE, 8 }, { 0xFF, 8 }, { 0x2, 4 }, { 0x3, 4 } //TODO: is it right table??? }; @@ -146,7 +155,7 @@ public: } } for (int i = 0; i < 256; i++) { - EncodeWindow[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5; + EncodeWindow[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0)/* * 0.5*/; } for (int i = 0; i < 256; i++) { const double a = EncodeWindow[i]; @@ -186,6 +195,7 @@ public: class SubbandInfo { public: + static const uint32_t MaxGainPointsNum = 8; struct TGainPoint { const uint32_t Level; const uint32_t Location; @@ -207,4 +217,26 @@ public: return Info[i]; } }; + + struct TTonalVal { + const uint16_t Pos; + const double Val; + }; + typedef std::vector<TTonalVal> TTonalComponents; }; + +struct TAtrac3EncoderSettings { + explicit TAtrac3EncoderSettings(uint32_t bitrate, bool noGainControll, bool noTonalComponents) + : ConteinerParams(TAtrac3Data::GetContainerParamsForBitrate(bitrate)) + , NoGainControll(noGainControll) + , NoTonalComponents(noTonalComponents) + { + std::cout << bitrate << " " << ConteinerParams->Bitrate << std::endl; + } + const TContainerParams* ConteinerParams; + const bool NoGainControll; + const bool NoTonalComponents; +}; + +} // namespace NAtrac3 +} // namespace NAtracDEnc diff --git a/src/atrac/atrac3_bitstream.cpp b/src/atrac/atrac3_bitstream.cpp index ff28132..3822a67 100644 --- a/src/atrac/atrac3_bitstream.cpp +++ b/src/atrac/atrac3_bitstream.cpp @@ -1,4 +1,5 @@ #include "atrac3_bitstream.h" +#include "atrac_psy_common.h" #include "../bitstream/bitstream.h" #include <cassert> #include <algorithm> @@ -6,10 +7,21 @@ #include <vector> #include <cstdlib> -using NAtracDEnc::TScaledBlock; +namespace NAtracDEnc { +namespace NAtrac3 { + using std::vector; -uint32_t TAtrac3BitStreamWriter::CLCEnc(const uint32_t selector, const int mantissas[MAXSPECPERBLOCK], const uint32_t blockSize, NBitStream::TBitStream* bitStream) { +static const uint32_t FixedBitAllocTable[TAtrac3Data::MaxBfus] = { + 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 3, 3, 3, 3, 3, + 3, 2, 2, 1, + 1, 0 +}; + +uint32_t TAtrac3BitStreamWriter::CLCEnc(const uint32_t selector, const int mantissas[MaxSpecsPerBlock], + const uint32_t blockSize, NBitStream::TBitStream* bitStream) +{ const uint32_t numBits = ClcLengthTab[selector]; const uint32_t bitsUsed = (selector > 1) ? numBits * blockSize : numBits * blockSize / 2; if (!bitStream) @@ -29,7 +41,10 @@ uint32_t TAtrac3BitStreamWriter::CLCEnc(const uint32_t selector, const int manti return bitsUsed; } -uint32_t TAtrac3BitStreamWriter::VLCEnc(const uint32_t selector, const int mantissas[MAXSPECPERBLOCK], const uint32_t blockSize, NBitStream::TBitStream* bitStream) { +uint32_t TAtrac3BitStreamWriter::VLCEnc(const uint32_t selector, const int mantissas[MaxSpecsPerBlock], + const uint32_t blockSize, NBitStream::TBitStream* bitStream) +{ + assert(selector > 0); const THuffEntry* huffTable = HuffTables[selector - 1].Table; const uint8_t tableSz = HuffTables[selector - 1].Sz; uint32_t bitsUsed = 0; @@ -58,7 +73,10 @@ uint32_t TAtrac3BitStreamWriter::VLCEnc(const uint32_t selector, const int manti } return bitsUsed; } -std::pair<uint8_t, uint32_t> TAtrac3BitStreamWriter::CalcSpecsBitsConsumption(const vector<TScaledBlock>& scaledBlocks, const vector<uint32_t>& precisionPerEachBlocks, int* mantisas) { + +std::pair<uint8_t, uint32_t> TAtrac3BitStreamWriter::CalcSpecsBitsConsumption(const vector<TScaledBlock>& scaledBlocks, + const vector<uint32_t>& precisionPerEachBlocks, int* mantisas) +{ uint32_t bitsUsed = 5 + 1; //numBlocks + codingMode const uint32_t numBlocks = precisionPerEachBlocks.size(); bitsUsed += numBlocks * 3; //used VLC or CLC table (precision) @@ -72,7 +90,7 @@ std::pair<uint8_t, uint32_t> TAtrac3BitStreamWriter::CalcSpecsBitsConsumption(co const uint32_t first = BlockSizeTab[i]; const uint32_t last = BlockSizeTab[i+1]; const uint32_t blockSize = last - first; - const double mul = MaxQuant[std::min(precisionPerEachBlocks[i], (uint32_t)7)]; + const TFloat mul = MaxQuant[std::min(precisionPerEachBlocks[i], (uint32_t)7)]; if (calcMant) { for (uint32_t j = 0, f = first; f < last; f++, j++) { mantisas[f] = round(scaledBlocks[i].Values[j] * mul); @@ -86,14 +104,73 @@ std::pair<uint8_t, uint32_t> TAtrac3BitStreamWriter::CalcSpecsBitsConsumption(co const uint32_t clcBits = lambda(true, true); const uint32_t vlcBits = lambda(false, false); bool mode = clcBits <= vlcBits; - //std::cout << "mode: " << mode << " " << clcBits << " " <<vlcBits << std::endl; return std::make_pair(mode, bitsUsed + (mode ? clcBits : vlcBits)); } -void TAtrac3BitStreamWriter::EncodeSpecs(const vector<TScaledBlock>& scaledBlocks, const vector<uint32_t>& precisionPerEachBlocks, NBitStream::TBitStream* bitStream) { + + +std::pair<uint8_t, vector<uint32_t>> TAtrac3BitStreamWriter::CreateAllocation(const vector<TScaledBlock>& scaledBlocks, + uint16_t bitsUsed, int mt[MaxSpecs]) +{ + TFloat spread = AnalizeScaleFactorSpread(scaledBlocks); + + uint8_t numBfu = 32; + vector<uint32_t> precisionPerEachBlocks(numBfu); + uint32_t targetBitsPerBfus; + uint32_t curBitsPerBfus; + uint8_t mode; + for (;;) { + precisionPerEachBlocks.resize(numBfu); + uint32_t usedBfus = 0; + for (auto v : precisionPerEachBlocks) { + if (v) + usedBfus++; + } + const uint32_t bitsAvaliablePerBfus = 8 * Params.FrameSz/2 - bitsUsed - + 5 - 1 - (numBfu * 3) - (usedBfus * 6); + TFloat maxShift = 15; + TFloat minShift = -3; + TFloat shift = 3.0; + const uint32_t maxBits = bitsAvaliablePerBfus; + const uint32_t minBits = bitsAvaliablePerBfus - 90; + for (;;) { + const vector<uint32_t>& tmpAlloc = CalcBitsAllocation(scaledBlocks, numBfu, spread, shift); + const auto consumption = CalcSpecsBitsConsumption(scaledBlocks, tmpAlloc, mt); + + if (consumption.second < minBits) { + if (maxShift - minShift < 0.1) { + precisionPerEachBlocks = tmpAlloc; + curBitsPerBfus = consumption.second; + mode = consumption.first; + break; + } + maxShift = shift; + shift -= (shift - minShift) / 2; + } else if (consumption.second > maxBits) { + minShift = shift; + shift += (maxShift - shift) / 2; + } else { + precisionPerEachBlocks = tmpAlloc; + curBitsPerBfus = consumption.second; + mode = consumption.first; + break; + } + } + targetBitsPerBfus = bitsAvaliablePerBfus; + break; + + } + return { mode, precisionPerEachBlocks }; +} + +void TAtrac3BitStreamWriter::EncodeSpecs(const vector<TScaledBlock>& scaledBlocks, NBitStream::TBitStream* bitStream, + const uint16_t bitsUsed) +{ + int mt[MaxSpecs]; + auto allocation = CreateAllocation(scaledBlocks, bitsUsed, mt); + const vector<uint32_t>& precisionPerEachBlocks = allocation.second; const uint32_t numBlocks = precisionPerEachBlocks.size(); //number of blocks to save - int mt[MAX_SPECS]; - const auto consumption = CalcSpecsBitsConsumption(scaledBlocks, precisionPerEachBlocks, mt); - const uint32_t codingMode = consumption.first;//0 - VLC, 1 - CLC + const uint32_t codingMode = allocation.first;//0 - VLC, 1 - CLC + assert(numBlocks <= 32); bitStream->Write(numBlocks-1, 5); bitStream->Write(codingMode, 1); @@ -122,53 +199,234 @@ void TAtrac3BitStreamWriter::EncodeSpecs(const vector<TScaledBlock>& scaledBlock } } -void TAtrac3BitStreamWriter::WriteSoundUnit(const TAtrac3Data::SubbandInfo& subbandInfo, const vector<TScaledBlock>& scaledBlocks) { +uint8_t TAtrac3BitStreamWriter::GroupTonalComponents(const std::vector<TTonalComponent>& tonalComponents, + TTonalComponentsSubGroup groups[64]) +{ + for (const TTonalComponent& tc : tonalComponents) { + assert(tc.ScaledBlock.Values.size() < 8); + assert(tc.ScaledBlock.Values.size() > 0); + assert(tc.QuantIdx >1); + assert(tc.QuantIdx <8); + groups[tc.QuantIdx * 8 + tc.ScaledBlock.Values.size()].SubGroupPtr.push_back(&tc); + } + uint8_t tcsgn = 0; + //for each group + for (uint8_t i = 0; i < 64; ++i) { + uint8_t start_pos; + uint8_t cur_pos = 0; + //scan tonal components + while (cur_pos < groups[i].SubGroupPtr.size()) { + start_pos = cur_pos; + ++tcsgn; + groups[i].SubGroupMap.push_back(cur_pos); + uint8_t groupLimiter = 0; + //allow not grather than 8 components in one subgroup limited by 64 specs + do { + ++cur_pos; + if (cur_pos == groups[i].SubGroupPtr.size()) + break; + if (groups[i].SubGroupPtr[cur_pos]->ValPtr->Pos - (groups[i].SubGroupPtr[start_pos]->ValPtr->Pos & ~63) < 64) { + ++groupLimiter; + } else { + groupLimiter = 0; + start_pos = cur_pos; + } + } while (groupLimiter < 7); + } + } + return tcsgn; +} + +uint16_t TAtrac3BitStreamWriter::EncodeTonalComponents(const std::vector<TTonalComponent>& tonalComponents, + NBitStream::TBitStream* bitStream, uint8_t numQmfBand) +{ + const uint16_t bitsUsed = bitStream->GetSizeInBits(); + //group tonal components with same quantizer and len + TTonalComponentsSubGroup groups[64]; + const uint8_t tcsgn = GroupTonalComponents(tonalComponents, groups); + + assert(tcsgn < 32); + bitStream->Write(tcsgn, 5); + if (tcsgn == 0) { + for (int i = 0; i < 64; ++i) + assert(groups[i].SubGroupPtr.size() == 0); + return 5; //wrote 0 but 5 bits for tcsgn + } + //Coding mode: + // 0 - All are VLC + // 1 - All are CLC + // 2 - Error + // 3 - Own mode for each component + + //TODO: implement switch for best coding mode. Now VLC for all + bitStream->Write(0, 2); + + uint8_t tcgnCheck = 0; + //for each group of equal quantiser and len + for (uint8_t i = 0; i < 64; ++i) { + const TTonalComponentsSubGroup& curGroup = groups[i]; + if (curGroup.SubGroupPtr.size() == 0) { + assert(curGroup.SubGroupMap.size() == 0); + continue; + } + assert(curGroup.SubGroupMap.size()); + for (uint8_t subgroup = 0; subgroup < curGroup.SubGroupMap.size(); ++subgroup) { + const uint8_t subGroupStartPos = curGroup.SubGroupMap[subgroup]; + const uint8_t subGroupEndPos = (subgroup < curGroup.SubGroupMap.size() - 1) ? + curGroup.SubGroupMap[subgroup+1] : curGroup.SubGroupPtr.size(); + assert(subGroupEndPos > subGroupStartPos); + //number of coded values are same in group + const uint8_t codedValues = curGroup.SubGroupPtr[0]->ScaledBlock.Values.size(); + + //Number of tonal component for each 64spec block. Used to set qmf band flags and simplify band encoding loop + uint8_t bandFlags[16]; + memset(bandFlags, 0, 16 * sizeof(uint8_t)); + assert(numQmfBand <= 4); + for (uint8_t j = subGroupStartPos; j < subGroupEndPos; ++j) { + //assert num of coded values are same in group + assert(codedValues == curGroup.SubGroupPtr[j]->ScaledBlock.Values.size()); + uint8_t specBlock = (curGroup.SubGroupPtr[j]->ValPtr->Pos) >> 6; + assert((specBlock >> 2) < numQmfBand); + bandFlags[specBlock]++; + } + + assert(numQmfBand == 4); + + tcgnCheck++; + + for (uint8_t j = 0; j < numQmfBand; ++j) { + bitStream->Write((bool)(*(uint32_t*)&bandFlags[j<<2]), 1); + } + //write number of coded values for components in current group + assert(codedValues > 0); + bitStream->Write(codedValues - 1, 3); + //write quant index + assert((i >> 3) > 1); + assert((i >> 3) < 8); + assert(i); + bitStream->Write(i >> 3, 3); + uint8_t lastPos = subGroupStartPos; + uint8_t checkPos = 0; + for (uint16_t j = 0; j < 16; ++j) { + if (!(*(uint32_t*)&bandFlags[j & 0xC])) { //discard two bits + continue; + } + + const uint8_t codedComponents = bandFlags[j]; + assert(codedComponents < 8); + bitStream->Write(codedComponents, 3); + uint8_t k = lastPos; + for (; k < lastPos + codedComponents; ++k) { + assert(curGroup.SubGroupPtr[k]->ValPtr->Pos >= j * 64); + uint16_t relPos = curGroup.SubGroupPtr[k]->ValPtr->Pos - j * 64; + assert(curGroup.SubGroupPtr[k]->ScaledBlock.ScaleFactorIndex < 64); + bitStream->Write(curGroup.SubGroupPtr[k]->ScaledBlock.ScaleFactorIndex, 6); + + assert(relPos < 64); + + bitStream->Write(relPos, 6); + + assert(curGroup.SubGroupPtr[k]->ScaledBlock.Values.size() < 8); + int mantisas[256]; + const TFloat mul = MaxQuant[std::min((uint32_t)(i>>3), (uint32_t)7)]; + assert(codedValues == curGroup.SubGroupPtr[k]->ScaledBlock.Values.size()); + for (uint32_t z = 0; z < curGroup.SubGroupPtr[k]->ScaledBlock.Values.size(); ++z) { + mantisas[z] = round(curGroup.SubGroupPtr[k]->ScaledBlock.Values[z] * mul); + } + //VLCEnc + + assert(i); + VLCEnc(i>>3, mantisas, curGroup.SubGroupPtr[k]->ScaledBlock.Values.size(), bitStream); + + + } + lastPos = k; + checkPos = lastPos; + bool shouldBreakCurrentGroup = true; + for (uint16_t k = j+1; k < 16; ++k) { + if (bandFlags[k]) { + shouldBreakCurrentGroup = false; + } + } + } + + assert(subGroupEndPos == checkPos); + } + } + assert(tcgnCheck == tcsgn); + return bitStream->GetSizeInBits() - bitsUsed; +} + +vector<uint32_t> TAtrac3BitStreamWriter::CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, + const uint32_t bfuNum, + const TFloat spread, + const TFloat shift) +{ + vector<uint32_t> bitsPerEachBlock(bfuNum); + for (int i = 0; i < bitsPerEachBlock.size(); ++i) { + const uint32_t fix = FixedBitAllocTable[i]; + int tmp = spread * ( (TFloat)scaledBlocks[i].ScaleFactorIndex/3.2) + (1.0 - spread) * fix - shift; + if (tmp > 7) { + bitsPerEachBlock[i] = 7; + } else if (tmp < 0) { + bitsPerEachBlock[i] = 0; + } else { + bitsPerEachBlock[i] = tmp; + } + } + return bitsPerEachBlock; +} + + +void TAtrac3BitStreamWriter::WriteSoundUnit(const TAtrac3Data::SubbandInfo& subbandInfo, + const std::vector<TTonalComponent>& tonalComponents, + const vector<TScaledBlock>& scaledBlocks) +{ NBitStream::TBitStream bitStream; if (Params.Js) { //TODO } else { bitStream.Write(0x28, 6); //0x28 - id } - const uint32_t numQmfBand = subbandInfo.GetQmfNum(); + const uint8_t numQmfBand = subbandInfo.GetQmfNum(); bitStream.Write(numQmfBand - 1, 2); //write gain info for (uint32_t band = 0; band < numQmfBand; ++band) { const vector<TAtrac3Data::SubbandInfo::TGainPoint>& GainPoints = subbandInfo.GetGainPoints(band); + assert(GainPoints.size() < TAtrac3Data::SubbandInfo::MaxGainPointsNum); bitStream.Write(GainPoints.size(), 3); + int s = 0; for (const TAtrac3Data::SubbandInfo::TGainPoint& point : GainPoints) { bitStream.Write(point.Level, 4); bitStream.Write(point.Location, 5); + s++; + assert(s < 8); } } - - //tonal components - bitStream.Write(0, 5); //disabled - - vector<uint32_t> precisionPerEachBlocks(27,1); - for (int i = 0; i < 2; i++) { - //precisionPerEachBlocks[i] = 4; - } - + const uint16_t bitsUsedByGainInfo = bitStream.GetSizeInBits() - 8; + const uint16_t bitsUsedByTonal = EncodeTonalComponents(tonalComponents, &bitStream, numQmfBand); //spec - EncodeSpecs(scaledBlocks, precisionPerEachBlocks, &bitStream); + EncodeSpecs(scaledBlocks, &bitStream, bitsUsedByTonal + bitsUsedByGainInfo); if (!Container) abort(); if (OutBuffer.empty()) { std::vector<char> channel = bitStream.GetBytes(); - //std::cout << channel.size() << std::endl; assert(channel.size() <= Params.FrameSz/2); channel.resize(Params.FrameSz/2); OutBuffer.insert(OutBuffer.end(), channel.begin(), channel.end()); } else { std::vector<char> channel = bitStream.GetBytes(); - channel.resize(Params.FrameSz/2); - //std::cout << channel.size() << std::endl; + assert(channel.size() <= Params.FrameSz/2); + channel.resize(Params.FrameSz/2); OutBuffer.insert(OutBuffer.end(), channel.begin(), channel.end()); Container->WriteFrame(OutBuffer); OutBuffer.clear(); } } + +} // namespace NAtrac3 +} // namespace NAtracDEnc diff --git a/src/atrac/atrac3_bitstream.h b/src/atrac/atrac3_bitstream.h index 60f1e4a..225d98c 100644 --- a/src/atrac/atrac3_bitstream.h +++ b/src/atrac/atrac3_bitstream.h @@ -8,16 +8,53 @@ #include <vector> #include <utility> -static const uint32_t MAXSPECPERBLOCK = 128; +namespace NAtracDEnc { +namespace NAtrac3 { + +struct TTonalComponent { + TTonalComponent(const TAtrac3Data::TTonalVal* valPtr, uint8_t quantIdx, const TScaledBlock& scaledBlock) + : ValPtr(valPtr) + , QuantIdx(quantIdx) + , ScaledBlock(scaledBlock) + {} + const TAtrac3Data::TTonalVal* ValPtr = nullptr; + uint8_t QuantIdx = 0; + TScaledBlock ScaledBlock; +}; class TAtrac3BitStreamWriter : public virtual TAtrac3Data { + struct TTonalComponentsSubGroup { + std::vector<uint8_t> SubGroupMap; + std::vector<const TTonalComponent*> SubGroupPtr; + }; TOma* Container; const TContainerParams Params; std::vector<char> OutBuffer; - uint32_t CLCEnc(const uint32_t selector, const int mantissas[MAXSPECPERBLOCK], const uint32_t blockSize, NBitStream::TBitStream* bitStream); - uint32_t VLCEnc(const uint32_t selector, const int mantissas[MAXSPECPERBLOCK], const uint32_t blockSize, NBitStream::TBitStream* bitStream); - std::pair<uint8_t, uint32_t> CalcSpecsBitsConsumption(const std::vector<NAtracDEnc::TScaledBlock>& scaledBlocks, const std::vector<uint32_t>& precisionPerEachBlocks, int* mantisas); - void EncodeSpecs(const std::vector<NAtracDEnc::TScaledBlock>& scaledBlocks, const std::vector<uint32_t>& bitsPerEachBlock, NBitStream::TBitStream* bitStream); + + uint32_t CLCEnc(const uint32_t selector, const int mantissas[MaxSpecsPerBlock], + const uint32_t blockSize, NBitStream::TBitStream* bitStream); + + uint32_t VLCEnc(const uint32_t selector, const int mantissas[MaxSpecsPerBlock], + const uint32_t blockSize, NBitStream::TBitStream* bitStream); + + std::vector<uint32_t> CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, + uint32_t bfuNum, TFloat spread, TFloat shift); + + std::pair<uint8_t, std::vector<uint32_t>> CreateAllocation(const std::vector<TScaledBlock>& scaledBlocks, + uint16_t bitsUsed, int mt[MaxSpecs]); + + std::pair<uint8_t, uint32_t> CalcSpecsBitsConsumption(const std::vector<TScaledBlock>& scaledBlocks, + const std::vector<uint32_t>& precisionPerEachBlocks, + int* mantisas); + + void EncodeSpecs(const std::vector<TScaledBlock>& scaledBlocks, NBitStream::TBitStream* bitStream, + uint16_t bitsUsed); + + uint8_t GroupTonalComponents(const std::vector<TTonalComponent>& tonalComponents, + TTonalComponentsSubGroup groups[64]); + + uint16_t EncodeTonalComponents(const std::vector<TTonalComponent>& tonalComponents, + NBitStream::TBitStream* bitStream, uint8_t numQmfBand); public: TAtrac3BitStreamWriter(TOma* container, const TContainerParams& params) //no mono mode for atrac3 : Container(container) @@ -25,5 +62,10 @@ public: { } - void WriteSoundUnit(const TAtrac3Data::SubbandInfo& subbandInfo, const std::vector<NAtracDEnc::TScaledBlock>& scaledBlocks); + void WriteSoundUnit(const TAtrac3Data::SubbandInfo& subbandInfo, + const std::vector<TTonalComponent>& tonalComponents, + const std::vector<TScaledBlock>& scaledBlocks); }; + +} // namespace NAtrac3 +} // namespace NAtracDEnc diff --git a/src/atrac/atrac3_qmf.h b/src/atrac/atrac3_qmf.h index bcd0475..f0ef805 100644 --- a/src/atrac/atrac3_qmf.h +++ b/src/atrac/atrac3_qmf.h @@ -2,22 +2,26 @@ #include <vector> #include "../qmf/qmf.h" +namespace NAtracDEnc { + template<class TIn> class Atrac3SplitFilterBank { const static int nInSamples = 1024; TQmf<TIn, nInSamples> Qmf1; TQmf<TIn, nInSamples / 2> Qmf2; TQmf<TIn, nInSamples / 2> Qmf3; - std::vector<double> Buf1; - std::vector<double> Buf2; + std::vector<TFloat> Buf1; + std::vector<TFloat> Buf2; public: Atrac3SplitFilterBank() { Buf1.resize(nInSamples); Buf2.resize(nInSamples); } - void Split(TIn* pcm, double* subs[4]) { + void Split(TIn* pcm, TFloat* subs[4]) { Qmf1.Split(pcm, Buf1.data(), Buf2.data()); Qmf2.Split(Buf1.data(), subs[0], subs[1]); Qmf3.Split(Buf2.data(), subs[3], subs[2]); } }; + +} //namespace NAtracDEnc diff --git a/src/atrac/atrac_psy_common.cpp b/src/atrac/atrac_psy_common.cpp new file mode 100644 index 0000000..44f973c --- /dev/null +++ b/src/atrac/atrac_psy_common.cpp @@ -0,0 +1,26 @@ +#include "atrac_psy_common.h" + +namespace NAtracDEnc { + +//returns 1 for tone-like, 0 - noise-like +TFloat AnalizeScaleFactorSpread(const std::vector<TScaledBlock>& scaledBlocks) { + TFloat s = 0.0; + for (int i = 0; i < scaledBlocks.size(); ++i) { + s += scaledBlocks[i].ScaleFactorIndex; + } + s /= scaledBlocks.size(); + TFloat sigma = 0.0; + TFloat t = 0.0; + for (int i = 0; i < scaledBlocks.size(); ++i) { + t = (scaledBlocks[i].ScaleFactorIndex - s); + t *= t; + sigma += t; + } + sigma /= scaledBlocks.size(); + sigma = sqrt(sigma); + if (sigma > 14.0) + sigma = 14.0; + return sigma/14.0; +} + +} //namespace NAtracDEnc diff --git a/src/atrac/atrac_psy_common.h b/src/atrac/atrac_psy_common.h new file mode 100644 index 0000000..4c580a4 --- /dev/null +++ b/src/atrac/atrac_psy_common.h @@ -0,0 +1,8 @@ +#pragma once +#include "atrac_scale.h" + +namespace NAtracDEnc { + +double AnalizeScaleFactorSpread(const std::vector<TScaledBlock>& scaledBlocks); + +} //namespace NAtracDEnc diff --git a/src/atrac/atrac_scale.cpp b/src/atrac/atrac_scale.cpp index 92d2645..a03bc13 100644 --- a/src/atrac/atrac_scale.cpp +++ b/src/atrac/atrac_scale.cpp @@ -4,47 +4,65 @@ #include <cmath> #include <iostream> #include <algorithm> + namespace NAtracDEnc { + using std::vector; using std::map; -using namespace std; +using std::cerr; +using std::endl; + +using std::abs; + static const uint32_t MAX_SCALE = 65536; template<class TBaseData> -vector<TScaledBlock> TScaler<TBaseData>::Scale(const vector<double>& specs, const TBlockSize& blockSize) { +TScaledBlock TScaler<TBaseData>::Scale(const TFloat* in, uint16_t len) { + TFloat maxAbsSpec = 0; + for (uint16_t i = 0; i < len; ++i) { + const TFloat absSpec = abs(in[i]); + if (absSpec > maxAbsSpec) { + if (absSpec > MAX_SCALE) { + cerr << "Scale error: absSpec > MAX_SCALE, val: " << absSpec << endl; + maxAbsSpec = MAX_SCALE; + } else { + maxAbsSpec = absSpec; + } + } + } + const map<TFloat, uint8_t>::const_iterator scaleIter = ScaleIndex.lower_bound(maxAbsSpec); + const TFloat scaleFactor = scaleIter->first; + const uint8_t scaleFactorIndex = scaleIter->second; + TScaledBlock res(scaleFactorIndex); + for (uint16_t i = 0; i < len; ++i) { + const TFloat scaledValue = in[i] / scaleFactor; + if (scaledValue > 1.0) { + cerr << "got "<< scaledValue << " it is wrong scalling" << endl; + } + res.Values.push_back(scaledValue); + } + return res; +} + +template<class TBaseData> +vector<TScaledBlock> TScaler<TBaseData>::ScaleFrame(const vector<TFloat>& specs, const TBlockSize& blockSize) { vector<TScaledBlock> scaledBlocks; for (uint8_t bandNum = 0; bandNum < this->NumQMF; ++bandNum) { const bool shortWinMode = !!blockSize.LogCount[bandNum]; for (uint8_t blockNum = this->BlocksPerBand[bandNum]; blockNum < this->BlocksPerBand[bandNum + 1]; ++blockNum) { - const uint16_t specNumStart = shortWinMode ? TBaseData::SpecsStartShort[blockNum] : TBaseData::SpecsStartLong[blockNum]; - const uint16_t specNumEnd = specNumStart + this->SpecsPerBlock[blockNum]; - double maxAbsSpec = 0; - for (uint16_t curSpec = specNumStart; curSpec < specNumEnd; ++curSpec) { - const double absSpec = abs(specs[curSpec]); - if (absSpec > maxAbsSpec) { - if (absSpec > MAX_SCALE) { - cerr << "got " << absSpec << " value - overflow" << endl; - maxAbsSpec = MAX_SCALE; - } else { - maxAbsSpec = absSpec; - } - } - } - const map<double, uint8_t>::const_iterator scaleIter = ScaleIndex.lower_bound(maxAbsSpec); - const double scaleFactor = scaleIter->first; - const uint8_t scaleFactorIndex = scaleIter->second; - scaledBlocks.push_back(TScaledBlock(scaleFactorIndex)); - for (uint16_t specNum = specNumStart; specNum < specNumEnd; ++specNum) { - const double scaledValue = specs[specNum] / scaleFactor; - if (scaledValue > 1.0) - cerr << "got "<< scaledValue << " value - wrong scalling" << endl; - scaledBlocks.back().Values.push_back(scaledValue); - } + const uint16_t specNumStart = shortWinMode ? TBaseData::SpecsStartShort[blockNum] : + TBaseData::SpecsStartLong[blockNum]; + scaledBlocks.emplace_back(Scale(&specs[specNumStart], this->SpecsPerBlock[blockNum])); } } return scaledBlocks; } -template class TScaler<TAtrac1Data>; -template class TScaler<TAtrac3Data>; -} + +template +class TScaler<NAtrac1::TAtrac1Data>; + +template +class TScaler<NAtrac3::TAtrac3Data>; + +} //namespace NAtracDEnc diff --git a/src/atrac/atrac_scale.h b/src/atrac/atrac_scale.h index dd437a2..499fac2 100644 --- a/src/atrac/atrac_scale.h +++ b/src/atrac/atrac_scale.h @@ -4,24 +4,60 @@ #include <cstdint> #include "atrac1.h" +#include "../config.h" + namespace NAtracDEnc { struct TScaledBlock { TScaledBlock(uint8_t sfi) : ScaleFactorIndex(sfi) {} - const uint8_t ScaleFactorIndex = 0; - std::vector<double> Values; + /* const */ uint8_t ScaleFactorIndex = 0; + std::vector<TFloat> Values; }; +class TBlockSize; + template <class TBaseData> class TScaler : public TBaseData { - std::map<double, uint8_t>ScaleIndex; + std::map<TFloat, uint8_t>ScaleIndex; public: TScaler() { for (int i = 0; i < 64; i++) { ScaleIndex[TBaseData::ScaleTable[i]] = i; } } - std::vector<TScaledBlock> Scale(const std::vector<double>& specs, const TBlockSize& blockSize); + TScaledBlock Scale(const TFloat* in, uint16_t len); + std::vector<TScaledBlock> ScaleFrame(const std::vector<TFloat>& specs, const TBlockSize& blockSize); +}; + +class TBlockSize { + static std::array<int, 4> Parse(NBitStream::TBitStream* stream) { + //ATRAC1 - 3 subbands, ATRAC3 - 4 subbands. + //TODO: rewrite + std::array<int, 4> tmp; + tmp[0] = 2 - stream->Read(2); + tmp[1] = 2 - stream->Read(2); + tmp[2] = 3 - stream->Read(2); + stream->Read(2); //skip unused 2 bits + return tmp; + } + static std::array<int, 4> Create(bool lowShort, bool midShort, bool hiShort) { + std::array<int, 4> tmp; + tmp[0] = lowShort ? 2 : 0; + tmp[1] = midShort ? 2 : 0; + tmp[2] = hiShort ? 3 : 0; + return tmp; + } +public: + TBlockSize(NBitStream::TBitStream* stream) + : LogCount(Parse(stream)) + {} + TBlockSize(bool lowShort, bool midShort, bool hiShort) + : LogCount(Create(lowShort, midShort, hiShort)) + {} + TBlockSize() + : LogCount({{0, 0, 0, 0}}) + {} + const std::array<int, 4> LogCount; }; -} +} //namespace NAtracDEnc diff --git a/src/atracdenc.cpp b/src/atrac1denc.cpp index 4754256..18b1014 100644 --- a/src/atracdenc.cpp +++ b/src/atrac1denc.cpp @@ -1,6 +1,6 @@ #include <vector> -#include "atracdenc.h" +#include "atrac1denc.h" #include "bitstream/bitstream.h" #include "atrac/atrac1.h" #include "atrac/atrac1_dequantiser.h" @@ -9,28 +9,28 @@ #include "util.h" namespace NAtracDEnc { -using namespace std; using namespace NBitStream; using namespace NAtrac1; using namespace NMDCT; +using std::vector; template<int N> -static vector<double> invertSpectr(double* in) { - vector<double> buf(N); - memcpy(&buf[0], in, N * sizeof(double)); +static vector<TFloat> invertSpectr(const TFloat* in) { + vector<TFloat> buf(N); + memcpy(&buf[0], in, N * sizeof(TFloat)); for (int i = 0; i < N; i+=2) buf[i] *= -1; return buf; } -TAtrac1Processor::TAtrac1Processor(TAeaPtr&& aea, TAtrac1EncodeSettings&& settings) +TAtrac1Processor::TAtrac1Processor(TCompressedIOPtr&& aea, TAtrac1EncodeSettings&& settings) : Aea(std::move(aea)) , Settings(std::move(settings)) { } -static void vector_fmul_window(double *dst, const double *src0, - const double *src1, const double *win, int len) +static void vector_fmul_window(TFloat *dst, const TFloat *src0, + const TFloat *src1, const TFloat *win, int len) { int i, j; @@ -39,21 +39,21 @@ static void vector_fmul_window(double *dst, const double *src0, src0 += len; for (i = -len, j = len - 1; i < 0; i++, j--) { - double s0 = src0[i]; - double s1 = src1[j]; - double wi = win[i]; - double wj = win[j]; + TFloat s0 = src0[i]; + TFloat s1 = src1[j]; + TFloat wi = win[i]; + TFloat wj = win[j]; dst[i] = s0 * wj - s1 * wi; dst[j] = s0 * wi + s1 * wj; } } -vector<double> midct(double* x, int N) { - vector<double> res; +vector<TFloat> midct(TFloat* x, int N) { + vector<TFloat> res; for (int n = 0; n < 2 * N; n++) { - double sum = 0; + TFloat sum = 0; for (int k = 0; k < N; k++) { - sum += (x[k] * cos((M_PI/N) * ((double)n + 0.5 + N/2) * ((double)k + 0.5))); + sum += (x[k] * cos((M_PI/N) * ((TFloat)n + 0.5 + N/2) * ((TFloat)k + 0.5))); } res.push_back(sum); @@ -61,27 +61,27 @@ vector<double> midct(double* x, int N) { return res; } -void TAtrac1MDCT::Mdct(double Specs[512], double* low, double* mid, double* hi, const TBlockSize& blockSize) { +void TAtrac1MDCT::Mdct(TFloat Specs[512], TFloat* low, TFloat* mid, TFloat* hi, const TBlockSize& blockSize) { uint32_t pos = 0; - for (uint32_t band = 0; band < QMF_BANDS; band++) { + for (uint32_t band = 0; band < NumQMF; band++) { const uint32_t numMdctBlocks = 1 << blockSize.LogCount[band]; - double* srcBuf = (band == 0) ? low : (band == 1) ? mid : hi; + TFloat* srcBuf = (band == 0) ? low : (band == 1) ? mid : hi; uint32_t bufSz = (band == 2) ? 256 : 128; const uint32_t blockSz = (numMdctBlocks == 1) ? bufSz : 32; uint32_t winStart = (numMdctBlocks == 1) ? ((band == 2) ? 112 : 48) : 0; //compensate level for 3rd band in case of short window - const double multiple = (numMdctBlocks != 1 && band == 2) ? 2.0 : 1.0; - vector<double> tmp(512); + const TFloat multiple = (numMdctBlocks != 1 && band == 2) ? 2.0 : 1.0; + vector<TFloat> tmp(512); uint32_t blockPos = 0; for (int k = 0; k < numMdctBlocks; ++k) { - memcpy(&tmp[winStart], &srcBuf[bufSz], 32 * sizeof(double)); + memcpy(&tmp[winStart], &srcBuf[bufSz], 32 * sizeof(TFloat)); for (int i = 0; i < 32; i++) { srcBuf[bufSz + i] = TAtrac1Data::SineWindow[i] * srcBuf[blockPos + blockSz - 32 + i]; srcBuf[blockPos + blockSz - 32 + i] = TAtrac1Data::SineWindow[31 - i] * srcBuf[blockPos + blockSz - 32 + i]; } - memcpy(&tmp[winStart+32], &srcBuf[blockPos], blockSz * sizeof(double)); - const vector<double>& sp = (numMdctBlocks == 1) ? ((band == 2) ? Mdct512(&tmp[0]) : Mdct256(&tmp[0])) : Mdct64(&tmp[0]); + memcpy(&tmp[winStart+32], &srcBuf[blockPos], blockSz * sizeof(TFloat)); + const vector<TFloat>& sp = (numMdctBlocks == 1) ? ((band == 2) ? Mdct512(&tmp[0]) : Mdct256(&tmp[0])) : Mdct64(&tmp[0]); for (uint32_t i = 0; i < sp.size(); i++) { Specs[blockPos + pos + i] = sp[i] * multiple; } @@ -94,23 +94,23 @@ void TAtrac1MDCT::Mdct(double Specs[512], double* low, double* mid, double* hi, pos += bufSz; } } -void TAtrac1MDCT::IMdct(double Specs[512], const TBlockSize& mode, double* low, double* mid, double* hi) { +void TAtrac1MDCT::IMdct(TFloat Specs[512], const TBlockSize& mode, TFloat* low, TFloat* mid, TFloat* hi) { uint32_t pos = 0; - for (uint32_t band = 0; band < QMF_BANDS; band++) { + for (uint32_t band = 0; band < NumQMF; band++) { const uint32_t numMdctBlocks = 1 << mode.LogCount[band]; const uint32_t bufSz = (band == 2) ? 256 : 128; const uint32_t blockSz = (numMdctBlocks == 1) ? bufSz : 32; uint32_t start = 0; - double* dstBuf = (band == 0) ? low : (band == 1) ? mid : hi; + TFloat* dstBuf = (band == 0) ? low : (band == 1) ? mid : hi; - vector<double> invBuf(512); - double* prevBuf = &dstBuf[bufSz * 2 - 16]; + vector<TFloat> invBuf(512); + TFloat* prevBuf = &dstBuf[bufSz * 2 - 16]; for (uint32_t block = 0; block < numMdctBlocks; block++) { if (band) { SwapArray(&Specs[pos], blockSz); } - vector<double> inv = (numMdctBlocks != 1) ? midct(&Specs[pos], blockSz) : (bufSz == 128) ? Midct256(&Specs[pos]) : Midct512(&Specs[pos]); + vector<TFloat> inv = (numMdctBlocks != 1) ? midct(&Specs[pos], blockSz) : (bufSz == 128) ? Midct256(&Specs[pos]) : Midct512(&Specs[pos]); for (int i = 0; i < (inv.size()/2); i++) { invBuf[start+i] = inv[i + inv.size()/4]; } @@ -122,7 +122,7 @@ void TAtrac1MDCT::IMdct(double Specs[512], const TBlockSize& mode, double* low, pos += blockSz; } if (numMdctBlocks == 1) - memcpy(dstBuf + 32, &invBuf[16], ((band == 2) ? 240 : 112) * sizeof(double)); + memcpy(dstBuf + 32, &invBuf[16], ((band == 2) ? 240 : 112) * sizeof(TFloat)); for (int j = 0; j < 16; j++) { dstBuf[bufSz*2 - 16 + j] = invBuf[bufSz - 16 + j]; @@ -130,9 +130,9 @@ void TAtrac1MDCT::IMdct(double Specs[512], const TBlockSize& mode, double* low, } } -TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetDecodeLambda() { - return [this](double* data, const TPCMEngine<double>::ProcessMeta& meta) { - double sum[512]; +TPCMEngine<TFloat>::TProcessLambda TAtrac1Processor::GetDecodeLambda() { + return [this](TFloat* data, const TPCMEngine<TFloat>::ProcessMeta& meta) { + TFloat sum[512]; const uint32_t srcChannels = Aea->GetChannelNum(); for (uint32_t channel = 0; channel < srcChannels; channel++) { std::unique_ptr<TAea::TFrame> frame(Aea->ReadFrame()); @@ -141,7 +141,7 @@ TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetDecodeLambda() { TBlockSize mode(&bitstream); TAtrac1Dequantiser dequantiser; - vector<double> specs; + vector<TFloat> specs; specs.resize(512);; dequantiser.Dequant(&bitstream, mode, &specs[0]); @@ -161,7 +161,7 @@ TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetDecodeLambda() { } -TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetEncodeLambda() { +TPCMEngine<TFloat>::TProcessLambda TAtrac1Processor::GetEncodeLambda() { const uint32_t srcChannels = Aea->GetChannelNum(); vector<IAtrac1BitAlloc*> bitAlloc; for (int i = 0; i < srcChannels; i++) { @@ -171,10 +171,10 @@ TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetEncodeLambda() { bitAlloc.push_back(new TAtrac1SimpleBitAlloc(atrac1container, Settings.GetBfuIdxConst(), Settings.GetFastBfuNumSearch())); } - return [this, srcChannels, bitAlloc](double* data, const TPCMEngine<double>::ProcessMeta& meta) { + return [this, srcChannels, bitAlloc](TFloat* data, const TPCMEngine<TFloat>::ProcessMeta& meta) { for (uint32_t channel = 0; channel < srcChannels; channel++) { - double src[NumSamples]; - vector<double> specs(512); + TFloat src[NumSamples]; + vector<TFloat> specs(512); for (int i = 0; i < NumSamples; ++i) { src[i] = data[i * srcChannels + channel]; } @@ -185,10 +185,10 @@ TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetEncodeLambda() { if (Settings.GetWindowMode() == TAtrac1EncodeSettings::EWindowMode::EWM_AUTO) { windowMask |= (uint32_t)TransientDetectors.GetDetector(channel, 0).Detect(&PcmBufLow[channel][0]); - const vector<double>& invMid = invertSpectr<128>(&PcmBufMid[channel][0]); + const vector<TFloat>& invMid = invertSpectr<128>(&PcmBufMid[channel][0]); windowMask |= (uint32_t)TransientDetectors.GetDetector(channel, 1).Detect(&invMid[0]) << 1; - const vector<double>& invHi = invertSpectr<256>(&PcmBufHi[channel][0]); + const vector<TFloat>& invHi = invertSpectr<256>(&PcmBufHi[channel][0]); windowMask |= (uint32_t)TransientDetectors.GetDetector(channel, 2).Detect(&invHi[0]) << 2; //std::cout << "trans: " << windowMask << std::endl; @@ -198,15 +198,10 @@ TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetEncodeLambda() { } const TBlockSize blockSize(windowMask & 0x1, windowMask & 0x2, windowMask & 0x4); //low, mid, hi - //for (int i = 0; i < 256; ++i) { - // std::cout << PcmBufHi[channel][i] << std::endl; - //} - //std::cout<< "============" << std::endl; Mdct(&specs[0], &PcmBufLow[channel][0], &PcmBufMid[channel][0], &PcmBufHi[channel][0], blockSize); - bitAlloc[channel]->Write(Scaler.Scale(specs, blockSize), blockSize); + bitAlloc[channel]->Write(Scaler.ScaleFrame(specs, blockSize), blockSize); } }; } - -} +} //namespace NAtracDEnc diff --git a/src/atracdenc.h b/src/atrac1denc.h index 8a9d59f..693a468 100644 --- a/src/atracdenc.h +++ b/src/atrac1denc.h @@ -1,6 +1,5 @@ #pragma once #include "pcmengin.h" -#include "atrac3denc.h" #include "aea.h" #include "oma.h" #include "atrac_encode_settings.h" @@ -21,34 +20,34 @@ enum EMode { E_ATRAC3 = 4 }; -class TAtrac1MDCT : public virtual TAtrac1Data { +class TAtrac1MDCT : public virtual NAtrac1::TAtrac1Data { NMDCT::TMDCT<512> Mdct512; NMDCT::TMDCT<256> Mdct256; NMDCT::TMDCT<64> Mdct64; NMDCT::TMIDCT<512> Midct512; NMDCT::TMIDCT<256> Midct256; public: - void IMdct(double specs[512], const TBlockSize& mode, double* low, double* mid, double* hi); - void Mdct(double specs[512], double* low, double* mid, double* hi, const TBlockSize& blockSize); + void IMdct(TFloat specs[512], const TBlockSize& mode, TFloat* low, TFloat* mid, TFloat* hi); + void Mdct(TFloat specs[512], TFloat* low, TFloat* mid, TFloat* hi, const TBlockSize& blockSize); TAtrac1MDCT() : Mdct512(2) , Mdct256(1) {} }; -class TAtrac1Processor : public IProcessor<double>, public TAtrac1MDCT, public virtual TAtrac1Data { - TAeaPtr Aea; - const TAtrac1EncodeSettings Settings; +class TAtrac1Processor : public IProcessor<TFloat>, public TAtrac1MDCT, public virtual NAtrac1::TAtrac1Data { + TCompressedIOPtr Aea; + const NAtrac1::TAtrac1EncodeSettings Settings; - double PcmBufLow[2][256 + 16]; - double PcmBufMid[2][256 + 16]; - double PcmBufHi[2][512 + 16]; + TFloat PcmBufLow[2][256 + 16]; + TFloat PcmBufMid[2][256 + 16]; + TFloat PcmBufHi[2][512 + 16]; int32_t PcmValueMax = 32767; int32_t PcmValueMin = -32767; - Atrac1SynthesisFilterBank<double> SynthesisFilterBank[2]; - Atrac1SplitFilterBank<double> SplitFilterBank[2]; + Atrac1SynthesisFilterBank<TFloat> SynthesisFilterBank[2]; + Atrac1SplitFilterBank<TFloat> SplitFilterBank[2]; class TTransientDetectors { std::vector<TTransientDetector> transientDetectorLow; @@ -82,9 +81,9 @@ class TAtrac1Processor : public IProcessor<double>, public TAtrac1MDCT, public v TScaler<TAtrac1Data> Scaler; public: - TAtrac1Processor(TAeaPtr&& aea, TAtrac1EncodeSettings&& settings); - TPCMEngine<double>::TProcessLambda GetDecodeLambda() override; + TAtrac1Processor(TCompressedIOPtr&& aea, NAtrac1::TAtrac1EncodeSettings&& settings); + TPCMEngine<TFloat>::TProcessLambda GetDecodeLambda() override; - TPCMEngine<double>::TProcessLambda GetEncodeLambda() override; + TPCMEngine<TFloat>::TProcessLambda GetEncodeLambda() override; }; } diff --git a/src/atrac3denc.cpp b/src/atrac3denc.cpp index 7557d5e..432fb18 100644 --- a/src/atrac3denc.cpp +++ b/src/atrac3denc.cpp @@ -1,22 +1,23 @@ #include "atrac3denc.h" -#include "atrac/atrac3_bitstream.h" +#include "transient_detector.h" #include "util.h" #include <assert.h> - +#include <algorithm> #include <iostream> - +#include <cmath> namespace NAtracDEnc { using namespace NMDCT; +using namespace NAtrac3; using std::vector; -void TAtrac3MDCT::Mdct(double specs[1024], double* bands[4], TGainModulatorArray gainModulators) { +void TAtrac3MDCT::Mdct(TFloat specs[1024], TFloat* bands[4], TGainModulatorArray gainModulators) { for (int band = 0; band < 4; ++band) { - double* srcBuff = bands[band]; - double* const curSpec = &specs[band*256]; + TFloat* srcBuff = bands[band]; + TFloat* const curSpec = &specs[band*256]; TGainModulator modFn = gainModulators[band]; - vector<double> tmp(512); - memcpy(&tmp[0], &srcBuff[256], 256 * sizeof(double)); + vector<TFloat> tmp(512); + memcpy(&tmp[0], &srcBuff[256], 256 * sizeof(TFloat)); if (modFn) { modFn(tmp.data(), srcBuff); } @@ -24,30 +25,30 @@ void TAtrac3MDCT::Mdct(double specs[1024], double* bands[4], TGainModulatorArray srcBuff[256+i] = TAtrac3Data::EncodeWindow[i] * srcBuff[i]; srcBuff[i] = TAtrac3Data::EncodeWindow[255-i] * srcBuff[i]; } - memcpy(&tmp[256], &srcBuff[0], 256 * sizeof(double)); - const vector<double>& sp = Mdct512(&tmp[0]); + memcpy(&tmp[256], &srcBuff[0], 256 * sizeof(TFloat)); + const vector<TFloat>& sp = Mdct512(&tmp[0]); assert(sp.size() == 256); - memcpy(curSpec, sp.data(), 256 * sizeof(double)); + memcpy(curSpec, sp.data(), 256 * sizeof(TFloat)); if (band & 1) { SwapArray(curSpec, 256); } } } -void TAtrac3MDCT::Midct(double specs[1024], double* bands[4], TGainDemodulatorArray gainDemodulators) { +void TAtrac3MDCT::Midct(TFloat specs[1024], TFloat* bands[4], TGainDemodulatorArray gainDemodulators) { for (int band = 0; band < 4; ++band) { - double* dstBuff = bands[band]; - double* curSpec = &specs[band*256]; - double* prevBuff = dstBuff + 256; + TFloat* dstBuff = bands[band]; + TFloat* curSpec = &specs[band*256]; + TFloat* prevBuff = dstBuff + 256; TAtrac3GainProcessor::TGainDemodulator demodFn = gainDemodulators[band]; if (band & 1) { SwapArray(curSpec, 256); } - vector<double> inv = Midct512(curSpec); + vector<TFloat> inv = Midct512(curSpec); assert(inv.size()/2 == 256); for (int j = 0; j < 256; ++j) { - inv[j] *= 2 * DecodeWindow[j]; - inv[511 - j] *= 2 * DecodeWindow[j]; + inv[j] *= /*2 */ DecodeWindow[j]; + inv[511 - j] *= /*2*/ DecodeWindow[j]; } if (demodFn) { demodFn(dstBuff, inv.data(), prevBuff); @@ -56,13 +57,14 @@ void TAtrac3MDCT::Midct(double specs[1024], double* bands[4], TGainDemodulatorAr dstBuff[j] = inv[j] + prevBuff[j]; } } - memcpy(prevBuff, &inv[256], sizeof(double)*256); + memcpy(prevBuff, &inv[256], sizeof(TFloat)*256); } } -TAtrac3Processor::TAtrac3Processor(TAeaPtr&& oma, const TContainerParams& params) +TAtrac3Processor::TAtrac3Processor(TCompressedIOPtr&& oma, TAtrac3EncoderSettings&& encoderSettings) : Oma(std::move(oma)) - , Params(params) + , Params(std::move(encoderSettings)) + , TransientDetectors(2 * 4, TTransientDetector(8, 256)) //2 - channels, 4 - bands {} TAtrac3Processor::~TAtrac3Processor() @@ -97,35 +99,111 @@ TAtrac3MDCT::TGainModulatorArray TAtrac3MDCT::MakeGainModulatorArray(const TAtra } } -TPCMEngine<double>::TProcessLambda TAtrac3Processor::GetEncodeLambda() { +//TODO: +TAtrac3Data::TTonalComponents TAtrac3Processor::ExtractTonalComponents(TFloat* specs, TTonalDetector fn) { + TAtrac3Data::TTonalComponents res; + const float thresholds[TAtrac3Data::NumQMF] = { 0.9, 2.4, 2.8, 3.2 }; + for (uint8_t bandNum = 0; bandNum < this->NumQMF; ++bandNum) { + //disable for frequence above 16KHz until we works without proper psy + if (bandNum > 2) + continue; + for (uint8_t blockNum = BlocksPerBand[bandNum]; blockNum < BlocksPerBand[bandNum + 1]; ++blockNum) { + const uint16_t specNumStart = SpecsStartLong[blockNum]; + const uint16_t specNumEnd = specNumStart + SpecsPerBlock[blockNum]; + float level = fn(specs + specNumStart, SpecsPerBlock[blockNum]); + if (!isnan(level)) { + for (uint16_t n = specNumStart; n < specNumEnd; ++n) { + //TODO: + TFloat absValue = std::abs(specs[n]); + if (absValue > 65535.0) { + TFloat shift = (specs[n] > 0) ? 65535.0 : -65535.0; + + std::cerr << "shift overflowed value " << specs[n] << " " << specs[n] - shift << " " << shift << std::endl; + res.push_back({n, specs[n] - shift}); + specs[n] = shift; + } else if (log10(std::abs(specs[n])) - log10(level) > thresholds[bandNum]) { + res.push_back({n, specs[n]/* - level*/}); + specs[n] = 0;//level; + } + + } + + } + } + } + return res; +} +std::vector<TTonalComponent> TAtrac3Processor::MapTonalComponents(const TTonalComponents& tonalComponents) { + vector<TTonalComponent> componentMap; + for (uint16_t i = 0; i < tonalComponents.size();) { + const uint16_t startPos = i; + uint16_t curPos; + do { + curPos = tonalComponents[i].Pos; + ++i; + } while ( i < tonalComponents.size() && tonalComponents[i].Pos == curPos + 1 && i - startPos < 7); + const uint16_t len = i - startPos; + TFloat tmp[8]; + for (uint8_t j = 0; j < len; ++j) + tmp[j] = tonalComponents[startPos + j].Val; + const TScaledBlock& scaledBlock = Scaler.Scale(tmp, len); + componentMap.push_back({&tonalComponents[startPos], 7, scaledBlock}); + } + return componentMap; +} + +TAtrac3Data::SubbandInfo TAtrac3Processor::CreateSubbandInfo(TFloat* in[4], uint32_t channel, TTransientDetector* transientDetector) { + assert(false); //not implemented + return {}; +} + +TPCMEngine<TFloat>::TProcessLambda TAtrac3Processor::GetEncodeLambda() { TOma* omaptr = dynamic_cast<TOma*>(Oma.get()); if (!omaptr) { std::cerr << "Wrong container" << std::endl; abort(); } - TAtrac3BitStreamWriter* bitStreamWriter = new TAtrac3BitStreamWriter(omaptr, Params); - return [this, bitStreamWriter](double* data, const TPCMEngine<double>::ProcessMeta& meta) { + TAtrac3BitStreamWriter* bitStreamWriter = new TAtrac3BitStreamWriter(omaptr, *Params.ConteinerParams); + return [this, bitStreamWriter](TFloat* data, const TPCMEngine<TFloat>::ProcessMeta& meta) { for (uint32_t channel=0; channel < 2; channel++) { - vector<double> specs(1024); - double src[NumSamples]; + vector<TFloat> specs(1024); + TFloat src[NumSamples]; for (int i = 0; i < NumSamples; ++i) { - src[i] = data[meta.Channels == 1 ? i : (i * 2 + channel)]; //no mono mode in atrac3. //TODO we can double frame after encoding + src[i] = data[meta.Channels == 1 ? i : (i * 2 + channel)] / 4.0; //no mono mode in atrac3. //TODO we can TFloat frame after encoding } - double* p[4] = {&PcmBuffer[channel][0][0], &PcmBuffer[channel][1][0], &PcmBuffer[channel][2][0], &PcmBuffer[channel][3][0]}; + TFloat* p[4] = {&PcmBuffer[channel][0][0], &PcmBuffer[channel][1][0], &PcmBuffer[channel][2][0], &PcmBuffer[channel][3][0]}; SplitFilterBank[channel].Split(&src[0], p); - - TAtrac3Data::SubbandInfo siCur; + + TAtrac3Data::SubbandInfo siCur = Params.NoGainControll ? + TAtrac3Data::SubbandInfo() : CreateSubbandInfo(p, channel, &TransientDetectors[channel*4]); //4 detectors per band Mdct(specs.data(), p, MakeGainModulatorArray(siCur)); - const TBlockSize blockSize(false, false, false); - bitStreamWriter->WriteSoundUnit(siCur, Scaler.Scale(specs, blockSize)); + TTonalComponents tonals = Params.NoTonalComponents ? + TAtrac3Data::TTonalComponents() : ExtractTonalComponents(specs.data(), [](const TFloat* spec, uint16_t len) { + std::vector<TFloat> magnitude(len); + for (uint16_t i = 0; i < len; ++i) { + magnitude[i] = std::abs(spec[i]); + } + float median = CalcMedian(magnitude.data(), len); + for (uint16_t i = 0; i < len; ++i) { + if (median > 0.001) { + return median; + } + } + return NAN; + }); + + const std::vector<TTonalComponent>& components = MapTonalComponents(tonals); + + //TBlockSize for ATRAC3 - 4 subband, all are long (no short window) + bitStreamWriter->WriteSoundUnit(siCur, components, Scaler.ScaleFrame(specs, TBlockSize())); } }; } -TPCMEngine<double>::TProcessLambda TAtrac3Processor::GetDecodeLambda() { +TPCMEngine<TFloat>::TProcessLambda TAtrac3Processor::GetDecodeLambda() { abort(); return {}; } diff --git a/src/atrac3denc.h b/src/atrac3denc.h index 149f1c6..5984728 100644 --- a/src/atrac3denc.h +++ b/src/atrac3denc.h @@ -1,10 +1,13 @@ #pragma once +#include "config.h" #include "pcmengin.h" #include "oma.h" #include "aea.h" #include "atrac/atrac3.h" #include "atrac/atrac3_qmf.h" +#include "transient_detector.h" +#include "atrac/atrac3_bitstream.h" #include "atrac/atrac_scale.h" #include "mdct/mdct.h" #include "gain_processor.h" @@ -13,33 +16,54 @@ #include <array> namespace NAtracDEnc { -class TAtrac3MDCT : public virtual TAtrac3Data { +class TAtrac3MDCT : public virtual NAtrac3::TAtrac3Data { NMDCT::TMDCT<512> Mdct512; NMDCT::TMIDCT<512> Midct512; public: typedef TGainProcessor<TAtrac3Data> TAtrac3GainProcessor; TAtrac3GainProcessor GainProcessor; + TAtrac3MDCT() + : Mdct512(2) + {} public: using TGainModulator = TAtrac3GainProcessor::TGainModulator; using TGainDemodulator = TAtrac3GainProcessor::TGainDemodulator; typedef std::array<TGainDemodulator, 4> TGainDemodulatorArray; typedef std::array<TGainModulator, 4> TGainModulatorArray; - void Mdct(double specs[1024], double* bands[4], TGainModulatorArray gainModulators = TGainModulatorArray()); - void Midct(double specs[1024], double* bands[4], TGainDemodulatorArray gainDemodulators = TGainDemodulatorArray()); + void Mdct(TFloat specs[1024], TFloat* bands[4], TGainModulatorArray gainModulators = TGainModulatorArray()); + void Midct(TFloat specs[1024], TFloat* bands[4], TGainDemodulatorArray gainDemodulators = TGainDemodulatorArray()); protected: TAtrac3MDCT::TGainModulatorArray MakeGainModulatorArray(const TAtrac3Data::SubbandInfo& si); }; -class TAtrac3Processor : public IProcessor<double>, public TAtrac3MDCT, public virtual TAtrac3Data { - TAeaPtr Oma; - const TContainerParams Params; - double PcmBuffer[2][4][256 + 256]; //2 channel, 4 band, 256 sample + 256 for overlap buffer - Atrac3SplitFilterBank<double> SplitFilterBank[2]; +//returns threshhold +typedef std::function<float(const TFloat* p, uint16_t len)> TTonalDetector; + +class TAtrac3Processor : public IProcessor<TFloat>, public TAtrac3MDCT, public virtual NAtrac3::TAtrac3Data { + TCompressedIOPtr Oma; + const NAtrac3::TAtrac3EncoderSettings Params; + TFloat PcmBuffer[2][4][256 + 256]; //2 channel, 4 band, 256 sample + 256 for overlap buffer + Atrac3SplitFilterBank<TFloat> SplitFilterBank[2]; TScaler<TAtrac3Data> Scaler; + std::vector<TTransientDetector> TransientDetectors; + typedef std::array<uint8_t, NumSpecs> TonalComponentMask; +#ifdef ATRAC_UT_PUBLIC +public: +#endif + uint32_t CheckLevelOverflow(const std::vector<TFloat>& gain, const TAtrac3Data::SubbandInfo::TGainPoint& point); + std::vector<SubbandInfo::TGainPoint> FilterCurve(const std::vector<TFloat>& gain, + const std::vector<SubbandInfo::TGainPoint>& curve, + const int threshold); + TFloat LimitRel(TFloat x); + TAtrac3Data::SubbandInfo CreateSubbandInfo(TFloat* in[4], uint32_t channel, TTransientDetector* transientDetector); + TonalComponentMask AnalyzeTonalComponent(TFloat* specs); + TTonalComponents ExtractTonalComponents(TFloat* specs, TTonalDetector fn); + + std::vector<NAtrac3::TTonalComponent> MapTonalComponents(const TTonalComponents& tonalComponents); public: - TAtrac3Processor(TAeaPtr&& oma, const TContainerParams& params); + TAtrac3Processor(TCompressedIOPtr&& oma, NAtrac3::TAtrac3EncoderSettings&& encoderSettings); ~TAtrac3Processor(); - TPCMEngine<double>::TProcessLambda GetDecodeLambda() override; - TPCMEngine<double>::TProcessLambda GetEncodeLambda() override; + TPCMEngine<TFloat>::TProcessLambda GetDecodeLambda() override; + TPCMEngine<TFloat>::TProcessLambda GetEncodeLambda() override; }; } diff --git a/src/atrac3denc_ut.cpp b/src/atrac3denc_ut.cpp index f15c799..e0602a2 100644 --- a/src/atrac3denc_ut.cpp +++ b/src/atrac3denc_ut.cpp @@ -1,3 +1,5 @@ +#define ATRAC_UT_PUBLIC + #include "atrac3denc.h" #include <gtest/gtest.h> @@ -5,21 +7,32 @@ #include <cmath> using std::vector; using namespace NAtracDEnc; +using namespace NAtrac3; -static void GenerateSignal(double* buf, size_t n, double f, double a) { +static void GenerateSignal(TFloat* buf, size_t n, TFloat f, TFloat a) { for (size_t i = 0; i < n; ++i) { buf[i] = a * sin((M_PI/2) * i * f); } } +static void GenerateSignalWithTransient(TFloat* buf, size_t n, TFloat f, TFloat a, + size_t transientPos, size_t transientLen, TFloat transientLev) { + assert(transientPos + transientLen < n); + GenerateSignal(buf, n, f, a); + GenerateSignal(buf+transientPos, transientLen, f, transientLev); +// for (size_t i = transientPos; i < transientPos + transientLen; ++i) { +// buf[i] += (i & 1) ? transientLev : - transientLev; +// } +} + class TWindowTest : public TAtrac3Data { public: void RunTest() { for (size_t i = 0; i < 256; i++) { - const double ha1 = EncodeWindow[i] / 2.0; //compensation - const double hs1 = DecodeWindow[i]; - const double hs2 = DecodeWindow[255-i]; - const double res = hs1 / (hs1 * hs1 + hs2 * hs2); + const TFloat ha1 = EncodeWindow[i] / 2.0; //compensation + const TFloat hs1 = DecodeWindow[i]; + const TFloat hs2 = DecodeWindow[255-i]; + const TFloat res = hs1 / (hs1 * hs1 + hs2 * hs2); EXPECT_NEAR(ha1, res, 0.000000001); } } @@ -62,18 +75,18 @@ public: TEST(TAtrac3MDCT, TAtrac3MDCTZeroOneBlock) { TAtrac3MDCT mdct; - TAtrac3MDCTWorkBuff<double> buff; - size_t workSz = TAtrac3MDCTWorkBuff<double>::BandBuffSz; + TAtrac3MDCTWorkBuff<TFloat> buff; + size_t workSz = TAtrac3MDCTWorkBuff<TFloat>::BandBuffSz; - vector<double> specs(1024); + vector<TFloat> specs(1024); - double* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; + TFloat* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; mdct.Mdct(specs.data(), p); for(auto s: specs) EXPECT_NEAR(s, 0.0, 0.0000000001); - double* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; + TFloat* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; mdct.Midct(specs.data(), p); for(size_t i = 0; i < workSz; ++i) @@ -93,25 +106,25 @@ TEST(TAtrac3MDCT, TAtrac3MDCTZeroOneBlock) { TEST(TAtrac3MDCT, TAtrac3MDCTSignal) { TAtrac3MDCT mdct; - TAtrac3MDCTWorkBuff<double> buff; - size_t workSz = TAtrac3MDCTWorkBuff<double>::BandBuffSz; + TAtrac3MDCTWorkBuff<TFloat> buff; + size_t workSz = TAtrac3MDCTWorkBuff<TFloat>::BandBuffSz; const size_t len = 1024; - vector<double> signal(len); - vector<double> signalRes(len); + vector<TFloat> signal(len); + vector<TFloat> signalRes(len); GenerateSignal(signal.data(), signal.size(), 0.25, 32768); for (size_t pos = 0; pos < len; pos += workSz) { - vector<double> specs(1024); - memcpy(buff.Band0, signal.data() + pos, workSz * sizeof(double)); + vector<TFloat> specs(1024); + memcpy(buff.Band0, signal.data() + pos, workSz * sizeof(TFloat)); - double* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; + TFloat* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; mdct.Mdct(specs.data(), p); - double* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; + TFloat* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; mdct.Midct(specs.data(), t); - memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(double)); + memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(TFloat)); } for (int i = workSz; i < len; ++i) @@ -120,19 +133,19 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignal) { TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { TAtrac3MDCT mdct; - TAtrac3MDCTWorkBuff<double> buff; - size_t workSz = TAtrac3MDCTWorkBuff<double>::BandBuffSz; + TAtrac3MDCTWorkBuff<TFloat> buff; + size_t workSz = TAtrac3MDCTWorkBuff<TFloat>::BandBuffSz; const size_t len = 4096; - vector<double> signal(len, 8000); - vector<double> signalRes(len); + vector<TFloat> signal(len, 8000); + vector<TFloat> signalRes(len); GenerateSignal(signal.data() + 1024, signal.size()-1024, 0.25, 32768); for (size_t pos = 0; pos < len; pos += workSz) { - vector<double> specs(1024); - memcpy(buff.Band0, signal.data() + pos, workSz * sizeof(double)); + vector<TFloat> specs(1024); + memcpy(buff.Band0, signal.data() + pos, workSz * sizeof(TFloat)); - double* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; + TFloat* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; if (pos == 256) { //apply gain modulation TAtrac3Data::SubbandInfo siCur; @@ -142,8 +155,8 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator()}); } else if (pos == 1024) { TAtrac3Data::SubbandInfo siCur; - siCur.AddSubbandCurve(0, {{3, 2}}); - siCur.AddSubbandCurve(0, {{2, 5}}); + std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve = {{3, 2}, {2, 5}}; + siCur.AddSubbandCurve(0, std::move(curve)); mdct.Mdct(specs.data(), p, { mdct.GainProcessor.Modulate(siCur.GetGainPoints(0)), TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator()}); @@ -155,8 +168,8 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator()}); } else if (pos == 2048) { TAtrac3Data::SubbandInfo siCur; - siCur.AddSubbandCurve(0, {{4, 2}}); - siCur.AddSubbandCurve(0, {{1, 5}}); + std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve = {{4, 2}, {1, 5}}; + siCur.AddSubbandCurve(0, std::move(curve)); mdct.Mdct(specs.data(), p, { mdct.GainProcessor.Modulate(siCur.GetGainPoints(0)), TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator()}); @@ -164,7 +177,7 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { mdct.Mdct(specs.data(), p); } - double* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; + TFloat* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; if (pos == 256) { //restore gain modulation TAtrac3Data::SubbandInfo siCur; @@ -187,8 +200,8 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { } else if (pos == 1024) { TAtrac3Data::SubbandInfo siCur; TAtrac3Data::SubbandInfo siNext; - siNext.AddSubbandCurve(0, {{3, 2}}); - siNext.AddSubbandCurve(0, {{2, 5}}); + std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve = {{3, 2}, {2, 5}}; + siNext.AddSubbandCurve(0, std::move(curve)); mdct.Midct(specs.data(), t, {mdct.GainProcessor.Demodulate(siCur.GetGainPoints(0), siNext.GetGainPoints(0)), TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator(), @@ -198,8 +211,8 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { TAtrac3Data::SubbandInfo siNext; TAtrac3Data::SubbandInfo siCur; siNext.AddSubbandCurve(0, {{1, 0}}); - siCur.AddSubbandCurve(0, {{3, 2}}); - siCur.AddSubbandCurve(0, {{2, 5}}); + std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve = {{3, 2}, {2, 5}}; + siCur.AddSubbandCurve(0, std::move(curve)); mdct.Midct(specs.data(), t, {mdct.GainProcessor.Demodulate(siCur.GetGainPoints(0), siNext.GetGainPoints(0)), TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator(), @@ -217,8 +230,8 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { } else if (pos == 2048) { TAtrac3Data::SubbandInfo siCur; TAtrac3Data::SubbandInfo siNext; - siNext.AddSubbandCurve(0, {{4, 2}}); - siNext.AddSubbandCurve(0, {{1, 5}}); + std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve = {{4, 2}, {1, 5}}; + siNext.AddSubbandCurve(0, std::move(curve)); mdct.Midct(specs.data(), t, {mdct.GainProcessor.Demodulate(siCur.GetGainPoints(0), siNext.GetGainPoints(0)), TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator(), @@ -227,8 +240,8 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { } else if (pos == 2048 + 256) { TAtrac3Data::SubbandInfo siNext; TAtrac3Data::SubbandInfo siCur; - siCur.AddSubbandCurve(0, {{4, 2}}); - siCur.AddSubbandCurve(0, {{1, 5}}); + std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve = {{4, 2}, {1, 5}}; + siCur.AddSubbandCurve(0, std::move(curve)); mdct.Midct(specs.data(), t, {mdct.GainProcessor.Demodulate(siCur.GetGainPoints(0), siNext.GetGainPoints(0)), TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator(), @@ -237,7 +250,7 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { } else { mdct.Midct(specs.data(), t); } - memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(double)); + memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(TFloat)); } for (int i = workSz; i < len; ++i) { //std::cout << "res: " << i << " " << signalRes[i] << std::endl; @@ -245,9 +258,78 @@ TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensation) { } } +TEST(TAtrac3MDCT, TAtrac3MDCTSignalWithGainCompensationAndManualTransient) { + TAtrac3MDCT mdct; + TAtrac3MDCTWorkBuff<TFloat> buff; + size_t workSz = TAtrac3MDCTWorkBuff<TFloat>::BandBuffSz; + + const size_t len = 1024; + vector<TFloat> signal(len); + vector<TFloat> signalRes(len); + GenerateSignalWithTransient(signal.data(), signal.size(), 0.03125, 512.0, + 640, 64, 32768.0); + const std::vector<TAtrac3Data::SubbandInfo::TGainPoint> curve1 = {{6, 13}, {4, 14}}; + + for (size_t pos = 0; pos < len; pos += workSz) { + vector<TFloat> specs(1024); + memcpy(buff.Band0, signal.data() + pos, workSz * sizeof(TFloat)); + + TFloat* p[4] = { buff.Band0, buff.Band1, buff.Band2, buff.Band3 }; + //for (int i = 0; i < 256; i++) { + // std::cout << i + pos << " " << buff.Band0[i] << std::endl; + //} + + if (pos == 512) { //apply gain modulation + TAtrac3Data::SubbandInfo siCur; + siCur.AddSubbandCurve(0, std::vector<TAtrac3Data::SubbandInfo::TGainPoint>(curve1)); + + for (int i = 0; i < 256; i++) { + std::cout << i << " " << buff.Band0[i] << std::endl; + } + + mdct.Mdct(specs.data(), p, { mdct.GainProcessor.Modulate(siCur.GetGainPoints(0)), + TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator(), TAtrac3MDCT::TGainModulator()}); + } else { + mdct.Mdct(specs.data(), p); + } + + for (int i = 0; i < specs.size(); ++i) { + if (i > 240 && i < 256) + specs[i] /= 1.9; + } + TFloat* t[4] = { buff.Band0Res, buff.Band1Res, buff.Band2Res, buff.Band3Res }; + if (pos == 512) { //restore gain modulation + TAtrac3Data::SubbandInfo siCur; + TAtrac3Data::SubbandInfo siNext; + siNext.AddSubbandCurve(0, std::vector<TAtrac3Data::SubbandInfo::TGainPoint>(curve1)); + mdct.Midct(specs.data(), t, {mdct.GainProcessor.Demodulate(siCur.GetGainPoints(0), siNext.GetGainPoints(0)), + TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator(), + TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator(), + TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator()}); + } else if (pos == 768) { + TAtrac3Data::SubbandInfo siNext; + TAtrac3Data::SubbandInfo siCur; + siCur.AddSubbandCurve(0, std::vector<TAtrac3Data::SubbandInfo::TGainPoint>(curve1)); + + mdct.Midct(specs.data(), t, {mdct.GainProcessor.Demodulate(siCur.GetGainPoints(0), siNext.GetGainPoints(0)), + TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator(), + TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator(), + TAtrac3MDCT::TAtrac3GainProcessor::TGainDemodulator()}); + } else { + mdct.Midct(specs.data(), t); + } + memcpy(signalRes.data() + pos, buff.Band0Res, workSz * sizeof(TFloat)); + } + for (int i = workSz; i < len; ++i) { + //std::cout << "res: " << i << " " << signalRes[i] << std::endl; + EXPECT_NEAR(signal[i - workSz], signalRes[i], 10); + } +} TEST(TAtrac3MDCT, TAtrac3MDCTWindow) { TWindowTest test; test.RunTest(); } + + diff --git a/src/atrac_encode_settings.h b/src/atrac_encode_settings.h index e3ae3b7..fd96fa4 100644 --- a/src/atrac_encode_settings.h +++ b/src/atrac_encode_settings.h @@ -2,29 +2,4 @@ namespace NAtracDEnc { -class TAtrac1EncodeSettings { -public: - enum class EWindowMode { - EWM_NOTRANSIENT, - EWM_AUTO - }; -private: - const uint32_t BfuIdxConst = 0; - const bool FastBfuNumSearch = false; - EWindowMode WindowMode = EWindowMode::EWM_AUTO; - const uint32_t WindowMask = 0; -public: - TAtrac1EncodeSettings(); - TAtrac1EncodeSettings(uint32_t bfuIdxConst, bool fastBfuNumSearch, EWindowMode windowMode, uint32_t windowMask) - : BfuIdxConst(bfuIdxConst) - , FastBfuNumSearch(fastBfuNumSearch) - , WindowMode(windowMode) - , WindowMask(windowMask) - {} - uint32_t GetBfuIdxConst() const { return BfuIdxConst; } - bool GetFastBfuNumSearch() const { return FastBfuNumSearch; } - EWindowMode GetWindowMode() const {return WindowMode; } - uint32_t GetWindowMask() const {return WindowMask; } -}; - } diff --git a/src/atracdenc_ut.cpp b/src/atracdenc_ut.cpp index 5370e9e..5d5f5eb 100644 --- a/src/atracdenc_ut.cpp +++ b/src/atracdenc_ut.cpp @@ -1,16 +1,16 @@ -#include "atracdenc.h" +#include "atrac1denc.h" #include <gtest/gtest.h> #include <vector> using std::vector; using namespace NAtracDEnc; -void CheckResult128(const vector<double>& a, const vector<double>& b) { +void CheckResult128(const vector<TFloat>& a, const vector<TFloat>& b) { for (int i = 0; i < 96; ++i ) { EXPECT_NEAR(a[i], 4 * b[i+32], 0.0000001); } } -void CheckResult256(const vector<double>& a, const vector<double>& b) { +void CheckResult256(const vector<TFloat>& a, const vector<TFloat>& b) { for (int i = 0; i < 192; ++i ) { EXPECT_NEAR(a[i], 2 * b[i+32], 0.0000001); } @@ -19,14 +19,14 @@ void CheckResult256(const vector<double>& a, const vector<double>& b) { TEST(TAtrac1MDCT, TAtrac1MDCTLongEncDec) { TAtrac1MDCT mdct; - vector<double> low(128 * 2); - vector<double> mid(128 * 2); - vector<double> hi(256 * 2); - vector<double> specs(512 * 2); - - vector<double> lowRes(128 * 2); - vector<double> midRes(128 * 2); - vector<double> hiRes(256 * 2); + vector<TFloat> low(128 * 2); + vector<TFloat> mid(128 * 2); + vector<TFloat> hi(256 * 2); + vector<TFloat> specs(512 * 2); + + vector<TFloat> lowRes(128 * 2); + vector<TFloat> midRes(128 * 2); + vector<TFloat> hiRes(256 * 2); for (int i = 0; i < 128; i++) { low[i] = mid[i] = i; @@ -48,25 +48,25 @@ TEST(TAtrac1MDCT, TAtrac1MDCTLongEncDec) { TEST(TAtrac1MDCT, TAtrac1MDCTShortEncDec) { TAtrac1MDCT mdct; - vector<double> low(128 * 2); - vector<double> mid(128 * 2); - vector<double> hi(256 * 2); - vector<double> specs(512 * 2); - - vector<double> lowRes(128 * 2); - vector<double> midRes(128 * 2); - vector<double> hiRes(256 * 2); + vector<TFloat> low(128 * 2); + vector<TFloat> mid(128 * 2); + vector<TFloat> hi(256 * 2); + vector<TFloat> specs(512 * 2); + + vector<TFloat> lowRes(128 * 2); + vector<TFloat> midRes(128 * 2); + vector<TFloat> hiRes(256 * 2); for (int i = 0; i < 128; i++) { low[i] = mid[i] = i; } - const vector<double> lowCopy = low; //in case of short wondow AtracMDCT changed input buffer during calculation - const vector<double> midCopy = mid; + const vector<TFloat> lowCopy = low; //in case of short wondow AtracMDCT changed input buffer during calculation + const vector<TFloat> midCopy = mid; for (int i = 0; i < 256; i++) { hi[i] = i; } - const vector<double> hiCopy = hi; + const vector<TFloat> hiCopy = hi; const TBlockSize blockSize(true, true, true); //short diff --git a/src/bitstream/bitstream.cpp b/src/bitstream/bitstream.cpp index d916f52..e8f1857 100644 --- a/src/bitstream/bitstream.cpp +++ b/src/bitstream/bitstream.cpp @@ -11,7 +11,6 @@ TBitStream::TBitStream(const char* buf, int size) {} TBitStream::TBitStream() {} - void TBitStream::Write(unsigned long long val, int n) { if (n > 23 || n < 0) abort(); @@ -29,10 +28,30 @@ void TBitStream::Write(unsigned long long val, int n) { for (int i = 0; i < n/8 + (overlap ? 2 : 1); ++i) { Buf[bytesPos+i] |= t.bytes[7-i]; + + // std::cout << "bufPos: "<< bytesPos+i << " buf: " << (int)Buf[bytesPos+i] << std::endl; } BitsUsed += n; } +/* +void TBitStream::Write(unsigned long long val, int n) { + if (n > 23 || n < 0) + abort(); + const int bitsLeft = Buf.size() * 8 - BitsUsed; + const int bitsReq = n - bitsLeft; + const int bytesPos = BitsUsed / 8; + const int overlap = BitsUsed % 8; + + if (overlap || bitsReq >= 0) { + Buf.resize(Buf.size() + (bitsReq / 8 + (overlap ? 2 : 1 )), 0); + } + TMix t; + t.ull = (val << (64 - n)) >> overlap; + *(unsigned long long*)&Buf[bytesPos-8] |= t.ull; + BitsUsed += n; +} +*/ unsigned long long TBitStream::Read(int n) { if (n >23 || n < 0) abort(); diff --git a/src/bitstream/bitstream_ut.cpp b/src/bitstream/bitstream_ut.cpp index 109570b..7e246ca 100644 --- a/src/bitstream/bitstream_ut.cpp +++ b/src/bitstream/bitstream_ut.cpp @@ -11,8 +11,10 @@ TEST(TBitStream, DefaultConstructor) { TEST(TBitStream, SimpleWriteRead) { TBitStream bs; bs.Write(5, 3); - EXPECT_EQ(3, bs.GetSizeInBits()); + bs.Write(true, 1); + EXPECT_EQ(4, bs.GetSizeInBits()); EXPECT_EQ(5, bs.Read(3)); + EXPECT_EQ(true, bs.Read(1)); } TEST(TBisStream, OverlapWriteRead) { @@ -30,6 +32,7 @@ TEST(TBisStream, OverlapWriteRead) { EXPECT_EQ(212, bs.Read(22)); EXPECT_EQ(323, bs.Read(22)); } + TEST(TBisStream, OverlapWriteRead2) { TBitStream bs; bs.Write(2, 2); @@ -41,6 +44,64 @@ TEST(TBisStream, OverlapWriteRead2) { EXPECT_EQ(10003, bs.Read(16)); } +TEST(TBisStream, OverlapWriteRead3) { + TBitStream bs; + bs.Write(40, 6); + bs.Write(3, 2); + bs.Write(0, 3); + bs.Write(0, 3); + bs.Write(0, 3); + bs.Write(0, 3); + + bs.Write(3, 5); + bs.Write(1, 2); + bs.Write(1, 1); + bs.Write(1, 1); + bs.Write(1, 1); + bs.Write(1, 1); + + bs.Write(0, 3); + bs.Write(4, 3); + bs.Write(35, 6); + bs.Write(25, 6); + bs.Write(3, 3); + bs.Write(32, 6); + bs.Write(29, 6); + bs.Write(3, 3); + bs.Write(36, 6); + bs.Write(49, 6); + + + + + EXPECT_EQ(40, bs.Read(6)); + EXPECT_EQ(3, bs.Read(2)); + EXPECT_EQ(0, bs.Read(3)); + EXPECT_EQ(0, bs.Read(3)); + EXPECT_EQ(0, bs.Read(3)); + EXPECT_EQ(0, bs.Read(3)); + EXPECT_EQ(3, bs.Read(5)); + + EXPECT_EQ(1, bs.Read(2)); + EXPECT_EQ(1, bs.Read(1)); + EXPECT_EQ(1, bs.Read(1)); + EXPECT_EQ(1, bs.Read(1)); + EXPECT_EQ(1, bs.Read(1)); + + EXPECT_EQ(0, bs.Read(3)); + EXPECT_EQ(4, bs.Read(3)); + EXPECT_EQ(35, bs.Read(6)); + EXPECT_EQ(25, bs.Read(6)); + EXPECT_EQ(3, bs.Read(3)); + EXPECT_EQ(32, bs.Read(6)); + EXPECT_EQ(29, bs.Read(6)); + EXPECT_EQ(3, bs.Read(3)); + EXPECT_EQ(36, bs.Read(6)); + EXPECT_EQ(49, bs.Read(6)); + +} + + TEST(TBisStream, SignWriteRead) { TBitStream bs; bs.Write(MakeSign(-2, 3), 3); diff --git a/src/compressed_io.h b/src/compressed_io.h index d8cfb11..8b6a235 100644 --- a/src/compressed_io.h +++ b/src/compressed_io.h @@ -29,3 +29,5 @@ public: virtual long long GetLengthInSamples() const = 0; virtual ~ICompressedIO() {} }; + +typedef std::unique_ptr<ICompressedIO> TCompressedIOPtr; diff --git a/src/config.h b/src/config.h index 942841a..698b865 100644 --- a/src/config.h +++ b/src/config.h @@ -2,3 +2,8 @@ #define CONFIG_DOUBLE +#ifdef CONFIG_DOUBLE +typedef double TFloat; +#else +typedef float TFloat; +#endif diff --git a/src/gain_processor.h b/src/gain_processor.h index 8369ccf..04f2041 100644 --- a/src/gain_processor.h +++ b/src/gain_processor.h @@ -1,10 +1,12 @@ #include <functional> +#include "config.h" + template<class T> class TGainProcessor : public virtual T { public: - typedef std::function<void(double* out, double* cur, double* prev)> TGainDemodulator; + typedef std::function<void(TFloat* out, TFloat* cur, TFloat* prev)> TGainDemodulator; /* * example GainModulation: * PCMinput: @@ -17,21 +19,32 @@ public: * ^^^^^ - modulated by previous step * lets consider a case we want to modulate mdct #2. * bufCur - is a buffer of first half of mdct transformation (a) - * bufNext - is a buffer of second half of mdct transformation and overlaping (i.e the input buffer started at b point) + * bufNext - is a buffer of second half of mdct transformation and overlaping + * (i.e the input buffer started at b point) * so next transformation (mdct #3) gets modulated first part */ - typedef std::function<void(double* bufCur, double* bufNext)> TGainModulator; + typedef std::function<void(TFloat* bufCur, TFloat* bufNext)> TGainModulator; + static TFloat GetGainInc(uint32_t levelIdxCur) { + const int incPos = T::ExponentOffset - levelIdxCur + T::GainInterpolationPosShift; + return T::GainInterpolation[incPos]; + } + static TFloat GetGainInc(uint32_t levelIdxCur, uint32_t levelIdxNext) { + const int incPos = levelIdxNext - levelIdxCur + T::GainInterpolationPosShift; + return T::GainInterpolation[incPos]; + } + + TGainDemodulator Demodulate(const std::vector<typename T::SubbandInfo::TGainPoint>& giNow, const std::vector<typename T::SubbandInfo::TGainPoint>& giNext) { - return [=](double* out, double* cur, double* prev) { + return [=](TFloat* out, TFloat* cur, TFloat* prev) { uint32_t pos = 0; - const double scale = giNext.size() ? T::GainLevel[giNext[0].Level] : 1; + const TFloat scale = giNext.size() ? T::GainLevel[giNext[0].Level] : 1; for (uint32_t i = 0; i < giNow.size(); ++i) { uint32_t lastPos = giNow[i].Location << T::LocScale; const uint32_t levelPos = giNow[i].Level; assert(levelPos < sizeof(T::GainLevel)/sizeof(T::GainLevel[0])); - double level = T::GainLevel[levelPos]; + TFloat level = T::GainLevel[levelPos]; const int incPos = ((i + 1) < giNow.size() ? giNow[i + 1].Level : T::ExponentOffset) - giNow[i].Level + T::GainInterpolationPosShift; - double gainInc = T::GainInterpolation[incPos]; + TFloat gainInc = T::GainInterpolation[incPos]; for (; pos < lastPos; pos++) { //std::cout << "pos: " << pos << " scale: " << scale << " level: " << level << std::endl; out[pos] = (cur[pos] * scale + prev[pos]) * level; @@ -51,22 +64,24 @@ public: TGainModulator Modulate(const std::vector<typename T::SubbandInfo::TGainPoint>& giCur) { if (giCur.empty()) return {}; - return [=](double* bufCur, double* bufNext) { + return [=](TFloat* bufCur, TFloat* bufNext) { uint32_t pos = 0; - const double scale = T::GainLevel[giCur[0].Level]; + const TFloat scale = T::GainLevel[giCur[0].Level]; for (uint32_t i = 0; i < giCur.size(); ++i) { uint32_t lastPos = giCur[i].Location << T::LocScale; const uint32_t levelPos = giCur[i].Level; assert(levelPos < sizeof(T::GainLevel)/sizeof(T::GainLevel[0])); - double level = T::GainLevel[levelPos]; + TFloat level = T::GainLevel[levelPos]; const int incPos = ((i + 1) < giCur.size() ? giCur[i + 1].Level : T::ExponentOffset) - giCur[i].Level + T::GainInterpolationPosShift; - double gainInc = T::GainInterpolation[incPos]; + TFloat gainInc = T::GainInterpolation[incPos]; for (; pos < lastPos; pos++) { + //std::cout << "mod pos: " << pos << " scale: " << scale << " bufCur: " << bufCur[pos] << " level: " << level << " bufNext: " << bufNext[pos] << std::endl; bufCur[pos] /= scale; bufNext[pos] /= level; - //std::cout << "mod pos: " << pos << " scale: " << scale << " level: " << level << std::endl; } for (; pos < lastPos + T::LocSz; pos++) { + + //std::cout << "mod pos: " << pos << " scale: " << scale << " bufCur: " << bufCur[pos] << " level: " << level << " (gainInc) " << gainInc << " bufNext: " << bufNext[pos] << std::endl; bufCur[pos] /= scale; bufNext[pos] /= level; //std::cout << "mod pos: " << pos << " scale: " << scale << " level: " << level << " gainInc: " << gainInc << std::endl; @@ -74,8 +89,9 @@ public: } } for (; pos < T::MDCTSz/2; pos++) { + + //std::cout << "mod pos: " << pos << " scale: " << scale << " bufCur: " << bufCur[pos] << std::endl; bufCur[pos] /= scale; - //std::cout << "mod pos: " << pos << " scale: " << scale << std::endl; } }; } diff --git a/src/main.cpp b/src/main.cpp index 5a9547e..b35f6bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,20 +6,23 @@ #include "pcmengin.h" #include "wav.h" #include "aea.h" -#include "atracdenc.h" +#include "config.h" +#include "atrac1denc.h" +#include "atrac3denc.h" using std::cout; using std::cerr; using std::endl; using std::string; -using std::unique_ptr; -using std::move; using std::stoi; using namespace NAtracDEnc; +typedef std::unique_ptr<TPCMEngine<TFloat>> TPcmEnginePtr; +typedef std::unique_ptr<IProcessor<TFloat>> TAtracProcessorPtr; -static void printUsage(const char* myName) { +static void printUsage(const char* myName) +{ cout << "\tusage: " << myName << " <-e|-d> <-i input> <-o output>\n" << endl; cout << "-e encode mode (PCM -> ATRAC), -i wav file, -o aea file" << endl; cout << "-d decode mode (ATRAC -> PCM), -i aea file, -o wav file" << endl; @@ -27,7 +30,8 @@ static void printUsage(const char* myName) { } -static void printProgress(int percent) { +static void printProgress(int percent) +{ static uint32_t counter; counter++; const char symbols[4] = {'-', '\\', '|', '/'}; @@ -35,18 +39,24 @@ static void printProgress(int percent) { fflush(stdout); } -static string GetHelp() { +static string GetHelp() +{ return "\n--encode -i \t - encode mode" "\n--decode -d \t - decode mode" "\n -i input file" "\n -o output file" "\n --bitrate (only if supported by codec)" - "\nAdvanced options:\n --bfuidxconst\t Set constant amount of used BFU. WARNING: It is not a lowpass filter! Do not use it to cut off hi frequency." - "\n --bfuidxfast\t enable fast search of BFU amount" - "\n --notransient[=mask] disable transient detection and use optional mask to set bands with short MDCT window"; + "\nAdvanced options:\n --bfuidxconst\t Set constant amount of used BFU (ATRAC1). " + "WARNING: It is not a lowpass filter! Do not use it to cut off hi frequency." + "\n --bfuidxfast\t enable fast search of BFU amount (ATRAC1)" + "\n --notransient[=mask] disable transient detection and use optional mask to set bands with short MDCT window " + "(ATRAC1)" + /*"\n --nogaincontrol disable gain control (ATRAC3)"*/ + "\n --notonal disable tonal components (ATRAC3)"; } -static int checkedStoi(const char* data, int min, int max, int def) { +static int checkedStoi(const char* data, int min, int max, int def) +{ int tmp = 0; try { tmp = stoi(data); @@ -59,18 +69,114 @@ static int checkedStoi(const char* data, int min, int max, int def) { } } -int main(int argc, char* const* argv) { +enum EOptions +{ + O_ENCODE = 'e', + O_DECODE = 'd', + O_HELP = 'h', + O_BITRATE = 'b', + O_BFUIDXCONST = 1, + O_BFUIDXFAST = 2, + O_NOTRANSIENT = 3, + O_MONO = 'm', + O_NOSTDOUT = '4', + O_NOTONAL = 5, + O_NOGAINCONTROL = 6, +}; + +static void PrepareAtrac1Encoder(const string& inFile, + const string& outFile, + const bool noStdOut, + NAtrac1::TAtrac1EncodeSettings&& encoderSettings, + uint64_t* totalSamples, + TWavPtr* wavIO, + TPcmEnginePtr* pcmEngine, + TAtracProcessorPtr* atracProcessor) +{ + using NAtrac1::TAtrac1Data; + + wavIO->reset(new TWav(inFile)); + const int numChannels = (*wavIO)->GetChannelNum(); + *totalSamples = (*wavIO)->GetTotalSamples(); + //TODO: recheck it + const uint32_t numFrames = numChannels * (*totalSamples) / TAtrac1Data::NumSamples; + TCompressedIOPtr aeaIO = TCompressedIOPtr(new TAea(outFile, "test", numChannels, numFrames)); + pcmEngine->reset(new TPCMEngine<TFloat>(4096, + numChannels, + TPCMEngine<TFloat>::TReaderPtr((*wavIO)->GetPCMReader<TFloat>()))); + if (!noStdOut) + cout << "Input file: " << inFile + << "\n Channels: " << numChannels + << "\n SampleRate: " << (*wavIO)->GetSampleRate() + << "\n TotalSamples: " << totalSamples + << endl; + atracProcessor->reset(new TAtrac1Processor(std::move(aeaIO), std::move(encoderSettings))); +} + +static void PrepareAtrac1Decoder(const string& inFile, + const string& outFile, + const bool noStdOut, + uint64_t* totalSamples, + TWavPtr* wavIO, + TPcmEnginePtr* pcmEngine, + TAtracProcessorPtr* atracProcessor) +{ + TCompressedIOPtr aeaIO = TCompressedIOPtr(new TAea(inFile)); + *totalSamples = aeaIO->GetLengthInSamples(); + uint32_t length = aeaIO->GetLengthInSamples(); + if (!noStdOut) + cout << "Name: " << aeaIO->GetName() + << "\n Channels: " << aeaIO->GetChannelNum() + << "\n Length: " << length + << endl; + wavIO->reset(new TWav(outFile, aeaIO->GetChannelNum(), 44100)); + pcmEngine->reset(new TPCMEngine<TFloat>(4096, + aeaIO->GetChannelNum(), + TPCMEngine<TFloat>::TWriterPtr((*wavIO)->GetPCMWriter<TFloat>()))); + atracProcessor->reset(new TAtrac1Processor(std::move(aeaIO), NAtrac1::TAtrac1EncodeSettings())); +} + +static void PrepareAtrac3Encoder(const string& inFile, + const string& outFile, + const bool noStdOut, + NAtrac3::TAtrac3EncoderSettings&& encoderSettings, + uint64_t* totalSamples, + TWavPtr* wavIO, + TPcmEnginePtr* pcmEngine, + TAtracProcessorPtr* atracProcessor) +{ + std::cout << "WARNING: ATRAC3 is uncompleted, result will be not good )))" << std::endl; + if (!noStdOut) + std::cout << "bitrate " << encoderSettings.ConteinerParams->Bitrate << std::endl; + wavIO->reset(new TWav(inFile)); + const int numChannels = (*wavIO)->GetChannelNum(); + *totalSamples = (*wavIO)->GetTotalSamples(); + TCompressedIOPtr omaIO = TCompressedIOPtr(new TOma(outFile, + "test", + numChannels, + numChannels * (*totalSamples) / 512, OMAC_ID_ATRAC3, + encoderSettings.ConteinerParams->FrameSz)); + pcmEngine->reset(new TPCMEngine<TFloat>(4096, + numChannels, + TPCMEngine<TFloat>::TReaderPtr((*wavIO)->GetPCMReader<TFloat>()))); + atracProcessor->reset(new TAtrac3Processor(std::move(omaIO), std::move(encoderSettings))); +} + +int main(int argc, char* const* argv) +{ const char* myName = argv[0]; static struct option longopts[] = { - { "encode", optional_argument, NULL, 'e' }, - { "decode", no_argument, NULL, 'd' }, - { "help", no_argument, NULL, 'h' }, - { "bitrate", required_argument, NULL, 'b'}, - { "bfuidxconst", required_argument, NULL, 1}, - { "bfuidxfast", no_argument, NULL, 2}, - { "notransient", optional_argument, NULL, 3}, - { "mono", no_argument, NULL, 'm'}, - { "nostdout", no_argument, NULL, 4}, + { "encode", optional_argument, NULL, O_ENCODE }, + { "decode", no_argument, NULL, O_DECODE }, + { "help", no_argument, NULL, O_HELP }, + { "bitrate", required_argument, NULL, O_BITRATE}, + { "bfuidxconst", required_argument, NULL, O_BFUIDXCONST}, + { "bfuidxfast", no_argument, NULL, O_BFUIDXFAST}, + { "notransient", optional_argument, NULL, O_NOTRANSIENT}, + { "mono", no_argument, NULL, O_MONO}, + { "nostdout", no_argument, NULL, O_NOSTDOUT}, + { "notonal", no_argument, NULL, O_NOTONAL}, + { "nogaincontrol", no_argument, NULL, O_NOGAINCONTROL}, { NULL, 0, NULL, 0} }; @@ -81,13 +187,15 @@ int main(int argc, char* const* argv) { uint32_t bfuIdxConst = 0; //0 - auto, no const bool fastBfuNumSearch = false; bool mono = false; - bool nostdout = false; - TAtrac1EncodeSettings::EWindowMode windowMode = TAtrac1EncodeSettings::EWindowMode::EWM_AUTO; + bool noStdOut = false; + bool noGainControl = true; + bool noTonalComponents = false; + NAtrac1::TAtrac1EncodeSettings::EWindowMode windowMode = NAtrac1::TAtrac1EncodeSettings::EWindowMode::EWM_AUTO; uint32_t winMask = 0; //0 - all is long uint32_t bitrate = 0; //0 - use default for codec while ((ch = getopt_long(argc, argv, "edhi:o:m", longopts, NULL)) != -1) { switch (ch) { - case 'e': + case O_ENCODE: mode |= E_ENCODE; if (optarg) { if (strcmp(optarg, "atrac3") == 0) { @@ -95,7 +203,7 @@ int main(int argc, char* const* argv) { } } break; - case 'd': + case O_DECODE: mode |= E_DECODE; break; case 'i': @@ -104,26 +212,27 @@ int main(int argc, char* const* argv) { case 'o': outFile = optarg; if (outFile == "-") - nostdout = true; + noStdOut = true; break; - case 'm': + case O_MONO: mono = true; break; - case 'h': + case O_HELP: cout << GetHelp() << endl; return 0; break; - case 'b': + case O_BITRATE: bitrate = checkedStoi(optarg, 32, 384, 0); + std::cout << "BITRATE" << bitrate << std::endl; break; - case 1: + case O_BFUIDXCONST: bfuIdxConst = checkedStoi(optarg, 1, 8, 0); break; - case 2: + case O_BFUIDXFAST: fastBfuNumSearch = true; break; - case 3: - windowMode = TAtrac1EncodeSettings::EWindowMode::EWM_NOTRANSIENT; + case O_NOTRANSIENT: + windowMode = NAtrac1::TAtrac1EncodeSettings::EWindowMode::EWM_NOTRANSIENT; if (optarg) { winMask = stoi(optarg); } @@ -132,8 +241,14 @@ int main(int argc, char* const* argv) { ((winMask & 2) ? "short": "long") << ", hi - " << ((winMask & 4) ? "short": "long") << endl; break; - case 4: - nostdout = true; + case O_NOSTDOUT: + noStdOut = true; + break; + case O_NOTONAL: + noTonalComponents = true; + break; + case O_NOGAINCONTROL: + noGainControl = true; break; default: printUsage(myName); @@ -152,54 +267,50 @@ int main(int argc, char* const* argv) { return 1; } if (bfuIdxConst > 8) { - cerr << "Wrong bfuidxconst value ("<< bfuIdxConst << "). This is advanced options, use --help to get more information" << endl; + cerr << "Wrong bfuidxconst value ("<< bfuIdxConst << "). " + << "This is advanced options, use --help to get more information" + << endl; return 1; } - TPCMEngine<double>* pcmEngine = nullptr; - IProcessor<double>* atracProcessor; + + TPcmEnginePtr pcmEngine; + TAtracProcessorPtr atracProcessor; uint64_t totalSamples = 0; TWavPtr wavIO; uint32_t pcmFrameSz = 0; //size of one pcm frame to process - if (mode == E_ENCODE) { - wavIO = TWavPtr(new TWav(inFile)); - const int numChannels = wavIO->GetChannelNum(); - totalSamples = wavIO->GetTotalSamples(); - //TODO: recheck it - TAeaPtr aeaIO = TAeaPtr(new TAea(outFile, "test", numChannels, numChannels * totalSamples / 512)); - pcmEngine = new TPCMEngine<double>(4096, numChannels, TPCMEngine<double>::TReaderPtr(wavIO->GetPCMReader<double>())); - if (!nostdout) - cout << "Input file: " << inFile << "\n Channels: " << numChannels << "\n SampleRate: " << wavIO->GetSampleRate() << "\n TotalSamples: " << totalSamples << endl; - atracProcessor = new TAtrac1Processor(move(aeaIO), TAtrac1EncodeSettings(bfuIdxConst, fastBfuNumSearch, windowMode, winMask)); - pcmFrameSz = 512; - } else if (mode == E_DECODE) { - TAeaPtr aeaIO = TAeaPtr(new TAea(inFile)); - totalSamples = aeaIO->GetLengthInSamples(); - uint32_t length = aeaIO->GetLengthInSamples(); - if (!nostdout) - cout << "Name: " << aeaIO->GetName() << "\n Channels: " << aeaIO->GetChannelNum() << "\n Length: " << length << endl; - wavIO = TWavPtr(new TWav(outFile, aeaIO->GetChannelNum(), 44100)); - pcmEngine = new TPCMEngine<double>(4096, aeaIO->GetChannelNum(), TPCMEngine<double>::TWriterPtr(wavIO->GetPCMWriter<double>())); - atracProcessor = new TAtrac1Processor(move(aeaIO), TAtrac1EncodeSettings(bfuIdxConst, fastBfuNumSearch, windowMode, winMask)); - pcmFrameSz = 512; - } else if (mode == (E_ENCODE | E_ATRAC3)) { - std::cout << "WARNING: ATRAC3 is uncompleted mode (no psy, tonal encoding, gc), result will be not good )))" << std::endl; - const TContainerParams* atrac3params = TAtrac3Data::GetContainerParamsForBitrate(bitrate*1024); - if (atrac3params == nullptr) { - std::cerr << "wrong atrac3 params, exiting" << std::endl; + switch (mode) { + case E_ENCODE: + { + using NAtrac1::TAtrac1Data; + NAtrac1::TAtrac1EncodeSettings encoderSettings(bfuIdxConst, fastBfuNumSearch, windowMode, winMask); + PrepareAtrac1Encoder(inFile, outFile, noStdOut, std::move(encoderSettings), + &totalSamples, &wavIO, &pcmEngine, &atracProcessor); + pcmFrameSz = TAtrac1Data::NumSamples; + } + break; + case E_DECODE: + { + using NAtrac1::TAtrac1Data; + PrepareAtrac1Decoder(inFile, outFile, noStdOut, + &totalSamples, &wavIO, &pcmEngine, &atracProcessor); + pcmFrameSz = TAtrac1Data::NumSamples; + } + break; + case (E_ENCODE | E_ATRAC3): + { + using NAtrac3::TAtrac3Data; + NAtrac3::TAtrac3EncoderSettings encoderSettings(bitrate * 1024, noGainControl, noTonalComponents); + PrepareAtrac3Encoder(inFile, outFile, noStdOut, std::move(encoderSettings), + &totalSamples, &wavIO, &pcmEngine, &atracProcessor); + pcmFrameSz = TAtrac3Data::NumSamples;; + } + break; + default: + { + cerr << "Processing mode was not specified" << endl; return 1; } - std::cout << "bitrate " << atrac3params->Bitrate << std::endl; - wavIO = TWavPtr(new TWav(inFile)); - const int numChannels = wavIO->GetChannelNum(); - totalSamples = wavIO->GetTotalSamples(); - TAeaPtr omaIO = TAeaPtr(new TOma(outFile, "test", numChannels, numChannels * totalSamples / 512, OMAC_ID_ATRAC3, atrac3params->FrameSz)); - pcmEngine = new TPCMEngine<double>(4096, numChannels, TPCMEngine<double>::TReaderPtr(wavIO->GetPCMReader<double>())); - atracProcessor = new TAtrac3Processor(move(omaIO), *atrac3params); - pcmFrameSz = 1024; - } else { - cerr << "Processing mode was not specified" << endl; - return 1; } auto atracLambda = (mode == E_DECODE) ? atracProcessor->GetDecodeLambda() : @@ -209,10 +320,10 @@ int main(int argc, char* const* argv) { try { while (totalSamples > (processed = pcmEngine->ApplyProcess(pcmFrameSz, atracLambda))) { - if (!nostdout) + if (!noStdOut) printProgress(processed*100/totalSamples); } - if (!nostdout) + if (!noStdOut) cout << "\nDone" << endl; } catch (TAeaIOError err) { diff --git a/src/mdct/mdct.h b/src/mdct/mdct.h index ced049c..33863fb 100644 --- a/src/mdct/mdct.h +++ b/src/mdct/mdct.h @@ -8,7 +8,7 @@ namespace NMDCT { class TMDCTBase { protected: MDCTContext Ctx; - TMDCTBase(int n, double scale) { + TMDCTBase(int n, TFloat scale) { mdct_ctx_init(&Ctx, n, scale); }; virtual ~TMDCTBase() { @@ -19,13 +19,13 @@ protected: template<int N> class TMDCT : public TMDCTBase { - std::vector<double> Buf; + std::vector<TFloat> Buf; public: TMDCT(float scale = 1.0) : TMDCTBase(N, scale) , Buf(N/2) {} - const std::vector<double>& operator()(double* in) { + const std::vector<TFloat>& operator()(TFloat* in) { mdct(&Ctx, &Buf[0], in); return Buf; } @@ -33,13 +33,13 @@ public: template<int N> class TMIDCT : public TMDCTBase { - std::vector<double> Buf; + std::vector<TFloat> Buf; public: TMIDCT(float scale = 1.0) : TMDCTBase(N, scale) , Buf(N) {} - const std::vector<double>& operator()(double* in) { + const std::vector<TFloat>& operator()(TFloat* in) { midct(&Ctx, &Buf[0], in); return Buf; } diff --git a/src/mdct/mdct_ut.cpp b/src/mdct/mdct_ut.cpp index e81bea1..3552afd 100644 --- a/src/mdct/mdct_ut.cpp +++ b/src/mdct/mdct_ut.cpp @@ -7,24 +7,24 @@ using std::vector; using namespace NMDCT; -static vector<double> mdct(double* x, int N) { - vector<double> res; +static vector<TFloat> mdct(TFloat* x, int N) { + vector<TFloat> res; for (int k = 0; k < N; k++) { - double sum = 0; + TFloat sum = 0; for (int n = 0; n < 2 * N; n++) - sum += x[n]* cos((M_PI/N) * ((double)n + 0.5 + N/2) * ((double)k + 0.5)); + sum += x[n]* cos((M_PI/N) * ((TFloat)n + 0.5 + N/2) * ((TFloat)k + 0.5)); res.push_back(sum); } return res; } -static vector<double> midct(double* x, int N) { - vector<double> res; +static vector<TFloat> midct(TFloat* x, int N) { + vector<TFloat> res; for (int n = 0; n < 2 * N; n++) { - double sum = 0; + TFloat sum = 0; for (int k = 0; k < N; k++) - sum += (x[k] * cos((M_PI/N) * ((double)n + 0.5 + N/2) * ((double)k + 0.5))); + sum += (x[k] * cos((M_PI/N) * ((TFloat)n + 0.5 + N/2) * ((TFloat)k + 0.5))); res.push_back(sum); } @@ -35,12 +35,12 @@ static vector<double> midct(double* x, int N) { TEST(TBitStream, MDCT64) { const int N = 64; TMDCT<N> transform(N); - vector<double> src(N); + vector<TFloat> src(N); for (int i = 0; i < N; i++) { src[i] = i; } - const vector<double> res1 = mdct(&src[0], N/2); - const vector<double> res2 = transform(&src[0]); + const vector<TFloat> res1 = mdct(&src[0], N/2); + const vector<TFloat> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); for (int i = 0; i < res1.size(); i++) { EXPECT_NEAR(res1[i], res2[i], 0.0000000001); @@ -50,12 +50,12 @@ TEST(TBitStream, MDCT64) { TEST(TBitStream, MDCT128) { const int N = 128; TMDCT<N> transform(N); - vector<double> src(N); + vector<TFloat> src(N); for (int i = 0; i < N; i++) { src[i] = i; } - const vector<double> res1 = mdct(&src[0], N/2); - const vector<double> res2 = transform(&src[0]); + const vector<TFloat> res1 = mdct(&src[0], N/2); + const vector<TFloat> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); for (int i = 0; i < res1.size(); i++) { EXPECT_NEAR(res1[i], res2[i], 0.0000000001); @@ -65,12 +65,12 @@ TEST(TBitStream, MDCT128) { TEST(TBitStream, MDCT256) { const int N = 256; TMDCT<N> transform(N); - vector<double> src(N); + vector<TFloat> src(N); for (int i = 0; i < N; i++) { src[i] = i; } - const vector<double> res1 = mdct(&src[0], N/2); - const vector<double> res2 = transform(&src[0]); + const vector<TFloat> res1 = mdct(&src[0], N/2); + const vector<TFloat> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); for (int i = 0; i < res1.size(); i++) { EXPECT_NEAR(res1[i], res2[i], 0.00000001); @@ -80,12 +80,12 @@ TEST(TBitStream, MDCT256) { TEST(TBitStream, MDCT256_RAND) { const int N = 256; TMDCT<N> transform(N); - vector<double> src(N); + vector<TFloat> src(N); for (int i = 0; i < N; i++) { src[i] = rand(); } - const vector<double> res1 = mdct(&src[0], N/2); - const vector<double> res2 = transform(&src[0]); + const vector<TFloat> res1 = mdct(&src[0], N/2); + const vector<TFloat> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); for (int i = 0; i < res1.size(); i++) { EXPECT_NEAR(res1[i], res2[i], 0.01); @@ -96,12 +96,12 @@ TEST(TBitStream, MDCT256_RAND) { TEST(TBitStream, MIDCT64) { const int N = 64; TMIDCT<N> transform(1); - vector<double> src(N); + vector<TFloat> src(N); for (int i = 0; i < N/2; i++) { src[i] = i; } - const vector<double> res1 = midct(&src[0], N/2); - const vector<double> res2 = transform(&src[0]); + const vector<TFloat> res1 = midct(&src[0], N/2); + const vector<TFloat> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); for (int i = 0; i < N; i++) { EXPECT_NEAR(res1[i], res2[i], 0.0000000001); @@ -111,12 +111,12 @@ TEST(TBitStream, MIDCT64) { TEST(TBitStream, MIDCT128) { const int N = 128; TMIDCT<N> transform(1); - vector<double> src(N); + vector<TFloat> src(N); for (int i = 0; i < N/2; i++) { src[i] = i; } - const vector<double> res1 = midct(&src[0], N/2); - const vector<double> res2 = transform(&src[0]); + const vector<TFloat> res1 = midct(&src[0], N/2); + const vector<TFloat> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); for (int i = 0; i < N; i++) { EXPECT_NEAR(res1[i], res2[i], 0.0000000001); @@ -126,12 +126,12 @@ TEST(TBitStream, MIDCT128) { TEST(TBitStream, MIDCT256) { const int N = 256; TMIDCT<N> transform(1); - vector<double> src(N); + vector<TFloat> src(N); for (int i = 0; i < N/2; i++) { src[i] = i; } - const vector<double> res1 = midct(&src[0], N/2); - const vector<double> res2 = transform(&src[0]); + const vector<TFloat> res1 = midct(&src[0], N/2); + const vector<TFloat> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); for (int i = 0; i < N; i++) { EXPECT_NEAR(res1[i], res2[i], 0.000000001); @@ -141,12 +141,12 @@ TEST(TBitStream, MIDCT256) { TEST(TBitStream, MIDCT256_RAND) { const int N = 256; TMIDCT<N> transform(1); - vector<double> src(N); + vector<TFloat> src(N); for (int i = 0; i < N/2; i++) { src[i] = rand(); } - const vector<double> res1 = midct(&src[0], N/2); - const vector<double> res2 = transform(&src[0]); + const vector<TFloat> res1 = midct(&src[0], N/2); + const vector<TFloat> res2 = transform(&src[0]); EXPECT_EQ(res1.size(), res2.size()); for (int i = 0; i < N; i++) { EXPECT_NEAR(res1[i], res2[i], 0.01); diff --git a/src/qmf/qmf.h b/src/qmf/qmf.h index 11e23ee..fc765a5 100644 --- a/src/qmf/qmf.h +++ b/src/qmf/qmf.h @@ -1,13 +1,15 @@ #pragma once #include <string.h> +#include "../config.h" + template<class TPCM, int nIn> class TQmf { static const float TapHalf[24]; - double QmfWindow[48]; + TFloat QmfWindow[48]; TPCM PcmBuffer[nIn + 46]; - double PcmBufferMerge[nIn + 46]; - double DelayBuff[46]; + TFloat PcmBufferMerge[nIn + 46]; + TFloat DelayBuff[46]; public: TQmf() { const int sz = sizeof(QmfWindow)/sizeof(QmfWindow[0]); @@ -21,8 +23,8 @@ public: } } - void Split(TPCM* in, double* lower, double* upper) { - double temp; + void Split(TPCM* in, TFloat* lower, TFloat* upper) { + TFloat temp; for (int i = 0; i < 46; i++) PcmBuffer[i] = PcmBuffer[nIn + i]; @@ -41,9 +43,9 @@ public: } } - void Merge(TPCM* out, double* lower, double* upper) { - memcpy(&PcmBufferMerge[0], &DelayBuff[0], 46*sizeof(double)); - double* newPart = &PcmBufferMerge[46]; + void Merge(TPCM* out, TFloat* lower, TFloat* upper) { + memcpy(&PcmBufferMerge[0], &DelayBuff[0], 46*sizeof(TFloat)); + TFloat* newPart = &PcmBufferMerge[46]; for (int i = 0; i < nIn; i+=4) { newPart[i+0] = lower[i/2] + upper[i/2]; newPart[i+1] = lower[i/2] - upper[i/2]; @@ -51,10 +53,10 @@ public: newPart[i+3] = lower[i/2 + 1] - upper[i/2 + 1]; } - double* winP = &PcmBufferMerge[0]; + TFloat* winP = &PcmBufferMerge[0]; for (int j = nIn/2; j != 0; j--) { - double s1 = 0; - double s2 = 0; + TFloat s1 = 0; + TFloat s2 = 0; for (int i = 0; i < 48; i+=2) { s1 += winP[i] * QmfWindow[i]; s2 += winP[i+1] * QmfWindow[i+1]; @@ -64,7 +66,7 @@ public: winP += 2; out += 2; } - memcpy(&DelayBuff[0], &PcmBufferMerge[nIn], 46*sizeof(double)); + memcpy(&DelayBuff[0], &PcmBufferMerge[nIn], 46*sizeof(TFloat)); } }; diff --git a/src/transient_detector.cpp b/src/transient_detector.cpp index 43897d6..b0e4aab 100644 --- a/src/transient_detector.cpp +++ b/src/transient_detector.cpp @@ -1,10 +1,13 @@ #include "transient_detector.h" #include <stdlib.h> - +#include <cmath> +#include <cassert> +#include <iostream> namespace NAtracDEnc { -static double calculateRMS(const double* in, uint32_t n) { - double s = 0; +using std::vector; +static TFloat calculateRMS(const TFloat* in, uint32_t n) { + TFloat s = 0; for (uint32_t i = 0; i < n; i++) { s += in[i] * in[i]; } @@ -12,8 +15,18 @@ static double calculateRMS(const double* in, uint32_t n) { return sqrt(s); } -void TTransientDetector::HPFilter(const double* in, double* out) { - static const double fircoef[] = { +static TFloat calculatePeak(const TFloat* in, uint32_t n) { + TFloat s = 0; + for (uint32_t i = 0; i < n; i++) { + TFloat absVal = std::abs(in[i]); + if (absVal > s) + s = absVal; + } + return s; +} + +void TTransientDetector::HPFilter(const TFloat* in, TFloat* out) { + static const TFloat fircoef[] = { -8.65163e-18 * 2.0, -0.00851586 * 2.0, -6.74764e-18 * 2.0, 0.0209036 * 2.0, -3.36639e-17 * 2.0, -0.0438162 * 2.0, -1.54175e-17 * 2.0, 0.0931738 * 2.0, -5.52212e-17 * 2.0, -0.313819 * 2.0 @@ -33,10 +46,10 @@ void TTransientDetector::HPFilter(const double* in, double* out) { } -bool TTransientDetector::Detect(const double* buf) { +bool TTransientDetector::Detect(const TFloat* buf) { const uint32_t nBlocksToAnalize = NShortBlocks + 1; - double* rmsPerShortBlock = reinterpret_cast<double*>(alloca(sizeof(double) * nBlocksToAnalize)); - std::vector<double> filtered(BlockSz); + TFloat* rmsPerShortBlock = reinterpret_cast<TFloat*>(alloca(sizeof(TFloat) * nBlocksToAnalize)); + std::vector<TFloat> filtered(BlockSz); HPFilter(buf, filtered.data()); bool trans = false; rmsPerShortBlock[0] = LastEnergy; @@ -44,13 +57,25 @@ bool TTransientDetector::Detect(const double* buf) { rmsPerShortBlock[i] = 19.0 * log10(calculateRMS(&filtered[(i - 1) * ShortSz], ShortSz)); if (rmsPerShortBlock[i] - rmsPerShortBlock[i - 1] > 16) { trans = true; + LastTransientPos = i; } if (rmsPerShortBlock[i - 1] - rmsPerShortBlock[i] > 20) { trans = true; + LastTransientPos = i; } } LastEnergy = rmsPerShortBlock[NShortBlocks]; return trans; } +std::vector<TFloat> AnalyzeGain(const TFloat* in, const uint32_t len, const uint32_t maxPoints) { + vector<TFloat> res; + const uint32_t step = len / maxPoints; + for (uint32_t pos = 0; pos < len; pos += step) { + TFloat rms = calculatePeak(in + pos, step); + res.emplace_back(rms); + } + return res; +} + } diff --git a/src/transient_detector.h b/src/transient_detector.h index b3db6ba..004eff6 100644 --- a/src/transient_detector.h +++ b/src/transient_detector.h @@ -3,6 +3,8 @@ #include <cstdint> #include <vector> +#include "config.h" + namespace NAtracDEnc { class TTransientDetector { const uint32_t ShortSz; @@ -10,9 +12,10 @@ class TTransientDetector { const uint32_t NShortBlocks; static const uint32_t PrevBufSz = 20; static const uint32_t FIRLen = 21; - void HPFilter(const double* in, double* out); - std::vector<double> HPFBuffer; - double LastEnergy = 0.0; + void HPFilter(const TFloat* in, TFloat* out); + std::vector<TFloat> HPFBuffer; + TFloat LastEnergy = 0.0; + uint32_t LastTransientPos = 0; public: TTransientDetector(uint32_t shortSz, uint32_t blockSz) : ShortSz(shortSz) @@ -21,6 +24,9 @@ public: { HPFBuffer.resize(BlockSz + FIRLen); } - bool Detect(const double* buf); + bool Detect(const TFloat* buf); + uint32_t GetLastTransientPos() const { return LastTransientPos; } }; + +std::vector<TFloat> AnalyzeGain(const TFloat* in, const uint32_t len, const uint32_t maxPoints); } diff --git a/src/transient_detector_ut.cpp b/src/transient_detector_ut.cpp new file mode 100644 index 0000000..3627d7d --- /dev/null +++ b/src/transient_detector_ut.cpp @@ -0,0 +1,36 @@ +#include "transient_detector.h" +#include <gtest/gtest.h> + +#include <vector> +#include <cmath> + +using std::vector; +using namespace NAtracDEnc; +TEST(AnalyzeGain, AnalyzeGainSimple) { + + TFloat in[256]; + for (int i = 0; i < 256; ++i) { + if (i <= 24) { + in[i] = 1.0; + } else if ( i > 24 && i <= 32) { + in[i] = 8.0; + } else if ( i > 32 && i <= 66) { + in[i] = 128.0; + } else { + in[i] = 0.5; + } + } + vector<TFloat> res = AnalyzeGain(in, 256, 32); + EXPECT_EQ(res.size(), 32); + +// for (TFloat v : res) +// std::cout << v << std::endl; + for (int i = 0; i < 3; ++i) + EXPECT_EQ(res[i], 1.0); + for (int i = 3; i < 4; ++i) + EXPECT_EQ(res[i], 8.0); + for (int i = 4; i < 9; ++i) + EXPECT_EQ(res[i], 128.0); + for (int i = 9; i < 32; ++i) + EXPECT_EQ(res[i], 0.5); +} @@ -1,6 +1,9 @@ #pragma once #include <cstdint> +#include <vector> +#include <algorithm> +#include "config.h" template<class T> inline void SwapArray(T* p, const size_t len) { @@ -10,3 +13,30 @@ inline void SwapArray(T* p, const size_t len) { p[j] = tmp; } } + +inline uint16_t GetFirstSetBit(uint32_t x) { + static const uint16_t multiplyDeBruijnBitPosition[32] = { + 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, + 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 + }; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return multiplyDeBruijnBitPosition[(uint32_t)(x * 0x07C4ACDDU) >> 27]; +} + +template<class T> +inline uint16_t Log2FloatToIdx(T x, uint16_t shift) { + T t = x * shift; + return GetFirstSetBit(t); +} + +template<class T> +inline T CalcMedian(T* in, uint32_t len) { + std::vector<T> tmp(in, in+len); + std::sort(tmp.begin(), tmp.end()); + uint32_t pos = (len - 1) / 2; + return tmp[pos]; +} diff --git a/src/util_ut.cpp b/src/util_ut.cpp index 8c5ea29..ccd9fce 100644 --- a/src/util_ut.cpp +++ b/src/util_ut.cpp @@ -6,10 +6,21 @@ TEST(Util, SwapArrayTest) { - double arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + TFloat arr[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; SwapArray(arr, 8); for (size_t i = 0; i < 8; ++i) { - EXPECT_NEAR((double)i, arr[7-i], 0.000000000001); + EXPECT_NEAR((TFloat)i, arr[7-i], 0.000000000001); } +} +TEST(Util, GetFirstSetBitTest) { + EXPECT_EQ(1, GetFirstSetBit(2)); + EXPECT_EQ(1, GetFirstSetBit(3)); + EXPECT_EQ(2, GetFirstSetBit(4)); + EXPECT_EQ(2, GetFirstSetBit(5)); + EXPECT_EQ(2, GetFirstSetBit(6)); + EXPECT_EQ(2, GetFirstSetBit(7)); + EXPECT_EQ(3, GetFirstSetBit(8)); + EXPECT_EQ(3, GetFirstSetBit(9)); + EXPECT_EQ(3, GetFirstSetBit(10)); } |