Index: trunk/source/Lib/TLibEncoder/TEncCavlc.cpp
===================================================================
--- trunk/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 33)
+++ trunk/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 125)
@@ -4,5 +4,5 @@
  * granted under this license.  
  *
- * Copyright (c) 2010-2012, ITU/ISO/IEC
+ * Copyright (c) 2010-2013, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -55,11 +55,4 @@
 }
 
-#if !REMOVE_APS
-Void  xTraceAPSHeader (TComAPS *pAPS)
-{
-  fprintf( g_hTrace, "=========== Adaptation Parameter Set ===========\n");
-}
-#endif
-
 Void  xTraceSliceHeader (TComSlice *pSlice)
 {
@@ -79,7 +72,4 @@
   m_pcBitIf           = NULL;
   m_uiCoeffCost       = 0;
-#if !REMOVE_FGS
-  m_iSliceGranularity = 0;
-#endif
 }
 
@@ -98,16 +88,4 @@
 
 
-#if !REMOVE_APS
-Void  TEncCavlc::codeAPSInitInfo(TComAPS* pcAPS)
-{
-
-#if ENC_DEC_TRACE  
-  xTraceAPSHeader(pcAPS);
-#endif
-  //APS ID
-  WRITE_UVLC( pcAPS->getAPSID(), "aps_id" );
-}
-#endif
-
 Void TEncCavlc::codeDFFlag(UInt uiCode, const Char *pSymbolName)
 {
@@ -119,25 +97,20 @@
 }
 
