Index: trunk/source/Lib/TLibCommon/TComWeightPrediction.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComWeightPrediction.cpp	(revision 56)
+++ trunk/source/Lib/TLibCommon/TComWeightPrediction.cpp	(revision 608)
@@ -4,5 +4,5 @@
  * granted under this license.  
  *
- * Copyright (c) 2010-2012, ITU/ISO/IEC
+ * Copyright (c) 2010-2013, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -41,4 +41,22 @@
 #include "TComInterpolationFilter.h"
 
+static inline Pel weightBidirY( Int w0, Pel P0, Int w1, Pel P1, Int round, Int shift, Int offset)
+{
+  return ClipY( ( (w0*(P0 + IF_INTERNAL_OFFS) + w1*(P1 + IF_INTERNAL_OFFS) + round + (offset << (shift-1))) >> shift ) );
+}
+static inline Pel weightBidirC( Int w0, Pel P0, Int w1, Pel P1, Int round, Int shift, Int offset)
+{
+  return ClipC( ( (w0*(P0 + IF_INTERNAL_OFFS) + w1*(P1 + IF_INTERNAL_OFFS) + round + (offset << (shift-1))) >> shift ) );
+}
+
+static inline Pel weightUnidirY( Int w0, Pel P0, Int round, Int shift, Int offset)
+{
+  return ClipY( ( (w0*(P0 + IF_INTERNAL_OFFS) + round) >> shift ) + offset );
+}
+static inline Pel weightUnidirC( Int w0, Pel P0, Int round, Int shift, Int offset)
+{
+  return ClipC( ( (w0*(P0 + IF_INTERNAL_OFFS) + round) >> shift ) + offset );
+}
+
 // ====================================================================================================================
 // Class definition
@@ -78,7 +96,7 @@
   Int w0      = wp0[0].w;
   Int offset  = wp0[0].offset;
-  Int shiftNum = IF_INTERNAL_PREC - ( g_uiBitDepth + g_uiBitIncrement );
+  Int shiftNum = IF_INTERNAL_PREC - g_bitDepthY;
   Int shift   = wp0[0].shift + shiftNum;
-  Int round   = (1<<(shift-1)) * bRound;
+  Int round   = shift?(1<<(shift-1)) * bRound:0;
   Int w1      = wp1[0].w;
 
