Ignore:
Timestamp:
2 Jan 2014, 07:21:30 (11 years ago)
Author:
seregin
Message:

update to HM-12.1 base

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/SHM-4.1-dev/source/Lib/TLibEncoder/TEncRateCtrl.cpp

    r313 r532  
    4242using namespace std;
    4343
    44 #if RATE_CONTROL_LAMBDA_DOMAIN
    45 
    4644//sequence level
    4745TEncRCSeq::TEncRCSeq()
     
    6765  m_bitsLeft            = 0;
    6866  m_useLCUSeparateModel = false;
    69 #if M0036_RC_IMPROVEMENT
    7067  m_adaptiveBit         = 0;
    7168  m_lastLambda          = 0.0;
    72 #endif
    7369}
    7470
     
    7874}
    7975
    80 #if M0036_RC_IMPROVEMENT
    8176Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit )
    82 #else
    83 Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel )
    84 #endif
    8577{
    8678  destroy();
     
    109101    m_betaUpdate  = 0.025;
    110102  }
    111 #if M0036_RC_IMPROVEMENT
    112103  else if ( m_seqTargetBpp < 0.2 )
    113104  {
     
    125116    m_betaUpdate  = 0.2;
    126117  }
    127 #else
    128   else
    129   {
    130     m_alphaUpdate = 0.1;
    131     m_betaUpdate  = 0.05;
    132   }
    133 #endif
    134118  m_averageBits     = (Int)(m_targetBits / totalFrames);
    135119  Int picWidthInBU  = ( m_picWidth  % m_LCUWidth  ) == 0 ? m_picWidth  / m_LCUWidth  : m_picWidth  / m_LCUWidth  + 1;
     
    172156  m_framesLeft = m_totalFrames;
    173157  m_bitsLeft   = m_targetBits;
    174 #if M0036_RC_IMPROVEMENT
    175158  m_adaptiveBit = adaptiveBit;
    176159  m_lastLambda = 0.0;
    177 #endif
    178160}
    179161
     
    233215    for ( Int i=0; i<m_numberOfLevel; i++ )
    234216    {
    235 #if RATE_CONTROL_INTRA
    236217      if (i>0)
    237218      {
     
    244225        m_picPara[i].m_beta  = BETA2;
    245226      }
    246 #else
    247       m_picPara[i].m_alpha = 3.2003;
    248       m_picPara[i].m_beta  = -1.367;
    249 #endif
    250227    }
    251228  }
     
    271248      for ( Int j=0; j<m_numberOfLCU; j++)
    272249      {
    273 #if RATE_CONTROL_INTRA
    274250        m_LCUPara[i][j].m_alpha = m_picPara[i].m_alpha;
    275251        m_LCUPara[i][j].m_beta  = m_picPara[i].m_beta;
    276 #else
    277         m_LCUPara[i][j].m_alpha = 3.2003;
    278         m_LCUPara[i][j].m_beta  = -1.367;
    279 #endif
    280252      }
    281253    }
     
    299271}
    300272
    301 #if !RATE_CONTROL_INTRA
    302 Int TEncRCSeq::getRefineBitsForIntra( Int orgBits )
    303 {
    304   Double bpp = ( (Double)orgBits ) / m_picHeight / m_picHeight;
    305   if ( bpp > 0.2 )
    306   {
    307     return orgBits * 5;
    308   }
    309   if ( bpp > 0.1 )
    310   {
    311     return orgBits * 7;
    312   }
    313   return orgBits * 10;
    314 }
    315 #endif
    316 
    317 #if M0036_RC_IMPROVEMENT
    318273Void TEncRCSeq::setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB )
    319274{
     
    326281  delete[] bitsRatio;
    327282}
    328 #endif
    329283
    330284//GOP level
     
    349303  Int targetBits = xEstGOPTargetBits( encRCSeq, numPic );
    350304
    351 #if M0036_RC_IMPROVEMENT
    352305  if ( encRCSeq->getAdaptiveBits() > 0 && encRCSeq->getLastLambda() > 0.1 )
    353306  {
     
    409362    delete []equaCoeffB;
    410363  }
    411 #endif
    412364
    413365  m_picTargetBitInGOP = new Int[numPic];
     
    422374  {
    423375    currPicRatio = encRCSeq->getBitRatio( i );
    424 #if M0036_RC_IMPROVEMENT
    425376    m_picTargetBitInGOP[i] = (Int)( ((Double)targetBits) * currPicRatio / totalPicRatio );
    426 #else
    427     m_picTargetBitInGOP[i] = targetBits * currPicRatio / totalPicRatio;
    428 #endif
    429377  }
    430378
     
    436384}
    437385
    438 #if M0036_RC_IMPROVEMENT
    439386Void TEncRCGOP::xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize )
    440387{
     
    482429  return solution;
    483430}
    484 #endif
    485431
    486432Void TEncRCGOP::destroy()
     
    534480
    535481  m_LCUs         = NULL;
    536 #if !M0036_RC_IMPROVEMENT
    537   m_lastPicture  = NULL;
    538 #endif
    539482  m_picActualHeaderBits = 0;
    540 #if !M0036_RC_IMPROVEMENT
    541   m_totalMAD            = 0.0;
    542 #endif
    543483  m_picActualBits       = 0;
    544484  m_picQP               = 0;
     
    565505  }
    566506
    567 #if M0036_RC_IMPROVEMENT
    568507  targetBits  = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio );
    569 #else
    570   targetBits  = Int( GOPbitsLeft * currPicRatio / totalPicRatio );
    571 #endif
    572508
    573509  if ( targetBits < 100 )
     
    665601      m_LCUs[LCUIdx].m_lambda     = 0.0;
    666602      m_LCUs[LCUIdx].m_targetBits = 0;
    667 #if M0036_RC_IMPROVEMENT
    668603      m_LCUs[LCUIdx].m_bitWeight  = 1.0;
    669 #else
    670       m_LCUs[LCUIdx].m_MAD        = 0.0;
    671 #endif
    672604      Int currWidth  = ( (i == picWidthInLCU -1) ? picWidth  - LCUWidth *(picWidthInLCU -1) : LCUWidth  );
    673605      Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight );
     
    676608  }
    677609  m_picActualHeaderBits = 0;
    678 #if !M0036_RC_IMPROVEMENT
    679   m_totalMAD            = 0.0;
    680 #endif
    681610  m_picActualBits       = 0;
    682611  m_picQP               = 0;
    683612  m_picLambda           = 0.0;
    684 
    685 #if !M0036_RC_IMPROVEMENT
    686   m_lastPicture = NULL;
    687   list<TEncRCPic*>::reverse_iterator it;
    688   for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ )
    689   {
    690     if ( (*it)->getFrameLevel() == m_frameLevel )
    691     {
    692       m_lastPicture = (*it);
    693       break;
    694     }
    695   }
    696 #endif
    697613}
    698614
     
    709625
    710626
    711 #if RATE_CONTROL_INTRA
    712627Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType)
    713 #else
    714 Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures )
    715 #endif
    716628{
    717629  Double alpha         = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
    718630  Double beta          = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
    719631  Double bpp       = (Double)m_targetBits/(Double)m_numberOfPixel;
    720 #if RATE_CONTROL_INTRA
    721632  Double estLambda;
    722633  if (eSliceType == I_SLICE)
     
    728639    estLambda = alpha * pow( bpp, beta );
    729640  }
    730 #else
    731   Double estLambda = alpha * pow( bpp, beta );
    732 #endif 
    733641 
    734642  Double lastLevelLambda = -1.0;
     
    778686  m_estPicLambda = estLambda;
    779687
    780 #if M0036_RC_IMPROVEMENT
    781688  Double totalWeight = 0.0;
    782689  // initial BU bit allocation weight
    783690  for ( Int i=0; i<m_numberOfLCU; i++ )
    784691  {
    785 #if RC_FIX
    786692    Double alphaLCU, betaLCU;
    787693    if ( m_encRCSeq->getUseLCUSeparateModel() )
     
    795701      betaLCU  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
    796702    }
    797 #else
    798     Double alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha;
    799     Double betaLCU  = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta;
    800 #endif
    801703
    802704    m_LCUs[i].m_bitWeight =  m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU );
     
    813715    m_LCUs[i].m_bitWeight = BUTargetBits;
    814716  }
    815 #endif
    816717
    817718  return estLambda;
     
    856757}
    857758
    858 #if RATE_CONTROL_INTRA
    859759Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) 
    860 #else
    861 Double TEncRCPic::getLCUTargetBpp()
    862 #endif
    863760{
    864761  Int   LCUIdx    = getLCUCoded();
    865762  Double bpp      = -1.0;
    866763  Int avgBits     = 0;
    867 #if !M0036_RC_IMPROVEMENT
    868   Double totalMAD = -1.0;
    869   Double MAD      = -1.0;
    870 #endif
    871 
    872 #if RATE_CONTROL_INTRA
    873   if (eSliceType == I_SLICE){
     764
     765  if (eSliceType == I_SLICE)
     766  {
    874767    Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1;
    875768    Int bitrateWindow = min(4,noOfLCUsLeft);
     
    889782  else
    890783  {
    891 #endif
    892 #if M0036_RC_IMPROVEMENT
    893784  Double totalWeight = 0;
    894785  for ( Int i=LCUIdx; i<m_numberOfLCU; i++ )
     
    898789  Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() );
    899790  avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 );
    900 #else
    901   if ( m_lastPicture == NULL )
    902   {
    903     avgBits = Int( m_bitsLeft / m_LCULeft );
    904   }
    905   else
    906   {
    907     MAD = m_lastPicture->getLCU(LCUIdx).m_MAD;
    908     totalMAD = m_lastPicture->getTotalMAD();
    909     for ( Int i=0; i<LCUIdx; i++ )
    910     {
    911       totalMAD -= m_lastPicture->getLCU(i).m_MAD;
    912     }
    913 
    914     if ( totalMAD > 0.1 )
    915     {
    916       avgBits = Int( m_bitsLeft * MAD / totalMAD );
    917     }
    918     else
    919     {
    920       avgBits = Int( m_bitsLeft / m_LCULeft );
    921     }
    922   }
    923 #endif
    924 #if RATE_CONTROL_INTRA
    925   }
    926 #endif
     791    }
    927792
    928793  if ( avgBits < 1 )
     
    1050915    beta  *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
    1051916
    1052 #if M0036_RC_IMPROVEMENT
    1053917    alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
    1054918    beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
    1055 #else
    1056     alpha = Clip3( 0.05, 20.0, alpha );
    1057     beta  = Clip3( -3.0, -0.1, beta  );
    1058 #endif
    1059919
    1060920    TRCParameter rcPara;
     
    1069929  alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
    1070930  double lnbpp = log( bpp );
    1071 #if M0036_RC_IMPROVEMENT
    1072931  lnbpp = Clip3( -5.0, -0.1, lnbpp );
    1073 #else
    1074   lnbpp = Clip3( -5.0, 1.0, lnbpp );
    1075 #endif
    1076932  beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
    1077933
    1078 #if M0036_RC_IMPROVEMENT
    1079934  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
    1080935  beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
    1081 #else
    1082   alpha = Clip3( 0.05, 20.0, alpha );
    1083   beta  = Clip3( -3.0, -0.1, beta  );
    1084 #endif
    1085936  TRCParameter rcPara;
    1086937  rcPara.m_alpha = alpha;
     
    1090941}
    1091942
    1092 #if !M0036_RC_IMPROVEMENT
    1093 Double TEncRCPic::getEffectivePercentage()
    1094 {
    1095   Int effectivePiexels = 0;
    1096   Int totalPixels = 0;
    1097 
    1098   for ( Int i=0; i<m_numberOfLCU; i++ )
    1099   {
    1100     totalPixels += m_LCUs[i].m_numberOfPixel;
    1101     if ( m_LCUs[i].m_QP > 0 )
    1102     {
    1103       effectivePiexels += m_LCUs[i].m_numberOfPixel;
    1104     }
    1105   }
    1106 
    1107   Double effectivePixelPercentage = (Double)effectivePiexels/(Double)totalPixels;
    1108   return effectivePixelPercentage;
    1109 }
    1110 #endif
    1111 
    1112943Double TEncRCPic::calAverageQP()
    1113944{
     
    1164995}
    1165996
    1166 #if M0036_RC_IMPROVEMENT
    1167 #if RATE_CONTROL_INTRA
    1168997Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType)
    1169 #else
    1170 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda )
    1171 #endif
    1172 #else
    1173 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage )
    1174 #endif
    1175998{
    1176999  m_picActualHeaderBits = actualHeaderBits;
     
    11851008  }
    11861009  m_picLambda           = averageLambda;
    1187 #if !M0036_RC_IMPROVEMENT
    1188   for ( Int i=0; i<m_numberOfLCU; i++ )
    1189   {
    1190     m_totalMAD += m_LCUs[i].m_MAD;
    1191   }
    1192 #endif
    11931010
    11941011  Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
    11951012  Double beta  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
    1196 #if RATE_CONTROL_INTRA
    11971013  if (eSliceType == I_SLICE)
    11981014  {
     
    12011017  else
    12021018  {
    1203 #endif
    12041019  // update parameters
    12051020  Double picActualBits = ( Double )m_picActualBits;
     
    12081023  Double inputLambda   = m_picLambda;
    12091024
    1210 #if M0036_RC_IMPROVEMENT
    12111025  if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 )
    1212 #else
    1213   if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 || effectivePercentage < 0.05 )
    1214 #endif
    12151026  {
    12161027    alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
    12171028    beta  *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
    12181029
    1219 #if M0036_RC_IMPROVEMENT
    12201030    alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
    12211031    beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
    1222 #else
    1223     alpha = Clip3( 0.05, 20.0, alpha );
    1224     beta  = Clip3( -3.0, -0.1, beta  );
    1225 #endif
    12261032    TRCParameter rcPara;
    12271033    rcPara.m_alpha = alpha;
     
    12351041  alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
    12361042  double lnbpp = log( picActualBpp );
    1237 #if M0036_RC_IMPROVEMENT
    12381043  lnbpp = Clip3( -5.0, -0.1, lnbpp );
    1239 #else
    1240   lnbpp = Clip3( -5.0, 1.0, lnbpp );
    1241 #endif
    12421044  beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
    12431045
    1244 #if M0036_RC_IMPROVEMENT
    12451046  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
    12461047  beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
    1247 #else
    1248   alpha = Clip3( 0.05, 20.0, alpha );
    1249   beta  = Clip3( -3.0, -0.1, beta  );
    1250 #endif
    1251 #if RATE_CONTROL_INTRA
    1252   }
    1253 #endif
     1048  }
    12541049
    12551050  TRCParameter rcPara;
     
    12591054  m_encRCSeq->setPicPara( m_frameLevel, rcPara );
    12601055
    1261 #if M0036_RC_IMPROVEMENT
    12621056  if ( m_frameLevel == 1 )
    12631057  {
     
    12661060    m_encRCSeq->setLastLambda( updateLastLambda );
    12671061  }
    1268 #endif
    1269 }
    1270 
    1271 #if RATE_CONTROL_INTRA
     1062}
     1063
    12721064Int TEncRCPic::getRefineBitsForIntra( Int orgBits )
    12731065{
     
    13581150  return estLambda;
    13591151}
    1360 #endif
    13611152
    13621153TEncRateCtrl::TEncRateCtrl()
     
    13921183}
    13931184
    1394 #if M0036_RC_IMPROVEMENT
    13951185Void TEncRateCtrl::init( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int keepHierBits, Bool useLCUSeparateModel, GOPEntry  GOPList[MAX_GOP] )
    1396 #else
    1397 Void TEncRateCtrl::init( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Bool keepHierBits, Bool useLCUSeparateModel, GOPEntry  GOPList[MAX_GOP] )
    1398 #endif
    13991186{
    14001187  destroy();
     
    14111198
    14121199  Int numberOfLevel = 1;
    1413 #if M0036_RC_IMPROVEMENT
    14141200  Int adaptiveBit = 0;
    14151201  if ( keepHierBits > 0 )
    1416 #else
    1417   if ( keepHierBits )
    1418 #endif
    14191202  {
    14201203    numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1;
     
    14391222  }
    14401223
    1441 #if M0036_RC_IMPROVEMENT
    14421224  if ( keepHierBits > 0 )
    1443 #else
    1444   if ( keepHierBits )
    1445 #endif
    14461225  {
    14471226    Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) );
     
    14761255        bitsRatio[3] = 14;
    14771256      }
    1478 #if M0036_RC_IMPROVEMENT
    14791257      if ( keepHierBits == 2 )
    14801258      {
    14811259        adaptiveBit = 1;
    14821260      }
    1483 #endif
    14841261    }
    14851262    else if ( GOPSize == 8 && !isLowdelay )
     
    15291306        bitsRatio[7] = 1;
    15301307      }
    1531 #if M0036_RC_IMPROVEMENT
    15321308      if ( keepHierBits == 2 )
    15331309      {
    15341310        adaptiveBit = 2;
    15351311      }
    1536 #endif
    15371312    }
    15381313    else
    15391314    {
    1540 #if M0036_RC_IMPROVEMENT
    15411315      printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" );
    1542 #else
    1543       printf( "\n hierarchical bit allocation is not support for the specified coding structure currently." );
    1544 #endif
    15451316    }
    15461317  }
     
    15551326    }
    15561327  }
    1557 #if M0036_RC_IMPROVEMENT
    15581328  if ( keepHierBits > 0 )
    1559 #else
    1560   if ( keepHierBits )
    1561 #endif
    15621329  {
    15631330    if ( GOPSize == 4 && isLowdelay )
     
    15941361
    15951362  m_encRCSeq = new TEncRCSeq;
    1596 #if M0036_RC_IMPROVEMENT
    15971363  m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit );
    1598 #else
    1599   m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel );
    1600 #endif
    16011364  m_encRCSeq->initBitsRatio( bitsRatio );
    16021365  m_encRCSeq->initGOPID2Level( GOPID2Level );
     
    16291392}
    16301393
    1631 #else
    1632 
    1633 #define ADJUSTMENT_FACTOR       0.60
    1634 #define HIGH_QSTEP_THRESHOLD    9.5238
    1635 #define HIGH_QSTEP_ALPHA        4.9371
    1636 #define HIGH_QSTEP_BETA         0.0922
    1637 #define LOW_QSTEP_ALPHA         16.7429
    1638 #define LOW_QSTEP_BETA          -1.1494
    1639 
    1640 #define MAD_PRED_Y1             1.0
    1641 #define MAD_PRED_Y2             0.0
    1642 
    1643 enum MAD_HISOTRY {
    1644   MAD_PPPrevious = 0,
    1645   MAD_PPrevious  = 1,
    1646   MAD_Previous   = 2
    1647 };
    1648 
    1649 Void    MADLinearModel::initMADLinearModel()
    1650 {
    1651   m_activeOn = false;
    1652   m_paramY1  = 1.0;
    1653   m_paramY2  = 0.0;
    1654   m_costMADs[0] = m_costMADs[1] = m_costMADs[2] = 0.0;
    1655 }
    1656 
    1657 Double  MADLinearModel::getMAD()
    1658 {
    1659   Double costPredMAD = m_paramY1 * m_costMADs[MAD_Previous] + m_paramY2;
    1660 
    1661   if(costPredMAD < 0)
    1662   {
    1663     costPredMAD = m_costMADs[MAD_Previous];
    1664     m_paramY1   = MAD_PRED_Y1;
    1665     m_paramY2   = MAD_PRED_Y2;
    1666   }
    1667   return costPredMAD;
    1668 }
    1669 
    1670 Void    MADLinearModel::updateMADLiearModel()
    1671 {
    1672   Double dNewY1 = ((m_costMADs[MAD_Previous] - m_costMADs[MAD_PPrevious]) / (m_costMADs[MAD_PPrevious] - m_costMADs[MAD_PPPrevious]));
    1673   Double dNewY2 =  (m_costMADs[MAD_Previous] - (dNewY1*m_costMADs[MAD_PPrevious]));
    1674  
    1675   m_paramY1 = 0.70+0.20*m_paramY1+ 0.10*dNewY1;
    1676   m_paramY2 =      0.20*m_paramY2+ 0.10*dNewY2;
    1677 }
    1678 
    1679 Void    MADLinearModel::updateMADHistory(Double dMAD)
    1680 {
    1681   m_costMADs[MAD_PPPrevious] = m_costMADs[MAD_PPrevious];
    1682   m_costMADs[MAD_PPrevious ] = m_costMADs[MAD_Previous ];
    1683   m_costMADs[MAD_Previous  ] = dMAD;
    1684   m_activeOn = (m_costMADs[MAD_Previous  ] && m_costMADs[MAD_PPrevious ] && m_costMADs[MAD_PPPrevious]);
    1685 }
    1686 
    1687 
    1688 Void    PixelBaseURQQuadraticModel::initPixelBaseQuadraticModel()
    1689 {
    1690   m_paramHighX1 = HIGH_QSTEP_ALPHA;
    1691   m_paramHighX2 = HIGH_QSTEP_BETA;
    1692   m_paramLowX1  = LOW_QSTEP_ALPHA;
    1693   m_paramLowX2  = LOW_QSTEP_BETA;
    1694 }
    1695 
    1696 Int     PixelBaseURQQuadraticModel::getQP(Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD)
    1697 {
    1698   Double qStep;
    1699   Double bppPerMAD = (Double)(targetBits/(numberOfPixels*costPredMAD));
    1700  
    1701   if(xConvertQP2QStep(qp) >= HIGH_QSTEP_THRESHOLD)
    1702   {
    1703     qStep = 1/( sqrt((bppPerMAD/m_paramHighX1)+((m_paramHighX2*m_paramHighX2)/(4*m_paramHighX1*m_paramHighX1))) - (m_paramHighX2/(2*m_paramHighX1)));
    1704   }
    1705   else
    1706   {
    1707     qStep = 1/( sqrt((bppPerMAD/m_paramLowX1)+((m_paramLowX2*m_paramLowX2)/(4*m_paramLowX1*m_paramLowX1))) - (m_paramLowX2/(2*m_paramLowX1)));
    1708   }
    1709  
    1710   return xConvertQStep2QP(qStep);
    1711 }
    1712 
    1713 Void    PixelBaseURQQuadraticModel::updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD)
    1714 {
    1715   Double qStep     = xConvertQP2QStep(qp);
    1716   Double invqStep = (1/qStep);
    1717   Double paramNewX1, paramNewX2;
    1718  
    1719   if(qStep >= HIGH_QSTEP_THRESHOLD)
    1720   {
    1721     paramNewX2    = (((bits/(numberOfPixels*costMAD))-(23.3772*invqStep*invqStep))/((1-200*invqStep)*invqStep));
    1722     paramNewX1    = (23.3772-200*paramNewX2);
    1723     m_paramHighX1 = 0.70*HIGH_QSTEP_ALPHA + 0.20 * m_paramHighX1 + 0.10 * paramNewX1;
    1724     m_paramHighX2 = 0.70*HIGH_QSTEP_BETA  + 0.20 * m_paramHighX2 + 0.10 * paramNewX2;
    1725   }
    1726   else
    1727   {
    1728     paramNewX2   = (((bits/(numberOfPixels*costMAD))-(5.8091*invqStep*invqStep))/((1-9.5455*invqStep)*invqStep));
    1729     paramNewX1   = (5.8091-9.5455*paramNewX2);
    1730     m_paramLowX1 = 0.90*LOW_QSTEP_ALPHA + 0.09 * m_paramLowX1 + 0.01 * paramNewX1;
    1731     m_paramLowX2 = 0.90*LOW_QSTEP_BETA  + 0.09 * m_paramLowX2 + 0.01 * paramNewX2;
    1732   }
    1733 }
    1734 
    1735 Bool    PixelBaseURQQuadraticModel::checkUpdateAvailable(Int qpReference )
    1736 {
    1737   Double qStep = xConvertQP2QStep(qpReference);
    1738 
    1739   if (qStep > xConvertQP2QStep(MAX_QP)
    1740     ||qStep < xConvertQP2QStep(MIN_QP) )
    1741   {
    1742     return false;
    1743   }
    1744 
    1745   return true;
    1746 }
    1747 
    1748 Double  PixelBaseURQQuadraticModel::xConvertQP2QStep(Int qp )
    1749 {
    1750   Int i;
    1751   Double qStep;
    1752   static const Double mapQP2QSTEP[6] = { 0.625, 0.703, 0.797, 0.891, 1.000, 1.125 };
    1753 
    1754   qStep = mapQP2QSTEP[qp % 6];
    1755   for( i=0; i<(qp/6); i++)
    1756   {
    1757     qStep *= 2;
    1758   }
    1759 
    1760   return qStep;
    1761 }
    1762 
    1763 Int     PixelBaseURQQuadraticModel::xConvertQStep2QP(Double qStep )
    1764 {
    1765   Int per = 0, rem = 0;
    1766 
    1767   if( qStep < xConvertQP2QStep(MIN_QP))
    1768   {
    1769     return MIN_QP;
    1770   }
    1771   else if (qStep > xConvertQP2QStep(MAX_QP) )
    1772   {
    1773     return MAX_QP;
    1774   }
    1775 
    1776   while( qStep > xConvertQP2QStep(5) )
    1777   {
    1778     qStep /= 2.0;
    1779     per++;
    1780   }
    1781 
    1782   if (qStep <= 0.625)
    1783   {
    1784     rem = 0;
    1785   }
    1786   else if (qStep <= 0.703)
    1787   {
    1788     rem = 1;
    1789   }
    1790   else if (qStep <= 0.797)
    1791   {
    1792     rem = 2;
    1793   }
    1794   else if (qStep <= 0.891)
    1795   {
    1796     rem = 3;
    1797   }
    1798   else if (qStep <= 1.000)
    1799   {
    1800     rem = 4;
    1801   }
    1802   else
    1803   {
    1804     rem = 5;
    1805   }
    1806   return (per * 6 + rem);
    1807 }
    1808 
    1809 
    1810 Void  TEncRateCtrl::create(Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight)
    1811 {
    1812   Int leftInHeight, leftInWidth;
    1813 
    1814   m_sourceWidthInLCU         = (sourceWidth  / maxCUWidth  ) + (( sourceWidth  %  maxCUWidth ) ? 1 : 0);
    1815   m_sourceHeightInLCU        = (sourceHeight / maxCUHeight) + (( sourceHeight %  maxCUHeight) ? 1 : 0); 
    1816   m_isLowdelay               = (sizeIntraPeriod == -1) ? true : false;
    1817   m_prevBitrate              = ( targetKbps << 10 );  // in units of 1,024 bps
    1818   m_currBitrate              = ( targetKbps << 10 );
    1819   m_frameRate                = frameRate;
    1820   m_refFrameNum              = m_isLowdelay ? (sizeGOP) : (sizeGOP>>1);
    1821   m_nonRefFrameNum           = sizeGOP-m_refFrameNum;
    1822   m_sizeGOP                  = sizeGOP;
    1823   m_numOfPixels              = ((sourceWidth*sourceHeight*3)>>1);
    1824   m_indexGOP                 = 0;
    1825   m_indexFrame               = 0;
    1826   m_indexLCU                 = 0;
    1827   m_indexUnit                = 0;
    1828   m_indexRefFrame            = 0;
    1829   m_indexNonRefFrame         = 0;
    1830   m_occupancyVB              = 0;
    1831   m_initialOVB               = 0;
    1832   m_targetBufLevel           = 0;
    1833   m_initialTBL               = 0;
    1834   m_occupancyVBInFrame       = 0;
    1835   m_remainingBitsInGOP       = (m_currBitrate*sizeGOP/m_frameRate);
    1836   m_remainingBitsInFrame     = 0;
    1837   m_numUnitInFrame           = m_sourceWidthInLCU*m_sourceHeightInLCU;
    1838   m_cMADLinearModel.        initMADLinearModel();
    1839   m_cPixelURQQuadraticModel.initPixelBaseQuadraticModel();
    1840 
    1841   m_costRefAvgWeighting      = 0.0;
    1842   m_costNonRefAvgWeighting   = 0.0;
    1843   m_costAvgbpp               = 0.0; 
    1844   m_activeUnitLevelOn        = false;
    1845 
    1846   m_pcFrameData              = new FrameData   [sizeGOP+1];         initFrameData(qp);
    1847   m_pcLCUData                = new LCUData     [m_numUnitInFrame];  initUnitData (qp);
    1848 
    1849   for(Int i = 0, addressUnit = 0; i < m_sourceHeightInLCU*maxCUHeight; i += maxCUHeight) 
    1850   {
    1851     leftInHeight = sourceHeight - i;
    1852     leftInHeight = min(leftInHeight, maxCUHeight);
    1853     for(Int j = 0; j < m_sourceWidthInLCU*maxCUWidth; j += maxCUWidth, addressUnit++)
    1854     {
    1855       leftInWidth = sourceWidth - j;
    1856       leftInWidth = min(leftInWidth, maxCUWidth);
    1857       m_pcLCUData[addressUnit].m_widthInPixel = leftInWidth;
    1858       m_pcLCUData[addressUnit].m_heightInPixel= leftInHeight;
    1859       m_pcLCUData[addressUnit].m_pixels       = ((leftInHeight*leftInWidth*3)>>1);
    1860     }
    1861   }
    1862 }
    1863 
    1864 Void  TEncRateCtrl::destroy()
    1865 {
    1866   if(m_pcFrameData)
    1867   {
    1868     delete [] m_pcFrameData;
    1869     m_pcFrameData = NULL;
    1870   }
    1871   if(m_pcLCUData)
    1872   {
    1873     delete [] m_pcLCUData;
    1874     m_pcLCUData = NULL;
    1875   }
    1876 }
    1877 
    1878 Void  TEncRateCtrl::initFrameData   (Int qp)
    1879 {
    1880   for(Int i = 0 ; i <= m_sizeGOP; i++)
    1881   {
    1882     m_pcFrameData[i].m_isReferenced = false;
    1883     m_pcFrameData[i].m_costMAD      = 0.0;
    1884     m_pcFrameData[i].m_bits         = 0;
    1885     m_pcFrameData[i].m_qp           = qp;
    1886   }
    1887 }
    1888 
    1889 Void  TEncRateCtrl::initUnitData    (Int qp)
    1890 {
    1891   for(Int i = 1 ; i < m_numUnitInFrame; i++)
    1892   {
    1893     m_pcLCUData[i].m_qp            = qp;
    1894     m_pcLCUData[i].m_bits          = 0;
    1895     m_pcLCUData[i].m_pixels        = 0;
    1896     m_pcLCUData[i].m_widthInPixel  = 0;
    1897     m_pcLCUData[i].m_heightInPixel = 0;
    1898     m_pcLCUData[i].m_costMAD       = 0.0;
    1899   }
    1900 }
    1901 
    1902 Int  TEncRateCtrl::getFrameQP(Bool isReferenced, Int POC)
    1903 {
    1904   Int numofReferenced = 0;
    1905   Int finalQP = 0;
    1906   FrameData* pcFrameData;
    1907 
    1908   m_indexPOCInGOP = (POC%m_sizeGOP) == 0 ? m_sizeGOP : (POC%m_sizeGOP);
    1909   pcFrameData     = &m_pcFrameData[m_indexPOCInGOP];
    1910    
    1911   if(m_indexFrame != 0)
    1912   {
    1913     if(isReferenced)
    1914     {
    1915       Double gamma = m_isLowdelay ? 0.5 : 0.25;
    1916       Double beta  = m_isLowdelay ? 0.9 : 0.6;
    1917       Int    numRemainingRefFrames  = m_refFrameNum    - m_indexRefFrame;
    1918       Int    numRemainingNRefFrames = m_nonRefFrameNum - m_indexNonRefFrame;
    1919      
    1920       Double targetBitsOccupancy  = (m_currBitrate/(Double)m_frameRate) + gamma*(m_targetBufLevel-m_occupancyVB - (m_initialOVB/(Double)m_frameRate));
    1921       Double targetBitsLeftBudget = ((m_costRefAvgWeighting*m_remainingBitsInGOP)/((m_costRefAvgWeighting*numRemainingRefFrames)+(m_costNonRefAvgWeighting*numRemainingNRefFrames)));
    1922 
    1923       m_targetBits = (Int)(beta * targetBitsLeftBudget + (1-beta) * targetBitsOccupancy);
    1924  
    1925       if(m_targetBits <= 0 || m_remainingBitsInGOP <= 0)
    1926       {
    1927         finalQP = m_pcFrameData[m_indexPrevPOCInGOP].m_qp + 2;
    1928       }
    1929       else
    1930       {
    1931         Double costPredMAD   = m_cMADLinearModel.getMAD();
    1932         Int    qpLowerBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp-2;
    1933         Int    qpUpperBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp+2;
    1934         finalQP = m_cPixelURQQuadraticModel.getQP(m_pcFrameData[m_indexPrevPOCInGOP].m_qp, m_targetBits, m_numOfPixels, costPredMAD);
    1935         finalQP = max(qpLowerBound, min(qpUpperBound, finalQP));
    1936         m_activeUnitLevelOn    = true;
    1937         m_remainingBitsInFrame = m_targetBits;
    1938         m_costAvgbpp           = (m_targetBits/(Double)m_numOfPixels);
    1939       }
    1940 
    1941       m_indexRefFrame++;
    1942     }
    1943     else
    1944     {
    1945       Int bwdQP = m_pcFrameData[m_indexPOCInGOP-1].m_qp;
    1946       Int fwdQP = m_pcFrameData[m_indexPOCInGOP+1].m_qp;
    1947        
    1948       if( (fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP-1].m_qp
    1949         ||(fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP+1].m_qp)
    1950       {
    1951         finalQP = (fwdQP+bwdQP);
    1952       }
    1953       else if(bwdQP != fwdQP)
    1954       {
    1955         finalQP = ((bwdQP+fwdQP+2)>>1);
    1956       }
    1957       else
    1958       {
    1959         finalQP = bwdQP+2;
    1960       }
    1961       m_indexNonRefFrame++;
    1962     }
    1963   }
    1964   else
    1965   {
    1966     Int lastQPminus2 = m_pcFrameData[0].m_qp - 2;
    1967     Int lastQPplus2  = m_pcFrameData[0].m_qp + 2;
    1968 
    1969     for(Int idx = 1; idx <= m_sizeGOP; idx++)
    1970     {
    1971       if(m_pcFrameData[idx].m_isReferenced)
    1972       {
    1973         finalQP += m_pcFrameData[idx].m_qp;
    1974         numofReferenced++;
    1975       }
    1976     }
    1977    
    1978     finalQP = (numofReferenced == 0) ? m_pcFrameData[0].m_qp : ((finalQP + (1<<(numofReferenced>>1)))/numofReferenced);
    1979     finalQP = max( lastQPminus2, min( lastQPplus2, finalQP));
    1980 
    1981     Double costAvgFrameBits = m_remainingBitsInGOP/(Double)m_sizeGOP;
    1982     Int    bufLevel  = m_occupancyVB + m_initialOVB;
    1983 
    1984     if(abs(bufLevel) > costAvgFrameBits)
    1985     {
    1986       if(bufLevel < 0)
    1987       {
    1988         finalQP -= 2;
    1989       }
    1990       else
    1991       {
    1992         finalQP += 2;
    1993       }
    1994     }
    1995     m_indexRefFrame++;
    1996   }
    1997   finalQP = max(MIN_QP, min(MAX_QP, finalQP));
    1998 
    1999   for(Int indexLCU = 0 ; indexLCU < m_numUnitInFrame; indexLCU++)
    2000   {
    2001     m_pcLCUData[indexLCU].m_qp = finalQP;
    2002   }
    2003 
    2004   pcFrameData->m_isReferenced = isReferenced;
    2005   pcFrameData->m_qp           = finalQP;
    2006 
    2007   return finalQP;
    2008 }
    2009 
    2010 Bool  TEncRateCtrl::calculateUnitQP ()
    2011 {
    2012   if(!m_activeUnitLevelOn || m_indexLCU == 0)
    2013   {
    2014     return false;
    2015   }
    2016   Int upperQPBound, lowerQPBound, finalQP;
    2017   Int    colQP        = m_pcLCUData[m_indexLCU].m_qp;
    2018   Double colMAD       = m_pcLCUData[m_indexLCU].m_costMAD;
    2019   Double budgetInUnit = m_pcLCUData[m_indexLCU].m_pixels*m_costAvgbpp;
    2020 
    2021 
    2022   Int targetBitsOccupancy = (Int)(budgetInUnit - (m_occupancyVBInFrame/(m_numUnitInFrame-m_indexUnit)));
    2023   Int targetBitsLeftBudget= (Int)((m_remainingBitsInFrame*m_pcLCUData[m_indexLCU].m_pixels)/(Double)(m_numOfPixels-m_codedPixels));
    2024   Int targetBits = (targetBitsLeftBudget>>1) + (targetBitsOccupancy>>1);
    2025  
    2026 
    2027   if( m_indexLCU >= m_sourceWidthInLCU)
    2028   {
    2029     upperQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) + MAX_DELTA_QP;
    2030     lowerQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) - MAX_DELTA_QP;
    2031   }
    2032   else
    2033   {
    2034     upperQPBound = m_pcLCUData[m_indexLCU-1].m_qp + MAX_DELTA_QP;
    2035     lowerQPBound = m_pcLCUData[m_indexLCU-1].m_qp - MAX_DELTA_QP;
    2036   }
    2037 
    2038   if(targetBits < 0)
    2039   {
    2040     finalQP = m_pcLCUData[m_indexLCU-1].m_qp + 1;
    2041   }
    2042   else
    2043   {
    2044     finalQP = m_cPixelURQQuadraticModel.getQP(colQP, targetBits, m_pcLCUData[m_indexLCU].m_pixels, colMAD);
    2045   }
    2046  
    2047   finalQP = max(lowerQPBound, min(upperQPBound, finalQP));
    2048   m_pcLCUData[m_indexLCU].m_qp = max(MIN_QP, min(MAX_QP, finalQP));
    2049  
    2050   return true;
    2051 }
    2052 
    2053 Void  TEncRateCtrl::updateRCGOPStatus()
    2054 {
    2055   m_remainingBitsInGOP = ((m_currBitrate/m_frameRate)*m_sizeGOP) - m_occupancyVB;
    2056  
    2057   FrameData cFrameData = m_pcFrameData[m_sizeGOP];
    2058   initFrameData();
    2059 
    2060   m_pcFrameData[0]   = cFrameData;
    2061   m_indexGOP++;
    2062   m_indexFrame       = 0;
    2063   m_indexRefFrame    = 0;
    2064   m_indexNonRefFrame = 0;
    2065 }
    2066 
    2067 Void  TEncRateCtrl::updataRCFrameStatus(Int frameBits, SliceType eSliceType)
    2068 {
    2069   FrameData* pcFrameData = &m_pcFrameData[m_indexPOCInGOP];
    2070   Int occupancyBits;
    2071   Double adjustmentBits;
    2072 
    2073   m_remainingBitsInGOP = m_remainingBitsInGOP + ( ((m_currBitrate-m_prevBitrate)/m_frameRate)*(m_sizeGOP-m_indexFrame) ) - frameBits;
    2074   occupancyBits        = (Int)((Double)frameBits - (m_currBitrate/(Double)m_frameRate));
    2075  
    2076   if( (occupancyBits < 0) && (m_initialOVB > 0) )
    2077   {
    2078     adjustmentBits = xAdjustmentBits(occupancyBits, m_initialOVB );
    2079 
    2080     if(m_initialOVB < 0)
    2081     {
    2082       adjustmentBits = m_initialOVB;
    2083       occupancyBits += (Int)adjustmentBits;
    2084       m_initialOVB   =  0;
    2085     }
    2086   }
    2087   else if( (occupancyBits > 0) && (m_initialOVB < 0) )
    2088   {
    2089     adjustmentBits = xAdjustmentBits(m_initialOVB, occupancyBits );
    2090    
    2091     if(occupancyBits < 0)
    2092     {
    2093       adjustmentBits = occupancyBits;
    2094       m_initialOVB  += (Int)adjustmentBits;
    2095       occupancyBits  =  0;
    2096     }
    2097   }
    2098 
    2099   if(m_indexGOP == 0)
    2100   {
    2101     m_initialOVB = occupancyBits;
    2102   }
    2103   else
    2104   {
    2105     m_occupancyVB= m_occupancyVB + occupancyBits;
    2106   }
    2107 
    2108   if(pcFrameData->m_isReferenced)
    2109   {
    2110     m_costRefAvgWeighting  = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costRefAvgWeighting)/8.0);
    2111 
    2112     if(m_indexFrame == 0)
    2113     {
    2114       m_initialTBL = m_targetBufLevel  = (frameBits - (m_currBitrate/m_frameRate));
    2115     }
    2116     else
    2117     {
    2118       Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1;
    2119       m_targetBufLevel =  m_targetBufLevel
    2120                             - (m_initialTBL/(m_refFrameNum-1))
    2121                             + (Int)((m_costRefAvgWeighting*(distance+1)*m_currBitrate)/(m_frameRate*(m_costRefAvgWeighting+(m_costNonRefAvgWeighting*distance))))
    2122                             - (m_currBitrate/m_frameRate);
    2123     }
    2124 
    2125     if(m_cMADLinearModel.IsUpdateAvailable())
    2126     {
    2127       m_cMADLinearModel.updateMADLiearModel();
    2128     }
    2129 
    2130     if(eSliceType != I_SLICE &&
    2131        m_cPixelURQQuadraticModel.checkUpdateAvailable(pcFrameData->m_qp))
    2132     {
    2133       m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(pcFrameData->m_qp, pcFrameData->m_bits, m_numOfPixels, pcFrameData->m_costMAD);
    2134     }
    2135   }
    2136   else
    2137   {
    2138     m_costNonRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costNonRefAvgWeighting)/8.0);
    2139   }
    2140 
    2141   m_indexFrame++;
    2142   m_indexLCU             = 0;
    2143   m_indexUnit            = 0;
    2144   m_occupancyVBInFrame   = 0;
    2145   m_remainingBitsInFrame = 0;
    2146   m_codedPixels          = 0;
    2147   m_activeUnitLevelOn    = false;
    2148   m_costAvgbpp           = 0.0;
    2149 }
    2150 Void  TEncRateCtrl::updataRCUnitStatus ()
    2151 {
    2152   if(!m_activeUnitLevelOn || m_indexLCU == 0)
    2153   {
    2154     return;
    2155   }
    2156 
    2157   m_codedPixels  += m_pcLCUData[m_indexLCU-1].m_pixels;
    2158   m_remainingBitsInFrame = m_remainingBitsInFrame - m_pcLCUData[m_indexLCU-1].m_bits;
    2159   m_occupancyVBInFrame   = (Int)(m_occupancyVBInFrame + m_pcLCUData[m_indexLCU-1].m_bits - m_pcLCUData[m_indexLCU-1].m_pixels*m_costAvgbpp);
    2160 
    2161   if( m_cPixelURQQuadraticModel.checkUpdateAvailable(m_pcLCUData[m_indexLCU-1].m_qp) )
    2162   {
    2163     m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(m_pcLCUData[m_indexLCU-1].m_qp, m_pcLCUData[m_indexLCU-1].m_bits, m_pcLCUData[m_indexLCU-1].m_pixels, m_pcLCUData[m_indexLCU-1].m_costMAD);
    2164   }
    2165 
    2166   m_indexUnit++;
    2167 }
    2168 
    2169 Void  TEncRateCtrl::updateFrameData(UInt64 actualFrameBits)
    2170 {
    2171   Double costMAD = 0.0;
    2172  
    2173   for(Int i = 0; i < m_numUnitInFrame; i++)
    2174   {
    2175     costMAD    += m_pcLCUData[i].m_costMAD;
    2176   }
    2177  
    2178   m_pcFrameData[m_indexPOCInGOP].m_costMAD = (costMAD/(Double)m_numUnitInFrame);
    2179   m_pcFrameData[m_indexPOCInGOP].m_bits    = (Int)actualFrameBits;
    2180  
    2181   if(m_pcFrameData[m_indexPOCInGOP].m_isReferenced)
    2182   {
    2183     m_indexPrevPOCInGOP = m_indexPOCInGOP;
    2184     m_cMADLinearModel.updateMADHistory(m_pcFrameData[m_indexPOCInGOP].m_costMAD);
    2185   }
    2186 }
    2187 
    2188 Void  TEncRateCtrl::updateLCUData(TComDataCU* pcCU, UInt64 actualLCUBits, Int qp)
    2189 {
    2190   Int     x, y;
    2191   Double  costMAD = 0.0;
    2192 
    2193   Pel*  pOrg   = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
    2194   Pel*  pRec   = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0);
    2195   Int   stride = pcCU->getPic()->getStride();
    2196 
    2197   Int   width  = m_pcLCUData[m_indexLCU].m_widthInPixel;
    2198   Int   height = m_pcLCUData[m_indexLCU].m_heightInPixel;
    2199 
    2200   for( y = 0; y < height; y++ )
    2201   {
    2202     for( x = 0; x < width; x++ )
    2203     {
    2204       costMAD += abs( pOrg[x] - pRec[x] );
    2205     }
    2206     pOrg += stride;
    2207     pRec += stride;
    2208   }
    2209   m_pcLCUData[m_indexLCU  ].m_qp      = qp;
    2210   m_pcLCUData[m_indexLCU  ].m_costMAD = (costMAD /(Double)(width*height));
    2211   m_pcLCUData[m_indexLCU++].m_bits    = (Int)actualLCUBits;
    2212 }
    2213 
    2214 Double TEncRateCtrl::xAdjustmentBits(Int& reductionBits, Int& compensationBits)
    2215 {
    2216   Double adjustment  = ADJUSTMENT_FACTOR*reductionBits;
    2217   reductionBits     -= (Int)adjustment;
    2218   compensationBits  += (Int)adjustment;
    2219 
    2220   return adjustment;
    2221 }
    2222 
    2223 #endif
    2224 
Note: See TracChangeset for help on using the changeset viewer.