Index: trunk/source/Lib/TLibCommon/TComRdCost.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComRdCost.cpp	(revision 655)
+++ trunk/source/Lib/TLibCommon/TComRdCost.cpp	(revision 833)
@@ -266,4 +266,7 @@
   m_bUseEstimatedVSD        = false; 
 #endif
+#if H_3D_DBBP
+  m_bUseMask                = false;
+#endif
 }
 
@@ -326,4 +329,30 @@
   rcDistParam.DistFunc = m_afpDistortFunc[eDFunc + g_aucConvertToBit[ rcDistParam.iCols ] + 1 ];
   
+#if H_3D_DBBP
+  if( m_bUseMask )
+  {
+    if( eDFunc >= DF_SSE && eDFunc <= DF_SSE16N )
+    {
+      rcDistParam.DistFunc = TComRdCost::xGetMaskedSSE;
+    }
+    else if( eDFunc >= DF_SAD && eDFunc <= DF_SADS16N )
+    {
+      rcDistParam.DistFunc = TComRdCost::xGetMaskedSAD;
+    }
+    else if( eDFunc >= DF_HADS && eDFunc <= DF_HADS16N )
+    {
+      rcDistParam.DistFunc = TComRdCost::xGetMaskedHADs;
+    }
+    else if( eDFunc >= DF_VSD && eDFunc <= DF_VSD16N )
+    {
+      rcDistParam.DistFunc = TComRdCost::xGetMaskedVSD;
+    }
+    else if( eDFunc >= DF_SAD12 && eDFunc <= DF_SADS48 )
+    {
+      rcDistParam.DistFunc = TComRdCost::xGetMaskedSAD;
+    }
+  }
+#endif
+  
   // initialize
   rcDistParam.iSubShift  = 0;
@@ -357,4 +386,11 @@
   {
     rcDistParam.DistFunc = m_afpDistortFunc[45 ];
+  }
+#endif
+  
+#if H_3D_DBBP
+  if( m_bUseMask )
+  {
+    rcDistParam.DistFunc = TComRdCost::xGetMaskedSAD;
   }
 #endif
@@ -411,4 +447,11 @@
     rcDistParam.DistFunc = m_afpDistortFunc[DF_HADS + g_aucConvertToBit[ rcDistParam.iCols ] + 1 ];
   }
+  
+#if H_3D_DBBP
+  if( m_bUseMask )
+  {
+    rcDistParam.DistFunc = (bHADME)?TComRdCost::xGetMaskedHADs:TComRdCost::xGetMaskedSAD;
+  }
+#endif
   
   // initialize
@@ -433,4 +476,12 @@
   rcDP.bitDepth   = bitDepth;
   rcDP.DistFunc   = m_afpDistortFunc[ ( bHadamard ? DF_HADS : DF_SADS ) + g_aucConvertToBit[ iWidth ] + 1 ];
+  
+#if H_3D_DBBP
+  if( m_bUseMask )
+  {
+    rcDP.DistFunc = (bHadamard)?TComRdCost::xGetMaskedHADs:TComRdCost::xGetMaskedSAD;
+  }
+#endif
+  
 #if NS_HAD
   rcDP.bUseNSHAD  = bUseNSHAD;
@@ -586,4 +637,7 @@
   cDtParam.uiComp       = 255;    // just for assert: to be sure it was set before use, since only values 0,1 or 2 are allowed.
 
+#if SCU_HS_VSD_BUGFIX_IMPROV_G0163
+  cDtParam.bitDepth   = g_bitDepthY;
+#endif
   Dist dist = cDtParam.DistFunc( &cDtParam );
 
@@ -631,4 +685,227 @@
 // Distortion functions
 // ====================================================================================================================
