Index: trunk/source/Lib/TLibCommon/TComRdCost.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComRdCost.cpp	(revision 1196)
+++ trunk/source/Lib/TLibCommon/TComRdCost.cpp	(revision 1313)
@@ -2,7 +2,7 @@
  * License, included below. This software may be subject to other third party
  * and contributor rights, including patent rights, and no such rights are
- * granted under this license.  
+ * granted under this license.
  *
-* Copyright (c) 2010-2015, ITU/ISO/IEC
+ * Copyright (c) 2010-2015, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -38,8 +38,10 @@
 #include <math.h>
 #include <assert.h>
+#include <limits>
 #include "TComRom.h"
 #include "TComRdCost.h"
-#if H_3D
+#if NH_3D
 #include "TComDataCU.h"
+#include "TComRectangle.h"
 #endif
 
@@ -47,5 +49,5 @@
 //! \{
 
-#if H_3D_VSO
+#if NH_3D_VSO
 // SAIT_VSO_EST_A0033
 Double TComRdCost::m_dDisparityCoeff = 1.0;
@@ -58,19 +60,16 @@
 TComRdCost::~TComRdCost()
 {
-#if !FIX203
-  xUninit();
-#endif
 }
 
 // Calculate RD functions
-#if H_3D_VSO
+#if NH_3D_VSO
 Double TComRdCost::calcRdCost( UInt uiBits, Dist uiDistortion, Bool bFlag, DFunc eDFunc )
 #else
-Double TComRdCost::calcRdCost( UInt uiBits, UInt uiDistortion, Bool bFlag, DFunc eDFunc )
+Double TComRdCost::calcRdCost( UInt uiBits, Distortion uiDistortion, Bool bFlag, DFunc eDFunc )
 #endif
 {
   Double dRdCost = 0.0;
   Double dLambda = 0.0;
-  
+
   switch ( eDFunc )
   {
@@ -79,5 +78,9 @@
       break;
     case DF_SAD:
-      dLambda = (Double)m_uiLambdaMotionSAD;
+#if RExt__HIGH_BIT_DEPTH_SUPPORT
+      dLambda = m_dLambdaMotionSAD[0]; // 0 is valid, because for lossless blocks, the cost equation is modified to compensate.
+#else
+      dLambda = (Double)m_uiLambdaMotionSAD[0]; // 0 is valid, because for lossless blocks, the cost equation is modified to compensate.
+#endif
       break;
     case DF_DEFAULT:
@@ -91,13 +94,16 @@
       break;
   }
-  
-  if (bFlag)
+
+  if (bFlag) //NOTE: this "bFlag" is never true
   {
     // Intra8x8, Intra4x4 Block only...
-#if SEQUENCE_LEVEL_LOSSLESS
-    dRdCost = (Double)(uiBits);
-#else
-    dRdCost = (((Double)uiDistortion) + ((Double)uiBits * dLambda));
-#endif
+    if (m_costMode != COST_STANDARD_LOSSY)
+    {
+      dRdCost = (Double(uiDistortion) / dLambda) + Double(uiBits); // all lossless costs would have uiDistortion=0, and therefore this cost function can be used.
+    }
+    else
+    {
+      dRdCost = (((Double)uiDistortion) + ((Double)uiBits * dLambda));
+    }
   }
   else
@@ -105,22 +111,31 @@
     if (eDFunc == DF_SAD)
     {
-      dRdCost = ((Double)uiDistortion + (Double)((Int)(uiBits * dLambda+.5)>>16));
-      dRdCost = (Double)(UInt)floor(dRdCost);
+      if (m_costMode != COST_STANDARD_LOSSY)
+      {
+        dRdCost = ((Double(uiDistortion) * 65536) / dLambda) + Double(uiBits); // all lossless costs would have uiDistortion=0, and therefore this cost function can be used.
+      }
+      else
+      {
+        dRdCost = floor(Double(uiDistortion) + (floor((Double(uiBits) * dLambda) + 0.5) / 65536.0));
+      }
     }
     else
     {
-#if SEQUENCE_LEVEL_LOSSLESS
-      dRdCost = (Double)(uiBits);
-#else
-      dRdCost = ((Double)uiDistortion + (Double)((Int)(uiBits * dLambda+.5)));
-      dRdCost = (Double)(UInt)floor(dRdCost);
-#endif
-    }
-  }
-  
+      if (m_costMode != COST_STANDARD_LOSSY)
+      {
+        dRdCost = (Double(uiDistortion) / dLambda) + Double(uiBits); // all lossless costs would have uiDistortion=0, and therefore this cost function can be used.
+      }
+      else
+      {
+        dRdCost = floor(Double(uiDistortion) + (Double(uiBits) * dLambda) + 0.5);
+      }
+    }
+  }
+
+  D_PRINT_INDENT( g_traceRDCost,  "Dist: " + n2s(uiDistortion) + " Bits: " + n2s(uiBits) + " RD Cost: " + n2s(dRdCost)); 
   return dRdCost;
 }
 
-#if H_3D_VSO
+#if NH_3D_VSO
 Double TComRdCost::calcRdCost64( UInt64 uiBits, Dist64 uiDistortion, Bool bFlag, DFunc eDFunc )
 #else
@@ -130,5 +145,5 @@
   Double dRdCost = 0.0;
   Double dLambda = 0.0;
-  
+
   switch ( eDFunc )
   {
@@ -137,5 +152,9 @@
       break;
     case DF_SAD:
-      dLambda = (Double)m_uiLambdaMotionSAD;
+#if RExt__HIGH_BIT_DEPTH_SUPPORT
+      dLambda = m_dLambdaMotionSAD[0]; // 0 is valid, because for lossless blocks, the cost equation is modified to compensate.
+#else
+      dLambda = (Double)m_uiLambdaMotionSAD[0]; // 0 is valid, because for lossless blocks, the cost equation is modified to compensate.
+#endif
       break;
     case DF_DEFAULT:
@@ -149,13 +168,16 @@
       break;
   }
-  
-  if (bFlag)
+
+  if (bFlag) //NOTE: this "bFlag" is never true
   {
     // Intra8x8, Intra4x4 Block only...
-#if SEQUENCE_LEVEL_LOSSLESS
-    dRdCost = (Double)(uiBits);
-#else
-    dRdCost = (((Double)(Int64)uiDistortion) + ((Double)(Int64)uiBits * dLambda));
-#endif
+    if (m_costMode != COST_STANDARD_LOSSY)
+    {
+      dRdCost = (Double(uiDistortion) / dLambda) + Double(uiBits); // all lossless costs would have uiDistortion=0, and therefore this cost function can be used.
+    }
+    else
+    {
+      dRdCost = (((Double)(Int64)uiDistortion) + ((Double)(Int64)uiBits * dLambda));
+    }
   }
   else
@@ -163,27 +185,54 @@
     if (eDFunc == DF_SAD)
     {
-      dRdCost = ((Double)(Int64)uiDistortion + (Double)((Int)((Int64)uiBits * dLambda+.5)>>16));
-      dRdCost = (Double)(UInt)floor(dRdCost);
+      if (m_costMode != COST_STANDARD_LOSSY)
+      {
+        dRdCost = ((Double(uiDistortion) * 65536) / dLambda) + Double(uiBits); // all lossless costs would have uiDistortion=0, and therefore this cost function can be used.
+      }
+      else
+      {
+        dRdCost = floor(Double(uiDistortion) + (floor((Double(uiBits) * dLambda) + 0.5) / 65536.0));
+      }
     }
     else
     {
-#if SEQUENCE_LEVEL_LOSSLESS
-      dRdCost = (Double)(uiBits);
-#else
-      dRdCost = ((Double)(Int64)uiDistortion + (Double)((Int)((Int64)uiBits * dLambda+.5)));
-      dRdCost = (Double)(UInt)floor(dRdCost);
-#endif
-    }
-  }
-  
+      if (m_costMode != COST_STANDARD_LOSSY)
+      {
+        dRdCost = (Double(uiDistortion) / dLambda) + Double(uiBits); // all lossless costs would have uiDistortion=0, and therefore this cost function can be used.
+      }
+      else
+      {
+        dRdCost = floor(Double(uiDistortion) + (Double(uiBits) * dLambda) + 0.5);
+      }
+    }
+  }
+
   return dRdCost;
 }
 
