Index: branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.cpp	(revision 824)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.cpp	(revision 825)
@@ -15,8 +15,7 @@
   m_pEncCuboid = NULL;
   m_pBestEncCuboid = NULL;
-  memset( m_nPrevFrameBit , 0 , sizeof( m_nPrevFrameBit ) );
-  memset( m_nPrevFrameCGSBit , 0 , sizeof( m_nPrevFrameCGSBit ) );
-  memset( m_nPrevFrameCGSPartNumLog2 , 0 , sizeof( m_nPrevFrameCGSPartNumLog2 ) );
-  memset( m_nPrevFrameOverWritePPS , 0 , sizeof( m_nPrevFrameOverWritePPS ) );
+  m_nAccuFrameBit = 0;
+  m_nAccuFrameCGSBit = 0;
+  m_nPrevFrameCGSPartNumLog2 = 0;
   m_dTotalFrameBit = 0;
   m_nTotalCGSBit = 0;
@@ -32,5 +31,9 @@
   }
 
-  TCom3DAsymLUT::create( nMaxOctantDepth , nInputBitDepth , nInputBitDepthC, nOutputBitDepth , nOutputBitDepthC, nMaxYPartNumLog2 );
+  TCom3DAsymLUT::create( nMaxOctantDepth , nInputBitDepth , nInputBitDepthC, nOutputBitDepth , nOutputBitDepthC, nMaxYPartNumLog2 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+    , 1 << ( nInputBitDepthC - 1 ) , 1 << ( nInputBitDepthC - 1 )
+#endif
+    );
   xAllocate3DArray( m_pColorInfo , xGetYSize() , xGetUSize() , xGetVSize() );
   xAllocate3DArray( m_pColorInfoC , xGetYSize() , xGetUSize() , xGetVSize() );
