From 8e7372ce432a69fcd826db62962b79a562305ea2 Mon Sep 17 00:00:00 2001
From: Christian Helmrich <christian.helmrich@hhi.fraunhofer.de>
Date: Mon, 21 Jan 2019 18:50:11 +0100
Subject: [PATCH] remove obsolete and unused ENABLE_QPA related code, fix
 QPA/GBI encapsulation in EncCfg.h

---
 source/Lib/EncoderLib/Analyze.h    | 20 ------------
 source/Lib/EncoderLib/EncCfg.h     |  6 +---
 source/Lib/EncoderLib/EncGOP.cpp   | 51 ++----------------------------
 source/Lib/EncoderLib/EncSlice.cpp | 32 ++++---------------
 4 files changed, 11 insertions(+), 98 deletions(-)

diff --git a/source/Lib/EncoderLib/Analyze.h b/source/Lib/EncoderLib/Analyze.h
index 69aaad7cef..086c834bf6 100644
--- a/source/Lib/EncoderLib/Analyze.h
+++ b/source/Lib/EncoderLib/Analyze.h
@@ -60,10 +60,6 @@
 // Class definition
 // ====================================================================================================================
 
-#if ENABLE_QPA
- #define FRAME_WEIGHTING 0 // WPSNR temporal weighting according to hierarchical coding structure; only for GOP size 16
-#endif
-
 /// encoder analyzer class
 class Analyze
 {
@@ -73,10 +69,6 @@ private:
   uint32_t      m_uiNumPic;
   double    m_dFrmRate; //--CFG_KDY
   double    m_MSEyuvframe[MAX_NUM_COMPONENT]; // sum of MSEs
-#if ENABLE_QPA && FRAME_WEIGHTING
-  double    m_sumWSSD[MAX_NUM_COMPONENT];   // weighted SSDs
-  double    m_sumW;
-#endif
 #if EXTENSION_360_VIDEO
   TExt360EncAnalyze m_ext360;
 #endif
@@ -101,13 +93,7 @@ public:
     m_uiNumPic++;
   }
 #if ENABLE_QPA
- #if FRAME_WEIGHTING
-  void    addWeightedSSD(const double dWeightedSSD, const ComponentID compID) { m_sumWSSD[compID] += dWeightedSSD; }
-  void    addWeight     (const double dWeight) { m_sumW += dWeight; }
-  double  getWPSNR      (const ComponentID compID) const { return (m_sumWSSD[compID] > 0.0 ? 10.0 * log10(m_sumW / m_sumWSSD[compID]) : 999.99); }
- #else
   double  getWPSNR      (const ComponentID compID) const { return m_dPSNRSum[compID] / (double)m_uiNumPic; }
- #endif
 #endif
   double  getPsnr(ComponentID compID) const { return  m_dPSNRSum[compID];  }
   double  getBits()                   const { return  m_dAddBits;   }
@@ -125,13 +111,7 @@ public:
     {
       m_dPSNRSum[i] = 0;
       m_MSEyuvframe[i] = 0;
-#if ENABLE_QPA && FRAME_WEIGHTING
-      m_sumWSSD[i] = 0;
-#endif
     }
-#if ENABLE_QPA && FRAME_WEIGHTING
-    m_sumW = 0;
-#endif
     m_uiNumPic = 0;
 #if EXTENSION_360_VIDEO
     m_ext360.clear();
diff --git a/source/Lib/EncoderLib/EncCfg.h b/source/Lib/EncoderLib/EncCfg.h
index c60a7ad506..9655f0e97d 100644
--- a/source/Lib/EncoderLib/EncCfg.h
+++ b/source/Lib/EncoderLib/EncCfg.h
@@ -878,13 +878,9 @@ public:
 #if X0038_LAMBDA_FROM_QP_CAPABILITY
   int       getIntraQPOffset                () const    { return  m_intraQPOffset; }
   int       getLambdaFromQPEnable           () const    { return  m_lambdaFromQPEnable; }
-#if ENABLE_QPA | JVET_L0646_GBI
+
 public:
-#else
-protected:
-#endif
   int       getBaseQP                       () const { return  m_iQP; } // public should use getQPForPicture.
-public:
   int       getQPForPicture                 (const uint32_t gopIndex, const Slice *pSlice) const; // Function actually defined in EncLib.cpp
 #else
   int       getBaseQP                       ()       { return  m_iQP; }
