Index: branches/SHM-upgrade/source/Lib/TLibDecoder/TDecCAVLC.cpp
===================================================================
--- branches/SHM-upgrade/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 979)
+++ branches/SHM-upgrade/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 981)
@@ -3548,7 +3548,16 @@
   }
 #endif
+
+#if VPS_FIX_TO_MATCH_SPEC
+  for( i = vps->getBaseLayerInternalFlag() ? 1 : 2; i < vps->getMaxLayers(); i++ )
+#else
   for(i = 1; i < vps->getMaxLayers(); i++)
-  {
+#endif
+  {
+#if VPS_FIX_TO_MATCH_SPEC
+    for( j = vps->getBaseLayerInternalFlag() ? 0 : 1; j < i; j++ )
+#else
     for(j = 0; j < i; j++)
+#endif
     {
       if (vps->getDirectDependencyFlag(i, j))
@@ -4746,6 +4755,4 @@
               if( vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams() > 1 )
               {
-#endif
-#if VPS_FIX_TO_MATCH_SPEC
                 Int numBits = 1;
                 while ((1 << numBits) < (vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams()))
@@ -4754,9 +4761,7 @@
                 }
                 READ_CODE(numBits, uiCode, "bsp_comb_hrd_idx[h][i][t][j][k]");      vps->setBspHrdIdx(h, i, t, j, k, uiCode);
-#else
-                READ_UVLC(uiCode, "bsp_comb_hrd_idx[h][i][t][j][k]");      vps->setBspHrdIdx(h, i, t, j, k, uiCode);
-#endif
-#if VPS_FIX_TO_MATCH_SPEC
               }
+#else
+              READ_UVLC(uiCode, "bsp_comb_hrd_idx[h][i][t][j][k]");      vps->setBspHrdIdx(h, i, t, j, k, uiCode);
 #endif
               READ_UVLC(uiCode, "bsp_comb_sched_idx[h][i][t][j][k]");    vps->setBspSchedIdx(h, i, t, j, k, uiCode);
Index: branches/SHM-upgrade/source/Lib/TLibEncoder/TEncCavlc.cpp
===================================================================
--- branches/SHM-upgrade/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 979)
+++ branches/SHM-upgrade/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 981)
@@ -2370,5 +2370,9 @@
   {
     Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx(i);
+#if VPS_FIX_TO_MATCH_SPEC
+    if( vps->getNumLayerSets() > 2 && i >= vps->getNumLayerSets() )
+#else
     if( i > (vps->getNumLayerSets() - 1) )
+#endif
     {
       Int numBits = 1;
@@ -2399,5 +2403,9 @@
     for(j = 0; j < vps->getNumLayersInIdList(layerSetIdxForOutputLayerSet) ; j++)
     {
+#if VPS_FIX_TO_MATCH_SPEC
+      if( vps->getNecessaryLayerFlag(i, j) && (vps->getNumProfileTierLevel() - 1) > 0 )
+#else
       if( vps->getNecessaryLayerFlag(i, j) )
+#endif
       {
         WRITE_CODE( vps->getProfileLevelTierIdx(i, j), numBitsForPtlIdx, "profile_level_tier_idx[i]" );
@@ -2468,5 +2476,9 @@
   if( vps->getRepFormatIdxPresentFlag() )
   {
+#if VPS_FIX_TO_MATCH_SPEC
+    for( i = vps->getBaseLayerInternalFlag() ? 1 : 0; i < vps->getMaxLayers(); i++ )
+#else
     for(i = 1; i < vps->getMaxLayers(); i++)
+#endif
     {
       Int numBits = 1;
@@ -2560,7 +2572,15 @@
   else
   {
+#if VPS_FIX_TO_MATCH_SPEC
+    for( i = vps->getBaseLayerInternalFlag() ? 1 : 2; i < vps->getMaxLayers(); i++ )
+#else
     for(i = 1; i < vps->getMaxLayers(); i++)
-    {
+#endif
+    {
+#if VPS_FIX_TO_MATCH_SPEC
+      for( j = vps->getBaseLayerInternalFlag() ? 0 : 1; j < i; j++ )
+#else
       for(j = 0; j < i; j++)
+#endif
       {
         if (vps->getDirectDependencyFlag(i, j))
@@ -2775,5 +2795,9 @@
         {
 #if DPB_INTERNAL_BL_SIG
+#if VPS_FIX_TO_MATCH_SPEC
+        if( vps->getNecessaryLayerFlag(i, k) && (vps->getBaseLayerInternalFlag() || (vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, k) != 0)) )
+#else
         if(vps->getBaseLayerInternalFlag()  || ( vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, k)   !=  0 ) )
+#endif
 #endif
           WRITE_UVLC( vps->getMaxVpsDecPicBufferingMinus1( i, k, j), "max_vps_dec_pic_buffering_minus1[i][k][j]" );
@@ -2905,5 +2929,9 @@
   if (!vps->getTilesNotInUseFlag())
   {
+#if VPS_FIX_TO_MATCH_SPEC
+    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++ )
+#else
     for(i = 0; i < vps->getMaxLayers(); i++)
+#endif
     {
       WRITE_FLAG( vps->getTilesInUseFlag(i) ? 1 : 0 , "tiles_in_use_flag[ i ]" );
@@ -2915,5 +2943,9 @@
 #endif
 
+#if VPS_FIX_TO_MATCH_SPEC
+    for( i = vps->getBaseLayerInternalFlag() ? 1 : 2; i < vps->getMaxLayers(); i++ )
+#else
     for(i = 1; i < vps->getMaxLayers(); i++)
+#endif
     {
       for(j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++)
@@ -2936,5 +2968,9 @@
   if (!vps->getWppNotInUseFlag())
   {
+#if VPS_FIX_TO_MATCH_SPEC
+    for( i = vps->getBaseLayerInternalFlag() ? 0 : 1; i < vps->getMaxLayers(); i++ )
+#else
     for(i = 0; i < vps->getMaxLayers(); i++)
+#endif
     {
       WRITE_FLAG( vps->getWppInUseFlag(i) ? 1 : 0 , "wpp_in_use_flag[ i ]" );
@@ -2967,16 +3003,23 @@
     {
       for(j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++)
-      {        
-        WRITE_UVLC(vps->getMinSpatialSegmentOffsetPlus1( i, j),    "min_spatial_segment_offset_plus1[i][j]");
-       
-        if( vps->getMinSpatialSegmentOffsetPlus1(i,j ) > 0 ) 
-        {  
-          WRITE_FLAG( vps->getCtuBasedOffsetEnabledFlag( i, j) ? 1 : 0 , "ctu_based_offset_enabled_flag[i][j]" );    
-          
-          if(vps->getCtuBasedOffsetEnabledFlag(i,j))  
-          {
-            WRITE_UVLC(vps->getMinHorizontalCtuOffsetPlus1( i, j),    "min_horizontal_ctu_offset_plus1[i][j]");            
+      {
+#if VPS_FIX_TO_MATCH_SPEC
+        if (vps->getBaseLayerInternalFlag() || vps->getRefLayerId(vps->getLayerIdInNuh(i), j))
+        {
+#endif
+          WRITE_UVLC(vps->getMinSpatialSegmentOffsetPlus1( i, j),    "min_spatial_segment_offset_plus1[i][j]");
+
+          if( vps->getMinSpatialSegmentOffsetPlus1(i,j ) > 0 ) 
+          {  
+            WRITE_FLAG( vps->getCtuBasedOffsetEnabledFlag( i, j) ? 1 : 0 , "ctu_based_offset_enabled_flag[i][j]" );    
+
+            if(vps->getCtuBasedOffsetEnabledFlag(i,j))  
+            {
+              WRITE_UVLC(vps->getMinHorizontalCtuOffsetPlus1( i, j),    "min_horizontal_ctu_offset_plus1[i][j]");            
+            }
           }
-        }  
+#if VPS_FIX_TO_MATCH_SPEC
+        }
+#endif
       }  
     }
@@ -3351,9 +3394,19 @@
     codeHrdParameters(vps->getBspHrd(j), i == 0 ? true : vps->getCprmsAddPresentFlag(j), vps->getNumSubLayerHrdMinus1(j));
   }
+
+#if VPS_FIX_TO_MATCH_SPEC
+  if( vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams() > 0 )
+  {
+#endif
   for( Int h = 1; h < vps->getNumOutputLayerSets(); h++ )
   {
     Int lsIdx = vps->getOutputLayerSetIdx( h );
     WRITE_UVLC( vps->getNumSignalledPartitioningSchemes(h), "num_signalled_partitioning_schemes[h]");
+
+#if VPS_FIX_TO_MATCH_SPEC
+    for( Int j = 1; j < vps->getNumSignalledPartitioningSchemes(h) + 1; j++ )
+#else
     for( Int j = 0; j < vps->getNumSignalledPartitioningSchemes(h); j++ )
+#endif
     {
       WRITE_UVLC( vps->getNumPartitionsInSchemeMinus1(h, j), "num_partitions_in_scheme_minus1[h][j]" );
@@ -3366,4 +3419,5 @@
       }
     }
+
     for( Int i = 0; i < vps->getNumSignalledPartitioningSchemes(h) + 1; i++ )
     {
@@ -3371,9 +3425,26 @@
       {
         WRITE_UVLC(vps->getNumBspSchedulesMinus1(h, i, t), "num_bsp_schedules_minus1[h][i][t]");
+
         for( Int j = 0; j <= vps->getNumBspSchedulesMinus1(h, i, t); j++ )
         {
+#if VPS_FIX_TO_MATCH_SPEC
+          for (Int k = 0; k <= vps->getNumPartitionsInSchemeMinus1(h, i); k++)
+#else
           for( Int k = 0; k < vps->getNumPartitionsInSchemeMinus1(h, i); k++ )
+#endif
           {
+#if VPS_FIX_TO_MATCH_SPEC
+            if (vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams() > 1)
+            {
+              Int numBits = 1;
+              while ((1 << numBits) < (vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams()))
+              {
+                numBits++;
+              }
+              WRITE_CODE(vps->getBspHrdIdx(h, i, t, j, k), numBits, "bsp_comb_hrd_idx[h][i][t][j][k]");
+            }
+#else
             WRITE_UVLC( vps->getBspHrdIdx(h, i, t, j, k),   "bsp_comb_hrd_idx[h][i][t][j][k]");
+#endif
             WRITE_UVLC( vps->getBspSchedIdx(h, i, t, j, k), "bsp_comb_sched_idx[h][i][t][j][k]");
           }
@@ -3382,4 +3453,7 @@
     }
   }
+#if VPS_FIX_TO_MATCH_SPEC
+  }
+#endif
 }
 #endif
