Index: /branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.cpp	(revision 1396)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.cpp	(revision 1397)
@@ -194,4 +194,5 @@
   // initialize
   rcDistParam.iSubShift  = 0;
+  rcDistParam.m_maximumDistortionForEarlyExit = std::numeric_limits<Distortion>::max();
 }
 
@@ -210,4 +211,5 @@
   rcDistParam.iRows    = pcPatternKey->getROIYHeight();
   rcDistParam.DistFunc = m_afpDistortFunc[DF_SAD + g_aucConvertToBit[ rcDistParam.iCols ] + 1 ];
+  rcDistParam.m_maximumDistortionForEarlyExit = std::numeric_limits<Distortion>::max();
 
   if (rcDistParam.iCols == 12)
@@ -244,4 +246,6 @@
   rcDistParam.iCols    = pcPatternKey->getROIYWidth();
   rcDistParam.iRows    = pcPatternKey->getROIYHeight();
+
+  rcDistParam.m_maximumDistortionForEarlyExit = std::numeric_limits<Distortion>::max();
 
   // set distortion function
@@ -283,4 +287,5 @@
   rcDP.bitDepth     = bitDepth;
   rcDP.DistFunc     = m_afpDistortFunc[ ( bHadamard ? DF_HADS : DF_SADS ) + g_aucConvertToBit[ iWidth ] + 1 ];
+  rcDP.m_maximumDistortionForEarlyExit = std::numeric_limits<Distortion>::max();
 }
 
@@ -358,14 +363,14 @@
     return TComRdCostWeightPrediction::xGetSADw( pcDtParam );
   }
-  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;
+  const Pel* piOrg           = pcDtParam->pOrg;
+  const Pel* piCur           = pcDtParam->pCur;
+  const Int  iCols           = pcDtParam->iCols;
+  const Int  iStrideCur      = pcDtParam->iStrideCur;
+  const Int  iStrideOrg      = pcDtParam->iStrideOrg;
+  const UInt distortionShift = DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth - 8);
 
   Distortion uiSum = 0;
 
-  for( ; iRows != 0; iRows-- )
+  for(Int iRows = pcDtParam->iRows ; iRows != 0; iRows-- )
   {
     for (Int n = 0; n < iCols; n++ )
@@ -373,9 +378,13 @@
       uiSum += abs( piOrg[n] - piCur[n] );
     }
-    piOrg += iStrideOrg;
-    piCur += iStrideCur;
-  }
-
-  return ( uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8) );
+    if (pcDtParam->m_maximumDistortionForEarlyExit < ( uiSum >> distortionShift ))
+    {
+      return ( uiSum >> distortionShift );
+    }
+    piOrg += iStrideOrg;
+    piCur += iStrideCur;
+  }
+
+  return ( uiSum >> distortionShift );
 }
 
Index: /branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.h	(revision 1396)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TComRdCost.h	(revision 1397)
@@ -81,4 +81,5 @@
   const WPScalingParam *wpCur;           // weighted prediction scaling parameters for current ref
   ComponentID           compIdx;
+  Distortion            m_maximumDistortionForEarlyExit; /// During cost calculations, if distortion exceeds this value, cost calculations may early-terminate.
 
   // (vertical) subsampling shift (for reducing complexity)
@@ -99,4 +100,5 @@
      wpCur(NULL),
      compIdx(MAX_NUM_COMPONENT),
+     m_maximumDistortionForEarlyExit(std::numeric_limits<Distortion>::max()),
      iSubShift(0)
   { }
Index: /branches/SHM-dev/source/Lib/TLibCommon/TComRdCostWeightPrediction.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TComRdCostWeightPrediction.cpp	(revision 1396)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TComRdCostWeightPrediction.cpp	(revision 1397)
@@ -70,4 +70,5 @@
   const Int             shift      = wpCur.shift;
   const Int             round      = wpCur.round;
+  const Int        distortionShift = DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
 
   Distortion uiSum = 0;
@@ -80,4 +81,8 @@
 
       uiSum += abs( piOrg[n] - pred );
+    }
+    if (pcDtParam->m_maximumDistortionForEarlyExit <  ( uiSum >> distortionShift))
+    {
+      return uiSum >> distortionShift;
     }
     piOrg += iStrideOrg;
@@ -87,5 +92,5 @@
   pcDtParam->compIdx = MAX_NUM_COMPONENT;  // reset for DEBUG (assert test)
 
-  return uiSum >> DISTORTION_PRECISION_ADJUSTMENT(pcDtParam->bitDepth-8);
+  return uiSum >> distortionShift;
 }
 
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncSearch.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncSearch.cpp	(revision 1396)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncSearch.cpp	(revision 1397)
@@ -386,4 +386,6 @@
   // distortion
   m_cDistParam.bitDepth = pcPatternKey->getBitDepthY();
+  m_cDistParam.m_maximumDistortionForEarlyExit = rcStruct.uiBestSad;
+
   if(m_pcEncCfg->getFastSearch() == SELECTIVE)
   {
@@ -439,4 +441,5 @@
           rcStruct.uiBestRound    = 0;
           rcStruct.ucPointNr      = ucPointNr;
+          m_cDistParam.m_maximumDistortionForEarlyExit = uiSad;
         }
       }
@@ -458,4 +461,5 @@
       rcStruct.uiBestRound    = 0;
       rcStruct.ucPointNr      = ucPointNr;
+      m_cDistParam.m_maximumDistortionForEarlyExit = uiSad;
     }
   }
@@ -882,4 +886,5 @@
       uiDistBest  = uiDist;
       uiDirecBest = i;
+      m_cDistParam.m_maximumDistortionForEarlyExit = uiDist;
     }
   }
@@ -3404,9 +3409,9 @@
             }
           }
-#if (ENCODER_FAST_MODE)
+#if ENCODER_FAST_MODE
           }
 #endif
         }
-#if (ENCODER_FAST_MODE)
+#if ENCODER_FAST_MODE
         } // for loop-iRefIdxTemp
 #endif
@@ -4064,4 +4069,5 @@
         iBestX    = x;
         iBestY    = y;
+        m_cDistParam.m_maximumDistortionForEarlyExit = uiSad;
       }
     }
