Index: branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp
===================================================================
--- branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp	(revision 1286)
+++ branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.cpp	(revision 1287)
@@ -57,5 +57,5 @@
 
 #if Q0074_COLOUR_REMAPPING_SEI
-static Void applyColourRemapping(TComPicYuv& pic, const SEIColourRemappingInfo* colourRemappingInfoSEI, UInt layerId=0 );
+static Void applyColourRemapping(TComPicYuv& pic, const SEIColourRemappingInfo* colourRemappingInfoSEI, const BitDepths& bitDpeths, UInt layerId=0 );
 static std::vector<SEIColourRemappingInfo> storeCriSEI; //Persistent Colour Remapping Information SEI
 static SEIColourRemappingInfo *seiColourRemappingInfoPrevious=NULL ;
@@ -290,8 +290,4 @@
 #endif
     {
-      // Set bitdepth for each layer when doing DBF
-      g_bitDepth[CHANNEL_TYPE_LUMA]   = g_bitDepthLayer[CHANNEL_TYPE_LUMA][curLayerId];
-      g_bitDepth[CHANNEL_TYPE_CHROMA] = g_bitDepthLayer[CHANNEL_TYPE_CHROMA][curLayerId];
-
       if (!loopFiltered[curLayerId] || bitstreamFile)
       {
@@ -322,12 +318,13 @@
       if ( m_pchReconFile[curLayerId] && !openedReconFile[curLayerId] )
       {
+        const BitDepths &bitDepths=pcListPic->front()->getSlice(0)->getBitDepths(); // use bit depths of first reconstructed picture.
         for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
         {
           if (m_outputBitDepth[channelType] == 0)
           {
-            m_outputBitDepth[channelType] = g_bitDepth[channelType];
+            m_outputBitDepth[channelType] = bitDepths.recon[channelType];
           }
         }
-        m_acTVideoIOYuvReconFile[curLayerId].open( m_pchReconFile[curLayerId], true, m_outputBitDepth, m_outputBitDepth, g_bitDepth ); // write mode
+        m_acTVideoIOYuvReconFile[curLayerId].open( m_pchReconFile[curLayerId], true, m_outputBitDepth, m_outputBitDepth, bitDepths.recon ); // write mode
 
         openedReconFile[curLayerId] = true;
@@ -541,13 +538,14 @@
       if ( m_pchReconFile && !openedReconFile )
       {
+        const BitDepths &bitDepths=pcListPic->front()->getPicSym()->getSPS().getBitDepths(); // use bit depths of first reconstructed picture.
         for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
         {
           if (m_outputBitDepth[channelType] == 0)
           {
-            m_outputBitDepth[channelType] = g_bitDepth[channelType];
+            m_outputBitDepth[channelType] = bitDepths.recon[channelType];
           }
         }
 
-        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepth, m_outputBitDepth, g_bitDepth ); // write mode
+        m_cTVideoIOYuvReconFile.open( m_pchReconFile, true, m_outputBitDepth, m_outputBitDepth, bitDepths.recon ); // write mode
         openedReconFile = true;
       }
@@ -1298,9 +1296,9 @@
       {
         //printf ("\n\nColour Remapping is applied to POC : %d and LayerId : %d ",pic->getPOC(), pic->getLayerId());
-        applyColourRemapping(*pic->getPicYuvRec(), seiColourRemappingInfo
 #if SVC_EXTENSION
-       , pic->getLayerId()
-#endif
-        );
+        applyColourRemapping(*pic->getPicYuvRec(), seiColourRemappingInfo, pic->getSlice(0)->getBitDepths(), pic->getLayerId());
+#else
+        applyColourRemapping(*pic->getPicYuvRec(), seiColourRemappingInfo, pic->getSlice(0)->getBitDepths());
+#endif
       }
       else  // using the last CRI SEI received
@@ -1308,9 +1306,9 @@
         const SEIColourRemappingInfo *seiColourRemappingInfoCopy;
         seiColourRemappingInfoCopy = seiColourRemappingInfoPrevious;
-        applyColourRemapping(*pic->getPicYuvRec(), seiColourRemappingInfoCopy
 #if SVC_EXTENSION
-        , pic->getLayerId()
-#endif
-        );
+        applyColourRemapping(*pic->getPicYuvRec(), seiColourRemappingInfoCopy, pic->getSlice(0)->getBitDepths(), pic->getLayerId());
+#else
+        applyColourRemapping(*pic->getPicYuvRec(), seiColourRemappingInfoCopy, pic->getSlice(0)->getBitDepths());
+#endif
       }
 
@@ -1545,5 +1543,5 @@
 
         TComPictureHash recon_digest;
-        Int numChar = calcMD5(*pic->getPicYuvRec(), recon_digest);
+        Int numChar = calcMD5(*pic->getPicYuvRec(), recon_digest, pic->getSlice(0)->getBitDepths());
         fprintf(fptr, "%8d%9d    MD5:%s\n", pic->getLayerId(), pic->getSlice(0)->getPOC(), hashToString(recon_digest, numChar).c_str());
         fclose(fptr);
@@ -1570,10 +1568,13 @@
       if( this->getDecodedYuvLayerRefresh(layerId) )
       {
-        m_outputBitDepth[CHANNEL_TYPE_LUMA]   = g_bitDepth[CHANNEL_TYPE_LUMA]   = g_bitDepthLayer[CHANNEL_TYPE_LUMA][layerId];
-        m_outputBitDepth[CHANNEL_TYPE_CHROMA] = g_bitDepth[CHANNEL_TYPE_CHROMA] = g_bitDepthLayer[CHANNEL_TYPE_CHROMA][layerId];
-
         char tempFileName[256];
         strcpy(tempFileName, this->getDecodedYuvLayerFileName( layerId ).c_str());
-        m_confReconFile[layerId].open(tempFileName, true, m_outputBitDepth, m_outputBitDepth, g_bitDepth ); // write mode
+
+        const TComVPS *vps = m_acTDecTop->getLayerDec(layerId)->getParameterSetManager()->getActiveVPS();
+        const TComSPS *sps = m_acTDecTop->getLayerDec(layerId)->getParameterSetManager()->getActiveSPS();
+        const BitDepths &bitDpeths = vps->getBitDepths(sps, layerId);
+        Int bitDepth[] = {bitDpeths.recon[CHANNEL_TYPE_LUMA], bitDpeths.recon[CHANNEL_TYPE_CHROMA]};
+
+        m_confReconFile[layerId].open(tempFileName, true, m_outputBitDepth, m_outputBitDepth, bitDepth ); // write mode
         this->setDecodedYuvLayerRefresh( layerId, false );
       }
@@ -1845,9 +1846,9 @@
 
 #if Q0074_COLOUR_REMAPPING_SEI
-Void xInitColourRemappingLut( const Int bitDepthY, const Int bitDepthC, std::vector<Int>(&preLut)[3], std::vector<Int>(&postLut)[3], const SEIColourRemappingInfo* const pCriSEI )
+Void xInitColourRemappingLut( const BitDepths &bitDepths, std::vector<Int>(&preLut)[3], std::vector<Int>(&postLut)[3], const SEIColourRemappingInfo* const pCriSEI )
 {
   for ( Int c=0 ; c<3 ; c++ )
   {  
-    Int bitDepth = c ? bitDepthC : bitDepthY ;
+    Int bitDepth = bitDepths.recon[toChannelType(ComponentID(c))];
     preLut[c].resize(1 << bitDepth);
     postLut[c].resize(1 << pCriSEI->m_colourRemapBitDepth);
@@ -1902,5 +1903,5 @@
 }
 
-static void applyColourRemapping(TComPicYuv& pic, const SEIColourRemappingInfo* pCriSEI, UInt layerId )
+static Void applyColourRemapping(TComPicYuv& pic, const SEIColourRemappingInfo* pCriSEI, const BitDepths& bitDpeths, UInt layerId )
 {  
   if( !storeCriSEI.size() )
@@ -1940,16 +1941,7 @@
     YUVOut[2] = picColourRemapped.getAddr(COMPONENT_Cr);
 
-#if SVC_EXTENSION
-    Int bitDepthY = g_bitDepthLayer[CHANNEL_TYPE_LUMA][layerId];
-    Int bitDepthC = g_bitDepthLayer[CHANNEL_TYPE_CHROMA][layerId];
-
-#else
-    Int bitDepthY = g_bitDepth[CHANNEL_TYPE_LUMA];
-    Int bitDepthC = g_bitDepth[CHANNEL_TYPE_CHROMA];
-#endif
-
     std::vector<Int> preLut[3];
     std::vector<Int> postLut[3];
-    xInitColourRemappingLut( bitDepthY, bitDepthC, preLut, postLut, &storeCriSEI[layerId] );
+    xInitColourRemappingLut( bitDpeths, preLut, postLut, &storeCriSEI[layerId] );
     
     Int roundingOffset = (storeCriSEI[layerId].m_log2MatrixDenom==0) ? 0 : (1 << (storeCriSEI[layerId].m_log2MatrixDenom - 1));
Index: branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.h
===================================================================
--- branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.h	(revision 1286)
+++ branches/SHM-dev/source/App/TAppDecoder/TAppDecTop.h	(revision 1287)
@@ -96,4 +96,7 @@
   Void  xInitDecLib       (); ///< initialize decoder class
   
+#if Q0074_COLOUR_REMAPPING_SEI
+  Void  xInitColourRemappingLut( const BitDepths &bitDepths, std::vector<Int>(&preLut)[3], std::vector<Int>(&postLut)[3], const SEIColourRemappingInfo* const pCriSEI );
+#endif
 #if SVC_EXTENSION
   Void  xWriteOutput      ( TComList<TComPic*>* pcListPic, UInt layerId, UInt tId ); ///< write YUV to file
Index: branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp
===================================================================
--- branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 1286)
+++ branches/SHM-dev/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 1287)
@@ -4308,14 +4308,4 @@
   g_uiMaxCUDepth = m_uiMaxCUDepth;
 #endif
-
-  // set internal bit-depth and constants
-  for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
-  {
-#if O0043_BEST_EFFORT_DECODING
-    g_bitDepthInStream[channelType] = g_bitDepth[channelType] = m_internalBitDepth[channelType];
-#else
-    g_bitDepth   [channelType] = m_internalBitDepth[channelType];
-#endif
-  }
 }
 #endif