-#if J0234_INTER_RPS_SIMPL
-Void TEncCavlc::codeShortTermRefPicSet( TComSPS* pcSPS, TComReferencePictureSet* rps, Bool calledFromSliceHeader)
-#else
-Void TEncCavlc::codeShortTermRefPicSet( TComSPS* pcSPS, TComReferencePictureSet* rps )
-#endif
+Void TEncCavlc::codeShortTermRefPicSet( TComSPS* pcSPS, TComReferencePictureSet* rps, Bool calledFromSliceHeader, Int idx)
 {
 #if PRINT_RPS_INFO
-  int lastBits = getNumberOfWrittenBits();
-#endif
+  Int lastBits = getNumberOfWrittenBits();
+#endif
+  if (idx > 0)
+  {
   WRITE_FLAG( rps->getInterRPSPrediction(), "inter_ref_pic_set_prediction_flag" ); // inter_RPS_prediction_flag
-  if (rps->getInterRPSPrediction()) 
+  }
+  if (rps->getInterRPSPrediction())
   {
     Int deltaRPS = rps->getDeltaRPS();
-#if J0234_INTER_RPS_SIMPL
     if(calledFromSliceHeader)
     {
       WRITE_UVLC( rps->getDeltaRIdxMinus1(), "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
     }
-#else
-    WRITE_UVLC( rps->getDeltaRIdxMinus1(), "delta_idx_minus1" ); // delta index of the Reference Picture Set used for prediction minus 1
-#endif
 
     WRITE_CODE( (deltaRPS >=0 ? 0: 1), 1, "delta_rps_sign" ); //delta_rps_sign
@@ -187,7 +160,11 @@
 #endif
   
-  WRITE_UVLC( pcPPS->getPPSId(),                             "pic_parameter_set_id" );
-  WRITE_UVLC( pcPPS->getSPSId(),                             "seq_parameter_set_id" );
-
+  WRITE_UVLC( pcPPS->getPPSId(),                             "pps_pic_parameter_set_id" );
+  WRITE_UVLC( pcPPS->getSPSId(),                             "pps_seq_parameter_set_id" );
+  WRITE_FLAG( pcPPS->getDependentSliceSegmentsEnabledFlag()    ? 1 : 0, "dependent_slice_segments_enabled_flag" );
+#if L0255_MOVE_PPS_FLAGS
+  WRITE_FLAG( pcPPS->getOutputFlagPresentFlag() ? 1 : 0,     "output_flag_present_flag" );
+  WRITE_CODE( pcPPS->getNumExtraSliceHeaderBits(), 3,        "num_extra_slice_header_bits");
+#endif
   WRITE_FLAG( pcPPS->getSignHideFlag(), "sign_data_hiding_flag" );
   WRITE_FLAG( pcPPS->getCabacInitPresentFlag() ? 1 : 0,   "cabac_init_present_flag" );
@@ -195,81 +172,25 @@
   WRITE_UVLC( pcPPS->getNumRefIdxL1DefaultActive()-1,     "num_ref_idx_l1_default_active_minus1");
 
-  WRITE_SVLC( pcPPS->getPicInitQPMinus26(),                  "pic_init_qp_minus26");
+  WRITE_SVLC( pcPPS->getPicInitQPMinus26(),                  "init_qp_minus26");
   WRITE_FLAG( pcPPS->getConstrainedIntraPred() ? 1 : 0,      "constrained_intra_pred_flag" );
-#if PPS_TS_FLAG
   WRITE_FLAG( pcPPS->getUseTransformSkip() ? 1 : 0,  "transform_skip_enabled_flag" ); 
-#endif
-#if !REMOVE_FGS
-  WRITE_CODE( pcPPS->getSliceGranularity(), 2,               "slice_granularity");
-#endif
-#if CU_DQP_ENABLE_FLAG
   WRITE_FLAG( pcPPS->getUseDQP() ? 1 : 0, "cu_qp_delta_enabled_flag" );
   if ( pcPPS->getUseDQP() )
   {
-#if REMOVE_FGS
     WRITE_UVLC( pcPPS->getMaxCuDQPDepth(), "diff_cu_qp_delta_depth" );
-#else
-    WRITE_UVLC( pcPPS->getMaxCuDQPDepth() - pcPPS->getSliceGranularity(), "diff_cu_qp_delta_depth" );
-#endif
-  }
-#else
-#if REMOVE_FGS
-  WRITE_UVLC( pcPPS->getMaxCuDQPDepth() + pcPPS->getUseDQP(),                   "diff_cu_qp_delta_depth" );
-#else
-  WRITE_UVLC( pcPPS->getMaxCuDQPDepth() - pcPPS->getSliceGranularity() + pcPPS->getUseDQP(),                   "diff_cu_qp_delta_depth" );
-#endif
-#endif
-  WRITE_SVLC( pcPPS->getChromaCbQpOffset(),                   "cb_qp_offset" );
-  WRITE_SVLC( pcPPS->getChromaCrQpOffset(),                   "cr_qp_offset" );
-#if CHROMA_QP_EXTENSION
-  WRITE_FLAG( pcPPS->getSliceChromaQpFlag() ? 1 : 0,          "slicelevel_chroma_qp_flag" );
-#endif
+  }
+  WRITE_SVLC( pcPPS->getChromaCbQpOffset(),                   "pps_cb_qp_offset" );
+  WRITE_SVLC( pcPPS->getChromaCrQpOffset(),                   "pps_cr_qp_offset" );
+  WRITE_FLAG( pcPPS->getSliceChromaQpFlag() ? 1 : 0,          "pps_slice_chroma_qp_offsets_present_flag" );
 
   WRITE_FLAG( pcPPS->getUseWP() ? 1 : 0,  "weighted_pred_flag" );   // Use of Weighting Prediction (P_SLICE)
   WRITE_FLAG( pcPPS->getWPBiPred() ? 1 : 0, "weighted_bipred_flag" );  // Use of Weighting Bi-Prediction (B_SLICE)
+#if !L0255_MOVE_PPS_FLAGS
   WRITE_FLAG( pcPPS->getOutputFlagPresentFlag() ? 1 : 0,  "output_flag_present_flag" );
-#if !TILES_WPP_ENTROPYSLICES_FLAGS
-#if DEPENDENT_SLICES
-  WRITE_FLAG( pcPPS->getDependentSliceEnabledFlag() ? 1 : 0, "dependent_slices_enabled_flag" );
-#endif
 #endif
   WRITE_FLAG( pcPPS->getTransquantBypassEnableFlag() ? 1 : 0, "transquant_bypass_enable_flag" );
-
-#if TILES_WPP_ENTROPYSLICES_FLAGS
-#if DEPENDENT_SLICES
-  WRITE_FLAG( pcPPS->getDependentSliceEnabledFlag()    ? 1 : 0, "dependent_slice_enabled_flag" );
-#endif
   WRITE_FLAG( pcPPS->getTilesEnabledFlag()             ? 1 : 0, "tiles_enabled_flag" );
   WRITE_FLAG( pcPPS->getEntropyCodingSyncEnabledFlag() ? 1 : 0, "entropy_coding_sync_enabled_flag" );
-  WRITE_FLAG( pcPPS->getEntropySliceEnabledFlag()      ? 1 : 0, "entropy_slice_enabled_flag" );
   if( pcPPS->getTilesEnabledFlag() )
-#else
-  Int tilesOrEntropyCodingSyncIdc = 0;
-  if ( pcPPS->getNumColumnsMinus1() > 0 || pcPPS->getNumRowsMinus1() > 0)
-  {
-    tilesOrEntropyCodingSyncIdc = 1;
-  }
-  else if ( pcPPS->getNumSubstreams() > 1 )
-  {
-    tilesOrEntropyCodingSyncIdc = 2;
-  }
-#if DEPENDENT_SLICES
-  else if( pcPPS->getDependentSliceEnabledFlag() )
-  {
-    if(pcPPS->getTilesOrEntropyCodingSyncIdc() != 2)
-    {
-      tilesOrEntropyCodingSyncIdc = 3;
-    }
-    else
-    {
-      tilesOrEntropyCodingSyncIdc = 2;
-    }
-  }
-#endif
-  pcPPS->setTilesOrEntropyCodingSyncIdc( tilesOrEntropyCodingSyncIdc );
-  WRITE_CODE(tilesOrEntropyCodingSyncIdc, 2, "tiles_or_entropy_coding_sync_idc");
-
-  if(pcPPS->getTilesOrEntropyCodingSyncIdc()==1)
-#endif
   {
     WRITE_UVLC( pcPPS->getNumColumnsMinus1(),                                    "num_tile_columns_minus1" );
@@ -292,20 +213,10 @@
     }
   }
-#if !TILES_WPP_ENTROPYSLICES_FLAGS
-#if DEPENDENT_SLICES
-  else if( pcPPS->getTilesOrEntropyCodingSyncIdc()==3 )
-  {
-    WRITE_FLAG( pcPPS->getCabacIndependentFlag()? 1 : 0,            "cabac_independent_flag" );
-  }
-#endif
-#endif
-#if MOVE_LOOP_FILTER_SLICES_FLAG
   WRITE_FLAG( pcPPS->getLoopFilterAcrossSlicesEnabledFlag()?1 : 0,        "loop_filter_across_slices_enabled_flag");
-#endif
   WRITE_FLAG( pcPPS->getDeblockingFilterControlPresentFlag()?1 : 0,       "deblocking_filter_control_present_flag");
   if(pcPPS->getDeblockingFilterControlPresentFlag())
   {
     WRITE_FLAG( pcPPS->getDeblockingFilterOverrideEnabledFlag() ? 1 : 0,  "deblocking_filter_override_enabled_flag" ); 
-    WRITE_FLAG( pcPPS->getPicDisableDeblockingFilterFlag(),               "pic_disable_deblocking_filter_flag" );
+    WRITE_FLAG( pcPPS->getPicDisableDeblockingFilterFlag() ? 1 : 0,       "pps_disable_deblocking_filter_flag" );
     if(!pcPPS->getPicDisableDeblockingFilterFlag())
     {
@@ -322,12 +233,13 @@
     codeScalingList( m_pcSlice->getScalingList() );
   }
+  WRITE_FLAG( pcPPS->getListsModificationPresentFlag(), "lists_modification_present_flag");
   WRITE_UVLC( pcPPS->getLog2ParallelMergeLevelMinus2(), "log2_parallel_merge_level_minus2");
-#if SLICE_HEADER_EXTENSION
-  WRITE_FLAG( pcPPS->getSliceHeaderExtensionPresentFlag(), "slice_header_extension_present_flag");
-#endif
+#if !L0255_MOVE_PPS_FLAGS
+  WRITE_CODE( pcPPS->getNumExtraSliceHeaderBits(), 3, "num_extra_slice_header_bits");
+#endif
+  WRITE_FLAG( pcPPS->getSliceHeaderExtensionPresentFlag() ? 1 : 0, "slice_segment_header_extension_present_flag");
   WRITE_FLAG( 0, "pps_extension_flag" );
 }
 
-#if SUPPORT_FOR_VUI
 Void TEncCavlc::codeVUI( TComVUI *pcVUI, TComSPS* pcSPS )
 {
@@ -374,59 +286,44 @@
   WRITE_FLAG(pcVUI->getFieldSeqFlag(),                          "field_seq_flag");
   assert(pcVUI->getFieldSeqFlag() == 0);                        // not currently supported
+  WRITE_FLAG(pcVUI->getFrameFieldInfoPresentFlag(),             "frame_field_info_present_flag");
+
+  Window defaultDisplayWindow = pcVUI->getDefaultDisplayWindow();
+  WRITE_FLAG(defaultDisplayWindow.getWindowEnabledFlag(),       "default_display_window_flag");
+  if( defaultDisplayWindow.getWindowEnabledFlag() )
+  {
+    WRITE_UVLC(defaultDisplayWindow.getWindowLeftOffset(),      "def_disp_win_left_offset");
+    WRITE_UVLC(defaultDisplayWindow.getWindowRightOffset(),     "def_disp_win_right_offset");
+    WRITE_UVLC(defaultDisplayWindow.getWindowTopOffset(),       "def_disp_win_top_offset");
+    WRITE_UVLC(defaultDisplayWindow.getWindowBottomOffset(),    "def_disp_win_bottom_offset");
+  }
+#if L0043_TIMING_INFO
+  TimingInfo *timingInfo = pcVUI->getTimingInfo();
+  WRITE_FLAG(timingInfo->getTimingInfoPresentFlag(),          "vui_timing_info_present_flag");
+  if(timingInfo->getTimingInfoPresentFlag())
+  {
+    WRITE_CODE(timingInfo->getNumUnitsInTick(), 32,           "vui_num_units_in_tick");
+    WRITE_CODE(timingInfo->getTimeScale(),      32,           "vui_time_scale");
+    WRITE_FLAG(timingInfo->getPocProportionalToTimingFlag(),  "vui_poc_proportional_to_timing_flag");
+    if(timingInfo->getPocProportionalToTimingFlag())
+    {
+      WRITE_UVLC(timingInfo->getNumTicksPocDiffOneMinus1(),   "vui_num_ticks_poc_diff_one_minus1");
+    }
+#endif
   WRITE_FLAG(pcVUI->getHrdParametersPresentFlag(),              "hrd_parameters_present_flag");
-#if !BUFFERING_PERIOD_AND_TIMING_SEI
-  assert(pcVUI->getHrdParametersPresentFlag() == 0);            // not currently supported
-#else
   if( pcVUI->getHrdParametersPresentFlag() )
   {
-    WRITE_FLAG(pcVUI->getTimingInfoPresentFlag(),               "timing_info_present_flag");
-    if( pcVUI->getTimingInfoPresentFlag() )
-    {
-      WRITE_CODE(pcVUI->getNumUnitsInTick(), 32,                "num_units_in_tick");
-      WRITE_CODE(pcVUI->getTimeScale(),      32,                "time_scale");
-    }
-    WRITE_FLAG(pcVUI->getNalHrdParametersPresentFlag(),         "nal_hrd_parameters_present_flag");
-    WRITE_FLAG(pcVUI->getVclHrdParametersPresentFlag(),         "vcl_hrd_parameters_present_flag");
-    if( pcVUI->getNalHrdParametersPresentFlag() || pcVUI->getVclHrdParametersPresentFlag() )
-    {
-      WRITE_FLAG(pcVUI->getSubPicCpbParamsPresentFlag(),        "sub_pic_cpb_params_present_flag");
-      if( pcVUI->getSubPicCpbParamsPresentFlag() )
-      {
-        WRITE_CODE(pcVUI->getTickDivisorMinus2(), 8,            "tick_divisor_minus2");
-        WRITE_CODE(pcVUI->getDuCpbRemovalDelayLengthMinus1(), 5,  "du_cpb_removal_delay_length_minus1");
-      }
-      WRITE_CODE(pcVUI->getBitRateScale(), 4,                   "bit_rate_scale");
-      WRITE_CODE(pcVUI->getCpbSizeScale(), 4,                   "cpb_size_scale");
-      WRITE_CODE(pcVUI->getInitialCpbRemovalDelayLengthMinus1(), 5, "initial_cpb_removal_delay_length_minus1");
-      WRITE_CODE(pcVUI->getCpbRemovalDelayLengthMinus1(),        5, "cpb_removal_delay_length_minus1");
-      WRITE_CODE(pcVUI->getDpbOutputDelayLengthMinus1(),         5, "dpb_output_delay_length_minus1");
-    }
-    Int i, j, nalOrVcl;
-    for( i = 0; i <= pcSPS->getMaxTLayers(); i ++ )
-    {
-      WRITE_FLAG(pcVUI->getFixedPicRateFlag(i),                 "fixed_pic_rate_flag");
-      if( pcVUI->getFixedPicRateFlag( i ) )
-      {
-        WRITE_UVLC(pcVUI->getPicDurationInTcMinus1(i),          "pic_duration_in_tc_minus1");
-      }
-      WRITE_FLAG(pcVUI->getLowDelayHrdFlag(i),                  "low_delay_hrd_flag");
-      WRITE_UVLC(pcVUI->getCpbCntMinus1(i),                     "cpb_cnt_minus1");
-     
-      for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
-      {
-        if( ( ( nalOrVcl == 0 ) && ( pcVUI->getNalHrdParametersPresentFlag() ) ) ||
-            ( ( nalOrVcl == 1 ) && ( pcVUI->getVclHrdParametersPresentFlag() ) ) )
-        {
-          for( j = 0; j < ( pcVUI->getCpbCntMinus1( i ) + 1 ); j ++ )
-          {
-            WRITE_UVLC(pcVUI->getBitRateValueMinus1(i, j, nalOrVcl), "bit_size_value_minus1");
-            WRITE_UVLC(pcVUI->getCpbSizeValueMinus1(i, j, nalOrVcl), "cpb_size_value_minus1");
-            WRITE_FLAG(pcVUI->getCbrFlag(i, j, nalOrVcl),            "cbr_flag");
-          }
-        }
-      }
-    }
-  }
-#endif
+    codeHrdParameters(pcVUI->getHrdParameters(), 1, pcSPS->getMaxTLayers() - 1 );
+  }
+#if L0043_TIMING_INFO
+  }
+#endif
+#if !L0043_TIMING_INFO
+  WRITE_FLAG( pcVUI->getPocProportionalToTimingFlag(), "poc_proportional_to_timing_flag" );
+  if( pcVUI->getPocProportionalToTimingFlag() && pcVUI->getHrdParameters()->getTimingInfoPresentFlag() )
+  {
+    WRITE_UVLC( pcVUI->getNumTicksPocDiffOneMinus1(), "num_ticks_poc_diff_one_minus1" );
+  }
+#endif
+
   WRITE_FLAG(pcVUI->getBitstreamRestrictionFlag(),              "bitstream_restriction_flag");
   if (pcVUI->getBitstreamRestrictionFlag())
@@ -434,4 +331,10 @@
     WRITE_FLAG(pcVUI->getTilesFixedStructureFlag(),             "tiles_fixed_structure_flag");
     WRITE_FLAG(pcVUI->getMotionVectorsOverPicBoundariesFlag(),  "motion_vectors_over_pic_boundaries_flag");
+    WRITE_FLAG(pcVUI->getRestrictedRefPicListsFlag(),           "restricted_ref_pic_lists_flag");
+#if L0043_MSS_IDC
+    WRITE_UVLC(pcVUI->getMinSpatialSegmentationIdc(),           "min_spatial_segmentation_idc");
+#else
+    WRITE_CODE(pcVUI->getMinSpatialSegmentationIdc(),        8, "min_spatial_segmentation_idc");
+#endif
     WRITE_UVLC(pcVUI->getMaxBytesPerPicDenom(),                 "max_bytes_per_pic_denom");
     WRITE_UVLC(pcVUI->getMaxBitsPerMinCuDenom(),                "max_bits_per_mincu_denom");
@@ -439,7 +342,95 @@
     WRITE_UVLC(pcVUI->getLog2MaxMvLengthVertical(),             "log2_max_mv_length_vertical");
   }
