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;
}
OMX_ERRORTYPE OMX_ComponentInit(OMX_HANDLETYPE hComponent)
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	OMX_ERRORTYPE dcc_eError = OMX_ErrorNone;
	OMX_COMPONENTTYPE *pHandle = NULL;
	PROXY_COMPONENT_PRIVATE *pComponentPrivate;
    OMX_U32 i = 0, j = 0;
    OMX_PROXY_CAM_PRIVATE* pCamPrv;
	MEMPLUGIN_ERRORTYPE eMemError = MEMPLUGIN_ERROR_NONE;
	pHandle = (OMX_COMPONENTTYPE *) hComponent;
	TIMM_OSAL_ERRORTYPE eOsalError = TIMM_OSAL_ERR_NONE;
	DOMX_ENTER("_____________________INSIDE CAMERA 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_ERROR(" ERROR IN ALLOCATING PROXY COMPONENT"
		    "PRIVATE STRUCTURE");
		eError = OMX_ErrorInsufficientResources;
		goto EXIT;
	}
	TIMM_OSAL_Memset(pComponentPrivate, 0,
		sizeof(PROXY_COMPONENT_PRIVATE));

	pComponentPrivate->cCompName =
	    TIMM_OSAL_Malloc(MAX_COMPONENT_NAME_LENGTH * sizeof(OMX_U8),
	    TIMM_OSAL_TRUE, 0, TIMMOSAL_MEM_SEGMENT_INT);
	/*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);

        pComponentPrivate->pCompProxyPrv =
            (OMX_PROXY_CAM_PRIVATE *)
            TIMM_OSAL_Malloc(sizeof(OMX_PROXY_CAM_PRIVATE), TIMM_OSAL_TRUE,
            0, TIMMOSAL_MEM_SEGMENT_INT);

        PROXY_assert(pComponentPrivate->pCompProxyPrv != NULL,
            OMX_ErrorInsufficientResources,
            "Could not allocate memory for proxy component private data structure");
        pCamPrv = (OMX_PROXY_CAM_PRIVATE*)pComponentPrivate->pCompProxyPrv;
        TIMM_OSAL_Memset(pComponentPrivate->pCompProxyPrv, 0,
                sizeof(OMX_PROXY_CAM_PRIVATE));

	pComponentPrivate->bMapBuffers = OMX_TRUE;
	/*Calling Proxy Common Init() */
	eError = OMX_ProxyCommonInit(hComponent);
	if (eError != OMX_ErrorNone)
	{
		DOMX_ERROR("\Error in Initializing Proxy");
		TIMM_OSAL_Free(pComponentPrivate->cCompName);
		TIMM_OSAL_Free(pComponentPrivate);
		TIMM_OSAL_Free(pComponentPrivate->pCompProxyPrv);
		goto EXIT;
	}
