Index: trunk/source/App/TAppEncoder/TAppEncCfg.cpp
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 608)
+++ trunk/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 622)
@@ -224,8 +224,10 @@
     in>>entry.m_interViewRefPosL[1][i];
   }
+#if !H_MV5
   if (entry.m_numActiveRefLayerPics > 0 )
   {
     in>>entry.m_collocatedRefLayerIdx; 
   }
+#endif
 #endif
   return in;
@@ -367,10 +369,19 @@
   ("NumberOfLayers",        m_numberOfLayers     , 1,                     "Number of layers")
 #if !H_3D
+#if H_MV5
+  ("ScalabilityMask",       m_scalabilityMask    , 2                    , "Scalability Mask")    
+#else
   ("ScalabilityMask",       m_scalabilityMask    , 1                    , "Scalability Mask")    
+#endif
 #else
   ("ScalabilityMask",       m_scalabilityMask    , 3                    , "Scalability Mask, 1: Texture 3: Texture + Depth ")    
 #endif  
   ("DimensionIdLen",        m_dimensionIdLen     , cfg_dimensionLength  , "Number of bits used to store dimensions Id")
+#if H_MV5
+  ("ViewOrderIndex",        m_viewOrderIndex     , std::vector<Int>(1,0), "View Order Index per layer")
+  ("ViewId",                m_viewId             , std::vector<Int>(1,0), "View Id per View Order Index")
+#else
   ("ViewId",                m_viewId             , std::vector<Int>(1,0), "View Id")
+#endif
 #if H_3D
   ("DepthFlag",             m_depthFlag          , std::vector<Int>(1,0), "Depth Flag")
@@ -587,4 +598,24 @@
   ("TargetBitrate,-tbr", m_targetBitrate, 0, "Input target bitrate")
   ("NumLCUInUnit,-nu", m_numLCUInUnit, 0, "Number of LCUs in an Unit")
+#endif
+
+#if H_MV5
+#if H_MV
+  // VPS VUI
+  ("VpsVuiPresentFlag"           , m_vpsVuiPresentFlag           , false                                           , "VpsVuiPresentFlag           ")
+  ("BitRatePresentVpsFlag"       , m_bitRatePresentVpsFlag       , false                                           , "BitRatePresentVpsFlag       ")
+  ("PicRatePresentVpsFlag"       , m_picRatePresentVpsFlag       , false                                           , "PicRatePresentVpsFlag       ")
+  ("BitRatePresentFlag"          , m_bitRatePresentFlag          , std::vector< Bool >(1,0)  ,MAX_VPS_OP_SETS_PLUS1, "BitRatePresentFlag per sub layer for the N-th layer set")
+  ("PicRatePresentFlag"          , m_picRatePresentFlag          , std::vector< Bool >(1,0)  ,MAX_VPS_OP_SETS_PLUS1, "PicRatePresentFlag per sub layer for the N-th layer set")
+  ("AvgBitRate"                  , m_avgBitRate                  , std::vector< Int  >(1,0)  ,MAX_VPS_OP_SETS_PLUS1, "AvgBitRate         per sub layer for the N-th layer set")
+  ("MaxBitRate"                  , m_maxBitRate                  , std::vector< Int  >(1,0)  ,MAX_VPS_OP_SETS_PLUS1, "MaxBitRate         per sub layer for the N-th layer set")
+  ("ConstantPicRateIdc"          , m_constantPicRateIdc          , std::vector< Int  >(1,0)  ,MAX_VPS_OP_SETS_PLUS1, "ConstantPicRateIdc per sub layer for the N-th layer set")
+  ("AvgPicRate"                  , m_avgPicRate                  , std::vector< Int  >(1,0)  ,MAX_VPS_OP_SETS_PLUS1, "AvgPicRate         per sub layer for the N-th layer set")
+  ("TileBoundariesAlignedFlag"   , m_tileBoundariesAlignedFlag   , std::vector< Bool >(1,0)  ,MAX_NUM_LAYERS       , "TileBoundariesAlignedFlag    per direct reference for the N-th layer")
+  ("IlpRestrictedRefLayersFlag"  , m_ilpRestrictedRefLayersFlag  , false                                           , "IlpRestrictedRefLayersFlag")
+  ("MinSpatialSegmentOffsetPlus1", m_minSpatialSegmentOffsetPlus1, std::vector< Int  >(1,0)  ,MAX_NUM_LAYERS       , "MinSpatialSegmentOffsetPlus1 per direct reference for the N-th layer")
+  ("CtuBasedOffsetEnabledFlag"   , m_ctuBasedOffsetEnabledFlag   , std::vector< Bool >(1,0)  ,MAX_NUM_LAYERS       , "CtuBasedOffsetEnabledFlag    per direct reference for the N-th layer")
+  ("MinHorizontalCtuOffsetPlus1" , m_minHorizontalCtuOffsetPlus1 , std::vector< Int  >(1,0)  ,MAX_NUM_LAYERS       , "MinHorizontalCtuOffsetPlus1  per direct reference for the N-th layer")
+#endif
 #endif
 
