Index: trunk/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncTop.cpp	(revision 442)
+++ trunk/source/App/TAppEncoder/TAppEncTop.cpp	(revision 494)
@@ -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-2013, ITU/ISO/IEC
@@ -98,10 +98,16 @@
     // Auto generation of the format index
     if( m_acLayerCfg[layer].getRepFormatIdx() == -1 )
-    {
-      // Currently only picture width and height are considred. It has to be updated if different chroma format and bit-depth will are used.
+    {      
       Bool found = false;
       for( UInt idx = 0; idx < layer; idx++ )
       {
-        if( m_acLayerCfg[layer].getSourceWidth() == m_acLayerCfg[idx].getSourceWidth() && m_acLayerCfg[layer].getSourceHeight() == m_acLayerCfg[idx].getSourceHeight() )
+        if( m_acLayerCfg[layer].getSourceWidth() == m_acLayerCfg[idx].getSourceWidth() && m_acLayerCfg[layer].getSourceHeight() == m_acLayerCfg[idx].getSourceHeight()
+#if AUXILIARY_PICTURES
+          && m_acLayerCfg[layer].getChromaFormatIDC() == m_acLayerCfg[idx].getChromaFormatIDC()
+#endif
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+          && m_acLayerCfg[layer].m_internalBitDepthY == m_acLayerCfg[idx].m_internalBitDepthY && m_acLayerCfg[layer].m_internalBitDepthC == m_acLayerCfg[idx].m_internalBitDepthC
+#endif
+          )
         {
           found = true;
@@ -117,5 +123,5 @@
     }
 
-    assert( m_acLayerCfg[layer].getRepFormatIdx() != -1 && "RepFormatIdx not assigned for a layer" );   
+    assert( m_acLayerCfg[layer].getRepFormatIdx() != -1 && "RepFormatIdx not assigned for a layer" );
     vps->setVpsRepFormatIdx( layer, m_acLayerCfg[layer].getRepFormatIdx() );
     maxRepFormatIdx = std::max( m_acLayerCfg[layer].getRepFormatIdx(), maxRepFormatIdx );
@@ -141,11 +147,20 @@
   for(UInt idx=0; idx < vps->getVpsNumRepFormats(); idx++)
   {
-    RepFormat *repFormat = vps->getVpsRepFormat( idx );    
+    RepFormat *repFormat = vps->getVpsRepFormat( idx );
     repFormat->setPicWidthVpsInLumaSamples  ( m_acLayerCfg[mapIdxToLayer[idx]].getSourceWidth()   );
     repFormat->setPicHeightVpsInLumaSamples ( m_acLayerCfg[mapIdxToLayer[idx]].getSourceHeight()  );
+#if AUXILIARY_PICTURES
+    repFormat->setChromaFormatVpsIdc        ( m_acLayerCfg[mapIdxToLayer[idx]].getChromaFormatIDC() );
+#else
     repFormat->setChromaFormatVpsIdc        ( 1                                             );  // Need modification to change for each layer - corresponds to 420
+#endif
     repFormat->setSeparateColourPlaneVpsFlag( 0                                             );  // Need modification to change for each layer
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+    repFormat->setBitDepthVpsLuma           ( getInternalBitDepthY(mapIdxToLayer[idx])      );  // Need modification to change for each layer
+    repFormat->setBitDepthVpsChroma         ( getInternalBitDepthC(mapIdxToLayer[idx])      );  // Need modification to change for each layer
+#else
     repFormat->setBitDepthVpsLuma           ( getInternalBitDepthY()                        );  // Need modification to change for each layer
     repFormat->setBitDepthVpsChroma         ( getInternalBitDepthC()                        );  // Need modification to change for each layer
+#endif
   }
   delete [] mapIdxToLayer;
@@ -153,4 +168,16 @@
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+    //1
+    g_bitDepthY = m_acLayerCfg[layer].m_internalBitDepthY;
+    g_bitDepthC = m_acLayerCfg[layer].m_internalBitDepthC;
+
+    g_uiPCMBitDepthLuma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthY : m_acLayerCfg[layer].m_internalBitDepthY;
+    g_uiPCMBitDepthChroma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthC : m_acLayerCfg[layer].m_internalBitDepthC;
+
+    // Set this to be used in Upsampling filter in function "TComUpsampleFilter::upsampleBasePic"
+    g_bitDepthYLayer[layer] = g_bitDepthY;
+    g_bitDepthCLayer[layer] = g_bitDepthC;
+#endif
     //m_acTEncTop[layer].setVPS(&vps);
     m_acTEncTop[layer].setFrameRate                    ( m_acLayerCfg[layer].getFrameRate() );
@@ -187,6 +214,6 @@
 #endif
 #endif
-    // set layer ID 
-    m_acTEncTop[layer].setLayerId ( layer ); 
+    // set layer ID
+    m_acTEncTop[layer].setLayerId ( layer );
     m_acTEncTop[layer].setNumLayer ( m_numLayers );
     m_acTEncTop[layer].setLayerEnc(m_apcTEncTop);
@@ -196,9 +223,6 @@
     m_acTEncTop[layer].setDecodingRefreshType          ( m_iDecodingRefreshType );
     m_acTEncTop[layer].setGOPSize                      ( m_iGOPSize );
-#if FINAL_RPL_CHANGE_N0082
-    m_acTEncTop[layer].setGopList                      ( m_acLayerCfg[layer].m_GOPListLayer );
-#else
     m_acTEncTop[layer].setGopList                      ( m_GOPList );
-#endif
+
     m_acTEncTop[layer].setExtraRPSs                    ( m_extraRPSs );
     for(Int i = 0; i < MAX_TLAYER; i++)
@@ -220,7 +244,7 @@
     if (layer<m_numLayers-1)
     {
-       m_acTEncTop[layer].setMaxTidIlRefPicsPlus1 ( m_acLayerCfg[layer].getMaxTidIlRefPicsPlus1()); 
-    }
-#endif 
+       m_acTEncTop[layer].setMaxTidIlRefPicsPlus1 ( m_acLayerCfg[layer].getMaxTidIlRefPicsPlus1());
+    }
+#endif
 #if VPS_EXTN_DIRECT_REF_LAYERS
     if(layer)
@@ -241,10 +265,10 @@
       else
       {
-        m_acTEncTop[layer].setNumSamplePredRefLayers   ( m_acLayerCfg[layer].getNumSamplePredRefLayers() );      
+        m_acTEncTop[layer].setNumSamplePredRefLayers   ( m_acLayerCfg[layer].getNumSamplePredRefLayers() );
         for(Int i = 0; i < m_acTEncTop[layer].getNumSamplePredRefLayers(); i++)
         {
           m_acTEncTop[layer].setSamplePredRefLayerId   ( i, m_acLayerCfg[layer].getSamplePredRefLayerId(i));
           m_acTEncTop[layer].setSamplePredEnabledFlag  (m_acLayerCfg[layer].getSamplePredRefLayerId(i), true);
-        }        
+        }
       }
       if(m_acLayerCfg[layer].getNumMotionPredRefLayers() == -1)
@@ -257,10 +281,10 @@
       else
       {
-        m_acTEncTop[layer].setNumMotionPredRefLayers   ( m_acLayerCfg[layer].getNumMotionPredRefLayers() );      
+        m_acTEncTop[layer].setNumMotionPredRefLayers   ( m_acLayerCfg[layer].getNumMotionPredRefLayers() );
         for(Int i = 0; i < m_acTEncTop[layer].getNumMotionPredRefLayers(); i++)
         {
           m_acTEncTop[layer].setMotionPredRefLayerId   ( i, m_acLayerCfg[layer].getMotionPredRefLayerId(i));
           m_acTEncTop[layer].setMotionPredEnabledFlag  (m_acLayerCfg[layer].getMotionPredRefLayerId(i), true);
-        }        
+        }
       }
       Int numDirectRefLayers = 0;
