Index: branches/HTM-4.0-Nokia/source/App/TAppDecoder/TAppDecTop.cpp
===================================================================
--- branches/HTM-4.0-Nokia/source/App/TAppDecoder/TAppDecTop.cpp	(revision 138)
+++ branches/HTM-4.0-Nokia/source/App/TAppDecoder/TAppDecTop.cpp	(revision 139)
@@ -87,8 +87,16 @@
   increaseNumberOfViews( 1 );
 #endif
-  
+#if FLEX_CODING_ORDER
+  Int iDepthViewIdx = 0;
+  Int iTextureViewIdx=0;
+  Bool first_frame=1;
+  Bool viewid_zero=true;
+  Int FCO_index=0;  //when the current frame is not first frame,use FCO_index stand for viewDepth. 
+#endif
+
 #if SONY_COLPIC_AVAILABILITY
   m_tDecTop[0]->setViewOrderIdx(0);
 #endif
+
   Int                 viewDepthId = 0;
   Int                 previousViewDepthId  = 0;
@@ -102,4 +110,7 @@
     pcListPic[i] = NULL;
     newPicture[i] = false;
+#if FLEX_CODING_ORDER
+    FCO_Order[i]=NULL;
+#endif
   }
 
@@ -156,9 +167,111 @@
         depth = getVPSAccess()->getActiveVPS()->getDepthFlag(nalu.m_layerId);
       }
-      viewDepthId = nalu.m_layerId;   // coding order T0D0T1D1T2D2
+#if FLEX_CODING_ORDER
+      if (viewId>0)
+      {
+        viewid_zero=false;
+      }
+      if (viewid_zero==false&&viewId==0)
+      {
+        first_frame=0; //if viewId has been more than zero and now it set to zero again, we can see that it is not the first view
+      }
+      if (first_frame)
+      { // if the current view is first frame, we set the viewDepthId as texture plus depth and get the FCO order 
+        viewDepthId = iDepthViewIdx+iTextureViewIdx;
+        FCO_viewDepthId=viewDepthId;
+      }
+      else
+      {//if current view is not first frame, we set the viewDepthId depended on the FCO order
+        viewDepthId=0;
+        if (depth)
+        {
+          for (FCO_index=0;FCO_index<2*MAX_VIEW_NUM;FCO_index++ )
+          {
+            if (FCO_Order[FCO_index]=='D')
+            {
+              if (viewId==viewDepthId)
+                break;
+              else
+                viewDepthId++;
+            }
+          }
+        }
+        else
+        {
+          for (FCO_index=0;FCO_index<2*MAX_VIEW_NUM;FCO_index++ )
+          {
+            if (FCO_Order[FCO_index]=='T')
+            {
+              if (viewId==viewDepthId)
+                break;
+              else
+                viewDepthId++;
+            }
+          }
+        }
+
+        viewDepthId=FCO_index;
+
+      }
+
+
+#else
+       viewDepthId = nalu.m_layerId;   // coding order T0D0T1D1T2D2
+#endif
+    
 #else
       Int viewId = nalu.m_viewId;
       Int depth = nalu.m_isDepth ? 1 : 0;
-      viewDepthId = viewId * 2 + depth;   // coding order T0D0T1D1T2D2
+#if FLEX_CODING_ORDER
+      if (viewId>0)
+      {
+        viewid_zero=false;
+      }
+      if (viewid_zero==false&&viewId==0)
+      {
+        first_frame=0;
+      }
+      if (first_frame)
+      {
+        viewDepthId = iDepthViewIdx+iTextureViewIdx;
+        FCO_viewDepthId=viewDepthId;
+      }
+      else
+      {
+        viewDepthId=0;
+        if (depth)
+        {
+          for (FCO_index=0;FCO_index<2*MAX_VIEW_NUM;FCO_index++ )
+          {
+            if (FCO_Order[FCO_index]=='D')
+            {
+              if (viewId==viewDepthId)
+                break;
+              else
+                viewDepthId++;
+            }
+          }
+        }
+        else
+        {
+          for (FCO_index=0;FCO_index<2*MAX_VIEW_NUM;FCO_index++ )
+          {
+            if (FCO_Order[FCO_index]=='T')
+            {
+              if (viewId==viewDepthId)
+                break;
+              else
+                viewDepthId++;
+            }
+          }
+        }
+
+        viewDepthId=FCO_index;
+
+      }
+#else
+  viewDepthId = viewId * 2 + depth;   // coding order T0D0T1D1T2D2
+#endif
+     
 #endif
       
@@ -190,4 +303,7 @@
         for( Int i = 0; i < m_tDecTop.size(); i++ )
         {
+//#if FLEX_CODING_ORDER
+//        FCO_Order[i]=NULL;
+//#endif
           m_tDecTop[i]->compressMotion( uiPOC[i] );
         }
@@ -209,4 +325,22 @@
         {
           previousPictureDecoded = true;
+#if FLEX_CODING_ORDER
+if (first_frame)
+{
+  if (depth)
+  {
+    iDepthViewIdx++;
+    FCO_Order[viewDepthId]='D';
+  }
+  else
+  {
+    iTextureViewIdx++;
+    FCO_Order[viewDepthId]='T';
+  }
+}
+
+
+
+#endif
         }
       }