@@ -58,4 +61,67 @@
 }
 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Double TEnc3DAsymLUT::xxDeriveVertexPerColor( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
+  Pel & rP0 , Pel & rP1 , Pel & rP3 , Pel & rP7 , Int nResQuantBit )
+{
+  Int nInitP0 = rP0;
+  Int nInitP1 = rP1;
+  Int nInitP3 = rP3;
+  Int nInitP7 = rP7;
+
+  const Int nOne = xGetNormCoeffOne();
+  Double dNorm = (N * yy * vv * uu - N * yy * uv * uv - N * yv * yv * uu - N * vv * yu * yu + 2 * N * yv * uv * yu - yy * vs * vs * uu + 2 * yy * vs * uv * us - yy * vv * us * us - 2 * vs * uv * yu * ys + uv * uv * ys * ys + vs * vs * yu * yu - 2 * yv * vs * us * yu + 2 * yv * vs * ys * uu - 2 * yv * uv * us * ys + 2 * vv * yu * ys * us - vv * uu * ys * ys + yv * yv * us * us);
+  if( N > 16 && dNorm != 0 )
+  {
+    Double dInitA = (-N * uu * yv * Yv + N * uu * Yy * vv - N * Yy * uv * uv + N * yv * uv * Yu - N * yu * Yu * vv + N * yu * uv * Yv + yu * us * Ys * vv - vs * ys * uv * Yu - yu * vs * us * Yv - yv * uv * us * Ys - yv * vs * us * Yu - yu * uv * vs * Ys - ys * us * uv * Yv + ys * us * Yu * vv + 2 * Yy * vs * uv * us + uu * yv * vs * Ys - uu * ys * Ys * vv + uu * vs * ys * Yv + ys * Ys * uv * uv - Yy * vv * us * us + yu * Yu * vs * vs + yv * Yv * us * us - uu * Yy * vs * vs) / dNorm;
+    Double dInitB = (N * yy * Yu * vv - N * yy * uv * Yv - N * Yu * yv * yv - N * yu * Yy * vv + N * uv * yv * Yy + N * yv * yu * Yv - yy * us * Ys * vv + yy * uv * vs * Ys - yy * Yu * vs * vs + yy * vs * us * Yv - uv * vs * ys * Yy - yv * yu * vs * Ys + yu * Yy * vs * vs + yu * ys * Ys * vv - uv * yv * ys * Ys + 2 * Yu * yv * vs * ys + us * ys * Yy * vv - vs * ys * yu * Yv + uv * ys * ys * Yv + us * Ys * yv * yv - Yu * ys * ys * vv - yv * ys * us * Yv - vs * us * yv * Yy) / dNorm;
+    Double dInitC = -(-N * yy * Yv * uu + N * yy * uv * Yu - N * yv * yu * Yu - N * uv * yu * Yy + N * Yv * yu * yu + N * yv * Yy * uu - yy * uv * us * Ys + yy * Yv * us * us + yy * vs * Ys * uu - yy * vs * us * Yu + yv * ys * us * Yu - vs * Ys * yu * yu - yv * ys * Ys * uu + vs * us * yu * Yy + vs * ys * yu * Yu - uv * Yu * ys * ys + Yv * uu * ys * ys - yv * Yy * us * us - 2 * Yv * yu * ys * us - vs * ys * Yy * uu + uv * us * ys * Yy + uv * yu * ys * Ys + yv * yu * us * Ys) / dNorm;
+    nInitP0 = ( Int )( dInitA * nOne + 0.5 ) >> nResQuantBit << nResQuantBit;
+    nInitP1 = ( Int )( dInitB * nOne + 0.5 ) >> nResQuantBit << nResQuantBit;
+    nInitP3 = ( Int )( dInitC * nOne + 0.5 ) >> nResQuantBit << nResQuantBit;
+  }
+
+  Int nMin = - ( 1 << ( m_nLUTBitDepth - 1 ) );
+  Int nMax = - nMin - ( 1 << nResQuantBit  );
+  Int nMask = ( 1 << nResQuantBit ) - 1;
+
+  Double dMinError = MAX_DOUBLE;
+  Int nTestRange = 2;
+  Int nStepSize = 1 << nResQuantBit;
+  for( Int i = - nTestRange ; i <= nTestRange ; i++ )
+  {
+    for( Int j = - nTestRange ; j <= nTestRange ; j++ )
+    {
+      for( Int k = - nTestRange ; k <= nTestRange ; k++ )
+      {
+        Int nTestP0 = Clip3( nMin , nMax , nInitP0 + i * nStepSize );
+        Int nTestP1 = Clip3( nMin , nMax , nInitP1 + j * nStepSize );
+        Int nTestP3 = Clip3( nMin , nMax , nInitP3 + k * nStepSize );
+        Double a = 1.0 * nTestP0 / nOne;
+        Double b = 1.0 * nTestP1 / nOne;
+        Double c = 1.0 * nTestP3 / nOne;
+        Double d = ( Ys - a * ys - b * us - c * vs ) / N;
+        nInitP7 = ( ( Int )d ) >> nResQuantBit << nResQuantBit;
+        for( Int m = 0 ; m < 2 ; m++ )
+        {
+          Int nTestP7 = Clip3( nMin , nMax , nInitP7 + m * nStepSize );
+          Double dError = xxCalEstDist( N , Ys , Yy , Yu , Yv , ys , us , vs , yy , yu , yv , uu , uv , vv , YY , a , b , c , nTestP7 );
+          if( dError < dMinError )
+          {
+            dMinError = dError;
+            rP0 = ( Pel )nTestP0;
+            rP1 = ( Pel )nTestP1;
+            rP3 = ( Pel )nTestP3;
+            rP7 = ( Pel )nTestP7;
+          }
+        }
+      }
+    }
+  }
+  assert( !( rP0 & nMask ) && !( rP1 & nMask ) && !( rP3 & nMask ) && !( rP7 & nMask ) );
+
+  return( dMinError );
+}
+#else
 Double TEnc3DAsymLUT::xxDeriveVertexPerColor( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
   Int y0 , Int u0 , Int v0 , Int nLengthY , Int nLengthUV ,
@@ -134,5 +200,42 @@
   return( dMinError );
 }