@@ -4489,5 +4479,5 @@
   printf("TOOL CFG: ");
 #if !SVC_EXTENSION
-  printf("IBD:%d ", ((g_bitDepth[CHANNEL_TYPE_LUMA] > m_MSBExtendedBitDepth[CHANNEL_TYPE_LUMA]) || (g_bitDepth[CHANNEL_TYPE_CHROMA] > m_MSBExtendedBitDepth[CHANNEL_TYPE_CHROMA])));
+  printf("IBD:%d ", ((m_internalBitDepth[CHANNEL_TYPE_LUMA] > m_MSBExtendedBitDepth[CHANNEL_TYPE_LUMA]) || (m_internalBitDepth[CHANNEL_TYPE_CHROMA] > m_MSBExtendedBitDepth[CHANNEL_TYPE_CHROMA])));
 #endif
   printf("HAD:%d ", m_bUseHADME           );
Index: branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp	(revision 1286)
+++ branches/SHM-dev/source/App/TAppEncoder/TAppEncTop.cpp	(revision 1287)
@@ -217,11 +217,4 @@
   {
     //1
-    g_bitDepth[CHANNEL_TYPE_LUMA]   = m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_LUMA];
-    g_bitDepth[CHANNEL_TYPE_CHROMA] = m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_CHROMA];
-
-    // Set this to be used in Upsampling filter in function "TComUpsampleFilter::upsampleBasePic"
-    g_bitDepthLayer[CHANNEL_TYPE_LUMA][m_acLayerCfg[layer].m_layerId]   = g_bitDepth[CHANNEL_TYPE_LUMA];
-    g_bitDepthLayer[CHANNEL_TYPE_CHROMA][m_acLayerCfg[layer].m_layerId] = g_bitDepth[CHANNEL_TYPE_CHROMA];
-
     m_acTEncTop[layer].setInterLayerWeightedPredFlag                      ( m_useInterLayerWeightedPred );
     
