| 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; |
|---|
| 69 | #if M0036_RC_IMPROVEMENT |
|---|
| 70 | m_adaptiveBit = 0; |
|---|
| 71 | m_lastLambda = 0.0; |
|---|
| 72 | #endif |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | TEncRCSeq::~TEncRCSeq() |
|---|
| 76 | { |
|---|
| 77 | destroy(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 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 |
|---|
| 83 | Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel ) |
|---|
| 84 | #endif |
|---|
| 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 | } |
|---|
| 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 | } |
|---|
| 122 | else |
|---|
| 123 | { |
|---|
| 124 | m_alphaUpdate = 0.4; |
|---|
| 125 | m_betaUpdate = 0.2; |
|---|
| 126 | } |
|---|
| 127 | #else |
|---|
| 128 | else |
|---|
| 129 | { |
|---|
| 130 | m_alphaUpdate = 0.1; |
|---|
| 131 | m_betaUpdate = 0.05; |
|---|
| 132 | } |
|---|
| 133 | #endif |
|---|
| 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; |
|---|
| 174 | #if M0036_RC_IMPROVEMENT |
|---|
| 175 | m_adaptiveBit = adaptiveBit; |
|---|
| 176 | m_lastLambda = 0.0; |
|---|
| 177 | #endif |
|---|
| 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 | { |
|---|
| 235 | #if RATE_CONTROL_INTRA |
|---|
| 236 | if (i>0) |
|---|
| 237 | { |
|---|
| 238 | m_picPara[i].m_alpha = 3.2003; |
|---|
| 239 | m_picPara[i].m_beta = -1.367; |
|---|
| 240 | } |
|---|
| 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 | } |
|---|
| 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 | { |
|---|
| 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 |
|---|
| 277 | m_LCUPara[i][j].m_alpha = 3.2003; |
|---|
| 278 | m_LCUPara[i][j].m_beta = -1.367; |
|---|
| 279 | #endif |
|---|
| 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 | |
|---|
| 301 | #if !RATE_CONTROL_INTRA |
|---|
| 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 | } |
|---|
| 315 | #endif |
|---|
| 316 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 ); |
|---|
| 424 | #if M0036_RC_IMPROVEMENT |
|---|
| 425 | m_picTargetBitInGOP[i] = (Int)( ((Double)targetBits) * currPicRatio / totalPicRatio ); |
|---|
| 426 | #else |
|---|
| 427 | m_picTargetBitInGOP[i] = targetBits * currPicRatio / totalPicRatio; |
|---|
| 428 | #endif |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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; |
|---|
| 536 | #if !M0036_RC_IMPROVEMENT |
|---|
| 537 | m_lastPicture = NULL; |
|---|
| 538 | #endif |
|---|
| 539 | |
|---|
| 540 | #if KWU_RC_MADPRED_E0227 |
|---|
| 541 | m_lastIVPicture = NULL; |
|---|
| 542 | #endif |
|---|
| 543 | |
|---|
| 544 | m_picActualHeaderBits = 0; |
|---|
| 545 | #if !M0036_RC_IMPROVEMENT |
|---|
| 546 | m_totalMAD = 0.0; |
|---|
| 547 | #endif |
|---|
| 548 | m_picActualBits = 0; |
|---|
| 549 | m_picQP = 0; |
|---|
| 550 | m_picLambda = 0.0; |
|---|
| 551 | |
|---|
| 552 | #if KWU_RC_MADPRED_E0227 |
|---|
| 553 | m_IVtotalMAD = 0.0; |
|---|
| 554 | #endif |
|---|
| 555 | } |
|---|
| 556 | |
|---|
| 557 | TEncRCPic::~TEncRCPic() |
|---|
| 558 | { |
|---|
| 559 | destroy(); |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | Int TEncRCPic::xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ) |
|---|
| 563 | { |
|---|
| 564 | Int targetBits = 0; |
|---|
| 565 | Int GOPbitsLeft = encRCGOP->getBitsLeft(); |
|---|
| 566 | |
|---|
| 567 | Int i; |
|---|
| 568 | Int currPicPosition = encRCGOP->getNumPic()-encRCGOP->getPicLeft(); |
|---|
| 569 | Int currPicRatio = encRCSeq->getBitRatio( currPicPosition ); |
|---|
| 570 | Int totalPicRatio = 0; |
|---|
| 571 | for ( i=currPicPosition; i<encRCGOP->getNumPic(); i++ ) |
|---|
| 572 | { |
|---|
| 573 | totalPicRatio += encRCSeq->getBitRatio( i ); |
|---|
| 574 | } |
|---|
| 575 | |
|---|
| 576 | #if M0036_RC_IMPROVEMENT |
|---|
| 577 | targetBits = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio ); |
|---|
| 578 | #else |
|---|
| 579 | targetBits = Int( GOPbitsLeft * currPicRatio / totalPicRatio ); |
|---|
| 580 | #endif |
|---|
| 581 | |
|---|
| 582 | if ( targetBits < 100 ) |
|---|
| 583 | { |
|---|
| 584 | targetBits = 100; // at least allocate 100 bits for one picture |
|---|
| 585 | } |
|---|
| 586 | |
|---|
| 587 | if ( m_encRCSeq->getFramesLeft() > 16 ) |
|---|
| 588 | { |
|---|
| 589 | targetBits = Int( g_RCWeightPicRargetBitInBuffer * targetBits + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP( currPicPosition ) ); |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | return targetBits; |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | Int TEncRCPic::xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ) |
|---|
| 596 | { |
|---|
| 597 | Int numPreviousPics = 0; |
|---|
| 598 | Int totalPreviousBits = 0; |
|---|
| 599 | |
|---|
| 600 | list<TEncRCPic*>::iterator it; |
|---|
| 601 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
|---|
| 602 | { |
|---|
| 603 | if ( (*it)->getFrameLevel() == frameLevel ) |
|---|
| 604 | { |
|---|
| 605 | totalPreviousBits += (*it)->getPicActualHeaderBits(); |
|---|
| 606 | numPreviousPics++; |
|---|
| 607 | } |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | Int estHeaderBits = 0; |
|---|
| 611 | if ( numPreviousPics > 0 ) |
|---|
| 612 | { |
|---|
| 613 | estHeaderBits = totalPreviousBits / numPreviousPics; |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | return estHeaderBits; |
|---|
| 617 | } |
|---|
| 618 | |
|---|
| 619 | Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ) |
|---|
| 620 | { |
|---|
| 621 | if ( listPreviousPictures.size() > g_RCMaxPicListSize ) |
|---|
| 622 | { |
|---|
| 623 | TEncRCPic* p = listPreviousPictures.front(); |
|---|
| 624 | listPreviousPictures.pop_front(); |
|---|
| 625 | p->destroy(); |
|---|
| 626 | delete p; |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | listPreviousPictures.push_back( this ); |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | #if KWU_RC_MADPRED_E0227 |
|---|
| 633 | Void TEncRCPic::addToPictureLsitIV( list<TEncRCPic*>& listPreviousPictures ) |
|---|
| 634 | { |
|---|
| 635 | m_lastIVPicture = NULL; |
|---|
| 636 | m_lastIVPicture = this; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | Void TEncRCPic::setIVPic( TEncRCPic* BaseRCPic ) |
|---|
| 640 | { |
|---|
| 641 | m_lastIVPicture = BaseRCPic; |
|---|
| 642 | } |
|---|
| 643 | #endif |
|---|
| 644 | |
|---|
| 645 | #if KWU_RC_MADPRED_E0227 |
|---|
| 646 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures, Int LayerID ) |
|---|
| 647 | #else |
|---|
| 648 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ) |
|---|
| 649 | #endif |
|---|
| 650 | { |
|---|
| 651 | destroy(); |
|---|
| 652 | m_encRCSeq = encRCSeq; |
|---|
| 653 | m_encRCGOP = encRCGOP; |
|---|
| 654 | |
|---|
| 655 | Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP ); |
|---|
| 656 | Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel ); |
|---|
| 657 | |
|---|
| 658 | if ( targetBits < estHeaderBits + 100 ) |
|---|
| 659 | { |
|---|
| 660 | targetBits = estHeaderBits + 100; // at least allocate 100 bits for picture data |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | m_frameLevel = frameLevel; |
|---|
| 664 | m_numberOfPixel = encRCSeq->getNumPixel(); |
|---|
| 665 | m_numberOfLCU = encRCSeq->getNumberOfLCU(); |
|---|
| 666 | m_estPicLambda = 100.0; |
|---|
| 667 | m_targetBits = targetBits; |
|---|
| 668 | m_estHeaderBits = estHeaderBits; |
|---|
| 669 | m_bitsLeft = m_targetBits; |
|---|
| 670 | Int picWidth = encRCSeq->getPicWidth(); |
|---|
| 671 | Int picHeight = encRCSeq->getPicHeight(); |
|---|
| 672 | Int LCUWidth = encRCSeq->getLCUWidth(); |
|---|
| 673 | Int LCUHeight = encRCSeq->getLCUHeight(); |
|---|
| 674 | Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1; |
|---|
| 675 | Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1; |
|---|
| 676 | |
|---|
| 677 | m_LCULeft = m_numberOfLCU; |
|---|
| 678 | m_bitsLeft -= m_estHeaderBits; |
|---|
| 679 | m_pixelsLeft = m_numberOfPixel; |
|---|
| 680 | |
|---|
| 681 | m_LCUs = new TRCLCU[m_numberOfLCU]; |
|---|
| 682 | Int i, j; |
|---|
| 683 | Int LCUIdx; |
|---|
| 684 | for ( i=0; i<picWidthInLCU; i++ ) |
|---|
| 685 | { |
|---|
| 686 | for ( j=0; j<picHeightInLCU; j++ ) |
|---|
| 687 | { |
|---|
| 688 | LCUIdx = j*picWidthInLCU + i; |
|---|
| 689 | m_LCUs[LCUIdx].m_actualBits = 0; |
|---|
| 690 | m_LCUs[LCUIdx].m_QP = 0; |
|---|
| 691 | m_LCUs[LCUIdx].m_lambda = 0.0; |
|---|
| 692 | m_LCUs[LCUIdx].m_targetBits = 0; |
|---|
| 693 | #if M0036_RC_IMPROVEMENT |
|---|
| 694 | m_LCUs[LCUIdx].m_bitWeight = 1.0; |
|---|
| 695 | #else |
|---|
| 696 | m_LCUs[LCUIdx].m_MAD = 0.0; |
|---|
| 697 | #endif |
|---|
| 698 | Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); |
|---|
| 699 | Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); |
|---|
| 700 | m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight; |
|---|
| 701 | |
|---|
| 702 | #if KWU_RC_MADPRED_E0227 |
|---|
| 703 | m_LCUs[LCUIdx].m_CUWidth = currWidth; |
|---|
| 704 | m_LCUs[LCUIdx].m_CUHeight = currHeight; |
|---|
| 705 | m_LCUs[LCUIdx].m_IVMAD = -1.0; |
|---|
| 706 | #endif |
|---|
| 707 | } |
|---|
| 708 | } |
|---|
| 709 | m_picActualHeaderBits = 0; |
|---|
| 710 | #if !M0036_RC_IMPROVEMENT |
|---|
| 711 | m_totalMAD = 0.0; |
|---|
| 712 | #endif |
|---|
| 713 | m_picActualBits = 0; |
|---|
| 714 | m_picQP = 0; |
|---|
| 715 | m_picLambda = 0.0; |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | #if KWU_RC_MADPRED_E0227 |
|---|
| 719 | m_LayerID = LayerID; |
|---|
| 720 | m_lastIVPicture = NULL; |
|---|
| 721 | m_IVtotalMAD = 0.0; |
|---|
| 722 | #endif |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | #if !M0036_RC_IMPROVEMENT |
|---|
| 726 | m_lastPicture = NULL; |
|---|
| 727 | list<TEncRCPic*>::reverse_iterator it; |
|---|
| 728 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
|---|
| 729 | { |
|---|
| 730 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
|---|
| 731 | { |
|---|
| 732 | m_lastPicture = (*it); |
|---|
| 733 | break; |
|---|
| 734 | } |
|---|
| 735 | } |
|---|
| 736 | #endif |
|---|
| 737 | |
|---|
| 738 | #if KWU_RC_MADPRED_E0227 |
|---|
| 739 | list<TEncRCPic*>::reverse_iterator it; |
|---|
| 740 | if( m_LayerID != 0) |
|---|
| 741 | { |
|---|
| 742 | m_lastIVPicture = NULL; |
|---|
| 743 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
|---|
| 744 | { |
|---|
| 745 | if ( (*it)->getLayerID() == 0 ) |
|---|
| 746 | { |
|---|
| 747 | m_lastIVPicture = (*it); |
|---|
| 748 | break; |
|---|
| 749 | } |
|---|
| 750 | } |
|---|
| 751 | } |
|---|
| 752 | |
|---|
| 753 | m_lastPicture = NULL; |
|---|
| 754 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
|---|
| 755 | { |
|---|
| 756 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
|---|
| 757 | { |
|---|
| 758 | m_lastPicture = (*it); |
|---|
| 759 | break; |
|---|
| 760 | } |
|---|
| 761 | } |
|---|
| 762 | #endif |
|---|
| 763 | } |
|---|
| 764 | |
|---|
| 765 | Void TEncRCPic::destroy() |
|---|
| 766 | { |
|---|
| 767 | if( m_LCUs != NULL ) |
|---|
| 768 | { |
|---|
| 769 | delete[] m_LCUs; |
|---|
| 770 | m_LCUs = NULL; |
|---|
| 771 | } |
|---|
| 772 | m_encRCSeq = NULL; |
|---|
| 773 | m_encRCGOP = NULL; |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | |
|---|
| 777 | #if RATE_CONTROL_INTRA |
|---|
| 778 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType) |
|---|
| 779 | #else |
|---|
| 780 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures ) |
|---|
| 781 | #endif |
|---|
| 782 | { |
|---|
| 783 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
|---|
| 784 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
|---|
| 785 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
|---|
| 786 | #if RATE_CONTROL_INTRA |
|---|
| 787 | Double estLambda; |
|---|
| 788 | if (eSliceType == I_SLICE) |
|---|
| 789 | { |
|---|
| 790 | estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp); |
|---|
| 791 | } |
|---|
| 792 | else |
|---|
| 793 | { |
|---|
| 794 | estLambda = alpha * pow( bpp, beta ); |
|---|
| 795 | } |
|---|
| 796 | #else |
|---|
| 797 | Double estLambda = alpha * pow( bpp, beta ); |
|---|
| 798 | #endif |
|---|
| 799 | |
|---|
| 800 | Double lastLevelLambda = -1.0; |
|---|
| 801 | Double lastPicLambda = -1.0; |
|---|
| 802 | Double lastValidLambda = -1.0; |
|---|
| 803 | list<TEncRCPic*>::iterator it; |
|---|
| 804 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
|---|
| 805 | { |
|---|
| 806 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
|---|
| 807 | { |
|---|
| 808 | lastLevelLambda = (*it)->getPicActualLambda(); |
|---|
| 809 | } |
|---|
| 810 | lastPicLambda = (*it)->getPicActualLambda(); |
|---|
| 811 | |
|---|
| 812 | if ( lastPicLambda > 0.0 ) |
|---|
| 813 | { |
|---|
| 814 | lastValidLambda = lastPicLambda; |
|---|
| 815 | } |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | if ( lastLevelLambda > 0.0 ) |
|---|
| 819 | { |
|---|
| 820 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
|---|
| 821 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | if ( lastPicLambda > 0.0 ) |
|---|
| 825 | { |
|---|
| 826 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
|---|
| 827 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
|---|
| 828 | } |
|---|
| 829 | else if ( lastValidLambda > 0.0 ) |
|---|
| 830 | { |
|---|
| 831 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
|---|
| 832 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
|---|
| 833 | } |
|---|
| 834 | else |
|---|
| 835 | { |
|---|
| 836 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| 839 | if ( estLambda < 0.1 ) |
|---|
| 840 | { |
|---|
| 841 | estLambda = 0.1; |
|---|
| 842 | } |
|---|
| 843 | |
|---|
| 844 | m_estPicLambda = estLambda; |
|---|
| 845 | |
|---|
| 846 | #if M0036_RC_IMPROVEMENT |
|---|
| 847 | Double totalWeight = 0.0; |
|---|
| 848 | // initial BU bit allocation weight |
|---|
| 849 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
|---|
| 850 | { |
|---|
| 851 | #if RC_FIX |
|---|
| 852 | Double alphaLCU, betaLCU; |
|---|
| 853 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
|---|
| 854 | { |
|---|
| 855 | alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
|---|
| 856 | betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
|---|
| 857 | } |
|---|
| 858 | else |
|---|
| 859 | { |
|---|
| 860 | alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
|---|
| 861 | betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
|---|
| 862 | } |
|---|
| 863 | #else |
|---|
| 864 | Double alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
|---|
| 865 | Double betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
|---|
| 866 | #endif |
|---|
| 867 | |
|---|
| 868 | m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU ); |
|---|
| 869 | |
|---|
| 870 | if ( m_LCUs[i].m_bitWeight < 0.01 ) |
|---|
| 871 | { |
|---|
| 872 | m_LCUs[i].m_bitWeight = 0.01; |
|---|
| 873 | } |
|---|
| 874 | totalWeight += m_LCUs[i].m_bitWeight; |
|---|
| 875 | } |
|---|
| 876 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
|---|
| 877 | { |
|---|
| 878 | Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight; |
|---|
| 879 | m_LCUs[i].m_bitWeight = BUTargetBits; |
|---|
| 880 | } |
|---|
| 881 | #endif |
|---|
| 882 | |
|---|
| 883 | return estLambda; |
|---|
| 884 | } |
|---|
| 885 | |
|---|
| 886 | Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures ) |
|---|
| 887 | { |
|---|
| 888 | Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
|---|
| 889 | |
|---|
| 890 | Int lastLevelQP = g_RCInvalidQPValue; |
|---|
| 891 | Int lastPicQP = g_RCInvalidQPValue; |
|---|
| 892 | Int lastValidQP = g_RCInvalidQPValue; |
|---|
| 893 | list<TEncRCPic*>::iterator it; |
|---|
| 894 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
|---|
| 895 | { |
|---|
| 896 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
|---|
| 897 | { |
|---|
| 898 | lastLevelQP = (*it)->getPicActualQP(); |
|---|
| 899 | } |
|---|
| 900 | lastPicQP = (*it)->getPicActualQP(); |
|---|
| 901 | if ( lastPicQP > g_RCInvalidQPValue ) |
|---|
| 902 | { |
|---|
| 903 | lastValidQP = lastPicQP; |
|---|
| 904 | } |
|---|
| 905 | } |
|---|
| 906 | |
|---|
| 907 | if ( lastLevelQP > g_RCInvalidQPValue ) |
|---|
| 908 | { |
|---|
| 909 | QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP ); |
|---|
| 910 | } |
|---|
| 911 | |
|---|
| 912 | if( lastPicQP > g_RCInvalidQPValue ) |
|---|
| 913 | { |
|---|
| 914 | QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP ); |
|---|
| 915 | } |
|---|
| 916 | else if( lastValidQP > g_RCInvalidQPValue ) |
|---|
| 917 | { |
|---|
| 918 | QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP ); |
|---|
| 919 | } |
|---|
| 920 | |
|---|
| 921 | return QP; |
|---|
| 922 | } |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | #if KWU_RC_MADPRED_E0227 |
|---|
| 926 | Double TEncRCPic::estimatePicLambdaIV( list<TEncRCPic*>& listPreviousPictures, Int CurPOC ) |
|---|
| 927 | { |
|---|
| 928 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
|---|
| 929 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
|---|
| 930 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
|---|
| 931 | Double estLambda = alpha * pow( bpp, beta ); |
|---|
| 932 | Double lastLevelLambda = -1.0; |
|---|
| 933 | Double lastPicLambda = -1.0; |
|---|
| 934 | Double lastValidLambda = -1.0; |
|---|
| 935 | list<TEncRCPic*>::iterator it; |
|---|
| 936 | |
|---|
| 937 | if(listPreviousPictures.size() == 0 || CurPOC%8 == 0) |
|---|
| 938 | { |
|---|
| 939 | lastLevelLambda = m_lastIVPicture->getPicActualLambda(); |
|---|
| 940 | lastPicLambda = m_lastIVPicture->getPicActualLambda(); |
|---|
| 941 | } |
|---|
| 942 | else |
|---|
| 943 | { |
|---|
| 944 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
|---|
| 945 | { |
|---|
| 946 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
|---|
| 947 | { |
|---|
| 948 | lastLevelLambda = (*it)->getPicActualLambda(); |
|---|
| 949 | } |
|---|
| 950 | lastPicLambda = (*it)->getPicActualLambda(); |
|---|
| 951 | |
|---|
| 952 | if ( lastPicLambda > 0.0 ) |
|---|
| 953 | { |
|---|
| 954 | lastValidLambda = lastPicLambda; |
|---|
| 955 | } |
|---|
| 956 | } |
|---|
| 957 | } |
|---|
| 958 | |
|---|
| 959 | if ( lastLevelLambda > 0.0 ) |
|---|
| 960 | { |
|---|
| 961 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
|---|
| 962 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
|---|
| 963 | } |
|---|
| 964 | |
|---|
| 965 | if ( lastPicLambda > 0.0 ) |
|---|
| 966 | { |
|---|
| 967 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
|---|
| 968 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
|---|
| 969 | } |
|---|
| 970 | else if ( lastValidLambda > 0.0 ) |
|---|
| 971 | { |
|---|
| 972 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
|---|
| 973 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
|---|
| 974 | } |
|---|
| 975 | else |
|---|
| 976 | { |
|---|
| 977 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
|---|
| 978 | } |
|---|
| 979 | |
|---|
| 980 | if ( estLambda < 0.1 ) |
|---|
| 981 | { |
|---|
| 982 | estLambda = 0.1; |
|---|
| 983 | } |
|---|
| 984 | |
|---|
| 985 | m_estPicLambda = estLambda; |
|---|
| 986 | return estLambda; |
|---|
| 987 | } |
|---|
| 988 | #endif |
|---|
| 989 | |
|---|
| 990 | |
|---|
| 991 | #if RATE_CONTROL_INTRA |
|---|
| 992 | Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) |
|---|
| 993 | #else |
|---|
| 994 | Double TEncRCPic::getLCUTargetBpp() |
|---|
| 995 | #endif |
|---|
| 996 | { |
|---|
| 997 | Int LCUIdx = getLCUCoded(); |
|---|
| 998 | Double bpp = -1.0; |
|---|
| 999 | Int avgBits = 0; |
|---|
| 1000 | #if !M0036_RC_IMPROVEMENT |
|---|
| 1001 | Double totalMAD = -1.0; |
|---|
| 1002 | Double MAD = -1.0; |
|---|
| 1003 | #endif |
|---|
| 1004 | |
|---|
| 1005 | #if RATE_CONTROL_INTRA |
|---|
| 1006 | if (eSliceType == I_SLICE){ |
|---|
| 1007 | Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1; |
|---|
| 1008 | Int bitrateWindow = min(4,noOfLCUsLeft); |
|---|
| 1009 | Double MAD = getLCU(LCUIdx).m_costIntra; |
|---|
| 1010 | |
|---|
| 1011 | if (m_remainingCostIntra > 0.1 ) |
|---|
| 1012 | { |
|---|
| 1013 | Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow; |
|---|
| 1014 | avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra ); |
|---|
| 1015 | } |
|---|
| 1016 | else |
|---|
| 1017 | { |
|---|
| 1018 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
|---|
| 1019 | } |
|---|
| 1020 | m_remainingCostIntra -= MAD; |
|---|
| 1021 | } |
|---|
| 1022 | else |
|---|
| 1023 | { |
|---|
| 1024 | #endif |
|---|
| 1025 | #if M0036_RC_IMPROVEMENT |
|---|
| 1026 | Double totalWeight = 0; |
|---|
| 1027 | for ( Int i=LCUIdx; i<m_numberOfLCU; i++ ) |
|---|
| 1028 | { |
|---|
| 1029 | totalWeight += m_LCUs[i].m_bitWeight; |
|---|
| 1030 | } |
|---|
| 1031 | Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() ); |
|---|
| 1032 | avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 ); |
|---|
| 1033 | #else |
|---|
| 1034 | if ( m_lastPicture == NULL ) |
|---|
| 1035 | { |
|---|
| 1036 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
|---|
| 1037 | } |
|---|
| 1038 | else |
|---|
| 1039 | { |
|---|
| 1040 | MAD = m_lastPicture->getLCU(LCUIdx).m_MAD; |
|---|
| 1041 | totalMAD = m_lastPicture->getTotalMAD(); |
|---|
| 1042 | for ( Int i=0; i<LCUIdx; i++ ) |
|---|
| 1043 | { |
|---|
| 1044 | totalMAD -= m_lastPicture->getLCU(i).m_MAD; |
|---|
| 1045 | } |
|---|
| 1046 | |
|---|
| 1047 | if ( totalMAD > 0.1 ) |
|---|
| 1048 | { |
|---|
| 1049 | avgBits = Int( m_bitsLeft * MAD / totalMAD ); |
|---|
| 1050 | } |
|---|
| 1051 | else |
|---|
| 1052 | { |
|---|
| 1053 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
|---|
| 1054 | } |
|---|
| 1055 | } |
|---|
| 1056 | #endif |
|---|
| 1057 | #if RATE_CONTROL_INTRA |
|---|
| 1058 | } |
|---|
| 1059 | #endif |
|---|
| 1060 | |
|---|
| 1061 | if ( avgBits < 1 ) |
|---|
| 1062 | { |
|---|
| 1063 | avgBits = 1; |
|---|
| 1064 | } |
|---|
| 1065 | |
|---|
| 1066 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
|---|
| 1067 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
|---|
| 1068 | |
|---|
| 1069 | return bpp; |
|---|
| 1070 | } |
|---|
| 1071 | |
|---|
| 1072 | |
|---|
| 1073 | #if KWU_RC_MADPRED_E0227 |
|---|
| 1074 | Double TEncRCPic::getLCUTargetBppforInterView( list<TEncRCPic*>& listPreviousPictures, TComDataCU* pcCU, Double BasePos, Double CurPos, Double FocalLen, Double Znear, Double Zfar, Int Direction, Int* iDisparity ) |
|---|
| 1075 | { |
|---|
| 1076 | Int LCUIdx = getLCUCoded(); |
|---|
| 1077 | Double bpp = -1.0; |
|---|
| 1078 | Int avgBits = 0; |
|---|
| 1079 | #if !M0036_RC_IMPROVEMENT |
|---|
| 1080 | Double totalMAD = -1.0; |
|---|
| 1081 | Double MAD = -1.0; |
|---|
| 1082 | #endif |
|---|
| 1083 | |
|---|
| 1084 | Double totalMAD = -1.0; |
|---|
| 1085 | Double MAD = -1.0; |
|---|
| 1086 | |
|---|
| 1087 | Double IVMAD = -1.0; |
|---|
| 1088 | Double SAD = 0.0; |
|---|
| 1089 | Int x, y; |
|---|
| 1090 | Int Sum = 0; |
|---|
| 1091 | |
|---|
| 1092 | { |
|---|
| 1093 | Pel* pOrg = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
|---|
| 1094 | Pel* pRec = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvPred()->getLumaAddr(pcCU->getAddr(), 0); |
|---|
| 1095 | Pel* pDep = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
|---|
| 1096 | Int iStride = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getStride(); |
|---|
| 1097 | |
|---|
| 1098 | Int iWidth = m_LCUs[ LCUIdx ].m_CUWidth; |
|---|
| 1099 | Int iHeight = m_LCUs[ LCUIdx ].m_CUHeight; |
|---|
| 1100 | |
|---|
| 1101 | for( y = 0 ; y < pcCU->getSlice()->getSPS()->getMaxCUHeight() ; y+=8) |
|---|
| 1102 | { |
|---|
| 1103 | for( x = 0 ; x < pcCU->getSlice()->getSPS()->getMaxCUWidth() ; x+=8) |
|---|
| 1104 | { |
|---|
| 1105 | Sum += pDep[x]; |
|---|
| 1106 | } |
|---|
| 1107 | pDep += iStride; |
|---|
| 1108 | } |
|---|
| 1109 | |
|---|
| 1110 | Double AvgDepth = (Double)Sum/((pcCU->getSlice()->getSPS()->getMaxCUHeight()/8)*(pcCU->getSlice()->getSPS()->getMaxCUWidth()/8)); |
|---|
| 1111 | |
|---|
| 1112 | //Int iDisparity = pDepthGen->RCGetDisparityFromVirtDepth(0, (Int)AvgDepth); |
|---|
| 1113 | |
|---|
| 1114 | Double dFL = FocalLen * abs( BasePos - CurPos ); |
|---|
| 1115 | Double dZ = abs( 1.0 / Znear - 1.0 / Zfar ) * ((Double)(AvgDepth) / (( 1 << g_bitDepthY ) - 1) ) + abs(1.0 / Zfar); |
|---|
| 1116 | *iDisparity = (Int)(Direction*dFL * dZ); |
|---|
| 1117 | Int shift = DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8); |
|---|
| 1118 | |
|---|
| 1119 | Int Disp = *iDisparity; |
|---|
| 1120 | Int PosX, PosY; |
|---|
| 1121 | pcCU->getPosInPic(0, PosX, PosY); |
|---|
| 1122 | if((PosX + *iDisparity) < 0 || (PosX + *iDisparity + iWidth) >= pcCU->getSlice()->getSPS()->getMaxCUWidth()) |
|---|
| 1123 | Disp = 0; |
|---|
| 1124 | |
|---|
| 1125 | for( y = 0; y < iHeight; y++ ) |
|---|
| 1126 | { |
|---|
| 1127 | for( x = 0; x < iWidth; x++ ) |
|---|
| 1128 | { |
|---|
| 1129 | SAD += abs( pOrg[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + Disp)] |
|---|
| 1130 | - pRec[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + Disp)] )>>shift; |
|---|
| 1131 | } |
|---|
| 1132 | pOrg += iStride; |
|---|
| 1133 | pRec += iStride; |
|---|
| 1134 | } |
|---|
| 1135 | IVMAD = SAD / (Double)(iHeight * iWidth); |
|---|
| 1136 | IVMAD = IVMAD * IVMAD; |
|---|
| 1137 | |
|---|
| 1138 | m_LCUs[ LCUIdx ].m_IVMAD = IVMAD; |
|---|
| 1139 | if(m_lastPicture) |
|---|
| 1140 | m_LCUs[ LCUIdx ].m_MAD = m_lastPicture->getLCU(LCUIdx).m_MAD; |
|---|
| 1141 | |
|---|
| 1142 | MAD = m_LCUs[ LCUIdx ].m_IVMAD; |
|---|
| 1143 | |
|---|
| 1144 | if(m_lastPicture) |
|---|
| 1145 | { |
|---|
| 1146 | totalMAD = m_lastPicture->getTotalMAD(); // get total MAD of temporal frame |
|---|
| 1147 | for ( Int i=0; i<LCUIdx; i++ ) |
|---|
| 1148 | { |
|---|
| 1149 | totalMAD -= m_lastPicture->getLCU(i).m_MAD; |
|---|
| 1150 | } |
|---|
| 1151 | } |
|---|
| 1152 | else |
|---|
| 1153 | { |
|---|
| 1154 | totalMAD = m_lastIVPicture->getTotalMAD(); // get total MAD of inter-view frame |
|---|
| 1155 | for ( Int i=0; i<LCUIdx; i++ ) |
|---|
| 1156 | { |
|---|
| 1157 | totalMAD -= m_lastIVPicture->getLCU(i).m_MAD; |
|---|
| 1158 | } |
|---|
| 1159 | } |
|---|
| 1160 | |
|---|
| 1161 | |
|---|
| 1162 | if ( totalMAD > 0.1 ) |
|---|
| 1163 | { |
|---|
| 1164 | avgBits = Int( (m_bitsLeft * MAD) / totalMAD ); |
|---|
| 1165 | } |
|---|
| 1166 | else |
|---|
| 1167 | { |
|---|
| 1168 | avgBits = Int( (m_bitsLeft) / m_LCULeft ); |
|---|
| 1169 | } |
|---|
| 1170 | } |
|---|
| 1171 | |
|---|
| 1172 | if ( avgBits < 5 ) |
|---|
| 1173 | { |
|---|
| 1174 | avgBits = 5; |
|---|
| 1175 | } |
|---|
| 1176 | |
|---|
| 1177 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
|---|
| 1178 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
|---|
| 1179 | |
|---|
| 1180 | return bpp; |
|---|
| 1181 | } |
|---|
| 1182 | #endif |
|---|
| 1183 | |
|---|
| 1184 | |
|---|
| 1185 | |
|---|
| 1186 | |
|---|
| 1187 | Double TEncRCPic::getLCUEstLambda( Double bpp ) |
|---|
| 1188 | { |
|---|
| 1189 | Int LCUIdx = getLCUCoded(); |
|---|
| 1190 | Double alpha; |
|---|
| 1191 | Double beta; |
|---|
| 1192 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
|---|
| 1193 | { |
|---|
| 1194 | alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
|---|
| 1195 | beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
|---|
| 1196 | } |
|---|
| 1197 | else |
|---|
| 1198 | { |
|---|
| 1199 | alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
|---|
| 1200 | beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
|---|
| 1201 | } |
|---|
| 1202 | |
|---|
| 1203 | Double estLambda = alpha * pow( bpp, beta ); |
|---|
| 1204 | //for Lambda clip, picture level clip |
|---|
| 1205 | Double clipPicLambda = m_estPicLambda; |
|---|
| 1206 | |
|---|
| 1207 | //for Lambda clip, LCU level clip |
|---|
| 1208 | Double clipNeighbourLambda = -1.0; |
|---|
| 1209 | for ( int i=LCUIdx - 1; i>=0; i-- ) |
|---|
| 1210 | { |
|---|
| 1211 | if ( m_LCUs[i].m_lambda > 0 ) |
|---|
| 1212 | { |
|---|
| 1213 | clipNeighbourLambda = m_LCUs[i].m_lambda; |
|---|
| 1214 | break; |
|---|
| 1215 | } |
|---|
| 1216 | } |
|---|
| 1217 | |
|---|
| 1218 | if ( clipNeighbourLambda > 0.0 ) |
|---|
| 1219 | { |
|---|
| 1220 | estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda ); |
|---|
| 1221 | } |
|---|
| 1222 | |
|---|
| 1223 | if ( clipPicLambda > 0.0 ) |
|---|
| 1224 | { |
|---|
| 1225 | estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda ); |
|---|
| 1226 | } |
|---|
| 1227 | else |
|---|
| 1228 | { |
|---|
| 1229 | estLambda = Clip3( 10.0, 1000.0, estLambda ); |
|---|
| 1230 | } |
|---|
| 1231 | |
|---|
| 1232 | if ( estLambda < 0.1 ) |
|---|
| 1233 | { |
|---|
| 1234 | estLambda = 0.1; |
|---|
| 1235 | } |
|---|
| 1236 | |
|---|
| 1237 | return estLambda; |
|---|
| 1238 | } |
|---|
| 1239 | |
|---|
| 1240 | Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP ) |
|---|
| 1241 | { |
|---|
| 1242 | Int LCUIdx = getLCUCoded(); |
|---|
| 1243 | Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
|---|
| 1244 | |
|---|
| 1245 | //for Lambda clip, LCU level clip |
|---|
| 1246 | Int clipNeighbourQP = g_RCInvalidQPValue; |
|---|
| 1247 | for ( int i=LCUIdx - 1; i>=0; i-- ) |
|---|
| 1248 | { |
|---|
| 1249 | if ( (getLCU(i)).m_QP > g_RCInvalidQPValue ) |
|---|
| 1250 | { |
|---|
| 1251 | clipNeighbourQP = getLCU(i).m_QP; |
|---|
| 1252 | break; |
|---|
| 1253 | } |
|---|
| 1254 | } |
|---|
| 1255 | |
|---|
| 1256 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
|---|
| 1257 | { |
|---|
| 1258 | estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP ); |
|---|
| 1259 | } |
|---|
| 1260 | |
|---|
| 1261 | estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP ); |
|---|
| 1262 | |
|---|
| 1263 | return estQP; |
|---|
| 1264 | } |
|---|
| 1265 | |
|---|
| 1266 | Void TEncRCPic::updateAfterLCU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter ) |
|---|
| 1267 | { |
|---|
| 1268 | m_LCUs[LCUIdx].m_actualBits = bits; |
|---|
| 1269 | m_LCUs[LCUIdx].m_QP = QP; |
|---|
| 1270 | m_LCUs[LCUIdx].m_lambda = lambda; |
|---|
| 1271 | |
|---|
| 1272 | m_LCULeft--; |
|---|
| 1273 | m_bitsLeft -= bits; |
|---|
| 1274 | m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel; |
|---|
| 1275 | |
|---|
| 1276 | if ( !updateLCUParameter ) |
|---|
| 1277 | { |
|---|
| 1278 | return; |
|---|
| 1279 | } |
|---|
| 1280 | |
|---|
| 1281 | if ( !m_encRCSeq->getUseLCUSeparateModel() ) |
|---|
| 1282 | { |
|---|
| 1283 | return; |
|---|
| 1284 | } |
|---|
| 1285 | |
|---|
| 1286 | Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
|---|
| 1287 | Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
|---|
| 1288 | |
|---|
| 1289 | Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits; |
|---|
| 1290 | Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel; |
|---|
| 1291 | Double bpp = ( Double )LCUActualBits/( Double )LCUTotalPixels; |
|---|
| 1292 | Double calLambda = alpha * pow( bpp, beta ); |
|---|
| 1293 | Double inputLambda = m_LCUs[LCUIdx].m_lambda; |
|---|
| 1294 | |
|---|
| 1295 | if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 ) |
|---|
| 1296 | { |
|---|
| 1297 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
|---|
| 1298 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
|---|
| 1299 | |
|---|
| 1300 | #if M0036_RC_IMPROVEMENT |
|---|
| 1301 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
|---|
| 1302 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
|---|
| 1303 | #else |
|---|
| 1304 | alpha = Clip3( 0.05, 20.0, alpha ); |
|---|
| 1305 | beta = Clip3( -3.0, -0.1, beta ); |
|---|
| 1306 | #endif |
|---|
| 1307 | |
|---|
| 1308 | TRCParameter rcPara; |
|---|
| 1309 | rcPara.m_alpha = alpha; |
|---|
| 1310 | rcPara.m_beta = beta; |
|---|
| 1311 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
|---|
| 1312 | |
|---|
| 1313 | return; |
|---|
| 1314 | } |
|---|
| 1315 | |
|---|
| 1316 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
|---|
| 1317 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
|---|
| 1318 | double lnbpp = log( bpp ); |
|---|
| 1319 | #if M0036_RC_IMPROVEMENT |
|---|
| 1320 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
|---|
| 1321 | #else |
|---|
| 1322 | lnbpp = Clip3( -5.0, 1.0, lnbpp ); |
|---|
| 1323 | #endif |
|---|
| 1324 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
|---|
| 1325 | |
|---|
| 1326 | #if M0036_RC_IMPROVEMENT |
|---|
| 1327 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
|---|
| 1328 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
|---|
| 1329 | #else |
|---|
| 1330 | alpha = Clip3( 0.05, 20.0, alpha ); |
|---|
| 1331 | beta = Clip3( -3.0, -0.1, beta ); |
|---|
| 1332 | #endif |
|---|
| 1333 | TRCParameter rcPara; |
|---|
| 1334 | rcPara.m_alpha = alpha; |
|---|
| 1335 | rcPara.m_beta = beta; |
|---|
| 1336 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
|---|
| 1337 | |
|---|
| 1338 | } |
|---|
| 1339 | |
|---|
| 1340 | #if !M0036_RC_IMPROVEMENT |
|---|
| 1341 | Double TEncRCPic::getEffectivePercentage() |
|---|
| 1342 | { |
|---|
| 1343 | Int effectivePiexels = 0; |
|---|
| 1344 | Int totalPixels = 0; |
|---|
| 1345 | |
|---|
| 1346 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
|---|
| 1347 | { |
|---|
| 1348 | totalPixels += m_LCUs[i].m_numberOfPixel; |
|---|
| 1349 | if ( m_LCUs[i].m_QP > 0 ) |
|---|
| 1350 | { |
|---|
| 1351 | effectivePiexels += m_LCUs[i].m_numberOfPixel; |
|---|
| 1352 | } |
|---|
| 1353 | } |
|---|
| 1354 | |
|---|
| 1355 | Double effectivePixelPercentage = (Double)effectivePiexels/(Double)totalPixels; |
|---|
| 1356 | return effectivePixelPercentage; |
|---|
| 1357 | } |
|---|
| 1358 | #endif |
|---|
| 1359 | |
|---|
| 1360 | Double TEncRCPic::calAverageQP() |
|---|
| 1361 | { |
|---|
| 1362 | Int totalQPs = 0; |
|---|
| 1363 | Int numTotalLCUs = 0; |
|---|
| 1364 | |
|---|
| 1365 | Int i; |
|---|
| 1366 | for ( i=0; i<m_numberOfLCU; i++ ) |
|---|
| 1367 | { |
|---|
| 1368 | if ( m_LCUs[i].m_QP > 0 ) |
|---|
| 1369 | { |
|---|
| 1370 | totalQPs += m_LCUs[i].m_QP; |
|---|
| 1371 | numTotalLCUs++; |
|---|
| 1372 | } |
|---|
| 1373 | } |
|---|
| 1374 | |
|---|
| 1375 | Double avgQP = 0.0; |
|---|
| 1376 | if ( numTotalLCUs == 0 ) |
|---|
| 1377 | { |
|---|
| 1378 | avgQP = g_RCInvalidQPValue; |
|---|
| 1379 | } |
|---|
| 1380 | else |
|---|
| 1381 | { |
|---|
| 1382 | avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs); |
|---|
| 1383 | } |
|---|
| 1384 | return avgQP; |
|---|
| 1385 | } |
|---|
| 1386 | |
|---|
| 1387 | Double TEncRCPic::calAverageLambda() |
|---|
| 1388 | { |
|---|
| 1389 | Double totalLambdas = 0.0; |
|---|
| 1390 | Int numTotalLCUs = 0; |
|---|
| 1391 | |
|---|
| 1392 | Int i; |
|---|
| 1393 | for ( i=0; i<m_numberOfLCU; i++ ) |
|---|
| 1394 | { |
|---|
| 1395 | if ( m_LCUs[i].m_lambda > 0.01 ) |
|---|
| 1396 | { |
|---|
| 1397 | totalLambdas += log( m_LCUs[i].m_lambda ); |
|---|
| 1398 | numTotalLCUs++; |
|---|
| 1399 | } |
|---|
| 1400 | } |
|---|
| 1401 | |
|---|
| 1402 | Double avgLambda; |
|---|
| 1403 | if( numTotalLCUs == 0 ) |
|---|
| 1404 | { |
|---|
| 1405 | avgLambda = -1.0; |
|---|
| 1406 | } |
|---|
| 1407 | else |
|---|
| 1408 | { |
|---|
| 1409 | avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs ); |
|---|
| 1410 | } |
|---|
| 1411 | return avgLambda; |
|---|
| 1412 | } |
|---|
| 1413 | |
|---|
| 1414 | #if M0036_RC_IMPROVEMENT |
|---|
| 1415 | #if RATE_CONTROL_INTRA |
|---|
| 1416 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType) |
|---|
| 1417 | #else |
|---|
| 1418 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda ) |
|---|
| 1419 | #endif |
|---|
| 1420 | #else |
|---|
| 1421 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage ) |
|---|
| 1422 | #endif |
|---|
| 1423 | { |
|---|
| 1424 | m_picActualHeaderBits = actualHeaderBits; |
|---|
| 1425 | m_picActualBits = actualTotalBits; |
|---|
| 1426 | if ( averageQP > 0.0 ) |
|---|
| 1427 | { |
|---|
| 1428 | m_picQP = Int( averageQP + 0.5 ); |
|---|
| 1429 | } |
|---|
| 1430 | else |
|---|
| 1431 | { |
|---|
| 1432 | m_picQP = g_RCInvalidQPValue; |
|---|
| 1433 | } |
|---|
| 1434 | m_picLambda = averageLambda; |
|---|
| 1435 | #if !M0036_RC_IMPROVEMENT || KWU_RC_MADPRED_E0227 |
|---|
| 1436 | m_totalMAD = 0; |
|---|
| 1437 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
|---|
| 1438 | { |
|---|
| 1439 | m_totalMAD += m_LCUs[i].m_MAD; |
|---|
| 1440 | } |
|---|
| 1441 | #endif |
|---|
| 1442 | |
|---|
| 1443 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
|---|
| 1444 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
|---|
| 1445 | #if RATE_CONTROL_INTRA |
|---|
| 1446 | if (eSliceType == I_SLICE) |
|---|
| 1447 | { |
|---|
| 1448 | updateAlphaBetaIntra(&alpha, &beta); |
|---|
| 1449 | } |
|---|
| 1450 | else |
|---|
| 1451 | { |
|---|
| 1452 | #endif |
|---|
| 1453 | // update parameters |
|---|
| 1454 | Double picActualBits = ( Double )m_picActualBits; |
|---|
| 1455 | Double picActualBpp = picActualBits/(Double)m_numberOfPixel; |
|---|
| 1456 | Double calLambda = alpha * pow( picActualBpp, beta ); |
|---|
| 1457 | Double inputLambda = m_picLambda; |
|---|
| 1458 | |
|---|
| 1459 | #if M0036_RC_IMPROVEMENT |
|---|
| 1460 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 ) |
|---|
| 1461 | #else |
|---|
| 1462 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 || effectivePercentage < 0.05 ) |
|---|
| 1463 | #endif |
|---|
| 1464 | { |
|---|
| 1465 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
|---|
| 1466 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
|---|
| 1467 | |
|---|
| 1468 | #if M0036_RC_IMPROVEMENT |
|---|
| 1469 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
|---|
| 1470 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
|---|
| 1471 | #else |
|---|
| 1472 | alpha = Clip3( 0.05, 20.0, alpha ); |
|---|
| 1473 | beta = Clip3( -3.0, -0.1, beta ); |
|---|
| 1474 | #endif |
|---|
| 1475 | TRCParameter rcPara; |
|---|
| 1476 | rcPara.m_alpha = alpha; |
|---|
| 1477 | rcPara.m_beta = beta; |
|---|
| 1478 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
|---|
| 1479 | |
|---|
| 1480 | return; |
|---|
| 1481 | } |
|---|
| 1482 | |
|---|
| 1483 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
|---|
| 1484 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
|---|
| 1485 | double lnbpp = log( picActualBpp ); |
|---|
| 1486 | #if M0036_RC_IMPROVEMENT |
|---|
| 1487 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
|---|
| 1488 | #else |
|---|
| 1489 | lnbpp = Clip3( -5.0, 1.0, lnbpp ); |
|---|
| 1490 | #endif |
|---|
| 1491 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
|---|
| 1492 | |
|---|
| 1493 | #if M0036_RC_IMPROVEMENT |
|---|
| 1494 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
|---|
| 1495 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
|---|
| 1496 | #else |
|---|
| 1497 | alpha = Clip3( 0.05, 20.0, alpha ); |
|---|
| 1498 | beta = Clip3( -3.0, -0.1, beta ); |
|---|
| 1499 | #endif |
|---|
| 1500 | #if RATE_CONTROL_INTRA |
|---|
| 1501 | } |
|---|
| 1502 | #endif |
|---|
| 1503 | |
|---|
| 1504 | TRCParameter rcPara; |
|---|
| 1505 | rcPara.m_alpha = alpha; |
|---|
| 1506 | rcPara.m_beta = beta; |
|---|
| 1507 | |
|---|
| 1508 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
|---|
| 1509 | |
|---|
| 1510 | #if M0036_RC_IMPROVEMENT |
|---|
| 1511 | if ( m_frameLevel == 1 ) |
|---|
| 1512 | { |
|---|
| 1513 | Double currLambda = Clip3( 0.1, 10000.0, m_picLambda ); |
|---|
| 1514 | Double updateLastLambda = g_RCWeightHistoryLambda * m_encRCSeq->getLastLambda() + g_RCWeightCurrentLambda * currLambda; |
|---|
| 1515 | m_encRCSeq->setLastLambda( updateLastLambda ); |
|---|
| 1516 | } |
|---|
| 1517 | #endif |
|---|
| 1518 | } |
|---|
| 1519 | |
|---|
| 1520 | #if RATE_CONTROL_INTRA |
|---|
| 1521 | Int TEncRCPic::getRefineBitsForIntra( Int orgBits ) |
|---|
| 1522 | { |
|---|
| 1523 | Double alpha=0.25, beta=0.5582; |
|---|
| 1524 | Int iIntraBits; |
|---|
| 1525 | |
|---|
| 1526 | if (orgBits*40 < m_numberOfPixel) |
|---|
| 1527 | { |
|---|
| 1528 | alpha=0.25; |
|---|
| 1529 | } |
|---|
| 1530 | else |
|---|
| 1531 | { |
|---|
| 1532 | alpha=0.30; |
|---|
| 1533 | } |
|---|
| 1534 | |
|---|
| 1535 | iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5); |
|---|
| 1536 | |
|---|
| 1537 | return iIntraBits; |
|---|
| 1538 | } |
|---|
| 1539 | |
|---|
| 1540 | Double TEncRCPic::calculateLambdaIntra(double alpha, double beta, double MADPerPixel, double bitsPerPixel) |
|---|
| 1541 | { |
|---|
| 1542 | return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) ); |
|---|
| 1543 | } |
|---|
| 1544 | |
|---|
| 1545 | Void TEncRCPic::updateAlphaBetaIntra(double *alpha, double *beta) |
|---|
| 1546 | { |
|---|
| 1547 | Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1)); |
|---|
| 1548 | Double diffLambda = (*beta)*(log((Double)m_picActualBits)-log((Double)m_targetBits)); |
|---|
| 1549 | |
|---|
| 1550 | diffLambda = Clip3(-0.125, 0.125, 0.25*diffLambda); |
|---|
| 1551 | *alpha = (*alpha) * exp(diffLambda); |
|---|
| 1552 | *beta = (*beta) + diffLambda / lnbpp; |
|---|
| 1553 | } |
|---|
| 1554 | |
|---|
| 1555 | |
|---|
| 1556 | Void TEncRCPic::getLCUInitTargetBits() |
|---|
| 1557 | { |
|---|
| 1558 | Int iAvgBits = 0; |
|---|
| 1559 | |
|---|
| 1560 | m_remainingCostIntra = m_totalCostIntra; |
|---|
| 1561 | for (Int i=m_numberOfLCU-1; i>=0; i--) |
|---|
| 1562 | { |
|---|
| 1563 | iAvgBits += Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra); |
|---|
| 1564 | getLCU(i).m_targetBitsLeft = iAvgBits; |
|---|
| 1565 | } |
|---|
| 1566 | } |
|---|
| 1567 | |
|---|
| 1568 | |
|---|
| 1569 | Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP) |
|---|
| 1570 | { |
|---|
| 1571 | Int LCUIdx = getLCUCoded(); |
|---|
| 1572 | |
|---|
| 1573 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
|---|
| 1574 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
|---|
| 1575 | |
|---|
| 1576 | Double costPerPixel = getLCU(LCUIdx).m_costIntra/(Double)getLCU(LCUIdx).m_numberOfPixel; |
|---|
| 1577 | costPerPixel = pow(costPerPixel, BETA1); |
|---|
| 1578 | Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp); |
|---|
| 1579 | |
|---|
| 1580 | Int clipNeighbourQP = g_RCInvalidQPValue; |
|---|
| 1581 | for (int i=LCUIdx-1; i>=0; i--) |
|---|
| 1582 | { |
|---|
| 1583 | if ((getLCU(i)).m_QP > g_RCInvalidQPValue) |
|---|
| 1584 | { |
|---|
| 1585 | clipNeighbourQP = getLCU(i).m_QP; |
|---|
| 1586 | break; |
|---|
| 1587 | } |
|---|
| 1588 | } |
|---|
| 1589 | |
|---|
| 1590 | Int minQP = clipPicQP - 2; |
|---|
| 1591 | Int maxQP = clipPicQP + 2; |
|---|
| 1592 | |
|---|
| 1593 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
|---|
| 1594 | { |
|---|
| 1595 | maxQP = min(clipNeighbourQP + 1, maxQP); |
|---|
| 1596 | minQP = max(clipNeighbourQP - 1, minQP); |
|---|
| 1597 | } |
|---|
| 1598 | |
|---|
| 1599 | Double maxLambda=exp(((Double)(maxQP+0.49)-13.7122)/4.2005); |
|---|
| 1600 | Double minLambda=exp(((Double)(minQP-0.49)-13.7122)/4.2005); |
|---|
| 1601 | |
|---|
| 1602 | estLambda = Clip3(minLambda, maxLambda, estLambda); |
|---|
| 1603 | |
|---|
| 1604 | *estQP = Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 ); |
|---|
| 1605 | *estQP = Clip3(minQP, maxQP, *estQP); |
|---|
| 1606 | |
|---|
| 1607 | return estLambda; |
|---|
| 1608 | } |
|---|
| 1609 | #endif |
|---|
| 1610 | |
|---|
| 1611 | TEncRateCtrl::TEncRateCtrl() |
|---|
| 1612 | { |
|---|
| 1613 | m_encRCSeq = NULL; |
|---|
| 1614 | m_encRCGOP = NULL; |
|---|
| 1615 | m_encRCPic = NULL; |
|---|
| 1616 | } |
|---|
| 1617 | |
|---|
| 1618 | TEncRateCtrl::~TEncRateCtrl() |
|---|
| 1619 | { |
|---|
| 1620 | destroy(); |
|---|
| 1621 | } |
|---|
| 1622 | |
|---|
| 1623 | Void TEncRateCtrl::destroy() |
|---|
| 1624 | { |
|---|
| 1625 | if ( m_encRCSeq != NULL ) |
|---|
| 1626 | { |
|---|
| 1627 | delete m_encRCSeq; |
|---|
| 1628 | m_encRCSeq = NULL; |
|---|
| 1629 | } |
|---|
| 1630 | if ( m_encRCGOP != NULL ) |
|---|
| 1631 | { |
|---|
| 1632 | delete m_encRCGOP; |
|---|
| 1633 | m_encRCGOP = NULL; |
|---|
| 1634 | } |
|---|
| 1635 | while ( m_listRCPictures.size() > 0 ) |
|---|
| 1636 | { |
|---|
| 1637 | TEncRCPic* p = m_listRCPictures.front(); |
|---|
| 1638 | m_listRCPictures.pop_front(); |
|---|
| 1639 | delete p; |
|---|
| 1640 | } |
|---|
| 1641 | } |
|---|
| 1642 | |
|---|
| 1643 | #if M0036_RC_IMPROVEMENT |
|---|
| 1644 | #if KWU_RC_MADPRED_E0227 |
|---|
| 1645 | 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], Int LayerID ) |
|---|
| 1646 | #else |
|---|
| 1647 | 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] ) |
|---|
| 1648 | #endif |
|---|
| 1649 | #else |
|---|
| 1650 | 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] ) |
|---|
| 1651 | #endif |
|---|
| 1652 | { |
|---|
| 1653 | destroy(); |
|---|
| 1654 | |
|---|
| 1655 | Bool isLowdelay = true; |
|---|
| 1656 | for ( Int i=0; i<GOPSize-1; i++ ) |
|---|
| 1657 | { |
|---|
| 1658 | if ( GOPList[i].m_POC > GOPList[i+1].m_POC ) |
|---|
| 1659 | { |
|---|
| 1660 | isLowdelay = false; |
|---|
| 1661 | break; |
|---|
| 1662 | } |
|---|
| 1663 | } |
|---|
| 1664 | |
|---|
| 1665 | Int numberOfLevel = 1; |
|---|
| 1666 | #if M0036_RC_IMPROVEMENT |
|---|
| 1667 | Int adaptiveBit = 0; |
|---|
| 1668 | if ( keepHierBits > 0 ) |
|---|
| 1669 | #else |
|---|
| 1670 | if ( keepHierBits ) |
|---|
| 1671 | #endif |
|---|
| 1672 | { |
|---|
| 1673 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
|---|
| 1674 | } |
|---|
| 1675 | if ( !isLowdelay && GOPSize == 8 ) |
|---|
| 1676 | { |
|---|
| 1677 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
|---|
| 1678 | } |
|---|
| 1679 | numberOfLevel++; // intra picture |
|---|
| 1680 | numberOfLevel++; // non-reference picture |
|---|
| 1681 | |
|---|
| 1682 | |
|---|
| 1683 | Int* bitsRatio; |
|---|
| 1684 | bitsRatio = new Int[ GOPSize ]; |
|---|
| 1685 | for ( Int i=0; i<GOPSize; i++ ) |
|---|
| 1686 | { |
|---|
| 1687 | bitsRatio[i] = 10; |
|---|
| 1688 | if ( !GOPList[i].m_refPic ) |
|---|
| 1689 | { |
|---|
| 1690 | bitsRatio[i] = 2; |
|---|
| 1691 | } |
|---|
| 1692 | } |
|---|
| 1693 | |
|---|
| 1694 | #if M0036_RC_IMPROVEMENT |
|---|
| 1695 | if ( keepHierBits > 0 ) |
|---|
| 1696 | #else |
|---|
| 1697 | if ( keepHierBits ) |
|---|
| 1698 | #endif |
|---|
| 1699 | { |
|---|
| 1700 | Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); |
|---|
| 1701 | if ( GOPSize == 4 && isLowdelay ) |
|---|
| 1702 | { |
|---|
| 1703 | if ( bpp > 0.2 ) |
|---|
| 1704 | { |
|---|
| 1705 | bitsRatio[0] = 2; |
|---|
| 1706 | bitsRatio[1] = 3; |
|---|
| 1707 | bitsRatio[2] = 2; |
|---|
| 1708 | bitsRatio[3] = 6; |
|---|
| 1709 | } |
|---|
| 1710 | else if( bpp > 0.1 ) |
|---|
| 1711 | { |
|---|
| 1712 | bitsRatio[0] = 2; |
|---|
| 1713 | bitsRatio[1] = 3; |
|---|
| 1714 | bitsRatio[2] = 2; |
|---|
| 1715 | bitsRatio[3] = 10; |
|---|
| 1716 | } |
|---|
| 1717 | else if ( bpp > 0.05 ) |
|---|
| 1718 | { |
|---|
| 1719 | bitsRatio[0] = 2; |
|---|
| 1720 | bitsRatio[1] = 3; |
|---|
| 1721 | bitsRatio[2] = 2; |
|---|
| 1722 | bitsRatio[3] = 12; |
|---|
| 1723 | } |
|---|
| 1724 | else |
|---|
| 1725 | { |
|---|
| 1726 | bitsRatio[0] = 2; |
|---|
| 1727 | bitsRatio[1] = 3; |
|---|
| 1728 | bitsRatio[2] = 2; |
|---|
| 1729 | bitsRatio[3] = 14; |
|---|
| 1730 | } |
|---|
| 1731 | #if M0036_RC_IMPROVEMENT |
|---|
| 1732 | if ( keepHierBits == 2 ) |
|---|
| 1733 | { |
|---|
| 1734 | adaptiveBit = 1; |
|---|
| 1735 | } |
|---|
| 1736 | #endif |
|---|
| 1737 | } |
|---|
| 1738 | else if ( GOPSize == 8 && !isLowdelay ) |
|---|
| 1739 | { |
|---|
| 1740 | if ( bpp > 0.2 ) |
|---|
| 1741 | { |
|---|
| 1742 | bitsRatio[0] = 15; |
|---|
| 1743 | bitsRatio[1] = 5; |
|---|
| 1744 | bitsRatio[2] = 4; |
|---|
| 1745 | bitsRatio[3] = 1; |
|---|
| 1746 | bitsRatio[4] = 1; |
|---|
| 1747 | bitsRatio[5] = 4; |
|---|
| 1748 | bitsRatio[6] = 1; |
|---|
| 1749 | bitsRatio[7] = 1; |
|---|
| 1750 | } |
|---|
| 1751 | else if ( bpp > 0.1 ) |
|---|
| 1752 | { |
|---|
| 1753 | bitsRatio[0] = 20; |
|---|
| 1754 | bitsRatio[1] = 6; |
|---|
| 1755 | bitsRatio[2] = 4; |
|---|
| 1756 | bitsRatio[3] = 1; |
|---|
| 1757 | bitsRatio[4] = 1; |
|---|
| 1758 | bitsRatio[5] = 4; |
|---|
| 1759 | bitsRatio[6] = 1; |
|---|
| 1760 | bitsRatio[7] = 1; |
|---|
| 1761 | } |
|---|
| 1762 | else if ( bpp > 0.05 ) |
|---|
| 1763 | { |
|---|
| 1764 | bitsRatio[0] = 25; |
|---|
| 1765 | bitsRatio[1] = 7; |
|---|
| 1766 | bitsRatio[2] = 4; |
|---|
| 1767 | bitsRatio[3] = 1; |
|---|
| 1768 | bitsRatio[4] = 1; |
|---|
| 1769 | bitsRatio[5] = 4; |
|---|
| 1770 | bitsRatio[6] = 1; |
|---|
| 1771 | bitsRatio[7] = 1; |
|---|
| 1772 | } |
|---|
| 1773 | else |
|---|
| 1774 | { |
|---|
| 1775 | bitsRatio[0] = 30; |
|---|
| 1776 | bitsRatio[1] = 8; |
|---|
| 1777 | bitsRatio[2] = 4; |
|---|
| 1778 | bitsRatio[3] = 1; |
|---|
| 1779 | bitsRatio[4] = 1; |
|---|
| 1780 | bitsRatio[5] = 4; |
|---|
| 1781 | bitsRatio[6] = 1; |
|---|
| 1782 | bitsRatio[7] = 1; |
|---|
| 1783 | } |
|---|
| 1784 | #if M0036_RC_IMPROVEMENT |
|---|
| 1785 | if ( keepHierBits == 2 ) |
|---|
| 1786 | { |
|---|
| 1787 | adaptiveBit = 2; |
|---|
| 1788 | } |
|---|
| 1789 | #endif |
|---|
| 1790 | } |
|---|
| 1791 | else |
|---|
| 1792 | { |
|---|
| 1793 | #if M0036_RC_IMPROVEMENT |
|---|
| 1794 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" ); |
|---|
| 1795 | #else |
|---|
| 1796 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently." ); |
|---|
| 1797 | #endif |
|---|
| 1798 | } |
|---|
| 1799 | } |
|---|
| 1800 | |
|---|
| 1801 | Int* GOPID2Level = new int[ GOPSize ]; |
|---|
| 1802 | for ( int i=0; i<GOPSize; i++ ) |
|---|
| 1803 | { |
|---|
| 1804 | GOPID2Level[i] = 1; |
|---|
| 1805 | if ( !GOPList[i].m_refPic ) |
|---|
| 1806 | { |
|---|
| 1807 | GOPID2Level[i] = 2; |
|---|
| 1808 | } |
|---|
| 1809 | } |
|---|
| 1810 | #if M0036_RC_IMPROVEMENT |
|---|
| 1811 | if ( keepHierBits > 0 ) |
|---|
| 1812 | #else |
|---|
| 1813 | if ( keepHierBits ) |
|---|
| 1814 | #endif |
|---|
| 1815 | { |
|---|
| 1816 | if ( GOPSize == 4 && isLowdelay ) |
|---|
| 1817 | { |
|---|
| 1818 | GOPID2Level[0] = 3; |
|---|
| 1819 | GOPID2Level[1] = 2; |
|---|
| 1820 | GOPID2Level[2] = 3; |
|---|
| 1821 | GOPID2Level[3] = 1; |
|---|
| 1822 | } |
|---|
| 1823 | else if ( GOPSize == 8 && !isLowdelay ) |
|---|
| 1824 | { |
|---|
| 1825 | GOPID2Level[0] = 1; |
|---|
| 1826 | GOPID2Level[1] = 2; |
|---|
| 1827 | GOPID2Level[2] = 3; |
|---|
| 1828 | GOPID2Level[3] = 4; |
|---|
| 1829 | GOPID2Level[4] = 4; |
|---|
| 1830 | GOPID2Level[5] = 3; |
|---|
| 1831 | GOPID2Level[6] = 4; |
|---|
| 1832 | GOPID2Level[7] = 4; |
|---|
| 1833 | } |
|---|
| 1834 | } |
|---|
| 1835 | |
|---|
| 1836 | if ( !isLowdelay && GOPSize == 8 ) |
|---|
| 1837 | { |
|---|
| 1838 | GOPID2Level[0] = 1; |
|---|
| 1839 | GOPID2Level[1] = 2; |
|---|
| 1840 | GOPID2Level[2] = 3; |
|---|
| 1841 | GOPID2Level[3] = 4; |
|---|
| 1842 | GOPID2Level[4] = 4; |
|---|
| 1843 | GOPID2Level[5] = 3; |
|---|
| 1844 | GOPID2Level[6] = 4; |
|---|
| 1845 | GOPID2Level[7] = 4; |
|---|
| 1846 | } |
|---|
| 1847 | |
|---|
| 1848 | m_encRCSeq = new TEncRCSeq; |
|---|
| 1849 | #if M0036_RC_IMPROVEMENT |
|---|
| 1850 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit ); |
|---|
| 1851 | #else |
|---|
| 1852 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel ); |
|---|
| 1853 | #endif |
|---|
| 1854 | m_encRCSeq->initBitsRatio( bitsRatio ); |
|---|
| 1855 | m_encRCSeq->initGOPID2Level( GOPID2Level ); |
|---|
| 1856 | m_encRCSeq->initPicPara(); |
|---|
| 1857 | if ( useLCUSeparateModel ) |
|---|
| 1858 | { |
|---|
| 1859 | m_encRCSeq->initLCUPara(); |
|---|
| 1860 | } |
|---|
| 1861 | |
|---|
| 1862 | #if KWU_RC_MADPRED_E0227 |
|---|
| 1863 | setLayerID(LayerID); |
|---|
| 1864 | #endif |
|---|
| 1865 | |
|---|
| 1866 | delete[] bitsRatio; |
|---|
| 1867 | delete[] GOPID2Level; |
|---|
| 1868 | } |
|---|
| 1869 | |
|---|
| 1870 | Void TEncRateCtrl::initRCPic( Int frameLevel ) |
|---|
| 1871 | { |
|---|
| 1872 | m_encRCPic = new TEncRCPic; |
|---|
| 1873 | #if KWU_RC_MADPRED_E0227 |
|---|
| 1874 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures, m_LayerID ); |
|---|
| 1875 | #else |
|---|
| 1876 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures ); |
|---|
| 1877 | #endif |
|---|
| 1878 | } |
|---|
| 1879 | |
|---|
| 1880 | Void TEncRateCtrl::initRCGOP( Int numberOfPictures ) |
|---|
| 1881 | { |
|---|
| 1882 | m_encRCGOP = new TEncRCGOP; |
|---|
| 1883 | m_encRCGOP->create( m_encRCSeq, numberOfPictures ); |
|---|
| 1884 | } |
|---|
| 1885 | |
|---|
| 1886 | Void TEncRateCtrl::destroyRCGOP() |
|---|
| 1887 | { |
|---|
| 1888 | delete m_encRCGOP; |
|---|
| 1889 | m_encRCGOP = NULL; |
|---|
| 1890 | } |
|---|
| 1891 | |
|---|
| 1892 | #else |
|---|
| 1893 | |
|---|
| 1894 | #define ADJUSTMENT_FACTOR 0.60 |
|---|
| 1895 | #define HIGH_QSTEP_THRESHOLD 9.5238 |
|---|
| 1896 | #define HIGH_QSTEP_ALPHA 4.9371 |
|---|
| 1897 | #define HIGH_QSTEP_BETA 0.0922 |
|---|
| 1898 | #define LOW_QSTEP_ALPHA 16.7429 |
|---|
| 1899 | #define LOW_QSTEP_BETA -1.1494 |
|---|
| 1900 | |
|---|
| 1901 | #define MAD_PRED_Y1 1.0 |
|---|
| 1902 | #define MAD_PRED_Y2 0.0 |
|---|
| 1903 | |
|---|
| 1904 | enum MAD_HISOTRY { |
|---|
| 1905 | MAD_PPPrevious = 0, |
|---|
| 1906 | MAD_PPrevious = 1, |
|---|
| 1907 | MAD_Previous = 2 |
|---|
| 1908 | }; |
|---|
| 1909 | |
|---|
| 1910 | Void MADLinearModel::initMADLinearModel() |
|---|
| 1911 | { |
|---|
| 1912 | m_activeOn = false; |
|---|
| 1913 | m_paramY1 = 1.0; |
|---|
| 1914 | m_paramY2 = 0.0; |
|---|
| 1915 | m_costMADs[0] = m_costMADs[1] = m_costMADs[2] = 0.0; |
|---|
| 1916 | } |
|---|
| 1917 | |
|---|
| 1918 | Double MADLinearModel::getMAD() |
|---|
| 1919 | { |
|---|
| 1920 | Double costPredMAD = m_paramY1 * m_costMADs[MAD_Previous] + m_paramY2; |
|---|
| 1921 | |
|---|
| 1922 | if(costPredMAD < 0) |
|---|
| 1923 | { |
|---|
| 1924 | costPredMAD = m_costMADs[MAD_Previous]; |
|---|
| 1925 | m_paramY1 = MAD_PRED_Y1; |
|---|
| 1926 | m_paramY2 = MAD_PRED_Y2; |
|---|
| 1927 | } |
|---|
| 1928 | return costPredMAD; |
|---|
| 1929 | } |
|---|
| 1930 | |
|---|
| 1931 | Void MADLinearModel::updateMADLiearModel() |
|---|
| 1932 | { |
|---|
| 1933 | Double dNewY1 = ((m_costMADs[MAD_Previous] - m_costMADs[MAD_PPrevious]) / (m_costMADs[MAD_PPrevious] - m_costMADs[MAD_PPPrevious])); |
|---|
| 1934 | Double dNewY2 = (m_costMADs[MAD_Previous] - (dNewY1*m_costMADs[MAD_PPrevious])); |
|---|
| 1935 | |
|---|
| 1936 | m_paramY1 = 0.70+0.20*m_paramY1+ 0.10*dNewY1; |
|---|
| 1937 | m_paramY2 = 0.20*m_paramY2+ 0.10*dNewY2; |
|---|
| 1938 | } |
|---|
| 1939 | |
|---|
| 1940 | Void MADLinearModel::updateMADHistory(Double dMAD) |
|---|
| 1941 | { |
|---|
| 1942 | m_costMADs[MAD_PPPrevious] = m_costMADs[MAD_PPrevious]; |
|---|
| 1943 | m_costMADs[MAD_PPrevious ] = m_costMADs[MAD_Previous ]; |
|---|
| 1944 | m_costMADs[MAD_Previous ] = dMAD; |
|---|
| 1945 | m_activeOn = (m_costMADs[MAD_Previous ] && m_costMADs[MAD_PPrevious ] && m_costMADs[MAD_PPPrevious]); |
|---|
| 1946 | } |
|---|
| 1947 | |
|---|
| 1948 | |
|---|
| 1949 | Void PixelBaseURQQuadraticModel::initPixelBaseQuadraticModel() |
|---|
| 1950 | { |
|---|
| 1951 | m_paramHighX1 = HIGH_QSTEP_ALPHA; |
|---|
| 1952 | m_paramHighX2 = HIGH_QSTEP_BETA; |
|---|
| 1953 | m_paramLowX1 = LOW_QSTEP_ALPHA; |
|---|
| 1954 | m_paramLowX2 = LOW_QSTEP_BETA; |
|---|
| 1955 | } |
|---|
| 1956 | |
|---|
| 1957 | Int PixelBaseURQQuadraticModel::getQP(Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD) |
|---|
| 1958 | { |
|---|
| 1959 | Double qStep; |
|---|
| 1960 | Double bppPerMAD = (Double)(targetBits/(numberOfPixels*costPredMAD)); |
|---|
| 1961 | |
|---|
| 1962 | if(xConvertQP2QStep(qp) >= HIGH_QSTEP_THRESHOLD) |
|---|
| 1963 | { |
|---|
| 1964 | qStep = 1/( sqrt((bppPerMAD/m_paramHighX1)+((m_paramHighX2*m_paramHighX2)/(4*m_paramHighX1*m_paramHighX1))) - (m_paramHighX2/(2*m_paramHighX1))); |
|---|
| 1965 | } |
|---|
| 1966 | else |
|---|
| 1967 | { |
|---|
| 1968 | qStep = 1/( sqrt((bppPerMAD/m_paramLowX1)+((m_paramLowX2*m_paramLowX2)/(4*m_paramLowX1*m_paramLowX1))) - (m_paramLowX2/(2*m_paramLowX1))); |
|---|
| 1969 | } |
|---|
| 1970 | |
|---|
| 1971 | return xConvertQStep2QP(qStep); |
|---|
| 1972 | } |
|---|
| 1973 | |
|---|
| 1974 | Void PixelBaseURQQuadraticModel::updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD) |
|---|
| 1975 | { |
|---|
| 1976 | Double qStep = xConvertQP2QStep(qp); |
|---|
| 1977 | Double invqStep = (1/qStep); |
|---|
| 1978 | Double paramNewX1, paramNewX2; |
|---|
| 1979 | |
|---|
| 1980 | if(qStep >= HIGH_QSTEP_THRESHOLD) |
|---|
| 1981 | { |
|---|
| 1982 | paramNewX2 = (((bits/(numberOfPixels*costMAD))-(23.3772*invqStep*invqStep))/((1-200*invqStep)*invqStep)); |
|---|
| 1983 | paramNewX1 = (23.3772-200*paramNewX2); |
|---|
| 1984 | m_paramHighX1 = 0.70*HIGH_QSTEP_ALPHA + 0.20 * m_paramHighX1 + 0.10 * paramNewX1; |
|---|
| 1985 | m_paramHighX2 = 0.70*HIGH_QSTEP_BETA + 0.20 * m_paramHighX2 + 0.10 * paramNewX2; |
|---|
| 1986 | } |
|---|
| 1987 | else |
|---|
| 1988 | { |
|---|
| 1989 | paramNewX2 = (((bits/(numberOfPixels*costMAD))-(5.8091*invqStep*invqStep))/((1-9.5455*invqStep)*invqStep)); |
|---|
| 1990 | paramNewX1 = (5.8091-9.5455*paramNewX2); |
|---|
| 1991 | m_paramLowX1 = 0.90*LOW_QSTEP_ALPHA + 0.09 * m_paramLowX1 + 0.01 * paramNewX1; |
|---|
| 1992 | m_paramLowX2 = 0.90*LOW_QSTEP_BETA + 0.09 * m_paramLowX2 + 0.01 * paramNewX2; |
|---|
| 1993 | } |
|---|
| 1994 | } |
|---|
| 1995 | |
|---|
| 1996 | Bool PixelBaseURQQuadraticModel::checkUpdateAvailable(Int qpReference ) |
|---|
| 1997 | { |
|---|
| 1998 | Double qStep = xConvertQP2QStep(qpReference); |
|---|
| 1999 | |
|---|
| 2000 | if (qStep > xConvertQP2QStep(MAX_QP) |
|---|
| 2001 | ||qStep < xConvertQP2QStep(MIN_QP) ) |
|---|
| 2002 | { |
|---|
| 2003 | return false; |
|---|
| 2004 | } |
|---|
| 2005 | |
|---|
| 2006 | return true; |
|---|
| 2007 | } |
|---|
| 2008 | |
|---|
| 2009 | Double PixelBaseURQQuadraticModel::xConvertQP2QStep(Int qp ) |
|---|
| 2010 | { |
|---|
| 2011 | Int i; |
|---|
| 2012 | Double qStep; |
|---|
| 2013 | static const Double mapQP2QSTEP[6] = { 0.625, 0.703, 0.797, 0.891, 1.000, 1.125 }; |
|---|
| 2014 | |
|---|
| 2015 | qStep = mapQP2QSTEP[qp % 6]; |
|---|
| 2016 | for( i=0; i<(qp/6); i++) |
|---|
| 2017 | { |
|---|
| 2018 | qStep *= 2; |
|---|
| 2019 | } |
|---|
| 2020 | |
|---|
| 2021 | return qStep; |
|---|
| 2022 | } |
|---|
| 2023 | |
|---|
| 2024 | Int PixelBaseURQQuadraticModel::xConvertQStep2QP(Double qStep ) |
|---|
| 2025 | { |
|---|
| 2026 | Int per = 0, rem = 0; |
|---|
| 2027 | |
|---|
| 2028 | if( qStep < xConvertQP2QStep(MIN_QP)) |
|---|
| 2029 | { |
|---|
| 2030 | return MIN_QP; |
|---|
| 2031 | } |
|---|
| 2032 | else if (qStep > xConvertQP2QStep(MAX_QP) ) |
|---|
| 2033 | { |
|---|
| 2034 | return MAX_QP; |
|---|
| 2035 | } |
|---|
| 2036 | |
|---|
| 2037 | while( qStep > xConvertQP2QStep(5) ) |
|---|
| 2038 | { |
|---|
| 2039 | qStep /= 2.0; |
|---|
| 2040 | per++; |
|---|
| 2041 | } |
|---|
| 2042 | |
|---|
| 2043 | if (qStep <= 0.625) |
|---|
| 2044 | { |
|---|
| 2045 | rem = 0; |
|---|
| 2046 | } |
|---|
| 2047 | else if (qStep <= 0.703) |
|---|
| 2048 | { |
|---|
| 2049 | rem = 1; |
|---|
| 2050 | } |
|---|
| 2051 | else if (qStep <= 0.797) |
|---|
| 2052 | { |
|---|
| 2053 | rem = 2; |
|---|
| 2054 | } |
|---|
| 2055 | else if (qStep <= 0.891) |
|---|
| 2056 | { |
|---|
| 2057 | rem = 3; |
|---|
| 2058 | } |
|---|
| 2059 | else if (qStep <= 1.000) |
|---|
| 2060 | { |
|---|
| 2061 | rem = 4; |
|---|
| 2062 | } |
|---|
| 2063 | else |
|---|
| 2064 | { |
|---|
| 2065 | rem = 5; |
|---|
| 2066 | } |
|---|
| 2067 | return (per * 6 + rem); |
|---|
| 2068 | } |
|---|
| 2069 | |
|---|
| 2070 | |
|---|
| 2071 | Void TEncRateCtrl::create(Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight) |
|---|
| 2072 | { |
|---|
| 2073 | Int leftInHeight, leftInWidth; |
|---|
| 2074 | |
|---|
| 2075 | m_sourceWidthInLCU = (sourceWidth / maxCUWidth ) + (( sourceWidth % maxCUWidth ) ? 1 : 0); |
|---|
| 2076 | m_sourceHeightInLCU = (sourceHeight / maxCUHeight) + (( sourceHeight % maxCUHeight) ? 1 : 0); |
|---|
| 2077 | m_isLowdelay = (sizeIntraPeriod == -1) ? true : false; |
|---|
| 2078 | m_prevBitrate = ( targetKbps * 1000 ); // in units of 1,024 bps |
|---|
| 2079 | m_currBitrate = ( targetKbps * 1000 ); |
|---|
| 2080 | m_frameRate = frameRate; |
|---|
| 2081 | m_refFrameNum = m_isLowdelay ? (sizeGOP) : (sizeGOP>>1); |
|---|
| 2082 | m_nonRefFrameNum = sizeGOP-m_refFrameNum; |
|---|
| 2083 | m_sizeGOP = sizeGOP; |
|---|
| 2084 | m_numOfPixels = ((sourceWidth*sourceHeight*3)>>1); |
|---|
| 2085 | m_indexGOP = 0; |
|---|
| 2086 | m_indexFrame = 0; |
|---|
| 2087 | m_indexLCU = 0; |
|---|
| 2088 | m_indexUnit = 0; |
|---|
| 2089 | m_indexRefFrame = 0; |
|---|
| 2090 | m_indexNonRefFrame = 0; |
|---|
| 2091 | m_occupancyVB = 0; |
|---|
| 2092 | m_initialOVB = 0; |
|---|
| 2093 | m_targetBufLevel = 0; |
|---|
| 2094 | m_initialTBL = 0; |
|---|
| 2095 | m_occupancyVBInFrame = 0; |
|---|
| 2096 | m_remainingBitsInGOP = (m_currBitrate*sizeGOP/m_frameRate); |
|---|
| 2097 | m_remainingBitsInFrame = 0; |
|---|
| 2098 | m_numUnitInFrame = m_sourceWidthInLCU*m_sourceHeightInLCU; |
|---|
| 2099 | m_cMADLinearModel. initMADLinearModel(); |
|---|
| 2100 | m_cPixelURQQuadraticModel.initPixelBaseQuadraticModel(); |
|---|
| 2101 | |
|---|
| 2102 | m_costRefAvgWeighting = 0.0; |
|---|
| 2103 | m_costNonRefAvgWeighting = 0.0; |
|---|
| 2104 | m_costAvgbpp = 0.0; |
|---|
| 2105 | m_activeUnitLevelOn = false; |
|---|
| 2106 | |
|---|
| 2107 | m_pcFrameData = new FrameData [sizeGOP+1]; initFrameData(qp); |
|---|
| 2108 | m_pcLCUData = new LCUData [m_numUnitInFrame]; initUnitData (qp); |
|---|
| 2109 | |
|---|
| 2110 | for(Int i = 0, addressUnit = 0; i < m_sourceHeightInLCU*maxCUHeight; i += maxCUHeight) |
|---|
| 2111 | { |
|---|
| 2112 | leftInHeight = sourceHeight - i; |
|---|
| 2113 | leftInHeight = min(leftInHeight, maxCUHeight); |
|---|
| 2114 | for(Int j = 0; j < m_sourceWidthInLCU*maxCUWidth; j += maxCUWidth, addressUnit++) |
|---|
| 2115 | { |
|---|
| 2116 | leftInWidth = sourceWidth - j; |
|---|
| 2117 | leftInWidth = min(leftInWidth, maxCUWidth); |
|---|
| 2118 | m_pcLCUData[addressUnit].m_widthInPixel = leftInWidth; |
|---|
| 2119 | m_pcLCUData[addressUnit].m_heightInPixel= leftInHeight; |
|---|
| 2120 | m_pcLCUData[addressUnit].m_pixels = ((leftInHeight*leftInWidth*3)>>1); |
|---|
| 2121 | } |
|---|
| 2122 | } |
|---|
| 2123 | } |
|---|
| 2124 | |
|---|
| 2125 | Void TEncRateCtrl::destroy() |
|---|
| 2126 | { |
|---|
| 2127 | if(m_pcFrameData) |
|---|
| 2128 | { |
|---|
| 2129 | delete [] m_pcFrameData; |
|---|
| 2130 | m_pcFrameData = NULL; |
|---|
| 2131 | } |
|---|
| 2132 | if(m_pcLCUData) |
|---|
| 2133 | { |
|---|
| 2134 | delete [] m_pcLCUData; |
|---|
| 2135 | m_pcLCUData = NULL; |
|---|
| 2136 | } |
|---|
| 2137 | } |
|---|
| 2138 | |
|---|
| 2139 | Void TEncRateCtrl::initFrameData (Int qp) |
|---|
| 2140 | { |
|---|
| 2141 | for(Int i = 0 ; i <= m_sizeGOP; i++) |
|---|
| 2142 | { |
|---|
| 2143 | m_pcFrameData[i].m_isReferenced = false; |
|---|
| 2144 | m_pcFrameData[i].m_costMAD = 0.0; |
|---|
| 2145 | m_pcFrameData[i].m_bits = 0; |
|---|
| 2146 | m_pcFrameData[i].m_qp = qp; |
|---|
| 2147 | } |
|---|
| 2148 | } |
|---|
| 2149 | |
|---|
| 2150 | Void TEncRateCtrl::initUnitData (Int qp) |
|---|
| 2151 | { |
|---|
| 2152 | for(Int i = 1 ; i < m_numUnitInFrame; i++) |
|---|
| 2153 | { |
|---|
| 2154 | m_pcLCUData[i].m_qp = qp; |
|---|
| 2155 | m_pcLCUData[i].m_bits = 0; |
|---|
| 2156 | m_pcLCUData[i].m_pixels = 0; |
|---|
| 2157 | m_pcLCUData[i].m_widthInPixel = 0; |
|---|
| 2158 | m_pcLCUData[i].m_heightInPixel = 0; |
|---|
| 2159 | m_pcLCUData[i].m_costMAD = 0.0; |
|---|
| 2160 | } |
|---|
| 2161 | } |
|---|
| 2162 | |
|---|
| 2163 | Int TEncRateCtrl::getFrameQP(Bool isReferenced, Int POC) |
|---|
| 2164 | { |
|---|
| 2165 | Int numofReferenced = 0; |
|---|
| 2166 | Int finalQP = 0; |
|---|
| 2167 | FrameData* pcFrameData; |
|---|
| 2168 | |
|---|
| 2169 | m_indexPOCInGOP = (POC%m_sizeGOP) == 0 ? m_sizeGOP : (POC%m_sizeGOP); |
|---|
| 2170 | pcFrameData = &m_pcFrameData[m_indexPOCInGOP]; |
|---|
| 2171 | |
|---|
| 2172 | if(m_indexFrame != 0) |
|---|
| 2173 | { |
|---|
| 2174 | if(isReferenced) |
|---|
| 2175 | { |
|---|
| 2176 | Double gamma = m_isLowdelay ? 0.5 : 0.25; |
|---|
| 2177 | Double beta = m_isLowdelay ? 0.9 : 0.6; |
|---|
| 2178 | Int numRemainingRefFrames = m_refFrameNum - m_indexRefFrame; |
|---|
| 2179 | Int numRemainingNRefFrames = m_nonRefFrameNum - m_indexNonRefFrame; |
|---|
| 2180 | |
|---|
| 2181 | Double targetBitsOccupancy = (m_currBitrate/(Double)m_frameRate) + gamma*(m_targetBufLevel-m_occupancyVB - (m_initialOVB/(Double)m_frameRate)); |
|---|
| 2182 | Double targetBitsLeftBudget = ((m_costRefAvgWeighting*m_remainingBitsInGOP)/((m_costRefAvgWeighting*numRemainingRefFrames)+(m_costNonRefAvgWeighting*numRemainingNRefFrames))); |
|---|
| 2183 | |
|---|
| 2184 | m_targetBits = (Int)(beta * targetBitsLeftBudget + (1-beta) * targetBitsOccupancy); |
|---|
| 2185 | |
|---|
| 2186 | if(m_targetBits <= 0 || m_remainingBitsInGOP <= 0) |
|---|
| 2187 | { |
|---|
| 2188 | finalQP = m_pcFrameData[m_indexPrevPOCInGOP].m_qp + 2; |
|---|
| 2189 | } |
|---|
| 2190 | else |
|---|
| 2191 | { |
|---|
| 2192 | Double costPredMAD = m_cMADLinearModel.getMAD(); |
|---|
| 2193 | Int qpLowerBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp-2; |
|---|
| 2194 | Int qpUpperBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp+2; |
|---|
| 2195 | finalQP = m_cPixelURQQuadraticModel.getQP(m_pcFrameData[m_indexPrevPOCInGOP].m_qp, m_targetBits, m_numOfPixels, costPredMAD); |
|---|
| 2196 | finalQP = max(qpLowerBound, min(qpUpperBound, finalQP)); |
|---|
| 2197 | m_activeUnitLevelOn = true; |
|---|
| 2198 | m_remainingBitsInFrame = m_targetBits; |
|---|
| 2199 | m_costAvgbpp = (m_targetBits/(Double)m_numOfPixels); |
|---|
| 2200 | } |
|---|
| 2201 | |
|---|
| 2202 | m_indexRefFrame++; |
|---|
| 2203 | } |
|---|
| 2204 | else |
|---|
| 2205 | { |
|---|
| 2206 | Int bwdQP = m_pcFrameData[m_indexPOCInGOP-1].m_qp; |
|---|
| 2207 | Int fwdQP = m_pcFrameData[m_indexPOCInGOP+1].m_qp; |
|---|
| 2208 | |
|---|
| 2209 | if( (fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP-1].m_qp |
|---|
| 2210 | ||(fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP+1].m_qp) |
|---|
| 2211 | { |
|---|
| 2212 | finalQP = (fwdQP+bwdQP); |
|---|
| 2213 | } |
|---|
| 2214 | else if(bwdQP != fwdQP) |
|---|
| 2215 | { |
|---|
| 2216 | finalQP = ((bwdQP+fwdQP+2)>>1); |
|---|
| 2217 | } |
|---|
| 2218 | else |
|---|
| 2219 | { |
|---|
| 2220 | finalQP = bwdQP+2; |
|---|
| 2221 | } |
|---|
| 2222 | m_indexNonRefFrame++; |
|---|
| 2223 | } |
|---|
| 2224 | } |
|---|
| 2225 | else |
|---|
| 2226 | { |
|---|
| 2227 | Int lastQPminus2 = m_pcFrameData[0].m_qp - 2; |
|---|
| 2228 | Int lastQPplus2 = m_pcFrameData[0].m_qp + 2; |
|---|
| 2229 | |
|---|
| 2230 | for(Int idx = 1; idx <= m_sizeGOP; idx++) |
|---|
| 2231 | { |
|---|
| 2232 | if(m_pcFrameData[idx].m_isReferenced) |
|---|
| 2233 | { |
|---|
| 2234 | finalQP += m_pcFrameData[idx].m_qp; |
|---|
| 2235 | numofReferenced++; |
|---|
| 2236 | } |
|---|
| 2237 | } |
|---|
| 2238 | |
|---|
| 2239 | finalQP = (numofReferenced == 0) ? m_pcFrameData[0].m_qp : ((finalQP + (1<<(numofReferenced>>1)))/numofReferenced); |
|---|
| 2240 | finalQP = max( lastQPminus2, min( lastQPplus2, finalQP)); |
|---|
| 2241 | |
|---|
| 2242 | Double costAvgFrameBits = m_remainingBitsInGOP/(Double)m_sizeGOP; |
|---|
| 2243 | Int bufLevel = m_occupancyVB + m_initialOVB; |
|---|
| 2244 | |
|---|
| 2245 | if(abs(bufLevel) > costAvgFrameBits) |
|---|
| 2246 | { |
|---|
| 2247 | if(bufLevel < 0) |
|---|
| 2248 | { |
|---|
| 2249 | finalQP -= 2; |
|---|
| 2250 | } |
|---|
| 2251 | else |
|---|
| 2252 | { |
|---|
| 2253 | finalQP += 2; |
|---|
| 2254 | } |
|---|
| 2255 | } |
|---|
| 2256 | m_indexRefFrame++; |
|---|
| 2257 | } |
|---|
| 2258 | finalQP = max(MIN_QP, min(MAX_QP, finalQP)); |
|---|
| 2259 | |
|---|
| 2260 | for(Int indexLCU = 0 ; indexLCU < m_numUnitInFrame; indexLCU++) |
|---|
| 2261 | { |
|---|
| 2262 | m_pcLCUData[indexLCU].m_qp = finalQP; |
|---|
| 2263 | } |
|---|
| 2264 | |
|---|
| 2265 | pcFrameData->m_isReferenced = isReferenced; |
|---|
| 2266 | pcFrameData->m_qp = finalQP; |
|---|
| 2267 | |
|---|
| 2268 | return finalQP; |
|---|
| 2269 | } |
|---|
| 2270 | |
|---|
| 2271 | Bool TEncRateCtrl::calculateUnitQP () |
|---|
| 2272 | { |
|---|
| 2273 | if(!m_activeUnitLevelOn || m_indexLCU == 0) |
|---|
| 2274 | { |
|---|
| 2275 | return false; |
|---|
| 2276 | } |
|---|
| 2277 | Int upperQPBound, lowerQPBound, finalQP; |
|---|
| 2278 | Int colQP = m_pcLCUData[m_indexLCU].m_qp; |
|---|
| 2279 | Double colMAD = m_pcLCUData[m_indexLCU].m_costMAD; |
|---|
| 2280 | Double budgetInUnit = m_pcLCUData[m_indexLCU].m_pixels*m_costAvgbpp; |
|---|
| 2281 | |
|---|
| 2282 | |
|---|
| 2283 | Int targetBitsOccupancy = (Int)(budgetInUnit - (m_occupancyVBInFrame/(m_numUnitInFrame-m_indexUnit))); |
|---|
| 2284 | Int targetBitsLeftBudget= (Int)((m_remainingBitsInFrame*m_pcLCUData[m_indexLCU].m_pixels)/(Double)(m_numOfPixels-m_codedPixels)); |
|---|
| 2285 | Int targetBits = (targetBitsLeftBudget>>1) + (targetBitsOccupancy>>1); |
|---|
| 2286 | |
|---|
| 2287 | |
|---|
| 2288 | if( m_indexLCU >= m_sourceWidthInLCU) |
|---|
| 2289 | { |
|---|
| 2290 | upperQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) + MAX_DELTA_QP; |
|---|
| 2291 | lowerQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) - MAX_DELTA_QP; |
|---|
| 2292 | } |
|---|
| 2293 | else |
|---|
| 2294 | { |
|---|
| 2295 | upperQPBound = m_pcLCUData[m_indexLCU-1].m_qp + MAX_DELTA_QP; |
|---|
| 2296 | lowerQPBound = m_pcLCUData[m_indexLCU-1].m_qp - MAX_DELTA_QP; |
|---|
| 2297 | } |
|---|
| 2298 | |
|---|
| 2299 | if(targetBits < 0) |
|---|
| 2300 | { |
|---|
| 2301 | finalQP = m_pcLCUData[m_indexLCU-1].m_qp + 1; |
|---|
| 2302 | } |
|---|
| 2303 | else |
|---|
| 2304 | { |
|---|
| 2305 | finalQP = m_cPixelURQQuadraticModel.getQP(colQP, targetBits, m_pcLCUData[m_indexLCU].m_pixels, colMAD); |
|---|
| 2306 | } |
|---|
| 2307 | |
|---|
| 2308 | finalQP = max(lowerQPBound, min(upperQPBound, finalQP)); |
|---|
| 2309 | m_pcLCUData[m_indexLCU].m_qp = max(MIN_QP, min(MAX_QP, finalQP)); |
|---|
| 2310 | |
|---|
| 2311 | return true; |
|---|
| 2312 | } |
|---|
| 2313 | |
|---|
| 2314 | Void TEncRateCtrl::updateRCGOPStatus() |
|---|
| 2315 | { |
|---|
| 2316 | m_remainingBitsInGOP = ((m_currBitrate/m_frameRate)*m_sizeGOP) - m_occupancyVB; |
|---|
| 2317 | |
|---|
| 2318 | FrameData cFrameData = m_pcFrameData[m_sizeGOP]; |
|---|
| 2319 | initFrameData(); |
|---|
| 2320 | |
|---|
| 2321 | m_pcFrameData[0] = cFrameData; |
|---|
| 2322 | m_indexGOP++; |
|---|
| 2323 | m_indexFrame = 0; |
|---|
| 2324 | m_indexRefFrame = 0; |
|---|
| 2325 | m_indexNonRefFrame = 0; |
|---|
| 2326 | } |
|---|
| 2327 | |
|---|
| 2328 | Void TEncRateCtrl::updataRCFrameStatus(Int frameBits, SliceType eSliceType) |
|---|
| 2329 | { |
|---|
| 2330 | FrameData* pcFrameData = &m_pcFrameData[m_indexPOCInGOP]; |
|---|
| 2331 | Int occupancyBits; |
|---|
| 2332 | Double adjustmentBits; |
|---|
| 2333 | |
|---|
| 2334 | m_remainingBitsInGOP = m_remainingBitsInGOP + ( ((m_currBitrate-m_prevBitrate)/m_frameRate)*(m_sizeGOP-m_indexFrame) ) - frameBits; |
|---|
| 2335 | occupancyBits = (Int)((Double)frameBits - (m_currBitrate/(Double)m_frameRate)); |
|---|
| 2336 | |
|---|
| 2337 | if( (occupancyBits < 0) && (m_initialOVB > 0) ) |
|---|
| 2338 | { |
|---|
| 2339 | adjustmentBits = xAdjustmentBits(occupancyBits, m_initialOVB ); |
|---|
| 2340 | |
|---|
| 2341 | if(m_initialOVB < 0) |
|---|
| 2342 | { |
|---|
| 2343 | adjustmentBits = m_initialOVB; |
|---|
| 2344 | occupancyBits += (Int)adjustmentBits; |
|---|
| 2345 | m_initialOVB = 0; |
|---|
| 2346 | } |
|---|
| 2347 | } |
|---|
| 2348 | else if( (occupancyBits > 0) && (m_initialOVB < 0) ) |
|---|
| 2349 | { |
|---|
| 2350 | adjustmentBits = xAdjustmentBits(m_initialOVB, occupancyBits ); |
|---|
| 2351 | |
|---|
| 2352 | if(occupancyBits < 0) |
|---|
| 2353 | { |
|---|
| 2354 | adjustmentBits = occupancyBits; |
|---|
| 2355 | m_initialOVB += (Int)adjustmentBits; |
|---|
| 2356 | occupancyBits = 0; |
|---|
| 2357 | } |
|---|
| 2358 | } |
|---|
| 2359 | |
|---|
| 2360 | if(m_indexGOP == 0) |
|---|
| 2361 | { |
|---|
| 2362 | m_initialOVB = occupancyBits; |
|---|
| 2363 | } |
|---|
| 2364 | else |
|---|
| 2365 | { |
|---|
| 2366 | m_occupancyVB= m_occupancyVB + occupancyBits; |
|---|
| 2367 | } |
|---|
| 2368 | |
|---|
| 2369 | if(pcFrameData->m_isReferenced) |
|---|
| 2370 | { |
|---|
| 2371 | m_costRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costRefAvgWeighting)/8.0); |
|---|
| 2372 | |
|---|
| 2373 | if(m_indexFrame == 0) |
|---|
| 2374 | { |
|---|
| 2375 | m_initialTBL = m_targetBufLevel = (frameBits - (m_currBitrate/m_frameRate)); |
|---|
| 2376 | } |
|---|
| 2377 | else |
|---|
| 2378 | { |
|---|
| 2379 | Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1; |
|---|
| 2380 | m_targetBufLevel = m_targetBufLevel |
|---|
| 2381 | - (m_initialTBL/(m_refFrameNum-1)) |
|---|
| 2382 | + (Int)((m_costRefAvgWeighting*(distance+1)*m_currBitrate)/(m_frameRate*(m_costRefAvgWeighting+(m_costNonRefAvgWeighting*distance)))) |
|---|
| 2383 | - (m_currBitrate/m_frameRate); |
|---|
| 2384 | } |
|---|
| 2385 | |
|---|
| 2386 | if(m_cMADLinearModel.IsUpdateAvailable()) |
|---|
| 2387 | { |
|---|
| 2388 | m_cMADLinearModel.updateMADLiearModel(); |
|---|
| 2389 | } |
|---|
| 2390 | |
|---|
| 2391 | if(eSliceType != I_SLICE && |
|---|
| 2392 | m_cPixelURQQuadraticModel.checkUpdateAvailable(pcFrameData->m_qp)) |
|---|
| 2393 | { |
|---|
| 2394 | m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(pcFrameData->m_qp, pcFrameData->m_bits, m_numOfPixels, pcFrameData->m_costMAD); |
|---|
| 2395 | } |
|---|
| 2396 | } |
|---|
| 2397 | else |
|---|
| 2398 | { |
|---|
| 2399 | m_costNonRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costNonRefAvgWeighting)/8.0); |
|---|
| 2400 | } |
|---|
| 2401 | |
|---|
| 2402 | m_indexFrame++; |
|---|
| 2403 | m_indexLCU = 0; |
|---|
| 2404 | m_indexUnit = 0; |
|---|
| 2405 | m_occupancyVBInFrame = 0; |
|---|
| 2406 | m_remainingBitsInFrame = 0; |
|---|
| 2407 | m_codedPixels = 0; |
|---|
| 2408 | m_activeUnitLevelOn = false; |
|---|
| 2409 | m_costAvgbpp = 0.0; |
|---|
| 2410 | } |
|---|
| 2411 | Void TEncRateCtrl::updataRCUnitStatus () |
|---|
| 2412 | { |
|---|
| 2413 | if(!m_activeUnitLevelOn || m_indexLCU == 0) |
|---|
| 2414 | { |
|---|
| 2415 | return; |
|---|
| 2416 | } |
|---|
| 2417 | |
|---|
| 2418 | m_codedPixels += m_pcLCUData[m_indexLCU-1].m_pixels; |
|---|
| 2419 | m_remainingBitsInFrame = m_remainingBitsInFrame - m_pcLCUData[m_indexLCU-1].m_bits; |
|---|
| 2420 | m_occupancyVBInFrame = (Int)(m_occupancyVBInFrame + m_pcLCUData[m_indexLCU-1].m_bits - m_pcLCUData[m_indexLCU-1].m_pixels*m_costAvgbpp); |
|---|
| 2421 | |
|---|
| 2422 | if( m_cPixelURQQuadraticModel.checkUpdateAvailable(m_pcLCUData[m_indexLCU-1].m_qp) ) |
|---|
| 2423 | { |
|---|
| 2424 | 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); |
|---|
| 2425 | } |
|---|
| 2426 | |
|---|
| 2427 | m_indexUnit++; |
|---|
| 2428 | } |
|---|
| 2429 | |
|---|
| 2430 | Void TEncRateCtrl::updateFrameData(UInt64 actualFrameBits) |
|---|
| 2431 | { |
|---|
| 2432 | Double costMAD = 0.0; |
|---|
| 2433 | |
|---|
| 2434 | for(Int i = 0; i < m_numUnitInFrame; i++) |
|---|
| 2435 | { |
|---|
| 2436 | costMAD += m_pcLCUData[i].m_costMAD; |
|---|
| 2437 | } |
|---|
| 2438 | |
|---|
| 2439 | m_pcFrameData[m_indexPOCInGOP].m_costMAD = (costMAD/(Double)m_numUnitInFrame); |
|---|
| 2440 | m_pcFrameData[m_indexPOCInGOP].m_bits = (Int)actualFrameBits; |
|---|
| 2441 | |
|---|
| 2442 | if(m_pcFrameData[m_indexPOCInGOP].m_isReferenced) |
|---|
| 2443 | { |
|---|
| 2444 | m_indexPrevPOCInGOP = m_indexPOCInGOP; |
|---|
| 2445 | m_cMADLinearModel.updateMADHistory(m_pcFrameData[m_indexPOCInGOP].m_costMAD); |
|---|
| 2446 | } |
|---|
| 2447 | } |
|---|
| 2448 | |
|---|
| 2449 | Void TEncRateCtrl::updateLCUData(TComDataCU* pcCU, UInt64 actualLCUBits, Int qp) |
|---|
| 2450 | { |
|---|
| 2451 | Int x, y; |
|---|
| 2452 | Double costMAD = 0.0; |
|---|
| 2453 | |
|---|
| 2454 | Pel* pOrg = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
|---|
| 2455 | Pel* pRec = pcCU->getPic()->getPicYuvPred()->getLumaAddr(pcCU->getAddr(), 0); |
|---|
| 2456 | Int stride = pcCU->getPic()->getStride(); |
|---|
| 2457 | |
|---|
| 2458 | Int width = m_pcLCUData[m_indexLCU].m_widthInPixel; |
|---|
| 2459 | Int height = m_pcLCUData[m_indexLCU].m_heightInPixel; |
|---|
| 2460 | |
|---|
| 2461 | for( y = 0; y < height; y++ ) |
|---|
| 2462 | { |
|---|
| 2463 | for( x = 0; x < width; x++ ) |
|---|
| 2464 | { |
|---|
| 2465 | costMAD += abs( pOrg[x] - pRec[x] ); |
|---|
| 2466 | } |
|---|
| 2467 | pOrg += stride; |
|---|
| 2468 | pRec += stride; |
|---|
| 2469 | } |
|---|
| 2470 | m_pcLCUData[m_indexLCU ].m_qp = qp; |
|---|
| 2471 | m_pcLCUData[m_indexLCU ].m_costMAD = (costMAD /(Double)(width*height)); |
|---|
| 2472 | m_pcLCUData[m_indexLCU++].m_bits = (Int)actualLCUBits; |
|---|
| 2473 | } |
|---|
| 2474 | |
|---|
| 2475 | |
|---|
| 2476 | #if KWU_RC_MADPRED_E0227 |
|---|
| 2477 | Void TEncRateCtrl::updateLCUDataEnhancedView(TComDataCU* pcCU, UInt64 uiBits, Int iQP, Double BasePos, Double CurPos, Double FocalLen, Double Znear, Double Zfar, Int Direction) |
|---|
| 2478 | { |
|---|
| 2479 | Int x, y; |
|---|
| 2480 | double dMAD = 0.0; |
|---|
| 2481 | Int Sum = 0; |
|---|
| 2482 | Double SAD = 0.0; |
|---|
| 2483 | |
|---|
| 2484 | Pel* pOrg = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
|---|
| 2485 | Pel* pRec = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvPred()->getLumaAddr(pcCU->getAddr(), 0); |
|---|
| 2486 | Pel* pDep = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
|---|
| 2487 | Int iStride = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getStride(); |
|---|
| 2488 | |
|---|
| 2489 | Int iWidth = m_pcLCUData[m_indexLCU].m_widthInPixel; |
|---|
| 2490 | Int iHeight = m_pcLCUData[m_indexLCU].m_heightInPixel; |
|---|
| 2491 | |
|---|
| 2492 | for( y = 0 ; y < pcCU->getSlice()->getSPS()->getMaxCUHeight() ; y+=8) |
|---|
| 2493 | { |
|---|
| 2494 | for( x = 0 ; x < pcCU->getSlice()->getSPS()->getMaxCUWidth() ; x+=8) |
|---|
| 2495 | { |
|---|
| 2496 | Sum += pDep[x]; |
|---|
| 2497 | } |
|---|
| 2498 | pDep += iStride; |
|---|
| 2499 | } |
|---|
| 2500 | |
|---|
| 2501 | Double AvgDepth = (Double)Sum/((pcCU->getSlice()->getSPS()->getMaxCUHeight()/8)*(pcCU->getSlice()->getSPS()->getMaxCUWidth()/8)); |
|---|
| 2502 | |
|---|
| 2503 | //Int iDisparity = pDepthGen->RCGetDisparityFromVirtDepth(0, (Int)AvgDepth); |
|---|
| 2504 | |
|---|
| 2505 | Double dFL = FocalLen * abs( BasePos - CurPos ); |
|---|
| 2506 | Double dZ = abs( 1.0 / Znear - 1.0 / Zfar ) * ((Double)(AvgDepth) / (( 1 << g_bitDepthY ) - 1) ) + abs(1.0 / Zfar); |
|---|
| 2507 | Int iDisparity = (Int)(Direction*dFL * dZ); |
|---|
| 2508 | Int shift = DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8); |
|---|
| 2509 | Int Disp = iDisparity; |
|---|
| 2510 | |
|---|
| 2511 | for( y = 0; y < iHeight; y++ ) |
|---|
| 2512 | { |
|---|
| 2513 | for( x = 0; x < iWidth; x++ ) |
|---|
| 2514 | { |
|---|
| 2515 | SAD += abs( pOrg[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + Disp)] |
|---|
| 2516 | - pRec[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + Disp)] )>>shift; |
|---|
| 2517 | } |
|---|
| 2518 | pOrg += iStride; |
|---|
| 2519 | pRec += iStride; |
|---|
| 2520 | } |
|---|
| 2521 | m_pcLCUData[m_indexLCU].m_qp = iQP; |
|---|
| 2522 | m_pcLCUData[m_indexLCU].m_costMAD = (SAD /(Double)(iWidth*iHeight)); |
|---|
| 2523 | m_pcLCUData[m_indexLCU].m_bits = (Int)uiBits; |
|---|
| 2524 | } |
|---|
| 2525 | #endif |
|---|
| 2526 | |
|---|
| 2527 | |
|---|
| 2528 | Double TEncRateCtrl::xAdjustmentBits(Int& reductionBits, Int& compensationBits) |
|---|
| 2529 | { |
|---|
| 2530 | Double adjustment = ADJUSTMENT_FACTOR*reductionBits; |
|---|
| 2531 | reductionBits -= (Int)adjustment; |
|---|
| 2532 | compensationBits += (Int)adjustment; |
|---|
| 2533 | |
|---|
| 2534 | return adjustment; |
|---|
| 2535 | } |
|---|
| 2536 | |
|---|
| 2537 | #endif |
|---|
| 2538 | |
|---|