-
+#endif
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Double TEnc3DAsymLUT::estimateDistWithCur3DAsymLUT( TComPic * pCurPic , UInt refLayerIdc )
+{
+  xxCollectData( pCurPic , refLayerIdc );
+
+  Double dErrorLuma = 0 , dErrorChroma = 0;
+  Int nYSize = 1 << ( getCurOctantDepth() + getCurYPartNumLog2() );
+  Int nUVSize = 1 << getCurOctantDepth();
+  for( Int yIdx = 0 ; yIdx < nYSize ; yIdx++ )
+  {
+    for( Int uIdx = 0 ; uIdx < nUVSize ; uIdx++ )
+    {
+      for( Int vIdx = 0 ; vIdx < nUVSize ; vIdx++ )
+      {
+        SColorInfo & rCuboidColorInfo = m_pColorInfo[yIdx][uIdx][vIdx];
+        SColorInfo & rCuboidColorInfoC = m_pColorInfoC[yIdx][uIdx][vIdx];
+        SCuboid & rCuboid = xGetCuboid( yIdx , uIdx , vIdx );
+        if( rCuboidColorInfo.N > 0 )
+        {
+          dErrorLuma += xxCalEstDist( rCuboidColorInfo.N , rCuboidColorInfo.Ys , rCuboidColorInfo.Yy , rCuboidColorInfo.Yu , rCuboidColorInfo.Yv , rCuboidColorInfo.ys , rCuboidColorInfo.us , rCuboidColorInfo.vs , rCuboidColorInfo.yy , rCuboidColorInfo.yu , rCuboidColorInfo.yv , rCuboidColorInfo.uu , rCuboidColorInfo.uv , rCuboidColorInfo.vv , rCuboidColorInfo.YY ,
+            rCuboid.P[0].Y , rCuboid.P[1].Y , rCuboid.P[2].Y , rCuboid.P[3].Y );
+        }
+        if( rCuboidColorInfoC.N > 0 )
+        {
+          dErrorChroma += xxCalEstDist( rCuboidColorInfoC.N , rCuboidColorInfoC.Us , rCuboidColorInfoC.Uy , rCuboidColorInfoC.Uu , rCuboidColorInfoC.Uv , rCuboidColorInfoC.ys , rCuboidColorInfoC.us , rCuboidColorInfoC.vs , rCuboidColorInfoC.yy , rCuboidColorInfoC.yu , rCuboidColorInfoC.yv , rCuboidColorInfoC.uu , rCuboidColorInfoC.uv , rCuboidColorInfoC.vv , rCuboidColorInfoC.UU ,
+            rCuboid.P[0].U , rCuboid.P[1].U , rCuboid.P[2].U , rCuboid.P[3].U );
+          dErrorChroma += xxCalEstDist( rCuboidColorInfoC.N , rCuboidColorInfoC.Vs , rCuboidColorInfoC.Vy , rCuboidColorInfoC.Vu , rCuboidColorInfoC.Vv , rCuboidColorInfoC.ys , rCuboidColorInfoC.us , rCuboidColorInfoC.vs , rCuboidColorInfoC.yy , rCuboidColorInfoC.yu , rCuboidColorInfoC.yv , rCuboidColorInfoC.uu , rCuboidColorInfoC.uv , rCuboidColorInfoC.vv , rCuboidColorInfoC.VV ,
+            rCuboid.P[0].V , rCuboid.P[1].V , rCuboid.P[2].V , rCuboid.P[3].V );
+        }
+      }
+    }
+  }
+
+  return( dErrorLuma + dErrorChroma);
+}
+#else
 Double TEnc3DAsymLUT::estimateDistWithCur3DAsymLUT( TComPic * pCurPic , UInt refLayerIdc )
 {
@@ -174,5 +277,66 @@
   return( dErrorLuma + dErrorChroma);
 }
-
+#endif
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Double TEnc3DAsymLUT::derive3DAsymLUT( TComSlice * pSlice , TComPic * pCurPic , UInt refLayerIdc , TEncCfg * pCfg , Bool bSignalPPS , Bool bElRapSliceTypeB )
+{
+  m_nLUTBitDepth = pCfg->getCGSLUTBit();
+  Int nCurYPartNumLog2 = 0 , nCurOctantDepth = 0; 
+  xxDerivePartNumLog2( pSlice , pCfg , nCurOctantDepth , nCurYPartNumLog2 , bSignalPPS , bElRapSliceTypeB );
+
+  Int nBestResQuanBit = 0;
+  Int nBestAdaptCThresholdU = 1 << ( getInputBitDepthC() - 1 );
+  Int nBestAdaptCThresholdV = 1 << ( getInputBitDepthC() - 1 );
+  Int nBestOctantDepth = nCurOctantDepth;
+  Int nBestYPartNumLog2 = nCurYPartNumLog2;
+  Int nTargetLoop = 1 + ( pCfg->getCGSAdaptChroma() && ( nCurOctantDepth == 1 || ( nCurOctantDepth * 3 + nCurYPartNumLog2 ) >= 5 ) );
+  Double dMinError = MAX_DOUBLE;
+  for( Int nLoop = 0 ; nLoop < nTargetLoop ; nLoop++ )
+  {
+    Int nAdaptCThresholdU = 1 << ( getInputBitDepthC() - 1 );
+    Int nAdaptCThresholdV = 1 << ( getInputBitDepthC() - 1 );
+    if( nLoop > 0 )
+    {
+      nAdaptCThresholdU = ( Int )( m_dSumU / m_nNChroma + 0.5 );
+      nAdaptCThresholdV = ( Int )( m_dSumV / m_nNChroma + 0.5 );
+      if( nCurOctantDepth > 1 )
+      {
+        nCurOctantDepth = 1;
+        nCurYPartNumLog2 = 2;
+      }
+      if( nAdaptCThresholdU == nBestAdaptCThresholdU && nAdaptCThresholdV == nBestAdaptCThresholdV 
+        && nCurOctantDepth == nBestOctantDepth && nCurYPartNumLog2 == nBestYPartNumLog2 )
+        break;
+    }
+    xUpdatePartitioning( nCurOctantDepth , nCurYPartNumLog2 , nAdaptCThresholdU , nAdaptCThresholdV );
+    xxCollectData( pCurPic , refLayerIdc );
+    for( Int nResQuanBit = 0 ; nResQuanBit < 4 ; nResQuanBit++ )
+    {
+      Double dError = xxDeriveVertexes( nResQuanBit , m_pEncCuboid ) / ( 1 + ( nResQuanBit > 0 ) * 0.001 * ( pSlice->getDepth() + 1 ) );
+      if( dError <= dMinError )
+      {
+        nBestResQuanBit = nResQuanBit;
+        nBestAdaptCThresholdU = nAdaptCThresholdU;
+        nBestAdaptCThresholdV = nAdaptCThresholdV;
+        nBestOctantDepth = nCurOctantDepth;
+        nBestYPartNumLog2 = nCurYPartNumLog2;
+        SCuboid *** tmp = m_pBestEncCuboid;
+        m_pBestEncCuboid = m_pEncCuboid;
+        m_pEncCuboid = tmp;
+        dMinError = dError;
+      }
+      else
+      {
+        break;
+      }
+    }
+  }
+  setResQuantBit( nBestResQuanBit );
+  xUpdatePartitioning( nBestOctantDepth , nBestYPartNumLog2 , nBestAdaptCThresholdU , nBestAdaptCThresholdV );
+  xSaveCuboids( m_pBestEncCuboid );
+  return( dMinError );
+}
+#else
 Double TEnc3DAsymLUT::derive3DAsymLUT( TComSlice * pSlice , TComPic * pCurPic , UInt refLayerIdc , TEncCfg * pCfg , Bool bSignalPPS , Bool bElRapSliceTypeB )
 {
@@ -204,7 +368,7 @@
   setResQuantBit( nBestResQuanBit );
   xSaveCuboids( m_pBestEncCuboid );
-
   return( dCurError );
 }
