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