From bc59a948f2ef7dba75ee8963ea62bc2293740eab Mon Sep 17 00:00:00 2001
From: Xin Zhao <xinzzhao@tencent.com>
Date: Thu, 24 Jan 2019 22:03:01 +0100
Subject: [PATCH] JVET-M0502

---
 source/Lib/CommonLib/ContextModelling.cpp | 11 +++++++++++
 source/Lib/CommonLib/ContextModelling.h   |  3 +++
 source/Lib/CommonLib/Contexts.cpp         | 13 +++++++++++++
 source/Lib/CommonLib/TypeDef.h            |  2 ++
 source/Lib/DecoderLib/CABACReader.cpp     |  4 ++++
 source/Lib/EncoderLib/CABACWriter.cpp     |  4 ++++
 6 files changed, 37 insertions(+)

diff --git a/source/Lib/CommonLib/ContextModelling.cpp b/source/Lib/CommonLib/ContextModelling.cpp
index 178cd6ffd7..b6fab097fc 100644
--- a/source/Lib/CommonLib/ContextModelling.cpp
+++ b/source/Lib/CommonLib/ContextModelling.cpp
@@ -439,6 +439,17 @@ unsigned DeriveCtx::CtxTriangleFlag( const CodingUnit& cu )
   return ctxId;
 }
 
+#if JVET_M0502_PRED_MODE_CTX
+unsigned DeriveCtx::CtxPredModeFlag( const CodingUnit& cu )
+{
+  const CodingUnit *cuLeft  = cu.cs->getCURestricted(cu.lumaPos().offset(-1, 0), cu, CH_L);
+  const CodingUnit *cuAbove = cu.cs->getCURestricted(cu.lumaPos().offset(0, -1), cu, CH_L);
+
+  unsigned ctxId = ((cuAbove && cuAbove->predMode == MODE_INTRA) || (cuLeft && cuLeft->predMode == MODE_INTRA)) ? 1 : 0;
+
+  return ctxId;
+}
+#endif
 
 void MergeCtx::setMergeInfo( PredictionUnit& pu, int candIdx )
 {
diff --git a/source/Lib/CommonLib/ContextModelling.h b/source/Lib/CommonLib/ContextModelling.h
index c2e6d7899d..959b45b7c2 100644
--- a/source/Lib/CommonLib/ContextModelling.h
+++ b/source/Lib/CommonLib/ContextModelling.h
@@ -310,6 +310,9 @@ unsigned CtxSkipFlag  ( const CodingUnit& cu );
 unsigned CtxIMVFlag   ( const CodingUnit& cu );
 unsigned CtxAffineFlag( const CodingUnit& cu );
 unsigned CtxTriangleFlag( const CodingUnit& cu );
+#if JVET_M0502_PRED_MODE_CTX
+unsigned CtxPredModeFlag( const CodingUnit& cu );
+#endif
 }
 
 #endif // __CONTEXTMODELLING__
diff --git a/source/Lib/CommonLib/Contexts.cpp b/source/Lib/CommonLib/Contexts.cpp
index 6555d5d765..3a29c4a408 100644
--- a/source/Lib/CommonLib/Contexts.cpp
+++ b/source/Lib/CommonLib/Contexts.cpp
@@ -519,15 +519,28 @@ const CtxSet ContextSetCfg::PartSize = ContextSetCfg::addCtxSet
 const CtxSet ContextSetCfg::PredMode = ContextSetCfg::addCtxSet
 ({
 #if JVET_M0453_CABAC_ENGINE
+#if JVET_M0502_PRED_MODE_CTX
+  { 193, 193, },
+  { 151, 151, },
+  { CNU, CNU, },
+  {   1,   1, },
+#else
   {  193,},
   {  151,},
   {  CNU,},
   {    1,},
+#endif
+#else
+#if JVET_M0502_PRED_MODE_CTX
+  { 178, 178, },
+  { 194, 194, },
+  { CNU, CNU, },
 #else
   { 178, },
   { 194, },
   { CNU, },
 #endif
+#endif
 });
 
 const CtxSet ContextSetCfg::MultiRefLineIdx = ContextSetCfg::addCtxSet
diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index d4a16eb0e1..dad438451e 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -52,6 +52,8 @@
 
 #define JVET_M0479_18BITS_MV_CLIP                         1
 
+#define JVET_M0502_PRED_MODE_CTX                          1
+
 #define JVET_M0464_UNI_MTS                                1
 
 #if JVET_M0464_UNI_MTS
diff --git a/source/Lib/DecoderLib/CABACReader.cpp b/source/Lib/DecoderLib/CABACReader.cpp
index 1d201b375c..757202d964 100644
--- a/source/Lib/DecoderLib/CABACReader.cpp
+++ b/source/Lib/DecoderLib/CABACReader.cpp
@@ -927,7 +927,11 @@ void CABACReader::pred_mode( CodingUnit& cu )
 {
   RExt__DECODER_DEBUG_BIT_STATISTICS_CREATE_SET( STATS__CABAC_BITS__PRED_MODE );
 
+#if JVET_M0502_PRED_MODE_CTX
+  if( cu.cs->slice->isIntra() || m_BinDecoder.decodeBin( Ctx::PredMode(DeriveCtx::CtxPredModeFlag(cu)) ) )
+#else
   if( cu.cs->slice->isIntra() || m_BinDecoder.decodeBin( Ctx::PredMode() ) )
+#endif
   {
     cu.predMode = MODE_INTRA;
   }
diff --git a/source/Lib/EncoderLib/CABACWriter.cpp b/source/Lib/EncoderLib/CABACWriter.cpp
index eb391f4dcb..d883588edd 100644
--- a/source/Lib/EncoderLib/CABACWriter.cpp
+++ b/source/Lib/EncoderLib/CABACWriter.cpp
@@ -783,7 +783,11 @@ void CABACWriter::pred_mode( const CodingUnit& cu )
   {
     return;
   }
+#if JVET_M0502_PRED_MODE_CTX
+  m_BinEncoder.encodeBin( ( CU::isIntra( cu ) ), Ctx::PredMode( DeriveCtx::CtxPredModeFlag( cu ) ) );
+#else
   m_BinEncoder.encodeBin( ( CU::isIntra( cu ) ), Ctx::PredMode() );
+#endif
 }
 
 void CABACWriter::pcm_data( const CodingUnit& cu, Partitioner& partitioner  )
-- 
GitLab