@@ -719,5 +750,5 @@
   ("ViewSynthesisPred",               m_viewSynthesisPredFlag,  true           , "view synthesis prediction " )  
 #endif
-#if H_3D_TMVP
+#if H_3D
   ("IvMvScaling",                     m_ivMvScalingFlag      ,  true            , "inter view motion vector scaling" )    
 #endif
@@ -731,4 +762,7 @@
     if( k == 0 )
     {
+#if H_MV5
+      m_GOPListMvc[0][0].m_sliceType = 'I'; 
+#endif
       for( Int i = 1; i < MAX_GOP + 1; i++ ) 
       {
@@ -934,4 +968,26 @@
   // allocate slice-based dQP values
 #if H_MV
+#if H_MV5
+  xResizeVector( m_viewOrderIndex    ); 
+
+  std::vector<Int> uniqueViewOrderIndices; 
+  for( Int layer = 0; layer < m_numberOfLayers; layer++ )
+  {    
+    Bool isIn = false; 
+    for ( Int i = 0 ; i < uniqueViewOrderIndices.size(); i++ )
+    {
+      isIn = isIn || ( m_viewOrderIndex[ layer ] == uniqueViewOrderIndices[ i ] ); 
+    }
+    if ( !isIn ) 
+    {
+      uniqueViewOrderIndices.push_back( m_viewOrderIndex[ layer ] ); 
+    } 
+  }
+  m_iNumberOfViews = (Int) uniqueViewOrderIndices.size(); 
+
+#if H_3D
+  xResizeVector( m_depthFlag ); 
+#endif
+#else
   xResizeVector( m_viewId    ); 
 #if H_3D
@@ -954,4 +1010,5 @@
 #endif
 
+#endif
   xResizeVector( m_fQP ); 
 
@@ -1226,4 +1283,20 @@
   xConfirmPara( (m_layerIdInNuh.size()!=1) && (m_layerIdInNuh.size() < m_numberOfLayers) , "LayerIdInNuh must be given for all layers. ");
   
+#if H_MV5
+#if H_3D
+  xConfirmPara( m_scalabilityMask != 2 && m_scalabilityMask != 3, "Scalability Mask must be equal to 2 or 3. ");
+#else
+  xConfirmPara( m_scalabilityMask != 2 , "Scalability Mask must be equal to 2. ");
+#endif
+
+#if H_3D
+  if ( m_scalabilityMask & ( 1 << DEPTH_ID ) )
+  {
+    m_dimIds.push_back( m_depthFlag ); 
+  }
+#endif
+
+  m_dimIds.push_back( m_viewOrderIndex );   
+#else
 #if H_3D
   xConfirmPara( m_scalabilityMask != 1 && m_scalabilityMask != 3, "Scalability Mask must be equal to 1 or 3. ");
@@ -1239,4 +1312,5 @@
 #endif
 
+#endif
   xConfirmPara(  m_dimensionIdLen.size() < m_dimIds.size(), "DimensionIdLen must be given for all dimensions. "   );   Int dimBitOffset[MAX_NUM_SCALABILITY_TYPES+1]; 
 
@@ -1255,5 +1329,9 @@
   {    
     xConfirmPara( m_dimIds[j].size() < m_numberOfLayers,  "DimensionId must be given for all layers and all dimensions. ");   
+#if H_MV5   
+    xConfirmPara( (m_dimIds[j][0] != 0)                 , "DimensionId of layer 0 must be 0. " );
+#else
     xConfirmPara( ( j != viewDimPosition ) &&  (m_dimIds[j][0] != 0), "DimensionId of layer 0 must be 0. " );
+#endif
     xConfirmPara( m_dimensionIdLen[j] < 1 || m_dimensionIdLen[j] > 8, "DimensionIdLen must be greater than 0 and less than 9 in all dimensions. " ); 
      
@@ -1297,16 +1375,31 @@
      {
        Bool inc = m_dimIds[ lastDiff ][ i ] > m_dimIds[ lastDiff ][ j ]; 
+#if H_MV5
+       Bool shallBeButIsNotIncreasing = ( !inc  ) ; 
+#else
        Bool shallBeButIsNotIncreasing = ( !inc && ( lastDiff != viewDimPosition ) ) ; 
+#endif
        if ( shallBeButIsNotIncreasing )
        {       
          printf( "\nError: Positions of Layers %d and %d is not increasing in dimension %d \n", i, j, lastDiff);        
        }
+#if H_MV5
+       xConfirmPara( shallBeButIsNotIncreasing,  "DimensionIds shall be increasing within one dimension. " );
+#else
        xConfirmPara( shallBeButIsNotIncreasing && ( lastDiff != viewDimPosition ),  "DimensionIds shall be increasing within one dimension. " );
+#endif
      }
    }
  }
 
+#if H_MV5
+ /// ViewId 
+ xConfirmPara( m_viewId.size() != m_iNumberOfViews, "The number of ViewIds must be equal to the number of views." ); 
+
   /// Layer sets
+  xConfirmPara( m_vpsNumLayerSets < 0 || m_vpsNumLayerSets > 1024, "VpsNumLayerSets must be greater than 0 and less than 1025. ") ; 
+#else
   xConfirmPara( m_vpsNumLayerSets < 0 || m_vpsNumLayerSets > 1024, "VpsNumLayerSets must be greater than 0 and less than 1025") ; 
+#endif
   for( Int lsIdx = 0; lsIdx < m_vpsNumLayerSets; lsIdx++ )
   {
@@ -2068,4 +2161,28 @@
   }
 #endif
+#if H_MV5
+#if H_MV
+  // VPS VUI
+  for(Int i = 0; i < MAX_VPS_OP_SETS_PLUS1; i++ )
+  { 
+    for (Int j = 0; j < MAX_TLAYER; j++)
+    {    
+      if ( j < m_avgBitRate        [i].size() ) xConfirmPara( m_avgBitRate[i][j]         <  0 || m_avgBitRate[i][j]         > 65535, "avg_bit_rate            must be more than or equal to     0 and less than 65536" );
+      if ( j < m_maxBitRate        [i].size() ) xConfirmPara( m_maxBitRate[i][j]         <  0 || m_maxBitRate[i][j]         > 65535, "max_bit_rate            must be more than or equal to     0 and less than 65536" );
+      if ( j < m_constantPicRateIdc[i].size() ) xConfirmPara( m_constantPicRateIdc[i][j] <  0 || m_constantPicRateIdc[i][j] >     3, "constant_pic_rate_idc   must be more than or equal to     0 and less than     4" );
+      if ( j < m_avgPicRate        [i].size() ) xConfirmPara( m_avgPicRate[i][j]         <  0 || m_avgPicRate[i][j]         > 65535, "avg_pic_rate            must be more than or equal to     0 and less than 65536" );
+    }
+  }
+  // todo: replace value of 100 with requirement in spec
+  for(Int i = 0; i < MAX_NUM_LAYERS; i++ )
+  { 
+    for (Int j = 0; j < MAX_NUM_LAYERS; j++)
+    {    
+      if ( j < m_minSpatialSegmentOffsetPlus1[i].size() ) xConfirmPara( m_minSpatialSegmentOffsetPlus1[i][j] < 0 || m_minSpatialSegmentOffsetPlus1[i][j] >   100, "min_spatial_segment_offset_plus1 must be more than or equal to     0 and less than   101" );
+      if ( j < m_minHorizontalCtuOffsetPlus1[i] .size() ) xConfirmPara( m_minHorizontalCtuOffsetPlus1[i][j]  < 0 || m_minHorizontalCtuOffsetPlus1[i][j]  >   100, "min_horizontal_ctu_offset_plus1  must be more than or equal to     0 and less than   101" );
+    }
+  }
+#endif
+#endif
 
   xConfirmPara(!m_TransquantBypassEnableFlag && m_CUTransquantBypassFlagValue, "CUTransquantBypassFlagValue cannot be 1 when TransquantBypassEnableFlag is 0");
@@ -2133,5 +2250,10 @@
 #endif
 #if H_MV
+#if H_MV5
+  xPrintParaVector( "ViewIdVal"     , m_viewId ); 
+  xPrintParaVector( "ViewOrderIndex", m_viewOrderIndex ); 
+#else
   xPrintParaVector( "ViewId", m_viewId ); 
+#endif
 #endif
 #if H_3D
@@ -2283,5 +2405,5 @@
   printf("ViewSynthesisPred:%d ", m_viewSynthesisPredFlag );
 #endif
-#if H_3D_TMVP
+#if H_3D
   printf("IvMvScaling:%d ", m_ivMvScalingFlag ? 1 : 0  );
 #endif
Index: trunk/source/App/TAppEncoder/TAppEncCfg.h
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncCfg.h	(revision 608)
+++ trunk/source/App/TAppEncoder/TAppEncCfg.h	(revision 622)
@@ -69,6 +69,10 @@
   std::vector<char*>     m_pchReconFileList;                  ///< output reconstruction file names
   Int                    m_numberOfLayers;                    ///< number of Layers to Encode
+#if H_MV5
+  Int                    m_iNumberOfViews;                    ///< number of Layers that are views
+#else
 #if H_3D
   Int                    m_iNumberOfViews;                    ///< number of Layers that are views
+#endif
 #endif
 #else
@@ -79,4 +83,7 @@
   std::vector< std::vector<Int> > m_dimIds;                   ///< dimension ids ( pointers to m_viewId and m_depthFlag 
   std::vector<Int>       m_viewId;                            ///< view id
+#if H_MV5
+  std::vector<Int>       m_viewOrderIndex;                    ///< view order index  
+#endif
 #if H_3D
   std::vector<Int>       m_depthFlag;                         ///< depth flag
@@ -99,4 +106,22 @@
   std::vector< std::vector<Int> > m_dependencyTypes;          ///< Dependency types of direct reference layers
 
+#if H_MV5
+  // VPS VUI
+  Bool m_vpsVuiPresentFlag;
+  Bool m_bitRatePresentVpsFlag;
+  Bool m_picRatePresentVpsFlag;
+  std::vector< std::vector<Bool > > m_bitRatePresentFlag;
+  std::vector< std::vector<Bool > > m_picRatePresentFlag;
+  std::vector< std::vector<Int  > > m_avgBitRate;
+  std::vector< std::vector<Int  > > m_maxBitRate;
+  std::vector< std::vector<Int  > > m_constantPicRateIdc;
+  std::vector< std::vector<Int  > > m_avgPicRate;
+  std::vector< std::vector<Bool > > m_tileBoundariesAlignedFlag;  
+  Bool m_ilpRestrictedRefLayersFlag;
+  std::vector< std::vector<Int  > > m_minSpatialSegmentOffsetPlus1;
+  std::vector< std::vector<Bool > > m_ctuBasedOffsetEnabledFlag;
+  std::vector< std::vector<Int  > > m_minHorizontalCtuOffsetPlus1;
+#endif
+
 #if H_3D_IV_MERGE
   Bool                   m_ivMvPredFlag;                      ///< Interview motion vector prediction 
@@ -115,5 +140,5 @@
   Bool m_viewSynthesisPredFlag;
 #endif
-#if H_3D_TMVP
+#if H_3D
   Bool m_ivMvScalingFlag; 
 #endif
Index: trunk/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncTop.cpp	(revision 608)
+++ trunk/source/App/TAppEncoder/TAppEncTop.cpp	(revision 622)
@@ -88,5 +88,9 @@
     vps.setTemporalNestingFlag(true);
   }
