Index: trunk/source/Lib/TLibCommon/CommonDef.h
===================================================================
--- trunk/source/Lib/TLibCommon/CommonDef.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/CommonDef.h	(revision 540)
@@ -40,5 +40,4 @@
 
 #include <algorithm>
-
 #if _MSC_VER > 1000
 // disable "signed and unsigned mismatch"
@@ -57,7 +56,8 @@
 
 #if SVC_EXTENSION
-#define NV_VERSION        "4.0"                 ///< Current software version
+#include <vector>
+#define NV_VERSION        "4.1"                 ///< Current software version
 #else
-#define NV_VERSION        "12.0"                ///< Current software version
+#define NV_VERSION        "12.1"                ///< Current software version
 #endif
 
@@ -209,5 +209,5 @@
   
   NAL_UNIT_CODED_SLICE_TSA_N,     // 2
-  NAL_UNIT_CODED_SLICE_TLA_R,       // 3
+  NAL_UNIT_CODED_SLICE_TSA_R,       // 3
   
   NAL_UNIT_CODED_SLICE_STSA_N,    // 4
@@ -280,4 +280,33 @@
 };
 
+#if OUTPUT_LAYER_SET_INDEX
+class CommonDecoderParams
+{
+  Int m_targetLayerId;
+  Int m_outputLayerSetIdx;
+  std::vector<Int> *m_targetDecLayerIdSet; 
+  Bool m_valueCheckedFlag;
+public:
+  CommonDecoderParams(): 
+    m_targetLayerId(0)
+    , m_outputLayerSetIdx(-1)
+    , m_targetDecLayerIdSet(NULL)
+    , m_valueCheckedFlag(false)
+ {}
+
+  Void setTargetLayerId(const Int x) { m_targetLayerId = x;   }
+  Int  getTargetLayerId()            { return m_targetLayerId;}
+  
+  Void setOutputLayerSetIdx(const Int x) { m_outputLayerSetIdx = x;   }
+  Int  getOutputLayerSetIdx()            { return m_outputLayerSetIdx;}
+
+  Void               setTargetDecLayerIdSet(std::vector<Int> *x) { m_targetDecLayerIdSet = x;   }
+  std::vector<Int>*  getTargetDecLayerIdSet()                    { return m_targetDecLayerIdSet;}
+  
+  Void setValueCheckedFlag(const Bool x) { m_valueCheckedFlag = x;   }
+  Bool getValueCheckedFlag()            { return m_valueCheckedFlag;}
+  
+};
+#endif
 //! \}
 
Index: trunk/source/Lib/TLibCommon/NAL.h
===================================================================
--- trunk/source/Lib/TLibCommon/NAL.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/NAL.h	(revision 540)
@@ -76,5 +76,5 @@
     return m_nalUnitType == NAL_UNIT_CODED_SLICE_TRAIL_R
         || m_nalUnitType == NAL_UNIT_CODED_SLICE_TRAIL_N
-        || m_nalUnitType == NAL_UNIT_CODED_SLICE_TLA_R
+        || m_nalUnitType == NAL_UNIT_CODED_SLICE_TSA_R
         || m_nalUnitType == NAL_UNIT_CODED_SLICE_TSA_N
         || m_nalUnitType == NAL_UNIT_CODED_SLICE_STSA_R
Index: trunk/source/Lib/TLibCommon/TComDataCU.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComDataCU.cpp	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComDataCU.cpp	(revision 540)
@@ -309,5 +309,5 @@
   }
 }
-
+#if !HM_CLEANUP_SAO
 const NDBFBlockInfo& NDBFBlockInfo::operator= (const NDBFBlockInfo& src)
 {
@@ -328,5 +328,5 @@
 }
 
-
+#endif
 // ====================================================================================================================
 // Public member functions
@@ -3638,5 +3638,5 @@
   return getPic()->getPicSym()->getInverseCUOrderMap(m_uiCUAddr)*(1<<(m_pcSlice->getSPS()->getMaxCUDepth()<<1))+m_uiAbsIdxInLCU; 
 }
-
+#if !HM_CLEANUP_SAO
 /** Set neighboring blocks availabilities for non-deblocked filtering 
  * \param numLCUInPicWidth number of LCUs in picture width
@@ -4008,4 +4008,6 @@
 }
 
+#endif
+
 #if SVC_EXTENSION
 TComDataCU*  TComDataCU::getBaseColCU( UInt refLayerIdc, UInt uiCuAbsPartIdx, UInt &uiCUAddrBase, UInt &uiAbsPartIdxBase, Int iMotionMapping )
@@ -4028,6 +4030,11 @@
 #endif
 
+#if O0098_SCALED_REF_LAYER_ID
+  Int leftStartL = getSlice()->getSPS()->getScaledRefLayerWindowForLayer(getSlice()->getVPS()->getRefLayerId(getSlice()->getLayerId(), refLayerIdc)).getWindowLeftOffset();
+  Int topStartL  = getSlice()->getSPS()->getScaledRefLayerWindowForLayer(getSlice()->getVPS()->getRefLayerId(getSlice()->getLayerId(), refLayerIdc)).getWindowTopOffset();
+#else
   Int leftStartL = this->getSlice()->getSPS()->getScaledRefLayerWindow(refLayerIdc).getWindowLeftOffset();
   Int topStartL  = this->getSlice()->getSPS()->getScaledRefLayerWindow(refLayerIdc).getWindowTopOffset();
+#endif
   Int iBX = ((uiPelX - leftStartL)*g_posScalingFactor[refLayerIdc][0] + (1<<15)) >> 16;
   Int iBY = ((uiPelY - topStartL )*g_posScalingFactor[refLayerIdc][1] + (1<<15)) >> 16;
Index: trunk/source/Lib/TLibCommon/TComDataCU.h
===================================================================
--- trunk/source/Lib/TLibCommon/TComDataCU.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComDataCU.h	(revision 540)
@@ -55,4 +55,5 @@
 //! \{
 
+#if !HM_CLEANUP_SAO
 // ====================================================================================================================
 // Non-deblocking in-loop filter processing block data structure
@@ -92,4 +93,5 @@
   const NDBFBlockInfo& operator= (const NDBFBlockInfo& src);  //!< "=" operator
 };
+#endif
 
 
@@ -159,7 +161,8 @@
   Pel*          m_pcIPCMSampleCr;     ///< PCM sample buffer (Cr)
 
+#if !HM_CLEANUP_SAO
   Int*          m_piSliceSUMap;       ///< pointer of slice ID map
   std::vector<NDBFBlockInfo> m_vNDFBlock;
-
+#endif
   // -------------------------------------------------------------------------------------------------------------------
   // neighbour access variables
@@ -389,4 +392,5 @@
   Void          setIPCMFlagSubParts   (Bool bIpcmFlag, UInt uiAbsPartIdx, UInt uiDepth);
 
+#if !HM_CLEANUP_SAO
   /// get slice ID for SU
   Int           getSUSliceID          (UInt uiIdx)              {return m_piSliceSUMap[uiIdx];      } 
@@ -403,4 +407,5 @@
                                           ,Bool bTopTileBoundary, Bool bDownTileBoundary, Bool bLeftTileBoundary, Bool bRightTileBoundary
                                           ,Bool bIndependentTileBoundaryEnabled );
+#endif
   // -------------------------------------------------------------------------------------------------------------------
   // member functions for accessing partition information
Index: trunk/source/Lib/TLibCommon/TComPic.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComPic.cpp	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComPic.cpp	(revision 540)
@@ -59,4 +59,5 @@
 , m_bNeededForOutput                      (false)
 , m_uiCurrSliceIdx                        (0)
+#if !HM_CLEANUP_SAO
 , m_pSliceSUMap                           (NULL)
 , m_pbValidSlice                          (NULL)
@@ -65,4 +66,5 @@
 , m_bIndependentTileBoundaryForNDBFilter  (false)
 , m_pNDBFilterYuvTmp                      (NULL)
+#endif
 , m_bCheckLTMSB                           (false)
 {
@@ -212,5 +214,12 @@
   } 
 }
-
+#if HM_CLEANUP_SAO
+Bool  TComPic::getSAOMergeAvailability(Int currAddr, Int mergeAddr)
+{
+  Bool mergeCtbInSliceSeg = (mergeAddr >= getPicSym()->getCUOrderMap(getCU(currAddr)->getSlice()->getSliceCurStartCUAddr()/getNumPartInCU()));
+  Bool mergeCtbInTile     = (getPicSym()->getTileIdxMap(mergeAddr) == getPicSym()->getTileIdxMap(currAddr));
+  return (mergeCtbInSliceSeg && mergeCtbInTile);
+}
+#else
 /** Create non-deblocked filter information
  * \param pSliceStartAddress array for storing slice start addresses
@@ -548,4 +557,5 @@
 
 }
+#endif
 
 #if SVC_EXTENSION
Index: trunk/source/Lib/TLibCommon/TComPic.h
===================================================================
--- trunk/source/Lib/TLibCommon/TComPic.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComPic.h	(revision 540)
@@ -75,4 +75,5 @@
   Bool                  m_bNeededForOutput;
   UInt                  m_uiCurrSliceIdx;         // Index of current slice
+#if !HM_CLEANUP_SAO
   Int*                  m_pSliceSUMap;
   Bool*                 m_pbValidSlice;
@@ -81,4 +82,5 @@
   Bool                  m_bIndependentTileBoundaryForNDBFilter;
   TComPicYuv*           m_pNDBFilterYuvTmp;    //!< temporary picture buffer when non-cross slice/tile boundary in-loop filtering is enabled
+#endif
   Bool                  m_bCheckLTMSB;
   
@@ -199,4 +201,7 @@
   Window&       getDefDisplayWindow()   { return m_defaultDisplayWindow; }
 
+#if HM_CLEANUP_SAO
+  Bool          getSAOMergeAvailability(Int currAddr, Int mergeAddr);
+#else
   Void          createNonDBFilterInfo   (std::vector<Int> sliceStartAddress, Int sliceGranularityDepth
                                         ,std::vector<Bool>* LFCrossSliceBoundary
@@ -211,4 +216,5 @@
   TComPicYuv*   getYuvPicBufferForIndependentBoundaryProcessing()             {return m_pNDBFilterYuvTmp;}
   std::vector<TComDataCU*>& getOneSliceCUDataForNDBFilter      (Int sliceID) { return m_vSliceCUDataLink[sliceID];}
+#endif
 
   /* field coding parameters*/
Index: trunk/source/Lib/TLibCommon/TComPicSym.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComPicSym.cpp	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComPicSym.cpp	(revision 540)
@@ -67,4 +67,7 @@
 ,m_puiTileIdxMap(NULL)
 ,m_puiInverseCUOrderMap(NULL)
+#if HM_CLEANUP_SAO
+,m_saoBlkParams(NULL)
+#endif
 {};
 
@@ -127,5 +130,10 @@
     m_puiInverseCUOrderMap[i] = i;
   }
+
+#if HM_CLEANUP_SAO
+  m_saoBlkParams = new SAOBlkParam[m_uiNumCUsInFrame];
+#else
   m_saoParam = NULL;
+#endif
 }
 
@@ -183,4 +191,10 @@
   m_puiInverseCUOrderMap = NULL;
   
+#if HM_CLEANUP_SAO
+  if(m_saoBlkParams)
+  {
+    delete[] m_saoBlkParams; m_saoBlkParams = NULL;
+  }
+#else
   if (m_saoParam)
   {
@@ -189,4 +203,5 @@
     m_saoParam = NULL;
   }
+#endif
 }
 
@@ -332,4 +347,114 @@
 }
 
+
+#if HM_CLEANUP_SAO
+Void TComPicSym::deriveLoopFilterBoundaryAvailibility(Int ctu,
+                                                      Bool& isLeftAvail,
+                                                      Bool& isRightAvail,
+                                                      Bool& isAboveAvail,
+                                                      Bool& isBelowAvail,
+                                                      Bool& isAboveLeftAvail,
+                                                      Bool& isAboveRightAvail,
+                                                      Bool& isBelowLeftAvail,
+                                                      Bool& isBelowRightAvail
+                                                      )
+{
+
+  isLeftAvail      = (ctu % m_uiWidthInCU != 0);
+  isRightAvail     = (ctu % m_uiWidthInCU != m_uiWidthInCU-1);
+  isAboveAvail     = (ctu >= m_uiWidthInCU );
+  isBelowAvail     = (ctu <  m_uiNumCUsInFrame - m_uiWidthInCU);
+  isAboveLeftAvail = (isAboveAvail && isLeftAvail);
+  isAboveRightAvail= (isAboveAvail && isRightAvail);
+  isBelowLeftAvail = (isBelowAvail && isLeftAvail);
+  isBelowRightAvail= (isBelowAvail && isRightAvail);
+
+  Bool isLoopFiltAcrossTilePPS = getCU(ctu)->getSlice()->getPPS()->getLoopFilterAcrossTilesEnabledFlag();
+
+  {
+    TComDataCU* ctuCurr  = getCU(ctu);
+    TComDataCU* ctuLeft  = isLeftAvail ?getCU(ctu-1):NULL;
+    TComDataCU* ctuRight = isRightAvail?getCU(ctu+1):NULL;
+    TComDataCU* ctuAbove = isAboveAvail?getCU(ctu-m_uiWidthInCU):NULL;
+    TComDataCU* ctuBelow = isBelowAvail?getCU(ctu+m_uiWidthInCU):NULL;
+    TComDataCU* ctuAboveLeft  = isAboveLeftAvail ? getCU(ctu-m_uiWidthInCU-1):NULL;
+    TComDataCU* ctuAboveRigtht= isAboveRightAvail? getCU(ctu-m_uiWidthInCU+1):NULL;
+    TComDataCU* ctuBelowLeft  = isBelowLeftAvail ? getCU(ctu+m_uiWidthInCU-1):NULL;
+    TComDataCU* ctuBelowRight = isBelowRightAvail? getCU(ctu+m_uiWidthInCU+1):NULL;
+
+    {
+      //left
+      if(ctuLeft != NULL)
+      {
+        isLeftAvail = (ctuCurr->getSlice()->getSliceCurStartCUAddr() != ctuLeft->getSlice()->getSliceCurStartCUAddr())?ctuCurr->getSlice()->getLFCrossSliceBoundaryFlag():true;
+      }
+      //above
+      if(ctuAbove != NULL)
+      {
+        isAboveAvail = (ctuCurr->getSlice()->getSliceCurStartCUAddr() != ctuAbove->getSlice()->getSliceCurStartCUAddr())?ctuCurr->getSlice()->getLFCrossSliceBoundaryFlag():true;
+      }
+      //right
+      if(ctuRight != NULL)
+      {
+        isRightAvail = (ctuCurr->getSlice()->getSliceCurStartCUAddr() != ctuRight->getSlice()->getSliceCurStartCUAddr())?ctuRight->getSlice()->getLFCrossSliceBoundaryFlag():true;
+      }
+      //below
+      if(ctuBelow != NULL)
+      {
+        isBelowAvail = (ctuCurr->getSlice()->getSliceCurStartCUAddr() != ctuBelow->getSlice()->getSliceCurStartCUAddr())?ctuBelow->getSlice()->getLFCrossSliceBoundaryFlag():true;
+      }
+      //above-left
+      if(ctuAboveLeft != NULL)
+      {
+        isAboveLeftAvail = (ctuCurr->getSlice()->getSliceCurStartCUAddr() != ctuAboveLeft->getSlice()->getSliceCurStartCUAddr())?ctuCurr->getSlice()->getLFCrossSliceBoundaryFlag():true;
+      }
+      //below-right
+      if(ctuBelowRight != NULL)
+      {
+        isBelowRightAvail = (ctuCurr->getSlice()->getSliceCurStartCUAddr() != ctuBelowRight->getSlice()->getSliceCurStartCUAddr())?ctuBelowRight->getSlice()->getLFCrossSliceBoundaryFlag():true;
+      }
+
+
+      //above-right
+      if(ctuAboveRigtht != NULL)
+      {
+        Int curSliceStartTS  = getCUOrderMap(ctuCurr->getSlice()->getSliceCurStartCUAddr()/m_uiNumPartitions);
+        Int aboveRigthtSliceStartTS = getCUOrderMap(ctuAboveRigtht->getSlice()->getSliceCurStartCUAddr()/m_uiNumPartitions);
+
+        isAboveRightAvail = (curSliceStartTS == aboveRigthtSliceStartTS)?(true):
+          (
+          (curSliceStartTS > aboveRigthtSliceStartTS)?(ctuCurr->getSlice()->getLFCrossSliceBoundaryFlag())
+          :(ctuAboveRigtht->getSlice()->getLFCrossSliceBoundaryFlag())
+          );          
+      }
+      //below-left
+      if(ctuBelowLeft != NULL)
+      {
+        Int curSliceStartTS       = getCUOrderMap(ctuCurr->getSlice()->getSliceCurStartCUAddr()/m_uiNumPartitions);
+        Int belowLeftSliceStartTS = getCUOrderMap(ctuBelowLeft->getSlice()->getSliceCurStartCUAddr()/m_uiNumPartitions);
+
+        isBelowLeftAvail = (curSliceStartTS == belowLeftSliceStartTS)?(true):
+          (
+          (curSliceStartTS > belowLeftSliceStartTS)?(ctuCurr->getSlice()->getLFCrossSliceBoundaryFlag())
+          :(ctuBelowLeft->getSlice()->getLFCrossSliceBoundaryFlag())
+          );
+      }        
+    }
+
+    if(!isLoopFiltAcrossTilePPS)
+    {      
+      isLeftAvail      = (!isLeftAvail      ) ?false:(getTileIdxMap( ctuLeft->getAddr()         ) == getTileIdxMap( ctu ));
+      isAboveAvail     = (!isAboveAvail     ) ?false:(getTileIdxMap( ctuAbove->getAddr()        ) == getTileIdxMap( ctu ));
+      isRightAvail     = (!isRightAvail     ) ?false:(getTileIdxMap( ctuRight->getAddr()        ) == getTileIdxMap( ctu ));
+      isBelowAvail     = (!isBelowAvail     ) ?false:(getTileIdxMap( ctuBelow->getAddr()        ) == getTileIdxMap( ctu ));
+      isAboveLeftAvail = (!isAboveLeftAvail ) ?false:(getTileIdxMap( ctuAboveLeft->getAddr()    ) == getTileIdxMap( ctu ));
+      isAboveRightAvail= (!isAboveRightAvail) ?false:(getTileIdxMap( ctuAboveRigtht->getAddr()  ) == getTileIdxMap( ctu ));
+      isBelowLeftAvail = (!isBelowLeftAvail ) ?false:(getTileIdxMap( ctuBelowLeft->getAddr()    ) == getTileIdxMap( ctu ));
+      isBelowRightAvail= (!isBelowRightAvail) ?false:(getTileIdxMap( ctuBelowRight->getAddr()   ) == getTileIdxMap( ctu ));
+    }
+  }
+
+}
+#else
 Void TComPicSym::allocSaoParam(TComSampleAdaptiveOffset *sao)
 {
@@ -337,4 +462,5 @@
   sao->allocSaoParam(m_saoParam);
 }
+#endif
 
 TComTile::TComTile()
Index: trunk/source/Lib/TLibCommon/TComPicSym.h
===================================================================
--- trunk/source/Lib/TLibCommon/TComPicSym.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComPicSym.h	(revision 540)
@@ -112,5 +112,10 @@
   UInt*         m_puiInverseCUOrderMap;
 
