Index: branches/HTM-10.0rc1-dev0/source/App/TAppDecoder/TAppDecTop.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/App/TAppDecoder/TAppDecTop.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/App/TAppDecoder/TAppDecTop.cpp	(revision 845)
@@ -288,5 +288,5 @@
           else
           {
-            g_disableHLSTrace = true;     // Trancing of second parsing of SH is not carried out
+            g_disableHLSTrace = true;     // Tracing of second parsing of SH is not carried out
           }      
 #endif
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComRom.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComRom.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComRom.cpp	(revision 845)
@@ -372,8 +372,10 @@
 Bool g_traceTU = true; 
 Bool g_disableHLSTrace = false; 
-UInt64 g_stopAtCounter       = 48; 
+UInt64 g_stopAtCounter       = 0; 
 Bool g_traceCopyBack         = false; 
 Bool g_decTraceDispDer       = false; 
 Bool g_decTraceMvFromMerge   = false; 
+Bool g_stopAtPos             = false; 
+Bool g_outputPos             = false; 
 #endif
 #endif
@@ -562,4 +564,44 @@
 #if H_MV_ENC_DEC_TRAC
 #if ENC_DEC_TRACE
+Void stopAtPos( Int poc, Int layerId, Int cuPelX, Int cuPelY, Int cuWidth, Int cuHeight )
+{
+
+  if ( g_outputPos ) 
+  {
+    std::cout << "POC\t"        << poc 
+              << "\tLayerId\t"  << layerId 
+              << "\tCuPelX\t"   << cuPelX  
+              << "\tCuPelY\t"   << cuPelY 
+              << "\tCuWidth\t"  << cuWidth
+              << "\tCuHeight\t" << cuHeight
+              << std::endl; 
+  }
+
+  Bool stopFlag = false; 
+  if ( g_stopAtPos && poc == 0 && layerId == 4 )
+  {
+    Bool stopAtCU = true; 
+    if ( stopAtCU )        // Stop at CU with specific size
+    {    
+      stopFlag = ( cuPelX  == 16 ) && ( cuPelY  == 112 ) && ( cuWidth == 16 ) && ( cuHeight == 16); 
+    }
+    else
+    {                     // Stop at specific position 
+      Int xPos = 18; 
+      Int yPos = 112; 
+
+      Int cuPelXEnd = cuPelX + cuWidth  - 1; 
+      Int cuPelYEnd = cuPelY + cuHeight - 1; 
+
+      stopFlag = (cuPelX <= xPos ) && (cuPelXEnd >= xPos ) && (cuPelY <= yPos ) && (cuPelYEnd >= yPos ); 
+    }
+  }
+  
+  if ( stopFlag )
+  { // Set breakpoint here. 
+    std::cout << "Stop position. Break point here." << std::endl; 
+  }  
+}
+
 Void writeToTraceFile( const Char* symbolName, Int val, Bool doIt )
 {
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComRom.h
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComRom.h	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComRom.h	(revision 845)
@@ -219,6 +219,8 @@
  extern UInt64 g_stopAtCounter;         // Counter to set breakpoint. 
  extern Bool   g_traceCopyBack;         // Output samples on copy back  
- extern Bool   g_decTraceDispDer; // Trace derived disparity vectors (decoder only) 
+ extern Bool   g_decTraceDispDer;       // Trace derived disparity vectors (decoder only) 
  extern Bool   g_decTraceMvFromMerge;   // Trace motion vectors obtained from merge (decoder only) 
+ extern Bool   g_stopAtPos;             // Stop at position
+ extern Bool   g_outputPos;             // Output position
 
 #define DTRACE_CU(x,y)             writeToTraceFile( x,y, g_traceCU );
@@ -229,7 +231,7 @@
 #define DTRACE_TU_S(x)             writeToTraceFile( x,   g_traceTU );
 
-
  Void           writeToTraceFile( const Char* symbolName, Int val, Bool doIt );
  Void           writeToTraceFile( const Char* symbolName, Bool doIt );
+ Void           stopAtPos       ( Int poc, Int layerId, Int cuPelX, Int cuPelY, Int cuWidth, Int cuHeight );
 #endif
 #else
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp	(revision 845)
@@ -41,5 +41,4 @@
 #include <stdio.h>
 #include <math.h>
-
 //! \ingroup TLibCommon
 //! \{
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComSlice.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComSlice.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TComSlice.cpp	(revision 845)
@@ -1942,4 +1942,5 @@
   for ( i = 0; i < iNumViews ; i++ )
   {
+    m_bCamParInSliceHeader[i] = false; 
     m_aaaiCodedScale[i] = new Int*[ 2 ];
     m_aaaiCodedOffset[i] = new Int*[ 2 ];
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TypeDef.h	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibCommon/TypeDef.h	(revision 845)
@@ -327,6 +327,6 @@
 ///////////////////////////////////   HTM-10.1 Integrations //////////////////////////////
 /////////////////////////////////////////////////////////////////////////////////////////
-
 #define UPDATE_HM13                       1
+#define H_3D_FIX_G0148_BRACE              1 
 /////////////////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////   HM RELATED DEFINES ////////////////////////////////
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibDecoder/TDecCu.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibDecoder/TDecCu.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibDecoder/TDecCu.cpp	(revision 845)
@@ -37,5 +37,4 @@
 
 #include "TDecCu.h"
-
 //! \ingroup TLibDecoder
 //! \{
@@ -639,5 +638,16 @@
   
   m_ppcCU[uiDepth]->copySubCU( pcCU, uiAbsPartIdx, uiDepth );
-  
+
+#if H_MV_ENC_DEC_TRAC
+#if ENC_DEC_TRACE
+  stopAtPos  ( m_ppcCU[uiDepth]->getSlice()->getPOC(), 
+    m_ppcCU[uiDepth]->getSlice()->getLayerId(), 
+    m_ppcCU[uiDepth]->getCUPelX(),
+    m_ppcCU[uiDepth]->getCUPelY(),
+    m_ppcCU[uiDepth]->getWidth(0), 
+    m_ppcCU[uiDepth]->getHeight(0) );
+#endif
+#endif
+
   switch( m_ppcCU[uiDepth]->getPredictionMode(0) )
   {
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibDecoder/TDecGop.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibDecoder/TDecGop.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibDecoder/TDecGop.cpp	(revision 845)
@@ -43,5 +43,4 @@
 #include "libmd5/MD5.h"
 #include "TLibCommon/SEI.h"
-
 #include <time.h>
 
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncCu.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncCu.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncCu.cpp	(revision 845)
@@ -40,5 +40,4 @@
 #include "TEncCu.h"
 #include "TEncAnalyze.h"
-
 #include <cmath>
 #include <algorithm>
@@ -422,5 +421,5 @@
   // get Original YUV data from picture
   m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() );
-
+  
 #if H_3D_QTLPC  
   Bool    bTrySplit     = true;
@@ -449,4 +448,15 @@
   UInt uiTPelY   = rpcBestCU->getCUPelY();
   UInt uiBPelY   = uiTPelY + rpcBestCU->getHeight(0) - 1;
+
+#if H_MV_ENC_DEC_TRAC
+#if ENC_DEC_TRACE
+    stopAtPos  ( rpcBestCU->getSlice()->getPOC(), 
+                 rpcBestCU->getSlice()->getLayerId(), 
+                 rpcBestCU->getCUPelX(),
+                 rpcBestCU->getCUPelY(),
+                 rpcBestCU->getWidth(0), 
+                 rpcBestCU->getHeight(0) );
+#endif
+#endif
 
   Int iBaseQP = xComputeQP( rpcBestCU, uiDepth );
@@ -1188,12 +1198,12 @@
         if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) )
         {
-            if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
-            {
-              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
-            }
-            else
-            {
-              m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
-            }
+          if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer
+          {
+            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]);
+          }
+          else
+          {
+            m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]);
+          }
 
 #if AMP_ENC_SPEEDUP
@@ -1226,6 +1236,6 @@
 
         rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits
-          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
-        }
+        rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
+      }
 #if H_3D_VSO // M10
       if ( m_pcRdCost->getUseVSO() )
@@ -1233,5 +1243,5 @@
       else
 #endif
-      rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
+        rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
 
       if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP())
@@ -1241,5 +1251,5 @@
         {
           if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && 
-              ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
+            ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) )
           {
             hasResidual = true;
@@ -1263,5 +1273,5 @@
           m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false );
           rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits
-            rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
+          rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded();
 #if H_3D_VSO // M11
           if ( m_pcRdCost->getUseLambdaScaleVSO())          
@@ -1269,5 +1279,5 @@
           else
 #endif
-          rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
+            rpcTempCU->getTotalCost()  = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() );
 #endif
 
@@ -1282,9 +1292,9 @@
       }
 
-        m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
+      m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]);
       Bool isEndOfSlice        = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES
-                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
+        && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3);
       Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES
-                                 && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
+        && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3);
       if(isEndOfSlice||isEndOfSliceSegment)
       {
@@ -1293,5 +1303,5 @@
       xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth);                                  // RD compare current larger prediction
     }                                                                                  // with sub partitioned prediction.