+#if H_MV5
+  vps.setMaxLayersMinus1( m_numberOfLayers - 1);
+#else
   vps.setMaxLayers( m_numberOfLayers );
+#endif
   for(Int i = 0; i < MAX_TLAYER; i++)
   {
@@ -120,12 +124,25 @@
   xSetDependencies( vps );
   xSetProfileTierLevel     ( vps ); 
+#if H_MV5
+  xSetRepFormat            ( vps ); 
+#endif
   xSetLayerSets            ( vps ); 
+#if H_MV5
+  xSetVPSVUI               ( vps ); 
+#endif
 #if H_3D
+#if !H_MV5
   vps.initViewIndex(); 
+#endif
   xSetVPSExtension2        ( vps ); 
   m_ivPicLists.setVPS      ( &vps ); 
 #endif
 
+
+#if H_MV5
+  for(Int layerIdInVps = 0; layerIdInVps < m_numberOfLayers; layerIdInVps++)
+#else 
   for(Int layer = 0; layer < m_numberOfLayers; layer++)
+#endif
   {
     m_frameRcvd                 .push_back(0);
@@ -134,5 +151,14 @@
     m_acTVideoIOYuvReconFileList.push_back(new TVideoIOYuv);
     m_picYuvRec                 .push_back(new TComList<TComPicYuv*>) ;
-
+#if H_MV5   
+    m_ivPicLists.push_back( m_acTEncTopList[ layerIdInVps ]->getListPic()  ); 
+    TEncTop& m_cTEncTop = *m_acTEncTopList[ layerIdInVps ];  // It is not a member, but this name helps avoiding code duplication !!!
+
+    Int layerId = vps.getLayerIdInNuh( layerIdInVps );
+    m_cTEncTop.setLayerIdInVps                 ( layerIdInVps ); 
+    m_cTEncTop.setLayerId                      ( layerId );    
+    m_cTEncTop.setViewId                       ( vps.getViewId      (  layerId ) );
+    m_cTEncTop.setViewIndex                    ( vps.getViewIndex   (  layerId ) );
+#else
     m_ivPicLists.push_back( m_acTEncTopList[ layer ]->getListPic()  ); 
     TEncTop& m_cTEncTop = *m_acTEncTopList[ layer ];  // It is not a member, but this name helps avoiding code duplication !!!
@@ -141,7 +167,13 @@
     m_cTEncTop.setLayerId                      ( vps.getLayerIdInNuh( layer ) );    
     m_cTEncTop.setViewId                       ( vps.getViewId      ( layer ) );
+
+#endif
 #if H_3D
+#if H_MV5
+    Bool isDepth = ( vps.getDepthId     ( layerId ) != 0 ) ;
+#else
     Bool isDepth = ( vps.getDepthId     ( layer ) != 0 ) ;
     m_cTEncTop.setViewIndex                    ( vps.getViewIndex   ( layer ) );
+#endif
     m_cTEncTop.setIsDepth                      ( isDepth );
     //====== Camera Parameters =========
@@ -169,4 +201,14 @@
     m_cTEncTop.setDWeight                      ( isDepth ? m_iDWeight             : 0     );
 #endif // H_3D_VSO
+#if H_MV5
+#if H_3D_ARP
+    //====== Advanced Inter-view Residual Prediction =========
+    m_cTEncTop.setUseAdvRP                     ( ( isDepth || 0==layerIdInVps ) ? 0 : m_uiUseAdvResPred );
+    m_cTEncTop.setARPStepNum                   ( ( isDepth || 0==layerIdInVps ) ? 1 : H_3D_ARP_WFNR     );
+#endif
+#if H_3D_IC
+    m_cTEncTop.setUseIC                        ( vps.getViewIndex( layerId ) == 0 ? false : m_abUseIC[isDepth ? 1 : 0] );
+#endif
+#else
 #if H_3D_ARP
     //====== Advanced Inter-view Residual Prediction =========
@@ -176,4 +218,5 @@
 #if H_3D_IC
     m_cTEncTop.setUseIC                        ( vps.getViewIndex( layer ) == 0 ? false : m_abUseIC[isDepth ? 1 : 0] );
+#endif
 #endif
   //========== Depth intra modes ==========
@@ -217,4 +260,13 @@
   m_cTEncTop.setGOPSize                      ( m_iGOPSize );
 #if H_MV
+#if H_MV5
+m_cTEncTop.setGopList                      ( m_GOPListMvc[layerIdInVps] );
+  m_cTEncTop.setExtraRPSs                    ( m_extraRPSsMvc[layerIdInVps] );
+  for(Int i = 0; i < MAX_TLAYER; i++)
+  {
+    m_cTEncTop.setNumReorderPics             ( m_numReorderPicsMvc[layerIdInVps][i], i );
+    m_cTEncTop.setMaxDecPicBuffering         ( m_maxDecPicBufferingMvc[layerIdInVps][i], i );
+  }
+#else
   m_cTEncTop.setGopList                      ( m_GOPListMvc[layer] );
   m_cTEncTop.setExtraRPSs                    ( m_extraRPSsMvc[layer] );
@@ -224,4 +276,5 @@
     m_cTEncTop.setMaxDecPicBuffering         ( m_maxDecPicBufferingMvc[layer][i], i );
   }
+#endif
 #else
   m_cTEncTop.setGopList                      ( m_GOPList );
@@ -238,5 +291,9 @@
   }
 #if H_MV
