Index: branches/SHM-3.1-dev/source/Lib/TLibCommon/TComDataCU.cpp
===================================================================
--- branches/SHM-3.1-dev/source/Lib/TLibCommon/TComDataCU.cpp	(revision 388)
+++ branches/SHM-3.1-dev/source/Lib/TLibCommon/TComDataCU.cpp	(revision 389)
@@ -1273,5 +1273,9 @@
   UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
   
+#if REPN_FORMAT_IN_VPS
+  if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getPicWidthInLumaSamples() )
+#else
   if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )
+#endif
   {
     uiARPartUnitIdx = MAX_UINT;
@@ -1335,6 +1339,10 @@
   UInt uiAbsZorderCUIdxLB = g_auiZscanToRaster[ m_uiAbsIdxInLCU ] + (m_puhHeight[0] / m_pcPic->getMinCUHeight() - 1)*m_pcPic->getNumPartInWidth();
   UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
-  
+
+#if REPN_FORMAT_IN_VPS
+  if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getPicHeightInLumaSamples() )
+#else
   if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getSPS()->getPicHeightInLumaSamples() )
+#endif
   {
     uiBLPartUnitIdx = MAX_UINT;
@@ -1384,5 +1392,9 @@
   UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
   
+#if REPN_FORMAT_IN_VPS
+  if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getPicHeightInLumaSamples())
+#else
   if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxLB] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getSPS()->getPicHeightInLumaSamples())
+#endif
   {
     uiBLPartUnitIdx = MAX_UINT;
@@ -1432,5 +1444,9 @@
   UInt uiNumPartInCUWidth = m_pcPic->getNumPartInWidth();
   
+#if REPN_FORMAT_IN_VPS
+  if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getPicWidthInLumaSamples() )
+#else
   if( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxRT] + (m_pcPic->getPicSym()->getMinCUHeight() * uiPartUnitOffset)) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )
+#endif
   {
     uiARPartUnitIdx = MAX_UINT;
@@ -2684,4 +2700,12 @@
     Int uiLCUIdx = -1;
 
+#if REPN_FORMAT_IN_VPS
+    if      ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxTmp] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getPicWidthInLumaSamples() )  // image boundary check
+    {
+    }
+    else if ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdxTmp] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getPicHeightInLumaSamples() )
+    {
+    }
+#else
     if      ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdxTmp] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )  // image boundary check
     {
@@ -2690,4 +2714,5 @@
     {
     }
+#endif
     else
     {
@@ -3020,4 +3045,12 @@
     uiAbsPartIdx = g_auiZscanToRaster[uiPartIdxRB];
     Int uiLCUIdx = -1;
+#if REPN_FORMAT_IN_VPS
+    if ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdx] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getPicWidthInLumaSamples() )  // image boundary check
+    {
+    }
+    else if ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelY() + g_auiRasterToPelY[uiAbsPartIdx] + m_pcPic->getMinCUHeight() ) >= m_pcSlice->getPicHeightInLumaSamples() )
+    {
+    }
+#else
     if ( ( m_pcPic->getCU(m_uiCUAddr)->getCUPelX() + g_auiRasterToPelX[uiAbsPartIdx] + m_pcPic->getMinCUWidth() ) >= m_pcSlice->getSPS()->getPicWidthInLumaSamples() )  // image boundary check
     {
@@ -3026,4 +3059,5 @@
     {
     }
+#endif
     else
     {
@@ -3095,8 +3129,16 @@
   Int  iMvShift = 2;
   Int iOffset = 8;
+#if REPN_FORMAT_IN_VPS
+  Int iHorMax = ( m_pcSlice->getPicWidthInLumaSamples() + iOffset - m_uiCUPelX - 1 ) << iMvShift;
+#else
   Int iHorMax = ( m_pcSlice->getSPS()->getPicWidthInLumaSamples() + iOffset - m_uiCUPelX - 1 ) << iMvShift;
+#endif
   Int iHorMin = (       -(Int)g_uiMaxCUWidth - iOffset - (Int)m_uiCUPelX + 1 ) << iMvShift;
   
+#if REPN_FORMAT_IN_VPS
+  Int iVerMax = ( m_pcSlice->getPicHeightInLumaSamples() + iOffset - m_uiCUPelY - 1 ) << iMvShift;
+#else
   Int iVerMax = ( m_pcSlice->getSPS()->getPicHeightInLumaSamples() + iOffset - m_uiCUPelY - 1 ) << iMvShift;
+#endif
   Int iVerMin = (       -(Int)g_uiMaxCUHeight - iOffset - (Int)m_uiCUPelY + 1 ) << iMvShift;
   
Index: branches/SHM-3.1-dev/source/Lib/TLibCommon/TComLoopFilter.cpp
===================================================================
--- branches/SHM-3.1-dev/source/Lib/TLibCommon/TComLoopFilter.cpp	(revision 388)
+++ branches/SHM-3.1-dev/source/Lib/TLibCommon/TComLoopFilter.cpp	(revision 389)
@@ -180,5 +180,9 @@
       UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsZorderIdx] ];
       UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsZorderIdx] ];