-Void TComRdCost::setLambda( Double dLambda )
+Void TComRdCost::setLambda( Double dLambda, const BitDepths &bitDepths )
 {
   m_dLambda           = dLambda;
   m_sqrtLambda        = sqrt(m_dLambda);
-  m_uiLambdaMotionSAD = (UInt)floor(65536.0 * m_sqrtLambda);
-  m_uiLambdaMotionSSE = (UInt)floor(65536.0 * m_dLambda   );
+#if RExt__HIGH_BIT_DEPTH_SUPPORT
+  m_dLambdaMotionSAD[0] = 65536.0 * m_sqrtLambda;
+  m_dLambdaMotionSSE[0] = 65536.0 * m_dLambda;
+#if FULL_NBIT
+  dLambda = 0.57 * pow(2.0, ((LOSSLESS_AND_MIXED_LOSSLESS_RD_COST_TEST_QP_PRIME - 12) / 3.0));
+#else
+  dLambda = 0.57 * pow(2.0, ((LOSSLESS_AND_MIXED_LOSSLESS_RD_COST_TEST_QP_PRIME - 12 - 6 * (bitDepths.recon[CHANNEL_TYPE_LUMA] - 8)) / 3.0));
+#endif
+  m_dLambdaMotionSAD[1] = 65536.0 * sqrt(dLambda);
+  m_dLambdaMotionSSE[1] = 65536.0 * dLambda;
+#else
+  m_uiLambdaMotionSAD[0] = (UInt)floor(65536.0 * m_sqrtLambda);
+  m_uiLambdaMotionSSE[0] = (UInt)floor(65536.0 * m_dLambda   );
+#if FULL_NBIT
+  dLambda = 0.57 * pow(2.0, ((LOSSLESS_AND_MIXED_LOSSLESS_RD_COST_TEST_QP_PRIME - 12) / 3.0));
+#else
+  dLambda = 0.57 * pow(2.0, ((LOSSLESS_AND_MIXED_LOSSLESS_RD_COST_TEST_QP_PRIME - 12 - 6 * (bitDepths.recon[CHANNEL_TYPE_LUMA] - 8)) / 3.0));
+#endif
+  m_uiLambdaMotionSAD[1] = (UInt)floor(65536.0 * sqrt(dLambda));
+  m_uiLambdaMotionSSE[1] = (UInt)floor(65536.0 * dLambda   );
+#endif
 }
 
@@ -192,48 +241,47 @@
 Void TComRdCost::init()
 {
-  m_afpDistortFunc[0]  = NULL;                  // for DF_DEFAULT
-  
-  m_afpDistortFunc[1]  = TComRdCost::xGetSSE;
-  m_afpDistortFunc[2]  = TComRdCost::xGetSSE4;
-  m_afpDistortFunc[3]  = TComRdCost::xGetSSE8;
-  m_afpDistortFunc[4]  = TComRdCost::xGetSSE16;
-  m_afpDistortFunc[5]  = TComRdCost::xGetSSE32;
-  m_afpDistortFunc[6]  = TComRdCost::xGetSSE64;
-  m_afpDistortFunc[7]  = TComRdCost::xGetSSE16N;
-  
-  m_afpDistortFunc[8]  = TComRdCost::xGetSAD;
-  m_afpDistortFunc[9]  = TComRdCost::xGetSAD4;
-  m_afpDistortFunc[10] = TComRdCost::xGetSAD8;
-  m_afpDistortFunc[11] = TComRdCost::xGetSAD16;
-  m_afpDistortFunc[12] = TComRdCost::xGetSAD32;
-  m_afpDistortFunc[13] = TComRdCost::xGetSAD64;
-  m_afpDistortFunc[14] = TComRdCost::xGetSAD16N;
-  
-  m_afpDistortFunc[15] = TComRdCost::xGetSAD;
-  m_afpDistortFunc[16] = TComRdCost::xGetSAD4;
-  m_afpDistortFunc[17] = TComRdCost::xGetSAD8;
-  m_afpDistortFunc[18] = TComRdCost::xGetSAD16;
-  m_afpDistortFunc[19] = TComRdCost::xGetSAD32;
-  m_afpDistortFunc[20] = TComRdCost::xGetSAD64;
-  m_afpDistortFunc[21] = TComRdCost::xGetSAD16N;
-  
-#if AMP_SAD
-  m_afpDistortFunc[43] = TComRdCost::xGetSAD12;
-  m_afpDistortFunc[44] = TComRdCost::xGetSAD24;
-  m_afpDistortFunc[45] = TComRdCost::xGetSAD48;
-
-  m_afpDistortFunc[46] = TComRdCost::xGetSAD12;
-  m_afpDistortFunc[47] = TComRdCost::xGetSAD24;
-  m_afpDistortFunc[48] = TComRdCost::xGetSAD48;
-#endif
-  m_afpDistortFunc[22] = TComRdCost::xGetHADs;
-  m_afpDistortFunc[23] = TComRdCost::xGetHADs;
-  m_afpDistortFunc[24] = TComRdCost::xGetHADs;
-  m_afpDistortFunc[25] = TComRdCost::xGetHADs;
-  m_afpDistortFunc[26] = TComRdCost::xGetHADs;
-  m_afpDistortFunc[27] = TComRdCost::xGetHADs;
-  m_afpDistortFunc[28] = TComRdCost::xGetHADs;
-
-#if H_3D_VSO
+  m_afpDistortFunc[DF_DEFAULT] = NULL;                  // for DF_DEFAULT
+
+  m_afpDistortFunc[DF_SSE    ] = TComRdCost::xGetSSE;
+  m_afpDistortFunc[DF_SSE4   ] = TComRdCost::xGetSSE4;
+  m_afpDistortFunc[DF_SSE8   ] = TComRdCost::xGetSSE8;
+  m_afpDistortFunc[DF_SSE16  ] = TComRdCost::xGetSSE16;
+  m_afpDistortFunc[DF_SSE32  ] = TComRdCost::xGetSSE32;
+  m_afpDistortFunc[DF_SSE64  ] = TComRdCost::xGetSSE64;
+  m_afpDistortFunc[DF_SSE16N ] = TComRdCost::xGetSSE16N;
+
+  m_afpDistortFunc[DF_SAD    ] = TComRdCost::xGetSAD;
+  m_afpDistortFunc[DF_SAD4   ] = TComRdCost::xGetSAD4;
+  m_afpDistortFunc[DF_SAD8   ] = TComRdCost::xGetSAD8;
+  m_afpDistortFunc[DF_SAD16  ] = TComRdCost::xGetSAD16;
+  m_afpDistortFunc[DF_SAD32  ] = TComRdCost::xGetSAD32;
+  m_afpDistortFunc[DF_SAD64  ] = TComRdCost::xGetSAD64;
+  m_afpDistortFunc[DF_SAD16N ] = TComRdCost::xGetSAD16N;
+
+  m_afpDistortFunc[DF_SADS   ] = TComRdCost::xGetSAD;
+  m_afpDistortFunc[DF_SADS4  ] = TComRdCost::xGetSAD4;
+  m_afpDistortFunc[DF_SADS8  ] = TComRdCost::xGetSAD8;
+  m_afpDistortFunc[DF_SADS16 ] = TComRdCost::xGetSAD16;
+  m_afpDistortFunc[DF_SADS32 ] = TComRdCost::xGetSAD32;
+  m_afpDistortFunc[DF_SADS64 ] = TComRdCost::xGetSAD64;
+  m_afpDistortFunc[DF_SADS16N] = TComRdCost::xGetSAD16N;
+
+  m_afpDistortFunc[DF_SAD12  ] = TComRdCost::xGetSAD12;
+  m_afpDistortFunc[DF_SAD24  ] = TComRdCost::xGetSAD24;
+  m_afpDistortFunc[DF_SAD48  ] = TComRdCost::xGetSAD48;
+
+  m_afpDistortFunc[DF_SADS12 ] = TComRdCost::xGetSAD12;
+  m_afpDistortFunc[DF_SADS24 ] = TComRdCost::xGetSAD24;
+  m_afpDistortFunc[DF_SADS48 ] = TComRdCost::xGetSAD48;
+
+  m_afpDistortFunc[DF_HADS   ] = TComRdCost::xGetHADs;
+  m_afpDistortFunc[DF_HADS4  ] = TComRdCost::xGetHADs;
+  m_afpDistortFunc[DF_HADS8  ] = TComRdCost::xGetHADs;
+  m_afpDistortFunc[DF_HADS16 ] = TComRdCost::xGetHADs;
+  m_afpDistortFunc[DF_HADS32 ] = TComRdCost::xGetHADs;
+  m_afpDistortFunc[DF_HADS64 ] = TComRdCost::xGetHADs;
+  m_afpDistortFunc[DF_HADS16N] = TComRdCost::xGetHADs;
+
+#if NH_3D_VSO
   // SAIT_VSO_EST_A0033
   m_afpDistortFunc[29]  = TComRdCost::xGetVSD;
@@ -245,16 +293,15 @@
   m_afpDistortFunc[35]  = TComRdCost::xGetVSD16N;
 #endif
-#if !FIX203
-  m_puiComponentCostOriginP = NULL;
-  m_puiComponentCost        = NULL;
-  m_puiVerCost              = NULL;
-  m_puiHorCost              = NULL;
-#endif
-  m_uiCost                  = 0;
-  m_iCostScale              = 0;
-#if !FIX203
-  m_iSearchLimit            = 0xdeaddead;
-#endif
-#if H_3D_VSO
+
+  m_costMode                   = COST_STANDARD_LOSSY;
+
+#if RExt__HIGH_BIT_DEPTH_SUPPORT
+  m_dCost                      = 0;
+#else
+  m_uiCost                     = 0;
+#endif
+  m_iCostScale                 = 0;
+
+#if NH_3D_VSO
   m_bUseVSO                 = false;
   m_uiVSOMode               = 0; 
@@ -265,51 +312,17 @@
   m_bUseEstimatedVSD        = false; 
 #endif
-#if H_3D_DBBP
+#if NH_3D_DBBP
   m_bUseMask                = false;
 #endif
-}
-
-#if !FIX203
-Void TComRdCost::initRateDistortionModel( Int iSubPelSearchLimit )
-{
-  // make it larger
-  iSubPelSearchLimit += 4;
-  iSubPelSearchLimit *= 8;
-  
-  if( m_iSearchLimit != iSubPelSearchLimit )
-  {
-    xUninit();
-    
-    m_iSearchLimit = iSubPelSearchLimit;
-    
-    m_puiComponentCostOriginP = new UInt[ 4 * iSubPelSearchLimit ];
-    iSubPelSearchLimit *= 2;
-    
-    m_puiComponentCost = m_puiComponentCostOriginP + iSubPelSearchLimit;
-    
-    for( Int n = -iSubPelSearchLimit; n < iSubPelSearchLimit; n++)
-    {
-      m_puiComponentCost[n] = xGetComponentBits( n );
-    }
-  }
-}
-
-Void TComRdCost::xUninit()
-{
-  if( NULL != m_puiComponentCostOriginP )
-  {
-    delete [] m_puiComponentCostOriginP;
-    m_puiComponentCostOriginP = NULL;
-  }
-}
-#endif
-
-UInt TComRdCost::xGetComponentBits( Int iVal )
-{
+
+}
+
+// Static member function
+UInt TComRdCost::xGetExpGolombNumberOfBits( Int iVal )
+{
+  assert(iVal != std::numeric_limits<Int>::min());
   UInt uiLength = 1;
-  UInt uiTemp   = ( iVal <= 0) ? (-iVal<<1)+1: (iVal<<1);
-  
-  assert ( uiTemp );
-  
+  UInt uiTemp   = ( iVal <= 0) ? (UInt(-iVal)<<1)+1: UInt(iVal<<1);
+
   while ( 1 != uiTemp )
   {
@@ -317,5 +330,5 @@
     uiLength += 2;
   }
-  
+
   return uiLength;
 }