+#if H_MV5
+  m_cTEncTop.setQP                           ( m_iQP[layerIdInVps] );
+#else
   m_cTEncTop.setQP                           ( m_iQP[layer] );
+#endif
 #else
   m_cTEncTop.setQP                           ( m_iQP );
@@ -246,5 +303,9 @@
 
 #if H_MV
+#if H_MV5
+  m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayerMvc[layerIdInVps] );
+#else
   m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayerMvc[layer] );
+#endif
 #else
   m_cTEncTop.setMaxTempLayer                 ( m_maxTempLayer );
@@ -256,5 +317,9 @@
   //====== Loop/Deblock Filter ========
 #if H_MV
+#if H_MV5
+  m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable[layerIdInVps]);
+#else
   m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable[layer]);
+#endif
 #else
   m_cTEncTop.setLoopFilterDisable            ( m_bLoopFilterDisable       );
@@ -286,5 +351,9 @@
 
 #if H_MV
+#if H_MV5
+  if ((m_iMaxDeltaQP == 0 ) && (m_iQP[layerIdInVps] == lowestQP) && (m_useLossless == true))
+#else
   if ((m_iMaxDeltaQP == 0 ) && (m_iQP[layer] == lowestQP) && (m_useLossless == true))
+#endif
 #else
   if ((m_iMaxDeltaQP == 0 ) && (m_iQP == lowestQP) && (m_useLossless == true))
