From 1ae35623bdbd799f5159e043e4fd06603baafe38 Mon Sep 17 00:00:00 2001
From: Christopher Hollmann <ehollch@esekilxxen2614.rnd.ericsson.se>
Date: Fri, 18 Oct 2019 10:06:26 +0200
Subject: [PATCH] JVET-P0273: MTSIntraMaxCand for LFNST

---
 cfg/encoder_intra_vtm.cfg             |  2 +-
 cfg/encoder_randomaccess_vtm.cfg      |  2 +-
 source/App/EncoderApp/EncApp.cpp      |  7 ++++++-
 source/Lib/CommonLib/TypeDef.h        |  2 ++
 source/Lib/EncoderLib/EncCfg.h        | 12 ++++++++++++
 source/Lib/EncoderLib/EncCu.cpp       |  4 ++++
 source/Lib/EncoderLib/InterSearch.cpp |  4 ++++
 source/Lib/EncoderLib/IntraSearch.cpp |  4 ++++
 8 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/cfg/encoder_intra_vtm.cfg b/cfg/encoder_intra_vtm.cfg
index 13191e962..63f157588 100644
--- a/cfg/encoder_intra_vtm.cfg
+++ b/cfg/encoder_intra_vtm.cfg
@@ -91,7 +91,7 @@ MaxMTTHierarchyDepthISliceL  : 3
 MaxMTTHierarchyDepthISliceC  : 3
 
 MTS                          : 1
-MTSIntraMaxCand              : 3
+MTSIntraMaxCand              : 4
 MTSInterMaxCand              : 4
 SBT                          : 1
 LFNST                        : 1
diff --git a/cfg/encoder_randomaccess_vtm.cfg b/cfg/encoder_randomaccess_vtm.cfg
index 9548bc8e6..631c6cb00 100644
--- a/cfg/encoder_randomaccess_vtm.cfg
+++ b/cfg/encoder_randomaccess_vtm.cfg
@@ -120,7 +120,7 @@ MaxMTTHierarchyDepthISliceL  : 3
 MaxMTTHierarchyDepthISliceC  : 3
 
 MTS                          : 1
-MTSIntraMaxCand              : 3
+MTSIntraMaxCand              : 4
 MTSInterMaxCand              : 4
 SBT                          : 1
 LFNST                        : 1
diff --git a/source/App/EncoderApp/EncApp.cpp b/source/App/EncoderApp/EncApp.cpp
index 81bad4b47..c60e71903 100644
--- a/source/App/EncoderApp/EncApp.cpp
+++ b/source/App/EncoderApp/EncApp.cpp
@@ -271,9 +271,14 @@ void EncApp::xInitLibCfg()
   m_cEncLib.setUseLMChroma                                       ( m_LMChroma );
   m_cEncLib.setCclmCollocatedChromaFlag                          ( m_cclmCollocatedChromaFlag );
   m_cEncLib.setIntraMTS                                          ( m_MTS & 1 );
-  m_cEncLib.setIntraMTSMaxCand                                   ( m_MTSIntraMaxCand );
   m_cEncLib.setInterMTS                                          ( ( m_MTS >> 1 ) & 1 );
+#if JVET_P0273_MTSIntraMaxCand
+  m_cEncLib.setMTSIntraMaxCand                                   ( m_MTSIntraMaxCand );
+  m_cEncLib.setMTSInterMaxCand                                   ( m_MTSInterMaxCand );
+#else
+  m_cEncLib.setIntraMTSMaxCand                                   ( m_MTSIntraMaxCand );
   m_cEncLib.setInterMTSMaxCand                                   ( m_MTSInterMaxCand );
+#endif
   m_cEncLib.setImplicitMTS                                       ( m_MTSImplicit );
   m_cEncLib.setUseSBT                                            ( m_SBT );
   m_cEncLib.setUseCompositeRef                                   ( m_compositeRefEnabled );
diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index 2789b0c21..d3ed6ccee 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -50,6 +50,8 @@
 #include <assert.h>
 #include <cassert>
 
+#define JVET_P0273_MTSIntraMaxCand                        1 // JVET-P0273: Use MTSIntraMaxCand if LFNST is used
+
 #define JVET_P0400_REMOVE_SHARED_MERGE_LIST               1 // JVET-P0400: removeal of shared merge list
 
 #define JVET_P0436_CQP_OFFSET_SIGNALLING                  1 // JVET_P0436: CU chroma QP offset signalling consistent with VPDU and bugfix 
diff --git a/source/Lib/EncoderLib/EncCfg.h b/source/Lib/EncoderLib/EncCfg.h
index 1d97cf8df..043a63567 100644
--- a/source/Lib/EncoderLib/EncCfg.h
+++ b/source/Lib/EncoderLib/EncCfg.h
@@ -276,8 +276,13 @@ protected:
   bool      m_cclmCollocatedChromaFlag;
   int       m_IntraMTS;
   int       m_InterMTS;
