From 2e17beffc4463d5de3ad112cf023ed511658d5b9 Mon Sep 17 00:00:00 2001 From: "Christian R. Helmrich" Date: Thu, 12 Mar 2020 02:00:00 +0100 Subject: [PATCH] low-rate tuning --- src/lib/exhaleEnc.cpp | 2 +- src/lib/quantization.cpp | 23 +++++++++++++++++++++-- src/lib/specGapFilling.cpp | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/lib/exhaleEnc.cpp b/src/lib/exhaleEnc.cpp index e6b89b8..d4b75ef 100644 --- a/src/lib/exhaleEnc.cpp +++ b/src/lib/exhaleEnc.cpp @@ -535,7 +535,7 @@ unsigned ExhaleEncoder::psychBitAllocation () // perceptual bit-allocation via s const unsigned samplingRate = toSamplingRate (m_frequencyIdx); const unsigned lfeChannelIndex = (m_channelConf >= CCI_6_CH ? __max (5, nChannels - 1) : USAC_MAX_NUM_CHANNELS); const uint32_t maxSfbLong = (samplingRate < 37566 ? 51 /*32 kHz*/ : brModeAndFsToMaxSfbLong (m_bitRateMode, samplingRate)); - const uint64_t scaleSr = (samplingRate < 27713 ? 37 - m_bitRateMode : 38 - (m_bitRateMode > 2 ? 1 : 0)); + const uint64_t scaleSr = (samplingRate < 27713 ? 38 - m_bitRateMode : 38 - (m_bitRateMode > 2 ? 1 : 0)); const uint64_t scaleBr = (m_bitRateMode == 0 ? 32 : scaleSr - eightTimesSqrt256Minus[256 - m_bitRateMode] - ((m_bitRateMode - 1) >> 1)); uint32_t* sfbStepSizes = (uint32_t*) m_tempIntBuf; uint8_t meanSpecFlat[USAC_MAX_NUM_CHANNELS]; diff --git a/src/lib/quantization.cpp b/src/lib/quantization.cpp index f28514c..33645a3 100644 --- a/src/lib/quantization.cpp +++ b/src/lib/quantization.cpp @@ -38,7 +38,7 @@ static inline short getBitCount (EntropyCoder& entrCoder, const int sfIndex, con #if EC_TRELLIS_OPT_CODING && !EC_TRAIN static inline double getLagrangeValue (const uint16_t rateIndex) // RD optimization constant { - return (108.0 + rateIndex * rateIndex) * 0.0009765625; // /1024 + return (95.0 + rateIndex * rateIndex) * 0.0009765625; // / 1024 } #endif @@ -191,14 +191,33 @@ uint8_t SfbQuantizer::quantizeMagnSfb (const unsigned* const coeffMagn, const ui // re-compute least-squares optimal scale factor modifier if (dNum > SF_THRESH_POS * dDen) sf++; +#if !SFB_QUANT_PERCEPT_OPT else - if (dNum < SF_THRESH_NEG * dDen) sf--; + if (dNum < SF_THRESH_NEG * dDen) sf--; // reduces SFB RMS +#endif } // if nonzero if (sigMaxQ) *sigMaxQ = (numQ > 0 ? maxQ : 0); // a new max if (sigNumQ) *sigNumQ = numQ; // a new nonzero coeff. count } } +#endif // EC_TRELLIS_OPT_CODING + +#if SFB_QUANT_PERCEPT_OPT + if ((numQ > 0) && (sf > 0 && sf <= scaleFactor)) // recover RMS + { + const double magnNormDiv = m_lutSfNorm[sf]; + + dNum = 0.0; // dDen has normalized energy after quantization + for (int i = numCoeffs - 1; i >= 0; i--) + { + const double normalizedMagn = (double) coeffMagn[i] * magnNormDiv; + + dNum += normalizedMagn * normalizedMagn; // TODO: use SIMD? + } + + if (dNum > SF_THRESH_POS * SF_THRESH_POS * dDen) sf++; + } #endif return (uint8_t) __max (0, sf); // optimized scale factor index } diff --git a/src/lib/specGapFilling.cpp b/src/lib/specGapFilling.cpp index 72299d8..b501018 100644 --- a/src/lib/specGapFilling.cpp +++ b/src/lib/specGapFilling.cpp @@ -165,7 +165,7 @@ uint8_t SpecGapFiller::getSpecGapFillParams (const SfbQuantizer& sfbQuantizer, c } s = 0; #endif - u = __min (7, uint16_t (14.47118288 + 9.965784285 * log10 (magnSum / (double) u))); + u = __min (7 + (specFlat >> 5), uint16_t (14.47118288 + 9.965784285 * log10 (magnSum / (double) u))); u = __max (1, u - int (specFlat >> 5)); // SFM-adaptive reduction magnSum = pow (2.0, (14 - u) / 3.0); // noiseVal^-1, 23003-3, 7.2