-
-}
-#endif
+}
+
+Void TEncCavlc::codeHrdParameters( TComHRD *hrd, Bool commonInfPresentFlag, UInt maxNumSubLayersMinus1 )
+{
+  if( commonInfPresentFlag )
+  {
+#if !L0043_TIMING_INFO
+    WRITE_FLAG( hrd->getTimingInfoPresentFlag() ? 1 : 0,        "timing_info_present_flag" );
+    if( hrd->getTimingInfoPresentFlag() )
+    {
+      WRITE_CODE( hrd->getNumUnitsInTick(), 32,                  "num_units_in_tick" );
+      WRITE_CODE( hrd->getTimeScale(),      32,                  "time_scale" );
+    }
+#endif
+    WRITE_FLAG( hrd->getNalHrdParametersPresentFlag() ? 1 : 0 ,  "nal_hrd_parameters_present_flag" );
+    WRITE_FLAG( hrd->getVclHrdParametersPresentFlag() ? 1 : 0 ,  "vcl_hrd_parameters_present_flag" );
+    if( hrd->getNalHrdParametersPresentFlag() || hrd->getVclHrdParametersPresentFlag() )
+    {
+      WRITE_FLAG( hrd->getSubPicCpbParamsPresentFlag() ? 1 : 0,  "sub_pic_cpb_params_present_flag" );
+      if( hrd->getSubPicCpbParamsPresentFlag() )
+      {
+        WRITE_CODE( hrd->getTickDivisorMinus2(), 8,              "tick_divisor_minus2" );
+        WRITE_CODE( hrd->getDuCpbRemovalDelayLengthMinus1(), 5,  "du_cpb_removal_delay_length_minus1" );
+        WRITE_FLAG( hrd->getSubPicCpbParamsInPicTimingSEIFlag() ? 1 : 0, "sub_pic_cpb_params_in_pic_timing_sei_flag" );
+#if L0044_DU_DPB_OUTPUT_DELAY_HRD
+        WRITE_CODE( hrd->getDpbOutputDelayDuLengthMinus1(), 5,   "dpb_output_delay_du_length_minus1"  );
+#endif
+      }
+      WRITE_CODE( hrd->getBitRateScale(), 4,                     "bit_rate_scale" );
+      WRITE_CODE( hrd->getCpbSizeScale(), 4,                     "cpb_size_scale" );
+      if( hrd->getSubPicCpbParamsPresentFlag() )
+      {
+        WRITE_CODE( hrd->getDuCpbSizeScale(), 4,                "du_cpb_size_scale" ); 
+      }
+      WRITE_CODE( hrd->getInitialCpbRemovalDelayLengthMinus1(), 5, "initial_cpb_removal_delay_length_minus1" );
+      WRITE_CODE( hrd->getCpbRemovalDelayLengthMinus1(),        5, "au_cpb_removal_delay_length_minus1" );
+      WRITE_CODE( hrd->getDpbOutputDelayLengthMinus1(),         5, "dpb_output_delay_length_minus1" );
+    }
+  }
+  Int i, j, nalOrVcl;
+  for( i = 0; i <= maxNumSubLayersMinus1; i ++ )
+  {
+    WRITE_FLAG( hrd->getFixedPicRateFlag( i ) ? 1 : 0,          "fixed_pic_rate_general_flag");
+    if( !hrd->getFixedPicRateFlag( i ) )
+    {
+      WRITE_FLAG( hrd->getFixedPicRateWithinCvsFlag( i ) ? 1 : 0, "fixed_pic_rate_within_cvs_flag");
+    }
+    else
+    {
+      hrd->setFixedPicRateWithinCvsFlag( i, true );
+    }
+    if( hrd->getFixedPicRateWithinCvsFlag( i ) )
+    {
+      WRITE_UVLC( hrd->getPicDurationInTcMinus1( i ),           "elemental_duration_in_tc_minus1");
+    }
+#if L0372
+    else
+    {
+      WRITE_FLAG( hrd->getLowDelayHrdFlag( i ) ? 1 : 0,           "low_delay_hrd_flag");
+    }
+    if (!hrd->getLowDelayHrdFlag( i ))
+    {
+      WRITE_UVLC( hrd->getCpbCntMinus1( i ),                      "cpb_cnt_minus1");
+    }
+#else
+    WRITE_FLAG( hrd->getLowDelayHrdFlag( i ) ? 1 : 0,           "low_delay_hrd_flag");
+    WRITE_UVLC( hrd->getCpbCntMinus1( i ),                      "cpb_cnt_minus1");
+#endif
+    
+    for( nalOrVcl = 0; nalOrVcl < 2; nalOrVcl ++ )
+    {
+      if( ( ( nalOrVcl == 0 ) && ( hrd->getNalHrdParametersPresentFlag() ) ) ||
+          ( ( nalOrVcl == 1 ) && ( hrd->getVclHrdParametersPresentFlag() ) ) )
+      {
+        for( j = 0; j <= ( hrd->getCpbCntMinus1( i ) ); j ++ )
+        {
+          WRITE_UVLC( hrd->getBitRateValueMinus1( i, j, nalOrVcl ), "bit_rate_value_minus1");
+          WRITE_UVLC( hrd->getCpbSizeValueMinus1( i, j, nalOrVcl ), "cpb_size_value_minus1");
+          if( hrd->getSubPicCpbParamsPresentFlag() )
+          {
+#if L0363_DU_BIT_RATE
+            WRITE_UVLC( hrd->getDuBitRateValueMinus1( i, j, nalOrVcl ), "bit_rate_du_value_minus1");
+#endif
+            WRITE_UVLC( hrd->getDuCpbSizeValueMinus1( i, j, nalOrVcl ), "cpb_size_du_value_minus1");  
+          }
+          WRITE_FLAG( hrd->getCbrFlag( i, j, nalOrVcl ) ? 1 : 0, "cbr_flag");
+        }
+      }
+    }
+  }
+}
 
 Void TEncCavlc::codeSPS( TComSPS* pcSPS )
@@ -448,10 +439,9 @@
   xTraceSPSHeader (pcSPS);
 #endif
-#if SPS_SYNTAX_CHANGES
-  WRITE_CODE( pcSPS->getVPSId (),          4,       "video_parameter_set_id" );
+  WRITE_CODE( pcSPS->getVPSId (),          4,       "sps_video_parameter_set_id" );
   WRITE_CODE( pcSPS->getMaxTLayers() - 1,  3,       "sps_max_sub_layers_minus1" );
-  WRITE_FLAG( 0,                                    "sps_reserved_zero_bit");
+  WRITE_FLAG( pcSPS->getTemporalIdNestingFlag() ? 1 : 0,                             "sps_temporal_id_nesting_flag" );
   codePTL(pcSPS->getPTL(), 1, pcSPS->getMaxTLayers() - 1);
-  WRITE_UVLC( pcSPS->getSPSId (),                   "seq_parameter_set_id" );
+  WRITE_UVLC( pcSPS->getSPSId (),                   "sps_seq_parameter_set_id" );
   WRITE_UVLC( pcSPS->getChromaFormatIdc (),         "chroma_format_idc" );
   assert(pcSPS->getChromaFormatIdc () == 1);
@@ -462,51 +452,33 @@
   }
 
-#else
-  WRITE_CODE( pcSPS->getProfileSpace (),   3,       "profile_space" );
-  WRITE_CODE( pcSPS->getProfileIdc (),     5,       "profile_idc" );
-  WRITE_CODE( pcSPS->getRsvdIndFlags(),   16,       "reserved_indicator_flags" );
-  WRITE_CODE( pcSPS->getLevelIdc (),       8,       "level_idc" );
-  WRITE_CODE( pcSPS->getProfileCompat (), 32,       "profile_compatibility" );
-  WRITE_UVLC( pcSPS->getSPSId (),                   "seq_parameter_set_id" );
-  WRITE_UVLC( pcSPS->getVPSId (),                   "video_parameter_set_id" );
-  WRITE_UVLC( pcSPS->getChromaFormatIdc (),         "chroma_format_idc" );
-  WRITE_CODE( pcSPS->getMaxTLayers() - 1,  3,       "max_temporal_layers_minus1" );
-#endif
   WRITE_UVLC( pcSPS->getPicWidthInLumaSamples (),   "pic_width_in_luma_samples" );
   WRITE_UVLC( pcSPS->getPicHeightInLumaSamples(),   "pic_height_in_luma_samples" );
-  WRITE_FLAG( pcSPS->getPicCroppingFlag(),          "pic_cropping_flag" );
-  if (pcSPS->getPicCroppingFlag())
-  {
-    WRITE_UVLC( pcSPS->getPicCropLeftOffset()   / TComSPS::getCropUnitX(pcSPS->getChromaFormatIdc() ), "pic_crop_left_offset" );
-    WRITE_UVLC( pcSPS->getPicCropRightOffset()  / TComSPS::getCropUnitX(pcSPS->getChromaFormatIdc() ), "pic_crop_right_offset" );
-    WRITE_UVLC( pcSPS->getPicCropTopOffset()    / TComSPS::getCropUnitY(pcSPS->getChromaFormatIdc() ), "pic_crop_top_offset" );
-    WRITE_UVLC( pcSPS->getPicCropBottomOffset() / TComSPS::getCropUnitY(pcSPS->getChromaFormatIdc() ), "pic_crop_bottom_offset" );
-  }
-
-#if FULL_NBIT
-  WRITE_UVLC( pcSPS->getBitDepth() - 8,             "bit_depth_luma_minus8" );
-#else
-  WRITE_UVLC( pcSPS->getBitIncrement(),             "bit_depth_luma_minus8" );
-#endif
-#if FULL_NBIT
-  WRITE_UVLC( pcSPS->getBitDepth() - 8,             "bit_depth_chroma_minus8" );
-#else
-  WRITE_UVLC( pcSPS->getBitIncrement(),             "bit_depth_chroma_minus8" );
-#endif
-
-  WRITE_FLAG( pcSPS->getUsePCM() ? 1 : 0,                   "pcm_enabled_flag");
-
-  if( pcSPS->getUsePCM() )
-  {
-  WRITE_CODE( pcSPS->getPCMBitDepthLuma() - 1, 4,   "pcm_bit_depth_luma_minus1" );
-  WRITE_CODE( pcSPS->getPCMBitDepthChroma() - 1, 4, "pcm_bit_depth_chroma_minus1" );
-  }
+  Window conf = pcSPS->getConformanceWindow();
+
+  WRITE_FLAG( conf.getWindowEnabledFlag(),          "conformance_window_flag" );
+  if (conf.getWindowEnabledFlag())
+  {
+    WRITE_UVLC( conf.getWindowLeftOffset()   / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc() ), "conf_win_left_offset" );
+    WRITE_UVLC( conf.getWindowRightOffset()  / TComSPS::getWinUnitX(pcSPS->getChromaFormatIdc() ), "conf_win_right_offset" );
+    WRITE_UVLC( conf.getWindowTopOffset()    / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc() ), "conf_win_top_offset" );
+    WRITE_UVLC( conf.getWindowBottomOffset() / TComSPS::getWinUnitY(pcSPS->getChromaFormatIdc() ), "conf_win_bottom_offset" );
+  }
+
+  WRITE_UVLC( pcSPS->getBitDepthY() - 8,             "bit_depth_luma_minus8" );
+  WRITE_UVLC( pcSPS->getBitDepthC() - 8,             "bit_depth_chroma_minus8" );
 
   WRITE_UVLC( pcSPS->getBitsForPOC()-4,                 "log2_max_pic_order_cnt_lsb_minus4" );
+
+  const Bool subLayerOrderingInfoPresentFlag = 1;
+  WRITE_FLAG(subLayerOrderingInfoPresentFlag,       "sps_sub_layer_ordering_info_present_flag");
   for(UInt i=0; i <= pcSPS->getMaxTLayers()-1; i++)
   {
-    WRITE_UVLC( pcSPS->getMaxDecPicBuffering(i),           "max_dec_pic_buffering[i]" );
-    WRITE_UVLC( pcSPS->getNumReorderPics(i),               "num_reorder_pics[i]" );
-    WRITE_UVLC( pcSPS->getMaxLatencyIncrease(i),           "max_latency_increase[i]" );
+    WRITE_UVLC( pcSPS->getMaxDecPicBuffering(i),           "sps_max_dec_pic_buffering[i]" );
+    WRITE_UVLC( pcSPS->getNumReorderPics(i),               "sps_num_reorder_pics[i]" );
+    WRITE_UVLC( pcSPS->getMaxLatencyIncrease(i),           "sps_max_latency_increase[i]" );
+    if (!subLayerOrderingInfoPresentFlag)
+    {
+      break;
+    }
   }
   assert( pcSPS->getMaxCUWidth() == pcSPS->getMaxCUHeight() );
