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_JpegEnc_Utils.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 
     54 #ifdef UNDER_CE
     55 #include <windows.h>
     56 #include <oaf_osal.h>
     57 #include <omx_core.h>
     58 #else
     59 #include <unistd.h>
     60 #include <sys/types.h>
     61 #include <malloc.h>
     62 #include <memory.h>
     63 #include <sys/time.h>
     64 #include <sys/types.h>
     65 #include <sys/stat.h>
     66 #endif
     67 
     68 #include <dbapi.h>
     69 #include <string.h>
     70 #include <stdio.h>
     71 #include <stdlib.h>
     72 #include <fcntl.h>
     73 
     74 
     75 #include <OMX_Types.h>
     76 #include <OMX_Component.h>
     77 #include <OMX_Core.h>
     78 #include <OMX_Index.h>
     79 #include <OMX_Image.h>
     80 #include <OMX_Audio.h>
     81 #include <OMX_Video.h>
     82 #include <OMX_IVCommon.h>
     83 #include <OMX_Other.h>
     84 
     85 #include <usn.h>
     86 
     87 /*------- Program Header Files ----------------------------------------*/
     88 
     89 #include "OMX_JpegEnc_Utils.h"
     90 #include "OMX_JpegEnc_CustomCmd.h"
     91 
     92 #ifdef RESOURCE_MANAGER_ENABLED
     93     #include <ResourceManagerProxyAPI.h>
     94 #endif
     95 
     96 #define JPEGENC_TIMEOUT 0xFFFFFFFE
     97 
     98 static OMX_ERRORTYPE HandleJpegEncInternalFlush(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nParam1);
     99 
    100 #ifdef RESOURCE_MANAGER_ENABLED
    101 void ResourceManagerCallback(RMPROXY_COMMANDDATATYPE cbData);
    102 #endif
    103 
    104 #ifdef UNDER_CE
    105 void sleep(DWORD Duration)
    106 {
    107     Sleep(Duration);
    108 }
    109 #endif
    110 
    111 /*--------function prototypes ---------------------------------*/
    112 OMX_ERRORTYPE JpegEncLCML_Callback (TUsnCodecEvent event,
    113                                     void * args [10]);
    114 
    115 /*-------- Function Implementations ---------------------------------*/
    116 OMX_ERRORTYPE GetJpegEncLCMLHandle(OMX_HANDLETYPE pComponent)
    117 {
    118     OMX_ERRORTYPE eError = OMX_ErrorNone;
    119 #ifndef UNDER_CE
    120     OMX_HANDLETYPE LCML_pHandle;
    121 
    122     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
    123     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    124     fpo fpGetHandle ;
    125     void *handle = NULL;
    126     char *error =NULL;
    127 
    128     OMX_PRDSP1(pComponentPrivate->dbg, "Inside GetLCMLHandle function\n");
    129 
    130     handle = dlopen("libLCML.so", RTLD_LAZY);
    131     if ( !handle ) {
    132         if ( (error = (char *)dlerror()) != NULL ) {
    133             fputs(error, stderr);
    134         }
    135         eError = OMX_ErrorComponentNotFound;
    136         goto EXIT;
    137     }
    138 
    139     fpGetHandle = dlsym (handle, "GetHandle");
    140     if ( (error = (char *)dlerror()) != NULL ) {
    141         fputs(error, stderr);
    142         eError = OMX_ErrorInvalidComponent;
    143         goto EXIT;
    144     }
    145 
    146     /*  calling gethandle and passing phandle to b filled   */
    147     if ( fpGetHandle != NULL ) {
    148         eError = (*fpGetHandle)(&LCML_pHandle);
    149     }
    150     else  {
    151         eError = OMX_ErrorInvalidComponent;
    152         goto EXIT;
    153     }
    154 
    155     if ( eError != OMX_ErrorNone ) {
    156         eError = OMX_ErrorUndefined;
    157         OMX_PRDSP4(pComponentPrivate->dbg, "eError != OMX_ErrorNone...\n");
    158         goto EXIT;
    159     }
    160 
    161     OMX_PRDSP2(pComponentPrivate->dbg, "Received LCML Handle\n");
    162 
    163     pComponentPrivate->pDllHandle = handle;
    164     pComponentPrivate->pLCML = (void *)LCML_pHandle;
    165     pComponentPrivate->pLCML->pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pComponentPrivate;
    166 #else
    167     typedef OMX_ERRORTYPE (*LPFNDLLFUNC1)(OMX_HANDLETYPE);
    168     LPFNDLLFUNC1 fpGetHandle1;
    169     OMX_HANDLETYPE LCML_pHandle = NULL;
    170     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
    171     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    172     HINSTANCE hDLL;
    173 
    174     hDLL = LoadLibraryEx(TEXT("OAF_BML.dll"), NULL, 0);
    175     if (hDLL == NULL)
    176     {
    177         eError = OMX_ErrorComponentNotFound;
    178         goto EXIT;
    179     }
    180 
    181     fpGetHandle1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,TEXT("GetHandle"));
    182     if (!fpGetHandle1)
    183     {
    184 
    185         FreeLibrary(hDLL);
    186         eError = OMX_ErrorComponentNotFound;
    187         goto EXIT;
    188     }
    189 
    190 
    191     eError = fpGetHandle1(&LCML_pHandle);
    192     if(eError != OMX_ErrorNone) {
    193 
    194         eError = OMX_ErrorUndefined;
    195         LCML_pHandle = NULL;
    196           goto EXIT;
    197     }
    198 
    199     (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML = (LCML_DSP_INTERFACE *)LCML_pHandle;
    200     pComponentPrivate->pLCML->pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pComponentPrivate;
    201 
    202 #endif
    203     OMX_PRINT1(pComponentPrivate->dbg, "Exit\n");
    204     EXIT:
    205     return eError;
    206 }
    207 
    208 
    209 /*-----------------------------------------------------------------------------*/
    210 /**
    211   * Disable Port()
    212   *
    213   * Called by component thread, handles commands sent by the app.
    214   *
    215   * @param
    216   *
    217   * @retval OMX_ErrorNone                  success, ready to roll
    218   *
    219   **/
    220 /*-----------------------------------------------------------------------------*/
    221 OMX_ERRORTYPE JpegEncDisablePort (JPEGENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_U32 nParam1)
    222 {
    223     OMX_ERRORTYPE eError = OMX_ErrorNone;
    224     OMX_CHECK_PARAM(pComponentPrivate);
    225 
    226     OMX_PRINT1(pComponentPrivate->dbg, "Inside DisablePort function\n");
    227 
    228     OMX_PRBUFFER1(pComponentPrivate->dbg, "Inside disable port (%lu) %lu %lu %lu %lu\n",
    229             nParam1,
    230             pComponentPrivate->nInPortIn,
    231             pComponentPrivate->nInPortOut,
    232             pComponentPrivate->nOutPortIn,
    233             pComponentPrivate->nOutPortOut);
    234 
    235    if (pComponentPrivate->nCurState == OMX_StateExecuting || pComponentPrivate->nCurState == OMX_StatePause) {
    236 	if ((nParam1 == JPEGENC_INP_PORT) || (nParam1 == JPEGENC_OUT_PORT) || ((int)nParam1 == -1)) {
    237            eError = HandleJpegEncInternalFlush(pComponentPrivate, nParam1);
    238        }
    239    }
    240 
    241     OMX_PRINT1(pComponentPrivate->dbg, "Exit form JPEGEnc Disable Port eError is = %x\n",eError);
    242 EXIT:
    243     return eError;
    244 }
    245 
    246 
    247 
    248 /*-----------------------------------------------------------------------------*/
    249 /**
    250   * Enable Port()
    251   *
    252   * Called by component thread, handles commands sent by the app.
    253   *
    254   * @param
    255   *
    256   * @retval OMX_ErrorNone                  success, ready to roll
    257   *
    258   **/
    259 /*-----------------------------------------------------------------------------*/
    260 
    261 OMX_ERRORTYPE JpegEncEnablePort (JPEGENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_U32 nParam1)
    262 {
    263     OMX_ERRORTYPE eError = OMX_ErrorNone;
    264     OMX_CHECK_PARAM(pComponentPrivate);
    265 
    266     OMX_PRINT1(pComponentPrivate->dbg, "Inside EnablePort function\n");
    267 
    268 
    269     if (nParam1 == 0) {
    270         if (pComponentPrivate->nCurState != OMX_StateLoaded) {
    271         pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
    272         while (!pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bPopulated) {
    273             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
    274             pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->jpege_mutex_app);
    275         }
    276         }
    277         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
    278         pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
    279                                                 pComponentPrivate->pHandle->pApplicationPrivate,
    280                                                 OMX_EventCmdComplete,
    281                                                 OMX_CommandPortEnable,
    282                                                 JPEGENC_INP_PORT,
    283                                                 NULL);
    284     } else if (nParam1 == 1) {
    285         if (pComponentPrivate->nCurState != OMX_StateLoaded) {
    286         pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
    287         while (!pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bPopulated) {
    288             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
    289             pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->jpege_mutex_app);
    290         }
    291         }
    292         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
    293         pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
    294                                                 pComponentPrivate->pHandle->pApplicationPrivate,
    295                                                 OMX_EventCmdComplete,
    296                                                 OMX_CommandPortEnable,
    297                                                 JPEGENC_OUT_PORT,
    298                                                 NULL);
    299     } else if ((int)nParam1 == -1) {
    300         if (pComponentPrivate->nCurState != OMX_StateLoaded) {
    301         pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
    302         while ((!pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bPopulated) ||
    303                (!pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bPopulated)) {
    304             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
    305             pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->jpege_mutex_app);
    306         }
    307         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
    308         }
    309         pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
    310                                                 pComponentPrivate->pHandle->pApplicationPrivate,
    311                                                 OMX_EventCmdComplete,
    312                                                 OMX_CommandPortEnable,
    313                                                 JPEGENC_INP_PORT,
    314                                                 NULL);
    315 
    316         pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
    317                                                 pComponentPrivate->pHandle->pApplicationPrivate,
    318                                                 OMX_EventCmdComplete,
    319                                                 OMX_CommandPortEnable,
    320                                                 JPEGENC_OUT_PORT,
    321                                                 NULL);
    322     }
    323 EXIT:
    324     return eError;
    325 }
    326 
    327 
    328 
    329 /*-------------------------------------------------------------------*/
    330 /**
    331   *  JPEGEnc_Start_ComponentThread() Starts Component Thread
    332   *
    333   *  Creates data pipes, commmand pipes and initializes Component thread
    334   *
    335   * @param pComponent    handle for this instance of the component
    336   *
    337   * @retval OMX_NoError              Success, ready to roll
    338   * @retval OMX_ErrorInsufficientResources    Insiffucient Resources
    339   *
    340   **/
    341 /*-------------------------------------------------------------------*/
    342 OMX_ERRORTYPE JPEGEnc_Start_ComponentThread(OMX_HANDLETYPE pComponent)
    343 {
    344     OMX_ERRORTYPE eError = OMX_ErrorNone;
    345     OMX_COMPONENTTYPE *pHandle = NULL;
    346     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
    347 
    348 
    349     OMX_CHECK_PARAM(pComponent);
    350     pHandle = (OMX_COMPONENTTYPE *)pComponent;
    351     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    352     OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGEnc_Start_ComponentThread function\n");
    353 
    354     /* create the pipe used to maintain free output buffers*/
    355     eError = pipe (pComponentPrivate->free_outBuf_Q);
    356     if ( eError ) {
    357         eError = OMX_ErrorInsufficientResources;
    358         goto EXIT;
    359     }
    360 
    361     /* create the pipe used to maintain filled input buffers*/
    362     eError = pipe (pComponentPrivate->filled_inpBuf_Q);
    363     if ( eError ) {
    364         eError = OMX_ErrorInsufficientResources;
    365         goto EXIT;
    366     }
    367 
    368     /* create the pipe used to send commands to the thread */
    369     eError = pipe (pComponentPrivate->nCmdPipe);
    370     if ( eError ) {
    371         eError = OMX_ErrorInsufficientResources;
    372         goto EXIT;
    373     }
    374 
    375     /* create the pipe used to send commands to the thread */
    376     eError = pipe (pComponentPrivate->nCmdDataPipe);
    377     if ( eError ) {
    378         eError = OMX_ErrorInsufficientResources;
    379         goto EXIT;
    380     }
    381 
    382     /*  No buffers have been sent to dsp    */
    383     pComponentPrivate->nNum_dspBuf = 0;
    384 
    385     /* Create the Component Thread */
    386     eError = pthread_create (&(pComponentPrivate->ComponentThread), NULL,
    387                              OMX_JpegEnc_Thread, pComponent);
    388 
    389 
    390     if ( eError || !pComponentPrivate->ComponentThread ) {
    391         eError = OMX_ErrorInsufficientResources;
    392         goto EXIT;
    393     }
    394 
    395 #ifdef __PERF_INSTRUMENTATION__
    396         PERF_ThreadCreated(pComponentPrivate->pPERF,
    397                            pComponentPrivate->ComponentThread,
    398                            PERF_FOURCC('J','P','E','T'));
    399 
    400 #endif
    401 
    402     EXIT:
    403     return eError;
    404 }
    405 
    406 
    407 
    408 /* -------------------------------------------------------------------------- */
    409 /**
    410 * @JPEGEnc_Free_ComponentResources() This function is called by the component during
    411 * de-init to close component thread, Command pipe, data pipe & LCML pipe.
    412 *
    413 * @param pComponent  handle for this instance of the component
    414 *
    415 * @pre
    416 *
    417 * @post
    418 *
    419 * @return none
    420 */
    421 /* -------------------------------------------------------------------------- */
    422 OMX_ERRORTYPE JPEGEnc_Free_ComponentResources(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate)
    423 {
    424     OMX_ERRORTYPE eError = OMX_ErrorNone;
    425     OMX_ERRORTYPE threadError = OMX_ErrorNone;
    426     OMX_ERRORTYPE err = OMX_ErrorNone;
    427     int pipeError = 0;
    428     int pthreadError = 0;
    429     OMX_COMMANDTYPE eCmd = OMX_CustomCommandStopThread;
    430     OMX_U32 nParam = 0;
    431     struct OMX_TI_Debug dbg;
    432 
    433     OMX_DBG_INIT_BASE(dbg);
    434     OMX_CHECK_PARAM(pComponentPrivate);
    435 
    436 #ifdef __PERF_INSTRUMENTATION__
    437         PERF_Boundary(pComponentPrivate->pPERF,
    438                       PERF_BoundaryStart | PERF_BoundaryCleanup);
    439 #endif
    440 
    441     if ( pComponentPrivate->pLCML != NULL && pComponentPrivate->isLCMLActive) {
    442     	LCML_ControlCodec(((LCML_DSP_INTERFACE*)pComponentPrivate->pLCML)->pCodecinterfacehandle,EMMCodecControlDestroy,NULL);
    443     	dlclose(pComponentPrivate->pDllHandle);
    444     	pComponentPrivate->pLCML = NULL;
    445     	pComponentPrivate->isLCMLActive = 0;
    446     }
    447 
    448     pipeError = write(pComponentPrivate->nCmdPipe[1], &eCmd, sizeof(eCmd));
    449     if (pipeError == -1) {
    450         eError = OMX_ErrorHardware;
    451         OMX_PRCOMM4(dbg, "Error while writing to nCmdPipe\n");
    452     }
    453 
    454     pipeError = write(pComponentPrivate->nCmdDataPipe[1], &nParam, sizeof(nParam));
    455     if (pipeError == -1) {
    456         eError = OMX_ErrorHardware;
    457         OMX_PRCOMM4(dbg, "Error while writing to nCmdPipe\n");
    458     }
    459 
    460     pthreadError = pthread_join (pComponentPrivate->ComponentThread,
    461                                  (void*)&threadError);
    462     if ( 0 != pthreadError ) {
    463         eError = OMX_ErrorHardware;
    464         OMX_TRACE4(dbg, "Error while closing Component Thread\n");
    465     }
    466 
    467     if ( OMX_ErrorNone != threadError && OMX_ErrorNone != eError ) {
    468         eError = OMX_ErrorInsufficientResources;
    469         OMX_TRACE4(dbg, "Error while closing Component Thread\n");
    470     }
    471 
    472     /*  close the data pipe handles */
    473 
    474     err = close (pComponentPrivate->free_outBuf_Q[0]);
    475     if ( 0 != err && OMX_ErrorNone == eError ) {
    476         eError = OMX_ErrorHardware;
    477         OMX_PRCOMM4(dbg, "Error while closing data pipe\n");
    478     }
    479 
    480     err = close (pComponentPrivate->filled_inpBuf_Q[0]);
    481     if ( 0 != err && OMX_ErrorNone == eError ) {
    482         eError = OMX_ErrorHardware;
    483         OMX_PRCOMM4(dbg, "Error while closing data pipe\n");
    484     }
    485 
    486     err = close (pComponentPrivate->free_outBuf_Q[1]);
    487     if ( 0 != err && OMX_ErrorNone == eError ) {
    488         eError = OMX_ErrorHardware;
    489         OMX_PRCOMM4(dbg, "Error while closing data pipe\n");
    490     }
    491 
    492     err = close (pComponentPrivate->filled_inpBuf_Q[1]);
    493     if ( 0 != err && OMX_ErrorNone == eError ) {
    494         eError = OMX_ErrorHardware;
    495         OMX_PRCOMM4(dbg, "Error while closing data pipe\n");
    496     }
    497 
    498     /*  close the command pipe handles  */
    499     err = close (pComponentPrivate->nCmdPipe[0]);
    500     if ( 0 != err && OMX_ErrorNone == eError ) {
    501         eError = OMX_ErrorHardware;
    502         OMX_PRCOMM4(dbg, "Error while closing cmd pipe\n");
    503     }
    504 
    505     err = close (pComponentPrivate->nCmdPipe[1]);
    506     if ( 0 != err && OMX_ErrorNone == eError ) {
    507         eError = OMX_ErrorHardware;
    508         OMX_PRCOMM4(dbg, "Error while closing cmd pipe\n");
    509     }
    510 
    511     /*  close the command data pipe handles */
    512     err = close (pComponentPrivate->nCmdDataPipe[0]);
    513     if ( 0 != err && OMX_ErrorNone == eError ) {
    514         eError = OMX_ErrorHardware;
    515         OMX_PRCOMM4(dbg, "Error while closing cmd pipe\n");
    516     }
    517 
    518     err = close (pComponentPrivate->nCmdDataPipe[1]);
    519     if ( 0 != err && OMX_ErrorNone == eError ) {
    520         eError = OMX_ErrorHardware;
    521         OMX_PRCOMM4(dbg, "Error while closing cmd pipe\n");
    522     }
    523 
    524     pthread_mutex_destroy(&pComponentPrivate->jpege_mutex);
    525     pthread_cond_destroy(&pComponentPrivate->stop_cond);
    526     pthread_cond_destroy(&pComponentPrivate->flush_cond);
    527     /* pthread_cond_destroy(&pComponentPrivate->control_cond); */
    528 
    529     pthread_mutex_destroy(&pComponentPrivate->jpege_mutex_app);
    530     pthread_cond_destroy(&pComponentPrivate->populate_cond);
    531     pthread_cond_destroy(&pComponentPrivate->unpopulate_cond);
    532 #ifdef __PERF_INSTRUMENTATION__
    533     PERF_Boundary(pComponentPrivate->pPERF,
    534     		PERF_BoundaryComplete | PERF_BoundaryCleanup);
    535     PERF_Done(pComponentPrivate->pPERF);
    536 #endif
    537 
    538     /* LinkedList_DisplayAll (&AllocList); */
    539     OMX_FREEALL();
    540     LinkedList_Destroy(&AllocList);
    541 
    542 EXIT:
    543     OMX_PRINT1(dbg, "Exiting JPEG FreeComponentresources\n");
    544     return eError;
    545 }
    546 
    547 OMX_ERRORTYPE Fill_JpegEncLCMLInitParams(LCML_DSP *lcml_dsp, OMX_U16 arr[], OMX_HANDLETYPE pComponent)
    548 {
    549     OMX_ERRORTYPE eError = OMX_ErrorNone;
    550     OMX_COMPONENTTYPE *pHandle = NULL;
    551     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
    552     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
    553     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
    554     int outbufsize = 0;
    555 
    556     OMX_U16 *ptCreateString = (OMX_U16*)arr;
    557     OMX_U32 *ptCreateStringPPLIB = (OMX_U32*)arr;
    558 
    559     OMX_CHECK_PARAM(pComponent);
    560     pHandle = (OMX_COMPONENTTYPE *)pComponent;
    561     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    562     OMX_PRINT1(pComponentPrivate->dbg, "Initialize Params\n");
    563     pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
    564     pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
    565     outbufsize = pPortDefOut->nBufferSize;
    566 
    567     lcml_dsp->In_BufInfo.nBuffers = NUM_OF_BUFFERSJPEG;
    568     lcml_dsp->In_BufInfo.nSize = pPortDefIn->nBufferSize;
    569     lcml_dsp->In_BufInfo.DataTrMethod = DMM_METHOD;
    570 
    571     lcml_dsp->Out_BufInfo.nBuffers = NUM_OF_BUFFERSJPEG;
    572     lcml_dsp->Out_BufInfo.nSize = outbufsize;
    573     lcml_dsp->Out_BufInfo.DataTrMethod = DMM_METHOD;
    574 
    575     lcml_dsp->NodeInfo.nNumOfDLLs = OMX_JPEGENC_NUM_DLLS;
    576     lcml_dsp->NodeInfo.AllUUIDs[0].uuid = (struct DSP_UUID * )&JPEGESOCKET_TI_UUID;
    577     strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[0].DllName,JPEG_ENC_NODE_DLL);
    578     lcml_dsp->NodeInfo.AllUUIDs[0].eDllType = DLL_NODEOBJECT;
    579 
    580     lcml_dsp->NodeInfo.AllUUIDs[1].uuid = (struct DSP_UUID * )&JPEGESOCKET_TI_UUID;
    581     strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[1].DllName,JPEG_ENC_NODE_DLL);
    582     lcml_dsp->NodeInfo.AllUUIDs[1].eDllType = DLL_DEPENDENT;
    583 
    584     lcml_dsp->NodeInfo.AllUUIDs[2].uuid =(struct DSP_UUID * ) &USN_UUID;
    585     strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[2].DllName,USN_DLL);
    586     lcml_dsp->NodeInfo.AllUUIDs[2].eDllType = DLL_DEPENDENT;
    587 
    588     lcml_dsp->NodeInfo.AllUUIDs[3].uuid =(struct DSP_UUID * ) &CONVERSIONS_UUID;
    589     strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[3].DllName,CONVERSIONS_DLL);
    590     lcml_dsp->NodeInfo.AllUUIDs[3].eDllType = DLL_DEPENDENT;
    591 
    592 #ifdef __JPEG_OMX_PPLIB_ENABLED__
    593     lcml_dsp->NodeInfo.AllUUIDs[4].uuid =(struct DSP_UUID * ) &PPLIB_UUID;
    594     strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[4].DllName,PPLIB_DLL);
    595     lcml_dsp->NodeInfo.AllUUIDs[4].eDllType = DLL_DEPENDENT;
    596 #endif
    597     lcml_dsp->SegID = 0;
    598     lcml_dsp->Timeout = -1;
    599     lcml_dsp->Alignment = 0;
    600     lcml_dsp->Priority = 5;
    601 
    602 #ifdef __JPEG_OMX_PPLIB_ENABLED__
    603       if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (352*288)) {
    604           lcml_dsp->ProfileID = 2 ;
    605       }
    606       else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (640*480)) {
    607           lcml_dsp->ProfileID = 2 +1 ; // temporary fix meanwhile SN confirms is there's any problem with VGA profile ID
    608       }
    609       else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (720*576)) {
    610           lcml_dsp->ProfileID = 3 ;
    611       }
    612       else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (1*1024*1024)) {
    613           lcml_dsp->ProfileID = 4 ;
    614       }
    615       else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (2*1024*1024)) {
    616           lcml_dsp->ProfileID = 5 ;
    617       }
    618       else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (3*1024*1024)) {
    619           lcml_dsp->ProfileID = 6 ;
    620       }
    621       else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (4*1024*1024)) {
    622           lcml_dsp->ProfileID = 7 ;
    623       }
    624       else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (5*1024*1024)) {
    625           lcml_dsp->ProfileID = 8 ;
    626       }
    627       else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (6*1024*1024)) {
    628           lcml_dsp->ProfileID = 9 ;
    629       }
    630       else {
    631           lcml_dsp->ProfileID = 10;
    632       }
    633 #else
    634     /* according to JPEG Enc SN Interface guide */
    635     /* if PPLib is not enabled than  use profile 1 by default */
    636     lcml_dsp->ProfileID = 2;
    637 #endif
    638 
    639     /* CrPhArgs for JpegEnc */
    640     ptCreateString[0] = JPGENC_SNTEST_STRMCNT;
    641     ptCreateString[1] = JPGENC_SNTEST_INSTRMID; /* Stream ID   */
    642     ptCreateString[2] = 0;                      /* Stream based input stream   */
    643     ptCreateString[3] = JPGENC_SNTEST_INBUFCNT; /* Number of buffers on input stream   */
    644     ptCreateString[4] = JPGENC_SNTEST_OUTSTRMID;/* Stream ID   */
    645     ptCreateString[5] = 0;                      /* Stream based input stream   */
    646     ptCreateString[6] = JPGENC_SNTEST_OUTBUFCNT;/* Number of buffers on input stream   */
    647     ptCreateString[7] = (pPortDefOut->format.image.nFrameWidth > 0) ? pPortDefOut->format.image.nFrameWidth : JPGENC_SNTEST_MAX_WIDTH;
    648     ptCreateString[8] = (pPortDefOut->format.image.nFrameHeight > 0) ? pPortDefOut->format.image.nFrameHeight : JPGENC_SNTEST_MAX_HEIGHT;
    649 
    650     /*
    651     if ( pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar ) {
    652         arr[9] = 1;
    653     } else if ( pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatCbYCrY ) {
    654         arr[9] = 4;
    655     }  else {
    656         arr[9] = 4;
    657     }
    658     */
    659     ptCreateString[9] = 1;
    660 
    661     ptCreateString[10] = 512; /* Maximum Horizontal Size of the Thumbnail for App0 marker */
    662     ptCreateString[11] = 384; /* Maximum Vertical Size of the Thumbnail for App0 marker */
    663     ptCreateString[12] = 512; /* Maximum Horizontal Size of the Thumbnail for App1 marker */
    664     ptCreateString[13] = 384; /* Maximum Vertical Size of the Thumbnail for App1 marker */
    665     ptCreateString[14] = 512; /* Maximum Horizontal Size of the Thumbnail for App13 marker */
    666     ptCreateString[15] = 384; /* Maximum Vertical Size of the Thumbnail for App13 marker */
    667     ptCreateString[16] = 0; /* Number of scans is always 0 */
    668 
    669 /*
    670 this option supportsonly up to 3 mega pixels
    671 	if (pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar)
    672     {
    673     	ptCreateString[16] = 1;
    674     }
    675 */
    676 
    677     ptCreateString[17] = 0;
    678     if ( pPortDefIn->format.image.eColorFormat == OMX_COLOR_Format32bitARGB8888){
    679     	ptCreateString[17] = 1; //Convert flag
    680     }
    681 
    682 	/*Do an internal conversion from YUV420P to YUV422I and encode so that
    683 	this JPEG would be viewable using the TI JPEG decoder*/
    684 	else if (pComponentPrivate->bConvert420pTo422i ){
    685 		if (pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar ){
    686 			ptCreateString[17] = 10;
    687 			if(pComponentPrivate->bPPLibEnable)
    688 			{
    689 				/* memory requirement for having both conversion and pplib is much larger */
    690 				lcml_dsp->ProfileID +=3;
    691 			}
    692 		}
    693 		else{
    694 			OMX_PRMGR4(pComponentPrivate->dbg, "Error invalid ColorFormat for YUVConvertion\n");
    695 			eError=-1;
    696 			goto EXIT;
    697 		}
    698 	}
    699 
    700     ptCreateString[18] = 512; /* Maximum Horizontal Size of the Thumbnail for App5 marker */
    701     ptCreateString[19] = 384; /* Maximum Vertical Size of the Thumbnail for App5 marker */
    702 
    703 #ifdef __JPEG_OMX_PPLIB_ENABLED__
    704 
    705     //size
    706     ptCreateStringPPLIB[10] = JPEGENC_PPLIB_CREATEPARAM_SIZE;
    707 
    708     //SlibVersion
    709     ptCreateStringPPLIB[11] = 0x00000100;
    710 
    711     //MaxInWidth
    712     ptCreateStringPPLIB[12] = pPortDefIn->format.image.nFrameWidth;
    713 
    714     //MaxOutWidth
    715     ptCreateStringPPLIB[13] = pPortDefIn->format.image.nFrameWidth;
    716     //Input Format => 0:RGB24, 1:RGB16, 2:RGB12, 3:RGB8, 4:RGB4, 5:YUV422ILE, 6:YUV422IBE,
    717     //                7:422_IN_UY_WS, 8:422_IN_YU_WS, 9:YUV420P, 10:GRAY8, 11:GRAY4, 12:GRAY2_IN, 13:GRAY1
    718     if (pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatCbYCrY
    719         ||  pPortDefIn->format.image.eColorFormat == OMX_COLOR_Format32bitARGB8888
    720         ||  pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYCbYCr
    721         ||  ptCreateString[17] == 10)
    722     {
    723         ptCreateStringPPLIB[14] = 5;
    724     }
    725     else if (pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatYUV420PackedPlanar)
    726     {
    727         ptCreateStringPPLIB[14] = 9;
    728     }
    729     else if (pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_Format16bitRGB565)
    730     {
    731         ptCreateStringPPLIB[14] = 1;
    732     }
    733     else{
    734         ptCreateStringPPLIB[14] = 9;
    735     }
    736 
    737     //YuvOutputFormat
    738     // --> 0 = OFF, 1 = YUV420, 2 = YUV422ILE, 3 = YUV422IBE
    739     if (pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatYUV420PackedPlanar
    740         && ptCreateString[17] != 10)
    741     {
    742         ptCreateStringPPLIB[15] = 1;
    743     }
    744     else
    745     {
    746         ptCreateStringPPLIB[15] = 2;
    747     }
    748 
    749     //RGBOuputFormat
    750     // --> 0 = OFF, 1 = RGB4, 2 = RGB8, 3 = RGB12, 4 = RGB16, 5 = RGB24, 6 = RGB32,
    751     //     7 = GRAY8, 8 = GRAY4, 9 = GRAY2, 10 = GRAY1
    752     ptCreateStringPPLIB[16] = 0;
    753 
    754     if(pComponentPrivate->bPPLibEnable)
    755     {
    756         ptCreateString[34] = END_OF_CR_PHASE_ARGS;
    757     }else
    758     {
    759         ptCreateString[20] = END_OF_CR_PHASE_ARGS;
    760     }
    761 #else
    762     ptCreateString[20] = END_OF_CR_PHASE_ARGS;
    763 #endif
    764     lcml_dsp->pCrPhArgs = ptCreateString;
    765     EXIT:
    766     return eError;
    767 }
    768 
    769 
    770 static OMX_ERRORTYPE HandleJpegEncInternalFlush(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nParam1)
    771 {
    772     OMX_ERRORTYPE eError = OMX_ErrorNone;
    773     OMX_U32 aParam[4];
    774     LCML_DSP_INTERFACE *pLcmlHandle = NULL;
    775 
    776     OMX_CHECK_PARAM(pComponentPrivate);
    777 
    778     if ( nParam1 == 0x0 ||
    779          (int)nParam1 == -1 ) {
    780 
    781         pComponentPrivate->bFlushComplete = OMX_FALSE;
    782         aParam[0] = USN_STRMCMD_FLUSH;
    783         aParam[1] = 0;
    784         aParam[2] = 0;
    785         pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
    786         eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStrmCtrl, (void*)aParam);
    787         if (eError != OMX_ErrorNone) {
    788             goto PRINT_EXIT;
    789         }
    790 
    791         pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
    792         while (pComponentPrivate->bFlushComplete == OMX_FALSE) {
    793             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
    794             pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->jpege_mutex);
    795         }
    796         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
    797 
    798         pComponentPrivate->bFlushComplete = OMX_FALSE;
    799     }
    800     if ( nParam1 == 0x1 ||
    801 	 (int)nParam1 == -1 ) {
    802 
    803         pComponentPrivate->bFlushComplete = OMX_FALSE;
    804         aParam[0] = USN_STRMCMD_FLUSH;
    805         aParam[1] = 1;
    806         aParam[2] = 0;
    807         pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
    808         eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStrmCtrl, (void*)aParam);
    809         if (eError != OMX_ErrorNone) {
    810             goto PRINT_EXIT;
    811         }
    812 
    813         pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
    814         while (pComponentPrivate->bFlushComplete == OMX_FALSE) {
    815             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
    816             pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->jpege_mutex);
    817         }
    818         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
    819         pComponentPrivate->bFlushComplete = OMX_FALSE;
    820 
    821         pComponentPrivate->bFlushComplete = OMX_FALSE;
    822     }
    823 
    824     PRINT_EXIT:
    825         OMX_PRINT1(pComponentPrivate->dbg, "Exiting HandleCommand FLush Function JEPG Encoder\n");
    826     EXIT:
    827     return eError;
    828 
    829 }
    830 
    831 
    832 OMX_ERRORTYPE HandleJpegEncCommandFlush(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nParam1)
    833 {
    834     OMX_ERRORTYPE eError = OMX_ErrorNone;
    835     OMX_U32 aParam[4];
    836     LCML_DSP_INTERFACE *pLcmlHandle = NULL;
    837 
    838     OMX_CHECK_PARAM(pComponentPrivate);
    839 
    840     if ( nParam1 == 0x0 ||
    841          (int)nParam1 == -1 ) {
    842 
    843         pComponentPrivate->bFlushComplete = OMX_FALSE;
    844 
    845         aParam[0] = USN_STRMCMD_FLUSH;
    846         aParam[1] = 0;
    847         aParam[2] = 0;
    848         pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
    849         eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStrmCtrl, (void*)aParam);
    850         if (eError != OMX_ErrorNone) {
    851             goto PRINT_EXIT;
    852         }
    853        OMX_PRDSP2(pComponentPrivate->dbg, "sent EMMCodecControlStrmCtrl command\n");
    854 
    855         pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
    856         while (pComponentPrivate->bFlushComplete == OMX_FALSE) {
    857             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
    858             pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->jpege_mutex);
    859         }
    860         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
    861 
    862         pComponentPrivate->bFlushComplete = OMX_FALSE;
    863 
    864         while (pComponentPrivate->nInPortIn > pComponentPrivate->nInPortOut) {
    865 
    866             OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
    867             JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
    868             int ret;
    869 
    870             ret = read(pComponentPrivate->filled_inpBuf_Q[0], &(pBuffHead), sizeof(pBuffHead));
    871             if ( ret == -1 ) {
    872                 OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
    873             }
    874 
    875             if (pBuffHead != NULL) {
    876                 pBuffPrivate = pBuffHead->pInputPortPrivate;
    877             }
    878 
    879             pComponentPrivate->nInPortOut ++;
    880             pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
    881             OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
    882                     pComponentPrivate->nInPortIn,
    883                     pComponentPrivate->nInPortOut,
    884                     pComponentPrivate->nOutPortIn,
    885                     pComponentPrivate->nOutPortOut);
    886             OMX_PRBUFFER1(pComponentPrivate->dbg, "before EmptyBufferDone\n");
    887             pComponentPrivate->cbInfo.EmptyBufferDone(
    888                            pComponentPrivate->pHandle,
    889                            pComponentPrivate->pHandle->pApplicationPrivate,
    890                            pBuffHead);
    891              OMX_PRBUFFER1(pComponentPrivate->dbg, "after EmptyBufferDone\n");
    892         }
    893 #if 0
    894         for ( i=0; i < pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual; i++ ) {
    895 
    896 #ifdef __PERF_INSTRUMENTATION__
    897             PERF_SendingFrame(pComponentPrivate->pPERFcomp,
    898                               PREF(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr,pBuffer),
    899                               0,
    900                               PERF_ModuleHLMM);
    901 #endif
    902           pBuffPrivate = (JPEGENC_BUFFER_PRIVATE*) pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr->pInputPortPrivate;
    903           OMX_PRBUFFER2(pComponentPrivate->dbg, "flush input port. buffer owner (%d) %d\n", i, pBuffPrivate->eBufferOwner);
    904         }
    905 #endif
    906 
    907         OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (flush input) %lu %lu %lu %lu\n",
    908                     pComponentPrivate->nInPortIn,
    909                     pComponentPrivate->nInPortOut,
    910                     pComponentPrivate->nOutPortIn,
    911                     pComponentPrivate->nOutPortOut);
    912 
    913         /* returned all input buffers */
    914         pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
    915                                       pComponentPrivate->pHandle->pApplicationPrivate,
    916                       OMX_EventCmdComplete,
    917                                       OMX_CommandFlush,
    918                                       JPEGENC_INP_PORT,
    919                                       NULL);
    920 
    921     }
    922     if ( nParam1 == 0x1 ||
    923          (int)nParam1 == -1 ) {
    924 
    925         pComponentPrivate->bFlushComplete = OMX_FALSE;
    926 
    927         aParam[0] = USN_STRMCMD_FLUSH;
    928         aParam[1] = 1;
    929         aParam[2] = 0;
    930         pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
    931         eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStrmCtrl, (void*)aParam);
    932         if (eError != OMX_ErrorNone) {
    933             goto PRINT_EXIT;
    934         }
    935         OMX_PRDSP2(pComponentPrivate->dbg, "(1) sent EMMCodecControlStrmCtrl command\n");
    936 
    937         pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
    938         while (pComponentPrivate->bFlushComplete == OMX_FALSE) {
    939             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
    940             pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->jpege_mutex);
    941         }
    942         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
    943         pComponentPrivate->bFlushComplete = OMX_FALSE;
    944 
    945         /* return all output buffers */
    946 
    947 #if 0
    948         for ( i=0; i < pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountActual ; i++ ) {
    949             OMX_PRBUFFER1(pComponentPrivate->dbg, "BEFORE  FillBufferDone in OMX_CommandFlush\n");
    950 
    951 #ifdef __PERF_INSTRUMENTATION__
    952                         PERF_SendingFrame(pComponentPrivate->pPERFcomp,
    953                                           PREF(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr,pBuffer),
    954                                           PREF(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr,nFilledLen),
    955                                           PERF_ModuleHLMM);
    956 #endif
    957           pBuffPrivate = (JPEGENC_BUFFER_PRIVATE*) pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr->pOutputPortPrivate;
    958           OMX_PRBUFFER2(pComponentPrivate->dbg, "flush output port. buffer owner (%d) %d\n", i, pBuffPrivate->eBufferOwner);
    959 
    960           OMX_PRBUFFER1(pComponentPrivate->dbg, "in flush 1: buffer %d owner %d\n", i, pBuffPrivate->eBufferOwner);
    961           if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_COMPONENT_IN) {
    962                   OMX_PRBUFFER1(pComponentPrivate->dbg, "return output buffer %p from free_in_pipe (flush)\n",
    963                      pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr);
    964                   pComponentPrivate->nOutPortOut ++;
    965                   pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
    966                   pComponentPrivate->cbInfo.FillBufferDone (pComponentPrivate->pHandle,
    967                                    pComponentPrivate->pHandle->pApplicationPrivate,
    968                                    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr);
    969           }
    970 
    971         }
    972 #endif
    973         while (pComponentPrivate->nOutPortIn > pComponentPrivate->nOutPortOut) {
    974             OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
    975             JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
    976             int ret;
    977 
    978             OMX_PRBUFFER1(pComponentPrivate->dbg, "in while loop %lu %lu )\n", pComponentPrivate->nOutPortIn, pComponentPrivate->nOutPortOut);
    979             ret = read(pComponentPrivate->free_outBuf_Q[0], &pBuffHead, sizeof(pBuffHead));
    980             if ( ret == -1 ) {
    981                 OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
    982                 goto PRINT_EXIT;
    983             }
    984             OMX_PRCOMM1(pComponentPrivate->dbg, "after read\n");
    985             if (pBuffHead != NULL) {
    986                pBuffPrivate = pBuffHead->pOutputPortPrivate;
    987             }
    988 
    989             pComponentPrivate->nOutPortOut ++;
    990             pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
    991             OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
    992                     pComponentPrivate->nInPortIn,
    993                     pComponentPrivate->nInPortOut,
    994                     pComponentPrivate->nOutPortIn,
    995                     pComponentPrivate->nOutPortOut);
    996              OMX_PRBUFFER1(pComponentPrivate->dbg, "before FillBufferDone\n");
    997              pComponentPrivate->cbInfo.FillBufferDone(pComponentPrivate->pHandle,
    998                     pComponentPrivate->pHandle->pApplicationPrivate,
    999                     pBuffHead);
   1000              OMX_PRBUFFER1(pComponentPrivate->dbg, "after FillBufferDone\n");
   1001         }
   1002 
   1003         OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (flush input) %lu %lu %lu %lu\n",
   1004                     pComponentPrivate->nInPortIn,
   1005                     pComponentPrivate->nInPortOut,
   1006                     pComponentPrivate->nOutPortIn,
   1007                     pComponentPrivate->nOutPortOut);
   1008 
   1009         pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1010                                                pComponentPrivate->pHandle->pApplicationPrivate,
   1011                                                OMX_EventCmdComplete,
   1012                                                OMX_CommandFlush,
   1013                                                JPEGENC_OUT_PORT,
   1014                                                NULL);
   1015     }
   1016 
   1017     PRINT_EXIT:
   1018         OMX_PRINT1(pComponentPrivate->dbg, "Exiting HandleCommand FLush Function JEPG Encoder\n");
   1019     EXIT:
   1020     return eError;
   1021 
   1022 }
   1023 
   1024 OMX_ERRORTYPE SendDynamicParam(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate)
   1025 {
   1026     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1027     IDMJPGE_TIGEM_DynamicParams* pTmpDynParams;
   1028     OMX_HANDLETYPE pLcmlHandle = NULL;
   1029     char* pTmp = NULL;
   1030     OMX_U32 cmdValues[3] = {0, 0, 0};
   1031     IIMGENC_DynamicParams ptParam;
   1032     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1033     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   1034 
   1035     OMX_CHECK_PARAM(pComponentPrivate);
   1036 
   1037     pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
   1038     pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
   1039 
   1040     ptParam.nNumAU = 0; /*  XDM_DEFAULT */
   1041     //ptParam.nSize   =   sizeof(IIMGENC_DynamicParams);
   1042     ptParam.nSize   =   sizeof(IDMJPGE_TIGEM_DynamicParams) ;
   1043 
   1044     if ( pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar ) {
   1045         ptParam.nInputChromaFormat = 1;
   1046     }
   1047     else if ( pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatCbYCrY
   1048     		|| pPortDefIn->format.image.eColorFormat == OMX_COLOR_Format32bitARGB8888
   1049             || pPortDefIn->format.image.eColorFormat == OMX_COLOR_Format16bitRGB565) {
   1050         ptParam.nInputChromaFormat = 4;
   1051     }
   1052 	else if ( pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYCbYCr) {
   1053 		ptParam.nInputChromaFormat = 9;
   1054 	}
   1055     else {
   1056         ptParam.nInputChromaFormat = 1;
   1057     }
   1058 
   1059     if (pComponentPrivate->pCrop->nWidth == 0)
   1060     {
   1061         ptParam.nInputWidth     = pPortDefIn->format.image.nFrameWidth;
   1062     }
   1063     else
   1064     {
   1065         ptParam.nInputWidth     = pComponentPrivate->pCrop->nWidth;
   1066     }
   1067 
   1068     if (pComponentPrivate->pCrop->nHeight == 0)
   1069     {
   1070         ptParam.nInputHeight     = pPortDefIn->format.image.nFrameHeight;
   1071     }
   1072     else
   1073     {
   1074         ptParam.nInputHeight     = pComponentPrivate->pCrop->nHeight;
   1075     }
   1076 
   1077     ptParam.nCaptureWidth   =  pPortDefIn->format.image.nFrameWidth;
   1078     ptParam.nGenerateHeader =   0; /*XDM_ENCODE_AU*/
   1079     ptParam.qValue          =   pComponentPrivate->pQualityfactor->nQFactor;
   1080 
   1081     OMX_PRDSP1(pComponentPrivate->dbg, "ptParam.qValue %lu\n", ptParam.qValue);
   1082 
   1083 
   1084     pTmp = (char*)pComponentPrivate->pDynParams;
   1085     pTmp += 128;
   1086     pTmpDynParams = (IDMJPGE_TIGEM_DynamicParams*)pTmp;
   1087 
   1088     pTmpDynParams->params         = ptParam;
   1089     pTmpDynParams->captureHeight = pPortDefIn->format.image.nFrameHeight;
   1090     pTmpDynParams->DRI_Interval  = pComponentPrivate->nDRI_Interval;
   1091     pTmpDynParams->huffmanTable = NULL;
   1092     pTmpDynParams->quantTable     = NULL;
   1093 
   1094     cmdValues[0] = IUALG_CMD_SETSTATUS;
   1095     cmdValues[1] = (OMX_U32)(pTmpDynParams);
   1096     cmdValues[2] = sizeof(IDMJPGE_TIGEM_DynamicParams) + 128;
   1097 
   1098     pComponentPrivate->bAckFromSetStatus = 0;
   1099     pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1100     eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1101                                                EMMCodecControlAlgCtrl,
   1102                                                (void*)&cmdValues);
   1103 
   1104 EXIT:
   1105     return eError;
   1106 }
   1107 
   1108 #ifdef __JPEG_OMX_PPLIB_ENABLED__
   1109 OMX_ERRORTYPE SendDynamicPPLibParam(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate,OMX_U32 *ptInputParam)
   1110 {
   1111     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1112     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1113     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   1114 
   1115     OMX_CHECK_PARAM(pComponentPrivate);
   1116     pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
   1117     pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
   1118 
   1119     // PPLIB hardcoded params
   1120     OMX_U32 ulOutPitch = 0;
   1121     OMX_U32 ulPPLIBVideoGain=64;
   1122     OMX_U32 ulPPLIBEnableCropping=0;
   1123     OMX_U32 ulPPLIBXstart=0;
   1124     OMX_U32 ulPPLIBYstart=0;
   1125     OMX_U32 ulPPLIBXsize=0;
   1126     OMX_U32 ulPPLIBYsize=0;
   1127     OMX_U32 ulPPLIBEnableZoom=0;
   1128     OMX_U32 ulPPLIBZoomFactor=0;
   1129     OMX_U32 ulPPLIBZoomLimit=0;
   1130     OMX_U32 ulPPLIBZoomSpeed=0;
   1131     OMX_U32 ulPPLIBLightChroma=0;
   1132     OMX_U32 ulPPLIBLockedRatio=1;
   1133     OMX_U32 ulPPLIBMirroring=0;
   1134     OMX_U32 ulPPLIBRGBrotation=0;
   1135     OMX_U32 ulPPLIBYUVRotation=0;
   1136     OMX_U32 ulPPLIBIORange=1;
   1137     OMX_U32 ulPPLIBDithering=0;
   1138 
   1139     OMX_U32 cOffset = 0;
   1140 
   1141     /* PPLIB_RunTimeParams */
   1142     // LgUns size; // size of structure
   1143 
   1144     ptInputParam[0] = JPEGENC_PPLIB_DYNPARM_SIZE; // 252 bytes
   1145 
   1146     // LgUns ulInWidth; // picture buffer width
   1147 
   1148     ptInputParam[1] = pPortDefIn->format.image.nFrameWidth;
   1149 
   1150     // LgUns ulInHeight; // picture buffer height
   1151 
   1152     ptInputParam[2] = pPortDefIn->format.image.nFrameHeight;
   1153 
   1154     // LgUns FrameEnabled[0] (enable instance 1 of VGPOP)
   1155 
   1156     ptInputParam[3] = 1;
   1157 
   1158     // LgUns FrameEnabled[1] (enable instance 2 of VGPOP - not used now)
   1159 
   1160     ptInputParam[4] = 0;
   1161 
   1162     // LgUns EnalbeYUVOutput[0]
   1163 
   1164     ptInputParam[5] = 1;
   1165 
   1166     // LgUns FrameEnabled[1]
   1167 
   1168     ptInputParam[6] = 0;
   1169 
   1170     // LgUns EnalbeRGBOutput[0]
   1171 
   1172     ptInputParam[7] = 0;
   1173 
   1174     // LgUns EnalbeRGBOutput[1]
   1175 
   1176     ptInputParam[8] = 0;
   1177 
   1178     // LgUns FrameInputStartYOffset[0]
   1179 
   1180     ptInputParam[9] = 0;
   1181 
   1182     // LgUns FrameInputStartYOffset[1]
   1183 
   1184     ptInputParam[10] = 0;
   1185 
   1186      cOffset = (pPortDefIn->format.image.nFrameWidth * pPortDefIn->format.image.nFrameHeight);
   1187 
   1188     // LgUns FrameInputStartCOffset[0]
   1189 
   1190     ptInputParam[11] = cOffset;
   1191 
   1192     // LgUns FrameInputStartCOffset[1]
   1193 
   1194     ptInputParam[12] = cOffset;
   1195 
   1196     // LgUns FrameOutputStartYOffset[0]
   1197 
   1198     ptInputParam[13] = 0;
   1199 
   1200     // LgUns FrameOutputStartYOffset[1]
   1201 
   1202     ptInputParam[14] = 0;
   1203 
   1204     if (ulOutPitch > 0) {
   1205         if (ulPPLIBYUVRotation == 0 || ulPPLIBYUVRotation == 180) {
   1206             cOffset = (pPortDefIn->format.image.nFrameHeight * ulOutPitch);
   1207         }
   1208         else {
   1209             cOffset = (pPortDefIn->format.image.nFrameWidth * ulOutPitch);
   1210         }
   1211     }
   1212     else {
   1213         cOffset = (pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth);
   1214     }
   1215 
   1216     // LgUns FrameOutputStartCOffset[0]
   1217     ptInputParam[15] = cOffset;
   1218 
   1219     // LgUns FrameOutputStartCOffset[1]
   1220     ptInputParam[16] = cOffset;
   1221 
   1222     // LgUns FrameOutputRGBOffset[0]
   1223     ptInputParam[17] = 0;
   1224 
   1225     // LgUns FrameOutputRGBOffset[1]
   1226     ptInputParam[18] = 0;
   1227 
   1228     // LgUns ulOutHeight[0]; // picture buffer height
   1229     ptInputParam[19] = pPortDefIn->format.image.nFrameHeight;
   1230 
   1231     // LgUns ulOutHeight[1]; // picture buffer height
   1232     ptInputParam[20] = 0;
   1233 
   1234     // LgUns ulOutWidth[0]; // picture buffer width
   1235     ptInputParam[21] = pPortDefIn->format.image.nFrameWidth;
   1236 
   1237     // LgUns ulOutWidth[1]; // picture buffer width
   1238     ptInputParam[22] = 0;
   1239 
   1240     //Contrast (same as Video Gain)
   1241     ptInputParam[23] = ulPPLIBVideoGain;
   1242 
   1243     //Contrast (same as Video Gain)
   1244     ptInputParam[24] = ulPPLIBVideoGain;
   1245 
   1246     if (ulPPLIBEnableCropping == 1) {
   1247         // Cropping
   1248         // LgUns ulInXstart[0]; // Hin active start
   1249         ptInputParam[25] = ulPPLIBXstart;
   1250 
   1251         // Cropping
   1252         // LgUns ulInXstart[1]; // Hin active start
   1253         ptInputParam[26] = 0;
   1254 
   1255         //LgUns ulInYstart[0]; // Vin active start
   1256         ptInputParam[27] = ulPPLIBYstart;
   1257 
   1258         //LgUns ulInYstart[1]; // Vin active start
   1259         ptInputParam[28] = 0;
   1260 
   1261         // LgUns ulInXsize[0]; // Hin active width
   1262         ptInputParam[29] = ulPPLIBXsize;
   1263 
   1264         // LgUns ulInXsize[1]; // Hin active width
   1265         ptInputParam[30] = 0;
   1266 
   1267         // LgUns ulInYsize; // Vin active height
   1268         ptInputParam[31] = ulPPLIBYsize;
   1269 
   1270         // LgUns ulInYsize; // Vin active height
   1271         ptInputParam[32] = 0;
   1272     }
   1273     else {
   1274         // Cropping
   1275         // LgUns ulInXstart; // Hin active start
   1276         ptInputParam[25] = 0;
   1277 
   1278         // Cropping
   1279         // LgUns ulInXstart[1]; // Hin active start
   1280         ptInputParam[26] = 0;
   1281 
   1282         //LgUns ulInYstart; // Vin active start
   1283         ptInputParam[27] = 0;
   1284 
   1285         //LgUns ulInYstart[1]; // Vin active start
   1286         ptInputParam[28] = 0;
   1287 
   1288         // LgUns ulInXsize; // Hin active width
   1289         ptInputParam[29] = 0;
   1290 
   1291         // LgUns ulInXsize[1]; // Hin active width
   1292         ptInputParam[30] = 0;
   1293 
   1294         // LgUns ulInYsize; // Vin active height
   1295         ptInputParam[31] = 0;
   1296 
   1297         // LgUns ulInYsize; // Vin active height
   1298         ptInputParam[32] = 0;
   1299     }
   1300 
   1301     if (ulPPLIBEnableZoom) {
   1302         //Zoom
   1303         //LgUns ulZoomFactor; // zooming ratio (/1024)
   1304         ptInputParam[33] = ulPPLIBZoomFactor;
   1305 
   1306         //Zoom
   1307         //LgUns ulZoomFactor; // zooming ratio (/1024)
   1308         ptInputParam[34] = 1024;
   1309 
   1310         // LgUns ulZoomLimit; // zooming ratio limit (/1024)
   1311         ptInputParam[35] = ulPPLIBZoomLimit;
   1312 
   1313         // LgUns ulZoomLimit; // zooming ratio limit (/1024)
   1314         ptInputParam[36] = 1024;
   1315 
   1316         // LgInt slZoomSpeed; // speed of ratio change
   1317         ptInputParam[37] = ulPPLIBZoomSpeed;
   1318 
   1319         // LgInt slZoomSpeed; // speed of ratio change
   1320         ptInputParam[38] = 0;
   1321     }
   1322     else {
   1323         //Zoom
   1324         //LgUns ulZoomFactor; // zooming ratio (/1024)
   1325         ptInputParam[33] = 1024;
   1326 
   1327         //Zoom
   1328         //LgUns ulZoomFactor; // zooming ratio (/1024)
   1329         ptInputParam[34] = 1024;
   1330 
   1331         // LgUns ulZoomLimit; // zooming ratio limit (/1024)
   1332         ptInputParam[35] = 1024;
   1333 
   1334         // LgUns ulZoomLimit; // zooming ratio limit (/1024)
   1335         ptInputParam[36] = 1024;
   1336 
   1337         // LgInt slZoomSpeed; // speed of ratio change
   1338         ptInputParam[37] = 0;
   1339 
   1340         // LgInt slZoomSpeed; // speed of ratio change
   1341         ptInputParam[38] = 0;
   1342     }
   1343 
   1344     // LgUns bLightChroma[0]; // Light chrominance process
   1345     ptInputParam[39] = ulPPLIBLightChroma;
   1346 
   1347     // LgUns bLightChroma[1]; // Light chrominance process
   1348     ptInputParam[40] = ulPPLIBLightChroma;
   1349 
   1350     //Aspect Ration Locked/unlocked
   1351     // LgUns bLockedRatio; // keep H/V ratio
   1352     ptInputParam[41] = ulPPLIBLockedRatio;
   1353 
   1354     //Aspect Ration Locked/unlocked
   1355     // LgUns bLockedRatio; // keep H/V ratio
   1356     ptInputParam[42] = ulPPLIBLockedRatio;
   1357 
   1358     //Mirroring and Rotation
   1359     // LgUns bMirror; // to mirror the picture
   1360     ptInputParam[43] = ulPPLIBMirroring;
   1361 
   1362     //Mirroring and Rotation
   1363     // LgUns bMirror; // to mirror the picture
   1364     ptInputParam[44] = ulPPLIBMirroring;
   1365 
   1366     // LgUns eRGBrotation; // 0, 90, 180, 270 deg.
   1367     ptInputParam[45] = ulPPLIBRGBrotation;
   1368 
   1369     // LgUns eRGBrotation; // 0, 90, 180, 270 deg.
   1370     ptInputParam[46] = ulPPLIBRGBrotation;
   1371 
   1372     // LgUns eYUVrotation; // 0, 90, 180, 270 deg.
   1373     ptInputParam[47] = ulPPLIBYUVRotation;
   1374 
   1375     // LgUns eYUVrotation; // 0, 90, 180, 270 deg.
   1376     ptInputParam[48] = ulPPLIBYUVRotation;
   1377 
   1378     // IO Range and Dithering
   1379     // LgUns eIORange; // Input/Output video range
   1380     // 0 = VGPOP_IN_16_235_OUT_16_235 (limi range to limi range),
   1381     // 1 = VGPOP_IN_00_255_OUT_00_255 (full range to full range),
   1382     // 2 = VGPOP_IN_00_255_OUT_16_235 (full range to limi range),
   1383     // 3 = VGPOP_IN_16_235_OUT_00_255 (limi range to full range)
   1384     ptInputParam[49] = ulPPLIBIORange;
   1385 
   1386     // IO Range and Dithering
   1387     // LgUns eIORange; // Input/Output video range
   1388     // 0 = VGPOP_IN_16_235_OUT_16_235 (limi range to limi range),
   1389     // 1 = VGPOP_IN_00_255_OUT_00_255 (full range to full range),
   1390     // 2 = VGPOP_IN_00_255_OUT_16_235 (full range to limi range),
   1391     // 3 = VGPOP_IN_16_235_OUT_00_255 (limi range to full range)
   1392     ptInputParam[50] = ulPPLIBIORange;
   1393 
   1394     // LgUns bDithering; // ON Enables the dithering
   1395     ptInputParam[51] = ulPPLIBDithering;
   1396 
   1397     // LgUns bDithering; // ON Enables the dithering
   1398     ptInputParam[52] = ulPPLIBDithering;
   1399 
   1400     // LgUns ulFrameOutputPitch; // ON Enables the dithering
   1401     ptInputParam[53] = ulOutPitch;
   1402 
   1403     // LgUns bDithering; // ON Enables the dithering
   1404     ptInputParam[54] = ulOutPitch;
   1405 
   1406     // LgUns ulAlphaRGB;
   1407     ptInputParam[55] = 0;
   1408 
   1409     // LgUns ulAlphaRGB;
   1410     ptInputParam[56] = 0;
   1411 
   1412     // LgUns ulIsFrameGenerated[0]
   1413     ptInputParam[57] = 0;
   1414 
   1415     // LgUns ulIsFrameGenerated[1]
   1416     ptInputParam[58] = 0;
   1417 
   1418     // LgUns ulYUVFrameSize[0]
   1419     ptInputParam[59] = 0;
   1420 
   1421     // LgUns ulYUVFrameSize[1]
   1422     ptInputParam[60] = 0;
   1423 
   1424     // LgUns ulRGBFrameSize[0]
   1425     ptInputParam[61] = 0;
   1426 
   1427     // LgUns ulRGBFrameSize[1]
   1428     ptInputParam[62] = 0;
   1429 
   1430 EXIT:
   1431     return eError;
   1432 }
   1433 #endif
   1434 
   1435 /*-------------------------------------------------------------------*/
   1436 /**
   1437   *  HandleCommand() Handle State type commands
   1438   *
   1439   *  Depending on the State Command received it executes the corresponding code.
   1440   *
   1441   * @param phandle    handle for this instance of the component
   1442   *
   1443   * @retval OMX_NoError              Success, ready to roll
   1444   * @retval OMX_ErrorInsufficientResources    Insiffucient Resources
   1445   * @retval OMX_ErrorInvalidState    Invalid State Change
   1446   *
   1447   **/
   1448 /*-------------------------------------------------------------------*/
   1449 OMX_ERRORTYPE HandleJpegEncCommand (JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nParam1)
   1450 {
   1451 
   1452     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1453     OMX_COMPONENTTYPE *pHandle = NULL;
   1454     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1455     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   1456     OMX_HANDLETYPE pLcmlHandle = NULL;
   1457     LCML_DSP *lcml_dsp;
   1458     OMX_U16 arr[100];
   1459     LCML_CALLBACKTYPE cb;
   1460 #ifdef RESOURCE_MANAGER_ENABLED
   1461     OMX_U32 lImageResolution = 0;
   1462     OMX_U8 nMHzRM = 0;
   1463 #endif
   1464 
   1465 
   1466 
   1467     OMX_CHECK_PARAM(pComponentPrivate);
   1468     OMX_PRINT1(pComponentPrivate->dbg, "JPEGEnc Handlecommand\n");
   1469     pHandle = (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
   1470     pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
   1471     pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
   1472 
   1473     switch ( (OMX_STATETYPE)(nParam1) ) {
   1474     case OMX_StateIdle:
   1475         OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateIdle\n");
   1476         OMX_PRSTATE1(pComponentPrivate->dbg, "CHP 1 pComponentPrivate->nCurState  = %d\n",pComponentPrivate->nCurState );
   1477         OMX_PRSTATE1(pComponentPrivate->dbg, "In idle in %lu out %lu\n", pComponentPrivate->nInPortIn, pComponentPrivate->nOutPortOut);
   1478 
   1479         if ( pComponentPrivate->nCurState == OMX_StateIdle ) {
   1480             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1481                                                    pComponentPrivate->pHandle->pApplicationPrivate,
   1482                                                    OMX_EventError,
   1483                                                    OMX_ErrorSameState,
   1484                                                    OMX_TI_ErrorMinor,
   1485                                                    NULL);
   1486             break;
   1487         }
   1488         else if ( pComponentPrivate->nCurState == OMX_StateLoaded ||
   1489                   pComponentPrivate->nCurState == OMX_StateWaitForResources) {
   1490 
   1491                       OMX_PRSTATE2(pComponentPrivate->dbg, "state tranc from loaded to idle\n");
   1492 #ifdef __PERF_INSTRUMENTATION__
   1493                   PERF_Boundary(pComponentPrivate->pPERFcomp,
   1494                                 PERF_BoundaryStart | PERF_BoundarySetup);
   1495 #endif
   1496 
   1497 #ifdef RESOURCE_MANAGER_ENABLED /* Resource Manager Proxy Calls */
   1498             pComponentPrivate->rmproxyCallback.RMPROXY_Callback = (void *)ResourceManagerCallback;
   1499             lImageResolution = pPortDefIn->format.image.nFrameWidth * pPortDefIn->format.image.nFrameHeight;
   1500             OMX_GET_RM_VALUE(lImageResolution, nMHzRM, pComponentPrivate->dbg);
   1501             OMX_PRMGR2(pComponentPrivate->dbg, "Value sent to RM = %d\n", nMHzRM);
   1502             if (pComponentPrivate->nCurState != OMX_StateWaitForResources) {
   1503 
   1504                 eError = RMProxy_NewSendCommand(pHandle, RMProxy_RequestResource, OMX_JPEG_Encoder_COMPONENT, nMHzRM, 3456, &(pComponentPrivate->rmproxyCallback));
   1505 
   1506                 if (eError != OMX_ErrorNone) {
   1507                     /* resource is not available, need set state to OMX_StateWaitForResources*/
   1508                     OMX_PRMGR4(pComponentPrivate->dbg, "Resource is not available\n");
   1509 
   1510                     pComponentPrivate->cbInfo.EventHandler(pHandle,
   1511                                                            pHandle->pApplicationPrivate,
   1512                                                            OMX_EventError,
   1513                                                            OMX_ErrorInsufficientResources,
   1514                                                            OMX_TI_ErrorSevere,
   1515                                                            NULL);
   1516                     eError = OMX_ErrorNone;
   1517                     break;
   1518                 }
   1519             }
   1520 #endif
   1521 
   1522             if ( pPortDefIn->bEnabled == OMX_TRUE && pPortDefOut->bEnabled == OMX_TRUE ) {
   1523                 pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
   1524                 while ( (!pPortDefIn->bPopulated) || (!pPortDefOut->bPopulated)) {
   1525                     OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
   1526                     pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->jpege_mutex_app);
   1527                 }
   1528                 pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
   1529             }
   1530 
   1531             eError =  GetJpegEncLCMLHandle(pHandle);
   1532 
   1533             if ( eError != OMX_ErrorNone ) {
   1534                 OMX_PRDSP4(pComponentPrivate->dbg, "GetLCMLHandle failed...\n");
   1535                 goto EXIT;
   1536             }
   1537             pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1538             lcml_dsp = (((LCML_DSP_INTERFACE*)pLcmlHandle)->dspCodec);
   1539             Fill_JpegEncLCMLInitParams(lcml_dsp,arr, pHandle);
   1540             cb.LCML_Callback = (void *) JpegEncLCML_Callback;
   1541             OMX_PRDSP2(pComponentPrivate->dbg, "Start LCML_InitMMCodec JPEG Phase in JPEG.....\n");
   1542 
   1543             /*  calling initMMCodec to init codec with details filled earlier   */
   1544             eError = LCML_InitMMCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle, NULL, &pLcmlHandle, NULL, &cb);
   1545             if ( eError != OMX_ErrorNone ) {
   1546                 OMX_PRDSP4(pComponentPrivate->dbg, "InitMMCodec failed...  %x\n", eError);
   1547                 printf("Error : InitMMCodec failed...>>>>>>");
   1548                 pComponentPrivate->cbInfo.EventHandler(pHandle,
   1549                                                        pHandle->pApplicationPrivate,
   1550                                                        OMX_EventError,
   1551                                                        OMX_ErrorHardware,
   1552                                                        OMX_TI_ErrorSevere,
   1553                                                        NULL);
   1554                 goto EXIT;
   1555             }
   1556             pComponentPrivate->isLCMLActive = 1;
   1557             OMX_PRDSP2(pComponentPrivate->dbg, "End LCML_InitMMCodec Phase\n");
   1558 
   1559 
   1560             pComponentPrivate->bFlushComplete = OMX_FALSE;
   1561             OMX_PRSTATE2(pComponentPrivate->dbg, "State has been Set to Idle\n");
   1562             pComponentPrivate->nCurState = OMX_StateIdle;
   1563 
   1564             /* Decrement reference count with signal enabled */
   1565             if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1566                    return OMX_ErrorUndefined;
   1567             }
   1568 
   1569             pComponentPrivate->nInPortIn   = pComponentPrivate->nInPortOut   = 0;
   1570             pComponentPrivate->nOutPortIn = pComponentPrivate->nOutPortOut = 0;
   1571 
   1572 
   1573 #ifdef RESOURCE_MANAGER_ENABLED
   1574             eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateIdle,  3456, NULL);
   1575             if (eError != OMX_ErrorNone) {
   1576                 OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available Loaded ->Idle\n");
   1577 
   1578                 pComponentPrivate->cbInfo.EventHandler(pHandle,
   1579                                                        pHandle->pApplicationPrivate,
   1580                                                        OMX_EventError,
   1581                                                        OMX_ErrorInsufficientResources,
   1582                                                        OMX_TI_ErrorSevere,
   1583                                                        NULL);
   1584                 break;
   1585             }
   1586 #endif
   1587 
   1588 #ifdef __PERF_INSTRUMENTATION__
   1589             PERF_Boundary(pComponentPrivate->pPERFcomp,
   1590                           PERF_BoundaryComplete | PERF_BoundarySetup);
   1591 #endif
   1592 
   1593             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1594                               pComponentPrivate->pHandle->pApplicationPrivate,
   1595                           OMX_EventCmdComplete,
   1596                           OMX_CommandStateSet,
   1597                           pComponentPrivate->nCurState,
   1598                           NULL);
   1599             break;
   1600 
   1601         }
   1602         else if ( pComponentPrivate->nCurState == OMX_StateExecuting ||
   1603                   pComponentPrivate->nCurState == OMX_StatePause ) {
   1604 #ifdef __PERF_INSTRUMENTATION__
   1605             PERF_Boundary(pComponentPrivate->pPERFcomp,
   1606                           PERF_BoundaryComplete | PERF_BoundarySteadyState);
   1607 #endif
   1608 
   1609           pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1610           pComponentPrivate->bDSPStopAck = OMX_FALSE;
   1611           OMX_PRDSP2(pComponentPrivate->dbg, "bDSPStopAck is %d\n", pComponentPrivate->bDSPStopAck);
   1612           eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,MMCodecControlStop,NULL);
   1613           pComponentPrivate->nApp_nBuf= 1;
   1614          /* HandleJpegEncCommandFlush(pComponentPrivate, -1); */
   1615           /*
   1616           if ( pComponentPrivate->isLCMLActive ==1 ) {
   1617               LCML_ControlCodec(((LCML_DSP_INTERFACE*)pComponentPrivate->pLCML)->pCodecinterfacehandle,EMMCodecControlDestroy,NULL);
   1618               dlclose(pComponentPrivate->pDllHandle);
   1619               pComponentPrivate->isLCMLActive = 0;
   1620           }
   1621           */
   1622 
   1623 #ifdef RESOURCE_MANAGER_ENABLED
   1624 
   1625         eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateIdle, 3456, NULL);
   1626         if (eError != OMX_ErrorNone) {
   1627 	        OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available Executing ->Idle\n");
   1628             pComponentPrivate->nCurState = OMX_StateWaitForResources;
   1629             pComponentPrivate->cbInfo.EventHandler(pHandle,
   1630                                                    pHandle->pApplicationPrivate,
   1631                                                    OMX_EventCmdComplete,
   1632                                                    OMX_CommandStateSet,
   1633                                                    pComponentPrivate->nCurState,
   1634                                                    NULL);
   1635             break;
   1636         }
   1637 #endif
   1638           pComponentPrivate->ExeToIdleFlag |= JPEGE_BUFFERBACK;
   1639 
   1640         OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc: before stop lock\n");
   1641         pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
   1642         /*
   1643         while ((pComponentPrivate->ExeToIdleFlag & 0x3) != JPEGE_IDLEREADY) {
   1644             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
   1645             pthread_cond_wait(&pComponentPrivate->stop_cond, &pComponentPrivate->jpege_mutex);
   1646         }
   1647         */
   1648         while (pComponentPrivate->bDSPStopAck == OMX_FALSE) {
   1649             OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
   1650             pthread_cond_wait(&pComponentPrivate->stop_cond, &pComponentPrivate->jpege_mutex);
   1651         }
   1652         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
   1653 
   1654         OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:got STOP ack from DSP\n");
   1655 
   1656         int i;
   1657         for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual); i ++) {
   1658             JPEGENC_BUFFER_PRIVATE    *pBuffPrivate = NULL;
   1659             OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
   1660 
   1661             pBuffHead = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr;
   1662             pBuffPrivate = pBuffHead->pInputPortPrivate;
   1663 
   1664             OMX_PRBUFFER1(pComponentPrivate->dbg, "JPEG enc:: owner %d \n", pBuffPrivate->eBufferOwner);
   1665             if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
   1666                 if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufSupplier != OMX_BufferSupplyInput) {
   1667                     if(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent == NULL){
   1668                         OMX_PRBUFFER2(pComponentPrivate->dbg, "Sending buffer to app\n");
   1669                         OMX_PRDSP2(pComponentPrivate->dbg, "Handle error from DSP/bridge\n");
   1670                         pComponentPrivate->nInPortOut ++;
   1671                         pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   1672                         pComponentPrivate->cbInfo.EmptyBufferDone(
   1673                                    pComponentPrivate->pHandle,
   1674                                    pComponentPrivate->pHandle->pApplicationPrivate,
   1675                                    pBuffHead);
   1676                 }
   1677                 else{
   1678                     OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending beffer to tunnel, pHandle=%p\n", pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent);
   1679                     pBuffHead->nFilledLen = 0;
   1680                     pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   1681                     eError = OMX_FillThisBuffer(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent,
   1682                                                 pBuffHead);
   1683                 }
   1684             }
   1685         }
   1686     }
   1687 
   1688         OMX_PRBUFFER2(pComponentPrivate->dbg, "returned all input buffers\n");
   1689 
   1690         for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountActual); i ++) {
   1691             JPEGENC_BUFFER_PRIVATE    *pBuffPrivate = NULL;
   1692             OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
   1693 
   1694             pBuffHead = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr;
   1695             pBuffPrivate = pBuffHead->pOutputPortPrivate;
   1696 
   1697             OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer %p owner %d \n", pBuffHead, pBuffPrivate->eBufferOwner);
   1698             if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
   1699                 if (pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufSupplier != OMX_BufferSupplyOutput) {
   1700                     if(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->hTunnelComponent == NULL){
   1701                         OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending OUTPUT buffer to app\n");
   1702                         OMX_PRDSP2(pComponentPrivate->dbg, "Handle error from DSP/bridge\n");
   1703                         pComponentPrivate->nOutPortOut ++;
   1704                         pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   1705                         pComponentPrivate->cbInfo.FillBufferDone(
   1706                                    pComponentPrivate->pHandle,
   1707                                    pComponentPrivate->pHandle->pApplicationPrivate,
   1708                                    pBuffHead);
   1709                     }
   1710                     else{
   1711                         OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending OUTPUT buffer to Tunnel component\n");
   1712                         pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   1713                     eError = OMX_EmptyThisBuffer(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->hTunnelComponent,
   1714                                                 pBuffHead);
   1715                     }
   1716                 }
   1717             }
   1718         }
   1719         OMX_PRBUFFER2(pComponentPrivate->dbg, "returned all output buffers\n");
   1720 
   1721          pComponentPrivate->nCurState = OMX_StateIdle;
   1722 
   1723          /* Decrement reference count with signal enabled */
   1724          if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1725               return OMX_ErrorUndefined;
   1726          }
   1727 
   1728          pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1729                pComponentPrivate->pHandle->pApplicationPrivate,
   1730                OMX_EventCmdComplete,
   1731                OMX_CommandStateSet,
   1732                pComponentPrivate->nCurState,
   1733                NULL);
   1734              pComponentPrivate->ExeToIdleFlag = 0;
   1735     }
   1736         else {
   1737          OMX_PRSTATE4(pComponentPrivate->dbg, "Error: Invalid State Given by Application\n");
   1738          pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
   1739                                                     pComponentPrivate->pHandle->pApplicationPrivate,
   1740                             OMX_EventError,
   1741                                                     OMX_ErrorIncorrectStateTransition,
   1742                                                     OMX_TI_ErrorMinor,
   1743                                                     "Invalid State");
   1744         }
   1745     break;
   1746 
   1747     case OMX_StateExecuting:
   1748 
   1749         OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateExecuting \n");
   1750         OMX_PRBUFFER2(pComponentPrivate->dbg, "In exec in %lu out %lu\n", pComponentPrivate->nInPortIn, pComponentPrivate->nOutPortOut);
   1751         if ( pComponentPrivate->nCurState == OMX_StateExecuting ) {
   1752             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1753                                                    pComponentPrivate->pHandle->pApplicationPrivate,
   1754                                                    OMX_EventError,
   1755                                                    OMX_ErrorSameState,
   1756                                                    OMX_TI_ErrorMinor,
   1757                                                    NULL);
   1758         }
   1759         else if ( pComponentPrivate->nCurState == OMX_StateIdle || pComponentPrivate->nCurState == OMX_StatePause ) {
   1760 
   1761 #ifdef __PERF_INSTRUMENTATION__
   1762             PERF_Boundary(pComponentPrivate->pPERFcomp,
   1763                           PERF_BoundaryStart | PERF_BoundarySteadyState);
   1764 #endif
   1765 
   1766 #if 1
   1767         eError = SendDynamicParam(pComponentPrivate);
   1768             if (eError != OMX_ErrorNone ) {
   1769                 OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
   1770                 goto EXIT;
   1771         }
   1772 #endif
   1773 
   1774 
   1775 
   1776 
   1777         OMX_PRDSP2(pComponentPrivate->dbg, "after SendDynamicParam\n");
   1778         pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1779         eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStart,NULL);
   1780 
   1781 #ifdef RESOURCE_MANAGER_ENABLED
   1782 
   1783     eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateExecuting, 3456, NULL);
   1784     if (eError != OMX_ErrorNone) {
   1785         OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available\n");
   1786         pComponentPrivate->nCurState = OMX_StateWaitForResources;
   1787         pComponentPrivate->cbInfo.EventHandler(pHandle,
   1788                                                pHandle->pApplicationPrivate,
   1789                                                OMX_EventCmdComplete,
   1790                                                OMX_CommandStateSet,
   1791                                                pComponentPrivate->nCurState,
   1792                                                NULL);
   1793         break;
   1794         }
   1795 #endif
   1796 
   1797         pComponentPrivate->nCurState = OMX_StateExecuting;
   1798         OMX_PRSTATE2(pComponentPrivate->dbg, "State has been set to Executing\n");
   1799 #ifdef __PERF_INSTRUMENTATION__
   1800             PERF_Boundary(pComponentPrivate->pPERFcomp,
   1801                           PERF_BoundarySteadyState| PERF_BoundaryComplete);
   1802 #endif
   1803 
   1804          /* Decrement reference count with signal enabled */
   1805          if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1806                return OMX_ErrorUndefined;
   1807          }
   1808 
   1809         pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1810                                                pComponentPrivate->pHandle->pApplicationPrivate,
   1811                                                OMX_EventCmdComplete,
   1812                                                OMX_CommandStateSet,
   1813                                                pComponentPrivate->nCurState,
   1814                                                NULL);
   1815         } else {
   1816             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1817                                                    pComponentPrivate->pHandle->pApplicationPrivate,
   1818                                                    OMX_EventError,
   1819                                                    OMX_ErrorIncorrectStateTransition,
   1820                                                    OMX_TI_ErrorMinor,
   1821                                                    NULL);
   1822         }
   1823         break;
   1824 
   1825     case OMX_StatePause:
   1826         OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StatePause\n");
   1827 
   1828         pComponentPrivate->nToState = OMX_StatePause;
   1829 #ifdef __PERF_INSTRUMENTATION__
   1830                 PERF_Boundary(pComponentPrivate->pPERFcomp,
   1831                               PERF_BoundaryComplete | PERF_BoundarySteadyState);
   1832 #endif
   1833 
   1834         if ( pComponentPrivate->nCurState == OMX_StatePause ) {
   1835             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
   1836                                                    OMX_EventError, OMX_ErrorSameState, OMX_TI_ErrorMinor , NULL);
   1837         } else if ( pComponentPrivate->nCurState == OMX_StateExecuting || pComponentPrivate->nCurState == OMX_StateIdle ) {
   1838 
   1839             pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1840             eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlPause,NULL);
   1841             if ( eError != OMX_ErrorNone ) {
   1842                 OMX_PRDSP4(pComponentPrivate->dbg, "Error during EMMCodecControlPause. Error: %d.\n", eError );
   1843                 goto EXIT;
   1844             }
   1845             /*
   1846             pComponentPrivate->nCurState = OMX_StatePause; */
   1847             /* Decrement reference count with signal enabled */
   1848             if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1849                    return OMX_ErrorUndefined;
   1850            }
   1851             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
   1852                                                    OMX_EventCmdComplete, OMX_CommandStateSet, pComponentPrivate->nCurState, NULL);
   1853         } else {
   1854             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
   1855                                                    OMX_EventError, OMX_ErrorIncorrectStateTransition, OMX_TI_ErrorMinor , NULL);
   1856             OMX_PRSTATE4(pComponentPrivate->dbg, "Error: Invalid State Given by Application\n");
   1857         }
   1858         break;
   1859 
   1860 
   1861     case OMX_StateInvalid:
   1862         OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateInvalid::\n");
   1863         if ( pComponentPrivate->nCurState == OMX_StateInvalid ) {
   1864             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle, pComponentPrivate->pHandle->pApplicationPrivate,
   1865                                                    OMX_EventError, OMX_ErrorSameState, OMX_TI_ErrorMinor , NULL);
   1866         }
   1867         if ( pComponentPrivate->nCurState != OMX_StateLoaded ) {
   1868             OMX_PRBUFFER2(pComponentPrivate->dbg, "HandleJpegEncInternalFlush \n");
   1869             eError = HandleJpegEncInternalFlush(pComponentPrivate, nParam1);
   1870             }
   1871 
   1872             pComponentPrivate->nCurState = OMX_StateInvalid;
   1873 
   1874             if(pComponentPrivate->nToState == OMX_StateInvalid){ /*if the IL client call directly send to invalid state*/
   1875                 pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1876                                        pComponentPrivate->pHandle->pApplicationPrivate,
   1877                                        OMX_EventCmdComplete,
   1878                                        OMX_CommandStateSet,
   1879                                        pComponentPrivate->nCurState,
   1880                                        NULL);
   1881             }
   1882             else{ /*When the component go to invalid state by it self*/
   1883                 pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1884                                        pComponentPrivate->pHandle->pApplicationPrivate,
   1885                                        OMX_EventError,
   1886                                        OMX_ErrorInvalidState,
   1887                                        OMX_TI_ErrorSevere,
   1888                                        NULL);
   1889             }
   1890         break;
   1891 
   1892     case OMX_StateLoaded:
   1893         if ( pComponentPrivate->nCurState == OMX_StateLoaded ) {
   1894             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1895                                                    pComponentPrivate->pHandle->pApplicationPrivate,
   1896                                                    OMX_EventError,
   1897                                                    OMX_ErrorSameState,
   1898                                                    OMX_TI_ErrorMinor ,
   1899                                                    NULL);
   1900 
   1901         } else if ( pComponentPrivate->nCurState == OMX_StateIdle ||
   1902                     pComponentPrivate->nCurState == OMX_StateWaitForResources ) {
   1903             /* Ports have to be unpopulated before transition completes */
   1904             OMX_PRSTATE2(pComponentPrivate->dbg, "from idle to loaded\n");
   1905 
   1906             pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1907             if ( pComponentPrivate->pLCML != NULL && pComponentPrivate->isLCMLActive) {
   1908                 pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1909                 OMX_PRDSP2(pComponentPrivate->dbg, "try to close library again %p\n", pComponentPrivate->pLCML);
   1910                 LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlDestroy,NULL);
   1911                 OMX_PRDSP2(pComponentPrivate->dbg, "after close library again %p\n", pComponentPrivate->pLCML);
   1912                 pComponentPrivate->pLCML = NULL;
   1913                 dlclose(pComponentPrivate->pDllHandle);
   1914                 pComponentPrivate->isLCMLActive = 0;
   1915 
   1916             }
   1917             OMX_PRDSP2(pComponentPrivate->dbg, "after release LCML\n");
   1918 #ifdef __PERF_INSTRUMENTATION__
   1919             PERF_Boundary(pComponentPrivate->pPERFcomp,
   1920                           PERF_BoundaryStart | PERF_BoundaryCleanup);
   1921 #endif
   1922 
   1923 #ifdef UNDER_CE
   1924             nTimeout=0x0;
   1925 
   1926                 while ( 1 )
   1927                 {
   1928                     if ( (pPortDefOut->bPopulated == OMX_FALSE) )
   1929                     {
   1930                         OMX_PRDSP2(pComponentPrivate->dbg, "Thread Sending Cmd EMMCodecControlDestroy\n");
   1931 
   1932 
   1933                         break;
   1934                     } else if ( nTimeout++ > JPEGENC_TIMEOUT )
   1935 
   1936                     {
   1937                         OMX_PRBUFFER4(pComponentPrivate->dbg, "Timeout ...\n");
   1938                         pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1939                                        pComponentPrivate->pHandle->pApplicationPrivate,
   1940                                        OMX_EventError,
   1941                                        OMX_ErrorPortUnresponsiveDuringAllocation,
   1942                                        OMX_TI_ErrorMajor,
   1943                                        "Not response Port -Loaded");
   1944                         OMX_PRBUFFER4(pComponentPrivate->dbg, "Not all ports are unpopulated!\n");
   1945                         break;
   1946                     }
   1947                     /* Sleep for a while, so the application thread can allocate buffers */
   1948             sched_yield();
   1949             }
   1950 #else
   1951 
   1952             pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
   1953             while ( pPortDefIn->bPopulated || pPortDefOut->bPopulated) {
   1954                 OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
   1955                 pthread_cond_wait(&pComponentPrivate->unpopulate_cond, &pComponentPrivate->jpege_mutex_app);
   1956             }
   1957             pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
   1958 #endif
   1959 
   1960 #ifdef __PERF_INSTRUMENTATION__
   1961                         PERF_Boundary(pComponentPrivate->pPERFcomp,
   1962                                       PERF_BoundaryComplete | PERF_BoundaryCleanup);
   1963 #endif
   1964 
   1965 #ifdef RESOURCE_MANAGER_ENABLED
   1966             if (pComponentPrivate->nCurState != OMX_StateWaitForResources) {
   1967                 eError= RMProxy_NewSendCommand(pHandle,  RMProxy_FreeResource, OMX_JPEG_Encoder_COMPONENT, 0, 3456, NULL);
   1968                 if (eError != OMX_ErrorNone) {
   1969                     OMX_PRMGR4(pComponentPrivate->dbg, "Cannot Free Resources\n");
   1970                     pComponentPrivate->cbInfo.EventHandler(pHandle,
   1971                                                            pHandle->pApplicationPrivate,
   1972                                                            OMX_EventError,
   1973                                                            OMX_ErrorUndefined,
   1974                                                            OMX_TI_ErrorMajor,
   1975                                                            NULL);
   1976                     break;
   1977                 }
   1978             }
   1979 #endif
   1980 
   1981             pComponentPrivate->nCurState = OMX_StateLoaded;
   1982 
   1983             if ((pComponentPrivate->nCurState == OMX_StateIdle) &&
   1984                  (pComponentPrivate->bPreempted == 1 )){
   1985 
   1986                 pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1987                                                        pComponentPrivate->pHandle->pApplicationPrivate,
   1988                                                        OMX_EventError,
   1989                                                        OMX_ErrorResourcesLost,
   1990                                                        OMX_TI_ErrorSevere,
   1991                                                        NULL);
   1992                 pComponentPrivate->bPreempted = 0;
   1993 
   1994             }
   1995             else {
   1996                 /* Decrement reference count with signal enabled */
   1997                 if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1998                   return OMX_ErrorUndefined;
   1999                 }
   2000                 pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   2001                                                        pComponentPrivate->pHandle->pApplicationPrivate,
   2002                                                        OMX_EventCmdComplete,
   2003                                                        OMX_CommandStateSet,
   2004                                                        OMX_StateLoaded,
   2005                                                        NULL);
   2006             }
   2007 
   2008         }
   2009         else {
   2010 
   2011             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   2012                                                    pComponentPrivate->pHandle->pApplicationPrivate,
   2013                                                    OMX_EventError,
   2014                                                    OMX_ErrorIncorrectStateTransition,
   2015                                                    OMX_TI_ErrorMinor,
   2016                                                    NULL);
   2017         }
   2018         break;
   2019 
   2020     case OMX_StateWaitForResources:
   2021         if ( pComponentPrivate->nCurState == OMX_StateWaitForResources ) {
   2022             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   2023                                                                             pComponentPrivate->pHandle->pApplicationPrivate,
   2024                                                                             OMX_EventError,
   2025                                                                             OMX_ErrorSameState,
   2026                                                                             OMX_TI_ErrorMinor,
   2027                                                                             NULL);
   2028         } else if ( pComponentPrivate->nCurState == OMX_StateLoaded ) {
   2029 
   2030 #ifdef RESOURCE_MANAGER_ENABLED
   2031             eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateWaitForResources, 3456, NULL);
   2032             if (eError != OMX_ErrorNone) {
   2033                 OMX_PRMGR4(pComponentPrivate->dbg, "RMProxy_NewSendCommand(OMX_StateWaitForResources) failed\n");
   2034                 pComponentPrivate->cbInfo.EventHandler(pHandle,
   2035                                                        pHandle->pApplicationPrivate,
   2036                                                        OMX_EventError,
   2037                                                        OMX_ErrorUndefined,
   2038                                                        OMX_TI_ErrorSevere,
   2039                                                        NULL);
   2040                 break;
   2041             }
   2042 #endif
   2043 
   2044             pComponentPrivate->nCurState = OMX_StateWaitForResources;
   2045             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   2046                                                                             pComponentPrivate->pHandle->pApplicationPrivate,
   2047                                                                             OMX_EventCmdComplete,
   2048                                                                             OMX_CommandStateSet,
   2049                                                                             pComponentPrivate->nCurState,
   2050                                                                             NULL);
   2051         } else {
   2052             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   2053                                                                             pComponentPrivate->pHandle->pApplicationPrivate,
   2054                                                                             OMX_EventError,
   2055                                                                             OMX_ErrorIncorrectStateTransition,
   2056                                                                             OMX_TI_ErrorMinor,
   2057                                                                             NULL);
   2058         }
   2059         break;
   2060 
   2061     case OMX_StateMax:
   2062         OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateMax::\n");
   2063         break;
   2064     } /* End of Switch */
   2065 
   2066 
   2067     EXIT:
   2068     return eError;
   2069 }
   2070 
   2071 
   2072 OMX_ERRORTYPE HandleJpegEncFreeOutputBufferFromApp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate )
   2073 {
   2074 
   2075     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2076     OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
   2077     /* IUALG_Buf *ptParam = NULL; */
   2078     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   2079     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   2080     JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
   2081     int ret;
   2082 
   2083     OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleFreeOutputBufferFromApp function\n");
   2084 
   2085     pLcmlHandle = (LCML_DSP_INTERFACE *)(pComponentPrivate->pLCML);
   2086     pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
   2087 
   2088     ret = read(pComponentPrivate->free_outBuf_Q[0], &pBuffHead, sizeof(pBuffHead));
   2089     if ( ret == -1 ) {
   2090         OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
   2091         goto EXIT;
   2092     }
   2093 
   2094     if (pBuffHead != NULL) {
   2095         pBuffPrivate = pBuffHead->pOutputPortPrivate;
   2096     } else {
   2097         OMX_PRBUFFER4(pComponentPrivate->dbg, "Error while reading pBuffHead from the pipe\n");
   2098         goto EXIT;
   2099     }
   2100 
   2101     if (pComponentPrivate->nCurState != OMX_StatePause || pComponentPrivate->nToState != OMX_StatePause) {
   2102 
   2103    if ((pComponentPrivate->nCurState != OMX_StateExecuting ) ||
   2104        (pComponentPrivate->nToState == OMX_StateIdle) ||
   2105          (pPortDefOut->bEnabled == OMX_FALSE)) {
   2106          if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
   2107             pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   2108             pComponentPrivate->nOutPortOut ++;
   2109             OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
   2110                     pComponentPrivate->nInPortIn,
   2111                     pComponentPrivate->nInPortOut,
   2112                     pComponentPrivate->nOutPortIn,
   2113                     pComponentPrivate->nOutPortOut);
   2114             OMX_PRBUFFER2(pComponentPrivate->dbg, "FillBufferDone (incorrect state %d) %p\n", pComponentPrivate->nCurState, pBuffHead);
   2115             pComponentPrivate->cbInfo.FillBufferDone(pComponentPrivate->pHandle,
   2116                     pComponentPrivate->pHandle->pApplicationPrivate,
   2117                     pBuffHead);
   2118          }
   2119         goto EXIT;
   2120     }
   2121     }
   2122 
   2123     OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (HandleJpegEncFreeOutputBufferFromApp) %lu %lu %lu %lu\n",
   2124                     pComponentPrivate->nInPortIn,
   2125                     pComponentPrivate->nInPortOut,
   2126                     pComponentPrivate->nOutPortIn,
   2127                     pComponentPrivate->nOutPortOut);
   2128 
   2129 #ifdef __PERF_INSTRUMENTATION__
   2130     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   2131                       pBuffHead->pBuffer,
   2132                       0,
   2133                       PERF_ModuleCommonLayer);
   2134 #endif
   2135 
   2136     /* ptParam =  (IUALG_Buf *)pBuffPrivate->pUALGParams; */
   2137     pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_DSP;
   2138 
   2139 #ifdef __JPEG_OMX_PPLIB_ENABLED__
   2140     if (pComponentPrivate->pOutParams != NULL)
   2141     {
   2142         pComponentPrivate->pOutParams = (OMX_U8*)pComponentPrivate->pOutParams - PADDING_128_BYTE;
   2143 	OMX_FREE(pComponentPrivate->pOutParams);
   2144     }
   2145     OMX_MALLOC(pComponentPrivate->pOutParams,sizeof(PPLIB_UALGRunTimeParam_t) + PADDING_256_BYTE);
   2146     pComponentPrivate->pOutParams = (OMX_U8*)pComponentPrivate->pOutParams + PADDING_128_BYTE;
   2147 
   2148     if (pComponentPrivate->pOutParams != NULL)
   2149     {
   2150     }
   2151     else
   2152     {
   2153         goto EXIT;
   2154     }
   2155 
   2156     eError = SendDynamicPPLibParam(pComponentPrivate,pComponentPrivate->pOutParams);
   2157        if (eError != OMX_ErrorNone ) {
   2158            goto EXIT;
   2159        }
   2160 
   2161 
   2162        eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   2163                                  EMMCodecOuputBuffer,
   2164                                  pBuffHead->pBuffer,
   2165                                  pPortDefOut->nBufferSize,
   2166                                  0,
   2167                                  (OMX_U8 *)pComponentPrivate->pOutParams,
   2168                                  sizeof(PPLIB_UALGRunTimeParam_t),
   2169                                  (OMX_U8 *)  pBuffHead);
   2170 
   2171 #else
   2172     eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   2173                               EMMCodecOuputBuffer,
   2174                               pBuffHead->pBuffer,
   2175                               pPortDefOut->nBufferSize,
   2176                               0,
   2177                               NULL,
   2178                               0,
   2179                               (OMX_U8 *)  pBuffHead);
   2180 #endif
   2181 
   2182     OMX_PRINT1(pComponentPrivate->dbg, "Error is %x\n",eError);
   2183 
   2184     EXIT:
   2185     return eError;
   2186 }
   2187 
   2188 OMX_U32 CalculateParamsSize (JPEGENC_COMPONENT_PRIVATE *pComponentPrivate) {
   2189 
   2190     OMX_U32 i = 0;
   2191 
   2192     i+= 4; /* 4 bytes for the size of the whole array */
   2193 
   2194     /* Set Custom Quantization Table */
   2195     if (pComponentPrivate->bSetLumaQuantizationTable && pComponentPrivate->bSetChromaQuantizationTable) {
   2196         i+=4;  /* 4 bytes for the Quantization table TAG */
   2197         i+=4;  /* 4 bytes for the size of this TAG */
   2198         i+=256; /* 256 bytes for the Quantization table data*/
   2199     }
   2200 
   2201     /* Set Custom Huffman Table */
   2202     if (pComponentPrivate->bSetHuffmanTable) {
   2203         i+=4;  /* 4 bytes for the Huffman table TAG */
   2204         i+=4;  /* 4 bytes for the size of this TAG */
   2205         if (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4) {
   2206             i += (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) + (4 - (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4)));
   2207         }
   2208         else {
   2209             i += sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE);
   2210         }
   2211     }
   2212 
   2213     /* handle APP0 marker (JFIF)*/
   2214     if(pComponentPrivate->sAPP0.bMarkerEnabled) {
   2215         i+=4;  /* 4 bytes for the Number of buffers TAG */
   2216         i+=4;  /* 4 bytes for the size of this TAG */
   2217         i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
   2218         i+=4;  /* 4 bytes for the buffer0  TAG */
   2219 
   2220         /* if thumbnail is set, or if explicity specified by application, set the marker from algo, otherwise set it from application */
   2221         if ((pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0)
   2222         || pComponentPrivate->sAPP0.nMarkerSize <= 0) {
   2223             i+=4;   /* 4 bytes for the size of this TAG */
   2224             i+=4;   /* 4 bytes for the actual data of this TAG */
   2225         }
   2226         else {
   2227             i+=4;   /* 4 bytes for the size of this TAG */
   2228             i += (pComponentPrivate->sAPP0.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
   2229             if (pComponentPrivate->sAPP0.nMarkerSize % 4) {
   2230                 i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
   2231             }
   2232         }
   2233         /* if thumbnail is set, configure it accordingly */
   2234         if (pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0) {
   2235             i+=4;  /* 4 bytes for the THUMB INDEX TAG */
   2236             i+=4;  /* 4 bytes for the size of this TAG */
   2237             i+=4;  /* 4 bytes for the actual data for this TAG */
   2238 
   2239             i+=4;  /* 4 bytes for the THUMB W TAG */
   2240             i+=4;  /* 4 bytes for the size of this TAG */
   2241             i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
   2242 
   2243             i+=4;  /* 4 bytes for the THUMB H TAG */
   2244             i+=4;  /* 4 bytes for the size of this TAG */
   2245             i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
   2246         }
   2247     }
   2248 
   2249     /* handle APP1 marker (EXIF)*/
   2250     if (pComponentPrivate->sAPP1.bMarkerEnabled) {
   2251         i+=4;  /* 4 bytes for the Number of buffers TAG */
   2252         i+=4;  /* 4 bytes for the size of this TAG */
   2253         i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
   2254         i+=4;  /* 4 bytes for the buffer0  TAG */
   2255 
   2256     	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
   2257     	if (pComponentPrivate->sAPP1.nMarkerSize <= 0) {
   2258             i+=4;   /* 4 bytes for the size of this TAG */
   2259             i+=4;   /* 4 bytes for the actual data of this TAG */
   2260             i+=4;   /* 4 bytes for the actual data of this TAG */
   2261     	}
   2262     	else {
   2263     	    i+=4;   /* 4 bytes for the size of this TAG */
   2264                 i += (pComponentPrivate->sAPP1.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
   2265                 if (pComponentPrivate->sAPP1.nMarkerSize % 4) {
   2266                     i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
   2267                 }
   2268     	}
   2269 
   2270     	/* if thumbnail is set, configure it accordingly */
   2271     	if (pComponentPrivate->sAPP1.nThumbnailWidth > 0 && pComponentPrivate->sAPP1.nThumbnailHeight > 0) {
   2272             i+=4;  /* 4 bytes for the THUMB INDEX TAG */
   2273             i+=4;  /* 4 bytes for the size of this TAG */
   2274             i+=4;  /* 4 bytes for the actual data for this TAG */
   2275 
   2276             i+=4;  /* 4 bytes for the THUMB W TAG */
   2277             i+=4;  /* 4 bytes for the size of this TAG */
   2278             i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
   2279 
   2280             i+=4;  /* 4 bytes for the THUMB H TAG */
   2281             i+=4;  /* 4 bytes for the size of this TAG */
   2282             i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
   2283     	}
   2284     }
   2285 
   2286 
   2287     /* handle APP5 marker */
   2288     if (pComponentPrivate->sAPP5.bMarkerEnabled) {
   2289         i+=4;  /* 4 bytes for the Number of buffers TAG */
   2290         i+=4;  /* 4 bytes for the size of this TAG */
   2291         i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
   2292         i+=4;  /* 4 bytes for the buffer0  TAG */
   2293 
   2294     	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
   2295     	if (pComponentPrivate->sAPP5.nMarkerSize <= 0) {
   2296             i+=4;   /* 4 bytes for the size of this TAG */
   2297             i+=4;   /* 4 bytes for the actual data of this TAG */
   2298             i+=4;   /* 4 bytes for the actual data of this TAG */
   2299     	}
   2300     	else {
   2301     	    i+=4;   /* 4 bytes for the size of this TAG */
   2302                 i += (pComponentPrivate->sAPP5.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
   2303                 if (pComponentPrivate->sAPP5.nMarkerSize % 4) {
   2304                     i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
   2305                 }
   2306     	}
   2307 
   2308     	/* if thumbnail is set, configure it accordingly */
   2309     	if (pComponentPrivate->sAPP5.nThumbnailWidth > 0 && pComponentPrivate->sAPP5.nThumbnailHeight > 0) {
   2310             i+=4;  /* 4 bytes for the THUMB INDEX TAG */
   2311             i+=4;  /* 4 bytes for the size of this TAG */
   2312             i+=4;  /* 4 bytes for the actual data for this TAG */
   2313 
   2314             i+=4;  /* 4 bytes for the THUMB W TAG */
   2315             i+=4;  /* 4 bytes for the size of this TAG */
   2316             i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
   2317 
   2318             i+=4;  /* 4 bytes for the THUMB H TAG */
   2319             i+=4;  /* 4 bytes for the size of this TAG */
   2320             i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
   2321     	}
   2322     }
   2323     /* handle APP13 marker */
   2324     if (pComponentPrivate->sAPP13.bMarkerEnabled) {
   2325         i+=4;  /* 4 bytes for the Number of buffers TAG */
   2326         i+=4;  /* 4 bytes for the size of this TAG */
   2327         i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
   2328         i+=4;  /* 4 bytes for the buffer0  TAG */
   2329 
   2330     	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
   2331     	if (pComponentPrivate->sAPP13.nMarkerSize <= 0) {
   2332             i+=4;   /* 4 bytes for the size of this TAG */
   2333             i+=4;   /* 4 bytes for the actual data of this TAG */
   2334             i+=4;   /* 4 bytes for the actual data of this TAG */
   2335     	}
   2336     	else {
   2337             i+=4;   /* 4 bytes for the size of this TAG */
   2338             i += (pComponentPrivate->sAPP13.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
   2339             if (pComponentPrivate->sAPP13.nMarkerSize % 4) {
   2340                 i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
   2341             }
   2342     	}
   2343     }
   2344 
   2345     /* comment flag needed */
   2346     i+=4;   /* 4 bytes for the size of this TAG */
   2347 
   2348     /* handle CommentFlag */
   2349     if (pComponentPrivate->nCommentFlag == 1 && pComponentPrivate->pString_Comment) {
   2350         i+=4;  /* 4 bytes for the Comment TAG */
   2351         i+=4;  /* 4 bytes for the size of this TAG */
   2352             /* comment string upper limit is 256 bytes */
   2353             i+=256;
   2354     }
   2355     else {
   2356         i+=4;  /* 4 bytes for the Comment TAG */
   2357         i+=4;  /* 4 bytes for the size of this TAG */
   2358     }
   2359 
   2360     return i;
   2361 }
   2362 
   2363 static OMX_ERRORTYPE SetJpegEncInPortParams(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32* new_params)
   2364 {
   2365     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2366     int i = 1;
   2367 
   2368     /* Set Custom Quantization Table */
   2369     if (pComponentPrivate->bSetLumaQuantizationTable && pComponentPrivate->bSetChromaQuantizationTable) {
   2370         new_params[i++] = DYNPARAMS_QUANTTABLE;
   2371         new_params[i++] = 256; /* 2 tables * 64 entries * 2(16bit entries) */
   2372         OMX_U16 *temp = (OMX_U16 *)&new_params[i];
   2373         int j, k;
   2374         for (j = 0; j < 64; j++) {
   2375             temp[j] = pComponentPrivate->pCustomLumaQuantTable->nQuantizationMatrix[j];
   2376         }
   2377         for (k = 0; k < 64; k++, j++) {
   2378             temp[j] = pComponentPrivate->pCustomChromaQuantTable->nQuantizationMatrix[k];
   2379         }
   2380         i += 64; /* 256 / 4 */
   2381     }
   2382 
   2383     /* Set Custom Huffman Table */
   2384     if (pComponentPrivate->bSetHuffmanTable) {
   2385         new_params[i++] = DYNPARAMS_HUFFMANTABLE;
   2386         new_params[i++] = sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE); /* 2572 % 4 = 0 */
   2387 
   2388         memcpy((OMX_U8 *)(&new_params[i]), &(pComponentPrivate->pHuffmanTable->sHuffmanTable), sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE));
   2389         if (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4) {
   2390             i += (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) + (4 - (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4)))/4 ;
   2391         }
   2392         else {
   2393            i += sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE)/4;
   2394         }
   2395     }
   2396 
   2397     /* handle APP0 marker (JFIF)*/
   2398     if(pComponentPrivate->sAPP0.bMarkerEnabled) {
   2399         new_params[i++] = APP0_NUMBUF;
   2400         new_params[i++] = 4;
   2401         new_params[i++] = 1;
   2402 
   2403         /* set default APP0 BUFFER */
   2404         new_params[i++] = APP0_BUFFER;
   2405 
   2406         /* if thumbnail is set, or if explicity specified by application, set the marker from algo, otherwise set it from application */
   2407         if ((pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0)
   2408         	|| pComponentPrivate->sAPP0.nMarkerSize <= 0) {
   2409             new_params[i++] = 4;
   2410             new_params[i++] = 0;
   2411         }
   2412         else {
   2413             new_params[i++] = pComponentPrivate->sAPP0.nMarkerSize;
   2414             memcpy(new_params + i, pComponentPrivate->sAPP0.pMarkerBuffer, pComponentPrivate->sAPP0.nMarkerSize);
   2415             i += pComponentPrivate->sAPP0.nMarkerSize / 4;
   2416             if (pComponentPrivate->sAPP0.nMarkerSize % 4) {
   2417             	i ++;
   2418             }
   2419         }
   2420 
   2421         /* if thumbnail is set, configure it accordingly */
   2422         if (pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0) {
   2423             new_params[i++] = APP0_THUMB_INDEX;
   2424             new_params[i++] = 4;
   2425             new_params[i++] = 1;
   2426 
   2427             new_params[i++] = APP0_THUMB_W;
   2428             new_params[i++] = 4;
   2429             new_params[i++] = pComponentPrivate->sAPP0.nThumbnailWidth;
   2430 
   2431             new_params[i++] = APP0_THUMB_H;
   2432             new_params[i++] = 4;
   2433             new_params[i++] = pComponentPrivate->sAPP0.nThumbnailHeight;
   2434         }
   2435     }
   2436 
   2437     /* handle APP1 marker (EXIF)*/
   2438     if(pComponentPrivate->sAPP1.bMarkerEnabled) {
   2439         new_params[i++] = APP1_NUMBUF;
   2440         new_params[i++] = 4;
   2441         new_params[i++] = 1;
   2442 
   2443         /* set default APP1 BUFFER */
   2444         new_params[i++] = APP1_BUFFER;
   2445 
   2446         /* if explicity specified by application, set the marker from algo, otherwise set it from application */
   2447         if (pComponentPrivate->sAPP1.nMarkerSize <= 0) {
   2448             new_params[i++] = 8;
   2449             new_params[i++] = 0;
   2450             new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
   2451         }
   2452         else {
   2453             new_params[i++] = pComponentPrivate->sAPP1.nMarkerSize;
   2454             memcpy(new_params + i, pComponentPrivate->sAPP1.pMarkerBuffer, pComponentPrivate->sAPP1.nMarkerSize);
   2455             i += pComponentPrivate->sAPP1.nMarkerSize / 4;
   2456             if (pComponentPrivate->sAPP1.nMarkerSize % 4) {
   2457             	i ++;
   2458             }
   2459         }
   2460 
   2461         /* if thumbnail is set, configure it accordingly */
   2462         if (pComponentPrivate->sAPP1.nThumbnailWidth > 0 && pComponentPrivate->sAPP1.nThumbnailHeight > 0) {
   2463             new_params[i++] = APP1_THUMB_INDEX;
   2464             new_params[i++] = 4;
   2465             new_params[i++] = 1;
   2466 
   2467             new_params[i++] = APP1_THUMB_W;
   2468             new_params[i++] = 4;
   2469             new_params[i++] = pComponentPrivate->sAPP1.nThumbnailWidth;
   2470 
   2471             new_params[i++] = APP1_THUMB_H;
   2472             new_params[i++] = 4;
   2473             new_params[i++] = pComponentPrivate->sAPP1.nThumbnailHeight;
   2474         }
   2475     }
   2476 
   2477 
   2478     /* handle APP5 marker */
   2479     if(pComponentPrivate->sAPP5.bMarkerEnabled) {
   2480         new_params[i++] = APP5_NUMBUF;
   2481         new_params[i++] = 4;
   2482         new_params[i++] = 1;
   2483 
   2484         /* set default APP5 BUFFER */
   2485         new_params[i++] = APP5_BUFFER;
   2486 
   2487         /* if explicity specified by application, set the marker from algo, otherwise set it from application */
   2488         if (pComponentPrivate->sAPP5.nMarkerSize <= 0) {
   2489             new_params[i++] = 8;
   2490             new_params[i++] = 0;
   2491             new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
   2492         }
   2493         else {
   2494             new_params[i++] = pComponentPrivate->sAPP5.nMarkerSize;
   2495             memcpy(new_params + i, pComponentPrivate->sAPP5.pMarkerBuffer, pComponentPrivate->sAPP5.nMarkerSize);
   2496             i += pComponentPrivate->sAPP5.nMarkerSize / 4;
   2497             if (pComponentPrivate->sAPP5.nMarkerSize % 4) {
   2498             	i ++;
   2499             }
   2500         }
   2501 
   2502         /* if thumbnail is set, configure it accordingly */
   2503         if (pComponentPrivate->sAPP5.nThumbnailWidth > 0 && pComponentPrivate->sAPP5.nThumbnailHeight > 0) {
   2504             new_params[i++] = APP5_THUMB_INDEX;
   2505             new_params[i++] = 4;
   2506             new_params[i++] = 1;
   2507 
   2508             new_params[i++] = APP5_THUMB_W;
   2509             new_params[i++] = 4;
   2510             new_params[i++] = pComponentPrivate->sAPP5.nThumbnailWidth;
   2511 
   2512             new_params[i++] = APP5_THUMB_H;
   2513             new_params[i++] = 4;
   2514             new_params[i++] = pComponentPrivate->sAPP5.nThumbnailHeight;
   2515         }
   2516     }
   2517 
   2518 
   2519     /* handle APP13 marker */
   2520     if(pComponentPrivate->sAPP13.bMarkerEnabled) {
   2521         new_params[i++] = APP13_NUMBUF;
   2522         new_params[i++] = 4;
   2523         new_params[i++] = 1;
   2524 
   2525         /* set default APP13 BUFFER */
   2526         new_params[i++] = APP13_BUFFER;
   2527 
   2528         /* if explicity specified by application, set the marker from algo, otherwise set it from application */
   2529         if (pComponentPrivate->sAPP13.nMarkerSize <= 0) {
   2530             new_params[i++] = 8;
   2531             new_params[i++] = 0;
   2532             new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
   2533         }
   2534         else {
   2535             new_params[i++] = pComponentPrivate->sAPP13.nMarkerSize;
   2536             memcpy(new_params + i, pComponentPrivate->sAPP13.pMarkerBuffer, pComponentPrivate->sAPP13.nMarkerSize);
   2537             i += pComponentPrivate->sAPP13.nMarkerSize / 4;
   2538             if (pComponentPrivate->sAPP13.nMarkerSize % 4) {
   2539             	i ++;
   2540             }
   2541         }
   2542     }
   2543 
   2544     new_params[i++] = COMMENT_BUFFER;
   2545 
   2546     /* handle CommentFlag */
   2547     if (pComponentPrivate->nCommentFlag == 1 && pComponentPrivate->pString_Comment) {
   2548         new_params[i++] = strlen((char *)pComponentPrivate->pString_Comment)  + 4 ;
   2549         new_params[i++] = 0;
   2550         strncpy((char *)(new_params+i), (char *)pComponentPrivate->pString_Comment, 255);
   2551     }
   2552     else {
   2553         new_params[i++] = 4;
   2554         new_params[i++] = 0;
   2555     }
   2556 
   2557     /* now that we know the final size of the buffer, we can set it accordingly */
   2558     new_params[0] = i * sizeof(OMX_U32);
   2559 
   2560 /*
   2561     printf("=========DUMP of new_params array=============\n");
   2562     int j;
   2563     for (j=0; j< i*4; j++) {
   2564     printf("new_params[%d] = 0x%x\n", j, *(((unsigned char *)new_params)+j));
   2565     }
   2566     printf("================================\n");
   2567 */
   2568     return eError;
   2569 }
   2570 
   2571 OMX_ERRORTYPE SetJpegEncInParams(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate)
   2572 {
   2573     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2574     OMX_U8 *p = NULL;
   2575     OMX_U32 params_size;
   2576 
   2577     if (pComponentPrivate->InParams.pInParams) {
   2578         p = (OMX_U8 *)pComponentPrivate->InParams.pInParams;
   2579         p -= 128;
   2580         OMX_FREE(p);
   2581         pComponentPrivate->InParams.pInParams = NULL;
   2582     }
   2583 
   2584    /*alloc enough memory for params array*/
   2585     params_size = CalculateParamsSize(pComponentPrivate);
   2586     OMX_MALLOC(p, params_size + 256);
   2587     p += 128;
   2588     pComponentPrivate->InParams.pInParams = (OMX_U32 *)p;
   2589     p = NULL;
   2590     eError = SetJpegEncInPortParams(pComponentPrivate, pComponentPrivate->InParams.pInParams);
   2591 
   2592 EXIT:
   2593     return eError;
   2594 }
   2595 
   2596 OMX_ERRORTYPE HandleJpegEncDataBuf_FromApp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate )
   2597 {
   2598     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2599     OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
   2600     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   2601     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   2602     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   2603     JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
   2604     int ret;
   2605 
   2606     OMX_CHECK_PARAM(pComponentPrivate);
   2607 
   2608     pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   2609     pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
   2610     pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
   2611 
   2612     OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleDataBuf_FromApp function\n");
   2613     ret = read(pComponentPrivate->filled_inpBuf_Q[0], &(pBuffHead), sizeof(pBuffHead));
   2614     if ( ret == -1 ) {
   2615         OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
   2616     }
   2617 
   2618     if (pBuffHead != NULL) {
   2619         pBuffPrivate = pBuffHead->pInputPortPrivate;
   2620     } else {
   2621         eError = OMX_ErrorInsufficientResources;
   2622           goto EXIT;
   2623         }
   2624 
   2625    if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
   2626         /* already returned to client */
   2627         OMX_PRBUFFER4(pComponentPrivate->dbg, "this buffer %p already returned to client\n", pBuffHead);
   2628         goto EXIT;
   2629     }
   2630 
   2631    if ((pComponentPrivate->nCurState != OMX_StateExecuting) ||
   2632        (pComponentPrivate->nToState == OMX_StateIdle) ||
   2633         (pPortDefIn->bEnabled == OMX_FALSE)) {
   2634             if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
   2635                 pComponentPrivate->nInPortOut ++;
   2636                 pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   2637                 OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty input buffer) %lu %lu %lu %lu\n",
   2638                     pComponentPrivate->nInPortIn,
   2639                     pComponentPrivate->nInPortOut,
   2640                     pComponentPrivate->nOutPortIn,
   2641                     pComponentPrivate->nOutPortOut);
   2642                 pComponentPrivate->cbInfo.EmptyBufferDone(
   2643                            pComponentPrivate->pHandle,
   2644                            pComponentPrivate->pHandle->pApplicationPrivate,
   2645                            pBuffHead);
   2646              }
   2647              goto EXIT;
   2648     }
   2649 
   2650     OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (HandleJpegEncDataBuf_FromApp) %lu %lu %lu %lu\n",
   2651                     pComponentPrivate->nInPortIn,
   2652                     pComponentPrivate->nInPortOut,
   2653                     pComponentPrivate->nOutPortIn,
   2654                     pComponentPrivate->nOutPortOut);
   2655 
   2656 #ifdef __PERF_INSTRUMENTATION__
   2657     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   2658                       pBuffHead->pBuffer,
   2659                       pBuffHead->nFilledLen,
   2660                       PERF_ModuleCommonLayer);
   2661 #endif
   2662 
   2663     if ((pBuffHead->nFlags == OMX_BUFFERFLAG_EOS) && (pBuffHead->nAllocLen == 0)) {
   2664             OMX_PRBUFFER2(pComponentPrivate->dbg, "BufferFlag Set!!\n");
   2665             pComponentPrivate->nFlags = OMX_BUFFERFLAG_EOS;
   2666         pBuffHead->nFlags = 0;
   2667     }
   2668 #if 0
   2669     eError = SendDynamicParam(pComponentPrivate);
   2670     if (eError != OMX_ErrorNone ) {
   2671             OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
   2672             goto EXIT;
   2673     }
   2674 #endif
   2675 
   2676     OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nAllocLen = %d\n",(int)pBuffHead->nAllocLen);
   2677     OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->pBuffer = %p\n",pBuffHead->pBuffer);
   2678     OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nFilledLen = %d\n",(int)pBuffHead->nFilledLen);
   2679     OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead = %p\n",pBuffHead);
   2680 
   2681     pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_DSP;
   2682 
   2683     OMX_PRDSP2(pComponentPrivate->dbg, "Input: before queue buffer %p\n", pBuffHead);
   2684         eError = LCML_QueueBuffer(
   2685                                   pLcmlHandle->pCodecinterfacehandle,
   2686                                   EMMCodecInputBuffer,
   2687                                   pBuffHead->pBuffer,
   2688                                   pPortDefIn->nBufferSize,
   2689                                   pBuffHead->nFilledLen,
   2690                                   (OMX_U8 *) pComponentPrivate->InParams.pInParams,
   2691                                   pComponentPrivate->InParams.pInParams[0],
   2692                                   (OMX_U8 *)pBuffHead);
   2693 
   2694     OMX_PRDSP2(pComponentPrivate->dbg, "Input: after queue buffer %p\n", pBuffHead);
   2695 
   2696     if ( eError ) {
   2697         eError = OMX_ErrorInsufficientResources;
   2698         OMX_PRDSP4(pComponentPrivate->dbg, "OMX_ErrorInsufficientResources\n");
   2699         goto EXIT;
   2700     }
   2701     OMX_PRINT1(pComponentPrivate->dbg, "Error is %x\n",eError);
   2702     EXIT:
   2703 
   2704     return eError;
   2705 }
   2706 
   2707 
   2708 OMX_ERRORTYPE HandleJpegEncDataBuf_FromDsp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE* pBuffHead)
   2709 {
   2710 
   2711     OMX_ERRORTYPE eError                 = OMX_ErrorNone;
   2712     OMX_BUFFERHEADERTYPE* pInpBuf        = NULL;
   2713     JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
   2714 
   2715     OMX_CHECK_PARAM(pComponentPrivate);
   2716 
   2717     pInpBuf = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[0]->pBufferHdr;
   2718 
   2719     pBuffPrivate = pBuffHead->pOutputPortPrivate;
   2720 
   2721         if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
   2722            OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer %p already at the client side\n", pBuffHead);
   2723            pComponentPrivate->nOutPortOut --;
   2724            OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (FromDsp escape return output buffer) %lu %lu %lu %lu\n",
   2725                                         pComponentPrivate->nInPortIn,
   2726                                         pComponentPrivate->nInPortOut,
   2727                                         pComponentPrivate->nOutPortIn,
   2728                                         pComponentPrivate->nOutPortOut);
   2729 
   2730            goto EXIT;
   2731         }
   2732 
   2733 #ifdef __PERF_INSTRUMENTATION__
   2734         PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   2735                           pBuffHead->pBuffer,
   2736                           pBuffHead->nFilledLen,
   2737                           PERF_ModuleHLMM);
   2738 #endif
   2739     pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   2740 
   2741         if (pBuffHead->pMarkData) {
   2742            OMX_PRBUFFER2(pComponentPrivate->dbg, "get Mark buffer %p %p %p\n", pBuffHead->pMarkData, pBuffHead->hMarkTargetComponent, pComponentPrivate->pHandle);
   2743         }
   2744 
   2745         if (pBuffHead->pMarkData && pBuffHead->hMarkTargetComponent == pComponentPrivate->pHandle) {
   2746            OMX_PRBUFFER2(pComponentPrivate->dbg, "send OMX_MarkEvent\n");
   2747            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   2748                                                 pComponentPrivate->pHandle->pApplicationPrivate,
   2749                                                 OMX_EventMark,
   2750                                                 JPEGENC_OUT_PORT,
   2751                                                 0,
   2752                                                 pBuffHead->pMarkData);
   2753         }
   2754 
   2755     OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
   2756                     pComponentPrivate->nInPortIn,
   2757                     pComponentPrivate->nInPortOut,
   2758                     pComponentPrivate->nOutPortIn,
   2759                     pComponentPrivate->nOutPortOut);
   2760     OMX_PRBUFFER1(pComponentPrivate->dbg, "Output: before fillbufferdone %p\n", pBuffHead);
   2761         pComponentPrivate->cbInfo.FillBufferDone(pComponentPrivate->pHandle,
   2762                         pComponentPrivate->pHandle->pApplicationPrivate,
   2763                         pBuffHead);
   2764 
   2765     if ( pComponentPrivate->nFlags & OMX_BUFFERFLAG_EOS )   {
   2766 
   2767         pBuffHead->nFlags |= OMX_BUFFERFLAG_EOS;
   2768         pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
   2769                                                 pComponentPrivate->pHandle->pApplicationPrivate,
   2770                                                 OMX_EventBufferFlag,
   2771                                                 JPEGENC_OUT_PORT,
   2772                                                 OMX_BUFFERFLAG_EOS,
   2773                                                 NULL);
   2774 
   2775         pComponentPrivate->nFlags = 0;
   2776     }
   2777 
   2778     EXIT:
   2779     return eError;
   2780 }
   2781 
   2782 
   2783 
   2784 OMX_ERRORTYPE HandleJpegEncFreeDataBuf( JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE* pBuffHead )
   2785 {
   2786     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2787     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   2788     JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
   2789     OMX_HANDLETYPE hTunnelComponent = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent;
   2790 
   2791     OMX_CHECK_PARAM(pComponentPrivate);
   2792     OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleFreeDataBuf function \n");
   2793     pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
   2794 
   2795     /* pBuffHead->nAllocLen = pPortDefIn->nBufferSize; */
   2796     pBuffPrivate = pBuffHead->pInputPortPrivate;
   2797 
   2798     OMX_PRCOMM2(pComponentPrivate->dbg, "hTunnelComponent = %p\n" ,hTunnelComponent );
   2799     OMX_PRINT1(pComponentPrivate->dbg, "pComponentPrivate->pHandle = %p\n",pComponentPrivate->pHandle);
   2800 
   2801     if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
   2802         OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer %p already at the client side\n", pBuffHead);
   2803         pComponentPrivate->nInPortOut --;
   2804         OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (FromDsp escape return input buffer) %lu %lu %lu %lu\n",
   2805                                         pComponentPrivate->nInPortIn,
   2806                                         pComponentPrivate->nInPortOut,
   2807                                         pComponentPrivate->nOutPortIn,
   2808                                         pComponentPrivate->nOutPortOut);
   2809 
   2810            goto EXIT;
   2811     }
   2812 
   2813     if(hTunnelComponent != NULL)
   2814     {
   2815 
   2816 #ifdef __PERF_INSTRUMENTATION__
   2817             PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   2818                               pBuffHead->pBuffer,
   2819                               0,
   2820                               PERF_ModuleLLMM);
   2821 #endif
   2822     pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_TUNNEL_COMPONENT;
   2823         eError = OMX_FillThisBuffer(hTunnelComponent, pBuffHead);
   2824 
   2825     }
   2826     else {
   2827 
   2828 #ifdef __PERF_INSTRUMENTATION__
   2829             PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   2830                               pBuffHead->pBuffer,
   2831                               0,
   2832                               PERF_ModuleHLMM);
   2833 #endif
   2834         pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
   2835         OMX_PRBUFFER2(pComponentPrivate->dbg, "before emptybufferdone in HandleJpegEncFreeDataBuf %p\n", pBuffHead);
   2836         pComponentPrivate->cbInfo.EmptyBufferDone(
   2837                  pComponentPrivate->pHandle,
   2838                  pComponentPrivate->pHandle->pApplicationPrivate,
   2839                  pBuffHead);
   2840     }
   2841 
   2842     EXIT:
   2843     return eError;
   2844 
   2845 }
   2846 
   2847 
   2848 /* -------------------------------------------------------------------*/
   2849 /**
   2850   *  Callback() function will be called LCML component to write the msg
   2851   *
   2852   * @param msgBuffer                 This buffer will be returned by the LCML
   2853   *
   2854   * @retval OMX_NoError              Success, ready to roll
   2855   *         OMX_Error_BadParameter   The input parameter pointer is null
   2856  **/
   2857 /*-------------------------------------------------------------------*/
   2858 OMX_ERRORTYPE JpegEncLCML_Callback (TUsnCodecEvent event,void * argsCb [10])
   2859 {
   2860     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2861     JPEGENC_BUFFER_PRIVATE *pBuffPrivate = NULL;
   2862     JPEG_PORT_TYPE *pPortType = NULL;
   2863     int i;
   2864 
   2865     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
   2866     OMX_COMPONENTTYPE *pHandle = NULL;
   2867 
   2868     if ( ((LCML_DSP_INTERFACE*)argsCb[6] ) != NULL ) {
   2869         pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)((LCML_DSP_INTERFACE*)argsCb[6])->pComponentPrivate;
   2870         OMX_CHECK_PARAM(pComponentPrivate);
   2871 
   2872         pHandle = (OMX_COMPONENTTYPE *)pComponentPrivate->pHandle;
   2873     }
   2874     else {
   2875         OMXDBG_PRINT(stderr, DSP, 5, 0, "wrong in LCML callback, exit\n");
   2876 	 goto EXIT;
   2877     }
   2878     OMX_PRDSP0(pComponentPrivate->dbg, "Event = %d\n", event);
   2879 
   2880     if ( event == EMMCodecBufferProcessed ) {
   2881     if ( (int)argsCb [0] == EMMCodecOuputBuffer ) {
   2882         OMX_BUFFERHEADERTYPE* pBuffHead = (OMX_BUFFERHEADERTYPE*)argsCb[7];
   2883         pBuffPrivate = pBuffHead->pOutputPortPrivate;
   2884 
   2885         pComponentPrivate->nOutPortOut ++;
   2886 #ifdef __PERF_INSTRUMENTATION__
   2887         PERF_ReceivedFrame(pComponentPrivate->pPERFcomp,
   2888                            PREF(pBuffHead,pBuffer),
   2889                            PREF(pBuffHead,nFilledLen),
   2890                            PERF_ModuleCommonLayer);
   2891 #endif
   2892         OMX_PRDSP1(pComponentPrivate->dbg, "argsCb[8] is %d\n", (int)(argsCb[8]));
   2893         pBuffHead->nFilledLen = (OMX_U32) argsCb[8];
   2894 
   2895         OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (LCML for output buffer %p) %lu %lu %lu %lu\n", pBuffHead,
   2896                     pComponentPrivate->nInPortIn,
   2897                     pComponentPrivate->nInPortOut,
   2898                     pComponentPrivate->nOutPortIn,
   2899                     pComponentPrivate->nOutPortOut);
   2900 
   2901         pPortType = pComponentPrivate->pCompPort[JPEGENC_INP_PORT];
   2902         for (i = 0; i < (int)(pPortType->pPortDef->nBufferCountActual); i ++) {
   2903             if (pComponentPrivate->nOutPortOut > 10) {
   2904                 OMX_PRBUFFER1(pComponentPrivate->dbg, "pPortType->sBufferFlagTrack[i].buffer_id %lu\n", pPortType->sBufferFlagTrack[i].buffer_id);
   2905             }
   2906             if (pPortType->sBufferFlagTrack[i].buffer_id == pComponentPrivate->nOutPortOut) {
   2907                 OMX_PRBUFFER1(pComponentPrivate->dbg, "output buffer %lu has flag %lx\n",
   2908                            pPortType->sBufferFlagTrack[i].buffer_id,
   2909                            pPortType->sBufferFlagTrack[i].flag);
   2910                 pBuffHead->nFlags = pPortType->sBufferFlagTrack[i].flag;
   2911                 pPortType->sBufferFlagTrack[i].flag = 0;
   2912                 pPortType->sBufferFlagTrack[i].buffer_id = 0xFFFFFFFF;
   2913                 break;
   2914             }
   2915         }
   2916         for (i = 0; i < (int)(pPortType->pPortDef->nBufferCountActual); i ++) {
   2917             if (pPortType->sBufferMarkTrack[i].buffer_id == pComponentPrivate->nInPortOut) {
   2918                 OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer ID %lu has mark (output port)\n", pPortType->sBufferMarkTrack[i].buffer_id);
   2919                 pBuffHead->pMarkData = pPortType->sBufferMarkTrack[i].pMarkData;
   2920                 pBuffHead->hMarkTargetComponent = pPortType->sBufferMarkTrack[i].hMarkTargetComponent;
   2921                 pPortType->sBufferMarkTrack[i].buffer_id = 0xFFFFFFFF;
   2922                 break;
   2923             }
   2924         }
   2925 
   2926         OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodec Args -> %x, %p\n", (int)argsCb[1] , (void *)(argsCb[5]));
   2927         if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
   2928             OMX_PRBUFFER2(pComponentPrivate->dbg, "return output buffer %p from LCML_Callback (%d)\n",
   2929                            pBuffHead,
   2930                            pBuffPrivate->eBufferOwner);
   2931             pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_OUT;
   2932             OMX_PRBUFFER2(pComponentPrivate->dbg, "LCML_Callback - Filled (output) Data from DSP %p\n", pBuffHead);
   2933             eError = HandleJpegEncDataBuf_FromDsp(pComponentPrivate, pBuffHead);
   2934         }
   2935     }
   2936 
   2937     if ((int) argsCb [0] == EMMCodecInputBuffer ) {
   2938         OMX_BUFFERHEADERTYPE* pBuffHead = (OMX_BUFFERHEADERTYPE*)argsCb[7];
   2939         pBuffPrivate = pBuffHead->pInputPortPrivate;
   2940 
   2941        pComponentPrivate->nInPortOut ++;
   2942         OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer summary (LCML for InputBuffer %p) %lu %lu %lu %lu\n", pBuffHead,
   2943                     pComponentPrivate->nInPortIn,
   2944                     pComponentPrivate->nInPortOut,
   2945                     pComponentPrivate->nOutPortIn,
   2946                     pComponentPrivate->nOutPortOut);
   2947 #ifdef __PERF_INSTRUMENTATION__
   2948 	PERF_ReceivedFrame(pComponentPrivate->pPERFcomp,
   2949 			   PREF(pBuffHead,pBuffer),
   2950 			   0,
   2951 			   PERF_ModuleCommonLayer);
   2952 #endif
   2953 	OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodec Args -> %x, %p\n", (int)argsCb[1] , (void *)(argsCb[5]));
   2954         if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
   2955             OMX_PRBUFFER2(pComponentPrivate->dbg, "return input buffer %p from LCML_Callback (%d)\n",
   2956                            pBuffHead,
   2957                            pBuffPrivate->eBufferOwner);
   2958             pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_OUT;
   2959             OMX_PRBUFFER2(pComponentPrivate->dbg, "LCML_Callback - Emptied (input) Data from DSP %p\n", pBuffHead);
   2960             eError = HandleJpegEncFreeDataBuf(pComponentPrivate, pBuffHead);
   2961         }
   2962     }
   2963     goto PRINT_EXIT;
   2964     } /* end     if ( event == EMMCodecBufferProcessed ) */
   2965 
   2966     if ( event == EMMCodecProcessingStoped ) {
   2967         OMX_PRDSP2(pComponentPrivate->dbg, "Entering To EMMCodecProcessingStoped \n");
   2968         OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (Stopped) %lu %lu %lu %lu\n",
   2969                     pComponentPrivate->nInPortIn,
   2970                     pComponentPrivate->nInPortOut,
   2971                     pComponentPrivate->nOutPortIn,
   2972                     pComponentPrivate->nOutPortOut);
   2973         pComponentPrivate->bDSPStopAck = OMX_TRUE;
   2974         OMX_PRSTATE2(pComponentPrivate->dbg, "to state is %d\n", pComponentPrivate->nToState);
   2975 
   2976 
   2977         /* if (pComponentPrivate->nToState == OMX_StateIdle) { */
   2978             pComponentPrivate->ExeToIdleFlag |= JPEGE_DSPSTOP;
   2979         /* } */
   2980 
   2981         OMX_TRACE1(pComponentPrivate->dbg, "before stop signal\n");
   2982 
   2983         pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
   2984         pthread_cond_signal(&pComponentPrivate->stop_cond);
   2985         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
   2986 
   2987         OMX_TRACE1(pComponentPrivate->dbg, "after stop signal\n");
   2988 
   2989         goto PRINT_EXIT;
   2990     }
   2991 
   2992     if ( event == EMMCodecDspError ) {
   2993 
   2994        OMX_PRDSP4(pComponentPrivate->dbg, "in EMMCodecDspError EMMCodec Args -> %x, %x\n", (int)argsCb[4] , (int)argsCb[5]);
   2995         if ((int)argsCb[4] == USN_ERR_PROCESS && (int)argsCb[5] == IUALG_ERR_INSUFF_BUFFER) {
   2996             OMX_PRDSP4(pComponentPrivate->dbg,
   2997                     "DSP Error. The allocated output buffer length is insufficient");
   2998            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   2999                                      pComponentPrivate->pHandle->pApplicationPrivate,
   3000                                      OMX_EventError,
   3001                                      OMX_ErrorInsufficientResources,
   3002                                      OMX_TI_ErrorCritical,
   3003                                      "The allocated output buffer length is insufficient");
   3004            goto PRINT_EXIT;
   3005        }
   3006        if ((int)argsCb[4] != 0x1 || (int)argsCb[5] != 0x500) {
   3007 		   if ((int)argsCb[5] == JPEGENC_THUMBNAIL_ABSENT_WARNING) {
   3008 	           OMX_PRDSP4(pComponentPrivate->dbg, "Thumbnail is not generated as it \
   3009 	                   exceeds 64K spec size limit for the given thumbnail resolution and yuv data\n");
   3010 	           pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   3011 	                                     pComponentPrivate->pHandle->pApplicationPrivate,
   3012 	                                     OMX_EventError,
   3013 	                                     OMX_ErrorUndefined,
   3014 	                                     OMX_TI_ErrorMinor,
   3015 	                                     "Thumbnail not generated as it exceeds 64K size limit set by spec");
   3016 		       goto EXIT;
   3017 		   }
   3018            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   3019                                      pComponentPrivate->pHandle->pApplicationPrivate,
   3020                                      OMX_EventError,
   3021                                      OMX_ErrorHardware,
   3022                                      OMX_TI_ErrorCritical,
   3023                                      NULL);
   3024 
   3025             pComponentPrivate->nCurState = OMX_StateInvalid;
   3026             pComponentPrivate->cbInfo.EventHandler(pHandle,
   3027                                                    pHandle->pApplicationPrivate,
   3028                                                    OMX_EventError,
   3029                                                    OMX_ErrorInvalidState,
   3030                                                    OMX_TI_ErrorCritical,
   3031                                                    "DSP Hardware Error");
   3032            goto PRINT_EXIT;
   3033        }
   3034 #ifdef DSP_MMU_FAULT_HANDLING
   3035         /* Cheking for MMU_fault */
   3036         if((argsCb[4] == (void *)NULL) && (argsCb[5] == (void*)NULL)) {
   3037             //JPEGENC_DPRINT("DSP MMU_Fault");
   3038             pComponentPrivate->nCurState = OMX_StateInvalid;
   3039             pComponentPrivate->cbInfo.EventHandler(pHandle,
   3040                                                    pHandle->pApplicationPrivate,
   3041                                                    OMX_EventError,
   3042                                                    OMX_ErrorInvalidState,
   3043                                                    OMX_TI_ErrorCritical,
   3044                                                    "DSP MMU FAULT");
   3045         }
   3046 #endif
   3047     }
   3048     if (event == EMMCodecInternalError) {
   3049         eError = OMX_ErrorHardware;
   3050         OMX_PRDSP4(pComponentPrivate->dbg, "JPEG-E: EMMCodecInternalError\n");
   3051         pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   3052                                                pComponentPrivate->pHandle->pApplicationPrivate,
   3053                                                OMX_EventError,
   3054                                                OMX_ErrorHardware,
   3055                                                OMX_TI_ErrorCritical,
   3056                                                NULL);
   3057         goto PRINT_EXIT;
   3058     }
   3059     if ( event == EMMCodecProcessingPaused ) {
   3060         OMX_PRDSP2(pComponentPrivate->dbg, "ENTERING TO EMMCodecProcessingPaused JPEG Encoder\n");
   3061         if (pComponentPrivate != NULL) {
   3062             pComponentPrivate->bDSPStopAck = OMX_TRUE;
   3063             pComponentPrivate->nCurState = OMX_StatePause;
   3064             /* Decrement reference count with signal enabled */
   3065             if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   3066                   return OMX_ErrorUndefined;
   3067             }
   3068             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
   3069                                                    OMX_EventCmdComplete, OMX_CommandStateSet, pComponentPrivate->nCurState, NULL);
   3070         }
   3071     }
   3072     if (event == EMMCodecStrmCtrlAck) {
   3073         OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodecStrmCtrlAck\n");
   3074         if ((int)argsCb [0] == USN_ERR_NONE) {
   3075             OMX_PRDSP2(pComponentPrivate->dbg, "Callback: no error\n");
   3076             pComponentPrivate->bFlushComplete = OMX_TRUE;
   3077             pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
   3078             pthread_cond_signal(&pComponentPrivate->flush_cond);
   3079             pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
   3080         }
   3081     }
   3082     if (event == EMMCodecAlgCtrlAck) {
   3083         OMX_PRDSP2(pComponentPrivate->dbg, "jpeg-enc: EMMCodecAlgCtrlAck\n");
   3084         pComponentPrivate->bAckFromSetStatus = 1;
   3085         /*
   3086         pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
   3087         pthread_cond_signal(&pComponentPrivate->control_cond);
   3088         pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
   3089         */
   3090     }
   3091 PRINT_EXIT:
   3092     OMX_PRDSP1(pComponentPrivate->dbg, "Exiting the LCML_Callback function\n");
   3093 EXIT:
   3094     return eError;
   3095 }
   3096 /*-------------------------------------------------------------------*/
   3097 /**
   3098   * IsTIOMXComponent()
   3099   *
   3100   * Check if the component is TI component.
   3101   *
   3102   * @param hTunneledComp Component Tunnel Pipe
   3103   *
   3104   * @retval OMX_TRUE   Input is a TI component.
   3105   *         OMX_FALSE  Input is a not a TI component.
   3106   *
   3107   **/
   3108 /*-------------------------------------------------------------------*/
   3109 OMX_BOOL IsTIOMXComponent(OMX_HANDLETYPE hComp)
   3110 {
   3111     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3112     OMX_STRING pTunnelcComponentName = NULL;
   3113     OMX_VERSIONTYPE* pTunnelComponentVersion = NULL;
   3114     OMX_VERSIONTYPE* pSpecVersion = NULL;
   3115     OMX_UUIDTYPE* pComponentUUID = NULL;
   3116     char *pSubstring = NULL;
   3117     OMX_BOOL bResult = OMX_TRUE;
   3118 
   3119     OMX_MALLOC(pTunnelcComponentName, 128);
   3120     OMX_MALLOC(pTunnelComponentVersion, sizeof(OMX_VERSIONTYPE));
   3121     OMX_MALLOC(pSpecVersion, sizeof(OMX_VERSIONTYPE));
   3122     OMX_MALLOC(pComponentUUID, sizeof(OMX_UUIDTYPE));
   3123 
   3124     eError = OMX_GetComponentVersion (hComp, pTunnelcComponentName, pTunnelComponentVersion, pSpecVersion, pComponentUUID);
   3125 
   3126     /* Check if tunneled component is a TI component */
   3127     pSubstring = strstr(pTunnelcComponentName, "OMX.TI.");
   3128     if(pSubstring == NULL) {
   3129         bResult = OMX_FALSE;
   3130     }
   3131 
   3132 EXIT:
   3133     OMX_FREE(pTunnelcComponentName);
   3134     OMX_FREE(pTunnelComponentVersion);
   3135     OMX_FREE(pSpecVersion);
   3136     OMX_FREE(pComponentUUID);
   3137     return bResult;
   3138 } /* End of IsTIOMXComponent */
   3139 
   3140 #ifdef RESOURCE_MANAGER_ENABLED
   3141 /* ========================================================================== */
   3142 /**
   3143  *  ResourceManagerCallback() - handle callbacks from Resource Manager
   3144  * @param cbData    Resource Manager Command Data Structure
   3145  * @return: void
   3146   **/
   3147 /* ========================================================================== */
   3148 
   3149 void ResourceManagerCallback(RMPROXY_COMMANDDATATYPE cbData)
   3150 {
   3151     OMX_COMMANDTYPE Cmd = OMX_CommandStateSet;
   3152     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)cbData.hComponent;
   3153     JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
   3154     OMX_ERRORTYPE RM_Error = *(cbData.RM_Error);
   3155 
   3156     pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
   3157     OMX_PRINT1(pComponentPrivate->dbg, "RM_Error = %x\n", RM_Error);
   3158 
   3159     if (RM_Error == OMX_RmProxyCallback_ResourcesPreempted) {
   3160 
   3161         pComponentPrivate->bPreempted = 1;
   3162 
   3163         if (pComponentPrivate->nCurState == OMX_StateExecuting ||
   3164             pComponentPrivate->nCurState == OMX_StatePause) {
   3165 
   3166             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   3167                                                    pComponentPrivate->pHandle->pApplicationPrivate,
   3168                                                    OMX_EventError,
   3169                                                    OMX_ErrorResourcesPreempted,
   3170                                                    OMX_TI_ErrorSevere,
   3171                                                    NULL);
   3172 
   3173             pComponentPrivate->nToState = OMX_StateIdle;
   3174             OMX_PRSTATE2(pComponentPrivate->dbg, "Component Preempted. Going to IDLE State.\n");
   3175         }
   3176         else if (pComponentPrivate->nCurState == OMX_StateIdle){
   3177             pComponentPrivate->nToState = OMX_StateLoaded;
   3178             OMX_PRSTATE2(pComponentPrivate->dbg, "Component Preempted. Going to LOADED State.\n");
   3179         }
   3180 
   3181 #ifdef __PERF_INSTRUMENTATION__
   3182         PERF_SendingCommand(pComponentPrivate->pPERF, Cmd, pComponentPrivate->nToState, PERF_ModuleComponent);
   3183 #endif
   3184 
   3185         write (pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
   3186         write (pComponentPrivate->nCmdDataPipe[1], &(pComponentPrivate->nToState) ,sizeof(OMX_U32));
   3187 
   3188     }
   3189     else if (RM_Error == OMX_RmProxyCallback_ResourcesAcquired ){
   3190 
   3191         if (pComponentPrivate->nCurState == OMX_StateWaitForResources) /* Wait for Resource Response */
   3192         {
   3193             pComponentPrivate->cbInfo.EventHandler (
   3194                             pHandle, pHandle->pApplicationPrivate,
   3195                             OMX_EventResourcesAcquired, 0,0,
   3196                             NULL);
   3197 
   3198             pComponentPrivate->nToState = OMX_StateIdle;
   3199 
   3200 #ifdef __PERF_INSTRUMENTATION__
   3201             PERF_SendingCommand(pComponentPrivate->pPERF, Cmd, pComponentPrivate->nToState, PERF_ModuleComponent);
   3202 #endif
   3203 
   3204             write (pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
   3205             write (pComponentPrivate->nCmdDataPipe[1], &(pComponentPrivate->nToState) ,sizeof(OMX_U32));
   3206             OMX_PRMGR2(pComponentPrivate->dbg, "OMX_RmProxyCallback_ResourcesAcquired.\n");
   3207         }
   3208 	}
   3209 	else if (RM_Error == OMX_RmProxyCallback_FatalError){
   3210 
   3211 		/* Deinitialize the component...no error should be returned from
   3212 		 * this function. It should clean the system as much as possible */
   3213 		if ( pComponentPrivate->pLCML != NULL && pComponentPrivate->isLCMLActive) {
   3214                 	LCML_ControlCodec(((LCML_DSP_INTERFACE*)pComponentPrivate->pLCML)->pCodecinterfacehandle, EMMCodecControlDestroy, NULL);
   3215                      	dlclose(pComponentPrivate->pDllHandle);
   3216                 	pComponentPrivate->pLCML = NULL;
   3217                 	pComponentPrivate->isLCMLActive = 0;
   3218 		}
   3219 
   3220 		pComponentPrivate->nCurState = OMX_StateInvalid;
   3221 		pComponentPrivate->cbInfo.EventHandler(pHandle,
   3222 											   pHandle->pApplicationPrivate,
   3223 											   OMX_EventError,
   3224 											   OMX_ErrorInvalidState,
   3225 											   OMX_TI_ErrorSevere,
   3226 											   NULL);
   3227 	}
   3228 }
   3229 #endif
   3230 
   3231 void LinkedList_Create(LinkedList *LinkedList) {
   3232     LinkedList->pRoot = NULL;
   3233     pthread_mutex_init(&LinkedList->lock, NULL);
   3234 }
   3235 
   3236 void LinkedList_AddElement(LinkedList *LinkedList, void *pValue) {
   3237     /* create new node and fill the value */
   3238     Node *pNewNode = (Node *)malloc(sizeof(Node));
   3239     if ( pNewNode != NULL ) {
   3240         pNewNode->pValue = (void *)pValue;
   3241         /*printf("LinkedList:::: Pointer=%p has been added.\n", pNewNode->pValue); */
   3242         /* add new node on the root to implement quick FIFO */
   3243         /* modify new node pointers */
   3244 
   3245         pthread_mutex_lock(&LinkedList->lock);
   3246         if(LinkedList->pRoot == NULL) {
   3247             pNewNode->pNextNode = NULL;
   3248         }
   3249         else {
   3250              pNewNode->pNextNode = LinkedList->pRoot;
   3251         }
   3252         /*modify root */
   3253         LinkedList->pRoot = pNewNode;
   3254         pthread_mutex_unlock(&LinkedList->lock);
   3255     }
   3256     else {
   3257          ALOGE("Linked list memory allocation failed.\n" );
   3258     }
   3259 }
   3260 
   3261 void LinkedList_FreeElement(LinkedList *LinkedList, void *pValue) {
   3262     pthread_mutex_lock(&LinkedList->lock);
   3263     Node *pNode = LinkedList->pRoot;
   3264     Node *pPastNode = NULL;
   3265     while (pNode != NULL) {
   3266         if (pNode->pValue == pValue) {
   3267             Node *pTempNode = pNode->pNextNode;
   3268             if(pPastNode == NULL) {
   3269                 LinkedList->pRoot = pTempNode;
   3270             }
   3271             else {
   3272                 pPastNode->pNextNode = pTempNode;
   3273             }
   3274             /*printf("LinkedList:::: Pointer=%p has been freed\n", pNode->pValue); */
   3275             free(pNode->pValue);
   3276             pNode->pValue = NULL;
   3277             free(pNode);
   3278             pNode = NULL;
   3279             break;
   3280         }
   3281         pPastNode = pNode;
   3282         pNode = pNode->pNextNode;
   3283     }
   3284     pthread_mutex_unlock(&LinkedList->lock);
   3285 }
   3286 
   3287 void LinkedList_FreeAll(LinkedList *LinkedList) {
   3288     Node *pTempNode;
   3289     int nodes = 0;
   3290     pthread_mutex_lock(&LinkedList->lock);
   3291     while (LinkedList->pRoot != NULL) {
   3292         pTempNode = LinkedList->pRoot->pNextNode;
   3293         /*printf("LinkedList:::: Pointer=%p has been freed\n", LinkedList->pRoot->pValue); */
   3294         if(LinkedList->pRoot->pValue != NULL) {
   3295 		free(LinkedList->pRoot->pValue);
   3296 		LinkedList->pRoot->pValue = NULL;
   3297 	}
   3298         free(LinkedList->pRoot);
   3299         LinkedList->pRoot = pTempNode;
   3300         nodes++;
   3301     }
   3302     pthread_mutex_unlock(&LinkedList->lock);
   3303     /*printf("==================No. of deleted nodes: %d=======================================\n\n", nodes); */
   3304 }
   3305 
   3306 void LinkedList_DisplayAll(LinkedList *LinkedList) {
   3307     Node *pNode = LinkedList->pRoot;
   3308     int nodes = 0;
   3309     printf("\n================== Displaying contents of linked list=%p=====================\n", LinkedList);
   3310     printf("root->\n");
   3311     while (pNode != NULL) {
   3312         printf("[Value=%p, NextNode=%p]->\n", pNode->pValue, pNode->pNextNode);
   3313         pNode = pNode->pNextNode;
   3314         nodes++;
   3315     }
   3316     printf("==================No. of existing nodes: %d=======================================\n\n", nodes);
   3317 }
   3318 
   3319 void LinkedList_Destroy(LinkedList *LinkedList) {
   3320     pthread_mutex_destroy(&LinkedList->lock);
   3321 }
   3322 
   3323 OMX_ERRORTYPE AddStateTransition(JPEGENC_COMPONENT_PRIVATE* pComponentPrivate) {
   3324 
   3325     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3326 
   3327     if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
   3328        return OMX_ErrorUndefined;
   3329     }
   3330 
   3331     /* Increment state change request reference count */
   3332     pComponentPrivate->nPendingStateChangeRequests++;
   3333 
   3334     if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
   3335        return OMX_ErrorUndefined;
   3336     }
   3337 
   3338     return eError;
   3339 }
   3340 
   3341 OMX_ERRORTYPE RemoveStateTransition(JPEGENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_BOOL bEnableSignal) {
   3342     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3343 
   3344      /* Decrement state change request reference count*/
   3345     if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
   3346        return OMX_ErrorUndefined;
   3347     }
   3348 
   3349     pComponentPrivate->nPendingStateChangeRequests--;
   3350 
   3351     /* If there are no more pending requests, signal the thread waiting on this*/
   3352     if(!pComponentPrivate->nPendingStateChangeRequests && bEnableSignal) {
   3353        pthread_cond_signal(&(pComponentPrivate->StateChangeCondition));
   3354     }
   3355 
   3356     if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
   3357        return OMX_ErrorUndefined;
   3358     }
   3359 
   3360     return eError;
   3361 }
   3362