Index: /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp
===================================================================
--- /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 824)
+++ /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 825)
@@ -1003,4 +1003,7 @@
   ("CGSMaxYPartNumLog",  m_nCGSMaxYPartNumLog2 , 2, "max Y part number ")
   ("CGSLUTBit",     m_nCGSLUTBit , 12, "bit depth of CGS LUT")
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  ("CGSAdaptC",     m_nCGSAdaptiveChroma , 1, "adaptive chroma partition (only for the case of two chroma partitions)")
+#endif
 #endif
 #if Q0108_TSA_STSA
@@ -3087,4 +3090,7 @@
   printf("CGS: %d CGSMaxOctantDepth: %d CGSMaxYPartNumLog2: %d CGSLUTBit:%d " , m_nCGSFlag , m_nCGSMaxOctantDepth , m_nCGSMaxYPartNumLog2 , m_nCGSLUTBit );
 #endif
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  printf("CGSAdaptC:%d " , m_nCGSAdaptiveChroma );
+#endif
   printf("\n\n");
   
Index: /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.h
===================================================================
--- /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.h	(revision 824)
+++ /branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.h	(revision 825)
@@ -435,5 +435,8 @@
   Int  m_nCGSMaxYPartNumLog2;
   Int  m_nCGSLUTBit;
-#endif
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Int  m_nCGSAdaptiveChroma;
+#endif
+#endif 
 #endif //SVC_EXTENSION
 public:
Index: /branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- /branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp	(revision 824)
+++ /branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp	(revision 825)
@@ -650,4 +650,7 @@
     m_acTEncTop[layer].setCGSMaxYPartNumLog2( m_nCGSMaxYPartNumLog2 );
     m_acTEncTop[layer].setCGSLUTBit( m_nCGSLUTBit );
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+    m_acTEncTop[layer].setCGSAdaptChroma( m_nCGSAdaptiveChroma );
+#endif
 #endif
 #if Q0078_ADD_LAYER_SETS
Index: /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.cpp	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.cpp	(revision 825)
@@ -17,4 +17,7 @@
   m_pCuboid = NULL;
   m_nResQuanBit = 0;
+#if R0164_CGS_LUT_BUGFIX
+  m_pCuboidExplicit = NULL;
+#endif
 }
 
@@ -24,5 +27,9 @@
 }
 
-Void TCom3DAsymLUT::create( Int nMaxOctantDepth , Int nInputBitDepth , Int nInputBitDepthC , Int nOutputBitDepth , Int nOutputBitDepthC , Int nMaxYPartNumLog2 )
+Void TCom3DAsymLUT::create( Int nMaxOctantDepth , Int nInputBitDepth , Int nInputBitDepthC , Int nOutputBitDepth , Int nOutputBitDepthC , Int nMaxYPartNumLog2 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  , Int nAdaptCThresholdU , Int nAdaptCThresholdV
+#endif
+  )
 {
   m_nMaxOctantDepth = nMaxOctantDepth;
@@ -36,5 +43,9 @@
   m_nMaxPartNumLog2 = 3 * m_nMaxOctantDepth + m_nMaxYPartNumLog2;
 
-  xUpdatePartitioning( nMaxOctantDepth , nMaxYPartNumLog2 );
+  xUpdatePartitioning( nMaxOctantDepth , nMaxYPartNumLog2 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+    , nAdaptCThresholdU , nAdaptCThresholdV
+#endif
+    );
 
   m_nYSize = 1 << ( m_nMaxOctantDepth + m_nMaxYPartNumLog2 );
@@ -48,4 +59,8 @@
   }
   xAllocate3DArray( m_pCuboid , m_nYSize , m_nUSize , m_nVSize );
+
+#if R0164_CGS_LUT_BUGFIX
+  xAllocate3DArray( m_pCuboidExplicit , m_nYSize , m_nUSize , m_nVSize );
+#endif
 }
 
@@ -53,7 +68,14 @@
 {
   xFree3DArray( m_pCuboid );
-}
-
-Void TCom3DAsymLUT::xUpdatePartitioning( Int nCurOctantDepth , Int nCurYPartNumLog2 )
+#if R0164_CGS_LUT_BUGFIX
+  xFree3DArray( m_pCuboidExplicit  );
+#endif
+}
+
+Void TCom3DAsymLUT::xUpdatePartitioning( Int nCurOctantDepth , Int nCurYPartNumLog2 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  , Int nAdaptCThresholdU , Int nAdaptCThresholdV
+#endif
+  )
 {
   assert( nCurOctantDepth <= m_nMaxOctantDepth );
@@ -64,5 +86,11 @@
   m_nYShift2Idx = m_nInputBitDepthY - m_nCurOctantDepth - m_nCurYPartNumLog2;
   m_nUShift2Idx = m_nVShift2Idx = m_nInputBitDepthC - m_nCurOctantDepth;
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  m_nMappingShift = 10 + m_nInputBitDepthY - m_nOutputBitDepthY; 
+  m_nAdaptCThresholdU = nAdaptCThresholdU;
+  m_nAdaptCThresholdV = nAdaptCThresholdV;
+#else
   m_nMappingShift = m_nYShift2Idx + m_nUShift2Idx;
+#endif
   m_nMappingOffset = 1 << ( m_nMappingShift - 1 );
 }
@@ -159,7 +187,17 @@
   
   SYUVP sPred;
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  sPred.Y = sPred.U = sPred.V = 0;
+  if( nVertexIdx == 0 )
+    sPred.Y = xGetNormCoeffOne() << ( m_nOutputBitDepthY - m_nInputBitDepthY );
+  else if( nVertexIdx == 1 )
+    sPred.U = xGetNormCoeffOne() << ( m_nOutputBitDepthY - m_nInputBitDepthY );
+  else if( nVertexIdx == 2 )
+    sPred.V = xGetNormCoeffOne() << ( m_nOutputBitDepthY - m_nInputBitDepthY );
+#else
   sPred.Y = ( yIdx + m_nVertexIdxOffset[nVertexIdx][0] ) << ( m_nYShift2Idx + m_nDeltaBitDepth );
   sPred.U = ( uIdx + m_nVertexIdxOffset[nVertexIdx][1] ) << ( m_nUShift2Idx + m_nDeltaBitDepthC );
   sPred.V = ( vIdx + m_nVertexIdxOffset[nVertexIdx][2] ) << ( m_nVShift2Idx + m_nDeltaBitDepthC );
+#endif
   return( sPred );
 }
@@ -169,4 +207,17 @@
   SCuboid***  pCuboid = pCurCuboid ? pCurCuboid : m_pCuboid ;
 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  SYUVP sPred;
+  if( yIdx == 0 )
+  {
+    sPred.Y = nVertexIdx == 0 ? 1024 : 0;
+    sPred.U = nVertexIdx == 1 ? 1024 : 0;
+    sPred.V = nVertexIdx == 2 ? 1024 : 0;
+  }
+  else
+  {
+    sPred = pCuboid[yIdx-1][uIdx][vIdx].P[nVertexIdx];
+  }
+#else
   // PredA
   SYUVP sPredA = xGetCuboidVertexPredA( yIdx , uIdx , vIdx , nVertexIdx );
@@ -196,5 +247,5 @@
   sPred.U = sPredA.U + sPredB.U;
   sPred.V = sPredA.V + sPredB.V;
-
+#endif
   return sPred ;
 }
@@ -220,8 +271,18 @@
   rYUVP.U = sPred.U + ( deltaU << m_nResQuanBit );
   rYUVP.V = sPred.V + ( deltaV << m_nResQuanBit );
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  // LUT coefficients are less than 12-bit
+  assert( -2048 <= rYUVP.Y && rYUVP.Y <= 2047 );
+  assert( -2048 <= rYUVP.U && rYUVP.U <= 2047 );
+  assert( -2048 <= rYUVP.V && rYUVP.V <= 2047 );
+#endif
 }
 
 Pel TCom3DAsymLUT::xMapY( Pel y , Pel u , Pel v )
 {
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  const SCuboid & rCuboid = m_pCuboid[xGetYIdx(y)][xGetUIdx(u)][xGetVIdx(v)];
+  Pel dstY = ( ( rCuboid.P[0].Y * y + rCuboid.P[1].Y * u + rCuboid.P[2].Y * v + m_nMappingOffset ) >> m_nMappingShift ) + rCuboid.P[3].Y;
+#else
   const SCuboid & rCuboid = m_pCuboid[y>>m_nYShift2Idx][u>>m_nUShift2Idx][v>>m_nVShift2Idx];
   Pel dstY = rCuboid.P[0].Y;
@@ -233,4 +294,5 @@
                    + ( ( deltaV * ( rCuboid.P[2].Y - rCuboid.P[1].Y ) ) << m_nYShift2Idx ) 
                    + m_nMappingOffset ) >> m_nMappingShift );
+#endif
   return( dstY );
 }
@@ -238,4 +300,11 @@
 SYUVP TCom3DAsymLUT::xMapUV( Pel y , Pel u , Pel v )
 {
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  const SCuboid & rCuboid = m_pCuboid[xGetYIdx(y)][xGetUIdx(u)][xGetVIdx(v)];
+  SYUVP dst;
+  dst.Y = 0;
+  dst.U = ( ( rCuboid.P[0].U * y + rCuboid.P[1].U * u + rCuboid.P[2].U * v + m_nMappingOffset ) >> m_nMappingShift ) + rCuboid.P[3].U;
+  dst.V = ( ( rCuboid.P[0].V * y + rCuboid.P[1].V * u + rCuboid.P[2].V * v + m_nMappingOffset ) >> m_nMappingShift ) + rCuboid.P[3].V;
+#else
   const SCuboid & rCuboid = m_pCuboid[y>>m_nYShift2Idx][u>>m_nUShift2Idx][v>>m_nVShift2Idx];
   SYUVP dst = rCuboid.P[0];
@@ -251,4 +320,5 @@
                     + ( ( deltaV * ( rCuboid.P[2].V - rCuboid.P[1].V ) ) << m_nYShift2Idx ) 
                     + m_nMappingOffset ) >> m_nMappingShift );
+#endif
   return( dst );
 }
@@ -262,9 +332,83 @@
 {
   assert( pSrc->getMaxOctantDepth() == getMaxOctantDepth() && pSrc->getMaxYPartNumLog2() == getMaxYPartNumLog2() );
-  xUpdatePartitioning( pSrc->getCurOctantDepth() , pSrc->getCurYPartNumLog2() );
+  xUpdatePartitioning( pSrc->getCurOctantDepth() , pSrc->getCurYPartNumLog2() 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+    , pSrc->getAdaptChromaThresholdU() , pSrc->getAdaptChromaThresholdV()
+#endif
+    );
   setResQuantBit( pSrc->getResQuantBit() );
   xSaveCuboids( pSrc->m_pCuboid );
 }
 
-#endif
-
+#if R0164_CGS_LUT_BUGFIX
+Void TCom3DAsymLUT::xInitCuboids( )
+{
+  // All vertices are initialized as non-exlicitly-encoded
+  for( Int yIdx = 0 ; yIdx < m_nYSize ; yIdx++ )
+  {
+    for( Int uIdx = 0 ; uIdx < m_nUSize ; uIdx++ )
+    {
+      for( Int vIdx = 0 ; vIdx < m_nVSize ; vIdx++ )
+      { 
+        m_pCuboidExplicit[yIdx][uIdx][vIdx] = false;
+      }
+    }
+  }
+}
+
+Void TCom3DAsymLUT::xCuboidsExplicitCheck( Int yIdx , Int uIdx , Int vIdx )
+{
+  if ( m_pCuboidExplicit[yIdx][uIdx][vIdx] == false )
+  {
+    if( yIdx > 0) 
+      assert ( m_pCuboidExplicit[yIdx-1][uIdx][vIdx] );
+
+    for ( Int nVertexIdx=0 ; nVertexIdx<4 ; nVertexIdx++ )
+      m_pCuboid[yIdx][uIdx][vIdx].P[nVertexIdx] = yIdx == 0 ? xGetCuboidVertexPredA( yIdx , uIdx , vIdx , nVertexIdx ): xGetCuboidVertexPredAll( yIdx , uIdx , vIdx , nVertexIdx );
+
+    m_pCuboidExplicit[yIdx][uIdx][vIdx] = true ;
+  }
+}
+
+
+Void TCom3DAsymLUT::xCuboidsExplicitCheck( Bool bDecode )
+{
+  Int ySize = 1 << ( getCurOctantDepth() + getCurYPartNumLog2() );
+  Int uSize = 1 << getCurOctantDepth();
+  Int vSize = 1 << getCurOctantDepth();
+  for( Int yIdx = 0 ; yIdx < ySize ; yIdx++ )
+  {
+    for( Int uIdx = 0 ; uIdx < uSize ; uIdx++ )
+    {
+      for( Int vIdx = 0 ; vIdx < vSize ; vIdx++ )
+      { 
+        if ( bDecode )
+          xCuboidsExplicitCheck( yIdx , uIdx , vIdx );
+
+        assert( m_pCuboidExplicit[yIdx][uIdx][vIdx] );
+      }
+    }
+  }
+
+}
+#endif
+
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+Bool TCom3DAsymLUT::isRefLayer( UInt uiRefLayerId )
+{
+  Bool bIsRefLayer = false;
+  for( UInt i = 0 ; i < m_vRefLayerId.size() ; i++ )
+  {
+    if( m_vRefLayerId[i] == uiRefLayerId )
+    {
+      bIsRefLayer = true;
+      break;
+    }
+  }
+
+  return( bIsRefLayer );
+}
+#endif
+
+#endif
+
Index: /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.h	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.h	(revision 825)
@@ -4,4 +4,8 @@
 
 #include "TypeDef.h"
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+#include <cassert>
+#include <vector>
+#endif
 
 #if Q0048_CGS_3D_ASYMLUT
@@ -25,5 +29,9 @@
   virtual ~TCom3DAsymLUT();
 
-  virtual Void  create( Int nMaxOctantDepth , Int nInputBitDepth , Int nInputBitDepthC , Int nOutputBitDepth , Int nOutputBitDepthC , Int nMaxYPartNumLog2 );
+  virtual Void  create( Int nMaxOctantDepth , Int nInputBitDepth , Int nInputBitDepthC , Int nOutputBitDepth , Int nOutputBitDepthC , Int nMaxYPartNumLog2 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  , Int nAdaptCThresholdU , Int nAdaptCThresholdV
+#endif
+    );
   virtual Void  destroy();
 
@@ -38,4 +46,20 @@
   Int   getMaxYPartNumLog2() { return m_nMaxYPartNumLog2; }
   Int   getCurYPartNumLog2() { return m_nCurYPartNumLog2; }
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  Void  addRefLayerId( UInt uiRefLayerId )  
+  { 
+    if( !isRefLayer( uiRefLayerId ) )
+      m_vRefLayerId.push_back( uiRefLayerId ); 
+  }
+  size_t  getRefLayerNum()     { return m_vRefLayerId.size();  }
+  UInt  getRefLayerId( UInt n )  { assert( n < m_vRefLayerId.size() ); return m_vRefLayerId[n];   }
+  Bool  isRefLayer( UInt uiRefLayerId );
+#endif
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Void  setAdaptChromaThresholdU( Int n ) { m_nAdaptCThresholdU = n; }
+  Int   getAdaptChromaThresholdU()        { return m_nAdaptCThresholdU; }
+  Void  setAdaptChromaThresholdV( Int n ) { m_nAdaptCThresholdV = n;  }
+  Int   getAdaptChromaThresholdV()        { return m_nAdaptCThresholdV; }
+#endif
 
   Void  colorMapping( TComPicYuv * pcPicSrc,  TComPicYuv * pcPicDst );
@@ -45,5 +69,10 @@
   SYUVP getCuboidVertexResTree( Int yIdx , Int uIdx , Int vIdx , Int nVertexIdx );
   Void  setCuboidVertexResTree( Int yIdx , Int uIdx , Int vIdx , Int nVertexIdx , Int deltaY , Int deltaU , Int deltaV );
-
+#if R0164_CGS_LUT_BUGFIX
+  Void  xInitCuboids( );
+  Void  xCuboidsExplicitCheck( Int yIdx , Int uIdx , Int vIdx );
+  Void  xCuboidsExplicitCheck( Bool bDecode );
+  Void  xSetExplicit( Int yIdx , Int uIdx , Int vIdx )  { m_pCuboidExplicit[yIdx][uIdx][vIdx] = true ;}
+#endif
 
 private:
@@ -70,4 +99,14 @@
   SCuboid *** m_pCuboid;
   const static Int m_nVertexIdxOffset[4][3];
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  std::vector<UInt> m_vRefLayerId;
+#endif
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Int   m_nAdaptCThresholdU;
+  Int   m_nAdaptCThresholdV;
+#endif
+#if R0164_CGS_LUT_BUGFIX
+  Bool  *** m_pCuboidExplicit;
+#endif
 
 protected:
@@ -79,5 +118,9 @@
   Void xFree3DArray( T *** &p );
 
-  Void  xUpdatePartitioning( Int nCurOctantDepth , Int nCurYPartNumLog2 );
+  Void  xUpdatePartitioning( Int nCurOctantDepth , Int nCurYPartNumLog2 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+    , Int nAdaptCThresholdU , Int nAdaptCThreshodV
+#endif
+    );
   SYUVP xGetCuboidVertexPredA( Int yIdx , Int uIdx , Int vIdx , Int nVertexIdx );
   Pel   xMapY( Pel y , Pel u , Pel v );
@@ -87,7 +130,14 @@
   Int   xGetUSize()  { return m_nUSize;  }
   Int   xGetVSize()  { return m_nVSize;  }
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Int   xGetYIdx(Pel y)  { return( y >> m_nYShift2Idx ); }
+  Int   xGetUIdx(Pel u)  { return( m_nCurOctantDepth == 1 ? u >= m_nAdaptCThresholdU : u >> m_nUShift2Idx ); }
+  Int   xGetVIdx(Pel v)  { return( m_nCurOctantDepth == 1 ? v >= m_nAdaptCThresholdV : v >> m_nVShift2Idx ); }
+  Int   xGetNormCoeffOne()    { return( 1 << m_nMappingShift ); }
+#else
   Int   xGetYShift2Idx() { return m_nYShift2Idx; }
   Int   xGetUShift2Idx() { return m_nUShift2Idx; }
   Int   xGetVShift2Idx() { return m_nVShift2Idx; } 
