• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*!
2  * \copy
3  *     Copyright (c)  2004-2013, Cisco Systems
4  *     All rights reserved.
5  *
6  *     Redistribution and use in source and binary forms, with or without
7  *     modification, are permitted provided that the following conditions
8  *     are met:
9  *
10  *        * Redistributions of source code must retain the above copyright
11  *          notice, this list of conditions and the following disclaimer.
12  *
13  *        * Redistributions in binary form must reproduce the above copyright
14  *          notice, this list of conditions and the following disclaimer in
15  *          the documentation and/or other materials provided with the
16  *          distribution.
17  *
18  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *     POSSIBILITY OF SUCH DAMAGE.
30  *
31  *
32  *  ratectl.c
33  *
34  *  Abstract
35  *      Include file for ratectl.c
36  *
37  *  History
38  *      9/8/2004 Created
39  *    12/26/2011 Modified
40  *
41  *
42  *************************************************************************/
43 #ifndef RC_H
44 #define RC_H
45 
46 
47 #include "codec_app_def.h"
48 #include "svc_enc_macroblock.h"
49 #include "slice.h"
50 
51 namespace WelsEnc {
52 
53 typedef struct TagWelsEncCtx sWelsEncCtx;
54 
55 //trace
56 #define GOM_TRACE_FLAG 0
57 #define GOM_H_SCC               8
58 
59 enum {
60   BITS_NORMAL,
61   BITS_LIMITED,
62   BITS_EXCEEDED
63 };
64 
65 enum {
66 //virtual gop size
67   VGOP_SIZE             = 8,
68 
69 //qp information
70   GOM_MIN_QP_MODE       = 12,
71   GOM_MAX_QP_MODE       = 36,
72   MAX_LOW_BR_QP         = 42,
73   MIN_IDR_QP            = 26,
74   MAX_IDR_QP            = 32,
75   MIN_SCREEN_QP         = 26,
76   MAX_SCREEN_QP         = 35,
77   DELTA_QP              = 2,
78   DELTA_QP_BGD_THD      = 3,
79   QP_MIN_VALUE          = 0,
80   QP_MAX_VALUE          = 51,
81 
82 //frame skip constants
83   SKIP_QP_90P           = 24,
84   SKIP_QP_180P          = 24,
85   SKIP_QP_360P          = 31,
86   SKIP_QP_720P          = 31,
87   LAST_FRAME_QP_RANGE_UPPER_MODE0  = 3,
88   LAST_FRAME_QP_RANGE_LOWER_MODE0  = 2,
89   LAST_FRAME_QP_RANGE_UPPER_MODE1  = 5,
90   LAST_FRAME_QP_RANGE_LOWER_MODE1  = 3,
91 
92   MB_WIDTH_THRESHOLD_90P   = 15,
93   MB_WIDTH_THRESHOLD_180P  = 30,
94   MB_WIDTH_THRESHOLD_360P  = 60,
95 
96 //Mode 0 parameter
97   GOM_ROW_MODE0_90P     = 2,
98   GOM_ROW_MODE0_180P    = 2,
99   GOM_ROW_MODE0_360P    = 4,
100   GOM_ROW_MODE0_720P    = 4,
101   QP_RANGE_MODE0        = 3,
102 
103 //Mode 1 parameter
104   GOM_ROW_MODE1_90P     = 1,
105   GOM_ROW_MODE1_180P    = 1,
106   GOM_ROW_MODE1_360P    = 2,
107   GOM_ROW_MODE1_720P    = 2,
108   QP_RANGE_UPPER_MODE1  = 9,
109   QP_RANGE_LOWER_MODE1  = 4,
110   QP_RANGE_INTRA_MODE1  = 3
111 };
112 
113 //bits allocation
114 #define MAX_BITS_VARY_PERCENTAGE 100 //bits vary range in percentage
115 #define MAX_BITS_VARY_PERCENTAGE_x3d2 150 //bits vary range in percentage * 3/2
116 #define INT_MULTIPLY 100 // use to multiply in Double to Int Conversion, should be same as AQ_QSTEP_INT_MULTIPLY in WelsVP
117 #define WEIGHT_MULTIPLY 2000
118 #define REMAIN_BITS_TH (1)
119 #define VGOP_BITS_PERCENTAGE_DIFF 5
120 #define IDR_BITRATE_RATIO  4
121 #define FRAME_iTargetBits_VARY_RANGE 50 // *INT_MULTIPLY
122 //R-Q Model
123 #define LINEAR_MODEL_DECAY_FACTOR 80 // *INT_MULTIPLY
124 #define FRAME_CMPLX_RATIO_RANGE 20 // *INT_MULTIPLY
125 #define SMOOTH_FACTOR_MIN_VALUE 2 // *INT_MULTIPLY
126 //#define VGOP_BITS_MIN_RATIO 0.8
127 //skip and padding
128 #define TIME_CHECK_WINDOW 5000 // ms
129 #define SKIP_RATIO  50 // *INT_MULTIPLY
130 #define LAST_FRAME_PREDICT_WEIGHT 0.5
131 #define PADDING_BUFFER_RATIO 50 // *INT_MULTIPLY
132 #define PADDING_THRESHOLD    5 //*INT_MULTIPLY
133 
134 #define VIRTUAL_BUFFER_LOW_TH   120 //*INT_MULTIPLY
135 #define VIRTUAL_BUFFER_HIGH_TH  180 //*INT_MULTIPLY
136 
137 #define _BITS_RANGE 0
138 
139 enum {
140   EVEN_TIME_WINDOW  =0,
141   ODD_TIME_WINDOW   =1,
142   TIME_WINDOW_TOTAL =2
143 };
144 
145 typedef struct TagRCTemporal {
146 int32_t   iMinBitsTl;
147 int32_t   iMaxBitsTl;
148 int32_t   iTlayerWeight;
149 int32_t   iGopBitsDq;
150 //P frame level R-Q Model
151 int64_t   iLinearCmplx; // *INT_MULTIPLY
152 int32_t   iPFrameNum;
153 int64_t   iFrameCmplxMean;
154 int32_t   iMaxQp;
155 int32_t   iMinQp;
156 } SRCTemporal;
157 
158 typedef struct TagWelsRc {
159 int32_t   iRcVaryPercentage;
160 int32_t   iRcVaryRatio;
161 
162 int32_t   iInitialQp; //initial qp
163 int64_t   iBitRate; // Note: although the max bit rate is 240000*1200 which can be represented by int32, but there are many multipler of this iBitRate in the calculation of RC, so use int64 to avoid type conversion at all such places
164 int32_t   iPreviousBitrate;
165 int32_t   iPreviousGopSize;
166 double    fFrameRate;
167 int32_t   iBitsPerFrame;
168 int32_t   iMaxBitsPerFrame;
169 double    dPreviousFps;
170 
171 // bits allocation and status
172 int32_t   iLastAllocatedBits;
173 int32_t   iRemainingBits;
174 int32_t   iBitsPerMb;
175 int32_t   iTargetBits;
176 int32_t   iCurrentBitsLevel;//0:normal; 1:limited; 2:exceeded.
177 
178 int32_t   iIdrNum;
179 int64_t   iIntraComplexity; //255*255(MaxMbSAD)*36864(MaxFS) make the highest bit of 32-bit integer 1
180 int32_t   iIntraMbCount;
181 int64_t   iIntraComplxMean;
182 
183 int8_t    iTlOfFrames[VGOP_SIZE];
184 int32_t   iRemainingWeights;
185 int32_t   iFrameDqBits;
186 
187 bool       bGomRC;
188 double*    pGomComplexity;
189 int32_t*   pGomForegroundBlockNum;
190 int32_t*   pCurrentFrameGomSad;
191 int32_t*   pGomCost;
192 
193 int32_t   bEnableGomQp;
194 int32_t   iAverageFrameQp;
195 int32_t   iMinFrameQp;
196 int32_t   iMaxFrameQp;
197 int32_t   iNumberMbFrame;
198 int32_t   iNumberMbGom;
199 int32_t   iGomSize;
200 
201 int32_t   iSkipFrameNum;
202 int32_t   iFrameCodedInVGop;
203 int32_t   iSkipFrameInVGop;
204 int32_t   iGopNumberInVGop;
205 int32_t   iGopIndexInVGop;
206 
207 int32_t   iSkipQpValue;
208 int32_t   iQpRangeUpperInFrame;
209 int32_t   iQpRangeLowerInFrame;
210 int32_t   iMinQp;
211 int32_t   iMaxQp;
212 //int32_t   delta_adaptive_qp;
213 int32_t   iSkipBufferRatio;
214 
215 int32_t   iQStep; // *INT_MULTIPLY
216 int32_t   iFrameDeltaQpUpper;
217 int32_t   iFrameDeltaQpLower;
218 int32_t   iLastCalculatedQScale;
219 
220 //for skip frame and padding
221 int32_t   iBufferSizeSkip;
222 int64_t   iBufferFullnessSkip;
223 int64_t   iBufferMaxBRFullness[TIME_WINDOW_TOTAL];//0: EVEN_TIME_WINDOW; 1: ODD_TIME_WINDOW
224 int32_t   iPredFrameBit;
225 bool      bNeedShiftWindowCheck[TIME_WINDOW_TOTAL];
226 int32_t   iBufferSizePadding;
227 int32_t   iBufferFullnessPadding;
228 int32_t   iPaddingSize;
229 int32_t   iPaddingBitrateStat;
230 bool      bSkipFlag;
231 int32_t   iContinualSkipFrames;
232 SRCTemporal* pTemporalOverRc;
233 
234 //for scc
235 int64_t     iAvgCost2Bits;
236 int64_t     iCost2BitsIntra;
237 int32_t    iBaseQp;
238 long long  uiLastTimeStamp;
239 
240 //for statistics and online adjustments
241 int32_t   iActualBitRate; // TODO: to complete later
242 float     fLatestFrameRate; // TODO: to complete later
243 } SWelsSvcRc;
244 
245 typedef  void (*PWelsRCPictureInitFunc) (sWelsEncCtx* pCtx,long long uiTimeStamp);
246 typedef  void (*PWelsRCPictureDelayJudgeFunc) (sWelsEncCtx* pCtx,long long uiTimeStamp,int32_t iDidIdx);
247 typedef  void (*PWelsRCPictureInfoUpdateFunc) (sWelsEncCtx* pCtx, int32_t iLayerSize);
248 typedef  void (*PWelsRCMBInfoUpdateFunc) (sWelsEncCtx* pCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice);
249 typedef  void (*PWelsRCMBInitFunc) (sWelsEncCtx* pCtx, SMB* pCurMb, SSlice* pSlice);
250 typedef  void (*PWelsCheckFrameSkipBasedMaxbrFunc) (sWelsEncCtx* pCtx, const long long uiTimeStamp, int32_t iDidIdx);
251 typedef  void (*PWelsUpdateBufferWhenFrameSkippedFunc)(sWelsEncCtx* pCtx, int32_t iSpatialNum);
252 typedef  void (*PWelsUpdateMaxBrCheckWindowStatusFunc)(sWelsEncCtx* pCtx, int32_t iSpatialNum, const long long uiTimeStamp);
253 typedef  bool (*PWelsRCPostFrameSkippingFunc)(sWelsEncCtx* pCtx, const int32_t iDid, const long long uiTimeStamp);
254 
255 typedef  struct  WelsRcFunc_s {
256 PWelsRCPictureInitFunc          pfWelsRcPictureInit;
257 PWelsRCPictureDelayJudgeFunc    pfWelsRcPicDelayJudge;
258 PWelsRCPictureInfoUpdateFunc    pfWelsRcPictureInfoUpdate;
259 PWelsRCMBInitFunc               pfWelsRcMbInit;
260 PWelsRCMBInfoUpdateFunc         pfWelsRcMbInfoUpdate;
261 PWelsCheckFrameSkipBasedMaxbrFunc pfWelsCheckSkipBasedMaxbr;
262 PWelsUpdateBufferWhenFrameSkippedFunc pfWelsUpdateBufferWhenSkip;
263 PWelsUpdateMaxBrCheckWindowStatusFunc pfWelsUpdateMaxBrWindowStatus;
264 
265 PWelsRCPostFrameSkippingFunc    pfWelsRcPostFrameSkipping;
266 } SWelsRcFunc;
267 
268 void GomRCInitForOneSlice(SSlice* pSlice, const int32_t kiBitsPerMb);
269 void CheckFrameSkipBasedMaxbr (sWelsEncCtx* pCtx,const long long uiTimeStamp, int32_t iDidIdx);
270 void UpdateBufferWhenFrameSkipped(sWelsEncCtx* pCtx, int32_t iSpatialNum);
271 void UpdateMaxBrCheckWindowStatus(sWelsEncCtx* pCtx, int32_t iSpatialNum, const long long uiTimeStamp);
272 bool WelsRcPostFrameSkipping(sWelsEncCtx* pCtx, const int32_t iDid, const long long uiTimeStamp);
273 void WelsRcPostFrameSkippedUpdate (sWelsEncCtx* pCtx, const int32_t iDid);
274 
275 void RcTraceFrameBits (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_t iFrameSize);
276 void WelsRcInitModule (sWelsEncCtx* pCtx, RC_MODES iRcMode);
277 void WelsRcInitFuncPointers (sWelsEncCtx* pEncCtx, RC_MODES iRcMode);
278 void WelsRcFreeMemory (sWelsEncCtx* pCtx);
279 bool WelsRcCheckFrameStatus (sWelsEncCtx* pEncCtx,long long uiTimeStamp,int32_t iSpatialNum,int32_t iCurDid);
280 bool WelsUpdateSkipFrameStatus();
281 long long GetTimestampForRc(const long long uiTimeStamp, const long long uiLastTimeStamp, const float fFrameRate);
282 
283 }
284 #endif //RC_H
285