Index: trunk/source/Lib/TLibDecoder/TDecCAVLC.cpp
===================================================================
--- trunk/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 1196)
+++ trunk/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 1313)
@@ -2,5 +2,5 @@
 * License, included below. This software may be subject to other third party
 * and contributor rights, including patent rights, and no such rights are
-* granted under this license.  
+* granted under this license.
 *
 * Copyright (c) 2010-2015, ITU/ISO/IEC
@@ -39,52 +39,33 @@
 #include "SEIread.h"
 #include "TDecSlice.h"
-#if H_3D
+#include "TLibCommon/TComChromaFormat.h"
+#if RExt__DECODER_DEBUG_BIT_STATISTICS
+#include "TLibCommon/TComCodingStatistics.h"
+#endif
+#if NH_MV
 #include "TDecTop.h"
 #endif
+
 //! \ingroup TLibDecoder
 //! \{
 
 #if ENC_DEC_TRACE
-
-Void  xTraceSPSHeader (TComSPS *pSPS)
-{
-#if H_MV_ENC_DEC_TRAC
-  if ( g_disableHLSTrace )
-  {
-    return; 
-  }
-  // To avoid mismatches
-  fprintf( g_hTrace, "=========== Sequence Parameter Set LayerId: %d ===========\n", pSPS->getLayerId() );
-#else
-  fprintf( g_hTrace, "=========== Sequence Parameter Set ID: %d ===========\n", pSPS->getSPSId() );
-#endif
-}
-
-Void  xTracePPSHeader (TComPPS *pPPS)
-{
-#if H_MV_ENC_DEC_TRAC
-  if ( g_disableHLSTrace )
-  {
-    return; 
-  }
-  fprintf( g_hTrace, "=========== Picture Parameter Set LayerId: %d ===========\n", pPPS->getLayerId() );
-#else
-  fprintf( g_hTrace, "=========== Picture Parameter Set ID: %d ===========\n", pPPS->getPPSId() );
-#endif
-}
-
-Void  xTraceSliceHeader (TComSlice *pSlice)
-{
-#if H_MV_ENC_DEC_TRAC
-  if ( g_disableHLSTrace )
-  {
-    return; 
-  }
-#endif
-  fprintf( g_hTrace, "=========== Slice ===========\n");
-}
-
-#endif
-
+#if !H_MV_ENC_DEC_TRAC
+Void  xTraceVPSHeader ()
+{
+  fprintf( g_hTrace, "=========== Video Parameter Set     ===========\n" );
+}
+
+Void  xTraceSPSHeader ()
+{
+  fprintf( g_hTrace, "=========== Sequence Parameter Set  ===========\n" );
+}
+
+Void  xTracePPSHeader ()
+{
+  fprintf( g_hTrace, "=========== Picture Parameter Set  ===========\n");
+}
+#endif
+#endif
 // ====================================================================================================================
 // Constructor / destructor / create / destroy
@@ -97,4 +78,5 @@
 TDecCavlc::~TDecCavlc()
 {
+
 }
 
@@ -103,5 +85,5 @@
 // ====================================================================================================================
 
-void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComReferencePictureSet* rps, Int idx )
+Void TDecCavlc::parseShortTermRefPicSet( TComSPS* sps, TComReferencePictureSet* rps, Int idx )
 {
   UInt code;
@@ -117,5 +99,5 @@
   }
 
-  if (interRPSPred) 
+  if (interRPSPred)
   {
     UInt bit;
@@ -138,7 +120,7 @@
     for(Int j=0 ; j <= rpsRef->getNumberOfPictures(); j++)
     {
-      READ_CODE(1, bit, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1 
+      READ_CODE(1, bit, "used_by_curr_pic_flag" ); //first bit is "1" if Idc is 1
       Int refIdc = bit;
-      if (refIdc == 0) 
+      if (refIdc == 0)
       {
         READ_CODE(1, bit, "use_delta_flag" ); //second bit is "1" if Idc is 2, "0" otherwise.
@@ -155,13 +137,13 @@
           k0++;
         }
-        else 
+        else
         {
           k1++;
         }
         k++;
-      }  
-      rps->setRefIdc(j,refIdc);  
-    }
-    rps->setNumRefIdc(rpsRef->getNumberOfPictures()+1);  
+      }
+      rps->setRefIdc(j,refIdc);
+    }
+    rps->setNumRefIdc(rpsRef->getNumberOfPictures()+1);
     rps->setNumberOfPictures(k);
     rps->setNumberOfNegativePictures(k0);
@@ -199,12 +181,12 @@
 }
 
-#if H_3D
-Void TDecCavlc::parsePPS(TComPPS* pcPPS, TComVPS* pcVPS )
+Void TDecCavlc::parsePPS(TComPPS* pcPPS)
+{
+#if ENC_DEC_TRACE
+#if H_MV_ENC_DEC_TRAC
+  tracePSHeader( "PPS", pcPPS->getLayerId() ); 
 #else
-Void TDecCavlc::parsePPS(TComPPS* pcPPS)
-#endif
-{
-#if ENC_DEC_TRACE  
-  xTracePPSHeader (pcPPS);
+  xTracePPSHeader ();
+#endif
 #endif
   UInt  uiCode;
@@ -215,13 +197,15 @@
   assert(uiCode <= 63);
   pcPPS->setPPSId (uiCode);
-  
+
   READ_UVLC( uiCode, "pps_seq_parameter_set_id");
   assert(uiCode <= 15);
   pcPPS->setSPSId (uiCode);
-  
+
   READ_FLAG( uiCode, "dependent_slice_segments_enabled_flag"    );    pcPPS->setDependentSliceSegmentsEnabledFlag   ( uiCode == 1 );
+
   READ_FLAG( uiCode, "output_flag_present_flag" );                    pcPPS->setOutputFlagPresentFlag( uiCode==1 );
 
   READ_CODE(3, uiCode, "num_extra_slice_header_bits");                pcPPS->setNumExtraSliceHeaderBits(uiCode);
+
   READ_FLAG ( uiCode, "sign_data_hiding_flag" ); pcPPS->setSignHideFlag( uiCode );
 
@@ -231,13 +215,13 @@
   assert(uiCode <= 14);
   pcPPS->setNumRefIdxL0DefaultActive(uiCode+1);
-  
+
   READ_UVLC(uiCode, "num_ref_idx_l1_default_active_minus1");
   assert(uiCode <= 14);
   pcPPS->setNumRefIdxL1DefaultActive(uiCode+1);
-  
+
   READ_SVLC(iCode, "init_qp_minus26" );                            pcPPS->setPicInitQPMinus26(iCode);
   READ_FLAG( uiCode, "constrained_intra_pred_flag" );              pcPPS->setConstrainedIntraPred( uiCode ? true : false );
-  READ_FLAG( uiCode, "transform_skip_enabled_flag" );               
-  pcPPS->setUseTransformSkip ( uiCode ? true : false ); 
+  READ_FLAG( uiCode, "transform_skip_enabled_flag" );
+  pcPPS->setUseTransformSkip ( uiCode ? true : false );
 
   READ_FLAG( uiCode, "cu_qp_delta_enabled_flag" );            pcPPS->setUseDQP( uiCode ? true : false );
@@ -252,12 +236,14 @@
   }
   READ_SVLC( iCode, "pps_cb_qp_offset");
-  pcPPS->setChromaCbQpOffset(iCode);
-  assert( pcPPS->getChromaCbQpOffset() >= -12 );
-  assert( pcPPS->getChromaCbQpOffset() <=  12 );
+  pcPPS->setQpOffset(COMPONENT_Cb, iCode);
+  assert( pcPPS->getQpOffset(COMPONENT_Cb) >= -12 );
+  assert( pcPPS->getQpOffset(COMPONENT_Cb) <=  12 );
 
   READ_SVLC( iCode, "pps_cr_qp_offset");
-  pcPPS->setChromaCrQpOffset(iCode);
-  assert( pcPPS->getChromaCrQpOffset() >= -12 );
-  assert( pcPPS->getChromaCrQpOffset() <=  12 );
+  pcPPS->setQpOffset(COMPONENT_Cr, iCode);
+  assert( pcPPS->getQpOffset(COMPONENT_Cr) >= -12 );
+  assert( pcPPS->getQpOffset(COMPONENT_Cr) <=  12 );
+
+  assert(MAX_NUM_COMPONENT<=3);
 
   READ_FLAG( uiCode, "pps_slice_chroma_qp_offsets_present_flag" );
@@ -273,5 +259,5 @@
   READ_FLAG( uiCode, "tiles_enabled_flag"               );    pcPPS->setTilesEnabledFlag            ( uiCode == 1 );
   READ_FLAG( uiCode, "entropy_coding_sync_enabled_flag" );    pcPPS->setEntropyCodingSyncEnabledFlag( uiCode == 1 );
-  
+
   if( pcPPS->getTilesEnabledFlag() )
   {
@@ -280,29 +266,38 @@
     READ_FLAG ( uiCode, "uniform_spacing_flag" );                   pcPPS->setTileUniformSpacingFlag( uiCode == 1 );
 
-    if( !pcPPS->getTileUniformSpacingFlag())
-    {
-      std::vector<Int> columnWidth(pcPPS->getNumTileColumnsMinus1());
-      for(UInt i=0; i<pcPPS->getNumTileColumnsMinus1(); i++)
-      { 
-        READ_UVLC( uiCode, "column_width_minus1" );  
-        columnWidth[i] = uiCode+1;
-      }
-      pcPPS->setTileColumnWidth(columnWidth);
-
-      std::vector<Int> rowHeight (pcPPS->getTileNumRowsMinus1());
-      for(UInt i=0; i<pcPPS->getTileNumRowsMinus1(); i++)
-      {
-        READ_UVLC( uiCode, "row_height_minus1" );
-        rowHeight[i] = uiCode + 1;
-      }
-      pcPPS->setTileRowHeight(rowHeight);
-    }
-
-    if(pcPPS->getNumTileColumnsMinus1() !=0 || pcPPS->getTileNumRowsMinus1() !=0)
+    const UInt tileColumnsMinus1 = pcPPS->getNumTileColumnsMinus1();
+    const UInt tileRowsMinus1    = pcPPS->getNumTileRowsMinus1();
+ 
+    if ( !pcPPS->getTileUniformSpacingFlag())
+    {
+      if (tileColumnsMinus1 > 0)
+      {
+        std::vector<Int> columnWidth(tileColumnsMinus1);
+        for(UInt i = 0; i < tileColumnsMinus1; i++)
+        { 
+          READ_UVLC( uiCode, "column_width_minus1" );  
+          columnWidth[i] = uiCode+1;
+        }
+        pcPPS->setTileColumnWidth(columnWidth);
+      }
+
+      if (tileRowsMinus1 > 0)
+      {
+        std::vector<Int> rowHeight (tileRowsMinus1);
+        for(UInt i = 0; i < tileRowsMinus1; i++)
+        {
+          READ_UVLC( uiCode, "row_height_minus1" );
+          rowHeight[i] = uiCode + 1;
+        }
+        pcPPS->setTileRowHeight(rowHeight);
+      }
+    }
+
+    if ((tileColumnsMinus1 + tileRowsMinus1) != 0)
     {
       READ_FLAG ( uiCode, "loop_filter_across_tiles_enabled_flag" );   pcPPS->setLoopFilterAcrossTilesEnabledFlag( uiCode ? true : false );
     }
   }
-  READ_FLAG( uiCode, "loop_filter_across_slices_enabled_flag" );       pcPPS->setLoopFilterAcrossSlicesEnabledFlag( uiCode ? true : false );
+  READ_FLAG( uiCode, "pps_loop_filter_across_slices_enabled_flag" );   pcPPS->setLoopFilterAcrossSlicesEnabledFlag( uiCode ? true : false );
   READ_FLAG( uiCode, "deblocking_filter_control_present_flag" );       pcPPS->setDeblockingFilterControlPresentFlag( uiCode ? true : false );
   if(pcPPS->getDeblockingFilterControlPresentFlag())
@@ -319,6 +314,7 @@
   if(pcPPS->getScalingListPresentFlag ())
   {
-    parseScalingList( pcPPS->getScalingList() );
-  }
+    parseScalingList( &(pcPPS->getScalingList()) );
+  }
+
   READ_FLAG( uiCode, "lists_modification_present_flag");
   pcPPS->setListsModificationPresentFlag(uiCode);
@@ -329,201 +325,320 @@
   READ_FLAG( uiCode, "slice_segment_header_extension_present_flag");
   pcPPS->setSliceHeaderExtensionPresentFlag(uiCode);
-  
-#if H_MV
+
   READ_FLAG( uiCode, "pps_extension_present_flag");
-#else
-  READ_FLAG( uiCode, "pps_extension_flag");
-#endif
   if (uiCode)
   {
-
-#if H_MV
+#if NH_MV
     READ_FLAG( uiCode, "pps_range_extensions_flag" ); pcPPS->setPpsRangeExtensionsFlag( uiCode == 1 );
     READ_FLAG( uiCode, "pps_multilayer_extension_flag" ); pcPPS->setPpsMultilayerExtensionFlag( uiCode == 1 );
-#if !H_3D
-    READ_CODE( 6, uiCode, "pps_extension_6bits" ); pcPPS->setPpsExtension6bits( uiCode );
-#else
     READ_FLAG( uiCode, "pps_3d_extension_flag" ); pcPPS->setPps3dExtensionFlag( uiCode == 1 );
     READ_CODE( 5, uiCode, "pps_extension_5bits" ); pcPPS->setPpsExtension5bits( uiCode );
-#endif
     if ( pcPPS->getPpsRangeExtensionsFlag() )
     { 
-      assert(0); 
-    }
+      TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();      
+
+      if (pcPPS->getUseTransformSkip())
+      {
+        READ_UVLC( uiCode, "log2_max_transform_skip_block_size_minus2");
+        ppsRangeExtension.setLog2MaxTransformSkipBlockSize(uiCode+2);
+      }
+
+      READ_FLAG( uiCode, "cross_component_prediction_enabled_flag");
+      ppsRangeExtension.setCrossComponentPredictionEnabledFlag(uiCode != 0);
+
+      READ_FLAG( uiCode, "chroma_qp_offset_list_enabled_flag");
+      if (uiCode == 0)
+      {
+        ppsRangeExtension.clearChromaQpOffsetList();
+        ppsRangeExtension.setDiffCuChromaQpOffsetDepth(0);
+      }
+      else
+      {
+        READ_UVLC(uiCode, "diff_cu_chroma_qp_offset_depth"); ppsRangeExtension.setDiffCuChromaQpOffsetDepth(uiCode);
+        UInt tableSizeMinus1 = 0;
+        READ_UVLC(tableSizeMinus1, "chroma_qp_offset_list_len_minus1");
+        assert(tableSizeMinus1 < MAX_QP_OFFSET_LIST_SIZE);
+
+        for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx <= (tableSizeMinus1); cuChromaQpOffsetIdx++)
+        {
+          Int cbOffset;
+          Int crOffset;
+          READ_SVLC(cbOffset, "cb_qp_offset_list[i]");
+          assert(cbOffset >= -12 && cbOffset <= 12);
+          READ_SVLC(crOffset, "cr_qp_offset_list[i]");
+          assert(crOffset >= -12 && crOffset <= 12);
+          // table uses +1 for index (see comment inside the function)
+          ppsRangeExtension.setChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1, cbOffset, crOffset);
+        }
+        assert(ppsRangeExtension.getChromaQpOffsetListLen() == tableSizeMinus1 + 1);
+      }
+
+      READ_UVLC( uiCode, "log2_sao_offset_scale_luma");
+      ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_LUMA, uiCode);
+      READ_UVLC( uiCode, "log2_sao_offset_scale_chroma");
+      ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA, uiCode);    }
 
     if ( pcPPS->getPpsMultilayerExtensionFlag() )
     { 
-      parsePPSMultilayerExtension( pcPPS ); 
-    }
-#if !H_3D
-    if ( pcPPS->getPpsExtension6bits() )
-    {
-#else
+      parsePpsMultilayerExtension( pcPPS ); 
+    }
+
     if ( pcPPS->getPps3dExtensionFlag() )
     { 
-      parsePPSExtension( pcPPS, pcVPS );
-    }
+#if NH_3D
+      parsePps3dExtension( pcPPS );
+#endif
+    }
+#if NH_3D
     if ( pcPPS->getPpsExtension5bits() )
-    {
-#endif
-
-#endif
-
+#else
+    if ( pcPPS->getPpsExtension5bits() || pcPPS->getPps3dExtensionFlag() )
+#endif
+    {
       while ( xMoreRbspData() )
       {
         READ_FLAG( uiCode, "pps_extension_data_flag");
       }
-#if H_MV
-    }
-#endif
-  }
-}
-
-
-#if H_3D
-Void TDecCavlc::parsePPSExtension( TComPPS* pcPPS, TComVPS* pcVPS )
-{
-  //Ed.(GT): pcVPS should not be used here. Needs to be fixed. 
-  UInt uiCode = 0; 
-  TComDLT* pcDLT = new TComDLT;
-
-  READ_FLAG(uiCode, "dlt_present_flag");
-  pcDLT->setDltPresentFlag( (uiCode == 1) ? true : false );
-
-  if ( pcDLT->getDltPresentFlag() )
-  {
-    READ_CODE(6, uiCode, "pps_depth_layers_minus1");
-    pcDLT->setNumDepthViews( uiCode );
-
-    READ_CODE(4, uiCode, "pps_bit_depth_for_depth_views_minus8");
-    pcDLT->setDepthViewBitDepth( (uiCode+8) );
-
-    for( Int i = 0; i <= pcVPS->getMaxLayersMinus1(); i++ )
-    {
-      if ( i != 0 )
-      {
-        if( pcVPS->getDepthId( i ) == 1 ) 
-        {
-          READ_FLAG(uiCode, "dlt_flag[i]");
-          pcDLT->setUseDLTFlag(i, (uiCode == 1) ? true : false);
-
-          if ( pcDLT->getUseDLTFlag( i ) )
-          {
-            Bool bDltBitMapRepFlag    = false;
-            UInt uiMaxDiff            = 0xffffffff;
-            UInt uiMinDiff            = 0;
-            UInt uiCodeLength         = 0;
-
-            READ_FLAG(uiCode, "inter_view_dlt_pred_enable_flag[ i ]"); 
-
-            if( uiCode )
+    }
+
+#else
+#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
+    static const char *syntaxStrings[]={ "pps_range_extension_flag",
+                                         "pps_multilayer_extension_flag",
+                                         "pps_extension_6bits[0]",
+                                         "pps_extension_6bits[1]",
+                                         "pps_extension_6bits[2]",
+                                         "pps_extension_6bits[3]",
+                                         "pps_extension_6bits[4]",
+                                         "pps_extension_6bits[5]" };
+#endif
+
+    Bool pps_extension_flags[NUM_PPS_EXTENSION_FLAGS];
+    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++)
+    {
+      READ_FLAG( uiCode, syntaxStrings[i] );
+      pps_extension_flags[i] = uiCode!=0;
+    }
+
+    Bool bSkipTrailingExtensionBits=false;
+    for(Int i=0; i<NUM_PPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
+    {
+      if (pps_extension_flags[i])
+      {
+        switch (PPSExtensionFlagIndex(i))
+        {
+          case PPS_EXT__REXT:
             {
-                assert( pcDLT->getUseDLTFlag( 1 ));
+              TComPPSRExt &ppsRangeExtension = pcPPS->getPpsRangeExtension();
+            assert(!bSkipTrailingExtensionBits);
+
+            if (pcPPS->getUseTransformSkip())
+            {
+              READ_UVLC( uiCode, "log2_max_transform_skip_block_size_minus2");
+                ppsRangeExtension.setLog2MaxTransformSkipBlockSize(uiCode+2);
             }
-            pcDLT->setInterViewDltPredEnableFlag( i, (uiCode == 1) ? true : false );
-
-            if ( pcDLT->getInterViewDltPredEnableFlag( i ) == false )
+
+            READ_FLAG( uiCode, "cross_component_prediction_enabled_flag");
+              ppsRangeExtension.setCrossComponentPredictionEnabledFlag(uiCode != 0);
+
+            READ_FLAG( uiCode, "chroma_qp_offset_list_enabled_flag");
+            if (uiCode == 0)
             {
-              READ_FLAG(uiCode, "dlt_bit_map_rep_flag[ layerId ]");
-              bDltBitMapRepFlag = (uiCode == 1) ? true : false; 
+                ppsRangeExtension.clearChromaQpOffsetList();
+                ppsRangeExtension.setDiffCuChromaQpOffsetDepth(0);
             }
             else
             {
-              bDltBitMapRepFlag = false;
+                READ_UVLC(uiCode, "diff_cu_chroma_qp_offset_depth"); ppsRangeExtension.setDiffCuChromaQpOffsetDepth(uiCode);
+              UInt tableSizeMinus1 = 0;
+              READ_UVLC(tableSizeMinus1, "chroma_qp_offset_list_len_minus1");
+              assert(tableSizeMinus1 < MAX_QP_OFFSET_LIST_SIZE);
+
+              for (Int cuChromaQpOffsetIdx = 0; cuChromaQpOffsetIdx <= (tableSizeMinus1); cuChromaQpOffsetIdx++)
+              {
+                Int cbOffset;
+                Int crOffset;
+                READ_SVLC(cbOffset, "cb_qp_offset_list[i]");
+                assert(cbOffset >= -12 && cbOffset <= 12); 
+                READ_SVLC(crOffset, "cr_qp_offset_list[i]");
+                assert(crOffset >= -12 && crOffset <= 12);
+                // table uses +1 for index (see comment inside the function)
+                  ppsRangeExtension.setChromaQpOffsetListEntry(cuChromaQpOffsetIdx+1, cbOffset, crOffset);
+              }
+                assert(ppsRangeExtension.getChromaQpOffsetListLen() == tableSizeMinus1 + 1);
+            }
+
+            READ_UVLC( uiCode, "log2_sao_offset_scale_luma");
+              ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_LUMA, uiCode);
+            READ_UVLC( uiCode, "log2_sao_offset_scale_chroma");
+              ppsRangeExtension.setLog2SaoOffsetScale(CHANNEL_TYPE_CHROMA, uiCode);
+            }
+            break;
+          default:
+            bSkipTrailingExtensionBits=true;
+            break;
+        }
+      }
+    }
+    if (bSkipTrailingExtensionBits)
+    {
+      while ( xMoreRbspData() )
+      {
+        READ_FLAG( uiCode, "pps_extension_data_flag");
+      }
+    }
+#endif
+  }
+  xReadRbspTrailingBits();
+}
+
+#if NH_3D
+Void TDecCavlc::parsePps3dExtension( TComPPS* pcPPS )
+{
+#if NH_3D_DLT
+  UInt uiCode = 0; 
+  //
+  TComDLT* pcDLT = pcPPS->getDLT();
+
+  READ_FLAG(uiCode, "dlts_present_flag");
+  pcDLT->setDltPresentFlag( (uiCode == 1) ? true : false );
+
+  if ( pcDLT->getDltPresentFlag() )
+  {
+    READ_CODE(6, uiCode, "pps_depth_layers_minus1");
+#if NH_3D_VER141_DEC_COMP_FLAG
+    pcDLT->setNumDepthViews( uiCode );
+#else
+    pcDLT->setNumDepthViews( uiCode+1 );
+#endif
+    
+    READ_CODE(4, uiCode, "pps_bit_depth_for_depth_layers_minus8");
+    pcDLT->setDepthViewBitDepth( (uiCode+8) );
+    
+#if NH_3D_DLT_FIX
+    for( Int i = 0; i <= pcDLT->getNumDepthViews()-1; i++ )
+#else
+    for( Int i = 0; i <= pcDLT->getNumDepthViews(); i++ )
+#endif
+    {
+      Int layerId = pcDLT->getDepthIdxToLayerId(i);
+      
+      READ_FLAG(uiCode, "dlt_flag[i]");
+      pcDLT->setUseDLTFlag(layerId, (uiCode == 1) ? true : false);
+      
+      if ( pcDLT->getUseDLTFlag( layerId ) )
+      {
+        Bool bDltBitMapRepFlag    = false;
+        UInt uiMaxDiff            = MAX_INT;
+        UInt uiMinDiff            = 0;
+        UInt uiCodeLength         = 0;
+        
+        READ_FLAG(uiCode, "dlt_pred_flag[i]");
+        
+        if( uiCode )
+        {
+          assert( pcDLT->getUseDLTFlag( 1 ));
+        }
+        pcDLT->setInterViewDltPredEnableFlag( layerId, (uiCode == 1) ? true : false );
+        
+        if ( pcDLT->getInterViewDltPredEnableFlag( layerId ) == false )
+        {
+          READ_FLAG(uiCode, "dlt_val_flags_present_flag[i]");
+          bDltBitMapRepFlag = (uiCode == 1) ? true : false;
+        }
+        else
+        {
+          bDltBitMapRepFlag = false;
+        }
+        
+        UInt uiNumDepthValues = 0;
+        std::vector<Int> aiIdx2DepthValue(256, 0);
+        
+        // Bit map
+        if ( bDltBitMapRepFlag )
+        {
+          for (UInt d=0; d<256; d++)
+          {
+            READ_FLAG(uiCode, "dlt_value_flag[i][j]");
+            if (uiCode == 1)
+            {
+              aiIdx2DepthValue[uiNumDepthValues] = d;
+              uiNumDepthValues++;
+            }
+          }
+        }
+        // Diff Coding
+        else
+        {
+          READ_CODE(8, uiNumDepthValues, "num_val_delta_dlt");   // num_entry
+          
+          {
+            // The condition if( pcVPS->getNumDepthValues(i) > 0 ) is always true since for Single-view Diff Coding, there is at least one depth value in depth component.
+            
+            if (uiNumDepthValues > 1)
+            {
+              READ_CODE(8, uiCode, "max_diff");
+              uiMaxDiff = uiCode;
+            }
+            else
+            {
+              uiMaxDiff = 0;           // when there is only one value in DLT
             }
             
-            UInt uiNumDepthValues = 0;
-            Int  aiIdx2DepthValue[256];
-
-            // Bit map
-            if ( bDltBitMapRepFlag )
+            if (uiNumDepthValues > 2)
             {
-              for (UInt d=0; d<256; d++)
+              uiCodeLength = (UInt) gCeilLog2(uiMaxDiff + 1);
+              READ_CODE(uiCodeLength, uiCode, "min_diff_minus1");
+              uiMinDiff = uiCode + 1;
+            }
+            else
+            {
+              uiMinDiff = uiMaxDiff;   // when there are only one or two values in DLT
+            }
+            
+            READ_CODE(8, uiCode, "delta_dlt_val0");   // entry0
+            aiIdx2DepthValue[0] = uiCode;
+            
+            if (uiMaxDiff == uiMinDiff)
+            {
+              for (UInt d=1; d<uiNumDepthValues; d++)
               {
-                READ_FLAG(uiCode, "dlt_bit_map_flag[ layerId ][ j ]");
-                if (uiCode == 1)
-                {
-                  aiIdx2DepthValue[uiNumDepthValues] = d;
-                  uiNumDepthValues++;
-                }
+                aiIdx2DepthValue[d] = aiIdx2DepthValue[d-1] + uiMinDiff + 0;
               }
             }
-            // Diff Coding
             else
             {
-              READ_CODE(8, uiNumDepthValues, "num_depth_values_in_dlt[i]");   // num_entry
-
+              uiCodeLength = (UInt) gCeilLog2(uiMaxDiff - uiMinDiff + 1);
+              for (UInt d=1; d<uiNumDepthValues; d++)
               {
-                // The condition if( pcVPS->getNumDepthValues(i) > 0 ) is always true since for Single-view Diff Coding, there is at least one depth value in depth component. 
-
-                if (uiNumDepthValues > 1)
-                {
-                  READ_CODE(8, uiCode, "max_diff[ layerId ]"); 
-                  uiMaxDiff = uiCode;
-                }
-                else
-                {
-                  uiMaxDiff = 0;           // when there is only one value in DLT
-                }
-
-                if (uiNumDepthValues > 2)
-                {
-                  uiCodeLength = (UInt) ceil(Log2(uiMaxDiff + 1));
-                  READ_CODE(uiCodeLength, uiCode, "min_diff_minus1[ layerId ]");
-                  uiMinDiff = uiCode + 1;
-                }
-                else
-                {
-                  uiMinDiff = uiMaxDiff;   // when there are only one or two values in DLT
-                }
-
-                READ_CODE(8, uiCode, "dlt_depth_value0[layerId]");   // entry0
-                aiIdx2DepthValue[0] = uiCode;
-
-                if (uiMaxDiff == uiMinDiff)
-                {
-                  for (UInt d=1; d<uiNumDepthValues; d++)
-                  {
-                    aiIdx2DepthValue[d] = aiIdx2DepthValue[d-1] + uiMinDiff + 0;
-                  }
-                }
-                else
-                {
-                  uiCodeLength = (UInt) ceil(Log2(uiMaxDiff - uiMinDiff + 1));
-                  for (UInt d=1; d<uiNumDepthValues; d++)
-                  {
-                    READ_CODE(uiCodeLength, uiCode, "dlt_depth_value_diff_minus_min[ layerId ][ j ]");
-                    aiIdx2DepthValue[d] = aiIdx2DepthValue[d-1] + uiMinDiff + uiCode;
-                  }
-                }
-
+                READ_CODE(uiCodeLength, uiCode, "delta_val_diff_minus_min[k]");
+                aiIdx2DepthValue[d] = aiIdx2DepthValue[d-1] + uiMinDiff + uiCode;
               }
             }
             
-            if( pcDLT->getInterViewDltPredEnableFlag( i ) )
-            {
-              // interpret decoded values as delta DLT
-              AOF( pcVPS->getDepthId( 1 ) == 1 );
-              AOF( i > 1 );
-              // assumes ref layer id to be 1
-              Int* piRefDLT = pcDLT->idx2DepthValue( 1 );
-              UInt uiRefNum = pcDLT->getNumDepthValues( 1 );
-              pcDLT->setDeltaDLT(i, piRefDLT, uiRefNum, aiIdx2DepthValue, uiNumDepthValues);
-            }
-            else
-            {
-              // store final DLT
-              pcDLT->setDepthLUTs(i, aiIdx2DepthValue, uiNumDepthValues);
-            }
-
           }
         }
-      }
-    }
-  }
-
-  pcPPS->setDLT( pcDLT );
-}
-#endif
+        
+        if( pcDLT->getInterViewDltPredEnableFlag( layerId ) )
+        {
+          // interpret decoded values as delta DLT
+          AOF( layerId > 1 );
+          // assumes ref layer id to be 1
+          std::vector<Int> viRefDLT = pcDLT->idx2DepthValue( 1 );
+          UInt uiRefNum = pcDLT->getNumDepthValues( 1 );
+          pcDLT->setDeltaDLT(layerId, viRefDLT, uiRefNum, aiIdx2DepthValue, uiNumDepthValues);
+        }
+        else
+        {
+          // store final DLT
+          pcDLT->setDepthLUTs(layerId, aiIdx2DepthValue, uiNumDepthValues);
+        }
+      }
+    }
+  }
+#endif
+}
+#endif
+
 
 Void  TDecCavlc::parseVUI(TComVUI* pcVUI, TComSPS *pcSPS)