+#endif
   SCuboid & xGetCuboid( Int yIdx , Int uIdx , Int vIdx ){ return m_pCuboid[yIdx][uIdx][vIdx];  }
   Void  xSaveCuboids( SCuboid *** pSrcCuboid );
Index: /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.h	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.h	(revision 825)
@@ -2626,2 +2626,3 @@
 
 #endif // __TCOMSLICE__
+
Index: /branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h	(revision 825)
@@ -93,8 +93,9 @@
 #if Q0048_CGS_3D_ASYMLUT
 #define CGS_GCC_NO_VECTORIZATION         1
-#define O0194_WEIGHTED_PREDICTION_CGS    0
-#else
+#define R0150_CGS_SIGNAL_CONSTRAINTS     1      ///< JCTVC-R0150: CGS signaling improvement and constraints
+#define R0151_CGS_3D_ASYMLUT_IMPROVE     1      ///< JCTVC-R0151: Non-uniform chroma partitioning and improved LUT coefficient coding
+#define R0164_CGS_LUT_BUGFIX             1      ///< JCTVC-R0164: Bug fix with LUT syntax
+#endif
 #define O0194_WEIGHTED_PREDICTION_CGS    1      ///< JCTVC-O0194: Weighted prediciton for color gamut scalability
-#endif
 #define POC_RESET_FLAG                   0      ///< JCTVC-N0244: POC reset flag for  layer pictures.
 #define POC_RESET_IDC                    1      ///< JCTVC-P0041: Include poc_reset_idc and related derivation - eventually will replace POC_RESET_FLAG
Index: /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 825)
@@ -316,10 +316,10 @@
 #endif
 
-  READ_FLAG( uiCode, "pps_scaling_list_data_present_flag" );           pcPPS->setScalingListPresentFlag( uiCode ? true : false );
-
-  if(pcPPS->getScalingListPresentFlag ())
-  {
-    parseScalingList( pcPPS->getScalingList() );
-  }
+    READ_FLAG( uiCode, "pps_scaling_list_data_present_flag" );           pcPPS->setScalingListPresentFlag( uiCode ? true : false );
+
+    if(pcPPS->getScalingListPresentFlag ())
+    {
+      parseScalingList( pcPPS->getScalingList() );
+    }
 
 #if SCALINGLIST_INFERRING
@@ -381,8 +381,8 @@
 #endif
 
-    while ( xMoreRbspData() )
-    {
-      READ_FLAG( uiCode, "pps_extension_data_flag");
-    }
+      while ( xMoreRbspData() )
+      {
+        READ_FLAG( uiCode, "pps_extension_data_flag");
+      }
 #if P0166_MODIFIED_PPS_EXTENSION
     }
@@ -474,9 +474,9 @@
       READ_UVLC(   uiCode, "vui_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
     }
-  READ_FLAG(     uiCode, "hrd_parameters_present_flag");              pcVUI->setHrdParametersPresentFlag(uiCode);
-  if( pcVUI->getHrdParametersPresentFlag() )
-  {
-    parseHrdParameters( pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
-  }
+    READ_FLAG(     uiCode, "hrd_parameters_present_flag");              pcVUI->setHrdParametersPresentFlag(uiCode);
+    if( pcVUI->getHrdParametersPresentFlag() )
+    {
+      parseHrdParameters( pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
+    }
   }
   READ_FLAG(     uiCode, "bitstream_restriction_flag");               pcVUI->setBitstreamRestrictionFlag(uiCode);
@@ -552,5 +552,5 @@
     {
       if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
-          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
+        ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
       {
         for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
@@ -747,10 +747,10 @@
   READ_UVLC( uiCode, "log2_diff_max_min_coding_block_size" );
   pcSPS->setLog2DiffMaxMinCodingBlockSize(uiCode);
-  
+
   if (pcSPS->getPTL()->getGeneralPTL()->getLevelIdc() >= Level::LEVEL5)
   {
     assert(log2MinCUSize + pcSPS->getLog2DiffMaxMinCodingBlockSize() >= 5);
   }
-  
+
   Int maxCUDepthDelta = uiCode;
   pcSPS->setMaxCUWidth  ( 1<<(log2MinCUSize + maxCUDepthDelta) );
@@ -788,9 +788,9 @@
     {
 #endif
-    READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
-    if(pcSPS->getScalingListPresentFlag ())
-    {
-      parseScalingList( pcSPS->getScalingList() );
-    }
+      READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
+      if(pcSPS->getScalingListPresentFlag ())
+      {
+        parseScalingList( pcSPS->getScalingList() );
+      }
 #if SCALINGLIST_INFERRING
     }
@@ -1097,5 +1097,5 @@
 #endif
 #if VPS_VUI_OFFSET
-  READ_CODE( 16, uiCode, "vps_vui_offset" );  vps->setVpsVuiOffset( uiCode );
+    READ_CODE( 16, uiCode, "vps_vui_offset" );  vps->setVpsVuiOffset( uiCode );
 #endif
 #if O0109_MOVE_VPS_VUI_FLAG
@@ -1148,12 +1148,12 @@
     if( !vps->getSplittingFlag() )
     {
-    for(j = 0; j < numScalabilityTypes; j++)
-    {
-      READ_CODE( vps->getDimensionIdLen(j), uiCode, "dimension_id[i][j]" ); vps->setDimensionId(i, j, uiCode);
+      for(j = 0; j < numScalabilityTypes; j++)
+      {
+        READ_CODE( vps->getDimensionIdLen(j), uiCode, "dimension_id[i][j]" ); vps->setDimensionId(i, j, uiCode);
 #if !AUXILIARY_PICTURES
-      assert( uiCode <= vps->getMaxLayerId() );
-#endif
-    }
-  }
+        assert( uiCode <= vps->getMaxLayerId() );
+#endif
+      }
+    }
   }
 #endif
@@ -1242,12 +1242,12 @@
     {
 #if O0225_MAX_TID_FOR_REF_LAYERS
-       for( j = i+1; j <= vps->getMaxLayers() - 1; j++)
-       {
-         if(vps->getDirectDependencyFlag(j, i))
-         {
-           READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1[i][j]" ); vps->setMaxTidIlRefPicsPlus1(i, j, uiCode);
-           assert( uiCode <= vps->getMaxTLayers());
-         }
-       }
+      for( j = i+1; j <= vps->getMaxLayers() - 1; j++)
+      {
+        if(vps->getDirectDependencyFlag(j, i))
+        {
+          READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1[i][j]" ); vps->setMaxTidIlRefPicsPlus1(i, j, uiCode);
+          assert( uiCode <= vps->getMaxTLayers());
+        }
+      }
 #else
       READ_CODE( 3, uiCode, "max_tid_il_ref_pics_plus1[i]" ); vps->setMaxTidIlRefPicsPlus1(i, uiCode);
@@ -1261,8 +1261,8 @@
     {
 #if O0225_MAX_TID_FOR_REF_LAYERS
-       for( j = i+1; j <= vps->getMaxLayers() - 1; j++)
-       {
-          vps->setMaxTidIlRefPicsPlus1(i, j, 7);
-       }
+      for( j = i+1; j <= vps->getMaxLayers() - 1; j++)
+      {
+        vps->setMaxTidIlRefPicsPlus1(i, j, 7);
+      }
 #else
       vps->setMaxTidIlRefPicsPlus1(i, 7);
@@ -1827,5 +1827,5 @@
 #endif
   }
-  
+
   // When not present, the value of num_add_olss is inferred to be equal to 0.
   // NumOutputLayerSets = num_add_olss + NumLayerSets
@@ -1841,5 +1841,5 @@
     for( j = 0; j < vps->getNumLayersInIdList(lsIdx); j++ )
     {
-    vps->setOutputLayerFlag(i, j, 1);
+      vps->setOutputLayerFlag(i, j, 1);
     }
   }
@@ -1851,5 +1851,5 @@
     vps->setSubLayerDpbInfoPresentFlag( i, 0, true );
   }
-  
+
   // When not present, the value of vps_num_rep_formats_minus1 is inferred to be equal to MaxLayersMinus1.
   vps->setVpsNumRepFormats( vps->getMaxLayers() );
@@ -1869,5 +1869,5 @@
   // vps_poc_lsb_aligned_flag
   // When not present, vps_poc_lsb_aligned_flag is inferred to be equal to 0.
-  
+
 #if O0062_POC_LSB_NOT_PRESENT_FLAG
   // When not present, poc_lsb_not_present_flag[ i ] is inferred to be equal to 0.
@@ -1946,5 +1946,5 @@
   READ_CODE( 2, uiCode, "chroma_format_idc" );               repFormat->setChromaFormatVpsIdc( uiCode );
 #endif
-  
+
   if( repFormat->getChromaFormatVpsIdc() == 3 )
   {
@@ -1966,31 +1966,31 @@
 #if DPB_PARAMS_MAXTLAYERS
 #if BITRATE_PICRATE_SIGNALLING
-    Int * MaxSubLayersInLayerSetMinus1 = new Int[vps->getNumLayerSets()];
-    for(Int i = 0; i < vps->getNumLayerSets(); i++)
-#else
-    Int * MaxSubLayersInLayerSetMinus1 = new Int[vps->getNumOutputLayerSets()];
-    for(Int i = 1; i < vps->getNumOutputLayerSets(); i++)
-#endif
-    {
-        UInt maxSLMinus1 = 0;
+  Int * MaxSubLayersInLayerSetMinus1 = new Int[vps->getNumLayerSets()];
+  for(Int i = 0; i < vps->getNumLayerSets(); i++)
+#else
+  Int * MaxSubLayersInLayerSetMinus1 = new Int[vps->getNumOutputLayerSets()];
+  for(Int i = 1; i < vps->getNumOutputLayerSets(); i++)
+#endif
+  {
+    UInt maxSLMinus1 = 0;
 #if CHANGE_NUMSUBDPB_IDX
-        Int optLsIdx = vps->getOutputLayerSetIdx( i );
-#else
-        Int optLsIdx = i;
+    Int optLsIdx = vps->getOutputLayerSetIdx( i );
+#else
+    Int optLsIdx = i;
 #endif
 #if BITRATE_PICRATE_SIGNALLING
-        optLsIdx = i;
-#endif
-        for(Int k = 0; k < vps->getNumLayersInIdList(optLsIdx); k++ ) {
-            Int  lId = vps->getLayerSetLayerIdList(optLsIdx, k);
-            maxSLMinus1 = max(maxSLMinus1, vps->getMaxTSLayersMinus1(vps->getLayerIdInVps(lId)));
-        }
-        MaxSubLayersInLayerSetMinus1[ i ] = maxSLMinus1;
+    optLsIdx = i;
+#endif
+    for(Int k = 0; k < vps->getNumLayersInIdList(optLsIdx); k++ ) {
+      Int  lId = vps->getLayerSetLayerIdList(optLsIdx, k);
+      maxSLMinus1 = max(maxSLMinus1, vps->getMaxTSLayersMinus1(vps->getLayerIdInVps(lId)));
+    }
+    MaxSubLayersInLayerSetMinus1[ i ] = maxSLMinus1;
 #if BITRATE_PICRATE_SIGNALLING
-        vps->setMaxSLayersInLayerSetMinus1(i,MaxSubLayersInLayerSetMinus1[ i ]);
-#endif
-    }
-#endif
-    
+    vps->setMaxSLayersInLayerSetMinus1(i,MaxSubLayersInLayerSetMinus1[ i ]);
+#endif
+  }
+#endif
+
 #if !RESOLUTION_BASED_DPB
   vps->deriveNumberOfSubDpbs();
@@ -2313,142 +2313,142 @@
 #if VPS_VUI_VIDEO_SIGNAL_MOVE
 #else
-    READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); vps->setVideoSigPresentVpsFlag( uiCode == 1 );
-    if (vps->getVideoSigPresentVpsFlag())
-    {
-        READ_CODE(4, uiCode, "vps_num_video_signal_info_minus1" ); vps->setNumVideoSignalInfo(uiCode + 1);
+  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); vps->setVideoSigPresentVpsFlag( uiCode == 1 );
+  if (vps->getVideoSigPresentVpsFlag())
+  {
+    READ_CODE(4, uiCode, "vps_num_video_signal_info_minus1" ); vps->setNumVideoSignalInfo(uiCode + 1);
+  }
+  else
+  {
+    vps->setNumVideoSignalInfo(vps->getMaxLayers());
+  }
+
+
+  for(i = 0; i < vps->getNumVideoSignalInfo(); i++)
+  {
+    READ_CODE(3, uiCode, "video_vps_format" ); vps->setVideoVPSFormat(i,uiCode);
+    READ_FLAG(uiCode, "video_full_range_vps_flag" ); vps->setVideoFullRangeVpsFlag(i,uiCode);
+    READ_CODE(8, uiCode, "color_primaries_vps" ); vps->setColorPrimaries(i,uiCode);
+    READ_CODE(8, uiCode, "transfer_characteristics_vps" ); vps->setTransCharacter(i,uiCode);
+    READ_CODE(8, uiCode, "matrix_coeffs_vps" );vps->setMaxtrixCoeff(i,uiCode);
+  }
+  if(!vps->getVideoSigPresentVpsFlag())
+  {
+    for (i=0; i < vps->getMaxLayers(); i++)
+    {
+      vps->setVideoSignalInfoIdx(i,i);
+    }
+  }
+  else {
+    vps->setVideoSignalInfoIdx(0,0);
+    if (vps->getNumVideoSignalInfo() > 1 )
+    {
+      for (i=1; i < vps->getMaxLayers(); i++)
+        READ_CODE(4, uiCode, "vps_video_signal_info_idx" ); vps->setVideoSignalInfoIdx(i, uiCode);
+    }
+    else {
+      for (i=1; i < vps->getMaxLayers(); i++)
+      {
+        vps->setVideoSignalInfoIdx(i,0);
+      }
+    }
+  }
+#endif 
+#endif
+
+#if O0164_MULTI_LAYER_HRD
+  READ_FLAG(uiCode, "vps_vui_bsp_hrd_present_flag" ); vps->setVpsVuiBspHrdPresentFlag(uiCode);
+  if (vps->getVpsVuiBspHrdPresentFlag())
+  {
+    READ_UVLC( uiCode, "vps_num_bsp_hrd_parameters_minus1" ); vps->setVpsNumBspHrdParametersMinus1(uiCode);
+    vps->createBspHrdParamBuffer(vps->getVpsNumBspHrdParametersMinus1() + 1);
+    for( i = 0; i <= vps->getVpsNumBspHrdParametersMinus1(); i++ )
+    {
+      if( i > 0 )
+      {
+        READ_FLAG( uiCode, "bsp_cprms_present_flag[i]" ); vps->setBspCprmsPresentFlag(i, uiCode);
+      }
+      parseHrdParameters(vps->getBspHrd(i), i==0 ? 1 : vps->getBspCprmsPresentFlag(i), vps->getMaxTLayers()-1);
+    }
+#if Q0078_ADD_LAYER_SETS
+    for (UInt h = 1; h <= vps->getVpsNumLayerSetsMinus1(); h++)
+#else
+    for( UInt h = 1; h <= (vps->getNumLayerSets()-1); h++ )
+#endif
+    {
+      READ_UVLC( uiCode, "num_bitstream_partitions[i]"); vps->setNumBitstreamPartitions(h, uiCode);
+#if HRD_BPB
+      Int chkPart=0;
+#endif
+      for( i = 0; i < vps->getNumBitstreamPartitions(h); i++ )
+      {
+        for( j = 0; j <= (vps->getMaxLayers()-1); j++ )
+        {
+          if( vps->getLayerIdIncludedFlag(h, j) )
+          {
+            READ_FLAG( uiCode, "layer_in_bsp_flag[h][i][j]" ); vps->setLayerInBspFlag(h, i, j, uiCode);
+          }
+        }
+#if HRD_BPB
+        chkPart+=vps->getLayerInBspFlag(h, i, j);
+#endif
+      }
+#if HRD_BPB
+      assert(chkPart<=1);
+#endif
+#if HRD_BPB
+      if(vps->getNumBitstreamPartitions(h)==1)
+      {
+        Int chkPartition1=0; Int chkPartition2=0;
+        for( j = 0; j <= (vps->getMaxLayers()-1); j++ )
+        {
+          if( vps->getLayerIdIncludedFlag(h, j) )
+          {
+            chkPartition1+=vps->getLayerInBspFlag(h, 0, j);
+            chkPartition2++;
+          }
+        }
+        assert(chkPartition1!=chkPartition2);
+      }
+#endif
+      if (vps->getNumBitstreamPartitions(h))
+      {
+#if Q0182_MULTI_LAYER_HRD_UPDATE
+        READ_UVLC( uiCode, "num_bsp_sched_combinations_minus1[h]"); vps->setNumBspSchedCombinations(h, uiCode + 1);
+#else
+        READ_UVLC( uiCode, "num_bsp_sched_combinations[h]"); vps->setNumBspSchedCombinations(h, uiCode);
+#endif
+        for( i = 0; i < vps->getNumBspSchedCombinations(h); i++ )
+        {
+          for( j = 0; j < vps->getNumBitstreamPartitions(h); j++ )
+          {
+            READ_UVLC( uiCode, "bsp_comb_hrd_idx[h][i][j]"); vps->setBspCombHrdIdx(h, i, j, uiCode);
+#if HRD_BPB
+            assert(uiCode <= vps->getVpsNumBspHrdParametersMinus1());
+#endif
+
+            READ_UVLC( uiCode, "bsp_comb_sched_idx[h][i][j]"); vps->setBspCombSchedIdx(h, i, j, uiCode);
+#if HRD_BPB
+            assert(uiCode <= vps->getBspHrdParamBufferCpbCntMinus1(uiCode,vps->getMaxTLayers()-1));
+#endif
+          }
+        }
+      }
+    }
+  }
+#endif
+
+#if P0182_VPS_VUI_PS_FLAG
+  for(i = 1; i < vps->getMaxLayers(); i++)
+  {
+    if (vps->getNumRefLayers(vps->getLayerIdInNuh(i)) == 0)
+    {
+      READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); 
+      vps->setBaseLayerPSCompatibilityFlag( i, uiCode );
     }
     else
     {
-        vps->setNumVideoSignalInfo(vps->getMaxLayers());
-    }
-    
-    
-    for(i = 0; i < vps->getNumVideoSignalInfo(); i++)
-    {
-        READ_CODE(3, uiCode, "video_vps_format" ); vps->setVideoVPSFormat(i,uiCode);
-        READ_FLAG(uiCode, "video_full_range_vps_flag" ); vps->setVideoFullRangeVpsFlag(i,uiCode);
-        READ_CODE(8, uiCode, "color_primaries_vps" ); vps->setColorPrimaries(i,uiCode);
-        READ_CODE(8, uiCode, "transfer_characteristics_vps" ); vps->setTransCharacter(i,uiCode);
-        READ_CODE(8, uiCode, "matrix_coeffs_vps" );vps->setMaxtrixCoeff(i,uiCode);
-    }
-    if(!vps->getVideoSigPresentVpsFlag())
-    {
-        for (i=0; i < vps->getMaxLayers(); i++)
-        {
-            vps->setVideoSignalInfoIdx(i,i);
-        }
-    }
-    else {
-        vps->setVideoSignalInfoIdx(0,0);
-        if (vps->getNumVideoSignalInfo() > 1 )
-        {
-            for (i=1; i < vps->getMaxLayers(); i++)
-                READ_CODE(4, uiCode, "vps_video_signal_info_idx" ); vps->setVideoSignalInfoIdx(i, uiCode);
-        }
-        else {
-          for (i=1; i < vps->getMaxLayers(); i++)
-          {
-            vps->setVideoSignalInfoIdx(i,0);
-          }
-        }
-    }
-#endif 
-#endif
-
-#if O0164_MULTI_LAYER_HRD
-    READ_FLAG(uiCode, "vps_vui_bsp_hrd_present_flag" ); vps->setVpsVuiBspHrdPresentFlag(uiCode);
-    if (vps->getVpsVuiBspHrdPresentFlag())
-    {
-      READ_UVLC( uiCode, "vps_num_bsp_hrd_parameters_minus1" ); vps->setVpsNumBspHrdParametersMinus1(uiCode);
-      vps->createBspHrdParamBuffer(vps->getVpsNumBspHrdParametersMinus1() + 1);
-      for( i = 0; i <= vps->getVpsNumBspHrdParametersMinus1(); i++ )
-      {
-        if( i > 0 )
-        {
-          READ_FLAG( uiCode, "bsp_cprms_present_flag[i]" ); vps->setBspCprmsPresentFlag(i, uiCode);
-        }
-        parseHrdParameters(vps->getBspHrd(i), i==0 ? 1 : vps->getBspCprmsPresentFlag(i), vps->getMaxTLayers()-1);
-      }
-#if Q0078_ADD_LAYER_SETS
-      for (UInt h = 1; h <= vps->getVpsNumLayerSetsMinus1(); h++)
-#else
-      for( UInt h = 1; h <= (vps->getNumLayerSets()-1); h++ )
-#endif
-      {
-        READ_UVLC( uiCode, "num_bitstream_partitions[i]"); vps->setNumBitstreamPartitions(h, uiCode);
-#if HRD_BPB
-        Int chkPart=0;
-#endif
-        for( i = 0; i < vps->getNumBitstreamPartitions(h); i++ )
-        {
-          for( j = 0; j <= (vps->getMaxLayers()-1); j++ )
-          {
-            if( vps->getLayerIdIncludedFlag(h, j) )
-            {
-              READ_FLAG( uiCode, "layer_in_bsp_flag[h][i][j]" ); vps->setLayerInBspFlag(h, i, j, uiCode);
-            }
-          }
-#if HRD_BPB
-          chkPart+=vps->getLayerInBspFlag(h, i, j);
-#endif
-        }
-#if HRD_BPB
-        assert(chkPart<=1);
-#endif
-#if HRD_BPB
-        if(vps->getNumBitstreamPartitions(h)==1)
-        {
-          Int chkPartition1=0; Int chkPartition2=0;
-          for( j = 0; j <= (vps->getMaxLayers()-1); j++ )
-          {
-            if( vps->getLayerIdIncludedFlag(h, j) )
-            {
-              chkPartition1+=vps->getLayerInBspFlag(h, 0, j);
-              chkPartition2++;
-            }
-          }
-          assert(chkPartition1!=chkPartition2);
-        }
-#endif
-        if (vps->getNumBitstreamPartitions(h))
-        {
-#if Q0182_MULTI_LAYER_HRD_UPDATE
-          READ_UVLC( uiCode, "num_bsp_sched_combinations_minus1[h]"); vps->setNumBspSchedCombinations(h, uiCode + 1);
-#else
-          READ_UVLC( uiCode, "num_bsp_sched_combinations[h]"); vps->setNumBspSchedCombinations(h, uiCode);
-#endif
-          for( i = 0; i < vps->getNumBspSchedCombinations(h); i++ )
-          {
-            for( j = 0; j < vps->getNumBitstreamPartitions(h); j++ )
-            {
-              READ_UVLC( uiCode, "bsp_comb_hrd_idx[h][i][j]"); vps->setBspCombHrdIdx(h, i, j, uiCode);
-#if HRD_BPB
-              assert(uiCode <= vps->getVpsNumBspHrdParametersMinus1());
-#endif
-                
-              READ_UVLC( uiCode, "bsp_comb_sched_idx[h][i][j]"); vps->setBspCombSchedIdx(h, i, j, uiCode);
-#if HRD_BPB
-              assert(uiCode <= vps->getBspHrdParamBufferCpbCntMinus1(uiCode,vps->getMaxTLayers()-1));
-#endif
-            }
-          }
-        }
-      }
-    }
-#endif
-
-#if P0182_VPS_VUI_PS_FLAG
-    for(i = 1; i < vps->getMaxLayers(); i++)
-    {
-      if (vps->getNumRefLayers(vps->getLayerIdInNuh(i)) == 0)
-      {
-        READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); 
-        vps->setBaseLayerPSCompatibilityFlag( i, uiCode );
-      }
-      else
-      {
-        vps->setBaseLayerPSCompatibilityFlag( i, 0 );
-      }
-    }
+      vps->setBaseLayerPSCompatibilityFlag( i, 0 );
+    }
+  }
 #endif
 }
