From 2d614a3f156805ed18e75e800e194aaaa36e3975 Mon Sep 17 00:00:00 2001
From: cjj490168650 <202221010107@std.uestc.edu.cn>
Date: Tue, 28 May 2024 12:30:53 +0800
Subject: [PATCH] add error message and some other modifications

---
 source/App/EncoderApp/EncAppCfg.cpp   |  6 +++-
 source/Lib/CommonLib/Rom.h            |  1 -
 source/Lib/EncoderLib/EncLib.h        |  7 ++---
 source/Lib/EncoderLib/EncModeCtrl.cpp |  9 +++---
 source/Lib/EncoderLib/EncSlice.cpp    | 45 +++++++++++++--------------
 source/Lib/EncoderLib/EncSlice.h      |  6 ++--
 6 files changed, 38 insertions(+), 36 deletions(-)

diff --git a/source/App/EncoderApp/EncAppCfg.cpp b/source/App/EncoderApp/EncAppCfg.cpp
index 5b3a060fe..bafb34f22 100644
--- a/source/App/EncoderApp/EncAppCfg.cpp
+++ b/source/App/EncoderApp/EncAppCfg.cpp
@@ -3708,6 +3708,11 @@ bool EncAppCfg::parseCfg( int argc, char* argv[] )
 
   m_maxCuWidth = m_maxCuHeight = m_ctuSize;
 
+#if JVET_AH0078_DPF
+  CHECK(m_bimEnabled && m_dpfEnabled, "DPF is not compatible with BIM");
+  CHECK(m_dpfEnabled && m_resChangeInClvsEnabled, "DPF is not compatible with resolution change in CLVS");
+#endif
+
   // check validity of input parameters
   if( xCheckParameter() )
   {
@@ -5241,7 +5246,6 @@ bool EncAppCfg::xCheckParameter()
       "Either TemporalFilterPastRefs or TemporalFilterFutureRefs must be larger than 0 when Block Importance Mapping is enabled" );
   }
 #if JVET_AH0078_DPF
-  xConfirmPara(m_bimEnabled && m_dpfEnabled, "DPF is not compatible with BIM");
   xConfirmPara(m_dpfKeyLen < 0, "DPF Key Length must be greater than or equal to 0");
   xConfirmPara(m_dpfNonkeyLen < 0, "DPF Non-key Length must be greater than or equal to 0");
 #endif