@@ -215,4 +349,5 @@
     {
       m_tDecTop[viewDepthId]->executeDeblockAndAlf(uiPOC[viewDepthId], pcListPic[viewDepthId], m_iSkipFrame, m_pocLastDisplay[viewDepthId]);
+
     }
     if( pcListPic[viewDepthId] )
@@ -234,4 +369,9 @@
     previousViewDepthId = viewDepthId;
   } 
+
+
+
+
+
   if( m_cCamParsCollector.isInitialized() )
   {
@@ -502,5 +642,63 @@
 TDecTop* TAppDecTop::getTDecTop( Int viewId, Bool isDepth )
 { 
+#if FLEX_CODING_ORDER
+  Int viewnumber=0;
+  Int i=0;
+  Bool FCO_flag=0;
+  if (viewId>FCO_viewDepthId)
+  {
+    return NULL;
+  }
+  else
+  {
+    if (isDepth)
+   {
+      for ( i=0; i<=FCO_viewDepthId;i++)
+      {
+         if (FCO_Order[i]=='D')
+         {
+           if (viewnumber==viewId)
+           {
+             FCO_flag=1;
+             break;
+           }
+           else
+             viewnumber++;
+         }
+      }
+    }
+    else
+    {
+      for ( i=0; i<=FCO_viewDepthId;i++)
+      {
+        if (FCO_Order[i]=='T')
+        {
+          if (viewnumber==viewId)
+          {
+            FCO_flag=1;
+            break;
+          }
+          else
+            viewnumber++;
+        }
+      }
+    }
+    if (FCO_flag)
+    {
+      return m_tDecTop[i];
+    }
+    else
+      return NULL;
+    
+  }
+
+    // coding order T0D0T1D1T2D2
+#else
   return m_tDecTop[(isDepth ? 1 : 0) + viewId * 2];  // coding order T0D0T1D1T2D2
+#endif
+  /*
+  //original code
+  return m_tDecTop[(isDepth ? 1 : 0) + viewId * 2];  // coding order T0D0T1D1T2D2
+  */
 } 
 
@@ -520,5 +718,22 @@
 {
   assert( ( viewId >= 0 ) );
-
+#if FLEX_CODING_ORDER
+if (getTDecTop(viewId,isDepth))
+{
+   TComList<TComPic*>* apcListPic = getTDecTop( viewId, isDepth )->getListPic();
+   TComPic* pcPic = NULL;
+   for( TComList<TComPic*>::iterator it=apcListPic->begin(); it!=apcListPic->end(); it++ )
+   {
+     if( (*it)->getPOC() == poc )
+     {
+       pcPic = *it;
+       break;
+     }
+   }
+   return pcPic;
+}
+else
+  return NULL;
+#else
   TComList<TComPic*>* apcListPic = getTDecTop( viewId, isDepth )->getListPic();
   TComPic* pcPic = NULL;
@@ -532,4 +747,7 @@
   }
   return pcPic;
+#endif
+
+
 }
 //! \}
Index: branches/HTM-4.0-Nokia/source/App/TAppDecoder/TAppDecTop.h
===================================================================
--- branches/HTM-4.0-Nokia/source/App/TAppDecoder/TAppDecTop.h	(revision 138)
+++ branches/HTM-4.0-Nokia/source/App/TAppDecoder/TAppDecTop.h	(revision 139)
@@ -70,5 +70,8 @@
   std::vector<Int>                m_pocLastDisplay;               ///< last POC in display order
   Bool                            m_useDepth;
-
+#if FLEX_CODING_ORDER
+  Int  FCO_viewDepthId;
+  Char FCO_Order[MAX_VIEW_NUM*2];
+#endif
   FILE*                           m_pScaleOffsetFile;
   CamParsCollector                m_cCamParsCollector;
Index: branches/HTM-4.0-Nokia/source/App/TAppEncoder/TAppEncCfg.h
===================================================================
--- branches/HTM-4.0-Nokia/source/App/TAppEncoder/TAppEncCfg.h	(revision 138)
+++ branches/HTM-4.0-Nokia/source/App/TAppEncoder/TAppEncCfg.h	(revision 139)
@@ -88,4 +88,13 @@
   Int       m_iNumberOfViews;                                 ///< number Views to Encode
   Bool      m_bUsingDepthMaps;
+
+#if FLEX_CODING_ORDER
+  char*  m_pchMVCJointCodingOrder;      ///<  texture-depth coding order
+  Bool    m_b3DVFlexOrder;    ///<  flexible coding order flag
+#endif
+
+
+
+
   // coding structure
   Int       m_iIntraPeriod;                                   ///< period of I-slice (random access period)
