From b6d80f2b99d6d3b4f4cbfd44c562c1e3fb8c438b Mon Sep 17 00:00:00 2001
From: Brian Heng <brian.heng@broadcom.com>
Date: Thu, 12 Nov 2020 11:35:23 -0800
Subject: [PATCH] Fix condition for coding weighted prediction  num_l1_weights.

   - See ticket #1404
---
 source/Lib/DecoderLib/VLCReader.cpp |  6 +++---
 source/Lib/DecoderLib/VLCReader.h   |  2 +-
 source/Lib/EncoderLib/VLCWriter.cpp | 10 +++++++---
 source/Lib/EncoderLib/VLCWriter.h   |  2 +-
 4 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/source/Lib/DecoderLib/VLCReader.cpp b/source/Lib/DecoderLib/VLCReader.cpp
index 500357231..2d125860e 100644
--- a/source/Lib/DecoderLib/VLCReader.cpp
+++ b/source/Lib/DecoderLib/VLCReader.cpp
@@ -3208,7 +3208,7 @@ void HLSyntaxReader::parsePictureHeader( PicHeader* picHeader, ParameterSetManag
 
     if( (pps->getUseWP() || pps->getWPBiPred()) && pps->getWpInfoInPhFlag() )
     {
-      parsePredWeightTable(picHeader, sps);
+      parsePredWeightTable(picHeader, pps, sps);
     }
   }
   // inherit constraint values from SPS
@@ -4767,7 +4767,7 @@ void HLSyntaxReader::parsePredWeightTable( Slice* pcSlice, const SPS *sps )
   CHECK(uiTotalSignalledWeightFlags>24, "Too many weight flag signalled");
 }
 
-void HLSyntaxReader::parsePredWeightTable(PicHeader *picHeader, const SPS *sps)
+void HLSyntaxReader::parsePredWeightTable(PicHeader *picHeader, const PPS *pps, const SPS *sps)
 {
   WPScalingParam *   wp;
   const ChromaFormat chFmt                     = sps->getChromaFormatIdc();
@@ -4898,7 +4898,7 @@ void HLSyntaxReader::parsePredWeightTable(PicHeader *picHeader, const SPS *sps)
 
     if (numRef == 0)
     {
-      if (picHeader->getRPL(1)->getNumRefEntries() > 0)
+      if (pps->getWPBiPred() && picHeader->getRPL(1)->getNumRefEntries() > 0)
       {
         READ_UVLC(numLxWeights, "num_l1_weights");
       }
diff --git a/source/Lib/DecoderLib/VLCReader.h b/source/Lib/DecoderLib/VLCReader.h
index 5c2534500..dfd0b53ad 100644
--- a/source/Lib/DecoderLib/VLCReader.h
+++ b/source/Lib/DecoderLib/VLCReader.h
@@ -180,7 +180,7 @@ public:
   void  parseRemainingBytes ( bool noTrailingBytesExpected );
 
   void  parsePredWeightTable( Slice* pcSlice, const SPS *sps );
-  void parsePredWeightTable ( PicHeader *picHeader, const SPS *sps );
+  void parsePredWeightTable ( PicHeader *picHeader, const PPS *pps, const SPS *sps );
   void parseScalingList     ( ScalingList *scalingList, bool aps_chromaPresentFlag );
   void  decodeScalingList   ( ScalingList *scalingList, uint32_t scalingListId, bool isPredictor);
   void parseReshaper        ( SliceReshapeInfo& sliceReshaperInfo, const SPS* pcSPS, const bool isIntra );
diff --git a/source/Lib/EncoderLib/VLCWriter.cpp b/source/Lib/EncoderLib/VLCWriter.cpp
index 84628f2c1..8b3531db3 100644
--- a/source/Lib/EncoderLib/VLCWriter.cpp
+++ b/source/Lib/EncoderLib/VLCWriter.cpp
@@ -2006,7 +2006,7 @@ WRITE_FLAG(picHeader->getGdrOrIrapPicFlag(), "ph_gdr_or_irap_pic_flag");
 
     if ((pps->getUseWP() || pps->getWPBiPred()) && pps->getWpInfoInPhFlag())
     {
-      xCodePredWeightTable(picHeader, sps);
+      xCodePredWeightTable(picHeader, pps, sps);
     }
   }
   // inherit constraint values from SPS
@@ -2835,7 +2835,7 @@ void HLSWriter::xCodePredWeightTable( Slice* pcSlice )
   }
 }
 
-void HLSWriter::xCodePredWeightTable(PicHeader *picHeader, const SPS *sps)
+void HLSWriter::xCodePredWeightTable(PicHeader *picHeader, const PPS *pps, const SPS *sps)
 {
   WPScalingParam *   wp;
   const ChromaFormat format                      = sps->getChromaFormatIdc();
@@ -2915,7 +2915,11 @@ void HLSWriter::xCodePredWeightTable(PicHeader *picHeader, const SPS *sps)
     if (numRef == 0)
     {
       numLxWeights         = picHeader->getNumL1Weights();
-      if (picHeader->getRPL(1)->getNumRefEntries() > 0)
+      if (pps->getWPBiPred() == 0) 
+      {
+        numLxWeights = 0;
+      }
+      else if (picHeader->getRPL(1)->getNumRefEntries() > 0)
       {
         WRITE_UVLC(numLxWeights, "num_l1_weights");
       }
diff --git a/source/Lib/EncoderLib/VLCWriter.h b/source/Lib/EncoderLib/VLCWriter.h
index 5496858cc..db61a5196 100644
--- a/source/Lib/EncoderLib/VLCWriter.h
+++ b/source/Lib/EncoderLib/VLCWriter.h
@@ -125,7 +125,7 @@ private:
   void xCodeRefPicList( const ReferencePictureList* rpl, bool isLongTermPresent, uint32_t ltLsbBitsCount, const bool isForbiddenZeroDeltaPoc, int rplIdx);
   bool xFindMatchingLTRP        ( Slice* pcSlice, uint32_t *ltrpsIndex, int ltrpPOC, bool usedFlag );
   void xCodePredWeightTable     ( Slice* pcSlice );
-  void xCodePredWeightTable     ( PicHeader *picHeader, const SPS *sps );
+  void xCodePredWeightTable     ( PicHeader *picHeader, const PPS *pps, const SPS *sps );
   void xCodeScalingList         ( const ScalingList* scalingList, uint32_t scalinListId, bool isPredictor);
 public:
   void  setBitstream            ( OutputBitstream* p )  { m_pcBitIf = p;  }
-- 
GitLab