@@ -283,5 +307,5 @@
       else
       {
-        m_acTEncTop[layer].setNumDirectRefLayers       ( m_acLayerCfg[layer].getNumDirectRefLayers() );      
+        m_acTEncTop[layer].setNumDirectRefLayers       ( m_acLayerCfg[layer].getNumDirectRefLayers() );
         for(Int i = 0; i < m_acTEncTop[layer].getNumDirectRefLayers(); i++)
         {
@@ -304,5 +328,5 @@
       else
       {
-        m_acTEncTop[layer].setNumActiveRefLayers       ( m_acLayerCfg[layer].getNumActiveRefLayers() );      
+        m_acTEncTop[layer].setNumActiveRefLayers       ( m_acLayerCfg[layer].getNumActiveRefLayers() );
         for(Int i = 0; i < m_acTEncTop[layer].getNumActiveRefLayers(); i++)
         {
@@ -358,10 +382,17 @@
     m_acTEncTop[layer].setUseRDOQTS                    ( m_useRDOQTS   );
     m_acTEncTop[layer].setRDpenalty                    ( m_rdPenalty );
+#if LAYER_CTB
+    m_acTEncTop[layer].setQuadtreeTULog2MaxSize        ( m_acLayerCfg[layer].m_uiQuadtreeTULog2MaxSize );
+    m_acTEncTop[layer].setQuadtreeTULog2MinSize        ( m_acLayerCfg[layer].m_uiQuadtreeTULog2MinSize );
+    m_acTEncTop[layer].setQuadtreeTUMaxDepthInter      ( m_acLayerCfg[layer].m_uiQuadtreeTUMaxDepthInter );
+    m_acTEncTop[layer].setQuadtreeTUMaxDepthIntra      ( m_acLayerCfg[layer].m_uiQuadtreeTUMaxDepthIntra );
+#else
     m_acTEncTop[layer].setQuadtreeTULog2MaxSize        ( m_uiQuadtreeTULog2MaxSize );
     m_acTEncTop[layer].setQuadtreeTULog2MinSize        ( m_uiQuadtreeTULog2MinSize );
     m_acTEncTop[layer].setQuadtreeTUMaxDepthInter      ( m_uiQuadtreeTUMaxDepthInter );
     m_acTEncTop[layer].setQuadtreeTUMaxDepthIntra      ( m_uiQuadtreeTUMaxDepthIntra );
+#endif
     m_acTEncTop[layer].setUseFastEnc                   ( m_bUseFastEnc  );
-    m_acTEncTop[layer].setUseEarlyCU                   ( m_bUseEarlyCU  ); 
+    m_acTEncTop[layer].setUseEarlyCU                   ( m_bUseEarlyCU  );
     m_acTEncTop[layer].setUseFastDecisionForMerge      ( m_useFastDecisionForMerge  );
     m_acTEncTop[layer].setUseCbfFastMode               ( m_bUseCbfFastMode  );
@@ -383,4 +414,12 @@
     m_acTEncTop[layer].setUseWP                   ( m_useWeightedPred      );
     m_acTEncTop[layer].setWPBiPred                ( m_useWeightedBiPred   );
+#if O0194_WEIGHTED_PREDICTION_CGS
+    if (layer!=0)
+    {
+      // Enable weighted prediction for enhancement layer
+      m_acTEncTop[layer].setUseWP                 ( true   );
+      m_acTEncTop[layer].setWPBiPred              ( true   );
+    }
+#endif
     //====== Parallel Merge Estimation ========
     m_acTEncTop[layer].setLog2ParallelMergeLevelMinus2 ( m_log2ParallelMergeLevel - 2 );
@@ -393,5 +432,9 @@
     m_acTEncTop[layer].setSliceSegmentMode        ( m_sliceSegmentMode         );
     m_acTEncTop[layer].setSliceSegmentArgument    ( m_sliceSegmentArgument     );
+#if LAYER_CTB
+    Int iNumPartInCU = 1<<(m_acLayerCfg[layer].m_uiMaxCUDepth<<1);
+#else
     Int iNumPartInCU = 1<<(m_uiMaxCUDepth<<1);
+#endif
     if(m_sliceSegmentMode==FIXED_NUMBER_OF_LCU)
     {
@@ -417,6 +460,6 @@
     m_acTEncTop[layer].setSaoLcuBoundary (m_saoLcuBoundary);
     m_acTEncTop[layer].setSaoLcuBasedOptimization (m_saoLcuBasedOptimization);
-    m_acTEncTop[layer].setPCMInputBitDepthFlag  ( m_bPCMInputBitDepthFlag); 
-    m_acTEncTop[layer].setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag); 
+    m_acTEncTop[layer].setPCMInputBitDepthFlag  ( m_bPCMInputBitDepthFlag);
+    m_acTEncTop[layer].setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag);
 
     m_acTEncTop[layer].setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
@@ -483,5 +526,5 @@
     if(uiTilesCount == 1)
     {
-      m_bLFCrossTileBoundaryFlag = true; 
+      m_bLFCrossTileBoundaryFlag = true;
     }
     m_acTEncTop[layer].setLFCrossTileBoundaryFlag( m_bLFCrossTileBoundaryFlag );
@@ -519,5 +562,5 @@
     m_acTEncTop[layer].setUseRecalculateQPAccordingToLambda( m_recalculateQPAccordingToLambda );
     m_acTEncTop[layer].setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
-    m_acTEncTop[layer].setActiveParameterSetsSEIEnabled ( m_activeParameterSetsSEIEnabled ); 
+    m_acTEncTop[layer].setActiveParameterSetsSEIEnabled ( m_activeParameterSetsSEIEnabled );
     m_acTEncTop[layer].setVuiParametersPresentFlag( m_vuiParametersPresentFlag );
     m_acTEncTop[layer].setAspectRatioIdc( m_aspectRatioIdc );
@@ -550,5 +593,4 @@
     m_acTEncTop[layer].setLog2MaxMvLengthVertical( m_log2MaxMvLengthVertical );
     m_acTEncTop[layer].setElRapSliceTypeB(layer == 0? 0 : m_elRapSliceBEnabled);
-#if SCALED_REF_LAYER_OFFSETS
     if( layer > 0 )
     {
@@ -557,14 +599,16 @@
       {
         m_acTEncTop[layer].getScaledRefLayerWindow(i).setWindow( 2*m_acLayerCfg[layer].m_scaledRefLayerLeftOffset[i], 2*m_acLayerCfg[layer].m_scaledRefLayerRightOffset[i],
-                                                  2*m_acLayerCfg[layer].m_scaledRefLayerTopOffset[i], 2*m_acLayerCfg[layer].m_scaledRefLayerBottomOffset[i]);  
-      }
-    }
-#endif
+                                                  2*m_acLayerCfg[layer].m_scaledRefLayerTopOffset[i], 2*m_acLayerCfg[layer].m_scaledRefLayerBottomOffset[i]);
+      }
+    }
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
     m_acTEncTop[layer].setAdaptiveResolutionChange( m_adaptiveResolutionChange );
 #endif