-    }
+  }
 
 #if H_3D_VSO // M12 
@@ -1776,7 +1786,7 @@
 #endif
 #if H_3D_ARP
-for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
-#else
-for( UInt ui = 0; ui < numValidMergeCand; ++ui )
+  for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui )
+#else
+  for( UInt ui = 0; ui < numValidMergeCand; ++ui )
 #endif
   {
@@ -1851,8 +1861,8 @@
 #endif
 
-  for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
-  {
-    for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
-    {      
+    for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual )
+    {
+      for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand )
+      {      
 #if H_3D_IC
         if( rpcTempCU->getSlice()->getApplyIC() && rpcTempCU->getSlice()->getIcSkipParseFlag() )
@@ -1866,135 +1876,167 @@
         if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1))
         {
-        if( !(bestIsSkip && uiNoResidual == 0) )
-        {
-          // set MC parameters
-          rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
-          rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag,     0, uhDepth );
-          rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
+          if( !(bestIsSkip && uiNoResidual == 0) )
+          {
+            // set MC parameters
+            rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level
+            rpcTempCU->setCUTransquantBypassSubParts( bTransquantBypassFlag,     0, uhDepth );
+            rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level
 #if H_3D_IC
-          rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
+            rpcTempCU->setICFlagSubParts( bICFlag, 0, 0, uhDepth );
 #endif
 #if H_3D_ARP
-          rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
-#endif
-          rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
-          rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
+            rpcTempCU->setARPWSubParts( (UChar)nARPW , 0 , uhDepth );
+#endif
+            rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level
+            rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level
 #if H_3D_VSP
-          rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
-          rpcTempCU->setDvInfoSubParts(inheritedVSPDisInfo[uiMergeCand].m_acDvInfo, 0, 0, uhDepth );
+            rpcTempCU->setVSPFlagSubParts( vspFlag[uiMergeCand], 0, 0, uhDepth );
+            rpcTempCU->setDvInfoSubParts(inheritedVSPDisInfo[uiMergeCand].m_acDvInfo, 0, 0, uhDepth );
 #endif
 #if MTK_DDD_G0063
-          if( rpcTempCU->getSlice()->getIsDepth() && rpcTempCU->getSlice()->getViewIndex() != 0 && iDDDCand == uiMergeCand )
-          {
+            if( rpcTempCU->getSlice()->getIsDepth() && rpcTempCU->getSlice()->getViewIndex() != 0 && iDDDCand == uiMergeCand )
+            {
               rpcTempCU->setUseDDD( true, 0, 0, uhDepth );
               rpcTempCU->setDDDepthSubParts( ucDDDepth, 0, 0, uhDepth );
-          }
-          else
-          {
-              rpcTempCU->setUseDDD( false, 0, 0, uhDepth );
-          }
-#endif
-#if H_3D_SPIVMP
-          rpcTempCU->setSPIVMPFlagSubParts(bSPIVMPFlag[uiMergeCand], 0, 0, uhDepth);
-          if (bSPIVMPFlag[uiMergeCand])
-          {
-            UInt uiSPAddr;
-            Int iWidth = rpcTempCU->getWidth(0);
-            Int iHeight = rpcTempCU->getHeight(0);
-            Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
-            rpcTempCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
-            for (Int iPartitionIdx = 0; iPartitionIdx < iNumSP; iPartitionIdx++)
-            {
-              rpcTempCU->getSPAbsPartIdx(0, iSPWidth, iSPHeight, iPartitionIdx, iNumSPInOneLine, uiSPAddr);
-              rpcTempCU->setInterDirSP(puhInterDirSP[iPartitionIdx], uiSPAddr, iSPWidth, iSPHeight);
-              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx], iSPWidth, iSPHeight);
-              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx + 1], iSPWidth, iSPHeight);
-            }
-          }
-          else
-#endif
-#if NTT_STORE_SPDV_VSP_G0148
-          if ( vspFlag[uiMergeCand] )
-          {
-            UInt partAddr;
-            Int vspSize;
-            Int width, height;
-            rpcTempCU->getPartIndexAndSize( 0, partAddr, width, height );
-            if( uhInterDirNeighbours[ uiMergeCand ] & 0x01 )
-            {
-              rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_0, cMvFieldNeighbours[ 2*uiMergeCand + 0 ].getRefIdx(), vspSize );
-              rpcTempCU->setVSPFlag( partAddr, vspSize );
             }
             else
             {
-              rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
+              rpcTempCU->setUseDDD( false, 0, 0, uhDepth );
             }
-            if( uhInterDirNeighbours[ uiMergeCand ] & 0x02 )
+#endif
+#if H_3D_SPIVMP
+            rpcTempCU->setSPIVMPFlagSubParts(bSPIVMPFlag[uiMergeCand], 0, 0, uhDepth);
+            if (bSPIVMPFlag[uiMergeCand])
             {
-              rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_1 , cMvFieldNeighbours[ 2*uiMergeCand + 1 ].getRefIdx(), vspSize );
-              rpcTempCU->setVSPFlag( partAddr, vspSize );
+              UInt uiSPAddr;
+              Int iWidth = rpcTempCU->getWidth(0);
+              Int iHeight = rpcTempCU->getHeight(0);
+              Int iNumSPInOneLine, iNumSP, iSPWidth, iSPHeight;
+              rpcTempCU->getSPPara(iWidth, iHeight, iNumSP, iNumSPInOneLine, iSPWidth, iSPHeight);
+              for (Int iPartitionIdx = 0; iPartitionIdx < iNumSP; iPartitionIdx++)
+              {
+                rpcTempCU->getSPAbsPartIdx(0, iSPWidth, iSPHeight, iPartitionIdx, iNumSPInOneLine, uiSPAddr);
+                rpcTempCU->setInterDirSP(puhInterDirSP[iPartitionIdx], uiSPAddr, iSPWidth, iSPHeight);
+                rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx], iSPWidth, iSPHeight);
+                rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setMvFieldSP(rpcTempCU, uiSPAddr, pcMvFieldSP[2*iPartitionIdx + 1], iSPWidth, iSPHeight);
+              }
             }
             else
+#endif
+#if NTT_STORE_SPDV_VSP_G0148
             {
-              rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
+              if ( vspFlag[uiMergeCand] )
+              {
+                UInt partAddr;
+                Int vspSize;
+                Int width, height;
+                rpcTempCU->getPartIndexAndSize( 0, partAddr, width, height );
+                if( uhInterDirNeighbours[ uiMergeCand ] & 0x01 )
+                {
+                  rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_0, cMvFieldNeighbours[ 2*uiMergeCand + 0 ].getRefIdx(), vspSize );
+                  rpcTempCU->setVSPFlag( partAddr, vspSize );
+                }
+                else
+                {
+                  rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
+                }
+                if( uhInterDirNeighbours[ uiMergeCand ] & 0x02 )
+                {
+                  rpcTempCU->setMvFieldPUForVSP( rpcTempCU, partAddr, width, height, REF_PIC_LIST_1 , cMvFieldNeighbours[ 2*uiMergeCand + 1 ].getRefIdx(), vspSize );
+                  rpcTempCU->setVSPFlag( partAddr, vspSize );
+                }
+                else
+                {
+                  rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
+                }
+                rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
+              }
+              else
+              {
+#endif
+                rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
+                rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
+                rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
+#if NTT_STORE_SPDV_VSP_G0148
+              }
             }
-            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
-          }
-          else
-          {
-#endif
-            rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level
-            rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
-            rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level
-#if NTT_STORE_SPDV_VSP_G0148
-          }
-#endif
-       // do MC
-       m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
-       // estimate residual and encode everything
+#endif
+            // do MC
+            m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] );
+            // estimate residual and encode everything
 #if H_3D_VSO //M2
-       if( m_pcRdCost->getUseRenModel() )
-       { //Reset
-         UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
-         UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
-         Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
-         UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
-         m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
-       }
-#endif
-       m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
-         m_ppcOrigYuv    [uhDepth],
-         m_ppcPredYuvTemp[uhDepth],
-         m_ppcResiYuvTemp[uhDepth],
-         m_ppcResiYuvBest[uhDepth],
-         m_ppcRecoYuvTemp[uhDepth],
-         (uiNoResidual? true:false));
-
-
-          if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
-         {
-            // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
-           mergeCandBuffer[uiMergeCand] = 1;
-         }
-
-          rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
+            if( m_pcRdCost->getUseRenModel() )
+            { //Reset
+              UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
+              UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
+              Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
+              UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
+              m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
+            }
+#endif
+            m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU,
+              m_ppcOrigYuv    [uhDepth],
+              m_ppcPredYuvTemp[uhDepth],
+              m_ppcResiYuvTemp[uhDepth],
+              m_ppcResiYuvBest[uhDepth],
+              m_ppcRecoYuvTemp[uhDepth],
+              (uiNoResidual? true:false));
+
+
+            if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 )
+            {
+              // If no residual when allowing for one, then set mark to not try case where residual is forced to 0
+              mergeCandBuffer[uiMergeCand] = 1;
+            }
+
+            rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth );
 #if NTT_STORE_SPDV_VSP_G0148 // possible bug fix