@@ -520,18 +492,8 @@
   }
 
-  WRITE_FLAG( pcSPS->getRestrictedRefPicListsFlag(),                                 "restricted_ref_pic_lists_flag" );
-  if( pcSPS->getRestrictedRefPicListsFlag() )
-  {
-    WRITE_FLAG( pcSPS->getListsModificationPresentFlag(),                            "lists_modification_present_flag" );
-  }
   WRITE_UVLC( log2MinCUSize - 3,                                                     "log2_min_coding_block_size_minus3" );
   WRITE_UVLC( pcSPS->getMaxCUDepth()-g_uiAddCUDepth,                                 "log2_diff_max_min_coding_block_size" );
   WRITE_UVLC( pcSPS->getQuadtreeTULog2MinSize() - 2,                                 "log2_min_transform_block_size_minus2" );
   WRITE_UVLC( pcSPS->getQuadtreeTULog2MaxSize() - pcSPS->getQuadtreeTULog2MinSize(), "log2_diff_max_min_transform_block_size" );
-  if( pcSPS->getUsePCM() )
-  {
-    WRITE_UVLC( pcSPS->getPCMLog2MinSize() - 3,                                      "log2_min_pcm_coding_block_size_minus3" );
-    WRITE_UVLC( pcSPS->getPCMLog2MaxSize() - pcSPS->getPCMLog2MinSize(),             "log2_diff_max_min_pcm_coding_block_size" );
-  }
   WRITE_UVLC( pcSPS->getQuadtreeTUMaxDepthInter() - 1,                               "max_transform_hierarchy_depth_inter" );
   WRITE_UVLC( pcSPS->getQuadtreeTUMaxDepthIntra() - 1,                               "max_transform_hierarchy_depth_intra" );
@@ -548,29 +510,18 @@
     }
   }
-#if !REMOVE_LMCHROMA
-  WRITE_FLAG( pcSPS->getUseLMChroma () ? 1 : 0,                                      "chroma_pred_from_luma_enabled_flag" );
-#endif
-#if !PPS_TS_FLAG
-  WRITE_FLAG( pcSPS->getUseTransformSkip () ? 1 : 0,                                 "transform_skip_enabled_flag" );
-#endif
-#if !MOVE_LOOP_FILTER_SLICES_FLAG
-  WRITE_FLAG( pcSPS->getLFCrossSliceBoundaryFlag()?1 : 0,                            "seq_loop_filter_across_slices_enabled_flag");
-#endif
-  WRITE_FLAG( pcSPS->getUseAMP(),                                                    "asymmetric_motion_partitions_enabled_flag" );
-#if !REMOVE_NSQT
-  WRITE_FLAG( pcSPS->getUseNSQT(),                                                   "non_square_quadtree_enabled_flag" );
-#endif
+  WRITE_FLAG( pcSPS->getUseAMP() ? 1 : 0,                                            "amp_enabled_flag" );
   WRITE_FLAG( pcSPS->getUseSAO() ? 1 : 0,                                            "sample_adaptive_offset_enabled_flag");
-#if !REMOVE_ALF
-  WRITE_FLAG( pcSPS->getUseALF () ? 1 : 0,                                           "adaptive_loop_filter_enabled_flag");
-#endif
+
+  WRITE_FLAG( pcSPS->getUsePCM() ? 1 : 0,                                            "pcm_enabled_flag");
   if( pcSPS->getUsePCM() )
   {
-  WRITE_FLAG( pcSPS->getPCMFilterDisableFlag()?1 : 0,                                "pcm_loop_filter_disable_flag");
+    WRITE_CODE( pcSPS->getPCMBitDepthLuma() - 1, 4,                                  "pcm_sample_bit_depth_luma_minus1" );
+    WRITE_CODE( pcSPS->getPCMBitDepthChroma() - 1, 4,                                "pcm_sample_bit_depth_chroma_minus1" );
+    WRITE_UVLC( pcSPS->getPCMLog2MinSize() - 3,                                      "log2_min_pcm_luma_coding_block_size_minus3" );
+    WRITE_UVLC( pcSPS->getPCMLog2MaxSize() - pcSPS->getPCMLog2MinSize(),             "log2_diff_max_min_pcm_luma_coding_block_size" );
+    WRITE_FLAG( pcSPS->getPCMFilterDisableFlag()?1 : 0,                              "pcm_loop_filter_disable_flag");
   }
 
   assert( pcSPS->getMaxTLayers() > 0 );         
-
-  WRITE_FLAG( pcSPS->getTemporalIdNestingFlag() ? 1 : 0,                             "temporal_id_nesting_flag" );
 
   TComRPSList* rpsList = pcSPS->getRPSList();
@@ -581,12 +532,7 @@
   {
     rps = rpsList->getReferencePictureSet(i);
-#if J0234_INTER_RPS_SIMPL
-    codeShortTermRefPicSet(pcSPS,rps,false);
-#else
-    codeShortTermRefPicSet(pcSPS,rps);
-#endif
-  }    
+    codeShortTermRefPicSet(pcSPS,rps,false, i);
+  }
   WRITE_FLAG( pcSPS->getLongTermRefsPresent() ? 1 : 0,         "long_term_ref_pics_present_flag" );
-#if LTRP_IN_SPS
   if (pcSPS->getLongTermRefsPresent()) 
   {
@@ -598,14 +544,13 @@
     }
   }
-#endif
   WRITE_FLAG( pcSPS->getTMVPFlagsPresent()  ? 1 : 0,           "sps_temporal_mvp_enable_flag" );
 #if REF_IDX_MFM
   if( pcSPS->getLayerId() > 0 )
   {
-    assert(pcSPS->getMFMEnabledFlag());
     WRITE_FLAG( pcSPS->getMFMEnabledFlag() ? 1 : 0,          "sps_enh_mfm_enable_flag" );
   }
 #endif
-#if SUPPORT_FOR_VUI
+  WRITE_FLAG( pcSPS->getUseStrongIntraSmoothing(),             "sps_strong_intra_smoothing_enable_flag" );
+
   WRITE_FLAG( pcSPS->getVuiParametersPresentFlag(),             "vui_parameters_present_flag" );
   if (pcSPS->getVuiParametersPresentFlag())
@@ -613,12 +558,5 @@
       codeVUI(pcSPS->getVuiParameters(), pcSPS);
   }
-#endif
-#if !SPS_AMVP_CLEANUP
-  // AMVP mode for each depth
-  for (Int i = 0; i < pcSPS->getMaxCUDepth(); i++)
-  {
-    xWriteFlag( pcSPS->getAMVPMode(i) ? 1 : 0);
-  }
-#endif
+
   WRITE_FLAG( 0, "sps_extension_flag" );
 }
