From 14f8598c967a2d5a7e66783f42f88df6d80d95a9 Mon Sep 17 00:00:00 2001
From: Adam Wieckowski <adam.wieckowski@hhi.fraunhofer.de>
Date: Thu, 14 Mar 2019 16:12:48 +0100
Subject: [PATCH] fixes to ENABLE_SPLIT_PARALLELISM

---
 source/Lib/CommonLib/Picture.cpp      | 32 ++++++++++++---
 source/Lib/CommonLib/RdCost.cpp       |  4 ++
 source/Lib/CommonLib/Reshape.h        |  4 ++
 source/Lib/CommonLib/TypeDef.h        |  4 +-
 source/Lib/EncoderLib/EncCu.cpp       | 28 ++++++++-----
 source/Lib/EncoderLib/EncLib.cpp      |  7 ++--
 source/Lib/EncoderLib/EncModeCtrl.cpp | 57 +++++++++++----------------
 source/Lib/EncoderLib/EncSlice.cpp    |  7 ++++
 8 files changed, 88 insertions(+), 55 deletions(-)

diff --git a/source/Lib/CommonLib/Picture.cpp b/source/Lib/CommonLib/Picture.cpp
index 01380ecb7..4fd8f73d7 100644
--- a/source/Lib/CommonLib/Picture.cpp
+++ b/source/Lib/CommonLib/Picture.cpp
@@ -1003,6 +1003,8 @@ void Picture::finishParallelPart( const UnitArea& area )
     const int destID = scheduler.getSplitPicId( tId );
 
     M_BUFS( destID, PIC_RECONSTRUCTION ).subBuf( clipdArea ).copyFrom( M_BUFS( sourceID, PIC_RECONSTRUCTION ).subBuf( clipdArea ) );
+    M_BUFS( destID, PIC_PREDICTION     )                    .copyFrom( M_BUFS( sourceID, PIC_PREDICTION     )                     );
+    M_BUFS( destID, PIC_RESIDUAL       )                    .copyFrom( M_BUFS( sourceID, PIC_RESIDUAL       )                     );
   }
 }
 
@@ -1088,12 +1090,20 @@ void Picture::extendPicBorder()
 
 PelBuf Picture::getBuf( const ComponentID compID, const PictureType &type )
 {
-  return M_BUFS( type == PIC_ORIGINAL ? 0 : scheduler.getSplitPicId(), type ).getBuf( compID );
+#if JVET_M0427_INLOOP_RESHAPER
+  return M_BUFS( ( type == PIC_ORIGINAL || type == PIC_TRUE_ORIGINAL ) ? 0 : scheduler.getSplitPicId(), type ).getBuf( compID );
+#else
+  return M_BUFS( ( type == PIC_ORIGINAL ) ? 0 : scheduler.getSplitPicId(), type ).getBuf( compID );
+#endif
 }
 
 const CPelBuf Picture::getBuf( const ComponentID compID, const PictureType &type ) const
 {
-  return M_BUFS( type == PIC_ORIGINAL ? 0 : scheduler.getSplitPicId(), type ).getBuf( compID );
+#if JVET_M0427_INLOOP_RESHAPER
+  return M_BUFS( ( type == PIC_ORIGINAL || type == PIC_TRUE_ORIGINAL ) ? 0 : scheduler.getSplitPicId(), type ).getBuf( compID );
+#else
+  return M_BUFS( ( type == PIC_ORIGINAL ) ? 0 : scheduler.getSplitPicId(), type ).getBuf( compID );
+#endif
 }
 
 PelBuf Picture::getBuf( const CompArea &blk, const PictureType &type )
@@ -1104,7 +1114,11 @@ PelBuf Picture::getBuf( const CompArea &blk, const PictureType &type )
   }
 
 #if ENABLE_SPLIT_PARALLELISM