+#if AUXILIARY_PICTURES
+    m_acTEncTop[layer].setChromaFormatIDC( m_acLayerCfg[layer].m_chromaFormatIDC );
+#endif
   }
 }
-#else
+#else //SVC_EXTENSION
 Void TAppEncTop::xInitLibCfg()
 {
@@ -671,5 +715,5 @@
   m_cTEncTop.setQuadtreeTUMaxDepthIntra      ( m_uiQuadtreeTUMaxDepthIntra );
   m_cTEncTop.setUseFastEnc                   ( m_bUseFastEnc  );
-  m_cTEncTop.setUseEarlyCU                   ( m_bUseEarlyCU  ); 
+  m_cTEncTop.setUseEarlyCU                   ( m_bUseEarlyCU  );
   m_cTEncTop.setUseFastDecisionForMerge      ( m_useFastDecisionForMerge  );
   m_cTEncTop.setUseCbfFastMode            ( m_bUseCbfFastMode  );
@@ -725,6 +769,6 @@
   m_cTEncTop.setSaoLcuBoundary (m_saoLcuBoundary);
   m_cTEncTop.setSaoLcuBasedOptimization (m_saoLcuBasedOptimization);
-  m_cTEncTop.setPCMInputBitDepthFlag  ( m_bPCMInputBitDepthFlag); 
-  m_cTEncTop.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag); 
+  m_cTEncTop.setPCMInputBitDepthFlag  ( m_bPCMInputBitDepthFlag);
+  m_cTEncTop.setPCMFilterDisableFlag  ( m_bPCMFilterDisableFlag);
 
   m_cTEncTop.setDecodedPictureHashSEIEnabled(m_decodedPictureHashSEIEnabled);
