From 1a04b7dd37e627d83e725e98a622cefa14952eaf Mon Sep 17 00:00:00 2001
From: Frank Bossen <fbossen@gmail.com>
Date: Wed, 29 Jul 2020 13:22:58 -0400
Subject: [PATCH] Cleanup weighted predition related code

- Rename members of WPScalingParam to conform to guidelines
- Avoid casts from const to non-const types
- Modify getWpScaling function to handle case where the reference
  index is negative (see #733)
- Add helper function isWeighted to determine whether weighting
  parameters are present for any colour component
- Fix some indentation issues
---
 source/Lib/CommonLib/InterPrediction.cpp     |  22 ++--
 source/Lib/CommonLib/Slice.cpp               |  85 ++++++++----
 source/Lib/CommonLib/Slice.h                 |  21 ++-
 source/Lib/CommonLib/UnitTools.cpp           |  58 ++++-----
 source/Lib/CommonLib/WeightPrediction.cpp    |  44 +++----
 source/Lib/CommonLib/WeightPrediction.h      |   8 +-
 source/Lib/DecoderLib/VLCReader.cpp          | 128 +++++++++----------
 source/Lib/EncoderLib/InterSearch.cpp        |  15 +--
 source/Lib/EncoderLib/VLCWriter.cpp          |  87 +++++++------
 source/Lib/EncoderLib/WeightPredAnalysis.cpp | 124 +++++++++---------
 10 files changed, 314 insertions(+), 278 deletions(-)

diff --git a/source/Lib/CommonLib/InterPrediction.cpp b/source/Lib/CommonLib/InterPrediction.cpp
index f18315056..d2dc6516f 100644
--- a/source/Lib/CommonLib/InterPrediction.cpp
+++ b/source/Lib/CommonLib/InterPrediction.cpp
@@ -517,12 +517,12 @@ void InterPrediction::xPredInterBi(PredictionUnit &pu, PelUnitBuf &pcYuvPred, co
   const PPS   &pps   = *pu.cs->pps;
   const Slice &slice = *pu.cs->slice;
   CHECK( !pu.cu->affine && pu.refIdx[0] >= 0 && pu.refIdx[1] >= 0 && ( pu.lwidth() + pu.lheight() == 12 ), "invalid 4x8/8x4 bi-predicted blocks" );
-  WPScalingParam *wp0;
-  WPScalingParam *wp1;
+
   int refIdx0 = pu.refIdx[REF_PIC_LIST_0];
   int refIdx1 = pu.refIdx[REF_PIC_LIST_1];
-  pu.cs->slice->getWpScaling(REF_PIC_LIST_0, refIdx0, wp0);
-  pu.cs->slice->getWpScaling(REF_PIC_LIST_1, refIdx1, wp1);
+
+  const WPScalingParam *wp0 = pu.cs->slice->getWpScaling(REF_PIC_LIST_0, refIdx0);
+  const WPScalingParam *wp1 = pu.cs->slice->getWpScaling(REF_PIC_LIST_1, refIdx1);
 
   bool bioApplied = false;
   if (pu.cs->sps->getBDOFEnabledFlag() && (!pu.cs->picHeader->getDisBdofFlag()))
@@ -533,7 +533,8 @@ void InterPrediction::xPredInterBi(PredictionUnit &pu, PelUnitBuf &pcYuvPred, co
     }
     else
     {
-      const bool biocheck0 = !((wp0[COMPONENT_Y].bPresentFlag || wp0[COMPONENT_Cb].bPresentFlag || wp0[COMPONENT_Cr].bPresentFlag || wp1[COMPONENT_Y].bPresentFlag || wp1[COMPONENT_Cb].bPresentFlag || wp1[COMPONENT_Cr].bPresentFlag) && slice.getSliceType() == B_SLICE);
+      const bool biocheck0 =
+        !((WPScalingParam::isWeighted(wp0) || WPScalingParam::isWeighted(wp1)) && slice.getSliceType() == B_SLICE);
       const bool biocheck1 = !(pps.getUseWP() && slice.getSliceType() == P_SLICE);
       if (biocheck0
         && biocheck1
@@ -1496,12 +1497,12 @@ void InterPrediction::motionCompensation( PredictionUnit &pu, PelUnitBuf &predBu
   {
 
     CHECK( !pu.cu->affine && pu.refIdx[0] >= 0 && pu.refIdx[1] >= 0 && ( pu.lwidth() + pu.lheight() == 12 ), "invalid 4x8/8x4 bi-predicted blocks" );
-    WPScalingParam *wp0;
-    WPScalingParam *wp1;
     int refIdx0 = pu.refIdx[REF_PIC_LIST_0];
     int refIdx1 = pu.refIdx[REF_PIC_LIST_1];
-    pu.cs->slice->getWpScaling(REF_PIC_LIST_0, refIdx0, wp0);
-    pu.cs->slice->getWpScaling(REF_PIC_LIST_1, refIdx1, wp1);
+
+    const WPScalingParam *wp0 = pu.cs->slice->getWpScaling(REF_PIC_LIST_0, refIdx0);
+    const WPScalingParam *wp1 = pu.cs->slice->getWpScaling(REF_PIC_LIST_1, refIdx1);
+
     bool bioApplied = false;
     const Slice &slice = *pu.cs->slice;
     if (pu.cs->sps->getBDOFEnabledFlag() && (!pu.cs->picHeader->getDisBdofFlag()))
@@ -1513,7 +1514,8 @@ void InterPrediction::motionCompensation( PredictionUnit &pu, PelUnitBuf &predBu
       }
       else
       {
-        const bool biocheck0 = !((wp0[COMPONENT_Y].bPresentFlag || wp0[COMPONENT_Cb].bPresentFlag || wp0[COMPONENT_Cr].bPresentFlag || wp1[COMPONENT_Y].bPresentFlag || wp1[COMPONENT_Cb].bPresentFlag || wp1[COMPONENT_Cr].bPresentFlag) && slice.getSliceType() == B_SLICE);
+        const bool biocheck0 =
+          !((WPScalingParam::isWeighted(wp0) || WPScalingParam::isWeighted(wp1)) && slice.getSliceType() == B_SLICE);
         const bool biocheck1 = !(pps.getUseWP() && slice.getSliceType() == P_SLICE);
         if (biocheck0
           && biocheck1
diff --git a/source/Lib/CommonLib/Slice.cpp b/source/Lib/CommonLib/Slice.cpp
index 1fa011cd7..0b6b1de8d 100644
--- a/source/Lib/CommonLib/Slice.cpp
+++ b/source/Lib/CommonLib/Slice.cpp
@@ -2227,10 +2227,30 @@ void  Slice::initWpAcDcParam()
 }
 
 //! get tables for weighted prediction
-void  Slice::getWpScaling( RefPicList e, int iRefIdx, WPScalingParam *&wp ) const
+const WPScalingParam *Slice::getWpScaling(const RefPicList refPicList, const int refIdx) const
 {
-  CHECK(e>=NUM_REF_PIC_LIST_01, "Invalid picture reference list");
-  wp = (WPScalingParam*) m_weightPredTable[e][iRefIdx];
+  CHECK(refPicList >= NUM_REF_PIC_LIST_01, "Invalid picture reference list");
+  if (refIdx < 0)
+  {
+    return nullptr;
+  }
+  else
+  {
+    return m_weightPredTable[refPicList][refIdx];
+  }
+}
+
+WPScalingParam *Slice::getWpScaling(const RefPicList refPicList, const int refIdx)
+{
+  CHECK(refPicList >= NUM_REF_PIC_LIST_01, "Invalid picture reference list");
+  if (refIdx < 0)
+  {
+    return nullptr;
+  }
+  else
+  {
+    return m_weightPredTable[refPicList][refIdx];
+  }
 }
 
 //! reset Default WP tables settings : no weight.
@@ -2243,11 +2263,11 @@ void  Slice::resetWpScaling()
       for ( int yuv=0 ; yuv<MAX_NUM_COMPONENT ; yuv++ )
       {
         WPScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
-        pwp->bPresentFlag      = false;
-        pwp->uiLog2WeightDenom = 0;
-        pwp->uiLog2WeightDenom = 0;
-        pwp->iWeight           = 1;
-        pwp->iOffset           = 0;
+        pwp->presentFlag     = false;
+        pwp->log2WeightDenom = 0;
+        pwp->log2WeightDenom = 0;
+        pwp->codedWeight     = 1;
+        pwp->codedOffset     = 0;
       }
     }
   }
@@ -2264,19 +2284,20 @@ void  Slice::initWpScaling(const SPS *sps)
       for ( int yuv=0 ; yuv<MAX_NUM_COMPONENT ; yuv++ )
       {
         WPScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
-        if ( !pwp->bPresentFlag )
+        if (!pwp->presentFlag)
         {
           // Inferring values not present :
-          pwp->iWeight = (1 << pwp->uiLog2WeightDenom);
-          pwp->iOffset = 0;
+          pwp->codedWeight = (1 << pwp->log2WeightDenom);
+          pwp->codedOffset = 0;
         }
 
         const int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (sps->getBitDepth(toChannelType(ComponentID(yuv)))-8));
 
-        pwp->w      = pwp->iWeight;
-        pwp->o      = pwp->iOffset * offsetScalingFactor; //NOTE: This value of the ".o" variable is never used - .o is set immediately before it gets used
-        pwp->shift  = pwp->uiLog2WeightDenom;
-        pwp->round  = (pwp->uiLog2WeightDenom>=1) ? (1 << (pwp->uiLog2WeightDenom-1)) : (0);
+        pwp->w = pwp->codedWeight;
+        pwp->o = pwp->codedOffset * offsetScalingFactor;   // NOTE: This value of the ".o" variable is never used - .o
+                                                           // is set immediately before it gets used
+        pwp->shift = pwp->log2WeightDenom;
+        pwp->round = (pwp->log2WeightDenom >= 1) ? (1 << (pwp->log2WeightDenom - 1)) : (0);
       }
     }
   }
@@ -2749,10 +2770,30 @@ void PicHeader::initPicHeader()
   m_alfApsId.resize(0);
 }
 
-void PicHeader::getWpScaling(RefPicList e, int iRefIdx, WPScalingParam *&wp) const
+const WPScalingParam *PicHeader::getWpScaling(const RefPicList refPicList, const int refIdx) const
 {
-  CHECK(e >= NUM_REF_PIC_LIST_01, "Invalid picture reference list");
-  wp = (WPScalingParam *) m_weightPredTable[e][iRefIdx];
+  CHECK(refPicList >= NUM_REF_PIC_LIST_01, "Invalid picture reference list");
+  if (refIdx < 0)
+  {
+    return nullptr;
+  }
+  else
+  {
+    return m_weightPredTable[refPicList][refIdx];
+  }
+}
+
+WPScalingParam *PicHeader::getWpScaling(const RefPicList refPicList, const int refIdx)
+{
+  CHECK(refPicList >= NUM_REF_PIC_LIST_01, "Invalid picture reference list");
+  if (refIdx < 0)
+  {
+    return nullptr;
+  }
+  else
+  {
+    return m_weightPredTable[refPicList][refIdx];
+  }
 }
 
 void PicHeader::resetWpScaling()
@@ -2764,10 +2805,10 @@ void PicHeader::resetWpScaling()
       for ( int yuv=0 ; yuv<MAX_NUM_COMPONENT ; yuv++ )
       {
         WPScalingParam  *pwp = &(m_weightPredTable[e][i][yuv]);
-        pwp->bPresentFlag      = false;
-        pwp->uiLog2WeightDenom = 0;
-        pwp->iWeight           = 1;
-        pwp->iOffset           = 0;
+        pwp->presentFlag     = false;
+        pwp->log2WeightDenom = 0;
+        pwp->codedWeight     = 1;
+        pwp->codedOffset     = 0;
       }
     }
   }
