1 /*!
2 * \copy
3 * Copyright (c) 2009-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 * Rate Control
36 *
37 * History
38 * 9/8/2009 Created
39 * 12/26/2011 Modified
40 *
41 *
42 *
43 *************************************************************************/
44 #include "rc.h"
45 #include "encoder_context.h"
46 #include "utils.h"
47 #include "svc_enc_golomb.h"
48
49
50 namespace WelsEnc {
51
52 const int32_t g_kiQpToQstepTable[52] = {
53 63, 71, 79, 89, 100, 112, 126, 141, 159, 178,
54 200, 224, 252, 283, 317, 356, 400, 449, 504, 566,
55 635, 713, 800, 898, 1008, 1131, 1270, 1425, 1600, 1796,
56 2016, 2263, 2540, 2851, 3200, 3592, 4032, 4525, 5080, 5702,
57 6400, 7184, 8063, 9051, 10159, 11404, 12800, 14368, 16127, 18102,
58 20319, 22807
59 }; //WELS_ROUND(INT_MULTIPLY*pow (2.0, (iQP - 4.0) / 6.0))
60
RcInitLayerMemory(SWelsSvcRc * pWelsSvcRc,CMemoryAlign * pMA,const int32_t kiMaxTl)61 void RcInitLayerMemory (SWelsSvcRc* pWelsSvcRc, CMemoryAlign* pMA, const int32_t kiMaxTl) {
62 const int32_t kiGomSize = pWelsSvcRc->iGomSize;
63 const int32_t kiGomSizeD = kiGomSize * sizeof (double);
64 const int32_t kiGomSizeI = kiGomSize * sizeof (int32_t);
65 const int32_t kiLayerRcSize = kiGomSizeD + (kiGomSizeI * 3) + sizeof (SRCTemporal) * kiMaxTl;
66 uint8_t* pBaseMem = (uint8_t*)pMA->WelsMalloc (kiLayerRcSize, "pWelsSvcRc->pTemporalOverRc");
67
68 if (NULL == pBaseMem)
69 return;
70
71 pWelsSvcRc->pTemporalOverRc = (SRCTemporal*)pBaseMem;
72 pBaseMem += sizeof (SRCTemporal) * kiMaxTl;
73 pWelsSvcRc->pGomComplexity = (double*)pBaseMem;
74 pBaseMem += kiGomSizeD;
75 pWelsSvcRc->pGomForegroundBlockNum = (int32_t*)pBaseMem;
76 pBaseMem += kiGomSizeI;
77 pWelsSvcRc->pCurrentFrameGomSad = (int32_t*)pBaseMem;
78 pBaseMem += kiGomSizeI;
79 pWelsSvcRc->pGomCost = (int32_t*)pBaseMem;
80
81 }
82
RcFreeLayerMemory(SWelsSvcRc * pWelsSvcRc,CMemoryAlign * pMA)83 void RcFreeLayerMemory (SWelsSvcRc* pWelsSvcRc, CMemoryAlign* pMA) {
84 if (pWelsSvcRc != NULL && pWelsSvcRc->pTemporalOverRc != NULL) {
85 pMA->WelsFree (pWelsSvcRc->pTemporalOverRc, "pWelsSvcRc->pTemporalOverRc");
86 pWelsSvcRc->pTemporalOverRc = NULL;
87 pWelsSvcRc->pGomComplexity = NULL;
88 pWelsSvcRc->pGomForegroundBlockNum = NULL;
89 pWelsSvcRc->pCurrentFrameGomSad = NULL;
90 pWelsSvcRc->pGomCost = NULL;
91 }
92 }
93
RcConvertQp2QStep(int32_t iQP)94 static inline int32_t RcConvertQp2QStep (int32_t iQP) {
95 return g_kiQpToQstepTable[iQP];
96 }
RcConvertQStep2Qp(int32_t iQpStep)97 static inline int32_t RcConvertQStep2Qp (int32_t iQpStep) {
98 if (iQpStep <= g_kiQpToQstepTable[0]) //Qp step too small, return qp=0
99 return 0;
100 return WELS_ROUND ((6 * log (iQpStep * 1.0f / INT_MULTIPLY) / log (2.0) + 4.0));
101 }
102
RcInitSequenceParameter(sWelsEncCtx * pEncCtx)103 void RcInitSequenceParameter (sWelsEncCtx* pEncCtx) {
104 SWelsSvcRc* pWelsSvcRc = NULL;
105 SSpatialLayerConfig* pDLayerParam = NULL;
106
107 int32_t j = 0;
108 int32_t iMbWidth = 0;
109
110 bool bMultiSliceMode = false;
111 int32_t iGomRowMode0 = 1, iGomRowMode1 = 1;
112 for (j = 0; j < pEncCtx->pSvcParam->iSpatialLayerNum; j++) {
113 pWelsSvcRc = &pEncCtx->pWelsSvcRc[j];
114 pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[j];
115 iMbWidth = (pDLayerParam->iVideoWidth >> 4);
116 pWelsSvcRc->iNumberMbFrame = iMbWidth * (pDLayerParam->iVideoHeight >> 4);
117
118 pWelsSvcRc->iRcVaryPercentage = pEncCtx->pSvcParam->iBitsVaryPercentage; // % -- for temp
119 pWelsSvcRc->iRcVaryRatio = pWelsSvcRc->iRcVaryPercentage;
120
121 pWelsSvcRc->iBufferFullnessSkip = 0;
122 pWelsSvcRc->uiLastTimeStamp = 0;
123 pWelsSvcRc->iCost2BitsIntra = 1;
124 pWelsSvcRc->iAvgCost2Bits = 1;
125 pWelsSvcRc->iSkipBufferRatio = SKIP_RATIO;
126 pWelsSvcRc->iContinualSkipFrames = 0;
127 pWelsSvcRc->iQpRangeUpperInFrame = (QP_RANGE_UPPER_MODE1 * MAX_BITS_VARY_PERCENTAGE - ((
128 QP_RANGE_UPPER_MODE1 - QP_RANGE_MODE0) *
129 pWelsSvcRc->iRcVaryRatio)) / MAX_BITS_VARY_PERCENTAGE;
130 pWelsSvcRc->iQpRangeLowerInFrame = (QP_RANGE_LOWER_MODE1 * MAX_BITS_VARY_PERCENTAGE - ((
131 QP_RANGE_LOWER_MODE1 - QP_RANGE_MODE0) *
132 pWelsSvcRc->iRcVaryRatio)) / MAX_BITS_VARY_PERCENTAGE;
133
134 if (iMbWidth <= MB_WIDTH_THRESHOLD_90P) {
135 pWelsSvcRc->iSkipQpValue = SKIP_QP_90P;
136 iGomRowMode0 = GOM_ROW_MODE0_90P;
137 iGomRowMode1 = GOM_ROW_MODE1_90P;
138 } else if (iMbWidth <= MB_WIDTH_THRESHOLD_180P) {
139 pWelsSvcRc->iSkipQpValue = SKIP_QP_180P;
140 iGomRowMode0 = GOM_ROW_MODE0_180P;
141 iGomRowMode1 = GOM_ROW_MODE1_180P;
142 } else if (iMbWidth <= MB_WIDTH_THRESHOLD_360P) {
143 pWelsSvcRc->iSkipQpValue = SKIP_QP_360P;
144 iGomRowMode0 = GOM_ROW_MODE0_360P;
145 iGomRowMode1 = GOM_ROW_MODE1_360P;
146 } else {
147 pWelsSvcRc->iSkipQpValue = SKIP_QP_720P;
148 iGomRowMode0 = GOM_ROW_MODE0_720P;
149 iGomRowMode1 = GOM_ROW_MODE1_720P;
150 }
151 iGomRowMode0 = iGomRowMode1 + ((iGomRowMode0 - iGomRowMode1) * pWelsSvcRc->iRcVaryRatio / MAX_BITS_VARY_PERCENTAGE);
152
153 pWelsSvcRc->iNumberMbGom = iMbWidth * iGomRowMode0;
154
155 pWelsSvcRc->iMinQp = pEncCtx->pSvcParam->iMinQp;
156
157 pWelsSvcRc->iMaxQp = pEncCtx->pSvcParam->iMaxQp;
158
159 pWelsSvcRc->iFrameDeltaQpUpper = LAST_FRAME_QP_RANGE_UPPER_MODE1 - ((LAST_FRAME_QP_RANGE_UPPER_MODE1 -
160 LAST_FRAME_QP_RANGE_UPPER_MODE0) * pWelsSvcRc->iRcVaryRatio / MAX_BITS_VARY_PERCENTAGE);
161 pWelsSvcRc->iFrameDeltaQpLower = LAST_FRAME_QP_RANGE_LOWER_MODE1 - ((LAST_FRAME_QP_RANGE_LOWER_MODE1 -
162 LAST_FRAME_QP_RANGE_LOWER_MODE0) * pWelsSvcRc->iRcVaryRatio / MAX_BITS_VARY_PERCENTAGE);
163
164 pWelsSvcRc->iSkipFrameNum = 0;
165 pWelsSvcRc->iGomSize = (pWelsSvcRc->iNumberMbFrame + pWelsSvcRc->iNumberMbGom - 1) / pWelsSvcRc->iNumberMbGom;
166 pWelsSvcRc->bEnableGomQp = true;
167
168 RcInitLayerMemory (pWelsSvcRc, pEncCtx->pMemAlign, 1 + pEncCtx->pSvcParam->sDependencyLayers[j].iHighestTemporalId);
169
170 bMultiSliceMode = ((SM_RASTER_SLICE == pDLayerParam->sSliceArgument.uiSliceMode) ||
171 (SM_SIZELIMITED_SLICE == pDLayerParam->sSliceArgument.uiSliceMode));
172 if (bMultiSliceMode)
173 pWelsSvcRc->iNumberMbGom = pWelsSvcRc->iNumberMbFrame;
174 }
175 }
176
177
RcInitTlWeight(sWelsEncCtx * pEncCtx)178 void RcInitTlWeight (sWelsEncCtx* pEncCtx) {
179 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
180 SRCTemporal* pTOverRc = pWelsSvcRc->pTemporalOverRc;
181 SSpatialLayerInternal* pDLayerParam = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId];
182 const int32_t kiDecompositionStages = pDLayerParam->iDecompositionStages;
183 const int32_t kiHighestTid = pDLayerParam->iHighestTemporalId;
184
185 //Index 0:Virtual GOP size, Index 1:Frame rate
186 //double WeightArray[4][4] = { {1.0, 0, 0, 0}, {0.6, 0.4, 0, 0}, {0.4, 0.3, 0.15, 0}, {0.25, 0.15, 0.125, 0.0875}};
187 int32_t iWeightArray[4][4] = { {2000, 0, 0, 0}, {1200, 800, 0, 0}, {800, 600, 300, 0}, {500, 300, 250, 175}}; // original*WEIGHT_MULTIPLY
188 const int32_t kiGopSize = (1 << kiDecompositionStages);
189 int32_t i, k, n;
190
191 n = 0;
192 while (n <= kiHighestTid) {
193 pTOverRc[n].iTlayerWeight = iWeightArray[kiDecompositionStages][n];
194 pTOverRc[n].iMinQp = pWelsSvcRc->iMinQp + (n << 1);
195 pTOverRc[n].iMinQp = WELS_CLIP3 (pTOverRc[n].iMinQp, 0, 51);
196 pTOverRc[n].iMaxQp = pWelsSvcRc->iMaxQp + (n << 1);
197 pTOverRc[n].iMaxQp = WELS_CLIP3 (pTOverRc[n].iMaxQp, pTOverRc[n].iMinQp, 51);
198 ++ n;
199 }
200 //Calculate the frame index for the current frame and its reference frame
201 for (n = 0; n < VGOP_SIZE; n += kiGopSize) {
202 pWelsSvcRc->iTlOfFrames[n] = 0;
203 for (i = 1; i <= kiDecompositionStages; i++) {
204 for (k = 1 << (kiDecompositionStages - i); k < kiGopSize; k += (kiGopSize >> (i - 1))) {
205 pWelsSvcRc->iTlOfFrames[k + n] = i;
206 }
207 }
208 }
209 pWelsSvcRc->iPreviousGopSize = kiGopSize;
210 pWelsSvcRc->iGopNumberInVGop = VGOP_SIZE / kiGopSize;
211 }
212
RcUpdateBitrateFps(sWelsEncCtx * pEncCtx)213 void RcUpdateBitrateFps (sWelsEncCtx* pEncCtx) {
214 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
215 SRCTemporal* pTOverRc = pWelsSvcRc->pTemporalOverRc;
216
217 SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId];
218 SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId];
219 const int32_t kiGopSize = (1 << pDLayerParamInternal->iDecompositionStages);
220 const int32_t kiHighestTid = pDLayerParamInternal->iHighestTemporalId;
221 const int32_t input_iBitsPerFrame = WELS_DIV_ROUND (pDLayerParam->iSpatialBitrate,
222 pDLayerParamInternal->fOutputFrameRate);
223 const int64_t kiGopBits = input_iBitsPerFrame * kiGopSize;
224 int32_t i;
225
226 pWelsSvcRc->iBitRate = pDLayerParam->iSpatialBitrate;
227 pWelsSvcRc->fFrameRate = pDLayerParamInternal->fOutputFrameRate;
228
229 int32_t iTargetVaryRange = ((MAX_BITS_VARY_PERCENTAGE - pWelsSvcRc->iRcVaryRatio) >> 1);
230 int32_t iMinBitsRatio = MAX_BITS_VARY_PERCENTAGE - iTargetVaryRange;
231 int32_t iMaxBitsRatio = MAX_BITS_VARY_PERCENTAGE_x3d2;
232
233 for (i = 0; i <= kiHighestTid; i++) {
234 const int64_t kdConstraitBits = kiGopBits * pTOverRc[i].iTlayerWeight;
235 pTOverRc[i].iMinBitsTl = WELS_DIV_ROUND (kdConstraitBits * iMinBitsRatio,
236 MAX_BITS_VARY_PERCENTAGE * WEIGHT_MULTIPLY);
237 pTOverRc[i].iMaxBitsTl = WELS_DIV_ROUND (kdConstraitBits * iMaxBitsRatio,
238 MAX_BITS_VARY_PERCENTAGE * WEIGHT_MULTIPLY);
239 }
240 //When bitrate is changed, pBuffer size should be updated
241 pWelsSvcRc->iBufferSizeSkip = WELS_DIV_ROUND (pWelsSvcRc->iBitRate * pWelsSvcRc->iSkipBufferRatio, INT_MULTIPLY);
242 pWelsSvcRc->iBufferSizePadding = WELS_DIV_ROUND (pWelsSvcRc->iBitRate * PADDING_BUFFER_RATIO, INT_MULTIPLY);
243
244 //change remaining bits
245 if (pWelsSvcRc->iBitsPerFrame > REMAIN_BITS_TH) {
246 pWelsSvcRc->iRemainingBits = WELS_DIV_ROUND (static_cast<int64_t> (pWelsSvcRc->iRemainingBits) * input_iBitsPerFrame,
247 pWelsSvcRc->iBitsPerFrame);
248 }
249 pWelsSvcRc->iBitsPerFrame = input_iBitsPerFrame;
250 pWelsSvcRc->iMaxBitsPerFrame = WELS_DIV_ROUND (pDLayerParam->iMaxSpatialBitrate,
251 pDLayerParamInternal->fOutputFrameRate);
252 }
253
254
RcInitVGop(sWelsEncCtx * pEncCtx)255 void RcInitVGop (sWelsEncCtx* pEncCtx) {
256 const int32_t kiDid = pEncCtx->uiDependencyId;
257 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[kiDid];
258 SRCTemporal* pTOverRc = pWelsSvcRc->pTemporalOverRc;
259 const int32_t kiHighestTid = pEncCtx->pSvcParam->sDependencyLayers[kiDid].iHighestTemporalId;
260
261 pWelsSvcRc->iRemainingBits = VGOP_SIZE * pWelsSvcRc->iBitsPerFrame;
262 pWelsSvcRc->iRemainingWeights = pWelsSvcRc->iGopNumberInVGop * WEIGHT_MULTIPLY;
263
264 pWelsSvcRc->iFrameCodedInVGop = 0;
265 pWelsSvcRc->iGopIndexInVGop = 0;
266
267 for (int32_t i = 0; i <= kiHighestTid; ++ i)
268 pTOverRc[i].iGopBitsDq = 0;
269 pWelsSvcRc->iSkipFrameInVGop = 0;
270 }
271
RcInitRefreshParameter(sWelsEncCtx * pEncCtx)272 void RcInitRefreshParameter (sWelsEncCtx* pEncCtx) {
273 const int32_t kiDid = pEncCtx->uiDependencyId;
274 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[kiDid];
275 SRCTemporal* pTOverRc = pWelsSvcRc->pTemporalOverRc;
276 SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[kiDid];
277 SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[kiDid];
278 const int32_t kiHighestTid = pDLayerParamInternal->iHighestTemporalId;
279 int32_t i;
280
281 //I frame R-Q Model
282 pWelsSvcRc->iIntraComplexity = 0;
283 pWelsSvcRc->iIntraMbCount = 0;
284 pWelsSvcRc->iIntraComplxMean = 0;
285 //P frame R-Q Model
286 for (i = 0; i <= kiHighestTid; i++) {
287 pTOverRc[i].iPFrameNum = 0;
288 pTOverRc[i].iLinearCmplx = 0;
289 pTOverRc[i].iFrameCmplxMean = 0;
290 }
291
292 pWelsSvcRc->iBufferFullnessSkip = 0;
293 pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW] = 0;
294 pWelsSvcRc->iBufferMaxBRFullness[ODD_TIME_WINDOW] = 0;
295 pWelsSvcRc->iPredFrameBit = 0;
296 pWelsSvcRc->iBufferFullnessPadding = 0;
297
298 pWelsSvcRc->iGopIndexInVGop = 0;
299 pWelsSvcRc->iRemainingBits = 0;
300 pWelsSvcRc->iBitsPerFrame = 0;
301
302 //Backup the initial bitrate and fps
303 pWelsSvcRc->iPreviousBitrate = pDLayerParam->iSpatialBitrate;
304 pWelsSvcRc->dPreviousFps = pDLayerParamInternal->fOutputFrameRate;
305
306 memset (pWelsSvcRc->pCurrentFrameGomSad, 0, pWelsSvcRc->iGomSize * sizeof (int32_t));
307
308 RcInitTlWeight (pEncCtx);
309 RcUpdateBitrateFps (pEncCtx);
310 RcInitVGop (pEncCtx);
311 }
312
RcJudgeBitrateFpsUpdate(sWelsEncCtx * pEncCtx)313 bool RcJudgeBitrateFpsUpdate (sWelsEncCtx* pEncCtx) {
314 int32_t iCurDid = pEncCtx->uiDependencyId;
315 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[iCurDid];
316 SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[iCurDid];
317 SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[iCurDid];
318
319 if ((pWelsSvcRc->iPreviousBitrate != pDLayerParam->iSpatialBitrate) ||
320 (pWelsSvcRc->dPreviousFps - pDLayerParamInternal->fOutputFrameRate) > EPSN ||
321 (pWelsSvcRc->dPreviousFps - pDLayerParamInternal->fOutputFrameRate) < -EPSN) {
322 pWelsSvcRc->iPreviousBitrate = pDLayerParam->iSpatialBitrate;
323 pWelsSvcRc->dPreviousFps = pDLayerParamInternal->fOutputFrameRate;
324 return true;
325 } else
326 return false;
327 }
328
329 #if GOM_TRACE_FLAG
RcTraceVGopBitrate(sWelsEncCtx * pEncCtx)330 void RcTraceVGopBitrate (sWelsEncCtx* pEncCtx) {
331 const int32_t kiDid = pEncCtx->uiDependencyId;
332 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[kiDid];
333
334 if (pWelsSvcRc->iFrameCodedInVGop) {
335 const int32_t kiHighestTid = pEncCtx->pSvcParam->sDependencyLayers[kiDid].iHighestTemporalId;
336 SRCTemporal* pTOverRc = pWelsSvcRc->pTemporalOverRc;
337 int32_t iVGopBitrate = 0;
338 int32_t iTotalBits = pWelsSvcRc->iPaddingBitrateStat;
339 int32_t iTid = 0;
340 while (iTid <= kiHighestTid) {
341 iTotalBits += pTOverRc[iTid].iGopBitsDq;
342 ++ iTid;
343 }
344 int32_t iFrameInVGop = pWelsSvcRc->iFrameCodedInVGop + pWelsSvcRc->iSkipFrameInVGop;
345 if (0 != iFrameInVGop)
346 iVGopBitrate = WELS_ROUND (iTotalBits / iFrameInVGop * pWelsSvcRc->fFrameRate);
347 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "[Rc] VGOPbitrate%d: %d ", kiDid, iVGopBitrate);
348 if (iTotalBits > 0) {
349 iTid = 0;
350 while (iTid <= kiHighestTid) {
351 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "T%d=%8.3f ", iTid, (double) (pTOverRc[iTid].iGopBitsDq / iTotalBits));
352 ++ iTid;
353 }
354 }
355 }
356 }
357 #endif
358
RcUpdateTemporalZero(sWelsEncCtx * pEncCtx)359 void RcUpdateTemporalZero (sWelsEncCtx* pEncCtx) {
360 const int32_t kiDid = pEncCtx->uiDependencyId;
361 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[kiDid];
362 SSpatialLayerInternal* pDLayerParam = &pEncCtx->pSvcParam->sDependencyLayers[kiDid];
363 const int32_t kiGopSize = (1 << pDLayerParam->iDecompositionStages);
364
365 if (pWelsSvcRc->iPreviousGopSize != kiGopSize) {
366 #if GOM_TRACE_FLAG
367 RcTraceVGopBitrate (pEncCtx);
368 #endif
369 RcInitTlWeight (pEncCtx);
370 RcInitVGop (pEncCtx);
371 } else if (pWelsSvcRc->iGopIndexInVGop == pWelsSvcRc->iGopNumberInVGop || pEncCtx->eSliceType == I_SLICE) {
372 #if GOM_TRACE_FLAG
373 RcTraceVGopBitrate (pEncCtx);
374 #endif
375 RcInitVGop (pEncCtx);
376 }
377 pWelsSvcRc->iGopIndexInVGop++;
378 }
379
380
RcCalculateIdrQp(sWelsEncCtx * pEncCtx)381 void RcCalculateIdrQp (sWelsEncCtx* pEncCtx) {
382 double dBpp = 0;
383 int32_t i;
384
385 //64k@6fps for 90p: bpp 0.74 QP:24
386 //192k@12fps for 180p: bpp 0.28 QP:26
387 //512k@24fps for 360p: bpp 0.09 QP:30
388 //1500k@30fps for 720p: bpp 0.05 QP:32
389 double dBppArray[4][3] = {{0.5, 0.75, 1.0}, {0.2, 0.3, 0.4}, {0.05, 0.09, 0.13}, {0.03, 0.06, 0.1}};
390 int32_t dInitialQPArray[4][4] = {{28, 26, 24, 22}, {30, 28, 26, 24}, {32, 30, 28, 26}, {34, 32, 30, 28}};
391 int32_t iBppIndex = 0;
392 int32_t iQpRangeArray[4][2] = {{37, 25}, {36, 24}, {35, 23}, {34, 22}};
393 int64_t iFrameComplexity = pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity;
394 if (pEncCtx->pSvcParam->iUsageType == SCREEN_CONTENT_REAL_TIME) {
395 SVAAFrameInfoExt* pVaa = static_cast<SVAAFrameInfoExt*> (pEncCtx->pVaa);
396 iFrameComplexity = pVaa->sComplexityScreenParam.iFrameComplexity;
397 }
398 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
399 SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId];
400 SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId];
401 if (pDLayerParamInternal->fOutputFrameRate > EPSN && pDLayerParam->iVideoWidth && pDLayerParam->iVideoHeight)
402 dBpp = (double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParamInternal->fOutputFrameRate *
403 pDLayerParam->iVideoWidth *
404 pDLayerParam->iVideoHeight);
405 else
406 dBpp = 0.1;
407 //Area*2
408 if (pDLayerParam->iVideoWidth * pDLayerParam->iVideoHeight <= 28800) // 90p video:160*90
409 iBppIndex = 0;
410 else if (pDLayerParam->iVideoWidth * pDLayerParam->iVideoHeight <= 115200) // 180p video:320*180
411 iBppIndex = 1;
412 else if (pDLayerParam->iVideoWidth * pDLayerParam->iVideoHeight <= 460800) // 360p video:640*360
413 iBppIndex = 2;
414 else
415 iBppIndex = 3;
416
417 //Search
418 for (i = 0; i < 3; i++) {
419 if (dBpp <= dBppArray[iBppIndex][i])
420 break;
421 }
422 int32_t iMaxQp = iQpRangeArray[i][0];
423 int32_t iMinQp = iQpRangeArray[i][1];
424 iMinQp = WELS_CLIP3 (iMinQp, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
425 iMaxQp = WELS_CLIP3 (iMaxQp, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
426 if (0 == pWelsSvcRc->iIdrNum) { //the first IDR frame
427 pWelsSvcRc->iInitialQp = dInitialQPArray[iBppIndex][i];
428 } else {
429
430 //obtain the idr qp using previous idr complexity
431 if (pWelsSvcRc->iNumberMbFrame != pWelsSvcRc->iIntraMbCount) {
432 pWelsSvcRc->iIntraComplexity = pWelsSvcRc->iIntraComplexity * pWelsSvcRc->iNumberMbFrame /
433 pWelsSvcRc->iIntraMbCount;
434 }
435
436 int64_t iCmplxRatio = WELS_DIV_ROUND64 (iFrameComplexity * INT_MULTIPLY,
437 pWelsSvcRc->iIntraComplxMean);
438 iCmplxRatio = WELS_CLIP3 (iCmplxRatio, INT_MULTIPLY - FRAME_CMPLX_RATIO_RANGE, INT_MULTIPLY + FRAME_CMPLX_RATIO_RANGE);
439 pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pWelsSvcRc->iIntraComplexity * iCmplxRatio),
440 (pWelsSvcRc->iTargetBits * INT_MULTIPLY));
441 pWelsSvcRc->iInitialQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep);
442 }
443
444 pWelsSvcRc->iInitialQp = WELS_CLIP3 (pWelsSvcRc->iInitialQp, iMinQp, iMaxQp);
445 pEncCtx->iGlobalQp = pWelsSvcRc->iInitialQp;
446 pWelsSvcRc->iQStep = RcConvertQp2QStep (pEncCtx->iGlobalQp);
447 pWelsSvcRc->iLastCalculatedQScale = pEncCtx->iGlobalQp;
448 pWelsSvcRc->iMinFrameQp = WELS_CLIP3 (pEncCtx->iGlobalQp - DELTA_QP_BGD_THD, iMinQp, iMaxQp);
449 pWelsSvcRc->iMaxFrameQp = WELS_CLIP3 (pEncCtx->iGlobalQp + DELTA_QP_BGD_THD, iMinQp, iMaxQp);
450
451 }
452
RcCalculatePictureQp(sWelsEncCtx * pEncCtx)453 void RcCalculatePictureQp (sWelsEncCtx* pEncCtx) {
454 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
455 int32_t iTl = pEncCtx->uiTemporalId;
456 SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[iTl];
457 int32_t iLumaQp = 0;
458 int32_t iDeltaQpTemporal = 0;
459 int64_t iFrameComplexity = pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity;
460 if (pEncCtx->pSvcParam->iUsageType == SCREEN_CONTENT_REAL_TIME) {
461 SVAAFrameInfoExt* pVaa = static_cast<SVAAFrameInfoExt*> (pEncCtx->pVaa);
462 iFrameComplexity = pVaa->sComplexityScreenParam.iFrameComplexity;
463 }
464 if (0 == pTOverRc->iPFrameNum) {
465 iLumaQp = pWelsSvcRc->iInitialQp;
466 } else if (pWelsSvcRc->iCurrentBitsLevel == BITS_EXCEEDED) {
467 iLumaQp = pWelsSvcRc->iLastCalculatedQScale + DELTA_QP_BGD_THD;
468 //limit QP
469 int32_t iLastIdxCodecInVGop = pWelsSvcRc->iFrameCodedInVGop - 1;
470 if (iLastIdxCodecInVGop < 0)
471 iLastIdxCodecInVGop += VGOP_SIZE;
472 int32_t iTlLast = pWelsSvcRc->iTlOfFrames[iLastIdxCodecInVGop];
473 iDeltaQpTemporal = iTl - iTlLast;
474 if (0 == iTlLast && iTl > 0)
475 iDeltaQpTemporal += 1;
476 else if (0 == iTl && iTlLast > 0)
477 iDeltaQpTemporal -= 1;
478
479 } else {
480 int64_t iCmplxRatio = WELS_DIV_ROUND64 (iFrameComplexity * INT_MULTIPLY,
481 pTOverRc->iFrameCmplxMean);
482 iCmplxRatio = WELS_CLIP3 (iCmplxRatio, INT_MULTIPLY - FRAME_CMPLX_RATIO_RANGE, INT_MULTIPLY + FRAME_CMPLX_RATIO_RANGE);
483
484 pWelsSvcRc->iQStep = WELS_DIV_ROUND ((pTOverRc->iLinearCmplx * iCmplxRatio), (pWelsSvcRc->iTargetBits * INT_MULTIPLY));
485 iLumaQp = RcConvertQStep2Qp (pWelsSvcRc->iQStep);
486 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
487 "iCmplxRatio = %d,frameComplexity = %" PRId64 ",iFrameCmplxMean = %" PRId64 ",iQStep = %d,iLumaQp = %d", (int)iCmplxRatio,
488 iFrameComplexity, pTOverRc->iFrameCmplxMean, pWelsSvcRc->iQStep, iLumaQp);
489 //limit QP
490 int32_t iLastIdxCodecInVGop = pWelsSvcRc->iFrameCodedInVGop - 1;
491 if (iLastIdxCodecInVGop < 0)
492 iLastIdxCodecInVGop += VGOP_SIZE;
493 int32_t iTlLast = pWelsSvcRc->iTlOfFrames[iLastIdxCodecInVGop];
494 int32_t iDeltaQpTemporal = iTl - iTlLast;
495 if (0 == iTlLast && iTl > 0)
496 iDeltaQpTemporal += 1;
497 else if (0 == iTl && iTlLast > 0)
498 iDeltaQpTemporal -= 1;
499 }
500 pWelsSvcRc->iMinFrameQp = WELS_CLIP3 (pWelsSvcRc->iLastCalculatedQScale - pWelsSvcRc->iFrameDeltaQpLower +
501 iDeltaQpTemporal, pTOverRc->iMinQp, pTOverRc->iMaxQp) ;
502 pWelsSvcRc->iMaxFrameQp = WELS_CLIP3 (pWelsSvcRc->iLastCalculatedQScale + pWelsSvcRc->iFrameDeltaQpUpper +
503 iDeltaQpTemporal, pTOverRc->iMinQp, pTOverRc->iMaxQp);
504
505 iLumaQp = WELS_CLIP3 (iLumaQp, pWelsSvcRc->iMinFrameQp, pWelsSvcRc->iMaxFrameQp);
506
507 if (pEncCtx->pSvcParam->bEnableAdaptiveQuant) {
508
509 iLumaQp = WELS_DIV_ROUND (iLumaQp * INT_MULTIPLY - pEncCtx->pVaa->sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp,
510 INT_MULTIPLY);
511 iLumaQp = WELS_CLIP3 (iLumaQp, pWelsSvcRc->iMinFrameQp, pWelsSvcRc->iMaxFrameQp);
512 }
513 pWelsSvcRc->iQStep = RcConvertQp2QStep (iLumaQp);
514 pWelsSvcRc->iLastCalculatedQScale = iLumaQp;
515 pEncCtx->iGlobalQp = iLumaQp;
516 }
517
GomRCInitForOneSlice(SSlice * pSlice,const int32_t kiBitsPerMb)518 void GomRCInitForOneSlice (SSlice* pSlice, const int32_t kiBitsPerMb) {
519 SRCSlicing* pSOverRc = &pSlice->sSlicingOverRc;
520 pSOverRc->iStartMbSlice = pSlice->sSliceHeaderExt.sSliceHeader.iFirstMbInSlice;
521 pSOverRc->iEndMbSlice = pSOverRc->iStartMbSlice + pSlice->iCountMbNumInSlice - 1;
522 pSOverRc->iTargetBitsSlice = WELS_DIV_ROUND (static_cast<int64_t> (kiBitsPerMb) * pSlice->iCountMbNumInSlice,
523 INT_MULTIPLY);
524 }
525
RcInitSliceInformation(sWelsEncCtx * pEncCtx)526 void RcInitSliceInformation (sWelsEncCtx* pEncCtx) {
527 SSlice** ppSliceInLayer = pEncCtx->pCurDqLayer->ppSliceInLayer;
528 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
529 const int32_t kiSliceNum = pEncCtx->pCurDqLayer->iMaxSliceNum;
530 pWelsSvcRc->iBitsPerMb = WELS_DIV_ROUND (static_cast<int64_t> (pWelsSvcRc->iTargetBits) * INT_MULTIPLY,
531 pWelsSvcRc->iNumberMbFrame);
532 pWelsSvcRc->bGomRC = (RC_OFF_MODE == pEncCtx->pSvcParam->iRCMode ||
533 RC_BUFFERBASED_MODE == pEncCtx->pSvcParam->iRCMode) ? false : true;
534 for (int32_t i = 0; i < kiSliceNum; i++) {
535 SRCSlicing* pSOverRc = &ppSliceInLayer[i]->sSlicingOverRc;
536 pSOverRc->iTotalQpSlice = 0;
537 pSOverRc->iTotalMbSlice = 0;
538 pSOverRc->iFrameBitsSlice = 0;
539 pSOverRc->iGomBitsSlice = 0;
540 pSOverRc->iStartMbSlice = 0;
541 pSOverRc->iEndMbSlice = 0;
542 pSOverRc->iTargetBitsSlice = 0;
543 }
544 }
545
RcDecideTargetBits(sWelsEncCtx * pEncCtx)546 void RcDecideTargetBits (sWelsEncCtx* pEncCtx) {
547 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
548 SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[pEncCtx->uiTemporalId];
549
550 pWelsSvcRc->iCurrentBitsLevel = BITS_NORMAL;
551 //allocate bits
552 if (pEncCtx->eSliceType == I_SLICE) {
553 pWelsSvcRc->iTargetBits = pWelsSvcRc->iBitsPerFrame * IDR_BITRATE_RATIO;
554 } else {
555 if (pWelsSvcRc->iRemainingWeights > pTOverRc->iTlayerWeight)
556 pWelsSvcRc->iTargetBits = WELS_DIV_ROUND (static_cast<int64_t> (pWelsSvcRc->iRemainingBits) * pTOverRc->iTlayerWeight,
557 pWelsSvcRc->iRemainingWeights);
558 else //this case should be not hit. needs to more test case to verify this
559 pWelsSvcRc->iTargetBits = pWelsSvcRc->iRemainingBits;
560 if ((pWelsSvcRc->iTargetBits <= 0) && ((pEncCtx->pSvcParam->iRCMode == RC_BITRATE_MODE)
561 && (pEncCtx->pSvcParam->bEnableFrameSkip == false))) {
562 pWelsSvcRc->iCurrentBitsLevel = BITS_EXCEEDED;
563 }
564 pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, pTOverRc->iMinBitsTl, pTOverRc->iMaxBitsTl);
565 }
566 pWelsSvcRc->iRemainingWeights -= pTOverRc->iTlayerWeight;
567
568 }
569
RcDecideTargetBitsTimestamp(sWelsEncCtx * pEncCtx)570 void RcDecideTargetBitsTimestamp (sWelsEncCtx* pEncCtx) {
571 //decide one frame bits allocated
572 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
573 SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId];
574 int32_t iTl = pEncCtx->uiTemporalId;
575 SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[iTl];
576 pWelsSvcRc->iCurrentBitsLevel = BITS_NORMAL;
577
578 if (pEncCtx->eSliceType == I_SLICE) {
579 int32_t iBufferTh = static_cast<int32_t> (pWelsSvcRc->iBufferSizeSkip - pWelsSvcRc->iBufferFullnessSkip);
580 if (iBufferTh <= 0) {
581 pWelsSvcRc->iCurrentBitsLevel = BITS_EXCEEDED;
582 pWelsSvcRc->iTargetBits = pTOverRc->iMinBitsTl;
583 } else {
584 int32_t iMaxTh = iBufferTh * 3 / 4;
585 int32_t iMinTh = static_cast<int32_t>((pDLayerParam->fFrameRate < 8) ? iBufferTh * 1.0 / 4 : iBufferTh * 2 / pDLayerParam->fFrameRate);
586 if (pDLayerParam->fFrameRate < (IDR_BITRATE_RATIO + 1))
587 pWelsSvcRc->iTargetBits = static_cast<int32_t> (((double) (pDLayerParam->iSpatialBitrate) / (double) (
588 pDLayerParam->fFrameRate)));
589 else
590 pWelsSvcRc->iTargetBits = static_cast<int32_t> (((double) (pDLayerParam->iSpatialBitrate) / (double) (
591 pDLayerParam->fFrameRate) * IDR_BITRATE_RATIO));
592 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
593 "iMaxTh = %d,iMinTh = %d,pWelsSvcRc->iTargetBits = %d,pWelsSvcRc->iBufferSizeSkip = %d, pWelsSvcRc->iBufferFullnessSkip= %"
594 PRId64,
595 iMaxTh, iMinTh, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferSizeSkip, pWelsSvcRc->iBufferFullnessSkip);
596 pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh);
597 }
598
599 } else {
600 int32_t iBufferTh = static_cast<int32_t> (pWelsSvcRc->iBufferSizeSkip - pWelsSvcRc->iBufferFullnessSkip);
601 if (iBufferTh <= 0) {
602 pWelsSvcRc->iCurrentBitsLevel = BITS_EXCEEDED;
603 pWelsSvcRc->iTargetBits = pTOverRc->iMinBitsTl;
604 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
605 "iMaxTh = %d,pWelsSvcRc->iTargetBits = %d,pWelsSvcRc->iBufferSizeSkip = %d, pWelsSvcRc->iBufferFullnessSkip= %" PRId64,
606 iBufferTh, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferSizeSkip, pWelsSvcRc->iBufferFullnessSkip);
607 } else {
608
609 SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId];
610 const int32_t kiGopSize = (1 << pDLayerParamInternal->iDecompositionStages);
611 int32_t iAverageFrameSize = (int32_t) ((double) (pDLayerParam->iSpatialBitrate) / (double) (pDLayerParam->fFrameRate));
612 const int32_t kiGopBits = iAverageFrameSize * kiGopSize;
613 pWelsSvcRc->iTargetBits = WELS_DIV_ROUND (pTOverRc->iTlayerWeight * kiGopBits, INT_MULTIPLY * 10 * 2);
614
615 int32_t iMaxTh = iBufferTh / 2;
616 int32_t iMinTh = static_cast<int32_t>((pDLayerParam->fFrameRate < 8) ? iBufferTh * 1.0 / 4 : iBufferTh * 2 / pDLayerParam->fFrameRate);
617 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
618 "iMaxTh = %d,iMinTh = %d,pWelsSvcRc->iTargetBits = %d,pWelsSvcRc->iBufferSizeSkip = %d, pWelsSvcRc->iBufferFullnessSkip= % "
619 PRId64,
620 iMaxTh, iMinTh, pWelsSvcRc->iTargetBits, pWelsSvcRc->iBufferSizeSkip, pWelsSvcRc->iBufferFullnessSkip);
621 pWelsSvcRc->iTargetBits = WELS_CLIP3 (pWelsSvcRc->iTargetBits, iMinTh, iMaxTh);
622 }
623 }
624 }
625
RcInitGomParameters(sWelsEncCtx * pEncCtx)626 void RcInitGomParameters (sWelsEncCtx* pEncCtx) {
627 SSlice** ppSliceInLayer = pEncCtx->pCurDqLayer->ppSliceInLayer;
628 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
629 SRCSlicing* pSOverRc = &ppSliceInLayer[0]->sSlicingOverRc;
630 const int32_t kiSliceNum = pEncCtx->pCurDqLayer->iMaxSliceNum;
631 const int32_t kiGlobalQp = pEncCtx->iGlobalQp;
632
633 pWelsSvcRc->iAverageFrameQp = 0;
634 for (int32_t i = 0; i < kiSliceNum; ++i) {
635 pSOverRc = &ppSliceInLayer[i]->sSlicingOverRc;
636 pSOverRc->iComplexityIndexSlice = 0;
637 pSOverRc->iCalculatedQpSlice = kiGlobalQp;
638 }
639 memset (pWelsSvcRc->pGomComplexity, 0, pWelsSvcRc->iGomSize * sizeof (double));
640 memset (pWelsSvcRc->pGomCost, 0, pWelsSvcRc->iGomSize * sizeof (int32_t));
641 }
642
RcCalculateMbQp(sWelsEncCtx * pEncCtx,SSlice * pSlice,SMB * pCurMb)643 void RcCalculateMbQp (sWelsEncCtx* pEncCtx, SSlice* pSlice, SMB* pCurMb) {
644 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
645 SRCSlicing* pSOverRc = &pSlice->sSlicingOverRc;
646
647 int32_t iLumaQp = pSOverRc->iCalculatedQpSlice;
648 SDqLayer* pCurLayer = pEncCtx->pCurDqLayer;
649 const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset;
650 if (pEncCtx->pSvcParam->bEnableAdaptiveQuant) {
651 iLumaQp = (int8_t)WELS_CLIP3 (iLumaQp +
652 pEncCtx->pVaa->sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp[pCurMb->iMbXY], pWelsSvcRc->iMinFrameQp,
653 pWelsSvcRc->iMaxFrameQp);
654 }
655 pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (iLumaQp + kuiChromaQpIndexOffset)];
656 pCurMb->uiLumaQp = iLumaQp;
657 }
658
RcJudgeBaseUsability(sWelsEncCtx * pEncCtx)659 SWelsSvcRc* RcJudgeBaseUsability (sWelsEncCtx* pEncCtx) {
660 SWelsSvcRc* pWelsSvcRc = NULL, *pWelsSvcRc_Base = NULL;
661 SSpatialLayerConfig* pDlpBase = NULL, *pDLayerParam = NULL;
662 SSpatialLayerInternal* pDlpBaseInternal = NULL;
663 if (pEncCtx->uiDependencyId <= 0)
664 return NULL;
665 pDlpBaseInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId - 1];
666 pDlpBase = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId - 1];
667 pWelsSvcRc_Base = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId - 1];
668 if (pEncCtx->uiTemporalId <= pDlpBaseInternal->iDecompositionStages) {
669 pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
670 pWelsSvcRc_Base = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId - 1];
671 pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId];
672 pDlpBase = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId - 1];
673 if ((pDLayerParam->iVideoWidth * pDLayerParam->iVideoHeight / pWelsSvcRc->iNumberMbGom) ==
674 (pDlpBase->iVideoWidth * pDlpBase->iVideoHeight / pWelsSvcRc_Base->iNumberMbGom))
675 return pWelsSvcRc_Base;
676 else
677 return NULL;
678 } else
679 return NULL;
680 }
681
RcGomTargetBits(sWelsEncCtx * pEncCtx,SSlice * pSlice)682 void RcGomTargetBits (sWelsEncCtx* pEncCtx, SSlice* pSlice) {
683 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
684 SWelsSvcRc* pWelsSvcRc_Base = NULL;
685 SRCSlicing* pSOverRc = &pSlice->sSlicingOverRc;
686
687 int32_t iAllocateBits = 0;
688 int32_t iSumSad = 0;
689 int32_t iLastGomIndex = 0;
690 int32_t iLeftBits = 0;
691 const int32_t kiComplexityIndex = pSOverRc->iComplexityIndexSlice;
692 int32_t i;
693
694 iLastGomIndex = pSOverRc->iEndMbSlice / pWelsSvcRc->iNumberMbGom;
695 iLeftBits = pSOverRc->iTargetBitsSlice - pSOverRc->iFrameBitsSlice;
696 if (iLeftBits <= 0) {
697 pSOverRc->iGomTargetBits = 0;
698 return;
699 } else if (kiComplexityIndex >= iLastGomIndex) {
700 iAllocateBits = iLeftBits;
701 } else {
702 pWelsSvcRc_Base = RcJudgeBaseUsability (pEncCtx);
703 pWelsSvcRc_Base = (pWelsSvcRc_Base) ? pWelsSvcRc_Base : pWelsSvcRc;
704 for (i = kiComplexityIndex + 1; i <= iLastGomIndex; i++) {
705 iSumSad += pWelsSvcRc_Base->pCurrentFrameGomSad[i];
706 }
707
708 if (0 == iSumSad)
709 iAllocateBits = WELS_DIV_ROUND (iLeftBits, (iLastGomIndex - kiComplexityIndex));
710 else
711 iAllocateBits = WELS_DIV_ROUND ((int64_t)iLeftBits * pWelsSvcRc_Base->pCurrentFrameGomSad[kiComplexityIndex + 1],
712 iSumSad);
713 }
714 pSOverRc->iGomTargetBits = iAllocateBits;
715 }
716
717
718
RcCalculateGomQp(sWelsEncCtx * pEncCtx,SSlice * pSlice,SMB * pCurMb)719 void RcCalculateGomQp (sWelsEncCtx* pEncCtx, SSlice* pSlice, SMB* pCurMb) {
720 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
721 SRCSlicing* pSOverRc = &pSlice->sSlicingOverRc;
722 int64_t iBitsRatio = 1;
723
724 int64_t iLeftBits = pSOverRc->iTargetBitsSlice - pSOverRc->iFrameBitsSlice;
725 int64_t iTargetLeftBits = iLeftBits + pSOverRc->iGomBitsSlice - pSOverRc->iGomTargetBits;
726 if ((iLeftBits <= 0) || (iTargetLeftBits <= 0)) {
727 pSOverRc->iCalculatedQpSlice += 2;
728 } else {
729 //globe decision
730 iBitsRatio = 10000 * iLeftBits / (iTargetLeftBits + 1);
731 if (iBitsRatio < 8409) //2^(-1.5/6)*10000
732 pSOverRc->iCalculatedQpSlice += 2;
733 else if (iBitsRatio < 9439) //2^(-0.5/6)*10000
734 pSOverRc->iCalculatedQpSlice += 1;
735 else if (iBitsRatio > 10600) //2^(0.5/6)*10000
736 pSOverRc->iCalculatedQpSlice -= 1;
737 else if (iBitsRatio > 11900) //2^(1.5/6)*10000
738 pSOverRc->iCalculatedQpSlice -= 2;
739 }
740 pSOverRc->iCalculatedQpSlice = WELS_CLIP3 (pSOverRc->iCalculatedQpSlice, pWelsSvcRc->iMinFrameQp,
741 pWelsSvcRc->iMaxFrameQp);
742 // WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,"iCalculatedQpSlice =%d,iBitsRatio = %d\n",pSOverRc->iCalculatedQpSlice,iBitsRatio);
743 pSOverRc->iGomBitsSlice = 0;
744
745 }
746
RcVBufferCalculationSkip(sWelsEncCtx * pEncCtx)747 void RcVBufferCalculationSkip (sWelsEncCtx* pEncCtx) {
748 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
749 SRCTemporal* pTOverRc = pWelsSvcRc->pTemporalOverRc;
750 const int32_t kiOutputBits = pWelsSvcRc->iBitsPerFrame;
751 const int32_t kiOutputMaxBits = pWelsSvcRc->iMaxBitsPerFrame;
752 //condition 1: whole pBuffer fullness
753 pWelsSvcRc->iBufferFullnessSkip += (pWelsSvcRc->iFrameDqBits - kiOutputBits);
754 pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW] += (pWelsSvcRc->iFrameDqBits - kiOutputMaxBits);
755 pWelsSvcRc->iBufferMaxBRFullness[ODD_TIME_WINDOW] += (pWelsSvcRc->iFrameDqBits - kiOutputMaxBits);
756
757 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
758 "[Rc] bits in buffer = %" PRId64 ", bits in Max bitrate buffer = %" PRId64,
759 pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW]);
760 //condition 2: VGOP bits constraint
761 int64_t iVGopBitsPred = 0;
762 for (int32_t i = pWelsSvcRc->iFrameCodedInVGop + 1; i < VGOP_SIZE; i++)
763 iVGopBitsPred += pTOverRc[pWelsSvcRc->iTlOfFrames[i]].iMinBitsTl;
764 iVGopBitsPred -= pWelsSvcRc->iRemainingBits;
765 double dIncPercent = iVGopBitsPred * 100.0 / (pWelsSvcRc->iBitsPerFrame * VGOP_SIZE) -
766 (double)VGOP_BITS_PERCENTAGE_DIFF;
767
768 if ((pWelsSvcRc->iBufferFullnessSkip > pWelsSvcRc->iBufferSizeSkip
769 && pWelsSvcRc->iAverageFrameQp > pWelsSvcRc->iSkipQpValue)
770 || (dIncPercent > pWelsSvcRc->iRcVaryPercentage)) {
771 pWelsSvcRc->bSkipFlag = true;
772 }
773 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
774 "[Rc] VBV_Skip,dIncPercent = %f,iRcVaryPercentage = %d,pWelsSvcRc->bSkipFlag = %d", dIncPercent,
775 pWelsSvcRc->iRcVaryPercentage, pWelsSvcRc->bSkipFlag);
776 }
CheckFrameSkipBasedMaxbr(sWelsEncCtx * pEncCtx,const long long uiTimeStamp,int32_t iDidIdx)777 void CheckFrameSkipBasedMaxbr (sWelsEncCtx* pEncCtx, const long long uiTimeStamp, int32_t iDidIdx) {
778 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[iDidIdx];
779 SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[iDidIdx];
780 //SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId];
781 if (!pEncCtx->pSvcParam->bEnableFrameSkip)
782 return;
783 const int32_t iSentBits = pWelsSvcRc->iBitsPerFrame;
784 const int32_t kiOutputMaxBits = pWelsSvcRc->iMaxBitsPerFrame;
785 const int64_t kiMaxSpatialBitRate = pDLayerParam->iMaxSpatialBitrate;
786
787 //estimate allowed continual skipped frames in the sequence
788 const int32_t iPredSkipFramesTarBr = (WELS_DIV_ROUND (pWelsSvcRc->iBufferFullnessSkip, iSentBits) + 1) >> 1;
789 const int32_t iPredSkipFramesMaxBr = (WELS_MAX (WELS_DIV_ROUND (pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW],
790 kiOutputMaxBits), 0) + 1) >> 1;
791
792 //calculate the remaining bits in TIME_CHECK_WINDOW
793 const int32_t iAvailableBitsInTimeWindow = WELS_DIV_ROUND ((TIME_CHECK_WINDOW - pEncCtx->iCheckWindowInterval) *
794 kiMaxSpatialBitRate, 1000);
795 const int32_t iAvailableBitsInShiftTimeWindow = WELS_DIV_ROUND ((TIME_CHECK_WINDOW - pEncCtx->iCheckWindowIntervalShift)
796 * kiMaxSpatialBitRate, 1000);
797
798 bool bJudgeMaxBRbSkip[TIME_WINDOW_TOTAL];//0: EVEN_TIME_WINDOW; 1: ODD_TIME_WINDOW
799
800 /* 4 cases for frame skipping
801 1:skipping when buffer size larger than target threshold and current continual skip frames is allowed
802 2:skipping when MaxBr buffer size + predict frame size - remaining bits in time window < 0 and current continual skip frames is allowed
803 3:if in last ODD_TIME_WINDOW the MAX Br is overflowed, make more strict skipping conditions
804 4:such as case 3 in the other window
805 */
806 bool bJudgeBufferFullSkip = (pWelsSvcRc->iContinualSkipFrames <= iPredSkipFramesTarBr)
807 && (pWelsSvcRc->iBufferFullnessSkip > pWelsSvcRc->iBufferSizeSkip);
808 bool bJudgeMaxBRbufferFullSkip = (pWelsSvcRc->iContinualSkipFrames <= iPredSkipFramesMaxBr)
809 && (pEncCtx->iCheckWindowInterval > TIME_CHECK_WINDOW / 2)
810 && (pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW] + pWelsSvcRc->iPredFrameBit - iAvailableBitsInTimeWindow > 0);
811 bJudgeMaxBRbSkip[EVEN_TIME_WINDOW] = (pEncCtx->iCheckWindowInterval > TIME_CHECK_WINDOW / 2)
812 && (pWelsSvcRc->bNeedShiftWindowCheck[EVEN_TIME_WINDOW])
813 && (pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW] + pWelsSvcRc->iPredFrameBit - iAvailableBitsInTimeWindow +
814 kiOutputMaxBits > 0);
815 bJudgeMaxBRbSkip[ODD_TIME_WINDOW] = (pEncCtx->iCheckWindowIntervalShift > TIME_CHECK_WINDOW / 2)
816 && (pWelsSvcRc->bNeedShiftWindowCheck[ODD_TIME_WINDOW])
817 && (pWelsSvcRc->iBufferMaxBRFullness[ODD_TIME_WINDOW] + pWelsSvcRc->iPredFrameBit - iAvailableBitsInShiftTimeWindow +
818 kiOutputMaxBits > 0);
819
820 pWelsSvcRc->bSkipFlag = false;
821 if (bJudgeBufferFullSkip || bJudgeMaxBRbufferFullSkip || bJudgeMaxBRbSkip[EVEN_TIME_WINDOW]
822 || bJudgeMaxBRbSkip[ODD_TIME_WINDOW]) {
823 pWelsSvcRc->bSkipFlag = true;
824 pWelsSvcRc->iSkipFrameNum++;
825 pWelsSvcRc->iSkipFrameInVGop++;
826 pWelsSvcRc->iBufferFullnessSkip -= iSentBits;
827 pWelsSvcRc->iRemainingBits += iSentBits;
828 pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW] -= kiOutputMaxBits;
829 pWelsSvcRc->iBufferMaxBRFullness[ODD_TIME_WINDOW] -= kiOutputMaxBits;
830 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
831 "[Rc] bits in buffer = %" PRId64 ", bits in Max bitrate buffer = %" PRId64 ", Predict skip frames = %d and %d",
832 pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW], iPredSkipFramesTarBr,
833 iPredSkipFramesMaxBr);
834 pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (pWelsSvcRc->iBufferFullnessSkip, 0);
835 }
836 }
837
WelsRcCheckFrameStatus(sWelsEncCtx * pEncCtx,long long uiTimeStamp,int32_t iSpatialNum,int32_t iCurDid)838 bool WelsRcCheckFrameStatus (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_t iSpatialNum, int32_t iCurDid) {
839
840 bool bSkipMustFlag = false;
841
842 SSpatialPicIndex* pSpatialIndexMap = &pEncCtx->sSpatialIndexMap[0];
843
844 //simul_cast AVC control
845 if (pEncCtx->pSvcParam->bSimulcastAVC) {
846 //check target_br skip and update info
847 int32_t iDidIdx = iCurDid;
848 if (pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge) {
849 pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge (pEncCtx, uiTimeStamp, iDidIdx);
850 }
851 if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) {
852 bSkipMustFlag = true;
853 }
854 //check max_br skip
855 if (pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr) {
856 if ((!bSkipMustFlag) && (pEncCtx->pSvcParam->sSpatialLayers[iDidIdx].iMaxSpatialBitrate != UNSPECIFIED_BIT_RATE)) {
857 pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr (pEncCtx, uiTimeStamp, iDidIdx);
858 if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) {
859 bSkipMustFlag = true;
860
861 }
862 }
863 }
864 if (bSkipMustFlag) {
865 pEncCtx->pWelsSvcRc[iDidIdx].uiLastTimeStamp = uiTimeStamp;
866 pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag = false;
867 pEncCtx->pWelsSvcRc[iDidIdx].iContinualSkipFrames++;
868 return true;
869 }
870 } else { //SVC control
871 for (int32_t i = 0; i < iSpatialNum; i++) {
872 int32_t iDidIdx = (pSpatialIndexMap + i)->iDid;
873 //check target_br skip and update info
874
875 if (pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge) {
876 pEncCtx->pFuncList->pfRc.pfWelsRcPicDelayJudge (pEncCtx, uiTimeStamp, iDidIdx);
877 }
878 if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) {
879 bSkipMustFlag = true;
880 }
881 //check max_br skip
882 if (pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr) {
883 if ((!bSkipMustFlag) && (pEncCtx->pSvcParam->sSpatialLayers[iDidIdx].iMaxSpatialBitrate != UNSPECIFIED_BIT_RATE)) {
884 pEncCtx->pFuncList->pfRc.pfWelsCheckSkipBasedMaxbr (pEncCtx, uiTimeStamp, iDidIdx);
885 if (true == pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag) {
886 bSkipMustFlag = true;
887 }
888 }
889 }
890 if (bSkipMustFlag) {
891 break;
892 }
893 }
894
895 if (bSkipMustFlag) {
896 for (int32_t i = 0; i < iSpatialNum; i++) {
897 int32_t iDidIdx = (pSpatialIndexMap + i)->iDid;
898 pEncCtx->pWelsSvcRc[iDidIdx].uiLastTimeStamp = uiTimeStamp;
899 pEncCtx->pWelsSvcRc[iDidIdx].bSkipFlag = false;
900 pEncCtx->pWelsSvcRc[iDidIdx].iContinualSkipFrames++;
901 }
902 return true;
903 }
904 }
905 return false;
906 }
UpdateBufferWhenFrameSkipped(sWelsEncCtx * pEncCtx,int32_t iCurDid)907 void UpdateBufferWhenFrameSkipped (sWelsEncCtx* pEncCtx, int32_t iCurDid) {
908 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[iCurDid];
909 const int32_t kiOutputBits = pWelsSvcRc->iBitsPerFrame;
910 const int32_t kiOutputMaxBits = pWelsSvcRc->iMaxBitsPerFrame;
911 pWelsSvcRc->iBufferFullnessSkip = pWelsSvcRc->iBufferFullnessSkip - kiOutputBits;
912 pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW] -= kiOutputMaxBits;
913 pWelsSvcRc->iBufferMaxBRFullness[ODD_TIME_WINDOW] -= kiOutputMaxBits;
914 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
915 "[Rc] iDid = %d,bits in buffer = %" PRId64 ", bits in Max bitrate buffer = %" PRId64,
916 iCurDid, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferMaxBRFullness[EVEN_TIME_WINDOW]);
917
918 pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (pWelsSvcRc->iBufferFullnessSkip, 0);
919
920 pWelsSvcRc->iRemainingBits += kiOutputBits;
921 pWelsSvcRc->iSkipFrameNum++;
922 pWelsSvcRc->iSkipFrameInVGop++;
923
924 if ((pWelsSvcRc->iContinualSkipFrames % 3) == 0) {
925 //output a warning when iContinualSkipFrames is large enough, which may indicate subjective quality problem
926 //note that here iContinualSkipFrames must be >0, so the log output will be 3/6/....
927 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_WARNING, "[Rc] iDid = %d,iContinualSkipFrames(%d) is large",
928 iCurDid, pWelsSvcRc->iContinualSkipFrames);
929 }
930 }
UpdateMaxBrCheckWindowStatus(sWelsEncCtx * pEncCtx,int32_t iSpatialNum,const long long uiTimeStamp)931 void UpdateMaxBrCheckWindowStatus (sWelsEncCtx* pEncCtx, int32_t iSpatialNum, const long long uiTimeStamp) {
932 SSpatialPicIndex* pSpatialIndexMap = &pEncCtx->sSpatialIndexMap[0];
933 if (pEncCtx->bCheckWindowStatusRefreshFlag) {
934 pEncCtx->iCheckWindowCurrentTs = uiTimeStamp;
935 } else {
936 pEncCtx->iCheckWindowCurrentTs = pEncCtx->iCheckWindowStartTs = uiTimeStamp;
937 pEncCtx->bCheckWindowStatusRefreshFlag = true;
938 for (int32_t i = 0; i < iSpatialNum; i++) {
939 int32_t iCurDid = (pSpatialIndexMap + i)->iDid;
940 pEncCtx->pWelsSvcRc[iCurDid].iBufferFullnessSkip = 0;
941 pEncCtx->pWelsSvcRc[iCurDid].iBufferMaxBRFullness[ODD_TIME_WINDOW] = 0;
942 pEncCtx->pWelsSvcRc[iCurDid].iBufferMaxBRFullness[EVEN_TIME_WINDOW] = 0;
943 pEncCtx->pWelsSvcRc[iCurDid].bNeedShiftWindowCheck[ODD_TIME_WINDOW] = false;
944 pEncCtx->pWelsSvcRc[iCurDid].bNeedShiftWindowCheck[EVEN_TIME_WINDOW] = false;
945 }
946
947 }
948 pEncCtx->iCheckWindowInterval = (int32_t) (pEncCtx->iCheckWindowCurrentTs - pEncCtx->iCheckWindowStartTs);
949 if (pEncCtx->iCheckWindowInterval >= (TIME_CHECK_WINDOW >> 1) && !pEncCtx->bCheckWindowShiftResetFlag) {
950 pEncCtx->bCheckWindowShiftResetFlag = true;
951 for (int32_t i = 0; i < iSpatialNum; i++) {
952 int32_t iCurDid = (pSpatialIndexMap + i)->iDid;
953 if (pEncCtx->pWelsSvcRc[iCurDid].iBufferMaxBRFullness[ODD_TIME_WINDOW] > 0
954 && pEncCtx->pWelsSvcRc[iCurDid].iBufferMaxBRFullness[ODD_TIME_WINDOW] !=
955 pEncCtx->pWelsSvcRc[iCurDid].iBufferMaxBRFullness[0]) {
956 pEncCtx->pWelsSvcRc[iCurDid].bNeedShiftWindowCheck[EVEN_TIME_WINDOW] = true;
957 } else {
958 pEncCtx->pWelsSvcRc[iCurDid].bNeedShiftWindowCheck[EVEN_TIME_WINDOW] = false;
959 }
960 pEncCtx->pWelsSvcRc[iCurDid].iBufferMaxBRFullness[ODD_TIME_WINDOW] = 0;
961 }
962 }
963 pEncCtx->iCheckWindowIntervalShift = pEncCtx->iCheckWindowInterval >= (TIME_CHECK_WINDOW >> 1) ?
964 pEncCtx->iCheckWindowInterval - (TIME_CHECK_WINDOW >> 1) : pEncCtx->iCheckWindowInterval + (TIME_CHECK_WINDOW >> 1);
965
966 if (pEncCtx->iCheckWindowInterval >= TIME_CHECK_WINDOW || pEncCtx->iCheckWindowInterval == 0) {
967 pEncCtx->iCheckWindowStartTs = pEncCtx->iCheckWindowCurrentTs;
968 pEncCtx->iCheckWindowInterval = 0;
969 pEncCtx->bCheckWindowShiftResetFlag = false;
970 for (int32_t i = 0; i < iSpatialNum; i++) {
971 int32_t iCurDid = (pSpatialIndexMap + i)->iDid;
972 if (pEncCtx->pWelsSvcRc[iCurDid].iBufferMaxBRFullness[EVEN_TIME_WINDOW] > 0) {
973 pEncCtx->pWelsSvcRc[iCurDid].bNeedShiftWindowCheck[ODD_TIME_WINDOW] = true;
974 } else {
975 pEncCtx->pWelsSvcRc[iCurDid].bNeedShiftWindowCheck[ODD_TIME_WINDOW] = false;
976 }
977 pEncCtx->pWelsSvcRc[iCurDid].iBufferMaxBRFullness[EVEN_TIME_WINDOW] = 0;
978 }
979 }
980 return;
981 }
982
WelsRcPostFrameSkipping(sWelsEncCtx * pCtx,const int32_t iDid,const long long uiTimeStamp)983 bool WelsRcPostFrameSkipping (sWelsEncCtx* pCtx, const int32_t iDid, const long long uiTimeStamp) {
984 //TODO: put in the decision of rate-control
985 return false;
986 }
987
WelsRcPostFrameSkippedUpdate(sWelsEncCtx * pCtx,const int32_t iDid)988 void WelsRcPostFrameSkippedUpdate (sWelsEncCtx* pCtx, const int32_t iDid) {
989 //TODO: do something to update buffers after post-skipping is done
990 //let RC know post-skipping happened and adjust strategy accordingly
991 }
992
RcVBufferCalculationPadding(sWelsEncCtx * pEncCtx)993 void RcVBufferCalculationPadding (sWelsEncCtx* pEncCtx) {
994 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
995 const int32_t kiOutputBits = pWelsSvcRc->iBitsPerFrame;
996 const int32_t kiBufferThreshold = WELS_DIV_ROUND (PADDING_THRESHOLD * (-pWelsSvcRc->iBufferSizePadding), INT_MULTIPLY);
997
998 pWelsSvcRc->iBufferFullnessPadding += (pWelsSvcRc->iFrameDqBits - kiOutputBits);
999
1000 if (pWelsSvcRc->iBufferFullnessPadding < kiBufferThreshold) {
1001 pWelsSvcRc->iPaddingSize = -pWelsSvcRc->iBufferFullnessPadding;
1002 pWelsSvcRc->iPaddingSize >>= 3; // /8
1003 pWelsSvcRc->iBufferFullnessPadding = 0;
1004 } else
1005 pWelsSvcRc->iPaddingSize = 0;
1006 }
1007
1008
RcTraceFrameBits(sWelsEncCtx * pEncCtx,long long uiTimeStamp,int32_t iFrameSize)1009 void RcTraceFrameBits (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_t iFrameSize) {
1010 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1011 SSpatialLayerInternal* pParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId];
1012 if (pWelsSvcRc->iPredFrameBit != 0)
1013 pWelsSvcRc->iPredFrameBit = (int32_t) (LAST_FRAME_PREDICT_WEIGHT * pWelsSvcRc->iFrameDqBits +
1014 (1 - LAST_FRAME_PREDICT_WEIGHT) * pWelsSvcRc->iPredFrameBit);
1015 else
1016 pWelsSvcRc->iPredFrameBit = pWelsSvcRc->iFrameDqBits;
1017
1018 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
1019 "[Rc]Layer %d: Frame timestamp = %lld, Frame type = %d, encoding_qp = %d, average qp = %d, max qp = %d, min qp = %d, index = %d, "
1020 "iTid = %d, used = %d, bitsperframe = %d, target = %d, remainingbits = %d, skipbuffersize = %d",
1021 pEncCtx->uiDependencyId, uiTimeStamp, pEncCtx->eSliceType, pEncCtx->iGlobalQp, pWelsSvcRc->iAverageFrameQp,
1022 pWelsSvcRc->iMaxFrameQp,
1023 pWelsSvcRc->iMinFrameQp,
1024 pParamInternal->iFrameIndex, pEncCtx->uiTemporalId,
1025 ( pWelsSvcRc->iFrameDqBits > 0 ) ? pWelsSvcRc->iFrameDqBits : (iFrameSize<<3) ,
1026 pWelsSvcRc->iBitsPerFrame,
1027 pWelsSvcRc->iTargetBits, pWelsSvcRc->iRemainingBits, pWelsSvcRc->iBufferSizeSkip);
1028
1029 }
1030
RcUpdatePictureQpBits(sWelsEncCtx * pEncCtx,int32_t iCodedBits)1031 void RcUpdatePictureQpBits (sWelsEncCtx* pEncCtx, int32_t iCodedBits) {
1032 SSlice** ppSliceInLayer = pEncCtx->pCurDqLayer->ppSliceInLayer;
1033 SRCSlicing* pSOverRc = &ppSliceInLayer[0]->sSlicingOverRc;
1034 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1035 SSliceCtx* pCurSliceCtx = &pEncCtx->pCurDqLayer->sSliceEncCtx;
1036 int32_t iTotalQp = 0, iTotalMb = 0;
1037 int32_t i;
1038
1039 if (pEncCtx->eSliceType == P_SLICE) {
1040 for (i = 0; i < pCurSliceCtx->iSliceNumInFrame; i++) {
1041 pSOverRc = &ppSliceInLayer[i]->sSlicingOverRc;
1042 iTotalQp += pSOverRc->iTotalQpSlice;
1043 iTotalMb += pSOverRc->iTotalMbSlice;
1044 }
1045 if (iTotalMb > 0)
1046 pWelsSvcRc->iAverageFrameQp = WELS_DIV_ROUND (INT_MULTIPLY * iTotalQp, iTotalMb * INT_MULTIPLY);
1047 else
1048 pWelsSvcRc->iAverageFrameQp = pEncCtx->iGlobalQp;
1049 } else {
1050 pWelsSvcRc->iAverageFrameQp = pEncCtx->iGlobalQp;
1051 }
1052 pWelsSvcRc->iFrameDqBits = iCodedBits;
1053 pWelsSvcRc->iLastCalculatedQScale = pWelsSvcRc->iAverageFrameQp;
1054 pWelsSvcRc->pTemporalOverRc[pEncCtx->uiTemporalId].iGopBitsDq += pWelsSvcRc->iFrameDqBits;
1055 }
1056
RcUpdateIntraComplexity(sWelsEncCtx * pEncCtx)1057 void RcUpdateIntraComplexity (sWelsEncCtx* pEncCtx) {
1058 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1059 int32_t iAlpha = WELS_DIV_ROUND (INT_MULTIPLY, (1 + pWelsSvcRc->iIdrNum));
1060 if (iAlpha < (INT_MULTIPLY / 4)) iAlpha = INT_MULTIPLY / 4;
1061 int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp);
1062 int64_t iIntraCmplx = iQStep * static_cast<int64_t> (pWelsSvcRc->iFrameDqBits);
1063 int64_t iFrameComplexity = pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity;
1064 if (pEncCtx->pSvcParam->iUsageType == SCREEN_CONTENT_REAL_TIME) {
1065 SVAAFrameInfoExt* pVaa = static_cast<SVAAFrameInfoExt*> (pEncCtx->pVaa);
1066 iFrameComplexity = pVaa->sComplexityScreenParam.iFrameComplexity;
1067 }
1068 if (pWelsSvcRc->iIdrNum == 0) {
1069 pWelsSvcRc->iIntraComplexity = iIntraCmplx;
1070 pWelsSvcRc->iIntraComplxMean = iFrameComplexity;
1071 } else {
1072 pWelsSvcRc->iIntraComplexity = WELS_DIV_ROUND64 (((LINEAR_MODEL_DECAY_FACTOR) * pWelsSvcRc->iIntraComplexity +
1073 (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) *
1074 iIntraCmplx), INT_MULTIPLY);
1075
1076
1077 pWelsSvcRc->iIntraComplxMean = WELS_DIV_ROUND64 (((LINEAR_MODEL_DECAY_FACTOR) * static_cast<int64_t>
1078 (pWelsSvcRc->iIntraComplxMean)
1079 + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * (iFrameComplexity)),
1080 INT_MULTIPLY);
1081 }
1082
1083 pWelsSvcRc->iIntraMbCount = pWelsSvcRc->iNumberMbFrame;
1084 pWelsSvcRc->iIdrNum++;
1085 if (pWelsSvcRc->iIdrNum > 255)
1086 pWelsSvcRc->iIdrNum = 255;
1087 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
1088 "RcUpdateIntraComplexity iFrameDqBits = %d,iQStep= %d,iIntraCmplx = %" PRId64,
1089 pWelsSvcRc->iFrameDqBits, pWelsSvcRc->iQStep, pWelsSvcRc->iIntraComplexity);
1090
1091 }
1092
RcUpdateFrameComplexity(sWelsEncCtx * pEncCtx)1093 void RcUpdateFrameComplexity (sWelsEncCtx* pEncCtx) {
1094 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1095 const int32_t kiTl = pEncCtx->uiTemporalId;
1096 SRCTemporal* pTOverRc = &pWelsSvcRc->pTemporalOverRc[kiTl];
1097
1098 int64_t iFrameComplexity = pEncCtx->pVaa->sComplexityAnalysisParam.iFrameComplexity;
1099 if (pEncCtx->pSvcParam->iUsageType == SCREEN_CONTENT_REAL_TIME) {
1100 SVAAFrameInfoExt* pVaa = static_cast<SVAAFrameInfoExt*> (pEncCtx->pVaa);
1101 iFrameComplexity = pVaa->sComplexityScreenParam.iFrameComplexity;
1102 }
1103 int32_t iQStep = RcConvertQp2QStep (pWelsSvcRc->iAverageFrameQp);
1104 int32_t iAlpha = WELS_DIV_ROUND (INT_MULTIPLY, (1 + pTOverRc->iPFrameNum));
1105 if (iAlpha < SMOOTH_FACTOR_MIN_VALUE)
1106 iAlpha = SMOOTH_FACTOR_MIN_VALUE;
1107 if (0 == pTOverRc->iPFrameNum) {
1108 pTOverRc->iLinearCmplx = ((int64_t)pWelsSvcRc->iFrameDqBits) * iQStep;
1109 pTOverRc->iFrameCmplxMean = (int32_t)iFrameComplexity;
1110 } else {
1111 pTOverRc->iLinearCmplx = WELS_DIV_ROUND64 (((LINEAR_MODEL_DECAY_FACTOR) * (int64_t)pTOverRc->iLinearCmplx
1112 + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * ((int64_t)pWelsSvcRc->iFrameDqBits * iQStep)),
1113 INT_MULTIPLY);
1114 pTOverRc->iFrameCmplxMean = WELS_DIV_ROUND64 (((LINEAR_MODEL_DECAY_FACTOR) * static_cast<int64_t>
1115 (pTOverRc->iFrameCmplxMean)
1116 + (INT_MULTIPLY - LINEAR_MODEL_DECAY_FACTOR) * iFrameComplexity),
1117 INT_MULTIPLY);
1118 }
1119
1120
1121 pTOverRc->iPFrameNum++;
1122 if (pTOverRc->iPFrameNum > 255)
1123 pTOverRc->iPFrameNum = 255;
1124 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
1125 "RcUpdateFrameComplexity iFrameDqBits = %d,iQStep= %d,pWelsSvcRc->iQStep= %d,pTOverRc->iLinearCmplx = %" PRId64,
1126 pWelsSvcRc->iFrameDqBits,
1127 iQStep, pWelsSvcRc->iQStep, pTOverRc->iLinearCmplx);
1128 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "iFrameCmplxMean = %" PRId64 ",iFrameComplexity = %" PRId64,
1129 pTOverRc->iFrameCmplxMean, iFrameComplexity);
1130 }
1131
RcCalculateCascadingQp(struct TagWelsEncCtx * pEncCtx,int32_t iQp)1132 int32_t RcCalculateCascadingQp (struct TagWelsEncCtx* pEncCtx, int32_t iQp) {
1133 int32_t iTemporalQp = 0;
1134 if (pEncCtx->pSvcParam->iDecompStages) {
1135 if (pEncCtx->uiTemporalId == 0)
1136 iTemporalQp = iQp - 3 - (pEncCtx->pSvcParam->iDecompStages - 1);
1137 else
1138 iTemporalQp = iQp - (pEncCtx->pSvcParam->iDecompStages - pEncCtx->uiTemporalId);
1139 iTemporalQp = WELS_CLIP3 (iTemporalQp, 1, 51);
1140 } else
1141 iTemporalQp = iQp;
1142 return iTemporalQp;
1143 }
1144
WelsRcPictureInitGom(sWelsEncCtx * pEncCtx,long long uiTimeStamp)1145 void WelsRcPictureInitGom (sWelsEncCtx* pEncCtx, long long uiTimeStamp) {
1146 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1147 const int32_t kiSliceNum = pEncCtx->pCurDqLayer->iMaxSliceNum;
1148 pWelsSvcRc->iContinualSkipFrames = 0;
1149
1150 if (pEncCtx->eSliceType == I_SLICE) {
1151 if (0 == pWelsSvcRc->iIdrNum) { //iIdrNum == 0 means encoder has been initialed
1152 RcInitRefreshParameter (pEncCtx);
1153 }
1154 }
1155 if (RcJudgeBitrateFpsUpdate (pEncCtx)) {
1156 RcUpdateBitrateFps (pEncCtx);
1157 }
1158 if (pEncCtx->uiTemporalId == 0) {
1159 RcUpdateTemporalZero (pEncCtx);
1160 }
1161 if (pEncCtx->pSvcParam->iRCMode == RC_TIMESTAMP_MODE) {
1162 RcDecideTargetBitsTimestamp (pEncCtx);
1163 pWelsSvcRc->uiLastTimeStamp = uiTimeStamp;
1164 } else {
1165 RcDecideTargetBits (pEncCtx);
1166 }
1167 //turn off GOM QP when slicenum is larger 1
1168 if ((kiSliceNum > 1) || ((pEncCtx->pSvcParam->iRCMode == RC_BITRATE_MODE)
1169 && (pEncCtx->eSliceType == I_SLICE))) {
1170 pWelsSvcRc->bEnableGomQp = false;
1171 } else
1172 pWelsSvcRc->bEnableGomQp = true;
1173
1174 //decide globe_qp
1175 if (pEncCtx->eSliceType == I_SLICE) {
1176 RcCalculateIdrQp (pEncCtx);
1177 } else {
1178 RcCalculatePictureQp (pEncCtx);
1179 }
1180 RcInitSliceInformation (pEncCtx);
1181 RcInitGomParameters (pEncCtx);
1182 }
1183
WelsRcPictureInfoUpdateGom(sWelsEncCtx * pEncCtx,int32_t iLayerSize)1184 void WelsRcPictureInfoUpdateGom (sWelsEncCtx* pEncCtx, int32_t iLayerSize) {
1185 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1186 int32_t iCodedBits = (iLayerSize << 3);
1187
1188 RcUpdatePictureQpBits (pEncCtx, iCodedBits);
1189
1190 if (pEncCtx->eSliceType == P_SLICE) {
1191 RcUpdateFrameComplexity (pEncCtx);
1192 } else {
1193 RcUpdateIntraComplexity (pEncCtx);
1194 }
1195 pWelsSvcRc->iRemainingBits -= pWelsSvcRc->iFrameDqBits;
1196
1197 if (pEncCtx->pSvcParam->bEnableFrameSkip /*&&
1198 pEncCtx->uiDependencyId == pEncCtx->pSvcParam->iSpatialLayerNum - 1*/) {
1199 RcVBufferCalculationSkip (pEncCtx);
1200 }
1201
1202 if (pEncCtx->pSvcParam->iPaddingFlag)
1203 RcVBufferCalculationPadding (pEncCtx);
1204 pWelsSvcRc->iFrameCodedInVGop++;
1205 }
1206
WelsRcMbInitGom(sWelsEncCtx * pEncCtx,SMB * pCurMb,SSlice * pSlice)1207 void WelsRcMbInitGom (sWelsEncCtx* pEncCtx, SMB* pCurMb, SSlice* pSlice) {
1208 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1209 SRCSlicing* pSOverRc = &pSlice->sSlicingOverRc;
1210 SDqLayer* pCurLayer = pEncCtx->pCurDqLayer;
1211 const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset;
1212
1213 pSOverRc->iBsPosSlice = pEncCtx->pFuncList->pfGetBsPosition (pSlice);
1214 if (pWelsSvcRc->bEnableGomQp) {
1215 //calculate gom qp and target bits at the beginning of gom
1216 if (0 == (pCurMb->iMbXY % pWelsSvcRc->iNumberMbGom)) {
1217 if (pCurMb->iMbXY != pSOverRc->iStartMbSlice) {
1218 pSOverRc->iComplexityIndexSlice++;
1219 RcCalculateGomQp (pEncCtx, pSlice, pCurMb);
1220 }
1221 RcGomTargetBits (pEncCtx, pSlice);
1222 }
1223
1224 RcCalculateMbQp (pEncCtx, pSlice, pCurMb);
1225 } else {
1226 pCurMb->uiLumaQp = pEncCtx->iGlobalQp;
1227 pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (pCurMb->uiLumaQp + kuiChromaQpIndexOffset)];
1228 }
1229
1230 }
1231
WelsRcMbInfoUpdateGom(sWelsEncCtx * pEncCtx,SMB * pCurMb,int32_t iCostLuma,SSlice * pSlice)1232 void WelsRcMbInfoUpdateGom (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice) {
1233 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1234 SRCSlicing* pSOverRc = &pSlice->sSlicingOverRc;
1235 const int32_t kiComplexityIndex = pSOverRc->iComplexityIndexSlice;
1236
1237 int32_t iCurMbBits = pEncCtx->pFuncList->pfGetBsPosition (pSlice) - pSOverRc->iBsPosSlice;
1238 pSOverRc->iFrameBitsSlice += iCurMbBits;
1239 pSOverRc->iGomBitsSlice += iCurMbBits;
1240
1241 pWelsSvcRc->pGomCost[kiComplexityIndex] += iCostLuma;
1242 if (iCurMbBits > 0) {
1243 pSOverRc->iTotalQpSlice += pCurMb->uiLumaQp;
1244 pSOverRc->iTotalMbSlice++;
1245 }
1246 }
1247
WelsRcPictureInitDisable(sWelsEncCtx * pEncCtx,long long uiTimeStamp)1248 void WelsRcPictureInitDisable (sWelsEncCtx* pEncCtx, long long uiTimeStamp) {
1249 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1250 SSpatialLayerConfig* pDLayerParam = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId];
1251 const int32_t kiQp = pDLayerParam->iDLayerQp;
1252
1253 pEncCtx->iGlobalQp = RcCalculateCascadingQp (pEncCtx, kiQp);
1254
1255 if (pEncCtx->pSvcParam->bEnableAdaptiveQuant && (pEncCtx->eSliceType == P_SLICE)) {
1256 pEncCtx->iGlobalQp = WELS_CLIP3 ((pEncCtx->iGlobalQp * INT_MULTIPLY -
1257 pEncCtx->pVaa->sAdaptiveQuantParam.iAverMotionTextureIndexToDeltaQp) / INT_MULTIPLY, pWelsSvcRc->iMinQp,
1258 pWelsSvcRc->iMaxQp);
1259 } else {
1260 pEncCtx->iGlobalQp = WELS_CLIP3 (pEncCtx->iGlobalQp, 0, 51);
1261 }
1262
1263 pWelsSvcRc->iAverageFrameQp = pEncCtx->iGlobalQp;
1264 }
1265
WelsRcPictureInfoUpdateDisable(sWelsEncCtx * pEncCtx,int32_t iLayerSize)1266 void WelsRcPictureInfoUpdateDisable (sWelsEncCtx* pEncCtx, int32_t iLayerSize) {
1267 }
1268
WelsRcMbInitDisable(sWelsEncCtx * pEncCtx,SMB * pCurMb,SSlice * pSlice)1269 void WelsRcMbInitDisable (sWelsEncCtx* pEncCtx, SMB* pCurMb, SSlice* pSlice) {
1270 int32_t iLumaQp = pEncCtx->iGlobalQp;
1271 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1272 SDqLayer* pCurLayer = pEncCtx->pCurDqLayer;
1273
1274 const uint8_t kuiChromaQpIndexOffset = pCurLayer->sLayerInfo.pPpsP->uiChromaQpIndexOffset;
1275
1276
1277 if (pEncCtx->pSvcParam->bEnableAdaptiveQuant && (pEncCtx->eSliceType == P_SLICE)) {
1278 iLumaQp = (int8_t)WELS_CLIP3 (iLumaQp +
1279 pEncCtx->pVaa->sAdaptiveQuantParam.pMotionTextureIndexToDeltaQp[pCurMb->iMbXY], pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
1280 } else {
1281 iLumaQp = WELS_CLIP3 (iLumaQp, 0, 51);
1282 }
1283 pCurMb->uiChromaQp = g_kuiChromaQpTable[CLIP3_QP_0_51 (iLumaQp + kuiChromaQpIndexOffset)];
1284 pCurMb->uiLumaQp = iLumaQp;
1285 }
1286
WelsRcMbInfoUpdateDisable(sWelsEncCtx * pEncCtx,SMB * pCurMb,int32_t iCostLuma,SSlice * pSlice)1287 void WelsRcMbInfoUpdateDisable (sWelsEncCtx* pEncCtx, SMB* pCurMb, int32_t iCostLuma, SSlice* pSlice) {
1288 }
1289
WelRcPictureInitBufferBasedQp(sWelsEncCtx * pEncCtx,long long uiTimeStamp)1290 void WelRcPictureInitBufferBasedQp (sWelsEncCtx* pEncCtx, long long uiTimeStamp) {
1291
1292 SVAAFrameInfo* pVaa = static_cast<SVAAFrameInfo*> (pEncCtx->pVaa);
1293 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1294
1295 int32_t iMinQp = pEncCtx->pSvcParam->iMinQp;
1296 if (pVaa->eSceneChangeIdc == LARGE_CHANGED_SCENE)
1297 iMinQp += 2;
1298 else if (pVaa->eSceneChangeIdc == MEDIUM_CHANGED_SCENE)
1299 iMinQp += 1;
1300 if (pEncCtx->bDeliveryFlag)
1301 pEncCtx->iGlobalQp -= 1;
1302 else
1303 pEncCtx->iGlobalQp += 2;
1304 pEncCtx->iGlobalQp = WELS_CLIP3 (pEncCtx->iGlobalQp, iMinQp, pWelsSvcRc->iMaxQp);
1305 pWelsSvcRc->iAverageFrameQp = pWelsSvcRc->iMaxFrameQp = pWelsSvcRc->iMinFrameQp = pEncCtx->iGlobalQp;
1306 }
WelRcPictureInitScc(sWelsEncCtx * pEncCtx,long long uiTimeStamp)1307 void WelRcPictureInitScc (sWelsEncCtx* pEncCtx, long long uiTimeStamp) {
1308 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1309 SVAAFrameInfoExt* pVaa = static_cast<SVAAFrameInfoExt*> (pEncCtx->pVaa);
1310 SSpatialLayerConfig* pDLayerConfig = &pEncCtx->pSvcParam->sSpatialLayers[pEncCtx->uiDependencyId];
1311 SSpatialLayerInternal* pDLayerParamInternal = &pEncCtx->pSvcParam->sDependencyLayers[pEncCtx->uiDependencyId];
1312 int64_t iFrameCplx = pVaa->sComplexityScreenParam.iFrameComplexity;
1313 int32_t iBitRate = pDLayerConfig->iSpatialBitrate;// pEncCtx->pSvcParam->target_bitrate;
1314
1315 int32_t iBaseQp = pWelsSvcRc->iBaseQp;
1316 pEncCtx->iGlobalQp = iBaseQp;
1317 int32_t iDeltaQp = 0;
1318 if (pEncCtx->eSliceType == I_SLICE) {
1319 int64_t iTargetBits = iBitRate * 2 - pWelsSvcRc->iBufferFullnessSkip;
1320 iTargetBits = WELS_MAX (1, iTargetBits);
1321 int32_t iQstep = WELS_DIV_ROUND (iFrameCplx * pWelsSvcRc->iCost2BitsIntra, iTargetBits);
1322 int32_t iQp = RcConvertQStep2Qp (iQstep);
1323
1324 pEncCtx->iGlobalQp = WELS_CLIP3 (iQp, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
1325 } else {
1326 int64_t iTargetBits = WELS_ROUND (((float)iBitRate / pDLayerParamInternal->fOutputFrameRate)); //iBitRate / 10;
1327 int32_t iQstep = WELS_DIV_ROUND (iFrameCplx * pWelsSvcRc->iAvgCost2Bits, iTargetBits);
1328 int32_t iQp = RcConvertQStep2Qp (iQstep);
1329 iDeltaQp = iQp - iBaseQp;
1330 if (pWelsSvcRc->iBufferFullnessSkip > iBitRate) {
1331 if (iDeltaQp > 0) {
1332 ++iBaseQp;
1333 }
1334 } else if (pWelsSvcRc->iBufferFullnessSkip == 0) {
1335 if (iDeltaQp < 0) {
1336 --iBaseQp;
1337 }
1338 }
1339 if (iDeltaQp >= 6) {
1340 iBaseQp += 3;
1341 } else if ((iDeltaQp <= -6)) {
1342 --iBaseQp;
1343 }
1344 iBaseQp = WELS_CLIP3 (iBaseQp, pWelsSvcRc->iMinQp, pWelsSvcRc->iMinQp);
1345
1346 pEncCtx->iGlobalQp = iBaseQp;
1347
1348
1349 if (iDeltaQp < -6) {
1350 pEncCtx->iGlobalQp = WELS_CLIP3 (pWelsSvcRc->iBaseQp - 6, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
1351 }
1352
1353 if (iDeltaQp > 5) {
1354 if (LARGE_CHANGED_SCENE == pEncCtx->pVaa->eSceneChangeIdc || pWelsSvcRc->iBufferFullnessSkip > 2 * iBitRate
1355 || iDeltaQp > 10) {
1356 pEncCtx->iGlobalQp = WELS_CLIP3 (pWelsSvcRc->iBaseQp + iDeltaQp, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
1357 } else if (MEDIUM_CHANGED_SCENE == pEncCtx->pVaa->eSceneChangeIdc || pWelsSvcRc->iBufferFullnessSkip > iBitRate) {
1358 pEncCtx->iGlobalQp = WELS_CLIP3 (pWelsSvcRc->iBaseQp + 5, pWelsSvcRc->iMinQp, pWelsSvcRc->iMaxQp);
1359 }
1360 }
1361 pWelsSvcRc->iBaseQp = iBaseQp;
1362 }
1363 pWelsSvcRc->iAverageFrameQp = pEncCtx->iGlobalQp;
1364 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "WelRcPictureInitScc iLumaQp = %d\n", pEncCtx->iGlobalQp);
1365 pWelsSvcRc->uiLastTimeStamp = uiTimeStamp;
1366
1367 }
WelsRcDropFrameUpdate(sWelsEncCtx * pEncCtx,uint32_t iDropSize)1368 void WelsRcDropFrameUpdate (sWelsEncCtx* pEncCtx, uint32_t iDropSize) {
1369 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[0];
1370
1371 pWelsSvcRc->iBufferFullnessSkip -= (int32_t)iDropSize;
1372 pWelsSvcRc->iBufferFullnessSkip = WELS_MAX (0, pWelsSvcRc->iBufferFullnessSkip);
1373 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG, "[WelsRcDropFrameUpdate:\tdrop:%d\t%" PRId64 "\n", iDropSize,
1374 pWelsSvcRc->iBufferFullnessSkip);
1375 }
1376
WelsRcPictureInfoUpdateScc(sWelsEncCtx * pEncCtx,int32_t iNalSize)1377 void WelsRcPictureInfoUpdateScc (sWelsEncCtx* pEncCtx, int32_t iNalSize) {
1378 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1379 int32_t iFrameBits = (iNalSize << 3);
1380 pWelsSvcRc->iBufferFullnessSkip += iFrameBits;
1381
1382 SVAAFrameInfoExt* pVaa = static_cast<SVAAFrameInfoExt*> (pEncCtx->pVaa);
1383
1384 int32_t iQstep = RcConvertQp2QStep (pEncCtx->iGlobalQp);
1385 int64_t iCost2Bits = WELS_DIV_ROUND64 ((((int64_t)iFrameBits * iQstep)), pVaa->sComplexityScreenParam.iFrameComplexity);
1386
1387 if (pEncCtx->eSliceType == P_SLICE) {
1388 pWelsSvcRc->iAvgCost2Bits = WELS_DIV_ROUND64 ((95 * pWelsSvcRc->iAvgCost2Bits + 5 * iCost2Bits), INT_MULTIPLY);
1389 } else {
1390 pWelsSvcRc->iCost2BitsIntra = WELS_DIV_ROUND64 ((90 * pWelsSvcRc->iCost2BitsIntra + 10 * iCost2Bits), INT_MULTIPLY);
1391 }
1392 }
1393
1394
WelsRcMbInitScc(sWelsEncCtx * pEncCtx,SMB * pCurMb,SSlice * pSlice)1395 void WelsRcMbInitScc (sWelsEncCtx* pEncCtx, SMB* pCurMb, SSlice* pSlice) {
1396 /* Get delta iQp of this MB */
1397 pCurMb->uiLumaQp = pEncCtx->iGlobalQp;
1398 pCurMb->uiChromaQp = g_kuiChromaQpTable[WELS_CLIP3 (pCurMb->uiLumaQp + pEncCtx->pPps->uiChromaQpIndexOffset, 0, 51)];
1399 }
1400
WelsRcFrameDelayJudgeTimeStamp(sWelsEncCtx * pEncCtx,long long uiTimeStamp,int32_t iDidIdx)1401 void WelsRcFrameDelayJudgeTimeStamp (sWelsEncCtx* pEncCtx, long long uiTimeStamp, int32_t iDidIdx) {
1402 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[iDidIdx];
1403 SSpatialLayerConfig* pDLayerConfig = &pEncCtx->pSvcParam->sSpatialLayers[iDidIdx];
1404
1405 int32_t iBitRate = pDLayerConfig->iSpatialBitrate;
1406 int32_t iEncTimeInv = (pWelsSvcRc->uiLastTimeStamp == 0) ? 0 : (int32_t) (uiTimeStamp - pWelsSvcRc->uiLastTimeStamp);
1407 if ((iEncTimeInv < 0) || (iEncTimeInv > 1000)) {
1408 iEncTimeInv = (int32_t) (1000.0 / pDLayerConfig->fFrameRate);
1409 pWelsSvcRc->uiLastTimeStamp = uiTimeStamp - iEncTimeInv;
1410 }
1411 int32_t iSentBits = (int32_t) ((double)iBitRate * iEncTimeInv * (1.0E-3) + 0.5);
1412 iSentBits = WELS_MAX (iSentBits, 0);
1413
1414 //When bitrate is changed, pBuffer size should be updated
1415 pWelsSvcRc->iBufferSizeSkip = WELS_DIV_ROUND (pDLayerConfig->iSpatialBitrate * pWelsSvcRc->iSkipBufferRatio,
1416 INT_MULTIPLY);
1417 pWelsSvcRc->iBufferSizePadding = WELS_DIV_ROUND (pDLayerConfig->iSpatialBitrate * PADDING_BUFFER_RATIO, INT_MULTIPLY);
1418
1419 pWelsSvcRc->iBufferFullnessSkip -= iSentBits;
1420 pWelsSvcRc->iBufferFullnessSkip = WELS_MAX ((-1) * (pDLayerConfig->iSpatialBitrate / 4),
1421 pWelsSvcRc->iBufferFullnessSkip);
1422
1423 if (pEncCtx->pSvcParam->bEnableFrameSkip) {
1424 pWelsSvcRc->bSkipFlag = true;
1425 if (pWelsSvcRc->iBufferFullnessSkip < pWelsSvcRc->iBufferSizeSkip) {
1426 pWelsSvcRc->bSkipFlag = false;
1427 }
1428 if (pWelsSvcRc->bSkipFlag) {
1429 pWelsSvcRc->iSkipFrameNum++;
1430 pWelsSvcRc->uiLastTimeStamp = uiTimeStamp;
1431 }
1432 }
1433 WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_DEBUG,
1434 "WelsRcFrameDelayJudgeTimeStamp iDidIdx = %d,iSkipFrameNum = %d,buffer = %" PRId64
1435 ",threadhold = %d,bitrate = %d,iSentBits = %d,lasttimestamp = %lld,timestamp=%lld", iDidIdx,
1436 pWelsSvcRc->iSkipFrameNum, pWelsSvcRc->iBufferFullnessSkip, pWelsSvcRc->iBufferSizeSkip, iBitRate, iSentBits,
1437 pWelsSvcRc->uiLastTimeStamp, uiTimeStamp);
1438 }
1439
WelsRcPictureInfoUpdateGomTimeStamp(sWelsEncCtx * pEncCtx,int32_t iLayerSize)1440 void WelsRcPictureInfoUpdateGomTimeStamp (sWelsEncCtx* pEncCtx, int32_t iLayerSize) {
1441 SWelsSvcRc* pWelsSvcRc = &pEncCtx->pWelsSvcRc[pEncCtx->uiDependencyId];
1442 int32_t iCodedBits = (iLayerSize << 3);
1443
1444 RcUpdatePictureQpBits (pEncCtx, iCodedBits);
1445 if (pEncCtx->eSliceType == P_SLICE) {
1446 RcUpdateFrameComplexity (pEncCtx);
1447 } else {
1448 RcUpdateIntraComplexity (pEncCtx);
1449 }
1450
1451 pWelsSvcRc->iRemainingBits -= pWelsSvcRc->iFrameDqBits;
1452 //condition 1: whole pBuffer fullness
1453 pWelsSvcRc->iBufferFullnessSkip += pWelsSvcRc->iFrameDqBits;
1454
1455 if (pEncCtx->pSvcParam->iPaddingFlag)
1456 RcVBufferCalculationPadding (pEncCtx);
1457 pWelsSvcRc->iFrameCodedInVGop++;
1458 }
1459
WelsRcInitFuncPointers(sWelsEncCtx * pEncCtx,RC_MODES iRcMode)1460 void WelsRcInitFuncPointers (sWelsEncCtx* pEncCtx, RC_MODES iRcMode) {
1461 SWelsRcFunc* pRcf = &pEncCtx->pFuncList->pfRc;
1462 switch (iRcMode) {
1463 case RC_OFF_MODE:
1464 pRcf->pfWelsRcPictureInit = WelsRcPictureInitDisable;
1465 pRcf->pfWelsRcPicDelayJudge = NULL;
1466 pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateDisable;
1467 pRcf->pfWelsRcMbInit = WelsRcMbInitDisable;
1468 pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateDisable;
1469 pRcf->pfWelsCheckSkipBasedMaxbr = NULL;
1470 pRcf->pfWelsUpdateBufferWhenSkip = NULL;
1471 pRcf->pfWelsUpdateMaxBrWindowStatus = NULL;
1472 pRcf->pfWelsRcPostFrameSkipping = NULL;
1473 break;
1474 case RC_BUFFERBASED_MODE:
1475 pRcf->pfWelsRcPictureInit = WelRcPictureInitBufferBasedQp;
1476 pRcf->pfWelsRcPicDelayJudge = NULL;
1477 pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateDisable;
1478 pRcf->pfWelsRcMbInit = WelsRcMbInitDisable;
1479 pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateDisable;
1480 pRcf->pfWelsCheckSkipBasedMaxbr = NULL;
1481 pRcf->pfWelsUpdateBufferWhenSkip = NULL;
1482 pRcf->pfWelsUpdateMaxBrWindowStatus = NULL;
1483 pRcf->pfWelsRcPostFrameSkipping = NULL;
1484 break;
1485 case RC_BITRATE_MODE:
1486 pRcf->pfWelsRcPictureInit = WelsRcPictureInitGom;
1487 pRcf->pfWelsRcPicDelayJudge = NULL;
1488 pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateGom;
1489 pRcf->pfWelsRcMbInit = WelsRcMbInitGom;
1490 pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateGom;
1491 pRcf->pfWelsCheckSkipBasedMaxbr = CheckFrameSkipBasedMaxbr;
1492 pRcf->pfWelsUpdateBufferWhenSkip = UpdateBufferWhenFrameSkipped;
1493 pRcf->pfWelsUpdateMaxBrWindowStatus = UpdateMaxBrCheckWindowStatus;
1494 pRcf->pfWelsRcPostFrameSkipping = WelsRcPostFrameSkipping;
1495 break;
1496 case RC_BITRATE_MODE_POST_SKIP:
1497 pRcf->pfWelsRcPictureInit = WelsRcPictureInitGom;
1498 pRcf->pfWelsRcPicDelayJudge = NULL;
1499 pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateGom;
1500 pRcf->pfWelsRcMbInit = WelsRcMbInitGom;
1501 pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateGom;
1502 pRcf->pfWelsCheckSkipBasedMaxbr = CheckFrameSkipBasedMaxbr;
1503 pRcf->pfWelsUpdateBufferWhenSkip = UpdateBufferWhenFrameSkipped;
1504 pRcf->pfWelsUpdateMaxBrWindowStatus = UpdateMaxBrCheckWindowStatus;
1505 pRcf->pfWelsRcPostFrameSkipping = WelsRcPostFrameSkipping;
1506 break;
1507 case RC_TIMESTAMP_MODE:
1508
1509 pRcf->pfWelsRcPictureInit = WelsRcPictureInitGom;
1510 pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateGomTimeStamp;
1511 pRcf->pfWelsRcMbInit = WelsRcMbInitGom;
1512 pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateGom;
1513
1514 pRcf->pfWelsRcPicDelayJudge = WelsRcFrameDelayJudgeTimeStamp;
1515 pRcf->pfWelsCheckSkipBasedMaxbr = NULL;
1516 pRcf->pfWelsUpdateBufferWhenSkip = NULL;
1517 pRcf->pfWelsUpdateMaxBrWindowStatus = NULL;
1518 pRcf->pfWelsRcPostFrameSkipping = NULL;
1519 break;
1520 case RC_QUALITY_MODE:
1521 default:
1522 pRcf->pfWelsRcPictureInit = WelsRcPictureInitGom;
1523 pRcf->pfWelsRcPicDelayJudge = NULL;
1524 pRcf->pfWelsRcPictureInfoUpdate = WelsRcPictureInfoUpdateGom;
1525 pRcf->pfWelsRcMbInit = WelsRcMbInitGom;
1526 pRcf->pfWelsRcMbInfoUpdate = WelsRcMbInfoUpdateGom;
1527 pRcf->pfWelsCheckSkipBasedMaxbr = CheckFrameSkipBasedMaxbr;
1528 pRcf->pfWelsUpdateBufferWhenSkip = UpdateBufferWhenFrameSkipped;
1529 pRcf->pfWelsUpdateMaxBrWindowStatus = UpdateMaxBrCheckWindowStatus;
1530 pRcf->pfWelsRcPostFrameSkipping = NULL;
1531 break;
1532 }
1533 }
1534
WelsRcInitModule(sWelsEncCtx * pEncCtx,RC_MODES iRcMode)1535 void WelsRcInitModule (sWelsEncCtx* pEncCtx, RC_MODES iRcMode) {
1536 WelsRcInitFuncPointers (pEncCtx, iRcMode);
1537 RcInitSequenceParameter (pEncCtx);
1538 }
1539
WelsRcFreeMemory(sWelsEncCtx * pEncCtx)1540 void WelsRcFreeMemory (sWelsEncCtx* pEncCtx) {
1541 SWelsSvcRc* pWelsSvcRc = NULL;
1542 int32_t i = 0;
1543 for (i = 0; i < pEncCtx->pSvcParam->iSpatialLayerNum; i++) {
1544 pWelsSvcRc = &pEncCtx->pWelsSvcRc[i];
1545 RcFreeLayerMemory (pWelsSvcRc, pEncCtx->pMemAlign);
1546 }
1547 }
1548
GetTimestampForRc(const long long uiTimeStamp,const long long uiLastTimeStamp,const float fFrameRate)1549 long long GetTimestampForRc (const long long uiTimeStamp, const long long uiLastTimeStamp, const float fFrameRate) {
1550 if ((uiLastTimeStamp >= uiTimeStamp) || ((uiTimeStamp == 0) && (uiLastTimeStamp != -1))) {
1551 return (uiLastTimeStamp + (int32_t) (1000.0 / fFrameRate));
1552 }
1553 return uiTimeStamp;
1554 }
1555
1556 }//end of namespace
1557