-          if( rpcTempCU->getSkipFlag(0) )
-          {
-            rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
-          }
+            if( rpcTempCU->getSkipFlag(0) )
+            {
+              rpcTempCU->setTrIdxSubParts(0, 0, uhDepth);
+            }
 #endif
 #if H_3D_INTER_SDC
-          TComDataCU *rpcTempCUPre = rpcTempCU;
-#endif
-          Int orgQP = rpcTempCU->getQP( 0 );
-          xCheckDQP( rpcTempCU );
-          xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
+            TComDataCU *rpcTempCUPre = rpcTempCU;
+#endif
+            Int orgQP = rpcTempCU->getQP( 0 );
+            xCheckDQP( rpcTempCU );
+            xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth);
 #if H_3D_INTER_SDC
-          if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && !uiNoResidual )
-          {
+            if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) && rpcTempCU->getSlice()->getIsDepth() && !uiNoResidual )
+            {
 #if SEC_INTER_SDC_G0101
-            for( Int uiOffest = -2 ; uiOffest <= 2 ; uiOffest++ )
-            {
+              for( Int uiOffest = -2 ; uiOffest <= 2 ; uiOffest++ )
+              {
+                if( rpcTempCU != rpcTempCUPre )
+                {
+                  rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag  );
+                  rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
+                }
+                rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
+                rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
+                rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
+#if H_3D_VSO //M2
+                if( m_pcRdCost->getUseRenModel() )
+                { //Reset
+                  UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
+                  UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
+                  Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
+                  UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
+                  m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
+                }
+#endif
+                m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
+                  m_ppcOrigYuv[uhDepth], 
+                  ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
+                  m_ppcResiYuvTemp[uhDepth], 
+                  m_ppcRecoYuvTemp[uhDepth],
+                  uiOffest,
+                  uhDepth );
+
+                xCheckDQP( rpcTempCU );
+                xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
+              }
+#else
               if( rpcTempCU != rpcTempCUPre )
               {
@@ -2019,99 +2061,69 @@
                 ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
                 m_ppcResiYuvTemp[uhDepth], 
-                m_ppcRecoYuvTemp[uhDepth],
-                uiOffest,
+                m_ppcRecoYuvTemp[uhDepth], 
                 uhDepth );
 
               xCheckDQP( rpcTempCU );
               xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
+#endif
             }
-#else
-            if( rpcTempCU != rpcTempCUPre )
+#endif
+            rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
+
+            if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
             {
-              rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag  );
-              rpcTempCU->copyPartFrom( rpcBestCU, 0, uhDepth );
+#if H_3D_INTER_SDC
+              if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) )
+              {
+                bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
+              }
+              else
+              {
+#endif
+                bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
+#if H_3D_INTER_SDC
+              }
+#endif
             }
-            rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth );
-            rpcTempCU->setTrIdxSubParts( 0, 0, uhDepth );
-            rpcTempCU->setCbfSubParts( 1, 1, 1, 0, uhDepth );
-#if H_3D_VSO //M2
-            if( m_pcRdCost->getUseRenModel() )
-            { //Reset
-              UInt  uiWidth     = m_ppcOrigYuv[uhDepth]->getWidth    ();
-              UInt  uiHeight    = m_ppcOrigYuv[uhDepth]->getHeight   ();
-              Pel*  piSrc       = m_ppcOrigYuv[uhDepth]->getLumaAddr ();
-              UInt  uiSrcStride = m_ppcOrigYuv[uhDepth]->getStride   ();
-              m_pcRdCost->setRenModelData( rpcTempCU, 0, piSrc, uiSrcStride, uiWidth, uiHeight );
-            }
-#endif
-            m_pcPredSearch->encodeResAndCalcRdInterSDCCU( rpcTempCU, 
-              m_ppcOrigYuv[uhDepth], 
-              ( rpcTempCU != rpcTempCUPre ) ? m_ppcPredYuvBest[uhDepth] : m_ppcPredYuvTemp[uhDepth], 
-              m_ppcResiYuvTemp[uhDepth], 
-              m_ppcRecoYuvTemp[uhDepth], 
-              uhDepth );
-
-            xCheckDQP( rpcTempCU );
-            xCheckBestMode( rpcBestCU, rpcTempCU, uhDepth );
-#endif
-          }
-#endif
-          rpcTempCU->initEstData( uhDepth, orgQP, bTransquantBypassFlag );
-
-      if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip )
-      {
-#if H_3D_INTER_SDC
-        if( rpcTempCU->getSlice()->getVPS()->getInterSDCFlag( rpcTempCU->getSlice()->getLayerIdInVps() ) )
-        {
-          bestIsSkip = !rpcBestCU->getSDCFlag( 0 ) && ( rpcBestCU->getQtRootCbf(0) == 0 );
-        }
-        else
-        {
-#endif
-        bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0;
-#if H_3D_INTER_SDC
-        }
-#endif
-      }
-    }
-   }
-  }
-
-  if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
-  {
-    if(rpcBestCU->getQtRootCbf( 0 ) == 0)
-    {
-      if( rpcBestCU->getMergeFlag( 0 ))
-      {
-        *earlyDetectionSkipMode = true;
-      }
-      else
-      {
-        Int absoulte_MV=0;
-        for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
-        {
-          if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
-          {
-            TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
-            Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
-            Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
-            absoulte_MV+=iHor+iVer;
           }
         }
-
-        if(absoulte_MV == 0)
+      }
+
+      if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection())
+      {
+        if(rpcBestCU->getQtRootCbf( 0 ) == 0)
         {
-          *earlyDetectionSkipMode = true;
+          if( rpcBestCU->getMergeFlag( 0 ))
+          {
+            *earlyDetectionSkipMode = true;
+          }
+          else
+          {
+            Int absoulte_MV=0;
+            for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ )
+            {
+              if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 )
+              {
+                TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx ));
+                Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor();
+                Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer();
+                absoulte_MV+=iHor+iVer;
+              }
+            }
+
+            if(absoulte_MV == 0)
+            {
+              *earlyDetectionSkipMode = true;
+            }
+          }
         }
       }
     }
-  }
- }
 #if H_3D_SPIVMP
- delete[] pcMvFieldSP;
- delete[] puhInterDirSP;
+    delete[] pcMvFieldSP;
+    delete[] puhInterDirSP;
 #endif
 #if H_3D_ARP
- }
+  }
 #endif
 }
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 845)
@@ -49,5 +49,4 @@
 #include <time.h>
 #include <math.h>
-
 using namespace std;
 //! \ingroup TLibEncoder
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.cpp	(revision 845)
@@ -41,5 +41,4 @@
 #include <stdio.h>
 #include <math.h>
-
 //! \ingroup TLibEncoder
 //! \{
Index: branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncSearch.cpp
===================================================================
--- branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncSearch.cpp	(revision 843)
+++ branches/HTM-10.0rc1-dev0/source/Lib/TLibEncoder/TEncSearch.cpp	(revision 845)
@@ -3950,5 +3950,4 @@
         pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], ePartSize, uiAbsPartIdx, 0, iPUIdx );
         pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], ePartSize, uiAbsPartIdx, 0, iPUIdx );
-
 #if NTT_STORE_SPDV_VSP_G0148
       }
@@ -4043,46 +4042,46 @@
   m_cYuvPredTemp.clear();
   rpcPredYuv->clear();
-  
+
   if ( !bUseRes )
   {
     rpcResiYuv->clear();
   }
-  
+
   rpcRecoYuv->clear();
-  
+
   TComMv        cMvSrchRngLT;
   TComMv        cMvSrchRngRB;
-  
+
   TComMv        cMvZero;
   TComMv        TempMv; //kolya
-  
+
   TComMv        cMv[2];
   TComMv        cMvBi[2];
   TComMv        cMvTemp[2][33];
-  
+
   Int           iNumPart    = pcCU->getNumPartInter();
   Int           iNumPredDir = pcCU->getSlice()->isInterP() ? 1 : 2;
-  
+
   TComMv        cMvPred[2][33];
-  
+
   TComMv        cMvPredBi[2][33];
   Int           aaiMvpIdxBi[2][33];
-  
+
   Int           aaiMvpIdx[2][33];
   Int           aaiMvpNum[2][33];
-  
+
   AMVPInfo aacAMVPInfo[2][33];
-  
+
   Int           iRefIdx[2]={0,0}; //If un-initialized, may cause SEGV in bi-directional prediction iterative stage.
   Int           iRefIdxBi[2];
-  
+
   UInt          uiPartAddr;
   Int           iRoiWidth, iRoiHeight;
-  
+
   UInt          uiMbBits[3] = {1, 1, 0};
-  
+
   UInt          uiLastMode = 0;
   Int           iRefStart, iRefEnd;
-  
+
   PartSize      ePartSize = pcCU->getPartitionSize( 0 );
 
@@ -4111,5 +4110,5 @@
     UInt          uiCostBi  =   MAX_UINT;
     UInt          uiCostTemp;
-    
+
     UInt          uiBits[3];
     UInt          uiBitsTemp;
@@ -4136,5 +4135,5 @@
 
     xGetBlkBits( ePartSize, pcCU->getSlice()->isInterP(), iPartIdx, uiLastMode, uiMbBits);
