From 667cd717f81f63d36654497c3c852d55a188b93e Mon Sep 17 00:00:00 2001
From: vdrugeon <virginie.drugeon@eu.panasonic.com>
Date: Fri, 10 Jul 2020 12:07:47 +0000
Subject: [PATCH] JVET-S0266: VUI modifications

VUI modifications including VUI length, moving two flags from GCI to
VUI, reordering VUI variables to follow syntax elements
---
 cfg/sei_vui/vui_HD.cfg              | 10 ++--
 cfg/sei_vui/vui_UHD_PQ.cfg          | 10 ++--
 doc/software-manual.tex             | 22 ++++-----
 source/App/EncoderApp/EncAppCfg.cpp |  6 +++
 source/Lib/CommonLib/Slice.cpp      |  2 +
 source/Lib/CommonLib/Slice.h        | 72 ++++++++++++++++++++++++++++-
 source/Lib/CommonLib/TypeDef.h      |  3 +-
 source/Lib/DecoderLib/VLCReader.cpp | 63 +++++++++++++++++++++++--
 source/Lib/EncoderLib/EncLib.cpp    |  6 +++
 source/Lib/EncoderLib/VLCWriter.cpp | 50 ++++++++++++++++++--
 source/Lib/EncoderLib/VLCWriter.h   |  3 ++
 11 files changed, 217 insertions(+), 30 deletions(-)

diff --git a/cfg/sei_vui/vui_HD.cfg b/cfg/sei_vui/vui_HD.cfg
index 394aa64850..3ee2d628f7 100644
--- a/cfg/sei_vui/vui_HD.cfg
+++ b/cfg/sei_vui/vui_HD.cfg
@@ -1,19 +1,21 @@
 VuiParametersPresent:            1      // enable VUI
+ProgressiveSource:               1      // indicates if content is progressive
+InterlacedSource:                0      // indicates if content is interlaced
+NonPackedSource:                 1      // indicates if content is not packed
+NonProjectedConstraintFlag:      1      // indicates if content is not projected
 AspectRatioInfoPresent:          1      // enable presence of sample aspect ratio information
 AspectRatioIdc:                  1      // sample aspect ratio pre-defined types according to Rec. ITU-T H.273 | ISO/IEC 23091-2
 SarWidth:                        1      // sample aspect ratio width, if AspectRatioIdc is equal to 255
 SarHeight:                       1      // sample aspect ratio height, if AspectRatioIdc is equal to 255
+OverscanInfoPresent:             1      // enable presence of overscan information
+OverscanAppropriate:             0      // indicates if the cropped decoded pictures output are suitable for display using overscan
 ColourDescriptionPresent:        1      // enable presence of colour description information
 ColourPrimaries:                 1      // the source colour primaries according to Rec. ITU-T H.273 | ISO/IEC 23091-2
 TransferCharacteristics:         1      // transfer characteristics function according to Rec. ITU-T H.273 | ISO/IEC 23091-2
 MatrixCoefficients:              1      // the formulae used in deriving luma and chroma signals acc. to Rec. ITU-T H.273 | ISO/IEC 23091-2
 VideoFullRange:                  0      // scaling and offset values applied according to Rec. ITU-T H.273 | ISO/IEC 23091-2
-ProgressiveSource:               1      // indicates if content is progressive
-InterlacedSource:                0      // indicates if content is interlaced
 ChromaLocInfoPresent:            1      // enable presence of chroma location information
 ChromaSampleLocTypeTopField:     0      // the location of chroma sample top field
 ChromaSampleLocTypeBottomField:  0      // the location of chroma sample bottom field
 ChromaSampleLocType:             0      // the location of chroma sample frame
-OverscanInfoPresent:             1      // enable presence of overscan information
-OverscanAppropriate:             0      // indicates if the cropped decoded pictures output are suitable for display using overscan
 
diff --git a/cfg/sei_vui/vui_UHD_PQ.cfg b/cfg/sei_vui/vui_UHD_PQ.cfg
index ec337fe50a..2aac422396 100644
--- a/cfg/sei_vui/vui_UHD_PQ.cfg
+++ b/cfg/sei_vui/vui_UHD_PQ.cfg
@@ -1,19 +1,21 @@
 VuiParametersPresent:            1      // enable VUI
+ProgressiveSource:               1      // Indicates if content is progressive
+InterlacedSource:                0      // Indicates if content is interlaced
+NonPackedSource:                 1      // indicates if content is not packed
+NonProjectedConstraintFlag:      1      // indicates if content is not projected
 AspectRatioInfoPresent:          1      // enable presence of sample aspect ratio information
 AspectRatioIdc:                  1      // sample aspect ratio pre-defined types according to Rec. ITU-T H.273 | ISO/IEC 23091-2
 SarWidth:                        1      // sample aspect ratio width, if AspectRatioIdc is equal to 255
 SarHeight:                       1      // sample aspect ratio height, if AspectRatioIdc is equal to 255