diff --git a/source/Lib/CommonLib/Slice.h b/source/Lib/CommonLib/Slice.h
index fc74bdd09..2ff65dc36 100644
--- a/source/Lib/CommonLib/Slice.h
+++ b/source/Lib/CommonLib/Slice.h
@@ -2325,10 +2325,10 @@ struct WPScalingParam
 {
   // Explicit weighted prediction parameters parsed in slice header,
   // or Implicit weighted prediction parameters (8 bits depth values).
-  bool bPresentFlag;
-  uint32_t uiLog2WeightDenom;
-  int  iWeight;
-  int  iOffset;
+  bool     presentFlag;
+  uint32_t log2WeightDenom;
+  int      codedWeight;
+  int      codedOffset;
 
   // Weighted prediction scaling values built from above parameters (bitdepth scaled):
   int  w;
@@ -2337,7 +2337,14 @@ struct WPScalingParam
   int  shift;
   int  round;
 
+  static bool isWeighted(const WPScalingParam *wp);
 };
+
+inline bool WPScalingParam::isWeighted(const WPScalingParam *wp)
+{
+  return wp != nullptr && (wp[COMPONENT_Y].presentFlag || wp[COMPONENT_Cb].presentFlag || wp[COMPONENT_Cr].presentFlag);
+}
+
 struct WPACDCParam
 {
   int64_t iAC;
@@ -2605,7 +2612,8 @@ public:
   {
     memcpy(m_weightPredTable, wp, sizeof(WPScalingParam) * NUM_REF_PIC_LIST_01 * MAX_NUM_REF * MAX_NUM_COMPONENT);
   }
-  void                        getWpScaling(RefPicList e, int iRefIdx, WPScalingParam *&wp) const;
+  const WPScalingParam *      getWpScaling(const RefPicList refPicList, const int refIdx) const;
+  WPScalingParam *            getWpScaling(const RefPicList refPicList, const int refIdx);
   WPScalingParam*             getWpScalingAll()                                        { return (WPScalingParam *) m_weightPredTable; }
   void                        resetWpScaling();
   void                        setNumL0Weights(int b)                                   { m_numL0Weights = b;                          }
@@ -2975,7 +2983,8 @@ public:
     memcpy(m_weightPredTable, wp, sizeof(WPScalingParam) * NUM_REF_PIC_LIST_01 * MAX_NUM_REF * MAX_NUM_COMPONENT);
   }
   WPScalingParam *            getWpScalingAll()                                      { return (WPScalingParam *) m_weightPredTable;                  }
-  void                        getWpScaling( RefPicList e, int iRefIdx, WPScalingParam *&wp) const;
+  WPScalingParam *            getWpScaling(const RefPicList refPicList, const int refIdx);
+  const WPScalingParam *      getWpScaling(const RefPicList refPicList, const int refIdx) const;
 
   void                        resetWpScaling();
   void                        initWpScaling(const SPS *sps);