@@ -541,5 +656,5 @@
     {
       READ_CODE(16, uiCode, "sar_width");                             pcVUI->setSarWidth(uiCode);
-      READ_CODE(16, uiCode, "sar_height");                            pcVUI->setSarHeight(uiCode);
+      READ_CODE(16, uiCode, "sar_height");                              pcVUI->setSarHeight(uiCode);
     }
   }
@@ -552,5 +667,5 @@
 
   READ_FLAG(     uiCode, "video_signal_type_present_flag");           pcVUI->setVideoSignalTypePresentFlag(uiCode);
-#if H_MV
+#if NH_MV
   assert( pcSPS->getLayerId() == 0 || !pcVUI->getVideoSignalTypePresentFlag() ); 
 #endif
@@ -564,5 +679,5 @@
       READ_CODE(8, uiCode, "colour_primaries");                       pcVUI->setColourPrimaries(uiCode);
       READ_CODE(8, uiCode, "transfer_characteristics");               pcVUI->setTransferCharacteristics(uiCode);
-      READ_CODE(8, uiCode, "matrix_coefficients");                    pcVUI->setMatrixCoefficients(uiCode);
+      READ_CODE(8, uiCode, "matrix_coeffs");                          pcVUI->setMatrixCoefficients(uiCode);
     }
   }
@@ -585,5 +700,5 @@
   {
     Window &defDisp = pcVUI->getDefaultDisplayWindow();
-#if H_MV
+#if NH_MV
     defDisp.setScaledFlag( false ); 
     READ_UVLC(   uiCode, "def_disp_win_left_offset" );                defDisp.setWindowLeftOffset  ( uiCode );
@@ -598,4 +713,5 @@
 #endif
   }
