Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h	(revision 1432)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncCfg.h	(revision 1433)
@@ -339,4 +339,9 @@
   Int       m_RCInitialQP;
   Bool      m_RCForceIntraQP;
+#if U0132_TARGET_BITS_SATURATION
+  Bool      m_RCCpbSaturationEnabled;
+  UInt      m_RCCpbSize;
+  Double    m_RCInitialCpbFullness;
+#endif
   Bool      m_TransquantBypassEnableFlag;                     ///< transquant_bypass_enable_flag setting in PPS.
   Bool      m_CUTransquantBypassFlagForce;                    ///< if transquant_bypass_enable_flag, then, if true, all CU transquant bypass flags will be set to true.
@@ -936,4 +941,12 @@
   Bool         getForceIntraQP        ()                             { return m_RCForceIntraQP;        }
   Void         setForceIntraQP        ( Bool b )                     { m_RCForceIntraQP = b;           }
+#if U0132_TARGET_BITS_SATURATION
+  Bool         getCpbSaturationEnabled()                             { return m_RCCpbSaturationEnabled;}
+  Void         setCpbSaturationEnabled( Bool b )                     { m_RCCpbSaturationEnabled = b;   }
+  UInt         getCpbSize             ()                             { return m_RCCpbSize;}
+  Void         setCpbSize             ( UInt ui )                    { m_RCCpbSize = ui;   }
+  Double       getInitialCpbFullness  ()                             { return m_RCInitialCpbFullness;  }
+  Void         setInitialCpbFullness  (Double f)                     { m_RCInitialCpbFullness = f;     }
+#endif
   Bool         getTransquantBypassEnableFlag()                       { return m_TransquantBypassEnableFlag; }
   Void         setTransquantBypassEnableFlag(Bool flag)              { m_TransquantBypassEnableFlag = flag; }
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 1432)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 1433)
@@ -2432,4 +2432,26 @@
       estimatedBits = m_pcRateCtrl->getRCPic()->getTargetBits();
 
+#if U0132_TARGET_BITS_SATURATION
+      if (m_pcRateCtrl->getCpbSaturationEnabled() && frameLevel != 0)
+      {
+        Int estimatedCpbFullness = m_pcRateCtrl->getCpbState() + m_pcRateCtrl->getBufferingRate();
+
+        // prevent overflow
+        if (estimatedCpbFullness - estimatedBits > (Int)(m_pcRateCtrl->getCpbSize()*0.9f))
+        {
+          estimatedBits = estimatedCpbFullness - (Int)(m_pcRateCtrl->getCpbSize()*0.9f);
+        }
+
+        estimatedCpbFullness -= m_pcRateCtrl->getBufferingRate();
+        // prevent underflow
+        if (estimatedCpbFullness - estimatedBits < (Int)(m_pcRateCtrl->getCpbSize()*0.1f))
+        {
+          estimatedBits = max(200, estimatedCpbFullness - (Int)(m_pcRateCtrl->getCpbSize()*0.1f));
+        }
+
+        m_pcRateCtrl->getRCPic()->setTargetBits(estimatedBits);
+      }
+#endif
+
       Int sliceQP = m_pcCfg->getInitialQP();
 #if SVC_EXTENSION
@@ -2455,4 +2477,25 @@
           Int bits = m_pcRateCtrl->getRCSeq()->getLeftAverageBits();
           bits = m_pcRateCtrl->getRCPic()->getRefineBitsForIntra( bits );