+#if JVET_P0273_MTSIntraMaxCand
+  int       m_MTSIntraMaxCand;
+  int       m_MTSInterMaxCand;
+#else
   int       m_IntraMTSMaxCand;
   int       m_InterMTSMaxCand;
+#endif
   int       m_ImplicitMTS;
   bool      m_SBT;                                ///< Sub-Block Transform for inter blocks
   bool      m_LFNST;
@@ -863,10 +868,17 @@ public:
   void      setBIO(bool b)                                   { m_BIO = b; }
   bool      getBIO()                                   const { return m_BIO; }
 
+#if JVET_P0273_MTSIntraMaxCand
+  void      setMTSIntraMaxCand              ( unsigned u )   { m_MTSIntraMaxCand = u; }
+  unsigned  getMTSIntraMaxCand              ()         const { return m_MTSIntraMaxCand; }
+  void      setMTSInterMaxCand              ( unsigned u )   { m_MTSInterMaxCand = u; }
+  unsigned  getMTSInterMaxCand              ()         const { return m_MTSInterMaxCand; }
+#else
   void      setIntraMTSMaxCand              ( unsigned u )   { m_IntraMTSMaxCand = u; }
   unsigned  getIntraMTSMaxCand              ()         const { return m_IntraMTSMaxCand; }
   void      setInterMTSMaxCand              ( unsigned u )   { m_InterMTSMaxCand = u; }
   unsigned  getInterMTSMaxCand              ()         const { return m_InterMTSMaxCand; }
+#endif
   void      setIntraMTS                     ( bool b )       { m_IntraMTS = b; }
   bool      getIntraMTS                     ()         const { return m_IntraMTS; }
   void      setInterMTS                     ( bool b )       { m_InterMTS = b; }
diff --git a/source/Lib/EncoderLib/EncCu.cpp b/source/Lib/EncoderLib/EncCu.cpp
index 10d54df71..46a8db6ff 100644
--- a/source/Lib/EncoderLib/EncCu.cpp
+++ b/source/Lib/EncoderLib/EncCu.cpp
@@ -1724,7 +1724,11 @@ void EncCu::xCheckRDCostIntra( CodingStructure *&tempCS, CodingStructure *&bestC
   int        startLfnstIdx       = 0;
   int        endLfnstIdx         = sps.getUseLFNST() ? maxLfnstIdx : 0;
 
+#if JVET_P0273_MTSIntraMaxCand
+  int grpNumMax = sps.getUseLFNST() ? m_pcEncCfg->getMTSIntraMaxCand() : 1;
+#else
   int grpNumMax = sps.getUseLFNST() ? 4 : 1;
+#endif
   m_pcIntraSearch->invalidateBestModeCost();
   for( int trGrpIdx = 0; trGrpIdx < grpNumMax; trGrpIdx++ )
   {
diff --git a/source/Lib/EncoderLib/InterSearch.cpp b/source/Lib/EncoderLib/InterSearch.cpp
index 24514c030..eddf5efdd 100644
--- a/source/Lib/EncoderLib/InterSearch.cpp
+++ b/source/Lib/EncoderLib/InterSearch.cpp
@@ -6614,7 +6614,11 @@ void InterSearch::xEstimateInterResidualQT(CodingStructure &cs, Partitioner &par
           {
             if( transformMode == 0 )
             {
+#if JVET_P0273_MTSIntraMaxCand
+              m_pcTrQuant->transformNxN( tu, compID, cQP, &trModes, m_pcEncCfg->getMTSInterMaxCand() );
+#else
               m_pcTrQuant->transformNxN( tu, compID, cQP, &trModes, CU::isIntra( *tu.cu ) ? m_pcEncCfg->getIntraMTSMaxCand() : m_pcEncCfg->getInterMTSMaxCand() );
+#endif
               tu.mtsIdx = trModes[0].first;
             }
             m_pcTrQuant->transformNxN( tu, compID, cQP, currAbsSum, m_CABACEstimator->getCtx(), true );
diff --git a/source/Lib/EncoderLib/IntraSearch.cpp b/source/Lib/EncoderLib/IntraSearch.cpp
index 3804b3817..078312917 100644
--- a/source/Lib/EncoderLib/IntraSearch.cpp
+++ b/source/Lib/EncoderLib/IntraSearch.cpp
@@ -2418,7 +2418,11 @@ void IntraSearch::xIntraCodingTUBlock(TransformUnit &tu, const ComponentID &comp
   {
     if (trModes)
     {
+#if JVET_P0273_MTSIntraMaxCand
+      m_pcTrQuant->transformNxN(tu, compID, cQP, trModes, m_pcEncCfg->getMTSIntraMaxCand());
+#else
       m_pcTrQuant->transformNxN(tu, compID, cQP, trModes, CU::isIntra(*tu.cu) ? m_pcEncCfg->getIntraMTSMaxCand() : m_pcEncCfg->getInterMTSMaxCand());
+#endif
       tu.mtsIdx = trModes->at(0).first;
     }
     m_pcTrQuant->transformNxN(tu, compID, cQP, uiAbsSum, m_CABACEstimator->getCtx(), loadTr);
-- 
GitLab