+#if HM_CLEANUP_SAO
+  SAOBlkParam *m_saoBlkParams;
+#else
   SAOParam *m_saoParam;
+#endif
+
 public:
   Void        create  ( Int iPicWidth, Int iPicHeight, UInt uiMaxWidth, UInt uiMaxHeight, UInt uiMaxDepth );
@@ -171,6 +176,13 @@
   Void         xInitTiles();
   UInt         xCalculateNxtCUAddr( UInt uiCurrCUAddr );
+#if HM_CLEANUP_SAO
+  SAOBlkParam* getSAOBlkParam() { return m_saoBlkParams;}
+  Void deriveLoopFilterBoundaryAvailibility(Int ctu, Bool& isLeftAvail,Bool& isRightAvail,Bool& isAboveAvail,Bool& isBelowAvail,Bool& isAboveLeftAvail,Bool& isAboveRightAvail,Bool& isBelowLeftAvail,Bool& isBelowRightAvail);
+#else
   Void allocSaoParam(TComSampleAdaptiveOffset *sao);
   SAOParam *getSaoParam() { return m_saoParam; }
+#endif
+
+
 };// END CLASS DEFINITION TComPicSym
 
Index: trunk/source/Lib/TLibCommon/TComRdCost.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComRdCost.cpp	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComRdCost.cpp	(revision 540)
@@ -330,9 +330,5 @@
 
 // Setting the Distortion Parameter for Inter (subpel ME with step)
-#if NS_HAD
-Void TComRdCost::setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME, Bool bUseNSHAD )
-#else
 Void TComRdCost::setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME )
-#endif
 {
 #if O0194_WEIGHTED_PREDICTION_CGS
@@ -353,7 +349,4 @@
   rcDistParam.iCols    = pcPatternKey->getROIYWidth();
   rcDistParam.iRows    = pcPatternKey->getROIYHeight();
-#if NS_HAD
-  rcDistParam.bUseNSHAD = bUseNSHAD;
-#endif
 
   // set distortion function
@@ -386,9 +379,5 @@
 
 Void
-#if NS_HAD
-TComRdCost::setDistParam( DistParam& rcDP, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard, Bool bUseNSHAD )
-#else
 TComRdCost::setDistParam( DistParam& rcDP, Int bitDepth, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard )
-#endif
 {
   rcDP.pOrg       = p1;
@@ -402,7 +391,4 @@
   rcDP.bitDepth   = bitDepth;
   rcDP.DistFunc   = m_afpDistortFunc[ ( bHadamard ? DF_HADS : DF_SADS ) + g_aucConvertToBit[ iWidth ] + 1 ];
-#if NS_HAD
-  rcDP.bUseNSHAD  = bUseNSHAD;
-#endif
 }
 
@@ -453,9 +439,5 @@
 }
 
-#if WEIGHTED_CHROMA_DISTORTION
 UInt TComRdCost::getDistPart(Int bitDepth, Pel* piCur, Int iCurStride,  Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, TextType eText, DFunc eDFunc)
-#else
-UInt TComRdCost::getDistPart(Int bitDepth, Pel* piCur, Int iCurStride,  Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc )
-#endif
 {
   DistParam cDtParam;
@@ -471,5 +453,4 @@
   cDtParam.bitDepth = bitDepth;
 
-#if WEIGHTED_CHROMA_DISTORTION
   if (eText == TEXT_CHROMA_U)
   {
@@ -484,26 +465,5 @@
     return cDtParam.DistFunc( &cDtParam );
   }
-#else
-  return cDtParam.DistFunc( &cDtParam );
-#endif
-}
-
-#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
-UInt TComRdCost::getSADPart ( Int bitDepth, Pel* pelCur, Int curStride,  Pel* pelOrg, Int orgStride, UInt width, UInt height )
-{
-  UInt SAD = 0;
-  Int shift = DISTORTION_PRECISION_ADJUSTMENT(bitDepth-8);
-  for ( Int i=0; i<height; i++ )
-  {
-    for( Int j=0; j<width; j++ )
-    {
-      SAD += abs((pelCur[j] - pelOrg[j])) >> shift;
-    }
-    pelCur = pelCur + curStride;
-    pelOrg = pelOrg + orgStride;
-  }
-  return SAD;
-}
-#endif
+}
 
 // ====================================================================================================================
@@ -1579,252 +1539,4 @@
 }
 
-#if NS_HAD
-UInt TComRdCost::xCalcHADs16x4( Pel *piOrg, Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
-{
-  Int k, i, j, jj, sad=0;
-  Int diff[64], m1[4][16], m2[4][16];
-  assert( iStep == 1 );
-  for( k = 0; k < 64; k += 16 )
-  {
-    diff[k+0] = piOrg[0] - piCur[0];
-    diff[k+1] = piOrg[1] - piCur[1];
-    diff[k+2] = piOrg[2] - piCur[2];
-    diff[k+3] = piOrg[3] - piCur[3];
-    diff[k+4] = piOrg[4] - piCur[4];
-    diff[k+5] = piOrg[5] - piCur[5];
-    diff[k+6] = piOrg[6] - piCur[6];
-    diff[k+7] = piOrg[7] - piCur[7];
-
-    diff[k+8]  = piOrg[8]  - piCur[8] ;
-    diff[k+9]  = piOrg[9]  - piCur[9] ;
-    diff[k+10] = piOrg[10] - piCur[10];
-    diff[k+11] = piOrg[11] - piCur[11];
-    diff[k+12] = piOrg[12] - piCur[12];
-    diff[k+13] = piOrg[13] - piCur[13];
-    diff[k+14] = piOrg[14] - piCur[14];
-    diff[k+15] = piOrg[15] - piCur[15];
-
-    piCur += iStrideCur;
-    piOrg += iStrideOrg;
-  }
-
-  //horizontal
-  for (j=0; j < 4; j++)
-  {
-    jj = j << 4;
-
-    m2[j][0]  = diff[jj  ] + diff[jj+8];
-    m2[j][1]  = diff[jj+1] + diff[jj+9];
-    m2[j][2]  = diff[jj+2] + diff[jj+10];
-    m2[j][3]  = diff[jj+3] + diff[jj+11];
-    m2[j][4]  = diff[jj+4] + diff[jj+12];
-    m2[j][5]  = diff[jj+5] + diff[jj+13];
-    m2[j][6]  = diff[jj+6] + diff[jj+14];
-    m2[j][7]  = diff[jj+7] + diff[jj+15];
-    m2[j][8]  = diff[jj  ] - diff[jj+8];
-    m2[j][9]  = diff[jj+1] - diff[jj+9];
-    m2[j][10] = diff[jj+2] - diff[jj+10];
-    m2[j][11] = diff[jj+3] - diff[jj+11];
-    m2[j][12] = diff[jj+4] - diff[jj+12];
-    m2[j][13] = diff[jj+5] - diff[jj+13];
-    m2[j][14] = diff[jj+6] - diff[jj+14];
-    m2[j][15] = diff[jj+7] - diff[jj+15];
-
-    m1[j][0]  = m2[j][0]  + m2[j][4];
-    m1[j][1]  = m2[j][1]  + m2[j][5];
-    m1[j][2]  = m2[j][2]  + m2[j][6];
-    m1[j][3]  = m2[j][3]  + m2[j][7];
-    m1[j][4]  = m2[j][0]  - m2[j][4];
-    m1[j][5]  = m2[j][1]  - m2[j][5];
-    m1[j][6]  = m2[j][2]  - m2[j][6];
-    m1[j][7]  = m2[j][3]  - m2[j][7];
-    m1[j][8]  = m2[j][8]  + m2[j][12];
-    m1[j][9]  = m2[j][9]  + m2[j][13];
-    m1[j][10] = m2[j][10] + m2[j][14];
-    m1[j][11] = m2[j][11] + m2[j][15];
-    m1[j][12] = m2[j][8]  - m2[j][12];
-    m1[j][13] = m2[j][9]  - m2[j][13];
-    m1[j][14] = m2[j][10] - m2[j][14];
-    m1[j][15] = m2[j][11] - m2[j][15];
-
-    m2[j][0]  = m1[j][0]  + m1[j][2];
-    m2[j][1]  = m1[j][1]  + m1[j][3];
-    m2[j][2]  = m1[j][0]  - m1[j][2];
-    m2[j][3]  = m1[j][1]  - m1[j][3];
-    m2[j][4]  = m1[j][4]  + m1[j][6];
-    m2[j][5]  = m1[j][5]  + m1[j][7];
-    m2[j][6]  = m1[j][4]  - m1[j][6];
-    m2[j][7]  = m1[j][5]  - m1[j][7];
-    m2[j][8]  = m1[j][8]  + m1[j][10];
-    m2[j][9]  = m1[j][9]  + m1[j][11];
-    m2[j][10] = m1[j][8]  - m1[j][10];
-    m2[j][11] = m1[j][9]  - m1[j][11];
-    m2[j][12] = m1[j][12] + m1[j][14];
-    m2[j][13] = m1[j][13] + m1[j][15];
-    m2[j][14] = m1[j][12] - m1[j][14];
-    m2[j][15] = m1[j][13] - m1[j][15];
-
-    m1[j][0]  = m2[j][0]  + m2[j][1];
-    m1[j][1]  = m2[j][0]  - m2[j][1];
-    m1[j][2]  = m2[j][2]  + m2[j][3];
-    m1[j][3]  = m2[j][2]  - m2[j][3];
-    m1[j][4]  = m2[j][4]  + m2[j][5];
-    m1[j][5]  = m2[j][4]  - m2[j][5];
-    m1[j][6]  = m2[j][6]  + m2[j][7];
-    m1[j][7]  = m2[j][6]  - m2[j][7];
-    m1[j][8]  = m2[j][8]  + m2[j][9];
-    m1[j][9]  = m2[j][8]  - m2[j][9];
-    m1[j][10] = m2[j][10] + m2[j][11];
-    m1[j][11] = m2[j][10] - m2[j][11];
-    m1[j][12] = m2[j][12] + m2[j][13];
-    m1[j][13] = m2[j][12] - m2[j][13];
-    m1[j][14] = m2[j][14] + m2[j][15];
-    m1[j][15] = m2[j][14] - m2[j][15];
-  }
-
-  //vertical
-  for (i=0; i < 16; i++)
-  {
-    m2[0][i] = m1[0][i] + m1[2][i];
-    m2[1][i] = m1[1][i] + m1[3][i];
-    m2[2][i] = m1[0][i] - m1[2][i];
-    m2[3][i] = m1[1][i] - m1[3][i];
-
-    m1[0][i] = m2[0][i] + m2[1][i];
-    m1[1][i] = m2[0][i] - m2[1][i];
-    m1[2][i] = m2[2][i] + m2[3][i];
-    m1[3][i] = m2[2][i] - m2[3][i];
-  }
-
-  for (i = 0; i < 4; i++)
-  {
-    for (j = 0; j < 16; j++)
-    {
-      sad += abs(m1[i][j]);
-    }
-  }
-
-  sad=((sad+2)>>2);
-
-  return sad;
-}
-
-UInt TComRdCost::xCalcHADs4x16( Pel *piOrg, Pel *piCur, Int iStrideOrg, Int iStrideCur, Int iStep )
-{
-  Int k, i, j, jj, sad=0;
-  Int diff[64], m1[16][4], m2[16][4], m3[16][4];
-  assert( iStep == 1 );
-  for( k = 0; k < 64; k += 4 )
-  {
-    diff[k+0] = piOrg[0] - piCur[0];
-    diff[k+1] = piOrg[1] - piCur[1];
-    diff[k+2] = piOrg[2] - piCur[2];
-    diff[k+3] = piOrg[3] - piCur[3];
-
-    piCur += iStrideCur;
-    piOrg += iStrideOrg;
-  }
-
-  //horizontal
-  for (j=0; j < 16; j++)
-  {
-    jj = j << 2;
-    m2[j][0] = diff[jj  ] + diff[jj+2];
-    m2[j][1] = diff[jj+1] + diff[jj+3];
-    m2[j][2] = diff[jj  ] - diff[jj+2];
-    m2[j][3] = diff[jj+1] - diff[jj+3];
-
-    m1[j][0] = m2[j][0] + m2[j][1];
-    m1[j][1] = m2[j][0] - m2[j][1];
-    m1[j][2] = m2[j][2] + m2[j][3];
-    m1[j][3] = m2[j][2] - m2[j][3];
-  }
-
-  //vertical
-  for (i=0; i < 4; i++)
-  {
-    m2[0][i]  = m1[0][i] + m1[8][i];
-    m2[1][i]  = m1[1][i] + m1[9][i];
-    m2[2][i]  = m1[2][i] + m1[10][i];
-    m2[3][i]  = m1[3][i] + m1[11][i];
-    m2[4][i]  = m1[4][i] + m1[12][i];
-    m2[5][i]  = m1[5][i] + m1[13][i];
-    m2[6][i]  = m1[6][i] + m1[14][i];
-    m2[7][i]  = m1[7][i] + m1[15][i];
-    m2[8][i]  = m1[0][i] - m1[8][i];
-    m2[9][i]  = m1[1][i] - m1[9][i];
-    m2[10][i] = m1[2][i] - m1[10][i];
-    m2[11][i] = m1[3][i] - m1[11][i];
-    m2[12][i] = m1[4][i] - m1[12][i];
-    m2[13][i] = m1[5][i] - m1[13][i];
-    m2[14][i] = m1[6][i] - m1[14][i];
-    m2[15][i] = m1[7][i] - m1[15][i];
-
-    m3[0][i]  = m2[0][i]  + m2[4][i];
-    m3[1][i]  = m2[1][i]  + m2[5][i];
-    m3[2][i]  = m2[2][i]  + m2[6][i];
-    m3[3][i]  = m2[3][i]  + m2[7][i];
-    m3[4][i]  = m2[0][i]  - m2[4][i];
-    m3[5][i]  = m2[1][i]  - m2[5][i];
-    m3[6][i]  = m2[2][i]  - m2[6][i];
-    m3[7][i]  = m2[3][i]  - m2[7][i];
-    m3[8][i]  = m2[8][i]  + m2[12][i];
-    m3[9][i]  = m2[9][i]  + m2[13][i];
-    m3[10][i] = m2[10][i] + m2[14][i];
-    m3[11][i] = m2[11][i] + m2[15][i];
-    m3[12][i] = m2[8][i]  - m2[12][i];
-    m3[13][i] = m2[9][i]  - m2[13][i];
-    m3[14][i] = m2[10][i] - m2[14][i];
-    m3[15][i] = m2[11][i] - m2[15][i];
-
-    m1[0][i]  = m3[0][i]  + m3[2][i];
-    m1[1][i]  = m3[1][i]  + m3[3][i];
-    m1[2][i]  = m3[0][i]  - m3[2][i];
-    m1[3][i]  = m3[1][i]  - m3[3][i];
-    m1[4][i]  = m3[4][i]  + m3[6][i];
-    m1[5][i]  = m3[5][i]  + m3[7][i];
-    m1[6][i]  = m3[4][i]  - m3[6][i];
-    m1[7][i]  = m3[5][i]  - m3[7][i];
-    m1[8][i]  = m3[8][i]  + m3[10][i];
-    m1[9][i]  = m3[9][i]  + m3[11][i];
-    m1[10][i] = m3[8][i]  - m3[10][i];
-    m1[11][i] = m3[9][i]  - m3[11][i];
-    m1[12][i] = m3[12][i] + m3[14][i];
-    m1[13][i] = m3[13][i] + m3[15][i];
-    m1[14][i] = m3[12][i] - m3[14][i];
-    m1[15][i] = m3[13][i] - m3[15][i];
-
-    m2[0][i]  = m1[0][i]  + m1[1][i];
-    m2[1][i]  = m1[0][i]  - m1[1][i];
-    m2[2][i]  = m1[2][i]  + m1[3][i];
-    m2[3][i]  = m1[2][i]  - m1[3][i];
-    m2[4][i]  = m1[4][i]  + m1[5][i];
-    m2[5][i]  = m1[4][i]  - m1[5][i];
-    m2[6][i]  = m1[6][i]  + m1[7][i];
-    m2[7][i]  = m1[6][i]  - m1[7][i];
-    m2[8][i]  = m1[8][i]  + m1[9][i];
-    m2[9][i]  = m1[8][i]  - m1[9][i];
-    m2[10][i] = m1[10][i] + m1[11][i];
-    m2[11][i] = m1[10][i] - m1[11][i];
-    m2[12][i] = m1[12][i] + m1[13][i];
-    m2[13][i] = m1[12][i] - m1[13][i];
-    m2[14][i] = m1[14][i] + m1[15][i];
-    m2[15][i] = m1[14][i] - m1[15][i];
-  }
-
-  for (i = 0; i < 16; i++)
-  {
-    for (j = 0; j < 4; j++)
-    {
-      sad += abs(m2[i][j]);
-    }
-  }
-
-  sad=((sad+2)>>2);
-
-  return sad;
-}
-#endif
-
 UInt TComRdCost::xGetHADs4( DistParam* pcDtParam )
 {
@@ -1909,9 +1621,5 @@
   UInt uiSum = 0;
 
-#if NS_HAD
-  if( ( ( iRows % 8 == 0) && (iCols % 8 == 0) && ( iRows == iCols ) ) || ( ( iRows % 8 == 0 ) && (iCols % 8 == 0) && !pcDtParam->bUseNSHAD ) )
-#else
   if( ( iRows % 8 == 0) && (iCols % 8 == 0) )
-#endif
   {
     Int  iOffsetOrg = iStrideOrg<<3;
@@ -1927,34 +1635,4 @@
     }
   }
-#if NS_HAD
-  else if ( ( iCols > 8 ) && ( iCols > iRows ) && pcDtParam->bUseNSHAD )
-  {
-    Int  iOffsetOrg = iStrideOrg<<2;
-    Int  iOffsetCur = iStrideCur<<2;
-    for ( y=0; y<iRows; y+= 4 )
-    {
-      for ( x=0; x<iCols; x+= 16 )
-      {
-        uiSum += xCalcHADs16x4( &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
-      }
-      piOrg += iOffsetOrg;
-      piCur += iOffsetCur;
-    }
-  }
-  else if ( ( iRows > 8 ) && ( iCols < iRows ) && pcDtParam->bUseNSHAD )
-  {
-    Int  iOffsetOrg = iStrideOrg<<4;
-    Int  iOffsetCur = iStrideCur<<4;
-    for ( y=0; y<iRows; y+= 16 )
-    {
-      for ( x=0; x<iCols; x+= 4 )
-      {
-        uiSum += xCalcHADs4x16( &piOrg[x], &piCur[x*iStep], iStrideOrg, iStrideCur, iStep );
-      }
-      piOrg += iOffsetOrg;
-      piCur += iOffsetCur;
-    }
-  }
-#endif
   else if( ( iRows % 4 == 0) && (iCols % 4 == 0) )
   {
Index: trunk/source/Lib/TLibCommon/TComRdCost.h
===================================================================
--- trunk/source/Lib/TLibCommon/TComRdCost.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComRdCost.h	(revision 540)
@@ -84,8 +84,4 @@
   UInt            uiComp;           // uiComp = 0 (luma Y), 1 (chroma U), 2 (chroma V)
 
-#if NS_HAD
-  Bool            bUseNSHAD;
-#endif
-
   // (vertical) subsampling shift (for reducing complexity)
   // - 0 = no subsampling, 1 = even rows, 2 = every 4th, etc.
@@ -104,7 +100,4 @@
     iSubShift = 0;
     bitDepth = 0;
-#if NS_HAD
-    bUseNSHAD = false;
-#endif
   }
 };
@@ -123,8 +116,6 @@
 #endif  
   
-#if WEIGHTED_CHROMA_DISTORTION
   Double                  m_cbDistortionWeight; 
   Double                  m_crDistortionWeight; 
-#endif
   Double                  m_dLambda;
   Double                  m_sqrtLambda;
@@ -155,8 +146,6 @@
   Double  calcRdCost64( UInt64 uiBits, UInt64 uiDistortion, Bool bFlag = false, DFunc eDFunc = DF_DEFAULT );
   
-#if WEIGHTED_CHROMA_DISTORTION
   Void    setCbDistortionWeight      ( Double cbDistortionWeight) { m_cbDistortionWeight = cbDistortionWeight; };
   Void    setCrDistortionWeight      ( Double crDistortionWeight) { m_crDistortionWeight = crDistortionWeight; };
-#endif
   Void    setLambda      ( Double dLambda );
   Void    setFrameLambda ( Double dLambda ) { m_dFrameLambda = dLambda; }
@@ -164,10 +153,6 @@
   Double  getSqrtLambda ()   { return m_sqrtLambda; }
 
-#if RATE_CONTROL_LAMBDA_DOMAIN
   Double  getLambda() { return m_dLambda; }
-#if M0036_RC_IMPROVEMENT
   Double  getChromaWeight () {return((m_cbDistortionWeight+m_crDistortionWeight)/2.0);}
-#endif
-#endif
   
   // Distortion Functions
@@ -176,11 +161,6 @@
   Void    setDistParam( UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc, DistParam& rcDistParam );
   Void    setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride,            DistParam& rcDistParam );
