From 43d0f72d0ad120b3a3e426a8b47e7f77d3b59043 Mon Sep 17 00:00:00 2001
From: Christian Helmrich <christian.helmrich@hhi.fraunhofer.de>
Date: Fri, 21 Jun 2019 16:16:29 +0200
Subject: [PATCH 1/4] use true orig buffer when reshaper is active

---
 source/Lib/EncoderLib/EncSlice.cpp | 45 +++++++++++++++---------------
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/source/Lib/EncoderLib/EncSlice.cpp b/source/Lib/EncoderLib/EncSlice.cpp
index b49240c0f3..5c1e397fe7 100644
--- a/source/Lib/EncoderLib/EncSlice.cpp
+++ b/source/Lib/EncoderLib/EncSlice.cpp
@@ -203,7 +203,7 @@ static double getAveragePictureEnergy (const CPelBuf picOrig, const uint32_t uBi
 }
 #endif
 
-static int getGlaringColorQPOffset (Picture* const pcPic, const int ctuAddr, const uint32_t startAddr, const uint32_t boundingAddr,
+static int getGlaringColorQPOffset (Picture* const pcPic, Slice* const pcSlice, const int ctuAddr, const uint32_t startAddr, const uint32_t boundingAddr,
                                     const int bitDepth,   uint32_t &avgLumaValue)
 {
   const PreCalcValues& pcv  = *pcPic->cs->pcv;
@@ -212,7 +212,7 @@ static int getGlaringColorQPOffset (Picture* const pcPic, const int ctuAddr, con
   const uint32_t chrHeight  = pcv.maxCUHeight >> getChannelTypeScaleY (CH_C, chrFmt);
   const int      midLevel   = 1 << (bitDepth - 1);
   int chrValue = MAX_INT;
-  avgLumaValue = (startAddr < boundingAddr) ? 0 : (uint32_t)pcPic->getOrigBuf().Y().computeAvg();
+  avgLumaValue = (startAddr < boundingAddr) ? 0 : uint32_t ((pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf() : pcPic->getOrigBuf()).Y().computeAvg());
 
   if (ctuAddr >= 0) // luma
   {
@@ -241,9 +241,9 @@ static int getGlaringColorQPOffset (Picture* const pcPic, const int ctuAddr, con
     {
       const CompArea chrArea = clipArea (CompArea (compID, chrFmt, Area ((ctuAddr % pcv.widthInCtus) * chrWidth, (ctuAddr / pcv.widthInCtus) * chrHeight, chrWidth, chrHeight)), pcPic->block (compID));
 
-      avgCompValue = pcPic->getOrigBuf (chrArea).computeAvg();
+      avgCompValue = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (chrArea) : pcPic->getOrigBuf (chrArea)).computeAvg();
     }
-    else avgCompValue = pcPic->getOrigBuf (pcPic->block (compID)).computeAvg();
+    else avgCompValue = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (pcPic->block (compID)) : pcPic->getOrigBuf (pcPic->block (compID))).computeAvg();
 
     if (chrValue > avgCompValue) chrValue = avgCompValue; // minimum of the DC offsets
   }
@@ -267,7 +267,7 @@ static int applyQPAdaptationChroma (Picture* const pcPic, Slice* const pcSlice,
   for (uint32_t comp = 0; comp < getNumberValidComponents (pcPic->chromaFormat); comp++)
   {
     const ComponentID compID = (ComponentID)comp;
-    const CPelBuf    picOrig = pcPic->getOrigBuf (pcPic->block (compID));
+    const CPelBuf    picOrig = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (pcPic->block (compID)) : pcPic->getOrigBuf (pcPic->block (compID)));
 
     filterAndCalculateAverageEnergies (picOrig.buf,    picOrig.stride, hpEner[comp],
                                        picOrig.height, picOrig.width,  bitDepth - (isChroma (compID) ? 1 : 0));
@@ -277,13 +277,14 @@ static int applyQPAdaptationChroma (Picture* const pcPic, Slice* const pcSlice,
 
       if (savedLumaQP < 0)
       {
+        PelBuf  picOrigLuma  = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf().Y() : pcPic->getOrigBuf().Y());
 #if GLOBAL_AVERAGING
-        int     averageAdaptedLumaQP = Clip3 (0, MAX_QP, sliceQP + apprI3Log2 (hpEner[0] / getAveragePictureEnergy (pcPic->getOrigBuf().Y(), bitDepth)));
+        int     averageAdaptedLumaQP = Clip3 (0, MAX_QP, sliceQP + apprI3Log2 (hpEner[0] / getAveragePictureEnergy (picOrigLuma, bitDepth)));
 #else
         int     averageAdaptedLumaQP = Clip3 (0, MAX_QP, sliceQP); // mean slice QP
 #endif
 
-        averageAdaptedLumaQP += getGlaringColorQPOffset (pcPic, -1 /*ctuRsAddr*/, 0 /*startAddr*/, 0 /*boundingAddr*/, bitDepth, meanLuma);
+        averageAdaptedLumaQP += getGlaringColorQPOffset (pcPic, pcSlice, -1 /*ctuRsAddr*/, 0 /*startAddr*/, 0 /*boundingAddr*/, bitDepth, meanLuma);
 
         if (averageAdaptedLumaQP > MAX_QP
 #if SHARP_LUMA_DELTA_QP
@@ -295,7 +296,7 @@ static int applyQPAdaptationChroma (Picture* const pcPic, Slice* const pcSlice,
         // change mean picture QP index based on picture's average luma value (Sharp)
         if (pcEncCfg->getLumaLevelToDeltaQPMapping().mode == LUMALVL_TO_DQP_NUM_MODES)
         {
-          if (meanLuma == MAX_UINT) meanLuma = pcPic->getOrigBuf().Y().computeAvg();
+          if (meanLuma == MAX_UINT) meanLuma = picOrigLuma.computeAvg();
 
           averageAdaptedLumaQP = Clip3 (0, MAX_QP, averageAdaptedLumaQP + lumaDQPOffset (meanLuma, bitDepth));
         }
@@ -891,20 +892,21 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
       const Position pos ((ctuRsAddr % pcv.widthInCtus) * pcv.maxCUWidth, (ctuRsAddr / pcv.widthInCtus) * pcv.maxCUHeight);
       const CompArea ctuArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (pos.x, pos.y, pcv.maxCUWidth, pcv.maxCUHeight)), pcPic->Y());
       const CompArea fltArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (pos.x > 0 ? pos.x - 1 : 0, pos.y > 0 ? pos.y - 1 : 0, pcv.maxCUWidth + (pos.x > 0 ? 2 : 1), pcv.maxCUHeight + (pos.y > 0 ? 2 : 1))), pcPic->Y());
-      const CPelBuf  picOrig    = pcPic->getOrigBuf (fltArea);
+      const CPelBuf  picOrig    = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (fltArea) : pcPic->getOrigBuf (fltArea));
       double hpEner = 0.0;
 
       filterAndCalculateAverageEnergies (picOrig.buf,    picOrig.stride, hpEner,
                                          picOrig.height, picOrig.width,  bitDepth);
       hpEnerAvg += hpEner;
       pcPic->m_uEnerHpCtu[ctuRsAddr] = hpEner;
-      pcPic->m_iOffsetCtu[ctuRsAddr] = pcPic->getOrigBuf (ctuArea).computeAvg();
+      pcPic->m_iOffsetCtu[ctuRsAddr] = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (ctuArea) : pcPic->getOrigBuf (ctuArea)).computeAvg();
     }
 
     hpEnerAvg /= double (boundingAddr - startAddr);
   }