@@ -327,6 +340,6 @@
   rcDistParam.iRows    = uiBlkHeight;
   rcDistParam.DistFunc = m_afpDistortFunc[eDFunc + g_aucConvertToBit[ rcDistParam.iCols ] + 1 ];
-  
-#if H_3D_DBBP
+
+#if NH_3D_DBBP
   if( m_bUseMask )
   {
@@ -363,29 +376,27 @@
   rcDistParam.pOrg = pcPatternKey->getROIY();
   rcDistParam.pCur = piRefY;
-  
+
   rcDistParam.iStrideOrg = pcPatternKey->getPatternLStride();
   rcDistParam.iStrideCur = iRefStride;
-  
+
   // set Block Width / Height
   rcDistParam.iCols    = pcPatternKey->getROIYWidth();
   rcDistParam.iRows    = pcPatternKey->getROIYHeight();
   rcDistParam.DistFunc = m_afpDistortFunc[DF_SAD + g_aucConvertToBit[ rcDistParam.iCols ] + 1 ];
-  
-#if AMP_SAD
+
   if (rcDistParam.iCols == 12)
   {
-    rcDistParam.DistFunc = m_afpDistortFunc[43 ];
+    rcDistParam.DistFunc = m_afpDistortFunc[DF_SAD12];
   }
   else if (rcDistParam.iCols == 24)
   {
-    rcDistParam.DistFunc = m_afpDistortFunc[44 ];
+    rcDistParam.DistFunc = m_afpDistortFunc[DF_SAD24];
   }
   else if (rcDistParam.iCols == 48)
   {
-    rcDistParam.DistFunc = m_afpDistortFunc[45 ];
-  }
-#endif
-  
-#if H_3D_DBBP
+    rcDistParam.DistFunc = m_afpDistortFunc[DF_SAD48];
+  }
+
+#if NH_3D_DBBP
   if( m_bUseMask )
   {
@@ -403,33 +414,31 @@
   rcDistParam.pOrg = pcPatternKey->getROIY();
   rcDistParam.pCur = piRefY;
-  
+
   rcDistParam.iStrideOrg = pcPatternKey->getPatternLStride();
   rcDistParam.iStrideCur = iRefStride * iStep;
-  
+
   // set Step for interpolated buffer
   rcDistParam.iStep = iStep;
-  
+
   // set Block Width / Height
   rcDistParam.iCols    = pcPatternKey->getROIYWidth();
   rcDistParam.iRows    = pcPatternKey->getROIYHeight();
-  
+
   // set distortion function
   if ( !bHADME )
   {
     rcDistParam.DistFunc = m_afpDistortFunc[DF_SADS + g_aucConvertToBit[ rcDistParam.iCols ] + 1 ];
-#if AMP_SAD
     if (rcDistParam.iCols == 12)
     {
-      rcDistParam.DistFunc = m_afpDistortFunc[46 ];
+      rcDistParam.DistFunc = m_afpDistortFunc[DF_SADS12];
     }
     else if (rcDistParam.iCols == 24)
     {
-      rcDistParam.DistFunc = m_afpDistortFunc[47 ];
+      rcDistParam.DistFunc = m_afpDistortFunc[DF_SADS24];
     }
     else if (rcDistParam.iCols == 48)
     {
-      rcDistParam.DistFunc = m_afpDistortFunc[48 ];
-    }
-#endif
+      rcDistParam.DistFunc = m_afpDistortFunc[DF_SADS48];
+    }
   }
   else
@@ -437,6 +446,6 @@
     rcDistParam.DistFunc = m_afpDistortFunc[DF_HADS + g_aucConvertToBit[ rcDistParam.iCols ] + 1 ];
   }
-  
-#if H_3D_DBBP
+
+#if NH_3D_DBBP
   if( m_bUseMask )
   {
@@ -448,6 +457,5 @@
 }
 
