Changeset 872 in 3DVCSoftware for trunk/source/Lib/TLibEncoder/TEncRateCtrl.cpp


Ignore:
Timestamp:
27 Mar 2014, 10:11:29 (11 years ago)
Author:
tech
Message:

Merged HTM-10.0-dev0@871. (MV-HEVC 7 HLS)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/Lib/TLibEncoder/TEncRateCtrl.cpp

    r655 r872  
    44 * granted under this license. 
    55 *
    6  * Copyright (c) 2010-2013, ITU/ISO/IEC
     6* Copyright (c) 2010-2014, ITU/ISO/IEC
    77 * All rights reserved.
    88 *
     
    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
    539 
    540482#if KWU_RC_MADPRED_E0227
    541483  m_lastIVPicture = NULL;
     
    543485
    544486  m_picActualHeaderBits = 0;
    545 #if !M0036_RC_IMPROVEMENT
    546   m_totalMAD            = 0.0;
    547 #endif
    548487  m_picActualBits       = 0;
    549488  m_picQP               = 0;
     
    574513  }
    575514
    576 #if M0036_RC_IMPROVEMENT
    577515  targetBits  = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio );
    578 #else
    579   targetBits  = Int( GOPbitsLeft * currPicRatio / totalPicRatio );
    580 #endif
    581516
    582517  if ( targetBits < 100 )
     
    691626      m_LCUs[LCUIdx].m_lambda     = 0.0;
    692627      m_LCUs[LCUIdx].m_targetBits = 0;
    693 #if M0036_RC_IMPROVEMENT
    694628      m_LCUs[LCUIdx].m_bitWeight  = 1.0;
    695 #else
    696       m_LCUs[LCUIdx].m_MAD        = 0.0;
    697 #endif
    698629      Int currWidth  = ( (i == picWidthInLCU -1) ? picWidth  - LCUWidth *(picWidthInLCU -1) : LCUWidth  );
    699630      Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight );
     
    708639  }
    709640  m_picActualHeaderBits = 0;
    710 #if !M0036_RC_IMPROVEMENT
    711   m_totalMAD            = 0.0;
    712 #endif
    713641  m_picActualBits       = 0;
    714642  m_picQP               = 0;
     
    723651
    724652
    725 #if !M0036_RC_IMPROVEMENT
    726   m_lastPicture = NULL;
    727   list<TEncRCPic*>::reverse_iterator it;
    728   for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ )
    729   {
    730     if ( (*it)->getFrameLevel() == m_frameLevel )
    731     {
    732       m_lastPicture = (*it);
    733       break;
    734     }
    735   }
    736 #endif
    737 
    738653#if KWU_RC_MADPRED_E0227
    739654  list<TEncRCPic*>::reverse_iterator it;
     
    775690
    776691
    777 #if RATE_CONTROL_INTRA
    778692Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType)
    779 #else
    780 Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures )
    781 #endif
    782693{
    783694  Double alpha         = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
    784695  Double beta          = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
    785696  Double bpp       = (Double)m_targetBits/(Double)m_numberOfPixel;
    786 #if RATE_CONTROL_INTRA
    787697  Double estLambda;
    788698  if (eSliceType == I_SLICE)
     
    794704    estLambda = alpha * pow( bpp, beta );
    795705  }
    796 #else
    797   Double estLambda = alpha * pow( bpp, beta );
    798 #endif 
    799706 
    800707  Double lastLevelLambda = -1.0;
     
    844751  m_estPicLambda = estLambda;
    845752
    846 #if M0036_RC_IMPROVEMENT
    847753  Double totalWeight = 0.0;
    848754  // initial BU bit allocation weight
    849755  for ( Int i=0; i<m_numberOfLCU; i++ )
    850756  {
    851 #if RC_FIX
    852757    Double alphaLCU, betaLCU;
    853758    if ( m_encRCSeq->getUseLCUSeparateModel() )
     
    861766      betaLCU  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
    862767    }
    863 #else
    864     Double alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha;
    865     Double betaLCU  = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta;
    866 #endif
    867768
    868769    m_LCUs[i].m_bitWeight =  m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU );
     
    879780    m_LCUs[i].m_bitWeight = BUTargetBits;
    880781  }
    881 #endif
    882782
    883783  return estLambda;
     
    989889
    990890
    991 #if RATE_CONTROL_INTRA
    992891Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) 
    993 #else
    994 Double TEncRCPic::getLCUTargetBpp()
    995 #endif
     892{
     893  Int   LCUIdx    = getLCUCoded();
     894  Double bpp      = -1.0;
     895  Int avgBits     = 0;
     896
     897  if (eSliceType == I_SLICE)
     898  {
     899    Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1;
     900    Int bitrateWindow = min(4,noOfLCUsLeft);
     901    Double MAD      = getLCU(LCUIdx).m_costIntra;
     902
     903    if (m_remainingCostIntra > 0.1 )
     904    {
     905      Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow;
     906      avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra );
     907    }
     908    else
     909    {
     910      avgBits = Int( m_bitsLeft / m_LCULeft );
     911    }
     912    m_remainingCostIntra -= MAD;
     913  }
     914  else
     915  {
     916  Double totalWeight = 0;
     917  for ( Int i=LCUIdx; i<m_numberOfLCU; i++ )
     918  {
     919    totalWeight += m_LCUs[i].m_bitWeight;
     920  }
     921  Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() );
     922  avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 );
     923  }
     924
     925  if ( avgBits < 1 )
     926  {
     927    avgBits = 1;
     928  }
     929
     930  bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel;
     931  m_LCUs[ LCUIdx ].m_targetBits = avgBits;
     932
     933  return bpp;
     934}
     935
     936
     937#if KWU_RC_MADPRED_E0227
     938Double TEncRCPic::getLCUTargetBppforInterView( list<TEncRCPic*>& listPreviousPictures, TComDataCU* pcCU, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction, Int* disparity )
    996939{
    997940  Int   LCUIdx    = getLCUCoded();
     
    1003946#endif
    1004947
    1005 #if RATE_CONTROL_INTRA
    1006   if (eSliceType == I_SLICE){
    1007     Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1;
    1008     Int bitrateWindow = min(4,noOfLCUsLeft);
    1009     Double MAD      = getLCU(LCUIdx).m_costIntra;
    1010 
    1011     if (m_remainingCostIntra > 0.1 )
    1012     {
    1013       Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow;
    1014       avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra );
    1015     }
    1016     else
    1017     {
    1018       avgBits = Int( m_bitsLeft / m_LCULeft );
    1019     }
    1020     m_remainingCostIntra -= MAD;
    1021   }
    1022   else
    1023   {
    1024 #endif
    1025 #if M0036_RC_IMPROVEMENT
    1026   Double totalWeight = 0;
    1027   for ( Int i=LCUIdx; i<m_numberOfLCU; i++ )
    1028   {
    1029     totalWeight += m_LCUs[i].m_bitWeight;
    1030   }
    1031   Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() );
    1032   avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 );
    1033 #else
    1034   if ( m_lastPicture == NULL )
    1035   {
    1036     avgBits = Int( m_bitsLeft / m_LCULeft );
    1037   }
    1038   else
    1039   {
    1040     MAD = m_lastPicture->getLCU(LCUIdx).m_MAD;
    1041     totalMAD = m_lastPicture->getTotalMAD();
    1042     for ( Int i=0; i<LCUIdx; i++ )
    1043     {
    1044       totalMAD -= m_lastPicture->getLCU(i).m_MAD;
    1045     }
    1046 
    1047     if ( totalMAD > 0.1 )
    1048     {
    1049       avgBits = Int( m_bitsLeft * MAD / totalMAD );
    1050     }
    1051     else
    1052     {
    1053       avgBits = Int( m_bitsLeft / m_LCULeft );
    1054     }
    1055   }
    1056 #endif
    1057 #if RATE_CONTROL_INTRA
    1058   }
    1059 #endif
    1060 
    1061   if ( avgBits < 1 )
    1062   {
    1063     avgBits = 1;
    1064   }
    1065 
    1066   bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel;
    1067   m_LCUs[ LCUIdx ].m_targetBits = avgBits;
    1068 
    1069   return bpp;
    1070 }
    1071 
    1072 
    1073 #if KWU_RC_MADPRED_E0227
    1074 Double TEncRCPic::getLCUTargetBppforInterView( list<TEncRCPic*>& listPreviousPictures, TComDataCU* pcCU, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction, Int* disparity )
    1075 {
    1076   Int   LCUIdx    = getLCUCoded();
    1077   Double bpp      = -1.0;
    1078   Int avgBits     = 0;
    1079 #if !M0036_RC_IMPROVEMENT
    1080   Double totalMAD = -1.0;
    1081   Double MAD      = -1.0;
    1082 #endif
    1083 
    1084948  Double totalMAD = -1.0;
    1085949  Double MAD      = -1.0;
     
    13001164    beta  *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
    13011165
    1302 #if M0036_RC_IMPROVEMENT
    13031166    alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
    13041167    beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
    1305 #else
    1306     alpha = Clip3( 0.05, 20.0, alpha );
    1307     beta  = Clip3( -3.0, -0.1, beta  );
    1308 #endif
    13091168
    13101169    TRCParameter rcPara;
     
    13191178  alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
    13201179  double lnbpp = log( bpp );
    1321 #if M0036_RC_IMPROVEMENT
    13221180  lnbpp = Clip3( -5.0, -0.1, lnbpp );
    1323 #else
    1324   lnbpp = Clip3( -5.0, 1.0, lnbpp );
    1325 #endif
    13261181  beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
    13271182
    1328 #if M0036_RC_IMPROVEMENT
    13291183  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
    13301184  beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
    1331 #else
    1332   alpha = Clip3( 0.05, 20.0, alpha );
    1333   beta  = Clip3( -3.0, -0.1, beta  );
    1334 #endif
    13351185  TRCParameter rcPara;
    13361186  rcPara.m_alpha = alpha;
     
    13401190}
    13411191
    1342 #if !M0036_RC_IMPROVEMENT
    1343 Double TEncRCPic::getEffectivePercentage()
    1344 {
    1345   Int effectivePiexels = 0;
    1346   Int totalPixels = 0;
    1347 
    1348   for ( Int i=0; i<m_numberOfLCU; i++ )
    1349   {
    1350     totalPixels += m_LCUs[i].m_numberOfPixel;
    1351     if ( m_LCUs[i].m_QP > 0 )
    1352     {
    1353       effectivePiexels += m_LCUs[i].m_numberOfPixel;
    1354     }
    1355   }
    1356 
    1357   Double effectivePixelPercentage = (Double)effectivePiexels/(Double)totalPixels;
    1358   return effectivePixelPercentage;
    1359 }
    1360 #endif
    1361 
    13621192Double TEncRCPic::calAverageQP()
    13631193{
     
    14141244}
    14151245
    1416 #if M0036_RC_IMPROVEMENT
    1417 #if RATE_CONTROL_INTRA
    14181246Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType)
    1419 #else
    1420 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda )
    1421 #endif
    1422 #else
    1423 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage )
    1424 #endif
    14251247{
    14261248  m_picActualHeaderBits = actualHeaderBits;
     
    14351257  }
    14361258  m_picLambda           = averageLambda;
    1437 #if !M0036_RC_IMPROVEMENT || KWU_RC_MADPRED_E0227
    14381259#if KWU_RC_MADPRED_E0227
    14391260  m_totalMAD = 0;
    1440 #endif
    14411261  for ( Int i=0; i<m_numberOfLCU; i++ )
    14421262  {
     
    14471267  Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
    14481268  Double beta  = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
    1449 #if RATE_CONTROL_INTRA
    14501269  if (eSliceType == I_SLICE)
    14511270  {
     
    14541273  else
    14551274  {
    1456 #endif
    14571275  // update parameters
    14581276  Double picActualBits = ( Double )m_picActualBits;
     
    14611279  Double inputLambda   = m_picLambda;
    14621280
    1463 #if M0036_RC_IMPROVEMENT
    14641281  if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 )
    1465 #else
    1466   if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 || effectivePercentage < 0.05 )
    1467 #endif
    14681282  {
    14691283    alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
    14701284    beta  *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
    14711285
    1472 #if M0036_RC_IMPROVEMENT
    14731286    alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
    14741287    beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
    1475 #else
    1476     alpha = Clip3( 0.05, 20.0, alpha );
    1477     beta  = Clip3( -3.0, -0.1, beta  );
    1478 #endif
    14791288    TRCParameter rcPara;
    14801289    rcPara.m_alpha = alpha;
     
    14881297  alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
    14891298  double lnbpp = log( picActualBpp );
    1490 #if M0036_RC_IMPROVEMENT
    14911299  lnbpp = Clip3( -5.0, -0.1, lnbpp );
    1492 #else
    1493   lnbpp = Clip3( -5.0, 1.0, lnbpp );
    1494 #endif
    14951300  beta  += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
    14961301
    1497 #if M0036_RC_IMPROVEMENT
    14981302  alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha );
    14991303  beta  = Clip3( g_RCBetaMinValue,  g_RCBetaMaxValue,  beta  );
    1500 #else
    1501   alpha = Clip3( 0.05, 20.0, alpha );
    1502   beta  = Clip3( -3.0, -0.1, beta  );
    1503 #endif
    1504 #if RATE_CONTROL_INTRA
    1505   }
    1506 #endif
     1304  }
    15071305
    15081306  TRCParameter rcPara;
     
    15121310  m_encRCSeq->setPicPara( m_frameLevel, rcPara );
    15131311
    1514 #if M0036_RC_IMPROVEMENT
    15151312  if ( m_frameLevel == 1 )
    15161313  {
     
    15191316    m_encRCSeq->setLastLambda( updateLastLambda );
    15201317  }
    1521 #endif
    1522 }
    1523 
    1524 #if RATE_CONTROL_INTRA
     1318}
     1319
    15251320Int TEncRCPic::getRefineBitsForIntra( Int orgBits )
    15261321{
     
    16111406  return estLambda;
    16121407}
    1613 #endif
    16141408
    16151409TEncRateCtrl::TEncRateCtrl()
     
    16451439}
    16461440
    1647 #if M0036_RC_IMPROVEMENT
    16481441#if KWU_RC_MADPRED_E0227
    16491442Void 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], Int layerID )
     
    16511444Void 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] )
    16521445#endif
    1653 #else
    1654 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] )
    1655 #endif
    16561446{
    16571447  destroy();
     
    16681458
    16691459  Int numberOfLevel = 1;
    1670 #if M0036_RC_IMPROVEMENT
    16711460  Int adaptiveBit = 0;
    16721461  if ( keepHierBits > 0 )
    1673 #else
    1674   if ( keepHierBits )
    1675 #endif
    16761462  {
    16771463    numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1;
     
    16961482  }
    16971483
    1698 #if M0036_RC_IMPROVEMENT
    16991484  if ( keepHierBits > 0 )
    1700 #else
    1701   if ( keepHierBits )
    1702 #endif
    17031485  {
    17041486    Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) );
     
    17331515        bitsRatio[3] = 14;
    17341516      }
    1735 #if M0036_RC_IMPROVEMENT
    17361517      if ( keepHierBits == 2 )
    17371518      {
    17381519        adaptiveBit = 1;
    17391520      }
    1740 #endif
    17411521    }
    17421522    else if ( GOPSize == 8 && !isLowdelay )
     
    17861566        bitsRatio[7] = 1;
    17871567      }
    1788 #if M0036_RC_IMPROVEMENT
    17891568      if ( keepHierBits == 2 )
    17901569      {
    17911570        adaptiveBit = 2;
    17921571      }
    1793 #endif
    17941572    }
    17951573    else
    17961574    {
    1797 #if M0036_RC_IMPROVEMENT
    17981575      printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" );
    1799 #else
    1800       printf( "\n hierarchical bit allocation is not support for the specified coding structure currently." );
    1801 #endif
    18021576    }
    18031577  }
     
    18121586    }
    18131587  }
    1814 #if M0036_RC_IMPROVEMENT
    18151588  if ( keepHierBits > 0 )
    1816 #else
    1817   if ( keepHierBits )
    1818 #endif
    18191589  {
    18201590    if ( GOPSize == 4 && isLowdelay )
     
    18511621
    18521622  m_encRCSeq = new TEncRCSeq;
    1853 #if M0036_RC_IMPROVEMENT
    18541623  m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit );
    1855 #else
    1856   m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel );
    1857 #endif
    18581624  m_encRCSeq->initBitsRatio( bitsRatio );
    18591625  m_encRCSeq->initGOPID2Level( GOPID2Level );
     
    18941660}
    18951661
    1896 #else
    1897 
    1898 #define ADJUSTMENT_FACTOR       0.60
    1899 #define HIGH_QSTEP_THRESHOLD    9.5238
    1900 #define HIGH_QSTEP_ALPHA        4.9371
    1901 #define HIGH_QSTEP_BETA         0.0922
    1902 #define LOW_QSTEP_ALPHA         16.7429
    1903 #define LOW_QSTEP_BETA          -1.1494
    1904 
    1905 #define MAD_PRED_Y1             1.0
    1906 #define MAD_PRED_Y2             0.0
    1907 
    1908 enum MAD_HISOTRY {
    1909   MAD_PPPrevious = 0,
    1910   MAD_PPrevious  = 1,
    1911   MAD_Previous   = 2
    1912 };
    1913 
    1914 Void    MADLinearModel::initMADLinearModel()
    1915 {
    1916   m_activeOn = false;
    1917   m_paramY1  = 1.0;
    1918   m_paramY2  = 0.0;
    1919   m_costMADs[0] = m_costMADs[1] = m_costMADs[2] = 0.0;
    1920 }
    1921 
    1922 Double  MADLinearModel::getMAD()
    1923 {
    1924   Double costPredMAD = m_paramY1 * m_costMADs[MAD_Previous] + m_paramY2;
    1925 
    1926   if(costPredMAD < 0)
    1927   {
    1928     costPredMAD = m_costMADs[MAD_Previous];
    1929     m_paramY1   = MAD_PRED_Y1;
    1930     m_paramY2   = MAD_PRED_Y2;
    1931   }
    1932   return costPredMAD;
    1933 }
    1934 
    1935 Void    MADLinearModel::updateMADLiearModel()
    1936 {
    1937   Double dNewY1 = ((m_costMADs[MAD_Previous] - m_costMADs[MAD_PPrevious]) / (m_costMADs[MAD_PPrevious] - m_costMADs[MAD_PPPrevious]));
    1938   Double dNewY2 =  (m_costMADs[MAD_Previous] - (dNewY1*m_costMADs[MAD_PPrevious]));
    1939  
    1940   m_paramY1 = 0.70+0.20*m_paramY1+ 0.10*dNewY1;
    1941   m_paramY2 =      0.20*m_paramY2+ 0.10*dNewY2;
    1942 }
    1943 
    1944 Void    MADLinearModel::updateMADHistory(Double dMAD)
    1945 {
    1946   m_costMADs[MAD_PPPrevious] = m_costMADs[MAD_PPrevious];
    1947   m_costMADs[MAD_PPrevious ] = m_costMADs[MAD_Previous ];
    1948   m_costMADs[MAD_Previous  ] = dMAD;
    1949   m_activeOn = (m_costMADs[MAD_Previous  ] && m_costMADs[MAD_PPrevious ] && m_costMADs[MAD_PPPrevious]);
    1950 }
    1951 
    1952 
    1953 Void    PixelBaseURQQuadraticModel::initPixelBaseQuadraticModel()
    1954 {
    1955   m_paramHighX1 = HIGH_QSTEP_ALPHA;
    1956   m_paramHighX2 = HIGH_QSTEP_BETA;
    1957   m_paramLowX1  = LOW_QSTEP_ALPHA;
    1958   m_paramLowX2  = LOW_QSTEP_BETA;
    1959 }
    1960 
    1961 Int     PixelBaseURQQuadraticModel::getQP(Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD)
    1962 {
    1963   Double qStep;
    1964   Double bppPerMAD = (Double)(targetBits/(numberOfPixels*costPredMAD));
    1965  
    1966   if(xConvertQP2QStep(qp) >= HIGH_QSTEP_THRESHOLD)
    1967   {
    1968     qStep = 1/( sqrt((bppPerMAD/m_paramHighX1)+((m_paramHighX2*m_paramHighX2)/(4*m_paramHighX1*m_paramHighX1))) - (m_paramHighX2/(2*m_paramHighX1)));
    1969   }
    1970   else
    1971   {
    1972     qStep = 1/( sqrt((bppPerMAD/m_paramLowX1)+((m_paramLowX2*m_paramLowX2)/(4*m_paramLowX1*m_paramLowX1))) - (m_paramLowX2/(2*m_paramLowX1)));
    1973   }
    1974  
    1975   return xConvertQStep2QP(qStep);
    1976 }
    1977 
    1978 Void    PixelBaseURQQuadraticModel::updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD)
    1979 {
    1980   Double qStep     = xConvertQP2QStep(qp);
    1981   Double invqStep = (1/qStep);
    1982   Double paramNewX1, paramNewX2;
    1983  
    1984   if(qStep >= HIGH_QSTEP_THRESHOLD)
    1985   {
    1986     paramNewX2    = (((bits/(numberOfPixels*costMAD))-(23.3772*invqStep*invqStep))/((1-200*invqStep)*invqStep));
    1987     paramNewX1    = (23.3772-200*paramNewX2);
    1988     m_paramHighX1 = 0.70*HIGH_QSTEP_ALPHA + 0.20 * m_paramHighX1 + 0.10 * paramNewX1;
    1989     m_paramHighX2 = 0.70*HIGH_QSTEP_BETA  + 0.20 * m_paramHighX2 + 0.10 * paramNewX2;
    1990   }
    1991   else
    1992   {
    1993     paramNewX2   = (((bits/(numberOfPixels*costMAD))-(5.8091*invqStep*invqStep))/((1-9.5455*invqStep)*invqStep));
    1994     paramNewX1   = (5.8091-9.5455*paramNewX2);
    1995     m_paramLowX1 = 0.90*LOW_QSTEP_ALPHA + 0.09 * m_paramLowX1 + 0.01 * paramNewX1;
    1996     m_paramLowX2 = 0.90*LOW_QSTEP_BETA  + 0.09 * m_paramLowX2 + 0.01 * paramNewX2;
    1997   }
    1998 }
    1999 
    2000 Bool    PixelBaseURQQuadraticModel::checkUpdateAvailable(Int qpReference )
    2001 {
    2002   Double qStep = xConvertQP2QStep(qpReference);
    2003 
    2004   if (qStep > xConvertQP2QStep(MAX_QP)
    2005     ||qStep < xConvertQP2QStep(MIN_QP) )
    2006   {
    2007     return false;
    2008   }
    2009 
    2010   return true;
    2011 }
    2012 
    2013 Double  PixelBaseURQQuadraticModel::xConvertQP2QStep(Int qp )
    2014 {
    2015   Int i;
    2016   Double qStep;
    2017   static const Double mapQP2QSTEP[6] = { 0.625, 0.703, 0.797, 0.891, 1.000, 1.125 };
    2018 
    2019   qStep = mapQP2QSTEP[qp % 6];
    2020   for( i=0; i<(qp/6); i++)
    2021   {
    2022     qStep *= 2;
    2023   }
    2024 
    2025   return qStep;
    2026 }
    2027 
    2028 Int     PixelBaseURQQuadraticModel::xConvertQStep2QP(Double qStep )
    2029 {
    2030   Int per = 0, rem = 0;
    2031 
    2032   if( qStep < xConvertQP2QStep(MIN_QP))
    2033   {
    2034     return MIN_QP;
    2035   }
    2036   else if (qStep > xConvertQP2QStep(MAX_QP) )
    2037   {
    2038     return MAX_QP;
    2039   }
    2040 
    2041   while( qStep > xConvertQP2QStep(5) )
    2042   {
    2043     qStep /= 2.0;
    2044     per++;
    2045   }
    2046 
    2047   if (qStep <= 0.625)
    2048   {
    2049     rem = 0;
    2050   }
    2051   else if (qStep <= 0.703)
    2052   {
    2053     rem = 1;
    2054   }
    2055   else if (qStep <= 0.797)
    2056   {
    2057     rem = 2;
    2058   }
    2059   else if (qStep <= 0.891)
    2060   {
    2061     rem = 3;
    2062   }
    2063   else if (qStep <= 1.000)
    2064   {
    2065     rem = 4;
    2066   }
    2067   else
    2068   {
    2069     rem = 5;
    2070   }
    2071   return (per * 6 + rem);
    2072 }
    2073 
    2074 
    2075 Void  TEncRateCtrl::create(Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight)
    2076 {
    2077   Int leftInHeight, leftInWidth;
    2078 
    2079   m_sourceWidthInLCU         = (sourceWidth  / maxCUWidth  ) + (( sourceWidth  %  maxCUWidth ) ? 1 : 0);
    2080   m_sourceHeightInLCU        = (sourceHeight / maxCUHeight) + (( sourceHeight %  maxCUHeight) ? 1 : 0); 
    2081   m_isLowdelay               = (sizeIntraPeriod == -1) ? true : false;
    2082 
    2083   m_prevBitrate              = ( targetKbps << 10 );  // in units of 1,024 bps
    2084   m_currBitrate              = ( targetKbps << 10 );
    2085 
    2086   m_frameRate                = frameRate;
    2087   m_refFrameNum              = m_isLowdelay ? (sizeGOP) : (sizeGOP>>1);
    2088   m_nonRefFrameNum           = sizeGOP-m_refFrameNum;
    2089   m_sizeGOP                  = sizeGOP;
    2090   m_numOfPixels              = ((sourceWidth*sourceHeight*3)>>1);
    2091   m_indexGOP                 = 0;
    2092   m_indexFrame               = 0;
    2093   m_indexLCU                 = 0;
    2094   m_indexUnit                = 0;
    2095   m_indexRefFrame            = 0;
    2096   m_indexNonRefFrame         = 0;
    2097   m_occupancyVB              = 0;
    2098   m_initialOVB               = 0;
    2099   m_targetBufLevel           = 0;
    2100   m_initialTBL               = 0;
    2101   m_occupancyVBInFrame       = 0;
    2102   m_remainingBitsInGOP       = (m_currBitrate*sizeGOP/m_frameRate);
    2103   m_remainingBitsInFrame     = 0;
    2104   m_numUnitInFrame           = m_sourceWidthInLCU*m_sourceHeightInLCU;
    2105   m_cMADLinearModel.        initMADLinearModel();
    2106   m_cPixelURQQuadraticModel.initPixelBaseQuadraticModel();
    2107 
    2108   m_costRefAvgWeighting      = 0.0;
    2109   m_costNonRefAvgWeighting   = 0.0;
    2110   m_costAvgbpp               = 0.0; 
    2111   m_activeUnitLevelOn        = false;
    2112 
    2113   m_pcFrameData              = new FrameData   [sizeGOP+1];         initFrameData(qp);
    2114   m_pcLCUData                = new LCUData     [m_numUnitInFrame];  initUnitData (qp);
    2115 
    2116   for(Int i = 0, addressUnit = 0; i < m_sourceHeightInLCU*maxCUHeight; i += maxCUHeight) 
    2117   {
    2118     leftInHeight = sourceHeight - i;
    2119     leftInHeight = min(leftInHeight, maxCUHeight);
    2120     for(Int j = 0; j < m_sourceWidthInLCU*maxCUWidth; j += maxCUWidth, addressUnit++)
    2121     {
    2122       leftInWidth = sourceWidth - j;
    2123       leftInWidth = min(leftInWidth, maxCUWidth);
    2124       m_pcLCUData[addressUnit].m_widthInPixel = leftInWidth;
    2125       m_pcLCUData[addressUnit].m_heightInPixel= leftInHeight;
    2126       m_pcLCUData[addressUnit].m_pixels       = ((leftInHeight*leftInWidth*3)>>1);
    2127     }
    2128   }
    2129 }
    2130 
    2131 Void  TEncRateCtrl::destroy()
    2132 {
    2133   if(m_pcFrameData)
    2134   {
    2135     delete [] m_pcFrameData;
    2136     m_pcFrameData = NULL;
    2137   }
    2138   if(m_pcLCUData)
    2139   {
    2140     delete [] m_pcLCUData;
    2141     m_pcLCUData = NULL;
    2142   }
    2143 }
    2144 
    2145 Void  TEncRateCtrl::initFrameData   (Int qp)
    2146 {
    2147   for(Int i = 0 ; i <= m_sizeGOP; i++)
    2148   {
    2149     m_pcFrameData[i].m_isReferenced = false;
    2150     m_pcFrameData[i].m_costMAD      = 0.0;
    2151     m_pcFrameData[i].m_bits         = 0;
    2152     m_pcFrameData[i].m_qp           = qp;
    2153   }
    2154 }
    2155 
    2156 Void  TEncRateCtrl::initUnitData    (Int qp)
    2157 {
    2158   for(Int i = 1 ; i < m_numUnitInFrame; i++)
    2159   {
    2160     m_pcLCUData[i].m_qp            = qp;
    2161     m_pcLCUData[i].m_bits          = 0;
    2162     m_pcLCUData[i].m_pixels        = 0;
    2163     m_pcLCUData[i].m_widthInPixel  = 0;
    2164     m_pcLCUData[i].m_heightInPixel = 0;
    2165     m_pcLCUData[i].m_costMAD       = 0.0;
    2166   }
    2167 }
    2168 
    2169 Int  TEncRateCtrl::getFrameQP(Bool isReferenced, Int POC)
    2170 {
    2171   Int numofReferenced = 0;
    2172   Int finalQP = 0;
    2173   FrameData* pcFrameData;
    2174 
    2175   m_indexPOCInGOP = (POC%m_sizeGOP) == 0 ? m_sizeGOP : (POC%m_sizeGOP);
    2176   pcFrameData     = &m_pcFrameData[m_indexPOCInGOP];
    2177    
    2178   if(m_indexFrame != 0)
    2179   {
    2180     if(isReferenced)
    2181     {
    2182       Double gamma = m_isLowdelay ? 0.5 : 0.25;
    2183       Double beta  = m_isLowdelay ? 0.9 : 0.6;
    2184       Int    numRemainingRefFrames  = m_refFrameNum    - m_indexRefFrame;
    2185       Int    numRemainingNRefFrames = m_nonRefFrameNum - m_indexNonRefFrame;
    2186      
    2187       Double targetBitsOccupancy  = (m_currBitrate/(Double)m_frameRate) + gamma*(m_targetBufLevel-m_occupancyVB - (m_initialOVB/(Double)m_frameRate));
    2188       Double targetBitsLeftBudget = ((m_costRefAvgWeighting*m_remainingBitsInGOP)/((m_costRefAvgWeighting*numRemainingRefFrames)+(m_costNonRefAvgWeighting*numRemainingNRefFrames)));
    2189 
    2190       m_targetBits = (Int)(beta * targetBitsLeftBudget + (1-beta) * targetBitsOccupancy);
    2191  
    2192       if(m_targetBits <= 0 || m_remainingBitsInGOP <= 0)
    2193       {
    2194         finalQP = m_pcFrameData[m_indexPrevPOCInGOP].m_qp + 2;
    2195       }
    2196       else
    2197       {
    2198         Double costPredMAD   = m_cMADLinearModel.getMAD();
    2199         Int    qpLowerBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp-2;
    2200         Int    qpUpperBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp+2;
    2201         finalQP = m_cPixelURQQuadraticModel.getQP(m_pcFrameData[m_indexPrevPOCInGOP].m_qp, m_targetBits, m_numOfPixels, costPredMAD);
    2202         finalQP = max(qpLowerBound, min(qpUpperBound, finalQP));
    2203         m_activeUnitLevelOn    = true;
    2204         m_remainingBitsInFrame = m_targetBits;
    2205         m_costAvgbpp           = (m_targetBits/(Double)m_numOfPixels);
    2206       }
    2207 
    2208       m_indexRefFrame++;
    2209     }
    2210     else
    2211     {
    2212       Int bwdQP = m_pcFrameData[m_indexPOCInGOP-1].m_qp;
    2213       Int fwdQP = m_pcFrameData[m_indexPOCInGOP+1].m_qp;
    2214        
    2215       if( (fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP-1].m_qp
    2216         ||(fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP+1].m_qp)
    2217       {
    2218         finalQP = (fwdQP+bwdQP);
    2219       }
    2220       else if(bwdQP != fwdQP)
    2221       {
    2222         finalQP = ((bwdQP+fwdQP+2)>>1);
    2223       }
    2224       else
    2225       {
    2226         finalQP = bwdQP+2;
    2227       }
    2228       m_indexNonRefFrame++;
    2229     }
    2230   }
    2231   else
    2232   {
    2233     Int lastQPminus2 = m_pcFrameData[0].m_qp - 2;
    2234     Int lastQPplus2  = m_pcFrameData[0].m_qp + 2;
    2235 
    2236     for(Int idx = 1; idx <= m_sizeGOP; idx++)
    2237     {
    2238       if(m_pcFrameData[idx].m_isReferenced)
    2239       {
    2240         finalQP += m_pcFrameData[idx].m_qp;
    2241         numofReferenced++;
    2242       }
    2243     }
    2244    
    2245     finalQP = (numofReferenced == 0) ? m_pcFrameData[0].m_qp : ((finalQP + (1<<(numofReferenced>>1)))/numofReferenced);
    2246     finalQP = max( lastQPminus2, min( lastQPplus2, finalQP));
    2247 
    2248     Double costAvgFrameBits = m_remainingBitsInGOP/(Double)m_sizeGOP;
    2249     Int    bufLevel  = m_occupancyVB + m_initialOVB;
    2250 
    2251     if(abs(bufLevel) > costAvgFrameBits)
    2252     {
    2253       if(bufLevel < 0)
    2254       {
    2255         finalQP -= 2;
    2256       }
    2257       else
    2258       {
    2259         finalQP += 2;
    2260       }
    2261     }
    2262     m_indexRefFrame++;
    2263   }
    2264   finalQP = max(MIN_QP, min(MAX_QP, finalQP));
    2265 
    2266   for(Int indexLCU = 0 ; indexLCU < m_numUnitInFrame; indexLCU++)
    2267   {
    2268     m_pcLCUData[indexLCU].m_qp = finalQP;
    2269   }
    2270 
    2271   pcFrameData->m_isReferenced = isReferenced;
    2272   pcFrameData->m_qp           = finalQP;
    2273 
    2274   return finalQP;
    2275 }
    2276 
    2277 Bool  TEncRateCtrl::calculateUnitQP ()
    2278 {
    2279   if(!m_activeUnitLevelOn || m_indexLCU == 0)
    2280   {
    2281     return false;
    2282   }
    2283   Int upperQPBound, lowerQPBound, finalQP;
    2284   Int    colQP        = m_pcLCUData[m_indexLCU].m_qp;
    2285   Double colMAD       = m_pcLCUData[m_indexLCU].m_costMAD;
    2286   Double budgetInUnit = m_pcLCUData[m_indexLCU].m_pixels*m_costAvgbpp;
    2287 
    2288 
    2289   Int targetBitsOccupancy = (Int)(budgetInUnit - (m_occupancyVBInFrame/(m_numUnitInFrame-m_indexUnit)));
    2290   Int targetBitsLeftBudget= (Int)((m_remainingBitsInFrame*m_pcLCUData[m_indexLCU].m_pixels)/(Double)(m_numOfPixels-m_codedPixels));
    2291   Int targetBits = (targetBitsLeftBudget>>1) + (targetBitsOccupancy>>1);
    2292  
    2293 
    2294   if( m_indexLCU >= m_sourceWidthInLCU)
    2295   {
    2296     upperQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) + MAX_DELTA_QP;
    2297     lowerQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) - MAX_DELTA_QP;
    2298   }
    2299   else
    2300   {
    2301     upperQPBound = m_pcLCUData[m_indexLCU-1].m_qp + MAX_DELTA_QP;
    2302     lowerQPBound = m_pcLCUData[m_indexLCU-1].m_qp - MAX_DELTA_QP;
    2303   }
    2304 
    2305   if(targetBits < 0)
    2306   {
    2307     finalQP = m_pcLCUData[m_indexLCU-1].m_qp + 1;
    2308   }
    2309   else
    2310   {
    2311     finalQP = m_cPixelURQQuadraticModel.getQP(colQP, targetBits, m_pcLCUData[m_indexLCU].m_pixels, colMAD);
    2312   }
    2313  
    2314   finalQP = max(lowerQPBound, min(upperQPBound, finalQP));
    2315   m_pcLCUData[m_indexLCU].m_qp = max(MIN_QP, min(MAX_QP, finalQP));
    2316  
    2317   return true;
    2318 }
    2319 
    2320 Void  TEncRateCtrl::updateRCGOPStatus()
    2321 {
    2322   m_remainingBitsInGOP = ((m_currBitrate/m_frameRate)*m_sizeGOP) - m_occupancyVB;
    2323  
    2324   FrameData cFrameData = m_pcFrameData[m_sizeGOP];
    2325   initFrameData();
    2326 
    2327   m_pcFrameData[0]   = cFrameData;
    2328   m_indexGOP++;
    2329   m_indexFrame       = 0;
    2330   m_indexRefFrame    = 0;
    2331   m_indexNonRefFrame = 0;
    2332 }
    2333 
    2334 Void  TEncRateCtrl::updataRCFrameStatus(Int frameBits, SliceType eSliceType)
    2335 {
    2336   FrameData* pcFrameData = &m_pcFrameData[m_indexPOCInGOP];
    2337   Int occupancyBits;
    2338   Double adjustmentBits;
    2339 
    2340   m_remainingBitsInGOP = m_remainingBitsInGOP + ( ((m_currBitrate-m_prevBitrate)/m_frameRate)*(m_sizeGOP-m_indexFrame) ) - frameBits;
    2341   occupancyBits        = (Int)((Double)frameBits - (m_currBitrate/(Double)m_frameRate));
    2342  
    2343   if( (occupancyBits < 0) && (m_initialOVB > 0) )
    2344   {
    2345     adjustmentBits = xAdjustmentBits(occupancyBits, m_initialOVB );
    2346 
    2347     if(m_initialOVB < 0)
    2348     {
    2349       adjustmentBits = m_initialOVB;
    2350       occupancyBits += (Int)adjustmentBits;
    2351       m_initialOVB   =  0;
    2352     }
    2353   }
    2354   else if( (occupancyBits > 0) && (m_initialOVB < 0) )
    2355   {
    2356     adjustmentBits = xAdjustmentBits(m_initialOVB, occupancyBits );
    2357    
    2358     if(occupancyBits < 0)
    2359     {
    2360       adjustmentBits = occupancyBits;
    2361       m_initialOVB  += (Int)adjustmentBits;
    2362       occupancyBits  =  0;
    2363     }
    2364   }
    2365 
    2366   if(m_indexGOP == 0)
    2367   {
    2368     m_initialOVB = occupancyBits;
    2369   }
    2370   else
    2371   {
    2372     m_occupancyVB= m_occupancyVB + occupancyBits;
    2373   }
    2374 
    2375   if(pcFrameData->m_isReferenced)
    2376   {
    2377     m_costRefAvgWeighting  = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costRefAvgWeighting)/8.0);
    2378 
    2379     if(m_indexFrame == 0)
    2380     {
    2381       m_initialTBL = m_targetBufLevel  = (frameBits - (m_currBitrate/m_frameRate));
    2382     }
    2383     else
    2384     {
    2385       Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1;
    2386       m_targetBufLevel =  m_targetBufLevel
    2387                             - (m_initialTBL/(m_refFrameNum-1))
    2388                             + (Int)((m_costRefAvgWeighting*(distance+1)*m_currBitrate)/(m_frameRate*(m_costRefAvgWeighting+(m_costNonRefAvgWeighting*distance))))
    2389                             - (m_currBitrate/m_frameRate);
    2390     }
    2391 
    2392     if(m_cMADLinearModel.IsUpdateAvailable())
    2393     {
    2394       m_cMADLinearModel.updateMADLiearModel();
    2395     }
    2396 
    2397     if(eSliceType != I_SLICE &&
    2398        m_cPixelURQQuadraticModel.checkUpdateAvailable(pcFrameData->m_qp))
    2399     {
    2400       m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(pcFrameData->m_qp, pcFrameData->m_bits, m_numOfPixels, pcFrameData->m_costMAD);
    2401     }
    2402   }
    2403   else
    2404   {
    2405     m_costNonRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costNonRefAvgWeighting)/8.0);
    2406   }
    2407 
    2408   m_indexFrame++;
    2409   m_indexLCU             = 0;
    2410   m_indexUnit            = 0;
    2411   m_occupancyVBInFrame   = 0;
    2412   m_remainingBitsInFrame = 0;
    2413   m_codedPixels          = 0;
    2414   m_activeUnitLevelOn    = false;
    2415   m_costAvgbpp           = 0.0;
    2416 }
    2417 Void  TEncRateCtrl::updataRCUnitStatus ()
    2418 {
    2419   if(!m_activeUnitLevelOn || m_indexLCU == 0)
    2420   {
    2421     return;
    2422   }
    2423 
    2424   m_codedPixels  += m_pcLCUData[m_indexLCU-1].m_pixels;
    2425   m_remainingBitsInFrame = m_remainingBitsInFrame - m_pcLCUData[m_indexLCU-1].m_bits;
    2426   m_occupancyVBInFrame   = (Int)(m_occupancyVBInFrame + m_pcLCUData[m_indexLCU-1].m_bits - m_pcLCUData[m_indexLCU-1].m_pixels*m_costAvgbpp);
    2427 
    2428   if( m_cPixelURQQuadraticModel.checkUpdateAvailable(m_pcLCUData[m_indexLCU-1].m_qp) )
    2429   {
    2430     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);
    2431   }
    2432 
    2433   m_indexUnit++;
    2434 }
    2435 
    2436 Void  TEncRateCtrl::updateFrameData(UInt64 actualFrameBits)
    2437 {
    2438   Double costMAD = 0.0;
    2439  
    2440   for(Int i = 0; i < m_numUnitInFrame; i++)
    2441   {
    2442     costMAD    += m_pcLCUData[i].m_costMAD;
    2443   }
    2444  
    2445   m_pcFrameData[m_indexPOCInGOP].m_costMAD = (costMAD/(Double)m_numUnitInFrame);
    2446   m_pcFrameData[m_indexPOCInGOP].m_bits    = (Int)actualFrameBits;
    2447  
    2448   if(m_pcFrameData[m_indexPOCInGOP].m_isReferenced)
    2449   {
    2450     m_indexPrevPOCInGOP = m_indexPOCInGOP;
    2451     m_cMADLinearModel.updateMADHistory(m_pcFrameData[m_indexPOCInGOP].m_costMAD);
    2452   }
    2453 }
    2454 
    2455 Void  TEncRateCtrl::updateLCUData(TComDataCU* pcCU, UInt64 actualLCUBits, Int qp)
    2456 {
    2457   Int     x, y;
    2458   Double  costMAD = 0.0;
    2459 
    2460   Pel*  pOrg   = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
    2461   Pel*  pRec   = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0);
    2462   Int   stride = pcCU->getPic()->getStride();
    2463 
    2464   Int   width  = m_pcLCUData[m_indexLCU].m_widthInPixel;
    2465   Int   height = m_pcLCUData[m_indexLCU].m_heightInPixel;
    2466 
    2467   for( y = 0; y < height; y++ )
    2468   {
    2469     for( x = 0; x < width; x++ )
    2470     {
    2471       costMAD += abs( pOrg[x] - pRec[x] );
    2472     }
    2473     pOrg += stride;
    2474     pRec += stride;
    2475   }
    2476   m_pcLCUData[m_indexLCU  ].m_qp      = qp;
    2477   m_pcLCUData[m_indexLCU  ].m_costMAD = (costMAD /(Double)(width*height));
    2478   m_pcLCUData[m_indexLCU++].m_bits    = (Int)actualLCUBits;
    2479 }
    2480 
    2481 
    2482 #if KWU_RC_MADPRED_E0227
    2483 Void  TEncRateCtrl::updateLCUDataEnhancedView(TComDataCU* pcCU, UInt64 uiBits, Int qp, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction)
    2484 {
    2485   Int     x, y;
    2486   Double dMAD = 0.0;
    2487   Int Sum = 0;
    2488   Double SAD = 0.0;
    2489 
    2490   Pel*  pOrg    = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
    2491   Pel*  pRec    = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0);
    2492   Pel*  pDep    = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
    2493   Int   iStride = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getStride();
    2494 
    2495   Int   width  = m_pcLCUData[m_indexLCU].m_widthInPixel;
    2496   Int   height = m_pcLCUData[m_indexLCU].m_heightInPixel;
    2497 
    2498   for( y = 0 ; y < pcCU->getSlice()->getSPS()->getMaxCUHeight() ; y+=8)
    2499   {
    2500     for( x = 0 ; x < pcCU->getSlice()->getSPS()->getMaxCUWidth() ; x+=8)
    2501     {
    2502       Sum += pDep[x];
    2503     }
    2504     pDep += iStride;
    2505   }
    2506 
    2507   Double AvgDepth = (Double)Sum/((pcCU->getSlice()->getSPS()->getMaxCUHeight()/8)*(pcCU->getSlice()->getSPS()->getMaxCUWidth()/8));
    2508   Double fL = focalLen * abs( basePos - curPos );
    2509   Double z  = abs( 1.0 / znear - 1.0 / zfar ) * ((Double)(AvgDepth) / (( 1 << g_bitDepthY ) - 1) ) + abs(1.0 / zfar);
    2510   Int   disparity = (Int)(direction*fL * z);
    2511   Int shift = DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8);
    2512   Int disp = disparity;
    2513 
    2514   for( y = 0; y < height; y++ )
    2515   {
    2516     for( x = 0; x < width; x++ )
    2517     {
    2518       SAD += abs( pOrg[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)]
    2519       - pRec[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] )>>shift;
    2520     }
    2521     pOrg += iStride;
    2522     pRec += iStride;
    2523   }
    2524   m_pcLCUData[m_indexLCU].m_qp   = qp;
    2525   m_pcLCUData[m_indexLCU].m_costMAD  = (SAD /(Double)(width*height));
    2526   m_pcLCUData[m_indexLCU].m_bits = (Int)uiBits;
    2527 }
    2528 #endif
    2529 
    2530 
    2531 Double TEncRateCtrl::xAdjustmentBits(Int& reductionBits, Int& compensationBits)
    2532 {
    2533   Double adjustment  = ADJUSTMENT_FACTOR*reductionBits;
    2534   reductionBits     -= (Int)adjustment;
    2535   compensationBits  += (Int)adjustment;
    2536 
    2537   return adjustment;
    2538 }
    2539 
    2540 #endif
    2541 
Note: See TracChangeset for help on using the changeset viewer.