-#if NS_HAD
-  Void    setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME=false, Bool bUseNSHAD=false );
-  Void    setDistParam( DistParam& rcDP, Int bitDepth, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard = false, Bool bUseNSHAD=false );
-#else
   Void    setDistParam( TComPattern* pcPatternKey, Pel* piRefY, Int iRefStride, Int iStep, DistParam& rcDistParam, Bool bHADME=false );
   Void    setDistParam( DistParam& rcDP, Int bitDepth, Pel* p1, Int iStride1, Pel* p2, Int iStride2, Int iWidth, Int iHeight, Bool bHadamard = false );
-#endif
   
   UInt    calcHAD(Int bitDepth, Pel* pi0, Int iStride0, Pel* pi1, Int iStride1, Int iWidth, Int iHeight );
@@ -253,19 +233,8 @@
   static UInt xCalcHADs4x4      ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
   static UInt xCalcHADs8x8      ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
-#if NS_HAD
-  static UInt xCalcHADs16x4     ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
-  static UInt xCalcHADs4x16     ( Pel *piOrg, Pel *piCurr, Int iStrideOrg, Int iStrideCur, Int iStep );
-#endif
   
 public:
-#if WEIGHTED_CHROMA_DISTORTION
   UInt   getDistPart(Int bitDepth, Pel* piCur, Int iCurStride,  Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, TextType eText = TEXT_LUMA, DFunc eDFunc = DF_SSE );
-#else
-  UInt   getDistPart(Int bitDepth, Pel* piCur, Int iCurStride,  Pel* piOrg, Int iOrgStride, UInt uiBlkWidth, UInt uiBlkHeight, DFunc eDFunc = DF_SSE );
-#endif
-
-#if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT
-  UInt   getSADPart ( Int bitDepth, Pel* pelCur, Int curStride,  Pel* pelOrg, Int orgStride, UInt width, UInt height );
-#endif
+
 };// END CLASS DEFINITION TComRdCost
 
Index: trunk/source/Lib/TLibCommon/TComRom.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComRom.cpp	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComRom.cpp	(revision 540)
@@ -363,15 +363,4 @@
 const UInt g_uiGroupIdx[ 32 ]   = {0,1,2,3,4,4,5,5,6,6,6,6,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9};
 