@@ -2532,5 +2532,5 @@
 #if Q0142_POC_LSB_NOT_PRESENT
 #if SHM_FIX7
-    Int iPOClsb = 0;
+  Int iPOClsb = 0;
 #endif
 #endif
@@ -2659,254 +2659,254 @@
 #if N0065_LAYER_POC_ALIGNMENT
 #if SHM_FIX7
-      }
+    }
 #endif
 #if POC_RESET_IDC_DECODER
-    else
-    {
-      rpcSlice->setPicOrderCntLsb( 0 );
-    }
-#endif
-      if( !rpcSlice->getIdrPicFlag() )
-      {
-#endif
-      TComReferencePictureSet* rps;
-      rps = rpcSlice->getLocalRPS();
-      rpcSlice->setRPS(rps);
-      READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" );
-      if(uiCode == 0) // use short-term reference picture set explicitly signalled in slice header
-      {
-        parseShortTermRefPicSet(sps,rps, sps->getRPSList()->getNumberOfReferencePictureSets());
-      }
-      else // use reference to short-term reference picture set in PPS
-      {
-        Int numBits = 0;
-        while ((1 << numBits) < rpcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
-        {
-          numBits++;
-        }
-        if (numBits > 0)
-        {
-          READ_CODE( numBits, uiCode, "short_term_ref_pic_set_idx");
+  else
+  {
+    rpcSlice->setPicOrderCntLsb( 0 );
+  }
+#endif
+  if( !rpcSlice->getIdrPicFlag() )
+  {
+#endif
+    TComReferencePictureSet* rps;
+    rps = rpcSlice->getLocalRPS();
+    rpcSlice->setRPS(rps);
+    READ_FLAG( uiCode, "short_term_ref_pic_set_sps_flag" );
+    if(uiCode == 0) // use short-term reference picture set explicitly signalled in slice header
+    {
+      parseShortTermRefPicSet(sps,rps, sps->getRPSList()->getNumberOfReferencePictureSets());
+    }
+    else // use reference to short-term reference picture set in PPS
+    {
+      Int numBits = 0;
+      while ((1 << numBits) < rpcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
+      {
+        numBits++;
+      }
+      if (numBits > 0)
+      {
+        READ_CODE( numBits, uiCode, "short_term_ref_pic_set_idx");
+      }
+      else
+      {
+        uiCode = 0;       
+      }
+      *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
+    }
+    if(sps->getLongTermRefsPresent())
+    {
+      Int offset = rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures();
+      UInt numOfLtrp = 0;
+      UInt numLtrpInSPS = 0;
+      if (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
+      {
+        READ_UVLC( uiCode, "num_long_term_sps");
+        numLtrpInSPS = uiCode;
+        numOfLtrp += numLtrpInSPS;
+        rps->setNumberOfLongtermPictures(numOfLtrp);
+      }
+      Int bitsForLtrpInSPS = 0;
+      while (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
+      {
+        bitsForLtrpInSPS++;
+      }
+      READ_UVLC( uiCode, "num_long_term_pics");             rps->setNumberOfLongtermPictures(uiCode);
+      numOfLtrp += uiCode;
+      rps->setNumberOfLongtermPictures(numOfLtrp);
+      Int maxPicOrderCntLSB = 1 << rpcSlice->getSPS()->getBitsForPOC();
+      Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;;
+      for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
+      {
+        Int pocLsbLt;
+        if (k < numLtrpInSPS)
+        {
+          uiCode = 0;
+          if (bitsForLtrpInSPS > 0)
+          {
+            READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
+          }
+          Int usedByCurrFromSPS=rpcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(uiCode);
+
+          pocLsbLt = rpcSlice->getSPS()->getLtRefPicPocLsbSps(uiCode);
+          rps->setUsed(j,usedByCurrFromSPS);
         }
         else
         {
-          uiCode = 0;       
-        }
-        *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
-      }
-      if(sps->getLongTermRefsPresent())
-      {
-        Int offset = rps->getNumberOfNegativePictures()+rps->getNumberOfPositivePictures();
-        UInt numOfLtrp = 0;
-        UInt numLtrpInSPS = 0;
-        if (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
-        {
-          READ_UVLC( uiCode, "num_long_term_sps");
-          numLtrpInSPS = uiCode;
-          numOfLtrp += numLtrpInSPS;
-          rps->setNumberOfLongtermPictures(numOfLtrp);
-        }
-        Int bitsForLtrpInSPS = 0;
-        while (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
-        {
-          bitsForLtrpInSPS++;
-        }
-        READ_UVLC( uiCode, "num_long_term_pics");             rps->setNumberOfLongtermPictures(uiCode);
-        numOfLtrp += uiCode;
-        rps->setNumberOfLongtermPictures(numOfLtrp);
-        Int maxPicOrderCntLSB = 1 << rpcSlice->getSPS()->getBitsForPOC();
-        Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;;
-        for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
-        {
-          Int pocLsbLt;
-          if (k < numLtrpInSPS)
+          READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
+          READ_FLAG( uiCode, "used_by_curr_pic_lt_flag");     rps->setUsed(j,uiCode);
+        }
+        READ_FLAG(uiCode,"delta_poc_msb_present_flag");
+        Bool mSBPresentFlag = uiCode ? true : false;
+        if(mSBPresentFlag)
+        {
+          READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
+          Bool deltaFlag = false;
+          //            First LTRP                               || First LTRP from SH
+          if( (j == offset+rps->getNumberOfLongtermPictures()-1) || (j == offset+(numOfLtrp-numLtrpInSPS)-1) )
           {
-            uiCode = 0;
-            if (bitsForLtrpInSPS > 0)
-            {
-              READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
-            }
-            Int usedByCurrFromSPS=rpcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(uiCode);
-
-            pocLsbLt = rpcSlice->getSPS()->getLtRefPicPocLsbSps(uiCode);
-            rps->setUsed(j,usedByCurrFromSPS);
+            deltaFlag = true;
+          }
+          if(deltaFlag)
+          {
+            deltaPocMSBCycleLT = uiCode;
           }
           else
           {
-            READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
-            READ_FLAG( uiCode, "used_by_curr_pic_lt_flag");     rps->setUsed(j,uiCode);
+            deltaPocMSBCycleLT = uiCode + prevDeltaMSB;
           }
-          READ_FLAG(uiCode,"delta_poc_msb_present_flag");
-          Bool mSBPresentFlag = uiCode ? true : false;
-          if(mSBPresentFlag)
+
+          Int pocLTCurr = rpcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
+            - iPOClsb + pocLsbLt;
+          rps->setPOC     (j, pocLTCurr);
+          rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLTCurr);
+          rps->setCheckLTMSBPresent(j,true);
+        }
+        else
+        {
+          rps->setPOC     (j, pocLsbLt);
+          rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLsbLt);
+          rps->setCheckLTMSBPresent(j,false);
+
+          // reset deltaPocMSBCycleLT for first LTRP from slice header if MSB not present
+          if( j == offset+(numOfLtrp-numLtrpInSPS)-1 )
           {
-            READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
-            Bool deltaFlag = false;
-            //            First LTRP                               || First LTRP from SH
-            if( (j == offset+rps->getNumberOfLongtermPictures()-1) || (j == offset+(numOfLtrp-numLtrpInSPS)-1) )
-            {
-              deltaFlag = true;
-            }
-            if(deltaFlag)
-            {
-              deltaPocMSBCycleLT = uiCode;
-            }
-            else
-            {
-              deltaPocMSBCycleLT = uiCode + prevDeltaMSB;
-            }
-
-            Int pocLTCurr = rpcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
-                                        - iPOClsb + pocLsbLt;
-            rps->setPOC     (j, pocLTCurr);
-            rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLTCurr);
-            rps->setCheckLTMSBPresent(j,true);
+            deltaPocMSBCycleLT = 0;
           }
-          else
+        }
+        prevDeltaMSB = deltaPocMSBCycleLT;
+      }
+      offset += rps->getNumberOfLongtermPictures();
+      rps->setNumberOfPictures(offset);
+    }
+#if DPB_CONSTRAINTS
+    if(rpcSlice->getVPS()->getVpsExtensionFlag()==1)
+    {
+#if Q0078_ADD_LAYER_SETS
+      for (Int ii = 1; ii < (rpcSlice->getVPS()->getVpsNumLayerSetsMinus1() + 1); ii++)  // prevent assert error when num_add_layer_sets > 0
+#else
+      for (Int ii=1; ii< rpcSlice->getVPS()->getNumOutputLayerSets(); ii++ )
+#endif
+      {
+        Int layerSetIdxForOutputLayerSet = rpcSlice->getVPS()->getOutputLayerSetIdx( ii );
+        Int chkAssert=0;
+        for(Int kk = 0; kk < rpcSlice->getVPS()->getNumLayersInIdList(layerSetIdxForOutputLayerSet); kk++)
+        {
+          if(rpcSlice->getLayerId()==rpcSlice->getVPS()->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, kk))
           {
-            rps->setPOC     (j, pocLsbLt);
-            rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLsbLt);
-            rps->setCheckLTMSBPresent(j,false);
-
-            // reset deltaPocMSBCycleLT for first LTRP from slice header if MSB not present
-            if( j == offset+(numOfLtrp-numLtrpInSPS)-1 )
-            {
-              deltaPocMSBCycleLT = 0;
+            chkAssert=1;
+          }
+        }
+        if(chkAssert)
+        {
+          // There may be something wrong here (layer id assumed to be layer idx?)
+          assert(rps->getNumberOfNegativePictures() <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)));
+          assert(rps->getNumberOfPositivePictures() <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)) - rps->getNumberOfNegativePictures());
+          assert((rps->getNumberOfPositivePictures() + rps->getNumberOfNegativePictures() + rps->getNumberOfLongtermPictures()) <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)));
+        }
+      }
+
+
+    }
+    if(rpcSlice->getLayerId() == 0)
+    {
+      assert(rps->getNumberOfNegativePictures() <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1) );
+      assert(rps->getNumberOfPositivePictures() <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1) -rps->getNumberOfNegativePictures());
+      assert((rps->getNumberOfPositivePictures() + rps->getNumberOfNegativePictures() + rps->getNumberOfLongtermPictures()) <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1));
+    }
+#endif
+    if ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
+      || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
+      || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
+    {
+      // In the case of BLA picture types, rps data is read from slice header but ignored
+      rps = rpcSlice->getLocalRPS();
+      rps->setNumberOfNegativePictures(0);
+      rps->setNumberOfPositivePictures(0);
+      rps->setNumberOfLongtermPictures(0);
+      rps->setNumberOfPictures(0);
+      rpcSlice->setRPS(rps);
+    }
+    if (rpcSlice->getSPS()->getTMVPFlagsPresent())
+    {
+      READ_FLAG( uiCode, "slice_temporal_mvp_enable_flag" );
+      rpcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false );
+    }
+    else
+    {
+      rpcSlice->setEnableTMVPFlag(false);
+    }
+#if N0065_LAYER_POC_ALIGNMENT && !SHM_FIX7
+  }
+#endif
+  }
+
+#if SVC_EXTENSION
+  rpcSlice->setActiveNumILRRefIdx(0);
+  if((rpcSlice->getLayerId() > 0) && !(rpcSlice->getVPS()->getIlpSshSignalingEnabledFlag()) && (rpcSlice->getNumILRRefIdx() > 0) )
+  {
+    READ_FLAG(uiCode,"inter_layer_pred_enabled_flag");
+    rpcSlice->setInterLayerPredEnabledFlag(uiCode);
+    if( rpcSlice->getInterLayerPredEnabledFlag())
+    {
+      if(rpcSlice->getNumILRRefIdx() > 1)
+      {
+        Int numBits = 1;
+        while ((1 << numBits) < rpcSlice->getNumILRRefIdx())
+        {
+          numBits++;
+        }
+        if( !rpcSlice->getVPS()->getMaxOneActiveRefLayerFlag())
+        {
+          READ_CODE( numBits, uiCode,"num_inter_layer_ref_pics_minus1" );
+          rpcSlice->setActiveNumILRRefIdx(uiCode + 1);
+        }
+        else
+        {
+#if P0079_DERIVE_NUMACTIVE_REF_PICS
+          for( Int i = 0; i < rpcSlice->getNumILRRefIdx(); i++ ) 
+          {
+#if Q0060_MAX_TID_REF_EQUAL_TO_ZERO
+            if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
+              (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
+#else 
+            if(rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() &&
+              (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
+#endif 
+            {          
+              rpcSlice->setActiveNumILRRefIdx(1);
+              break;
             }
           }
-          prevDeltaMSB = deltaPocMSBCycleLT;
-        }
-        offset += rps->getNumberOfLongtermPictures();
-        rps->setNumberOfPictures(offset);
-      }
-#if DPB_CONSTRAINTS
-          if(rpcSlice->getVPS()->getVpsExtensionFlag()==1)
+#else
+          rpcSlice->setActiveNumILRRefIdx(1);
+#endif 
+        }
+
+        if( rpcSlice->getActiveNumILRRefIdx() == rpcSlice->getNumILRRefIdx() )
+        {
+          for( Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
           {
-#if Q0078_ADD_LAYER_SETS
-              for (Int ii = 1; ii < (rpcSlice->getVPS()->getVpsNumLayerSetsMinus1() + 1); ii++)  // prevent assert error when num_add_layer_sets > 0
-#else
-              for (Int ii=1; ii< rpcSlice->getVPS()->getNumOutputLayerSets(); ii++ )
-#endif
-              {
-                  Int layerSetIdxForOutputLayerSet = rpcSlice->getVPS()->getOutputLayerSetIdx( ii );
-                  Int chkAssert=0;
-                  for(Int kk = 0; kk < rpcSlice->getVPS()->getNumLayersInIdList(layerSetIdxForOutputLayerSet); kk++)
-                  {
-                      if(rpcSlice->getLayerId()==rpcSlice->getVPS()->getLayerSetLayerIdList(layerSetIdxForOutputLayerSet, kk))
-                      {
-                          chkAssert=1;
-                      }
-                  }
-                  if(chkAssert)
-                  {
-                      // There may be something wrong here (layer id assumed to be layer idx?)
-                      assert(rps->getNumberOfNegativePictures() <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)));
-                      assert(rps->getNumberOfPositivePictures() <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)) - rps->getNumberOfNegativePictures());
-                      assert((rps->getNumberOfPositivePictures() + rps->getNumberOfNegativePictures() + rps->getNumberOfLongtermPictures()) <= rpcSlice->getVPS()->getMaxVpsDecPicBufferingMinus1(ii , rpcSlice->getLayerId() , rpcSlice->getVPS()->getMaxSLayersInLayerSetMinus1(ii)));
-                  }
-              }
-              
-              
+            rpcSlice->setInterLayerPredLayerIdc(i,i);
           }
-          if(rpcSlice->getLayerId() == 0)
+        }
+        else
+        {
+          for(Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
           {
-              assert(rps->getNumberOfNegativePictures() <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1) );
-              assert(rps->getNumberOfPositivePictures() <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1) -rps->getNumberOfNegativePictures());
-              assert((rps->getNumberOfPositivePictures() + rps->getNumberOfNegativePictures() + rps->getNumberOfLongtermPictures()) <= rpcSlice->getSPS()->getMaxDecPicBuffering(rpcSlice->getSPS()->getMaxTLayers()-1));
+            READ_CODE( numBits,uiCode,"inter_layer_pred_layer_idc[i]" );
+            rpcSlice->setInterLayerPredLayerIdc(uiCode,i);
           }
-#endif
-      if ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
-        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
-        || rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
-      {
-        // In the case of BLA picture types, rps data is read from slice header but ignored
-        rps = rpcSlice->getLocalRPS();
-        rps->setNumberOfNegativePictures(0);
-        rps->setNumberOfPositivePictures(0);
-        rps->setNumberOfLongtermPictures(0);
-        rps->setNumberOfPictures(0);
-        rpcSlice->setRPS(rps);
-      }
-      if (rpcSlice->getSPS()->getTMVPFlagsPresent())
-      {
-        READ_FLAG( uiCode, "slice_temporal_mvp_enable_flag" );
-        rpcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false );
+        }
       }
       else
       {
-        rpcSlice->setEnableTMVPFlag(false);
-      }
-#if N0065_LAYER_POC_ALIGNMENT && !SHM_FIX7
-    }
-#endif
-    }
-
-#if SVC_EXTENSION
-    rpcSlice->setActiveNumILRRefIdx(0);
-    if((rpcSlice->getLayerId() > 0) && !(rpcSlice->getVPS()->getIlpSshSignalingEnabledFlag()) && (rpcSlice->getNumILRRefIdx() > 0) )
-    {
-      READ_FLAG(uiCode,"inter_layer_pred_enabled_flag");
-      rpcSlice->setInterLayerPredEnabledFlag(uiCode);
-      if( rpcSlice->getInterLayerPredEnabledFlag())
-      {
-        if(rpcSlice->getNumILRRefIdx() > 1)
-        {
-          Int numBits = 1;
-          while ((1 << numBits) < rpcSlice->getNumILRRefIdx())
-          {
-            numBits++;
-          }
-          if( !rpcSlice->getVPS()->getMaxOneActiveRefLayerFlag())
-          {
-            READ_CODE( numBits, uiCode,"num_inter_layer_ref_pics_minus1" );
-            rpcSlice->setActiveNumILRRefIdx(uiCode + 1);
-          }
-          else
-          {
-#if P0079_DERIVE_NUMACTIVE_REF_PICS
-            for( Int i = 0; i < rpcSlice->getNumILRRefIdx(); i++ ) 
-            {
-#if Q0060_MAX_TID_REF_EQUAL_TO_ZERO
-              if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
-                (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
-#else 
-              if(rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() &&
-                (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
-#endif 
-              {          
-                rpcSlice->setActiveNumILRRefIdx(1);
-                break;
-              }
-            }
-#else
-            rpcSlice->setActiveNumILRRefIdx(1);
-#endif 
-          }
-
-          if( rpcSlice->getActiveNumILRRefIdx() == rpcSlice->getNumILRRefIdx() )
-          {
-            for( Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
-            {
-              rpcSlice->setInterLayerPredLayerIdc(i,i);
-            }
-          }
-          else
-          {
-            for(Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
-            {
-              READ_CODE( numBits,uiCode,"inter_layer_pred_layer_idc[i]" );
-              rpcSlice->setInterLayerPredLayerIdc(uiCode,i);
-            }
-          }
-        }
-        else
-        {
 #if O0225_TID_BASED_IL_RPS_DERIV && TSLAYERS_IL_RPS
 #if Q0060_MAX_TID_REF_EQUAL_TO_ZERO
-          if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(0,rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
-            (rpcSlice->getVPS()->getMaxTSLayersMinus1(0) >=  rpcSlice->getTLayer()) )
-#else
-          if( (rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(0,rpcSlice->getLayerId()) >  rpcSlice->getTLayer()) &&
-             (rpcSlice->getVPS()->getMaxTSLayersMinus1(0) >=  rpcSlice->getTLayer()) )
+        if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(0,rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
+          (rpcSlice->getVPS()->getMaxTSLayersMinus1(0) >=  rpcSlice->getTLayer()) )
+#else
+        if( (rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(0,rpcSlice->getLayerId()) >  rpcSlice->getTLayer()) &&
+          (rpcSlice->getVPS()->getMaxTSLayersMinus1(0) >=  rpcSlice->getTLayer()) )
 #endif 
         {
@@ -2917,568 +2917,568 @@
         }
 #endif
-        }
-      }
-    }
-    else if( rpcSlice->getVPS()->getIlpSshSignalingEnabledFlag() == true &&  (rpcSlice->getLayerId() > 0 ))
-    {
-      rpcSlice->setInterLayerPredEnabledFlag(true);
+      }
+    }
+  }
+  else if( rpcSlice->getVPS()->getIlpSshSignalingEnabledFlag() == true &&  (rpcSlice->getLayerId() > 0 ))
+  {
+    rpcSlice->setInterLayerPredEnabledFlag(true);
 
 #if O0225_TID_BASED_IL_RPS_DERIV && TSLAYERS_IL_RPS
-      Int   numRefLayerPics = 0;
-      Int   i = 0;
-      Int   refLayerPicIdc  [MAX_VPS_LAYER_ID_PLUS1];
-      for(i = 0, numRefLayerPics = 0;  i < rpcSlice->getNumILRRefIdx(); i++ ) 
-      {
+    Int   numRefLayerPics = 0;
+    Int   i = 0;
+    Int   refLayerPicIdc  [MAX_VPS_LAYER_ID_PLUS1];
+    for(i = 0, numRefLayerPics = 0;  i < rpcSlice->getNumILRRefIdx(); i++ ) 
+    {
 #if Q0060_MAX_TID_REF_EQUAL_TO_ZERO
-        if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
-          (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
+      if((rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() || rpcSlice->getTLayer()==0) &&
+        (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
 #else 
-        if(rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() &&
-           (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
+      if(rpcSlice->getVPS()->getMaxTidIlRefPicsPlus1(rpcSlice->getVPS()->getLayerIdInVps(i),rpcSlice->getLayerId()) >  rpcSlice->getTLayer() &&
+        (rpcSlice->getVPS()->getMaxTSLayersMinus1(rpcSlice->getVPS()->getLayerIdInVps(i)) >=  rpcSlice->getTLayer()) )
 #endif 
-        {          
-          refLayerPicIdc[ numRefLayerPics++ ] = i;
-        }
-      }
-      rpcSlice->setActiveNumILRRefIdx(numRefLayerPics);
-      for( i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
-      {
-        rpcSlice->setInterLayerPredLayerIdc(refLayerPicIdc[i],i);
-      }      
-#else
-      rpcSlice->setActiveNumILRRefIdx(rpcSlice->getNumILRRefIdx());
-      for( Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
-      {
-        rpcSlice->setInterLayerPredLayerIdc(i,i);
-      }
+      {          
+        refLayerPicIdc[ numRefLayerPics++ ] = i;
+      }
+    }
+    rpcSlice->setActiveNumILRRefIdx(numRefLayerPics);
+    for( i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
+    {
+      rpcSlice->setInterLayerPredLayerIdc(refLayerPicIdc[i],i);
+    }      
+#else
+    rpcSlice->setActiveNumILRRefIdx(rpcSlice->getNumILRRefIdx());
+    for( Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ )
+    {
+      rpcSlice->setInterLayerPredLayerIdc(i,i);
+    }
 #endif 
-    }
+  }
 #if P0312_VERT_PHASE_ADJ
-    for(Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ ) 
-    {
-      UInt refLayerIdc = rpcSlice->getInterLayerPredLayerIdc(i);
-      if( rpcSlice->getSPS()->getVertPhasePositionEnableFlag(refLayerIdc) )
-      {
-        READ_FLAG( uiCode, "vert_phase_position_flag" ); rpcSlice->setVertPhasePositionFlag( uiCode? true : false, refLayerIdc );
-      }
-    }
+  for(Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ ) 
+  {
+    UInt refLayerIdc = rpcSlice->getInterLayerPredLayerIdc(i);
+    if( rpcSlice->getSPS()->getVertPhasePositionEnableFlag(refLayerIdc) )
+    {
+      READ_FLAG( uiCode, "vert_phase_position_flag" ); rpcSlice->setVertPhasePositionFlag( uiCode? true : false, refLayerIdc );
+    }
+  }
 #endif
 #endif //SVC_EXTENSION
 
-    if(sps->getUseSAO())
-    {
-      READ_FLAG(uiCode, "slice_sao_luma_flag");  rpcSlice->setSaoEnabledFlag((Bool)uiCode);
+  if(sps->getUseSAO())
+  {
+    READ_FLAG(uiCode, "slice_sao_luma_flag");  rpcSlice->setSaoEnabledFlag((Bool)uiCode);
 #if AUXILIARY_PICTURES
-      ChromaFormat format;
+    ChromaFormat format;
 #if REPN_FORMAT_IN_VPS
 #if O0096_REP_FORMAT_INDEX
-      if( sps->getLayerId() == 0 )
-      {
-        format = sps->getChromaFormatIdc();
-      }
-      else
-      {
-        format = rpcSlice->getVPS()->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : rpcSlice->getVPS()->getVpsRepFormatIdx(sps->getLayerId()) )->getChromaFormatVpsIdc();
+    if( sps->getLayerId() == 0 )
+    {
+      format = sps->getChromaFormatIdc();
+    }
+    else
+    {
+      format = rpcSlice->getVPS()->getVpsRepFormat( sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : rpcSlice->getVPS()->getVpsRepFormatIdx(sps->getLayerId()) )->getChromaFormatVpsIdc();
 #if Q0195_REP_FORMAT_CLEANUP
-         assert( (sps->getUpdateRepFormatFlag()==false && rpcSlice->getVPS()->getVpsNumRepFormats()==1) || rpcSlice->getVPS()->getVpsNumRepFormats() > 1 ); //conformance check
-#endif
-      }
-#else
-      if( ( sps->getLayerId() == 0 ) || sps->getUpdateRepFormatFlag() )
-      {
-        format = sps->getChromaFormatIdc();
-      }
-      else
-      {
-        format = rpcSlice->getVPS()->getVpsRepFormat( rpcSlice->getVPS()->getVpsRepFormatIdx(sps->getLayerId()) )->getChromaFormatVpsIdc();
-      }
-#endif
-#else
+      assert( (sps->getUpdateRepFormatFlag()==false && rpcSlice->getVPS()->getVpsNumRepFormats()==1) || rpcSlice->getVPS()->getVpsNumRepFormats() > 1 ); //conformance check
+#endif
+    }
+#else
+    if( ( sps->getLayerId() == 0 ) || sps->getUpdateRepFormatFlag() )
+    {
       format = sps->getChromaFormatIdc();
-#endif
-      if (format != CHROMA_400)
-      {
+    }
+    else
+    {
+      format = rpcSlice->getVPS()->getVpsRepFormat( rpcSlice->getVPS()->getVpsRepFormatIdx(sps->getLayerId()) )->getChromaFormatVpsIdc();
+    }
+#endif
+#else
+    format = sps->getChromaFormatIdc();
+#endif
+    if (format != CHROMA_400)
+    {
 #endif
       READ_FLAG(uiCode, "slice_sao_chroma_flag");  rpcSlice->setSaoEnabledFlagChroma((Bool)uiCode);
 #if AUXILIARY_PICTURES
+    }
+    else
+    {
+      rpcSlice->setSaoEnabledFlagChroma(false);
+    }
+#endif
+  }
+
+  if (rpcSlice->getIdrPicFlag())
+  {
+    rpcSlice->setEnableTMVPFlag(false);
+  }
+  if (!rpcSlice->isIntra())
+  {
+
+    READ_FLAG( uiCode, "num_ref_idx_active_override_flag");
+    if (uiCode)
+    {
+      READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
+      if (rpcSlice->isInterB())
+      {
+        READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
       }
       else
       {
-        rpcSlice->setSaoEnabledFlagChroma(false);
-      }
-#endif
-    }
-
-    if (rpcSlice->getIdrPicFlag())
-    {
-      rpcSlice->setEnableTMVPFlag(false);
-    }
-    if (!rpcSlice->isIntra())
-    {
-
-      READ_FLAG( uiCode, "num_ref_idx_active_override_flag");
-      if (uiCode)
-      {
-        READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
-        if (rpcSlice->isInterB())
-        {
-          READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  rpcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
-        }
-        else
-        {
-          rpcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
-        }
+        rpcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
+      }
+    }
+    else
+    {
+      rpcSlice->setNumRefIdx(REF_PIC_LIST_0, rpcSlice->getPPS()->getNumRefIdxL0DefaultActive());
+      if (rpcSlice->isInterB())
+      {
+        rpcSlice->setNumRefIdx(REF_PIC_LIST_1, rpcSlice->getPPS()->getNumRefIdxL1DefaultActive());
       }
       else
       {
-        rpcSlice->setNumRefIdx(REF_PIC_LIST_0, rpcSlice->getPPS()->getNumRefIdxL0DefaultActive());
-        if (rpcSlice->isInterB())
-        {
-          rpcSlice->setNumRefIdx(REF_PIC_LIST_1, rpcSlice->getPPS()->getNumRefIdxL1DefaultActive());
-        }
-        else
-        {
-          rpcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
-        }
-      }
-    }
-    // }
-    TComRefPicListModification* refPicListModification = rpcSlice->getRefPicListModification();
-    if(!rpcSlice->isIntra())
-    {
-      if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
-      {
-        refPicListModification->setRefPicListModificationFlagL0( 0 );
+        rpcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
+      }
+    }
+  }
+  // }
+  TComRefPicListModification* refPicListModification = rpcSlice->getRefPicListModification();
+  if(!rpcSlice->isIntra())
+  {
+    if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
+    {
+      refPicListModification->setRefPicListModificationFlagL0( 0 );
+    }
+    else
+    {
+      READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); refPicListModification->setRefPicListModificationFlagL0( uiCode ? 1 : 0 );
+    }
+
+    if(refPicListModification->getRefPicListModificationFlagL0())
+    {
+      uiCode = 0;
+      Int i = 0;
+      Int numRpsCurrTempList0 = rpcSlice->getNumRpsCurrTempList();
+      if ( numRpsCurrTempList0 > 1 )
+      {
+        Int length = 1;
+        numRpsCurrTempList0 --;
+        while ( numRpsCurrTempList0 >>= 1)
+        {
+          length ++;
+        }
+        for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
+        {
+          READ_CODE( length, uiCode, "list_entry_l0" );
+          refPicListModification->setRefPicSetIdxL0(i, uiCode );
+        }
       }
       else
       {
-        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l0" ); refPicListModification->setRefPicListModificationFlagL0( uiCode ? 1 : 0 );
-      }
-
-      if(refPicListModification->getRefPicListModificationFlagL0())
-      {
-        uiCode = 0;
-        Int i = 0;
-        Int numRpsCurrTempList0 = rpcSlice->getNumRpsCurrTempList();
-        if ( numRpsCurrTempList0 > 1 )
-        {
-          Int length = 1;
-          numRpsCurrTempList0 --;
-          while ( numRpsCurrTempList0 >>= 1)
-          {
-            length ++;
-          }
-          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
-          {
-            READ_CODE( length, uiCode, "list_entry_l0" );
-            refPicListModification->setRefPicSetIdxL0(i, uiCode );
-          }
-        }
-        else
-        {
-          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
-          {
-            refPicListModification->setRefPicSetIdxL0(i, 0 );
-          }
-        }
-      }
+        for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
+        {
+          refPicListModification->setRefPicSetIdxL0(i, 0 );
+        }
+      }
+    }
+  }
+  else
+  {
+    refPicListModification->setRefPicListModificationFlagL0(0);
+  }
+  if(rpcSlice->isInterB())
+  {
+    if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
+    {
+      refPicListModification->setRefPicListModificationFlagL1( 0 );
     }
     else
     {
-      refPicListModification->setRefPicListModificationFlagL0(0);
-    }
-    if(rpcSlice->isInterB())
-    {
-      if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
-      {
-        refPicListModification->setRefPicListModificationFlagL1( 0 );
+      READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); refPicListModification->setRefPicListModificationFlagL1( uiCode ? 1 : 0 );
+    }
+    if(refPicListModification->getRefPicListModificationFlagL1())
+    {
+      uiCode = 0;
+      Int i = 0;
+      Int numRpsCurrTempList1 = rpcSlice->getNumRpsCurrTempList();
+      if ( numRpsCurrTempList1 > 1 )
+      {
+        Int length = 1;
+        numRpsCurrTempList1 --;
+        while ( numRpsCurrTempList1 >>= 1)
+        {
+          length ++;
+        }
+        for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
+        {
+          READ_CODE( length, uiCode, "list_entry_l1" );
+          refPicListModification->setRefPicSetIdxL1(i, uiCode );
+        }
       }
       else
       {
-        READ_FLAG( uiCode, "ref_pic_list_modification_flag_l1" ); refPicListModification->setRefPicListModificationFlagL1( uiCode ? 1 : 0 );
-      }
-      if(refPicListModification->getRefPicListModificationFlagL1())
-      {
-        uiCode = 0;
-        Int i = 0;
-        Int numRpsCurrTempList1 = rpcSlice->getNumRpsCurrTempList();
-        if ( numRpsCurrTempList1 > 1 )
-        {
-          Int length = 1;
-          numRpsCurrTempList1 --;
-          while ( numRpsCurrTempList1 >>= 1)
-          {
-            length ++;
-          }
-          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
-          {
-            READ_CODE( length, uiCode, "list_entry_l1" );
-            refPicListModification->setRefPicSetIdxL1(i, uiCode );
-          }
-        }
-        else
-        {
-          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
-          {
-            refPicListModification->setRefPicSetIdxL1(i, 0 );
-          }
-        }
-      }
+        for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
+        {
+          refPicListModification->setRefPicSetIdxL1(i, 0 );
+        }
+      }
+    }
+  }
+  else
+  {
+    refPicListModification->setRefPicListModificationFlagL1(0);
+  }
+  if (rpcSlice->isInterB())
+  {
+    READ_FLAG( uiCode, "mvd_l1_zero_flag" );       rpcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
+  }
+
+  rpcSlice->setCabacInitFlag( false ); // default
+  if(pps->getCabacInitPresentFlag() && !rpcSlice->isIntra())
+  {
+    READ_FLAG(uiCode, "cabac_init_flag");
+    rpcSlice->setCabacInitFlag( uiCode ? true : false );
+  }
+
+  if ( rpcSlice->getEnableTMVPFlag() )
+  {
+#if SVC_EXTENSION && REF_IDX_MFM
+    // set motion mapping flag
+    rpcSlice->setMFMEnabledFlag( ( rpcSlice->getNumMotionPredRefLayers() > 0 && rpcSlice->getActiveNumILRRefIdx() && !rpcSlice->isIntra() ) ? true : false );
+#endif
+    if ( rpcSlice->getSliceType() == B_SLICE )
+    {
+      READ_FLAG( uiCode, "collocated_from_l0_flag" );
+      rpcSlice->setColFromL0Flag(uiCode);
     }
     else
     {
-      refPicListModification->setRefPicListModificationFlagL1(0);
-    }
-    if (rpcSlice->isInterB())
-    {
-      READ_FLAG( uiCode, "mvd_l1_zero_flag" );       rpcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
-    }
-
-    rpcSlice->setCabacInitFlag( false ); // default
-    if(pps->getCabacInitPresentFlag() && !rpcSlice->isIntra())
-    {
-      READ_FLAG(uiCode, "cabac_init_flag");
-      rpcSlice->setCabacInitFlag( uiCode ? true : false );
-    }
-
-    if ( rpcSlice->getEnableTMVPFlag() )
-    {
-#if SVC_EXTENSION && REF_IDX_MFM
-      // set motion mapping flag
-      rpcSlice->setMFMEnabledFlag( ( rpcSlice->getNumMotionPredRefLayers() > 0 && rpcSlice->getActiveNumILRRefIdx() && !rpcSlice->isIntra() ) ? true : false );
-#endif
-      if ( rpcSlice->getSliceType() == B_SLICE )
-      {
-        READ_FLAG( uiCode, "collocated_from_l0_flag" );
-        rpcSlice->setColFromL0Flag(uiCode);
-      }
-      else
-      {
-        rpcSlice->setColFromL0Flag( 1 );
-      }
-
-      if ( rpcSlice->getSliceType() != I_SLICE &&
-          ((rpcSlice->getColFromL0Flag() == 1 && rpcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
-           (rpcSlice->getColFromL0Flag() == 0 && rpcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
-      {
-        READ_UVLC( uiCode, "collocated_ref_idx" );
-        rpcSlice->setColRefIdx(uiCode);
-      }
-      else
-      {
-        rpcSlice->setColRefIdx(0);
-      }
-    }
-    if ( (pps->getUseWP() && rpcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && rpcSlice->getSliceType()==B_SLICE) )
-    {
-      xParsePredWeightTable(rpcSlice);
-      rpcSlice->initWpScaling();
-    }
-    if (!rpcSlice->isIntra())
-    {
-      READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
-      rpcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
-    }
-
-    READ_SVLC( iCode, "slice_qp_delta" );
-    rpcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
+      rpcSlice->setColFromL0Flag( 1 );
+    }
+
+    if ( rpcSlice->getSliceType() != I_SLICE &&
+      ((rpcSlice->getColFromL0Flag() == 1 && rpcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
+      (rpcSlice->getColFromL0Flag() == 0 && rpcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
+    {
+      READ_UVLC( uiCode, "collocated_ref_idx" );
+      rpcSlice->setColRefIdx(uiCode);
+    }
+    else
+    {
+      rpcSlice->setColRefIdx(0);
+    }
+  }
+  if ( (pps->getUseWP() && rpcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && rpcSlice->getSliceType()==B_SLICE) )
+  {
+    xParsePredWeightTable(rpcSlice);
+    rpcSlice->initWpScaling();
+  }
+  if (!rpcSlice->isIntra())
+  {
+    READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
+    rpcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
+  }
+
+  READ_SVLC( iCode, "slice_qp_delta" );
+  rpcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
 
 #if REPN_FORMAT_IN_VPS
 #if O0194_DIFFERENT_BITDEPTH_EL_BL
-    g_bitDepthYLayer[rpcSlice->getLayerId()] = rpcSlice->getBitDepthY();
-    g_bitDepthCLayer[rpcSlice->getLayerId()] = rpcSlice->getBitDepthC();
-#endif
-    assert( rpcSlice->getSliceQp() >= -rpcSlice->getQpBDOffsetY() );
-#else
-    assert( rpcSlice->getSliceQp() >= -sps->getQpBDOffsetY() );
-#endif
-    assert( rpcSlice->getSliceQp() <=  51 );
-
-    if (rpcSlice->getPPS()->getSliceChromaQpFlag())
-    {
-      READ_SVLC( iCode, "slice_qp_delta_cb" );
-      rpcSlice->setSliceQpDeltaCb( iCode );
-      assert( rpcSlice->getSliceQpDeltaCb() >= -12 );
-      assert( rpcSlice->getSliceQpDeltaCb() <=  12 );
-      assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) >= -12 );
-      assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) <=  12 );
-
-      READ_SVLC( iCode, "slice_qp_delta_cr" );
-      rpcSlice->setSliceQpDeltaCr( iCode );
-      assert( rpcSlice->getSliceQpDeltaCr() >= -12 );
-      assert( rpcSlice->getSliceQpDeltaCr() <=  12 );
-      assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) >= -12 );
-      assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) <=  12 );
-    }
-
-    if (rpcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
-    {
-      if(rpcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag())
-      {
-        READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        rpcSlice->setDeblockingFilterOverrideFlag(uiCode ? true : false);
+  g_bitDepthYLayer[rpcSlice->getLayerId()] = rpcSlice->getBitDepthY();
+  g_bitDepthCLayer[rpcSlice->getLayerId()] = rpcSlice->getBitDepthC();
+#endif
+  assert( rpcSlice->getSliceQp() >= -rpcSlice->getQpBDOffsetY() );
+#else
+  assert( rpcSlice->getSliceQp() >= -sps->getQpBDOffsetY() );
+#endif
+  assert( rpcSlice->getSliceQp() <=  51 );
+
+  if (rpcSlice->getPPS()->getSliceChromaQpFlag())
+  {
+    READ_SVLC( iCode, "slice_qp_delta_cb" );
+    rpcSlice->setSliceQpDeltaCb( iCode );
+    assert( rpcSlice->getSliceQpDeltaCb() >= -12 );
+    assert( rpcSlice->getSliceQpDeltaCb() <=  12 );
+    assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) >= -12 );
+    assert( (rpcSlice->getPPS()->getChromaCbQpOffset() + rpcSlice->getSliceQpDeltaCb()) <=  12 );
+
+    READ_SVLC( iCode, "slice_qp_delta_cr" );
+    rpcSlice->setSliceQpDeltaCr( iCode );
+    assert( rpcSlice->getSliceQpDeltaCr() >= -12 );
+    assert( rpcSlice->getSliceQpDeltaCr() <=  12 );
+    assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) >= -12 );
+    assert( (rpcSlice->getPPS()->getChromaCrQpOffset() + rpcSlice->getSliceQpDeltaCr()) <=  12 );
+  }
+
+  if (rpcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
+  {
+    if(rpcSlice->getPPS()->getDeblockingFilterOverrideEnabledFlag())
+    {
+      READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        rpcSlice->setDeblockingFilterOverrideFlag(uiCode ? true : false);
+    }
+    else
+    {
+      rpcSlice->setDeblockingFilterOverrideFlag(0);
+    }
+    if(rpcSlice->getDeblockingFilterOverrideFlag())
+    {
+      READ_FLAG ( uiCode, "slice_disable_deblocking_filter_flag" );   rpcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
+      if(!rpcSlice->getDeblockingFilterDisable())
+      {
+        READ_SVLC( iCode, "slice_beta_offset_div2" );                       rpcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
+        assert(rpcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
+          rpcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
+        READ_SVLC( iCode, "slice_tc_offset_div2" );                         rpcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
+        assert(rpcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
+          rpcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
+      }
+    }
+    else
+    {
+      rpcSlice->setDeblockingFilterDisable   ( rpcSlice->getPPS()->getPicDisableDeblockingFilterFlag() );
+      rpcSlice->setDeblockingFilterBetaOffsetDiv2( rpcSlice->getPPS()->getDeblockingFilterBetaOffsetDiv2() );
+      rpcSlice->setDeblockingFilterTcOffsetDiv2  ( rpcSlice->getPPS()->getDeblockingFilterTcOffsetDiv2() );
+    }
+  }
+  else
+  {
+    rpcSlice->setDeblockingFilterDisable       ( false );
+    rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
+    rpcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
+  }
+
+  Bool isSAOEnabled = (!rpcSlice->getSPS()->getUseSAO())?(false):(rpcSlice->getSaoEnabledFlag()||rpcSlice->getSaoEnabledFlagChroma());
+  Bool isDBFEnabled = (!rpcSlice->getDeblockingFilterDisable());
+
+  if(rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
+  {
+    READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
+  }
+  else
+  {
+    uiCode = rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
+  }
+  rpcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
+
+}
+
+UInt *entryPointOffset          = NULL;
+UInt numEntryPointOffsets, offsetLenMinus1;
+if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
+{
+  READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets"); rpcSlice->setNumEntryPointOffsets ( numEntryPointOffsets );
+  if (numEntryPointOffsets>0)
+  {
+    READ_UVLC(offsetLenMinus1, "offset_len_minus1");
+  }
+  entryPointOffset = new UInt[numEntryPointOffsets];
+  for (UInt idx=0; idx<numEntryPointOffsets; idx++)
+  {
+    READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
+    entryPointOffset[ idx ] = uiCode + 1;
+  }
+}
+else
+{
+  rpcSlice->setNumEntryPointOffsets ( 0 );
+}
+
+#if POC_RESET_IDC_SIGNALLING
+Int sliceHeaderExtensionLength = 0;
+if(pps->getSliceHeaderExtensionPresentFlag())
+{
+  READ_UVLC( uiCode, "slice_header_extension_length"); sliceHeaderExtensionLength = uiCode;
+}
+else
+{
+  sliceHeaderExtensionLength = 0;
+}
+UInt startBits = m_pcBitstream->getNumBitsRead();     // Start counter of # SH Extn bits
+if( sliceHeaderExtensionLength > 0 )
+{
+  if( rpcSlice->getPPS()->getPocResetInfoPresentFlag() )
+  {
+    READ_CODE( 2, uiCode,       "poc_reset_idc"); rpcSlice->setPocResetIdc(uiCode);
+  }
+  else
+  {
+    rpcSlice->setPocResetIdc( 0 );
+  }
+#if Q0142_POC_LSB_NOT_PRESENT
+  if ( rpcSlice->getVPS()->getPocLsbNotPresentFlag(rpcSlice->getLayerId()) && iPOClsb > 0 )
+  {
+    assert( rpcSlice->getPocResetIdc() != 2 );
+  }
+#endif
+  if( rpcSlice->getPocResetIdc() > 0 )
+  {
+    READ_CODE(6, uiCode,      "poc_reset_period_id"); rpcSlice->setPocResetPeriodId(uiCode);
+  }
+  else
+  {
+
+    rpcSlice->setPocResetPeriodId( 0 );
+  }
+
+  if (rpcSlice->getPocResetIdc() == 3)
+  {
+    READ_FLAG( uiCode,        "full_poc_reset_flag"); rpcSlice->setFullPocResetFlag((uiCode == 1) ? true : false);
+    READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode,"poc_lsb_val"); rpcSlice->setPocLsbVal(uiCode);
+#if Q0142_POC_LSB_NOT_PRESENT
+    if ( rpcSlice->getVPS()->getPocLsbNotPresentFlag(rpcSlice->getLayerId()) && rpcSlice->getFullPocResetFlag() )
+    {
+      assert( rpcSlice->getPocLsbVal() == 0 );
+    }
+#endif
+  }
+
+  // Derive the value of PocMsbValRequiredFlag
+  rpcSlice->setPocMsbValRequiredFlag( rpcSlice->getCraPicFlag() || rpcSlice->getBlaPicFlag()
+    /* || related to vps_poc_lsb_aligned_flag */
+    );
+
+  if( !rpcSlice->getPocMsbValRequiredFlag() /* vps_poc_lsb_aligned_flag */ )
+  {
+    READ_FLAG( uiCode,    "poc_msb_val_present_flag"); rpcSlice->setPocMsbValPresentFlag( uiCode ? true : false );
+  }
+  else
+  {
+#if POC_MSB_VAL_PRESENT_FLAG_SEM
+    if( sliceHeaderExtensionLength == 0 )
+    {
+      rpcSlice->setPocMsbValPresentFlag( false );
+    }
+    else if( rpcSlice->getPocMsbValRequiredFlag() )
+#else
+    if( rpcSlice->getPocMsbValRequiredFlag() )
+#endif
+    {
+      rpcSlice->setPocMsbValPresentFlag( true );
+    }
+    else
+    {
+      rpcSlice->setPocMsbValPresentFlag( false );
+    }
+  }
+
+#if !POC_RESET_IDC_DECODER
+  Int maxPocLsb  = 1 << rpcSlice->getSPS()->getBitsForPOC();
+#endif
+  if( rpcSlice->getPocMsbValPresentFlag() )
+  {
+    READ_UVLC( uiCode,    "poc_msb_val");             rpcSlice->setPocMsbVal( uiCode );
+
+#if !POC_RESET_IDC_DECODER
+    // Update POC of the slice based on this MSB val
+    Int pocLsb     = rpcSlice->getPOC() % maxPocLsb;
+    rpcSlice->setPOC((rpcSlice->getPocMsbVal() * maxPocLsb) + pocLsb);
+  }
+  else
+  {
+    rpcSlice->setPocMsbVal( rpcSlice->getPOC() / maxPocLsb );
+#endif
+  }
+
+  // Read remaining bits in the slice header extension.
+  UInt endBits = m_pcBitstream->getNumBitsRead();
+  Int counter = (endBits - startBits) % 8;
+  if( counter )
+  {
+    counter = 8 - counter;
+  }
+
+  while( counter )
+  {
+#if Q0146_SSH_EXT_DATA_BIT 
+    READ_FLAG( uiCode, "slice_segment_header_extension_data_bit" );
+#else
+    READ_FLAG( uiCode, "slice_segment_header_extension_reserved_bit" ); assert( uiCode == 1 );
+#endif
+    counter--;
+  }
+}
+#else
+if(pps->getSliceHeaderExtensionPresentFlag())
+{
+  READ_UVLC(uiCode,"slice_header_extension_length");
+  for(Int i=0; i<uiCode; i++)
+  {
+    UInt ignore;
+    READ_CODE(8,ignore,"slice_header_extension_data_byte");
+  }
+}
+#endif
+m_pcBitstream->readByteAlignment();
+
+if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
+{
+  Int endOfSliceHeaderLocation = m_pcBitstream->getByteLocation();
+
+  // Adjust endOfSliceHeaderLocation to account for emulation prevention bytes in the slice segment header
+  for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
+  {
+    if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) < endOfSliceHeaderLocation )
+    {
+      endOfSliceHeaderLocation++;
+    }
+  }
+
+  Int  curEntryPointOffset     = 0;
+  Int  prevEntryPointOffset    = 0;
+  for (UInt idx=0; idx<numEntryPointOffsets; idx++)
+  {
+    curEntryPointOffset += entryPointOffset[ idx ];
+
+    Int emulationPreventionByteCount = 0;
+    for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
+    {
+      if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) &&
+        m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
+      {
+        emulationPreventionByteCount++;
+      }
+    }
+
+    entryPointOffset[ idx ] -= emulationPreventionByteCount;
+    prevEntryPointOffset = curEntryPointOffset;
+  }
+
+  if ( pps->getTilesEnabledFlag() )
+  {
+    rpcSlice->setTileLocationCount( numEntryPointOffsets );
+
+    UInt prevPos = 0;
+    for (Int idx=0; idx<rpcSlice->getTileLocationCount(); idx++)
+    {
+      rpcSlice->setTileLocation( idx, prevPos + entryPointOffset [ idx ] );
+      prevPos += entryPointOffset[ idx ];
+    }
+  }
+  else if ( pps->getEntropyCodingSyncEnabledFlag() )
+  {
+    Int numSubstreams = rpcSlice->getNumEntryPointOffsets()+1;
+    rpcSlice->allocSubstreamSizes(numSubstreams);
+    UInt *pSubstreamSizes       = rpcSlice->getSubstreamSizes();
+    for (Int idx=0; idx<numSubstreams-1; idx++)
+    {
+      if ( idx < numEntryPointOffsets )
+      {
+        pSubstreamSizes[ idx ] = ( entryPointOffset[ idx ] << 3 ) ;
       }
       else
       {
-        rpcSlice->setDeblockingFilterOverrideFlag(0);
-      }
-      if(rpcSlice->getDeblockingFilterOverrideFlag())
-      {
-        READ_FLAG ( uiCode, "slice_disable_deblocking_filter_flag" );   rpcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
-        if(!rpcSlice->getDeblockingFilterDisable())
-        {
-          READ_SVLC( iCode, "slice_beta_offset_div2" );                       rpcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
-          assert(rpcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
-                 rpcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
-          READ_SVLC( iCode, "slice_tc_offset_div2" );                         rpcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
-          assert(rpcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
-                 rpcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
-        }
-      }
-      else
-      {
-        rpcSlice->setDeblockingFilterDisable   ( rpcSlice->getPPS()->getPicDisableDeblockingFilterFlag() );
-        rpcSlice->setDeblockingFilterBetaOffsetDiv2( rpcSlice->getPPS()->getDeblockingFilterBetaOffsetDiv2() );
-        rpcSlice->setDeblockingFilterTcOffsetDiv2  ( rpcSlice->getPPS()->getDeblockingFilterTcOffsetDiv2() );
-      }
-    }
-    else
-    {
-      rpcSlice->setDeblockingFilterDisable       ( false );
-      rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
-      rpcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
-    }
-
-    Bool isSAOEnabled = (!rpcSlice->getSPS()->getUseSAO())?(false):(rpcSlice->getSaoEnabledFlag()||rpcSlice->getSaoEnabledFlagChroma());
-    Bool isDBFEnabled = (!rpcSlice->getDeblockingFilterDisable());
-
-    if(rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
-    {
-      READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
-    }
-    else
-    {
-      uiCode = rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
-    }
-    rpcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
-
-  }
-
-    UInt *entryPointOffset          = NULL;
-    UInt numEntryPointOffsets, offsetLenMinus1;
-  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
-  {
-    READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets"); rpcSlice->setNumEntryPointOffsets ( numEntryPointOffsets );
-    if (numEntryPointOffsets>0)
-    {
-      READ_UVLC(offsetLenMinus1, "offset_len_minus1");
-    }
-    entryPointOffset = new UInt[numEntryPointOffsets];
-    for (UInt idx=0; idx<numEntryPointOffsets; idx++)
-    {
-      READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
-      entryPointOffset[ idx ] = uiCode + 1;
-    }
-  }
-  else
-  {
-    rpcSlice->setNumEntryPointOffsets ( 0 );
-  }
-
-#if POC_RESET_IDC_SIGNALLING
-  Int sliceHeaderExtensionLength = 0;
-  if(pps->getSliceHeaderExtensionPresentFlag())
-  {
-    READ_UVLC( uiCode, "slice_header_extension_length"); sliceHeaderExtensionLength = uiCode;
-  }
-  else
-  {
-    sliceHeaderExtensionLength = 0;
-  }
-  UInt startBits = m_pcBitstream->getNumBitsRead();     // Start counter of # SH Extn bits
-  if( sliceHeaderExtensionLength > 0 )
-  {
-    if( rpcSlice->getPPS()->getPocResetInfoPresentFlag() )
-    {
-      READ_CODE( 2, uiCode,       "poc_reset_idc"); rpcSlice->setPocResetIdc(uiCode);
-    }
-    else
-    {
-      rpcSlice->setPocResetIdc( 0 );
-    }
-#if Q0142_POC_LSB_NOT_PRESENT
-    if ( rpcSlice->getVPS()->getPocLsbNotPresentFlag(rpcSlice->getLayerId()) && iPOClsb > 0 )
-    {
-      assert( rpcSlice->getPocResetIdc() != 2 );
-    }
-#endif
-    if( rpcSlice->getPocResetIdc() > 0 )
-    {
-      READ_CODE(6, uiCode,      "poc_reset_period_id"); rpcSlice->setPocResetPeriodId(uiCode);
-    }
-    else
-    {
-     
-      rpcSlice->setPocResetPeriodId( 0 );
-    }
-
-    if (rpcSlice->getPocResetIdc() == 3)
-    {
-      READ_FLAG( uiCode,        "full_poc_reset_flag"); rpcSlice->setFullPocResetFlag((uiCode == 1) ? true : false);
-      READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode,"poc_lsb_val"); rpcSlice->setPocLsbVal(uiCode);
-#if Q0142_POC_LSB_NOT_PRESENT
-      if ( rpcSlice->getVPS()->getPocLsbNotPresentFlag(rpcSlice->getLayerId()) && rpcSlice->getFullPocResetFlag() )
-      {
-        assert( rpcSlice->getPocLsbVal() == 0 );
-      }
-#endif
-    }
-
-    // Derive the value of PocMsbValRequiredFlag
-    rpcSlice->setPocMsbValRequiredFlag( rpcSlice->getCraPicFlag() || rpcSlice->getBlaPicFlag()
-                                          /* || related to vps_poc_lsb_aligned_flag */
-                                          );
-
-    if( !rpcSlice->getPocMsbValRequiredFlag() /* vps_poc_lsb_aligned_flag */ )
-    {
-      READ_FLAG( uiCode,    "poc_msb_val_present_flag"); rpcSlice->setPocMsbValPresentFlag( uiCode ? true : false );
-    }
-    else
-    {
-#if POC_MSB_VAL_PRESENT_FLAG_SEM
-      if( sliceHeaderExtensionLength == 0 )
-      {
-        rpcSlice->setPocMsbValPresentFlag( false );
-      }
-      else if( rpcSlice->getPocMsbValRequiredFlag() )
-#else
-      if( rpcSlice->getPocMsbValRequiredFlag() )
-#endif
-      {
-        rpcSlice->setPocMsbValPresentFlag( true );
-      }
-      else
-      {
-        rpcSlice->setPocMsbValPresentFlag( false );
-      }
-    }
-
-#if !POC_RESET_IDC_DECODER
-    Int maxPocLsb  = 1 << rpcSlice->getSPS()->getBitsForPOC();
-#endif
-    if( rpcSlice->getPocMsbValPresentFlag() )
-    {
-      READ_UVLC( uiCode,    "poc_msb_val");             rpcSlice->setPocMsbVal( uiCode );
-
-#if !POC_RESET_IDC_DECODER
-      // Update POC of the slice based on this MSB val
-      Int pocLsb     = rpcSlice->getPOC() % maxPocLsb;
-      rpcSlice->setPOC((rpcSlice->getPocMsbVal() * maxPocLsb) + pocLsb);
-    }
-    else
-    {
-      rpcSlice->setPocMsbVal( rpcSlice->getPOC() / maxPocLsb );
-#endif
-    }
-
-    // Read remaining bits in the slice header extension.
-    UInt endBits = m_pcBitstream->getNumBitsRead();
-    Int counter = (endBits - startBits) % 8;
-    if( counter )
-    {
-      counter = 8 - counter;
-    }
-
-    while( counter )
-    {
-#if Q0146_SSH_EXT_DATA_BIT 
-      READ_FLAG( uiCode, "slice_segment_header_extension_data_bit" );
-#else
-      READ_FLAG( uiCode, "slice_segment_header_extension_reserved_bit" ); assert( uiCode == 1 );
-#endif
-      counter--;
-    }
-  }
-#else
-  if(pps->getSliceHeaderExtensionPresentFlag())
-  {
-    READ_UVLC(uiCode,"slice_header_extension_length");
-    for(Int i=0; i<uiCode; i++)
-    {
-      UInt ignore;
-      READ_CODE(8,ignore,"slice_header_extension_data_byte");
-    }
-  }
-#endif
-  m_pcBitstream->readByteAlignment();
-
-  if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
-  {
-    Int endOfSliceHeaderLocation = m_pcBitstream->getByteLocation();
-
-    // Adjust endOfSliceHeaderLocation to account for emulation prevention bytes in the slice segment header
-    for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
-    {
-      if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) < endOfSliceHeaderLocation )
-      {
-        endOfSliceHeaderLocation++;
-      }
-    }
-
-    Int  curEntryPointOffset     = 0;
-    Int  prevEntryPointOffset    = 0;
-    for (UInt idx=0; idx<numEntryPointOffsets; idx++)
-    {
-      curEntryPointOffset += entryPointOffset[ idx ];
-
-      Int emulationPreventionByteCount = 0;
-      for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
-      {
-        if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) &&
-             m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
-        {
-          emulationPreventionByteCount++;
-        }
-      }
-
-      entryPointOffset[ idx ] -= emulationPreventionByteCount;
-      prevEntryPointOffset = curEntryPointOffset;
-    }
-
-    if ( pps->getTilesEnabledFlag() )
-    {
-      rpcSlice->setTileLocationCount( numEntryPointOffsets );
-
-      UInt prevPos = 0;
-      for (Int idx=0; idx<rpcSlice->getTileLocationCount(); idx++)
-      {
-        rpcSlice->setTileLocation( idx, prevPos + entryPointOffset [ idx ] );
-        prevPos += entryPointOffset[ idx ];
-      }
-    }
-    else if ( pps->getEntropyCodingSyncEnabledFlag() )
-    {
-    Int numSubstreams = rpcSlice->getNumEntryPointOffsets()+1;
-      rpcSlice->allocSubstreamSizes(numSubstreams);
-      UInt *pSubstreamSizes       = rpcSlice->getSubstreamSizes();
-      for (Int idx=0; idx<numSubstreams-1; idx++)
-      {
-        if ( idx < numEntryPointOffsets )
-        {
-          pSubstreamSizes[ idx ] = ( entryPointOffset[ idx ] << 3 ) ;
-        }
-        else
-        {
-          pSubstreamSizes[ idx ] = 0;
-        }
-      }
-    }
-
-    if (entryPointOffset)
-    {
-      delete [] entryPointOffset;
-    }
-  }
-
-  return;
+        pSubstreamSizes[ idx ] = 0;
+      }
+    }
+  }
+
+  if (entryPointOffset)
+  {
+    delete [] entryPointOffset;
+  }
+}
+
+return;
 }
 