+OverscanInfoPresent:             1      // enable presence of overscan information
+OverscanAppropriate:             0      // indicates if the cropped decoded pictures output are suitable for display using overscan
 ColourDescriptionPresent:        1      // enable presence of colour description information
 ColourPrimaries:                 9      // the source colour primaries according to Rec. ITU-T H.273 | ISO/IEC 23091-2
 TransferCharacteristics:         16     // transfer characteristics function according to Rec. ITU-T H.273 | ISO/IEC 23091-2
 MatrixCoefficients:              9      // the formulae used in deriving luma and chroma signals acc. to Rec. ITU-T H.273 | ISO/IEC 23091-2
 VideoFullRange:                  0      // scaling and offset values applied according to Rec. ITU-T H.273 | ISO/IEC 23091-2
-ProgressiveSource:               1      // Indicates if content is progressive
-InterlacedSource:                0      // Indicates if content is interlaced
 ChromaLocInfoPresent:            1      // enable presence of chroma location information
 ChromaSampleLocTypeTopField:     2      // the location of chroma sample top field
 ChromaSampleLocTypeBottomField:  2      // the location of chroma sample bottom field
 ChromaSampleLocType:             2      // the location of chroma sample frame
-OverscanInfoPresent:             1      // enable presence of overscan information
-OverscanAppropriate:             0      // indicates if the cropped decoded pictures output are suitable for display using overscan
 
diff --git a/doc/software-manual.tex b/doc/software-manual.tex
index 59b73d6773..a2d36e6b5d 100644
--- a/doc/software-manual.tex
+++ b/doc/software-manual.tex
@@ -1018,18 +1018,6 @@ Specifies the value of general_one_picture_only_constraint_flag.
 Specifies the value of general_lower_bit_constraint_flag to use for RExt profiles.
 \\
 
-\Option{NonPackedSource} &
-%\ShortOption{\None} &
-\Default{false} &
-Specifies the value of general_non_packed_constraint_flag
-\\
-
-\Option{NonProjectedConstraintFlag} &
-%\ShortOption{\None} &
-\Default{false} &
-Specifies the value of general_non_projected_constraint_flag
-\\
-
 \Option{NoResChangeInClvsConstraintFlag} &
 %\ShortOption{\None} &
 \Default{false} &
@@ -2918,6 +2906,16 @@ Specifies the value of general_progressive_source_flag
 \Default{false} &
 Specifies the value of general_interlaced_source_flag 
 \\
+\Option{NonPackedSource} &
+%\ShortOption{\None} &
+\Default{false} &
+Specifies the value of general_non_packed_constraint_flag
+\\
+\Option{NonProjectedConstraintFlag} &
+%\ShortOption{\None} &
+\Default{false} &
+Specifies the value of general_non_projected_constraint_flag
+\\
 \Option{ChromaLocInfoPresent} &
 \Default{false} &
 Signals whether chroma_sample_loc_type_top_field and chroma_sample_loc_type_bottom_field are present.
diff --git a/source/App/EncoderApp/EncAppCfg.cpp b/source/App/EncoderApp/EncAppCfg.cpp
index 6f9c6a55b6..619895ecf0 100644
--- a/source/App/EncoderApp/EncAppCfg.cpp
+++ b/source/App/EncoderApp/EncAppCfg.cpp
@@ -821,8 +821,10 @@ bool EncAppCfg::parseCfg( int argc, char* argv[] )
   ("OnePictureOnlyConstraintFlag",                    m_onePictureOnlyConstraintFlag,                   false, "Value of general_intra_constraint_flag. Can only be used for single frame encodings. Will be set to true for still picture profiles")
   ("IntraConstraintFlag",                             m_intraConstraintFlag,                            false, "Value of intra_only_constraint_flag")
 
+#if !JVET_S0266_VUI_length
   ("NonPackedSource",                                 m_nonPackedConstraintFlag,                        false, "Indicate that source does not contain frame packing")
   ("NonProjectedConstraintFlag",                      m_nonProjectedConstraintFlag,                     false, "Indicate that the bitstream contains projection SEI messages")
