Index: trunk/source/Lib/TLibEncoder/TEncRateCtrl.cpp
===================================================================
--- trunk/source/Lib/TLibEncoder/TEncRateCtrl.cpp	(revision 1179)
+++ trunk/source/Lib/TLibEncoder/TEncRateCtrl.cpp	(revision 1313)
@@ -2,7 +2,7 @@
  * License, included below. This software may be subject to other third party
  * and contributor rights, including patent rights, and no such rights are
- * granted under this license.  
+ * granted under this license.
  *
-* Copyright (c) 2010-2015, ITU/ISO/IEC
+ * Copyright (c) 2010-2015, ITU/ISO/IEC
  * All rights reserved.
  *
@@ -37,4 +37,5 @@
 #include "TEncRateCtrl.h"
 #include "../TLibCommon/TComPic.h"
+#include "../TLibCommon/TComChromaFormat.h"
 
 #include <cmath>
@@ -116,4 +117,5 @@
     m_betaUpdate  = 0.2;
   }
+
   m_averageBits     = (Int)(m_targetBits / totalFrames);
   Int picWidthInBU  = ( m_picWidth  % m_LCUWidth  ) == 0 ? m_picWidth  / m_LCUWidth  : m_picWidth  / m_LCUWidth  + 1;
@@ -222,5 +224,5 @@
       else
       {
-        m_picPara[i].m_alpha = ALPHA;   
+        m_picPara[i].m_alpha = ALPHA;
         m_picPara[i].m_beta  = BETA2;
       }
@@ -402,5 +404,5 @@
   Double maxNumber = 10000.0;
   for ( Int i=0; i<g_RCIterationNum; i++ )
-  { 
+  {
     Double fx = 0.0;
     for ( Int j=0; j<GOPSize; j++ )
@@ -698,5 +700,5 @@
   if (eSliceType == I_SLICE)
   {
-    estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp); 
+    estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp);
   }
   else
@@ -704,5 +706,5 @@
     estLambda = alpha * pow( bpp, beta );
   }
-  
+
   Double lastLevelLambda = -1.0;
   Double lastPicLambda   = -1.0;
@@ -786,5 +788,5 @@
 Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures )
 {
-  Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); 
+  Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 );
 
   Int lastLevelQP = g_RCInvalidQPValue;
@@ -889,5 +891,5 @@
 
 
-Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType)  
+Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType)
 {
   Int   LCUIdx    = getLCUCoded();
@@ -914,11 +916,11 @@
   else
   {
-  Double totalWeight = 0;
-  for ( Int i=LCUIdx; i<m_numberOfLCU; i++ )
-  {
-    totalWeight += m_LCUs[i].m_bitWeight;
-  }
-  Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() );
-  avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 );
+    Double totalWeight = 0;
+    for ( Int i=LCUIdx; i<m_numberOfLCU; i++ )
+    {
+      totalWeight += m_LCUs[i].m_bitWeight;
+    }
+    Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() );
+    avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 );
   }
 
@@ -1073,5 +1075,5 @@
   //for Lambda clip, LCU level clip
   Double clipNeighbourLambda = -1.0;
-  for ( int i=LCUIdx - 1; i>=0; i-- )
+  for ( Int i=LCUIdx - 1; i>=0; i-- )
   {
     if ( m_LCUs[i].m_lambda > 0 )
@@ -1085,5 +1087,5 @@
   {
     estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda );
-  }  
+  }
 
   if ( clipPicLambda > 0.0 )
@@ -1111,5 +1113,5 @@
   //for Lambda clip, LCU level clip
   Int clipNeighbourQP = g_RCInvalidQPValue;
-  for ( int i=LCUIdx - 1; i>=0; i-- )
+  for ( Int i=LCUIdx - 1; i>=0; i-- )
   {
     if ( (getLCU(i)).m_QP > g_RCInvalidQPValue )
@@ -1130,5 +1132,5 @@
 }
 
-Void TEncRCPic::updateAfterLCU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter )
+Void TEncRCPic::updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter )
 {
   m_LCUs[LCUIdx].m_actualBits = bits;
@@ -1177,5 +1179,5 @@
   calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
   alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
-  double lnbpp = log( bpp );
+  Double lnbpp = log( bpp );
   lnbpp = Clip3( -5.0, -0.1, lnbpp );
   beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
@@ -1183,4 +1185,5 @@
   alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
   beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
+
   TRCParameter rcPara;
   rcPara.m_alpha = alpha;
@@ -1232,5 +1235,5 @@
   }
 