+  PelBuf     picOrigLuma = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf().Y() : pcPic->getOrigBuf().Y());
 #if GLOBAL_AVERAGING
-  const double hpEnerPic = 1.0 / getAveragePictureEnergy (pcPic->getOrigBuf().Y(), bitDepth);  // inverse, speed
+  const double hpEnerPic = 1.0 / getAveragePictureEnergy (picOrigLuma, bitDepth); // inv. to speed up code below
 #else
   const double hpEnerPic = 1.0 / hpEnerAvg; // speedup: multiply instead of divide in loop below; 1.0 for tuning
 #endif
@@ -915,7 +917,7 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
 
     if (isChromaEnabled (pcPic->chromaFormat) && (iQPIndex < MAX_QP) && (previouslyAdaptedLumaQP < 0))
     {
-      iQPFixed += getGlaringColorQPOffset (pcPic, -1 /*ctuRsAddr*/, startAddr, boundingAddr, bitDepth, meanLuma);
+      iQPFixed += getGlaringColorQPOffset (pcPic, pcSlice, -1 /*ctuRsAddr*/, startAddr, boundingAddr, bitDepth, meanLuma);
 
       if (iQPFixed > MAX_QP
 #if SHARP_LUMA_DELTA_QP
@@ -993,7 +995,7 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
 
         if (isChromaEnabled (pcPic->chromaFormat))
         {
-          iQPAdapt += getGlaringColorQPOffset (pcPic, (int)ctuRsAddr, startAddr, boundingAddr, bitDepth, meanLuma);
+          iQPAdapt += getGlaringColorQPOffset (pcPic, pcSlice, (int)ctuRsAddr, startAddr, boundingAddr, bitDepth, meanLuma);
 
           if (iQPAdapt > MAX_QP
 #if SHARP_LUMA_DELTA_QP
@@ -1020,10 +1022,11 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
         const uint32_t uRefScale  = g_invQuantScales[iQPAdapt % 6] << ((iQPAdapt / 6) + bitDepth - 4);
 #endif
         const CompArea subArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area ((ctuRsAddr % pcv.widthInCtus) * pcv.maxCUWidth, (ctuRsAddr / pcv.widthInCtus) * pcv.maxCUHeight, pcv.maxCUWidth, pcv.maxCUHeight)), pcPic->Y());
-        const Pel*     pSrc       = pcPic->getOrigBuf (subArea).buf;
-        const SizeType iSrcStride = pcPic->getOrigBuf (subArea).stride;
-        const SizeType iSrcHeight = pcPic->getOrigBuf (subArea).height;
-        const SizeType iSrcWidth  = pcPic->getOrigBuf (subArea).width;
+        const CPelBuf  picOrigSub = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (subArea) : pcPic->getOrigBuf (subArea));
+        const Pel*     pSrc       = picOrigSub.buf;
+        const SizeType iSrcStride = picOrigSub.stride;
+        const SizeType iSrcHeight = picOrigSub.height;
+        const SizeType iSrcWidth  = picOrigSub.width;
         uint32_t uAbsDCless = 0;
 
         // compute sum of absolute DC-less (high-pass) luma values
@@ -1058,9 +1061,7 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
       pcPic->m_iOffsetCtu[ctuRsAddr] = (Pel)iQPAdapt; // adapted QPs
 
 #if ENABLE_QPA_SUB_CTU
-      if (pcv.widthInCtus > 1 && pcSlice->getPPS()->getCuQpDeltaSubdiv() == 0)  // reduce local DQP rate peaks
-#elif ENABLE_QPA_SUB_CTU
-      if (pcv.widthInCtus > 1 && pcSlice->getPPS()->getMaxCuDQPDepth() == 0)  // reduce local DQP rate peaks
+      if (pcv.widthInCtus > 1 && pcSlice->getPPS()->getCuQpDeltaSubdiv() == 0) // reduce local QP rate peaks
 #else
       if (pcv.widthInCtus > 1) // try to reduce local bitrate peaks via minimum smoothing of the adapted QPs
 #endif
@@ -1140,7 +1141,7 @@ static int applyQPAdaptationSubCtu (CodingStructure &cs, const UnitArea ctuArea,
         const PosType  x       = ctuArea.lx() + w * mts;
         const PosType  y       = ctuArea.ly() + h * mts;
         const CompArea fltArea = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (x > 0 ? x - 1 : 0, y > 0 ? y - 1 : 0, mts + (x > 0 ? 2 : 1), mts + (y > 0 ? 2 : 1))), pcPic->Y());