+#endif
   ("NoResChangeInClvsConstraintFlag",                 m_noResChangeInClvsConstraintFlag,                false, "Indicate that the picture spatial resolution does not change within any CLVS referring to the SPS")
   ("SingleLayerConstraintFlag",                       m_singleLayerConstraintFlag,                     false, "Indicate that the bitstream contains only one layer")
   ("AllLayersIndependentConstraintFlag",              m_allLayersIndependentConstraintFlag,            false, "Indicate that all layers are independent")
@@ -1177,6 +1179,10 @@ bool EncAppCfg::parseCfg( int argc, char* argv[] )
   ("MatrixCoefficients",                              m_matrixCoefficients,                                 2, "Describes the matrix coefficients used in deriving luma and chroma from RGB primaries")
   ("ProgressiveSource",                               m_progressiveSourceFlag,                          false, "Indicate that source is progressive")
   ("InterlacedSource",                                m_interlacedSourceFlag,                           false, "Indicate that source is interlaced")
+#if JVET_S0266_VUI_length
+  ("NonPackedSource",                                 m_nonPackedConstraintFlag,                        false, "Indicate that source does not contain frame packing")
+  ("NonProjectedConstraintFlag",                      m_nonProjectedConstraintFlag,                     false, "Indicate that the bitstream contains projection SEI messages")
+#endif
   ("ChromaLocInfoPresent",                            m_chromaLocInfoPresentFlag,                       false, "Signals whether chroma_sample_loc_type_top_field and chroma_sample_loc_type_bottom_field are present")
   ("ChromaSampleLocTypeTopField",                     m_chromaSampleLocTypeTopField,                        0, "Specifies the location of chroma samples for top field")
   ("ChromaSampleLocTypeBottomField",                  m_chromaSampleLocTypeBottomField,                     0, "Specifies the location of chroma samples for bottom field")
