OMX_ERRORTYPE OMX_ComponentInit(OMX_HANDLETYPE hComponent)
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	OMX_COMPONENTTYPE *pHandle = NULL;
	PROXY_COMPONENT_PRIVATE *pComponentPrivate;
	pHandle = (OMX_COMPONENTTYPE *) hComponent;

	DOMX_DEBUG("___INSISDE VIDEO DECODER PROXY WRAPPER__\n");

	pHandle->pComponentPrivate =
	    (PROXY_COMPONENT_PRIVATE *)
	    TIMM_OSAL_Malloc(sizeof(PROXY_COMPONENT_PRIVATE), TIMM_OSAL_TRUE,
	    0, TIMMOSAL_MEM_SEGMENT_INT);

	pComponentPrivate =
	    (PROXY_COMPONENT_PRIVATE *) pHandle->pComponentPrivate;
	if (pHandle->pComponentPrivate == NULL)
	{
		DOMX_DEBUG
		    (" ERROR IN ALLOCATING PROXY COMPONENT PRIVATE STRUCTURE");
		eError = OMX_ErrorInsufficientResources;
		goto EXIT;
	}
	pComponentPrivate->cCompName =
	    (OMX_U8 *) TIMM_OSAL_Malloc(MAX_COMPONENT_NAME_LENGTH *
	    sizeof(OMX_U8), TIMM_OSAL_TRUE, 0, TIMMOSAL_MEM_SEGMENT_INT);
	if (pComponentPrivate->cCompName == NULL)
	{
		DOMX_DEBUG
		    (" ERROR IN ALLOCATING PROXY COMPONENT NAME STRUCTURE");
		TIMM_OSAL_Free(pComponentPrivate);
		eError = OMX_ErrorInsufficientResources;
		goto EXIT;
	}
	// Copying component Name - this will be picked up in the proxy common
	assert(strlen(COMPONENT_NAME) + 1 < MAX_COMPONENT_NAME_LENGTH);
	TIMM_OSAL_Memcpy(pComponentPrivate->cCompName, COMPONENT_NAME,
	    strlen(COMPONENT_NAME) + 1);
	eError = OMX_ProxyCommonInit(hComponent);	// Calling Proxy Common Init()

	if (eError != OMX_ErrorNone)
	{
		DOMX_DEBUG("Error in Initializing Proxy");
		TIMM_OSAL_Free(pComponentPrivate->cCompName);
		TIMM_OSAL_Free(pComponentPrivate);
	}
	// Make sure private function to component is always assigned
	// after component init.
#ifdef _OPENCORE
	pHandle->GetParameter = ComponentPrivateGetParameters;
