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 #ifndef _MFE_COMMON_H_ 80 #define _MFE_COMMON_H_ 81 82 #include "mfe_reg.h" 83 #include "IntraUpdate.h" 84 #include "msRateCtrl.h" 85 #include "OutStrm.h" 86 #include "udma_share.h" 87 #include "MFE_chip.h" 88 89 // if not T8 and KERNEL 90 #if (!defined(_KERNEL_MODE_) || !defined(_MFE_T8_)) 91 #include <stdio.h> 92 #include <string.h> 93 #include <stdlib.h> 94 #endif 95 96 97 #if (defined(_MFE_T8_)&&defined(_MIPS_PLATFORM_)&&defined(_KERNEL_MODE_)) 98 #ifndef _MFE_ASSERT_ 99 #define _MFE_ASSERT_ 100 #define MFE_ASSERT(p) do {\ 101 if (!(p)) {\ 102 printk(KERN_CRIT "MFE BUG at %s:%d assert(%s)\n",\ 103 __FILE__, __LINE__, #p);\ 104 BUG();\ 105 }\ 106 } while (0) 107 #endif 108 #include <linux/string.h> 109 #include "mdrv_types.h" 110 #else 111 #include <assert.h> 112 #define MFE_ASSERT(p) assert(p) 113 #endif 114 115 extern MFE_REG mfe_reg; 116 117 #define MIU_MB_SIZE 16 118 119 #define MAX_RESOLUTION_X 960 120 #define MAX_RESOLUTION_Y 576 121 122 // Used in IME and FME 123 #define BLOCK16x16 1 124 #define BLOCK16x8 2 125 #define BLOCK8x16 3 126 #define BLOCK8x8 4 127 #define BLOCK8x4 5 128 #define BLOCK4x8 6 129 #define BLOCK4x4 7 130 131 typedef enum { 132 M4VE_FRM_CUR_ORI = 0, 133 M4VE_FRM_REF0_ORI, 134 M4VE_FRM_REF1_ORI, 135 M4VE_FRM_CUR_REC, 136 M4VE_FRM_REF0_REC, 137 M4VE_FRM_REF1_REC, 138 } M4VE_FRM_NAME; 139 140 typedef enum 141 { 142 FRAME_CODING = 0, 143 FIELD_CODING, 144 ADAPTIVE_CODING, // Not supported! 145 FRAME_MB_PAIR_CODING // Not supported! 146 } CodingType; 147 148 typedef enum 149 { 150 FRAME = 0, 151 TOP_FIELD, 152 BOTTOM_FIELD 153 } PictureStructure; //!< New enum for field processing 154 155 typedef struct _GOPINFO { 156 int nPCount; 157 int nBCount; 158 MFE_U32 nCodingOrder; 159 MFE_U32 nDispOrder; // Display order of current frame 160 } GOPINFO; 161 162 // Mstar: for swcfg1 163 typedef struct BufInfo 164 { 165 MEMMAP_t m_nCurYAddr; 166 MEMMAP_t m_nCurCAddr; 167 MEMMAP_t m_nRefYAddr[2]; 168 MEMMAP_t m_nRefCAddr[2]; 169 MEMMAP_t m_nRecYAddr; 170 MEMMAP_t m_nRecCAddr; 171 int m_nOutBuffer; 172 int m_OutBufferSize; 173 MEMMAP_t m_BitsOutBuffer[MAX_OUTBUFFER]; 174 MEMMAP_t m_nOutBufAddr[MAX_OUTBUFFER]; 175 // Below is for video 176 MEMMAP_t m_nGNAddr; 177 // Below is only for MPEG-4 178 MEMMAP_t m_nMvStoreAddr; 179 MFE_U8 m_bEnableMvStore; 180 // Below is only for JPEG 181 int m_nRowBufCount; 182 } BufInfo; 183 184 typedef struct _JpgInfo 185 { 186 int nFrameMode; 187 int nUseYVYU; 188 int nWidth, nHeight; 189 int nLastZZ; 190 int QTable[2][64]; 191 int nQFactor; 192 } JpgInfo; 193 194 typedef struct _M4VEINFO 195 { 196 MFE_U8 bShortHeader; // 0: MPEG-4, 1: H263 197 MFE_U8 bInterlacedCoding; // Seq-wide 0: Progressive, 1: Use Field-DCT 198 199 // Frame type 200 MFE_U8 bInterlace; // Frame-wide 0: Progressive, 1: Use Field-DCT 201 MFE_U8 bAllowSkippedPMBs; // If B-frame, this must be 0 202 MFE_U8 nAllowDirectBMBs; 203 MFE_U8 bMvStore; 204 MFE_U8 iRoundingControlSwitch, iRoundingControl; 205 MFE_U8 g_rec_en; 206 int g_ref_en; 207 int m_nLastZZ; 208 int intStep; 209 210 // Frame time 211 int m_tFutureRef; 212 int m_tPastRef; 213 int m_t; 214 int m_tModuloBaseDecd; 215 int m_tModuloBaseDisp; 216 // Derived 217 int m_nBitsModuloBase; 218 int m_iVopTimeIncr; 219 220 // Motion 221 int m_nFmePrec; 222 int SEARCH_RANGE_Y; 223 int iSearchRangeForward; 224 MFE_U8 IME_ADAPTIVE_WINDOW; 225 // Derived 226 int iFCode; 227 228 // VideoPacket, GOB parameters 229 int m_nBitsResyncMarker; 230 int nNumBitsVPMBnum; 231 MFE_U8 bHECEnabled; 232 int nHECPeriod; 233 234 // Time parameters 235 int nTimeResolution; 236 int nFixedIncrement; 237 int iClockRate; 238 int nNumBitsTimeIncr; 239 240 // Quant 241 MFE_U8 bQuantizerType; // 0: H263, 1: MPEG-4 (Q-matrices) 242 int rgiIntraQuantizerMatrix[64]; 243 int rgiInterQuantizerMatrix[64]; 244 245 // H263 246 int nGobUnit; 247 int m_iGobFrameId; 248 249 // Frame buffer 250 MEMMAP_t m_FrameBuf[6][2]; 251 } M4VEINFO; 252 253 #define MAX_BUFPOOL_COUNT 12//20 254 typedef struct _FrameBufPool { 255 MEMMAP_t addr[MAX_BUFPOOL_COUNT][2]; // [cur, ref0, ref1, rec][Y, C] 256 char available[MAX_BUFPOOL_COUNT]; 257 } FrameBufPool; 258 259 #define DPB_SIZE 3 260 typedef struct _DPB_ITEM { 261 MEMMAP_t addr[2]; 262 char is_used; 263 } DPB_ITEM; 264 265 typedef struct _H264INFO 266 { 267 int ProfileIDC, LevelIDC; 268 CodingType PicInterlace; 269 270 int nMaxP8x8Count; 271 int log2_max_frame_num_minus4; 272 273 // ME 274 int num_ref_frames; 275 int iSearchRangeForward; 276 int SEARCH_RANGE_Y; 277 MFE_U8 IME_ADAPTIVE_WINDOW; 278 int fme_precision; // 0--fullpel, 1--halfpel, 2--quarterpel 279 int nMaxFmeMode; 280 MFE_U8 no_umv; 281 int InterSearch[2][8]; 282 int num_ref_idx_l0_active_minus1; // PPS 283 284 // DBF 285 MFE_U8 bDeblockCtrlPresent; 286 int nDeblockIDC; 287 int nDeblockAlpha, nDeblockBeta; 288 289 // Frame type 290 int pic_order_cnt_type; 291 PictureStructure structure; 292 MFE_U8 idr_flag; 293 int nal_ref_idc; 294 int frame_num; 295 int number; 296 int num_ref_idx_l0_active; 297 int RefCount; // 0: 1ref, 1: 2ref 298 int h264_mcc_offset[2]; 299 int intStep; 300 301 // Slice 302 MFE_U8 pic_order_present_flag; 303 int delta_pic_order_cnt_bottom; 304 305 // IEAP 306 int ieap_last_mode; 307 int ieap_constraint_intra; 308 int ieap_ccest_en; 309 int ieap_ccest_thr; 310 int ieap_drop_i16; 311 312 // Frame buffer 313 FrameBufPool BufPool; 314 DPB_ITEM dpb[DPB_SIZE]; 315 } H264INFO; 316 317 typedef struct BitsInfo { 318 unsigned char* ptr; 319 int len; // Number of bytes (not including the last not-full byte, if existed) 320 unsigned char bits; // The last not-full byte, 0 if not existed 321 int bit_len; // Number of bits of the last not-full byte, 0 if not-existed. 322 } BitsInfo; 323 324 typedef struct _MFE_CONFIG 325 { 326 // [Input] 327 int nCodecType; 328 int nDispWidth, nDispHeight; // Display resolution 329 int nBufWidth, nBufHeight; // Image buffer resolution (aligned to 16 or 32 byte, depends on nCodecType) 330 MFE_U8 bInterlace; 331 MFE_U8 bQuantType; 332 // GOP 333 int nPbetweenI; 334 int nBbetweenP; 335 int nP_is_infinite; 336 // Rate control 337 int FrameRatex100; 338 int nBitrate; 339 // Error resilience 340 int nVPSizeInBits; 341 int nVPSizeInMbRow; 342 int g_intraupdate_enable; 343 int g_intraupdate_period; 344 // Memory buffer 345 MFE_U8* dram_base; // Start pointer 346 MFE_U32 dram_size; // Maximal size for use 347 348 // Buffer 349 BufInfo ctxBufInfo; 350 MFE_U32 nOBufIndex; 351 // Memory buffer 352 MFE_U8* dram_ptr; // Curent pointer of available memory buffer 353 354 // Frame stat 355 MFE_U32 nCodingOrder; 356 MFE_U8 vopPredType; // I_VOP, P_VOP, B_VOP 357 358 // Headers 359 BitsInfo ctxBitsInfo; 360 OutStream m_OutStream; 361 MFE_U8 m_FdcBuffer[1024]; 362 363 // Format-dependent 364 union { 365 JpgInfo ctxJpgInfo; 366 M4VEINFO ctxM4veInfo; 367 H264INFO ctxH264Info; 368 }; 369 370 // H263/MPEG4/H264 rate control 371 CVBRRCInfo rcInfo; 372 CVBRRateControl ctxRateControl; 373 int m_cvbrFrameSkip; 374 int m_bGenSkipVopHeader; 375 376 // IntraUpdate 377 IntraUpdateContext m_IUContext; 378 HW_MB_MAP m_UIHwMap[MAX_RESOLUTION_X*MAX_RESOLUTION_Y/256]; 379 SW_MB_MAP m_UISwMap[MAX_RESOLUTION_X*MAX_RESOLUTION_Y/256]; 380 381 int test_case_format; 382 int test_case_num; 383 unsigned char CModel_pattern[20*8];//20 frames 384 385 } MFE_CONFIG; 386 387 // Null functions 388 // #define sd_output_rel(arg1, arg2, arg3) 389 // #define sd_output(arg1, arg2, arg3, arg4) 390 // #define sd_output_reg(arg1, arg2, arg3, arg4, arg5, arg6) 391 392 void ReadRegMFE(MFE_U32 u32Address, MFE_U16 *val); 393 void WriteRegMFE(MFE_U32 u32Address, MFE_U16 val, char *str1, int num, char *str3); 394 void WriteRegMFE_Bank1(MFE_U32 u32Address, MFE_U16 val, char *str1, int num, char *str3); 395 void DumpAllReg(void); 396 397 int PutFDC(void* pContext, int mode); 398 void WriteQTable(int* Table0, int* Table1); 399 400 void SetObufAddr(MFE_U16 sadr_low, MFE_U16 sadr_high, MFE_U16 eadr_low, MFE_U16 eadr_high); 401 void ClearBsfFullIRQ(void); 402 403 void ClearIRQ(int number); 404 void Enable_HW(void); 405 void ResetAllRegs(void); 406 407 void TestStopAtMb(void); 408 void TestStop(void); 409 410 void IntraUpdateInit(MFE_CONFIG *pConfig); 411 void IntraUpdateFrame(MFE_CONFIG *pConfig); 412 void IntraUpdateClose(MFE_CONFIG *pConfig); 413 414 // Rate control 415 void MfeDrvRateControlInit(MFE_CONFIG* pConfig); 416 void MfeDrvRateControlUpdate(MFE_CONFIG* pConfig, char nFieldType); 417 418 // H263/MPEG4 419 void mfeM4VE_Init(MFE_CONFIG *pConfig); 420 void mfeM4VE_EncodeFrame(MFE_CONFIG *pConfig, GOPINFO* pGopInfo); 421 void mfeM4VE_UpdateFrame(MFE_CONFIG *pConfig); 422 int codeM4vConfigHeaders(MFE_CONFIG *pConfig); 423 424 // H264 425 void mfeH264_Init(MFE_CONFIG *pConfig); 426 void mfeH264_EncodeFrame(MFE_CONFIG *pConfig, GOPINFO* pGopInfo); 427 void mfeH264_UpdateFrame(MFE_CONFIG *pConfig); 428 int codeH264ConfigHeaders(MFE_CONFIG *pConfig); 429 430 #endif // _MFE_COMMON_H_ 431 432