static OMX_ERRORTYPE SEC_StateSet(SEC_OMX_BASECOMPONENT *pSECComponent, OMX_U32 nParam)
{
    OMX_U32 destState = nParam;
    OMX_U32 i = 0;

    if ((destState == OMX_StateIdle) && (pSECComponent->currentState == OMX_StateLoaded)) {
        pSECComponent->transientState = SEC_OMX_TransStateLoadedToIdle;
        for(i = 0; i < pSECComponent->portParam.nPorts; i++) {
            pSECComponent->pSECPort[i].portState = OMX_StateIdle;
        }
        SEC_OSAL_Log(SEC_LOG_TRACE, "to OMX_StateIdle");
    } else if ((destState == OMX_StateLoaded) && (pSECComponent->currentState == OMX_StateIdle)) {
        pSECComponent->transientState = SEC_OMX_TransStateIdleToLoaded;
        for (i = 0; i < pSECComponent->portParam.nPorts; i++) {
            pSECComponent->pSECPort[i].portState = OMX_StateLoaded;
        }
        SEC_OSAL_Log(SEC_LOG_TRACE, "to OMX_StateLoaded");
    } else if ((destState == OMX_StateIdle) && (pSECComponent->currentState == OMX_StateExecuting)) {
        pSECComponent->transientState = SEC_OMX_TransStateExecutingToIdle;
        SEC_OSAL_Log(SEC_LOG_TRACE, "to OMX_StateIdle");
    } else if ((destState == OMX_StateExecuting) && (pSECComponent->currentState == OMX_StateIdle)) {
        pSECComponent->transientState = SEC_OMX_TransStateIdleToExecuting;
        SEC_OSAL_Log(SEC_LOG_TRACE, "to OMX_StateExecuting");
    } else if (destState == OMX_StateInvalid) {
        for (i = 0; i < pSECComponent->portParam.nPorts; i++) {
            pSECComponent->pSECPort[i].portState = OMX_StateInvalid;
        }
    }

    return OMX_ErrorNone;
}
OMX_ERRORTYPE SEC_OMX_BaseComponent_Constructor(
    OMX_IN OMX_HANDLETYPE hComponent)
{
    OMX_ERRORTYPE          ret = OMX_ErrorNone;
    OMX_COMPONENTTYPE     *pOMXComponent;
    SEC_OMX_BASECOMPONENT *pSECComponent = NULL;

    FunctionIn();

    if (hComponent == NULL) {
        ret = OMX_ErrorBadParameter;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, Line:%d", __LINE__);
        goto EXIT;
    }
    pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    pSECComponent = SEC_OSAL_Malloc(sizeof(SEC_OMX_BASECOMPONENT));
    if (pSECComponent == NULL) {
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }
    SEC_OSAL_Memset(pSECComponent, 0, sizeof(SEC_OMX_BASECOMPONENT));
    pOMXComponent->pComponentPrivate = (OMX_PTR)pSECComponent;

    ret = SEC_OSAL_SemaphoreCreate(&pSECComponent->msgSemaphoreHandle);
    if (ret != OMX_ErrorNone) {
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }
    ret = SEC_OSAL_MutexCreate(&pSECComponent->compMutex);
    if (ret != OMX_ErrorNone) {
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }

    pSECComponent->bExitMessageHandlerThread = OMX_FALSE;
    SEC_OSAL_QueueCreate(&pSECComponent->messageQ);
    ret = SEC_OSAL_ThreadCreate(&pSECComponent->hMessageHandler, SEC_OMX_MessageHandlerThread, pOMXComponent);
    if (ret != OMX_ErrorNone) {
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }

    pOMXComponent->GetComponentVersion = &SEC_OMX_GetComponentVersion;
    pOMXComponent->SendCommand         = &SEC_OMX_SendCommand;
    pOMXComponent->GetConfig           = &SEC_OMX_GetConfig;
    pOMXComponent->GetExtensionIndex   = &SEC_OMX_GetExtensionIndex;
    pOMXComponent->GetState            = &SEC_OMX_GetState;
    pOMXComponent->SetCallbacks        = &SEC_OMX_SetCallbacks;
    pOMXComponent->UseEGLImage         = &SEC_OMX_UseEGLImage;

EXIT:
    FunctionOut();

    return ret;
}
/* MFC Init */
OMX_ERRORTYPE SEC_MFC_H264Enc_Init(OMX_COMPONENTTYPE *pOMXComponent)
{
    OMX_ERRORTYPE              ret = OMX_ErrorNone;
    SEC_OMX_BASECOMPONENT     *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    OMX_PTR                    hMFCHandle = NULL;
    OMX_S32                    returnCodec = 0;

    FunctionIn();

    pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    pH264Enc->hMFCH264Handle.bConfiguredMFC = OMX_FALSE;
    pSECComponent->bUseFlagEOF = OMX_FALSE;
    pSECComponent->bSaveFlagEOS = OMX_FALSE;

    /* MFC(Multi Function Codec) encoder and CMM(Codec Memory Management) driver open */
    hMFCHandle = (OMX_PTR)SsbSipMfcEncOpen();
    if (hMFCHandle == NULL) {
        ret = OMX_ErrorInsufficientResources;
        goto EXIT;
    }
    pH264Enc->hMFCH264Handle.hMFCHandle = hMFCHandle;

    Set_H264ENC_Param(&(pH264Enc->hMFCH264Handle.mfcVideoAvc), pSECComponent);

    returnCodec = SsbSipMfcEncInit(hMFCHandle, &(pH264Enc->hMFCH264Handle.mfcVideoAvc));
    if (returnCodec != MFC_RET_OK) {
        ret = OMX_ErrorInsufficientResources;
        goto EXIT;
    }

    /* Allocate encoder's input buffer */
    returnCodec = SsbSipMfcEncGetInBuf(hMFCHandle, &(pH264Enc->hMFCH264Handle.inputInfo));
    if (returnCodec != MFC_RET_OK) {
        ret = OMX_ErrorInsufficientResources;
        goto EXIT;
    }

    SEC_OSAL_Log(SEC_LOG_TRACE, "pH264Enc->hMFCH264Handle.inputInfo.YVirAddr : 0x%x", pH264Enc->hMFCH264Handle.inputInfo.YVirAddr);
    SEC_OSAL_Log(SEC_LOG_TRACE, "pH264Enc->hMFCH264Handle.inputInfo.CVirAddr : 0x%x", pH264Enc->hMFCH264Handle.inputInfo.CVirAddr);

    pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YPhyAddr = pH264Enc->hMFCH264Handle.inputInfo.YPhyAddr;
    pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CPhyAddr = pH264Enc->hMFCH264Handle.inputInfo.CPhyAddr;
    pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YVirAddr = pH264Enc->hMFCH264Handle.inputInfo.YVirAddr;
    pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CVirAddr = pH264Enc->hMFCH264Handle.inputInfo.CVirAddr;
    pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YSize = pH264Enc->hMFCH264Handle.inputInfo.YSize;
    pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CSize = pH264Enc->hMFCH264Handle.inputInfo.CSize;

    SEC_OSAL_Memset(pSECComponent->timeStamp, -19771003, sizeof(OMX_TICKS) * MAX_TIMESTAMP);
    SEC_OSAL_Memset(pSECComponent->nFlags, 0, sizeof(OMX_U32) * MAX_FLAGS);
    pH264Enc->hMFCH264Handle.indexTimestamp = 0;

EXIT:
    FunctionOut();

    return ret;
}
OMX_ERRORTYPE SEC_OMX_ComponentLoad(SEC_OMX_COMPONENT *sec_component)
{
    OMX_ERRORTYPE      ret = OMX_ErrorNone;
    OMX_HANDLETYPE     libHandle;
    OMX_COMPONENTTYPE *pOMXComponent;

    FunctionIn();

    OMX_ERRORTYPE (*SEC_OMX_ComponentInit)(OMX_HANDLETYPE hComponent, OMX_STRING componentName);

    libHandle = SEC_OSAL_dlopen((OMX_STRING)sec_component->libName, RTLD_NOW);
    if (!libHandle) {
        ret = OMX_ErrorInvalidComponentName;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInvalidComponentName, Line:%d", __LINE__);
        goto EXIT;
    }

    SEC_OMX_ComponentInit = SEC_OSAL_dlsym(libHandle, "SEC_OMX_ComponentInit");
    if (!SEC_OMX_ComponentInit) {
        SEC_OSAL_dlclose(libHandle);
        ret = OMX_ErrorInvalidComponent;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
        goto EXIT;
    }

    pOMXComponent = (OMX_COMPONENTTYPE *)SEC_OSAL_Malloc(sizeof(OMX_COMPONENTTYPE));
    INIT_SET_SIZE_VERSION(pOMXComponent, OMX_COMPONENTTYPE);
    ret = (*SEC_OMX_ComponentInit)((OMX_HANDLETYPE)pOMXComponent, (OMX_STRING)sec_component->componentName);
    if (ret != OMX_ErrorNone) {
        SEC_OSAL_Free(pOMXComponent);
        SEC_OSAL_dlclose(libHandle);
        ret = OMX_ErrorInvalidComponent;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
        goto EXIT;
    } else {
        if (SEC_OMX_ComponentAPICheck(pOMXComponent) != OMX_ErrorNone) {
            if (NULL != pOMXComponent->ComponentDeInit)
                pOMXComponent->ComponentDeInit(pOMXComponent);
            SEC_OSAL_Free(pOMXComponent);
            SEC_OSAL_dlclose(libHandle);
            ret = OMX_ErrorInvalidComponent;
            SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInvalidComponent, Line:%d", __LINE__);
            goto EXIT;
        }
        sec_component->libHandle = libHandle;
        sec_component->pOMXComponent = pOMXComponent;
        ret = OMX_ErrorNone;
    }

EXIT:
    FunctionOut();

    return ret;
}
void Set_Mpeg4Enc_Param(SSBSIP_MFC_ENC_MPEG4_PARAM *pMpeg4Param, SEC_OMX_BASECOMPONENT *pSECComponent)
{
    SEC_OMX_BASEPORT    *pSECInputPort = NULL;
    SEC_OMX_BASEPORT    *pSECOutputPort = NULL;
    SEC_MPEG4ENC_HANDLE *pMpeg4Enc = NULL;

    pMpeg4Enc = (SEC_MPEG4ENC_HANDLE *)pSECComponent->hCodecHandle;
    pSECInputPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    pSECOutputPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];

    pMpeg4Param->codecType            = MPEG4_ENC;
    pMpeg4Param->SourceWidth          = pSECOutputPort->portDefinition.format.video.nFrameWidth;
    pMpeg4Param->SourceHeight         = pSECOutputPort->portDefinition.format.video.nFrameHeight;
    pMpeg4Param->IDRPeriod            = pMpeg4Enc->mpeg4Component[OUTPUT_PORT_INDEX].nPFrames + 1;
    pMpeg4Param->SliceMode            = 0;
    pMpeg4Param->RandomIntraMBRefresh = 0;
    pMpeg4Param->EnableFRMRateControl = 1;    /* 0: disable, 1: enable */
    pMpeg4Param->Bitrate              = pSECOutputPort->portDefinition.format.video.nBitrate;
    pMpeg4Param->FrameQp              = 20;
    pMpeg4Param->FrameQp_P            = 20;
    pMpeg4Param->QSCodeMax            = 30;
    pMpeg4Param->QSCodeMin            = 10;
    pMpeg4Param->CBRPeriodRf          = 10;
    pMpeg4Param->PadControlOn         = 0;    /* 0: Use boundary pixel, 1: Use the below setting value */
    pMpeg4Param->LumaPadVal           = 0;
    pMpeg4Param->CbPadVal             = 0;
    pMpeg4Param->CrPadVal             = 0;

    pMpeg4Param->ProfileIDC           = OMXMpeg4ProfileToMFCProfile(pMpeg4Enc->mpeg4Component[OUTPUT_PORT_INDEX].eProfile);
    pMpeg4Param->LevelIDC             = OMXMpeg4LevelToMFCLevel(pMpeg4Enc->mpeg4Component[OUTPUT_PORT_INDEX].eLevel);
    pMpeg4Param->FrameQp_B            = 20;
    pMpeg4Param->TimeIncreamentRes    = (pSECInputPort->portDefinition.format.video.xFramerate) >> 16;
    pMpeg4Param->VopTimeIncreament    = 1;
    pMpeg4Param->SliceArgument        = 0;    /* MB number or byte number */
    pMpeg4Param->NumberBFrames        = 0;    /* 0(not used) ~ 2 */
    pMpeg4Param->DisableQpelME        = 1;

    SEC_OSAL_Log(SEC_LOG_TRACE, "pSECPort->eControlRate: 0x%x", pSECOutputPort->eControlRate);
    switch (pSECOutputPort->eControlRate) {
    case OMX_Video_ControlRateDisable:
        /* TBD */
        break;
    case OMX_Video_ControlRateVariable:
        /* TBD */
        break;
    default:
        break;
    }

    Mpeg4PrintParams(*pMpeg4Param);
}
void Set_H263Enc_Param(SSBSIP_MFC_ENC_H263_PARAM *pH263Param, SEC_OMX_BASECOMPONENT *pSECComponent)
{
    SEC_OMX_BASEPORT    *pSECInputPort = NULL;
    SEC_OMX_BASEPORT    *pSECOutputPort = NULL;
    SEC_MPEG4ENC_HANDLE *pMpeg4Enc = NULL;

    pMpeg4Enc = (SEC_MPEG4ENC_HANDLE *)pSECComponent->hCodecHandle;
    pSECInputPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    pSECOutputPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];

    pH263Param->codecType            = H263_ENC;
    pH263Param->SourceWidth          = pSECOutputPort->portDefinition.format.video.nFrameWidth;
    pH263Param->SourceHeight         = pSECOutputPort->portDefinition.format.video.nFrameHeight;
    pH263Param->IDRPeriod            = pMpeg4Enc->h263Component[OUTPUT_PORT_INDEX].nPFrames + 1;
    pH263Param->SliceMode            = 0;
    pH263Param->RandomIntraMBRefresh = 0;
    pH263Param->EnableFRMRateControl = 1;    /* 0: disable, 1: enable */
    pH263Param->Bitrate              = pSECOutputPort->portDefinition.format.video.nBitrate;
    pH263Param->FrameQp              = 20;
    pH263Param->FrameQp_P            = 20;
    pH263Param->QSCodeMax            = 30;
    pH263Param->QSCodeMin            = 10;
    pH263Param->CBRPeriodRf          = 10;
    pH263Param->PadControlOn         = 0;    /* 0: Use boundary pixel, 1: Use the below setting value */
    pH263Param->LumaPadVal           = 0;
    pH263Param->CbPadVal             = 0;
    pH263Param->CrPadVal             = 0;

    pH263Param->FrameRate            = (pSECInputPort->portDefinition.format.video.xFramerate) >> 16;

    SEC_OSAL_Log(SEC_LOG_TRACE, "pSECPort->eControlRate: 0x%x", pSECOutputPort->eControlRate);
    switch (pSECOutputPort->eControlRate) {
    case OMX_Video_ControlRateDisable:
        /* TBD */
        break;
    case OMX_Video_ControlRateVariable:
        /* TBD */
        break;
    default:
        break;
    }

    H263PrintParams(*pH263Param);
}
void H263PrintParams(SSBSIP_MFC_ENC_H263_PARAM h263Param)
{
    SEC_OSAL_Log(SEC_LOG_TRACE, "SourceWidth             : %d\n", h263Param.SourceWidth);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SourceHeight            : %d\n", h263Param.SourceHeight);
    SEC_OSAL_Log(SEC_LOG_TRACE, "IDRPeriod               : %d\n", h263Param.IDRPeriod);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SliceMode               : %d\n", h263Param.SliceMode);
    SEC_OSAL_Log(SEC_LOG_TRACE, "RandomIntraMBRefresh    : %d\n", h263Param.RandomIntraMBRefresh);
    SEC_OSAL_Log(SEC_LOG_TRACE, "EnableFRMRateControl    : %d\n", h263Param.EnableFRMRateControl);
    SEC_OSAL_Log(SEC_LOG_TRACE, "Bitrate                 : %d\n", h263Param.Bitrate);
    SEC_OSAL_Log(SEC_LOG_TRACE, "FrameQp                 : %d\n", h263Param.FrameQp);
    SEC_OSAL_Log(SEC_LOG_TRACE, "FrameQp_P               : %d\n", h263Param.FrameQp_P);
    SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMax               : %d\n", h263Param.QSCodeMax);
    SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMin               : %d\n", h263Param.QSCodeMin);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CBRPeriodRf             : %d\n", h263Param.CBRPeriodRf);
    SEC_OSAL_Log(SEC_LOG_TRACE, "PadControlOn            : %d\n", h263Param.PadControlOn);
    SEC_OSAL_Log(SEC_LOG_TRACE, "LumaPadVal              : %d\n", h263Param.LumaPadVal);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CbPadVal                : %d\n", h263Param.CbPadVal);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CrPadVal                : %d\n", h263Param.CrPadVal);

    /* H.263 specific parameters */
    SEC_OSAL_Log(SEC_LOG_TRACE, "FrameRate               : %d\n", h263Param.FrameRate);
}
void Mpeg4PrintParams(SSBSIP_MFC_ENC_MPEG4_PARAM mpeg4Param)
{
    SEC_OSAL_Log(SEC_LOG_TRACE, "SourceWidth             : %d\n", mpeg4Param.SourceWidth);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SourceHeight            : %d\n", mpeg4Param.SourceHeight);
    SEC_OSAL_Log(SEC_LOG_TRACE, "IDRPeriod               : %d\n", mpeg4Param.IDRPeriod);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SliceMode               : %d\n", mpeg4Param.SliceMode);
    SEC_OSAL_Log(SEC_LOG_TRACE, "RandomIntraMBRefresh    : %d\n", mpeg4Param.RandomIntraMBRefresh);
    SEC_OSAL_Log(SEC_LOG_TRACE, "EnableFRMRateControl    : %d\n", mpeg4Param.EnableFRMRateControl);
    SEC_OSAL_Log(SEC_LOG_TRACE, "Bitrate                 : %d\n", mpeg4Param.Bitrate);
    SEC_OSAL_Log(SEC_LOG_TRACE, "FrameQp                 : %d\n", mpeg4Param.FrameQp);
    SEC_OSAL_Log(SEC_LOG_TRACE, "FrameQp_P               : %d\n", mpeg4Param.FrameQp_P);
    SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMax               : %d\n", mpeg4Param.QSCodeMax);
    SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMin               : %d\n", mpeg4Param.QSCodeMin);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CBRPeriodRf             : %d\n", mpeg4Param.CBRPeriodRf);
    SEC_OSAL_Log(SEC_LOG_TRACE, "PadControlOn            : %d\n", mpeg4Param.PadControlOn);
    SEC_OSAL_Log(SEC_LOG_TRACE, "LumaPadVal              : %d\n", mpeg4Param.LumaPadVal);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CbPadVal                : %d\n", mpeg4Param.CbPadVal);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CrPadVal                : %d\n", mpeg4Param.CrPadVal);

    /* MPEG4 specific parameters */
    SEC_OSAL_Log(SEC_LOG_TRACE, "ProfileIDC              : %d\n", mpeg4Param.ProfileIDC);
    SEC_OSAL_Log(SEC_LOG_TRACE, "LevelIDC                : %d\n", mpeg4Param.LevelIDC);
    SEC_OSAL_Log(SEC_LOG_TRACE, "FrameQp_B               : %d\n", mpeg4Param.FrameQp_B);
    SEC_OSAL_Log(SEC_LOG_TRACE, "TimeIncreamentRes       : %d\n", mpeg4Param.TimeIncreamentRes);
    SEC_OSAL_Log(SEC_LOG_TRACE, "VopTimeIncreament       : %d\n", mpeg4Param.VopTimeIncreament);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SliceArgument           : %d\n", mpeg4Param.SliceArgument);
    SEC_OSAL_Log(SEC_LOG_TRACE, "NumberBFrames           : %d\n", mpeg4Param.NumberBFrames);
    SEC_OSAL_Log(SEC_LOG_TRACE, "DisableQpelME           : %d\n", mpeg4Param.DisableQpelME);
}
OMX_ERRORTYPE SEC_OMX_FlushPort(OMX_COMPONENTTYPE *pOMXComponent, OMX_S32 portIndex)
{
    OMX_ERRORTYPE          ret = OMX_ErrorNone;
    SEC_OMX_BASECOMPONENT *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    SEC_OMX_BASEPORT      *pSECPort = NULL;
    OMX_BUFFERHEADERTYPE  *bufferHeader = NULL;
    SEC_OMX_MESSAGE       *message = NULL;
    OMX_U32                flushNum = 0;
    OMX_S32                semValue = 0;

    FunctionIn();

    pSECPort = &pSECComponent->pSECPort[portIndex];
    while (SEC_OSAL_GetElemNum(&pSECPort->bufferQ) > 0) {
        SEC_OSAL_Get_SemaphoreCount(pSECComponent->pSECPort[portIndex].bufferSemID, &semValue);
        if (semValue == 0)
            SEC_OSAL_SemaphorePost(pSECComponent->pSECPort[portIndex].bufferSemID);
        SEC_OSAL_SemaphoreWait(pSECComponent->pSECPort[portIndex].bufferSemID);

        message = (SEC_OMX_MESSAGE *)SEC_OSAL_Dequeue(&pSECPort->bufferQ);
        if (message != NULL) {
            bufferHeader = (OMX_BUFFERHEADERTYPE *)message->pCmdData;
            bufferHeader->nFilledLen = 0;

            if (CHECK_PORT_TUNNELED(pSECPort) && !CHECK_PORT_BUFFER_SUPPLIER(pSECPort)) {
                if (portIndex) {
                    OMX_EmptyThisBuffer(pSECPort->tunneledComponent, bufferHeader);
                } else {
                    OMX_FillThisBuffer(pSECPort->tunneledComponent, bufferHeader);
                }
                SEC_OSAL_Free(message);
                message = NULL;
            } else if (CHECK_PORT_TUNNELED(pSECPort) && CHECK_PORT_BUFFER_SUPPLIER(pSECPort)) {
                SEC_OSAL_Log(SEC_LOG_ERROR, "Tunneled mode is not working, Line:%d", __LINE__);
                ret = OMX_ErrorNotImplemented;
                SEC_OSAL_Queue(&pSECPort->bufferQ, pSECPort);
                goto EXIT;
            } else {
                if (portIndex == OUTPUT_PORT_INDEX) {
                    pSECComponent->pCallbacks->FillBufferDone(pOMXComponent, pSECComponent->callbackData, bufferHeader);
                } else {
                    pSECComponent->pCallbacks->EmptyBufferDone(pOMXComponent, pSECComponent->callbackData, bufferHeader);
                }

                SEC_OSAL_Free(message);
                message = NULL;
            }
        }
    }

    if (pSECComponent->secDataBuffer[portIndex].dataValid == OMX_TRUE) {
        if (CHECK_PORT_TUNNELED(pSECPort) && CHECK_PORT_BUFFER_SUPPLIER(pSECPort)) {
            message = SEC_OSAL_Malloc(sizeof(SEC_OMX_MESSAGE));
            message->pCmdData = pSECComponent->secDataBuffer[portIndex].bufferHeader;
            message->messageType = 0;
            message->messageParam = -1;
            SEC_OSAL_Queue(&pSECPort->bufferQ, message);
            pSECComponent->sec_BufferReset(pOMXComponent, portIndex);
        } else {
            if (portIndex == INPUT_PORT_INDEX)
                pSECComponent->sec_InputBufferReturn(pOMXComponent);
            else if (portIndex == OUTPUT_PORT_INDEX)
                pSECComponent->sec_OutputBufferReturn(pOMXComponent);
        }
    }

    if (CHECK_PORT_TUNNELED(pSECPort) && CHECK_PORT_BUFFER_SUPPLIER(pSECPort)) {
        while (SEC_OSAL_GetElemNum(&pSECPort->bufferQ) < pSECPort->assignedBufferNum) {
            SEC_OSAL_SemaphoreWait(pSECComponent->pSECPort[portIndex].bufferSemID);
        }
        if (SEC_OSAL_GetElemNum(&pSECPort->bufferQ) != pSECPort->assignedBufferNum)
            SEC_OSAL_SetElemNum(&pSECPort->bufferQ, pSECPort->assignedBufferNum);
    } else {
        while(1) {
            int cnt;
            SEC_OSAL_Get_SemaphoreCount(pSECComponent->pSECPort[portIndex].bufferSemID, &cnt);
            if (cnt == 0)
                break;
            SEC_OSAL_SemaphoreWait(pSECComponent->pSECPort[portIndex].bufferSemID);
        }
        SEC_OSAL_SetElemNum(&pSECPort->bufferQ, 0);
    }

    pSECComponent->processData[portIndex].dataLen       = 0;
    pSECComponent->processData[portIndex].nFlags        = 0;
    pSECComponent->processData[portIndex].remainDataLen = 0;
    pSECComponent->processData[portIndex].timeStamp     = 0;
    pSECComponent->processData[portIndex].usedDataLen   = 0;

EXIT:
    FunctionOut();

    return ret;
}
void Set_H264ENC_Param(SSBSIP_MFC_ENC_H264_PARAM *pH264Arg, SEC_OMX_BASECOMPONENT *pSECComponent)
{
    SEC_OMX_BASEPORT          *pSECInputPort = NULL;
    SEC_OMX_BASEPORT          *pSECOutputPort = NULL;
    SEC_H264ENC_HANDLE        *pH264Enc = NULL;

    pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    pSECInputPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    pSECOutputPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];

    pH264Arg->codecType    = H264_ENC;
    pH264Arg->SourceWidth  = pSECOutputPort->portDefinition.format.video.nFrameWidth;
    pH264Arg->SourceHeight = pSECOutputPort->portDefinition.format.video.nFrameHeight;
    pH264Arg->IDRPeriod    = pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].nPFrames + 1;
    pH264Arg->SliceMode    = 0;
    pH264Arg->RandomIntraMBRefresh = 0;
    pH264Arg->EnableFRMRateControl = 1;        // 0: Disable, 1: Frame level RC
    pH264Arg->Bitrate      = pSECOutputPort->portDefinition.format.video.nBitrate;
    pH264Arg->FrameQp      = 20;
    pH264Arg->FrameQp_P    = 20;
    pH264Arg->QSCodeMax    = 30;
    pH264Arg->QSCodeMin    = 10;
    pH264Arg->CBRPeriodRf  = 100;
    pH264Arg->PadControlOn = 0;             // 0: disable, 1: enable
    pH264Arg->LumaPadVal   = 0;
    pH264Arg->CbPadVal     = 0;
    pH264Arg->CrPadVal     = 0;

    pH264Arg->ProfileIDC   = OMXAVCProfileToProfileIDC(pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].eProfile); //0;  //(OMX_VIDEO_AVCProfileMain)
    pH264Arg->LevelIDC     = OMXAVCLevelToLevelIDC(pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].eLevel);       //40; //(OMX_VIDEO_AVCLevel4)
    pH264Arg->FrameQp_B    = 20;
    pH264Arg->FrameRate    = (pSECInputPort->portDefinition.format.video.xFramerate) >> 16;
    pH264Arg->SliceArgument = 0;          // Slice mb/byte size number
    pH264Arg->NumberBFrames = 0;            // 0 ~ 2
    pH264Arg->NumberReferenceFrames = 1;
    pH264Arg->NumberRefForPframes   = 1;
    pH264Arg->LoopFilterDisable     = 1;    // 1: Loop Filter Disable, 0: Filter Enable
    pH264Arg->LoopFilterAlphaC0Offset = 0;
    pH264Arg->LoopFilterBetaOffset    = 0;
    pH264Arg->SymbolMode       = 0;         // 0: CAVLC, 1: CABAC
    pH264Arg->PictureInterlace = 0;
    pH264Arg->Transform8x8Mode = 0;         // 0: 4x4, 1: allow 8x8
    pH264Arg->EnableMBRateControl = 0;        // 0: Disable, 1:MB level RC
    pH264Arg->DarkDisable     = 1;
    pH264Arg->SmoothDisable   = 1;
    pH264Arg->StaticDisable   = 1;
    pH264Arg->ActivityDisable = 1;

    SEC_OSAL_Log(SEC_LOG_TRACE, "pSECPort->eControlRate: 0x%x", pSECOutputPort->eControlRate);
    switch (pSECOutputPort->eControlRate) {
    case OMX_Video_ControlRateDisable:
        /* TBD */
        break;
    case OMX_Video_ControlRateVariable:
        /* TBD */
        break;
    default:
        break;
    }

    H264PrintParams(*pH264Arg);
}
void H264PrintParams(SSBSIP_MFC_ENC_H264_PARAM h264Arg)
{
    SEC_OSAL_Log(SEC_LOG_TRACE, "SourceWidth             : %d\n", h264Arg.SourceWidth);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SourceHeight            : %d\n", h264Arg.SourceHeight);
    SEC_OSAL_Log(SEC_LOG_TRACE, "ProfileIDC              : %d\n", h264Arg.ProfileIDC);
    SEC_OSAL_Log(SEC_LOG_TRACE, "LevelIDC                : %d\n", h264Arg.LevelIDC);
    SEC_OSAL_Log(SEC_LOG_TRACE, "IDRPeriod               : %d\n", h264Arg.IDRPeriod);
    SEC_OSAL_Log(SEC_LOG_TRACE, "NumberReferenceFrames   : %d\n", h264Arg.NumberReferenceFrames);
    SEC_OSAL_Log(SEC_LOG_TRACE, "NumberRefForPframes     : %d\n", h264Arg.NumberRefForPframes);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SliceMode               : %d\n", h264Arg.SliceMode);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SliceArgument           : %d\n", h264Arg.SliceArgument);
    SEC_OSAL_Log(SEC_LOG_TRACE, "NumberBFrames           : %d\n", h264Arg.NumberBFrames);
    SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterDisable       : %d\n", h264Arg.LoopFilterDisable);
    SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterAlphaC0Offset : %d\n", h264Arg.LoopFilterAlphaC0Offset);
    SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterBetaOffset    : %d\n", h264Arg.LoopFilterBetaOffset);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SymbolMode              : %d\n", h264Arg.SymbolMode);
    SEC_OSAL_Log(SEC_LOG_TRACE, "PictureInterlace        : %d\n", h264Arg.PictureInterlace);
    SEC_OSAL_Log(SEC_LOG_TRACE, "Transform8x8Mode        : %d\n", h264Arg.Transform8x8Mode);
    SEC_OSAL_Log(SEC_LOG_TRACE, "RandomIntraMBRefresh    : %d\n", h264Arg.RandomIntraMBRefresh);
    SEC_OSAL_Log(SEC_LOG_TRACE, "PadControlOn            : %d\n", h264Arg.PadControlOn);
    SEC_OSAL_Log(SEC_LOG_TRACE, "LumaPadVal              : %d\n", h264Arg.LumaPadVal);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CbPadVal                : %d\n", h264Arg.CbPadVal);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CrPadVal                : %d\n", h264Arg.CrPadVal);
    SEC_OSAL_Log(SEC_LOG_TRACE, "EnableFRMRateControl    : %d\n", h264Arg.EnableFRMRateControl);
    SEC_OSAL_Log(SEC_LOG_TRACE, "EnableMBRateControl     : %d\n", h264Arg.EnableMBRateControl);
    SEC_OSAL_Log(SEC_LOG_TRACE, "FrameRate               : %d\n", h264Arg.FrameRate);
    SEC_OSAL_Log(SEC_LOG_TRACE, "Bitrate                 : %d\n", h264Arg.Bitrate);
    SEC_OSAL_Log(SEC_LOG_TRACE, "FrameQp                 : %d\n", h264Arg.FrameQp);
    SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMax               : %d\n", h264Arg.QSCodeMax);
    SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMin               : %d\n", h264Arg.QSCodeMin);
    SEC_OSAL_Log(SEC_LOG_TRACE, "CBRPeriodRf             : %d\n", h264Arg.CBRPeriodRf);
    SEC_OSAL_Log(SEC_LOG_TRACE, "DarkDisable             : %d\n", h264Arg.DarkDisable);
    SEC_OSAL_Log(SEC_LOG_TRACE, "SmoothDisable           : %d\n", h264Arg.SmoothDisable);
    SEC_OSAL_Log(SEC_LOG_TRACE, "StaticDisable           : %d\n", h264Arg.StaticDisable);
    SEC_OSAL_Log(SEC_LOG_TRACE, "ActivityDisable         : %d\n", h264Arg.ActivityDisable);
}
OMX_ERRORTYPE SEC_OMX_Component_Register(SEC_OMX_COMPONENT_REGLIST **compList, OMX_U32 *compNum)
{
    OMX_ERRORTYPE  ret = OMX_ErrorNone;
    int            componentNum = 0, roleNum = 0, totalCompNum = 0;
    int            read;
    char          *libName;
    size_t         len;
    const char    *errorMsg;
    DIR           *dir;
    struct dirent *d;

    int (*SEC_OMX_COMPONENT_Library_Register)(SECRegisterComponentType **secComponents);
    SECRegisterComponentType **secComponentsTemp;
    SEC_OMX_COMPONENT_REGLIST *componentList;

    FunctionIn();

    dir = opendir(SEC_OMX_INSTALL_PATH);
    if (dir == NULL) {
        ret = OMX_ErrorUndefined;
        goto EXIT;
    }

    componentList = (SEC_OMX_COMPONENT_REGLIST *)SEC_OSAL_Malloc(sizeof(SEC_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
    SEC_OSAL_Memset(componentList, 0, sizeof(SEC_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
    libName = SEC_OSAL_Malloc(MAX_OMX_COMPONENT_LIBNAME_SIZE);

    while ((d = readdir(dir)) != NULL) {
        OMX_HANDLETYPE soHandle;
        SEC_OSAL_Log(SEC_LOG_ERROR, "%s", d->d_name);

        if (SEC_OSAL_Strncmp(d->d_name, "libOMX.SEC.", SEC_OSAL_Strlen("libOMX.SEC.")) == 0) {
            SEC_OSAL_Memset(libName, 0, MAX_OMX_COMPONENT_LIBNAME_SIZE);
            SEC_OSAL_Strcpy(libName, SEC_OMX_INSTALL_PATH);
            SEC_OSAL_Strcat(libName, d->d_name);
            SEC_OSAL_Log(SEC_LOG_ERROR, "Path & libName : %s", libName);
            if ((soHandle = SEC_OSAL_dlopen(libName, RTLD_NOW)) != NULL) {
                SEC_OSAL_dlerror();    /* clear error*/
                if ((SEC_OMX_COMPONENT_Library_Register = SEC_OSAL_dlsym(soHandle, "SEC_OMX_COMPONENT_Library_Register")) != NULL) {
                    int i = 0;
                    unsigned int j = 0;

                    componentNum = (*SEC_OMX_COMPONENT_Library_Register)(NULL);
                    secComponentsTemp = (SECRegisterComponentType **)SEC_OSAL_Malloc(sizeof(SECRegisterComponentType*) * componentNum);
                    for (i = 0; i < componentNum; i++) {
                        secComponentsTemp[i] = SEC_OSAL_Malloc(sizeof(SECRegisterComponentType));
                        SEC_OSAL_Memset(secComponentsTemp[i], 0, sizeof(SECRegisterComponentType));
                    }
                    (*SEC_OMX_COMPONENT_Library_Register)(secComponentsTemp);

                    for (i = 0; i < componentNum; i++) {
                        SEC_OSAL_Strcpy(componentList[totalCompNum].component.componentName, secComponentsTemp[i]->componentName);
                        for (j = 0; j < secComponentsTemp[i]->totalRoleNum; j++)
                            SEC_OSAL_Strcpy(componentList[totalCompNum].component.roles[j], secComponentsTemp[i]->roles[j]);
                        componentList[totalCompNum].component.totalRoleNum = secComponentsTemp[i]->totalRoleNum;

                        SEC_OSAL_Strcpy(componentList[totalCompNum].libName, libName);

                        totalCompNum++;
                    }
                    for (i = 0; i < componentNum; i++) {
                        SEC_OSAL_Free(secComponentsTemp[i]);
                    }

                    SEC_OSAL_Free(secComponentsTemp);
                } else {
                    if ((errorMsg = SEC_OSAL_dlerror()) != NULL)
                        SEC_OSAL_Log(SEC_LOG_WARNING, "dlsym failed: %s", errorMsg);
                }
                SEC_OSAL_dlclose(soHandle);
            } else {
                SEC_OSAL_Log(SEC_LOG_WARNING, "dlopen failed: %s", SEC_OSAL_dlerror());
            }
        } else {
            /* not a component name line. skip */
            continue;
        }
    }

    SEC_OSAL_Free(libName);

    closedir(dir);

    *compList = componentList;
    *compNum = totalCompNum;

EXIT:
    FunctionOut();

    return ret;
}
OMX_ERRORTYPE SEC_OMX_Component_Register(SEC_OMX_COMPONENT_REGLIST **compList, OMX_U32 *compNum)
{
    OMX_ERRORTYPE  ret = OMX_ErrorNone;
    int            componentNum = 0, roleNum = 0, totalCompNum = 0;
    int            read;
    char          *omxregistryfile = NULL;
    char          *line = NULL;
    char          *libName;
    FILE          *omxregistryfp;
    size_t         len;
    OMX_HANDLETYPE soHandle;
    const char    *errorMsg;
    int (*SEC_OMX_COMPONENT_Library_Register)(SECRegisterComponentType **secComponents);
    SECRegisterComponentType **secComponentsTemp;
    SEC_OMX_COMPONENT_REGLIST *componentList;

    FunctionIn();

    omxregistryfile = SEC_OSAL_Malloc(strlen("/system/etc/") + strlen(REGISTRY_FILENAME) + 2);
    SEC_OSAL_Strcpy(omxregistryfile, "/system/etc/");
    SEC_OSAL_Strcat(omxregistryfile, REGISTRY_FILENAME);

    omxregistryfp = fopen(omxregistryfile, "r");
    if (omxregistryfp == NULL) {
        ret = OMX_ErrorUndefined;
        goto EXIT;
    }
    SEC_OSAL_Free(omxregistryfile);

    fseek(omxregistryfp, 0, 0);
    componentList = (SEC_OMX_COMPONENT_REGLIST *)SEC_OSAL_Malloc(sizeof(SEC_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
    SEC_OSAL_Memset(componentList, 0, sizeof(SEC_OMX_COMPONENT_REGLIST) * MAX_OMX_COMPONENT_NUM);
    libName = SEC_OSAL_Malloc(MAX_OMX_COMPONENT_LIBNAME_SIZE);

    while ((read = getline(&line, &len, omxregistryfp)) != -1) {
        if ((*line == 'l') && (*(line + 1) == 'i') && (*(line + 2) == 'b') &&
            (*(line + 3) == 'O') && (*(line + 4) == 'M') && (*(line + 5) == 'X')) {
            SEC_OSAL_Memset(libName, 0, MAX_OMX_COMPONENT_LIBNAME_SIZE);
            SEC_OSAL_Strncpy(libName, line, SEC_OSAL_Strlen(line)-1);
            SEC_OSAL_Log(SEC_LOG_TRACE, "libName : %s", libName);

            if ((soHandle = SEC_OSAL_dlopen(libName, RTLD_NOW)) != NULL) {
                SEC_OSAL_dlerror();    /* clear error*/
                if ((SEC_OMX_COMPONENT_Library_Register = SEC_OSAL_dlsym(soHandle, "SEC_OMX_COMPONENT_Library_Register")) != NULL) {
                    int i = 0, j = 0;

                    componentNum = (*SEC_OMX_COMPONENT_Library_Register)(NULL);
                    secComponentsTemp = (SECRegisterComponentType **)SEC_OSAL_Malloc(sizeof(SECRegisterComponentType*) * componentNum);
                    for (i = 0; i < componentNum; i++) {
                        secComponentsTemp[i] = SEC_OSAL_Malloc(sizeof(SECRegisterComponentType));
                        SEC_OSAL_Memset(secComponentsTemp[i], 0, sizeof(SECRegisterComponentType));
                    }
                    (*SEC_OMX_COMPONENT_Library_Register)(secComponentsTemp);

                    for (i = 0; i < componentNum; i++) {
                        SEC_OSAL_Strcpy(componentList[totalCompNum].component.componentName, secComponentsTemp[i]->componentName);
                        for (j = 0; j < secComponentsTemp[i]->totalRoleNum; j++)
                            SEC_OSAL_Strcpy(componentList[totalCompNum].component.roles[j], secComponentsTemp[i]->roles[j]);
                        componentList[totalCompNum].component.totalRoleNum = secComponentsTemp[i]->totalRoleNum;

                        SEC_OSAL_Strcpy(componentList[totalCompNum].libName, libName);

                        totalCompNum++;
                    }
                    for (i = 0; i < componentNum; i++) {
                        SEC_OSAL_Free(secComponentsTemp[i]);
                    }

                    SEC_OSAL_Free(secComponentsTemp);
                } else {
                    if ((errorMsg = SEC_OSAL_dlerror()) != NULL)
                        SEC_OSAL_Log(SEC_LOG_WARNING, "dlsym failed: %s", errorMsg);
                }
                SEC_OSAL_dlclose(soHandle);
            } else {
                SEC_OSAL_Log(SEC_LOG_WARNING, "dlopen failed: %s", SEC_OSAL_dlerror());
            }
        } else {
            /* not a component name line. skip */
            continue;
        }
    }

    SEC_OSAL_Free(libName);
    fclose(omxregistryfp);

    *compList = componentList;
    *compNum = totalCompNum;

EXIT:
    FunctionOut();

    return ret;
}
OMX_ERRORTYPE SEC_OMX_Port_Constructor(OMX_HANDLETYPE hComponent)
{
    OMX_ERRORTYPE          ret = OMX_ErrorNone;
    OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    SEC_OMX_BASEPORT      *pSECPort = NULL;
    SEC_OMX_BASEPORT      *pSECInputPort = NULL;
    SEC_OMX_BASEPORT      *pSECOutputPort = NULL;
    int i = 0;

    FunctionIn();

    if (hComponent == NULL) {
        ret = OMX_ErrorBadParameter;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, Line:%d", __LINE__);
        goto EXIT;
    }
    pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    if (ret != OMX_ErrorNone) {
        goto EXIT;
    }

    if (pOMXComponent->pComponentPrivate == NULL) {
        ret = OMX_ErrorBadParameter;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, Line:%d", __LINE__);
        goto EXIT;
    }
    pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;

    INIT_SET_SIZE_VERSION(&pSECComponent->portParam, OMX_PORT_PARAM_TYPE);
    pSECComponent->portParam.nPorts = ALL_PORT_NUM;
    pSECComponent->portParam.nStartPortNumber = INPUT_PORT_INDEX;

    pSECPort = SEC_OSAL_Malloc(sizeof(SEC_OMX_BASEPORT) * ALL_PORT_NUM);
    if (pSECPort == NULL) {
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }
    SEC_OSAL_Memset(pSECPort, 0, sizeof(SEC_OMX_BASEPORT) * ALL_PORT_NUM);
    pSECComponent->pSECPort = pSECPort;

    /* Input Port */
    pSECInputPort = &pSECPort[INPUT_PORT_INDEX];

    SEC_OSAL_QueueCreate(&pSECInputPort->bufferQ);

    pSECInputPort->bufferHeader = SEC_OSAL_Malloc(sizeof(OMX_BUFFERHEADERTYPE*) * MAX_BUFFER_NUM);
    if (pSECInputPort->bufferHeader == NULL) {
        SEC_OSAL_Free(pSECPort);
        pSECPort = NULL;
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }
    SEC_OSAL_Memset(pSECInputPort->bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE*) * MAX_BUFFER_NUM);

    pSECInputPort->bufferStateAllocate = SEC_OSAL_Malloc(sizeof(OMX_U32) * MAX_BUFFER_NUM);
    if (pSECInputPort->bufferStateAllocate == NULL) {
        SEC_OSAL_Free(pSECInputPort->bufferHeader);
        pSECInputPort->bufferHeader = NULL;
        SEC_OSAL_Free(pSECPort);
        pSECPort = NULL;
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }
    SEC_OSAL_Memset(pSECInputPort->bufferStateAllocate, 0, sizeof(OMX_U32) * MAX_BUFFER_NUM);

    pSECInputPort->bufferSemID = NULL;
    pSECInputPort->assignedBufferNum = 0;
    pSECInputPort->portState = OMX_StateMax;
    pSECInputPort->bIsPortFlushed = OMX_FALSE;
    pSECInputPort->bIsPortDisabled = OMX_FALSE;
    pSECInputPort->tunneledComponent = NULL;
    pSECInputPort->tunneledPort = 0;
    pSECInputPort->tunnelBufferNum = 0;
    pSECInputPort->bufferSupplier = OMX_BufferSupplyUnspecified;
    pSECInputPort->tunnelFlags = 0;
    pSECInputPort->eControlRate = OMX_Video_ControlRateDisable;
    ret = SEC_OSAL_SemaphoreCreate(&pSECInputPort->loadedResource);
    if (ret != OMX_ErrorNone) {
        SEC_OSAL_Free(pSECInputPort->bufferStateAllocate);
        pSECInputPort->bufferStateAllocate = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferHeader);
        pSECInputPort->bufferHeader = NULL;
        SEC_OSAL_Free(pSECPort);
        pSECPort = NULL;
        goto EXIT;
    }
    ret = SEC_OSAL_SemaphoreCreate(&pSECInputPort->unloadedResource);
    if (ret != OMX_ErrorNone) {
        SEC_OSAL_SemaphoreTerminate(pSECInputPort->loadedResource);
        pSECInputPort->loadedResource = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferStateAllocate);
        pSECInputPort->bufferStateAllocate = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferHeader);
        pSECInputPort->bufferHeader = NULL;
        SEC_OSAL_Free(pSECPort);
        pSECPort = NULL;
        goto EXIT;
    }

    INIT_SET_SIZE_VERSION(&pSECInputPort->portDefinition, OMX_PARAM_PORTDEFINITIONTYPE);
    pSECInputPort->portDefinition.nPortIndex = INPUT_PORT_INDEX;
    pSECInputPort->portDefinition.eDir = OMX_DirInput;
    pSECInputPort->portDefinition.nBufferCountActual = 0;
    pSECInputPort->portDefinition.nBufferCountMin = 0;
    pSECInputPort->portDefinition.nBufferSize = 0;
    pSECInputPort->portDefinition.bEnabled = OMX_FALSE;
    pSECInputPort->portDefinition.bPopulated = OMX_FALSE;
    pSECInputPort->portDefinition.eDomain = OMX_PortDomainMax;
    pSECInputPort->portDefinition.bBuffersContiguous = OMX_FALSE;
    pSECInputPort->portDefinition.nBufferAlignment = 0;
    pSECInputPort->markType.hMarkTargetComponent = NULL;
    pSECInputPort->markType.pMarkData = NULL;

    /* Output Port */
    pSECOutputPort = &pSECPort[OUTPUT_PORT_INDEX];

    SEC_OSAL_QueueCreate(&pSECOutputPort->bufferQ);

    pSECOutputPort->bufferHeader = SEC_OSAL_Malloc(sizeof(OMX_BUFFERHEADERTYPE*) * MAX_BUFFER_NUM);
    if (pSECOutputPort->bufferHeader == NULL) {
        SEC_OSAL_SemaphoreTerminate(pSECInputPort->unloadedResource);
        pSECInputPort->unloadedResource = NULL;
        SEC_OSAL_SemaphoreTerminate(pSECInputPort->loadedResource);
        pSECInputPort->loadedResource = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferStateAllocate);
        pSECInputPort->bufferStateAllocate = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferHeader);
        pSECInputPort->bufferHeader = NULL;
        SEC_OSAL_Free(pSECPort);
        pSECPort = NULL;
        ret = OMX_ErrorInsufficientResources;
        goto EXIT;
    }
    SEC_OSAL_Memset(pSECOutputPort->bufferHeader, 0, sizeof(OMX_BUFFERHEADERTYPE*) * MAX_BUFFER_NUM);

    pSECOutputPort->bufferStateAllocate = SEC_OSAL_Malloc(sizeof(OMX_U32) * MAX_BUFFER_NUM);
    if (pSECOutputPort->bufferStateAllocate == NULL) {
        SEC_OSAL_Free(pSECOutputPort->bufferHeader);
        pSECOutputPort->bufferHeader = NULL;

        SEC_OSAL_SemaphoreTerminate(pSECInputPort->unloadedResource);
        pSECInputPort->unloadedResource = NULL;
        SEC_OSAL_SemaphoreTerminate(pSECInputPort->loadedResource);
        pSECInputPort->loadedResource = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferStateAllocate);
        pSECInputPort->bufferStateAllocate = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferHeader);
        pSECInputPort->bufferHeader = NULL;
        SEC_OSAL_Free(pSECPort);
        pSECPort = NULL;
        ret = OMX_ErrorInsufficientResources;
        goto EXIT;
    }
    SEC_OSAL_Memset(pSECOutputPort->bufferStateAllocate, 0, sizeof(OMX_U32) * MAX_BUFFER_NUM);

    pSECOutputPort->bufferSemID = NULL;
    pSECOutputPort->assignedBufferNum = 0;
    pSECOutputPort->portState = OMX_StateMax;
    pSECOutputPort->bIsPortFlushed = OMX_FALSE;
    pSECOutputPort->bIsPortDisabled = OMX_FALSE;
    pSECOutputPort->tunneledComponent = NULL;
    pSECOutputPort->tunneledPort = 0;
    pSECOutputPort->tunnelBufferNum = 0;
    pSECOutputPort->bufferSupplier = OMX_BufferSupplyUnspecified;
    pSECOutputPort->tunnelFlags = 0;
    pSECOutputPort->eControlRate = OMX_Video_ControlRateDisable;
    ret = SEC_OSAL_SemaphoreCreate(&pSECOutputPort->loadedResource);
    if (ret != OMX_ErrorNone) {
        SEC_OSAL_Free(pSECOutputPort->bufferStateAllocate);
        pSECOutputPort->bufferStateAllocate = NULL;
        SEC_OSAL_Free(pSECOutputPort->bufferHeader);
        pSECOutputPort->bufferHeader = NULL;

        SEC_OSAL_SemaphoreTerminate(pSECInputPort->unloadedResource);
        pSECInputPort->unloadedResource = NULL;
        SEC_OSAL_SemaphoreTerminate(pSECInputPort->loadedResource);
        pSECInputPort->loadedResource = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferStateAllocate);
        pSECInputPort->bufferStateAllocate = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferHeader);
        pSECInputPort->bufferHeader = NULL;
        SEC_OSAL_Free(pSECPort);
        pSECPort = NULL;
        goto EXIT;
    }
    ret = SEC_OSAL_SemaphoreCreate(&pSECOutputPort->unloadedResource);
    if (ret != OMX_ErrorNone) {
        SEC_OSAL_SemaphoreTerminate(pSECOutputPort->loadedResource);
        pSECOutputPort->loadedResource = NULL;
        SEC_OSAL_Free(pSECOutputPort->bufferStateAllocate);
        pSECOutputPort->bufferStateAllocate = NULL;
        SEC_OSAL_Free(pSECOutputPort->bufferHeader);
        pSECOutputPort->bufferHeader = NULL;

        SEC_OSAL_SemaphoreTerminate(pSECInputPort->unloadedResource);
        pSECInputPort->unloadedResource = NULL;
        SEC_OSAL_SemaphoreTerminate(pSECInputPort->loadedResource);
        pSECInputPort->loadedResource = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferStateAllocate);
        pSECInputPort->bufferStateAllocate = NULL;
        SEC_OSAL_Free(pSECInputPort->bufferHeader);
        pSECInputPort->bufferHeader = NULL;
        SEC_OSAL_Free(pSECPort);
        pSECPort = NULL;
        goto EXIT;
    }

    INIT_SET_SIZE_VERSION(&pSECOutputPort->portDefinition, OMX_PARAM_PORTDEFINITIONTYPE);
    pSECOutputPort->portDefinition.nPortIndex = OUTPUT_PORT_INDEX;
    pSECOutputPort->portDefinition.eDir = OMX_DirOutput;
    pSECOutputPort->portDefinition.nBufferCountActual = 0;
    pSECOutputPort->portDefinition.nBufferCountMin = 0;
    pSECOutputPort->portDefinition.nBufferSize = 0;
    pSECOutputPort->portDefinition.bEnabled = OMX_FALSE;
    pSECOutputPort->portDefinition.bPopulated = OMX_FALSE;
    pSECOutputPort->portDefinition.eDomain = OMX_PortDomainMax;
    pSECOutputPort->portDefinition.bBuffersContiguous = OMX_FALSE;
    pSECOutputPort->portDefinition.nBufferAlignment = 0;
    pSECOutputPort->markType.hMarkTargetComponent = NULL;
    pSECOutputPort->markType.pMarkData = NULL;

    pSECComponent->checkTimeStamp.needSetStartTimeStamp = OMX_TRUE;
    pSECComponent->checkTimeStamp.needCheckStartTimeStamp = OMX_FALSE;
    pSECComponent->checkTimeStamp.startTimeStamp  = 0;
    pSECComponent->checkTimeStamp.nStartFlags = 0x0;

    pOMXComponent->EmptyThisBuffer = &SEC_OMX_EmptyThisBuffer;
    pOMXComponent->FillThisBuffer  = &SEC_OMX_FillThisBuffer;

    ret = OMX_ErrorNone;