+
+#if H_3D_DBBP
+// --------------------------------------------------------------------------------------------------------------------
+// Masked distortion functions
+// --------------------------------------------------------------------------------------------------------------------
+
+UInt TComRdCost::xGetMaskedSSE( DistParam* pcDtParam )
+{
+  Pel* piOrg   = pcDtParam->pOrg;
+  Pel* piCur   = pcDtParam->pCur;
+  Int  iRows   = pcDtParam->iRows;
+  Int  iCols   = pcDtParam->iCols;
+  Int  iStrideOrg = pcDtParam->iStrideOrg;
+  Int  iStrideCur = pcDtParam->iStrideCur;
+  
+  UInt uiSum = 0;
+  
+  UInt uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
+  
+  Int iTemp;
+  
+  for( ; iRows != 0; iRows-- )
+  {
+    for (Int n = 0; n < iCols; n++ )
+    {
+      if( piOrg[n] != DBBP_INVALID_SHORT )
+      {
+        iTemp = piOrg[n  ] - piCur[n  ];
+        uiSum += ( iTemp * iTemp ) >> uiShift;
+      }
+    }
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+  
+  return ( uiSum );
+}
+
+UInt TComRdCost::xGetMaskedSAD( DistParam* pcDtParam )
+{
+  
+  AOF(!pcDtParam->bApplyWeight);
+#if H_3D_IC
+  AOF(!pcDtParam->bUseIC);
+#endif
+  
+  Pel* piOrg   = pcDtParam->pOrg;
+  Pel* piCur   = pcDtParam->pCur;
+  Int  iRows   = pcDtParam->iRows;
+  Int  iCols   = pcDtParam->iCols;
+  Int  iStrideCur = pcDtParam->iStrideCur;
+  Int  iStrideOrg = pcDtParam->iStrideOrg;
+  
+  UInt uiSum = 0;
+  
+  for( ; iRows != 0; iRows-- )
+  {
+    for (Int n = 0; n < iCols; n++ )
+    {
+      if( piOrg[n] != DBBP_INVALID_SHORT )
+      {
+        uiSum += abs( piOrg[n] - piCur[n] );
+      }
+    }
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+  
+  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
+}
+
+UInt TComRdCost::xGetMaskedHADs( DistParam* pcDtParam )
+{
+  AOF(!pcDtParam->bApplyWeight);
+#if H_3D_IC
+  AOF(!pcDtParam->bUseIC);
+#endif
+  Pel* piOrg   = pcDtParam->pOrg;
+  Pel* piCur   = pcDtParam->pCur;
+  Int  iRows   = pcDtParam->iRows;
+  Int  iCols   = pcDtParam->iCols;
+  Int  iStrideCur = pcDtParam->iStrideCur;
+  Int  iStrideOrg = pcDtParam->iStrideOrg;
+  Int  iStep  = pcDtParam->iStep;
+  
+  Int  x, y;
+  
+  UInt uiSum = 0;
+  
+#if NS_HAD
+  if( ( ( iRows % 8 == 0) && (iCols % 8 == 0) && ( iRows == iCols ) ) || ( ( iRows % 8 == 0 ) && (iCols % 8 == 0) && !pcDtParam->bUseNSHAD ) )
+#else
+    if( ( iRows % 8 == 0) && (iCols % 8 == 0) )
+#endif
+    {
+      Int  iOffsetOrg = iStrideOrg<<3;
+      Int  iOffsetCur = iStrideCur<<3;
+      for ( y=0; y<iRows; y+= 8 )
+      {
+        for ( x=0; x<iCols; x+= 8 )
+        {
+          if( piOrg[x] != DBBP_INVALID_SHORT )
+          {
+            uiSum += xCalcHADs8x8( &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
+          }
+        }
+        piOrg += iOffsetOrg;
+        piCur += iOffsetCur;
+      }
+    }
+#if NS_HAD
+    else if ( ( iCols > 8 ) && ( iCols > iRows ) && pcDtParam->bUseNSHAD )
+    {
+      Int  iOffsetOrg = iStrideOrg<<2;
+      Int  iOffsetCur = iStrideCur<<2;
+      for ( y=0; y<iRows; y+= 4 )
+      {
+        for ( x=0; x<iCols; x+= 16 )
+        {
+          if( piOrg[x] != DBBP_INVALID_SHORT )
+          {
+            uiSum += xCalcHADs16x4( &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
+          }
+        }
+        piOrg += iOffsetOrg;
+        piCur += iOffsetCur;
+      }
+    }
+    else if ( ( iRows > 8 ) && ( iCols < iRows ) && pcDtParam->bUseNSHAD )
+    {
+      Int  iOffsetOrg = iStrideOrg<<4;
+      Int  iOffsetCur = iStrideCur<<4;
+      for ( y=0; y<iRows; y+= 16 )
+      {
+        for ( x=0; x<iCols; x+= 4 )
+        {
+          if( piOrg[x] != DBBP_INVALID_SHORT )
+          {
+            uiSum += xCalcHADs4x16( &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
+          }
+        }
+        piOrg += iOffsetOrg;
+        piCur += iOffsetCur;
+      }
+    }
+#endif
+    else if( ( iRows % 4 == 0) && (iCols % 4 == 0) )
+    {
+      Int  iOffsetOrg = iStrideOrg<<2;
+      Int  iOffsetCur = iStrideCur<<2;
+      
+      for ( y=0; y<iRows; y+= 4 )
+      {
+        for ( x=0; x<iCols; x+= 4 )
+        {
+          if( piOrg[x] != DBBP_INVALID_SHORT )
+          {
+            uiSum += xCalcHADs4x4( &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
+          }
+        }
+        piOrg += iOffsetOrg;
+        piCur += iOffsetCur;
+      }
+    }
+    else if( ( iRows % 2 == 0) && (iCols % 2 == 0) )
+    {
+      Int  iOffsetOrg = iStrideOrg<<1;
+      Int  iOffsetCur = iStrideCur<<1;
+      for ( y=0; y<iRows; y+=2 )
+      {
+        for ( x=0; x<iCols; x+=2 )
+        {
+          if( piOrg[x] != DBBP_INVALID_SHORT )
+          {
+            uiSum += xCalcHADs2x2( &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
+          }
+        }
+        piOrg += iOffsetOrg;
+        piCur += iOffsetCur;
+      }
+    }
+    else
+    {
+      assert(false);
+    }
+  
+  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
+}
+
+UInt TComRdCost::xGetMaskedVSD( DistParam* pcDtParam )
+{
+  Pel* piOrg    = pcDtParam->pOrg;
+  Pel* piCur    = pcDtParam->pCur;
+  Pel* piVirRec = pcDtParam->pVirRec;
+  Pel* piVirOrg = pcDtParam->pVirOrg;
+  Int  iRows    = pcDtParam->iRows;
+  Int  iCols    = pcDtParam->iCols;
+  Int  iStrideOrg = pcDtParam->iStrideOrg;
+  Int  iStrideCur = pcDtParam->iStrideCur;
+  Int  iStrideVir = pcDtParam->iStrideVir;
+  
+  UInt uiSum = 0;
+  UInt uiShift = DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8)<<1;
+  
+  Int dDM;
+  
+  for ( Int y = 0 ; y < iRows ; y++ )
+  {
+    for (Int x = 0; x < iCols; x++ )
+    {
+      if( piOrg[x] != DBBP_INVALID_SHORT )
+      {
+        dDM = (Int) ( piOrg[x  ] - piCur[x  ] );
+        uiSum += getVSDEstimate( dDM, piOrg, iStrideOrg, piVirRec, piVirOrg, iStrideVir, x, y ) >> uiShift;
+      }
+    }
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+  
+  return ( uiSum );
+}
+#endif
 
 // --------------------------------------------------------------------------------------------------------------------
@@ -2747,5 +3024,10 @@
   dD = ( (Double) ( dDM >> DISTORTION_PRECISION_ADJUSTMENT( g_bitDepthY - 8 ) ) ) * m_dDisparityCoeff;
 
+#if SCU_HS_VSD_BUGFIX_IMPROV_G0163
+  Double dDepthWeight = ( pOrg[x] >=  ( (1<<(g_bitDepthY - 3)) + (1<<(g_bitDepthY - 2)) ) ? 4 : pOrg[x] > ((1<<g_bitDepthY) >> 4) ? (Float)(pOrg[x] - ((1<<g_bitDepthY) >> 4))/(Float)((1<<g_bitDepthY) >> 3) + 1 : 1.0 );
+  Double dTemp = ( 0.5 * fabs(dD) * dDepthWeight * ( abs( (Int) pVirRec[ x+y*iVirStride ] - (Int) pVirRec[ x-1+y*iVirStride ] ) + abs( (Int) pVirRec[ x+y*iVirStride ] - (Int) pVirRec[ x+1+y*iVirStride ] ) ) );
+#else
   Double dTemp = ( 0.5 * fabs(dD) * ( abs( (Int) pVirRec[ x+y*iVirStride ] - (Int) pVirRec[ x-1+y*iVirStride ] ) + abs( (Int) pVirRec[ x+y*iVirStride ] - (Int) pVirRec[ x+1+y*iVirStride ] ) ) );
+#endif  
   iTemp = (Int) (((dTemp) < 0)? (Int)((dTemp) - 0.5) : (Int)((dTemp) + 0.5));
 