+
   TimingInfo *timingInfo = pcVUI->getTimingInfo();
   READ_FLAG(       uiCode, "vui_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
@@ -609,10 +725,12 @@
       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, "vui_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);
   if (pcVUI->getBitstreamRestrictionFlag())
@@ -621,8 +739,8 @@
     READ_FLAG(   uiCode, "motion_vectors_over_pic_boundaries_flag");  pcVUI->setMotionVectorsOverPicBoundariesFlag(uiCode);
     READ_FLAG(   uiCode, "restricted_ref_pic_lists_flag");            pcVUI->setRestrictedRefPicListsFlag(uiCode);
-    READ_UVLC( uiCode, "min_spatial_segmentation_idc");            pcVUI->setMinSpatialSegmentationIdc(uiCode);
+    READ_UVLC(   uiCode, "min_spatial_segmentation_idc");             pcVUI->setMinSpatialSegmentationIdc(uiCode);
     assert(uiCode < 4096);
     READ_UVLC(   uiCode, "max_bytes_per_pic_denom" );                 pcVUI->setMaxBytesPerPicDenom(uiCode);
-    READ_UVLC(   uiCode, "max_bits_per_mincu_denom" );                pcVUI->setMaxBitsPerMinCuDenom(uiCode);
+    READ_UVLC(   uiCode, "max_bits_per_min_cu_denom" );               pcVUI->setMaxBitsPerMinCuDenom(uiCode);
     READ_UVLC(   uiCode, "log2_max_mv_length_horizontal" );           pcVUI->setLog2MaxMvLengthHorizontal(uiCode);
     READ_UVLC(   uiCode, "log2_max_mv_length_vertical" );             pcVUI->setLog2MaxMvLengthVertical(uiCode);
@@ -639,9 +757,9 @@
     if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
     {
-      READ_FLAG( uiCode, "sub_pic_cpb_params_present_flag" );         hrd->setSubPicCpbParamsPresentFlag( uiCode == 1 ? true : false );
+      READ_FLAG( uiCode, "sub_pic_hrd_params_present_flag" );         hrd->setSubPicCpbParamsPresentFlag( uiCode == 1 ? true : false );
       if( hrd->getSubPicCpbParamsPresentFlag() )
       {
         READ_CODE( 8, uiCode, "tick_divisor_minus2" );                hrd->setTickDivisorMinus2( uiCode );
-        READ_CODE( 5, uiCode, "du_cpb_removal_delay_length_minus1" ); hrd->setDuCpbRemovalDelayLengthMinus1( uiCode );
+        READ_CODE( 5, uiCode, "du_cpb_removal_delay_increment_length_minus1" ); hrd->setDuCpbRemovalDelayLengthMinus1( uiCode );
         READ_FLAG( uiCode, "sub_pic_cpb_params_in_pic_timing_sei_flag" ); hrd->setSubPicCpbParamsInPicTimingSEIFlag( uiCode == 1 ? true : false );
         READ_CODE( 5, uiCode, "dpb_output_delay_du_length_minus1"  ); hrd->setDpbOutputDelayDuLengthMinus1( uiCode );
@@ -670,6 +788,8 @@
       hrd->setFixedPicRateWithinCvsFlag( i, true );
     }
+
     hrd->setLowDelayHrdFlag( i, 0 ); // Infered to be 0 when not present
     hrd->setCpbCntMinus1   ( i, 0 ); // Infered to be 0 when not present
+
     if( hrd->getFixedPicRateWithinCvsFlag( i ) )
     {
@@ -677,11 +797,12 @@
     }
     else
-    {      
+    {
       READ_FLAG( uiCode, "low_delay_hrd_flag" );                      hrd->setLowDelayHrdFlag( i, uiCode == 1 ? true : false  );
     }
     if (!hrd->getLowDelayHrdFlag( i ))
     {
-      READ_UVLC( uiCode, "cpb_cnt_minus1" );                          hrd->setCpbCntMinus1( i, uiCode );      
-    }
+      READ_UVLC( uiCode, "cpb_cnt_minus1" );                          hrd->setCpbCntMinus1( i, uiCode );
+    }
+
     for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
     {
@@ -707,6 +828,10 @@
 Void TDecCavlc::parseSPS(TComSPS* pcSPS)
 {
-#if ENC_DEC_TRACE  
-  xTraceSPSHeader (pcSPS);
+#if ENC_DEC_TRACE
+#if H_MV_ENC_DEC_TRAC
+  tracePSHeader( "SPS", pcSPS->getLayerId() ); 
+#else
+  xTraceSPSHeader ();
+#endif
 #endif
 
@@ -714,11 +839,11 @@
   READ_CODE( 4,  uiCode, "sps_video_parameter_set_id");          pcSPS->setVPSId        ( uiCode );
 
-#if H_MV
+#if NH_MV
   if ( pcSPS->getLayerId() == 0 )
   {
 #endif
-    READ_CODE( 3,  uiCode, "sps_max_sub_layers_minus1" );          pcSPS->setMaxTLayers   ( uiCode+1 );
-    assert(uiCode <= 6);
-#if H_MV
+  READ_CODE( 3,  uiCode, "sps_max_sub_layers_minus1" );          pcSPS->setMaxTLayers   ( uiCode+1 );
+  assert(uiCode <= 6);
+#if NH_MV
   }
   else
@@ -731,13 +856,13 @@
 #endif
 
-    READ_FLAG( uiCode, "sps_temporal_id_nesting_flag" );               pcSPS->setTemporalIdNestingFlag ( uiCode > 0 ? true : false );
-    if ( pcSPS->getMaxTLayers() == 1 )
-    {
-      // sps_temporal_id_nesting_flag must be 1 when sps_max_sub_layers_minus1 is 0
-      assert( uiCode == 1 );
-    }
-
-    parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
-#if H_MV
+  READ_FLAG( uiCode, "sps_temporal_id_nesting_flag" );           pcSPS->setTemporalIdNestingFlag ( uiCode > 0 ? true : false );
+  if ( pcSPS->getMaxTLayers() == 1 )
+  {
+    // sps_temporal_id_nesting_flag must be 1 when sps_max_sub_layers_minus1 is 0
+    assert( uiCode == 1 );
+  }
+
+  parsePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
+#if NH_MV
     pcSPS->getPTL()->inferGeneralValues ( true, 0, NULL ); 
     pcSPS->getPTL()->inferSubLayerValues( pcSPS->getMaxTLayers() - 1, 0, NULL );
@@ -746,5 +871,5 @@
   READ_UVLC(     uiCode, "sps_seq_parameter_set_id" );           pcSPS->setSPSId( uiCode );
   assert(uiCode <= 15);
-#if H_MV
+#if NH_MV
   if ( pcSPS->getMultiLayerExtSpsFlag() )
   {
@@ -758,22 +883,19 @@
   {
 #endif
-    READ_UVLC(     uiCode, "chroma_format_idc" );                  pcSPS->setChromaFormatIdc( uiCode );
-    assert(uiCode <= 3);
-    // in the first version we only support chroma_format_idc equal to 1 (4:2:0), so separate_colour_plane_flag cannot appear in the bitstream
-#if !H_3D_DISABLE_CHROMA
-    assert (uiCode == 1);
-#endif
-    if( uiCode == 3 )
-    {
-      READ_FLAG(     uiCode, "separate_colour_plane_flag");        assert(uiCode == 0);
-    }
-
-    READ_UVLC (    uiCode, "pic_width_in_luma_samples" );          pcSPS->setPicWidthInLumaSamples ( uiCode    );
-    READ_UVLC (    uiCode, "pic_height_in_luma_samples" );         pcSPS->setPicHeightInLumaSamples( uiCode    );
+
+  READ_UVLC(     uiCode, "chroma_format_idc" );                  pcSPS->setChromaFormatIdc( ChromaFormat(uiCode) );
+  assert(uiCode <= 3);
+  if( pcSPS->getChromaFormatIdc() == CHROMA_444 )
+  {
+    READ_FLAG(     uiCode, "separate_colour_plane_flag");        assert(uiCode == 0);
+  }
+
+  READ_UVLC (    uiCode, "pic_width_in_luma_samples" );          pcSPS->setPicWidthInLumaSamples ( uiCode    );
+  READ_UVLC (    uiCode, "pic_height_in_luma_samples" );         pcSPS->setPicHeightInLumaSamples( uiCode    );
   READ_FLAG(     uiCode, "conformance_window_flag");
   if (uiCode != 0)
   {
     Window &conf = pcSPS->getConformanceWindow();
-#if H_MV
+#if NH_MV
     // Needs to be scaled later, when ChromaFormatIdc is known. 
     conf.setScaledFlag( false ); 
@@ -791,18 +913,43 @@
   }
 
-#if H_MV
+#if NH_MV
   if ( !pcSPS->getMultiLayerExtSpsFlag() )
   { 
 #endif
-    READ_UVLC(     uiCode, "bit_depth_luma_minus8" );
-    assert(uiCode <= 6);
-    pcSPS->setBitDepthY( uiCode + 8 );
-    pcSPS->setQpBDOffsetY( (Int) (6*uiCode) );
-
-    READ_UVLC( uiCode,    "bit_depth_chroma_minus8" );
-    assert(uiCode <= 6);
-    pcSPS->setBitDepthC( uiCode + 8 );
-    pcSPS->setQpBDOffsetC( (Int) (6*uiCode) );
-#if H_MV
+
+  READ_UVLC(     uiCode, "bit_depth_luma_minus8" );
+#if O0043_BEST_EFFORT_DECODING
+  pcSPS->setStreamBitDepth(CHANNEL_TYPE_LUMA, 8 + uiCode);
+  const UInt forceDecodeBitDepth = pcSPS->getForceDecodeBitDepth();
+  if (forceDecodeBitDepth != 0)
+  {
+    uiCode = forceDecodeBitDepth - 8;
+  }
+#endif
+  assert(uiCode <= 8);
+  pcSPS->setBitDepth(CHANNEL_TYPE_LUMA, 8 + uiCode);
+
+#if O0043_BEST_EFFORT_DECODING
+  pcSPS->setQpBDOffset(CHANNEL_TYPE_LUMA, (Int) (6*(pcSPS->getStreamBitDepth(CHANNEL_TYPE_LUMA)-8)) );
+#else
+  pcSPS->setQpBDOffset(CHANNEL_TYPE_LUMA, (Int) (6*uiCode) );
+#endif
+
+  READ_UVLC( uiCode,    "bit_depth_chroma_minus8" );
+#if O0043_BEST_EFFORT_DECODING
+  pcSPS->setStreamBitDepth(CHANNEL_TYPE_CHROMA, 8 + uiCode);
+  if (forceDecodeBitDepth != 0)
+  {
+    uiCode = forceDecodeBitDepth - 8;
+  }
+#endif
+  assert(uiCode <= 8);
+  pcSPS->setBitDepth(CHANNEL_TYPE_CHROMA, 8 + uiCode);
+#if O0043_BEST_EFFORT_DECODING
+  pcSPS->setQpBDOffset(CHANNEL_TYPE_CHROMA,  (Int) (6*(pcSPS->getStreamBitDepth(CHANNEL_TYPE_CHROMA)-8)) );
+#else
+  pcSPS->setQpBDOffset(CHANNEL_TYPE_CHROMA,  (Int) (6*uiCode) );
+#endif
+#if NH_MV
   }
 #endif
@@ -811,52 +958,52 @@
   assert(uiCode <= 12);
 
-#if H_MV
+#if NH_MV
   if ( !pcSPS->getMultiLayerExtSpsFlag()) 
   {  
 #endif
-    UInt subLayerOrderingInfoPresentFlag;
-    READ_FLAG(subLayerOrderingInfoPresentFlag, "sps_sub_layer_ordering_info_present_flag");
-
-    for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
-    {
-      READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1[i]");
-      pcSPS->setMaxDecPicBuffering( uiCode + 1, i);
-      READ_UVLC ( uiCode, "sps_num_reorder_pics[i]" );
-      pcSPS->setNumReorderPics(uiCode, i);
-      READ_UVLC ( uiCode, "sps_max_latency_increase_plus1[i]");
-      pcSPS->setMaxLatencyIncrease( uiCode, i );
-
-      if (!subLayerOrderingInfoPresentFlag)
-      {
-        for (i++; i <= pcSPS->getMaxTLayers()-1; i++)
-        {
-          pcSPS->setMaxDecPicBuffering(pcSPS->getMaxDecPicBuffering(0), i);
-          pcSPS->setNumReorderPics(pcSPS->getNumReorderPics(0), i);
-          pcSPS->setMaxLatencyIncrease(pcSPS->getMaxLatencyIncrease(0), i);
-        }
-        break;
-      }
-    }
-#if H_MV
-  }
-#endif
-
-  READ_UVLC( uiCode, "log2_min_coding_block_size_minus3" );
+  UInt subLayerOrderingInfoPresentFlag;
+  READ_FLAG(subLayerOrderingInfoPresentFlag, "sps_sub_layer_ordering_info_present_flag");
+
+  for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
+  {
+    READ_UVLC ( uiCode, "sps_max_dec_pic_buffering_minus1[i]");
+    pcSPS->setMaxDecPicBuffering( uiCode + 1, i);
+    READ_UVLC ( uiCode, "sps_max_num_reorder_pics[i]" );
+    pcSPS->setNumReorderPics(uiCode, i);
+    READ_UVLC ( uiCode, "sps_max_latency_increase_plus1[i]");
+    pcSPS->setMaxLatencyIncrease( uiCode, i );
+
+    if (!subLayerOrderingInfoPresentFlag)
+    {
+      for (i++; i <= pcSPS->getMaxTLayers()-1; i++)
+      {
+        pcSPS->setMaxDecPicBuffering(pcSPS->getMaxDecPicBuffering(0), i);
+        pcSPS->setNumReorderPics(pcSPS->getNumReorderPics(0), i);
+        pcSPS->setMaxLatencyIncrease(pcSPS->getMaxLatencyIncrease(0), i);
+      }
+      break;
+    }
+  }
+#if NH_MV
+  }
+#endif
+
+  READ_UVLC( uiCode, "log2_min_luma_coding_block_size_minus3" );
   Int log2MinCUSize = uiCode + 3;
   pcSPS->setLog2MinCodingBlockSize(log2MinCUSize);
-  READ_UVLC( uiCode, "log2_diff_max_min_coding_block_size" );
+  READ_UVLC( uiCode, "log2_diff_max_min_luma_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) ); 
+  pcSPS->setMaxCUWidth  ( 1<<(log2MinCUSize + maxCUDepthDelta) );
   pcSPS->setMaxCUHeight ( 1<<(log2MinCUSize + maxCUDepthDelta) );
-  READ_UVLC( uiCode, "log2_min_transform_block_size_minus2" );   pcSPS->setQuadtreeTULog2MinSize( uiCode + 2 );
-
-  READ_UVLC( uiCode, "log2_diff_max_min_transform_block_size" ); pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
+  READ_UVLC( uiCode, "log2_min_luma_transform_block_size_minus2" );   pcSPS->setQuadtreeTULog2MinSize( uiCode + 2 );
+
+  READ_UVLC( uiCode, "log2_diff_max_min_luma_transform_block_size" ); pcSPS->setQuadtreeTULog2MaxSize( uiCode + pcSPS->getQuadtreeTULog2MinSize() );
   pcSPS->setMaxTrSize( 1<<(uiCode + pcSPS->getQuadtreeTULog2MinSize()) );
 
@@ -865,10 +1012,10 @@
 
   Int addCuDepth = max (0, log2MinCUSize - (Int)pcSPS->getQuadtreeTULog2MinSize() );
-  pcSPS->setMaxCUDepth( maxCUDepthDelta + addCuDepth ); 
+  pcSPS->setMaxTotalCUDepth( maxCUDepthDelta + addCuDepth  + getMaxCUDepthOffset(pcSPS->getChromaFormatIdc(), pcSPS->getQuadtreeTULog2MinSize()) );
 
   READ_FLAG( uiCode, "scaling_list_enabled_flag" );                 pcSPS->setScalingListFlag ( uiCode );
   if(pcSPS->getScalingListFlag())
   {
-#if H_MV
+#if NH_MV
     if ( pcSPS->getMultiLayerExtSpsFlag() )
     {    
@@ -883,10 +1030,10 @@
     {    
 #endif
-      READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
-      if(pcSPS->getScalingListPresentFlag ())
-      {
-        parseScalingList( pcSPS->getScalingList() );
-      }
-#if H_MV
+    READ_FLAG( uiCode, "sps_scaling_list_data_present_flag" );                 pcSPS->setScalingListPresentFlag ( uiCode );
+    if(pcSPS->getScalingListPresentFlag ())
+    {
+      parseScalingList( &(pcSPS->getScalingList()) );
+    }
+#if NH_MV
     }
 #endif
@@ -898,6 +1045,6 @@
   if( pcSPS->getUsePCM() )
   {
-    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_luma_minus1" );          pcSPS->setPCMBitDepthLuma   ( 1 + uiCode );
-    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1" );        pcSPS->setPCMBitDepthChroma ( 1 + uiCode );
+    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_luma_minus1" );          pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_LUMA, 1 + uiCode );
+    READ_CODE( 4, uiCode, "pcm_sample_bit_depth_chroma_minus1" );        pcSPS->setPCMBitDepth    ( CHANNEL_TYPE_CHROMA, 1 + uiCode );
     READ_UVLC( uiCode, "log2_min_pcm_luma_coding_block_size_minus3" );   pcSPS->setPCMLog2MinSize (uiCode+3);
     READ_UVLC( uiCode, "log2_diff_max_min_pcm_luma_coding_block_size" ); pcSPS->setPCMLog2MaxSize ( uiCode+pcSPS->getPCMLog2MinSize() );
@@ -918,7 +1065,7 @@
   }
   READ_FLAG( uiCode, "long_term_ref_pics_present_flag" );          pcSPS->setLongTermRefsPresent(uiCode);
-  if (pcSPS->getLongTermRefsPresent()) 
-  {
-    READ_UVLC( uiCode, "num_long_term_ref_pic_sps" );
+  if (pcSPS->getLongTermRefsPresent())
+  {
+    READ_UVLC( uiCode, "num_long_term_ref_pics_sps" );
     pcSPS->setNumLongTermRefPicSPS(uiCode);
     for (UInt k = 0; k < pcSPS->getNumLongTermRefPicSPS(); k++)
@@ -941,96 +1088,154 @@
   }
 
-
-#if H_MV
   READ_FLAG( uiCode, "sps_extension_present_flag");
+#if NH_MV
   pcSPS->setSpsExtensionPresentFlag( uiCode ); 
   if (pcSPS->getSpsExtensionPresentFlag( ) )
+  {  
+    READ_FLAG( uiCode, "sps_range_extensions_flag" );     pcSPS->setSpsRangeExtensionsFlag( uiCode == 1 );
+    READ_FLAG( uiCode, "sps_multilayer_extension_flag" ); pcSPS->setSpsMultilayerExtensionFlag( uiCode == 1 );
+    READ_FLAG( uiCode   , "sps_3d_extension_flag" );      pcSPS->setSps3dExtensionFlag( uiCode == 1 );
+    READ_CODE( 5, uiCode, "sps_extension_5bits" )  ;      pcSPS->setSpsExtension5bits( uiCode );
+
+    if ( pcSPS->getSpsRangeExtensionsFlag() )
+    {
+              TComSPSRExt &spsRangeExtension = pcSPS->getSpsRangeExtension();
+              READ_FLAG( uiCode, "transform_skip_rotation_enabled_flag");     spsRangeExtension.setTransformSkipRotationEnabledFlag(uiCode != 0);
+              READ_FLAG( uiCode, "transform_skip_context_enabled_flag");      spsRangeExtension.setTransformSkipContextEnabledFlag (uiCode != 0);
+              READ_FLAG( uiCode, "implicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT, (uiCode != 0));
+              READ_FLAG( uiCode, "explicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT, (uiCode != 0));
+              READ_FLAG( uiCode, "extended_precision_processing_flag");       spsRangeExtension.setExtendedPrecisionProcessingFlag (uiCode != 0);
+              READ_FLAG( uiCode, "intra_smoothing_disabled_flag");            spsRangeExtension.setIntraSmoothingDisabledFlag      (uiCode != 0);
+              READ_FLAG( uiCode, "high_precision_offsets_enabled_flag");      spsRangeExtension.setHighPrecisionOffsetsEnabledFlag (uiCode != 0);
+              READ_FLAG( uiCode, "persistent_rice_adaptation_enabled_flag");  spsRangeExtension.setPersistentRiceAdaptationEnabledFlag (uiCode != 0);
+              READ_FLAG( uiCode, "cabac_bypass_alignment_enabled_flag");      spsRangeExtension.setCabacBypassAlignmentEnabledFlag  (uiCode != 0);
+    }
+
+    if ( pcSPS->getSpsMultilayerExtensionFlag() )
+    {
+      parseSpsMultilayerExtension( pcSPS ); 
+    }
+
+    if ( pcSPS->getSps3dExtensionFlag() )
+    {
+#if NH_3D
+      parseSps3dExtension( pcSPS ); 
+#endif
+    }
+
+#if NH_3D
+    if ( pcSPS->getSpsExtension5bits() )
 #else
-  READ_FLAG( uiCode, "sps_extension_flag");
+    if ( pcSPS->getSpsExtension5bits() || pcSPS->getSps3dExtensionFlag() )
+#endif
+    { 
+      while ( xMoreRbspData() )
+      {
+        READ_FLAG( uiCode, "sps_extension_data_flag");
+      }
+    }
+  }
+#else
   if (uiCode)
-#endif
-  {
-#if H_MV
-    READ_FLAG( uiCode, "sps_range_extensions_flag" ); pcSPS->setSpsRangeExtensionsFlag( uiCode == 1 );
-    READ_FLAG( uiCode, "sps_multilayer_extension_flag" ); pcSPS->setSpsMultilayerExtensionFlag( uiCode == 1 );
-#if !H_3D
-    READ_CODE( 6, uiCode, "sps_extension_6bits" ); pcSPS->setSpsExtension6bits( uiCode );
-#else
-    READ_FLAG( uiCode, "sps_3d_extension_flag" ); pcSPS->setSps3dExtensionFlag( uiCode == 1 );
-    READ_CODE( 5, uiCode, "sps_extension_5bits" ); pcSPS->setSpsExtension5bits( uiCode );
-#endif
-  }
-
-  if ( pcSPS->getSpsRangeExtensionsFlag() )
-  {
-    assert( 0 ); 
-  }
-
-  if ( pcSPS->getSpsMultilayerExtensionFlag() )
-  {
-    parseSPSExtension( pcSPS ); 
-  }
-
-#if H_3D
-  if ( pcSPS->getSps3dExtensionFlag() )
-  {
-    parseSPS3dExtension( pcSPS ); 
-  }
-
-  if ( pcSPS->getSpsExtension5bits() )
-  { 
-#else
-  if ( pcSPS->getSpsExtension6bits() )
-  { 
-#endif
-
-#endif
-    while ( xMoreRbspData() )
-    {
-      READ_FLAG( uiCode, "sps_extension_data_flag");
-    }
-  }
-}
-
-#if H_MV
-Void TDecCavlc::parseSPSExtension( TComSPS* pcSPS )
+  {
+
+#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
+    static const char *syntaxStrings[]={ "sps_range_extension_flag",
+                                         "sps_multilayer_extension_flag",
+                                         "sps_extension_6bits[0]",
+                                         "sps_extension_6bits[1]",
+                                         "sps_extension_6bits[2]",
+                                         "sps_extension_6bits[3]",
+                                         "sps_extension_6bits[4]",
+                                         "sps_extension_6bits[5]" };
+#endif
+    Bool sps_extension_flags[NUM_SPS_EXTENSION_FLAGS];
+
+    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++)
+    {
+      READ_FLAG( uiCode, syntaxStrings[i] );
+      sps_extension_flags[i] = uiCode!=0;
+    }
+
+    Bool bSkipTrailingExtensionBits=false;
+    for(Int i=0; i<NUM_SPS_EXTENSION_FLAGS; i++) // loop used so that the order is determined by the enum.
+    {
+      if (sps_extension_flags[i])
+      {
+        switch (SPSExtensionFlagIndex(i))
+        {
+          case SPS_EXT__REXT:
+            assert(!bSkipTrailingExtensionBits);
+            {
+              TComSPSRExt &spsRangeExtension = pcSPS->getSpsRangeExtension();
+              READ_FLAG( uiCode, "transform_skip_rotation_enabled_flag");     spsRangeExtension.setTransformSkipRotationEnabledFlag(uiCode != 0);
+              READ_FLAG( uiCode, "transform_skip_context_enabled_flag");      spsRangeExtension.setTransformSkipContextEnabledFlag (uiCode != 0);
+              READ_FLAG( uiCode, "implicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_IMPLICIT, (uiCode != 0));
+              READ_FLAG( uiCode, "explicit_rdpcm_enabled_flag");              spsRangeExtension.setRdpcmEnabledFlag(RDPCM_SIGNAL_EXPLICIT, (uiCode != 0));
+              READ_FLAG( uiCode, "extended_precision_processing_flag");       spsRangeExtension.setExtendedPrecisionProcessingFlag (uiCode != 0);
+              READ_FLAG( uiCode, "intra_smoothing_disabled_flag");            spsRangeExtension.setIntraSmoothingDisabledFlag      (uiCode != 0);
+              READ_FLAG( uiCode, "high_precision_offsets_enabled_flag");      spsRangeExtension.setHighPrecisionOffsetsEnabledFlag (uiCode != 0);
+              READ_FLAG( uiCode, "persistent_rice_adaptation_enabled_flag");  spsRangeExtension.setPersistentRiceAdaptationEnabledFlag (uiCode != 0);
+              READ_FLAG( uiCode, "cabac_bypass_alignment_enabled_flag");      spsRangeExtension.setCabacBypassAlignmentEnabledFlag  (uiCode != 0);
+            }
+            break;
+          default:
+            bSkipTrailingExtensionBits=true;
+            break;
+        }
+      }
+    }
+    if (bSkipTrailingExtensionBits)
+    {
+      while ( xMoreRbspData() )
+      {
+        READ_FLAG( uiCode, "sps_extension_data_flag");
+      }
+    }
+  }
+#endif
+  xReadRbspTrailingBits();
+}
+
+#if NH_MV
+Void TDecCavlc::parseSpsMultilayerExtension( TComSPS* pcSPS )
 {
   UInt uiCode; 
-  READ_FLAG( uiCode, "inter_view_mv_vert_constraint_flag" );    pcSPS->setInterViewMvVertConstraintFlag(uiCode == 1 ? true : false);
-  
-}
-
-#if H_3D
-Void TDecCavlc::parseSPS3dExtension( TComSPS* pcSPS )
+  READ_FLAG( uiCode, "inter_view_mv_vert_constraint_flag" );    pcSPS->setInterViewMvVertConstraintFlag(uiCode == 1 ? true : false);  
+}
+
+#if NH_3D
+Void TDecCavlc::parseSps3dExtension( TComSPS* pcSPS )
 { 
-  TComSps3dExtension* sps3dExt = pcSPS->getSps3dExtension(); 
+  TComSps3dExtension sps3dExt; 
   UInt uiCode; 
   for( Int d = 0; d  <=  1; d++ )
   {
-    READ_FLAG( uiCode, "iv_mv_pred_flag" ); sps3dExt->setIvMvPredFlag( d, uiCode == 1 );
-    READ_FLAG( uiCode, "iv_mv_scaling_flag" ); sps3dExt->setIvMvScalingFlag( d, uiCode == 1 );
+    READ_FLAG( uiCode, "iv_mv_pred_flag" ); sps3dExt.setIvMvPredFlag( d, uiCode == 1 );
+    READ_FLAG( uiCode, "iv_mv_scaling_flag" ); sps3dExt.setIvMvScalingFlag( d, uiCode == 1 );
     if( d  ==  0 )
     {
-      READ_UVLC( uiCode, "log2_sub_pb_size_minus3" ); sps3dExt->setLog2SubPbSizeMinus3( d, uiCode );
-      READ_FLAG( uiCode, "iv_res_pred_flag" ); sps3dExt->setIvResPredFlag( d, uiCode == 1 );
-      READ_FLAG( uiCode, "depth_refinement_flag" ); sps3dExt->setDepthRefinementFlag( d, uiCode == 1 );
-      READ_FLAG( uiCode, "view_synthesis_pred_flag" ); sps3dExt->setViewSynthesisPredFlag( d, uiCode == 1 );
-      READ_FLAG( uiCode, "depth_based_blk_part_flag" ); sps3dExt->setDepthBasedBlkPartFlag( d, uiCode == 1 );
+      READ_UVLC( uiCode, "log2_sub_pb_size_minus3" ); sps3dExt.setLog2SubPbSizeMinus3( d, uiCode );
+      READ_FLAG( uiCode, "iv_res_pred_flag" ); sps3dExt.setIvResPredFlag( d, uiCode == 1 );
+      READ_FLAG( uiCode, "depth_refinement_flag" ); sps3dExt.setDepthRefinementFlag( d, uiCode == 1 );
+      READ_FLAG( uiCode, "view_synthesis_pred_flag" ); sps3dExt.setViewSynthesisPredFlag( d, uiCode == 1 );
+      READ_FLAG( uiCode, "depth_based_blk_part_flag" ); sps3dExt.setDepthBasedBlkPartFlag( d, uiCode == 1 );
     }
     else 
     {
-      READ_FLAG( uiCode, "mpi_flag" ); sps3dExt->setMpiFlag( d, uiCode == 1 );
-      READ_UVLC( uiCode, "log2_mpi_sub_pb_size_minus3" ); sps3dExt->setLog2MpiSubPbSizeMinus3( d, uiCode );
-      READ_FLAG( uiCode, "intra_contour_flag" ); sps3dExt->setIntraContourFlag( d, uiCode == 1 );
-      READ_FLAG( uiCode, "intra_sdc_wedge_flag" ); sps3dExt->setIntraSdcWedgeFlag( d, uiCode == 1 );
-      READ_FLAG( uiCode, "qt_pred_flag" ); sps3dExt->setQtPredFlag( d, uiCode == 1 );
-      READ_FLAG( uiCode, "inter_sdc_flag" ); sps3dExt->setInterSdcFlag( d, uiCode == 1 );
-      READ_FLAG( uiCode, "intra_skip_flag" ); sps3dExt->setDepthIntraSkipFlag( d, uiCode == 1 );
-    }
-  }
-}
-#endif
-
-Void TDecCavlc::parsePPSMultilayerExtension(TComPPS* pcPPS)
+      READ_FLAG( uiCode, "mpi_flag" ); sps3dExt.setMpiFlag( d, uiCode == 1 );
+      READ_UVLC( uiCode, "log2_mpi_sub_pb_size_minus3" ); sps3dExt.setLog2MpiSubPbSizeMinus3( d, uiCode );
+      READ_FLAG( uiCode, "intra_contour_flag" ); sps3dExt.setIntraContourFlag( d, uiCode == 1 );
+      READ_FLAG( uiCode, "intra_sdc_wedge_flag" ); sps3dExt.setIntraSdcWedgeFlag( d, uiCode == 1 );
+      READ_FLAG( uiCode, "qt_pred_flag" ); sps3dExt.setQtPredFlag( d, uiCode == 1 );
+      READ_FLAG( uiCode, "inter_sdc_flag" ); sps3dExt.setInterSdcFlag( d, uiCode == 1 );
+      READ_FLAG( uiCode, "intra_skip_flag" ); sps3dExt.setDepthIntraSkipFlag( d, uiCode == 1 );
+    }
+  }
+  pcSPS->setSps3dExtension( sps3dExt ); 
+}
+#endif
+
+Void TDecCavlc::parsePpsMultilayerExtension(TComPPS* pcPPS)
 {
   UInt uiCode = 0; 
@@ -1087,25 +1292,33 @@
 Void TDecCavlc::parseVPS(TComVPS* pcVPS)
 {
+#if ENC_DEC_TRACE
+#if H_MV_ENC_DEC_TRAC
+  tracePSHeader( "VPS", getDecTop()->getLayerId() ); 
+#else
+  xTraceVPSHeader ();
+#endif
+#endif
+
   UInt  uiCode;
-  
+
   READ_CODE( 4,  uiCode,  "vps_video_parameter_set_id" );         pcVPS->setVPSId( uiCode );
-#if H_MV
+#if NH_MV
   READ_FLAG( uiCode, "vps_base_layer_internal_flag" );            pcVPS->setVpsBaseLayerInternalFlag( uiCode == 1 );
   READ_FLAG( uiCode, "vps_base_layer_available_flag" );           pcVPS->setVpsBaseLayerAvailableFlag( uiCode == 1 );
 #else
-  READ_CODE( 2,  uiCode,  "vps_reserved_three_2bits" );           assert(uiCode == 3);
-#endif
-#if H_MV
+  READ_FLAG( uiCode,      "vps_base_layer_internal_flag" );       assert(uiCode == 1);
+  READ_FLAG( uiCode,      "vps_base_layer_available_flag" );      assert(uiCode == 1);
+#endif
+#if NH_MV
   READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );              pcVPS->setMaxLayersMinus1( std::min( uiCode, (UInt) ( MAX_NUM_LAYER_IDS-1) )  );
 #else
-  READ_CODE( 6,  uiCode,  "vps_reserved_zero_6bits" );            assert(uiCode == 0);
+  READ_CODE( 6,  uiCode,  "vps_max_layers_minus1" );
 #endif
   READ_CODE( 3,  uiCode,  "vps_max_sub_layers_minus1" );          pcVPS->setMaxTLayers( uiCode + 1 );    assert(uiCode+1 <= MAX_TLAYER);
   READ_FLAG(     uiCode,  "vps_temporal_id_nesting_flag" );       pcVPS->setTemporalNestingFlag( uiCode ? true:false );
   assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
-
-  READ_CODE( 16, uiCode,  "vps_reserved_ffff_16bits" );           assert(uiCode == 0xffff);
+  READ_CODE( 16, uiCode,  "vps_reserved_0xffff_16bits" );         assert(uiCode == 0xffff);
   parsePTL ( pcVPS->getPTL(), true, pcVPS->getMaxTLayers()-1);
-#if H_MV
+#if NH_MV
   pcVPS->getPTL()->inferGeneralValues ( true, 0, NULL );
   pcVPS->getPTL()->inferSubLayerValues( pcVPS->getMaxTLayers() - 1, 0, NULL );
@@ -1115,6 +1328,6 @@
   for(UInt i = 0; i <= pcVPS->getMaxTLayers()-1; i++)
   {
-    READ_UVLC( uiCode,  "vps_max_dec_pic_buffering_minus1[i]" );     pcVPS->setMaxDecPicBuffering( uiCode + 1, i );
-    READ_UVLC( uiCode,  "vps_num_reorder_pics[i]" );          pcVPS->setNumReorderPics( uiCode, i );
+    READ_UVLC( uiCode,  "vps_max_dec_pic_buffering_minus1[i]" );    pcVPS->setMaxDecPicBuffering( uiCode + 1, i );
+    READ_UVLC( uiCode,  "vps_max_num_reorder_pics[i]" );            pcVPS->setNumReorderPics( uiCode, i );
     READ_UVLC( uiCode,  "vps_max_latency_increase_plus1[i]" );      pcVPS->setMaxLatencyIncrease( uiCode, i );
 
@@ -1132,5 +1345,5 @@
 
   assert( pcVPS->getNumHrdParameters() < MAX_VPS_OP_SETS_PLUS1 );
-#if H_MV
+#if NH_MV
   assert( pcVPS->getVpsMaxLayerId() < MAX_VPS_NUH_LAYER_ID_PLUS1 );
   READ_CODE( 6, uiCode, "vps_max_layer_id" );   pcVPS->setVpsMaxLayerId( uiCode );
@@ -1142,6 +1355,6 @@
 #else
   assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
-  READ_CODE( 6, uiCode, "vps_max_nuh_reserved_zero_layer_id" );   pcVPS->setMaxNuhReservedZeroLayerId( uiCode );
-  READ_UVLC(    uiCode, "vps_max_op_sets_minus1" );               pcVPS->setMaxOpSets( uiCode + 1 );
+  READ_CODE( 6, uiCode, "vps_max_layer_id" );                        pcVPS->setMaxNuhReservedZeroLayerId( uiCode );
+  READ_UVLC(    uiCode, "vps_num_layer_sets_minus1" );               pcVPS->setMaxOpSets( uiCode + 1 );
   for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
   {
@@ -1150,10 +1363,11 @@
 #endif
     {
-      READ_FLAG( uiCode, "layer_id_included_flag[opsIdx][i]" );     pcVPS->setLayerIdIncludedFlag( uiCode == 1 ? true : false, opsIdx, i );
-    }
-  }
-#if H_MV
+      READ_FLAG( uiCode, "layer_id_included_flag[opsIdx][i]" );   pcVPS->setLayerIdIncludedFlag( uiCode == 1 ? true : false, opsIdx, i );
+    }
+  }
+#if NH_MV
   pcVPS->deriveLayerSetLayerIdList(); 
 #endif
+
   TimingInfo *timingInfo = pcVPS->getTimingInfo();
   READ_FLAG(       uiCode, "vps_timing_info_present_flag");         timingInfo->setTimingInfoPresentFlag      (uiCode ? true : false);
@@ -1167,4 +1381,5 @@
       READ_UVLC(   uiCode, "vps_num_ticks_poc_diff_one_minus1");    timingInfo->setNumTicksPocDiffOneMinus1   (uiCode);
     }
+
     READ_UVLC( uiCode, "vps_num_hrd_parameters" );                  pcVPS->setNumHrdParameters( uiCode );
 
@@ -1175,8 +1390,8 @@
     for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
     {
-      READ_UVLC( uiCode, "hrd_op_set_idx" );                       pcVPS->setHrdOpSetIdx( uiCode, i );
+      READ_UVLC( uiCode, "hrd_layer_set_idx[i]" );                  pcVPS->setHrdOpSetIdx( uiCode, i );
       if( i > 0 )
       {
-        READ_FLAG( uiCode, "cprms_present_flag[i]" );              pcVPS->setCprmsPresentFlag( uiCode == 1 ? true : false, i );
+        READ_FLAG( uiCode, "cprms_present_flag[i]" );               pcVPS->setCprmsPresentFlag( uiCode == 1 ? true : false, i );
       }
       else
@@ -1188,5 +1403,5 @@
     }
   }
-#if H_MV
+#if NH_MV
   READ_FLAG( uiCode,  "vps_extension_flag" );                      pcVPS->setVpsExtensionFlag( uiCode == 1 ? true : false );
   if ( pcVPS->getVpsExtensionFlag() )
@@ -1196,5 +1411,5 @@
 #endif
   {
-#if H_MV
+#if NH_MV
     m_pcBitstream->readOutTrailingBits();
     parseVPSExtension( pcVPS );   
@@ -1202,5 +1417,5 @@
     if (uiCode)
     {
-#if H_3D
+#if NH_3D
       READ_FLAG( uiCode,  "vps_3d_extension_flag" );
       if ( uiCode )
@@ -1208,5 +1423,5 @@
         m_pcBitstream->readOutTrailingBits();
         pcVPS->createCamPars(pcVPS->getNumViews());
-        parseVPS3dExtension( pcVPS );   
+        parseVps3dExtension( pcVPS );   
       }
       READ_FLAG( uiCode,  "vps_extension3_flag" );
@@ -1219,15 +1434,16 @@
           READ_FLAG( uiCode, "vps_extension_data_flag");
         }
-#if H_MV
-#if H_3D
-      }
-#endif
-    }
-#endif
-  }
-  return; 
-}
-
-#if H_MV
+#if NH_MV
+#if NH_3D
+      }
+#endif
+    }
+#endif
+  }
+
+  xReadRbspTrailingBits();
+}
+
+#if NH_MV
 Void TDecCavlc::parseVPSExtension( TComVPS* pcVPS )
 {
@@ -1306,5 +1522,5 @@
   }
 