@@ -783,5 +827,5 @@
   if(uiTilesCount == 1)
   {
-    m_bLFCrossTileBoundaryFlag = true; 
+    m_bLFCrossTileBoundaryFlag = true;
   }
   m_cTEncTop.setLFCrossTileBoundaryFlag( m_bLFCrossTileBoundaryFlag );
@@ -809,5 +853,5 @@
   m_cTEncTop.setUseRecalculateQPAccordingToLambda( m_recalculateQPAccordingToLambda );
   m_cTEncTop.setUseStrongIntraSmoothing( m_useStrongIntraSmoothing );
-  m_cTEncTop.setActiveParameterSetsSEIEnabled ( m_activeParameterSetsSEIEnabled ); 
+  m_cTEncTop.setActiveParameterSetsSEIEnabled ( m_activeParameterSetsSEIEnabled );
   m_cTEncTop.setVuiParametersPresentFlag( m_vuiParametersPresentFlag );
   m_cTEncTop.setAspectRatioIdc( m_aspectRatioIdc );
@@ -840,5 +884,5 @@
   m_cTEncTop.setLog2MaxMvLengthVertical( m_log2MaxMvLengthVertical );
 }
-#endif
+#endif //SVC_EXTENSION
 
 Void TAppEncTop::xCreateLib()
@@ -851,15 +895,37 @@
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+    //2
+    g_bitDepthY = m_acLayerCfg[layer].m_internalBitDepthY;
+    g_bitDepthC = m_acLayerCfg[layer].m_internalBitDepthC;
+
+    g_uiPCMBitDepthLuma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthY : m_acLayerCfg[layer].m_internalBitDepthY;
+    g_uiPCMBitDepthChroma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthC : m_acLayerCfg[layer].m_internalBitDepthC;
+#endif
+#if LAYER_CTB
+    g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
+    g_uiMaxCUHeight = g_auiLayerMaxCUHeight[layer];
+    g_uiMaxCUDepth  = g_auiLayerMaxCUDepth[layer];
+    g_uiAddCUDepth  = g_auiLayerAddCUDepth[layer];
+#endif
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+    m_acTVideoIOYuvInputFile[layer].open( (Char *)m_acLayerCfg[layer].getInputFile().c_str(),  false, m_acLayerCfg[layer].m_inputBitDepthY, m_acLayerCfg[layer].m_inputBitDepthC, m_acLayerCfg[layer].m_internalBitDepthY, m_acLayerCfg[layer].m_internalBitDepthC );  // read  mode
+#else
     m_acTVideoIOYuvInputFile[layer].open( (Char *)m_acLayerCfg[layer].getInputFile().c_str(),  false, m_inputBitDepthY, m_inputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // read  mode
+#endif
     m_acTVideoIOYuvInputFile[layer].skipFrames(m_FrameSkip, m_acLayerCfg[layer].getSourceWidth() - m_acLayerCfg[layer].getPad()[0], m_acLayerCfg[layer].getSourceHeight() - m_acLayerCfg[layer].getPad()[1]);
 
     if (!m_acLayerCfg[layer].getReconFile().empty())
     {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+      m_acTVideoIOYuvReconFile[layer].open((Char *)m_acLayerCfg[layer].getReconFile().c_str(), true, m_acLayerCfg[layer].m_outputBitDepthY, m_acLayerCfg[layer].m_outputBitDepthC, m_acLayerCfg[layer].m_internalBitDepthY, m_acLayerCfg[layer].m_internalBitDepthC );  // write mode
+#else
       m_acTVideoIOYuvReconFile[layer].open((Char *)m_acLayerCfg[layer].getReconFile().c_str(), true, m_outputBitDepthY, m_outputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // write mode
+#endif
     }
 
     m_acTEncTop[layer].create();
   }
-#else
+#else //SVC_EXTENSION
   m_cTVideoIOYuvInputFile.open( m_pchInputFile,     false, m_inputBitDepthY, m_inputBitDepthC, m_internalBitDepthY, m_internalBitDepthC );  // read  mode
   m_cTVideoIOYuvInputFile.skipFrames(m_FrameSkip, m_iSourceWidth - m_aiPad[0], m_iSourceHeight - m_aiPad[1]);
@@ -870,5 +936,5 @@
   // Neo Decoder
   m_cTEncTop.create();
-#endif
+#endif //SVC_EXTENSION
 }
 
