Index: /branches/SHM-2.1-dev/source/Lib/TLibCommon/TComUpsampleFilter.cpp
===================================================================
--- /branches/SHM-2.1-dev/source/Lib/TLibCommon/TComUpsampleFilter.cpp	(revision 286)
+++ /branches/SHM-2.1-dev/source/Lib/TLibCommon/TComUpsampleFilter.cpp	(revision 287)
@@ -242,5 +242,4 @@
     }
 
-
     //========== vertical upsampling ===========
     pcTempPic->setBorderExtension(false);
@@ -269,13 +268,10 @@
 
       piSrcY = piTempBufY + (refPos -((NTAPS_US_LUMA>>1) - 1))*strideEL;
-      piDstY = piDstBufY + j * strideEL;
-#if SCALED_REF_LAYER_OFFSETS
-      for( i = 0; i < leftStartL; i++ )
-      {
-        *piDstY = ClipY( (sumLumaVer(piSrcY, coeff, strideEL) + iOffset) >> (nShift));
-        piDstY++;
-      }
-
-      for( i = leftStartL; i < rightEndL-1; i++ )
+#if SCALED_REF_LAYER_OFFSETS
+#if 1 // it should provide identical result
+      Pel* piDstY0 = piDstBufY + j * strideEL;            
+      piDstY = piDstY0 + ( leftStartL > 0 ? leftStartL : 0 );
+
+      for( i = min<Int>(rightEndL, pcTempPic->getWidth()) - max<Int>(0, leftStartL); i > 0; i-- )
       {
         *piDstY = ClipY( (sumLumaVer(piSrcY, coeff, strideEL) + iOffset) >> (nShift));
@@ -284,10 +280,35 @@
       }
 
-      for( i = rightEndL-1; i < pcTempPic->getWidth(); i++ )
+      for( i = rightEndL; i < pcTempPic->getWidth(); i++ )
+      {
+        *piDstY = piDstY0[rightEndL-1];
+        piDstY++;
+      }
+
+      piDstY = piDstY0;
+      for( i = 0; i < leftStartL; i++ )
+      {
+        *piDstY = piDstY0[leftStartL];
+        piDstY++;
+      }
+#else
+      piDstY = piDstBufY + j * strideEL;
+
+      for( i = 0; i < pcTempPic->getWidth(); i++ )
       {
         *piDstY = ClipY( (sumLumaVer(piSrcY, coeff, strideEL) + iOffset) >> (nShift));
+
+        // Only increase the x position of reference upsample picture when within the window
+        // "-2" to ensure that pointer doesn't go beyond the boundary rightEndL-1
+        if( (i >= leftStartL) && (i <= rightEndL-2) )
+        {
+          piSrcY++;
+        }
         piDstY++;
       }
-#else
+#endif
+#else
+      piDstY = piDstBufY + j * strideEL;
+
       for( i = 0; i < widthEL; i++ )
       {
@@ -411,21 +432,15 @@
       refPos   = refPos16 >> 4; 
       coeff = m_chromaFilter[phase];
-
+      
       piSrcU = piTempBufU  + (refPos -((NTAPS_US_CHROMA>>1) - 1))*strideEL;
       piSrcV = piTempBufV  + (refPos -((NTAPS_US_CHROMA>>1) - 1))*strideEL;
-
-      piDstU = piDstBufU + j*strideEL;
-      piDstV = piDstBufV + j*strideEL;
-
-#if SCALED_REF_LAYER_OFFSETS
-      for( i = 0; i < leftStartC; i++ )
-      {
-        *piDstU = ClipC( (sumChromaVer(piSrcU, coeff, strideEL) + iOffset) >> (nShift));
-        *piDstV = ClipC( (sumChromaVer(piSrcV, coeff, strideEL) + iOffset) >> (nShift));
-        piDstU++;
-        piDstV++;
-      }
-
-      for( i = leftStartC; i < rightEndC-1; i++ )
+#if SCALED_REF_LAYER_OFFSETS
+#if 1 // it should provide identical result
+      Pel* piDstU0 = piDstBufU + j*strideEL;
+      Pel* piDstV0 = piDstBufV + j*strideEL;
+      piDstU = piDstU0 + ( leftStartC > 0 ? leftStartC : 0 );
+      piDstV = piDstV0 + ( leftStartC > 0 ? leftStartC : 0 );
+
+      for( i = min<Int>(rightEndC, pcTempPic->getWidth() >> 1) - max<Int>(0, leftStartC); i > 0; i-- )
       {
         *piDstU = ClipC( (sumChromaVer(piSrcU, coeff, strideEL) + iOffset) >> (nShift));
@@ -437,12 +452,46 @@
       }
 
-      for( i = rightEndC-1; i < pcTempPic->getWidth() >> 1; i++ )
+      for( i = rightEndC; i < pcTempPic->getWidth() >> 1; i++ )
+      {
+        *piDstU = piDstU0[rightEndC-1];
+        *piDstV = piDstV0[rightEndC-1];
+        piDstU++;
+        piDstV++;
+      }
+
+      piDstU = piDstU0;
+      piDstV = piDstV0;
+      for( i = 0; i < leftStartC; i++ )
+      {
+        *piDstU = piDstU0[leftStartC];
+        *piDstV = piDstV0[leftStartC];
+        piDstU++;
+        piDstV++;
+      }
+#else
+      piDstU = piDstBufU + j*strideEL;
+      piDstV = piDstBufV + j*strideEL;
+
+      for( i = 0; i < pcTempPic->getWidth() >> 1; i++ )
       {
         *piDstU = ClipC( (sumChromaVer(piSrcU, coeff, strideEL) + iOffset) >> (nShift));
         *piDstV = ClipC( (sumChromaVer(piSrcV, coeff, strideEL) + iOffset) >> (nShift));
+
+        // Only increase the x position of reference upsample picture when within the window
+        // "-2" to ensure that pointer doesn't go beyond the boundary rightEndC-1
+        if( (i >= leftStartC) && (i <= rightEndC-2) )
+        {
+          piSrcU++;
+          piSrcV++;
+        }
+
         piDstU++;
         piDstV++;
       }
-#else
+#endif
+#else
+      piDstU = piDstBufU + j*strideEL;
+      piDstV = piDstBufV + j*strideEL;
+
       for( i = 0; i < widthEL; i++ )
       {
