HEVC Test Model (HM)  HM-16.3
TEncRateCtrl.h
Go to the documentation of this file.
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-2015, 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 
38 #ifndef __TENCRATECTRL__
39 #define __TENCRATECTRL__
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 
56 
57 #include "../TLibEncoder/TEncCfg.h"
58 #include <list>
59 #include <cassert>
60 
61 const Int g_RCInvalidQPValue = -999;
66 const Int g_RCIterationNum = 20;
71 const Double g_RCAlphaMaxValue = 500.0;
72 const Double g_RCBetaMinValue = -3.0;
73 const Double g_RCBetaMaxValue = -0.1;
74 
75 #define ALPHA 6.7542;
76 #define BETA1 1.2517
77 #define BETA2 1.7860
78 
79 struct TRCLCU
80 {
82  Int m_QP; // QP of skip mode is set to g_RCInvalidQPValue
89 };
90 
92 {
95 };
96 
97 class TEncRCSeq
98 {
99 public:
100  TEncRCSeq();
101  ~TEncRCSeq();
102 
103 public:
104  Void create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit );
105  Void destroy();
106  Void initBitsRatio( Int bitsRatio[] );
107  Void initGOPID2Level( Int GOPID2Level[] );
108  Void initPicPara( TRCParameter* picPara = NULL ); // NULL to initial with default value
109  Void initLCUPara( TRCParameter** LCUPara = NULL ); // NULL to initial with default value
110  Void updateAfterPic ( Int bits );
111  Void setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB );
112 
113 public:
114  Int getTotalFrames() { return m_totalFrames; }
115  Int getTargetRate() { return m_targetRate; }
116  Int getFrameRate() { return m_frameRate; }
117  Int getGOPSize() { return m_GOPSize; }
118  Int getPicWidth() { return m_picWidth; }
119  Int getPicHeight() { return m_picHeight; }
120  Int getLCUWidth() { return m_LCUWidth; }
121  Int getLCUHeight() { return m_LCUHeight; }
122  Int getNumberOfLevel() { return m_numberOfLevel; }
123  Int getAverageBits() { return m_averageBits; }
124  Int getLeftAverageBits() { assert( m_framesLeft > 0 ); return (Int)(m_bitsLeft / m_framesLeft); }
125  Bool getUseLCUSeparateModel() { return m_useLCUSeparateModel; }
126 
127  Int getNumPixel() { return m_numberOfPixel; }
128  Int64 getTargetBits() { return m_targetBits; }
129  Int getNumberOfLCU() { return m_numberOfLCU; }
130  Int* getBitRatio() { return m_bitsRatio; }
131  Int getBitRatio( Int idx ) { assert( idx<m_GOPSize); return m_bitsRatio[idx]; }
132  Int* getGOPID2Level() { return m_GOPID2Level; }
133  Int getGOPID2Level( Int ID ) { assert( ID < m_GOPSize ); return m_GOPID2Level[ID]; }
134  TRCParameter* getPicPara() { return m_picPara; }
135  TRCParameter getPicPara( Int level ) { assert( level < m_numberOfLevel ); return m_picPara[level]; }
136  Void setPicPara( Int level, TRCParameter para ) { assert( level < m_numberOfLevel ); m_picPara[level] = para; }
137  TRCParameter** getLCUPara() { return m_LCUPara; }
138  TRCParameter* getLCUPara( Int level ) { assert( level < m_numberOfLevel ); return m_LCUPara[level]; }
139  TRCParameter getLCUPara( Int level, Int LCUIdx ) { assert( LCUIdx < m_numberOfLCU ); return getLCUPara(level)[LCUIdx]; }
140  Void setLCUPara( Int level, Int LCUIdx, TRCParameter para ) { assert( level < m_numberOfLevel ); assert( LCUIdx < m_numberOfLCU ); m_LCUPara[level][LCUIdx] = para; }
141 
142  Int getFramesLeft() { return m_framesLeft; }
143  Int64 getBitsLeft() { return m_bitsLeft; }
144 
145  Double getSeqBpp() { return m_seqTargetBpp; }
146  Double getAlphaUpdate() { return m_alphaUpdate; }
147  Double getBetaUpdate() { return m_betaUpdate; }
148 
149  Int getAdaptiveBits() { return m_adaptiveBit; }
150  Double getLastLambda() { return m_lastLambda; }
151  Void setLastLambda( Double lamdba ) { m_lastLambda = lamdba; }
152 
153 private:
164 
172 
179 
182 };
183 
185 {
186 public:
187  TEncRCGOP();
188  ~TEncRCGOP();
189 
190 public:
191  Void create( TEncRCSeq* encRCSeq, Int numPic );
192  Void destroy();
193  Void updateAfterPicture( Int bitsCost );
194 
195 private:
196  Int xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize );
197  Void xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize );
198  Double xSolveEqua( Double targetBpp, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize );
199 
200 public:
201  TEncRCSeq* getEncRCSeq() { return m_encRCSeq; }
202  Int getNumPic() { return m_numPic;}
203  Int getTargetBits() { return m_targetBits; }
204  Int getPicLeft() { return m_picLeft; }
205  Int getBitsLeft() { return m_bitsLeft; }
206  Int getTargetBitInGOP( Int i ) { return m_picTargetBitInGOP[i]; }
207 
208 private:
215 };
216 
218 {
219 public:
220  TEncRCPic();
221  ~TEncRCPic();
222 
223 public:
224  Void create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures );
225  Void destroy();
226 
227  Int estimatePicQP ( Double lambda, list<TEncRCPic*>& listPreviousPictures );
228  Int getRefineBitsForIntra(Int orgBits);
229  Double calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel);
230  Double estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType);
231 
232  Void updateAlphaBetaIntra(Double *alpha, Double *beta);
233 
234  Double getLCUTargetBpp(SliceType eSliceType);
235  Double getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP);
236  Double getLCUEstLambda( Double bpp );
237  Int getLCUEstQP( Double lambda, Int clipPicQP );
238 
239  Void updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter = true );
240  Void updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType);
241 
242  Void addToPictureLsit( list<TEncRCPic*>& listPreviousPictures );
243  Double calAverageQP();
244  Double calAverageLambda();
245 
246 private:
247  Int xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP );
248  Int xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel );
249 
250 public:
251  TEncRCSeq* getRCSequence() { return m_encRCSeq; }
252  TEncRCGOP* getRCGOP() { return m_encRCGOP; }
253 
254  Int getFrameLevel() { return m_frameLevel; }
255  Int getNumberOfPixel() { return m_numberOfPixel; }
256  Int getNumberOfLCU() { return m_numberOfLCU; }
257  Int getTargetBits() { return m_targetBits; }
258  Int getEstHeaderBits() { return m_estHeaderBits; }
259  Int getLCULeft() { return m_LCULeft; }
260  Int getBitsLeft() { return m_bitsLeft; }
261  Int getPixelsLeft() { return m_pixelsLeft; }
262  Int getBitsCoded() { return m_targetBits - m_estHeaderBits - m_bitsLeft; }
263  Int getLCUCoded() { return m_numberOfLCU - m_LCULeft; }
264  TRCLCU* getLCU() { return m_LCUs; }
265  TRCLCU& getLCU( Int LCUIdx ) { return m_LCUs[LCUIdx]; }
266  Int getPicActualHeaderBits() { return m_picActualHeaderBits; }
267  Void setTargetBits( Int bits ) { m_targetBits = bits; m_bitsLeft = bits;}
268  Void setTotalIntraCost(Double cost) { m_totalCostIntra = cost; }
269  Void getLCUInitTargetBits();
270 
271  Int getPicActualBits() { return m_picActualBits; }
272  Int getPicActualQP() { return m_picQP; }
273  Double getPicActualLambda() { return m_picLambda; }
274  Int getPicEstQP() { return m_estPicQP; }
275  Void setPicEstQP( Int QP ) { m_estPicQP = QP; }
276  Double getPicEstLambda() { return m_estPicLambda; }
277  Void setPicEstLambda( Double lambda ) { m_picLambda = lambda; }
278 
279 private:
282 
290 
294 
296  Int m_picActualHeaderBits; // only SH and potential APS
299  Int m_picActualBits; // the whole picture, including header
300  Int m_picQP; // in integer form
302 };
303 
305 {
306 public:
307  TEncRateCtrl();
308  ~TEncRateCtrl();
309 
310 public:
311  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] );
312  Void destroy();
313  Void initRCPic( Int frameLevel );
314  Void initRCGOP( Int numberOfPictures );
315  Void destroyRCGOP();
316 
317 public:
318  Void setRCQP ( Int QP ) { m_RCQP = QP; }
319  Int getRCQP () { return m_RCQP; }
320  TEncRCSeq* getRCSeq() { assert ( m_encRCSeq != NULL ); return m_encRCSeq; }
321  TEncRCGOP* getRCGOP() { assert ( m_encRCGOP != NULL ); return m_encRCGOP; }
322  TEncRCPic* getRCPic() { assert ( m_encRCPic != NULL ); return m_encRCPic; }
323  list<TEncRCPic*>& getPicList() { return m_listRCPictures; }
324 
325 private:
329  list<TEncRCPic*> m_listRCPictures;
331 };
332 
333 #endif
334 
335 
Int m_targetBits
Definition: TEncRateCtrl.h:286
Int m_totalFrames
Definition: TEncRateCtrl.h:154
Int m_numberOfPixel
Definition: TEncRateCtrl.h:86
Double m_betaUpdate
Definition: TEncRateCtrl.h:177
Int getPixelsLeft()
Definition: TEncRateCtrl.h:261
Int m_picHeight
Definition: TEncRateCtrl.h:159
const Double g_RCBetaMaxValue
Definition: TEncRateCtrl.h:73
Int getPicActualBits()
Definition: TEncRateCtrl.h:271
SliceType
supported slice type
Definition: TypeDef.h:343
Double m_bitWeight
Definition: TEncRateCtrl.h:85
Double m_beta
Definition: TEncRateCtrl.h:94
Int getGOPSize()
Definition: TEncRateCtrl.h:117
Int getNumberOfLevel()
Definition: TEncRateCtrl.h:122
TRCLCU * getLCU()
Definition: TEncRateCtrl.h:264
Int m_bitsLeft
Definition: TEncRateCtrl.h:214
Void setPicEstLambda(Double lambda)
Definition: TEncRateCtrl.h:277
Int getNumberOfPixel()
Definition: TEncRateCtrl.h:255
Double m_lambda
Definition: TEncRateCtrl.h:84
void Void
Definition: TypeDef.h:285
Int getPicLeft()
Definition: TEncRateCtrl.h:204
Int m_numberOfPixel
Definition: TEncRateCtrl.h:284
TEncRCSeq * m_encRCSeq
Definition: TEncRateCtrl.h:326
#define NULL
Definition: CommonDef.h:100
Int getLCUHeight()
Definition: TEncRateCtrl.h:121
Void setPicEstQP(Int QP)
Definition: TEncRateCtrl.h:275
Double m_remainingCostIntra
Definition: TEncRateCtrl.h:298
Int m_pixelsLeft
Definition: TEncRateCtrl.h:293
Int m_LCUHeight
Definition: TEncRateCtrl.h:161
Double m_totalCostIntra
Definition: TEncRateCtrl.h:297
TEncRCGOP * m_encRCGOP
Definition: TEncRateCtrl.h:281
TEncRCGOP * getRCGOP()
Definition: TEncRateCtrl.h:321
const Double g_RCAlphaMaxValue
Definition: TEncRateCtrl.h:71
const Double g_RCAlphaMinValue
Definition: TEncRateCtrl.h:70
Double getPicActualLambda()
Definition: TEncRateCtrl.h:273
Int getPicWidth()
Definition: TEncRateCtrl.h:118
Double getSeqBpp()
Definition: TEncRateCtrl.h:145
Int * getBitRatio()
Definition: TEncRateCtrl.h:130
Int getLCUCoded()
Definition: TEncRateCtrl.h:263
TEncRCSeq * m_encRCSeq
Definition: TEncRateCtrl.h:209
STL namespace.
Int64 getBitsLeft()
Definition: TEncRateCtrl.h:143
TEncRCSeq * getRCSeq()
Definition: TEncRateCtrl.h:320
Definition: TEncCfg.h:49
Int m_picActualBits
Definition: TEncRateCtrl.h:299
Double m_lastLambda
Definition: TEncRateCtrl.h:181
Int m_targetBits
Definition: TEncRateCtrl.h:83
TRCParameter getLCUPara(Int level, Int LCUIdx)
Definition: TEncRateCtrl.h:139
const Int g_RCInvalidQPValue
Definition: TEncRateCtrl.h:61
Int getLeftAverageBits()
Definition: TEncRateCtrl.h:124
Int getGOPID2Level(Int ID)
Definition: TEncRateCtrl.h:133
TEncRCPic * getRCPic()
Definition: TEncRateCtrl.h:322
TRCLCU & getLCU(Int LCUIdx)
Definition: TEncRateCtrl.h:265
Int getTargetBits()
Definition: TEncRateCtrl.h:203
Int getNumberOfLCU()
Definition: TEncRateCtrl.h:129
Int * m_GOPID2Level
Definition: TEncRateCtrl.h:169
Double m_costIntra
Definition: TEncRateCtrl.h:87
Int getFramesLeft()
Definition: TEncRateCtrl.h:142
Int getFrameLevel()
Definition: TEncRateCtrl.h:254
const Double g_RCWeightPicTargetBitInGOP
Definition: TEncRateCtrl.h:64
Void setPicPara(Int level, TRCParameter para)
Definition: TEncRateCtrl.h:136
Int getBitsCoded()
Definition: TEncRateCtrl.h:262
Double getPicEstLambda()
Definition: TEncRateCtrl.h:276
Int getNumPic()
Definition: TEncRateCtrl.h:202
list< TEncRCPic * > m_listRCPictures
Definition: TEncRateCtrl.h:329
Int m_numberOfLCU
Definition: TEncRateCtrl.h:285
#define MAX_GOP
max. value of hierarchical GOP size
Definition: CommonDef.h:110
Void setTotalIntraCost(Double cost)
Definition: TEncRateCtrl.h:268
Int m_estPicQP
Definition: TEncRateCtrl.h:288
Int * getGOPID2Level()
Definition: TEncRateCtrl.h:132
Int getBitsLeft()
Definition: TEncRateCtrl.h:205
const Int g_RCMaxPicListSize
Definition: TEncRateCtrl.h:63
TRCParameter * getPicPara()
Definition: TEncRateCtrl.h:134
Int m_frameLevel
Definition: TEncRateCtrl.h:283
bool Bool
Definition: TypeDef.h:286
Int * m_picTargetBitInGOP
Definition: TEncRateCtrl.h:210
long long Int64
Definition: TypeDef.h:317
const Int g_RCIterationNum
Definition: TEncRateCtrl.h:66
Int getLCULeft()
Definition: TEncRateCtrl.h:259
TEncRCSeq * m_encRCSeq
Definition: TEncRateCtrl.h:280
Int * m_bitsRatio
Definition: TEncRateCtrl.h:168
Int getNumPixel()
Definition: TEncRateCtrl.h:127
Int m_targetBits
Definition: TEncRateCtrl.h:212
Int getEstHeaderBits()
Definition: TEncRateCtrl.h:258
Int getPicHeight()
Definition: TEncRateCtrl.h:119
Int getBitsLeft()
Definition: TEncRateCtrl.h:260
TRCLCU * m_LCUs
Definition: TEncRateCtrl.h:295
Int getLCUWidth()
Definition: TEncRateCtrl.h:120
Bool m_useLCUSeparateModel
Definition: TEncRateCtrl.h:178
Double m_picLambda
Definition: TEncRateCtrl.h:301
const Double g_RCWeightPicRargetBitInBuffer
Definition: TEncRateCtrl.h:65
Int64 m_bitsLeft
Definition: TEncRateCtrl.h:174
TEncRCSeq * getRCSequence()
Definition: TEncRateCtrl.h:251
Double getAlphaUpdate()
Definition: TEncRateCtrl.h:146
const Int g_RCSmoothWindowSize
Definition: TEncRateCtrl.h:62
Double m_estPicLambda
Definition: TEncRateCtrl.h:289
Int64 m_targetBits
Definition: TEncRateCtrl.h:166
TEncRCGOP * getRCGOP()
Definition: TEncRateCtrl.h:252
Int m_estHeaderBits
Definition: TEncRateCtrl.h:287
Double getBetaUpdate()
Definition: TEncRateCtrl.h:147
TEncRCPic * m_encRCPic
Definition: TEncRateCtrl.h:328
Double getLastLambda()
Definition: TEncRateCtrl.h:150
Int m_picActualHeaderBits
Definition: TEncRateCtrl.h:296
Int m_numberOfLCU
Definition: TEncRateCtrl.h:167
TRCParameter ** m_LCUPara
Definition: TEncRateCtrl.h:171
Int getFrameRate()
Definition: TEncRateCtrl.h:116
Int m_bitsLeft
Definition: TEncRateCtrl.h:292
Int getNumberOfLCU()
Definition: TEncRateCtrl.h:256
Void setLCUPara(Int level, Int LCUIdx, TRCParameter para)
Definition: TEncRateCtrl.h:140
TRCParameter * getLCUPara(Int level)
Definition: TEncRateCtrl.h:138
Int getTargetRate()
Definition: TEncRateCtrl.h:115
const Double g_RCWeightHistoryLambda
Definition: TEncRateCtrl.h:67
Int m_frameRate
Definition: TEncRateCtrl.h:156
Int m_numberOfLevel
Definition: TEncRateCtrl.h:162
Int m_targetRate
Definition: TEncRateCtrl.h:155
const Double g_RCWeightCurrentLambda
Definition: TEncRateCtrl.h:68
Int m_picWidth
Definition: TEncRateCtrl.h:158
Void setTargetBits(Int bits)
Definition: TEncRateCtrl.h:267
Bool getUseLCUSeparateModel()
Definition: TEncRateCtrl.h:125
Int m_LCUWidth
Definition: TEncRateCtrl.h:160
Void setRCQP(Int QP)
Definition: TEncRateCtrl.h:318
Int m_targetBitsLeft
Definition: TEncRateCtrl.h:88
Int m_QP
Definition: TEncRateCtrl.h:82
Int getTargetBitInGOP(Int i)
Definition: TEncRateCtrl.h:206
Int m_actualBits
Definition: TEncRateCtrl.h:81
Int64 getTargetBits()
Definition: TEncRateCtrl.h:128
Int m_numberOfPixel
Definition: TEncRateCtrl.h:165
Int m_framesLeft
Definition: TEncRateCtrl.h:173
int Int
Definition: TypeDef.h:296
Int getAdaptiveBits()
Definition: TEncRateCtrl.h:149
TRCParameter getPicPara(Int level)
Definition: TEncRateCtrl.h:135
Int m_averageBits
Definition: TEncRateCtrl.h:163
Void setLastLambda(Double lamdba)
Definition: TEncRateCtrl.h:151
TRCParameter * m_picPara
Definition: TEncRateCtrl.h:170
Int getPicActualHeaderBits()
Definition: TEncRateCtrl.h:266
list< TEncRCPic * > & getPicList()
Definition: TEncRateCtrl.h:323
Int getTargetBits()
Definition: TEncRateCtrl.h:257
Int getAverageBits()
Definition: TEncRateCtrl.h:123
Double m_alphaUpdate
Definition: TEncRateCtrl.h:176
double Double
Definition: TypeDef.h:298
Double m_seqTargetBpp
Definition: TEncRateCtrl.h:175
const Int g_RCLCUSmoothWindowSize
Definition: TEncRateCtrl.h:69
Int m_adaptiveBit
Definition: TEncRateCtrl.h:180
Double m_alpha
Definition: TEncRateCtrl.h:93
Int getPicEstQP()
Definition: TEncRateCtrl.h:274
Int getTotalFrames()
Definition: TEncRateCtrl.h:114
const Double g_RCBetaMinValue
Definition: TEncRateCtrl.h:72
Int getBitRatio(Int idx)
Definition: TEncRateCtrl.h:131
Int getPicActualQP()
Definition: TEncRateCtrl.h:272
TEncRCSeq * getEncRCSeq()
Definition: TEncRateCtrl.h:201
TEncRCGOP * m_encRCGOP
Definition: TEncRateCtrl.h:327
TRCParameter ** getLCUPara()
Definition: TEncRateCtrl.h:137