Changeset 872 in 3DVCSoftware for trunk/source/Lib/TLibEncoder/TEncRateCtrl.cpp
- Timestamp:
- 27 Mar 2014, 10:11:29 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/source/Lib/TLibEncoder/TEncRateCtrl.cpp
r655 r872 4 4 * granted under this license. 5 5 * 6 * Copyright (c) 2010-2013, ITU/ISO/IEC6 * Copyright (c) 2010-2014, ITU/ISO/IEC 7 7 * All rights reserved. 8 8 * … … 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 540 482 #if KWU_RC_MADPRED_E0227 541 483 m_lastIVPicture = NULL; … … 543 485 544 486 m_picActualHeaderBits = 0; 545 #if !M0036_RC_IMPROVEMENT546 m_totalMAD = 0.0;547 #endif548 487 m_picActualBits = 0; 549 488 m_picQP = 0; … … 574 513 } 575 514 576 #if M0036_RC_IMPROVEMENT577 515 targetBits = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio ); 578 #else579 targetBits = Int( GOPbitsLeft * currPicRatio / totalPicRatio );580 #endif581 516 582 517 if ( targetBits < 100 ) … … 691 626 m_LCUs[LCUIdx].m_lambda = 0.0; 692 627 m_LCUs[LCUIdx].m_targetBits = 0; 693 #if M0036_RC_IMPROVEMENT694 628 m_LCUs[LCUIdx].m_bitWeight = 1.0; 695 #else696 m_LCUs[LCUIdx].m_MAD = 0.0;697 #endif698 629 Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); 699 630 Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); … … 708 639 } 709 640 m_picActualHeaderBits = 0; 710 #if !M0036_RC_IMPROVEMENT711 m_totalMAD = 0.0;712 #endif713 641 m_picActualBits = 0; 714 642 m_picQP = 0; … … 723 651 724 652 725 #if !M0036_RC_IMPROVEMENT726 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 #endif737 738 653 #if KWU_RC_MADPRED_E0227 739 654 list<TEncRCPic*>::reverse_iterator it; … … 775 690 776 691 777 #if RATE_CONTROL_INTRA778 692 Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType) 779 #else780 Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures )781 #endif782 693 { 783 694 Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; 784 695 Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; 785 696 Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; 786 #if RATE_CONTROL_INTRA787 697 Double estLambda; 788 698 if (eSliceType == I_SLICE) … … 794 704 estLambda = alpha * pow( bpp, beta ); 795 705 } 796 #else797 Double estLambda = alpha * pow( bpp, beta );798 #endif799 706 800 707 Double lastLevelLambda = -1.0; … … 844 751 m_estPicLambda = estLambda; 845 752 846 #if M0036_RC_IMPROVEMENT847 753 Double totalWeight = 0.0; 848 754 // initial BU bit allocation weight 849 755 for ( Int i=0; i<m_numberOfLCU; i++ ) 850 756 { 851 #if RC_FIX852 757 Double alphaLCU, betaLCU; 853 758 if ( m_encRCSeq->getUseLCUSeparateModel() ) … … 861 766 betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; 862 767 } 863 #else864 Double alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha;865 Double betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta;866 #endif867 768 868 769 m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU ); … … 879 780 m_LCUs[i].m_bitWeight = BUTargetBits; 880 781 } 881 #endif882 782 883 783 return estLambda; … … 989 889 990 890 991 #if RATE_CONTROL_INTRA992 891 Double 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 938 Double TEncRCPic::getLCUTargetBppforInterView( list<TEncRCPic*>& listPreviousPictures, TComDataCU* pcCU, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction, Int* disparity ) 996 939 { 997 940 Int LCUIdx = getLCUCoded(); … … 1003 946 #endif 1004 947 1005 #if RATE_CONTROL_INTRA1006 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 else1017 {1018 avgBits = Int( m_bitsLeft / m_LCULeft );1019 }1020 m_remainingCostIntra -= MAD;1021 }1022 else1023 {1024 #endif1025 #if M0036_RC_IMPROVEMENT1026 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 #else1034 if ( m_lastPicture == NULL )1035 {1036 avgBits = Int( m_bitsLeft / m_LCULeft );1037 }1038 else1039 {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 else1052 {1053 avgBits = Int( m_bitsLeft / m_LCULeft );1054 }1055 }1056 #endif1057 #if RATE_CONTROL_INTRA1058 }1059 #endif1060 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_E02271074 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_IMPROVEMENT1080 Double totalMAD = -1.0;1081 Double MAD = -1.0;1082 #endif1083 1084 948 Double totalMAD = -1.0; 1085 949 Double MAD = -1.0; … … 1300 1164 beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); 1301 1165 1302 #if M0036_RC_IMPROVEMENT1303 1166 alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); 1304 1167 beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); 1305 #else1306 alpha = Clip3( 0.05, 20.0, alpha );1307 beta = Clip3( -3.0, -0.1, beta );1308 #endif1309 1168 1310 1169 TRCParameter rcPara; … … 1319 1178 alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; 1320 1179 double lnbpp = log( bpp ); 1321 #if M0036_RC_IMPROVEMENT1322 1180 lnbpp = Clip3( -5.0, -0.1, lnbpp ); 1323 #else1324 lnbpp = Clip3( -5.0, 1.0, lnbpp );1325 #endif1326 1181 beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; 1327 1182 1328 #if M0036_RC_IMPROVEMENT1329 1183 alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); 1330 1184 beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); 1331 #else1332 alpha = Clip3( 0.05, 20.0, alpha );1333 beta = Clip3( -3.0, -0.1, beta );1334 #endif1335 1185 TRCParameter rcPara; 1336 1186 rcPara.m_alpha = alpha; … … 1340 1190 } 1341 1191 1342 #if !M0036_RC_IMPROVEMENT1343 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 #endif1361 1362 1192 Double TEncRCPic::calAverageQP() 1363 1193 { … … 1414 1244 } 1415 1245 1416 #if M0036_RC_IMPROVEMENT1417 #if RATE_CONTROL_INTRA1418 1246 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType) 1419 #else1420 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda )1421 #endif1422 #else1423 Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage )1424 #endif1425 1247 { 1426 1248 m_picActualHeaderBits = actualHeaderBits; … … 1435 1257 } 1436 1258 m_picLambda = averageLambda; 1437 #if !M0036_RC_IMPROVEMENT || KWU_RC_MADPRED_E02271438 1259 #if KWU_RC_MADPRED_E0227 1439 1260 m_totalMAD = 0; 1440 #endif1441 1261 for ( Int i=0; i<m_numberOfLCU; i++ ) 1442 1262 { … … 1447 1267 Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; 1448 1268 Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; 1449 #if RATE_CONTROL_INTRA1450 1269 if (eSliceType == I_SLICE) 1451 1270 { … … 1454 1273 else 1455 1274 { 1456 #endif1457 1275 // update parameters 1458 1276 Double picActualBits = ( Double )m_picActualBits; … … 1461 1279 Double inputLambda = m_picLambda; 1462 1280 1463 #if M0036_RC_IMPROVEMENT1464 1281 if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 ) 1465 #else1466 if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 || effectivePercentage < 0.05 )1467 #endif1468 1282 { 1469 1283 alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); 1470 1284 beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); 1471 1285 1472 #if M0036_RC_IMPROVEMENT1473 1286 alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); 1474 1287 beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); 1475 #else1476 alpha = Clip3( 0.05, 20.0, alpha );1477 beta = Clip3( -3.0, -0.1, beta );1478 #endif1479 1288 TRCParameter rcPara; 1480 1289 rcPara.m_alpha = alpha; … … 1488 1297 alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; 1489 1298 double lnbpp = log( picActualBpp ); 1490 #if M0036_RC_IMPROVEMENT1491 1299 lnbpp = Clip3( -5.0, -0.1, lnbpp ); 1492 #else1493 lnbpp = Clip3( -5.0, 1.0, lnbpp );1494 #endif1495 1300 beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; 1496 1301 1497 #if M0036_RC_IMPROVEMENT1498 1302 alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); 1499 1303 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 } 1507 1305 1508 1306 TRCParameter rcPara; … … 1512 1310 m_encRCSeq->setPicPara( m_frameLevel, rcPara ); 1513 1311 1514 #if M0036_RC_IMPROVEMENT1515 1312 if ( m_frameLevel == 1 ) 1516 1313 { … … 1519 1316 m_encRCSeq->setLastLambda( updateLastLambda ); 1520 1317 } 1521 #endif 1522 } 1523 1524 #if RATE_CONTROL_INTRA 1318 } 1319 1525 1320 Int TEncRCPic::getRefineBitsForIntra( Int orgBits ) 1526 1321 { … … 1611 1406 return estLambda; 1612 1407 } 1613 #endif1614 1408 1615 1409 TEncRateCtrl::TEncRateCtrl() … … 1645 1439 } 1646 1440 1647 #if M0036_RC_IMPROVEMENT1648 1441 #if KWU_RC_MADPRED_E0227 1649 1442 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], Int layerID ) … … 1651 1444 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] ) 1652 1445 #endif 1653 #else1654 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 #endif1656 1446 { 1657 1447 destroy(); … … 1668 1458 1669 1459 Int numberOfLevel = 1; 1670 #if M0036_RC_IMPROVEMENT1671 1460 Int adaptiveBit = 0; 1672 1461 if ( keepHierBits > 0 ) 1673 #else1674 if ( keepHierBits )1675 #endif1676 1462 { 1677 1463 numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; … … 1696 1482 } 1697 1483 1698 #if M0036_RC_IMPROVEMENT1699 1484 if ( keepHierBits > 0 ) 1700 #else1701 if ( keepHierBits )1702 #endif1703 1485 { 1704 1486 Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); … … 1733 1515 bitsRatio[3] = 14; 1734 1516 } 1735 #if M0036_RC_IMPROVEMENT1736 1517 if ( keepHierBits == 2 ) 1737 1518 { 1738 1519 adaptiveBit = 1; 1739 1520 } 1740 #endif1741 1521 } 1742 1522 else if ( GOPSize == 8 && !isLowdelay ) … … 1786 1566 bitsRatio[7] = 1; 1787 1567 } 1788 #if M0036_RC_IMPROVEMENT1789 1568 if ( keepHierBits == 2 ) 1790 1569 { 1791 1570 adaptiveBit = 2; 1792 1571 } 1793 #endif1794 1572 } 1795 1573 else 1796 1574 { 1797 #if M0036_RC_IMPROVEMENT1798 1575 printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" ); 1799 #else1800 printf( "\n hierarchical bit allocation is not support for the specified coding structure currently." );1801 #endif1802 1576 } 1803 1577 } … … 1812 1586 } 1813 1587 } 1814 #if M0036_RC_IMPROVEMENT1815 1588 if ( keepHierBits > 0 ) 1816 #else1817 if ( keepHierBits )1818 #endif1819 1589 { 1820 1590 if ( GOPSize == 4 && isLowdelay ) … … 1851 1621 1852 1622 m_encRCSeq = new TEncRCSeq; 1853 #if M0036_RC_IMPROVEMENT1854 1623 m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit ); 1855 #else1856 m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel );1857 #endif1858 1624 m_encRCSeq->initBitsRatio( bitsRatio ); 1859 1625 m_encRCSeq->initGOPID2Level( GOPID2Level ); … … 1894 1660 } 1895 1661 1896 #else1897 1898 #define ADJUSTMENT_FACTOR 0.601899 #define HIGH_QSTEP_THRESHOLD 9.52381900 #define HIGH_QSTEP_ALPHA 4.93711901 #define HIGH_QSTEP_BETA 0.09221902 #define LOW_QSTEP_ALPHA 16.74291903 #define LOW_QSTEP_BETA -1.14941904 1905 #define MAD_PRED_Y1 1.01906 #define MAD_PRED_Y2 0.01907 1908 enum MAD_HISOTRY {1909 MAD_PPPrevious = 0,1910 MAD_PPrevious = 1,1911 MAD_Previous = 21912 };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 else1971 {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 else1992 {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 else2068 {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 bps2084 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 else2197 {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 else2211 {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_qp2216 ||(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 else2225 {2226 finalQP = bwdQP+2;2227 }2228 m_indexNonRefFrame++;2229 }2230 }2231 else2232 {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 else2258 {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 else2300 {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 else2310 {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 else2371 {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 else2384 {2385 Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1;2386 m_targetBufLevel = m_targetBufLevel2387 - (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 else2404 {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_E02272483 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 #endif2529 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 #endif2541
Note: See TracChangeset for help on using the changeset viewer.