@@ -882,4 +948,11 @@
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
+#if LAYER_CTB
+    g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
+    g_uiMaxCUHeight = g_auiLayerMaxCUHeight[layer];
+    g_uiMaxCUDepth  = g_auiLayerMaxCUDepth[layer];
+    g_uiAddCUDepth  = g_auiLayerAddCUDepth[layer];
+#endif
+
     m_acTVideoIOYuvInputFile[layer].close();
     m_acTVideoIOYuvReconFile[layer].close();
@@ -887,5 +960,5 @@
     m_acTEncTop[layer].destroy();
   }
-#else
+#else //SVC_EXTENSION
   m_cTVideoIOYuvInputFile.close();
   m_cTVideoIOYuvReconFile.close();
@@ -893,5 +966,5 @@
   // Neo Decoder
   m_cTEncTop.destroy();
-#endif
+#endif //SVC_EXTENSION
 }
 
@@ -901,4 +974,23 @@
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+    //3
+    g_bitDepthY = m_acLayerCfg[layer].m_internalBitDepthY;
+    g_bitDepthC = m_acLayerCfg[layer].m_internalBitDepthC;
+
+    g_uiPCMBitDepthLuma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthY : m_acLayerCfg[layer].m_internalBitDepthY;
+    g_uiPCMBitDepthChroma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthC : m_acLayerCfg[layer].m_internalBitDepthC;
+#endif
+#if LAYER_CTB
+    g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
+    g_uiMaxCUHeight = g_auiLayerMaxCUHeight[layer];
+    g_uiMaxCUDepth  = g_auiLayerMaxCUDepth[layer];
+    g_uiAddCUDepth  = g_auiLayerAddCUDepth[layer];
+
+    memcpy( g_auiZscanToRaster, g_auiLayerZscanToRaster[layer], sizeof( g_auiZscanToRaster ) );
+    memcpy( g_auiRasterToZscan, g_auiLayerRasterToZscan[layer], sizeof( g_auiRasterToZscan ) );
+    memcpy( g_auiRasterToPelX,  g_auiLayerRasterToPelX[layer],  sizeof( g_auiRasterToPelX ) );
+    memcpy( g_auiRasterToPelY,  g_auiLayerRasterToPelY[layer],  sizeof( g_auiRasterToPelY ) );
+#endif
     m_acTEncTop[layer].init(isFieldCoding);
   }
@@ -915,4 +1007,12 @@
     for(Int layerId = 0; layerId <= vps->getMaxLayerId(); layerId++)
     {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+      //4
+      g_bitDepthY = m_acLayerCfg[layerId].m_internalBitDepthY;
+      g_bitDepthC = m_acLayerCfg[layerId].m_internalBitDepthC;
+
+      g_uiPCMBitDepthLuma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layerId].m_inputBitDepthY : m_acLayerCfg[layerId].m_internalBitDepthY;
+      g_uiPCMBitDepthChroma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layerId].m_inputBitDepthC : m_acLayerCfg[layerId].m_internalBitDepthC;
+#endif
       vps->setLayerIdIncludedFlag(true, setId, layerId);
     }
@@ -930,5 +1030,5 @@
     vps->setScalabilityMask(i, false);
   }
-  if(m_numLayers > 1) 
+  if(m_numLayers > 1)
   {
     Int scalabilityTypes = 0;
@@ -938,5 +1038,9 @@
       scalabilityTypes += m_scalabilityMask[i];
     }
+#if AUXILIARY_PICTURES
+    assert( scalabilityTypes <= 2 );
+#else
     assert( scalabilityTypes == 1 );
+#endif
     vps->setNumScalabilityTypes(scalabilityTypes);
   }
@@ -959,32 +1063,80 @@
     vps->setDimensionId(i, 0, i);
   }
+#if AUXILIARY_PICTURES
+  if (m_scalabilityMask[3])
+  {
+    UInt maxAuxId = 0;
+    UInt auxDimIdLen = 0;
+    for(i = 1; i < vps->getMaxLayers(); i++)
+    {
+      if (m_acLayerCfg[i].getAuxId() > maxAuxId)
+      {
+        maxAuxId = m_acLayerCfg[i].getAuxId();
+      }
+    }
+    while((1 << auxDimIdLen) < (maxAuxId + 1))
+    {
+      auxDimIdLen++;
+    }
+    vps->setDimensionIdLen(1, auxDimIdLen);
+    for(i = 1; i < vps->getMaxLayers(); i++)
+    {
+      vps->setDimensionId(i, 1, m_acLayerCfg[i].getAuxId());
+    }
+  }
+#endif
 #endif
 #if N0120_MAX_TID_REF_PRESENT_FLAG
 #if N0120_MAX_TID_REF_CFG
   vps->setMaxTidRefPresentFlag(m_maxTidRefPresentFlag);
