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 | 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: |
---|
280 | TEncRCSeq* m_encRCSeq; |
---|
281 | TEncRCGOP* m_encRCGOP; |
---|
282 | |
---|
283 | Int m_frameLevel; |
---|
284 | Int m_numberOfPixel; |
---|
285 | Int m_numberOfLCU; |
---|
286 | Int m_targetBits; |
---|
287 | Int m_estHeaderBits; |
---|
288 | Int m_estPicQP; |
---|
289 | Double m_estPicLambda; |
---|
290 | |
---|
291 | Int m_LCULeft; |
---|
292 | Int m_bitsLeft; |
---|
293 | Int m_pixelsLeft; |
---|
294 | |
---|
295 | TRCLCU* m_LCUs; |
---|
296 | Int m_picActualHeaderBits; // only SH and potential APS |
---|
297 | Double m_totalCostIntra; |
---|
298 | Double m_remainingCostIntra; |
---|
299 | Int m_picActualBits; // the whole picture, including header |
---|
300 | Int m_picQP; // in integer form |
---|
301 | Double m_picLambda; |
---|
302 | }; |
---|
303 | |
---|
304 | class TEncRateCtrl |
---|
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: |
---|
326 | TEncRCSeq* m_encRCSeq; |
---|
327 | TEncRCGOP* m_encRCGOP; |
---|
328 | TEncRCPic* m_encRCPic; |
---|
329 | list<TEncRCPic*> m_listRCPictures; |
---|
330 | Int m_RCQP; |
---|
331 | }; |
---|
332 | |
---|
333 | #endif |
---|
334 | |
---|
335 | |
---|