static OMX_ERRORTYPE ComponentPrivateDeInit(OMX_IN OMX_HANDLETYPE hComponent)
{
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	TIMM_OSAL_ERRORTYPE eOsalError = TIMM_OSAL_ERR_NONE;

	if (dcc_flag)
	{
		eOsalError =
		    TIMM_OSAL_MutexObtain(cam_mutex, TIMM_OSAL_SUSPEND);
		if (eOsalError != TIMM_OSAL_ERR_NONE)
		{
			TIMM_OSAL_Error("Mutex Obtain failed");
		}

		numofInstance = numofInstance - 1;

		eOsalError = TIMM_OSAL_MutexRelease(cam_mutex);
		PROXY_assert(eOsalError == TIMM_OSAL_ERR_NONE,
		    OMX_ErrorInsufficientResources, "Mutex release failed");
	}

	eError = PROXY_ComponentDeInit(hComponent);

      EXIT:
	return eError;
}
static OMX_ERRORTYPE ComponentPrivateDeInit(OMX_IN OMX_HANDLETYPE hComponent)
{
	OMX_ERRORTYPE eError = OMX_ErrorNone, eCompReturn = OMX_ErrorNone;
	TIMM_OSAL_ERRORTYPE eOsalError = TIMM_OSAL_ERR_NONE;
	PROXY_COMPONENT_PRIVATE *pCompPrv;
	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
	OMX_U32 i, j;
    OMX_PROXY_CAM_PRIVATE* pCamPrv;
    MEMPLUGIN_BUFFER_PARAMS delBuffer_params;
    MEMPLUGIN_BUFFER_PROPERTIES delBuffer_prop;
    RPC_OMX_ERRORTYPE eRPCError = RPC_OMX_ErrorNone;

        MEMPLUGIN_BUFFER_PARAMS_INIT(delBuffer_params);
	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
        if (dcc_flag)
        {
            eOsalError =
                TIMM_OSAL_MutexObtain(cam_mutex, TIMM_OSAL_SUSPEND);
            if (eOsalError != TIMM_OSAL_ERR_NONE)
            {
                    TIMM_OSAL_Error("Mutex Obtain failed");
            }
            numofInstance = numofInstance - 1;

            eOsalError = TIMM_OSAL_MutexRelease(cam_mutex);
            PROXY_assert(eOsalError == TIMM_OSAL_ERR_NONE,
                OMX_ErrorInsufficientResources, "Mutex release failed");
        }
        OMX_CameraVtcFreeMemory(hComponent);


    if(pCompPrv->pCompProxyPrv != NULL) {
        pCamPrv = (OMX_PROXY_CAM_PRIVATE*)pCompPrv->pCompProxyPrv;
        for (i = 0; i < PROXY_MAXNUMOFPORTS; i++) {
            for (j = 0; j < MAX_NUM_INTERNAL_BUFFERS; j++) {
                if (pCamPrv->gComponentBufferAllocation[i][j]) {
                    delBuffer_prop.sBuffer_accessor.pBufferHandle = pCamPrv->gComponentBufferAllocation[i][j];
                    MemPlugin_Free(pCompPrv->pMemPluginHandle,pCompPrv->nMemmgrClientDesc,&delBuffer_params,&delBuffer_prop);
                }
                pCamPrv->gComponentBufferAllocation[i][j] = NULL;
            }
        }

        TIMM_OSAL_Free(pCompPrv->pCompProxyPrv);
        pCompPrv->pCompProxyPrv = NULL;
        pCamPrv = NULL;
    }

	eError = PROXY_ComponentDeInit(hComponent);

      EXIT:
	return eError;
}
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;
	OMX_U32 status = 0;

	switch (nParamIndex)
	{
	case OMX_TI_IndexConfigAAAskipBuffer:
	case OMX_TI_IndexConfigCamCapabilities:
	case OMX_TI_IndexConfigExifTags:
	case OMX_TI_IndexConfigAlgoAreas:
	case OMX_TI_IndexConfigGammaTable:
        case OMX_TI_IndexConfigDynamicCameraDescriptor:
		pConfigSharedBuffer =
			(OMX_TI_CONFIG_SHAREDBUFFER *)
			pComponentParameterStructure;

		pTempSharedBuff = pConfigSharedBuffer->pSharedBuff;

		// TODO(XXX): Cache API is not yet available. Client needs to
		// allocate tiler buffer directly and assign to pSharedBuff.
		// Ptr allocated by MemMgr_Alloc in uncacheable so there
		// would be no need to cache API

		eError = __PROXY_SetConfig(hComponent,
								nParamIndex,
								pConfigSharedBuffer,
								&(pConfigSharedBuffer->pSharedBuff));

		PROXY_assert((eError == OMX_ErrorNone), eError,
		    "Error in GetConfig");

		pConfigSharedBuffer->pSharedBuff = pTempSharedBuff;

		goto EXIT;
		break;
	default:
		break;
	}

	return __PROXY_SetConfig(hComponent,
							nParamIndex,
							pComponentParameterStructure,
							NULL);

 EXIT:
	return eError;
}
static OMX_ERRORTYPE PrearrageEmptyThisBuffer(OMX_HANDLETYPE hComponent,
	OMX_BUFFERHEADERTYPE * pBufferHdr)
{

	LOGV("Inside PrearrageEmptyThisBuffer");
	OMX_ERRORTYPE eError = OMX_ErrorNone;
	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
	OMX_U8* pBuffer = NULL;
	OMX_U8* pData = NULL;
	OMX_U32 nValue = 0;
	OMX_U32 nWidth = 0;
	OMX_U32 nHeight = 0;
	OMX_U32 nActualCompression = 0;
	OMX_U8* pCSD = NULL;
	OMX_U32 nSize_CSD = 0;

	PROXY_assert(pBufferHdr != NULL, OMX_ErrorBadParameter, NULL);

	if (pBufferHdr->nFlags & OMX_BUFFERFLAG_CODECCONFIG){
		PROXY_assert(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter, NULL);

		pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
		pBuffer = pBufferHdr->pBuffer;

		VIDDEC_WMV_RCV_header  hBufferRCV;
		VIDDEC_WMV_VC1_header  hBufferVC1;

		LOGV("nFlags: %x", pBufferHdr->nFlags);

		pData = pBufferHdr->pBuffer + 15; /*Position to Width & Height*/
		VIDDEC_LoadDWORD(nValue, pData);
		nWidth = nValue;
		VIDDEC_LoadDWORD(nValue, pData);
		nHeight = nValue;

		pData += 4; /*Position to compression type*/
		VIDDEC_LoadDWORD(nValue, pData);
		nActualCompression = nValue;

		/*Seting pCSD to proper position*/
		pCSD = pBufferHdr->pBuffer;
		pCSD += CSD_POSITION;
		nSize_CSD = pBufferHdr->nFilledLen - CSD_POSITION;

		if(nActualCompression == FOURCC_WMV3){
			hBufferRCV.sStructRCV =
				(VIDDEC_WMV_RCV_struct *)
				TIMM_OSAL_Malloc(sizeof(VIDDEC_WMV_RCV_struct), TIMM_OSAL_TRUE,
				0, TIMMOSAL_MEM_SEGMENT_INT);

			PROXY_assert(hBufferRCV.sStructRCV != NULL,
				OMX_ErrorInsufficientResources, "Malloc failed");

			//From VC-1 spec: Table 265: Sequence Layer Data Structure
			hBufferRCV.sStructRCV->nNumFrames = 0xFFFFFF; /*Infinite frame number*/
			hBufferRCV.sStructRCV->nFrameType = 0xc5; /*0x85 is the value given by ASF to rcv converter*/
			hBufferRCV.sStructRCV->nID = 0x04; /*WMV3*/
			hBufferRCV.sStructRCV->nStructData = 0x018a3106; /*0x06318a01zero fill 0x018a3106*/
			hBufferRCV.sStructRCV->nVertSize = nHeight;
			hBufferRCV.sStructRCV->nHorizSize = nWidth;
			hBufferRCV.sStructRCV->nID2 = 0x0c; /* Fix value */
			hBufferRCV.sStructRCV->nSequenceHdr = 0x00002a9f; /* This value is not provided by parser, so giving a value from a video*/

			LOGV("initial: nStructData: %x", hBufferRCV.sStructRCV->nStructData);
			LOGV("pCSD = %x", (OMX_U32)*pCSD);

			hBufferRCV.sStructRCV->nStructData = (OMX_U32)pCSD[0] << 0  |
				pCSD[1] << 8  |
				pCSD[2] << 16 |
				pCSD[3] << 24;

			LOGV("FINAL: nStructData: %x", hBufferRCV.sStructRCV->nStructData);

			//Copy RCV structure to actual buffer
			assert(pBufferHdr->nFilledLen < pBufferHdr->nAllocLen);
			pBufferHdr->nFilledLen = sizeof(VIDDEC_WMV_RCV_struct);
			TIMM_OSAL_Memcpy(pBufferHdr->pBuffer, (OMX_U8*)hBufferRCV.pBuffer,
				pBufferHdr->nFilledLen);

			//Free aloocated memory
			TIMM_OSAL_Free(hBufferRCV.sStructRCV);
		}
		else if (nActualCompression == FOURCC_WVC1){
			LOGV("VC-1 Advance Profile prearrange");
			OMX_U8* pTempBuf =
				(OMX_U8 *)
				TIMM_OSAL_Malloc(pBufferHdr->nFilledLen, TIMM_OSAL_TRUE,
				0, TIMMOSAL_MEM_SEGMENT_INT);

			PROXY_assert(pTempBuf != NULL,
				OMX_ErrorInsufficientResources, "Malloc failed");

			TIMM_OSAL_Memcpy(pTempBuf, pBufferHdr->pBuffer+52,
				pBufferHdr->nFilledLen-52);
			TIMM_OSAL_Memcpy(pBufferHdr->pBuffer, pTempBuf,
				pBufferHdr->nFilledLen-52);
			pBufferHdr->nFilledLen -= 52;

			TIMM_OSAL_Free(pTempBuf);
		}
	}

	EXIT:
	LOGV("Redirection from PrearrageEmptyThisBuffer to PROXY_EmptyThisBuffer, nFilledLen=%d, nAllocLen=%d", pBufferHdr->nFilledLen, pBufferHdr->nAllocLen);

	return PROXY_EmptyThisBuffer(hComponent, pBufferHdr);
}
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;
}