@@ -626,18 +564,21 @@
 Void TEncCavlc::codeVPS( TComVPS* pcVPS )
 {
-#if VPS_SYNTAX_CHANGES
-  WRITE_CODE( pcVPS->getVPSId(),                    4,        "video_parameter_set_id" );
+  WRITE_CODE( pcVPS->getVPSId(),                    4,        "vps_video_parameter_set_id" );
+  WRITE_CODE( 3,                                    2,        "vps_reserved_three_2bits" );
+#if VPS_RENAME
+  WRITE_CODE( pcVPS->getMaxLayers() - 1,            6,        "vps_max_layers_minus1" );            
+#else
+  WRITE_CODE( 0,                                    6,        "vps_reserved_zero_6bits" );
+#endif
+  WRITE_CODE( pcVPS->getMaxTLayers() - 1,           3,        "vps_max_sub_layers_minus1" );
   WRITE_FLAG( pcVPS->getTemporalNestingFlag(),                "vps_temporal_id_nesting_flag" );
-  WRITE_CODE( 0,                                    2,        "vps_reserved_zero_2bits" );
-  WRITE_CODE( 0,                                    6,        "vps_reserved_zero_6bits" );
-  WRITE_CODE( pcVPS->getMaxTLayers() - 1,           3,        "vps_max_sub_layers_minus1" );
+  assert (pcVPS->getMaxTLayers()>1||pcVPS->getTemporalNestingFlag());
+  WRITE_CODE( 0xffff,                              16,        "vps_reserved_ffff_16bits" );
   codePTL( pcVPS->getPTL(), true, pcVPS->getMaxTLayers() - 1 );
-  WRITE_CODE( 0,                                   12,        "vps_reserved_zero_12bits" );
-#else
-  WRITE_CODE( pcVPS->getMaxTLayers() - 1,     3,        "vps_max_temporal_layers_minus1" );
-  WRITE_CODE( pcVPS->getMaxLayers() - 1,      5,        "vps_max_layers_minus1" );
-  WRITE_UVLC( pcVPS->getVPSId(),                        "video_parameter_set_id" );
-  WRITE_FLAG( pcVPS->getTemporalNestingFlag(),          "vps_temporal_id_nesting_flag" );
-#endif
+#if SIGNAL_BITRATE_PICRATE_IN_VPS
+  codeBitratePicRateInfo(pcVPS->getBitratePicrateInfo(), 0, pcVPS->getMaxTLayers() - 1);
+#endif  
+  const Bool subLayerOrderingInfoPresentFlag = 1;
+  WRITE_FLAG(subLayerOrderingInfoPresentFlag,              "vps_sub_layer_ordering_info_present_flag");
   for(UInt i=0; i <= pcVPS->getMaxTLayers()-1; i++)
   {
@@ -645,11 +586,82 @@
     WRITE_UVLC( pcVPS->getNumReorderPics(i),               "vps_num_reorder_pics[i]" );
     WRITE_UVLC( pcVPS->getMaxLatencyIncrease(i),           "vps_max_latency_increase[i]" );
-  }
-#if VPS_SYNTAX_CHANGES
-  WRITE_UVLC( 0,                                           "vps_num_hrd_parameters" );
-  // hrd_parameters
-#endif
+    if (!subLayerOrderingInfoPresentFlag)
+    {
+      break;
+    }
+  }
+
+#if VPS_RENAME
+  assert( pcVPS->getNumHrdParameters() <= MAX_VPS_LAYER_SETS_PLUS1 );
+  assert( pcVPS->getMaxLayerId() < MAX_VPS_LAYER_ID_PLUS1 );
+#if !VPS_EXTN_OP_LAYER_SETS     // num layer sets set in TAppEncTop.cpp
+  pcVPS->setNumLayerSets(1);
+#endif
+  WRITE_CODE( pcVPS->getMaxLayerId(), 6,                       "vps_max_layer_id" );
+  WRITE_UVLC( pcVPS->getNumLayerSets() - 1,                 "vps_num_layer_sets_minus1" );
+  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getNumLayerSets() - 1 ); opsIdx ++ )
+  {
+    // Operation point set
+    for( UInt i = 0; i <= pcVPS->getMaxLayerId(); i ++ )
+#else
+  assert( pcVPS->getNumHrdParameters() <= MAX_VPS_NUM_HRD_PARAMETERS );
+  assert( pcVPS->getMaxNuhReservedZeroLayerId() < MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1 );
+  WRITE_CODE( pcVPS->getMaxNuhReservedZeroLayerId(), 6,     "vps_max_nuh_reserved_zero_layer_id" );
+  pcVPS->setMaxOpSets(1);
+  WRITE_UVLC( pcVPS->getMaxOpSets() - 1,                    "vps_max_op_sets_minus1" );
+  for( UInt opsIdx = 1; opsIdx <= ( pcVPS->getMaxOpSets() - 1 ); opsIdx ++ )
+  {
+    // Operation point set
+    for( UInt i = 0; i <= pcVPS->getMaxNuhReservedZeroLayerId(); i ++ )
+#endif
+    {
+#if !VPS_EXTN_OP_LAYER_SETS     // layer Id include flag set in TAppEncTop.cpp
+      // Only applicable for version 1
+      pcVPS->setLayerIdIncludedFlag( true, opsIdx, i );
+#endif
+      WRITE_FLAG( pcVPS->getLayerIdIncludedFlag( opsIdx, i ) ? 1 : 0, "layer_id_included_flag[opsIdx][i]" );
+    }
+  }
+#if L0043_TIMING_INFO
+  TimingInfo *timingInfo = pcVPS->getTimingInfo();
+  WRITE_FLAG(timingInfo->getTimingInfoPresentFlag(),          "vps_timing_info_present_flag");
+  if(timingInfo->getTimingInfoPresentFlag())
+  {
+    WRITE_CODE(timingInfo->getNumUnitsInTick(), 32,           "vps_num_units_in_tick");
+    WRITE_CODE(timingInfo->getTimeScale(),      32,           "vps_time_scale");
+    WRITE_FLAG(timingInfo->getPocProportionalToTimingFlag(),  "vps_poc_proportional_to_timing_flag");
+    if(timingInfo->getPocProportionalToTimingFlag())
+    {
+      WRITE_UVLC(timingInfo->getNumTicksPocDiffOneMinus1(),   "vps_num_ticks_poc_diff_one_minus1");
+    }
+#endif
+    pcVPS->setNumHrdParameters( 0 );
+    WRITE_UVLC( pcVPS->getNumHrdParameters(),                 "vps_num_hrd_parameters" );
+
+    if( pcVPS->getNumHrdParameters() > 0 )
+    {
+      pcVPS->createHrdParamBuffer();
+    }
+    for( UInt i = 0; i < pcVPS->getNumHrdParameters(); i ++ )
+    {
+      // Only applicable for version 1
+      pcVPS->setHrdOpSetIdx( 0, i );
+      WRITE_UVLC( pcVPS->getHrdOpSetIdx( i ),                "hrd_op_set_idx" );
+      if( i > 0 )
+      {
+        WRITE_FLAG( pcVPS->getCprmsPresentFlag( i ) ? 1 : 0, "cprms_present_flag[i]" );
+      }
+      codeHrdParameters(pcVPS->getHrdParameters(i), pcVPS->getCprmsPresentFlag( i ), pcVPS->getMaxTLayers() - 1);
+    }
+#if L0043_TIMING_INFO
+  }
+#endif
+#if !VPS_EXTNS
   WRITE_FLAG( 0,                     "vps_extension_flag" );
-  
+#else
+  WRITE_FLAG( 1,                     "vps_extension_flag" );
+  codeVPSExtension(pcVPS);
+  WRITE_FLAG( 0,                     "vps_extension2_flag" );   // Flag value of 1 reserved
+#endif  
   //future extensions here..
   
@@ -657,4 +669,78 @@
 }
 