-#if H_3D
+#if NH_3D
   pcVPS->initViewCompLayer( ); 
 #endif
@@ -1441,12 +1657,13 @@
   READ_UVLC( uiCode, "vps_num_rep_formats_minus1" ); pcVPS->setVpsNumRepFormatsMinus1( uiCode );
 
+  std::vector<TComRepFormat> repFormats;
+  repFormats.resize( pcVPS->getVpsNumRepFormatsMinus1() + 1 ); 
   for (Int i = 0; i <= pcVPS->getVpsNumRepFormatsMinus1(); i++ )
-  { 
-    assert( pcVPS->getRepFormat(i) == NULL ); 
-    TComRepFormat* curRepFormat = new TComRepFormat(); 
-    TComRepFormat* prevRepFormat = i > 0 ? pcVPS->getRepFormat( i - 1) : NULL; 
-    parseRepFormat( i, curRepFormat ,  prevRepFormat); 
-    pcVPS->setRepFormat(i, curRepFormat ); 
-  }
+  {     
+    TComRepFormat* curRepFormat = &repFormats[i]; 
+    TComRepFormat* prevRepFormat = i > 0 ? &repFormats[ i - 1] : NULL; 
+    parseRepFormat( i, curRepFormat ,  prevRepFormat);     
+  }
+  pcVPS->setRepFormat( repFormats ); 
 
   if ( pcVPS->getVpsNumRepFormatsMinus1() > 0 )
@@ -1514,19 +1731,21 @@
   }
   READ_FLAG( uiCode, "vps_vui_present_flag" );  pcVPS->setVpsVuiPresentFlag( uiCode == 1 );
+  
+  TComVPSVUI vpsVui;
   if( pcVPS->getVpsVuiPresentFlag() )
   {
     m_pcBitstream->readOutTrailingBits(); // vps_vui_alignment_bit_equal_to_one
-    parseVPSVUI( pcVPS ); 
+    parseVPSVUI( pcVPS, &vpsVui ); 
   }     
   else
-  {
-    TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
-    assert( pcVPSVUI ); 
-    pcVPSVUI->inferVpsVui( false ); 
-  }
+  {    
+    // inference of syntax elements that differ from default inference (as done in constructor), when VPS VUI is not present    
+    vpsVui.setCrossLayerIrapAlignedFlag( false ); 
+  }
+  pcVPS->setVPSVUI( vpsVui ); 
   pcVPS->checkVPSExtensionSyntax(); 
 }
 
-Void TDecCavlc::parseRepFormat( Int i, TComRepFormat* pcRepFormat, TComRepFormat* pcPrevRepFormat )
+Void TDecCavlc::parseRepFormat( Int i, TComRepFormat* pcRepFormat, const TComRepFormat* pcPrevRepFormat )
 {
   assert( pcRepFormat ); 
@@ -1552,5 +1771,5 @@
   else
   {
-    pcRepFormat->inferChromaAndBitDepth(pcPrevRepFormat, false ); 
+    pcRepFormat->inferChromaAndBitDepth(pcPrevRepFormat ); 
   }
   READ_FLAG( uiCode, "conformance_window_vps_flag" ); pcRepFormat->setConformanceWindowVpsFlag( uiCode == 1 );
@@ -1565,25 +1784,24 @@
 
 
-Void TDecCavlc::parseVPSVUI( TComVPS* pcVPS )
+Void TDecCavlc::parseVPSVUI( const TComVPS* pcVPS, TComVPSVUI* vpsVui )
 {
   assert( pcVPS ); 
-
-  TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
-
-  assert( pcVPSVUI ); 
+  assert( vpsVui ); 
+  
+  vpsVui->init(pcVPS->getNumAddLayerSets(),pcVPS->getMaxSubLayersMinus1() + 1, pcVPS->getMaxLayersMinus1() + 1 );  
 
   UInt uiCode; 
-  READ_FLAG( uiCode, "cross_layer_pic_type_aligned_flag" ); pcVPSVUI->setCrossLayerPicTypeAlignedFlag( uiCode == 1 );
-  if ( !pcVPSVUI->getCrossLayerPicTypeAlignedFlag() )
+  READ_FLAG( uiCode, "cross_layer_pic_type_aligned_flag" ); vpsVui->setCrossLayerPicTypeAlignedFlag( uiCode == 1 );
+  if ( !vpsVui->getCrossLayerPicTypeAlignedFlag() )
   {  
-    READ_FLAG( uiCode, "cross_layer_irap_aligned_flag" ); pcVPSVUI->setCrossLayerIrapAlignedFlag( uiCode == 1 );
-  }
-  if( pcVPSVUI->getCrossLayerIrapAlignedFlag( ) )
-  {
-    READ_FLAG( uiCode, "all_layers_idr_aligned_flag" ); pcVPSVUI->setAllLayersIdrAlignedFlag( uiCode == 1 );
-  }
-  READ_FLAG( uiCode, "bit_rate_present_vps_flag" ); pcVPSVUI->setBitRatePresentVpsFlag( uiCode == 1 );
-  READ_FLAG( uiCode, "pic_rate_present_vps_flag" ); pcVPSVUI->setPicRatePresentVpsFlag( uiCode == 1 );
-  if( pcVPSVUI->getBitRatePresentVpsFlag( )  ||  pcVPSVUI->getPicRatePresentVpsFlag( ) )
+    READ_FLAG( uiCode, "cross_layer_irap_aligned_flag" ); vpsVui->setCrossLayerIrapAlignedFlag( uiCode == 1 );
+  }
+  if( vpsVui->getCrossLayerIrapAlignedFlag( ) )
+  {
+    READ_FLAG( uiCode, "all_layers_idr_aligned_flag" ); vpsVui->setAllLayersIdrAlignedFlag( uiCode == 1 );
+  }
+  READ_FLAG( uiCode, "bit_rate_present_vps_flag" ); vpsVui->setBitRatePresentVpsFlag( uiCode == 1 );
+  READ_FLAG( uiCode, "pic_rate_present_vps_flag" ); vpsVui->setPicRatePresentVpsFlag( uiCode == 1 );
+  if( vpsVui->getBitRatePresentVpsFlag( )  ||  vpsVui->getPicRatePresentVpsFlag( ) )
   {
     for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <  pcVPS->getNumLayerSets(); i++ )
@@ -1591,56 +1809,57 @@
       for( Int j = 0; j  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( i ); j++ ) 
       {
-        if( pcVPSVUI->getBitRatePresentVpsFlag( ) )
-        {
-          READ_FLAG( uiCode, "bit_rate_present_flag" ); pcVPSVUI->setBitRatePresentFlag( i, j, uiCode == 1 );            
-        }
-        if( pcVPSVUI->getPicRatePresentVpsFlag( )  )
-        {
-          READ_FLAG( uiCode, "pic_rate_present_flag" ); pcVPSVUI->setPicRatePresentFlag( i, j, uiCode == 1 );
-        }
-        if( pcVPSVUI->getBitRatePresentFlag( i, j ) )
-        {
-          READ_CODE( 16, uiCode, "avg_bit_rate" ); pcVPSVUI->setAvgBitRate( i, j, uiCode );
-          READ_CODE( 16, uiCode, "max_bit_rate" ); pcVPSVUI->setMaxBitRate( i, j, uiCode );
-        }
-        if( pcVPSVUI->getPicRatePresentFlag( i, j ) )
-        {
-          READ_CODE( 2,  uiCode, "constant_pic_rate_idc" ); pcVPSVUI->setConstantPicRateIdc( i, j, uiCode );
-          READ_CODE( 16, uiCode, "avg_pic_rate" );          pcVPSVUI->setAvgPicRate( i, j, uiCode );
-        }
-      }
-    }
-  }
-
-  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); pcVPSVUI->setVideoSignalInfoIdxPresentFlag( uiCode == 1 );
-  if( pcVPSVUI->getVideoSignalInfoIdxPresentFlag() )
-  {
-    READ_CODE( 4, uiCode, "vps_num_video_signal_info_minus1" ); pcVPSVUI->setVpsNumVideoSignalInfoMinus1( uiCode );
+        if( vpsVui->getBitRatePresentVpsFlag( ) )
+        {
+          READ_FLAG( uiCode, "bit_rate_present_flag" ); vpsVui->setBitRatePresentFlag( i, j, uiCode == 1 );            
+        }
+        if( vpsVui->getPicRatePresentVpsFlag( )  )
+        {
+          READ_FLAG( uiCode, "pic_rate_present_flag" ); vpsVui->setPicRatePresentFlag( i, j, uiCode == 1 );
+        }
+        if( vpsVui->getBitRatePresentFlag( i, j ) )
+        {
+          READ_CODE( 16, uiCode, "avg_bit_rate" ); vpsVui->setAvgBitRate( i, j, uiCode );
+          READ_CODE( 16, uiCode, "max_bit_rate" ); vpsVui->setMaxBitRate( i, j, uiCode );
+        }
+        if( vpsVui->getPicRatePresentFlag( i, j ) )
+        {
+          READ_CODE( 2,  uiCode, "constant_pic_rate_idc" ); vpsVui->setConstantPicRateIdc( i, j, uiCode );
+          READ_CODE( 16, uiCode, "avg_pic_rate" );          vpsVui->setAvgPicRate( i, j, uiCode );
+        }
+      }
+    }
+  }
+
+  READ_FLAG( uiCode, "video_signal_info_idx_present_flag" ); vpsVui->setVideoSignalInfoIdxPresentFlag( uiCode == 1 );
+  if( vpsVui->getVideoSignalInfoIdxPresentFlag() )
+  {
+    READ_CODE( 4, uiCode, "vps_num_video_signal_info_minus1" ); vpsVui->setVpsNumVideoSignalInfoMinus1( uiCode );
   }
   else
   {
-    pcVPSVUI->setVpsNumVideoSignalInfoMinus1( pcVPS->getMaxLayersMinus1() - pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1 ); 
-  }
-
-  for( Int i = 0; i <= pcVPSVUI->getVpsNumVideoSignalInfoMinus1(); i++ )
-  {
-    assert( pcVPSVUI->getVideoSignalInfo( i ) == NULL ); 
-    TComVideoSignalInfo* curVideoSignalInfo = new TComVideoSignalInfo();     
-    parseVideoSignalInfo( curVideoSignalInfo ); 
-    pcVPSVUI->setVideoSignalInfo(i, curVideoSignalInfo ); 
-  }
-
-  if( pcVPSVUI->getVideoSignalInfoIdxPresentFlag() && pcVPSVUI->getVpsNumVideoSignalInfoMinus1() > 0 )
+    vpsVui->setVpsNumVideoSignalInfoMinus1( (pcVPS->getMaxLayersMinus1() - pcVPS->getVpsBaseLayerInternalFlag()) ? 0 : 1 );
+  }
+
+  std::vector<TComVideoSignalInfo> videoSignalInfos; 
+  videoSignalInfos.resize(vpsVui->getVpsNumVideoSignalInfoMinus1() + 1 );
+
+  for( Int i = 0; i <= vpsVui->getVpsNumVideoSignalInfoMinus1(); i++ )
+  { 
+    parseVideoSignalInfo( &videoSignalInfos[i] );     
+  }
+  vpsVui->setVideoSignalInfo( videoSignalInfos ); 
+
+  if( vpsVui->getVideoSignalInfoIdxPresentFlag() && vpsVui->getVpsNumVideoSignalInfoMinus1() > 0 )
   {
     for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
     {
-      READ_CODE( 4, uiCode, "vps_video_signal_info_idx" ); pcVPSVUI->setVpsVideoSignalInfoIdx( i, uiCode );
-    }
-  }
-  else if ( !pcVPSVUI->getVideoSignalInfoIdxPresentFlag() )
+      READ_CODE( 4, uiCode, "vps_video_signal_info_idx" ); vpsVui->setVpsVideoSignalInfoIdx( i, uiCode );
+    }
+  }
+  else if ( !vpsVui->getVideoSignalInfoIdxPresentFlag() )
   {
     for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
     {
-      pcVPSVUI->setVpsVideoSignalInfoIdx( i, i );
+      vpsVui->setVpsVideoSignalInfoIdx( i, i );
     }
   }
@@ -1649,17 +1868,17 @@
     for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i <=  pcVPS->getMaxLayersMinus1(); i++ )
     {
-      pcVPSVUI->setVpsVideoSignalInfoIdx( i, 0 );
-    }
-  }
-
-  READ_FLAG( uiCode, "tiles_not_in_use_flag" ); pcVPSVUI->setTilesNotInUseFlag( uiCode == 1 );
-  if( !pcVPSVUI->getTilesNotInUseFlag() ) 
+      vpsVui->setVpsVideoSignalInfoIdx( i, 0 );
+    }
+  }
+
+  READ_FLAG( uiCode, "tiles_not_in_use_flag" ); vpsVui->setTilesNotInUseFlag( uiCode == 1 );
+  if( !vpsVui->getTilesNotInUseFlag() ) 
   {      
     for( Int i = pcVPS->getVpsBaseLayerInternalFlag() ? 0 : 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
     {
-      READ_FLAG( uiCode, "tiles_in_use_flag[i]" ); pcVPSVUI->setTilesInUseFlag( i, uiCode == 1 );
-      if( pcVPSVUI->getTilesInUseFlag( i ) )  
-      {
-        READ_FLAG( uiCode, "loop_filter_not_across_tiles_flag[i]" ); pcVPSVUI->setLoopFilterNotAcrossTilesFlag( i, uiCode == 1 );
+      READ_FLAG( uiCode, "tiles_in_use_flag[i]" ); vpsVui->setTilesInUseFlag( i, uiCode == 1 );
+      if( vpsVui->getTilesInUseFlag( i ) )  
+      {
+        READ_FLAG( uiCode, "loop_filter_not_across_tiles_flag[i]" ); vpsVui->setLoopFilterNotAcrossTilesFlag( i, uiCode == 1 );
       }
     }  
@@ -1669,7 +1888,7 @@
       {
         Int layerIdx = pcVPS->getLayerIdInVps(pcVPS->getIdDirectRefLayer(pcVPS->getLayerIdInNuh( i ) , j  ));  
-        if( pcVPSVUI->getTilesInUseFlag( i )  &&  pcVPSVUI->getTilesInUseFlag( layerIdx ) )  
-        {
-          READ_FLAG( uiCode, "tile_boundaries_aligned_flag[i][j]" ); pcVPSVUI->setTileBoundariesAlignedFlag( i, j, uiCode == 1 );
+        if( vpsVui->getTilesInUseFlag( i )  &&  vpsVui->getTilesInUseFlag( layerIdx ) )  
+        {
+          READ_FLAG( uiCode, "tile_boundaries_aligned_flag[i][j]" ); vpsVui->setTileBoundariesAlignedFlag( i, j, uiCode == 1 );
         }
       }  
@@ -1677,18 +1896,18 @@
   }  
   
-  READ_FLAG( uiCode, "wpp_not_in_use_flag" ); pcVPSVUI->setWppNotInUseFlag( uiCode == 1 );
+  READ_FLAG( uiCode, "wpp_not_in_use_flag" ); vpsVui->setWppNotInUseFlag( uiCode == 1 );
   
-  if( !pcVPSVUI->getWppNotInUseFlag( ))
+  if( !vpsVui->getWppNotInUseFlag( ))
   {
     for( Int i = 0; i  <=  pcVPS->getMaxLayersMinus1(); i++ )  
     {
-      READ_FLAG( uiCode, "wpp_in_use_flag[i]" ); pcVPSVUI->setWppInUseFlag( i, uiCode == 1 );
-    }
-  }
-  READ_FLAG( uiCode, "single_layer_for_non_irap_flag" ); pcVPSVUI->setSingleLayerForNonIrapFlag( uiCode == 1 );
-  READ_FLAG( uiCode, "higher_layer_irap_skip_flag" ); pcVPSVUI->setHigherLayerIrapSkipFlag( uiCode == 1 );
-  READ_FLAG( uiCode, "ilp_restricted_ref_layers_flag" ); pcVPSVUI->setIlpRestrictedRefLayersFlag( uiCode == 1 );
-
-  if( pcVPSVUI->getIlpRestrictedRefLayersFlag( ) )
+      READ_FLAG( uiCode, "wpp_in_use_flag[i]" ); vpsVui->setWppInUseFlag( i, uiCode == 1 );
+    }
+  }
+  READ_FLAG( uiCode, "single_layer_for_non_irap_flag" ); vpsVui->setSingleLayerForNonIrapFlag( uiCode == 1 );
+  READ_FLAG( uiCode, "higher_layer_irap_skip_flag" ); vpsVui->setHigherLayerIrapSkipFlag( uiCode == 1 );
+  READ_FLAG( uiCode, "ilp_restricted_ref_layers_flag" ); vpsVui->setIlpRestrictedRefLayersFlag( uiCode == 1 );
+
+  if( vpsVui->getIlpRestrictedRefLayersFlag( ) )
   {
     for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
@@ -1698,11 +1917,11 @@
         if( pcVPS->getVpsBaseLayerInternalFlag() || pcVPS->getIdDirectRefLayer( pcVPS->getLayerIdInNuh( i ), j ) > 0 )
         {        
-          READ_UVLC( uiCode, "min_spatial_segment_offset_plus1" ); pcVPSVUI->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
-          if( pcVPSVUI->getMinSpatialSegmentOffsetPlus1( i, j ) > 0 )
+          READ_UVLC( uiCode, "min_spatial_segment_offset_plus1" ); vpsVui->setMinSpatialSegmentOffsetPlus1( i, j, uiCode );
+          if( vpsVui->getMinSpatialSegmentOffsetPlus1( i, j ) > 0 )
           {
-            READ_FLAG( uiCode, "ctu_based_offset_enabled_flag" ); pcVPSVUI->setCtuBasedOffsetEnabledFlag( i, j, uiCode == 1 );
-            if( pcVPSVUI->getCtuBasedOffsetEnabledFlag( i, j ) )
+            READ_FLAG( uiCode, "ctu_based_offset_enabled_flag" ); vpsVui->setCtuBasedOffsetEnabledFlag( i, j, uiCode == 1 );
+            if( vpsVui->getCtuBasedOffsetEnabledFlag( i, j ) )
             {
-              READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1" ); pcVPSVUI->setMinHorizontalCtuOffsetPlus1( i, j, uiCode );
+              READ_UVLC( uiCode, "min_horizontal_ctu_offset_plus1" ); vpsVui->setMinHorizontalCtuOffsetPlus1( i, j, uiCode );
             }
           }
@@ -1712,9 +1931,9 @@
   }
 
-  READ_FLAG( uiCode, "vps_vui_bsp_hrd_present_flag" ); pcVPSVUI->setVpsVuiBspHrdPresentFlag( uiCode == 1 );
-  if ( pcVPSVUI->getVpsVuiBspHrdPresentFlag( ) )
-  {
-    assert(pcVPS->getTimingInfo()->getTimingInfoPresentFlag() == 1);
-    parseVpsVuiBspHrdParameters( pcVPS ); 
+  READ_FLAG( uiCode, "vps_vui_bsp_hrd_present_flag" ); vpsVui->setVpsVuiBspHrdPresentFlag( uiCode == 1 );
+  if ( vpsVui->getVpsVuiBspHrdPresentFlag( ) )
+  {
+    assert(pcVPS->getTimingInfo()->getTimingInfoPresentFlag() == 1);    
+    parseVpsVuiBspHrdParameters( pcVPS, vpsVui );     
   }
   for( Int i = 1; i  <=  pcVPS->getMaxLayersMinus1(); i++ )
@@ -1722,42 +1941,39 @@
     if( pcVPS->getNumDirectRefLayers( pcVPS->getLayerIdInNuh( i )) == 0 ) 
     {
-      READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); pcVPSVUI->setBaseLayerParameterSetCompatibilityFlag( i, uiCode == 1 );
-    }
-  }
-}
-
-Void TDecCavlc::parseVpsVuiBspHrdParameters( TComVPS* pcVPS )
-{
-  assert( pcVPS ); 
-
-  TComVPSVUI* pcVPSVUI = pcVPS->getVPSVUI( ); 
-
-  assert( pcVPSVUI ); 
-
-  TComVpsVuiBspHrdParameters*  vpsVuiBspHrdP = pcVPSVUI->getVpsVuiBspHrdParameters(); 
-  assert( vpsVuiBspHrdP == NULL ); 
-  vpsVuiBspHrdP = new TComVpsVuiBspHrdParameters; 
-  pcVPSVUI->setVpsVuiBspHrdParameters( vpsVuiBspHrdP ); 
+      READ_FLAG( uiCode, "base_layer_parameter_set_compatibility_flag" ); vpsVui->setBaseLayerParameterSetCompatibilityFlag( i, uiCode == 1 );
+    }
+  }  
+}
+
+Void TDecCavlc::parseVpsVuiBspHrdParameters( const TComVPS* pcVPS, TComVPSVUI* vpsVui )
+{
+  assert( pcVPS  ); 
+  assert( vpsVui ); 
+
+  TComVpsVuiBspHrdParameters  vpsVuiBspHrdP;    
+  
   UInt uiCode; 
-  READ_UVLC( uiCode, "vps_num_add_hrd_params" ); vpsVuiBspHrdP->setVpsNumAddHrdParams( uiCode );
-  vpsVuiBspHrdP->createAfterVpsNumAddHrdParams( pcVPS ); 
-  for( Int i = pcVPS->getNumHrdParameters(); i < pcVPS->getNumHrdParameters() + vpsVuiBspHrdP->getVpsNumAddHrdParams(); i++ )
+  READ_UVLC( uiCode, "vps_num_add_hrd_params" ); vpsVuiBspHrdP.setVpsNumAddHrdParams( uiCode );
+  vpsVuiBspHrdP.createAfterVpsNumAddHrdParams( pcVPS ); 
+  for( Int i = pcVPS->getNumHrdParameters(); i < pcVPS->getNumHrdParameters() + vpsVuiBspHrdP.getVpsNumAddHrdParams(); i++ )
   {  
     if( i > 0 )  
     {
-      READ_FLAG( uiCode, "cprms_add_present_flag" ); vpsVuiBspHrdP->setCprmsAddPresentFlag( i, uiCode == 1 );
+      READ_FLAG( uiCode, "cprms_add_present_flag" ); vpsVuiBspHrdP.setCprmsAddPresentFlag( i, uiCode == 1 );
     }
     else
     {
-       vpsVuiBspHrdP->setCprmsAddPresentFlag( i, true );
-    }
-
-    READ_UVLC( uiCode, "num_sub_layer_hrd_minus1" ); vpsVuiBspHrdP->setNumSubLayerHrdMinus1( i, uiCode );
-    TComHRD* hrdParameters = vpsVuiBspHrdP->getHrdParametermeters( i ); 
-    parseHrdParameters( hrdParameters, vpsVuiBspHrdP->getCprmsAddPresentFlag( i ), vpsVuiBspHrdP->getNumSubLayerHrdMinus1( i ) );     
-  }
-
-  vpsVuiBspHrdP->setNumPartitionsInSchemeMinus1( 0, 0, 0);
-  vpsVuiBspHrdP->createAfterNumPartitionsInSchemeMinus1( 0, 0 );
+       vpsVuiBspHrdP.setCprmsAddPresentFlag( i, true );
+    }
+
+    READ_UVLC( uiCode, "num_sub_layer_hrd_minus1" ); vpsVuiBspHrdP.setNumSubLayerHrdMinus1( i, uiCode );
+    
+    TComHRD hrdParameters;
+    parseHrdParameters( &hrdParameters, vpsVuiBspHrdP.getCprmsAddPresentFlag( i ), vpsVuiBspHrdP.getNumSubLayerHrdMinus1( i ) );     
+    vpsVuiBspHrdP.setHrdParametermeters( i, hrdParameters ); 
+  }
+
+  vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( 0, 0, 0);
+  vpsVuiBspHrdP.createAfterNumPartitionsInSchemeMinus1( pcVPS, 0, 0 );
 
   for( Int h = 0; h < pcVPS->getNumOutputLayerSets(); h++ )