-        const CPelBuf  picOrig = pcPic->getOrigBuf (fltArea);
+        const CPelBuf  picOrig = (cs.slice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (fltArea) : pcPic->getOrigBuf (fltArea));
 
         if (x >= pcPic->lwidth() || y >= pcPic->lheight())
         {
@@ -1156,7 +1157,7 @@ static int applyQPAdaptationSubCtu (CodingStructure &cs, const UnitArea ctuArea,
         {
           const CompArea subArea = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (x, y, mts, mts)), pcPic->Y());
 
-          subMLV[addr] = pcPic->getOrigBuf (subArea).computeAvg();
+          subMLV[addr] = (cs.slice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (subArea) : pcPic->getOrigBuf (subArea)).computeAvg();
         }
 #endif
       }
-- 
GitLab


From 88f762e9dc6bae356f154b615f842461c7a3719a Mon Sep 17 00:00:00 2001
From: Christian Helmrich <christian.helmrich@hhi.fraunhofer.de>
Date: Thu, 15 Aug 2019 11:51:48 +0200
Subject: [PATCH 2/4] Revert "use true orig buffer when reshaper is active"

This reverts commit 43d0f72d0ad120b3a3e426a8b47e7f77d3b59043
---
 source/Lib/EncoderLib/EncSlice.cpp | 45 +++++++++++++++---------------
 1 file changed, 22 insertions(+), 23 deletions(-)