-  Double avgLambda; 
+  Double avgLambda;
   if( numTotalLCUs == 0 )
   {
@@ -1243,4 +1246,5 @@
   return avgLambda;
 }
+
 
 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType)
@@ -1267,4 +1271,5 @@
   Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
   Double beta  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
+
   if (eSliceType == I_SLICE)
   {
@@ -1273,33 +1278,35 @@
   else
   {
-  // update parameters
-  Double picActualBits = ( Double )m_picActualBits;
-  Double picActualBpp  = picActualBits/(Double)m_numberOfPixel;
-  Double calLambda     = alpha * pow( picActualBpp, beta );
-  Double inputLambda   = m_picLambda;
-
-  if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 )
-  {
-    alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
-    beta  *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
+    // update parameters
+    Double picActualBits = ( Double )m_picActualBits;
+    Double picActualBpp  = picActualBits/(Double)m_numberOfPixel;
+    Double calLambda     = alpha * pow( picActualBpp, beta );
+    Double inputLambda   = m_picLambda;
+
+    if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 )
+    {
+      alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
+      beta  *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
+
+      alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
+      beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
+
+      TRCParameter rcPara;
+      rcPara.m_alpha = alpha;
+      rcPara.m_beta  = beta;
+      m_encRCSeq->setPicPara( m_frameLevel, rcPara );
+
+      return;
+    }
+
+    calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
+    alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
+    Double lnbpp = log( picActualBpp );
+    lnbpp = Clip3( -5.0, -0.1, lnbpp );
+
+    beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
 
     alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
     beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
-    TRCParameter rcPara;
-    rcPara.m_alpha = alpha;
-    rcPara.m_beta  = beta;
-    m_encRCSeq->setPicPara( m_frameLevel, rcPara );
-
-    return;
-  }
-
-  calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
-  alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
-  double lnbpp = log( picActualBpp );
-  lnbpp = Clip3( -5.0, -0.1, lnbpp );
-  beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
-
-  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
-  beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
   }
 
@@ -1333,14 +1340,14 @@
 
   iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5);
-  
+
   return iIntraBits;
 }
 
-Double TEncRCPic::calculateLambdaIntra(double alpha, double beta, double MADPerPixel, double bitsPerPixel)
+Double TEncRCPic::calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel)
 {
   return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) );
 }
 
-Void TEncRCPic::updateAlphaBetaIntra(double *alpha, double *beta)
+Void TEncRCPic::updateAlphaBetaIntra(Double *alpha, Double *beta)
 {
   Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1));
@@ -1353,5 +1360,5 @@
 
 
-Void TEncRCPic::getLCUInitTargetBits()  
+Void TEncRCPic::getLCUInitTargetBits()
 {
   Int iAvgBits     = 0;
@@ -1366,5 +1373,5 @@
 
 
-Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP) 
+Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP)
 {
   Int   LCUIdx = getLCUCoded();
@@ -1378,5 +1385,5 @@
 
   Int clipNeighbourQP = g_RCInvalidQPValue;
-  for (int i=LCUIdx-1; i>=0; i--)
+  for (Int i=LCUIdx-1; i>=0; i--)
   {
     if ((getLCU(i)).m_QP > g_RCInvalidQPValue)
@@ -1392,6 +1399,6 @@
   if ( clipNeighbourQP > g_RCInvalidQPValue )
   {
-    maxQP = min(clipNeighbourQP + 1, maxQP); 
-    minQP = max(clipNeighbourQP - 1, minQP); 
+    maxQP = min(clipNeighbourQP + 1, maxQP);
+    minQP = max(clipNeighbourQP - 1, minQP);
   }
 
@@ -1515,4 +1522,5 @@
         bitsRatio[3] = 14;
       }
+
       if ( keepHierBits == 2 )
       {
@@ -1566,4 +1574,5 @@
         bitsRatio[7] = 1;
       }
+
       if ( keepHierBits == 2 )
       {
@@ -1577,6 +1586,6 @@
   }
 
-  Int* GOPID2Level = new int[ GOPSize ];
-  for ( int i=0; i<GOPSize; i++ )
+  Int* GOPID2Level = new Int[ GOPSize ];
+  for ( Int i=0; i<GOPSize; i++ )
   {
     GOPID2Level[i] = 1;
@@ -1586,4 +1595,5 @@
     }
   }
+
   if ( keepHierBits > 0 )
   {
@@ -1659,3 +1669,2 @@
   m_encRCGOP = NULL;
 }
-