@@ -3743,6 +3743,6 @@
       {
 #endif
-      wp[1].uiLog2WeightDenom = uiLog2WeightDenomChroma;
-      wp[2].uiLog2WeightDenom = uiLog2WeightDenomChroma;
+        wp[1].uiLog2WeightDenom = uiLog2WeightDenomChroma;
+        wp[2].uiLog2WeightDenom = uiLog2WeightDenomChroma;
 #if AUXILIARY_PICTURES
       }
@@ -3923,7 +3923,25 @@
 Void TDecCavlc::xParse3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT )
 {
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  UInt uiNumRefLayersM1;
+  READ_UVLC( uiNumRefLayersM1 , "num_cm_ref_layers_minus1" );
+  assert( uiNumRefLayersM1 <= 61 );
+  for( UInt i = 0 ; i <= uiNumRefLayersM1 ; i++ )
+  {
+    UInt uiRefLayerId;
+    READ_CODE( 6 , uiRefLayerId , "cm_ref_layer_id" );
+    pc3DAsymLUT->addRefLayerId( uiRefLayerId );
+  }
+#endif
   UInt uiCurOctantDepth , uiCurPartNumLog2 , uiInputBitDepthM8 , uiOutputBitDepthM8 , uiResQaunBit;
   READ_CODE( 2 , uiCurOctantDepth , "cm_octant_depth" ); 
   READ_CODE( 2 , uiCurPartNumLog2 , "cm_y_part_num_log2" );     
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  UInt uiChromaInputBitDepthM8 , uiChromaOutputBitDepthM8;
+  READ_CODE( 3 , uiInputBitDepthM8 , "cm_input_luma_bit_depth_minus8" );
+  READ_CODE( 3 , uiChromaInputBitDepthM8 , "cm_input_chroma_bit_depth_minus8" );
+  READ_CODE( 3 , uiOutputBitDepthM8 , "cm_output_luma_bit_depth_minus8" );
+  READ_CODE( 3 , uiChromaOutputBitDepthM8 , "cm_output_chroma_bit_depth_minus8" );
+#else
   READ_CODE( 3 , uiInputBitDepthM8 , "cm_input_bit_depth_minus8" );
   Int iInputBitDepthCDelta;
@@ -3932,10 +3950,50 @@
   Int iOutputBitDepthCDelta;
   READ_SVLC(iOutputBitDepthCDelta, "cm_output_bit_depth_chroma_delta");
+#endif
   READ_CODE( 2 , uiResQaunBit , "cm_res_quant_bit" );
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  Int nAdaptCThresholdU = 1 << ( uiChromaInputBitDepthM8 + 8 - 1 );
+  Int nAdaptCThresholdV = 1 << ( uiChromaInputBitDepthM8 + 8 - 1 );
+#else
+  Int nAdaptCThresholdU = 1 << ( uiInputBitDepthM8 + 8 + iInputBitDepthCDelta - 1 );
+  Int nAdaptCThresholdV = 1 << ( uiInputBitDepthM8 + 8 + iInputBitDepthCDelta - 1 );
+#endif
+  if( uiCurOctantDepth == 1 )
+  {
+    Int delta = 0;
+    READ_SVLC( delta , "cm_adapt_threshold_u_delta" );
+    nAdaptCThresholdU += delta;
+    READ_SVLC( delta , "cm_adapt_threshold_v_delta" );
+    nAdaptCThresholdV += delta;
+  }
+#endif
   pc3DAsymLUT->destroy();
-  pc3DAsymLUT->create( uiCurOctantDepth , uiInputBitDepthM8 + 8 ,  uiInputBitDepthM8 + 8 + iInputBitDepthCDelta, uiOutputBitDepthM8 + 8 , uiOutputBitDepthM8 + 8 + iOutputBitDepthCDelta ,uiCurPartNumLog2 );
+  pc3DAsymLUT->create( uiCurOctantDepth , uiInputBitDepthM8 + 8 ,  
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+    uiChromaInputBitDepthM8 + 8 ,
+#else
+    uiInputBitDepthM8 + 8 + iInputBitDepthCDelta, 
+#endif
+    uiOutputBitDepthM8 + 8 , 
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+    uiChromaOutputBitDepthM8 + 8 ,
+#else
+    uiOutputBitDepthM8 + 8 + iOutputBitDepthCDelta ,
+#endif
+    uiCurPartNumLog2 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+    , nAdaptCThresholdU , nAdaptCThresholdV 
+#endif    
+    );
   pc3DAsymLUT->setResQuantBit( uiResQaunBit );
 
+#if R0164_CGS_LUT_BUGFIX
+  pc3DAsymLUT->xInitCuboids();
+#endif
   xParse3DAsymLUTOctant( pc3DAsymLUT , 0 , 0 , 0 , 0 , 1 << pc3DAsymLUT->getCurOctantDepth() );
+#if R0164_CGS_LUT_BUGFIX
+  pc3DAsymLUT->xCuboidsExplicitCheck( true );
+#endif
 }
 
