📄 mfcdriver.cpp
字号:
#include <windows.h>
#include <nkintr.h>
#include <oal_intr.h>
//#include <s3c6400.h>
//#include <DrvLib.h>
#include "MfcDriver.h"
#include "MfcPwrMgmt.h"
#include "MfcSetClk.h"
#include "MfcDrvParams.h"
#include "MfcConfig.h"
#include "MFC_HW_Init.h"
#include "MFC_Instance.h"
#include "MFC_Inst_Pool.h"
#include "MfcSfr.h"
#include "DataBuf.h"
#include "LogMsg.h"
#include "MfcMutex.h"
#include "MfcIntrNotification.h"
static BOOL InitializeIST();
static LONG _openhandle_count = 0;
typedef struct _MFC_HANDLE
{
MFCInstCtx *mfc_inst;
#if (_WIN32_WCE >= 600)
HANDLE hUsrProc; // Used for virtual address conversion (WCE600 or higher)
unsigned char *pStrmBuf; // STRM_BUF pointer (virtual address of Application)
unsigned char *pFramBuf; // FRAM_BUF pointer (virtual address of Application)
#endif
} MFC_HANDLE;
BOOL WINAPI
DllEntry(HANDLE hInstDll, DWORD dwReason, LPVOID lpvReserved)
{
switch ( dwReason )
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls((HMODULE) hInstDll);
break;
}
return (TRUE);
}
// 1. Mutex Creation
// 2. MFC Hardware Initialization
DWORD
MFC_Init(
DWORD dwContext
)
{
/////////////////////////
// 1. Mutex Creation //
/////////////////////////
if (MFC_Mutex_Create() == FALSE)
return FALSE;
///////////////////////////
// 2. MFC Memory Setup //
///////////////////////////
if (MFC_MemorySetup() == FALSE)
return FALSE;
/////////////////////////////////////////
// 3. MFC Clock divider configuration //
/////////////////////////////////////////
if (Mfc_Set_ClkDiv(2) == FALSE)
return FALSE;
// Turn on MFC Power & Clock
Mfc_Pwr_On();
Mfc_Clk_On();
//////////////////////////////////////
// 4. MFC Hardware Initialization //
//////////////////////////////////////
if (MFC_HW_Init() == FALSE)
return FALSE;
// Turn off MFC Power & Clock
Mfc_Clk_Off();
Mfc_Pwr_Off();
///////////////////////////////////////////////////////
// 5. IST(Interrupt Service Thread) Initialization //
///////////////////////////////////////////////////////
if (InitializeIST() == FALSE)
return FALSE;
// Number of open handle
_openhandle_count = 0;
return 0x9224033;
}
// 1. Mutex Deletion
BOOL
MFC_Deinit(
DWORD InitHandle
)
{
MFC_Mutex_Delete();
return TRUE;
}
DWORD
MFC_Open(
DWORD InitHandle,
DWORD dwAccess,
DWORD dwShareMode
)
{
MFC_HANDLE *handle;
// Mutex Lock
MFC_Mutex_Lock();
////////////////
// MFC Handle //
////////////////
handle = (MFC_HANDLE *) malloc(sizeof(MFC_HANDLE));
if (!handle) {
RETAILMSG(1, (L"\n[MFC_Open Error] ERROR\n"));
MFC_Mutex_Release();
return 0;
}
memset(handle, 0, sizeof(MFC_HANDLE));
// MFCInst
handle->mfc_inst = MFCInst_Create();
if (!handle->mfc_inst) {
free(handle);
MFC_Mutex_Release();
RETAILMSG(1, (L"\n[MFC_Open Error] ERROR\n"));
}
// MFC Power is on and MFC HW is initialized
// only if the Open handle count is 1.
// (If number of open handles are more than 1,
// then MFC should not be initialized.)
if (InterlockedIncrement(&_openhandle_count) == 1) {
// MFC Power On
Mfc_Pwr_On();
// MFC HW Init
if (MFC_HW_Init() == FALSE) {
Mfc_Pwr_Off();
return 0;
}
}
// Mutex Release
MFC_Mutex_Release();
return (DWORD) handle;
}
BOOL
MFC_Close(
DWORD OpenHandle
)
{
MFC_HANDLE *handle;
BOOL r;
MFCINST_DEC_INBUF_TYPE inbuf_type;
handle = (MFC_HANDLE *) OpenHandle;
if (handle == NULL)
return FALSE;
#if (_WIN32_WCE >= 600)
r = VirtualFreeEx(handle->hUsrProc, // HANDLE hProcess
handle->pStrmBuf,
handle->mfc_inst->nStrmBufSize,
MEM_DECOMMIT);
if (r == FALSE)
RETAILMSG(1, (L"\n[MFC_Close] VirtualFreeEx(STRM_BUF) returns FALSE.\n"));
r = VirtualFreeEx(handle->hUsrProc, // HANDLE hProcess
handle->pFramBuf,
handle->mfc_inst->nFramBufSize,
MEM_DECOMMIT);
if (r == FALSE)
RETAILMSG(1, (L"\n[MFC_Close] VirtualFreeEx(FRAM_BUF) returns FALSE.\n"));
#endif
if (handle->mfc_inst) {
inbuf_type = handle->mfc_inst->inbuf_type;
MFCInst_Delete(handle->mfc_inst);
}
free(handle);
#if (MFC_LINE_RING_SHARE == 1)
// In case of (MFC_LINE_RING_SHARE == 1), all the instances were reserved.
// Therefore the instances need to be released.
if (inbuf_type == DEC_INBUF_RING_BUF) {
MfcInstPool_ReleaseAll();
}
#endif
if (InterlockedDecrement(&_openhandle_count) == 0) {
// MFC Power Off
Mfc_Pwr_Off();
}
return TRUE;
}
// 1. MFC instance Initialization
// 2. MFC Dec/Enc
BOOL
MFC_IOControl(
DWORD OpenHandle,
DWORD dwIoControlCode,
PBYTE pInBuf,
DWORD nInBufSize,
PBYTE pOutBuf,
DWORD nOutBufSize,
PDWORD pBytesReturned
)
{
int ret;
int nStrmLen;
MFC_HANDLE *handle;
MFCInstCtx *pMfcInst;
MFC_CODECMODE codec_mode;
unsigned char *p_buf;
int n_bufsize;
MFC_ARGS *args;
enc_info_t enc_info;
/////////////////////
// Parameter Check //
/////////////////////
if (OpenHandle == NULL){
LOG_MSG(LOG_TRACE, "MFC_IOControl", "OpenHandle == NULL\n");
return FALSE;
}
if (pInBuf == NULL){
LOG_MSG(LOG_TRACE, "MFC_IOControl", "pInBuf == NULL\n");
return FALSE;
}
if (nInBufSize <= 0){
LOG_MSG(LOG_TRACE, "MFC_IOControl", "nInBufSize <= 0\n");
return FALSE;
}
if ((pOutBuf != NULL) || (nOutBufSize != 0) || (pBytesReturned != NULL)){
LOG_MSG(LOG_TRACE, "MFC_IOControl", "others.....\n");
return FALSE;
}
handle = (MFC_HANDLE *) OpenHandle;
pMfcInst = handle->mfc_inst;
/*
///////////////////
// MFC_CTX check //
///////////////////
if (MfcCtxIsValid(pMfcInst) == FALSE)
return FALSE;
*/
// IOCTL argument
args = (MFC_ARGS *) pInBuf;
MFC_Mutex_Lock();
switch ( dwIoControlCode ) {
case IOCTL_MFC_MPEG4_ENC_INIT:
case IOCTL_MFC_H264_ENC_INIT:
case IOCTL_MFC_H263_ENC_INIT:
if (dwIoControlCode == IOCTL_MFC_MPEG4_ENC_INIT)
codec_mode = MP4_ENC;
else if (dwIoControlCode == IOCTL_MFC_H264_ENC_INIT)
codec_mode = AVC_ENC;
else
codec_mode = H263_ENC;
// Input arguments for IOCTL_MFC_xxx_ENC_INIT
enc_info.width = args->enc_init.in_width;;
enc_info.height = args->enc_init.in_height;;
enc_info.frameRateRes = args->enc_init.in_frameRateRes;
enc_info.frameRateDiv = args->enc_init.in_frameRateDiv;
enc_info.gopNum = args->enc_init.in_gopNum;;
enc_info.bitrate = args->enc_init.in_bitrate;;
///////////////////////////////////
/// Initialize MFC Instance ///
///////////////////////////////////
Mfc_Clk_On();
ret = MFCInst_Enc_Init(pMfcInst, codec_mode, &enc_info);
Mfc_Clk_Off();
// Output arguments for IOCTL_MFC_xxx_ENC_INIT
args->enc_init.ret_code = ret;
break;
case IOCTL_MFC_MPEG4_ENC_EXE:
case IOCTL_MFC_H264_ENC_EXE:
case IOCTL_MFC_H263_ENC_EXE:
// nStrmLen is size of output stream data
Mfc_Clk_On();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -