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-2016, 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 | #if V0078_ADAPTIVE_LOWER_BOUND |
---|
250 | Int xEstPicLowerBound( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ); |
---|
251 | #endif |
---|
252 | |
---|
253 | public: |
---|
254 | TEncRCSeq* getRCSequence() { return m_encRCSeq; } |
---|
255 | TEncRCGOP* getRCGOP() { return m_encRCGOP; } |
---|
256 | |
---|
257 | Int getFrameLevel() { return m_frameLevel; } |
---|
258 | Int getNumberOfPixel() { return m_numberOfPixel; } |
---|
259 | Int getNumberOfLCU() { return m_numberOfLCU; } |
---|
260 | Int getTargetBits() { return m_targetBits; } |
---|
261 | Int getEstHeaderBits() { return m_estHeaderBits; } |
---|
262 | Int getLCULeft() { return m_LCULeft; } |
---|
263 | Int getBitsLeft() { return m_bitsLeft; } |
---|
264 | Int getPixelsLeft() { return m_pixelsLeft; } |
---|
265 | Int getBitsCoded() { return m_targetBits - m_estHeaderBits - m_bitsLeft; } |
---|
266 | Int getLCUCoded() { return m_numberOfLCU - m_LCULeft; } |
---|
267 | #if V0078_ADAPTIVE_LOWER_BOUND |
---|
268 | Int getLowerBound() { return m_lowerBound; } |
---|
269 | #endif |
---|
270 | TRCLCU* getLCU() { return m_LCUs; } |
---|
271 | TRCLCU& getLCU( Int LCUIdx ) { return m_LCUs[LCUIdx]; } |
---|
272 | Int getPicActualHeaderBits() { return m_picActualHeaderBits; } |
---|
273 | #if U0132_TARGET_BITS_SATURATION |
---|
274 | Void setBitLeft(Int bits) { m_bitsLeft = bits; } |
---|
275 | #endif |
---|
276 | Void setTargetBits( Int bits ) { m_targetBits = bits; m_bitsLeft = bits;} |
---|
277 | Void setTotalIntraCost(Double cost) { m_totalCostIntra = cost; } |
---|
278 | Void getLCUInitTargetBits(); |
---|
279 | |
---|
280 | Int getPicActualBits() { return m_picActualBits; } |
---|
281 | Int getPicActualQP() { return m_picQP; } |
---|
282 | Double getPicActualLambda() { return m_picLambda; } |
---|
283 | Int getPicEstQP() { return m_estPicQP; } |
---|
284 | Void setPicEstQP( Int QP ) { m_estPicQP = QP; } |
---|
285 | Double getPicEstLambda() { return m_estPicLambda; } |
---|
286 | Void setPicEstLambda( Double lambda ) { m_picLambda = lambda; } |
---|
287 | |
---|
288 | private: |
---|
289 | TEncRCSeq* m_encRCSeq; |
---|
290 | TEncRCGOP* m_encRCGOP; |
---|
291 | |
---|
292 | Int m_frameLevel; |
---|
293 | Int m_numberOfPixel; |
---|
294 | Int m_numberOfLCU; |
---|
295 | Int m_targetBits; |
---|
296 | Int m_estHeaderBits; |
---|
297 | Int m_estPicQP; |
---|
298 | #if V0078_ADAPTIVE_LOWER_BOUND |
---|
299 | Int m_lowerBound; |
---|
300 | #endif |
---|
301 | Double m_estPicLambda; |
---|
302 | |
---|
303 | Int m_LCULeft; |
---|
304 | Int m_bitsLeft; |
---|
305 | Int m_pixelsLeft; |
---|
306 | |
---|
307 | TRCLCU* m_LCUs; |
---|
308 | Int m_picActualHeaderBits; // only SH and potential APS |
---|
309 | Double m_totalCostIntra; |
---|
310 | Double m_remainingCostIntra; |
---|
311 | Int m_picActualBits; // the whole picture, including header |
---|
312 | Int m_picQP; // in integer form |
---|
313 | Double m_picLambda; |
---|
314 | }; |
---|
315 | |
---|
316 | class TEncRateCtrl |
---|
317 | { |
---|
318 | public: |
---|
319 | TEncRateCtrl(); |
---|
320 | ~TEncRateCtrl(); |
---|
321 | |
---|
322 | public: |
---|
323 | 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] ); |
---|
324 | Void destroy(); |
---|
325 | Void initRCPic( Int frameLevel ); |
---|
326 | Void initRCGOP( Int numberOfPictures ); |
---|
327 | Void destroyRCGOP(); |
---|
328 | |
---|
329 | public: |
---|
330 | Void setRCQP ( Int QP ) { m_RCQP = QP; } |
---|
331 | Int getRCQP () { return m_RCQP; } |
---|
332 | TEncRCSeq* getRCSeq() { assert ( m_encRCSeq != NULL ); return m_encRCSeq; } |
---|
333 | TEncRCGOP* getRCGOP() { assert ( m_encRCGOP != NULL ); return m_encRCGOP; } |
---|
334 | TEncRCPic* getRCPic() { assert ( m_encRCPic != NULL ); return m_encRCPic; } |
---|
335 | list<TEncRCPic*>& getPicList() { return m_listRCPictures; } |
---|
336 | #if U0132_TARGET_BITS_SATURATION |
---|
337 | Bool getCpbSaturationEnabled() { return m_CpbSaturationEnabled; } |
---|
338 | UInt getCpbState() { return m_cpbState; } |
---|
339 | UInt getCpbSize() { return m_cpbSize; } |
---|
340 | UInt getBufferingRate() { return m_bufferingRate; } |
---|
341 | Int updateCpbState(Int actualBits); |
---|
342 | Void initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness); |
---|
343 | #endif |
---|
344 | |
---|
345 | private: |
---|
346 | TEncRCSeq* m_encRCSeq; |
---|
347 | TEncRCGOP* m_encRCGOP; |
---|
348 | TEncRCPic* m_encRCPic; |
---|
349 | list<TEncRCPic*> m_listRCPictures; |
---|
350 | Int m_RCQP; |
---|
351 | #if U0132_TARGET_BITS_SATURATION |
---|
352 | Bool m_CpbSaturationEnabled; // Enable target bits saturation to avoid CPB overflow and underflow |
---|
353 | Int m_cpbState; // CPB State |
---|
354 | UInt m_cpbSize; // CPB size |
---|
355 | UInt m_bufferingRate; // Buffering rate |
---|
356 | #endif |
---|
357 | }; |
---|
358 | |
---|
359 | #endif |
---|
360 | |
---|
361 | |
---|