EXIT:
    FunctionOut();

    return ret;
}
OMX_ERRORTYPE SEC_MFC_Mpeg4_Encode(OMX_COMPONENTTYPE *pOMXComponent, SEC_OMX_DATA *pInputData, SEC_OMX_DATA *pOutputData)
{
    OMX_ERRORTYPE              ret = OMX_ErrorNone;
    SEC_OMX_BASECOMPONENT     *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    SEC_MPEG4ENC_HANDLE       *pMpeg4Enc = (SEC_MPEG4ENC_HANDLE *)pSECComponent->hCodecHandle;
    OMX_HANDLETYPE             hMFCHandle = pMpeg4Enc->hMFCMpeg4Handle.hMFCHandle;
    SSBSIP_MFC_ENC_INPUT_INFO *pInputInfo = &(pMpeg4Enc->hMFCMpeg4Handle.inputInfo);
    SSBSIP_MFC_ENC_OUTPUT_INFO outputInfo;
    SEC_OMX_BASEPORT          *pSECPort = NULL;
    MFC_ENC_ADDR_INFO          addrInfo;
    OMX_U32                    oneFrameSize = pInputData->dataLen;
    OMX_S32                    returnCodec = 0;

    FunctionIn();

    if (pMpeg4Enc->hMFCMpeg4Handle.bConfiguredMFC == OMX_FALSE) {
        returnCodec = SsbSipMfcEncGetOutBuf(hMFCHandle, &outputInfo);
        if (returnCodec != MFC_RET_OK)
        {
            SEC_OSAL_Log(SEC_LOG_ERROR, "%s: SsbSipMfcEncGetOutBuf failed, ret:%d", __FUNCTION__, returnCodec);
            ret = OMX_ErrorUndefined;
            goto EXIT;
        }

        pOutputData->dataBuffer = outputInfo.StrmVirAddr;
        pOutputData->allocSize = outputInfo.headerSize;
        pOutputData->dataLen = outputInfo.headerSize;
        pOutputData->timeStamp = pInputData->timeStamp;
        pOutputData->nFlags |= OMX_BUFFERFLAG_CODECCONFIG;
        pOutputData->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;

        pMpeg4Enc->hMFCMpeg4Handle.bConfiguredMFC = OMX_TRUE;

        if (pOutputData->dataLen > 0) {
            ret = OMX_ErrorNone;
            goto EXIT;
        } else {
            ret = OMX_ErrorInputDataEncodeYet;
            goto EXIT;
        }
    }

    if ((pInputData->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) &&
        (pSECComponent->bUseFlagEOF == OMX_FALSE)) {
        pSECComponent->bUseFlagEOF = OMX_TRUE;
    }

    pSECComponent->timeStamp[pMpeg4Enc->hMFCMpeg4Handle.indexTimestamp] = pInputData->timeStamp;
    pSECComponent->nFlags[pMpeg4Enc->hMFCMpeg4Handle.indexTimestamp] = pInputData->nFlags;
    SsbSipMfcEncSetConfig(hMFCHandle, MFC_ENC_SETCONF_FRAME_TAG, &(pMpeg4Enc->hMFCMpeg4Handle.indexTimestamp));
    pMpeg4Enc->hMFCMpeg4Handle.indexTimestamp++;
    if (pMpeg4Enc->hMFCMpeg4Handle.indexTimestamp >= MAX_TIMESTAMP)
        pMpeg4Enc->hMFCMpeg4Handle.indexTimestamp = 0;

    if (oneFrameSize <= 0) {
        pOutputData->timeStamp = pInputData->timeStamp;
        pOutputData->nFlags = pInputData->nFlags;

        ret = OMX_ErrorNone;
        goto EXIT;
    }

    pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    if (pSECPort->portDefinition.format.video.eColorFormat == SEC_OMX_COLOR_FormatNV12PhysicalAddress) {
    /* input data from Real camera */
#define USE_FIMC_FRAME_BUFFER
#ifdef USE_FIMC_FRAME_BUFFER
        SEC_OSAL_Memcpy(&addrInfo.pAddrY, pInputData->dataBuffer, sizeof(addrInfo.pAddrY));
        SEC_OSAL_Memcpy(&addrInfo.pAddrC, pInputData->dataBuffer + sizeof(addrInfo.pAddrY), sizeof(addrInfo.pAddrC));
        pInputInfo->YPhyAddr = addrInfo.pAddrY;
        pInputInfo->CPhyAddr = addrInfo.pAddrC;
        returnCodec = SsbSipMfcEncSetInBuf(hMFCHandle, pInputInfo);
        if (returnCodec != MFC_RET_OK) {
            SEC_OSAL_Log(SEC_LOG_ERROR, "%s: SsbSipMfcEncSetInBuf failed, ret:%d", __FUNCTION__, returnCodec);
            ret = OMX_ErrorUndefined;
            goto EXIT;
        }
#else
        OMX_U32 width, height;

        width = pSECPort->portDefinition.format.video.nFrameWidth;
        height = pSECPort->portDefinition.format.video.nFrameHeight;

        SEC_OSAL_Memcpy(pInputInfo->YVirAddr, pInputData->dataBuffer, ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)));
        SEC_OSAL_Memcpy(pInputInfo->CVirAddr, pInputData->dataBuffer + ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)), ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height / 2)));