+#endif
 
 Double TEnc3DAsymLUT::xxDeriveVertexes( Int nResQuanBit , SCuboid *** pCurCuboid )
@@ -213,6 +377,8 @@
   Int nYSize = 1 << ( getCurOctantDepth() + getCurYPartNumLog2() );
   Int nUVSize = 1 << getCurOctantDepth();
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
   Int nLengthY = 1 << ( getInputBitDepthY() - getCurOctantDepth() - getCurYPartNumLog2() );
   Int nLengthUV = 1 << ( getInputBitDepthC() - getCurOctantDepth() );
+#endif
   for( Int yIdx = 0 ; yIdx < nYSize ; yIdx++ )
   {
@@ -224,7 +390,9 @@
         SColorInfo & rCuboidColorInfoC = m_pColorInfoC[yIdx][uIdx][vIdx];
         SCuboid & rCuboid = pCurCuboid[yIdx][uIdx][vIdx];
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
         Int y0 = yIdx << xGetYShift2Idx();
         Int u0 = uIdx << xGetUShift2Idx();
         Int v0 = vIdx << xGetVShift2Idx();
+#endif
         for( Int idxVertex = 0 ; idxVertex < 4 ; idxVertex++ )
         {
@@ -235,12 +403,21 @@
         {
           dErrorLuma += xxDeriveVertexPerColor( rCuboidColorInfo.N , rCuboidColorInfo.Ys , rCuboidColorInfo.Yy , rCuboidColorInfo.Yu , rCuboidColorInfo.Yv , rCuboidColorInfo.ys , rCuboidColorInfo.us , rCuboidColorInfo.vs , rCuboidColorInfo.yy , rCuboidColorInfo.yu , rCuboidColorInfo.yv , rCuboidColorInfo.uu , rCuboidColorInfo.uv , rCuboidColorInfo.vv , rCuboidColorInfo.YY ,
-            y0 , u0 , v0 , nLengthY , nLengthUV , rCuboid.P[0].Y , rCuboid.P[1].Y , rCuboid.P[2].Y , rCuboid.P[3].Y , nResQuanBit );
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
+            y0 , u0 , v0 , nLengthY , nLengthUV , 
+#endif
+            rCuboid.P[0].Y , rCuboid.P[1].Y , rCuboid.P[2].Y , rCuboid.P[3].Y , nResQuanBit );
         }
         if( rCuboidColorInfoC.N > 0 )
         {
           dErrorChroma += xxDeriveVertexPerColor( rCuboidColorInfoC.N , rCuboidColorInfoC.Us , rCuboidColorInfoC.Uy , rCuboidColorInfoC.Uu , rCuboidColorInfoC.Uv , rCuboidColorInfoC.ys , rCuboidColorInfoC.us , rCuboidColorInfoC.vs , rCuboidColorInfoC.yy , rCuboidColorInfoC.yu , rCuboidColorInfoC.yv , rCuboidColorInfoC.uu , rCuboidColorInfoC.uv , rCuboidColorInfoC.vv , rCuboidColorInfoC.UU ,
-            y0 , u0 , v0 , nLengthY , nLengthUV , rCuboid.P[0].U , rCuboid.P[1].U , rCuboid.P[2].U , rCuboid.P[3].U , nResQuanBit );
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
+            y0 , u0 , v0 , nLengthY , nLengthUV , 
+#endif
+            rCuboid.P[0].U , rCuboid.P[1].U , rCuboid.P[2].U , rCuboid.P[3].U , nResQuanBit );
           dErrorChroma += xxDeriveVertexPerColor( rCuboidColorInfoC.N , rCuboidColorInfoC.Vs , rCuboidColorInfoC.Vy , rCuboidColorInfoC.Vu , rCuboidColorInfoC.Vv , rCuboidColorInfoC.ys , rCuboidColorInfoC.us , rCuboidColorInfoC.vs , rCuboidColorInfoC.yy , rCuboidColorInfoC.yu , rCuboidColorInfoC.yv , rCuboidColorInfoC.uu , rCuboidColorInfoC.uv , rCuboidColorInfoC.vv , rCuboidColorInfoC.VV ,
-            y0 , u0 , v0 , nLengthY , nLengthUV , rCuboid.P[0].V , rCuboid.P[1].V , rCuboid.P[2].V , rCuboid.P[3].V , nResQuanBit );
+#if !R0151_CGS_3D_ASYMLUT_IMPROVE
+            y0 , u0 , v0 , nLengthY , nLengthUV , 
+#endif
+            rCuboid.P[0].V , rCuboid.P[1].V , rCuboid.P[2].V , rCuboid.P[3].V , nResQuanBit );
         }
 