diff --git a/source/Lib/CommonLib/Slice.cpp b/source/Lib/CommonLib/Slice.cpp
index 4901051d25..6c52755318 100644
--- a/source/Lib/CommonLib/Slice.cpp
+++ b/source/Lib/CommonLib/Slice.cpp
@@ -4318,7 +4318,9 @@ bool Slice::checkRPR()
 
 bool             operator == (const ConstraintInfo& op1, const ConstraintInfo& op2)
 {
+#if !JVET_S0266_VUI_length
   if( op1.m_nonPackedConstraintFlag                      != op2.m_nonPackedConstraintFlag                        ) return false;
+#endif
   if( op1.m_frameOnlyConstraintFlag                      != op2.m_frameOnlyConstraintFlag                        ) return false;
   if( op1.m_intraOnlyConstraintFlag                      != op2.m_intraOnlyConstraintFlag                        ) return false;
   if( op1.m_maxBitDepthConstraintIdc                     != op2.m_maxBitDepthConstraintIdc                       ) return false;
diff --git a/source/Lib/CommonLib/Slice.h b/source/Lib/CommonLib/Slice.h
index 43b5355996..4cb16aa32c 100644
--- a/source/Lib/CommonLib/Slice.h
+++ b/source/Lib/CommonLib/Slice.h
@@ -238,8 +238,10 @@ class ConstraintInfo
 #if JVET_S0179_CONDITIONAL_SIGNAL_GCI
   bool              m_gciPresentFlag;
 #endif
+#if !JVET_S0266_VUI_length
   bool              m_nonPackedConstraintFlag;
   bool              m_nonProjectedConstraintFlag;
+#endif
   bool              m_noResChangeInClvsConstraintFlag;
   bool              m_oneTilePerPicConstraintFlag;
   bool              m_picHeaderInSliceHeaderConstraintFlag;
@@ -305,12 +307,15 @@ public:
   ConstraintInfo()
 #if JVET_S0179_CONDITIONAL_SIGNAL_GCI
     : m_gciPresentFlag(true)
+#if !JVET_S0266_VUI_length
     , m_nonPackedConstraintFlag(false)
+    , m_nonProjectedConstraintFlag(false)
+#endif
 #else
     : m_nonPackedConstraintFlag (false)
-#endif
     , m_nonProjectedConstraintFlag(false)
     , m_noResChangeInClvsConstraintFlag(false)
+#endif
     , m_oneTilePerPicConstraintFlag(false)
     , m_picHeaderInSliceHeaderConstraintFlag(false)
     , m_oneSlicePerPicConstraintFlag(false)
@@ -378,8 +383,10 @@ public:
   void          setGciPresentFlag(bool b) { m_gciPresentFlag = b; }
 #endif
 
+#if !JVET_S0266_VUI_length
   bool          getNonPackedConstraintFlag() const { return m_nonPackedConstraintFlag; }
   void          setNonPackedConstraintFlag(bool b) { m_nonPackedConstraintFlag = b; }
+#endif
 
   bool          getFrameOnlyConstraintFlag() const { return m_frameOnlyConstraintFlag; }
   void          setFrameOnlyConstraintFlag(bool b) { m_frameOnlyConstraintFlag = b; }
@@ -390,8 +397,10 @@ public:
   ChromaFormat  getMaxChromaFormatConstraintIdc() const { return m_maxChromaFormatConstraintIdc; }
   void          setMaxChromaFormatConstraintIdc(ChromaFormat fmt) { m_maxChromaFormatConstraintIdc = fmt; }
 
+#if !JVET_S0266_VUI_length
   bool          getNonProjectedConstraintFlag() const { return m_nonProjectedConstraintFlag; }
   void          setNonProjectedConstraintFlag(bool b) { m_nonProjectedConstraintFlag = b; }
+#endif
 
   bool          getNoResChangeInClvsConstraintFlag() const { return m_noResChangeInClvsConstraintFlag; }
   void          setNoResChangeInClvsConstraintFlag(bool b) { m_noResChangeInClvsConstraintFlag = b; }
@@ -1120,6 +1129,28 @@ public:
 class VUI
 {
 private:
+#if JVET_S0266_VUI_length
+  bool       m_progressiveSourceFlag;
+  bool       m_interlacedSourceFlag;
+  bool       m_nonPackedFlag;
+  bool       m_nonProjectedFlag;
+  bool       m_aspectRatioInfoPresentFlag;
+  bool       m_aspectRatioConstantFlag;
+  int        m_aspectRatioIdc;
+  int        m_sarWidth;
+  int        m_sarHeight;
+  bool       m_overscanInfoPresentFlag;
+  bool       m_overscanAppropriateFlag;
+  bool       m_colourDescriptionPresentFlag;
+  int        m_colourPrimaries;
+  int        m_transferCharacteristics;
+  int        m_matrixCoefficients;
+  bool       m_videoFullRangeFlag;
+  bool       m_chromaLocInfoPresentFlag;
+  int        m_chromaSampleLocTypeTopField;
+  int        m_chromaSampleLocTypeBottomField;
+  int        m_chromaSampleLocType;
+#else
   bool       m_aspectRatioInfoPresentFlag;
   bool       m_aspectRatioConstantFlag;
   int        m_aspectRatioIdc;
@@ -1138,9 +1169,32 @@ private:
   bool       m_overscanInfoPresentFlag;
   bool       m_overscanAppropriateFlag;
   bool       m_videoFullRangeFlag;
+#endif
 
 public:
   VUI()
+#if JVET_S0266_VUI_length
+    : m_progressiveSourceFlag             (false) // Default values as documented in VVC D10 are used
+    , m_interlacedSourceFlag              (false)
+    , m_nonPackedFlag                     (false)
+    , m_nonProjectedFlag                  (false)
+    , m_aspectRatioInfoPresentFlag        (false) 
+    , m_aspectRatioConstantFlag           (false)
+    , m_aspectRatioIdc                    (0)
+    , m_sarWidth                          (0)
+    , m_sarHeight                         (0)
+    , m_overscanInfoPresentFlag           (false)
+    , m_overscanAppropriateFlag           (false)
+    , m_colourDescriptionPresentFlag      (false)
+    , m_colourPrimaries                   (2)
+    , m_transferCharacteristics           (2)
+    , m_matrixCoefficients                (2)
+    , m_videoFullRangeFlag                (false)
+    , m_chromaLocInfoPresentFlag          (false)
+    , m_chromaSampleLocTypeTopField       (6)
+    , m_chromaSampleLocTypeBottomField    (6)
+    , m_chromaSampleLocType               (6)
+#else
     : m_aspectRatioInfoPresentFlag        (false) //TODO: This initialiser list contains magic numbers
     , m_aspectRatioConstantFlag           (true)
     , m_aspectRatioIdc                    (0)
@@ -1159,6 +1213,7 @@ public:
     , m_overscanInfoPresentFlag           (false)
     , m_overscanAppropriateFlag           (false)
     , m_videoFullRangeFlag                (false)
+#endif
   {}
 
   virtual           ~VUI() {}
@@ -1195,6 +1250,14 @@ public:
   bool              getInterlacedSourceFlag() const                        { return m_interlacedSourceFlag; }
   void              setInterlacedSourceFlag(bool b)                        { m_interlacedSourceFlag = b; }
 
+#if JVET_S0266_VUI_length
+  bool              getNonPackedFlag() const                               { return m_nonPackedFlag; }
+  void              setNonPackedFlag(bool b)                               { m_nonPackedFlag = b; }
+
+  bool              getNonProjectedFlag() const                            { return m_nonProjectedFlag; }
+  void              setNonProjectedFlag(bool b)                            { m_nonProjectedFlag = b; }
+#endif
+
   bool              getChromaLocInfoPresentFlag() const                    { return m_chromaLocInfoPresentFlag;             }
   void              setChromaLocInfoPresentFlag(bool i)                    { m_chromaLocInfoPresentFlag = i;                }
 
@@ -1394,6 +1457,9 @@ private:
 
   bool              m_fieldSeqFlag;
   bool              m_vuiParametersPresentFlag;
+#if JVET_S0266_VUI_length
+  unsigned          m_vuiPayloadSize;
+#endif
   VUI               m_vuiParameters;
 
   SPSRExt           m_spsRangeExtension;
@@ -1731,6 +1797,10 @@ void                    setCCALFEnabledFlag( bool b )
   void                    setFieldSeqFlag(bool i)                                                         { m_fieldSeqFlag = i;                            }
   bool                    getVuiParametersPresentFlag() const                                             { return m_vuiParametersPresentFlag;                                   }
   void                    setVuiParametersPresentFlag(bool b)                                             { m_vuiParametersPresentFlag = b;                                      }
+#if JVET_S0266_VUI_length
+  unsigned                getVuiPayloadSize() const                                                       { return m_vuiPayloadSize; }
+  void                    setVuiPayloadSize(unsigned i)                                                   { m_vuiPayloadSize = i; }
+#endif
   VUI*                    getVuiParameters()                                                              { return &m_vuiParameters;                                             }
   const VUI*              getVuiParameters() const                                                        { return &m_vuiParameters;                                             }
   const ProfileTierLevel* getProfileTierLevel() const                                                     { return &m_profileTierLevel; }
diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index 516deaedc0..e4aeb66a82 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -53,6 +53,8 @@
 //########### place macros to be removed in next cycle below this line ###############
 #define JVET_S0133_PH_SYNTAX_OVERRIDE_ENC_FIX             1 // JVET-S0133: Encoder-only fix on the override of partition constriants in PH
 
+#define JVET_S0266_VUI_length                             1 // JVET-S0266: VUI modifications including signalling of VUI length
+
 #define JVET_S0179_CONDITIONAL_SIGNAL_GCI                 1 // JVET-S0179: Conditional signalling of GCI fields
 
 #define JVET_S0049_ASPECT4                                1 // JVET-S0049 aspect 4: Constrain the value of pps_alf_info_in_ph_flag to be equal to 0 when the PH is in the SH
@@ -75,7 +77,6 @@
                                                             //             Aspect 9 The value of ph_poc_msb_cycle_present_flag is required to be equal to 0 when vps_independent_layer_flag[GeneralLayerIdx[nuh_layer_id]] is equal to 0 and there is an ILRP entry in RefPicList[0] or RefPicList[1] of a slice of the current picture
 
 
-
 //########### place macros to be be kept below this line ###############
 #define JVET_S0257_DUMP_360SEI_MESSAGE                    1 // Software support of 360 SEI messages
 
diff --git a/source/Lib/DecoderLib/VLCReader.cpp b/source/Lib/DecoderLib/VLCReader.cpp
index bed0f24c21..1fea6882a4 100644
--- a/source/Lib/DecoderLib/VLCReader.cpp
+++ b/source/Lib/DecoderLib/VLCReader.cpp
@@ -1097,12 +1097,20 @@ void  HLSyntaxReader::parseVUI(VUI* pcVUI, SPS *pcSPS)
 #if ENABLE_TRACING
   DTRACE( g_trace_ctx, D_HEADER, "----------- vui_parameters -----------\n");
 #endif
-
+#if JVET_S0266_VUI_length
+  unsigned vuiPayloadSize = pcSPS->getVuiPayloadSize();
+  InputBitstream *bs = getBitstream();
+  setBitstream(bs->extractSubstream(vuiPayloadSize * 8));
+#endif
 
   uint32_t  symbol;
 
-  READ_FLAG(symbol,  "vui_general_progressive_source_flag"          ); pcVUI->setProgressiveSourceFlag(symbol ? true : false);
-  READ_FLAG(symbol,  "vui_general_interlaced_source_flag"           ); pcVUI->setInterlacedSourceFlag(symbol ? true : false);
+  READ_FLAG(symbol,  "vui_progressive_source_flag"          ); pcVUI->setProgressiveSourceFlag(symbol ? true : false);
+  READ_FLAG(symbol,  "vui_interlaced_source_flag"           ); pcVUI->setInterlacedSourceFlag(symbol ? true : false);
+#if JVET_S0266_VUI_length
+  READ_FLAG(symbol, "vui_non_packed_constraint_flag");         pcVUI->setNonPackedFlag(symbol ? true : false);
+  READ_FLAG(symbol, "vui_non_projected_constraint_flag");      pcVUI->setNonProjectedFlag(symbol ? true : false);
+#endif
   READ_FLAG( symbol, "vui_aspect_ratio_info_present_flag");           pcVUI->setAspectRatioInfoPresentFlag(symbol);
   if (pcVUI->getAspectRatioInfoPresentFlag())
   {
@@ -1127,7 +1135,7 @@ void  HLSyntaxReader::parseVUI(VUI* pcVUI, SPS *pcSPS)
     READ_CODE(8, symbol, "vui_colour_primaries");                       pcVUI->setColourPrimaries(symbol);
     READ_CODE(8, symbol, "vui_transfer_characteristics");               pcVUI->setTransferCharacteristics(symbol);
     READ_CODE(8, symbol, "vui_matrix_coeffs");                          pcVUI->setMatrixCoefficients(symbol);
-    READ_FLAG(   symbol, "vui_video_full_range_flag");                    pcVUI->setVideoFullRangeFlag(symbol);
+    READ_FLAG(   symbol, "vui_full_range_flag");                    pcVUI->setVideoFullRangeFlag(symbol);
   }
 
   READ_FLAG(     symbol, "vui_chroma_loc_info_present_flag");             pcVUI->setChromaLocInfoPresentFlag(symbol);
@@ -1144,6 +1152,40 @@ void  HLSyntaxReader::parseVUI(VUI* pcVUI, SPS *pcSPS)
     }
   }
 
