diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2015-12-31 01:26:42 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2015-12-31 01:26:42 +0300 |
commit | 016da62160aaeba4da182cd80f8b6dfe8c767fad (patch) | |
tree | 4e0f7a67628a18413aff1b5baea2252e74d05ea6 | |
parent | 5d7ebdd8cc837ff124e132210e3c0e6220be05a8 (diff) | |
download | atracdenc-016da62160aaeba4da182cd80f8b6dfe8c767fad.tar.gz |
improve scaling accuracy, prevent overflow
-rw-r--r-- | src/atrac/atrac1_scale.cpp | 12 | ||||
-rw-r--r-- | src/atrac/atrac1_scale.h | 2 | ||||
-rw-r--r-- | src/atracdenc.cpp | 5 | ||||
-rw-r--r-- | src/atracdenc.h | 3 |
4 files changed, 16 insertions, 6 deletions
diff --git a/src/atrac/atrac1_scale.cpp b/src/atrac/atrac1_scale.cpp index 9346a68..627513a 100644 --- a/src/atrac/atrac1_scale.cpp +++ b/src/atrac/atrac1_scale.cpp @@ -7,7 +7,7 @@ using std::vector; using std::map; using namespace std; -map<uint32_t, uint8_t> TScaler::ScaleIndex; +map<double, uint8_t> TScaler::ScaleIndex; static const uint32_t MAX_SCALE = 65536; static bool absComp(double a, double b) { @@ -17,7 +17,7 @@ static bool absComp(double a, double b) { TScaler::TScaler() { if (ScaleIndex.empty()) { for (int i = 0; i < 64; i++) { - ScaleIndex[ScaleTable[i] * 256] = i; + ScaleIndex[ScaleTable[i]] = i; } } } @@ -41,12 +41,14 @@ vector<TScaledBlock> TScaler::Scale(const vector<double>& specs, const TBlockSiz } } } - const map<uint32_t, uint8_t>::const_iterator scaleIter = ScaleIndex.lower_bound(maxAbsSpec * 256); - const double scaleFactor = scaleIter->first / 256.0; + 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; + const double scaledValue = specs[specNum] / scaleFactor; + if (scaledValue > 1.0) + cerr << "got "<< scaledValue << " value - wrong scalling" << endl; scaledBlocks.back().Values.push_back(scaledValue); } } diff --git a/src/atrac/atrac1_scale.h b/src/atrac/atrac1_scale.h index 7bbfa56..0a0e852 100644 --- a/src/atrac/atrac1_scale.h +++ b/src/atrac/atrac1_scale.h @@ -14,7 +14,7 @@ struct TScaledBlock { }; class TScaler : public TAtrac1Data { - static std::map<uint32_t, uint8_t>ScaleIndex; + static std::map<double, uint8_t>ScaleIndex; public: TScaler(); std::vector<TScaledBlock> Scale(const std::vector<double>& specs, const TBlockSize& blockSize); diff --git a/src/atracdenc.cpp b/src/atracdenc.cpp index 98c741e..86ac0a7 100644 --- a/src/atracdenc.cpp +++ b/src/atracdenc.cpp @@ -158,6 +158,11 @@ TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetDecodeLambda() { IMdct(&specs[0], mode, &PcmBufLow[channel][0], &PcmBufMid[channel][0], &PcmBufHi[channel][0]); SynthesisFilterBank[channel].Synthesis(&sum[0], &PcmBufLow[channel][0], &PcmBufMid[channel][0], &PcmBufHi[channel][0]); for (int i = 0; i < NumSamples; ++i) { + if (sum[i] > PcmValueMax) + sum[i] = PcmValueMax; + if (sum[i] < PcmValueMin) + sum[i] = PcmValueMin; + data[i][srcChannels - 1-channel] = sum[i]; } } diff --git a/src/atracdenc.h b/src/atracdenc.h index 8a50cf1..03e85ea 100644 --- a/src/atracdenc.h +++ b/src/atracdenc.h @@ -41,6 +41,9 @@ class TAtrac1Processor : public TAtrac1MDCT, public virtual TAtrac1Data { double PcmBufMid[2][256 + 16]; double PcmBufHi[2][512 + 16]; + int32_t PcmValueMax = 32767; + int32_t PcmValueMin = -32767; + Atrac1SynthesisFilterBank<double> SynthesisFilterBank[2]; Atrac1SplitFilterBank<double> SplitFilterBank[2]; |