source: 3DVCSoftware/branches/HTM-DEV-0.3-dev2/source/Lib/TLibEncoder/TEncRateCtrl.h @ 537

Last change on this file since 537 was 537, checked in by tech, 11 years ago

Update to HM 11.0.

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