From ea18006d30d291b19506fe71c455ae6911e8e3d6 Mon Sep 17 00:00:00 2001
From: Takeshi Tsukuba <takeshi.tsukuba@sony.com>
Date: Tue, 3 Aug 2021 16:17:49 +0000
Subject: [PATCH] Fix#1499: Fix a bitsream constraint on RExt tools for low
 bit-depth (JVET-W0178)

---
 source/App/EncoderApp/EncAppCfg.cpp |  4 ++--
 source/Lib/DecoderLib/VLCReader.cpp | 10 ++++++----
 source/Lib/EncoderLib/VLCWriter.cpp | 11 ++++++-----
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/source/App/EncoderApp/EncAppCfg.cpp b/source/App/EncoderApp/EncAppCfg.cpp
index e0a823bd7..d0f5fabc8 100644
--- a/source/App/EncoderApp/EncAppCfg.cpp
+++ b/source/App/EncoderApp/EncAppCfg.cpp
@@ -2992,12 +2992,12 @@ bool EncAppCfg::xCheckParameter()
   xConfirmPara( (m_MSBExtendedBitDepth[CHANNEL_TYPE_CHROMA] < m_inputBitDepth[CHANNEL_TYPE_CHROMA]), "MSB-extended bit depth for chroma channel (--MSBExtendedBitDepthC) must be greater than or equal to input bit depth for chroma channel (--InputBitDepthC)" );
 
 #if JVET_W0178_CONSTRAINTS_ON_REXT_TOOLS
-  bool check_sps_extension_flag = m_extendedPrecisionProcessingFlag || 
+  bool check_sps_range_extension_flag = m_extendedPrecisionProcessingFlag || 
                                   m_rrcRiceExtensionEnableFlag ||
                                   m_persistentRiceAdaptationEnabledFlag || 
                                   m_tsrcRicePresentFlag;
   if (m_internalBitDepth[CHANNEL_TYPE_LUMA] <= 10)
-    xConfirmPara( (check_sps_extension_flag == 1) ,
+    xConfirmPara( (check_sps_range_extension_flag == 1) ,
                  "RExt tools (Extended Precision Processing, RRC Rice Extension, Persistent Rice Adaptation and TSRC Rice Extension) must be disabled for BitDepth is less than or equal to 10 (the value of sps_range_extension_flag shall be 0 when BitDepth is less than or equal to 10.)");
 #endif
 
diff --git a/source/Lib/DecoderLib/VLCReader.cpp b/source/Lib/DecoderLib/VLCReader.cpp
index dc1c1b15a..e8e0553b3 100644
--- a/source/Lib/DecoderLib/VLCReader.cpp
+++ b/source/Lib/DecoderLib/VLCReader.cpp
@@ -2077,10 +2077,6 @@ void HLSyntaxReader::parseSPS(SPS* pcSPS)
   // KJS: no SPS extensions defined yet
 
   READ_FLAG( uiCode, "sps_extension_present_flag");
-#if JVET_W0178_CONSTRAINTS_ON_REXT_TOOLS
-  if (pcSPS->getBitDepth(CHANNEL_TYPE_LUMA) <= 10)
-    CHECK(uiCode == 1, "The value of sps_range_extension_flag shall be 0 when BitDepth is less than or equal to 10.");
-#endif
 
   if (uiCode)
   {
@@ -2102,6 +2098,12 @@ void HLSyntaxReader::parseSPS(SPS* pcSPS)
       sps_extension_flags[i] = uiCode!=0;
     }
 
+#if JVET_W0178_CONSTRAINTS_ON_REXT_TOOLS
+    if (pcSPS->getBitDepth(CHANNEL_TYPE_LUMA) <= 10)
+      CHECK(sps_extension_flags[SPS_EXT__REXT] == 1,
+            "The value of sps_range_extension_flag shall be 0 when BitDepth is less than or equal to 10.");
+#endif
+
     bool bSkipTrailingExtensionBits=false;
     for(int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
     {
diff --git a/source/Lib/EncoderLib/VLCWriter.cpp b/source/Lib/EncoderLib/VLCWriter.cpp
index 6e727016b..1e4b6e507 100644
--- a/source/Lib/EncoderLib/VLCWriter.cpp
+++ b/source/Lib/EncoderLib/VLCWriter.cpp
@@ -1258,11 +1258,6 @@ void HLSWriter::codeSPS( const SPS* pcSPS )
     sps_extension_present_flag|=sps_extension_flags[i];
   }
 
-#if JVET_W0178_CONSTRAINTS_ON_REXT_TOOLS
-  if (pcSPS->getBitDepth(CHANNEL_TYPE_LUMA) <= 10)
-    CHECK( (sps_extension_present_flag == 1),
-          "The value of sps_range_extension_flag shall be 0 when BitDepth is less than or equal to 10.");
-#endif
   WRITE_FLAG( (sps_extension_present_flag?1:0), "sps_extension_present_flag" );
 
   if (sps_extension_present_flag)
@@ -1278,6 +1273,12 @@ void HLSWriter::codeSPS( const SPS* pcSPS )
       "sps_extension_6bits[5]" };
 #endif
 
+#if JVET_W0178_CONSTRAINTS_ON_REXT_TOOLS
+    if (pcSPS->getBitDepth(CHANNEL_TYPE_LUMA) <= 10)
+      CHECK((sps_extension_flags[SPS_EXT__REXT] == 1),
+            "The value of sps_range_extension_flag shall be 0 when BitDepth is less than or equal to 10.");
+#endif
+
     for(int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
     {
       WRITE_FLAG( sps_extension_flags[i]?1:0, syntaxStrings[i] );
-- 
GitLab