@@ -303,5 +372,9 @@
   m_cTEncTop.setUseLossless                  ( m_useLossless );
 #if H_MV
+#if H_MV5
+  m_cTEncTop.setdQPs                         ( m_aidQP[layerIdInVps]   );
+#else
   m_cTEncTop.setdQPs                         ( m_aidQP[layer]   );
+#endif
 #else
   m_cTEncTop.setdQPs                         ( m_aidQP        );
@@ -362,5 +435,9 @@
   m_cTEncTop.setLFCrossSliceBoundaryFlag( m_bLFCrossSliceBoundaryFlag );
 #if H_MV
+#if H_MV5
+  m_cTEncTop.setUseSAO ( m_bUseSAO[layerIdInVps] );
+#else
   m_cTEncTop.setUseSAO ( m_bUseSAO[layer] );
+#endif
 #else
   m_cTEncTop.setUseSAO ( m_bUseSAO );
@@ -1044,13 +1121,37 @@
 Void TAppEncTop::xSetDimensionIdAndLength( TComVPS& vps )
 {   
+#if H_MV5
+  vps.setScalabilityMaskFlag( m_scalabilityMask ); 
+#else
   vps.setScalabilityMask( m_scalabilityMask ); 
+#endif
   for( Int dim = 0; dim < m_dimIds.size(); dim++ )
   {
     vps.setDimensionIdLen( dim, m_dimensionIdLen[ dim ] );
+#if H_MV5
+    for( Int layer = 0; layer <= vps.getMaxLayersMinus1(); layer++ )
+#else
     for( Int layer = 0; layer < vps.getMaxLayers(); layer++ )
+#endif
+
     {        
       vps.setDimensionId( layer, dim, m_dimIds[ dim ][ layer ] );        
     }  
   }
+
+#if H_MV5
+  Int maxViewId = xGetMax( m_viewId ); 
+
+  Int viewIdLenMinus1 = std::max( gCeilLog2( maxViewId + 1 ) - 1, 0 ) ; 
+  const Int maxViewIdLenMinus1 = ( 1 << 4 ) - 1; 
+  assert( viewIdLenMinus1 <= maxViewIdLenMinus1  ); 
+  vps.setViewIdLenMinus1( viewIdLenMinus1 ); 
+  for (Int i = 0; i < m_iNumberOfViews; i++)
+  {
+    vps.setViewIdVal( i, m_viewId[ i] ); 
+  }
+
+  assert( m_iNumberOfViews == vps.getNumViews() ); 
+#endif
 }
 