+#if REPN_FORMAT_IN_VPS
+      if( ( uiLPelX < pcCU->getSlice()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcCU->getSlice()->getPicHeightInLumaSamples() ) )
+#else
       if( ( uiLPelX < pcCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcCU->getSlice()->getSPS()->getPicHeightInLumaSamples() ) )
+#endif
       {
         xDeblockCU( pcCU, uiAbsZorderIdx, uiDepth+1, Edge );
Index: branches/SHM-3.1-dev/source/Lib/TLibCommon/TComPic.cpp
===================================================================
--- branches/SHM-3.1-dev/source/Lib/TLibCommon/TComPic.cpp	(revision 388)
+++ branches/SHM-3.1-dev/source/Lib/TLibCommon/TComPic.cpp	(revision 389)
@@ -213,6 +213,11 @@
   UInt maxNumSUInLCU = getNumPartInCU();
   UInt numLCUInPic   = getNumCUsInFrame();
+#if REPN_FORMAT_IN_VPS
+  UInt picWidth      = getSlice(0)->getPicWidthInLumaSamples();
+  UInt picHeight     = getSlice(0)->getPicHeightInLumaSamples();
+#else
   UInt picWidth      = getSlice(0)->getSPS()->getPicWidthInLumaSamples();
   UInt picHeight     = getSlice(0)->getSPS()->getPicHeightInLumaSamples();
+#endif
   Int  numLCUsInPicWidth = getFrameWidthInCU();
   Int  numLCUsInPicHeight= getFrameHeightInCU();
Index: branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp
===================================================================
--- branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp	(revision 388)
+++ branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp	(revision 389)
@@ -1414,5 +1414,9 @@
       UInt uiLPelX   = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsZorderIdx] ];
       UInt uiTPelY   = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsZorderIdx] ];
+#if REPN_FORMAT_IN_VPS
+      if( ( uiLPelX < pcCU->getSlice()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcCU->getSlice()->getPicHeightInLumaSamples() ) )
+#else
       if( ( uiLPelX < pcCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcCU->getSlice()->getSPS()->getPicHeightInLumaSamples() ) )
+#endif
         xPCMCURestoration( pcCU, uiAbsZorderIdx, uiDepth+1 );
     }
Index: branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSlice.cpp
===================================================================
--- branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSlice.cpp	(revision 388)
+++ branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSlice.cpp	(revision 389)
@@ -1587,4 +1587,99 @@
 }
 
