xref: /utopia/UTPA2-700.0.x/modules/mfe/drv/mfe_ex/cModel/msRateCtrl.h (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
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 #ifndef _RATECTRL_H_
83 #define _RATECTRL_H_
84 
85 #include "mfe_type.h"
86 
87 /************************************************************************/
88 /* Configuration                                                        */
89 /************************************************************************/
90 #define QS_SHIFT_FACTOR	5UL
91 #define MAX_BUF_FULL	(100L*1024L*1024L)
92 // Define this to support MB-level rate control
93 #define _SUPPORT_MBLEVEL_RC_
94 
95 // Define this to declare RC variables outside RC kernel
96 // NOTE: MFE_MPEG4_CMODEL-- define this.
97 //#define _USE_EXT_RCDATA_
98 
99 // The maximal allowable MB width
100 #define MAX_MB_WIDTH (1920UL>>4)
101 
102 /* ~Configuration *******************************************************/
103 
104 #ifndef MSRC_MAX
105 #define  MSRC_MAX(a,b)              (((a) > (b)) ? (a) : (b))
106 #endif
107 #ifndef MSRC_MIN
108 #define  MSRC_MIN(a,b)              (((a) < (b)) ? (a) : (b))
109 #endif
110 
111 #ifdef _SUPPORT_MBLEVEL_RC_
112 	#define TOP_QP_DIFF_LIMIT          3UL // Must >= SPEC_QP_DIFF_LIMIT
113 	#define LEFT_QP_DIFF_LIMIT         3UL // Must >= SPEC_QP_DIFF_LIMIT
114 #endif
115 
116 typedef enum {
117 	CONST_QUALITY = 1,								//!< Constant qscale: Use specified QP.
118 	CONST_BITRATE = 2,								//!< Constant bitrate.
119 	VARIABLE_BITRATE = 3,							//!< Variable bitrate: instant bitrate is unrestricted.
120 	CONSTRAINED_VARIABLE_BITRATE = 4,	//!< Variable bitrate: instant maximal bitrate is restricted.
121        STRICT_CONST_BITRATE = 5,
122 } RC_METHOD;
123 
124 typedef enum {
125 	FRAMELEVELRC = 1,   //!< All MB's in one frame uses the same qscale.
126 	MBLEVELRC = 2       //!< Allow qscale changing within frame, for better bitrate achievement.
127 } RC_GRANULARITY;
128 
129 
130 /* Input for rate control */
131 typedef struct {
132 	MS_S32 nCodecType;	// 0/1/2/3: MPEG4/H263/H264/JPEG
133 	MS_S32 nWidth, nHeight;
134 	MS_BOOL fTargetFrameRate_is_float;
135 	MS_FLOAT fTargetFrameRate;
136 	MS_S32 int_fTargetFrameRate;
137 	MS_S32 nBitrate;     // Meaningful only when rcMethod!=CONST_QUALITY (bits)
138 										// If rcMethod=CONSTRAINED_VARIABLE_BITRATE, 0 means internally-decided.
139 	MS_S32 nMaxBitrate;  // Meaningful only when rcMethod=CONSTRAINED_VARIABLE_BITRATE (Bits)
140 										// For others, this should be 0.
141 	MS_S32 nConstQP;			// Meaningful only when rcMethod=CONST_QUALITY
142 	//
143 	// For coded frame slicing: GOB (H.263), video packet (MPEG4), or slice (H.264)
144 	MS_S32 nVPSize;	// Video packet size in bits.
145 	MS_S32 nVPMbRow;   // Video packet size in number of MB rows. If H.263, this must be multiple of "MBrow per GOB".
146 	//
147 	MS_BOOL bFixedFrameRate;	// If FALSE, rate control can signal frame-skipping.
148 	MS_S32 nPCount;			// Number of P-frames between I-frames
149 	MS_S32 nBCount;			// Number of B-frames between P-frames // Suggest: 0, 1, 2
150 	RC_GRANULARITY rcGranularity;
151 	RC_METHOD rcMethod;
152 } CVBRRCInfo;
153 
154 typedef struct {
155 	/* Input parameters */
156 
157 	MS_S32 m_nCodecType;
158 	MS_S32 m_nWidth, m_nHeight;
159 	MS_S32 m_nTargetFrameRateNum, m_nTargetFrameRateDeNum, m_nTargetFrameRateInt;	// The target rate is Num/Denum
160 	MS_S32 m_nBitrate;
161 	MS_S32 m_nMaxBitrate; // Only when CONSTRAINED_VARIABLE_BITRATE
162 	MS_S32 m_nConstQP;   // Only when CONST_QUALITY
163 	MS_S32 m_nVPSize, m_nVPMbRow;
164 	MS_BOOL m_bFixedFrameRate;
165 	MS_S32 m_nPCount, m_nBCount;
166 	RC_GRANULARITY m_rcGranularity;
167 	RC_METHOD m_rcMethod;
168 	// Derived variables
169 	MS_S32 m_nAvgBitsPerFrame;
170 	MS_S32 m_nBitsPerFrame[3];	// I, P, B
171 
172 	/* rate control variables */
173 
174 	MS_S32 m_nFrameCount;  // How many frame coded
175 	MS_S32 m_nBufFullness;  // Rate control buffer
176 	MS_S64 m_nTotalBits;
177 	// Last-frame status
178 	MS_BOOL m_bIsBotField;
179 	MS_S32 m_nLastFrameBits, m_nLastFrameAvgQStep[2];
180 	MS_S32 m_nLastTargetBits;
181 
182 	/* Bitrate usage compensation */
183 	MS_S32 m_nTargetFullness;
184 	MS_S32 m_nDeputyCount, m_nMinDeputyCount;
185 
186 	/* Variable bitrate */
187 	MS_S32 m_nLongTermQP64;
188 
189 	// Model parameters
190 
191 	MS_S32 m_nTargetBits;   // target number of bits of current frame
192 	MS_S32 m_nTotalMB;			// number of macroblocks in a frame
193 	MS_S32 m_nMBN;					// Accumulated handled MB count (within one frame)
194 	MS_S32 m_nFrameQStep;
195 	MS_S32 m_nFrameType;
196 
197 	/* Only for CONSTRAINED_VARIABLE_BITRATE */
198 	MS_S32 m_nMaxOffset;
199 	MS_S32* m_BitrateGauge;
200 	MS_S32 m_nGaugeCount, m_nGaugeIndex, m_nGaugeBitrate;
201 	MS_S32 m_nMaxFrozenFrame;
202 
203 	// QP, QStep: Min, Max
204 	MS_S32 m_nMinQP, m_nMaxQP;
205 	MS_S32 m_nMinQStep, m_nMaxQStep;
206 	MS_S32 m_nFrameSkipThrQP;
207 
208 	/* MB-level rate control */
209 #ifdef _SUPPORT_MBLEVEL_RC_
210 	MS_S32 m_nTargetMbBits;
211 	MS_S32 m_nTargetUsedBits;
212 	MS_S32 m_nUsedBits[3];	// For HW pipeline delay
213 	MS_S32 m_nSumAct;
214 	MS_S32 m_nMbWidth, m_nPrevQP, m_nLeftQP, m_nPrevTopQP[MAX_MB_WIDTH];
215 	MS_S32 m_nLastTargetQP;	// For sig_dump
216 	// For coded frame slicing
217 	MS_S32 m_nNewPacket;	// New GOB, video packet, slice... Simulating HW er_en
218 	MS_S32 m_nLastVPBits;
219 #endif
220 
221 	MS_S32* m_pMBBitsArray;	// Only for DEBUG usage.
222 } CVBRRateControl;
223 
224 // Global
225 void cvbr_InitRateControl(CVBRRateControl* ct, CVBRRCInfo* pRCInfo);
226 void cvbr_CloseRateControl(CVBRRateControl* ct);
227 // Each frame
228 MS_S32 cvbr_InitFrame(CVBRRateControl* ct, MS_S32 nFrameType, MS_S8 FieldType);    // Return the initial frame qp
229 MS_S32 cvbr_UpdateFrame(CVBRRateControl* ct, MS_S32 totalUsedBits, MS_S8 bDummyFrame, MS_S8 FieldType);
230 // Each macroblock
231 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);
232 void output_MBR_reg(void* hd, CVBRRateControl* ct);
233 MS_S32 rcQP2Qstep(CVBRRateControl* ct, MS_S32 QP);
234 MS_S32 rcQstep2QP(CVBRRateControl* ct, MS_S32 QstepX32);
235 
236 #endif
237