-// Rice parameters for absolute transform levels
-const UInt g_auiGoRiceRange[5] =
-{
-  7, 14, 26, 46, 78
-};
-
-const UInt g_auiGoRicePrefixLen[5] =
-{
-  8, 7, 6, 5, 4
-};
-
 Void initSigLastScan(UInt* pBuffD, UInt* pBuffH, UInt* pBuffV, Int iWidth, Int iHeight)
 {
@@ -541,11 +530,4 @@
 Int g_mvScalingFactor  [MAX_LAYERS][2] = {{0,0}, {0,0}};
 Int g_posScalingFactor [MAX_LAYERS][2] = {{0,0}, {0,0}};
-
-#if IL_SL_SIGNALLING_N0371
-Int ref_scalingListDC         [MAX_LAYERS][SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM];
-Int ref_scalingListCoef       [MAX_LAYERS][SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][MAX_MATRIX_COEF_NUM];
-Int activeRefPPSId            [MAX_LAYERS];
-Int activeRefSPSId            [MAX_LAYERS];
-#endif 
 #endif
 
Index: trunk/source/Lib/TLibCommon/TComRom.h
===================================================================
--- trunk/source/Lib/TLibCommon/TComRom.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComRom.h	(revision 540)
@@ -131,7 +131,4 @@
 extern const UInt   g_uiMinInGroup[ 10 ];
 
-extern const UInt   g_auiGoRiceRange[5];                  //!< maximum value coded with Rice codes
-extern const UInt   g_auiGoRicePrefixLen[5];              //!< prefix length for each maximum value
-  
 extern const UInt   g_sigLastScan8x8[ 3 ][ 4 ];           //!< coefficient group scan order for 8x8 TUs
 extern       UInt   g_sigLastScanCG32x32[ 64 ];
@@ -291,12 +288,5 @@
 #if SVC_EXTENSION
 extern Int g_mvScalingFactor  [MAX_LAYERS][2];
-extern Int g_posScalingFactor [MAX_LAYERS][2];
-
-#if IL_SL_SIGNALLING_N0371
-extern Int ref_scalingListDC         [MAX_LAYERS][SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM];
-extern Int ref_scalingListCoef       [MAX_LAYERS][SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM][MAX_MATRIX_COEF_NUM];
-extern Int activeRefPPSId            [MAX_LAYERS];
-extern Int activeRefSPSId            [MAX_LAYERS];  
-#endif   
+extern Int g_posScalingFactor [MAX_LAYERS][2]; 
 #endif
 
Index: trunk/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComSampleAdaptiveOffset.cpp	(revision 540)
@@ -44,4 +44,659 @@
 //! \ingroup TLibCommon
 //! \{
+#if HM_CLEANUP_SAO
+UInt g_saoMaxOffsetQVal[NUM_SAO_COMPONENTS]; 
+
+SAOOffset::SAOOffset()
+{ 
+  reset();
+}
+
+SAOOffset::~SAOOffset()
+{
+
+}
+
+Void SAOOffset::reset()
+{
+  modeIdc = SAO_MODE_OFF;
+  typeIdc = -1;
+  typeAuxInfo = -1;
+  ::memset(offset, 0, sizeof(Int)* MAX_NUM_SAO_CLASSES);
+}
+
+const SAOOffset& SAOOffset::operator= (const SAOOffset& src)
+{
+  modeIdc = src.modeIdc;
+  typeIdc = src.typeIdc;
+  typeAuxInfo = src.typeAuxInfo;
+  ::memcpy(offset, src.offset, sizeof(Int)* MAX_NUM_SAO_CLASSES);
+
+  return *this;
+}
+
+
+SAOBlkParam::SAOBlkParam()
+{
+  reset();
+}
+
+SAOBlkParam::~SAOBlkParam()
+{
+
+}
+
+Void SAOBlkParam::reset()
+{
+  for(Int compIdx=0; compIdx< 3; compIdx++)
+  {
+    offsetParam[compIdx].reset();
+  }
+}
+
+const SAOBlkParam& SAOBlkParam::operator= (const SAOBlkParam& src)
+{
+  for(Int compIdx=0; compIdx< 3; compIdx++)
+  {
+    offsetParam[compIdx] = src.offsetParam[compIdx];
+  }
+  return *this;
+
+}
+
+TComSampleAdaptiveOffset::TComSampleAdaptiveOffset()
+{
+  m_tempPicYuv = NULL;
+  for(Int compIdx=0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
+  {
+    m_offsetClipTable[compIdx] = NULL;
+  }
+  m_signTable = NULL; 
+
+  
+  m_lineBufWidth = 0;
+  m_signLineBuf1 = NULL;
+  m_signLineBuf2 = NULL;
+}
+
+
+TComSampleAdaptiveOffset::~TComSampleAdaptiveOffset()
+{
+  destroy();
+  
+  if (m_signLineBuf1) delete[] m_signLineBuf1; m_signLineBuf1 = NULL;
+  if (m_signLineBuf2) delete[] m_signLineBuf2; m_signLineBuf2 = NULL;
+}
+
+#if AUXILIARY_PICTURES
+Void TComSampleAdaptiveOffset::create( Int picWidth, Int picHeight, ChromaFormat chromaFormatIDC, UInt maxCUWidth, UInt maxCUHeight, UInt maxCUDepth )
+#else
+Void TComSampleAdaptiveOffset::create( Int picWidth, Int picHeight, UInt maxCUWidth, UInt maxCUHeight, UInt maxCUDepth )
+#endif
+{
+  destroy();
+
+  m_picWidth = picWidth;    
+  m_picHeight= picHeight;
+  m_maxCUWidth= maxCUWidth; 
+  m_maxCUHeight= maxCUHeight;
+
+  m_numCTUInWidth = (m_picWidth/m_maxCUWidth) + ((m_picWidth % m_maxCUWidth)?1:0);
+  m_numCTUInHeight= (m_picHeight/m_maxCUHeight) + ((m_picHeight % m_maxCUHeight)?1:0);
+  m_numCTUsPic = m_numCTUInHeight*m_numCTUInWidth;
+
+  //temporary picture buffer
+  if ( !m_tempPicYuv )
+  {
+    m_tempPicYuv = new TComPicYuv;
+#if AUXILIARY_PICTURES
+    m_tempPicYuv->create( m_picWidth, m_picHeight, chromaFormatIDC, m_maxCUWidth, m_maxCUHeight, maxCUDepth );
+#else
+    m_tempPicYuv->create( m_picWidth, m_picHeight, m_maxCUWidth, m_maxCUHeight, maxCUDepth );
+#endif
+  }
+
+  //bit-depth related
+  for(Int compIdx =0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
+  {
+    Int bitDepthSample = (compIdx == SAO_Y)?g_bitDepthY:g_bitDepthC;
+    m_offsetStepLog2  [compIdx] = max(bitDepthSample - MAX_SAO_TRUNCATED_BITDEPTH, 0);
+    g_saoMaxOffsetQVal[compIdx] = (1<<(min(bitDepthSample,MAX_SAO_TRUNCATED_BITDEPTH)-5))-1; //Table 9-32, inclusive
+  }
+
+  //look-up table for clipping
+  for(Int compIdx =0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
+  {
+    Int bitDepthSample = (compIdx == SAO_Y)?g_bitDepthY:g_bitDepthC; //exclusive
+    Int maxSampleValue = (1<< bitDepthSample); //exclusive
+    Int maxOffsetValue = (g_saoMaxOffsetQVal[compIdx] << m_offsetStepLog2[compIdx]); 
+
+    m_offsetClipTable[compIdx] = new Int[(maxSampleValue + maxOffsetValue -1)+ (maxOffsetValue)+1 ]; //positive & negative range plus 0
+    m_offsetClip[compIdx] = &(m_offsetClipTable[compIdx][maxOffsetValue]);
+
+    //assign clipped values 
+    Int* offsetClipPtr = m_offsetClip[compIdx];
+    for(Int k=0; k< maxSampleValue; k++)
+    {
+      *(offsetClipPtr + k) = k;
+    }
+    for(Int k=0; k< maxOffsetValue; k++ )
+    {
+      *(offsetClipPtr + maxSampleValue+ k) = maxSampleValue-1;
+      *(offsetClipPtr -k -1 )              = 0;
+    }
+    if(compIdx == SAO_Y) //g_bitDepthY is always larger than or equal to g_bitDepthC
+    {
+      m_signTable = new Short[ 2*(maxSampleValue-1) + 1 ];
+      m_sign = &(m_signTable[maxSampleValue-1]);
+
+      m_sign[0] = 0;
+      for(Int k=1; k< maxSampleValue; k++)
+      {
+        m_sign[k] = 1;
+        m_sign[-k]= -1;
+      }
+    }
+  }  
+
+}
+
+Void TComSampleAdaptiveOffset::destroy()
+{
+  if ( m_tempPicYuv )
+  {
+    m_tempPicYuv->destroy();
+    delete m_tempPicYuv;
+    m_tempPicYuv = NULL;
+  }
+
+  for(Int compIdx=0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
+  {
+    if(m_offsetClipTable[compIdx])
+    {
+      delete[] m_offsetClipTable[compIdx]; m_offsetClipTable[compIdx] = NULL;
+    }
+  }
+  if( m_signTable )
+  {
+    delete[] m_signTable; m_signTable = NULL;
+  }
+}
+
+Void TComSampleAdaptiveOffset::invertQuantOffsets(Int compIdx, Int typeIdc, Int typeAuxInfo, Int* dstOffsets, Int* srcOffsets)
+{
+  Int codedOffset[MAX_NUM_SAO_CLASSES];
+
+  ::memcpy(codedOffset, srcOffsets, sizeof(Int)*MAX_NUM_SAO_CLASSES);
+  ::memset(dstOffsets, 0, sizeof(Int)*MAX_NUM_SAO_CLASSES);
+
+  if(typeIdc == SAO_TYPE_START_BO)
+  {
+    for(Int i=0; i< 4; i++)
+    {
+      dstOffsets[(typeAuxInfo+ i)%NUM_SAO_BO_CLASSES] = codedOffset[(typeAuxInfo+ i)%NUM_SAO_BO_CLASSES]*(1<<m_offsetStepLog2[compIdx]);
+    }
+  }
+  else //EO
+  {
+    for(Int i=0; i< NUM_SAO_EO_CLASSES; i++)
+    {
+      dstOffsets[i] = codedOffset[i] *(1<<m_offsetStepLog2[compIdx]);
+    }
+    assert(dstOffsets[SAO_CLASS_EO_PLAIN] == 0); //keep EO plain offset as zero
+  }
+
+}
+
+Int TComSampleAdaptiveOffset::getMergeList(TComPic* pic, Int ctu, SAOBlkParam* blkParams, std::vector<SAOBlkParam*>& mergeList)
+{
+  Int ctuX = ctu % m_numCTUInWidth;
+  Int ctuY = ctu / m_numCTUInWidth;
+  Int mergedCTUPos;
+  Int numValidMergeCandidates = 0;
+
+  for(Int mergeType=0; mergeType< NUM_SAO_MERGE_TYPES; mergeType++)
+  {
+    SAOBlkParam* mergeCandidate = NULL;
+
+    switch(mergeType)
+    {
+    case SAO_MERGE_ABOVE:
+      {
+        if(ctuY > 0)
+        {
+          mergedCTUPos = ctu- m_numCTUInWidth;
+          if( pic->getSAOMergeAvailability(ctu, mergedCTUPos) )
+          {
+            mergeCandidate = &(blkParams[mergedCTUPos]);
+          }
+        }
+      }
+      break;
+    case SAO_MERGE_LEFT:
+      {
+        if(ctuX > 0)
+        {
+          mergedCTUPos = ctu- 1;
+          if( pic->getSAOMergeAvailability(ctu, mergedCTUPos) )
+          {
+            mergeCandidate = &(blkParams[mergedCTUPos]);
+          }
+        }
+      }
+      break;
+    default:
+      {
+        printf("not a supported merge type");
+        assert(0);
+        exit(-1);
+      }
+    }
+
+    mergeList.push_back(mergeCandidate);
+    if (mergeCandidate != NULL)
+    {
+      numValidMergeCandidates++;
+    }
+  }
+
+  return numValidMergeCandidates;
+}
+
+
+Void TComSampleAdaptiveOffset::reconstructBlkSAOParam(SAOBlkParam& recParam, std::vector<SAOBlkParam*>& mergeList)
+{
+  for(Int compIdx=0; compIdx< NUM_SAO_COMPONENTS; compIdx++)
+  {
+    SAOOffset& offsetParam = recParam[compIdx];
+
+    if(offsetParam.modeIdc == SAO_MODE_OFF)
+    {
+      continue;
+    }
+
+    switch(offsetParam.modeIdc)
+    {
+    case SAO_MODE_NEW:
+      {
+        invertQuantOffsets(compIdx, offsetParam.typeIdc, offsetParam.typeAuxInfo, offsetParam.offset, offsetParam.offset);
+      }
+      break;
+    case SAO_MODE_MERGE:
+      {
+        SAOBlkParam* mergeTarget = mergeList[offsetParam.typeIdc];
+        assert(mergeTarget != NULL);
+
+        offsetParam = (*mergeTarget)[compIdx];
+      }
+      break;
+    default:
+      {
+        printf("Not a supported mode");
+        assert(0);
+        exit(-1);
+      }
+    }
+  }
+}
+
+Void TComSampleAdaptiveOffset::reconstructBlkSAOParams(TComPic* pic, SAOBlkParam* saoBlkParams)
+{
+  m_picSAOEnabled[SAO_Y] = m_picSAOEnabled[SAO_Cb] = m_picSAOEnabled[SAO_Cr] = false;
+
+  for(Int ctu=0; ctu< m_numCTUsPic; ctu++)
+  {
+    std::vector<SAOBlkParam*> mergeList;
+    getMergeList(pic, ctu, saoBlkParams, mergeList);
+
+    reconstructBlkSAOParam(saoBlkParams[ctu], mergeList);
+
+    for(Int compIdx=0; compIdx< NUM_SAO_COMPONENTS; compIdx++)
+    {
+      if(saoBlkParams[ctu][compIdx].modeIdc != SAO_MODE_OFF)
+      {
+        m_picSAOEnabled[compIdx] = true;
+      }
+    }
+  }
+
+
+}
+
+
+Void TComSampleAdaptiveOffset::offsetBlock(Int compIdx, Int typeIdx, Int* offset  
+                                          , Pel* srcBlk, Pel* resBlk, Int srcStride, Int resStride,  Int width, Int height
+                                          , Bool isLeftAvail,  Bool isRightAvail, Bool isAboveAvail, Bool isBelowAvail, Bool isAboveLeftAvail, Bool isAboveRightAvail, Bool isBelowLeftAvail, Bool isBelowRightAvail)
+{
+  if(m_lineBufWidth != m_maxCUWidth)
+  {
+    m_lineBufWidth = m_maxCUWidth;
+    
+    if (m_signLineBuf1) delete[] m_signLineBuf1; m_signLineBuf1 = NULL;
+    m_signLineBuf1 = new Char[m_lineBufWidth+1];
+    
+    if (m_signLineBuf2) delete[] m_signLineBuf2; m_signLineBuf2 = NULL;
+    m_signLineBuf2 = new Char[m_lineBufWidth+1];
+  }
+
+  Int* offsetClip = m_offsetClip[compIdx];
+
+  Int x,y, startX, startY, endX, endY, edgeType;
+  Int firstLineStartX, firstLineEndX, lastLineStartX, lastLineEndX;
+  Char signLeft, signRight, signDown;
+
+  Pel* srcLine = srcBlk;
+  Pel* resLine = resBlk;
+
+  switch(typeIdx)
+  {
+  case SAO_TYPE_EO_0:
+    {
+      offset += 2;
+      startX = isLeftAvail ? 0 : 1;
+      endX   = isRightAvail ? width : (width -1);
+      for (y=0; y< height; y++)
+      {
+        signLeft = (Char)m_sign[srcLine[startX] - srcLine[startX-1]];
+        for (x=startX; x< endX; x++)
+        {
+          signRight = (Char)m_sign[srcLine[x] - srcLine[x+1]]; 
+          edgeType =  signRight + signLeft;
+          signLeft  = -signRight;
+
+          resLine[x] = offsetClip[srcLine[x] + offset[edgeType]];
+        }
+        srcLine  += srcStride;
+        resLine += resStride;
+      }
+
+    }
+    break;
+  case SAO_TYPE_EO_90:
+    {
+      offset += 2;
+      Char *signUpLine = m_signLineBuf1;
+
+      startY = isAboveAvail ? 0 : 1;
+      endY   = isBelowAvail ? height : height-1;
+      if (!isAboveAvail)
+      {
+        srcLine += srcStride;
+        resLine += resStride;
+      }
+
+      Pel* srcLineAbove= srcLine- srcStride;
+      for (x=0; x< width; x++)
+      {
+        signUpLine[x] = (Char)m_sign[srcLine[x] - srcLineAbove[x]];
+      }
+
+      Pel* srcLineBelow;
+      for (y=startY; y<endY; y++)
+      {
+        srcLineBelow= srcLine+ srcStride;
+
+        for (x=0; x< width; x++)
+        {
+          signDown  = (Char)m_sign[srcLine[x] - srcLineBelow[x]]; 
+          edgeType = signDown + signUpLine[x];
+          signUpLine[x]= -signDown;
+
+          resLine[x] = offsetClip[srcLine[x] + offset[edgeType]];
+        }
+        srcLine += srcStride;
+        resLine += resStride;
+      }
+
+    }
+    break;
+  case SAO_TYPE_EO_135:
+    {
+      offset += 2;
+      Char *signUpLine, *signDownLine, *signTmpLine;
+
+      signUpLine  = m_signLineBuf1;
+      signDownLine= m_signLineBuf2;
+
+      startX = isLeftAvail ? 0 : 1 ;
+      endX   = isRightAvail ? width : (width-1);
+
+      //prepare 2nd line's upper sign
+      Pel* srcLineBelow= srcLine+ srcStride;
+      for (x=startX; x< endX+1; x++)
+      {
+        signUpLine[x] = (Char)m_sign[srcLineBelow[x] - srcLine[x- 1]];
+      }
+
+      //1st line
+      Pel* srcLineAbove= srcLine- srcStride;
+      firstLineStartX = isAboveLeftAvail ? 0 : 1;
+      firstLineEndX   = isAboveAvail? endX: 1;
+      for(x= firstLineStartX; x< firstLineEndX; x++)
+      {
+        edgeType  =  m_sign[srcLine[x] - srcLineAbove[x- 1]] - signUpLine[x+1];
+        resLine[x] = offsetClip[srcLine[x] + offset[edgeType]];
+      }
+      srcLine  += srcStride;
+      resLine  += resStride;
+
+
+      //middle lines
+      for (y= 1; y< height-1; y++)
+      {
+        srcLineBelow= srcLine+ srcStride;
+
+        for (x=startX; x<endX; x++)
+        {
+          signDown =  (Char)m_sign[srcLine[x] - srcLineBelow[x+ 1]] ;
+          edgeType =  signDown + signUpLine[x];
+          resLine[x] = offsetClip[srcLine[x] + offset[edgeType]];
+
+          signDownLine[x+1] = -signDown; 
+        }
+        signDownLine[startX] = (Char)m_sign[srcLineBelow[startX] - srcLine[startX-1]];
+
+        signTmpLine  = signUpLine;
+        signUpLine   = signDownLine;
+        signDownLine = signTmpLine;
+
+        srcLine += srcStride;
+        resLine += resStride;
+      }
+
+      //last line
+      srcLineBelow= srcLine+ srcStride;
+      lastLineStartX = isBelowAvail ? startX : (width -1);
+      lastLineEndX   = isBelowRightAvail ? width : (width -1);
+      for(x= lastLineStartX; x< lastLineEndX; x++)
+      {
+        edgeType =  m_sign[srcLine[x] - srcLineBelow[x+ 1]] + signUpLine[x];
+        resLine[x] = offsetClip[srcLine[x] + offset[edgeType]];
+
+      }
+    }
+    break;
+  case SAO_TYPE_EO_45:
+    {
+      offset += 2;
+      Char *signUpLine = m_signLineBuf1+1;
+
+      startX = isLeftAvail ? 0 : 1;
+      endX   = isRightAvail ? width : (width -1);
+
+      //prepare 2nd line upper sign
+      Pel* srcLineBelow= srcLine+ srcStride;
+      for (x=startX-1; x< endX; x++)
+      {
+        signUpLine[x] = (Char)m_sign[srcLineBelow[x] - srcLine[x+1]];
+      }
+
+
+      //first line
+      Pel* srcLineAbove= srcLine- srcStride;
+      firstLineStartX = isAboveAvail ? startX : (width -1 );
+      firstLineEndX   = isAboveRightAvail ? width : (width-1);
+      for(x= firstLineStartX; x< firstLineEndX; x++)
+      {
+        edgeType = m_sign[srcLine[x] - srcLineAbove[x+1]] -signUpLine[x-1];
+        resLine[x] = offsetClip[srcLine[x] + offset[edgeType]];
+      }
+      srcLine += srcStride;
+      resLine += resStride;
+
+      //middle lines
+      for (y= 1; y< height-1; y++)
+      {
+        srcLineBelow= srcLine+ srcStride;
+
+        for(x= startX; x< endX; x++)
+        {
+          signDown =  (Char)m_sign[srcLine[x] - srcLineBelow[x-1]] ;
+          edgeType =  signDown + signUpLine[x];
+          resLine[x] = offsetClip[srcLine[x] + offset[edgeType]];
+          signUpLine[x-1] = -signDown; 
+        }
+        signUpLine[endX-1] = (Char)m_sign[srcLineBelow[endX-1] - srcLine[endX]];
+        srcLine  += srcStride;
+        resLine += resStride;
+      }
+
+      //last line
+      srcLineBelow= srcLine+ srcStride;
+      lastLineStartX = isBelowLeftAvail ? 0 : 1;
+      lastLineEndX   = isBelowAvail ? endX : 1;
+      for(x= lastLineStartX; x< lastLineEndX; x++)
+      {
+        edgeType = m_sign[srcLine[x] - srcLineBelow[x-1]] + signUpLine[x];
+        resLine[x] = offsetClip[srcLine[x] + offset[edgeType]];
+
+      }
+    }
+    break;
+  case SAO_TYPE_BO:
+    {
+      Int shiftBits = ((compIdx == SAO_Y)?g_bitDepthY:g_bitDepthC)- NUM_SAO_BO_CLASSES_LOG2;
+      for (y=0; y< height; y++)
+      {
+        for (x=0; x< width; x++)
+        {
+          resLine[x] = offsetClip[ srcLine[x] + offset[srcLine[x] >> shiftBits] ];
+        }
+        srcLine += srcStride;
+        resLine += resStride;
+      }
+    }
+    break;
+  default:
+    {
+      printf("Not a supported SAO types\n");
+      assert(0);
+      exit(-1);
+    }
+  }
+
+
+}
+
+Void TComSampleAdaptiveOffset::offsetCTU(Int ctu, TComPicYuv* srcYuv, TComPicYuv* resYuv, SAOBlkParam& saoblkParam, TComPic* pPic)
+{
+  Bool isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail;
+
+  if( 
+    (saoblkParam[SAO_Y ].modeIdc == SAO_MODE_OFF) &&
+    (saoblkParam[SAO_Cb].modeIdc == SAO_MODE_OFF) &&
+    (saoblkParam[SAO_Cr].modeIdc == SAO_MODE_OFF)
+    )
+  {
+    return;
+  }
+
+  //block boundary availability
+  pPic->getPicSym()->deriveLoopFilterBoundaryAvailibility(ctu, isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail);
+
+  Int yPos   = (ctu / m_numCTUInWidth)*m_maxCUHeight;
+  Int xPos   = (ctu % m_numCTUInWidth)*m_maxCUWidth;
+  Int height = (yPos + m_maxCUHeight > m_picHeight)?(m_picHeight- yPos):m_maxCUHeight;
+  Int width  = (xPos + m_maxCUWidth  > m_picWidth )?(m_picWidth - xPos):m_maxCUWidth;
+
+  for(Int compIdx= 0; compIdx < NUM_SAO_COMPONENTS; compIdx++)
+  {
+    SAOOffset& ctbOffset = saoblkParam[compIdx];
+
+    if(ctbOffset.modeIdc != SAO_MODE_OFF)
+    {
+      Bool isLuma     = (compIdx == SAO_Y);
+      Int  formatShift= isLuma?0:1;
+
+      Int  blkWidth   = (width  >> formatShift);
+      Int  blkHeight  = (height >> formatShift);
+      Int  blkYPos    = (yPos   >> formatShift);
+      Int  blkXPos    = (xPos   >> formatShift);
+
+      Int  srcStride = isLuma?srcYuv->getStride():srcYuv->getCStride();
+      Pel* srcBlk    = getPicBuf(srcYuv, compIdx)+ (yPos >> formatShift)*srcStride+ (xPos >> formatShift);
+
+      Int  resStride  = isLuma?resYuv->getStride():resYuv->getCStride();
+      Pel* resBlk     = getPicBuf(resYuv, compIdx)+ blkYPos*resStride+ blkXPos;
+
+      offsetBlock( compIdx, ctbOffset.typeIdc, ctbOffset.offset
+                  , srcBlk, resBlk, srcStride, resStride, blkWidth, blkHeight
+                  , isLeftAvail, isRightAvail
+                  , isAboveAvail, isBelowAvail
+                  , isAboveLeftAvail, isAboveRightAvail
+                  , isBelowLeftAvail, isBelowRightAvail
+                  );
+    }
+  } //compIdx
+
+}
+
+
+Void TComSampleAdaptiveOffset::SAOProcess(TComPic* pDecPic)
+{
+  if(!m_picSAOEnabled[SAO_Y] && !m_picSAOEnabled[SAO_Cb] && !m_picSAOEnabled[SAO_Cr])
+  {
+    return;
+  }
+  TComPicYuv* resYuv = pDecPic->getPicYuvRec();
+  TComPicYuv* srcYuv = m_tempPicYuv;
+  resYuv->copyToPic(srcYuv);
+  for(Int ctu= 0; ctu < m_numCTUsPic; ctu++)
+  {
+    offsetCTU(ctu, srcYuv, resYuv, (pDecPic->getPicSym()->getSAOBlkParam())[ctu], pDecPic);
+  } //ctu
+}
+
+
+Pel* TComSampleAdaptiveOffset::getPicBuf(TComPicYuv* pPicYuv, Int compIdx)
+{
+  Pel* pBuf = NULL;
+  switch(compIdx)
+  {
+  case SAO_Y:
+    {
+      pBuf = pPicYuv->getLumaAddr();
+    }
+    break;
+  case SAO_Cb:
+    {
+      pBuf = pPicYuv->getCbAddr();
+    }
+    break;
+  case SAO_Cr:
+    {
+      pBuf = pPicYuv->getCrAddr();
+    }
+    break;
+  default:
+    {
+      printf("Not a legal component ID for SAO\n");
+      assert(0);
+      exit(-1);
+    }
+  }
+
+  return pBuf;
+}
+#else
 
 SAOParam::~SAOParam()
@@ -1364,5 +2019,5 @@
   }
 }
-
+#endif
 /** PCM LF disable process. 
  * \param pcPic picture (TComPic) pointer
Index: trunk/source/Lib/TLibCommon/TComSampleAdaptiveOffset.h
===================================================================
--- trunk/source/Lib/TLibCommon/TComSampleAdaptiveOffset.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComSampleAdaptiveOffset.h	(revision 540)
@@ -45,4 +45,66 @@
 //! \{
 
+
+#if HM_CLEANUP_SAO
+// ====================================================================================================================
+// Constants
+// ====================================================================================================================
+
+#define MAX_SAO_TRUNCATED_BITDEPTH     10 
+// ====================================================================================================================
+// Class definition
+// ====================================================================================================================
+extern UInt g_saoMaxOffsetQVal[NUM_SAO_COMPONENTS]; 
+
+class TComSampleAdaptiveOffset
+{
+public:
+  TComSampleAdaptiveOffset();
+  virtual ~TComSampleAdaptiveOffset();
+  Void SAOProcess(TComPic* pDecPic);
+#if AUXILIARY_PICTURES
+  Void create( Int picWidth, Int picHeight, ChromaFormat chromaFormatIDC, UInt maxCUWidth, UInt maxCUHeight, UInt maxCUDepth );
+#else
+  Void create( Int picWidth, Int picHeight, UInt maxCUWidth, UInt maxCUHeight, UInt maxCUDepth );
+#endif
+  Void destroy();
+  Void reconstructBlkSAOParams(TComPic* pic, SAOBlkParam* saoBlkParams);
+  Void PCMLFDisableProcess (TComPic* pcPic);
+protected:
+  Void offsetBlock(Int compIdx, Int typeIdx, Int* offset, Pel* srcBlk, Pel* resBlk, Int srcStride, Int resStride,  Int width, Int height
+                  , Bool isLeftAvail, Bool isRightAvail, Bool isAboveAvail, Bool isBelowAvail, Bool isAboveLeftAvail, Bool isAboveRightAvail, Bool isBelowLeftAvail, Bool isBelowRightAvail);
+  Pel* getPicBuf(TComPicYuv* pPicYuv, Int compIdx);
+  Void invertQuantOffsets(Int compIdx, Int typeIdc, Int typeAuxInfo, Int* dstOffsets, Int* srcOffsets);
+  Void reconstructBlkSAOParam(SAOBlkParam& recParam, std::vector<SAOBlkParam*>& mergeList);
+  Int  getMergeList(TComPic* pic, Int ctu, SAOBlkParam* blkParams, std::vector<SAOBlkParam*>& mergeList);
+  Void offsetCTU(Int ctu, TComPicYuv* srcYuv, TComPicYuv* resYuv, SAOBlkParam& saoblkParam, TComPic* pPic);
+  Void xPCMRestoration(TComPic* pcPic);
+  Void xPCMCURestoration ( TComDataCU* pcCU, UInt uiAbsZorderIdx, UInt uiDepth );
+  Void xPCMSampleRestoration (TComDataCU* pcCU, UInt uiAbsZorderIdx, UInt uiDepth, TextType ttText);
+protected:
+  UInt m_offsetStepLog2[NUM_SAO_COMPONENTS]; //offset step  
+  Int* m_offsetClip[NUM_SAO_COMPONENTS]; //clip table for fast operation
+  Short* m_sign; //sign table for fast operation
+  TComPicYuv*   m_tempPicYuv; //temporary buffer
+  Int m_picWidth;
+  Int m_picHeight;
+  Int m_maxCUWidth;
+  Int m_maxCUHeight;
+  Int m_numCTUInWidth;
+  Int m_numCTUInHeight;
+  Int m_numCTUsPic;
+  
+  
+  Int m_lineBufWidth;
+  Char* m_signLineBuf1;
+  Char* m_signLineBuf2;
+private:
+  Bool m_picSAOEnabled[NUM_SAO_COMPONENTS];
+  Int*   m_offsetClipTable[NUM_SAO_COMPONENTS];
+  Short* m_signTable;
+
+};
+#else
+
 // ====================================================================================================================
 // Constants
@@ -145,4 +207,6 @@
 };
 
+#endif
+
 //! \}
 #endif
Index: trunk/source/Lib/TLibCommon/TComSlice.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComSlice.cpp	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComSlice.cpp	(revision 540)
@@ -48,8 +48,4 @@
 ParameterSetMap<TComVPS> ParameterSetManager::m_vpsMap(MAX_NUM_VPS);
 Int ParameterSetManager::m_activeVPSId = -1;
-#if IL_SL_SIGNALLING_N0371
-TComSPS* TComSPS::m_pcSPS[MAX_LAYERS];
-TComPPS* TComPPS::m_pcPPS[MAX_LAYERS];
-#endif
 #endif
 
@@ -80,10 +76,4 @@
 , m_colFromL0Flag                 ( 1 )
 , m_colRefIdx                     ( 0 )
-#if SAO_CHROMA_LAMBDA
-, m_dLambdaLuma( 0.0 )
-, m_dLambdaChroma( 0.0 )
-#else
-, m_dLambda                       ( 0.0 )
-#endif
 , m_uiTLayer                      ( 0 )
 , m_bTLayerSwitchingFlag          ( false )
@@ -118,4 +108,7 @@
 #endif
 , m_bDiscardableFlag              ( false )
+#if O0149_CROSS_LAYER_BLA_FLAG
+, m_bCrossLayerBLAFlag            ( false )
+#endif
 #endif //SVC_EXTENSION
 {
@@ -143,4 +136,9 @@
   initEqualRef();
   
+  for (Int component = 0; component < 3; component++)
+  {
+    m_lambdas[component] = 0.0;
+  }
+  
   for ( Int idx = 0; idx < MAX_NUM_REF; idx++ )
   {
@@ -157,4 +155,7 @@
   initWpAcDcParam();
   m_saoEnabledFlag = false;
+#if HM_CLEANUP_SAO
+  m_saoEnabledFlagChroma = false;
+#endif
 }
 
@@ -276,4 +277,11 @@
     }
     iterPic++;
+#if SVC_EXTENSION
+    // return NULL, if picture with requested POC is not in the list, otherwise iterator goes outside of the list
+    if( iterPic == rcListPic.end() )
+    {
+      return NULL;
+    }
+#endif
     pcPic = *(iterPic);
   }
@@ -308,5 +316,9 @@
       }
       
+#if POC_RESET_RPS
+      if( ((!pocHasMsb) && ((poc & (pocCycle-1)) == picPoc)) || ( pocHasMsb && (poc == picPoc)) )
+#else
       if (poc == picPoc)
+#endif
       {
        if (pcPic->getIsLongTerm())
@@ -357,5 +369,4 @@
 }
 
-#if FIX1071
 #if SVC_EXTENSION
 Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr, TComPic** ilpPic)
@@ -363,11 +374,6 @@
 Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr )
 #endif
-#else
-Void TComSlice::setRefPicList( TComList<TComPic*>& rcListPic )
-#endif
-{
-#if FIX1071
+{
   if (!checkNumPocTotalCurr)
-#endif
   {
     if (m_eSliceType == I_SLICE)
@@ -522,4 +528,5 @@
       memset( refResamplingLayer, 0, sizeof( refResamplingLayer ) );
 #endif
+#if !RESAMPLING_CONSTRAINT_BUG_FIX
       const Window &scalEL = getSPS()->getScaledRefLayerWindow(m_interLayerPredLayerIdc[i]);
       Int scalingOffset = ((scalEL.getWindowLeftOffset()   == 0 ) && 
@@ -528,8 +535,21 @@
                            (scalEL.getWindowBottomOffset() == 0 ) 
                           );
+#endif
 
       for( i=0; i < m_activeNumILRRefIdx; i++ )
       {
         UInt refLayerIdc = m_interLayerPredLayerIdc[i];
+#if RESAMPLING_CONSTRAINT_BUG_FIX
+#if O0098_SCALED_REF_LAYER_ID
+        const Window &scalEL = getSPS()->getScaledRefLayerWindowForLayer(m_pcVPS->getRefLayerId( m_layerId, m_interLayerPredLayerIdc[i] ));
+#else
+        const Window &scalEL = getSPS()->getScaledRefLayerWindow(m_interLayerPredLayerIdc[i]);
+#endif
+        Int scalingOffset = ((scalEL.getWindowLeftOffset()   == 0 ) && 
+                             (scalEL.getWindowRightOffset()  == 0 ) && 
+                             (scalEL.getWindowTopOffset()    == 0 ) && 
+                             (scalEL.getWindowBottomOffset() == 0 ) 
+                            );
+#endif
         if(!( g_posScalingFactor[refLayerIdc][0] == 65536 && g_posScalingFactor[refLayerIdc][1] == 65536 ) || (!scalingOffset)) // ratio 1x
         {
@@ -585,5 +605,5 @@
   Int numPocTotalCurr = NumPocStCurr0 + NumPocStCurr1 + NumPocLtCurr;
 #endif //SVC_EXTENSION
-#if FIX1071
+
   if (checkNumPocTotalCurr)
   {
@@ -615,5 +635,4 @@
     m_aiNumRefIdx[1] = getNumRefIdx(REF_PIC_LIST_1);
   }
-#endif
 
 #if M0457_IL_SAMPLE_PRED_ONLY_FLAG
@@ -932,5 +951,5 @@
     numRpsCurrTempList += getNumILRRefIdx();
 #endif
-  }
+}
 #endif
 
@@ -1038,5 +1057,9 @@
  * If the current picture has a nal_ref_idc that is not 0, it will remain marked as "used for reference".
  */
+#if NO_CLRAS_OUTPUT_FLAG
+Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic, Bool noClrasOutputFlag)
+#else
 Void TComSlice::decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic)
+#endif
 {
   TComPic*                 rpcPic;
@@ -1056,5 +1079,16 @@
       rpcPic = *(iterPic);
       rpcPic->setCurrSliceIdx(0);
+#if NO_CLRAS_OUTPUT_FLAG
+      if (noClrasOutputFlag)
+      {
+        if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);  // all layers
+      }
+      else
+      {
+        if (rpcPic->getPOC() != pocCurr && rpcPic->getLayerId() == m_layerId) rpcPic->getSlice(0)->setReferenced(false);  // only current layer
+      }
+#else
       if (rpcPic->getPOC() != pocCurr) rpcPic->getSlice(0)->setReferenced(false);
+#endif
       iterPic++;
     }