diff --git a/source/Lib/CommonLib/Rom.h b/source/Lib/CommonLib/Rom.h
index c96131305..31418778a 100644
--- a/source/Lib/CommonLib/Rom.h
+++ b/source/Lib/CommonLib/Rom.h
@@ -226,6 +226,5 @@ extern int16_t   g_weightOffset       [GEO_NUM_PARTITION_MODE][GEO_NUM_CU_SIZE][
 extern int8_t    g_angle2mask         [GEO_NUM_ANGLES];
 extern int8_t    g_dis[GEO_NUM_ANGLES];
 extern int8_t    g_angle2mirror[GEO_NUM_ANGLES];
-
 #endif  //__TCOMROM__
 
diff --git a/source/Lib/EncoderLib/EncLib.h b/source/Lib/EncoderLib/EncLib.h
index 839adc4cc..04b0ff50e 100644
--- a/source/Lib/EncoderLib/EncLib.h
+++ b/source/Lib/EncoderLib/EncLib.h
@@ -141,6 +141,9 @@ private:
   EncTemporalFilter         m_temporalFilter;
   EncTemporalFilter         m_temporalFilterForFG;
   SEINeuralNetworkPostFiltering m_nnPostFiltering;
+#if JVET_AH0078_DPF
+  EncType                   m_encType;
+#endif
 public:
   SPS*                      getSPS( int spsId ) { return m_spsMap.getPS( spsId ); };
   APS**                     getApss() { return m_apss; }
@@ -229,10 +232,6 @@ public:
   bool                   getPltEnc()                      const { return   m_doPlt; }
   void                   checkPltStats( Picture* pic );
 #if JVET_AH0078_DPF
-private:
-  EncType                m_encType;
-
-public:
   EncType                getEncType()          const { return m_encType; }
   void                   setEncType(EncType enctype) { m_encType = enctype; }
 #endif
diff --git a/source/Lib/EncoderLib/EncModeCtrl.cpp b/source/Lib/EncoderLib/EncModeCtrl.cpp
index f892f2cf6..a15085eb8 100644
--- a/source/Lib/EncoderLib/EncModeCtrl.cpp
+++ b/source/Lib/EncoderLib/EncModeCtrl.cpp
@@ -1259,9 +1259,9 @@ void EncModeCtrlMTnoRQT::initCULevel( Partitioner &partitioner, const CodingStru
   if (m_pcEncCfg->getDPF() && encType == ENC_PRE)
   {
     // fix test modes for pre-encoding
-    int width = m_currCsArea->lwidth();
-    int heiht = m_currCsArea->lheight();
-    int sizeCu = m_pcEncCfg->getCTUSize();
+    const int width = m_currCsArea->lwidth();
+    const int heiht = m_currCsArea->lheight();
+    const int sizeCu = m_pcEncCfg->getCTUSize();
     int sizeBlk = BLK_32;
     int maxDepth = floorLog2(sizeCu / sizeBlk);
     while (width % sizeBlk != 0 || heiht % sizeBlk != 0)
@@ -1269,8 +1269,7 @@ void EncModeCtrlMTnoRQT::initCULevel( Partitioner &partitioner, const CodingStru
       maxDepth++;
       sizeBlk >>= 1;
     }
-    int depth = partitioner.currDepth;
-    if (depth < maxDepth)
+    if (partitioner.currDepth < maxDepth)
     {
       for (int qp = maxQP; qp >= minQP; qp--)
       {
diff --git a/source/Lib/EncoderLib/EncSlice.cpp b/source/Lib/EncoderLib/EncSlice.cpp
index 505f07d5c..b1ff2ecde 100644
--- a/source/Lib/EncoderLib/EncSlice.cpp
+++ b/source/Lib/EncoderLib/EncSlice.cpp
@@ -83,10 +83,9 @@ void EncSlice::destroy()
   if (m_pcCfg->getDPF())
   {
     m_lambdaWeight.clear();
-    const int height = m_pcCfg->getSourceHeight();
     if (m_pixelRecDis)
     {
-      for (int i = 0; i < height; i++)
+      for (int i = 0; i < m_maxPicHeight; i++)
       {
         delete[] m_pixelRecDis[i];
         m_pixelRecDis[i] = nullptr;
@@ -96,7 +95,7 @@ void EncSlice::destroy()
     }
     if (m_pixelPredErr)
     {
-      for (int i = 0; i < height; i++)
+      for (int i = 0; i < m_maxPicHeight; i++)
       {
         delete[] m_pixelPredErr[i];
         m_pixelPredErr[i] = nullptr;
@@ -131,14 +130,14 @@ void EncSlice::init( EncLib* pcEncLib, const SPS& sps )
 #if JVET_AH0078_DPF
   if (m_pcCfg->getDPF())
   {
-    const int height = m_pcCfg->getSourceHeight();
-    const int width = m_pcCfg->getSourceWidth();
-    m_pixelPredErr = new int*[height];
-    m_pixelRecDis = new int*[height];
-    for (int i = 0; i < height; i++)
+    m_maxPicHeight = sps.getMaxPicHeightInLumaSamples();
+    m_maxPicWidth = sps.getMaxPicWidthInLumaSamples();
+    m_pixelPredErr = new int*[m_maxPicHeight];
+    m_pixelRecDis = new int*[m_maxPicHeight];
+    for (int i = 0; i < m_maxPicHeight; i++)
     {
-      m_pixelPredErr[i] = new int[width];
-      m_pixelRecDis[i] = new int[width];
+      m_pixelPredErr[i] = new int[m_maxPicWidth];
+      m_pixelRecDis[i] = new int[m_maxPicWidth];
     }
   }
 #endif
@@ -1400,7 +1399,7 @@ void EncSlice::precompressSlice( Picture* pcPic )
 #if JVET_AH0078_DPF
   if (m_pcCfg->getDPF())
   {
-    estLamWt(pcPic);
+    setLambdaWeightByDPF(pcPic);
   }
 #endif
   // if deltaQP RD is not used, simply return
@@ -1927,7 +1926,7 @@ void EncSlice::encodeCtus( Picture* pcPic, const bool bCompressEntireSlice, cons
 #if JVET_AH0078_DPF
     if (m_pcCfg->getDPF() && m_pcLib->getEncType() == ENC_FULL && !pcPic->slices[0]->isIntra())
     {
-      setCTULambdaQp(pTrQuant, ctuIdx, pRdCost, pcSlice);
+      setCTULambdaQpByWeight(ctuIdx, pTrQuant, pRdCost, pcSlice);
     }
 #endif
 
@@ -2069,8 +2068,8 @@ void EncSlice::encodeCtus( Picture* pcPic, const bool bCompressEntireSlice, cons
     if (m_pcCfg->getDPF() && m_pcLib->getEncType() == ENC_PRE)
     {
       // merge clipped pred buffer
-      const int width = m_pcCfg->getSourceWidth();
-      const int height = m_pcCfg->getSourceHeight();
+      const int width = pcPic->getOrigBuf().Y().width;
+      const int height = pcPic->getOrigBuf().Y().height;
       const int clipWidth = std::min((int)pcv.maxCUWidth, width - pos.x);
       const int clipHeight = std::min((int)pcv.maxCUHeight, height - pos.y);
       const UnitArea clipArea(cs.area.chromaFormat, Area(pos.x, pos.y, clipWidth, clipHeight));
@@ -2210,10 +2209,10 @@ double EncSlice::xGetQPValueAccordingToLambda ( double lambda )
 }
 
 #if JVET_AH0078_DPF
-void EncSlice::setCTULambdaQp(TrQuant* pTrQuant, uint32_t ctuIdx, RdCost* pRdCost, Slice* pcSlice)
+void EncSlice::setCTULambdaQpByWeight(uint32_t ctuIdx, TrQuant* pTrQuant, RdCost* pRdCost, Slice* pcSlice)
 {
-  double lambdaCTU = m_lambda * m_lambdaWeight[ctuIdx];
-  int    estQP = (int)(4.3281 * log(lambdaCTU) + 2.1829 + 0.499999);
+  const double lambdaCTU = m_lambda * m_lambdaWeight[ctuIdx];
+  const int    estQP = (int)(4.3281 * log(lambdaCTU) + 2.1829 + 0.499999);  // fitted QP-lambda relationship
   m_qpCtu = estQP;
   pRdCost->setLambda(lambdaCTU, pcSlice->getSPS()->getBitDepths());
 #if WCG_EXT
@@ -2242,7 +2241,7 @@ void EncSlice::setCTULambdaQp(TrQuant* pTrQuant, uint32_t ctuIdx, RdCost* pRdCos
 #endif
 }
 
-void EncSlice::estLamWt(Picture* pcPic)
+void EncSlice::setLambdaWeightByDPF(Picture* pcPic)
 {
   if (pcPic->slices[0]->getSliceType() == I_SLICE)
   {
@@ -2254,8 +2253,8 @@ void EncSlice::estLamWt(Picture* pcPic)
   const int rPOC = curPOC % gopSize;
   const int numPropa = rPOC == 0 ? m_pcCfg->getDPFKeyLen() : m_pcCfg->getDPFNonkeyLen();
 
-  const int width = m_pcCfg->getSourceWidth();
-  const int height = m_pcCfg->getSourceHeight();
+  const int width = pcPic->getOrigBuf().Y().width;
+  const int height = pcPic->getOrigBuf().Y().height;
   const int sizeBlk = BLK_32;
   const int sizeCu = m_pcCfg->getCTUSize();
   const int numBlkWidth = (width + sizeBlk - 1) / sizeBlk;
@@ -2279,9 +2278,9 @@ void EncSlice::estLamWt(Picture* pcPic)
   pcSlice->setSliceQp(qp);
   setUpLambda(pcSlice, lambda, qp);
 
-  const auto m_chromaFormatIdc = m_pcCfg->getChromaFormatIdc();
-  const auto m_area = Area(0, 0, width, height);
-  m_pre.create(m_chromaFormatIdc, m_area, 0);
+  const auto chromaFormatIdc = m_pcCfg->getChromaFormatIdc();
+  const auto area = Area(0, 0, width, height);
+  m_pre.create(chromaFormatIdc, area, 0);
 
   m_pcLib->setEncType(ENC_PRE);
   compressSlice(pcPic, true, m_pcCfg->getFastDeltaQp());  // pre-encoding
diff --git a/source/Lib/EncoderLib/EncSlice.h b/source/Lib/EncoderLib/EncSlice.h
index 945f9ed12..5abc812e9 100644
--- a/source/Lib/EncoderLib/EncSlice.h
+++ b/source/Lib/EncoderLib/EncSlice.h
@@ -156,9 +156,11 @@ private:
   std::vector<double>     m_factorBlk;
   int**                   m_pixelPredErr;
   int**                   m_pixelRecDis;
+  int                     m_maxPicWidth;
+  int                     m_maxPicHeight;
 
-  void    setCTULambdaQp(TrQuant* pTrQuant, uint32_t ctuIdx, RdCost* pRdCost, Slice* pcSlice);
-  void    estLamWt(Picture* pcPic);
+  void    setCTULambdaQpByWeight(uint32_t ctuIdx, TrQuant* pTrQuant, RdCost* pRdCost, Slice* pcSlice);
+  void    setLambdaWeightByDPF(Picture* pcPic);
 
 public:
   int     getQpCtu() const { return m_qpCtu; }
-- 
GitLab