From ea32bf6fc63babec3a7736a6ce9debd5a2e1669d Mon Sep 17 00:00:00 2001
From: hanhuang <hanhuang@qti.qualcomm.com>
Date: Fri, 13 Mar 2020 14:06:09 -0700
Subject: [PATCH] Fix for JVET_Q0471_CHROMA_QT_SPLIT

---
 source/Lib/CommonLib/UnitPartitioner.cpp | 9 +++++++--
 source/Lib/EncoderLib/EncCu.cpp          | 5 +++++
 source/Lib/EncoderLib/EncModeCtrl.cpp    | 9 +++++++++
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/source/Lib/CommonLib/UnitPartitioner.cpp b/source/Lib/CommonLib/UnitPartitioner.cpp
index 81b0469a..6dc3e785 100644
--- a/source/Lib/CommonLib/UnitPartitioner.cpp
+++ b/source/Lib/CommonLib/UnitPartitioner.cpp
@@ -166,7 +166,12 @@ void Partitioner::copyState( const Partitioner& other )
 void AdaptiveDepthPartitioner::setMaxMinDepth( unsigned& minDepth, unsigned& maxDepth, const CodingStructure& cs ) const
 {
   unsigned          stdMinDepth = 0;
+#if JVET_Q0471_CHROMA_QT_SPLIT
+  const unsigned    minQTSize  = cs.sps->getMinQTSize(cs.slice->getSliceType(), chType) >> ((currArea().Y().chromaFormat == CHROMA_400) ? 0 : ((int)getChannelTypeScaleX(chType, currArea().Y().chromaFormat) - (int)getChannelTypeScaleY(chType, currArea().Y().chromaFormat)));
+  unsigned          stdMaxDepth = (floorLog2(cs.sps->getCTUSize()) - floorLog2(minQTSize));
+#else
   unsigned          stdMaxDepth = ( floorLog2(cs.sps->getCTUSize()) - floorLog2(cs.sps->getMinQTSize( cs.slice->getSliceType(), chType )));
+#endif
   const Position    pos         = currArea().blocks[chType].pos();
   const unsigned    curSliceIdx = cs.slice->getIndependentSliceIdx();
   const unsigned    curTileIdx  = cs.pps->getTileIdx( currArea().lumaPos() );
@@ -392,7 +397,7 @@ void QTBTPartitioner::canSplit( const CodingStructure &cs, bool& canNo, bool& ca
   if( lastSplit != CTU_LEVEL && lastSplit != CU_QUAD_SPLIT ) canQt = false;
 #if JVET_Q0471_CHROMA_QT_SPLIT
   // minQtSize is in luma samples unit
-  const unsigned minQTThreshold = minQtSize >> ((area.chromaFormat == CHROMA_400) ? 0 : ((int) getChannelTypeScaleX(CHANNEL_TYPE_CHROMA, area.chromaFormat) - (int) getChannelTypeScaleY(CHANNEL_TYPE_CHROMA, area.chromaFormat)));
+  const unsigned minQTThreshold = minQtSize >> ((area.chromaFormat == CHROMA_400) ? 0 : ((int)getChannelTypeScaleX(chType, area.chromaFormat) - (int)getChannelTypeScaleY(chType, area.chromaFormat)));
   if( area.width <= minQTThreshold )                         canQt = false;
 #else
   if( area.width <= minQtSize )                              canQt = false;
@@ -572,7 +577,7 @@ PartSplit QTBTPartitioner::getImplicitSplit( const CodingStructure &cs )
     const unsigned minQtSize  = cs.pcv->getMinQtSize( *cs.slice, chType );
 #if JVET_Q0471_CHROMA_QT_SPLIT
     // minQtSize is in luma samples unit
-    const unsigned minQTThreshold = minQtSize >> ((area.chromaFormat == CHROMA_400) ? 0 : ((int) getChannelTypeScaleX(CHANNEL_TYPE_CHROMA, area.chromaFormat) - (int) getChannelTypeScaleY(CHANNEL_TYPE_CHROMA, area.chromaFormat)));
+    const unsigned minQTThreshold = minQtSize >> ((area.chromaFormat == CHROMA_400) ? 0 : ((int)getChannelTypeScaleX(chType, area.chromaFormat) - (int)getChannelTypeScaleY(chType, area.chromaFormat)));
     const bool isQtAllowed    = area.width > minQTThreshold && currBtDepth == 0;
 #else
     const bool isQtAllowed    = area.width >  minQtSize && area.height >  minQtSize && currBtDepth == 0;
diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp
index c5cce5f9..b9c02067 100644
--- a/source/Lib/EncoderLib/EncCu.cpp
+++ b/source/Lib/EncoderLib/EncCu.cpp
@@ -1597,7 +1597,12 @@ void EncCu::xCheckModeSplit(CodingStructure *&tempCS, CodingStructure *&bestCS,
     if( m_pcEncCfg->getUseFastLCTU() )
     {
       unsigned minDepth = 0;
+#if JVET_Q0471_CHROMA_QT_SPLIT
+      const unsigned    minQTSize = tempCS->sps->getMinQTSize(slice.getSliceType(), partitioner.chType) >> ((tempCS->area.Y().chromaFormat == CHROMA_400) ? 0 : ((int)getChannelTypeScaleX(partitioner.chType, tempCS->area.Y().chromaFormat) - (int)getChannelTypeScaleY(partitioner.chType, tempCS->area.Y().chromaFormat)));
+      unsigned maxDepth = floorLog2(tempCS->sps->getCTUSize()) - floorLog2(minQTSize);
+#else
       unsigned maxDepth = floorLog2(tempCS->sps->getCTUSize()) - floorLog2(tempCS->sps->getMinQTSize(slice.getSliceType(), partitioner.chType));
+#endif
 
       if( auto ad = dynamic_cast<AdaptiveDepthPartitioner*>( &partitioner ) )
       {
diff --git a/source/Lib/EncoderLib/EncModeCtrl.cpp b/source/Lib/EncoderLib/EncModeCtrl.cpp
index da6b06b2..36a2da88 100644
--- a/source/Lib/EncoderLib/EncModeCtrl.cpp
+++ b/source/Lib/EncoderLib/EncModeCtrl.cpp
@@ -1124,7 +1124,12 @@ void EncModeCtrlMTnoRQT::initCULevel( Partitioner &partitioner, const CodingStru
 {
   // Min/max depth
   unsigned minDepth = 0;
+#if JVET_Q0471_CHROMA_QT_SPLIT
+  const unsigned    minQTSize = cs.sps->getMinQTSize(cs.slice->getSliceType(), partitioner.chType) >> ((cs.area.Y().chromaFormat == CHROMA_400) ? 0 : ((int)getChannelTypeScaleX(partitioner.chType, cs.area.Y().chromaFormat) - (int)getChannelTypeScaleY(partitioner.chType, cs.area.Y().chromaFormat)));
+  unsigned maxDepth = floorLog2(cs.sps->getCTUSize()) - floorLog2(minQTSize);
+#else
   unsigned maxDepth = floorLog2(cs.sps->getCTUSize()) - floorLog2(cs.sps->getMinQTSize( m_slice->getSliceType(), partitioner.chType ));
+#endif
   if( m_pcEncCfg->getUseFastLCTU() )
   {
     if( auto adPartitioner = dynamic_cast<AdaptiveDepthPartitioner*>( &partitioner ) )
@@ -1156,7 +1161,11 @@ void EncModeCtrlMTnoRQT::initCULevel( Partitioner &partitioner, const CodingStru
                          || (  cuLeft  && !cuAbove  && cuLeft ->qtDepth > partitioner.currQtDepth )
                          || ( !cuLeft  &&  cuAbove  && cuAbove->qtDepth > partitioner.currQtDepth )
                          || ( !cuAbove && !cuLeft   && cs.area.lwidth() >= ( 32 << cs.slice->getDepth() ) ) )
+#if JVET_Q0471_CHROMA_QT_SPLIT
+                         && ( cs.area.lwidth() > (minQTSize << 1 ) );
+#else
                          && ( cs.area.lwidth() > ( cs.pcv->getMinQtSize( *cs.slice, partitioner.chType ) << 1 ) );
+#endif
 
   // set features
   ComprCUCtx &cuECtx  = m_ComprCUCtxList.back();
-- 
2.19.1.windows.1