+
+#if U0132_TARGET_BITS_SATURATION
+          if (m_pcRateCtrl->getCpbSaturationEnabled() )
+          {
+            Int estimatedCpbFullness = m_pcRateCtrl->getCpbState() + m_pcRateCtrl->getBufferingRate();
+
+            // prevent overflow
+            if (estimatedCpbFullness - bits > (Int)(m_pcRateCtrl->getCpbSize()*0.9f))
+            {
+              bits = estimatedCpbFullness - (Int)(m_pcRateCtrl->getCpbSize()*0.9f);
+            }
+
+            estimatedCpbFullness -= m_pcRateCtrl->getBufferingRate();
+            // prevent underflow
+            if (estimatedCpbFullness - bits < (Int)(m_pcRateCtrl->getCpbSize()*0.1f))
+            {
+              bits = estimatedCpbFullness - (Int)(m_pcRateCtrl->getCpbSize()*0.1f);
+            }
+          }
+#endif
+
           if ( bits < 200 )
           {
@@ -2846,4 +2889,11 @@
         m_pcRateCtrl->getRCGOP()->updateAfterPicture( estimatedBits );
       }
+#if U0132_TARGET_BITS_SATURATION
+      if (m_pcRateCtrl->getCpbSaturationEnabled())
+      {
+        m_pcRateCtrl->updateCpbState(actualTotalBits);
+        printf(" [CPB %6d bits]", m_pcRateCtrl->getCpbState());
+      }
+#endif
     }
 
@@ -4256,5 +4306,5 @@
 #endif
 #endif
-Void TEncGOP::xFilterImg( Pel *src, Int iSrcStride, Pel *dst, Int iDstStride, Int height1, Int width1, BitDepths& bitDepth, ComponentID comp )
+Void TEncGOP::xFilterImg( Pel *src, Int iSrcStride, Pel *dst, Int iDstStride, Int height, Int width, BitDepths& bitDepth, ComponentID comp )
 {
   Int height2, width2;
@@ -4264,14 +4314,14 @@
   Int length = m_cgsFilterLength;
   Int shift  = bitDepth.recon[CHANNEL_TYPE_LUMA] - bitDepth.recon[CHANNEL_TYPE_CHROMA];
-  Int shift2 = 2*7+shift;
+  Int shift2 = 2*7 + shift;
   Int roundingOffset = (1 << (shift2 - 1));
   Int maxVal = (1<<(bitDepth.recon[toChannelType(comp)]-shift))-1;
-  height2 = (height1 * m_cgsFilterPhases) / m_iN;
-  width2  = (width1  * m_cgsFilterPhases) / m_iN;
+  height2 = (height * m_cgsFilterPhases) / m_iN;
+  width2  = (width  * m_cgsFilterPhases) / m_iN;
 
   m_phaseFilter = comp == COMPONENT_Y ? m_phaseFilterLuma : m_phaseFilterChroma;
 
   // horizontal filtering
-  for( Int j1 = 0; j1 < height1; j1++ )
+  for( Int j1 = 0; j1 < height; j1++ )
   {
     Int i0 = -m_iN;
@@ -4302,5 +4352,5 @@
   }
 
-  for( Int k = height1; k < (height1 + (length>>1)); k++)
+  for( Int k = height; k < (height + (length>>1)); k++)
   {
     memcpy(m_temp[k], m_temp[k-1], (width2) * sizeof(Int));
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.h	(revision 1432)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.h	(revision 1433)
@@ -292,5 +292,5 @@
   inline Short xClip( Short x, Int bitdepth );
   Void xInitDs( const Int iWidth, const Int iHeight, const Bool allIntra, const Int posScalingFactorX);
-  Void xFilterImg( Pel *src, Int iSrcStride, Pel *dst, Int iDstStride, Int height1, Int width1, BitDepths& bitDepth, ComponentID comp );
+  Void xFilterImg( Pel *src, Int iSrcStride, Pel *dst, Int iDstStride, Int height, Int width, BitDepths& bitDepth, ComponentID comp );
 
   Int  xCreate2DArray(Int ***array2D, Int dim0, Int dim1, Int iPadY, Int iPadX);
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncRateCtrl.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncRateCtrl.cpp	(revision 1432)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncRateCtrl.cpp	(revision 1433)
@@ -1379,4 +1379,10 @@
     m_encRCSeq->initLCUPara();
   }
+#if U0132_TARGET_BITS_SATURATION
+  m_CpbSaturationEnabled = false;
+  m_cpbSize              = targetBitrate;
+  m_cpbState             = (UInt)(m_cpbSize*0.5f);
+  m_bufferingRate        = (Int)(targetBitrate / frameRate);
+#endif
 
   delete[] bitsRatio;
@@ -1396,4 +1402,34 @@
 }
 
