38 #include "../TLibCommon/TComPic.h"
39 #include "../TLibCommon/TComChromaFormat.h"
67 m_useLCUSeparateModel =
false;
77 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 )
80 m_totalFrames = totalFrames;
81 m_targetRate = targetBitrate;
82 m_frameRate = frameRate;
84 m_picWidth = picWidth;
85 m_picHeight = picHeight;
86 m_LCUWidth = LCUWidth;
87 m_LCUHeight = LCUHeight;
88 m_numberOfLevel = numberOfLevel;
89 m_useLCUSeparateModel = useLCUSeparateModel;
91 m_numberOfPixel = m_picWidth * m_picHeight;
92 m_targetBits = (
Int64)m_totalFrames * (
Int64)m_targetRate / (
Int64)m_frameRate;
94 if ( m_seqTargetBpp < 0.03 )
99 else if ( m_seqTargetBpp < 0.08 )
101 m_alphaUpdate = 0.05;
102 m_betaUpdate = 0.025;
104 else if ( m_seqTargetBpp < 0.2 )
109 else if ( m_seqTargetBpp < 0.5 )
120 m_averageBits = (
Int)(m_targetBits / totalFrames);
121 Int picWidthInBU = ( m_picWidth % m_LCUWidth ) == 0 ? m_picWidth / m_LCUWidth : m_picWidth / m_LCUWidth + 1;
122 Int picHeightInBU = ( m_picHeight % m_LCUHeight ) == 0 ? m_picHeight / m_LCUHeight : m_picHeight / m_LCUHeight + 1;
123 m_numberOfLCU = picWidthInBU * picHeightInBU;
125 m_bitsRatio =
new Int[m_GOPSize];
126 for (
Int i=0; i<m_GOPSize; i++ )
131 m_GOPID2Level =
new Int[m_GOPSize];
132 for (
Int i=0; i<m_GOPSize; i++ )
134 m_GOPID2Level[i] = 1;
138 for (
Int i=0; i<m_numberOfLevel; i++ )
141 m_picPara[i].m_beta = 0.0;
144 if ( m_useLCUSeparateModel )
147 for (
Int i=0; i<m_numberOfLevel; i++ )
150 for (
Int j=0; j<m_numberOfLCU; j++)
153 m_LCUPara[i][j].m_beta = 0.0;
158 m_framesLeft = m_totalFrames;
159 m_bitsLeft = m_targetBits;
160 m_adaptiveBit = adaptiveBit;
166 if (m_bitsRatio !=
NULL)
168 delete[] m_bitsRatio;
172 if ( m_GOPID2Level !=
NULL )
174 delete[] m_GOPID2Level;
175 m_GOPID2Level =
NULL;
178 if ( m_picPara !=
NULL )
184 if ( m_LCUPara !=
NULL )
186 for (
Int i=0; i<m_numberOfLevel; i++ )
188 delete[] m_LCUPara[i];
197 for (
Int i=0; i<m_GOPSize; i++)
199 m_bitsRatio[i] = bitsRatio[i];
205 for (
Int i=0; i<m_GOPSize; i++ )
207 m_GOPID2Level[i] = GOPID2Level[i];
213 assert( m_picPara !=
NULL );
215 if ( picPara ==
NULL )
217 for (
Int i=0; i<m_numberOfLevel; i++ )
221 m_picPara[i].m_alpha = 3.2003;
222 m_picPara[i].m_beta = -1.367;
226 m_picPara[i].m_alpha =
ALPHA;
227 m_picPara[i].m_beta =
BETA2;
233 for (
Int i=0; i<m_numberOfLevel; i++ )
235 m_picPara[i] = picPara[i];
242 if ( m_LCUPara ==
NULL )
246 if ( LCUPara ==
NULL )
248 for (
Int i=0; i<m_numberOfLevel; i++ )
250 for (
Int j=0; j<m_numberOfLCU; j++)
252 m_LCUPara[i][j].m_alpha = m_picPara[i].m_alpha;
253 m_LCUPara[i][j].m_beta = m_picPara[i].m_beta;
259 for (
Int i=0; i<m_numberOfLevel; i++ )
261 for (
Int j=0; j<m_numberOfLCU; j++)
263 m_LCUPara[i][j] = LCUPara[i][j];
277 Int* bitsRatio =
new Int[m_GOPSize];
278 for (
Int i=0; i<m_GOPSize; i++ )
280 bitsRatio[i] = (
Int)( equaCoeffA[i] * pow( basicLambda, equaCoeffB[i] ) * m_numberOfPixel );
282 initBitsRatio( bitsRatio );
290 m_picTargetBitInGOP =
NULL;
305 Int targetBits = xEstGOPTargetBits( encRCSeq, numPic );
319 lambdaRatio[1] = 0.725 * log( encRCSeq->
getLastLambda() ) + 0.5793;
320 lambdaRatio[0] = 1.3 * lambdaRatio[1];
321 lambdaRatio[2] = 1.3 * lambdaRatio[1];
322 lambdaRatio[3] = 1.0;
326 lambdaRatio[0] = 5.0;
327 lambdaRatio[1] = 4.0;
328 lambdaRatio[2] = 5.0;
329 lambdaRatio[3] = 1.0;
336 lambdaRatio[0] = 1.0;
337 lambdaRatio[1] = 0.725 * log( encRCSeq->
getLastLambda() ) + 0.7963;
338 lambdaRatio[2] = 1.3 * lambdaRatio[1];
339 lambdaRatio[3] = 3.25 * lambdaRatio[1];
340 lambdaRatio[4] = 3.25 * lambdaRatio[1];
341 lambdaRatio[5] = 1.3 * lambdaRatio[1];
342 lambdaRatio[6] = 3.25 * lambdaRatio[1];
343 lambdaRatio[7] = 3.25 * lambdaRatio[1];
347 lambdaRatio[0] = 1.0;
348 lambdaRatio[1] = 4.0;
349 lambdaRatio[2] = 5.0;
350 lambdaRatio[3] = 12.3;
351 lambdaRatio[4] = 12.3;
352 lambdaRatio[5] = 5.0;
353 lambdaRatio[6] = 12.3;
354 lambdaRatio[7] = 12.3;
358 xCalEquaCoeff( encRCSeq, lambdaRatio, equaCoeffA, equaCoeffB, encRCSeq->
getGOPSize() );
359 basicLambda = xSolveEqua( targetBpp, equaCoeffA, equaCoeffB, encRCSeq->
getGOPSize() );
362 delete []lambdaRatio;
367 m_picTargetBitInGOP =
new Int[numPic];
369 Int totalPicRatio = 0;
370 Int currPicRatio = 0;
371 for ( i=0; i<numPic; i++ )
375 for ( i=0; i<numPic; i++ )
378 m_picTargetBitInGOP[i] = (
Int)( ((
Double)targetBits) * currPicRatio / totalPicRatio );
381 m_encRCSeq = encRCSeq;
383 m_targetBits = targetBits;
384 m_picLeft = m_numPic;
385 m_bitsLeft = m_targetBits;
390 for (
Int i=0; i<GOPSize; i++ )
395 equaCoeffA[i] = pow( 1.0/alpha, 1.0/beta ) * pow( lambdaRatio[i], 1.0/beta );
396 equaCoeffB[i] = 1.0/beta;
404 Double maxNumber = 10000.0;
408 for (
Int j=0; j<GOPSize; j++ )
410 fx += equaCoeffA[j] * pow( solution, equaCoeffB[j] );
413 if ( fabs( fx - targetBpp ) < 0.000001 )
418 if ( fx > targetBpp )
420 minNumber = solution;
421 solution = ( solution + maxNumber ) / 2.0;
425 maxNumber = solution;
426 solution = ( solution + minNumber ) / 2.0;
430 solution =
Clip3( 0.1, 10000.0, solution );
437 if ( m_picTargetBitInGOP !=
NULL )
439 delete[] m_picTargetBitInGOP;
440 m_picTargetBitInGOP =
NULL;
446 m_bitsLeft -= bitsCost;
454 Int currentTargetBitsPerPic = (
Int)( ( encRCSeq->
getBitsLeft() - averageTargetBitsPerPic * (encRCSeq->
getFramesLeft() - realInfluencePicture) ) / realInfluencePicture );
455 Int targetBits = currentTargetBitsPerPic * GOPSize;
457 if ( targetBits < 200 )
477 m_estPicLambda = 0.0;
484 m_picActualHeaderBits = 0;
503 Int totalPicRatio = 0;
504 for ( i=currPicPosition; i<encRCGOP->
getNumPic(); i++ )
509 targetBits =
Int( ((
Double)GOPbitsLeft) * currPicRatio / totalPicRatio );
511 if ( targetBits < 100 )
516 if ( m_encRCSeq->getFramesLeft() > 16 )
526 Int numPreviousPics = 0;
527 Int totalPreviousBits = 0;
529 list<TEncRCPic*>::iterator it;
530 for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
532 if ( (*it)->getFrameLevel() == frameLevel )
534 totalPreviousBits += (*it)->getPicActualHeaderBits();
539 Int estHeaderBits = 0;
540 if ( numPreviousPics > 0 )
542 estHeaderBits = totalPreviousBits / numPreviousPics;
545 return estHeaderBits;
556 Int totalPicRatio = 0;
557 for (
Int i = nextPicPosition; i < encRCGOP->
getNumPic(); i++)
562 if (nextPicPosition == 0)
568 GOPbitsLeft -= m_targetBits;
571 lowerBound =
Int(((
Double)GOPbitsLeft) * nextPicRatio / totalPicRatio);
573 if (lowerBound < 100)
578 if (m_encRCSeq->getFramesLeft() > 16)
590 TEncRCPic* p = listPreviousPictures.front();
591 listPreviousPictures.pop_front();
596 listPreviousPictures.push_back(
this );
602 m_encRCSeq = encRCSeq;
603 m_encRCGOP = encRCGOP;
605 Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP );
606 Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel );
608 if ( targetBits < estHeaderBits + 100 )
610 targetBits = estHeaderBits + 100;
613 m_frameLevel = frameLevel;
616 m_estPicLambda = 100.0;
617 m_targetBits = targetBits;
618 m_estHeaderBits = estHeaderBits;
619 m_bitsLeft = m_targetBits;
624 Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1;
625 Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1;
626 m_lowerBound = xEstPicLowerBound( encRCSeq, encRCGOP );
628 m_LCULeft = m_numberOfLCU;
629 m_bitsLeft -= m_estHeaderBits;
630 m_pixelsLeft = m_numberOfPixel;
632 m_LCUs =
new TRCLCU[m_numberOfLCU];
635 for ( i=0; i<picWidthInLCU; i++ )
637 for ( j=0; j<picHeightInLCU; j++ )
639 LCUIdx = j*picWidthInLCU + i;
641 m_LCUs[LCUIdx].m_QP = 0;
642 m_LCUs[LCUIdx].m_lambda = 0.0;
643 m_LCUs[LCUIdx].m_targetBits = 0;
644 m_LCUs[LCUIdx].m_bitWeight = 1.0;
645 Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth );
646 Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight );
647 m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight;
650 m_picActualHeaderBits = 0;
670 Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
671 Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
676 estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(
Double)m_numberOfPixel,
BETA1), bpp);
680 estLambda = alpha * pow( bpp, beta );
683 Double lastLevelLambda = -1.0;
684 Double lastPicLambda = -1.0;
685 Double lastValidLambda = -1.0;
686 list<TEncRCPic*>::iterator it;
687 for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
689 if ( (*it)->getFrameLevel() == m_frameLevel )
691 lastLevelLambda = (*it)->getPicActualLambda();
693 lastPicLambda = (*it)->getPicActualLambda();
695 if ( lastPicLambda > 0.0 )
697 lastValidLambda = lastPicLambda;
701 if ( lastLevelLambda > 0.0 )
703 lastLevelLambda =
Clip3( 0.1, 10000.0, lastLevelLambda );
704 estLambda =
Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda );
707 if ( lastPicLambda > 0.0 )
709 lastPicLambda =
Clip3( 0.1, 2000.0, lastPicLambda );
710 estLambda =
Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda );
712 else if ( lastValidLambda > 0.0 )
714 lastValidLambda =
Clip3( 0.1, 2000.0, lastValidLambda );
715 estLambda =
Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda );
719 estLambda =
Clip3( 0.1, 10000.0, estLambda );
722 if ( estLambda < 0.1 )
727 m_estPicLambda = estLambda;
731 for (
Int i=0; i<m_numberOfLCU; i++ )
734 if ( m_encRCSeq->getUseLCUSeparateModel() )
736 alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha;
737 betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta;
741 alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
742 betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
745 m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU );
747 if ( m_LCUs[i].m_bitWeight < 0.01 )
749 m_LCUs[i].m_bitWeight = 0.01;
751 totalWeight += m_LCUs[i].m_bitWeight;
753 for (
Int i=0; i<m_numberOfLCU; i++ )
755 Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight;
756 m_LCUs[i].m_bitWeight = BUTargetBits;
764 Int QP =
Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 );
769 list<TEncRCPic*>::iterator it;
770 for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ )
772 if ( (*it)->getFrameLevel() == m_frameLevel )
774 lastLevelQP = (*it)->getPicActualQP();
776 lastPicQP = (*it)->getPicActualQP();
779 lastValidQP = lastPicQP;
785 QP =
Clip3( lastLevelQP - 3, lastLevelQP + 3, QP );
790 QP =
Clip3( lastPicQP - 10, lastPicQP + 10, QP );
794 QP =
Clip3( lastValidQP - 10, lastValidQP + 10, QP );
802 Int LCUIdx = getLCUCoded();
808 Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1;
809 Int bitrateWindow = min(4,noOfLCUsLeft);
810 Double MAD = getLCU(LCUIdx).m_costIntra;
812 if (m_remainingCostIntra > 0.1 )
814 Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(
Double)bitrateWindow;
815 avgBits =
Int( MAD*weightedBitsLeft/m_remainingCostIntra );
819 avgBits =
Int( m_bitsLeft / m_LCULeft );
821 m_remainingCostIntra -= MAD;
826 for (
Int i=LCUIdx; i<m_numberOfLCU; i++ )
828 totalWeight += m_LCUs[i].m_bitWeight;
831 avgBits = (
Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 );
839 bpp = (
Double )avgBits/(
Double )m_LCUs[ LCUIdx ].m_numberOfPixel;
840 m_LCUs[ LCUIdx ].m_targetBits = avgBits;
847 Int LCUIdx = getLCUCoded();
850 if ( m_encRCSeq->getUseLCUSeparateModel() )
852 alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha;
853 beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta;
857 alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
858 beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
861 Double estLambda = alpha * pow( bpp, beta );
863 Double clipPicLambda = m_estPicLambda;
866 Double clipNeighbourLambda = -1.0;
867 for (
Int i=LCUIdx - 1; i>=0; i-- )
869 if ( m_LCUs[i].m_lambda > 0 )
871 clipNeighbourLambda = m_LCUs[i].m_lambda;
876 if ( clipNeighbourLambda > 0.0 )
878 estLambda =
Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda );
881 if ( clipPicLambda > 0.0 )
883 estLambda =
Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda );
887 estLambda =
Clip3( 10.0, 1000.0, estLambda );
890 if ( estLambda < 0.1 )
900 Int LCUIdx = getLCUCoded();
901 Int estQP =
Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 );
905 for (
Int i=LCUIdx - 1; i>=0; i-- )
909 clipNeighbourQP = getLCU(i).m_QP;
916 estQP =
Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP );
919 estQP =
Clip3( clipPicQP - 2, clipPicQP + 2, estQP );
926 m_LCUs[LCUIdx].m_actualBits = bits;
927 m_LCUs[LCUIdx].m_QP = QP;
928 m_LCUs[LCUIdx].m_lambda = lambda;
932 m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel;
934 if ( !updateLCUParameter )
939 if ( !m_encRCSeq->getUseLCUSeparateModel() )
944 Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha;
945 Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta;
947 Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits;
948 Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel;
950 Double calLambda = alpha * pow( bpp, beta );
951 Double inputLambda = m_LCUs[LCUIdx].m_lambda;
953 if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 )
955 alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
956 beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
964 m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara );
969 calLambda =
Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
970 alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
971 Double lnbpp = log( bpp );
972 lnbpp =
Clip3( -5.0, -0.1, lnbpp );
973 beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
981 m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara );
988 Int numTotalLCUs = 0;
991 for ( i=0; i<m_numberOfLCU; i++ )
993 if ( m_LCUs[i].m_QP > 0 )
995 totalQPs += m_LCUs[i].m_QP;
1001 if ( numTotalLCUs == 0 )
1014 Double totalLambdas = 0.0;
1015 Int numTotalLCUs = 0;
1018 for ( i=0; i<m_numberOfLCU; i++ )
1020 if ( m_LCUs[i].m_lambda > 0.01 )
1022 totalLambdas += log( m_LCUs[i].m_lambda );
1028 if( numTotalLCUs == 0 )
1034 avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs );
1042 m_picActualHeaderBits = actualHeaderBits;
1043 m_picActualBits = actualTotalBits;
1044 if ( averageQP > 0.0 )
1046 m_picQP =
Int( averageQP + 0.5 );
1052 m_picLambda = averageLambda;
1054 Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
1055 Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
1059 updateAlphaBetaIntra(&alpha, &beta);
1065 Double picActualBpp = picActualBits/(
Double)m_numberOfPixel;
1066 Double calLambda = alpha * pow( picActualBpp, beta );
1067 Double inputLambda = m_picLambda;
1069 if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 )
1071 alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 );
1072 beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 );
1080 m_encRCSeq->setPicPara( m_frameLevel, rcPara );
1085 calLambda =
Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda );
1086 alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha;
1087 Double lnbpp = log( picActualBpp );
1088 lnbpp =
Clip3( -5.0, -0.1, lnbpp );
1090 beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp;
1100 m_encRCSeq->setPicPara( m_frameLevel, rcPara );
1102 if ( m_frameLevel == 1 )
1104 Double currLambda =
Clip3( 0.1, 10000.0, m_picLambda );
1106 m_encRCSeq->setLastLambda( updateLastLambda );
1112 Double alpha=0.25, beta=0.5582;
1115 if (orgBits*40 < m_numberOfPixel)
1124 iIntraBits = (
Int)(alpha* pow(m_totalCostIntra*4.0/(
Double)orgBits, beta)*(
Double)orgBits+0.5);
1131 return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) );
1137 Double diffLambda = (*beta)*(log((
Double)m_picActualBits)-log((
Double)m_targetBits));
1139 diffLambda =
Clip3(-0.125, 0.125, 0.25*diffLambda);
1140 *alpha = (*alpha) * exp(diffLambda);
1141 *beta = (*beta) + diffLambda / lnbpp;
1149 m_remainingCostIntra = m_totalCostIntra;
1150 for (
Int i=m_numberOfLCU-1; i>=0; i--)
1152 iAvgBits +=
Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra);
1153 getLCU(i).m_targetBitsLeft = iAvgBits;
1160 Int LCUIdx = getLCUCoded();
1162 Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha;
1163 Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta;
1165 Double costPerPixel = getLCU(LCUIdx).m_costIntra/(
Double)getLCU(LCUIdx).m_numberOfPixel;
1166 costPerPixel = pow(costPerPixel,
BETA1);
1167 Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp);
1170 for (
Int i=LCUIdx-1; i>=0; i--)
1174 clipNeighbourQP = getLCU(i).m_QP;
1179 Int minQP = clipPicQP - 2;
1180 Int maxQP = clipPicQP + 2;
1184 maxQP = min(clipNeighbourQP + 1, maxQP);
1185 minQP = max(clipNeighbourQP - 1, minQP);
1188 Double maxLambda=exp(((
Double)(maxQP+0.49)-13.7122)/4.2005);
1189 Double minLambda=exp(((
Double)(minQP-0.49)-13.7122)/4.2005);
1191 estLambda =
Clip3(minLambda, maxLambda, estLambda);
1193 *estQP =
Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 );
1194 *estQP =
Clip3(minQP, maxQP, *estQP);
1213 if ( m_encRCSeq !=
NULL )
1218 if ( m_encRCGOP !=
NULL )
1223 while ( m_listRCPictures.size() > 0 )
1225 TEncRCPic* p = m_listRCPictures.front();
1226 m_listRCPictures.pop_front();
1231 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] )
1235 Bool isLowdelay =
true;
1236 for (
Int i=0; i<GOPSize-1; i++ )
1238 if ( GOPList[i].m_POC > GOPList[i+1].m_POC )
1245 Int numberOfLevel = 1;
1246 Int adaptiveBit = 0;
1247 if ( keepHierBits > 0 )
1249 numberOfLevel =
Int( log((
Double)GOPSize)/log(2.0) + 0.5 ) + 1;
1251 if ( !isLowdelay && GOPSize == 8 )
1253 numberOfLevel =
Int( log((
Double)GOPSize)/log(2.0) + 0.5 ) + 1;
1260 bitsRatio =
new Int[ GOPSize ];
1261 for (
Int i=0; i<GOPSize; i++ )
1264 if ( !GOPList[i].m_refPic )
1270 if ( keepHierBits > 0 )
1273 if ( GOPSize == 4 && isLowdelay )
1282 else if( bpp > 0.1 )
1289 else if ( bpp > 0.05 )
1304 if ( keepHierBits == 2 )
1309 else if ( GOPSize == 8 && !isLowdelay )
1322 else if ( bpp > 0.1 )
1333 else if ( bpp > 0.05 )
1356 if ( keepHierBits == 2 )
1363 printf(
"\n hierarchical bit allocation is not support for the specified coding structure currently.\n" );
1367 Int* GOPID2Level =
new Int[ GOPSize ];
1368 for (
Int i=0; i<GOPSize; i++ )
1371 if ( !GOPList[i].m_refPic )
1377 if ( keepHierBits > 0 )
1379 if ( GOPSize == 4 && isLowdelay )
1386 else if ( GOPSize == 8 && !isLowdelay )
1399 if ( !isLowdelay && GOPSize == 8 )
1412 m_encRCSeq->
create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit );
1413 m_encRCSeq->initBitsRatio( bitsRatio );
1414 m_encRCSeq->initGOPID2Level( GOPID2Level );
1415 m_encRCSeq->initPicPara();
1416 if ( useLCUSeparateModel )
1418 m_encRCSeq->initLCUPara();
1420 m_CpbSaturationEnabled =
false;
1421 m_cpbSize = targetBitrate;
1422 m_cpbState = (
UInt)(m_cpbSize*0.5f);
1423 m_bufferingRate = (
Int)(targetBitrate / frameRate);
1426 delete[] GOPID2Level;
1432 m_encRCPic->
create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures );
1438 m_encRCGOP->
create( m_encRCSeq, numberOfPictures );
1445 m_cpbState -= actualBits;
1451 m_cpbState += m_bufferingRate;
1452 if (m_cpbState > m_cpbSize)
1462 m_CpbSaturationEnabled =
true;
1464 m_cpbState = (
UInt)(m_cpbSize*fInitialCpbFullness);
1466 printf(
"\nHRD - [Initial CPB state %6d] [CPB Size %6d] [Buffering Rate %6d]\n", m_cpbState, m_cpbSize, m_bufferingRate);
const Double g_RCBetaMaxValue
SliceType
supported slice type
UInt getCpbSizeValueMinus1(Int layer, Int cpbcnt, Int nalOrVcl) const
Void initLCUPara(TRCParameter **LCUPara=0)
Void getLCUInitTargetBits()
const Double g_RCAlphaMaxValue
const Double g_RCAlphaMinValue
Double calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel)
Int xEstGOPTargetBits(TEncRCSeq *encRCSeq, Int GOPSize)
Int updateCpbState(Int actualBits)
const Int g_RCInvalidQPValue
Int estimatePicQP(Double lambda, list< TEncRCPic * > &listPreviousPictures)
Double estimatePicLambda(list< TEncRCPic * > &listPreviousPictures, SliceType eSliceType)
Double xSolveEqua(Double targetBpp, Double *equaCoeffA, Double *equaCoeffB, Int GOPSize)
const Double g_RCWeightPicTargetBitInGOP
Void create(TEncRCSeq *encRCSeq, Int numPic)
static const Int MAX_GOP
max. value of hierarchical GOP size
const Int g_RCMaxPicListSize
TRCParameter * getPicPara()
Void create(Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit)
const Int g_RCIterationNum
Void initRCGOP(Int numberOfPictures)
Void updateAfterPicture(Int bitsCost)
Int getRefineBitsForIntra(Int orgBits)
UInt getCpbSizeScale() const
T Clip3(const T minVal, const T maxVal, const T a)
general min/max clip
Int xEstPicTargetBits(TEncRCSeq *encRCSeq, TEncRCGOP *encRCGOP)
Double getLCUTargetBpp(SliceType eSliceType)
const Double g_RCWeightPicRargetBitInBuffer
Void initGOPID2Level(Int GOPID2Level[])
Void initHrdParam(const TComHRD *pcHrd, Int iFrameRate, Double fInitialCpbFullness)
const Int g_RCSmoothWindowSize
Int getLCUEstQP(Double lambda, Int clipPicQP)
Void updateAfterCTU(Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter=true)
Double calAverageLambda()
Void updateAfterPicture(Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType)
Void addToPictureLsit(list< TEncRCPic * > &listPreviousPictures)
const Double g_RCWeightHistoryLambda
Double getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP)
const Double g_RCWeightCurrentLambda
Int xEstPicLowerBound(TEncRCSeq *encRCSeq, TEncRCGOP *encRCGOP)
Void create(TEncRCSeq *encRCSeq, TEncRCGOP *encRCGOP, Int frameLevel, list< TEncRCPic * > &listPreviousPictures)
Void initRCPic(Int frameLevel)
UInt getBitRateValueMinus1(Int layer, Int cpbcnt, Int nalOrVcl) const
Void initBitsRatio(Int bitsRatio[])
Rate control manager class.
Void updateAlphaBetaIntra(Double *alpha, Double *beta)
Void setAllBitRatio(Double basicLambda, Double *equaCoeffA, Double *equaCoeffB)
const Int g_RCLCUSmoothWindowSize
Int xEstPicHeaderBits(list< TEncRCPic * > &listPreviousPictures, Int frameLevel)
Void initPicPara(TRCParameter *picPara=0)
const Double g_RCBetaMinValue
Void updateAfterPic(Int bits)
Void 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])
Double getLCUEstLambda(Double bpp)
UInt getBitRateScale() const
Void xCalEquaCoeff(TEncRCSeq *encRCSeq, Double *lambdaRatio, Double *equaCoeffA, Double *equaCoeffB, Int GOPSize)