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-2014, 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 | #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 updateAfterLCU( 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 | |
---|
270 | public: |
---|
271 | TEncRCSeq* getRCSequence() { return m_encRCSeq; } |
---|
272 | TEncRCGOP* getRCGOP() { return m_encRCGOP; } |
---|
273 | |
---|
274 | Int getFrameLevel() { return m_frameLevel; } |
---|
275 | Int getNumberOfPixel() { return m_numberOfPixel; } |
---|
276 | Int getNumberOfLCU() { return m_numberOfLCU; } |
---|
277 | Int getTargetBits() { return m_targetBits; } |
---|
278 | Int getEstHeaderBits() { return m_estHeaderBits; } |
---|
279 | Int getLCULeft() { return m_LCULeft; } |
---|
280 | Int getBitsLeft() { return m_bitsLeft; } |
---|
281 | Int getPixelsLeft() { return m_pixelsLeft; } |
---|
282 | Int getBitsCoded() { return m_targetBits - m_estHeaderBits - m_bitsLeft; } |
---|
283 | Int getLCUCoded() { return m_numberOfLCU - m_LCULeft; } |
---|
284 | TRCLCU* getLCU() { return m_LCUs; } |
---|
285 | TRCLCU& getLCU( Int LCUIdx ) { return m_LCUs[LCUIdx]; } |
---|
286 | Int getPicActualHeaderBits() { return m_picActualHeaderBits; } |
---|
287 | Void setTargetBits( Int bits ) { m_targetBits = bits; m_bitsLeft = bits;} |
---|
288 | Void setTotalIntraCost(Double cost) { m_totalCostIntra = cost; } |
---|
289 | Void getLCUInitTargetBits(); |
---|
290 | #if KWU_RC_MADPRED_E0227 |
---|
291 | Double getTotalMAD() { return m_totalMAD; } |
---|
292 | Void setTotalMAD( Double MAD ) { m_totalMAD = MAD; } |
---|
293 | |
---|
294 | Double getIVTotalMAD() { return m_IVtotalMAD; } |
---|
295 | Void setIVTotalMAD( Double MAD ) { m_IVtotalMAD = MAD; } |
---|
296 | #endif |
---|
297 | |
---|
298 | Int getPicActualBits() { return m_picActualBits; } |
---|
299 | Int getPicActualQP() { return m_picQP; } |
---|
300 | Double getPicActualLambda() { return m_picLambda; } |
---|
301 | Int getPicEstQP() { return m_estPicQP; } |
---|
302 | Void setPicEstQP( Int QP ) { m_estPicQP = QP; } |
---|
303 | Double getPicEstLambda() { return m_estPicLambda; } |
---|
304 | Void setPicEstLambda( Double lambda ) { m_picLambda = lambda; } |
---|
305 | |
---|
306 | #if KWU_RC_MADPRED_E0227 |
---|
307 | Int getLayerID() { return m_LayerID; } |
---|
308 | Void setLayerID(Int layerid) { m_LayerID = layerid; } |
---|
309 | #endif |
---|
310 | private: |
---|
311 | TEncRCSeq* m_encRCSeq; |
---|
312 | TEncRCGOP* m_encRCGOP; |
---|
313 | |
---|
314 | Int m_frameLevel; |
---|
315 | Int m_numberOfPixel; |
---|
316 | Int m_numberOfLCU; |
---|
317 | Int m_targetBits; |
---|
318 | Int m_estHeaderBits; |
---|
319 | Int m_estPicQP; |
---|
320 | Double m_estPicLambda; |
---|
321 | |
---|
322 | Int m_LCULeft; |
---|
323 | Int m_bitsLeft; |
---|
324 | Int m_pixelsLeft; |
---|
325 | |
---|
326 | TRCLCU* m_LCUs; |
---|
327 | Int m_picActualHeaderBits; // only SH and potential APS |
---|
328 | Double m_totalCostIntra; |
---|
329 | Double m_remainingCostIntra; |
---|
330 | Int m_picActualBits; // the whole picture, including header |
---|
331 | Int m_picQP; // in integer form |
---|
332 | Double m_picLambda; |
---|
333 | #if KWU_RC_MADPRED_E0227 |
---|
334 | Double m_totalMAD; |
---|
335 | TEncRCPic* m_lastPicture; |
---|
336 | Int m_LayerID; |
---|
337 | TEncRCPic* m_lastIVPicture; |
---|
338 | Double m_IVtotalMAD; |
---|
339 | #endif |
---|
340 | }; |
---|
341 | |
---|
342 | class TEncRateCtrl |
---|
343 | { |
---|
344 | public: |
---|
345 | TEncRateCtrl(); |
---|
346 | ~TEncRateCtrl(); |
---|
347 | |
---|
348 | public: |
---|
349 | #if KWU_RC_MADPRED_E0227 |
---|
350 | 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 ); |
---|
351 | #else |
---|
352 | 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] ); |
---|
353 | #endif |
---|
354 | Void destroy(); |
---|
355 | Void initRCPic( Int frameLevel ); |
---|
356 | Void initRCGOP( Int numberOfPictures ); |
---|
357 | Void destroyRCGOP(); |
---|
358 | |
---|
359 | public: |
---|
360 | Void setRCQP ( Int QP ) { m_RCQP = QP; } |
---|
361 | Int getRCQP () { return m_RCQP; } |
---|
362 | TEncRCSeq* getRCSeq() { assert ( m_encRCSeq != NULL ); return m_encRCSeq; } |
---|
363 | TEncRCGOP* getRCGOP() { assert ( m_encRCGOP != NULL ); return m_encRCGOP; } |
---|
364 | TEncRCPic* getRCPic() { assert ( m_encRCPic != NULL ); return m_encRCPic; } |
---|
365 | list<TEncRCPic*>& getPicList() { return m_listRCPictures; } |
---|
366 | |
---|
367 | #if KWU_RC_MADPRED_E0227 |
---|
368 | Int getLayerID() { return m_LayerID; } |
---|
369 | Void setLayerID(Int layerid) { m_LayerID = layerid; } |
---|
370 | #endif |
---|
371 | private: |
---|
372 | TEncRCSeq* m_encRCSeq; |
---|
373 | TEncRCGOP* m_encRCGOP; |
---|
374 | TEncRCPic* m_encRCPic; |
---|
375 | list<TEncRCPic*> m_listRCPictures; |
---|
376 | Int m_RCQP; |
---|
377 | #if KWU_RC_MADPRED_E0227 |
---|
378 | Int m_LayerID; |
---|
379 | #endif |
---|
380 | }; |
---|
381 | |
---|
382 | #endif |
---|
383 | |
---|
384 | |
---|