[608] | 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.h |
---|
| 35 | \brief Rate control manager class |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | #ifndef _HM_TENCRATECTRL_H_ |
---|
| 39 | #define _HM_TENCRATECTRL_H_ |
---|
| 40 | |
---|
| 41 | #if _MSC_VER > 1000 |
---|
| 42 | #pragma once |
---|
| 43 | #endif // _MSC_VER > 1000 |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | #include "../TLibCommon/CommonDef.h" |
---|
| 47 | #include "../TLibCommon/TComDataCU.h" |
---|
| 48 | |
---|
| 49 | #include <vector> |
---|
| 50 | #include <algorithm> |
---|
| 51 | |
---|
| 52 | using namespace std; |
---|
| 53 | |
---|
| 54 | //! \ingroup TLibEncoder |
---|
| 55 | //! \{ |
---|
| 56 | |
---|
| 57 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
| 58 | #include "../TLibEncoder/TEncCfg.h" |
---|
| 59 | #include <list> |
---|
| 60 | #include <cassert> |
---|
| 61 | |
---|
| 62 | const Int g_RCInvalidQPValue = -999; |
---|
| 63 | const Int g_RCSmoothWindowSize = 40; |
---|
| 64 | const Int g_RCMaxPicListSize = 32; |
---|
| 65 | const Double g_RCWeightPicTargetBitInGOP = 0.9; |
---|
| 66 | const Double g_RCWeightPicRargetBitInBuffer = 1.0 - g_RCWeightPicTargetBitInGOP; |
---|
| 67 | #if M0036_RC_IMPROVEMENT |
---|
| 68 | const Int g_RCIterationNum = 20; |
---|
| 69 | const Double g_RCWeightHistoryLambda = 0.5; |
---|
| 70 | const Double g_RCWeightCurrentLambda = 1.0 - g_RCWeightHistoryLambda; |
---|
| 71 | const Int g_RCLCUSmoothWindowSize = 4; |
---|
| 72 | const Double g_RCAlphaMinValue = 0.05; |
---|
| 73 | const Double g_RCAlphaMaxValue = 500.0; |
---|
| 74 | const Double g_RCBetaMinValue = -3.0; |
---|
| 75 | const Double g_RCBetaMaxValue = -0.1; |
---|
| 76 | #endif |
---|
| 77 | |
---|
| 78 | #if RATE_CONTROL_INTRA |
---|
| 79 | #define ALPHA 6.7542; |
---|
| 80 | #define BETA1 1.2517 |
---|
| 81 | #define BETA2 1.7860 |
---|
| 82 | #endif |
---|
| 83 | |
---|
| 84 | struct TRCLCU |
---|
| 85 | { |
---|
| 86 | Int m_actualBits; |
---|
| 87 | Int m_QP; // QP of skip mode is set to g_RCInvalidQPValue |
---|
| 88 | Int m_targetBits; |
---|
| 89 | Double m_lambda; |
---|
| 90 | #if M0036_RC_IMPROVEMENT |
---|
| 91 | Double m_bitWeight; |
---|
| 92 | #else |
---|
| 93 | Double m_MAD; |
---|
| 94 | #endif |
---|
| 95 | Int m_numberOfPixel; |
---|
| 96 | #if RATE_CONTROL_INTRA |
---|
| 97 | Double m_costIntra; |
---|
| 98 | Int m_targetBitsLeft; |
---|
| 99 | #endif |
---|
[635] | 100 | |
---|
| 101 | #if KWU_RC_MADPRED_E0227 |
---|
| 102 | Double m_MAD; |
---|
| 103 | |
---|
| 104 | Int m_CUWidth; |
---|
| 105 | Int m_CUHeight; |
---|
| 106 | Double m_IVMAD; |
---|
| 107 | #endif |
---|
[608] | 108 | }; |
---|
| 109 | |
---|
| 110 | struct TRCParameter |
---|
| 111 | { |
---|
| 112 | Double m_alpha; |
---|
| 113 | Double m_beta; |
---|
| 114 | }; |
---|
| 115 | |
---|
| 116 | class TEncRCSeq |
---|
| 117 | { |
---|
| 118 | public: |
---|
| 119 | TEncRCSeq(); |
---|
| 120 | ~TEncRCSeq(); |
---|
| 121 | |
---|
| 122 | public: |
---|
| 123 | #if M0036_RC_IMPROVEMENT |
---|
| 124 | Void create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit ); |
---|
| 125 | #else |
---|
| 126 | Void create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel ); |
---|
| 127 | #endif |
---|
| 128 | Void destroy(); |
---|
| 129 | Void initBitsRatio( Int bitsRatio[] ); |
---|
| 130 | Void initGOPID2Level( Int GOPID2Level[] ); |
---|
| 131 | Void initPicPara( TRCParameter* picPara = NULL ); // NULL to initial with default value |
---|
| 132 | Void initLCUPara( TRCParameter** LCUPara = NULL ); // NULL to initial with default value |
---|
| 133 | Void updateAfterPic ( Int bits ); |
---|
| 134 | #if !RATE_CONTROL_INTRA |
---|
| 135 | Int getRefineBitsForIntra( Int orgBits ); |
---|
| 136 | #endif |
---|
| 137 | #if M0036_RC_IMPROVEMENT |
---|
| 138 | Void setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB ); |
---|
| 139 | #endif |
---|
| 140 | |
---|
| 141 | public: |
---|
| 142 | Int getTotalFrames() { return m_totalFrames; } |
---|
| 143 | Int getTargetRate() { return m_targetRate; } |
---|
| 144 | Int getFrameRate() { return m_frameRate; } |
---|
| 145 | Int getGOPSize() { return m_GOPSize; } |
---|
| 146 | Int getPicWidth() { return m_picWidth; } |
---|
| 147 | Int getPicHeight() { return m_picHeight; } |
---|
| 148 | Int getLCUWidth() { return m_LCUWidth; } |
---|
| 149 | Int getLCUHeight() { return m_LCUHeight; } |
---|
| 150 | Int getNumberOfLevel() { return m_numberOfLevel; } |
---|
| 151 | Int getAverageBits() { return m_averageBits; } |
---|
| 152 | Int getLeftAverageBits() { assert( m_framesLeft > 0 ); return (Int)(m_bitsLeft / m_framesLeft); } |
---|
| 153 | Bool getUseLCUSeparateModel() { return m_useLCUSeparateModel; } |
---|
| 154 | |
---|
| 155 | Int getNumPixel() { return m_numberOfPixel; } |
---|
| 156 | Int64 getTargetBits() { return m_targetBits; } |
---|
| 157 | Int getNumberOfLCU() { return m_numberOfLCU; } |
---|
| 158 | Int* getBitRatio() { return m_bitsRatio; } |
---|
| 159 | Int getBitRatio( Int idx ) { assert( idx<m_GOPSize); return m_bitsRatio[idx]; } |
---|
| 160 | Int* getGOPID2Level() { return m_GOPID2Level; } |
---|
| 161 | Int getGOPID2Level( Int ID ) { assert( ID < m_GOPSize ); return m_GOPID2Level[ID]; } |
---|
| 162 | TRCParameter* getPicPara() { return m_picPara; } |
---|
| 163 | TRCParameter getPicPara( Int level ) { assert( level < m_numberOfLevel ); return m_picPara[level]; } |
---|
| 164 | Void setPicPara( Int level, TRCParameter para ) { assert( level < m_numberOfLevel ); m_picPara[level] = para; } |
---|
| 165 | TRCParameter** getLCUPara() { return m_LCUPara; } |
---|
| 166 | TRCParameter* getLCUPara( Int level ) { assert( level < m_numberOfLevel ); return m_LCUPara[level]; } |
---|
| 167 | TRCParameter getLCUPara( Int level, Int LCUIdx ) { assert( LCUIdx < m_numberOfLCU ); return getLCUPara(level)[LCUIdx]; } |
---|
| 168 | Void setLCUPara( Int level, Int LCUIdx, TRCParameter para ) { assert( level < m_numberOfLevel ); assert( LCUIdx < m_numberOfLCU ); m_LCUPara[level][LCUIdx] = para; } |
---|
| 169 | |
---|
| 170 | Int getFramesLeft() { return m_framesLeft; } |
---|
| 171 | Int64 getBitsLeft() { return m_bitsLeft; } |
---|
| 172 | |
---|
| 173 | Double getSeqBpp() { return m_seqTargetBpp; } |
---|
| 174 | Double getAlphaUpdate() { return m_alphaUpdate; } |
---|
| 175 | Double getBetaUpdate() { return m_betaUpdate; } |
---|
| 176 | |
---|
| 177 | #if M0036_RC_IMPROVEMENT |
---|
| 178 | Int getAdaptiveBits() { return m_adaptiveBit; } |
---|
| 179 | Double getLastLambda() { return m_lastLambda; } |
---|
| 180 | Void setLastLambda( Double lamdba ) { m_lastLambda = lamdba; } |
---|
| 181 | #endif |
---|
| 182 | |
---|
| 183 | private: |
---|
| 184 | Int m_totalFrames; |
---|
| 185 | Int m_targetRate; |
---|
| 186 | Int m_frameRate; |
---|
| 187 | Int m_GOPSize; |
---|
| 188 | Int m_picWidth; |
---|
| 189 | Int m_picHeight; |
---|
| 190 | Int m_LCUWidth; |
---|
| 191 | Int m_LCUHeight; |
---|
| 192 | Int m_numberOfLevel; |
---|
| 193 | Int m_averageBits; |
---|
| 194 | |
---|
| 195 | Int m_numberOfPixel; |
---|
| 196 | Int64 m_targetBits; |
---|
| 197 | Int m_numberOfLCU; |
---|
| 198 | Int* m_bitsRatio; |
---|
| 199 | Int* m_GOPID2Level; |
---|
| 200 | TRCParameter* m_picPara; |
---|
| 201 | TRCParameter** m_LCUPara; |
---|
| 202 | |
---|
| 203 | Int m_framesLeft; |
---|
| 204 | Int64 m_bitsLeft; |
---|
| 205 | Double m_seqTargetBpp; |
---|
| 206 | Double m_alphaUpdate; |
---|
| 207 | Double m_betaUpdate; |
---|
| 208 | Bool m_useLCUSeparateModel; |
---|
| 209 | |
---|
| 210 | #if M0036_RC_IMPROVEMENT |
---|
| 211 | Int m_adaptiveBit; |
---|
| 212 | Double m_lastLambda; |
---|
| 213 | #endif |
---|
| 214 | }; |
---|
| 215 | |
---|
| 216 | class TEncRCGOP |
---|
| 217 | { |
---|
| 218 | public: |
---|
| 219 | TEncRCGOP(); |
---|
| 220 | ~TEncRCGOP(); |
---|
| 221 | |
---|
| 222 | public: |
---|
| 223 | Void create( TEncRCSeq* encRCSeq, Int numPic ); |
---|
| 224 | Void destroy(); |
---|
| 225 | Void updateAfterPicture( Int bitsCost ); |
---|
| 226 | |
---|
| 227 | private: |
---|
| 228 | Int xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize ); |
---|
| 229 | #if M0036_RC_IMPROVEMENT |
---|
| 230 | Void xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ); |
---|
| 231 | Double xSolveEqua( Double targetBpp, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ); |
---|
| 232 | #endif |
---|
| 233 | |
---|
| 234 | public: |
---|
| 235 | TEncRCSeq* getEncRCSeq() { return m_encRCSeq; } |
---|
| 236 | Int getNumPic() { return m_numPic;} |
---|
| 237 | Int getTargetBits() { return m_targetBits; } |
---|
| 238 | Int getPicLeft() { return m_picLeft; } |
---|
| 239 | Int getBitsLeft() { return m_bitsLeft; } |
---|
| 240 | Int getTargetBitInGOP( Int i ) { return m_picTargetBitInGOP[i]; } |
---|
| 241 | |
---|
| 242 | private: |
---|
| 243 | TEncRCSeq* m_encRCSeq; |
---|
| 244 | Int* m_picTargetBitInGOP; |
---|
| 245 | Int m_numPic; |
---|
| 246 | Int m_targetBits; |
---|
| 247 | Int m_picLeft; |
---|
| 248 | Int m_bitsLeft; |
---|
| 249 | }; |
---|
| 250 | |
---|
| 251 | class TEncRCPic |
---|
| 252 | { |
---|
| 253 | public: |
---|
| 254 | TEncRCPic(); |
---|
| 255 | ~TEncRCPic(); |
---|
| 256 | |
---|
| 257 | public: |
---|
[635] | 258 | #if KWU_RC_MADPRED_E0227 |
---|
[637] | 259 | Void create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures, Int layerID ); |
---|
[635] | 260 | #else |
---|
[608] | 261 | Void create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ); |
---|
[635] | 262 | #endif |
---|
[608] | 263 | Void destroy(); |
---|
| 264 | |
---|
[635] | 265 | #if KWU_RC_MADPRED_E0227 |
---|
[637] | 266 | Double estimatePicLambdaIV( list<TEncRCPic*>& listPreviousPictures, Int curPOC ); |
---|
[635] | 267 | #endif |
---|
[608] | 268 | #if !RATE_CONTROL_INTRA |
---|
| 269 | Double estimatePicLambda( list<TEncRCPic*>& listPreviousPictures ); |
---|
| 270 | #endif |
---|
| 271 | Int estimatePicQP ( Double lambda, list<TEncRCPic*>& listPreviousPictures ); |
---|
| 272 | #if RATE_CONTROL_INTRA |
---|
| 273 | Int getRefineBitsForIntra(Int orgBits); |
---|
| 274 | Double calculateLambdaIntra(double alpha, double beta, double MADPerPixel, double bitsPerPixel); |
---|
| 275 | Double estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType); |
---|
| 276 | |
---|
| 277 | Void updateAlphaBetaIntra(double *alpha, double *beta); |
---|
| 278 | |
---|
| 279 | Double getLCUTargetBpp(SliceType eSliceType); |
---|
| 280 | Double getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP); |
---|
| 281 | #else |
---|
| 282 | Double getLCUTargetBpp(); |
---|
| 283 | #endif |
---|
[635] | 284 | |
---|
| 285 | #if KWU_RC_MADPRED_E0227 |
---|
[637] | 286 | Double getLCUTargetBppforInterView( list<TEncRCPic*>& listPreviousPictures, TComDataCU* pcCU, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction, Int* disparity ); |
---|
[635] | 287 | #endif |
---|
| 288 | |
---|
[608] | 289 | Double getLCUEstLambda( Double bpp ); |
---|
| 290 | Int getLCUEstQP( Double lambda, Int clipPicQP ); |
---|
| 291 | |
---|
| 292 | Void updateAfterLCU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter = true ); |
---|
| 293 | #if M0036_RC_IMPROVEMENT |
---|
| 294 | #if RATE_CONTROL_INTRA |
---|
| 295 | Void updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType); |
---|
| 296 | #else |
---|
| 297 | Void updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda ); |
---|
| 298 | #endif |
---|
| 299 | #else |
---|
| 300 | Void updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage ); |
---|
| 301 | #endif |
---|
| 302 | |
---|
| 303 | Void addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ); |
---|
[635] | 304 | |
---|
| 305 | #if KWU_RC_MADPRED_E0227 |
---|
| 306 | Void addToPictureLsitIV( list<TEncRCPic*>& listPreviousPictures ); |
---|
[637] | 307 | Void setIVPic( TEncRCPic* baseRCPic ); |
---|
[635] | 308 | #endif |
---|
| 309 | |
---|
[608] | 310 | #if !M0036_RC_IMPROVEMENT |
---|
| 311 | Double getEffectivePercentage(); |
---|
| 312 | #endif |
---|
| 313 | Double calAverageQP(); |
---|
| 314 | Double calAverageLambda(); |
---|
| 315 | |
---|
| 316 | private: |
---|
| 317 | Int xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ); |
---|
| 318 | Int xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ); |
---|
| 319 | |
---|
| 320 | public: |
---|
| 321 | TEncRCSeq* getRCSequence() { return m_encRCSeq; } |
---|
| 322 | TEncRCGOP* getRCGOP() { return m_encRCGOP; } |
---|
| 323 | |
---|
| 324 | Int getFrameLevel() { return m_frameLevel; } |
---|
| 325 | Int getNumberOfPixel() { return m_numberOfPixel; } |
---|
| 326 | Int getNumberOfLCU() { return m_numberOfLCU; } |
---|
| 327 | Int getTargetBits() { return m_targetBits; } |
---|
| 328 | #if !RATE_CONTROL_INTRA |
---|
| 329 | Void setTargetBits( Int bits ) { m_targetBits = bits; } |
---|
| 330 | #endif |
---|
| 331 | Int getEstHeaderBits() { return m_estHeaderBits; } |
---|
| 332 | Int getLCULeft() { return m_LCULeft; } |
---|
| 333 | Int getBitsLeft() { return m_bitsLeft; } |
---|
| 334 | Int getPixelsLeft() { return m_pixelsLeft; } |
---|
| 335 | Int getBitsCoded() { return m_targetBits - m_estHeaderBits - m_bitsLeft; } |
---|
| 336 | Int getLCUCoded() { return m_numberOfLCU - m_LCULeft; } |
---|
| 337 | TRCLCU* getLCU() { return m_LCUs; } |
---|
| 338 | TRCLCU& getLCU( Int LCUIdx ) { return m_LCUs[LCUIdx]; } |
---|
| 339 | Int getPicActualHeaderBits() { return m_picActualHeaderBits; } |
---|
| 340 | #if !M0036_RC_IMPROVEMENT |
---|
| 341 | Double getTotalMAD() { return m_totalMAD; } |
---|
| 342 | Void setTotalMAD( Double MAD ) { m_totalMAD = MAD; } |
---|
| 343 | #endif |
---|
| 344 | #if RATE_CONTROL_INTRA |
---|
| 345 | Void setTargetBits( Int bits ) { m_targetBits = bits; m_bitsLeft = bits;} |
---|
| 346 | Void setTotalIntraCost(Double cost) { m_totalCostIntra = cost; } |
---|
| 347 | Void getLCUInitTargetBits(); |
---|
| 348 | #endif |
---|
| 349 | |
---|
[635] | 350 | #if KWU_RC_MADPRED_E0227 |
---|
| 351 | Double getTotalMAD() { return m_totalMAD; } |
---|
| 352 | Void setTotalMAD( Double MAD ) { m_totalMAD = MAD; } |
---|
| 353 | |
---|
| 354 | Double getIVTotalMAD() { return m_IVtotalMAD; } |
---|
| 355 | Void setIVTotalMAD( Double MAD ) { m_IVtotalMAD = MAD; } |
---|
| 356 | #endif |
---|
| 357 | |
---|
[608] | 358 | Int getPicActualBits() { return m_picActualBits; } |
---|
| 359 | Int getPicActualQP() { return m_picQP; } |
---|
| 360 | Double getPicActualLambda() { return m_picLambda; } |
---|
| 361 | Int getPicEstQP() { return m_estPicQP; } |
---|
| 362 | Void setPicEstQP( Int QP ) { m_estPicQP = QP; } |
---|
| 363 | Double getPicEstLambda() { return m_estPicLambda; } |
---|
| 364 | Void setPicEstLambda( Double lambda ) { m_picLambda = lambda; } |
---|
| 365 | |
---|
[635] | 366 | #if KWU_RC_MADPRED_E0227 |
---|
| 367 | Int getLayerID() { return m_LayerID; } |
---|
| 368 | Void setLayerID(Int layerid) { m_LayerID = layerid; } |
---|
| 369 | #endif |
---|
| 370 | |
---|
[608] | 371 | private: |
---|
| 372 | TEncRCSeq* m_encRCSeq; |
---|
| 373 | TEncRCGOP* m_encRCGOP; |
---|
| 374 | |
---|
| 375 | Int m_frameLevel; |
---|
| 376 | Int m_numberOfPixel; |
---|
| 377 | Int m_numberOfLCU; |
---|
| 378 | Int m_targetBits; |
---|
| 379 | Int m_estHeaderBits; |
---|
| 380 | Int m_estPicQP; |
---|
| 381 | Double m_estPicLambda; |
---|
| 382 | |
---|
| 383 | Int m_LCULeft; |
---|
| 384 | Int m_bitsLeft; |
---|
| 385 | Int m_pixelsLeft; |
---|
| 386 | |
---|
| 387 | TRCLCU* m_LCUs; |
---|
| 388 | Int m_picActualHeaderBits; // only SH and potential APS |
---|
| 389 | #if !M0036_RC_IMPROVEMENT |
---|
| 390 | Double m_totalMAD; |
---|
| 391 | #endif |
---|
| 392 | #if RATE_CONTROL_INTRA |
---|
| 393 | Double m_totalCostIntra; |
---|
| 394 | Double m_remainingCostIntra; |
---|
| 395 | #endif |
---|
| 396 | Int m_picActualBits; // the whole picture, including header |
---|
| 397 | Int m_picQP; // in integer form |
---|
| 398 | Double m_picLambda; |
---|
| 399 | #if !M0036_RC_IMPROVEMENT |
---|
| 400 | TEncRCPic* m_lastPicture; |
---|
| 401 | #endif |
---|
[635] | 402 | |
---|
| 403 | #if KWU_RC_MADPRED_E0227 |
---|
| 404 | Double m_totalMAD; |
---|
| 405 | TEncRCPic* m_lastPicture; |
---|
| 406 | Int m_LayerID; |
---|
| 407 | TEncRCPic* m_lastIVPicture; |
---|
| 408 | Double m_IVtotalMAD; |
---|
| 409 | #endif |
---|
[608] | 410 | }; |
---|
| 411 | |
---|
| 412 | class TEncRateCtrl |
---|
| 413 | { |
---|
| 414 | public: |
---|
| 415 | TEncRateCtrl(); |
---|
| 416 | ~TEncRateCtrl(); |
---|
| 417 | |
---|
| 418 | public: |
---|
| 419 | #if M0036_RC_IMPROVEMENT |
---|
[635] | 420 | #if KWU_RC_MADPRED_E0227 |
---|
[637] | 421 | Void 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 ); |
---|
[635] | 422 | #else |
---|
[608] | 423 | Void 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] ); |
---|
[635] | 424 | #endif |
---|
[608] | 425 | #else |
---|
| 426 | Void 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] ); |
---|
| 427 | #endif |
---|
| 428 | Void destroy(); |
---|
| 429 | Void initRCPic( Int frameLevel ); |
---|
| 430 | Void initRCGOP( Int numberOfPictures ); |
---|
| 431 | Void destroyRCGOP(); |
---|
| 432 | |
---|
| 433 | public: |
---|
| 434 | Void setRCQP ( Int QP ) { m_RCQP = QP; } |
---|
| 435 | Int getRCQP () { return m_RCQP; } |
---|
| 436 | TEncRCSeq* getRCSeq() { assert ( m_encRCSeq != NULL ); return m_encRCSeq; } |
---|
| 437 | TEncRCGOP* getRCGOP() { assert ( m_encRCGOP != NULL ); return m_encRCGOP; } |
---|
| 438 | TEncRCPic* getRCPic() { assert ( m_encRCPic != NULL ); return m_encRCPic; } |
---|
| 439 | list<TEncRCPic*>& getPicList() { return m_listRCPictures; } |
---|
| 440 | |
---|
[635] | 441 | #if KWU_RC_MADPRED_E0227 |
---|
| 442 | Int getLayerID() { return m_LayerID; } |
---|
| 443 | Void setLayerID(Int layerid) { m_LayerID = layerid; } |
---|
| 444 | #endif |
---|
| 445 | |
---|
[608] | 446 | private: |
---|
| 447 | TEncRCSeq* m_encRCSeq; |
---|
| 448 | TEncRCGOP* m_encRCGOP; |
---|
| 449 | TEncRCPic* m_encRCPic; |
---|
| 450 | list<TEncRCPic*> m_listRCPictures; |
---|
| 451 | Int m_RCQP; |
---|
[635] | 452 | |
---|
| 453 | #if KWU_RC_MADPRED_E0227 |
---|
| 454 | Int m_LayerID; |
---|
| 455 | #endif |
---|
[608] | 456 | }; |
---|
| 457 | |
---|
| 458 | #else |
---|
| 459 | |
---|
| 460 | // ==================================================================================================================== |
---|
| 461 | // Class definition |
---|
| 462 | // ==================================================================================================================== |
---|
| 463 | #define MAX_DELTA_QP 2 |
---|
| 464 | #define MAX_CUDQP_DEPTH 0 |
---|
| 465 | |
---|
| 466 | typedef struct FrameData |
---|
| 467 | { |
---|
| 468 | Bool m_isReferenced; |
---|
| 469 | Int m_qp; |
---|
| 470 | Int m_bits; |
---|
| 471 | Double m_costMAD; |
---|
| 472 | }FrameData; |
---|
| 473 | |
---|
| 474 | typedef struct LCUData |
---|
| 475 | { |
---|
| 476 | Int m_qp; ///< coded QP |
---|
| 477 | Int m_bits; ///< actually generated bits |
---|
| 478 | Int m_pixels; ///< number of pixels for a unit |
---|
| 479 | Int m_widthInPixel; ///< number of pixels for width |
---|
| 480 | Int m_heightInPixel; ///< number of pixels for height |
---|
| 481 | Double m_costMAD; ///< texture complexity for a unit |
---|
| 482 | }LCUData; |
---|
| 483 | |
---|
| 484 | class MADLinearModel |
---|
| 485 | { |
---|
| 486 | private: |
---|
| 487 | Bool m_activeOn; |
---|
| 488 | Double m_paramY1; |
---|
| 489 | Double m_paramY2; |
---|
| 490 | Double m_costMADs[3]; |
---|
| 491 | |
---|
| 492 | public: |
---|
| 493 | MADLinearModel () {}; |
---|
| 494 | ~MADLinearModel() {}; |
---|
| 495 | |
---|
| 496 | Void initMADLinearModel (); |
---|
| 497 | Double getMAD (); |
---|
| 498 | Void updateMADLiearModel (); |
---|
| 499 | Void updateMADHistory (Double costMAD); |
---|
| 500 | Bool IsUpdateAvailable () { return m_activeOn; } |
---|
| 501 | }; |
---|
| 502 | |
---|
| 503 | class PixelBaseURQQuadraticModel |
---|
| 504 | { |
---|
| 505 | private: |
---|
| 506 | Double m_paramHighX1; |
---|
| 507 | Double m_paramHighX2; |
---|
| 508 | Double m_paramLowX1; |
---|
| 509 | Double m_paramLowX2; |
---|
| 510 | public: |
---|
| 511 | PixelBaseURQQuadraticModel () {}; |
---|
| 512 | ~PixelBaseURQQuadraticModel() {}; |
---|
| 513 | |
---|
| 514 | Void initPixelBaseQuadraticModel (); |
---|
| 515 | Int getQP (Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD); |
---|
| 516 | Void updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD); |
---|
| 517 | Bool checkUpdateAvailable (Int qpReference ); |
---|
| 518 | Double xConvertQP2QStep (Int qp ); |
---|
| 519 | Int xConvertQStep2QP (Double qStep ); |
---|
| 520 | }; |
---|
| 521 | |
---|
| 522 | class TEncRateCtrl |
---|
| 523 | { |
---|
| 524 | private: |
---|
| 525 | Bool m_isLowdelay; |
---|
| 526 | Int m_prevBitrate; |
---|
| 527 | Int m_currBitrate; |
---|
| 528 | Int m_frameRate; |
---|
| 529 | Int m_refFrameNum; |
---|
| 530 | Int m_nonRefFrameNum; |
---|
| 531 | Int m_numOfPixels; |
---|
| 532 | Int m_sourceWidthInLCU; |
---|
| 533 | Int m_sourceHeightInLCU; |
---|
| 534 | Int m_sizeGOP; |
---|
| 535 | Int m_indexGOP; |
---|
| 536 | Int m_indexFrame; |
---|
| 537 | Int m_indexLCU; |
---|
| 538 | Int m_indexUnit; |
---|
| 539 | Int m_indexRefFrame; |
---|
| 540 | Int m_indexNonRefFrame; |
---|
| 541 | Int m_indexPOCInGOP; |
---|
| 542 | Int m_indexPrevPOCInGOP; |
---|
| 543 | Int m_occupancyVB; |
---|
| 544 | Int m_initialOVB; |
---|
| 545 | Int m_targetBufLevel; |
---|
| 546 | Int m_initialTBL; |
---|
| 547 | Int m_remainingBitsInGOP; |
---|
| 548 | Int m_remainingBitsInFrame; |
---|
| 549 | Int m_occupancyVBInFrame; |
---|
| 550 | Int m_targetBits; |
---|
| 551 | Int m_numUnitInFrame; |
---|
| 552 | Int m_codedPixels; |
---|
| 553 | Bool m_activeUnitLevelOn; |
---|
| 554 | Double m_costNonRefAvgWeighting; |
---|
| 555 | Double m_costRefAvgWeighting; |
---|
| 556 | Double m_costAvgbpp; |
---|
| 557 | |
---|
| 558 | FrameData* m_pcFrameData; |
---|
| 559 | LCUData* m_pcLCUData; |
---|
| 560 | |
---|
| 561 | MADLinearModel m_cMADLinearModel; |
---|
| 562 | PixelBaseURQQuadraticModel m_cPixelURQQuadraticModel; |
---|
| 563 | |
---|
| 564 | public: |
---|
| 565 | TEncRateCtrl () {}; |
---|
| 566 | virtual ~TEncRateCtrl() {}; |
---|
| 567 | |
---|
| 568 | Void create (Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight); |
---|
| 569 | Void destroy (); |
---|
| 570 | |
---|
| 571 | Void initFrameData (Int qp = 0); |
---|
| 572 | Void initUnitData (Int qp = 0); |
---|
| 573 | Int getFrameQP (Bool isReferenced, Int POC); |
---|
| 574 | Bool calculateUnitQP (); |
---|
| 575 | Int getUnitQP () { return m_pcLCUData[m_indexLCU].m_qp; } |
---|
| 576 | Void updateRCGOPStatus (); |
---|
| 577 | Void updataRCFrameStatus (Int frameBits, SliceType eSliceType); |
---|
| 578 | Void updataRCUnitStatus (); |
---|
| 579 | Void updateLCUData (TComDataCU* pcCU, UInt64 actualLCUBits, Int qp); |
---|
[635] | 580 | #if KWU_RC_MADPRED_E0227 |
---|
[637] | 581 | Void updateLCUDataEnhancedView(TComDataCU* pcCU, UInt64 uiBits, Int qp, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction); |
---|
[635] | 582 | #endif |
---|
[608] | 583 | Void updateFrameData (UInt64 actualFrameBits); |
---|
| 584 | Double xAdjustmentBits (Int& reductionBits, Int& compensationBits); |
---|
| 585 | Int getGOPId () { return m_indexFrame; } |
---|
| 586 | }; |
---|
| 587 | #endif |
---|
| 588 | |
---|
| 589 | #endif |
---|
| 590 | |
---|
| 591 | |
---|