+#if JVET_S0266_VUI_length
+  int payloadBitsRem = getBitstream()->getNumBitsLeft();
+  if(payloadBitsRem)      //Corresponds to more_data_in_payload()
+  {
+    while(payloadBitsRem > 9)    //payload_extension_present()
+    {
+      READ_CODE(1, symbol, "vui_reserved_payload_extension_data");
+      payloadBitsRem--;
+    }
+    int finalBits = getBitstream()->peekBits(payloadBitsRem);
+    int numFinalZeroBits = 0;
+    int mask = 0xff;
+    while(finalBits & (mask >> numFinalZeroBits))
+    {
+      numFinalZeroBits++;
+    }
+    while(payloadBitsRem > 9-numFinalZeroBits)     //payload_extension_present()
+    {
+      READ_CODE(1, symbol, "vui_reserved_payload_extension_data");
+      payloadBitsRem--;
+    }
+    READ_FLAG(symbol, "vui_payload_bit_equal_to_one");
+    CHECK(symbol != 1, "vui_payload_bit_equal_to_one not equal to 1");
+    payloadBitsRem--;
+    while(payloadBitsRem)
+    {
+      READ_FLAG(symbol, "vui_payload_bit_equal_to_zero");
+      CHECK(symbol != 0, "vui_payload_bit_equal_to_zero not equal to 0");
+      payloadBitsRem--;
+    }
+  }
+  delete getBitstream();
+  setBitstream(bs);
+#endif
 }
 
 void HLSyntaxReader::parseGeneralHrdParameters(GeneralHrdParams *hrd)
