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-2017, 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 __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 | |
---|
54 | //! \ingroup TLibEncoder |
---|
55 | //! \{ |
---|
56 | |
---|
57 | #include "../TLibEncoder/TEncCfg.h" |
---|
58 | #include <list> |
---|
59 | #include <cassert> |
---|
60 | |
---|
61 | const Int g_RCInvalidQPValue = -999; |
---|
62 | const Int g_RCSmoothWindowSize = 40; |
---|
63 | const Int g_RCMaxPicListSize = 32; |
---|
64 | const Double g_RCWeightPicTargetBitInGOP = 0.9; |
---|
65 | const Double g_RCWeightPicRargetBitInBuffer = 1.0 - g_RCWeightPicTargetBitInGOP; |
---|
66 | const Int g_RCIterationNum = 20; |
---|
67 | const Double g_RCWeightHistoryLambda = 0.5; |
---|
68 | const Double g_RCWeightCurrentLambda = 1.0 - g_RCWeightHistoryLambda; |
---|
69 | const Int g_RCLCUSmoothWindowSize = 4; |
---|
70 | const Double g_RCAlphaMinValue = 0.05; |
---|
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 | { |
---|
81 | Int m_actualBits; |
---|
82 | Int m_QP; // QP of skip mode is set to g_RCInvalidQPValue |
---|
83 | Int m_targetBits; |
---|
84 | Double m_lambda; |
---|
85 | Double m_bitWeight; |
---|
86 | Int m_numberOfPixel; |
---|
87 | Double m_costIntra; |
---|
88 | Int m_targetBitsLeft; |
---|
89 | #if KWU_RC_MADPRED_E0227 |
---|
90 | Double m_MAD; |
---|
91 | Int m_CUWidth; |
---|
92 | Int m_CUHeight; |
---|
93 | Double m_IVMAD; |
---|
94 | #endif |
---|
95 | }; |
---|
96 | |
---|
97 | struct TRCParameter |
---|
98 | { |
---|
99 | Double m_alpha; |
---|
100 | Double m_beta; |
---|
101 | }; |
---|
102 | |
---|
103 | class TEncRCSeq |
---|
104 | { |
---|
105 | public: |
---|
106 | TEncRCSeq(); |
---|
107 | ~TEncRCSeq(); |
---|
108 | |
---|
109 | public: |
---|
110 | Void create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit ); |
---|
111 | Void destroy(); |
---|
112 | Void initBitsRatio( Int bitsRatio[] ); |
---|
113 | Void initGOPID2Level( Int GOPID2Level[] ); |
---|
114 | Void initPicPara( TRCParameter* picPara = NULL ); // NULL to initial with default value |
---|
115 | Void initLCUPara( TRCParameter** LCUPara = NULL ); // NULL to initial with default value |
---|
116 | Void updateAfterPic ( Int bits ); |
---|
117 | Void setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB ); |
---|
118 | |
---|
119 | public: |
---|
120 | Int getTotalFrames() { return m_totalFrames; } |
---|
121 | Int getTargetRate() { return m_targetRate; } |
---|
122 | Int getFrameRate() { return m_frameRate; } |
---|
123 | Int getGOPSize() { return m_GOPSize; } |
---|
124 | Int getPicWidth() { return m_picWidth; } |
---|
125 | Int getPicHeight() { return m_picHeight; } |
---|
126 | Int getLCUWidth() { return m_LCUWidth; } |
---|
127 | Int getLCUHeight() { return m_LCUHeight; } |
---|
128 | Int getNumberOfLevel() { return m_numberOfLevel; } |
---|
129 | Int getAverageBits() { return m_averageBits; } |
---|
130 | Int getLeftAverageBits() { assert( m_framesLeft > 0 ); return (Int)(m_bitsLeft / m_framesLeft); } |
---|
131 | Bool getUseLCUSeparateModel() { return m_useLCUSeparateModel; } |
---|
132 | |
---|
133 | Int getNumPixel() { return m_numberOfPixel; } |
---|
134 | Int64 getTargetBits() { return m_targetBits; } |
---|
135 | Int getNumberOfLCU() { return m_numberOfLCU; } |
---|
136 | Int* getBitRatio() { return m_bitsRatio; } |
---|
137 | Int getBitRatio( Int idx ) { assert( idx<m_GOPSize); return m_bitsRatio[idx]; } |
---|
138 | Int* getGOPID2Level() { return m_GOPID2Level; } |
---|
139 | Int getGOPID2Level( Int ID ) { assert( ID < m_GOPSize ); return m_GOPID2Level[ID]; } |
---|
140 | TRCParameter* getPicPara() { return m_picPara; } |
---|
141 | TRCParameter getPicPara( Int level ) { assert( level < m_numberOfLevel ); return m_picPara[level]; } |
---|
142 | Void setPicPara( Int level, TRCParameter para ) { assert( level < m_numberOfLevel ); m_picPara[level] = para; } |
---|
143 | TRCParameter** getLCUPara() { return m_LCUPara; } |
---|
144 | TRCParameter* getLCUPara( Int level ) { assert( level < m_numberOfLevel ); return m_LCUPara[level]; } |
---|
145 | TRCParameter getLCUPara( Int level, Int LCUIdx ) { assert( LCUIdx < m_numberOfLCU ); return getLCUPara(level)[LCUIdx]; } |
---|
146 | Void setLCUPara( Int level, Int LCUIdx, TRCParameter para ) { assert( level < m_numberOfLevel ); assert( LCUIdx < m_numberOfLCU ); m_LCUPara[level][LCUIdx] = para; } |
---|
147 | |
---|
148 | Int getFramesLeft() { return m_framesLeft; } |
---|
149 | Int64 getBitsLeft() { return m_bitsLeft; } |
---|
150 | |
---|
151 | Double getSeqBpp() { return m_seqTargetBpp; } |
---|
152 | Double getAlphaUpdate() { return m_alphaUpdate; } |
---|
153 | Double getBetaUpdate() { return m_betaUpdate; } |
---|
154 | |
---|
155 | Int getAdaptiveBits() { return m_adaptiveBit; } |
---|
156 | Double getLastLambda() { return m_lastLambda; } |
---|
157 | Void setLastLambda( Double lamdba ) { m_lastLambda = lamdba; } |
---|
158 | |
---|
159 | private: |
---|
160 | Int m_totalFrames; |
---|
161 | Int m_targetRate; |
---|
162 | Int m_frameRate; |
---|
163 | Int m_GOPSize; |
---|
164 | Int m_picWidth; |
---|
165 | Int m_picHeight; |
---|
166 | Int m_LCUWidth; |
---|
167 | Int m_LCUHeight; |
---|
168 | Int m_numberOfLevel; |
---|
169 | Int m_averageBits; |
---|
170 | |
---|
171 | Int m_numberOfPixel; |
---|
172 | Int64 m_targetBits; |
---|
173 | Int m_numberOfLCU; |
---|
174 | Int* m_bitsRatio; |
---|
175 | Int* m_GOPID2Level; |
---|
176 | TRCParameter* m_picPara; |
---|
177 | TRCParameter** m_LCUPara; |
---|
178 | |
---|
179 | Int m_framesLeft; |
---|
180 | Int64 m_bitsLeft; |
---|
181 | Double m_seqTargetBpp; |
---|
182 | Double m_alphaUpdate; |
---|
183 | Double m_betaUpdate; |
---|
184 | Bool m_useLCUSeparateModel; |
---|
185 | |
---|
186 | Int m_adaptiveBit; |
---|
187 | Double m_lastLambda; |
---|
188 | }; |
---|
189 | |
---|
190 | class TEncRCGOP |
---|
191 | { |
---|
192 | public: |
---|
193 | TEncRCGOP(); |
---|
194 | ~TEncRCGOP(); |
---|
195 | |
---|
196 | public: |
---|
197 | Void create( TEncRCSeq* encRCSeq, Int numPic ); |
---|
198 | Void destroy(); |
---|
199 | Void updateAfterPicture( Int bitsCost ); |
---|
200 | |
---|
201 | private: |
---|
202 | Int xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize ); |
---|
203 | Void xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ); |
---|
204 | Double xSolveEqua( Double targetBpp, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ); |
---|
205 | |
---|
206 | public: |
---|
207 | TEncRCSeq* getEncRCSeq() { return m_encRCSeq; } |
---|
208 | Int getNumPic() { return m_numPic;} |
---|
209 | Int getTargetBits() { return m_targetBits; } |
---|
210 | Int getPicLeft() { return m_picLeft; } |
---|
211 | Int getBitsLeft() { return m_bitsLeft; } |
---|
212 | Int getTargetBitInGOP( Int i ) { return m_picTargetBitInGOP[i]; } |
---|
213 | |
---|
214 | private: |
---|
215 | TEncRCSeq* m_encRCSeq; |
---|
216 | Int* m_picTargetBitInGOP; |
---|
217 | Int m_numPic; |
---|
218 | Int m_targetBits; |
---|
219 | Int m_picLeft; |
---|
220 | Int m_bitsLeft; |
---|
221 | }; |
---|
222 | |
---|
223 | class TEncRCPic |
---|
224 | { |
---|
225 | public: |
---|
226 | TEncRCPic(); |
---|
227 | ~TEncRCPic(); |
---|
228 | |
---|
229 | public: |
---|
230 | #if KWU_RC_MADPRED_E0227 |
---|
231 | Void create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures, Int layerID ); |
---|
232 | #else |
---|
233 | Void create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ); |
---|
234 | #endif |
---|
235 | Void destroy(); |
---|
236 | |
---|
237 | #if KWU_RC_MADPRED_E0227 |
---|
238 | Double estimatePicLambdaIV( list<TEncRCPic*>& listPreviousPictures, Int curPOC ); |
---|
239 | #endif |
---|
240 | Int estimatePicQP ( Double lambda, list<TEncRCPic*>& listPreviousPictures ); |
---|
241 | Int getRefineBitsForIntra(Int orgBits); |
---|
242 | Double calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel); |
---|
243 | Double estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType); |
---|
244 | |
---|
245 | Void updateAlphaBetaIntra(Double *alpha, Double *beta); |
---|
246 | |
---|
247 | Double getLCUTargetBpp(SliceType eSliceType); |
---|
248 | Double getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP); |
---|
249 | #if KWU_RC_MADPRED_E0227 |
---|
250 | Double getLCUTargetBppforInterView( list<TEncRCPic*>& listPreviousPictures, TComDataCU* pcCU, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction, Int* disparity ); |
---|
251 | #endif |
---|
252 | Double getLCUEstLambda( Double bpp ); |
---|
253 | Int getLCUEstQP( Double lambda, Int clipPicQP ); |
---|
254 | |
---|
255 | Void updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter = true ); |
---|
256 | Void updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType); |
---|
257 | |
---|
258 | Void addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ); |
---|
259 | #if KWU_RC_MADPRED_E0227 |
---|
260 | Void addToPictureLsitIV( list<TEncRCPic*>& listPreviousPictures ); |
---|
261 | Void setIVPic( TEncRCPic* baseRCPic ); |
---|
262 | #endif |
---|
263 | Double calAverageQP(); |
---|
264 | Double calAverageLambda(); |
---|
265 | |
---|
266 | private: |
---|
267 | Int xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ); |
---|
268 | Int xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ); |
---|
269 | Int xEstPicLowerBound( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ); |
---|
270 | |
---|
271 | public: |
---|
272 | TEncRCSeq* getRCSequence() { return m_encRCSeq; } |
---|
273 | TEncRCGOP* getRCGOP() { return m_encRCGOP; } |
---|
274 | |
---|
275 | Int getFrameLevel() { return m_frameLevel; } |
---|
276 | Int getNumberOfPixel() { return m_numberOfPixel; } |
---|
277 | Int getNumberOfLCU() { return m_numberOfLCU; } |
---|
278 | Int getTargetBits() { return m_targetBits; } |
---|
279 | Int getEstHeaderBits() { return m_estHeaderBits; } |
---|
280 | Int getLCULeft() { return m_LCULeft; } |
---|
281 | Int getBitsLeft() { return m_bitsLeft; } |
---|
282 | Int getPixelsLeft() { return m_pixelsLeft; } |
---|
283 | Int getBitsCoded() { return m_targetBits - m_estHeaderBits - m_bitsLeft; } |
---|
284 | Int getLCUCoded() { return m_numberOfLCU - m_LCULeft; } |
---|
285 | Int getLowerBound() { return m_lowerBound; } |
---|
286 | TRCLCU* getLCU() { return m_LCUs; } |
---|
287 | TRCLCU& getLCU( Int LCUIdx ) { return m_LCUs[LCUIdx]; } |
---|
288 | Int getPicActualHeaderBits() { return m_picActualHeaderBits; } |
---|
289 | Void setBitLeft(Int bits) { m_bitsLeft = bits; } |
---|
290 | Void setTargetBits( Int bits ) { m_targetBits = bits; m_bitsLeft = bits;} |
---|
291 | Void setTotalIntraCost(Double cost) { m_totalCostIntra = cost; } |
---|
292 | Void getLCUInitTargetBits(); |
---|
293 | #if KWU_RC_MADPRED_E0227 |
---|
294 | Double getTotalMAD() { return m_totalMAD; } |
---|
295 | Void setTotalMAD( Double MAD ) { m_totalMAD = MAD; } |
---|
296 | |
---|
297 | Double getIVTotalMAD() { return m_IVtotalMAD; } |
---|
298 | Void setIVTotalMAD( Double MAD ) { m_IVtotalMAD = MAD; } |
---|
299 | #endif |
---|
300 | |
---|
301 | Int getPicActualBits() { return m_picActualBits; } |
---|
302 | Int getPicActualQP() { return m_picQP; } |
---|
303 | Double getPicActualLambda() { return m_picLambda; } |
---|
304 | Int getPicEstQP() { return m_estPicQP; } |
---|
305 | Void setPicEstQP( Int QP ) { m_estPicQP = QP; } |
---|
306 | Double getPicEstLambda() { return m_estPicLambda; } |
---|
307 | Void setPicEstLambda( Double lambda ) { m_picLambda = lambda; } |
---|
308 | |
---|
309 | #if KWU_RC_MADPRED_E0227 |
---|
310 | Int getLayerID() { return m_LayerID; } |
---|
311 | Void setLayerID(Int layerid) { m_LayerID = layerid; } |
---|
312 | #endif |
---|
313 | private: |
---|
314 | TEncRCSeq* m_encRCSeq; |
---|
315 | TEncRCGOP* m_encRCGOP; |
---|
316 | |
---|
317 | Int m_frameLevel; |
---|
318 | Int m_numberOfPixel; |
---|
319 | Int m_numberOfLCU; |
---|
320 | Int m_targetBits; |
---|
321 | Int m_estHeaderBits; |
---|
322 | Int m_estPicQP; |
---|
323 | Int m_lowerBound; |
---|
324 | Double m_estPicLambda; |
---|
325 | |
---|
326 | Int m_LCULeft; |
---|
327 | Int m_bitsLeft; |
---|
328 | Int m_pixelsLeft; |
---|
329 | |
---|
330 | TRCLCU* m_LCUs; |
---|
331 | Int m_picActualHeaderBits; // only SH and potential APS |
---|
332 | Double m_totalCostIntra; |
---|
333 | Double m_remainingCostIntra; |
---|
334 | Int m_picActualBits; // the whole picture, including header |
---|
335 | Int m_picQP; // in integer form |
---|
336 | Double m_picLambda; |
---|
337 | #if KWU_RC_MADPRED_E0227 |
---|
338 | Double m_totalMAD; |
---|
339 | TEncRCPic* m_lastPicture; |
---|
340 | Int m_LayerID; |
---|
341 | TEncRCPic* m_lastIVPicture; |
---|
342 | Double m_IVtotalMAD; |
---|
343 | #endif |
---|
344 | }; |
---|
345 | |
---|
346 | class TEncRateCtrl |
---|
347 | { |
---|
348 | public: |
---|
349 | TEncRateCtrl(); |
---|
350 | ~TEncRateCtrl(); |
---|
351 | |
---|
352 | public: |
---|
353 | #if KWU_RC_MADPRED_E0227 |
---|
354 | 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 ); |
---|
355 | #else |
---|
356 | 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] ); |
---|
357 | #endif |
---|
358 | Void destroy(); |
---|
359 | Void initRCPic( Int frameLevel ); |
---|
360 | Void initRCGOP( Int numberOfPictures ); |
---|
361 | Void destroyRCGOP(); |
---|
362 | |
---|
363 | public: |
---|
364 | Void setRCQP ( Int QP ) { m_RCQP = QP; } |
---|
365 | Int getRCQP () { return m_RCQP; } |
---|
366 | TEncRCSeq* getRCSeq() { assert ( m_encRCSeq != NULL ); return m_encRCSeq; } |
---|
367 | TEncRCGOP* getRCGOP() { assert ( m_encRCGOP != NULL ); return m_encRCGOP; } |
---|
368 | TEncRCPic* getRCPic() { assert ( m_encRCPic != NULL ); return m_encRCPic; } |
---|
369 | list<TEncRCPic*>& getPicList() { return m_listRCPictures; } |
---|
370 | |
---|
371 | #if KWU_RC_MADPRED_E0227 |
---|
372 | Int getLayerID() { return m_LayerID; } |
---|
373 | Void setLayerID(Int layerid) { m_LayerID = layerid; } |
---|
374 | #endif |
---|
375 | Bool getCpbSaturationEnabled() { return m_CpbSaturationEnabled; } |
---|
376 | UInt getCpbState() { return m_cpbState; } |
---|
377 | UInt getCpbSize() { return m_cpbSize; } |
---|
378 | UInt getBufferingRate() { return m_bufferingRate; } |
---|
379 | Int updateCpbState(Int actualBits); |
---|
380 | Void initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness); |
---|
381 | |
---|
382 | private: |
---|
383 | TEncRCSeq* m_encRCSeq; |
---|
384 | TEncRCGOP* m_encRCGOP; |
---|
385 | TEncRCPic* m_encRCPic; |
---|
386 | list<TEncRCPic*> m_listRCPictures; |
---|
387 | Int m_RCQP; |
---|
388 | Bool m_CpbSaturationEnabled; // Enable target bits saturation to avoid CPB overflow and underflow |
---|
389 | Int m_cpbState; // CPB State |
---|
390 | UInt m_cpbSize; // CPB size |
---|
391 | UInt m_bufferingRate; // Buffering rate |
---|
392 | |
---|
393 | #if KWU_RC_MADPRED_E0227 |
---|
394 | Int m_LayerID; |
---|
395 | #endif |
---|
396 | }; |
---|
397 | |
---|
398 | #endif |
---|
399 | |
---|
400 | |
---|