+#if VPS_EXTNS
+Void TEncCavlc::codeVPSExtension (TComVPS *vps)
+{
+  // ... More syntax elements to be written here
+#if VPS_EXTN_MASK_AND_DIM_INFO
+  UInt i = 0, j = 0;
+
+  WRITE_FLAG( vps->getAvcBaseLayerFlag(),              "avc_base_layer_flag" );
+  WRITE_FLAG( vps->getSplittingFlag(),                 "splitting_flag" );
+
+  for(i = 0; i < MAX_VPS_NUM_SCALABILITY_TYPES; i++)
+  {
+    WRITE_FLAG( vps->getScalabilityMask(i),            "scalability_mask[i]" );
+  }
+
+  for(j = 0; j < vps->getNumScalabilityTypes(); j++)
+  {
+    WRITE_CODE( vps->getDimensionIdLen(j) - 1, 3,      "dimension_id_len_minus1[j]" );
+  }
+
+  WRITE_FLAG( vps->getNuhLayerIdPresentFlag(),         "vps_nuh_layer_id_present_flag" );
+  for(i = 1; i < vps->getMaxLayers(); i++)
+  {
+    if( vps->getNuhLayerIdPresentFlag() )
+    {
+      WRITE_CODE( vps->getLayerIdInNuh(i),     6,      "layer_id_in_nuh[i]" );
+    }
+    for(j = 0; j < vps->getNumScalabilityTypes(); j++)
+    {
+      UInt bits = vps->getDimensionIdLen(j);
+      WRITE_CODE( vps->getDimensionId(i, j),   bits,   "dimension_id[i][j]" );
+    }
+  }
+#endif
+#if VPS_EXTN_PROFILE_INFO
+  // Profile-tier-level signalling
+  for(Int idx = 1; idx <= vps->getNumLayerSets() - 1; idx++)
+  {
+    WRITE_FLAG( vps->getProfilePresentFlag(idx),       "vps_profile_present_flag[i]" );
+    if( !vps->getProfilePresentFlag(idx) )
+    {
+      WRITE_UVLC( vps->getProfileLayerSetRef(idx) - 1, "vps_profile_layer_set_ref_minus1[i]" );
+    }
+    codePTL( vps->getPTLForExtn(idx), vps->getProfilePresentFlag(idx), vps->getMaxTLayers() - 1 );
+  }
+#endif
+#if VPS_EXTN_OP_LAYER_SETS
+  // Target output layer signalling
+  WRITE_UVLC( vps->getNumOutputLayerSets(),            "vps_num_output_layer_sets");
+  for(i = 0; i < vps->getNumOutputLayerSets(); i++)
+  {
+    WRITE_UVLC( vps->getOutputLayerSetIdx(i),           "vps_output_layer_set_idx[i]");
+    Int lsIdx = vps->getOutputLayerSetIdx(i);
+    for(j = 0; j <= vps->getMaxLayerId(); j++)
+    {
+      if(vps->getLayerIdIncludedFlag(lsIdx, j))
+      {
+        WRITE_FLAG( vps->getOutputLayerFlag(lsIdx, j), "vps_output_layer_flag[lsIdx][j]");
+      }
+    }
+  }
+#endif
+#if VPS_EXTN_DIRECT_REF_LAYERS
+  for( Int layerCtr = 1; layerCtr <= vps->getMaxLayers() - 1; layerCtr++)
+  {
+    for( Int refLayerCtr = 0; refLayerCtr < layerCtr; refLayerCtr++)
+    {
+      WRITE_FLAG(vps->getDirectDependencyFlag(layerCtr, refLayerCtr), "direct_dependency_flag[i][j]" );
+    }
+  }
+#endif
+}
+#endif
+
 Void TEncCavlc::codeSliceHeader         ( TComSlice* pcSlice )
 {
@@ -664,84 +750,49 @@
 
   //calculate number of bits required for slice address
-  Int maxAddrOuter = pcSlice->getPic()->getNumCUsInFrame();
-  Int reqBitsOuter = 0;
-  while(maxAddrOuter>(1<<reqBitsOuter)) 
-  {
-    reqBitsOuter++;
-  }
-  Int maxAddrInner = pcSlice->getPic()->getNumPartInCU()>>(2);
-#if REMOVE_FGS
-  maxAddrInner = 1;
-#else
-  maxAddrInner = (1<<(pcSlice->getPPS()->getSliceGranularity()<<1));
-#endif
-  Int reqBitsInner = 0;
-  
-  while(maxAddrInner>(1<<reqBitsInner))
-  {
-    reqBitsInner++;
-  }
-  Int lCUAddress;
-  Int innerAddress;
+  Int maxSliceSegmentAddress = pcSlice->getPic()->getNumCUsInFrame();
+  Int bitsSliceSegmentAddress = 0;
+  while(maxSliceSegmentAddress>(1<<bitsSliceSegmentAddress)) 
+  {
+    bitsSliceSegmentAddress++;
+  }
+  Int ctuAddress;
   if (pcSlice->isNextSlice())
   {
     // Calculate slice address
-    lCUAddress = (pcSlice->getSliceCurStartCUAddr()/pcSlice->getPic()->getNumPartInCU());
-#if REMOVE_FGS
-    innerAddress = 0;
-#else
-    innerAddress = (pcSlice->getSliceCurStartCUAddr()%(pcSlice->getPic()->getNumPartInCU()))>>((pcSlice->getSPS()->getMaxCUDepth()-pcSlice->getPPS()->getSliceGranularity())<<1);
-#endif
+    ctuAddress = (pcSlice->getSliceCurStartCUAddr()/pcSlice->getPic()->getNumPartInCU());
   }
   else
   {
     // Calculate slice address
-    lCUAddress = (pcSlice->getDependentSliceCurStartCUAddr()/pcSlice->getPic()->getNumPartInCU());
-#if REMOVE_FGS
-    innerAddress = 0;
-#else
-    innerAddress = (pcSlice->getDependentSliceCurStartCUAddr()%(pcSlice->getPic()->getNumPartInCU()))>>((pcSlice->getSPS()->getMaxCUDepth()-pcSlice->getPPS()->getSliceGranularity())<<1);
-#endif
-    
+    ctuAddress = (pcSlice->getSliceSegmentCurStartCUAddr()/pcSlice->getPic()->getNumPartInCU());
   }
   //write slice address
-  Int address = (pcSlice->getPic()->getPicSym()->getCUOrderMap(lCUAddress) << reqBitsInner) + innerAddress;
-  WRITE_FLAG( address==0, "first_slice_in_pic_flag" );
-#if SPLICING_FRIENDLY_PARAMS
+  Int sliceSegmentAddress = pcSlice->getPic()->getPicSym()->getCUOrderMap(ctuAddress);
+
+  WRITE_FLAG( sliceSegmentAddress==0, "first_slice_segment_in_pic_flag" );
   if ( pcSlice->getRapPicFlag() )
   {
     WRITE_FLAG( 0, "no_output_of_prior_pics_flag" );
   }
-#endif
-  WRITE_UVLC( pcSlice->getPPS()->getPPSId(), "pic_parameter_set_id" );
-  if(address>0) 
-  {
-    WRITE_CODE( address, reqBitsOuter+reqBitsInner, "slice_address" );
-  }
-
-#if SLICEHEADER_SYNTAX_FIX
-  pcSlice->setDependentSliceFlag(!pcSlice->isNextSlice());
-  if ( pcSlice->getPPS()->getDependentSliceEnabledFlag() && (address!=0) )
-  {
-    WRITE_FLAG( pcSlice->getDependentSliceFlag() ? 1 : 0, "dependent_slice_flag" );
-  }
-  if ( !pcSlice->getDependentSliceFlag() )
-  {
-#endif
+  WRITE_UVLC( pcSlice->getPPS()->getPPSId(), "slice_pic_parameter_set_id" );
+  pcSlice->setDependentSliceSegmentFlag(!pcSlice->isNextSlice());
+  if ( pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag() && (sliceSegmentAddress!=0) )
+  {
+    WRITE_FLAG( pcSlice->getDependentSliceSegmentFlag() ? 1 : 0, "dependent_slice_segment_flag" );
+  }
+  if(sliceSegmentAddress>0)
+  {
+    WRITE_CODE( sliceSegmentAddress, bitsSliceSegmentAddress, "slice_segment_address" );
+  }
+  if ( !pcSlice->getDependentSliceSegmentFlag() )
+  {
+    for (Int i = 0; i < pcSlice->getPPS()->getNumExtraSliceHeaderBits(); i++)
+    {
+      assert(!!"slice_reserved_undetermined_flag[]");
+      WRITE_FLAG(0, "slice_reserved_undetermined_flag[]");
+    }
 
     WRITE_UVLC( pcSlice->getSliceType(),       "slice_type" );
 
-#if !SLICEHEADER_SYNTAX_FIX
-  Bool bDependentSlice = (!pcSlice->isNextSlice());
-  WRITE_FLAG( bDependentSlice ? 1 : 0, "dependent_slice_flag" );
-  
-#if DEPENDENT_SLICES
-  if( pcSlice->getPPS()->getDependentSliceEnabledFlag() && bDependentSlice )
-    return;
-#endif
-
-  if (!bDependentSlice)
-  {
-#endif
     if( pcSlice->getPPS()->getOutputFlagPresentFlag() )
     {
@@ -754,26 +805,5 @@
     //   colour_plane_id                                      u(2)
 
-#if !SPLICING_FRIENDLY_PARAMS
-    if(   pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR
-#if SUPPORT_FOR_RAP_N_LP
-       || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_IDR_N_LP
-       || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA_N_LP
-#endif
-       || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLANT
-       || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_BLA
-#if !NAL_UNIT_TYPES_J1003_D7
-       || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRANT
-#endif
-       || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_CRA )
-    {
-      WRITE_UVLC( 0, "rap_pic_id" );
-      WRITE_FLAG( 0, "no_output_of_prior_pics_flag" );
-    }
-#endif
-#if SUPPORT_FOR_RAP_N_LP
     if( !pcSlice->getIdrPicFlag() )
-#else
-    if( pcSlice->getNalUnitType() != NAL_UNIT_CODED_SLICE_IDR )
-#endif
     {
       Int picOrderCntLSB = (pcSlice->getPOC()-pcSlice->getLastIDR()+(1<<pcSlice->getSPS()->getBitsForPOC()))%(1<<pcSlice->getSPS()->getBitsForPOC());
@@ -783,18 +813,21 @@
       {
         WRITE_FLAG( 0, "short_term_ref_pic_set_sps_flag");
-#if J0234_INTER_RPS_SIMPL
-        codeShortTermRefPicSet(pcSlice->getSPS(), rps, true);
-#else
-        codeShortTermRefPicSet(pcSlice->getSPS(), rps);
-#endif
+        codeShortTermRefPicSet(pcSlice->getSPS(), rps, true, pcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets());
       }
       else
       {
         WRITE_FLAG( 1, "short_term_ref_pic_set_sps_flag");
-        WRITE_UVLC( pcSlice->getRPSidx(), "short_term_ref_pic_set_idx" );
+        Int numBits = 0;
+        while ((1 << numBits) < pcSlice->getSPS()->getRPSList()->getNumberOfReferencePictureSets())
+        {
+          numBits++;
+        }
+        if (numBits > 0)
+        {
+          WRITE_CODE( pcSlice->getRPSidx(), numBits, "short_term_ref_pic_set_idx" );          
+        }
       }
       if(pcSlice->getSPS()->getLongTermRefsPresent())
       {
-#if LTRP_IN_SPS
         Int numLtrpInSH = rps->getNumberOfLongtermPictures();
         Int ltrpInSPS[MAX_NUM_REF_PICS];
@@ -816,5 +849,5 @@
         numLtrpInSH -= numLtrpInSPS;
 
-        Int bitsForLtrpInSPS = 1;
+        Int bitsForLtrpInSPS = 0;
         while (pcSlice->getSPS()->getNumLongTermRefPicSPS() > (1 << bitsForLtrpInSPS))
         {
@@ -826,7 +859,4 @@
         }
         WRITE_UVLC( numLtrpInSH, "num_long_term_pics");
-#else
-        WRITE_UVLC( rps->getNumberOfLongtermPictures(), "num_long_term_pics");
-#endif
         // Note that the LSBs of the LT ref. pic. POCs must be sorted before.
         // Not sorted here because LT ref indices will be used in setRefPicList()
@@ -835,8 +865,10 @@
         for(Int i=rps->getNumberOfPictures()-1 ; i > offset-1; i--)
         {
-#if LTRP_IN_SPS
           if (counter < numLtrpInSPS)
           {
-            WRITE_CODE( ltrpInSPS[counter], bitsForLtrpInSPS, "lt_idx_sps[i]");
+            if (bitsForLtrpInSPS > 0)
+            {
+              WRITE_CODE( ltrpInSPS[counter], bitsForLtrpInSPS, "lt_idx_sps[i]");              
+            }
           }
           else 
@@ -845,7 +877,4 @@
             WRITE_FLAG( rps->getUsed(i), "used_by_curr_pic_lt_flag"); 
           }
-#else
-          WRITE_CODE( rps->getPocLSBLT(i), pcSlice->getSPS()->getBitsForPOC(), "poc_lsb_lt");
-#endif
           WRITE_FLAG( rps->getDeltaPocMSBPresentFlag(i), "delta_poc_msb_present_flag");
 
@@ -853,11 +882,6 @@
           {
             Bool deltaFlag = false;
-#if LTRP_IN_SPS
             //  First LTRP from SPS                 ||  First LTRP from SH                              || curr LSB            != prev LSB
             if( (i == rps->getNumberOfPictures()-1) || (i == rps->getNumberOfPictures()-1-numLtrpInSPS) || (rps->getPocLSBLT(i) != prevLSB) )
-#else
-            //   First LTRP                         ||  curr LSB            != prev LSB
-            if( (i == rps->getNumberOfPictures()-1) || (rps->getPocLSBLT(i) != prevLSB) )
-#endif
             {
               deltaFlag = true;
@@ -876,48 +900,27 @@
             prevDeltaMSB = rps->getDeltaPocMSBCycleLT(i);
           }
-#if !LTRP_IN_SPS
-          WRITE_FLAG( rps->getUsed(i), "used_by_curr_pic_lt_flag"); 
-#endif
-        }
-      }
-    }
-#if REMOVE_ALF
+        }
+      }
+      if (pcSlice->getSPS()->getTMVPFlagsPresent())
+      {
+        WRITE_FLAG( pcSlice->getEnableTMVPFlag() ? 1 : 0, "slice_temporal_mvp_enable_flag" );
+      }
+    }
     if(pcSlice->getSPS()->getUseSAO())
-#else
-    if(pcSlice->getSPS()->getUseSAO() || pcSlice->getSPS()->getUseALF())
-#endif
     {
       if (pcSlice->getSPS()->getUseSAO())
       {
          WRITE_FLAG( pcSlice->getSaoEnabledFlag(), "slice_sao_luma_flag" );
-#if !SAO_LUM_CHROMA_ONOFF_FLAGS
-         if (pcSlice->getSaoEnabledFlag() )
-#endif
          {
-#if REMOVE_APS
            SAOParam *saoParam = pcSlice->getPic()->getPicSym()->getSaoParam();
-#else
-           SAOParam *saoParam = pcSlice->getAPS()->getSaoParam();
-#endif
-#if SAO_TYPE_SHARING 
           WRITE_FLAG( saoParam->bSaoFlag[1], "slice_sao_chroma_flag" );
-#else
-          WRITE_FLAG( saoParam->bSaoFlag[1], "SAO on/off flag for Cb in slice header" );
-          WRITE_FLAG( saoParam->bSaoFlag[2], "SAO on/off flag for Cr in slice header" );
-#endif
          }
       }
-#if !REMOVE_APS
-      WRITE_UVLC( pcSlice->getAPS()->getAPSID(), "aps_id");
-#endif
     }
 
     //check if numrefidxes match the defaults. If not, override
+
     if (!pcSlice->isIntra())
     {
-      if (pcSlice->getSPS()->getTMVPFlagsPresent())
-      {
-        WRITE_FLAG( pcSlice->getEnableTMVPFlag(), "enable_temporal_mvp_flag" );
-      }
       Bool overrideFlag = (pcSlice->getNumRefIdx( REF_PIC_LIST_0 )!=pcSlice->getPPS()->getNumRefIdxL0DefaultActive()||(pcSlice->isInterB()&&pcSlice->getNumRefIdx( REF_PIC_LIST_1 )!=pcSlice->getPPS()->getNumRefIdxL1DefaultActive()));
       WRITE_FLAG( overrideFlag ? 1 : 0,                               "num_ref_idx_active_override_flag");
@@ -940,5 +943,6 @@
       pcSlice->setNumRefIdx(REF_PIC_LIST_1, 0);
     }
-    if( pcSlice->getSPS()->getListsModificationPresentFlag() )
+
+    if( pcSlice->getPPS()->getListsModificationPresentFlag() && pcSlice->getNumRpsCurrTempList() > 1)
     {
       TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
@@ -986,7 +990,4 @@
       }
     }
-#if !SLICEHEADER_SYNTAX_FIX
-  }
-#endif
     
     if (pcSlice->isInterB())
@@ -1007,9 +1008,4 @@
     }
 