#endif
    }

    returnCodec = SsbSipMfcEncExe(hMFCHandle);
    if (returnCodec == MFC_RET_OK) {
        OMX_S32 indexTimestamp = 0;

        returnCodec = SsbSipMfcEncGetOutBuf(hMFCHandle, &outputInfo);

        if ((SsbSipMfcEncGetConfig(hMFCHandle, MFC_ENC_GETCONF_FRAME_TAG, &indexTimestamp) != MFC_RET_OK) ||
            (((indexTimestamp < 0) || (indexTimestamp > MAX_TIMESTAMP)))) {
            pOutputData->timeStamp = pInputData->timeStamp;
            pOutputData->nFlags = pInputData->nFlags;
        } else {
            pOutputData->timeStamp = pSECComponent->timeStamp[indexTimestamp];
            pOutputData->nFlags = pSECComponent->nFlags[indexTimestamp];
        }

        if (returnCodec == MFC_RET_OK) {
            /** Fill Output Buffer **/
            pOutputData->dataBuffer = outputInfo.StrmVirAddr;
            pOutputData->allocSize = outputInfo.dataSize;
            pOutputData->dataLen = outputInfo.dataSize;
            pOutputData->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
            if (outputInfo.frameType == MFC_FRAME_TYPE_I_FRAME)
                    pOutputData->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;

            ret = OMX_ErrorNone;
        } else {
            SEC_OSAL_Log(SEC_LOG_ERROR, "%s: SsbSipMfcEncGetOutBuf failed, ret:%d", __FUNCTION__, returnCodec);
            ret = OMX_ErrorUndefined;
        }
    } else {
        SEC_OSAL_Log(SEC_LOG_ERROR, "%s: SsbSipMfcEncExe failed, ret:%d", __FUNCTION__, returnCodec);
        ret = OMX_ErrorUndefined;
    }

