Index: /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.cpp	(revision 825)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.cpp	(revision 826)
@@ -80,5 +80,9 @@
 {
   assert( nCurOctantDepth <= m_nMaxOctantDepth );
+#if R0179_CGS_SIZE_8x1x1
+  assert( nCurYPartNumLog2 + nCurOctantDepth <= m_nMaxYPartNumLog2 + m_nMaxOctantDepth );
+#else
   assert( nCurYPartNumLog2 <= m_nMaxYPartNumLog2 );
+#endif 
 
   m_nCurOctantDepth = nCurOctantDepth;
Index: /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.h	(revision 825)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TCom3DAsymLUT.h	(revision 826)
@@ -44,4 +44,9 @@
   Int   getResQuantBit()     { return m_nResQuanBit; }
   Void  setResQuantBit(Int n){ m_nResQuanBit = n; }
+#if R0300_CGS_RES_COEFF_CODING 
+  Int   getMappingShift()     { return m_nMappingShift; }
+  Int   getDeltaBits()        { return m_nDeltaBits; }
+  Void  setDeltaBits(Int n)   { m_nDeltaBits = n; }
+#endif 
   Int   getMaxYPartNumLog2() { return m_nMaxYPartNumLog2; }
   Int   getCurYPartNumLog2() { return m_nCurYPartNumLog2; }
@@ -97,4 +102,7 @@
   Int   m_nMappingOffset;
   Int   m_nResQuanBit;
+#if R0300_CGS_RES_COEFF_CODING
+  Int   m_nDeltaBits;
+#endif
   SCuboid *** m_pCuboid;
   const static Int m_nVertexIdxOffset[4][3];
Index: /branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h	(revision 825)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h	(revision 826)
@@ -96,4 +96,6 @@
 #define R0151_CGS_3D_ASYMLUT_IMPROVE     1      ///< JCTVC-R0151: Non-uniform chroma partitioning and improved LUT coefficient coding
 #define R0164_CGS_LUT_BUGFIX             1      ///< JCTVC-R0164: Bug fix with LUT syntax
+#define R0179_CGS_SIZE_8x1x1             1      ///< JCTVC-R0179: allow CGS LUT size to be 8x1x1 as well 
+#define R0300_CGS_RES_COEFF_CODING       1      ///< JCTVC-R0300: improved residual coefficient coding for R0151
 #endif
 #define O0194_WEIGHTED_PREDICTION_CGS    1      ///< JCTVC-O0194: Weighted prediciton for color gamut scalability
Index: /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 825)
+++ /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 826)
@@ -3935,4 +3935,7 @@
 #endif
   UInt uiCurOctantDepth , uiCurPartNumLog2 , uiInputBitDepthM8 , uiOutputBitDepthM8 , uiResQaunBit;
+#if R0300_CGS_RES_COEFF_CODING
+  UInt uiDeltaBits; 
+#endif 
   READ_CODE( 2 , uiCurOctantDepth , "cm_octant_depth" ); 
   READ_CODE( 2 , uiCurPartNumLog2 , "cm_y_part_num_log2" );     
@@ -3952,4 +3955,9 @@
 #endif
   READ_CODE( 2 , uiResQaunBit , "cm_res_quant_bit" );
+#if R0300_CGS_RES_COEFF_CODING
+  READ_CODE( 2 , uiDeltaBits , "cm_flc_bits" );
+  pc3DAsymLUT->setDeltaBits(uiDeltaBits + 1);
+#endif
+
 #if R0151_CGS_3D_ASYMLUT_IMPROVE
 #if R0150_CGS_SIGNAL_CONSTRAINTS