-Void
-TComRdCost::setDistParam( DistParam& rcDP, Int bitDepth, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard )
+Void TComRdCost::setDistParam( DistParam& rcDP, Int bitDepth, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard )
 {
   rcDP.pOrg       = p1;
@@ -461,5 +469,5 @@
   rcDP.bitDepth   = bitDepth;
   rcDP.DistFunc   = m_afpDistortFunc[ ( bHadamard ? DF_HADS : DF_SADS ) + g_aucConvertToBit[ iWidth ] + 1 ];
-#if H_3D_DBBP
+#if NH_3D_DBBP
   if( m_bUseMask )
   {
@@ -469,9 +477,9 @@
 }
 
-UInt TComRdCost::calcHAD(Int bitDepth, Pel* pi0, Int iStride0, Pel* pi1, Int iStride1, Int iWidth, Int iHeight )
-{
-  UInt uiSum = 0;
+Distortion TComRdCost::calcHAD( Int bitDepth, Pel* pi0, Int iStride0, Pel* pi1, Int iStride1, Int iWidth, Int iHeight )
+{
+  Distortion uiSum = 0;
   Int x, y;
-  
+
   if ( ( (iWidth % 8) == 0 ) && ( (iHeight % 8) == 0 ) )
   {
@@ -488,6 +496,6 @@
   else
   {
-    assert(iWidth % 4 == 0 && iHeight % 4 == 0);
-    
+    assert ( ( (iWidth % 4) == 0 ) && ( (iHeight % 4) == 0 ) );
+
     for ( y=0; y<iHeight; y+= 4 )
     {
@@ -500,12 +508,10 @@
     }
   }
-  
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(bitDepth-8);
-
-}
-
-#if H_3D_FAST_DEPTH_INTRA
-
-UInt TComRdCost::calcVAR (Pel* pi0, Int stride, Int width, Int height, Int cuDepth)
+
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(bitDepth-8) );
+}
+
+#if NH_3D_ENC_DEPTH
+UInt TComRdCost::calcVAR (Pel* pi0, Int stride, Int width, Int height, Int cuDepth, UInt maxCuWidth)
 { 
   Int temp = 0;
@@ -519,5 +525,5 @@
   }
 
-  Int cuMaxLog2Size = g_aucConvertToBit[g_uiMaxCUWidth]+2;
+  Int cuMaxLog2Size = g_aucConvertToBit[maxCuWidth]+2;
   
   if ( width == 4 ) 
@@ -542,5 +548,5 @@
 
 
-UInt TComRdCost::getDistPart(Int bitDepth, Pel* piCur, Int iCurStride,  Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, TextType eText, DFunc eDFunc)
+Distortion TComRdCost::getDistPart( Int bitDepth, Pel* piCur, Int iCurStride,  Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, const ComponentID compID, DFunc eDFunc )
 {
   DistParam cDtParam;
@@ -553,20 +559,17 @@
 
   cDtParam.bApplyWeight = false;
-  cDtParam.uiComp       = 255;    // just for assert: to be sure it was set before use, since only values 0,1 or 2 are allowed.
-  cDtParam.bitDepth = bitDepth;
-
-#if H_3D_IC
+  cDtParam.compIdx      = MAX_NUM_COMPONENT; // just for assert: to be sure it was set before use
+  cDtParam.bitDepth     = bitDepth;
+
+#if NH_3D_IC
   cDtParam.bUseIC       = false;
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   cDtParam.bUseSDCMRSAD = false;
 #endif
-  if (eText == TEXT_CHROMA_U)
-  {
-   return ((Int) (m_cbDistortionWeight * cDtParam.DistFunc( &cDtParam )));
-  }
-  else if (eText == TEXT_CHROMA_V)
-  {
-   return ((Int) (m_crDistortionWeight * cDtParam.DistFunc( &cDtParam )));
+
+  if (isChroma(compID))
+  {
+    return ((Distortion) (m_distortionWeight[compID] * cDtParam.DistFunc( &cDtParam )));
   }
   else
@@ -575,13 +578,13 @@
   }
 }
-#if H_3D_VSO
+#if NH_3D_VSO
 // SAIT_VSO_EST_A0033
-UInt TComRdCost::getDistPartVSD( TComDataCU* pcCU, UInt uiPartOffset, Pel* piCur, Int iCurStride,  Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight,  Bool bHAD, DFunc eDFunc )
+UInt TComRdCost::getDistPartVSD( TComDataCU* pcCU, UInt uiPartOffset, Int bitDepth, Pel* piCur, Int iCurStride,  Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight,  Bool bHAD, DFunc eDFunc )
 {
   AOT( ( m_dDisparityCoeff <= 0 ) || ( m_dDisparityCoeff > 10 ) );
 
-  Pel* piVirRec  = m_pcVideoRecPicYuv->getLumaAddr(pcCU->getAddr(),pcCU->getZorderIdxInCU()+uiPartOffset); 
-  Pel* piVirOrg  = m_pcDepthPicYuv   ->getLumaAddr(pcCU->getAddr(),pcCU->getZorderIdxInCU()+uiPartOffset); 
-  Int iVirStride = m_pcVideoRecPicYuv->getStride();   
+  Pel* piVirRec  = m_pcVideoRecPicYuv->getAddr( COMPONENT_Y, pcCU->getCtuRsAddr( ), pcCU->getZorderIdxInCtu()+uiPartOffset ); 
+  Pel* piVirOrg  = m_pcDepthPicYuv   ->getAddr( COMPONENT_Y, pcCU->getCtuRsAddr( ), pcCU->getZorderIdxInCtu()+uiPartOffset ); 
+  Int iVirStride = m_pcVideoRecPicYuv->getStride( COMPONENT_Y );   
 
   DistParam cDtParam;
@@ -596,8 +599,14 @@
   cDtParam.iStep      = 1;
 
-  cDtParam.bApplyWeight = false;
-  cDtParam.uiComp       = 255;    // just for assert: to be sure it was set before use, since only values 0,1 or 2 are allowed.
-
-  cDtParam.bitDepth   = g_bitDepthY;
+  cDtParam.bApplyWeight = false;  
+
+  cDtParam.bitDepth   = bitDepth;
+
+#if NH_3D_IC_FIX
+  cDtParam.bUseIC       = false;
+#endif
+#if NH_3D_SDC_INTER
+  cDtParam.bUseSDCMRSAD = false;
+#endif
 
   Dist dist = cDtParam.DistFunc( &cDtParam );
@@ -611,9 +620,9 @@
     if ( !bHAD )
     {
-      distDepth = (Dist) getDistPart( g_bitDepthY, piCur, iCurStride, piOrg, iOrgStride, uiBlkWidth, uiBlkHeight);
+      distDepth = (Dist) getDistPart( bitDepth, piCur, iCurStride, piOrg, iOrgStride, uiBlkWidth, uiBlkHeight, COMPONENT_Y);
     }
     else
     {
-      distDepth = (Dist) calcHAD( g_bitDepthY, piCur, iCurStride, piOrg, iOrgStride, uiBlkWidth, uiBlkHeight);
+      distDepth = (Dist) calcHAD( bitDepth, piCur, iCurStride, piOrg, iOrgStride, uiBlkWidth, uiBlkHeight);
     }
 
@@ -647,5 +656,5 @@
 // ====================================================================================================================
 
-#if H_3D_DBBP
+#if NH_3D_DBBP
 // --------------------------------------------------------------------------------------------------------------------
 // Masked distortion functions
@@ -688,5 +697,5 @@
   
   AOF(!pcDtParam->bApplyWeight);
-#if H_3D_IC
+#if NH_3D_IC
   AOF(!pcDtParam->bUseIC);
 #endif
@@ -756,11 +765,11 @@
 // --------------------------------------------------------------------------------------------------------------------
 
-UInt TComRdCost::xGetSAD( DistParam* pcDtParam )
+Distortion TComRdCost::xGetSAD( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -768,5 +777,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -774,13 +783,14 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iCols   = pcDtParam->iCols;
   Int  iStrideCur = pcDtParam->iStrideCur;
   Int  iStrideOrg = pcDtParam->iStrideOrg;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-- )
   {
@@ -792,15 +802,16 @@
     piCur += iStrideCur;
   }
-  
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-UInt TComRdCost::xGetSAD4( DistParam* pcDtParam )
-{
-  if ( pcDtParam->bApplyWeight ) 
-  {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD4( DistParam* pcDtParam )
+{
+  if ( pcDtParam->bApplyWeight )
+  {
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -808,5 +819,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -814,6 +825,7 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iSubShift  = pcDtParam->iSubShift;
@@ -821,7 +833,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -830,20 +842,21 @@
     uiSum += abs( piOrg[2] - piCur[2] );
     uiSum += abs( piOrg[3] - piCur[3] );
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-UInt TComRdCost::xGetSAD8( DistParam* pcDtParam )
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD8( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -851,5 +864,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -857,6 +870,7 @@
   }
 #endif
-  Pel* piOrg      = pcDtParam->pOrg;
-  Pel* piCur      = pcDtParam->pCur;
+
+  const Pel* piOrg      = pcDtParam->pOrg;
+  const Pel* piCur      = pcDtParam->pCur;
   Int  iRows      = pcDtParam->iRows;
   Int  iSubShift  = pcDtParam->iSubShift;
@@ -864,7 +878,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -877,20 +891,21 @@
     uiSum += abs( piOrg[6] - piCur[6] );
     uiSum += abs( piOrg[7] - piCur[7] );
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-UInt TComRdCost::xGetSAD16( DistParam* pcDtParam )
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD16( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -898,5 +913,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -904,6 +919,7 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iSubShift  = pcDtParam->iSubShift;
@@ -911,7 +927,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -932,21 +948,20 @@
     uiSum += abs( piOrg[14] - piCur[14] );
     uiSum += abs( piOrg[15] - piCur[15] );
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-#if AMP_SAD
-UInt TComRdCost::xGetSAD12( DistParam* pcDtParam )
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD12( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -954,5 +969,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -960,6 +975,7 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iSubShift  = pcDtParam->iSubShift;
@@ -967,7 +983,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -984,17 +1000,16 @@
     uiSum += abs( piOrg[10] - piCur[10] );
     uiSum += abs( piOrg[11] - piCur[11] );
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-#endif
-
-UInt TComRdCost::xGetSAD16N( DistParam* pcDtParam )
-{
-#if H_3D_IC
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD16N( DistParam* pcDtParam )
+{
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -1002,5 +1017,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -1008,6 +1023,7 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iCols   = pcDtParam->iCols;
@@ -1016,7 +1032,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -1043,16 +1059,17 @@
     piCur += iStrideCur;
   }
-  
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-UInt TComRdCost::xGetSAD32( DistParam* pcDtParam )
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD32( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -1060,5 +1077,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -1066,6 +1083,7 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iSubShift  = pcDtParam->iSubShift;
@@ -1073,7 +1091,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -1110,21 +1128,21 @@
     uiSum += abs( piOrg[30] - piCur[30] );
     uiSum += abs( piOrg[31] - piCur[31] );
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-#if AMP_SAD
-UInt TComRdCost::xGetSAD24( DistParam* pcDtParam )
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD24( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -1132,5 +1150,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -1138,6 +1156,7 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iSubShift  = pcDtParam->iSubShift;
@@ -1145,7 +1164,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -1174,22 +1193,21 @@
     uiSum += abs( piOrg[22] - piCur[22] );
     uiSum += abs( piOrg[23] - piCur[23] );
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-#endif
-
-UInt TComRdCost::xGetSAD64( DistParam* pcDtParam )
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD64( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -1197,5 +1215,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -1203,6 +1221,7 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iSubShift  = pcDtParam->iSubShift;
@@ -1210,7 +1229,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -1279,21 +1298,20 @@
     uiSum += abs( piOrg[62] - piCur[62] );
     uiSum += abs( piOrg[63] - piCur[63] );
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-#if AMP_SAD
-UInt TComRdCost::xGetSAD48( DistParam* pcDtParam )
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+Distortion TComRdCost::xGetSAD48( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
+  }
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -1301,5 +1319,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -1307,6 +1325,7 @@
   }
 #endif
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iSubShift  = pcDtParam->iSubShift;
@@ -1314,7 +1333,7 @@
   Int  iStrideCur = pcDtParam->iStrideCur*iSubStep;
   Int  iStrideOrg = pcDtParam->iStrideOrg*iSubStep;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   for( ; iRows != 0; iRows-=iSubStep )
   {
@@ -1367,20 +1386,20 @@
     uiSum += abs( piOrg[46] - piCur[46] );
     uiSum += abs( piOrg[47] - piCur[47] );
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   uiSum <<= iSubShift;
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-#endif
-
-#if H_3D_IC || H_3D_INTER_SDC
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+
+#if NH_3D_IC || NH_3D_SDC_INTER
 UInt TComRdCost::xGetSADic( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
   Pel* piOrg   = pcDtParam->pOrg;
@@ -1430,5 +1449,5 @@
   if ( pcDtParam->bApplyWeight ) 
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
   Pel* piOrg   = pcDtParam->pOrg;
@@ -1487,5 +1506,5 @@
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
   Pel* piOrg      = pcDtParam->pOrg;
@@ -1556,5 +1575,5 @@
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
   Pel* piOrg   = pcDtParam->pOrg;
@@ -1645,10 +1664,9 @@
 }
 
-#if AMP_SAD
 UInt TComRdCost::xGetSAD12ic( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
   Pel* piOrg   = pcDtParam->pOrg;
@@ -1726,5 +1744,5 @@
   return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT( pcDtParam->bitDepth - 8 ) );
 }
-#endif
+
 
 UInt TComRdCost::xGetSAD16Nic( DistParam* pcDtParam )
@@ -1825,5 +1843,5 @@
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
   Pel* piOrg   = pcDtParam->pOrg;
@@ -1962,10 +1980,10 @@
 }
 
-#if AMP_SAD
+
 UInt TComRdCost::xGetSAD24ic( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
   Pel* piOrg   = pcDtParam->pOrg;
@@ -2079,5 +2097,4 @@
   return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT( pcDtParam->bitDepth - 8 ) );
 }
-#endif
 
 UInt TComRdCost::xGetSAD64ic( DistParam* pcDtParam )
@@ -2085,5 +2102,5 @@
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
   Pel* piOrg   = pcDtParam->pOrg;
@@ -2318,10 +2335,10 @@
 }
 
-#if AMP_SAD
+
 UInt TComRdCost::xGetSAD48ic( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSADw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
 
@@ -2509,28 +2526,26 @@
 }
 #endif
-
-#endif
 // --------------------------------------------------------------------------------------------------------------------
 // SSE
 // --------------------------------------------------------------------------------------------------------------------
 
-UInt TComRdCost::xGetSSE( DistParam* pcDtParam )
+Distortion TComRdCost::xGetSSE( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSSEw( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+    return TComRdCostWeightPrediction::xGetSSEw( pcDtParam );
+  }
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const 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;
-  
+
+  Distortion uiSum   = 0;
+  UInt       uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
+
+  Intermediate_Int iTemp;
+
   for( ; iRows != 0; iRows-- )
   {
@@ -2538,331 +2553,337 @@
     {
       iTemp = piOrg[n  ] - piCur[n  ];
-      uiSum += ( iTemp * iTemp ) >> uiShift;
-    }
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+      uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    }
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   return ( uiSum );
 }
 
-UInt TComRdCost::xGetSSE4( DistParam* pcDtParam )
+Distortion TComRdCost::xGetSSE4( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
     assert( pcDtParam->iCols == 4 );
-    return xGetSSEw( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+    return TComRdCostWeightPrediction::xGetSSEw( pcDtParam );
+  }
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iStrideOrg = pcDtParam->iStrideOrg;
   Int  iStrideCur = pcDtParam->iStrideCur;
-  
-  UInt uiSum = 0;
-  UInt uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
-  
-  Int  iTemp;
-  
+
+  Distortion uiSum   = 0;
+  UInt       uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
+
+  Intermediate_Int  iTemp;
+
   for( ; iRows != 0; iRows-- )
   {
-    
-    iTemp = piOrg[0] - piCur[0]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[1] - piCur[1]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[2] - piCur[2]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[3] - piCur[3]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    iTemp = piOrg[0] - piCur[0]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[1] - piCur[1]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[2] - piCur[2]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[3] - piCur[3]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   return ( uiSum );
 }
 
-UInt TComRdCost::xGetSSE8( DistParam* pcDtParam )
+Distortion TComRdCost::xGetSSE8( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
     assert( pcDtParam->iCols == 8 );
-    return xGetSSEw( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+    return TComRdCostWeightPrediction::xGetSSEw( pcDtParam );
+  }
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iStrideOrg = pcDtParam->iStrideOrg;
   Int  iStrideCur = pcDtParam->iStrideCur;
-  
-  UInt uiSum = 0;
-  UInt uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
-  
-  Int  iTemp;
-  
+
+  Distortion uiSum   = 0;
+  UInt       uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
+
+  Intermediate_Int  iTemp;
+
   for( ; iRows != 0; iRows-- )
   {
-    iTemp = piOrg[0] - piCur[0]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[1] - piCur[1]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[2] - piCur[2]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[3] - piCur[3]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[4] - piCur[4]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[5] - piCur[5]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[6] - piCur[6]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[7] - piCur[7]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+    iTemp = piOrg[0] - piCur[0]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[1] - piCur[1]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[2] - piCur[2]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[3] - piCur[3]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[4] - piCur[4]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[5] - piCur[5]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[6] - piCur[6]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[7] - piCur[7]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   return ( uiSum );
 }
 
-UInt TComRdCost::xGetSSE16( DistParam* pcDtParam )
+Distortion TComRdCost::xGetSSE16( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
     assert( pcDtParam->iCols == 16 );
-    return xGetSSEw( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+    return TComRdCostWeightPrediction::xGetSSEw( pcDtParam );
+  }
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iStrideOrg = pcDtParam->iStrideOrg;
   Int  iStrideCur = pcDtParam->iStrideCur;
-  
-  UInt uiSum = 0;
-  UInt uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
-  
-  Int  iTemp;
-  
+
+  Distortion uiSum   = 0;
+  UInt       uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
+
+  Intermediate_Int  iTemp;
+
   for( ; iRows != 0; iRows-- )
   {
-    
-    iTemp = piOrg[ 0] - piCur[ 0]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 1] - piCur[ 1]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 2] - piCur[ 2]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 3] - piCur[ 3]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 4] - piCur[ 4]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 5] - piCur[ 5]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 6] - piCur[ 6]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 7] - piCur[ 7]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 8] - piCur[ 8]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 9] - piCur[ 9]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[10] - piCur[10]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[11] - piCur[11]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[12] - piCur[12]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[13] - piCur[13]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[14] - piCur[14]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[15] - piCur[15]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    iTemp = piOrg[ 0] - piCur[ 0]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 1] - piCur[ 1]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 2] - piCur[ 2]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 3] - piCur[ 3]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 4] - piCur[ 4]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 5] - piCur[ 5]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 6] - piCur[ 6]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 7] - piCur[ 7]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 8] - piCur[ 8]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 9] - piCur[ 9]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[10] - piCur[10]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[11] - piCur[11]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[12] - piCur[12]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[13] - piCur[13]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[14] - piCur[14]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[15] - piCur[15]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   return ( uiSum );
 }
 
