From e1b0d398aeefb8f9e12f3faeb81600f8ca762867 Mon Sep 17 00:00:00 2001
From: zhipin <zhipin.deng@bytedance.com>
Date: Tue, 16 Jun 2020 22:37:15 +0800
Subject: [PATCH 1/2] fix the GDR recovery POC check

---
 source/Lib/CommonLib/Slice.cpp      | 21 +++++++++++----------
 source/Lib/CommonLib/Slice.h        |  2 +-
 source/Lib/CommonLib/TypeDef.h      |  3 +++
 source/Lib/DecoderLib/DecLib.cpp    |  8 ++++----
 source/Lib/DecoderLib/VLCReader.cpp |  2 +-
 source/Lib/EncoderLib/VLCWriter.cpp |  2 +-
 6 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/source/Lib/CommonLib/Slice.cpp b/source/Lib/CommonLib/Slice.cpp
index d12915c569..0b447f32f4 100644
--- a/source/Lib/CommonLib/Slice.cpp
+++ b/source/Lib/CommonLib/Slice.cpp
@@ -50,7 +50,7 @@ Slice::Slice()
 : m_iPOC                          ( 0 )
 , m_iLastIDR                      ( 0 )
 #if JVET_R0041
-, m_prevGDRInSameLayerPOC         ( 0 )
+, m_prevGDRInSameLayerPOC         ( MAX_INT )
 #endif
 , m_iAssociatedIRAP               ( 0 )
 , m_iAssociatedIRAPType           ( NAL_UNIT_INVALID )
@@ -721,9 +721,9 @@ void Slice::checkRPL(const ReferencePictureList* pRPL0, const ReferencePictureLi
 #endif
     {
 #if JVET_R0041
-      CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "When the current picture follows an IRAP picture having the same value "
-            "of nuh_layer_id in both decoding order and output order, there shall be no picture referred to by an active entry in RefPicList[ 0 ] or RefPicList[ 1 ] that "
-            "precedes that IRAP picture in output order or decoding order.");
+      CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "When the current picture, with nuh_layer_id equal to a particular value layerId, "
+           "is an IRAP picture, there shall be no picture referred to by an entry in RefPicList[ 0 ] that precedes, in output order or decoding order, any preceding IRAP picture "
+           "with nuh_layer_id equal to layerId in decoding order (when present).");
 #else
       CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "IRAP picture detected that violate the rule that no entry in RefPicList[] shall precede, in output order or decoding order, any preceding IRAP picture in decoding order (when present).");
 #endif
@@ -804,9 +804,9 @@ void Slice::checkRPL(const ReferencePictureList* pRPL0, const ReferencePictureLi
 #if JVET_R0041
     if ((m_eNalUnitType == NAL_UNIT_CODED_SLICE_CRA || m_eNalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_eNalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP))
     {
-        CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "When the current picture follows an IRAP picture having the same value of "
-              "nuh_layer_id in both decoding order and output order, there shall be no picture referred to by an active entry in RefPicList[ 0 ] or RefPicList[ 1 ] that "
-              "precedes that IRAP picture in output order or decoding order.");
+        CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "When the current picture, with nuh_layer_id equal to a particular value layerId, "
+             "is an IRAP picture, there shall be no picture referred to by an entry in RefPicList[ 1 ] that precedes, in output order or decoding order, any preceding IRAP picture "
+             "with nuh_layer_id equal to layerId in decoding order (when present).");
     }
 #else
     if (m_eNalUnitType == NAL_UNIT_CODED_SLICE_CRA)
@@ -1319,7 +1319,8 @@ void Slice::checkLeadingPictureRestrictions(PicList& rcListPic, const PPS& pps)
 #endif
 
 #if JVET_R0041