-    
+
     pcCU->getPartIndexAndSize( iPartIdx, uiPartAddr, iRoiWidth, iRoiHeight );
 
@@ -4142,5 +4141,5 @@
     pcCU->setVSPFlagSubParts( 0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr) );
 #endif
-    
+
 #if AMP_MRG
     Bool bTestNormalMC = true;
@@ -4153,72 +4152,77 @@
       bTestNormalMC = false;
     }
-    
+
     if (bTestNormalMC)
     {
 #endif
 
-    //  Uni-directional prediction
-    for ( Int iRefList = 0; iRefList < iNumPredDir; iRefList++ )
-    {
-      RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
-      
-      for ( Int iRefIdxTemp = 0; iRefIdxTemp < pcCU->getSlice()->getNumRefIdx(eRefPicList); iRefIdxTemp++ )
-      {
-        uiBitsTemp = uiMbBits[iRefList];
-        if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
-        {
-          uiBitsTemp += iRefIdxTemp+1;
-          if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
-        }
+      //  Uni-directional prediction
+      for ( Int iRefList = 0; iRefList < iNumPredDir; iRefList++ )
+      {
+        RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
+
+        for ( Int iRefIdxTemp = 0; iRefIdxTemp < pcCU->getSlice()->getNumRefIdx(eRefPicList); iRefIdxTemp++ )
+        {
+          uiBitsTemp = uiMbBits[iRefList];
+          if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
+          {
+            uiBitsTemp += iRefIdxTemp+1;
+            if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
+          }
 #if ZERO_MVD_EST
-        xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp, &uiZeroMvdDistTemp);
+          xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp, &uiZeroMvdDistTemp);
 #else
-        xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp);
-#endif
-        aaiMvpIdx[iRefList][iRefIdxTemp] = pcCU->getMVPIdx(eRefPicList, uiPartAddr);
-        aaiMvpNum[iRefList][iRefIdxTemp] = pcCU->getMVPNum(eRefPicList, uiPartAddr);
-        
-        if(pcCU->getSlice()->getMvdL1ZeroFlag() && iRefList==1 && biPDistTemp < bestBiPDist)
-        {
-          bestBiPDist = biPDistTemp;
-          bestBiPMvpL1 = aaiMvpIdx[iRefList][iRefIdxTemp];
-          bestBiPRefIdxL1 = iRefIdxTemp;
-        }
-
-        uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
+          xEstimateMvPredAMVP( pcCU, pcOrgYuv, iPartIdx, eRefPicList, iRefIdxTemp, cMvPred[iRefList][iRefIdxTemp], false, &biPDistTemp);
+#endif
+          aaiMvpIdx[iRefList][iRefIdxTemp] = pcCU->getMVPIdx(eRefPicList, uiPartAddr);
+          aaiMvpNum[iRefList][iRefIdxTemp] = pcCU->getMVPNum(eRefPicList, uiPartAddr);
+
+          if(pcCU->getSlice()->getMvdL1ZeroFlag() && iRefList==1 && biPDistTemp < bestBiPDist)
+          {
+            bestBiPDist = biPDistTemp;
+            bestBiPMvpL1 = aaiMvpIdx[iRefList][iRefIdxTemp];
+            bestBiPRefIdxL1 = iRefIdxTemp;
+          }
+
+          uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdx[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
 #if ZERO_MVD_EST
-        if ( iRefList == 0 || pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp ) < 0 )
-        {
-          uiZeroMvdBitsTemp = uiBitsTemp;
-          uiZeroMvdBitsTemp += 2; //zero mvd bits
-
-          m_pcRdCost->getMotionCost( 1, 0 );
-          uiZeroMvdCostTemp = uiZeroMvdDistTemp + m_pcRdCost->getCost(uiZeroMvdBitsTemp);
-
-          if (uiZeroMvdCostTemp < uiZeroMvdCost)
+          if ( iRefList == 0 || pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp ) < 0 )
           {
-            uiZeroMvdCost = uiZeroMvdCostTemp;
-            iZeroMvdDir = iRefList + 1;
-            aiZeroMvdRefIdx[iRefList] = iRefIdxTemp;
-            aiZeroMvdMvpIdx[iRefList] = aaiMvpIdx[iRefList][iRefIdxTemp];
-            auiZeroMvdBits[iRefList] = uiZeroMvdBitsTemp;
-          }          
-        }
-#endif
-        
+            uiZeroMvdBitsTemp = uiBitsTemp;
+            uiZeroMvdBitsTemp += 2; //zero mvd bits
+
+            m_pcRdCost->getMotionCost( 1, 0 );
+            uiZeroMvdCostTemp = uiZeroMvdDistTemp + m_pcRdCost->getCost(uiZeroMvdBitsTemp);
+
+            if (uiZeroMvdCostTemp < uiZeroMvdCost)
+            {
+              uiZeroMvdCost = uiZeroMvdCostTemp;
+              iZeroMvdDir = iRefList + 1;
+              aiZeroMvdRefIdx[iRefList] = iRefIdxTemp;
+              aiZeroMvdMvpIdx[iRefList] = aaiMvpIdx[iRefList][iRefIdxTemp];
+              auiZeroMvdBits[iRefList] = uiZeroMvdBitsTemp;
+            }          
+          }
+#endif
+
 #if GPB_SIMPLE_UNI
-        if ( iRefList == 1 )    // list 1
-        {
-          if ( pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp ) >= 0 )
+          if ( iRefList == 1 )    // list 1
           {
-            cMvTemp[1][iRefIdxTemp] = cMvTemp[0][pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp )];
-            uiCostTemp = uiCostTempL0[pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp )];
-            /*first subtract the bit-rate part of the cost of the other list*/
-            uiCostTemp -= m_pcRdCost->getCost( uiBitsTempL0[pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp )] );
-            /*correct the bit-rate part of the current ref*/
-            m_pcRdCost->setPredictor  ( cMvPred[iRefList][iRefIdxTemp] );
-            uiBitsTemp += m_pcRdCost->getBits( cMvTemp[1][iRefIdxTemp].getHor(), cMvTemp[1][iRefIdxTemp].getVer() );
-            /*calculate the correct cost*/
-            uiCostTemp += m_pcRdCost->getCost( uiBitsTemp );
+            if ( pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp ) >= 0 )
+            {
+              cMvTemp[1][iRefIdxTemp] = cMvTemp[0][pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp )];
+              uiCostTemp = uiCostTempL0[pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp )];
+              /*first subtract the bit-rate part of the cost of the other list*/
+              uiCostTemp -= m_pcRdCost->getCost( uiBitsTempL0[pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp )] );
+              /*correct the bit-rate part of the current ref*/
+              m_pcRdCost->setPredictor  ( cMvPred[iRefList][iRefIdxTemp] );
+              uiBitsTemp += m_pcRdCost->getBits( cMvTemp[1][iRefIdxTemp].getHor(), cMvTemp[1][iRefIdxTemp].getVer() );
+              /*calculate the correct cost*/
+              uiCostTemp += m_pcRdCost->getCost( uiBitsTemp );
+            }
+            else
+            {
+              xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
+            }
           }
           else
@@ -4226,240 +4230,235 @@
             xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
           }