-UInt TComRdCost::xGetSSE16N( DistParam* pcDtParam )
+Distortion TComRdCost::xGetSSE16N( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetSSEw( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+    return TComRdCostWeightPrediction::xGetSSEw( pcDtParam );
+  }
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const 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;
-  
+
+  Distortion uiSum   = 0;
+  UInt       uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
+
+  Intermediate_Int  iTemp;
+
   for( ; iRows != 0; iRows-- )
   {
     for (Int n = 0; n < iCols; n+=16 )
     {
-      
-      iTemp = piOrg[n+ 0] - piCur[n+ 0]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 1] - piCur[n+ 1]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 2] - piCur[n+ 2]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 3] - piCur[n+ 3]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 4] - piCur[n+ 4]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 5] - piCur[n+ 5]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 6] - piCur[n+ 6]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 7] - piCur[n+ 7]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 8] - piCur[n+ 8]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+ 9] - piCur[n+ 9]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+10] - piCur[n+10]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+11] - piCur[n+11]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+12] - piCur[n+12]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+13] - piCur[n+13]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+14] - piCur[n+14]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      iTemp = piOrg[n+15] - piCur[n+15]; uiSum += ( iTemp * iTemp ) >> uiShift;
-      
-    }
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+      iTemp = piOrg[n+ 0] - piCur[n+ 0]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 1] - piCur[n+ 1]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 2] - piCur[n+ 2]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 3] - piCur[n+ 3]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 4] - piCur[n+ 4]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 5] - piCur[n+ 5]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 6] - piCur[n+ 6]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 7] - piCur[n+ 7]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 8] - piCur[n+ 8]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+ 9] - piCur[n+ 9]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+10] - piCur[n+10]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+11] - piCur[n+11]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+12] - piCur[n+12]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+13] - piCur[n+13]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+14] - piCur[n+14]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+      iTemp = piOrg[n+15] - piCur[n+15]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+
+    }
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   return ( uiSum );
 }
 
