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