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