+#else
+          xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
+#endif
+          xCopyAMVPInfo(pcCU->getCUMvField(eRefPicList)->getAMVPInfo(), &aacAMVPInfo[iRefList][iRefIdxTemp]); // must always be done ( also when AMVP_MODE = AM_NONE )
+          xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
+
+          if ( iRefList == 0 )
+          {
+            uiCostTempL0[iRefIdxTemp] = uiCostTemp;
+            uiBitsTempL0[iRefIdxTemp] = uiBitsTemp;
+          }
+          if ( uiCostTemp < uiCost[iRefList] )
+          {
+            uiCost[iRefList] = uiCostTemp;
+            uiBits[iRefList] = uiBitsTemp; // storing for bi-prediction
+
+            // set motion
+            cMv[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
+            iRefIdx[iRefList] = iRefIdxTemp;
+          }
+
+          if ( iRefList == 1 && uiCostTemp < costValidList1 && pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp ) < 0 )
+          {
+            costValidList1 = uiCostTemp;
+            bitsValidList1 = uiBitsTemp;
+
+            // set motion
+            mvValidList1     = cMvTemp[iRefList][iRefIdxTemp];
+            refIdxValidList1 = iRefIdxTemp;
+          }
+        }
+      }
+      //  Bi-directional prediction
+      if ( (pcCU->getSlice()->isInterB()) && (pcCU->isBipredRestriction(iPartIdx) == false) )
+      {
+
+        cMvBi[0] = cMv[0];            cMvBi[1] = cMv[1];
+        iRefIdxBi[0] = iRefIdx[0];    iRefIdxBi[1] = iRefIdx[1];
+
+        ::memcpy(cMvPredBi, cMvPred, sizeof(cMvPred));
+        ::memcpy(aaiMvpIdxBi, aaiMvpIdx, sizeof(aaiMvpIdx));
+
+        UInt uiMotBits[2];
+
+        if(pcCU->getSlice()->getMvdL1ZeroFlag())
+        {
+          xCopyAMVPInfo(&aacAMVPInfo[1][bestBiPRefIdxL1], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
+          pcCU->setMVPIdxSubParts( bestBiPMvpL1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          aaiMvpIdxBi[1][bestBiPRefIdxL1] = bestBiPMvpL1;
+          cMvPredBi[1][bestBiPRefIdxL1]   = pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo()->m_acMvCand[bestBiPMvpL1];
+
+          cMvBi[1] = cMvPredBi[1][bestBiPRefIdxL1];
+          iRefIdxBi[1] = bestBiPRefIdxL1;
+          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMv( cMvBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
+          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllRefIdx( iRefIdxBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
+          TComYuv* pcYuvPred = &m_acYuvPred[1];
+          motionCompensation( pcCU, pcYuvPred, REF_PIC_LIST_1, iPartIdx );
+
+          uiMotBits[0] = uiBits[0] - uiMbBits[0];
+          uiMotBits[1] = uiMbBits[1];
+
+          if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1) > 1 )
+          {
+            uiMotBits[1] += bestBiPRefIdxL1+1;
+            if ( bestBiPRefIdxL1 == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1 ) uiMotBits[1]--;
+          }
+
+          uiMotBits[1] += m_auiMVPIdxCost[aaiMvpIdxBi[1][bestBiPRefIdxL1]][AMVP_MAX_NUM_CANDS];
+
+          uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
+
+          cMvTemp[1][bestBiPRefIdxL1] = cMvBi[1];
         }
         else
         {
-          xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
-        }
-#else
-        xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPred[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp );
-#endif
-        xCopyAMVPInfo(pcCU->getCUMvField(eRefPicList)->getAMVPInfo(), &aacAMVPInfo[iRefList][iRefIdxTemp]); // must always be done ( also when AMVP_MODE = AM_NONE )
-        xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPred[iRefList][iRefIdxTemp], aaiMvpIdx[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
-
-        if ( iRefList == 0 )
-        {
-          uiCostTempL0[iRefIdxTemp] = uiCostTemp;
-          uiBitsTempL0[iRefIdxTemp] = uiBitsTemp;
-        }
-        if ( uiCostTemp < uiCost[iRefList] )
-        {
-          uiCost[iRefList] = uiCostTemp;
-          uiBits[iRefList] = uiBitsTemp; // storing for bi-prediction
-
-          // set motion
-          cMv[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
-          iRefIdx[iRefList] = iRefIdxTemp;
-        }
-
-        if ( iRefList == 1 && uiCostTemp < costValidList1 && pcCU->getSlice()->getList1IdxToList0Idx( iRefIdxTemp ) < 0 )
-        {
-          costValidList1 = uiCostTemp;
-          bitsValidList1 = uiBitsTemp;
-
-          // set motion
-          mvValidList1     = cMvTemp[iRefList][iRefIdxTemp];
-          refIdxValidList1 = iRefIdxTemp;
-        }
-      }
-    }
-    //  Bi-directional prediction
-    if ( (pcCU->getSlice()->isInterB()) && (pcCU->isBipredRestriction(iPartIdx) == false) )
-    {
-      
-      cMvBi[0] = cMv[0];            cMvBi[1] = cMv[1];
-      iRefIdxBi[0] = iRefIdx[0];    iRefIdxBi[1] = iRefIdx[1];
-      
-      ::memcpy(cMvPredBi, cMvPred, sizeof(cMvPred));
-      ::memcpy(aaiMvpIdxBi, aaiMvpIdx, sizeof(aaiMvpIdx));
-      
-      UInt uiMotBits[2];
-
-      if(pcCU->getSlice()->getMvdL1ZeroFlag())
-      {
-        xCopyAMVPInfo(&aacAMVPInfo[1][bestBiPRefIdxL1], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
-        pcCU->setMVPIdxSubParts( bestBiPMvpL1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        aaiMvpIdxBi[1][bestBiPRefIdxL1] = bestBiPMvpL1;
-        cMvPredBi[1][bestBiPRefIdxL1]   = pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo()->m_acMvCand[bestBiPMvpL1];
-
-        cMvBi[1] = cMvPredBi[1][bestBiPRefIdxL1];
-        iRefIdxBi[1] = bestBiPRefIdxL1;
-        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMv( cMvBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
-        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllRefIdx( iRefIdxBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
-        TComYuv* pcYuvPred = &m_acYuvPred[1];
-        motionCompensation( pcCU, pcYuvPred, REF_PIC_LIST_1, iPartIdx );
-
-        uiMotBits[0] = uiBits[0] - uiMbBits[0];
-        uiMotBits[1] = uiMbBits[1];
-
-        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1) > 1 )
-        {
-          uiMotBits[1] += bestBiPRefIdxL1+1;
-          if ( bestBiPRefIdxL1 == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1 ) uiMotBits[1]--;
-        }
-
-        uiMotBits[1] += m_auiMVPIdxCost[aaiMvpIdxBi[1][bestBiPRefIdxL1]][AMVP_MAX_NUM_CANDS];
-
-        uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
-
-        cMvTemp[1][bestBiPRefIdxL1] = cMvBi[1];
-      }
-      else
-      {
-        uiMotBits[0] = uiBits[0] - uiMbBits[0];
-        uiMotBits[1] = uiBits[1] - uiMbBits[1];
-        uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
-      }
-
-      // 4-times iteration (default)
-      Int iNumIter = 4;
-      
-      // fast encoder setting: only one iteration
-      if ( m_pcEncCfg->getUseFastEnc() || pcCU->getSlice()->getMvdL1ZeroFlag())
-      {
-        iNumIter = 1;
-      }
-      
-      for ( Int iIter = 0; iIter < iNumIter; iIter++ )
-      {
-        
-        Int         iRefList    = iIter % 2;
-        if ( m_pcEncCfg->getUseFastEnc() )
-        {
-          if( uiCost[0] <= uiCost[1] )
+          uiMotBits[0] = uiBits[0] - uiMbBits[0];
+          uiMotBits[1] = uiBits[1] - uiMbBits[1];
+          uiBits[2] = uiMbBits[2] + uiMotBits[0] + uiMotBits[1];
+        }
+
+        // 4-times iteration (default)
+        Int iNumIter = 4;
+
+        // fast encoder setting: only one iteration
+        if ( m_pcEncCfg->getUseFastEnc() || pcCU->getSlice()->getMvdL1ZeroFlag())
+        {
+          iNumIter = 1;
+        }
+
+        for ( Int iIter = 0; iIter < iNumIter; iIter++ )
+        {
+
+          Int         iRefList    = iIter % 2;
+          if ( m_pcEncCfg->getUseFastEnc() )
           {
-            iRefList = 1;
+            if( uiCost[0] <= uiCost[1] )
+            {
+              iRefList = 1;
+            }
+            else
+            {
+              iRefList = 0;
+            }
           }
-          else
+          else if ( iIter == 0 )
           {
             iRefList = 0;
           }
-        }
-        else if ( iIter == 0 )
-        {
-          iRefList = 0;
-        }
-        if ( iIter == 0 && !pcCU->getSlice()->getMvdL1ZeroFlag())
-        {
-          pcCU->getCUMvField(RefPicList(1-iRefList))->setAllMv( cMv[1-iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
-          pcCU->getCUMvField(RefPicList(1-iRefList))->setAllRefIdx( iRefIdx[1-iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
-          TComYuv*  pcYuvPred = &m_acYuvPred[1-iRefList];
-          motionCompensation ( pcCU, pcYuvPred, RefPicList(1-iRefList), iPartIdx );
-        }
-        RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
-
-        if(pcCU->getSlice()->getMvdL1ZeroFlag())
-        {
-          iRefList = 0;
-          eRefPicList = REF_PIC_LIST_0;
-        }
-
-        Bool bChanged = false;
-        
-        iRefStart = 0;
-        iRefEnd   = pcCU->getSlice()->getNumRefIdx(eRefPicList)-1;
-        
-        for ( Int iRefIdxTemp = iRefStart; iRefIdxTemp <= iRefEnd; iRefIdxTemp++ )
-        {
-          uiBitsTemp = uiMbBits[2] + uiMotBits[1-iRefList];
-          if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
+          if ( iIter == 0 && !pcCU->getSlice()->getMvdL1ZeroFlag())
           {
-            uiBitsTemp += iRefIdxTemp+1;
-            if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
+            pcCU->getCUMvField(RefPicList(1-iRefList))->setAllMv( cMv[1-iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
+            pcCU->getCUMvField(RefPicList(1-iRefList))->setAllRefIdx( iRefIdx[1-iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
+            TComYuv*  pcYuvPred = &m_acYuvPred[1-iRefList];
+            motionCompensation ( pcCU, pcYuvPred, RefPicList(1-iRefList), iPartIdx );
           }
-          uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdxBi[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
-          // call ME
-          xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPredBi[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp, true );
-          xCopyAMVPInfo(&aacAMVPInfo[iRefList][iRefIdxTemp], pcCU->getCUMvField(eRefPicList)->getAMVPInfo());
-          xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPredBi[iRefList][iRefIdxTemp], aaiMvpIdxBi[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
-
-          if ( uiCostTemp < uiCostBi )
+          RefPicList  eRefPicList = ( iRefList ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
+
+          if(pcCU->getSlice()->getMvdL1ZeroFlag())
           {
-            bChanged = true;
-            
-            cMvBi[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
-            iRefIdxBi[iRefList] = iRefIdxTemp;
-            
-            uiCostBi            = uiCostTemp;
-            uiMotBits[iRefList] = uiBitsTemp - uiMbBits[2] - uiMotBits[1-iRefList];
-            uiBits[2]           = uiBitsTemp;
-            
-            if(iNumIter!=1)
+            iRefList = 0;
+            eRefPicList = REF_PIC_LIST_0;
+          }
+
+          Bool bChanged = false;
+
+          iRefStart = 0;
+          iRefEnd   = pcCU->getSlice()->getNumRefIdx(eRefPicList)-1;
+
+          for ( Int iRefIdxTemp = iRefStart; iRefIdxTemp <= iRefEnd; iRefIdxTemp++ )
+          {
+            uiBitsTemp = uiMbBits[2] + uiMotBits[1-iRefList];
+            if ( pcCU->getSlice()->getNumRefIdx(eRefPicList) > 1 )
             {
-              //  Set motion
-              pcCU->getCUMvField( eRefPicList )->setAllMv( cMvBi[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
-              pcCU->getCUMvField( eRefPicList )->setAllRefIdx( iRefIdxBi[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
-
-              TComYuv* pcYuvPred = &m_acYuvPred[iRefList];
-              motionCompensation( pcCU, pcYuvPred, eRefPicList, iPartIdx );
+              uiBitsTemp += iRefIdxTemp+1;
+              if ( iRefIdxTemp == pcCU->getSlice()->getNumRefIdx(eRefPicList)-1 ) uiBitsTemp--;
+            }
+            uiBitsTemp += m_auiMVPIdxCost[aaiMvpIdxBi[iRefList][iRefIdxTemp]][AMVP_MAX_NUM_CANDS];
+            // call ME
+            xMotionEstimation ( pcCU, pcOrgYuv, iPartIdx, eRefPicList, &cMvPredBi[iRefList][iRefIdxTemp], iRefIdxTemp, cMvTemp[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp, true );
+            xCopyAMVPInfo(&aacAMVPInfo[iRefList][iRefIdxTemp], pcCU->getCUMvField(eRefPicList)->getAMVPInfo());
+            xCheckBestMVP(pcCU, eRefPicList, cMvTemp[iRefList][iRefIdxTemp], cMvPredBi[iRefList][iRefIdxTemp], aaiMvpIdxBi[iRefList][iRefIdxTemp], uiBitsTemp, uiCostTemp);
+
+            if ( uiCostTemp < uiCostBi )
+            {
+              bChanged = true;
+
+              cMvBi[iRefList]     = cMvTemp[iRefList][iRefIdxTemp];
+              iRefIdxBi[iRefList] = iRefIdxTemp;
+
+              uiCostBi            = uiCostTemp;
+              uiMotBits[iRefList] = uiBitsTemp - uiMbBits[2] - uiMotBits[1-iRefList];
+              uiBits[2]           = uiBitsTemp;
+
+              if(iNumIter!=1)
+              {
+                //  Set motion
+                pcCU->getCUMvField( eRefPicList )->setAllMv( cMvBi[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
+                pcCU->getCUMvField( eRefPicList )->setAllRefIdx( iRefIdxBi[iRefList], ePartSize, uiPartAddr, 0, iPartIdx );
+
+                TComYuv* pcYuvPred = &m_acYuvPred[iRefList];
+                motionCompensation( pcCU, pcYuvPred, eRefPicList, iPartIdx );
+              }
+            }
+          } // for loop-iRefIdxTemp
+
+          if ( !bChanged )
+          {
+            if ( uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1] )
+            {
+              xCopyAMVPInfo(&aacAMVPInfo[0][iRefIdxBi[0]], pcCU->getCUMvField(REF_PIC_LIST_0)->getAMVPInfo());
+              xCheckBestMVP(pcCU, REF_PIC_LIST_0, cMvBi[0], cMvPredBi[0][iRefIdxBi[0]], aaiMvpIdxBi[0][iRefIdxBi[0]], uiBits[2], uiCostBi);
+              if(!pcCU->getSlice()->getMvdL1ZeroFlag())
+              {
+                xCopyAMVPInfo(&aacAMVPInfo[1][iRefIdxBi[1]], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
+                xCheckBestMVP(pcCU, REF_PIC_LIST_1, cMvBi[1], cMvPredBi[1][iRefIdxBi[1]], aaiMvpIdxBi[1][iRefIdxBi[1]], uiBits[2], uiCostBi);
+              }
+            }
+            break;
+          }
+        } // for loop-iter
+      } // if (B_SLICE)
+#if ZERO_MVD_EST
+      if ( (pcCU->getSlice()->isInterB()) && (pcCU->isBipredRestriction(iPartIdx) == false) )
+      {
+        m_pcRdCost->getMotionCost( 1, 0 );
+
+        for ( Int iL0RefIdxTemp = 0; iL0RefIdxTemp <= pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0)-1; iL0RefIdxTemp++ )
+          for ( Int iL1RefIdxTemp = 0; iL1RefIdxTemp <= pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1; iL1RefIdxTemp++ )
+          {
+            UInt uiRefIdxBitsTemp = 0;
+            if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0) > 1 )
+            {
+              uiRefIdxBitsTemp += iL0RefIdxTemp+1;
+              if ( iL0RefIdxTemp == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0)-1 ) uiRefIdxBitsTemp--;
+            }
+            if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1) > 1 )
+            {
+              uiRefIdxBitsTemp += iL1RefIdxTemp+1;
+              if ( iL1RefIdxTemp == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1 ) uiRefIdxBitsTemp--;
+            }
+
+            Int iL0MVPIdx = 0;
+            Int iL1MVPIdx = 0;
+
+            for (iL0MVPIdx = 0; iL0MVPIdx < aaiMvpNum[0][iL0RefIdxTemp]; iL0MVPIdx++)
+            {
+              for (iL1MVPIdx = 0; iL1MVPIdx < aaiMvpNum[1][iL1RefIdxTemp]; iL1MVPIdx++)
+              {
+                uiZeroMvdBitsTemp = uiRefIdxBitsTemp;
+                uiZeroMvdBitsTemp += uiMbBits[2];
+                uiZeroMvdBitsTemp += m_auiMVPIdxCost[iL0MVPIdx][aaiMvpNum[0][iL0RefIdxTemp]] + m_auiMVPIdxCost[iL1MVPIdx][aaiMvpNum[1][iL1RefIdxTemp]];
+                uiZeroMvdBitsTemp += 4; //zero mvd for both directions
+                pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( aacAMVPInfo[0][iL0RefIdxTemp].m_acMvCand[iL0MVPIdx], iL0RefIdxTemp, ePartSize, uiPartAddr, iPartIdx, 0 );
+                pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( aacAMVPInfo[1][iL1RefIdxTemp].m_acMvCand[iL1MVPIdx], iL1RefIdxTemp, ePartSize, uiPartAddr, iPartIdx, 0 );
+
+                xGetInterPredictionError( pcCU, pcOrgYuv, iPartIdx, uiZeroMvdDistTemp, m_pcEncCfg->getUseHADME() );
+                uiZeroMvdCostTemp = uiZeroMvdDistTemp + m_pcRdCost->getCost( uiZeroMvdBitsTemp );
+                if (uiZeroMvdCostTemp < uiZeroMvdCost)
+                {
+                  uiZeroMvdCost = uiZeroMvdCostTemp;
+                  iZeroMvdDir = 3;
+                  aiZeroMvdMvpIdx[0] = iL0MVPIdx;
+                  aiZeroMvdMvpIdx[1] = iL1MVPIdx;
+                  aiZeroMvdRefIdx[0] = iL0RefIdxTemp;
+                  aiZeroMvdRefIdx[1] = iL1RefIdxTemp;
+                  auiZeroMvdBits[2] = uiZeroMvdBitsTemp;
+                }
+              }
             }
           }
-        } // for loop-iRefIdxTemp
-        
-        if ( !bChanged )
-        {
-          if ( uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1] )
-          {
-            xCopyAMVPInfo(&aacAMVPInfo[0][iRefIdxBi[0]], pcCU->getCUMvField(REF_PIC_LIST_0)->getAMVPInfo());
-            xCheckBestMVP(pcCU, REF_PIC_LIST_0, cMvBi[0], cMvPredBi[0][iRefIdxBi[0]], aaiMvpIdxBi[0][iRefIdxBi[0]], uiBits[2], uiCostBi);
-            if(!pcCU->getSlice()->getMvdL1ZeroFlag())
-            {
-              xCopyAMVPInfo(&aacAMVPInfo[1][iRefIdxBi[1]], pcCU->getCUMvField(REF_PIC_LIST_1)->getAMVPInfo());
-              xCheckBestMVP(pcCU, REF_PIC_LIST_1, cMvBi[1], cMvPredBi[1][iRefIdxBi[1]], aaiMvpIdxBi[1][iRefIdxBi[1]], uiBits[2], uiCostBi);
-            }
-          }
-          break;
-        }
-      } // for loop-iter
-    } // if (B_SLICE)
-#if ZERO_MVD_EST
-    if ( (pcCU->getSlice()->isInterB()) && (pcCU->isBipredRestriction(iPartIdx) == false) )
-    {
-      m_pcRdCost->getMotionCost( 1, 0 );
-
-      for ( Int iL0RefIdxTemp = 0; iL0RefIdxTemp <= pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0)-1; iL0RefIdxTemp++ )
-      for ( Int iL1RefIdxTemp = 0; iL1RefIdxTemp <= pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1; iL1RefIdxTemp++ )
-      {
-        UInt uiRefIdxBitsTemp = 0;
-        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0) > 1 )
-        {
-          uiRefIdxBitsTemp += iL0RefIdxTemp+1;
-          if ( iL0RefIdxTemp == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_0)-1 ) uiRefIdxBitsTemp--;
-        }
-        if ( pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1) > 1 )
-        {
-          uiRefIdxBitsTemp += iL1RefIdxTemp+1;
-          if ( iL1RefIdxTemp == pcCU->getSlice()->getNumRefIdx(REF_PIC_LIST_1)-1 ) uiRefIdxBitsTemp--;
-        }
-
-        Int iL0MVPIdx = 0;
-        Int iL1MVPIdx = 0;
-
-        for (iL0MVPIdx = 0; iL0MVPIdx < aaiMvpNum[0][iL0RefIdxTemp]; iL0MVPIdx++)
-        {
-          for (iL1MVPIdx = 0; iL1MVPIdx < aaiMvpNum[1][iL1RefIdxTemp]; iL1MVPIdx++)
-          {
-            uiZeroMvdBitsTemp = uiRefIdxBitsTemp;
-            uiZeroMvdBitsTemp += uiMbBits[2];
-            uiZeroMvdBitsTemp += m_auiMVPIdxCost[iL0MVPIdx][aaiMvpNum[0][iL0RefIdxTemp]] + m_auiMVPIdxCost[iL1MVPIdx][aaiMvpNum[1][iL1RefIdxTemp]];
-            uiZeroMvdBitsTemp += 4; //zero mvd for both directions
-            pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( aacAMVPInfo[0][iL0RefIdxTemp].m_acMvCand[iL0MVPIdx], iL0RefIdxTemp, ePartSize, uiPartAddr, iPartIdx, 0 );
-            pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( aacAMVPInfo[1][iL1RefIdxTemp].m_acMvCand[iL1MVPIdx], iL1RefIdxTemp, ePartSize, uiPartAddr, iPartIdx, 0 );
-  
-            xGetInterPredictionError( pcCU, pcOrgYuv, iPartIdx, uiZeroMvdDistTemp, m_pcEncCfg->getUseHADME() );
-            uiZeroMvdCostTemp = uiZeroMvdDistTemp + m_pcRdCost->getCost( uiZeroMvdBitsTemp );
-            if (uiZeroMvdCostTemp < uiZeroMvdCost)
-            {
-              uiZeroMvdCost = uiZeroMvdCostTemp;
-              iZeroMvdDir = 3;
-              aiZeroMvdMvpIdx[0] = iL0MVPIdx;
-              aiZeroMvdMvpIdx[1] = iL1MVPIdx;
-              aiZeroMvdRefIdx[0] = iL0RefIdxTemp;
-              aiZeroMvdRefIdx[1] = iL1RefIdxTemp;
-              auiZeroMvdBits[2] = uiZeroMvdBitsTemp;
-            }
-          }
-        }
-      }
-    }
+      }
 #endif
 
@@ -4477,5 +4476,5 @@
     pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
     pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-    
+
     UInt uiMEBits = 0;
     // Set Motion Field_
@@ -4489,103 +4488,103 @@
 #endif
 #if ZERO_MVD_EST
-    if (uiZeroMvdCost <= uiCostBi && uiZeroMvdCost <= uiCost[0] && uiZeroMvdCost <= uiCost[1])
-    {
-      if (iZeroMvdDir == 3)
-      {
-        uiLastMode = 2;
-
-        pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( aacAMVPInfo[0][aiZeroMvdRefIdx[0]].m_acMvCand[aiZeroMvdMvpIdx[0]], aiZeroMvdRefIdx[0], ePartSize, uiPartAddr, iPartIdx, 0 );
-        pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( aacAMVPInfo[1][aiZeroMvdRefIdx[1]].m_acMvCand[aiZeroMvdMvpIdx[1]], aiZeroMvdRefIdx[1], ePartSize, uiPartAddr, iPartIdx, 0 );
-  
-        pcCU->setInterDirSubParts( 3, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
-        
-        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[0], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        pcCU->setMVPNumSubParts( aaiMvpNum[0][aiZeroMvdRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[1], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        pcCU->setMVPNumSubParts( aaiMvpNum[1][aiZeroMvdRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        uiMEBits = auiZeroMvdBits[2];
-      }
-      else if (iZeroMvdDir == 1)
-      {        
-        uiLastMode = 0;
-
-        pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( aacAMVPInfo[0][aiZeroMvdRefIdx[0]].m_acMvCand[aiZeroMvdMvpIdx[0]], aiZeroMvdRefIdx[0], ePartSize, uiPartAddr, iPartIdx, 0 );
-
-        pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
-        
-        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[0], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        pcCU->setMVPNumSubParts( aaiMvpNum[0][aiZeroMvdRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        uiMEBits = auiZeroMvdBits[0];
-      }
-      else if (iZeroMvdDir == 2)
-      {
-        uiLastMode = 1;
-
-        pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( aacAMVPInfo[1][aiZeroMvdRefIdx[1]].m_acMvCand[aiZeroMvdMvpIdx[1]], aiZeroMvdRefIdx[1], ePartSize, uiPartAddr, iPartIdx, 0 );
-
-        pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
-        
-        pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[1], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        pcCU->setMVPNumSubParts( aaiMvpNum[1][aiZeroMvdRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        uiMEBits = auiZeroMvdBits[1];
+      if (uiZeroMvdCost <= uiCostBi && uiZeroMvdCost <= uiCost[0] && uiZeroMvdCost <= uiCost[1])
+      {
+        if (iZeroMvdDir == 3)
+        {
+          uiLastMode = 2;
+
+          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( aacAMVPInfo[0][aiZeroMvdRefIdx[0]].m_acMvCand[aiZeroMvdMvpIdx[0]], aiZeroMvdRefIdx[0], ePartSize, uiPartAddr, iPartIdx, 0 );
+          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( aacAMVPInfo[1][aiZeroMvdRefIdx[1]].m_acMvCand[aiZeroMvdMvpIdx[1]], aiZeroMvdRefIdx[1], ePartSize, uiPartAddr, iPartIdx, 0 );
+
+          pcCU->setInterDirSubParts( 3, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
+
+          pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[0], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( aaiMvpNum[0][aiZeroMvdRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[1], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( aaiMvpNum[1][aiZeroMvdRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          uiMEBits = auiZeroMvdBits[2];
+        }
+        else if (iZeroMvdDir == 1)
+        {        
+          uiLastMode = 0;
+
+          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvField( aacAMVPInfo[0][aiZeroMvdRefIdx[0]].m_acMvCand[aiZeroMvdMvpIdx[0]], aiZeroMvdRefIdx[0], ePartSize, uiPartAddr, iPartIdx, 0 );
+
+          pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
+
+          pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[0], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( aaiMvpNum[0][aiZeroMvdRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          uiMEBits = auiZeroMvdBits[0];
+        }
+        else if (iZeroMvdDir == 2)
+        {
+          uiLastMode = 1;
+
+          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvField( aacAMVPInfo[1][aiZeroMvdRefIdx[1]].m_acMvCand[aiZeroMvdMvpIdx[1]], aiZeroMvdRefIdx[1], ePartSize, uiPartAddr, iPartIdx, 0 );
+
+          pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
+
+          pcCU->setMVPIdxSubParts( aiZeroMvdMvpIdx[1], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( aaiMvpNum[1][aiZeroMvdRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          uiMEBits = auiZeroMvdBits[1];
+        }
+        else
+        {
+          assert(0);
+        }
       }
       else
-      {
-        assert(0);
-      }
-    }
-    else
-#endif
-    if ( uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1])
-    {
-      uiLastMode = 2;
-            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv( cMvBi[0], ePartSize, uiPartAddr, 0, iPartIdx );
-            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdxBi[0], ePartSize, uiPartAddr, 0, iPartIdx );
-            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv( cMvBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
-            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdxBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
-      
-        TempMv = cMvBi[0] - cMvPredBi[0][iRefIdxBi[0]];
-            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
-      
-        TempMv = cMvBi[1] - cMvPredBi[1][iRefIdxBi[1]];
-            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
-      
-      pcCU->setInterDirSubParts( 3, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
-      
-      pcCU->setMVPIdxSubParts( aaiMvpIdxBi[0][iRefIdxBi[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-      pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdxBi[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-      pcCU->setMVPIdxSubParts( aaiMvpIdxBi[1][iRefIdxBi[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-      pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdxBi[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-
-      uiMEBits = uiBits[2];
-    }
-    else if ( uiCost[0] <= uiCost[1] )
-    {
-      uiLastMode = 0;
+#endif
+        if ( uiCostBi <= uiCost[0] && uiCostBi <= uiCost[1])
+        {
+          uiLastMode = 2;
+          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv( cMvBi[0], ePartSize, uiPartAddr, 0, iPartIdx );
+          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdxBi[0], ePartSize, uiPartAddr, 0, iPartIdx );
+          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv( cMvBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
+          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdxBi[1], ePartSize, uiPartAddr, 0, iPartIdx );
+
+          TempMv = cMvBi[0] - cMvPredBi[0][iRefIdxBi[0]];
+          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
+
+          TempMv = cMvBi[1] - cMvPredBi[1][iRefIdxBi[1]];
+          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
+
+          pcCU->setInterDirSubParts( 3, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
+
+          pcCU->setMVPIdxSubParts( aaiMvpIdxBi[0][iRefIdxBi[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdxBi[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPIdxSubParts( aaiMvpIdxBi[1][iRefIdxBi[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdxBi[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+
+          uiMEBits = uiBits[2];
+        }
+        else if ( uiCost[0] <= uiCost[1] )
+        {
+          uiLastMode = 0;
           pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMv( cMv[0], ePartSize, uiPartAddr, 0, iPartIdx );
           pcCU->getCUMvField(REF_PIC_LIST_0)->setAllRefIdx( iRefIdx[0], ePartSize, uiPartAddr, 0, iPartIdx );
-        TempMv = cMv[0] - cMvPred[0][iRefIdx[0]];
-            pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
-      pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
-      
-      pcCU->setMVPIdxSubParts( aaiMvpIdx[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-      pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-
-      uiMEBits = uiBits[0];
-    }
-    else
-    {
-      uiLastMode = 1;
+          TempMv = cMv[0] - cMvPred[0][iRefIdx[0]];
+          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
+          pcCU->setInterDirSubParts( 1, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
+
+          pcCU->setMVPIdxSubParts( aaiMvpIdx[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( aaiMvpNum[0][iRefIdx[0]], REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+
+          uiMEBits = uiBits[0];
+        }
+        else
+        {
+          uiLastMode = 1;
           pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMv( cMv[1], ePartSize, uiPartAddr, 0, iPartIdx );
           pcCU->getCUMvField(REF_PIC_LIST_1)->setAllRefIdx( iRefIdx[1], ePartSize, uiPartAddr, 0, iPartIdx );
-        TempMv = cMv[1] - cMvPred[1][iRefIdx[1]];
-            pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
-      pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
-      
-      pcCU->setMVPIdxSubParts( aaiMvpIdx[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-      pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-
-      uiMEBits = uiBits[1];
-    }
+          TempMv = cMv[1] - cMvPred[1][iRefIdx[1]];
+          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( TempMv,                 ePartSize, uiPartAddr, 0, iPartIdx );
+          pcCU->setInterDirSubParts( 2, uiPartAddr, iPartIdx, pcCU->getDepth(0) );
+
+          pcCU->setMVPIdxSubParts( aaiMvpIdx[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( aaiMvpNum[1][iRefIdx[1]], REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+
+          uiMEBits = uiBits[1];
+        }
 #if AMP_MRG
     } // end if bTestNormalMC
@@ -4650,12 +4649,12 @@
       xMergeEstimation( pcCU, pcOrgYuv, iPartIdx, uiMRGInterDir, cMRGMvField, uiMRGIndex, uiMRGCost, cMvFieldNeighbours, uhInterDirNeighbours
 #if H_3D_VSP
-                      , vspFlag
-                      , inheritedVSPDisInfo
+        , vspFlag
+        , inheritedVSPDisInfo
 #endif
 #if H_3D_SPIVMP
-                      , bSPIVMPFlag, pcMvFieldSP, puhInterDirSP
+        , bSPIVMPFlag, pcMvFieldSP, puhInterDirSP
 #endif 
-                      , numValidMergeCand
-                      );
+        , numValidMergeCand
+        );
       if ( uiMRGCost < uiMECost )
       {
@@ -4670,12 +4669,12 @@
         if( uiMRGIndex == pcCU->getUseDDDCandIdx() )
         {
-            assert( vspFlag[uiMRGIndex]     == 0 );
-            assert( bSPIVMPFlag[uiMRGIndex] == 0 );
-            pcCU->setUseDDD( true, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
-            pcCU->setDDDepthSubParts( pcCU->getDDTmpDepth(), uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
+          assert( vspFlag[uiMRGIndex]     == 0 );
+          assert( bSPIVMPFlag[uiMRGIndex] == 0 );
+          pcCU->setUseDDD( true, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
+          pcCU->setDDDepthSubParts( pcCU->getDDTmpDepth(), uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
         }
         else
         {
-            pcCU->setUseDDD( false, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
+          pcCU->setUseDDD( false, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
         }
 #endif
@@ -4705,46 +4704,51 @@
 #if NTT_STORE_SPDV_VSP_G0148
 #if H_3D_DBBP
-        if ( vspFlag[uiMRGIndex] && !pcCU->getDBBPFlag(uiPartAddr) )
+          if ( vspFlag[uiMRGIndex] && !pcCU->getDBBPFlag(uiPartAddr) )
 #else
-        if ( vspFlag[uiMRGIndex] )
-#endif
-        {
-          UInt partAddrTemp;
-          Int vspSize;
-          Int width, height;
-          pcCU->getPartIndexAndSize( iPartIdx, partAddrTemp, width, height ); // true or pcCU->getTotalNumPart()==256
-          if( uiMRGInterDir & 0x01 )
+          if ( vspFlag[uiMRGIndex] )
+#endif
           {
-            pcCU->setMvFieldPUForVSP( pcCU, partAddrTemp, width, height, REF_PIC_LIST_0, cMRGMvField[0].getRefIdx(), vspSize );
-            pcCU->setVSPFlag( partAddrTemp, vspSize );
+            UInt partAddrTemp;
+            Int vspSize;
+            Int width, height;
+            pcCU->getPartIndexAndSize( iPartIdx, partAddrTemp, width, height ); // true or pcCU->getTotalNumPart()==256
+            if( uiMRGInterDir & 0x01 )
+            {
+              pcCU->setMvFieldPUForVSP( pcCU, partAddrTemp, width, height, REF_PIC_LIST_0, cMRGMvField[0].getRefIdx(), vspSize );
+              pcCU->setVSPFlag( partAddrTemp, vspSize );
+            }
+            else
+            {
+              pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMRGMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
+            }
+            if( uiMRGInterDir & 0x02 )
+            {
+              pcCU->setMvFieldPUForVSP( pcCU, partAddrTemp, width, height, REF_PIC_LIST_1, cMRGMvField[1].getRefIdx(), vspSize );
+              pcCU->setVSPFlag( partAddrTemp, vspSize );
+            }
+            else
+            {
+              pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMRGMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
+            }
+            pcCU->setInterDirSubParts  ( uiMRGInterDir, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
           }
           else
+#if H_3D_FIX_G0148_BRACE
           {
-            pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMRGMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
-          }
-          if( uiMRGInterDir & 0x02 )
-          {
-            pcCU->setMvFieldPUForVSP( pcCU, partAddrTemp, width, height, REF_PIC_LIST_1, cMRGMvField[1].getRefIdx(), vspSize );
-            pcCU->setVSPFlag( partAddrTemp, vspSize );
-          }
-          else
-          {
-            pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMRGMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
-          }
-          pcCU->setInterDirSubParts  ( uiMRGInterDir, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
-        }
-        else
-#endif
-          pcCU->setInterDirSubParts  ( uiMRGInterDir, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
+#endif
+#endif
+            pcCU->setInterDirSubParts  ( uiMRGInterDir, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
             pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMRGMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
             pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMRGMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
-
-        pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, 0, iPartIdx );
-        pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, 0, iPartIdx );
-
-        pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
-        pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+#if H_3D_FIX_G0148_BRACE
+          }
+#endif
+          pcCU->getCUMvField(REF_PIC_LIST_0)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, 0, iPartIdx );
+          pcCU->getCUMvField(REF_PIC_LIST_1)->setAllMvd    ( cMvZero,            ePartSize, uiPartAddr, 0, iPartIdx );
+
+          pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_0, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPIdxSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
+          pcCU->setMVPNumSubParts( -1, REF_PIC_LIST_1, uiPartAddr, iPartIdx, pcCU->getDepth(uiPartAddr));
       }
       else
@@ -4763,7 +4767,7 @@
         pcCU->setDvInfoSubParts(OriginalDvInfo, uiPartAddr, iPartIdx, pcCU->getDepth( uiPartAddr ) );
 #endif
-          pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMEMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
-          pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMEMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
-        }
+        pcCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMEMvField[0], ePartSize, uiPartAddr, 0, iPartIdx );
+        pcCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMEMvField[1], ePartSize, uiPartAddr, 0, iPartIdx );
+      }
 #if H_3D_SPIVMP
       delete[] pcMvFieldSP;
@@ -4774,5 +4778,5 @@
     //  MC
     motionCompensation ( pcCU, rpcPredYuv, REF_PIC_LIST_X, iPartIdx );
-    
+
   } //  end of for ( Int iPartIdx = 0; iPartIdx < iNumPart; iPartIdx++ )
 