-    if (pcSlice->getPicHeader()->getPicOutputFlag() == 1 && !this->getPicHeader()->getNoOutputBeforeRecoveryFlag() && pcPic->layerId == this->m_nuhLayerId)
+    if (pcSlice->getPicHeader()->getPicOutputFlag() == 1 && !this->getPicHeader()->getNoOutputBeforeRecoveryFlag() && pcPic->layerId == this->m_nuhLayerId
+        && nalUnitType != NAL_UNIT_CODED_SLICE_GDR && this->getPicHeader()->getRecoveryPocCnt() != -1)
     {
       if (this->getPOC() == this->getPicHeader()->getRecoveryPocCnt() + this->getPrevGDRInSameLayerPOC())
       {
@@ -2365,7 +2366,7 @@ PicHeader::PicHeader()
 , m_nonReferencePictureFlag                       ( 0 )
 , m_gdrPicFlag                                    ( 0 )
 , m_noOutputOfPriorPicsFlag                       ( 0 )
-, m_recoveryPocCnt                                ( 0 )
+, m_recoveryPocCnt                                ( -1 )
 , m_noOutputBeforeRecoveryFlag                    ( false )
 , m_handleCraAsCvsStartFlag                       ( false )
 , m_handleGdrAsCvsStartFlag                       ( false )
@@ -2462,7 +2463,7 @@ void PicHeader::initPicHeader()
   m_nonReferencePictureFlag                       = 0;
   m_gdrPicFlag                                    = 0;
   m_noOutputOfPriorPicsFlag                       = 0;
-  m_recoveryPocCnt                                = 0;
+  m_recoveryPocCnt                                = -1;
   m_spsId                                         = -1;
   m_ppsId                                         = -1;
   m_pocMsbPresentFlag                             = 0;
diff --git a/source/Lib/CommonLib/Slice.h b/source/Lib/CommonLib/Slice.h
index 68c9e48aa6..96c1ce7325 100644
--- a/source/Lib/CommonLib/Slice.h
+++ b/source/Lib/CommonLib/Slice.h
@@ -2425,7 +2425,7 @@ public:
   void                        setNoOutputOfPriorPicsFlag( bool b )                      { m_noOutputOfPriorPicsFlag = b;                                                               }
   bool                        getNoOutputOfPriorPicsFlag() const                        { return m_noOutputOfPriorPicsFlag;                                                            }
   void                        setRecoveryPocCnt( uint32_t u )                           { m_recoveryPocCnt = u;                                                                        }
-  bool                        getRecoveryPocCnt() const                                 { return m_recoveryPocCnt;                                                                     }
+  uint32_t                    getRecoveryPocCnt() const                                 { return m_recoveryPocCnt;                                                                     }
   void                        setSPSId( uint32_t u )                                    { m_spsId = u;                                                                                 }
   uint32_t                    getSPSId() const                                          { return m_spsId;                                                                              }
   void                        setPPSId( uint32_t u )                                    { m_ppsId = u;                                                                                 }
diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index c82f1006d0..35d084363e 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -52,6 +52,9 @@
 
 //########### place macros to be removed in next cycle below this line ###############
 #define JVET_R0080                                        1 // JVET-R0080, Change the syntax condition for pps_tile_idx_delta_present_flag. When the value of pps_num_slices_in_pic_minus1 is greater than 1 instead of 0, the syntax element of pps_tile_idx_delta_present_flag is signalled.
+
+#define JVET_R0041                                        1 // JVET-R0041: Picture types related constraints
+
 #define JVET_R0064                                        1 // JVET-R0064, aspect 2: Move the flag scaling_matrix_for_lfnst_disabled_flag from the scaling_list_data( ) syntax to the SPS.
 
 #define RETRAIN_CABAC                                     1 // CABAC initial values retrained on VTM-9.0rc1
diff --git a/source/Lib/DecoderLib/DecLib.cpp b/source/Lib/DecoderLib/DecLib.cpp
index 619cfb9f61..f6cec4810a 100644
--- a/source/Lib/DecoderLib/DecLib.cpp
+++ b/source/Lib/DecoderLib/DecLib.cpp
@@ -393,8 +393,8 @@ DecLib::DecLib()
   , m_isFirstGeneralHrd(true)
   , m_prevGeneralHrdParams()
 #if JVET_R0041
-  , m_prevGDRInSameLayerPOC{ 0 }
-  , m_pocCRA{ 0 }
+  , m_prevGDRInSameLayerPOC{ MAX_INT }
+  , m_pocCRA{ MAX_INT }
   , m_associatedIRAPDecodingOrderNumber{ 0 }
 #else
   , m_associatedIRAPType(NAL_UNIT_INVALID)
@@ -2668,8 +2668,8 @@ bool DecLib::decode(InputNALUnit& nalu, int& iSkipFrame, int& iPOCLastDisplay, i
     case NAL_UNIT_EOS:
 #if JVET_R0041
       m_associatedIRAPType[nalu.m_nuhLayerId] = NAL_UNIT_INVALID;
-      m_pocCRA[nalu.m_nuhLayerId] = 0;
-      m_prevGDRInSameLayerPOC[nalu.m_nuhLayerId] = 0;
+      m_pocCRA[nalu.m_nuhLayerId] = MAX_INT;
+      m_prevGDRInSameLayerPOC[nalu.m_nuhLayerId] = MAX_INT;
 #else
       m_associatedIRAPType = NAL_UNIT_INVALID;
       m_pocCRA = 0;
diff --git a/source/Lib/DecoderLib/VLCReader.cpp b/source/Lib/DecoderLib/VLCReader.cpp
index 518bbb73d6..0a8069b4d2 100644
--- a/source/Lib/DecoderLib/VLCReader.cpp
+++ b/source/Lib/DecoderLib/VLCReader.cpp
@@ -2838,7 +2838,7 @@ void HLSyntaxReader::parsePictureHeader( PicHeader* picHeader, ParameterSetManag
   }
   else
   {
-    picHeader->setRecoveryPocCnt( 0 );
+    picHeader->setRecoveryPocCnt( -1 );
   }
 
   std::vector<bool> phExtraBitsPresent = sps->getExtraPHBitPresentFlags();
diff --git a/source/Lib/EncoderLib/VLCWriter.cpp b/source/Lib/EncoderLib/VLCWriter.cpp
index ec0a1bd159..5133c0cfb0 100644
--- a/source/Lib/EncoderLib/VLCWriter.cpp
+++ b/source/Lib/EncoderLib/VLCWriter.cpp
@@ -1770,7 +1770,7 @@ void HLSWriter::codePictureHeader( PicHeader* picHeader, bool writeRbspTrailingB
   }
   else
   {
-    picHeader->setRecoveryPocCnt( 0 );
+    picHeader->setRecoveryPocCnt( -1 );
   }
   // PH extra bits are not written in the reference encoder
   // as these bits are reserved for future extensions
-- 
GitLab


From eb388637c2e70de034ddebcf71f8e93a415b265a Mon Sep 17 00:00:00 2001
From: zhipin <zhipin.deng@bytedance.com>
Date: Tue, 16 Jun 2020 22:37:15 +0800
Subject: [PATCH 2/2] fix the GDR recovery POC check

---
 source/Lib/CommonLib/Slice.cpp      | 21 +++++++++++----------
 source/Lib/CommonLib/Slice.h        |  2 +-
 source/Lib/DecoderLib/DecLib.cpp    |  8 ++++----
 source/Lib/DecoderLib/VLCReader.cpp |  2 +-
 source/Lib/EncoderLib/VLCWriter.cpp |  2 +-
 5 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/source/Lib/CommonLib/Slice.cpp b/source/Lib/CommonLib/Slice.cpp
index d12915c569..0b447f32f4 100644
--- a/source/Lib/CommonLib/Slice.cpp
+++ b/source/Lib/CommonLib/Slice.cpp
@@ -50,7 +50,7 @@ Slice::Slice()
 : m_iPOC                          ( 0 )
 , m_iLastIDR                      ( 0 )
 #if JVET_R0041
-, m_prevGDRInSameLayerPOC         ( 0 )
+, m_prevGDRInSameLayerPOC         ( MAX_INT )
 #endif
 , m_iAssociatedIRAP               ( 0 )
 , m_iAssociatedIRAPType           ( NAL_UNIT_INVALID )
@@ -721,9 +721,9 @@ void Slice::checkRPL(const ReferencePictureList* pRPL0, const ReferencePictureLi
 #endif
     {
 #if JVET_R0041
-      CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "When the current picture follows an IRAP picture having the same value "
-            "of nuh_layer_id in both decoding order and output order, there shall be no picture referred to by an active entry in RefPicList[ 0 ] or RefPicList[ 1 ] that "
-            "precedes that IRAP picture in output order or decoding order.");
+      CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "When the current picture, with nuh_layer_id equal to a particular value layerId, "
+           "is an IRAP picture, there shall be no picture referred to by an entry in RefPicList[ 0 ] that precedes, in output order or decoding order, any preceding IRAP picture "
+           "with nuh_layer_id equal to layerId in decoding order (when present).");
 #else
       CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "IRAP picture detected that violate the rule that no entry in RefPicList[] shall precede, in output order or decoding order, any preceding IRAP picture in decoding order (when present).");
 #endif
@@ -804,9 +804,9 @@ void Slice::checkRPL(const ReferencePictureList* pRPL0, const ReferencePictureLi
 #if JVET_R0041
     if ((m_eNalUnitType == NAL_UNIT_CODED_SLICE_CRA || m_eNalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL || m_eNalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP))
     {
-        CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "When the current picture follows an IRAP picture having the same value of "
-              "nuh_layer_id in both decoding order and output order, there shall be no picture referred to by an active entry in RefPicList[ 0 ] or RefPicList[ 1 ] that "
-              "precedes that IRAP picture in output order or decoding order.");
+        CHECK(refPicPOC < irapPOC || refPicDecodingOrderNumber < associatedIRAPDecodingOrderNumber, "When the current picture, with nuh_layer_id equal to a particular value layerId, "
+             "is an IRAP picture, there shall be no picture referred to by an entry in RefPicList[ 1 ] that precedes, in output order or decoding order, any preceding IRAP picture "
+             "with nuh_layer_id equal to layerId in decoding order (when present).");
     }
 #else
     if (m_eNalUnitType == NAL_UNIT_CODED_SLICE_CRA)
@@ -1319,7 +1319,8 @@ void Slice::checkLeadingPictureRestrictions(PicList& rcListPic, const PPS& pps)
 #endif
 
 #if JVET_R0041
-    if (pcSlice->getPicHeader()->getPicOutputFlag() == 1 && !this->getPicHeader()->getNoOutputBeforeRecoveryFlag() && pcPic->layerId == this->m_nuhLayerId)
+    if (pcSlice->getPicHeader()->getPicOutputFlag() == 1 && !this->getPicHeader()->getNoOutputBeforeRecoveryFlag() && pcPic->layerId == this->m_nuhLayerId
+        && nalUnitType != NAL_UNIT_CODED_SLICE_GDR && this->getPicHeader()->getRecoveryPocCnt() != -1)
     {
       if (this->getPOC() == this->getPicHeader()->getRecoveryPocCnt() + this->getPrevGDRInSameLayerPOC())
       {
@@ -2365,7 +2366,7 @@ PicHeader::PicHeader()
 , m_nonReferencePictureFlag                       ( 0 )
 , m_gdrPicFlag                                    ( 0 )
 , m_noOutputOfPriorPicsFlag                       ( 0 )
-, m_recoveryPocCnt                                ( 0 )
+, m_recoveryPocCnt                                ( -1 )
 , m_noOutputBeforeRecoveryFlag                    ( false )
 , m_handleCraAsCvsStartFlag                       ( false )
 , m_handleGdrAsCvsStartFlag                       ( false )
@@ -2462,7 +2463,7 @@ void PicHeader::initPicHeader()
   m_nonReferencePictureFlag                       = 0;
   m_gdrPicFlag                                    = 0;
   m_noOutputOfPriorPicsFlag                       = 0;
-  m_recoveryPocCnt                                = 0;
+  m_recoveryPocCnt                                = -1;
   m_spsId                                         = -1;
   m_ppsId                                         = -1;
   m_pocMsbPresentFlag                             = 0;
diff --git a/source/Lib/CommonLib/Slice.h b/source/Lib/CommonLib/Slice.h
index 4b94fd718d..5eb49c23e6 100644
--- a/source/Lib/CommonLib/Slice.h
+++ b/source/Lib/CommonLib/Slice.h
@@ -2436,7 +2436,7 @@ public:
   void                        setNoOutputOfPriorPicsFlag( bool b )                      { m_noOutputOfPriorPicsFlag = b;                                                               }
   bool                        getNoOutputOfPriorPicsFlag() const                        { return m_noOutputOfPriorPicsFlag;                                                            }
   void                        setRecoveryPocCnt( uint32_t u )                           { m_recoveryPocCnt = u;                                                                        }
-  bool                        getRecoveryPocCnt() const                                 { return m_recoveryPocCnt;                                                                     }
+  uint32_t                    getRecoveryPocCnt() const                                 { return m_recoveryPocCnt;                                                                     }
   void                        setSPSId( uint32_t u )                                    { m_spsId = u;                                                                                 }
   uint32_t                    getSPSId() const                                          { return m_spsId;                                                                              }
   void                        setPPSId( uint32_t u )                                    { m_ppsId = u;                                                                                 }
diff --git a/source/Lib/DecoderLib/DecLib.cpp b/source/Lib/DecoderLib/DecLib.cpp
index 619cfb9f61..f6cec4810a 100644
--- a/source/Lib/DecoderLib/DecLib.cpp
+++ b/source/Lib/DecoderLib/DecLib.cpp
@@ -393,8 +393,8 @@ DecLib::DecLib()
   , m_isFirstGeneralHrd(true)
   , m_prevGeneralHrdParams()
 #if JVET_R0041
-  , m_prevGDRInSameLayerPOC{ 0 }
-  , m_pocCRA{ 0 }
+  , m_prevGDRInSameLayerPOC{ MAX_INT }
+  , m_pocCRA{ MAX_INT }
   , m_associatedIRAPDecodingOrderNumber{ 0 }
 #else
   , m_associatedIRAPType(NAL_UNIT_INVALID)
@@ -2668,8 +2668,8 @@ bool DecLib::decode(InputNALUnit& nalu, int& iSkipFrame, int& iPOCLastDisplay, i
     case NAL_UNIT_EOS:
 #if JVET_R0041
       m_associatedIRAPType[nalu.m_nuhLayerId] = NAL_UNIT_INVALID;
-      m_pocCRA[nalu.m_nuhLayerId] = 0;
-      m_prevGDRInSameLayerPOC[nalu.m_nuhLayerId] = 0;
+      m_pocCRA[nalu.m_nuhLayerId] = MAX_INT;
+      m_prevGDRInSameLayerPOC[nalu.m_nuhLayerId] = MAX_INT;
 #else
       m_associatedIRAPType = NAL_UNIT_INVALID;
       m_pocCRA = 0;
diff --git a/source/Lib/DecoderLib/VLCReader.cpp b/source/Lib/DecoderLib/VLCReader.cpp
index 2f42046823..bef1f8b86f 100644
--- a/source/Lib/DecoderLib/VLCReader.cpp
+++ b/source/Lib/DecoderLib/VLCReader.cpp
@@ -2838,7 +2838,7 @@ void HLSyntaxReader::parsePictureHeader( PicHeader* picHeader, ParameterSetManag
   }
   else
   {
-    picHeader->setRecoveryPocCnt( 0 );
+    picHeader->setRecoveryPocCnt( -1 );
   }
 
   std::vector<bool> phExtraBitsPresent = sps->getExtraPHBitPresentFlags();
diff --git a/source/Lib/EncoderLib/VLCWriter.cpp b/source/Lib/EncoderLib/VLCWriter.cpp
index 3e9a0af096..38672edfcb 100644
--- a/source/Lib/EncoderLib/VLCWriter.cpp
+++ b/source/Lib/EncoderLib/VLCWriter.cpp
@@ -1770,7 +1770,7 @@ void HLSWriter::codePictureHeader( PicHeader* picHeader, bool writeRbspTrailingB
   }
   else
   {
-    picHeader->setRecoveryPocCnt( 0 );
+    picHeader->setRecoveryPocCnt( -1 );
   }
   // PH extra bits are not written in the reference encoder
   // as these bits are reserved for future extensions
-- 
GitLab