@@ -3964,4 +4022,7 @@
     for( Int l = 0 ; l < nYPartNum ; l++ )
     {
+#if R0164_CGS_LUT_BUGFIX
+      Int shift = pc3DAsymLUT->getMaxOctantDepth() - nDepth ;
+#endif
       for( Int nVertexIdx = 0 ; nVertexIdx < 4 ; nVertexIdx++ )
       {
@@ -3971,13 +4032,48 @@
         if( uiCodeVertex )
         {
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+          xReadParam( deltaY );
+          xReadParam( deltaU );
+          xReadParam( deltaV );
+#else
           READ_SVLC( deltaY , "resY" );
           READ_SVLC( deltaU , "resU" );
           READ_SVLC( deltaV , "resV" );
-        }
+#endif
+        }
+#if R0164_CGS_LUT_BUGFIX
+        pc3DAsymLUT->setCuboidVertexResTree( yIdx + (l<<shift) , uIdx , vIdx , nVertexIdx , deltaY , deltaU , deltaV );
+#else
         pc3DAsymLUT->setCuboidVertexResTree( yIdx + l , uIdx , vIdx , nVertexIdx , deltaY , deltaU , deltaV );
-      }
-    }
-  }
-}
+#endif
+      }
+#if R0164_CGS_LUT_BUGFIX
+      pc3DAsymLUT->xSetExplicit( yIdx + (l<<shift) , uIdx , vIdx );
+#endif
+    }
+  }
+}
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Void TDecCavlc::xReadParam( Int& param )
+{
+  const UInt rParam = 7;
+  UInt prefix;
+  UInt codeWord ;
+  UInt rSymbol;
+  UInt sign;
+
+  READ_UVLC( prefix, "quotient")  ;
+  READ_CODE (rParam, codeWord, "remainder");
+  rSymbol = (prefix<<rParam) + codeWord;
+
+  if(rSymbol)
+  {
+    READ_FLAG(sign, "sign");
+    param = sign ? -(Int)(rSymbol) : (Int)(rSymbol);
+  }
+  else param = 0;
+}
+#endif
 #endif
 //! \}
Index: /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.h	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.h	(revision 825)
@@ -143,4 +143,7 @@
   Void xParse3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT );
   Void xParse3DAsymLUTOctant( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength );
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Void xReadParam( Int& param );
+#endif
 #endif
 };
Index: /branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.cpp	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibDecoder/TDecTop.cpp	(revision 825)
@@ -1779,6 +1779,16 @@
 #if Q0048_CGS_3D_ASYMLUT 
         TComPicYuv* pBaseColRec = pcSlice->getBaseColPic(refLayerIdc)->getPicYuvRec();
-        if( pcSlice->getPPS()->getCGSFlag() )
-        {
+        if( pcSlice->getPPS()->getCGSFlag() 
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+          && m_c3DAsymLUTPPS.isRefLayer( pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc) )
+#endif
+          )
+        {
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+          assert( pcSlice->getBaseColPic( refLayerIdc )->getSlice( 0 )->getBitDepthY() == m_c3DAsymLUTPPS.getInputBitDepthY() );
+          assert( pcSlice->getBaseColPic( refLayerIdc )->getSlice( 0 )->getBitDepthC() == m_c3DAsymLUTPPS.getInputBitDepthC() );
+          assert( pcSlice->getBitDepthY() >= m_c3DAsymLUTPPS.getOutputBitDepthY() );
+          assert( pcSlice->getBitDepthY() >= m_c3DAsymLUTPPS.getOutputBitDepthC() );
+#endif
           if(!m_pColorMappedPic)
           {
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.cpp	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.cpp	(revision 825)
@@ -15,8 +15,7 @@
   m_pEncCuboid = NULL;
   m_pBestEncCuboid = NULL;
-  memset( m_nPrevFrameBit , 0 , sizeof( m_nPrevFrameBit ) );
-  memset( m_nPrevFrameCGSBit , 0 , sizeof( m_nPrevFrameCGSBit ) );
-  memset( m_nPrevFrameCGSPartNumLog2 , 0 , sizeof( m_nPrevFrameCGSPartNumLog2 ) );
-  memset( m_nPrevFrameOverWritePPS , 0 , sizeof( m_nPrevFrameOverWritePPS ) );
+  m_nAccuFrameBit = 0;
+  m_nAccuFrameCGSBit = 0;
+  m_nPrevFrameCGSPartNumLog2 = 0;
   m_dTotalFrameBit = 0;
   m_nTotalCGSBit = 0;
@@ -32,5 +31,9 @@
   }
 
-  TCom3DAsymLUT::create( nMaxOctantDepth , nInputBitDepth , nInputBitDepthC, nOutputBitDepth , nOutputBitDepthC, nMaxYPartNumLog2 );
+  TCom3DAsymLUT::create( nMaxOctantDepth , nInputBitDepth , nInputBitDepthC, nOutputBitDepth , nOutputBitDepthC, nMaxYPartNumLog2 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+    , 1 << ( nInputBitDepthC - 1 ) , 1 << ( nInputBitDepthC - 1 )
+#endif
+    );
   xAllocate3DArray( m_pColorInfo , xGetYSize() , xGetUSize() , xGetVSize() );
   xAllocate3DArray( m_pColorInfoC , xGetYSize() , xGetUSize() , xGetVSize() );
@@ -58,4 +61,67 @@
 }
 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Double TEnc3DAsymLUT::xxDeriveVertexPerColor( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
+  Pel & rP0 , Pel & rP1 , Pel & rP3 , Pel & rP7 , Int nResQuantBit )
+{
+  Int nInitP0 = rP0;
+  Int nInitP1 = rP1;
+  Int nInitP3 = rP3;
+  Int nInitP7 = rP7;
+
+  const Int nOne = xGetNormCoeffOne();
+  Double dNorm = (N * yy * vv * uu - N * yy * uv * uv - N * yv * yv * uu - N * vv * yu * yu + 2 * N * yv * uv * yu - yy * vs * vs * uu + 2 * yy * vs * uv * us - yy * vv * us * us - 2 * vs * uv * yu * ys + uv * uv * ys * ys + vs * vs * yu * yu - 2 * yv * vs * us * yu + 2 * yv * vs * ys * uu - 2 * yv * uv * us * ys + 2 * vv * yu * ys * us - vv * uu * ys * ys + yv * yv * us * us);
+  if( N > 16 && dNorm != 0 )
+  {
+    Double dInitA = (-N * uu * yv * Yv + N * uu * Yy * vv - N * Yy * uv * uv + N * yv * uv * Yu - N * yu * Yu * vv + N * yu * uv * Yv + yu * us * Ys * vv - vs * ys * uv * Yu - yu * vs * us * Yv - yv * uv * us * Ys - yv * vs * us * Yu - yu * uv * vs * Ys - ys * us * uv * Yv + ys * us * Yu * vv + 2 * Yy * vs * uv * us + uu * yv * vs * Ys - uu * ys * Ys * vv + uu * vs * ys * Yv + ys * Ys * uv * uv - Yy * vv * us * us + yu * Yu * vs * vs + yv * Yv * us * us - uu * Yy * vs * vs) / dNorm;
+    Double dInitB = (N * yy * Yu * vv - N * yy * uv * Yv - N * Yu * yv * yv - N * yu * Yy * vv + N * uv * yv * Yy + N * yv * yu * Yv - yy * us * Ys * vv + yy * uv * vs * Ys - yy * Yu * vs * vs + yy * vs * us * Yv - uv * vs * ys * Yy - yv * yu * vs * Ys + yu * Yy * vs * vs + yu * ys * Ys * vv - uv * yv * ys * Ys + 2 * Yu * yv * vs * ys + us * ys * Yy * vv - vs * ys * yu * Yv + uv * ys * ys * Yv + us * Ys * yv * yv - Yu * ys * ys * vv - yv * ys * us * Yv - vs * us * yv * Yy) / dNorm;
+    Double dInitC = -(-N * yy * Yv * uu + N * yy * uv * Yu - N * yv * yu * Yu - N * uv * yu * Yy + N * Yv * yu * yu + N * yv * Yy * uu - yy * uv * us * Ys + yy * Yv * us * us + yy * vs * Ys * uu - yy * vs * us * Yu + yv * ys * us * Yu - vs * Ys * yu * yu - yv * ys * Ys * uu + vs * us * yu * Yy + vs * ys * yu * Yu - uv * Yu * ys * ys + Yv * uu * ys * ys - yv * Yy * us * us - 2 * Yv * yu * ys * us - vs * ys * Yy * uu + uv * us * ys * Yy + uv * yu * ys * Ys + yv * yu * us * Ys) / dNorm;
+    nInitP0 = ( Int )( dInitA * nOne + 0.5 ) >> nResQuantBit << nResQuantBit;
+    nInitP1 = ( Int )( dInitB * nOne + 0.5 ) >> nResQuantBit << nResQuantBit;
+    nInitP3 = ( Int )( dInitC * nOne + 0.5 ) >> nResQuantBit << nResQuantBit;
+  }
+
+  Int nMin = - ( 1 << ( m_nLUTBitDepth - 1 ) );
+  Int nMax = - nMin - ( 1 << nResQuantBit  );
+  Int nMask = ( 1 << nResQuantBit ) - 1;
+
+  Double dMinError = MAX_DOUBLE;
+  Int nTestRange = 2;
+  Int nStepSize = 1 << nResQuantBit;
+  for( Int i = - nTestRange ; i <= nTestRange ; i++ )
+  {
+    for( Int j = - nTestRange ; j <= nTestRange ; j++ )
+    {
+      for( Int k = - nTestRange ; k <= nTestRange ; k++ )
+      {
+        Int nTestP0 = Clip3( nMin , nMax , nInitP0 + i * nStepSize );
+        Int nTestP1 = Clip3( nMin , nMax , nInitP1 + j * nStepSize );
+        Int nTestP3 = Clip3( nMin , nMax , nInitP3 + k * nStepSize );
+        Double a = 1.0 * nTestP0 / nOne;
+        Double b = 1.0 * nTestP1 / nOne;
+        Double c = 1.0 * nTestP3 / nOne;
+        Double d = ( Ys - a * ys - b * us - c * vs ) / N;
+        nInitP7 = ( ( Int )d ) >> nResQuantBit << nResQuantBit;
+        for( Int m = 0 ; m < 2 ; m++ )
+        {
+          Int nTestP7 = Clip3( nMin , nMax , nInitP7 + m * nStepSize );
+          Double dError = xxCalEstDist( N , Ys , Yy , Yu , Yv , ys , us , vs , yy , yu , yv , uu , uv , vv , YY , a , b , c , nTestP7 );
+          if( dError < dMinError )
+          {
+            dMinError = dError;
+            rP0 = ( Pel )nTestP0;
+            rP1 = ( Pel )nTestP1;
+            rP3 = ( Pel )nTestP3;
+            rP7 = ( Pel )nTestP7;
+          }
+        }
+      }
+    }
+  }
+  assert( !( rP0 & nMask ) && !( rP1 & nMask ) && !( rP3 & nMask ) && !( rP7 & nMask ) );
+
+  return( dMinError );
+}
+#else
 Double TEnc3DAsymLUT::xxDeriveVertexPerColor( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
   Int y0 , Int u0 , Int v0 , Int nLengthY , Int nLengthUV ,
@@ -134,5 +200,42 @@
   return( dMinError );
 }
