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 iRemainingBits; 173 int32_t iBitsPerMb; 174 int32_t iTargetBits; 175 int32_t iCurrentBitsLevel;//0:normal; 1:limited; 2:exceeded. 176 177 int32_t iIdrNum; 178 int64_t iIntraComplexity; //255*255(MaxMbSAD)*36864(MaxFS) make the highest bit of 32-bit integer 1 179 int32_t iIntraMbCount; 180 int64_t iIntraComplxMean; 181 182 int8_t iTlOfFrames[VGOP_SIZE]; 183 int32_t iRemainingWeights; 184 int32_t iFrameDqBits; 185 186 bool bGomRC; 187 double* pGomComplexity; 188 int32_t* pGomForegroundBlockNum; 189 int32_t* pCurrentFrameGomSad; 190 int32_t* pGomCost; 191 192 int32_t bEnableGomQp; 193 int32_t iAverageFrameQp; 194 int32_t iMinFrameQp; 195 int32_t iMaxFrameQp; 196 int32_t iNumberMbFrame; 197 int32_t iNumberMbGom; 198 int32_t iGomSize; 199 200 int32_t iSkipFrameNum; 201 int32_t iFrameCodedInVGop; 202 int32_t iSkipFrameInVGop; 203 int32_t iGopNumberInVGop; 204 int32_t iGopIndexInVGop; 205 206 int32_t iSkipQpValue; 207 int32_t iQpRangeUpperInFrame; 208 int32_t iQpRangeLowerInFrame; 209 int32_t iMinQp; 210 int32_t iMaxQp; 211 //int32_t delta_adaptive_qp; 212 int32_t iSkipBufferRatio; 213 214 int32_t iQStep; // *INT_MULTIPLY 215 int32_t iFrameDeltaQpUpper; 216 int32_t iFrameDeltaQpLower; 217 int32_t iLastCalculatedQScale; 218 219 //for skip frame and padding 220 int32_t iBufferSizeSkip; 221 int64_t iBufferFullnessSkip; 222 int64_t iBufferMaxBRFullness[TIME_WINDOW_TOTAL];//0: EVEN_TIME_WINDOW; 1: ODD_TIME_WINDOW 223 int32_t iPredFrameBit; 224 bool bNeedShiftWindowCheck[TIME_WINDOW_TOTAL]; 225 int32_t iBufferSizePadding; 226 int32_t iBufferFullnessPadding; 227 int32_t iPaddingSize; 228 int32_t iPaddingBitrateStat; 229 bool bSkipFlag; 230 int32_t iContinualSkipFrames; 231 SRCTemporal* pTemporalOverRc; 232 233 //for scc 234 int64_t iAvgCost2Bits; 235 int64_t iCost2BitsIntra; 236 int32_t iBaseQp; 237 long long uiLastTimeStamp; 238 239 //for statistics and online adjustments 240 int32_t iActualBitRate; // TODO: to complete later 241 float fLatestFrameRate; // TODO: to complete later 242 } SWelsSvcRc; 243 244 typedef void (*PWelsRCPictureInitFunc) (sWelsEncCtx* pCtx,long long uiTimeStamp); 245 typedef void (*PWelsRCPictureDelayJudgeFunc) (sWelsEncCtx* pCtx,long long uiTimeStamp,int32_t iDidIdx); 246 typedef void (*PWelsRCPictureInfoUpdateFunc) (sWelsEncCtx* pCtx, int32_t iLayerSize); 247 typedef void (*PWelsRCMBInfoUpdateFunc) (sWelsEncCtx* pCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice); 248 typedef void (*PWelsRCMBInitFunc) (sWelsEncCtx* pCtx, SMB* pCurMb, SSlice* pSlice); 249 typedef void (*PWelsCheckFrameSkipBasedMaxbrFunc) (sWelsEncCtx* pCtx, const long long uiTimeStamp, int32_t iDidIdx); 250 typedef void (*PWelsUpdateBufferWhenFrameSkippedFunc)(sWelsEncCtx* pCtx, int32_t iSpatialNum); 251 typedef void (*PWelsUpdateMaxBrCheckWindowStatusFunc)(sWelsEncCtx* pCtx, int32_t iSpatialNum, const long long uiTimeStamp); 252 typedef bool (*PWelsRCPostFrameSkippingFunc)(sWelsEncCtx* pCtx, const int32_t iDid, const long long uiTimeStamp); 253 254 typedef struct WelsRcFunc_s { 255 PWelsRCPictureInitFunc pfWelsRcPictureInit; 256 PWelsRCPictureDelayJudgeFunc pfWelsRcPicDelayJudge; 257 PWelsRCPictureInfoUpdateFunc pfWelsRcPictureInfoUpdate; 258 PWelsRCMBInitFunc pfWelsRcMbInit; 259 PWelsRCMBInfoUpdateFunc pfWelsRcMbInfoUpdate; 260 PWelsCheckFrameSkipBasedMaxbrFunc pfWelsCheckSkipBasedMaxbr; 261 PWelsUpdateBufferWhenFrameSkippedFunc pfWelsUpdateBufferWhenSkip; 262 PWelsUpdateMaxBrCheckWindowStatusFunc pfWelsUpdateMaxBrWindowStatus; 263 264 PWelsRCPostFrameSkippingFunc pfWelsRcPostFrameSkipping; 265 } SWelsRcFunc; 266 267 void GomRCInitForOneSlice(SSlice* pSlice, const int32_t kiBitsPerMb); 268 void CheckFrameSkipBasedMaxbr (sWelsEncCtx* pCtx,const long long uiTimeStamp, int32_t iDidIdx); 269 void UpdateBufferWhenFrameSkipped(sWelsEncCtx* pCtx, int32_t iSpatialNum); 270 void UpdateMaxBrCheckWindowStatus(sWelsEncCtx* pCtx, int32_t iSpatialNum, const long long uiTimeStamp); 271 bool WelsRcPostFrameSkipping(sWelsEncCtx* pCtx, const int32_t iDid, const long long uiTimeStamp); 272 void WelsRcPostFrameSkippedUpdate (sWelsEncCtx* pCtx, const int32_t iDid); 273 274 void RcTraceFrameBits (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_t iFrameSize); 275 void WelsRcInitModule (sWelsEncCtx* pCtx, RC_MODES iRcMode); 276 void WelsRcInitFuncPointers (sWelsEncCtx* pEncCtx, RC_MODES iRcMode); 277 void WelsRcFreeMemory (sWelsEncCtx* pCtx); 278 bool WelsRcCheckFrameStatus (sWelsEncCtx* pEncCtx,long long uiTimeStamp,int32_t iSpatialNum,int32_t iCurDid); 279 bool WelsUpdateSkipFrameStatus(); 280 long long GetTimestampForRc(const long long uiTimeStamp, const long long uiLastTimeStamp, const float fFrameRate); 281 282 } 283 #endif //RC_H 284