@@ -1160,10 +1194,5 @@
   m_colFromL0Flag        = pSrc->m_colFromL0Flag;
   m_colRefIdx            = pSrc->m_colRefIdx;
-#if SAO_CHROMA_LAMBDA 
-  m_dLambdaLuma          = pSrc->m_dLambdaLuma;
-  m_dLambdaChroma        = pSrc->m_dLambdaChroma;
-#else
-  m_dLambda              = pSrc->m_dLambda;
-#endif
+  setLambdas(pSrc->getLambdas());
   for (i = 0; i < 2; i++)
   {
@@ -1490,5 +1519,5 @@
     assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getUsedByCurr()==0||rpcPic->getTLayer()<=this->getTLayer());
     //check that pictures of higher or equal temporal layer are not in the RPS if the current picture is a TSA picture
-    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
+    if(this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_R || this->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N)
     {
       assert(rpcPic->getSlice( 0 )->isReferenced()==0||rpcPic->getTLayer()<this->getTLayer());
@@ -1649,9 +1678,5 @@
 /** Function for constructing an explicit Reference Picture Set out of the available pictures in a referenced Reference Picture Set
 */
-#if FIX1071
 Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool isRAP)
-#else
-Void TComSlice::createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet)
-#endif
 {
   TComPic* rpcPic;
@@ -1678,9 +1703,5 @@
         // and should be added to the explicit Reference Picture Set
         pcRPS->setDeltaPOC(k, pReferencePictureSet->getDeltaPOC(i));
-#if FIX1071
         pcRPS->setUsed(k, pReferencePictureSet->getUsed(i) && (!isRAP));
-#else
-        pcRPS->setUsed(k, pReferencePictureSet->getUsed(i));
-#endif
         if(pcRPS->getDeltaPOC(k) < 0)
         {
@@ -1834,4 +1855,14 @@
   TComVPS *vps = getVPS();
   UInt retVal, layerId = getLayerId();
+#if O0096_REP_FORMAT_INDEX
+  if ( layerId == 0 )
+  {
+    retVal = sps->getPicWidthInLumaSamples();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getPicWidthVpsInLumaSamples();
+  }
+#else
   if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
   {
@@ -1842,4 +1873,5 @@
     retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getPicWidthVpsInLumaSamples();
   }
+#endif
   return retVal;
 }
@@ -1849,4 +1881,14 @@
   TComVPS *vps = getVPS();
   UInt retVal, layerId = getLayerId();
+#if O0096_REP_FORMAT_INDEX
+  if( layerId == 0 )
+  {
+    retVal = sps->getPicHeightInLumaSamples();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getPicHeightVpsInLumaSamples();
+  }
+#else
   if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
   {
@@ -1857,4 +1899,5 @@
     retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getPicHeightVpsInLumaSamples();
   }
+#endif
   return retVal;
 }
@@ -1873,4 +1916,14 @@
   UInt retVal, layerId = getLayerId();
 #endif
+#if O0096_REP_FORMAT_INDEX
+  if( layerId == 0 )
+  {
+    retVal = sps->getChromaFormatIdc();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getChromaFormatVpsIdc();
+  }
+#else
   if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
   {
@@ -1881,4 +1934,5 @@
     retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getChromaFormatVpsIdc();
   }
+#endif
   return retVal;
 }
@@ -1888,4 +1942,14 @@
   TComVPS *vps = getVPS();
   UInt retVal, layerId = getLayerId();
+#if O0096_REP_FORMAT_INDEX
+  if( layerId == 0 )
+  {
+    retVal = sps->getBitDepthY();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getBitDepthVpsLuma();
+  }
+#else
   if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
   {
@@ -1896,4 +1960,5 @@
     retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getBitDepthVpsLuma();
   }
+#endif
   return retVal;
 }
@@ -1903,4 +1968,14 @@
   TComVPS *vps = getVPS();
   UInt retVal, layerId = getLayerId();
+#if O0096_REP_FORMAT_INDEX
+  if( layerId == 0 )
+  {
+    retVal = sps->getBitDepthC();
+  }
+  else
+  {
+    retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(sps->getUpdateRepFormatFlag() ? sps->getUpdateRepFormatIndex() : layerId) )->getBitDepthVpsChroma();
+  }
+#else
   if( ( layerId == 0 ) || sps->getUpdateRepFormatFlag() )
   {
@@ -1911,4 +1986,5 @@
     retVal = vps->getVpsRepFormat( vps->getVpsRepFormatIdx(layerId) )->getBitDepthVpsChroma();
   }
+#endif
   return retVal;
 }
@@ -2023,4 +2099,7 @@
   ::memset(m_pocLsbNotPresentFlag, 0, sizeof(m_pocLsbNotPresentFlag));
 #endif
+#if O0223_PICTURE_TYPES_ALIGN_FLAG
+  m_crossLayerPictureTypeAlignFlag = true;
+#endif 
 #if N0147_IRAP_ALIGN_FLAG
   m_crossLayerIrapAlignFlag = true;
@@ -2042,7 +2121,16 @@
   }
 #endif
+#if VPS_VUI_TILES_NOT_IN_USE__FLAG
+   m_tilesNotInUseFlag = true;
+   ::memset(m_tilesInUseFlag,  0, sizeof(m_tilesInUseFlag));
+   ::memset(m_loopFilterNotAcrossTilesFlag,  0, sizeof(m_loopFilterNotAcrossTilesFlag));
+#endif
 #if TILE_BOUNDARY_ALIGNED_FLAG
    ::memset(m_tileBoundariesAlignedFlag,  0, sizeof(m_tileBoundariesAlignedFlag));
 #endif 
+#if VPS_VUI_WPP_NOT_IN_USE__FLAG
+   m_wppNotInUseFlag = true;
+   ::memset(m_wppInUseFlag,  0, sizeof(m_wppInUseFlag));
+#endif
 #if N0160_VUI_EXT_ILP_REF
    m_numIlpRestrictedRefLayers = false;
@@ -2050,4 +2138,18 @@
    ::memset(m_ctuBasedOffsetEnabledFlag,     0, sizeof(m_ctuBasedOffsetEnabledFlag));
    ::memset(m_minHorizontalCtuOffsetPlus1,   0, sizeof(m_minHorizontalCtuOffsetPlus1));
+#endif
+#if VPS_VUI_VIDEO_SIGNAL
+    m_vidSigPresentVpsFlag=true;
+    m_vpsVidSigInfo=1;
+    ::memset( m_vpsVidSigIdx, 0, sizeof(m_vpsVidSigIdx) );
+    m_vpsVidSigIdx[0]=0;
+    for (Int i=0; i < 16; i++)
+    {
+      m_vpsVidFormat[i] = 5;
+      m_vpsFullRangeFlag[i] = false;
+      m_vpsColorPrimaries[i] = 2;
+      m_vpsTransChar[i] = 2;
+      m_vpsMatCoeff[i] = 2;
+    }
 #endif
 #if VPS_VUI_BITRATE_PICRATE  
@@ -2064,4 +2166,22 @@
 #if VIEW_ID_RELATED_SIGNALING 
   ::memset(m_viewIdVal, 0, sizeof(m_viewIdVal));
+#endif
+#if O0092_0094_DEPENDENCY_CONSTRAINT
+  for (Int i = 0; i < MAX_NUM_LAYER_IDS; i++)
+  {
+    m_numberRefLayers[i] = 0;
+    for (Int j = 0; j < MAX_NUM_LAYER_IDS; j++)
+    {
+      m_recursiveRefLayerFlag[i][j] = 0;
+    }
+  }
+#endif
+#if VPS_DPB_SIZE_TABLE
+  ::memset( m_subLayerFlagInfoPresentFlag,  0, sizeof(m_subLayerFlagInfoPresentFlag ) );
+  ::memset( m_subLayerDpbInfoPresentFlag,   0, sizeof(m_subLayerDpbInfoPresentFlag )  );
+  ::memset( m_maxVpsDecPicBufferingMinus1,  0, sizeof(m_maxVpsDecPicBufferingMinus1 ) );
+  ::memset( m_maxVpsNumReorderPics,         0, sizeof(m_maxVpsNumReorderPics )        );
+  ::memset( m_maxVpsLatencyIncreasePlus1,   0, sizeof(m_maxVpsLatencyIncreasePlus1 )  );
+  ::memset( m_numSubDpbs                ,   0, sizeof(m_numSubDpbs)                   );
 #endif
 }
@@ -2117,23 +2237,80 @@
 }
 #endif
-
-#if IL_SL_SIGNALLING_N0371
-Bool TComVPS::checkLayerDependency(UInt i, UInt j)
-{
-  if( this->getDirectDependencyFlag(i, j) == true )
-  {                                                                                                                                    
-    return true;                                                                                                                       
-  }                                                                                                                                    
-  else                                                                                                                                   
-  {                                                                                                                                    
-    for(UInt k=i-1; k>j; k--)
-    {                                                                                                                                
-      if( this->getDirectDependencyFlag(i, k) == true )
-      {                                                                                                                            
-        checkLayerDependency( k,j );                                                                                           
-      }
-    }
-  }
-  return false;        
+#if VPS_DPB_SIZE_TABLE
+Void TComVPS::deriveNumberOfSubDpbs()
+{
+  // Derive number of sub-DPBs
+  // For output layer set 0
+  setNumSubDpbs(0, 1);
+  // For other output layer sets
+  for( Int i = 1; i < getNumOutputLayerSets(); i++)
+  {
+    setNumSubDpbs( i, getNumLayersInIdList( getOutputLayerSetIdx(i)) );
+  }
+}
+#endif
+#if VPS_VUI_TILES_NOT_IN_USE__FLAG
+Void TComVPS::setTilesNotInUseFlag(Bool x)
+{
+  m_tilesNotInUseFlag = x;
+  if (m_tilesNotInUseFlag)
+  {
+    for (int i = 0; i < getMaxLayers(); i++)
+    {
+      m_tilesInUseFlag[i] = m_loopFilterNotAcrossTilesFlag[i] = m_tilesNotInUseFlag;
+    }
+  }
+#if TILE_BOUNDARY_ALIGNED_FLAG  
+  if (m_tilesNotInUseFlag)
+  {
+    for (int i = 1; i < getMaxLayers(); i++)
+    {
+      for(int j = 0; j < getNumDirectRefLayers(getLayerIdInNuh(i)); j++)
+      {
+        setTileBoundariesAlignedFlag(i, j, m_tilesNotInUseFlag);
+      }
+    }
+  }
+#endif
+}
+#endif
+#if VPS_VUI_WPP_NOT_IN_USE__FLAG
+Void TComVPS::setWppNotInUseFlag(Bool x)
+{
+  m_wppNotInUseFlag = x;
+  if (m_wppNotInUseFlag)
+  {
+    for (int i = 0; i < getMaxLayers(); i++)
+    {
+      m_wppInUseFlag[i] = m_wppNotInUseFlag;
+    }
+  }
+}
+#endif
+#if O0092_0094_DEPENDENCY_CONSTRAINT
+Void TComVPS::setRefLayersFlags(Int currLayerId)
+{
+  for (Int i = 0; i < getNumDirectRefLayers(currLayerId); i++)
+  {
+    UInt refLayerId = getRefLayerId(currLayerId, i);
+    setRecursiveRefLayerFlag(currLayerId, refLayerId, true);
+    for (Int k = 0; k < MAX_NUM_LAYER_IDS; k++)
+    {
+      setRecursiveRefLayerFlag(currLayerId, k, (getRecursiveRefLayerFlag(currLayerId, k) | getRecursiveRefLayerFlag(refLayerId, k)));
+    }
+  }
+}
+
+Void TComVPS::setNumRefLayers(Int currLayerId)
+{
+  for (Int i = 0; i <= getMaxLayers(); i++)
+  {
+    UInt iNuhLId = getLayerIdInNuh(i);
+    setRefLayersFlags(iNuhLId);
+    for (UInt j = 0; j < MAX_NUM_LAYER_IDS; j++)
+    {
+      m_numberRefLayers[iNuhLId] += (getRecursiveRefLayerFlag(iNuhLId, j) == true ? 1 : 0);
+    }
+  }
 }
 #endif
@@ -2224,4 +2401,11 @@
 #if REPN_FORMAT_IN_VPS
 , m_updateRepFormatFlag       (false)
+#if O0096_REP_FORMAT_INDEX
+, m_updateRepFormatIndex      (0)
+#endif
+#endif
+#if SCALINGLIST_INFERRING
+, m_inferScalingListFlag ( false )
+, m_scalingListRefLayerId ( 0 )
 #endif
 #endif //SVC_EXTENSION
@@ -2248,4 +2432,7 @@
 TComSPS::~TComSPS()
 {
+#if SCALINGLIST_INFERRING
+  if( !m_inferScalingListFlag )
+#endif
   delete m_scalingList;
   m_RPSList.destroy();
@@ -2373,4 +2560,22 @@
 const Int TComSPS::m_winUnitX[]={1,2,2,1};
 const Int TComSPS::m_winUnitY[]={1,2,1,1};
+
+#if O0098_SCALED_REF_LAYER_ID
+Window& TComSPS::getScaledRefLayerWindowForLayer(Int layerId)
+{
+  static Window win;
+
+  for (Int i = 0; i < m_numScaledRefLayerOffsets; i++)
+  {
+    if (layerId == m_scaledRefLayerId[i])
+    {
+      return m_scaledRefLayerWindow[i];
+    }
+  }
+
+  win.resetWindow();  // scaled reference layer offsets are inferred to be zero when not present
+  return win;
+}
+#endif
 
 TComPPS::TComPPS()
@@ -2407,4 +2612,8 @@
 , m_listsModificationPresentFlag(  0)
 , m_numExtraSliceHeaderBits(0)
+#if SCALINGLIST_INFERRING
+, m_inferScalingListFlag ( false )
+, m_scalingListRefLayerId ( 0 )
+#endif
 {
   m_scalingList = new TComScalingList;
@@ -2423,4 +2632,8 @@
     m_puiRowHeight = NULL;
   }
+
+#if SCALINGLIST_INFERRING
+  if( !m_inferScalingListFlag )
+#endif
   delete m_scalingList;
 }
@@ -2640,9 +2853,5 @@
 /** set default quantization matrix to array
 */
-#if IL_SL_SIGNALLING_N0371
-Void TComSlice::setDefaultScalingList( UInt layerId )
-#else
 Void TComSlice::setDefaultScalingList()
-#endif
 {
   for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
@@ -2650,9 +2859,5 @@
     for(UInt listId=0;listId<g_scalingListNum[sizeId];listId++)
     {
-#if IL_SL_SIGNALLING_N0371
-      getScalingList()->processDefaultMarix(sizeId, listId, layerId);
-#else
-      getScalingList()->processDefaultMarix(sizeId, listId);
-#endif
+      getScalingList()->processDefaultMatrix(sizeId, listId);
     }
   }
@@ -2829,38 +3034,13 @@
  * \param Index of input matrix
  */
-#if IL_SL_SIGNALLING_N0371
-Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId, UInt layerId)
-#else
-Void TComScalingList::processDefaultMarix(UInt sizeId, UInt listId)
-#endif
-{
-#if IL_SL_SIGNALLING_N0371
-  Int i,coefNum = min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]);
-  UInt* scan  = (sizeId == 0) ? g_auiSigLastScan [ SCAN_DIAG ] [ 1 ] :  g_sigLastScanCG32x32; 
-  Int *src = getScalingListDefaultAddress(sizeId, listId);   
-#endif
-
+Void TComScalingList::processDefaultMatrix(UInt sizeId, UInt listId)
+{
   ::memcpy(getScalingListAddress(sizeId, listId),getScalingListDefaultAddress(sizeId,listId),sizeof(Int)*min(MAX_MATRIX_COEF_NUM,(Int)g_scalingListSize[sizeId]));
-
-#if IL_SL_SIGNALLING_N0371
-  for(i = 0; i < coefNum; i++) 
-  {
-    ref_scalingListCoef[layerId][sizeId][listId][i] = src[scan[i]];
-  }
-#endif
-
   setScalingListDC(sizeId,listId,SCALING_LIST_DC);
-#if IL_SL_SIGNALLING_N0371
-  ref_scalingListDC[layerId][sizeId][listId] = SCALING_LIST_DC;
-#endif
 }
 
 /** check DC value of matrix for default matrix signaling
  */
-#if IL_SL_SIGNALLING_N0371
-Void TComScalingList::checkDcOfMatrix( UInt layerId )
-#else
 Void TComScalingList::checkDcOfMatrix()
-#endif
 {
   for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
@@ -2871,9 +3051,5 @@
       if(getScalingListDC(sizeId,listId) == 0)
       {
-#if IL_SL_SIGNALLING_N0371
-        processDefaultMarix(sizeId, listId, layerId);
-#else
-        processDefaultMarix(sizeId, listId);
-#endif
+        processDefaultMatrix(sizeId, listId);
       }
     }
@@ -2956,9 +3132,4 @@
         m_activeVPSId = vpsId;
         m_activeSPSId = spsId;