diff --git a/source/Lib/EncoderLib/EncSlice.cpp b/source/Lib/EncoderLib/EncSlice.cpp
index 5c1e397fe7..b49240c0f3 100644
--- a/source/Lib/EncoderLib/EncSlice.cpp
+++ b/source/Lib/EncoderLib/EncSlice.cpp
@@ -203,7 +203,7 @@ static double getAveragePictureEnergy (const CPelBuf picOrig, const uint32_t uBi
 }
 #endif
 
-static int getGlaringColorQPOffset (Picture* const pcPic, Slice* const pcSlice, const int ctuAddr, const uint32_t startAddr, const uint32_t boundingAddr,
+static int getGlaringColorQPOffset (Picture* const pcPic, const int ctuAddr, const uint32_t startAddr, const uint32_t boundingAddr,
                                     const int bitDepth,   uint32_t &avgLumaValue)
 {
   const PreCalcValues& pcv  = *pcPic->cs->pcv;
@@ -212,7 +212,7 @@ static int getGlaringColorQPOffset (Picture* const pcPic, Slice* const pcSlice,
   const uint32_t chrHeight  = pcv.maxCUHeight >> getChannelTypeScaleY (CH_C, chrFmt);
   const int      midLevel   = 1 << (bitDepth - 1);
   int chrValue = MAX_INT;
-  avgLumaValue = (startAddr < boundingAddr) ? 0 : uint32_t ((pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf() : pcPic->getOrigBuf()).Y().computeAvg());
+  avgLumaValue = (startAddr < boundingAddr) ? 0 : (uint32_t)pcPic->getOrigBuf().Y().computeAvg();
 
   if (ctuAddr >= 0) // luma
   {
@@ -241,9 +241,9 @@ static int getGlaringColorQPOffset (Picture* const pcPic, Slice* const pcSlice,
     {
       const CompArea chrArea = clipArea (CompArea (compID, chrFmt, Area ((ctuAddr % pcv.widthInCtus) * chrWidth, (ctuAddr / pcv.widthInCtus) * chrHeight, chrWidth, chrHeight)), pcPic->block (compID));
 
-      avgCompValue = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (chrArea) : pcPic->getOrigBuf (chrArea)).computeAvg();
+      avgCompValue = pcPic->getOrigBuf (chrArea).computeAvg();
     }
-    else avgCompValue = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (pcPic->block (compID)) : pcPic->getOrigBuf (pcPic->block (compID))).computeAvg();
+    else avgCompValue = pcPic->getOrigBuf (pcPic->block (compID)).computeAvg();
 
     if (chrValue > avgCompValue) chrValue = avgCompValue; // minimum of the DC offsets
   }
@@ -267,7 +267,7 @@ static int applyQPAdaptationChroma (Picture* const pcPic, Slice* const pcSlice,
   for (uint32_t comp = 0; comp < getNumberValidComponents (pcPic->chromaFormat); comp++)
   {
     const ComponentID compID = (ComponentID)comp;
-    const CPelBuf    picOrig = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (pcPic->block (compID)) : pcPic->getOrigBuf (pcPic->block (compID)));
+    const CPelBuf    picOrig = pcPic->getOrigBuf (pcPic->block (compID));
 
     filterAndCalculateAverageEnergies (picOrig.buf,    picOrig.stride, hpEner[comp],
                                        picOrig.height, picOrig.width,  bitDepth - (isChroma (compID) ? 1 : 0));
@@ -277,14 +277,13 @@ static int applyQPAdaptationChroma (Picture* const pcPic, Slice* const pcSlice,
 
       if (savedLumaQP < 0)
       {
-        PelBuf  picOrigLuma  = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf().Y() : pcPic->getOrigBuf().Y());
 #if GLOBAL_AVERAGING
-        int     averageAdaptedLumaQP = Clip3 (0, MAX_QP, sliceQP + apprI3Log2 (hpEner[0] / getAveragePictureEnergy (picOrigLuma, bitDepth)));
+        int     averageAdaptedLumaQP = Clip3 (0, MAX_QP, sliceQP + apprI3Log2 (hpEner[0] / getAveragePictureEnergy (pcPic->getOrigBuf().Y(), bitDepth)));
 #else
         int     averageAdaptedLumaQP = Clip3 (0, MAX_QP, sliceQP); // mean slice QP
 #endif
 
-        averageAdaptedLumaQP += getGlaringColorQPOffset (pcPic, pcSlice, -1 /*ctuRsAddr*/, 0 /*startAddr*/, 0 /*boundingAddr*/, bitDepth, meanLuma);
+        averageAdaptedLumaQP += getGlaringColorQPOffset (pcPic, -1 /*ctuRsAddr*/, 0 /*startAddr*/, 0 /*boundingAddr*/, bitDepth, meanLuma);
 
         if (averageAdaptedLumaQP > MAX_QP
 #if SHARP_LUMA_DELTA_QP
@@ -296,7 +295,7 @@ static int applyQPAdaptationChroma (Picture* const pcPic, Slice* const pcSlice,
         // change mean picture QP index based on picture's average luma value (Sharp)
         if (pcEncCfg->getLumaLevelToDeltaQPMapping().mode == LUMALVL_TO_DQP_NUM_MODES)
         {
-          if (meanLuma == MAX_UINT) meanLuma = picOrigLuma.computeAvg();
+          if (meanLuma == MAX_UINT) meanLuma = pcPic->getOrigBuf().Y().computeAvg();
 
           averageAdaptedLumaQP = Clip3 (0, MAX_QP, averageAdaptedLumaQP + lumaDQPOffset (meanLuma, bitDepth));
         }
@@ -892,21 +891,20 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
       const Position pos ((ctuRsAddr % pcv.widthInCtus) * pcv.maxCUWidth, (ctuRsAddr / pcv.widthInCtus) * pcv.maxCUHeight);
       const CompArea ctuArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (pos.x, pos.y, pcv.maxCUWidth, pcv.maxCUHeight)), pcPic->Y());
       const CompArea fltArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (pos.x > 0 ? pos.x - 1 : 0, pos.y > 0 ? pos.y - 1 : 0, pcv.maxCUWidth + (pos.x > 0 ? 2 : 1), pcv.maxCUHeight + (pos.y > 0 ? 2 : 1))), pcPic->Y());
-      const CPelBuf  picOrig    = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (fltArea) : pcPic->getOrigBuf (fltArea));
+      const CPelBuf  picOrig    = pcPic->getOrigBuf (fltArea);
       double hpEner = 0.0;
 
       filterAndCalculateAverageEnergies (picOrig.buf,    picOrig.stride, hpEner,
                                          picOrig.height, picOrig.width,  bitDepth);
       hpEnerAvg += hpEner;
       pcPic->m_uEnerHpCtu[ctuRsAddr] = hpEner;
-      pcPic->m_iOffsetCtu[ctuRsAddr] = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (ctuArea) : pcPic->getOrigBuf (ctuArea)).computeAvg();
+      pcPic->m_iOffsetCtu[ctuRsAddr] = pcPic->getOrigBuf (ctuArea).computeAvg();
     }
 
     hpEnerAvg /= double (boundingAddr - startAddr);
   }