@@ -1078,5 +1179,8 @@
   }
 
-  // Max temporal id for inter layer reference pictures
+  // Max temporal id for inter layer reference pictures + presence flag
+#if H_MV5
+  Bool maxTidRefPresentFlag = false; 
+#endif
   for ( Int refLayerIdInVps = 0; refLayerIdInVps < m_numberOfLayers; refLayerIdInVps++)
     {
@@ -1099,13 +1203,27 @@
     }
     vps.setMaxTidIlRefPicPlus1( refLayerIdInVps, maxTid + 1 );
-  }
-
+#if H_MV5
+    maxTidRefPresentFlag = maxTidRefPresentFlag || ( maxTid != 6 );    
+#endif
+  }
+
+#if H_MV5
+  vps.setMaxTidRefPresentFlag( maxTidRefPresentFlag );
+#endif
   // Max one active ref layer flag
   Bool maxOneActiveRefLayerFlag = true;  
+#if H_MV5
+  for ( Int layerIdInVps = 1; layerIdInVps < m_numberOfLayers && maxOneActiveRefLayerFlag; layerIdInVps++)
+#else
   for ( Int currLayerIdInVps = 1; currLayerIdInVps < m_numberOfLayers && maxOneActiveRefLayerFlag; currLayerIdInVps++)