@@ -475,5 +468,6 @@
     for( UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++ )
     {
-      m_acTEncTop[layer].setPCMBitDepth                                      ((ChannelType)channelType, m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_MSBExtendedBitDepth[channelType] : m_acLayerCfg[layer].m_internalBitDepth[channelType]);
+      m_acTEncTop[layer].setBitDepth                                       ((ChannelType)channelType, m_acLayerCfg[layer].m_internalBitDepth[channelType]);
+      m_acTEncTop[layer].setPCMBitDepth                                    ((ChannelType)channelType, m_bPCMInputBitDepthFlag ? m_acLayerCfg[layer].m_MSBExtendedBitDepth[channelType] : m_acLayerCfg[layer].m_internalBitDepth[channelType]);
     }
     m_acTEncTop[layer].setPCMLog2MaxSize                                   ( m_pcmLog2MaxSize);
@@ -904,9 +898,5 @@
   for (UInt channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++)
   {
-//#if O0043_BEST_EFFORT_DECODING
-    //g_bitDepthInStream[channelType] = g_bitDepth[channelType] = m_internalBitDepth[channelType];
-//#else
-    //g_bitDepth   [channelType] = m_internalBitDepth[channelType];
-//#endif
+    m_cTEncTop.setBitDepth((ChannelType)channelType, m_internalBitDepth[channelType]);
     m_cTEncTop.setPCMBitDepth((ChannelType)channelType, m_bPCMInputBitDepthFlag ? m_MSBExtendedBitDepth[channelType] : m_internalBitDepth[channelType]);
   }