EXIT:
    FunctionOut();

    return ret;
}
OSCL_EXPORT_REF OMX_ERRORTYPE SEC_OMX_ComponentInit(OMX_HANDLETYPE hComponent, OMX_STRING componentName)
{
    OMX_ERRORTYPE            ret = OMX_ErrorNone;
    OMX_COMPONENTTYPE       *pOMXComponent = NULL;
    SEC_OMX_BASECOMPONENT   *pSECComponent = NULL;
    SEC_OMX_BASEPORT        *pSECPort = NULL;
    SEC_MPEG4ENC_HANDLE     *pMpeg4Enc = NULL;
    OMX_S32                  codecType = -1;
    int i = 0;

    FunctionIn();

    if ((hComponent == NULL) || (componentName == NULL)) {
        ret = OMX_ErrorBadParameter;
        SEC_OSAL_Log(SEC_LOG_ERROR, "%s: parameters are null, ret: %X", __FUNCTION__, ret);
        goto EXIT;
    }
    if (SEC_OSAL_Strcmp(SEC_OMX_COMPOMENT_MPEG4_ENC, componentName) == 0) {
        codecType = CODEC_TYPE_MPEG4;
    } else if (SEC_OSAL_Strcmp(SEC_OMX_COMPOMENT_H263_ENC, componentName) == 0) {
        codecType = CODEC_TYPE_H263;
    } else {
        ret = OMX_ErrorBadParameter;
        SEC_OSAL_Log(SEC_LOG_ERROR, "%s: componentName(%s) error, ret: %X", __FUNCTION__, componentName, ret);
        goto EXIT;
    }

    pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    ret = SEC_OMX_VideoEncodeComponentInit(pOMXComponent);
    if (ret != OMX_ErrorNone) {
        SEC_OSAL_Log(SEC_LOG_ERROR, "%s: SEC_OMX_VideoDecodeComponentInit error, ret: %X", __FUNCTION__, ret);
        goto EXIT;
    }
    pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    pSECComponent->codecType = HW_VIDEO_CODEC;

    pSECComponent->componentName = (OMX_STRING)SEC_OSAL_Malloc(MAX_OMX_COMPONENT_NAME_SIZE);
    if (pSECComponent->componentName == NULL) {
        SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "%s: componentName alloc error, ret: %X", __FUNCTION__, ret);
        goto EXIT;
    }
    SEC_OSAL_Memset(pSECComponent->componentName, 0, MAX_OMX_COMPONENT_NAME_SIZE);

    pMpeg4Enc = SEC_OSAL_Malloc(sizeof(SEC_MPEG4ENC_HANDLE));
    if (pMpeg4Enc == NULL) {
        SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "%s: SEC_MPEG4ENC_HANDLE alloc error, ret: %X", __FUNCTION__, ret);
        goto EXIT;
    }
    SEC_OSAL_Memset(pMpeg4Enc, 0, sizeof(SEC_MPEG4ENC_HANDLE));
    pSECComponent->hCodecHandle = (OMX_HANDLETYPE)pMpeg4Enc;
    pMpeg4Enc->hMFCMpeg4Handle.codecType = codecType;

    if (codecType == CODEC_TYPE_MPEG4)
        SEC_OSAL_Strcpy(pSECComponent->componentName, SEC_OMX_COMPOMENT_MPEG4_ENC);
    else
        SEC_OSAL_Strcpy(pSECComponent->componentName, SEC_OMX_COMPOMENT_H263_ENC);

    /* Set componentVersion */
    pSECComponent->componentVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
    pSECComponent->componentVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
    pSECComponent->componentVersion.s.nRevision     = REVISION_NUMBER;
    pSECComponent->componentVersion.s.nStep         = STEP_NUMBER;
    /* Set specVersion */
    pSECComponent->specVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
    pSECComponent->specVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
    pSECComponent->specVersion.s.nRevision     = REVISION_NUMBER;
    pSECComponent->specVersion.s.nStep         = STEP_NUMBER;

    /* Android CapabilityFlags */
    pSECComponent->capabilityFlags.iIsOMXComponentMultiThreaded                   = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentSupportsExternalInputBufferAlloc  = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentSupportsExternalOutputBufferAlloc = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentSupportsMovableInputBuffers       = OMX_FALSE;
    pSECComponent->capabilityFlags.iOMXComponentSupportsPartialFrames             = OMX_FALSE;
    pSECComponent->capabilityFlags.iOMXComponentUsesNALStartCodes                 = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentCanHandleIncompleteFrames         = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentUsesFullAVCFrames                 = OMX_TRUE;

    /* Input port */
    pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    pSECPort->portDefinition.format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
    pSECPort->portDefinition.format.video.nFrameHeight= DEFAULT_FRAME_HEIGHT;
    pSECPort->portDefinition.format.video.nBitrate = 64000;
    pSECPort->portDefinition.format.video.xFramerate= (15 << 16);
    pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
    pSECPort->portDefinition.format.video.pNativeRender = 0;
    pSECPort->portDefinition.format.video.bFlagErrorConcealment = OMX_FALSE;
    SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
    SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "raw/video");
    pSECPort->portDefinition.format.video.eColorFormat = SEC_OMX_COLOR_FormatNV12PhysicalAddress;//OMX_COLOR_FormatYUV420SemiPlanar;
    pSECPort->portDefinition.nBufferSize = DEFAULT_VIDEO_INPUT_BUFFER_SIZE;
    pSECPort->portDefinition.bEnabled = OMX_TRUE;

    /* Output port */
    pSECPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
    pSECPort->portDefinition.format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
    pSECPort->portDefinition.format.video.nFrameHeight= DEFAULT_FRAME_HEIGHT;
    pSECPort->portDefinition.format.video.nBitrate = 64000;
    pSECPort->portDefinition.format.video.xFramerate= (15 << 16);
    if (codecType == CODEC_TYPE_MPEG4) {
        pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
        SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
        SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "video/mpeg4");
    } else {
        pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingH263;
        SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
        SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "video/h263");
    }
    pSECPort->portDefinition.format.video.pNativeRender = 0;
    pSECPort->portDefinition.format.video.bFlagErrorConcealment = OMX_FALSE;
    pSECPort->portDefinition.format.video.eColorFormat = OMX_COLOR_FormatUnused;
    pSECPort->portDefinition.nBufferSize = DEFAULT_VIDEO_OUTPUT_BUFFER_SIZE;
    pSECPort->portDefinition.bEnabled = OMX_TRUE;

    if (codecType == CODEC_TYPE_MPEG4) {
        for(i = 0; i < ALL_PORT_NUM; i++) {
            INIT_SET_SIZE_VERSION(&pMpeg4Enc->mpeg4Component[i], OMX_VIDEO_PARAM_MPEG4TYPE);
            pMpeg4Enc->mpeg4Component[i].nPortIndex = i;
            pMpeg4Enc->mpeg4Component[i].eProfile   = OMX_VIDEO_MPEG4ProfileSimple;
            pMpeg4Enc->mpeg4Component[i].eLevel     = OMX_VIDEO_MPEG4Level4;

            pMpeg4Enc->mpeg4Component[i].nPFrames = 10;
            pMpeg4Enc->mpeg4Component[i].nBFrames = 0;          /* No support for B frames */
            pMpeg4Enc->mpeg4Component[i].nMaxPacketSize = 256;  /* Default value */
            pMpeg4Enc->mpeg4Component[i].nAllowedPictureTypes =  OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
            pMpeg4Enc->mpeg4Component[i].bGov = OMX_FALSE;

        }
    } else {
        for(i = 0; i < ALL_PORT_NUM; i++) {
            INIT_SET_SIZE_VERSION(&pMpeg4Enc->h263Component[i], OMX_VIDEO_PARAM_H263TYPE);
            pMpeg4Enc->h263Component[i].nPortIndex = i;
            pMpeg4Enc->h263Component[i].eProfile   = OMX_VIDEO_H263ProfileBaseline;
            pMpeg4Enc->h263Component[i].eLevel     = OMX_VIDEO_H263Level45;

            pMpeg4Enc->h263Component[i].nPFrames = 20;
            pMpeg4Enc->h263Component[i].nBFrames = 0;          /* No support for B frames */
            pMpeg4Enc->h263Component[i].bPLUSPTYPEAllowed = OMX_FALSE;
            pMpeg4Enc->h263Component[i].nAllowedPictureTypes = OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP;
            pMpeg4Enc->h263Component[i].bForceRoundingTypeToZero = OMX_TRUE;
            pMpeg4Enc->h263Component[i].nPictureHeaderRepetition = 0;
            pMpeg4Enc->h263Component[i].nGOBHeaderInterval = 0;
        }
    }

    pOMXComponent->GetParameter      = &SEC_MFC_Mpeg4Enc_GetParameter;
    pOMXComponent->SetParameter      = &SEC_MFC_Mpeg4Enc_SetParameter;
    pOMXComponent->SetConfig         = &SEC_MFC_Mpeg4Enc_SetConfig;
    pOMXComponent->ComponentRoleEnum = &SEC_MFC_Mpeg4Enc_ComponentRoleEnum;
    pOMXComponent->ComponentDeInit   = &SEC_OMX_ComponentDeinit;

    pSECComponent->sec_mfc_componentInit      = &SEC_MFC_Mpeg4Enc_Init;
    pSECComponent->sec_mfc_componentTerminate = &SEC_MFC_Mpeg4Enc_Terminate;
    pSECComponent->sec_mfc_bufferProcess      = &SEC_MFC_Mpeg4Enc_bufferProcess;
    pSECComponent->sec_checkInputFrame        = NULL;

    pSECComponent->currentState = OMX_StateLoaded;

    ret = OMX_ErrorNone;

EXIT:
    FunctionOut();

    return ret;
}
OMX_ERRORTYPE SEC_MFC_H264_Encode(OMX_COMPONENTTYPE *pOMXComponent, SEC_OMX_DATA *pInputData, SEC_OMX_DATA *pOutputData)
{
    OMX_ERRORTYPE              ret = OMX_ErrorNone;
    SEC_OMX_BASECOMPONENT     *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    SEC_H264ENC_HANDLE        *pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    SSBSIP_MFC_ENC_INPUT_INFO *pInputInfo = &pH264Enc->hMFCH264Handle.inputInfo;
    SSBSIP_MFC_ENC_OUTPUT_INFO outputInfo;
    SEC_OMX_BASEPORT          *pSECPort = NULL;
    MFC_ENC_ADDR_INFO          addrInfo;
    OMX_U32                    oneFrameSize = pInputData->dataLen;
    OMX_S32                    returnCodec = 0;

    FunctionIn();

    if (pH264Enc->hMFCH264Handle.bConfiguredMFC == OMX_FALSE) {
        returnCodec = SsbSipMfcEncGetOutBuf(pH264Enc->hMFCH264Handle.hMFCHandle, &outputInfo);
        if (returnCodec != MFC_RET_OK)
        {
            SEC_OSAL_Log(SEC_LOG_TRACE, "%s - SsbSipMfcEncGetOutBuf Failed\n", __func__);
            ret = OMX_ErrorUndefined;
            goto EXIT;
        } else {
            unsigned char *p = NULL;
            int iSpsSize = 0;
            int iPpsSize = 0;

            p = FindDelimiter((OMX_U8 *)outputInfo.StrmVirAddr + 4, outputInfo.headerSize - 4);

            iSpsSize = (unsigned int)p - (unsigned int)outputInfo.StrmVirAddr;
            pH264Enc->hMFCH264Handle.headerData.pHeaderSPS = (OMX_PTR)outputInfo.StrmVirAddr;
            pH264Enc->hMFCH264Handle.headerData.SPSLen = iSpsSize;

            iPpsSize = outputInfo.headerSize - iSpsSize;
            pH264Enc->hMFCH264Handle.headerData.pHeaderPPS = outputInfo.StrmVirAddr + iSpsSize;
            pH264Enc->hMFCH264Handle.headerData.PPSLen = iPpsSize;
        }

        /* SEC_OSAL_Memcpy((void*)(pOutputData->dataBuffer), (const void*)(outputInfo.StrmVirAddr), outputInfo.headerSize); */
        pOutputData->dataBuffer = outputInfo.StrmVirAddr;
        pOutputData->allocSize = outputInfo.headerSize;
        pOutputData->dataLen = outputInfo.headerSize;
        pOutputData->timeStamp = pInputData->timeStamp;
        pOutputData->nFlags |= OMX_BUFFERFLAG_CODECCONFIG;
        pOutputData->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;

        pH264Enc->hMFCH264Handle.bConfiguredMFC = OMX_TRUE;

        ret = OMX_ErrorNone;
        goto EXIT;
    }

    if ((pInputData->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) &&
        (pSECComponent->bUseFlagEOF == OMX_FALSE)) {
        pSECComponent->bUseFlagEOF = OMX_TRUE;
    }

    pSECComponent->timeStamp[pH264Enc->hMFCH264Handle.indexTimestamp] = pInputData->timeStamp;
    pSECComponent->nFlags[pH264Enc->hMFCH264Handle.indexTimestamp] = pInputData->nFlags;
    SsbSipMfcEncSetConfig(pH264Enc->hMFCH264Handle.hMFCHandle, MFC_ENC_SETCONF_FRAME_TAG, &(pH264Enc->hMFCH264Handle.indexTimestamp));
    pH264Enc->hMFCH264Handle.indexTimestamp++;
    if (pH264Enc->hMFCH264Handle.indexTimestamp >= MAX_TIMESTAMP)
        pH264Enc->hMFCH264Handle.indexTimestamp = 0;

    if (oneFrameSize <= 0) {
        pOutputData->timeStamp = pInputData->timeStamp;
        pOutputData->nFlags = pInputData->nFlags;

        ret = OMX_ErrorNone;
        goto EXIT;
    }

    pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    if (pSECPort->portDefinition.format.video.eColorFormat == SEC_OMX_COLOR_FormatNV12PhysicalAddress) {
#define USE_FIMC_FRAME_BUFFER
#ifdef USE_FIMC_FRAME_BUFFER
        SEC_OSAL_Memcpy(&addrInfo.pAddrY, pInputData->dataBuffer, sizeof(addrInfo.pAddrY));
        SEC_OSAL_Memcpy(&addrInfo.pAddrC, pInputData->dataBuffer + sizeof(addrInfo.pAddrY), sizeof(addrInfo.pAddrC));
        pInputInfo->YPhyAddr = addrInfo.pAddrY;
        pInputInfo->CPhyAddr = addrInfo.pAddrC;
        ret = SsbSipMfcEncSetInBuf(pH264Enc->hMFCH264Handle.hMFCHandle, pInputInfo);
        if (ret != MFC_RET_OK) {
            SEC_OSAL_Log(SEC_LOG_TRACE, "Error : SsbSipMfcEncSetInBuf() \n");
            ret = OMX_ErrorUndefined;
            goto EXIT;
        }
#else
        OMX_U32 width, height;

        width = pSECPort->portDefinition.format.video.nFrameWidth;
        height = pSECPort->portDefinition.format.video.nFrameHeight;

        SEC_OSAL_Memcpy(pInputInfo->YVirAddr, pInputData->dataBuffer, ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)));
        SEC_OSAL_Memcpy(pInputInfo->CVirAddr, pInputData->dataBuffer + ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)), ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height / 2)));