@@ -2015,6 +2057,15 @@ void HLSyntaxReader::parseSPS(SPS* pcSPS)
 
   if (pcSPS->getVuiParametersPresentFlag())
   {
+#if JVET_S0266_VUI_length
+    READ_UVLC(uiCode, "sps_vui_payload_size_minus1");
+    pcSPS->setVuiPayloadSize(uiCode+1);
+    while (!isByteAligned())
+    {
+      READ_FLAG(uiCode, "sps_vui_alignment_zero_bit");
+      CHECK(uiCode != 0, "sps_vui_alignment_zero_bit not equal to 0");
+    }
+#endif
     parseVUI(pcSPS->getVuiParameters(), pcSPS);
   }
 
@@ -4345,9 +4396,13 @@ void HLSyntaxReader::parseConstraintInfo(ConstraintInfo *cinfo)
   if (cinfo->getGciPresentFlag())
   {
 #endif
+#if !JVET_S0266_VUI_length
     READ_FLAG(symbol,  "general_non_packed_constraint_flag"       ); cinfo->setNonPackedConstraintFlag(symbol ? true : false);
+#endif
     READ_FLAG(symbol,  "general_frame_only_constraint_flag"       ); cinfo->setFrameOnlyConstraintFlag(symbol ? true : false);
+#if !JVET_S0266_VUI_length
     READ_FLAG(symbol,  "general_non_projected_constraint_flag"    ); cinfo->setNonProjectedConstraintFlag(symbol ? true : false);
+#endif
     READ_FLAG(symbol,  "general_one_picture_only_constraint_flag"    ); cinfo->setOnePictureOnlyConstraintFlag(symbol ? true : false);
     READ_FLAG(symbol,  "intra_only_constraint_flag"               ); cinfo->setIntraOnlyConstraintFlag(symbol ? true : false);
 
diff --git a/source/Lib/EncoderLib/EncLib.cpp b/source/Lib/EncoderLib/EncLib.cpp
index 7b60e0c3f0..49ab6fcc59 100644
--- a/source/Lib/EncoderLib/EncLib.cpp
+++ b/source/Lib/EncoderLib/EncLib.cpp
@@ -1093,8 +1093,10 @@ void EncLib::xInitSPS( SPS& sps )
 #if JVET_S0179_CONDITIONAL_SIGNAL_GCI
   cinfo->setGciPresentFlag(m_gciPresentFlag);
 #endif
+#if !JVET_S0266_VUI_length
   cinfo->setNonPackedConstraintFlag     (m_nonPackedConstraintFlag);
   cinfo->setNonProjectedConstraintFlag(m_nonProjectedConstraintFlag);
+#endif
   cinfo->setNoResChangeInClvsConstraintFlag(m_noResChangeInClvsConstraintFlag);
   cinfo->setOneTilePerPicConstraintFlag(m_oneTilePerPicConstraintFlag);
   cinfo->setPicHeaderInSliceHeaderConstraintFlag(m_picHeaderInSliceHeaderConstraintFlag);
@@ -1324,6 +1326,10 @@ void EncLib::xInitSPS( SPS& sps )
     pcVUI->setMatrixCoefficients(getMatrixCoefficients());
     pcVUI->setProgressiveSourceFlag       (getProgressiveSourceFlag());
     pcVUI->setInterlacedSourceFlag        (getInterlacedSourceFlag());
+#if JVET_S0266_VUI_length
+    pcVUI->setNonPackedFlag               (getNonPackedConstraintFlag());
+    pcVUI->setNonProjectedFlag            (getNonProjectedConstraintFlag());
+#endif
     pcVUI->setChromaLocInfoPresentFlag(getChromaLocInfoPresentFlag());
     pcVUI->setChromaSampleLocTypeTopField(getChromaSampleLocTypeTopField());
     pcVUI->setChromaSampleLocTypeBottomField(getChromaSampleLocTypeBottomField());
diff --git a/source/Lib/EncoderLib/VLCWriter.cpp b/source/Lib/EncoderLib/VLCWriter.cpp
index 1a1e8915ee..65d77713d1 100644
--- a/source/Lib/EncoderLib/VLCWriter.cpp
+++ b/source/Lib/EncoderLib/VLCWriter.cpp
@@ -646,12 +646,19 @@ void HLSWriter::codeScalingListAps( APS* pcAPS )
 void HLSWriter::codeVUI( const VUI *pcVUI, const SPS* pcSPS )
 {
 #if ENABLE_TRACING
-  DTRACE( g_trace_ctx, D_HEADER, "----------- vui_parameters -----------\n");
+  if( g_HLSTraceEnable )
+  {
+    DTRACE( g_trace_ctx, D_HEADER, "----------- vui_parameters -----------\n");
+  }
 #endif
 
 
-  WRITE_FLAG(pcVUI->getProgressiveSourceFlag(),   "vui_general_progressive_source_flag"         );
-  WRITE_FLAG(pcVUI->getInterlacedSourceFlag(),    "vui_general_interlaced_source_flag"          );
+  WRITE_FLAG(pcVUI->getProgressiveSourceFlag(),   "vui_progressive_source_flag"         );
+  WRITE_FLAG(pcVUI->getInterlacedSourceFlag(),    "vui_interlaced_source_flag"          );
+#if JVET_S0266_VUI_length
+  WRITE_FLAG(pcVUI->getNonPackedFlag(),           "vui_non_packed_constraint_flag");
+  WRITE_FLAG(pcVUI->getNonProjectedFlag(),        "vui_non_projected_constraint_flag");
+#endif
   WRITE_FLAG(pcVUI->getAspectRatioInfoPresentFlag(),            "vui_aspect_ratio_info_present_flag");
   if (pcVUI->getAspectRatioInfoPresentFlag())
   {
@@ -674,7 +681,7 @@ void HLSWriter::codeVUI( const VUI *pcVUI, const SPS* pcSPS )
     WRITE_CODE(pcVUI->getColourPrimaries(), 8,                "vui_colour_primaries");
     WRITE_CODE(pcVUI->getTransferCharacteristics(), 8,        "vui_transfer_characteristics");
     WRITE_CODE(pcVUI->getMatrixCoefficients(), 8,             "vui_matrix_coeffs");
-    WRITE_FLAG(pcVUI->getVideoFullRangeFlag(),                "vui_video_full_range_flag");
+    WRITE_FLAG(pcVUI->getVideoFullRangeFlag(),                "vui_full_range_flag");
   }
   WRITE_FLAG(pcVUI->getChromaLocInfoPresentFlag(),              "vui_chroma_loc_info_present_flag");
   if (pcVUI->getChromaLocInfoPresentFlag())
@@ -689,6 +696,16 @@ void HLSWriter::codeVUI( const VUI *pcVUI, const SPS* pcSPS )
       WRITE_UVLC(pcVUI->getChromaSampleLocTypeBottomField(),      "vui_chroma_sample_loc_type_bottom_field");
     }
   }