diff --git a/source/Lib/CommonLib/UnitTools.cpp b/source/Lib/CommonLib/UnitTools.cpp
index 8e11c1ec0..3546ef08b 100644
--- a/source/Lib/CommonLib/UnitTools.cpp
+++ b/source/Lib/CommonLib/UnitTools.cpp
@@ -1403,29 +1403,23 @@ void PU::getInterMergeCandidates( const PredictionUnit &pu, MergeCtx& mrgCtx,
 
 bool PU::checkDMVRCondition(const PredictionUnit& pu)
 {
-  WPScalingParam *wp0;
-  WPScalingParam *wp1;
-  int refIdx0 = pu.refIdx[REF_PIC_LIST_0];
-  int refIdx1 = pu.refIdx[REF_PIC_LIST_1];
-  pu.cu->slice->getWpScaling(REF_PIC_LIST_0, refIdx0, wp0);
-  pu.cu->slice->getWpScaling(REF_PIC_LIST_1, refIdx1, wp1);
-  if (pu.cs->sps->getUseDMVR() && (!pu.cs->picHeader->getDisDmvrFlag()))
-  {
-    return pu.mergeFlag
-      && pu.mergeType == MRG_TYPE_DEFAULT_N
-      && !pu.ciipFlag
-      && !pu.cu->affine
-      && !pu.mmvdMergeFlag
-      && !pu.cu->mmvdSkip
-      && PU::isBiPredFromDifferentDirEqDistPoc(pu)
-      && (pu.lheight() >= 8)
-      && (pu.lwidth() >= 8)
-      && ((pu.lheight() * pu.lwidth()) >= 128)
-      && (pu.cu->BcwIdx == BCW_DEFAULT)
-      && ((!wp0[COMPONENT_Y].bPresentFlag) && (!wp0[COMPONENT_Cb].bPresentFlag) && (!wp0[COMPONENT_Cr].bPresentFlag) && (!wp1[COMPONENT_Y].bPresentFlag) && (!wp1[COMPONENT_Cb].bPresentFlag) && (!wp1[COMPONENT_Cr].bPresentFlag))
-      && ( refIdx0 < 0 ? true : (pu.cu->slice->getRefPic( REF_PIC_LIST_0, refIdx0 )->isRefScaled( pu.cs->pps ) == false) )
-      && ( refIdx1 < 0 ? true : (pu.cu->slice->getRefPic( REF_PIC_LIST_1, refIdx1 )->isRefScaled( pu.cs->pps ) == false) )
-      ;
+  if (pu.cs->sps->getUseDMVR() && !pu.cs->picHeader->getDisDmvrFlag())
+  {
+    const int refIdx0 = pu.refIdx[REF_PIC_LIST_0];
+    const int refIdx1 = pu.refIdx[REF_PIC_LIST_1];
+
+    const WPScalingParam *wp0 = pu.cu->slice->getWpScaling(REF_PIC_LIST_0, refIdx0);
+    const WPScalingParam *wp1 = pu.cu->slice->getWpScaling(REF_PIC_LIST_1, refIdx1);
+
+    const bool ref0IsScaled =
+      refIdx0 < 0 ? false : pu.cu->slice->getRefPic(REF_PIC_LIST_0, refIdx0)->isRefScaled(pu.cs->pps);
+    const bool ref1IsScaled =
+      refIdx1 < 0 ? false : pu.cu->slice->getRefPic(REF_PIC_LIST_1, refIdx1)->isRefScaled(pu.cs->pps);
+
+    return pu.mergeFlag && pu.mergeType == MRG_TYPE_DEFAULT_N && !pu.ciipFlag && !pu.cu->affine && !pu.mmvdMergeFlag
+           && !pu.cu->mmvdSkip && PU::isBiPredFromDifferentDirEqDistPoc(pu) && (pu.lheight() >= 8) && (pu.lwidth() >= 8)
+           && ((pu.lheight() * pu.lwidth()) >= 128) && (pu.cu->BcwIdx == BCW_DEFAULT)
+           && !WPScalingParam::isWeighted(wp0) && !WPScalingParam::isWeighted(wp1) && !ref0IsScaled && !ref1IsScaled;
   }
   else
   {
@@ -3732,19 +3726,13 @@ bool CU::isBcwIdxCoded( const CodingUnit &cu )
   {
     if( cu.firstPU->interDir == 3 )
     {
-      WPScalingParam *wp0;
-      WPScalingParam *wp1;
-      int refIdx0 = cu.firstPU->refIdx[REF_PIC_LIST_0];
-      int refIdx1 = cu.firstPU->refIdx[REF_PIC_LIST_1];
+      const int refIdx0 = cu.firstPU->refIdx[REF_PIC_LIST_0];
+      const int refIdx1 = cu.firstPU->refIdx[REF_PIC_LIST_1];
 
-      cu.cs->slice->getWpScaling(REF_PIC_LIST_0, refIdx0, wp0);
-      cu.cs->slice->getWpScaling(REF_PIC_LIST_1, refIdx1, wp1);
-      if ((wp0[COMPONENT_Y].bPresentFlag || wp0[COMPONENT_Cb].bPresentFlag || wp0[COMPONENT_Cr].bPresentFlag
-        || wp1[COMPONENT_Y].bPresentFlag || wp1[COMPONENT_Cb].bPresentFlag || wp1[COMPONENT_Cr].bPresentFlag))
-      {
-        return false;
-      }
-      return true;
+      const WPScalingParam *wp0 = cu.cs->slice->getWpScaling(REF_PIC_LIST_0, refIdx0);
+      const WPScalingParam *wp1 = cu.cs->slice->getWpScaling(REF_PIC_LIST_1, refIdx1);
+
+      return !(WPScalingParam::isWeighted(wp0) || WPScalingParam::isWeighted(wp1));
     }
   }
 
diff --git a/source/Lib/CommonLib/WeightPrediction.cpp b/source/Lib/CommonLib/WeightPrediction.cpp
index cab07bc6d..ddff5abcb 100644
--- a/source/Lib/CommonLib/WeightPrediction.cpp
+++ b/source/Lib/CommonLib/WeightPrediction.cpp
@@ -72,14 +72,8 @@ WeightPrediction::WeightPrediction()
 {
 }
 
-
-
-void  WeightPrediction::getWpScaling(const Slice                *pcSlice,
-                                     const int                  &iRefIdx0,
-                                     const int                  &iRefIdx1,
-                                           WPScalingParam      *&wp0,
-                                           WPScalingParam      *&wp1,
-                                     const ComponentID           maxNumComp)
+void WeightPrediction::getWpScaling(Slice *pcSlice, const int &iRefIdx0, const int &iRefIdx1, WPScalingParam *&wp0,
+                                    WPScalingParam *&wp1, const ComponentID maxNumComp)
 {
   CHECK(iRefIdx0 < 0 && iRefIdx1 < 0, "Both picture reference list indizes smaller than '0'");
 
@@ -90,14 +84,8 @@ void  WeightPrediction::getWpScaling(const Slice                *pcSlice,
   if (bUniPred || wpBiPred)
   {
     // explicit --------------------
-    if (iRefIdx0 >= 0)
-    {
-      pcSlice->getWpScaling(REF_PIC_LIST_0, iRefIdx0, wp0);
-    }
-    if (iRefIdx1 >= 0)
-    {
-      pcSlice->getWpScaling(REF_PIC_LIST_1, iRefIdx1, wp1);
-    }
+    wp0 = pcSlice->getWpScaling(REF_PIC_LIST_0, iRefIdx0);
+    wp1 = pcSlice->getWpScaling(REF_PIC_LIST_1, iRefIdx1);
   }
   else
   {
@@ -106,11 +94,11 @@ void  WeightPrediction::getWpScaling(const Slice                *pcSlice,
 
   if (iRefIdx0 < 0)
   {
-    wp0 = NULL;
+    wp0 = nullptr;
   }
   if (iRefIdx1 < 0)
   {
-    wp1 = NULL;
+    wp1 = nullptr;
   }
 
   const uint32_t numValidComponent = getNumberValidComponents(pcSlice->getSPS()->getChromaFormatIdc());
@@ -124,13 +112,13 @@ void  WeightPrediction::getWpScaling(const Slice                *pcSlice,
       const int bitDepth = pcSlice->getSPS()->getBitDepth(toChannelType(ComponentID(yuv)));
       const int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (bitDepth - 8));
 
-      wp0[yuv].w = wp0[yuv].iWeight;
-      wp1[yuv].w = wp1[yuv].iWeight;
-      wp0[yuv].o = wp0[yuv].iOffset * offsetScalingFactor;
-      wp1[yuv].o = wp1[yuv].iOffset * offsetScalingFactor;
+      wp0[yuv].w      = wp0[yuv].codedWeight;
+      wp1[yuv].w      = wp1[yuv].codedWeight;
+      wp0[yuv].o      = wp0[yuv].codedOffset * offsetScalingFactor;
+      wp1[yuv].o      = wp1[yuv].codedOffset * offsetScalingFactor;
       wp0[yuv].offset = wp0[yuv].o + wp1[yuv].o;
-      wp0[yuv].shift = wp0[yuv].uiLog2WeightDenom + 1;
-      wp0[yuv].round = (1 << wp0[yuv].uiLog2WeightDenom);
+      wp0[yuv].shift  = wp0[yuv].log2WeightDenom + 1;
+      wp0[yuv].round  = (1 << wp0[yuv].log2WeightDenom);
       wp1[yuv].offset = wp0[yuv].offset;
       wp1[yuv].shift = wp0[yuv].shift;
       wp1[yuv].round = wp0[yuv].round;
@@ -146,10 +134,10 @@ void  WeightPrediction::getWpScaling(const Slice                *pcSlice,
       const int bitDepth            = pcSlice->getSPS()->getBitDepth(toChannelType(ComponentID(yuv)));
       const int offsetScalingFactor = bUseHighPrecisionPredictionWeighting ? 1 : (1 << (bitDepth - 8));
 
-      pwp[yuv].w      = pwp[yuv].iWeight;
-      pwp[yuv].offset = pwp[yuv].iOffset * offsetScalingFactor;
-      pwp[yuv].shift  = pwp[yuv].uiLog2WeightDenom;
-      pwp[yuv].round  = (pwp[yuv].uiLog2WeightDenom >= 1) ? (1 << (pwp[yuv].uiLog2WeightDenom - 1)) : (0);
+      pwp[yuv].w      = pwp[yuv].codedWeight;
+      pwp[yuv].offset = pwp[yuv].codedOffset * offsetScalingFactor;
+      pwp[yuv].shift  = pwp[yuv].log2WeightDenom;
+      pwp[yuv].round  = (pwp[yuv].log2WeightDenom >= 1) ? (1 << (pwp[yuv].log2WeightDenom - 1)) : (0);
     }
   }
 }
diff --git a/source/Lib/CommonLib/WeightPrediction.h b/source/Lib/CommonLib/WeightPrediction.h
index 3b4596964..80915b618 100644
--- a/source/Lib/CommonLib/WeightPrediction.h
+++ b/source/Lib/CommonLib/WeightPrediction.h
@@ -55,12 +55,8 @@ class WeightPrediction
 public:
   WeightPrediction();
 
-  void  getWpScaling(           const Slice                *slice,
-                                const int                  &iRefIdx0,
-                                const int                  &iRefIdx1,
-                                      WPScalingParam      *&wp0,
-                                      WPScalingParam      *&wp1,
-                                const ComponentID           maxNumComp = MAX_NUM_COMPONENT );
+  void getWpScaling(Slice *slice, const int &iRefIdx0, const int &iRefIdx1, WPScalingParam *&wp0, WPScalingParam *&wp1,
+                    const ComponentID maxNumComp = MAX_NUM_COMPONENT);
 
   void addWeightBi(             const CPelUnitBuf          &pcYuvSrc0,
                                 const CPelUnitBuf          &pcYuvSrc1,
diff --git a/source/Lib/DecoderLib/VLCReader.cpp b/source/Lib/DecoderLib/VLCReader.cpp
index 2187ff5ed..9f1dc8276 100644
--- a/source/Lib/DecoderLib/VLCReader.cpp
+++ b/source/Lib/DecoderLib/VLCReader.cpp
@@ -4203,16 +4203,16 @@ void HLSyntaxReader::parseSliceHeader (Slice* pcSlice, PicHeader* picHeader, Par
     }
     else
     {
-      WPScalingParam *wp;
       for ( int iNumRef=0 ; iNumRef<((pcSlice->getSliceType() == B_SLICE )?2:1); iNumRef++ )
       {
         RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
         for ( int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
         {
-          pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
-          wp[0].bPresentFlag = false;
-          wp[1].bPresentFlag = false;
-          wp[2].bPresentFlag = false;
+          WPScalingParam *wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
+
+          wp[0].presentFlag = false;
+          wp[1].presentFlag = false;
+          wp[2].presentFlag = false;
         }
       }
     }
@@ -4869,7 +4869,6 @@ void HLSyntaxReader::parseRemainingBytes( bool noTrailingBytesExpected )
 //! parse explicit wp tables
 void HLSyntaxReader::parsePredWeightTable( Slice* pcSlice, const SPS *sps )
 {
-  WPScalingParam *wp;
   const ChromaFormat    chFmt        = sps->getChromaFormatIdc();
   const int             numValidComp = int(getNumberValidComponents(chFmt));
   const bool            bChroma      = (chFmt!=CHROMA_400);
@@ -4895,65 +4894,66 @@ void HLSyntaxReader::parsePredWeightTable( Slice* pcSlice, const SPS *sps )
     RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
     for ( int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
     {
-      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
+      WPScalingParam *wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
 
-      wp[COMPONENT_Y].uiLog2WeightDenom = uiLog2WeightDenomLuma;
+      wp[COMPONENT_Y].log2WeightDenom = uiLog2WeightDenomLuma;
       for(int j=1; j<numValidComp; j++)
       {
-        wp[j].uiLog2WeightDenom = uiLog2WeightDenomChroma;
+        wp[j].log2WeightDenom = uiLog2WeightDenomChroma;
       }
 
       uint32_t  uiCode;
       READ_FLAG( uiCode, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
-      wp[COMPONENT_Y].bPresentFlag = ( uiCode == 1 );
-      uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
+      wp[COMPONENT_Y].presentFlag = (uiCode == 1);
+      uiTotalSignalledWeightFlags += wp[COMPONENT_Y].presentFlag;
     }
     if ( bChroma )
     {
       uint32_t  uiCode;
       for ( int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
       {
-        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
+        WPScalingParam *wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
         READ_FLAG( uiCode, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
         for(int j=1; j<numValidComp; j++)
         {
-          wp[j].bPresentFlag = ( uiCode == 1 );
+          wp[j].presentFlag = (uiCode == 1);
         }
-        uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
+        uiTotalSignalledWeightFlags += 2 * wp[COMPONENT_Cb].presentFlag;
       }
     }
     else
     {
       for ( int iRefIdx=0; iRefIdx<MAX_NUM_REF; iRefIdx++ )
       {
-        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
-        wp[1].bPresentFlag = false;
-        wp[2].bPresentFlag = false;
+        WPScalingParam *wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
+
+        wp[COMPONENT_Cb].presentFlag = false;
+        wp[COMPONENT_Cr].presentFlag = false;
       }
     }
     for ( int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
     {
-      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
-      if ( wp[COMPONENT_Y].bPresentFlag )
+      WPScalingParam *wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
+      if (wp[COMPONENT_Y].presentFlag)
       {
         int iDeltaWeight;
         READ_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
         CHECK( iDeltaWeight < -128, "delta_luma_weight_lx shall be in the rage of -128 to 127" );
         CHECK( iDeltaWeight >  127, "delta_luma_weight_lx shall be in the rage of -128 to 127" );
-        wp[COMPONENT_Y].iWeight = (iDeltaWeight + (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
-        READ_SVLC( wp[COMPONENT_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
+        wp[COMPONENT_Y].codedWeight = (iDeltaWeight + (1 << wp[COMPONENT_Y].log2WeightDenom));
+        READ_SVLC(wp[COMPONENT_Y].codedOffset, iNumRef == 0 ? "luma_offset_l0[i]" : "luma_offset_l1[i]");
         const int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_LUMA))/2 : 128;
-        CHECK ( wp[0].iOffset < -range , "luma_offset_lx shall be in the rage of -128 to 127");
-        CHECK ( wp[0].iOffset >= range , "luma_offset_lx shall be in the rage of -128 to 127");
+        CHECK(wp[0].codedOffset < -range, "luma_offset_lx shall be in the rage of -128 to 127");
+        CHECK(wp[0].codedOffset >= range, "luma_offset_lx shall be in the rage of -128 to 127");
       }
       else
       {
-        wp[COMPONENT_Y].iWeight = (1 << wp[COMPONENT_Y].uiLog2WeightDenom);
-        wp[COMPONENT_Y].iOffset = 0;
+        wp[COMPONENT_Y].codedWeight = (1 << wp[COMPONENT_Y].log2WeightDenom);
+        wp[COMPONENT_Y].codedOffset = 0;
       }
       if ( bChroma )
       {
-        if ( wp[COMPONENT_Cb].bPresentFlag )
+        if (wp[COMPONENT_Cb].presentFlag)
         {
           int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
           for ( int j=1 ; j<numValidComp ; j++ )
@@ -4962,22 +4962,22 @@ void HLSyntaxReader::parsePredWeightTable( Slice* pcSlice, const SPS *sps )
             READ_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
             CHECK( iDeltaWeight < -128, "delta_chroma_weight_lx shall be in the rage of -128 to 127" );
             CHECK( iDeltaWeight >  127, "delta_chroma_weight_lx shall be in the rage of -128 to 127" );
-            wp[j].iWeight = (iDeltaWeight + (1<<wp[j].uiLog2WeightDenom));
+            wp[j].codedWeight = (iDeltaWeight + (1 << wp[j].log2WeightDenom));
 
             int iDeltaChroma;
             READ_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
             CHECK( iDeltaChroma <  -4*range, "delta_chroma_offset_lx shall be in the range of -4 * 128 to 4 * 127" );
             CHECK( iDeltaChroma >  4*(range-1), "delta_chroma_offset_lx shall be in the range of -4 * 128 to 4 * 127" );
-            int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
-            wp[j].iOffset = Clip3(-range, range-1, (iDeltaChroma + pred) );
+            int pred          = (range - ((range * wp[j].codedWeight) >> (wp[j].log2WeightDenom)));
+            wp[j].codedOffset = Clip3(-range, range - 1, (iDeltaChroma + pred));
           }
         }
         else
         {
           for ( int j=1 ; j<numValidComp ; j++ )
           {
-            wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
-            wp[j].iOffset = 0;
+            wp[j].codedWeight = (1 << wp[j].log2WeightDenom);
+            wp[j].codedOffset = 0;
           }
         }
       }
@@ -4985,11 +4985,11 @@ void HLSyntaxReader::parsePredWeightTable( Slice* pcSlice, const SPS *sps )
 
     for ( int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
     {
-      pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
+      WPScalingParam *wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
 
-      wp[0].bPresentFlag = false;
-      wp[1].bPresentFlag = false;
-      wp[2].bPresentFlag = false;
+      wp[COMPONENT_Y].presentFlag  = false;
+      wp[COMPONENT_Cb].presentFlag = false;
+      wp[COMPONENT_Cr].presentFlag = false;
     }
   }
   CHECK(uiTotalSignalledWeightFlags>24, "Too many weight flag signalled");
@@ -5027,65 +5027,65 @@ void HLSyntaxReader::parsePredWeightTable(PicHeader *picHeader, const SPS *sps)
     RefPicList refPicList = (numRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
     for (int refIdx = 0; refIdx < numLxWeights; refIdx++)
     {
-      picHeader->getWpScaling(refPicList, refIdx, wp);
+      wp = picHeader->getWpScaling(refPicList, refIdx);
 
-      wp[COMPONENT_Y].uiLog2WeightDenom = log2WeightDenomLuma;
+      wp[COMPONENT_Y].log2WeightDenom = log2WeightDenomLuma;
       for (int j = 1; j < numValidComp; j++)
       {
-        wp[j].uiLog2WeightDenom = log2WeightDenomChroma;
+        wp[j].log2WeightDenom = log2WeightDenomChroma;
       }
 
       uint32_t uiCode;
       READ_FLAG(uiCode, numRef == 0 ? "luma_weight_l0_flag[i]" : "luma_weight_l1_flag[i]");
-      wp[COMPONENT_Y].bPresentFlag = (uiCode == 1);
-      totalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
+      wp[COMPONENT_Y].presentFlag = (uiCode == 1);
+      totalSignalledWeightFlags += wp[COMPONENT_Y].presentFlag;
     }
     if (chroma)
     {
       uint32_t uiCode;
       for (int refIdx = 0; refIdx < numLxWeights; refIdx++)
       {
-        picHeader->getWpScaling(refPicList, refIdx, wp);
+        wp = picHeader->getWpScaling(refPicList, refIdx);
         READ_FLAG(uiCode, numRef == 0 ? "chroma_weight_l0_flag[i]" : "chroma_weight_l1_flag[i]");
         for (int j = 1; j < numValidComp; j++)
         {
-          wp[j].bPresentFlag = (uiCode == 1);
+          wp[j].presentFlag = (uiCode == 1);
         }
-        totalSignalledWeightFlags += 2 * wp[COMPONENT_Cb].bPresentFlag;
+        totalSignalledWeightFlags += 2 * wp[COMPONENT_Cb].presentFlag;
       }
     }
     else
     {
       for ( int refIdx=0; refIdx<MAX_NUM_REF; refIdx++ )
       {
-        picHeader->getWpScaling(refPicList, refIdx, wp);
-        wp[1].bPresentFlag = false;
-        wp[2].bPresentFlag = false;
+        wp                = picHeader->getWpScaling(refPicList, refIdx);
+        wp[1].presentFlag = false;
+        wp[2].presentFlag = false;
       }
     }
     for (int refIdx = 0; refIdx < numLxWeights; refIdx++)
     {
-      picHeader->getWpScaling(refPicList, refIdx, wp);
-      if (wp[COMPONENT_Y].bPresentFlag)
+      wp = picHeader->getWpScaling(refPicList, refIdx);
+      if (wp[COMPONENT_Y].presentFlag)
       {
         int deltaWeight;
         READ_SVLC(deltaWeight, numRef == 0 ? "delta_luma_weight_l0[i]" : "delta_luma_weight_l1[i]");
         CHECK(deltaWeight < -128, "delta_luma_weight_lx shall be in the rage of -128 to 127");
         CHECK(deltaWeight > 127, "delta_luma_weight_lx shall be in the rage of -128 to 127");
-        wp[COMPONENT_Y].iWeight = (deltaWeight + (1 << wp[COMPONENT_Y].uiLog2WeightDenom));
-        READ_SVLC(wp[COMPONENT_Y].iOffset, numRef == 0 ? "luma_offset_l0[i]" : "luma_offset_l1[i]");
+        wp[COMPONENT_Y].codedWeight = (deltaWeight + (1 << wp[COMPONENT_Y].log2WeightDenom));
+        READ_SVLC(wp[COMPONENT_Y].codedOffset, numRef == 0 ? "luma_offset_l0[i]" : "luma_offset_l1[i]");
         const int range = sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1 << sps->getBitDepth(CHANNEL_TYPE_LUMA)) / 2 : 128;
-        CHECK ( wp[0].iOffset < -range , "luma_offset_lx shall be in the rage of -128 to 127");
-        CHECK ( wp[0].iOffset >= range , "luma_offset_lx shall be in the rage of -128 to 127");
+        CHECK(wp[0].codedOffset < -range, "luma_offset_lx shall be in the rage of -128 to 127");
+        CHECK(wp[0].codedOffset >= range, "luma_offset_lx shall be in the rage of -128 to 127");
       }
       else
       {
-        wp[COMPONENT_Y].iWeight = (1 << wp[COMPONENT_Y].uiLog2WeightDenom);
-        wp[COMPONENT_Y].iOffset = 0;
+        wp[COMPONENT_Y].codedWeight = (1 << wp[COMPONENT_Y].log2WeightDenom);
+        wp[COMPONENT_Y].codedOffset = 0;
       }
       if (chroma)
       {
-        if (wp[COMPONENT_Cb].bPresentFlag)
+        if (wp[COMPONENT_Cb].presentFlag)
         {
           int range = sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1 << sps->getBitDepth(CHANNEL_TYPE_CHROMA)) / 2 : 128;
           for (int j = 1; j < numValidComp; j++)
@@ -5094,22 +5094,22 @@ void HLSyntaxReader::parsePredWeightTable(PicHeader *picHeader, const SPS *sps)
             READ_SVLC(deltaWeight, numRef == 0 ? "delta_chroma_weight_l0[i]" : "delta_chroma_weight_l1[i]");
             CHECK( deltaWeight < -128, "delta_chroma_weight_lx shall be in the rage of -128 to 127" );
             CHECK( deltaWeight >  127, "delta_chroma_weight_lx shall be in the rage of -128 to 127" );
-            wp[j].iWeight = (deltaWeight + (1 << wp[j].uiLog2WeightDenom));
+            wp[j].codedWeight = (deltaWeight + (1 << wp[j].log2WeightDenom));
 
             int deltaChroma;
             READ_SVLC(deltaChroma, numRef == 0 ? "delta_chroma_offset_l0[i]" : "delta_chroma_offset_l1[i]");
             CHECK( deltaChroma <  -4*range, "delta_chroma_offset_lx shall be in the range of -4 * 128 to 4 * 127" );
             CHECK( deltaChroma >=  4*range, "delta_chroma_offset_lx shall be in the range of -4 * 128 to 4 * 127" );
-            int pred      = (range - ((range * wp[j].iWeight) >> (wp[j].uiLog2WeightDenom)));
-            wp[j].iOffset = Clip3(-range, range - 1, (deltaChroma + pred));
+            int pred          = (range - ((range * wp[j].codedWeight) >> (wp[j].log2WeightDenom)));
+            wp[j].codedOffset = Clip3(-range, range - 1, (deltaChroma + pred));
           }
         }
         else
         {
           for (int j = 1; j < numValidComp; j++)
           {
-            wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
-            wp[j].iOffset = 0;
+            wp[j].codedWeight = (1 << wp[j].log2WeightDenom);
+            wp[j].codedOffset = 0;
           }
         }
       }
@@ -5117,11 +5117,11 @@ void HLSyntaxReader::parsePredWeightTable(PicHeader *picHeader, const SPS *sps)
 
     for (int refIdx = numLxWeights; refIdx < MAX_NUM_REF; refIdx++)
     {
-      picHeader->getWpScaling(refPicList, refIdx, wp);
+      wp = picHeader->getWpScaling(refPicList, refIdx);
 
-      wp[0].bPresentFlag = false;
-      wp[1].bPresentFlag = false;
-      wp[2].bPresentFlag = false;
+      wp[0].presentFlag = false;
+      wp[1].presentFlag = false;
+      wp[2].presentFlag = false;
     }
 
     if (numRef == 0)
diff --git a/source/Lib/EncoderLib/InterSearch.cpp b/source/Lib/EncoderLib/InterSearch.cpp
index e49c6c51b..e628bbf54 100644
--- a/source/Lib/EncoderLib/InterSearch.cpp
+++ b/source/Lib/EncoderLib/InterSearch.cpp
@@ -2832,10 +2832,9 @@ void InterSearch::predInterSearch(CodingUnit& cu, Partitioner& partitioner)
     {
       CHECK(iRefIdxBi[0]<0, "Invalid picture reference index");
       CHECK(iRefIdxBi[1]<0, "Invalid picture reference index");
-      cu.cs->slice->getWpScaling(REF_PIC_LIST_0, iRefIdxBi[0], wp0);
-      cu.cs->slice->getWpScaling(REF_PIC_LIST_1, iRefIdxBi[1], wp1);
-      if ((wp0[COMPONENT_Y].bPresentFlag || wp0[COMPONENT_Cb].bPresentFlag || wp0[COMPONENT_Cr].bPresentFlag
-        || wp1[COMPONENT_Y].bPresentFlag || wp1[COMPONENT_Cb].bPresentFlag || wp1[COMPONENT_Cr].bPresentFlag))
+      wp0 = cu.cs->slice->getWpScaling(REF_PIC_LIST_0, iRefIdxBi[0]);
+      wp1 = cu.cs->slice->getWpScaling(REF_PIC_LIST_1, iRefIdxBi[1]);
+      if (WPScalingParam::isWeighted(wp0) || WPScalingParam::isWeighted(wp1))
       {
         uiCostBi = MAX_UINT;
         enforceBcwPred = false;
@@ -5125,10 +5124,10 @@ void InterSearch::xPredAffineInterSearch( PredictionUnit&       pu,
   {
     CHECK(iRefIdxBi[0]<0, "Invalid picture reference index");
     CHECK(iRefIdxBi[1]<0, "Invalid picture reference index");
-    pu.cs->slice->getWpScaling(REF_PIC_LIST_0, iRefIdxBi[0], wp0);
-    pu.cs->slice->getWpScaling(REF_PIC_LIST_1, iRefIdxBi[1], wp1);
-    if ((wp0[COMPONENT_Y].bPresentFlag || wp0[COMPONENT_Cb].bPresentFlag || wp0[COMPONENT_Cr].bPresentFlag
-      || wp1[COMPONENT_Y].bPresentFlag || wp1[COMPONENT_Cb].bPresentFlag || wp1[COMPONENT_Cr].bPresentFlag))
+    wp0 = pu.cs->slice->getWpScaling(REF_PIC_LIST_0, iRefIdxBi[0]);
+    wp1 = pu.cs->slice->getWpScaling(REF_PIC_LIST_1, iRefIdxBi[1]);
+
+    if (WPScalingParam::isWeighted(wp0) || WPScalingParam::isWeighted(wp1))
     {
       uiCostBi = MAX_UINT;
       enforceBcwPred = false;
diff --git a/source/Lib/EncoderLib/VLCWriter.cpp b/source/Lib/EncoderLib/VLCWriter.cpp
index 40885b813..0097898ea 100644
--- a/source/Lib/EncoderLib/VLCWriter.cpp
+++ b/source/Lib/EncoderLib/VLCWriter.cpp
@@ -2841,61 +2841,65 @@ void HLSWriter::xCodePredWeightTable( Slice* pcSlice )
     {
       RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
 
-      // NOTE: wp[].uiLog2WeightDenom and wp[].bPresentFlag are actually per-channel-type settings.
+      // NOTE: wp[].log2WeightDenom and wp[].presentFlag are actually per-channel-type settings.
 
       for ( int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
       {
-        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
+        wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
         if ( !bDenomCoded )
         {
           int iDeltaDenom;
-          WRITE_UVLC( wp[COMPONENT_Y].uiLog2WeightDenom, "luma_log2_weight_denom" );
+          WRITE_UVLC(wp[COMPONENT_Y].log2WeightDenom, "luma_log2_weight_denom");
 
           if( bChroma )
           {
-            CHECK( wp[COMPONENT_Cb].uiLog2WeightDenom != wp[COMPONENT_Cr].uiLog2WeightDenom, "Chroma blocks of different size not supported" );
-            iDeltaDenom = (wp[COMPONENT_Cb].uiLog2WeightDenom - wp[COMPONENT_Y].uiLog2WeightDenom);
+            CHECK(wp[COMPONENT_Cb].log2WeightDenom != wp[COMPONENT_Cr].log2WeightDenom,
+                  "Chroma blocks of different size not supported");
+            iDeltaDenom = (wp[COMPONENT_Cb].log2WeightDenom - wp[COMPONENT_Y].log2WeightDenom);
             WRITE_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
           }
           bDenomCoded = true;
         }
-        WRITE_FLAG( wp[COMPONENT_Y].bPresentFlag, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
-        uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
+        WRITE_FLAG(wp[COMPONENT_Y].presentFlag, iNumRef == 0 ? "luma_weight_l0_flag[i]" : "luma_weight_l1_flag[i]");
+        uiTotalSignalledWeightFlags += wp[COMPONENT_Y].presentFlag;
       }
       if (bChroma)
       {
         for ( int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
         {
-          pcSlice->getWpScaling( eRefPicList, iRefIdx, wp );
-          CHECK( wp[COMPONENT_Cb].bPresentFlag != wp[COMPONENT_Cr].bPresentFlag, "Inconsistent settings for chroma channels" );
-          WRITE_FLAG( wp[COMPONENT_Cb].bPresentFlag, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
-          uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
+          wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
+          CHECK(wp[COMPONENT_Cb].presentFlag != wp[COMPONENT_Cr].presentFlag,
+                "Inconsistent settings for chroma channels");
+          WRITE_FLAG(wp[COMPONENT_Cb].presentFlag,
+                     iNumRef == 0 ? "chroma_weight_l0_flag[i]" : "chroma_weight_l1_flag[i]");
+          uiTotalSignalledWeightFlags += 2 * wp[COMPONENT_Cb].presentFlag;
         }
       }
 
       for ( int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
       {
-        pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
-        if ( wp[COMPONENT_Y].bPresentFlag )
+        wp = pcSlice->getWpScaling(eRefPicList, iRefIdx);
+        if (wp[COMPONENT_Y].presentFlag)
         {
-          int iDeltaWeight = (wp[COMPONENT_Y].iWeight - (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
+          int iDeltaWeight = (wp[COMPONENT_Y].codedWeight - (1 << wp[COMPONENT_Y].log2WeightDenom));
           WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
-          WRITE_SVLC( wp[COMPONENT_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
+          WRITE_SVLC(wp[COMPONENT_Y].codedOffset, iNumRef == 0 ? "luma_offset_l0[i]" : "luma_offset_l1[i]");
         }
 
         if ( bChroma )
         {
-          if ( wp[COMPONENT_Cb].bPresentFlag )
+          if (wp[COMPONENT_Cb].presentFlag)
           {
             for ( int j = COMPONENT_Cb ; j < numberValidComponents ; j++ )
             {
-              CHECK(wp[COMPONENT_Cb].uiLog2WeightDenom != wp[COMPONENT_Cr].uiLog2WeightDenom, "Chroma blocks of different size not supported");
-              int iDeltaWeight = (wp[j].iWeight - (1<<wp[COMPONENT_Cb].uiLog2WeightDenom));
+              CHECK(wp[COMPONENT_Cb].log2WeightDenom != wp[COMPONENT_Cr].log2WeightDenom,
+                    "Chroma blocks of different size not supported");
+              int iDeltaWeight = (wp[j].codedWeight - (1 << wp[COMPONENT_Cb].log2WeightDenom));
               WRITE_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
 
               int range=pcSlice->getSPS()->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<pcSlice->getSPS()->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
-              int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
-              int iDeltaChroma = (wp[j].iOffset - pred);
+              int pred         = (range - ((range * wp[j].codedWeight) >> (wp[j].log2WeightDenom)));
+              int iDeltaChroma = (wp[j].codedOffset - pred);
               WRITE_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
             }
           }
@@ -2920,61 +2924,64 @@ void HLSWriter::xCodePredWeightTable(PicHeader *picHeader, const SPS *sps)
   for (int numRef = 0; numRef < NUM_REF_PIC_LIST_01 && moreSyntaxToBeParsed; numRef++)   // loop over l0 and l1 syntax elements
   {
     RefPicList refPicList = (numRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0);
-    // NOTE: wp[].uiLog2WeightDenom and wp[].bPresentFlag are actually per-channel-type settings.
+    // NOTE: wp[].log2WeightDenom and wp[].presentFlag are actually per-channel-type settings.
 
     for (int refIdx = 0; refIdx < numLxWeights; refIdx++)
     {
-      picHeader->getWpScaling(refPicList, refIdx, wp);
+      wp = picHeader->getWpScaling(refPicList, refIdx);
       if (!denomCoded)
       {
         int deltaDenom;
-        WRITE_UVLC(wp[COMPONENT_Y].uiLog2WeightDenom, "luma_log2_weight_denom");
+        WRITE_UVLC(wp[COMPONENT_Y].log2WeightDenom, "luma_log2_weight_denom");
 
         if (chroma)
         {
-          CHECK(wp[COMPONENT_Cb].uiLog2WeightDenom != wp[COMPONENT_Cr].uiLog2WeightDenom, "Chroma blocks of different size not supported");
-          deltaDenom = (wp[COMPONENT_Cb].uiLog2WeightDenom - wp[COMPONENT_Y].uiLog2WeightDenom);
+          CHECK(wp[COMPONENT_Cb].log2WeightDenom != wp[COMPONENT_Cr].log2WeightDenom,
+                "Chroma blocks of different size not supported");
+          deltaDenom = (wp[COMPONENT_Cb].log2WeightDenom - wp[COMPONENT_Y].log2WeightDenom);
           WRITE_SVLC(deltaDenom, "delta_chroma_log2_weight_denom");
         }
         denomCoded = true;
       }
-      WRITE_FLAG(wp[COMPONENT_Y].bPresentFlag, numRef == 0 ? "luma_weight_l0_flag[i]" : "luma_weight_l1_flag[i]");
-      totalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
+      WRITE_FLAG(wp[COMPONENT_Y].presentFlag, numRef == 0 ? "luma_weight_l0_flag[i]" : "luma_weight_l1_flag[i]");
+      totalSignalledWeightFlags += wp[COMPONENT_Y].presentFlag;
     }
     if (chroma)
     {
       for (int refIdx = 0; refIdx < numLxWeights; refIdx++)
       {
-        picHeader->getWpScaling(refPicList, refIdx, wp);
-        CHECK(wp[COMPONENT_Cb].bPresentFlag != wp[COMPONENT_Cr].bPresentFlag, "Inconsistent settings for chroma channels");
-        WRITE_FLAG(wp[COMPONENT_Cb].bPresentFlag, numRef == 0 ? "chroma_weight_l0_flag[i]" : "chroma_weight_l1_flag[i]");
-        totalSignalledWeightFlags += 2 * wp[COMPONENT_Cb].bPresentFlag;
+        wp = picHeader->getWpScaling(refPicList, refIdx);
+        CHECK(wp[COMPONENT_Cb].presentFlag != wp[COMPONENT_Cr].presentFlag,
+              "Inconsistent settings for chroma channels");
+        WRITE_FLAG(wp[COMPONENT_Cb].presentFlag, numRef == 0 ? "chroma_weight_l0_flag[i]" : "chroma_weight_l1_flag[i]");
+        totalSignalledWeightFlags += 2 * wp[COMPONENT_Cb].presentFlag;
       }
     }
 
     for (int refIdx = 0; refIdx < numLxWeights; refIdx++)
     {
-      picHeader->getWpScaling(refPicList, refIdx, wp);
-      if (wp[COMPONENT_Y].bPresentFlag)
+      wp = picHeader->getWpScaling(refPicList, refIdx);
+      if (wp[COMPONENT_Y].presentFlag)
       {
-        int deltaWeight = (wp[COMPONENT_Y].iWeight - (1 << wp[COMPONENT_Y].uiLog2WeightDenom));
+        int deltaWeight = (wp[COMPONENT_Y].codedWeight - (1 << wp[COMPONENT_Y].log2WeightDenom));
         WRITE_SVLC(deltaWeight, numRef == 0 ? "delta_luma_weight_l0[i]" : "delta_luma_weight_l1[i]");
-        WRITE_SVLC(wp[COMPONENT_Y].iOffset, numRef == 0 ? "luma_offset_l0[i]" : "luma_offset_l1[i]");
+        WRITE_SVLC(wp[COMPONENT_Y].codedOffset, numRef == 0 ? "luma_offset_l0[i]" : "luma_offset_l1[i]");
       }
 
       if (chroma)
       {
-        if (wp[COMPONENT_Cb].bPresentFlag)
+        if (wp[COMPONENT_Cb].presentFlag)
         {
           for (int j = COMPONENT_Cb; j < numberValidComponents; j++)
           {
-            CHECK(wp[COMPONENT_Cb].uiLog2WeightDenom != wp[COMPONENT_Cr].uiLog2WeightDenom, "Chroma blocks of different size not supported");
-            int deltaWeight = (wp[j].iWeight - (1 << wp[COMPONENT_Cb].uiLog2WeightDenom));
+            CHECK(wp[COMPONENT_Cb].log2WeightDenom != wp[COMPONENT_Cr].log2WeightDenom,
+                  "Chroma blocks of different size not supported");
+            int deltaWeight = (wp[j].codedWeight - (1 << wp[COMPONENT_Cb].log2WeightDenom));
             WRITE_SVLC(deltaWeight, numRef == 0 ? "delta_chroma_weight_l0[i]" : "delta_chroma_weight_l1[i]");
 
             int range = sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1 << sps->getBitDepth(CHANNEL_TYPE_CHROMA)) / 2 : 128;
-            int pred         = (range - ((range * wp[j].iWeight) >> (wp[j].uiLog2WeightDenom)));
-            int deltaChroma = (wp[j].iOffset - pred);
+            int pred        = (range - ((range * wp[j].codedWeight) >> (wp[j].log2WeightDenom)));
+            int deltaChroma = (wp[j].codedOffset - pred);
             WRITE_SVLC(deltaChroma, numRef == 0 ? "delta_chroma_offset_l0[i]" : "delta_chroma_offset_l1[i]");
           }
         }
diff --git a/source/Lib/EncoderLib/WeightPredAnalysis.cpp b/source/Lib/EncoderLib/WeightPredAnalysis.cpp
index c3dcee328..6db6870f2 100644
--- a/source/Lib/EncoderLib/WeightPredAnalysis.cpp
+++ b/source/Lib/EncoderLib/WeightPredAnalysis.cpp
@@ -235,10 +235,10 @@ WeightPredAnalysis::WeightPredAnalysis()
       for ( int comp=0 ; comp<MAX_NUM_COMPONENT ;comp++ )
       {
         WPScalingParam  *pwp   = &(m_wp[lst][refIdx][comp]);
-        pwp->bPresentFlag      = false;
-        pwp->uiLog2WeightDenom = 0;
-        pwp->iWeight           = 1;
-        pwp->iOffset           = 0;
+        pwp->presentFlag       = false;
+        pwp->log2WeightDenom   = 0;
+        pwp->codedWeight       = 1;
+        pwp->codedOffset       = 0;
       }
     }
   }
@@ -318,7 +318,7 @@ void  WeightPredAnalysis::xCheckWPEnable(Slice *const slice)
       for(int componentIndex = 0; componentIndex < ::getNumberValidComponents( slice->getSPS()->getChromaFormatIdc() ); componentIndex++)
       {
         WPScalingParam  *pwp = &(m_wp[lst][refIdx][componentIndex]);
-        presentCnt += (int)pwp->bPresentFlag;
+        presentCnt += (int) pwp->presentFlag;
       }
     }
   }
@@ -336,10 +336,10 @@ void  WeightPredAnalysis::xCheckWPEnable(Slice *const slice)
         {
           WPScalingParam  *pwp = &(m_wp[lst][refIdx][componentIndex]);
 
-          pwp->bPresentFlag      = false;
-          pwp->uiLog2WeightDenom = 0;
-          pwp->iWeight           = 1;
-          pwp->iOffset           = 0;
+          pwp->presentFlag     = false;
+          pwp->log2WeightDenom = 0;
+          pwp->codedWeight     = 1;
+          pwp->codedOffset     = 0;
         }
       }
     }
@@ -435,7 +435,7 @@ bool WeightPredAnalysis::xUpdatingWPParameters(Slice *const slice, const int log
         const int64_t refDC  = refWeightACDCParam[comp].iDC;
         const int64_t refAC  = refWeightACDCParam[comp].iAC;
 
-        // calculating iWeight and iOffset params
+        // calculating codedWeight and codedOffset params
         const double dWeight = (refAC==0) ? (double)1.0 : Clip3( -16.0, 15.0, ((double)currAC / (double)refAC) );
         const int weight     = (int)( 0.5 + dWeight * (double)(1<<log2Denom) );
         const int offset     = (int)( ((currDC<<log2Denom) - ((int64_t)weight * refDC) + (int64_t)realOffset) >> realLog2Denom );
@@ -462,10 +462,10 @@ bool WeightPredAnalysis::xUpdatingWPParameters(Slice *const slice, const int log
           return false;
         }
 
-        m_wp[refList][refIdxTemp][comp].bPresentFlag      = true;
-        m_wp[refList][refIdxTemp][comp].iWeight           = weight;
-        m_wp[refList][refIdxTemp][comp].iOffset           = clippedOffset;
-        m_wp[refList][refIdxTemp][comp].uiLog2WeightDenom = log2Denom;
+        m_wp[refList][refIdxTemp][comp].presentFlag     = true;
+        m_wp[refList][refIdxTemp][comp].codedWeight     = weight;
+        m_wp[refList][refIdxTemp][comp].codedOffset     = clippedOffset;
+        m_wp[refList][refIdxTemp][comp].log2WeightDenom = log2Denom;
       }
     }
   }
@@ -507,41 +507,45 @@ bool WeightPredAnalysis::xSelectWPHistExtClip(Slice *const slice, const int log2
         const int          width      = compBuf.width;
         const int          height     = compBuf.height;
         const int          bitDepth   = slice->getSPS()->getBitDepth(toChannelType(compID));
-              WPScalingParam &wp      = m_wp[refList][refIdxTemp][compID];
-              int          weight     = wp.iWeight;
-              int          offset     = wp.iOffset;
-              int          weightDef  = defaultWeight;
-              int          offsetDef  = 0;
+
+        WPScalingParam &wp = m_wp[refList][refIdxTemp][compID];
+
+        int weight    = wp.codedWeight;
+        int offset    = wp.codedOffset;
+        int weightDef = defaultWeight;
+        int offsetDef = 0;
 
         // calculate SAD costs with/without wp for luma
-              std::vector<int> histogramOrg;
-              std::vector<int> histogramRef;
-              uint64_t SADnoWP = std::numeric_limits<uint64_t>::max();
-              if (bUseHistogram && compID == COMPONENT_Y)
-              {
-                xCalcHistogram(pOrg, histogramOrg, width, height, orgStride, 1 << bitDepth);
-                xCalcHistogram(pRef, histogramRef, width, height, refStride, 1 << bitDepth);
+        std::vector<int> histogramOrg;
+        std::vector<int> histogramRef;
+        Distortion       SADnoWP = std::numeric_limits<Distortion>::max();
+        if (bUseHistogram && compID == COMPONENT_Y)
+        {
+          xCalcHistogram(pOrg, histogramOrg, width, height, orgStride, 1 << bitDepth);
+          xCalcHistogram(pRef, histogramRef, width, height, refStride, 1 << bitDepth);
 
-                std::vector<int> histogramRef_noWP;
-                xScaleHistogram(histogramRef, histogramRef_noWP, bitDepth, log2Denom, defaultWeight, 0, useHighPrecision);
-                SADnoWP = (uint64_t)xCalcHistCumulDistortion(histogramOrg, histogramRef_noWP);
-              }
-              else
-              {
-                SADnoWP = (uint64_t)xCalcSADvalueWPOptionalClip(bitDepth, pOrg, pRef, width, height, orgStride, refStride, log2Denom, defaultWeight, 0, useHighPrecision, bClipInitialSADWP);
-              }
+          std::vector<int> histogramRef_noWP;
+          xScaleHistogram(histogramRef, histogramRef_noWP, bitDepth, log2Denom, defaultWeight, 0, useHighPrecision);
+          SADnoWP = xCalcHistCumulDistortion(histogramOrg, histogramRef_noWP);
+        }
+        else
+        {
+          SADnoWP = xCalcSADvalueWPOptionalClip(bitDepth, pOrg, pRef, width, height, orgStride, refStride, log2Denom,
+                                                defaultWeight, 0, useHighPrecision, bClipInitialSADWP);
+        }
         if (SADnoWP > 0)
         {
-          uint64_t SADWP = std::numeric_limits<uint64_t>::max();
+          Distortion SADWP = std::numeric_limits<Distortion>::max();
           if (bUseHistogram && compID == COMPONENT_Y)
           {
             std::vector<int> histogramRef_WP;
             xScaleHistogram(histogramRef, histogramRef_WP, bitDepth, log2Denom, weight, offset, useHighPrecision);
-            SADWP = (uint64_t)xCalcHistCumulDistortion(histogramOrg, histogramRef_WP);
+            SADWP = xCalcHistCumulDistortion(histogramOrg, histogramRef_WP);
           }
           else
           {
-            SADWP = (uint64_t)xCalcSADvalueWPOptionalClip(bitDepth, pOrg, pRef, width, height, orgStride, refStride, log2Denom, weight, offset, useHighPrecision, bClipInitialSADWP);
+            SADWP = xCalcSADvalueWPOptionalClip(bitDepth, pOrg, pRef, width, height, orgStride, refStride, log2Denom,
+                                                weight, offset, useHighPrecision, bClipInitialSADWP);
           }
           const double dRatioSAD = (double)SADWP / (double)SADnoWP;
           double dRatioSr0SAD = std::numeric_limits<double>::max();
@@ -590,10 +594,10 @@ bool WeightPredAnalysis::xSelectWPHistExtClip(Slice *const slice, const int log2
 
           if(std::min(dRatioSr0SAD, std::min(dRatioSAD, dRatioSrSAD)) >= WEIGHT_PRED_SAD_RELATIVE_TO_NON_WEIGHT_PRED_SAD)
           {
-            wp.bPresentFlag      = false;
-            wp.iOffset           = 0;
-            wp.iWeight           = defaultWeight;
-            wp.uiLog2WeightDenom = log2Denom;
+            wp.presentFlag     = false;
+            wp.codedOffset     = 0;
+            wp.codedWeight     = defaultWeight;
+            wp.log2WeightDenom = log2Denom;
           }
           else
           {
@@ -604,32 +608,32 @@ bool WeightPredAnalysis::xSelectWPHistExtClip(Slice *const slice, const int log2
 
             if (dRatioSr0SAD < dRatioSrSAD && dRatioSr0SAD < dRatioSAD)
             {
-              wp.bPresentFlag      = true;
-              wp.iOffset           = offsetDef;
-              wp.iWeight           = weightDef;
-              wp.uiLog2WeightDenom = log2Denom;
+              wp.presentFlag     = true;
+              wp.codedOffset     = offsetDef;
+              wp.codedWeight     = weightDef;
+              wp.log2WeightDenom = log2Denom;
             }
             else if (dRatioSrSAD < dRatioSAD)
             {
-              wp.bPresentFlag      = true;
-              wp.iOffset           = offset;
-              wp.iWeight           = weight;
-              wp.uiLog2WeightDenom = log2Denom;
+              wp.presentFlag     = true;
+              wp.codedOffset     = offset;
+              wp.codedWeight     = weight;
+              wp.log2WeightDenom = log2Denom;
             }
           }
         }
         else // (SADnoWP <= 0)
         {
-          wp.bPresentFlag      = false;
-          wp.iOffset           = 0;
-          wp.iWeight           = defaultWeight;
-          wp.uiLog2WeightDenom = log2Denom;
+          wp.presentFlag     = false;
+          wp.codedOffset     = 0;
+          wp.codedWeight     = defaultWeight;
+          wp.log2WeightDenom = log2Denom;
         }
       }
 
       for (int comp = 1; comp < ::getNumberValidComponents(pPic.chromaFormat); comp++)
       {
-        m_wp[refList][refIdxTemp][comp].bPresentFlag = useChromaWeight;
+        m_wp[refList][refIdxTemp][comp].presentFlag = useChromaWeight;
       }
     }
   }
@@ -668,7 +672,9 @@ bool WeightPredAnalysis::xSelectWP(Slice *const slice, const int log2Denom)
         const int          bitDepth   = slice->getSPS()->getBitDepth(toChannelType(compID));
 
         // calculate SAD costs with/without wp for luma
-        SADWP   += xCalcSADvalueWP(bitDepth, pOrg, pRef, width, height, orgStride, refStride, log2Denom, m_wp[refList][refIdxTemp][compID].iWeight, m_wp[refList][refIdxTemp][compID].iOffset, useHighPrecisionPredictionWeighting);
+        SADWP += xCalcSADvalueWP(bitDepth, pOrg, pRef, width, height, orgStride, refStride, log2Denom,
+                                 m_wp[refList][refIdxTemp][compID].codedWeight,
+                                 m_wp[refList][refIdxTemp][compID].codedOffset, useHighPrecisionPredictionWeighting);
         SADnoWP += xCalcSADvalueWP(bitDepth, pOrg, pRef, width, height, orgStride, refStride, log2Denom, defaultWeight, 0, useHighPrecisionPredictionWeighting);
       }
 
@@ -679,10 +685,10 @@ bool WeightPredAnalysis::xSelectWP(Slice *const slice, const int log2Denom)
         for(int comp=0; comp < ::getNumberValidComponents(pPic.chromaFormat); comp++)
         {
           WPScalingParam &wp=m_wp[refList][refIdxTemp][comp];
-          wp.bPresentFlag      = false;
-          wp.iOffset           = 0;
-          wp.iWeight           = defaultWeight;
-          wp.uiLog2WeightDenom = log2Denom;
+          wp.presentFlag     = false;
+          wp.codedOffset     = 0;
+          wp.codedWeight     = defaultWeight;
+          wp.log2WeightDenom = log2Denom;
         }
       }
     }
-- 
GitLab