OMX_VideoEncoder.c revision fb3766f18a2c18b6f4798a6a631fdb88fcacd1dc
1
2/*
3 * Copyright (C) Texas Instruments - http://www.ti.com/
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21/* =============================================================================
22*             Texas Instruments OMAP(TM) Platform Software
23*  (c) Copyright Texas Instruments, Incorporated.  All Rights Reserved.
24*
25*  Use of this software is controlled by the terms and conditions found
26*  in the license agreement under which this software has been supplied.
27* ============================================================================*/
28/**
29* @file OMX_VideoEncoder.c
30*
31* This file implements OMX Component for MPEG-4 encoder that
32* is fully compliant with the OMX specification 1.5.
33*
34* @path  $(CSLPATH)\src
35*
36* @rev  0.1
37*/
38/* ---------------------------------------------------------------------------*/
39/* =============================================================================
40*!
41*! Revision History
42*! =============================================================================
43*!
44*! 24-Jul-2005 mf: Revisions appear in reverse chronological order;
45*! that is, newest first.  The date format is dd-Mon-yyyy.
46* ============================================================================*/
47
48/* ------compilation control switches ----------------------------------------*/
49/******************************************************************************
50*  INCLUDE FILES
51*******************************************************************************/
52/* ----- system and platform files -------------------------------------------*/
53#ifdef UNDER_CE
54    #include <windows.h>
55    #include <oaf_osal.h>
56    #include <omx_core.h>
57#else
58    #include <wchar.h>
59    #include <unistd.h>
60    #include <sys/time.h>
61    #include <sys/types.h>
62    #include <sys/ioctl.h>
63    #include <sys/select.h>
64    #include <errno.h>
65    #include <pthread.h>
66	#include <dlfcn.h>
67#endif
68
69#include <string.h>
70#include <fcntl.h>
71#include <stdlib.h>
72#include <stdio.h>
73#include <dbapi.h>
74
75/*------- Program Header Files -----------------------------------------------*/
76#include "OMX_VideoEnc_Utils.h"
77#include "OMX_VideoEnc_DSP.h"
78#include "OMX_VideoEnc_Thread.h"
79
80#ifdef RESOURCE_MANAGER_ENABLED
81    #include <ResourceManagerProxyAPI.h>
82#endif
83#ifdef UNDER_CE
84    extern HINSTANCE g_hLcmlDllHandle;
85#endif
86/******************************************************************************
87*  EXTERNAL REFERENCES NOTE : only use if not found in header file
88*******************************************************************************/
89/*--------data declarations --------------------------------------------------*/
90/*--------function prototypes ------------------------------------------------*/
91
92/******************************************************************************
93*  PUBLIC DECLARATIONS Defined here, used elsewhere
94*******************************************************************************/
95/*--------data declarations --------------------------------------------------*/
96/*--------function prototypes ------------------------------------------------*/
97#ifndef UNDER_CE
98    OMX_ERRORTYPE OMX_ComponentInit(OMX_HANDLETYPE hComp);
99#else
100    #define OMX_EXPORT __declspec(dllexport)
101    OMX_EXPORT OMX_ERRORTYPE OMX_ComponentInit (OMX_HANDLETYPE hComp);
102#endif
103
104/******************************************************************************
105*  PRIVATE DECLARATIONS Defined here, used only here
106*******************************************************************************/
107/*--------data declarations --------------------------------------------------*/
108#ifdef UNDER_CE
109           static pthread_t ComponentThread;
110#endif
111/*--------macro definitions --------------------------------------------------*/
112#define OMX_CONVERT_STATE(_s_, _p_)        \
113    if (_p_ == 0) {                        \
114        _s_ = "OMX_StateInvalid";          \
115    }                                      \
116    else if (_p_ == 1) {                   \
117        _s_ = "OMX_StateLoaded";           \
118    }                                      \
119    else if (_p_ == 2) {                   \
120        _s_ = "OMX_StateIdle";             \
121    }                                      \
122    else if (_p_ == 3) {                   \
123        _s_ = "OMX_StateExecuting";        \
124    }                                      \
125    else if (_p_ == 4) {                   \
126        _s_ = "OMX_StatePause";            \
127    }                                      \
128    else if (_p_ == 5) {                   \
129        _s_ = "OMX_StateWaitForResources"; \
130    }                                      \
131    else {                                 \
132        _s_ = "UnsupportedCommand";        \
133    }
134
135#define OMX_CONVERT_CMD(_s_, _p_)          \
136    if (_p_ == 0) {                        \
137        _s_ = "OMX_CommandStateSet";       \
138    }                                      \
139    else if (_p_ == 1) {                   \
140        _s_ = "OMX_CommandFlush";          \
141    }                                      \
142    else if (_p_ == 2) {                   \
143        _s_ = "OMX_CommandPortDisable";    \
144    }                                      \
145    else if (_p_ == 3) {                   \
146        _s_ = "OMX_CommandPortEnable";     \
147    }                                      \
148    else if (_p_ == 4) {                   \
149        _s_ = "OMX_CommandMarkBuffer";     \
150    }                                      \
151    else {                                 \
152        _s_ = "UnsupportedCommand";        \
153    }
154
155/*--------function prototypes ------------------------------------------------*/
156static OMX_ERRORTYPE SetCallbacks (OMX_IN OMX_HANDLETYPE hComponent,
157                                   OMX_IN OMX_CALLBACKTYPE* pCallBacks,
158                                   OMX_IN OMX_PTR pAppData);
159
160static OMX_ERRORTYPE GetComponentVersion (OMX_HANDLETYPE hComponent,
161                                          OMX_STRING  szComponentName,
162                                          OMX_VERSIONTYPE* pComponentVersion,
163                                          OMX_VERSIONTYPE* pSpecVersion,
164                                          OMX_UUIDTYPE* pComponentUUID);
165
166static OMX_ERRORTYPE SendCommand (OMX_IN OMX_HANDLETYPE hComponent,
167                                  OMX_IN OMX_COMMANDTYPE Cmd,
168                                  OMX_IN OMX_U32 nParam1,
169                                  OMX_IN OMX_PTR pCmdData);
170
171static OMX_ERRORTYPE GetParameter (OMX_IN OMX_HANDLETYPE hComponent,
172                                   OMX_IN OMX_INDEXTYPE nParamIndex,
173                                   OMX_INOUT OMX_PTR CompParamStruct);
174
175static OMX_ERRORTYPE SetParameter (OMX_IN OMX_HANDLETYPE hComponent,
176                                   OMX_IN OMX_INDEXTYPE nParamIndex,
177                                   OMX_IN OMX_PTR CompParamStruct);
178
179static OMX_ERRORTYPE GetConfig (OMX_HANDLETYPE hComponent,
180                                OMX_INDEXTYPE nConfigIndex,
181                                OMX_PTR ComponentConfigStructure);
182
183static OMX_ERRORTYPE SetConfig (OMX_HANDLETYPE hComponent,
184                                OMX_INDEXTYPE nConfigIndex,
185                                OMX_PTR ComponentConfigStructure);
186
187static OMX_ERRORTYPE EmptyThisBuffer (OMX_IN OMX_HANDLETYPE hComponent,
188                                      OMX_IN OMX_BUFFERHEADERTYPE* pBuffer);
189
190static OMX_ERRORTYPE FillThisBuffer (OMX_IN OMX_HANDLETYPE hComponent,
191                                     OMX_IN OMX_BUFFERHEADERTYPE* pBuffer);
192
193static OMX_ERRORTYPE GetState (OMX_IN OMX_HANDLETYPE hComponent,
194                               OMX_OUT OMX_STATETYPE* pState);
195
196static OMX_ERRORTYPE ComponentTunnelRequest (OMX_IN OMX_HANDLETYPE hComponent,
197                                             OMX_IN OMX_U32 nPort,
198                                             OMX_IN OMX_HANDLETYPE hTunneledComp,
199                                             OMX_IN  OMX_U32 nTunneledPort,
200                                             OMX_INOUT OMX_TUNNELSETUPTYPE* pTunnelSetup);
201
202static OMX_ERRORTYPE UseBuffer (OMX_IN OMX_HANDLETYPE hComponent,
203                                OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
204                                OMX_IN OMX_U32 nPortIndex,
205                                OMX_IN OMX_PTR pAppPrivate,
206                                OMX_IN OMX_U32 nSizeBytes,
207                                OMX_IN OMX_U8* pBuffer);
208
209static OMX_ERRORTYPE AllocateBuffer (OMX_IN OMX_HANDLETYPE hComponent,
210                                     OMX_INOUT OMX_BUFFERHEADERTYPE** pBuffer,
211                                     OMX_IN OMX_U32 nPortIndex,
212                                     OMX_IN OMX_PTR pAppPrivate,
213                                     OMX_IN OMX_U32 nSizeBytes);
214
215static OMX_ERRORTYPE FreeBuffer (OMX_IN OMX_HANDLETYPE hComponent,
216                                 OMX_IN OMX_U32 nPortIndex,
217                                 OMX_IN OMX_BUFFERHEADERTYPE* pBuffer);
218
219static OMX_ERRORTYPE ComponentDeInit (OMX_IN OMX_HANDLETYPE hComponent);
220
221static OMX_ERRORTYPE VerifyTunnelConnection (VIDEOENC_PORT_TYPE* pPort,
222                                             OMX_HANDLETYPE hTunneledComp,
223                                             OMX_PARAM_PORTDEFINITIONTYPE* pPortDef,
224                                             struct OMX_TI_Debug *dbg);
225
226static OMX_ERRORTYPE ExtensionIndex (OMX_IN OMX_HANDLETYPE hComponent,
227                                     OMX_IN OMX_STRING cParameterName,
228                                     OMX_OUT OMX_INDEXTYPE* pIndexType);
229
230#ifdef __KHRONOS_CONF_1_1__
231static OMX_ERRORTYPE ComponentRoleEnum(OMX_IN OMX_HANDLETYPE hComponent,
232                                       OMX_OUT OMX_U8 *cRole,
233                                       OMX_IN OMX_U32 nIndex);
234#endif
235/*----------------------------------------------------------------------------*/
236/**
237  * OMX_ComponentInit() Set the all the function pointers of component
238  *
239  * This method will update the component function pointer to the handle
240  *
241  * @param hComp         handle for this instance of the component
242  *
243  * @retval OMX_NoError              Success, ready to roll
244  *         OMX_ErrorInsufficientResources If the malloc fails
245  **/
246/*----------------------------------------------------------------------------*/
247
248#ifdef UNDER_CE
249OMX_EXPORT
250#endif
251OMX_ERRORTYPE OMX_ComponentInit (OMX_HANDLETYPE hComponent)
252{
253    OMX_COMPONENTTYPE* pHandle                  = NULL;
254    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
255    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
256    VIDEOENC_PORT_TYPE* pCompPortIn             = NULL;
257    VIDEOENC_PORT_TYPE* pCompPortOut            = NULL;
258    OMX_PARAM_PORTDEFINITIONTYPE* pPortDef      = NULL;
259    OMX_VIDEO_PARAM_PORTFORMATTYPE* pPortFormat = NULL;
260    OMX_PRIORITYMGMTTYPE* pPriorityMgmt         = NULL;
261    VIDENC_NODE* pMemoryListHead                = NULL;
262    struct OMX_TI_Debug dbg;
263
264    OMX_S32 nError = 0;
265    OMX_U32 i = 0;
266	char* sDynamicFormat;
267#ifdef UNDER_CE
268    pthread_attr_t attr;
269    memset(&attr, 0, sizeof(attr));
270#endif
271
272
273    /* get default settings for debug */
274    OMX_DBG_INIT(dbg, "OMX_DBG_VIDENC");
275
276	/*dlopen("libLCML.so", RTLD_LAZY);*/
277    OMX_PRINT2(dbg, "Enter to ComponetInit\n");
278    if (!hComponent)
279    {
280        eError = OMX_ErrorBadParameter;
281        goto OMX_CONF_CMD_BAIL;
282    }
283    pHandle = (OMX_COMPONENTTYPE*)hComponent;
284    eError = OMX_VIDENC_ListCreate(&dbg, &pMemoryListHead);
285    OMX_CONF_BAIL_IF_ERROR(eError);
286
287    /* Allocate memory for component's private data area */
288    VIDENC_MALLOC(pHandle->pComponentPrivate,
289                  sizeof(VIDENC_COMPONENT_PRIVATE),
290                  VIDENC_COMPONENT_PRIVATE,
291                  pMemoryListHead, dbg);
292
293    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
294    pComponentPrivate->pMemoryListHead = pMemoryListHead;
295
296    pComponentPrivate->dbg = dbg;
297    pComponentPrivate->compressionFormats[0]=OMX_VIDEO_CodingAVC;
298    pComponentPrivate->compressionFormats[1]=OMX_VIDEO_CodingMPEG4;
299    pComponentPrivate->compressionFormats[2]=OMX_VIDEO_CodingH263;
300
301#ifdef __PERF_INSTRUMENTATION__
302    pComponentPrivate->pPERF = PERF_Create(PERF_FOURCC('V','E',' ',' '),
303                                           PERF_ModuleLLMM |
304                                           PERF_ModuleVideoEncode);
305#endif
306
307    pComponentPrivate->bDeblockFilter       = OMX_TRUE;
308    pComponentPrivate->nVBVSize             = 120;
309    pComponentPrivate->bForceIFrame         = OMX_FALSE;
310    pComponentPrivate->nIntraFrameInterval  = 15;
311    pComponentPrivate->nQPI                 = 12;
312    pComponentPrivate->nAIRRate             = 0;
313    pComponentPrivate->bHideEvents          = OMX_FALSE;
314    pComponentPrivate->bHandlingFatalError  = OMX_FALSE;
315    pComponentPrivate->bUnresponsiveDsp     = OMX_FALSE;
316    pComponentPrivate->bCodecLoaded         = OMX_FALSE;
317    pComponentPrivate->cComponentName		= "OMX.TI.Video.encoder";
318
319#ifdef __KHRONOS_CONF__
320    pComponentPrivate->bPassingIdleToLoaded = OMX_FALSE;
321    pComponentPrivate->bErrorLcmlHandle     = OMX_FALSE;
322#endif
323
324    /*Initialize Circular Buffer*/
325    OMX_CONF_CIRCULAR_BUFFER_INIT(pComponentPrivate);
326
327    /* ASO/FMO*/
328    pComponentPrivate->numSliceASO = 0;
329    for( i=0; i<MAXNUMSLCGPS;i++)
330    {
331        pComponentPrivate->asoSliceOrder[i] = 0;
332    }
333    pComponentPrivate->numSliceGroups                = 0;
334    pComponentPrivate->sliceGroupMapType             = 0;
335    pComponentPrivate->sliceGroupChangeDirectionFlag = 0;
336    pComponentPrivate->sliceGroupChangeRate          = 0;
337    pComponentPrivate->sliceGroupChangeCycle         = 0;
338    for( i=0; i<MAXNUMSLCGPS;i++)
339    {
340        pComponentPrivate->sliceGroupParams[i] = 0;
341    }
342
343    /*Assigning address of Component Structure point to place holder inside
344       component private structure
345    */
346    ((VIDENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate)->pHandle = pHandle;
347
348    /* fill in function pointers */
349    pHandle->SetCallbacks           = SetCallbacks;
350    pHandle->GetComponentVersion    = GetComponentVersion;
351    pHandle->SendCommand            = SendCommand;
352    pHandle->GetParameter           = GetParameter;
353    pHandle->SetParameter           = SetParameter;
354    pHandle->GetConfig              = GetConfig;
355    pHandle->SetConfig              = SetConfig;
356    pHandle->GetExtensionIndex      = ExtensionIndex;
357    pHandle->GetState               = GetState;
358    pHandle->ComponentTunnelRequest = ComponentTunnelRequest;
359    pHandle->UseBuffer              = UseBuffer;
360    pHandle->AllocateBuffer         = AllocateBuffer;
361    pHandle->FreeBuffer             = FreeBuffer;
362    pHandle->EmptyThisBuffer        = EmptyThisBuffer;
363    pHandle->FillThisBuffer         = FillThisBuffer;
364    pHandle->ComponentDeInit        = ComponentDeInit;
365#ifdef __KHRONOS_CONF_1_1__
366    pHandle->ComponentRoleEnum      = ComponentRoleEnum;
367
368sDynamicFormat = getenv("FORMAT");
369/*printf("\n ** FORMAT = %s\n\n", sDynamicFormat);*/
370#if 1
371    if (sDynamicFormat != NULL)
372    {
373        if ( strcmp(sDynamicFormat,  "MPEG4") == 0 )
374        {
375
376    strcpy((char *)pComponentPrivate->componentRole.cRole, "video_encoder.mpeg4");
377		}
378        else if (strcmp(sDynamicFormat,  "H263") == 0 )
379        {
380    strcpy((char *)pComponentPrivate->componentRole.cRole, "video_encoder.h263");
381		}
382        else if (strcmp(sDynamicFormat,  "H264") == 0 )
383        {
384    strcpy((char *)pComponentPrivate->componentRole.cRole, "video_encoder.avc");
385		}
386		}
387    else
388    {
389    strcpy((char *)pComponentPrivate->componentRole.cRole, "video_encoder.avc");
390		}
391#else
392    strcpy((char *)pComponentPrivate->componentRole.cRole, "VideoEncode");
393/*    strcpy((char *)pComponentPrivate->componentRole.cRole, "video_encoder.mpeg4");*/
394#endif
395#endif
396
397    /* Allocate memory for component data structures */
398    VIDENC_MALLOC(pComponentPrivate->pPortParamType,
399                  sizeof(OMX_PORT_PARAM_TYPE),
400                  OMX_PORT_PARAM_TYPE,
401                  pMemoryListHead, dbg);
402#ifdef __KHRONOS_CONF_1_1__
403    VIDENC_MALLOC(pComponentPrivate->pPortAudioType,
404                  sizeof(OMX_PORT_PARAM_TYPE),
405                  OMX_PORT_PARAM_TYPE,
406                  pMemoryListHead, dbg);
407    VIDENC_MALLOC(pComponentPrivate->pPortImageType,
408              sizeof(OMX_PORT_PARAM_TYPE),
409              OMX_PORT_PARAM_TYPE,
410                  pMemoryListHead, dbg);
411    VIDENC_MALLOC(pComponentPrivate->pPortOtherType,
412                  sizeof(OMX_PORT_PARAM_TYPE),
413                  OMX_PORT_PARAM_TYPE,
414                  pMemoryListHead, dbg);
415#endif
416
417
418    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_INPUT_PORT],
419                  sizeof(VIDEOENC_PORT_TYPE),
420                  VIDEOENC_PORT_TYPE,
421                  pMemoryListHead, dbg);
422    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT],
423                  sizeof(VIDEOENC_PORT_TYPE),
424                  VIDEOENC_PORT_TYPE,
425                  pMemoryListHead, dbg);
426    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef,
427                  sizeof(OMX_PARAM_PORTDEFINITIONTYPE),
428                  OMX_PARAM_PORTDEFINITIONTYPE,
429                  pMemoryListHead, dbg);
430    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef,
431                  sizeof(OMX_PARAM_PORTDEFINITIONTYPE),
432                  OMX_PARAM_PORTDEFINITIONTYPE,
433                  pMemoryListHead, dbg);
434    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortFormat,
435                  sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE),
436                  OMX_VIDEO_PARAM_PORTFORMATTYPE,
437                  pMemoryListHead, dbg);
438    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortFormat,
439                  sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE),
440                  OMX_VIDEO_PARAM_PORTFORMATTYPE,
441                  pMemoryListHead, dbg);
442#ifdef __KHRONOS_CONF_1_1__
443    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pProfileType,
444                  sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE),
445                  OMX_VIDEO_PARAM_PROFILELEVELTYPE,
446                  pMemoryListHead, dbg);
447    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pProfileType,
448                  sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE),
449                  OMX_VIDEO_PARAM_PROFILELEVELTYPE,
450                  pMemoryListHead, dbg);
451    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pBitRateTypeConfig,
452                  sizeof(OMX_VIDEO_CONFIG_BITRATETYPE),
453                  OMX_VIDEO_CONFIG_BITRATETYPE,
454                  pMemoryListHead, dbg);
455    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateTypeConfig,
456                  sizeof(OMX_VIDEO_CONFIG_BITRATETYPE),
457                  OMX_VIDEO_CONFIG_BITRATETYPE,
458                  pMemoryListHead, dbg);
459    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pFrameRateConfig,
460                  sizeof(OMX_CONFIG_FRAMERATETYPE),
461                  OMX_CONFIG_FRAMERATETYPE,
462                  pMemoryListHead, dbg);
463    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pFrameRateConfig,
464                  sizeof(OMX_CONFIG_FRAMERATETYPE),
465                  OMX_CONFIG_FRAMERATETYPE,
466                  pMemoryListHead, dbg);
467    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pErrorCorrectionType,
468                  sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE),
469                  OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE,
470                  pMemoryListHead, dbg);
471	VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pIntraRefreshType,
472                  sizeof(OMX_VIDEO_PARAM_INTRAREFRESHTYPE),
473                  OMX_VIDEO_PARAM_INTRAREFRESHTYPE,
474                  pMemoryListHead, dbg);
475#endif
476    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pBitRateType,
477                  sizeof(OMX_VIDEO_PARAM_BITRATETYPE),
478                  OMX_VIDEO_PARAM_BITRATETYPE,
479                  pMemoryListHead, dbg);
480    VIDENC_MALLOC(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateType,
481                  sizeof(OMX_VIDEO_PARAM_BITRATETYPE),
482                  OMX_VIDEO_PARAM_BITRATETYPE,
483                  pMemoryListHead, dbg);
484    VIDENC_MALLOC(pComponentPrivate->pPriorityMgmt,
485                  sizeof(OMX_PRIORITYMGMTTYPE),
486                  OMX_PRIORITYMGMTTYPE,
487                  pMemoryListHead, dbg);
488    VIDENC_MALLOC(pComponentPrivate->pH264,
489                  sizeof(OMX_VIDEO_PARAM_AVCTYPE),
490                  OMX_VIDEO_PARAM_AVCTYPE,
491                  pMemoryListHead, dbg);
492    VIDENC_MALLOC(pComponentPrivate->pMpeg4,
493                  sizeof(OMX_VIDEO_PARAM_MPEG4TYPE),
494                  OMX_VIDEO_PARAM_MPEG4TYPE,
495                  pMemoryListHead, dbg);
496    VIDENC_MALLOC(pComponentPrivate->pH263,
497                  sizeof(OMX_VIDEO_PARAM_H263TYPE),
498                  OMX_VIDEO_PARAM_H263TYPE,
499                  pMemoryListHead, dbg);
500    VIDENC_MALLOC(pComponentPrivate->pVidParamBitrate,
501                  sizeof(OMX_VIDEO_PARAM_BITRATETYPE),
502                  OMX_VIDEO_PARAM_BITRATETYPE,
503                  pMemoryListHead, dbg);
504    VIDENC_MALLOC(pComponentPrivate->pQuantization,
505                  sizeof(OMX_VIDEO_PARAM_QUANTIZATIONTYPE),
506                  OMX_VIDEO_PARAM_QUANTIZATIONTYPE,
507                  pMemoryListHead, dbg);
508    VIDENC_MALLOC(pComponentPrivate->pH264IntraPeriod,
509                  sizeof(OMX_VIDEO_CONFIG_AVCINTRAPERIOD),
510                  OMX_VIDEO_CONFIG_AVCINTRAPERIOD,
511                  pMemoryListHead, dbg);
512    VIDENC_MALLOC(pComponentPrivate->pMotionVector,
513                  sizeof(OMX_VIDEO_PARAM_MOTIONVECTORTYPE),
514                  OMX_VIDEO_PARAM_MOTIONVECTORTYPE,
515                  pMemoryListHead, dbg);
516    VIDENC_MALLOC(pComponentPrivate->pCapabilityFlags,
517                  sizeof(PV_OMXComponentCapabilityFlagsType),
518                  PV_OMXComponentCapabilityFlagsType,
519                  pMemoryListHead, dbg);
520
521    /* Set pPortParamType defaults */
522    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamType, OMX_PORT_PARAM_TYPE);
523    pComponentPrivate->pPortParamType->nPorts = VIDENC_NUM_OF_PORTS;
524    pComponentPrivate->pPortParamType->nStartPortNumber = VIDENC_INPUT_PORT;
525
526#ifdef __KHRONOS_CONF_1_1__
527    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortAudioType, OMX_PORT_PARAM_TYPE);
528    pComponentPrivate->pPortAudioType->nPorts = 0;
529    pComponentPrivate->pPortAudioType->nStartPortNumber = -1;
530
531    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortImageType, OMX_PORT_PARAM_TYPE);
532    pComponentPrivate->pPortImageType->nPorts = 0;
533    pComponentPrivate->pPortImageType->nStartPortNumber = -1;
534
535    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortOtherType, OMX_PORT_PARAM_TYPE);
536    pComponentPrivate->pPortOtherType->nPorts = 0;
537    pComponentPrivate->pPortOtherType->nStartPortNumber = -1;
538
539#endif
540
541	pCompPortIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT];
542
543    /* Set input port defaults */
544
545    pPortDef = pCompPortIn->pPortDef;
546    OMX_CONF_INIT_STRUCT(pPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
547    pPortDef->nPortIndex                         = VIDENC_INPUT_PORT;
548    pPortDef->eDir                               = OMX_DirInput;
549    pPortDef->nBufferCountActual                 = VIDENC_NUM_OF_IN_BUFFERS;
550    pPortDef->nBufferCountMin                    = 1;
551    pPortDef->nBufferSize                        = 38016;
552    pPortDef->bEnabled                           = OMX_TRUE;
553    pPortDef->bPopulated                         = OMX_FALSE;
554    pPortDef->eDomain                            = OMX_PortDomainVideo;
555    pPortDef->format.video.cMIMEType             = "yuv";
556    pPortDef->format.video.pNativeRender         = NULL;
557    pPortDef->format.video.nFrameWidth           = 176;
558    pPortDef->format.video.nFrameHeight          = 144;
559    pPortDef->format.video.nStride               = -1;
560    pPortDef->format.video.nSliceHeight          = -1;
561    pPortDef->format.video.xFramerate            = 15;
562    pPortDef->format.video.bFlagErrorConcealment = OMX_FALSE;
563    pPortDef->format.video.eCompressionFormat    = OMX_VIDEO_CodingUnused;
564    pPortDef->format.video.eColorFormat          = OMX_COLOR_FormatYUV420Planar;
565
566    /* Set the default value of the run-time Target Frame Rate to the create-time Frame Rate */
567    pComponentPrivate->nTargetFrameRate = pPortDef->format.video.xFramerate;
568
569
570    for (i = 0; i < VIDENC_MAX_NUM_OF_IN_BUFFERS; i++)
571    {
572        VIDENC_MALLOC(pCompPortIn->pBufferPrivate[i],
573                      sizeof(VIDENC_BUFFER_PRIVATE),
574                      VIDENC_BUFFER_PRIVATE,
575                      pMemoryListHead, dbg);
576    }
577    for (i = 0; i < VIDENC_MAX_NUM_OF_IN_BUFFERS; i++)
578    {
579        pCompPortIn->pBufferPrivate[i]->pBufferHdr = NULL;
580    }
581    pCompPortIn->nBufferCnt = 0;
582
583    /* Set output port defaults */
584    pCompPortOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
585    pPortDef = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
586    OMX_CONF_INIT_STRUCT(pPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
587    pPortDef->nPortIndex                         = VIDENC_OUTPUT_PORT;
588    pPortDef->eDir                               = OMX_DirOutput;
589    pPortDef->nBufferCountActual                 = VIDENC_NUM_OF_OUT_BUFFERS;
590    pPortDef->nBufferCountMin                    = 1;
591    pPortDef->nBufferSize                        = 220000;
592    pPortDef->bEnabled                           = OMX_TRUE;
593    pPortDef->bPopulated                         = OMX_FALSE;
594    pPortDef->eDomain                            = OMX_PortDomainVideo;
595    pPortDef->format.video.cMIMEType             = "264";
596    pPortDef->format.video.pNativeRender         = NULL;
597    pPortDef->format.video.nFrameWidth           = 176;
598    pPortDef->format.video.nFrameHeight          = 144;
599    pPortDef->format.video.nStride               = -1;
600    pPortDef->format.video.nSliceHeight          = -1;
601    pPortDef->format.video.nBitrate              = 64000;
602    pPortDef->format.video.xFramerate            = (15<<16);
603    pPortDef->format.video.bFlagErrorConcealment = OMX_FALSE;
604
605    if (sDynamicFormat != NULL)
606    {
607        if ( strcmp(sDynamicFormat,  "MPEG4") == 0 )
608        {
609
610		pPortDef->format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
611		}
612        else if (strcmp(sDynamicFormat,  "H263") == 0 )
613        {
614
615		pPortDef->format.video.eCompressionFormat  = OMX_VIDEO_CodingH263;
616		}
617        else if (strcmp(sDynamicFormat,  "H264") == 0 )
618        {
619		pPortDef->format.video.eCompressionFormat    = OMX_VIDEO_CodingAVC;
620		}
621		}
622    else
623    {
624		pPortDef->format.video.eCompressionFormat    = OMX_VIDEO_CodingAVC;
625		}
626
627    pPortDef->format.video.eColorFormat          = OMX_COLOR_FormatUnused;
628
629    /* Set the default value of the run-time Target Bit Rate to the create-time Bit Rate */
630    pComponentPrivate->nTargetBitRate = pPortDef->format.video.nBitrate;
631
632    for (i = 0; i < VIDENC_MAX_NUM_OF_OUT_BUFFERS; i++)
633    {
634        VIDENC_MALLOC(pCompPortOut->pBufferPrivate[i],
635                      sizeof(VIDENC_BUFFER_PRIVATE),
636                      VIDENC_BUFFER_PRIVATE,
637                      pMemoryListHead, pComponentPrivate->dbg);
638    }
639    for (i = 0; i < VIDENC_MAX_NUM_OF_OUT_BUFFERS; i++)
640    {
641        pCompPortOut->pBufferPrivate[i]->pBufferHdr = NULL;
642    }
643		/*allocate MPEG4 metadata structure*/
644		for (i = 0; i < VIDENC_MAX_NUM_OF_OUT_BUFFERS; i++)
645		  {
646			VIDENC_MALLOC(pCompPortOut->pBufferPrivate[i]->pMetaData,
647						  sizeof(VIDENC_MPEG4_SEGMENTMODE_METADATA),
648						  VIDENC_MPEG4_SEGMENTMODE_METADATA,
649                      pMemoryListHead, pComponentPrivate->dbg);
650		  }
651
652		/*segment mode defaults*/
653		pComponentPrivate->bMVDataEnable=OMX_FALSE;
654		pComponentPrivate->bResyncDataEnable=OMX_FALSE;
655	pCompPortOut->nBufferCnt = 0;
656
657    /* Set input port format defaults */
658    pPortFormat = pCompPortIn->pPortFormat;
659    OMX_CONF_INIT_STRUCT(pPortFormat, OMX_VIDEO_PARAM_PORTFORMATTYPE);
660    pPortFormat->nPortIndex         = VIDENC_INPUT_PORT;
661    pPortFormat->nIndex             = 0x0;
662    pPortFormat->eCompressionFormat = OMX_VIDEO_CodingUnused;
663    pPortFormat->eColorFormat       = OMX_COLOR_FormatYUV420Planar;
664
665    /* Set output port format defaults */
666    pPortFormat = pCompPortOut->pPortFormat;
667    OMX_CONF_INIT_STRUCT(pPortFormat, OMX_VIDEO_PARAM_PORTFORMATTYPE);
668    pPortFormat->nPortIndex         = VIDENC_OUTPUT_PORT;
669    pPortFormat->nIndex             = 0x0;
670
671    if (sDynamicFormat != NULL)
672    {
673        if ( strcmp(sDynamicFormat,  "MPEG4") == 0 )
674        {
675            pPortFormat->eCompressionFormat = OMX_VIDEO_CodingMPEG4;
676        }
677        else if (strcmp(sDynamicFormat,  "H263") == 0 )
678        {
679            pPortFormat->eCompressionFormat = OMX_VIDEO_CodingH263;
680        }
681        else if (strcmp(sDynamicFormat,  "H264") == 0 )
682        {
683            pPortFormat->eCompressionFormat = OMX_VIDEO_CodingAVC;
684        }
685    }
686    else
687    {
688    pPortFormat->eCompressionFormat = OMX_VIDEO_CodingAVC;
689		}
690
691
692    pPortFormat->eColorFormat       = OMX_COLOR_FormatUnused;
693
694    /* Set pPriorityMgmt defaults */
695    pPriorityMgmt = pComponentPrivate->pPriorityMgmt;
696    OMX_CONF_INIT_STRUCT(pPriorityMgmt, OMX_PRIORITYMGMTTYPE);
697    pPriorityMgmt->nGroupPriority   = -1;
698    pPriorityMgmt->nGroupID         = -1;
699
700    /* Buffer supplier setting */
701    pCompPortIn->eSupplierSetting = OMX_BufferSupplyOutput;
702
703    /* Set pH264 defaults */
704    OMX_CONF_INIT_STRUCT(pComponentPrivate->pH264, OMX_VIDEO_PARAM_AVCTYPE);
705    pComponentPrivate->pH264->nPortIndex                = VIDENC_OUTPUT_PORT;
706    pComponentPrivate->pH264->nSliceHeaderSpacing       = 0;
707    pComponentPrivate->pH264->nPFrames                  = -1;
708    pComponentPrivate->pH264->nBFrames                  = -1;
709    pComponentPrivate->pH264->bUseHadamard              = OMX_TRUE; /*OMX_FALSE*/
710    pComponentPrivate->pH264->nRefFrames                = 1; /*-1;  */
711    pComponentPrivate->pH264->nRefIdx10ActiveMinus1     = -1;
712    pComponentPrivate->pH264->nRefIdx11ActiveMinus1     = -1;
713    pComponentPrivate->pH264->bEnableUEP                = OMX_FALSE;
714    pComponentPrivate->pH264->bEnableFMO                = OMX_FALSE;
715    pComponentPrivate->pH264->bEnableASO                = OMX_FALSE;
716    pComponentPrivate->pH264->bEnableRS                 = OMX_FALSE;
717    pComponentPrivate->pH264->eProfile                  = OMX_VIDEO_AVCProfileBaseline; /*0x01;*/
718    pComponentPrivate->pH264->eLevel                    = OMX_VIDEO_AVCLevel1; /*OMX_VIDEO_AVCLevel11; */
719    pComponentPrivate->pH264->nAllowedPictureTypes      = -1;
720    pComponentPrivate->pH264->bFrameMBsOnly             = OMX_FALSE;
721    pComponentPrivate->pH264->bMBAFF                    = OMX_FALSE;
722    pComponentPrivate->pH264->bEntropyCodingCABAC       = OMX_FALSE;
723    pComponentPrivate->pH264->bWeightedPPrediction      = OMX_FALSE;
724    pComponentPrivate->pH264->nWeightedBipredicitonMode = -1;
725    pComponentPrivate->pH264->bconstIpred               = OMX_FALSE;
726    pComponentPrivate->pH264->bDirect8x8Inference       = OMX_FALSE;
727    pComponentPrivate->pH264->bDirectSpatialTemporal    = OMX_FALSE;
728    pComponentPrivate->pH264->nCabacInitIdc             = -1;
729    pComponentPrivate->pH264->eLoopFilterMode           = 1;
730	/*other h264 defaults*/
731	pComponentPrivate->intra4x4EnableIdc                = INTRA4x4_IPSLICES;
732	pComponentPrivate->maxMVperMB                       = 4;
733	pComponentPrivate->nEncodingPreset	                = 3;/*0:DEFAULT/ 1:HIGH QUALITY/ 2:HIGH SPEED/ 3:USER DEFINED*/
734   pComponentPrivate->AVCNALFormat						= VIDENC_AVC_NAL_SLICE;/*VIDENC_AVC_NAL_UNIT;*/
735    /* Set pMpeg4 defaults */
736    OMX_CONF_INIT_STRUCT(pComponentPrivate->pMpeg4, OMX_VIDEO_PARAM_MPEG4TYPE);
737    pComponentPrivate->pMpeg4->nPortIndex           = VIDENC_OUTPUT_PORT;
738    pComponentPrivate->pMpeg4->nSliceHeaderSpacing  = 0;
739    pComponentPrivate->pMpeg4->bSVH                 = OMX_FALSE;
740    pComponentPrivate->pMpeg4->bGov                 = OMX_FALSE;
741    pComponentPrivate->pMpeg4->nPFrames             = -1;
742    pComponentPrivate->pMpeg4->nBFrames             = -1;
743    pComponentPrivate->pMpeg4->nIDCVLCThreshold     = 0;  /*-1*/
744    pComponentPrivate->pMpeg4->bACPred              = OMX_TRUE;
745    pComponentPrivate->pMpeg4->nMaxPacketSize       = -1;
746    pComponentPrivate->pMpeg4->nTimeIncRes          = -1;
747    pComponentPrivate->pMpeg4->eProfile             = 0x01;
748#ifdef __KHRONOS_CONF_1_1__
749    pComponentPrivate->pMpeg4->eLevel               = OMX_VIDEO_MPEG4Level1;
750#else
751    pComponentPrivate->pMpeg4->eLevel               = 0x0;
752#endif
753    pComponentPrivate->pMpeg4->nAllowedPictureTypes = -1;
754    pComponentPrivate->pMpeg4->nHeaderExtension     = 0;
755    pComponentPrivate->pMpeg4->bReversibleVLC       = OMX_FALSE;
756
757    /* Set pH263 defaults */
758    OMX_CONF_INIT_STRUCT(pComponentPrivate->pH263, OMX_VIDEO_PARAM_H263TYPE);
759    pComponentPrivate->pH263->nPortIndex               = VIDENC_OUTPUT_PORT;
760    pComponentPrivate->pH263->nPFrames                 = 0;
761    pComponentPrivate->pH263->nBFrames                 = 0;
762    pComponentPrivate->pH263->eProfile                 = OMX_VIDEO_H263ProfileBaseline;
763    pComponentPrivate->pH263->eLevel                   = OMX_VIDEO_H263Level10;
764    pComponentPrivate->pH263->bPLUSPTYPEAllowed        = OMX_FALSE;
765    pComponentPrivate->pH263->nAllowedPictureTypes     = 0;
766    pComponentPrivate->pH263->bForceRoundingTypeToZero = OMX_TRUE;
767    pComponentPrivate->pH263->nPictureHeaderRepetition = 0;
768    pComponentPrivate->pH263->nGOBHeaderInterval       = 1;
769
770    /* Set pVidParamBitrate and intraRefreshType defaults */
771    OMX_CONF_INIT_STRUCT(pCompPortOut->pErrorCorrectionType, OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE);
772	OMX_CONF_INIT_STRUCT(pCompPortOut->pIntraRefreshType, OMX_VIDEO_PARAM_INTRAREFRESHTYPE);
773    pCompPortOut->pErrorCorrectionType->nPortIndex= VIDENC_OUTPUT_PORT;
774	pCompPortOut->pIntraRefreshType->nPortIndex= VIDENC_OUTPUT_PORT;
775	/*initDSP params*/
776	/*Error resilience tools used by MPEG4/H263 encoder*/
777	pCompPortOut->pErrorCorrectionType->bEnableHEC= OMX_TRUE;/*shouldn't be 0?*/
778	pCompPortOut->pErrorCorrectionType->bEnableResync = OMX_TRUE;/*shouldn't be 0?*/
779	pCompPortOut->pErrorCorrectionType->bEnableDataPartitioning= OMX_FALSE;
780	pCompPortOut->pErrorCorrectionType->bEnableRVLC= OMX_FALSE;
781	pCompPortOut->pErrorCorrectionType->nResynchMarkerSpacing = 1024;
782
783	pCompPortOut->pIntraRefreshType->nAirRef = 10;
784    /* Set pVidParamBitrate defaults */
785    OMX_CONF_INIT_STRUCT(pComponentPrivate->pVidParamBitrate, OMX_VIDEO_PARAM_BITRATETYPE);
786    pComponentPrivate->pVidParamBitrate->nPortIndex     = VIDENC_OUTPUT_PORT;
787    pComponentPrivate->pVidParamBitrate->eControlRate   = OMX_Video_ControlRateConstant;
788    pComponentPrivate->pVidParamBitrate->nTargetBitrate = 64000;
789	/**/
790	pComponentPrivate->nMIRRate=0;
791    /* Set pQuantization defaults */
792    OMX_CONF_INIT_STRUCT(pComponentPrivate->pQuantization, OMX_VIDEO_PARAM_QUANTIZATIONTYPE);
793    pComponentPrivate->pQuantization->nPortIndex = VIDENC_OUTPUT_PORT;
794    pComponentPrivate->pQuantization->nQpI       = 12;
795    pComponentPrivate->pQuantization->nQpP       = 0;
796    pComponentPrivate->pQuantization->nQpB       = 0;
797
798    /* Set pMotionVector defaults */
799    OMX_CONF_INIT_STRUCT(pComponentPrivate->pMotionVector, OMX_VIDEO_PARAM_MOTIONVECTORTYPE);
800    pComponentPrivate->pMotionVector->nPortIndex = VIDENC_OUTPUT_PORT;
801    pComponentPrivate->pMotionVector->bFourMV    = 1;
802    pComponentPrivate->pMotionVector->bUnrestrictedMVs = 0;   /* unused */
803    pComponentPrivate->pMotionVector->eAccuracy  = OMX_Video_MotionVectorQuarterPel;
804    pComponentPrivate->pMotionVector->sXSearchRange = pComponentPrivate->pMotionVector->sXSearchRange = 64;
805
806    /* Set pIntraPeriod defaults */
807    OMX_CONF_INIT_STRUCT(pComponentPrivate->pH264IntraPeriod, OMX_VIDEO_CONFIG_AVCINTRAPERIOD);
808    pComponentPrivate->pH264IntraPeriod->nPortIndex = VIDENC_OUTPUT_PORT;
809    pComponentPrivate->pH264IntraPeriod->nIDRPeriod = 0;
810    pComponentPrivate->pH264IntraPeriod->nPFrames = 30;
811
812#ifdef __KHRONOS_CONF_1_1__
813	OMX_CONF_INIT_STRUCT(pCompPortIn->pProfileType, OMX_VIDEO_PARAM_PROFILELEVELTYPE);
814	pCompPortIn->pProfileType->nPortIndex = VIDENC_INPUT_PORT;
815	pCompPortIn->pProfileType->eLevel = OMX_VIDEO_AVCLevel1;
816	pCompPortIn->pProfileType->eProfile = OMX_VIDEO_AVCProfileBaseline;
817	pCompPortIn->pProfileType->nProfileIndex = 0;
818
819	OMX_CONF_INIT_STRUCT(pCompPortOut->pProfileType, OMX_VIDEO_PARAM_PROFILELEVELTYPE);
820	pCompPortOut->pProfileType->nPortIndex = VIDENC_OUTPUT_PORT;
821	pCompPortOut->pProfileType->eLevel = OMX_VIDEO_AVCLevel1;
822	pCompPortOut->pProfileType->eProfile = OMX_VIDEO_AVCProfileBaseline;
823	pCompPortOut->pProfileType->nProfileIndex = 0;
824	OMX_CONF_INIT_STRUCT(pCompPortIn->pFrameRateConfig, OMX_CONFIG_FRAMERATETYPE);
825	pCompPortIn->pFrameRateConfig->nPortIndex = VIDENC_INPUT_PORT;
826	pCompPortIn->pFrameRateConfig->xEncodeFramerate = 0;
827
828	OMX_CONF_INIT_STRUCT(pCompPortOut->pFrameRateConfig, OMX_CONFIG_FRAMERATETYPE);
829	pCompPortOut->pFrameRateConfig->nPortIndex = VIDENC_OUTPUT_PORT;
830	pCompPortOut->pFrameRateConfig->xEncodeFramerate = (15<<16);
831
832	OMX_CONF_INIT_STRUCT(pCompPortIn->pBitRateTypeConfig, OMX_VIDEO_CONFIG_BITRATETYPE);
833	pCompPortIn->pBitRateTypeConfig->nPortIndex = VIDENC_INPUT_PORT;
834	pCompPortIn->pBitRateTypeConfig->nEncodeBitrate = 0;
835
836	OMX_CONF_INIT_STRUCT(pCompPortOut->pBitRateTypeConfig, OMX_VIDEO_CONFIG_BITRATETYPE);
837	pCompPortOut->pBitRateTypeConfig->nPortIndex = VIDENC_OUTPUT_PORT;
838	pCompPortOut->pBitRateTypeConfig->nEncodeBitrate = 64000;
839
840#endif
841	OMX_CONF_INIT_STRUCT(pCompPortIn->pBitRateType, OMX_VIDEO_PARAM_BITRATETYPE);
842	pCompPortIn->pBitRateType->nPortIndex = VIDENC_INPUT_PORT;
843	pCompPortIn->pBitRateType->eControlRate = OMX_Video_ControlRateDisable;
844	pCompPortIn->pBitRateType->nTargetBitrate = 64000;
845
846	OMX_CONF_INIT_STRUCT(pCompPortOut->pBitRateType, OMX_VIDEO_PARAM_BITRATETYPE);
847	pCompPortOut->pBitRateType->nPortIndex = VIDENC_OUTPUT_PORT;
848	pCompPortOut->pBitRateType->eControlRate = OMX_Video_ControlRateConstant;
849	pCompPortOut->pBitRateType->nTargetBitrate = 64000;
850
851    /*set the capability Flags needed by Opencore*/
852    pComponentPrivate->pCapabilityFlags->iIsOMXComponentMultiThreaded=OMX_TRUE;
853    pComponentPrivate->pCapabilityFlags->iOMXComponentCanHandleIncompleteFrames=OMX_FALSE;
854    pComponentPrivate->pCapabilityFlags->iOMXComponentSupportsExternalInputBufferAlloc=OMX_FALSE;
855    pComponentPrivate->pCapabilityFlags->iOMXComponentSupportsExternalOutputBufferAlloc=OMX_FALSE;
856    pComponentPrivate->pCapabilityFlags->iOMXComponentSupportsMovableInputBuffers=OMX_FALSE;
857    pComponentPrivate->pCapabilityFlags->iOMXComponentSupportsPartialFrames=OMX_FALSE;
858    pComponentPrivate->pCapabilityFlags->iOMXComponentUsesFullAVCFrames=OMX_FALSE;
859    pComponentPrivate->pCapabilityFlags->iOMXComponentUsesNALStartCode=OMX_FALSE;
860
861#ifndef UNDER_CE
862    /* Initialize Mutex for Buffer Tracking */
863    pthread_mutex_init(&(pComponentPrivate->mVideoEncodeBufferMutex), NULL);
864#else
865    /* Add WinCE critical section API */
866#endif
867
868    /* create the pipe used to maintain free input buffers*/
869    eError = pipe(pComponentPrivate->nFree_oPipe);
870    if (eError)
871    {
872        OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
873    }
874
875    /* create the pipe used to maintain input buffers*/
876    eError = pipe(pComponentPrivate->nFilled_iPipe);
877    if (eError)
878    {
879        OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
880    }
881
882    /* create the pipe used to send commands to the thread */
883    eError = pipe(pComponentPrivate->nCmdPipe);
884    if (eError)
885    {
886        OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
887    }
888
889    /* create the pipe used to send commands to the thread */
890    eError = pipe(pComponentPrivate->nCmdDataPipe);
891    if (eError)
892    {
893        OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
894    }
895
896#ifdef RESOURCE_MANAGER_ENABLED
897    /* Initialize Resource Manager */
898    eError = RMProxy_NewInitalizeEx(OMX_COMPONENTTYPE_VIDEO);
899    if (eError != OMX_ErrorNone)
900    {
901        OMX_PRMGR4(dbg, "Error returned from loading ResourceManagerProxy thread...\n");
902        OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
903    }
904#endif
905    /* Create the Component Thread */
906#ifdef UNDER_CE
907    attr.__inheritsched = PTHREAD_EXPLICIT_SCHED;
908    attr.__schedparam.__sched_priority = OMX_VIDEO_ENCODER_THREAD_PRIORITY;
909    nError = pthread_create(&ComponentThread, &attr, OMX_VIDENC_Thread, pComponentPrivate);
910#else
911    nError = pthread_create(&pComponentPrivate->ComponentThread, NULL, OMX_VIDENC_Thread, pComponentPrivate);
912#endif
913
914
915#ifndef UNDER_CE
916    if (nError == EAGAIN)
917    {
918        OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
919    }
920#else
921    if (nError || !(pComponentPrivate->ComponentThread))
922    {
923        OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
924    }
925#endif
926
927#ifdef __PERF_INSTRUMENTATION__
928    PERF_ThreadCreated(pComponentPrivate->pPERF, pComponentPrivate->ComponentThread,
929                       PERF_FOURCC('V','E',' ','T'));
930#endif
931#ifndef UNDER_CE
932/*    pthread_mutex_init(&pComponentPrivate->videoe_mutex_app, NULL);*/
933    pthread_mutex_init(&pComponentPrivate->videoe_mutex, NULL);
934    pthread_cond_init (&pComponentPrivate->populate_cond, NULL);
935    pthread_mutex_init(&pComponentPrivate->videoe_mutex_app, NULL);
936    pthread_cond_init (&pComponentPrivate->unpopulate_cond, NULL);
937	pthread_cond_init (&pComponentPrivate->flush_cond, NULL);
938	pthread_cond_init (&pComponentPrivate->stop_cond, NULL);
939
940#else
941    OMX_CreateEvent(&(pComponentPrivate->InLoaded_event));
942    OMX_CreateEvent(&(pComponentPrivate->InIdle_event));
943#endif
944OMX_CONF_CMD_BAIL:
945    OMX_PRINT2(dbg, "Component Init Exit\n");
946    return eError;
947}
948
949/*----------------------------------------------------------------------------*/
950/**
951  *  SetCallbacks() Sets application callbacks to the component
952  *
953  * This method will update application callbacks
954  * the application.
955  *
956  * @param pComp         handle for this instance of the component
957  * @param pCallBacks    application callbacks
958  * @param ptr
959  *
960  * @retval OMX_NoError              Success, ready to roll
961  *         OMX_Error_BadParameter   The input parameter pointer is null
962  **/
963/*----------------------------------------------------------------------------*/
964
965static OMX_ERRORTYPE SetCallbacks (OMX_IN  OMX_HANDLETYPE hComponent,
966                                   OMX_IN  OMX_CALLBACKTYPE* pCallBacks,
967                                   OMX_IN  OMX_PTR pAppData)
968{
969    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
970    OMX_COMPONENTTYPE* pHandle                  = NULL;
971    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
972    OMX_U32* pTmp                               = NULL;
973
974    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
975    OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
976    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pCallBacks, 1, 1);
977
978    /*Copy the callbacks of the application to the component private */
979    pTmp = memcpy (&(pComponentPrivate->sCbData), pCallBacks, sizeof(OMX_CALLBACKTYPE));
980    if (pTmp == NULL)
981    {
982        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined, pComponentPrivate->dbg,
983                               OMX_TRACE4, "Failed to copy callbacks.\n");
984    }
985
986    /*copy the application private data to component memory*/
987    pHandle = (OMX_COMPONENTTYPE*)hComponent;
988    pHandle->pApplicationPrivate = pAppData;
989    pComponentPrivate->eState = OMX_StateLoaded;
990
991OMX_CONF_CMD_BAIL:
992    return eError;
993}
994
995/*----------------------------------------------------------------------------*/
996/**
997  *  GetComponentVersion() Sets application callbacks to the component
998  *
999  * This method will update application callbacks
1000  * the application.
1001  *
1002  * @param pComp         handle for this instance of the component
1003  * @param pCallBacks    application callbacks
1004  * @param ptr
1005  *
1006  * @retval OMX_NoError              Success, ready to roll
1007  *         OMX_Error_BadParameter   The input parameter pointer is null
1008  **/
1009/*----------------------------------------------------------------------------*/
1010
1011static OMX_ERRORTYPE GetComponentVersion (OMX_HANDLETYPE hComp,
1012                                          OMX_STRING  szComponentName,
1013                                          OMX_VERSIONTYPE* pComponentVersion,
1014                                          OMX_VERSIONTYPE* pSpecVersion,
1015                                          OMX_UUIDTYPE* pComponentUUID)
1016{
1017    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
1018    OMX_COMPONENTTYPE* pHandle                  = NULL;
1019    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
1020
1021    OMX_CONF_CHECK_CMD(hComp, ((OMX_COMPONENTTYPE *) hComp)->pComponentPrivate, 1);
1022    pHandle = (OMX_COMPONENTTYPE*)hComp;
1023    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
1024
1025    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, szComponentName, pComponentVersion, pSpecVersion);
1026    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pComponentUUID, 1, 1);
1027
1028    if (pComponentPrivate->eState == OMX_StateInvalid)
1029    {
1030        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
1031                               pComponentPrivate->dbg, OMX_PRSTATE3,
1032                               "Component is in invalid state.\n");
1033    }
1034
1035    strcpy(szComponentName, pComponentPrivate->cComponentName);
1036    memcpy(pComponentVersion,
1037           &(pComponentPrivate->ComponentVersion.s),
1038           sizeof(pComponentPrivate->ComponentVersion.s));
1039    memcpy(pSpecVersion,
1040           &(pComponentPrivate->SpecVersion.s),
1041           sizeof(pComponentPrivate->SpecVersion.s));
1042
1043OMX_CONF_CMD_BAIL:
1044    return eError;
1045}
1046
1047/*----------------------------------------------------------------------------*/
1048/**
1049  *  SendCommand() Sets application callbacks to the component
1050  *
1051  * This method will update application callbacks
1052  * the application.
1053  *
1054  * @param pComp         handle for this instance of the component
1055  * @param pCallBacks    application callbacks
1056  * @param ptr
1057  *
1058  * @retval OMX_NoError              Success, ready to roll
1059  *         OMX_Error_BadParameter   The input parameter pointer is null
1060  **/
1061/*----------------------------------------------------------------------------*/
1062
1063static OMX_ERRORTYPE SendCommand (OMX_IN OMX_HANDLETYPE hComponent,
1064                                  OMX_IN OMX_COMMANDTYPE Cmd,
1065                                  OMX_IN OMX_U32 nParam1,
1066                                  OMX_IN OMX_PTR pCmdData)
1067{
1068    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
1069    int nRet                                    = 0;
1070    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
1071    char* szCommandType                         = NULL;
1072    char* szParam                               = NULL;
1073    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn    = NULL;
1074    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut   = NULL;
1075    VIDENC_NODE* pMemoryListHead                = NULL;
1076
1077    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
1078
1079    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
1080    if (Cmd == OMX_CommandMarkBuffer)
1081    {
1082        OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pCmdData, 1, 1);
1083    }
1084
1085    if (pComponentPrivate->eState == OMX_StateInvalid)
1086    {
1087        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
1088                               pComponentPrivate->dbg, OMX_PRSTATE3,
1089                               "Component is in invalid state.\n");
1090    }
1091
1092    pMemoryListHead = pComponentPrivate->pMemoryListHead;
1093
1094#ifdef __PERF_INSTRUMENTATION__
1095    PERF_SendingCommand(pComponentPrivate->pPERF,
1096                        Cmd,
1097                        (Cmd == OMX_CommandMarkBuffer) ? ((OMX_U32) pCmdData) : nParam1,
1098                        PERF_ModuleComponent);
1099#endif
1100
1101    switch (Cmd)
1102    {
1103        case OMX_CommandStateSet:
1104
1105#ifdef __KHRONOS_CONF__
1106            if(nParam1 == OMX_StateLoaded &&
1107               pComponentPrivate->eState == OMX_StateIdle)
1108            {
1109                pComponentPrivate->bPassingIdleToLoaded = OMX_TRUE;
1110            }
1111#endif
1112        OMX_PRCOMM2(pComponentPrivate->dbg, "Write to cmd pipe!\n");
1113            nRet = write(pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
1114            if (nRet == -1)
1115            {
1116            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1117                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1118                                   "Failed to write to cmd pipe.\n");
1119            }
1120            nRet = write(pComponentPrivate->nCmdDataPipe[1],
1121                         &nParam1,
1122                         sizeof(nParam1));
1123            if (nRet == -1)
1124            {
1125            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1126                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1127                                   "Failed to write to cmd pipe.\n");
1128            }
1129            break;
1130        case OMX_CommandFlush:
1131            if (nParam1 > 1 && nParam1 != -1)
1132            {
1133            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadPortIndex,
1134                                   pComponentPrivate->dbg, OMX_PRBUFFER4,
1135                                   "Invalid flush nParam1 (%lu).\n", nParam1);
1136            }
1137            nRet = write(pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
1138            if (nRet == -1)
1139            {
1140            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1141                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1142                                   "Failed to write to cmd pipe.\n");
1143            }
1144            nRet = write(pComponentPrivate->nCmdDataPipe[1],
1145                       &nParam1,
1146                       sizeof(nParam1));
1147            if (nRet == -1)
1148            {
1149            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1150                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1151                                   "Failed to write to cmd pipe.\n");
1152            }
1153            break;
1154        case OMX_CommandPortDisable:
1155
1156            pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
1157            pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
1158
1159            if (nParam1 == VIDENC_INPUT_PORT ||
1160                nParam1 == VIDENC_OUTPUT_PORT ||
1161                nParam1 == -1)
1162            {
1163                if (nParam1 == VIDENC_INPUT_PORT || nParam1 == -1)
1164                {
1165
1166                    pPortDefIn->bEnabled = OMX_FALSE;
1167                }
1168                if (nParam1 == VIDENC_OUTPUT_PORT || nParam1 == -1)
1169                {
1170
1171                    pPortDefOut->bEnabled = OMX_FALSE;
1172                }
1173            }
1174            else
1175            {
1176            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter,
1177                                   pComponentPrivate->dbg, OMX_PRBUFFER4,
1178                                   "Invalid port disable nParam1 (%lu).\n", nParam1);
1179            }
1180
1181            nRet = write(pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
1182            if (nRet == -1)
1183            {
1184            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1185                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1186                                   "Failed to write to cmd pipe.\n");
1187            }
1188            nRet = write(pComponentPrivate->nCmdDataPipe[1],
1189                         &nParam1,
1190                         sizeof(nParam1));
1191            if (nRet == -1)
1192            {
1193            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1194                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1195                                   "Failed to write to cmd pipe.\n");
1196            }
1197            break;
1198        case OMX_CommandPortEnable:
1199            pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
1200            pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
1201
1202            if (nParam1 == VIDENC_INPUT_PORT ||
1203                nParam1 == VIDENC_OUTPUT_PORT ||
1204                nParam1 == -1)
1205            {
1206                if (nParam1 == VIDENC_INPUT_PORT || nParam1 == -1)
1207                {
1208                    pPortDefIn->bEnabled = OMX_TRUE;
1209                }
1210                if (nParam1 == VIDENC_OUTPUT_PORT || nParam1 == -1)
1211                {
1212                    pPortDefOut->bEnabled = OMX_TRUE;
1213                }
1214            }
1215            else
1216            {
1217            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter,
1218                                   pComponentPrivate->dbg, OMX_PRBUFFER4,
1219                                   "Invalid port enable nParam1 (%lu).\n", nParam1);
1220            }
1221
1222            nRet = write(pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
1223            if (nRet == -1)
1224            {
1225            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1226                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1227                                   "Failed to write to cmd pipe.\n");
1228            }
1229            nRet = write(pComponentPrivate->nCmdDataPipe[1],
1230                         &nParam1,
1231                         sizeof(nParam1));
1232            if (nRet == -1)
1233            {
1234            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1235                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1236                                   "Failed to write to cmd pipe.\n");
1237            }
1238            break;
1239        case OMX_CommandMarkBuffer:
1240            if (nParam1 > 0)
1241            {
1242            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter,
1243                                   pComponentPrivate->dbg, OMX_PRBUFFER4,
1244                                   "Invalid mark nParam1 (%lu).\n", nParam1);
1245            }
1246            nRet = write(pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
1247            if (nRet == -1)
1248            {
1249            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1250                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1251                                   "Failed to write to cmd pipe.\n");
1252            }
1253            nRet = write(pComponentPrivate->nCmdDataPipe[1],
1254                         &pCmdData,
1255                         sizeof(pCmdData));
1256            if (nRet == -1)
1257            {
1258            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1259                                   pComponentPrivate->dbg, OMX_PRCOMM4,
1260                                   "Failed to write to cmd pipe.\n");
1261            }
1262            break;
1263        case OMX_CommandMax:
1264            break;
1265        default:
1266        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1267                               pComponentPrivate->dbg, OMX_PRCOMM4,
1268                               "Invalid command (%x).\n", Cmd);
1269    }
1270
1271    OMX_CONVERT_CMD(szCommandType, Cmd);
1272    if (Cmd == OMX_CommandStateSet)
1273    {
1274        OMX_CONVERT_STATE(szParam, nParam1);
1275        OMX_PRSTATE2(pComponentPrivate->dbg, "%s -> %s\n", szCommandType, szParam);
1276    }
1277
1278OMX_CONF_CMD_BAIL:
1279    return eError;
1280}
1281
1282/*----------------------------------------------------------------------------*/
1283/**
1284  *  GetParameter() Sets application callbacks to the component
1285  *
1286  * This method will update application callbacks
1287  * the application.
1288  *
1289  * @param pComp         handle for this instance of the component
1290  * @param pCallBacks    application callbacks
1291  * @param ptr
1292  *
1293  * @retval OMX_NoError              Success, ready to roll
1294  *         OMX_Error_BadParameter   The input parameter pointer is null
1295  **/
1296/*----------------------------------------------------------------------------*/
1297
1298static OMX_ERRORTYPE GetParameter (OMX_IN OMX_HANDLETYPE hComponent,
1299                                   OMX_IN OMX_INDEXTYPE nParamIndex,
1300                                   OMX_INOUT OMX_PTR ComponentParameterStructure)
1301{
1302    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
1303    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
1304    OMX_U32* pTmp                               = NULL;
1305    VIDEOENC_PORT_TYPE* pCompPortIn             = NULL;
1306    VIDEOENC_PORT_TYPE* pCompPortOut            = NULL;
1307
1308    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
1309
1310    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
1311    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, ComponentParameterStructure, 1, 1);
1312
1313    if (pComponentPrivate->eState == OMX_StateInvalid)
1314    {
1315        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
1316                               pComponentPrivate->dbg, OMX_PRSTATE3,
1317                               "Component is in invalid state.\n");
1318    }
1319
1320    pCompPortIn     = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT];
1321    pCompPortOut    = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
1322
1323    switch (nParamIndex)
1324    {
1325        case OMX_IndexParamVideoInit:
1326            pTmp = memcpy(ComponentParameterStructure,
1327                          pComponentPrivate->pPortParamType,
1328                          sizeof(OMX_PORT_PARAM_TYPE));
1329            if (pTmp == NULL)
1330            {
1331            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1332                                   pComponentPrivate->dbg, OMX_TRACE4,
1333                                   "Failed to copy parameter.\n");
1334            }
1335            break;
1336#ifdef __KHRONOS_CONF_1_1__
1337
1338        case OMX_IndexParamImageInit:
1339            pTmp = memcpy(ComponentParameterStructure,
1340                          pComponentPrivate->pPortImageType,
1341                          sizeof(OMX_PORT_PARAM_TYPE));
1342            if (pTmp == NULL)
1343            {
1344            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1345                                   pComponentPrivate->dbg, OMX_TRACE4,
1346                                   "Failed to copy parameter.\n");
1347            }
1348            break;
1349
1350        case OMX_IndexParamAudioInit:
1351            pTmp = memcpy(ComponentParameterStructure,
1352                          pComponentPrivate->pPortAudioType,
1353                          sizeof(OMX_PORT_PARAM_TYPE));
1354            if (pTmp == NULL)
1355            {
1356            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1357                                   pComponentPrivate->dbg, OMX_TRACE4,
1358                                   "Failed to copy parameter.\n");
1359            }
1360            break;
1361
1362        case OMX_IndexParamOtherInit:
1363            pTmp = memcpy(ComponentParameterStructure,
1364                          pComponentPrivate->pPortOtherType,
1365                          sizeof(OMX_PORT_PARAM_TYPE));
1366            if (pTmp == NULL)
1367            {
1368            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1369                                   pComponentPrivate->dbg, OMX_TRACE4,
1370                                   "Failed to copy parameter.\n");
1371            }
1372            break;
1373
1374#endif
1375
1376        case OMX_IndexParamPortDefinition:
1377		{
1378            if (((OMX_PARAM_PORTDEFINITIONTYPE*)(ComponentParameterStructure))->nPortIndex ==
1379                pCompPortIn->pPortDef->nPortIndex)
1380            {
1381                pTmp = memcpy(ComponentParameterStructure,
1382                              pCompPortIn->pPortDef,
1383                              sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1384                if (pTmp == NULL)
1385                {
1386                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1387                                           pComponentPrivate->dbg, OMX_TRACE4,
1388                                           "Failed to copy parameter.\n");
1389                }
1390            }
1391            else if (((OMX_PARAM_PORTDEFINITIONTYPE*)(ComponentParameterStructure))->nPortIndex ==
1392                pCompPortOut->pPortDef->nPortIndex)
1393            {
1394                pTmp = memcpy(ComponentParameterStructure,
1395                              pCompPortOut->pPortDef,
1396                              sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1397                if (pTmp == NULL)
1398                {
1399                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1400                                           pComponentPrivate->dbg, OMX_TRACE4,
1401                                           "Failed to copy parameter.\n");
1402                }
1403            }
1404            else
1405            {
1406                eError = OMX_ErrorBadPortIndex;
1407            }
1408            break;
1409        	}
1410        case OMX_IndexParamVideoPortFormat:
1411            if (((OMX_VIDEO_PARAM_PORTFORMATTYPE*)(ComponentParameterStructure))->nPortIndex ==
1412                pCompPortIn->pPortFormat->nPortIndex)
1413        {
1414                if (((OMX_VIDEO_PARAM_PORTFORMATTYPE*)(ComponentParameterStructure))->nIndex ==
1415                    pCompPortIn->pPortFormat->nIndex)
1416                {
1417                ((OMX_VIDEO_PARAM_PORTFORMATTYPE*)(ComponentParameterStructure))->eColorFormat = pCompPortIn->pPortFormat->eColorFormat;
1418                    eError = OMX_ErrorNone;
1419                }
1420                else
1421                {
1422
1423                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorNoMore,
1424                                           pComponentPrivate->dbg, OMX_TRACE4,
1425                                           "No such index.\n");
1426                }
1427            }
1428            else if (((OMX_VIDEO_PARAM_PORTFORMATTYPE*)(ComponentParameterStructure))->nPortIndex ==
1429                     pCompPortOut->pPortFormat->nPortIndex)
1430            {
1431                if (((OMX_VIDEO_PARAM_PORTFORMATTYPE*)(ComponentParameterStructure))->nIndex >= 0 &&
1432                    ((OMX_VIDEO_PARAM_PORTFORMATTYPE*)(ComponentParameterStructure))->nIndex < 3)
1433                {
1434/*                    OMX_TRACE("OMX_IndexParamVideoPortFormat index found\n");*/
1435                    ((OMX_VIDEO_PARAM_PORTFORMATTYPE*)(ComponentParameterStructure))->eCompressionFormat =
1436                    pComponentPrivate->compressionFormats[((OMX_VIDEO_PARAM_PORTFORMATTYPE*)(ComponentParameterStructure))->nIndex];
1437                    eError = OMX_ErrorNone;
1438                }
1439                else
1440                {
1441                    /*OMX_TRACE("OMX_IndexParamVideoPortFormat OMX_ErrorNoMore, no such index\n");*/
1442                    OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorNoMore);
1443                }
1444            }
1445            else
1446        {
1447                eError = OMX_ErrorBadPortIndex;
1448            }
1449            break;
1450        case OMX_IndexParamPriorityMgmt:
1451            pTmp = memcpy(ComponentParameterStructure,
1452                          pComponentPrivate->pPriorityMgmt,
1453                          sizeof(OMX_PRIORITYMGMTTYPE));
1454            if (pTmp == NULL)
1455            {
1456            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1457                                   pComponentPrivate->dbg, OMX_TRACE4,
1458                                   "Failed to copy parameter.\n");
1459            }
1460            break;
1461        case OMX_IndexParamVideoAvc:
1462            if (((OMX_VIDEO_PARAM_AVCTYPE*)(ComponentParameterStructure))->nPortIndex ==
1463                pComponentPrivate->pH264->nPortIndex)
1464            {
1465                pTmp = memcpy(ComponentParameterStructure,
1466                              pComponentPrivate->pH264,
1467                              sizeof(OMX_VIDEO_PARAM_AVCTYPE));
1468                if (pTmp == NULL)
1469                {
1470                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1471                                       pComponentPrivate->dbg, OMX_TRACE4,
1472                                       "Failed to copy parameter.\n");
1473                }
1474            }
1475            else
1476            {
1477                eError = OMX_ErrorBadPortIndex;
1478            }
1479            break;
1480        case OMX_IndexParamVideoMpeg4:
1481            if (((OMX_VIDEO_PARAM_MPEG4TYPE*)(ComponentParameterStructure))->nPortIndex ==
1482                pComponentPrivate->pMpeg4->nPortIndex)
1483            {
1484                pTmp = memcpy(ComponentParameterStructure,
1485                              pComponentPrivate->pMpeg4,
1486                              sizeof(OMX_VIDEO_PARAM_MPEG4TYPE));
1487                if (pTmp == NULL)
1488                {
1489                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1490                                       pComponentPrivate->dbg, OMX_TRACE4,
1491                                       "Failed to copy parameter.\n");
1492                }
1493            }
1494            else
1495            {
1496                eError = OMX_ErrorBadPortIndex;
1497            }
1498            break;
1499        case OMX_IndexParamCompBufferSupplier:
1500        {
1501            OMX_PARAM_BUFFERSUPPLIERTYPE* pBuffSupplierParam = (OMX_PARAM_BUFFERSUPPLIERTYPE*)ComponentParameterStructure;
1502            if (pBuffSupplierParam->nPortIndex == VIDENC_INPUT_PORT)
1503            {
1504                pBuffSupplierParam->eBufferSupplier = pCompPortIn->eSupplierSetting;
1505            }
1506            else if (pBuffSupplierParam->nPortIndex == VIDENC_OUTPUT_PORT)
1507            {
1508                pBuffSupplierParam->eBufferSupplier = pCompPortOut->eSupplierSetting;
1509            }
1510            else
1511            {
1512                eError = OMX_ErrorBadPortIndex;
1513            }
1514            break;
1515        }
1516
1517        case OMX_IndexParamVideoBitrate:
1518            if (((OMX_VIDEO_PARAM_BITRATETYPE*)(ComponentParameterStructure))->nPortIndex ==
1519                pComponentPrivate->pVidParamBitrate->nPortIndex)
1520            {
1521                pTmp = memcpy(ComponentParameterStructure,
1522                              pComponentPrivate->pVidParamBitrate,
1523                              sizeof(OMX_VIDEO_PARAM_BITRATETYPE));
1524                if (pTmp == NULL)
1525                {
1526                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1527                                       pComponentPrivate->dbg, OMX_TRACE4,
1528                                       "Failed to copy parameter.\n");
1529                }
1530            }
1531            else
1532            {
1533                eError = OMX_ErrorBadPortIndex;
1534            }
1535            break;
1536
1537        case OMX_IndexParamVideoH263:
1538            if (((OMX_VIDEO_PARAM_H263TYPE*)(ComponentParameterStructure))->nPortIndex ==
1539                pComponentPrivate->pH263->nPortIndex)
1540            {
1541                pTmp = memcpy(ComponentParameterStructure,
1542                              pComponentPrivate->pH263,
1543                              sizeof(OMX_VIDEO_PARAM_H263TYPE));
1544                if (pTmp == NULL)
1545                {
1546                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1547                                       pComponentPrivate->dbg, OMX_TRACE4,
1548                                       "Failed to copy parameter.\n");
1549                }
1550            }
1551            else
1552            {
1553                eError = OMX_ErrorBadPortIndex;
1554            }
1555            break;
1556        case OMX_IndexParamVideoQuantization:
1557            if (((OMX_VIDEO_PARAM_QUANTIZATIONTYPE*)(ComponentParameterStructure))->nPortIndex ==
1558                pComponentPrivate->pQuantization->nPortIndex)
1559            {
1560                pTmp = memcpy(ComponentParameterStructure,
1561                              pComponentPrivate->pQuantization,
1562                              sizeof(OMX_VIDEO_PARAM_QUANTIZATIONTYPE));
1563                if (pTmp == NULL)
1564                {
1565                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1566                                       pComponentPrivate->dbg, OMX_TRACE4,
1567                                       "Failed to copy parameter.\n");
1568                }
1569            }
1570            else
1571            {
1572                eError = OMX_ErrorBadPortIndex;
1573            }
1574            break;
1575
1576#ifdef __KHRONOS_CONF_1_1__
1577	case OMX_IndexParamVideoProfileLevelQuerySupported:
1578		{
1579			if (((OMX_VIDEO_PARAM_PROFILELEVELTYPE*)ComponentParameterStructure)->nPortIndex ==
1580                pCompPortIn->pProfileType->nPortIndex)
1581            {
1582	                if (((OMX_VIDEO_PARAM_PROFILELEVELTYPE*)(ComponentParameterStructure))->nProfileIndex >
1583	                    pCompPortIn->pPortFormat->nIndex)
1584       		         {
1585                    			eError = OMX_ErrorNoMore;
1586					break;
1587                		   }
1588				pTmp = memcpy(ComponentParameterStructure,
1589								pCompPortIn->pProfileType, 	sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
1590                if (pTmp == NULL)
1591                {
1592                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1593                                           pComponentPrivate->dbg, OMX_TRACE4,
1594                                           "Failed to copy parameter.\n");
1595					}
1596				}
1597			else if  (((OMX_VIDEO_PARAM_PROFILELEVELTYPE*)ComponentParameterStructure)->nPortIndex ==
1598                     pCompPortOut->pProfileType->nPortIndex)
1599            {
1600				if (((OMX_VIDEO_PARAM_PROFILELEVELTYPE*)(ComponentParameterStructure))->nProfileIndex >
1601	                    pCompPortOut->pPortFormat->nIndex)
1602       		         {
1603                    			eError = OMX_ErrorNoMore;
1604					break;
1605                		   }
1606				pTmp = memcpy(ComponentParameterStructure,
1607								pCompPortOut->pProfileType, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
1608                if (pTmp == NULL)
1609                {
1610                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1611                                           pComponentPrivate->dbg, OMX_TRACE4,
1612                                           "Failed to copy parameter.\n");
1613					}
1614            }
1615
1616            else
1617            {
1618				eError = OMX_ErrorBadPortIndex;
1619				}
1620			break;
1621		}
1622	case OMX_IndexParamVideoProfileLevelCurrent:
1623			{
1624			if (((OMX_VIDEO_PARAM_PROFILELEVELTYPE*)ComponentParameterStructure)->nPortIndex ==
1625                pCompPortIn->pProfileType->nPortIndex)
1626            {
1627				pTmp = memcpy(ComponentParameterStructure,
1628								pCompPortIn->pProfileType, 	sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
1629                if (pTmp == NULL)
1630                {
1631                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1632                                           pComponentPrivate->dbg, OMX_TRACE4,
1633                                           "Failed to copy parameter.\n");
1634					}
1635				}
1636			else if  (((OMX_VIDEO_PARAM_PROFILELEVELTYPE*)ComponentParameterStructure)->nPortIndex ==
1637                     pCompPortOut->pProfileType->nPortIndex)
1638            {
1639				pTmp = memcpy(ComponentParameterStructure,
1640								pCompPortOut->pProfileType, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
1641                if (pTmp == NULL)
1642                {
1643                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1644                                           pComponentPrivate->dbg, OMX_TRACE4,
1645                                           "Failed to copy parameter.\n");
1646					}
1647            }
1648
1649            else
1650            {
1651				eError = OMX_ErrorBadPortIndex;
1652				}
1653			break;
1654		}
1655#endif
1656		case OMX_IndexParamVideoErrorCorrection:
1657					{
1658					if (((OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE*)(ComponentParameterStructure))->nPortIndex ==
1659						VIDENC_OUTPUT_PORT)
1660					{
1661						pTmp = memcpy(ComponentParameterStructure,
1662									  pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pErrorCorrectionType,
1663									  sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE));
1664                if (pTmp == NULL)
1665                {
1666                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1667                                           pComponentPrivate->dbg, OMX_TRACE4,
1668                                           "Failed to copy parameter.\n");
1669                }
1670            }
1671					else
1672					{
1673						eError = OMX_ErrorBadPortIndex;
1674					}
1675					break;
1676				}
1677
1678        case VideoEncodeCustomParamIndexVBVSize:
1679            (*((OMX_U32*)ComponentParameterStructure)) = (OMX_U32)pComponentPrivate->nVBVSize;
1680            break;
1681        case VideoEncodeCustomParamIndexDeblockFilter:
1682            (*((OMX_BOOL*)ComponentParameterStructure)) = (OMX_BOOL)pComponentPrivate->bDeblockFilter;
1683            break;
1684		case VideoEncodeCustomParamIndexEncodingPreset:
1685			(*((unsigned int*)ComponentParameterStructure)) = (unsigned int)pComponentPrivate->nEncodingPreset;
1686			break;
1687       case VideoEncodeCustomParamIndexNALFormat:
1688           (*((unsigned int*)ComponentParameterStructure)) = (unsigned int)pComponentPrivate->AVCNALFormat;
1689           break;
1690       case PV_OMX_COMPONENT_CAPABILITY_TYPE_INDEX:
1691            pTmp = memcpy(ComponentParameterStructure,
1692            pComponentPrivate->pCapabilityFlags,
1693            sizeof(PV_OMXComponentCapabilityFlagsType));
1694            if (pTmp == NULL)
1695            {
1696                OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
1697            }
1698            break;
1699       //not supported yet
1700       case OMX_IndexConfigCommonRotate:
1701           break;
1702        default:
1703            eError = OMX_ErrorUnsupportedIndex;
1704            break;
1705    }
1706OMX_CONF_CMD_BAIL:
1707    return eError;
1708}
1709
1710/*----------------------------------------------------------------------------*/
1711/**
1712  *  SetParameter() Sets application callbacks to the component
1713  *
1714  * This method will update application callbacks
1715  * the application.
1716  *
1717  * @param pComp         handle for this instance of the component
1718  * @param pCallBacks    application callbacks
1719  * @param ptr
1720  *
1721  * @retval OMX_NoError              Success, ready to roll
1722  *         OMX_Error_BadParameter   The input parameter pointer is null
1723  **/
1724/*----------------------------------------------------------------------------*/
1725
1726static OMX_ERRORTYPE SetParameter (OMX_IN OMX_HANDLETYPE hComponent,
1727                                   OMX_IN OMX_INDEXTYPE nParamIndex,
1728                                   OMX_IN OMX_PTR pCompParam)
1729{
1730    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
1731    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
1732    OMX_U32* pTmp                               = NULL;
1733    VIDENC_NODE* pMemoryListHead                = NULL;
1734    VIDEOENC_PORT_TYPE* pCompPortIn             = NULL;
1735    VIDEOENC_PORT_TYPE* pCompPortOut            = NULL;
1736
1737#ifdef __KHRONOS_CONF_1_1__
1738    OMX_PARAM_COMPONENTROLETYPE  *pRole = NULL;
1739	OMX_VIDEO_PARAM_PROFILELEVELTYPE* sProfileLevel;
1740#endif
1741    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
1742
1743    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
1744    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pCompParam, 1, 1);
1745
1746    if (pComponentPrivate->eState == OMX_StateInvalid)
1747    {
1748        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
1749                               pComponentPrivate->dbg, OMX_PRSTATE3,
1750                               "Component is in invalid state.\n");
1751    }
1752
1753    pMemoryListHead = pComponentPrivate->pMemoryListHead;
1754    pCompPortIn     = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT];
1755    pCompPortOut    = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
1756
1757    switch (nParamIndex)
1758    {
1759        case OMX_IndexParamVideoPortFormat:
1760        {
1761            OMX_VIDEO_PARAM_PORTFORMATTYPE* pComponentParam = (OMX_VIDEO_PARAM_PORTFORMATTYPE*)pCompParam;
1762            if (pComponentParam->nPortIndex == pCompPortIn->pPortFormat->nPortIndex)
1763            {
1764                pTmp = memcpy(pCompPortIn->pPortFormat,
1765                              pComponentParam,
1766                              sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
1767                if (pTmp == NULL)
1768                {
1769                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1770                                           pComponentPrivate->dbg, OMX_TRACE4,
1771                                           "Failed to copy parameter.\n");
1772                }
1773            }
1774            else if (pComponentParam->nPortIndex == pCompPortOut->pPortFormat->nPortIndex)
1775            {
1776                pTmp = memcpy(pCompPortOut->pPortFormat,
1777                              pComponentParam,
1778                              sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
1779                if (pTmp == NULL)
1780                {
1781                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1782                                           pComponentPrivate->dbg, OMX_TRACE4,
1783                                           "Failed to copy parameter.\n");
1784                }
1785            }
1786            else
1787            {
1788                eError = OMX_ErrorBadPortIndex;
1789            }
1790            break;
1791        }
1792        case OMX_IndexParamVideoInit:
1793            pTmp = memcpy(pComponentPrivate->pPortParamType,
1794                          (OMX_PORT_PARAM_TYPE*)pCompParam,
1795                          sizeof(OMX_PORT_PARAM_TYPE));
1796            if (pTmp == NULL)
1797            {
1798            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1799                                   pComponentPrivate->dbg, OMX_TRACE4,
1800                                   "Failed to copy parameter.\n");
1801            }
1802            break;
1803        case OMX_IndexParamPortDefinition:
1804        {
1805            OMX_PARAM_PORTDEFINITIONTYPE* pComponentParam = (OMX_PARAM_PORTDEFINITIONTYPE*)pCompParam;
1806            if (pComponentParam->nPortIndex == pCompPortIn->pPortDef->nPortIndex)
1807            {
1808                pTmp = memcpy(pCompPortIn->pPortDef,
1809                              pComponentParam,
1810                              sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1811                if (pTmp == NULL)
1812                {
1813                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1814                                           pComponentPrivate->dbg, OMX_TRACE4,
1815                                           "Failed to copy parameter.\n");
1816                }
1817            }
1818            else if (pComponentParam->nPortIndex == pCompPortOut->pPortDef->nPortIndex)
1819            {
1820                pTmp = memcpy(pCompPortOut->pPortDef,
1821                              pComponentParam,
1822                              sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1823                if (pTmp == NULL)
1824                {
1825                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1826                                           pComponentPrivate->dbg, OMX_TRACE4,
1827                                           "Failed to copy parameter.\n");
1828                }
1829            }
1830            else
1831            {
1832                eError = OMX_ErrorBadPortIndex;
1833            }
1834            break;
1835        }
1836        case OMX_IndexParamVideoAvc:
1837        {
1838            OMX_VIDEO_PARAM_AVCTYPE* pComponentParam = (OMX_VIDEO_PARAM_AVCTYPE*)pCompParam;
1839            if (pComponentParam->nPortIndex == pComponentPrivate->pH264->nPortIndex)
1840            {
1841                pTmp = memcpy(pComponentPrivate->pH264,
1842                              pCompParam,
1843                              sizeof(OMX_VIDEO_PARAM_AVCTYPE));
1844                if (pTmp == NULL)
1845                {
1846                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1847                                           pComponentPrivate->dbg, OMX_TRACE4,
1848                                           "Failed to copy parameter.\n");
1849                }
1850            }
1851            else
1852            {
1853                eError = OMX_ErrorBadPortIndex;
1854            }
1855            break;
1856        }
1857        case OMX_IndexParamVideoMpeg4:
1858        {
1859            OMX_VIDEO_PARAM_MPEG4TYPE* pComponentParam = (OMX_VIDEO_PARAM_MPEG4TYPE*)pCompParam;
1860            if (pComponentParam->nPortIndex == pComponentPrivate->pMpeg4->nPortIndex)
1861            {
1862                pTmp = memcpy(pComponentPrivate->pMpeg4,
1863                              pCompParam,
1864                              sizeof(OMX_VIDEO_PARAM_MPEG4TYPE));
1865                if (pTmp == NULL)
1866                {
1867                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1868                                           pComponentPrivate->dbg, OMX_TRACE4,
1869                                           "Failed to copy parameter.\n");
1870                }
1871            }
1872            else
1873            {
1874                eError = OMX_ErrorBadPortIndex;
1875            }
1876            break;
1877        }
1878        case OMX_IndexParamPriorityMgmt:
1879            pTmp = memcpy(pComponentPrivate->pPriorityMgmt,
1880                          (OMX_PRIORITYMGMTTYPE*)pCompParam,
1881                          sizeof(OMX_PRIORITYMGMTTYPE));
1882            if (pTmp == NULL)
1883            {
1884            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1885                                   pComponentPrivate->dbg, OMX_TRACE4,
1886                                   "Failed to copy parameter.\n");
1887            }
1888            break;
1889        case OMX_IndexParamCompBufferSupplier:
1890        {
1891            OMX_PARAM_BUFFERSUPPLIERTYPE* pBuffSupplierParam = (OMX_PARAM_BUFFERSUPPLIERTYPE*)pCompParam;
1892            if (pBuffSupplierParam->nPortIndex == VIDENC_INPUT_PORT)
1893            {
1894                pCompPortIn->eSupplierSetting = pBuffSupplierParam->eBufferSupplier;
1895            }
1896            else if (pBuffSupplierParam->nPortIndex == VIDENC_OUTPUT_PORT)
1897            {
1898                pCompPortOut->eSupplierSetting = pBuffSupplierParam->eBufferSupplier;
1899            }
1900            else
1901            {
1902                eError = OMX_ErrorBadPortIndex;
1903            }
1904            break;
1905        }
1906        case OMX_IndexParamVideoBitrate:
1907        {
1908            OMX_VIDEO_PARAM_BITRATETYPE* pComponentParam = (OMX_VIDEO_PARAM_BITRATETYPE*)pCompParam;
1909            if (pComponentParam->nPortIndex == pComponentPrivate->pVidParamBitrate->nPortIndex)
1910            {
1911                pTmp = memcpy(pComponentPrivate->pVidParamBitrate,
1912                              pCompParam,
1913                              sizeof(OMX_VIDEO_PARAM_BITRATETYPE));
1914                if (pTmp == NULL)
1915                {
1916                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1917                                           pComponentPrivate->dbg, OMX_TRACE4,
1918                                           "Failed to copy parameter.\n");
1919                }
1920            }
1921            else
1922            {
1923                eError = OMX_ErrorBadPortIndex;
1924            }
1925            break;
1926        }
1927		case OMX_IndexParamVideoErrorCorrection:
1928				{
1929					   pTmp = memcpy(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pErrorCorrectionType,
1930									 pCompParam,
1931									 sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE));
1932					   if (pTmp == NULL)
1933						{
1934                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1935                                       pComponentPrivate->dbg, OMX_TRACE4,
1936                                       "Failed to copy parameter.\n");
1937						}
1938				}
1939				break;
1940        case OMX_IndexParamVideoH263:
1941        {
1942            OMX_VIDEO_PARAM_H263TYPE* pComponentParam = (OMX_VIDEO_PARAM_H263TYPE*)pCompParam;
1943            if (pComponentParam->nPortIndex == pComponentPrivate->pH263->nPortIndex)
1944            {
1945                pTmp = memcpy(pComponentPrivate->pH263,
1946                              pCompParam,
1947                              sizeof(OMX_VIDEO_PARAM_H263TYPE));
1948                if (pTmp == NULL)
1949                {
1950                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1951                                           pComponentPrivate->dbg, OMX_TRACE4,
1952                                           "Failed to copy parameter.\n");
1953                }
1954            }
1955            else
1956            {
1957                eError = OMX_ErrorBadPortIndex;
1958            }
1959            break;
1960        }
1961        case OMX_IndexParamVideoQuantization:
1962        {
1963            OMX_VIDEO_PARAM_QUANTIZATIONTYPE* pComponentParam = (OMX_VIDEO_PARAM_QUANTIZATIONTYPE*)pCompParam;
1964            if (pComponentParam->nPortIndex == pComponentPrivate->pQuantization->nPortIndex)
1965            {
1966                pTmp = memcpy(pComponentPrivate->pQuantization,
1967                              pCompParam,
1968                              sizeof(OMX_VIDEO_PARAM_QUANTIZATIONTYPE));
1969                if (pTmp == NULL)
1970                {
1971                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
1972                                           pComponentPrivate->dbg, OMX_TRACE4,
1973                                           "Failed to copy parameter.\n");
1974                }
1975            }
1976            else
1977            {
1978                eError = OMX_ErrorBadPortIndex;
1979            }
1980            break;
1981        }
1982        case VideoEncodeCustomParamIndexVBVSize:
1983            pComponentPrivate->nVBVSize = (OMX_U32)(*((OMX_U32*)pCompParam));
1984            break;
1985        case VideoEncodeCustomParamIndexDeblockFilter:
1986            pComponentPrivate->bDeblockFilter = (OMX_BOOL)(*((OMX_BOOL*)pCompParam));
1987            break;
1988
1989#ifdef __KHRONOS_CONF_1_1__
1990	case OMX_IndexParamStandardComponentRole:
1991        if (pCompParam)
1992        {
1993			pRole = (OMX_PARAM_COMPONENTROLETYPE *)pCompParam;
1994			memcpy(&(pComponentPrivate->componentRole), (void *)pRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
1995            if (strcmp((char *)pRole->cRole,"video_encoder.mpeg4")==0)
1996            {
1997		        pCompPortOut->pPortDef->format.video.eCompressionFormat = OMX_VIDEO_CodingMPEG4;
1998				pCompPortOut->pPortFormat->eCompressionFormat = OMX_VIDEO_CodingMPEG4;
1999				}
2000            else if (strcmp((char *)pRole->cRole,"video_encoder.h263")==0)
2001            {
2002				pCompPortOut->pPortDef->format.video.eCompressionFormat = OMX_VIDEO_CodingH263;
2003				pCompPortOut->pPortFormat->eCompressionFormat = OMX_VIDEO_CodingH263;
2004				}
2005            else if (strcmp((char *)pRole->cRole,"video_encoder.avc")==0)
2006            {
2007				pCompPortOut->pPortDef->format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
2008				pCompPortOut->pPortFormat->eCompressionFormat= OMX_VIDEO_CodingAVC;
2009				}
2010
2011			pCompPortOut->pPortFormat->eColorFormat = OMX_COLOR_FormatUnused;
2012			pCompPortOut->pPortDef->eDomain = OMX_PortDomainVideo;
2013    		pCompPortOut->pPortDef->format.video.eColorFormat = OMX_COLOR_FormatUnused;
2014        	pCompPortOut->pPortDef->format.video.nFrameWidth = 176;
2015		    pCompPortOut->pPortDef->format.video.nFrameHeight = 144;
2016			pCompPortOut->pPortDef->format.video.nBitrate = 64000;
2017        	pCompPortOut->pPortDef->format.video.xFramerate = (15 << 16);
2018
2019        }
2020        else
2021        {
2022			eError = OMX_ErrorBadParameter;
2023		}
2024		break;
2025
2026	case OMX_IndexParamVideoProfileLevelCurrent:
2027			{
2028	            sProfileLevel= (OMX_VIDEO_PARAM_PROFILELEVELTYPE*)pCompParam;
2029	            if (sProfileLevel -> nPortIndex == VIDENC_INPUT_PORT)
2030	            {
2031	                pCompPortIn -> pProfileType = sProfileLevel;
2032	            }
2033	            else if (sProfileLevel -> nPortIndex == VIDENC_OUTPUT_PORT)
2034	            {
2035	                pCompPortOut -> pProfileType = sProfileLevel;
2036	            }
2037	            else
2038	            {
2039	                eError = OMX_ErrorBadPortIndex;
2040	            }
2041	            break;
2042
2043		}
2044#endif
2045		/*valid for H264 only*/
2046		case VideoEncodeCustomParamIndexEncodingPreset:
2047				pComponentPrivate->nEncodingPreset = (unsigned int)(*((unsigned int*)pCompParam));
2048			break;
2049       case VideoEncodeCustomParamIndexNALFormat:
2050              pComponentPrivate->AVCNALFormat = (VIDENC_AVC_NAL_FORMAT)(*((unsigned int*)pCompParam));
2051       break;
2052       //not supported yet
2053       case OMX_IndexConfigCommonRotate:
2054       break;
2055
2056        default:
2057            eError = OMX_ErrorUnsupportedIndex;
2058            break;
2059    }
2060OMX_CONF_CMD_BAIL:
2061    return eError;
2062}
2063
2064/*----------------------------------------------------------------------------*/
2065/**
2066  *  GetConfig() Sets application callbacks to the component
2067  *
2068  * This method will update application callbacks
2069  * the application.
2070  *
2071  * @param pComp         handle for this instance of the component
2072  * @param pCallBacks    application callbacks
2073  * @param ptr
2074  *
2075  * @retval OMX_NoError              Success, ready to roll
2076  *         OMX_Error_BadParameter   The input parameter pointer is null
2077  **/
2078/*----------------------------------------------------------------------------*/
2079
2080static OMX_ERRORTYPE GetConfig (OMX_HANDLETYPE hComponent,
2081                                OMX_INDEXTYPE nConfigIndex,
2082                                OMX_PTR ComponentConfigStructure)
2083{
2084    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
2085    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
2086    VIDENC_NODE* pMemoryListHead                = NULL;
2087	OMX_U32* pTmp								= NULL;
2088
2089    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
2090
2091    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2092    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, ComponentConfigStructure, 1, 1);
2093
2094    if (pComponentPrivate->eState == OMX_StateInvalid)
2095    {
2096        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
2097                               pComponentPrivate->dbg, OMX_PRSTATE3,
2098                               "Component is in invalid state.\n");
2099    }
2100
2101    pMemoryListHead = pComponentPrivate->pMemoryListHead;
2102
2103    switch (nConfigIndex)
2104    {
2105        case VideoEncodeCustomConfigIndexForceIFrame:
2106            (*((OMX_BOOL*)ComponentConfigStructure)) = (OMX_BOOL)pComponentPrivate->bForceIFrame;
2107            break;
2108        case VideoEncodeCustomConfigIndexIntraFrameInterval:
2109            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->nIntraFrameInterval;
2110            break;
2111        case VideoEncodeCustomConfigIndexTargetFrameRate:
2112            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->nTargetFrameRate;
2113            break;
2114        case VideoEncodeCustomConfigIndexQPI:
2115            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->nQPI;
2116            break;
2117        case VideoEncodeCustomConfigIndexAIRRate:
2118            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->nAIRRate;
2119            break;
2120        case VideoEncodeCustomConfigIndexTargetBitRate:
2121            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->nTargetBitRate;
2122            break;
2123        /*ASO/FMO*/
2124        case VideoEncodeCustomConfigIndexNumSliceASO:
2125            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->numSliceASO;
2126            break;
2127        case VideoEncodeCustomConfigIndexAsoSliceOrder:
2128            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->asoSliceOrder;
2129            break;
2130        case VideoEncodeCustomConfigIndexNumSliceGroups:
2131            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->numSliceGroups;
2132            break;
2133        case VideoEncodeCustomConfigIndexSliceGroupMapType:
2134            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->sliceGroupMapType;
2135            break;
2136        case VideoEncodeCustomConfigIndexSliceGroupChangeDirectionFlag:
2137            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->sliceGroupChangeDirectionFlag;
2138            break;
2139        case VideoEncodeCustomConfigIndexSliceGroupChangeRate:
2140            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->sliceGroupChangeRate;
2141            break;
2142        case VideoEncodeCustomConfigIndexSliceGroupChangeCycle:
2143            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->sliceGroupChangeCycle;
2144            break;
2145        case VideoEncodeCustomConfigIndexSliceGroupParams:
2146            (*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->sliceGroupParams;
2147            break;
2148
2149#ifdef __KHRONOS_CONF_1_1__
2150	case OMX_IndexConfigVideoFramerate:
2151			{
2152
2153            pTmp = memcpy(ComponentConfigStructure,
2154				          pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pFrameRateConfig,
2155				          sizeof(OMX_CONFIG_FRAMERATETYPE));
2156			if (pTmp == NULL)
2157            {
2158                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
2159                                       pComponentPrivate->dbg, OMX_TRACE4,
2160                                       "Failed to copy config.\n");
2161            }
2162		}
2163	break;
2164	case OMX_IndexConfigVideoBitrate:
2165			{
2166            pTmp = memcpy(ComponentConfigStructure,
2167				          pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateTypeConfig,
2168				          sizeof(OMX_VIDEO_CONFIG_BITRATETYPE));
2169			if (pTmp == NULL)
2170            {
2171                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
2172                                       pComponentPrivate->dbg, OMX_TRACE4,
2173                                       "Failed to copy config.\n");
2174            }
2175		}
2176	break;
2177#endif
2178    case OMX_IndexParamVideoMotionVector:
2179        {
2180            /* also get parameters in this structure that are tracked outside of it */
2181            pComponentPrivate->pMotionVector->bFourMV = pComponentPrivate->maxMVperMB >= 4;
2182            pComponentPrivate->pMotionVector->eAccuracy = OMX_Video_MotionVectorQuarterPel;
2183
2184            pTmp = memcpy(ComponentConfigStructure,
2185                          pComponentPrivate->pMotionVector,
2186                          sizeof(OMX_VIDEO_PARAM_MOTIONVECTORTYPE));
2187            if (pTmp == NULL)
2188            {
2189                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
2190                                       pComponentPrivate->dbg, OMX_TRACE4,
2191                                       "Failed to copy config.\n");
2192            }
2193        }
2194    case OMX_IndexConfigVideoAVCIntraPeriod:
2195        {
2196            /* also get parameters in this structure that are tracked outside of it */
2197            pComponentPrivate->pH264IntraPeriod->nPFrames = pComponentPrivate->nIntraFrameInterval;
2198            pComponentPrivate->pH264IntraPeriod->nIDRPeriod = 0;
2199
2200            pTmp = memcpy(ComponentConfigStructure,
2201                          pComponentPrivate->pH264IntraPeriod,
2202                          sizeof(OMX_VIDEO_CONFIG_AVCINTRAPERIOD));
2203            if (pTmp == NULL)
2204            {
2205                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
2206                                       pComponentPrivate->dbg, OMX_TRACE4,
2207                                       "Failed to copy config.\n");
2208            }
2209        }
2210	case OMX_IndexParamVideoIntraRefresh:
2211			 {
2212       			 pTmp = memcpy(ComponentConfigStructure,
2213		    			       pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pIntraRefreshType,
2214			    		       sizeof(OMX_VIDEO_PARAM_INTRAREFRESHTYPE));
2215			     if (pTmp == NULL)
2216            {
2217                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
2218                                       pComponentPrivate->dbg, OMX_TRACE4,
2219                                       "Failed to copy config.\n");
2220            }
2221		}
2222	break;
2223	case OMX_IndexParamVideoErrorCorrection:
2224			{
2225            if (((OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE*)(ComponentConfigStructure))->nPortIndex ==
2226                VIDENC_OUTPUT_PORT)
2227            {
2228                pTmp = memcpy(ComponentConfigStructure,
2229                              pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pErrorCorrectionType,
2230                              sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE));
2231                if (pTmp == NULL)
2232                {
2233                    OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUndefined,
2234                                           pComponentPrivate->dbg, OMX_TRACE4,
2235                                           "Failed to copy config.\n");
2236                }
2237            }
2238            else
2239            {
2240                eError = OMX_ErrorBadPortIndex;
2241            }
2242            break;
2243        }
2244    case VideoEncodeCustomConfigIndexDebug:
2245        OMX_DBG_GETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
2246        break;
2247    case VideoEncodeCustomConfigIndexMIRRate:
2248			(*((OMX_U32*)ComponentConfigStructure)) = (OMX_U32)pComponentPrivate->nMIRRate;
2249            break;
2250	case VideoEncodeCustomConfigIndexMVDataEnable:
2251			(*((OMX_BOOL*)ComponentConfigStructure)) = (OMX_BOOL)pComponentPrivate->bMVDataEnable;
2252			 break;
2253	case VideoEncodeCustomConfigIndexResyncDataEnable:
2254			(*((OMX_BOOL*)ComponentConfigStructure)) = (OMX_BOOL)pComponentPrivate->bResyncDataEnable;
2255			 break;
2256	case VideoEncodeCustomConfigIndexMaxMVperMB:
2257	    	(*((OMX_U32*)ComponentConfigStructure))  =  (OMX_U32)pComponentPrivate->maxMVperMB;
2258		 	break;
2259	case VideoEncodeCustomConfigIndexIntra4x4EnableIdc:
2260	        (*((IH264VENC_Intra4x4Params*)ComponentConfigStructure)) = (IH264VENC_Intra4x4Params)pComponentPrivate->intra4x4EnableIdc;
2261		 break;
2262        default:
2263            eError = OMX_ErrorUnsupportedIndex;
2264            break;
2265    }
2266
2267OMX_CONF_CMD_BAIL:
2268    return eError;
2269}
2270
2271/*----------------------------------------------------------------------------*/
2272/**
2273  *  SetConfig() Sets application callbacks to the component
2274  *
2275  * This method will update application callbacks
2276  * the application.
2277  *
2278  * @param pComp         handle for this instance of the component
2279  * @param pCallBacks    application callbacks
2280  * @param ptr
2281  *
2282  * @retval OMX_NoError              Success, ready to roll
2283  *         OMX_Error_BadParameter   The input parameter pointer is null
2284  **/
2285/*----------------------------------------------------------------------------*/
2286
2287static OMX_ERRORTYPE SetConfig (OMX_HANDLETYPE hComponent,
2288                                OMX_INDEXTYPE nConfigIndex,
2289                                OMX_PTR ComponentConfigStructure)
2290{
2291    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
2292    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
2293    VIDENC_NODE* pMemoryListHead                = NULL;
2294    OMX_U32 i;
2295
2296    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
2297
2298    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2299    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, ComponentConfigStructure, 1, 1);
2300
2301    if (pComponentPrivate->eState == OMX_StateInvalid)
2302    {
2303        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
2304                               pComponentPrivate->dbg, OMX_PRSTATE3,
2305                               "Component is in invalid state.\n");
2306    }
2307
2308    pMemoryListHead = pComponentPrivate->pMemoryListHead;
2309
2310    switch (nConfigIndex)
2311    {
2312        case VideoEncodeCustomConfigIndexForceIFrame:
2313            pComponentPrivate->bForceIFrame = (OMX_BOOL)(*((OMX_BOOL*)ComponentConfigStructure));
2314            break;
2315        case VideoEncodeCustomConfigIndexIntraFrameInterval:
2316            pComponentPrivate->nIntraFrameInterval = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2317            break;
2318        case VideoEncodeCustomConfigIndexTargetFrameRate:
2319            pComponentPrivate->nTargetFrameRate = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2320            break;
2321        case VideoEncodeCustomConfigIndexQPI:
2322            pComponentPrivate->nQPI = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2323            break;
2324        case VideoEncodeCustomConfigIndexAIRRate:
2325            pComponentPrivate->nAIRRate = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2326            break;
2327        case VideoEncodeCustomConfigIndexTargetBitRate:
2328            pComponentPrivate->nTargetBitRate = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2329            break;
2330        /*ASO/FMO*/
2331        case VideoEncodeCustomConfigIndexNumSliceASO:
2332            pComponentPrivate->numSliceASO = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2333            break;
2334        case VideoEncodeCustomConfigIndexAsoSliceOrder:
2335            for(i=0; i<MAXNUMSLCGPS;i++)
2336            {
2337                pComponentPrivate->asoSliceOrder[i] = (OMX_U32)(*((*((OMX_U32**)ComponentConfigStructure))+i));
2338            }
2339            break;
2340        case VideoEncodeCustomConfigIndexNumSliceGroups:
2341            pComponentPrivate->numSliceGroups  = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2342            break;
2343        case VideoEncodeCustomConfigIndexSliceGroupMapType:
2344            pComponentPrivate->sliceGroupMapType = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2345            break;
2346        case VideoEncodeCustomConfigIndexSliceGroupChangeDirectionFlag:
2347            pComponentPrivate->sliceGroupChangeDirectionFlag = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2348            break;
2349        case VideoEncodeCustomConfigIndexSliceGroupChangeRate:
2350            pComponentPrivate->sliceGroupChangeRate = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2351            break;
2352        case VideoEncodeCustomConfigIndexSliceGroupChangeCycle:
2353            pComponentPrivate->sliceGroupChangeCycle = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2354            break;
2355        case VideoEncodeCustomConfigIndexSliceGroupParams:
2356            for(i=0; i<MAXNUMSLCGPS;i++)
2357            {
2358                pComponentPrivate->sliceGroupParams[i] = (OMX_U32)(*((*((OMX_U32**)ComponentConfigStructure))+i));
2359            }
2360            break;
2361	case OMX_IndexConfigVideoFramerate:
2362			{
2363            memcpy(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pFrameRateConfig,
2364				ComponentConfigStructure,
2365				sizeof(OMX_CONFIG_FRAMERATETYPE));
2366	}
2367	break;
2368	case OMX_IndexConfigVideoBitrate:
2369		{
2370		memcpy(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateTypeConfig,
2371				ComponentConfigStructure,
2372				sizeof(OMX_VIDEO_CONFIG_BITRATETYPE));
2373
2374	}
2375break;
2376	case OMX_IndexParamVideoErrorCorrection:
2377		{
2378		memcpy(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pErrorCorrectionType,
2379				ComponentConfigStructure,
2380				sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE));
2381		}
2382		break;
2383	case OMX_IndexParamVideoIntraRefresh:
2384		{
2385		memcpy(pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pIntraRefreshType,
2386				ComponentConfigStructure,
2387				sizeof(OMX_VIDEO_PARAM_INTRAREFRESHTYPE));
2388		}
2389		break;
2390    case OMX_IndexParamVideoMotionVector:
2391        {
2392            memcpy(pComponentPrivate->pMotionVector,
2393                    ComponentConfigStructure,
2394                    sizeof(OMX_VIDEO_PARAM_MOTIONVECTORTYPE));
2395            /* also set parameters set by this structure that are tracked outside of it */
2396            pComponentPrivate->maxMVperMB = pComponentPrivate->pMotionVector->bFourMV ? 4 : 1;
2397
2398            /* quarter pixel accuracy must be always enabled */
2399            if (pComponentPrivate->pMotionVector->eAccuracy < OMX_Video_MotionVectorQuarterPel)
2400                eError = OMX_ErrorBadParameter;
2401        }
2402        break;
2403    case OMX_IndexConfigVideoAVCIntraPeriod:
2404        {
2405            memcpy(pComponentPrivate->pH264IntraPeriod,
2406                    ComponentConfigStructure,
2407                    sizeof(OMX_VIDEO_CONFIG_AVCINTRAPERIOD));
2408            /* also set parameters set by this structure that are tracked outside of it */
2409            pComponentPrivate->nIntraFrameInterval = pComponentPrivate->pH264IntraPeriod->nPFrames;
2410        }
2411        break;
2412    case VideoEncodeCustomConfigIndexDebug:
2413        OMX_DBG_SETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
2414        break;
2415	case VideoEncodeCustomConfigIndexMIRRate:
2416				pComponentPrivate->nMIRRate = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2417		break;
2418	case VideoEncodeCustomConfigIndexMVDataEnable:
2419		pComponentPrivate->bMVDataEnable = (OMX_BOOL)(*((OMX_BOOL*)ComponentConfigStructure));
2420		 break;
2421	case VideoEncodeCustomConfigIndexResyncDataEnable:
2422		pComponentPrivate->bResyncDataEnable = (OMX_BOOL)(*((OMX_BOOL*)ComponentConfigStructure));
2423		 break;
2424    case VideoEncodeCustomConfigIndexMaxMVperMB:
2425	    i = (OMX_U32)(*((OMX_U32*)ComponentConfigStructure));
2426		if (i==1 || i==4)
2427			pComponentPrivate->maxMVperMB=i;
2428		else
2429			eError = OMX_ErrorBadParameter;
2430		 break;
2431	case VideoEncodeCustomConfigIndexIntra4x4EnableIdc:
2432	    pComponentPrivate->intra4x4EnableIdc = (IH264VENC_Intra4x4Params)(*((IH264VENC_Intra4x4Params*)ComponentConfigStructure));
2433		 break;
2434    default:
2435        eError = OMX_ErrorUnsupportedIndex;
2436         break;
2437        }
2438
2439OMX_CONF_CMD_BAIL:
2440    return eError;
2441}
2442
2443/*----------------------------------------------------------------------------*/
2444/**
2445  *  ExtensionIndex()
2446  *
2447  *
2448  *
2449  *
2450  * @param pComponent    handle for this instance of the component
2451  * @param pCallBacks    application callbacks
2452  * @param ptr
2453  *
2454  * @retval OMX_NoError              Success, ready to roll
2455  *         OMX_Error_BadParameter   The input parameter pointer is null
2456  **/
2457/*----------------------------------------------------------------------------*/
2458
2459static OMX_ERRORTYPE ExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent,
2460                                       OMX_IN OMX_STRING cParameterName,
2461                                       OMX_OUT OMX_INDEXTYPE* pIndexType)
2462{
2463    VIDENC_CUSTOM_DEFINITION sVideoEncodeCustomIndex[VIDENC_NUM_CUSTOM_INDEXES] =
2464    {
2465        {"OMX.TI.VideoEncode.Param.VBVSize", VideoEncodeCustomParamIndexVBVSize},
2466                                    {"OMX.TI.VideoEncode.Param.DeblockFilter", VideoEncodeCustomParamIndexDeblockFilter},
2467                                    {"OMX.TI.VideoEncode.Config.ForceIFrame", VideoEncodeCustomConfigIndexForceIFrame},
2468                                    {"OMX.TI.VideoEncode.Config.IntraFrameInterval", VideoEncodeCustomConfigIndexIntraFrameInterval},
2469                                    {"OMX.TI.VideoEncode.Config.TargetFrameRate", VideoEncodeCustomConfigIndexTargetFrameRate},
2470                                    {"OMX.TI.VideoEncode.Config.QPI", VideoEncodeCustomConfigIndexQPI},
2471                                    {"OMX.TI.VideoEncode.Config.AIRRate", VideoEncodeCustomConfigIndexAIRRate},
2472                                    {"OMX.TI.VideoEncode.Config.TargetBitRate", VideoEncodeCustomConfigIndexTargetBitRate},
2473
2474									/*Segment mode Metadata*/
2475									{"OMX.TI.VideoEncode.Config.MVDataEnable", VideoEncodeCustomConfigIndexMVDataEnable},
2476									{"OMX.TI.VideoEncode.Config.ResyncDataEnable", VideoEncodeCustomConfigIndexResyncDataEnable},
2477
2478                                    /*ASO*/
2479                                    {"OMX.TI.VideoEncode.Config.NumSliceASO", VideoEncodeCustomConfigIndexNumSliceASO},
2480                                    {"OMX.TI.VideoEncode.Config.AsoSliceOrder", VideoEncodeCustomConfigIndexAsoSliceOrder},
2481                                    /*FMO*/
2482                                    {"OMX.TI.VideoEncode.Config.NumSliceGroups", VideoEncodeCustomConfigIndexNumSliceGroups},
2483                                    {"OMX.TI.VideoEncode.Config.SliceGroupMapType", VideoEncodeCustomConfigIndexSliceGroupMapType},
2484                                    {"OMX.TI.VideoEncode.Config.SliceGroupChangeDirectionFlag", VideoEncodeCustomConfigIndexSliceGroupChangeDirectionFlag},
2485                                    {"OMX.TI.VideoEncode.Config.SliceGroupChangeRate", VideoEncodeCustomConfigIndexSliceGroupChangeRate},
2486                                    {"OMX.TI.VideoEncode.Config.SliceGroupChangeCycle", VideoEncodeCustomConfigIndexSliceGroupChangeCycle},
2487                                    {"OMX.TI.VideoEncode.Config.SliceGroupParams", VideoEncodeCustomConfigIndexSliceGroupParams},
2488                                    /**/
2489								    {"OMX.TI.VideoEncode.Config.MIRRate", VideoEncodeCustomConfigIndexMIRRate},
2490                                     {"OMX.TI.VideoEncode.Config.MaxMVperMB", VideoEncodeCustomConfigIndexMaxMVperMB},
2491                                     {"OMX.TI.VideoEncode.Config.Intra4x4EnableIdc", VideoEncodeCustomConfigIndexIntra4x4EnableIdc},
2492                                     {"OMX.TI.VideoEncode.Config.EncodingPreset", VideoEncodeCustomParamIndexEncodingPreset},
2493        {"OMX.TI.VideoEncode.Config.NALFormat", VideoEncodeCustomParamIndexNALFormat},
2494        {"OMX.TI.VideoEncode.Debug", VideoEncodeCustomConfigIndexDebug}
2495                                   };
2496    OMX_ERRORTYPE eError = OMX_ErrorNone;
2497    int nIndex = 0;
2498
2499    if (!hComponent || !pIndexType)
2500    {
2501        eError = OMX_ErrorBadParameter;
2502        goto OMX_CONF_CMD_BAIL;
2503    }
2504
2505    for (nIndex = 0; nIndex < VIDENC_NUM_CUSTOM_INDEXES; nIndex++)
2506    {
2507        if (!strcmp((const char*)cParameterName, (const char*)(&(sVideoEncodeCustomIndex[nIndex].cCustomName))))
2508        {
2509            *pIndexType = sVideoEncodeCustomIndex[nIndex].nCustomIndex;
2510            eError = OMX_ErrorNone;
2511            break;
2512        }
2513    }
2514
2515OMX_CONF_CMD_BAIL:
2516    return eError;
2517}
2518
2519/*----------------------------------------------------------------------------*/
2520/**
2521  *  GetState() Sets application callbacks to the component
2522  *
2523  * This method will update application callbacks
2524  * the application.
2525  *
2526  * @param pComp         handle for this instance of the component
2527  * @param pCallBacks    application callbacks
2528  * @param ptr
2529  *
2530  * @retval OMX_NoError              Success, ready to roll
2531  *         OMX_Error_BadParameter   The input parameter pointer is null
2532  **/
2533/*----------------------------------------------------------------------------*/
2534
2535static OMX_ERRORTYPE GetState (OMX_IN OMX_HANDLETYPE hComponent,
2536                               OMX_OUT OMX_STATETYPE* pState)
2537{
2538    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
2539    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
2540
2541    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
2542
2543    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2544    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pState, 1, 1);
2545
2546
2547    *pState = pComponentPrivate->eState;
2548
2549OMX_CONF_CMD_BAIL:
2550    return eError;
2551}
2552
2553/*----------------------------------------------------------------------------*/
2554/**
2555  *  EmptyThisBuffer() Sets application callbacks to the component
2556  *
2557  * This method will update application callbacks
2558  * the application.
2559  *
2560  * @param pComp         handle for this instance of the component
2561  * @param pCallBacks    application callbacks
2562  * @param ptr
2563  *
2564  * @retval OMX_NoError              Success, ready to roll
2565  *         OMX_Error_BadParameter   The input parameter pointer is null
2566  **/
2567/*----------------------------------------------------------------------------*/
2568
2569static OMX_ERRORTYPE EmptyThisBuffer (OMX_IN OMX_HANDLETYPE hComponent,
2570                                      OMX_IN OMX_BUFFERHEADERTYPE* pBufHead)
2571{
2572    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
2573    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
2574    int nRet                                    = 0;
2575    OMX_HANDLETYPE hTunnelComponent             = NULL;
2576    VIDENC_BUFFER_PRIVATE* pBufferPrivate       = NULL;
2577    VIDENC_NODE* pMemoryListHead                = NULL;
2578    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn    = NULL;
2579
2580    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
2581
2582    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2583    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pBufHead, 1, 1);
2584
2585    hTunnelComponent = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->hTunnelComponent;
2586
2587    pComponentPrivate->pMarkData = pBufHead->pMarkData;
2588    pComponentPrivate->hMarkTargetComponent = pBufHead->hMarkTargetComponent;
2589    pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
2590    if (!(pPortDefIn->bEnabled))
2591    {
2592        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
2593                               pComponentPrivate->dbg, OMX_PRBUFFER4,
2594                               "Emptying buffer on disabled port.\n");
2595    }
2596
2597    if(!hTunnelComponent)
2598    {
2599        if (pBufHead->nInputPortIndex != 0x0  ||
2600            pBufHead->nOutputPortIndex != OMX_NOPORT)
2601        {
2602            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadPortIndex,
2603                                   pComponentPrivate->dbg, OMX_PRBUFFER4,
2604                                   "Emptying buffer on invalid port.\n");
2605        }
2606
2607        if (pComponentPrivate->eState != OMX_StateExecuting &&
2608            pComponentPrivate->eState != OMX_StatePause)
2609        {
2610            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
2611                                   pComponentPrivate->dbg, OMX_PRBUFFER4,
2612                                   "Emptying buffer in invalid state (%d).\n", pComponentPrivate->eState);
2613        }
2614    }
2615
2616    OMX_CONF_CHK_VERSION(pBufHead, OMX_BUFFERHEADERTYPE, eError);
2617    pMemoryListHead = pComponentPrivate->pMemoryListHead;
2618
2619
2620#ifdef __PERF_INSTRUMENTATION__
2621    PERF_ReceivedFrame(pComponentPrivate->pPERF,
2622                       pBufHead->pBuffer,
2623                       pBufHead->nFilledLen,
2624                       PERF_ModuleHLMM);
2625#endif
2626
2627#ifndef UNDER_CE
2628    if (pthread_mutex_lock(&(pComponentPrivate->mVideoEncodeBufferMutex)) != 0)
2629    {
2630        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorHardware,
2631                               pComponentPrivate->dbg, OMX_TRACE4,
2632                               "pthread_mutex_lock() failed.\n");
2633    }
2634    pBufferPrivate = pBufHead->pInputPortPrivate;
2635
2636
2637    pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_COMPONENT;
2638    pBufferPrivate->bReadFromPipe = OMX_FALSE;
2639    nRet = write(pComponentPrivate->nFilled_iPipe[1],
2640                 &(pBufHead),
2641                 sizeof(pBufHead));
2642    if (nRet == -1)
2643    {
2644    	pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex));
2645        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorHardware,
2646                               pComponentPrivate->dbg, OMX_PRBUFFER4,
2647                               "failed to write to nFilled_iPipe.\n");
2648    }
2649    if (pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex)) != 0)
2650    {
2651        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorHardware,
2652                               pComponentPrivate->dbg, OMX_TRACE4,
2653                               "pthread_mutex_unlock() failed.\n");
2654    }
2655#else
2656    pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_COMPONENT;
2657    pBufferPrivate->bReadFromPipe = OMX_FALSE;
2658    nRet = write(pComponentPrivate->nFilled_iPipe[1],
2659                 &(pBufHead),
2660                 sizeof(pBufHead));
2661    if (nRet == -1)
2662    {
2663        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorHardware,
2664                               pComponentPrivate->dbg, OMX_PRBUFFER4,
2665                               "failed to write to nFilled_iPipe.\n");
2666    }
2667#endif
2668
2669OMX_CONF_CMD_BAIL:
2670    return eError;
2671}
2672
2673/*----------------------------------------------------------------------------*/
2674/**
2675  *  FillThisBuffer() Sets application callbacks to the component
2676  *
2677  * This method will update application callbacks
2678  * the application.
2679  *
2680  * @param pComp         handle for this instance of the component
2681  * @param pCallBacks    application callbacks
2682  * @param ptr
2683  *
2684  * @retval OMX_NoError              Success, ready to roll
2685  *         OMX_Error_BadParameter   The input parameter pointer is null
2686  **/
2687/*----------------------------------------------------------------------------*/
2688static OMX_ERRORTYPE FillThisBuffer (OMX_IN OMX_HANDLETYPE hComponent,
2689                                     OMX_IN OMX_BUFFERHEADERTYPE* pBufHead)
2690{
2691    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
2692    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
2693    int nRet                                    = 0;
2694    VIDENC_BUFFER_PRIVATE* pBufferPrivate       = NULL;
2695    OMX_HANDLETYPE hTunnelComponent             = NULL;
2696    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut   = NULL;
2697
2698    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
2699
2700    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2701    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pBufHead, 1, 1);
2702
2703
2704    hTunnelComponent = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->hTunnelComponent;
2705    pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
2706    pBufHead->nFilledLen = 0;
2707    pBufferPrivate = (VIDENC_BUFFER_PRIVATE*)pBufHead->pOutputPortPrivate;
2708
2709	if (!(pPortDefOut->bEnabled))
2710    {
2711        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
2712                               pComponentPrivate->dbg, OMX_PRBUFFER4,
2713                               "Filling buffer on disabled port.\n");
2714    }
2715
2716    if(!hTunnelComponent)
2717    {
2718        if (pBufHead->nOutputPortIndex != 0x1  ||
2719            pBufHead->nInputPortIndex != OMX_NOPORT)
2720        {
2721            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadPortIndex,
2722                                   pComponentPrivate->dbg, OMX_PRBUFFER4,
2723                                   "Filling buffer on invalid port.\n");
2724        }
2725
2726        if (pComponentPrivate->eState != OMX_StateExecuting &&
2727            pComponentPrivate->eState != OMX_StatePause)
2728        {
2729            OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
2730                                   pComponentPrivate->dbg, OMX_PRBUFFER4,
2731                                   "Filling buffer in invalid state (%d).\n", pComponentPrivate->eState);
2732        }
2733    }
2734    OMX_CONF_CHK_VERSION(pBufHead, OMX_BUFFERHEADERTYPE, eError);
2735#ifdef __PERF_INSTRUMENTATION__
2736    PERF_ReceivedFrame(pComponentPrivate->pPERF,
2737                       pBufHead->pBuffer,
2738                       0,
2739                       PERF_ModuleHLMM);
2740#endif
2741
2742    if (pComponentPrivate->pMarkBuf)
2743    {
2744        pBufHead->hMarkTargetComponent = pComponentPrivate->pMarkBuf->hMarkTargetComponent;
2745        pBufHead->pMarkData = pComponentPrivate->pMarkBuf->pMarkData;
2746        pComponentPrivate->pMarkBuf = NULL;
2747    }
2748
2749    if (pComponentPrivate->pMarkData)
2750    {
2751        pBufHead->hMarkTargetComponent = pComponentPrivate->hMarkTargetComponent;
2752        pBufHead->pMarkData = pComponentPrivate->pMarkData;
2753        pComponentPrivate->pMarkData = NULL;
2754    }
2755
2756#ifndef UNDER_CE
2757    if (pthread_mutex_lock(&(pComponentPrivate->mVideoEncodeBufferMutex)) != 0)
2758    {
2759        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorHardware,
2760                               pComponentPrivate->dbg, OMX_TRACE4,
2761                               "pthread_mutex_lock() failed.\n");
2762    }
2763
2764
2765    pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_COMPONENT;
2766    pBufferPrivate->bReadFromPipe = OMX_FALSE;
2767    nRet = write(pComponentPrivate->nFree_oPipe[1],
2768                 &(pBufHead),
2769                 sizeof (pBufHead));
2770    if (nRet == -1)
2771    {
2772		pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex));
2773        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorHardware,
2774                               pComponentPrivate->dbg, OMX_PRBUFFER4,
2775                               "failed to write to nFree_oPipe.\n");
2776    }
2777    if (pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex)) != 0)
2778    {
2779        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorHardware,
2780                               pComponentPrivate->dbg, OMX_TRACE4,
2781                               "pthread_mutex_unlock() failed.\n");
2782    }
2783#else
2784
2785    pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_COMPONENT;
2786    pBufferPrivate->bReadFromPipe = OMX_FALSE;
2787    nRet = write(pComponentPrivate->nFree_oPipe[1],
2788                 &(pBufHead),
2789                 sizeof (pBufHead));
2790    if (nRet == -1)
2791    {
2792        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorHardware,
2793                               pComponentPrivate->dbg, OMX_PRBUFFER4,
2794                               "failed to write to nFree_oPipe.\n");
2795    }
2796#endif
2797OMX_CONF_CMD_BAIL:
2798    return eError;
2799}
2800/*----------------------------------------------------------------------------*/
2801/**
2802  * OMX_ComponentDeInit() Sets application callbacks to the component
2803  *
2804  * This method will update application callbacks
2805  * the application.
2806  *
2807  * @param pComp         handle for this instance of the component
2808  * @param pCallBacks    application callbacks
2809  * @param ptr
2810  *
2811  * @retval OMX_NoError              Success, ready to roll
2812  *         OMX_Error_BadParameter   The input parameter pointer is null
2813  **/
2814/*----------------------------------------------------------------------------*/
2815
2816static OMX_ERRORTYPE ComponentDeInit(OMX_IN OMX_HANDLETYPE hComponent)
2817{
2818    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
2819    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
2820    LCML_DSP_INTERFACE* pLcmlHandle             = NULL;
2821    VIDENC_NODE* pMemoryListHead                = NULL;
2822    OMX_ERRORTYPE eErr  = OMX_ErrorNone;
2823    OMX_S32 nRet        = -1;
2824    OMX_S32 nStop       = -1;
2825    OMX_U32 nTimeout    = 0;
2826    struct OMX_TI_Debug dbg;
2827
2828    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
2829    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2830
2831    dbg = pComponentPrivate->dbg;
2832
2833    pMemoryListHead=pComponentPrivate->pMemoryListHead;
2834
2835#ifdef __PERF_INSTRUMENTATION__
2836    PERF_Boundary(pComponentPrivate->pPERF,
2837                  PERF_BoundaryStart | PERF_BoundaryCleanup);
2838#endif
2839    while(1)
2840    {
2841        if(!(pComponentPrivate->bHandlingFatalError))
2842        {
2843            if(!(pComponentPrivate->bErrorLcmlHandle) &&
2844               !(pComponentPrivate->bUnresponsiveDsp))
2845            { /* Add for ResourceExhaustionTest*/
2846                pLcmlHandle = pComponentPrivate->pLCML;
2847                if (pLcmlHandle != NULL)
2848                {
2849	                if (pComponentPrivate->bCodecStarted == OMX_TRUE ||
2850    	            pComponentPrivate->bCodecLoaded == OMX_TRUE)
2851	                {
2852	                    eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2853	                                               EMMCodecControlDestroy,
2854	                                               NULL);
2855	                    if (eError != OMX_ErrorNone)
2856	                    {
2857                            OMX_PRDSP4(dbg, "error when requesting EMMCodecControlDestroy");
2858	                        eError = OMX_ErrorUndefined;
2859	                    }
2860
2861#ifdef UNDER_CE
2862	                    FreeLibrary(g_hLcmlDllHandle);
2863	                    g_hLcmlDllHandle = NULL;
2864#endif
2865	                }
2866                }
2867            }
2868            break;
2869        }
2870        if(nTimeout++ > VIDENC_MAX_COMPONENT_TIMEOUT)
2871        {
2872            OMX_ERROR5(dbg, "TimeOut in HandlingFatalError!\n");
2873            break;
2874        }
2875        sched_yield();
2876    }
2877
2878
2879        /*Unload LCML */
2880    if(pComponentPrivate->pModLcml != NULL)
2881    {
2882#ifndef UNDER_CE
2883        dlclose(pComponentPrivate->pModLcml);
2884#else
2885        FreeLibrary(pComponentPrivate->pModLcml);
2886#endif
2887       pComponentPrivate->pModLcml = NULL;
2888       pComponentPrivate->pLCML = NULL;
2889    }
2890
2891
2892	pComponentPrivate->bCodecStarted = OMX_FALSE;
2893
2894    nStop = -1;
2895#ifdef __PERF_INSTRUMENTATION__
2896    PERF_SendingCommand(pComponentPrivate->pPERF, nStop, 0, PERF_ModuleComponent);
2897#endif
2898    OMX_PRCOMM2(dbg, "eCmd: -1 Send\n");
2899    nRet = write(pComponentPrivate->nCmdPipe[1],
2900                 &nStop,
2901                 sizeof(OMX_COMMANDTYPE));
2902
2903    /*Join the component thread*/
2904    /*pthread_cancel(ComponentThread);*/
2905#ifdef UNDER_CE
2906    eErr = pthread_join(ComponentThread, NULL);
2907#else
2908    eErr = pthread_join(pComponentPrivate->ComponentThread, NULL);
2909#endif
2910    if (eErr != 0)
2911    {
2912        eError = OMX_ErrorHardware;
2913        OMX_TRACE4(dbg, "Error pthread_join (%d).\n", eErr);
2914    }
2915
2916    /*close the data pipe handles*/
2917    eErr = close(pComponentPrivate->nFree_oPipe[0]);
2918    if (0 != eErr && OMX_ErrorNone == eError)
2919    {
2920        eError = OMX_ErrorHardware;
2921        OMX_PRBUFFER4(dbg, "Error while closing data pipe (%d).\n", eErr);
2922    }
2923
2924    eErr = close(pComponentPrivate->nFilled_iPipe[0]);
2925    if (0 != eErr && OMX_ErrorNone == eError)
2926    {
2927        eError = OMX_ErrorHardware;
2928        OMX_PRBUFFER4(dbg, "Error while closing data pipe (%d).\n", eErr);
2929    }
2930
2931    eErr = close(pComponentPrivate->nFree_oPipe[1]);
2932    if (0 != eErr && OMX_ErrorNone == eError)
2933    {
2934        eError = OMX_ErrorHardware;
2935        OMX_PRBUFFER4(dbg, "Error while closing data pipe (%d).\n", eErr);
2936    }
2937
2938    eErr = close(pComponentPrivate->nFilled_iPipe[1]);
2939    if (0 != eErr && OMX_ErrorNone == eError)
2940    {
2941        eError = OMX_ErrorHardware;
2942        OMX_PRBUFFER4(dbg, "Error while closing data pipe (%d).\n", eErr);
2943    }
2944
2945    /*Close the command pipe handles*/
2946    eErr = close(pComponentPrivate->nCmdPipe[0]);
2947    if (0 != eErr && OMX_ErrorNone == eError)
2948    {
2949        eError = OMX_ErrorHardware;
2950        OMX_PRCOMM4(dbg, "Error while closing data pipe (%d).\n", eErr);
2951    }
2952
2953    eErr = close(pComponentPrivate->nCmdPipe[1]);
2954    if (0 != eErr && OMX_ErrorNone == eError)
2955    {
2956        eError = OMX_ErrorHardware;
2957        OMX_PRCOMM4(dbg, "Error while closing data pipe (%d).\n", eErr);
2958    }
2959    /*Close the command data pipe handles*/
2960    eErr = close(pComponentPrivate->nCmdDataPipe[0]);
2961    if (0 != eErr && OMX_ErrorNone == eError)
2962    {
2963        eError = OMX_ErrorHardware;
2964        OMX_PRCOMM4(dbg, "Error while closing data pipe (%d).\n", eErr);
2965    }
2966
2967    eErr = close(pComponentPrivate->nCmdDataPipe[1]);
2968    if (0 != eErr && OMX_ErrorNone == eError)
2969    {
2970        eError = OMX_ErrorHardware;
2971        OMX_PRCOMM4(dbg, "Error while closing data pipe (%d).\n", eErr);
2972    }
2973
2974    OMX_PRINT2(dbg, "pipes closed...\n");
2975
2976#ifndef UNDER_CE
2977    OMX_TRACE2(dbg, "destroy mVideoEncodeBufferMutex -> %p\n",
2978              &(pComponentPrivate->mVideoEncodeBufferMutex));
2979    /* Destroy Mutex for Buffer Tracking */
2980    nRet = pthread_mutex_destroy(&(pComponentPrivate->mVideoEncodeBufferMutex));
2981    while (nRet == EBUSY)
2982    {
2983        /* The mutex is busy. We need to unlock it, then destroy it. */
2984        OMX_TRACE2(dbg, "destroy status = EBUSY\n");
2985        pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex));
2986        nRet = pthread_mutex_destroy(&(pComponentPrivate->mVideoEncodeBufferMutex));
2987    }
2988#else
2989    /* Add WinCE critical section API here... */
2990#endif
2991#ifndef UNDER_CE
2992    pthread_mutex_destroy(&pComponentPrivate->videoe_mutex);
2993    pthread_cond_destroy(&pComponentPrivate->populate_cond);
2994    pthread_mutex_destroy(&pComponentPrivate->videoe_mutex_app);
2995    pthread_cond_destroy(&pComponentPrivate->unpopulate_cond);
2996	pthread_cond_destroy(&pComponentPrivate->flush_cond);
2997	pthread_cond_destroy(&pComponentPrivate->stop_cond);
2998#else
2999    OMX_DestroyEvent(&(pComponentPrivate->InLoaded_event));
3000    OMX_DestroyEvent(&(pComponentPrivate->InIdle_event));
3001#endif
3002
3003#ifdef RESOURCE_MANAGER_ENABLED
3004    /* Deinitialize Resource Manager */
3005    eError = RMProxy_DeinitalizeEx(OMX_COMPONENTTYPE_VIDEO);
3006    if (eError != OMX_ErrorNone)
3007    {
3008        OMX_PRMGR4(dbg, "Error returned from destroy ResourceManagerProxy thread\n");
3009        eError = OMX_ErrorUndefined;
3010    }
3011#endif
3012
3013#ifdef __PERF_INSTRUMENTATION__
3014    PERF_Boundary(pComponentPrivate->pPERF,
3015                  PERF_BoundaryComplete | PERF_BoundaryCleanup);
3016    PERF_Done(pComponentPrivate->pPERF);
3017#endif
3018
3019    if (pComponentPrivate != NULL)
3020    {
3021        VIDENC_FREE(pComponentPrivate, pMemoryListHead, dbg);
3022    }
3023
3024    /* Free Resources */
3025    OMX_VIDENC_ListDestroy(&dbg, pMemoryListHead);
3026
3027    OMX_DBG_CLOSE(dbg);
3028
3029OMX_CONF_CMD_BAIL:
3030    return eError;
3031}
3032
3033/*----------------------------------------------------------------------------*/
3034/**
3035  *  UseBuffer()
3036  *
3037  *
3038  *
3039  *
3040  * @param
3041  * @param
3042  * @param
3043  *
3044  * @retval OMX_NoError              Success, ready to roll
3045  *         OMX_Error_BadParameter   The input parameter pointer is null
3046  **/
3047/*----------------------------------------------------------------------------*/
3048
3049OMX_ERRORTYPE UseBuffer(OMX_IN OMX_HANDLETYPE hComponent,
3050                        OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
3051                        OMX_IN OMX_U32 nPortIndex,
3052                        OMX_IN OMX_PTR pAppPrivate,
3053                        OMX_IN OMX_U32 nSizeBytes,
3054                        OMX_IN OMX_U8* pBuffer)
3055{
3056    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
3057    OMX_PARAM_PORTDEFINITIONTYPE* pPortDef      = NULL;
3058    VIDEOENC_PORT_TYPE* pCompPort               = NULL;
3059    VIDENC_BUFFER_PRIVATE* pBufferPrivate       = NULL;
3060    OMX_U32 nBufferCnt      = -1;
3061    OMX_ERRORTYPE eError    = OMX_ErrorNone;
3062    OMX_HANDLETYPE hTunnelComponent = NULL;
3063    VIDENC_NODE* pMemoryListHead    = NULL;
3064
3065    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
3066
3067    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
3068    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, ppBufferHdr, pBuffer, 1);
3069
3070    if (nPortIndex == VIDENC_INPUT_PORT)
3071    {
3072       pPortDef = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
3073    }
3074    else if (nPortIndex == VIDENC_OUTPUT_PORT)
3075    {
3076        pPortDef = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
3077    }
3078    else
3079    {
3080        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter,
3081                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3082                               "Using buffer on invalid port index.\n");
3083    }
3084
3085    if (!pPortDef->bEnabled)
3086    {
3087        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
3088                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3089                               "Using buffer on disabled port.\n");
3090    }
3091
3092    if (nSizeBytes != pPortDef->nBufferSize || pPortDef->bPopulated)
3093    {
3094        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter,
3095                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3096                               "Using duplicate buffer or of invalid size.\n");
3097    }
3098
3099
3100    if (pComponentPrivate->eState == OMX_StateInvalid)
3101    {
3102        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorInvalidState,
3103                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3104                               "Using buffer in invalid state.\n");
3105    }
3106
3107    pMemoryListHead=pComponentPrivate->pMemoryListHead;
3108
3109
3110#ifdef __PERF_INSTRUMENTATION__
3111    PERF_ReceivedBuffer(pComponentPrivate->pPERF,
3112                        pBuffer, nSizeBytes,
3113                        PERF_ModuleHLMM);
3114#endif
3115
3116    nBufferCnt       = pComponentPrivate->pCompPort[nPortIndex]->nBufferCnt;
3117    hTunnelComponent = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->hTunnelComponent;
3118    pCompPort        = pComponentPrivate->pCompPort[nPortIndex];
3119    pBufferPrivate   = pCompPort->pBufferPrivate[nBufferCnt];
3120
3121    VIDENC_MALLOC(pBufferPrivate->pBufferHdr,
3122                  sizeof(OMX_BUFFERHEADERTYPE),
3123                  OMX_BUFFERHEADERTYPE, pMemoryListHead, pComponentPrivate->dbg);
3124
3125    pBufferPrivate->pBufferHdr->nSize       = sizeof(OMX_BUFFERHEADERTYPE);
3126    pBufferPrivate->pBufferHdr->nVersion    = pPortDef->nVersion;
3127    pBufferPrivate->pBufferHdr->pBuffer     = pBuffer;
3128    pBufferPrivate->pBufferHdr->pAppPrivate = pAppPrivate;
3129    pBufferPrivate->pBufferHdr->nAllocLen   = nSizeBytes;
3130
3131    if (hTunnelComponent != NULL)
3132    {
3133        /* set direction dependent fields */
3134        if (pPortDef->eDir == OMX_DirInput)
3135        {
3136            pBufferPrivate->pBufferHdr->nInputPortIndex  = nPortIndex;
3137            pBufferPrivate->pBufferHdr->nOutputPortIndex = pCompPort->nTunnelPort;
3138        }
3139        else
3140        {
3141            pBufferPrivate->pBufferHdr->nInputPortIndex  = pCompPort->nTunnelPort;
3142            pBufferPrivate->pBufferHdr->nOutputPortIndex = nPortIndex;
3143        }
3144    }
3145    else
3146    {
3147        if (nPortIndex == VIDENC_INPUT_PORT)
3148        {
3149            pBufferPrivate->pBufferHdr->nInputPortIndex  = VIDENC_INPUT_PORT;
3150            pBufferPrivate->pBufferHdr->nOutputPortIndex = OMX_NOPORT;
3151        }
3152        else
3153        {
3154            pBufferPrivate->pBufferHdr->nInputPortIndex  = OMX_NOPORT;
3155            pBufferPrivate->pBufferHdr->nOutputPortIndex = VIDENC_OUTPUT_PORT;
3156        }
3157    }
3158    *ppBufferHdr = pBufferPrivate->pBufferHdr;
3159    pBufferPrivate->pBufferHdr = pBufferPrivate->pBufferHdr;
3160
3161    if (nPortIndex == VIDENC_INPUT_PORT)
3162    {
3163        pBufferPrivate->pBufferHdr->pInputPortPrivate = pBufferPrivate;
3164    }
3165    else
3166    {
3167       pBufferPrivate->pBufferHdr->pOutputPortPrivate = pBufferPrivate;
3168    }
3169    pBufferPrivate->bAllocByComponent = OMX_FALSE;
3170
3171    if (hTunnelComponent != NULL)
3172    {
3173        pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_TUNNELEDCOMP;
3174    }
3175    else
3176    {
3177        pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
3178    }
3179
3180    eError = OMX_VIDENC_Allocate_DSPResources(pComponentPrivate, nPortIndex);
3181    OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
3182                          "Failed to allocate DSP resources.\n");
3183
3184    OMX_CONF_CIRCULAR_BUFFER_ADD_NODE(pComponentPrivate,
3185                                      pComponentPrivate->sCircularBuffer);
3186    pCompPort->nBufferCnt++;
3187    if(pCompPort->nBufferCnt == pPortDef->nBufferCountActual)
3188    {
3189        pPortDef->bPopulated = OMX_TRUE;
3190#ifndef UNDER_CE
3191        pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
3192        pthread_cond_signal(&pComponentPrivate->populate_cond);
3193        pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
3194#else
3195        OMX_SignalEvent(&(pComponentPrivate->InLoaded_event));
3196#endif
3197    }
3198OMX_CONF_CMD_BAIL:
3199    return eError;
3200}
3201
3202/*----------------------------------------------------------------------------*/
3203/**
3204  *  FreeBuffer()
3205  *
3206  *
3207  *
3208  *
3209  * @param
3210  * @param
3211  * @param
3212  *
3213  * @retval OMX_NoError              Success, ready to roll
3214  *         OMX_Error_BadParameter   The input parameter pointer is null
3215  **/
3216/*----------------------------------------------------------------------------*/
3217
3218OMX_ERRORTYPE FreeBuffer(OMX_IN  OMX_HANDLETYPE hComponent,
3219                         OMX_IN  OMX_U32 nPortIndex,
3220                         OMX_IN  OMX_BUFFERHEADERTYPE* pBufHead)
3221{
3222    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
3223    OMX_COMPONENTTYPE* pHandle                  = NULL;
3224    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
3225    char* pTemp                                 = NULL;
3226    VIDEOENC_PORT_TYPE* pCompPort               = NULL;
3227    OMX_PARAM_PORTDEFINITIONTYPE* pPortDef      = NULL;
3228    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut   = NULL;
3229    OMX_VIDEO_CODINGTYPE eCompressionFormat     = -1;
3230    OMX_U32 nBufferCnt                          = -1;
3231    OMX_U8 nCount                               = 0;
3232    VIDENC_BUFFER_PRIVATE* pBufferPrivate       = NULL;
3233    VIDENC_NODE* pMemoryListHead                = NULL;
3234
3235    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
3236
3237    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
3238    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pBufHead, 1, 1);
3239    /*OMX_CONF_CHK_VERSION(pBufHead, OMX_BUFFERHEADERTYPE, eError); Makes CONF_FlushTest Fail*/
3240
3241    pHandle = (OMX_COMPONENTTYPE*)hComponent;
3242
3243    pMemoryListHead = pComponentPrivate->pMemoryListHead;
3244    pCompPort = pComponentPrivate->pCompPort[nPortIndex];
3245    pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
3246    pPortDef = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
3247    nBufferCnt = pComponentPrivate->pCompPort[nPortIndex]->nBufferCnt;
3248
3249    eCompressionFormat = pPortDefOut->format.video.eCompressionFormat;
3250
3251    if (nPortIndex == VIDENC_INPUT_PORT)
3252    {
3253        pBufferPrivate = pBufHead->pInputPortPrivate;
3254        if (pBufferPrivate != NULL)
3255        {
3256			if (pBufferPrivate->pUalgParam != NULL)
3257            {
3258                pTemp = (char*)pBufferPrivate->pUalgParam;
3259                pTemp -= 128;
3260                if (eCompressionFormat == OMX_VIDEO_CodingAVC)
3261                {
3262                    pBufferPrivate->pUalgParam = (H264VE_GPP_SN_UALGInputParams*)pTemp;
3263                }
3264                else if (eCompressionFormat == OMX_VIDEO_CodingMPEG4 ||
3265                         eCompressionFormat ==OMX_VIDEO_CodingH263)
3266                {
3267                    pBufferPrivate->pUalgParam = (MP4VE_GPP_SN_UALGInputParams*)pTemp;
3268                }
3269            }
3270            VIDENC_FREE(pBufferPrivate->pUalgParam, pMemoryListHead, pComponentPrivate->dbg);
3271        }
3272    }
3273    else if (nPortIndex == VIDENC_OUTPUT_PORT)
3274    {
3275        pBufferPrivate = pBufHead->pOutputPortPrivate;
3276        if (pBufferPrivate != NULL)
3277        {
3278            if (pBufferPrivate->pUalgParam != NULL)
3279            {
3280                pTemp = (char*)pBufferPrivate->pUalgParam;
3281                pTemp -= 128;
3282                if (eCompressionFormat == OMX_VIDEO_CodingAVC)
3283                {
3284                    pBufferPrivate->pUalgParam = (H264VE_GPP_SN_UALGOutputParams*)pTemp;
3285                }
3286                else if (eCompressionFormat == OMX_VIDEO_CodingMPEG4 ||
3287                         eCompressionFormat ==OMX_VIDEO_CodingH263)
3288                {
3289                    pBufferPrivate->pUalgParam = (MP4VE_GPP_SN_UALGOutputParams*)pTemp;
3290                }
3291            }
3292            VIDENC_FREE(pBufferPrivate->pUalgParam, pMemoryListHead, pComponentPrivate->dbg);
3293    	}
3294    }
3295    else
3296    {
3297        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadPortIndex,
3298                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3299                               "Freeing buffer on invalid port index.\n");
3300    }
3301
3302    if (pPortDef->bEnabled && pComponentPrivate->eState != OMX_StateIdle)
3303    {
3304        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
3305                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3306                               "Freeing buffer in invalid state or on a disabled port.\n");
3307    }
3308
3309#ifdef __PERF_INSTRUMENTATION__
3310    PERF_SendingBuffer(pComponentPrivate->pPERF,
3311                       pBufHead->pBuffer, pBufHead->nAllocLen,
3312                       (pBufferPrivate->bAllocByComponent == OMX_TRUE) ?
3313                       PERF_ModuleMemory :
3314                       PERF_ModuleHLMM);
3315#endif
3316
3317    if (pBufferPrivate->bAllocByComponent == OMX_TRUE)
3318    {
3319        if (pBufHead->pBuffer != NULL)
3320        {
3321            pBufHead->pBuffer -= 128;
3322            pBufHead->pBuffer = (unsigned char*)pBufHead->pBuffer;
3323            VIDENC_FREE(pBufHead->pBuffer, pMemoryListHead, pComponentPrivate->dbg);
3324        }
3325    }
3326
3327    while (1)
3328    {
3329        if (pCompPort->pBufferPrivate[nCount]->pBufferHdr == pBufHead)
3330        {
3331            break;
3332        }
3333        nCount++;
3334    }
3335
3336    if (pBufHead != NULL)
3337    {
3338        VIDENC_FREE(pBufHead, pMemoryListHead, pComponentPrivate->dbg);
3339    }
3340
3341    OMX_CONF_CIRCULAR_BUFFER_DELETE_NODE(pComponentPrivate,
3342                                         pComponentPrivate->sCircularBuffer);
3343    pCompPort->nBufferCnt--;
3344    if (pCompPort->nBufferCnt == 0)
3345    {
3346        pPortDef->bPopulated = OMX_FALSE;
3347
3348#ifndef UNDER_CE
3349       pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
3350       pthread_cond_signal(&pComponentPrivate->unpopulate_cond);
3351       pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
3352#else
3353           OMX_SignalEvent(&(pComponentPrivate->InIdle_event));
3354#endif
3355    }
3356
3357    if (pPortDef->bEnabled &&
3358        (pComponentPrivate->eState == OMX_StateIdle ||
3359         pComponentPrivate->eState == OMX_StateExecuting  ||
3360         pComponentPrivate->eState == OMX_StatePause))
3361    {
3362#ifdef  __KHRONOS_CONF__
3363         if(!pComponentPrivate->bPassingIdleToLoaded)
3364#endif
3365             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
3366                                      OMX_EventError,
3367                                      OMX_ErrorPortUnpopulated,
3368                                      nPortIndex,
3369                                      NULL);
3370    }
3371OMX_CONF_CMD_BAIL:
3372    return eError;
3373}
3374
3375/*----------------------------------------------------------------------------*/
3376/**
3377  *  AllocateBuffer()
3378  *
3379  *
3380  *
3381  *
3382  * @param
3383  * @param
3384  * @param
3385  *
3386  * @retval OMX_NoError              Success, ready to roll
3387  *         OMX_Error_BadParameter   The input parameter pointer is null
3388  **/
3389/*----------------------------------------------------------------------------*/
3390
3391OMX_ERRORTYPE AllocateBuffer(OMX_IN OMX_HANDLETYPE hComponent,
3392                             OMX_INOUT OMX_BUFFERHEADERTYPE** pBufHead,
3393                             OMX_IN OMX_U32 nPortIndex,
3394                             OMX_IN OMX_PTR pAppPrivate,
3395                             OMX_IN OMX_U32 nSizeBytes)
3396{
3397    OMX_COMPONENTTYPE* pHandle                  = NULL;
3398    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
3399    OMX_PARAM_PORTDEFINITIONTYPE* pPortDef      = NULL;
3400    VIDEOENC_PORT_TYPE* pCompPort               = NULL;
3401    OMX_HANDLETYPE hTunnelComponent             = NULL;
3402    VIDENC_BUFFER_PRIVATE* pBufferPrivate       = NULL;
3403    OMX_U32 nBufferCnt                          = -1;
3404    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
3405    VIDENC_NODE* pMemoryListHead                = NULL;
3406
3407    OMX_CONF_CHECK_CMD(hComponent, ((OMX_COMPONENTTYPE *) hComponent)->pComponentPrivate, 1);
3408
3409    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
3410    OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pBufHead, 1, 1);
3411
3412    pHandle = (OMX_COMPONENTTYPE*)hComponent;
3413
3414    if (nPortIndex == VIDENC_INPUT_PORT)
3415    {
3416       pPortDef = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
3417    }
3418    else if (nPortIndex == VIDENC_OUTPUT_PORT)
3419    {
3420        pPortDef = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
3421    }
3422    else
3423    {
3424        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter,
3425                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3426                               "Allocating buffer on invalid port index.\n");
3427    }
3428
3429    if (!pPortDef->bEnabled)
3430    {
3431        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorIncorrectStateOperation,
3432                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3433                               "Allocating buffer on disabled port.\n");
3434    }
3435
3436    if (nSizeBytes != pPortDef->nBufferSize || pPortDef->bPopulated)
3437    {
3438        OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter,
3439                               pComponentPrivate->dbg, OMX_PRBUFFER4,
3440                               "Allocating duplicate buffer or of invalid size.\n");
3441    }
3442
3443    pMemoryListHead = pComponentPrivate->pMemoryListHead;
3444    pCompPort = pComponentPrivate->pCompPort[nPortIndex];
3445    nBufferCnt = pComponentPrivate->pCompPort[nPortIndex]->nBufferCnt;
3446    hTunnelComponent = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->hTunnelComponent;
3447    pBufferPrivate = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCnt];
3448
3449    VIDENC_MALLOC(*pBufHead,
3450                  sizeof(OMX_BUFFERHEADERTYPE),
3451                  OMX_BUFFERHEADERTYPE,
3452                  pMemoryListHead, pComponentPrivate->dbg);
3453
3454    if (nPortIndex == VIDENC_INPUT_PORT)
3455    {
3456        (*pBufHead)->nInputPortIndex  = VIDENC_INPUT_PORT;
3457        (*pBufHead)->nOutputPortIndex = OMX_NOPORT;
3458    }
3459    else
3460    {
3461        (*pBufHead)->nInputPortIndex  = OMX_NOPORT;
3462        (*pBufHead)->nOutputPortIndex = VIDENC_OUTPUT_PORT;
3463    }
3464
3465    VIDENC_MALLOC((*pBufHead)->pBuffer,
3466                  nSizeBytes + 256,
3467                  OMX_U8,
3468                  pMemoryListHead, pComponentPrivate->dbg);
3469    ((*pBufHead)->pBuffer) += 128;
3470    ((*pBufHead)->pBuffer) = (unsigned char*)((*pBufHead)->pBuffer);
3471    (*pBufHead)->nSize       = sizeof(OMX_BUFFERHEADERTYPE);
3472    (*pBufHead)->nVersion    = pPortDef->nVersion;
3473    (*pBufHead)->pAppPrivate = pAppPrivate;
3474    (*pBufHead)->nAllocLen   = nSizeBytes;
3475    pBufferPrivate->pBufferHdr = *pBufHead;
3476
3477#ifdef __PERF_INSTRUMENTATION__
3478    PERF_ReceivedBuffer(pComponentPrivate->pPERF,
3479                        (*pBufHead)->pBuffer, nSizeBytes,
3480                        PERF_ModuleMemory);
3481#endif
3482
3483    if (nPortIndex == VIDENC_INPUT_PORT)
3484    {
3485        pBufferPrivate->pBufferHdr->pInputPortPrivate = pBufferPrivate;
3486    }
3487    else
3488    {
3489        pBufferPrivate->pBufferHdr->pOutputPortPrivate = pBufferPrivate;
3490    }
3491    pBufferPrivate->bAllocByComponent = OMX_TRUE;
3492
3493    if (hTunnelComponent != NULL)
3494    {
3495        pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_TUNNELEDCOMP;
3496    }
3497    else
3498    {
3499        pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
3500    }
3501
3502    eError = OMX_VIDENC_Allocate_DSPResources(pComponentPrivate, nPortIndex);
3503    OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
3504                          "Failed to allocate DSP resources.\n");
3505
3506    OMX_CONF_CIRCULAR_BUFFER_ADD_NODE(pComponentPrivate,
3507                                      pComponentPrivate->sCircularBuffer);
3508
3509    pCompPort->nBufferCnt++;
3510    if(pCompPort->nBufferCnt == pPortDef->nBufferCountActual)
3511    {
3512        pPortDef->bPopulated = OMX_TRUE;
3513#ifndef UNDER_CE
3514        pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
3515        pthread_cond_signal(&pComponentPrivate->populate_cond);
3516        pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
3517#else
3518        OMX_SignalEvent(&(pComponentPrivate->InLoaded_event));
3519#endif
3520    }
3521
3522OMX_CONF_CMD_BAIL:
3523    return eError;
3524}
3525
3526
3527/*----------------------------------------------------------------------------*/
3528/**
3529  *  VerifyTunnelConnection()
3530  *
3531  *
3532  *
3533  *
3534  * @param
3535  * @param
3536  * @param
3537  *
3538  * @retval OMX_NoError              Success, ready to roll
3539  *         OMX_Error_BadParameter   The input parameter pointer is null
3540  **/
3541/*----------------------------------------------------------------------------*/
3542
3543OMX_ERRORTYPE VerifyTunnelConnection(VIDEOENC_PORT_TYPE* pPort,
3544                                     OMX_HANDLETYPE hTunneledComp,
3545                                     OMX_PARAM_PORTDEFINITIONTYPE* pPortDef,
3546                                     struct OMX_TI_Debug *dbg)
3547{
3548   OMX_PARAM_PORTDEFINITIONTYPE sPortDef;
3549   OMX_ERRORTYPE eError = OMX_ErrorNone;
3550
3551    OMX_DBG_CHECK_CMD(*dbg, pPort, hTunneledComp, pPortDef);
3552
3553   sPortDef.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
3554   sPortDef.nVersion.s.nVersionMajor = 0x1;
3555   sPortDef.nVersion.s.nVersionMinor = 0x0;
3556   sPortDef.nPortIndex = pPort->nTunnelPort;
3557
3558   eError = OMX_GetParameter(hTunneledComp,
3559                             OMX_IndexParamPortDefinition,
3560                             &sPortDef);
3561   if (eError != OMX_ErrorNone)
3562   {
3563       return eError;
3564   }
3565
3566   switch (pPortDef->eDomain)
3567   {
3568       case OMX_PortDomainOther:
3569           if (sPortDef.format.other.eFormat!= pPortDef->format.other.eFormat)
3570           {
3571               pPort->hTunnelComponent = 0;
3572               pPort->nTunnelPort      = 0;
3573               return OMX_ErrorPortsNotCompatible;
3574           }
3575           break;
3576       case OMX_PortDomainAudio:
3577           if (sPortDef.format.audio.eEncoding != pPortDef->format.audio.eEncoding)
3578           {
3579               pPort->hTunnelComponent = 0;
3580               pPort->nTunnelPort      = 0;
3581               return OMX_ErrorPortsNotCompatible;
3582           }
3583           break;
3584       case OMX_PortDomainVideo:
3585           if (sPortDef.format.video.eCompressionFormat != pPortDef->format.video.eCompressionFormat)
3586           {
3587               pPort->hTunnelComponent = 0;
3588               pPort->nTunnelPort      = 0;
3589               return OMX_ErrorPortsNotCompatible;
3590           }
3591           break;
3592       case OMX_PortDomainImage:
3593           if (sPortDef.format.image.eCompressionFormat != pPortDef->format.image.eCompressionFormat)
3594           {
3595               pPort->hTunnelComponent = 0;
3596               pPort->nTunnelPort      = 0;
3597               return OMX_ErrorPortsNotCompatible;
3598           }
3599           break;
3600       default:
3601           pPort->hTunnelComponent = 0;
3602           pPort->nTunnelPort      = 0;
3603           return OMX_ErrorPortsNotCompatible;
3604   }
3605
3606OMX_CONF_CMD_BAIL:
3607    return eError;
3608}
3609
3610/*-------------------------------------------------------------------*/
3611/**
3612  * IsTIOMXComponent()
3613  * Check if the component is TI component.
3614  * @param hTunneledComp Component Tunnel Pipe
3615  * @retval OMX_TRUE   Input is a TI component.
3616  *             OMX_FALSE  Input is a not a TI component.
3617  *
3618  **/
3619/*-------------------------------------------------------------------*/
3620
3621static OMX_BOOL IsTIOMXComponent(OMX_HANDLETYPE hComp, struct OMX_TI_Debug *dbg)
3622{
3623
3624    OMX_ERRORTYPE eError = OMX_ErrorNone;
3625    OMX_STRING pTunnelcComponentName = NULL;
3626    OMX_VERSIONTYPE* pTunnelComponentVersion = NULL;
3627    OMX_VERSIONTYPE* pSpecVersion = NULL;
3628    OMX_UUIDTYPE* pComponentUUID = NULL;
3629    char *pSubstring = NULL;
3630    OMX_BOOL bResult = OMX_TRUE;
3631
3632    pTunnelcComponentName = malloc(128);
3633
3634    if (pTunnelcComponentName == NULL)
3635    {
3636        eError = OMX_ErrorInsufficientResources;
3637        OMX_TRACE4(*dbg, "Error in video encoder OMX_ErrorInsufficientResources %d\n",__LINE__);
3638        goto EXIT;
3639    }
3640
3641    pTunnelComponentVersion = malloc(sizeof(OMX_VERSIONTYPE));
3642    if (pTunnelComponentVersion == NULL)
3643    {
3644        OMX_TRACE4(*dbg, "Error in video encoder OMX_ErrorInsufficientResources %d\n",__LINE__);
3645        eError = OMX_ErrorInsufficientResources;
3646        goto EXIT;
3647    }
3648
3649    pSpecVersion = malloc(sizeof(OMX_VERSIONTYPE));
3650    if (pSpecVersion == NULL)
3651    {
3652        OMX_TRACE4(*dbg, "Error in video encoder OMX_ErrorInsufficientResources %d\n",__LINE__);
3653        eError = OMX_ErrorInsufficientResources;
3654        goto EXIT;
3655    }
3656
3657    pComponentUUID = malloc(sizeof(OMX_UUIDTYPE));
3658    if (pComponentUUID == NULL)
3659    {
3660        OMX_TRACE4(*dbg, "Error in video encoder OMX_ErrorInsufficientResources %d\n",__LINE__);
3661        eError = OMX_ErrorInsufficientResources;
3662        goto EXIT;
3663    }
3664
3665    eError = OMX_GetComponentVersion (hComp, pTunnelcComponentName, pTunnelComponentVersion, pSpecVersion, pComponentUUID);
3666
3667    /* Check if tunneled component is a TI component */
3668    pSubstring = strstr(pTunnelcComponentName, "OMX.TI.");
3669    if (pSubstring == NULL)
3670    {
3671        bResult = OMX_FALSE;
3672    }
3673
3674EXIT:
3675    free(pTunnelcComponentName);
3676    free(pTunnelComponentVersion);
3677    free(pSpecVersion);
3678    free(pComponentUUID);
3679
3680    return bResult;
3681} /* End of IsTIOMXComponent */
3682
3683
3684
3685
3686/*----------------------------------------------------------------------------*/
3687/**
3688  *  ComponentTunnelRequest() Sets application callbacks to the component
3689  *
3690  * This method will update application callbacks
3691  * the application.
3692  *
3693  * @param pComp         handle for this instance of the component
3694  * @param pCallBacks    application callbacks
3695  * @param ptr
3696  *
3697  * @retval OMX_NoError              Success, ready to roll
3698  *         OMX_Error_BadParameter   The input parameter pointer is null
3699  **/
3700/*----------------------------------------------------------------------------*/
3701
3702OMX_ERRORTYPE ComponentTunnelRequest(OMX_IN  OMX_HANDLETYPE hComponent,
3703                                     OMX_IN  OMX_U32 nPort,
3704                                     OMX_IN  OMX_HANDLETYPE hTunneledComp,
3705                                     OMX_IN  OMX_U32 nTunneledPort,
3706                                     OMX_INOUT OMX_TUNNELSETUPTYPE* pTunnelSetup)
3707{
3708    OMX_ERRORTYPE eError       = OMX_ErrorNone;
3709    OMX_COMPONENTTYPE* pHandle = (OMX_COMPONENTTYPE*)hComponent;
3710    VIDENC_COMPONENT_PRIVATE* pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
3711    OMX_PARAM_BUFFERSUPPLIERTYPE sBufferSupplier;
3712    VIDEOENC_PORT_TYPE* pPort = pComponentPrivate->pCompPort[nPort];
3713    if (pTunnelSetup == NULL || hTunneledComp == 0)
3714    {
3715        /* cancel previous tunnel */
3716        pPort->hTunnelComponent = 0;
3717        pPort->nTunnelPort = 0;
3718        pPort->eSupplierSetting = OMX_BufferSupplyUnspecified;
3719    }
3720    else
3721    {
3722        pHandle = (OMX_COMPONENTTYPE*)hComponent;
3723        if (!pHandle->pComponentPrivate)
3724        {
3725            OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter);
3726        }
3727        pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
3728        pPort = pComponentPrivate->pCompPort[nPort];
3729
3730        if (pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef->eDir != OMX_DirInput &&
3731            pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef->eDir != OMX_DirOutput)
3732        {
3733            return OMX_ErrorBadParameter;
3734        }
3735
3736		/* Check if the other component is developed by TI */
3737        if (IsTIOMXComponent(hTunneledComp, &pComponentPrivate->dbg) != OMX_TRUE)
3738        {
3739		    eError = OMX_ErrorTunnelingUnsupported;
3740			goto OMX_CONF_CMD_BAIL;
3741		}
3742		pPort->hTunnelComponent = hTunneledComp;
3743        pPort->nTunnelPort = nTunneledPort;
3744
3745        if (pPort->pPortDef->eDir == OMX_DirOutput)
3746        {
3747            /* Component is the output (source of data) */
3748            pTunnelSetup->eSupplier = pPort->eSupplierSetting;
3749        }
3750        else
3751        {
3752            /* Component is the input (sink of data) */
3753            eError = VerifyTunnelConnection(pPort,
3754                                            hTunneledComp,
3755                                            pPort->pPortDef,
3756                                            &pComponentPrivate->dbg);
3757            if(OMX_ErrorNone != eError)
3758            {
3759                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorPortsNotCompatible,
3760                                       pComponentPrivate->dbg, OMX_PRCOMM3,
3761                                       "VerifyTunnelConnection failed.\n");
3762            }
3763
3764            /* If specified obey output port's preferences. Otherwise choose output */
3765            pPort->eSupplierSetting = pTunnelSetup->eSupplier;
3766            if (OMX_BufferSupplyUnspecified == pPort->eSupplierSetting)
3767            {
3768                pPort->eSupplierSetting = pTunnelSetup->eSupplier = OMX_BufferSupplyOutput;
3769            }
3770
3771            /* Tell the output port who the supplier is */
3772            sBufferSupplier.nSize = sizeof(sBufferSupplier);
3773            sBufferSupplier.nVersion.s.nVersionMajor = 0x1;
3774            sBufferSupplier.nVersion.s.nVersionMinor = 0x0;
3775            sBufferSupplier.nPortIndex = nTunneledPort;
3776            sBufferSupplier.eBufferSupplier = pPort->eSupplierSetting;
3777
3778            eError = OMX_SetParameter(hTunneledComp,
3779                                      OMX_IndexParamCompBufferSupplier,
3780                                      &sBufferSupplier);
3781            eError = OMX_GetParameter(hTunneledComp,
3782                                      OMX_IndexParamCompBufferSupplier,
3783                                      &sBufferSupplier);
3784
3785            if (sBufferSupplier.eBufferSupplier != pPort->eSupplierSetting)
3786            {
3787                OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorPortsNotCompatible,
3788                                       pComponentPrivate->dbg, OMX_PRCOMM3,
3789                                       "SetParameter: OMX_IndexParamCompBufferSupplier failed to change setting.\n");
3790            }
3791        }
3792    }
3793OMX_CONF_CMD_BAIL:
3794    return eError;
3795}
3796
3797#ifdef __KHRONOS_CONF_1_1__
3798
3799/*----------------------------------------------------------------------------*/
3800/**
3801  *  ComponentRoleEnum()
3802  *
3803  *
3804  * @param pComp         handle for this instance of the component
3805  *
3806  * @retval OMX_NoError              Success, ready to roll
3807  *         OMX_Error_BadParameter   The input parameter pointer is null
3808  **/
3809/*----------------------------------------------------------------------------*/
3810static OMX_ERRORTYPE ComponentRoleEnum(OMX_IN OMX_HANDLETYPE hComponent,
3811                                       OMX_OUT OMX_U8 *cRole,
3812                                       OMX_IN OMX_U32 nIndex)
3813{
3814    VIDENC_COMPONENT_PRIVATE *pComponentPrivate;
3815	OMX_ERRORTYPE eError = OMX_ErrorNone;
3816
3817    if (hComponent==NULL)
3818    {
3819		goto OMX_CONF_CMD_BAIL;
3820		eError= OMX_ErrorBadParameter;
3821		}
3822
3823    pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
3824
3825    if(nIndex == 0)
3826    {
3827	  strncpy((char*)cRole, (char *)pComponentPrivate->componentRole.cRole, sizeof(OMX_U8) * OMX_MAX_STRINGNAME_SIZE - 1);
3828	}
3829    else
3830    {
3831      eError = OMX_ErrorNoMore;
3832    }
3833
3834OMX_CONF_CMD_BAIL:
3835    return eError;
3836};
3837#endif
3838