-
-#if IL_SL_SIGNALLING_N0371
-        activeRefPPSId[ sps->getLayerId() ] = ppsId;
-        activeRefSPSId[ sps->getLayerId() ] = spsId;
-#endif
 
         return true;
@@ -3068,20 +3239,24 @@
 #endif
 
-Void TComSlice::setBaseColPic(  TComList<TComPic*>& rcListPic, UInt refLayerIdc )
+Bool TComSlice::setBaseColPic(  TComList<TComPic*>& rcListPic, UInt refLayerIdc )
 {  
   if(m_layerId == 0)
   {
     memset( m_pcBaseColPic, 0, sizeof( m_pcBaseColPic ) );
-    return;
+    return false;
   }        
 #if POC_RESET_FLAG
-  if( this->getPocResetFlag() )
-  {
-    setBaseColPic(refLayerIdc, xGetRefPic(rcListPic, 0)); 
+  TComPic* pic = xGetRefPic( rcListPic, m_bPocResetFlag ? 0 : m_iPOC );
+
+  if( pic )
+  {
+    setBaseColPic(refLayerIdc, pic );
   }
   else
   {
-    setBaseColPic(refLayerIdc, xGetRefPic(rcListPic, getPOC())); 
-  }
+    return false;
+  }
+  
+  return true;
 #else
   setBaseColPic(refLayerIdc, xGetRefPic(rcListPic, getPOC())); 
Index: trunk/source/Lib/TLibCommon/TComSlice.h
===================================================================
--- trunk/source/Lib/TLibCommon/TComSlice.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComSlice.h	(revision 540)
@@ -156,14 +156,4 @@
   Void     setScalingListPresentFlag    (Bool b)                               { m_scalingListPresentFlag = b;    }
   Bool     getScalingListPresentFlag    ()                                     { return m_scalingListPresentFlag; }
-
-#if IL_SL_SIGNALLING_N0371
-  UInt     m_layerId;
-
-  Void     setPredScalingListFlag    (Bool b)                               { m_predScalingListFlag = b;    }
-  Bool     getPredScalingListFlag    ()                                     { return m_predScalingListFlag; }
-  Void     setScalingListRefLayerId  (UInt b)                               { m_scalingListRefLayerId = b;  }
-  UInt     getScalingListRefLayerId  ()                                     { return m_scalingListRefLayerId; }
-#endif
-
   Int*     getScalingListAddress          (UInt sizeId, UInt listId)           { return m_scalingListCoef[sizeId][listId]; } //!< get matrix coefficient
   Bool     checkPredMode                  (UInt sizeId, UInt listId);
@@ -171,23 +161,9 @@
   UInt     getRefMatrixId                 (UInt sizeId, UInt listId)           { return m_refMatrixId[sizeId][listId]; }     //!< get reference matrix ID
   Int*     getScalingListDefaultAddress   (UInt sizeId, UInt listId);                                                        //!< get default matrix coefficient
-
-#if IL_SL_SIGNALLING_N0371
-  Void     processDefaultMarix            (UInt sizeId, UInt listId, UInt layerId );
-#else
-  Void     processDefaultMarix            (UInt sizeId, UInt listId);
-#endif
-
+  Void     processDefaultMatrix            (UInt sizeId, UInt listId);
   Void     setScalingListDC               (UInt sizeId, UInt listId, UInt u)   { m_scalingListDC[sizeId][listId] = u; }      //!< set DC value
 
   Int      getScalingListDC               (UInt sizeId, UInt listId)           { return m_scalingListDC[sizeId][listId]; }   //!< get DC value
-
-#if IL_SL_SIGNALLING_N0371
-  Void     setLayerId(UInt layerId) { m_layerId = layerId; }
-  UInt     getLayerId() { return m_layerId; }
-  Void     checkDcOfMatrix                ( UInt m_layerId );
-#else
   Void     checkDcOfMatrix                ();
-#endif
-
   Void     processRefMatrix               (UInt sizeId, UInt listId , UInt refListId );
   Bool     xParseScalingList              (Char* pchFile);
@@ -200,10 +176,4 @@
   UInt     m_refMatrixId                 [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM]; //!< RefMatrixID
   Bool     m_scalingListPresentFlag;                                                //!< flag for using default matrix
-
-#if IL_SL_SIGNALLING_N0371
-  Bool     m_predScalingListFlag;                                                   //!< flag for inter-layer scaling-list prediction
-  UInt     m_scalingListRefLayerId;                                                 //!< scaling_list_ref_layer_id  
-#endif
-
   UInt     m_predMatrixId                [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM]; //!< reference list index
   Int      *m_scalingListCoef            [SCALING_LIST_SIZE_NUM][SCALING_LIST_NUM]; //!< quantization matrix                                            
@@ -436,4 +406,7 @@
 class RepFormat
 {
+#if REPN_FORMAT_CONTROL_FLAG
+  Bool m_chromaAndBitDepthVpsPresentFlag;
+#endif 
 #if AUXILIARY_PICTURES
   ChromaFormat m_chromaFormatVpsIdc;
@@ -449,4 +422,9 @@
 public:
   RepFormat();
+
+#if REPN_FORMAT_CONTROL_FLAG
+  Bool getChromaAndBitDepthVpsPresentFlag() { return m_chromaAndBitDepthVpsPresentFlag; }
+  void setChromaAndBitDepthVpsPresentFlag(Bool x) { m_chromaAndBitDepthVpsPresentFlag = x; }
+#endif 
 
 #if AUXILIARY_PICTURES
@@ -504,7 +482,4 @@
   Int         m_layerSetLayerIdList[MAX_VPS_LAYER_SETS_PLUS1][MAX_VPS_LAYER_ID_PLUS1];
   Int         m_numLayerInIdList[MAX_VPS_LAYER_SETS_PLUS1];
-#endif
-#if IL_SL_SIGNALLING_N0371
-  Bool        m_scalingListLayerDependency[MAX_LAYERS][MAX_LAYERS];  // layer dependency for scaling list
 #endif
 #if VPS_EXTN_OFFSET
@@ -555,4 +530,8 @@
 #if M0457_PREDICTION_INDICATIONS
   UInt       m_directDepTypeLen;
+#if O0096_DEFAULT_DEPENDENCY_TYPE
+  Bool       m_defaultDirectDependencyTypeFlag;
+  UInt       m_defaultDirectDependencyType;
+#endif
   UInt       m_directDependencyType[MAX_VPS_LAYER_ID_PLUS1][MAX_VPS_LAYER_ID_PLUS1];
 #endif
@@ -569,4 +548,7 @@
   Bool       m_pocLsbNotPresentFlag[MAX_VPS_LAYER_ID_PLUS1];
 #endif
+#if O0223_PICTURE_TYPES_ALIGN_FLAG
+  Bool       m_crossLayerPictureTypeAlignFlag;
+#endif
 #if N0147_IRAP_ALIGN_FLAG
   Bool       m_crossLayerIrapAlignFlag;
@@ -582,9 +564,25 @@
   Bool       m_maxTidRefPresentFlag;
 #endif 
+#if VPS_TSLAYERS
+  Bool       m_maxTSLayersPresentFlag;
+  UInt       m_maxTSLayerMinus1[MAX_VPS_LAYER_ID_PLUS1 - 1];
+#endif
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
   Bool       m_singleLayerForNonIrapFlag;
 #endif
+#if HIGHER_LAYER_IRAP_SKIP_FLAG
+  Bool       m_higherLayerIrapSkipFlag;
+#endif
+#if VPS_VUI_TILES_NOT_IN_USE__FLAG
+  Bool       m_tilesNotInUseFlag;
+  Bool       m_tilesInUseFlag[MAX_VPS_LAYER_ID_PLUS1];
+  Bool       m_loopFilterNotAcrossTilesFlag[MAX_VPS_LAYER_ID_PLUS1];
+#endif 
 #if TILE_BOUNDARY_ALIGNED_FLAG
   Bool       m_tileBoundariesAlignedFlag[MAX_VPS_LAYER_ID_PLUS1][MAX_VPS_LAYER_ID_PLUS1];
+#endif 
+#if VPS_VUI_WPP_NOT_IN_USE__FLAG
+  Bool       m_wppNotInUseFlag;
+  Bool       m_wppInUseFlag[MAX_VPS_LAYER_ID_PLUS1];
 #endif 
 #if N0160_VUI_EXT_ILP_REF    
@@ -593,4 +591,14 @@
   Bool        m_ctuBasedOffsetEnabledFlag   [MAX_VPS_LAYER_ID_PLUS1][MAX_VPS_LAYER_ID_PLUS1];
   Int         m_minHorizontalCtuOffsetPlus1 [MAX_VPS_LAYER_ID_PLUS1][MAX_VPS_LAYER_ID_PLUS1];
+#endif 
+#if VPS_VUI_VIDEO_SIGNAL
+    Bool        m_vidSigPresentVpsFlag;
+    Int         m_vpsVidSigInfo;
+    Int         m_vpsVidSigIdx[MAX_VPS_LAYER_ID_PLUS1];
+    Int         m_vpsVidFormat[16];
+    Bool        m_vpsFullRangeFlag[16];
+    Int         m_vpsColorPrimaries[16];
+    Int         m_vpsTransChar[16];
+    Int         m_vpsMatCoeff[16];
 #endif 
 #if VPS_VUI_BITRATE_PICRATE
@@ -604,4 +612,7 @@
   Int         m_avgPicRate          [MAX_VPS_LAYER_SETS_PLUS1][MAX_TLAYER];
 #endif
+#if O0153_ALT_OUTPUT_LAYER_FLAG
+  Bool       m_altOutputLayerFlag;
+#endif
 #if REPN_FORMAT_IN_VPS
   Bool       m_repFormatIdxPresentFlag;
@@ -617,4 +628,17 @@
 #if O0215_PHASE_ALIGNMENT
   Bool       m_phaseAlignFlag;
+#endif
+
+#if O0092_0094_DEPENDENCY_CONSTRAINT
+  Int        m_numberRefLayers[MAX_NUM_LAYER_IDS];  // number of direct and indirect reference layers of a coding layer
+  Bool       m_recursiveRefLayerFlag[MAX_NUM_LAYER_IDS][MAX_NUM_LAYER_IDS];  // flag to indicate if j-th layer is a direct or indirect reference layer of i-th layer
+#endif
+#if VPS_DPB_SIZE_TABLE
+  Bool    m_subLayerFlagInfoPresentFlag [MAX_VPS_OP_LAYER_SETS_PLUS1];
+  Bool    m_subLayerDpbInfoPresentFlag  [MAX_VPS_OP_LAYER_SETS_PLUS1][MAX_LAYERS];
+  Int     m_maxVpsDecPicBufferingMinus1 [MAX_VPS_OP_LAYER_SETS_PLUS1][MAX_LAYERS][MAX_TLAYER];
+  Int     m_maxVpsNumReorderPics        [MAX_VPS_OP_LAYER_SETS_PLUS1][MAX_LAYERS];
+  Int     m_maxVpsLatencyIncreasePlus1  [MAX_VPS_OP_LAYER_SETS_PLUS1][MAX_LAYERS];
+  Int     m_numSubDpbs                  [MAX_VPS_OP_LAYER_SETS_PLUS1];
 #endif
 #endif //SVC_EXTENSION
@@ -683,11 +707,15 @@
   Void    deriveLayerIdListVariables();
 #endif
-
-#if IL_SL_SIGNALLING_N0371
-  Bool    checkLayerDependency(UInt i, UInt j);
-  Bool    getScalingListLayerDependency  ( UInt layerId, UInt refLayerId )            { return m_scalingListLayerDependency[layerId][refLayerId]; }
-  Void    setScalingListLayerDependency  ( UInt layerId, UInt refLayerId, Bool val  ) { m_scalingListLayerDependency[layerId][refLayerId] = val;  }
-#endif
-
+#if VPS_DPB_SIZE_TABLE
+Void      deriveNumberOfSubDpbs();
+#endif
+
+#if O0092_0094_DEPENDENCY_CONSTRAINT
+  Void    setRefLayersFlags(Int currLayerId);
+  Bool    getRecursiveRefLayerFlag(Int currLayerId, Int refLayerId)              { return m_recursiveRefLayerFlag[currLayerId][refLayerId];}
+  Void    setRecursiveRefLayerFlag(Int currLayerId, Int refLayerId, Bool x)      { m_recursiveRefLayerFlag[currLayerId][refLayerId] = x;   }
+  Int     getNumRefLayers(Int currLayerId)                                       { return m_numberRefLayers[currLayerId];                  }
+  Void    setNumRefLayers(Int currLayerId);
+#endif
 #if VPS_RENAME
   UInt    getMaxLayerId()                                       { return m_maxLayerId; }
@@ -764,5 +792,10 @@
   UInt   getDirectDepTypeLen()                                                  { return m_directDepTypeLen;                              }
   Void   setDirectDepTypeLen(UInt x)                                            { m_directDepTypeLen = x;                                 }
-
+#if O0096_DEFAULT_DEPENDENCY_TYPE
+  Bool   getDefaultDirectDependencyTypeFlag()                                   { return m_defaultDirectDependencyTypeFlag;               }
+  Void   setDefaultDirectDependecyTypeFlag(Bool x)                              { m_defaultDirectDependencyTypeFlag = x;                  }
+  UInt   getDefaultDirectDependencyType()                                       { return m_defaultDirectDependencyType;                   }
+  Void   setDefaultDirectDependecyType(UInt x)                                  { m_defaultDirectDependencyType = x;                      }
+#endif
   UInt   getDirectDependencyType(Int currLayerId, Int refLayerId)               { return m_directDependencyType[currLayerId][refLayerId]; }
   Void   setDirectDependencyType(Int currLayerId, Int refLayerId, UInt x)       { m_directDependencyType[currLayerId][refLayerId] = x;    }
@@ -791,4 +824,8 @@
   Void   setPocLsbNotPresentFlag(Int i, Bool x)                                 { m_pocLsbNotPresentFlag[i] = x;    }
 #endif
+#if O0223_PICTURE_TYPES_ALIGN_FLAG
+  Bool   getCrossLayerPictureTypeAlignFlag()                                    { return m_crossLayerPictureTypeAlignFlag;                      }
+  Void   setCrossLayerPictureTypeAlignFlag(Bool x)                              { m_crossLayerPictureTypeAlignFlag = x;                         }
+#endif 
 #if N0147_IRAP_ALIGN_FLAG
   Bool   getCrossLayerIrapAlignFlag()                                           { return m_crossLayerIrapAlignFlag;                      }
@@ -808,12 +845,36 @@
   Void   setMaxTidRefPresentFlag(Bool x)                            { m_maxTidRefPresentFlag = x;}
 #endif 
+#if VPS_TSLAYERS
+    Bool   getMaxTSLayersPresentFlag()                                  { return m_maxTSLayersPresentFlag ;}
+    Void   setMaxTSLayersPresentFlag(Bool x)                            { m_maxTSLayersPresentFlag = x;}
+    UInt   getMaxTSLayersMinus1(Int layerId)                            { return m_maxTSLayerMinus1[layerId];}
+    Void   setMaxTSLayersMinus1(Int layerId, UInt maxTSublayer)         { m_maxTSLayerMinus1[layerId] = maxTSublayer;}
+#endif
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
   Bool   getSingleLayerForNonIrapFlag()                             { return m_singleLayerForNonIrapFlag; }
   Void   setSingleLayerForNonIrapFlag(Bool x)                       { m_singleLayerForNonIrapFlag = x;    }
 #endif
+#if HIGHER_LAYER_IRAP_SKIP_FLAG
+  Bool   getHigherLayerIrapSkipFlag()                             { return m_higherLayerIrapSkipFlag; }
+  Void   setHigherLayerIrapSkipFlag(Bool x)                       { m_higherLayerIrapSkipFlag = x;    }
+#endif
+#if VPS_VUI_TILES_NOT_IN_USE__FLAG  
+  Bool   getTilesNotInUseFlag()         { return m_tilesNotInUseFlag; }
+  Void   setTilesNotInUseFlag(Bool x); 
+  Bool   getTilesInUseFlag(Int currLayerId)    { return m_tilesInUseFlag[currLayerId]; }
+  Void   setTilesInUseFlag(Int currLayerId, Bool x)    { m_tilesInUseFlag[currLayerId] = x; } 
+  Bool   getLoopFilterNotAcrossTilesFlag(Int currLayerId)    { return m_loopFilterNotAcrossTilesFlag[currLayerId]; }
+  Void   setLoopFilterNotAcrossTilesFlag(Int currLayerId, Bool x)    { m_loopFilterNotAcrossTilesFlag[currLayerId] = x; } 
+#endif 
 #if TILE_BOUNDARY_ALIGNED_FLAG  
   Bool   getTileBoundariesAlignedFlag(Int currLayerId, Int refLayerId)           { return m_tileBoundariesAlignedFlag[currLayerId][refLayerId]; }
   Void   setTileBoundariesAlignedFlag(Int currLayerId, Int refLayerId, Bool x)   { m_tileBoundariesAlignedFlag[currLayerId][refLayerId] = x; } 
 #endif 
+#if VPS_VUI_WPP_NOT_IN_USE__FLAG  
+  Bool   getWppNotInUseFlag()         { return m_wppNotInUseFlag; }
+  Void   setWppNotInUseFlag(Bool x); 
+  Bool   getWppInUseFlag(Int currLayerId)    { return m_wppInUseFlag[currLayerId]; }
+  Void   setWppInUseFlag(Int currLayerId, Bool x)    { m_wppInUseFlag[currLayerId] = x; } 
+#endif 
 #if N0160_VUI_EXT_ILP_REF  
   Bool  getNumIlpRestrictedRefLayers   ( )                                         { return m_numIlpRestrictedRefLayers        ;}
@@ -828,4 +889,22 @@
   Int  getMinHorizontalCtuOffsetPlus1 ( Int currLayerId, Int refLayerId )            { return m_minHorizontalCtuOffsetPlus1[currLayerId][refLayerId];}
   Void setMinHorizontalCtuOffsetPlus1 ( Int currLayerId, Int refLayerId, Int val )   { m_minHorizontalCtuOffsetPlus1[currLayerId][refLayerId] = val;}  
+#endif
+#if VPS_VUI_VIDEO_SIGNAL
+    Bool   getVideoSigPresentVpsFlag()           { return m_vidSigPresentVpsFlag; }
+    Void   setVideoSigPresentVpsFlag(Bool x)     { m_vidSigPresentVpsFlag = x;    }
+    Int    getNumVideoSignalInfo()               { return m_vpsVidSigInfo;        }
+    Void   setNumVideoSignalInfo(Int x)          { m_vpsVidSigInfo = x;           }
+    Int    getVideoSignalInfoIdx(Int idx)        { return m_vpsVidSigIdx[idx];    }
+    Void   setVideoSignalInfoIdx(Int idx, Int x) { m_vpsVidSigIdx[idx] = x;       }
+    Int    getVideoVPSFormat(Int idx)            { return m_vpsVidFormat[idx];    }
+    Void   setVideoVPSFormat(Int idx, Int x)     { m_vpsVidFormat[idx] = x;       }
+    Bool   getVideoFullRangeVpsFlag(Int idx)     { return m_vpsFullRangeFlag[idx];}
+    Void   setVideoFullRangeVpsFlag(Int idx, Bool x) { m_vpsFullRangeFlag[idx] = x;   }
+    Int    getColorPrimaries(Int idx)            { return m_vpsColorPrimaries[idx];   }
+    Void   setColorPrimaries(Int idx, Int x)     { m_vpsColorPrimaries[idx] = x;      }
+    Int    getTransCharacter(Int idx)            { return m_vpsTransChar[idx];    }
+    Void   setTransCharacter(Int idx, Int x)     { m_vpsTransChar[idx] = x;       }
+    Int    getMaxtrixCoeff(Int idx)              { return m_vpsMatCoeff[idx];     }
+    Void   setMaxtrixCoeff(Int idx, Int x)       { m_vpsMatCoeff[idx] = x;        }
 #endif
 #if VPS_VUI_BITRATE_PICRATE
@@ -850,5 +929,8 @@
   Void setAvgPicRate(Int i, Int j, Int x)   { m_avgPicRate[i][j] = x;    }
 #endif
-
+#if O0153_ALT_OUTPUT_LAYER_FLAG
+  Bool   getAltOuputLayerFlag()             { return m_altOutputLayerFlag; }
+  Void   setAltOuputLayerFlag(Bool x)       { m_altOutputLayerFlag = x;    }
+#endif
 #if REPN_FORMAT_IN_VPS
   Bool   getRepFormatIdxPresentFlag()       { return m_repFormatIdxPresentFlag; }
@@ -883,4 +965,24 @@
   Bool   getPhaseAlignFlag()                             { return m_phaseAlignFlag; }
   Void   setPhaseAlignFlag(Bool x)                       { m_phaseAlignFlag = x;    }
+#endif
+#if VPS_DPB_SIZE_TABLE
+  Bool    getSubLayerFlagInfoPresentFlag(Int i)         {return m_subLayerFlagInfoPresentFlag[i]; }
+  Void    setSubLayerFlagInfoPresentFlag(Int i, Bool x) {m_subLayerFlagInfoPresentFlag[i] = x;    }
+
+  Bool    getSubLayerDpbInfoPresentFlag(Int i, Int j)         {return m_subLayerDpbInfoPresentFlag[i][j]; }
+  Void    setSubLayerDpbInfoPresentFlag(Int i, Int j, Bool x) {m_subLayerDpbInfoPresentFlag[i][j] = x;    }
+
+  // For the 0-th output layer set, use the date from the active SPS for base layer.
+  Int     getMaxVpsDecPicBufferingMinus1(Int i, Int k, Int j)         { assert(i != 0); return m_maxVpsDecPicBufferingMinus1[i][k][j]; }
+  Void    setMaxVpsDecPicBufferingMinus1(Int i, Int k, Int j, Int x) { m_maxVpsDecPicBufferingMinus1[i][k][j] = x;    }
+
+  Int     getMaxVpsNumReorderPics(Int i, Int j)         { assert(i != 0); return m_maxVpsNumReorderPics[i][j]; }
+  Void    setMaxVpsNumReorderPics(Int i, Int j, Int x) { m_maxVpsNumReorderPics[i][j] = x;    }
+
+  Int     getMaxVpsLatencyIncreasePlus1(Int i, Int j)         { assert(i != 0); return m_maxVpsLatencyIncreasePlus1[i][j]; }
+  Void    setMaxVpsLatencyIncreasePlus1(Int i, Int j, Int x) { m_maxVpsLatencyIncreasePlus1[i][j] = x;    }
+
+  Int     getNumSubDpbs(Int i)                          { return m_numSubDpbs[i]; }
+  Void    setNumSubDpbs(Int i, Int x)                   { m_numSubDpbs[i] = x;    }
 #endif
 #endif //SVC_EXTENSION
@@ -1185,11 +1287,4 @@
   UInt m_layerId;
 
-#if IL_SL_SIGNALLING_N0371
-  TComVPS*    m_pVPS;
-  static TComSPS* m_pcSPS[MAX_LAYERS];
-  Bool        m_predScalingListFlag;
-  UInt        m_scalingListRefLayerId;
-#endif
-
 #if REF_IDX_MFM
 #if !M0457_COL_PICTURE_SIGNALING
@@ -1198,7 +1293,17 @@
 #endif
   UInt        m_numScaledRefLayerOffsets;
+#if O0098_SCALED_REF_LAYER_ID
+  UInt        m_scaledRefLayerId[MAX_LAYERS];
+#endif
   Window      m_scaledRefLayerWindow[MAX_LAYERS];
 #if REPN_FORMAT_IN_VPS
   Bool m_updateRepFormatFlag;
+#if O0096_REP_FORMAT_INDEX
+  UInt m_updateRepFormatIndex;
+#endif
+#endif
+#if SCALINGLIST_INFERRING
+  Bool       m_inferScalingListFlag;
+  UInt       m_scalingListRefLayerId;
 #endif
 #endif //SVC_EXTENSION
@@ -1322,17 +1427,9 @@
   Void setScalingListPresentFlag( Bool b ) { m_scalingListPresentFlag  = b;       }
 
-#if IL_SL_SIGNALLING_N0371
-  Bool getPredScalingListFlag()         { return m_predScalingListFlag;     }
-  Void setPredScalingListFlag( Bool b ) { m_predScalingListFlag  = b;       }
-  UInt getScalingListRefLayerId()         { return m_scalingListRefLayerId;   }
-  Void setScalingListRefLayerId( UInt b ) { m_scalingListRefLayerId  = b;       }
-
-  TComVPS*  getVPS()                      { return  m_pVPS; }
-  Void      setVPS( TComVPS* vps )        { m_pVPS = vps;   }
-  static   TComSPS* getSPS(UInt layerId)               { return m_pcSPS[layerId]; }
-  static   Void     setSPS(UInt layerId, TComSPS* sps) { m_pcSPS[layerId] = sps;  }
-#endif
-
+#if SCALINGLIST_INFERRING
+  Void setScalingList( TComScalingList *scalingList ) { m_scalingList = scalingList; }
+#else
   Void setScalingList      ( TComScalingList *scalingList);
+#endif
   TComScalingList* getScalingList ()       { return m_scalingList; }               //!< get ScalingList class pointer in SPS
 
@@ -1380,8 +1477,23 @@
   UInt     getNumScaledRefLayerOffsets()  { return m_numScaledRefLayerOffsets; }
   Void     setNumScaledRefLayerOffsets(Int x)  { m_numScaledRefLayerOffsets = x; }
+#if O0098_SCALED_REF_LAYER_ID
+  UInt     getScaledRefLayerId(Int x)          { return m_scaledRefLayerId[x]; }
+  Void     setScaledRefLayerId(Int x, UInt id) { m_scaledRefLayerId[x] = id; }
+  Window&  getScaledRefLayerWindowForLayer( Int layerId );
+#endif
   Window&  getScaledRefLayerWindow( Int x )   { return m_scaledRefLayerWindow[x]; }
 #if REPN_FORMAT_IN_VPS
   Bool     getUpdateRepFormatFlag()       { return m_updateRepFormatFlag; }
   Void     setUpdateRepFormatFlag(Bool x) { m_updateRepFormatFlag = x;    }
+#if O0096_REP_FORMAT_INDEX
+  Int      getUpdateRepFormatIndex()      { return m_updateRepFormatIndex; }
+  Void     setUpdateRepFormatIndex(UInt index)  { m_updateRepFormatIndex = index; }
+#endif
+#endif
+#if SCALINGLIST_INFERRING
+  Bool     getInferScalingListFlag()  { return m_inferScalingListFlag;  }
+  UInt     getScalingListRefLayerId() { return m_scalingListRefLayerId; }
+  Void     setInferScalingListFlag( Bool flag )     { m_inferScalingListFlag = flag;     }
+  Void     setScalingListRefLayerId( UInt layerId ) { m_scalingListRefLayerId = layerId; }
 #endif
 #endif //SVC_EXTENSION
@@ -1468,21 +1580,16 @@
   Int      m_deblockingFilterTcOffsetDiv2;      //< tc offset for deblocking filter
   Bool     m_scalingListPresentFlag;
-
-#if SVC_EXTENSION
-  UInt m_layerId;
-
-#if IL_SL_SIGNALLING_N0371
-  static TComPPS* m_pcPPS[MAX_LAYERS];
-  Bool     m_predScalingListFlag;
-  UInt     m_scalingListRefLayerId;
-#endif
-
-#endif
-
   TComScalingList*     m_scalingList;   //!< ScalingList class pointer
-
   Bool m_listsModificationPresentFlag;
   UInt m_log2ParallelMergeLevelMinus2;
   Int m_numExtraSliceHeaderBits;
+
+#if SVC_EXTENSION
+#if SCALINGLIST_INFERRING
+  UInt     m_layerId;
+  Bool     m_inferScalingListFlag;
+  UInt     m_scalingListRefLayerId;
+#endif
+#endif
 
 public:
@@ -1596,18 +1703,18 @@
   Void     setScalingListPresentFlag( Bool b ) { m_scalingListPresentFlag  = b;       }
 
-#if IL_SL_SIGNALLING_N0371
-  Void     setLayerId(UInt layerId) { m_layerId = layerId; }
+#if SCALINGLIST_INFERRING
   UInt     getLayerId() { return m_layerId; }
-
-  Bool     getPredScalingListFlag()         { return m_predScalingListFlag;     }
-  Void     setPredScalingListFlag( Bool b ) { m_predScalingListFlag  = b;       }
-  UInt     getScalingListRefLayerId()         { return m_scalingListRefLayerId;     }
-  Void     setScalingListRefLayerId( UInt b ) { m_scalingListRefLayerId  = b;       }
-
-  static   TComPPS* getPPS(UInt layerId)               { return m_pcPPS[layerId]; }
-  static   Void     setPPS(UInt layerId, TComPPS* pps) { m_pcPPS[layerId] = pps;  }
-#endif
-
+  Void     setLayerId( UInt layerId ) { m_layerId = layerId;            }
+  Bool     getInferScalingListFlag()  { return m_inferScalingListFlag;  }
+  UInt     getScalingListRefLayerId() { return m_scalingListRefLayerId; }
+  Void     setInferScalingListFlag( Bool flag )     { m_inferScalingListFlag = flag;     }
+  Void     setScalingListRefLayerId( UInt layerId ) { m_scalingListRefLayerId = layerId; }
+#endif
+
+#if SCALINGLIST_INFERRING
+  Void     setScalingList( TComScalingList *scalingList ) { m_scalingList = scalingList; }
+#else
   Void     setScalingList      ( TComScalingList *scalingList);
+#endif
   TComScalingList* getScalingList ()          { return m_scalingList; }         //!< get ScalingList class pointer in PPS
   Bool getListsModificationPresentFlag ()          { return m_listsModificationPresentFlag; }
@@ -1705,10 +1812,5 @@
   UInt        m_maxNumMergeCand;
 
-#if SAO_CHROMA_LAMBDA
-  Double      m_dLambdaLuma;
-  Double      m_dLambdaChroma;
-#else
-  Double      m_dLambda;
-#endif
+  Double      m_lambdas[3];
 
   Bool        m_abEqualRef  [2][MAX_NUM_REF][MAX_NUM_REF];
@@ -1780,4 +1882,7 @@
 #endif  
   Bool        m_bDiscardableFlag;
+#if O0149_CROSS_LAYER_BLA_FLAG
+  Bool        m_bCrossLayerBLAFlag;
+#endif
 #endif //SVC_EXTENSION
 
@@ -1819,4 +1924,7 @@
   Int       getRPSidx          () { return m_iBDidx; }
   Int       getPrevTid0POC      ()                        { return  m_prevTid0POC;       }
+#if PREVTID0_POC_RESET
+    Void     adjustPrevTid0POC      (Int adj)             { m_prevTid0POC=m_prevTid0POC-adj; }
+#endif
   TComRefPicListModification* getRefPicListModification() { return &m_RefPicListModification; }
   Void      setLastIDR(Int iIDRPOC)                       { m_iLastIDR = iIDRPOC; }
@@ -1866,5 +1974,9 @@
   Bool      isIRAP              () const                        { return (getNalUnitType() >= 16) && (getNalUnitType() <= 23); }  
   Void      checkCRA(TComReferencePictureSet *pReferencePictureSet, Int& pocCRA, NalUnitType& associatedIRAPType, TComList<TComPic *>& rcListPic);
+#if NO_CLRAS_OUTPUT_FLAG
+  Void      decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic, Bool noClrasOutputFlag);
+#else
   Void      decodingRefreshMarking(Int& pocCRA, Bool& bRefreshPending, TComList<TComPic*>& rcListPic);
+#endif
   Void      setSliceType        ( SliceType e )                 { m_eSliceType        = e;      }
   Void      setSliceQp          ( Int i )                       { m_iSliceQp          = i;      }
@@ -1886,12 +1998,8 @@
   Void      setDepth            ( Int iDepth )                  { m_iDepth            = iDepth; }
 
-#if FIX1071
 #if SVC_EXTENSION
   Void      setRefPicList       ( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr = false, TComPic** ilpPic = NULL );
 #else
   Void      setRefPicList       ( TComList<TComPic*>& rcListPic, Bool checkNumPocTotalCurr = false );
-#endif
-#else
-  Void      setRefPicList       ( TComList<TComPic*>& rcListPic );
 #endif
   Void      setRefPOCList       ();
@@ -1905,12 +2013,6 @@
   Bool      isInterP        ()                          { return  m_eSliceType == P_SLICE;  }
   
-#if SAO_CHROMA_LAMBDA  
-  Void      setLambda( Double d, Double e ) { m_dLambdaLuma = d; m_dLambdaChroma = e;}
-  Double    getLambdaLuma() { return m_dLambdaLuma;        }
-  Double    getLambdaChroma() { return m_dLambdaChroma;        }
-#else
-  Void      setLambda( Double d ) { m_dLambda = d; }
-  Double    getLambda() { return m_dLambda;        }
-#endif
+  Void      setLambdas ( const Double lambdas[3] ) { for (Int component = 0; component < 3; component++) m_lambdas[component] = lambdas[component]; }
+  const Double* getLambdas() const { return m_lambdas; }
   
   Void      initEqualRef();
@@ -1939,9 +2041,5 @@
   Bool isStepwiseTemporalLayerSwitchingPointCandidate( TComList<TComPic*>& rcListPic );
   Int       checkThatAllRefPicsAreAvailable( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool printErrors, Int pocRandomAccess = 0);
-#if FIX1071
   Void      createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet, Bool isRAP);