@@ -1765,29 +1981,29 @@
     if ( h == 0)
     {
-      vpsVuiBspHrdP->setNumSignalledPartitioningSchemes( h, 0 );
+      vpsVuiBspHrdP.setNumSignalledPartitioningSchemes( h, 0 );
     }
     else
     {
-      READ_UVLC( uiCode, "num_signalled_partitioning_schemes" ); vpsVuiBspHrdP->setNumSignalledPartitioningSchemes( h, uiCode );
+      READ_UVLC( uiCode, "num_signalled_partitioning_schemes" ); vpsVuiBspHrdP.setNumSignalledPartitioningSchemes( h, uiCode );
     }    
-    vpsVuiBspHrdP->createAfterNumSignalledPartitioningSchemes( h );
-
-    for( Int j = 0; j < vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ) + 1; j++ )
+    vpsVuiBspHrdP.createAfterNumSignalledPartitioningSchemes( pcVPS, h );
+
+    for( Int j = 0; j < vpsVuiBspHrdP.getNumSignalledPartitioningSchemes( h ) + 1; j++ )
     { 
       if ( j == 0 && h == 0 )
       {
-        vpsVuiBspHrdP->setNumPartitionsInSchemeMinus1( h, j, uiCode );
+        vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, uiCode );
       }
       else if( j == 0 )
       {
-        vpsVuiBspHrdP->setNumPartitionsInSchemeMinus1( h, j, pcVPS->getNumLayersInIdList( h ) - 1 );
+        vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, pcVPS->getNumLayersInIdList( h ) - 1 );
       }
       else
       {
-        READ_UVLC( uiCode, "num_partitions_in_scheme_minus1" ); vpsVuiBspHrdP->setNumPartitionsInSchemeMinus1( h, j, uiCode );
-      }
-      vpsVuiBspHrdP->createAfterNumPartitionsInSchemeMinus1( h, j );
-
-      for( Int k = 0; k  <=  vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ); k++ )  
+        READ_UVLC( uiCode, "num_partitions_in_scheme_minus1" ); vpsVuiBspHrdP.setNumPartitionsInSchemeMinus1( h, j, uiCode );
+      }
+      vpsVuiBspHrdP.createAfterNumPartitionsInSchemeMinus1(pcVPS, h, j );
+
+      for( Int k = 0; k  <=  vpsVuiBspHrdP.getNumPartitionsInSchemeMinus1( h, j ); k++ )  
       {
         for( Int r = 0; r < pcVPS->getNumLayersInIdList(pcVPS->olsIdxToLsIdx( h ) )   ; r++ )  
@@ -1795,13 +2011,13 @@
           if( h == 0 && j == 0 && k == 0 && r == 0 )
           {
-             vpsVuiBspHrdP->setLayerIncludedInPartitionFlag( h, j, k, r, true );
+             vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, true );
           }
           else if ( h > 0 && j == 0 )
           {
-             vpsVuiBspHrdP->setLayerIncludedInPartitionFlag( h, j, k, r, (k == r) );
+             vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, (k == r) );
           }
           else
           {
-            READ_FLAG( uiCode, "layer_included_in_partition_flag" ); vpsVuiBspHrdP->setLayerIncludedInPartitionFlag( h, j, k, r, uiCode == 1 );
+            READ_FLAG( uiCode, "layer_included_in_partition_flag" ); vpsVuiBspHrdP.setLayerIncludedInPartitionFlag( h, j, k, r, uiCode == 1 );
           }          
         }
@@ -1810,16 +2026,16 @@
     if ( h > 0 )
     {
-      for( Int i = 0; i < vpsVuiBspHrdP->getNumSignalledPartitioningSchemes( h ) + 1; i++ )  
+      for( Int i = 0; i < vpsVuiBspHrdP.getNumSignalledPartitioningSchemes( h ) + 1; i++ )  
       {
         for( Int t = 0; t  <=  pcVPS->getMaxSubLayersInLayerSetMinus1( pcVPS->olsIdxToLsIdx( i ) ); t++ )
         {  
-          READ_UVLC( uiCode, "num_bsp_schedules_minus1" ); vpsVuiBspHrdP->setNumBspSchedulesMinus1( h, i, t, uiCode );
-          vpsVuiBspHrdP->createAfterNumBspSchedulesMinus1( h, i, t );
-          for( Int j = 0; j  <=  vpsVuiBspHrdP->getNumBspSchedulesMinus1( h, i, t ); j++ )  
+          READ_UVLC( uiCode, "num_bsp_schedules_minus1" ); vpsVuiBspHrdP.setNumBspSchedulesMinus1( h, i, t, uiCode );
+          vpsVuiBspHrdP.createAfterNumBspSchedulesMinus1(pcVPS, h, i, t );
+          for( Int j = 0; j  <=  vpsVuiBspHrdP.getNumBspSchedulesMinus1( h, i, t ); j++ )  
           {
-            for( Int k = 0; k  <=  vpsVuiBspHrdP->getNumPartitionsInSchemeMinus1( h, j ); k++ )
+            for( Int k = 0; k  <=  vpsVuiBspHrdP.getNumPartitionsInSchemeMinus1( h, j ); k++ )
             {  
-              READ_CODE( vpsVuiBspHrdP->getBspHrdIdxLen( pcVPS ), uiCode, "bsp_hrd_idx" ); vpsVuiBspHrdP->setBspHrdIdx( h, i, t, j, k, uiCode );
-              READ_UVLC( uiCode, "bsp_sched_idx" ); vpsVuiBspHrdP->setBspSchedIdx( h, i, t, j, k, uiCode );
+              READ_CODE( vpsVuiBspHrdP.getBspHrdIdxLen( pcVPS ), uiCode, "bsp_hrd_idx" ); vpsVuiBspHrdP.setBspHrdIdx( h, i, t, j, k, uiCode );
+              READ_UVLC( uiCode, "bsp_sched_idx" ); vpsVuiBspHrdP.setBspSchedIdx( h, i, t, j, k, uiCode );
             }  
           }
@@ -1828,4 +2044,5 @@
     }
   }  
+  vpsVui->setVpsVuiBspHrdParameters( vpsVuiBspHrdP ); 
 }
 
@@ -1843,18 +2060,19 @@
 {
   UInt uiCode; 
-  TComDpbSize* dpbSize = vps->getDpbSize(); 
-  assert ( dpbSize != 0 ); 
+  TComDpbSize dpbSize; 
+  
+  dpbSize.init( vps->getNumOutputLayerSets(), vps->getVpsMaxLayerId() + 1, vps->getMaxSubLayersMinus1() + 1 ) ;
 
   for( Int i = 1; i < vps->getNumOutputLayerSets(); i++ )
   {  
     Int currLsIdx = vps->olsIdxToLsIdx( i ); 
-    READ_FLAG( uiCode, "sub_layer_flag_info_present_flag" ); dpbSize->setSubLayerFlagInfoPresentFlag( i, uiCode == 1 );
+    READ_FLAG( uiCode, "sub_layer_flag_info_present_flag" ); dpbSize.setSubLayerFlagInfoPresentFlag( i, uiCode == 1 );
     for( Int j = 0; j  <=  vps->getMaxSubLayersInLayerSetMinus1( currLsIdx ); j++ )
     {  
-      if( j > 0  &&  dpbSize->getSubLayerDpbInfoPresentFlag( i, j )  )  
-      {
-        READ_FLAG( uiCode, "sub_layer_dpb_info_present_flag" ); dpbSize->setSubLayerDpbInfoPresentFlag( i, j, uiCode == 1 );
-      }
-      if( dpbSize->getSubLayerDpbInfoPresentFlag( i, j ) )
+      if( j > 0  &&  dpbSize.getSubLayerDpbInfoPresentFlag( i, j )  )  
+      {
+        READ_FLAG( uiCode, "sub_layer_dpb_info_present_flag" ); dpbSize.setSubLayerDpbInfoPresentFlag( i, j, uiCode == 1 );
+      }
+      if( dpbSize.getSubLayerDpbInfoPresentFlag( i, j ) )
       {  
         for( Int k = 0; k < vps->getNumLayersInIdList( currLsIdx ); k++ )   
@@ -1862,5 +2080,5 @@
           if ( vps->getNecessaryLayerFlag( i, k ) && ( vps->getVpsBaseLayerInternalFlag() || ( vps->getLayerSetLayerIdList(vps->olsIdxToLsIdx(i),k) != 0 ) ))
           {
-            READ_UVLC( uiCode, "max_vps_dec_pic_buffering_minus1" ); dpbSize->setMaxVpsDecPicBufferingMinus1( i, k, j, uiCode );
+            READ_UVLC( uiCode, "max_vps_dec_pic_buffering_minus1" ); dpbSize.setMaxVpsDecPicBufferingMinus1( i, k, j, uiCode );
           }
           else
@@ -1868,10 +2086,10 @@
             if ( vps->getNecessaryLayerFlag( i, k ) && ( j == 0 ) && ( k == 0 ))
             {
-              dpbSize->setMaxVpsDecPicBufferingMinus1(i ,k, j, 0 );
+              dpbSize.setMaxVpsDecPicBufferingMinus1(i ,k, j, 0 );
             }
           }
         }
-        READ_UVLC( uiCode, "max_vps_num_reorder_pics" ); dpbSize->setMaxVpsNumReorderPics( i, j, uiCode );
-        READ_UVLC( uiCode, "max_vps_latency_increase_plus1" ); dpbSize->setMaxVpsLatencyIncreasePlus1( i, j, uiCode );
+        READ_UVLC( uiCode, "max_vps_num_reorder_pics" ); dpbSize.setMaxVpsNumReorderPics( i, j, uiCode );
+        READ_UVLC( uiCode, "max_vps_latency_increase_plus1" ); dpbSize.setMaxVpsLatencyIncreasePlus1( i, j, uiCode );
       }
       else
@@ -1883,17 +2101,18 @@
             if ( vps->getNecessaryLayerFlag(i, k ) )
             {            
-              dpbSize->setMaxVpsDecPicBufferingMinus1( i, k, j, dpbSize->getMaxVpsDecPicBufferingMinus1( i,k, j - 1 ) );
+              dpbSize.setMaxVpsDecPicBufferingMinus1( i, k, j, dpbSize.getMaxVpsDecPicBufferingMinus1( i,k, j - 1 ) );
             }
           }
-          dpbSize->setMaxVpsNumReorderPics      ( i, j, dpbSize->getMaxVpsNumReorderPics      ( i, j - 1 ) );
-          dpbSize->setMaxVpsLatencyIncreasePlus1( i, j, dpbSize->getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
+          dpbSize.setMaxVpsNumReorderPics      ( i, j, dpbSize.getMaxVpsNumReorderPics      ( i, j - 1 ) );
+          dpbSize.setMaxVpsLatencyIncreasePlus1( i, j, dpbSize.getMaxVpsLatencyIncreasePlus1( i, j - 1 ) );
         }
       }
     }  
   }  
-}
-
-#if H_3D
-Void TDecCavlc::parseVPS3dExtension( TComVPS* pcVPS )
+  vps->setDpbSize( dpbSize ); 
+}
+
+#if NH_3D
+Void TDecCavlc::parseVps3dExtension( TComVPS* pcVPS )
 {
   UInt uiCode;   
@@ -1929,9 +2148,6 @@
 #endif
 #endif
-#if H_MV
-Void TDecCavlc::parseSliceHeader (TComSlice*& rpcSlice, ParameterSetManagerDecoder *parameterSetManager, Int targetOlsIdx)
-#else
-Void TDecCavlc::parseSliceHeader (TComSlice*& rpcSlice, ParameterSetManagerDecoder *parameterSetManager)
-#endif
+
+Void TDecCavlc::parseSliceHeader (TComSlice* pcSlice, ParameterSetManager *parameterSetManager, const Int prevTid0POC)
 {
   UInt  uiCode;
@@ -1939,9 +2155,13 @@
 
 #if ENC_DEC_TRACE
-  xTraceSliceHeader(rpcSlice);
+#if NH_MV
+  tracePSHeader( "Slice", pcSlice->getLayerId() ); 
+#else
+  xTraceSliceHeader();
+#endif
 #endif
   TComPPS* pps = NULL;
   TComSPS* sps = NULL;
-#if H_MV
+#if NH_MV
   TComVPS* vps = NULL;
 #endif
@@ -1949,52 +2169,62 @@
   UInt firstSliceSegmentInPic;
   READ_FLAG( firstSliceSegmentInPic, "first_slice_segment_in_pic_flag" );
-  if( rpcSlice->getRapPicFlag())
-  { 
+  if( pcSlice->getRapPicFlag())
+  {
     READ_FLAG( uiCode, "no_output_of_prior_pics_flag" );  //ignored -- updated already
-#if SETTING_NO_OUT_PIC_PRIOR
-    rpcSlice->setNoOutputPriorPicsFlag(uiCode ? true : false);
-#else
-    rpcSlice->setNoOutputPicPrior( false );
-#endif
-  }
-
-  READ_UVLC (    uiCode, "slice_pic_parameter_set_id" );  rpcSlice->setPPSId(uiCode);
-  pps = parameterSetManager->getPrefetchedPPS(uiCode);
+    pcSlice->setNoOutputPriorPicsFlag(uiCode ? true : false);
+  }
+  READ_UVLC (    uiCode, "slice_pic_parameter_set_id" );  pcSlice->setPPSId(uiCode);
+  pps = parameterSetManager->getPPS(uiCode);
   //!KS: need to add error handling code here, if PPS is not available
   assert(pps!=0);
-  sps = parameterSetManager->getPrefetchedSPS(pps->getSPSId());
+  sps = parameterSetManager->getSPS(pps->getSPSId());
   //!KS: need to add error handling code here, if SPS is not available
   assert(sps!=0);
-#if H_MV
-  vps = parameterSetManager->getPrefetchedVPS(sps->getVPSId());
-  assert( vps != NULL );
-  
-  sps->inferRepFormat  ( vps , rpcSlice->getLayerId() ); 
+#if NH_MV  
+  vps = parameterSetManager->getVPS(sps->getVPSId());
+  assert( vps != NULL );  
+  m_decTop->initFromActiveVps( vps );
+  Int targetOlsIdx = m_decTop->getTargetOlsIdx(); 
+
+  // Do inference 
+  sps->inferRepFormat  ( vps , pcSlice->getLayerId(), false ); 
   sps->inferScalingList( parameterSetManager->getActiveSPS( sps->getSpsScalingListRefLayerId() ) );   
-  sps->inferSpsMaxDecPicBufferingMinus1( vps, targetOlsIdx, rpcSlice->getLayerId(), false ); 
+  sps->inferSpsMaxDecPicBufferingMinus1( vps, targetOlsIdx, pcSlice->getLayerId(), false ); 
+
+  if( sps->getMultiLayerExtSpsFlag() )
+  {
+    sps->setTemporalIdNestingFlag( (sps->getMaxTLayers() > 1) ? vps->getTemporalNestingFlag() : true );
+  }
 
   if ( sps->getVuiParametersPresentFlag() )
   {
-    sps->getVuiParameters()->inferVideoSignalInfo( vps, rpcSlice->getLayerId() ); 
-  }
-  rpcSlice->setVPS(vps);      
-  rpcSlice->setViewId   ( vps->getViewId   ( rpcSlice->getLayerId() )      );
-  rpcSlice->setViewIndex( vps->getViewIndex( rpcSlice->getLayerId() )      );  
-#if H_3D  
-  rpcSlice->setIsDepth  ( vps->getDepthId  ( rpcSlice->getLayerId() ) == 1 );
-#endif
-#endif
-  rpcSlice->setSPS(sps);
-  rpcSlice->setPPS(pps);
+    sps->getVuiParameters()->inferVideoSignalInfo( vps, pcSlice->getLayerId() ); 
+  }
+
+
+  pcSlice->setVPS(vps);      
+  pcSlice->setSPS(sps);
+  pcSlice->setPPS(pps);
+
+  pcSlice->setViewId   ( vps->getViewId   ( pcSlice->getLayerId() )      );
+  pcSlice->setViewIndex( vps->getViewIndex( pcSlice->getLayerId() )      );  
+#if NH_3D
+  pcSlice->setIsDepth  ( vps->getDepthId  ( pcSlice->getLayerId() ) == 1 );
+#endif
+#endif
+
+  const ChromaFormat chFmt = sps->getChromaFormatIdc();
+  const UInt numValidComp=getNumberValidComponents(chFmt);
+  const Bool bChroma=(chFmt!=CHROMA_400);
+
   if( pps->getDependentSliceSegmentsEnabledFlag() && ( !firstSliceSegmentInPic ))
   {
-    READ_FLAG( uiCode, "dependent_slice_segment_flag" );       rpcSlice->setDependentSliceSegmentFlag(uiCode ? true : false);
+    READ_FLAG( uiCode, "dependent_slice_segment_flag" );       pcSlice->setDependentSliceSegmentFlag(uiCode ? true : false);
   }
   else
   {
-    rpcSlice->setDependentSliceSegmentFlag(false);
+    pcSlice->setDependentSliceSegmentFlag(false);
   }
   Int numCTUs = ((sps->getPicWidthInLumaSamples()+sps->getMaxCUWidth()-1)/sps->getMaxCUWidth())*((sps->getPicHeightInLumaSamples()+sps->getMaxCUHeight()-1)/sps->getMaxCUHeight());
-  Int maxParts = (1<<(sps->getMaxCUDepth()<<1));
   UInt sliceSegmentAddress = 0;
   Int bitsSliceSegmentAddress = 0;
@@ -2009,97 +2239,82 @@
   }
   //set uiCode to equal slice start address (or dependent slice start address)
-  Int startCuAddress = maxParts*sliceSegmentAddress;
-  rpcSlice->setSliceSegmentCurStartCUAddr( startCuAddress );
-  rpcSlice->setSliceSegmentCurEndCUAddr(numCTUs*maxParts);
-
-  if (rpcSlice->getDependentSliceSegmentFlag())
-  {
-    rpcSlice->setNextSlice          ( false );
-    rpcSlice->setNextSliceSegment ( true  );
-  }
-  else
-  {
-    rpcSlice->setNextSlice          ( true  );
-    rpcSlice->setNextSliceSegment ( false );
-
-    rpcSlice->setSliceCurStartCUAddr(startCuAddress);
-    rpcSlice->setSliceCurEndCUAddr(numCTUs*maxParts);
-  }
-  
-#if H_MV
+  pcSlice->setSliceSegmentCurStartCtuTsAddr( sliceSegmentAddress );// this is actually a Raster-Scan (RS) address, but we do not have the RS->TS conversion table defined yet.
+  pcSlice->setSliceSegmentCurEndCtuTsAddr(numCTUs);                // Set end as the last CTU of the picture.
+
+#if NH_MV
     UInt slicePicOrderCntLsb = 0;
 #endif
 
-  if(!rpcSlice->getDependentSliceSegmentFlag())
-  {
-#if H_MV    
+
+  if (!pcSlice->getDependentSliceSegmentFlag())
+  {
+    pcSlice->setSliceCurStartCtuTsAddr(sliceSegmentAddress); // this is actually a Raster-Scan (RS) address, but we do not have the RS->TS conversion table defined yet.
+    pcSlice->setSliceCurEndCtuTsAddr(numCTUs);
+  }
+
+  if(!pcSlice->getDependentSliceSegmentFlag())
+  {
+#if NH_MV    
     Int esb = 0; //Don't use i, otherwise will shadow something below
 
-    if ( rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
+    if ( pps->getNumExtraSliceHeaderBits() > esb )
     {
       esb++; 
-      READ_FLAG( uiCode, "discardable_flag" ); rpcSlice->setDiscardableFlag( uiCode == 1 );
+      READ_FLAG( uiCode, "discardable_flag" ); pcSlice->setDiscardableFlag( uiCode == 1 );
       if ( uiCode == 1 )
       {
-        assert(rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
-          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
-          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
-          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
-          rpcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
-      }
-    }
-
-    if ( rpcSlice->getPPS()->getNumExtraSliceHeaderBits() > esb )
+        assert(pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TRAIL_R &&
+          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_TSA_R &&
+          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_STSA_R &&
+          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RADL_R &&
+          pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_RASL_R);
+      }
+    }
+
+    if ( pps->getNumExtraSliceHeaderBits() > esb )
     {
       esb++; 
-      READ_FLAG( uiCode, "cross_layer_bla_flag" ); rpcSlice->setCrossLayerBlaFlag( uiCode == 1 );
-    }
-    rpcSlice->checkCrossLayerBlaFlag( ); 
-
-
-    for (; esb < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); esb++)    
+      READ_FLAG( uiCode, "cross_layer_bla_flag" ); pcSlice->setCrossLayerBlaFlag( uiCode == 1 );
+    }
+    pcSlice->checkCrossLayerBlaFlag( ); 
+
+
+    for (; esb < pps->getNumExtraSliceHeaderBits(); esb++)    
 #else
-    for (Int i = 0; i < rpcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
-#endif     
-    {
-      READ_FLAG(uiCode, "slice_reserved_undetermined_flag[]"); // ignored
-    }
-
-    READ_UVLC (    uiCode, "slice_type" );            rpcSlice->setSliceType((SliceType)uiCode);
+    for (Int i = 0; i < pps->getNumExtraSliceHeaderBits(); i++)
+#endif
+    {
+      READ_FLAG(uiCode, "slice_reserved_flag[]"); // ignored
+    }
+
+    READ_UVLC (    uiCode, "slice_type" );            pcSlice->setSliceType((SliceType)uiCode);
     if( pps->getOutputFlagPresentFlag() )
     {
-      READ_FLAG( uiCode, "pic_output_flag" );    rpcSlice->setPicOutputFlag( uiCode ? true : false );
+      READ_FLAG( uiCode, "pic_output_flag" );    pcSlice->setPicOutputFlag( uiCode ? true : false );
     }
     else
     {
-      rpcSlice->setPicOutputFlag( true );
-    }
-    // in the first version chroma_format_idc is equal to one, thus colour_plane_id will not be present
-
-#if H_3D_DISABLE_CHROMA
-    assert (sps->getChromaFormatIdc() == 1 || rpcSlice->getIsDepth() );
-    assert (sps->getChromaFormatIdc() == 0 || !rpcSlice->getIsDepth() );
-#else
-    assert (sps->getChromaFormatIdc() == 1 );
-#endif
-    // if( separate_colour_plane_flag  ==  1 )
-    //   colour_plane_id                                      u(2)
-
-
-#if H_MV
+      pcSlice->setPicOutputFlag( true );
+    }
+
+    // if (separate_colour_plane_flag == 1)
+    //   read colour_plane_id
+    //   (separate_colour_plane_flag == 1) is not supported in this version of the standard.
+
+#if NH_MV
     Int iPOClsb = slicePicOrderCntLsb;  // Needed later
