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 83 #include "MFE_chip.h" 84 #include "mfe_reg.h" 85 #include "IntraUpdate.h" 86 #include "msRateCtrl.h" 87 #include "OutStrm.h" 88 #include "udma_share.h" 89 #include "mfe_h263_rc.h" 90 #include "parsetcommon.h" 91 #include "nalucommon.h" 92 93 // if not T8 and KERNEL 94 #if (!defined(_KERNEL_MODE_) && defined(_MFE_T8_)) 95 #include <stdio.h> 96 #include <string.h> 97 #include <stdlib.h> 98 #endif 99 100 /* 101 #if (defined(_MFE_T8_)&&defined(_MIPS_PLATFORM_)&&defined(_KERNEL_MODE_)) 102 #ifndef _MFE_ASSERT_ 103 #define _MFE_ASSERT_ 104 #define MFE_ASSERT(p) do {\ 105 if (!(p)) {\ 106 printk(KERN_CRIT "MFE BUG at %s:%d assert(%s)\n",\ 107 __FILE__, __LINE__, #p);\ 108 BUG();\ 109 }\ 110 } while (0) 111 #endif 112 #include <linux/string.h> 113 #include "mdrv_types.h" 114 #elif defined(__UBOOT__) 115 #define MFE_ASSERT(p) 116 #elif defined(__MOBILE_CASE__) 117 #include <assert.h> 118 #define MFE_ASSERT(p) assert(p) 119 #elif defined(_MFE_UTOPIA_) 120 #define MFE_ASSERT(p) MS_ASSERT(p) 121 #else 122 #include <assert.h> 123 #define MFE_ASSERT(p) assert(p) 124 #endif 125 */ 126 127 extern MFE_REG* mfe_reg; 128 #if defined(_MFE_MUJI_) || defined(_MFE_MONET_) || defined(_MFE_MESSI_) || defined(_MFE_MANHATTAN_) || defined(_MFE_MASERATI_) || defined(_MFE_MAXIM_) || defined(_MFE_KANO_) || defined(_MFE_K6_) 129 extern MFE_REG1* mfe_reg1; 130 #endif 131 #define MIU_MB_SIZE 16UL 132 133 134 // Used in IME and FME 135 #define BLOCK16x16 1UL 136 #define BLOCK16x8 2UL 137 #define BLOCK8x16 3UL 138 #define BLOCK8x8 4UL 139 #define BLOCK8x4 5UL 140 #define BLOCK4x8 6UL 141 #define BLOCK4x4 7UL 142 143 typedef enum { 144 M4VE_FRM_CUR_ORI = 0, 145 M4VE_FRM_REF0_ORI, 146 M4VE_FRM_REF1_ORI, 147 M4VE_FRM_CUR_REC, 148 M4VE_FRM_REF0_REC, 149 M4VE_FRM_REF1_REC, 150 } M4VE_FRM_NAME; 151 152 typedef enum 153 { 154 FRAME_CODING = 0, 155 FIELD_CODING, 156 ADAPTIVE_CODING, // Not supported! 157 FRAME_MB_PAIR_CODING // Not supported! 158 } CodingType; 159 160 /* Don't change the numbering! It must match cvbr_InitFrame() FieldType definition. */ 161 typedef enum 162 { 163 FRAME = 0, 164 TOP_FIELD = 1, 165 BOTTOM_FIELD = 2 166 } PictureStructure; //!< New enum for field processing 167 168 typedef struct _GOPINFO { 169 MS_S32 nPCount; 170 MS_S32 nBCount; 171 MS_U32 nCodingOrder; 172 MS_U32 nDispOrder; // Display order of current frame 173 } GOPINFO; 174 175 // Mstar: for swcfg1 176 typedef struct BufInfo 177 { 178 MEMMAP_t m_nCurYAddr; 179 MEMMAP_t m_nCurCAddr; 180 MEMMAP_t m_nRefYAddr[2]; 181 MEMMAP_t m_nRefCAddr[2]; 182 MEMMAP_t m_nRecYAddr; 183 MEMMAP_t m_nRecCAddr; 184 MS_S32 m_nOutBuffer; 185 MS_S32 m_OutBufferSize; 186 MEMMAP_t m_BitsOutBuffer[MAX_OUTBUFFER]; 187 MEMMAP_t m_nOutBufAddr[MAX_OUTBUFFER]; 188 // Below is for video 189 MEMMAP_t m_nGNAddr; 190 // Below is only for MPEG-4 191 MEMMAP_t m_nMvStoreAddr; 192 MS_U8 m_bEnableMvStore; 193 // Below is only for JPEG 194 MS_S32 m_nRowBufCount; 195 } BufInfo; 196 197 typedef struct { 198 MS_U32 start_addr; 199 MS_U32 end_addr; 200 MS_S32 used_size; // 0 means not used 201 } OutBitSBUF; 202 203 typedef struct _JpgInfo 204 { 205 MS_S32 nFrameMode; 206 MS_S32 nUseMST422; 207 MS_S32 nUseYVYU; 208 MS_S32 nWidth, nHeight; 209 MS_S32 nLastZZ; 210 MS_S32 QTable[2][64]; 211 MS_S32 nQFactor; 212 MS_S32 fdc_mode; 213 } JpgInfo; 214 215 typedef struct _M4VEINFO 216 { 217 MS_U8 bShortHeader; // 0: MPEG-4, 1: H263 218 MS_U8 bInterlacedCoding; // Seq-wide 0: Progressive, 1: Use Field-DCT 219 220 // Frame type 221 MS_U8 bInterlace; // Frame-wide 0: Progressive, 1: Use Field-DCT 222 MS_U8 bAllowSkippedPMBs; // If B-frame, this must be 0 223 MS_U8 nAllowDirectBMBs; 224 MS_U8 bMvStore; 225 MS_U8 iRoundingControlSwitch, iRoundingControl; 226 MS_U8 g_rec_en; 227 MS_S32 g_ref_en; 228 MS_S32 m_nLastZZ; 229 MS_S32 intQP; 230 231 // Frame time 232 MS_S32 m_tFutureRef; 233 MS_S32 m_tPastRef; 234 MS_S32 m_t; 235 MS_S32 m_tModuloBaseDecd; 236 MS_S32 m_tModuloBaseDisp; 237 // Derived 238 MS_S32 m_nBitsModuloBase; 239 MS_S32 m_iVopTimeIncr; 240 241 // Motion 242 MS_S32 m_nFmePrec; 243 MS_S32 SEARCH_RANGE_Y; 244 MS_S32 iSearchRangeForward; 245 MS_U8 IME_ADAPTIVE_WINDOW; 246 // Derived 247 MS_S32 iFCode; 248 249 // VideoPacket, GOB parameters 250 MS_S32 m_nBitsResyncMarker; 251 MS_S32 nNumBitsVPMBnum; 252 MS_U8 bHECEnabled; 253 MS_S32 nHECPeriod; 254 255 // Time parameters 256 MS_S32 nTimeResolution; 257 MS_S32 nFixedIncrement; 258 MS_S32 iClockRate; 259 MS_S32 nNumBitsTimeIncr; 260 261 // Quant 262 MS_U8 bQuantizerType; // 0: H263, 1: MPEG-4 (Q-matrices) 263 MS_S32 rgiIntraQuantizerMatrix[64]; 264 MS_S32 rgiInterQuantizerMatrix[64]; 265 266 // H263 267 MS_S32 nGobUnit; 268 MS_S32 m_iGobFrameId; 269 270 // Frame buffer 271 MEMMAP_t m_FrameBuf[6][2]; 272 } M4VEINFO; 273 274 #define MAX_BUFPOOL_COUNT 3UL //20 275 276 typedef struct _FrameBufPool { 277 MS_S32 FrameBufPoolSize; 278 MEMMAP_t** addr; // [cur, ref0, ref1, rec][Y, C] 279 MS_S32* available; 280 } FrameBufPool; 281 282 typedef struct _DPB_ITEM { 283 MEMMAP_t addr[2]; 284 MS_S8 is_used; 285 PictureStructure structure; // For H264 PAFF 286 } DPB_ITEM; 287 288 typedef struct _H264INFO 289 { 290 MS_S32 ProfileIDC, LevelIDC; 291 CodingType PicInterlace; 292 293 MS_S32 nMaxP8x8Count; 294 MS_S32 log2_max_frame_num_minus4; 295 296 // ME 297 MS_S32 num_ref_frames; 298 MS_S32 iSearchRangeForward; 299 MS_S32 SEARCH_RANGE_Y; 300 MS_U8 IME_ADAPTIVE_WINDOW; 301 MS_S32 fme_precision; // 0--fullpel, 1--halfpel, 2--quarterpel 302 MS_S32 nMaxFmeMode; 303 MS_U8 no_umv; 304 MS_S32 InterSearch[2][8]; 305 MS_S32 num_ref_idx_l0_active_minus1; // PPS 306 307 // DBF 308 MS_U8 bDeblockCtrlPresent; 309 MS_S32 nDeblockIDC; 310 MS_S32 nDeblockAlpha, nDeblockBeta; 311 312 // Frame type 313 MS_S32 pic_order_cnt_type; 314 PictureStructure structure; 315 MS_U8 idr_flag; 316 MS_S32 nal_ref_idc; 317 MS_S32 frame_num; 318 MS_S32 number; 319 MS_S32 num_ref_idx_l0_active; 320 MS_S32 RefCount; // 0: 1ref, 1: 2ref 321 MS_S32 h264_mcc_offset[2]; 322 MS_S32 intQP; 323 // For PAFF handling 324 MS_U8 vopPredType_FirstField; 325 326 // Slice 327 MS_U8 pic_order_present_flag; 328 MS_S32 delta_pic_order_cnt_bottom; 329 330 // IEAP 331 MS_S32 ieap_last_mode; 332 MS_S32 ieap_constraint_intra; 333 MS_S32 ieap_ccest_en; 334 MS_S32 ieap_ccest_thr; 335 MS_S32 ieap_drop_i16; 336 337 // Frame buffer 338 FrameBufPool BufPool; 339 MS_S32 dpb_size; 340 DPB_ITEM* dpb; 341 } H264INFO; 342 343 typedef struct BitsInfo { 344 MS_U8* ptr; 345 MS_S32 len; // Number of bytes (not including the last not-full byte, if existed) 346 MS_U8 bits; // The last not-full byte, 0 if not existed 347 MS_S32 bit_len; // Number of bits of the last not-full byte, 0 if not-existed. 348 } BitsInfo; 349 350 typedef enum { 351 WAIT_FRAME_DONE = 0, 352 WAIT_INPUT_FRAME = 1, 353 WRITE_MFE_REG = 2, 354 WAIT_AVAIL_BITSBUF = 3, 355 } MFE_STAT; 356 357 typedef struct _MFE_CONFIG 358 { 359 // [Input] 360 MS_S32 nCodecType; 361 MS_S32 nDispWidth, nDispHeight; // Display resolution 362 MS_S32 nBufWidth, nBufHeight; // Image buffer resolution (aligned to 16 or 32 byte, depends on nCodecType) 363 MFE_INTERLACE_INFO bInterlace; 364 MS_U8 bQuantType; 365 // GOP 366 MS_S32 nPbetweenI; 367 MS_S32 nBbetweenP; 368 MS_S32 nP_is_infinite; 369 // Rate control 370 MS_S32 FrameRatex100; 371 MS_S32 nBitrate; 372 // Error resilience 373 MS_S32 nVPSizeInBits; 374 MS_S32 nVPSizeInMbRow; 375 MS_S32 g_intraupdate_enable; 376 MS_S32 g_intraupdate_period; 377 // Memory buffer 378 MS_PHY dram_base; // Start pointer 379 MS_U32 dram_size; // Maximal size for use 380 381 // Buffer 382 BufInfo ctxBufInfo; 383 MS_U32 nOBufIndex; 384 // Memory buffer 385 MS_PHY dram_ptr; // Curent pointer of available memory buffer 386 387 // Frame stat 388 MS_U32 nCodingOrder; 389 MS_U8 vopPredType; // I_VOP, P_VOP, B_VOP 390 391 // Headers 392 BitsInfo ctxBitsInfo; 393 OutStream m_OutStream; 394 MS_U8 m_FdcBuffer[2048]; 395 396 // Format-dependent 397 JpgInfo ctxJpgInfo; 398 M4VEINFO ctxM4veInfo; 399 H264INFO ctxH264Info; 400 401 MS_U32 m_quality; 402 MS_U32 m_bFrameMode; 403 MS_U32 m_bFDC_mode; 404 // H263/MPEG4/H264 rate control 405 CVBRRCInfo rcInfo; 406 CVBRRateControl ctxRateControl; 407 MS_S32 m_cvbrFrameSkip; 408 MS_S32 m_bGenSkipVopHeader; 409 410 // IntraUpdate 411 IntraUpdateContext m_IUContext; 412 //HW_MB_MAP* m_UIHwMap;//[MAX_RESOLUTION_X*MAX_RESOLUTION_Y/256]; 413 //SW_MB_MAP* m_UISwMap://[MAX_RESOLUTION_X*MAX_RESOLUTION_Y/256]; 414 415 MS_S32 test_case_format; 416 MS_S32 test_case_num; 417 MS_U8 CModel_pattern[20*8];//20 frames 418 MS_S32 TimeIncreamentRes; // [IN] frame rate 419 MS_S32 VopTimeIncreament; // [IN] frame rate 420 MFE_CLK_LEVEL MFEClk; 421 MFE_ADV_INFO_t MfeAdvInfo; 422 MFE_CROP_INFO_t MfeCropInfo; 423 MS_U32 imi_addr; 424 MS_U32 imi_size; 425 MS_U32 nDropFlag; 426 MS_U32 VTMode; 427 RateCtrl_t VTRateCtrl; 428 MFE_COLOR_FORMAT_INFO bColorFormat; 429 MS_U32 SecurityMode; 430 MS_U32 PollingMode; 431 MS_U32 g_nUseYUV422; 432 MS_U32 UseCABAC; 433 MS_U32 enableReduceBW; 434 MS_U32 enableFastMode; 435 GOPINFO GopInfo; 436 437 //out buffer 438 OutBitSBUF outbitsbuf[MAX_OUTBUFFER]; 439 MS_S32 rbits_index; 440 441 MS_U32 _MFE_U32MFEEvent; 442 BITSFRAME_INFO bitsframe[MAX_OUTBUFFER]; 443 444 MFE_STAT encode_state; 445 MS_BOOL bForceIframe; 446 MS_U32 MFE_U32MFEMIUOffset; 447 448 seq_parameter_set_rbsp_t *active_sps; 449 pic_parameter_set_rbsp_t *active_pps; 450 // Static SPS, PPS NALU's 451 MS_U8 NaluBuf[2][MAXRBSPSIZE]; 452 NALU_t NaluStruct[2]; 453 454 #ifdef MFE_SUPPORT_TLB 455 MS_U8 bEnableTLB; 456 #endif 457 458 MFE_REG mfe_reg; 459 MFE_REG1 mfe_reg1; 460 461 #ifdef MFE_MIU_PROFILE 462 MS_U32 total_r_count; 463 MS_U32 total_w_count; 464 MS_U32 max_r_count; 465 MS_U32 max_w_count; 466 MS_U32 min_w_count; 467 MS_U32 min_r_count; 468 MS_U32 miu_count; 469 #endif 470 //MFE_REG* mfe_reg_comm; 471 472 MS_U32 setLevel; 473 MS_BOOL setVUI_aspect_ratio_info_present_flag; 474 MS_U32 setVUI_aspect_ratio_idc; 475 MS_U32 setVUI_sar_width; 476 MS_U32 setVUI_sar_height; 477 478 MS_U8 encodeDummyFrame; 479 } MFE_CONFIG; 480 481 typedef struct _VUI_INFO 482 { 483 MS_U32 level; 484 MS_BOOL aspect_ratio_info_present_flag; 485 MS_U32 aspect_ratio_idc; 486 MS_U32 sar_width; 487 MS_U32 sar_height; 488 } VUI_Info; 489 490 // Null functions 491 // #define sd_output_rel(arg1, arg2, arg3) 492 // #define sd_output(arg1, arg2, arg3, arg4) 493 // #define sd_output_reg(arg1, arg2, arg3, arg4, arg5, arg6) 494 495 void ReadRegMFE(MS_U32 u32Address, MS_U16 *val); 496 void WriteRegMFE(MS_U32 u32Address, MS_U16 val, MS_S8 *str1, MS_S32 num, MS_S8 *str3); 497 #if defined(_MFE_MUJI_) || defined(_MFE_MONET_) || defined(_MFE_MESSI_) || defined(_MFE_MANHATTAN_) || defined(_MFE_MASERATI_) || defined(_MFE_MAXIM_) || defined(_MFE_KANO_) || defined(_MFE_K6_) 498 void WriteRegMFE_BANK1(MS_U32 u32Address, MS_U16 val, MS_S8 *str1, MS_S32 num, MS_S8 *str3); 499 #endif 500 void DumpAllReg(MFE_REG* mfe_reg); 501 502 MS_S32 PutFDC(MFE_REG* mfe_reg, void* pContext, MS_S32 mode); 503 void WriteQTable(MFE_REG* mfe_reg, MS_S32* Table0, MS_S32* Table1); 504 505 #if defined(_MFE_M1_)||defined(_MFE_AGATE_) 506 MS_S32 SetObufAddr(MFE_REG* mfe_reg, MS_U32 pAddr, MS_U32 nBufSize, MS_S32 nObufIdx, MS_S32 bSetAdrFlag); 507 #else 508 void SetObufAddr(MFE_REG* mfe_reg, MS_U16 sadr_low, MS_U16 sadr_high, MS_U16 eadr_low, MS_U16 eadr_high); 509 #endif 510 void ClearBsfFullIRQ(MFE_REG* mfe_reg); 511 512 void ClearIRQ(MFE_REG* mfe_reg, MS_S32 number); 513 void Enable_HW(MFE_REG* mfe_reg); 514 void ResetAllRegs(MFE_REG* mfe_reg); 515 516 void TestStopAtMb(MFE_REG* mfe_reg); 517 void TestStop(MFE_REG* mfe_reg); 518 519 void IntraUpdateInit(MFE_CONFIG *pConfig); 520 void IntraUpdateFrame(MFE_CONFIG *pConfig); 521 void IntraUpdateClose(MFE_CONFIG *pConfig); 522 523 // Rate control 524 void MfeDrvRateControlInit(MFE_CONFIG* pConfig); 525 void MfeDrvRateControlUpdate(MFE_CONFIG* pConfig, MS_S8 nFieldType); 526 void MfeDrvRateControlDeInit(MFE_CONFIG* pConfig); 527 // H263/MPEG4 528 void mfeM4VE_Init(MFE_CONFIG *pConfig); 529 void mfeM4VE_EncodeFrame(MFE_CONFIG *pConfig, GOPINFO* pGopInfo); 530 void mfeM4VE_UpdateFrame(MFE_CONFIG *pConfig); 531 MS_S32 codeM4vConfigHeaders(MFE_CONFIG *pConfig,MS_BOOL IsSkipHeader); 532 533 // H264 534 void mfeH264_Init(MFE_CONFIG *pConfig); 535 void mfeH264_EncodeFrame(MFE_CONFIG *pConfig, GOPINFO* pGopInfo); 536 void mfeH264_EncodeDummyFrame(MFE_CONFIG *pConfig, GOPINFO* pGopInfo); 537 void mfeH264_UpdateFrame(MFE_CONFIG *pConfig); 538 MS_S32 codeH264ConfigHeaders(MFE_CONFIG *pConfig,MS_U32* size_of_sps,MS_U32* size_of_pps); 539 void mfeH264_DeInit(MFE_CONFIG* pConfig); 540 MS_S32 MfeDrvCabacStuffingReport(MFE_CONFIG* pConfig); 541 // JPE 542 void mfeJPE_Init(MFE_CONFIG *pConfig); 543 void mfeJPE_Set_QTABLE(MFE_CONFIG *pConfig); 544 void mfeJPE_EncodeFrame(MFE_CONFIG *pConfig, GOPINFO* pGopInfo); 545 546 //MEM MALLOC/FREE 547 void* MfeDrvMemMalloc(MS_SIZE size,const MS_S8* msg); 548 void MfeDrvMemFree(void** mem_ptr,const MS_S8* msg); 549 550 #endif // _MFE_COMMON_H_ 551 552