#endif
    }

    returnCodec = SsbSipMfcEncExe(pH264Enc->hMFCH264Handle.hMFCHandle);
    if (returnCodec == MFC_RET_OK) {
        OMX_S32 indexTimestamp = 0;

        returnCodec = SsbSipMfcEncGetOutBuf(pH264Enc->hMFCH264Handle.hMFCHandle, &outputInfo);
        if ((SsbSipMfcEncGetConfig(pH264Enc->hMFCH264Handle.hMFCHandle, MFC_ENC_GETCONF_FRAME_TAG, &indexTimestamp) != MFC_RET_OK) ||
            (((indexTimestamp < 0) || (indexTimestamp > MAX_TIMESTAMP)))){
            pOutputData->timeStamp = pInputData->timeStamp;
            pOutputData->nFlags = pInputData->nFlags;
        } else {
            pOutputData->timeStamp = pSECComponent->timeStamp[indexTimestamp];
            pOutputData->nFlags = pSECComponent->nFlags[indexTimestamp];
        }

        if (returnCodec == MFC_RET_OK) {
            /** Fill Output Buffer **/
            pOutputData->dataBuffer = outputInfo.StrmVirAddr;
            pOutputData->allocSize = outputInfo.dataSize;
            pOutputData->dataLen = outputInfo.dataSize;
            pOutputData->usedDataLen = 0;

            pOutputData->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
            if (outputInfo.frameType == MFC_FRAME_TYPE_I_FRAME)
                    pOutputData->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;

            SEC_OSAL_Log(SEC_LOG_TRACE, "MFC Encode OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");

            ret = OMX_ErrorNone;
        }
    }

    if (returnCodec != MFC_RET_OK) {
        SEC_OSAL_Log(SEC_LOG_ERROR, "In %s : SsbSipMfcEncExe OR SsbSipMfcEncGetOutBuf Failed!!!\n", __func__);
        ret = OMX_ErrorUndefined;
    }