-    if ( (rpcSlice->getLayerId() > 0 && !vps->getPocLsbNotPresentFlag( rpcSlice->getLayerIdInVps())) || !rpcSlice->getIdrPicFlag() )
+    if ( (pcSlice->getLayerId() > 0 && !vps->getPocLsbNotPresentFlag( pcSlice->getLayerIdInVps())) || !pcSlice->getIdrPicFlag() )
     {
       READ_CODE(sps->getBitsForPOC(), slicePicOrderCntLsb, "slice_pic_order_cnt_lsb");        
     }    
-    rpcSlice->setSlicePicOrderCntLsb( slicePicOrderCntLsb ); 
+    pcSlice->setSlicePicOrderCntLsb( slicePicOrderCntLsb ); 
 
     Bool picOrderCntMSBZeroFlag = false;     
 
     // as in HM code. However are all cases for IRAP picture with NoRaslOutputFlag equal to 1 covered??
-    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ); 
-    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ); 
-    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( rpcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ); 
-    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag ||   rpcSlice->getIdrPicFlag(); 
+    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP   ); 
+    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL ); 
+    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP   ); 
+    picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag ||   pcSlice->getIdrPicFlag(); 
 
     // TBD picOrderCntMSBZeroFlag = picOrderCntMSBZeroFlag || ( rpcSlice->getLayerId() > 0 &&   !rpcSlice->getFirstPicInLayerDecodedFlag() ); 
@@ -2109,5 +2324,5 @@
     if ( !picOrderCntMSBZeroFlag )
     {
-      Int prevPicOrderCnt    = rpcSlice->getPrevTid0POC();
+      Int prevPicOrderCnt    = prevTid0POC;
       Int maxPicOrderCntLsb  = 1 << sps->getBitsForPOC();
       Int prevPicOrderCntLsb = prevPicOrderCnt & (maxPicOrderCntLsb - 1);
@@ -2128,33 +2343,30 @@
     }
       
-    rpcSlice->setPOC( picOrderCntMSB + slicePicOrderCntLsb );
-    if ( rpcSlice->getPocResetFlag() )  
-    {
-      rpcSlice->setPocBeforeReset   ( rpcSlice->getPOC() ); 
-      rpcSlice->setPOC              ( 0 );
+    pcSlice->setPOC( picOrderCntMSB + slicePicOrderCntLsb );
+    if ( pcSlice->getPocResetFlag() )  
+    {
+      pcSlice->setPocBeforeReset   ( pcSlice->getPOC() ); 
+      pcSlice->setPOC              ( 0 );
     }      
 #endif
 
-    if( rpcSlice->getIdrPicFlag() )
-    {
-#if !H_MV
-      rpcSlice->setPOC(0);
-#endif
-      TComReferencePictureSet* rps = rpcSlice->getLocalRPS();
-      rps->setNumberOfNegativePictures(0);
-      rps->setNumberOfPositivePictures(0);
-      rps->setNumberOfLongtermPictures(0);
-      rps->setNumberOfPictures(0);
-      rpcSlice->setRPS(rps);
-#if H_MV
-      rpcSlice->setEnableTMVPFlag(false);
+    if( pcSlice->getIdrPicFlag() )
+    {
+#if !NH_MV
+      pcSlice->setPOC(0);
+#endif
+      TComReferencePictureSet* rps = pcSlice->getLocalRPS();
+      (*rps)=TComReferencePictureSet();
+      pcSlice->setRPS(rps);
+#if NH_MV
+      pcSlice->setEnableTMVPFlag(false);
 #endif
     }
     else
     {
-#if !H_MV
-      READ_CODE(sps->getBitsForPOC(), uiCode, "pic_order_cnt_lsb");  
+#if !NH_MV
+      READ_CODE(sps->getBitsForPOC(), uiCode, "slice_pic_order_cnt_lsb");
       Int iPOClsb = uiCode;
-      Int iPrevPOC = rpcSlice->getPrevTid0POC();
+      Int iPrevPOC = prevTid0POC;
       Int iMaxPOClsb = 1<< sps->getBitsForPOC();
       Int iPrevPOClsb = iPrevPOC & (iMaxPOClsb - 1);
@@ -2165,5 +2377,5 @@
         iPOCmsb = iPrevPOCmsb + iMaxPOClsb;
       }
-      else if( (iPOClsb  >  iPrevPOClsb )  && ( (iPOClsb - iPrevPOClsb )  >  ( iMaxPOClsb / 2 ) ) ) 
+      else if( (iPOClsb  >  iPrevPOClsb )  && ( (iPOClsb - iPrevPOClsb )  >  ( iMaxPOClsb / 2 ) ) )
       {
         iPOCmsb = iPrevPOCmsb - iMaxPOClsb;
@@ -2173,21 +2385,23 @@
         iPOCmsb = iPrevPOCmsb;
       }
-      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 )
+      if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
+        || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
+        || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP )
       {
         // For BLA picture types, POCmsb is set to 0.
         iPOCmsb = 0;
       }
-      rpcSlice->setPOC              (iPOCmsb+iPOClsb);
+      pcSlice->setPOC              (iPOCmsb+iPOClsb);
 #endif
       TComReferencePictureSet* rps;
-      rps = rpcSlice->getLocalRPS();
-      rpcSlice->setRPS(rps);
+      rps = pcSlice->getLocalRPS();
+      (*rps)=TComReferencePictureSet();
+
+      pcSlice->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());
-#if H_MV
+#if NH_MV
         if ( !rps->getInterRPSPrediction( ) )
         { // check sum of num_positive_pics and num_negative_pics
@@ -2195,13 +2409,14 @@
             vps->getVpsExtensionFlag(), 
             MAX_INT,  // To be replaced by MaxDbpSize
-            rpcSlice->getLayerId(), 
+            pcSlice->getLayerId(), 
             sps->getMaxDecPicBuffering( sps->getSpsMaxSubLayersMinus1() ) - 1 );
         }
 #endif
+
       }
       else // use reference to short-term reference picture set in PPS
       {
         Int numBits = 0;
-        while ((1 << numBits) < rpcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
+        while ((1 << numBits) < sps->getRPSList()->getNumberOfReferencePictureSets())
         {
           numBits++;
@@ -2214,4 +2429,5 @@
         {
           uiCode = 0;
+       
         }
         *rps = *(sps->getRPSList()->getReferencePictureSet(uiCode));
@@ -2222,5 +2438,5 @@
         UInt numOfLtrp = 0;
         UInt numLtrpInSPS = 0;
-        if (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > 0)
+        if (sps->getNumLongTermRefPicSPS() > 0)
         {
           READ_UVLC( uiCode, "num_long_term_sps");
@@ -2230,5 +2446,5 @@
         }
         Int bitsForLtrpInSPS = 0;
-        while (rpcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
+        while (sps->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
         {
           bitsForLtrpInSPS++;
@@ -2237,6 +2453,6 @@
         numOfLtrp += uiCode;
         rps->setNumberOfLongtermPictures(numOfLtrp);
-        Int maxPicOrderCntLSB = 1 << rpcSlice->getSPS()->getBitsForPOC();
-        Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;;
+        Int maxPicOrderCntLSB = 1 << sps->getBitsForPOC();
+        Int prevDeltaMSB = 0, deltaPocMSBCycleLT = 0;
         for(Int j=offset+rps->getNumberOfLongtermPictures()-1, k = 0; k < numOfLtrp; j--, k++)
         {
@@ -2249,17 +2465,17 @@
               READ_CODE(bitsForLtrpInSPS, uiCode, "lt_idx_sps[i]");
             }
-            Int usedByCurrFromSPS=rpcSlice->getSPS()->getUsedByCurrPicLtSPSFlag(uiCode);
-
-            pocLsbLt = rpcSlice->getSPS()->getLtRefPicPocLsbSps(uiCode);
+            Bool usedByCurrFromSPS=sps->getUsedByCurrPicLtSPSFlag(uiCode);
+
+            pocLsbLt = sps->getLtRefPicPocLsbSps(uiCode);
             rps->setUsed(j,usedByCurrFromSPS);
           }
           else
           {
-            READ_CODE(rpcSlice->getSPS()->getBitsForPOC(), uiCode, "poc_lsb_lt"); pocLsbLt= uiCode;
+            READ_CODE(sps->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)                  
+          if(mSBPresentFlag)
           {
             READ_UVLC( uiCode, "delta_poc_msb_cycle_lt[i]" );
@@ -2276,19 +2492,19 @@
             else
             {
-              deltaPocMSBCycleLT = uiCode + prevDeltaMSB;              
+              deltaPocMSBCycleLT = uiCode + prevDeltaMSB;
             }
 
-            Int pocLTCurr = rpcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB 
+            Int pocLTCurr = pcSlice->getPOC() - deltaPocMSBCycleLT * maxPicOrderCntLSB
                                         - iPOClsb + pocLsbLt;
-            rps->setPOC     (j, pocLTCurr); 
-            rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLTCurr);
-            rps->setCheckLTMSBPresent(j,true);  
+            rps->setPOC     (j, pocLTCurr);
+            rps->setDeltaPOC(j, - pcSlice->getPOC() + pocLTCurr);
+            rps->setCheckLTMSBPresent(j,true);
           }
           else
           {
             rps->setPOC     (j, pocLsbLt);
-            rps->setDeltaPOC(j, - rpcSlice->getPOC() + pocLsbLt);
-            rps->setCheckLTMSBPresent(j,false);  
-            
+            rps->setDeltaPOC(j, - pcSlice->getPOC() + pocLsbLt);
+            rps->setCheckLTMSBPresent(j,false);
+
             // reset deltaPocMSBCycleLT for first LTRP from slice header if MSB not present
             if( j == offset+(numOfLtrp-numLtrpInSPS)-1 )
@@ -2300,7 +2516,7 @@
         }
         offset += rps->getNumberOfLongtermPictures();
-        rps->setNumberOfPictures(offset);        
-      }  
-#if H_MV
+        rps->setNumberOfPictures(offset);
+      }
+#if NH_MV
       if ( !rps->getInterRPSPrediction( ) )
       { // check sum of NumPositivePics, NumNegativePics, num_long_term_sps and num_long_term_pics 
@@ -2308,64 +2524,62 @@
           vps->getVpsExtensionFlag(), 
             MAX_INT,  // To be replaced by MaxDbpsize
-          rpcSlice->getLayerId(), 
+          pcSlice->getLayerId(), 
           sps->getMaxDecPicBuffering( sps->getSpsMaxSubLayersMinus1() ) - 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 )
+
+      if ( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_LP
+        || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_W_RADL
+        || pcSlice->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())
-      {
-#if H_MV
+        rps = pcSlice->getLocalRPS();
+        (*rps)=TComReferencePictureSet();
+        pcSlice->setRPS(rps);
+      }
+      if (sps->getTMVPFlagsPresent())
+      {
+#if NH_MV
         READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" );
 #else
-        READ_FLAG( uiCode, "slice_temporal_mvp_enable_flag" );
-#endif
-        rpcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false ); 
+        READ_FLAG( uiCode, "slice_temporal_mvp_enabled_flag" );
+#endif
+        pcSlice->setEnableTMVPFlag( uiCode == 1 ? true : false );
       }
       else
       {
-        rpcSlice->setEnableTMVPFlag(false);
-      }
-    }
-#if H_MV
+        pcSlice->setEnableTMVPFlag(false);
+      }
+    }
+#if NH_MV
     Bool interLayerPredLayerIdcPresentFlag = false; 
-    Int layerId       = rpcSlice->getLayerId(); 
-#if H_3D
-    if( rpcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumRefListLayers( layerId ) > 0 )
+    Int layerId       = pcSlice->getLayerId(); 
+#if NH_3D
+    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumRefListLayers( layerId ) > 0 )
 #else
-    if( rpcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumDirectRefLayers( layerId ) > 0 )
+    if( pcSlice->getLayerId() > 0 && !vps->getAllRefLayersActiveFlag() && vps->getNumDirectRefLayers( layerId ) > 0 )
 #endif
     {   
-      READ_FLAG( uiCode, "inter_layer_pred_enabled_flag" ); rpcSlice->setInterLayerPredEnabledFlag( uiCode == 1 );
-#if H_3D
-      if( rpcSlice->getInterLayerPredEnabledFlag() && vps->getNumRefListLayers( layerId ) > 1 )
+      READ_FLAG( uiCode, "inter_layer_pred_enabled_flag" ); pcSlice->setInterLayerPredEnabledFlag( uiCode == 1 );
+#if NH_3D
+      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumRefListLayers( layerId ) > 1 )
 #else
-      if( rpcSlice->getInterLayerPredEnabledFlag() && vps->getNumDirectRefLayers( layerId ) > 1 )
+      if( pcSlice->getInterLayerPredEnabledFlag() && vps->getNumDirectRefLayers( layerId ) > 1 )
 #endif
       {            
         if( !vps->getMaxOneActiveRefLayerFlag())  
         {
-          READ_CODE( rpcSlice->getNumInterLayerRefPicsMinus1Len( ), uiCode, "num_inter_layer_ref_pics_minus1" ); rpcSlice->setNumInterLayerRefPicsMinus1( uiCode );
-        }
-#if H_3D
-        if ( rpcSlice->getNumActiveRefLayerPics() != vps->getNumRefListLayers( layerId ) )
+          READ_CODE( pcSlice->getNumInterLayerRefPicsMinus1Len( ), uiCode, "num_inter_layer_ref_pics_minus1" ); pcSlice->setNumInterLayerRefPicsMinus1( uiCode );
+        }
+#if NH_3D
+        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumRefListLayers( layerId ) )
 #else
-        if ( rpcSlice->getNumActiveRefLayerPics() != vps->getNumDirectRefLayers( layerId ) )
+        if ( pcSlice->getNumActiveRefLayerPics() != vps->getNumDirectRefLayers( layerId ) )
 #endif
         {
           interLayerPredLayerIdcPresentFlag = true; 
-          for( Int idx = 0; idx < rpcSlice->getNumActiveRefLayerPics(); idx++ )   
+          for( Int idx = 0; idx < pcSlice->getNumActiveRefLayerPics(); idx++ )   
           {
-            READ_CODE( rpcSlice->getInterLayerPredLayerIdcLen( ), uiCode, "inter_layer_pred_layer_idc" ); rpcSlice->setInterLayerPredLayerIdc( idx, uiCode );
+            READ_CODE( pcSlice->getInterLayerPredLayerIdcLen( ), uiCode, "inter_layer_pred_layer_idc" ); pcSlice->setInterLayerPredLayerIdc( idx, uiCode );
           }
         }
@@ -2374,18 +2588,18 @@
     if ( !interLayerPredLayerIdcPresentFlag )
     {
-      for( Int i = 0; i < rpcSlice->getNumActiveRefLayerPics(); i++ )   
-      {
-        rpcSlice->setInterLayerPredLayerIdc( i, rpcSlice->getRefLayerPicIdc( i ) );
-      }
-    }
-#if H_3D
+      for( Int i = 0; i < pcSlice->getNumActiveRefLayerPics(); i++ )   
+      {
+        pcSlice->setInterLayerPredLayerIdc( i, pcSlice->getRefLayerPicIdc( i ) );
+      }
+    }
+#if NH_3D
     if ( getDecTop()->decProcAnnexI() )
     {    
-      rpcSlice->deriveInCmpPredAndCpAvailFlag();
-      if ( rpcSlice->getInCmpPredAvailFlag() )
-      {
-        READ_FLAG(uiCode, "in_comp_pred_flag");  rpcSlice->setInCompPredFlag((Bool)uiCode);      
-      }
-      rpcSlice->init3dToolParameters(); 
+      pcSlice->deriveInCmpPredAndCpAvailFlag( );
+      if ( pcSlice->getInCmpPredAvailFlag() )
+      {
+        READ_FLAG(uiCode, "in_comp_pred_flag");  pcSlice->setInCompPredFlag((Bool)uiCode);      
+      }
+      pcSlice->init3dToolParameters(); 
     }
 #endif
@@ -2393,24 +2607,17 @@
     if(sps->getUseSAO())
     {
-      READ_FLAG(uiCode, "slice_sao_luma_flag");  rpcSlice->setSaoEnabledFlag((Bool)uiCode);
-#if H_3D_DISABLE_CHROMA
-      if( rpcSlice->getSPS()->getChromaFormatIdc() != 0 )
-      {
-        READ_FLAG(uiCode, "slice_sao_chroma_flag");  rpcSlice->setSaoEnabledFlagChroma((Bool)uiCode);
-      }
-      else
-      {
-        rpcSlice->setSaoEnabledFlagChroma( false );
-      }
-#else
-      READ_FLAG(uiCode, "slice_sao_chroma_flag");  rpcSlice->setSaoEnabledFlagChroma((Bool)uiCode);
-#endif
-    }
-
-    if (rpcSlice->getIdrPicFlag())
-    {
-      rpcSlice->setEnableTMVPFlag(false);
-    }
-    if (!rpcSlice->isIntra())
+      READ_FLAG(uiCode, "slice_sao_luma_flag");  pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_LUMA, (Bool)uiCode);
+
+      if (bChroma)
+      {
+        READ_FLAG(uiCode, "slice_sao_chroma_flag");  pcSlice->setSaoEnabledFlag(CHANNEL_TYPE_CHROMA, (Bool)uiCode);
+      }
+    }
+
+    if (pcSlice->getIdrPicFlag())
+    {
+      pcSlice->setEnableTMVPFlag(false);
+    }
+    if (!pcSlice->isIntra())
     {
 
@@ -2418,32 +2625,32 @@
       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 );
+        READ_UVLC (uiCode, "num_ref_idx_l0_active_minus1" );  pcSlice->setNumRefIdx( REF_PIC_LIST_0, uiCode + 1 );
+        if (pcSlice->isInterB())
+        {
+          READ_UVLC (uiCode, "num_ref_idx_l1_active_minus1" );  pcSlice->setNumRefIdx( REF_PIC_LIST_1, uiCode + 1 );
         }
         else
         {
-          rpcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
+          pcSlice->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());
+        pcSlice->setNumRefIdx(REF_PIC_LIST_0, pps->getNumRefIdxL0DefaultActive());
+        if (pcSlice->isInterB())
+        {
+          pcSlice->setNumRefIdx(REF_PIC_LIST_1, pps->getNumRefIdxL1DefaultActive());
         }
         else
         {
-          rpcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
+          pcSlice->setNumRefIdx(REF_PIC_LIST_1,0);
         }
       }
     }
     // }