-  PelBuf     picOrigLuma = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf().Y() : pcPic->getOrigBuf().Y());
 #if GLOBAL_AVERAGING
-  const double hpEnerPic = 1.0 / getAveragePictureEnergy (picOrigLuma, bitDepth); // inv. to speed up code below
+  const double hpEnerPic = 1.0 / getAveragePictureEnergy (pcPic->getOrigBuf().Y(), bitDepth);  // inverse, speed
 #else
   const double hpEnerPic = 1.0 / hpEnerAvg; // speedup: multiply instead of divide in loop below; 1.0 for tuning
 #endif
@@ -917,7 +915,7 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
 
     if (isChromaEnabled (pcPic->chromaFormat) && (iQPIndex < MAX_QP) && (previouslyAdaptedLumaQP < 0))
     {
-      iQPFixed += getGlaringColorQPOffset (pcPic, pcSlice, -1 /*ctuRsAddr*/, startAddr, boundingAddr, bitDepth, meanLuma);
+      iQPFixed += getGlaringColorQPOffset (pcPic, -1 /*ctuRsAddr*/, startAddr, boundingAddr, bitDepth, meanLuma);
 
       if (iQPFixed > MAX_QP
 #if SHARP_LUMA_DELTA_QP
@@ -995,7 +993,7 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
 
         if (isChromaEnabled (pcPic->chromaFormat))
         {
-          iQPAdapt += getGlaringColorQPOffset (pcPic, pcSlice, (int)ctuRsAddr, startAddr, boundingAddr, bitDepth, meanLuma);
+          iQPAdapt += getGlaringColorQPOffset (pcPic, (int)ctuRsAddr, startAddr, boundingAddr, bitDepth, meanLuma);
 
           if (iQPAdapt > MAX_QP
 #if SHARP_LUMA_DELTA_QP
@@ -1022,11 +1020,10 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
         const uint32_t uRefScale  = g_invQuantScales[iQPAdapt % 6] << ((iQPAdapt / 6) + bitDepth - 4);
 #endif
         const CompArea subArea    = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area ((ctuRsAddr % pcv.widthInCtus) * pcv.maxCUWidth, (ctuRsAddr / pcv.widthInCtus) * pcv.maxCUHeight, pcv.maxCUWidth, pcv.maxCUHeight)), pcPic->Y());
-        const CPelBuf  picOrigSub = (pcSlice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (subArea) : pcPic->getOrigBuf (subArea));
-        const Pel*     pSrc       = picOrigSub.buf;
-        const SizeType iSrcStride = picOrigSub.stride;
-        const SizeType iSrcHeight = picOrigSub.height;
-        const SizeType iSrcWidth  = picOrigSub.width;
+        const Pel*     pSrc       = pcPic->getOrigBuf (subArea).buf;
+        const SizeType iSrcStride = pcPic->getOrigBuf (subArea).stride;
+        const SizeType iSrcHeight = pcPic->getOrigBuf (subArea).height;
+        const SizeType iSrcWidth  = pcPic->getOrigBuf (subArea).width;
         uint32_t uAbsDCless = 0;
 
         // compute sum of absolute DC-less (high-pass) luma values
@@ -1061,7 +1058,9 @@ static bool applyQPAdaptation (Picture* const pcPic,       Slice* const pcSlice,
       pcPic->m_iOffsetCtu[ctuRsAddr] = (Pel)iQPAdapt; // adapted QPs
 
 #if ENABLE_QPA_SUB_CTU
-      if (pcv.widthInCtus > 1 && pcSlice->getPPS()->getCuQpDeltaSubdiv() == 0) // reduce local QP rate peaks
+      if (pcv.widthInCtus > 1 && pcSlice->getPPS()->getCuQpDeltaSubdiv() == 0)  // reduce local DQP rate peaks
+#elif ENABLE_QPA_SUB_CTU
+      if (pcv.widthInCtus > 1 && pcSlice->getPPS()->getMaxCuDQPDepth() == 0)  // reduce local DQP rate peaks
 #else
       if (pcv.widthInCtus > 1) // try to reduce local bitrate peaks via minimum smoothing of the adapted QPs
 #endif
@@ -1141,7 +1140,7 @@ static int applyQPAdaptationSubCtu (CodingStructure &cs, const UnitArea ctuArea,
         const PosType  x       = ctuArea.lx() + w * mts;
         const PosType  y       = ctuArea.ly() + h * mts;
         const CompArea fltArea = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (x > 0 ? x - 1 : 0, y > 0 ? y - 1 : 0, mts + (x > 0 ? 2 : 1), mts + (y > 0 ? 2 : 1))), pcPic->Y());
-        const CPelBuf  picOrig = (cs.slice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (fltArea) : pcPic->getOrigBuf (fltArea));
+        const CPelBuf  picOrig = pcPic->getOrigBuf (fltArea);
 
         if (x >= pcPic->lwidth() || y >= pcPic->lheight())
         {
@@ -1157,7 +1156,7 @@ static int applyQPAdaptationSubCtu (CodingStructure &cs, const UnitArea ctuArea,
         {
           const CompArea subArea = clipArea (CompArea (COMPONENT_Y, pcPic->chromaFormat, Area (x, y, mts, mts)), pcPic->Y());
 
-          subMLV[addr] = (cs.slice->getSPS()->getUseReshaper() ? pcPic->getTrueOrigBuf (subArea) : pcPic->getOrigBuf (subArea)).computeAvg();
+          subMLV[addr] = pcPic->getOrigBuf (subArea).computeAvg();
         }
 #endif
       }
-- 
GitLab


From f7e67b7acd9b50d5748c31ee953a64d8ac49cbae Mon Sep 17 00:00:00 2001
From: Christian Helmrich <christian.helmrich@hhi.fraunhofer.de>
Date: Thu, 15 Aug 2019 12:00:30 +0200
Subject: [PATCH 3/4] fix sub-CTU QPA affecting HDR coding even when disabled

---
 source/App/EncoderApp/EncAppCfg.cpp |  3 +++
 source/Lib/EncoderLib/EncCu.cpp     | 23 +++++++++++++++++------
 source/Lib/EncoderLib/EncCu.h       |  8 ++++++--
 3 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/source/App/EncoderApp/EncAppCfg.cpp b/source/App/EncoderApp/EncAppCfg.cpp
index c1dced4d8f..02dbc955a3 100644
--- a/source/App/EncoderApp/EncAppCfg.cpp
+++ b/source/App/EncoderApp/EncAppCfg.cpp
@@ -2214,6 +2214,9 @@ bool EncAppCfg::parseCfg( int argc, char* argv[] )
  #else
   if (((int)m_fQP < 38) && m_bUsePerceptQPA && !m_bUseAdaptiveQP && (m_iSourceWidth <= 2048) && (m_iSourceHeight <= 1280)
  #endif
+ #if WCG_EXT && ER_CHROMA_QP_WCG_PPS
+      && (!m_wcgChromaQpControl.enabled)
+ #endif
 #if MAX_TB_SIZE_SIGNALLING
       && ((1 << (m_log2MaxTbSize + 1)) == m_uiCTUSize) && (m_iSourceWidth > 512 || m_iSourceHeight > 320))
 #else
diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp
index b32a868382..8836fa7ec1 100644
--- a/source/Lib/EncoderLib/EncCu.cpp
+++ b/source/Lib/EncoderLib/EncCu.cpp
@@ -800,7 +800,11 @@ void EncCu::xCompressCU( CodingStructure *&tempCS, CodingStructure *&bestCS, Par
 #endif
       if (currTestMode.qp >= 0)
       {
-        updateLambda (&slice, currTestMode.qp, CS::isDualITree (*tempCS) || (partitioner.currDepth == 0));
+        updateLambda (&slice, currTestMode.qp,
+ #if WCG_EXT && ER_CHROMA_QP_WCG_PPS
+                      m_pcEncCfg->getWCGChromaQPControl().isEnabled(),
+ #endif
+                      CS::isDualITree (*tempCS) || (partitioner.currDepth == 0));
       }
     }
 #endif
@@ -1077,9 +1081,15 @@ void EncCu::xCompressCU( CodingStructure *&tempCS, CodingStructure *&bestCS, Par
 }
 
 #if SHARP_LUMA_DELTA_QP || ENABLE_QPA_SUB_CTU
-void EncCu::updateLambda (Slice* slice, const int dQP, const bool updateRdCostLambda)
+void EncCu::updateLambda (Slice* slice, const int dQP,
+ #if WCG_EXT && ER_CHROMA_QP_WCG_PPS
+                          const bool useWCGChromaControl,
+ #endif
+                          const bool updateRdCostLambda)
 {
-#if WCG_EXT && !ENABLE_QPA_SUB_CTU
+#if WCG_EXT && ER_CHROMA_QP_WCG_PPS
+ if (useWCGChromaControl)
+ {
   int    NumberBFrames = ( m_pcEncCfg->getGOPSize() - 1 );
   int    SHIFT_QP = 12;
   double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(double)(slice->getPic()->fieldPic ? NumberBFrames/2 : NumberBFrames) );
@@ -1144,7 +1154,9 @@ void EncCu::updateLambda (Slice* slice, const int dQP, const bool updateRdCostLa
   int iQP = Clip3(-qpBDoffset, MAX_QP, (int)floor((double)dQP + 0.5));
   m_pcSliceEncoder->setUpLambda(slice, dLambda, iQP);
 
-#else
+  return;
+ }
+#endif
   int iQP = dQP;
   const double oldQP     = (double)slice->getSliceQpBase();
 #if ENABLE_QPA_SUB_CTU
@@ -1165,9 +1177,8 @@ void EncCu::updateLambda (Slice* slice, const int dQP, const bool updateRdCostLa
   {
     m_pcRdCost->setLambda (newLambda, slice->getSPS()->getBitDepths());
   }
-#endif
 }
-#endif
+#endif // SHARP_LUMA_DELTA_QP || ENABLE_QPA_SUB_CTU
 
 #if ENABLE_SPLIT_PARALLELISM
 //#undef DEBUG_PARALLEL_TIMINGS
diff --git a/source/Lib/EncoderLib/EncCu.h b/source/Lib/EncoderLib/EncCu.h
index 41a449efbb..5a50a1c513 100644
--- a/source/Lib/EncoderLib/EncCu.h
+++ b/source/Lib/EncoderLib/EncCu.h
@@ -142,9 +142,13 @@ private:
   int                   m_bestGbiIdx[2];
   double                m_bestGbiCost[2];
   TriangleMotionInfo    m_triangleModeTest[TRIANGLE_MAX_NUM_CANDS];
-  uint8_t                          m_triangleIdxBins[2][TRIANGLE_MAX_NUM_UNI_CANDS][TRIANGLE_MAX_NUM_UNI_CANDS];
+  uint8_t               m_triangleIdxBins[2][TRIANGLE_MAX_NUM_UNI_CANDS][TRIANGLE_MAX_NUM_UNI_CANDS];
 #if SHARP_LUMA_DELTA_QP || ENABLE_QPA_SUB_CTU
-  void    updateLambda      ( Slice* slice, const int dQP, const bool updateRdCostLambda );
+  void    updateLambda      ( Slice* slice, const int dQP,
+ #if WCG_EXT && ER_CHROMA_QP_WCG_PPS
+                              const bool useWCGChromaControl,
+ #endif
+                              const bool updateRdCostLambda );
 #endif
   double                m_sbtCostSave[2];
 public:
-- 
GitLab


From 8ac16bc54032f75c380a0c292ab79d3acd49e22f Mon Sep 17 00:00:00 2001
From: Christian Helmrich <christian.helmrich@hhi.fraunhofer.de>
Date: Thu, 15 Aug 2019 17:00:52 +0200
Subject: [PATCH 4/4] remove unnecessary differences between updateLambda()
 versions

---
 source/Lib/EncoderLib/EncCu.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp
index 8836fa7ec1..7b7d0e5744 100644
--- a/source/Lib/EncoderLib/EncCu.cpp
+++ b/source/Lib/EncoderLib/EncCu.cpp
@@ -1103,7 +1103,7 @@ void EncCu::updateLambda (Slice* slice, const int dQP,
 
   if( slice->getSliceType() == I_SLICE )
   {
-    if( m_pcEncCfg->getIntraQpFactor() >= 0.0 /*&& m_pcEncCfg->getGOPEntry( m_pcSliceEncoder->getGopId() ).m_sliceType != I_SLICE*/ )
+    if( m_pcEncCfg->getIntraQpFactor() >= 0.0 && m_pcEncCfg->getGOPEntry( m_pcSliceEncoder->getGopId() ).m_sliceType != I_SLICE )
     {
       dQPFactor = m_pcEncCfg->getIntraQpFactor();
     }
@@ -1121,7 +1121,7 @@ void EncCu::updateLambda (Slice* slice, const int dQP,
   }
   else if( m_pcEncCfg->getLambdaFromQPEnable() )
   {
-    dQPFactor = 0.57*dQPFactor;
+    dQPFactor = 0.57;
   }
 
   double dLambda = dQPFactor*pow( 2.0, qp_temp/3.0 );
-- 
GitLab