diff --git a/source/Lib/EncoderLib/EncGOP.cpp b/source/Lib/EncoderLib/EncGOP.cpp
index 7377a0f2dd..cc12a9a9f0 100644
--- a/source/Lib/EncoderLib/EncGOP.cpp
+++ b/source/Lib/EncoderLib/EncGOP.cpp
@@ -2638,10 +2638,6 @@ void EncGOP::xGetBuffer( PicList&                  rcListPic,
 #ifndef BETA
   #define BETA 0.5 // value between 0.0 and 1; use 0.0 to obtain traditional PSNR
 #endif
-#define GLOBAL_AVERAGING 1 // "global" averaging of a_k across a set instead of one picture
-#if FRAME_WEIGHTING
-static const uint32_t DQP[16] = { 4, 12, 11, 12,  9, 12, 11, 12,  6, 12, 11, 12,  9, 12, 11, 12 };
-#endif
 
 static inline double calcWeightedSquaredError(const CPelBuf& org,        const CPelBuf& rec,
                                               double &sumAct,            const uint32_t bitDepth,
@@ -2745,9 +2741,7 @@ uint64_t EncGOP::xFindDistortionPlane(const CPelBuf& pic0, const CPelBuf& pic1,
       }
 
       double wmse = 0.0, sumAct = 0.0; // compute activity normalized SNR value
-#if !GLOBAL_AVERAGING
-      double numAct = 0.0;
-#endif
+
       for (y = 0; y < H; y += B)
       {
         for (x = 0; x < W; x += B)
@@ -2757,29 +2751,13 @@ uint64_t EncGOP::xFindDistortionPlane(const CPelBuf& pic0, const CPelBuf& pic1,
                                            W,      H,
                                            x,      y,
                                            B,      B);
-#if !GLOBAL_AVERAGING
-          numAct += 1.0;
-#endif
         }
       }
 
       // integer weighted distortion
-#if GLOBAL_AVERAGING
-      sumAct = 32.0 * double(1 << BD);
-
-      if ((W << chromaShift) > 2048 && (H << chromaShift) > 1280) // for UHD/4K
-      {
-        sumAct *= 0.5;
-      }
-      else if ((W << chromaShift) <= 1024 || (H << chromaShift) <= 640) // 480p
-      {
-        sumAct *= 2.0;
-      }
+      sumAct = 16.0 * sqrt ((3840.0 * 2160.0) / double((W << chromaShift) * (H << chromaShift))) * double(1 << BD);
 
       return (wmse <= 0.0) ? 0 : uint64_t(wmse * pow(sumAct, BETA) + 0.5);
-#else
-      return (wmse <= 0.0 || numAct <= 0.0) ? 0 : uint64_t(wmse * pow(sumAct / numAct, BETA) + 0.5);
-#endif
     }
 #endif // ENABLE_QPA
     uiTotalDiff = 0;