+#if U0132_TARGET_BITS_SATURATION
+Int  TEncRateCtrl::updateCpbState(Int actualBits)
+{
+  Int cpbState = 1;
+
+  m_cpbState -= actualBits;
+  if (m_cpbState < 0)
+  {
+    cpbState = -1;
+  }
+
+  m_cpbState += m_bufferingRate;
+  if (m_cpbState > m_cpbSize)
+  {
+    cpbState = 0;
+  }
+
+  return cpbState;
+}
+
+Void TEncRateCtrl::initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness)
+{
+  m_CpbSaturationEnabled = true;
+  m_cpbSize = (pcHrd->getCpbSizeValueMinus1(0, 0, 0) + 1) << (4 + pcHrd->getCpbSizeScale());
+  m_cpbState = (UInt)(m_cpbSize*fInitialCpbFullness);
+  m_bufferingRate = (UInt)(((pcHrd->getBitRateValueMinus1(0, 0, 0) + 1) << (6 + pcHrd->getBitRateScale())) / iFrameRate);
+  printf("\nHRD - [Initial CPB state %6d] [CPB Size %6d] [Buffering Rate %6d]\n", m_cpbState, m_cpbSize, m_bufferingRate);
+}
+#endif
+
 Void TEncRateCtrl::destroyRCGOP()
 {
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncRateCtrl.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncRateCtrl.h	(revision 1432)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncRateCtrl.h	(revision 1433)
@@ -265,4 +265,7 @@
   TRCLCU& getLCU( Int LCUIdx )                            { return m_LCUs[LCUIdx]; }
   Int  getPicActualHeaderBits()                           { return m_picActualHeaderBits; }
+#if U0132_TARGET_BITS_SATURATION
+  Void setBitLeft(Int bits)                               { m_bitsLeft = bits; }
+#endif
   Void setTargetBits( Int bits )                          { m_targetBits = bits; m_bitsLeft = bits;}
   Void setTotalIntraCost(Double cost)                     { m_totalCostIntra = cost; }
@@ -322,4 +325,12 @@
   TEncRCPic* getRCPic()          { assert ( m_encRCPic != NULL ); return m_encRCPic; }
   list<TEncRCPic*>& getPicList() { return m_listRCPictures; }
+#if U0132_TARGET_BITS_SATURATION
+  Bool       getCpbSaturationEnabled()  { return m_CpbSaturationEnabled;  }
+  UInt       getCpbState()              { return m_cpbState;       }
+  UInt       getCpbSize()               { return m_cpbSize;        }
+  UInt       getBufferingRate()         { return m_bufferingRate;  }
+  Int        updateCpbState(Int actualBits);
+  Void       initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness);
+#endif
 
 private:
@@ -329,7 +340,13 @@
   list<TEncRCPic*> m_listRCPictures;
   Int        m_RCQP;
-};
-
+#if U0132_TARGET_BITS_SATURATION
+  Bool       m_CpbSaturationEnabled;    // Enable target bits saturation to avoid CPB overflow and underflow
+  Int        m_cpbState;                // CPB State 
+  UInt       m_cpbSize;                 // CPB size
+  UInt       m_bufferingRate;           // Buffering rate
 #endif
-
-
+};
+
+#endif
+
+
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncTop.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncTop.cpp	(revision 1432)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncTop.cpp	(revision 1433)
@@ -228,4 +228,10 @@
   xInitVPS();
 