Index: branches/HTM-4.0-Nokia/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- branches/HTM-4.0-Nokia/source/App/TAppEncoder/TAppEncTop.cpp	(revision 138)
+++ branches/HTM-4.0-Nokia/source/App/TAppEncoder/TAppEncTop.cpp	(revision 139)
@@ -369,4 +369,27 @@
     for(Int iViewIdx=0; iViewIdx<m_iNumberOfViews; iViewIdx++)
     {
+#if FLEX_CODING_ORDER
+      // Detect whether depth comes before than texture for this view
+      Bool isDepthFirst = false;
+      if ( m_b3DVFlexOrder )
+      {
+        for ( Int ii=1; ii<12; ii+=2 )
+        {
+          Int iViewIdxCfg = (Int)(m_pchMVCJointCodingOrder[ii]-'0');
+          if ( iViewIdxCfg == iViewIdx )
+          {
+            if ( m_pchMVCJointCodingOrder[ii-1]=='D' ) // depth comes first for this view
+            {
+              isDepthFirst = true;
+            }
+            else
+            {
+              assert(m_pchMVCJointCodingOrder[ii-1]=='T');
+            }
+            break;
+          }
+        }
+      }
+#endif
       m_depthFrameRcvd.push_back(0);
       m_acTEncDepthTopList.push_back(new TEncTop); 
@@ -634,6 +657,17 @@
     m_acTEncDepthTopList[iViewIdx]->setUseDMM                     ( m_bUseDMM );
 #endif
+
+#if FLEX_CODING_ORDER && HHI_DMM_PRED_TEX
+    m_acTEncDepthTopList[iViewIdx]->setUseDMM34( (m_b3DVFlexOrder && isDepthFirst) ? false : m_bUseDMM );
+#endif
+
+
 #if HHI_MPI
-     m_acTEncDepthTopList[iViewIdx]->setUseMVI( m_bUseMVI );
+#if FLEX_CODING_ORDER
+    m_acTEncDepthTopList[iViewIdx]->setUseMVI( (m_b3DVFlexOrder && isDepthFirst) ? false : m_bUseMVI );
+#else
+    m_acTEncDepthTopList[iViewIdx]->setUseMVI( m_bUseMVI );
+#endif
+    // m_acTEncDepthTopList[iViewIdx]->setUseMVI( m_bUseMVI );
 #endif
     }
@@ -920,4 +954,59 @@
       m_cCameraData.update( iNextPoc );
       }
+
+
+
+#if FLEX_CODING_ORDER
+      if (m_b3DVFlexOrder)
+      {
+        Int  iNumDepthEncoded = 0;
+        iNumEncoded = 0;
+        Int i=0;
+        Int iViewIdx=0;
+        Int iNumberofDepthViews = m_bUsingDepthMaps?m_iNumberOfViews:0;
+        for (Int j=0; j < (m_iNumberOfViews+ iNumberofDepthViews); j++ )
+        {
+          if (m_pchMVCJointCodingOrder[i]=='T')
+          {
+
+            i++;
+            assert(isdigit(m_pchMVCJointCodingOrder[i]));
+            iViewIdx = (Int)(m_pchMVCJointCodingOrder[i]-'0');
+
+            m_acTEncTopList[iViewIdx]->encode( eos[iViewIdx], pcPicYuvOrg, *m_picYuvRec[iViewIdx], outputAccessUnits, iNumEncoded, gopId );
+            xWriteOutput(bitstreamFile, iNumEncoded, outputAccessUnits, iViewIdx, false);
+            outputAccessUnits.clear();
+            i++;
+          }
+          else if ( m_pchMVCJointCodingOrder[i] == 'D')
+          {
+
+            i++;
+            if( m_bUsingDepthMaps )
+            {
+              assert(isdigit(m_pchMVCJointCodingOrder[i]));
+              iViewIdx = (Int)(m_pchMVCJointCodingOrder[i]-'0');
+#if SAIT_VSO_EST_A0033
+              if( m_bUseVSO && iNextPoc < m_iFrameToBeEncoded )
+              {
+                m_cCameraData.xSetDispCoeff( iNextPoc, iViewIdx );
+                m_acTEncDepthTopList[iViewIdx]->setDispCoeff( m_cCameraData.getDispCoeff() );
+              }
+#endif
+              m_acTEncDepthTopList[iViewIdx]->encode( depthEos[iViewIdx], pcDepthPicYuvOrg, *m_picYuvDepthRec[iViewIdx], outputAccessUnits, iNumDepthEncoded, gopId );
+              xWriteOutput(bitstreamFile, iNumDepthEncoded, outputAccessUnits, iViewIdx, true);
+              outputAccessUnits.clear();
+              i++;
+            }
+          }
+        }
+      }
+      else
+      {
+
+#endif
+
+//original code
+
       for(Int iViewIdx=0; iViewIdx < m_iNumberOfViews; iViewIdx++ )
       {
@@ -943,4 +1032,12 @@
         }
       }
+
+#if FLEX_CODING_ORDER
+      }
+#endif
+
+
+
+
 #if HHI_INTERVIEW_SKIP || HHI_INTER_VIEW_MOTION_PRED || HHI_INTER_VIEW_RESIDUAL_PRED
       for( Int iViewIdx = 0; iViewIdx < m_iNumberOfViews; iViewIdx++ )
