Changeset 532 in SHVCSoftware for branches/SHM-4.1-dev/source/Lib/TLibEncoder/TEncRateCtrl.cpp
- Timestamp:
- 2 Jan 2014, 07:21:30 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SHM-4.1-dev/source/Lib/TLibEncoder/TEncRateCtrl.cpp
r313 r532 42 42 using namespace std; 43 43 44 #if RATE_CONTROL_LAMBDA_DOMAIN45 46 44 //sequence level 47 45 TEncRCSeq::TEncRCSeq() … … 67 65 m_bitsLeft = 0; 68 66 m_useLCUSeparateModel = false; 69 #if M0036_RC_IMPROVEMENT70 67 m_adaptiveBit = 0; 71 68 m_lastLambda = 0.0; 72 #endif73 69 } 74 70 … … 78 74 } 79 75 80 #if M0036_RC_IMPROVEMENT81 76 Void 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 #else83 Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel )84 #endif85 77 { 86 78 destroy(); … … 109 101 m_betaUpdate = 0.025; 110 102 } 111 #if M0036_RC_IMPROVEMENT112 103 else if ( m_seqTargetBpp < 0.2 ) 113 104 { … … 125 116 m_betaUpdate = 0.2; 126 117 } 127 #else128 else129 {130 m_alphaUpdate = 0.1;131 m_betaUpdate = 0.05;132 }133 #endif134 118 m_averageBits = (Int)(m_targetBits / totalFrames); 135 119 Int picWidthInBU = ( m_picWidth % m_LCUWidth ) == 0 ? m_picWidth / m_LCUWidth : m_picWidth / m_LCUWidth + 1; … … 172 156 m_framesLeft = m_totalFrames; 173 157 m_bitsLeft = m_targetBits; 174 #if M0036_RC_IMPROVEMENT175 158 m_adaptiveBit = adaptiveBit; 176 159 m_lastLambda = 0.0; 177 #endif178 160 } 179 161 … … 233 215 for ( Int i=0; i<m_numberOfLevel; i++ ) 234 216 { 235 #if RATE_CONTROL_INTRA236 217 if (i>0) 237 218 { … … 244 225 m_picPara[i].m_beta = BETA2; 245 226 } 246 #else247 m_picPara[i].m_alpha = 3.2003;248 m_picPara[i].m_beta = -1.367;249 #endif250 227 } 251 228 } … … 271 248 for ( Int j=0; j<m_numberOfLCU; j++) 272 249 { 273 #if RATE_CONTROL_INTRA274 250 m_LCUPara[i][j].m_alpha = m_picPara[i].m_alpha; 275 251 m_LCUPara[i][j].m_beta = m_picPara[i].m_beta; 276 #else277 m_LCUPara[i][j].m_alpha = 3.2003;278 m_LCUPara[i][j].m_beta = -1.367;279 #endif280 252 } 281 253 } … … 299 271 } 300 272 301 #if !RATE_CONTROL_INTRA302 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 #endif316 317 #if M0036_RC_IMPROVEMENT318 273 Void TEncRCSeq::setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB ) 319 274 { … … 326 281 delete[] bitsRatio; 327 282 } 328 #endif329 283 330 284 //GOP level … … 349 303 Int targetBits = xEstGOPTargetBits( encRCSeq, numPic ); 350 304 351 #if M0036_RC_IMPROVEMENT352 305 if ( encRCSeq->getAdaptiveBits() > 0 && encRCSeq->getLastLambda() > 0.1 ) 353 306 { … … 409 362 delete []equaCoeffB; 410 363 } 411 #endif412 364 413 365 m_picTargetBitInGOP = new Int[numPic]; … … 422 374 { 423 375 currPicRatio = encRCSeq->getBitRatio( i ); 424 #if M0036_RC_IMPROVEMENT425 376 m_picTargetBitInGOP[i] = (Int)( ((Double)targetBits) * currPicRatio / totalPicRatio ); 426 #else427 m_picTargetBitInGOP[i] = targetBits * currPicRatio / totalPicRatio;428 #endif429 377 } 430 378 … … 436 384 } 437 385 438 #if M0036_RC_IMPROVEMENT439 386 Void TEncRCGOP::xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) 440 387 { … … 482 429 return solution; 483 430 } 484 #endif485 431 486 432 Void TEncRCGOP::destroy() … … 534 480 535 481 m_LCUs = NULL; 536 #if !M0036_RC_IMPROVEMENT537 m_lastPicture = NULL;538 #endif539 482 m_picActualHeaderBits = 0; 540 #if !M0036_RC_IMPROVEMENT541 m_totalMAD = 0.0;542 #endif543 483 m_picActualBits = 0; 544 484 m_picQP = 0; … … 565 505 } 566 506 567 #if M0036_RC_IMPROVEMENT568 507 targetBits = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio ); 569 #else570 targetBits = Int( GOPbitsLeft * currPicRatio / totalPicRatio );571 #endif572 508 573 509 if ( targetBits < 100 ) … … 665 601 m_LCUs[LCUIdx].m_lambda = 0.0; 666 602 m_LCUs[LCUIdx].m_targetBits = 0; 667 #if M0036_RC_IMPROVEMENT668 603 m_LCUs[LCUIdx].m_bitWeight = 1.0; 669 #else670 m_LCUs[LCUIdx].m_MAD = 0.0;671 #endif672 604 Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); 673 605 Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); … … 676 608 } 677 609 m_picActualHeaderBits = 0; 678 #if !M0036_RC_IMPROVEMENT679 m_totalMAD = 0.0;680 #endif681 610 m_picActualBits = 0; 682 611 m_picQP = 0; 683 612 m_picLambda = 0.0; 684 685 #if !M0036_RC_IMPROVEMENT686 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 #endif697 613 } 698 614 … … 709 625 710 626 711 #if RATE_CONTROL_INTRA712 627 Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType) 713 #else714 Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures )715 #endif716 628 { 717 629 Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; 718 630 Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; 719 631 Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; 720 #if RATE_CONTROL_INTRA721 632 Double estLambda; 722 633 if (eSliceType == I_SLICE) … … 728 639 estLambda = alpha * pow( bpp, beta ); 729 640 } 730 #else731 Double estLambda = alpha * pow( bpp, beta );732 #endif733 641 734 642 Double lastLevelLambda = -1.0; … … 778 686 m_estPicLambda = estLambda; 779 687 780 #if M0036_RC_IMPROVEMENT781 688 Double totalWeight = 0.0; 782 689 // initial BU bit allocation weight 783 690 for ( Int i=0; i<m_numberOfLCU; i++ ) 784 691 { 785 #if RC_FIX786 692 Double alphaLCU, betaLCU; 787 693 if ( m_encRCSeq->getUseLCUSeparateModel() ) … … 795 701 betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; 796 702 } 797 #else798 Double alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha;799 Double betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta;800 #endif801 703 802 704 m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU ); … … 813 715 m_LCUs[i].m_bitWeight = BUTargetBits; 814 716 } 815 #endif816 717 817 718 return estLambda; … … 856 757 } 857 758 858 #if RATE_CONTROL_INTRA859 759 Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) 860 #else861 Double TEncRCPic::getLCUTargetBpp()862 #endif863 760 { 864 761 Int LCUIdx = getLCUCoded(); 865 762 Double bpp = -1.0; 866 763 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 { 874 767 Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1; 875 768 Int bitrateWindow = min(4,noOfLCUsLeft); … … 889 782 else 890 783 { 891 #endif892 #if M0036_RC_IMPROVEMENT893 784 Double totalWeight = 0; 894 785 for ( Int i=LCUIdx; i<m_numberOfLCU; i++ ) … … 898 789 Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() ); 899 790 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 } 927 792 928 793 if ( avgBits < 1 ) … … 1050 915 beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); 1051 916 1052 #if M0036_RC_IMPROVEMENT1053 917 alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); 1054 918 beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); 1055 #else1056 alpha = Clip3( 0.05, 20.0, alpha );1057 beta = Clip3( -3.0, -0.1, beta );1058 #endif1059 919 1060 920 TRCParameter rcPara; … … 1069 929 alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; 1070 930 double lnbpp = log( bpp ); 1071 #if M0036_RC_IMPROVEMENT1072 931 lnbpp = Clip3( -5.0, -0.1, lnbpp ); 1073 #else1074 lnbpp = Clip3( -5.0, 1.0, lnbpp );1075 #endif1076 932 beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; 1077 933 1078 #if M0036_RC_IMPROVEMENT1079 934 alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); 1080 935 beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); 1081 #else1082 alpha = Clip3( 0.05, 20.0, alpha );1083 beta = Clip3( -3.0, -0.1, beta );1084 #endif1085 936 TRCParameter rcPara; 1086 937 rcPara.m_alpha = alpha; … … 1090 941 } 1091 942 1092 #if !M0036_RC_IMPROVEMENT1093 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 #endif1111 1112 943 Double TEncRCPic::calAverageQP() 1113 944 { … … 1164 995 } 1165 996 1166 #if M0036_RC_IMPROVEMENT1167 #if RATE_CONTROL_INTRA1168 997 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType) 1169 #else1170 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda )1171 #endif1172 #else1173 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage )1174 #endif1175 998 { 1176 999 m_picActualHeaderBits = actualHeaderBits; … … 1185 1008 } 1186 1009 m_picLambda = averageLambda; 1187 #if !M0036_RC_IMPROVEMENT1188 for ( Int i=0; i<m_numberOfLCU; i++ )1189 {1190 m_totalMAD += m_LCUs[i].m_MAD;1191 }1192 #endif1193 1010 1194 1011 Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; 1195 1012 Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; 1196 #if RATE_CONTROL_INTRA1197 1013 if (eSliceType == I_SLICE) 1198 1014 { … … 1201 1017 else 1202 1018 { 1203 #endif1204 1019 // update parameters 1205 1020 Double picActualBits = ( Double )m_picActualBits; … … 1208 1023 Double inputLambda = m_picLambda; 1209 1024 1210 #if M0036_RC_IMPROVEMENT1211 1025 if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 ) 1212 #else1213 if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 || effectivePercentage < 0.05 )1214 #endif1215 1026 { 1216 1027 alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); 1217 1028 beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); 1218 1029 1219 #if M0036_RC_IMPROVEMENT1220 1030 alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); 1221 1031 beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); 1222 #else1223 alpha = Clip3( 0.05, 20.0, alpha );1224 beta = Clip3( -3.0, -0.1, beta );1225 #endif1226 1032 TRCParameter rcPara; 1227 1033 rcPara.m_alpha = alpha; … … 1235 1041 alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; 1236 1042 double lnbpp = log( picActualBpp ); 1237 #if M0036_RC_IMPROVEMENT1238 1043 lnbpp = Clip3( -5.0, -0.1, lnbpp ); 1239 #else1240 lnbpp = Clip3( -5.0, 1.0, lnbpp );1241 #endif1242 1044 beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; 1243 1045 1244 #if M0036_RC_IMPROVEMENT1245 1046 alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); 1246 1047 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 } 1254 1049 1255 1050 TRCParameter rcPara; … … 1259 1054 m_encRCSeq->setPicPara( m_frameLevel, rcPara ); 1260 1055 1261 #if M0036_RC_IMPROVEMENT1262 1056 if ( m_frameLevel == 1 ) 1263 1057 { … … 1266 1060 m_encRCSeq->setLastLambda( updateLastLambda ); 1267 1061 } 1268 #endif 1269 } 1270 1271 #if RATE_CONTROL_INTRA 1062 } 1063 1272 1064 Int TEncRCPic::getRefineBitsForIntra( Int orgBits ) 1273 1065 { … … 1358 1150 return estLambda; 1359 1151 } 1360 #endif1361 1152 1362 1153 TEncRateCtrl::TEncRateCtrl() … … 1392 1183 } 1393 1184 1394 #if M0036_RC_IMPROVEMENT1395 1185 Void 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 #else1397 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 #endif1399 1186 { 1400 1187 destroy(); … … 1411 1198 1412 1199 Int numberOfLevel = 1; 1413 #if M0036_RC_IMPROVEMENT1414 1200 Int adaptiveBit = 0; 1415 1201 if ( keepHierBits > 0 ) 1416 #else1417 if ( keepHierBits )1418 #endif1419 1202 { 1420 1203 numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; … … 1439 1222 } 1440 1223 1441 #if M0036_RC_IMPROVEMENT1442 1224 if ( keepHierBits > 0 ) 1443 #else1444 if ( keepHierBits )1445 #endif1446 1225 { 1447 1226 Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); … … 1476 1255 bitsRatio[3] = 14; 1477 1256 } 1478 #if M0036_RC_IMPROVEMENT1479 1257 if ( keepHierBits == 2 ) 1480 1258 { 1481 1259 adaptiveBit = 1; 1482 1260 } 1483 #endif1484 1261 } 1485 1262 else if ( GOPSize == 8 && !isLowdelay ) … … 1529 1306 bitsRatio[7] = 1; 1530 1307 } 1531 #if M0036_RC_IMPROVEMENT1532 1308 if ( keepHierBits == 2 ) 1533 1309 { 1534 1310 adaptiveBit = 2; 1535 1311 } 1536 #endif1537 1312 } 1538 1313 else 1539 1314 { 1540 #if M0036_RC_IMPROVEMENT1541 1315 printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" ); 1542 #else1543 printf( "\n hierarchical bit allocation is not support for the specified coding structure currently." );1544 #endif1545 1316 } 1546 1317 } … … 1555 1326 } 1556 1327 } 1557 #if M0036_RC_IMPROVEMENT1558 1328 if ( keepHierBits > 0 ) 1559 #else1560 if ( keepHierBits )1561 #endif1562 1329 { 1563 1330 if ( GOPSize == 4 && isLowdelay ) … … 1594 1361 1595 1362 m_encRCSeq = new TEncRCSeq; 1596 #if M0036_RC_IMPROVEMENT1597 1363 m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit ); 1598 #else1599 m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel );1600 #endif1601 1364 m_encRCSeq->initBitsRatio( bitsRatio ); 1602 1365 m_encRCSeq->initGOPID2Level( GOPID2Level ); … … 1629 1392 } 1630 1393 1631 #else1632 1633 #define ADJUSTMENT_FACTOR 0.601634 #define HIGH_QSTEP_THRESHOLD 9.52381635 #define HIGH_QSTEP_ALPHA 4.93711636 #define HIGH_QSTEP_BETA 0.09221637 #define LOW_QSTEP_ALPHA 16.74291638 #define LOW_QSTEP_BETA -1.14941639 1640 #define MAD_PRED_Y1 1.01641 #define MAD_PRED_Y2 0.01642 1643 enum MAD_HISOTRY {1644 MAD_PPPrevious = 0,1645 MAD_PPrevious = 1,1646 MAD_Previous = 21647 };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 else1706 {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 else1727 {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 else1803 {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 bps1818 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 else1930 {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 else1944 {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_qp1949 ||(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 else1958 {1959 finalQP = bwdQP+2;1960 }1961 m_indexNonRefFrame++;1962 }1963 }1964 else1965 {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 else1991 {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 else2033 {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 else2043 {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 else2104 {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 else2117 {2118 Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1;2119 m_targetBufLevel = m_targetBufLevel2120 - (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 else2137 {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 #endif2224
Note: See TracChangeset for help on using the changeset viewer.