Index: branches/HTM-DEV-0.3-dev2/source/Lib/TLibCommon/TComDataCU.cpp
===================================================================
--- branches/HTM-DEV-0.3-dev2/source/Lib/TLibCommon/TComDataCU.cpp	(revision 513)
+++ branches/HTM-DEV-0.3-dev2/source/Lib/TLibCommon/TComDataCU.cpp	(revision 514)
@@ -2644,14 +2644,14 @@
                                      Bool* abCandIsInter, TComMvField* pcMvFieldNeighbours, UChar* puhInterDirNeighbours, Int* vspFlag )
 {
-  if( !m_pcSlice->getVPS()->getViewSynthesisPredFlag( m_pcSlice->getLayerIdInVps() ) ) // Not to add the candidate if VSP is turned off
+  if ( pDInfo->bDV == false || ucVspMergePos != H_3D_VSP_POSITION || 0 == m_pcSlice->getViewIndex() || 
+      !m_pcSlice->getVPS()->getViewSynthesisPredFlag( m_pcSlice->getLayerIdInVps() ) || // Not to add the candidate if VSP is turned off
+       m_pcSlice->getIsDepth() // VSP is disabled for depth layers
     return false;
-  if( m_pcSlice->getIsDepth() ) // VSP is turned off for depth layers
+
+  Int refViewIdx = pDInfo->m_aVIdxCan;
+  TComPic* picDepth = getSlice()->getIvPic( true, refViewIdx );
+  if( picDepth == NULL ) // No depth reference avail
     return false;
-
-  Int refViewIdx = pDInfo->m_aVIdxCan;
-  TComPic* picDepth = NULL;
-  //assert(getSlice()->getRefPic(eRefPicList, refId)->getPOC() == getSlice()->getPOC());
-  picDepth = getSlice()->getIvPic( true, refViewIdx );
-
+  
   //////////
   // Code if simply re-writing
@@ -2681,60 +2681,59 @@
   /////////////
   
-  if(ucVspMergePos == H_3D_VSP_POSITION && picDepth != NULL && 0 != m_pcSlice->getViewIndex() ) // VSP can be used only when depth is used as input
-  {
-    Bool refViewAvailFlag = false;
-    UChar predFlag[2] = {0, 0};
-    Int  iRefListIdX = 0;
-    Int  iRefListIdY = 0;
-
-    for( iRefListIdX = 0; iRefListIdX < 2 && !refViewAvailFlag; iRefListIdX++ )
-    {
-      RefPicList eRefPicList = RefPicList( iRefListIdX );
-      for (Int i = 0; i < m_pcSlice->getNumRefIdx(eRefPicList) && !refViewAvailFlag; i++)
-      {
-        Int viewIdxRefInList = m_pcSlice->getRefPic(eRefPicList, i)->getViewIndex();
-        if (refViewIdx == viewIdxRefInList)
-        {
-          Int iRefIdxList0 = getSlice()->getRefPic(REF_PIC_LIST_0, 0)->getPOC() == getSlice()->getPOC() ? 0 :
-                            (getSlice()->getAlterRefIdx(REF_PIC_LIST_0) == -1 ? NOT_VALID : getSlice()->getAlterRefIdx(REF_PIC_LIST_0));
-
-          refViewAvailFlag = true;
-          predFlag[0] = 1; // iRefListIdX
-          iRefListIdY = 1 - iRefListIdX;
-          pcMvFieldNeighbours[iCount<<1].setMvField( pDInfo->m_acDoNBDV, iRefIdxList0 );
-          // pcMvFieldNeighbours[(iCount<<1)+iRefListIdX].setMvField( pDInfo->m_acDoNBDV, i ); // Buggy code
-        }
-      }
-    }
-
-    if (m_pcSlice->isInterB() && refViewAvailFlag)
-    {
-      RefPicList eRefPicList = RefPicList( iRefListIdY );
-      refViewAvailFlag = false;
-      for (Int i = 0; i < m_pcSlice->getNumRefIdx(eRefPicList) && !refViewAvailFlag; i++)
-      {
-        Int viewIdxRefInList = m_pcSlice->getRefPic(eRefPicList, i)->getViewIndex();
-        if (refViewIdx != viewIdxRefInList)
-        {
-          Int iRefIdxList1 = getSlice()->getRefPic(REF_PIC_LIST_1, 0)->getPOC() == getSlice()->getPOC() ? 0 :
-                            (getSlice()->getAlterRefIdx(REF_PIC_LIST_1) == -1 ? NOT_VALID : getSlice()->getAlterRefIdx(REF_PIC_LIST_1));
-
-          refViewAvailFlag = true;
-          predFlag[1] = 1; // iRefListIdY
-          pcMvFieldNeighbours[(iCount<<1)+1].setMvField( pDInfo->m_acDoNBDV, iRefIdxList1 );
-          //pcMvFieldNeighbours[(iCount<<1)+iRefListIdY].setMvField( pDInfo->m_acDoNBDV, i );  // Buggy code
-        }
-      }
-    }
-
-    abCandIsInter[iCount] = true;
-    puhInterDirNeighbours[iCount] = (predFlag[0] | (predFlag[1] << 1));
-    vspFlag[iCount] = 1;
-
-    if ( mrgCandIdx == iCount )
-      return true;
-
-    iCount++;
-  }
+  
+  Bool refViewAvailFlag = false;
+  UChar predFlag[2] = {0, 0};
+  Int  iRefListIdX = 0;
+  Int  iRefListIdY = 0;
+
+  for( iRefListIdX = 0; iRefListIdX < 2 && !refViewAvailFlag; iRefListIdX++ )
+  {
+    RefPicList eRefPicList = RefPicList( iRefListIdX );
+    for (Int i = 0; i < m_pcSlice->getNumRefIdx(eRefPicList) && !refViewAvailFlag; i++)
+    {
+      Int viewIdxRefInList = m_pcSlice->getRefPic(eRefPicList, i)->getViewIndex();
+      if (refViewIdx == viewIdxRefInList)
+      {
+        Int iRefIdxList0 = getSlice()->getRefPic(REF_PIC_LIST_0, 0)->getPOC() == getSlice()->getPOC() ? 
+                           0 : getSlice()->getAlterRefIdx(REF_PIC_LIST_0);
+
+        refViewAvailFlag = true;
+        predFlag[0] = 1; // iRefListIdX
+        iRefListIdY = 1 - iRefListIdX;
+        pcMvFieldNeighbours[iCount<<1].setMvField( pDInfo->m_acDoNBDV, iRefIdxList0 );
+        // pcMvFieldNeighbours[(iCount<<1)+iRefListIdX].setMvField( pDInfo->m_acDoNBDV, i ); // Buggy code
+      }
+    }
+  }
+
+  if (m_pcSlice->isInterB() && refViewAvailFlag)
+  {
+    RefPicList eRefPicList = RefPicList( iRefListIdY );
+    refViewAvailFlag = false;
+    for (Int i = 0; i < m_pcSlice->getNumRefIdx(eRefPicList) && !refViewAvailFlag; i++)
+    {
+      Int viewIdxRefInList = m_pcSlice->getRefPic(eRefPicList, i)->getViewIndex();
+      if (refViewIdx != viewIdxRefInList)
+      {
+        Int iRefIdxList1 = getSlice()->getRefPic(REF_PIC_LIST_1, 0)->getPOC() == getSlice()->getPOC() ? 
+                           0 : getSlice()->getAlterRefIdx(REF_PIC_LIST_1);
+
+        refViewAvailFlag = true;
+        predFlag[1] = 1; // iRefListIdY
+        pcMvFieldNeighbours[(iCount<<1)+1].setMvField( pDInfo->m_acDoNBDV, iRefIdxList1 );
+        //pcMvFieldNeighbours[(iCount<<1)+iRefListIdY].setMvField( pDInfo->m_acDoNBDV, i );  // Buggy code
+      }
+    }
+  }
+
+  // Set values to be returned
+  abCandIsInter[iCount] = true;
+  puhInterDirNeighbours[iCount] = (predFlag[0] | (predFlag[1] << 1));
+  vspFlag[iCount] = 1;
+
+  if ( mrgCandIdx == iCount )
+    return true;
+
+  iCount++;
 
   return false;