@@ -299,4 +476,8 @@
   // since we do data collection only for overlapped region, the border extension is good enough
 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  m_dSumU = m_dSumV = 0;
+  m_nNChroma = 0;
+#endif
   for( Int i = top ; i <= bottom ; i++ )
   {
@@ -330,6 +511,15 @@
       }
 
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+      m_dSumU += u;
+      m_dSumV += v;
+      m_nNChroma++;
+#endif
       SColorInfo sColorInfo;
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+      SColorInfo & rCuboidColorInfo = m_pColorInfo[xGetYIdx(y)][xGetUIdx(u)][xGetVIdx(v)];
+#else
       SColorInfo & rCuboidColorInfo = m_pColorInfo[y>>xGetYShift2Idx()][u>>xGetUShift2Idx()][v>>xGetVShift2Idx()];
+#endif
       memset(&sColorInfo, 0, sizeof(SColorInfo));
       sColorInfo.Ys = Y;
@@ -358,5 +548,9 @@
         u = pIRLU[posIRLUV];
         v = pIRLV[posIRLUV];
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+        SColorInfo & rCuboidColorInfoC = m_pColorInfoC[xGetYIdx(y)][xGetUIdx(u)][xGetVIdx(v)];
+#else
         SColorInfo & rCuboidColorInfoC = m_pColorInfoC[y>>xGetYShift2Idx()][u>>xGetUShift2Idx()][v>>xGetVShift2Idx()];
+#endif
         sColorInfo.Us = U;
         sColorInfo.Vs = V;
@@ -389,21 +583,4 @@
 Void TEnc3DAsymLUT::xxDerivePartNumLog2( TComSlice * pSlice , TEncCfg * pcCfg , Int & rOctantDepth , Int & rYPartNumLog2 , Bool bSignalPPS , Bool bElRapSliceTypeB )
 {
-  Int nSliceType = pSlice->getSliceType();
-  // update slice type as what will be done later
-  if( pSlice->getActiveNumILRRefIdx() == 0 && pSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA )
-  {
-    nSliceType = I_SLICE;
-  }
-  else if( !bElRapSliceTypeB )
-  {
-    if( (pSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP) &&
-      (pSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA) &&
-      pSlice->getSliceType() == B_SLICE )
-    {
-      nSliceType = P_SLICE;
-    }
-  }
-
-  const Int nSliceTempLevel = pSlice->getDepth();
   Int nPartNumLog2 = 4;
   if( pSlice->getBaseColPic( pSlice->getInterLayerPredLayerIdc( 0 ) )->getSlice( 0 )->isIntra() )
@@ -411,8 +588,8 @@
     nPartNumLog2 = xGetMaxPartNumLog2();
   }