-#if !SLICEHEADER_SYNTAX_FIX
-  // if( !lightweight_slice_flag ) {
-  if (!bDependentSlice)
-  {
-#else
     if ( pcSlice->getEnableTMVPFlag() )
     {
@@ -1031,9 +1027,10 @@
     }
     assert(pcSlice->getMaxNumMergeCand()<=MRG_MAX_NUM_CANDS);
-    WRITE_UVLC(MRG_MAX_NUM_CANDS - pcSlice->getMaxNumMergeCand(), "five_minus_max_num_merge_cand");
-#endif
+    if (!pcSlice->isIntra())
+    {
+      WRITE_UVLC(MRG_MAX_NUM_CANDS - pcSlice->getMaxNumMergeCand(), "five_minus_max_num_merge_cand");
+    }
     Int iCode = pcSlice->getSliceQp() - ( pcSlice->getPPS()->getPicInitQPMinus26() + 26 );
     WRITE_SVLC( iCode, "slice_qp_delta" ); 
-#if CHROMA_QP_EXTENSION
     if (pcSlice->getPPS()->getSliceChromaQpFlag())
     {
@@ -1043,5 +1040,4 @@
       WRITE_SVLC( iCode, "slice_qp_delta_cr" );
     }
-#endif
     if (pcSlice->getPPS()->getDeblockingFilterControlPresentFlag())
     {
@@ -1055,83 +1051,24 @@
         if(!pcSlice->getDeblockingFilterDisable())
         {
-          WRITE_SVLC (pcSlice->getDeblockingFilterBetaOffsetDiv2(), "beta_offset_div2");
-          WRITE_SVLC (pcSlice->getDeblockingFilterTcOffsetDiv2(),   "tc_offset_div2");
-        }
-      }
-    }
-#if !SLICEHEADER_SYNTAX_FIX
-    if ( pcSlice->getEnableTMVPFlag() )
-    {
-      if ( pcSlice->getSliceType() == B_SLICE )
-      {
-        WRITE_FLAG( pcSlice->getColFromL0Flag(), "collocated_from_l0_flag" );
-      }
-
-      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)))
-      {
-        WRITE_UVLC( pcSlice->getColRefIdx(), "collocated_ref_idx" );
-      }
-    }
-    if ( (pcSlice->getPPS()->getUseWP() && pcSlice->getSliceType()==P_SLICE) || (pcSlice->getPPS()->getWPBiPred() && pcSlice->getSliceType()==B_SLICE) )
-    {
-      xCodePredWeightTable( pcSlice );
-    }
-  }
-    assert(pcSlice->getMaxNumMergeCand()<=MRG_MAX_NUM_CANDS);
-    WRITE_UVLC(MRG_MAX_NUM_CANDS - pcSlice->getMaxNumMergeCand(), "five_minus_max_num_merge_cand");
-#endif
-
-#if !REMOVE_ALF
-  if (!bDependentSlice)
-  {
-    if (pcSlice->getSPS()->getUseALF())
-    {
-      char syntaxString[50];
-      for(Int compIdx=0; compIdx< 3; compIdx++)
-      {
-        sprintf(syntaxString, "alf_slice_filter_flag[%d]", compIdx);
-        WRITE_FLAG( pcSlice->getAlfEnabledFlag(compIdx)?1:0, syntaxString );
-      }
-    }
-  }
-#endif
-#if !SLICEHEADER_SYNTAX_FIX
-  if(!bDependentSlice)
-  {
-#endif
-#if !REMOVE_ALF
-    Bool isAlfEnabled = (!pcSlice->getSPS()->getUseALF())?(false):(pcSlice->getAlfEnabledFlag(0)||pcSlice->getAlfEnabledFlag(1)||pcSlice->getAlfEnabledFlag(2));
-#endif
-#if !SAO_LUM_CHROMA_ONOFF_FLAGS
-    Bool isSAOEnabled = (!pcSlice->getSPS()->getUseSAO())?(false):(pcSlice->getSaoEnabledFlag());
-#else
+          WRITE_SVLC (pcSlice->getDeblockingFilterBetaOffsetDiv2(), "slice_beta_offset_div2");
+          WRITE_SVLC (pcSlice->getDeblockingFilterTcOffsetDiv2(),   "slice_tc_offset_div2");
+        }
+      }
+    }
+
     Bool isSAOEnabled = (!pcSlice->getSPS()->getUseSAO())?(false):(pcSlice->getSaoEnabledFlag()||pcSlice->getSaoEnabledFlagChroma());
-#endif
     Bool isDBFEnabled = (!pcSlice->getDeblockingFilterDisable());
 
-#if REMOVE_ALF
-#if MOVE_LOOP_FILTER_SLICES_FLAG
     if(pcSlice->getPPS()->getLoopFilterAcrossSlicesEnabledFlag() && ( isSAOEnabled || isDBFEnabled ))
-#else
-    if(pcSlice->getSPS()->getLFCrossSliceBoundaryFlag() && ( isSAOEnabled || isDBFEnabled ))
-#endif
-#else
-    if(pcSlice->getSPS()->getLFCrossSliceBoundaryFlag() && ( isAlfEnabled || isSAOEnabled || isDBFEnabled ))
-#endif
     {
       WRITE_FLAG(pcSlice->getLFCrossSliceBoundaryFlag()?1:0, "slice_loop_filter_across_slices_enabled_flag");
     }
   }
-#if SLICE_HEADER_EXTENSION
   if(pcSlice->getPPS()->getSliceHeaderExtensionPresentFlag())
   {
     WRITE_UVLC(0,"slice_header_extension_length");
   }
-#endif
-}
-
-#if PROFILE_TIER_LEVEL_SYNTAX
+}
+
 Void TEncCavlc::codePTL( TComPTL* pcPTL, Bool profilePresentFlag, Int maxNumSubLayersMinus1)
 {
@@ -1142,8 +1079,34 @@
   WRITE_CODE( pcPTL->getGeneralPTL()->getLevelIdc(), 8, "general_level_idc" );
 
+#if L0363_BYTE_ALIGN
+  for (Int i = 0; i < maxNumSubLayersMinus1; i++)
+  {
+    if(profilePresentFlag)
+    {
+      WRITE_FLAG( pcPTL->getSubLayerProfilePresentFlag(i), "sub_layer_profile_present_flag[i]" );
+    }
+    
+    WRITE_FLAG( pcPTL->getSubLayerLevelPresentFlag(i),   "sub_layer_level_present_flag[i]" );
+  }
+  
+  if (maxNumSubLayersMinus1 > 0)
+  {
+    for (Int i = maxNumSubLayersMinus1; i < 8; i++)
+    {
+      WRITE_CODE(0, 2, "reserved_zero_2bits");
+    }
+  }
+#endif
+  
   for(Int i = 0; i < maxNumSubLayersMinus1; i++)
   {
-    WRITE_FLAG( pcPTL->getSubLayerProfilePresentFlag(i), "sub_layer_profile_present_flag[i]" );
+#if !L0363_BYTE_ALIGN
+    if(profilePresentFlag)
+    {
+      WRITE_FLAG( pcPTL->getSubLayerProfilePresentFlag(i), "sub_layer_profile_present_flag[i]" );
+    }
+
     WRITE_FLAG( pcPTL->getSubLayerLevelPresentFlag(i),   "sub_layer_level_present_flag[i]" );
+#endif
     if( profilePresentFlag && pcPTL->getSubLayerProfilePresentFlag(i) )
     {
@@ -1165,8 +1128,42 @@
     WRITE_FLAG( ptl->getProfileCompatibilityFlag(j), "XXX_profile_compatibility_flag[][j]");   
   }
-  WRITE_CODE(0 , 16, "XXX_reserved_zero_16bits[]");  
-}
-#endif
-
+
+#if L0046_CONSTRAINT_FLAGS
+  WRITE_FLAG(ptl->getProgressiveSourceFlag(),   "general_progressive_source_flag");
+  WRITE_FLAG(ptl->getInterlacedSourceFlag(),    "general_interlaced_source_flag");
+  WRITE_FLAG(ptl->getNonPackedConstraintFlag(), "general_non_packed_constraint_flag");
+  WRITE_FLAG(ptl->getFrameOnlyConstraintFlag(), "general_frame_only_constraint_flag");
+  
+  WRITE_CODE(0 , 16, "XXX_reserved_zero_44bits[0..15]");
+  WRITE_CODE(0 , 16, "XXX_reserved_zero_44bits[16..31]");
+  WRITE_CODE(0 , 12, "XXX_reserved_zero_44bits[32..43]");
+#elif L0363_MORE_BITS
+  WRITE_CODE(0 , 16, "XXX_reserved_zero_48bits[0..15]");
+  WRITE_CODE(0 , 16, "XXX_reserved_zero_48bits[16..31]");
+  WRITE_CODE(0 , 16, "XXX_reserved_zero_48bits[32..47]");
+#else
+  WRITE_CODE(0 , 16, "XXX_reserved_zero_16bits[]");
+#endif
+}
+#if SIGNAL_BITRATE_PICRATE_IN_VPS
+Void TEncCavlc::codeBitratePicRateInfo(TComBitRatePicRateInfo *info, Int tempLevelLow, Int tempLevelHigh)
+{
+  for(Int i = tempLevelLow; i <= tempLevelHigh; i++)
+  {
+    WRITE_FLAG( info->getBitRateInfoPresentFlag(i),  "bit_rate_info_present_flag[i]" );
+    WRITE_FLAG( info->getPicRateInfoPresentFlag(i),  "pic_rate_info_present_flag[i]" );
+    if(info->getBitRateInfoPresentFlag(i))
+    {
+      WRITE_CODE( info->getAvgBitRate(i), 16, "avg_bit_rate[i]" );
+      WRITE_CODE( info->getMaxBitRate(i), 16, "max_bit_rate[i]" );
+    }
+    if(info->getPicRateInfoPresentFlag(i))
+    {
+      WRITE_CODE( info->getConstantPicRateIdc(i),  2, "constant_pic_rate_idc[i]" );
+      WRITE_CODE( info->getAvgPicRate(i),         16, "avg_pic_rate[i]"          );
+    }
+  }
+}
+#endif  
 /**
  - write wavefront substreams sizes for the slice header.
@@ -1176,10 +1173,5 @@
 Void  TEncCavlc::codeTilesWPPEntryPoint( TComSlice* pSlice )
 {
-#if TILES_WPP_ENTROPYSLICES_FLAGS
   if (!pSlice->getPPS()->getTilesEnabledFlag() && !pSlice->getPPS()->getEntropyCodingSyncEnabledFlag())
-#else
-  Int tilesOrEntropyCodingSyncIdc = pSlice->getPPS()->getTilesOrEntropyCodingSyncIdc();
-  if ( tilesOrEntropyCodingSyncIdc == 0 )
-#endif
   {
     return;
@@ -1188,9 +1180,5 @@
   Int  numZeroSubstreamsAtStartOfSlice  = 0;
   UInt *entryPointOffset = NULL;
-#if TILES_WPP_ENTROPYSLICES_FLAGS
   if ( pSlice->getPPS()->getTilesEnabledFlag() )
-#else
-  if (tilesOrEntropyCodingSyncIdc == 1) // tiles
-#endif
   {
     numEntryPointOffsets = pSlice->getTileLocationCount();
@@ -1213,19 +1201,10 @@
     }
   }
-#if TILES_WPP_ENTROPYSLICES_FLAGS
   else if ( pSlice->getPPS()->getEntropyCodingSyncEnabledFlag() )
-#else
-  else if (tilesOrEntropyCodingSyncIdc == 2) // wavefront
-#endif
   {
     UInt* pSubstreamSizes               = pSlice->getSubstreamSizes();
     Int maxNumParts                       = pSlice->getPic()->getNumPartInCU();
-#if DEPENDENT_SLICES
-    numZeroSubstreamsAtStartOfSlice       = pSlice->getDependentSliceCurStartCUAddr()/maxNumParts/pSlice->getPic()->getFrameWidthInCU();
-    Int  numZeroSubstreamsAtEndOfSlice    = pSlice->getPic()->getFrameHeightInCU()-1 - ((pSlice->getDependentSliceCurEndCUAddr()-1)/maxNumParts/pSlice->getPic()->getFrameWidthInCU());
-#else
-    numZeroSubstreamsAtStartOfSlice       = pSlice->getSliceCurStartCUAddr()/maxNumParts/pSlice->getPic()->getFrameWidthInCU();
-    Int  numZeroSubstreamsAtEndOfSlice    = pSlice->getPic()->getFrameHeightInCU()-1 - ((pSlice->getSliceCurEndCUAddr()-1)/maxNumParts/pSlice->getPic()->getFrameWidthInCU());
-#endif
+    numZeroSubstreamsAtStartOfSlice       = pSlice->getSliceSegmentCurStartCUAddr()/maxNumParts/pSlice->getPic()->getFrameWidthInCU();
+    Int  numZeroSubstreamsAtEndOfSlice    = pSlice->getPic()->getFrameHeightInCU()-1 - ((pSlice->getSliceSegmentCurEndCUAddr()-1)/maxNumParts/pSlice->getPic()->getFrameWidthInCU());
     numEntryPointOffsets                  = pSlice->getPPS()->getNumSubstreams() - numZeroSubstreamsAtStartOfSlice - numZeroSubstreamsAtEndOfSlice - 1;
     pSlice->setNumEntryPointOffsets(numEntryPointOffsets);
@@ -1256,5 +1235,9 @@
   for (UInt idx=0; idx<numEntryPointOffsets; idx++)
   {
+#if L0116_ENTRY_POINT
+    WRITE_CODE(entryPointOffset[ idx ]-1, offsetLenMinus1+1, "entry_point_offset_minus1");
+#else
     WRITE_CODE(entryPointOffset[ idx ], offsetLenMinus1+1, "entry_point_offset");
+#endif
   }
 
@@ -1295,9 +1278,4 @@
 }
 
-Void TEncCavlc::codeApsExtensionFlag ()
-{
-  WRITE_FLAG(0, "aps_extension_flag");
-}
-
 Void TEncCavlc::codeInterModeFlag( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth, UInt uiEncMode )
 {
@@ -1315,11 +1293,4 @@
 }
 
-#if INTRA_BL
-Void TEncCavlc::codeIntraBLFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
-{
-  assert(0);
-}
-
-#endif
 Void TEncCavlc::codeSplitFlag   ( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth )
 {
@@ -1342,28 +1313,24 @@
 }
 
-#if TU_ZERO_CBF_RDO
-Void TEncCavlc::codeQtCbfZero( TComDataCU* pcCU, UInt uiAbsPartIdx, TextType eType, UInt uiTrDepth )
-{
-  assert(0);
-}
-Void TEncCavlc::codeQtRootCbfZero( TComDataCU* pcCU, UInt uiAbsPartIdx )
-{
-  assert(0);
-}
-#endif
-
-Void TEncCavlc::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, UInt uiDepth, TextType eTType )
-{
-  assert(0);
-}
-
-/** Code I_PCM information. 
+Void TEncCavlc::codeQtCbfZero( TComDataCU* pcCU, TextType eType, UInt uiTrDepth )
+{
+  assert(0);
+}
+Void TEncCavlc::codeQtRootCbfZero( TComDataCU* pcCU )
+{
+  assert(0);
+}
+
+Void TEncCavlc::codeTransformSkipFlags (TComDataCU* pcCU, UInt uiAbsPartIdx, UInt width, UInt height, TextType eTType )
+{
+  assert(0);
+}
+
+/** Code I_PCM information.
  * \param pcCU pointer to CU
  * \param uiAbsPartIdx CU index
- * \param numIPCM the number of succesive IPCM blocks with the same size 
- * \param firstIPCMFlag 
  * \returns Void
  */
