[191] | 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 | * |
---|
| 6 | * Copyright (c) 2010-2013, ITU/ISO/IEC |
---|
| 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 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 45 | |
---|
| 46 | //sequence level |
---|
| 47 | TEncRCSeq::TEncRCSeq() |
---|
| 48 | { |
---|
| 49 | m_totalFrames = 0; |
---|
| 50 | m_targetRate = 0; |
---|
| 51 | m_frameRate = 0; |
---|
| 52 | m_targetBits = 0; |
---|
| 53 | m_GOPSize = 0; |
---|
| 54 | m_picWidth = 0; |
---|
| 55 | m_picHeight = 0; |
---|
| 56 | m_LCUWidth = 0; |
---|
| 57 | m_LCUHeight = 0; |
---|
| 58 | m_numberOfLevel = 0; |
---|
| 59 | m_numberOfLCU = 0; |
---|
| 60 | m_averageBits = 0; |
---|
| 61 | m_bitsRatio = NULL; |
---|
| 62 | m_GOPID2Level = NULL; |
---|
| 63 | m_picPara = NULL; |
---|
| 64 | m_LCUPara = NULL; |
---|
| 65 | m_numberOfPixel = 0; |
---|
| 66 | m_framesLeft = 0; |
---|
| 67 | m_bitsLeft = 0; |
---|
| 68 | m_useLCUSeparateModel = false; |
---|
[302] | 69 | #if M0036_RC_IMPROVEMENT |
---|
| 70 | m_adaptiveBit = 0; |
---|
| 71 | m_lastLambda = 0.0; |
---|
| 72 | #endif |
---|
[191] | 73 | } |
---|
| 74 | |
---|
| 75 | TEncRCSeq::~TEncRCSeq() |
---|
| 76 | { |
---|
| 77 | destroy(); |
---|
| 78 | } |
---|
| 79 | |
---|
[302] | 80 | #if M0036_RC_IMPROVEMENT |
---|
| 81 | Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit ) |
---|
| 82 | #else |
---|
[191] | 83 | Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel ) |
---|
[302] | 84 | #endif |
---|
[191] | 85 | { |
---|
| 86 | destroy(); |
---|
| 87 | m_totalFrames = totalFrames; |
---|
| 88 | m_targetRate = targetBitrate; |
---|
| 89 | m_frameRate = frameRate; |
---|
| 90 | m_GOPSize = GOPSize; |
---|
| 91 | m_picWidth = picWidth; |
---|
| 92 | m_picHeight = picHeight; |
---|
| 93 | m_LCUWidth = LCUWidth; |
---|
| 94 | m_LCUHeight = LCUHeight; |
---|
| 95 | m_numberOfLevel = numberOfLevel; |
---|
| 96 | m_useLCUSeparateModel = useLCUSeparateModel; |
---|
| 97 | |
---|
| 98 | m_numberOfPixel = m_picWidth * m_picHeight; |
---|
| 99 | m_targetBits = (Int64)m_totalFrames * (Int64)m_targetRate / (Int64)m_frameRate; |
---|
| 100 | m_seqTargetBpp = (Double)m_targetRate / (Double)m_frameRate / (Double)m_numberOfPixel; |
---|
| 101 | if ( m_seqTargetBpp < 0.03 ) |
---|
| 102 | { |
---|
| 103 | m_alphaUpdate = 0.01; |
---|
| 104 | m_betaUpdate = 0.005; |
---|
| 105 | } |
---|
| 106 | else if ( m_seqTargetBpp < 0.08 ) |
---|
| 107 | { |
---|
| 108 | m_alphaUpdate = 0.05; |
---|
| 109 | m_betaUpdate = 0.025; |
---|
| 110 | } |
---|
[302] | 111 | #if M0036_RC_IMPROVEMENT |
---|
| 112 | else if ( m_seqTargetBpp < 0.2 ) |
---|
| 113 | { |
---|
| 114 | m_alphaUpdate = 0.1; |
---|
| 115 | m_betaUpdate = 0.05; |
---|
| 116 | } |
---|
| 117 | else if ( m_seqTargetBpp < 0.5 ) |
---|
| 118 | { |
---|
| 119 | m_alphaUpdate = 0.2; |
---|
| 120 | m_betaUpdate = 0.1; |
---|
| 121 | } |
---|
[191] | 122 | else |
---|
| 123 | { |
---|
[302] | 124 | m_alphaUpdate = 0.4; |
---|
| 125 | m_betaUpdate = 0.2; |
---|
| 126 | } |
---|
| 127 | #else |
---|
| 128 | else |
---|
| 129 | { |
---|
[191] | 130 | m_alphaUpdate = 0.1; |
---|
| 131 | m_betaUpdate = 0.05; |
---|
| 132 | } |
---|
[302] | 133 | #endif |
---|
[191] | 134 | m_averageBits = (Int)(m_targetBits / totalFrames); |
---|
| 135 | Int picWidthInBU = ( m_picWidth % m_LCUWidth ) == 0 ? m_picWidth / m_LCUWidth : m_picWidth / m_LCUWidth + 1; |
---|
| 136 | Int picHeightInBU = ( m_picHeight % m_LCUHeight ) == 0 ? m_picHeight / m_LCUHeight : m_picHeight / m_LCUHeight + 1; |
---|
| 137 | m_numberOfLCU = picWidthInBU * picHeightInBU; |
---|
| 138 | |
---|
| 139 | m_bitsRatio = new Int[m_GOPSize]; |
---|
| 140 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
| 141 | { |
---|
| 142 | m_bitsRatio[i] = 1; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | m_GOPID2Level = new Int[m_GOPSize]; |
---|
| 146 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
| 147 | { |
---|
| 148 | m_GOPID2Level[i] = 1; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | m_picPara = new TRCParameter[m_numberOfLevel]; |
---|
| 152 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 153 | { |
---|
| 154 | m_picPara[i].m_alpha = 0.0; |
---|
| 155 | m_picPara[i].m_beta = 0.0; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | if ( m_useLCUSeparateModel ) |
---|
| 159 | { |
---|
| 160 | m_LCUPara = new TRCParameter*[m_numberOfLevel]; |
---|
| 161 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 162 | { |
---|
| 163 | m_LCUPara[i] = new TRCParameter[m_numberOfLCU]; |
---|
| 164 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
| 165 | { |
---|
| 166 | m_LCUPara[i][j].m_alpha = 0.0; |
---|
| 167 | m_LCUPara[i][j].m_beta = 0.0; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | m_framesLeft = m_totalFrames; |
---|
| 173 | m_bitsLeft = m_targetBits; |
---|
[302] | 174 | #if M0036_RC_IMPROVEMENT |
---|
| 175 | m_adaptiveBit = adaptiveBit; |
---|
| 176 | m_lastLambda = 0.0; |
---|
| 177 | #endif |
---|
[191] | 178 | } |
---|
| 179 | |
---|
| 180 | Void TEncRCSeq::destroy() |
---|
| 181 | { |
---|
| 182 | if (m_bitsRatio != NULL) |
---|
| 183 | { |
---|
| 184 | delete[] m_bitsRatio; |
---|
| 185 | m_bitsRatio = NULL; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | if ( m_GOPID2Level != NULL ) |
---|
| 189 | { |
---|
| 190 | delete[] m_GOPID2Level; |
---|
| 191 | m_GOPID2Level = NULL; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | if ( m_picPara != NULL ) |
---|
| 195 | { |
---|
| 196 | delete[] m_picPara; |
---|
| 197 | m_picPara = NULL; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if ( m_LCUPara != NULL ) |
---|
| 201 | { |
---|
| 202 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 203 | { |
---|
| 204 | delete[] m_LCUPara[i]; |
---|
| 205 | } |
---|
| 206 | delete[] m_LCUPara; |
---|
| 207 | m_LCUPara = NULL; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | Void TEncRCSeq::initBitsRatio( Int bitsRatio[]) |
---|
| 212 | { |
---|
| 213 | for (Int i=0; i<m_GOPSize; i++) |
---|
| 214 | { |
---|
| 215 | m_bitsRatio[i] = bitsRatio[i]; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | Void TEncRCSeq::initGOPID2Level( Int GOPID2Level[] ) |
---|
| 220 | { |
---|
| 221 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
| 222 | { |
---|
| 223 | m_GOPID2Level[i] = GOPID2Level[i]; |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | Void TEncRCSeq::initPicPara( TRCParameter* picPara ) |
---|
| 228 | { |
---|
| 229 | assert( m_picPara != NULL ); |
---|
| 230 | |
---|
| 231 | if ( picPara == NULL ) |
---|
| 232 | { |
---|
| 233 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 234 | { |
---|
[302] | 235 | #if RATE_CONTROL_INTRA |
---|
| 236 | if (i>0) |
---|
| 237 | { |
---|
[191] | 238 | m_picPara[i].m_alpha = 3.2003; |
---|
| 239 | m_picPara[i].m_beta = -1.367; |
---|
| 240 | } |
---|
[302] | 241 | else |
---|
| 242 | { |
---|
| 243 | m_picPara[i].m_alpha = ALPHA; |
---|
| 244 | m_picPara[i].m_beta = BETA2; |
---|
| 245 | } |
---|
| 246 | #else |
---|
| 247 | m_picPara[i].m_alpha = 3.2003; |
---|
| 248 | m_picPara[i].m_beta = -1.367; |
---|
| 249 | #endif |
---|
| 250 | } |
---|
[191] | 251 | } |
---|
| 252 | else |
---|
| 253 | { |
---|
| 254 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 255 | { |
---|
| 256 | m_picPara[i] = picPara[i]; |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | Void TEncRCSeq::initLCUPara( TRCParameter** LCUPara ) |
---|
| 262 | { |
---|
| 263 | if ( m_LCUPara == NULL ) |
---|
| 264 | { |
---|
| 265 | return; |
---|
| 266 | } |
---|
| 267 | if ( LCUPara == NULL ) |
---|
| 268 | { |
---|
| 269 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 270 | { |
---|
| 271 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
| 272 | { |
---|
[302] | 273 | #if RATE_CONTROL_INTRA |
---|
| 274 | m_LCUPara[i][j].m_alpha = m_picPara[i].m_alpha; |
---|
| 275 | m_LCUPara[i][j].m_beta = m_picPara[i].m_beta; |
---|
| 276 | #else |
---|
[191] | 277 | m_LCUPara[i][j].m_alpha = 3.2003; |
---|
| 278 | m_LCUPara[i][j].m_beta = -1.367; |
---|
[302] | 279 | #endif |
---|
[191] | 280 | } |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
| 285 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
| 286 | { |
---|
| 287 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
| 288 | { |
---|
| 289 | m_LCUPara[i][j] = LCUPara[i][j]; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | Void TEncRCSeq::updateAfterPic ( Int bits ) |
---|
| 296 | { |
---|
| 297 | m_bitsLeft -= bits; |
---|
| 298 | m_framesLeft--; |
---|
| 299 | } |
---|
| 300 | |
---|
[302] | 301 | #if !RATE_CONTROL_INTRA |
---|
[191] | 302 | Int TEncRCSeq::getRefineBitsForIntra( Int orgBits ) |
---|
| 303 | { |
---|
| 304 | Double bpp = ( (Double)orgBits ) / m_picHeight / m_picHeight; |
---|
| 305 | if ( bpp > 0.2 ) |
---|
| 306 | { |
---|
| 307 | return orgBits * 5; |
---|
| 308 | } |
---|
| 309 | if ( bpp > 0.1 ) |
---|
| 310 | { |
---|
| 311 | return orgBits * 7; |
---|
| 312 | } |
---|
| 313 | return orgBits * 10; |
---|
| 314 | } |
---|
[302] | 315 | #endif |
---|
[191] | 316 | |
---|
[302] | 317 | #if M0036_RC_IMPROVEMENT |
---|
| 318 | Void TEncRCSeq::setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB ) |
---|
| 319 | { |
---|
| 320 | Int* bitsRatio = new Int[m_GOPSize]; |
---|
| 321 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
| 322 | { |
---|
| 323 | bitsRatio[i] = (Int)( equaCoeffA[i] * pow( basicLambda, equaCoeffB[i] ) * m_numberOfPixel ); |
---|
| 324 | } |
---|
| 325 | initBitsRatio( bitsRatio ); |
---|
| 326 | delete[] bitsRatio; |
---|
| 327 | } |
---|
| 328 | #endif |
---|
| 329 | |
---|
[191] | 330 | //GOP level |
---|
| 331 | TEncRCGOP::TEncRCGOP() |
---|
| 332 | { |
---|
| 333 | m_encRCSeq = NULL; |
---|
| 334 | m_picTargetBitInGOP = NULL; |
---|
| 335 | m_numPic = 0; |
---|
| 336 | m_targetBits = 0; |
---|
| 337 | m_picLeft = 0; |
---|
| 338 | m_bitsLeft = 0; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | TEncRCGOP::~TEncRCGOP() |
---|
| 342 | { |
---|
| 343 | destroy(); |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | Void TEncRCGOP::create( TEncRCSeq* encRCSeq, Int numPic ) |
---|
| 347 | { |
---|
| 348 | destroy(); |
---|
| 349 | Int targetBits = xEstGOPTargetBits( encRCSeq, numPic ); |
---|
| 350 | |
---|
[302] | 351 | #if M0036_RC_IMPROVEMENT |
---|
| 352 | if ( encRCSeq->getAdaptiveBits() > 0 && encRCSeq->getLastLambda() > 0.1 ) |
---|
| 353 | { |
---|
| 354 | Double targetBpp = (Double)targetBits / encRCSeq->getNumPixel(); |
---|
| 355 | Double basicLambda = 0.0; |
---|
| 356 | Double* lambdaRatio = new Double[encRCSeq->getGOPSize()]; |
---|
| 357 | Double* equaCoeffA = new Double[encRCSeq->getGOPSize()]; |
---|
| 358 | Double* equaCoeffB = new Double[encRCSeq->getGOPSize()]; |
---|
| 359 | |
---|
| 360 | if ( encRCSeq->getAdaptiveBits() == 1 ) // for GOP size =4, low delay case |
---|
| 361 | { |
---|
| 362 | if ( encRCSeq->getLastLambda() < 120.0 ) |
---|
| 363 | { |
---|
| 364 | lambdaRatio[1] = 0.725 * log( encRCSeq->getLastLambda() ) + 0.5793; |
---|
| 365 | lambdaRatio[0] = 1.3 * lambdaRatio[1]; |
---|
| 366 | lambdaRatio[2] = 1.3 * lambdaRatio[1]; |
---|
| 367 | lambdaRatio[3] = 1.0; |
---|
| 368 | } |
---|
| 369 | else |
---|
| 370 | { |
---|
| 371 | lambdaRatio[0] = 5.0; |
---|
| 372 | lambdaRatio[1] = 4.0; |
---|
| 373 | lambdaRatio[2] = 5.0; |
---|
| 374 | lambdaRatio[3] = 1.0; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | else if ( encRCSeq->getAdaptiveBits() == 2 ) // for GOP size = 8, random access case |
---|
| 378 | { |
---|
| 379 | if ( encRCSeq->getLastLambda() < 90.0 ) |
---|
| 380 | { |
---|
| 381 | lambdaRatio[0] = 1.0; |
---|
| 382 | lambdaRatio[1] = 0.725 * log( encRCSeq->getLastLambda() ) + 0.7963; |
---|
| 383 | lambdaRatio[2] = 1.3 * lambdaRatio[1]; |
---|
| 384 | lambdaRatio[3] = 3.25 * lambdaRatio[1]; |
---|
| 385 | lambdaRatio[4] = 3.25 * lambdaRatio[1]; |
---|
| 386 | lambdaRatio[5] = 1.3 * lambdaRatio[1]; |
---|
| 387 | lambdaRatio[6] = 3.25 * lambdaRatio[1]; |
---|
| 388 | lambdaRatio[7] = 3.25 * lambdaRatio[1]; |
---|
| 389 | } |
---|
| 390 | else |
---|
| 391 | { |
---|
| 392 | lambdaRatio[0] = 1.0; |
---|
| 393 | lambdaRatio[1] = 4.0; |
---|
| 394 | lambdaRatio[2] = 5.0; |
---|
| 395 | lambdaRatio[3] = 12.3; |
---|
| 396 | lambdaRatio[4] = 12.3; |
---|
| 397 | lambdaRatio[5] = 5.0; |
---|
| 398 | lambdaRatio[6] = 12.3; |
---|
| 399 | lambdaRatio[7] = 12.3; |
---|
| 400 | } |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | xCalEquaCoeff( encRCSeq, lambdaRatio, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() ); |
---|
| 404 | basicLambda = xSolveEqua( targetBpp, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() ); |
---|
| 405 | encRCSeq->setAllBitRatio( basicLambda, equaCoeffA, equaCoeffB ); |
---|
| 406 | |
---|
| 407 | delete []lambdaRatio; |
---|
| 408 | delete []equaCoeffA; |
---|
| 409 | delete []equaCoeffB; |
---|
| 410 | } |
---|
| 411 | #endif |
---|
| 412 | |
---|
[191] | 413 | m_picTargetBitInGOP = new Int[numPic]; |
---|
| 414 | Int i; |
---|
| 415 | Int totalPicRatio = 0; |
---|
| 416 | Int currPicRatio = 0; |
---|
| 417 | for ( i=0; i<numPic; i++ ) |
---|
| 418 | { |
---|
| 419 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
| 420 | } |
---|
| 421 | for ( i=0; i<numPic; i++ ) |
---|
| 422 | { |
---|
| 423 | currPicRatio = encRCSeq->getBitRatio( i ); |
---|
[302] | 424 | #if M0036_RC_IMPROVEMENT |
---|
| 425 | m_picTargetBitInGOP[i] = (Int)( ((Double)targetBits) * currPicRatio / totalPicRatio ); |
---|
| 426 | #else |
---|
[191] | 427 | m_picTargetBitInGOP[i] = targetBits * currPicRatio / totalPicRatio; |
---|
[302] | 428 | #endif |
---|
[191] | 429 | } |
---|
| 430 | |
---|
| 431 | m_encRCSeq = encRCSeq; |
---|
| 432 | m_numPic = numPic; |
---|
| 433 | m_targetBits = targetBits; |
---|
| 434 | m_picLeft = m_numPic; |
---|
| 435 | m_bitsLeft = m_targetBits; |
---|
| 436 | } |
---|
| 437 | |
---|
[302] | 438 | #if M0036_RC_IMPROVEMENT |
---|
| 439 | Void TEncRCGOP::xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) |
---|
| 440 | { |
---|
| 441 | for ( Int i=0; i<GOPSize; i++ ) |
---|
| 442 | { |
---|
| 443 | Int frameLevel = encRCSeq->getGOPID2Level(i); |
---|
| 444 | Double alpha = encRCSeq->getPicPara(frameLevel).m_alpha; |
---|
| 445 | Double beta = encRCSeq->getPicPara(frameLevel).m_beta; |
---|
| 446 | equaCoeffA[i] = pow( 1.0/alpha, 1.0/beta ) * pow( lambdaRatio[i], 1.0/beta ); |
---|
| 447 | equaCoeffB[i] = 1.0/beta; |
---|
| 448 | } |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | Double TEncRCGOP::xSolveEqua( Double targetBpp, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) |
---|
| 452 | { |
---|
| 453 | Double solution = 100.0; |
---|
| 454 | Double minNumber = 0.1; |
---|
| 455 | Double maxNumber = 10000.0; |
---|
| 456 | for ( Int i=0; i<g_RCIterationNum; i++ ) |
---|
| 457 | { |
---|
| 458 | Double fx = 0.0; |
---|
| 459 | for ( Int j=0; j<GOPSize; j++ ) |
---|
| 460 | { |
---|
| 461 | fx += equaCoeffA[j] * pow( solution, equaCoeffB[j] ); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | if ( fabs( fx - targetBpp ) < 0.000001 ) |
---|
| 465 | { |
---|
| 466 | break; |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | if ( fx > targetBpp ) |
---|
| 470 | { |
---|
| 471 | minNumber = solution; |
---|
| 472 | solution = ( solution + maxNumber ) / 2.0; |
---|
| 473 | } |
---|
| 474 | else |
---|
| 475 | { |
---|
| 476 | maxNumber = solution; |
---|
| 477 | solution = ( solution + minNumber ) / 2.0; |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | solution = Clip3( 0.1, 10000.0, solution ); |
---|
| 482 | return solution; |
---|
| 483 | } |
---|
| 484 | #endif |
---|
| 485 | |
---|
[191] | 486 | Void TEncRCGOP::destroy() |
---|
| 487 | { |
---|
| 488 | m_encRCSeq = NULL; |
---|
| 489 | if ( m_picTargetBitInGOP != NULL ) |
---|
| 490 | { |
---|
| 491 | delete[] m_picTargetBitInGOP; |
---|
| 492 | m_picTargetBitInGOP = NULL; |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | Void TEncRCGOP::updateAfterPicture( Int bitsCost ) |
---|
| 497 | { |
---|
| 498 | m_bitsLeft -= bitsCost; |
---|
| 499 | m_picLeft--; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | Int TEncRCGOP::xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize ) |
---|
| 503 | { |
---|
| 504 | Int realInfluencePicture = min( g_RCSmoothWindowSize, encRCSeq->getFramesLeft() ); |
---|
| 505 | Int averageTargetBitsPerPic = (Int)( encRCSeq->getTargetBits() / encRCSeq->getTotalFrames() ); |
---|
| 506 | Int currentTargetBitsPerPic = (Int)( ( encRCSeq->getBitsLeft() - averageTargetBitsPerPic * (encRCSeq->getFramesLeft() - realInfluencePicture) ) / realInfluencePicture ); |
---|
| 507 | Int targetBits = currentTargetBitsPerPic * GOPSize; |
---|
| 508 | |
---|
| 509 | if ( targetBits < 200 ) |
---|
| 510 | { |
---|
| 511 | targetBits = 200; // at least allocate 200 bits for one GOP |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | return targetBits; |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | //picture level |
---|
| 518 | TEncRCPic::TEncRCPic() |
---|
| 519 | { |
---|
| 520 | m_encRCSeq = NULL; |
---|
| 521 | m_encRCGOP = NULL; |
---|
| 522 | |
---|
| 523 | m_frameLevel = 0; |
---|
| 524 | m_numberOfPixel = 0; |
---|
| 525 | m_numberOfLCU = 0; |
---|
| 526 | m_targetBits = 0; |
---|
| 527 | m_estHeaderBits = 0; |
---|
| 528 | m_estPicQP = 0; |
---|
| 529 | m_estPicLambda = 0.0; |
---|
| 530 | |
---|
| 531 | m_LCULeft = 0; |
---|
| 532 | m_bitsLeft = 0; |
---|
| 533 | m_pixelsLeft = 0; |
---|
| 534 | |
---|
| 535 | m_LCUs = NULL; |
---|
[302] | 536 | #if !M0036_RC_IMPROVEMENT |
---|
[191] | 537 | m_lastPicture = NULL; |
---|
[302] | 538 | #endif |
---|
[191] | 539 | m_picActualHeaderBits = 0; |
---|
[302] | 540 | #if !M0036_RC_IMPROVEMENT |
---|
[191] | 541 | m_totalMAD = 0.0; |
---|
[302] | 542 | #endif |
---|
[191] | 543 | m_picActualBits = 0; |
---|
| 544 | m_picQP = 0; |
---|
| 545 | m_picLambda = 0.0; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | TEncRCPic::~TEncRCPic() |
---|
| 549 | { |
---|
| 550 | destroy(); |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | Int TEncRCPic::xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ) |
---|
| 554 | { |
---|
| 555 | Int targetBits = 0; |
---|
| 556 | Int GOPbitsLeft = encRCGOP->getBitsLeft(); |
---|
| 557 | |
---|
| 558 | Int i; |
---|
| 559 | Int currPicPosition = encRCGOP->getNumPic()-encRCGOP->getPicLeft(); |
---|
| 560 | Int currPicRatio = encRCSeq->getBitRatio( currPicPosition ); |
---|
| 561 | Int totalPicRatio = 0; |
---|
| 562 | for ( i=currPicPosition; i<encRCGOP->getNumPic(); i++ ) |
---|
| 563 | { |
---|
| 564 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
| 565 | } |
---|
| 566 | |
---|
[302] | 567 | #if M0036_RC_IMPROVEMENT |
---|
| 568 | targetBits = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio ); |
---|
| 569 | #else |
---|
[191] | 570 | targetBits = Int( GOPbitsLeft * currPicRatio / totalPicRatio ); |
---|
[302] | 571 | #endif |
---|
[191] | 572 | |
---|
| 573 | if ( targetBits < 100 ) |
---|
| 574 | { |
---|
| 575 | targetBits = 100; // at least allocate 100 bits for one picture |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | if ( m_encRCSeq->getFramesLeft() > 16 ) |
---|
| 579 | { |
---|
| 580 | targetBits = Int( g_RCWeightPicRargetBitInBuffer * targetBits + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP( currPicPosition ) ); |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | return targetBits; |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | Int TEncRCPic::xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ) |
---|
| 587 | { |
---|
| 588 | Int numPreviousPics = 0; |
---|
| 589 | Int totalPreviousBits = 0; |
---|
| 590 | |
---|
| 591 | list<TEncRCPic*>::iterator it; |
---|
| 592 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
| 593 | { |
---|
| 594 | if ( (*it)->getFrameLevel() == frameLevel ) |
---|
| 595 | { |
---|
| 596 | totalPreviousBits += (*it)->getPicActualHeaderBits(); |
---|
| 597 | numPreviousPics++; |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | Int estHeaderBits = 0; |
---|
| 602 | if ( numPreviousPics > 0 ) |
---|
| 603 | { |
---|
| 604 | estHeaderBits = totalPreviousBits / numPreviousPics; |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | return estHeaderBits; |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ) |
---|
| 611 | { |
---|
| 612 | if ( listPreviousPictures.size() > g_RCMaxPicListSize ) |
---|
| 613 | { |
---|
| 614 | TEncRCPic* p = listPreviousPictures.front(); |
---|
| 615 | listPreviousPictures.pop_front(); |
---|
| 616 | p->destroy(); |
---|
| 617 | delete p; |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | listPreviousPictures.push_back( this ); |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ) |
---|
| 624 | { |
---|
| 625 | destroy(); |
---|
| 626 | m_encRCSeq = encRCSeq; |
---|
| 627 | m_encRCGOP = encRCGOP; |
---|
| 628 | |
---|
| 629 | Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP ); |
---|
| 630 | Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel ); |
---|
| 631 | |
---|
| 632 | if ( targetBits < estHeaderBits + 100 ) |
---|
| 633 | { |
---|
| 634 | targetBits = estHeaderBits + 100; // at least allocate 100 bits for picture data |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | m_frameLevel = frameLevel; |
---|
| 638 | m_numberOfPixel = encRCSeq->getNumPixel(); |
---|
| 639 | m_numberOfLCU = encRCSeq->getNumberOfLCU(); |
---|
| 640 | m_estPicLambda = 100.0; |
---|
| 641 | m_targetBits = targetBits; |
---|
| 642 | m_estHeaderBits = estHeaderBits; |
---|
| 643 | m_bitsLeft = m_targetBits; |
---|
| 644 | Int picWidth = encRCSeq->getPicWidth(); |
---|
| 645 | Int picHeight = encRCSeq->getPicHeight(); |
---|
| 646 | Int LCUWidth = encRCSeq->getLCUWidth(); |
---|
| 647 | Int LCUHeight = encRCSeq->getLCUHeight(); |
---|
| 648 | Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1; |
---|
| 649 | Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1; |
---|
| 650 | |
---|
| 651 | m_LCULeft = m_numberOfLCU; |
---|
| 652 | m_bitsLeft -= m_estHeaderBits; |
---|
| 653 | m_pixelsLeft = m_numberOfPixel; |
---|
| 654 | |
---|
| 655 | m_LCUs = new TRCLCU[m_numberOfLCU]; |
---|
| 656 | Int i, j; |
---|
| 657 | Int LCUIdx; |
---|
| 658 | for ( i=0; i<picWidthInLCU; i++ ) |
---|
| 659 | { |
---|
| 660 | for ( j=0; j<picHeightInLCU; j++ ) |
---|
| 661 | { |
---|
| 662 | LCUIdx = j*picWidthInLCU + i; |
---|
| 663 | m_LCUs[LCUIdx].m_actualBits = 0; |
---|
| 664 | m_LCUs[LCUIdx].m_QP = 0; |
---|
| 665 | m_LCUs[LCUIdx].m_lambda = 0.0; |
---|
| 666 | m_LCUs[LCUIdx].m_targetBits = 0; |
---|
[302] | 667 | #if M0036_RC_IMPROVEMENT |
---|
| 668 | m_LCUs[LCUIdx].m_bitWeight = 1.0; |
---|
| 669 | #else |
---|
[191] | 670 | m_LCUs[LCUIdx].m_MAD = 0.0; |
---|
[302] | 671 | #endif |
---|
[191] | 672 | Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); |
---|
| 673 | Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); |
---|
| 674 | m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight; |
---|
| 675 | } |
---|
| 676 | } |
---|
| 677 | m_picActualHeaderBits = 0; |
---|
[302] | 678 | #if !M0036_RC_IMPROVEMENT |
---|
[191] | 679 | m_totalMAD = 0.0; |
---|
[302] | 680 | #endif |
---|
[191] | 681 | m_picActualBits = 0; |
---|
| 682 | m_picQP = 0; |
---|
| 683 | m_picLambda = 0.0; |
---|
| 684 | |
---|
[302] | 685 | #if !M0036_RC_IMPROVEMENT |
---|
[191] | 686 | m_lastPicture = NULL; |
---|
| 687 | list<TEncRCPic*>::reverse_iterator it; |
---|
| 688 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
---|
| 689 | { |
---|
| 690 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
| 691 | { |
---|
| 692 | m_lastPicture = (*it); |
---|
| 693 | break; |
---|
| 694 | } |
---|
| 695 | } |
---|
[302] | 696 | #endif |
---|
[191] | 697 | } |
---|
| 698 | |
---|
| 699 | Void TEncRCPic::destroy() |
---|
| 700 | { |
---|
| 701 | if( m_LCUs != NULL ) |
---|
| 702 | { |
---|
| 703 | delete[] m_LCUs; |
---|
| 704 | m_LCUs = NULL; |
---|
| 705 | } |
---|
| 706 | m_encRCSeq = NULL; |
---|
| 707 | m_encRCGOP = NULL; |
---|
| 708 | } |
---|
| 709 | |
---|
[302] | 710 | |
---|
| 711 | #if RATE_CONTROL_INTRA |
---|
| 712 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType) |
---|
| 713 | #else |
---|
[191] | 714 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures ) |
---|
[302] | 715 | #endif |
---|
[191] | 716 | { |
---|
| 717 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 718 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 719 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
---|
[302] | 720 | #if RATE_CONTROL_INTRA |
---|
| 721 | Double estLambda; |
---|
| 722 | if (eSliceType == I_SLICE) |
---|
| 723 | { |
---|
| 724 | estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp); |
---|
| 725 | } |
---|
| 726 | else |
---|
| 727 | { |
---|
| 728 | estLambda = alpha * pow( bpp, beta ); |
---|
| 729 | } |
---|
| 730 | #else |
---|
[191] | 731 | Double estLambda = alpha * pow( bpp, beta ); |
---|
[302] | 732 | #endif |
---|
| 733 | |
---|
[191] | 734 | Double lastLevelLambda = -1.0; |
---|
| 735 | Double lastPicLambda = -1.0; |
---|
| 736 | Double lastValidLambda = -1.0; |
---|
| 737 | list<TEncRCPic*>::iterator it; |
---|
| 738 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
| 739 | { |
---|
| 740 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
| 741 | { |
---|
| 742 | lastLevelLambda = (*it)->getPicActualLambda(); |
---|
| 743 | } |
---|
| 744 | lastPicLambda = (*it)->getPicActualLambda(); |
---|
| 745 | |
---|
| 746 | if ( lastPicLambda > 0.0 ) |
---|
| 747 | { |
---|
| 748 | lastValidLambda = lastPicLambda; |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | if ( lastLevelLambda > 0.0 ) |
---|
| 753 | { |
---|
| 754 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
---|
| 755 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
---|
| 756 | } |
---|
| 757 | |
---|
| 758 | if ( lastPicLambda > 0.0 ) |
---|
| 759 | { |
---|
| 760 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
---|
| 761 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
---|
| 762 | } |
---|
| 763 | else if ( lastValidLambda > 0.0 ) |
---|
| 764 | { |
---|
| 765 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
---|
| 766 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
---|
| 767 | } |
---|
| 768 | else |
---|
| 769 | { |
---|
| 770 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | if ( estLambda < 0.1 ) |
---|
| 774 | { |
---|
| 775 | estLambda = 0.1; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | m_estPicLambda = estLambda; |
---|
[302] | 779 | |
---|
| 780 | #if M0036_RC_IMPROVEMENT |
---|
| 781 | Double totalWeight = 0.0; |
---|
| 782 | // initial BU bit allocation weight |
---|
| 783 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
| 784 | { |
---|
| 785 | #if RC_FIX |
---|
| 786 | Double alphaLCU, betaLCU; |
---|
| 787 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 788 | { |
---|
| 789 | alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
---|
| 790 | betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
---|
| 791 | } |
---|
| 792 | else |
---|
| 793 | { |
---|
| 794 | alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 795 | betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 796 | } |
---|
| 797 | #else |
---|
| 798 | Double alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
---|
| 799 | Double betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
---|
| 800 | #endif |
---|
| 801 | |
---|
| 802 | m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU ); |
---|
| 803 | |
---|
| 804 | if ( m_LCUs[i].m_bitWeight < 0.01 ) |
---|
| 805 | { |
---|
| 806 | m_LCUs[i].m_bitWeight = 0.01; |
---|
| 807 | } |
---|
| 808 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
| 809 | } |
---|
| 810 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
| 811 | { |
---|
| 812 | Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight; |
---|
| 813 | m_LCUs[i].m_bitWeight = BUTargetBits; |
---|
| 814 | } |
---|
| 815 | #endif |
---|
| 816 | |
---|
[191] | 817 | return estLambda; |
---|
| 818 | } |
---|
| 819 | |
---|
| 820 | Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures ) |
---|
| 821 | { |
---|
| 822 | Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
| 823 | |
---|
| 824 | Int lastLevelQP = g_RCInvalidQPValue; |
---|
| 825 | Int lastPicQP = g_RCInvalidQPValue; |
---|
| 826 | Int lastValidQP = g_RCInvalidQPValue; |
---|
| 827 | list<TEncRCPic*>::iterator it; |
---|
| 828 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
| 829 | { |
---|
| 830 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
| 831 | { |
---|
| 832 | lastLevelQP = (*it)->getPicActualQP(); |
---|
| 833 | } |
---|
| 834 | lastPicQP = (*it)->getPicActualQP(); |
---|
| 835 | if ( lastPicQP > g_RCInvalidQPValue ) |
---|
| 836 | { |
---|
| 837 | lastValidQP = lastPicQP; |
---|
| 838 | } |
---|
| 839 | } |
---|
| 840 | |
---|
| 841 | if ( lastLevelQP > g_RCInvalidQPValue ) |
---|
| 842 | { |
---|
| 843 | QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP ); |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | if( lastPicQP > g_RCInvalidQPValue ) |
---|
| 847 | { |
---|
| 848 | QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP ); |
---|
| 849 | } |
---|
| 850 | else if( lastValidQP > g_RCInvalidQPValue ) |
---|
| 851 | { |
---|
| 852 | QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP ); |
---|
| 853 | } |
---|
| 854 | |
---|
| 855 | return QP; |
---|
| 856 | } |
---|
| 857 | |
---|
[302] | 858 | #if RATE_CONTROL_INTRA |
---|
| 859 | Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) |
---|
| 860 | #else |
---|
[191] | 861 | Double TEncRCPic::getLCUTargetBpp() |
---|
[302] | 862 | #endif |
---|
[191] | 863 | { |
---|
| 864 | Int LCUIdx = getLCUCoded(); |
---|
| 865 | Double bpp = -1.0; |
---|
| 866 | Int avgBits = 0; |
---|
[302] | 867 | #if !M0036_RC_IMPROVEMENT |
---|
[191] | 868 | Double totalMAD = -1.0; |
---|
| 869 | Double MAD = -1.0; |
---|
[302] | 870 | #endif |
---|
[191] | 871 | |
---|
[302] | 872 | #if RATE_CONTROL_INTRA |
---|
| 873 | if (eSliceType == I_SLICE){ |
---|
| 874 | Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1; |
---|
| 875 | Int bitrateWindow = min(4,noOfLCUsLeft); |
---|
| 876 | Double MAD = getLCU(LCUIdx).m_costIntra; |
---|
| 877 | |
---|
| 878 | if (m_remainingCostIntra > 0.1 ) |
---|
| 879 | { |
---|
| 880 | Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow; |
---|
| 881 | avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra ); |
---|
| 882 | } |
---|
| 883 | else |
---|
| 884 | { |
---|
| 885 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
| 886 | } |
---|
| 887 | m_remainingCostIntra -= MAD; |
---|
| 888 | } |
---|
| 889 | else |
---|
| 890 | { |
---|
| 891 | #endif |
---|
| 892 | #if M0036_RC_IMPROVEMENT |
---|
| 893 | Double totalWeight = 0; |
---|
| 894 | for ( Int i=LCUIdx; i<m_numberOfLCU; i++ ) |
---|
| 895 | { |
---|
| 896 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
| 897 | } |
---|
| 898 | Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() ); |
---|
| 899 | avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 ); |
---|
| 900 | #else |
---|
[191] | 901 | if ( m_lastPicture == NULL ) |
---|
| 902 | { |
---|
| 903 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
| 904 | } |
---|
| 905 | else |
---|
| 906 | { |
---|
| 907 | MAD = m_lastPicture->getLCU(LCUIdx).m_MAD; |
---|
| 908 | totalMAD = m_lastPicture->getTotalMAD(); |
---|
| 909 | for ( Int i=0; i<LCUIdx; i++ ) |
---|
| 910 | { |
---|
| 911 | totalMAD -= m_lastPicture->getLCU(i).m_MAD; |
---|
| 912 | } |
---|
| 913 | |
---|
| 914 | if ( totalMAD > 0.1 ) |
---|
| 915 | { |
---|
| 916 | avgBits = Int( m_bitsLeft * MAD / totalMAD ); |
---|
| 917 | } |
---|
| 918 | else |
---|
| 919 | { |
---|
| 920 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
| 921 | } |
---|
| 922 | } |
---|
[302] | 923 | #endif |
---|
| 924 | #if RATE_CONTROL_INTRA |
---|
| 925 | } |
---|
| 926 | #endif |
---|
[191] | 927 | |
---|
| 928 | if ( avgBits < 1 ) |
---|
| 929 | { |
---|
| 930 | avgBits = 1; |
---|
| 931 | } |
---|
| 932 | |
---|
| 933 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
---|
| 934 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
---|
| 935 | |
---|
| 936 | return bpp; |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | Double TEncRCPic::getLCUEstLambda( Double bpp ) |
---|
| 940 | { |
---|
| 941 | Int LCUIdx = getLCUCoded(); |
---|
| 942 | Double alpha; |
---|
| 943 | Double beta; |
---|
| 944 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 945 | { |
---|
| 946 | alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
| 947 | beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
| 948 | } |
---|
| 949 | else |
---|
| 950 | { |
---|
| 951 | alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 952 | beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 953 | } |
---|
| 954 | |
---|
| 955 | Double estLambda = alpha * pow( bpp, beta ); |
---|
| 956 | //for Lambda clip, picture level clip |
---|
| 957 | Double clipPicLambda = m_estPicLambda; |
---|
| 958 | |
---|
| 959 | //for Lambda clip, LCU level clip |
---|
| 960 | Double clipNeighbourLambda = -1.0; |
---|
| 961 | for ( int i=LCUIdx - 1; i>=0; i-- ) |
---|
| 962 | { |
---|
| 963 | if ( m_LCUs[i].m_lambda > 0 ) |
---|
| 964 | { |
---|
| 965 | clipNeighbourLambda = m_LCUs[i].m_lambda; |
---|
| 966 | break; |
---|
| 967 | } |
---|
| 968 | } |
---|
| 969 | |
---|
| 970 | if ( clipNeighbourLambda > 0.0 ) |
---|
| 971 | { |
---|
| 972 | estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda ); |
---|
| 973 | } |
---|
| 974 | |
---|
| 975 | if ( clipPicLambda > 0.0 ) |
---|
| 976 | { |
---|
| 977 | estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda ); |
---|
| 978 | } |
---|
| 979 | else |
---|
| 980 | { |
---|
| 981 | estLambda = Clip3( 10.0, 1000.0, estLambda ); |
---|
| 982 | } |
---|
| 983 | |
---|
| 984 | if ( estLambda < 0.1 ) |
---|
| 985 | { |
---|
| 986 | estLambda = 0.1; |
---|
| 987 | } |
---|
| 988 | |
---|
| 989 | return estLambda; |
---|
| 990 | } |
---|
| 991 | |
---|
| 992 | Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP ) |
---|
| 993 | { |
---|
| 994 | Int LCUIdx = getLCUCoded(); |
---|
| 995 | Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
| 996 | |
---|
| 997 | //for Lambda clip, LCU level clip |
---|
| 998 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
| 999 | for ( int i=LCUIdx - 1; i>=0; i-- ) |
---|
| 1000 | { |
---|
| 1001 | if ( (getLCU(i)).m_QP > g_RCInvalidQPValue ) |
---|
| 1002 | { |
---|
| 1003 | clipNeighbourQP = getLCU(i).m_QP; |
---|
| 1004 | break; |
---|
| 1005 | } |
---|
| 1006 | } |
---|
| 1007 | |
---|
| 1008 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
| 1009 | { |
---|
| 1010 | estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP ); |
---|
| 1011 | } |
---|
| 1012 | |
---|
| 1013 | estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP ); |
---|
| 1014 | |
---|
| 1015 | return estQP; |
---|
| 1016 | } |
---|
| 1017 | |
---|
| 1018 | Void TEncRCPic::updateAfterLCU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter ) |
---|
| 1019 | { |
---|
| 1020 | m_LCUs[LCUIdx].m_actualBits = bits; |
---|
| 1021 | m_LCUs[LCUIdx].m_QP = QP; |
---|
| 1022 | m_LCUs[LCUIdx].m_lambda = lambda; |
---|
| 1023 | |
---|
| 1024 | m_LCULeft--; |
---|
| 1025 | m_bitsLeft -= bits; |
---|
| 1026 | m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel; |
---|
| 1027 | |
---|
| 1028 | if ( !updateLCUParameter ) |
---|
| 1029 | { |
---|
| 1030 | return; |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | if ( !m_encRCSeq->getUseLCUSeparateModel() ) |
---|
| 1034 | { |
---|
| 1035 | return; |
---|
| 1036 | } |
---|
| 1037 | |
---|
| 1038 | Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
| 1039 | Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
| 1040 | |
---|
| 1041 | Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits; |
---|
| 1042 | Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel; |
---|
| 1043 | Double bpp = ( Double )LCUActualBits/( Double )LCUTotalPixels; |
---|
| 1044 | Double calLambda = alpha * pow( bpp, beta ); |
---|
| 1045 | Double inputLambda = m_LCUs[LCUIdx].m_lambda; |
---|
| 1046 | |
---|
| 1047 | if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 ) |
---|
| 1048 | { |
---|
| 1049 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
| 1050 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
| 1051 | |
---|
[302] | 1052 | #if M0036_RC_IMPROVEMENT |
---|
| 1053 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 1054 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
| 1055 | #else |
---|
[191] | 1056 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
| 1057 | beta = Clip3( -3.0, -0.1, beta ); |
---|
[302] | 1058 | #endif |
---|
[191] | 1059 | |
---|
| 1060 | TRCParameter rcPara; |
---|
| 1061 | rcPara.m_alpha = alpha; |
---|
| 1062 | rcPara.m_beta = beta; |
---|
| 1063 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
| 1064 | |
---|
| 1065 | return; |
---|
| 1066 | } |
---|
| 1067 | |
---|
| 1068 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
| 1069 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
| 1070 | double lnbpp = log( bpp ); |
---|
[302] | 1071 | #if M0036_RC_IMPROVEMENT |
---|
| 1072 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
| 1073 | #else |
---|
[191] | 1074 | lnbpp = Clip3( -5.0, 1.0, lnbpp ); |
---|
[302] | 1075 | #endif |
---|
[191] | 1076 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
| 1077 | |
---|
[302] | 1078 | #if M0036_RC_IMPROVEMENT |
---|
| 1079 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 1080 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
| 1081 | #else |
---|
[191] | 1082 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
| 1083 | beta = Clip3( -3.0, -0.1, beta ); |
---|
[302] | 1084 | #endif |
---|
[191] | 1085 | TRCParameter rcPara; |
---|
| 1086 | rcPara.m_alpha = alpha; |
---|
| 1087 | rcPara.m_beta = beta; |
---|
| 1088 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
| 1089 | |
---|
| 1090 | } |
---|
| 1091 | |
---|
[302] | 1092 | #if !M0036_RC_IMPROVEMENT |
---|
[191] | 1093 | Double TEncRCPic::getEffectivePercentage() |
---|
| 1094 | { |
---|
| 1095 | Int effectivePiexels = 0; |
---|
| 1096 | Int totalPixels = 0; |
---|
| 1097 | |
---|
| 1098 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
| 1099 | { |
---|
| 1100 | totalPixels += m_LCUs[i].m_numberOfPixel; |
---|
| 1101 | if ( m_LCUs[i].m_QP > 0 ) |
---|
| 1102 | { |
---|
| 1103 | effectivePiexels += m_LCUs[i].m_numberOfPixel; |
---|
| 1104 | } |
---|
| 1105 | } |
---|
| 1106 | |
---|
| 1107 | Double effectivePixelPercentage = (Double)effectivePiexels/(Double)totalPixels; |
---|
| 1108 | return effectivePixelPercentage; |
---|
| 1109 | } |
---|
[302] | 1110 | #endif |
---|
[191] | 1111 | |
---|
| 1112 | Double TEncRCPic::calAverageQP() |
---|
| 1113 | { |
---|
| 1114 | Int totalQPs = 0; |
---|
| 1115 | Int numTotalLCUs = 0; |
---|
| 1116 | |
---|
| 1117 | Int i; |
---|
| 1118 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
| 1119 | { |
---|
| 1120 | if ( m_LCUs[i].m_QP > 0 ) |
---|
| 1121 | { |
---|
| 1122 | totalQPs += m_LCUs[i].m_QP; |
---|
| 1123 | numTotalLCUs++; |
---|
| 1124 | } |
---|
| 1125 | } |
---|
| 1126 | |
---|
| 1127 | Double avgQP = 0.0; |
---|
| 1128 | if ( numTotalLCUs == 0 ) |
---|
| 1129 | { |
---|
| 1130 | avgQP = g_RCInvalidQPValue; |
---|
| 1131 | } |
---|
| 1132 | else |
---|
| 1133 | { |
---|
| 1134 | avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs); |
---|
| 1135 | } |
---|
| 1136 | return avgQP; |
---|
| 1137 | } |
---|
| 1138 | |
---|
| 1139 | Double TEncRCPic::calAverageLambda() |
---|
| 1140 | { |
---|
| 1141 | Double totalLambdas = 0.0; |
---|
| 1142 | Int numTotalLCUs = 0; |
---|
| 1143 | |
---|
| 1144 | Int i; |
---|
| 1145 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
| 1146 | { |
---|
| 1147 | if ( m_LCUs[i].m_lambda > 0.01 ) |
---|
| 1148 | { |
---|
| 1149 | totalLambdas += log( m_LCUs[i].m_lambda ); |
---|
| 1150 | numTotalLCUs++; |
---|
| 1151 | } |
---|
| 1152 | } |
---|
| 1153 | |
---|
| 1154 | Double avgLambda; |
---|
| 1155 | if( numTotalLCUs == 0 ) |
---|
| 1156 | { |
---|
| 1157 | avgLambda = -1.0; |
---|
| 1158 | } |
---|
| 1159 | else |
---|
| 1160 | { |
---|
| 1161 | avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs ); |
---|
| 1162 | } |
---|
| 1163 | return avgLambda; |
---|
| 1164 | } |
---|
| 1165 | |
---|
[302] | 1166 | #if M0036_RC_IMPROVEMENT |
---|
| 1167 | #if RATE_CONTROL_INTRA |
---|
| 1168 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType) |
---|
| 1169 | #else |
---|
| 1170 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda ) |
---|
| 1171 | #endif |
---|
| 1172 | #else |
---|
[191] | 1173 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage ) |
---|
[302] | 1174 | #endif |
---|
[191] | 1175 | { |
---|
| 1176 | m_picActualHeaderBits = actualHeaderBits; |
---|
| 1177 | m_picActualBits = actualTotalBits; |
---|
| 1178 | if ( averageQP > 0.0 ) |
---|
| 1179 | { |
---|
| 1180 | m_picQP = Int( averageQP + 0.5 ); |
---|
| 1181 | } |
---|
| 1182 | else |
---|
| 1183 | { |
---|
| 1184 | m_picQP = g_RCInvalidQPValue; |
---|
| 1185 | } |
---|
| 1186 | m_picLambda = averageLambda; |
---|
[302] | 1187 | #if !M0036_RC_IMPROVEMENT |
---|
[191] | 1188 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
| 1189 | { |
---|
| 1190 | m_totalMAD += m_LCUs[i].m_MAD; |
---|
| 1191 | } |
---|
[302] | 1192 | #endif |
---|
[191] | 1193 | |
---|
| 1194 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 1195 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
[302] | 1196 | #if RATE_CONTROL_INTRA |
---|
| 1197 | if (eSliceType == I_SLICE) |
---|
| 1198 | { |
---|
| 1199 | updateAlphaBetaIntra(&alpha, &beta); |
---|
| 1200 | } |
---|
| 1201 | else |
---|
| 1202 | { |
---|
| 1203 | #endif |
---|
[191] | 1204 | // update parameters |
---|
| 1205 | Double picActualBits = ( Double )m_picActualBits; |
---|
| 1206 | Double picActualBpp = picActualBits/(Double)m_numberOfPixel; |
---|
| 1207 | Double calLambda = alpha * pow( picActualBpp, beta ); |
---|
| 1208 | Double inputLambda = m_picLambda; |
---|
| 1209 | |
---|
[302] | 1210 | #if M0036_RC_IMPROVEMENT |
---|
| 1211 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 ) |
---|
| 1212 | #else |
---|
[191] | 1213 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 || effectivePercentage < 0.05 ) |
---|
[302] | 1214 | #endif |
---|
[191] | 1215 | { |
---|
| 1216 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
| 1217 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
| 1218 | |
---|
[302] | 1219 | #if M0036_RC_IMPROVEMENT |
---|
| 1220 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 1221 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
| 1222 | #else |
---|
[191] | 1223 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
| 1224 | beta = Clip3( -3.0, -0.1, beta ); |
---|
[302] | 1225 | #endif |
---|
[191] | 1226 | TRCParameter rcPara; |
---|
| 1227 | rcPara.m_alpha = alpha; |
---|
| 1228 | rcPara.m_beta = beta; |
---|
| 1229 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
| 1230 | |
---|
| 1231 | return; |
---|
| 1232 | } |
---|
| 1233 | |
---|
| 1234 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
| 1235 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
| 1236 | double lnbpp = log( picActualBpp ); |
---|
[302] | 1237 | #if M0036_RC_IMPROVEMENT |
---|
| 1238 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
| 1239 | #else |
---|
[191] | 1240 | lnbpp = Clip3( -5.0, 1.0, lnbpp ); |
---|
[302] | 1241 | #endif |
---|
[191] | 1242 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
| 1243 | |
---|
[302] | 1244 | #if M0036_RC_IMPROVEMENT |
---|
| 1245 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
| 1246 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
| 1247 | #else |
---|
[191] | 1248 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
| 1249 | beta = Clip3( -3.0, -0.1, beta ); |
---|
[302] | 1250 | #endif |
---|
| 1251 | #if RATE_CONTROL_INTRA |
---|
| 1252 | } |
---|
| 1253 | #endif |
---|
[191] | 1254 | |
---|
| 1255 | TRCParameter rcPara; |
---|
| 1256 | rcPara.m_alpha = alpha; |
---|
| 1257 | rcPara.m_beta = beta; |
---|
| 1258 | |
---|
| 1259 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
[302] | 1260 | |
---|
| 1261 | #if M0036_RC_IMPROVEMENT |
---|
| 1262 | if ( m_frameLevel == 1 ) |
---|
| 1263 | { |
---|
| 1264 | Double currLambda = Clip3( 0.1, 10000.0, m_picLambda ); |
---|
| 1265 | Double updateLastLambda = g_RCWeightHistoryLambda * m_encRCSeq->getLastLambda() + g_RCWeightCurrentLambda * currLambda; |
---|
| 1266 | m_encRCSeq->setLastLambda( updateLastLambda ); |
---|
| 1267 | } |
---|
| 1268 | #endif |
---|
[191] | 1269 | } |
---|
| 1270 | |
---|
[302] | 1271 | #if RATE_CONTROL_INTRA |
---|
| 1272 | Int TEncRCPic::getRefineBitsForIntra( Int orgBits ) |
---|
| 1273 | { |
---|
| 1274 | Double alpha=0.25, beta=0.5582; |
---|
| 1275 | Int iIntraBits; |
---|
| 1276 | |
---|
| 1277 | if (orgBits*40 < m_numberOfPixel) |
---|
| 1278 | { |
---|
| 1279 | alpha=0.25; |
---|
| 1280 | } |
---|
| 1281 | else |
---|
| 1282 | { |
---|
| 1283 | alpha=0.30; |
---|
| 1284 | } |
---|
| 1285 | |
---|
| 1286 | iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5); |
---|
| 1287 | |
---|
| 1288 | return iIntraBits; |
---|
| 1289 | } |
---|
| 1290 | |
---|
| 1291 | Double TEncRCPic::calculateLambdaIntra(double alpha, double beta, double MADPerPixel, double bitsPerPixel) |
---|
| 1292 | { |
---|
| 1293 | return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) ); |
---|
| 1294 | } |
---|
| 1295 | |
---|
| 1296 | Void TEncRCPic::updateAlphaBetaIntra(double *alpha, double *beta) |
---|
| 1297 | { |
---|
| 1298 | Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1)); |
---|
| 1299 | Double diffLambda = (*beta)*(log((Double)m_picActualBits)-log((Double)m_targetBits)); |
---|
| 1300 | |
---|
| 1301 | diffLambda = Clip3(-0.125, 0.125, 0.25*diffLambda); |
---|
| 1302 | *alpha = (*alpha) * exp(diffLambda); |
---|
| 1303 | *beta = (*beta) + diffLambda / lnbpp; |
---|
| 1304 | } |
---|
| 1305 | |
---|
| 1306 | |
---|
| 1307 | Void TEncRCPic::getLCUInitTargetBits() |
---|
| 1308 | { |
---|
| 1309 | Int iAvgBits = 0; |
---|
| 1310 | |
---|
| 1311 | m_remainingCostIntra = m_totalCostIntra; |
---|
| 1312 | for (Int i=m_numberOfLCU-1; i>=0; i--) |
---|
| 1313 | { |
---|
| 1314 | iAvgBits += Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra); |
---|
| 1315 | getLCU(i).m_targetBitsLeft = iAvgBits; |
---|
| 1316 | } |
---|
| 1317 | } |
---|
| 1318 | |
---|
| 1319 | |
---|
| 1320 | Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP) |
---|
| 1321 | { |
---|
| 1322 | Int LCUIdx = getLCUCoded(); |
---|
| 1323 | |
---|
| 1324 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
| 1325 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
| 1326 | |
---|
| 1327 | Double costPerPixel = getLCU(LCUIdx).m_costIntra/(Double)getLCU(LCUIdx).m_numberOfPixel; |
---|
| 1328 | costPerPixel = pow(costPerPixel, BETA1); |
---|
| 1329 | Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp); |
---|
| 1330 | |
---|
| 1331 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
| 1332 | for (int i=LCUIdx-1; i>=0; i--) |
---|
| 1333 | { |
---|
| 1334 | if ((getLCU(i)).m_QP > g_RCInvalidQPValue) |
---|
| 1335 | { |
---|
| 1336 | clipNeighbourQP = getLCU(i).m_QP; |
---|
| 1337 | break; |
---|
| 1338 | } |
---|
| 1339 | } |
---|
| 1340 | |
---|
| 1341 | Int minQP = clipPicQP - 2; |
---|
| 1342 | Int maxQP = clipPicQP + 2; |
---|
| 1343 | |
---|
| 1344 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
| 1345 | { |
---|
| 1346 | maxQP = min(clipNeighbourQP + 1, maxQP); |
---|
| 1347 | minQP = max(clipNeighbourQP - 1, minQP); |
---|
| 1348 | } |
---|
| 1349 | |
---|
| 1350 | Double maxLambda=exp(((Double)(maxQP+0.49)-13.7122)/4.2005); |
---|
| 1351 | Double minLambda=exp(((Double)(minQP-0.49)-13.7122)/4.2005); |
---|
| 1352 | |
---|
| 1353 | estLambda = Clip3(minLambda, maxLambda, estLambda); |
---|
| 1354 | |
---|
| 1355 | *estQP = Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 ); |
---|
| 1356 | *estQP = Clip3(minQP, maxQP, *estQP); |
---|
| 1357 | |
---|
| 1358 | return estLambda; |
---|
| 1359 | } |
---|
| 1360 | #endif |
---|
| 1361 | |
---|
[191] | 1362 | TEncRateCtrl::TEncRateCtrl() |
---|
| 1363 | { |
---|
| 1364 | m_encRCSeq = NULL; |
---|
| 1365 | m_encRCGOP = NULL; |
---|
| 1366 | m_encRCPic = NULL; |
---|
| 1367 | } |
---|
| 1368 | |
---|
| 1369 | TEncRateCtrl::~TEncRateCtrl() |
---|
| 1370 | { |
---|
| 1371 | destroy(); |
---|
| 1372 | } |
---|
| 1373 | |
---|
| 1374 | Void TEncRateCtrl::destroy() |
---|
| 1375 | { |
---|
| 1376 | if ( m_encRCSeq != NULL ) |
---|
| 1377 | { |
---|
| 1378 | delete m_encRCSeq; |
---|
| 1379 | m_encRCSeq = NULL; |
---|
| 1380 | } |
---|
| 1381 | if ( m_encRCGOP != NULL ) |
---|
| 1382 | { |
---|
| 1383 | delete m_encRCGOP; |
---|
| 1384 | m_encRCGOP = NULL; |
---|
| 1385 | } |
---|
| 1386 | while ( m_listRCPictures.size() > 0 ) |
---|
| 1387 | { |
---|
| 1388 | TEncRCPic* p = m_listRCPictures.front(); |
---|
| 1389 | m_listRCPictures.pop_front(); |
---|
| 1390 | delete p; |
---|
| 1391 | } |
---|
| 1392 | } |
---|
| 1393 | |
---|
[302] | 1394 | #if M0036_RC_IMPROVEMENT |
---|
| 1395 | 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] ) |
---|
| 1396 | #else |
---|
[191] | 1397 | Void TEncRateCtrl::init( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Bool keepHierBits, Bool useLCUSeparateModel, GOPEntry GOPList[MAX_GOP] ) |
---|
[302] | 1398 | #endif |
---|
[191] | 1399 | { |
---|
| 1400 | destroy(); |
---|
| 1401 | |
---|
| 1402 | Bool isLowdelay = true; |
---|
| 1403 | for ( Int i=0; i<GOPSize-1; i++ ) |
---|
| 1404 | { |
---|
| 1405 | if ( GOPList[i].m_POC > GOPList[i+1].m_POC ) |
---|
| 1406 | { |
---|
| 1407 | isLowdelay = false; |
---|
| 1408 | break; |
---|
| 1409 | } |
---|
| 1410 | } |
---|
| 1411 | |
---|
| 1412 | Int numberOfLevel = 1; |
---|
[302] | 1413 | #if M0036_RC_IMPROVEMENT |
---|
| 1414 | Int adaptiveBit = 0; |
---|
| 1415 | if ( keepHierBits > 0 ) |
---|
| 1416 | #else |
---|
[191] | 1417 | if ( keepHierBits ) |
---|
[302] | 1418 | #endif |
---|
[191] | 1419 | { |
---|
| 1420 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
| 1421 | } |
---|
| 1422 | if ( !isLowdelay && GOPSize == 8 ) |
---|
| 1423 | { |
---|
| 1424 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
| 1425 | } |
---|
| 1426 | numberOfLevel++; // intra picture |
---|
| 1427 | numberOfLevel++; // non-reference picture |
---|
| 1428 | |
---|
| 1429 | |
---|
| 1430 | Int* bitsRatio; |
---|
| 1431 | bitsRatio = new Int[ GOPSize ]; |
---|
| 1432 | for ( Int i=0; i<GOPSize; i++ ) |
---|
| 1433 | { |
---|
| 1434 | bitsRatio[i] = 10; |
---|
| 1435 | if ( !GOPList[i].m_refPic ) |
---|
| 1436 | { |
---|
| 1437 | bitsRatio[i] = 2; |
---|
| 1438 | } |
---|
| 1439 | } |
---|
[302] | 1440 | |
---|
| 1441 | #if M0036_RC_IMPROVEMENT |
---|
| 1442 | if ( keepHierBits > 0 ) |
---|
| 1443 | #else |
---|
[191] | 1444 | if ( keepHierBits ) |
---|
[302] | 1445 | #endif |
---|
[191] | 1446 | { |
---|
| 1447 | Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); |
---|
| 1448 | if ( GOPSize == 4 && isLowdelay ) |
---|
| 1449 | { |
---|
| 1450 | if ( bpp > 0.2 ) |
---|
| 1451 | { |
---|
| 1452 | bitsRatio[0] = 2; |
---|
| 1453 | bitsRatio[1] = 3; |
---|
| 1454 | bitsRatio[2] = 2; |
---|
| 1455 | bitsRatio[3] = 6; |
---|
| 1456 | } |
---|
| 1457 | else if( bpp > 0.1 ) |
---|
| 1458 | { |
---|
| 1459 | bitsRatio[0] = 2; |
---|
| 1460 | bitsRatio[1] = 3; |
---|
| 1461 | bitsRatio[2] = 2; |
---|
| 1462 | bitsRatio[3] = 10; |
---|
| 1463 | } |
---|
| 1464 | else if ( bpp > 0.05 ) |
---|
| 1465 | { |
---|
| 1466 | bitsRatio[0] = 2; |
---|
| 1467 | bitsRatio[1] = 3; |
---|
| 1468 | bitsRatio[2] = 2; |
---|
| 1469 | bitsRatio[3] = 12; |
---|
| 1470 | } |
---|
| 1471 | else |
---|
| 1472 | { |
---|
| 1473 | bitsRatio[0] = 2; |
---|
| 1474 | bitsRatio[1] = 3; |
---|
| 1475 | bitsRatio[2] = 2; |
---|
| 1476 | bitsRatio[3] = 14; |
---|
| 1477 | } |
---|
[302] | 1478 | #if M0036_RC_IMPROVEMENT |
---|
| 1479 | if ( keepHierBits == 2 ) |
---|
| 1480 | { |
---|
| 1481 | adaptiveBit = 1; |
---|
| 1482 | } |
---|
| 1483 | #endif |
---|
[191] | 1484 | } |
---|
| 1485 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
| 1486 | { |
---|
| 1487 | if ( bpp > 0.2 ) |
---|
| 1488 | { |
---|
| 1489 | bitsRatio[0] = 15; |
---|
| 1490 | bitsRatio[1] = 5; |
---|
| 1491 | bitsRatio[2] = 4; |
---|
| 1492 | bitsRatio[3] = 1; |
---|
| 1493 | bitsRatio[4] = 1; |
---|
| 1494 | bitsRatio[5] = 4; |
---|
| 1495 | bitsRatio[6] = 1; |
---|
| 1496 | bitsRatio[7] = 1; |
---|
| 1497 | } |
---|
| 1498 | else if ( bpp > 0.1 ) |
---|
| 1499 | { |
---|
| 1500 | bitsRatio[0] = 20; |
---|
| 1501 | bitsRatio[1] = 6; |
---|
| 1502 | bitsRatio[2] = 4; |
---|
| 1503 | bitsRatio[3] = 1; |
---|
| 1504 | bitsRatio[4] = 1; |
---|
| 1505 | bitsRatio[5] = 4; |
---|
| 1506 | bitsRatio[6] = 1; |
---|
| 1507 | bitsRatio[7] = 1; |
---|
| 1508 | } |
---|
| 1509 | else if ( bpp > 0.05 ) |
---|
| 1510 | { |
---|
| 1511 | bitsRatio[0] = 25; |
---|
| 1512 | bitsRatio[1] = 7; |
---|
| 1513 | bitsRatio[2] = 4; |
---|
| 1514 | bitsRatio[3] = 1; |
---|
| 1515 | bitsRatio[4] = 1; |
---|
| 1516 | bitsRatio[5] = 4; |
---|
| 1517 | bitsRatio[6] = 1; |
---|
| 1518 | bitsRatio[7] = 1; |
---|
| 1519 | } |
---|
| 1520 | else |
---|
| 1521 | { |
---|
| 1522 | bitsRatio[0] = 30; |
---|
| 1523 | bitsRatio[1] = 8; |
---|
| 1524 | bitsRatio[2] = 4; |
---|
| 1525 | bitsRatio[3] = 1; |
---|
| 1526 | bitsRatio[4] = 1; |
---|
| 1527 | bitsRatio[5] = 4; |
---|
| 1528 | bitsRatio[6] = 1; |
---|
| 1529 | bitsRatio[7] = 1; |
---|
| 1530 | } |
---|
[302] | 1531 | #if M0036_RC_IMPROVEMENT |
---|
| 1532 | if ( keepHierBits == 2 ) |
---|
| 1533 | { |
---|
| 1534 | adaptiveBit = 2; |
---|
| 1535 | } |
---|
| 1536 | #endif |
---|
[191] | 1537 | } |
---|
| 1538 | else |
---|
| 1539 | { |
---|
[302] | 1540 | #if M0036_RC_IMPROVEMENT |
---|
| 1541 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" ); |
---|
| 1542 | #else |
---|
[191] | 1543 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently." ); |
---|
[302] | 1544 | #endif |
---|
[191] | 1545 | } |
---|
| 1546 | } |
---|
| 1547 | |
---|
| 1548 | Int* GOPID2Level = new int[ GOPSize ]; |
---|
| 1549 | for ( int i=0; i<GOPSize; i++ ) |
---|
| 1550 | { |
---|
| 1551 | GOPID2Level[i] = 1; |
---|
| 1552 | if ( !GOPList[i].m_refPic ) |
---|
| 1553 | { |
---|
| 1554 | GOPID2Level[i] = 2; |
---|
| 1555 | } |
---|
| 1556 | } |
---|
[302] | 1557 | #if M0036_RC_IMPROVEMENT |
---|
| 1558 | if ( keepHierBits > 0 ) |
---|
| 1559 | #else |
---|
[191] | 1560 | if ( keepHierBits ) |
---|
[302] | 1561 | #endif |
---|
[191] | 1562 | { |
---|
| 1563 | if ( GOPSize == 4 && isLowdelay ) |
---|
| 1564 | { |
---|
| 1565 | GOPID2Level[0] = 3; |
---|
| 1566 | GOPID2Level[1] = 2; |
---|
| 1567 | GOPID2Level[2] = 3; |
---|
| 1568 | GOPID2Level[3] = 1; |
---|
| 1569 | } |
---|
| 1570 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
| 1571 | { |
---|
| 1572 | GOPID2Level[0] = 1; |
---|
| 1573 | GOPID2Level[1] = 2; |
---|
| 1574 | GOPID2Level[2] = 3; |
---|
| 1575 | GOPID2Level[3] = 4; |
---|
| 1576 | GOPID2Level[4] = 4; |
---|
| 1577 | GOPID2Level[5] = 3; |
---|
| 1578 | GOPID2Level[6] = 4; |
---|
| 1579 | GOPID2Level[7] = 4; |
---|
| 1580 | } |
---|
| 1581 | } |
---|
| 1582 | |
---|
| 1583 | if ( !isLowdelay && GOPSize == 8 ) |
---|
| 1584 | { |
---|
| 1585 | GOPID2Level[0] = 1; |
---|
| 1586 | GOPID2Level[1] = 2; |
---|
| 1587 | GOPID2Level[2] = 3; |
---|
| 1588 | GOPID2Level[3] = 4; |
---|
| 1589 | GOPID2Level[4] = 4; |
---|
| 1590 | GOPID2Level[5] = 3; |
---|
| 1591 | GOPID2Level[6] = 4; |
---|
| 1592 | GOPID2Level[7] = 4; |
---|
| 1593 | } |
---|
| 1594 | |
---|
| 1595 | m_encRCSeq = new TEncRCSeq; |
---|
[302] | 1596 | #if M0036_RC_IMPROVEMENT |
---|
| 1597 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit ); |
---|
| 1598 | #else |
---|
[191] | 1599 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel ); |
---|
[302] | 1600 | #endif |
---|
[191] | 1601 | m_encRCSeq->initBitsRatio( bitsRatio ); |
---|
| 1602 | m_encRCSeq->initGOPID2Level( GOPID2Level ); |
---|
| 1603 | m_encRCSeq->initPicPara(); |
---|
| 1604 | if ( useLCUSeparateModel ) |
---|
| 1605 | { |
---|
| 1606 | m_encRCSeq->initLCUPara(); |
---|
| 1607 | } |
---|
| 1608 | |
---|
| 1609 | delete[] bitsRatio; |
---|
| 1610 | delete[] GOPID2Level; |
---|
| 1611 | } |
---|
| 1612 | |
---|
| 1613 | Void TEncRateCtrl::initRCPic( Int frameLevel ) |
---|
| 1614 | { |
---|
| 1615 | m_encRCPic = new TEncRCPic; |
---|
| 1616 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures ); |
---|
| 1617 | } |
---|
| 1618 | |
---|
| 1619 | Void TEncRateCtrl::initRCGOP( Int numberOfPictures ) |
---|
| 1620 | { |
---|
| 1621 | m_encRCGOP = new TEncRCGOP; |
---|
| 1622 | m_encRCGOP->create( m_encRCSeq, numberOfPictures ); |
---|
| 1623 | } |
---|
| 1624 | |
---|
| 1625 | Void TEncRateCtrl::destroyRCGOP() |
---|
| 1626 | { |
---|
| 1627 | delete m_encRCGOP; |
---|
| 1628 | m_encRCGOP = NULL; |
---|
| 1629 | } |
---|
| 1630 | |
---|
| 1631 | #else |
---|
| 1632 | |
---|
| 1633 | #define ADJUSTMENT_FACTOR 0.60 |
---|
| 1634 | #define HIGH_QSTEP_THRESHOLD 9.5238 |
---|
| 1635 | #define HIGH_QSTEP_ALPHA 4.9371 |
---|
| 1636 | #define HIGH_QSTEP_BETA 0.0922 |
---|
| 1637 | #define LOW_QSTEP_ALPHA 16.7429 |
---|
| 1638 | #define LOW_QSTEP_BETA -1.1494 |
---|
| 1639 | |
---|
| 1640 | #define MAD_PRED_Y1 1.0 |
---|
| 1641 | #define MAD_PRED_Y2 0.0 |
---|
| 1642 | |
---|
| 1643 | enum MAD_HISOTRY { |
---|
| 1644 | MAD_PPPrevious = 0, |
---|
| 1645 | MAD_PPrevious = 1, |
---|
| 1646 | MAD_Previous = 2 |
---|
| 1647 | }; |
---|
| 1648 | |
---|
| 1649 | Void MADLinearModel::initMADLinearModel() |
---|
| 1650 | { |
---|
| 1651 | m_activeOn = false; |
---|
| 1652 | m_paramY1 = 1.0; |
---|
| 1653 | m_paramY2 = 0.0; |
---|
| 1654 | m_costMADs[0] = m_costMADs[1] = m_costMADs[2] = 0.0; |
---|
| 1655 | } |
---|
| 1656 | |
---|
| 1657 | Double MADLinearModel::getMAD() |
---|
| 1658 | { |
---|
| 1659 | Double costPredMAD = m_paramY1 * m_costMADs[MAD_Previous] + m_paramY2; |
---|
| 1660 | |
---|
| 1661 | if(costPredMAD < 0) |
---|
| 1662 | { |
---|
| 1663 | costPredMAD = m_costMADs[MAD_Previous]; |
---|
| 1664 | m_paramY1 = MAD_PRED_Y1; |
---|
| 1665 | m_paramY2 = MAD_PRED_Y2; |
---|
| 1666 | } |
---|
| 1667 | return costPredMAD; |
---|
| 1668 | } |
---|
| 1669 | |
---|
| 1670 | Void MADLinearModel::updateMADLiearModel() |
---|
| 1671 | { |
---|
| 1672 | Double dNewY1 = ((m_costMADs[MAD_Previous] - m_costMADs[MAD_PPrevious]) / (m_costMADs[MAD_PPrevious] - m_costMADs[MAD_PPPrevious])); |
---|
| 1673 | Double dNewY2 = (m_costMADs[MAD_Previous] - (dNewY1*m_costMADs[MAD_PPrevious])); |
---|
| 1674 | |
---|
| 1675 | m_paramY1 = 0.70+0.20*m_paramY1+ 0.10*dNewY1; |
---|
| 1676 | m_paramY2 = 0.20*m_paramY2+ 0.10*dNewY2; |
---|
| 1677 | } |
---|
| 1678 | |
---|
| 1679 | Void MADLinearModel::updateMADHistory(Double dMAD) |
---|
| 1680 | { |
---|
| 1681 | m_costMADs[MAD_PPPrevious] = m_costMADs[MAD_PPrevious]; |
---|
| 1682 | m_costMADs[MAD_PPrevious ] = m_costMADs[MAD_Previous ]; |
---|
| 1683 | m_costMADs[MAD_Previous ] = dMAD; |
---|
| 1684 | m_activeOn = (m_costMADs[MAD_Previous ] && m_costMADs[MAD_PPrevious ] && m_costMADs[MAD_PPPrevious]); |
---|
| 1685 | } |
---|
| 1686 | |
---|
| 1687 | |
---|
| 1688 | Void PixelBaseURQQuadraticModel::initPixelBaseQuadraticModel() |
---|
| 1689 | { |
---|
| 1690 | m_paramHighX1 = HIGH_QSTEP_ALPHA; |
---|
| 1691 | m_paramHighX2 = HIGH_QSTEP_BETA; |
---|
| 1692 | m_paramLowX1 = LOW_QSTEP_ALPHA; |
---|
| 1693 | m_paramLowX2 = LOW_QSTEP_BETA; |
---|
| 1694 | } |
---|
| 1695 | |
---|
| 1696 | Int PixelBaseURQQuadraticModel::getQP(Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD) |
---|
| 1697 | { |
---|
| 1698 | Double qStep; |
---|
| 1699 | Double bppPerMAD = (Double)(targetBits/(numberOfPixels*costPredMAD)); |
---|
| 1700 | |
---|
| 1701 | if(xConvertQP2QStep(qp) >= HIGH_QSTEP_THRESHOLD) |
---|
| 1702 | { |
---|
| 1703 | qStep = 1/( sqrt((bppPerMAD/m_paramHighX1)+((m_paramHighX2*m_paramHighX2)/(4*m_paramHighX1*m_paramHighX1))) - (m_paramHighX2/(2*m_paramHighX1))); |
---|
| 1704 | } |
---|
| 1705 | else |
---|
| 1706 | { |
---|
| 1707 | qStep = 1/( sqrt((bppPerMAD/m_paramLowX1)+((m_paramLowX2*m_paramLowX2)/(4*m_paramLowX1*m_paramLowX1))) - (m_paramLowX2/(2*m_paramLowX1))); |
---|
| 1708 | } |
---|
| 1709 | |
---|
| 1710 | return xConvertQStep2QP(qStep); |
---|
| 1711 | } |
---|
| 1712 | |
---|
| 1713 | Void PixelBaseURQQuadraticModel::updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD) |
---|
| 1714 | { |
---|
| 1715 | Double qStep = xConvertQP2QStep(qp); |
---|
| 1716 | Double invqStep = (1/qStep); |
---|
| 1717 | Double paramNewX1, paramNewX2; |
---|
| 1718 | |
---|
| 1719 | if(qStep >= HIGH_QSTEP_THRESHOLD) |
---|
| 1720 | { |
---|
| 1721 | paramNewX2 = (((bits/(numberOfPixels*costMAD))-(23.3772*invqStep*invqStep))/((1-200*invqStep)*invqStep)); |
---|
| 1722 | paramNewX1 = (23.3772-200*paramNewX2); |
---|
| 1723 | m_paramHighX1 = 0.70*HIGH_QSTEP_ALPHA + 0.20 * m_paramHighX1 + 0.10 * paramNewX1; |
---|
| 1724 | m_paramHighX2 = 0.70*HIGH_QSTEP_BETA + 0.20 * m_paramHighX2 + 0.10 * paramNewX2; |
---|
| 1725 | } |
---|
| 1726 | else |
---|
| 1727 | { |
---|
| 1728 | paramNewX2 = (((bits/(numberOfPixels*costMAD))-(5.8091*invqStep*invqStep))/((1-9.5455*invqStep)*invqStep)); |
---|
| 1729 | paramNewX1 = (5.8091-9.5455*paramNewX2); |
---|
| 1730 | m_paramLowX1 = 0.90*LOW_QSTEP_ALPHA + 0.09 * m_paramLowX1 + 0.01 * paramNewX1; |
---|
| 1731 | m_paramLowX2 = 0.90*LOW_QSTEP_BETA + 0.09 * m_paramLowX2 + 0.01 * paramNewX2; |
---|
| 1732 | } |
---|
| 1733 | } |
---|
| 1734 | |
---|
| 1735 | Bool PixelBaseURQQuadraticModel::checkUpdateAvailable(Int qpReference ) |
---|
| 1736 | { |
---|
| 1737 | Double qStep = xConvertQP2QStep(qpReference); |
---|
| 1738 | |
---|
| 1739 | if (qStep > xConvertQP2QStep(MAX_QP) |
---|
| 1740 | ||qStep < xConvertQP2QStep(MIN_QP) ) |
---|
| 1741 | { |
---|
| 1742 | return false; |
---|
| 1743 | } |
---|
| 1744 | |
---|
| 1745 | return true; |
---|
| 1746 | } |
---|
| 1747 | |
---|
| 1748 | Double PixelBaseURQQuadraticModel::xConvertQP2QStep(Int qp ) |
---|
| 1749 | { |
---|
| 1750 | Int i; |
---|
| 1751 | Double qStep; |
---|
| 1752 | static const Double mapQP2QSTEP[6] = { 0.625, 0.703, 0.797, 0.891, 1.000, 1.125 }; |
---|
| 1753 | |
---|
| 1754 | qStep = mapQP2QSTEP[qp % 6]; |
---|
| 1755 | for( i=0; i<(qp/6); i++) |
---|
| 1756 | { |
---|
| 1757 | qStep *= 2; |
---|
| 1758 | } |
---|
| 1759 | |
---|
| 1760 | return qStep; |
---|
| 1761 | } |
---|
| 1762 | |
---|
| 1763 | Int PixelBaseURQQuadraticModel::xConvertQStep2QP(Double qStep ) |
---|
| 1764 | { |
---|
| 1765 | Int per = 0, rem = 0; |
---|
| 1766 | |
---|
| 1767 | if( qStep < xConvertQP2QStep(MIN_QP)) |
---|
| 1768 | { |
---|
| 1769 | return MIN_QP; |
---|
| 1770 | } |
---|
| 1771 | else if (qStep > xConvertQP2QStep(MAX_QP) ) |
---|
| 1772 | { |
---|
| 1773 | return MAX_QP; |
---|
| 1774 | } |
---|
| 1775 | |
---|
| 1776 | while( qStep > xConvertQP2QStep(5) ) |
---|
| 1777 | { |
---|
| 1778 | qStep /= 2.0; |
---|
| 1779 | per++; |
---|
| 1780 | } |
---|
| 1781 | |
---|
| 1782 | if (qStep <= 0.625) |
---|
| 1783 | { |
---|
| 1784 | rem = 0; |
---|
| 1785 | } |
---|
| 1786 | else if (qStep <= 0.703) |
---|
| 1787 | { |
---|
| 1788 | rem = 1; |
---|
| 1789 | } |
---|
| 1790 | else if (qStep <= 0.797) |
---|
| 1791 | { |
---|
| 1792 | rem = 2; |
---|
| 1793 | } |
---|
| 1794 | else if (qStep <= 0.891) |
---|
| 1795 | { |
---|
| 1796 | rem = 3; |
---|
| 1797 | } |
---|
| 1798 | else if (qStep <= 1.000) |
---|
| 1799 | { |
---|
| 1800 | rem = 4; |
---|
| 1801 | } |
---|
| 1802 | else |
---|
| 1803 | { |
---|
| 1804 | rem = 5; |
---|
| 1805 | } |
---|
| 1806 | return (per * 6 + rem); |
---|
| 1807 | } |
---|
| 1808 | |
---|
| 1809 | |
---|
| 1810 | Void TEncRateCtrl::create(Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight) |
---|
| 1811 | { |
---|
| 1812 | Int leftInHeight, leftInWidth; |
---|
| 1813 | |
---|
| 1814 | m_sourceWidthInLCU = (sourceWidth / maxCUWidth ) + (( sourceWidth % maxCUWidth ) ? 1 : 0); |
---|
| 1815 | m_sourceHeightInLCU = (sourceHeight / maxCUHeight) + (( sourceHeight % maxCUHeight) ? 1 : 0); |
---|
| 1816 | m_isLowdelay = (sizeIntraPeriod == -1) ? true : false; |
---|
| 1817 | m_prevBitrate = ( targetKbps << 10 ); // in units of 1,024 bps |
---|
| 1818 | m_currBitrate = ( targetKbps << 10 ); |
---|
| 1819 | m_frameRate = frameRate; |
---|
| 1820 | m_refFrameNum = m_isLowdelay ? (sizeGOP) : (sizeGOP>>1); |
---|
| 1821 | m_nonRefFrameNum = sizeGOP-m_refFrameNum; |
---|
| 1822 | m_sizeGOP = sizeGOP; |
---|
| 1823 | m_numOfPixels = ((sourceWidth*sourceHeight*3)>>1); |
---|
| 1824 | m_indexGOP = 0; |
---|
| 1825 | m_indexFrame = 0; |
---|
| 1826 | m_indexLCU = 0; |
---|
| 1827 | m_indexUnit = 0; |
---|
| 1828 | m_indexRefFrame = 0; |
---|
| 1829 | m_indexNonRefFrame = 0; |
---|
| 1830 | m_occupancyVB = 0; |
---|
| 1831 | m_initialOVB = 0; |
---|
| 1832 | m_targetBufLevel = 0; |
---|
| 1833 | m_initialTBL = 0; |
---|
| 1834 | m_occupancyVBInFrame = 0; |
---|
| 1835 | m_remainingBitsInGOP = (m_currBitrate*sizeGOP/m_frameRate); |
---|
| 1836 | m_remainingBitsInFrame = 0; |
---|
| 1837 | m_numUnitInFrame = m_sourceWidthInLCU*m_sourceHeightInLCU; |
---|
| 1838 | m_cMADLinearModel. initMADLinearModel(); |
---|
| 1839 | m_cPixelURQQuadraticModel.initPixelBaseQuadraticModel(); |
---|
| 1840 | |
---|
| 1841 | m_costRefAvgWeighting = 0.0; |
---|
| 1842 | m_costNonRefAvgWeighting = 0.0; |
---|
| 1843 | m_costAvgbpp = 0.0; |
---|
| 1844 | m_activeUnitLevelOn = false; |
---|
| 1845 | |
---|
| 1846 | m_pcFrameData = new FrameData [sizeGOP+1]; initFrameData(qp); |
---|
| 1847 | m_pcLCUData = new LCUData [m_numUnitInFrame]; initUnitData (qp); |
---|
| 1848 | |
---|
| 1849 | for(Int i = 0, addressUnit = 0; i < m_sourceHeightInLCU*maxCUHeight; i += maxCUHeight) |
---|
| 1850 | { |
---|
| 1851 | leftInHeight = sourceHeight - i; |
---|
| 1852 | leftInHeight = min(leftInHeight, maxCUHeight); |
---|
| 1853 | for(Int j = 0; j < m_sourceWidthInLCU*maxCUWidth; j += maxCUWidth, addressUnit++) |
---|
| 1854 | { |
---|
| 1855 | leftInWidth = sourceWidth - j; |
---|
| 1856 | leftInWidth = min(leftInWidth, maxCUWidth); |
---|
| 1857 | m_pcLCUData[addressUnit].m_widthInPixel = leftInWidth; |
---|
| 1858 | m_pcLCUData[addressUnit].m_heightInPixel= leftInHeight; |
---|
| 1859 | m_pcLCUData[addressUnit].m_pixels = ((leftInHeight*leftInWidth*3)>>1); |
---|
| 1860 | } |
---|
| 1861 | } |
---|
| 1862 | } |
---|
| 1863 | |
---|
| 1864 | Void TEncRateCtrl::destroy() |
---|
| 1865 | { |
---|
| 1866 | if(m_pcFrameData) |
---|
| 1867 | { |
---|
| 1868 | delete [] m_pcFrameData; |
---|
| 1869 | m_pcFrameData = NULL; |
---|
| 1870 | } |
---|
| 1871 | if(m_pcLCUData) |
---|
| 1872 | { |
---|
| 1873 | delete [] m_pcLCUData; |
---|
| 1874 | m_pcLCUData = NULL; |
---|
| 1875 | } |
---|
| 1876 | } |
---|
| 1877 | |
---|
| 1878 | Void TEncRateCtrl::initFrameData (Int qp) |
---|
| 1879 | { |
---|
| 1880 | for(Int i = 0 ; i <= m_sizeGOP; i++) |
---|
| 1881 | { |
---|
| 1882 | m_pcFrameData[i].m_isReferenced = false; |
---|
| 1883 | m_pcFrameData[i].m_costMAD = 0.0; |
---|
| 1884 | m_pcFrameData[i].m_bits = 0; |
---|
| 1885 | m_pcFrameData[i].m_qp = qp; |
---|
| 1886 | } |
---|
| 1887 | } |
---|
| 1888 | |
---|
| 1889 | Void TEncRateCtrl::initUnitData (Int qp) |
---|
| 1890 | { |
---|
| 1891 | for(Int i = 1 ; i < m_numUnitInFrame; i++) |
---|
| 1892 | { |
---|
| 1893 | m_pcLCUData[i].m_qp = qp; |
---|
| 1894 | m_pcLCUData[i].m_bits = 0; |
---|
| 1895 | m_pcLCUData[i].m_pixels = 0; |
---|
| 1896 | m_pcLCUData[i].m_widthInPixel = 0; |
---|
| 1897 | m_pcLCUData[i].m_heightInPixel = 0; |
---|
| 1898 | m_pcLCUData[i].m_costMAD = 0.0; |
---|
| 1899 | } |
---|
| 1900 | } |
---|
| 1901 | |
---|
| 1902 | Int TEncRateCtrl::getFrameQP(Bool isReferenced, Int POC) |
---|
| 1903 | { |
---|
| 1904 | Int numofReferenced = 0; |
---|
| 1905 | Int finalQP = 0; |
---|
| 1906 | FrameData* pcFrameData; |
---|
| 1907 | |
---|
| 1908 | m_indexPOCInGOP = (POC%m_sizeGOP) == 0 ? m_sizeGOP : (POC%m_sizeGOP); |
---|
| 1909 | pcFrameData = &m_pcFrameData[m_indexPOCInGOP]; |
---|
| 1910 | |
---|
| 1911 | if(m_indexFrame != 0) |
---|
| 1912 | { |
---|
| 1913 | if(isReferenced) |
---|
| 1914 | { |
---|
| 1915 | Double gamma = m_isLowdelay ? 0.5 : 0.25; |
---|
| 1916 | Double beta = m_isLowdelay ? 0.9 : 0.6; |
---|
| 1917 | Int numRemainingRefFrames = m_refFrameNum - m_indexRefFrame; |
---|
| 1918 | Int numRemainingNRefFrames = m_nonRefFrameNum - m_indexNonRefFrame; |
---|
| 1919 | |
---|
| 1920 | Double targetBitsOccupancy = (m_currBitrate/(Double)m_frameRate) + gamma*(m_targetBufLevel-m_occupancyVB - (m_initialOVB/(Double)m_frameRate)); |
---|
| 1921 | Double targetBitsLeftBudget = ((m_costRefAvgWeighting*m_remainingBitsInGOP)/((m_costRefAvgWeighting*numRemainingRefFrames)+(m_costNonRefAvgWeighting*numRemainingNRefFrames))); |
---|
| 1922 | |
---|
| 1923 | m_targetBits = (Int)(beta * targetBitsLeftBudget + (1-beta) * targetBitsOccupancy); |
---|
| 1924 | |
---|
| 1925 | if(m_targetBits <= 0 || m_remainingBitsInGOP <= 0) |
---|
| 1926 | { |
---|
| 1927 | finalQP = m_pcFrameData[m_indexPrevPOCInGOP].m_qp + 2; |
---|
| 1928 | } |
---|
| 1929 | else |
---|
| 1930 | { |
---|
| 1931 | Double costPredMAD = m_cMADLinearModel.getMAD(); |
---|
| 1932 | Int qpLowerBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp-2; |
---|
| 1933 | Int qpUpperBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp+2; |
---|
| 1934 | finalQP = m_cPixelURQQuadraticModel.getQP(m_pcFrameData[m_indexPrevPOCInGOP].m_qp, m_targetBits, m_numOfPixels, costPredMAD); |
---|
| 1935 | finalQP = max(qpLowerBound, min(qpUpperBound, finalQP)); |
---|
| 1936 | m_activeUnitLevelOn = true; |
---|
| 1937 | m_remainingBitsInFrame = m_targetBits; |
---|
| 1938 | m_costAvgbpp = (m_targetBits/(Double)m_numOfPixels); |
---|
| 1939 | } |
---|
| 1940 | |
---|
| 1941 | m_indexRefFrame++; |
---|
| 1942 | } |
---|
| 1943 | else |
---|
| 1944 | { |
---|
| 1945 | Int bwdQP = m_pcFrameData[m_indexPOCInGOP-1].m_qp; |
---|
| 1946 | Int fwdQP = m_pcFrameData[m_indexPOCInGOP+1].m_qp; |
---|
| 1947 | |
---|
| 1948 | if( (fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP-1].m_qp |
---|
| 1949 | ||(fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP+1].m_qp) |
---|
| 1950 | { |
---|
| 1951 | finalQP = (fwdQP+bwdQP); |
---|
| 1952 | } |
---|
| 1953 | else if(bwdQP != fwdQP) |
---|
| 1954 | { |
---|
| 1955 | finalQP = ((bwdQP+fwdQP+2)>>1); |
---|
| 1956 | } |
---|
| 1957 | else |
---|
| 1958 | { |
---|
| 1959 | finalQP = bwdQP+2; |
---|
| 1960 | } |
---|
| 1961 | m_indexNonRefFrame++; |
---|
| 1962 | } |
---|
| 1963 | } |
---|
| 1964 | else |
---|
| 1965 | { |
---|
| 1966 | Int lastQPminus2 = m_pcFrameData[0].m_qp - 2; |
---|
| 1967 | Int lastQPplus2 = m_pcFrameData[0].m_qp + 2; |
---|
| 1968 | |
---|
| 1969 | for(Int idx = 1; idx <= m_sizeGOP; idx++) |
---|
| 1970 | { |
---|
| 1971 | if(m_pcFrameData[idx].m_isReferenced) |
---|
| 1972 | { |
---|
| 1973 | finalQP += m_pcFrameData[idx].m_qp; |
---|
| 1974 | numofReferenced++; |
---|
| 1975 | } |
---|
| 1976 | } |
---|
| 1977 | |
---|
| 1978 | finalQP = (numofReferenced == 0) ? m_pcFrameData[0].m_qp : ((finalQP + (1<<(numofReferenced>>1)))/numofReferenced); |
---|
| 1979 | finalQP = max( lastQPminus2, min( lastQPplus2, finalQP)); |
---|
| 1980 | |
---|
| 1981 | Double costAvgFrameBits = m_remainingBitsInGOP/(Double)m_sizeGOP; |
---|
| 1982 | Int bufLevel = m_occupancyVB + m_initialOVB; |
---|
| 1983 | |
---|
| 1984 | if(abs(bufLevel) > costAvgFrameBits) |
---|
| 1985 | { |
---|
| 1986 | if(bufLevel < 0) |
---|
| 1987 | { |
---|
| 1988 | finalQP -= 2; |
---|
| 1989 | } |
---|
| 1990 | else |
---|
| 1991 | { |
---|
| 1992 | finalQP += 2; |
---|
| 1993 | } |
---|
| 1994 | } |
---|
| 1995 | m_indexRefFrame++; |
---|
| 1996 | } |
---|
| 1997 | finalQP = max(MIN_QP, min(MAX_QP, finalQP)); |
---|
| 1998 | |
---|
| 1999 | for(Int indexLCU = 0 ; indexLCU < m_numUnitInFrame; indexLCU++) |
---|
| 2000 | { |
---|
| 2001 | m_pcLCUData[indexLCU].m_qp = finalQP; |
---|
| 2002 | } |
---|
| 2003 | |
---|
| 2004 | pcFrameData->m_isReferenced = isReferenced; |
---|
| 2005 | pcFrameData->m_qp = finalQP; |
---|
| 2006 | |
---|
| 2007 | return finalQP; |
---|
| 2008 | } |
---|
| 2009 | |
---|
| 2010 | Bool TEncRateCtrl::calculateUnitQP () |
---|
| 2011 | { |
---|
| 2012 | if(!m_activeUnitLevelOn || m_indexLCU == 0) |
---|
| 2013 | { |
---|
| 2014 | return false; |
---|
| 2015 | } |
---|
| 2016 | Int upperQPBound, lowerQPBound, finalQP; |
---|
| 2017 | Int colQP = m_pcLCUData[m_indexLCU].m_qp; |
---|
| 2018 | Double colMAD = m_pcLCUData[m_indexLCU].m_costMAD; |
---|
| 2019 | Double budgetInUnit = m_pcLCUData[m_indexLCU].m_pixels*m_costAvgbpp; |
---|
| 2020 | |
---|
| 2021 | |
---|
| 2022 | Int targetBitsOccupancy = (Int)(budgetInUnit - (m_occupancyVBInFrame/(m_numUnitInFrame-m_indexUnit))); |
---|
| 2023 | Int targetBitsLeftBudget= (Int)((m_remainingBitsInFrame*m_pcLCUData[m_indexLCU].m_pixels)/(Double)(m_numOfPixels-m_codedPixels)); |
---|
| 2024 | Int targetBits = (targetBitsLeftBudget>>1) + (targetBitsOccupancy>>1); |
---|
| 2025 | |
---|
| 2026 | |
---|
| 2027 | if( m_indexLCU >= m_sourceWidthInLCU) |
---|
| 2028 | { |
---|
| 2029 | upperQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) + MAX_DELTA_QP; |
---|
| 2030 | lowerQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) - MAX_DELTA_QP; |
---|
| 2031 | } |
---|
| 2032 | else |
---|
| 2033 | { |
---|
| 2034 | upperQPBound = m_pcLCUData[m_indexLCU-1].m_qp + MAX_DELTA_QP; |
---|
| 2035 | lowerQPBound = m_pcLCUData[m_indexLCU-1].m_qp - MAX_DELTA_QP; |
---|
| 2036 | } |
---|
| 2037 | |
---|
| 2038 | if(targetBits < 0) |
---|
| 2039 | { |
---|
| 2040 | finalQP = m_pcLCUData[m_indexLCU-1].m_qp + 1; |
---|
| 2041 | } |
---|
| 2042 | else |
---|
| 2043 | { |
---|
| 2044 | finalQP = m_cPixelURQQuadraticModel.getQP(colQP, targetBits, m_pcLCUData[m_indexLCU].m_pixels, colMAD); |
---|
| 2045 | } |
---|
| 2046 | |
---|
| 2047 | finalQP = max(lowerQPBound, min(upperQPBound, finalQP)); |
---|
| 2048 | m_pcLCUData[m_indexLCU].m_qp = max(MIN_QP, min(MAX_QP, finalQP)); |
---|
| 2049 | |
---|
| 2050 | return true; |
---|
| 2051 | } |
---|
| 2052 | |
---|
| 2053 | Void TEncRateCtrl::updateRCGOPStatus() |
---|
| 2054 | { |
---|
| 2055 | m_remainingBitsInGOP = ((m_currBitrate/m_frameRate)*m_sizeGOP) - m_occupancyVB; |
---|
| 2056 | |
---|
| 2057 | FrameData cFrameData = m_pcFrameData[m_sizeGOP]; |
---|
| 2058 | initFrameData(); |
---|
| 2059 | |
---|
| 2060 | m_pcFrameData[0] = cFrameData; |
---|
| 2061 | m_indexGOP++; |
---|
| 2062 | m_indexFrame = 0; |
---|
| 2063 | m_indexRefFrame = 0; |
---|
| 2064 | m_indexNonRefFrame = 0; |
---|
| 2065 | } |
---|
| 2066 | |
---|
| 2067 | Void TEncRateCtrl::updataRCFrameStatus(Int frameBits, SliceType eSliceType) |
---|
| 2068 | { |
---|
| 2069 | FrameData* pcFrameData = &m_pcFrameData[m_indexPOCInGOP]; |
---|
| 2070 | Int occupancyBits; |
---|
| 2071 | Double adjustmentBits; |
---|
| 2072 | |
---|
| 2073 | m_remainingBitsInGOP = m_remainingBitsInGOP + ( ((m_currBitrate-m_prevBitrate)/m_frameRate)*(m_sizeGOP-m_indexFrame) ) - frameBits; |
---|
| 2074 | occupancyBits = (Int)((Double)frameBits - (m_currBitrate/(Double)m_frameRate)); |
---|
| 2075 | |
---|
| 2076 | if( (occupancyBits < 0) && (m_initialOVB > 0) ) |
---|
| 2077 | { |
---|
| 2078 | adjustmentBits = xAdjustmentBits(occupancyBits, m_initialOVB ); |
---|
| 2079 | |
---|
| 2080 | if(m_initialOVB < 0) |
---|
| 2081 | { |
---|
| 2082 | adjustmentBits = m_initialOVB; |
---|
| 2083 | occupancyBits += (Int)adjustmentBits; |
---|
| 2084 | m_initialOVB = 0; |
---|
| 2085 | } |
---|
| 2086 | } |
---|
| 2087 | else if( (occupancyBits > 0) && (m_initialOVB < 0) ) |
---|
| 2088 | { |
---|
| 2089 | adjustmentBits = xAdjustmentBits(m_initialOVB, occupancyBits ); |
---|
| 2090 | |
---|
| 2091 | if(occupancyBits < 0) |
---|
| 2092 | { |
---|
| 2093 | adjustmentBits = occupancyBits; |
---|
| 2094 | m_initialOVB += (Int)adjustmentBits; |
---|
| 2095 | occupancyBits = 0; |
---|
| 2096 | } |
---|
| 2097 | } |
---|
| 2098 | |
---|
| 2099 | if(m_indexGOP == 0) |
---|
| 2100 | { |
---|
| 2101 | m_initialOVB = occupancyBits; |
---|
| 2102 | } |
---|
| 2103 | else |
---|
| 2104 | { |
---|
| 2105 | m_occupancyVB= m_occupancyVB + occupancyBits; |
---|
| 2106 | } |
---|
| 2107 | |
---|
| 2108 | if(pcFrameData->m_isReferenced) |
---|
| 2109 | { |
---|
| 2110 | m_costRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costRefAvgWeighting)/8.0); |
---|
| 2111 | |
---|
| 2112 | if(m_indexFrame == 0) |
---|
| 2113 | { |
---|
| 2114 | m_initialTBL = m_targetBufLevel = (frameBits - (m_currBitrate/m_frameRate)); |
---|
| 2115 | } |
---|
| 2116 | else |
---|
| 2117 | { |
---|
| 2118 | Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1; |
---|
| 2119 | m_targetBufLevel = m_targetBufLevel |
---|
| 2120 | - (m_initialTBL/(m_refFrameNum-1)) |
---|
| 2121 | + (Int)((m_costRefAvgWeighting*(distance+1)*m_currBitrate)/(m_frameRate*(m_costRefAvgWeighting+(m_costNonRefAvgWeighting*distance)))) |
---|
| 2122 | - (m_currBitrate/m_frameRate); |
---|
| 2123 | } |
---|
| 2124 | |
---|
| 2125 | if(m_cMADLinearModel.IsUpdateAvailable()) |
---|
| 2126 | { |
---|
| 2127 | m_cMADLinearModel.updateMADLiearModel(); |
---|
| 2128 | } |
---|
| 2129 | |
---|
| 2130 | if(eSliceType != I_SLICE && |
---|
| 2131 | m_cPixelURQQuadraticModel.checkUpdateAvailable(pcFrameData->m_qp)) |
---|
| 2132 | { |
---|
| 2133 | m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(pcFrameData->m_qp, pcFrameData->m_bits, m_numOfPixels, pcFrameData->m_costMAD); |
---|
| 2134 | } |
---|
| 2135 | } |
---|
| 2136 | else |
---|
| 2137 | { |
---|
| 2138 | m_costNonRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costNonRefAvgWeighting)/8.0); |
---|
| 2139 | } |
---|
| 2140 | |
---|
| 2141 | m_indexFrame++; |
---|
| 2142 | m_indexLCU = 0; |
---|
| 2143 | m_indexUnit = 0; |
---|
| 2144 | m_occupancyVBInFrame = 0; |
---|
| 2145 | m_remainingBitsInFrame = 0; |
---|
| 2146 | m_codedPixels = 0; |
---|
| 2147 | m_activeUnitLevelOn = false; |
---|
| 2148 | m_costAvgbpp = 0.0; |
---|
| 2149 | } |
---|
| 2150 | Void TEncRateCtrl::updataRCUnitStatus () |
---|
| 2151 | { |
---|
| 2152 | if(!m_activeUnitLevelOn || m_indexLCU == 0) |
---|
| 2153 | { |
---|
| 2154 | return; |
---|
| 2155 | } |
---|
| 2156 | |
---|
| 2157 | m_codedPixels += m_pcLCUData[m_indexLCU-1].m_pixels; |
---|
| 2158 | m_remainingBitsInFrame = m_remainingBitsInFrame - m_pcLCUData[m_indexLCU-1].m_bits; |
---|
| 2159 | m_occupancyVBInFrame = (Int)(m_occupancyVBInFrame + m_pcLCUData[m_indexLCU-1].m_bits - m_pcLCUData[m_indexLCU-1].m_pixels*m_costAvgbpp); |
---|
| 2160 | |
---|
| 2161 | if( m_cPixelURQQuadraticModel.checkUpdateAvailable(m_pcLCUData[m_indexLCU-1].m_qp) ) |
---|
| 2162 | { |
---|
| 2163 | m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(m_pcLCUData[m_indexLCU-1].m_qp, m_pcLCUData[m_indexLCU-1].m_bits, m_pcLCUData[m_indexLCU-1].m_pixels, m_pcLCUData[m_indexLCU-1].m_costMAD); |
---|
| 2164 | } |
---|
| 2165 | |
---|
| 2166 | m_indexUnit++; |
---|
| 2167 | } |
---|
| 2168 | |
---|
| 2169 | Void TEncRateCtrl::updateFrameData(UInt64 actualFrameBits) |
---|
| 2170 | { |
---|
| 2171 | Double costMAD = 0.0; |
---|
| 2172 | |
---|
| 2173 | for(Int i = 0; i < m_numUnitInFrame; i++) |
---|
| 2174 | { |
---|
| 2175 | costMAD += m_pcLCUData[i].m_costMAD; |
---|
| 2176 | } |
---|
| 2177 | |
---|
| 2178 | m_pcFrameData[m_indexPOCInGOP].m_costMAD = (costMAD/(Double)m_numUnitInFrame); |
---|
| 2179 | m_pcFrameData[m_indexPOCInGOP].m_bits = (Int)actualFrameBits; |
---|
| 2180 | |
---|
| 2181 | if(m_pcFrameData[m_indexPOCInGOP].m_isReferenced) |
---|
| 2182 | { |
---|
| 2183 | m_indexPrevPOCInGOP = m_indexPOCInGOP; |
---|
| 2184 | m_cMADLinearModel.updateMADHistory(m_pcFrameData[m_indexPOCInGOP].m_costMAD); |
---|
| 2185 | } |
---|
| 2186 | } |
---|
| 2187 | |
---|
| 2188 | Void TEncRateCtrl::updateLCUData(TComDataCU* pcCU, UInt64 actualLCUBits, Int qp) |
---|
| 2189 | { |
---|
| 2190 | Int x, y; |
---|
| 2191 | Double costMAD = 0.0; |
---|
| 2192 | |
---|
| 2193 | Pel* pOrg = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
| 2194 | Pel* pRec = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0); |
---|
| 2195 | Int stride = pcCU->getPic()->getStride(); |
---|
| 2196 | |
---|
| 2197 | Int width = m_pcLCUData[m_indexLCU].m_widthInPixel; |
---|
| 2198 | Int height = m_pcLCUData[m_indexLCU].m_heightInPixel; |
---|
| 2199 | |
---|
| 2200 | for( y = 0; y < height; y++ ) |
---|
| 2201 | { |
---|
| 2202 | for( x = 0; x < width; x++ ) |
---|
| 2203 | { |
---|
| 2204 | costMAD += abs( pOrg[x] - pRec[x] ); |
---|
| 2205 | } |
---|
| 2206 | pOrg += stride; |
---|
| 2207 | pRec += stride; |
---|
| 2208 | } |
---|
| 2209 | m_pcLCUData[m_indexLCU ].m_qp = qp; |
---|
| 2210 | m_pcLCUData[m_indexLCU ].m_costMAD = (costMAD /(Double)(width*height)); |
---|
| 2211 | m_pcLCUData[m_indexLCU++].m_bits = (Int)actualLCUBits; |
---|
| 2212 | } |
---|
| 2213 | |
---|
| 2214 | Double TEncRateCtrl::xAdjustmentBits(Int& reductionBits, Int& compensationBits) |
---|
| 2215 | { |
---|
| 2216 | Double adjustment = ADJUSTMENT_FACTOR*reductionBits; |
---|
| 2217 | reductionBits -= (Int)adjustment; |
---|
| 2218 | compensationBits += (Int)adjustment; |
---|
| 2219 | |
---|
| 2220 | return adjustment; |
---|
| 2221 | } |
---|
| 2222 | |
---|
| 2223 | #endif |
---|
| 2224 | |
---|