+#if REPN_FORMAT_IN_VPS
+UInt TComSlice::getPicWidthInLumaSamples()
+{
+  TComSPS *sps = getSPS();
+  TComVPS *vps = getVPS();
+  UInt retVal, layerId = getLayerId();
+  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
+  {
+    retVal = sps->getPicWidthInLumaSamples();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getPicWidthVpsInLumaSamples();
+  }
+  return retVal;
+}
+UInt TComSlice::getPicHeightInLumaSamples()
+{
+  TComSPS *sps = getSPS();
+  TComVPS *vps = getVPS();
+  UInt retVal, layerId = getLayerId();
+  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
+  {
+    retVal = sps->getPicHeightInLumaSamples();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getPicHeightVpsInLumaSamples();
+  }
+  return retVal;
+}
+UInt TComSlice::getChromaFormatIdc()
+{
+  TComSPS *sps = getSPS();
+  TComVPS *vps = getVPS();
+  UInt retVal, layerId = getLayerId();
+  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
+  {
+    retVal = sps->getChromaFormatIdc();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getChromaFormatVpsIdc();
+  }
+  return retVal;
+}
+UInt TComSlice::getBitDepthY()
+{
+  TComSPS *sps = getSPS();
+  TComVPS *vps = getVPS();
+  UInt retVal, layerId = getLayerId();
+  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
+  {
+    retVal = sps->getBitDepthY();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getBitDepthVpsLuma();
+  }
+  return retVal;
+}
+UInt TComSlice::getBitDepthC()
+{
+  TComSPS *sps = getSPS();
+  TComVPS *vps = getVPS();
+  UInt retVal, layerId = getLayerId();
+  if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
+  {
+    retVal = sps->getBitDepthC();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getBitDepthVpsChroma();
+  }
+  return retVal;
+}
+Int TComSlice::getQpBDOffsetY()
+{
+  return (getBitDepthY() - 8) * 6;
+}
+Int TComSlice::getQpBDOffsetC()
+{
+  return (getBitDepthC() - 8) * 6;
+}
+
+RepFormat::RepFormat()
+: m_chromaFormatVpsIdc          (0)
+, m_separateColourPlaneVpsFlag  (false)
+, m_picWidthVpsInLumaSamples    (0)
+, m_picHeightVpsInLumaSamples   (0)
+, m_bitDepthVpsLuma             (0)
+, m_bitDepthVpsChroma           (0)
+{}
+#endif
+
 // ------------------------------------------------------------------------------------------------
 // Video parameter set (VPS)
@@ -1619,4 +1714,8 @@
 , m_picRatePresentVpsFlag     (false)
 #endif
+#if REPN_FORMAT_IN_VPS
+, m_repFormatIdxPresentFlag   (true)
+, m_vpsNumRepFormats          (1)
+#endif
 {
   for( Int i = 0; i < MAX_TLAYER; i++)
@@ -1692,4 +1791,7 @@
   ::memset(m_constPicRateIdc   , 0, sizeof(m_constPicRateIdc)   );
   ::memset(m_avgPicRate        , 0, sizeof(m_avgPicRate)        );
+#endif
+#if REPN_FORMAT_IN_VPS
+  ::memset( m_vpsRepFormatIdx, 0, sizeof(m_vpsRepFormatIdx) );
 #endif
 }
@@ -1778,4 +1880,7 @@
 , m_numScaledRefLayerOffsets  (0)
 #endif
+#if REPN_FORMAT_IN_VPS
+, m_updateRepFormatFlag       (false)
+#endif
 {
   for ( Int i = 0; i < MAX_TLAYER; i++ )
Index: branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSlice.h
===================================================================
--- branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSlice.h	(revision 388)
+++ branches/SHM-3.1-dev/source/Lib/TLibCommon/TComSlice.h	(revision 389)
@@ -406,4 +406,36 @@
 };
 