-Void TEncCavlc::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx, Int numIPCM, Bool firstIPCMFlag)
+Void TEncCavlc::codeIPCMInfo( TComDataCU* pcCU, UInt uiAbsPartIdx )
 {
   assert(0);
@@ -1411,78 +1378,4 @@
   assert(0);
 }
-
-Void TEncCavlc::xGolombEncode(Int coeff, Int k)
-{
-  xWriteEpExGolomb((UInt)abs(coeff), k);
-  if(coeff != 0)
-  {
-    Int sign = (coeff > 0)? 1: 0;
-    xWriteFlag(sign);
-  }
-#if ENC_DEC_TRACE
-  fprintf( g_hTrace, "%8lld  ", g_nSymbolCounter++ );
-  fprintf( g_hTrace, "%-40s se(v) : %d\n", "alf_filt_coeff", coeff ); 
-#endif
-
-}
-
-#if !REMOVE_ALF
-Void TEncCavlc::codeAlfParam(ALFParam* alfParam)
-{
-  char syntaxString[50];
-  sprintf(syntaxString, "alf_aps_filter_flag[%d]", alfParam->componentID);
-  WRITE_FLAG(alfParam->alf_flag, syntaxString);
-  if(alfParam->alf_flag == 0)
-  {
-    return;
-  }
-
-  const Int numCoeff = (Int)ALF_MAX_NUM_COEF;
-  switch(alfParam->componentID)
-  {
-  case ALF_Cb:
-  case ALF_Cr:
-    {
-      for(Int pos=0; pos< numCoeff; pos++)
-      {
-        WRITE_SVLC(alfParam->coeffmulti[0][pos], "alf_filt_coeff");
-      }
-    }
-    break;
-  case ALF_Y:
-    {
-      Int noFilters = min(alfParam->filters_per_group-1, 2);
-      WRITE_UVLC(noFilters, "alf_no_filters_minus1");
-      if(noFilters == 1)
-      {
-        WRITE_UVLC(alfParam->startSecondFilter, "alf_start_second_filter");
-      }
-      else if (noFilters == 2)
-      {
-        Int numMergeFlags = 16;
-        for (Int i=1; i<numMergeFlags; i++) 
-        {
-          WRITE_FLAG(alfParam->filterPattern[i], "alf_filter_pattern");
-        }
-      }
-      for(Int f=0; f< alfParam->filters_per_group; f++)
-      {
-        for(Int pos=0; pos< numCoeff; pos++)
-        {
-          xGolombEncode(alfParam->coeffmulti[f][pos], kTableTabShapes[ALF_CROSS9x7_SQUARE3x3][pos]);
-        }
-      }
-    }
-    break;
-  default:
-    {
-      printf("Not a legal component ID\n");
-      assert(0);
-      exit(-1);
-    }
-  }
-
-}
-#endif
 
 Void TEncCavlc::estBit( estBitsSbacStruct* pcEstBitsCabac, Int width, Int height, TextType eTType )
@@ -1585,7 +1478,5 @@
   Bool            bDenomCoded  = false;
   UInt            uiMode = 0;
-#if NUM_WP_LIMIT
   UInt            uiTotalSignalledWeightFlags = 0;
-#endif
   if ( (pcSlice->getSliceType()==P_SLICE && pcSlice->getPPS()->getUseWP()) || (pcSlice->getSliceType()==B_SLICE && pcSlice->getPPS()->getWPBiPred()) )
   {
@@ -1615,5 +1506,4 @@
         }
         WRITE_FLAG( wp[0].bPresentFlag, "luma_weight_lX_flag" );               // u(1): luma_weight_lX_flag
-#if NUM_WP_LIMIT
         uiTotalSignalledWeightFlags += wp[0].bPresentFlag;
       }
@@ -1631,5 +1521,4 @@
       {
         pcSlice->getWpScaling(eRefPicList, iRefIdx, wp);
-#endif
         if ( wp[0].bPresentFlag ) 
         {
@@ -1641,7 +1530,4 @@
         if ( bChroma ) 
         {
-#if !NUM_WP_LIMIT
-          WRITE_FLAG( wp[1].bPresentFlag, "chroma_weight_lX_flag" );           // u(1): chroma_weight_lX_flag
-#endif
           if ( wp[1].bPresentFlag )
           {
@@ -1651,6 +1537,5 @@
               WRITE_SVLC( iDeltaWeight, "delta_chroma_weight_lX" );            // se(v): delta_chroma_weight_lX
 
-              Int shift = ((1<<(g_uiBitDepth+g_uiBitIncrement-1)));
-              Int pred = ( shift - ( ( shift*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
+              Int pred = ( 128 - ( ( 128*wp[j].iWeight)>>(wp[j].uiLog2WeightDenom) ) );
               Int iDeltaChroma = (wp[j].iOffset - pred);
               WRITE_SVLC( iDeltaChroma, "delta_chroma_offset_lX" );            // se(v): delta_chroma_offset_lX
@@ -1660,7 +1545,5 @@
       }
     }
-#if NUM_WP_LIMIT
     assert(uiTotalSignalledWeightFlags<=24);
-#endif
   }
 }
@@ -1717,9 +1600,5 @@
 {
   Int coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
-#if REMOVE_ZIGZAG_SCAN
   UInt* scan  = (sizeId == 0) ? g_auiSigLastScan [ SCAN_DIAG ] [ 1 ] :  g_sigLastScanCG32x32;
-#else
-  UInt* scan    = g_auiFrameScanXY [ (sizeId == 0)? 1 : 2];
-#endif
   Int nextCoef = SCALING_LIST_START_VALUE;
   Int data;
@@ -1746,8 +1625,7 @@
     }
 }
-#if LTRP_IN_SPS
 Bool TEncCavlc::findMatchingLTRP ( TComSlice* pcSlice, UInt *ltrpsIndex, Int ltrpPOC, Bool usedFlag )
 {
-  // bool state = true, state2 = false;
+  // Bool state = true, state2 = false;
   Int lsb = ltrpPOC % (1<<pcSlice->getSPS()->getBitsForPOC());
   for (Int k = 0; k < pcSlice->getSPS()->getNumLongTermRefPicSPS(); k++)
@@ -1761,5 +1639,4 @@
   return false;
 }
-#endif
 Bool TComScalingList::checkPredMode(UInt sizeId, UInt listId)
 {
@@ -1776,3 +1653,11 @@
   return true;
 }
+
+#if INTRA_BL
+Void TEncCavlc::codeIntraBLFlag( TComDataCU* pcCU, UInt uiAbsPartIdx )
+{
+  assert(0);
+}
+
+#endif
 //! \}
