Home | History | Annotate | Download | only in src
      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_JpegEncoder.c
     30 *
     31 * This file implements OMX Component for JPEG 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 *! 22-May-2006 mf: Revisions appear in reverse chronological order;
     45 *! that is, newest first.  The date format is dd-Mon-yyyy.
     46 * ================================================================================= */
     47 
     48 /****************************************************************
     49 *  INCLUDE FILES
     50 ****************************************************************/
     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 <unistd.h>
     59 #include <sys/time.h>
     60 #include <sys/types.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/select.h>
     63 #include <errno.h>
     64 #include <pthread.h>
     65 #endif
     66 
     67 #include <string.h>
     68 #include <fcntl.h>
     69 #include <stdlib.h>
     70 #include <stdio.h>
     71 #include <dbapi.h>
     72 
     73 
     74 /*------- Program Header Files ----------------------------------------*/
     75 
     76 #include "OMX_JpegEnc_Utils.h"
     77 #include "OMX_JpegEnc_CustomCmd.h"
     78 
     79 #ifdef RESOURCE_MANAGER_ENABLED
     80     #include <ResourceManagerProxyAPI.h>
     81 #endif
     82 /*----------------------Global-----------------------------------*/
     83 OMX_STRING cJPEGencName = "OMX.TI.JPEG.encoder";
     84 /*--------function prototypes ---------------------------------*/
     85 static OMX_ERRORTYPE JPEGENC_SetCallbacks (OMX_HANDLETYPE hComp,
     86                                    OMX_CALLBACKTYPE* pCallBacks,
     87                                    OMX_PTR pAppData);
     88 
     89 static OMX_ERRORTYPE JPEGENC_GetComponentVersion (OMX_HANDLETYPE hComp,
     90                                           OMX_STRING pComponentName,
     91                                           OMX_VERSIONTYPE* pComponentVersion,
     92                                           OMX_VERSIONTYPE* pSpecVersion,
     93                                           OMX_UUIDTYPE* pComponentUUID);
     94 
     95 static OMX_ERRORTYPE JPEGENC_SendCommand (OMX_HANDLETYPE hComponent,
     96                                   OMX_COMMANDTYPE Cmd,
     97                                   OMX_U32 nParam,
     98                                   OMX_PTR pCmdData);
     99 
    100 static OMX_ERRORTYPE JPEGENC_GetParameter (OMX_IN OMX_HANDLETYPE hComponent,
    101                                    OMX_IN OMX_INDEXTYPE nParamIndex,
    102                                    OMX_INOUT OMX_PTR ComponentParameterStructure);
    103 
    104 static OMX_ERRORTYPE JPEGENC_SetParameter (OMX_HANDLETYPE hComp,
    105                                    OMX_INDEXTYPE nParamIndex,
    106                                    OMX_PTR ComponentParameterStructure);
    107 
    108 static OMX_ERRORTYPE JPEGENC_GetConfig (OMX_HANDLETYPE hComp,
    109                                 OMX_INDEXTYPE nConfigIndex,
    110                                 OMX_PTR ComponentConfigStructure);
    111 
    112 static OMX_ERRORTYPE JPEGENC_SetConfig (OMX_HANDLETYPE hComp,
    113                                 OMX_INDEXTYPE nConfigIndex,
    114                                 OMX_PTR ComponentConfigStructure);
    115 
    116 static OMX_ERRORTYPE JPEGENC_EmptyThisBuffer (OMX_HANDLETYPE hComp,
    117                                       OMX_BUFFERHEADERTYPE* pBuffer);
    118 
    119 static OMX_ERRORTYPE JPEGENC_FillThisBuffer (OMX_HANDLETYPE hComp,
    120                                      OMX_BUFFERHEADERTYPE* pBuffer);
    121 
    122 static OMX_ERRORTYPE JPEGENC_GetState (OMX_HANDLETYPE hComp,
    123                                OMX_STATETYPE* pState);
    124 
    125 static OMX_ERRORTYPE JPEGENC_ComponentTunnelRequest(OMX_IN  OMX_HANDLETYPE hComp,
    126                                             OMX_IN  OMX_U32 nPort,
    127                                             OMX_IN  OMX_HANDLETYPE hTunneledComp,
    128                                             OMX_IN  OMX_U32 nTunneledPort,
    129                                             OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup);
    130 
    131 
    132 static OMX_ERRORTYPE JPEGENC_VerifyTunnelConnection(JPEG_PORT_TYPE*pPort,
    133                                             OMX_HANDLETYPE hTunneledComp,
    134 					    OMX_PARAM_PORTDEFINITIONTYPE* pPortDef,
    135 					    struct OMX_TI_Debug *dbg);
    136 
    137 static OMX_ERRORTYPE JPEGENC_UseBuffer(OMX_IN OMX_HANDLETYPE hComponent,
    138                                OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
    139                                OMX_IN OMX_U32 nPortIndex,
    140                                OMX_IN OMX_PTR pAppPrivate,
    141                                OMX_IN OMX_U32 nSizeBytes,
    142                                OMX_IN OMX_U8* pBuffer);
    143 
    144 static OMX_ERRORTYPE JPEGENC_FreeBuffer(OMX_IN  OMX_HANDLETYPE hComponent,
    145                                 OMX_IN  OMX_U32 nPortIndex,
    146                                 OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
    147 
    148 static OMX_ERRORTYPE JPEGENC_ComponentDeInit(OMX_HANDLETYPE pHandle);
    149 
    150 static OMX_ERRORTYPE JPEGENC_AllocateBuffer(OMX_IN OMX_HANDLETYPE hComponent,
    151                              OMX_INOUT OMX_BUFFERHEADERTYPE** pBuffer,
    152                              OMX_IN OMX_U32 nPortIndex,
    153                              OMX_IN OMX_PTR pAppPrivate,
    154                              OMX_IN OMX_U32 nSizeBytes);
    155 
    156 static void JPEGENC_InitBufferFlagTrack(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nPortIndex);
    157 
    158 #ifdef KHRONOS_1_1
    159 static OMX_ERRORTYPE ComponentRoleEnum(
    160                 OMX_IN OMX_HANDLETYPE hComponent,
    161                 OMX_OUT OMX_U8 *cRole,
    162                 OMX_IN OMX_U32 nIndex);
    163 #endif
    164 
    165 static OMX_ERRORTYPE JPEGENC_GetExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent,
    166                                                 OMX_IN OMX_STRING cParameterName,
    167                                                 OMX_OUT OMX_INDEXTYPE* pIndexType);
    168 
    169 
    170 /*-------- Function Implementations ---------------------------------*/
    171 
    172 
    173 static OMX_ERRORTYPE JPEGENC_FreeBuffer(OMX_IN  OMX_HANDLETYPE hComponent,
    174                                         OMX_IN  OMX_U32 nPortIndex,
    175                                         OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
    176 {
    177     OMX_COMPONENTTYPE *pHandle                   = NULL;
    178     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
    179     OMX_PARAM_PORTDEFINITIONTYPE* pPortDef       = NULL;
    180     JPEGENC_BUFFER_PRIVATE* pBuffPrivate         = NULL;
    181     JPEG_PORT_TYPE* pCompPort                    = NULL;
    182     OMX_ERRORTYPE eError                         = OMX_ErrorNone;
    183     char* pTemp                                  = NULL;
    184 
    185     OMX_CHECK_PARAM(hComponent);
    186     OMX_CHECK_PARAM(pBuffer);
    187 
    188     pHandle = (OMX_COMPONENTTYPE *) hComponent;
    189     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *) pHandle->pComponentPrivate;
    190     pCompPort = pComponentPrivate->pCompPort[nPortIndex];
    191 
    192     OMX_PRINT1(pComponentPrivate->dbg, "JPEGENC_FreeBuffer %p at port %d\n", pBuffer, (int)nPortIndex);
    193 
    194 #ifdef __PERF_INSTRUMENTATION__
    195     /* never frees buffer */
    196     PERF_SendingFrame(pComponentPrivate->pPERF,
    197                       pBuffer->pBuffer,
    198                       pBuffer->nFilledLen,
    199                       PERF_ModuleMax);
    200 #endif
    201 
    202     pPortDef = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
    203 
    204     if ( nPortIndex == pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->nPortIndex ) {
    205         pBuffPrivate = pBuffer->pInputPortPrivate;
    206     }
    207     else if ( nPortIndex == pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->nPortIndex  ) {
    208         pBuffPrivate = pBuffer->pOutputPortPrivate;
    209     }
    210     else {
    211             eError = OMX_ErrorBadPortIndex;
    212             goto PRINT_EXIT;
    213     }
    214 
    215     if (pBuffPrivate->bAllocByComponent == OMX_TRUE) {
    216         if (pBuffer->pBuffer) {
    217            pTemp = (char*)pBuffer->pBuffer;
    218            pTemp -= 128;
    219            OMX_FREE(pTemp);
    220            pBuffer->pBuffer = NULL;
    221         }
    222     }
    223 
    224     OMX_FREE(pBuffer);
    225 
    226     if ( pPortDef->bEnabled &&
    227         ((pComponentPrivate->nCurState == OMX_StateIdle && pComponentPrivate->nToState != OMX_StateLoaded) ||
    228          pComponentPrivate->nCurState == OMX_StateExecuting ||
    229          pComponentPrivate->nCurState == OMX_StatePause) ) {
    230         pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
    231                                       pComponentPrivate->pHandle->pApplicationPrivate,
    232                                       OMX_EventError,
    233                                       OMX_ErrorPortUnpopulated,
    234                                       OMX_TI_ErrorMinor,
    235                                       "Port Unpopulated");
    236         }
    237 
    238         pCompPort->nBuffCount--;
    239     OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEGE: bPopulated %d\n", pComponentPrivate->pCompPort[nPortIndex]->pPortDef->bPopulated);
    240     if (pCompPort->nBuffCount == 0) {
    241         pComponentPrivate->pCompPort[nPortIndex]->pPortDef->bPopulated = OMX_FALSE;
    242 
    243         pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
    244         pthread_cond_signal(&pComponentPrivate->unpopulate_cond);
    245         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
    246 
    247         JPEGENC_InitBufferFlagTrack(pComponentPrivate, nPortIndex);
    248     }
    249 
    250     if ((!pPortDef->bEnabled) &&
    251         (!pComponentPrivate->pCompPort[nPortIndex]->pPortDef->bPopulated)) {
    252         if ((nPortIndex == 0) && (!pComponentPrivate->bInportDisableIncomplete)) {
    253             pComponentPrivate->bInportDisableIncomplete = OMX_TRUE;
    254             pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
    255                                                 pComponentPrivate->pHandle->pApplicationPrivate,
    256                                                 OMX_EventCmdComplete,
    257                                                 OMX_CommandPortDisable,
    258                                                 JPEGENC_INP_PORT,
    259                                                 NULL);
    260         }
    261         if ((nPortIndex == 1) && (!pComponentPrivate->bOutportDisableIncomplete)) {
    262             pComponentPrivate->bOutportDisableIncomplete = OMX_TRUE;
    263             pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
    264                                                 pComponentPrivate->pHandle->pApplicationPrivate,
    265                                                 OMX_EventCmdComplete,
    266                                                 OMX_CommandPortDisable,
    267                                                 JPEGENC_OUT_PORT,
    268                                                 NULL);
    269         }
    270     }
    271 
    272 PRINT_EXIT:
    273     OMX_PRINT1(pComponentPrivate->dbg, "Exit from FreeBuffer\n");
    274 EXIT:
    275     return eError;
    276 }
    277 
    278 OMX_ERRORTYPE JPEGENC_UseBuffer(OMX_IN OMX_HANDLETYPE hComponent,
    279             OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
    280             OMX_IN OMX_U32 nPortIndex,
    281             OMX_IN OMX_PTR pAppPrivate,
    282             OMX_IN OMX_U32 nSizeBytes,
    283             OMX_IN OMX_U8* pBuffer)
    284 
    285 {
    286     OMX_COMPONENTTYPE *pHandle                      = NULL;
    287     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate    = NULL;
    288     OMX_PARAM_PORTDEFINITIONTYPE* pPortDef          = NULL;
    289     OMX_ERRORTYPE eError                            = OMX_ErrorNone;
    290     OMX_HANDLETYPE hTunnelComponent                 = NULL;
    291     OMX_U32 nBufferCount                            = -1;
    292     OMX_BUFFERHEADERTYPE *pBuffHeader               = NULL;
    293     int nInpIndex;
    294     int nOutIndex;
    295 
    296 
    297     OMX_CHECK_PARAM(hComponent);
    298     OMX_CHECK_PARAM(ppBufferHdr);
    299     OMX_CHECK_PARAM(pBuffer);
    300 
    301     if (nPortIndex != 0 && nPortIndex != 1) {
    302         eError = OMX_ErrorBadParameter;
    303         goto EXIT;
    304     }
    305 
    306     pHandle = (OMX_COMPONENTTYPE *)hComponent;
    307     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    308     hTunnelComponent = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent;
    309     pPortDef = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
    310     nBufferCount = pComponentPrivate->pCompPort[nPortIndex]->nBuffCount;
    311 
    312 #ifdef __PERF_INSTRUMENTATION__
    313     PERF_ReceivedFrame(pComponentPrivate->pPERF,
    314                        pBuffer,
    315                        nSizeBytes,
    316                        PERF_ModuleLLMM);
    317 #endif
    318 
    319     pPortDef = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
    320 
    321 
    322     nInpIndex = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->nPortIndex;
    323     nOutIndex = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->nPortIndex;
    324 
    325     if ( (int)nPortIndex == nInpIndex ) {
    326         OMX_MALLOC(pBuffHeader, sizeof(OMX_BUFFERHEADERTYPE));
    327         OMX_CONF_INIT_STRUCT(pBuffHeader, OMX_BUFFERHEADERTYPE);
    328         pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[nBufferCount]->pBufferHdr = pBuffHeader;
    329         *ppBufferHdr = pBuffHeader;
    330     }
    331     else if ( (int)nPortIndex == nOutIndex ) {
    332         OMX_MALLOC(pBuffHeader, sizeof(OMX_BUFFERHEADERTYPE));
    333         OMX_CONF_INIT_STRUCT(pBuffHeader, OMX_BUFFERHEADERTYPE);
    334         pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[nBufferCount]->pBufferHdr = pBuffHeader;
    335         *ppBufferHdr = pBuffHeader;
    336     }
    337     else {
    338         eError = OMX_ErrorBadPortIndex;
    339         goto EXIT;
    340     }
    341 
    342     /* set relevant fields */
    343     (*ppBufferHdr)->nSize       = sizeof(OMX_BUFFERHEADERTYPE);
    344     (*ppBufferHdr)->nVersion    = pPortDef->nVersion;
    345     (*ppBufferHdr)->pBuffer     = pBuffer;
    346     (*ppBufferHdr)->nAllocLen   = nSizeBytes;
    347     (*ppBufferHdr)->pAppPrivate = pAppPrivate;
    348     pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->bAllocByComponent = OMX_FALSE;
    349 
    350     /* set direction dependent fields */
    351     if (hTunnelComponent != NULL) {
    352 
    353         if ( pPortDef->eDir == OMX_DirInput ) {
    354             (*ppBufferHdr)->nInputPortIndex   = nPortIndex;
    355             (*ppBufferHdr)->nOutputPortIndex  = pComponentPrivate->pCompPort[nPortIndex]->nTunnelPort;
    356             /* pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->pBufferHdr = (*ppBufferHdr); */
    357 
    358         }
    359         else {
    360             (*ppBufferHdr)->nOutputPortIndex   = nPortIndex;
    361             (*ppBufferHdr)->nInputPortIndex    = pComponentPrivate->pCompPort[nPortIndex]->nTunnelPort;
    362             /* pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->pBufferHdr = (*ppBufferHdr); */
    363         }
    364     }
    365     else {
    366         if (nPortIndex == JPEGENC_INP_PORT) {
    367             pBuffHeader->nInputPortIndex  = JPEGENC_INP_PORT;
    368             pBuffHeader->nOutputPortIndex = OMX_NOPORT;
    369         }
    370         else {
    371             pBuffHeader->nInputPortIndex  = OMX_NOPORT;
    372             pBuffHeader->nOutputPortIndex = JPEGENC_OUT_PORT;
    373         }
    374     }
    375     if (nPortIndex == JPEGENC_INP_PORT) {
    376         pBuffHeader->pInputPortPrivate = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount];
    377     }
    378     else {
    379         pBuffHeader->pOutputPortPrivate = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount];
    380     }
    381 
    382     if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent != NULL) {
    383         pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->eBufferOwner = JPEGENC_BUFFER_TUNNEL_COMPONENT;
    384     }
    385     else {
    386         pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->eBufferOwner = JPEGENC_BUFFER_CLIENT;
    387     }
    388 
    389     if ( nPortIndex == pComponentPrivate->pCompPort[nPortIndex]->pPortFormat->nPortIndex ) {
    390         pComponentPrivate->pCompPort[nPortIndex]->nBuffCount++;
    391         if (pComponentPrivate->pCompPort[nPortIndex]->nBuffCount == pPortDef->nBufferCountActual) {
    392             pPortDef->bPopulated = OMX_TRUE;
    393 
    394             pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
    395             pthread_cond_signal(&pComponentPrivate->populate_cond);
    396             pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
    397 
    398             JPEGENC_InitBufferFlagTrack(pComponentPrivate, nPortIndex);
    399         }
    400     }
    401 
    402 EXIT:
    403     return eError;
    404 
    405 }
    406 /*-------------------------------------------------------------------*/
    407 /**
    408   * OMX_ComponentInit() Set the all the function pointers of component
    409   *
    410   * This method will update the component function pointer to the handle
    411   *
    412   * @param hComp         handle for this instance of the component
    413   *
    414   * @retval OMX_NoError              Success, ready to roll
    415   *         OMX_ErrorInsufficientResources If the malloc fails
    416   **/
    417 /*-------------------------------------------------------------------*/
    418 OMX_ERRORTYPE OMX_ComponentInit (OMX_HANDLETYPE hComponent)
    419 {
    420     OMX_COMPONENTTYPE *pHandle  = NULL;
    421     OMX_ERRORTYPE eError            = OMX_ErrorNone;
    422     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
    423     OMX_U8 i = 0;
    424 
    425     /* Allocate memory for component's private data area */
    426     OMX_CHECK_PARAM(hComponent);
    427 
    428 
    429     LinkedList_Create(&AllocList);
    430 
    431     pHandle = (OMX_COMPONENTTYPE *)hComponent;
    432     OMX_MALLOC(pHandle->pComponentPrivate, sizeof(JPEGENC_COMPONENT_PRIVATE));
    433 
    434     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    435 
    436     /* get default settings for debug */
    437     OMX_DBG_INIT(pComponentPrivate->dbg, "OMX_DBG_JPGENC");
    438     /* Allocate memory for component's private data area */
    439     OMX_PRINT1(pComponentPrivate->dbg, "in jpeg-enc OMX_ComponentInit\n");
    440 
    441 #ifdef __PERF_INSTRUMENTATION__
    442         pComponentPrivate->pPERF = PERF_Create(PERF_FOURCC('J','P','E',' '),
    443                                                PERF_ModuleLLMM | PERF_ModuleImageEncode);
    444         pComponentPrivate->pPERFcomp = NULL;
    445 #endif
    446 
    447     /**Assigning address of Component Structure point to place holder inside comp
    448     nentprivate structure
    449     */
    450     ((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pHandle = pHandle;
    451 
    452     pComponentPrivate->nCurState = OMX_StateLoaded;
    453 
    454     pHandle->SetCallbacks               = JPEGENC_SetCallbacks;
    455     pHandle->GetComponentVersion        = JPEGENC_GetComponentVersion;
    456     pHandle->SendCommand                = JPEGENC_SendCommand;
    457     pHandle->GetParameter               = JPEGENC_GetParameter;
    458     pHandle->SetParameter               = JPEGENC_SetParameter;
    459     pHandle->GetConfig                  = JPEGENC_GetConfig;
    460     pHandle->SetConfig                  = JPEGENC_SetConfig;
    461     pHandle->GetState                   = JPEGENC_GetState;
    462     pHandle->EmptyThisBuffer            = JPEGENC_EmptyThisBuffer;
    463     pHandle->FillThisBuffer             = JPEGENC_FillThisBuffer;
    464     pHandle->ComponentTunnelRequest     = JPEGENC_ComponentTunnelRequest;
    465     pHandle->ComponentDeInit            = JPEGENC_ComponentDeInit;
    466     pHandle->UseBuffer                  = JPEGENC_UseBuffer    ;
    467     pHandle->FreeBuffer                 = JPEGENC_FreeBuffer;
    468     pHandle->AllocateBuffer             = JPEGENC_AllocateBuffer;
    469     pHandle->GetExtensionIndex = JPEGENC_GetExtensionIndex;
    470 #ifdef KHRONOS_1_1
    471     pHandle->ComponentRoleEnum          = ComponentRoleEnum;
    472 #endif
    473     OMX_PRINT2(pComponentPrivate->dbg, "Inside Component Init JPEG encoder\n");
    474     pComponentPrivate->ComponentVersion.s.nVersionMajor = 0x01;
    475     pComponentPrivate->ComponentVersion.s.nVersionMinor = 0x00;
    476     pComponentPrivate->ComponentVersion.s.nRevision = 0x00;
    477     pComponentPrivate->ComponentVersion.s.nStep = 0x00;
    478     OMX_MALLOC(pComponentPrivate->cComponentName, COMP_MAX_NAMESIZE+1);
    479     strncpy(pComponentPrivate->cComponentName, cJPEGencName, COMP_MAX_NAMESIZE);
    480 
    481     /* Allocate memory for component data structures */
    482     OMX_MALLOC(pComponentPrivate->pPortParamType, sizeof(OMX_PORT_PARAM_TYPE));
    483     OMX_MALLOC(pComponentPrivate->pPortParamTypeAudio, sizeof(OMX_PORT_PARAM_TYPE));
    484     OMX_MALLOC(pComponentPrivate->pPortParamTypeVideo, sizeof(OMX_PORT_PARAM_TYPE));
    485     OMX_MALLOC(pComponentPrivate->pPortParamTypeOthers, sizeof(OMX_PORT_PARAM_TYPE));
    486     OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT], sizeof(JPEG_PORT_TYPE));
    487     OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT], sizeof(JPEG_PORT_TYPE));
    488     OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
    489     OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
    490     OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
    491     OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
    492     OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pParamBufSupplier, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE));
    493     OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pParamBufSupplier, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE));
    494     OMX_MALLOC(pComponentPrivate->pPriorityMgmt, sizeof(OMX_PRIORITYMGMTTYPE));
    495     OMX_MALLOC(pComponentPrivate->pQualityfactor, sizeof(OMX_IMAGE_PARAM_QFACTORTYPE));
    496     OMX_MALLOC(pComponentPrivate->pCrop, sizeof(OMX_CONFIG_RECTTYPE));
    497     OMX_MALLOC(pComponentPrivate->pCustomLumaQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
    498     OMX_MALLOC(pComponentPrivate->pCustomChromaQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
    499     OMX_MALLOC(pComponentPrivate->pHuffmanTable, sizeof(JPEGENC_CUSTOM_HUFFMANTTABLETYPE));
    500     for (i = 0;i<NUM_OF_BUFFERSJPEG;i++) {
    501         OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i], sizeof(JPEGENC_BUFFER_PRIVATE));
    502     }
    503 
    504     for (i = 0;i<NUM_OF_BUFFERSJPEG;i++) {
    505         pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr = NULL;
    506     }
    507 
    508     for (i = 0;i<NUM_OF_BUFFERSJPEG;i++) {
    509         OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i], sizeof(JPEGENC_BUFFER_PRIVATE));
    510     }
    511 
    512     for (i = 0;i<NUM_OF_BUFFERSJPEG;i++) {
    513         pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr = NULL;
    514     }
    515 
    516     pComponentPrivate->nFlags               = 0;
    517     pComponentPrivate->isLCMLActive         = 0;
    518     pComponentPrivate->nCommentFlag         = 0;
    519     /* pComponentPrivate->nThumbnailFlag   = 0; */
    520     pComponentPrivate->pMarkData            = NULL;
    521     pComponentPrivate->hMarkTargetComponent = NULL;
    522     pComponentPrivate->pString_Comment      = NULL;
    523     pComponentPrivate->nDRI_Interval        = 0;
    524     pComponentPrivate->bSetHuffmanTable = OMX_FALSE;
    525     pComponentPrivate->bSetLumaQuantizationTable = OMX_FALSE;
    526     pComponentPrivate->bSetChromaQuantizationTable = OMX_FALSE;
    527     pComponentPrivate->bConvert420pTo422i = OMX_FALSE;
    528 #ifdef __JPEG_OMX_PPLIB_ENABLED__
    529     pComponentPrivate->bPPLibEnable = OMX_TRUE;
    530 #else
    531     pComponentPrivate->bPPLibEnable = OMX_FALSE;
    532 #endif
    533 
    534     pComponentPrivate->InParams.pInParams = NULL;
    535     pComponentPrivate->InParams.size = 0;
    536     pComponentPrivate->bPreempted = OMX_FALSE;
    537 
    538 #ifdef __JPEG_OMX_PPLIB_ENABLED__
    539     pComponentPrivate->pOutParams = NULL;
    540 #endif
    541 
    542 
    543     OMX_MALLOC(pComponentPrivate->pDynParams, sizeof(IDMJPGE_TIGEM_DynamicParams)+256);
    544     if (!pComponentPrivate->pDynParams) {
    545         eError = OMX_ErrorInsufficientResources;
    546         goto EXIT;
    547     }
    548 
    549     eError = SetJpegEncInParams(pComponentPrivate);
    550 
    551 #ifdef KHRONOS_1_1
    552     strcpy((char *)pComponentPrivate->componentRole.cRole, "image_encoder.jpeg");
    553 #endif
    554 
    555     /* Set pPortParamType defaults */
    556     OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamType, OMX_PORT_PARAM_TYPE);
    557     pComponentPrivate->pPortParamType->nPorts = NUM_OF_PORTS;
    558     pComponentPrivate->pPortParamType->nStartPortNumber = 0;
    559 
    560     OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamTypeAudio, OMX_PORT_PARAM_TYPE);
    561     pComponentPrivate->pPortParamTypeAudio->nPorts = 0;
    562     pComponentPrivate->pPortParamTypeAudio->nStartPortNumber = -1;
    563 
    564     OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamTypeVideo, OMX_PORT_PARAM_TYPE);
    565     pComponentPrivate->pPortParamTypeVideo->nPorts = 0;
    566     pComponentPrivate->pPortParamTypeVideo->nStartPortNumber = -1;
    567 
    568     OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamTypeOthers, OMX_PORT_PARAM_TYPE);
    569     pComponentPrivate->pPortParamTypeOthers->nPorts = 0;
    570     pComponentPrivate->pPortParamTypeOthers->nStartPortNumber = -1;
    571 
    572     /*Set Quality factor structure */
    573 /*    OMX_CONF_INIT_STRUCT(pComponentPrivate->pQualityfactor, OMX_IMAGE_PARAM_QFACTORTYPE);*/
    574 
    575     /* Set pInPortDef defaults */
    576     OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
    577     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nPortIndex            = JPEGENC_INP_PORT;
    578     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->eDir                  = OMX_DirInput;
    579     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual    = NUM_OF_BUFFERSJPEG;
    580     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountMin       = 1;
    581     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferSize           = 460800; /* 50688; */
    582     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bEnabled              = OMX_TRUE;
    583     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bPopulated            = OMX_FALSE;
    584     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->eDomain               = OMX_PortDomainImage;
    585 
    586     pComponentPrivate->isLCMLActive = 0;
    587     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.cMIMEType             = "JPEGENC";
    588     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.pNativeRender         = NULL;
    589     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.nFrameWidth           = 640;  /* 176; */
    590     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.nFrameHeight          = 480; /* 144; */
    591     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.nStride               = -1;
    592     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.nSliceHeight          = -1;
    593     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.bFlagErrorConcealment = OMX_FALSE;
    594     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.eCompressionFormat    =  OMX_IMAGE_CodingUnused; /* OMX_IMAGE_CodingJPEG; */
    595     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.eColorFormat          =  OMX_COLOR_FormatYUV420PackedPlanar; /* OMX_COLOR_FormatCbYCrY; */
    596 
    597 
    598     /* Set pOutPortDef defaults */
    599     OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
    600     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nPortIndex         = JPEGENC_OUT_PORT;
    601     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->eDir               = OMX_DirOutput;
    602     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountMin    = 1;
    603     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountActual = NUM_OF_BUFFERSJPEG;
    604     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferSize        = 460800; /* 50688; */
    605     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bEnabled           = OMX_TRUE;
    606     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bPopulated         = OMX_FALSE;
    607     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->eDomain            = OMX_PortDomainImage;
    608     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.cMIMEType        = "JPEGENC";
    609     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.pNativeRender    = NULL;
    610     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.nFrameWidth      = 640; /* 176; */
    611     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.nFrameHeight     = 480; /* 144; */
    612     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.nStride          = -1;
    613     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.nSliceHeight     = -1;
    614     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.bFlagErrorConcealment    = OMX_FALSE;
    615     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.eCompressionFormat       = OMX_IMAGE_CodingJPEG; /* OMX_IMAGE_CodingUnused; */
    616     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.eColorFormat             =  OMX_COLOR_FormatUnused; /* OMX_COLOR_FormatCbYCrY; */
    617 
    618 
    619    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCustomLumaQuantTable, OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE);
    620     pComponentPrivate->pCustomLumaQuantTable->nPortIndex = JPEGENC_OUT_PORT;
    621     pComponentPrivate->pCustomLumaQuantTable->eQuantizationTable = OMX_IMAGE_QuantizationTableLuma;
    622 
    623    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCustomChromaQuantTable, OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE);
    624     pComponentPrivate->pCustomChromaQuantTable->nPortIndex = JPEGENC_OUT_PORT;
    625     pComponentPrivate->pCustomChromaQuantTable->eQuantizationTable = OMX_IMAGE_QuantizationTableChroma;
    626 
    627    OMX_CONF_INIT_STRUCT(pComponentPrivate->pHuffmanTable, JPEGENC_CUSTOM_HUFFMANTTABLETYPE);
    628     pComponentPrivate->pHuffmanTable->nPortIndex = JPEGENC_OUT_PORT;
    629 
    630     /* Set pInPortFormat defaults */
    631     OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat, OMX_IMAGE_PARAM_PORTFORMATTYPE);
    632     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->nPortIndex         = JPEGENC_INP_PORT;
    633     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->nIndex             = 0x0;
    634     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->eCompressionFormat = OMX_IMAGE_CodingUnused; /* OMX_IMAGE_CodingJPEG; */
    635     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->eColorFormat       = OMX_COLOR_FormatYUV420PackedPlanar; /* OMX_COLOR_FormatCbYCrY; */
    636 
    637     /* Set pOutPortFormat defaults */
    638     OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat, OMX_IMAGE_PARAM_PORTFORMATTYPE);
    639     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->nPortIndex         = JPEGENC_OUT_PORT;
    640     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->nIndex             = 0x0;
    641     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->eCompressionFormat = OMX_IMAGE_CodingJPEG; /* OMX_IMAGE_CodingUnused; */
    642     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->eColorFormat       = OMX_COLOR_FormatUnused; /* OMX_COLOR_FormatCbYCrY; */
    643 
    644     /* Set pPriorityMgmt defaults */
    645     OMX_CONF_INIT_STRUCT(pComponentPrivate->pPriorityMgmt, OMX_PRIORITYMGMTTYPE);
    646     pComponentPrivate->pPriorityMgmt->nGroupPriority = -1;
    647     pComponentPrivate->pPriorityMgmt->nGroupID       = -1;
    648 
    649     OMX_CONF_INIT_STRUCT(pComponentPrivate->pCrop, OMX_CONFIG_RECTTYPE);
    650     OMX_CONF_INIT_STRUCT(pComponentPrivate->pQualityfactor, OMX_IMAGE_PARAM_QFACTORTYPE);
    651     pComponentPrivate->pQualityfactor->nPortIndex = 1;
    652     pComponentPrivate->pQualityfactor->nQFactor   = 100;
    653 
    654    pComponentPrivate->sAPP0.bMarkerEnabled = OMX_FALSE;
    655    pComponentPrivate->sAPP1.bMarkerEnabled = OMX_FALSE;
    656    pComponentPrivate->sAPP5.bMarkerEnabled = OMX_FALSE;
    657    pComponentPrivate->sAPP13.bMarkerEnabled = OMX_FALSE;
    658 
    659    pComponentPrivate->sAPP0.pMarkerBuffer = NULL;
    660    pComponentPrivate->sAPP1.pMarkerBuffer = NULL;
    661    pComponentPrivate->sAPP5.pMarkerBuffer = NULL;
    662    pComponentPrivate->sAPP13.pMarkerBuffer = NULL;
    663 
    664     OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pParamBufSupplier, OMX_PARAM_BUFFERSUPPLIERTYPE);
    665     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pParamBufSupplier->nPortIndex      = JPEGENC_INP_PORT;
    666     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pParamBufSupplier->eBufferSupplier = 0x0;
    667 
    668     /* Set pOutBufSupplier defaults */
    669     OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pParamBufSupplier, OMX_PARAM_BUFFERSUPPLIERTYPE);
    670     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pParamBufSupplier->nPortIndex      = JPEGENC_OUT_PORT;
    671     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pParamBufSupplier->eBufferSupplier = 0x0;
    672 
    673 
    674 
    675     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufSupplier= OMX_BufferSupplyOutput;
    676     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent = NULL;
    677     pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->nBuffCount       = 0;
    678 
    679 
    680     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->hTunnelComponent = NULL;
    681     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->nBuffCount       = 0;
    682 
    683     pthread_mutex_init(&pComponentPrivate->jpege_mutex, NULL);
    684     pthread_cond_init(&pComponentPrivate->stop_cond, NULL);
    685     pthread_cond_init(&pComponentPrivate->flush_cond, NULL);
    686     /* pthread_cond_init(&pComponentPrivate->control_cond, NULL); */
    687 
    688     pthread_mutex_init(&pComponentPrivate->jpege_mutex_app, NULL);
    689     pthread_cond_init(&pComponentPrivate->populate_cond, NULL);
    690     pthread_cond_init(&pComponentPrivate->unpopulate_cond, NULL);
    691 
    692     if(pthread_mutex_init(&pComponentPrivate->mutexStateChangeRequest, NULL)) {
    693        return OMX_ErrorUndefined;
    694     }
    695 
    696     if(pthread_cond_init (&pComponentPrivate->StateChangeCondition, NULL)) {
    697        return OMX_ErrorUndefined;
    698     }
    699     pComponentPrivate->nPendingStateChangeRequests = 0;
    700 
    701 #ifdef RESOURCE_MANAGER_ENABLED
    702     /* load the ResourceManagerProxy thread */
    703     eError = RMProxy_NewInitalizeEx(OMX_COMPONENTTYPE_IMAGE);
    704     if ( eError != OMX_ErrorNone ) {
    705         OMX_PRMGR4(pComponentPrivate->dbg, "Error returned from loading ResourceManagerProxy thread\n");
    706         goto EXIT;
    707     }
    708 
    709 #endif
    710     eError = JPEGEnc_Start_ComponentThread(pHandle);
    711     if (eError) {
    712         OMX_PRINT4(pComponentPrivate->dbg, "Error while initializing thread\n");
    713         goto EXIT;
    714     }
    715 
    716     OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG-ENC: actual buffer %d\n", (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual));
    717  EXIT:
    718     if(eError != OMX_ErrorNone){
    719         /* LinkedList_DisplayAll (&AllocList); */
    720         OMX_FREEALL();
    721         LinkedList_Destroy(&AllocList);
    722     }
    723     return eError;
    724 }
    725 
    726 
    727 /*-------------------------------------------------------------------*/
    728 /**
    729   *  JPEGENC_SetCallbacks() Sets application callbacks to the component
    730   *
    731   * This method will update application callbacks
    732   * the application.
    733   *
    734   * @param pComponent    handle for this instance of the component
    735   * @param pCallBacks    application callbacks
    736   * @param pAppData      pointer to application Data
    737   *
    738   * @retval OMX_NoError              Success, ready to roll
    739   *         OMX_Error_BadParameter   The input parameter pointer is null
    740   **/
    741 /*-------------------------------------------------------------------*/
    742 static OMX_ERRORTYPE JPEGENC_SetCallbacks (OMX_HANDLETYPE pComponent,
    743                                    OMX_CALLBACKTYPE* pCallBacks,
    744                                    OMX_PTR pAppData)
    745 {
    746     OMX_ERRORTYPE eError        = OMX_ErrorNone;
    747     OMX_COMPONENTTYPE *pHandle  = NULL;
    748     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
    749 
    750     OMX_CHECK_PARAM(pComponent);
    751     OMX_CHECK_PARAM(pCallBacks);
    752 
    753     pHandle = (OMX_COMPONENTTYPE*)pComponent;
    754     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    755 
    756     /*Copy the callbacks of the application to the component private  */
    757     memcpy (&(pComponentPrivate->cbInfo), pCallBacks, sizeof(OMX_CALLBACKTYPE));
    758     OMX_MEMCPY_CHECK(&(pComponentPrivate->cbInfo));
    759 
    760     /*Copy the application private data to component memory  */
    761     pHandle->pApplicationPrivate = pAppData;
    762     pComponentPrivate->nCurState = OMX_StateLoaded;
    763 
    764 EXIT:
    765     return eError;
    766 }
    767 
    768 
    769 /*-------------------------------------------------------------------*/
    770 /**
    771   *  JPEGENC_GetComponentVersion() Sets application callbacks to the component
    772   *
    773   * This method will get the component Version.
    774   *
    775   * @param hComp         handle for this instance of the component
    776   * @param pComponentName    application callbacks
    777   * @param pComponentVersion
    778   * @param pSpecVersion
    779   * @param pComponentUUID
    780   *
    781   * @retval OMX_NoError              Success, ready to roll
    782   *         OMX_Error_BadParameter   The input parameter pointer is null
    783   **/
    784 /*-------------------------------------------------------------------*/
    785 static OMX_ERRORTYPE JPEGENC_GetComponentVersion (OMX_HANDLETYPE hComp,
    786                                           OMX_STRING szComponentName,
    787                                           OMX_VERSIONTYPE* pComponentVersion,
    788                                           OMX_VERSIONTYPE* pSpecVersion,
    789                                           OMX_UUIDTYPE* pComponentUUID)
    790 {
    791     OMX_ERRORTYPE          eError            = OMX_ErrorNone;
    792     OMX_COMPONENTTYPE    * pHandle           = NULL;
    793     JPEGENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
    794     OMX_U8 *pTemp = NULL;
    795     if (!hComp || !szComponentName || !pComponentVersion || !pSpecVersion || !pComponentUUID) {
    796         eError = OMX_ErrorBadParameter;
    797         goto EXIT;
    798     }
    799     pHandle = (OMX_COMPONENTTYPE*)hComp;
    800     if (!pHandle->pComponentPrivate) {
    801         eError = OMX_ErrorBadParameter;
    802         goto EXIT;
    803     }
    804     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
    805 
    806     strncpy(szComponentName, pComponentPrivate->cComponentName, COMP_MAX_NAMESIZE);
    807     pTemp = memcpy(pComponentVersion, &(pComponentPrivate->ComponentVersion.s), sizeof(pComponentPrivate->ComponentVersion.s));
    808     if(pTemp == NULL){
    809         eError = OMX_ErrorUndefined;
    810         goto EXIT;
    811     }
    812     pTemp = memcpy(pSpecVersion, &(pComponentPrivate->SpecVersion.s), sizeof(pComponentPrivate->SpecVersion.s));
    813     if(pTemp == NULL){
    814         eError = OMX_ErrorUndefined;
    815         goto EXIT;
    816     }
    817 EXIT:
    818     return eError;
    819 }
    820 
    821 /*-------------------------------------------------------------------*/
    822 /**
    823   *  JPEGENC_SendCommand() Send commands to the
    824   *
    825   * This method will update application callbacks
    826   * the application.
    827   *
    828   * @param phandle       handle for this instance of the component
    829   * @param Cmd           OMX Command usually OMX_CommandStateSet
    830   * @param nParam
    831   *
    832   * @retval OMX_NoError              Success, ready to roll
    833   *         OMX_Error_BadParameter   The input parameter pointer is null
    834   **/
    835 /*-------------------------------------------------------------------*/
    836 static OMX_ERRORTYPE JPEGENC_SendCommand (
    837              OMX_HANDLETYPE hComponent,
    838              OMX_COMMANDTYPE Cmd,
    839              OMX_U32 nParam,
    840              OMX_PTR pCmdData
    841              )
    842 {
    843     OMX_ERRORTYPE                eError         = OMX_ErrorNone;
    844     OMX_COMPONENTTYPE            *pHandle       = NULL;
    845     JPEGENC_COMPONENT_PRIVATE    *pCompPrivate  = NULL;
    846     OMX_PARAM_PORTDEFINITIONTYPE *pPortDefIn    = NULL;
    847     OMX_PARAM_PORTDEFINITIONTYPE *pPortDefOut   = NULL;
    848     OMX_MARKTYPE *pMarkType = NULL;
    849     OMX_COMMANDTYPE eCmd         = -1;
    850     int nRet                = 0;
    851 
    852     OMX_CHECK_PARAM(hComponent);
    853 
    854     pHandle      = (OMX_COMPONENTTYPE *)hComponent;
    855     pCompPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    856     pPortDefIn   = pCompPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
    857     pPortDefOut  = pCompPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
    858 
    859     OMX_PRINT2(pCompPrivate->dbg, "Print nParam = %d\n", (int)nParam);
    860 
    861     if ( pCompPrivate->nCurState == OMX_StateInvalid ) {
    862         eError = OMX_ErrorInvalidState;
    863         goto EXIT;
    864     }
    865 
    866     switch ( Cmd ) {
    867     case OMX_CommandStateSet:
    868          /* Add a pending transition */
    869          if(AddStateTransition(pCompPrivate) != OMX_ErrorNone) {
    870              return OMX_ErrorUndefined;
    871          }
    872         eCmd = SetState;
    873         pCompPrivate->nToState = nParam;
    874         break;
    875     case OMX_CommandFlush:
    876 	if ((nParam != 0) && (nParam != 1) && ((int)nParam != -1)) {
    877             eError = OMX_ErrorBadPortIndex;
    878             goto EXIT;
    879         }
    880         eCmd = Flush;
    881         break;
    882     case OMX_CommandPortDisable:
    883 	if (nParam == JPEGENC_INP_PORT  || (int)nParam == -1) {
    884             pPortDefIn->bEnabled = OMX_FALSE;
    885             pCompPrivate->bInportDisableIncomplete = OMX_FALSE;
    886         }
    887         if (nParam == JPEGENC_OUT_PORT  || (int)nParam == -1) {
    888             pPortDefOut->bEnabled = OMX_FALSE;
    889             pCompPrivate->bOutportDisableIncomplete = OMX_FALSE;
    890         }
    891         if ((nParam != JPEGENC_INP_PORT) && (nParam != JPEGENC_OUT_PORT) && ((int)nParam != -1))
    892         {
    893             eError = OMX_ErrorBadParameter;
    894             goto EXIT;
    895         }
    896 
    897         if (!pPortDefIn->bPopulated) {
    898             pCompPrivate->bInportDisableIncomplete = OMX_TRUE;
    899             pCompPrivate->cbInfo.EventHandler (pCompPrivate->pHandle,
    900             pCompPrivate->pHandle->pApplicationPrivate,
    901             OMX_EventCmdComplete,
    902             OMX_CommandPortDisable,
    903             JPEGENC_INP_PORT,
    904             NULL);
    905         }
    906 
    907         if (!pPortDefOut->bPopulated) {
    908             pCompPrivate->bOutportDisableIncomplete = OMX_TRUE;
    909             pCompPrivate->cbInfo.EventHandler (pCompPrivate->pHandle,
    910                             pCompPrivate->pHandle->pApplicationPrivate,
    911                             OMX_EventCmdComplete,
    912                             OMX_CommandPortDisable,
    913                             JPEGENC_OUT_PORT,
    914                             NULL);
    915         }
    916         eCmd = OMX_CommandPortDisable;
    917         break;
    918 
    919     case OMX_CommandPortEnable:
    920 	if (nParam == JPEGENC_INP_PORT  || (int)nParam == -1) {
    921             pPortDefIn->bEnabled = OMX_TRUE;
    922         }
    923         if (nParam == JPEGENC_OUT_PORT  || (int)nParam == -1) {
    924             pPortDefOut->bEnabled = OMX_TRUE;
    925         }
    926         if ((nParam != JPEGENC_INP_PORT) && (nParam != JPEGENC_OUT_PORT) && ((int)nParam != -1)) {
    927             eError = OMX_ErrorBadParameter;
    928             goto EXIT;
    929         }
    930         eCmd = OMX_CommandPortEnable;
    931         break;
    932 
    933     case OMX_CommandMarkBuffer:
    934         if ((nParam != JPEGENC_OUT_PORT) && (nParam != JPEGENC_INP_PORT)) {
    935             eError = OMX_ErrorBadPortIndex;
    936             goto EXIT;
    937         }
    938         pMarkType = (OMX_MARKTYPE *)pCmdData;
    939         pCompPrivate->pMarkData = pMarkType->pMarkData;
    940         pCompPrivate->hMarkTargetComponent = pMarkType->hMarkTargetComponent;
    941         pCompPrivate->nMarkPort = nParam;
    942         OMX_PRBUFFER2(pCompPrivate->dbg, "IN SendCommand, port %d mark %p\n", pCompPrivate->nMarkPort, pCompPrivate->pMarkData);
    943         goto EXIT;
    944         break;
    945     default:
    946         break;
    947 
    948     }
    949 
    950     nRet = write(pCompPrivate->nCmdPipe[1], &eCmd, sizeof(eCmd));
    951     if (nRet == -1) {
    952        if(RemoveStateTransition(pCompPrivate, OMX_FALSE) != OMX_ErrorNone) {
    953            return OMX_ErrorUndefined;
    954        }
    955        return  OMX_ErrorUndefined;
    956     }
    957 
    958 #ifdef __PERF_INSTRUMENTATION__
    959         PERF_SendingCommand(pCompPrivate->pPERF,
    960                             Cmd,
    961                             (Cmd == OMX_CommandMarkBuffer) ? (OMX_U32) pCmdData : nParam,
    962                             PERF_ModuleComponent);
    963 #endif
    964 
    965 
    966     nRet = write(pCompPrivate->nCmdDataPipe[1], &nParam, sizeof(nParam));
    967     if (nRet == -1) {
    968        if(RemoveStateTransition(pCompPrivate, OMX_FALSE) != OMX_ErrorNone) {
    969            return OMX_ErrorUndefined;
    970        }
    971        return  OMX_ErrorUndefined;
    972     }
    973 
    974 EXIT:
    975 
    976     return eError;
    977 }
    978 
    979 
    980 /*-------------------------------------------------------------------*/
    981 /**
    982   *  JPEGENC_GetParameter() Gets Parameters from the Component
    983   *
    984   * This method will update parameters from the component to the app.
    985   *
    986   * @param hComp         handle for this instance of the component
    987   * @param nParamIndex   Component Index Port
    988   * @param ComponentParameterStructure Component Parameter Structure
    989   *
    990   * @retval OMX_NoError              Success, ready to roll
    991   *         OMX_Error_BadParameter   The input parameter pointer is null
    992   **/
    993 /*-------------------------------------------------------------------*/
    994 static OMX_ERRORTYPE JPEGENC_GetParameter (OMX_IN OMX_HANDLETYPE hComponent,
    995                                    OMX_IN OMX_INDEXTYPE nParamIndex,
    996                                    OMX_INOUT OMX_PTR pComponentParameterStructure)
    997 {
    998     OMX_COMPONENTTYPE         *pComp             = NULL;
    999     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
   1000     OMX_ERRORTYPE             eError             = OMX_ErrorNone;
   1001     JPEG_PORT_TYPE            *pInpPortType      = NULL;
   1002     JPEG_PORT_TYPE            *pOutPortType      = NULL;
   1003 
   1004     OMX_CHECK_PARAM(hComponent);
   1005     OMX_CHECK_PARAM(pComponentParameterStructure);
   1006 
   1007     pComp = (OMX_COMPONENTTYPE *)hComponent;
   1008     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)pComp->pComponentPrivate;
   1009     OMX_PRINT1(pComponentPrivate->dbg, "Entering function\n");
   1010 
   1011     if ( pComponentPrivate->nCurState == OMX_StateInvalid ) {
   1012         eError = OMX_ErrorInvalidState;
   1013         goto PRINT_EXIT;
   1014     }
   1015     pInpPortType = pComponentPrivate->pCompPort[JPEGENC_INP_PORT];
   1016     pOutPortType = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT];
   1017 
   1018     switch ( nParamIndex ) {
   1019     case OMX_IndexParamImageInit:
   1020         memcpy(pComponentParameterStructure, pComponentPrivate->pPortParamType, sizeof(OMX_PORT_PARAM_TYPE));
   1021         OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1022         break;
   1023     case OMX_IndexParamAudioInit:
   1024         memcpy(pComponentParameterStructure, pComponentPrivate->pPortParamTypeAudio, sizeof(OMX_PORT_PARAM_TYPE));
   1025         OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1026         break;
   1027     case OMX_IndexParamVideoInit:
   1028         memcpy(pComponentParameterStructure, pComponentPrivate->pPortParamTypeVideo, sizeof(OMX_PORT_PARAM_TYPE));
   1029         OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1030         break;
   1031     case OMX_IndexParamOtherInit:
   1032         memcpy(pComponentParameterStructure, pComponentPrivate->pPortParamTypeOthers, sizeof(OMX_PORT_PARAM_TYPE));
   1033         OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1034         break;
   1035     case OMX_IndexParamPortDefinition:
   1036     {
   1037        OMX_PARAM_PORTDEFINITIONTYPE *pParamPortDef  = NULL;
   1038 
   1039        pParamPortDef = (OMX_PARAM_PORTDEFINITIONTYPE *)pComponentParameterStructure;
   1040        OMX_PRINT2(pComponentPrivate->dbg, "index is %d (%d) (%d)\n",
   1041 		  (int)(pParamPortDef->nPortIndex),
   1042 		  (int)(pInpPortType->pPortDef->nPortIndex),
   1043 		  (int)(pOutPortType->pPortDef->nPortIndex));
   1044         if (pParamPortDef->nPortIndex == pInpPortType->pPortDef->nPortIndex)
   1045         {
   1046 	    OMX_PRBUFFER2(pComponentPrivate->dbg, "nBufferCountActual %d (0) \n", (int)(pInpPortType->pPortDef->nBufferCountActual));
   1047             /* pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bPopulated = OMX_FALSE; */
   1048             memcpy(pComponentParameterStructure, pInpPortType->pPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
   1049             OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1050         } else if (pParamPortDef->nPortIndex == pOutPortType->pPortDef->nPortIndex)
   1051         {
   1052 	    OMX_PRBUFFER2(pComponentPrivate->dbg, "nBufferCountActual %d (1)\n", (int)(pOutPortType->pPortDef->nBufferCountActual));
   1053             /* pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bPopulated = OMX_FALSE; */
   1054             memcpy(pComponentParameterStructure, pOutPortType->pPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
   1055             OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1056         } else {
   1057             eError = OMX_ErrorBadPortIndex;
   1058         }
   1059     }
   1060         break;
   1061 
   1062     case OMX_IndexParamImagePortFormat:
   1063         {
   1064         OMX_IMAGE_PARAM_PORTFORMATTYPE *pParamImagePortFormat = NULL;
   1065 
   1066         pParamImagePortFormat = (OMX_IMAGE_PARAM_PORTFORMATTYPE *)pComponentParameterStructure;
   1067         if ( pParamImagePortFormat->nPortIndex == pInpPortType->pPortFormat->nPortIndex )
   1068         {
   1069             if (pParamImagePortFormat->nIndex > pInpPortType->pPortFormat->nIndex ) {
   1070                 eError = OMX_ErrorNoMore;
   1071             } else {
   1072                 memcpy(pComponentParameterStructure, pInpPortType->pPortFormat, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
   1073                 OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1074             }
   1075         } else if (pParamImagePortFormat->nPortIndex == pOutPortType->pPortFormat->nPortIndex )
   1076         {
   1077             if ( pParamImagePortFormat->nIndex > pOutPortType->pPortFormat->nIndex ) {
   1078                 eError = OMX_ErrorNoMore;
   1079             } else {
   1080                 memcpy(pComponentParameterStructure, pOutPortType->pPortFormat, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
   1081                 OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1082             }
   1083         } else {
   1084             eError = OMX_ErrorBadPortIndex;
   1085         }
   1086         }
   1087         break;
   1088 
   1089     case OMX_IndexParamPriorityMgmt:
   1090         memcpy(pComponentParameterStructure, pComponentPrivate->pPriorityMgmt, sizeof(OMX_PRIORITYMGMTTYPE));
   1091         OMX_MEMCPY_CHECK(pComponentParameterStructure);
   1092         break;
   1093 
   1094     case OMX_IndexParamCompBufferSupplier:
   1095         {
   1096         OMX_PARAM_BUFFERSUPPLIERTYPE *pBuffSupplierParam = (OMX_PARAM_BUFFERSUPPLIERTYPE *)pComponentParameterStructure;
   1097 
   1098         if ( pBuffSupplierParam->nPortIndex == pOutPortType->pPortDef->nPortIndex)
   1099         {
   1100             pBuffSupplierParam->eBufferSupplier = pOutPortType->pParamBufSupplier->eBufferSupplier;
   1101         } else if ( pBuffSupplierParam->nPortIndex == pInpPortType->pPortDef->nPortIndex )
   1102         {
   1103             pBuffSupplierParam->eBufferSupplier = pInpPortType->pParamBufSupplier->eBufferSupplier;
   1104         } else
   1105         {
   1106             eError = OMX_ErrorBadPortIndex;
   1107             break;
   1108         }
   1109         }
   1110         break;
   1111     case OMX_IndexParamQuantizationTable:
   1112         {
   1113         OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE *pQuantTable = (OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE *)pComponentParameterStructure;
   1114         if (pQuantTable->eQuantizationTable == OMX_IMAGE_QuantizationTableLuma) {
   1115             memcpy(pQuantTable, pComponentPrivate->pCustomLumaQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
   1116         }
   1117         else if (pQuantTable->eQuantizationTable == OMX_IMAGE_QuantizationTableChroma) {
   1118             memcpy(pQuantTable, pComponentPrivate->pCustomChromaQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
   1119         }
   1120         else { /* wrong eQuantizationTable, return error */
   1121            eError = OMX_ErrorBadParameter;
   1122         }
   1123         break;
   1124         }
   1125     case OMX_IndexCustomHuffmanTable:
   1126         {
   1127         JPEGENC_CUSTOM_HUFFMANTTABLETYPE *pHuffmanTable = (JPEGENC_CUSTOM_HUFFMANTTABLETYPE *)pComponentParameterStructure;
   1128         memcpy(pHuffmanTable, pComponentPrivate->pHuffmanTable, sizeof(JPEGENC_CUSTOM_HUFFMANTTABLETYPE));
   1129         break;
   1130         }
   1131     default:
   1132         eError = OMX_ErrorUnsupportedIndex;
   1133         break;
   1134     }
   1135 PRINT_EXIT:
   1136     OMX_PRINT1(pComponentPrivate->dbg, "Exit function eError = %x\n", eError);
   1137 EXIT:
   1138     return eError;
   1139 
   1140 }
   1141 
   1142 
   1143 /*-------------------------------------------------------------------*/
   1144 /**
   1145   *  JPEGENC_SetParameter() Gets the parameters sent by the Application and sets it to the component.
   1146   *
   1147   * This method will update component parameters .
   1148   *
   1149   *
   1150   * @param hComp         handle for this instance of the component
   1151   * @param nParamIndex    Component Params Index Port
   1152   * @param ComponentParameterStructure Component Parameter Structure
   1153   *
   1154   * @retval OMX_NoError              Success, ready to roll
   1155   *         OMX_Error_BadParameter   The input parameter pointer is null
   1156   **/
   1157 /*-------------------------------------------------------------------*/
   1158 static OMX_ERRORTYPE JPEGENC_SetParameter (OMX_HANDLETYPE hComponent,
   1159                                            OMX_INDEXTYPE nParamIndex,
   1160                                            OMX_PTR pCompParam)
   1161 {
   1162     OMX_COMPONENTTYPE         * pHandle           = NULL;
   1163     JPEGENC_COMPONENT_PRIVATE * pComponentPrivate = NULL;
   1164     OMX_ERRORTYPE               eError            = OMX_ErrorNone;
   1165     JPEG_PORT_TYPE            *pInpPortType       = NULL;
   1166     JPEG_PORT_TYPE            *pOutPortType       = NULL;
   1167 #ifdef KHRONOS_1_1
   1168     OMX_PARAM_COMPONENTROLETYPE  *pRole;
   1169 #endif
   1170 
   1171     OMX_CHECK_PARAM(hComponent);
   1172     OMX_CHECK_PARAM(pCompParam);
   1173 
   1174     pHandle= (OMX_COMPONENTTYPE*)hComponent;
   1175     pComponentPrivate = pHandle->pComponentPrivate;
   1176 
   1177     if ( pComponentPrivate->nCurState != OMX_StateLoaded ) {
   1178         eError = OMX_ErrorIncorrectStateOperation;
   1179         goto EXIT;
   1180     }
   1181 
   1182     pInpPortType = pComponentPrivate->pCompPort[JPEGENC_INP_PORT];
   1183     pOutPortType = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT];
   1184     OMX_CHECK_PARAM(pInpPortType);
   1185     OMX_CHECK_PARAM(pOutPortType);
   1186 
   1187     switch ( nParamIndex ) {
   1188     case OMX_IndexParamImagePortFormat:
   1189     {
   1190         OMX_IMAGE_PARAM_PORTFORMATTYPE* pComponentParam = (OMX_IMAGE_PARAM_PORTFORMATTYPE *)pCompParam;
   1191         OMX_MEMCPY_CHECK(pInpPortType->pPortFormat);
   1192         OMX_MEMCPY_CHECK(pOutPortType->pPortFormat);
   1193         if ( pComponentParam->nPortIndex == pInpPortType->pPortFormat->nPortIndex )
   1194         {
   1195             memcpy(pInpPortType->pPortFormat, pComponentParam, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
   1196         }
   1197         else if ( pComponentParam->nPortIndex == pOutPortType->pPortFormat->nPortIndex ) {
   1198             memcpy(pOutPortType->pPortFormat, pComponentParam, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
   1199         }
   1200         else {
   1201              eError = OMX_ErrorBadPortIndex;
   1202         }
   1203         break;
   1204     }
   1205     case OMX_IndexParamImageInit:
   1206     {
   1207         OMX_MEMCPY_CHECK(pComponentPrivate->pPortParamType);
   1208         memcpy(pComponentPrivate->pPortParamType, (OMX_PORT_PARAM_TYPE*)pCompParam, sizeof(OMX_PORT_PARAM_TYPE));
   1209         break;
   1210     }
   1211     case OMX_IndexParamPortDefinition:
   1212     {
   1213         OMX_PARAM_PORTDEFINITIONTYPE* pComponentParam = (OMX_PARAM_PORTDEFINITIONTYPE *)pCompParam;
   1214         OMX_MEMCPY_CHECK(pInpPortType->pPortDef);
   1215         OMX_MEMCPY_CHECK(pOutPortType->pPortDef);
   1216         if ( pComponentParam->nPortIndex == pInpPortType->pPortDef->nPortIndex ) {
   1217             memcpy(pInpPortType->pPortDef, pComponentParam, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
   1218         }
   1219         else if ( pComponentParam->nPortIndex == pOutPortType->pPortDef->nPortIndex ) {
   1220             memcpy(pOutPortType->pPortDef, pComponentParam, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
   1221         }
   1222         else {
   1223             eError = OMX_ErrorBadPortIndex;
   1224         }
   1225         break;
   1226     }
   1227     case OMX_IndexParamPriorityMgmt:
   1228     {
   1229         OMX_MEMCPY_CHECK(pComponentPrivate->pPriorityMgmt);
   1230         memcpy(pComponentPrivate->pPriorityMgmt, (OMX_PRIORITYMGMTTYPE*)pCompParam, sizeof(OMX_PRIORITYMGMTTYPE));
   1231         break;
   1232     }
   1233     case OMX_IndexParamQFactor:
   1234     {
   1235         OMX_MEMCPY_CHECK(pComponentPrivate->pQualityfactor);
   1236         memcpy(pComponentPrivate->pQualityfactor, (OMX_IMAGE_PARAM_QFACTORTYPE*)pCompParam, sizeof(OMX_IMAGE_PARAM_QFACTORTYPE));
   1237         break;
   1238     }
   1239     case OMX_IndexParamCompBufferSupplier:
   1240     {
   1241         OMX_PARAM_BUFFERSUPPLIERTYPE *pBuffSupplierParam = (OMX_PARAM_BUFFERSUPPLIERTYPE *)pCompParam;
   1242 
   1243         if ( pBuffSupplierParam->nPortIndex == 1 /*pOutPortType->pBuffSupplierParam->nPortIndex */) {
   1244             /* Copy parameters to input port buffer supplier type */
   1245             pOutPortType->pBufSupplier = pBuffSupplierParam->eBufferSupplier;
   1246         }
   1247         else if ( pBuffSupplierParam->nPortIndex == 0 /* pInpPortType->pBuffSupplierParam->nPortIndex */ ) {
   1248             pInpPortType->pBufSupplier = pBuffSupplierParam->eBufferSupplier;
   1249         }
   1250         else {
   1251             eError = OMX_ErrorBadPortIndex;
   1252             break;
   1253         }
   1254         break;
   1255     }
   1256 #ifdef KHRONOS_1_1
   1257     case OMX_IndexParamStandardComponentRole:
   1258         if (pCompParam) {
   1259             pRole = (OMX_PARAM_COMPONENTROLETYPE *)pCompParam;
   1260             memcpy(&(pComponentPrivate->componentRole), (void *)pRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
   1261         } else {
   1262             eError = OMX_ErrorBadParameter;
   1263         }
   1264         break;
   1265 #endif
   1266    case OMX_IndexParamQuantizationTable:
   1267     {
   1268         OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE *pQuantTable = (OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE *)pCompParam;
   1269         if (pQuantTable->eQuantizationTable == OMX_IMAGE_QuantizationTableLuma) {
   1270             memcpy(pComponentPrivate->pCustomLumaQuantTable, pQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
   1271             pComponentPrivate->bSetLumaQuantizationTable = OMX_TRUE;
   1272             eError = SetJpegEncInParams(pComponentPrivate);
   1273         }
   1274         else if (pQuantTable->eQuantizationTable == OMX_IMAGE_QuantizationTableChroma) {
   1275             memcpy(pComponentPrivate->pCustomChromaQuantTable, pQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
   1276             pComponentPrivate->bSetChromaQuantizationTable = OMX_TRUE;
   1277             eError = SetJpegEncInParams(pComponentPrivate);
   1278         }
   1279         else { /* wrong eQuantizationTable, return error */
   1280            eError = OMX_ErrorBadParameter;
   1281         }
   1282         break;
   1283     }
   1284    case OMX_IndexCustomHuffmanTable:
   1285     {
   1286         JPEGENC_CUSTOM_HUFFMANTTABLETYPE *pHuffmanTable = (JPEGENC_CUSTOM_HUFFMANTTABLETYPE *)pCompParam;
   1287         if (pHuffmanTable->nPortIndex == pOutPortType->pPortDef->nPortIndex) {
   1288             memcpy(pComponentPrivate->pHuffmanTable, pHuffmanTable, sizeof(JPEGENC_CUSTOM_HUFFMANTTABLETYPE));
   1289             pComponentPrivate->bSetHuffmanTable = OMX_TRUE;
   1290             eError = SetJpegEncInParams(pComponentPrivate);
   1291         } else { /* wrong nPortIndex, return error */
   1292            eError = OMX_ErrorBadPortIndex;
   1293         }
   1294         break;
   1295     }
   1296         break;  default:
   1297         eError = OMX_ErrorUnsupportedIndex;
   1298         break;
   1299     }
   1300 
   1301     EXIT:
   1302     return eError;
   1303 }
   1304 
   1305 
   1306 /*-------------------------------------------------------------------*/
   1307 /**
   1308   *  JPEGENC_GetConfig() Gets Configuration from the Component
   1309   *
   1310   * This method will update component config structure .(NOT IMPLEMENTED)
   1311   *
   1312   * @param hComp         handle for this instance of the component
   1313   * @param nConfigIndex   Component Config Index Port
   1314   * @param ComponentConfigStructure Component Config Structure
   1315   *
   1316   * @retval OMX_NoError              Success, ready to roll
   1317   *         OMX_Error_BadParameter   The input parameter pointer is null
   1318   **/
   1319 /*-------------------------------------------------------------------*/
   1320 static OMX_ERRORTYPE JPEGENC_GetConfig (OMX_HANDLETYPE hComp,
   1321                                         OMX_INDEXTYPE nConfigIndex,
   1322                                         OMX_PTR ComponentConfigStructure)
   1323 {
   1324     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1325     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE*)hComp;
   1326     OMX_CHECK_PARAM(hComp);
   1327 
   1328     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = pHandle->pComponentPrivate;
   1329 
   1330     OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGENC_GetConfig function\n");
   1331     switch ( nConfigIndex ) {
   1332     case OMX_IndexCustomDebug:
   1333 	OMX_DBG_GETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
   1334 	break;
   1335     default:
   1336         eError = OMX_ErrorUnsupportedIndex;
   1337         break;
   1338     }
   1339  EXIT:
   1340     return eError;
   1341 }
   1342 
   1343 
   1344 /*-------------------------------------------------------------------*/
   1345 /**
   1346   *  JPEGENC_SetConfig() Gets Configuration structure to the component.
   1347   *
   1348   * This method will update application callbacks
   1349   * the application.
   1350   *
   1351   * @param pComp         handle for this instance of the component
   1352   * @param pCallBacks    application callbacks
   1353   * @param ptr
   1354   *
   1355   * @retval OMX_NoError              Success, ready to roll
   1356   *         OMX_Error_BadParameter   The input parameter pointer is null
   1357   **/
   1358 /*-------------------------------------------------------------------*/
   1359 
   1360 static OMX_ERRORTYPE JPEGENC_SetConfig (OMX_HANDLETYPE hComp,
   1361                                         OMX_INDEXTYPE nConfigIndex,
   1362                                         OMX_PTR ComponentConfigStructure)
   1363 {
   1364     OMX_ERRORTYPE      eError   = OMX_ErrorNone;
   1365     OMX_COMPONENTTYPE* pHandle  = NULL;
   1366     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
   1367 
   1368     OMX_CHECK_PARAM(hComp);
   1369 
   1370     pHandle = (OMX_COMPONENTTYPE *)hComp;
   1371 
   1372     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
   1373     switch ( nConfigIndex ) {
   1374     case OMX_IndexCustomCommentFlag :
   1375     {
   1376         int *nComment = (int*)ComponentConfigStructure;
   1377         if ( nComment == NULL ) {
   1378             eError = OMX_ErrorBadParameter;
   1379             goto EXIT;
   1380         }
   1381         ((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->nCommentFlag = *nComment;
   1382 
   1383 
   1384         break;
   1385     }
   1386 
   1387 	case OMX_IndexCustomAPP0:
   1388 		{
   1389 			JPEG_APPTHUMB_MARKER *pMarkerInfo = (JPEG_APPTHUMB_MARKER *) ComponentConfigStructure;
   1390 
   1391 			if (pComponentPrivate->sAPP0.pMarkerBuffer != NULL) {
   1392 					OMX_FREE(pComponentPrivate->sAPP0.pMarkerBuffer);
   1393 			}
   1394 
   1395 			memcpy (&pComponentPrivate->sAPP0, pMarkerInfo, sizeof(JPEG_APPTHUMB_MARKER));
   1396 			OMX_MEMCPY_CHECK(&pComponentPrivate->sAPP0);
   1397 			if (pMarkerInfo->pMarkerBuffer != NULL) {
   1398 				OMX_MALLOC(pComponentPrivate->sAPP0.pMarkerBuffer, pMarkerInfo->nMarkerSize);
   1399 				memcpy (pComponentPrivate->sAPP0.pMarkerBuffer, pMarkerInfo->pMarkerBuffer, pMarkerInfo->nMarkerSize);
   1400 				OMX_MEMCPY_CHECK(pComponentPrivate->sAPP0.pMarkerBuffer);
   1401 			}
   1402 
   1403 			eError = SetJpegEncInParams(pComponentPrivate);
   1404         break;
   1405 		}
   1406 
   1407 	case OMX_IndexCustomAPP1:
   1408 		{
   1409 			JPEG_APPTHUMB_MARKER *pMarkerInfo = (JPEG_APPTHUMB_MARKER *) ComponentConfigStructure;
   1410 
   1411 			if (pComponentPrivate->sAPP1.pMarkerBuffer != NULL) {
   1412 				OMX_FREE(pComponentPrivate->sAPP1.pMarkerBuffer);
   1413 			}
   1414 
   1415 			memcpy (&pComponentPrivate->sAPP1, pMarkerInfo, sizeof(JPEG_APPTHUMB_MARKER));
   1416 			OMX_MEMCPY_CHECK(&pComponentPrivate->sAPP1);
   1417 			if (pMarkerInfo->pMarkerBuffer != NULL) {
   1418 				OMX_MALLOC(pComponentPrivate->sAPP1.pMarkerBuffer, pMarkerInfo->nMarkerSize);
   1419 				memcpy (pComponentPrivate->sAPP1.pMarkerBuffer, pMarkerInfo->pMarkerBuffer, pMarkerInfo->nMarkerSize);
   1420 				OMX_MEMCPY_CHECK(pComponentPrivate->sAPP1.pMarkerBuffer);
   1421 			}
   1422 
   1423 			eError = SetJpegEncInParams(pComponentPrivate);
   1424 			break;
   1425 		}
   1426 
   1427 	case OMX_IndexCustomAPP5:
   1428 		{
   1429 			JPEG_APPTHUMB_MARKER *pMarkerInfo = (JPEG_APPTHUMB_MARKER *) ComponentConfigStructure;
   1430 
   1431 			if (pComponentPrivate->sAPP5.pMarkerBuffer != NULL) {
   1432 				OMX_FREE(pComponentPrivate->sAPP5.pMarkerBuffer);
   1433 			}
   1434 
   1435 			memcpy (&pComponentPrivate->sAPP5, pMarkerInfo, sizeof(JPEG_APPTHUMB_MARKER));
   1436 			OMX_MEMCPY_CHECK(&pComponentPrivate->sAPP5);
   1437 			if (pMarkerInfo->pMarkerBuffer != NULL) {
   1438 				OMX_MALLOC(pComponentPrivate->sAPP5.pMarkerBuffer, pMarkerInfo->nMarkerSize);
   1439 				memcpy (pComponentPrivate->sAPP5.pMarkerBuffer, pMarkerInfo->pMarkerBuffer, pMarkerInfo->nMarkerSize);
   1440 				OMX_MEMCPY_CHECK(pComponentPrivate->sAPP5.pMarkerBuffer);
   1441 			}
   1442 			eError = SetJpegEncInParams(pComponentPrivate);
   1443 			break;
   1444 		}
   1445 
   1446 	case OMX_IndexCustomAPP13:
   1447 		{
   1448 			JPEG_APP13_MARKER *pMarkerInfo = (JPEG_APP13_MARKER *) ComponentConfigStructure;
   1449 			if (pComponentPrivate->sAPP13.pMarkerBuffer != NULL) {
   1450 				OMX_FREE(pComponentPrivate->sAPP13.pMarkerBuffer);
   1451 			}
   1452 
   1453 			memcpy (&pComponentPrivate->sAPP13, pMarkerInfo, sizeof(JPEG_APP13_MARKER));
   1454 			OMX_MEMCPY_CHECK(&pComponentPrivate->sAPP13);
   1455 			if (pMarkerInfo->pMarkerBuffer != NULL) {
   1456 				OMX_MALLOC(pComponentPrivate->sAPP13.pMarkerBuffer, pMarkerInfo->nMarkerSize);
   1457 				memcpy (pComponentPrivate->sAPP13.pMarkerBuffer, pMarkerInfo->pMarkerBuffer, pMarkerInfo->nMarkerSize);
   1458 				OMX_MEMCPY_CHECK(pComponentPrivate->sAPP13.pMarkerBuffer);
   1459 			}
   1460 
   1461 			eError = SetJpegEncInParams(pComponentPrivate);
   1462 			break;
   1463 		}
   1464 
   1465     case OMX_IndexCustomDRI:
   1466         pComponentPrivate->nDRI_Interval = *(OMX_U8 *)ComponentConfigStructure;
   1467         break;
   1468 
   1469     case OMX_IndexCustomCommentString :
   1470     {
   1471         if(((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pString_Comment == NULL){
   1472             OMX_MALLOC(((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pString_Comment , 256);
   1473         }
   1474         strncpy((char *)((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pString_Comment, (char *)ComponentConfigStructure, 255);
   1475         eError = SetJpegEncInParams(pComponentPrivate);
   1476         break;
   1477     }
   1478     case OMX_IndexCustomInputFrameWidth:
   1479     {
   1480         OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1481         int *nWidth= (int*)ComponentConfigStructure;
   1482 
   1483         pPortDefIn = ((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pCompPort[JPEGENC_INP_PORT]->pPortDef;
   1484         OMX_PRINT1(pComponentPrivate->dbg, "INIT nFrameHeight = %d\n", (int)(pPortDefIn->format.image.nFrameHeight));
   1485         OMX_PRINT1(pComponentPrivate->dbg, "INIT nFrameWidth = %d\n", (int)(pPortDefIn->format.image.nFrameWidth));
   1486         pPortDefIn->format.image.nFrameWidth = *nWidth;
   1487         OMX_PRINT1(pComponentPrivate->dbg, "nFrameWidth = %d\n", (int)(pPortDefIn->format.image.nFrameWidth));
   1488 #if 0
   1489         eError = SendDynamicParam(pComponentPrivate);
   1490             if (eError != OMX_ErrorNone ) {
   1491                 OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
   1492                 goto EXIT;
   1493         }
   1494 #endif
   1495         break;
   1496     }
   1497     case OMX_IndexCustomInputFrameHeight:
   1498     {
   1499         OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1500         int *nHeight = (int*)ComponentConfigStructure;
   1501 
   1502         pPortDefIn = ((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pCompPort[JPEGENC_INP_PORT]->pPortDef;
   1503         pPortDefIn->format.image.nFrameHeight = *nHeight;
   1504         OMX_PRINT1(pComponentPrivate->dbg, "nFrameHeight = %d\n", (int)(pPortDefIn->format.image.nFrameHeight));
   1505 #if 0
   1506         eError = SendDynamicParam(pComponentPrivate);
   1507             if (eError != OMX_ErrorNone ) {
   1508                 OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
   1509                 goto EXIT;
   1510         }
   1511 #endif
   1512         break;
   1513     }
   1514     case OMX_IndexCustomQFactor:
   1515     {
   1516         OMX_MEMCPY_CHECK(pComponentPrivate->pQualityfactor);
   1517         memcpy(pComponentPrivate->pQualityfactor, (OMX_IMAGE_PARAM_QFACTORTYPE*)ComponentConfigStructure, sizeof(OMX_IMAGE_PARAM_QFACTORTYPE));
   1518         break;
   1519     }
   1520     case OMX_IndexConfigCommonInputCrop :
   1521     {
   1522         OMX_CONFIG_RECTTYPE *crop = (OMX_CONFIG_RECTTYPE*)ComponentConfigStructure;
   1523         if ((crop->nTop != 0) || (crop->nLeft != 0))
   1524         {
   1525             eError = OMX_ErrorBadParameter;
   1526         }
   1527         else{
   1528             pComponentPrivate->pCrop->nWidth = crop->nWidth;
   1529             pComponentPrivate->pCrop->nHeight = crop->nHeight;
   1530         }
   1531         break;
   1532     }
   1533     case OMX_IndexCustomDebug:
   1534         OMX_DBG_SETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
   1535 	break;
   1536 
   1537 	case OMX_IndexCustomColorFormatConvertion_420pTo422i :
   1538 	{
   1539 		pComponentPrivate->bConvert420pTo422i = *((OMX_BOOL*)ComponentConfigStructure);
   1540 		break;
   1541 	}
   1542 
   1543 	 case OMX_IndexCustomPPLibEnable :
   1544 	{
   1545 #ifdef __JPEG_OMX_PPLIB_ENABLED__
   1546 		pComponentPrivate->bPPLibEnable = *((OMX_BOOL*)ComponentConfigStructure);
   1547 #endif
   1548 		break;
   1549 	}
   1550 
   1551     default:
   1552         eError = OMX_ErrorUnsupportedIndex;
   1553         break;
   1554     }
   1555 
   1556     if (pComponentPrivate->nCurState == OMX_StateExecuting || pComponentPrivate->nCurState == OMX_StatePause) {
   1557         eError = SendDynamicParam(pComponentPrivate);
   1558             if (eError != OMX_ErrorNone ) {
   1559                 OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
   1560                 goto EXIT;
   1561         }
   1562     }
   1563 
   1564     OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGENC_SetConfig function\n");
   1565 EXIT:
   1566     return eError;
   1567 }
   1568 
   1569 
   1570 /*-------------------------------------------------------------------*/
   1571 /**
   1572   *  JPEGENC_GetState() Gets OMX Component State
   1573   *
   1574   * This method will return OMX Component State
   1575   *
   1576   * @param pComponent   handle for this instance of the component
   1577   * @param pState       State type
   1578   *
   1579   *
   1580   * @retval OMX_NoError              Success, ready to roll
   1581   *         OMX_Error_BadParameter   The input parameter pointer is null
   1582   **/
   1583 /*-------------------------------------------------------------------*/
   1584 static OMX_ERRORTYPE JPEGENC_GetState (OMX_HANDLETYPE hComponent, OMX_STATETYPE* pState)
   1585 {
   1586     OMX_ERRORTYPE eError                        = OMX_ErrorNone;
   1587     OMX_COMPONENTTYPE* pHandle = NULL;
   1588     JPEGENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
   1589     struct timespec abs_time = {0,0};
   1590     int nPendingStateChangeRequests = 0;
   1591     int ret = 0;
   1592     /* Set to sufficiently high value */
   1593     int mutex_timeout = 3;
   1594 
   1595     if(hComponent == NULL || pState == NULL) {
   1596         return OMX_ErrorBadParameter;
   1597     }
   1598 
   1599 
   1600     pHandle = (OMX_COMPONENTTYPE*)hComponent;
   1601     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
   1602 
   1603     /* Retrieve current state */
   1604     if (pHandle && pHandle->pComponentPrivate) {
   1605         /* Check for any pending state transition requests */
   1606         if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
   1607             return OMX_ErrorUndefined;
   1608         }
   1609         nPendingStateChangeRequests = pComponentPrivate->nPendingStateChangeRequests;
   1610         if(!nPendingStateChangeRequests) {
   1611            if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
   1612                return OMX_ErrorUndefined;
   1613            }
   1614 
   1615            /* No pending state transitions */
   1616 	   *pState = ((JPEGENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate)->nCurState;
   1617             eError = OMX_ErrorNone;
   1618         }
   1619         else {
   1620        	   /* Wait for component to complete state transition */
   1621            clock_gettime(CLOCK_REALTIME, &abs_time);
   1622            abs_time.tv_sec += mutex_timeout;
   1623            abs_time.tv_nsec = 0;
   1624 	   ret = pthread_cond_timedwait(&(pComponentPrivate->StateChangeCondition), &(pComponentPrivate->mutexStateChangeRequest), &abs_time);
   1625            if (!ret) {
   1626               /* Component has completed state transitions*/
   1627               *pState = ((JPEGENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate)->nCurState;
   1628               if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
   1629                  return OMX_ErrorUndefined;
   1630               }
   1631               eError = OMX_ErrorNone;
   1632            }
   1633            else if(ret == ETIMEDOUT) {
   1634               /* Unlock mutex in case of timeout */
   1635               pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest);
   1636               return OMX_ErrorTimeout;
   1637            }
   1638         }
   1639      }
   1640      else {
   1641         eError = OMX_ErrorInvalidComponent;
   1642         *pState = OMX_StateInvalid;
   1643     }
   1644     return eError;
   1645 }
   1646 
   1647 
   1648 /*-------------------------------------------------------------------*/
   1649 /**
   1650   *  JPEGENC_EmptyThisBuffer() Send Input Buffers
   1651   *
   1652   * The application uses this macro to send the input buffers filled with data to the input port of the component
   1653   *
   1654   * @param pComponent    handle for this instance of the component
   1655   * @param nPortIndex    PortIndex
   1656   * @param pBuffHead     Pointer to data filled
   1657   *
   1658   * @retval OMX_NoError              Success, ready to roll
   1659   *         OMX_Error_BadParameter   The input parameter pointer is null
   1660   *     OMX_ErrorInvalidState    Called in a invalid state
   1661   *     OMX_ErrorHardware        Error in Writing to the Data pipe
   1662   **/
   1663 /*-------------------------------------------------------------------*/
   1664 static OMX_ERRORTYPE JPEGENC_EmptyThisBuffer (OMX_HANDLETYPE pComponent,
   1665                                               OMX_BUFFERHEADERTYPE* pBuffHead)
   1666 {
   1667     OMX_ERRORTYPE              eError            = OMX_ErrorNone;
   1668     OMX_COMPONENTTYPE         *pHandle           = NULL;
   1669     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
   1670     JPEGENC_BUFFER_PRIVATE    *pBuffPrivate = NULL;
   1671     OMX_HANDLETYPE             hTunnelComponent;
   1672     int i;
   1673     int ret;
   1674 
   1675     OMX_CHECK_PARAM(pComponent);
   1676     OMX_CHECK_PARAM(pBuffHead);
   1677 
   1678     pHandle = (OMX_COMPONENTTYPE *)pComponent;
   1679     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
   1680     OMX_PRBUFFER1(pComponentPrivate->dbg, "inside JPEGENC_EmptyThisBuffer\n");
   1681     hTunnelComponent = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent;
   1682     pBuffPrivate = pBuffHead->pInputPortPrivate;
   1683 
   1684     if ( pComponentPrivate->nCurState != OMX_StateExecuting &&
   1685         pComponentPrivate->nCurState != OMX_StatePause &&
   1686         pComponentPrivate->nCurState != OMX_StateIdle) {
   1687         eError= OMX_ErrorIncorrectStateOperation;
   1688         goto EXIT;
   1689     }
   1690 
   1691     if ( pBuffHead == NULL ) {
   1692         eError = OMX_ErrorBadParameter;
   1693         goto EXIT;
   1694     }
   1695     if ( pBuffHead->nSize != sizeof(OMX_BUFFERHEADERTYPE) ) {
   1696         eError = OMX_ErrorBadParameter;
   1697         OMX_PRBUFFER4(pComponentPrivate->dbg, "JPEG-ENC: buffer header size is not correct\n");
   1698         goto EXIT;
   1699     }
   1700 
   1701     if (!pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bEnabled) {
   1702         eError = OMX_ErrorIncorrectStateOperation;
   1703         goto EXIT;
   1704     }
   1705 
   1706     if ( (pBuffHead->nVersion.s.nVersionMajor != 0x1) ||
   1707          (pBuffHead->nVersion.s.nVersionMinor != 0x0) ||
   1708          (pBuffHead->nVersion.s.nRevision != 0x0) ||
   1709          (pBuffHead->nVersion.s.nStep != 0x0) ) {
   1710 
   1711         eError= OMX_ErrorVersionMismatch;
   1712         goto EXIT;
   1713     }
   1714 
   1715     if (pBuffHead->nInputPortIndex != 0x0) {
   1716             eError = OMX_ErrorBadPortIndex;
   1717             goto EXIT;
   1718     }
   1719 
   1720 #ifdef __PERF_INSTRUMENTATION__
   1721         PERF_ReceivedFrame(pComponentPrivate->pPERF,
   1722                            pBuffHead->pBuffer,
   1723                            pBuffHead->nFilledLen,
   1724                            PERF_ModuleHLMM);
   1725 #endif
   1726 
   1727     OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nAllocLen = %lu\n", pBuffHead->nAllocLen);
   1728     OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nFilledLen = %lu\n", pBuffHead->nFilledLen);
   1729 
   1730     pComponentPrivate->nInPortIn ++;
   1731 
   1732     OMX_PRBUFFER2(pComponentPrivate->dbg, "EmptyThisBuffer nInPortIn %lu\n", pComponentPrivate->nInPortIn);
   1733 
   1734     if (pBuffHead->nFlags == OMX_BUFFERFLAG_EOS) {
   1735         OMX_PRBUFFER2(pComponentPrivate->dbg, "END OF STREAM DETECTED\n");
   1736         pComponentPrivate->nFlags = OMX_BUFFERFLAG_EOS;
   1737          /* pBuffHead->nFlags = 0; */
   1738         OMX_PRBUFFER2(pComponentPrivate->dbg, "record EOS flag on buffer ID %lu\n", pComponentPrivate->nInPortIn);
   1739         for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual); i ++) {
   1740             if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferFlagTrack[i].buffer_id == 0xFFFFFFFF)
   1741             {
   1742                  OMX_PRBUFFER2(pComponentPrivate->dbg, "record buffer id in array %d\n", i);
   1743                  pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferFlagTrack[i].flag = pBuffHead->nFlags;
   1744                  pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferFlagTrack[i].buffer_id =
   1745                  pComponentPrivate->nInPortIn;
   1746                  break;
   1747              }
   1748          }
   1749      }
   1750 
   1751      /* mark the first buffer from input port after receiving mark buffer command */
   1752      if (pComponentPrivate->nMarkPort == JPEGENC_INP_PORT) {
   1753          if (pComponentPrivate->pMarkData) {
   1754                OMX_PRBUFFER2(pComponentPrivate->dbg, "get mark buffer command, mark buffer %p\n", pBuffHead);
   1755                pBuffHead->pMarkData = pComponentPrivate->pMarkData;
   1756                pBuffHead->hMarkTargetComponent = pComponentPrivate->hMarkTargetComponent;
   1757                pComponentPrivate->pMarkData = NULL;
   1758                pComponentPrivate->hMarkTargetComponent = NULL;
   1759          }
   1760      }
   1761 
   1762      /* if a buffer from input port is marked, record this port # in the buffer queue */
   1763      if (pBuffHead->pMarkData) {
   1764 	     for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual); i ++) {
   1765              if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferMarkTrack[i].buffer_id == 0xFFFFFFFF)
   1766              {
   1767                  JPEGENC_BUFFERMARK_TRACK *pMarkTrack;
   1768                  pMarkTrack = &(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferMarkTrack[i]);
   1769                  pMarkTrack->buffer_id = pComponentPrivate->nInPortIn;
   1770                  pMarkTrack->pMarkData = pBuffHead->pMarkData;
   1771                  pMarkTrack->hMarkTargetComponent = pBuffHead->hMarkTargetComponent;
   1772                  OMX_PRBUFFER2(pComponentPrivate->dbg, "mark buffer at ID %lu\n", pComponentPrivate->nInPortIn);
   1773                  break;
   1774              }
   1775          }
   1776      }
   1777 
   1778 #if 0
   1779     eError = SendDynamicParam(pComponentPrivate);
   1780     if (eError != OMX_ErrorNone ) {
   1781             JOMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
   1782             goto EXIT;
   1783     }
   1784 #endif
   1785 
   1786      pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_IN;
   1787      pBuffPrivate->bReadFromPipe = OMX_FALSE;
   1788 
   1789      OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (get filled input buffer) %lu %lu %lu %lu (%p)\n",
   1790                     pComponentPrivate->nInPortIn,
   1791                     pComponentPrivate->nInPortOut,
   1792                     pComponentPrivate->nOutPortIn,
   1793                     pComponentPrivate->nOutPortOut, pBuffHead);
   1794 
   1795      /*Writing the component buffer corresponding to input buffer to infill_q  */
   1796      OMX_PRCOMM2(pComponentPrivate->dbg, "EmptyThisBuffer: write to the queue %p \n", pBuffHead);
   1797 
   1798      ret = write (pComponentPrivate->filled_inpBuf_Q[1], &(pBuffHead),sizeof(pBuffHead));
   1799 
   1800      if ( ret == -1 ) {
   1801          OMX_PRCOMM4(pComponentPrivate->dbg, "Error in Writing to the Data pipe\n");
   1802          eError = OMX_ErrorHardware;
   1803          goto EXIT;
   1804      }
   1805 
   1806 EXIT:
   1807     return eError;
   1808 }
   1809 
   1810 
   1811 /*-------------------------------------------------------------------*/
   1812 /**
   1813   *  JPEGENC_FillThisBuffer() Empty an output Buffer
   1814   *
   1815   * The component uses this to request the application to
   1816   * empty an output buffer which contains the decoded data..
   1817   * @param pComponent         handle for this instance of the component
   1818   * @param pCallBacks    application callbacks
   1819   * @param ptr
   1820   *
   1821   * @retval OMX_NoError              Success, ready to roll
   1822   *         OMX_Error_BadParameter   The input parameter pointer is null
   1823   **/
   1824 /*-------------------------------------------------------------------*/
   1825 static OMX_ERRORTYPE JPEGENC_FillThisBuffer (OMX_HANDLETYPE pComponent,
   1826                                      OMX_BUFFERHEADERTYPE* pBuffHead)
   1827 {
   1828     OMX_ERRORTYPE eError        = OMX_ErrorNone;
   1829     int err = 0;
   1830     OMX_COMPONENTTYPE *pHandle = NULL;
   1831     JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
   1832     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
   1833 
   1834     OMX_CHECK_PARAM(pComponent);
   1835     OMX_CHECK_PARAM(pBuffHead);
   1836 
   1837     pHandle = (OMX_COMPONENTTYPE *)pComponent;
   1838     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
   1839     pBuffPrivate = pBuffHead->pOutputPortPrivate;
   1840 
   1841     OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGENC_FillThisBuffer function (%p) (state %d -> %d)\n", pBuffHead, pComponentPrivate->nCurState, pComponentPrivate->nToState);
   1842 
   1843     if ( pComponentPrivate->nCurState != OMX_StateExecuting &&
   1844         pComponentPrivate->nCurState != OMX_StatePause &&
   1845         pComponentPrivate->nCurState != OMX_StateIdle) {
   1846         eError= OMX_ErrorIncorrectStateOperation;
   1847         goto EXIT;
   1848     }
   1849 
   1850     if ( pBuffHead->nSize != sizeof(OMX_BUFFERHEADERTYPE) ) {
   1851         eError = OMX_ErrorBadParameter;
   1852         OMX_PRBUFFER4(pComponentPrivate->dbg, "JPEG-ENC: buffer header size is not correct\n");
   1853         goto EXIT;
   1854     }
   1855 
   1856     if (!pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bEnabled) {
   1857             eError = OMX_ErrorIncorrectStateOperation;
   1858             goto EXIT;
   1859     }
   1860 
   1861     if ( (pBuffHead->nVersion.s.nVersionMajor != 0x1) ||
   1862          (pBuffHead->nVersion.s.nVersionMinor != 0x0) ||
   1863          (pBuffHead->nVersion.s.nRevision != 0x0) ||
   1864          (pBuffHead->nVersion.s.nStep != 0x0) ) {
   1865 
   1866         eError= OMX_ErrorVersionMismatch;
   1867         goto EXIT;
   1868     }
   1869 
   1870     if (pBuffHead->nOutputPortIndex != 0x1) {
   1871             eError = OMX_ErrorBadPortIndex;
   1872             goto EXIT;
   1873     }
   1874 
   1875     pBuffHead->nFilledLen = 0;
   1876 #ifdef __PERF_INSTRUMENTATION__
   1877         PERF_ReceivedFrame(pComponentPrivate->pPERF,
   1878                            pBuffHead->pBuffer,
   1879                            0,
   1880                            PERF_ModuleHLMM);
   1881 #endif
   1882 
   1883     pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_IN;
   1884     pBuffPrivate->bReadFromPipe = OMX_FALSE;
   1885 
   1886 
   1887     pComponentPrivate->nOutPortIn ++;
   1888 
   1889     OMX_PRBUFFER2(pComponentPrivate->dbg, "FillThisBuffer nOutPortIn %lu\n", pComponentPrivate->nOutPortIn);
   1890 
   1891     OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (get empty output buffer) %lu %lu %lu %lu\n",
   1892                     pComponentPrivate->nInPortIn,
   1893                     pComponentPrivate->nInPortOut,
   1894                     pComponentPrivate->nOutPortIn,
   1895                     pComponentPrivate->nOutPortOut);
   1896 
   1897     /*  writing the component structure corresponding to output buffer    */
   1898     OMX_PRCOMM2(pComponentPrivate->dbg, "FillThisBuffer: write to the queue %p \n", pBuffHead);
   1899     err = write (pComponentPrivate->free_outBuf_Q[1], &(pBuffHead), sizeof (pBuffHead));
   1900     if (err == -1) {
   1901         eError = OMX_ErrorInsufficientResources;
   1902         goto EXIT;
   1903     }
   1904     OMX_PRINT1(pComponentPrivate->dbg, "Error is %x\n",eError);
   1905  EXIT:
   1906     return eError;
   1907 }
   1908 
   1909 
   1910 /*-------------------------------------------------------------------*/
   1911 /**
   1912   * OMX_ComponentDeinit() Deinitialize Component
   1913   *
   1914   * This method will clean all resources in the component (NOT IMPLEMENTED)
   1915   *
   1916   * @param pHandle        handle for this instance of the component
   1917   *
   1918   * @retval OMX_NoError              Success, ready to roll
   1919   *         OMX_Error_BadParameter   The input parameter pointer is null
   1920   **/
   1921 /*-------------------------------------------------------------------*/
   1922 
   1923 static OMX_ERRORTYPE JPEGENC_ComponentDeInit(OMX_HANDLETYPE hComponent)
   1924 {
   1925 	OMX_ERRORTYPE eError        = OMX_ErrorNone;
   1926 	OMX_COMPONENTTYPE *pHandle  = NULL;
   1927 	JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
   1928 	struct OMX_TI_Debug dbg;
   1929     OMX_DBG_INIT_BASE(dbg);
   1930 	OMX_CHECK_PARAM(hComponent);
   1931 	pHandle = (OMX_COMPONENTTYPE *)hComponent;
   1932 	pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
   1933 	memcpy(&dbg, &(pComponentPrivate->dbg), sizeof(dbg));
   1934         pthread_mutex_destroy(&pComponentPrivate->mutexStateChangeRequest);
   1935         pthread_cond_destroy(&pComponentPrivate->StateChangeCondition);
   1936 	JPEGEnc_Free_ComponentResources(pComponentPrivate);
   1937 
   1938 #ifdef RESOURCE_MANAGER_ENABLED
   1939 	eError = RMProxy_NewSendCommand(pHandle,  RMProxy_FreeResource, OMX_JPEG_Encoder_COMPONENT, 0, 3456, NULL);
   1940 	if (eError != OMX_ErrorNone) {
   1941 		OMX_PRMGR4(dbg, "Cannot Free RMProxy Resources\n");
   1942 	}
   1943 
   1944 	eError = RMProxy_DeinitalizeEx(OMX_COMPONENTTYPE_IMAGE);
   1945 	if ( eError != OMX_ErrorNone )  {
   1946 		OMX_PRMGR4(dbg, "Error returned from destroy ResourceManagerProxy thread\n");
   1947 	}
   1948 
   1949 #endif
   1950 
   1951 EXIT:
   1952 #if 0
   1953 #ifdef __PERF_INSTRUMENTATION__
   1954 	PERF_Boundary(pComponentPrivate->pPERF,
   1955 		      PERF_BoundaryComplete | PERF_BoundaryCleanup);
   1956 	PERF_Done(pComponentPrivate->pPERF);
   1957 #endif
   1958 #endif
   1959 	OMX_DBG_CLOSE(dbg);
   1960 	return eError;
   1961 }
   1962 
   1963 
   1964 
   1965 /*-------------------------------------------------------------------*/
   1966 /**
   1967   *  JPEGENC_VerifyTunnelConnection()
   1968   *
   1969   *
   1970   *
   1971   *
   1972   * @param
   1973   * @param
   1974   * @param
   1975   *
   1976   * @retval OMX_NoError              Success, ready to roll
   1977   *         OMX_Error_BadParameter   The input parameter pointer is null
   1978   **/
   1979 /*-------------------------------------------------------------------*/
   1980 /* TODO: Component Name */
   1981 
   1982 OMX_ERRORTYPE JPEGENC_VerifyTunnelConnection (JPEG_PORT_TYPE *pPort,
   1983                                               OMX_HANDLETYPE hTunneledComp,
   1984                                               OMX_PARAM_PORTDEFINITIONTYPE* pPortDef,
   1985                                               struct OMX_TI_Debug *dbg)
   1986 {
   1987    OMX_PARAM_PORTDEFINITIONTYPE sPortDef;
   1988    OMX_ERRORTYPE eError = OMX_ErrorNone;
   1989    /*sPortDef.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;*/
   1990 
   1991    OMX_PRINT1(*dbg, "Inside JPEG JPEGENC_VerifyTunnelConnection..\n");
   1992    sPortDef.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
   1993    sPortDef.nVersion.s.nVersionMajor = 0x1;
   1994    sPortDef.nVersion.s.nVersionMinor = 0x0;
   1995    sPortDef.nPortIndex = pPort->nTunnelPort;
   1996 
   1997    eError = OMX_GetParameter(hTunneledComp, OMX_IndexParamPortDefinition, &sPortDef);
   1998    if (eError != OMX_ErrorNone) {
   1999        OMX_ERROR4(*dbg, "error 1\n");
   2000        return eError;
   2001    }
   2002 
   2003    switch(pPort->pPortDef->eDomain) {
   2004        case OMX_PortDomainOther:
   2005            if (sPortDef.format.other.eFormat!= pPortDef->format.other.eFormat) {
   2006                 pPort->hTunnelComponent = 0;
   2007                 pPort->nTunnelPort      = 0;
   2008                 return OMX_ErrorPortsNotCompatible;
   2009             }
   2010             break;
   2011         case OMX_PortDomainAudio:
   2012             if (sPortDef.format.audio.eEncoding != pPortDef->format.audio.eEncoding) {
   2013                 pPort->hTunnelComponent = 0;
   2014                 pPort->nTunnelPort      = 0;
   2015                 return OMX_ErrorPortsNotCompatible;
   2016             }
   2017             break;
   2018         case OMX_PortDomainVideo:
   2019             if (sPortDef.format.video.eCompressionFormat != pPortDef->format.video.eCompressionFormat) {
   2020                 pPort->hTunnelComponent = 0;
   2021                 pPort->nTunnelPort      = 0;
   2022                 return OMX_ErrorPortsNotCompatible;
   2023             }
   2024             break;
   2025         case OMX_PortDomainImage:
   2026             if (sPortDef.format.image.eCompressionFormat != pPortDef->format.image.eCompressionFormat) {
   2027                 pPort->hTunnelComponent = 0;
   2028                 pPort->nTunnelPort      = 0;
   2029                 return OMX_ErrorPortsNotCompatible;
   2030             }
   2031             break;
   2032         default:
   2033             pPort->hTunnelComponent = 0;
   2034             pPort->nTunnelPort      = 0;
   2035             return OMX_ErrorPortsNotCompatible; /* Our current port is not set up correctly */
   2036    }
   2037     return eError;
   2038 }
   2039 
   2040 /*-------------------------------------------------------------------*/
   2041 /**
   2042   *  JPEGENC_ComponentTunnelRequest() Sets application callbacks to the component
   2043   *
   2044   * This method will update application callbacks
   2045   * the application.
   2046   *
   2047   * @param hComp         handle for this instance of the component
   2048   * @param nPortInput
   2049   * @param hTunneledComp
   2050   * @param nTunneledPort
   2051   * @param eDir
   2052   * @param pCallbacks
   2053   *
   2054   * @retval OMX_NoError              Success, ready to roll
   2055   *         OMX_Error_BadParameter   The input parameter pointer is null
   2056   *
   2057 static OMX_ERRORTYPE JPEGENC_ComponentTunnelRequest (OMX_HANDLETYPE hComp,
   2058                                              OMX_U32 nPortInput,
   2059                                              OMX_HANDLETYPE hTunneledComp,
   2060                                              OMX_U32 nTunneledPort,
   2061                                              OMX_DIRTYPE eDir,
   2062                                              OMX_CALLBACKTYPE* pCallbacks)
   2063 
   2064 -------------------------------------------------------------------*/
   2065 
   2066 OMX_ERRORTYPE JPEGENC_ComponentTunnelRequest(OMX_IN  OMX_HANDLETYPE hComponent,
   2067                                      OMX_IN  OMX_U32 nPort,
   2068                                      OMX_IN  OMX_HANDLETYPE hTunneledComp,
   2069                                      OMX_IN  OMX_U32 nTunneledPort,
   2070                                      OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup)
   2071 {
   2072     OMX_ERRORTYPE eError            = OMX_ErrorNone;
   2073     OMX_COMPONENTTYPE *pHandle  = NULL;
   2074     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
   2075     OMX_PARAM_BUFFERSUPPLIERTYPE sBufferSupplier;
   2076     JPEG_PORT_TYPE *pPortType           = NULL;
   2077 
   2078     OMX_CHECK_PARAM(hComponent);
   2079 
   2080 
   2081     pHandle = (OMX_COMPONENTTYPE *)hComponent;
   2082     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
   2083     pPortType = pComponentPrivate->pCompPort[nPort];
   2084 
   2085     OMX_PRBUFFER2(pComponentPrivate->dbg, "nPort = %d nTunneledPort = %d\n",(int)nPort, (int)nTunneledPort);
   2086 
   2087     if (pTunnelSetup == NULL || hTunneledComp == 0)
   2088     {
   2089         /* cancel previous tunnel */
   2090         pPortType->hTunnelComponent = 0;
   2091         pPortType->nTunnelPort = 0;
   2092         pPortType->pBufSupplier = OMX_BufferSupplyUnspecified;
   2093         goto EXIT;
   2094     }
   2095 
   2096     if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->eDir != OMX_DirInput &&
   2097         pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->eDir != OMX_DirOutput) {
   2098         return OMX_ErrorBadParameter;
   2099     }
   2100 
   2101      /* Check if the other component is developed by TI */
   2102      if(IsTIOMXComponent(hTunneledComp) != OMX_TRUE)
   2103     {
   2104        eError = OMX_ErrorTunnelingUnsupported;
   2105        goto EXIT;
   2106     }
   2107 
   2108     pPortType->hTunnelComponent = hTunneledComp;
   2109     pPortType->nTunnelPort = nTunneledPort;
   2110     OMX_PRCOMM2(pComponentPrivate->dbg, "PP comp = %x, tunneled comp = %x\n",(int)hComponent, (int)pPortType->hTunnelComponent);
   2111 
   2112     if (pPortType->pPortDef->eDir == OMX_DirOutput) {
   2113         /* Component is the output (source of data) */
   2114         pTunnelSetup->eSupplier = pPortType->pBufSupplier;
   2115     }
   2116     else {
   2117 /*      Component is the input (sink of data) */
   2118         eError = JPEGENC_VerifyTunnelConnection(pPortType,
   2119                                                hTunneledComp,
   2120 						pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef, &(pComponentPrivate->dbg));
   2121         if (OMX_ErrorNone != eError) {
   2122             OMX_PRCOMM4(pComponentPrivate->dbg, "PP JPEGENC_VerifyTunnelConnection inside JPEGfailed\n");
   2123             /* Invalid connection formats. Return eError */
   2124             return OMX_ErrorPortsNotCompatible;
   2125         }
   2126 
   2127         /* If specified obey output port's preferences. Otherwise choose output */
   2128         pPortType->pBufSupplier = pTunnelSetup->eSupplier;
   2129         if (OMX_BufferSupplyUnspecified == pPortType->pBufSupplier) {
   2130             pPortType->pBufSupplier = pTunnelSetup->eSupplier = OMX_BufferSupplyOutput;
   2131         }
   2132 
   2133         /* Tell the output port who the supplier is */
   2134         sBufferSupplier.nSize = sizeof(sBufferSupplier);
   2135         sBufferSupplier.nVersion.s.nVersionMajor = 0x1;
   2136         sBufferSupplier.nVersion.s.nVersionMinor = 0x0;
   2137         sBufferSupplier.nPortIndex = nTunneledPort;
   2138         sBufferSupplier.eBufferSupplier = pPortType->pBufSupplier;
   2139         eError = OMX_SetParameter(hTunneledComp, OMX_IndexParamCompBufferSupplier, &sBufferSupplier);
   2140 
   2141         eError = OMX_GetParameter(hTunneledComp, OMX_IndexParamCompBufferSupplier, &sBufferSupplier);
   2142         if (sBufferSupplier.eBufferSupplier != pPortType->pBufSupplier) {
   2143             OMX_PRCOMM4(pComponentPrivate->dbg, "- JPEGENC_SetParameter: OMX_IndexParamCompBufferSupplier failed to change setting\n" );
   2144             return OMX_ErrorUndefined;
   2145         }
   2146     }
   2147 
   2148 EXIT:
   2149     return eError;
   2150 }
   2151 
   2152 
   2153 
   2154 /*-------------------------------------------------------------------*/
   2155 /**
   2156   *  JPEGENC_AllocateBuffer()
   2157   *
   2158   *
   2159   *
   2160   *
   2161   * @param
   2162   * @param
   2163   * @param
   2164   *
   2165   * @retval OMX_NoError              Success, ready to roll
   2166   *         OMX_Error_BadParameter   The input parameter pointer is null
   2167   **/
   2168 /*-------------------------------------------------------------------*/
   2169 OMX_ERRORTYPE JPEGENC_AllocateBuffer(OMX_IN OMX_HANDLETYPE hComponent,
   2170                              OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffHead,
   2171                              OMX_IN OMX_U32 nPortIndex,
   2172                              OMX_IN OMX_PTR pAppPrivate,
   2173                              OMX_IN OMX_U32 nSizeBytes)
   2174 {
   2175     OMX_COMPONENTTYPE            *pHandle           = NULL;
   2176     JPEGENC_COMPONENT_PRIVATE    *pComponentPrivate = NULL;
   2177     OMX_PARAM_PORTDEFINITIONTYPE *pPortDef          = NULL;
   2178     OMX_BUFFERHEADERTYPE         *pBufferHdr        = NULL;
   2179     OMX_U32 nBufferCount = -1;
   2180     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2181     OMX_U8* pTemp = NULL;
   2182 
   2183     pHandle           = (OMX_COMPONENTTYPE *) hComponent;
   2184     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
   2185     pPortDef          = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
   2186     nBufferCount      = pComponentPrivate->pCompPort[nPortIndex]->nBuffCount;
   2187 
   2188     if (nBufferCount >= pPortDef->nBufferCountActual) {
   2189         eError = OMX_ErrorInsufficientResources;
   2190         OMX_PRBUFFER4(pComponentPrivate->dbg, " try to allocate more buffers that the port supports\n");
   2191         goto EXIT;
   2192     }
   2193 
   2194     if(nPortIndex == JPEGENC_INP_PORT) {
   2195         OMX_MALLOC(pBufferHdr, sizeof(OMX_BUFFERHEADERTYPE));
   2196         OMX_PRBUFFER2(pComponentPrivate->dbg, "Allocate Buffer Input pBufferPrivate = %p\n",pBufferHdr);
   2197 
   2198         OMX_CONF_INIT_STRUCT(pBufferHdr, OMX_BUFFERHEADERTYPE);
   2199         pBufferHdr->nOutputPortIndex = OMX_NOPORT;
   2200         pBufferHdr->nInputPortIndex = nPortIndex;
   2201         pBufferHdr->nSize = sizeof(OMX_BUFFERHEADERTYPE);
   2202         pBufferHdr->nVersion = pPortDef->nVersion;
   2203         pBufferHdr->pAppPrivate = pAppPrivate;
   2204         pBufferHdr->nAllocLen = nSizeBytes;
   2205         pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[nBufferCount]->bAllocByComponent = OMX_TRUE;
   2206         pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[nBufferCount]->pBufferHdr = pBufferHdr;
   2207         *ppBuffHead = pBufferHdr;
   2208     }
   2209     else if(nPortIndex == JPEGENC_OUT_PORT) {
   2210         OMX_MALLOC(pBufferHdr, sizeof(OMX_BUFFERHEADERTYPE));
   2211         OMX_PRBUFFER2(pComponentPrivate->dbg, "Allocate Buffer Output pBufferPrivate[0] = %p\n", pBufferHdr);
   2212         OMX_CONF_INIT_STRUCT(pBufferHdr, OMX_BUFFERHEADERTYPE);
   2213         pBufferHdr->nInputPortIndex = OMX_NOPORT;
   2214         pBufferHdr->nOutputPortIndex = nPortIndex;
   2215         pBufferHdr->nSize = sizeof(OMX_BUFFERHEADERTYPE);
   2216         pBufferHdr->nVersion = pPortDef->nVersion;
   2217         pBufferHdr->pAppPrivate = pAppPrivate;
   2218         pBufferHdr->nAllocLen = nSizeBytes;
   2219         pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[nBufferCount]->bAllocByComponent = OMX_TRUE;
   2220         pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[nBufferCount]->pBufferHdr = pBufferHdr;
   2221         *ppBuffHead = pBufferHdr;
   2222     }
   2223     else {
   2224         eError = OMX_ErrorBadPortIndex;
   2225         goto EXIT;
   2226     }
   2227 
   2228     OMX_MALLOC(pBufferHdr->pBuffer, nSizeBytes+256);
   2229 
   2230 #ifdef __PERF_INSTRUMENTATION__
   2231         PERF_ReceivedFrame(pComponentPrivate->pPERF,
   2232                            pBufferHdr->pBuffer,
   2233                            pBufferHdr->nFilledLen,
   2234                            PERF_ModuleMemory);
   2235 #endif
   2236 
   2237     if (!pBufferHdr->pBuffer) {
   2238         OMX_TRACE4(pComponentPrivate->dbg, "Error: Malloc failed\n");
   2239         eError = OMX_ErrorInsufficientResources;
   2240         goto EXIT;
   2241     }
   2242 
   2243 
   2244     pTemp = (OMX_U8 *)(pBufferHdr->pBuffer);
   2245     pTemp += 128;
   2246     pBufferHdr->pBuffer = pTemp;
   2247     OMX_PRBUFFER2(pComponentPrivate->dbg, "Allocate Buffer Input pBufferPrivate[0]-pBuffer = %p\n",pBufferHdr->pBuffer);
   2248 
   2249 
   2250     if (nPortIndex == JPEGENC_INP_PORT) {
   2251         pBufferHdr->pInputPortPrivate  = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount];
   2252     }
   2253     else {
   2254         pBufferHdr->pOutputPortPrivate = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount];
   2255     }
   2256 
   2257     /** The following 6 lines need to be changed **/
   2258     if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent != NULL) {
   2259         pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->eBufferOwner = JPEGENC_BUFFER_TUNNEL_COMPONENT;
   2260     }
   2261     else {
   2262         pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   2263     }
   2264 
   2265 
   2266    pComponentPrivate->pCompPort[nPortIndex]->nBuffCount++;
   2267    OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG-ENC: actual %d ask %lu\n",
   2268         pComponentPrivate->pCompPort[nPortIndex]->nBuffCount,
   2269         pPortDef->nBufferCountActual);
   2270 
   2271    if (pComponentPrivate->pCompPort[nPortIndex]->nBuffCount == pPortDef->nBufferCountActual) {
   2272        pPortDef->bPopulated = OMX_TRUE;
   2273 
   2274        pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
   2275        pthread_cond_signal(&pComponentPrivate->populate_cond);
   2276        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
   2277 
   2278        JPEGENC_InitBufferFlagTrack(pComponentPrivate, nPortIndex);
   2279        OMX_PRBUFFER2(pComponentPrivate->dbg, " Port [%d] Populated!\n", (int)(nPortIndex));
   2280    }
   2281 
   2282 EXIT:
   2283     OMX_PRINT1(pComponentPrivate->dbg, "Exiting pHandle = %d\n", (int)pHandle);
   2284     return eError;
   2285 }
   2286 
   2287 
   2288 static void JPEGENC_InitBufferFlagTrack(
   2289     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate,
   2290     OMX_U32 nPortIndex)
   2291 
   2292 {
   2293     JPEG_PORT_TYPE *pPortType = NULL;
   2294     int i;
   2295 
   2296     pPortType = pComponentPrivate->pCompPort[nPortIndex];
   2297 
   2298     /* assume  pPortType->pPortDef->nBufferCountActual <= NUM_OF_BUFFERSJPEG */
   2299     for (i = 0; i < (int)(pPortType->pPortDef->nBufferCountActual); i ++) {
   2300         pPortType->sBufferFlagTrack[i].flag = 0;
   2301         pPortType->sBufferFlagTrack[i].buffer_id = 0xFFFFFFFF;
   2302         pPortType->sBufferMarkTrack[i].buffer_id = 0xFFFFFFFF;
   2303         pPortType->sBufferMarkTrack[i].pMarkData = NULL;
   2304     }
   2305 }
   2306 
   2307 #ifdef KHRONOS_1_1
   2308 
   2309 static OMX_ERRORTYPE ComponentRoleEnum(
   2310         OMX_IN OMX_HANDLETYPE hComponent,
   2311                 OMX_OUT OMX_U8 *cRole,
   2312                 OMX_IN OMX_U32 nIndex)
   2313 {
   2314     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate;
   2315 
   2316     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2317     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
   2318 
   2319     if(nIndex == 0){
   2320       /* memcpy(cRole, &(pComponentPrivate->componentRole.cRole), sizeof(OMX_U8) * OMX_MAX_STRINGNAME_SIZE - 1); */
   2321       strncpy((char *)cRole, (char *)pComponentPrivate->componentRole.cRole, sizeof(OMX_U8) * OMX_MAX_STRINGNAME_SIZE - 1);
   2322     }
   2323     else {
   2324       eError = OMX_ErrorNoMore;
   2325         }
   2326 
   2327     return eError;
   2328 };
   2329 #endif
   2330 
   2331 
   2332 /*-------------------------------------------------------------------*/
   2333 /**
   2334   * JPEGENC_GetExtensionIndex()
   2335   *
   2336   * Return the index corresponding to the string.
   2337   *
   2338   * @retval OMX_ErrorNone                    Successful operation.
   2339   *             OMX_ErrorUnsupportedIndex  If no index corresponding to the string is found
   2340   **/
   2341 /*-------------------------------------------------------------------*/
   2342 OMX_ERRORTYPE JPEGENC_GetExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent, OMX_IN OMX_STRING cParameterName, OMX_OUT OMX_INDEXTYPE* pIndexType)
   2343 {
   2344     OMX_U16 nIndex;
   2345     OMX_ERRORTYPE eError = OMX_ErrorUndefined;
   2346     JPEGENC_CUSTOM_PARAM_DEFINITION sJpegEncCustomParams[] = {
   2347     {"OMX.TI.JPEG.encoder.Config.HuffmanTable", OMX_IndexCustomHuffmanTable},
   2348     {"OMX.TI.JPEG.encoder.Config.CommentFlag", OMX_IndexCustomCommentFlag},
   2349     {"OMX.TI.JPEG.encoder.Config.CommentString", OMX_IndexCustomCommentString},
   2350     {"OMX.TI.JPEG.encoder.Config.InputFrameWidth", OMX_IndexCustomInputFrameWidth},
   2351     {"OMX.TI.JPEG.encoder.Config.InputFrameHeight", OMX_IndexCustomInputFrameHeight},
   2352     {"OMX.TI.JPEG.encoder.Config.APP0", OMX_IndexCustomAPP0},
   2353     {"OMX.TI.JPEG.encoder.Config.APP1", OMX_IndexCustomAPP1},
   2354     {"OMX.TI.JPEG.encoder.Config.APP5", OMX_IndexCustomAPP5},
   2355     {"OMX.TI.JPEG.encoder.Config.APP13", OMX_IndexCustomAPP13},
   2356     {"OMX.TI.JPEG.encoder.Config.QFactor", OMX_IndexCustomQFactor},
   2357     {"OMX.TI.JPEG.encoder.Config.DRI", OMX_IndexCustomDRI},
   2358     {"OMX.TI.JPEG.encoder.Config.Debug", OMX_IndexCustomDebug},
   2359     {"OMX.TI.JPEG.encoder.Config.ColorFormatConvertion_420pTo422i", OMX_IndexCustomColorFormatConvertion_420pTo422i},
   2360     {"OMX.TI.JPEG.encoder.Config.PPLibEnable", OMX_IndexCustomPPLibEnable},
   2361     {"",0x0}
   2362     };
   2363 
   2364     /* Check parameter validity */
   2365     OMX_CHECK_PARAM(hComponent);
   2366     OMX_CHECK_PARAM(pIndexType);
   2367     *pIndexType = OMX_IndexMax;
   2368 
   2369     const OMX_U32 nExtensions = sizeof(sJpegEncCustomParams)/sizeof(JPEGENC_CUSTOM_PARAM_DEFINITION);
   2370     for (nIndex = 0; nIndex < nExtensions; ++nIndex) {
   2371         if (!strcmp((const char*)cParameterName, (const char*)(&(sJpegEncCustomParams[nIndex].cCustomParamName)))){
   2372             *pIndexType = sJpegEncCustomParams[nIndex].nCustomParamIndex;
   2373             eError = OMX_ErrorNone;
   2374             break;
   2375         }
   2376     }
   2377 
   2378     if(*pIndexType == OMX_IndexMax){
   2379          eError = OMX_ErrorUnsupportedIndex;
   2380     }
   2381 
   2382 EXIT:
   2383     return eError;
   2384 }
   2385 
   2386 
   2387 
   2388