Index: branches/SHM-upgrade/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- branches/SHM-upgrade/source/Lib/TLibCommon/TypeDef.h	(revision 965)
+++ branches/SHM-upgrade/source/Lib/TLibCommon/TypeDef.h	(revision 967)
@@ -293,4 +293,6 @@
 #define R0157_RESTRICT_PPSID_FOR_CGS_LUT 1      ///< JCTVC-R0157: when pps_pic_parameter_set_id greater than or equal to 8, colour_mapping_enabled_flag shall be equal to 0
 
+#define VPS_FIX_TO_MATCH_SPEC            1
+
 /// scalability types
 enum ScalabilityType
Index: branches/SHM-upgrade/source/Lib/TLibDecoder/TDecCAVLC.cpp
===================================================================
--- branches/SHM-upgrade/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 965)
+++ branches/SHM-upgrade/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 967)
@@ -3119,5 +3119,9 @@
   for(i = 1; i < numOutputLayerSets; i++)
   {
+#if VPS_FIX_TO_MATCH_SPEC
+    if( vps->getNumLayerSets() > 2 && i >= vps->getNumLayerSets() )
+#else
     if( i > (vps->getNumLayerSets() - 1) )
+#endif
     {
       Int numBits = 1;
@@ -3134,7 +3138,15 @@
     Int layerSetIdxForOutputLayerSet = vps->getOutputLayerSetIdx(i);
 #if Q0078_ADD_LAYER_SETS
-    if ( i > vps->getVpsNumLayerSetsMinus1() || vps->getDefaultTargetOutputLayerIdc() >= 2 )
-#else
-    if ( i > (vps->getNumLayerSets() - 1) || vps->getDefaultTargetOutputLayerIdc() >= 2 )
+#if VPS_FIX_TO_MATCH_SPEC
+    if( i > vps->getVpsNumLayerSetsMinus1() || vps->getDefaultTargetOutputLayerIdc() == 2 )
+#else
+    if( i > vps->getVpsNumLayerSetsMinus1() || vps->getDefaultTargetOutputLayerIdc() >= 2 )
+#endif
+#else
+#if VPS_FIX_TO_MATCH_SPEC
+    if( i > (vps->getNumLayerSets() - 1) || vps->getDefaultTargetOutputLayerIdc() == 2 )
+#else
+    if( i > (vps->getNumLayerSets() - 1) || vps->getDefaultTargetOutputLayerIdc() >= 2 )
+#endif
 #endif
     {
@@ -3179,7 +3191,11 @@
     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) )
-      {
-        READ_CODE( numBitsForPtlIdx, uiCode, "profile_level_tier_idx[i]" );
+#endif
+      {
+        READ_CODE( numBitsForPtlIdx, uiCode, "profile_level_tier_idx[i]" ); 
         vps->setProfileLevelTierIdx(i, j, uiCode );
 
@@ -3371,5 +3387,9 @@
   if( vps->getRepFormatIdxPresentFlag() )
   {
-    for(i = 1; i < vps->getMaxLayers(); i++)
+#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;
@@ -3860,5 +3880,10 @@
 #if DPB_INTERNAL_BL_SIG
             uiCode=0;
-        if(vps->getBaseLayerInternalFlag()  || ( vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, k)   !=  0 ) )
+
+#if VPS_FIX_TO_MATCH_SPEC
+        if( vps->getNecessaryLayerFlag(i, k) && ( vps->getBaseLayerInternalFlag() || vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, k) ) )
+#else
+        if(vps->getBaseLayerInternalFlag() || ( vps->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, k) !=  0 ) )
+#endif
 #endif
           READ_UVLC( uiCode, "max_vps_dec_pic_buffering_minus1[i][k][j]" ); vps->setMaxVpsDecPicBufferingMinus1( i, k, j, uiCode );
@@ -4131,5 +4156,9 @@
   if (!uiCode)
   {
-    for(i = 0; i < vps->getMaxLayers(); i++)
+#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
     {
       READ_FLAG( uiCode, "tiles_in_use_flag[ i ]" ); vps->setTilesInUseFlag(i, (uiCode == 1));
@@ -4145,5 +4174,9 @@
 #endif
 
-    for(i = 1; i < vps->getMaxLayers(); i++)
+#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++)
@@ -4166,5 +4199,9 @@
   if (!uiCode)
   {
-    for(i = 0; i < vps->getMaxLayers(); i++)
+#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
     {
       READ_FLAG( uiCode, "wpp_in_use_flag[ i ]" ); vps->setWppInUseFlag(i, (uiCode == 1));
@@ -4198,13 +4235,20 @@
       for(j = 0; j < vps->getNumDirectRefLayers(vps->getLayerIdInNuh(i)); j++)
       {
-        READ_UVLC( uiCode, "min_spatial_segment_offset_plus1[i][j]" ); vps->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
-        if( vps->getMinSpatialSegmentOffsetPlus1(i,j ) > 0 )
-        {
-          READ_FLAG( uiCode, "ctu_based_offset_enabled_flag[i][j]"); vps->setCtuBasedOffsetEnabledFlag(i, j, uiCode == 1 );
-          if(vps->getCtuBasedOffsetEnabledFlag(i,j))
+#if VPS_FIX_TO_MATCH_SPEC
+        if( vps->getBaseLayerInternalFlag() || vps->getRefLayerId(vps->getLayerIdInNuh(i), j) )
+        {
+#endif
+          READ_UVLC( uiCode, "min_spatial_segment_offset_plus1[i][j]" ); vps->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
+          if( vps->getMinSpatialSegmentOffsetPlus1(i,j ) > 0 )
           {
-            READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1[i][j]"); vps->setMinHorizontalCtuOffsetPlus1( i,j, uiCode );
+            READ_FLAG( uiCode, "ctu_based_offset_enabled_flag[i][j]"); vps->setCtuBasedOffsetEnabledFlag(i, j, uiCode == 1 );
+            if(vps->getCtuBasedOffsetEnabledFlag(i,j))
+            {
+              READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1[i][j]"); vps->setMinHorizontalCtuOffsetPlus1( i,j, uiCode );
+            }
           }
-        }
+#if VPS_FIX_TO_MATCH_SPEC
+        }
+#endif
       }
     }
@@ -4642,39 +4686,70 @@
     }
   }
-  for (Int h = 1; h < vps->getNumOutputLayerSets(); h++)
-  {
-    Int lsIdx = vps->getOutputLayerSetIdx( h );
-    READ_UVLC( uiCode, "num_signalled_partitioning_schemes[h]"); vps->setNumSignalledPartitioningSchemes(h, uiCode);
-    for( Int j = 0; j < vps->getNumSignalledPartitioningSchemes(h); j++ )
-    {
-      READ_UVLC( uiCode, "num_partitions_in_scheme_minus1[h][j]" ); vps->setNumPartitionsInSchemeMinus1(h, j, uiCode);
-      for( Int k = 0; k <= vps->getNumPartitionsInSchemeMinus1(h, j); k++ )
-      {
-        for( Int r = 0; r < vps->getNumLayersInIdList( lsIdx ); r++ )
-        {
-          READ_FLAG( uiCode, "layer_included_in_partition_flag[h][j][k][r]" ); vps->setLayerIncludedInPartitionFlag(h, j, k, r, uiCode ? true : false);
-        }
-      }
-    }
-    for( Int i = 0; i < vps->getNumSignalledPartitioningSchemes(h) + 1; i++ )
-    {
-      for( Int t = 0; t <= vps->getMaxSLayersInLayerSetMinus1(lsIdx); t++ )
-      {
-        READ_UVLC( uiCode, "num_bsp_schedules_minus1[h][i][t]");              vps->setNumBspSchedulesMinus1(h, i, t, uiCode);
-        for( Int j = 0; j <= vps->getNumBspSchedulesMinus1(h, i, t); j++ )
-        {
-          for( Int k = 0; k < vps->getNumPartitionsInSchemeMinus1(h, i); k++ )
+#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);
+      READ_UVLC(uiCode, "num_signalled_partitioning_schemes[h]"); vps->setNumSignalledPartitioningSchemes(h, uiCode);
+#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
+      {
+        READ_UVLC(uiCode, "num_partitions_in_scheme_minus1[h][j]"); vps->setNumPartitionsInSchemeMinus1(h, j, uiCode);
+        for (Int k = 0; k <= vps->getNumPartitionsInSchemeMinus1(h, j); k++)
+        {
+          for (Int r = 0; r < vps->getNumLayersInIdList(lsIdx); r++)
           {
-            READ_UVLC( uiCode, "bsp_comb_hrd_idx[h][i][t][j][k]");      vps->setBspHrdIdx(h, i, t, j, k, uiCode);
-            READ_UVLC( uiCode, "bsp_comb_sched_idx[h][i][t][j][k]");    vps->setBspSchedIdx(h, i, t, j, k, uiCode);
+            READ_FLAG(uiCode, "layer_included_in_partition_flag[h][j][k][r]"); vps->setLayerIncludedInPartitionFlag(h, j, k, r, uiCode ? true : false);
           }
         }
       }
-    }
-
-    // To be done: Check each layer included in not more than one BSP in every partitioning scheme,
-    // and other related checks associated with layers in bitstream partitions.
-
-  }
+      for (Int i = 0; i < vps->getNumSignalledPartitioningSchemes(h) + 1; i++)
+      {
+        for (Int t = 0; t <= vps->getMaxSLayersInLayerSetMinus1(lsIdx); t++)
+        {
+          READ_UVLC(uiCode, "num_bsp_schedules_minus1[h][i][t]");              vps->setNumBspSchedulesMinus1(h, i, t, uiCode);
+          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 )
+              {
+#endif
+#if VPS_FIX_TO_MATCH_SPEC
+                Int numBits = 1;
+                while ((1 << numBits) < (vps->getNumHrdParameters() + vps->getVpsNumAddHrdParams()))
+                {
+                  numBits++;
+                }
+                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
+              }
+#endif
+              READ_UVLC(uiCode, "bsp_comb_sched_idx[h][i][t][j][k]");    vps->setBspSchedIdx(h, i, t, j, k, uiCode);
+            }
+          }
+        }
+      }
+
+      // To be done: Check each layer included in not more than one BSP in every partitioning scheme,
+      // and other related checks associated with layers in bitstream partitions.
+
+    }
+#if VPS_FIX_TO_MATCH_SPEC
+  }
+#endif
 }
 #endif
