1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 //////////////////////////////////////////////////////////////////////////
79 // MStar simple rate control
80 //////////////////////////////////////////////////////////////////////////
81
82 #include "MFE_chip.h"
83 #include "mfe_type.h"
84 #include "mfe_common.h"
85 #include "ms_dprintf.h"
86
87 #include "mdrv_mfe_math.h"
88
89 #include "msRateCtrl.h"
90 #if defined(_MFE_T8_)&&defined(_MIPS_PLATFORM_)&&!defined(_KERNEL_MODE_)
91 #include <stdlib.h>
92 #include <stdio.h>
93 #endif
94
95
96
97 // H264
98 // Spec limitation is [1, 51]
99 #define MSRC_MAX_QP_H264 48UL
100 #define MSRC_MIN_QP_H264 12UL
101 #define FRAMESKIP_THR_QP_H264 40UL
102 // MPEG-4, H263
103 // Spec limitation is [1, 31]
104 #define MSRC_MAX_QP_MPEG4 28UL
105 #define MSRC_MIN_QP_MPEG4 1UL
106 #define FRAMESKIP_THR_QP_MPEG4 20UL
107
108 //! How many seconds of frames are responsible for compensation of bitrate usage.
109 #define CBR_DEPUTY_SECOND 1UL
110 #define CVBR_DEPUTY_SECOND 3UL
111 #define VBR_DEPUTY_SECOND 10UL
112
113 #define MIN_DEPUTY_FACTOR 10UL
114
115 //! Default I-frame weighting over inter-frame
116 // #define IFRAME_WEIGHT 2UL
117 // #define PFRAME_WEIGHT 1UL
118 #define IFRAME_WEIGHT 8UL
119 #define PFRAME_WEIGHT 4UL
120 #define BFRAME_WEIGHT 3UL
121
122
123
124 #ifdef _SUPPORT_MBLEVEL_RC_
125 #define MBACT_NORMALIZATION_FACTOR 2UL
126 //#define FLAT_AREA_VAR_THR 5UL
127 #define SPEC_QP_DIFF_LIMIT 2UL // Spec limitation
128 #define USE_INT_PREC
129 // Below are used when USE_INT_PREC
130 #define SCALE_BITS 6UL
131 #define SCALE_ACT 6UL
132 #endif
133
134 //////////////////////////////////////////////////////////////////////////
135 // global variables
136 //////////////////////////////////////////////////////////////////////////
137 #if 0//ndef _USE_EXT_RCDATA_
138 CVBRRateControl g_cvbrContext;
139 CVBRRCInfo g_cvbrInfo;
140 MS_S32 g_cvbrFrameSkip;
141 #endif
142
143 //private:
144 MS_S32 cvbr_ComputeFrameQStep(CVBRRateControl* ct, MS_S32 nFrameType); // Return the target 'QStep'
145 MS_S32 cvbr_ComputeMbQP(CVBRRateControl* ct, MS_S32 mb_idx); // Return the target 'QP'
146
147 MS_S32 rcQP2Qstep(CVBRRateControl* ct, MS_S32 QP);
148 MS_S32 rcQstep2QP(CVBRRateControl* ct, MS_S32 QstepX32);
149
150 /*
151 void output_MBR_reg(void* hd, CVBRRateControl* ct)
152 {
153 if (ct->m_nVPMbRow>0 && ct->m_nVPSize<=0)
154 sd_output(hd, 0, 2, "reg_mfe_g_er_mode");
155 else if (ct->m_nVPMbRow<=0 && ct->m_nVPSize>0)
156 sd_output(hd, 1, 2, "reg_mfe_g_er_mode");
157 else if (ct->m_nVPMbRow>0 && ct->m_nVPSize>0)
158 sd_output(hd, 2, 2, "reg_mfe_g_er_mode");
159 else
160 sd_output(hd, 3, 2, "reg_mfe_g_er_mode");
161 if (ct->m_nVPMbRow==1)
162 sd_output(hd, 0, 2, "reg_mfe_g_er_mby");
163 else if (ct->m_nVPMbRow==2)
164 sd_output(hd, 1, 2, "reg_mfe_g_er_mby");
165 else if (ct->m_nVPMbRow==4)
166 sd_output(hd, 2, 2, "reg_mfe_g_er_mby");
167 else if (ct->m_nVPMbRow==8)
168 sd_output(hd, 3, 2, "reg_mfe_g_er_mby");
169 else
170 sd_output(hd, 0, 2, "reg_mfe_g_er_mby");
171 }
172 */
173
cvbr_InitRateControl(CVBRRateControl * ct,CVBRRCInfo * pRCInfo)174 void cvbr_InitRateControl(CVBRRateControl* ct, CVBRRCInfo* pRCInfo)
175 {
176 MS_S32 i, j;
177
178 ct->m_nFrameCount = 0;
179
180 MS_ASSERT(pRCInfo->nWidth<=MAX_MB_WIDTH*16);
181 if (pRCInfo->rcMethod==CONST_QUALITY)
182 MS_ASSERT(pRCInfo->rcGranularity==FRAMELEVELRC);
183 // Copy parameters, no error checking yet.
184 ct->m_nCodecType = pRCInfo->nCodecType;
185 ct->m_nWidth = pRCInfo->nWidth;
186 ct->m_nHeight = pRCInfo->nHeight;
187 /*
188 if (pRCInfo->fTargetFrameRate - (MS_S32)pRCInfo->fTargetFrameRate != 0) {
189 switch ((MS_S32)pRCInfo->fTargetFrameRate) {
190 case 14: // 14.985
191 ct->m_nTargetFrameRateNum = 15000;
192 ct->m_nTargetFrameRateDeNum = 1001;
193 break;
194 case 23: // 23.976
195 ct->m_nTargetFrameRateNum = 24000;
196 ct->m_nTargetFrameRateDeNum = 1001;
197 break;
198 case 29: // 29.97
199 ct->m_nTargetFrameRateNum = 30000;
200 ct->m_nTargetFrameRateDeNum = 1001;
201 break;
202 default:
203 MS_ASSERT(0);
204 }
205 }
206 */
207 //2010/01/21 because LG GP3 no support MS_FLOAT operation, so only support 29.97 now.
208 //if fps is 29.97, fTargetFrameRate_is_float =1
209 if (pRCInfo->fTargetFrameRate_is_float==1) {
210 ct->m_nTargetFrameRateNum = 30000;
211 ct->m_nTargetFrameRateDeNum = 1001;
212 }
213 else {
214 ct->m_nTargetFrameRateNum = pRCInfo->int_fTargetFrameRate;
215 ct->m_nTargetFrameRateDeNum = 1;
216 }
217
218 ct->m_nTargetFrameRateInt = ct->m_nTargetFrameRateNum / ct->m_nTargetFrameRateDeNum;
219 ct->m_nBitrate = pRCInfo->nBitrate;
220 ct->m_nMaxBitrate = pRCInfo->nMaxBitrate;
221 ct->m_nConstQP = pRCInfo->nConstQP;
222 ct->m_nVPSize = pRCInfo->nVPSize;
223 ct->m_nVPMbRow = pRCInfo->nVPMbRow;
224 ct->m_bFixedFrameRate = pRCInfo->bFixedFrameRate;
225 ct->m_nPCount = pRCInfo->nPCount;
226 ct->m_nBCount = pRCInfo->nBCount;
227 ct->m_rcGranularity = pRCInfo->rcGranularity;
228 ct->m_rcMethod = pRCInfo->rcMethod;
229 // More bitrate checking
230 if (ct->m_rcMethod==CONSTRAINED_VARIABLE_BITRATE) {
231 if (ct->m_nBitrate==0)
232 ct->m_nBitrate = (MS_S32)(ct->m_nMaxBitrate *6/10);
233 if (ct->m_nMaxBitrate==0)
234 ct->m_nMaxBitrate = (MS_S32)(ct->m_nBitrate * 14/10);
235 ct->m_nMaxOffset = (MS_S32)MFE_DIV_S64((MS_S64)(ct->m_nMaxBitrate - ct->m_nBitrate)*ct->m_nTargetFrameRateDeNum, ct->m_nTargetFrameRateNum) ;
236 // ct->m_nMaxOffset = (MS_S32)((MS_S64)(ct->m_nMaxBitrate - ct->m_nBitrate)*ct->m_nTargetFrameRateDeNum / ct->m_nTargetFrameRateNum);
237
238 if (ct->m_rcGranularity==FRAMELEVELRC)
239 ct->m_nMaxOffset = (ct->m_nMaxOffset) >> 2;
240 }
241 else {
242 ct->m_nMaxBitrate = 0; // Don't care
243 }
244
245 // Derived
246 ct->m_nAvgBitsPerFrame = (MS_S32)MFE_DIV_S64((MS_S64)ct->m_nBitrate*ct->m_nTargetFrameRateDeNum, ct->m_nTargetFrameRateNum) ;
247 //ct->m_nAvgBitsPerFrame = (MS_S32)((MS_S64)ct->m_nBitrate *ct->m_nTargetFrameRateDeNum / ct->m_nTargetFrameRateNum);
248
249 if (ct->m_nPCount == -1) {
250 // infinite P, no B
251 // lim((1 + m_nPCount) / (IFRAME_WEIGHT + PFRAME_WEIGHT * m_nPCount)) = 1 / 4
252 i = 1;
253 j = 4;
254 }
255 else {
256 i = 1+ct->m_nPCount*(1+ct->m_nBCount);
257 j = IFRAME_WEIGHT + PFRAME_WEIGHT*ct->m_nPCount + BFRAME_WEIGHT*(ct->m_nPCount*ct->m_nBCount);
258 }
259
260 ct->m_nBitsPerFrame[0] = (MS_S32)MFE_DIV_S64((MS_S64)ct->m_nBitrate*ct->m_nTargetFrameRateDeNum*IFRAME_WEIGHT*i, (MS_S64)j*ct->m_nTargetFrameRateNum);
261 ct->m_nBitsPerFrame[1] = (MS_S32)MFE_DIV_S64((MS_S64)ct->m_nBitrate*ct->m_nTargetFrameRateDeNum*PFRAME_WEIGHT*i, (MS_S64)j*ct->m_nTargetFrameRateNum);
262 ct->m_nBitsPerFrame[2] = (MS_S32)MFE_DIV_S64((MS_S64)ct->m_nBitrate*ct->m_nTargetFrameRateDeNum*BFRAME_WEIGHT*i, (MS_S64)j*ct->m_nTargetFrameRateNum);
263 #ifdef _SUPPORT_MBLEVEL_RC_
264 ct->m_nMbWidth = (ct->m_nWidth+15)>>4;
265 #endif
266
267 // QP, QStep: min, max
268 if (ct->m_nCodecType==REG_ENC_MODE_MPG4 || ct->m_nCodecType==REG_ENC_MODE_H263) {
269 ct->m_nMinQP = MSRC_MIN_QP_MPEG4;
270 ct->m_nMaxQP = MSRC_MAX_QP_MPEG4;
271 ct->m_nMinQStep = rcQP2Qstep(ct, MSRC_MIN_QP_MPEG4)-1;
272 ct->m_nMaxQStep = rcQP2Qstep(ct, MSRC_MAX_QP_MPEG4);
273 ct->m_nFrameSkipThrQP = FRAMESKIP_THR_QP_MPEG4;
274 }
275 else {
276 ct->m_nMinQP = MSRC_MIN_QP_H264;
277 ct->m_nMaxQP = MSRC_MAX_QP_H264;
278 ct->m_nMinQStep = rcQP2Qstep(ct, MSRC_MIN_QP_H264)-1;
279 ct->m_nMaxQStep = rcQP2Qstep(ct, MSRC_MAX_QP_H264);
280 ct->m_nFrameSkipThrQP = FRAMESKIP_THR_QP_H264;
281 MS_ASSERT(ct->m_nCodecType==REG_ENC_MODE_H264);
282 }
283
284 // Bitrate usage monitoring
285 ct->m_nMinDeputyCount = (ct->m_nTargetFrameRateNum*CBR_DEPUTY_SECOND)/ct->m_nTargetFrameRateDeNum;
286 ct->m_nTargetFullness = ct->m_nBitrate >> 1;
287 ct->m_nBufFullness = ct->m_nTargetFullness;
288 switch (ct->m_rcMethod) {
289 case VARIABLE_BITRATE:
290 ct->m_nDeputyCount = (ct->m_nTargetFrameRateNum*VBR_DEPUTY_SECOND)/ct->m_nTargetFrameRateDeNum;
291 break;
292 case CONSTRAINED_VARIABLE_BITRATE:
293 ct->m_BitrateGauge = MfeDrvMemMalloc(ct->m_nTargetFrameRateInt * sizeof(MS_S32), (const MS_S8*)("m_BitrateGauge"));
294 if (ct->m_BitrateGauge) {
295 memset(ct->m_BitrateGauge, 0, ct->m_nTargetFrameRateInt * sizeof(MS_S32));
296
297 ct->m_BitrateGauge[0] = ct->m_nBitsPerFrame[0];
298 for (i = 1; i < ct->m_nTargetFrameRateInt; i++) {
299 ct->m_BitrateGauge[i] = ct->m_nBitsPerFrame[1];
300 for (j = 0; j < ct->m_nBCount; j++, i++) {
301 if (i == ct->m_nTargetFrameRateInt)
302 break;
303 ct->m_BitrateGauge[i] = ct->m_nBitsPerFrame[2];
304 }
305 }
306 }
307 ct->m_nDeputyCount = (ct->m_nTargetFrameRateNum*CVBR_DEPUTY_SECOND)/ct->m_nTargetFrameRateDeNum;
308 ct->m_nGaugeCount = ct->m_nTargetFrameRateInt;
309 ct->m_nGaugeIndex = 0;
310 ct->m_nGaugeBitrate = (MS_S32)MFE_DIV_S64((MS_S64)ct->m_nBitrate*ct->m_nTargetFrameRateDeNum*ct->m_nGaugeCount, ct->m_nTargetFrameRateNum) ;
311 //ct->m_nGaugeBitrate = (MS_S32)((MS_S64)ct->m_nBitrate*ct->m_nTargetFrameRateDeNum*ct->m_nGaugeCount) / ct->m_nTargetFrameRateNum;
312 break;
313 case CONST_BITRATE:
314 default:
315 ct->m_nDeputyCount = (MS_S32)MFE_DIV_S64((MS_S64)ct->m_nTargetFrameRateNum*CBR_DEPUTY_SECOND,ct->m_nTargetFrameRateDeNum) ;
316 //ct->m_nDeputyCount = (MS_S32)((MS_S64)ct->m_nTargetFrameRateNum*CBR_DEPUTY_SECOND)/ct->m_nTargetFrameRateDeNum;
317 break;
318 }
319
320 ct->m_nFrameCount = 0;
321 ct->m_nTotalBits = 0;
322 ct->m_nLastFrameAvgQStep[0] = ct->m_nLastFrameAvgQStep[1] = ct->m_nLastFrameBits = 0;
323 ct->m_nLongTermQP64 = 0;
324
325 ct->m_nTotalMB = (ct->m_nWidth>>4)*(ct->m_nHeight>>4);
326 #if 0//ndef _USE_EXT_RCDATA_
327 g_cvbrFrameSkip = 0;
328 #endif
329
330
331 // ms_dprintk(DRV_L3,"[CVBRRC] InitRateControl: w=%d, h=%d, FPS=%2.3f, FixedFPS=%d, Bitrate=%d\n",
332 // ct->m_nWidth, ct->m_nHeight, (MS_FLOAT)ct->m_nTargetFrameRateNum/ct->m_nTargetFrameRateDeNum,
333 // ct->m_bFixedFrameRate, ct->m_nBitrate);
334
335
336
337
338 }
339
340
cvbr_CloseRateControl(CVBRRateControl * ct)341 void cvbr_CloseRateControl(CVBRRateControl* ct)
342 {
343 if (ct->m_rcMethod == CONSTRAINED_VARIABLE_BITRATE) {
344 if (ct->m_BitrateGauge) {
345 MfeDrvMemFree((void**)&ct->m_BitrateGauge, (const MS_S8*)("m_BitrateGauge"));
346 ct->m_BitrateGauge = NULL;
347 }
348 }
349 }
350
351 // FieldType: 0--Progressive, 1--First field, 2--second field.
cvbr_InitFrame(CVBRRateControl * ct,MS_S32 nFrameType,MS_S8 FieldType)352 MS_S32 cvbr_InitFrame(CVBRRateControl* ct, MS_S32 nFrameType, MS_S8 FieldType)
353 {
354 MS_S32 nDeputyCount = 0;
355 MS_S32 delta = 0, nRtnQP;
356
357 if (FieldType==0||FieldType==1)
358 ct->m_bIsBotField = 0;
359 else
360 ct->m_bIsBotField = 1; // FieldType is 2
361
362 ct->m_nFrameType = nFrameType;
363 ct->m_nLastVPBits = 0;
364 ct->m_nNewPacket = 0;
365
366 // Target frame bitcount
367 if (FieldType!=2)
368 {
369 if (ct->m_nFrameCount>0) {
370 // 1. Determine the number of future frame to compensate for current bitrate mismatch.
371 if (ct->m_nFrameCount>ct->m_nDeputyCount*MIN_DEPUTY_FACTOR)
372 nDeputyCount = ct->m_nDeputyCount;
373 else if (ct->m_nFrameCount<ct->m_nMinDeputyCount)
374 nDeputyCount = ct->m_nMinDeputyCount;
375 else
376 nDeputyCount = ct->m_nMinDeputyCount +
377 (ct->m_nFrameCount-ct->m_nMinDeputyCount)*(ct->m_nDeputyCount-ct->m_nMinDeputyCount)/(ct->m_nDeputyCount*MIN_DEPUTY_FACTOR-ct->m_nMinDeputyCount);
378 // 2. Calculate the bitcount that this frame should be compensate for.
379 if (ct->m_rcMethod==CONST_BITRATE) {
380 delta = (MS_S32)MFE_DIV_S64((MS_S64)(ct->m_nBufFullness-ct->m_nTargetFullness)*ct->m_nTargetFrameRateDeNum, ct->m_nTargetFrameRateNum) ;
381 //delta = (MS_S32)((MS_S64)(ct->m_nBufFullness-ct->m_nTargetFullness)*ct->m_nTargetFrameRateDeNum)/ct->m_nTargetFrameRateNum;
382
383
384 // delta = (ct->m_nBufFullness>ct->m_nTargetFullness) ?
385 // (MS_S32)((ct->m_nBufFullness-ct->m_nTargetFullness)/ct->m_fTargetFrameRate) : ct->m_nBufFullness-ct->m_nTargetFullness;
386 }
387 else if (ct->m_rcMethod==CONSTRAINED_VARIABLE_BITRATE) {
388 delta = (ct->m_nBufFullness-ct->m_nTargetFullness) / nDeputyCount;
389 if (delta<-ct->m_nMaxOffset)
390 delta = -ct->m_nMaxOffset;
391 }
392 else if (ct->m_rcMethod==VARIABLE_BITRATE) {
393 delta = (ct->m_nBufFullness-ct->m_nTargetFullness) / nDeputyCount;
394 if (delta>0 && ((ct->m_nLastFrameAvgQStep[0]<<6)>ct->m_nLongTermQP64))
395 delta = delta>>1; // Make it more variable bitrate to allow better quality
396 }
397 // 3. Finally, calculate the target bitcount.
398 if (ct->m_nPCount==0) {
399 ct->m_nTargetBits = ct->m_nBitsPerFrame[0] - delta;
400 }
401 else {
402 switch (nFrameType) {
403 case I_VOP:
404 ct->m_nTargetBits = ct->m_nBitsPerFrame[0] - delta;
405 break;
406 case P_VOP:
407 ct->m_nTargetBits = ct->m_nBitsPerFrame[1] - delta;
408 break;
409 case B_VOP:
410 ct->m_nTargetBits = ct->m_nBitsPerFrame[2] - delta;
411 break;
412 }
413 }
414 if (ct->m_nTargetBits<=(ct->m_nAvgBitsPerFrame>>3))
415 ct->m_nTargetBits = (ct->m_nAvgBitsPerFrame>>3); // Target bitcount must>0 for ComputeFrameQP()
416 }
417 else {
418 ct->m_nTargetBits = ct->m_nBitsPerFrame[0]; // Must be I-frame
419 }
420
421 /* Return initial frame QP */
422
423 ct->m_nFrameQStep = cvbr_ComputeFrameQStep(ct, nFrameType);
424
425 ct->m_nLastFrameAvgQStep[0] = ct->m_nLastFrameAvgQStep[1] = 0;
426 ct->m_nLastFrameBits = 0;
427
428 #ifdef _SUPPORT_MBLEVEL_RC_
429 ct->m_nTargetMbBits = ct->m_nTargetBits / ct->m_nTotalMB;
430 #endif
431 }
432 else { // bottom field
433 MS_S32 nTargetBits;
434 ct->m_nFrameQStep = ct->m_nLastFrameAvgQStep[0] / ((ct->m_nTotalMB>>1)-1);
435 #ifdef _SUPPORT_MBLEVEL_RC_
436 nTargetBits = ct->m_nTargetBits - ct->m_nLastFrameBits;
437 if (nTargetBits<ct->m_nTargetBits>>3)
438 nTargetBits = ct->m_nTargetBits>>3;
439 ct->m_nTargetMbBits = nTargetBits / (ct->m_nTotalMB>>1);
440 #endif
441 }
442
443 #ifdef _SUPPORT_MBLEVEL_RC_
444 ct->m_nMBN = 0;
445 if (ct->m_nTargetMbBits<1)
446 ct->m_nTargetMbBits = 1;
447 #endif
448
449 nRtnQP = rcQstep2QP(ct, ct->m_nFrameQStep);
450 //ct->m_nFrameQStep = rcQP2Qstep(ct, nRtnQP);
451
452 // ms_dprintk(DRV_L3,"[CVBRRC] %5d%s[%s] InitFrame: TargetBits %7d InitQP %2d Buffer %7d Deputy=%d\n",
453 // ct->m_nFrameCount, FieldType==0?"F":(FieldType==1?"T":"B"), nFrameType==I_VOP?"I":(nFrameType==P_VOP?"P":"B"),
454 // (MS_S32)(ct->m_nTargetBits), nRtnQP, ct->m_nBufFullness, nDeputyCount);
455
456 return nRtnQP;
457 }
458
cvbr_UpdateFrame(CVBRRateControl * ct,MS_S32 totalUsedBits,MS_S8 bDummyFrame,MS_S8 FieldType)459 MS_S32 cvbr_UpdateFrame(CVBRRateControl* ct, MS_S32 totalUsedBits, MS_S8 bDummyFrame, MS_S8 FieldType)
460 {
461 MS_S32 frameskip = 0;
462
463 // Update counter
464 ct->m_nTotalBits += totalUsedBits;
465 ct->m_nLastFrameBits += totalUsedBits;
466 if(ct->m_nBufFullness < MAX_BUF_FULL)
467 ct->m_nBufFullness += totalUsedBits;
468 if (FieldType!=1) {
469 ct->m_nFrameCount++;
470 ct->m_nLastTargetBits = ct->m_nTargetBits;
471
472 if (!bDummyFrame) {
473 ct->m_nLastFrameAvgQStep[0] = (ct->m_nLastFrameAvgQStep[0]+ct->m_nLastFrameAvgQStep[1]) / (FieldType==0 ? ct->m_nTotalMB-1 : ct->m_nTotalMB-2);
474 // Variable bitrate
475 if (ct->m_rcMethod==VARIABLE_BITRATE)
476 ct->m_nLongTermQP64 += ((ct->m_nLastFrameAvgQStep[0]<<6)-ct->m_nLongTermQP64) / ct->m_nFrameCount;
477 else if (ct->m_rcMethod==CONSTRAINED_VARIABLE_BITRATE) {
478 if (ct->m_nFrameCount==1)
479 ct->m_nLongTermQP64 = ct->m_nLastFrameAvgQStep[0]<<6;
480 else
481 ct->m_nLongTermQP64 = (ct->m_nLongTermQP64*(ct->m_nDeputyCount-1) + (ct->m_nLastFrameAvgQStep[0]<<6)) / ct->m_nDeputyCount;
482 }
483 }
484 else {
485 ct->m_nLastFrameAvgQStep[0] = ct->m_nFrameQStep;
486 }
487
488 if (ct->m_rcMethod==CONSTRAINED_VARIABLE_BITRATE) {
489 ct->m_nGaugeBitrate -= ct->m_BitrateGauge[ct->m_nGaugeIndex];
490 ct->m_nGaugeBitrate += totalUsedBits;
491 ct->m_BitrateGauge[ct->m_nGaugeIndex] = totalUsedBits;
492 ct->m_nGaugeIndex++;
493 if (ct->m_nGaugeIndex==ct->m_nGaugeCount)
494 ct->m_nGaugeIndex = 0;
495 }
496
497 // Update buffer status
498 ct->m_nBufFullness -= ct->m_nAvgBitsPerFrame;
499 }
500
501
502 // Check if next skipped frame(s) needed
503 if (FieldType!=1 && !ct->m_bFixedFrameRate && !bDummyFrame)
504 {
505 if (ct->m_rcMethod==CONSTRAINED_VARIABLE_BITRATE || ct->m_rcMethod==VARIABLE_BITRATE) {
506 if ( ct->m_nLongTermQP64>(rcQP2Qstep(ct, ct->m_nFrameSkipThrQP)<<6)
507 && ct->m_nLastFrameBits >= (ct->m_nLastTargetBits<<1) ) {
508 //frameskip = (ct->m_nLastFrameBits / ct->m_nLastTargetBits)-1;
509 frameskip = (MS_S32)((ct->m_nLastFrameBits - ct->m_nLastTargetBits) / ct->m_nAvgBitsPerFrame - 1);
510 if (frameskip<0)
511 frameskip = 0;
512 else if (frameskip>ct->m_nMaxFrozenFrame)
513 frameskip = ct->m_nMaxFrozenFrame;
514 }
515 }
516 else if (ct->m_rcMethod==CONST_BITRATE) {
517 if (ct->m_nLastFrameAvgQStep[0]>rcQP2Qstep(ct, ct->m_nFrameSkipThrQP)) {
518 // Actual fullness is updated after encoding dummy-P frame
519 MS_S32 nBufFullness = ct->m_nBufFullness;
520 while (nBufFullness > ct->m_nTargetFullness) {
521 nBufFullness = (MS_S32)(nBufFullness - ct->m_nAvgBitsPerFrame);
522 frameskip += 1;
523 }
524 }
525 }
526 }
527 /*
528 #ifdef DEBUG
529 if (frameskip > 255)
530 {
531 fprintf (stderr, "Warning: frameskip > 255\n");
532 }
533 if (FieldType!=1){
534 MS_S64 tmp_bitrate ;
535 tmp_bitrate= (MS_S32)MFE_DIV_S64((MS_S64)ct->m_nTotalBits*ct->m_nTargetFrameRateNum, ct->m_nFrameCount*ct->m_nTargetFrameRateDeNum) ;
536
537 // ms_dprintk(DRV_L3,"[CVBRRC] UpdateFrame(%7d bits, %3d%%) AvgQ=%2d LTQ=%2d Bitrate=%8d frameskip=%2d\n",
538 // ct->m_nLastFrameBits, (ct->m_nLastFrameBits-ct->m_nTargetBits)*100/ct->m_nTargetBits, rcQstep2QP(ct, ct->m_nLastFrameAvgQStep[0]),
539 // (MS_S32)(ct->m_nLongTermQP64+32)>>6,(MS_S32)tmp_bitrate, frameskip);
540 }
541
542 // DEBUG_RC(("[CVBRRC] UpdateFrame(%7d bits, %3d%%) AvgQ=%2d LTQ=%2d Bitrate=%8d frameskip=%2d\n",
543 // totalUsedBits, (totalUsedBits-ct->m_nTargetBits)*100/ct->m_nTargetBits, ct->m_nLastFrameAvgQStep[0],
544 // (MS_S32)ct->m_fLongTermQP, ct->m_nGaugeBitrate, frameskip));
545 #endif
546 */
547 return frameskip;
548 }
549
550 #define SMOOTH_PERIOD 1UL
551 #define INIT_QP_FACTOR 720UL
552 #define MIN_INIT_QP 1UL
553 #define MAX_INIT_QP 15UL
554
555 /* Return target QPStep */
cvbr_ComputeFrameQStep(CVBRRateControl * ct,MS_S32 nFrameType)556 MS_S32 cvbr_ComputeFrameQStep(CVBRRateControl* ct, MS_S32 nFrameType)
557 {
558 MS_S32 newQPStep=0;
559 MS_S64 buf_rest;
560 MS_S32 buf_rest_pic;
561 MS_S32 frames_left;
562 MS_S32 nAdjust;
563
564 MS_S32 bitrate = ct->m_nBitrate;
565 MS_S32 targetFPS_DeNum = ct->m_nTargetFrameRateDeNum;
566 MS_S32 targetFPS_Num = ct->m_nTargetFrameRateNum;
567 MS_S32 TotalMB = ct->m_nTotalMB;
568 MS_S32 frame_count = ct->m_nFrameCount;
569
570 // For the very first frame, guess one qp!
571 if (ct->m_nFrameCount==0) {
572 MS_S32 nbpMb, newQP;
573 if (ct->m_rcMethod==CONST_QUALITY) {
574 newQP = ct->m_nConstQP;
575 newQPStep = rcQP2Qstep(ct, newQP); // So that frame qp will be exactly ct->m_nConstQP
576 }
577 else {
578 nbpMb= (MS_S32)MFE_DIV_S64((MS_S64)bitrate*targetFPS_DeNum, TotalMB*targetFPS_Num) +1 ;
579 //nbpMb = (MS_S32)((MS_S64)ct->m_nBitrate*ct->m_nTargetFrameRateDeNum)/(ct->m_nTotalMB*ct->m_nTargetFrameRateNum) + 1;
580 newQP = INIT_QP_FACTOR / nbpMb;
581 if (newQP<MIN_INIT_QP)
582 newQP = MIN_INIT_QP;
583 else if (newQP>MAX_INIT_QP)
584 newQP = MAX_INIT_QP;
585 newQPStep = newQP<<QS_SHIFT_FACTOR;
586 }
587
588 ct->m_nMaxFrozenFrame = newQP>>1;
589 if (ct->m_nMaxFrozenFrame>15)
590 ct->m_nMaxFrozenFrame=15;
591 return newQPStep;
592 }
593
594 if (ct->m_rcMethod==CONST_QUALITY)
595 return rcQP2Qstep(ct, ct->m_nConstQP);
596
597 if (ct->m_rcMethod==CONST_BITRATE) {
598 buf_rest= MFE_DIV_S64((MS_S64)frame_count*targetFPS_DeNum*bitrate, targetFPS_Num) ;
599 //buf_rest = (((MS_S64)ct->m_nFrameCount*ct->m_nTargetFrameRateDeNum*ct->m_nBitrate)/ct->m_nTargetFrameRateNum);
600
601 buf_rest += (SMOOTH_PERIOD*ct->m_nBitrate) - ct->m_nTotalBits;
602
603 newQPStep = ct->m_nLastFrameAvgQStep[0];
604 frames_left = (SMOOTH_PERIOD * ct->m_nTargetFrameRateNum) / ct->m_nTargetFrameRateDeNum;
605 //if (frames_left > 0)
606 {
607 MS_S32 dQP;
608 buf_rest_pic = (MS_S32)MFE_DIV_S64(buf_rest , frames_left);
609 dQP = ct->m_nLastFrameAvgQStep[0]>>3;
610 if (ct->m_nLastFrameBits > (buf_rest_pic*9)>>3) {
611 newQPStep = ct->m_nLastFrameAvgQStep[0]+dQP;
612 }
613 else if (ct->m_nLastFrameBits < (buf_rest_pic*7)>>3) {
614 newQPStep = ct->m_nLastFrameAvgQStep[0]-dQP;
615 }
616 }
617 }
618
619 else if (ct->m_rcMethod==CONSTRAINED_VARIABLE_BITRATE) {
620 MS_S32 nLowBound, nHighBound;
621 nAdjust = ct->m_nLongTermQP64>>2;
622 nLowBound = (ct->m_nLongTermQP64 - nAdjust) >> 6;
623 nHighBound = (ct->m_nLongTermQP64 + nAdjust) >> 6;
624 if (ct->m_nPCount>0 && nFrameType==I_VOP) {
625 newQPStep = ct->m_nLastFrameAvgQStep[0];
626 if (ct->m_nGaugeBitrate<ct->m_nBitrate)
627 newQPStep = newQPStep-(1<<QS_SHIFT_FACTOR);
628 newQPStep = MSRC_MAX((1<<QS_SHIFT_FACTOR), newQPStep);
629 newQPStep = MSRC_MIN((12<<QS_SHIFT_FACTOR), newQPStep);
630 }
631 else {
632 MS_S64 tmp;
633 tmp= MFE_DIV_S64((MS_S64)ct->m_nLongTermQP64 * MFE_DIV_S64(ct->m_nTotalBits,ct->m_nFrameCount), ct->m_nTargetBits) ;
634 //tmp = ct->m_nLongTermQP64 * (ct->m_nTotalBits/ct->m_nFrameCount) /ct->m_nTargetBits);
635 MS_ASSERT((tmp >> 6) < (((MS_S64)1)<<31));
636 newQPStep= (MS_S32)(
637 MFE_DIV_S64((MS_S64)ct->m_nLongTermQP64 * MFE_DIV_S64(ct->m_nTotalBits,ct->m_nFrameCount),
638 ct->m_nTargetBits) >>6) ;
639 //newQPStep = (MS_S32)((ct->m_nLongTermQP64 * (ct->m_nTotalBits/ct->m_nFrameCount) / ct->m_nTargetBits) >> 6);
640 if (ct->m_nLastFrameBits>ct->m_nLastTargetBits) {
641 nAdjust = ((ct->m_nLastFrameBits-ct->m_nLastTargetBits)/ct->m_nMaxOffset) + (1<<QS_SHIFT_FACTOR);
642 if (nAdjust>(3<<QS_SHIFT_FACTOR)) nAdjust=(3<<QS_SHIFT_FACTOR);
643 if ((ct->m_nLastFrameAvgQStep[0]<<6) > ct->m_nLongTermQP64) { // Danger! Make it more aggressive
644 nHighBound = ct->m_nLastFrameAvgQStep[0]+nAdjust;
645 newQPStep = ct->m_nLastFrameAvgQStep[0]+nAdjust;
646 }
647 else {
648 nHighBound += nAdjust;
649 newQPStep += nAdjust;
650 }
651 }
652 else if (ct->m_nGaugeBitrate>ct->m_nBitrate) {
653 if (newQPStep < ct->m_nLastFrameAvgQStep[0])
654 newQPStep = ct->m_nLastFrameAvgQStep[0];
655 if ((newQPStep<<6) < ct->m_nLongTermQP64)
656 newQPStep = ct->m_nLongTermQP64>>6;
657 }
658 else {
659 if ((newQPStep<<6) >= ct->m_nLongTermQP64)
660 newQPStep = (ct->m_nLongTermQP64>>6)-1;
661 if (ct->m_nTargetFullness>ct->m_nBufFullness) {
662 nAdjust = (ct->m_nTargetFullness-ct->m_nBufFullness) / (MS_S32)ct->m_nBitrate;
663 newQPStep -= nAdjust;
664 }
665 }
666 }
667 newQPStep = MSRC_MIN(nHighBound, newQPStep);
668 newQPStep = MSRC_MAX(nLowBound, newQPStep);
669 }
670 else if (ct->m_rcMethod==VARIABLE_BITRATE) {
671 MS_S32 nLowBound, nHighBound;
672 if (ct->m_nPCount>0 && nFrameType==I_VOP) {
673 newQPStep = ct->m_nLastFrameAvgQStep[0];
674 if ((ct->m_nLastFrameAvgQStep[0]<<6) > ct->m_nLongTermQP64)
675 newQPStep = newQPStep-(1<<QS_SHIFT_FACTOR);
676 }
677 else {
678 MS_S32 nAdjLTQ;
679 MS_S64 tmp;
680 if (ct->m_nFrameCount>=ct->m_nTargetFrameRateInt || ct->m_nPCount==0) {
681 tmp= MFE_DIV_S64(
682 (MS_S64)ct->m_nLongTermQP64 * MFE_DIV_S64(ct->m_nTotalBits,ct->m_nFrameCount),
683 ct->m_nAvgBitsPerFrame) ;
684 //tmp = ct->m_nLongTermQP64 * (ct->m_nTotalBits/ct->m_nFrameCount) / ct->m_nAvgBitsPerFrame;
685 MS_ASSERT((tmp >> 6) < (((MS_S64)1)<<31));
686
687 nAdjLTQ= (MS_S32)(MFE_DIV_S64(
688 (MS_S64)ct->m_nLongTermQP64 * MFE_DIV_S64(ct->m_nTotalBits,ct->m_nFrameCount),
689 ct->m_nAvgBitsPerFrame) >>6) ;
690 //nAdjLTQ = (MS_S32)((ct->m_nLongTermQP64 * (ct->m_nTotalBits/ct->m_nFrameCount) / ct->m_nAvgBitsPerFrame) >> 6);
691
692 }
693 else {
694 nAdjLTQ = ct->m_nLongTermQP64>>6; // Wait for stabilization
695 }
696 MS_ASSERT(ct->m_nTargetBits>0);
697 newQPStep = (nAdjLTQ * ct->m_nAvgBitsPerFrame) / ct->m_nTargetBits;
698
699 nAdjust = MSRC_MAX((2<<QS_SHIFT_FACTOR), (MS_S32)(nAdjLTQ)>>2);
700 nLowBound = (MS_S32)(nAdjLTQ) - nAdjust;
701 nHighBound = (MS_S32)(nAdjLTQ) + nAdjust;
702 if (ct->m_nLastFrameBits>ct->m_nLastTargetBits) {
703 nAdjust = (MS_S32)(ct->m_nLastFrameBits/ct->m_nLastTargetBits);
704 if (nAdjust>2) nAdjust=2;
705 nHighBound += nAdjust;
706 }
707
708 if (ct->m_nAvgBitsPerFrame>ct->m_nTargetBits) {
709 newQPStep = MSRC_MIN(nHighBound, newQPStep);
710 }
711 else {
712 newQPStep = MSRC_MAX(nLowBound, newQPStep);
713 }
714 }
715 }
716
717 return newQPStep;
718 }
719
720 // MS_S32 QP2QSTEP32[52] = { // QP2Qstep(QP) * 32
721 // 20,
722 // 22, 26, 28, 32, 36, 40, 44, 52, 56, 64,
723 // 72, 80, 88, 104, 112, 128, 144, 160, 176, 208,
724 // 224, 256, 288, 320, 352, 416, 448, 512, 576, 640,
725 // 704, 832, 896, 1024, 1152, 1280, 1408, 1664, 1792, 2048,
726 // 2304, 2560, 2816, 3328, 3584, 4096, 4608, 5120, 5632, 6656,
727 // 7168,
728 // };
rcQP2Qstep(CVBRRateControl * ct,MS_S32 QP)729 MS_S32 rcQP2Qstep(CVBRRateControl* ct, MS_S32 QP)
730 {
731 if (ct->m_nCodecType==REG_ENC_MODE_H264)
732 {
733 MS_S32 i;
734 MS_S32 Qstep;
735 static const MS_S32 QP2QSTEP[6] = { 20, 22, 26, 28, 32, 36 };
736
737 Qstep = QP2QSTEP[QP % 6];
738 for( i=0; i<(QP/6); i++)
739 Qstep *= 2;
740
741 return Qstep;
742 }
743 else
744 {
745 return QP<<QS_SHIFT_FACTOR;
746 }
747
748 return 0; // Should never entering this.
749 }
750
rcQstep2QP(CVBRRateControl * ct,MS_S32 QstepX32)751 MS_S32 rcQstep2QP(CVBRRateControl* ct, MS_S32 QstepX32)
752 {
753 if (ct->m_nCodecType==REG_ENC_MODE_H264)
754 {
755 MS_S32 q_per = 0, q_rem = 0;
756
757 // MS_ASSERT( QstepX32 >= QP2QSTEP[0] && QstepX32 <= QP2QSTEP[51]);
758 if( QstepX32 <= ct->m_nMinQStep)
759 return ct->m_nMinQP;
760 else if (QstepX32 > ct->m_nMaxQStep)
761 return ct->m_nMaxQP;
762
763 while( QstepX32 > rcQP2Qstep(ct, 5) )
764 {
765 QstepX32 >>= 1;
766 q_per += 1;
767 }
768
769 if (QstepX32 <= 21)
770 {
771 //QstepX32 = 20;
772 q_rem = 0;
773 }
774 else if (QstepX32 <= 24)
775 {
776 //QstepX32 = 22;
777 q_rem = 1;
778 }
779 else if (QstepX32 <= 27)
780 {
781 //QstepX32 = 26;
782 q_rem = 2;
783 }
784 else if (QstepX32 <= 30)
785 {
786 //QstepX32 = 28;
787 q_rem = 3;
788 }
789 else if (QstepX32 <= 34)
790 {
791 //QstepX32 = 32;
792 q_rem = 4;
793 }
794 else
795 {
796 //QstepX32 = 36;
797 q_rem = 5;
798 }
799
800 return (q_per * 6 + q_rem);
801 }
802 else if (ct->m_nCodecType==REG_ENC_MODE_MPG4 || ct->m_nCodecType==REG_ENC_MODE_H263)
803 {
804 if( QstepX32 <= ct->m_nMinQStep)
805 return ct->m_nMinQP;
806 else if (QstepX32 > ct->m_nMaxQStep)
807 return ct->m_nMaxQP;
808
809 return QstepX32>>QS_SHIFT_FACTOR;
810 }
811
812 return 0; // Should never entering this.
813 }
814
815 #ifdef _SUPPORT_MBLEVEL_RC_
816
817 #define DUMP_FRAME_NO -1L //140
818 // Each MB must call this routine once
cvbr_InitMB(CVBRRateControl * ct,MS_S32 nVar,const MS_S32 nPrevQP,const MS_S32 nBits,MS_S32 IsIntra,MS_S32 IsP4MV,MS_S32 BPredType,MS_S32 nResetDQ)819 MS_S32 cvbr_InitMB(CVBRRateControl* ct, MS_S32 nVar, const MS_S32 nPrevQP, const MS_S32 nBits, MS_S32 IsIntra, MS_S32 IsP4MV, MS_S32 BPredType, MS_S32 nResetDQ)
820 {
821 #ifdef USE_INT_PREC
822 MS_S32 nAvgAct, nActj, nNBits=0;
823 #else
824 MS_FLOAT fAvgAct, fActj, fNBits;
825 #endif
826 MS_S32 QStep, RtnQP, nVariance;
827 // MS_S32 TgtQP;
828 MS_BOOL bChangeQP = 0;
829 MS_S32 nMBX, nMBY;
830
831 #ifdef RAND_INPUT
832 nVariance = nVar = rand()%128;
833 //IsIntra = (rand()%4)==0 ? 1 : 0;
834 //Is4MV = (rand()%4)==0 ? 1 : 0;
835 #else
836 nVariance = nVar;
837 #endif
838
839 // if (ct->m_nFrameCount == DUMP_FRAME_NO)
840 // printf("ct->m_nMBN = %d (MBX, MBY) = (%2d, %2d), nBits = %d\n", ct->m_nMBN, ct->m_nMBN%ct->m_nMbWidth, ct->m_nMBN/ct->m_nMbWidth, nBits);
841
842 // if (ct->m_rcGranularity==FRAMELEVELRC) {
843 // return rcQstep2QP(ct->m_nFrameQStep);
844 // }
845
846 if (ct->m_nMBN==0) { // Initialization
847 ct->m_nSumAct = 0;
848 ct->m_nTargetUsedBits = 0;
849 }
850
851 if (ct->m_nMBN>0)
852 { // Previous MB updating: Last MB is not counted!
853 // Update QP
854 ct->m_nPrevTopQP[(ct->m_nMBN-1)%ct->m_nMbWidth] = nPrevQP;
855 ct->m_nLastFrameAvgQStep[ct->m_bIsBotField] += rcQP2Qstep(ct, nPrevQP);
856
857 // SW simulation hw pipeline: Update bit usage history
858 if (ct->m_nMBN<3)
859 ct->m_nUsedBits[ct->m_nMBN-1] = nBits;
860 else {
861 ct->m_nUsedBits[0] = ct->m_nUsedBits[1];
862 ct->m_nUsedBits[1] = nBits;
863 }
864 }
865
866 if (ct->m_nMBN>1) // HW pipeline: See below nNBits (or fNBits) calculation
867 ct->m_nTargetUsedBits += ct->m_nTargetMbBits;
868
869 // Update history
870 if (nVariance==0) nVariance = 1;
871 ct->m_nSumAct += nVariance;
872
873 // QP calculation
874 ct->m_nNewPacket = 0; // Default
875 if (ct->m_nMBN<2) {
876 RtnQP = rcQstep2QP(ct, ct->m_nFrameQStep);
877 }
878 else
879 {
880 if (ct->m_nCodecType==REG_ENC_MODE_MPG4 && (!IsIntra) && ((ct->m_nFrameType==P_VOP&&IsP4MV)||(ct->m_nFrameType==B_VOP&&BPredType==0))) {
881 RtnQP = nPrevQP; // Not allowing DQ!=0
882 }
883 else {
884 #ifdef USE_INT_PREC
885 nNBits = (ct->m_nUsedBits[0]<<SCALE_BITS) / ct->m_nTargetUsedBits;
886 //MS_ASSERT(nNBits<(1<<(SCALE_BITS+6)));
887 if (nNBits>=(1<<(SCALE_BITS+6)))
888 nNBits = (1<<(SCALE_BITS+6)) - 1;
889 nAvgAct = ct->m_nSumAct / (ct->m_nMBN+1);
890 nActj = (((nVariance<<1) + nAvgAct) << SCALE_ACT) / (nVariance + (nAvgAct<<1));
891 // Target QStep
892 if (ct->m_nFrameType==I_VOP)
893 QStep = (MS_S32)(((MS_S64)ct->m_nFrameQStep * nActj) >> SCALE_ACT);
894 else
895 QStep = (MS_S32)(((MS_S64)ct->m_nFrameQStep * nActj * nNBits) >> (SCALE_ACT+SCALE_BITS));
896 #else
897 fNBits = (MS_FLOAT)ct->m_nUsedBits[0] / ct->m_nTargetUsedBits;
898 fAvgAct = (MS_FLOAT)ct->m_nSumAct / (ct->m_nMBN+1);
899 fActj = ((nVariance*MBACT_NORMALIZATION_FACTOR) + fAvgAct) / (nVariance + (fAvgAct*MBACT_NORMALIZATION_FACTOR));
900 // Target QStep
901 if (ct->m_nFrameType==I_VOP)
902 QStep = (MS_S32)(ct->m_nFrameQStep * fActj);
903 else
904 QStep = (MS_S32)(ct->m_nFrameQStep * fActj * fNBits);
905 #endif
906 // if (QStep<(1<<QS_SHIFT_FACTOR))
907 // QStep = (1<<QS_SHIFT_FACTOR);
908 MS_ASSERT(QStep>=0 && QStep<(1<<20));
909
910 // Target QP
911 RtnQP = rcQstep2QP(ct, QStep);
912 // Refinement: Phase 1, top and left MB-QP limitation
913 if (ct->m_nMBN >= ct->m_nMbWidth) {
914 if (RtnQP > ct->m_nPrevTopQP[ct->m_nMBN%ct->m_nMbWidth] + TOP_QP_DIFF_LIMIT) {
915 RtnQP = ct->m_nPrevTopQP[ct->m_nMBN%ct->m_nMbWidth] + SPEC_QP_DIFF_LIMIT;
916 bChangeQP = 1;
917 }
918 else if (RtnQP < ct->m_nPrevTopQP[ct->m_nMBN%ct->m_nMbWidth] - TOP_QP_DIFF_LIMIT) {
919 RtnQP = ct->m_nPrevTopQP[ct->m_nMBN%ct->m_nMbWidth] - SPEC_QP_DIFF_LIMIT;
920 bChangeQP = 1;
921 }
922 }
923 if (RtnQP > nPrevQP + LEFT_QP_DIFF_LIMIT) {
924 RtnQP = nPrevQP + SPEC_QP_DIFF_LIMIT;
925 bChangeQP = 1;
926 }
927 else if (RtnQP < nPrevQP - LEFT_QP_DIFF_LIMIT) {
928 RtnQP = nPrevQP - SPEC_QP_DIFF_LIMIT;
929 bChangeQP = 1;
930 }
931 // Refinement: Phase 2, spec QP range limitation
932 RtnQP = MSRC_MAX(ct->m_nMinQP, RtnQP);
933 RtnQP = MSRC_MIN(ct->m_nMaxQP, RtnQP);
934 #if 0 // Don't need this because QP is bounded by rcQstep2QP(),
935 // and if TOP_QP_DIFF_LIMIT and BOTTOM_QP_DIFF_LIMIT >=2, there is no way to have +-1.
936 // Refinement: Phase 3, MPEG-4 B-frame limitation
937 if (ct->m_nCodecType==REG_ENC_MODE_MPG4 && ct->m_nFrameType==B_VOP /*&& RtnQP!=nPrevQP*/) {
938 if (RtnQP-nPrevQP==-1 || RtnQP-nPrevQP==1)
939 RtnQP = nPrevQP;
940 }
941 #endif
942 // Avoid frequent QP changing
943 if (!bChangeQP)
944 RtnQP = nPrevQP; // Reset
945 }
946
947 //DEBUG_RC((" [CVBRRC] %d : NBits=%d, AvgAct=%d\n", ct->m_nMBN, (MS_S32)fNBits, (MS_S32)fAvgAct ));
948
949 //////////////////////////////////////////////////////////////////////////
950 // er_en
951 if (ct->m_nVPSize) {
952 if (ct->m_nUsedBits[0]-ct->m_nLastVPBits>=ct->m_nVPSize) {
953 if (ct->m_nVPMbRow) {
954 nMBX = ct->m_nMBN%ct->m_nMbWidth;
955 nMBY = ct->m_nMBN/ct->m_nMbWidth;
956 if (nMBX==0 && nMBY>0 && (nMBY%ct->m_nVPMbRow)==0) {
957 ct->m_nNewPacket = 1;
958 ct->m_nLastVPBits = ct->m_nUsedBits[0];
959 }
960 }
961 else {
962 ct->m_nNewPacket = 1;
963 ct->m_nLastVPBits = ct->m_nUsedBits[0];
964 }
965 }
966 }
967 else if (ct->m_nVPMbRow) {
968 nMBX = ct->m_nMBN%ct->m_nMbWidth;
969 nMBY = ct->m_nMBN/ct->m_nMbWidth;
970 if (nMBX==0 && nMBY>0 && (nMBY%ct->m_nVPMbRow)==0) {
971 ct->m_nNewPacket = 1;
972 ct->m_nLastVPBits = ct->m_nUsedBits[0];
973 }
974 }
975 }
976
977 if (ct->m_rcGranularity==FRAMELEVELRC) {
978 RtnQP = rcQstep2QP(ct, ct->m_nFrameQStep);
979 }
980
981 ct->m_nMBN++;
982 if (ct->m_rcMethod==CONST_QUALITY)
983 return ct->m_nConstQP;
984 MS_ASSERT(RtnQP<=ct->m_nMaxQP && RtnQP>=ct->m_nMinQP);
985 return RtnQP;
986 }
987
988 #else // _SUPPORT_MBLEVEL_RC_ not defined
989
cvbr_InitMB(CVBRRateControl * ct,MS_S32 nVar,const MS_S32 nPrevQP,const MS_S32 nBits,MS_S32 IsIntra,MS_S32 IsP4MV,MS_S32 BPredType,MS_S32 nResetDQ)990 MS_S32 cvbr_InitMB(CVBRRateControl* ct, MS_S32 nVar, const MS_S32 nPrevQP, const MS_S32 nBits, MS_S32 IsIntra, MS_S32 IsP4MV, MS_S32 BPredType, MS_S32 nResetDQ)
991 {
992 if (ct->m_nMBN>0)
993 ct->m_nLastFrameAvgQStep[ct->m_bIsBotField] += rcQP2Qstep(nPrevQP);
994 if (ct->m_rcMethod==CONST_QUALITY)
995 return ct->m_nConstQP;
996 return rcQstep2QP(ct->m_nFrameQStep);
997 }
998
999 #endif // _SUPPORT_MBLEVEL_RC_
1000