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