-#else 
+#else
   vps->setMaxTidRefPresentFlag(true);
+#endif
+  if (vps->getMaxTidRefPresentFlag())
+  {
+    for( i = 0; i < MAX_VPS_LAYER_ID_PLUS1 - 1; i++ )
+    {
+#if N0120_MAX_TID_REF_CFG
+#if O0225_MAX_TID_FOR_REF_LAYERS
+      for( Int j = i+1; j <= MAX_VPS_LAYER_ID_PLUS1 - 1; j++)
+      {
+        vps->setMaxTidIlRefPicsPlus1(i, j, m_acTEncTop[i].getMaxTidIlRefPicsPlus1());
+      }
+#else
+      vps->setMaxTidIlRefPicsPlus1(i, m_acTEncTop[i].getMaxTidIlRefPicsPlus1());
 #endif 
-  if (vps->getMaxTidRefPresentFlag())
+#else
+#if O0225_MAX_TID_FOR_REF_LAYERS
+      for( Int j = i+1; j <= MAX_VPS_LAYER_ID_PLUS1 - 1; j++)
+      {
+        vps->setMaxTidIlRefPicsPlus1(i, j, vps->getMaxTLayers()+1);
+      }
+#else
+      vps->setMaxTidIlRefPicsPlus1(i, vps->getMaxTLayers()+1);
+#endif
+#endif
+    }
+  }
+  else
   {
     for( i = 0; i < MAX_VPS_LAYER_ID_PLUS1 - 1; i++ )
     {
-#if N0120_MAX_TID_REF_CFG
-      vps->setMaxTidIlRefPicsPlus1(i, m_acTEncTop[i].getMaxTidIlRefPicsPlus1()); 
-#else 
-      vps->setMaxTidIlRefPicsPlus1(i, vps->getMaxTLayers()+1);
+#if O0225_MAX_TID_FOR_REF_LAYERS
+      for( Int j = i+1; j <= MAX_VPS_LAYER_ID_PLUS1 - 1; j++)
+      {
+        vps->setMaxTidIlRefPicsPlus1(i, j, 7);
+      }
+#else
+      vps->setMaxTidIlRefPicsPlus1(i, 7);
 #endif 
     }
   }
-  else
-  {
-    for( i = 0; i < MAX_VPS_LAYER_ID_PLUS1 - 1; i++ )
-    {
-      vps->setMaxTidIlRefPicsPlus1(i, 7);
-    }
-  }
-#endif 
+#endif
 #if ILP_SSH_SIG
+#if ILP_SSH_SIG_FIX
+    vps->setIlpSshSignalingEnabledFlag(false);
+#else
     vps->setIlpSshSignalingEnabledFlag(true);
+#endif
 #endif
 #if VPS_EXTN_PROFILE_INFO
@@ -998,21 +1150,16 @@
 #endif
   // Target output layer
