Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify scaler code reusage. #41

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/atrac/atrac1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "atrac1.h"
#include "bitstream/bitstream.h"

namespace NAtracDEnc {
namespace NAtrac1 {
Expand All @@ -31,5 +32,14 @@ float TAtrac1Data::SineWindow[32] = {0};

const static TAtrac1Data Atrac1Data;

std::array<int, 3> TAtrac1Data::TBlockSizeMod::Parse(NBitStream::TBitStream* stream) {
std::array<int, 3> 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;
}

} //namespace NAtrac1
} //namespace NAtracDEnc
33 changes: 33 additions & 0 deletions src/atrac/atrac1.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include <map>
#include <math.h>
#include "config.h"

namespace NBitStream {
class TBitStream;
}

namespace NAtracDEnc {
namespace NAtrac1 {

Expand Down Expand Up @@ -53,6 +58,34 @@ class TAtrac1EncodeSettings {

class TAtrac1Data {
public:
class TBlockSizeMod {
static std::array<int, 3> Parse(NBitStream::TBitStream* stream);
static std::array<int, 3> Create(bool lowShort, bool midShort, bool hiShort) {
std::array<int, 3> tmp;
tmp[0] = lowShort ? 2 : 0;
tmp[1] = midShort ? 2 : 0;
tmp[2] = hiShort ? 3 : 0;
return tmp;
}
public:
bool ShortWin(uint8_t band) const noexcept {
return LogCount[band];
}

TBlockSizeMod(NBitStream::TBitStream* stream)
: LogCount(Parse(stream))
{}

TBlockSizeMod(bool lowShort, bool midShort, bool hiShort)
: LogCount(Create(lowShort, midShort, hiShort))
{}

TBlockSizeMod()
: LogCount({{0, 0, 0}})
{}

std::array<int, 3> LogCount;
};
static constexpr uint8_t MaxBfus = 52;
static constexpr uint8_t NumQMF = 3;

Expand Down
6 changes: 3 additions & 3 deletions src/atrac/atrac1_bitalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ vector<uint32_t> TAtrac1SimpleBitAlloc::CalcBitsAllocation(const std::vector<TSc
const uint32_t bfuNum,
const float spread,
const float shift,
const TBlockSize& blockSize,
const TAtrac1Data::TBlockSizeMod& blockSize,
const float loudness) {
vector<uint32_t> bitsPerEachBlock(bfuNum);
for (size_t i = 0; i < bitsPerEachBlock.size(); ++i) {
Expand Down Expand Up @@ -186,7 +186,7 @@ uint32_t TAtrac1SimpleBitAlloc::CheckBfuUsage(bool* changed,
return curBfuId;
}

uint32_t TAtrac1SimpleBitAlloc::Write(const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize, float loudness) {
uint32_t TAtrac1SimpleBitAlloc::Write(const std::vector<TScaledBlock>& scaledBlocks, const TAtrac1Data::TBlockSizeMod& blockSize, float loudness) {
uint32_t bfuIdx = BfuIdxConst ? BfuIdxConst - 1 : 7;
bool autoBfu = !BfuIdxConst;
float spread = AnalizeScaleFactorSpread(scaledBlocks);
Expand Down Expand Up @@ -261,7 +261,7 @@ TAtrac1BitStreamWriter::TAtrac1BitStreamWriter(ICompressedOutput* container)
void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachBlock,
const std::vector<TScaledBlock>& scaledBlocks,
uint32_t bfuAmountIdx,
const TBlockSize& blockSize) {
const TAtrac1Data::TBlockSizeMod& blockSize) {
NBitStream::TBitStream bitStream;
size_t bitUsed = 0;
if (bfuAmountIdx >= (1 << TAtrac1Data::BitsPerBfuAmountTabIdx)) {
Expand Down
10 changes: 6 additions & 4 deletions src/atrac/atrac1_bitalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once
#include "atrac_scale.h"
#include "atrac/atrac1.h"
#include "../aea.h"
#include <vector>
#include <map>
Expand All @@ -32,7 +33,7 @@ class IAtrac1BitAlloc {
public:
IAtrac1BitAlloc() {};
virtual ~IAtrac1BitAlloc() {};
virtual uint32_t Write(const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize, float loudness) = 0;
virtual uint32_t Write(const std::vector<TScaledBlock>& scaledBlocks, const TAtrac1Data::TBlockSizeMod& blockSize, float loudness) = 0;
};

class TBitsBooster {
Expand All @@ -50,12 +51,13 @@ class TAtrac1BitStreamWriter {
explicit TAtrac1BitStreamWriter(ICompressedOutput* container);

void WriteBitStream(const std::vector<uint32_t>& bitsPerEachBlock, const std::vector<TScaledBlock>& scaledBlocks,
uint32_t bfuAmountIdx, const TBlockSize& blockSize);
uint32_t bfuAmountIdx, const TAtrac1Data::TBlockSizeMod& 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 float spread, const float shift, const TBlockSize& blockSize,
const float spread, const float shift,
const TAtrac1Data::TBlockSizeMod& blockSize,
const float loudness);
const uint32_t BfuIdxConst;
const bool FastBfuNumSearch;
Expand All @@ -66,7 +68,7 @@ class TAtrac1SimpleBitAlloc : public TAtrac1BitStreamWriter, public TBitsBooster
public:
TAtrac1SimpleBitAlloc(ICompressedOutput* container, uint32_t bfuIdxConst, bool fastBfuNumSearch);
~TAtrac1SimpleBitAlloc() {};
uint32_t Write(const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize, float loudness) override;
uint32_t Write(const std::vector<TScaledBlock>& scaledBlocks, const TAtrac1Data::TBlockSizeMod& blockSize, float loudness) override;
};

} //namespace NAtrac1
Expand Down
2 changes: 1 addition & 1 deletion src/atrac/atrac1_dequantiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace NBitStream;
TAtrac1Dequantiser::TAtrac1Dequantiser() {
}

void TAtrac1Dequantiser::Dequant(TBitStream* stream, const TBlockSize& bs, float specs[512]) {
void TAtrac1Dequantiser::Dequant(TBitStream* stream, const TAtrac1Data::TBlockSizeMod& bs, float specs[512]) {
uint32_t wordLens[TAtrac1Data::MaxBfus];
uint32_t idScaleFactors[TAtrac1Data::MaxBfus];
const uint32_t numBFUs = TAtrac1Data::BfuAmountTab[stream->Read(3)];
Expand Down
2 changes: 1 addition & 1 deletion src/atrac/atrac1_dequantiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace NAtrac1 {
class TAtrac1Dequantiser {
public:
TAtrac1Dequantiser();
void Dequant(NBitStream::TBitStream* stream, const TBlockSize& bs, float specs[512]);
void Dequant(NBitStream::TBitStream* stream, const TAtrac1Data::TBlockSizeMod& bs, float specs[512]);
};

} //namespace NAtrac1
Expand Down
7 changes: 7 additions & 0 deletions src/atrac/atrac3.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ inline bool operator> (const TContainerParams& x, const unsigned int y)

class TAtrac3Data {
public:
class TBlockSizeMod {
public:
constexpr bool ShortWin(uint8_t) const noexcept {
return false;
}
};

static constexpr uint8_t MaxBfus = 32;
static constexpr uint32_t NumSamples = 1024;

Expand Down
5 changes: 3 additions & 2 deletions src/atrac/atrac_scale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ TScaledBlock TScaler<TBaseData>::Scale(const float* in, uint16_t len) {
}

template<class TBaseData>
vector<TScaledBlock> TScaler<TBaseData>::ScaleFrame(const vector<float>& specs, const TBlockSize& blockSize) {
vector<TScaledBlock> TScaler<TBaseData>::ScaleFrame(const vector<float>& specs, const typename TBaseData::TBlockSizeMod& blockSize) {
vector<TScaledBlock> scaledBlocks;
scaledBlocks.reserve(TBaseData::MaxBfus);
for (uint8_t bandNum = 0; bandNum < TBaseData::NumQMF; ++bandNum) {
const bool shortWinMode = !!blockSize.LogCount[bandNum];
const bool shortWinMode = blockSize.ShortWin(bandNum);

for (uint8_t blockNum = TBaseData::BlocksPerBand[bandNum]; blockNum < TBaseData::BlocksPerBand[bandNum + 1]; ++blockNum) {
const uint16_t specNumStart = shortWinMode ? TBaseData::SpecsStartShort[blockNum] :
TBaseData::SpecsStartLong[blockNum];
Expand Down
35 changes: 1 addition & 34 deletions src/atrac/atrac_scale.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,13 @@ struct TScaledBlock {
float MaxEnergy;
};

class TBlockSize;

template <class TBaseData>
class TScaler {
std::map<float, uint8_t> ScaleIndex;
public:
TScaler();
TScaledBlock Scale(const float* in, uint16_t len);
std::vector<TScaledBlock> ScaleFrame(const std::vector<float>& 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}})
{}
std::array<int, 4> LogCount;
std::vector<TScaledBlock> ScaleFrame(const std::vector<float>& specs, const typename TBaseData::TBlockSizeMod& blockSize);
};

} //namespace NAtracDEnc
10 changes: 5 additions & 5 deletions src/atrac1denc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static void vector_fmul_window(float *dst, const float *src0,
}
}

void TAtrac1MDCT::Mdct(float Specs[512], float* low, float* mid, float* hi, const TBlockSize& blockSize) {
void TAtrac1MDCT::Mdct(float Specs[512], float* low, float* mid, float* hi, const TAtrac1Data::TBlockSizeMod& blockSize) {
uint32_t pos = 0;
for (uint32_t band = 0; band < TAtrac1Data::NumQMF; band++) {
const uint32_t numMdctBlocks = 1 << blockSize.LogCount[band];
Expand Down Expand Up @@ -97,7 +97,7 @@ void TAtrac1MDCT::Mdct(float Specs[512], float* low, float* mid, float* hi, cons
pos += bufSz;
}
}
void TAtrac1MDCT::IMdct(float Specs[512], const TBlockSize& mode, float* low, float* mid, float* hi) {
void TAtrac1MDCT::IMdct(float Specs[512], const TAtrac1Data::TBlockSizeMod& mode, float* low, float* mid, float* hi) {
uint32_t pos = 0;
for (size_t band = 0; band < TAtrac1Data::NumQMF; band++) {
const uint32_t numMdctBlocks = 1 << mode.LogCount[band];
Expand Down Expand Up @@ -142,7 +142,7 @@ TPCMEngine::TProcessLambda TAtrac1Decoder::GetLambda() {

TBitStream bitstream(frame->Get(), frame->Size());

TBlockSize mode(&bitstream);
TAtrac1Data::TBlockSizeMod mode(&bitstream);
TAtrac1Dequantiser dequantiser;
vector<float> specs;
specs.resize(512);;
Expand Down Expand Up @@ -186,7 +186,7 @@ TPCMEngine::TProcessLambda TAtrac1Encoder::GetLambda() {
auto buf = std::make_shared<TData>(srcChannels);

return [this, srcChannels, bitAlloc, buf](float* data, const TPCMEngine::ProcessMeta& /*meta*/) {
TBlockSize blockSz[2];
TAtrac1Data::TBlockSizeMod blockSz[2];

uint32_t windowMasks[2] = {0};
for (uint32_t channel = 0; channel < srcChannels; channel++) {
Expand All @@ -213,7 +213,7 @@ TPCMEngine::TProcessLambda TAtrac1Encoder::GetLambda() {
windowMask = Settings.GetWindowMask();
}

blockSz[channel] = TBlockSize(windowMask & 0x1, windowMask & 0x2, windowMask & 0x4); //low, mid, hi
blockSz[channel] = TAtrac1Data::TBlockSizeMod(windowMask & 0x1, windowMask & 0x2, windowMask & 0x4); //low, mid, hi

auto& specs = (*buf)[channel].Specs;

Expand Down
4 changes: 2 additions & 2 deletions src/atrac1denc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class TAtrac1MDCT {
NMDCT::TMIDCT<256> Midct256;
NMDCT::TMIDCT<64> Midct64;
public:
void IMdct(float specs[512], const TBlockSize& mode, float* low, float* mid, float* hi);
void Mdct(float specs[512], float* low, float* mid, float* hi, const TBlockSize& blockSize);
void IMdct(float specs[512], const NAtrac1::TAtrac1Data::TBlockSizeMod& mode, float* low, float* mid, float* hi);
void Mdct(float specs[512], float* low, float* mid, float* hi, const NAtrac1::TAtrac1Data::TBlockSizeMod& blockSize);
TAtrac1MDCT()
: Mdct512(1)
, Mdct256(0.5)
Expand Down
2 changes: 1 addition & 1 deletion src/atrac3denc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ TPCMEngine::TProcessLambda TAtrac3Encoder::GetLambda()
sce->Loudness = l;

//TBlockSize for ATRAC3 - 4 subband, all are long (no short window)
sce->ScaledBlocks = Scaler.ScaleFrame(specs, TBlockSize());
sce->ScaledBlocks = Scaler.ScaleFrame(specs, TAtrac3Data::TBlockSizeMod());
}

if (meta.Channels == 2 && !Params.ConteinerParams->Js) {
Expand Down
5 changes: 3 additions & 2 deletions src/atracdenc_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <vector>
using std::vector;
using namespace NAtracDEnc;
using namespace NAtrac1;

void CheckResult128(const vector<float>& a, const vector<float>& b) {
float m = 0.0;
Expand Down Expand Up @@ -69,7 +70,7 @@ TEST(TAtrac1MDCT, TAtrac1MDCTLongEncDec) {
for (int i = 0; i < 256; i++) {
hi[i] = i;
}
const TBlockSize blockSize(false, false, false);
const TAtrac1Data::TBlockSizeMod blockSize(false, false, false);

mdct.Mdct(&specs[0], &low[0], &mid[0], &hi[0], blockSize);

Expand Down Expand Up @@ -102,7 +103,7 @@ TEST(TAtrac1MDCT, TAtrac1MDCTShortEncDec) {
}
const vector<float> hiCopy = hi;

const TBlockSize blockSize(true, true, true); //short
const TAtrac1Data::TBlockSizeMod blockSize(true, true, true); //short

mdct.Mdct(&specs[0], &low[0], &mid[0], &hi[0], blockSize);

Expand Down
Loading