+#endif
   {
     for( Int i = 0; i < ( getGOPSize() + 1) && maxOneActiveRefLayerFlag; i++ ) 
     {        
+#if H_MV5
+      GOPEntry ge =  m_GOPListMvc[layerIdInVps][ ( i < getGOPSize()  ? i : MAX_GOP ) ]; 
+#else
       GOPEntry ge =  m_GOPListMvc[currLayerIdInVps][ ( i < getGOPSize()  ? i : MAX_GOP ) ]; 
+#endif
       maxOneActiveRefLayerFlag =  maxOneActiveRefLayerFlag && (ge.m_numActiveRefLayerPics <= 1); 
     }            
@@ -1113,7 +1231,43 @@
 
   vps.setMaxOneActiveRefLayerFlag( maxOneActiveRefLayerFlag );
+  
+  
+#if H_MV5
+  // All Ref layers active flag
+  Bool allRefLayersActiveFlag = true; 
+  for ( Int layerIdInVps = 1; layerIdInVps < m_numberOfLayers && allRefLayersActiveFlag; layerIdInVps++)
+  {    
+    for( Int i = 0; i < ( getGOPSize() + 1) && allRefLayersActiveFlag; i++ ) 
+    {        
+      GOPEntry ge =  m_GOPListMvc[layerIdInVps][ ( i < getGOPSize()  ? i : MAX_GOP ) ]; 
+      allRefLayersActiveFlag = allRefLayersActiveFlag && (ge.m_numActiveRefLayerPics == m_directRefLayers[ layerIdInVps ].size() ); 
+    }            
+  }
+
+  vps.setAllRefLayersActiveFlag( allRefLayersActiveFlag );
+
+  // Currently cross layer irap aligned is always true.   
+  vps.setCrossLayerIrapAlignedFlag( true ); 
+#endif
   vps.setRefLayers(); 
 }; 
 
+#if H_MV5
+GOPEntry* TAppEncTop::xGetGopEntry( Int layerIdInVps, Int poc )
+{
+  GOPEntry* geFound = NULL; 
+  for( Int i = 0; i < ( getGOPSize() + 1) && geFound == NULL ; i++ ) 
+  {
+    GOPEntry* ge = &(m_GOPListMvc[layerIdInVps][ ( i < getGOPSize()  ? i : MAX_GOP ) ]);
+    if ( ge->m_POC == poc )
+    {
+      geFound = ge;       
+    }
+  }
+  assert( geFound != NULL ); 
+  return geFound; 
+}
+#endif
+
 Void TAppEncTop::xSetLayerIds( TComVPS& vps )
 {
@@ -1137,5 +1291,7 @@
   Int maxVec = 0; 
   for ( Int i = 0; i < vec.size(); i++)    
+  {
     maxVec = max( vec[i], maxVec ); 
+  }
   return maxVec;
 }
@@ -1152,4 +1308,30 @@
 }
 
+#if H_MV5
+
+Void TAppEncTop::xSetRepFormat( TComVPS& vps )
+{
+  vps.setRepFormatIdxPresentFlag( true ); 
+  vps.setVpsNumRepFormatsMinus1 ( 0    ); 
+
+  TComRepFormat* repFormat = new TComRepFormat; 
+
+  repFormat->setBitDepthVpsChromaMinus8   ( g_bitDepthC - 8 ); 
+  repFormat->setBitDepthVpsLumaMinus8     ( g_bitDepthY - 8 );
+  repFormat->setChromaFormatVpsIdc        ( CHROMA_420      );
+  repFormat->setPicHeightVpsInLumaSamples ( m_iSourceHeight );
+  repFormat->setPicWidthVpsInLumaSamples  ( m_iSourceWidth  );    
+  // ToDo not supported yet. 
+  //repFormat->setSeparateColourPlaneVpsFlag( );
+
+  assert( vps.getRepFormat( 0 ) == NULL ); 
+  vps.setRepFormat( 0 , repFormat );
+
+  for(Int i = 0; i <= vps.getMaxLayersMinus1(); i++ )
+  {
+    vps.setVpsRepFormatIdx( i , 0 ); 
+  }
+}
+#endif
 
 Void TAppEncTop::xSetLayerSets( TComVPS& vps )
@@ -1204,9 +1386,106 @@
   }
 }