#endif
	pHandle->EmptyThisBuffer = PrearrageEmptyThisBuffer;

	EXIT:
	return eError;
}
OMX_ERRORTYPE OMX_ComponentInit(OMX_HANDLETYPE hComponent)
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	OMX_COMPONENTTYPE *pHandle = NULL;
	PROXY_COMPONENT_PRIVATE *pComponentPrivate;
	pHandle = (OMX_COMPONENTTYPE *) hComponent;

	DOMX_DEBUG("__INSIDE VP6 VIDEO DECODER PROXY WRAPPER__\n");

	pHandle->pComponentPrivate =
	    (PROXY_COMPONENT_PRIVATE *)
	    TIMM_OSAL_Malloc(sizeof(PROXY_COMPONENT_PRIVATE), TIMM_OSAL_TRUE,
	    0, TIMMOSAL_MEM_SEGMENT_INT);

	pComponentPrivate = (PROXY_COMPONENT_PRIVATE *)
	    pHandle->pComponentPrivate;
	if (pHandle->pComponentPrivate == NULL)
	{
		DOMX_DEBUG(" ERROR IN ALLOCATING PROXY "
		    "COMPONENT PRIVATE STRUCTURE");
		eError = OMX_ErrorInsufficientResources;
		goto EXIT;
	}
	pComponentPrivate->cCompName =
	    TIMM_OSAL_Malloc(MAX_COMPONENT_NAME_LENGTH * sizeof(OMX_U8),
	    TIMM_OSAL_TRUE, 0, TIMMOSAL_MEM_SEGMENT_INT);

	if (pComponentPrivate->cCompName == NULL)
	{
		DOMX_DEBUG(" ERROR IN ALLOCATING PROXY " "COMPONENT NAME");
		eError = OMX_ErrorInsufficientResources;
		goto EXIT;
	}
	/*Copying component Name - this will be picked up in the proxy common */
	assert(strlen(COMPONENT_NAME) + 1 < MAX_COMPONENT_NAME_LENGTH);
	TIMM_OSAL_Memcpy(pComponentPrivate->cCompName, COMPONENT_NAME,
	    strlen(COMPONENT_NAME) + 1);
	printf("pComponentPrivate->cCompName : %s \n",
	    pComponentPrivate->cCompName);
	eError = OMX_ProxyCommonInit(hComponent);

      EXIT:
	if (eError != OMX_ErrorNone)
	{
		DOMX_DEBUG("Error in Initializing Proxy");
		if (pComponentPrivate)
		{
			if (pComponentPrivate->cCompName)
				TIMM_OSAL_Free(pComponentPrivate->cCompName);
			TIMM_OSAL_Free(pComponentPrivate);
		}
	}

	return eError;
}
static RPC_OMX_ERRORTYPE ComponentPrivateGetParameters(OMX_IN OMX_HANDLETYPE
	hComponent, OMX_IN OMX_INDEXTYPE nParamIndex,
	OMX_INOUT OMX_PTR pComponentParameterStructure)
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;

	if (nParamIndex == PV_OMX_COMPONENT_CAPABILITY_TYPE_INDEX)
	{
		DOMX_DEBUG("PV_OMX_COMPONENT_CAPABILITY_TYPE_INDEX\n");
		PV_OMXComponentCapabilityFlagsType *pPVCapaFlags =
		    (PV_OMXComponentCapabilityFlagsType *)
		    pComponentParameterStructure;
		/*Set PV (opencore) capability flags */
		pPVCapaFlags->iIsOMXComponentMultiThreaded = OMX_TRUE;
		pPVCapaFlags->iOMXComponentSupportsExternalOutputBufferAlloc =
		    OMX_TRUE;
		pPVCapaFlags->iOMXComponentSupportsExternalInputBufferAlloc =
		    OMX_FALSE;
		pPVCapaFlags->iOMXComponentSupportsMovableInputBuffers =
		    OMX_FALSE;
		pPVCapaFlags->iOMXComponentSupportsPartialFrames = OMX_FALSE;
		pPVCapaFlags->iOMXComponentCanHandleIncompleteFrames =
		    OMX_FALSE;
#if defined(__H264_DEC__) || defined(__VID_DEC__)
		pPVCapaFlags->iOMXComponentUsesNALStartCodes = OMX_TRUE;
#else
		pPVCapaFlags->iOMXComponentUsesNALStartCodes = OMX_FALSE;
#endif
		pPVCapaFlags->iOMXComponentUsesFullAVCFrames = OMX_TRUE;
		return OMX_ErrorNone;
	}
	return PROXY_GetParameter(hComponent, nParamIndex,
	    pComponentParameterStructure);
}
/* ===========================================================================*/
OMX_ERRORTYPE OMX_CameraVtcFreeMemory(OMX_IN OMX_HANDLETYPE hComponent)
{
    OMX_ERRORTYPE eError = OMX_ErrorNone;
    PROXY_COMPONENT_PRIVATE *pCompPrv;
    OMX_PROXY_CAM_PRIVATE* pCamPrv;
    OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
    OMX_U32 i = 0;
    RPC_OMX_ERRORTYPE eRPCError = RPC_OMX_ErrorNone;
    MEMPLUGIN_BUFFER_PARAMS delBuffer_params;
    MEMPLUGIN_BUFFER_PROPERTIES delBuffer_prop;

    pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
    pCamPrv = (OMX_PROXY_CAM_PRIVATE*)pCompPrv->pCompProxyPrv;

    MEMPLUGIN_BUFFER_PARAMS_INIT(delBuffer_params);

    for(i=0; i < MAX_NUM_INTERNAL_BUFFERS; i++) {
        if (pCamPrv->sInternalBuffers[i][0].pBufferHandle != NULL) {
            eRPCError = RPC_UnRegisterBuffer(pCompPrv->hRemoteComp, pCamPrv->sInternalBuffers[i][0].pRegBufferHandle, NULL , IONPointers);
            if (eRPCError != RPC_OMX_ErrorNone) {
                DOMX_ERROR("%s: DOMX: Unexpected error occurred while Unregistering Y Buffer#%d: eRPCError = 0x%x", __func__, i, eRPCError);
            }
            delBuffer_prop.sBuffer_accessor.pBufferHandle =  pCamPrv->sInternalBuffers[i][0].pBufferHandle;
            MemPlugin_Free(pCompPrv->pMemPluginHandle,pCompPrv->nMemmgrClientDesc,&delBuffer_params,&delBuffer_prop);
            pCamPrv->sInternalBuffers[i][0].pRegBufferHandle = NULL;
            pCamPrv->sInternalBuffers[i][0].pBufferHandle = NULL;
            DOMX_DEBUG("%s: DOMX: #%d Y Memory freed; eRPCError = 0x%x", __func__, i, eRPCError);
        }
        if (pCamPrv->sInternalBuffers[i][1].pBufferHandle != NULL) {
            eRPCError = RPC_UnRegisterBuffer(pCompPrv->hRemoteComp, pCamPrv->sInternalBuffers[i][1].pRegBufferHandle, NULL , IONPointers);
            if (eRPCError != RPC_OMX_ErrorNone) {
                DOMX_ERROR("%s: DOMX: Unexpected error occurred while Unregistering UV Buffer#%d: eRPCError = 0x%x", __func__, i, eRPCError);
            }
            delBuffer_prop.sBuffer_accessor.pBufferHandle =  pCamPrv->sInternalBuffers[i][1].pBufferHandle;
            MemPlugin_Free(pCompPrv->pMemPluginHandle,pCompPrv->nMemmgrClientDesc,&delBuffer_params,&delBuffer_prop);
            pCamPrv->sInternalBuffers[i][1].pRegBufferHandle = NULL;
            pCamPrv->sInternalBuffers[i][1].pBufferHandle = NULL;
            DOMX_DEBUG("%s: DOMX: #%d UV Memory freed; eRPCError = 0x%x", __func__, i, eRPCError);
        }
    }

EXIT:
   DOMX_EXIT("eError: %d", eError);
   return eError;
}
OMX_ERRORTYPE CameraMaptoTilerDuc(OMX_TI_CONFIG_SHAREDBUFFER *
    pConfigSharedBuffer, unsigned int *pDucMappedBuf,
    OMX_PTR * pBufToBeMapped)
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	OMX_U32 nBufLineSize = 0;
	MemAllocBlock block = { 0 };
	OMX_U32 nDiff = 0;
	ProcMgr_MapType mapType;
	SyslinkMemUtils_MpuAddrToMap MpuAddr_list_1D = { 0 };
	OMX_S32 status = 0;

	nBufLineSize = pConfigSharedBuffer->nSharedBuffSize;
	if (!MemMgr_IsMapped(pConfigSharedBuffer->pSharedBuff))
	{
		DOMX_DEBUG
		    ("Buffer is not mapped: Mapping as 1D buffer now..");
		block.pixelFormat = PIXEL_FMT_PAGE;
		block.ptr =
		    (OMX_PTR) (((OMX_U32) pConfigSharedBuffer->pSharedBuff /
			LINUX_PAGE_SIZE) * LINUX_PAGE_SIZE);
		block.dim.len =
		    (OMX_U32) ((((OMX_U32) pConfigSharedBuffer->pSharedBuff +
			    nBufLineSize + LINUX_PAGE_SIZE -
			    1) / LINUX_PAGE_SIZE) * LINUX_PAGE_SIZE) -
		    (OMX_U32) block.ptr;
		block.stride = 0;

		nDiff =
		    (OMX_U32) pConfigSharedBuffer->pSharedBuff -
		    (OMX_U32) block.ptr;

		*pBufToBeMapped = MemMgr_Map(&block, 1);

		PROXY_assert(*pBufToBeMapped != 0,
		    OMX_ErrorInsufficientResources,
		    "Map to TILER space failed");
	}

	if (MemMgr_IsMapped((OMX_PTR) * pBufToBeMapped))
	{

		mapType = ProcMgr_MapType_Tiler;
		MpuAddr_list_1D.mpuAddr = (OMX_U32) (*pBufToBeMapped) + nDiff;

		MpuAddr_list_1D.size = pConfigSharedBuffer->nSharedBuffSize;
		status = SysLinkMemUtils_map(&MpuAddr_list_1D, 1,
		    pDucMappedBuf, mapType, PROC_APPM3);

		PROXY_assert(status >= 0, OMX_ErrorInsufficientResources,
		    "Syslink map failed");
		pConfigSharedBuffer->pSharedBuff = (OMX_PTR) (*pDucMappedBuf);

	}
      EXIT:
	return eError;
}
/* ===========================================================================*/
RPC_OMX_ERRORTYPE RPC_SKEL_EventHandler(void *data)
{
	OMX_HANDLETYPE hComp;
	RPC_OMX_ERRORTYPE tRPCError = RPC_OMX_ErrorNone;
	OMX_COMPONENTTYPE *pHandle = NULL;
	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
	OMX_EVENTTYPE eEvent;
	OMX_U32 nData1;
	OMX_U32 nData2;
	OMX_PTR pEventData;
	OMX_U32 nPos = 0;
	//RPC_OMX_MESSAGE *recdMsg;
	OMX_U8 *pMsgBody = NULL;
	//recdMsg = (RPC_OMX_MESSAGE *) (data);
	pMsgBody = data;	//&recdMsg->msgBody[0];

	DOMX_ENTER("");

	//Marshalled:[>hComp|>eEvent|>nData1|>nData2|>pEventData]

	RPC_GETFIELDVALUE(pMsgBody, nPos, hComp, OMX_HANDLETYPE);
	pHandle = (OMX_COMPONENTTYPE *) hComp;
	pCompPrv = (PROXY_COMPONENT_PRIVATE *) pHandle->pComponentPrivate;

	RPC_GETFIELDVALUE(pMsgBody, nPos, eEvent, OMX_EVENTTYPE);
	RPC_GETFIELDVALUE(pMsgBody, nPos, nData1, OMX_U32);
	RPC_GETFIELDVALUE(pMsgBody, nPos, nData2, OMX_U32);
	RPC_GETFIELDVALUE(pMsgBody, nPos, pEventData, OMX_PTR);

	DOMX_DEBUG
	    ("\n**__**Got eventhandler from Remote Core succesffuly - values are : **__**");
	DOMX_DEBUG
	    ("hComp :0x%x,eEvent: 0x%x,nData1: 0x%x,nData2: 0x%x,pEventData: 0x%x",
	    hComp, eEvent, nData1, nData2, pEventData);
	DOMX_DEBUG("Just Before Entering Proxy Event handler call");

	tRPCError =
	    pCompPrv->proxyEventHandler(hComp, pCompPrv->pILAppData, eEvent,
	    nData1, nData2, pEventData);

	DOMX_EXIT("");
	return tRPCError;

}
/* ===========================================================================*/
static OMX_ERRORTYPE _OMX_CameraVtcAllocateMemory(OMX_IN OMX_HANDLETYPE hComponent)
{
    OMX_ERRORTYPE eError = OMX_ErrorNone, eCompReturn = OMX_ErrorNone;
    OMX_STATETYPE tState= OMX_StateInvalid;
    PROXY_COMPONENT_PRIVATE *pCompPrv;
    OMX_PROXY_CAM_PRIVATE* pCamPrv;
    OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
    OMX_U32 i = 0;
    OMX_CONFIG_RECTTYPE tFrameDim;
    OMX_U32 nFrmWidth, nFrmHeight;
    OMX_TI_PARAM_VTCSLICE tVtcConfig;
    RPC_OMX_ERRORTYPE eRPCError = RPC_OMX_ErrorNone;

    pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
    pCamPrv = (OMX_PROXY_CAM_PRIVATE*)pCompPrv->pCompProxyPrv;

    _PROXY_OMX_INIT_PARAM(&tFrameDim, OMX_CONFIG_RECTTYPE);
    _PROXY_OMX_INIT_PARAM(&tVtcConfig, OMX_TI_PARAM_VTCSLICE);

    /* Get the current state of the component */
    eError = OMX_GetState(hComponent, &tState);
    if(OMX_ErrorNone != eError)
    {
        DOMX_ERROR("%s: Error in fetching current state - %d", __func__, tState);
    }
    else
    {
        if (tState == OMX_StateLoaded) {
            DOMX_DEBUG("%s: Current state returned is %d", __func__, tState);

            if(OMX_GetParameter(hComponent, OMX_TI_IndexParamVtcSlice, &tVtcConfig) == OMX_ErrorNone) {
                if (tVtcConfig.nSliceHeight != 0 ) {
                    OMX_CONFIG_BOOLEANTYPE tVstabParam;
                    OMX_PARAM_VIDEONOISEFILTERTYPE tVnfParam;
                    OMX_TI_PARAM_VTCSLICE *pVtcConfig = &tVtcConfig;

                    tFrameDim.nPortIndex = PREVIEW_PORT; //Preview Port
                    if(OMX_GetParameter(hComponent, OMX_TI_IndexParam2DBufferAllocDimension, &tFrameDim) == OMX_ErrorNone){
                        DOMX_DEBUG("Acquired OMX_TI_IndexParam2DBufferAllocDimension data. nWidth = %d, nHeight = %d.\n\n", tFrameDim.nWidth, tFrameDim.nHeight);
                        nFrmWidth = tFrameDim.nWidth;
                        nFrmHeight = tFrameDim.nHeight;
                    }else {
                        DOMX_DEBUG("%s: No OMX_TI_IndexParam2DBufferAllocDimension data.\n\n", __func__);
                        nFrmWidth = MAX_VTC_WIDTH_WITH_VNF;
                        nFrmHeight = MAX_VTC_HEIGHT_WITH_VNF;
                    }

                    _PROXY_OMX_INIT_PARAM(&tVnfParam, OMX_PARAM_VIDEONOISEFILTERTYPE);
                    _PROXY_OMX_INIT_PARAM(&tVstabParam, OMX_CONFIG_BOOLEANTYPE);
                    eError = OMX_GetParameter(hComponent, OMX_IndexParamFrameStabilisation, &tVstabParam);
                    if(eError != OMX_ErrorNone) {
                        DOMX_ERROR("OMX_GetParameter for OMX_IndexParamFrameStabilisation returned error %x", eError);
                        goto EXIT;
                    }
                    tVnfParam.nPortIndex = PREVIEW_PORT;
                    eError = OMX_GetParameter(hComponent, OMX_IndexParamVideoNoiseFilter, &tVnfParam);
                    if(eError != OMX_ErrorNone) {
                        DOMX_ERROR("OMX_GetParameter for OMX_IndexParamVideoNoiseFilter returned error %x", eError);
                        goto EXIT;
                    }
                    DOMX_DEBUG(" Acquired OMX_TI_IndexParamVtcSlice data. nSliceHeight = %d, bVstabOn = %d, Vnfmode = %d, nWidth = %d, nHeight = %d.\n\n", tVtcConfig.nSliceHeight, tVstabParam.bEnabled, tVnfParam.eMode, nFrmWidth, nFrmHeight);
                    if (tVstabParam.bEnabled == OMX_FALSE && tVnfParam.eMode != OMX_VideoNoiseFilterModeOff) {
                        eError = GLUE_CameraVtcAllocateMemory(hComponent,
                                                              pVtcConfig,
                                                              nFrmWidth,
                                                              nFrmHeight);
                        if(eError != OMX_ErrorNone) {
                           DOMX_ERROR("Allocate Memory for vtc config returned error %x", eError);
                           goto EXIT;
                        }
                   }
                }
            }
        }
    }
EXIT:

   DOMX_EXIT("eError: %d", eError);
   return eError;
}
/* ===========================================================================*/
RPC_OMX_ERRORTYPE RPC_AllocateBuffer(RPC_OMX_HANDLE hRPCCtx,
    OMX_INOUT OMX_BUFFERHEADERTYPE ** ppBufferHdr, OMX_IN OMX_U32 nPortIndex,
    OMX_U32 * pBufHeaderRemote, OMX_U32 * pBufferMapped, OMX_PTR pAppPrivate,
    OMX_U32 nSizeBytes, OMX_ERRORTYPE * eCompReturn)
{
	RPC_OMX_ERRORTYPE eRPCError = RPC_OMX_ErrorNone;
	RcmClient_Message *pPacket = NULL;
	RcmClient_Message *pRetPacket = NULL;
	OMX_U32 nPacketSize = PACKET_SIZE;
	RPC_OMX_MESSAGE *pRPCMsg = NULL;
	RPC_OMX_BYTE *pMsgBody;
	RPC_INDEX fxnIdx;
	OMX_U32 nPos = 0;
	OMX_S16 status;
	RPC_OMX_CONTEXT *hCtx = hRPCCtx;
	RPC_OMX_HANDLE hComp = hCtx->remoteHandle;

	OMX_U32 offset = 0;
	OMX_TI_PLATFORMPRIVATE *pPlatformPrivate = NULL;
	OMX_BUFFERHEADERTYPE *pBufferHdr = *ppBufferHdr;

	DOMX_ENTER("");

	RPC_exitOnDucatiFault();

	fxnIdx =
	    rpcHndl[TARGET_CORE_ID].rpcFxns[RPC_OMX_FXN_IDX_ALLOCATE_BUFFER].
	    rpcFxnIdx;

	RPC_getPacket(hCtx->ClientHndl[RCM_DEFAULT_CLIENT], nPacketSize,
	    pPacket);
	pRPCMsg = (RPC_OMX_MESSAGE *) (&pPacket->data);
	pMsgBody = &pRPCMsg->msgBody[0];

	//Marshalled:[>hComp|>nPortIndex|>pAppPrivate|>nSizeBytes|
	//<pBufHeaderRemote|<offset(BufHeaderRemotecontents)|<offset(platformprivate)|
	//<--BufHeaderRemotecontents--|!<--pPlatformPrivate--]

	RPC_SETFIELDVALUE(pMsgBody, nPos, hComp, RPC_OMX_HANDLE);
	RPC_SETFIELDVALUE(pMsgBody, nPos, nPortIndex, OMX_U32);
	RPC_SETFIELDVALUE(pMsgBody, nPos, pAppPrivate, OMX_PTR);
	RPC_SETFIELDVALUE(pMsgBody, nPos, nSizeBytes, OMX_U32);

	pPacket->poolId = hCtx->nPoolId;
	pPacket->jobId = hCtx->nJobId;
	RPC_sendPacket_sync(hCtx->ClientHndl[RCM_DEFAULT_CLIENT], pPacket,
	    fxnIdx, pRetPacket);
	pRPCMsg = (RPC_OMX_MESSAGE *) (&pRetPacket->data);
	pMsgBody = &pRPCMsg->msgBody[0];

	*eCompReturn = pRPCMsg->msgHeader.nOMXReturn;

	if (pRPCMsg->msgHeader.nOMXReturn == OMX_ErrorNone)
	{

		RPC_GETFIELDVALUE(pMsgBody, nPos, *pBufHeaderRemote, OMX_U32);

		RPC_GETFIELDOFFSET(pMsgBody, nPos, offset, OMX_U32);
		//save platform private before overwriting
		pPlatformPrivate = (*ppBufferHdr)->pPlatformPrivate;
		//RPC_GETFIELDCOPYTYPE(pMsgBody, offset, pBufferHdr, OMX_BUFFERHEADERTYPE);
		/*Copying each field of the header separately due to padding issues in
		   the buffer header structure */
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->nSize,
		    OMX_U32);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->nVersion,
		    OMX_VERSIONTYPE);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->pBuffer,
		    OMX_U8 *);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->nAllocLen,
		    OMX_U32);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->nFilledLen,
		    OMX_U32);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->nOffset,
		    OMX_U32);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->pAppPrivate,
		    OMX_PTR);
		RPC_GETFIELDVALUE(pMsgBody, offset,
		    pBufferHdr->pPlatformPrivate, OMX_PTR);
		RPC_GETFIELDVALUE(pMsgBody, offset,
		    pBufferHdr->pInputPortPrivate, OMX_PTR);
		RPC_GETFIELDVALUE(pMsgBody, offset,
		    pBufferHdr->pOutputPortPrivate, OMX_PTR);
		RPC_GETFIELDVALUE(pMsgBody, offset,
		    pBufferHdr->hMarkTargetComponent, OMX_HANDLETYPE);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->pMarkData,
		    OMX_PTR);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->nTickCount,
		    OMX_U32);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->nTimeStamp,
		    OMX_TICKS);
		RPC_GETFIELDVALUE(pMsgBody, offset, pBufferHdr->nFlags,
		    OMX_U32);
		RPC_GETFIELDVALUE(pMsgBody, offset,
		    pBufferHdr->nInputPortIndex, OMX_U32);
		RPC_GETFIELDVALUE(pMsgBody, offset,
		    pBufferHdr->nOutputPortIndex, OMX_U32);

		(*ppBufferHdr)->pPlatformPrivate = pPlatformPrivate;

