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