From f1072b45112b90fc12c79622ab36fe3294c8ec5b Mon Sep 17 00:00:00 2001
From: Seungwook Hong <seungwook.hong@nokia.com>
Date: Fri, 27 May 2022 21:41:18 +0000
Subject: [PATCH] Fix: Reduce CTC runtime when JVET_Z0118_GDR macro in on but
 GDR is not enabled

---
 source/Lib/CommonLib/CodingStructure.cpp | 61 +++++++++++++++++++++++-
 source/Lib/CommonLib/Picture.cpp         | 10 ++--
 source/Lib/DecoderLib/DecLib.cpp         | 10 +++-
 source/Lib/EncoderLib/EncGOP.cpp         |  5 +-
 source/Lib/EncoderLib/EncSlice.cpp       |  5 +-
 5 files changed, 83 insertions(+), 8 deletions(-)

diff --git a/source/Lib/CommonLib/CodingStructure.cpp b/source/Lib/CommonLib/CodingStructure.cpp
index 370d1dd02..380426b32 100644
--- a/source/Lib/CommonLib/CodingStructure.cpp
+++ b/source/Lib/CommonLib/CodingStructure.cpp
@@ -357,6 +357,11 @@ bool CodingStructure::isClean(const ChannelType effChType) const
 
 bool CodingStructure::isClean(const Position &IntPos, RefPicList e, int refIdx) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    return false;
