1/*
2 * Copyright (c) 2010, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * *  Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 *
12 * *  Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * *  Neither the name of Texas Instruments Incorporated nor the names of
17 *    its contributors may be used to endorse or promote products derived
18 *    from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/**
34 *  @file  omx_proxy_mpeg4enc.c
35 *         This file contains methods that provides the functionality for
36 *         the OpenMAX1.1 DOMX Framework Proxy component.
37 *********************************************************************************************
38 This is the proxy specific wrapper that passes the component name to the generic proxy init()
39 The proxy wrapper also does some runtime/static time onfig on per proxy basis
40 This is a thin wrapper that is called when componentiit() of the proxy is called
41 static OMX_ERRORTYPE PROXY_Wrapper_init(OMX_HANDLETYPE hComponent, OMX_PTR pAppData);
42 this layer gets called first whenever a proxy's get handle is called
43 ************************************************************************************************
44 *  @path WTSD_DucatiMMSW\omx\omx_il_1_x\omx_proxy_component\src
45 *
46 *  @rev 1.0
47 */
48
49/*==============================================================
50 *! Revision History
51 *! ============================
52 * 31-August-2011 Lakshman N : Support for color conv at encoder
53 *                                 input port
54 *
55 *! 20-August-2010 Sarthak Aggarwal sarthak@ti.com: Initial Version
56 *================================================================*/
57
58/******************************************************************
59 *   INCLUDE FILES
60 ******************************************************************/
61
62#include <stdio.h>
63#include <string.h>
64#include <assert.h>
65#include "omx_proxy_common.h"
66#include <timm_osal_interfaces.h>
67#include "OMX_TI_IVCommon.h"
68#include "OMX_TI_Video.h"
69#include "OMX_TI_Index.h"
70
71#include <MetadataBufferType.h>
72#ifdef  ENABLE_GRALLOC_BUFFER
73#include "native_handle.h"
74#include <hal_public.h>
75#include <VideoMetadata.h>
76#endif
77
78#include <stdlib.h>
79#include <cutils/properties.h>
80
81#define COMPONENT_NAME "OMX.TI.DUCATI1.VIDEO.MPEG4E"
82/* needs to be specific for every configuration wrapper */
83
84#define OMX_MPEG4E_INPUT_PORT 0
85#define LINUX_PAGE_SIZE 4096
86
87#ifdef ANDROID_QUIRK_CHANGE_PORT_VALUES
88/* Opaque color format requires below quirks to be enabled
89 * ENABLE_GRALLOC_BUFFER
90 * ANDROID_QUIRK_CHANGE_PORT_VALUES
91 */
92OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_GetParameter(OMX_IN OMX_HANDLETYPE hComponent,
93    OMX_IN OMX_INDEXTYPE nParamIndex, OMX_INOUT OMX_PTR pParamStruct);
94
95OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_SetParameter(OMX_IN OMX_HANDLETYPE hComponent,
96    OMX_IN OMX_INDEXTYPE nParamIndex, OMX_INOUT OMX_PTR pParamStruct);
97
98#endif
99
100
101#define OMX_INIT_STRUCT(_s_, _name_)    \
102   memset(&(_s_), 0x0, sizeof(_name_));         \
103   (_s_).nSize = sizeof(_name_);               \
104   (_s_).nVersion.s.nVersionMajor = 0x1;    \
105   (_s_).nVersion.s.nVersionMinor = 0x1;     \
106   (_s_).nVersion.s.nRevision = 0x0;            \
107   (_s_).nVersion.s.nStep = 0x0
108
109
110/* Params needed for Dynamic Frame Rate Control*/
111#define FRAME_RATE_THRESHOLD 1 /* Change in Frame rate to configure the encoder */
112OMX_U32 nFrameRateThreshold = 0;/* Frame Rate threshold for every frame rate update */
113OMX_U32 nPortFrameRate = 0; /* Port FPS initially set to the Encoder */
114OMX_U32 nFrameCounter = 0; /* Number of input frames recieved since last framerate calculation */
115OMX_TICKS nVideoTime = 0; /* Video duration since last framerate calculation */
116OMX_TICKS nLastFrameRateUpdateTime = 0; /*Time stamp at last frame rate update */
117OMX_U16 nBFrames = 0; /* Number of B Frames in H264 Encoder */
118
119
120#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
121#define OMX_MPEG4E_NUM_INTERNAL_BUF (8)
122#define HAL_PIXEL_FORMAT_TI_NV12 (0x100)
123
124#define COLORCONVERT_MAX_SUB_BUFFERS (3)
125
126#define COLORCONVERT_BUFTYPE_VIRTUAL (0x0)
127#define COLORCONVERT_BUFTYPE_ION     (0x1)
128#define COLORCONVERT_BUFTYPE_GRALLOCOPAQUE (0x2)
129
130int COLORCONVERT_open(void **hCC, PROXY_COMPONENT_PRIVATE *pCompPrv);
131int COLORCONVERT_PlatformOpaqueToNV12(void *hCC, void *pSrc[],
132				      void *pDst[], int nWidth,
133				      int nHeight, int nStride,
134				      int nSrcBufType, int nDstBufType);
135int COLORCONVERT_close(void *hCC,PROXY_COMPONENT_PRIVATE *pCompPrv);
136
137static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_AllocateBuffer(OMX_IN OMX_HANDLETYPE hComponent,
138    OMX_INOUT OMX_BUFFERHEADERTYPE ** ppBufferHdr, OMX_IN OMX_U32 nPortIndex,
139    OMX_IN OMX_PTR pAppPrivate, OMX_IN OMX_U32 nSizeBytes);
140
141static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_FreeBuffer(OMX_IN OMX_HANDLETYPE hComponent,
142    OMX_IN OMX_U32 nPortIndex, OMX_IN OMX_BUFFERHEADERTYPE * pBufferHdr);
143
144static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_ComponentDeInit(OMX_HANDLETYPE hComponent);
145
146typedef struct _OMX_PROXY_MPEG4E_PRIVATE
147{
148	OMX_PTR  hBufPipe;
149	OMX_BOOL bAndroidOpaqueFormat;
150	OMX_PTR  hCC;
151	IMG_native_handle_t* gralloc_handle[OMX_MPEG4E_NUM_INTERNAL_BUF];
152	OMX_S32  nCurBufIndex;
153	alloc_device_t* mAllocDev;
154}OMX_PROXY_MPEG4E_PRIVATE;
155
156RPC_OMX_ERRORTYPE RPC_RegisterBuffer(OMX_HANDLETYPE hRPCCtx, int fd,
157				     OMX_PTR *handle1, OMX_PTR *handle2,
158				     PROXY_BUFFER_TYPE proxyBufferType);
159RPC_OMX_ERRORTYPE RPC_UnRegisterBuffer(OMX_HANDLETYPE hRPCCtx, OMX_PTR handle);
160#endif
161
162OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_GetExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent,
163    OMX_IN OMX_STRING cParameterName, OMX_OUT OMX_INDEXTYPE * pIndexType);
164
165OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_EmptyThisBuffer(OMX_HANDLETYPE hComponent,
166    OMX_BUFFERHEADERTYPE * pBufferHdr);
167
168static OMX_ERRORTYPE OMX_ConfigureDynamicFrameRate( OMX_HANDLETYPE hComponent,
169	OMX_BUFFERHEADERTYPE * pBufferHdr)
170{
171	OMX_ERRORTYPE eError = OMX_ErrorNone;
172	OMX_U32 nTargetFrameRate = 0; /* Target Frame Rate to be provided to Encoder */
173	OMX_U32 nCurrentFrameRate = 0; /* Current Frame Rate currently set  in Encoder */
174	OMX_CONFIG_FRAMERATETYPE tFrameRate;
175	OMX_COMPONENTTYPE *pHandle;
176	if (hComponent == NULL){
177		DOMX_ERROR("Component is invalid/ not present ");
178		return OMX_ErrorBadParameter;
179	}
180	pHandle = (OMX_COMPONENTTYPE *) hComponent;
181
182	/* Initialise the OMX structures */
183	OMX_INIT_STRUCT(tFrameRate,OMX_CONFIG_FRAMERATETYPE);
184
185	/* Intialise nLastFrameRateUpdateTime for the 1st frame */
186	if((!nFrameCounter) && (!nLastFrameRateUpdateTime)){
187		nLastFrameRateUpdateTime = pBufferHdr-> nTimeStamp;
188	}
189
190	/* Increment the Frame Counter and Calculate Frame Rate*/
191	nFrameCounter++;
192	nVideoTime = pBufferHdr->nTimeStamp - nLastFrameRateUpdateTime;
193
194	if(nVideoTime < 0) {
195		return OMX_ErrorBadParameter;
196	}
197
198	/*Get Port Frame Rate if not read yet*/
199	if(!nFrameRateThreshold) {
200		tFrameRate.nPortIndex = OMX_MPEG4E_INPUT_PORT; /* As per ducati support-set for input port */
201
202		/* Read Current FrameRate */
203		eError = pHandle->GetConfig(hComponent,OMX_IndexConfigVideoFramerate,&tFrameRate);
204        if (eError != OMX_ErrorNone)
205            DOMX_ERROR ("pHandle->GetConfig OMX_IndexConfigVideoFramerate eError :0x%x \n",eError);
206		nFrameRateThreshold = tFrameRate.xEncodeFramerate >>16;
207		nPortFrameRate = nFrameRateThreshold;
208		DOMX_DEBUG(" Port Frame Rate is %d ", nPortFrameRate);
209	}
210	nCurrentFrameRate = nFrameRateThreshold;
211
212	/* If Number of frames is less than the Threshold
213	  *  Frame Rate udpate is not necessary
214	  */
215	if(nFrameCounter < nFrameRateThreshold){
216		DOMX_EXIT(" Threshold not reached, no update necessary");
217		return OMX_ErrorNone;
218	}
219
220	/*Calculate the new target Frame Rate*/
221    if (nVideoTime != 0)
222        nTargetFrameRate = nFrameCounter * 1000000 / nVideoTime;
223
224	/* For 1080p  record, max FPS supported by Codec for profile 4.1 is 30.
225	  * When Dynamic Frame Rate is enabled, there might be scenario when FPS
226	  * calculated is more than 30. Hence adding the check so that Dynamic Frame
227	  * Rate set is never greater than the port FPS initially set.
228	  */
229	if(nTargetFrameRate > nPortFrameRate){
230		DOMX_DEBUG("Frame Rate Calculated is more than initial port set Frame Rate");
231		nTargetFrameRate = nPortFrameRate;
232	}
233
234	/* Difference in Frame Rate is more than Threshold - Only then update Frame Rate*/
235	if((( (OMX_S32)nTargetFrameRate) -((OMX_S32) nCurrentFrameRate) >= FRAME_RATE_THRESHOLD) ||
236		(((OMX_S32) nCurrentFrameRate) - ( (OMX_S32)nTargetFrameRate) >= FRAME_RATE_THRESHOLD)) {
237
238		/* Now Send the new Frame Rate */
239		tFrameRate.nPortIndex = OMX_MPEG4E_INPUT_PORT; /* As per ducati support-set for input port */
240		tFrameRate.xEncodeFramerate = (OMX_U32)(nTargetFrameRate * (1 << 16));
241		eError = pHandle->SetConfig(hComponent,OMX_IndexConfigVideoFramerate,&tFrameRate);
242		if(eError != OMX_ErrorNone){
243			DOMX_ERROR(" Error while configuring Dynamic Frame Rate,Error info = %d",eError);
244			return eError;
245		} else {
246            DOMX_DEBUG("Dynamic Frame Rate configuration successful \n");
247        }
248		nFrameRateThreshold = nTargetFrameRate; /*Update the threshold */
249	}
250
251	/* reset all params */
252	nFrameCounter = 0 ;
253	nVideoTime = 0;
254	nLastFrameRateUpdateTime = pBufferHdr->nTimeStamp;
255	return OMX_ErrorNone;
256}
257
258static OMX_ERRORTYPE ComponentPrivateEmptyThisBuffer(OMX_HANDLETYPE hComponent,
259	OMX_BUFFERHEADERTYPE * pBufferHdr)
260{
261	OMX_ERRORTYPE eError = OMX_ErrorNone;
262
263    eError = OMX_ConfigureDynamicFrameRate(hComponent, pBufferHdr);
264    if( eError != OMX_ErrorNone)
265        DOMX_ERROR(" Error while configuring FrameRate Dynamically.Error  info = %d",eError);
266
267    DOMX_DEBUG("Redirection from ComponentPricateEmptyThisBuffer to PROXY_EmptyThisBuffer");
268    return LOCAL_PROXY_MPEG4E_EmptyThisBuffer (hComponent,pBufferHdr);
269}
270
271OMX_ERRORTYPE OMX_ComponentInit(OMX_HANDLETYPE hComponent)
272{
273	OMX_ERRORTYPE eError = OMX_ErrorNone;
274	OMX_COMPONENTTYPE *pHandle = NULL;
275	PROXY_COMPONENT_PRIVATE *pComponentPrivate = NULL;
276	pHandle = (OMX_COMPONENTTYPE *) hComponent;
277        OMX_TI_PARAM_ENHANCEDPORTRECONFIG tParamStruct;
278#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
279	TIMM_OSAL_ERRORTYPE eOSALStatus = TIMM_OSAL_ERR_NONE;
280	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
281#endif
282	char value[OMX_MAX_STRINGNAME_SIZE];
283	OMX_U32 mEnableVFR = 1; /* Flag used to enable/disable VFR for Encoder */
284	property_get("debug.vfr.enable", value, "1");
285	mEnableVFR = atoi(value);
286
287	DOMX_ENTER("");
288
289	DOMX_DEBUG("Component name provided is %s", COMPONENT_NAME);
290
291	pHandle->pComponentPrivate =
292	    (PROXY_COMPONENT_PRIVATE *)
293	    TIMM_OSAL_Malloc(sizeof(PROXY_COMPONENT_PRIVATE), TIMM_OSAL_TRUE,
294	    0, TIMMOSAL_MEM_SEGMENT_INT);
295
296	PROXY_assert(pHandle->pComponentPrivate != NULL,
297	    OMX_ErrorInsufficientResources,
298	    "ERROR IN ALLOCATING PROXY COMPONENT PRIVATE STRUCTURE");
299
300	pComponentPrivate =
301	    (PROXY_COMPONENT_PRIVATE *) pHandle->pComponentPrivate;
302
303	TIMM_OSAL_Memset(pComponentPrivate, 0,
304		sizeof(PROXY_COMPONENT_PRIVATE));
305
306	pComponentPrivate->cCompName =
307	    TIMM_OSAL_Malloc(MAX_COMPONENT_NAME_LENGTH * sizeof(OMX_U8),
308	    TIMM_OSAL_TRUE, 0, TIMMOSAL_MEM_SEGMENT_INT);
309
310	PROXY_assert(pComponentPrivate->cCompName != NULL,
311	    OMX_ErrorInsufficientResources,
312	    " Error in Allocating space for proxy component table");
313
314#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
315	pComponentPrivate->pCompProxyPrv =
316	    (OMX_PROXY_MPEG4E_PRIVATE *)
317	    TIMM_OSAL_Malloc(sizeof(OMX_PROXY_MPEG4E_PRIVATE), TIMM_OSAL_TRUE,
318	    0, TIMMOSAL_MEM_SEGMENT_INT);
319
320	PROXY_assert(pComponentPrivate->pCompProxyPrv != NULL,
321	    OMX_ErrorInsufficientResources,
322	    " Could not allocate proxy component private");
323
324	TIMM_OSAL_Memset(pComponentPrivate->pCompProxyPrv, 0,
325		sizeof(OMX_PROXY_MPEG4E_PRIVATE));
326
327	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pComponentPrivate->pCompProxyPrv;
328
329	/* Create Pipe of for encoder input buffers */
330	eOSALStatus = TIMM_OSAL_CreatePipe(&pProxy->hBufPipe, sizeof(OMX_U32),
331					   OMX_MPEG4E_NUM_INTERNAL_BUF, 1);
332	PROXY_assert(eOSALStatus == TIMM_OSAL_ERR_NONE,
333			OMX_ErrorInsufficientResources,
334			"Pipe creation failed");
335
336#endif
337
338	// Copying component Name - this will be picked up in the proxy common
339	PROXY_assert(strlen(COMPONENT_NAME) + 1 < MAX_COMPONENT_NAME_LENGTH,
340	    OMX_ErrorInvalidComponentName,
341	    "Length of component name is longer than the max allowed");
342	TIMM_OSAL_Memcpy(pComponentPrivate->cCompName, COMPONENT_NAME,
343	    strlen(COMPONENT_NAME) + 1);
344
345	eError = OMX_ProxyCommonInit(hComponent);	// Calling Proxy Common Init()
346#ifdef ANDROID_QUIRK_CHANGE_PORT_VALUES
347	pHandle->SetParameter = LOCAL_PROXY_MPEG4E_SetParameter;
348    pHandle->GetParameter = LOCAL_PROXY_MPEG4E_GetParameter;
349#endif
350	pHandle->ComponentDeInit = LOCAL_PROXY_MPEG4E_ComponentDeInit;
351	pHandle->FreeBuffer = LOCAL_PROXY_MPEG4E_FreeBuffer;
352	pHandle->AllocateBuffer = LOCAL_PROXY_MPEG4E_AllocateBuffer;
353
354	pComponentPrivate->IsLoadedState = OMX_TRUE;
355	pHandle->EmptyThisBuffer = LOCAL_PROXY_MPEG4E_EmptyThisBuffer;
356	pHandle->GetExtensionIndex = LOCAL_PROXY_MPEG4E_GetExtensionIndex;
357
358	if(mEnableVFR)
359	    pHandle->EmptyThisBuffer = ComponentPrivateEmptyThisBuffer;
360
361    EXIT:
362	if (eError != OMX_ErrorNone)
363	{
364		DOMX_DEBUG("Error in Initializing Proxy");
365
366#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
367		if(pProxy->hBufPipe != NULL)
368		{
369			TIMM_OSAL_DeletePipe(pProxy->hBufPipe);
370			pProxy->hBufPipe = NULL;
371		}
372
373		if(pComponentPrivate->pCompProxyPrv != NULL)
374		{
375			TIMM_OSAL_Free(pComponentPrivate->pCompProxyPrv);
376			pComponentPrivate->pCompProxyPrv = NULL;
377			pProxy = NULL;
378		}
379#endif
380		if (pComponentPrivate->cCompName != NULL)
381		{
382			TIMM_OSAL_Free(pComponentPrivate->cCompName);
383			pComponentPrivate->cCompName = NULL;
384		}
385		if (pComponentPrivate != NULL)
386		{
387			TIMM_OSAL_Free(pComponentPrivate);
388			pComponentPrivate = NULL;
389		}
390	}
391	return eError;
392}
393
394#ifdef  ANDROID_QUIRK_CHANGE_PORT_VALUES
395
396/* ===========================================================================*/
397/**
398 * @name PROXY_MPEG4E_GetParameter()
399 * @brief
400 * @param void
401 * @return OMX_ErrorNone = Successful
402 * @sa TBD
403 *
404 */
405/* ===========================================================================*/
406OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_GetParameter(OMX_IN OMX_HANDLETYPE hComponent,
407    OMX_IN OMX_INDEXTYPE nParamIndex, OMX_INOUT OMX_PTR pParamStruct)
408{
409	OMX_ERRORTYPE eError = OMX_ErrorNone;
410	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
411	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
412	OMX_PARAM_PORTDEFINITIONTYPE* pPortDef = NULL;
413	OMX_VIDEO_PARAM_PORTFORMATTYPE* pPortParam = NULL;
414#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
415	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
416#endif
417
418	PROXY_require((pParamStruct != NULL), OMX_ErrorBadParameter, NULL);
419	PROXY_assert((hComp->pComponentPrivate != NULL),
420	    OMX_ErrorBadParameter, NULL);
421
422	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
423#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
424	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
425#endif
426
427	DOMX_ENTER
428	    ("hComponent = %p, pCompPrv = %p, nParamIndex = %d, pParamStruct = %p",
429	    hComponent, pCompPrv, nParamIndex, pParamStruct);
430
431	eError = PROXY_GetParameter(hComponent,nParamIndex, pParamStruct);
432
433	if(nParamIndex == OMX_IndexParamPortDefinition)
434	{
435		pPortDef = (OMX_PARAM_PORTDEFINITIONTYPE *)pParamStruct;
436
437		if(pPortDef->format.video.eColorFormat == OMX_COLOR_FormatYUV420PackedSemiPlanar)
438		{
439#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
440			if(pProxy->bAndroidOpaqueFormat == OMX_TRUE)
441			{
442				pPortDef->format.video.eColorFormat = OMX_COLOR_FormatAndroidOpaque;
443			}
444			else
445#endif
446			{
447				pPortDef->format.video.eColorFormat = OMX_TI_COLOR_FormatYUV420PackedSemiPlanar;
448			}
449		}
450	}
451	else if (nParamIndex == OMX_IndexParamVideoPortFormat)
452	{
453		pPortParam = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)pParamStruct;
454
455		if((eError == OMX_ErrorNone) &&
456		   (pPortParam->eColorFormat == OMX_COLOR_FormatYUV420PackedSemiPlanar))
457		{
458			pPortParam->eColorFormat = OMX_TI_COLOR_FormatYUV420PackedSemiPlanar;
459		}
460#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
461		else if ((eError == OMX_ErrorNoMore) && (pPortParam->nIndex == 1))
462		{
463			/* HACK:Remote OMX-MPEG4E supports only 1 color format (index 0). The
464			 * OMX_COLOR_FormatAndroidOpaque is supported only at the proxy.
465			 * Call GetParameter() to fill in defaults for parameters and
466			 * override color format and index for the additional
467			 * OMX_COLOR_FormatAndroidOpaque support*/
468			pPortParam->nIndex = 0;
469			eError = PROXY_GetParameter(hComponent, nParamIndex, pParamStruct);
470			pPortParam->nIndex = 1;
471			pPortParam->eColorFormat = OMX_COLOR_FormatAndroidOpaque;
472			eError = OMX_ErrorNone;
473		}
474#endif
475        }
476
477	PROXY_assert((eError == OMX_ErrorNone) || (eError == OMX_ErrorNoMore),
478		    eError," Error in Proxy GetParameter");
479
480      EXIT:
481	DOMX_EXIT("eError: %d", eError);
482	return eError;
483}
484
485/* ===========================================================================*/
486/**
487 * @name PROXY_MPEG4E_SetParameter()
488 * @brief
489 * @param void
490 * @return OMX_ErrorNone = Successful
491 * @sa TBD
492 *
493 */
494/* ===========================================================================*/
495OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_SetParameter(OMX_IN OMX_HANDLETYPE hComponent,
496    OMX_IN OMX_INDEXTYPE nParamIndex, OMX_IN OMX_PTR pParamStruct)
497{
498	OMX_ERRORTYPE eError = OMX_ErrorNone;
499	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
500	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
501	OMX_PARAM_PORTDEFINITIONTYPE* pPortDef = (OMX_PARAM_PORTDEFINITIONTYPE *)pParamStruct;
502	OMX_VIDEO_PARAM_PORTFORMATTYPE* pPortParams = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)pParamStruct;
503	OMX_VIDEO_STOREMETADATAINBUFFERSPARAMS* pStoreMetaData = (OMX_VIDEO_STOREMETADATAINBUFFERSPARAMS *) pParamStruct;
504	OMX_TI_PARAM_BUFFERPREANNOUNCE tParamSetNPA;
505	OMX_PARAM_PORTDEFINITIONTYPE sPortDef;
506#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
507	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
508#endif
509
510	DOMX_ENTER
511	    ("hComponent = %p, pCompPrv = %p, nParamIndex = %d, pParamStruct = %p",
512	    hComponent, pCompPrv, nParamIndex, pParamStruct);
513
514	PROXY_require((pParamStruct != NULL), OMX_ErrorBadParameter, NULL);
515	PROXY_require((hComp->pComponentPrivate != NULL),
516	    OMX_ErrorBadParameter, NULL);
517
518	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
519#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
520	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
521#endif
522
523	if(nParamIndex == OMX_IndexParamPortDefinition)
524	{
525		if(pPortDef->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
526		{
527			pPortDef->format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar;
528		}
529#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
530		else if(pPortDef->format.video.eColorFormat == OMX_COLOR_FormatAndroidOpaque)
531		{
532			if(COLORCONVERT_open(&pProxy->hCC,pCompPrv) != 0)
533			{
534				PROXY_assert(0, OMX_ErrorInsufficientResources,
535							"Failed to open Color converting service");
536			}
537			pProxy->bAndroidOpaqueFormat = OMX_TRUE;
538			pPortDef->format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar;
539		}
540#endif
541	}
542	else if(nParamIndex == OMX_IndexParamVideoPortFormat)
543	{
544		if(pPortParams->eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
545		{
546			pPortParams->eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar;
547		}
548#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
549		else if(pPortParams->eColorFormat == OMX_COLOR_FormatAndroidOpaque)
550		{
551			if(COLORCONVERT_open(&pProxy->hCC,pCompPrv) != 0)
552			{
553				PROXY_assert(0, OMX_ErrorInsufficientResources,
554							"Failed to open Color converting service");
555			}
556			pProxy->bAndroidOpaqueFormat = OMX_TRUE;
557			pPortParams->eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar;
558		}
559#endif
560	}
561	else if(nParamIndex == (OMX_INDEXTYPE) OMX_TI_IndexEncoderStoreMetadatInBuffers)
562	{
563		DOMX_DEBUG("Moving to Metadatamode");
564	    if (pStoreMetaData->nPortIndex == OMX_MPEG4E_INPUT_PORT && pStoreMetaData->bStoreMetaData == OMX_TRUE)
565	    {
566		tParamSetNPA.nSize = sizeof(OMX_TI_PARAM_BUFFERPREANNOUNCE);
567		tParamSetNPA.nVersion.s.nVersionMajor = OMX_VER_MAJOR;
568		tParamSetNPA.nVersion.s.nVersionMinor = OMX_VER_MINOR;
569		tParamSetNPA.nVersion.s.nRevision = 0x0;
570		tParamSetNPA.nVersion.s.nStep = 0x0;
571		tParamSetNPA.nPortIndex = OMX_MPEG4E_INPUT_PORT;
572		tParamSetNPA.bEnabled = OMX_TRUE;
573		//Call NPA on OMX encoder on ducati.
574		PROXY_SetParameter(hComponent,OMX_TI_IndexParamBufferPreAnnouncement, &tParamSetNPA);
575		pCompPrv->proxyPortBuffers[pStoreMetaData->nPortIndex].proxyBufferType = EncoderMetadataPointers;
576		DOMX_DEBUG("Moving to Metadatamode done");
577
578		/*Initializing Structure */
579		sPortDef.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
580		sPortDef.nVersion.s.nVersionMajor = OMX_VER_MAJOR;
581		sPortDef.nVersion.s.nVersionMinor = OMX_VER_MINOR;
582		sPortDef.nVersion.s.nRevision = 0x0;
583		sPortDef.nVersion.s.nStep = 0x0;
584		sPortDef.nPortIndex = OMX_MPEG4E_INPUT_PORT;
585
586		eError = PROXY_GetParameter(hComponent,OMX_IndexParamPortDefinition, &sPortDef);
587		PROXY_assert(eError == OMX_ErrorNone, eError," Error in Proxy GetParameter for Port Def");
588
589		sPortDef.format.video.nStride = LINUX_PAGE_SIZE;
590
591		eError = PROXY_SetParameter(hComponent,OMX_IndexParamPortDefinition, &sPortDef);
592
593		PROXY_assert(eError == OMX_ErrorNone, eError," Error in Proxy SetParameter for Port Def");
594	    }
595	    goto EXIT;
596	}
597
598	eError = PROXY_SetParameter(hComponent, nParamIndex, pParamStruct);
599	PROXY_assert(eError == OMX_ErrorNone,
600		    eError," Error in Proxy SetParameter");
601
602	EXIT:
603	DOMX_EXIT("eError: %d", eError);
604	return eError;
605}
606
607#endif
608
609
610/* ===========================================================================*/
611/**
612 * @name PROXY_GetExtensionIndex()
613 * @brief
614 * @param void
615 * @return OMX_ErrorNone = Successful
616 * @sa TBD
617 *
618 */
619/* ===========================================================================*/
620OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_GetExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent,
621    OMX_IN OMX_STRING cParameterName, OMX_OUT OMX_INDEXTYPE * pIndexType)
622{
623	OMX_ERRORTYPE eError = OMX_ErrorNone;
624	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
625	OMX_COMPONENTTYPE *hComp = hComponent;
626
627	PROXY_require((hComp->pComponentPrivate != NULL),
628	    OMX_ErrorBadParameter, NULL);
629	PROXY_require(cParameterName != NULL, OMX_ErrorBadParameter, NULL);
630	PROXY_require(pIndexType != NULL, OMX_ErrorBadParameter, NULL);
631
632	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
633
634	DOMX_ENTER("%s hComponent = %p, pCompPrv = %p, cParameterName = %s",
635	    __FUNCTION__,hComponent, pCompPrv, cParameterName);
636
637	// Check for NULL Parameters
638	PROXY_require((cParameterName != NULL && pIndexType != NULL),
639	    OMX_ErrorBadParameter, NULL);
640
641	// Ensure that String length is not greater than Max allowed length
642	PROXY_require(strlen(cParameterName) <= 127, OMX_ErrorBadParameter, NULL);
643
644	if(strcmp(cParameterName, "OMX.google.android.index.storeMetaDataInBuffers") == 0)
645	{
646		// If Index type is 2D Buffer Allocated Dimension
647		*pIndexType = (OMX_INDEXTYPE) OMX_TI_IndexEncoderStoreMetadatInBuffers;
648		goto EXIT;
649	}
650
651        eError = PROXY_GetExtensionIndex(hComponent, cParameterName, pIndexType);
652
653      EXIT:
654	DOMX_EXIT("%s eError: %d",__FUNCTION__, eError);
655	return eError;
656}
657
658/* ===========================================================================*/
659/**
660 * @name PROXY_MPEG4E_EmptyThisBuffer()
661 * @brief
662 * @param void
663 * @return OMX_ErrorNone = Successful
664 * @sa TBD
665 *
666 */
667/* ===========================================================================*/
668OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_EmptyThisBuffer(OMX_HANDLETYPE hComponent,
669    OMX_BUFFERHEADERTYPE * pBufferHdr)
670{
671
672	OMX_ERRORTYPE eError = OMX_ErrorNone;
673	PROXY_COMPONENT_PRIVATE *pCompPrv;
674	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
675	OMX_PTR pBufferOrig = NULL;
676	OMX_U32 nStride = 0, nNumLines = 0;
677	OMX_PARAM_PORTDEFINITIONTYPE tParamStruct;
678	OMX_U32 nFilledLen, nAllocLen;
679#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
680	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
681	TIMM_OSAL_ERRORTYPE eOSALStatus = TIMM_OSAL_ERR_NONE;
682	OMX_U32 nBufIndex = 0, nSize=0, nRet=0;
683#endif
684#ifdef ENABLE_GRALLOC_BUFFER
685	OMX_PTR pAuxBuf0 = NULL, pAuxBuf1 = NULL;
686	RPC_OMX_ERRORTYPE eRPCError = RPC_OMX_ErrorNone;
687	OMX_ERRORTYPE eCompReturn = OMX_ErrorNone;
688#endif
689
690	PROXY_require(pBufferHdr != NULL, OMX_ErrorBadParameter, NULL);
691	PROXY_require(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter,
692	    NULL);
693	PROXY_CHK_VERSION(pBufferHdr, OMX_BUFFERHEADERTYPE);
694
695	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
696#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
697	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
698#endif
699
700	tParamStruct.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
701	tParamStruct.nVersion.s.nVersionMajor = OMX_VER_MAJOR;
702	tParamStruct.nVersion.s.nVersionMinor = OMX_VER_MINOR;
703	tParamStruct.nVersion.s.nRevision = 0x0;
704	tParamStruct.nVersion.s.nStep = 0x0;
705	tParamStruct.nPortIndex = OMX_MPEG4E_INPUT_PORT;
706
707	eError = PROXY_GetParameter(hComponent, OMX_IndexParamPortDefinition, &tParamStruct);
708	PROXY_require(eError == OMX_ErrorNone, OMX_ErrorBadParameter, "Error is Get Parameter for port def");
709	nFilledLen = pBufferHdr->nFilledLen;
710	nAllocLen = pBufferHdr->nAllocLen;
711        if(nFilledLen != 0)
712        {
713        	pBufferHdr->nFilledLen = tParamStruct.nBufferSize;
714        }
715	pBufferHdr->nAllocLen =  tParamStruct.nBufferSize;
716
717	DOMX_DEBUG
718	    ("%s hComponent=%p, pCompPrv=%p, nFilledLen=%d, nOffset=%d, nFlags=%08x",
719	    __FUNCTION__,hComponent, pCompPrv, pBufferHdr->nFilledLen,
720	    pBufferHdr->nOffset, pBufferHdr->nFlags);
721
722	if( pCompPrv->proxyPortBuffers[OMX_MPEG4E_INPUT_PORT].proxyBufferType == EncoderMetadataPointers
723                && nFilledLen != 0)
724	{
725		OMX_U32 *pTempBuffer;
726		OMX_U32 nMetadataBufferType;
727		DOMX_DEBUG("Passing meta data to encoder");
728
729		pBufferOrig = pBufferHdr->pBuffer;
730
731		pTempBuffer = (OMX_U32 *) (pBufferHdr->pBuffer);
732		nMetadataBufferType = *pTempBuffer;
733
734		if(nMetadataBufferType == kMetadataBufferTypeCameraSource)
735		{
736#ifdef ENABLE_GRALLOC_BUFFER
737			IMG_native_handle_t* pGrallocHandle;
738			video_metadata_t* pVideoMetadataBuffer;
739			DOMX_DEBUG("MetadataBufferType is kMetadataBufferTypeCameraSource");
740
741			pVideoMetadataBuffer = (video_metadata_t*) ((OMX_U32 *)(pBufferHdr->pBuffer));
742			pGrallocHandle = (IMG_native_handle_t*) (pVideoMetadataBuffer->handle);
743			DOMX_DEBUG("Grallloc buffer recieved in metadata buffer 0x%x",pGrallocHandle );
744
745			pBufferHdr->pBuffer = (OMX_U8 *)(pGrallocHandle->fd[0]);
746			((OMX_TI_PLATFORMPRIVATE *) pBufferHdr->pPlatformPrivate)->
747			pAuxBuf1 = (OMX_PTR) pGrallocHandle->fd[1];
748			DOMX_DEBUG("%s Gralloc=0x%x, Y-fd=%d, UV-fd=%d", __FUNCTION__, pGrallocHandle,
749			            pGrallocHandle->fd[0], pGrallocHandle->fd[1]);
750
751			pBufferHdr->nOffset = pVideoMetadataBuffer->offset;
752#endif
753		}
754		else if(nMetadataBufferType == kMetadataBufferTypeGrallocSource)
755		{
756#ifdef ENABLE_GRALLOC_BUFFER
757			IMG_native_handle_t* pGrallocHandle;
758			buffer_handle_t  tBufHandle;
759			DOMX_DEBUG("MetadataBufferType is kMetadataBufferTypeGrallocSource");
760
761			pTempBuffer++;
762			tBufHandle =  *((buffer_handle_t *)pTempBuffer);
763			pGrallocHandle = (IMG_native_handle_t*) tBufHandle;
764			DOMX_DEBUG("Grallloc buffer recieved in metadata buffer 0x%x",pGrallocHandle );
765
766			pBufferHdr->pBuffer = (OMX_U8 *)(pGrallocHandle->fd[0]);
767			((OMX_TI_PLATFORMPRIVATE *) pBufferHdr->pPlatformPrivate)->
768			pAuxBuf1 = (OMX_PTR) pGrallocHandle->fd[1];
769			DOMX_DEBUG("%s Gralloc=0x%x, Y-fd=%d, UV-fd=%d", __FUNCTION__, pGrallocHandle,
770			            pGrallocHandle->fd[0], pGrallocHandle->fd[1]);
771#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
772			if (pProxy->bAndroidOpaqueFormat)
773			{
774                                DOMX_DEBUG(" ++TIMM_OSAL_ReadFromPipe() ");
775				/* Dequeue NV12 buffer for encoder */
776				eOSALStatus = TIMM_OSAL_ReadFromPipe(pProxy->hBufPipe, &nBufIndex,
777						                     sizeof(OMX_PTR), (TIMM_OSAL_U32 *)(&nSize),
778						                     TIMM_OSAL_SUSPEND);
779				PROXY_assert(eOSALStatus == TIMM_OSAL_ERR_NONE, OMX_ErrorBadParameter, NULL);
780
781				/* Get NV12 data after colorconv*/
782				nRet = COLORCONVERT_PlatformOpaqueToNV12(pProxy->hCC, (void **) &pGrallocHandle, (void **) &pProxy->gralloc_handle[nBufIndex],
783									 pGrallocHandle->iWidth,
784									 pGrallocHandle->iHeight,
785									 4096, COLORCONVERT_BUFTYPE_GRALLOCOPAQUE,
786									 COLORCONVERT_BUFTYPE_GRALLOCOPAQUE );
787				if(nRet != 0)
788				{
789					eOSALStatus = TIMM_OSAL_WriteToPipe(pProxy->hBufPipe, (void *) &nBufIndex,
790						                     sizeof(OMX_U32), TIMM_OSAL_SUSPEND);
791					PROXY_assert(0, OMX_ErrorBadParameter, "Color conversion routine failed");
792				}
793                                DOMX_DEBUG(" --COLORCONVERT_PlatformOpaqueToNV12() ");
794
795				/* Update pBufferHdr with NV12 buffers for OMX component */
796				pBufferHdr->pBuffer= (OMX_U8 *)(pProxy->gralloc_handle[nBufIndex]->fd[0]);
797				((OMX_TI_PLATFORMPRIVATE *) pBufferHdr->pPlatformPrivate)->pAuxBuf1 = (OMX_PTR)(pProxy->gralloc_handle[nBufIndex]->fd[1]);
798			}
799#endif
800#endif
801		}
802		else
803		{
804			return OMX_ErrorBadParameter;
805		}
806#ifdef ENABLE_GRALLOC_BUFFER
807		eRPCError = RPC_RegisterBuffer(pCompPrv->hRemoteComp, pBufferHdr->pBuffer,
808									   &pAuxBuf0, &pAuxBuf1,
809									   GrallocPointers);
810		PROXY_checkRpcError();
811		if (pAuxBuf0)
812			pBufferHdr->pBuffer = pAuxBuf0;
813		if (pAuxBuf1)
814			((OMX_TI_PLATFORMPRIVATE *) pBufferHdr->pPlatformPrivate)->pAuxBuf1 = pAuxBuf1;
815#endif
816	}
817
818	eError = PROXY_EmptyThisBuffer(hComponent, pBufferHdr);
819#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
820	if (pProxy->bAndroidOpaqueFormat)
821	{
822		/*Write buffer to end of pipe for re-circulation for future ETB()*/
823		eOSALStatus = TIMM_OSAL_WriteToPipe(pProxy->hBufPipe, (void *) &nBufIndex,
824					    sizeof(OMX_U32), TIMM_OSAL_SUSPEND);
825		PROXY_assert(eOSALStatus == TIMM_OSAL_ERR_NONE, OMX_ErrorBadParameter, "Pipe write failed");
826	}
827#endif
828
829	if( pCompPrv->proxyPortBuffers[pBufferHdr->nInputPortIndex].proxyBufferType == EncoderMetadataPointers) {
830		pBufferHdr->pBuffer = pBufferOrig;
831#ifdef ENABLE_GRALLOC_BUFFER
832		RPC_UnRegisterBuffer(pCompPrv->hRemoteComp, pAuxBuf0);
833		RPC_UnRegisterBuffer(pCompPrv->hRemoteComp, pAuxBuf1);
834#endif
835	}
836	EXIT:
837		return eError;
838}
839
840#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
841static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_AllocateBuffer(OMX_HANDLETYPE hComponent,
842		     OMX_BUFFERHEADERTYPE ** ppBufferHdr, OMX_U32 nPortIndex,
843		     OMX_PTR pAppPrivate, OMX_U32 nSizeBytes)
844{
845	OMX_ERRORTYPE eError = OMX_ErrorNone;
846	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
847	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
848	OMX_CONFIG_RECTTYPE tParamRect;
849	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
850	TIMM_OSAL_ERRORTYPE eOSALStatus = TIMM_OSAL_ERR_NONE;
851        DOMX_DEBUG(" ++LOCAL_PROXY_MPEG4E_AllocateBuffer");
852	int err, nStride;
853
854	PROXY_require(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter,
855	    NULL);
856	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
857	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
858
859	if((nPortIndex == OMX_MPEG4E_INPUT_PORT) &&
860	   (pProxy->bAndroidOpaqueFormat))
861	{
862		tParamRect.nSize = sizeof(OMX_CONFIG_RECTTYPE);
863		tParamRect.nVersion.s.nVersionMajor = 1;
864		tParamRect.nVersion.s.nVersionMinor = 1;
865		tParamRect.nVersion.s.nRevision = 0;
866		tParamRect.nVersion.s.nStep = 0;
867		tParamRect.nPortIndex = nPortIndex;
868
869		eError = PROXY_GetParameter(hComponent, (OMX_INDEXTYPE)OMX_TI_IndexParam2DBufferAllocDimension, &tParamRect);
870 		PROXY_assert(eError == OMX_ErrorNone, eError," Error in Proxy GetParameter from 2d index in allocate buffer");
871
872		err = pProxy->mAllocDev->alloc(pProxy->mAllocDev,(int) tParamRect.nWidth,(int) tParamRect.nHeight,
873			(int) HAL_PIXEL_FORMAT_TI_NV12,(int) GRALLOC_USAGE_HW_RENDER,
874			(const struct native_handle_t **)(&(pProxy->gralloc_handle[pProxy->nCurBufIndex])), &nStride);
875	}
876
877	eError = PROXY_AllocateBuffer(hComponent, ppBufferHdr, nPortIndex,
878				      pAppPrivate, nSizeBytes);
879EXIT:
880	if((nPortIndex == OMX_MPEG4E_INPUT_PORT) &&
881	   (pProxy->bAndroidOpaqueFormat))
882	{
883		if(eError != OMX_ErrorNone)
884		{
885			err = pProxy->mAllocDev->free(pProxy->mAllocDev, (buffer_handle_t)(pProxy->gralloc_handle[pProxy->nCurBufIndex]));
886		}
887		else
888		{
889			/*Populate buffer to pipe*/
890			eOSALStatus = TIMM_OSAL_WriteToPipe(pProxy->hBufPipe, (void *) &pProxy->nCurBufIndex,
891						    sizeof(OMX_U32), TIMM_OSAL_SUSPEND);
892			pProxy->nCurBufIndex++;
893		}
894	}
895        DOMX_DEBUG(" --LOCAL_PROXY_MPEG4E_AllocateBuffer");
896	return eError;
897}
898
899static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_FreeBuffer(OMX_IN OMX_HANDLETYPE hComponent,
900    OMX_IN OMX_U32 nPortIndex, OMX_IN OMX_BUFFERHEADERTYPE * pBufferHdr)
901{
902	OMX_ERRORTYPE eError = OMX_ErrorNone;
903	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
904	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
905	OMX_U32 nBufIndex, nSize, nCount=0;
906	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
907
908	PROXY_require(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter,
909	    NULL);
910	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
911	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
912
913	if((nPortIndex == OMX_MPEG4E_INPUT_PORT) &&
914	   (pProxy->bAndroidOpaqueFormat))
915	{
916		pProxy->nCurBufIndex--;
917		PROXY_require(pProxy->nCurBufIndex >=0,
918			      OMX_ErrorBadParameter, "Buffer index underflow");
919
920		if(pProxy->gralloc_handle[pProxy->nCurBufIndex])
921		{
922			pProxy->mAllocDev->free(pProxy->mAllocDev, (buffer_handle_t)(pProxy->gralloc_handle[pProxy->nCurBufIndex]));
923			pProxy->gralloc_handle[pProxy->nCurBufIndex] = NULL;
924		}
925
926		/*Clear the Bufindex pipe by dummy reads*/
927		TIMM_OSAL_GetPipeReadyMessageCount(pProxy->hBufPipe, (TIMM_OSAL_U32 *)&nCount);
928		if(nCount)
929		{
930			TIMM_OSAL_ReadFromPipe(pProxy->hBufPipe, &nBufIndex,
931					       sizeof(OMX_PTR), (TIMM_OSAL_U32 *)&nSize, TIMM_OSAL_NO_SUSPEND);
932		}
933	}
934
935	eError = PROXY_FreeBuffer(hComponent, nPortIndex, pBufferHdr);
936
937EXIT:
938	return eError;
939}
940
941OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_ComponentDeInit(OMX_HANDLETYPE hComponent)
942{
943	OMX_ERRORTYPE eError = OMX_ErrorNone;
944	PROXY_COMPONENT_PRIVATE *pCompPrv;
945	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
946	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
947	TIMM_OSAL_ERRORTYPE eOSALStatus = TIMM_OSAL_ERR_NONE;
948	OMX_U32 i;
949
950	PROXY_require(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter,
951	    NULL);
952	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
953	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
954
955	if(pProxy->hBufPipe != NULL)
956	{
957		eOSALStatus = TIMM_OSAL_DeletePipe(pProxy->hBufPipe);
958		pProxy->hBufPipe = NULL;
959
960		if(eOSALStatus != TIMM_OSAL_ERR_NONE)
961		{
962			DOMX_ERROR("Pipe deletion failed");
963		}
964	}
965
966	if(pProxy->bAndroidOpaqueFormat == OMX_TRUE)
967	{
968		/* Cleanup internal buffers in pipe if not freed on FreeBuffer */
969		for(i=0; i<OMX_MPEG4E_NUM_INTERNAL_BUF; i++)
970		{
971			if(pProxy->gralloc_handle[i])
972			{
973				pProxy->mAllocDev->free(pProxy->mAllocDev, (buffer_handle_t)(pProxy->gralloc_handle[i]));
974				pProxy->gralloc_handle[i] = NULL;
975			}
976		}
977
978		COLORCONVERT_close(pProxy->hCC,pCompPrv);
979		pProxy->bAndroidOpaqueFormat = OMX_FALSE;
980
981		if(pCompPrv->pCompProxyPrv != NULL)
982		{
983			TIMM_OSAL_Free(pCompPrv->pCompProxyPrv);
984			pCompPrv->pCompProxyPrv = NULL;
985		}
986	}
987
988	eError = PROXY_ComponentDeInit(hComponent);
989EXIT:
990	DOMX_EXIT("eError: %d", eError);
991	return eError;
992}
993
994int COLORCONVERT_open(void **hCC, PROXY_COMPONENT_PRIVATE *pCompPrv)
995{
996	int nErr = -1;
997	hw_module_t const* module = NULL;
998	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
999
1000	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
1001	nErr = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
1002
1003	if (nErr == 0)
1004	{
1005		*hCC = (void *) ((IMG_gralloc_module_public_t const *)module);
1006	}
1007	else
1008	{
1009		 DOMX_ERROR("FATAL: gralloc api hw_get_module() returned error: Can't find \
1010			    %s module err = %x", GRALLOC_HARDWARE_MODULE_ID, nErr);
1011	}
1012
1013	gralloc_open(module, &(pProxy->mAllocDev));
1014
1015	return nErr;
1016}
1017
1018int COLORCONVERT_PlatformOpaqueToNV12(void *hCC,
1019				      void *pSrc[COLORCONVERT_MAX_SUB_BUFFERS],
1020				      void *pDst[COLORCONVERT_MAX_SUB_BUFFERS],
1021				      int nWidth, int nHeight, int nStride,
1022				      int nSrcBufType,int nDstBufType)
1023{
1024	IMG_gralloc_module_public_t const* module = hCC;
1025	int nErr = -1;
1026
1027	if((nSrcBufType == COLORCONVERT_BUFTYPE_GRALLOCOPAQUE) && (nDstBufType == COLORCONVERT_BUFTYPE_VIRTUAL))
1028	{
1029		nErr = module->Blit(module, pSrc[0], pDst, HAL_PIXEL_FORMAT_TI_NV12);
1030
1031	}
1032	else if((nSrcBufType == COLORCONVERT_BUFTYPE_GRALLOCOPAQUE) && (nDstBufType == COLORCONVERT_BUFTYPE_GRALLOCOPAQUE ))
1033	{
1034		nErr = module->Blit2(module, pSrc[0], pDst[0], nWidth, nHeight, 0, 0);
1035	}
1036
1037	return nErr;
1038}
1039
1040int COLORCONVERT_close(void *hCC,PROXY_COMPONENT_PRIVATE *pCompPrv)
1041{
1042	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
1043	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
1044	if(pProxy && pProxy->mAllocDev)
1045	{
1046		gralloc_close(pProxy->mAllocDev);
1047	}
1048	return 0;
1049}
1050#endif
1051