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-2013, 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 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
58 | #include "../TLibEncoder/TEncCfg.h" |
---|
59 | #include <list> |
---|
60 | #include <cassert> |
---|
61 | |
---|
62 | const Int g_RCInvalidQPValue = -999; |
---|
63 | const Int g_RCSmoothWindowSize = 40; |
---|
64 | const Int g_RCMaxPicListSize = 32; |
---|
65 | const Double g_RCWeightPicTargetBitInGOP = 0.9; |
---|
66 | const Double g_RCWeightPicRargetBitInBuffer = 1.0 - g_RCWeightPicTargetBitInGOP; |
---|
67 | |
---|
68 | struct TRCLCU |
---|
69 | { |
---|
70 | Int m_actualBits; |
---|
71 | Int m_QP; // QP of skip mode is set to g_RCInvalidQPValue |
---|
72 | Int m_targetBits; |
---|
73 | Double m_lambda; |
---|
74 | Double m_MAD; |
---|
75 | Int m_numberOfPixel; |
---|
76 | }; |
---|
77 | |
---|
78 | struct TRCParameter |
---|
79 | { |
---|
80 | Double m_alpha; |
---|
81 | Double m_beta; |
---|
82 | }; |
---|
83 | |
---|
84 | class TEncRCSeq |
---|
85 | { |
---|
86 | public: |
---|
87 | TEncRCSeq(); |
---|
88 | ~TEncRCSeq(); |
---|
89 | |
---|
90 | public: |
---|
91 | Void create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel ); |
---|
92 | Void destroy(); |
---|
93 | Void initBitsRatio( Int bitsRatio[] ); |
---|
94 | Void initGOPID2Level( Int GOPID2Level[] ); |
---|
95 | Void initPicPara( TRCParameter* picPara = NULL ); // NULL to initial with default value |
---|
96 | Void initLCUPara( TRCParameter** LCUPara = NULL ); // NULL to initial with default value |
---|
97 | Void updateAfterPic ( Int bits ); |
---|
98 | Int getRefineBitsForIntra( Int orgBits ); |
---|
99 | |
---|
100 | public: |
---|
101 | Int getTotalFrames() { return m_totalFrames; } |
---|
102 | Int getTargetRate() { return m_targetRate; } |
---|
103 | Int getFrameRate() { return m_frameRate; } |
---|
104 | Int getGOPSize() { return m_GOPSize; } |
---|
105 | Int getPicWidth() { return m_picWidth; } |
---|
106 | Int getPicHeight() { return m_picHeight; } |
---|
107 | Int getLCUWidth() { return m_LCUWidth; } |
---|
108 | Int getLCUHeight() { return m_LCUHeight; } |
---|
109 | Int getNumberOfLevel() { return m_numberOfLevel; } |
---|
110 | Int getAverageBits() { return m_averageBits; } |
---|
111 | Int getLeftAverageBits() { assert( m_framesLeft > 0 ); return (Int)(m_bitsLeft / m_framesLeft); } |
---|
112 | Bool getUseLCUSeparateModel() { return m_useLCUSeparateModel; } |
---|
113 | |
---|
114 | Int getNumPixel() { return m_numberOfPixel; } |
---|
115 | Int64 getTargetBits() { return m_targetBits; } |
---|
116 | Int getNumberOfLCU() { return m_numberOfLCU; } |
---|
117 | Int* getBitRatio() { return m_bitsRatio; } |
---|
118 | Int getBitRatio( Int idx ) { assert( idx<m_GOPSize); return m_bitsRatio[idx]; } |
---|
119 | Int* getGOPID2Level() { return m_GOPID2Level; } |
---|
120 | Int getGOPID2Level( Int ID ) { assert( ID < m_GOPSize ); return m_GOPID2Level[ID]; } |
---|
121 | TRCParameter* getPicPara() { return m_picPara; } |
---|
122 | TRCParameter getPicPara( Int level ) { assert( level < m_numberOfLevel ); return m_picPara[level]; } |
---|
123 | Void setPicPara( Int level, TRCParameter para ) { assert( level < m_numberOfLevel ); m_picPara[level] = para; } |
---|
124 | TRCParameter** getLCUPara() { return m_LCUPara; } |
---|
125 | TRCParameter* getLCUPara( Int level ) { assert( level < m_numberOfLevel ); return m_LCUPara[level]; } |
---|
126 | TRCParameter getLCUPara( Int level, Int LCUIdx ) { assert( LCUIdx < m_numberOfLCU ); return getLCUPara(level)[LCUIdx]; } |
---|
127 | Void setLCUPara( Int level, Int LCUIdx, TRCParameter para ) { assert( level < m_numberOfLevel ); assert( LCUIdx < m_numberOfLCU ); m_LCUPara[level][LCUIdx] = para; } |
---|
128 | |
---|
129 | Int getFramesLeft() { return m_framesLeft; } |
---|
130 | Int64 getBitsLeft() { return m_bitsLeft; } |
---|
131 | |
---|
132 | Double getSeqBpp() { return m_seqTargetBpp; } |
---|
133 | Double getAlphaUpdate() { return m_alphaUpdate; } |
---|
134 | Double getBetaUpdate() { return m_betaUpdate; } |
---|
135 | |
---|
136 | private: |
---|
137 | Int m_totalFrames; |
---|
138 | Int m_targetRate; |
---|
139 | Int m_frameRate; |
---|
140 | Int m_GOPSize; |
---|
141 | Int m_picWidth; |
---|
142 | Int m_picHeight; |
---|
143 | Int m_LCUWidth; |
---|
144 | Int m_LCUHeight; |
---|
145 | Int m_numberOfLevel; |
---|
146 | Int m_averageBits; |
---|
147 | |
---|
148 | Int m_numberOfPixel; |
---|
149 | Int64 m_targetBits; |
---|
150 | Int m_numberOfLCU; |
---|
151 | Int* m_bitsRatio; |
---|
152 | Int* m_GOPID2Level; |
---|
153 | TRCParameter* m_picPara; |
---|
154 | TRCParameter** m_LCUPara; |
---|
155 | |
---|
156 | Int m_framesLeft; |
---|
157 | Int64 m_bitsLeft; |
---|
158 | Double m_seqTargetBpp; |
---|
159 | Double m_alphaUpdate; |
---|
160 | Double m_betaUpdate; |
---|
161 | Bool m_useLCUSeparateModel; |
---|
162 | }; |
---|
163 | |
---|
164 | class TEncRCGOP |
---|
165 | { |
---|
166 | public: |
---|
167 | TEncRCGOP(); |
---|
168 | ~TEncRCGOP(); |
---|
169 | |
---|
170 | public: |
---|
171 | Void create( TEncRCSeq* encRCSeq, Int numPic ); |
---|
172 | Void destroy(); |
---|
173 | Void updateAfterPicture( Int bitsCost ); |
---|
174 | |
---|
175 | private: |
---|
176 | Int xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize ); |
---|
177 | |
---|
178 | public: |
---|
179 | TEncRCSeq* getEncRCSeq() { return m_encRCSeq; } |
---|
180 | Int getNumPic() { return m_numPic;} |
---|
181 | Int getTargetBits() { return m_targetBits; } |
---|
182 | Int getPicLeft() { return m_picLeft; } |
---|
183 | Int getBitsLeft() { return m_bitsLeft; } |
---|
184 | Int getTargetBitInGOP( Int i ) { return m_picTargetBitInGOP[i]; } |
---|
185 | |
---|
186 | private: |
---|
187 | TEncRCSeq* m_encRCSeq; |
---|
188 | Int* m_picTargetBitInGOP; |
---|
189 | Int m_numPic; |
---|
190 | Int m_targetBits; |
---|
191 | Int m_picLeft; |
---|
192 | Int m_bitsLeft; |
---|
193 | }; |
---|
194 | |
---|
195 | class TEncRCPic |
---|
196 | { |
---|
197 | public: |
---|
198 | TEncRCPic(); |
---|
199 | ~TEncRCPic(); |
---|
200 | |
---|
201 | public: |
---|
202 | Void create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ); |
---|
203 | Void destroy(); |
---|
204 | |
---|
205 | Double estimatePicLambda( list<TEncRCPic*>& listPreviousPictures ); |
---|
206 | Int estimatePicQP ( Double lambda, list<TEncRCPic*>& listPreviousPictures ); |
---|
207 | Double getLCUTargetBpp(); |
---|
208 | Double getLCUEstLambda( Double bpp ); |
---|
209 | Int getLCUEstQP( Double lambda, Int clipPicQP ); |
---|
210 | |
---|
211 | Void updateAfterLCU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter = true ); |
---|
212 | Void updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage ); |
---|
213 | |
---|
214 | Void addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ); |
---|
215 | Double getEffectivePercentage(); |
---|
216 | Double calAverageQP(); |
---|
217 | Double calAverageLambda(); |
---|
218 | |
---|
219 | private: |
---|
220 | Int xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ); |
---|
221 | Int xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ); |
---|
222 | |
---|
223 | public: |
---|
224 | TEncRCSeq* getRCSequence() { return m_encRCSeq; } |
---|
225 | TEncRCGOP* getRCGOP() { return m_encRCGOP; } |
---|
226 | |
---|
227 | Int getFrameLevel() { return m_frameLevel; } |
---|
228 | Int getNumberOfPixel() { return m_numberOfPixel; } |
---|
229 | Int getNumberOfLCU() { return m_numberOfLCU; } |
---|
230 | Int getTargetBits() { return m_targetBits; } |
---|
231 | Void setTargetBits( Int bits ) { m_targetBits = bits; } |
---|
232 | Int getEstHeaderBits() { return m_estHeaderBits; } |
---|
233 | Int getLCULeft() { return m_LCULeft; } |
---|
234 | Int getBitsLeft() { return m_bitsLeft; } |
---|
235 | Int getPixelsLeft() { return m_pixelsLeft; } |
---|
236 | Int getBitsCoded() { return m_targetBits - m_estHeaderBits - m_bitsLeft; } |
---|
237 | Int getLCUCoded() { return m_numberOfLCU - m_LCULeft; } |
---|
238 | TRCLCU* getLCU() { return m_LCUs; } |
---|
239 | TRCLCU& getLCU( Int LCUIdx ) { return m_LCUs[LCUIdx]; } |
---|
240 | Int getPicActualHeaderBits() { return m_picActualHeaderBits; } |
---|
241 | Double getTotalMAD() { return m_totalMAD; } |
---|
242 | Void setTotalMAD( Double MAD ) { m_totalMAD = MAD; } |
---|
243 | Int getPicActualBits() { return m_picActualBits; } |
---|
244 | Int getPicActualQP() { return m_picQP; } |
---|
245 | Double getPicActualLambda() { return m_picLambda; } |
---|
246 | Int getPicEstQP() { return m_estPicQP; } |
---|
247 | Void setPicEstQP( Int QP ) { m_estPicQP = QP; } |
---|
248 | Double getPicEstLambda() { return m_estPicLambda; } |
---|
249 | Void setPicEstLambda( Double lambda ) { m_picLambda = lambda; } |
---|
250 | |
---|
251 | private: |
---|
252 | TEncRCSeq* m_encRCSeq; |
---|
253 | TEncRCGOP* m_encRCGOP; |
---|
254 | |
---|
255 | Int m_frameLevel; |
---|
256 | Int m_numberOfPixel; |
---|
257 | Int m_numberOfLCU; |
---|
258 | Int m_targetBits; |
---|
259 | Int m_estHeaderBits; |
---|
260 | Int m_estPicQP; |
---|
261 | Double m_estPicLambda; |
---|
262 | |
---|
263 | Int m_LCULeft; |
---|
264 | Int m_bitsLeft; |
---|
265 | Int m_pixelsLeft; |
---|
266 | |
---|
267 | TRCLCU* m_LCUs; |
---|
268 | Int m_picActualHeaderBits; // only SH and potential APS |
---|
269 | Double m_totalMAD; |
---|
270 | Int m_picActualBits; // the whole picture, including header |
---|
271 | Int m_picQP; // in integer form |
---|
272 | Double m_picLambda; |
---|
273 | TEncRCPic* m_lastPicture; |
---|
274 | }; |
---|
275 | |
---|
276 | class TEncRateCtrl |
---|
277 | { |
---|
278 | public: |
---|
279 | TEncRateCtrl(); |
---|
280 | ~TEncRateCtrl(); |
---|
281 | |
---|
282 | public: |
---|
283 | 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] ); |
---|
284 | Void destroy(); |
---|
285 | Void initRCPic( Int frameLevel ); |
---|
286 | Void initRCGOP( Int numberOfPictures ); |
---|
287 | Void destroyRCGOP(); |
---|
288 | |
---|
289 | public: |
---|
290 | Void setRCQP ( Int QP ) { m_RCQP = QP; } |
---|
291 | Int getRCQP () { return m_RCQP; } |
---|
292 | TEncRCSeq* getRCSeq() { assert ( m_encRCSeq != NULL ); return m_encRCSeq; } |
---|
293 | TEncRCGOP* getRCGOP() { assert ( m_encRCGOP != NULL ); return m_encRCGOP; } |
---|
294 | TEncRCPic* getRCPic() { assert ( m_encRCPic != NULL ); return m_encRCPic; } |
---|
295 | list<TEncRCPic*>& getPicList() { return m_listRCPictures; } |
---|
296 | |
---|
297 | private: |
---|
298 | TEncRCSeq* m_encRCSeq; |
---|
299 | TEncRCGOP* m_encRCGOP; |
---|
300 | TEncRCPic* m_encRCPic; |
---|
301 | list<TEncRCPic*> m_listRCPictures; |
---|
302 | Int m_RCQP; |
---|
303 | }; |
---|
304 | |
---|
305 | #else |
---|
306 | |
---|
307 | // ==================================================================================================================== |
---|
308 | // Class definition |
---|
309 | // ==================================================================================================================== |
---|
310 | #define MAX_DELTA_QP 2 |
---|
311 | #define MAX_CUDQP_DEPTH 0 |
---|
312 | |
---|
313 | typedef struct FrameData |
---|
314 | { |
---|
315 | Bool m_isReferenced; |
---|
316 | Int m_qp; |
---|
317 | Int m_bits; |
---|
318 | Double m_costMAD; |
---|
319 | }FrameData; |
---|
320 | |
---|
321 | typedef struct LCUData |
---|
322 | { |
---|
323 | Int m_qp; ///< coded QP |
---|
324 | Int m_bits; ///< actually generated bits |
---|
325 | Int m_pixels; ///< number of pixels for a unit |
---|
326 | Int m_widthInPixel; ///< number of pixels for width |
---|
327 | Int m_heightInPixel; ///< number of pixels for height |
---|
328 | Double m_costMAD; ///< texture complexity for a unit |
---|
329 | }LCUData; |
---|
330 | |
---|
331 | class MADLinearModel |
---|
332 | { |
---|
333 | private: |
---|
334 | Bool m_activeOn; |
---|
335 | Double m_paramY1; |
---|
336 | Double m_paramY2; |
---|
337 | Double m_costMADs[3]; |
---|
338 | |
---|
339 | public: |
---|
340 | MADLinearModel () {}; |
---|
341 | ~MADLinearModel() {}; |
---|
342 | |
---|
343 | Void initMADLinearModel (); |
---|
344 | Double getMAD (); |
---|
345 | Void updateMADLiearModel (); |
---|
346 | Void updateMADHistory (Double costMAD); |
---|
347 | Bool IsUpdateAvailable () { return m_activeOn; } |
---|
348 | }; |
---|
349 | |
---|
350 | class PixelBaseURQQuadraticModel |
---|
351 | { |
---|
352 | private: |
---|
353 | Double m_paramHighX1; |
---|
354 | Double m_paramHighX2; |
---|
355 | Double m_paramLowX1; |
---|
356 | Double m_paramLowX2; |
---|
357 | public: |
---|
358 | PixelBaseURQQuadraticModel () {}; |
---|
359 | ~PixelBaseURQQuadraticModel() {}; |
---|
360 | |
---|
361 | Void initPixelBaseQuadraticModel (); |
---|
362 | Int getQP (Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD); |
---|
363 | Void updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD); |
---|
364 | Bool checkUpdateAvailable (Int qpReference ); |
---|
365 | Double xConvertQP2QStep (Int qp ); |
---|
366 | Int xConvertQStep2QP (Double qStep ); |
---|
367 | }; |
---|
368 | |
---|
369 | class TEncRateCtrl |
---|
370 | { |
---|
371 | private: |
---|
372 | Bool m_isLowdelay; |
---|
373 | Int m_prevBitrate; |
---|
374 | Int m_currBitrate; |
---|
375 | Int m_frameRate; |
---|
376 | Int m_refFrameNum; |
---|
377 | Int m_nonRefFrameNum; |
---|
378 | Int m_numOfPixels; |
---|
379 | Int m_sourceWidthInLCU; |
---|
380 | Int m_sourceHeightInLCU; |
---|
381 | Int m_sizeGOP; |
---|
382 | Int m_indexGOP; |
---|
383 | Int m_indexFrame; |
---|
384 | Int m_indexLCU; |
---|
385 | Int m_indexUnit; |
---|
386 | Int m_indexRefFrame; |
---|
387 | Int m_indexNonRefFrame; |
---|
388 | Int m_indexPOCInGOP; |
---|
389 | Int m_indexPrevPOCInGOP; |
---|
390 | Int m_occupancyVB; |
---|
391 | Int m_initialOVB; |
---|
392 | Int m_targetBufLevel; |
---|
393 | Int m_initialTBL; |
---|
394 | Int m_remainingBitsInGOP; |
---|
395 | Int m_remainingBitsInFrame; |
---|
396 | Int m_occupancyVBInFrame; |
---|
397 | Int m_targetBits; |
---|
398 | Int m_numUnitInFrame; |
---|
399 | Int m_codedPixels; |
---|
400 | Bool m_activeUnitLevelOn; |
---|
401 | Double m_costNonRefAvgWeighting; |
---|
402 | Double m_costRefAvgWeighting; |
---|
403 | Double m_costAvgbpp; |
---|
404 | |
---|
405 | FrameData* m_pcFrameData; |
---|
406 | LCUData* m_pcLCUData; |
---|
407 | |
---|
408 | MADLinearModel m_cMADLinearModel; |
---|
409 | PixelBaseURQQuadraticModel m_cPixelURQQuadraticModel; |
---|
410 | |
---|
411 | public: |
---|
412 | TEncRateCtrl () {}; |
---|
413 | virtual ~TEncRateCtrl() {}; |
---|
414 | |
---|
415 | Void create (Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight); |
---|
416 | Void destroy (); |
---|
417 | |
---|
418 | Void initFrameData (Int qp = 0); |
---|
419 | Void initUnitData (Int qp = 0); |
---|
420 | Int getFrameQP (Bool isReferenced, Int POC); |
---|
421 | Bool calculateUnitQP (); |
---|
422 | Int getUnitQP () { return m_pcLCUData[m_indexLCU].m_qp; } |
---|
423 | Void updateRCGOPStatus (); |
---|
424 | Void updataRCFrameStatus (Int frameBits, SliceType eSliceType); |
---|
425 | Void updataRCUnitStatus (); |
---|
426 | Void updateLCUData (TComDataCU* pcCU, UInt64 actualLCUBits, Int qp); |
---|
427 | Void updateFrameData (UInt64 actualFrameBits); |
---|
428 | Double xAdjustmentBits (Int& reductionBits, Int& compensationBits); |
---|
429 | Int getGOPId () { return m_indexFrame; } |
---|
430 | }; |
---|
431 | #endif |
---|
432 | |
---|
433 | #endif |
---|
434 | |
---|
435 | |
---|