-    TComRefPicListModification* refPicListModification = rpcSlice->getRefPicListModification();
-    if(!rpcSlice->isIntra())
-    {
-      if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
+    TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
+    if(!pcSlice->isIntra())
+    {
+      if( !pps->getListsModificationPresentFlag() || pcSlice->getNumRpsCurrTempList() <= 1 )
       {
         refPicListModification->setRefPicListModificationFlagL0( 0 );
@@ -2455,17 +2662,17 @@
 
       if(refPicListModification->getRefPicListModificationFlagL0())
-      { 
+      {
         uiCode = 0;
         Int i = 0;
-        Int numRpsCurrTempList0 = rpcSlice->getNumRpsCurrTempList();
+        Int numRpsCurrTempList0 = pcSlice->getNumRpsCurrTempList();
         if ( numRpsCurrTempList0 > 1 )
         {
           Int length = 1;
           numRpsCurrTempList0 --;
-          while ( numRpsCurrTempList0 >>= 1) 
+          while ( numRpsCurrTempList0 >>= 1)
           {
             length ++;
           }
-          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
+          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
           {
             READ_CODE( length, uiCode, "list_entry_l0" );
@@ -2475,5 +2682,5 @@
         else
         {
-          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
+          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_0); i ++)
           {
             refPicListModification->setRefPicSetIdxL0(i, 0 );
@@ -2486,7 +2693,7 @@
       refPicListModification->setRefPicListModificationFlagL0(0);
     }
-    if(rpcSlice->isInterB())
-    {
-      if( !rpcSlice->getPPS()->getListsModificationPresentFlag() || rpcSlice->getNumRpsCurrTempList() <= 1 )
+    if(pcSlice->isInterB())
+    {
+      if( !pps->getListsModificationPresentFlag() || pcSlice->getNumRpsCurrTempList() <= 1 )
       {
         refPicListModification->setRefPicListModificationFlagL1( 0 );
@@ -2500,14 +2707,14 @@
         uiCode = 0;
         Int i = 0;
-        Int numRpsCurrTempList1 = rpcSlice->getNumRpsCurrTempList();
+        Int numRpsCurrTempList1 = pcSlice->getNumRpsCurrTempList();
         if ( numRpsCurrTempList1 > 1 )
         {
           Int length = 1;
           numRpsCurrTempList1 --;
-          while ( numRpsCurrTempList1 >>= 1) 
+          while ( numRpsCurrTempList1 >>= 1)
           {
             length ++;
           }
-          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
+          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
           {
             READ_CODE( length, uiCode, "list_entry_l1" );
@@ -2517,5 +2724,5 @@
         else
         {
-          for (i = 0; i < rpcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
+          for (i = 0; i < pcSlice->getNumRefIdx(REF_PIC_LIST_1); i ++)
           {
             refPicListModification->setRefPicSetIdxL1(i, 0 );
@@ -2523,53 +2730,54 @@
         }
       }
-    }  
+    }
     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())
+    if (pcSlice->isInterB())
+    {
+      READ_FLAG( uiCode, "mvd_l1_zero_flag" );       pcSlice->setMvdL1ZeroFlag( (uiCode ? true : false) );
+    }
+
+    pcSlice->setCabacInitFlag( false ); // default
+    if(pps->getCabacInitPresentFlag() && !pcSlice->isIntra())
     {
       READ_FLAG(uiCode, "cabac_init_flag");
-      rpcSlice->setCabacInitFlag( uiCode ? true : false );
-    }
-
-    if ( rpcSlice->getEnableTMVPFlag() )
-    {
-      if ( rpcSlice->getSliceType() == B_SLICE )
+      pcSlice->setCabacInitFlag( uiCode ? true : false );
+    }
+
+    if ( pcSlice->getEnableTMVPFlag() )
+    {
+      if ( pcSlice->getSliceType() == B_SLICE )
       {
         READ_FLAG( uiCode, "collocated_from_l0_flag" );
-        rpcSlice->setColFromL0Flag(uiCode);
+        pcSlice->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)))
+        pcSlice->setColFromL0Flag( 1 );
+      }
+
+      if ( pcSlice->getSliceType() != I_SLICE &&
+          ((pcSlice->getColFromL0Flag() == 1 && pcSlice->getNumRefIdx(REF_PIC_LIST_0) > 1)||
+           (pcSlice->getColFromL0Flag() == 0 && pcSlice->getNumRefIdx(REF_PIC_LIST_1) > 1)))
       {
         READ_UVLC( uiCode, "collocated_ref_idx" );
-        rpcSlice->setColRefIdx(uiCode);
+        pcSlice->setColRefIdx(uiCode);
       }
       else
       {
-        rpcSlice->setColRefIdx(0);
-      }
-    }
-    if ( (pps->getUseWP() && rpcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && rpcSlice->getSliceType()==B_SLICE) )
-    {
-      xParsePredWeightTable(rpcSlice);
-      rpcSlice->initWpScaling();
-    }
-#if H_3D_IC
-    else if(    rpcSlice->getViewIndex() && ( rpcSlice->getSliceType() == P_SLICE || rpcSlice->getSliceType() == B_SLICE ) 
-             && !rpcSlice->getIsDepth() && vps->getNumRefListLayers( layerId ) > 0 
+        pcSlice->setColRefIdx(0);
+      }
+    }
+    if ( (pps->getUseWP() && pcSlice->getSliceType()==P_SLICE) || (pps->getWPBiPred() && pcSlice->getSliceType()==B_SLICE) )
+    {
+      xParsePredWeightTable(pcSlice, sps);
+      pcSlice->initWpScaling(sps);
+    }
+
+#if NH_3D_IC
+    else if(    pcSlice->getViewIndex() && ( pcSlice->getSliceType() == P_SLICE || pcSlice->getSliceType() == B_SLICE ) 
+             && !pcSlice->getIsDepth() && vps->getNumRefListLayers( layerId ) > 0 
              && getDecTop()->decProcAnnexI()
            )
@@ -2578,87 +2786,103 @@
 
       READ_FLAG ( uiCodeTmp, "slice_ic_enable_flag" );
-      rpcSlice->setApplyIC( uiCodeTmp );
+      pcSlice->setApplyIC( uiCodeTmp );
 
       if ( uiCodeTmp )
       {
-        READ_FLAG ( uiCodeTmp, "ic_skip_mergeidx0" );
-        rpcSlice->setIcSkipParseFlag( uiCodeTmp );
-      }
-    }
-#endif
-    if (!rpcSlice->isIntra())
+        READ_FLAG ( uiCodeTmp, "slice_ic_disabled_merge_zero_idx_flag" );
+        pcSlice->setIcSkipParseFlag( uiCodeTmp );
+      }
+    }
+#endif
+
+    if (!pcSlice->isIntra())
     {
       READ_UVLC( uiCode, "five_minus_max_num_merge_cand");
-#if H_3D_IV_MERGE
-      rpcSlice->setMaxNumMergeCand(( ( rpcSlice->getMpiFlag() || rpcSlice->getIvMvPredFlag() || rpcSlice->getViewSynthesisPredFlag() ) ? MRG_MAX_NUM_CANDS_MEM : MRG_MAX_NUM_CANDS) - uiCode);
+#if NH_3D_IV_MERGE
+      pcSlice->setMaxNumMergeCand(( ( pcSlice->getMpiFlag() || pcSlice->getIvMvPredFlag() || pcSlice->getViewSynthesisPredFlag() ) ? MRG_MAX_NUM_CANDS_MEM : MRG_MAX_NUM_CANDS) - uiCode);
 #else
-      rpcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
+      pcSlice->setMaxNumMergeCand(MRG_MAX_NUM_CANDS - uiCode);
 #endif
     }
 
     READ_SVLC( iCode, "slice_qp_delta" );
-    rpcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
-
-    assert( rpcSlice->getSliceQp() >= -sps->getQpBDOffsetY() );
-    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);
+    pcSlice->setSliceQp (26 + pps->getPicInitQPMinus26() + iCode);
+
+    assert( pcSlice->getSliceQp() >= -sps->getQpBDOffset(CHANNEL_TYPE_LUMA) );
+    assert( pcSlice->getSliceQp() <=  51 );
+
+    if (pps->getSliceChromaQpFlag())
+    {
+      if (numValidComp>COMPONENT_Cb)
+      {
+        READ_SVLC( iCode, "slice_cb_qp_offset" );
+        pcSlice->setSliceChromaQpDelta(COMPONENT_Cb, iCode );
+        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb) >= -12 );
+        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cb) <=  12 );
+        assert( (pps->getQpOffset(COMPONENT_Cb) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cb)) >= -12 );
+        assert( (pps->getQpOffset(COMPONENT_Cb) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cb)) <=  12 );
+      }
+
+      if (numValidComp>COMPONENT_Cr)
+      {
+        READ_SVLC( iCode, "slice_cr_qp_offset" );
+        pcSlice->setSliceChromaQpDelta(COMPONENT_Cr, iCode );
+        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr) >= -12 );
+        assert( pcSlice->getSliceChromaQpDelta(COMPONENT_Cr) <=  12 );
+        assert( (pps->getQpOffset(COMPONENT_Cr) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cr)) >= -12 );
+        assert( (pps->getQpOffset(COMPONENT_Cr) + pcSlice->getSliceChromaQpDelta(COMPONENT_Cr)) <=  12 );
+      }
+    }
+
+    if (pps->getPpsRangeExtension().getChromaQpOffsetListEnabledFlag())
+    {
+      READ_FLAG(uiCode, "cu_chroma_qp_offset_enabled_flag"); pcSlice->setUseChromaQpAdj(uiCode != 0);
+    }
+    else
+    {
+      pcSlice->setUseChromaQpAdj(false);
+    }
+
+    if (pps->getDeblockingFilterControlPresentFlag())
+    {
+      if(pps->getDeblockingFilterOverrideEnabledFlag())
+      {
+        READ_FLAG ( uiCode, "deblocking_filter_override_flag" );        pcSlice->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);
+      {
+        pcSlice->setDeblockingFilterOverrideFlag(0);
+      }
+      if(pcSlice->getDeblockingFilterOverrideFlag())
+      {
+        READ_FLAG ( uiCode, "slice_disable_deblocking_filter_flag" );   pcSlice->setDeblockingFilterDisable(uiCode ? 1 : 0);
+        if(!pcSlice->getDeblockingFilterDisable())
+        {
+          READ_SVLC( iCode, "slice_beta_offset_div2" );                       pcSlice->setDeblockingFilterBetaOffsetDiv2(iCode);
+          assert(pcSlice->getDeblockingFilterBetaOffsetDiv2() >= -6 &&
+                 pcSlice->getDeblockingFilterBetaOffsetDiv2() <=  6);
+          READ_SVLC( iCode, "slice_tc_offset_div2" );                         pcSlice->setDeblockingFilterTcOffsetDiv2(iCode);
+          assert(pcSlice->getDeblockingFilterTcOffsetDiv2() >= -6 &&
+                 pcSlice->getDeblockingFilterTcOffsetDiv2() <=  6);
         }
       }
       else
       {
-        rpcSlice->setDeblockingFilterDisable   ( rpcSlice->getPPS()->getPicDisableDeblockingFilterFlag() );
-        rpcSlice->setDeblockingFilterBetaOffsetDiv2( rpcSlice->getPPS()->getDeblockingFilterBetaOffsetDiv2() );
-        rpcSlice->setDeblockingFilterTcOffsetDiv2  ( rpcSlice->getPPS()->getDeblockingFilterTcOffsetDiv2() );
+        pcSlice->setDeblockingFilterDisable   ( pps->getPicDisableDeblockingFilterFlag() );
+        pcSlice->setDeblockingFilterBetaOffsetDiv2( pps->getDeblockingFilterBetaOffsetDiv2() );
+        pcSlice->setDeblockingFilterTcOffsetDiv2  ( pps->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 ))
+    {
+      pcSlice->setDeblockingFilterDisable       ( false );
+      pcSlice->setDeblockingFilterBetaOffsetDiv2( 0 );
+      pcSlice->setDeblockingFilterTcOffsetDiv2  ( 0 );
+    }
+
+    Bool isSAOEnabled = sps->getUseSAO() && (pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_LUMA) || (bChroma && pcSlice->getSaoEnabledFlag(CHANNEL_TYPE_CHROMA)));
+    Bool isDBFEnabled = (!pcSlice->getDeblockingFilterDisable());
+
+    if(pps->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
     {
       READ_FLAG( uiCode, "slice_loop_filter_across_slices_enabled_flag");
@@ -2666,125 +2890,133 @@
     else
     {
-      uiCode = rpcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
-    }
-    rpcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
-
-#if H_3D
+      uiCode = pps->getLoopFilterAcrossSlicesEnabledFlag()?1:0;
+    }
+    pcSlice->setLFCrossSliceBoundaryFlag( (uiCode==1)?true:false);
+
+#if NH_3D
     if ( getDecTop()->decProcAnnexI() )
     {
-      Int voiInVps = vps->getVoiInVps( rpcSlice->getViewIndex() ); 
-      if( vps->getCpInSliceSegmentHeaderFlag( voiInVps ) && !rpcSlice->getIsDepth() )
+      Int voiInVps = vps->getVoiInVps( pcSlice->getViewIndex() ); 
+#if NH_3D_FIX_TICKET_101
+      if( vps->getCpInSliceSegmentHeaderFlag( voiInVps ) )
+#else
+      if( vps->getCpInSliceSegmentHeaderFlag( voiInVps ) && !pcSlice->getIsDepth() )
+#endif
       {
         for( Int m = 0; m < vps->getNumCp( voiInVps ); m++ )
         {
           Int jInVps = vps->getVoiInVps( vps->getCpRefVoi( voiInVps, m ));
-          READ_SVLC( iCode, "cp_scale" );                rpcSlice->setCpScale   ( jInVps, iCode );
-          READ_SVLC( iCode, "cp_off" );                  rpcSlice->setCpOff     ( jInVps, iCode );
-          READ_SVLC( iCode, "cp_inv_scale_plus_scale" ); rpcSlice->setCpInvScale( jInVps, iCode - rpcSlice->getCpScale   ( jInVps ));
-          READ_SVLC( iCode, "cp_inv_off_plus_off" );     rpcSlice->setCpInvOff  ( jInVps, iCode - rpcSlice->getCpOff     ( jInVps ));
-        }
-      }
-    }
-#endif
-  }
-  
-    UInt *entryPointOffset          = NULL;
-    UInt numEntryPointOffsets, offsetLenMinus1;
+          READ_SVLC( iCode, "cp_scale" );                pcSlice->setCpScale   ( jInVps, iCode );
+          READ_SVLC( iCode, "cp_off" );                  pcSlice->setCpOff     ( jInVps, iCode );
+          READ_SVLC( iCode, "cp_inv_scale_plus_scale" ); pcSlice->setCpInvScale( jInVps, iCode - pcSlice->getCpScale   ( jInVps ));
+          READ_SVLC( iCode, "cp_inv_off_plus_off" );     pcSlice->setCpInvOff  ( jInVps, iCode - pcSlice->getCpOff     ( jInVps ));
+        }
+      }
+    }
+#endif
+
+  }
+
+  std::vector<UInt> entryPointOffset;
   if( pps->getTilesEnabledFlag() || pps->getEntropyCodingSyncEnabledFlag() )
   {
-    READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets"); rpcSlice->setNumEntryPointOffsets ( numEntryPointOffsets );
+    UInt numEntryPointOffsets;
+    UInt offsetLenMinus1;
+    READ_UVLC(numEntryPointOffsets, "num_entry_point_offsets");
     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 );
-  }
-
+      entryPointOffset.resize(numEntryPointOffsets);
+      for (UInt idx=0; idx<numEntryPointOffsets; idx++)
+      {
+        READ_CODE(offsetLenMinus1+1, uiCode, "entry_point_offset_minus1");
+        entryPointOffset[ idx ] = uiCode + 1;
+      }
+    }
+  }
 
   if(pps->getSliceHeaderExtensionPresentFlag())
   {
-#if H_MV
-    READ_UVLC( uiCode, "slice_segment_header_extension_length" ); rpcSlice->setSliceSegmentHeaderExtensionLength( uiCode );
+#if NH_MV
+    READ_UVLC( uiCode, "slice_segment_header_extension_length" ); pcSlice->setSliceSegmentHeaderExtensionLength( uiCode );
     UInt posFollSliceSegHeaderExtLen = m_pcBitstream->getNumBitsRead();
 
-    if( rpcSlice->getPPS()->getPocResetInfoPresentFlag() )
-    {
-      READ_CODE( 2, uiCode, "poc_reset_idc" ); rpcSlice->setPocResetIdc( uiCode );
+    if( pps->getPocResetInfoPresentFlag() )
+    {
+      READ_CODE( 2, uiCode, "poc_reset_idc" ); pcSlice->setPocResetIdc( uiCode );
     }
     else
     {
-      rpcSlice->setPocResetIdc( 0 );
-    }
-    rpcSlice->checkPocResetIdc(); 
-
-    if ( rpcSlice->getVPS()->getPocLsbNotPresentFlag(rpcSlice->getLayerId()) && slicePicOrderCntLsb > 0 )
-    {
-      assert( rpcSlice->getPocResetIdc() != 2 );
-    }
-
-    if( rpcSlice->getPocResetIdc() !=  0 )
-    {
-      READ_CODE( 6, uiCode, "poc_reset_period_id" ); rpcSlice->setPocResetPeriodId( uiCode );
+      pcSlice->setPocResetIdc( 0 );
+    }
+    pcSlice->checkPocResetIdc(); 
+
+    if ( pcSlice->getVPS()->getPocLsbNotPresentFlag(pcSlice->getLayerId()) && slicePicOrderCntLsb > 0 )
+    {
+      assert( pcSlice->getPocResetIdc() != 2 );
+    }
+
+    if( pcSlice->getPocResetIdc() !=  0 )
+    {
+      READ_CODE( 6, uiCode, "poc_reset_period_id" ); pcSlice->setPocResetPeriodId( uiCode );
     }
     else
     {
       // TODO Copy poc_reset_period from earlier picture
-      rpcSlice->setPocResetPeriodId( 0 );
+      pcSlice->setPocResetPeriodId( 0 );
     }
     
-    if( rpcSlice->getPocResetIdc() ==  3 ) 
-    {
-      READ_FLAG( uiCode, "full_poc_reset_flag" ); rpcSlice->setFullPocResetFlag( uiCode == 1 );
-      READ_CODE( rpcSlice->getPocLsbValLen() , uiCode, "poc_lsb_val" ); rpcSlice->setPocLsbVal( uiCode );
+    if( pcSlice->getPocResetIdc() ==  3 ) 
+    {
+      READ_FLAG( uiCode, "full_poc_reset_flag" ); pcSlice->setFullPocResetFlag( uiCode == 1 );
+      READ_CODE( pcSlice->getPocLsbValLen() , uiCode, "poc_lsb_val" ); pcSlice->setPocLsbVal( uiCode );
     }          
-    rpcSlice->checkPocLsbVal(); 
+    pcSlice->checkPocLsbVal(); 
 
     // Derive the value of PocMs8bValRequiredFlag
 
-    if( !rpcSlice->getPocMsbValRequiredFlag() && rpcSlice->getVPS()->getVpsPocLsbAlignedFlag() )
-    {
-      READ_FLAG( uiCode, "poc_msb_val_present_flag" ); rpcSlice->setPocMsbValPresentFlag( uiCode == 1 );
+    if( !pcSlice->getPocMsbValRequiredFlag() && pcSlice->getVPS()->getVpsPocLsbAlignedFlag() )
+    {
+      READ_FLAG( uiCode, "poc_msb_val_present_flag" ); pcSlice->setPocMsbValPresentFlag( uiCode == 1 );
     }
     else
     {
-      rpcSlice->setPocMsbValPresentFlag( rpcSlice->inferPocMsbValPresentFlag( ) ); 
+      pcSlice->setPocMsbValPresentFlag( pcSlice->inferPocMsbValPresentFlag( ) ); 
     }
 
     
-    if( rpcSlice->getPocMsbValPresentFlag() )
-    {
-      READ_UVLC( uiCode, "poc_msb_val" ); rpcSlice->setPocMsbVal( uiCode );
-    }
-
-    while( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) < rpcSlice->getSliceSegmentHeaderExtensionLength() * 8 )
+    if( pcSlice->getPocMsbValPresentFlag() )
+    {
+      READ_UVLC( uiCode, "poc_msb_val" ); pcSlice->setPocMsbVal( uiCode );
+    }
+
+    while( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) < pcSlice->getSliceSegmentHeaderExtensionLength() * 8 )
     {
      READ_FLAG( uiCode, "slice_segment_header_extension_data_bit" );
     }
-    assert( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) == rpcSlice->getSliceSegmentHeaderExtensionLength() * 8  ); 
+    assert( ( m_pcBitstream->getNumBitsRead() - posFollSliceSegHeaderExtLen ) == pcSlice->getSliceSegmentHeaderExtensionLength() * 8  ); 
   }
 #else
-    READ_UVLC( uiCode, "slice_header_extension_length" );
+
+    READ_UVLC(uiCode,"slice_segment_header_extension_length");
     for(Int i=0; i<uiCode; i++)
     {
       UInt ignore;
-      READ_CODE(8,ignore,"slice_header_extension_data_byte");
-    }  
-  }
-#endif
+      READ_CODE(8,ignore,"slice_segment_header_extension_data_byte");
+    }
+  }
+#endif
+#if RExt__DECODER_DEBUG_BIT_STATISTICS
+  TComCodingStatistics::IncrementStatisticEP(STATS__BYTE_ALIGNMENT_BITS,m_pcBitstream->readByteAlignment(),0);
+#else
   m_pcBitstream->readByteAlignment();
+#endif
+
+  pcSlice->clearSubstreamSizes();
 
   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++ )
@@ -2798,5 +3030,5 @@
     Int  curEntryPointOffset     = 0;
     Int  prevEntryPointOffset    = 0;
-    for (UInt idx=0; idx<numEntryPointOffsets; idx++)
+    for (UInt idx=0; idx<entryPointOffset.size(); idx++)
     {
       curEntryPointOffset += entryPointOffset[ idx ];
@@ -2805,5 +3037,5 @@
       for ( UInt curByteIdx  = 0; curByteIdx<m_pcBitstream->numEmulationPreventionBytesRead(); curByteIdx++ )
       {
-        if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) && 
+        if ( m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) >= ( prevEntryPointOffset + endOfSliceHeaderLocation ) &&
              m_pcBitstream->getEmulationPreventionByteLocation( curByteIdx ) <  ( curEntryPointOffset  + endOfSliceHeaderLocation ) )
         {
@@ -2814,38 +3046,5 @@
       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;
+      pcSlice->addSubstreamSize(entryPointOffset [ idx ] );
     }
   }
@@ -2853,5 +3052,5 @@
   return;
 }
-  
+
 Void TDecCavlc::parsePTL( TComPTL *rpcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1 )
 {
@@ -2859,24 +3058,18 @@
   if(profilePresentFlag)
   {
-    parseProfileTier(rpcPTL->getGeneralPTL());
-  }
-  READ_CODE( 8, uiCode, "general_level_idc" );    rpcPTL->getGeneralPTL()->setLevelIdc(uiCode);
+    parseProfileTier(rpcPTL->getGeneralPTL(), false);
+  }
+  READ_CODE( 8, uiCode, "general_level_idc" );    rpcPTL->getGeneralPTL()->setLevelIdc(Level::Name(uiCode));
 
   for (Int i = 0; i < maxNumSubLayersMinus1; i++)
   {
-#if !H_MV
-    if(profilePresentFlag)
-    {
-#endif
-      READ_FLAG( uiCode, "sub_layer_profile_present_flag[i]" ); rpcPTL->setSubLayerProfilePresentFlag(i, uiCode);
-#if H_MV
-      // When profilePresentFlag is equal to 0, sub_layer_profile_present_flag[ i ] shall be equal to 0.
-      assert( profilePresentFlag || !rpcPTL->getSubLayerProfilePresentFlag(i) );
-#else
-    }
-#endif
+    READ_FLAG( uiCode, "sub_layer_profile_present_flag[i]" ); rpcPTL->setSubLayerProfilePresentFlag(i, uiCode);
     READ_FLAG( uiCode, "sub_layer_level_present_flag[i]"   ); rpcPTL->setSubLayerLevelPresentFlag  (i, uiCode);
-  }
-  
+#if NH_MV
+    // When profilePresentFlag is equal to 0, sub_layer_profile_present_flag[ i ] shall be equal to 0.
+    assert( profilePresentFlag || !rpcPTL->getSubLayerProfilePresentFlag(i) );
+#endif
+  }
+
   if (maxNumSubLayersMinus1 > 0)
   {
@@ -2887,45 +3080,42 @@
     }
   }
-  
+
   for(Int i = 0; i < maxNumSubLayersMinus1; i++)
   {
-#if H_MV
     if( rpcPTL->getSubLayerProfilePresentFlag(i) )         
+    {
+      parseProfileTier(rpcPTL->getSubLayerPTL(i), true);
+    }
+    if(rpcPTL->getSubLayerLevelPresentFlag(i))
+    {
+      READ_CODE( 8, uiCode, "sub_layer_level_idc[i]" );   rpcPTL->getSubLayerPTL(i)->setLevelIdc(Level::Name(uiCode));
+    }
+  }
+}
+
+#if ENC_DEC_TRACE || RExt__DECODER_DEBUG_BIT_STATISTICS
+Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl, const Bool bIsSubLayer)
+#define PTL_TRACE_TEXT(txt) bIsSubLayer?("sub_layer_" txt) : ("general_" txt)
 #else
-    if( profilePresentFlag && rpcPTL->getSubLayerProfilePresentFlag(i) )          
-#endif
-    {
-      parseProfileTier(rpcPTL->getSubLayerPTL(i));
-    }
-    if(rpcPTL->getSubLayerLevelPresentFlag(i))
-    {
-      READ_CODE( 8, uiCode, "sub_layer_level_idc[i]" );   rpcPTL->getSubLayerPTL(i)->setLevelIdc(uiCode);
-    }
-  }
-}
-
-Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl)
+Void TDecCavlc::parseProfileTier(ProfileTierLevel *ptl, const Bool /*bIsSubLayer*/)
+#define PTL_TRACE_TEXT(txt) txt
+#endif
 {
   UInt uiCode;
-  READ_CODE(2 , uiCode, "XXX_profile_space[]");   ptl->setProfileSpace(uiCode);
-  READ_FLAG(    uiCode, "XXX_tier_flag[]"    );   ptl->setTierFlag    (uiCode ? 1 : 0);
-  READ_CODE(5 , uiCode, "XXX_profile_idc[]"  );   ptl->setProfileIdc  (uiCode);
+  READ_CODE(2 , uiCode,   PTL_TRACE_TEXT("profile_space"                   )); ptl->setProfileSpace(uiCode);
+  READ_FLAG(    uiCode,   PTL_TRACE_TEXT("tier_flag"                       )); ptl->setTierFlag    (uiCode ? Level::HIGH : Level::MAIN);
+  READ_CODE(5 , uiCode,   PTL_TRACE_TEXT("profile_idc"                     )); ptl->setProfileIdc  (Profile::Name(uiCode));
   for(Int j = 0; j < 32; j++)
   {
-    READ_FLAG(  uiCode, "XXX_profile_compatibility_flag[][j]");   ptl->setProfileCompatibilityFlag(j, uiCode ? 1 : 0);
-  }
-  READ_FLAG(uiCode, "general_progressive_source_flag");
-  ptl->setProgressiveSourceFlag(uiCode ? true : false);
-
-  READ_FLAG(uiCode, "general_interlaced_source_flag");
-  ptl->setInterlacedSourceFlag(uiCode ? true : false);
-  
-  READ_FLAG(uiCode, "general_non_packed_constraint_flag");
-  ptl->setNonPackedConstraintFlag(uiCode ? true : false);
-  
-  READ_FLAG(uiCode, "general_frame_only_constraint_flag");
-  ptl->setFrameOnlyConstraintFlag(uiCode ? true : false);
-  
-#if H_MV
+    READ_FLAG(  uiCode,   PTL_TRACE_TEXT("profile_compatibility_flag[][j]" )); ptl->setProfileCompatibilityFlag(j, uiCode ? 1 : 0);
+  }
+  READ_FLAG(uiCode,       PTL_TRACE_TEXT("progressive_source_flag"         )); ptl->setProgressiveSourceFlag(uiCode ? true : false);
+
+  READ_FLAG(uiCode,       PTL_TRACE_TEXT("interlaced_source_flag"          )); ptl->setInterlacedSourceFlag(uiCode ? true : false);
+
+  READ_FLAG(uiCode,       PTL_TRACE_TEXT("non_packed_constraint_flag"      )); ptl->setNonPackedConstraintFlag(uiCode ? true : false);
+
+  READ_FLAG(uiCode,       PTL_TRACE_TEXT("frame_only_constraint_flag"      )); ptl->setFrameOnlyConstraintFlag(uiCode ? true : false);
+#if NH_MV
   if( ptl->getV2ConstraintsPresentFlag() )
   {
@@ -2958,7 +3148,50 @@
   }
 #else
