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#define COMPONENT_NAME "OMX.TI.DUCATI1.VIDEO.MPEG4E"
79/* needs to be specific for every configuration wrapper */
80
81#define OMX_MPEG4E_INPUT_PORT 0
82#define LINUX_PAGE_SIZE 4096
83
84#ifdef ANDROID_QUIRK_CHANGE_PORT_VALUES
85/* Opaque color format requires below quirks to be enabled
86 * ENABLE_GRALLOC_BUFFER
87 * ANDROID_QUIRK_CHANGE_PORT_VALUES
88 */
89OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_GetParameter(OMX_IN OMX_HANDLETYPE hComponent,
90    OMX_IN OMX_INDEXTYPE nParamIndex, OMX_INOUT OMX_PTR pParamStruct);
91
92OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_SetParameter(OMX_IN OMX_HANDLETYPE hComponent,
93    OMX_IN OMX_INDEXTYPE nParamIndex, OMX_INOUT OMX_PTR pParamStruct);
94
95#endif
96
97#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
98#define OMX_MPEG4E_NUM_INTERNAL_BUF (8)
99#define HAL_PIXEL_FORMAT_TI_NV12 (0x100)
100
101#define COLORCONVERT_MAX_SUB_BUFFERS (3)
102
103#define COLORCONVERT_BUFTYPE_VIRTUAL (0x0)
104#define COLORCONVERT_BUFTYPE_ION     (0x1)
105#define COLORCONVERT_BUFTYPE_GRALLOCOPAQUE (0x2)
106
107int COLORCONVERT_open(void **hCC, PROXY_COMPONENT_PRIVATE *pCompPrv);
108int COLORCONVERT_PlatformOpaqueToNV12(void *hCC, void *pSrc[],
109				      void *pDst[], int nWidth,
110				      int nHeight, int nStride,
111				      int nSrcBufType, int nDstBufType);
112int COLORCONVERT_close(void *hCC,PROXY_COMPONENT_PRIVATE *pCompPrv);
113
114static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_AllocateBuffer(OMX_IN OMX_HANDLETYPE hComponent,
115    OMX_INOUT OMX_BUFFERHEADERTYPE ** ppBufferHdr, OMX_IN OMX_U32 nPortIndex,
116    OMX_IN OMX_PTR pAppPrivate, OMX_IN OMX_U32 nSizeBytes);
117
118static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_FreeBuffer(OMX_IN OMX_HANDLETYPE hComponent,
119    OMX_IN OMX_U32 nPortIndex, OMX_IN OMX_BUFFERHEADERTYPE * pBufferHdr);
120
121static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_ComponentDeInit(OMX_HANDLETYPE hComponent);
122
123typedef struct _OMX_PROXY_MPEG4E_PRIVATE
124{
125	OMX_PTR  hBufPipe;
126	OMX_BOOL bAndroidOpaqueFormat;
127	OMX_PTR  hCC;
128	IMG_native_handle_t* gralloc_handle[OMX_MPEG4E_NUM_INTERNAL_BUF];
129	OMX_S32  nCurBufIndex;
130	alloc_device_t* mAllocDev;
131}OMX_PROXY_MPEG4E_PRIVATE;
132#endif
133
134OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_GetExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent,
135    OMX_IN OMX_STRING cParameterName, OMX_OUT OMX_INDEXTYPE * pIndexType);
136
137OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_EmptyThisBuffer(OMX_HANDLETYPE hComponent,
138    OMX_BUFFERHEADERTYPE * pBufferHdr);
139
140OMX_ERRORTYPE OMX_ComponentInit(OMX_HANDLETYPE hComponent)
141{
142	OMX_ERRORTYPE eError = OMX_ErrorNone;
143	OMX_COMPONENTTYPE *pHandle = NULL;
144	PROXY_COMPONENT_PRIVATE *pComponentPrivate = NULL;
145	pHandle = (OMX_COMPONENTTYPE *) hComponent;
146        OMX_TI_PARAM_ENHANCEDPORTRECONFIG tParamStruct;
147#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
148	TIMM_OSAL_ERRORTYPE eOSALStatus = TIMM_OSAL_ERR_NONE;
149	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
150#endif
151
152	DOMX_ENTER("");
153
154	DOMX_DEBUG("Component name provided is %s", COMPONENT_NAME);
155
156	pHandle->pComponentPrivate =
157	    (PROXY_COMPONENT_PRIVATE *)
158	    TIMM_OSAL_Malloc(sizeof(PROXY_COMPONENT_PRIVATE), TIMM_OSAL_TRUE,
159	    0, TIMMOSAL_MEM_SEGMENT_INT);
160
161	PROXY_assert(pHandle->pComponentPrivate != NULL,
162	    OMX_ErrorInsufficientResources,
163	    "ERROR IN ALLOCATING PROXY COMPONENT PRIVATE STRUCTURE");
164
165	pComponentPrivate =
166	    (PROXY_COMPONENT_PRIVATE *) pHandle->pComponentPrivate;
167
168	TIMM_OSAL_Memset(pComponentPrivate, 0,
169		sizeof(PROXY_COMPONENT_PRIVATE));
170
171	pComponentPrivate->cCompName =
172	    TIMM_OSAL_Malloc(MAX_COMPONENT_NAME_LENGTH * sizeof(OMX_U8),
173	    TIMM_OSAL_TRUE, 0, TIMMOSAL_MEM_SEGMENT_INT);
174
175	PROXY_assert(pComponentPrivate->cCompName != NULL,
176	    OMX_ErrorInsufficientResources,
177	    " Error in Allocating space for proxy component table");
178
179#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
180	pComponentPrivate->pCompProxyPrv =
181	    (OMX_PROXY_MPEG4E_PRIVATE *)
182	    TIMM_OSAL_Malloc(sizeof(OMX_PROXY_MPEG4E_PRIVATE), TIMM_OSAL_TRUE,
183	    0, TIMMOSAL_MEM_SEGMENT_INT);
184
185	PROXY_assert(pComponentPrivate->pCompProxyPrv != NULL,
186	    OMX_ErrorInsufficientResources,
187	    " Could not allocate proxy component private");
188
189	TIMM_OSAL_Memset(pComponentPrivate->pCompProxyPrv, 0,
190		sizeof(OMX_PROXY_MPEG4E_PRIVATE));
191
192	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pComponentPrivate->pCompProxyPrv;
193
194	/* Create Pipe of for encoder input buffers */
195	eOSALStatus = TIMM_OSAL_CreatePipe(&pProxy->hBufPipe, sizeof(OMX_U32),
196					   OMX_MPEG4E_NUM_INTERNAL_BUF, 1);
197	PROXY_assert(eOSALStatus == TIMM_OSAL_ERR_NONE,
198			OMX_ErrorInsufficientResources,
199			"Pipe creation failed");
200
201#endif
202
203	// Copying component Name - this will be picked up in the proxy common
204	PROXY_assert(strlen(COMPONENT_NAME) + 1 < MAX_COMPONENT_NAME_LENGTH,
205	    OMX_ErrorInvalidComponentName,
206	    "Length of component name is longer than the max allowed");
207	TIMM_OSAL_Memcpy(pComponentPrivate->cCompName, COMPONENT_NAME,
208	    strlen(COMPONENT_NAME) + 1);
209
210	eError = OMX_ProxyCommonInit(hComponent);	// Calling Proxy Common Init()
211#ifdef ANDROID_QUIRK_CHANGE_PORT_VALUES
212	pHandle->SetParameter = LOCAL_PROXY_MPEG4E_SetParameter;
213    pHandle->GetParameter = LOCAL_PROXY_MPEG4E_GetParameter;
214#endif
215	pHandle->ComponentDeInit = LOCAL_PROXY_MPEG4E_ComponentDeInit;
216	pHandle->FreeBuffer = LOCAL_PROXY_MPEG4E_FreeBuffer;
217	pHandle->AllocateBuffer = LOCAL_PROXY_MPEG4E_AllocateBuffer;
218
219	pComponentPrivate->IsLoadedState = OMX_TRUE;
220	pHandle->EmptyThisBuffer = LOCAL_PROXY_MPEG4E_EmptyThisBuffer;
221	pHandle->GetExtensionIndex = LOCAL_PROXY_MPEG4E_GetExtensionIndex;
222
223    EXIT:
224	if (eError != OMX_ErrorNone)
225	{
226		DOMX_DEBUG("Error in Initializing Proxy");
227
228#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
229		if(pProxy->hBufPipe != NULL)
230		{
231			TIMM_OSAL_DeletePipe(pProxy->hBufPipe);
232			pProxy->hBufPipe = NULL;
233		}
234
235		if(pComponentPrivate->pCompProxyPrv != NULL)
236		{
237			TIMM_OSAL_Free(pComponentPrivate->pCompProxyPrv);
238			pComponentPrivate->pCompProxyPrv = NULL;
239			pProxy = NULL;
240		}
241#endif
242		if (pComponentPrivate->cCompName != NULL)
243		{
244			TIMM_OSAL_Free(pComponentPrivate->cCompName);
245			pComponentPrivate->cCompName = NULL;
246		}
247		if (pComponentPrivate != NULL)
248		{
249			TIMM_OSAL_Free(pComponentPrivate);
250			pComponentPrivate = NULL;
251		}
252	}
253	return eError;
254}
255
256#ifdef  ANDROID_QUIRK_CHANGE_PORT_VALUES
257
258/* ===========================================================================*/
259/**
260 * @name PROXY_MPEG4E_GetParameter()
261 * @brief
262 * @param void
263 * @return OMX_ErrorNone = Successful
264 * @sa TBD
265 *
266 */
267/* ===========================================================================*/
268OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_GetParameter(OMX_IN OMX_HANDLETYPE hComponent,
269    OMX_IN OMX_INDEXTYPE nParamIndex, OMX_INOUT OMX_PTR pParamStruct)
270{
271	OMX_ERRORTYPE eError = OMX_ErrorNone;
272	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
273	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
274	OMX_PARAM_PORTDEFINITIONTYPE* pPortDef = NULL;
275	OMX_VIDEO_PARAM_PORTFORMATTYPE* pPortParam = NULL;
276#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
277	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
278#endif
279
280	PROXY_require((pParamStruct != NULL), OMX_ErrorBadParameter, NULL);
281	PROXY_assert((hComp->pComponentPrivate != NULL),
282	    OMX_ErrorBadParameter, NULL);
283
284	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
285#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
286	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
287#endif
288
289	DOMX_ENTER
290	    ("hComponent = %p, pCompPrv = %p, nParamIndex = %d, pParamStruct = %p",
291	    hComponent, pCompPrv, nParamIndex, pParamStruct);
292
293	eError = PROXY_GetParameter(hComponent,nParamIndex, pParamStruct);
294
295	if(nParamIndex == OMX_IndexParamPortDefinition)
296	{
297		pPortDef = (OMX_PARAM_PORTDEFINITIONTYPE *)pParamStruct;
298
299		if(pPortDef->format.video.eColorFormat == OMX_COLOR_FormatYUV420PackedSemiPlanar)
300		{
301#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
302			if(pProxy->bAndroidOpaqueFormat == OMX_TRUE)
303			{
304				pPortDef->format.video.eColorFormat = OMX_COLOR_FormatAndroidOpaque;
305			}
306			else
307#endif
308			{
309				pPortDef->format.video.eColorFormat = OMX_TI_COLOR_FormatYUV420PackedSemiPlanar;
310			}
311		}
312	}
313	else if (nParamIndex == OMX_IndexParamVideoPortFormat)
314	{
315		pPortParam = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)pParamStruct;
316
317		if((eError == OMX_ErrorNone) &&
318		   (pPortParam->eColorFormat == OMX_COLOR_FormatYUV420PackedSemiPlanar))
319		{
320			pPortParam->eColorFormat = OMX_TI_COLOR_FormatYUV420PackedSemiPlanar;
321		}
322#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
323		else if ((eError == OMX_ErrorNoMore) && (pPortParam->nIndex == 1))
324		{
325			/* HACK:Remote OMX-MPEG4E supports only 1 color format (index 0). The
326			 * OMX_COLOR_FormatAndroidOpaque is supported only at the proxy.
327			 * Call GetParameter() to fill in defaults for parameters and
328			 * override color format and index for the additional
329			 * OMX_COLOR_FormatAndroidOpaque support*/
330			pPortParam->nIndex = 0;
331			eError = PROXY_GetParameter(hComponent, nParamIndex, pParamStruct);
332			pPortParam->nIndex = 1;
333			pPortParam->eColorFormat = OMX_COLOR_FormatAndroidOpaque;
334			eError = OMX_ErrorNone;
335		}
336#endif
337        }
338
339	PROXY_assert((eError == OMX_ErrorNone) || (eError == OMX_ErrorNoMore),
340		    eError," Error in Proxy GetParameter");
341
342      EXIT:
343	DOMX_EXIT("eError: %d", eError);
344	return eError;
345}
346
347/* ===========================================================================*/
348/**
349 * @name PROXY_MPEG4E_SetParameter()
350 * @brief
351 * @param void
352 * @return OMX_ErrorNone = Successful
353 * @sa TBD
354 *
355 */
356/* ===========================================================================*/
357OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_SetParameter(OMX_IN OMX_HANDLETYPE hComponent,
358    OMX_IN OMX_INDEXTYPE nParamIndex, OMX_IN OMX_PTR pParamStruct)
359{
360	OMX_ERRORTYPE eError = OMX_ErrorNone;
361	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
362	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
363	OMX_PARAM_PORTDEFINITIONTYPE* pPortDef = (OMX_PARAM_PORTDEFINITIONTYPE *)pParamStruct;
364	OMX_VIDEO_PARAM_PORTFORMATTYPE* pPortParams = (OMX_VIDEO_PARAM_PORTFORMATTYPE *)pParamStruct;
365	OMX_VIDEO_STOREMETADATAINBUFFERSPARAMS* pStoreMetaData = (OMX_VIDEO_STOREMETADATAINBUFFERSPARAMS *) pParamStruct;
366	OMX_TI_PARAM_BUFFERPREANNOUNCE tParamSetNPA;
367	OMX_PARAM_PORTDEFINITIONTYPE sPortDef;
368#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
369	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
370#endif
371
372	DOMX_ENTER
373	    ("hComponent = %p, pCompPrv = %p, nParamIndex = %d, pParamStruct = %p",
374	    hComponent, pCompPrv, nParamIndex, pParamStruct);
375
376	PROXY_require((pParamStruct != NULL), OMX_ErrorBadParameter, NULL);
377	PROXY_require((hComp->pComponentPrivate != NULL),
378	    OMX_ErrorBadParameter, NULL);
379
380	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
381#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
382	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
383#endif
384
385	if(nParamIndex == OMX_IndexParamPortDefinition)
386	{
387		if(pPortDef->format.video.eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
388		{
389			pPortDef->format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar;
390		}
391#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
392		else if(pPortDef->format.video.eColorFormat == OMX_COLOR_FormatAndroidOpaque)
393		{
394			if(COLORCONVERT_open(&pProxy->hCC,pCompPrv) != 0)
395			{
396				PROXY_assert(0, OMX_ErrorInsufficientResources,
397							"Failed to open Color converting service");
398			}
399			pProxy->bAndroidOpaqueFormat = OMX_TRUE;
400			pPortDef->format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar;
401		}
402#endif
403	}
404	else if(nParamIndex == OMX_IndexParamVideoPortFormat)
405	{
406		if(pPortParams->eColorFormat == OMX_TI_COLOR_FormatYUV420PackedSemiPlanar)
407		{
408			pPortParams->eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar;
409		}
410#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
411		else if(pPortParams->eColorFormat == OMX_COLOR_FormatAndroidOpaque)
412		{
413			if(COLORCONVERT_open(&pProxy->hCC,pCompPrv) != 0)
414			{
415				PROXY_assert(0, OMX_ErrorInsufficientResources,
416							"Failed to open Color converting service");
417			}
418			pProxy->bAndroidOpaqueFormat = OMX_TRUE;
419			pPortParams->eColorFormat = OMX_COLOR_FormatYUV420PackedSemiPlanar;
420		}
421#endif
422	}
423	else if(nParamIndex == (OMX_INDEXTYPE) OMX_TI_IndexEncoderStoreMetadatInBuffers)
424	{
425		DOMX_DEBUG("Moving to Metadatamode");
426	    if (pStoreMetaData->nPortIndex == OMX_MPEG4E_INPUT_PORT && pStoreMetaData->bStoreMetaData == OMX_TRUE)
427	    {
428		tParamSetNPA.nSize = sizeof(OMX_TI_PARAM_BUFFERPREANNOUNCE);
429		tParamSetNPA.nVersion.s.nVersionMajor = OMX_VER_MAJOR;
430		tParamSetNPA.nVersion.s.nVersionMinor = OMX_VER_MINOR;
431		tParamSetNPA.nVersion.s.nRevision = 0x0;
432		tParamSetNPA.nVersion.s.nStep = 0x0;
433		tParamSetNPA.nPortIndex = OMX_MPEG4E_INPUT_PORT;
434		tParamSetNPA.bEnabled = OMX_TRUE;
435		//Call NPA on OMX encoder on ducati.
436		PROXY_SetParameter(hComponent,OMX_TI_IndexParamBufferPreAnnouncement, &tParamSetNPA);
437		pCompPrv->proxyPortBuffers[pStoreMetaData->nPortIndex].proxyBufferType = EncoderMetadataPointers;
438		DOMX_DEBUG("Moving to Metadatamode done");
439
440		/*Initializing Structure */
441		sPortDef.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
442		sPortDef.nVersion.s.nVersionMajor = OMX_VER_MAJOR;
443		sPortDef.nVersion.s.nVersionMinor = OMX_VER_MINOR;
444		sPortDef.nVersion.s.nRevision = 0x0;
445		sPortDef.nVersion.s.nStep = 0x0;
446		sPortDef.nPortIndex = OMX_MPEG4E_INPUT_PORT;
447
448		eError = PROXY_GetParameter(hComponent,OMX_IndexParamPortDefinition, &sPortDef);
449		PROXY_assert(eError == OMX_ErrorNone, eError," Error in Proxy GetParameter for Port Def");
450
451		sPortDef.format.video.nStride = LINUX_PAGE_SIZE;
452
453		eError = PROXY_SetParameter(hComponent,OMX_IndexParamPortDefinition, &sPortDef);
454
455		PROXY_assert(eError == OMX_ErrorNone, eError," Error in Proxy SetParameter for Port Def");
456	    }
457	    goto EXIT;
458	}
459
460	eError = PROXY_SetParameter(hComponent, nParamIndex, pParamStruct);
461	PROXY_assert(eError == OMX_ErrorNone,
462		    eError," Error in Proxy SetParameter");
463
464	EXIT:
465	DOMX_EXIT("eError: %d", eError);
466	return eError;
467}
468
469#endif
470
471
472/* ===========================================================================*/
473/**
474 * @name PROXY_GetExtensionIndex()
475 * @brief
476 * @param void
477 * @return OMX_ErrorNone = Successful
478 * @sa TBD
479 *
480 */
481/* ===========================================================================*/
482OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_GetExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent,
483    OMX_IN OMX_STRING cParameterName, OMX_OUT OMX_INDEXTYPE * pIndexType)
484{
485	OMX_ERRORTYPE eError = OMX_ErrorNone;
486	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
487	OMX_COMPONENTTYPE *hComp = hComponent;
488
489	PROXY_require((hComp->pComponentPrivate != NULL),
490	    OMX_ErrorBadParameter, NULL);
491	PROXY_require(cParameterName != NULL, OMX_ErrorBadParameter, NULL);
492	PROXY_require(pIndexType != NULL, OMX_ErrorBadParameter, NULL);
493
494	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
495
496	DOMX_ENTER("%s hComponent = %p, pCompPrv = %p, cParameterName = %s",
497	    __FUNCTION__,hComponent, pCompPrv, cParameterName);
498
499	// Check for NULL Parameters
500	PROXY_require((cParameterName != NULL && pIndexType != NULL),
501	    OMX_ErrorBadParameter, NULL);
502
503	// Ensure that String length is not greater than Max allowed length
504	PROXY_require(strlen(cParameterName) <= 127, OMX_ErrorBadParameter, NULL);
505
506	if(strcmp(cParameterName, "OMX.google.android.index.storeMetaDataInBuffers") == 0)
507	{
508		// If Index type is 2D Buffer Allocated Dimension
509		*pIndexType = (OMX_INDEXTYPE) OMX_TI_IndexEncoderStoreMetadatInBuffers;
510		goto EXIT;
511	}
512
513        PROXY_GetExtensionIndex(hComponent, cParameterName, pIndexType);
514
515      EXIT:
516	DOMX_EXIT("%s eError: %d",__FUNCTION__, eError);
517	return eError;
518}
519
520/* ===========================================================================*/
521/**
522 * @name PROXY_MPEG4E_EmptyThisBuffer()
523 * @brief
524 * @param void
525 * @return OMX_ErrorNone = Successful
526 * @sa TBD
527 *
528 */
529/* ===========================================================================*/
530OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_EmptyThisBuffer(OMX_HANDLETYPE hComponent,
531    OMX_BUFFERHEADERTYPE * pBufferHdr)
532{
533
534	OMX_ERRORTYPE eError = OMX_ErrorNone;
535	PROXY_COMPONENT_PRIVATE *pCompPrv;
536	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
537	OMX_PTR pBufferOrig = pBufferHdr->pBuffer;
538	OMX_U32 nStride = 0, nNumLines = 0;
539	OMX_PARAM_PORTDEFINITIONTYPE tParamStruct;
540	OMX_U32 nFilledLen, nAllocLen;
541#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
542	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
543	TIMM_OSAL_ERRORTYPE eOSALStatus = TIMM_OSAL_ERR_NONE;
544	OMX_U32 nBufIndex = 0, nSize=0, nRet=0;
545#endif
546
547	PROXY_require(pBufferHdr != NULL, OMX_ErrorBadParameter, NULL);
548	PROXY_require(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter,
549	    NULL);
550	PROXY_CHK_VERSION(pBufferHdr, OMX_BUFFERHEADERTYPE);
551
552	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
553#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
554	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
555#endif
556
557	tParamStruct.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
558	tParamStruct.nVersion.s.nVersionMajor = OMX_VER_MAJOR;
559	tParamStruct.nVersion.s.nVersionMinor = OMX_VER_MINOR;
560	tParamStruct.nVersion.s.nRevision = 0x0;
561	tParamStruct.nVersion.s.nStep = 0x0;
562	tParamStruct.nPortIndex = OMX_MPEG4E_INPUT_PORT;
563
564	eError = PROXY_GetParameter(hComponent, OMX_IndexParamPortDefinition, &tParamStruct);
565	PROXY_require(eError == OMX_ErrorNone, OMX_ErrorBadParameter, "Error is Get Parameter for port def");
566	nFilledLen = pBufferHdr->nFilledLen;
567	nAllocLen = pBufferHdr->nAllocLen;
568        if(nFilledLen != 0)
569        {
570        	pBufferHdr->nFilledLen = tParamStruct.nBufferSize;
571        }
572	pBufferHdr->nAllocLen =  tParamStruct.nBufferSize;
573
574	DOMX_DEBUG
575	    ("%s hComponent=%p, pCompPrv=%p, nFilledLen=%d, nOffset=%d, nFlags=%08x",
576	    __FUNCTION__,hComponent, pCompPrv, pBufferHdr->nFilledLen,
577	    pBufferHdr->nOffset, pBufferHdr->nFlags);
578
579	if( pCompPrv->proxyPortBuffers[OMX_MPEG4E_INPUT_PORT].proxyBufferType == EncoderMetadataPointers
580                && nFilledLen != 0)
581	{
582		OMX_U32 *pTempBuffer;
583		OMX_U32 nMetadataBufferType;
584		DOMX_DEBUG("Passing meta data to encoder");
585
586		pBufferOrig = pBufferHdr->pBuffer;
587
588		pTempBuffer = (OMX_U32 *) (pBufferHdr->pBuffer);
589		nMetadataBufferType = *pTempBuffer;
590
591		if(nMetadataBufferType == kMetadataBufferTypeCameraSource)
592		{
593#ifdef ENABLE_GRALLOC_BUFFER
594			IMG_native_handle_t* pGrallocHandle;
595			video_metadata_t* pVideoMetadataBuffer;
596			DOMX_DEBUG("MetadataBufferType is kMetadataBufferTypeCameraSource");
597
598			pVideoMetadataBuffer = (video_metadata_t*) ((OMX_U32 *)(pBufferHdr->pBuffer));
599			pGrallocHandle = (IMG_native_handle_t*) (pVideoMetadataBuffer->handle);
600			DOMX_DEBUG("Grallloc buffer recieved in metadata buffer 0x%x",pGrallocHandle );
601
602			pBufferHdr->pBuffer = (OMX_U8 *)(pGrallocHandle->fd[0]);
603			((OMX_TI_PLATFORMPRIVATE *) pBufferHdr->pPlatformPrivate)->
604			pAuxBuf1 = (OMX_PTR) pGrallocHandle->fd[1];
605			DOMX_DEBUG("%s Gralloc=0x%x, Y-fd=%d, UV-fd=%d", __FUNCTION__, pGrallocHandle,
606			            pGrallocHandle->fd[0], pGrallocHandle->fd[1]);
607
608			pBufferHdr->nOffset = pVideoMetadataBuffer->offset;
609#endif
610		}
611		else if(nMetadataBufferType == kMetadataBufferTypeGrallocSource)
612		{
613#ifdef ENABLE_GRALLOC_BUFFER
614			IMG_native_handle_t* pGrallocHandle;
615			buffer_handle_t  tBufHandle;
616			DOMX_DEBUG("MetadataBufferType is kMetadataBufferTypeGrallocSource");
617
618			pTempBuffer++;
619			tBufHandle =  *((buffer_handle_t *)pTempBuffer);
620			pGrallocHandle = (IMG_native_handle_t*) tBufHandle;
621			DOMX_DEBUG("Grallloc buffer recieved in metadata buffer 0x%x",pGrallocHandle );
622
623			pBufferHdr->pBuffer = (OMX_U8 *)(pGrallocHandle->fd[0]);
624			((OMX_TI_PLATFORMPRIVATE *) pBufferHdr->pPlatformPrivate)->
625			pAuxBuf1 = (OMX_PTR) pGrallocHandle->fd[1];
626			DOMX_DEBUG("%s Gralloc=0x%x, Y-fd=%d, UV-fd=%d", __FUNCTION__, pGrallocHandle,
627			            pGrallocHandle->fd[0], pGrallocHandle->fd[1]);
628#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
629			if (pProxy->bAndroidOpaqueFormat)
630			{
631                                DOMX_DEBUG(" ++TIMM_OSAL_ReadFromPipe() ");
632				/* Dequeue NV12 buffer for encoder */
633				eOSALStatus = TIMM_OSAL_ReadFromPipe(pProxy->hBufPipe, &nBufIndex,
634						                     sizeof(OMX_PTR), (TIMM_OSAL_U32 *)(&nSize),
635						                     TIMM_OSAL_SUSPEND);
636				PROXY_assert(eOSALStatus == TIMM_OSAL_ERR_NONE, OMX_ErrorBadParameter, NULL);
637
638				/* Get NV12 data after colorconv*/
639				nRet = COLORCONVERT_PlatformOpaqueToNV12(pProxy->hCC, (void **) &pGrallocHandle, (void **) &pProxy->gralloc_handle[nBufIndex],
640									 pGrallocHandle->iWidth,
641									 pGrallocHandle->iHeight,
642									 4096, COLORCONVERT_BUFTYPE_GRALLOCOPAQUE,
643									 COLORCONVERT_BUFTYPE_GRALLOCOPAQUE );
644				if(nRet != 0)
645				{
646					eOSALStatus = TIMM_OSAL_WriteToPipe(pProxy->hBufPipe, (void *) &nBufIndex,
647						                     sizeof(OMX_U32), TIMM_OSAL_SUSPEND);
648					PROXY_assert(0, OMX_ErrorBadParameter, "Color conversion routine failed");
649				}
650                                DOMX_DEBUG(" --COLORCONVERT_PlatformOpaqueToNV12() ");
651
652				/* Update pBufferHdr with NV12 buffers for OMX component */
653				pBufferHdr->pBuffer= pProxy->gralloc_handle[nBufIndex]->fd[0];
654				((OMX_TI_PLATFORMPRIVATE *) pBufferHdr->pPlatformPrivate)->pAuxBuf1 = pProxy->gralloc_handle[nBufIndex]->fd[1];
655			}
656#endif
657#endif
658		}
659		else
660		{
661			return OMX_ErrorBadParameter;
662		}
663	}
664
665	PROXY_EmptyThisBuffer(hComponent, pBufferHdr);
666#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
667	if (pProxy->bAndroidOpaqueFormat)
668	{
669		/*Write buffer to end of pipe for re-circulation for future ETB()*/
670		eOSALStatus = TIMM_OSAL_WriteToPipe(pProxy->hBufPipe, (void *) &nBufIndex,
671					    sizeof(OMX_U32), TIMM_OSAL_SUSPEND);
672		PROXY_assert(eOSALStatus == TIMM_OSAL_ERR_NONE, OMX_ErrorBadParameter, "Pipe write failed");
673	}
674#endif
675	if( pCompPrv->proxyPortBuffers[pBufferHdr->nInputPortIndex].proxyBufferType == EncoderMetadataPointers)
676	{
677		pBufferHdr->pBuffer = pBufferOrig;
678		pBufferHdr->nFilledLen = nFilledLen;
679		pBufferHdr->nAllocLen = nAllocLen;
680	}
681
682	EXIT:
683		return eError;
684}
685
686#ifdef ANDROID_CUSTOM_OPAQUECOLORFORMAT
687static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_AllocateBuffer(OMX_HANDLETYPE hComponent,
688		     OMX_BUFFERHEADERTYPE ** ppBufferHdr, OMX_U32 nPortIndex,
689		     OMX_PTR pAppPrivate, OMX_U32 nSizeBytes)
690{
691	OMX_ERRORTYPE eError = OMX_ErrorNone;
692	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
693	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
694	OMX_CONFIG_RECTTYPE tParamRect;
695	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
696	TIMM_OSAL_ERRORTYPE eOSALStatus = TIMM_OSAL_ERR_NONE;
697        DOMX_DEBUG(" ++LOCAL_PROXY_MPEG4E_AllocateBuffer");
698	int err, nStride;
699
700	PROXY_require(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter,
701	    NULL);
702	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
703	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
704
705	if((nPortIndex == OMX_MPEG4E_INPUT_PORT) &&
706	   (pProxy->bAndroidOpaqueFormat))
707	{
708		tParamRect.nSize = sizeof(OMX_CONFIG_RECTTYPE);
709		tParamRect.nVersion.s.nVersionMajor = 1;
710		tParamRect.nVersion.s.nVersionMinor = 1;
711		tParamRect.nVersion.s.nRevision = 0;
712		tParamRect.nVersion.s.nStep = 0;
713		tParamRect.nPortIndex = nPortIndex;
714
715		eError = PROXY_GetParameter(hComponent, (OMX_INDEXTYPE)OMX_TI_IndexParam2DBufferAllocDimension, &tParamRect);
716 		PROXY_assert(eError == OMX_ErrorNone, eError," Error in Proxy GetParameter from 2d index in allocate buffer");
717
718		err = pProxy->mAllocDev->alloc(pProxy->mAllocDev,(int) tParamRect.nWidth,(int) tParamRect.nHeight,
719			(int) HAL_PIXEL_FORMAT_TI_NV12,(int) GRALLOC_USAGE_HW_RENDER, &(pProxy->gralloc_handle[pProxy->nCurBufIndex]), &nStride);
720	}
721
722	eError = PROXY_AllocateBuffer(hComponent, ppBufferHdr, nPortIndex,
723				      pAppPrivate, nSizeBytes);
724EXIT:
725	if((nPortIndex == OMX_MPEG4E_INPUT_PORT) &&
726	   (pProxy->bAndroidOpaqueFormat))
727	{
728		if(eError != OMX_ErrorNone)
729		{
730			err = pProxy->mAllocDev->free(pProxy->mAllocDev, pProxy->gralloc_handle[pProxy->nCurBufIndex]);
731		}
732		else
733		{
734			/*Populate buffer to pipe*/
735			eOSALStatus = TIMM_OSAL_WriteToPipe(pProxy->hBufPipe, (void *) &pProxy->nCurBufIndex,
736						    sizeof(OMX_U32), TIMM_OSAL_SUSPEND);
737			pProxy->nCurBufIndex++;
738		}
739	}
740        DOMX_DEBUG(" --LOCAL_PROXY_MPEG4E_AllocateBuffer");
741	return eError;
742}
743
744static OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_FreeBuffer(OMX_IN OMX_HANDLETYPE hComponent,
745    OMX_IN OMX_U32 nPortIndex, OMX_IN OMX_BUFFERHEADERTYPE * pBufferHdr)
746{
747	OMX_ERRORTYPE eError = OMX_ErrorNone;
748	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
749	PROXY_COMPONENT_PRIVATE *pCompPrv = NULL;
750	OMX_U32 nBufIndex, nSize, nCount=0;
751	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
752
753	PROXY_require(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter,
754	    NULL);
755	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
756	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
757
758	if((nPortIndex == OMX_MPEG4E_INPUT_PORT) &&
759	   (pProxy->bAndroidOpaqueFormat))
760	{
761		pProxy->nCurBufIndex--;
762		PROXY_require(pProxy->nCurBufIndex >=0,
763			      OMX_ErrorBadParameter, "Buffer index underflow");
764
765		if(pProxy->gralloc_handle[pProxy->nCurBufIndex])
766		{
767			pProxy->mAllocDev->free(pProxy->mAllocDev, pProxy->gralloc_handle[pProxy->nCurBufIndex]);
768			pProxy->gralloc_handle[pProxy->nCurBufIndex] = NULL;
769		}
770
771		/*Clear the Bufindex pipe by dummy reads*/
772		TIMM_OSAL_GetPipeReadyMessageCount(pProxy->hBufPipe, (TIMM_OSAL_U32 *)&nCount);
773		if(nCount)
774		{
775			TIMM_OSAL_ReadFromPipe(pProxy->hBufPipe, &nBufIndex,
776					       sizeof(OMX_PTR), (TIMM_OSAL_U32 *)&nSize, TIMM_OSAL_NO_SUSPEND);
777		}
778	}
779
780	eError = PROXY_FreeBuffer(hComponent, nPortIndex, pBufferHdr);
781
782EXIT:
783	return eError;
784}
785
786OMX_ERRORTYPE LOCAL_PROXY_MPEG4E_ComponentDeInit(OMX_HANDLETYPE hComponent)
787{
788	OMX_ERRORTYPE eError = OMX_ErrorNone;
789	PROXY_COMPONENT_PRIVATE *pCompPrv;
790	OMX_COMPONENTTYPE *hComp = (OMX_COMPONENTTYPE *) hComponent;
791	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
792	TIMM_OSAL_ERRORTYPE eOSALStatus = TIMM_OSAL_ERR_NONE;
793	OMX_U32 i;
794
795	PROXY_require(hComp->pComponentPrivate != NULL, OMX_ErrorBadParameter,
796	    NULL);
797	pCompPrv = (PROXY_COMPONENT_PRIVATE *) hComp->pComponentPrivate;
798	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
799
800	if(pProxy->hBufPipe != NULL)
801	{
802		eOSALStatus = TIMM_OSAL_DeletePipe(pProxy->hBufPipe);
803		pProxy->hBufPipe = NULL;
804
805		if(eOSALStatus != TIMM_OSAL_ERR_NONE)
806		{
807			DOMX_ERROR("Pipe deletion failed");
808		}
809	}
810
811	if(pProxy->bAndroidOpaqueFormat == OMX_TRUE)
812	{
813		/* Cleanup internal buffers in pipe if not freed on FreeBuffer */
814		for(i=0; i<OMX_MPEG4E_NUM_INTERNAL_BUF; i++)
815		{
816			if(pProxy->gralloc_handle[i])
817			{
818				pProxy->mAllocDev->free(pProxy->mAllocDev, pProxy->gralloc_handle[i]);
819				pProxy->gralloc_handle[i] = NULL;
820			}
821		}
822
823		COLORCONVERT_close(pProxy->hCC,pCompPrv);
824		pProxy->bAndroidOpaqueFormat = OMX_FALSE;
825
826		if(pCompPrv->pCompProxyPrv != NULL)
827		{
828			TIMM_OSAL_Free(pCompPrv->pCompProxyPrv);
829			pCompPrv->pCompProxyPrv = NULL;
830		}
831	}
832
833	eError = PROXY_ComponentDeInit(hComponent);
834EXIT:
835	DOMX_EXIT("eError: %d", eError);
836	return eError;
837}
838
839int COLORCONVERT_open(void **hCC, PROXY_COMPONENT_PRIVATE *pCompPrv)
840{
841	int nErr = -1;
842	hw_module_t const* module = NULL;
843	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
844
845	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
846	nErr = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
847
848	if (nErr == 0)
849	{
850		*hCC = (void *) ((IMG_gralloc_module_public_t const *)module);
851	}
852	else
853	{
854		 DOMX_ERROR("FATAL: gralloc api hw_get_module() returned error: Can't find \
855			    %s module err = %x", GRALLOC_HARDWARE_MODULE_ID, nErr);
856	}
857
858	gralloc_open(module, &(pProxy->mAllocDev));
859
860	return nErr;
861}
862
863int COLORCONVERT_PlatformOpaqueToNV12(void *hCC,
864				      void *pSrc[COLORCONVERT_MAX_SUB_BUFFERS],
865				      void *pDst[COLORCONVERT_MAX_SUB_BUFFERS],
866				      int nWidth, int nHeight, int nStride,
867				      int nSrcBufType,int nDstBufType)
868{
869	IMG_gralloc_module_public_t const* module = hCC;
870	int nErr = -1;
871
872	if((nSrcBufType == COLORCONVERT_BUFTYPE_GRALLOCOPAQUE) && (nDstBufType == COLORCONVERT_BUFTYPE_VIRTUAL))
873	{
874		nErr = module->Blit(module, pSrc[0], pDst, HAL_PIXEL_FORMAT_TI_NV12);
875
876	}
877	else if((nSrcBufType == COLORCONVERT_BUFTYPE_GRALLOCOPAQUE) && (nDstBufType == COLORCONVERT_BUFTYPE_GRALLOCOPAQUE ))
878	{
879		nErr = module->Blit2(module, pSrc[0], pDst[0], nWidth, nHeight, 0, 0);
880	}
881
882	return nErr;
883}
884
885int COLORCONVERT_close(void *hCC,PROXY_COMPONENT_PRIVATE *pCompPrv)
886{
887	OMX_PROXY_MPEG4E_PRIVATE *pProxy = NULL;
888	pProxy = (OMX_PROXY_MPEG4E_PRIVATE *) pCompPrv->pCompProxyPrv;
889	if(pProxy && pProxy->mAllocDev)
890	{
891		gralloc_close(pProxy->mAllocDev);
892	}
893	return 0;
894}
895#endif
896