#ifdef TILER_BUFF
		DOMX_DEBUG(" Copying plat pvt. ");
		RPC_GETFIELDOFFSET(pMsgBody, nPos, offset, OMX_U32);
		if (offset != 0)
		{
			RPC_GETFIELDCOPYTYPE(pMsgBody, offset,
			    (OMX_TI_PLATFORMPRIVATE
				*) ((*ppBufferHdr)->pPlatformPrivate),
			    OMX_TI_PLATFORMPRIVATE);
			DOMX_DEBUG("Done copying plat pvt., aux buf = 0x%x",
			    ((OMX_TI_PLATFORMPRIVATE
				    *) ((*ppBufferHdr)->
				    pPlatformPrivate))->pAuxBuf1);
		}
#endif

		*pBufferMapped = (OMX_U32) (*ppBufferHdr)->pBuffer;
	} else
/* ===========================================================================*/
RPC_OMX_ERRORTYPE RPC_GetHandle(RPC_OMX_HANDLE hRPCCtx,
    OMX_STRING cComponentName, OMX_PTR pAppData,
    OMX_CALLBACKTYPE * pCallBacks, OMX_ERRORTYPE * eCompReturn)
{
	RPC_OMX_ERRORTYPE eRPCError = RPC_OMX_ErrorNone;
	RPC_OMX_MESSAGE *pRPCMsg = NULL;
	RPC_OMX_BYTE *pMsgBody = NULL;
	OMX_U32 dataOffset = 0;
	OMX_U32 dataOffset2 = 0;
	OMX_U32 offset = 0;
	OMX_U32 nPacketSize = PACKET_SIZE;
	RcmClient_Message *pPacket = NULL;
	RcmClient_Message *pRetPacket = NULL;
	OMX_U32 nPos = 0;
	RPC_OMX_CONTEXT *hCtx = hRPCCtx;
	RPC_OMX_HANDLE hComp = NULL;
	OMX_HANDLETYPE hActualComp = NULL;

	OMX_S16 status;
	RPC_INDEX fxnIdx;
	OMX_STRING CallingCorercmServerName;

	OMX_S32 pid = 0;	//For TILER memory allocation changes

	DOMX_ENTER("");
	DOMX_DEBUG("RPC_GetHandle: Recieved GetHandle request from %s",
	    cComponentName);

	RPC_exitOnDucatiFault();

	RPC_UTIL_GetLocalServerName(cComponentName,
	    &CallingCorercmServerName);
	DOMX_DEBUG(" RCM Server Name Calling on Current Core: %s",
	    CallingCorercmServerName);

	fxnIdx =
	    rpcHndl[TARGET_CORE_ID].rpcFxns[RPC_OMX_FXN_IDX_GET_HANDLE].
	    rpcFxnIdx;

	//Allocating remote command message
	RPC_getPacket(hCtx->ClientHndl[RCM_DEFAULT_CLIENT], nPacketSize,
	    pPacket);
	pRPCMsg = (RPC_OMX_MESSAGE *) (&pPacket->data);
	pMsgBody = &pRPCMsg->msgBody[0];

	//Marshalled:[>offset(cParameterName)|>pAppData|>offset(CallingCorercmServerName)|>pid|
	//>--cComponentName--|>--CallingCorercmServerName--|
	//<hComp]

	dataOffset =
	    sizeof(OMX_U32) + sizeof(OMX_PTR) + sizeof(OMX_U32) +
	    sizeof(OMX_S32);
	RPC_SETFIELDOFFSET(pMsgBody, nPos, dataOffset, OMX_U32);
	//To update with RPC macros
	strcpy((char *)(pMsgBody + dataOffset), cComponentName);

	RPC_SETFIELDVALUE(pMsgBody, nPos, pAppData, OMX_PTR);

	dataOffset2 = dataOffset + 128;	//max size of omx comp name
	RPC_SETFIELDOFFSET(pMsgBody, nPos, dataOffset2, OMX_U32);
	//To update with RPC macros
	strcpy((char *)(pMsgBody + dataOffset2), CallingCorercmServerName);

	//Towards TILER memory allocation changes
	pid = getpid();
	RPC_SETFIELDVALUE(pMsgBody, nPos, pid, OMX_S32);

	pPacket->poolId = hCtx->nPoolId;
	pPacket->jobId = hCtx->nJobId;

	RPC_sendPacket_sync(hCtx->ClientHndl[RCM_DEFAULT_CLIENT], pPacket,
	    fxnIdx, pRetPacket);

	pRPCMsg = (RPC_OMX_MESSAGE *) (&pRetPacket->data);
	pMsgBody = &pRPCMsg->msgBody[0];

	*eCompReturn = pRPCMsg->msgHeader.nOMXReturn;

	if (*eCompReturn == OMX_ErrorNone)
	{
		offset = dataOffset2 + 32;	//max size of rcm server name
		RPC_GETFIELDVALUE(pMsgBody, offset, hComp, RPC_OMX_HANDLE);
		DOMX_DEBUG("Received Remote Handle 0x%x", hComp);
		hCtx->remoteHandle = hComp;
		/* The handle received above is used for all communications
		   with the remote component but is not the actual component
		   handle (it is actually the rpc context handle which
		   contains lot of other info). The handle recd. below is the
		   actual remote component handle. This is used at present for
		   mark buffer implementation since in that case it is not
		   feasible to send the context handle */
		RPC_GETFIELDVALUE(pMsgBody, offset, hActualComp,
		    OMX_HANDLETYPE);
		hCtx->hActualRemoteCompHandle = hActualComp;
	}

	RPC_freePacket(hCtx->ClientHndl[RCM_DEFAULT_CLIENT], pRetPacket);

      EXIT:
	DOMX_EXIT("");
	return eRPCError;
}
static OMX_ERRORTYPE CameraSetConfig(OMX_IN OMX_HANDLETYPE
    hComponent, OMX_IN OMX_INDEXTYPE nParamIndex,
    OMX_INOUT OMX_PTR pComponentParameterStructure)
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	OMX_TI_CONFIG_SHAREDBUFFER *pConfigSharedBuffer = NULL;
	OMX_PTR pTempSharedBuff = NULL;
	/* Stores the Ducati Mapped addr for the user space allocated buffers for CameraCap/Exif Tags */
	unsigned int pDucMappedBuf = 0;
	OMX_PTR pBufToBeMapped = NULL;
	OMX_U32 status = 0;
	switch (nParamIndex)
	{
	case OMX_TI_IndexConfigExifTags:
	case OMX_TI_IndexConfigCamCapabilities:
	case OMX_TI_IndexConfigAAAskipBuffer:
		pConfigSharedBuffer =
		    (OMX_TI_CONFIG_SHAREDBUFFER *)
		    pComponentParameterStructure;

		pTempSharedBuff = pConfigSharedBuffer->pSharedBuff;

		eError =
		    CameraMaptoTilerDuc(pConfigSharedBuffer, &pDucMappedBuf,
		    &pBufToBeMapped);
		PROXY_assert((eError == OMX_ErrorNone), eError,
		    "Error in mapping to TILER or Ducati");

		eError =
		    RPC_FlushBuffer(pTempSharedBuff,
		    pConfigSharedBuffer->nSharedBuffSize, TARGET_CORE_ID);
		PROXY_assert(eError == OMX_ErrorNone, OMX_ErrorHardware,
		    "Flush Buffer failed");

		eError =
		    PROXY_SetConfig(hComponent, nParamIndex,
		    pConfigSharedBuffer);
		PROXY_assert((eError == OMX_ErrorNone), eError,
		    "Error in GetConfig");

		pConfigSharedBuffer->pSharedBuff = pTempSharedBuff;

		goto EXIT;
		break;

	default:
		break;
	}


	return PROXY_SetConfig(hComponent, nParamIndex,
	    pComponentParameterStructure);

      EXIT:
	if (pDucMappedBuf)
	{
		status = SysLinkMemUtils_unmap(pDucMappedBuf, PROC_APPM3);
		if (status <= 0)
			DOMX_DEBUG("Syslink unmap failed with status %d",
			    status);
		pDucMappedBuf = 0;
	}
	if (pBufToBeMapped)
	{
		status = MemMgr_UnMap(pBufToBeMapped);
		if (status != 0)
			DOMX_DEBUG("MemMgr unmap failed with status %d",
			    status);
		pBufToBeMapped = NULL;
	}

	return eError;
}
MEMPLUGIN_ERRORTYPE MemPlugin_ION_Alloc(void *pMemPluginHandle, OMX_U32 nClient,
                                    MEMPLUGIN_BUFFER_PARAMS *pIonBufferParams,
                                    MEMPLUGIN_BUFFER_PROPERTIES *pIonBufferProp)
{
    OMX_S16 ret;
    struct ion_handle *temp;
    size_t stride;
    MEMPLUGIN_ERRORTYPE eError = MEMPLUGIN_ERROR_NONE;
    MEMPLUGIN_ION_PARAMS sIonParams;
    MEMPLUGIN_OBJECT    *pMemPluginHdl = (MEMPLUGIN_OBJECT *)pMemPluginHandle;

    if(pIonBufferParams->nWidth <= 0)
    {
        eError = MEMPLUGIN_ERROR_BADPARAMETER;
        DOMX_ERROR("%s: width should be positive %d", __FUNCTION__,pIonBufferParams->nWidth);
        goto EXIT;
    }

    if(pMemPluginHdl->pPluginExtendedInfo == NULL)
    {
        MEMPLUGIN_ION_PARAMS_INIT(&sIonParams);
    }
    else
    {
        MEMPLUGIN_ION_PARAMS_COPY(((MEMPLUGIN_ION_PARAMS *)pMemPluginHdl->pPluginExtendedInfo),sIonParams);
    }
    if(pIonBufferParams->eBuffer_type == DEFAULT)
    {
        ret = (OMX_S16)ion_alloc(nClient,
                                    pIonBufferParams->nWidth,
                                    sIonParams.nAlign,
                                    sIonParams.alloc_flags,
                                    &temp);
        if(ret || (int)temp == -ENOMEM)
        {
            if(sIonParams.alloc_flags != OMAP_ION_HEAP_SECURE_INPUT)
            {
               //for non default types of allocation - no retry with tiler 1d - throw error
//STARGO: ducati secure heap is too small, need to allocate from heap
#if 0
               DOMX_ERROR("FAILED to allocate secure buffer of size=%d. ret=0x%x",pIonBufferParams->nWidth, ret);
               eError = MEMPLUGIN_ERROR_NORESOURCES;
               goto EXIT;
#endif
               DOMX_ERROR("FAILED to allocate secure buffer of size=%d. ret=0x%x - trying tiler 1d space",pIonBufferParams->nWidth, ret);
               pIonBufferParams->eBuffer_type = TILER1D;
               pIonBufferParams->eTiler_format = MEMPLUGIN_TILER_FORMAT_PAGE;
               sIonParams.alloc_flags = OMAP_ION_HEAP_TILER_MASK;
               sIonParams.nAlign = -1;
            }
            else
            {
                // for default non tiler (OMAP_ION_HEAP_SECURE_INPUT) retry allocating from tiler 1D
                DOMX_DEBUG("FAILED to allocate from non tiler space - trying tiler 1d space");
                pIonBufferParams->eBuffer_type = TILER1D;
                pIonBufferParams->eTiler_format = MEMPLUGIN_TILER_FORMAT_PAGE;
                sIonParams.alloc_flags = OMAP_ION_HEAP_TILER_MASK;
                sIonParams.nAlign = -1;
            }
        }
    }
    if(pIonBufferParams->eBuffer_type == TILER1D)
    {
        ret = (OMX_S16)ion_alloc_tiler(nClient,
                                        pIonBufferParams->nWidth,
                                        pIonBufferParams->nHeight,
                                        pIonBufferParams->eTiler_format,
                                        sIonParams.alloc_flags,
                                        &temp,
                                        &(pIonBufferProp->nStride));

         if (ret || ((int)temp == -ENOMEM))
         {
               DOMX_ERROR("FAILED to allocate buffer of size=%d. ret=0x%x",pIonBufferParams->nWidth, ret);
               eError = MEMPLUGIN_ERROR_NORESOURCES;
               goto EXIT;
         }
    }
    else if(pIonBufferParams->eBuffer_type == TILER2D)
    {
        DOMX_ERROR("Tiler 2D not implemented");
        eError = MEMPLUGIN_ERROR_NOTIMPLEMENTED;
        goto EXIT;
    }
    else if(!temp)
    {
        DOMX_ERROR("Undefined option for buffer type");
        eError = MEMPLUGIN_ERROR_UNDEFINED;
        goto EXIT;
    }
    pIonBufferProp->sBuffer_accessor.pBufferHandle = (OMX_PTR)temp;
    pIonBufferProp->nStride =  stride;

    if(pIonBufferParams->bMap == OMX_TRUE)
    {
        ret = (OMX_S16) ion_map(nClient,
                                pIonBufferProp->sBuffer_accessor.pBufferHandle,
                                pIonBufferParams->nWidth*pIonBufferParams->nHeight,
                                sIonParams.prot,
                                sIonParams.map_flags,
                                sIonParams.nOffset,
                                (unsigned char **) &(pIonBufferProp->sBuffer_accessor.pBufferMappedAddress),
                                &(pIonBufferProp->sBuffer_accessor.bufferFd));

        if(ret < 0)
        {
                DOMX_ERROR("userspace mapping of ION buffers returned error");
                eError = MEMPLUGIN_ERROR_NORESOURCES;
                goto EXIT;
        }
    }
    else
    {
        ret = (OMX_S16) ion_share(nClient,
                                    pIonBufferProp->sBuffer_accessor.pBufferHandle,
                                    &(pIonBufferProp->sBuffer_accessor.bufferFd));
        if(ret < 0)
        {
                DOMX_ERROR("ION share returned error");
                eError = MEMPLUGIN_ERROR_NORESOURCES;
                goto EXIT;
        }
    }
EXIT:
      if (eError != MEMPLUGIN_ERROR_NONE) {
          DOMX_EXIT("%s exited with error 0x%x",__FUNCTION__,eError);
         return eError;
      }
      else {
          DOMX_EXIT("%s executed successfully",__FUNCTION__);
         return MEMPLUGIN_ERROR_NONE;
      }
}
OMX_ERRORTYPE GLUE_CameraVtcAllocateMemory(OMX_IN OMX_HANDLETYPE hComponent, OMX_TI_PARAM_VTCSLICE *pVtcConfig,
											OMX_U32 nFrmWidth, OMX_U32 nFrmHeight)
{
	OMX_U32 i;
	OMX_ERRORTYPE eError = OMX_ErrorNone, eCompReturn = OMX_ErrorNone;
    PROXY_COMPONENT_PRIVATE *pCompPrv;
    OMX_PROXY_CAM_PRIVATE* pCamPrv;
    OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
	MEMPLUGIN_BUFFER_PARAMS newBuffer_params,delBuffer_params;
	MEMPLUGIN_BUFFER_PROPERTIES newBuffer_prop,delBuffer_prop;
	RPC_OMX_ERRORTYPE eRPCError = RPC_OMX_ErrorNone;
	MEMPLUGIN_ERRORTYPE eMemError = MEMPLUGIN_ERROR_NONE;
	MEMPLUGIN_ION_PARAMS *pIonParams;
    MEMPLUGIN_OBJECT	*pMemPluginHdl;

    pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
    pCamPrv = (OMX_PROXY_CAM_PRIVATE*)pCompPrv->pCompProxyPrv;
    pMemPluginHdl = ((MEMPLUGIN_OBJECT *)pCompPrv->pMemPluginHandle);
    pIonParams = ((MEMPLUGIN_ION_PARAMS *)pMemPluginHdl->pPluginExtendedInfo);

	MEMPLUGIN_BUFFER_PARAMS_INIT(newBuffer_params);
	MEMPLUGIN_BUFFER_PARAMS_INIT(delBuffer_params);
	for(i=0; i < MAX_NUM_INTERNAL_BUFFERS; i++) {
        pVtcConfig->nInternalBuffers = i;
        newBuffer_params.nWidth = nFrmWidth;
        newBuffer_params.nHeight = nFrmHeight;
        newBuffer_params.eBuffer_type = TILER1D;
        newBuffer_params.eTiler_format = MEMPLUGIN_TILER_FORMAT_8BIT;

		if(pIonParams == NULL)
		{
			pIonParams = TIMM_OSAL_MallocExtn(sizeof(MEMPLUGIN_ION_PARAMS), TIMM_OSAL_TRUE,
                                      0, TIMMOSAL_MEM_SEGMENT_EXT, NULL);
			if(pIonParams == NULL)
			{
				DOMX_ERROR("%s:Error allocating pPluginExtendedInfo",__FUNCTION__);
				goto EXIT;
			}
                        pMemPluginHdl->pPluginExtendedInfo = pIonParams;
		}
		MEMPLUGIN_ION_PARAMS_INIT(pIonParams);
                //override alloc_flags for tiler 1d non secure
		pIonParams->alloc_flags = OMAP_ION_HEAP_TILER_MASK;
		eMemError = MemPlugin_Alloc(pCompPrv->pMemPluginHandle,pCompPrv->nMemmgrClientDesc,&newBuffer_params,&newBuffer_prop);
		if(eMemError != MEMPLUGIN_ERROR_NONE)
		{
			DOMX_ERROR("%s:allocation failed size: %d",newBuffer_params.nWidth*newBuffer_params.nHeight);
			eError = OMX_ErrorInsufficientResources;
			goto EXIT;
		}
        eRPCError = RPC_RegisterBuffer(pCompPrv->hRemoteComp, newBuffer_prop.sBuffer_accessor.bufferFd, -1,
                                       &pCamPrv->sInternalBuffers[i][0].pRegBufferHandle, NULL, IONPointers);
        PROXY_checkRpcError();
        pVtcConfig->IonBufhdl[0] = (OMX_PTR)pCamPrv->sInternalBuffers[i][0].pRegBufferHandle;
        pCamPrv->sInternalBuffers[i][0].pBufferHandle = newBuffer_prop.sBuffer_accessor.pBufferHandle;
        close (newBuffer_prop.sBuffer_accessor.bufferFd);
        newBuffer_prop.sBuffer_accessor.bufferFd = -1;
        DOMX_DEBUG(" DOMX: ION Buffer#%d: Y: 0x%x, eError = 0x%x, eRPCError = 0x%x\n", i, pVtcConfig->IonBufhdl[0], eError, eRPCError);

		MEMPLUGIN_BUFFER_PARAMS_INIT(newBuffer_params);
		newBuffer_params.nWidth = nFrmWidth/2;
        newBuffer_params.nHeight = nFrmHeight/2;
        newBuffer_params.eBuffer_type = TILER1D;
        newBuffer_params.eTiler_format = MEMPLUGIN_TILER_FORMAT_16BIT;
        pIonParams->alloc_flags = OMAP_ION_HEAP_TILER_MASK;
		eMemError = MemPlugin_Alloc(pCompPrv->pMemPluginHandle,pCompPrv->nMemmgrClientDesc,&newBuffer_params,&newBuffer_prop);

		if(eMemError != MEMPLUGIN_ERROR_NONE)
		{
			DOMX_ERROR("%s:allocation failed size: %d",newBuffer_params.nWidth*newBuffer_params.nHeight);
			eError = OMX_ErrorInsufficientResources;
			if (pCamPrv->sInternalBuffers[i][0].pRegBufferHandle != NULL) {
               eRPCError = RPC_UnRegisterBuffer(pCompPrv->hRemoteComp, pCamPrv->sInternalBuffers[i][0].pRegBufferHandle, NULL , IONPointers);
               PROXY_checkRpcError();
            }
            MEMPLUGIN_BUFFER_PARAMS_INIT(delBuffer_params);
            delBuffer_prop.sBuffer_accessor.pBufferHandle = pCamPrv->sInternalBuffers[i][0].pBufferHandle;
			MemPlugin_Free(pCompPrv->pMemPluginHandle,pCompPrv->nMemmgrClientDesc,&delBuffer_params,&delBuffer_prop );
			pCamPrv->sInternalBuffers[i][0].pBufferHandle = NULL;
			goto EXIT;
		}

		eRPCError = RPC_RegisterBuffer(pCompPrv->hRemoteComp, newBuffer_prop.sBuffer_accessor.bufferFd,-1,
                                                           &pCamPrv->sInternalBuffers[i][1].pRegBufferHandle, NULL, IONPointers);
        PROXY_checkRpcError();
        pVtcConfig->IonBufhdl[1] = pCamPrv->sInternalBuffers[i][1].pRegBufferHandle;
        pCamPrv->sInternalBuffers[i][1].pBufferHandle = newBuffer_prop.sBuffer_accessor.pBufferHandle;
        close (newBuffer_prop.sBuffer_accessor.bufferFd);
        DOMX_DEBUG("DOMX: ION Buffer#%d: UV: 0x%x, eError: 0x%x eRPCError: 0x%x\n", i, pVtcConfig->IonBufhdl[1],eError,eRPCError);

        eError = __PROXY_SetParameter(hComponent,
                                      OMX_TI_IndexParamVtcSlice,
                                      pVtcConfig,
                                      pVtcConfig->IonBufhdl, 2);
        if (eError != OMX_ErrorNone) {
            DOMX_ERROR("DOMX: PROXY_SetParameter for OMX_TI_IndexParamVtcSlice completed with error 0x%x\n", eError);
            OMX_CameraVtcFreeMemory(hComponent);
            goto EXIT;
        }
	}
EXIT:
	pMemPluginHdl->pPluginExtendedInfo = NULL;
	if (eError != OMX_ErrorNone) {
		DOMX_ERROR("%s: Error  0x%x",__FUNCTION__, eError);
	}
    return eError;
}