EXIT:
    FunctionOut();

    return ret;
}
OMX_ERRORTYPE SEC_OMX_ComponentStateSet(OMX_COMPONENTTYPE *pOMXComponent, OMX_U32 messageParam)
{
    OMX_ERRORTYPE          ret = OMX_ErrorNone;
    SEC_OMX_BASECOMPONENT *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    SEC_OMX_MESSAGE       *message;
    OMX_STATETYPE          destState = messageParam;
    OMX_STATETYPE          currentState = pSECComponent->currentState;
    SEC_OMX_BASEPORT      *pSECPort = NULL;
    OMX_S32                countValue = 0;
    int                   i = 0, j = 0;

    FunctionIn();

    /* check parameters */
    if (currentState == destState) {
         ret = OMX_ErrorSameState;
            goto EXIT;
    }
    if (currentState == OMX_StateInvalid) {
        ret = OMX_ErrorInvalidState;
        goto EXIT;
    }

    if ((currentState == OMX_StateLoaded) && (destState == OMX_StateIdle)) {
        ret = SEC_OMX_Get_Resource(pOMXComponent);
        if (ret != OMX_ErrorNone) {
            goto EXIT;
        }
    }
    if (((currentState == OMX_StateIdle) && (destState == OMX_StateLoaded))       ||
        ((currentState == OMX_StateIdle) && (destState == OMX_StateInvalid))      ||
        ((currentState == OMX_StateExecuting) && (destState == OMX_StateInvalid)) ||
        ((currentState == OMX_StatePause) && (destState == OMX_StateInvalid))) {
        SEC_OMX_Release_Resource(pOMXComponent);
    }

    SEC_OSAL_Log(SEC_LOG_TRACE, "destState: %d", destState);

    switch (destState) {
    case OMX_StateInvalid:
        switch (currentState) {
        case OMX_StateIdle:
        case OMX_StateExecuting:
        case OMX_StatePause:
        case OMX_StateLoaded:
        case OMX_StateWaitForResources:
            pSECComponent->currentState = OMX_StateInvalid;
            if (pSECComponent->hBufferProcess) {
                pSECComponent->bExitBufferProcessThread = OMX_TRUE;

                for (i = 0; i < ALL_PORT_NUM; i++) {
                    SEC_OSAL_Get_SemaphoreCount(pSECComponent->pSECPort[i].bufferSemID, &countValue);
                    if (countValue == 0)
                        SEC_OSAL_SemaphorePost(pSECComponent->pSECPort[i].bufferSemID);
                }

                SEC_OSAL_SignalSet(pSECComponent->pauseEvent);
                SEC_OSAL_ThreadTerminate(pSECComponent->hBufferProcess);
                pSECComponent->hBufferProcess = NULL;

                for (i = 0; i < ALL_PORT_NUM; i++) {
                    SEC_OSAL_MutexTerminate(pSECComponent->secDataBuffer[i].bufferMutex);
                    pSECComponent->secDataBuffer[i].bufferMutex = NULL;
                }

                SEC_OSAL_SignalTerminate(pSECComponent->pauseEvent);
                for (i = 0; i < ALL_PORT_NUM; i++) {
                    SEC_OSAL_SemaphoreTerminate(pSECComponent->pSECPort[i].bufferSemID);
                    pSECComponent->pSECPort[i].bufferSemID = NULL;
                }
            }
            if (pSECComponent->sec_mfc_componentTerminate != NULL)
                pSECComponent->sec_mfc_componentTerminate(pOMXComponent);
            break;
        }
        ret = OMX_ErrorInvalidState;
        break;
    case OMX_StateLoaded:
        switch (currentState) {
        case OMX_StateIdle:
            pSECComponent->bExitBufferProcessThread = OMX_TRUE;

            for (i = 0; i < ALL_PORT_NUM; i++) {
                SEC_OSAL_Get_SemaphoreCount(pSECComponent->pSECPort[i].bufferSemID, &countValue);
                if (countValue == 0)
                    SEC_OSAL_SemaphorePost(pSECComponent->pSECPort[i].bufferSemID);
            }

            SEC_OSAL_SignalSet(pSECComponent->pauseEvent);
            SEC_OSAL_ThreadTerminate(pSECComponent->hBufferProcess);
            pSECComponent->hBufferProcess = NULL;

            for (i = 0; i < ALL_PORT_NUM; i++) {
                SEC_OSAL_MutexTerminate(pSECComponent->secDataBuffer[i].bufferMutex);
                pSECComponent->secDataBuffer[i].bufferMutex = NULL;
            }

            SEC_OSAL_SignalTerminate(pSECComponent->pauseEvent);
            for (i = 0; i < ALL_PORT_NUM; i++) {
                SEC_OSAL_SemaphoreTerminate(pSECComponent->pSECPort[i].bufferSemID);
                pSECComponent->pSECPort[i].bufferSemID = NULL;
            }

            pSECComponent->sec_mfc_componentTerminate(pOMXComponent);

            for (i = 0; i < (pSECComponent->portParam.nPorts); i++) {
                pSECPort = (pSECComponent->pSECPort + i);
                if (CHECK_PORT_TUNNELED(pSECPort) && CHECK_PORT_BUFFER_SUPPLIER(pSECPort)) {
                    while (SEC_OSAL_GetElemNum(&pSECPort->bufferQ) > 0) {
                        message = (SEC_OMX_MESSAGE*)SEC_OSAL_Dequeue(&pSECPort->bufferQ);
                        if (message != NULL)
                            SEC_OSAL_Free(message);
                    }
                    ret = pSECComponent->sec_FreeTunnelBuffer(pSECComponent, i);
                    if (OMX_ErrorNone != ret) {
                        goto EXIT;
                    }
                } else {
                    if (CHECK_PORT_ENABLED(pSECPort)) {
                        SEC_OSAL_SemaphoreWait(pSECPort->unloadedResource);
                        pSECPort->portDefinition.bPopulated = OMX_FALSE;
                    }
                }
            }
            pSECComponent->currentState = OMX_StateLoaded;
            break;
        case OMX_StateWaitForResources:
            ret = SEC_OMX_Out_WaitForResource(pOMXComponent);
            pSECComponent->currentState = OMX_StateLoaded;
            break;
        case OMX_StateExecuting:
        case OMX_StatePause:
        default:
            ret = OMX_ErrorIncorrectStateTransition;
            break;
        }
        break;
    case OMX_StateIdle:
        switch (currentState) {
        case OMX_StateLoaded:
            for (i = 0; i < pSECComponent->portParam.nPorts; i++) {
                pSECPort = (pSECComponent->pSECPort + i);
                if (CHECK_PORT_TUNNELED(pSECPort) && CHECK_PORT_BUFFER_SUPPLIER(pSECPort)) {
                    if (CHECK_PORT_ENABLED(pSECPort)) {
                        ret = pSECComponent->sec_AllocateTunnelBuffer(pSECPort, i);
                        if (ret!=OMX_ErrorNone)
                            goto EXIT;
                    }
                } else {
                    if (CHECK_PORT_ENABLED(pSECPort)) {
                        SEC_OSAL_SemaphoreWait(pSECComponent->pSECPort[i].loadedResource);
                        pSECPort->portDefinition.bPopulated = OMX_TRUE;
                    }
                }
            }
            ret = pSECComponent->sec_mfc_componentInit(pOMXComponent);
            if (ret != OMX_ErrorNone) {
                /*
                 * if (CHECK_PORT_TUNNELED == OMX_TRUE) thenTunnel Buffer Free
                 */
                goto EXIT;
            }
            pSECComponent->bExitBufferProcessThread = OMX_FALSE;
            SEC_OSAL_SignalCreate(&pSECComponent->pauseEvent);
            for (i = 0; i < ALL_PORT_NUM; i++) {
                SEC_OSAL_SemaphoreCreate(&pSECComponent->pSECPort[i].bufferSemID);
            }
            for (i = 0; i < ALL_PORT_NUM; i++) {
                SEC_OSAL_MutexCreate(&pSECComponent->secDataBuffer[i].bufferMutex);
            }
            ret = SEC_OSAL_ThreadCreate(&pSECComponent->hBufferProcess,
                             SEC_OMX_BufferProcessThread,
                             pOMXComponent);
            if (ret != OMX_ErrorNone) {
                /*
                 * if (CHECK_PORT_TUNNELED == OMX_TRUE) thenTunnel Buffer Free
                 */

                SEC_OSAL_SignalTerminate(pSECComponent->pauseEvent);
                for (i = 0; i < ALL_PORT_NUM; i++) {
                    SEC_OSAL_MutexTerminate(pSECComponent->secDataBuffer[i].bufferMutex);
                    pSECComponent->secDataBuffer[i].bufferMutex = NULL;
                }
                for (i = 0; i < ALL_PORT_NUM; i++) {
                    SEC_OSAL_SemaphoreTerminate(pSECComponent->pSECPort[i].bufferSemID);
                    pSECComponent->pSECPort[i].bufferSemID = NULL;
                }

                ret = OMX_ErrorInsufficientResources;
                goto EXIT;
            }
            pSECComponent->currentState = OMX_StateIdle;
            break;
        case OMX_StateExecuting:
        case OMX_StatePause:
            SEC_OMX_BufferFlushProcessNoEvent(pOMXComponent, ALL_PORT_INDEX);
            pSECComponent->currentState = OMX_StateIdle;
            break;
        case OMX_StateWaitForResources:
            pSECComponent->currentState = OMX_StateIdle;
            break;
        }
        break;
    case OMX_StateExecuting:
        switch (currentState) {
        case OMX_StateLoaded:
            ret = OMX_ErrorIncorrectStateTransition;
            break;
        case OMX_StateIdle:
            for (i = 0; i < pSECComponent->portParam.nPorts; i++) {
                pSECPort = &pSECComponent->pSECPort[i];
                if (CHECK_PORT_TUNNELED(pSECPort) && CHECK_PORT_BUFFER_SUPPLIER(pSECPort) && CHECK_PORT_ENABLED(pSECPort)) {
                    for (j = 0; j < pSECPort->tunnelBufferNum; j++) {
                        SEC_OSAL_SemaphorePost(pSECComponent->pSECPort[i].bufferSemID);
                    }
                }
            }

            pSECComponent->transientState = SEC_OMX_TransStateMax;
            pSECComponent->currentState = OMX_StateExecuting;
            SEC_OSAL_SignalSet(pSECComponent->pauseEvent);
            break;
        case OMX_StatePause:
            for (i = 0; i < pSECComponent->portParam.nPorts; i++) {
                pSECPort = &pSECComponent->pSECPort[i];
                if (CHECK_PORT_TUNNELED(pSECPort) && CHECK_PORT_BUFFER_SUPPLIER(pSECPort) && CHECK_PORT_ENABLED(pSECPort)) {
                    OMX_U32 semaValue = 0, cnt = 0;
                    SEC_OSAL_Get_SemaphoreCount(pSECComponent->pSECPort[i].bufferSemID, &semaValue);
                    if (SEC_OSAL_GetElemNum(&pSECPort->bufferQ) > semaValue) {
                        cnt = SEC_OSAL_GetElemNum(&pSECPort->bufferQ) - semaValue;
                        for (j = 0; j < cnt; j++) {
                            SEC_OSAL_SemaphorePost(pSECComponent->pSECPort[i].bufferSemID);
                        }
                    }
                }
            }

            pSECComponent->currentState = OMX_StateExecuting;
            SEC_OSAL_SignalSet(pSECComponent->pauseEvent);
            break;
        case OMX_StateWaitForResources:
            ret = OMX_ErrorIncorrectStateTransition;
            break;
        }
        break;
    case OMX_StatePause:
        switch (currentState) {
        case OMX_StateLoaded:
            ret = OMX_ErrorIncorrectStateTransition;
            break;
        case OMX_StateIdle:
            pSECComponent->currentState = OMX_StatePause;
            break;
        case OMX_StateExecuting:
            pSECComponent->currentState = OMX_StatePause;
            break;
        case OMX_StateWaitForResources:
            ret = OMX_ErrorIncorrectStateTransition;
            break;
        }
        break;
    case OMX_StateWaitForResources:
        switch (currentState) {
        case OMX_StateLoaded:
            ret = SEC_OMX_In_WaitForResource(pOMXComponent);
            pSECComponent->currentState = OMX_StateWaitForResources;
            break;
        case OMX_StateIdle:
        case OMX_StateExecuting:
        case OMX_StatePause:
            ret = OMX_ErrorIncorrectStateTransition;
            break;
        }
        break;
    }

EXIT:
    if (ret == OMX_ErrorNone) {
        if (pSECComponent->pCallbacks != NULL) {
            pSECComponent->pCallbacks->EventHandler((OMX_HANDLETYPE)pOMXComponent,
            pSECComponent->callbackData,
            OMX_EventCmdComplete, OMX_CommandStateSet,
            destState, NULL);
        }
    } else {
        if (pSECComponent->pCallbacks != NULL) {
            pSECComponent->pCallbacks->EventHandler((OMX_HANDLETYPE)pOMXComponent,
            pSECComponent->callbackData,
            OMX_EventError, ret, 0, NULL);
        }
    }
    FunctionOut();

    return ret;
}
OSCL_EXPORT_REF OMX_ERRORTYPE SEC_OMX_ComponentInit(OMX_HANDLETYPE hComponent, OMX_STRING componentName)
{
    OMX_ERRORTYPE            ret = OMX_ErrorNone;
    OMX_COMPONENTTYPE       *pOMXComponent = NULL;
    SEC_OMX_BASECOMPONENT   *pSECComponent = NULL;
    SEC_OMX_BASEPORT        *pSECPort = NULL;
    SEC_H264ENC_HANDLE      *pH264Enc = NULL;
    int i = 0;

    FunctionIn();

    if ((hComponent == NULL) || (componentName == NULL)) {
        ret = OMX_ErrorBadParameter;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, Line:%d", __LINE__);
        goto EXIT;
    }
    if (SEC_OSAL_Strcmp(SEC_OMX_COMPOMENT_H264_ENC, componentName) != 0) {
        ret = OMX_ErrorBadParameter;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, componentName:%s, Line:%d", componentName, __LINE__);
        goto EXIT;
    }

    pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    ret = SEC_OMX_VideoEncodeComponentInit(pOMXComponent);
    if (ret != OMX_ErrorNone) {
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_Error, Line:%d", __LINE__);
        goto EXIT;
    }
    pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    pSECComponent->codecType = HW_VIDEO_CODEC;

    pSECComponent->componentName = (OMX_STRING)SEC_OSAL_Malloc(MAX_OMX_COMPONENT_NAME_SIZE);
    if (pSECComponent->componentName == NULL) {
        SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }
    SEC_OSAL_Memset(pSECComponent->componentName, 0, MAX_OMX_COMPONENT_NAME_SIZE);

    pH264Enc = SEC_OSAL_Malloc(sizeof(SEC_H264ENC_HANDLE));
    if (pH264Enc == NULL) {
        SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
        ret = OMX_ErrorInsufficientResources;
        SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
        goto EXIT;
    }
    SEC_OSAL_Memset(pH264Enc, 0, sizeof(SEC_H264ENC_HANDLE));
    pSECComponent->hCodecHandle = (OMX_HANDLETYPE)pH264Enc;

    SEC_OSAL_Strcpy(pSECComponent->componentName, SEC_OMX_COMPOMENT_H264_ENC);
    /* Set componentVersion */
    pSECComponent->componentVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
    pSECComponent->componentVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
    pSECComponent->componentVersion.s.nRevision     = REVISION_NUMBER;
    pSECComponent->componentVersion.s.nStep         = STEP_NUMBER;
    /* Set specVersion */
    pSECComponent->specVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
    pSECComponent->specVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
    pSECComponent->specVersion.s.nRevision     = REVISION_NUMBER;
    pSECComponent->specVersion.s.nStep         = STEP_NUMBER;

    /* Android CapabilityFlags */
    pSECComponent->capabilityFlags.iIsOMXComponentMultiThreaded                   = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentSupportsExternalInputBufferAlloc  = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentSupportsExternalOutputBufferAlloc = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentSupportsMovableInputBuffers       = OMX_FALSE;
    pSECComponent->capabilityFlags.iOMXComponentSupportsPartialFrames             = OMX_FALSE;
    pSECComponent->capabilityFlags.iOMXComponentUsesNALStartCodes                 = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentCanHandleIncompleteFrames         = OMX_TRUE;
    pSECComponent->capabilityFlags.iOMXComponentUsesFullAVCFrames                 = OMX_TRUE;

    /* Input port */
    pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    pSECPort->portDefinition.format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
    pSECPort->portDefinition.format.video.nFrameHeight= DEFAULT_FRAME_HEIGHT;
    pSECPort->portDefinition.format.video.nStride = 0; /*DEFAULT_FRAME_WIDTH;*/
    pSECPort->portDefinition.nBufferSize = DEFAULT_VIDEO_INPUT_BUFFER_SIZE;
    pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
    SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
    SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "raw/video");
    pSECPort->portDefinition.format.video.eColorFormat = SEC_OMX_COLOR_FormatNV12PhysicalAddress;
    pSECPort->portDefinition.bEnabled = OMX_TRUE;

    /* Output port */
    pSECPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
    pSECPort->portDefinition.format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
    pSECPort->portDefinition.format.video.nFrameHeight= DEFAULT_FRAME_HEIGHT;
    pSECPort->portDefinition.format.video.nStride = 0; /*DEFAULT_FRAME_WIDTH;*/
    pSECPort->portDefinition.nBufferSize = DEFAULT_VIDEO_OUTPUT_BUFFER_SIZE;
    pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
    SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
    SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "video/avc");
    pSECPort->portDefinition.format.video.eColorFormat = OMX_COLOR_FormatUnused;
    pSECPort->portDefinition.bEnabled = OMX_TRUE;

    for(i = 0; i < ALL_PORT_NUM; i++) {
        INIT_SET_SIZE_VERSION(&pH264Enc->AVCComponent[i], OMX_VIDEO_PARAM_AVCTYPE);
        pH264Enc->AVCComponent[i].nPortIndex = i;
        pH264Enc->AVCComponent[i].eProfile   = OMX_VIDEO_AVCProfileBaseline;
        pH264Enc->AVCComponent[i].eLevel     = OMX_VIDEO_AVCLevel31;
    }

    pOMXComponent->GetParameter      = &SEC_MFC_H264Enc_GetParameter;
    pOMXComponent->SetParameter      = &SEC_MFC_H264Enc_SetParameter;
    pOMXComponent->SetConfig         = &SEC_MFC_H264Enc_SetConfig;
    pOMXComponent->ComponentRoleEnum = &SEC_MFC_H264Enc_ComponentRoleEnum;
    pOMXComponent->ComponentDeInit   = &SEC_OMX_ComponentDeinit;

    pSECComponent->sec_mfc_componentInit      = &SEC_MFC_H264Enc_Init;
    pSECComponent->sec_mfc_componentTerminate = &SEC_MFC_H264Enc_Terminate;
    pSECComponent->sec_mfc_bufferProcess      = &SEC_MFC_H264Enc_bufferProcess;
    pSECComponent->sec_checkInputFrame        = NULL;

    pSECComponent->currentState = OMX_StateLoaded;

    ret = OMX_ErrorNone;