@@ -1094,9 +1084,4 @@
   {
     //2
-    for( UInt channelTypeIndex = 0; channelTypeIndex < MAX_NUM_CHANNEL_TYPE; channelTypeIndex++)
-    {
-      g_bitDepth[channelTypeIndex]    = m_acLayerCfg[layer].m_internalBitDepth[channelTypeIndex];
-    }
-
 #if LAYER_CTB
     g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
@@ -1193,8 +1178,4 @@
   {
     //3
-    for (UInt channelTypeIndex = 0; channelTypeIndex < MAX_NUM_CHANNEL_TYPE; channelTypeIndex++)
-    {
-      g_bitDepth[channelTypeIndex]    = m_acLayerCfg[layer].m_internalBitDepth[channelTypeIndex];
-    }
 #if LAYER_CTB
     g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
@@ -1233,7 +1214,4 @@
         Int layerIdx = vps->getLayerIdxInVps(m_layerSetLayerIdList[setId][i]);
         //4
-        g_bitDepth[CHANNEL_TYPE_LUMA]   = m_acLayerCfg[layerIdx].m_internalBitDepth[CHANNEL_TYPE_LUMA];
-        g_bitDepth[CHANNEL_TYPE_CHROMA] = m_acLayerCfg[layerIdx].m_internalBitDepth[CHANNEL_TYPE_CHROMA];
-
         vps->setLayerIdIncludedFlag(true, setId, layerId);
       }
@@ -1250,6 +1228,4 @@
         //4
         UInt layerId = vps->getLayerIdInNuh(layerIdx);
-        g_bitDepth[CHANNEL_TYPE_LUMA]   = m_acLayerCfg[layerIdx].m_internalBitDepth[CHANNEL_TYPE_LUMA];
-        g_bitDepth[CHANNEL_TYPE_CHROMA] = m_acLayerCfg[layerIdx].m_internalBitDepth[CHANNEL_TYPE_CHROMA];
 
         if (layerId <= setId)
@@ -1702,7 +1678,4 @@
   {
     //5
-    g_bitDepth[CHANNEL_TYPE_LUMA]   = m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_LUMA];
-    g_bitDepth[CHANNEL_TYPE_CHROMA] = m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_CHROMA];
-
     // allocate original YUV buffer
     pcPicYuvOrg[layer] = new TComPicYuv;