+#if REPN_FORMAT_IN_VPS
+class RepFormat
+{
+  Int  m_chromaFormatVpsIdc;
+  Bool m_separateColourPlaneVpsFlag;
+  Int  m_picWidthVpsInLumaSamples;
+  Int  m_picHeightVpsInLumaSamples;
+  Int  m_bitDepthVpsLuma;               // coded as minus8
+  Int  m_bitDepthVpsChroma;             // coded as minus8
+
+public:
+  RepFormat();
+
+  Int  getChromaFormatVpsIdc()        { return m_chromaFormatVpsIdc; }
+  Void setChromaFormatVpsIdc(Int x)   { m_chromaFormatVpsIdc = x;    }
+
+  Bool getSeparateColourPlaneVpsFlag()        { return m_separateColourPlaneVpsFlag; }
+  Void setSeparateColourPlaneVpsFlag(Bool x)  { m_separateColourPlaneVpsFlag = x;    }
+
+  Int  getPicWidthVpsInLumaSamples()        { return m_picWidthVpsInLumaSamples; }
+  Void setPicWidthVpsInLumaSamples(Int x)   { m_picWidthVpsInLumaSamples = x;    }
+
+  Int  getPicHeightVpsInLumaSamples()        { return m_picHeightVpsInLumaSamples; }
+  Void setPicHeightVpsInLumaSamples(Int x)   { m_picHeightVpsInLumaSamples = x;    }
+
+  Int  getBitDepthVpsLuma()           { return m_bitDepthVpsLuma;   }
+  Void setBitDepthVpsLuma(Int x)      { m_bitDepthVpsLuma = x;      }
+
+  Int  getBitDepthVpsChroma()           { return m_bitDepthVpsChroma;   }
+  Void setBitDepthVpsChroma(Int x)      { m_bitDepthVpsChroma = x;      }
+};
+#endif
 class TComVPS
 {
@@ -519,4 +551,10 @@
   Int         m_avgPicRate          [MAX_VPS_LAYER_SETS_PLUS1][MAX_TLAYER];
 #endif
+#if REPN_FORMAT_IN_VPS
+  Bool       m_repFormatIdxPresentFlag;
+  Int        m_vpsNumRepFormats;            // coded as minus1
+  RepFormat  m_vpsRepFormat[16];
+  Int        m_vpsRepFormatIdx[16];
+#endif
 public:
   TComVPS();
@@ -727,4 +765,17 @@
   Int  getAvgPicRate(Int i, Int j)          { return m_avgPicRate[i][j]; }
   Void setAvgPicRate(Int i, Int j, Int x)   { m_avgPicRate[i][j] = x;    }
+#endif
+
+#if REPN_FORMAT_IN_VPS
+  Bool   getRepFormatIdxPresentFlag()       { return m_repFormatIdxPresentFlag; }
+  Void   setRepFormatIdxPresentFlag(Bool x) { m_repFormatIdxPresentFlag = x;    }
+
+  Int    getVpsNumRepFormats()              { return m_vpsNumRepFormats;        }
+  Void   setVpsNumRepFormats(Int x)         { m_vpsNumRepFormats = x;           }
+
+  RepFormat* getVpsRepFormat(Int idx)       { return &m_vpsRepFormat[idx];      }
+
+  Int    getVpsRepFormatIdx(Int idx)        { return m_vpsRepFormatIdx[idx];   }
+  Void   setVpsRepFormatIdx(Int idx, Int x) { m_vpsRepFormatIdx[idx] = x;      }         
 #endif
 };
@@ -1041,4 +1092,7 @@
   UInt        m_numScaledRefLayerOffsets;
   Window      m_scaledRefLayerWindow[MAX_LAYERS];
+#endif
+#if REPN_FORMAT_IN_VPS
+  Bool m_updateRepFormatFlag;
 #endif
 public:
@@ -1200,4 +1254,8 @@
   Window&  getScaledRefLayerWindow( Int x )   { return m_scaledRefLayerWindow[x]; }
 #endif