+#if U0132_TARGET_BITS_SATURATION
+  if (m_RCCpbSaturationEnabled)
+  {
+    m_cRateCtrl.initHrdParam(m_cSPS.getVuiParameters()->getHrdParameters(), m_iFrameRate, m_RCInitialCpbFullness);
+  }
+#endif
   m_cRdCost.setCostMode(m_costMode);
 
@@ -1002,5 +1008,10 @@
     m_cSPS.setUsedByCurrPicLtSPSFlag(k, 0);
   }
+
+#if U0132_TARGET_BITS_SATURATION
+  if( getPictureTimingSEIEnabled() || getDecodingUnitInfoSEIEnabled() || getCpbSaturationEnabled() )
+#else
   if( getPictureTimingSEIEnabled() || getDecodingUnitInfoSEIEnabled() )
+#endif
   {
     xInitHrdParameters();
@@ -1025,4 +1036,20 @@
 }
 
+#if U0132_TARGET_BITS_SATURATION
+// calculate scale value of bitrate and initial delay
+Int calcScale(Int x)
+{
+  UInt iMask = 0xffffffff;
+  Int ScaleValue = 32;
+
+  while ((x&iMask) != 0)
+  {
+    ScaleValue--;
+    iMask = (iMask >> 1);
+  }
+
+  return ScaleValue;
+}
+#endif
 Void TEncTop::xInitHrdParameters()
 {
@@ -1030,6 +1057,11 @@
   Int  bitRate         = getTargetBitrate();
   Bool isRandomAccess  = getIntraPeriod() > 0;
-
+# if U0132_TARGET_BITS_SATURATION
+  Int cpbSize          = getCpbSize();
+
+  if( !getVuiParametersPresentFlag() && !getCpbSaturationEnabled() )
+#else
   if( !getVuiParametersPresentFlag() )
+#endif
   {
     return;
@@ -1075,5 +1107,4 @@
   hrd->setNalHrdParametersPresentFlag( rateCnt );
   hrd->setVclHrdParametersPresentFlag( rateCnt );
-
   hrd->setSubPicCpbParamsPresentFlag( useSubCpbParams );
 
@@ -1090,6 +1121,27 @@
   }
 
+#if U0132_TARGET_BITS_SATURATION
+  if (calcScale(bitRate) <= 6)
+  {
+    hrd->setBitRateScale(0);
+  }
+  else
+  {
+    hrd->setBitRateScale(calcScale(bitRate) - 6);
+  }
+
+  if (calcScale(cpbSize) <= 4)
+  {
+    hrd->setCpbSizeScale(0);
+  }
+  else
+  {
+    hrd->setCpbSizeScale(calcScale(cpbSize) - 4);
+  }
+#else
   hrd->setBitRateScale( 4 );                                       // in units of 2^( 6 + 4 ) = 1,024 bps
   hrd->setCpbSizeScale( 6 );                                       // in units of 2^( 4 + 6 ) = 1,024 bit
+#endif
+
   hrd->setDuCpbSizeScale( 6 );                                     // in units of 2^( 4 + 6 ) = 1,024 bit
 
@@ -1123,5 +1175,10 @@
     bitrateValue = bitRate / (1 << (6 + hrd->getBitRateScale()) );      // bitRate is in bits, so it needs to be scaled down
     // CpbSize[ i ] = ( cpb_size_value_minus1[ i ] + 1 ) * 2^( 4 + cpb_size_scale )
+#if U0132_TARGET_BITS_SATURATION
+    cpbSizeValue = cpbSize / (1 << (4 + hrd->getCpbSizeScale()) );      // using bitRate results in 1 second CPB size
+#else
     cpbSizeValue = bitRate / (1 << (4 + hrd->getCpbSizeScale()) );      // using bitRate results in 1 second CPB size
+#endif
+
 
     // DU CPB size could be smaller (i.e. bitrateValue / number of DUs), but we don't know 