@@ -2984,12 +2962,7 @@ void EncGOP::xCalculateAddPSNR(Picture* pcPic, PelUnitBuf cPicD, const AccessUni
 
   const bool bPicIsField     = pcPic->fieldPic;
   const Slice*  pcSlice      = pcPic->slices[0];
-#if ENABLE_QPA && FRAME_WEIGHTING
-  const uint32_t    currDQP      = (pcSlice->getPOC() % m_pcEncLib->getIntraPeriod()) == 0 ? 0 : DQP[pcSlice->getPOC() % m_pcEncLib->getGOPSize()];
-  const double  frameWeight  = pow(2.0, (double)currDQP / -3.0);
 
-  if (useWPSNR) m_gcAnalyzeAll.addWeight(frameWeight);
-#endif
   for (int comp = 0; comp < ::getNumberValidComponents(formatD); comp++)
   {
     const ComponentID compID = ComponentID(comp);
@@ -3008,11 +2981,10 @@ void EncGOP::xCalculateAddPSNR(Picture* pcPic, PelUnitBuf cPicD, const AccessUni
     const uint32_t    bitDepth = sps.getBitDepth(toChannelType(compID));
 #if ENABLE_QPA
     const uint64_t uiSSDtemp = xFindDistortionPlane(recPB, orgPB, useWPSNR ? bitDepth : 0, ::getComponentScaleX(compID, format));
-    const uint32_t maxval = /*useWPSNR ? (1 << bitDepth) - 1 :*/ 255 << (bitDepth - 8); // fix with WPSNR: 1023 (4095) instead of 1020 (4080) for bit-depth 10 (12)
 #else
     const uint64_t uiSSDtemp = xFindDistortionPlane(recPB, orgPB, 0);
-    const uint32_t maxval = 255 << (bitDepth - 8);
 #endif
+    const uint32_t maxval = 255 << (bitDepth - 8);
     const uint32_t size   = width * height;
     const double fRefValue = (double)maxval * maxval * size;
     dPSNR[comp]       = uiSSDtemp ? 10.0 * log10(fRefValue / (double)uiSSDtemp) : 999.99;
@@ -3025,10 +2997,6 @@ void EncGOP::xCalculateAddPSNR(Picture* pcPic, PelUnitBuf cPicD, const AccessUni
       MSEyuvframeWeighted[comp] = (double)uiSSDtempWeighted / size;
     }
 #endif
-
-#if ENABLE_QPA && FRAME_WEIGHTING
-    if (useWPSNR) m_gcAnalyzeAll.addWeightedSSD(frameWeight * (double)uiSSDtemp / fRefValue, compID);
-#endif
   }
 
 #if EXTENSION_360_VIDEO
@@ -3216,13 +3184,7 @@ void EncGOP::xCalculateInterlacedAddPSNR( Picture* pcPicOrgFirstField, Picture*
 
   CHECK(!(acPicRecFields[0].chromaFormat==acPicRecFields[1].chromaFormat), "Unspecified error");
   const uint32_t numValidComponents = ::getNumberValidComponents( acPicRecFields[0].chromaFormat );
-#if ENABLE_QPA && FRAME_WEIGHTING
-  const Slice*  pcSlice      = pcPicOrgFirstField->slices[0];
-  const uint32_t    currDQP      = (pcSlice->getPOC() % m_pcEncLib->getIntraPeriod()) == 0 ? 0 : DQP[pcSlice->getPOC() % m_pcEncLib->getGOPSize()];
-  const double  frameWeight  = pow(2.0, (double)currDQP / -3.0);
 
-  if (useWPSNR) m_gcAnalyzeAll_in.addWeight(frameWeight);
-#endif
   for (int chan = 0; chan < numValidComponents; chan++)
   {
     const ComponentID ch=ComponentID(chan);
@@ -3243,18 +3205,11 @@ void EncGOP::xCalculateInterlacedAddPSNR( Picture* pcPicOrgFirstField, Picture*
       uiSSDtemp += xFindDistortionPlane( acPicRecFields[fieldNum].get(ch), apcPicOrgFields[fieldNum]->getOrigBuf().get(ch), 0 );
 #endif
     }
-#if ENABLE_QPA
-    const uint32_t maxval = /*useWPSNR ? (1 << bitDepth) - 1 :*/ 255 << (bitDepth - 8); // fix with WPSNR: 1023 (4095) instead of 1020 (4080) for bit-depth 10 (12)
-#else
     const uint32_t maxval = 255 << (bitDepth - 8);
-#endif
     const uint32_t size   = width * height * 2;
     const double fRefValue = (double)maxval * maxval * size;
     dPSNR[ch]         = uiSSDtemp ? 10.0 * log10(fRefValue / (double)uiSSDtemp) : 999.99;
     MSEyuvframe[ch]   = (double)uiSSDtemp / size;
-#if ENABLE_QPA && FRAME_WEIGHTING
-    if (useWPSNR) m_gcAnalyzeAll_in.addWeightedSSD(frameWeight * (double)uiSSDtemp / fRefValue, ch);
-#endif
   }
 
   uint32_t uibits = 0; // the number of bits for the pair is not calculated here - instead the overall total is used elsewhere.
diff --git a/source/Lib/EncoderLib/EncSlice.cpp b/source/Lib/EncoderLib/EncSlice.cpp
index acdebc0583..73b9cc08af 100644
--- a/source/Lib/EncoderLib/EncSlice.cpp
+++ b/source/Lib/EncoderLib/EncSlice.cpp
@@ -188,18 +188,9 @@ static void filterAndCalculateAverageEnergies (const Pel* pSrc, const int  iSrcS
 #if GLOBAL_AVERAGING
 static double getAveragePictureEnergy (const CPelBuf picOrig, const uint32_t uBitDepth)
 {
-  double hpEnerPic = 5.65625 * double(1 << (uBitDepth >> 1));   // square-root of a_pic value
+  const double hpEnerPic = 16.0 * sqrt ((3840.0 * 2160.0) / double(picOrig.width * picOrig.height)) * double(1 << uBitDepth);
 
-  if (picOrig.width > 2048 && picOrig.height > 1280) // for UHD/4K
-  {
-    hpEnerPic *= (4.0 / 5.65625);
-  }
-  else if (picOrig.width <= 1024 || picOrig.height <= 640) // 480p
-  {
-    hpEnerPic *= (8.0 / 5.65625);
-  }
-
-  return hpEnerPic;
+  return sqrt (hpEnerPic); // square-root of a_pic value
 }
 #endif
 
@@ -884,11 +875,7 @@ static bool applyQPAdaptation (Picture* const pcPic,     Slice* const pcSlice,
 
       int iQPAdapt = Clip3 (0, MAX_QP, iQPIndex + apprI3Log2 (pcPic->m_uEnerHpCtu[ctuRsAddr] * hpEnerPic));
 
-#if SHARP_LUMA_DELTA_QP
       if (pcv.widthInCtus > 1) // try to enforce CTU SNR greater than zero dB
-#else
-      if (!pcSlice->isIntra()) // try to enforce CTU SNR greater than zero dB
-#endif
       {
         const Pel      dcOffset   = pcPic->m_iOffsetCtu[ctuRsAddr];
 #if SHARP_LUMA_DELTA_QP
@@ -1263,7 +1250,7 @@ void EncSlice::compressSlice( Picture* pcPic, const bool bCompressEntireSlice, c
   CodingStructure&  cs          = *pcPic->cs;
 #if ENABLE_QPA || ENABLE_WPP_PARALLELISM
   const PreCalcValues& pcv      = *cs.pcv;
-  const uint32_t        widthInCtus = pcv.widthInCtus;
+  const uint32_t    widthInCtus = pcv.widthInCtus;
 #endif
 
   cs.slice = pcSlice;
@@ -1278,7 +1265,7 @@ void EncSlice::compressSlice( Picture* pcPic, const bool bCompressEntireSlice, c
   double hpEnerPic     = 0.0;
   int    iSrcOffset;
 
-  if (m_pcCfg->getUsePerceptQPA() && !m_pcCfg->getUseRateCtrl())
+  if (m_pcCfg->getUsePerceptQPA() && !m_pcCfg->getUseRateCtrl() && (boundingCtuTsAddr > startCtuTsAddr))
   {
     for (uint32_t ctuTsAddr = startCtuTsAddr; ctuTsAddr < boundingCtuTsAddr; ctuTsAddr++)
     {
@@ -1325,10 +1312,6 @@ void EncSlice::compressSlice( Picture* pcPic, const bool bCompressEntireSlice, c
       pcPic->m_iOffsetCtu[ctuRsAddr] = (Pel)iSrcOffset;
     } // end iteration over all CTUs in current slice
 
-  }
-
-  if (m_pcCfg->getUsePerceptQPA() && !m_pcCfg->getUseRateCtrl() && (boundingCtuTsAddr > startCtuTsAddr))
-  {
     const double hpEnerAvg = hpEnerPic / double(boundingCtuTsAddr - startCtuTsAddr);
 
     if (applyQPAdaptation (pcPic, pcSlice, pcv, startCtuTsAddr, boundingCtuTsAddr, m_pcCfg->getLumaLevelToDeltaQPMapping().mode == LUMALVL_TO_DQP_NUM_MODES,
@@ -1420,7 +1403,6 @@ void EncSlice::encodeCtus( Picture* pcPic, const bool bCompressEntireSlice, cons
 #endif
 #if ENABLE_QPA
   const int iQPIndex              = pcSlice->getSliceQpBase();
-  int iSrcOffset                  = 0;
 #endif
 
 #if ENABLE_WPP_PARALLELISM
@@ -1561,8 +1543,8 @@ void EncSlice::encodeCtus( Picture* pcPic, const bool bCompressEntireSlice, cons
 #if ENABLE_QPA
     else if (pCfg->getUsePerceptQPA() && pcSlice->getPPS()->getUseDQP())
     {
-      iSrcOffset = pcPic->m_iOffsetCtu[ctuRsAddr];
-      const double newLambda = oldLambda * pow (2.0, double(iSrcOffset - iQPIndex) / 3.0);
+      const int adaptedQP    = pcPic->m_iOffsetCtu[ctuRsAddr];
+      const double newLambda = oldLambda * pow (2.0, double (adaptedQP - iQPIndex) / 3.0);
       pcPic->m_uEnerHpCtu[ctuRsAddr] = newLambda;
 #if RDOQ_CHROMA_LAMBDA
       pTrQuant->getLambdas (oldLambdaArray); // save the old lambdas
@@ -1573,7 +1555,7 @@ void EncSlice::encodeCtus( Picture* pcPic, const bool bCompressEntireSlice, cons
       pTrQuant->setLambda (newLambda);
 #endif
       pRdCost->setLambda (newLambda, pcSlice->getSPS()->getBitDepths());
-      currQP[0] = currQP[1] = iSrcOffset;
+      currQP[0] = currQP[1] = adaptedQP;
     }
 #endif
 
-- 
GitLab