+#if REPN_FORMAT_IN_VPS
+  Bool     getUpdateRepFormatFlag()       { return m_updateRepFormatFlag; }
+  Void     setUpdateRepFormatFlag(Bool x) { m_updateRepFormatFlag = x;    }
+#endif
 };
 
@@ -1866,4 +1924,13 @@
 #endif //SVC_EXTENSION
 
+#if REPN_FORMAT_IN_VPS
+  UInt getPicWidthInLumaSamples();
+  UInt getPicHeightInLumaSamples();
+  UInt getChromaFormatIdc();
+  UInt getBitDepthY();
+  UInt getBitDepthC();
+  Int getQpBDOffsetY();
+  Int getQpBDOffsetC();
+#endif
 protected:
   TComPic*  xGetRefPic  (TComList<TComPic*>& rcListPic,
Index: branches/SHM-3.1-dev/source/Lib/TLibCommon/TComTrQuant.cpp
===================================================================
--- branches/SHM-3.1-dev/source/Lib/TLibCommon/TComTrQuant.cpp	(revision 388)
+++ branches/SHM-3.1-dev/source/Lib/TLibCommon/TComTrQuant.cpp	(revision 389)
@@ -42,5 +42,5 @@
 #include "TComPic.h"
 #include "ContextTables.h"
-
+#define MAYBE_BUGFIX 1
 typedef struct
 {
@@ -50,4 +50,14 @@
   Double d64SigCost;
   Double d64SigCost_0;
+#if MAYBE_BUGFIX
+  Void init()
+  {
+    iNNZbeforePos0 = 0;
+  d64CodedLevelandDist = 0;
+  d64UncodedDist = 0;
+  d64SigCost = 0;
+  d64SigCost_0 = 0;
+  }
+#endif
 } coeffGroupRDStats;
 
@@ -1033,5 +1043,9 @@
 
     Int qpScaled;
+#if REPN_FORMAT_IN_VPS
+    Int qpBDOffset = (eTType == TEXT_LUMA)? pcCU->getSlice()->getQpBDOffsetY() : pcCU->getSlice()->getQpBDOffsetC();
+#else
     Int qpBDOffset = (eTType == TEXT_LUMA)? pcCU->getSlice()->getSPS()->getQpBDOffsetY() : pcCU->getSlice()->getSPS()->getQpBDOffsetC();
+#endif
 
     if(eTType == TEXT_LUMA)
@@ -1581,6 +1595,9 @@
     UInt uiCGPosY   = uiCGBlkPos / uiNumBlkSide;
     UInt uiCGPosX   = uiCGBlkPos - (uiCGPosY * uiNumBlkSide);
+#if MAYBE_BUGFIX
+    rdStats.init();
+#else
     ::memset( &rdStats, 0, sizeof (coeffGroupRDStats));
-    
+#endif
     const Int patternSigCtx = TComTrQuant::calcPatternSigCtx(uiSigCoeffGroupFlag, uiCGPosX, uiCGPosY, uiWidth, uiHeight);
     for (Int iScanPosinCG = uiCGSize-1; iScanPosinCG >= 0; iScanPosinCG--)
Index: branches/SHM-3.1-dev/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- branches/SHM-3.1-dev/source/Lib/TLibCommon/TypeDef.h	(revision 388)
+++ branches/SHM-3.1-dev/source/Lib/TLibCommon/TypeDef.h	(revision 389)
@@ -48,4 +48,5 @@
 #if SVC_EXTENSION
 #define MAX_LAYERS                       2      ///< max number of layers the codec is supposed to handle
+#define REPN_FORMAT_IN_VPS               1      ///< N0092: Signal represenation format (spatial resolution, bit depth, colour format) in the VPS
 #define TIMING_INFO_NONZERO_LAYERID_SPS  1      ///< N0085: Semantics of vui_timing_info_present_flag to always set that flag to zero for non-zero layer ID SPS 
 #define RPL_INIT_N0316_N0082             1      ///< N0316, N0082: initial reference picture list construction 