@@ -4020,4 +4028,8 @@
   else
   {
+#if R0300_CGS_RES_COEFF_CODING
+    Int nFLCbits = pc3DAsymLUT->getMappingShift()-pc3DAsymLUT->getResQuantBit()-pc3DAsymLUT->getDeltaBits() ; 
+    nFLCbits = nFLCbits >= 0 ? nFLCbits:0;
+#endif
     for( Int l = 0 ; l < nYPartNum ; l++ )
     {
@@ -4033,7 +4045,13 @@
         {
 #if R0151_CGS_3D_ASYMLUT_IMPROVE
+#if R0300_CGS_RES_COEFF_CODING
+          xReadParam( deltaY, nFLCbits );
+          xReadParam( deltaU, nFLCbits );
+          xReadParam( deltaV, nFLCbits );
+#else
           xReadParam( deltaY );
           xReadParam( deltaU );
           xReadParam( deltaV );
+#endif
 #else
           READ_SVLC( deltaY , "resY" );
@@ -4056,7 +4074,13 @@
 
 #if R0151_CGS_3D_ASYMLUT_IMPROVE
+#if R0300_CGS_RES_COEFF_CODING
+Void TDecCavlc::xReadParam( Int& param, Int rParam )
+#else
 Void TDecCavlc::xReadParam( Int& param )
-{
+#endif
+{
+#if !R0300_CGS_RES_COEFF_CODING
   const UInt rParam = 7;
+#endif
   UInt prefix;
   UInt codeWord ;
Index: /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.h	(revision 825)
+++ /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.h	(revision 826)
@@ -144,5 +144,9 @@
   Void xParse3DAsymLUTOctant( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength );
 #if R0151_CGS_3D_ASYMLUT_IMPROVE
+#if R0300_CGS_RES_COEFF_CODING
+  Void xReadParam( Int& param, Int flc_bits );
+#else
   Void xReadParam( Int& param );
+#endif
 #endif
 #endif
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 825)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 826)
@@ -2789,4 +2789,9 @@
   assert( pc3DAsymLUT->getResQuantBit() < 4 );
   WRITE_CODE( pc3DAsymLUT->getResQuantBit() , 2 , "cm_res_quant_bit" );
+#if R0300_CGS_RES_COEFF_CODING
+  xFindDeltaBits( pc3DAsymLUT );
+  assert(pc3DAsymLUT->getDeltaBits() >=1 && pc3DAsymLUT->getDeltaBits() <= 4);
+  WRITE_CODE( pc3DAsymLUT->getDeltaBits()-1 , 2 , "cm_delta_bit" );
+#endif 
 #if R0151_CGS_3D_ASYMLUT_IMPROVE
   if( pc3DAsymLUT->getCurOctantDepth() == 1 )
@@ -2828,4 +2833,8 @@
   else
   {
+#if R0300_CGS_RES_COEFF_CODING
+    Int nFLCbits = pc3DAsymLUT->getMappingShift()-pc3DAsymLUT->getResQuantBit()-pc3DAsymLUT->getDeltaBits() ; 
+    nFLCbits = nFLCbits >= 0 ? nFLCbits : 0;
+#endif
     for( Int l = 0 ; l < nYPartNum ; l++ )
     {
@@ -2845,7 +2854,13 @@
         {
 #if R0151_CGS_3D_ASYMLUT_IMPROVE
+#if R0300_CGS_RES_COEFF_CODING
+          xWriteParam( sRes.Y, nFLCbits );
+          xWriteParam( sRes.U, nFLCbits );
+          xWriteParam( sRes.V, nFLCbits );
+#else
           xWriteParam( sRes.Y );
           xWriteParam( sRes.U );
           xWriteParam( sRes.V );
+#endif
 #else
           WRITE_SVLC( sRes.Y , "resY" );
@@ -2863,7 +2878,13 @@
 
 #if R0151_CGS_3D_ASYMLUT_IMPROVE
+#if R0300_CGS_RES_COEFF_CODING
+Void TEncCavlc::xWriteParam( Int param, UInt rParam)
+#else
 Void TEncCavlc::xWriteParam( Int param)
-{
+#endif
+{
+#if !R0300_CGS_RES_COEFF_CODING
   const UInt rParam = 7;
+#endif
   Int codeNumber = abs(param);
   WRITE_UVLC(codeNumber / (1 << rParam), "quotient");
@@ -2874,4 +2895,96 @@
 #endif
 
+#if R0300_CGS_RES_COEFF_CODING
+Void TEncCavlc::xFindDeltaBits( TCom3DAsymLUT * pc3DAsymLUT )
+{
+  Int nDeltaBits; 
+  Int nBestDeltaBits = -1; 
+  Int nBestBits = MAX_INT; 
+  for( nDeltaBits = 1; nDeltaBits < 5; nDeltaBits++)
+  {
+    Int nCurBits = 0;
+    xTally3DAsymLUTOctantBits( pc3DAsymLUT , 0 , 0 , 0 , 0 , 1 << pc3DAsymLUT->getCurOctantDepth(), nDeltaBits, nCurBits );
+    //printf("%d, %d, %d\n", nDeltaBits, nCurBits, nBestBits); 
+    if(nCurBits < nBestBits)
+    {
+      nBestDeltaBits = nDeltaBits; 
+      nBestBits = nCurBits;
+    }
+  }
+
+  assert(nBestDeltaBits >=1 && nBestDeltaBits < 5);
+  pc3DAsymLUT->setDeltaBits(nBestDeltaBits); 
+}
+
+Void TEncCavlc::xTally3DAsymLUTOctantBits( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength, Int nDeltaBits, Int& nCurBits )
+{
+  UInt uiOctantSplit = nDepth < pc3DAsymLUT->getCurOctantDepth();
+  if( nDepth < pc3DAsymLUT->getCurOctantDepth() )
+    nCurBits ++; 
+  Int nYPartNum = 1 << pc3DAsymLUT->getCurYPartNumLog2();
+  if( uiOctantSplit )
+  {
+    Int nHalfLength = nLength >> 1;
+    for( Int l = 0 ; l < 2 ; l++ )
+    {
+      for( Int m = 0 ; m < 2 ; m++ )
+      {
+        for( Int n = 0 ; n < 2 ; n++ )
+        {
+          xTally3DAsymLUTOctantBits( pc3DAsymLUT , nDepth + 1 , yIdx + l * nHalfLength * nYPartNum , uIdx + m * nHalfLength , vIdx + n * nHalfLength , nHalfLength, nDeltaBits, nCurBits );
+        }
+      }
+    }
+  }
+  else
+  {
+    Int nFLCbits = pc3DAsymLUT->getMappingShift()-pc3DAsymLUT->getResQuantBit()-nDeltaBits ; 
+    nFLCbits = nFLCbits >= 0 ? nFLCbits:0;
+    //printf("nFLCbits = %d\n", nFLCbits);
+
+    for( Int l = 0 ; l < nYPartNum ; l++ )
+    {
+      for( Int nVertexIdx = 0 ; nVertexIdx < 4 ; nVertexIdx++ )
+      {
+        SYUVP sRes = pc3DAsymLUT->getCuboidVertexResTree( yIdx + l , uIdx , vIdx , nVertexIdx );
+
+        UInt uiCodeVertex = sRes.Y != 0 || sRes.U != 0 || sRes.V != 0;
+        nCurBits++;
+        if( uiCodeVertex )
+        {
+          xCheckParamBits( sRes.Y, nFLCbits, nCurBits );
+          xCheckParamBits( sRes.U, nFLCbits, nCurBits );
+          xCheckParamBits( sRes.V, nFLCbits, nCurBits );
+        }
+      }
+    }
+  }
+}
+
+Void TEncCavlc::xCheckParamBits( Int param, Int rParam, Int &nBits)
+{
+  Int codeNumber = abs(param);
+  Int codeQuotient = codeNumber >> rParam;
+  Int qLen; 
+
+  UInt uiLength = 1;
+  UInt uiTemp = ++codeQuotient;
+    
+  while( 1 != uiTemp )
+  {
+    uiTemp >>= 1;
+    uiLength += 2;
+  }
+
+  qLen  = (uiLength >> 1);
+  qLen += ((uiLength+1) >> 1);
+
+  nBits += qLen; 
+  nBits += rParam; 
+  if (abs(param))
+    nBits++; 
+}
+#endif
+
 #endif
 //! \}
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h	(revision 825)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h	(revision 826)
@@ -174,5 +174,12 @@
   Void xCode3DAsymLUTOctant( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength );
 #if R0151_CGS_3D_ASYMLUT_IMPROVE
+#if R0300_CGS_RES_COEFF_CODING
+  Void xWriteParam( Int param, UInt nFLCBits);
+  Void xCheckParamBits( Int param, Int nFLCBits, Int & nCurBits);
+  Void xTally3DAsymLUTOctantBits( TCom3DAsymLUT * pc3DAsymLUT , Int nDepth , Int yIdx , Int uIdx , Int vIdx , Int nLength, Int nDeltaBits, Int &nCurBits); 
+  Void xFindDeltaBits( TCom3DAsymLUT * pc3DAsymLUT );
+#else
   Void xWriteParam( Int param);
+#endif
 #endif
 #endif