-
+#endif
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Double TEnc3DAsymLUT::estimateDistWithCur3DAsymLUT( TComPic * pCurPic , UInt refLayerIdc )
+{
+  xxCollectData( pCurPic , refLayerIdc );
+
+  Double dErrorLuma = 0 , dErrorChroma = 0;
+  Int nYSize = 1 << ( getCurOctantDepth() + getCurYPartNumLog2() );
+  Int nUVSize = 1 << getCurOctantDepth();
+  for( Int yIdx = 0 ; yIdx < nYSize ; yIdx++ )
+  {
+    for( Int uIdx = 0 ; uIdx < nUVSize ; uIdx++ )
+    {
+      for( Int vIdx = 0 ; vIdx < nUVSize ; vIdx++ )
+      {
+        SColorInfo & rCuboidColorInfo = m_pColorInfo[yIdx][uIdx][vIdx];
+        SColorInfo & rCuboidColorInfoC = m_pColorInfoC[yIdx][uIdx][vIdx];
+        SCuboid & rCuboid = xGetCuboid( yIdx , uIdx , vIdx );
+        if( rCuboidColorInfo.N > 0 )
+        {
+          dErrorLuma += xxCalEstDist( rCuboidColorInfo.N , rCuboidColorInfo.Ys , rCuboidColorInfo.Yy , rCuboidColorInfo.Yu , rCuboidColorInfo.Yv , rCuboidColorInfo.ys , rCuboidColorInfo.us , rCuboidColorInfo.vs , rCuboidColorInfo.yy , rCuboidColorInfo.yu , rCuboidColorInfo.yv , rCuboidColorInfo.uu , rCuboidColorInfo.uv , rCuboidColorInfo.vv , rCuboidColorInfo.YY ,
+            rCuboid.P[0].Y , rCuboid.P[1].Y , rCuboid.P[2].Y , rCuboid.P[3].Y );
+        }
+        if( rCuboidColorInfoC.N > 0 )
+        {
+          dErrorChroma += xxCalEstDist( rCuboidColorInfoC.N , rCuboidColorInfoC.Us , rCuboidColorInfoC.Uy , rCuboidColorInfoC.Uu , rCuboidColorInfoC.Uv , rCuboidColorInfoC.ys , rCuboidColorInfoC.us , rCuboidColorInfoC.vs , rCuboidColorInfoC.yy , rCuboidColorInfoC.yu , rCuboidColorInfoC.yv , rCuboidColorInfoC.uu , rCuboidColorInfoC.uv , rCuboidColorInfoC.vv , rCuboidColorInfoC.UU ,
+            rCuboid.P[0].U , rCuboid.P[1].U , rCuboid.P[2].U , rCuboid.P[3].U );
+          dErrorChroma += xxCalEstDist( rCuboidColorInfoC.N , rCuboidColorInfoC.Vs , rCuboidColorInfoC.Vy , rCuboidColorInfoC.Vu , rCuboidColorInfoC.Vv , rCuboidColorInfoC.ys , rCuboidColorInfoC.us , rCuboidColorInfoC.vs , rCuboidColorInfoC.yy , rCuboidColorInfoC.yu , rCuboidColorInfoC.yv , rCuboidColorInfoC.uu , rCuboidColorInfoC.uv , rCuboidColorInfoC.vv , rCuboidColorInfoC.VV ,
+            rCuboid.P[0].V , rCuboid.P[1].V , rCuboid.P[2].V , rCuboid.P[3].V );
+        }
+      }
+    }
+  }
+
+  return( dErrorLuma + dErrorChroma);
+}
+#else
 Double TEnc3DAsymLUT::estimateDistWithCur3DAsymLUT( TComPic * pCurPic , UInt refLayerIdc )
 {
@@ -174,5 +277,66 @@
   return( dErrorLuma + dErrorChroma);
 }
-
+#endif
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Double TEnc3DAsymLUT::derive3DAsymLUT( TComSlice * pSlice , TComPic * pCurPic , UInt refLayerIdc , TEncCfg * pCfg , Bool bSignalPPS , Bool bElRapSliceTypeB )
+{
+  m_nLUTBitDepth = pCfg->getCGSLUTBit();
+  Int nCurYPartNumLog2 = 0 , nCurOctantDepth = 0; 
+  xxDerivePartNumLog2( pSlice , pCfg , nCurOctantDepth , nCurYPartNumLog2 , bSignalPPS , bElRapSliceTypeB );
+
+  Int nBestResQuanBit = 0;
+  Int nBestAdaptCThresholdU = 1 << ( getInputBitDepthC() - 1 );
+  Int nBestAdaptCThresholdV = 1 << ( getInputBitDepthC() - 1 );
+  Int nBestOctantDepth = nCurOctantDepth;
+  Int nBestYPartNumLog2 = nCurYPartNumLog2;
+  Int nTargetLoop = 1 + ( pCfg->getCGSAdaptChroma() && ( nCurOctantDepth == 1 || ( nCurOctantDepth * 3 + nCurYPartNumLog2 ) >= 5 ) );
+  Double dMinError = MAX_DOUBLE;
+  for( Int nLoop = 0 ; nLoop < nTargetLoop ; nLoop++ )
+  {
+    Int nAdaptCThresholdU = 1 << ( getInputBitDepthC() - 1 );
+    Int nAdaptCThresholdV = 1 << ( getInputBitDepthC() - 1 );
+    if( nLoop > 0 )
+    {
+      nAdaptCThresholdU = ( Int )( m_dSumU / m_nNChroma + 0.5 );
+      nAdaptCThresholdV = ( Int )( m_dSumV / m_nNChroma + 0.5 );
+      if( nCurOctantDepth > 1 )
+      {
+        nCurOctantDepth = 1;
+        nCurYPartNumLog2 = 2;
+      }
+      if( nAdaptCThresholdU == nBestAdaptCThresholdU && nAdaptCThresholdV == nBestAdaptCThresholdV 
+        && nCurOctantDepth == nBestOctantDepth && nCurYPartNumLog2 == nBestYPartNumLog2 )
+        break;
+    }
+    xUpdatePartitioning( nCurOctantDepth , nCurYPartNumLog2 , nAdaptCThresholdU , nAdaptCThresholdV );
+    xxCollectData( pCurPic , refLayerIdc );
+    for( Int nResQuanBit = 0 ; nResQuanBit < 4 ; nResQuanBit++ )
+    {
+      Double dError = xxDeriveVertexes( nResQuanBit , m_pEncCuboid ) / ( 1 + ( nResQuanBit > 0 ) * 0.001 * ( pSlice->getDepth() + 1 ) );
+      if( dError <= dMinError )
+      {
+        nBestResQuanBit = nResQuanBit;
+        nBestAdaptCThresholdU = nAdaptCThresholdU;
+        nBestAdaptCThresholdV = nAdaptCThresholdV;
+        nBestOctantDepth = nCurOctantDepth;
+        nBestYPartNumLog2 = nCurYPartNumLog2;
+        SCuboid *** tmp = m_pBestEncCuboid;
+        m_pBestEncCuboid = m_pEncCuboid;
+        m_pEncCuboid = tmp;
+        dMinError = dError;
+      }
+      else
+      {
+        break;
+      }
+    }
+  }
+  setResQuantBit( nBestResQuanBit );
+  xUpdatePartitioning( nBestOctantDepth , nBestYPartNumLog2 , nBestAdaptCThresholdU , nBestAdaptCThresholdV );
+  xSaveCuboids( m_pBestEncCuboid );
+  return( dMinError );
+}
+#else
 Double TEnc3DAsymLUT::derive3DAsymLUT( TComSlice * pSlice , TComPic * pCurPic , UInt refLayerIdc , TEncCfg * pCfg , Bool bSignalPPS , Bool bElRapSliceTypeB )
 {
@@ -204,7 +368,7 @@
   setResQuantBit( nBestResQuanBit );
   xSaveCuboids( m_pBestEncCuboid );
-
   return( dCurError );
 }
+#endif
 
 Double TEnc3DAsymLUT::xxDeriveVertexes( Int nResQuanBit , SCuboid *** pCurCuboid )
@@ -213,6 +377,8 @@
   Int nYSize = 1 << ( getCurOctantDepth() + getCurYPartNumLog2() );
   Int nUVSize = 1 << getCurOctantDepth();
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
   Int nLengthY = 1 << ( getInputBitDepthY() - getCurOctantDepth() - getCurYPartNumLog2() );
   Int nLengthUV = 1 << ( getInputBitDepthC() - getCurOctantDepth() );
