[313] | 1 | /* The copyright in this software is being made available under the BSD |
---|
| 2 | * License, included below. This software may be subject to other third party |
---|
| 3 | * and contributor rights, including patent rights, and no such rights are |
---|
[1029] | 4 | * granted under this license. |
---|
[313] | 5 | * |
---|
[1549] | 6 | * Copyright (c) 2010-2016, ITU/ISO/IEC |
---|
[313] | 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright notice, |
---|
| 13 | * this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
| 15 | * this list of conditions and the following disclaimer in the documentation |
---|
| 16 | * and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
| 18 | * be used to endorse or promote products derived from this software without |
---|
| 19 | * specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
| 31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** \file TEncRateCtrl.cpp |
---|
| 35 | \brief Rate control manager class |
---|
| 36 | */ |
---|
| 37 | #include "TEncRateCtrl.h" |
---|
| 38 | #include "../TLibCommon/TComPic.h" |
---|
[1029] | 39 | #include "../TLibCommon/TComChromaFormat.h" |
---|
[313] | 40 | |
---|
| 41 | #include <cmath> |
---|
| 42 | |
---|
| 43 | using namespace std; |
---|
| 44 | |
---|
| 45 | //sequence level |
---|
| 46 | TEncRCSeq::TEncRCSeq() |
---|
| 47 | { |
---|
| 48 | m_totalFrames = 0; |
---|
| 49 | m_targetRate = 0; |
---|
| 50 | m_frameRate = 0; |
---|
| 51 | m_targetBits = 0; |
---|
| 52 | m_GOPSize = 0; |
---|
| 53 | m_picWidth = 0; |
---|
| 54 | m_picHeight = 0; |
---|
| 55 | m_LCUWidth = 0; |
---|
| 56 | m_LCUHeight = 0; |
---|
| 57 | m_numberOfLevel = 0; |
---|
| 58 | m_numberOfLCU = 0; |
---|
| 59 | m_averageBits = 0; |
---|
| 60 | m_bitsRatio = NULL; |
---|
| 61 | m_GOPID2Level = NULL; |
---|
| 62 | m_picPara = NULL; |
---|
| 63 | m_LCUPara = NULL; |
---|
| 64 | m_numberOfPixel = 0; |
---|
| 65 | m_framesLeft = 0; |
---|
| 66 | m_bitsLeft = 0; |
---|
| 67 | m_useLCUSeparateModel = false; |
---|
| 68 | m_adaptiveBit = 0; |
---|
| 69 | m_lastLambda = 0.0; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | TEncRCSeq::~TEncRCSeq() |
---|
| 73 | { |
---|
| 74 | destroy(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 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 ) |
---|
| 78 | { |
---|
| 79 | destroy(); |
---|
| 80 | m_totalFrames = totalFrames; |
---|
| 81 | m_targetRate = targetBitrate; |
---|
| 82 | m_frameRate = frameRate; |
---|
| 83 | m_GOPSize = GOPSize; |
---|
| 84 | m_picWidth = picWidth; |
---|
| 85 | m_picHeight = picHeight; |
---|
| 86 | m_LCUWidth = LCUWidth; |
---|
| 87 | m_LCUHeight = LCUHeight; |
---|
| 88 | m_numberOfLevel = numberOfLevel; |
---|
| 89 | m_useLCUSeparateModel = useLCUSeparateModel; |
---|
| 90 | |
---|
| 91 | m_numberOfPixel = m_picWidth * m_picHeight; |
---|
| 92 | m_targetBits = (Int64)m_totalFrames * (Int64)m_targetRate / (Int64)m_frameRate; |
---|
| 93 | m_seqTargetBpp = (Double)m_targetRate / (Double)m_frameRate / (Double)m_numberOfPixel; |
---|
| 94 | if ( m_seqTargetBpp < 0.03 ) |
---|
| 95 | { |
---|
| 96 | m_alphaUpdate = 0.01; |
---|
| 97 | m_betaUpdate = 0.005; |
---|
| 98 | } |
---|
| 99 | else if ( m_seqTargetBpp < 0.08 ) |
---|
| 100 | { |
---|
| 101 | m_alphaUpdate = 0.05; |
---|
| 102 | m_betaUpdate = 0.025; |
---|
| 103 | } |
---|
| 104 | else if ( m_seqTargetBpp < 0.2 ) |
---|
| 105 | { |
---|
| 106 | m_alphaUpdate = 0.1; |
---|
| 107 | m_betaUpdate = 0.05; |
---|
| 108 | } |
---|
| 109 | else if ( m_seqTargetBpp < 0.5 ) |
---|
| 110 | { |
---|
| 111 | m_alphaUpdate = 0.2; |
---|
| 112 | m_betaUpdate = 0.1; |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | m_alphaUpdate = 0.4; |
---|
| 117 | m_betaUpdate = 0.2; |
---|
| 118 | } |
---|
[1029] | 119 | |
---|
[313] | 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; |
---|
| 124 | |
---|
| 125 | m_bitsRatio = new Int[m_GOPSize]; |
---|
| 126 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
| 127 | { |
---|
| 128 | m_bitsRatio[i] = 1; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | m_GOPID2Level = new Int[m_GOPSize]; |
---|
| 132 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
| 133 | { |
---|
| 134 | m_GOPID2Level[i] = 1; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | m_picPara = new TRCParameter[m_numberOfLevel]; |
---|
| 138 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 139 | { |
---|
| 140 | m_picPara[i].m_alpha = 0.0; |
---|
| 141 | m_picPara[i].m_beta = 0.0; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | if ( m_useLCUSeparateModel ) |
---|
| 145 | { |
---|
| 146 | m_LCUPara = new TRCParameter*[m_numberOfLevel]; |
---|
| 147 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 148 | { |
---|
| 149 | m_LCUPara[i] = new TRCParameter[m_numberOfLCU]; |
---|
| 150 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
| 151 | { |
---|
| 152 | m_LCUPara[i][j].m_alpha = 0.0; |
---|
| 153 | m_LCUPara[i][j].m_beta = 0.0; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | m_framesLeft = m_totalFrames; |
---|
| 159 | m_bitsLeft = m_targetBits; |
---|
| 160 | m_adaptiveBit = adaptiveBit; |
---|
| 161 | m_lastLambda = 0.0; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | Void TEncRCSeq::destroy() |
---|
| 165 | { |
---|
| 166 | if (m_bitsRatio != NULL) |
---|
| 167 | { |
---|
| 168 | delete[] m_bitsRatio; |
---|
| 169 | m_bitsRatio = NULL; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | if ( m_GOPID2Level != NULL ) |
---|
| 173 | { |
---|
| 174 | delete[] m_GOPID2Level; |
---|
| 175 | m_GOPID2Level = NULL; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | if ( m_picPara != NULL ) |
---|
| 179 | { |
---|
| 180 | delete[] m_picPara; |
---|
| 181 | m_picPara = NULL; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | if ( m_LCUPara != NULL ) |
---|
| 185 | { |
---|
| 186 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 187 | { |
---|
| 188 | delete[] m_LCUPara[i]; |
---|
| 189 | } |
---|
| 190 | delete[] m_LCUPara; |
---|
| 191 | m_LCUPara = NULL; |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | Void TEncRCSeq::initBitsRatio( Int bitsRatio[]) |
---|
| 196 | { |
---|
| 197 | for (Int i=0; i<m_GOPSize; i++) |
---|
| 198 | { |
---|
| 199 | m_bitsRatio[i] = bitsRatio[i]; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | Void TEncRCSeq::initGOPID2Level( Int GOPID2Level[] ) |
---|
| 204 | { |
---|
| 205 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
| 206 | { |
---|
| 207 | m_GOPID2Level[i] = GOPID2Level[i]; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | Void TEncRCSeq::initPicPara( TRCParameter* picPara ) |
---|
| 212 | { |
---|
| 213 | assert( m_picPara != NULL ); |
---|
| 214 | |
---|
| 215 | if ( picPara == NULL ) |
---|
| 216 | { |
---|
| 217 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 218 | { |
---|
| 219 | if (i>0) |
---|
| 220 | { |
---|
[1029] | 221 | m_picPara[i].m_alpha = 3.2003; |
---|
| 222 | m_picPara[i].m_beta = -1.367; |
---|
| 223 | } |
---|
[313] | 224 | else |
---|
| 225 | { |
---|
[1029] | 226 | m_picPara[i].m_alpha = ALPHA; |
---|
[313] | 227 | m_picPara[i].m_beta = BETA2; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | else |
---|
| 232 | { |
---|
| 233 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 234 | { |
---|
| 235 | m_picPara[i] = picPara[i]; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | Void TEncRCSeq::initLCUPara( TRCParameter** LCUPara ) |
---|
| 241 | { |
---|
| 242 | if ( m_LCUPara == NULL ) |
---|
| 243 | { |
---|
| 244 | return; |
---|
| 245 | } |
---|
| 246 | if ( LCUPara == NULL ) |
---|
| 247 | { |
---|
| 248 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 249 | { |
---|
| 250 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
| 251 | { |
---|
| 252 | m_LCUPara[i][j].m_alpha = m_picPara[i].m_alpha; |
---|
| 253 | m_LCUPara[i][j].m_beta = m_picPara[i].m_beta; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | else |
---|
| 258 | { |
---|
| 259 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 260 | { |
---|
| 261 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
| 262 | { |
---|
| 263 | m_LCUPara[i][j] = LCUPara[i][j]; |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | Void TEncRCSeq::updateAfterPic ( Int bits ) |
---|
| 270 | { |
---|
| 271 | m_bitsLeft -= bits; |
---|
| 272 | m_framesLeft--; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | Void TEncRCSeq::setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB ) |
---|
| 276 | { |
---|
| 277 | Int* bitsRatio = new Int[m_GOPSize]; |
---|
| 278 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
| 279 | { |
---|
| 280 | bitsRatio[i] = (Int)( equaCoeffA[i] * pow( basicLambda, equaCoeffB[i] ) * m_numberOfPixel ); |
---|
| 281 | } |
---|
| 282 | initBitsRatio( bitsRatio ); |
---|
| 283 | delete[] bitsRatio; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | //GOP level |
---|
| 287 | TEncRCGOP::TEncRCGOP() |
---|
| 288 | { |
---|
| 289 | m_encRCSeq = NULL; |
---|
| 290 | m_picTargetBitInGOP = NULL; |
---|
| 291 | m_numPic = 0; |
---|
| 292 | m_targetBits = 0; |
---|
| 293 | m_picLeft = 0; |
---|
| 294 | m_bitsLeft = 0; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | TEncRCGOP::~TEncRCGOP() |
---|
| 298 | { |
---|
| 299 | destroy(); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | Void TEncRCGOP::create( TEncRCSeq* encRCSeq, Int numPic ) |
---|
| 303 | { |
---|
| 304 | destroy(); |
---|
| 305 | Int targetBits = xEstGOPTargetBits( encRCSeq, numPic ); |
---|
| 306 | |
---|
| 307 | if ( encRCSeq->getAdaptiveBits() > 0 && encRCSeq->getLastLambda() > 0.1 ) |
---|
| 308 | { |
---|
| 309 | Double targetBpp = (Double)targetBits / encRCSeq->getNumPixel(); |
---|
| 310 | Double basicLambda = 0.0; |
---|
| 311 | Double* lambdaRatio = new Double[encRCSeq->getGOPSize()]; |
---|
| 312 | Double* equaCoeffA = new Double[encRCSeq->getGOPSize()]; |
---|
| 313 | Double* equaCoeffB = new Double[encRCSeq->getGOPSize()]; |
---|
| 314 | |
---|
| 315 | if ( encRCSeq->getAdaptiveBits() == 1 ) // for GOP size =4, low delay case |
---|
| 316 | { |
---|
| 317 | if ( encRCSeq->getLastLambda() < 120.0 ) |
---|
| 318 | { |
---|
| 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; |
---|
| 323 | } |
---|
| 324 | else |
---|
| 325 | { |
---|
| 326 | lambdaRatio[0] = 5.0; |
---|
| 327 | lambdaRatio[1] = 4.0; |
---|
| 328 | lambdaRatio[2] = 5.0; |
---|
| 329 | lambdaRatio[3] = 1.0; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | else if ( encRCSeq->getAdaptiveBits() == 2 ) // for GOP size = 8, random access case |
---|
| 333 | { |
---|
| 334 | if ( encRCSeq->getLastLambda() < 90.0 ) |
---|
| 335 | { |
---|
| 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]; |
---|
| 344 | } |
---|
| 345 | else |
---|
| 346 | { |
---|
| 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; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | xCalEquaCoeff( encRCSeq, lambdaRatio, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() ); |
---|
| 359 | basicLambda = xSolveEqua( targetBpp, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() ); |
---|
| 360 | encRCSeq->setAllBitRatio( basicLambda, equaCoeffA, equaCoeffB ); |
---|
| 361 | |
---|
| 362 | delete []lambdaRatio; |
---|
| 363 | delete []equaCoeffA; |
---|
| 364 | delete []equaCoeffB; |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | m_picTargetBitInGOP = new Int[numPic]; |
---|
| 368 | Int i; |
---|
| 369 | Int totalPicRatio = 0; |
---|
| 370 | Int currPicRatio = 0; |
---|
| 371 | for ( i=0; i<numPic; i++ ) |
---|
| 372 | { |
---|
| 373 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
| 374 | } |
---|
| 375 | for ( i=0; i<numPic; i++ ) |
---|
| 376 | { |
---|
| 377 | currPicRatio = encRCSeq->getBitRatio( i ); |
---|
| 378 | m_picTargetBitInGOP[i] = (Int)( ((Double)targetBits) * currPicRatio / totalPicRatio ); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | m_encRCSeq = encRCSeq; |
---|
| 382 | m_numPic = numPic; |
---|
| 383 | m_targetBits = targetBits; |
---|
| 384 | m_picLeft = m_numPic; |
---|
| 385 | m_bitsLeft = m_targetBits; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | Void TEncRCGOP::xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) |
---|
| 389 | { |
---|
| 390 | for ( Int i=0; i<GOPSize; i++ ) |
---|
| 391 | { |
---|
| 392 | Int frameLevel = encRCSeq->getGOPID2Level(i); |
---|
| 393 | Double alpha = encRCSeq->getPicPara(frameLevel).m_alpha; |
---|
| 394 | Double beta = encRCSeq->getPicPara(frameLevel).m_beta; |
---|
| 395 | equaCoeffA[i] = pow( 1.0/alpha, 1.0/beta ) * pow( lambdaRatio[i], 1.0/beta ); |
---|
| 396 | equaCoeffB[i] = 1.0/beta; |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | Double TEncRCGOP::xSolveEqua( Double targetBpp, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) |
---|
| 401 | { |
---|
| 402 | Double solution = 100.0; |
---|
| 403 | Double minNumber = 0.1; |
---|
| 404 | Double maxNumber = 10000.0; |
---|
| 405 | for ( Int i=0; i<g_RCIterationNum; i++ ) |
---|
[1029] | 406 | { |
---|
[313] | 407 | Double fx = 0.0; |
---|
| 408 | for ( Int j=0; j<GOPSize; j++ ) |
---|
| 409 | { |
---|
| 410 | fx += equaCoeffA[j] * pow( solution, equaCoeffB[j] ); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | if ( fabs( fx - targetBpp ) < 0.000001 ) |
---|
| 414 | { |
---|
| 415 | break; |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | if ( fx > targetBpp ) |
---|
| 419 | { |
---|
| 420 | minNumber = solution; |
---|
| 421 | solution = ( solution + maxNumber ) / 2.0; |
---|
| 422 | } |
---|
| 423 | else |
---|
| 424 | { |
---|
| 425 | maxNumber = solution; |
---|
| 426 | solution = ( solution + minNumber ) / 2.0; |
---|
| 427 | } |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | solution = Clip3( 0.1, 10000.0, solution ); |
---|
| 431 | return solution; |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | Void TEncRCGOP::destroy() |
---|
| 435 | { |
---|
| 436 | m_encRCSeq = NULL; |
---|
| 437 | if ( m_picTargetBitInGOP != NULL ) |
---|
| 438 | { |
---|
| 439 | delete[] m_picTargetBitInGOP; |
---|
| 440 | m_picTargetBitInGOP = NULL; |
---|
| 441 | } |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | Void TEncRCGOP::updateAfterPicture( Int bitsCost ) |
---|
| 445 | { |
---|
| 446 | m_bitsLeft -= bitsCost; |
---|
| 447 | m_picLeft--; |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | Int TEncRCGOP::xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize ) |
---|
| 451 | { |
---|
| 452 | Int realInfluencePicture = min( g_RCSmoothWindowSize, encRCSeq->getFramesLeft() ); |
---|
| 453 | Int averageTargetBitsPerPic = (Int)( encRCSeq->getTargetBits() / encRCSeq->getTotalFrames() ); |
---|
| 454 | Int currentTargetBitsPerPic = (Int)( ( encRCSeq->getBitsLeft() - averageTargetBitsPerPic * (encRCSeq->getFramesLeft() - realInfluencePicture) ) / realInfluencePicture ); |
---|
| 455 | Int targetBits = currentTargetBitsPerPic * GOPSize; |
---|
| 456 | |
---|
| 457 | if ( targetBits < 200 ) |
---|
| 458 | { |
---|
| 459 | targetBits = 200; // at least allocate 200 bits for one GOP |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | return targetBits; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | //picture level |
---|
| 466 | TEncRCPic::TEncRCPic() |
---|
| 467 | { |
---|
| 468 | m_encRCSeq = NULL; |
---|
| 469 | m_encRCGOP = NULL; |
---|
| 470 | |
---|
| 471 | m_frameLevel = 0; |
---|
| 472 | m_numberOfPixel = 0; |
---|
| 473 | m_numberOfLCU = 0; |
---|
| 474 | m_targetBits = 0; |
---|
| 475 | m_estHeaderBits = 0; |
---|
| 476 | m_estPicQP = 0; |
---|
| 477 | m_estPicLambda = 0.0; |
---|
| 478 | |
---|
| 479 | m_LCULeft = 0; |
---|
| 480 | m_bitsLeft = 0; |
---|
| 481 | m_pixelsLeft = 0; |
---|
| 482 | |
---|
| 483 | m_LCUs = NULL; |
---|
| 484 | m_picActualHeaderBits = 0; |
---|
| 485 | m_picActualBits = 0; |
---|
| 486 | m_picQP = 0; |
---|
| 487 | m_picLambda = 0.0; |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | TEncRCPic::~TEncRCPic() |
---|
| 491 | { |
---|
| 492 | destroy(); |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | Int TEncRCPic::xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ) |
---|
| 496 | { |
---|
| 497 | Int targetBits = 0; |
---|
| 498 | Int GOPbitsLeft = encRCGOP->getBitsLeft(); |
---|
| 499 | |
---|
| 500 | Int i; |
---|
| 501 | Int currPicPosition = encRCGOP->getNumPic()-encRCGOP->getPicLeft(); |
---|
| 502 | Int currPicRatio = encRCSeq->getBitRatio( currPicPosition ); |
---|
| 503 | Int totalPicRatio = 0; |
---|
| 504 | for ( i=currPicPosition; i<encRCGOP->getNumPic(); i++ ) |
---|
| 505 | { |
---|
| 506 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | targetBits = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio ); |
---|
| 510 | |
---|
| 511 | if ( targetBits < 100 ) |
---|
| 512 | { |
---|
| 513 | targetBits = 100; // at least allocate 100 bits for one picture |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | if ( m_encRCSeq->getFramesLeft() > 16 ) |
---|
| 517 | { |
---|
| 518 | targetBits = Int( g_RCWeightPicRargetBitInBuffer * targetBits + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP( currPicPosition ) ); |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | return targetBits; |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | Int TEncRCPic::xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ) |
---|
| 525 | { |
---|
| 526 | Int numPreviousPics = 0; |
---|
| 527 | Int totalPreviousBits = 0; |
---|
| 528 | |
---|
| 529 | list<TEncRCPic*>::iterator it; |
---|
| 530 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
| 531 | { |
---|
| 532 | if ( (*it)->getFrameLevel() == frameLevel ) |
---|
| 533 | { |
---|
| 534 | totalPreviousBits += (*it)->getPicActualHeaderBits(); |
---|
| 535 | numPreviousPics++; |
---|
| 536 | } |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | Int estHeaderBits = 0; |
---|
| 540 | if ( numPreviousPics > 0 ) |
---|
| 541 | { |
---|
| 542 | estHeaderBits = totalPreviousBits / numPreviousPics; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | return estHeaderBits; |
---|
| 546 | } |
---|
| 547 | |
---|
[1537] | 548 | #if V0078_ADAPTIVE_LOWER_BOUND |
---|
| 549 | Int TEncRCPic::xEstPicLowerBound(TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP) |
---|
| 550 | { |
---|
| 551 | Int lowerBound = 0; |
---|
| 552 | Int GOPbitsLeft = encRCGOP->getBitsLeft(); |
---|
| 553 | |
---|
| 554 | const Int nextPicPosition = (encRCGOP->getNumPic() - encRCGOP->getPicLeft() + 1) % encRCGOP->getNumPic(); |
---|
| 555 | const Int nextPicRatio = encRCSeq->getBitRatio(nextPicPosition); |
---|
| 556 | |
---|
| 557 | Int totalPicRatio = 0; |
---|
| 558 | for (Int i = nextPicPosition; i < encRCGOP->getNumPic(); i++) |
---|
| 559 | { |
---|
| 560 | totalPicRatio += encRCSeq->getBitRatio(i); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | if (nextPicPosition == 0) |
---|
| 564 | { |
---|
| 565 | GOPbitsLeft = encRCGOP->getTargetBits(); |
---|
| 566 | } |
---|
| 567 | else |
---|
| 568 | { |
---|
| 569 | GOPbitsLeft -= m_targetBits; |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | lowerBound = Int(((Double)GOPbitsLeft) * nextPicRatio / totalPicRatio); |
---|
| 573 | |
---|
| 574 | if (lowerBound < 100) |
---|
| 575 | { |
---|
| 576 | lowerBound = 100; // at least allocate 100 bits for one picture |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | if (m_encRCSeq->getFramesLeft() > 16) |
---|
| 580 | { |
---|
| 581 | lowerBound = Int(g_RCWeightPicRargetBitInBuffer * lowerBound + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP(nextPicPosition)); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | return lowerBound; |
---|
| 585 | } |
---|
| 586 | #endif |
---|
| 587 | |
---|
[313] | 588 | Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ) |
---|
| 589 | { |
---|
| 590 | if ( listPreviousPictures.size() > g_RCMaxPicListSize ) |
---|
| 591 | { |
---|
| 592 | TEncRCPic* p = listPreviousPictures.front(); |
---|
| 593 | listPreviousPictures.pop_front(); |
---|
| 594 | p->destroy(); |
---|
| 595 | delete p; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | listPreviousPictures.push_back( this ); |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ) |
---|
| 602 | { |
---|
| 603 | destroy(); |
---|
| 604 | m_encRCSeq = encRCSeq; |
---|
| 605 | m_encRCGOP = encRCGOP; |
---|
| 606 | |
---|
| 607 | Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP ); |
---|
| 608 | Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel ); |
---|
| 609 | |
---|
| 610 | if ( targetBits < estHeaderBits + 100 ) |
---|
| 611 | { |
---|
| 612 | targetBits = estHeaderBits + 100; // at least allocate 100 bits for picture data |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | m_frameLevel = frameLevel; |
---|
| 616 | m_numberOfPixel = encRCSeq->getNumPixel(); |
---|
| 617 | m_numberOfLCU = encRCSeq->getNumberOfLCU(); |
---|
| 618 | m_estPicLambda = 100.0; |
---|
| 619 | m_targetBits = targetBits; |
---|
| 620 | m_estHeaderBits = estHeaderBits; |
---|
| 621 | m_bitsLeft = m_targetBits; |
---|
| 622 | Int picWidth = encRCSeq->getPicWidth(); |
---|
| 623 | Int picHeight = encRCSeq->getPicHeight(); |
---|
| 624 | Int LCUWidth = encRCSeq->getLCUWidth(); |
---|
| 625 | Int LCUHeight = encRCSeq->getLCUHeight(); |
---|
| 626 | Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1; |
---|
| 627 | Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1; |
---|
[1537] | 628 | #if V0078_ADAPTIVE_LOWER_BOUND |
---|
| 629 | m_lowerBound = xEstPicLowerBound( encRCSeq, encRCGOP ); |
---|
| 630 | #endif |
---|
[313] | 631 | |
---|
| 632 | m_LCULeft = m_numberOfLCU; |
---|
| 633 | m_bitsLeft -= m_estHeaderBits; |
---|
| 634 | m_pixelsLeft = m_numberOfPixel; |
---|
| 635 | |
---|
| 636 | m_LCUs = new TRCLCU[m_numberOfLCU]; |
---|
| 637 | Int i, j; |
---|
| 638 | Int LCUIdx; |
---|
| 639 | for ( i=0; i<picWidthInLCU; i++ ) |
---|
| 640 | { |
---|
| 641 | for ( j=0; j<picHeightInLCU; j++ ) |
---|
| 642 | { |
---|
| 643 | LCUIdx = j*picWidthInLCU + i; |
---|
| 644 | m_LCUs[LCUIdx].m_actualBits = 0; |
---|
| 645 | m_LCUs[LCUIdx].m_QP = 0; |
---|
| 646 | m_LCUs[LCUIdx].m_lambda = 0.0; |
---|
| 647 | m_LCUs[LCUIdx].m_targetBits = 0; |
---|
| 648 | m_LCUs[LCUIdx].m_bitWeight = 1.0; |
---|
| 649 | Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); |
---|
| 650 | Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); |
---|
| 651 | m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight; |
---|
| 652 | } |
---|
| 653 | } |
---|
| 654 | m_picActualHeaderBits = 0; |
---|
| 655 | m_picActualBits = 0; |
---|
| 656 | m_picQP = 0; |
---|
| 657 | m_picLambda = 0.0; |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | Void TEncRCPic::destroy() |
---|
| 661 | { |
---|
| 662 | if( m_LCUs != NULL ) |
---|
| 663 | { |
---|
| 664 | delete[] m_LCUs; |
---|
| 665 | m_LCUs = NULL; |
---|
| 666 | } |
---|
| 667 | m_encRCSeq = NULL; |
---|
| 668 | m_encRCGOP = NULL; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | |
---|
| 672 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType) |
---|
| 673 | { |
---|
| 674 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 675 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 676 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
---|
| 677 | Double estLambda; |
---|
| 678 | if (eSliceType == I_SLICE) |
---|
| 679 | { |
---|
[1029] | 680 | estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp); |
---|
[313] | 681 | } |
---|
| 682 | else |
---|
| 683 | { |
---|
| 684 | estLambda = alpha * pow( bpp, beta ); |
---|
| 685 | } |
---|
[1029] | 686 | |
---|
[313] | 687 | Double lastLevelLambda = -1.0; |
---|
| 688 | Double lastPicLambda = -1.0; |
---|
| 689 | Double lastValidLambda = -1.0; |
---|
| 690 | list<TEncRCPic*>::iterator it; |
---|
| 691 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
| 692 | { |
---|
| 693 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
| 694 | { |
---|
| 695 | lastLevelLambda = (*it)->getPicActualLambda(); |
---|
| 696 | } |
---|
| 697 | lastPicLambda = (*it)->getPicActualLambda(); |
---|
| 698 | |
---|
| 699 | if ( lastPicLambda > 0.0 ) |
---|
| 700 | { |
---|
| 701 | lastValidLambda = lastPicLambda; |
---|
| 702 | } |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | if ( lastLevelLambda > 0.0 ) |
---|
| 706 | { |
---|
| 707 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
---|
| 708 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | if ( lastPicLambda > 0.0 ) |
---|
| 712 | { |
---|
| 713 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
---|
| 714 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
---|
| 715 | } |
---|
| 716 | else if ( lastValidLambda > 0.0 ) |
---|
| 717 | { |
---|
| 718 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
---|
| 719 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
---|
| 720 | } |
---|
| 721 | else |
---|
| 722 | { |
---|
| 723 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | if ( estLambda < 0.1 ) |
---|
| 727 | { |
---|
| 728 | estLambda = 0.1; |
---|
| 729 | } |
---|
| 730 | |
---|
| 731 | m_estPicLambda = estLambda; |
---|
| 732 | |
---|
| 733 | Double totalWeight = 0.0; |
---|
| 734 | // initial BU bit allocation weight |
---|
| 735 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
| 736 | { |
---|
| 737 | Double alphaLCU, betaLCU; |
---|
| 738 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 739 | { |
---|
| 740 | alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
---|
| 741 | betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
---|
| 742 | } |
---|
| 743 | else |
---|
| 744 | { |
---|
| 745 | alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 746 | betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU ); |
---|
| 750 | |
---|
| 751 | if ( m_LCUs[i].m_bitWeight < 0.01 ) |
---|
| 752 | { |
---|
| 753 | m_LCUs[i].m_bitWeight = 0.01; |
---|
| 754 | } |
---|
| 755 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
| 756 | } |
---|
| 757 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
| 758 | { |
---|
| 759 | Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight; |
---|
| 760 | m_LCUs[i].m_bitWeight = BUTargetBits; |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | return estLambda; |
---|
| 764 | } |
---|
| 765 | |
---|
| 766 | Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures ) |
---|
| 767 | { |
---|
[1029] | 768 | Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
[313] | 769 | |
---|
| 770 | Int lastLevelQP = g_RCInvalidQPValue; |
---|
| 771 | Int lastPicQP = g_RCInvalidQPValue; |
---|
| 772 | Int lastValidQP = g_RCInvalidQPValue; |
---|
| 773 | list<TEncRCPic*>::iterator it; |
---|
| 774 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
| 775 | { |
---|
| 776 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
| 777 | { |
---|
| 778 | lastLevelQP = (*it)->getPicActualQP(); |
---|
| 779 | } |
---|
| 780 | lastPicQP = (*it)->getPicActualQP(); |
---|
| 781 | if ( lastPicQP > g_RCInvalidQPValue ) |
---|
| 782 | { |
---|
| 783 | lastValidQP = lastPicQP; |
---|
| 784 | } |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | if ( lastLevelQP > g_RCInvalidQPValue ) |
---|
| 788 | { |
---|
| 789 | QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP ); |
---|
| 790 | } |
---|
| 791 | |
---|
| 792 | if( lastPicQP > g_RCInvalidQPValue ) |
---|
| 793 | { |
---|
| 794 | QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP ); |
---|
| 795 | } |
---|
| 796 | else if( lastValidQP > g_RCInvalidQPValue ) |
---|
| 797 | { |
---|
| 798 | QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP ); |
---|
| 799 | } |
---|
| 800 | |
---|
| 801 | return QP; |
---|
| 802 | } |
---|
| 803 | |
---|
[1029] | 804 | Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) |
---|
[313] | 805 | { |
---|
| 806 | Int LCUIdx = getLCUCoded(); |
---|
| 807 | Double bpp = -1.0; |
---|
| 808 | Int avgBits = 0; |
---|
| 809 | |
---|
[540] | 810 | if (eSliceType == I_SLICE) |
---|
| 811 | { |
---|
[313] | 812 | Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1; |
---|
| 813 | Int bitrateWindow = min(4,noOfLCUsLeft); |
---|
| 814 | Double MAD = getLCU(LCUIdx).m_costIntra; |
---|
| 815 | |
---|
| 816 | if (m_remainingCostIntra > 0.1 ) |
---|
| 817 | { |
---|
| 818 | Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow; |
---|
| 819 | avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra ); |
---|
| 820 | } |
---|
| 821 | else |
---|
| 822 | { |
---|
| 823 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
| 824 | } |
---|
| 825 | m_remainingCostIntra -= MAD; |
---|
| 826 | } |
---|
| 827 | else |
---|
| 828 | { |
---|
[1029] | 829 | Double totalWeight = 0; |
---|
| 830 | for ( Int i=LCUIdx; i<m_numberOfLCU; i++ ) |
---|
| 831 | { |
---|
| 832 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
| 833 | } |
---|
| 834 | Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() ); |
---|
| 835 | avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 ); |
---|
[313] | 836 | } |
---|
| 837 | |
---|
| 838 | if ( avgBits < 1 ) |
---|
| 839 | { |
---|
| 840 | avgBits = 1; |
---|
| 841 | } |
---|
| 842 | |
---|
| 843 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
---|
| 844 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
---|
| 845 | |
---|
| 846 | return bpp; |
---|
| 847 | } |
---|
| 848 | |
---|
| 849 | Double TEncRCPic::getLCUEstLambda( Double bpp ) |
---|
| 850 | { |
---|
| 851 | Int LCUIdx = getLCUCoded(); |
---|
| 852 | Double alpha; |
---|
| 853 | Double beta; |
---|
| 854 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 855 | { |
---|
| 856 | alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
| 857 | beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
| 858 | } |
---|
| 859 | else |
---|
| 860 | { |
---|
| 861 | alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 862 | beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 863 | } |
---|
| 864 | |
---|
| 865 | Double estLambda = alpha * pow( bpp, beta ); |
---|
| 866 | //for Lambda clip, picture level clip |
---|
| 867 | Double clipPicLambda = m_estPicLambda; |
---|
| 868 | |
---|
| 869 | //for Lambda clip, LCU level clip |
---|
| 870 | Double clipNeighbourLambda = -1.0; |
---|
[1029] | 871 | for ( Int i=LCUIdx - 1; i>=0; i-- ) |
---|
[313] | 872 | { |
---|
| 873 | if ( m_LCUs[i].m_lambda > 0 ) |
---|
| 874 | { |
---|
| 875 | clipNeighbourLambda = m_LCUs[i].m_lambda; |
---|
| 876 | break; |
---|
| 877 | } |
---|
| 878 | } |
---|
| 879 | |
---|
| 880 | if ( clipNeighbourLambda > 0.0 ) |
---|
| 881 | { |
---|
| 882 | estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda ); |
---|
[1029] | 883 | } |
---|
[313] | 884 | |
---|
| 885 | if ( clipPicLambda > 0.0 ) |
---|
| 886 | { |
---|
| 887 | estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda ); |
---|
| 888 | } |
---|
| 889 | else |
---|
| 890 | { |
---|
| 891 | estLambda = Clip3( 10.0, 1000.0, estLambda ); |
---|
| 892 | } |
---|
| 893 | |
---|
| 894 | if ( estLambda < 0.1 ) |
---|
| 895 | { |
---|
| 896 | estLambda = 0.1; |
---|
| 897 | } |
---|
| 898 | |
---|
| 899 | return estLambda; |
---|
| 900 | } |
---|
| 901 | |
---|
| 902 | Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP ) |
---|
| 903 | { |
---|
| 904 | Int LCUIdx = getLCUCoded(); |
---|
| 905 | Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
| 906 | |
---|
| 907 | //for Lambda clip, LCU level clip |
---|
| 908 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
[1029] | 909 | for ( Int i=LCUIdx - 1; i>=0; i-- ) |
---|
[313] | 910 | { |
---|
| 911 | if ( (getLCU(i)).m_QP > g_RCInvalidQPValue ) |
---|
| 912 | { |
---|
| 913 | clipNeighbourQP = getLCU(i).m_QP; |
---|
| 914 | break; |
---|
| 915 | } |
---|
| 916 | } |
---|
| 917 | |
---|
| 918 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
| 919 | { |
---|
| 920 | estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP ); |
---|
| 921 | } |
---|
| 922 | |
---|
| 923 | estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP ); |
---|
| 924 | |
---|
| 925 | return estQP; |
---|
| 926 | } |
---|
| 927 | |
---|
[1029] | 928 | Void TEncRCPic::updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter ) |
---|
[313] | 929 | { |
---|
| 930 | m_LCUs[LCUIdx].m_actualBits = bits; |
---|
| 931 | m_LCUs[LCUIdx].m_QP = QP; |
---|
| 932 | m_LCUs[LCUIdx].m_lambda = lambda; |
---|
| 933 | |
---|
| 934 | m_LCULeft--; |
---|
| 935 | m_bitsLeft -= bits; |
---|
| 936 | m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel; |
---|
| 937 | |
---|
| 938 | if ( !updateLCUParameter ) |
---|
| 939 | { |
---|
| 940 | return; |
---|
| 941 | } |
---|
| 942 | |
---|
| 943 | if ( !m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 944 | { |
---|
| 945 | return; |
---|
| 946 | } |
---|
| 947 | |
---|
| 948 | Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
| 949 | Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
| 950 | |
---|
| 951 | Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits; |
---|
| 952 | Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel; |
---|
| 953 | Double bpp = ( Double )LCUActualBits/( Double )LCUTotalPixels; |
---|
| 954 | Double calLambda = alpha * pow( bpp, beta ); |
---|
| 955 | Double inputLambda = m_LCUs[LCUIdx].m_lambda; |
---|
| 956 | |
---|
| 957 | if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 ) |
---|
| 958 | { |
---|
| 959 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
| 960 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
| 961 | |
---|
| 962 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 963 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
| 964 | |
---|
| 965 | TRCParameter rcPara; |
---|
| 966 | rcPara.m_alpha = alpha; |
---|
| 967 | rcPara.m_beta = beta; |
---|
| 968 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
| 969 | |
---|
| 970 | return; |
---|
| 971 | } |
---|
| 972 | |
---|
| 973 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
| 974 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
[1029] | 975 | Double lnbpp = log( bpp ); |
---|
[313] | 976 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
| 977 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
| 978 | |
---|
| 979 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 980 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
[1029] | 981 | |
---|
[313] | 982 | TRCParameter rcPara; |
---|
| 983 | rcPara.m_alpha = alpha; |
---|
| 984 | rcPara.m_beta = beta; |
---|
| 985 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
| 986 | |
---|
| 987 | } |
---|
| 988 | |
---|
| 989 | Double TEncRCPic::calAverageQP() |
---|
| 990 | { |
---|
| 991 | Int totalQPs = 0; |
---|
| 992 | Int numTotalLCUs = 0; |
---|
| 993 | |
---|
| 994 | Int i; |
---|
| 995 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
| 996 | { |
---|
| 997 | if ( m_LCUs[i].m_QP > 0 ) |
---|
| 998 | { |
---|
| 999 | totalQPs += m_LCUs[i].m_QP; |
---|
| 1000 | numTotalLCUs++; |
---|
| 1001 | } |
---|
| 1002 | } |
---|
| 1003 | |
---|
| 1004 | Double avgQP = 0.0; |
---|
| 1005 | if ( numTotalLCUs == 0 ) |
---|
| 1006 | { |
---|
| 1007 | avgQP = g_RCInvalidQPValue; |
---|
| 1008 | } |
---|
| 1009 | else |
---|
| 1010 | { |
---|
| 1011 | avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs); |
---|
| 1012 | } |
---|
| 1013 | return avgQP; |
---|
| 1014 | } |
---|
| 1015 | |
---|
| 1016 | Double TEncRCPic::calAverageLambda() |
---|
| 1017 | { |
---|
| 1018 | Double totalLambdas = 0.0; |
---|
| 1019 | Int numTotalLCUs = 0; |
---|
| 1020 | |
---|
| 1021 | Int i; |
---|
| 1022 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
| 1023 | { |
---|
| 1024 | if ( m_LCUs[i].m_lambda > 0.01 ) |
---|
| 1025 | { |
---|
| 1026 | totalLambdas += log( m_LCUs[i].m_lambda ); |
---|
| 1027 | numTotalLCUs++; |
---|
| 1028 | } |
---|
| 1029 | } |
---|
| 1030 | |
---|
[1029] | 1031 | Double avgLambda; |
---|
[313] | 1032 | if( numTotalLCUs == 0 ) |
---|
| 1033 | { |
---|
| 1034 | avgLambda = -1.0; |
---|
| 1035 | } |
---|
| 1036 | else |
---|
| 1037 | { |
---|
| 1038 | avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs ); |
---|
| 1039 | } |
---|
| 1040 | return avgLambda; |
---|
| 1041 | } |
---|
| 1042 | |
---|
[1029] | 1043 | |
---|
[313] | 1044 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType) |
---|
| 1045 | { |
---|
| 1046 | m_picActualHeaderBits = actualHeaderBits; |
---|
| 1047 | m_picActualBits = actualTotalBits; |
---|
| 1048 | if ( averageQP > 0.0 ) |
---|
| 1049 | { |
---|
| 1050 | m_picQP = Int( averageQP + 0.5 ); |
---|
| 1051 | } |
---|
| 1052 | else |
---|
| 1053 | { |
---|
| 1054 | m_picQP = g_RCInvalidQPValue; |
---|
| 1055 | } |
---|
| 1056 | m_picLambda = averageLambda; |
---|
| 1057 | |
---|
| 1058 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 1059 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
[1029] | 1060 | |
---|
[313] | 1061 | if (eSliceType == I_SLICE) |
---|
| 1062 | { |
---|
| 1063 | updateAlphaBetaIntra(&alpha, &beta); |
---|
| 1064 | } |
---|
| 1065 | else |
---|
| 1066 | { |
---|
[1029] | 1067 | // update parameters |
---|
| 1068 | Double picActualBits = ( Double )m_picActualBits; |
---|
| 1069 | Double picActualBpp = picActualBits/(Double)m_numberOfPixel; |
---|
| 1070 | Double calLambda = alpha * pow( picActualBpp, beta ); |
---|
| 1071 | Double inputLambda = m_picLambda; |
---|
[313] | 1072 | |
---|
[1029] | 1073 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 ) |
---|
| 1074 | { |
---|
| 1075 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
| 1076 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
[313] | 1077 | |
---|
[1029] | 1078 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 1079 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
[313] | 1080 | |
---|
[1029] | 1081 | TRCParameter rcPara; |
---|
| 1082 | rcPara.m_alpha = alpha; |
---|
| 1083 | rcPara.m_beta = beta; |
---|
| 1084 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
[313] | 1085 | |
---|
[1029] | 1086 | return; |
---|
| 1087 | } |
---|
[313] | 1088 | |
---|
[1029] | 1089 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
| 1090 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
| 1091 | Double lnbpp = log( picActualBpp ); |
---|
| 1092 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
| 1093 | |
---|
| 1094 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
| 1095 | |
---|
| 1096 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 1097 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
[313] | 1098 | } |
---|
| 1099 | |
---|
| 1100 | TRCParameter rcPara; |
---|
| 1101 | rcPara.m_alpha = alpha; |
---|
| 1102 | rcPara.m_beta = beta; |
---|
| 1103 | |
---|
| 1104 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
| 1105 | |
---|
| 1106 | if ( m_frameLevel == 1 ) |
---|
| 1107 | { |
---|
| 1108 | Double currLambda = Clip3( 0.1, 10000.0, m_picLambda ); |
---|
| 1109 | Double updateLastLambda = g_RCWeightHistoryLambda * m_encRCSeq->getLastLambda() + g_RCWeightCurrentLambda * currLambda; |
---|
| 1110 | m_encRCSeq->setLastLambda( updateLastLambda ); |
---|
| 1111 | } |
---|
| 1112 | } |
---|
| 1113 | |
---|
| 1114 | Int TEncRCPic::getRefineBitsForIntra( Int orgBits ) |
---|
| 1115 | { |
---|
| 1116 | Double alpha=0.25, beta=0.5582; |
---|
| 1117 | Int iIntraBits; |
---|
| 1118 | |
---|
| 1119 | if (orgBits*40 < m_numberOfPixel) |
---|
| 1120 | { |
---|
| 1121 | alpha=0.25; |
---|
| 1122 | } |
---|
| 1123 | else |
---|
| 1124 | { |
---|
| 1125 | alpha=0.30; |
---|
| 1126 | } |
---|
| 1127 | |
---|
| 1128 | iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5); |
---|
[1029] | 1129 | |
---|
[313] | 1130 | return iIntraBits; |
---|
| 1131 | } |
---|
| 1132 | |
---|
[1029] | 1133 | Double TEncRCPic::calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel) |
---|
[313] | 1134 | { |
---|
| 1135 | return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) ); |
---|
| 1136 | } |
---|
| 1137 | |
---|
[1029] | 1138 | Void TEncRCPic::updateAlphaBetaIntra(Double *alpha, Double *beta) |
---|
[313] | 1139 | { |
---|
| 1140 | Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1)); |
---|
| 1141 | Double diffLambda = (*beta)*(log((Double)m_picActualBits)-log((Double)m_targetBits)); |
---|
| 1142 | |
---|
| 1143 | diffLambda = Clip3(-0.125, 0.125, 0.25*diffLambda); |
---|
| 1144 | *alpha = (*alpha) * exp(diffLambda); |
---|
| 1145 | *beta = (*beta) + diffLambda / lnbpp; |
---|
| 1146 | } |
---|
| 1147 | |
---|
| 1148 | |
---|
[1029] | 1149 | Void TEncRCPic::getLCUInitTargetBits() |
---|
[313] | 1150 | { |
---|
| 1151 | Int iAvgBits = 0; |
---|
| 1152 | |
---|
| 1153 | m_remainingCostIntra = m_totalCostIntra; |
---|
| 1154 | for (Int i=m_numberOfLCU-1; i>=0; i--) |
---|
| 1155 | { |
---|
| 1156 | iAvgBits += Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra); |
---|
| 1157 | getLCU(i).m_targetBitsLeft = iAvgBits; |
---|
| 1158 | } |
---|
| 1159 | } |
---|
| 1160 | |
---|
| 1161 | |
---|
[1029] | 1162 | Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP) |
---|
[313] | 1163 | { |
---|
| 1164 | Int LCUIdx = getLCUCoded(); |
---|
| 1165 | |
---|
| 1166 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 1167 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 1168 | |
---|
| 1169 | Double costPerPixel = getLCU(LCUIdx).m_costIntra/(Double)getLCU(LCUIdx).m_numberOfPixel; |
---|
| 1170 | costPerPixel = pow(costPerPixel, BETA1); |
---|
| 1171 | Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp); |
---|
| 1172 | |
---|
| 1173 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
[1029] | 1174 | for (Int i=LCUIdx-1; i>=0; i--) |
---|
[313] | 1175 | { |
---|
| 1176 | if ((getLCU(i)).m_QP > g_RCInvalidQPValue) |
---|
| 1177 | { |
---|
| 1178 | clipNeighbourQP = getLCU(i).m_QP; |
---|
| 1179 | break; |
---|
| 1180 | } |
---|
| 1181 | } |
---|
| 1182 | |
---|
| 1183 | Int minQP = clipPicQP - 2; |
---|
| 1184 | Int maxQP = clipPicQP + 2; |
---|
| 1185 | |
---|
| 1186 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
| 1187 | { |
---|
[1029] | 1188 | maxQP = min(clipNeighbourQP + 1, maxQP); |
---|
| 1189 | minQP = max(clipNeighbourQP - 1, minQP); |
---|
[313] | 1190 | } |
---|
| 1191 | |
---|
| 1192 | Double maxLambda=exp(((Double)(maxQP+0.49)-13.7122)/4.2005); |
---|
| 1193 | Double minLambda=exp(((Double)(minQP-0.49)-13.7122)/4.2005); |
---|
| 1194 | |
---|
| 1195 | estLambda = Clip3(minLambda, maxLambda, estLambda); |
---|
| 1196 | |
---|
| 1197 | *estQP = Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 ); |
---|
| 1198 | *estQP = Clip3(minQP, maxQP, *estQP); |
---|
| 1199 | |
---|
| 1200 | return estLambda; |
---|
| 1201 | } |
---|
| 1202 | |
---|
| 1203 | TEncRateCtrl::TEncRateCtrl() |
---|
| 1204 | { |
---|
| 1205 | m_encRCSeq = NULL; |
---|
| 1206 | m_encRCGOP = NULL; |
---|
| 1207 | m_encRCPic = NULL; |
---|
| 1208 | } |
---|
| 1209 | |
---|
| 1210 | TEncRateCtrl::~TEncRateCtrl() |
---|
| 1211 | { |
---|
| 1212 | destroy(); |
---|
| 1213 | } |
---|
| 1214 | |
---|
| 1215 | Void TEncRateCtrl::destroy() |
---|
| 1216 | { |
---|
| 1217 | if ( m_encRCSeq != NULL ) |
---|
| 1218 | { |
---|
| 1219 | delete m_encRCSeq; |
---|
| 1220 | m_encRCSeq = NULL; |
---|
| 1221 | } |
---|
| 1222 | if ( m_encRCGOP != NULL ) |
---|
| 1223 | { |
---|
| 1224 | delete m_encRCGOP; |
---|
| 1225 | m_encRCGOP = NULL; |
---|
| 1226 | } |
---|
| 1227 | while ( m_listRCPictures.size() > 0 ) |
---|
| 1228 | { |
---|
| 1229 | TEncRCPic* p = m_listRCPictures.front(); |
---|
| 1230 | m_listRCPictures.pop_front(); |
---|
| 1231 | delete p; |
---|
| 1232 | } |
---|
| 1233 | } |
---|
| 1234 | |
---|
| 1235 | 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] ) |
---|
| 1236 | { |
---|
| 1237 | destroy(); |
---|
| 1238 | |
---|
| 1239 | Bool isLowdelay = true; |
---|
| 1240 | for ( Int i=0; i<GOPSize-1; i++ ) |
---|
| 1241 | { |
---|
| 1242 | if ( GOPList[i].m_POC > GOPList[i+1].m_POC ) |
---|
| 1243 | { |
---|
| 1244 | isLowdelay = false; |
---|
| 1245 | break; |
---|
| 1246 | } |
---|
| 1247 | } |
---|
| 1248 | |
---|
| 1249 | Int numberOfLevel = 1; |
---|
| 1250 | Int adaptiveBit = 0; |
---|
| 1251 | if ( keepHierBits > 0 ) |
---|
| 1252 | { |
---|
| 1253 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
| 1254 | } |
---|
| 1255 | if ( !isLowdelay && GOPSize == 8 ) |
---|
| 1256 | { |
---|
| 1257 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
| 1258 | } |
---|
| 1259 | numberOfLevel++; // intra picture |
---|
| 1260 | numberOfLevel++; // non-reference picture |
---|
| 1261 | |
---|
| 1262 | |
---|
| 1263 | Int* bitsRatio; |
---|
| 1264 | bitsRatio = new Int[ GOPSize ]; |
---|
| 1265 | for ( Int i=0; i<GOPSize; i++ ) |
---|
| 1266 | { |
---|
| 1267 | bitsRatio[i] = 10; |
---|
| 1268 | if ( !GOPList[i].m_refPic ) |
---|
| 1269 | { |
---|
| 1270 | bitsRatio[i] = 2; |
---|
| 1271 | } |
---|
| 1272 | } |
---|
| 1273 | |
---|
| 1274 | if ( keepHierBits > 0 ) |
---|
| 1275 | { |
---|
| 1276 | Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); |
---|
| 1277 | if ( GOPSize == 4 && isLowdelay ) |
---|
| 1278 | { |
---|
| 1279 | if ( bpp > 0.2 ) |
---|
| 1280 | { |
---|
| 1281 | bitsRatio[0] = 2; |
---|
| 1282 | bitsRatio[1] = 3; |
---|
| 1283 | bitsRatio[2] = 2; |
---|
| 1284 | bitsRatio[3] = 6; |
---|
| 1285 | } |
---|
| 1286 | else if( bpp > 0.1 ) |
---|
| 1287 | { |
---|
| 1288 | bitsRatio[0] = 2; |
---|
| 1289 | bitsRatio[1] = 3; |
---|
| 1290 | bitsRatio[2] = 2; |
---|
| 1291 | bitsRatio[3] = 10; |
---|
| 1292 | } |
---|
| 1293 | else if ( bpp > 0.05 ) |
---|
| 1294 | { |
---|
| 1295 | bitsRatio[0] = 2; |
---|
| 1296 | bitsRatio[1] = 3; |
---|
| 1297 | bitsRatio[2] = 2; |
---|
| 1298 | bitsRatio[3] = 12; |
---|
| 1299 | } |
---|
| 1300 | else |
---|
| 1301 | { |
---|
| 1302 | bitsRatio[0] = 2; |
---|
| 1303 | bitsRatio[1] = 3; |
---|
| 1304 | bitsRatio[2] = 2; |
---|
| 1305 | bitsRatio[3] = 14; |
---|
| 1306 | } |
---|
[1029] | 1307 | |
---|
[313] | 1308 | if ( keepHierBits == 2 ) |
---|
| 1309 | { |
---|
| 1310 | adaptiveBit = 1; |
---|
| 1311 | } |
---|
| 1312 | } |
---|
| 1313 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
| 1314 | { |
---|
| 1315 | if ( bpp > 0.2 ) |
---|
| 1316 | { |
---|
| 1317 | bitsRatio[0] = 15; |
---|
| 1318 | bitsRatio[1] = 5; |
---|
| 1319 | bitsRatio[2] = 4; |
---|
| 1320 | bitsRatio[3] = 1; |
---|
| 1321 | bitsRatio[4] = 1; |
---|
| 1322 | bitsRatio[5] = 4; |
---|
| 1323 | bitsRatio[6] = 1; |
---|
| 1324 | bitsRatio[7] = 1; |
---|
| 1325 | } |
---|
| 1326 | else if ( bpp > 0.1 ) |
---|
| 1327 | { |
---|
| 1328 | bitsRatio[0] = 20; |
---|
| 1329 | bitsRatio[1] = 6; |
---|
| 1330 | bitsRatio[2] = 4; |
---|
| 1331 | bitsRatio[3] = 1; |
---|
| 1332 | bitsRatio[4] = 1; |
---|
| 1333 | bitsRatio[5] = 4; |
---|
| 1334 | bitsRatio[6] = 1; |
---|
| 1335 | bitsRatio[7] = 1; |
---|
| 1336 | } |
---|
| 1337 | else if ( bpp > 0.05 ) |
---|
| 1338 | { |
---|
| 1339 | bitsRatio[0] = 25; |
---|
| 1340 | bitsRatio[1] = 7; |
---|
| 1341 | bitsRatio[2] = 4; |
---|
| 1342 | bitsRatio[3] = 1; |
---|
| 1343 | bitsRatio[4] = 1; |
---|
| 1344 | bitsRatio[5] = 4; |
---|
| 1345 | bitsRatio[6] = 1; |
---|
| 1346 | bitsRatio[7] = 1; |
---|
| 1347 | } |
---|
| 1348 | else |
---|
| 1349 | { |
---|
| 1350 | bitsRatio[0] = 30; |
---|
| 1351 | bitsRatio[1] = 8; |
---|
| 1352 | bitsRatio[2] = 4; |
---|
| 1353 | bitsRatio[3] = 1; |
---|
| 1354 | bitsRatio[4] = 1; |
---|
| 1355 | bitsRatio[5] = 4; |
---|
| 1356 | bitsRatio[6] = 1; |
---|
| 1357 | bitsRatio[7] = 1; |
---|
| 1358 | } |
---|
[1029] | 1359 | |
---|
[313] | 1360 | if ( keepHierBits == 2 ) |
---|
| 1361 | { |
---|
| 1362 | adaptiveBit = 2; |
---|
| 1363 | } |
---|
| 1364 | } |
---|
| 1365 | else |
---|
| 1366 | { |
---|
| 1367 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" ); |
---|
| 1368 | } |
---|
| 1369 | } |
---|
| 1370 | |
---|
[1029] | 1371 | Int* GOPID2Level = new Int[ GOPSize ]; |
---|
| 1372 | for ( Int i=0; i<GOPSize; i++ ) |
---|
[313] | 1373 | { |
---|
| 1374 | GOPID2Level[i] = 1; |
---|
| 1375 | if ( !GOPList[i].m_refPic ) |
---|
| 1376 | { |
---|
| 1377 | GOPID2Level[i] = 2; |
---|
| 1378 | } |
---|
| 1379 | } |
---|
[1029] | 1380 | |
---|
[313] | 1381 | if ( keepHierBits > 0 ) |
---|
| 1382 | { |
---|
| 1383 | if ( GOPSize == 4 && isLowdelay ) |
---|
| 1384 | { |
---|
| 1385 | GOPID2Level[0] = 3; |
---|
| 1386 | GOPID2Level[1] = 2; |
---|
| 1387 | GOPID2Level[2] = 3; |
---|
| 1388 | GOPID2Level[3] = 1; |
---|
| 1389 | } |
---|
| 1390 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
| 1391 | { |
---|
| 1392 | GOPID2Level[0] = 1; |
---|
| 1393 | GOPID2Level[1] = 2; |
---|
| 1394 | GOPID2Level[2] = 3; |
---|
| 1395 | GOPID2Level[3] = 4; |
---|
| 1396 | GOPID2Level[4] = 4; |
---|
| 1397 | GOPID2Level[5] = 3; |
---|
| 1398 | GOPID2Level[6] = 4; |
---|
| 1399 | GOPID2Level[7] = 4; |
---|
| 1400 | } |
---|
| 1401 | } |
---|
| 1402 | |
---|
| 1403 | if ( !isLowdelay && GOPSize == 8 ) |
---|
| 1404 | { |
---|
| 1405 | GOPID2Level[0] = 1; |
---|
| 1406 | GOPID2Level[1] = 2; |
---|
| 1407 | GOPID2Level[2] = 3; |
---|
| 1408 | GOPID2Level[3] = 4; |
---|
| 1409 | GOPID2Level[4] = 4; |
---|
| 1410 | GOPID2Level[5] = 3; |
---|
| 1411 | GOPID2Level[6] = 4; |
---|
| 1412 | GOPID2Level[7] = 4; |
---|
| 1413 | } |
---|
| 1414 | |
---|
| 1415 | m_encRCSeq = new TEncRCSeq; |
---|
| 1416 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit ); |
---|
| 1417 | m_encRCSeq->initBitsRatio( bitsRatio ); |
---|
| 1418 | m_encRCSeq->initGOPID2Level( GOPID2Level ); |
---|
| 1419 | m_encRCSeq->initPicPara(); |
---|
| 1420 | if ( useLCUSeparateModel ) |
---|
| 1421 | { |
---|
| 1422 | m_encRCSeq->initLCUPara(); |
---|
| 1423 | } |
---|
[1433] | 1424 | #if U0132_TARGET_BITS_SATURATION |
---|
| 1425 | m_CpbSaturationEnabled = false; |
---|
| 1426 | m_cpbSize = targetBitrate; |
---|
| 1427 | m_cpbState = (UInt)(m_cpbSize*0.5f); |
---|
| 1428 | m_bufferingRate = (Int)(targetBitrate / frameRate); |
---|
| 1429 | #endif |
---|
[313] | 1430 | |
---|
| 1431 | delete[] bitsRatio; |
---|
| 1432 | delete[] GOPID2Level; |
---|
| 1433 | } |
---|
| 1434 | |
---|
| 1435 | Void TEncRateCtrl::initRCPic( Int frameLevel ) |
---|
| 1436 | { |
---|
| 1437 | m_encRCPic = new TEncRCPic; |
---|
| 1438 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures ); |
---|
| 1439 | } |
---|
| 1440 | |
---|
| 1441 | Void TEncRateCtrl::initRCGOP( Int numberOfPictures ) |
---|
| 1442 | { |
---|
| 1443 | m_encRCGOP = new TEncRCGOP; |
---|
| 1444 | m_encRCGOP->create( m_encRCSeq, numberOfPictures ); |
---|
| 1445 | } |
---|
| 1446 | |
---|
[1433] | 1447 | #if U0132_TARGET_BITS_SATURATION |
---|
| 1448 | Int TEncRateCtrl::updateCpbState(Int actualBits) |
---|
| 1449 | { |
---|
| 1450 | Int cpbState = 1; |
---|
| 1451 | |
---|
| 1452 | m_cpbState -= actualBits; |
---|
| 1453 | if (m_cpbState < 0) |
---|
| 1454 | { |
---|
| 1455 | cpbState = -1; |
---|
| 1456 | } |
---|
| 1457 | |
---|
| 1458 | m_cpbState += m_bufferingRate; |
---|
| 1459 | if (m_cpbState > m_cpbSize) |
---|
| 1460 | { |
---|
| 1461 | cpbState = 0; |
---|
| 1462 | } |
---|
| 1463 | |
---|
| 1464 | return cpbState; |
---|
| 1465 | } |
---|
| 1466 | |
---|
| 1467 | Void TEncRateCtrl::initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness) |
---|
| 1468 | { |
---|
| 1469 | m_CpbSaturationEnabled = true; |
---|
| 1470 | m_cpbSize = (pcHrd->getCpbSizeValueMinus1(0, 0, 0) + 1) << (4 + pcHrd->getCpbSizeScale()); |
---|
| 1471 | m_cpbState = (UInt)(m_cpbSize*fInitialCpbFullness); |
---|
| 1472 | m_bufferingRate = (UInt)(((pcHrd->getBitRateValueMinus1(0, 0, 0) + 1) << (6 + pcHrd->getBitRateScale())) / iFrameRate); |
---|
| 1473 | printf("\nHRD - [Initial CPB state %6d] [CPB Size %6d] [Buffering Rate %6d]\n", m_cpbState, m_cpbSize, m_bufferingRate); |
---|
| 1474 | } |
---|
| 1475 | #endif |
---|
| 1476 | |
---|
[313] | 1477 | Void TEncRateCtrl::destroyRCGOP() |
---|
| 1478 | { |
---|
| 1479 | delete m_encRCGOP; |
---|
| 1480 | m_encRCGOP = NULL; |
---|
| 1481 | } |
---|
| 1482 | |
---|