-UInt TComRdCost::xGetSSE32( DistParam* pcDtParam )
+Distortion TComRdCost::xGetSSE32( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
     assert( pcDtParam->iCols == 32 );
-    return xGetSSEw( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+    return TComRdCostWeightPrediction::xGetSSEw( pcDtParam );
+  }
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iStrideOrg = pcDtParam->iStrideOrg;
   Int  iStrideCur = pcDtParam->iStrideCur;
-  
-  UInt uiSum = 0;
-  UInt uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
-  Int  iTemp;
-  
+
+  Distortion uiSum   = 0;
+  UInt       uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
+
+  Intermediate_Int  iTemp;
+
   for( ; iRows != 0; iRows-- )
   {
-    
-    iTemp = piOrg[ 0] - piCur[ 0]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 1] - piCur[ 1]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 2] - piCur[ 2]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 3] - piCur[ 3]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 4] - piCur[ 4]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 5] - piCur[ 5]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 6] - piCur[ 6]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 7] - piCur[ 7]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 8] - piCur[ 8]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 9] - piCur[ 9]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[10] - piCur[10]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[11] - piCur[11]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[12] - piCur[12]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[13] - piCur[13]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[14] - piCur[14]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[15] - piCur[15]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[16] - piCur[16]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[17] - piCur[17]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[18] - piCur[18]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[19] - piCur[19]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[20] - piCur[20]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[21] - piCur[21]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[22] - piCur[22]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[23] - piCur[23]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[24] - piCur[24]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[25] - piCur[25]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[26] - piCur[26]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[27] - piCur[27]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[28] - piCur[28]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[29] - piCur[29]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[30] - piCur[30]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[31] - piCur[31]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+
+    iTemp = piOrg[ 0] - piCur[ 0]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 1] - piCur[ 1]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 2] - piCur[ 2]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 3] - piCur[ 3]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 4] - piCur[ 4]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 5] - piCur[ 5]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 6] - piCur[ 6]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 7] - piCur[ 7]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 8] - piCur[ 8]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 9] - piCur[ 9]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[10] - piCur[10]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[11] - piCur[11]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[12] - piCur[12]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[13] - piCur[13]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[14] - piCur[14]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[15] - piCur[15]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[16] - piCur[16]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[17] - piCur[17]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[18] - piCur[18]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[19] - piCur[19]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[20] - piCur[20]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[21] - piCur[21]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[22] - piCur[22]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[23] - piCur[23]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[24] - piCur[24]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[25] - piCur[25]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[26] - piCur[26]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[27] - piCur[27]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[28] - piCur[28]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[29] - piCur[29]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[30] - piCur[30]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[31] - piCur[31]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   return ( uiSum );
 }
 
-UInt TComRdCost::xGetSSE64( DistParam* pcDtParam )
+Distortion TComRdCost::xGetSSE64( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
     assert( pcDtParam->iCols == 64 );
-    return xGetSSEw( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
+    return TComRdCostWeightPrediction::xGetSSEw( pcDtParam );
+  }
+  const Pel* piOrg   = pcDtParam->pOrg;
+  const Pel* piCur   = pcDtParam->pCur;
   Int  iRows   = pcDtParam->iRows;
   Int  iStrideOrg = pcDtParam->iStrideOrg;
   Int  iStrideCur = pcDtParam->iStrideCur;
-  
-  UInt uiSum = 0;
-  UInt uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
-  Int  iTemp;
-  
+
+  Distortion uiSum   = 0;
+  UInt       uiShift = DISTORTION_PRECISION_ADJUSTMENT((pcDtParam->bitDepth-8) << 1);
+
+  Intermediate_Int  iTemp;
+
   for( ; iRows != 0; iRows-- )
   {
-    iTemp = piOrg[ 0] - piCur[ 0]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 1] - piCur[ 1]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 2] - piCur[ 2]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 3] - piCur[ 3]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 4] - piCur[ 4]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 5] - piCur[ 5]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 6] - piCur[ 6]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 7] - piCur[ 7]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 8] - piCur[ 8]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[ 9] - piCur[ 9]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[10] - piCur[10]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[11] - piCur[11]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[12] - piCur[12]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[13] - piCur[13]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[14] - piCur[14]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[15] - piCur[15]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[16] - piCur[16]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[17] - piCur[17]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[18] - piCur[18]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[19] - piCur[19]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[20] - piCur[20]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[21] - piCur[21]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[22] - piCur[22]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[23] - piCur[23]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[24] - piCur[24]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[25] - piCur[25]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[26] - piCur[26]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[27] - piCur[27]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[28] - piCur[28]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[29] - piCur[29]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[30] - piCur[30]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[31] - piCur[31]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[32] - piCur[32]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[33] - piCur[33]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[34] - piCur[34]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[35] - piCur[35]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[36] - piCur[36]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[37] - piCur[37]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[38] - piCur[38]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[39] - piCur[39]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[40] - piCur[40]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[41] - piCur[41]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[42] - piCur[42]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[43] - piCur[43]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[44] - piCur[44]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[45] - piCur[45]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[46] - piCur[46]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[47] - piCur[47]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[48] - piCur[48]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[49] - piCur[49]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[50] - piCur[50]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[51] - piCur[51]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[52] - piCur[52]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[53] - piCur[53]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[54] - piCur[54]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[55] - piCur[55]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[56] - piCur[56]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[57] - piCur[57]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[58] - piCur[58]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[59] - piCur[59]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[60] - piCur[60]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[61] - piCur[61]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[62] - piCur[62]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    iTemp = piOrg[63] - piCur[63]; uiSum += ( iTemp * iTemp ) >> uiShift;
-    
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-  
+    iTemp = piOrg[ 0] - piCur[ 0]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 1] - piCur[ 1]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 2] - piCur[ 2]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 3] - piCur[ 3]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 4] - piCur[ 4]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 5] - piCur[ 5]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 6] - piCur[ 6]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 7] - piCur[ 7]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 8] - piCur[ 8]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[ 9] - piCur[ 9]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[10] - piCur[10]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[11] - piCur[11]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[12] - piCur[12]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[13] - piCur[13]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[14] - piCur[14]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[15] - piCur[15]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[16] - piCur[16]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[17] - piCur[17]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[18] - piCur[18]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[19] - piCur[19]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[20] - piCur[20]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[21] - piCur[21]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[22] - piCur[22]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[23] - piCur[23]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[24] - piCur[24]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[25] - piCur[25]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[26] - piCur[26]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[27] - piCur[27]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[28] - piCur[28]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[29] - piCur[29]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[30] - piCur[30]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[31] - piCur[31]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[32] - piCur[32]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[33] - piCur[33]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[34] - piCur[34]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[35] - piCur[35]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[36] - piCur[36]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[37] - piCur[37]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[38] - piCur[38]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[39] - piCur[39]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[40] - piCur[40]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[41] - piCur[41]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[42] - piCur[42]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[43] - piCur[43]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[44] - piCur[44]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[45] - piCur[45]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[46] - piCur[46]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[47] - piCur[47]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[48] - piCur[48]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[49] - piCur[49]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[50] - piCur[50]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[51] - piCur[51]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[52] - piCur[52]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[53] - piCur[53]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[54] - piCur[54]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[55] - piCur[55]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[56] - piCur[56]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[57] - piCur[57]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[58] - piCur[58]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[59] - piCur[59]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[60] - piCur[60]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[61] - piCur[61]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[62] - piCur[62]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+    iTemp = piOrg[63] - piCur[63]; uiSum += Distortion(( iTemp * iTemp ) >> uiShift);
+
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
   return ( uiSum );
 }
