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


Ignore:
Timestamp:
23 Oct 2013, 23:01:30 (11 years ago)
Author:
tech
Message:

Merged 8.1-Cleanup@654

File:
1 edited

Legend:

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

    r608 r655  
    537537  m_lastPicture  = NULL;
    538538#endif
     539
     540#if KWU_RC_MADPRED_E0227
     541  m_lastIVPicture = NULL;
     542#endif
     543
    539544  m_picActualHeaderBits = 0;
    540545#if !M0036_RC_IMPROVEMENT
     
    544549  m_picQP               = 0;
    545550  m_picLambda           = 0.0;
     551
     552#if KWU_RC_MADPRED_E0227
     553  m_IVtotalMAD            = 0.0;
     554#endif
    546555}
    547556
     
    621630}
    622631
     632#if KWU_RC_MADPRED_E0227
     633Void TEncRCPic::addToPictureLsitIV( list<TEncRCPic*>& listPreviousPictures )
     634{
     635  m_lastIVPicture = NULL;
     636  m_lastIVPicture = this;
     637}
     638
     639Void TEncRCPic::setIVPic( TEncRCPic* BaseRCPic )
     640{
     641  m_lastIVPicture = BaseRCPic;
     642}
     643#endif
     644
     645#if KWU_RC_MADPRED_E0227
     646Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures, Int layerID )
     647#else
    623648Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures )
     649#endif
    624650{
    625651  destroy();
     
    673699      Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight );
    674700      m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight;
     701
     702#if KWU_RC_MADPRED_E0227
     703      m_LCUs[LCUIdx].m_CUWidth = currWidth;
     704      m_LCUs[LCUIdx].m_CUHeight = currHeight;
     705      m_LCUs[LCUIdx].m_IVMAD = -1.0;
     706#endif
    675707    }
    676708  }
     
    683715  m_picLambda           = 0.0;
    684716
     717
     718#if KWU_RC_MADPRED_E0227
     719  m_LayerID = layerID;
     720  m_lastIVPicture = NULL;
     721  m_IVtotalMAD            = 0.0;
     722#endif
     723
     724
    685725#if !M0036_RC_IMPROVEMENT
    686726  m_lastPicture = NULL;
    687727  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
     738#if KWU_RC_MADPRED_E0227
     739  list<TEncRCPic*>::reverse_iterator it;
     740  if( m_LayerID != 0)
     741  {
     742    m_lastIVPicture = NULL;
     743    for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ )
     744    {
     745      if ( (*it)->getLayerID() == 0 )
     746      {
     747        m_lastIVPicture = (*it);
     748        break;
     749      }
     750    }
     751  }
     752
     753  m_lastPicture = NULL;
    688754  for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ )
    689755  {
     
    856922}
    857923
     924
     925#if KWU_RC_MADPRED_E0227
     926Double TEncRCPic::estimatePicLambdaIV( list<TEncRCPic*>& listPreviousPictures, Int CurPOC )
     927{
     928  Double alpha         = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
     929  Double beta          = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
     930  Double bpp       = (Double)m_targetBits/(Double)m_numberOfPixel;
     931  Double estLambda = alpha * pow( bpp, beta );
     932  Double lastLevelLambda = -1.0;
     933  Double lastPicLambda   = -1.0;
     934  Double lastValidLambda = -1.0;
     935  list<TEncRCPic*>::iterator it;
     936
     937  if(listPreviousPictures.size() == 0 || CurPOC%8 == 0)
     938  {
     939    lastLevelLambda = m_lastIVPicture->getPicActualLambda();
     940    lastPicLambda     = m_lastIVPicture->getPicActualLambda();
     941  }
     942  else
     943  {
     944    for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
     945    {
     946      if ( (*it)->getFrameLevel() == m_frameLevel )
     947      {
     948        lastLevelLambda = (*it)->getPicActualLambda();
     949      }
     950      lastPicLambda     = (*it)->getPicActualLambda();
     951
     952      if ( lastPicLambda > 0.0 )
     953      {
     954        lastValidLambda = lastPicLambda;
     955      }
     956    }
     957  }
     958
     959  if ( lastLevelLambda > 0.0 )
     960  {
     961    lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda );
     962    estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda );
     963  }
     964
     965  if ( lastPicLambda > 0.0 )
     966  {
     967    lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda );
     968    estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda );
     969  }
     970  else if ( lastValidLambda > 0.0 )
     971  {
     972    lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda );
     973    estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda );
     974  }
     975  else
     976  {
     977    estLambda = Clip3( 0.1, 10000.0, estLambda );
     978  }
     979
     980  if ( estLambda < 0.1 )
     981  {
     982    estLambda = 0.1;
     983  }
     984
     985  m_estPicLambda = estLambda;
     986  return estLambda;
     987}
     988#endif
     989
     990
    858991#if RATE_CONTROL_INTRA
    859992Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) 
     
    9371070}
    9381071
     1072
     1073#if KWU_RC_MADPRED_E0227
     1074Double 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
     1084  Double totalMAD = -1.0;
     1085  Double MAD      = -1.0;
     1086
     1087  Double IVMAD      = -1.0;
     1088  Double SAD = 0.0;
     1089  Int     x, y;
     1090  Int Sum = 0;
     1091
     1092  {
     1093    Pel*  pOrg    = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
     1094    Pel*  pRec    = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0);
     1095    Pel*  pDep    = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0);
     1096    Int   iStride = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getStride();
     1097
     1098    Int   width  = m_LCUs[ LCUIdx ].m_CUWidth;
     1099    Int   height = m_LCUs[ LCUIdx ].m_CUHeight;
     1100
     1101    for( y = 0 ; y < pcCU->getSlice()->getSPS()->getMaxCUHeight() ; y+=8)
     1102    {
     1103      for( x = 0 ; x < pcCU->getSlice()->getSPS()->getMaxCUWidth() ; x+=8)
     1104      {
     1105        Sum += pDep[x];
     1106      }
     1107      pDep += iStride;
     1108    }
     1109
     1110    Double AvgDepth = (Double)Sum/((pcCU->getSlice()->getSPS()->getMaxCUHeight()/8)*(pcCU->getSlice()->getSPS()->getMaxCUWidth()/8));
     1111
     1112    Double fL = focalLen * abs( basePos - curPos );
     1113    Double z  = abs( 1.0 / znear - 1.0 / zfar ) * ((Double)(AvgDepth) / (( 1 << g_bitDepthY ) - 1) ) + abs(1.0 / zfar);
     1114    *disparity = (Int)(direction*fL * z);
     1115    Int shift = DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8);
     1116
     1117    Int disp = *disparity;
     1118    Int posX, posY;
     1119    pcCU->getPosInPic(0, posX, posY);
     1120    if((posX + *disparity) < 0 || (posX + *disparity + width) >= pcCU->getSlice()->getSPS()->getMaxCUWidth())
     1121    {
     1122      disp = 0;
     1123    }
     1124
     1125    for( y = 0; y < height; y++ )
     1126    {
     1127      for( x = 0; x < width; x++ )
     1128      {
     1129        SAD += abs( pOrg[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)]
     1130                  - pRec[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] )>>shift;
     1131      }
     1132      pOrg += iStride;
     1133      pRec += iStride;
     1134    }
     1135    IVMAD = SAD / (Double)(height * width);
     1136    IVMAD = IVMAD * IVMAD;
     1137
     1138    m_LCUs[ LCUIdx ].m_IVMAD = IVMAD;
     1139    if(m_lastPicture)
     1140    {
     1141      m_LCUs[ LCUIdx ].m_MAD = m_lastPicture->getLCU(LCUIdx).m_MAD;
     1142    }
     1143
     1144    MAD = m_LCUs[ LCUIdx ].m_IVMAD;
     1145
     1146    if(m_lastPicture)
     1147    {
     1148      totalMAD = m_lastPicture->getTotalMAD();      // get total MAD of temporal frame
     1149      for ( Int i=0; i<LCUIdx; i++ )
     1150      {
     1151        totalMAD -= m_lastPicture->getLCU(i).m_MAD;
     1152      }
     1153    }
     1154    else
     1155    {
     1156      totalMAD = m_lastIVPicture->getTotalMAD();      // get total MAD of inter-view frame
     1157      for ( Int i=0; i<LCUIdx; i++ )
     1158      {
     1159        totalMAD -= m_lastIVPicture->getLCU(i).m_MAD;
     1160      }
     1161    }
     1162
     1163
     1164    if ( totalMAD > 0.1 )
     1165    {
     1166      avgBits = Int( (m_bitsLeft * MAD) / totalMAD );
     1167    }
     1168    else
     1169    {
     1170      avgBits = Int( (m_bitsLeft) / m_LCULeft );
     1171    }
     1172  }
     1173
     1174  if ( avgBits < 5 )
     1175  {
     1176    avgBits = 5;
     1177  }
     1178
     1179  bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel;
     1180  m_LCUs[ LCUIdx ].m_targetBits = avgBits;
     1181
     1182  return bpp;
     1183}
     1184#endif
     1185
     1186
     1187
     1188
    9391189Double TEncRCPic::getLCUEstLambda( Double bpp )
    9401190{
     
    11851435  }
    11861436  m_picLambda           = averageLambda;
    1187 #if !M0036_RC_IMPROVEMENT
     1437#if !M0036_RC_IMPROVEMENT || KWU_RC_MADPRED_E0227
     1438#if KWU_RC_MADPRED_E0227
     1439  m_totalMAD = 0;
     1440#endif
    11881441  for ( Int i=0; i<m_numberOfLCU; i++ )
    11891442  {
     
    13931646
    13941647#if M0036_RC_IMPROVEMENT
     1648#if KWU_RC_MADPRED_E0227
     1649Void 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 )
     1650#else
    13951651Void 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] )
     1652#endif
    13961653#else
    13971654Void 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] )
     
    16071864  }
    16081865
     1866#if KWU_RC_MADPRED_E0227
     1867  setLayerID(layerID);
     1868#endif
     1869
    16091870  delete[] bitsRatio;
    16101871  delete[] GOPID2Level;
     
    16141875{
    16151876  m_encRCPic = new TEncRCPic;
     1877#if KWU_RC_MADPRED_E0227
     1878  m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures, m_LayerID );
     1879#else
    16161880  m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures );
     1881#endif
    16171882}
    16181883
     
    18152080  m_sourceHeightInLCU        = (sourceHeight / maxCUHeight) + (( sourceHeight %  maxCUHeight) ? 1 : 0); 
    18162081  m_isLowdelay               = (sizeIntraPeriod == -1) ? true : false;
     2082
    18172083  m_prevBitrate              = ( targetKbps << 10 );  // in units of 1,024 bps
    18182084  m_currBitrate              = ( targetKbps << 10 );
     2085
    18192086  m_frameRate                = frameRate;
    18202087  m_refFrameNum              = m_isLowdelay ? (sizeGOP) : (sizeGOP>>1);
     
    22122479}
    22132480
     2481
     2482#if KWU_RC_MADPRED_E0227
     2483Void  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
    22142531Double TEncRateCtrl::xAdjustmentBits(Int& reductionBits, Int& compensationBits)
    22152532{
Note: See TracChangeset for help on using the changeset viewer.