[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 | * |
---|
[1259] | 6 | * Copyright (c) 2010-2015, 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 | |
---|
| 548 | Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ) |
---|
| 549 | { |
---|
| 550 | if ( listPreviousPictures.size() > g_RCMaxPicListSize ) |
---|
| 551 | { |
---|
| 552 | TEncRCPic* p = listPreviousPictures.front(); |
---|
| 553 | listPreviousPictures.pop_front(); |
---|
| 554 | p->destroy(); |
---|
| 555 | delete p; |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | listPreviousPictures.push_back( this ); |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ) |
---|
| 562 | { |
---|
| 563 | destroy(); |
---|
| 564 | m_encRCSeq = encRCSeq; |
---|
| 565 | m_encRCGOP = encRCGOP; |
---|
| 566 | |
---|
| 567 | Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP ); |
---|
| 568 | Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel ); |
---|
| 569 | |
---|
| 570 | if ( targetBits < estHeaderBits + 100 ) |
---|
| 571 | { |
---|
| 572 | targetBits = estHeaderBits + 100; // at least allocate 100 bits for picture data |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | m_frameLevel = frameLevel; |
---|
| 576 | m_numberOfPixel = encRCSeq->getNumPixel(); |
---|
| 577 | m_numberOfLCU = encRCSeq->getNumberOfLCU(); |
---|
| 578 | m_estPicLambda = 100.0; |
---|
| 579 | m_targetBits = targetBits; |
---|
| 580 | m_estHeaderBits = estHeaderBits; |
---|
| 581 | m_bitsLeft = m_targetBits; |
---|
| 582 | Int picWidth = encRCSeq->getPicWidth(); |
---|
| 583 | Int picHeight = encRCSeq->getPicHeight(); |
---|
| 584 | Int LCUWidth = encRCSeq->getLCUWidth(); |
---|
| 585 | Int LCUHeight = encRCSeq->getLCUHeight(); |
---|
| 586 | Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1; |
---|
| 587 | Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1; |
---|
| 588 | |
---|
| 589 | m_LCULeft = m_numberOfLCU; |
---|
| 590 | m_bitsLeft -= m_estHeaderBits; |
---|
| 591 | m_pixelsLeft = m_numberOfPixel; |
---|
| 592 | |
---|
| 593 | m_LCUs = new TRCLCU[m_numberOfLCU]; |
---|
| 594 | Int i, j; |
---|
| 595 | Int LCUIdx; |
---|
| 596 | for ( i=0; i<picWidthInLCU; i++ ) |
---|
| 597 | { |
---|
| 598 | for ( j=0; j<picHeightInLCU; j++ ) |
---|
| 599 | { |
---|
| 600 | LCUIdx = j*picWidthInLCU + i; |
---|
| 601 | m_LCUs[LCUIdx].m_actualBits = 0; |
---|
| 602 | m_LCUs[LCUIdx].m_QP = 0; |
---|
| 603 | m_LCUs[LCUIdx].m_lambda = 0.0; |
---|
| 604 | m_LCUs[LCUIdx].m_targetBits = 0; |
---|
| 605 | m_LCUs[LCUIdx].m_bitWeight = 1.0; |
---|
| 606 | Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); |
---|
| 607 | Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); |
---|
| 608 | m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight; |
---|
| 609 | } |
---|
| 610 | } |
---|
| 611 | m_picActualHeaderBits = 0; |
---|
| 612 | m_picActualBits = 0; |
---|
| 613 | m_picQP = 0; |
---|
| 614 | m_picLambda = 0.0; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | Void TEncRCPic::destroy() |
---|
| 618 | { |
---|
| 619 | if( m_LCUs != NULL ) |
---|
| 620 | { |
---|
| 621 | delete[] m_LCUs; |
---|
| 622 | m_LCUs = NULL; |
---|
| 623 | } |
---|
| 624 | m_encRCSeq = NULL; |
---|
| 625 | m_encRCGOP = NULL; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | |
---|
| 629 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType) |
---|
| 630 | { |
---|
| 631 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 632 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 633 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
---|
| 634 | Double estLambda; |
---|
| 635 | if (eSliceType == I_SLICE) |
---|
| 636 | { |
---|
[1029] | 637 | estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp); |
---|
[313] | 638 | } |
---|
| 639 | else |
---|
| 640 | { |
---|
| 641 | estLambda = alpha * pow( bpp, beta ); |
---|
| 642 | } |
---|
[1029] | 643 | |
---|
[313] | 644 | Double lastLevelLambda = -1.0; |
---|
| 645 | Double lastPicLambda = -1.0; |
---|
| 646 | Double lastValidLambda = -1.0; |
---|
| 647 | list<TEncRCPic*>::iterator it; |
---|
| 648 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
| 649 | { |
---|
| 650 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
| 651 | { |
---|
| 652 | lastLevelLambda = (*it)->getPicActualLambda(); |
---|
| 653 | } |
---|
| 654 | lastPicLambda = (*it)->getPicActualLambda(); |
---|
| 655 | |
---|
| 656 | if ( lastPicLambda > 0.0 ) |
---|
| 657 | { |
---|
| 658 | lastValidLambda = lastPicLambda; |
---|
| 659 | } |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | if ( lastLevelLambda > 0.0 ) |
---|
| 663 | { |
---|
| 664 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
---|
| 665 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | if ( lastPicLambda > 0.0 ) |
---|
| 669 | { |
---|
| 670 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
---|
| 671 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
---|
| 672 | } |
---|
| 673 | else if ( lastValidLambda > 0.0 ) |
---|
| 674 | { |
---|
| 675 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
---|
| 676 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
---|
| 677 | } |
---|
| 678 | else |
---|
| 679 | { |
---|
| 680 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | if ( estLambda < 0.1 ) |
---|
| 684 | { |
---|
| 685 | estLambda = 0.1; |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | m_estPicLambda = estLambda; |
---|
| 689 | |
---|
| 690 | Double totalWeight = 0.0; |
---|
| 691 | // initial BU bit allocation weight |
---|
| 692 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
| 693 | { |
---|
| 694 | Double alphaLCU, betaLCU; |
---|
| 695 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 696 | { |
---|
| 697 | alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
---|
| 698 | betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
---|
| 699 | } |
---|
| 700 | else |
---|
| 701 | { |
---|
| 702 | alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 703 | betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 704 | } |
---|
| 705 | |
---|
| 706 | m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU ); |
---|
| 707 | |
---|
| 708 | if ( m_LCUs[i].m_bitWeight < 0.01 ) |
---|
| 709 | { |
---|
| 710 | m_LCUs[i].m_bitWeight = 0.01; |
---|
| 711 | } |
---|
| 712 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
| 713 | } |
---|
| 714 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
| 715 | { |
---|
| 716 | Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight; |
---|
| 717 | m_LCUs[i].m_bitWeight = BUTargetBits; |
---|
| 718 | } |
---|
| 719 | |
---|
| 720 | return estLambda; |
---|
| 721 | } |
---|
| 722 | |
---|
| 723 | Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures ) |
---|
| 724 | { |
---|
[1029] | 725 | Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
[313] | 726 | |
---|
| 727 | Int lastLevelQP = g_RCInvalidQPValue; |
---|
| 728 | Int lastPicQP = g_RCInvalidQPValue; |
---|
| 729 | Int lastValidQP = g_RCInvalidQPValue; |
---|
| 730 | list<TEncRCPic*>::iterator it; |
---|
| 731 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
| 732 | { |
---|
| 733 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
| 734 | { |
---|
| 735 | lastLevelQP = (*it)->getPicActualQP(); |
---|
| 736 | } |
---|
| 737 | lastPicQP = (*it)->getPicActualQP(); |
---|
| 738 | if ( lastPicQP > g_RCInvalidQPValue ) |
---|
| 739 | { |
---|
| 740 | lastValidQP = lastPicQP; |
---|
| 741 | } |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | if ( lastLevelQP > g_RCInvalidQPValue ) |
---|
| 745 | { |
---|
| 746 | QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP ); |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | if( lastPicQP > g_RCInvalidQPValue ) |
---|
| 750 | { |
---|
| 751 | QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP ); |
---|
| 752 | } |
---|
| 753 | else if( lastValidQP > g_RCInvalidQPValue ) |
---|
| 754 | { |
---|
| 755 | QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP ); |
---|
| 756 | } |
---|
| 757 | |
---|
| 758 | return QP; |
---|
| 759 | } |
---|
| 760 | |
---|
[1029] | 761 | Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) |
---|
[313] | 762 | { |
---|
| 763 | Int LCUIdx = getLCUCoded(); |
---|
| 764 | Double bpp = -1.0; |
---|
| 765 | Int avgBits = 0; |
---|
| 766 | |
---|
[540] | 767 | if (eSliceType == I_SLICE) |
---|
| 768 | { |
---|
[313] | 769 | Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1; |
---|
| 770 | Int bitrateWindow = min(4,noOfLCUsLeft); |
---|
| 771 | Double MAD = getLCU(LCUIdx).m_costIntra; |
---|
| 772 | |
---|
| 773 | if (m_remainingCostIntra > 0.1 ) |
---|
| 774 | { |
---|
| 775 | Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow; |
---|
| 776 | avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra ); |
---|
| 777 | } |
---|
| 778 | else |
---|
| 779 | { |
---|
| 780 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
| 781 | } |
---|
| 782 | m_remainingCostIntra -= MAD; |
---|
| 783 | } |
---|
| 784 | else |
---|
| 785 | { |
---|
[1029] | 786 | Double totalWeight = 0; |
---|
| 787 | for ( Int i=LCUIdx; i<m_numberOfLCU; i++ ) |
---|
| 788 | { |
---|
| 789 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
| 790 | } |
---|
| 791 | Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() ); |
---|
| 792 | avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 ); |
---|
[313] | 793 | } |
---|
| 794 | |
---|
| 795 | if ( avgBits < 1 ) |
---|
| 796 | { |
---|
| 797 | avgBits = 1; |
---|
| 798 | } |
---|
| 799 | |
---|
| 800 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
---|
| 801 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
---|
| 802 | |
---|
| 803 | return bpp; |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | Double TEncRCPic::getLCUEstLambda( Double bpp ) |
---|
| 807 | { |
---|
| 808 | Int LCUIdx = getLCUCoded(); |
---|
| 809 | Double alpha; |
---|
| 810 | Double beta; |
---|
| 811 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 812 | { |
---|
| 813 | alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
| 814 | beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
| 815 | } |
---|
| 816 | else |
---|
| 817 | { |
---|
| 818 | alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 819 | beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 820 | } |
---|
| 821 | |
---|
| 822 | Double estLambda = alpha * pow( bpp, beta ); |
---|
| 823 | //for Lambda clip, picture level clip |
---|
| 824 | Double clipPicLambda = m_estPicLambda; |
---|
| 825 | |
---|
| 826 | //for Lambda clip, LCU level clip |
---|
| 827 | Double clipNeighbourLambda = -1.0; |
---|
[1029] | 828 | for ( Int i=LCUIdx - 1; i>=0; i-- ) |
---|
[313] | 829 | { |
---|
| 830 | if ( m_LCUs[i].m_lambda > 0 ) |
---|
| 831 | { |
---|
| 832 | clipNeighbourLambda = m_LCUs[i].m_lambda; |
---|
| 833 | break; |
---|
| 834 | } |
---|
| 835 | } |
---|
| 836 | |
---|
| 837 | if ( clipNeighbourLambda > 0.0 ) |
---|
| 838 | { |
---|
| 839 | estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda ); |
---|
[1029] | 840 | } |
---|
[313] | 841 | |
---|
| 842 | if ( clipPicLambda > 0.0 ) |
---|
| 843 | { |
---|
| 844 | estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda ); |
---|
| 845 | } |
---|
| 846 | else |
---|
| 847 | { |
---|
| 848 | estLambda = Clip3( 10.0, 1000.0, estLambda ); |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | if ( estLambda < 0.1 ) |
---|
| 852 | { |
---|
| 853 | estLambda = 0.1; |
---|
| 854 | } |
---|
| 855 | |
---|
| 856 | return estLambda; |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP ) |
---|
| 860 | { |
---|
| 861 | Int LCUIdx = getLCUCoded(); |
---|
| 862 | Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
| 863 | |
---|
| 864 | //for Lambda clip, LCU level clip |
---|
| 865 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
[1029] | 866 | for ( Int i=LCUIdx - 1; i>=0; i-- ) |
---|
[313] | 867 | { |
---|
| 868 | if ( (getLCU(i)).m_QP > g_RCInvalidQPValue ) |
---|
| 869 | { |
---|
| 870 | clipNeighbourQP = getLCU(i).m_QP; |
---|
| 871 | break; |
---|
| 872 | } |
---|
| 873 | } |
---|
| 874 | |
---|
| 875 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
| 876 | { |
---|
| 877 | estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP ); |
---|
| 878 | } |
---|
| 879 | |
---|
| 880 | estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP ); |
---|
| 881 | |
---|
| 882 | return estQP; |
---|
| 883 | } |
---|
| 884 | |
---|
[1029] | 885 | Void TEncRCPic::updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter ) |
---|
[313] | 886 | { |
---|
| 887 | m_LCUs[LCUIdx].m_actualBits = bits; |
---|
| 888 | m_LCUs[LCUIdx].m_QP = QP; |
---|
| 889 | m_LCUs[LCUIdx].m_lambda = lambda; |
---|
| 890 | |
---|
| 891 | m_LCULeft--; |
---|
| 892 | m_bitsLeft -= bits; |
---|
| 893 | m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel; |
---|
| 894 | |
---|
| 895 | if ( !updateLCUParameter ) |
---|
| 896 | { |
---|
| 897 | return; |
---|
| 898 | } |
---|
| 899 | |
---|
| 900 | if ( !m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 901 | { |
---|
| 902 | return; |
---|
| 903 | } |
---|
| 904 | |
---|
| 905 | Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
| 906 | Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
| 907 | |
---|
| 908 | Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits; |
---|
| 909 | Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel; |
---|
| 910 | Double bpp = ( Double )LCUActualBits/( Double )LCUTotalPixels; |
---|
| 911 | Double calLambda = alpha * pow( bpp, beta ); |
---|
| 912 | Double inputLambda = m_LCUs[LCUIdx].m_lambda; |
---|
| 913 | |
---|
| 914 | if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 ) |
---|
| 915 | { |
---|
| 916 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
| 917 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
| 918 | |
---|
| 919 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 920 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
| 921 | |
---|
| 922 | TRCParameter rcPara; |
---|
| 923 | rcPara.m_alpha = alpha; |
---|
| 924 | rcPara.m_beta = beta; |
---|
| 925 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
| 926 | |
---|
| 927 | return; |
---|
| 928 | } |
---|
| 929 | |
---|
| 930 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
| 931 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
[1029] | 932 | Double lnbpp = log( bpp ); |
---|
[313] | 933 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
| 934 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
| 935 | |
---|
| 936 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 937 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
[1029] | 938 | |
---|
[313] | 939 | TRCParameter rcPara; |
---|
| 940 | rcPara.m_alpha = alpha; |
---|
| 941 | rcPara.m_beta = beta; |
---|
| 942 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
| 943 | |
---|
| 944 | } |
---|
| 945 | |
---|
| 946 | Double TEncRCPic::calAverageQP() |
---|
| 947 | { |
---|
| 948 | Int totalQPs = 0; |
---|
| 949 | Int numTotalLCUs = 0; |
---|
| 950 | |
---|
| 951 | Int i; |
---|
| 952 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
| 953 | { |
---|
| 954 | if ( m_LCUs[i].m_QP > 0 ) |
---|
| 955 | { |
---|
| 956 | totalQPs += m_LCUs[i].m_QP; |
---|
| 957 | numTotalLCUs++; |
---|
| 958 | } |
---|
| 959 | } |
---|
| 960 | |
---|
| 961 | Double avgQP = 0.0; |
---|
| 962 | if ( numTotalLCUs == 0 ) |
---|
| 963 | { |
---|
| 964 | avgQP = g_RCInvalidQPValue; |
---|
| 965 | } |
---|
| 966 | else |
---|
| 967 | { |
---|
| 968 | avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs); |
---|
| 969 | } |
---|
| 970 | return avgQP; |
---|
| 971 | } |
---|
| 972 | |
---|
| 973 | Double TEncRCPic::calAverageLambda() |
---|
| 974 | { |
---|
| 975 | Double totalLambdas = 0.0; |
---|
| 976 | Int numTotalLCUs = 0; |
---|
| 977 | |
---|
| 978 | Int i; |
---|
| 979 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
| 980 | { |
---|
| 981 | if ( m_LCUs[i].m_lambda > 0.01 ) |
---|
| 982 | { |
---|
| 983 | totalLambdas += log( m_LCUs[i].m_lambda ); |
---|
| 984 | numTotalLCUs++; |
---|
| 985 | } |
---|
| 986 | } |
---|
| 987 | |
---|
[1029] | 988 | Double avgLambda; |
---|
[313] | 989 | if( numTotalLCUs == 0 ) |
---|
| 990 | { |
---|
| 991 | avgLambda = -1.0; |
---|
| 992 | } |
---|
| 993 | else |
---|
| 994 | { |
---|
| 995 | avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs ); |
---|
| 996 | } |
---|
| 997 | return avgLambda; |
---|
| 998 | } |
---|
| 999 | |
---|
[1029] | 1000 | |
---|
[313] | 1001 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType) |
---|
| 1002 | { |
---|
| 1003 | m_picActualHeaderBits = actualHeaderBits; |
---|
| 1004 | m_picActualBits = actualTotalBits; |
---|
| 1005 | if ( averageQP > 0.0 ) |
---|
| 1006 | { |
---|
| 1007 | m_picQP = Int( averageQP + 0.5 ); |
---|
| 1008 | } |
---|
| 1009 | else |
---|
| 1010 | { |
---|
| 1011 | m_picQP = g_RCInvalidQPValue; |
---|
| 1012 | } |
---|
| 1013 | m_picLambda = averageLambda; |
---|
| 1014 | |
---|
| 1015 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 1016 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
[1029] | 1017 | |
---|
[313] | 1018 | if (eSliceType == I_SLICE) |
---|
| 1019 | { |
---|
| 1020 | updateAlphaBetaIntra(&alpha, &beta); |
---|
| 1021 | } |
---|
| 1022 | else |
---|
| 1023 | { |
---|
[1029] | 1024 | // update parameters |
---|
| 1025 | Double picActualBits = ( Double )m_picActualBits; |
---|
| 1026 | Double picActualBpp = picActualBits/(Double)m_numberOfPixel; |
---|
| 1027 | Double calLambda = alpha * pow( picActualBpp, beta ); |
---|
| 1028 | Double inputLambda = m_picLambda; |
---|
[313] | 1029 | |
---|
[1029] | 1030 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 ) |
---|
| 1031 | { |
---|
| 1032 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
| 1033 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
[313] | 1034 | |
---|
[1029] | 1035 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 1036 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
[313] | 1037 | |
---|
[1029] | 1038 | TRCParameter rcPara; |
---|
| 1039 | rcPara.m_alpha = alpha; |
---|
| 1040 | rcPara.m_beta = beta; |
---|
| 1041 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
[313] | 1042 | |
---|
[1029] | 1043 | return; |
---|
| 1044 | } |
---|
[313] | 1045 | |
---|
[1029] | 1046 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
| 1047 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
| 1048 | Double lnbpp = log( picActualBpp ); |
---|
| 1049 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
| 1050 | |
---|
| 1051 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
| 1052 | |
---|
| 1053 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 1054 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
[313] | 1055 | } |
---|
| 1056 | |
---|
| 1057 | TRCParameter rcPara; |
---|
| 1058 | rcPara.m_alpha = alpha; |
---|
| 1059 | rcPara.m_beta = beta; |
---|
| 1060 | |
---|
| 1061 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
| 1062 | |
---|
| 1063 | if ( m_frameLevel == 1 ) |
---|
| 1064 | { |
---|
| 1065 | Double currLambda = Clip3( 0.1, 10000.0, m_picLambda ); |
---|
| 1066 | Double updateLastLambda = g_RCWeightHistoryLambda * m_encRCSeq->getLastLambda() + g_RCWeightCurrentLambda * currLambda; |
---|
| 1067 | m_encRCSeq->setLastLambda( updateLastLambda ); |
---|
| 1068 | } |
---|
| 1069 | } |
---|
| 1070 | |
---|
| 1071 | Int TEncRCPic::getRefineBitsForIntra( Int orgBits ) |
---|
| 1072 | { |
---|
| 1073 | Double alpha=0.25, beta=0.5582; |
---|
| 1074 | Int iIntraBits; |
---|
| 1075 | |
---|
| 1076 | if (orgBits*40 < m_numberOfPixel) |
---|
| 1077 | { |
---|
| 1078 | alpha=0.25; |
---|
| 1079 | } |
---|
| 1080 | else |
---|
| 1081 | { |
---|
| 1082 | alpha=0.30; |
---|
| 1083 | } |
---|
| 1084 | |
---|
| 1085 | iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5); |
---|
[1029] | 1086 | |
---|
[313] | 1087 | return iIntraBits; |
---|
| 1088 | } |
---|
| 1089 | |
---|
[1029] | 1090 | Double TEncRCPic::calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel) |
---|
[313] | 1091 | { |
---|
| 1092 | return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) ); |
---|
| 1093 | } |
---|
| 1094 | |
---|
[1029] | 1095 | Void TEncRCPic::updateAlphaBetaIntra(Double *alpha, Double *beta) |
---|
[313] | 1096 | { |
---|
| 1097 | Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1)); |
---|
| 1098 | Double diffLambda = (*beta)*(log((Double)m_picActualBits)-log((Double)m_targetBits)); |
---|
| 1099 | |
---|
| 1100 | diffLambda = Clip3(-0.125, 0.125, 0.25*diffLambda); |
---|
| 1101 | *alpha = (*alpha) * exp(diffLambda); |
---|
| 1102 | *beta = (*beta) + diffLambda / lnbpp; |
---|
| 1103 | } |
---|
| 1104 | |
---|
| 1105 | |
---|
[1029] | 1106 | Void TEncRCPic::getLCUInitTargetBits() |
---|
[313] | 1107 | { |
---|
| 1108 | Int iAvgBits = 0; |
---|
| 1109 | |
---|
| 1110 | m_remainingCostIntra = m_totalCostIntra; |
---|
| 1111 | for (Int i=m_numberOfLCU-1; i>=0; i--) |
---|
| 1112 | { |
---|
| 1113 | iAvgBits += Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra); |
---|
| 1114 | getLCU(i).m_targetBitsLeft = iAvgBits; |
---|
| 1115 | } |
---|
| 1116 | } |
---|
| 1117 | |
---|
| 1118 | |
---|
[1029] | 1119 | Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP) |
---|
[313] | 1120 | { |
---|
| 1121 | Int LCUIdx = getLCUCoded(); |
---|
| 1122 | |
---|
| 1123 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 1124 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 1125 | |
---|
| 1126 | Double costPerPixel = getLCU(LCUIdx).m_costIntra/(Double)getLCU(LCUIdx).m_numberOfPixel; |
---|
| 1127 | costPerPixel = pow(costPerPixel, BETA1); |
---|
| 1128 | Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp); |
---|
| 1129 | |
---|
| 1130 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
[1029] | 1131 | for (Int i=LCUIdx-1; i>=0; i--) |
---|
[313] | 1132 | { |
---|
| 1133 | if ((getLCU(i)).m_QP > g_RCInvalidQPValue) |
---|
| 1134 | { |
---|
| 1135 | clipNeighbourQP = getLCU(i).m_QP; |
---|
| 1136 | break; |
---|
| 1137 | } |
---|
| 1138 | } |
---|
| 1139 | |
---|
| 1140 | Int minQP = clipPicQP - 2; |
---|
| 1141 | Int maxQP = clipPicQP + 2; |
---|
| 1142 | |
---|
| 1143 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
| 1144 | { |
---|
[1029] | 1145 | maxQP = min(clipNeighbourQP + 1, maxQP); |
---|
| 1146 | minQP = max(clipNeighbourQP - 1, minQP); |
---|
[313] | 1147 | } |
---|
| 1148 | |
---|
| 1149 | Double maxLambda=exp(((Double)(maxQP+0.49)-13.7122)/4.2005); |
---|
| 1150 | Double minLambda=exp(((Double)(minQP-0.49)-13.7122)/4.2005); |
---|
| 1151 | |
---|
| 1152 | estLambda = Clip3(minLambda, maxLambda, estLambda); |
---|
| 1153 | |
---|
| 1154 | *estQP = Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 ); |
---|
| 1155 | *estQP = Clip3(minQP, maxQP, *estQP); |
---|
| 1156 | |
---|
| 1157 | return estLambda; |
---|
| 1158 | } |
---|
| 1159 | |
---|
| 1160 | TEncRateCtrl::TEncRateCtrl() |
---|
| 1161 | { |
---|
| 1162 | m_encRCSeq = NULL; |
---|
| 1163 | m_encRCGOP = NULL; |
---|
| 1164 | m_encRCPic = NULL; |
---|
| 1165 | } |
---|
| 1166 | |
---|
| 1167 | TEncRateCtrl::~TEncRateCtrl() |
---|
| 1168 | { |
---|
| 1169 | destroy(); |
---|
| 1170 | } |
---|
| 1171 | |
---|
| 1172 | Void TEncRateCtrl::destroy() |
---|
| 1173 | { |
---|
| 1174 | if ( m_encRCSeq != NULL ) |
---|
| 1175 | { |
---|
| 1176 | delete m_encRCSeq; |
---|
| 1177 | m_encRCSeq = NULL; |
---|
| 1178 | } |
---|
| 1179 | if ( m_encRCGOP != NULL ) |
---|
| 1180 | { |
---|
| 1181 | delete m_encRCGOP; |
---|
| 1182 | m_encRCGOP = NULL; |
---|
| 1183 | } |
---|
| 1184 | while ( m_listRCPictures.size() > 0 ) |
---|
| 1185 | { |
---|
| 1186 | TEncRCPic* p = m_listRCPictures.front(); |
---|
| 1187 | m_listRCPictures.pop_front(); |
---|
| 1188 | delete p; |
---|
| 1189 | } |
---|
| 1190 | } |
---|
| 1191 | |
---|
| 1192 | 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] ) |
---|
| 1193 | { |
---|
| 1194 | destroy(); |
---|
| 1195 | |
---|
| 1196 | Bool isLowdelay = true; |
---|
| 1197 | for ( Int i=0; i<GOPSize-1; i++ ) |
---|
| 1198 | { |
---|
| 1199 | if ( GOPList[i].m_POC > GOPList[i+1].m_POC ) |
---|
| 1200 | { |
---|
| 1201 | isLowdelay = false; |
---|
| 1202 | break; |
---|
| 1203 | } |
---|
| 1204 | } |
---|
| 1205 | |
---|
| 1206 | Int numberOfLevel = 1; |
---|
| 1207 | Int adaptiveBit = 0; |
---|
| 1208 | if ( keepHierBits > 0 ) |
---|
| 1209 | { |
---|
| 1210 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
| 1211 | } |
---|
| 1212 | if ( !isLowdelay && GOPSize == 8 ) |
---|
| 1213 | { |
---|
| 1214 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
| 1215 | } |
---|
| 1216 | numberOfLevel++; // intra picture |
---|
| 1217 | numberOfLevel++; // non-reference picture |
---|
| 1218 | |
---|
| 1219 | |
---|
| 1220 | Int* bitsRatio; |
---|
| 1221 | bitsRatio = new Int[ GOPSize ]; |
---|
| 1222 | for ( Int i=0; i<GOPSize; i++ ) |
---|
| 1223 | { |
---|
| 1224 | bitsRatio[i] = 10; |
---|
| 1225 | if ( !GOPList[i].m_refPic ) |
---|
| 1226 | { |
---|
| 1227 | bitsRatio[i] = 2; |
---|
| 1228 | } |
---|
| 1229 | } |
---|
| 1230 | |
---|
| 1231 | if ( keepHierBits > 0 ) |
---|
| 1232 | { |
---|
| 1233 | Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); |
---|
| 1234 | if ( GOPSize == 4 && isLowdelay ) |
---|
| 1235 | { |
---|
| 1236 | if ( bpp > 0.2 ) |
---|
| 1237 | { |
---|
| 1238 | bitsRatio[0] = 2; |
---|
| 1239 | bitsRatio[1] = 3; |
---|
| 1240 | bitsRatio[2] = 2; |
---|
| 1241 | bitsRatio[3] = 6; |
---|
| 1242 | } |
---|
| 1243 | else if( bpp > 0.1 ) |
---|
| 1244 | { |
---|
| 1245 | bitsRatio[0] = 2; |
---|
| 1246 | bitsRatio[1] = 3; |
---|
| 1247 | bitsRatio[2] = 2; |
---|
| 1248 | bitsRatio[3] = 10; |
---|
| 1249 | } |
---|
| 1250 | else if ( bpp > 0.05 ) |
---|
| 1251 | { |
---|
| 1252 | bitsRatio[0] = 2; |
---|
| 1253 | bitsRatio[1] = 3; |
---|
| 1254 | bitsRatio[2] = 2; |
---|
| 1255 | bitsRatio[3] = 12; |
---|
| 1256 | } |
---|
| 1257 | else |
---|
| 1258 | { |
---|
| 1259 | bitsRatio[0] = 2; |
---|
| 1260 | bitsRatio[1] = 3; |
---|
| 1261 | bitsRatio[2] = 2; |
---|
| 1262 | bitsRatio[3] = 14; |
---|
| 1263 | } |
---|
[1029] | 1264 | |
---|
[313] | 1265 | if ( keepHierBits == 2 ) |
---|
| 1266 | { |
---|
| 1267 | adaptiveBit = 1; |
---|
| 1268 | } |
---|
| 1269 | } |
---|
| 1270 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
| 1271 | { |
---|
| 1272 | if ( bpp > 0.2 ) |
---|
| 1273 | { |
---|
| 1274 | bitsRatio[0] = 15; |
---|
| 1275 | bitsRatio[1] = 5; |
---|
| 1276 | bitsRatio[2] = 4; |
---|
| 1277 | bitsRatio[3] = 1; |
---|
| 1278 | bitsRatio[4] = 1; |
---|
| 1279 | bitsRatio[5] = 4; |
---|
| 1280 | bitsRatio[6] = 1; |
---|
| 1281 | bitsRatio[7] = 1; |
---|
| 1282 | } |
---|
| 1283 | else if ( bpp > 0.1 ) |
---|
| 1284 | { |
---|
| 1285 | bitsRatio[0] = 20; |
---|
| 1286 | bitsRatio[1] = 6; |
---|
| 1287 | bitsRatio[2] = 4; |
---|
| 1288 | bitsRatio[3] = 1; |
---|
| 1289 | bitsRatio[4] = 1; |
---|
| 1290 | bitsRatio[5] = 4; |
---|
| 1291 | bitsRatio[6] = 1; |
---|
| 1292 | bitsRatio[7] = 1; |
---|
| 1293 | } |
---|
| 1294 | else if ( bpp > 0.05 ) |
---|
| 1295 | { |
---|
| 1296 | bitsRatio[0] = 25; |
---|
| 1297 | bitsRatio[1] = 7; |
---|
| 1298 | bitsRatio[2] = 4; |
---|
| 1299 | bitsRatio[3] = 1; |
---|
| 1300 | bitsRatio[4] = 1; |
---|
| 1301 | bitsRatio[5] = 4; |
---|
| 1302 | bitsRatio[6] = 1; |
---|
| 1303 | bitsRatio[7] = 1; |
---|
| 1304 | } |
---|
| 1305 | else |
---|
| 1306 | { |
---|
| 1307 | bitsRatio[0] = 30; |
---|
| 1308 | bitsRatio[1] = 8; |
---|
| 1309 | bitsRatio[2] = 4; |
---|
| 1310 | bitsRatio[3] = 1; |
---|
| 1311 | bitsRatio[4] = 1; |
---|
| 1312 | bitsRatio[5] = 4; |
---|
| 1313 | bitsRatio[6] = 1; |
---|
| 1314 | bitsRatio[7] = 1; |
---|
| 1315 | } |
---|
[1029] | 1316 | |
---|
[313] | 1317 | if ( keepHierBits == 2 ) |
---|
| 1318 | { |
---|
| 1319 | adaptiveBit = 2; |
---|
| 1320 | } |
---|
| 1321 | } |
---|
| 1322 | else |
---|
| 1323 | { |
---|
| 1324 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" ); |
---|
| 1325 | } |
---|
| 1326 | } |
---|
| 1327 | |
---|
[1029] | 1328 | Int* GOPID2Level = new Int[ GOPSize ]; |
---|
| 1329 | for ( Int i=0; i<GOPSize; i++ ) |
---|
[313] | 1330 | { |
---|
| 1331 | GOPID2Level[i] = 1; |
---|
| 1332 | if ( !GOPList[i].m_refPic ) |
---|
| 1333 | { |
---|
| 1334 | GOPID2Level[i] = 2; |
---|
| 1335 | } |
---|
| 1336 | } |
---|
[1029] | 1337 | |
---|
[313] | 1338 | if ( keepHierBits > 0 ) |
---|
| 1339 | { |
---|
| 1340 | if ( GOPSize == 4 && isLowdelay ) |
---|
| 1341 | { |
---|
| 1342 | GOPID2Level[0] = 3; |
---|
| 1343 | GOPID2Level[1] = 2; |
---|
| 1344 | GOPID2Level[2] = 3; |
---|
| 1345 | GOPID2Level[3] = 1; |
---|
| 1346 | } |
---|
| 1347 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
| 1348 | { |
---|
| 1349 | GOPID2Level[0] = 1; |
---|
| 1350 | GOPID2Level[1] = 2; |
---|
| 1351 | GOPID2Level[2] = 3; |
---|
| 1352 | GOPID2Level[3] = 4; |
---|
| 1353 | GOPID2Level[4] = 4; |
---|
| 1354 | GOPID2Level[5] = 3; |
---|
| 1355 | GOPID2Level[6] = 4; |
---|
| 1356 | GOPID2Level[7] = 4; |
---|
| 1357 | } |
---|
| 1358 | } |
---|
| 1359 | |
---|
| 1360 | if ( !isLowdelay && GOPSize == 8 ) |
---|
| 1361 | { |
---|
| 1362 | GOPID2Level[0] = 1; |
---|
| 1363 | GOPID2Level[1] = 2; |
---|
| 1364 | GOPID2Level[2] = 3; |
---|
| 1365 | GOPID2Level[3] = 4; |
---|
| 1366 | GOPID2Level[4] = 4; |
---|
| 1367 | GOPID2Level[5] = 3; |
---|
| 1368 | GOPID2Level[6] = 4; |
---|
| 1369 | GOPID2Level[7] = 4; |
---|
| 1370 | } |
---|
| 1371 | |
---|
| 1372 | m_encRCSeq = new TEncRCSeq; |
---|
| 1373 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit ); |
---|
| 1374 | m_encRCSeq->initBitsRatio( bitsRatio ); |
---|
| 1375 | m_encRCSeq->initGOPID2Level( GOPID2Level ); |
---|
| 1376 | m_encRCSeq->initPicPara(); |
---|
| 1377 | if ( useLCUSeparateModel ) |
---|
| 1378 | { |
---|
| 1379 | m_encRCSeq->initLCUPara(); |
---|
| 1380 | } |
---|
| 1381 | |
---|
| 1382 | delete[] bitsRatio; |
---|
| 1383 | delete[] GOPID2Level; |
---|
| 1384 | } |
---|
| 1385 | |
---|
| 1386 | Void TEncRateCtrl::initRCPic( Int frameLevel ) |
---|
| 1387 | { |
---|
| 1388 | m_encRCPic = new TEncRCPic; |
---|
| 1389 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures ); |
---|
| 1390 | } |
---|
| 1391 | |
---|
| 1392 | Void TEncRateCtrl::initRCGOP( Int numberOfPictures ) |
---|
| 1393 | { |
---|
| 1394 | m_encRCGOP = new TEncRCGOP; |
---|
| 1395 | m_encRCGOP->create( m_encRCSeq, numberOfPictures ); |
---|
| 1396 | } |
---|
| 1397 | |
---|
| 1398 | Void TEncRateCtrl::destroyRCGOP() |
---|
| 1399 | { |
---|
| 1400 | delete m_encRCGOP; |
---|
| 1401 | m_encRCGOP = NULL; |
---|
| 1402 | } |
---|
| 1403 | |
---|