-#if H_3D_VSO
+
+#if NH_3D_VSO
 //SAIT_VSO_EST_A0033
 UInt TComRdCost::getVSDEstimate( Int dDM, Pel* pOrg, Int iOrgStride,  Pel* pVirRec, Pel* pVirOrg, Int iVirStride, Int x, Int y )
-{
-  Double  dD = ( (Double) ( dDM >> DISTORTION_PRECISION_ADJUSTMENT( g_bitDepthY - 8 ) ) ) * m_dDisparityCoeff;
-
-  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 );
+{ 
+  // change to use bit depth from DistParam struct
+  Double  dD = ( (Double) ( dDM >> ( ENC_INTERNAL_BIT_DEPTH - 8 ) ) ) * m_dDisparityCoeff;
+
+  Double dDepthWeight = ( pOrg[x] >=  ( (1<<(REN_BIT_DEPTH - 3)) + (1<<(REN_BIT_DEPTH - 2)) ) ? 4 : pOrg[x] > ((1<<REN_BIT_DEPTH) >> 4) ? (Float)(pOrg[x] - ((1<<REN_BIT_DEPTH) >> 4))/(Float)((1<<REN_BIT_DEPTH) >> 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 ] ) ) );
   Int iTemp = (Int) (((dTemp) < 0)? (Int)((dTemp) - 0.5) : (Int)((dTemp) + 0.5));
@@ -3092,7 +3113,8 @@
 // --------------------------------------------------------------------------------------------------------------------
 