-#else
-  Void      createExplicitReferencePictureSetFromReference( TComList<TComPic*>& rcListPic, TComReferencePictureSet *pReferencePictureSet);
-#endif
 
   Void setMaxNumMergeCand               (UInt val )         { m_maxNumMergeCand = val;                    }
@@ -2001,11 +2099,5 @@
   Void  setScalingList              ( TComScalingList* scalingList ) { m_scalingList = scalingList; }
   TComScalingList*   getScalingList ()                               { return m_scalingList; }
-
-#if IL_SL_SIGNALLING_N0371
-  Void  setDefaultScalingList       ( UInt m_layerId );
-#else
   Void  setDefaultScalingList       ();
-#endif
-
   Bool  checkDefaultScalingList     ();
   Void      setCabacInitFlag  ( Bool val ) { m_cabacInitFlag = val;      }  //!< set CABAC initial flag 
@@ -2022,5 +2114,5 @@
 
 #if SVC_EXTENSION
-  Void      setBaseColPic       ( TComList<TComPic*>& rcListPic , UInt refLayerIdc );
+  Bool      setBaseColPic       ( TComList<TComPic*>& rcListPic , UInt refLayerIdc );
   Void      setBaseColPic       (UInt refLayerIdc, TComPic* p)     { m_pcBaseColPic[refLayerIdc] = p; }
   TComPic*  getBaseColPic       (UInt refLayerIdc)                { return m_pcBaseColPic[refLayerIdc]; }
@@ -2099,4 +2191,8 @@
   Bool      getDiscardableFlag  ()                           { return m_bDiscardableFlag;    }
   Void      setDiscardableFlag  (Bool b)                     { m_bDiscardableFlag = b;       }
+#if O0149_CROSS_LAYER_BLA_FLAG
+  Bool      getCrossLayerBLAFlag  ()                         { return m_bCrossLayerBLAFlag;  }
+  Void      setCrossLayerBLAFlag  (Bool b)                   { m_bCrossLayerBLAFlag = b;     }
+#endif
 
 #if RPL_INIT_N0316_N0082
Index: trunk/source/Lib/TLibCommon/TComTrQuant.cpp
===================================================================
--- trunk/source/Lib/TLibCommon/TComTrQuant.cpp	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComTrQuant.cpp	(revision 540)
@@ -54,8 +54,8 @@
   {
     iNNZbeforePos0 = 0;
-  d64CodedLevelandDist = 0;
-  d64UncodedDist = 0;
-  d64SigCost = 0;
-  d64SigCost_0 = 0;
+    d64CodedLevelandDist = 0;
+    d64UncodedDist = 0;
+    d64SigCost = 0;
+    d64SigCost_0 = 0;
   }
 #endif
@@ -1944,5 +1944,5 @@
               Int64 costUp   = rdFactor * ( - deltaU[uiBlkPos] ) + rateIncUp[uiBlkPos] ;
               Int64 costDown = rdFactor * (   deltaU[uiBlkPos] ) + rateIncDown[uiBlkPos] 
-              -   ( abs(piDstCoeff[uiBlkPos])==1?((1<<15)+sigRateDelta[uiBlkPos]):0 );
+              -   ((abs(piDstCoeff[uiBlkPos]) == 1) ? sigRateDelta[uiBlkPos] : 0);
               
               if(lastCG==1 && lastNZPosInCG==n && abs(piDstCoeff[uiBlkPos])==1)
