From 8d6e4cd89feeb592ae5dde91f09a4c388deb799c Mon Sep 17 00:00:00 2001
From: Ramin Ghaznavi Youvalari <raminyouvalari@xiaomi.com>
Date: Sat, 15 Jun 2024 01:49:00 +0000
Subject: [PATCH] harmonizing IPM derivation for transform selection/ clean-ups
 (lossless)

---
 source/Lib/CommonLib/InterPrediction.cpp | 29 -------------
 source/Lib/CommonLib/InterPrediction.h   |  3 --
 source/Lib/CommonLib/IntraPrediction.cpp | 52 ++++++------------------
 source/Lib/CommonLib/IntraPrediction.h   |  7 +---
 source/Lib/DecoderLib/DecCu.cpp          | 12 +-----
 source/Lib/EncoderLib/EncCu.cpp          |  2 +-
 source/Lib/EncoderLib/IntraSearch.cpp    |  4 +-
 7 files changed, 19 insertions(+), 90 deletions(-)

diff --git a/source/Lib/CommonLib/InterPrediction.cpp b/source/Lib/CommonLib/InterPrediction.cpp
index 6bdfed33c..69e2a5328 100644
--- a/source/Lib/CommonLib/InterPrediction.cpp
+++ b/source/Lib/CommonLib/InterPrediction.cpp
@@ -907,35 +907,6 @@ void InterPrediction::init( RdCost* pcRdCost, ChromaFormat chromaFormatIDC, cons
 // ====================================================================================================================
 // Public member functions
 // ====================================================================================================================
-#if JVET_AG0061_INTER_LFNST_NSPT
-int InterPrediction::deriveInterDimdMode(const CodingUnit cu, CPelBuf predBuf)
-{
-  const Pel *pPred   = predBuf.buf;
-  const int  iStride = predBuf.stride;
-  int        height  = predBuf.height;
-  int        width   = predBuf.width;
-
-  int piHistogramClean[NUM_LUMA_MODE] = { 0 };
-
-  pPred = pPred + iStride + 1;
-  buildHistogram(pPred, iStride, height - 2, width - 2, piHistogramClean, 0, width - 2, height - 2);
-
-  int firstAmp = 0, curAmp = 0;
-  int firstMode = 0, curMode = 0;
-  for (int i = 0; i < NUM_LUMA_MODE; i++)
-  {
-    curAmp  = piHistogramClean[i];
-    curMode = i;
-    if (curAmp > firstAmp)
-    {
-      firstAmp  = curAmp;
-      firstMode = curMode;
-    }
-  }
-  return firstMode;
-}
-#endif
-
 #if JVET_AC0185_ENHANCED_TEMPORAL_MOTION_DERIVATION 
 bool InterPrediction::xCheckIdenticalMotionSubTMVP(const PredictionUnit &pu)
 {
diff --git a/source/Lib/CommonLib/InterPrediction.h b/source/Lib/CommonLib/InterPrediction.h
index 3058a133e..fdaf9b544 100644
--- a/source/Lib/CommonLib/InterPrediction.h
+++ b/source/Lib/CommonLib/InterPrediction.h
@@ -189,9 +189,6 @@ public:
   bool isMvOOB(const Mv& rcMv, const struct Position pos, const struct Size size, const SPS* sps, const PPS* pps, bool *mcMask, bool *mcMaskChroma, bool lumaOnly = false);
   bool isMvOOBSubBlk(const Mv& rcMv, const struct Position pos, const struct Size size, const SPS* sps, const PPS* pps, bool *mcMask, int mcStride, bool *mcMaskChroma, int mcCStride, bool lumaOnly = false);
 #endif
-#if JVET_AG0061_INTER_LFNST_NSPT
-  static int deriveInterDimdMode(const CodingUnit cu, CPelBuf predBuf);
-#endif
 #if INTER_LIC || (TM_AMVP || TM_MRG || JVET_Z0084_IBC_TM) || JVET_W0090_ARMC_TM || JVET_Z0056_GPM_SPLIT_MODE_REORDERING || JVET_Z0061_TM_OBMC // note: already refactor
   Reshape*          m_pcReshape;
 #endif
diff --git a/source/Lib/CommonLib/IntraPrediction.cpp b/source/Lib/CommonLib/IntraPrediction.cpp
index 57660e122..ad913db4c 100644
--- a/source/Lib/CommonLib/IntraPrediction.cpp
+++ b/source/Lib/CommonLib/IntraPrediction.cpp
@@ -9926,36 +9926,6 @@ void IntraPrediction::ibcCiipBlending(Pel *pDst, int strideDst, const Pel *pSrc0
 #endif
 
 #if ENABLE_DIMD
-
-#if JVET_AC0115_INTRA_TMP_DIMD_MTS_LFNST
-int IntraPrediction::deriveDimdIntraTmpModePred(const CodingUnit cu, CPelBuf predBuf)
-{
-  const Pel* pPred = predBuf.buf;
-  const int iStride = predBuf.stride;
-  int height = predBuf.height;
-  int width = predBuf.width;
-
-  int piHistogramClean[NUM_LUMA_MODE] = { 0 };
-
-  pPred = pPred + iStride + 1;
-  buildHistogram(pPred, iStride, height - 2, width - 2, piHistogramClean, 0, width - 2, height - 2);
-
-  int firstAmp = 0, curAmp = 0;
-  int firstMode = 0, curMode = 0;
-  for (int i = 0; i < NUM_LUMA_MODE; i++)
-  {
-    curAmp = piHistogramClean[i];
-    curMode = i;
-    if (curAmp > firstAmp)
-    {
-      firstAmp = curAmp;
-      firstMode = curMode;
-    }
-  }
-  return firstMode;
-}
-#endif
-
 void IntraPrediction::deriveDimdMode(const CPelBuf &recoBuf, const CompArea &area, CodingUnit &cu)
 {
   if( !cu.slice->getSPS()->getUseDimd() )
@@ -11493,15 +11463,17 @@ void IntraPrediction::predChromaTM(const CompArea &areaCb, const CompArea &areaC
 }
 #endif
 
-#if JVET_AB0067_MIP_DIMD_LFNST && ENABLE_DIMD
-int IntraPrediction::deriveDimdMipMode(PelBuf& reducedPred, int width, int height, CodingUnit& cu)
+#if ENABLE_DIMD && (JVET_AB0067_MIP_DIMD_LFNST || JVET_AC0115_INTRA_TMP_DIMD_MTS_LFNST || JVET_AG0058_EIP || JVET_AG0061_INTER_LFNST_NSPT)
+int IntraPrediction::deriveIpmForTransform(CPelBuf predBuf, CodingUnit& cu)
 {
   if (!cu.slice->getSPS()->getUseDimd())
   {
     return PLANAR_IDX;
   }
-  const Pel* pPred = reducedPred.buf;
-  const int iStride = reducedPred.stride;
+  const Pel* pPred = predBuf.buf;
+  const int iStride = predBuf.stride;
+  const int width = predBuf.width;
+  const int height = predBuf.height;
 
   int histogram[NUM_LUMA_MODE] = { 0 };
 
@@ -13549,7 +13521,7 @@ void IntraPrediction::predIntraMip( const ComponentID compId, PelBuf &piPred, co
       }
       pReducePred += reducedPredTemp.stride;
     }
-    int iMode = deriveDimdMipMode(reducedPredTemp, reducedPredSize, reducedPredSize, *pu.cu);
+    int iMode = deriveIpmForTransform(reducedPredTemp, *pu.cu);
     pu.cu->mipDimdMode = iMode;
   }
   else
@@ -16661,7 +16633,7 @@ void IntraPrediction::xTMPFusionApplyModel(PelBuf &piPred, unsigned int uiBlkWid
   if (bDeriveDimdMode)
   {
     CPelBuf predBuf      = piPred;
-    cu->intraTmpDimdMode = deriveDimdIntraTmpModePred(*cu, predBuf);
+    cu->intraTmpDimdMode = deriveIpmForTransform(predBuf, *cu);
   }
 #endif
   return;
@@ -16713,7 +16685,7 @@ void IntraPrediction::xGenerateTmpFlmPred(PelBuf &piPred, unsigned int uiBlkWidt
   if (bDeriveDimdMode)
   {
     CPelBuf predBuf      = piPred;
-    cu->intraTmpDimdMode = deriveDimdIntraTmpModePred(*cu, predBuf);
+    cu->intraTmpDimdMode = deriveIpmForTransform(predBuf, *cu);
   }
 #endif
   return;
@@ -16915,7 +16887,7 @@ bool IntraPrediction::generateTMPrediction( Pel* piPred, unsigned int uiStride,
 
 #if JVET_AC0115_INTRA_TMP_DIMD_MTS_LFNST
   CPelBuf predBuf(pPred, uiStride, uiWidth, uiHeight);
-  cu->intraTmpDimdMode = deriveDimdIntraTmpModePred(*cu, predBuf);
+  cu->intraTmpDimdMode = deriveIpmForTransform(predBuf, *cu);
 #endif
   return bSucceedFlag;
 }
@@ -17277,7 +17249,7 @@ bool IntraPrediction::generateTMPrediction(Pel *piPred, unsigned int uiStride, i
   {
 #endif
     CPelBuf predBuf(pPred, uiStride, uiWidth, uiHeight);
-    pu.cu->intraTmpDimdMode = deriveDimdIntraTmpModePred(*pu.cu, predBuf);
+    pu.cu->intraTmpDimdMode = deriveIpmForTransform(predBuf, *pu.cu);
 #if JVET_AD0086_ENHANCED_INTRA_TMP
   }
 #endif
@@ -17692,7 +17664,7 @@ bool IntraPrediction::generateTmDcPrediction( Pel* piPred, unsigned int uiStride
   }
 #if JVET_AC0115_INTRA_TMP_DIMD_MTS_LFNST
   CPelBuf predBuf(pPred, uiStride, uiBlkWidth, uiBlkHeight);
-  cu->intraTmpDimdMode = deriveDimdIntraTmpModePred(*cu, predBuf);
+  cu->intraTmpDimdMode = deriveIpmForTransform(predBuf, *cu);
 #endif
   return bSucceedFlag;
 }
diff --git a/source/Lib/CommonLib/IntraPrediction.h b/source/Lib/CommonLib/IntraPrediction.h
index d65a787ea..faa92c107 100644
--- a/source/Lib/CommonLib/IntraPrediction.h
+++ b/source/Lib/CommonLib/IntraPrediction.h
@@ -790,9 +790,6 @@ public:
 #endif
 
 #if ENABLE_DIMD
-#if JVET_AC0115_INTRA_TMP_DIMD_MTS_LFNST
-  static int deriveDimdIntraTmpModePred(const CodingUnit cu, CPelBuf predBuf); // using prediction samples
-#endif
   static void deriveDimdMode      (const CPelBuf &recoBuf, const CompArea &area, CodingUnit &cu);
 #if JVET_Z0050_DIMD_CHROMA_FUSION && ENABLE_DIMD
   static void deriveDimdChromaMode(const CPelBuf &recoBufY, const CPelBuf &recoBufCb, const CPelBuf &recoBufCr, const CompArea &areaY, const CompArea &areaCb, const CompArea &areaCr, CodingUnit &cu);
@@ -803,8 +800,8 @@ public:
    void predChromaTM(const CompArea &areaCb, const CompArea &areaCr, PredictionUnit &pu, uint8_t predMode, PelBuf predCb, PelBuf predCr, TemplateType eTplType, InterPrediction *pcInterPred);
 #endif
 #endif
-#if JVET_AB0067_MIP_DIMD_LFNST && ENABLE_DIMD
-  static int deriveDimdMipMode(PelBuf& reducedPred, int width, int height, CodingUnit& cu);
+#if ENABLE_DIMD && (JVET_AB0067_MIP_DIMD_LFNST || JVET_AC0115_INTRA_TMP_DIMD_MTS_LFNST || JVET_AG0058_EIP || JVET_AG0061_INTER_LFNST_NSPT)
+  static int deriveIpmForTransform(CPelBuf predBuf, CodingUnit& cu);
 #endif
 #if !JVET_AG0061_INTER_LFNST_NSPT
   static int  buildHistogram      ( const Pel *pReco, int iStride, uint32_t uiHeight, uint32_t uiWidth, int* piHistogram, int direction, int bw, int bh );
diff --git a/source/Lib/DecoderLib/DecCu.cpp b/source/Lib/DecoderLib/DecCu.cpp
index b9581755d..90fb33183 100644
--- a/source/Lib/DecoderLib/DecCu.cpp
+++ b/source/Lib/DecoderLib/DecCu.cpp
@@ -1194,15 +1194,7 @@ void DecCu::xIntraRecBlk( TransformUnit& tu, const ComponentID compID )
       m_pcIntraPred->eipPred(pu, piPred);
       if (!isEncoder)
       {
-        pu.cu->eipModel.eipDimdMode = IntraPrediction::deriveDimdMipMode(piPred, piPred.width, piPred.height, *pu.cu);
-
-        // const CodingUnit& cu = *pu.cu;
-        // printf("pos(%d, %d), size(%d, %d), merge(%d), ", cu.lx(), cu.ly(), cu.lwidth(), cu.lheight(), int(cu.eipMerge));
-        // for (int i = 0; i < 15; i++)
-        // {
-        // printf("%d ", int(cu.eipModel.params[i]));
-        // }
-        // printf("\n");
+        pu.cu->eipModel.eipDimdMode = IntraPrediction::deriveIpmForTransform(piPred, *pu.cu);
       }
     }
 #endif
@@ -2657,7 +2649,7 @@ void DecCu::xDecodeInterTexture(CodingUnit &cu)
 #if JVET_AG0061_INTER_LFNST_NSPT
   if (cu.lfnstIdx)
   {
-    cu.dimdDerivedIntraDir = m_pcInterPred->deriveInterDimdMode(cu, cu.cs->getPredBuf(*cu.firstPU).Y());
+    cu.dimdDerivedIntraDir = m_pcIntraPred->deriveIpmForTransform(cu.cs->getPredBuf(*cu.firstPU).Y(), cu);
   }
 #endif
 
diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp
index 02a76ca07..bca42b840 100644
--- a/source/Lib/EncoderLib/EncCu.cpp
+++ b/source/Lib/EncoderLib/EncCu.cpp
@@ -22236,7 +22236,7 @@ void EncCu::xEncodeInterResidual(   CodingStructure *&tempCS
           bestCost = bestCS->cost;
           cu       = tempCS->getCU(partitioner.chType);
         }
-        cu->dimdDerivedIntraDir = m_pcInterSearch->deriveInterDimdMode(*cu, cu->cs->getPredBuf(*cu->firstPU).Y());
+        cu->dimdDerivedIntraDir = m_pcIntraSearch->deriveIpmForTransform(cu->cs->getPredBuf(*cu->firstPU).Y(), *cu);
         // we need to restart the distortion for the new tempCS, the bit count and the cost
         tempCS->dist         = 0;
         tempCS->fracBits     = 0;
diff --git a/source/Lib/EncoderLib/IntraSearch.cpp b/source/Lib/EncoderLib/IntraSearch.cpp
index 9154bf493..309f917a4 100644
--- a/source/Lib/EncoderLib/IntraSearch.cpp
+++ b/source/Lib/EncoderLib/IntraSearch.cpp
@@ -2754,13 +2754,13 @@ bool IntraSearch::estIntraPredLumaQT(CodingUnit &cu, Partitioner &partitioner, c
                 if(modeInfo.mipTrFlg)
                 {
                   PelBuf eipSaveBuf(m_eipMergePredBuf[modeIdx], pu.Y());
-                  m_eipMergeModel[modeIdx].eipDimdMode = deriveDimdMipMode(eipSaveBuf, piPred.width, piPred.height, cu);
+                  m_eipMergeModel[modeIdx].eipDimdMode = deriveIpmForTransform(eipSaveBuf, cu);
                   CHECK(modeIdx >= NUM_EIP_MERGE_SIGNAL, "modeIdx >= NUM_EIP_MERGE_SIGNAL");
                 }
                 else
                 {
                   PelBuf eipSaveBuf(m_eipPredBuf[modeIdx], pu.Y());
-                  m_eipModel[modeIdx].eipDimdMode = deriveDimdMipMode(eipSaveBuf, piPred.width, piPred.height, cu);
+                  m_eipModel[modeIdx].eipDimdMode = deriveIpmForTransform(eipSaveBuf, cu);
                   CHECK(modeIdx >= NUM_DERIVED_EIP, "modeIdx >= NUM_DERIVED_EIP");
                 }
               }
-- 
GitLab