From 4c411ad6f696c363120118f9faa08fb74a129d57 Mon Sep 17 00:00:00 2001
From: ezhizng <zhi.a.zhang@ericsson.com>
Date: Fri, 19 Jul 2019 19:49:54 +0200
Subject: [PATCH] JVET-O0219: LFNST transform set selection for a CCLM coded
 block. Remove duplicate code. Fix a bug due to convert MIP mode to
 convensional mode

---
 source/Lib/CommonLib/TrQuant.cpp   | 12 ++----------
 source/Lib/CommonLib/UnitTools.cpp | 19 +++++++++++++++++++
 source/Lib/CommonLib/UnitTools.h   |  3 +++
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/source/Lib/CommonLib/TrQuant.cpp b/source/Lib/CommonLib/TrQuant.cpp
index 90fcbd4ae..52225aee9 100644
--- a/source/Lib/CommonLib/TrQuant.cpp
+++ b/source/Lib/CommonLib/TrQuant.cpp
@@ -261,11 +261,7 @@ void TrQuant::xInvLfnst( const TransformUnit &tu, const ComponentID compID )
     if( PU::isLMCMode( tu.cs->getPU( area.pos(), toChannelType( compID ) )->intraDir[ toChannelType( compID ) ] ) )
     {
 #if JVET_O0219_LFNST_TRANSFORM_SET_FOR_LMCMODE
-      Position topLeftPos = tu.cs->getPU(area.pos(), toChannelType(compID))->blocks[toChannelType(compID)].lumaPos();
-      Position refPos = topLeftPos.offset( tu.cs->getPU(area.pos(), toChannelType(compID))->blocks[toChannelType(compID)].lumaSize().width >> 1,
-                                           tu.cs->getPU(area.pos(), toChannelType(compID))->blocks[toChannelType(compID)].lumaSize().height >> 1 );
-      const PredictionUnit &lumaPU = CS::isDualITree( *tu.cs ) ? *tu.cs->picture->cs->getPU( refPos, CHANNEL_TYPE_LUMA ) : *tu.cs->getPU( topLeftPos, CHANNEL_TYPE_LUMA );
-      intraMode = lumaPU.intraDir[0];
+      intraMode = PU::getCoLocatedIntraLumaMode( *tu.cs->getPU( area.pos(), toChannelType( compID ) ) );
 #else
       intraMode = PLANAR_IDX;
 #endif
@@ -369,11 +365,7 @@ void TrQuant::xFwdLfnst( const TransformUnit &tu, const ComponentID compID, cons
     if( PU::isLMCMode( tu.cs->getPU( area.pos(), toChannelType( compID ) )->intraDir[ toChannelType( compID ) ] ) )
     {
 #if JVET_O0219_LFNST_TRANSFORM_SET_FOR_LMCMODE
-      Position topLeftPos = tu.cs->getPU(area.pos(), toChannelType(compID))->blocks[toChannelType(compID)].lumaPos();
-      Position refPos = topLeftPos.offset( tu.cs->getPU(area.pos(), toChannelType(compID))->blocks[toChannelType(compID)].lumaSize().width >> 1,
-                                           tu.cs->getPU(area.pos(), toChannelType(compID))->blocks[toChannelType(compID)].lumaSize().height >> 1 );
-      const PredictionUnit &lumaPU = CS::isDualITree( *tu.cs ) ? *tu.cs->picture->cs->getPU( refPos, CHANNEL_TYPE_LUMA ) : *tu.cs->getPU( topLeftPos, CHANNEL_TYPE_LUMA );
-      intraMode = lumaPU.intraDir[0];
+      intraMode = PU::getCoLocatedIntraLumaMode( *tu.cs->getPU( area.pos(), toChannelType( compID ) ) );
 #else
       intraMode = PLANAR_IDX;
 #endif
diff --git a/source/Lib/CommonLib/UnitTools.cpp b/source/Lib/CommonLib/UnitTools.cpp
index ac2bc0d32..10cc78a12 100644
--- a/source/Lib/CommonLib/UnitTools.cpp
+++ b/source/Lib/CommonLib/UnitTools.cpp
@@ -765,10 +765,14 @@ void PU::getIntraChromaCandModes( const PredictionUnit &pu, unsigned modeList[NU
     modeList[6] = MDLM_T_IDX;
     modeList[7] = DM_CHROMA_IDX;
 
+#if JVET_O0219_LFNST_TRANSFORM_SET_FOR_LMCMODE
+    const uint32_t lumaMode = getCoLocatedIntraLumaMode(pu);
+#else
     Position topLeftPos = pu.blocks[pu.chType].lumaPos();
     Position refPos = topLeftPos.offset( pu.blocks[pu.chType].lumaSize().width >> 1, pu.blocks[pu.chType].lumaSize().height >> 1 );
     const PredictionUnit *lumaPU = CS::isDualITree( *pu.cs ) ? pu.cs->picture->cs->getPU( refPos, CHANNEL_TYPE_LUMA ) : &pu;
     const uint32_t lumaMode = PU::getIntraDirLuma( *lumaPU );
+#endif
     for( int i = 0; i < 4; i++ )
     {
       if( lumaMode == modeList[i] )
@@ -839,11 +843,15 @@ uint32_t PU::getFinalIntraMode( const PredictionUnit &pu, const ChannelType &chT
 
   if( uiIntraMode == DM_CHROMA_IDX && !isLuma( chType ) )
   {
+#if JVET_O0219_LFNST_TRANSFORM_SET_FOR_LMCMODE
+    uiIntraMode = getCoLocatedIntraLumaMode(pu);
+#else
     Position topLeftPos = pu.blocks[pu.chType].lumaPos();
     Position refPos = topLeftPos.offset( pu.blocks[pu.chType].lumaSize().width >> 1, pu.blocks[pu.chType].lumaSize().height >> 1 );
     const PredictionUnit &lumaPU = CS::isDualITree( *pu.cs ) ? *pu.cs->picture->cs->getPU( refPos, CHANNEL_TYPE_LUMA ) : *pu.cs->getPU( topLeftPos, CHANNEL_TYPE_LUMA );
 
     uiIntraMode = PU::getIntraDirLuma( lumaPU );
+#endif
   }
   if( pu.chromaFormat == CHROMA_422 && !isLuma( chType ) && uiIntraMode < NUM_LUMA_MODE ) // map directional, planar and dc
   {
@@ -852,6 +860,17 @@ uint32_t PU::getFinalIntraMode( const PredictionUnit &pu, const ChannelType &chT
   return uiIntraMode;
 }
 
+#if JVET_O0219_LFNST_TRANSFORM_SET_FOR_LMCMODE
+uint32_t PU::getCoLocatedIntraLumaMode( const PredictionUnit &pu )
+{
+  Position topLeftPos = pu.blocks[pu.chType].lumaPos();
+  Position refPos = topLeftPos.offset( pu.blocks[pu.chType].lumaSize().width >> 1, pu.blocks[pu.chType].lumaSize().height >> 1 );
+  const PredictionUnit &lumaPU = CS::isDualITree( *pu.cs ) ? *pu.cs->picture->cs->getPU( refPos, CHANNEL_TYPE_LUMA ) : *pu.cs->getPU( topLeftPos, CHANNEL_TYPE_LUMA );
+
+  return PU::getIntraDirLuma( lumaPU );
+}
+#endif
+
 int PU::getWideAngIntraMode( const TransformUnit &tu, const uint32_t dirMode, const ComponentID compID )
 {
   if( dirMode < 2 )
diff --git a/source/Lib/CommonLib/UnitTools.h b/source/Lib/CommonLib/UnitTools.h
index 6bc6eb80c..4bc321ae6 100644
--- a/source/Lib/CommonLib/UnitTools.h
+++ b/source/Lib/CommonLib/UnitTools.h
@@ -131,6 +131,9 @@ namespace PU
   AvailableInfo getAvailableInfoLuma  (const PredictionUnit &pu);
   void getIntraChromaCandModes        (const PredictionUnit &pu, unsigned modeList[NUM_CHROMA_MODE]);
   uint32_t getFinalIntraMode              (const PredictionUnit &pu, const ChannelType &chType);
+#if JVET_O0219_LFNST_TRANSFORM_SET_FOR_LMCMODE
+  uint32_t getCoLocatedIntraLumaMode      (const PredictionUnit &pu);
+#endif
   int getWideAngIntraMode             ( const TransformUnit &tu, const uint32_t dirMode, const ComponentID compID );
   void getInterMergeCandidates        (const PredictionUnit &pu, MergeCtx& mrgCtx,
     int mmvdList,
-- 
GitLab