source: 3DVCSoftware/trunk/source/Lib/TLibEncoder/TEncRateCtrl.h

Last change on this file was 1413, checked in by tech, 6 years ago

Merged HTM-16.2-dev@1412

File size: 16.0 KB
RevLine 
[608]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
[1313]4 * granted under this license.
[608]5 *
[1413]6 * Copyright (c) 2010-2017, ITU/ISO/IEC
[608]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
[1313]38#ifndef __TENCRATECTRL__
39#define __TENCRATECTRL__
[608]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
52using namespace std;
53
54//! \ingroup TLibEncoder
55//! \{
56
57#include "../TLibEncoder/TEncCfg.h"
58#include <list>
59#include <cassert>
60
61const Int g_RCInvalidQPValue = -999;
62const Int g_RCSmoothWindowSize = 40;
63const Int g_RCMaxPicListSize = 32;
64const Double g_RCWeightPicTargetBitInGOP    = 0.9;
65const Double g_RCWeightPicRargetBitInBuffer = 1.0 - g_RCWeightPicTargetBitInGOP;
66const Int g_RCIterationNum = 20;
67const Double g_RCWeightHistoryLambda = 0.5;
68const Double g_RCWeightCurrentLambda = 1.0 - g_RCWeightHistoryLambda;
69const Int g_RCLCUSmoothWindowSize = 4;
70const Double g_RCAlphaMinValue = 0.05;
71const Double g_RCAlphaMaxValue = 500.0;
72const Double g_RCBetaMinValue  = -3.0;
73const Double g_RCBetaMaxValue  = -0.1;
74
75#define ALPHA     6.7542;
76#define BETA1     1.2517
77#define BETA2     1.7860
78
79struct 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;
[655]89#if KWU_RC_MADPRED_E0227
90  Double m_MAD;
91  Int m_CUWidth;
92  Int m_CUHeight;
93  Double m_IVMAD;
94#endif
[608]95};
96
97struct TRCParameter
98{
99  Double m_alpha;
100  Double m_beta;
101};
102
103class TEncRCSeq
104{
105public:
106  TEncRCSeq();
107  ~TEncRCSeq();
108
109public:
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
119public:
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; }
[1313]125  Int  getPicHeight()                   { return m_picHeight; }
[608]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
159private:
160  Int m_totalFrames;
161  Int m_targetRate;
[1313]162  Int m_frameRate;
[608]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
190class TEncRCGOP
191{
192public:
193  TEncRCGOP();
194  ~TEncRCGOP();
195
196public:
197  Void create( TEncRCSeq* encRCSeq, Int numPic );
198  Void destroy();
199  Void updateAfterPicture( Int bitsCost );
200
201private:
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
206public:
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
214private:
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
223class TEncRCPic
224{
225public:
226  TEncRCPic();
227  ~TEncRCPic();
228
229public:
[655]230#if KWU_RC_MADPRED_E0227
231  Void create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures, Int layerID );
232#else
[608]233  Void create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures );
[655]234#endif
[608]235  Void destroy();
236
[655]237#if KWU_RC_MADPRED_E0227
238  Double estimatePicLambdaIV( list<TEncRCPic*>& listPreviousPictures, Int curPOC );
239#endif
[608]240  Int    estimatePicQP    ( Double lambda, list<TEncRCPic*>& listPreviousPictures );
241  Int    getRefineBitsForIntra(Int orgBits);
[1313]242  Double calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel);
[608]243  Double estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType);
244
[1313]245  Void   updateAlphaBetaIntra(Double *alpha, Double *beta);
[608]246
247  Double getLCUTargetBpp(SliceType eSliceType);
248  Double getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP);
[655]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
[608]252  Double getLCUEstLambda( Double bpp );
253  Int    getLCUEstQP( Double lambda, Int clipPicQP );
254
[1313]255  Void updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter = true );
[608]256  Void updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType);
257
258  Void addToPictureLsit( list<TEncRCPic*>& listPreviousPictures );
[655]259#if KWU_RC_MADPRED_E0227
260  Void addToPictureLsitIV( list<TEncRCPic*>& listPreviousPictures );
261  Void setIVPic( TEncRCPic* baseRCPic );
262#endif
[608]263  Double calAverageQP();
264  Double calAverageLambda();
265
266private:
267  Int xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP );
268  Int xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel );
[1405]269  Int xEstPicLowerBound( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP );
[608]270
271public:
272  TEncRCSeq*      getRCSequence()                         { return m_encRCSeq; }
273  TEncRCGOP*      getRCGOP()                              { return m_encRCGOP; }
274
275  Int  getFrameLevel()                                    { return m_frameLevel; }
276  Int  getNumberOfPixel()                                 { return m_numberOfPixel; }
277  Int  getNumberOfLCU()                                   { return m_numberOfLCU; }
278  Int  getTargetBits()                                    { return m_targetBits; }
279  Int  getEstHeaderBits()                                 { return m_estHeaderBits; }
280  Int  getLCULeft()                                       { return m_LCULeft; }
281  Int  getBitsLeft()                                      { return m_bitsLeft; }
282  Int  getPixelsLeft()                                    { return m_pixelsLeft; }
283  Int  getBitsCoded()                                     { return m_targetBits - m_estHeaderBits - m_bitsLeft; }
284  Int  getLCUCoded()                                      { return m_numberOfLCU - m_LCULeft; }
[1405]285  Int  getLowerBound()                                    { return m_lowerBound; }
[608]286  TRCLCU* getLCU()                                        { return m_LCUs; }
287  TRCLCU& getLCU( Int LCUIdx )                            { return m_LCUs[LCUIdx]; }
288  Int  getPicActualHeaderBits()                           { return m_picActualHeaderBits; }
[1386]289  Void setBitLeft(Int bits)                               { m_bitsLeft = bits; }
[608]290  Void setTargetBits( Int bits )                          { m_targetBits = bits; m_bitsLeft = bits;}
291  Void setTotalIntraCost(Double cost)                     { m_totalCostIntra = cost; }
292  Void getLCUInitTargetBits();
[655]293#if KWU_RC_MADPRED_E0227
294  Double getTotalMAD()                                    { return m_totalMAD; }
295  Void   setTotalMAD( Double MAD )                        { m_totalMAD = MAD; }
296
297  Double getIVTotalMAD()                                    { return m_IVtotalMAD; }
298  Void   setIVTotalMAD( Double MAD )                        { m_IVtotalMAD = MAD; }
299#endif
300
[608]301  Int  getPicActualBits()                                 { return m_picActualBits; }
302  Int  getPicActualQP()                                   { return m_picQP; }
303  Double getPicActualLambda()                             { return m_picLambda; }
304  Int  getPicEstQP()                                      { return m_estPicQP; }
305  Void setPicEstQP( Int QP )                              { m_estPicQP = QP; }
306  Double getPicEstLambda()                                { return m_estPicLambda; }
307  Void setPicEstLambda( Double lambda )                   { m_picLambda = lambda; }
308
[655]309#if KWU_RC_MADPRED_E0227
310  Int getLayerID()                                         { return m_LayerID; }
311  Void setLayerID(Int layerid)                              { m_LayerID = layerid; }
312#endif
[608]313private:
314  TEncRCSeq* m_encRCSeq;
315  TEncRCGOP* m_encRCGOP;
316
317  Int m_frameLevel;
318  Int m_numberOfPixel;
319  Int m_numberOfLCU;
320  Int m_targetBits;
321  Int m_estHeaderBits;
322  Int m_estPicQP;
[1405]323  Int m_lowerBound;
[608]324  Double m_estPicLambda;
325
326  Int m_LCULeft;
327  Int m_bitsLeft;
328  Int m_pixelsLeft;
329
330  TRCLCU* m_LCUs;
331  Int m_picActualHeaderBits;    // only SH and potential APS
[1313]332  Double m_totalCostIntra;
[608]333  Double m_remainingCostIntra;
334  Int m_picActualBits;          // the whole picture, including header
335  Int m_picQP;                  // in integer form
336  Double m_picLambda;
[655]337#if KWU_RC_MADPRED_E0227
338  Double m_totalMAD;
339  TEncRCPic* m_lastPicture;
340  Int m_LayerID;
341  TEncRCPic* m_lastIVPicture;
342  Double m_IVtotalMAD;
343#endif
[608]344};
345
346class TEncRateCtrl
347{
348public:
349  TEncRateCtrl();
350  ~TEncRateCtrl();
351
352public:
[655]353#if KWU_RC_MADPRED_E0227
354  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 );
355#else
[608]356  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] );
[655]357#endif
[608]358  Void destroy();
359  Void initRCPic( Int frameLevel );
360  Void initRCGOP( Int numberOfPictures );
361  Void destroyRCGOP();
362
363public:
364  Void       setRCQP ( Int QP ) { m_RCQP = QP;   }
365  Int        getRCQP ()         { return m_RCQP; }
366  TEncRCSeq* getRCSeq()          { assert ( m_encRCSeq != NULL ); return m_encRCSeq; }
367  TEncRCGOP* getRCGOP()          { assert ( m_encRCGOP != NULL ); return m_encRCGOP; }
368  TEncRCPic* getRCPic()          { assert ( m_encRCPic != NULL ); return m_encRCPic; }
369  list<TEncRCPic*>& getPicList() { return m_listRCPictures; }
370
[655]371#if KWU_RC_MADPRED_E0227
372  Int getLayerID()                { return m_LayerID; }
373  Void setLayerID(Int layerid)     { m_LayerID = layerid; }
374#endif
[1386]375  Bool       getCpbSaturationEnabled()  { return m_CpbSaturationEnabled;  }
376  UInt       getCpbState()              { return m_cpbState;       }
377  UInt       getCpbSize()               { return m_cpbSize;        }
378  UInt       getBufferingRate()         { return m_bufferingRate;  }
379  Int        updateCpbState(Int actualBits);
380  Void       initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness);
381
[608]382private:
383  TEncRCSeq* m_encRCSeq;
384  TEncRCGOP* m_encRCGOP;
385  TEncRCPic* m_encRCPic;
386  list<TEncRCPic*> m_listRCPictures;
387  Int        m_RCQP;
[1386]388  Bool       m_CpbSaturationEnabled;    // Enable target bits saturation to avoid CPB overflow and underflow
389  Int        m_cpbState;                // CPB State
390  UInt       m_cpbSize;                 // CPB size
391  UInt       m_bufferingRate;           // Buffering rate
392
[655]393#if KWU_RC_MADPRED_E0227
394  Int m_LayerID;
395#endif
[608]396};
397
[655]398#endif
[608]399
400
Note: See TracBrowser for help on using the repository browser.