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 | |
---|
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 | }; |
---|
90 | |
---|
91 | struct TRCParameter |
---|
92 | { |
---|
93 | Double m_alpha; |
---|
94 | Double m_beta; |
---|
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: |
---|
154 | Int m_totalFrames; |
---|
155 | Int m_targetRate; |
---|
156 | Int m_frameRate; |
---|
157 | Int m_GOPSize; |
---|
158 | Int m_picWidth; |
---|
159 | Int m_picHeight; |
---|
160 | Int m_LCUWidth; |
---|
161 | Int m_LCUHeight; |
---|
162 | Int m_numberOfLevel; |
---|
163 | Int m_averageBits; |
---|
164 | |
---|
165 | Int m_numberOfPixel; |
---|
166 | Int64 m_targetBits; |
---|
167 | Int m_numberOfLCU; |
---|
168 | Int* m_bitsRatio; |
---|
169 | Int* m_GOPID2Level; |
---|
170 | TRCParameter* m_picPara; |
---|
171 | TRCParameter** m_LCUPara; |
---|
172 | |
---|
173 | Int m_framesLeft; |
---|
174 | Int64 m_bitsLeft; |
---|
175 | Double m_seqTargetBpp; |
---|
176 | Double m_alphaUpdate; |
---|
177 | Double m_betaUpdate; |
---|
178 | Bool m_useLCUSeparateModel; |
---|
179 | |
---|
180 | Int m_adaptiveBit; |
---|
181 | Double m_lastLambda; |
---|
182 | }; |
---|
183 | |
---|
184 | class TEncRCGOP |
---|
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: |
---|
209 | TEncRCSeq* m_encRCSeq; |
---|
210 | Int* m_picTargetBitInGOP; |
---|
211 | Int m_numPic; |
---|
212 | Int m_targetBits; |
---|
213 | Int m_picLeft; |
---|
214 | Int m_bitsLeft; |
---|
215 | }; |
---|
216 | |
---|
217 | class TEncRCPic |
---|
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 | #if U0132_TARGET_BITS_SATURATION |
---|
268 | Void setBitLeft(Int bits) { m_bitsLeft = bits; } |
---|
269 | #endif |
---|
270 | Void setTargetBits( Int bits ) { m_targetBits = bits; m_bitsLeft = bits;} |
---|
271 | Void setTotalIntraCost(Double cost) { m_totalCostIntra = cost; } |
---|
272 | Void getLCUInitTargetBits(); |
---|
273 | |
---|
274 | Int getPicActualBits() { return m_picActualBits; } |
---|
275 | Int getPicActualQP() { return m_picQP; } |
---|
276 | Double getPicActualLambda() { return m_picLambda; } |
---|
277 | Int getPicEstQP() { return m_estPicQP; } |
---|
278 | Void setPicEstQP( Int QP ) { m_estPicQP = QP; } |
---|
279 | Double getPicEstLambda() { return m_estPicLambda; } |
---|
280 | Void setPicEstLambda( Double lambda ) { m_picLambda = lambda; } |
---|
281 | |
---|
282 | private: |
---|
283 | TEncRCSeq* m_encRCSeq; |
---|
284 | TEncRCGOP* m_encRCGOP; |
---|
285 | |
---|
286 | Int m_frameLevel; |
---|
287 | Int m_numberOfPixel; |
---|
288 | Int m_numberOfLCU; |
---|
289 | Int m_targetBits; |
---|
290 | Int m_estHeaderBits; |
---|
291 | Int m_estPicQP; |
---|
292 | Double m_estPicLambda; |
---|
293 | |
---|
294 | Int m_LCULeft; |
---|
295 | Int m_bitsLeft; |
---|
296 | Int m_pixelsLeft; |
---|
297 | |
---|
298 | TRCLCU* m_LCUs; |
---|
299 | Int m_picActualHeaderBits; // only SH and potential APS |
---|
300 | Double m_totalCostIntra; |
---|
301 | Double m_remainingCostIntra; |
---|
302 | Int m_picActualBits; // the whole picture, including header |
---|
303 | Int m_picQP; // in integer form |
---|
304 | Double m_picLambda; |
---|
305 | }; |
---|
306 | |
---|
307 | class TEncRateCtrl |
---|
308 | { |
---|
309 | public: |
---|
310 | TEncRateCtrl(); |
---|
311 | ~TEncRateCtrl(); |
---|
312 | |
---|
313 | public: |
---|
314 | 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] ); |
---|
315 | Void destroy(); |
---|
316 | Void initRCPic( Int frameLevel ); |
---|
317 | Void initRCGOP( Int numberOfPictures ); |
---|
318 | Void destroyRCGOP(); |
---|
319 | |
---|
320 | public: |
---|
321 | Void setRCQP ( Int QP ) { m_RCQP = QP; } |
---|
322 | Int getRCQP () { return m_RCQP; } |
---|
323 | TEncRCSeq* getRCSeq() { assert ( m_encRCSeq != NULL ); return m_encRCSeq; } |
---|
324 | TEncRCGOP* getRCGOP() { assert ( m_encRCGOP != NULL ); return m_encRCGOP; } |
---|
325 | TEncRCPic* getRCPic() { assert ( m_encRCPic != NULL ); return m_encRCPic; } |
---|
326 | list<TEncRCPic*>& getPicList() { return m_listRCPictures; } |
---|
327 | #if U0132_TARGET_BITS_SATURATION |
---|
328 | Bool getCpbSaturationEnabled() { return m_CpbSaturationEnabled; } |
---|
329 | UInt getCpbState() { return m_cpbState; } |
---|
330 | UInt getCpbSize() { return m_cpbSize; } |
---|
331 | UInt getBufferingRate() { return m_bufferingRate; } |
---|
332 | Int updateCpbState(Int actualBits); |
---|
333 | Void initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness); |
---|
334 | #endif |
---|
335 | |
---|
336 | private: |
---|
337 | TEncRCSeq* m_encRCSeq; |
---|
338 | TEncRCGOP* m_encRCGOP; |
---|
339 | TEncRCPic* m_encRCPic; |
---|
340 | list<TEncRCPic*> m_listRCPictures; |
---|
341 | Int m_RCQP; |
---|
342 | #if U0132_TARGET_BITS_SATURATION |
---|
343 | Bool m_CpbSaturationEnabled; // Enable target bits saturation to avoid CPB overflow and underflow |
---|
344 | Int m_cpbState; // CPB State |
---|
345 | UInt m_cpbSize; // CPB size |
---|
346 | UInt m_bufferingRate; // Buffering rate |
---|
347 | #endif |
---|
348 | }; |
---|
349 | |
---|
350 | #endif |
---|
351 | |
---|
352 | |
---|