-  READ_CODE(16, uiCode, "XXX_reserved_zero_44bits[0..15]");
-  READ_CODE(16, uiCode, "XXX_reserved_zero_44bits[16..31]");
-  READ_CODE(12, uiCode, "XXX_reserved_zero_44bits[32..43]");
+
+  if (ptl->getProfileIdc() == Profile::MAINREXT           || ptl->getProfileCompatibilityFlag(Profile::MAINREXT) ||
+      ptl->getProfileIdc() == Profile::HIGHTHROUGHPUTREXT || ptl->getProfileCompatibilityFlag(Profile::HIGHTHROUGHPUTREXT))
+  {
+    UInt maxBitDepth=16;
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_12bit_constraint_flag"       )); if (uiCode) maxBitDepth=12;
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_10bit_constraint_flag"       )); if (uiCode) maxBitDepth=10;
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_8bit_constraint_flag"        )); if (uiCode) maxBitDepth=8;
+    ptl->setBitDepthConstraint(maxBitDepth);
+    ChromaFormat chromaFmtConstraint=CHROMA_444;
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_422chroma_constraint_flag"   )); if (uiCode) chromaFmtConstraint=CHROMA_422;
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_420chroma_constraint_flag"   )); if (uiCode) chromaFmtConstraint=CHROMA_420;
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("max_monochrome_constraint_flag"  )); if (uiCode) chromaFmtConstraint=CHROMA_400;
+    ptl->setChromaFormatConstraint(chromaFmtConstraint);
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("intra_constraint_flag"           )); ptl->setIntraConstraintFlag(uiCode != 0);
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("one_picture_only_constraint_flag")); ptl->setOnePictureOnlyConstraintFlag(uiCode != 0);
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("lower_bit_rate_constraint_flag"  )); ptl->setLowerBitRateConstraintFlag(uiCode != 0);
+    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[0..15]"     ));
+    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[16..31]"    ));
+    READ_CODE(2,  uiCode, PTL_TRACE_TEXT("reserved_zero_34bits[32..33]"    ));
+  }
+  else
+  {
+    ptl->setBitDepthConstraint((ptl->getProfileIdc() == Profile::MAIN10)?10:8);
+    ptl->setChromaFormatConstraint(CHROMA_420);
+    ptl->setIntraConstraintFlag(false);
+    ptl->setLowerBitRateConstraintFlag(true);
+    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[0..15]"     ));
+    READ_CODE(16, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[16..31]"    ));
+    READ_CODE(11, uiCode, PTL_TRACE_TEXT("reserved_zero_43bits[32..42]"    ));
+  }
+
+  if ((ptl->getProfileIdc() >= Profile::MAIN && ptl->getProfileIdc() <= Profile::HIGHTHROUGHPUTREXT) ||
+       ptl->getProfileCompatibilityFlag(Profile::MAIN) ||
+       ptl->getProfileCompatibilityFlag(Profile::MAIN10) ||
+       ptl->getProfileCompatibilityFlag(Profile::MAINSTILLPICTURE) ||
+       ptl->getProfileCompatibilityFlag(Profile::MAINREXT) ||
+       ptl->getProfileCompatibilityFlag(Profile::HIGHTHROUGHPUTREXT) )
+  {
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("inbld_flag"                      )); assert(uiCode == 0);
+  }
+  else
+  {
+    READ_FLAG(    uiCode, PTL_TRACE_TEXT("reserved_zero_bit"               ));
+  }
+#undef PTL_TRACE_TEXT
 #endif
 }
@@ -2978,41 +3211,63 @@
 }
 
+Void TDecCavlc::parseRemainingBytes( Bool noTrailingBytesExpected )
+{
+  if (noTrailingBytesExpected)
+  {
+    const UInt numberOfRemainingSubstreamBytes=m_pcBitstream->getNumBitsLeft();
+    assert (numberOfRemainingSubstreamBytes == 0);
+  }
+  else
+  {
+    while (m_pcBitstream->getNumBitsLeft())
+    {
+      UInt trailingNullByte=m_pcBitstream->readByte();
+      if (trailingNullByte!=0)
+      {
+        printf("Trailing byte should be 0, but has value %02x\n", trailingNullByte);
+        assert(trailingNullByte==0);
+      }
+    }
+  }
+}
+
+#if NH_3D_DIS
+Void TDecCavlc::parseDIS( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
+{
+  assert(0);
+}
+#endif
+
 Void TDecCavlc::parseSkipFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
 {
   assert(0);
 }
-#if H_3D
-Void TDecCavlc::parseDIS( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
+
+Void TDecCavlc::parseCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
 {
   assert(0);
 }
-#endif
-
-Void TDecCavlc::parseCUTransquantBypassFlag( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
+
+Void TDecCavlc::parseMVPIdx( Int& /*riMVPIdx*/ )
 {
   assert(0);
 }
 
-Void TDecCavlc::parseMVPIdx( Int& /*riMVPIdx*/ )
+Void TDecCavlc::parseSplitFlag     ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
 {
   assert(0);
 }
 
-Void TDecCavlc::parseSplitFlag     ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
+Void TDecCavlc::parsePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
 {
   assert(0);
 }
 
-Void TDecCavlc::parsePartSize( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
+Void TDecCavlc::parsePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
 {
   assert(0);
 }
 
-Void TDecCavlc::parsePredMode( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
-{
-  assert(0);
-}
-
-/** Parse I_PCM information. 
+/** Parse I_PCM information.
 * \param pcCU pointer to CU
 * \param uiAbsPartIdx CU index
@@ -3020,5 +3275,5 @@
 * \returns Void
 *
-* If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.  
+* If I_PCM flag indicates that the CU is I_PCM, parse its PCM alignment bits and codes.
 */
 Void TDecCavlc::parseIPCMInfo( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
@@ -3028,5 +3283,5 @@
 
 Void TDecCavlc::parseIntraDirLumaAng  ( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
-{ 
+{
   assert(0);
 }
@@ -3052,25 +3307,41 @@
 }
 
+Void TDecCavlc::parseCrossComponentPrediction( class TComTU& /*rTu*/, ComponentID /*compID*/ )
+{
+  assert(0);
+}
+
 Void TDecCavlc::parseDeltaQP( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
 {
-  Int qp;
   Int  iDQp;
 
+#if RExt__DECODER_DEBUG_BIT_STATISTICS
+  READ_SVLC(iDQp, "delta_qp");
+#else
   xReadSvlc( iDQp );
-
-  Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffsetY();
-  qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+ qpBdOffsetY)) -  qpBdOffsetY;
-
-  UInt uiAbsQpCUPartIdx = (uiAbsPartIdx>>((g_uiMaxCUDepth - pcCU->getSlice()->getPPS()->getMaxCuDQPDepth())<<1))<<((g_uiMaxCUDepth - pcCU->getSlice()->getPPS()->getMaxCuDQPDepth())<<1) ;
-  UInt uiQpCUDepth =   min(uiDepth,pcCU->getSlice()->getPPS()->getMaxCuDQPDepth()) ;
+#endif
+
+  Int qpBdOffsetY = pcCU->getSlice()->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA);
+  const Int qp = (((Int) pcCU->getRefQP( uiAbsPartIdx ) + iDQp + 52 + 2*qpBdOffsetY )%(52+ qpBdOffsetY)) -  qpBdOffsetY;
+
+  const UInt maxCUDepth        = pcCU->getSlice()->getSPS()->getMaxTotalCUDepth();
+  const UInt maxCuDQPDepth     = pcCU->getSlice()->getPPS()->getMaxCuDQPDepth();
+  const UInt doubleDepthDifference = ((maxCUDepth - maxCuDQPDepth)<<1);
+  const UInt uiAbsQpCUPartIdx = (uiAbsPartIdx>>doubleDepthDifference)<<doubleDepthDifference ;
+  const UInt uiQpCUDepth =   min(uiDepth,pcCU->getSlice()->getPPS()->getMaxCuDQPDepth()) ;
 
   pcCU->setQPSubParts( qp, uiAbsQpCUPartIdx, uiQpCUDepth );
 }
 
-Void TDecCavlc::parseCoeffNxN( TComDataCU* /*pcCU*/, TCoeff* /*pcCoef*/, UInt /*uiAbsPartIdx*/, UInt /*uiWidth*/, UInt /*uiHeight*/, UInt /*uiDepth*/, TextType /*eTType*/ )
+Void TDecCavlc::parseChromaQpAdjustment( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
 {
   assert(0);
 }
 
+Void TDecCavlc::parseCoeffNxN( TComTU &/*rTu*/, ComponentID /*compID*/ )
+{
+  assert(0);
+}
+
 Void TDecCavlc::parseTransformSubdivFlag( UInt& /*ruiSubdivFlag*/, UInt /*uiLog2TransformBlockSize*/ )
 {
@@ -3078,5 +3349,5 @@
 }
 
-Void TDecCavlc::parseQtCbf( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, TextType /*eType*/, UInt /*uiTrDepth*/, UInt /*uiDepth*/ )
+Void TDecCavlc::parseQtCbf( TComTU &/*rTu*/, const ComponentID /*compID*/, const Bool /*lowestLevel*/ )
 {
   assert(0);
@@ -3088,5 +3359,5 @@
 }
 
-Void TDecCavlc::parseTransformSkipFlags (TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*width*/, UInt /*height*/, UInt /*uiDepth*/, TextType /*eTType*/)
+Void TDecCavlc::parseTransformSkipFlags (TComTU &/*rTu*/, ComponentID /*component*/)
 {
   assert(0);
@@ -3103,5 +3374,5 @@
 }
 
-#if H_3D_ARP
+#if NH_3D_ARP
 Void TDecCavlc::parseARPW( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
 {
@@ -3109,5 +3380,5 @@
 }
 #endif
-#if H_3D_IC
+#if NH_3D_IC
 Void TDecCavlc::parseICFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
 {
@@ -3115,17 +3386,12 @@
 }
 #endif
-#if H_3D_INTER_SDC
-Void TDecCavlc::parseDeltaDC( TComDataCU* /*pcCU*/, UInt /*uiAbsPartIdx*/, UInt /*uiDepth*/ )
-{ 
+#if NH_3D_SDC_INTRA || NH_3D_SDC_INTER
+Void TDecCavlc::parseSDCFlag    ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
+{
   assert(0);
 }
 
-Void TDecCavlc::parseSDCFlag    ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
-{
-  assert(0);
-}
-
-#endif
-#if H_3D_DBBP
+#endif
+#if NH_3D_DBBP
   Void TDecCavlc::parseDBBPFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
   {
@@ -3137,109 +3403,106 @@
 // ====================================================================================================================
 
-/** parse explicit wp tables
-* \param TComSlice* pcSlice
-* \returns Void
-*/
-Void TDecCavlc::xParsePredWeightTable( TComSlice* pcSlice )
-{
-  wpScalingParam  *wp;
-#if H_3D_DISABLE_CHROMA
-  Bool            bChroma     = ( pcSlice->getSPS()->getChromaFormatIdc() != 0 ); 
-#else
-  Bool            bChroma     = true; // color always present in HEVC ?
-#endif
-  SliceType       eSliceType  = pcSlice->getSliceType();
-  Int             iNbRef       = (eSliceType == B_SLICE ) ? (2) : (1);
-  UInt            uiLog2WeightDenomLuma, uiLog2WeightDenomChroma;
-  UInt            uiTotalSignalledWeightFlags = 0;
+//! parse explicit wp tables
+Void TDecCavlc::xParsePredWeightTable( TComSlice* pcSlice, const TComSPS *sps )
+{
+        WPScalingParam *wp;
+  const ChromaFormat    chFmt        = sps->getChromaFormatIdc();
+  const Int             numValidComp = Int(getNumberValidComponents(chFmt));
+  const Bool            bChroma      = (chFmt!=CHROMA_400);
+  const SliceType       eSliceType   = pcSlice->getSliceType();
+  const Int             iNbRef       = (eSliceType == B_SLICE ) ? (2) : (1);
+        UInt            uiLog2WeightDenomLuma=0, uiLog2WeightDenomChroma=0;
+        UInt            uiTotalSignalledWeightFlags = 0;
 
   Int iDeltaDenom;
   // decode delta_luma_log2_weight_denom :
-  READ_UVLC( uiLog2WeightDenomLuma, "luma_log2_weight_denom" );     // ue(v): luma_log2_weight_denom
+  READ_UVLC( uiLog2WeightDenomLuma, "luma_log2_weight_denom" );
   assert( uiLog2WeightDenomLuma <= 7 );
-  if( bChroma ) 
-  {
-    READ_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );     // se(v): delta_chroma_log2_weight_denom
+  if( bChroma )
+  {
+    READ_SVLC( iDeltaDenom, "delta_chroma_log2_weight_denom" );
     assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)>=0);
     assert((iDeltaDenom + (Int)uiLog2WeightDenomLuma)<=7);
     uiLog2WeightDenomChroma = (UInt)(iDeltaDenom + uiLog2WeightDenomLuma);
   }
-  else
-  { 
-    // For some reasons this is also needed to fix a compiler warning when H_3D_DISABLE_CHROMA is equal to 0.
-    uiLog2WeightDenomChroma = 0; 
-  }
-
-
-  for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) 
+
+
+
+  for ( Int iNumRef=0 ; iNumRef<iNbRef ; iNumRef++ ) // loop over l0 and l1 syntax elements
   {
     RefPicList  eRefPicList = ( iNumRef ? REF_PIC_LIST_1 : REF_PIC_LIST_0 );
-    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
+    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
     {
       pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
 
-      wp[0].uiLog2WeightDenom = uiLog2WeightDenomLuma;
-      wp[1].uiLog2WeightDenom = uiLog2WeightDenomChroma;
-      wp[2].uiLog2WeightDenom = uiLog2WeightDenomChroma;
+      wp[COMPONENT_Y].uiLog2WeightDenom = uiLog2WeightDenomLuma;
+      for(Int j=1; j<numValidComp; j++)
+      {
+        wp[j].uiLog2WeightDenom = uiLog2WeightDenomChroma;
+      }
 
       UInt  uiCode;
-      READ_FLAG( uiCode, "luma_weight_lX_flag" );           // u(1): luma_weight_l0_flag
-      wp[0].bPresentFlag = ( uiCode == 1 );
-      uiTotalSignalledWeightFlags += wp[0].bPresentFlag;
-    }
-    if ( bChroma ) 
+      READ_FLAG( uiCode, iNumRef==0?"luma_weight_l0_flag[i]":"luma_weight_l1_flag[i]" );
+      wp[COMPONENT_Y].bPresentFlag = ( uiCode == 1 );
+      uiTotalSignalledWeightFlags += wp[COMPONENT_Y].bPresentFlag;
+    }
+    if ( bChroma )
     {
       UInt  uiCode;
-      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
+      for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
       {
         pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
-        READ_FLAG( uiCode, "chroma_weight_lX_flag" );      // u(1): chroma_weight_l0_flag
-        wp[1].bPresentFlag = ( uiCode == 1 );
-        wp[2].bPresentFlag = ( uiCode == 1 );
-        uiTotalSignalledWeightFlags += 2*wp[1].bPresentFlag;
-      }
-    }
-    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ ) 
+        READ_FLAG( uiCode, iNumRef==0?"chroma_weight_l0_flag[i]":"chroma_weight_l1_flag[i]" );
+        for(Int j=1; j<numValidComp; j++)
+        {
+          wp[j].bPresentFlag = ( uiCode == 1 );
+        }
+        uiTotalSignalledWeightFlags += 2*wp[COMPONENT_Cb].bPresentFlag;
+      }
+    }
+    for ( Int iRefIdx=0 ; iRefIdx<pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx++ )
     {
       pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
-      if ( wp[0].bPresentFlag ) 
+      if ( wp[COMPONENT_Y].bPresentFlag )
       {
         Int iDeltaWeight;
-        READ_SVLC( iDeltaWeight, "delta_luma_weight_lX" );  // se(v): delta_luma_weight_l0[i]
+        READ_SVLC( iDeltaWeight, iNumRef==0?"delta_luma_weight_l0[i]":"delta_luma_weight_l1[i]" );
         assert( iDeltaWeight >= -128 );
         assert( iDeltaWeight <=  127 );
-        wp[0].iWeight = (iDeltaWeight + (1<<wp[0].uiLog2WeightDenom));
-        READ_SVLC( wp[0].iOffset, "luma_offset_lX" );       // se(v): luma_offset_l0[i]
-        assert( wp[0].iOffset >= -128 );
-        assert( wp[0].iOffset <=  127 );
-      }
-      else 
-      {
-        wp[0].iWeight = (1 << wp[0].uiLog2WeightDenom);
-        wp[0].iOffset = 0;
-      }
-      if ( bChroma ) 
-      {
-        if ( wp[1].bPresentFlag ) 
-        {
-          for ( Int j=1 ; j<3 ; j++ ) 
+        wp[COMPONENT_Y].iWeight = (iDeltaWeight + (1<<wp[COMPONENT_Y].uiLog2WeightDenom));
+        READ_SVLC( wp[COMPONENT_Y].iOffset, iNumRef==0?"luma_offset_l0[i]":"luma_offset_l1[i]" );
+        Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_LUMA))/2 : 128;
+        assert( wp[0].iOffset >= -range );
+        assert( wp[0].iOffset <   range );
+      }
+      else
+      {
+        wp[COMPONENT_Y].iWeight = (1 << wp[COMPONENT_Y].uiLog2WeightDenom);
+        wp[COMPONENT_Y].iOffset = 0;
+      }
+      if ( bChroma )
+      {
+        if ( wp[COMPONENT_Cb].bPresentFlag )
+        {
+          Int range=sps->getSpsRangeExtension().getHighPrecisionOffsetsEnabledFlag() ? (1<<sps->getBitDepth(CHANNEL_TYPE_CHROMA))/2 : 128;
+          for ( Int j=1 ; j<numValidComp ; j++ )
           {
             Int iDeltaWeight;
-            READ_SVLC( iDeltaWeight, "delta_chroma_weight_lX" );  // se(v): chroma_weight_l0[i][j]
+            READ_SVLC( iDeltaWeight, iNumRef==0?"delta_chroma_weight_l0[i]":"delta_chroma_weight_l1[i]" );
             assert( iDeltaWeight >= -128 );
             assert( iDeltaWeight <=  127 );
-            wp[j].iWeight = (iDeltaWeight + (1<<wp[1].uiLog2WeightDenom));
+            wp[j].iWeight = (iDeltaWeight + (1<<wp[j].uiLog2WeightDenom));
 
             Int iDeltaChroma;
-            READ_SVLC( iDeltaChroma, "delta_chroma_offset_lX" );  // se(v): delta_chroma_offset_l0[i][j]
-            assert( iDeltaChroma >= -512 );
-            assert( iDeltaChroma <=  511 );
-            Int pred = ( 128 - ( ( 128*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
-            wp[j].iOffset = Clip3(-128, 127, (iDeltaChroma + pred) );
+            READ_SVLC( iDeltaChroma, iNumRef==0?"delta_chroma_offset_l0[i]":"delta_chroma_offset_l1[i]" );
+            assert( iDeltaChroma >= -4*range);
+            assert( iDeltaChroma <   4*range);
+            Int pred = ( range - ( ( range*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
+            wp[j].iOffset = Clip3(-range, range-1, (iDeltaChroma + pred) );
           }
         }
-        else 
-        {
-          for ( Int j=1 ; j<3 ; j++ ) 
+        else
+        {
+          for ( Int j=1 ; j<numValidComp ; j++ )
           {
             wp[j].iWeight = (1 << wp[j].uiLog2WeightDenom);
@@ -3250,5 +3513,5 @@
     }
 
-    for ( Int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ ) 
+    for ( Int iRefIdx=pcSlice->getNumRefIdx(eRefPicList) ; iRefIdx<MAX_NUM_REF ; iRefIdx++ )
     {
       pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
@@ -3272,22 +3535,43 @@
   for(sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
   {
-    for(listId = 0; listId <  g_scalingListNum[sizeId]; listId++)
-    {
-      READ_FLAG( code, "scaling_list_pred_mode_flag");
-      scalingListPredModeFlag = (code) ? true : false;
-      if(!scalingListPredModeFlag) //Copy Mode
-      {
-        READ_UVLC( code, "scaling_list_pred_matrix_id_delta");
-        scalingList->setRefMatrixId (sizeId,listId,(UInt)((Int)(listId)-(code)));
-        if( sizeId > SCALING_LIST_8x8 )
-        {
-          scalingList->setScalingListDC(sizeId,listId,((listId == scalingList->getRefMatrixId (sizeId,listId))? 16 :scalingList->getScalingListDC(sizeId, scalingList->getRefMatrixId (sizeId,listId))));
-        }
-        scalingList->processRefMatrix( sizeId, listId, scalingList->getRefMatrixId (sizeId,listId));
-
-      }
-      else //DPCM Mode
-      {
-        xDecodeScalingList(scalingList, sizeId, listId);
+    for(listId = 0; listId <  SCALING_LIST_NUM; listId++)
+    {
+      if ((sizeId==SCALING_LIST_32x32) && (listId%(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES) != 0))
+      {
+        Int *src = scalingList->getScalingListAddress(sizeId, listId);
+        const Int size = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
+        const Int *srcNextSmallerSize = scalingList->getScalingListAddress(sizeId-1, listId);
+        for(Int i=0; i<size; i++)
+        {
+          src[i] = srcNextSmallerSize[i];
+        }
+        scalingList->setScalingListDC(sizeId,listId,(sizeId > SCALING_LIST_8x8) ? scalingList->getScalingListDC(sizeId-1, listId) : src[0]);
+      }
+      else
+      {
+        READ_FLAG( code, "scaling_list_pred_mode_flag");
+        scalingListPredModeFlag = (code) ? true : false;
+        scalingList->setScalingListPredModeFlag(sizeId, listId, scalingListPredModeFlag);
+        if(!scalingListPredModeFlag) //Copy Mode
+        {
+          READ_UVLC( code, "scaling_list_pred_matrix_id_delta");
+
+          if (sizeId==SCALING_LIST_32x32)
+          {
+            code*=(SCALING_LIST_NUM/NUMBER_OF_PREDICTION_MODES); // Adjust the decoded code for this size, to cope with the missing 32x32 chroma entries.
+          }
+
+          scalingList->setRefMatrixId (sizeId,listId,(UInt)((Int)(listId)-(code)));
+          if( sizeId > SCALING_LIST_8x8 )
+          {
+            scalingList->setScalingListDC(sizeId,listId,((listId == scalingList->getRefMatrixId (sizeId,listId))? 16 :scalingList->getScalingListDC(sizeId, scalingList->getRefMatrixId (sizeId,listId))));
+          }
+          scalingList->processRefMatrix( sizeId, listId, scalingList->getRefMatrixId (sizeId,listId));
+
+        }
+        else //DPCM Mode
+        {
+          xDecodeScalingList(scalingList, sizeId, listId);
+        }
       }
     }
@@ -3307,5 +3591,5 @@
   Int scalingListDcCoefMinus8 = 0;
   Int nextCoef = SCALING_LIST_START_VALUE;
-  UInt* scan  = (sizeId == 0) ? g_auiSigLastScan [ SCAN_DIAG ] [ 1 ] :  g_sigLastScanCG32x32;
+  UInt* scan  = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId==0 ? 2 : 3][sizeId==0 ? 2 : 3];
   Int *dst = scalingList->getScalingListAddress(sizeId, listId);
 
@@ -3326,5 +3610,5 @@
 
 Bool TDecCavlc::xMoreRbspData()
-{ 
+{
   Int bitsLeft = m_pcBitstream->getNumBitsLeft();
 
@@ -3354,4 +3638,8 @@
 }
 
+Void TDecCavlc::parseExplicitRdpcmMode( TComTU& /*rTu*/, ComponentID /*compID*/ )
+{
+  assert(0);
+}
 //! \}
 