+#if H_MV5
+
+Void TAppEncTop::xSetVPSVUI( TComVPS& vps )
+{
+  vps.setVpsVuiPresentFlag( m_vpsVuiPresentFlag ); 
+
+  if ( m_vpsVuiPresentFlag )
+  {
+    TComVPSVUI* pcVPSVUI = vps.getVPSVUI(  ); 
+
+    assert( pcVPSVUI ); 
+
+    pcVPSVUI->setBitRatePresentVpsFlag( m_bitRatePresentVpsFlag );
+    pcVPSVUI->setPicRatePresentVpsFlag( m_picRatePresentVpsFlag );
+
+    if( pcVPSVUI->getBitRatePresentVpsFlag( )  ||  pcVPSVUI->getPicRatePresentVpsFlag( ) )
+    {
+      for( Int i = 0; i  <=  vps.getVpsNumberLayerSetsMinus1(); i++ )
+      {
+        for( Int j = 0; j  <=  vps.getMaxTLayers(); j++ ) 
+        {
+          if( pcVPSVUI->getBitRatePresentVpsFlag( ) && m_bitRatePresentFlag[i].size() > j )
+          {
+            pcVPSVUI->setBitRatePresentFlag( i, j, m_bitRatePresentFlag[i][j] );            
+          }
+          if( pcVPSVUI->getPicRatePresentVpsFlag( ) && m_picRatePresentFlag[i].size() > j   )
+          {
+            pcVPSVUI->setPicRatePresentFlag( i, j, m_picRatePresentFlag[i][j] );
+          }
+          if( pcVPSVUI->getBitRatePresentFlag( i, j )  && m_avgBitRate[i].size() > j )
+          {
+            pcVPSVUI->setAvgBitRate( i, j, m_avgBitRate[i][j] );          
+          }
+          if( pcVPSVUI->getBitRatePresentFlag( i, j )  && m_maxBitRate[i].size() > j )
+          {
+            pcVPSVUI->setMaxBitRate( i, j, m_maxBitRate[i][j] );
+          }
+          if( pcVPSVUI->getPicRatePresentFlag( i, j ) && m_constantPicRateIdc[i].size() > j )
+          {
+            pcVPSVUI->setConstantPicRateIdc( i, j, m_constantPicRateIdc[i][j] );
+          }
+          if( pcVPSVUI->getPicRatePresentFlag( i, j ) && m_avgPicRate[i].size() > j )
+          {
+            pcVPSVUI->setAvgPicRate( i, j, m_avgPicRate[i][j] );
+          }
+        }
+      }
+    }
+
+    for( Int i = 1; i  <=  vps.getMaxLayersMinus1(); i++ )
+    {
+      for( Int  j = 0; j < vps.getNumDirectRefLayers( vps.getLayerIdInNuh( i ) ); j++ ) 
+      {
+        if ( m_tileBoundariesAlignedFlag[i].size() > j )
+        {
+          pcVPSVUI->setTileBoundariesAlignedFlag( i, j, m_tileBoundariesAlignedFlag[i][j]);
+        }
+      }
+    }
+
+    pcVPSVUI->setIlpRestrictedRefLayersFlag( m_ilpRestrictedRefLayersFlag );
+
+    if( pcVPSVUI->getIlpRestrictedRefLayersFlag( ) )
+    {
+      for( Int i = 1; i  <=  vps.getMaxLayersMinus1(); i++ )
+      {
+        for( Int j = 0; j < vps.getNumDirectRefLayers( vps.getLayerIdInNuh( i ) ); j++ )
+        {
+          if ( m_minSpatialSegmentOffsetPlus1[i].size() > j )
+          {        
+            pcVPSVUI->setMinSpatialSegmentOffsetPlus1( i, j, m_minSpatialSegmentOffsetPlus1[i][j] );
+          }
+          if( pcVPSVUI->getMinSpatialSegmentOffsetPlus1( i, j ) > 0 )
+          {
+            if ( m_ctuBasedOffsetEnabledFlag[i].size() > j )
+            {        
+              pcVPSVUI->setCtuBasedOffsetEnabledFlag( i, j, m_ctuBasedOffsetEnabledFlag[i][j] );
+            }
+            if( pcVPSVUI->getCtuBasedOffsetEnabledFlag( i, j ) )
+            {
+              if ( m_minHorizontalCtuOffsetPlus1[i].size() > j )
+              {
+                pcVPSVUI->setMinHorizontalCtuOffsetPlus1( i, j, m_minHorizontalCtuOffsetPlus1[i][j] );
+              }
+            }
+          }
+        }
+      }
+    }      
+  }
+}
+#endif
 #endif
 #if H_3D
 Void TAppEncTop::xSetVPSExtension2( TComVPS& vps )
 {
+#if H_MV5
+
+  for ( Int layer = 0; layer <= vps.getMaxLayersMinus1(); layer++ )
+#else
   for ( Int layer = 0; layer < vps.getMaxLayers(); layer++ )
+#endif
   {
     Bool isDepth      = ( vps.getDepthId( layer ) == 1 ) ;
@@ -1242,5 +1521,5 @@
 #endif
   }  
-#if H_3D_TMVP
+#if H_3D
   vps.setIvMvScalingFlag( m_ivMvScalingFlag );   
 #endif
Index: trunk/source/App/TAppEncoder/TAppEncTop.h
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncTop.h	(revision 608)
+++ trunk/source/App/TAppEncoder/TAppEncTop.h	(revision 622)
@@ -121,4 +121,9 @@
   Void xSetLayerSets              ( TComVPS& vps );
   Void xSetProfileTierLevel       ( TComVPS& vps );
+#if H_MV5
+  Void xSetRepFormat              ( TComVPS& vps );
+  Void xSetVPSVUI                 ( TComVPS& vps );
+  GOPEntry* xGetGopEntry( Int layerIdInVps, Int poc );
+#endif
   Int  xGetMax( std::vector<Int>& vec);
 #endif