-  if( m_nPrevFrameBit[nSliceType][nSliceTempLevel] && pSlice->getPPS()->getCGSFlag() ) 
-  {
-    Double dBitCost = 1.0 * m_nPrevFrameCGSBit[nSliceType][nSliceTempLevel] / m_nPrevFrameBit[nSliceType][nSliceTempLevel];
-    nPartNumLog2 = m_nPrevFrameCGSPartNumLog2[nSliceType][nSliceTempLevel];
+  if( m_nAccuFrameBit && pSlice->getPPS()->getCGSFlag() ) 
+  {
+    Double dBitCost = 1.0 * m_nAccuFrameCGSBit / m_nAccuFrameBit;
+    nPartNumLog2 = m_nPrevFrameCGSPartNumLog2;
     Double dBitCostT = 0.03;
     if( dBitCost < dBitCostT / 6.0 )
@@ -425,9 +602,5 @@
     }
   }
-  else
-  {
-    nPartNumLog2 -= nSliceTempLevel;
-  }
-  nPartNumLog2 = Clip3( 0 , xGetMaxPartNumLog2() , nPartNumLog2 );
+  nPartNumLog2 = Clip3( 0 , xGetMaxPartNumLog2()  , nPartNumLog2 );
   xxMapPartNum2DepthYPart( nPartNumLog2 , rOctantDepth , rYPartNumLog2 );
 }
@@ -453,18 +626,14 @@
 Void TEnc3DAsymLUT::updatePicCGSBits( TComSlice * pcSlice , Int nPPSBit )
 {
-  const Int nSliceType = pcSlice->getSliceType();
-  const Int nSliceTempLevel = pcSlice->getDepth();
-
   for( Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
   {
     UInt refLayerIdc = pcSlice->getInterLayerPredLayerIdc(i);
-    m_nPrevFrameBit[nSliceType][nSliceTempLevel] = pcSlice->getPic()->getFrameBit() + pcSlice->getBaseColPic(refLayerIdc)->getFrameBit();
+    m_nAccuFrameBit += pcSlice->getPic()->getFrameBit() + pcSlice->getBaseColPic(refLayerIdc)->getFrameBit();
     m_dTotalFrameBit += pcSlice->getPic()->getFrameBit() + pcSlice->getBaseColPic(refLayerIdc)->getFrameBit();
   }
-  m_nPrevFrameOverWritePPS[nSliceType][nSliceTempLevel] = pcSlice->getCGSOverWritePPS();
-  m_nPrevFrameCGSBit[nSliceType][nSliceTempLevel] = nPPSBit;
+  m_nAccuFrameCGSBit += nPPSBit;
   m_nTotalCGSBit += nPPSBit;
-  m_nPrevFrameCGSPartNumLog2[nSliceType][nSliceTempLevel] = getCurOctantDepth() * 3 + getCurYPartNumLog2();
-}
-
-#endif
+  m_nPrevFrameCGSPartNumLog2 = getCurOctantDepth() * 3 + getCurYPartNumLog2();
+}
+
+#endif
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.h	(revision 824)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEnc3DAsymLUT.h	(revision 825)
@@ -78,17 +78,26 @@
   SCuboid *** m_pEncCuboid;
   SCuboid *** m_pBestEncCuboid;
-  Int   m_nPrevFrameBit[3][MAX_TLAYER];                  // base + enhancement layer
-  Int   m_nPrevFrameCGSBit[3][MAX_TLAYER];
-  Int   m_nPrevFrameCGSPartNumLog2[3][MAX_TLAYER];
-  Int   m_nPrevFrameOverWritePPS[3][MAX_TLAYER];
+  Int   m_nAccuFrameBit;                  // base + enhancement layer
+  Int   m_nAccuFrameCGSBit;
+  Int   m_nPrevFrameCGSPartNumLog2;
   Double m_dTotalFrameBit;
   Int   m_nTotalCGSBit;
   Int   m_nPPSBit;
   Int   m_nLUTBitDepth;
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Double m_dSumU;
+  Double m_dSumV;
+  Int    m_nNChroma;
+#endif
 
 private:
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Double  xxDeriveVertexPerColor( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
+    Pel & rP0 , Pel & rP1 , Pel & rP3 , Pel & rP7 , Int nResQuantBit );
+#else
   Double  xxDeriveVertexPerColor( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
     Int y0 , Int u0 , Int v0 , Int nLengthY , Int nLengthUV ,
     Pel & rP0 , Pel & rP1 , Pel & rP3 , Pel & rP7 , Int nResQuantBit );
+#endif
   Void    xxDerivePartNumLog2( TComSlice * pSlice , TEncCfg * pcCfg , Int & rOctantDepth , Int & rYPartNumLog2 , Bool bSignalPPS , Bool bElRapSliceTypeB );
   Void    xxMapPartNum2DepthYPart( Int nPartNumLog2 , Int & rOctantDepth , Int & rYPartNumLog2 );