-UInt TComRdCost::xCalcHADs2x2( Pel *piOrg, Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
-{
-  Int satd = 0, diff[4], m[4];
+Distortion TComRdCost::xCalcHADs2x2( Pel *piOrg, Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
+{
+  Distortion satd = 0;
+  TCoeff diff[4], m[4];
   assert( iStep == 1 );
   diff[0] = piOrg[0             ] - piCur[0];
@@ -3104,17 +3126,19 @@
   m[2] = diff[0] - diff[2];
   m[3] = diff[1] - diff[3];
-  
+
   satd += abs(m[0] + m[1]);
   satd += abs(m[0] - m[1]);
   satd += abs(m[2] + m[3]);
   satd += abs(m[2] - m[3]);
-  
+
   return satd;
 }
 
-UInt TComRdCost::xCalcHADs4x4( Pel *piOrg, Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
-{
-  Int k, satd = 0, diff[16], m[16], d[16];
-  
+Distortion TComRdCost::xCalcHADs4x4( Pel *piOrg, Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
+{
+  Int k;
+  Distortion satd = 0;
+  TCoeff diff[16], m[16], d[16];
+
   assert( iStep == 1 );
   for( k = 0; k < 16; k+=4 )
@@ -3124,9 +3148,9 @@
     diff[k+2] = piOrg[2] - piCur[2];
     diff[k+3] = piOrg[3] - piCur[3];
-    
-    piCur += iStrideCur;
-    piOrg += iStrideOrg;
-  }
-  
+
+    piCur += iStrideCur;
+    piOrg += iStrideOrg;
+  }
+
   /*===== hadamard transform =====*/
   m[ 0] = diff[ 0] + diff[12];
@@ -3146,5 +3170,5 @@
   m[14] = diff[ 2] - diff[14];
   m[15] = diff[ 3] - diff[15];
-  
+
   d[ 0] = m[ 0] + m[ 4];
   d[ 1] = m[ 1] + m[ 5];
@@ -3163,5 +3187,5 @@
   d[14] = m[14] - m[10];
   d[15] = m[15] - m[11];
-  
+
   m[ 0] = d[ 0] + d[ 3];
   m[ 1] = d[ 1] + d[ 2];
@@ -3180,5 +3204,5 @@
   m[14] = d[13] - d[14];
   m[15] = d[12] - d[15];
-  
+
   d[ 0] = m[ 0] + m[ 1];
   d[ 1] = m[ 0] - m[ 1];
@@ -3197,5 +3221,5 @@
   d[14] = m[14] + m[15];
   d[15] = m[15] - m[14];
-  
+
   for (k=0; k<16; ++k)
   {
@@ -3203,12 +3227,13 @@
   }
   satd = ((satd+1)>>1);
-  
+
   return satd;
 }
 
-UInt TComRdCost::xCalcHADs8x8( Pel *piOrg, Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
-{
-  Int k, i, j, jj, sad=0;
-  Int diff[64], m1[8][8], m2[8][8], m3[8][8];
+Distortion TComRdCost::xCalcHADs8x8( Pel *piOrg, Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
+{
+  Int k, i, j, jj;
+  Distortion sad = 0;
+  TCoeff diff[64], m1[8][8], m2[8][8], m3[8][8];
   assert( iStep == 1 );
   for( k = 0; k < 64; k += 8 )
@@ -3222,9 +3247,9 @@
     diff[k+6] = piOrg[6] - piCur[6];
     diff[k+7] = piOrg[7] - piCur[7];
-    
-    piCur += iStrideCur;
-    piOrg += iStrideOrg;
-  }
-  
+
+    piCur += iStrideCur;
+    piOrg += iStrideOrg;
+  }
+
   //horizontal
   for (j=0; j < 8; j++)
@@ -3239,5 +3264,5 @@
     m2[j][6] = diff[jj+2] - diff[jj+6];
     m2[j][7] = diff[jj+3] - diff[jj+7];
-    
+
     m1[j][0] = m2[j][0] + m2[j][2];
     m1[j][1] = m2[j][1] + m2[j][3];
@@ -3248,5 +3273,5 @@
     m1[j][6] = m2[j][4] - m2[j][6];
     m1[j][7] = m2[j][5] - m2[j][7];
-    
+
     m2[j][0] = m1[j][0] + m1[j][1];
     m2[j][1] = m1[j][0] - m1[j][1];
@@ -3258,5 +3283,5 @@
     m2[j][7] = m1[j][6] - m1[j][7];
   }
-  
+
   //vertical
   for (i=0; i < 8; i++)
@@ -3270,5 +3295,5 @@
     m3[6][i] = m2[2][i] - m2[6][i];
     m3[7][i] = m2[3][i] - m2[7][i];
-    
+
     m1[0][i] = m3[0][i] + m3[2][i];
     m1[1][i] = m3[1][i] + m3[3][i];
@@ -3279,5 +3304,5 @@
     m1[6][i] = m3[4][i] - m3[6][i];
     m1[7][i] = m3[5][i] - m3[7][i];
-    
+
     m2[0][i] = m1[0][i] + m1[1][i];
     m2[1][i] = m1[0][i] - m1[1][i];
@@ -3289,5 +3314,5 @@
     m2[7][i] = m1[6][i] - m1[7][i];
   }
-  
+
   for (i = 0; i < 8; i++)
   {
@@ -3297,81 +3322,18 @@
     }
   }
-  
+
   sad=((sad+2)>>2);
-  
+
   return sad;
 }
 
-UInt TComRdCost::xGetHADs4( DistParam* pcDtParam )
+
+Distortion TComRdCost::xGetHADs( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetHADs4w( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
-  Int  iRows   = pcDtParam->iRows;
-  Int  iStrideCur = pcDtParam->iStrideCur;
-  Int  iStrideOrg = pcDtParam->iStrideOrg;
-  Int  iStep  = pcDtParam->iStep;
-  Int  y;
-  Int  iOffsetOrg = iStrideOrg<<2;
-  Int  iOffsetCur = iStrideCur<<2;
-  
-  UInt uiSum = 0;
-  
-  for ( y=0; y<iRows; y+= 4 )
-  {
-    uiSum += xCalcHADs4x4( piOrg, piCur, iStrideOrg, iStrideCur, iStep );
-    piOrg += iOffsetOrg;
-    piCur += iOffsetCur;
-  }
-  
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-UInt TComRdCost::xGetHADs8( DistParam* pcDtParam )
-{
-  if ( pcDtParam->bApplyWeight )
-  {
-    return xGetHADs8w( pcDtParam );
-  }
-  Pel* piOrg   = pcDtParam->pOrg;
-  Pel* piCur   = pcDtParam->pCur;
-  Int  iRows   = pcDtParam->iRows;
-  Int  iStrideCur = pcDtParam->iStrideCur;
-  Int  iStrideOrg = pcDtParam->iStrideOrg;
-  Int  iStep  = pcDtParam->iStep;
-  Int  y;
-  
-  UInt uiSum = 0;
-  
-  if ( iRows == 4 )
-  {
-    uiSum += xCalcHADs4x4( piOrg+0, piCur        , iStrideOrg, iStrideCur, iStep );
-    uiSum += xCalcHADs4x4( piOrg+4, piCur+4*iStep, iStrideOrg, iStrideCur, iStep );
-  }
-  else
-  {
-    Int  iOffsetOrg = iStrideOrg<<3;
-    Int  iOffsetCur = iStrideCur<<3;
-    for ( y=0; y<iRows; y+= 8 )
-    {
-      uiSum += xCalcHADs8x8( piOrg, piCur, iStrideOrg, iStrideCur, iStep );
-      piOrg += iOffsetOrg;
-      piCur += iOffsetCur;
-    }
-  }
-  
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-UInt TComRdCost::xGetHADs( DistParam* pcDtParam )
-{
-  if ( pcDtParam->bApplyWeight )
-  {
-    return xGetHADsw( pcDtParam );
-  }
-#if H_3D_IC
+    return TComRdCostWeightPrediction::xGetHADsw( pcDtParam );
+  }
+#if NH_3D_IC
   if( pcDtParam->bUseIC )
   {
@@ -3379,5 +3341,5 @@
   }
 #endif
-#if H_3D_INTER_SDC
+#if NH_3D_SDC_INTER
   if( pcDtParam->bUseSDCMRSAD )
   {
@@ -3385,4 +3347,5 @@
   }
 #endif
+
   Pel* piOrg   = pcDtParam->pOrg;
   Pel* piCur   = pcDtParam->pCur;
@@ -3392,9 +3355,9 @@
   Int  iStrideOrg = pcDtParam->iStrideOrg;
   Int  iStep  = pcDtParam->iStep;
-  
+
   Int  x, y;
-  
-  UInt uiSum = 0;
-  
+
+  Distortion uiSum = 0;
+
   if( ( iRows % 8 == 0) && (iCols % 8 == 0) )
   {
@@ -3415,5 +3378,5 @@
     Int  iOffsetOrg = iStrideOrg<<2;
     Int  iOffsetCur = iStrideCur<<2;
-    
+
     for ( y=0; y<iRows; y+= 4 )
     {
@@ -3444,14 +3407,14 @@
     assert(false);
   }
-  
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
-}
-
-#if H_3D_IC || H_3D_INTER_SDC
+
+  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+}
+
+#if NH_3D_IC || NH_3D_SDC_INTER
 UInt TComRdCost::xGetHADsic( DistParam* pcDtParam )
 {
   if ( pcDtParam->bApplyWeight )
   {
-    return xGetHADsw( pcDtParam );
+    return TComRdCostWeightPrediction::xGetHADsw( pcDtParam );
   }
   Pel* piOrg   = pcDtParam->pOrg;
@@ -3593,6 +3556,5 @@
 }
 #endif
-
-#if H_3D_VSO
+#if NH_3D_VSO
 Void TComRdCost::setLambdaVSO( Double dLambdaVSO )
 {
@@ -3602,5 +3564,6 @@
   m_uiLambdaMotionSSEVSO = (UInt)floor(65536.0 *       m_dLambdaVSO    );
 }
-
+#endif
+#if NH_3D_VSO
 Dist TComRdCost::xGetDistVSOMode4( Int iStartPosX, Int iStartPosY, Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bSAD )
 { 
@@ -3621,5 +3584,5 @@
 
 
-Dist TComRdCost::getDistPartVSO( TComDataCU* pcCU, UInt uiAbsPartIndex, Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bHAD )
+Dist TComRdCost::getDistPartVSO( TComDataCU* pcCU, UInt uiAbsPartIndex, Int bitDepth, Pel* piCur, Int iCurStride, Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, Bool bHAD )
 { 
   assert( m_bUseVSO );  
@@ -3635,5 +3598,5 @@
   if ( m_bUseWVSO )   
   {
-    Int iDWeight   = m_iDWeight   * m_iDWeight;
+    Int iDWeight   = m_iDWeight   * m_iDWeight  ;
     Int iVSOWeight = m_iVSOWeight * m_iVSOWeight;
     Dist distDepth;
@@ -3641,9 +3604,9 @@
     if ( !bHAD )
     {
-       distDepth = (Dist) getDistPart( g_bitDepthY, piCur, iCurStride, piOrg, iOrgStride, uiBlkWidth, uiBlkHeight);
+       distDepth = (Dist) getDistPart( bitDepth, piCur, iCurStride, piOrg, iOrgStride, uiBlkWidth, uiBlkHeight, COMPONENT_Y);
     }
     else
     {
-       distDepth = (Dist) calcHAD( g_bitDepthY, piCur, iCurStride, piOrg, iOrgStride, uiBlkWidth, uiBlkHeight);
+       distDepth = (Dist) calcHAD    ( bitDepth, piCur, iCurStride, piOrg, iOrgStride, uiBlkWidth, uiBlkHeight);
     }
     
@@ -3722,8 +3685,21 @@
   }
 
+  D_PRINT_INDENT( g_traceRDCost, "VSO Dist: " + n2s(uiDistortion) + " Bits: " + n2s(uiBits) + " RD Cost: " + n2s(dRdCost)); 
+
   return dRdCost;
 }
 
-Void TComRdCost::setRenModelData( TComDataCU* pcCU, UInt uiAbsPartIndex, Pel* piData, Int iStride, Int iBlkWidth, Int iBlkHeight )
+Void TComRdCost::setRenModelData( const TComDataCU* pcCU, UInt uiAbsPartIndex, const TComYuv* pcYuv, const TComTURecurse* tuRecurseWithPU )
+{
+  const TComRectangle &puRect=tuRecurseWithPU->getRect(COMPONENT_Y);
+  const UInt  uiCompWidth   = puRect.width;
+  const UInt  uiCompHeight  = puRect.height;
+
+  const Pel*  piSrc         = pcYuv->getAddr( COMPONENT_Y, uiAbsPartIndex );
+  const UInt  uiSrcStride   = pcYuv->getStride( COMPONENT_Y);
+  setRenModelData( pcCU, uiAbsPartIndex, piSrc, uiSrcStride, uiCompWidth, uiCompHeight ); 
+}
+
+Void TComRdCost::setRenModelData( const TComDataCU* pcCU, UInt uiAbsPartIndex, const Pel* piData, Int iStride, Int iBlkWidth, Int iBlkHeight )
 {
   UInt iBlkX = g_auiRasterToPelX[g_auiZscanToRaster[uiAbsPartIndex]];