-#if VPS_PROFILE_OUTPUT_LAYERS
-  vps->setNumOutputLayerSets(vps->getNumLayerSets());    
-  vps->setNumProfileTierLevel(vps->getNumLayerSets());   
+  vps->setNumOutputLayerSets(vps->getNumLayerSets());
+  vps->setNumProfileTierLevel(vps->getNumLayerSets());
   vps->setDefaultOneTargetOutputLayerFlag(true);
   for(i = 1; i < vps->getNumLayerSets(); i++)
   {
     vps->setProfileLevelTierIdx(i, i);
-    vps->setOutputLayerSetIdx(i, i); 
-  }
-#else
-  vps->setNumOutputLayerSets(1);
-  Int lsIdx = 1;
-  vps->setOutputLayerSetIdx(0, lsIdx); // Because only one layer set
-#endif
+    vps->setOutputLayerSetIdx(i, i);
+  }
+
   for(Int lsIdx = 1; lsIdx < vps->getNumLayerSets(); lsIdx++)
   {
-    // Include the highest layer as output layer 
+    // Include the highest layer as output layer
     for(UInt layer=0; layer <= vps->getMaxLayerId() ; layer++)
     {
@@ -1073,9 +1220,18 @@
 #endif
 #endif
-#if JCTVC_M0458_INTERLAYER_RPS_SIG        
-    vps->setMaxOneActiveRefLayerFlag(maxDirectRefLayers > 1 ? false : true); 
-#endif 
-#if N0147_IRAP_ALIGN_FLAG    
-    vps->setCrossLayerIrapAlignFlag(true); 
+#if JCTVC_M0458_INTERLAYER_RPS_SIG
+    vps->setMaxOneActiveRefLayerFlag(maxDirectRefLayers > 1 ? false : true);
+#endif
+#if O0062_POC_LSB_NOT_PRESENT_FLAG
+    for(i = 1; i< vps->getMaxLayers(); i++)
+    {
+      if( vps->getNumDirectRefLayers( vps->getLayerIdInNuh(i) ) == 0  )
+      {
+        vps->setPocLsbNotPresentFlag(i, false);
+      }
+    }
+#endif
+#if N0147_IRAP_ALIGN_FLAG
+    vps->setCrossLayerIrapAlignFlag(true);
     for(UInt layerCtr = 1;layerCtr <= vps->getMaxLayers() - 1; layerCtr++)
     {
@@ -1083,5 +1239,5 @@
       {
         if (vps->getDirectDependencyFlag( layerCtr, refLayerCtr))
-        {          
+        {
           if(m_acTEncTop[layerCtr].getIntraPeriod() !=  m_acTEncTop[refLayerCtr].getIntraPeriod())
           {
@@ -1097,12 +1253,19 @@
       vps->setCrossLayerIrapAlignFlag(false);
     }
-#endif 
+#endif
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
   vps->setSingleLayerForNonIrapFlag(m_adaptiveResolutionChange > 0 ? true : false);
 #endif
+#if !VPS_EXTN_OFFSET_CALC
 #if VPS_EXTN_OFFSET
   // to be updated according to the current semantics
   vps->setExtensionOffset( 0xffff );
 #endif
+#endif
+
+#if O0215_PHASE_ALIGNMENT
+  vps->setPhaseAlignFlag( m_phaseAlignFlag );
+#endif
+
 #else //SVC_EXTENSION
   m_cTEncTop.init(isFieldCoding);
@@ -1148,4 +1311,12 @@
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+    //5
+    g_bitDepthY = m_acLayerCfg[layer].m_internalBitDepthY;
+    g_bitDepthC = m_acLayerCfg[layer].m_internalBitDepthC;
+
+    g_uiPCMBitDepthLuma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthY : m_acLayerCfg[layer].m_internalBitDepthY;
+    g_uiPCMBitDepthChroma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthC : m_acLayerCfg[layer].m_internalBitDepthC;
+#endif
     // allocate original YUV buffer
     pcPicYuvOrg[layer] = new TComPicYuv;
@@ -1153,5 +1324,17 @@
     {
 #if SVC_UPSAMPLING
+#if LAYER_CTB
+#if AUXILIARY_PICTURES
+      pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeightOrg(), m_acLayerCfg[layer].getChromaFormatIDC(), m_acLayerCfg[layer].m_uiMaxCUWidth, m_acLayerCfg[layer].m_uiMaxCUHeight, m_acLayerCfg[layer].m_uiMaxCUDepth, NULL );
+#else
+      pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeightOrg(), m_acLayerCfg[layer].m_uiMaxCUWidth, m_acLayerCfg[layer].m_uiMaxCUHeight, m_acLayerCfg[layer].m_uiMaxCUDepth, NULL );
+#endif
+#else
+#if AUXILIARY_PICTURES
+      pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeightOrg(), m_acLayerCfg[layer].getChromaFormatIDC(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
+#else
       pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeightOrg(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
+#endif
+#endif
 #else
       pcPicYuvOrg->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeightOrg(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
@@ -1161,5 +1344,17 @@
     {
 #if SVC_UPSAMPLING
+#if LAYER_CTB
+#if AUXILIARY_PICTURES
+      pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_acLayerCfg[layer].getChromaFormatIDC(), m_acLayerCfg[layer].m_uiMaxCUWidth, m_acLayerCfg[layer].m_uiMaxCUHeight, m_acLayerCfg[layer].m_uiMaxCUDepth, NULL );
+#else
+      pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_acLayerCfg[layer].m_uiMaxCUWidth, m_acLayerCfg[layer].m_uiMaxCUHeight, m_acLayerCfg[layer].m_uiMaxCUDepth, NULL );
+#endif
+#else
+#if AUXILIARY_PICTURES
+      pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_acLayerCfg[layer].getChromaFormatIDC(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
+#else
       pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
+#endif
+#endif
 #else
       pcPicYuvOrg->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
@@ -1190,5 +1385,5 @@
   while ( !bEos )
   {
-    // Read enough frames 
+    // Read enough frames
     Bool bFramesReadyToCode = false;
     while(!bFramesReadyToCode)
@@ -1196,4 +1391,19 @@
       for(UInt layer=0; layer<m_numLayers; layer++)
       {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+        //6
+        g_bitDepthY = m_acLayerCfg[layer].m_internalBitDepthY;
+        g_bitDepthC = m_acLayerCfg[layer].m_internalBitDepthC;
+
+        g_uiPCMBitDepthLuma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthY : m_acLayerCfg[layer].m_internalBitDepthY;
+        g_uiPCMBitDepthChroma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthC : m_acLayerCfg[layer].m_internalBitDepthC;
+#endif
+#if LAYER_CTB
+        g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
+        g_uiMaxCUHeight = g_auiLayerMaxCUHeight[layer];
+        g_uiMaxCUDepth  = g_auiLayerMaxCUDepth[layer];
+        g_uiAddCUDepth  = g_auiLayerAddCUDepth[layer];
+#endif
+
         // get buffers
         xGetBuffer(pcPicYuvRec, layer);
@@ -1201,4 +1411,11 @@
         // read input YUV file
         m_acTVideoIOYuvInputFile[layer].read( pcPicYuvOrg[layer], m_acLayerCfg[layer].getPad() );
+
+#if AUXILIARY_PICTURES
+        if (m_acLayerCfg[layer].getChromaFormatIDC() == CHROMA_400)
+        {
+          pcPicYuvOrg[layer]->convertToMonochrome();
+        }
+#endif
 
         if(layer == m_numLayers-1)
@@ -1257,5 +1474,5 @@
 #endif
 
-    // loop through frames in one GOP 
+    // loop through frames in one GOP
     for ( UInt iPicIdInGOP=0; iPicIdInGOP < (bFirstFrame? 1:m_iGOPSize); iPicIdInGOP++ )
     {
@@ -1263,4 +1480,23 @@
       for(UInt layer=0; layer<m_numLayers; layer++)
       {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+        //7
+        g_bitDepthY = m_acLayerCfg[layer].m_internalBitDepthY;
+        g_bitDepthC = m_acLayerCfg[layer].m_internalBitDepthC;
+
+        g_uiPCMBitDepthLuma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthY : m_acLayerCfg[layer].m_internalBitDepthY;
+        g_uiPCMBitDepthChroma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthC : m_acLayerCfg[layer].m_internalBitDepthC;
+#endif
+#if LAYER_CTB
+        g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
+        g_uiMaxCUHeight = g_auiLayerMaxCUHeight[layer];
+        g_uiMaxCUDepth  = g_auiLayerMaxCUDepth[layer];
+        g_uiAddCUDepth  = g_auiLayerAddCUDepth[layer];
+
+        memcpy( g_auiZscanToRaster, g_auiLayerZscanToRaster[layer], sizeof( g_auiZscanToRaster ) );
+        memcpy( g_auiRasterToZscan, g_auiLayerRasterToZscan[layer], sizeof( g_auiRasterToZscan ) );
+        memcpy( g_auiRasterToPelX,  g_auiLayerRasterToPelX[layer],  sizeof( g_auiRasterToPelX ) );
+        memcpy( g_auiRasterToPelY,  g_auiLayerRasterToPelY[layer],  sizeof( g_auiRasterToPelY ) );
+#endif
         // call encoding function for one frame
         if ( m_isField )
@@ -1288,4 +1524,12 @@
     for(UInt layer=0; layer<m_numLayers; layer++)
     {
+#if O0194_DIFFERENT_BITDEPTH_EL_BL
+      //8
+      g_bitDepthY = m_acLayerCfg[layer].m_internalBitDepthY;
+      g_bitDepthC = m_acLayerCfg[layer].m_internalBitDepthC;
+
+      g_uiPCMBitDepthLuma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthY : m_acLayerCfg[layer].m_internalBitDepthY;
+      g_uiPCMBitDepthChroma = m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_inputBitDepthC : m_acLayerCfg[layer].m_internalBitDepthC;
+#endif
       // write bistream to file if necessary
       iNumEncoded = m_acTEncTop[layer].getNumPicRcvd();
@@ -1528,5 +1772,17 @@
 
 #if SVC_UPSAMPLING
+#if LAYER_CTB
+#if AUXILIARY_PICTURES
+    rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_acLayerCfg[layer].getChromaFormatIDC(), m_acLayerCfg[layer].m_uiMaxCUWidth, m_acLayerCfg[layer].m_uiMaxCUHeight, m_acLayerCfg[layer].m_uiMaxCUDepth, NULL );
+#else
+    rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_acLayerCfg[layer].m_uiMaxCUWidth, m_acLayerCfg[layer].m_uiMaxCUHeight, m_acLayerCfg[layer].m_uiMaxCUDepth, NULL );
+#endif
+#else
+#if AUXILIARY_PICTURES
+    rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_acLayerCfg[layer].getChromaFormatIDC(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
+#else
     rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
+#endif
+#endif
 #else
     rpcPicYuvRec->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
@@ -1551,5 +1807,5 @@
       delete pcPicYuvRec; pcPicYuvRec = NULL;
     }
-  }  
+  }
 }
 
@@ -1602,5 +1858,5 @@
 #endif
       {
-        m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRec, m_acLayerCfg[layer].getConfLeft(), m_acLayerCfg[layer].getConfRight(), 
+        m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRec, m_acLayerCfg[layer].getConfLeft(), m_acLayerCfg[layer].getConfRight(),
           m_acLayerCfg[layer].getConfTop(), m_acLayerCfg[layer].getConfBottom() );
       }
@@ -1622,5 +1878,5 @@
     for ( i = 0; i < iNumEncoded/2; i++ )
 #endif
-    {      
+    {
       const AccessUnit& auTop = *(iterBitstream++);
       const vector<UInt>& statsTop = writeAnnexB(bitstreamFile, auTop);