@@ -100,4 +109,8 @@
   inline Double  xxCalEstDist( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
     Double a , Double b , Double c , Double d );
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  inline Double  xxCalEstDist( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
+    Pel nP0 , Pel nP1 , Pel nP3 , Pel nP7 );
+#endif
 };
 
@@ -118,5 +131,20 @@
   return( dError );
 };
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Double TEnc3DAsymLUT::xxCalEstDist( Double N , Double Ys , Double Yy , Double Yu , Double Yv , Double ys , Double us , Double vs , Double yy , Double yu , Double yv , Double uu , Double uv , Double vv , Double YY ,
+  Pel nP0 , Pel nP1 , Pel nP3 , Pel nP7 )
+{
+  const Int nOne = xGetNormCoeffOne();
+  Double a = 1.0 * nP0 / nOne;
+  Double b = 1.0 * nP1 / nOne;
+  Double c = 1.0 * nP3 / nOne;
+  Double d = nP7;
+  Double dError = N * d * d + 2 * b * c * uv + 2 * a * c * yv + 2 * a * b * yu - 2 * c * Yv - 2 * b * Yu - 2 * a * Yy + 2 * c * d * vs + 2 * b * d * us + 2 * a * d * ys + a * a * yy + c * c * vv + b * b * uu - 2 * d * Ys + YY;
+  return( dError );
+};
 #endif
 
 #endif
+
+#endif
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 824)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 825)
@@ -2762,4 +2762,12 @@
 Void TEncCavlc::xCode3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT )
 {
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  UInt uiNumRefLayers = ( UInt )pc3DAsymLUT->getRefLayerNum();
+  WRITE_UVLC( uiNumRefLayers - 1 , "num_cm_ref_layers_minus1" );
+  for( UInt i = 0 ; i < uiNumRefLayers ; i++ )
+  {
+    WRITE_CODE( pc3DAsymLUT->getRefLayerId( i ) , 6 , "cm_ref_layer_id" );
+  }
+#endif
   assert( pc3DAsymLUT->getCurOctantDepth() < 4 );
   WRITE_CODE( pc3DAsymLUT->getCurOctantDepth() , 2 , "cm_octant_depth" );
@@ -2767,4 +2775,10 @@
   WRITE_CODE( pc3DAsymLUT->getCurYPartNumLog2() , 2 , "cm_y_part_num_log2" );
   assert( pc3DAsymLUT->getInputBitDepthY() < 16 );
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+  WRITE_CODE( pc3DAsymLUT->getInputBitDepthY() - 8 , 3 , "cm_input_luma_bit_depth_minus8" );
+  WRITE_CODE( pc3DAsymLUT->getInputBitDepthC() - 8 , 3 , "cm_input_chroma_bit_depth_minus8" );
+  WRITE_CODE( pc3DAsymLUT->getOutputBitDepthY() - 8 , 3 , "cm_output_luma_bit_depth_minus8" );
+  WRITE_CODE( pc3DAsymLUT->getOutputBitDepthC() - 8 , 3 , "cm_output_chroma_bit_depth_minus8" );
+#else
   WRITE_CODE( pc3DAsymLUT->getInputBitDepthY() - 8 , 3 , "cm_input_bit_depth_minus8" );
   WRITE_SVLC(pc3DAsymLUT->getInputBitDepthC()-pc3DAsymLUT->getInputBitDepthY(), "cm_input_bit_depth_chroma delta");
@@ -2772,8 +2786,22 @@
   WRITE_CODE( pc3DAsymLUT->getOutputBitDepthY() - 8 , 3 , "cm_output_bit_depth_minus8" );
   WRITE_SVLC(pc3DAsymLUT->getOutputBitDepthC()-pc3DAsymLUT->getOutputBitDepthY(), "cm_output_bit_depth_chroma_delta");
+#endif
   assert( pc3DAsymLUT->getResQuantBit() < 4 );
   WRITE_CODE( pc3DAsymLUT->getResQuantBit() , 2 , "cm_res_quant_bit" );
-
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  if( pc3DAsymLUT->getCurOctantDepth() == 1 )
+  {
+    WRITE_SVLC( pc3DAsymLUT->getAdaptChromaThresholdU() - ( 1 << ( pc3DAsymLUT->getInputBitDepthC() - 1 ) ) , "cm_adapt_threshold_u_delta" );
+    WRITE_SVLC( pc3DAsymLUT->getAdaptChromaThresholdV() - ( 1 << ( pc3DAsymLUT->getInputBitDepthC() - 1 ) ) , "cm_adapt_threshold_v_delta" );
+  }
+#endif
+
+#if R0164_CGS_LUT_BUGFIX
+  pc3DAsymLUT->xInitCuboids();
+#endif
   xCode3DAsymLUTOctant( pc3DAsymLUT , 0 , 0 , 0 , 0 , 1 << pc3DAsymLUT->getCurOctantDepth() );
+#if R0164_CGS_LUT_BUGFIX
+  pc3DAsymLUT->xCuboidsExplicitCheck( false );
+#endif
 }
 