@@ -2163,5 +2163,5 @@
   {
     Double dErr         = Double( lLevelDouble  - ( uiAbsLevel << iQBits ) );
-    Double dCurrCost    = dErr * dErr * dTemp + xGetICRateCost( uiAbsLevel, ui16CtxNumOne, ui16CtxNumAbs, ui16AbsGoRice, c1Idx, c2Idx );
+    Double dCurrCost    = dErr * dErr * dTemp + xGetICost(xGetICRate( uiAbsLevel, ui16CtxNumOne, ui16CtxNumAbs, ui16AbsGoRice, c1Idx, c2Idx ));
     dCurrCost          += dCurrCostSig;
 
@@ -2184,5 +2184,5 @@
  * \returns cost of given absolute transform level
  */
-__inline Double TComTrQuant::xGetICRateCost  ( UInt                            uiAbsLevel,
+__inline Int TComTrQuant::xGetICRate  ( UInt                            uiAbsLevel,
                                                UShort                          ui16CtxNumOne,
                                                UShort                          ui16CtxNumAbs,
@@ -2192,5 +2192,5 @@
                                                ) const
 {
-  Double iRate = xGetIEPRate();
+  Int iRate = Int(xGetIEPRate());
   UInt baseLevel  =  (c1Idx < C1FLAG_NUMBER)? (2 + (c2Idx < C2FLAG_NUMBER)) : 1;
 
@@ -2236,66 +2236,5 @@
   else
   {
-    assert (0);
-  }
-  return xGetICost( iRate );
-}
-
-__inline Int TComTrQuant::xGetICRate  ( UInt                            uiAbsLevel,
-                                       UShort                          ui16CtxNumOne,
-                                       UShort                          ui16CtxNumAbs,
-                                       UShort                          ui16AbsGoRice
-                                     , UInt                            c1Idx,
-                                       UInt                            c2Idx
-                                       ) const
-{
-  Int iRate = 0;
-  UInt baseLevel  =  (c1Idx < C1FLAG_NUMBER)? (2 + (c2Idx < C2FLAG_NUMBER)) : 1;
-
-  if ( uiAbsLevel >= baseLevel )
-  {
-    UInt uiSymbol     = uiAbsLevel - baseLevel;
-    UInt uiMaxVlc     = g_auiGoRiceRange[ ui16AbsGoRice ];
-    Bool bExpGolomb   = ( uiSymbol > uiMaxVlc );
-
-    if( bExpGolomb )
-    {
-      uiAbsLevel  = uiSymbol - uiMaxVlc;
-      Int iEGS    = 1;  for( UInt uiMax = 2; uiAbsLevel >= uiMax; uiMax <<= 1, iEGS += 2 );
-      iRate      += iEGS << 15;
-      uiSymbol    = min<UInt>( uiSymbol, ( uiMaxVlc + 1 ) );
-    }
-
-    UShort ui16PrefLen = UShort( uiSymbol >> ui16AbsGoRice ) + 1;
-    UShort ui16NumBins = min<UInt>( ui16PrefLen, g_auiGoRicePrefixLen[ ui16AbsGoRice ] ) + ui16AbsGoRice;
-
-    iRate += ui16NumBins << 15;
-
-    if (c1Idx < C1FLAG_NUMBER)
-    {
-      iRate += m_pcEstBitsSbac->m_greaterOneBits[ ui16CtxNumOne ][ 1 ];
-
-      if (c2Idx < C2FLAG_NUMBER)
-      {
-        iRate += m_pcEstBitsSbac->m_levelAbsBits[ ui16CtxNumAbs ][ 1 ];
-      }
-    }
-  }
-  else
-  if( uiAbsLevel == 0 )
-  {
-    return 0;
-  }
-  else if( uiAbsLevel == 1 )
-  {
-    iRate += m_pcEstBitsSbac->m_greaterOneBits[ ui16CtxNumOne ][ 0 ];
-  }
-  else if( uiAbsLevel == 2 )
-  {
-    iRate += m_pcEstBitsSbac->m_greaterOneBits[ ui16CtxNumOne ][ 1 ];
-    iRate += m_pcEstBitsSbac->m_levelAbsBits[ ui16CtxNumAbs ][ 0 ];
-  }
-  else
-  {
-    assert(0);
+    iRate = 0;
   }
   return iRate;
@@ -2494,9 +2433,5 @@
 /** set flat matrix value to quantized coefficient
  */
-#if IL_SL_SIGNALLING_N0371
-Void TComTrQuant::setFlatScalingList( UInt m_layerId )
-#else
 Void TComTrQuant::setFlatScalingList()
-#endif
 {
   UInt size,list;
@@ -2507,12 +2442,4 @@
     for(list = 0; list <  g_scalingListNum[size]; list++)
     {
-#if IL_SL_SIGNALLING_N0371
-      ref_scalingListDC[m_layerId][size][list] = SCALING_LIST_DC;
-      for(UInt i=0; i<MAX_MATRIX_COEF_NUM; i++)
-      {
-        ref_scalingListCoef[m_layerId][size][list][i] = SCALING_LIST_DC;
-      }
-#endif
-
       for(qp=0;qp<SCALING_LIST_REM_NUM;qp++)
       {
Index: trunk/source/Lib/TLibCommon/TComTrQuant.h
===================================================================
--- trunk/source/Lib/TLibCommon/TComTrQuant.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TComTrQuant.h	(revision 540)
@@ -150,6 +150,6 @@
 
 #if RDOQ_CHROMA_LAMBDA 
-  Void setLambda(Double dLambdaLuma, Double dLambdaChroma) { m_dLambdaLuma = dLambdaLuma; m_dLambdaChroma = dLambdaChroma; }
-  Void selectLambda(TextType eTType) { m_dLambda = (eTType == TEXT_LUMA) ? m_dLambdaLuma : m_dLambdaChroma; }
+  Void setLambdas ( const Double lambdas[3] ) { for (Int component = 0; component < 3; component++) m_lambdas[component] = lambdas[component]; }
+  Void selectLambda(TextType eTType) { m_dLambda = (eTType == TEXT_LUMA) ? m_lambdas[0] : ((eTType == TEXT_CHROMA_U) ? m_lambdas[1] : m_lambdas[2]); }
 #else
   Void setLambda(Double dLambda) { m_dLambda = dLambda;}
@@ -181,11 +181,5 @@
   Void setUseScalingList   ( Bool bUseScalingList){ m_scalingListEnabledFlag = bUseScalingList; };
   Bool getUseScalingList   (){ return m_scalingListEnabledFlag; };
-
-#if IL_SL_SIGNALLING_N0371
-  Void setFlatScalingList  ( UInt m_layerId );
-#else
   Void setFlatScalingList  ();
-#endif
-
   Void xsetFlatScalingList ( UInt list, UInt size, UInt qp);
   Void xSetScalingListEnc  ( TComScalingList *scalingList, UInt list, UInt size, UInt qp);
@@ -213,6 +207,5 @@
   QpParam  m_cQP;
 #if RDOQ_CHROMA_LAMBDA
-  Double   m_dLambdaLuma;
-  Double   m_dLambdaChroma;
+  Double   m_lambdas[3];
 #endif
   Double   m_dLambda;
@@ -279,11 +272,4 @@
                                              Double                          dTemp,
                                              Bool                            bLast        ) const;
-  __inline Double xGetICRateCost   ( UInt                            uiAbsLevel,
-                                     UShort                          ui16CtxNumOne,
-                                     UShort                          ui16CtxNumAbs,
-                                     UShort                          ui16AbsGoRice 
-                                   , UInt                            c1Idx,
-                                     UInt                            c2Idx
-                                     ) const;
 __inline Int xGetICRate  ( UInt                            uiAbsLevel,
                            UShort                          ui16CtxNumOne,
Index: trunk/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- trunk/source/Lib/TLibCommon/TypeDef.h	(revision 499)
+++ trunk/source/Lib/TLibCommon/TypeDef.h	(revision 540)
@@ -51,5 +51,5 @@
 #define O0194_JOINT_US_BITSHIFT          1      ///< JCTVC-O0194: Joint Upsampling and bit-shift
 #endif
-#define O0194_WEIGHTED_PREDICTION_CGS    1      ///< JCTVC-O0194: Weighted prediciton for color gamut scalability
+#define O0194_WEIGHTED_PREDICTION_CGS    0      ///< JCTVC-O0194: Weighted prediciton for color gamut scalability
 #define MFM_ENCCONSTRAINT                1      ///< JCTVC-O0216: Encoder constraint for motion field mapping
 #define VPS_NUH_LAYER_ID                 1      ///< JCTVC-N0085: Assert that the nuh_layer_id of VPS NAL unit should be 0
@@ -57,13 +57,23 @@
 #define ALIGN_TSA_STSA_PICS              1      ///< JCTVC-N0084: Alignment of TSA and STSA pictures across AU.
 #define REPN_FORMAT_IN_VPS               1      ///< JCTVC-N0092: Signal represenation format (spatial resolution, bit depth, colour format) in the VPS
+#if REPN_FORMAT_IN_VPS
+#define REPN_FORMAT_CONTROL_FLAG         1      ///< JCTVC-O0179: Add control flag in representation format to control sending of chroma and bitdepth parameters
+#endif 
 #define TIMING_INFO_NONZERO_LAYERID_SPS  1      ///< JCTVC-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      ///< JCTVC-N0316, JCTVC-N0082: initial reference picture list construction 
 
-#define IL_SL_SIGNALLING_N0371           0      ///< JCTVC-N0371: inter-layer scaling list
+#define SCALINGLIST_INFERRING            1      ///< JCTVC-N0371: inter-layer scaling list
 #define M0463_VUI_EXT_ILP_REF            0      ///< JCTVC-M0463: VUI extension inter-layer dependency offset signalling
 #define SPS_EXTENSION                    1      ///< Define sps_extension() syntax structure
+#if SPS_EXTENSION
+#define O0142_CONDITIONAL_SPS_EXTENSION  1      ///< JCTVC-O0142: Conditional SPS extension
+#endif
 #define VERT_MV_CONSTRAINT               1      ///< Vertical MV component constraint flag
 #define SCALABILITY_MASK_E0104           1      ///< JCT3V-E0104: scalability mask for depth
 #define LAYER_CTB                        0      ///< enable layer-specific CTB structure
+#if POC_RESET_FLAG
+#define PREVTID0_POC_RESET               1      ///< JCTVC-O0117 Modification of the PicOrderCntVal of prevTid0Pic
+#define POC_RESET_RPS                    1      ///< JCTVC-O0117 Modification to the decoding process for rps
+#endif
 
 #define ILP_SSH_SIG                      1      ///< JCTVC-N0195 proposal 2, JCTVC-N0118: add presence flag in VPS ext to condition inter-layer prediction signaling in slice segment header
@@ -86,7 +96,13 @@
 
 #if VPS_VUI
+#define VPS_VUI_TILES_NOT_IN_USE__FLAG    1      ///< JCTVC-O0226: VPS VUI flag to indicate tile not in use
+#define VPS_VUI_WPP_NOT_IN_USE__FLAG    1      ///< JCTVC-O0226: VPS VUI flag to indicate tile not in use
 #define TILE_BOUNDARY_ALIGNED_FLAG       1      ///< JCTVC-N0160/JCTVC-N0199 proposal 2 variant 2: VPS VUI flag to indicate tile boundary alignment
 #define N0160_VUI_EXT_ILP_REF            1      ///< VUI extension inter-layer dependency offset signalling
 #define VPS_VUI_BITRATE_PICRATE          1      ///< JCTVC-N0085: Signal bit rate and picture in VPS VUI
+#if M0040_ADAPTIVE_RESOLUTION_CHANGE
+#define HIGHER_LAYER_IRAP_SKIP_FLAG      1      ///< JCTVC-O0199: Indication that higher layer IRAP picture uses skip blocks only
+#endif
+#define VPS_VUI_VIDEO_SIGNAL             1      ///< JCTVC-O0118 video signal information
 #endif //VPS_VUI
 
@@ -127,4 +143,8 @@
 #define N0120_MAX_TID_REF_CFG            1      ///< set max_tid_il_ref_pics_plus1 and max_tid_ref_present_flag in the config. file (configuration setting)
 #define O0225_MAX_TID_FOR_REF_LAYERS     1
+#define O0225_TID_BASED_IL_RPS_DERIV     1
+
+#define VPS_TSLAYERS                     1      ///< JCTVC-O0120 signal max temporal sub-layers for each layer
+#define TSLAYERS_IL_RPS                  1      ///< JCTVC-O0120 IL RPS based on max temporal sub-layers
 #endif
 #if REF_IDX_MFM
@@ -139,4 +159,6 @@
 #define M0457_IL_SAMPLE_PRED_ONLY_FLAG   0      ///< shall be 0, JCTVC-N0107
 #endif
+
+#define O0223_PICTURE_TYPES_ALIGN_FLAG   1  ///< a flag to indicatate whether picture types are aligned across layers.
 
 #define N0147_IRAP_ALIGN_FLAG            1      ///< a flag to indicatate whether IRAPs are aligned across layers
@@ -166,4 +188,27 @@
 #define SHM_FIX7                         1      ///< fix for SHVC WD ticket #7
 
+#define O0092_0094_DEPENDENCY_CONSTRAINT 1      ///< JCTVC-O0092: constraint on the layer_id of SPS/PPS
+#if O0092_0094_DEPENDENCY_CONSTRAINT
+#define MAX_REF_LAYERS                   7
+#endif
+#define O0096_REP_FORMAT_INDEX           1      ///< JCTVC-O0096: identify SPS rep_format() with an index into the lists of formats in VPS extension.
+#define O0096_DEFAULT_DEPENDENCY_TYPE    1      ///< JCTVC-O0096: specify default dependency type for all direct reference layers
+
+#define RESAMPLING_CONSTRAINT_BUG_FIX    1
+#define O0098_SCALED_REF_LAYER_ID        1      ///< JCTVC-O0098: signal scaled reference id
+
+#define O0153_ALT_OUTPUT_LAYER_FLAG      1      ///< JCTVC-O0153: alt output layer flag
+
+#define VPS_DPB_SIZE_TABLE               1      ///< JCTVC-O0217: DPB operations: signaling DPB-related parameters
+#if VPS_DPB_SIZE_TABLE 
+#define OUTPUT_LAYER_SET_INDEX           1      ///< JCTVC-O0217: DPB operations: Inference/input of output layer set index
+#if OUTPUT_LAYER_SET_INDEX
+#define USE_DPB_SIZE_TABLE               1      ///< JCTVC-O0217: DPB operations: Use signaled DPB-size table parameters in the decoder
+#endif
+#endif
+
+#define NO_CLRAS_OUTPUT_FLAG             1
+#define O0149_CROSS_LAYER_BLA_FLAG       1      ///< JCTVC-O0149: signal cross_layer_bla_flag in slice header
+
 #else
 #define SYNTAX_OUTPUT                    0
@@ -174,5 +219,8 @@
 //! \{
 
-#define FIX1071 1 ///< fix for issue #1071
+#define HM_CLEANUP_SAO                  1  ///< JCTVC-N0230, 1) three SAO encoder-only software bugfixes. 2) new SAO implementation without picture quadtree, fine-grained slice legacies, and other redundancies.
+#if HM_CLEANUP_SAO  
+#define SAO_ENCODE_ALLOW_USE_PREDEBLOCK 1
+#endif
 
 #define MAX_NUM_PICS_IN_SOP           1024
@@ -184,4 +232,5 @@
 #define MAX_VPS_NUM_SCALABILITY_TYPES             16
 #endif
+#define MAX_VPS_OP_LAYER_SETS_PLUS1               3
 #if VPS_RENAME
 #define MAX_VPS_LAYER_SETS_PLUS1                  1024
@@ -192,10 +241,4 @@
 #define MAX_VPS_NUH_RESERVED_ZERO_LAYER_ID_PLUS1  1
 #endif
-#define RATE_CONTROL_LAMBDA_DOMAIN                  1  ///< JCTVC-K0103, rate control by R-lambda model
-#define M0036_RC_IMPROVEMENT                        1  ///< JCTVC-M0036, improvement for R-lambda model based rate control
-#define TICKET_1090_FIX                             1
-
-#define RC_FIX                                      1  /// suggested fix for M0036
-#define RATE_CONTROL_INTRA                          1  ///< JCTVC-M0257, rate control for intra 
 
 #define MAX_CPB_CNT                     32  ///< Upper bound of (cpb_cnt_minus1 + 1)
@@ -216,7 +259,7 @@
 #define C1FLAG_NUMBER               8 // maximum number of largerThan1 flag coded in one chunk :  16 in HM5
 #define C2FLAG_NUMBER               1 // maximum number of largerThan2 flag coded in one chunk:  16 in HM5 
-
+#if !HM_CLEANUP_SAO
 #define REMOVE_SAO_LCU_ENC_CONSTRAINTS_3 1  ///< disable the encoder constraint that conditionally disable SAO for chroma for entire slice in interleaved mode
-
+#endif
 #define SAO_ENCODING_CHOICE              1  ///< I0184: picture early termination
 #if SAO_ENCODING_CHOICE
@@ -232,7 +275,5 @@
 #define MAX_NUM_PPS                64
 
-#define WEIGHTED_CHROMA_DISTORTION  1   ///< F386: weighting of chroma for RDO
 #define RDOQ_CHROMA_LAMBDA          1   ///< F386: weighting of chroma for RDOQ
-#define SAO_CHROMA_LAMBDA           1   ///< F386: weighting of chroma for SAO
 
 #define MIN_SCAN_POS_CROSS          4
@@ -248,6 +289,4 @@
 #define LEVEL_RANGE                         30     ///< G382: max coefficient level in statistics collection
 #endif
-
-#define NS_HAD                               0
 
 #define HHI_RQT_INTRA_SPEEDUP             1           ///< tests one best mode with full rqt
@@ -316,6 +355,4 @@
 #define AMP_MRG                               1           ///< encoder only force merge for AMP partition (no motion search for AMP)
 #endif
-
-#define SCALING_LIST_OUTPUT_RESULT    0 //JCTVC-G880/JCTVC-G1016 quantization matrices
 
 #define CABAC_INIT_PRESENT_FLAG     1
@@ -377,4 +414,97 @@
 };
 
+#if HM_CLEANUP_SAO
+enum SAOComponentIdx
+{
+  SAO_Y =0,
+  SAO_Cb,
+  SAO_Cr,
+  NUM_SAO_COMPONENTS
+};
+
+enum SAOMode //mode
+{
+  SAO_MODE_OFF = 0,
+  SAO_MODE_NEW,
+  SAO_MODE_MERGE,
+  NUM_SAO_MODES
+};
+
+enum SAOModeMergeTypes 
+{
+  SAO_MERGE_LEFT =0,
+  SAO_MERGE_ABOVE,
+  NUM_SAO_MERGE_TYPES
+};
+
+
+enum SAOModeNewTypes 
+{
+  SAO_TYPE_START_EO =0,
+  SAO_TYPE_EO_0 = SAO_TYPE_START_EO,
+  SAO_TYPE_EO_90,
+  SAO_TYPE_EO_135,
+  SAO_TYPE_EO_45,
+  
+  SAO_TYPE_START_BO,
+  SAO_TYPE_BO = SAO_TYPE_START_BO,
+
+  NUM_SAO_NEW_TYPES
+};
+#define NUM_SAO_EO_TYPES_LOG2 2
+
+enum SAOEOClasses 
+{
+  SAO_CLASS_EO_FULL_VALLEY = 0,
+  SAO_CLASS_EO_HALF_VALLEY = 1,
+  SAO_CLASS_EO_PLAIN       = 2,
+  SAO_CLASS_EO_HALF_PEAK   = 3,
+  SAO_CLASS_EO_FULL_PEAK   = 4,
+  NUM_SAO_EO_CLASSES,
+};
+
+
+#define NUM_SAO_BO_CLASSES_LOG2  5
+enum SAOBOClasses
+{
+  //SAO_CLASS_BO_BAND0 = 0,
+  //SAO_CLASS_BO_BAND1,
+  //SAO_CLASS_BO_BAND2,
+  //...
+  //SAO_CLASS_BO_BAND31,
+
+  NUM_SAO_BO_CLASSES = (1<<NUM_SAO_BO_CLASSES_LOG2),
+};
+#define MAX_NUM_SAO_CLASSES  32  //(NUM_SAO_EO_GROUPS > NUM_SAO_BO_GROUPS)?NUM_SAO_EO_GROUPS:NUM_SAO_BO_GROUPS
+
+struct SAOOffset
+{
+  Int modeIdc; //NEW, MERGE, OFF
+  Int typeIdc; //NEW: EO_0, EO_90, EO_135, EO_45, BO. MERGE: left, above
+  Int typeAuxInfo; //BO: starting band index
+  Int offset[MAX_NUM_SAO_CLASSES];
+
+  SAOOffset();
+  ~SAOOffset();
+  Void reset();
+
+  const SAOOffset& operator= (const SAOOffset& src);
+};
+
+struct SAOBlkParam
+{
+
+  SAOBlkParam();
+  ~SAOBlkParam();
+  Void reset();
+  const SAOBlkParam& operator= (const SAOBlkParam& src);
+  SAOOffset& operator[](Int compIdx){ return offsetParam[compIdx];}
+private:
+  SAOOffset offsetParam[NUM_SAO_COMPONENTS];
+
+};
+
+
+#else
 #define NUM_DOWN_PART 4
 
@@ -448,5 +578,5 @@
   ~SAOParam();
 };
-
+#endif
 /// parameters for deblocking filter
 typedef struct _LFCUParam