@@ -91,8 +109,8 @@
     {
       // note: luma min width is 4
-      pDstY[x] = weightBidir(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
-      pDstY[x] = weightBidir(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
-      pDstY[x] = weightBidir(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
-      pDstY[x] = weightBidir(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
+      pDstY[x] = weightBidirY(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
+      pDstY[x] = weightBidirY(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
+      pDstY[x] = weightBidirY(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
+      pDstY[x] = weightBidirY(w0,pSrcY0[x], w1,pSrcY1[x], round, shift, offset); x--;
     }
     pSrcY0 += iSrc0Stride;
@@ -105,6 +123,7 @@
   w0      = wp0[1].w;
   offset  = wp0[1].offset;
+  shiftNum = IF_INTERNAL_PREC - g_bitDepthC;
   shift   = wp0[1].shift + shiftNum;
-  round   = (1<<(shift-1));
+  round   = shift?(1<<(shift-1)):0;
   w1      = wp1[1].w;
 
@@ -121,6 +140,6 @@
     {
       // note: chroma min width is 2
-      pDstU[x] = weightBidir(w0,pSrcU0[x], w1,pSrcU1[x], round, shift, offset); x--;
-      pDstU[x] = weightBidir(w0,pSrcU0[x], w1,pSrcU1[x], round, shift, offset); x--;
+      pDstU[x] = weightBidirC(w0,pSrcU0[x], w1,pSrcU1[x], round, shift, offset); x--;
+      pDstU[x] = weightBidirC(w0,pSrcU0[x], w1,pSrcU1[x], round, shift, offset); x--;
     }
     pSrcU0 += iSrc0Stride;
@@ -133,5 +152,5 @@
   offset  = wp0[2].offset;
   shift   = wp0[2].shift + shiftNum;
-  round   = (1<<(shift-1));
+  round   = shift?(1<<(shift-1)):0;
   w1      = wp1[2].w;
 
@@ -141,6 +160,6 @@
     {
       // note: chroma min width is 2
-      pDstV[x] = weightBidir(w0,pSrcV0[x], w1,pSrcV1[x], round, shift, offset); x--;
-      pDstV[x] = weightBidir(w0,pSrcV0[x], w1,pSrcV1[x], round, shift, offset); x--;
+      pDstV[x] = weightBidirC(w0,pSrcV0[x], w1,pSrcV1[x], round, shift, offset); x--;
+      pDstV[x] = weightBidirC(w0,pSrcV0[x], w1,pSrcV1[x], round, shift, offset); x--;
     }
     pSrcV0 += iSrc0Stride;
@@ -174,7 +193,7 @@
   Int w0      = wp0[0].w;
   Int offset  = wp0[0].offset;
-  Int shiftNum = IF_INTERNAL_PREC - ( g_uiBitDepth + g_uiBitIncrement );
+  Int shiftNum = IF_INTERNAL_PREC - g_bitDepthY;
   Int shift   = wp0[0].shift + shiftNum;
-  Int round   = (1<<(shift-1));
+  Int round   = shift?(1<<(shift-1)):0;
   UInt  iSrc0Stride = pcYuvSrc0->getStride();
   UInt  iDstStride  = rpcYuvDst->getStride();
@@ -185,8 +204,8 @@
     {
       // note: luma min width is 4
-      pDstY[x] = weightUnidir(w0,pSrcY0[x], round, shift, offset); x--;
-      pDstY[x] = weightUnidir(w0,pSrcY0[x], round, shift, offset); x--;
-      pDstY[x] = weightUnidir(w0,pSrcY0[x], round, shift, offset); x--;
-      pDstY[x] = weightUnidir(w0,pSrcY0[x], round, shift, offset); x--;
+      pDstY[x] = weightUnidirY(w0,pSrcY0[x], round, shift, offset); x--;
+      pDstY[x] = weightUnidirY(w0,pSrcY0[x], round, shift, offset); x--;
+      pDstY[x] = weightUnidirY(w0,pSrcY0[x], round, shift, offset); x--;
+      pDstY[x] = weightUnidirY(w0,pSrcY0[x], round, shift, offset); x--;
     }
     pSrcY0 += iSrc0Stride;
@@ -197,6 +216,7 @@
   w0      = wp0[1].w;
   offset  = wp0[1].offset;
+  shiftNum = IF_INTERNAL_PREC - g_bitDepthC;
   shift   = wp0[1].shift + shiftNum;
-  round   = (1<<(shift-1));
+  round   = shift?(1<<(shift-1)):0;
 
   iSrc0Stride = pcYuvSrc0->getCStride();
@@ -211,6 +231,6 @@
     {
       // note: chroma min width is 2
-      pDstU[x] = weightUnidir(w0,pSrcU0[x], round, shift, offset); x--;
-      pDstU[x] = weightUnidir(w0,pSrcU0[x], round, shift, offset); x--;
+      pDstU[x] = weightUnidirC(w0,pSrcU0[x], round, shift, offset); x--;
+      pDstU[x] = weightUnidirC(w0,pSrcU0[x], round, shift, offset); x--;
     }
     pSrcU0 += iSrc0Stride;
@@ -222,5 +242,5 @@
   offset  = wp0[2].offset;
   shift   = wp0[2].shift + shiftNum;
-  round   = (1<<(shift-1));
+  round   = shift?(1<<(shift-1)):0;
 
   for ( y = iHeight-1; y >= 0; y-- )
@@ -229,6 +249,6 @@
     {
       // note: chroma min width is 2
-      pDstV[x] = weightUnidir(w0,pSrcV0[x], round, shift, offset); x--;
-      pDstV[x] = weightUnidir(w0,pSrcV0[x], round, shift, offset); x--;
+      pDstV[x] = weightUnidirC(w0,pSrcV0[x], round, shift, offset); x--;
+      pDstV[x] = weightUnidirC(w0,pSrcV0[x], round, shift, offset); x--;
     }
     pSrcV0 += iSrc0Stride;
@@ -249,16 +269,16 @@
  * \returns Void
  */
-Void TComWeightPrediction::getWpScaling( TComDataCU* pcCU, Int iRefIdx0, Int iRefIdx1, wpScalingParam *&wp0, wpScalingParam *&wp1, Int ibdi)
-{
+Void TComWeightPrediction::getWpScaling( TComDataCU* pcCU, Int iRefIdx0, Int iRefIdx1, wpScalingParam *&wp0, wpScalingParam *&wp1)
+{
+  assert(iRefIdx0 >= 0 || iRefIdx1 >= 0);
+  
   TComSlice*      pcSlice       = pcCU->getSlice();
   TComPPS*        pps           = pcCU->getSlice()->getPPS();
-  UInt            uiWPBiPredIdc = pps->getWPBiPredIdc();
+  Bool            wpBiPred = pps->getWPBiPred();
   wpScalingParam* pwp;
   Bool            bBiDir        = (iRefIdx0>=0 && iRefIdx1>=0);
   Bool            bUniDir       = !bBiDir;
 
-  m_ibdi = ibdi;
-
-  if ( bUniDir || (uiWPBiPredIdc==1) )
+  if ( bUniDir || wpBiPred )
   { // explicit --------------------
     if ( iRefIdx0 >= 0 )
@@ -271,42 +291,4 @@
     }
   }
-  else if ( uiWPBiPredIdc == 2 )
-  { // implicit --------------------
-    Int poc0    = pcSlice->getRefPOC(REF_PIC_LIST_0, iRefIdx0);
-    Int poc1    = pcSlice->getRefPOC(REF_PIC_LIST_1, iRefIdx1);
-    Int pocCur  = pcSlice->getPOC();
-    Int td      = Clip3(-128,127,poc1 - poc0);
-    Int tb      = Clip3(-128,127,pocCur - poc0);
-    Int tx      = ( td != 0 ) ? ( ( 16384 + abs( td / 2 ) ) / td ) : (0);
-    Int DistScaleFactor = Clip3( -1024, 1023, ( tb * tx + 32 ) >> 6 );
-
-    Bool  implicitScale=true;
-    if ( poc1==poc0 || (DistScaleFactor>>2)<(-64) || (DistScaleFactor>>2)>128 )
-    {
-      implicitScale = false;
-    }
-
-    for ( int e=0 ; e<2 ; e++ )
-    {
-      pwp = (e==0) ? m_wp0 : m_wp1;
-      for ( int k=0 ; k<3 ; k++ ) // 3 components: Y,U,V
-      {   
-        pwp->uiLog2WeightDenom = 5;  pwp->iOffset = 0;  pwp->bPresentFlag = true;  
-        if ( implicitScale )
-        {
-          pwp->iWeight = (e==0) ? (64 - (DistScaleFactor >> 2)) : (DistScaleFactor >> 2);
-        }
-        else
-        {
-          pwp->iWeight = 32;
-        }
-        pwp++;
-      }
-
-    }
-
-    wp0 = m_wp0;
-    wp1 = m_wp1;
-  }
   else
   {
@@ -325,10 +307,11 @@
   if ( bBiDir )
   { // Bi-Dir case
-    for ( int yuv=0 ; yuv<3 ; yuv++ )
-    {
+    for ( Int yuv=0 ; yuv<3 ; yuv++ )
+    {
+      Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
       wp0[yuv].w      = wp0[yuv].iWeight;
-      wp0[yuv].o      = wp0[yuv].iOffset * (1 << (m_ibdi-8));
+      wp0[yuv].o      = wp0[yuv].iOffset * (1 << (bitDepth-8));
       wp1[yuv].w      = wp1[yuv].iWeight;
-      wp1[yuv].o      = wp1[yuv].iOffset * (1 << (m_ibdi-8));
+      wp1[yuv].o      = wp1[yuv].iOffset * (1 << (bitDepth-8));
       wp0[yuv].offset = wp0[yuv].o + wp1[yuv].o;
       wp0[yuv].shift  = wp0[yuv].uiLog2WeightDenom + 1;
@@ -342,22 +325,11 @@
   {  // Unidir
     pwp = (iRefIdx0>=0) ? wp0 : wp1 ;
-    if ( uiWPBiPredIdc == 2 )
-    {
-      for ( int yuv=0 ; yuv<3 ; yuv++ )
-      {
-        pwp[yuv].w      = 1;
-        pwp[yuv].offset = 0;
-        pwp[yuv].shift  = 0;
-        pwp[yuv].round  = 0;
-      }
-    }
-    else {
-      for ( int yuv=0 ; yuv<3 ; yuv++ )
-      {
-        pwp[yuv].w      = pwp[yuv].iWeight;
-        pwp[yuv].offset = pwp[yuv].iOffset * (1 << (m_ibdi-8));
-        pwp[yuv].shift  = pwp[yuv].uiLog2WeightDenom;
-        pwp[yuv].round  = (pwp[yuv].uiLog2WeightDenom>=1) ? (1 << (pwp[yuv].uiLog2WeightDenom-1)) : (0);
-      }
+    for ( Int yuv=0 ; yuv<3 ; yuv++ )
+    {
+      Int bitDepth = yuv ? g_bitDepthC : g_bitDepthY;
+      pwp[yuv].w      = pwp[yuv].iWeight;
+      pwp[yuv].offset = pwp[yuv].iOffset * (1 << (bitDepth-8));
+      pwp[yuv].shift  = pwp[yuv].uiLog2WeightDenom;
+      pwp[yuv].round  = (pwp[yuv].uiLog2WeightDenom>=1) ? (1 << (pwp[yuv].uiLog2WeightDenom-1)) : (0);
     }
   }
@@ -380,9 +352,7 @@
   wpScalingParam  *pwp0, *pwp1;
   TComPPS         *pps = pcCU->getSlice()->getPPS();
-
-  assert( pps->getWPBiPredIdc() != 0 );
-
-  Int ibdi = (g_uiBitDepth+g_uiBitIncrement);
-  getWpScaling(pcCU, iRefIdx0, iRefIdx1, pwp0, pwp1, ibdi);
+  assert( pps->getWPBiPred());
+
+  getWpScaling(pcCU, iRefIdx0, iRefIdx1, pwp0, pwp1);
 
   if( iRefIdx0 >= 0 && iRefIdx1 >= 0 )
@@ -416,5 +386,5 @@
  * \returns Void
  */
-Void TComWeightPrediction::xWeightedPredictionUni( TComDataCU* pcCU, TComYuv* pcYuvSrc, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Int iPartIdx, Int iRefIdx)
+Void TComWeightPrediction::xWeightedPredictionUni( TComDataCU* pcCU, TComYuv* pcYuvSrc, UInt uiPartAddr, Int iWidth, Int iHeight, RefPicList eRefPicList, TComYuv*& rpcYuvPred, Int iRefIdx)
 { 
   wpScalingParam  *pwp, *pwpTmp;
@@ -425,13 +395,11 @@
   assert (iRefIdx >= 0);
 
-  Int ibdi = (g_uiBitDepth+g_uiBitIncrement);
-
   if ( eRefPicList == REF_PIC_LIST_0 )
   {
-    getWpScaling(pcCU, iRefIdx, -1, pwp, pwpTmp, ibdi);
+    getWpScaling(pcCU, iRefIdx, -1, pwp, pwpTmp);
   }
   else
   {
-    getWpScaling(pcCU, -1, iRefIdx, pwpTmp, pwp, ibdi);
+    getWpScaling(pcCU, -1, iRefIdx, pwpTmp, pwp);
   }
   addWeightUni( pcYuvSrc, uiPartAddr, iWidth, iHeight, pwp, rpcYuvPred );