@@ -2802,19 +2830,48 @@
     for( Int l = 0 ; l < nYPartNum ; l++ )
     {
+#if R0164_CGS_LUT_BUGFIX      
+      Int shift = pc3DAsymLUT->getMaxOctantDepth() - nDepth ;
+#endif
       for( Int nVertexIdx = 0 ; nVertexIdx < 4 ; nVertexIdx++ )
       {
+#if R0164_CGS_LUT_BUGFIX 
+        SYUVP sRes = pc3DAsymLUT->getCuboidVertexResTree( yIdx + (l<<shift) , uIdx , vIdx , nVertexIdx );
+#else
         SYUVP sRes = pc3DAsymLUT->getCuboidVertexResTree( yIdx + l , uIdx , vIdx , nVertexIdx );
+#endif
         UInt uiCodeVertex = sRes.Y != 0 || sRes.U != 0 || sRes.V != 0;
         WRITE_FLAG( uiCodeVertex , "coded_vertex_flag" );
         if( uiCodeVertex )
         {
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+          xWriteParam( sRes.Y );
+          xWriteParam( sRes.U );
+          xWriteParam( sRes.V );
+#else
           WRITE_SVLC( sRes.Y , "resY" );
           WRITE_SVLC( sRes.U , "resU" );
           WRITE_SVLC( sRes.V , "resV" );
-        }
-      }
-    }
-  }
-}
+#endif
+        }
+      }
+#if R0164_CGS_LUT_BUGFIX
+      pc3DAsymLUT->xSetExplicit( yIdx + (l<<shift) , uIdx , vIdx );
+#endif
+    }
+  }
+}
+
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+Void TEncCavlc::xWriteParam( Int param)
+{
+  const UInt rParam = 7;
+  Int codeNumber = abs(param);
+  WRITE_UVLC(codeNumber / (1 << rParam), "quotient");
+  WRITE_CODE((codeNumber % (1 << rParam)), rParam, "remainder");
+  if (abs(param))
+    WRITE_FLAG( param <0, "sign");
+}
+#endif
+
 #endif
 //! \}
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h	(revision 824)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h	(revision 825)
@@ -173,4 +173,7 @@
   Void xCode3DAsymLUT( TCom3DAsymLUT * pc3DAsymLUT );
   Void xCode3DAsymLUTOctant( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength );
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Void xWriteParam( Int param);
+#endif
 #endif
 #endif //SVC_EXTENSION
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h	(revision 824)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h	(revision 825)
@@ -367,4 +367,7 @@
   Int  m_nCGSMaxYPartNumLog2;
   Int  m_nCGSLUTBit;
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Int  m_nCGSAdaptiveChroma;
+#endif
 #endif
 #if P0050_KNEE_FUNCTION_SEI
@@ -940,4 +943,8 @@
   Void      setCGSLUTBit(Int n)           { m_nCGSLUTBit = n;    }
   Int       getCGSLUTBit()                { return m_nCGSLUTBit; }
+#if R0151_CGS_3D_ASYMLUT_IMPROVE
+  Void      setCGSAdaptChroma(Int n)      { m_nCGSAdaptiveChroma = n;  }
+  Int       getCGSAdaptChroma()           { return m_nCGSAdaptiveChroma; }
+#endif
 #endif
 #endif
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 824)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 825)
@@ -1153,4 +1153,9 @@
         if( pcSlice->getPPS()->getCGSFlag() )
         {
+#if R0150_CGS_SIGNAL_CONSTRAINTS
+          // all reference layers are currently taken as CGS reference layers
+          m_Enc3DAsymLUTPPS.addRefLayerId( pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc) );
+          m_Enc3DAsymLUTPicUpdate.addRefLayerId( pcSlice->getVPS()->getRefLayerId(m_layerId, refLayerIdc) );
+#endif
           if(g_posScalingFactor[refLayerIdc][0] < (1<<16) || g_posScalingFactor[refLayerIdc][1] < (1<<16)) //if(pcPic->isSpatialEnhLayer(refLayerIdc))
           {
@@ -3987,4 +3992,6 @@
   if( m_layerId && pcSlice->getPPS()->getCGSFlag() )
   {
+    if( m_Enc3DAsymLUTPPS.getPPSBit() > 0 )
+      m_Enc3DAsymLUTPicUpdate.copy3DAsymLUT( &m_Enc3DAsymLUTPPS );
     m_Enc3DAsymLUTPicUpdate.updatePicCGSBits( pcSlice , m_Enc3DAsymLUTPPS.getPPSBit() );
   }