+#endif
   for( Int yIdx = 0 ; yIdx < nYSize ; yIdx++ )
   {
@@ -224,7 +390,9 @@
         SColorInfo & rCuboidColorInfoC = m_pColorInfoC[yIdx][uIdx][vIdx];
         SCuboid & rCuboid = pCurCuboid[yIdx][uIdx][vIdx];
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
         Int y0 = yIdx << xGetYShift2Idx();
         Int u0 = uIdx << xGetUShift2Idx();
         Int v0 = vIdx << xGetVShift2Idx();
+#endif
         for( Int idxVertex = 0 ; idxVertex < 4 ; idxVertex++ )
         {
@@ -235,12 +403,21 @@
         {
           dErrorLuma += xxDeriveVertexPerColor( rCuboidColorInfo.N , rCuboidColorInfo.Ys , rCuboidColorInfo.Yy , rCuboidColorInfo.Yu , rCuboidColorInfo.Yv , rCuboidColorInfo.ys , rCuboidColorInfo.us , rCuboidColorInfo.vs , rCuboidColorInfo.yy , rCuboidColorInfo.yu , rCuboidColorInfo.yv , rCuboidColorInfo.uu , rCuboidColorInfo.uv , rCuboidColorInfo.vv , rCuboidColorInfo.YY ,
-            y0 , u0 , v0 , nLengthY , nLengthUV , rCuboid.P[0].Y , rCuboid.P[1].Y , rCuboid.P[2].Y , rCuboid.P[3].Y , nResQuanBit );
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
+            y0 , u0 , v0 , nLengthY , nLengthUV , 
+#endif
+            rCuboid.P[0].Y , rCuboid.P[1].Y , rCuboid.P[2].Y , rCuboid.P[3].Y , nResQuanBit );
         }
         if( rCuboidColorInfoC.N > 0 )
         {
           dErrorChroma += xxDeriveVertexPerColor( rCuboidColorInfoC.N , rCuboidColorInfoC.Us , rCuboidColorInfoC.Uy , rCuboidColorInfoC.Uu , rCuboidColorInfoC.Uv , rCuboidColorInfoC.ys , rCuboidColorInfoC.us , rCuboidColorInfoC.vs , rCuboidColorInfoC.yy , rCuboidColorInfoC.yu , rCuboidColorInfoC.yv , rCuboidColorInfoC.uu , rCuboidColorInfoC.uv , rCuboidColorInfoC.vv , rCuboidColorInfoC.UU ,
-            y0 , u0 , v0 , nLengthY , nLengthUV , rCuboid.P[0].U , rCuboid.P[1].U , rCuboid.P[2].U , rCuboid.P[3].U , nResQuanBit );
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
+            y0 , u0 , v0 , nLengthY , nLengthUV , 
+#endif
+            rCuboid.P[0].U , rCuboid.P[1].U , rCuboid.P[2].U , rCuboid.P[3].U , nResQuanBit );
           dErrorChroma += xxDeriveVertexPerColor( rCuboidColorInfoC.N , rCuboidColorInfoC.Vs , rCuboidColorInfoC.Vy , rCuboidColorInfoC.Vu , rCuboidColorInfoC.Vv , rCuboidColorInfoC.ys , rCuboidColorInfoC.us , rCuboidColorInfoC.vs , rCuboidColorInfoC.yy , rCuboidColorInfoC.yu , rCuboidColorInfoC.yv , rCuboidColorInfoC.uu , rCuboidColorInfoC.uv , rCuboidColorInfoC.vv , rCuboidColorInfoC.VV ,
-            y0 , u0 , v0 , nLengthY , nLengthUV , rCuboid.P[0].V , rCuboid.P[1].V , rCuboid.P[2].V , rCuboid.P[3].V , nResQuanBit );
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
+            y0 , u0 , v0 , nLengthY , nLengthUV , 
+#endif
+            rCuboid.P[0].V , rCuboid.P[1].V , rCuboid.P[2].V , rCuboid.P[3].V , nResQuanBit );
         }
 
@@ -299,4 +476,8 @@
   // since we do data collection only for overlapped region, the border extension is good enough
 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  m_dSumU = m_dSumV = 0;
+  m_nNChroma = 0;
+#endif
   for( Int i = top ; i <= bottom ; i++ )
   {
@@ -330,6 +511,15 @@
       }
 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+      m_dSumU += u;
+      m_dSumV += v;
+      m_nNChroma++;
+#endif
       SColorInfo sColorInfo;
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+      SColorInfo & rCuboidColorInfo = m_pColorInfo[xGetYIdx(y)][xGetUIdx(u)][xGetVIdx(v)];
+#else
       SColorInfo & rCuboidColorInfo = m_pColorInfo[y>>xGetYShift2Idx()][u>>xGetUShift2Idx()][v>>xGetVShift2Idx()];
+#endif
       memset(&sColorInfo, 0, sizeof(SColorInfo));
       sColorInfo.Ys = Y;
@@ -358,5 +548,9 @@
         u = pIRLU[posIRLUV];
         v = pIRLV[posIRLUV];
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+        SColorInfo & rCuboidColorInfoC = m_pColorInfoC[xGetYIdx(y)][xGetUIdx(u)][xGetVIdx(v)];
+#else
         SColorInfo & rCuboidColorInfoC = m_pColorInfoC[y>>xGetYShift2Idx()][u>>xGetUShift2Idx()][v>>xGetVShift2Idx()];
+#endif
         sColorInfo.Us = U;
         sColorInfo.Vs = V;
@@ -389,21 +583,4 @@
 Void TEnc3DAsymLUT::xxDerivePartNumLog2( TComSlice * pSlice , TEncCfg * pcCfg , Int & rOctantDepth , Int & rYPartNumLog2 , Bool bSignalPPS , Bool bElRapSliceTypeB )
 {
-  Int nSliceType = pSlice->getSliceType();
-  // update slice type as what will be done later
-  if( pSlice->getActiveNumILRRefIdx() == 0 && pSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA )
-  {
-    nSliceType = I_SLICE;
-  }
-  else if( !bElRapSliceTypeB )
-  {
-    if( (pSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP) &&
-      (pSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA) &&
-      pSlice->getSliceType() == B_SLICE )
-    {
-      nSliceType = P_SLICE;
-    }
-  }
-
-  const Int nSliceTempLevel = pSlice->getDepth();
   Int nPartNumLog2 = 4;
   if( pSlice->getBaseColPic( pSlice->getInterLayerPredLayerIdc( 0 ) )->getSlice( 0 )->isIntra() )
@@ -411,8 +588,8 @@
     nPartNumLog2 = xGetMaxPartNumLog2();
   }
-  if( m_nPrevFrameBit[nSliceType][nSliceTempLevel] && pSlice->getPPS()->getCGSFlag() ) 
-  {
-    Double dBitCost = 1.0 * m_nPrevFrameCGSBit[nSliceType][nSliceTempLevel] / m_nPrevFrameBit[nSliceType][nSliceTempLevel];
-    nPartNumLog2 = m_nPrevFrameCGSPartNumLog2[nSliceType][nSliceTempLevel];
+  if( m_nAccuFrameBit && pSlice->getPPS()->getCGSFlag() ) 
+  {
+    Double dBitCost = 1.0 * m_nAccuFrameCGSBit / m_nAccuFrameBit;
+    nPartNumLog2 = m_nPrevFrameCGSPartNumLog2;
     Double dBitCostT = 0.03;
     if( dBitCost < dBitCostT / 6.0 )
@@ -425,9 +602,5 @@
     }
   }
-  else
-  {
-    nPartNumLog2 -= nSliceTempLevel;
-  }
-  nPartNumLog2 = Clip3( 0 , xGetMaxPartNumLog2() , nPartNumLog2 );
+  nPartNumLog2 = Clip3( 0 , xGetMaxPartNumLog2()  , nPartNumLog2 );
   xxMapPartNum2DepthYPart( nPartNumLog2 , rOctantDepth , rYPartNumLog2 );
 }
@@ -453,18 +626,14 @@
 Void TEnc3DAsymLUT::updatePicCGSBits( TComSlice * pcSlice , Int nPPSBit )
 {
-  const Int nSliceType = pcSlice->getSliceType();
-  const Int nSliceTempLevel = pcSlice->getDepth();
-
   for( Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
   {
     UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
-    m_nPrevFrameBit[nSliceType][nSliceTempLevel] = pcSlice->getPic()->getFrameBit() + pcSlice->getBaseColPic(refLayerIdc)->getFrameBit();
+    m_nAccuFrameBit += pcSlice->getPic()->getFrameBit() + pcSlice->getBaseColPic(refLayerIdc)->getFrameBit();
     m_dTotalFrameBit += pcSlice->getPic()->getFrameBit() + pcSlice->getBaseColPic(refLayerIdc)->getFrameBit();
   }
-  m_nPrevFrameOverWritePPS[nSliceType][nSliceTempLevel] = pcSlice->getCGSOverWritePPS();
-  m_nPrevFrameCGSBit[nSliceType][nSliceTempLevel] = nPPSBit;
+  m_nAccuFrameCGSBit += nPPSBit;
   m_nTotalCGSBit += nPPSBit;
-  m_nPrevFrameCGSPartNumLog2[nSliceType][nSliceTempLevel] = getCurOctantDepth() * 3 + getCurYPartNumLog2();
-}
-
-#endif
+  m_nPrevFrameCGSPartNumLog2 = getCurOctantDepth() * 3 + getCurYPartNumLog2();
+}
+
+#endif
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.h	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.h	(revision 825)
@@ -78,17 +78,26 @@
   SCuboid *** m_pEncCuboid;
   SCuboid *** m_pBestEncCuboid;
-  Int   m_nPrevFrameBit[3][MAX_TLAYER];                  // base + enhancement layer
-  Int   m_nPrevFrameCGSBit[3][MAX_TLAYER];
-  Int   m_nPrevFrameCGSPartNumLog2[3][MAX_TLAYER];
-  Int   m_nPrevFrameOverWritePPS[3][MAX_TLAYER];
+  Int   m_nAccuFrameBit;                  // base + enhancement layer
+  Int   m_nAccuFrameCGSBit;
+  Int   m_nPrevFrameCGSPartNumLog2;
   Double m_dTotalFrameBit;
   Int   m_nTotalCGSBit;
   Int   m_nPPSBit;
   Int   m_nLUTBitDepth;
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Double m_dSumU;
+  Double m_dSumV;
+  Int    m_nNChroma;
+#endif
 
 private:
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Double  xxDeriveVertexPerColor( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
+    Pel & rP0 , Pel & rP1 , Pel & rP3 , Pel & rP7 , Int nResQuantBit );
+#else
   Double  xxDeriveVertexPerColor( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
     Int y0 , Int u0 , Int v0 , Int nLengthY , Int nLengthUV ,
     Pel & rP0 , Pel & rP1 , Pel & rP3 , Pel & rP7 , Int nResQuantBit );
+#endif
   Void    xxDerivePartNumLog2( TComSlice * pSlice , TEncCfg * pcCfg , Int & rOctantDepth , Int & rYPartNumLog2 , Bool bSignalPPS , Bool bElRapSliceTypeB );
   Void    xxMapPartNum2DepthYPart( Int nPartNumLog2 , Int & rOctantDepth , Int & rYPartNumLog2 );
@@ -100,4 +109,8 @@
   inline Double  xxCalEstDist( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
     Double a , Double b , Double c , Double d );
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  inline Double  xxCalEstDist( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
+    Pel nP0 , Pel nP1 , Pel nP3 , Pel nP7 );
+#endif
 };
 
@@ -118,5 +131,20 @@
   return( dError );
 };
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Double TEnc3DAsymLUT::xxCalEstDist( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
+  Pel nP0 , Pel nP1 , Pel nP3 , Pel nP7 )
+{
+  const Int nOne = xGetNormCoeffOne();
+  Double a = 1.0 * nP0 / nOne;
+  Double b = 1.0 * nP1 / nOne;
+  Double c = 1.0 * nP3 / nOne;
+  Double d = nP7;
+  Double dError = N * d * d + 2 * b * c * uv + 2 * a * c * yv + 2 * a * b * yu - 2 * c * Yv - 2 * b * Yu - 2 * a * Yy + 2 * c * d * vs + 2 * b * d * us + 2 * a * d * ys + a * a * yy + c * c * vv + b * b * uu - 2 * d * Ys + YY;
+  return( dError );
+};
 #endif
 
 #endif
+
+#endif
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 825)
@@ -2762,4 +2762,12 @@
 Void TEncCavlc::xCode3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT )
 {
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  UInt uiNumRefLayers = ( UInt )pc3DAsymLUT->getRefLayerNum();
+  WRITE_UVLC( uiNumRefLayers - 1 , "num_cm_ref_layers_minus1" );
+  for( UInt i = 0 ; i < uiNumRefLayers ; i++ )
+  {
+    WRITE_CODE( pc3DAsymLUT->getRefLayerId( i ) , 6 , "cm_ref_layer_id" );
+  }
+#endif
   assert( pc3DAsymLUT->getCurOctantDepth() < 4 );
   WRITE_CODE( pc3DAsymLUT->getCurOctantDepth() , 2 , "cm_octant_depth" );
@@ -2767,4 +2775,10 @@
   WRITE_CODE( pc3DAsymLUT->getCurYPartNumLog2() , 2 , "cm_y_part_num_log2" );
   assert( pc3DAsymLUT->getInputBitDepthY() < 16 );
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  WRITE_CODE( pc3DAsymLUT->getInputBitDepthY() - 8 , 3 , "cm_input_luma_bit_depth_minus8" );
+  WRITE_CODE( pc3DAsymLUT->getInputBitDepthC() - 8 , 3 , "cm_input_chroma_bit_depth_minus8" );
+  WRITE_CODE( pc3DAsymLUT->getOutputBitDepthY() - 8 , 3 , "cm_output_luma_bit_depth_minus8" );
+  WRITE_CODE( pc3DAsymLUT->getOutputBitDepthC() - 8 , 3 , "cm_output_chroma_bit_depth_minus8" );
+#else
   WRITE_CODE( pc3DAsymLUT->getInputBitDepthY() - 8 , 3 , "cm_input_bit_depth_minus8" );
   WRITE_SVLC(pc3DAsymLUT->getInputBitDepthC()-pc3DAsymLUT->getInputBitDepthY(), "cm_input_bit_depth_chroma delta");
@@ -2772,8 +2786,22 @@
   WRITE_CODE( pc3DAsymLUT->getOutputBitDepthY() - 8 , 3 , "cm_output_bit_depth_minus8" );
   WRITE_SVLC(pc3DAsymLUT->getOutputBitDepthC()-pc3DAsymLUT->getOutputBitDepthY(), "cm_output_bit_depth_chroma_delta");
+#endif
   assert( pc3DAsymLUT->getResQuantBit() < 4 );
   WRITE_CODE( pc3DAsymLUT->getResQuantBit() , 2 , "cm_res_quant_bit" );
-
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  if( pc3DAsymLUT->getCurOctantDepth() == 1 )
+  {
+    WRITE_SVLC( pc3DAsymLUT->getAdaptChromaThresholdU() - ( 1 << ( pc3DAsymLUT->getInputBitDepthC() - 1 ) ) , "cm_adapt_threshold_u_delta" );
+    WRITE_SVLC( pc3DAsymLUT->getAdaptChromaThresholdV() - ( 1 << ( pc3DAsymLUT->getInputBitDepthC() - 1 ) ) , "cm_adapt_threshold_v_delta" );
+  }
+#endif
+
+#if R0164_CGS_LUT_BUGFIX
+  pc3DAsymLUT->xInitCuboids();
+#endif
   xCode3DAsymLUTOctant( pc3DAsymLUT , 0 , 0 , 0 , 0 , 1 << pc3DAsymLUT->getCurOctantDepth() );
+#if R0164_CGS_LUT_BUGFIX
+  pc3DAsymLUT->xCuboidsExplicitCheck( false );
+#endif
 }
 
@@ -2802,19 +2830,48 @@
     for( Int l = 0 ; l < nYPartNum ; l++ )
     {
+#if R0164_CGS_LUT_BUGFIX      
+      Int shift = pc3DAsymLUT->getMaxOctantDepth() - nDepth ;
+#endif
       for( Int nVertexIdx = 0 ; nVertexIdx < 4 ; nVertexIdx++ )
       {
+#if R0164_CGS_LUT_BUGFIX 
+        SYUVP sRes = pc3DAsymLUT->getCuboidVertexResTree( yIdx + (l<<shift) , uIdx , vIdx , nVertexIdx );
+#else
         SYUVP sRes = pc3DAsymLUT->getCuboidVertexResTree( yIdx + l , uIdx , vIdx , nVertexIdx );
+#endif
         UInt uiCodeVertex = sRes.Y != 0 || sRes.U != 0 || sRes.V != 0;
         WRITE_FLAG( uiCodeVertex , "coded_vertex_flag" );
         if( uiCodeVertex )
         {
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+          xWriteParam( sRes.Y );
+          xWriteParam( sRes.U );
+          xWriteParam( sRes.V );
+#else
           WRITE_SVLC( sRes.Y , "resY" );
           WRITE_SVLC( sRes.U , "resU" );
           WRITE_SVLC( sRes.V , "resV" );
-        }
-      }
-    }
-  }
-}
+#endif
+        }
+      }
+#if R0164_CGS_LUT_BUGFIX
+      pc3DAsymLUT->xSetExplicit( yIdx + (l<<shift) , uIdx , vIdx );
+#endif
+    }
+  }
+}
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Void TEncCavlc::xWriteParam( Int param)
+{
+  const UInt rParam = 7;
+  Int codeNumber = abs(param);
+  WRITE_UVLC(codeNumber / (1 << rParam), "quotient");
+  WRITE_CODE((codeNumber % (1 << rParam)), rParam, "remainder");
+  if (abs(param))
+    WRITE_FLAG( param <0, "sign");
+}
+#endif
+
 #endif
 //! \}
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h	(revision 825)
@@ -173,4 +173,7 @@
   Void xCode3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT );
   Void xCode3DAsymLUTOctant( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength );
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Void xWriteParam( Int param);
+#endif
 #endif
 #endif //SVC_EXTENSION
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h	(revision 825)
@@ -367,4 +367,7 @@
   Int  m_nCGSMaxYPartNumLog2;
   Int  m_nCGSLUTBit;
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Int  m_nCGSAdaptiveChroma;
+#endif
 #endif
 #if P0050_KNEE_FUNCTION_SEI
@@ -940,4 +943,8 @@
   Void      setCGSLUTBit(Int n)           { m_nCGSLUTBit = n;    }
   Int       getCGSLUTBit()                { return m_nCGSLUTBit; }
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Void      setCGSAdaptChroma(Int n)      { m_nCGSAdaptiveChroma = n;  }
+  Int       getCGSAdaptChroma()           { return m_nCGSAdaptiveChroma; }
+#endif
 #endif
 #endif
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 824)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 825)
@@ -1153,4 +1153,9 @@
         if( pcSlice->getPPS()->getCGSFlag() )
         {
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+          // all reference layers are currently taken as CGS reference layers
+          m_Enc3DAsymLUTPPS.addRefLayerId( pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc) );
+          m_Enc3DAsymLUTPicUpdate.addRefLayerId( pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc) );
+#endif
           if(g_posScalingFactor[refLayerIdc][0] < (1<<16) || g_posScalingFactor[refLayerIdc][1] < (1<<16)) //if(pcPic->isSpatialEnhLayer(refLayerIdc))
           {
@@ -3987,4 +3992,6 @@
   if( m_layerId && pcSlice->getPPS()->getCGSFlag() )
   {
+    if( m_Enc3DAsymLUTPPS.getPPSBit() > 0 )
+      m_Enc3DAsymLUTPicUpdate.copy3DAsymLUT( &m_Enc3DAsymLUTPPS );
     m_Enc3DAsymLUTPicUpdate.updatePicCGSBits( pcSlice , m_Enc3DAsymLUTPPS.getPPSBit() );
   }