@@ -1739,8 +1712,4 @@
       {
         //6
-        for (UInt channelTypeIndex = 0; channelTypeIndex < MAX_NUM_CHANNEL_TYPE; channelTypeIndex++)
-        {
-          g_bitDepth[channelTypeIndex]    = m_acLayerCfg[layer].m_internalBitDepth[channelTypeIndex];
-        }
 #if LAYER_CTB
         g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
@@ -1759,5 +1728,5 @@
         if( m_acLayerCfg[layer].getChromaFormatIDC() == CHROMA_400 || (m_apcTEncTop[0]->getVPS()->getScalabilityMask(AUX_ID) && (m_acLayerCfg[layer].getAuxId() == AUX_ALPHA || m_acLayerCfg[layer].getAuxId() == AUX_DEPTH)) )
         {
-          pcPicYuvOrg[layer]->convertToMonochrome();
+          pcPicYuvOrg[layer]->convertToMonochrome(m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_CHROMA]);
         }
 #endif
@@ -1823,8 +1792,4 @@
       {
         //7
-        for( UInt channelTypeIndex = 0; channelTypeIndex < MAX_NUM_CHANNEL_TYPE; channelTypeIndex++ )
-        {
-          g_bitDepth[channelTypeIndex]    = m_acLayerCfg[layer].m_internalBitDepth[channelTypeIndex];
-        }
 #if LAYER_CTB
         g_uiMaxCUWidth  = g_auiLayerMaxCUWidth[layer];
@@ -1895,9 +1860,4 @@
     {
       //8
-      for( UInt channelTypeIndex = 0; channelTypeIndex < MAX_NUM_CHANNEL_TYPE; channelTypeIndex++ )
-      {
-        g_bitDepth[channelTypeIndex]    = m_acLayerCfg[layer].m_internalBitDepth[channelTypeIndex];
-      }
-
       // write bistream to file if necessary
       iNumEncoded = m_acTEncTop[layer].getNumPicRcvd();
@@ -1983,5 +1943,7 @@
   for(layer = 0; layer < m_numLayers; layer++)
   {
-    m_gcAnalyzeAll[layer].printOut('a', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, layer);
+    const BitDepths bitDepths(m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_LUMA], m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_CHROMA]);
+
+    m_gcAnalyzeAll[layer].printOut('a', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, bitDepths, layer);
   }
 
@@ -1989,5 +1951,7 @@
   for(layer = 0; layer < m_numLayers; layer++)
   {
-    m_gcAnalyzeI[layer].printOut('i', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, layer);
+    const BitDepths bitDepths(m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_LUMA], m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_CHROMA]);
+
+    m_gcAnalyzeI[layer].printOut('i', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, bitDepths, layer);
   }
 
@@ -1995,5 +1959,7 @@
   for(layer = 0; layer < m_numLayers; layer++)
   {
-    m_gcAnalyzeP[layer].printOut('p', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, layer);
+    const BitDepths bitDepths(m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_LUMA], m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_CHROMA]);
+
+    m_gcAnalyzeP[layer].printOut('p', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, bitDepths, layer);
   }
 
@@ -2001,5 +1967,7 @@
   for(layer = 0; layer < m_numLayers; layer++)
   {
-    m_gcAnalyzeB[layer].printOut('b', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, layer);
+    const BitDepths bitDepths(m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_LUMA], m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_CHROMA]);
+
+    m_gcAnalyzeB[layer].printOut('b', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, bitDepths, layer);
   }
 
@@ -2008,4 +1976,6 @@
     for(layer = 0; layer < m_numLayers; layer++)
     {
+      const BitDepths bitDepths(m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_LUMA], m_acLayerCfg[layer].m_internalBitDepth[CHANNEL_TYPE_CHROMA]);
+
       //-- interlaced summary
       m_gcAnalyzeAll_in.setFrmRate( m_acLayerCfg[layer].getFrameRate());
@@ -2014,5 +1984,5 @@
 
       printf( "\n\nSUMMARY INTERLACED ---------------------------------------------\n" );
-      m_gcAnalyzeAll_in.printOut('a', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, layer);
+      m_gcAnalyzeAll_in.printOut('a', m_acLayerCfg[layer].getChromaFormatIDC(), printMSEBasedSNR, printSequenceMSE, bitDepths, layer);
 
 #if _SUMMARY_OUT_