EXIT:
    FunctionOut();

    return ret;
}
OMX_ERRORTYPE SEC_OMX_SendCommand(
    OMX_IN OMX_HANDLETYPE  hComponent,
    OMX_IN OMX_COMMANDTYPE Cmd,
    OMX_IN OMX_U32         nParam,
    OMX_IN OMX_PTR         pCmdData)
{
    OMX_ERRORTYPE          ret = OMX_ErrorNone;
    OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    SEC_OMX_MESSAGE       *message = NULL;

    FunctionIn();

    if (hComponent == NULL) {
        ret = OMX_ErrorBadParameter;
        goto EXIT;
    }
    pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    if (ret != OMX_ErrorNone) {
        goto EXIT;
    }

    if (pOMXComponent->pComponentPrivate == NULL) {
        ret = OMX_ErrorBadParameter;
        goto EXIT;
    }
    pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;

    if (pSECComponent->currentState == OMX_StateInvalid) {
        ret = OMX_ErrorInvalidState;
        goto EXIT;
    }

    switch (Cmd) {
    case OMX_CommandStateSet :
        SEC_OSAL_Log(SEC_LOG_TRACE, "Command: OMX_CommandStateSet");
        SEC_StateSet(pSECComponent, nParam);
        break;
    case OMX_CommandFlush :
        SEC_OSAL_Log(SEC_LOG_TRACE, "Command: OMX_CommandFlush");
        ret = SEC_SetPortFlush(pSECComponent, nParam);
        if (ret != OMX_ErrorNone)
            goto EXIT;
        break;
    case OMX_CommandPortDisable :
        SEC_OSAL_Log(SEC_LOG_TRACE, "Command: OMX_CommandPortDisable");
        ret = SEC_SetPortDisable(pSECComponent, nParam);
        if (ret != OMX_ErrorNone)
            goto EXIT;
        break;
    case OMX_CommandPortEnable :
        SEC_OSAL_Log(SEC_LOG_TRACE, "Command: OMX_CommandPortEnable");
        ret = SEC_SetPortEnable(pSECComponent, nParam);
        if (ret != OMX_ErrorNone)
            goto EXIT;
        break;
    case OMX_CommandMarkBuffer :
        SEC_OSAL_Log(SEC_LOG_TRACE, "Command: OMX_CommandMarkBuffer");
        ret = SEC_SetMarkBuffer(pSECComponent, nParam);
        if (ret != OMX_ErrorNone)
            goto EXIT;
        break;
/*
    case SEC_CommandFillBuffer :
    case SEC_CommandEmptyBuffer :
    case SEC_CommandDeInit :
*/
    default:
        break;
    }

    ret = SEC_OMX_CommandQueue(pSECComponent, Cmd, nParam, pCmdData);

EXIT:
    FunctionOut();

    return ret;
}
OMX_ERRORTYPE SEC_OMX_BufferFlushProcess(OMX_COMPONENTTYPE *pOMXComponent, OMX_S32 nPortIndex)
{
    OMX_ERRORTYPE          ret = OMX_ErrorNone;
    SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    SEC_OMX_BASEPORT      *pSECPort = NULL;
    OMX_S32                portIndex = 0;
    OMX_U32                i = 0, cnt = 0;
    SEC_OMX_DATABUFFER    *flushBuffer = NULL;

    FunctionIn();

    if (pOMXComponent == NULL) {
        ret = OMX_ErrorBadParameter;
        goto EXIT;
    }
    ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    if (ret != OMX_ErrorNone) {
        goto EXIT;
    }

    if (pOMXComponent->pComponentPrivate == NULL) {
        ret = OMX_ErrorBadParameter;
        goto EXIT;
    }
    pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;

    cnt = (nPortIndex == ALL_PORT_INDEX ) ? ALL_PORT_NUM : 1;

    for (i = 0; i < cnt; i++) {
        if (nPortIndex == ALL_PORT_INDEX)
            portIndex = i;
        else
            portIndex = nPortIndex;

        SEC_OSAL_SignalSet(pSECComponent->pauseEvent);

        flushBuffer = &pSECComponent->secDataBuffer[portIndex];

        SEC_OSAL_MutexLock(flushBuffer->bufferMutex);
        ret = SEC_OMX_FlushPort(pOMXComponent, portIndex);
        SEC_OSAL_MutexUnlock(flushBuffer->bufferMutex);

        pSECComponent->pSECPort[portIndex].bIsPortFlushed = OMX_FALSE;

        if (ret == OMX_ErrorNone) {
            SEC_OSAL_Log(SEC_LOG_TRACE,"OMX_CommandFlush EventCmdComplete");
            pSECComponent->pCallbacks->EventHandler((OMX_HANDLETYPE)pOMXComponent,
                            pSECComponent->callbackData,
                            OMX_EventCmdComplete,
                            OMX_CommandFlush, portIndex, NULL);
        }

        if (portIndex == INPUT_PORT_INDEX) {
            pSECComponent->checkTimeStamp.needSetStartTimeStamp = OMX_TRUE;
            pSECComponent->checkTimeStamp.needCheckStartTimeStamp = OMX_FALSE;
            SEC_OSAL_Memset(pSECComponent->timeStamp, -19771003, sizeof(OMX_TICKS) * MAX_TIMESTAMP);
            SEC_OSAL_Memset(pSECComponent->nFlags, 0, sizeof(OMX_U32) * MAX_FLAGS);
            pSECComponent->getAllDelayBuffer = OMX_FALSE;
            pSECComponent->bSaveFlagEOS = OMX_FALSE;
            pSECComponent->reInputData = OMX_FALSE;
        } else if (portIndex == OUTPUT_PORT_INDEX) {
            pSECComponent->remainOutputData = OMX_FALSE;
        }
    }

EXIT:
    if (ret != OMX_ErrorNone) {
            pSECComponent->pCallbacks->EventHandler(pOMXComponent,
                            pSECComponent->callbackData,
                            OMX_EventError,
                            ret, 0, NULL);
    }

    FunctionOut();

    return ret;
}