+#if JVET_S0266_VUI_length
+  if(!isByteAligned())
+  {
+    WRITE_FLAG(1, "vui_payload_bit_equal_to_one");
+    while(!isByteAligned())
+    {
+      WRITE_FLAG(0, "vui_payload_bit_equal_to_zero");
+    }
+  }
+#endif
 }
 
 void HLSWriter::codeGeneralHrdparameters(const GeneralHrdParams * hrd)
@@ -1238,6 +1255,27 @@ void HLSWriter::codeSPS( const SPS* pcSPS )
   WRITE_FLAG( pcSPS->getVuiParametersPresentFlag(),            "vui_parameters_present_flag" );
   if (pcSPS->getVuiParametersPresentFlag())
   {
+#if JVET_S0266_VUI_length
+    OutputBitstream *bs = getBitstream();
+    OutputBitstream bs_count;
+    setBitstream(&bs_count);
+#if ENABLE_TRACING
+    bool traceEnable = g_HLSTraceEnable;
+    g_HLSTraceEnable = false;
+#endif
+    codeVUI(pcSPS->getVuiParameters(), pcSPS);
+#if ENABLE_TRACING
+    g_HLSTraceEnable = traceEnable;
+#endif
+    unsigned vui_payload_data_num_bits = bs_count.getNumberOfWrittenBits();
+    CHECK( vui_payload_data_num_bits % 8 != 0, "Invalid number of VUI payload data bits" );
+    setBitstream(bs);
+    WRITE_UVLC((vui_payload_data_num_bits >> 3) - 1, "sps_vui_payload_size_minus1");
+    while (!isByteAligned())
+    {
+      WRITE_FLAG(0, "sps_vui_alignment_zero_bit");
+    }
+#endif
     codeVUI(pcSPS->getVuiParameters(), pcSPS);
   }
 