+  }
+
   /*
     1. non gdr picture --> false;
     2. gdr picture
@@ -415,6 +420,11 @@ bool CodingStructure::isClean(const Position &IntPos, RefPicList e, int refIdx)
 
 bool CodingStructure::isClean(const Position &IntPos, const Picture* const refPic) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    return false;
+  }
+
   if (!refPic)
   {
     return false;
@@ -465,6 +475,11 @@ bool CodingStructure::isClean(const Position &IntPos, const Picture* const refPi
 
 bool CodingStructure::isClean(const int Intx, const int Inty, const ChannelType effChType) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    return false;
+  }
+
   /*
     1. non gdr picture --> false;
     2. gdr picture
@@ -517,6 +532,11 @@ bool CodingStructure::isClean(const Position &IntPos, const ChannelType effChTyp
 
 bool CodingStructure::isClean(const Area &area, const ChannelType effChType) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    return false;
+  }
+
   Position pTopLeft  = area.topLeft();
   Position pTopRight = area.topRight();
   Position pBotLeft  = area.bottomLeft();
@@ -532,6 +552,11 @@ bool CodingStructure::isClean(const Area &area, const ChannelType effChType) con
 
 bool CodingStructure::isClean(const CodingUnit &cu) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    return false;
+  }
+
   bool ret = cu.Y().valid() ? isClean(cu.Y().bottomRight(), CHANNEL_TYPE_LUMA) : isClean(cu.Cb().bottomRight(), CHANNEL_TYPE_CHROMA);
 
   return ret;
@@ -539,6 +564,11 @@ bool CodingStructure::isClean(const CodingUnit &cu) const
 
 bool CodingStructure::isClean(const PredictionUnit &pu) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    return false;
+  }
+
   bool ret = pu.Y().valid() ? isClean(pu.Y().bottomRight(), CHANNEL_TYPE_LUMA) : isClean(pu.Cb().bottomRight(), CHANNEL_TYPE_CHROMA);
 
   return ret;
@@ -546,6 +576,11 @@ bool CodingStructure::isClean(const PredictionUnit &pu) const
 
 bool CodingStructure::isClean(const TransformUnit &tu) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    return false;
+  }
+
   bool ret = tu.Y().valid() ? isClean(tu.Y().bottomRight(), CHANNEL_TYPE_LUMA) : isClean(tu.Cb().bottomRight(), CHANNEL_TYPE_CHROMA);
 
   return ret;
@@ -555,16 +590,34 @@ bool CodingStructure::isClean(const TransformUnit &tu) const
 #if JVET_Z0118_GDR
 void CodingStructure::updateReconMotIPM(const UnitArea &uarea) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    picture->getRecoBuf(uarea).copyFrom(getRecoBuf(uarea));
+    return;
+  }
+
   updateReconMotIPM(uarea, getRecoBuf(uarea));
 }
 
 void CodingStructure::updateReconMotIPM(const CompArea &carea) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    picture->getRecoBuf(carea).copyFrom(getRecoBuf(carea));   
+    return;
+  }  
+
   updateReconMotIPM(carea, getRecoBuf(carea));
 }
 
 void CodingStructure::updateReconMotIPM(const UnitArea &uarea, const CPelUnitBuf &pbuf) const
 {
+  if (!sps->getGDREnabledFlag())
+  {
+    picture->getRecoBuf(uarea).copyFrom(pbuf);
+    return;
+  }
+
   for (int i = 0; i < MAX_NUM_COMPONENT; i++)
   {
     ComponentID compID = (ComponentID)i;
@@ -577,7 +630,13 @@ void CodingStructure::updateReconMotIPM(const UnitArea &uarea, const CPelUnitBuf
 
 void CodingStructure::updateReconMotIPM(const CompArea &carea, const CPelBuf &pbuf) const
 {
-  const ComponentID compID = carea.compID;
+  if (!sps->getGDREnabledFlag())
+  {
+    picture->getRecoBuf(carea).copyFrom(pbuf);
+    return;
+  }
+
+  const ComponentID compID = carea.compID;  
 
   if (!isInGdrIntervalOrRecoveryPoc())
   {
diff --git a/source/Lib/CommonLib/Picture.cpp b/source/Lib/CommonLib/Picture.cpp
index 8a7e2a259..555a79b76 100644
--- a/source/Lib/CommonLib/Picture.cpp
+++ b/source/Lib/CommonLib/Picture.cpp
@@ -897,7 +897,6 @@ void Picture::rescalePicture( const std::pair<int, int> scalingRatio,
 
 void Picture::saveSubPicBorder(int POC, int subPicX0, int subPicY0, int subPicWidth, int subPicHeight)
 {
-
   // 1.1 set up margin for back up memory allocation
   int xMargin = margin >> getComponentScaleX(COMPONENT_Y, cs->area.chromaFormat);
   int yMargin = margin >> getComponentScaleY(COMPONENT_Y, cs->area.chromaFormat);
@@ -1000,7 +999,6 @@ void Picture::saveSubPicBorder(int POC, int subPicX0, int subPicY0, int subPicWi
 
 void Picture::extendSubPicBorder(int POC, int subPicX0, int subPicY0, int subPicWidth, int subPicHeight)
 {
-
   for (int comp = 0; comp < getNumberValidComponents(cs->area.chromaFormat); comp++)
   {
     ComponentID compID = ComponentID(comp);
@@ -1207,7 +1205,13 @@ void Picture::extendPicBorder( const PPS *pps )
   }
 
 #if JVET_Z0118_GDR
-  for (int pt = (int) PIC_RECONSTRUCTION_0; pt <= (int) PIC_RECONSTRUCTION_1; pt++) 
+  int numPt = PIC_RECONSTRUCTION_0;
+  if (cs->slice->getSPS()->getGDREnabledFlag())
+  {
+    numPt = PIC_RECONSTRUCTION_1;
+  }
+
+  for (int pt = (int) PIC_RECONSTRUCTION_0; pt <= (int) numPt; pt++)
   {
     for (int comp = 0; comp < getNumberValidComponents(cs->area.chromaFormat); comp++)
     {
diff --git a/source/Lib/DecoderLib/DecLib.cpp b/source/Lib/DecoderLib/DecLib.cpp
index b08e4ce0a..b313620ef 100644
--- a/source/Lib/DecoderLib/DecLib.cpp
+++ b/source/Lib/DecoderLib/DecLib.cpp
@@ -828,7 +828,10 @@ void DecLib::finishPicture(int& poc, PicList*& rpcListPic, MsgLevel msgl )
 
 #if JVET_Z0118_GDR
  m_pcPic->setCleanDirty(false);
- m_pcPic->copyCleanCurPicture(); 
+ if (m_pcPic->cs->sps->getGDREnabledFlag())
+ {
+   m_pcPic->copyCleanCurPicture();
+ }
 #endif
 
   Slice*  pcSlice = m_pcPic->cs->slice;
@@ -3116,7 +3119,10 @@ bool DecLib::xDecodeSlice(InputNALUnit &nalu, int &iSkipFrame, int iPOCLastDispl
 #endif
   
 #if JVET_Z0118_GDR
-  m_pcPic->initCleanCurPicture();
+  if (m_pcPic->cs->sps->getGDREnabledFlag())
+  {
+    m_pcPic->initCleanCurPicture();
+  }
 #endif
 
   //  Decode a picture
diff --git a/source/Lib/EncoderLib/EncGOP.cpp b/source/Lib/EncoderLib/EncGOP.cpp
index 57b234ea7..9b138f52f 100644
--- a/source/Lib/EncoderLib/EncGOP.cpp
+++ b/source/Lib/EncoderLib/EncGOP.cpp
@@ -3994,7 +3994,10 @@ void EncGOP::compressGOP(int iPOCLast, int iNumPicRcvd, PicList &rcListPic, std:
 
 #if JVET_Z0118_GDR
       pcPic->setCleanDirty(false);
-      pcPic->copyCleanCurPicture();      
+      if (pcPic->cs->sps->getGDREnabledFlag())
+      {
+        pcPic->copyCleanCurPicture();
+      }
 #endif
 
       //-- For time output for each slice
diff --git a/source/Lib/EncoderLib/EncSlice.cpp b/source/Lib/EncoderLib/EncSlice.cpp
index b1db0c91c..45b45f824 100644
--- a/source/Lib/EncoderLib/EncSlice.cpp
+++ b/source/Lib/EncoderLib/EncSlice.cpp
@@ -913,7 +913,10 @@ void EncSlice::initEncSlice(Picture* pcPic, const int pocLast, const int pocCurr
     }
 
 #if JVET_Z0118_GDR    
-    pcPic->initCleanCurPicture();
+    if (pcPic->cs->sps->getGDREnabledFlag())
+    {
+      pcPic->initCleanCurPicture();
+    }
 #endif
 
   }
-- 
GitLab