-  const int jId = type == PIC_ORIGINAL ? 0 : scheduler.getSplitPicId();
+#if JVET_M0427_INLOOP_RESHAPER
+  const int jId = ( type == PIC_ORIGINAL || type == PIC_TRUE_ORIGINAL ) ? 0 : scheduler.getSplitPicId();
+#else
+  const int jId = ( type == PIC_ORIGINAL ) ? 0 : scheduler.getSplitPicId();
+#endif
 
 #endif
 #if !KEEP_PRED_AND_RESI_SIGNALS
@@ -1129,7 +1143,11 @@ const CPelBuf Picture::getBuf( const CompArea &blk, const PictureType &type ) co
   }
 
 #if ENABLE_SPLIT_PARALLELISM
-  const int jId = type == PIC_ORIGINAL ? 0 : scheduler.getSplitPicId();
+#if JVET_M0427_INLOOP_RESHAPER
+  const int jId = ( type == PIC_ORIGINAL || type == PIC_TRUE_ORIGINAL ) ? 0 : scheduler.getSplitPicId();
+#else
+  const int jId = ( type == PIC_ORIGINAL ) ? 0 : scheduler.getSplitPicId();
+#endif
 
 #endif
 #if !KEEP_PRED_AND_RESI_SIGNALS
@@ -1173,7 +1191,11 @@ const CPelUnitBuf Picture::getBuf( const UnitArea &unit, const PictureType &type
 Pel* Picture::getOrigin( const PictureType &type, const ComponentID compID ) const
 {
 #if ENABLE_SPLIT_PARALLELISM
-  const int jId = type == PIC_ORIGINAL ? 0 : scheduler.getSplitPicId();
+#if JVET_M0427_INLOOP_RESHAPER
+  const int jId = ( type == PIC_ORIGINAL || type == PIC_TRUE_ORIGINAL ) ? 0 : scheduler.getSplitPicId();
+#else
+  const int jId = ( type == PIC_ORIGINAL ) ? 0 : scheduler.getSplitPicId();
+#endif
 #endif
   return M_BUFS( jId, type ).getOrigin( compID );
 
diff --git a/source/Lib/CommonLib/RdCost.cpp b/source/Lib/CommonLib/RdCost.cpp
index e116ad724..cac3f5978 100644
--- a/source/Lib/CommonLib/RdCost.cpp
+++ b/source/Lib/CommonLib/RdCost.cpp
@@ -191,6 +191,10 @@ void RdCost::copyState( const RdCost& other )
   m_motionLambda  = other.m_motionLambda;
   m_iCostScale    = other.m_iCostScale;
   memcpy( m_dLambdaMotionSAD, other.m_dLambdaMotionSAD, sizeof( m_dLambdaMotionSAD ) );
+#if WCG_EXT
+  m_dLambda_unadjusted  = other.m_dLambda_unadjusted ;
+  m_DistScaleUnadjusted = other.m_DistScaleUnadjusted;
+#endif
 }
 #endif
 
diff --git a/source/Lib/CommonLib/Reshape.h b/source/Lib/CommonLib/Reshape.h
index 64aa62f39..26b1f86c0 100644
--- a/source/Lib/CommonLib/Reshape.h
+++ b/source/Lib/CommonLib/Reshape.h
@@ -69,7 +69,11 @@ protected:
   int                     m_reshapeLUTSize;
 public:
   Reshape();
+#if ENABLE_SPLIT_PARALLELISM
+  virtual ~Reshape();
+#else
   ~Reshape();
+#endif
 
   void createDec(int bitDepth);
   void destroy();
diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index 2ecd2395d..b09ad5b63 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -81,7 +81,7 @@
 #define FIX_DB_MAX_TRANSFORM_SIZE                         1
 #define JVET_M0908_CIIP_DB                                1
 #define JVET_M0471_LONG_DEBLOCKING_FILTERS                1
-#define JVET_M0427_INLOOP_RESHAPER                        0
+#define JVET_M0427_INLOOP_RESHAPER                        1
 #define JVET_M0470                                        1 // Fixed GR/TU+EG-k transition point, use limited prefix length for escape codes
 
 #define JVET_M0253_HASH_ME                                1
@@ -185,7 +185,7 @@ typedef std::pair<int, int>  TrCost;
 
 #define ENABLE_JVET_L0283_MRL                             1 // 1: Enable MRL, 0: Disable MRL
 #define JVET_L0090_PAIR_AVG                               1 // Add pairwise average candidates, replace HEVC combined candidates
-#define REUSE_CU_RESULTS                                  0
+#define REUSE_CU_RESULTS                                  1
 #if REUSE_CU_RESULTS && JVET_M0102_INTRA_SUBPARTITIONS
 #define REUSE_CU_RESULTS_WITH_MULTIPLE_TUS                1
 #define MAX_NUM_TUS                                       4
diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp
index 5669fcaf8..1ec435fdf 100644
--- a/source/Lib/EncoderLib/EncCu.cpp
+++ b/source/Lib/EncoderLib/EncCu.cpp
@@ -327,7 +327,9 @@ void EncCu::compressCtu( CodingStructure& cs, const UnitArea& area, const unsign
 #if REUSE_CU_RESULTS
       BestEncInfoCache* bestCache = dynamic_cast< BestEncInfoCache* >( jobEncCu->m_modeCtrl );
 #endif
+#if JVET_M0140_SBT
       SaveLoadEncInfoSbt *sbtCache = dynamic_cast< SaveLoadEncInfoSbt* >( jobEncCu->m_modeCtrl );
+#endif
       if( cacheCtrl )
       {
         cacheCtrl->init( *cs.slice );
@@ -338,10 +340,12 @@ void EncCu::compressCtu( CodingStructure& cs, const UnitArea& area, const unsign
         bestCache->init(*cs.slice);
       }
 #endif
+#if JVET_M0140_SBT
       if (sbtCache)
       {
         sbtCache->init(*cs.slice);
       }
+#endif
     }
   }
 
@@ -1065,16 +1069,9 @@ void EncCu::xCompressCUParallel( CodingStructure *&tempCS, CodingStructure *&bes
     jobPartitioner->copyState( partitioner );
     jobCuEnc      ->copyState( this, *jobPartitioner, currArea, true );
 
-    if( jobBlkCache )
-    {
-      jobBlkCache->tick();
-    }
-
+    if( jobBlkCache  ) { jobBlkCache ->tick(); }
 #if REUSE_CU_RESULTS
-    if( jobBestCache )
-    {
-      jobBestCache->tick();
-    }
+    if( jobBestCache ) { jobBestCache->tick(); }
 
 #endif
     CodingStructure *&jobBest = jobCuEnc->m_pBestCS[wIdx][hIdx];
@@ -1167,7 +1164,11 @@ void EncCu::copyState( EncCu* other, Partitioner& partitioner, const UnitArea& c
     const CodingStructure* src = other->m_pBestCS[wIdx][hIdx];
     bool keepResi = KEEP_PRED_AND_RESI_SIGNALS;
 
+#if JVET_M0427_INLOOP_RESHAPER
+    dst->useSubStructure( *src, partitioner.chType, currArea, true, true, keepResi, true );
+#else
     dst->useSubStructure( *src, partitioner.chType, currArea, KEEP_PRED_AND_RESI_SIGNALS, true, keepResi, keepResi );
+#endif
     dst->cost           =  src->cost;
     dst->dist           =  src->dist;
     dst->fracBits       =  src->fracBits;
@@ -1183,7 +1184,14 @@ void EncCu::copyState( EncCu* other, Partitioner& partitioner, const UnitArea& c
   m_modeCtrl     ->copyState( *other->m_modeCtrl, partitioner.currArea() );
   m_pcRdCost     ->copyState( *other->m_pcRdCost );
   m_pcTrQuant    ->copyState( *other->m_pcTrQuant );
-  //m_pcReshape    ->copyState( *other->m_pcReshape );
+#if JVET_M0427_INLOOP_RESHAPER
+  if( m_pcEncCfg->getReshaper() )
+  {
+    EncReshape *encReshapeThis  = dynamic_cast<EncReshape*>(       m_pcReshape);
+    EncReshape *encReshapeOther = dynamic_cast<EncReshape*>(other->m_pcReshape);
+    encReshapeThis->copyState( *encReshapeOther );
+  }
+#endif
 
   m_CABACEstimator->getCtx() = other->m_CABACEstimator->getCtx();
 }
diff --git a/source/Lib/EncoderLib/EncLib.cpp b/source/Lib/EncoderLib/EncLib.cpp
index ed5e137c1..1b6579b04 100644
--- a/source/Lib/EncoderLib/EncLib.cpp
+++ b/source/Lib/EncoderLib/EncLib.cpp
@@ -145,11 +145,12 @@ void EncLib::create ()
   }
 
 #if JVET_M0427_INLOOP_RESHAPER
+#if ENABLE_SPLIT_PARALLELISM || ENABLE_WPP_PARALLELISM
+  m_cReshaper = new EncReshape[m_numCuEncStacks];
+#endif
   if (m_lumaReshapeEnable)
   {
 #if ENABLE_SPLIT_PARALLELISM || ENABLE_WPP_PARALLELISM
-    m_cReshaper = new EncReshape[m_numCuEncStacks];
-
     for (int jId = 0; jId < m_numCuEncStacks; jId++)
     {
       m_cReshaper[jId].createEnc(getSourceWidth(), getSourceHeight(), m_maxCUWidth, m_maxCUHeight, m_bitDepth[COMPONENT_Y]);
@@ -628,7 +629,7 @@ void EncLib::encode( bool flush, PelStorage* pcPicYuvOrg, PelStorage* cPicYuvTru
 
       pcPicCurr->M_BUFS( 0, PIC_ORIGINAL ).swap( *pcPicYuvOrg );
 #if JVET_M0427_INLOOP_RESHAPER
-      pcPicCurr->M_BUFS( 0, PIC_TRUE_ORIGINAL).swap(*cPicYuvTrueOrg);
+      pcPicCurr->M_BUFS( 0, PIC_TRUE_ORIGINAL ).swap(*cPicYuvTrueOrg );
 #endif
 
 #if JVET_M0132_APS
diff --git a/source/Lib/EncoderLib/EncModeCtrl.cpp b/source/Lib/EncoderLib/EncModeCtrl.cpp
index 557e145ba..560990829 100644
--- a/source/Lib/EncoderLib/EncModeCtrl.cpp
+++ b/source/Lib/EncoderLib/EncModeCtrl.cpp
@@ -1076,9 +1076,9 @@ bool BestEncInfoCache::setCsFrom( CodingStructure& cs, EncTestMode& testMode, co
 #if ENABLE_SPLIT_PARALLELISM
 void BestEncInfoCache::copyState(const BestEncInfoCache &other, const UnitArea &area)
 {
-  m_slice_bencinf = other.m_slice_bencinf;
-
+  m_slice_bencinf  = other.m_slice_bencinf;
   m_currTemporalId = other.m_currTemporalId;
+#if 0
 
   if( m_slice_bencinf->isIntra() ) return;
 
@@ -1124,6 +1124,7 @@ void BestEncInfoCache::copyState(const BestEncInfoCache &other, const UnitArea &
       }
     }
   }
+#endif
 }
 
 void BestEncInfoCache::touch(const UnitArea &area)
@@ -1132,7 +1133,7 @@ void BestEncInfoCache::touch(const UnitArea &area)
   getAreaIdx(area.Y(), *m_slice_bencinf->getPPS()->pcv, idx1, idx2, idx3, idx4);
   BestEncodingInfo &encInfo = *m_bestEncInfo[idx1][idx2][idx3][idx4];
 
-  encInfo.temporalId   = m_currTemporalId;
+  encInfo.temporalId = m_currTemporalId;
 }
 
 #endif
@@ -2190,38 +2191,40 @@ void EncModeCtrlMTnoRQT::copyState( const EncModeCtrl& other, const UnitArea& ar
 #if REUSE_CU_RESULTS
   this->BestEncInfoCache   ::copyState( *pOther, area );
 #endif
+#if JVET_M0140_SBT
   this->SaveLoadEncInfoSbt ::copyState( *pOther );
+#endif
 
   m_skipThreshold = pOther->m_skipThreshold;
 }
 
 int EncModeCtrlMTnoRQT::getNumParallelJobs( const CodingStructure &cs, Partitioner& partitioner ) const
 {
-  int numJobs = 1; // for no-split coding
+  int numJobs = 0;
 
-  if( partitioner.canSplit( CU_QUAD_SPLIT, cs ) )
+  if(      partitioner.canSplit( CU_TRIH_SPLIT, cs ) )
   {
-    numJobs = 2;
+    numJobs = 6;
   }
-
-  if( partitioner.canSplit( CU_VERT_SPLIT, cs ) )
+  else if( partitioner.canSplit( CU_TRIV_SPLIT, cs ) )
   {
-    numJobs = 3;
+    numJobs = 5;
   }
-
-  if( partitioner.canSplit( CU_HORZ_SPLIT, cs ) )
+  else if( partitioner.canSplit( CU_HORZ_SPLIT, cs ) )
   {
     numJobs = 4;
   }
-
-  if( partitioner.canSplit( CU_TRIV_SPLIT, cs ) )
+  else if (partitioner.canSplit(CU_VERT_SPLIT, cs))
   {
-    numJobs = 5;
+    numJobs = 3;
   }
-
-  if( partitioner.canSplit( CU_TRIH_SPLIT, cs ) )
+  else if( partitioner.canSplit( CU_QUAD_SPLIT, cs ) )
   {
-    numJobs = 6;
+    numJobs = 2;
+  }
+  else if( partitioner.canSplit( CU_DONT_SPLIT, cs ) )
+  {
+    numJobs = 1;
   }
 
   CHECK( numJobs >= NUM_RESERVERD_SPLIT_JOBS, "More jobs specified than allowed" );
@@ -2259,26 +2262,10 @@ bool EncModeCtrlMTnoRQT::parallelJobSelector( const EncTestMode& encTestmode, co
     return encTestmode.type == ETM_SPLIT_QT;
     break;
   case 3:
-    switch( encTestmode.type )
-    {
-    case ETM_SPLIT_BT_V:
-      return true;
-      break;
-    default:
-      return false;
-      break;
-    }
+    return encTestmode.type == ETM_SPLIT_BT_V;
     break;
   case 4:
-    switch( encTestmode.type )
-    {
-    case ETM_SPLIT_BT_H:
-      return true;
-      break;
-    default:
-      return false;
-      break;
-    }
+    return encTestmode.type == ETM_SPLIT_BT_H;
     break;
   case 5:
     return encTestmode.type == ETM_SPLIT_TT_V;
diff --git a/source/Lib/EncoderLib/EncSlice.cpp b/source/Lib/EncoderLib/EncSlice.cpp
index 674e3a10f..f2186981f 100644
--- a/source/Lib/EncoderLib/EncSlice.cpp
+++ b/source/Lib/EncoderLib/EncSlice.cpp
@@ -1829,6 +1829,13 @@ void EncSlice::encodeCtus( Picture* pcPic, const bool bCompressEntireSlice, cons
     if (pcSlice->getSPS()->getUseReshaper())
     {
       m_pcCuEncoder->setDecCuReshaperInEncCU(m_pcLib->getReshaper(), pcSlice->getSPS()->getChromaFormatIdc());
+
+#if ENABLE_SPLIT_PARALLELISM || ENABLE_WPP_PARALLELISM
+      for (int jId = 1; jId < m_pcLib->getNumCuEncStacks(); jId++)
+      {
+        m_pcLib->getCuEncoder(jId)->setDecCuReshaperInEncCU(m_pcLib->getReshaper(jId), pcSlice->getSPS()->getChromaFormatIdc());
+      }
+#endif
     }
 #endif
 #if JVET_M0445_MCTS
-- 
GitLab