@@ -2512,9 +2550,13 @@ void  HLSWriter::codeConstraintInfo  ( const ConstraintInfo* cinfo )
   if (cinfo->getGciPresentFlag())
   {
 #endif
+#if !JVET_S0266_VUI_length
     WRITE_FLAG(cinfo->getNonPackedConstraintFlag(), "general_non_packed_constraint_flag"      );
+#endif
     WRITE_FLAG(cinfo->getFrameOnlyConstraintFlag(), "general_frame_only_constraint_flag"      );
+#if !JVET_S0266_VUI_length
     WRITE_FLAG(cinfo->getNonProjectedConstraintFlag(), "general_non_projected_constraint_flag");
+#endif
     WRITE_FLAG(cinfo->getOnePictureOnlyConstraintFlag(), "general_one_picture_only_constraint_flag" );
     WRITE_FLAG(cinfo->getIntraOnlyConstraintFlag(),     "intra_only_constraint_flag"      );
 
diff --git a/source/Lib/EncoderLib/VLCWriter.h b/source/Lib/EncoderLib/VLCWriter.h
index bc2b28d18d..c044001e67 100644
--- a/source/Lib/EncoderLib/VLCWriter.h
+++ b/source/Lib/EncoderLib/VLCWriter.h
@@ -77,6 +77,9 @@ protected:
   virtual ~VLCWriter() {}
 
   void  setBitstream          ( OutputBitstream* p )  { m_pcBitIf = p;  }
+#if JVET_S0266_VUI_length
+  OutputBitstream* getBitstream( )                    { return m_pcBitIf; }
+#endif
   void  xWriteSCode           ( int  code,  uint32_t length );
   void  xWriteCode            ( uint32_t uiCode, uint32_t uiLength );
   void  xWriteUvlc            ( uint32_t uiCode );
-- 
GitLab