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_AmrEnc_Utils.c
     30 *
     31 * This file implements NBAMR Encoder Component Specific APIs and its functionality
     32 * that is fully compliant with the Khronos OpenMAX (TM) 1.0 Specification
     33 *
     34 * @path  $(CSLPATH)\OMAPSW_MPU\linux\audio\src\openmax_il\nbamr_enc\src
     35 *
     36 * @rev  1.0
     37 */
     38 /* ----------------------------------------------------------------------------
     39 *!
     40 *! Revision History
     41 *! ===================================
     42 *! 21-sept-2006 bk: updated review findings for alpha release
     43 *! 24-Aug-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests some more
     44 *! 18-July-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests validated for few cases
     45 *! 21-Jun-2006 bk: Khronos OpenMAX (TM) 1.0 migration done
     46 *! 22-May-2006 bk: DASF recording quality improved
     47 *! 19-Apr-2006 bk: DASF recording speed issue resloved
     48 *! 23-Feb-2006 bk: DASF functionality added
     49 *! 18-Jan-2006 bk: Repated recording issue fixed and LCML changes taken care
     50 *! 14-Dec-2005 bk: Initial Version
     51 *! 16-Nov-2005 bk: Initial Version
     52 *! 23-Sept-2005 bk: Initial Version
     53 *! 10-Sept-2005 bk: Initial Version
     54 *! 10-Sept-2005 bk:
     55 *! This is newest file
     56 * =========================================================================== */
     57 
     58 /* ------compilation control switches -------------------------*/
     59 /****************************************************************
     60 *  INCLUDE FILES
     61 ****************************************************************/
     62 /* ----- system and platform files ----------------------------*/
     63 #ifdef UNDER_CE
     64 #include <windows.h>
     65 #include <oaf_osal.h>
     66 #include <omx_core.h>
     67 #else
     68 #include <wchar.h>
     69 #include <unistd.h>
     70 #include <sys/types.h>
     71 #include <sys/wait.h>
     72 #include <sys/types.h>
     73 #include <sys/stat.h>
     74 #include <dlfcn.h>
     75 #include <malloc.h>
     76 #include <memory.h>
     77 #include <fcntl.h>
     78 #include <errno.h>
     79 #endif
     80 
     81 #include <dbapi.h>
     82 #include <string.h>
     83 #include <stdio.h>
     84 #include <stdlib.h>
     85 /*-------program files ----------------------------------------*/
     86 #include "OMX_AmrEnc_Utils.h"
     87 #include "amrencsocket_ti.h"
     88 #include <encode_common_ti.h>
     89 #include "OMX_AmrEnc_ComponentThread.h"
     90 #include "usn.h"
     91 #include "LCML_DspCodec.h"
     92 
     93 #ifdef RESOURCE_MANAGER_ENABLED
     94     #include <ResourceManagerProxyAPI.h>
     95 #endif
     96 
     97 /* ========================================================================== */
     98 /**
     99 * @NBAMRENC_FillLCMLInitParams () This function is used by the component thread to
    100 * fill the all of its initialization parameters, buffer deatils  etc
    101 * to LCML structure,
    102 *
    103 * @param pComponent  handle for this instance of the component
    104 * @param plcml_Init  pointer to LCML structure to be filled
    105 *
    106 * @pre
    107 *
    108 * @post
    109 *
    110 * @return none
    111 */
    112 /* ========================================================================== */
    113 
    114 OMX_ERRORTYPE NBAMRENC_FillLCMLInitParams(OMX_HANDLETYPE pComponent,
    115                                   LCML_DSP *plcml_Init, OMX_U16 arr[])
    116 {
    117     OMX_ERRORTYPE eError = OMX_ErrorNone;
    118     OMX_U32 nIpBuf,nIpBufSize,nOpBuf,nOpBufSize;
    119     OMX_BUFFERHEADERTYPE *pTemp;
    120     LCML_DSP_INTERFACE *pHandle = (LCML_DSP_INTERFACE *)pComponent;
    121     AMRENC_COMPONENT_PRIVATE *pComponentPrivate = pHandle->pComponentPrivate;
    122     NBAMRENC_LCML_BUFHEADERTYPE *pTemp_lcml = NULL;
    123     OMX_U16 i;
    124     OMX_U32 size_lcml;
    125     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering NBAMRENC_FillLCMLInitParams\n",__LINE__);
    126     nIpBuf = pComponentPrivate->pInputBufferList->numBuffers;
    127     nIpBufSize = pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nBufferSize;
    128     pComponentPrivate->nRuntimeInputBuffers = nIpBuf;
    129     nOpBuf = pComponentPrivate->pOutputBufferList->numBuffers;
    130     nOpBufSize = pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nBufferSize;
    131     pComponentPrivate->nRuntimeOutputBuffers = nOpBuf;
    132     OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: ------ Buffer Details -----------\n",__LINE__);
    133     OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Input  Buffer Count = %ld\n",__LINE__,nIpBuf);
    134     OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Input  Buffer Size = %ld\n",__LINE__,nIpBufSize);
    135     OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Output Buffer Count = %ld\n",__LINE__,nOpBuf);
    136     OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Output Buffer Size = %ld\n",__LINE__,nOpBufSize);
    137     OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: ------ Buffer Details ------------\n",__LINE__);
    138     /* Fill Input Buffers Info for LCML */
    139     plcml_Init->In_BufInfo.nBuffers = nIpBuf;
    140     plcml_Init->In_BufInfo.nSize = nIpBufSize;
    141     plcml_Init->In_BufInfo.DataTrMethod = DMM_METHOD;
    142 
    143     /* Fill Output Buffers Info for LCML */
    144     plcml_Init->Out_BufInfo.nBuffers = nOpBuf;
    145     plcml_Init->Out_BufInfo.nSize = nOpBufSize;
    146     plcml_Init->Out_BufInfo.DataTrMethod = DMM_METHOD;
    147 
    148     /*Copy the node information*/
    149     plcml_Init->NodeInfo.nNumOfDLLs = 3;
    150 
    151     plcml_Init->NodeInfo.AllUUIDs[0].uuid = &AMRENCSOCKET_TI_UUID;
    152     strcpy ((char*)plcml_Init->NodeInfo.AllUUIDs[0].DllName,NBAMRENC_DLL_NAME);
    153     plcml_Init->NodeInfo.AllUUIDs[0].eDllType = DLL_NODEOBJECT;
    154 
    155     plcml_Init->NodeInfo.AllUUIDs[1].uuid = &AMRENCSOCKET_TI_UUID;
    156     strcpy ((char*)plcml_Init->NodeInfo.AllUUIDs[1].DllName,NBAMRENC_DLL_NAME);
    157     plcml_Init->NodeInfo.AllUUIDs[1].eDllType = DLL_DEPENDENT;
    158 
    159     plcml_Init->NodeInfo.AllUUIDs[2].uuid = &USN_TI_UUID;
    160     strcpy ((char*)plcml_Init->NodeInfo.AllUUIDs[2].DllName,NBAMRENC_USN_DLL_NAME);
    161     plcml_Init->NodeInfo.AllUUIDs[2].eDllType = DLL_DEPENDENT;
    162     plcml_Init->DeviceInfo.TypeofDevice = 0;
    163 
    164     if(pComponentPrivate->dasfMode == 1) {
    165         OMX_PRDSP2(pComponentPrivate->dbg, "%d :: Codec is configuring to DASF mode\n",__LINE__);
    166         OMX_MALLOC_GENERIC(pComponentPrivate->strmAttr, LCML_STRMATTR);
    167         pComponentPrivate->strmAttr->uSegid = NBAMRENC_DEFAULT_SEGMENT;
    168         pComponentPrivate->strmAttr->uAlignment = 0;
    169         pComponentPrivate->strmAttr->uTimeout = NBAMRENC_SN_TIMEOUT;
    170         pComponentPrivate->strmAttr->uBufsize = NBAMRENC_INPUT_BUFFER_SIZE_DASF;
    171         pComponentPrivate->strmAttr->uNumBufs = NBAMRENC_NUM_INPUT_BUFFERS_DASF;
    172         pComponentPrivate->strmAttr->lMode = STRMMODE_PROCCOPY;
    173         /* Device is Configuring to DASF Mode */
    174         plcml_Init->DeviceInfo.TypeofDevice = 1;
    175         /* Device is Configuring to Record Mode */
    176         plcml_Init->DeviceInfo.TypeofRender = 1;
    177 
    178         if(pComponentPrivate->acdnMode == 1) {
    179             /* ACDN mode */
    180             plcml_Init->DeviceInfo.AllUUIDs[0].uuid = &ACDN_TI_UUID;
    181         }
    182         else {
    183             /* DASF/TeeDN mode */
    184                 if(pComponentPrivate->teeMode!= TEEMODE_NONE) {
    185                     plcml_Init->DeviceInfo.AllUUIDs[0].uuid = &TEEDN_TI_UUID;
    186                 }
    187                 else {
    188                     plcml_Init->DeviceInfo.AllUUIDs[0].uuid = &DCTN_TI_UUID;
    189                 }
    190         }
    191         plcml_Init->DeviceInfo.DspStream = pComponentPrivate->strmAttr;
    192     }
    193 
    194     /*copy the other information*/
    195     plcml_Init->SegID = NBAMRENC_DEFAULT_SEGMENT;
    196     plcml_Init->Timeout = NBAMRENC_SN_TIMEOUT;
    197     plcml_Init->Alignment = 0;
    198     plcml_Init->Priority = NBAMRENC_SN_PRIORITY;
    199     plcml_Init->ProfileID = -1;
    200 
    201     /* Setting Creat Phase Parameters here */
    202     arr[0] = NBAMRENC_STREAM_COUNT;
    203     arr[1] = NBAMRENC_INPUT_PORT;
    204 
    205     if(pComponentPrivate->dasfMode == 1) {
    206         arr[2] = NBAMRENC_INSTRM;
    207         arr[3] = NBAMRENC_NUM_INPUT_BUFFERS_DASF;
    208     }
    209     else {
    210         arr[2] = NBAMRENC_DMM;
    211         if (pComponentPrivate->pInputBufferList->numBuffers) {
    212             arr[3] = (OMX_U16) pComponentPrivate->pInputBufferList->numBuffers;
    213         }
    214         else {
    215             arr[3] = 1;
    216         }
    217     }
    218 
    219     arr[4] = NBAMRENC_OUTPUT_PORT;
    220     arr[5] = NBAMRENC_DMM;
    221     if (pComponentPrivate->pOutputBufferList->numBuffers) {
    222         arr[6] = (OMX_U16) pComponentPrivate->pOutputBufferList->numBuffers;
    223     }
    224     else {
    225         arr[6] = 1;
    226     }
    227 
    228     if (pComponentPrivate->efrMode == 1) {
    229         OMX_PRDSP2(pComponentPrivate->dbg, "%d :: Codec is configuring to EFR mode\n",__LINE__);
    230         arr[7] = NBAMRENC_EFR;
    231     }
    232     else {
    233         OMX_PRDSP2(pComponentPrivate->dbg, "%d :: Codec is configuring to NBAMR mode\n",__LINE__);
    234         arr[7] = NBAMRENC_NBAMR;
    235     }
    236 
    237     if(pComponentPrivate->frameMode == NBAMRENC_MIMEMODE ) {
    238         OMX_PRDSP2(pComponentPrivate->dbg, "%d :: Codec is configuring MIME mode\n",__LINE__);
    239         arr[8] = NBAMRENC_MIMEMODE;
    240     }
    241     else if(pComponentPrivate->frameMode == NBAMRENC_IF2 ){
    242         OMX_PRDSP2(pComponentPrivate->dbg, "%d :: Codec is configuring IF2 mode\n",__LINE__);
    243         arr[8] = NBAMRENC_IF2;
    244         }
    245     else {
    246         OMX_PRDSP2(pComponentPrivate->dbg, "%d :: Codec is configuring FORMAT CONFORMANCE mode\n",__LINE__);
    247         arr[8] = NBAMRENC_FORMATCONFORMANCE;
    248     }
    249     arr[9] = END_OF_CR_PHASE_ARGS;
    250 
    251     plcml_Init->pCrPhArgs = arr;
    252 
    253     /* Allocate memory for all input buffer headers..
    254      * This memory pointer will be sent to LCML */
    255     size_lcml = nIpBuf * sizeof(NBAMRENC_LCML_BUFHEADERTYPE);
    256 
    257     OMX_MALLOC_SIZE(pTemp_lcml, size_lcml,NBAMRENC_LCML_BUFHEADERTYPE);
    258 
    259     pComponentPrivate->pLcmlBufHeader[NBAMRENC_INPUT_PORT] = pTemp_lcml;
    260     for (i=0; i<nIpBuf; i++) {
    261         OMX_PRCOMM2(pComponentPrivate->dbg, "%d :: INPUT--------- Inside Ip Loop\n",__LINE__);
    262         pTemp = pComponentPrivate->pInputBufferList->pBufHdr[i];
    263         pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
    264         pTemp->nFilledLen = nIpBufSize;
    265         pTemp->nVersion.s.nVersionMajor = NBAMRENC_MAJOR_VER;
    266         pTemp->nVersion.s.nVersionMinor = NBAMRENC_MINOR_VER;
    267         pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
    268         pTemp->nTickCount = NBAMRENC_NOT_USED;
    269         pTemp_lcml->buffer = pTemp;
    270         OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pTemp_lcml->buffer->pBuffer = %p \n",__LINE__,pTemp_lcml->buffer->pBuffer);
    271         pTemp_lcml->eDir = OMX_DirInput;
    272 
    273         OMX_MALLOC_SIZE_DSPALIGN(pTemp_lcml->pBufferParam, sizeof(NBAMRENC_ParamStruct),OMX_U8);
    274 
    275         pTemp_lcml->pBufferParam->usNbFrames=0;
    276         pTemp_lcml->pBufferParam->pParamElem=NULL;
    277         pTemp_lcml->pFrameParam=NULL;
    278 
    279         OMX_MALLOC_GENERIC(pTemp_lcml->pDmmBuf, DMM_BUFFER_OBJ);
    280 
    281         /* This means, it is not a last buffer. This flag is to be modified by
    282          * the application to indicate the last buffer */
    283         pTemp->nFlags = NBAMRENC_NORMAL_BUFFER;
    284         pTemp++;
    285         pTemp_lcml++;
    286     }
    287 
    288     /* Allocate memory for all output buffer headers..
    289      * This memory pointer will be sent to LCML */
    290     size_lcml = nOpBuf * sizeof(NBAMRENC_LCML_BUFHEADERTYPE);
    291 
    292     OMX_MALLOC_SIZE(pTemp_lcml, size_lcml,NBAMRENC_LCML_BUFHEADERTYPE);
    293 
    294     pComponentPrivate->pLcmlBufHeader[NBAMRENC_OUTPUT_PORT] = pTemp_lcml;
    295     for (i=0; i<nOpBuf; i++) {
    296         OMX_PRCOMM2(pComponentPrivate->dbg, "%d :: OUTPUT--------- Inside Op Loop\n",__LINE__);
    297         pTemp = pComponentPrivate->pOutputBufferList->pBufHdr[i];
    298         pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
    299         pTemp->nFilledLen = nOpBufSize;
    300         pTemp->nVersion.s.nVersionMajor = NBAMRENC_MAJOR_VER;
    301         pTemp->nVersion.s.nVersionMinor = NBAMRENC_MINOR_VER;
    302         pComponentPrivate->nVersion = pTemp->nVersion.nVersion;
    303         pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
    304         pTemp->nTickCount = NBAMRENC_NOT_USED;
    305         pTemp_lcml->buffer = pTemp;
    306         OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pTemp_lcml->buffer->pBuffer = %p \n",__LINE__,pTemp_lcml->buffer->pBuffer);
    307         pTemp_lcml->eDir = OMX_DirOutput;
    308 
    309         OMX_MALLOC_SIZE_DSPALIGN(pTemp_lcml->pBufferParam,
    310                                 sizeof(NBAMRENC_ParamStruct),
    311                                 NBAMRENC_ParamStruct);
    312 
    313         pTemp_lcml->pBufferParam->usNbFrames=0;
    314         pTemp_lcml->pBufferParam->pParamElem=NULL;
    315         pTemp_lcml->pFrameParam=NULL;
    316 
    317         OMX_MALLOC_GENERIC(pTemp_lcml->pDmmBuf, DMM_BUFFER_OBJ);
    318 
    319         pTemp->nFlags = NBAMRENC_NORMAL_BUFFER;
    320         pTemp++;
    321         pTemp_lcml++;
    322     }
    323 #ifdef __PERF_INSTRUMENTATION__
    324         pComponentPrivate->nLcml_nCntIp = 0;
    325         pComponentPrivate->nLcml_nCntOpReceived = 0;
    326 #endif
    327 
    328     pComponentPrivate->bInitParamsInitialized = 1;
    329 EXIT:
    330     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_FillLCMLInitParams\n",__LINE__);
    331     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
    332     return eError;
    333 }
    334 
    335 /* ========================================================================== */
    336 /**
    337 * @NBAMRENC_StartComponentThread() This function is called by the component to create
    338 * the component thread, command pipes, data pipes and LCML Pipes.
    339 *
    340 * @param pComponent  handle for this instance of the component
    341 *
    342 * @pre
    343 *
    344 * @post
    345 *
    346 * @return none
    347 */
    348 /* ========================================================================== */
    349 
    350 OMX_ERRORTYPE NBAMRENC_StartComponentThread(OMX_HANDLETYPE pComponent)
    351 {
    352     OMX_ERRORTYPE eError = OMX_ErrorNone;
    353     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
    354     AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
    355                         (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
    356 #ifdef UNDER_CE
    357     pthread_attr_t attr;
    358     memset(&attr, 0, sizeof(attr));
    359     attr.__inheritsched = PTHREAD_EXPLICIT_SCHED;
    360     attr.__schedparam.__sched_priority = OMX_AUDIO_ENCODER_THREAD_PRIORITY;
    361 #endif
    362 
    363     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering  NBAMRENC_StartComponentThread\n", __LINE__);
    364     /* Initialize all the variables*/
    365     pComponentPrivate->bIsStopping = 0;
    366     pComponentPrivate->bIsThreadstop = 0;
    367     pComponentPrivate->lcml_nOpBuf = 0;
    368     pComponentPrivate->lcml_nIpBuf = 0;
    369     pComponentPrivate->app_nBuf = 0;
    370     pComponentPrivate->num_Op_Issued = 0;
    371 
    372 
    373     /* create the pipe used to send buffers to the thread */
    374     eError = pipe (pComponentPrivate->cmdDataPipe);
    375     if (eError) {
    376        eError = OMX_ErrorInsufficientResources;
    377        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error while creating cmdDataPipe\n",__LINE__);
    378        goto EXIT;
    379     }
    380     /* create the pipe used to send buffers to the thread */
    381     eError = pipe (pComponentPrivate->dataPipe);
    382     if (eError) {
    383        eError = OMX_ErrorInsufficientResources;
    384        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error while creating dataPipe\n",__LINE__);
    385        goto EXIT;
    386     }
    387 
    388     /* create the pipe used to send commands to the thread */
    389     eError = pipe (pComponentPrivate->cmdPipe);
    390     if (eError) {
    391        eError = OMX_ErrorInsufficientResources;
    392        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error while creating cmdPipe\n",__LINE__);
    393        goto EXIT;
    394     }
    395 
    396     /* Create the Component Thread */
    397 #ifdef UNDER_CE
    398     eError = pthread_create (&(pComponentPrivate->ComponentThread), &attr, NBAMRENC_CompThread, pComponentPrivate);
    399 #else
    400     eError = pthread_create (&(pComponentPrivate->ComponentThread), NULL, NBAMRENC_CompThread, pComponentPrivate);
    401 #endif
    402     if (eError || !pComponentPrivate->ComponentThread) {
    403        eError = OMX_ErrorInsufficientResources;
    404        goto EXIT;
    405     }
    406 
    407     pComponentPrivate->bCompThreadStarted = 1;
    408 EXIT:
    409     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_StartComponentThread\n", __LINE__);
    410     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
    411     return eError;
    412 }
    413 
    414 /* ========================================================================== */
    415 /**
    416 * @NBAMRENC_FreeCompResources() This function is called by the component during
    417 * de-init , to newfree Command pipe, data pipe & LCML pipe.
    418 *
    419 * @param pComponent  handle for this instance of the component
    420 *
    421 * @pre
    422 *
    423 * @post
    424 *
    425 * @return none
    426 */
    427 /* ========================================================================== */
    428 
    429 OMX_ERRORTYPE NBAMRENC_FreeCompResources(OMX_HANDLETYPE pComponent)
    430 {
    431     OMX_ERRORTYPE eError = OMX_ErrorNone;
    432     OMX_ERRORTYPE err = OMX_ErrorNone;
    433     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
    434     AMRENC_COMPONENT_PRIVATE *pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
    435                                                      pHandle->pComponentPrivate;
    436 
    437     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering NBAMRENC_FreeCompResources\n",__LINE__);
    438 
    439     if (pComponentPrivate->bCompThreadStarted) {
    440         OMX_NBCLOSE_PIPE(pComponentPrivate->dataPipe[0],err);
    441         OMX_NBCLOSE_PIPE(pComponentPrivate->dataPipe[1],err);
    442         OMX_NBCLOSE_PIPE(pComponentPrivate->cmdPipe[0],err);
    443         OMX_NBCLOSE_PIPE(pComponentPrivate->cmdPipe[1],err);
    444         OMX_NBCLOSE_PIPE(pComponentPrivate->cmdDataPipe[0],err);
    445         OMX_NBCLOSE_PIPE(pComponentPrivate->cmdDataPipe[1],err);
    446     }
    447 
    448     OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pAlgParam, NBAMRENC_TALGCtrl);
    449     OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pAlgParamDTX, NBAMRENC_TALGCtrlDTX);
    450     OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pParams, NBAMRENC_AudioCodecParams);
    451 
    452     OMX_MEMFREE_STRUCT(pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]);
    453     OMX_MEMFREE_STRUCT(pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]);
    454     OMX_MEMFREE_STRUCT(pComponentPrivate->pcmParams);
    455     OMX_MEMFREE_STRUCT(pComponentPrivate->amrParams);
    456 
    457     OMX_MEMFREE_STRUCT(pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]->pPortFormat);
    458     OMX_MEMFREE_STRUCT(pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]->pPortFormat);
    459     OMX_MEMFREE_STRUCT(pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]);
    460     OMX_MEMFREE_STRUCT(pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]);
    461 
    462     OMX_MEMFREE_STRUCT(pComponentPrivate->sPortParam);
    463     OMX_MEMFREE_STRUCT(pComponentPrivate->sPriorityMgmt);
    464     OMX_MEMFREE_STRUCT(pComponentPrivate->pInputBufferList);
    465     OMX_MEMFREE_STRUCT(pComponentPrivate->pOutputBufferList);
    466 
    467 EXIT:
    468     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_FreeCompResources()\n",__LINE__);
    469     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
    470     return eError;
    471 }
    472 
    473 /* ========================================================================== */
    474 /**
    475 * @NBAMRENC_CleanupInitParams() This function is called by the component during
    476 * de-init to newfree structues that are been allocated at intialization stage
    477 *
    478 * @param pComponent  handle for this instance of the component
    479 *
    480 * @pre
    481 *
    482 * @post
    483 *
    484 * @return none
    485 */
    486 /* ========================================================================== */
    487 
    488 OMX_ERRORTYPE NBAMRENC_CleanupInitParams(OMX_HANDLETYPE pComponent)
    489 {
    490     OMX_ERRORTYPE eError = OMX_ErrorNone;
    491     OMX_U32 nIpBuf = 0;
    492     OMX_U32 nOpBuf = 0;
    493     OMX_U16 i = 0;
    494     NBAMRENC_LCML_BUFHEADERTYPE *pTemp_lcml;
    495 
    496     LCML_DSP_INTERFACE *pLcmlHandle;
    497     LCML_DSP_INTERFACE *pLcmlHandleAux;
    498 
    499     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
    500     AMRENC_COMPONENT_PRIVATE *pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
    501                                                      pHandle->pComponentPrivate;
    502     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering NBAMRENC_CleanupInitParams()\n", __LINE__);
    503 
    504     if(pComponentPrivate->dasfMode == 1) {
    505 
    506         OMX_MEMFREE_STRUCT(pComponentPrivate->strmAttr);
    507     }
    508 
    509     OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pAlgParam, NBAMRENC_TALGCtrl);
    510     OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pAlgParamDTX, NBAMRENC_TALGCtrlDTX);
    511 
    512     pComponentPrivate->nHoldLength = 0;
    513     OMX_MEMFREE_STRUCT(pComponentPrivate->pHoldBuffer);
    514     OMX_MEMFREE_STRUCT(pComponentPrivate->pHoldBuffer2);
    515     OMX_MEMFREE_STRUCT(pComponentPrivate->iHoldBuffer);
    516     OMX_MEMFREE_STRUCT(pComponentPrivate->iMMFDataLastBuffer);
    517 
    518     pTemp_lcml = pComponentPrivate->pLcmlBufHeader[NBAMRENC_INPUT_PORT];
    519     nIpBuf = pComponentPrivate->nRuntimeInputBuffers;
    520 
    521     for(i=0; i<nIpBuf; i++) {
    522         if(pTemp_lcml->pFrameParam!=NULL){
    523               pLcmlHandle = (LCML_DSP_INTERFACE *)pComponentPrivate->pLcmlHandle;
    524               pLcmlHandleAux = (LCML_DSP_INTERFACE *)(((LCML_CODEC_INTERFACE *)pLcmlHandle->pCodecinterfacehandle)->pCodec);
    525               OMX_DmmUnMap(pLcmlHandleAux->dspCodec->hProc,
    526                                (void*)pTemp_lcml->pBufferParam->pParamElem,
    527                                pTemp_lcml->pDmmBuf->pReserved, pComponentPrivate->dbg);
    528 
    529               OMX_MEMFREE_STRUCT_DSPALIGN(pTemp_lcml->pFrameParam, NBAMRENC_FrameStruct);
    530         }
    531 
    532     OMX_MEMFREE_STRUCT_DSPALIGN(pTemp_lcml->pBufferParam, NBAMRENC_ParamStruct);
    533     OMX_MEMFREE_STRUCT(pTemp_lcml->pDmmBuf);
    534     pTemp_lcml++;
    535     }
    536 
    537     pTemp_lcml = pComponentPrivate->pLcmlBufHeader[NBAMRENC_OUTPUT_PORT];
    538     nOpBuf = pComponentPrivate->nRuntimeOutputBuffers;
    539 
    540     for(i=0; i<nOpBuf; i++) {
    541         if(pTemp_lcml->pFrameParam!=NULL){
    542               pLcmlHandle = (LCML_DSP_INTERFACE *)pComponentPrivate->pLcmlHandle;
    543               pLcmlHandleAux = (LCML_DSP_INTERFACE *)(((LCML_CODEC_INTERFACE *)pLcmlHandle->pCodecinterfacehandle)->pCodec);
    544 #ifndef UNDER_CE
    545             OMX_DmmUnMap(pLcmlHandleAux->dspCodec->hProc,
    546                               (void*)pTemp_lcml->pBufferParam->pParamElem,
    547                                pTemp_lcml->pDmmBuf->pReserved, pComponentPrivate->dbg);
    548 #endif
    549 
    550             OMX_MEMFREE_STRUCT_DSPALIGN(pTemp_lcml->pFrameParam, NBAMRENC_FrameStruct);
    551         }
    552 
    553     OMX_MEMFREE_STRUCT_DSPALIGN(pTemp_lcml->pBufferParam, NBAMRENC_ParamStruct);
    554     OMX_MEMFREE_STRUCT(pTemp_lcml->pDmmBuf);
    555     pTemp_lcml++;
    556     }
    557 
    558 
    559     OMX_MEMFREE_STRUCT(pComponentPrivate->pLcmlBufHeader[NBAMRENC_INPUT_PORT]);
    560     OMX_MEMFREE_STRUCT(pComponentPrivate->pLcmlBufHeader[NBAMRENC_OUTPUT_PORT]);
    561 
    562     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_CleanupInitParams()\n",__LINE__);
    563     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
    564     return eError;
    565 }
    566 
    567 /* ========================================================================== */
    568 /**
    569 * @NBAMRENC_StopComponentThread() This function is called by the component during
    570 * de-init to close component thread.
    571 *
    572 * @param pComponent  handle for this instance of the component
    573 *
    574 * @pre
    575 *
    576 * @post
    577 *
    578 * @return none
    579 */
    580 /* ========================================================================== */
    581 
    582 OMX_ERRORTYPE NBAMRENC_StopComponentThread(OMX_HANDLETYPE pComponent)
    583 {
    584     OMX_ERRORTYPE eError = OMX_ErrorNone;
    585     OMX_ERRORTYPE threadError = OMX_ErrorNone;
    586     int pthreadError = 0;
    587     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
    588     AMRENC_COMPONENT_PRIVATE *pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
    589                                                      pHandle->pComponentPrivate;
    590     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering NBAMRENC_StopComponentThread\n",__LINE__);
    591     pComponentPrivate->bIsThreadstop = 1;
    592     write (pComponentPrivate->cmdPipe[1], &pComponentPrivate->bIsThreadstop, sizeof(OMX_U16));
    593     OMX_PRINT2(pComponentPrivate->dbg, "%d :: About to call pthread_join\n",__LINE__);
    594     pthreadError = pthread_join (pComponentPrivate->ComponentThread,
    595                                  (void*)&threadError);
    596     if (0 != pthreadError) {
    597         eError = OMX_ErrorHardware;
    598         OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error closing ComponentThread - pthreadError = %d\n",__LINE__,pthreadError);
    599         goto EXIT;
    600     }
    601     if (OMX_ErrorNone != threadError && OMX_ErrorNone != eError) {
    602         eError = OMX_ErrorInsufficientResources;
    603         OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error while closing Component Thread\n",__LINE__);
    604         goto EXIT;
    605     }
    606 EXIT:
    607    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_StopComponentThread\n",__LINE__);
    608    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
    609    return eError;
    610 }
    611 
    612 
    613 /* ========================================================================== */
    614 /**
    615 * @NBAMRENC_HandleCommand() This function is called by the component when ever it
    616 * receives the command from the application
    617 *
    618 * @param pComponentPrivate  Component private data
    619 *
    620 * @pre
    621 *
    622 * @post
    623 *
    624 * @return none
    625 */
    626 /* ========================================================================== */
    627 
    628 OMX_U32 NBAMRENC_HandleCommand (AMRENC_COMPONENT_PRIVATE *pComponentPrivate)
    629 {
    630     OMX_ERRORTYPE eError = OMX_ErrorNone;
    631     OMX_COMMANDTYPE command;
    632     OMX_STATETYPE commandedState;
    633     OMX_HANDLETYPE pLcmlHandle;
    634 #ifdef RESOURCE_MANAGER_ENABLED
    635     OMX_ERRORTYPE rm_error;
    636 #endif
    637     LCML_CALLBACKTYPE cb;
    638     LCML_DSP *pLcmlDsp;
    639     OMX_U32 cmdValues[4];
    640     OMX_U32 pValues[4];
    641     OMX_U32 commandData;
    642     OMX_U16 arr[100];
    643     char *pArgs = "damedesuStr";
    644     char *p = "hello";
    645     OMX_U16 i = 0;
    646     OMX_U32 ret = 0;
    647     OMX_U8 inputPortFlag=0,outputPortFlag=0;
    648     NBAMRENC_LCML_BUFHEADERTYPE *pLcmlHdr = NULL;
    649 #ifdef DSP_RENDERING_ON
    650 #if 0
    651    AM_COMMANDDATATYPE cmd_data;
    652 #endif
    653 #endif
    654     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
    655     pLcmlHandle = pComponentPrivate->pLcmlHandle;
    656 
    657     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering NBAMRENCHandleCommand Function \n",__LINE__);
    658     OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: pComponentPrivate->curState = %d\n",__LINE__,pComponentPrivate->curState);
    659     ret = read(pComponentPrivate->cmdPipe[0], &command, sizeof (command));
    660     if (ret == -1) {
    661         OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error in Reading from the Data pipe\n", __LINE__);
    662         eError = OMX_ErrorHardware;
    663         goto EXIT;
    664     }
    665     ret = read(pComponentPrivate->cmdDataPipe[0], &commandData, sizeof (commandData));
    666     if (ret == -1) {
    667         OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error in Reading from the Data pipe\n", __LINE__);
    668         eError = OMX_ErrorHardware;
    669         goto EXIT;
    670     }
    671 
    672 #ifdef __PERF_INSTRUMENTATION__
    673     PERF_ReceivedCommand(pComponentPrivate->pPERFcomp,
    674                         command,
    675                         commandData,
    676                         PERF_ModuleLLMM);
    677 #endif
    678     if (command == OMX_CommandStateSet) {
    679         commandedState = (OMX_STATETYPE)commandData;
    680         switch(commandedState) {
    681         case OMX_StateIdle:
    682             OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleCommand :: OMX_StateIdle \n",__LINE__);
    683             OMX_PRSTATE1(pComponentPrivate->dbg, "%d :: pComponentPrivate->curState = %d\n",__LINE__,pComponentPrivate->curState);
    684             if (pComponentPrivate->curState == commandedState){
    685                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
    686                                                          pHandle->pApplicationPrivate,
    687                                                          OMX_EventError,
    688                                                          OMX_ErrorSameState,
    689                                                          OMX_TI_ErrorMinor,
    690                                                          NULL);
    691                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: Same State Given by Application\n",__LINE__);
    692             }
    693             else if (pComponentPrivate->curState == OMX_StateLoaded) {
    694 #ifdef __PERF_INSTRUMENTATION__
    695             PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryStart | PERF_BoundarySetup);
    696 #endif
    697 
    698                  if(pComponentPrivate->dasfMode == 1) {
    699                      if(pComponentPrivate->streamID == 0)
    700                      {
    701                          OMX_ERROR4(pComponentPrivate->dbg, "**************************************\n");
    702                          OMX_ERROR4(pComponentPrivate->dbg, ":: Error = OMX_ErrorInsufficientResources\n");
    703                          OMX_ERROR4(pComponentPrivate->dbg, "**************************************\n");
    704                          eError = OMX_ErrorInsufficientResources;
    705                          pComponentPrivate->curState = OMX_StateInvalid;
    706                          pComponentPrivate->cbInfo.EventHandler(pHandle,
    707                                                                 pHandle->pApplicationPrivate,
    708                                                                 OMX_EventError,
    709                                                                 OMX_ErrorInvalidState,
    710                                                                 OMX_TI_ErrorMajor,
    711                                                                 "No Stream ID Available");
    712                          goto EXIT;
    713                      }
    714                  }
    715 
    716                  if (pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated &&  pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled)  {
    717                                      inputPortFlag = 1;
    718                  }
    719                  if (pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated && pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled) {
    720                                      outputPortFlag = 1;
    721                  }
    722 
    723                 if (!pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated && !pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled) {
    724                     inputPortFlag = 1;
    725                 }
    726 
    727                 if (!pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated && !pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled) {
    728                     outputPortFlag = 1;
    729                 }
    730 
    731                 if((pComponentPrivate->dasfMode && !outputPortFlag) ||
    732                (!pComponentPrivate->dasfMode && (!inputPortFlag || !outputPortFlag)))
    733                  {
    734                          /* Sleep for a while, so the application thread can allocate buffers */
    735                          OMX_PRSTATE1(pComponentPrivate->dbg, "%d :: Sleeping...\n",__LINE__);
    736                          pComponentPrivate->InLoaded_readytoidle = 1;
    737 #ifndef UNDER_CE
    738                          pthread_mutex_lock(&pComponentPrivate->InLoaded_mutex);
    739                          pthread_cond_wait(&pComponentPrivate->InLoaded_threshold, &pComponentPrivate->InLoaded_mutex);
    740                          pthread_mutex_unlock(&pComponentPrivate->InLoaded_mutex);
    741 #else
    742                          OMX_WaitForEvent(&(pComponentPrivate->InLoaded_event));
    743 #endif
    744                  }
    745 
    746                 cb.LCML_Callback = (void *) NBAMRENC_LCMLCallback;
    747                 pLcmlHandle = (OMX_HANDLETYPE) NBAMRENC_GetLCMLHandle(pComponentPrivate);
    748 
    749                 if (pLcmlHandle == NULL) {
    750                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: LCML Handle is NULL........exiting..\n",__LINE__);
    751                     goto EXIT;
    752                 }
    753 
    754 
    755                 /* Got handle of dsp via phandle filling information about DSP Specific things */
    756                 pLcmlDsp = (((LCML_DSP_INTERFACE*)pLcmlHandle)->dspCodec);
    757                 eError = NBAMRENC_FillLCMLInitParams(pHandle, pLcmlDsp, arr);
    758                 if(eError != OMX_ErrorNone) {
    759                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error from NBAMRENCFill_LCMLInitParams()\n",__LINE__);
    760                     goto EXIT;
    761                 }
    762 
    763 
    764                 pComponentPrivate->pLcmlHandle = (LCML_DSP_INTERFACE *)pLcmlHandle;
    765                 cb.LCML_Callback = (void *) NBAMRENC_LCMLCallback;
    766 
    767 #ifndef UNDER_CE
    768                 eError = LCML_InitMMCodecEx(((LCML_DSP_INTERFACE *)pLcmlHandle)->pCodecinterfacehandle,
    769                                           p,&pLcmlHandle,(void *)p,&cb, (OMX_STRING)pComponentPrivate->sDeviceString);
    770 
    771 #else
    772                 eError = LCML_InitMMCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
    773                                                                     p,&pLcmlHandle, (void *)p, &cb);
    774 #endif
    775 
    776                 if(eError != OMX_ErrorNone) {
    777                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error returned from LCML_Init()\n",__LINE__);
    778                     /* send an event to client */
    779                     /* client should unload the component if the codec is not able to load */
    780                     eError = OMX_ErrorInvalidState;
    781                     pComponentPrivate->cbInfo.EventHandler (pHandle,
    782                                                 pHandle->pApplicationPrivate,
    783                                                 OMX_EventError,
    784                                                 eError,
    785                                                 OMX_TI_ErrorSevere,
    786                                                 NULL);
    787                     goto EXIT;
    788                 }
    789 
    790 #ifdef RESOURCE_MANAGER_ENABLED
    791                 /* Need check the resource with RM */
    792                 pComponentPrivate->rmproxyCallback.RMPROXY_Callback = (void *) NBAMRENC_ResourceManagerCallback;
    793 
    794                 if (pComponentPrivate->curState != OMX_StateWaitForResources){
    795 
    796                     rm_error = RMProxy_NewSendCommand(pHandle,
    797                                                         RMProxy_RequestResource,
    798                                                         OMX_NBAMR_Encoder_COMPONENT, NBAMRENC_CPU_LOAD,
    799                                                         3456, &(pComponentPrivate->rmproxyCallback));
    800 
    801                     if(rm_error == OMX_ErrorNone) {
    802                         /* resource is available */
    803 #ifdef __PERF_INSTRUMENTATION__
    804                         PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryComplete | PERF_BoundarySetup);
    805 #endif
    806                         pComponentPrivate->curState = OMX_StateIdle;
    807 
    808                        /* Decrement reference count with signal enabled */
    809                         if(RemoveStateTransition(pComponentPrivate, OMX_TRUE) != OMX_ErrorNone) {
    810                             return OMX_ErrorUndefined;
    811                         }
    812 
    813                         pComponentPrivate->cbInfo.EventHandler(pHandle,
    814                                 pHandle->pApplicationPrivate,
    815                                 OMX_EventCmdComplete,
    816                                 OMX_CommandStateSet,
    817                                 pComponentPrivate->curState,
    818                                 NULL);
    819 
    820                         rm_error = RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_NBAMR_Encoder_COMPONENT, OMX_StateIdle, 3456, NULL);
    821                     }
    822                     else if(rm_error == OMX_ErrorInsufficientResources) {
    823                         /* resource is not available, need set state to OMX_StateWaitForResources */
    824                         pComponentPrivate->curState = OMX_StateWaitForResources;
    825                         pComponentPrivate->cbInfo.EventHandler( pHandle,
    826                                                                 pHandle->pApplicationPrivate,
    827                                                                 OMX_EventCmdComplete,
    828                                                                 OMX_CommandStateSet,
    829                                                                 pComponentPrivate->curState,
    830                                                                 NULL);
    831                         OMX_ERROR4(pComponentPrivate->dbg, "%d :: Comp: OMX_ErrorInsufficientResources\n", __LINE__);
    832                     }
    833             }
    834             else {
    835                     pComponentPrivate->curState = OMX_StateIdle;
    836 
    837                     /* Decrement reference count with signal enabled */
    838                     if(RemoveStateTransition(pComponentPrivate, OMX_TRUE) != OMX_ErrorNone) {
    839                           return OMX_ErrorUndefined;
    840                     }
    841 
    842                     pComponentPrivate->cbInfo.EventHandler(pHandle,
    843                                                             pHandle->pApplicationPrivate,
    844                                                             OMX_EventCmdComplete,
    845                                                             OMX_CommandStateSet,
    846                                                             pComponentPrivate->curState,
    847                                                             NULL);
    848 
    849                 rm_error = RMProxy_NewSendCommand(pHandle,
    850                                                        RMProxy_StateSet,
    851                                                        OMX_NBAMR_Encoder_COMPONENT,
    852                                                        OMX_StateIdle,
    853                                                        3456,
    854                                                        NULL);
    855 
    856             }
    857 #else
    858                 pComponentPrivate->curState = OMX_StateIdle;
    859 
    860                 /* Decrement reference count with signal enabled */
    861                 if(RemoveStateTransition(pComponentPrivate, OMX_TRUE) != OMX_ErrorNone) {
    862                       return OMX_ErrorUndefined;
    863                 }
    864 
    865                 pComponentPrivate->cbInfo.EventHandler( pHandle,
    866                                                         pHandle->pApplicationPrivate,
    867                                                         OMX_EventCmdComplete,
    868                                                         OMX_CommandStateSet,
    869                                                         pComponentPrivate->curState,
    870                                                         NULL);
    871 
    872 #endif
    873 
    874             }
    875             else if (pComponentPrivate->curState == OMX_StateExecuting) {
    876                 OMX_PRSTATE1(pComponentPrivate->dbg, "%d :: Setting Component to OMX_StateIdle\n",__LINE__);
    877                 OMX_PRINT2(pComponentPrivate->dbg, "%d :: AMRENC: About to Call MMCodecControlStop\n", __LINE__);
    878 #ifdef __PERF_INSTRUMENTATION__
    879                     PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryComplete | PERF_BoundarySteadyState);
    880 #endif
    881                 pComponentPrivate->bIsStopping = 1;
    882 
    883                 if (pComponentPrivate->codecStop_waitingsignal == 0){
    884                     pthread_mutex_lock(&pComponentPrivate->codecStop_mutex);
    885                 }
    886                 eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
    887                                                                     MMCodecControlStop,(void *)pArgs);
    888 
    889                 if (pComponentPrivate->codecStop_waitingsignal == 0){
    890                     pthread_cond_wait(&pComponentPrivate->codecStop_threshold, &pComponentPrivate->codecStop_mutex);
    891                     pComponentPrivate->codecStop_waitingsignal = 0;
    892                     pthread_mutex_unlock(&pComponentPrivate->codecStop_mutex);
    893                 }
    894 
    895                 OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pAlgParam, NBAMRENC_TALGCtrl);
    896                 OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pAlgParamDTX, NBAMRENC_TALGCtrlDTX);
    897 
    898                 pComponentPrivate->nOutStandingFillDones = 0;
    899                                 pComponentPrivate->nOutStandingEmptyDones = 0;
    900 
    901                 OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pParams, NBAMRENC_AudioCodecParams);
    902 
    903                 if(eError != OMX_ErrorNone) {
    904                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error from LCML_ControlCodec MMCodecControlStop..\n",__LINE__);
    905                     goto EXIT;
    906                 }
    907             }
    908             else if(pComponentPrivate->curState == OMX_StatePause) {
    909 
    910                 pComponentPrivate->curState = OMX_StateIdle;
    911 #ifdef __PERF_INSTRUMENTATION__
    912                     PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryComplete | PERF_BoundarySteadyState);
    913 #endif
    914 #ifdef RESOURCE_MANAGER_ENABLED
    915                 rm_error = RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_NBAMR_Encoder_COMPONENT, OMX_StateIdle, 3456, NULL);
    916 
    917 #endif
    918                 OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: The component is stopped\n",__LINE__);
    919 
    920                 /* Decrement reference count with signal enabled */
    921                 if(RemoveStateTransition(pComponentPrivate, OMX_TRUE) != OMX_ErrorNone) {
    922                       return OMX_ErrorUndefined;
    923                 }
    924 
    925                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
    926                                                          pHandle->pApplicationPrivate,
    927                                                          OMX_EventCmdComplete,
    928                                                          OMX_CommandStateSet,
    929                                                          pComponentPrivate->curState,
    930                                                          NULL);
    931             } else {    /* This means, it is invalid state from application */
    932                 pComponentPrivate->cbInfo.EventHandler( pHandle,
    933                                                         pHandle->pApplicationPrivate,
    934                                                         OMX_EventError,
    935                                                         OMX_ErrorIncorrectStateTransition,
    936                                                         OMX_TI_ErrorMinor,
    937                                                         "Invalid State");
    938                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Comp: OMX_ErrorIncorrectStateTransition\n",__LINE__);
    939             }
    940             break;
    941 
    942         case OMX_StateExecuting:
    943             OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleCommand :: OMX_StateExecuting \n",__LINE__);
    944             if (pComponentPrivate->curState == commandedState){
    945                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
    946                                                          pHandle->pApplicationPrivate,
    947                                                          OMX_EventError,
    948                                                          OMX_ErrorSameState,
    949                                                          OMX_TI_ErrorMinor,
    950                                                          "Invalid State");
    951                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Comp: OMX_ErrorSameState Given by Comp\n",__LINE__);
    952                 break;
    953             }
    954             else if (pComponentPrivate->curState == OMX_StateIdle) {
    955                 /* Sending commands to DSP via LCML_ControlCodec third argument
    956                 is not used for time being */
    957                 pComponentPrivate->nNumInputBufPending = 0;
    958                 pComponentPrivate->nNumOutputBufPending = 0;
    959                 pComponentPrivate->nNumOfFramesSent=0;
    960                 pComponentPrivate->nEmptyBufferDoneCount = 0;
    961                 pComponentPrivate->nEmptyThisBufferCount =0;
    962 
    963                 OMX_MALLOC_SIZE_DSPALIGN(pComponentPrivate->pAlgParam, sizeof(NBAMRENC_TALGCtrl),OMX_U8);
    964                 OMX_MALLOC_SIZE_DSPALIGN(pComponentPrivate->pAlgParamDTX, sizeof(NBAMRENC_TALGCtrlDTX),OMX_U8);
    965 
    966                 pComponentPrivate->pAlgParam->iBitrate = pComponentPrivate->amrParams->eAMRBandMode;
    967 
    968 #ifdef NBAMR_DTX_VAD_MODE_SUPPORT_ENABLED
    969                 if (pComponentPrivate->amrParams->eAMRDTXMode == OMX_AUDIO_AMRDTXModeOnAuto) {
    970                     pComponentPrivate->pAlgParamDTX->iVADFlag = OMX_TRUE;
    971                 }
    972                 else {
    973                     pComponentPrivate->pAlgParamDTX->iVADFlag = OMX_FALSE;
    974                 }
    975 #else
    976                 pComponentPrivate->pAlgParamDTX->iVADFlag = OMX_FALSE;
    977 #endif
    978                 pComponentPrivate->pAlgParam->iSize = sizeof (NBAMRENC_TALGCtrl);
    979                 pComponentPrivate->pAlgParamDTX->iSize = sizeof (NBAMRENC_TALGCtrlDTX);
    980                 OMX_PRINT2(pComponentPrivate->dbg, "%d :: pAlgParam->iBitrate = %d\n",__LINE__,pComponentPrivate->pAlgParam->iBitrate);
    981                 OMX_PRINT2(pComponentPrivate->dbg, "%d :: pAlgParamDTX->iVADFlag  = %d\n",__LINE__,pComponentPrivate->pAlgParamDTX->iVADFlag);
    982 
    983                 cmdValues[0] = ALGCMD_BITRATE;                  /*setting the bit-rate*/
    984                 cmdValues[1] = (OMX_U32)pComponentPrivate->pAlgParam;
    985                 cmdValues[2] = sizeof (NBAMRENC_TALGCtrl);
    986                 p = (void *)&cmdValues;
    987                 OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: EMMCodecControlAlgCtrl-1 Sending...\n",__LINE__);
    988                 /* Sending ALGCTRL MESSAGE DTX to DSP via LCML_ControlCodec*/
    989                 eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
    990                                                 EMMCodecControlAlgCtrl, (void *)p);
    991                 if (eError != OMX_ErrorNone) {
    992                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error from LCML_ControlCodec EMMCodecControlAlgCtrl-1 failed = %x\n",__LINE__,eError);
    993                     goto EXIT;
    994                 }
    995                 cmdValues[0] = ALGCMD_DTX;                  /*setting DTX mode*/
    996                 cmdValues[1] = (OMX_U32)pComponentPrivate->pAlgParamDTX;
    997                 cmdValues[2] = sizeof (NBAMRENC_TALGCtrlDTX);
    998                 p = (void *)&cmdValues;
    999                 OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: EMMCodecControlAlgCtrl-2 Sending...\n",__LINE__);
   1000                 /* Sending ALGCTRL MESSAGE BITRATE to DSP via LCML_ControlCodec*/
   1001                 eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1002                                                 EMMCodecControlAlgCtrl, (void *)p);
   1003                 if (eError != OMX_ErrorNone) {
   1004                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error from LCML_ControlCodec EMMCodecControlAlgCtrl-2 failed = %x\n",__LINE__,eError);
   1005                     goto EXIT;
   1006                 }
   1007                 if(pComponentPrivate->dasfMode == 1) {
   1008                     OMX_PRDSP2(pComponentPrivate->dbg, "%d :: ---- Comp: DASF Functionality is ON ---\n",__LINE__);
   1009 
   1010                     OMX_MALLOC_SIZE_DSPALIGN(pComponentPrivate->pParams, sizeof(NBAMRENC_AudioCodecParams),OMX_U8);
   1011 
   1012                     pComponentPrivate->pParams->iAudioFormat = 1;
   1013                     pComponentPrivate->pParams->iStrmId = pComponentPrivate->streamID;
   1014                     pComponentPrivate->pParams->iSamplingRate = NBAMRENC_SAMPLING_FREQUENCY;
   1015                     pValues[0] = USN_STRMCMD_SETCODECPARAMS;
   1016                     pValues[1] = (OMX_U32)pComponentPrivate->pParams;
   1017                     pValues[2] = sizeof(NBAMRENC_AudioCodecParams);
   1018                     /* Sending STRMCTRL MESSAGE to DSP via LCML_ControlCodec*/
   1019                     eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1020                                                               EMMCodecControlStrmCtrl,(void *)pValues);
   1021                     if(eError != OMX_ErrorNone) {
   1022                        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error from LCML_ControlCodec EMMCodecControlStrmCtrl = %x\n",__LINE__,eError);
   1023                        goto EXIT;
   1024                     }
   1025                 }
   1026                 /* Sending START MESSAGE to DSP via LCML_ControlCodec*/
   1027                 eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1028                                                 EMMCodecControlStart, (void *)p);
   1029                 if(eError != OMX_ErrorNone) {
   1030                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error from LCML_ControlCodec EMMCodecControlStart = %x\n",__LINE__,eError);
   1031                     goto EXIT;
   1032                 }
   1033 
   1034             } else if (pComponentPrivate->curState == OMX_StatePause) {
   1035                 eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1036                                                 EMMCodecControlStart, (void *)p);
   1037                 if (eError != OMX_ErrorNone) {
   1038                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error While Resuming the codec = %x\n",__LINE__,eError);
   1039                     goto EXIT;
   1040                 }
   1041 
   1042                 for (i=0; i < pComponentPrivate->nNumInputBufPending; i++) {
   1043                     if (pComponentPrivate->pInputBufHdrPending[i]) {
   1044                         NBAMRENC_GetCorrespondingLCMLHeader(pComponentPrivate, pComponentPrivate->pInputBufHdrPending[i]->pBuffer, OMX_DirInput, &pLcmlHdr);
   1045                         NBAMRENC_SetPending(pComponentPrivate,pComponentPrivate->pInputBufHdrPending[i],OMX_DirInput,__LINE__);
   1046 
   1047                         eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1048                                                     EMMCodecInputBuffer,
   1049                                                     pComponentPrivate->pInputBufHdrPending[i]->pBuffer,
   1050                                                     pComponentPrivate->pInputBufHdrPending[i]->nAllocLen,
   1051                                                     pComponentPrivate->pInputBufHdrPending[i]->nFilledLen,
   1052                                                     (OMX_U8 *) pLcmlHdr->pBufferParam,
   1053                                                     sizeof(NBAMRENC_ParamStruct),
   1054                                                     NULL);
   1055                     }
   1056                 }
   1057                 pComponentPrivate->nNumInputBufPending = 0;
   1058 
   1059                 for (i=0; i < pComponentPrivate->nNumOutputBufPending; i++) {
   1060                     if (pComponentPrivate->pOutputBufHdrPending[i]) {
   1061                         NBAMRENC_GetCorrespondingLCMLHeader(pComponentPrivate,pComponentPrivate->pOutputBufHdrPending[i]->pBuffer, OMX_DirOutput, &pLcmlHdr);
   1062                         NBAMRENC_SetPending(pComponentPrivate,pComponentPrivate->pOutputBufHdrPending[i],OMX_DirOutput,__LINE__);
   1063                         eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1064                                                     EMMCodecOuputBuffer,
   1065                                                     pComponentPrivate->pOutputBufHdrPending[i]->pBuffer,
   1066                                                     pComponentPrivate->pOutputBufHdrPending[i]->nAllocLen,
   1067                                                     pComponentPrivate->pOutputBufHdrPending[i]->nFilledLen,
   1068                                                     (OMX_U8 *) pLcmlHdr->pBufferParam,
   1069                                                     sizeof(NBAMRENC_ParamStruct),
   1070                                                     NULL);
   1071                     }
   1072                 }
   1073                 pComponentPrivate->nNumOutputBufPending = 0;
   1074             } else {
   1075                 pComponentPrivate->cbInfo.EventHandler( pHandle,
   1076                                                         pHandle->pApplicationPrivate,
   1077                                                         OMX_EventError,
   1078                                                         OMX_ErrorIncorrectStateTransition,
   1079                                                         OMX_TI_ErrorMinor,
   1080                                                         "Incorrect State Transition");
   1081                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Comp: OMX_ErrorIncorrectStateTransition Given by Comp\n",__LINE__);
   1082                 goto EXIT;
   1083 
   1084             }
   1085 #ifdef RESOURCE_MANAGER_ENABLED
   1086              rm_error = RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_NBAMR_Encoder_COMPONENT, OMX_StateExecuting, 3456, NULL);
   1087 #endif
   1088             pComponentPrivate->curState = OMX_StateExecuting;
   1089 #ifdef __PERF_INSTRUMENTATION__
   1090             PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryStart | PERF_BoundarySteadyState);
   1091 #endif
   1092 
   1093             /* Decrement reference count with signal enabled */
   1094             if(RemoveStateTransition(pComponentPrivate, OMX_TRUE) != OMX_ErrorNone) {
   1095                   return OMX_ErrorUndefined;
   1096             }
   1097 
   1098             pComponentPrivate->cbInfo.EventHandler( pHandle,
   1099                                                     pHandle->pApplicationPrivate,
   1100                                                     OMX_EventCmdComplete,
   1101                                                     OMX_CommandStateSet,
   1102                                                     pComponentPrivate->curState,
   1103                                                     NULL);
   1104             OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: Comp: OMX_CommandStateSet Given by Comp\n",__LINE__);
   1105             break;
   1106 
   1107         case OMX_StateLoaded:
   1108             OMX_PRDSP2(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleCommand :: OMX_StateLoaded\n",__LINE__);
   1109             if (pComponentPrivate->curState == commandedState){
   1110                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
   1111                                                          pHandle->pApplicationPrivate,
   1112                                                          OMX_EventError,
   1113                                                          OMX_ErrorSameState,
   1114                                                          OMX_TI_ErrorMinor,
   1115                                                          "Same State");
   1116                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Comp: OMX_ErrorSameState Given by Comp\n",__LINE__);
   1117                 break;
   1118              }
   1119             if (pComponentPrivate->curState == OMX_StateWaitForResources){
   1120                 OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleCommand :: OMX_StateWaitForResources\n",__LINE__);
   1121 #ifdef __PERF_INSTRUMENTATION__
   1122                 PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryStart | PERF_BoundaryCleanup);
   1123 #endif
   1124                 pComponentPrivate->curState = OMX_StateLoaded;
   1125 #ifdef __PERF_INSTRUMENTATION__
   1126                 PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryComplete | PERF_BoundaryCleanup);
   1127 #endif
   1128                 pComponentPrivate->curState = OMX_StateLoaded;
   1129 
   1130                 /* Decrement reference count with signal enabled */
   1131                 if(RemoveStateTransition(pComponentPrivate, OMX_TRUE) != OMX_ErrorNone) {
   1132                     return OMX_ErrorUndefined;
   1133                 }
   1134                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
   1135                                                          pHandle->pApplicationPrivate,
   1136                                                          OMX_EventCmdComplete,
   1137                                                          OMX_CommandStateSet,
   1138                                                          pComponentPrivate->curState,
   1139                                                          NULL);
   1140                 OMX_PRINT2(pComponentPrivate->dbg, "%d :: Comp: OMX_CommandStateSet Given by Comp\n",__LINE__);
   1141                 break;
   1142             }
   1143             if (pComponentPrivate->curState != OMX_StateIdle &&
   1144                 pComponentPrivate->curState != OMX_StateWaitForResources) {
   1145                 OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleCommand :: OMX_StateIdle && OMX_StateWaitForResources\n",__LINE__);
   1146                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
   1147                                                          pHandle->pApplicationPrivate,
   1148                                                          OMX_EventError,
   1149                                                          OMX_ErrorIncorrectStateTransition,
   1150                                                          OMX_TI_ErrorMinor,
   1151                                                          "Incorrect State Transition");
   1152                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: OMX_ErrorIncorrectStateTransition Given by Comp\n",__LINE__);
   1153                 goto EXIT;
   1154             }
   1155 #ifdef __PERF_INSTRUMENTATION__
   1156             PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryStart | PERF_BoundaryCleanup);
   1157 #endif
   1158             OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleCommand - evaluating if all buffers are free\n",__LINE__);
   1159 
   1160             if (pComponentPrivate->pInputBufferList->numBuffers ||
   1161                 pComponentPrivate->pOutputBufferList->numBuffers) {
   1162 
   1163                 pthread_mutex_lock(&pComponentPrivate->ToLoaded_mutex);
   1164                 pComponentPrivate->InIdle_goingtoloaded = 1;
   1165                 pthread_mutex_unlock(&pComponentPrivate->ToLoaded_mutex);
   1166 
   1167             }
   1168 
   1169             if (!pComponentPrivate->pInputBufferList->numBuffers &&
   1170                 !pComponentPrivate->pOutputBufferList->numBuffers) {
   1171 
   1172             /* Now Deinitialize the component No error should be returned from
   1173             * this function. It should clean the system as much as possible */
   1174             NBAMRENC_CleanupInitParams(pComponentPrivate->pHandle);
   1175             eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1176                                         EMMCodecControlDestroy, (void *)p);
   1177             if (eError != OMX_ErrorNone) {
   1178                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: LCML_ControlCodec EMMCodecControlDestroy = %x\n",__LINE__, eError);
   1179                 goto EXIT;
   1180             }
   1181 
   1182     /*Closing LCML Lib*/
   1183     if (pComponentPrivate->ptrLibLCML != NULL)
   1184     {
   1185         OMX_PRDSP2(pComponentPrivate->dbg, "%d OMX_AmrEncoder.c Closing LCML library\n",__LINE__);
   1186         dlclose( pComponentPrivate->ptrLibLCML);
   1187         pComponentPrivate->ptrLibLCML = NULL;
   1188     }
   1189 
   1190 
   1191 #ifdef __PERF_INSTRUMENTATION__
   1192             PERF_SendingCommand(pComponentPrivate->pPERF, -1, 0, PERF_ModuleComponent);
   1193 #endif
   1194             eError = NBAMRENC_EXIT_COMPONENT_THRD;
   1195             pComponentPrivate->bInitParamsInitialized = 0;
   1196             pComponentPrivate->bLoadedCommandPending = OMX_FALSE;
   1197             pComponentPrivate->bLoadedWaitingFreeBuffers = OMX_FALSE;
   1198 
   1199         }
   1200         else {
   1201             pComponentPrivate->bLoadedWaitingFreeBuffers = OMX_TRUE;
   1202             OMX_PRBUFFER2(pComponentPrivate->dbg, "Skipped this section because buffers not yet freed\n");
   1203         }
   1204             break;
   1205 
   1206         case OMX_StatePause:
   1207             OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleCommand :: OMX_StatePause\n",__LINE__);
   1208             if (pComponentPrivate->curState == commandedState){
   1209                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
   1210                                                          pHandle->pApplicationPrivate,
   1211                                                          OMX_EventError,
   1212                                                          OMX_ErrorSameState,
   1213                                                          OMX_TI_ErrorMinor,
   1214                                                          "Same State");
   1215                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: OMX_ErrorSameState Given by Comp\n",__LINE__);
   1216                 break;
   1217             }
   1218             if (pComponentPrivate->curState != OMX_StateExecuting &&
   1219                 pComponentPrivate->curState != OMX_StateIdle) {
   1220                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
   1221                                                          pHandle->pApplicationPrivate,
   1222                                                          OMX_EventError,
   1223                                                          OMX_ErrorIncorrectStateTransition,
   1224                                                          OMX_TI_ErrorMinor,
   1225                                                          "Incorrect State Transition");
   1226                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: OMX_ErrorIncorrectStateTransition Given by Comp\n",__LINE__);
   1227                 goto EXIT;
   1228             }
   1229 #ifdef __PERF_INSTRUMENTATION__
   1230                 PERF_Boundary(pComponentPrivate->pPERFcomp,PERF_BoundaryComplete | PERF_BoundarySteadyState);
   1231 #endif
   1232             eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1233                                         EMMCodecControlPause, (void *)p);
   1234             if (eError != OMX_ErrorNone) {
   1235                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: LCML_ControlCodec EMMCodecControlPause = %x\n",__LINE__,eError);
   1236                 goto EXIT;
   1237             }
   1238             OMX_PRSTATE1(pComponentPrivate->dbg, "%d :: Comp: OMX_CommandStateSet Given by Comp\n",__LINE__);
   1239             break;
   1240 
   1241         case OMX_StateWaitForResources:
   1242             if (pComponentPrivate->curState == commandedState) {
   1243                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
   1244                                                          pHandle->pApplicationPrivate,
   1245                                                          OMX_EventError,
   1246                                                          OMX_ErrorSameState,
   1247                                                          OMX_TI_ErrorMinor,
   1248                                                          "Same State");
   1249                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: OMX_ErrorSameState Given by Comp\n",__LINE__);
   1250             } else if (pComponentPrivate->curState == OMX_StateLoaded) {
   1251 
   1252 #ifdef RESOURCE_MANAGER_ENABLED
   1253             rm_error = RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_NBAMR_Encoder_COMPONENT, OMX_StateWaitForResources, 3456, NULL);
   1254 #endif
   1255 
   1256                 pComponentPrivate->curState = OMX_StateWaitForResources;
   1257                 pComponentPrivate->cbInfo.EventHandler( pHandle,
   1258                                                         pHandle->pApplicationPrivate,
   1259                                                         OMX_EventCmdComplete,
   1260                                                         OMX_CommandStateSet,
   1261                                                         pComponentPrivate->curState,
   1262                                                         NULL);
   1263                 OMX_PRINT2(pComponentPrivate->dbg, "%d :: Comp: OMX_CommandStateSet Given by Comp\n",__LINE__);
   1264             } else {
   1265                 pComponentPrivate->cbInfo.EventHandler( pHandle,
   1266                                                         pHandle->pApplicationPrivate,
   1267                                                         OMX_EventError,
   1268                                                         OMX_ErrorIncorrectStateTransition,
   1269                                                         OMX_TI_ErrorMinor,
   1270                                                         "Incorrect State Transition");
   1271                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: OMX_ErrorIncorrectStateTransition Given by Comp\n",__LINE__);
   1272             }
   1273             break;
   1274 
   1275         case OMX_StateInvalid:
   1276             if (pComponentPrivate->curState == commandedState) {
   1277                 pComponentPrivate->cbInfo.EventHandler ( pHandle,
   1278                                                          pHandle->pApplicationPrivate,
   1279                                                          OMX_EventError,
   1280                                                          OMX_ErrorSameState,
   1281                                                          OMX_TI_ErrorSevere,
   1282                                                          "Same State");
   1283                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: OMX_ErrorSameState Given by Comp\n",__LINE__);
   1284             }
   1285             else {
   1286                 OMX_PRSTATE2(pComponentPrivate->dbg, "%d: HandleCommand: Cmd OMX_StateInvalid:\n",__LINE__);
   1287                 if (pComponentPrivate->curState != OMX_StateWaitForResources &&
   1288                     pComponentPrivate->curState != OMX_StateInvalid &&
   1289                     pComponentPrivate->curState != OMX_StateLoaded) {
   1290 
   1291                     eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1292                                                 EMMCodecControlDestroy, (void *)pArgs);
   1293                 }
   1294                 pComponentPrivate->curState = OMX_StateInvalid;
   1295                 pComponentPrivate->cbInfo.EventHandler( pHandle,
   1296                                                     pHandle->pApplicationPrivate,
   1297                                                     OMX_EventError,
   1298                                                     OMX_ErrorInvalidState,
   1299                                                     OMX_TI_ErrorSevere,
   1300                                                     "Incorrect State Transition");
   1301 
   1302                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: Comp: OMX_ErrorInvalidState Given by Comp\n",__LINE__);
   1303                 NBAMRENC_CleanupInitParams(pHandle);
   1304             }
   1305             break;
   1306 
   1307         case OMX_StateMax:
   1308             OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: NBAMRENC_HandleCommand :: Cmd OMX_StateMax\n",__LINE__);
   1309             break;
   1310         } /* End of Switch */
   1311     } else if (command == OMX_CommandMarkBuffer) {
   1312         OMX_PRSTATE2(pComponentPrivate->dbg, "%d :: command OMX_CommandMarkBuffer received\n",__LINE__);
   1313         if(!pComponentPrivate->pMarkBuf){
   1314             /* TODO Need to handle multiple marks */
   1315             pComponentPrivate->pMarkBuf = (OMX_MARKTYPE *)(commandData);
   1316         }
   1317     } else if (command == OMX_CommandPortDisable) {
   1318         if (!pComponentPrivate->bDisableCommandPending) {
   1319             OMX_PRINT2(pComponentPrivate->dbg, "I'm here Line %d\n",__LINE__);
   1320             if(commandData == 0x0 || commandData == -1){
   1321                 pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled = OMX_FALSE;
   1322             }
   1323             if(commandData == 0x1 || commandData == -1){
   1324                 char *pArgs = "damedesuStr";
   1325 
   1326                 pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled = OMX_FALSE;
   1327                 if (pComponentPrivate->curState == OMX_StateExecuting) {
   1328                     pComponentPrivate->bNoIdleOnStop = OMX_TRUE;
   1329                     pComponentPrivate->bIsStopping = 1;
   1330                     if (pComponentPrivate->codecStop_waitingsignal == 0){
   1331                         pthread_mutex_lock(&pComponentPrivate->codecStop_mutex);
   1332                     }
   1333                     eError = LCML_ControlCodec(
   1334                                   ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1335                                   MMCodecControlStop,(void *)pArgs);
   1336                     if (pComponentPrivate->codecStop_waitingsignal == 0){
   1337                         pthread_cond_wait(&pComponentPrivate->codecStop_threshold, &pComponentPrivate->codecStop_mutex);
   1338                         pComponentPrivate->codecStop_waitingsignal = 0;
   1339                         pthread_mutex_unlock(&pComponentPrivate->codecStop_mutex);
   1340                     }
   1341 
   1342                 }
   1343             }
   1344         }
   1345 
   1346         OMX_PRCOMM2(pComponentPrivate->dbg, "commandData = %d\n",(int)commandData);
   1347         OMX_PRCOMM2(pComponentPrivate->dbg, "pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated = %d\n",pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated);
   1348         OMX_PRCOMM2(pComponentPrivate->dbg, "pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated = %d\n",pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated);
   1349 
   1350         if(commandData == 0x0) {
   1351             if(!pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated){
   1352                 /* return cmdcomplete event if input unpopulated */
   1353                 pComponentPrivate->cbInfo.EventHandler(
   1354                      pHandle, pHandle->pApplicationPrivate,
   1355                      OMX_EventCmdComplete, OMX_CommandPortDisable,NBAMRENC_INPUT_PORT, NULL);
   1356                 pComponentPrivate->bDisableCommandPending = 0;
   1357             }
   1358             else{
   1359                 pComponentPrivate->bDisableCommandPending = 1;
   1360                 pComponentPrivate->bDisableCommandParam = commandData;
   1361             }
   1362         }
   1363 
   1364         if(commandData == 0x1) {
   1365             if (!pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated){
   1366                 /* return cmdcomplete event if output unpopulated */
   1367                 pComponentPrivate->cbInfo.EventHandler(
   1368                     pHandle, pHandle->pApplicationPrivate,
   1369                     OMX_EventCmdComplete, OMX_CommandPortDisable,NBAMRENC_OUTPUT_PORT, NULL);
   1370                 pComponentPrivate->bDisableCommandPending = 0;
   1371             }
   1372             else {
   1373                 pComponentPrivate->bDisableCommandPending = 1;
   1374                 pComponentPrivate->bDisableCommandParam = commandData;
   1375             }
   1376         }
   1377 
   1378         if(commandData == -1) {
   1379             if (!pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated &&
   1380                 !pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated){
   1381 
   1382                 /* return cmdcomplete event if inout & output unpopulated */
   1383                 pComponentPrivate->cbInfo.EventHandler(
   1384                      pHandle, pHandle->pApplicationPrivate,
   1385                      OMX_EventCmdComplete, OMX_CommandPortDisable,NBAMRENC_INPUT_PORT, NULL);
   1386 
   1387                 pComponentPrivate->cbInfo.EventHandler(
   1388                      pHandle, pHandle->pApplicationPrivate,
   1389                      OMX_EventCmdComplete, OMX_CommandPortDisable,NBAMRENC_OUTPUT_PORT, NULL);
   1390                 pComponentPrivate->bDisableCommandPending = 0;
   1391             }
   1392             else {
   1393                 pComponentPrivate->bDisableCommandPending = 1;
   1394                 pComponentPrivate->bDisableCommandParam = commandData;
   1395             }
   1396         }
   1397 #ifndef UNDER_CE
   1398         sched_yield();
   1399 #endif
   1400     }
   1401     else if (command == OMX_CommandPortEnable) {
   1402         if(!pComponentPrivate->bEnableCommandPending) {
   1403             if(commandData == 0x0 || commandData == -1){
   1404                 /* enable in port */
   1405                 OMX_PRCOMM2(pComponentPrivate->dbg, "setting input port to enabled\n");
   1406                 pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled = OMX_TRUE;
   1407                 if(pComponentPrivate->AlloBuf_waitingsignal)
   1408                 {
   1409                      pComponentPrivate->AlloBuf_waitingsignal = 0;
   1410 
   1411                 }
   1412                 OMX_PRCOMM2(pComponentPrivate->dbg, "pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled = %d\n",pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled);
   1413             }
   1414             if(commandData == 0x1 || commandData == -1){
   1415                 char *pArgs = "damedesuStr";
   1416                 /* enable out port */
   1417                 if(pComponentPrivate->AlloBuf_waitingsignal)
   1418                 {
   1419                      pComponentPrivate->AlloBuf_waitingsignal = 0;
   1420 #ifndef UNDER_CE
   1421                      pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
   1422                      pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
   1423                      pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
   1424 #else
   1425                      OMX_SignalEvent(&(pComponentPrivate->AlloBuf_event));
   1426 #endif
   1427                 }
   1428                 if (pComponentPrivate->curState == OMX_StateExecuting) {
   1429                     pComponentPrivate->bDspStoppedWhileExecuting = OMX_FALSE;
   1430                     eError = LCML_ControlCodec(
   1431                                           ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1432                                           EMMCodecControlStart,(void *)pArgs);
   1433                 }
   1434                 OMX_PRCOMM2(pComponentPrivate->dbg, "setting output port to enabled\n");
   1435                 pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled = OMX_TRUE;
   1436                 OMX_PRCOMM2(pComponentPrivate->dbg, "pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled = %d\n",pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled);
   1437             }
   1438         }
   1439           if(commandData == 0x0 ){
   1440                 if (pComponentPrivate->curState == OMX_StateLoaded || pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated){
   1441                 pComponentPrivate->cbInfo.EventHandler(
   1442                      pHandle, pHandle->pApplicationPrivate,
   1443                      OMX_EventCmdComplete, OMX_CommandPortEnable,NBAMRENC_INPUT_PORT, NULL);
   1444                      pComponentPrivate->bEnableCommandPending = 0;
   1445                     }
   1446                 else{
   1447                     pComponentPrivate->bEnableCommandPending = 1;
   1448                     pComponentPrivate->bEnableCommandParam = commandData;
   1449                 }
   1450               }
   1451             else if(commandData == 0x1){
   1452                 if (pComponentPrivate->curState == OMX_StateLoaded || pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated){
   1453                 pComponentPrivate->cbInfo.EventHandler(
   1454                      pHandle, pHandle->pApplicationPrivate,
   1455                      OMX_EventCmdComplete, OMX_CommandPortEnable,NBAMRENC_OUTPUT_PORT, NULL);
   1456                      pComponentPrivate->bEnableCommandPending = 0;
   1457                 }
   1458                 else{
   1459                      pComponentPrivate->bEnableCommandPending = 1;
   1460                      pComponentPrivate->bEnableCommandParam = commandData;
   1461                 }
   1462             }
   1463             else if(commandData == -1){
   1464                 if(pComponentPrivate->curState == OMX_StateLoaded || (pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated
   1465                     && pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated)){
   1466                     pComponentPrivate->cbInfo.EventHandler(
   1467                      pHandle, pHandle->pApplicationPrivate,
   1468                      OMX_EventCmdComplete, OMX_CommandPortEnable,NBAMRENC_INPUT_PORT, NULL);
   1469 
   1470                     pComponentPrivate->cbInfo.EventHandler(
   1471                      pHandle, pHandle->pApplicationPrivate,
   1472                      OMX_EventCmdComplete, OMX_CommandPortEnable,NBAMRENC_OUTPUT_PORT, NULL);
   1473 
   1474                     pComponentPrivate->bEnableCommandPending = 0;
   1475                     NBAMRENC_FillLCMLInitParamsEx(pComponentPrivate->pHandle);
   1476                 }
   1477                 else {
   1478                     pComponentPrivate->bEnableCommandPending = 1;
   1479                     pComponentPrivate->bEnableCommandParam = commandData;
   1480                 }
   1481             }
   1482 #ifndef UNDER_CE
   1483                     pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
   1484                     pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
   1485                     pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
   1486 #else
   1487                     OMX_SignalEvent(&(pComponentPrivate->AlloBuf_event));
   1488 #endif
   1489 
   1490     } else if (command == OMX_CommandFlush) {
   1491           if(commandData == 0x0 || commandData == -1){/*input*/
   1492                     OMX_PRCOMM2(pComponentPrivate->dbg, "Flushing input port %d\n",__LINE__);
   1493                     for (i=0; i < NBAMRENC_MAX_NUM_OF_BUFS; i++) {
   1494                         pComponentPrivate->pInputBufHdrPending[i] = NULL;
   1495                     }
   1496                     pComponentPrivate->nNumInputBufPending=0;
   1497 
   1498             for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
   1499 
   1500 
   1501 #ifdef __PERF_INSTRUMENTATION__
   1502                             PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1503                                               pComponentPrivate->pInputBufferList->pBufHdr[i]->pBuffer,
   1504                                               0,
   1505                                               PERF_ModuleHLMM);
   1506 #endif
   1507                        pComponentPrivate->cbInfo.EmptyBufferDone (
   1508                                        pComponentPrivate->pHandle,
   1509                                        pComponentPrivate->pHandle->pApplicationPrivate,
   1510                                        pComponentPrivate->pInputBufferList->pBufHdr[i]
   1511                                        );
   1512                     pComponentPrivate->nEmptyBufferDoneCount++;
   1513                     pComponentPrivate->nOutStandingEmptyDones--;
   1514 
   1515                     }
   1516                     pComponentPrivate->cbInfo.EventHandler(
   1517                          pHandle, pHandle->pApplicationPrivate,
   1518                          OMX_EventCmdComplete, OMX_CommandFlush,NBAMRENC_INPUT_PORT, NULL);
   1519           }
   1520 
   1521           if(commandData == 0x1 || commandData == -1){/*output*/
   1522                      for (i=0; i < NBAMRENC_MAX_NUM_OF_BUFS; i++) {
   1523                         pComponentPrivate->pOutputBufHdrPending[i] = NULL;
   1524                     }
   1525                     pComponentPrivate->nNumOutputBufPending=0;
   1526 
   1527                     for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
   1528 #ifdef __PERF_INSTRUMENTATION__
   1529                     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1530                                       pComponentPrivate->pOutputBufferList->pBufHdr[i]->pBuffer,
   1531                                       pComponentPrivate->pOutputBufferList->pBufHdr[i]->nFilledLen,
   1532                                       PERF_ModuleHLMM);
   1533 #endif
   1534                        pComponentPrivate->cbInfo.FillBufferDone (
   1535                                        pComponentPrivate->pHandle,
   1536                                        pComponentPrivate->pHandle->pApplicationPrivate,
   1537                                        pComponentPrivate->pOutputBufferList->pBufHdr[i]
   1538                                        );
   1539                         pComponentPrivate->nFillBufferDoneCount++;
   1540                         pComponentPrivate->nOutStandingFillDones--;
   1541 
   1542                     }
   1543                      pComponentPrivate->cbInfo.EventHandler(
   1544                              pHandle, pHandle->pApplicationPrivate,
   1545                              OMX_EventCmdComplete, OMX_CommandFlush,NBAMRENC_OUTPUT_PORT, NULL);
   1546           }
   1547     }
   1548 
   1549 EXIT:
   1550     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_HandleCommand Function\n",__LINE__);
   1551     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
   1552     if (eError != OMX_ErrorNone && eError != NBAMRENC_EXIT_COMPONENT_THRD) {
   1553         pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   1554                                                pComponentPrivate->pHandle->pApplicationPrivate,
   1555                                                OMX_EventError,
   1556                                                eError,
   1557                                                OMX_TI_ErrorSevere,
   1558                                                NULL);
   1559     }
   1560     return eError;
   1561 }
   1562 
   1563 /* ========================================================================== */
   1564 /**
   1565 * @NBAMRENC_HandleDataBufFromApp() This function is called by the component when ever it
   1566 * receives the buffer from the application
   1567 *
   1568 * @param pComponentPrivate  Component private data
   1569 * @param pBufHeader Buffer from the application
   1570 *
   1571 * @pre
   1572 *
   1573 * @post
   1574 *
   1575 * @return none
   1576 */
   1577 /* ========================================================================== */
   1578 OMX_ERRORTYPE NBAMRENC_HandleDataBufFromApp(OMX_BUFFERHEADERTYPE* pBufHeader,
   1579                                     AMRENC_COMPONENT_PRIVATE *pComponentPrivate)
   1580 {
   1581     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1582     OMX_DIRTYPE eDir;
   1583     NBAMRENC_LCML_BUFHEADERTYPE *pLcmlHdr = NULL;
   1584     LCML_DSP_INTERFACE *pLcmlHandle = (LCML_DSP_INTERFACE *)
   1585                                               pComponentPrivate->pLcmlHandle;
   1586     OMX_U32 frameLength, remainingBytes;
   1587     OMX_U8* pExtraData = NULL;
   1588     OMX_U8 nFrames=0,i;
   1589     LCML_DSP_INTERFACE * phandle = NULL;
   1590 
   1591     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering NBAMRENC_HandleDataBufFromApp Function\n",__LINE__);
   1592     /*Find the direction of the received buffer from buffer list */
   1593         eError = NBAMRENC_GetBufferDirection(pBufHeader, &eDir);
   1594         if (eError != OMX_ErrorNone) {
   1595                 OMX_ERROR4(pComponentPrivate->dbg, "%d :: The pBufHeader is not found in the list\n", __LINE__);
   1596                 goto EXIT;
   1597         }
   1598 
   1599     if (eDir == OMX_DirInput) {
   1600                 pComponentPrivate->nEmptyThisBufferCount++;
   1601                 pComponentPrivate->nUnhandledEmptyThisBuffers--;
   1602                 if  (pBufHeader->nFilledLen > 0) {
   1603                         if (pComponentPrivate->nHoldLength == 0) {
   1604                                 frameLength = NBAMRENC_INPUT_FRAME_SIZE;
   1605                                 nFrames = (OMX_U8)(pBufHeader->nFilledLen / frameLength);
   1606                                 if ( nFrames>=1 ) { /*At least there is 1 frame in the buffer*/
   1607                                         pComponentPrivate->nHoldLength = pBufHeader->nFilledLen - frameLength*nFrames;
   1608                                         if (pComponentPrivate->nHoldLength > 0) {/* something need to be hold in pHoldBuffer */
   1609                                                 if (pComponentPrivate->pHoldBuffer == NULL) {
   1610                                                    OMX_MALLOC_SIZE(pComponentPrivate->pHoldBuffer, NBAMRENC_INPUT_FRAME_SIZE,OMX_U8);
   1611                                                 }
   1612 
   1613                                                 /* Copy the extra data into pHoldBuffer. Size will be nHoldLength. */
   1614                                                 pExtraData = pBufHeader->pBuffer + frameLength*nFrames;
   1615 
   1616                         if(pComponentPrivate->nHoldLength <= NBAMRENC_INPUT_FRAME_SIZE) {
   1617                             memcpy(pComponentPrivate->pHoldBuffer, pExtraData,  pComponentPrivate->nHoldLength);
   1618                         }
   1619                         else {
   1620                             OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: pHoldLenght is bigger than the input frame size\n", __LINE__);
   1621                                     goto EXIT;
   1622                         }
   1623 
   1624                                                 pBufHeader->nFilledLen-=pComponentPrivate->nHoldLength;
   1625                                         }
   1626                                 }
   1627                                 else {
   1628                                         if( !pComponentPrivate->InBuf_Eos_alreadysent ){
   1629                                                 /* received buffer with less than 1 AMR frame length. Save the data in pHoldBuffer.*/
   1630                                                 pComponentPrivate->nHoldLength = pBufHeader->nFilledLen;
   1631                                                 /* save the data into pHoldBuffer */
   1632                                                 if (pComponentPrivate->pHoldBuffer == NULL) {
   1633                                                     OMX_MALLOC_SIZE(pComponentPrivate->pHoldBuffer, NBAMRENC_INPUT_FRAME_SIZE,OMX_U8);
   1634                                                 }
   1635 
   1636                                                 if(pComponentPrivate->nHoldLength <= NBAMRENC_INPUT_FRAME_SIZE) {
   1637                                                     memcpy(pComponentPrivate->pHoldBuffer, pBufHeader->pBuffer, pComponentPrivate->nHoldLength);
   1638                                                 }
   1639                                                 else {
   1640                                                     OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: pHoldLenght is bigger than the input frame size\n", __LINE__);
   1641                                                     goto EXIT;
   1642                                                 }
   1643                                         }
   1644                                         /* since not enough data, we shouldn't send anything to SN, but instead request to EmptyBufferDone again.*/
   1645                                         if (pComponentPrivate->curState != OMX_StatePause ) {
   1646                                                 OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Calling EmptyBufferDone\n",__LINE__);
   1647 
   1648 #ifdef __PERF_INSTRUMENTATION__
   1649                             PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1650                                               pBufHeader->pBuffer,
   1651                                               0,
   1652                                               PERF_ModuleHLMM);
   1653 #endif
   1654 
   1655                                               pComponentPrivate->cbInfo.EmptyBufferDone( pComponentPrivate->pHandle,
   1656                                                                                                                 pComponentPrivate->pHandle->pApplicationPrivate,
   1657                                                                                                                 pBufHeader);
   1658                                               pComponentPrivate->nEmptyBufferDoneCount++;
   1659 
   1660                                         }
   1661                                         else {
   1662                                                 pComponentPrivate->pInputBufHdrPending[pComponentPrivate->nNumInputBufPending++] = pBufHeader;
   1663                                         }
   1664 
   1665                                         pComponentPrivate->ProcessingInputBuf--;
   1666                                         goto EXIT;
   1667 
   1668                                 }
   1669                         }
   1670                         else {
   1671                                 if((pComponentPrivate->nHoldLength+pBufHeader->nFilledLen) > pBufHeader->nAllocLen){
   1672                                         /*means that a second Acumulator must be used to insert holdbuffer to pbuffer and save remaining bytes
   1673                                                      into hold buffer*/
   1674                                         remainingBytes = pComponentPrivate->nHoldLength+pBufHeader->nFilledLen-pBufHeader->nAllocLen;
   1675                                         if (pComponentPrivate->pHoldBuffer2 == NULL) {
   1676                                                         OMX_MALLOC_SIZE(pComponentPrivate->pHoldBuffer2, NBAMRENC_INPUT_FRAME_SIZE,OMX_U8);
   1677                                         }
   1678                                         pExtraData = (pBufHeader->pBuffer)+(pBufHeader->nFilledLen-remainingBytes);
   1679                                         memcpy(pComponentPrivate->pHoldBuffer2,pExtraData,remainingBytes);
   1680                                         pBufHeader->nFilledLen-=remainingBytes;
   1681                                         memmove(pBufHeader->pBuffer+ pComponentPrivate->nHoldLength,pBufHeader->pBuffer,pBufHeader->nFilledLen);
   1682                                         memcpy(pBufHeader->pBuffer,pComponentPrivate->pHoldBuffer,pComponentPrivate->nHoldLength);
   1683                                         pBufHeader->nFilledLen+=pComponentPrivate->nHoldLength;
   1684                                         memcpy(pComponentPrivate->pHoldBuffer, pComponentPrivate->pHoldBuffer2, remainingBytes);
   1685                                         pComponentPrivate->nHoldLength=remainingBytes;
   1686                                 }
   1687                                 else{
   1688                                         memmove(pBufHeader->pBuffer+pComponentPrivate->nHoldLength, pBufHeader->pBuffer, pBufHeader->nFilledLen);
   1689                                         memcpy(pBufHeader->pBuffer,pComponentPrivate->pHoldBuffer, pComponentPrivate->nHoldLength);
   1690                                         pBufHeader->nFilledLen+=pComponentPrivate->nHoldLength;
   1691                                         pComponentPrivate->nHoldLength=0;
   1692                                 }
   1693                                 frameLength = NBAMRENC_INPUT_FRAME_SIZE;
   1694                                 nFrames = (OMX_U8)(pBufHeader->nFilledLen / frameLength);
   1695                                 pComponentPrivate->nHoldLength = pBufHeader->nFilledLen - frameLength*nFrames;
   1696                                 pExtraData = pBufHeader->pBuffer + pBufHeader->nFilledLen-pComponentPrivate->nHoldLength;
   1697                                 memcpy(pComponentPrivate->pHoldBuffer, pExtraData,  pComponentPrivate->nHoldLength);
   1698                                 pBufHeader->nFilledLen-=pComponentPrivate->nHoldLength;
   1699                                 if(nFrames < 1 ){
   1700                                         if (pComponentPrivate->curState != OMX_StatePause ) {
   1701                                                 OMX_PRBUFFER2(pComponentPrivate->dbg, "line %d:: Calling EmptyBufferDone\n",__LINE__);
   1702 
   1703 #ifdef __PERF_INSTRUMENTATION__
   1704                                                         PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1705                                                                         pBufHeader->pBuffer,
   1706                                                                         0,
   1707                                                                         PERF_ModuleHLMM);
   1708 #endif
   1709 
   1710                                                 pComponentPrivate->cbInfo.EmptyBufferDone( pComponentPrivate->pHandle,
   1711                                                                                                                 pComponentPrivate->pHandle->pApplicationPrivate,
   1712                                                                                                                 pBufHeader);
   1713                                                 pComponentPrivate->nEmptyBufferDoneCount++;
   1714 
   1715                                         }
   1716                                         else {
   1717                                                 pComponentPrivate->pInputBufHdrPending[pComponentPrivate->nNumInputBufPending++] = pBufHeader;
   1718                                         }
   1719                                         goto EXIT;
   1720                                 }
   1721                         }
   1722                 }else{
   1723                         if((pBufHeader->nFlags&OMX_BUFFERFLAG_EOS) != OMX_BUFFERFLAG_EOS){
   1724                             if (pComponentPrivate->dasfMode == 0 && !pBufHeader->pMarkData) {
   1725 #ifdef __PERF_INSTRUMENTATION__
   1726                                 PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1727                                               pComponentPrivate->pInputBufferList->pBufHdr[0]->pBuffer,
   1728                                               0,
   1729                                               PERF_ModuleHLMM);
   1730 #endif
   1731 
   1732                                 pComponentPrivate->cbInfo.EmptyBufferDone( pComponentPrivate->pHandle,
   1733                                                                                 pComponentPrivate->pHandle->pApplicationPrivate,
   1734                                                                                 pComponentPrivate->pInputBufferList->pBufHdr[0]);
   1735                                 pComponentPrivate->nEmptyBufferDoneCount++;
   1736                                 pComponentPrivate->ProcessingInputBuf--;
   1737 
   1738                                 }
   1739                         }
   1740                         else{
   1741                frameLength = NBAMRENC_INPUT_FRAME_SIZE;
   1742                             nFrames=1;
   1743                             }
   1744                 }
   1745                 if(nFrames >= 1){
   1746                 eError = NBAMRENC_GetCorrespondingLCMLHeader(pComponentPrivate,pBufHeader->pBuffer, OMX_DirInput, &pLcmlHdr);
   1747                 if (eError != OMX_ErrorNone) {
   1748                         OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error: Invalid Buffer Came ...\n",__LINE__);
   1749                         goto EXIT;
   1750                 }
   1751 
   1752 #ifdef __PERF_INSTRUMENTATION__
   1753     /*For Steady State Instumentation*/
   1754     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1755                       PREF(pBufHeader,pBuffer),
   1756                       pComponentPrivate->pPortDef[OMX_DirInput]->nBufferSize,
   1757                       PERF_ModuleCommonLayer);
   1758 #endif
   1759 
   1760 /*---------------------------------------------------------------*/
   1761 
   1762                 pComponentPrivate->nNumOfFramesSent = nFrames;
   1763 
   1764                 phandle = (LCML_DSP_INTERFACE *)(((LCML_CODEC_INTERFACE *)pLcmlHandle->pCodecinterfacehandle)->pCodec);
   1765 
   1766                 if( (pLcmlHdr->pBufferParam->usNbFrames < nFrames) && (pLcmlHdr->pFrameParam!=NULL) ){
   1767                         OMX_MEMFREE_STRUCT_DSPALIGN(pLcmlHdr->pFrameParam, NBAMRENC_FrameStruct);
   1768                         OMX_DmmUnMap(phandle->dspCodec->hProc, /*Unmap DSP memory used*/
   1769                                    (void*)pLcmlHdr->pBufferParam->pParamElem,
   1770                                    pLcmlHdr->pDmmBuf->pReserved, pComponentPrivate->dbg);
   1771                         pLcmlHdr->pBufferParam->pParamElem = NULL;
   1772                 }
   1773 
   1774                 if(pLcmlHdr->pFrameParam==NULL ){
   1775                         OMX_MALLOC_SIZE_DSPALIGN(pLcmlHdr->pFrameParam, (sizeof(NBAMRENC_FrameStruct)*nFrames),OMX_U8);
   1776                         eError = OMX_DmmMap(phandle->dspCodec->hProc,
   1777                                         nFrames*sizeof(NBAMRENC_FrameStruct),
   1778                                         (void*)pLcmlHdr->pFrameParam,
   1779                                         (pLcmlHdr->pDmmBuf), pComponentPrivate->dbg);
   1780                         if (eError != OMX_ErrorNone){
   1781                                 OMX_ERROR4(pComponentPrivate->dbg, "OMX_DmmMap ERRROR!!!!\n\n");
   1782                                 goto EXIT;
   1783                         }
   1784                         pLcmlHdr->pBufferParam->pParamElem = (NBAMRENC_FrameStruct *)pLcmlHdr->pDmmBuf->pMapped; /*DSP Address*/
   1785                 }
   1786 
   1787                 for(i=0;i<nFrames;i++){
   1788                         (pLcmlHdr->pFrameParam+i)->usLastFrame = 0;
   1789                 }
   1790 
   1791                 if((pBufHeader->nFlags & OMX_BUFFERFLAG_EOS)  == OMX_BUFFERFLAG_EOS) {
   1792                         (pLcmlHdr->pFrameParam+(nFrames-1))->usLastFrame = OMX_BUFFERFLAG_EOS;
   1793                         pComponentPrivate->InBuf_Eos_alreadysent = 1; /*TRUE*/
   1794                         if(pComponentPrivate->dasfMode == 0) {
   1795                                if(!pBufHeader->nFilledLen){
   1796                                      pComponentPrivate->pOutputBufferList->pBufHdr[0]->nFlags |= OMX_BUFFERFLAG_EOS;
   1797                                }
   1798                                 pComponentPrivate->cbInfo.EventHandler( pComponentPrivate->pHandle,
   1799                                                                                         pComponentPrivate->pHandle->pApplicationPrivate,
   1800                                                                                         OMX_EventBufferFlag,
   1801                                                                                         pComponentPrivate->pOutputBufferList->pBufHdr[0]->nOutputPortIndex,
   1802                                                                                         pComponentPrivate->pOutputBufferList->pBufHdr[0]->nFlags, NULL);
   1803                         }
   1804                         pBufHeader->nFlags = 0;
   1805                 }
   1806                 pLcmlHdr->pBufferParam->usNbFrames = nFrames;
   1807 /*---------------------------------------------------------------*/
   1808             /* Store time stamp information */
   1809                 /*pComponentPrivate->arrBufIndex[pComponentPrivate->IpBufindex] = pBufHeader->nTimeStamp;*/
   1810                 if (!pComponentPrivate->bFirstInputBufReceived) {
   1811                     /* Reset TimeStamp when first input buffer received */
   1812                     pComponentPrivate->TimeStamp = 0;
   1813                     /* First Input buffer received */
   1814                     pComponentPrivate->bFirstInputBufReceived = OMX_TRUE;
   1815                 }
   1816             /* Store nTickCount information */
   1817                         pComponentPrivate->arrTickCount[pComponentPrivate->IpBufindex] = pBufHeader->nTickCount;
   1818                         pComponentPrivate->IpBufindex++;
   1819                         pComponentPrivate->IpBufindex %= pComponentPrivate->pPortDef[OMX_DirOutput]->nBufferCountActual;
   1820 
   1821                         if (pComponentPrivate->curState == OMX_StateExecuting) {
   1822                                 if(!pComponentPrivate->bDspStoppedWhileExecuting)
   1823                                 {
   1824                                         if (!NBAMRENC_IsPending(pComponentPrivate,pBufHeader,OMX_DirInput)) {
   1825                                                 NBAMRENC_SetPending(pComponentPrivate,pBufHeader,OMX_DirInput,__LINE__);
   1826 /*              if (pLcmlHdr->buffer->nFilledLen != 0)
   1827                     fwrite(pLcmlHdr->buffer->pBuffer, 1, pLcmlHdr->buffer->nFilledLen, fOut);
   1828 */
   1829                 /* fflush(fOut); */
   1830 
   1831 /*
   1832                 fclose(fOut);
   1833 */
   1834 
   1835                                                 eError = LCML_QueueBuffer( pLcmlHandle->pCodecinterfacehandle,
   1836                                                                                                 EMMCodecInputBuffer,
   1837                                                                                                 (OMX_U8 *)pBufHeader->pBuffer,
   1838                                                                                                 pBufHeader->nAllocLen,
   1839                                                                                                 pBufHeader->nFilledLen,
   1840                                                                                                 (OMX_U8 *) pLcmlHdr->pBufferParam,
   1841                                                                                                 sizeof(NBAMRENC_ParamStruct),
   1842                                                                                                 NULL);
   1843                                                 if (eError != OMX_ErrorNone) {
   1844                                                         eError = OMX_ErrorHardware;
   1845                                                         goto EXIT;
   1846                                                 }
   1847                                                 pComponentPrivate->lcml_nIpBuf++;
   1848                                         }
   1849                                     }
   1850                         }
   1851                         else if(pComponentPrivate->curState == OMX_StatePause){
   1852                                 pComponentPrivate->pInputBufHdrPending[pComponentPrivate->nNumInputBufPending++] = pBufHeader;
   1853                         }
   1854                         pComponentPrivate->ProcessingInputBuf--;
   1855           }
   1856 /******************************************************************************/
   1857         if(pBufHeader->pMarkData){
   1858                 if(pComponentPrivate->pOutputBufferList->pBufHdr[0]!=NULL) {
   1859                        /* copy mark to output buffer header */
   1860                        pComponentPrivate->pOutputBufferList->pBufHdr[0]->pMarkData = pBufHeader->pMarkData;
   1861                        pComponentPrivate->pOutputBufferList->pBufHdr[0]->hMarkTargetComponent = pBufHeader->hMarkTargetComponent;
   1862                 }
   1863                 /* trigger event handler if we are supposed to */
   1864                 if(pBufHeader->hMarkTargetComponent == pComponentPrivate->pHandle && pBufHeader->pMarkData){
   1865                         pComponentPrivate->cbInfo.EventHandler( pComponentPrivate->pHandle,
   1866                                                                                         pComponentPrivate->pHandle->pApplicationPrivate,
   1867                                                                                         OMX_EventMark,
   1868                                                                                         0,
   1869                                                                                         0,
   1870                                                                                         pBufHeader->pMarkData);
   1871                 }
   1872                 if (pComponentPrivate->curState != OMX_StatePause && !NBAMRENC_IsPending(pComponentPrivate,pBufHeader,OMX_DirInput)) {
   1873                     OMX_PRBUFFER2(pComponentPrivate->dbg, "line %d:: Calling EmptyBufferDone\n",__LINE__);
   1874 
   1875 #ifdef __PERF_INSTRUMENTATION__
   1876                             PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1877                                             pBufHeader->pBuffer,
   1878                                             0,
   1879                                             PERF_ModuleHLMM);
   1880 #endif
   1881 
   1882                     pComponentPrivate->cbInfo.EmptyBufferDone( pComponentPrivate->pHandle,
   1883                                                                                     pComponentPrivate->pHandle->pApplicationPrivate,
   1884                                                                                     pBufHeader);
   1885                     pComponentPrivate->nEmptyBufferDoneCount++;
   1886 
   1887                 }
   1888         }
   1889 
   1890         if (pComponentPrivate->bFlushInputPortCommandPending) {
   1891                 OMX_SendCommand(pComponentPrivate->pHandle,OMX_CommandFlush,0,NULL);
   1892             }
   1893 
   1894     } else if (eDir == OMX_DirOutput) {
   1895                 /* Make sure that output buffer is issued to output stream only when
   1896                 * there is an outstanding input buffer already issued on input stream
   1897                 */
   1898 
   1899 /***--------------------------------------****/
   1900      pComponentPrivate->nUnhandledFillThisBuffers--;
   1901      nFrames = pComponentPrivate->nNumOfFramesSent;
   1902      if(nFrames == 0)
   1903            nFrames = 1;
   1904 
   1905      eError = NBAMRENC_GetCorrespondingLCMLHeader(pComponentPrivate,pBufHeader->pBuffer, OMX_DirOutput, &pLcmlHdr);
   1906 
   1907      phandle = (LCML_DSP_INTERFACE *)(((LCML_CODEC_INTERFACE *)pLcmlHandle->pCodecinterfacehandle)->pCodec);
   1908 
   1909      if( (pLcmlHdr->pBufferParam->usNbFrames < nFrames) && (pLcmlHdr->pFrameParam!=NULL) ){
   1910                    OMX_MEMFREE_STRUCT_DSPALIGN(pLcmlHdr->pFrameParam, NBAMRENC_FrameStruct);
   1911 #ifndef UNDER_CE
   1912                    OMX_DmmUnMap(phandle->dspCodec->hProc,
   1913                                    (void*)pLcmlHdr->pBufferParam->pParamElem,
   1914                                    pLcmlHdr->pDmmBuf->pReserved, pComponentPrivate->dbg);
   1915 #endif
   1916 
   1917                    pLcmlHdr->pBufferParam->pParamElem = NULL;
   1918          }
   1919 
   1920      if(pLcmlHdr->pFrameParam==NULL ){
   1921                    OMX_MALLOC_SIZE_DSPALIGN(pLcmlHdr->pFrameParam, (sizeof(NBAMRENC_FrameStruct)*nFrames ),OMX_U8);
   1922 #ifndef UNDER_CE
   1923                    eError = OMX_DmmMap(phandle->dspCodec->hProc,
   1924                                        nFrames*sizeof(NBAMRENC_FrameStruct),
   1925                                        (void*)pLcmlHdr->pFrameParam,
   1926                                        (pLcmlHdr->pDmmBuf), pComponentPrivate->dbg);
   1927 
   1928                    if (eError != OMX_ErrorNone)
   1929                    {
   1930                                OMX_ERROR4(pComponentPrivate->dbg, "OMX_DmmMap ERRROR!!!!\n");
   1931                                goto EXIT;
   1932                    }
   1933                    pLcmlHdr->pBufferParam->pParamElem = (NBAMRENC_FrameStruct *)pLcmlHdr->pDmmBuf->pMapped; /*DSP Address*/
   1934 #endif
   1935          }
   1936 
   1937      pLcmlHdr->pBufferParam->usNbFrames = nFrames;
   1938 
   1939                         if (pComponentPrivate->curState == OMX_StateExecuting) {
   1940                                 if (!NBAMRENC_IsPending(pComponentPrivate,pBufHeader,OMX_DirOutput)) {
   1941                                                 NBAMRENC_SetPending(pComponentPrivate,pBufHeader,OMX_DirOutput,__LINE__);
   1942                                                 eError = LCML_QueueBuffer( pLcmlHandle->pCodecinterfacehandle,
   1943                                                                                                 EMMCodecOuputBuffer,
   1944                                                                                                 (OMX_U8 *)pBufHeader->pBuffer,
   1945                                                                                                 NBAMRENC_OUTPUT_FRAME_SIZE * nFrames,
   1946                                                                                                 0,
   1947                                                                                                 (OMX_U8 *) pLcmlHdr->pBufferParam,
   1948                                                                                                 sizeof(NBAMRENC_ParamStruct),
   1949                                                                                                 NULL);
   1950                                                 OMX_PRBUFFER1(pComponentPrivate->dbg, "After QueueBuffer Line %d\n",__LINE__);
   1951                                                 if (eError != OMX_ErrorNone ) {
   1952                                                         OMX_ERROR4(pComponentPrivate->dbg, "%d :: Issuing DSP OP: Error Occurred\n",__LINE__);
   1953                                                         eError = OMX_ErrorHardware;
   1954                                                         goto EXIT;
   1955                                                 }
   1956                                                 pComponentPrivate->lcml_nOpBuf++;
   1957                                                 pComponentPrivate->num_Op_Issued++;
   1958                                         }
   1959                                 }
   1960                         else  if (pComponentPrivate->curState == OMX_StatePause){
   1961                                 pComponentPrivate->pOutputBufHdrPending[pComponentPrivate->nNumOutputBufPending++] = pBufHeader;
   1962                         }
   1963                         pComponentPrivate->ProcessingOutputBuf--;
   1964 
   1965                         if (pComponentPrivate->bFlushOutputPortCommandPending) {
   1966                                   OMX_SendCommand( pComponentPrivate->pHandle,
   1967                                   OMX_CommandFlush,
   1968                                   1,NULL);
   1969         }
   1970 
   1971     }
   1972     else {
   1973         eError = OMX_ErrorBadParameter;
   1974     }
   1975 
   1976 EXIT:
   1977     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting from  NBAMRENC_HandleDataBufFromApp \n",__LINE__);
   1978     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning error %d\n",__LINE__,eError);
   1979 
   1980     return eError;
   1981 }
   1982 
   1983 /*-------------------------------------------------------------------*/
   1984 /**
   1985 * NBAMRENC_GetBufferDirection () This function is used by the component
   1986 * to get the direction of the buffer
   1987 * @param eDir pointer will be updated with buffer direction
   1988 * @param pBufHeader pointer to the buffer to be requested to be filled
   1989 *
   1990 * @retval none
   1991 **/
   1992 /*-------------------------------------------------------------------*/
   1993 
   1994 OMX_ERRORTYPE NBAMRENC_GetBufferDirection(OMX_BUFFERHEADERTYPE *pBufHeader,
   1995                                                          OMX_DIRTYPE *eDir)
   1996 {
   1997     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1998     AMRENC_COMPONENT_PRIVATE *pComponentPrivate = pBufHeader->pPlatformPrivate;
   1999     OMX_U32 nBuf = 0;
   2000     OMX_BUFFERHEADERTYPE *pBuf = NULL;
   2001     OMX_U16 flag = 1,i = 0;
   2002     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering NBAMRENC_GetBufferDirection Function\n",__LINE__);
   2003     /*Search this buffer in input buffers list */
   2004     nBuf = pComponentPrivate->pInputBufferList->numBuffers;
   2005     for(i=0; i<nBuf; i++) {
   2006         pBuf = pComponentPrivate->pInputBufferList->pBufHdr[i];
   2007         if(pBufHeader == pBuf) {
   2008             *eDir = OMX_DirInput;
   2009             OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBufHeader = %p is INPUT BUFFER pBuf = %p\n",__LINE__,pBufHeader,pBuf);
   2010             flag = 0;
   2011             goto EXIT;
   2012         }
   2013     }
   2014     /*Search this buffer in output buffers list */
   2015     nBuf = pComponentPrivate->pOutputBufferList->numBuffers;
   2016     for(i=0; i<nBuf; i++) {
   2017         pBuf = pComponentPrivate->pOutputBufferList->pBufHdr[i];
   2018         if(pBufHeader == pBuf) {
   2019             *eDir = OMX_DirOutput;
   2020             OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBufHeader = %p is OUTPUT BUFFER pBuf = %p\n",__LINE__,pBufHeader,pBuf);
   2021             flag = 0;
   2022             goto EXIT;
   2023         }
   2024     }
   2025 
   2026     if (flag == 1) {
   2027         OMX_ERROR4(pComponentPrivate->dbg, "%d :: Buffer %p is Not Found in the List\n",__LINE__, pBufHeader);
   2028         eError = OMX_ErrorUndefined;
   2029         goto EXIT;
   2030     }
   2031 EXIT:
   2032     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_GetBufferDirection Function\n",__LINE__);
   2033     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
   2034     return eError;
   2035 }
   2036 
   2037 /* -------------------------------------------------------------------*/
   2038 /**
   2039   * NBAMRENC_GetCorrespondingLCMLHeader() function will be called by LCML_Callback
   2040   * component to write the msg
   2041   * @param *pBuffer,          Event which gives to details about USN status
   2042   * @param NBAMRENC_LCML_BUFHEADERTYPE **ppLcmlHdr
   2043   * @param  OMX_DIRTYPE eDir this gives direction of the buffer
   2044   * @retval OMX_NoError              Success, ready to roll
   2045   *         OMX_Error_BadParameter   The input parameter pointer is null
   2046  **/
   2047 /* -------------------------------------------------------------------*/
   2048 OMX_ERRORTYPE NBAMRENC_GetCorrespondingLCMLHeader(AMRENC_COMPONENT_PRIVATE *pComponentPrivate,
   2049                                                   OMX_U8 *pBuffer,
   2050                                                   OMX_DIRTYPE eDir,
   2051                                                   NBAMRENC_LCML_BUFHEADERTYPE **ppLcmlHdr)
   2052 {
   2053     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2054     NBAMRENC_LCML_BUFHEADERTYPE *pLcmlBufHeader;
   2055     OMX_S16 nIpBuf;
   2056     OMX_S16 nOpBuf;
   2057     OMX_S16 i;
   2058 
   2059     AMRENC_COMPONENT_PRIVATE *pComponentPrivate_CC;
   2060 
   2061     pComponentPrivate_CC = (AMRENC_COMPONENT_PRIVATE*) pComponentPrivate;
   2062     nIpBuf = pComponentPrivate_CC->pInputBufferList->numBuffers;
   2063     nOpBuf = pComponentPrivate_CC->pOutputBufferList->numBuffers;
   2064 
   2065     OMX_PRINT1(pComponentPrivate_CC->dbg, "%d :: Entering NBAMRENC_GetCorrespondingLCMLHeader..\n",__LINE__);
   2066     while (!pComponentPrivate_CC->bInitParamsInitialized) {
   2067         OMX_PRSTATE2(pComponentPrivate_CC->dbg, "%d :: Waiting for init to complete........\n",__LINE__);
   2068 #ifndef UNDER_CE
   2069         sched_yield();
   2070 #else
   2071         Sleep(1);
   2072 #endif
   2073     }
   2074     if(eDir == OMX_DirInput) {
   2075         OMX_PRINT2(pComponentPrivate_CC->dbg, "%d :: Entering NBAMRENC_GetCorrespondingLCMLHeader..\n",__LINE__);
   2076         pLcmlBufHeader = pComponentPrivate_CC->pLcmlBufHeader[NBAMRENC_INPUT_PORT];
   2077         for(i = 0; i < nIpBuf; i++) {
   2078             OMX_PRBUFFER2(pComponentPrivate_CC->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
   2079             OMX_PRBUFFER2(pComponentPrivate_CC->dbg, "%d :: pLcmlBufHeader->buffer->pBuffer = %p\n",__LINE__,pLcmlBufHeader->buffer->pBuffer);
   2080             if(pBuffer == pLcmlBufHeader->buffer->pBuffer) {
   2081                 *ppLcmlHdr = pLcmlBufHeader;
   2082                  OMX_PRDSP2(pComponentPrivate_CC->dbg, "%d :: Corresponding Input LCML Header Found = %p\n",__LINE__,pLcmlBufHeader);
   2083                  eError = OMX_ErrorNone;
   2084                  goto EXIT;
   2085             }
   2086             pLcmlBufHeader++;
   2087         }
   2088     } else if (eDir == OMX_DirOutput) {
   2089         OMX_PRINT1(pComponentPrivate_CC->dbg, "%d :: Entering NBAMRENC_GetCorrespondingLCMLHeader..\n",__LINE__);
   2090         pLcmlBufHeader = pComponentPrivate_CC->pLcmlBufHeader[NBAMRENC_OUTPUT_PORT];
   2091         for(i = 0; i < nOpBuf; i++) {
   2092             OMX_PRBUFFER2(pComponentPrivate_CC->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
   2093             OMX_PRBUFFER2(pComponentPrivate_CC->dbg, "%d :: pLcmlBufHeader->buffer->pBuffer = %p\n",__LINE__,pLcmlBufHeader->buffer->pBuffer);
   2094             if(pBuffer == pLcmlBufHeader->buffer->pBuffer) {
   2095                 *ppLcmlHdr = pLcmlBufHeader;
   2096                  OMX_PRDSP2(pComponentPrivate_CC->dbg, "%d :: Corresponding Output LCML Header Found = %p\n",__LINE__,pLcmlBufHeader);
   2097                  eError = OMX_ErrorNone;
   2098                  goto EXIT;
   2099             }
   2100             pLcmlBufHeader++;
   2101         }
   2102     } else {
   2103       OMX_ERROR4(pComponentPrivate_CC->dbg, "%d :: Invalid Buffer Type :: exiting...\n",__LINE__);
   2104       eError = OMX_ErrorUndefined;
   2105     }
   2106 
   2107 EXIT:
   2108     OMX_PRINT1(pComponentPrivate_CC->dbg, "%d :: Exiting NBAMRENC_GetCorrespondingLCMLHeader..\n",__LINE__);
   2109     OMX_PRINT1(pComponentPrivate_CC->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
   2110     return eError;
   2111 }
   2112 
   2113 /* -------------------------------------------------------------------*/
   2114 /**
   2115   *  NBAMRENC_LCMLCallback() will be called LCML component to write the msg
   2116   *
   2117   * @param event                 Event which gives to details about USN status
   2118   * @param void * args        //    args [0] //bufType;
   2119                               //    args [1] //arm address fpr buffer
   2120                               //    args [2] //BufferSize;
   2121                               //    args [3]  //arm address for param
   2122                               //    args [4] //ParamSize;
   2123                               //    args [6] //LCML Handle
   2124   * @retval OMX_NoError              Success, ready to roll
   2125   *         OMX_Error_BadParameter   The input parameter pointer is null
   2126  **/
   2127 /*-------------------------------------------------------------------*/
   2128 
   2129 OMX_ERRORTYPE NBAMRENC_LCMLCallback (TUsnCodecEvent event,void * args[10])
   2130 {
   2131     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2132     OMX_U8 *pBuffer = args[1];
   2133     NBAMRENC_LCML_BUFHEADERTYPE *pLcmlHdr;
   2134     OMX_U16 i,index,frameLength, length;
   2135     OMX_COMPONENTTYPE *pHandle;
   2136     LCML_DSP_INTERFACE *pLcmlHandle;
   2137     OMX_U8 nFrames;
   2138     OMX_U32 buffer_duration = 0;
   2139 
   2140     NBAMRENC_BUFDATA* OutputFrames = NULL;
   2141 
   2142     AMRENC_COMPONENT_PRIVATE* pComponentPrivate_CC = NULL;
   2143     pComponentPrivate_CC = (AMRENC_COMPONENT_PRIVATE*)((LCML_DSP_INTERFACE*)args[6])->pComponentPrivate;
   2144     pHandle = pComponentPrivate_CC->pHandle;
   2145 
   2146 
   2147 #ifdef AMRENC_DEBUG
   2148     switch(event) {
   2149 
   2150         case EMMCodecDspError:
   2151             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecDspError\n");
   2152             break;
   2153 
   2154         case EMMCodecInternalError:
   2155             OMX_ERROR4(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecInternalError\n");
   2156             break;
   2157 
   2158         case EMMCodecInitError:
   2159             OMX_ERROR4(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecInitError\n");
   2160             break;
   2161 
   2162         case EMMCodecDspMessageRecieved:
   2163             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecDspMessageRecieved\n");
   2164             break;
   2165 
   2166         case EMMCodecBufferProcessed:
   2167             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecBufferProcessed\n");
   2168             break;
   2169 
   2170         case EMMCodecProcessingStarted:
   2171             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecProcessingStarted\n");
   2172             break;
   2173 
   2174         case EMMCodecProcessingPaused:
   2175             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecProcessingPaused\n");
   2176             break;
   2177 
   2178         case EMMCodecProcessingStoped:
   2179             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecProcessingStoped\n");
   2180             break;
   2181 
   2182         case EMMCodecProcessingEof:
   2183             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecProcessingEof\n");
   2184             break;
   2185 
   2186         case EMMCodecBufferNotProcessed:
   2187             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecBufferNotProcessed\n");
   2188             break;
   2189 
   2190         case EMMCodecAlgCtrlAck:
   2191             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecAlgCtrlAck\n");
   2192             break;
   2193 
   2194         case EMMCodecStrmCtrlAck:
   2195             OMX_PRDSP2(pComponentPrivate_CC->dbg, "[LCML CALLBACK EVENT]  EMMCodecStrmCtrlAck\n");
   2196             break;
   2197     }
   2198 #endif
   2199 
   2200     if(event == EMMCodecBufferProcessed)
   2201     {
   2202 
   2203         if((OMX_U32)args[0] == EMMCodecInputBuffer) {
   2204                 OMX_PRBUFFER1(pComponentPrivate_CC->dbg, "%d :: INPUT: pBuffer = %p\n",__LINE__, pBuffer);
   2205             eError = NBAMRENC_GetCorrespondingLCMLHeader(pComponentPrivate_CC,pBuffer, OMX_DirInput, &pLcmlHdr);
   2206             if (eError != OMX_ErrorNone) {
   2207                 OMX_ERROR4(pComponentPrivate_CC->dbg, "%d :: Error: Invalid Buffer Came ...\n",__LINE__);
   2208                 goto EXIT;
   2209             }
   2210 #ifdef __PERF_INSTRUMENTATION__
   2211             PERF_ReceivedFrame(pComponentPrivate_CC->pPERFcomp,
   2212                                PREF(pLcmlHdr->buffer,pBuffer),
   2213                                0,
   2214                                PERF_ModuleCommonLayer);
   2215 #endif
   2216 
   2217             NBAMRENC_ClearPending(pComponentPrivate_CC,pLcmlHdr->buffer,OMX_DirInput,__LINE__);
   2218 
   2219 #ifdef __PERF_INSTRUMENTATION__
   2220                                 PERF_SendingFrame(pComponentPrivate_CC->pPERFcomp,
   2221                                               pLcmlHdr->buffer->pBuffer,
   2222                                               0,
   2223                                               PERF_ModuleHLMM);
   2224 #endif
   2225                 pComponentPrivate_CC->cbInfo.EmptyBufferDone( pHandle,
   2226                                                            pHandle->pApplicationPrivate,
   2227                                                            pLcmlHdr->buffer);
   2228                 pComponentPrivate_CC->nEmptyBufferDoneCount++;
   2229                 pComponentPrivate_CC->nOutStandingEmptyDones--;
   2230                 pComponentPrivate_CC->lcml_nIpBuf--;
   2231                 pComponentPrivate_CC->app_nBuf++;
   2232 
   2233         pComponentPrivate_CC->nOutStandingEmptyDones++;
   2234 
   2235         } else if((OMX_U32)args[0] == EMMCodecOuputBuffer) {
   2236 
   2237             if (!NBAMRENC_IsValid(pComponentPrivate_CC,pBuffer,OMX_DirOutput)) {
   2238 
   2239                 for (i=0; i < pComponentPrivate_CC->pOutputBufferList->numBuffers; i++) {
   2240 #ifdef __PERF_INSTRUMENTATION__
   2241                 PERF_SendingFrame(pComponentPrivate_CC->pPERFcomp,
   2242                                   pComponentPrivate_CC->pOutputBufferList->pBufHdr[i]->pBuffer,
   2243                                   pComponentPrivate_CC->pOutputBufferList->pBufHdr[i]->nFilledLen,
   2244                                   PERF_ModuleHLMM);
   2245 #endif
   2246                     pComponentPrivate_CC->cbInfo.FillBufferDone (pComponentPrivate_CC->pHandle,
   2247                                                           pComponentPrivate_CC->pHandle->pApplicationPrivate,
   2248                                                           pComponentPrivate_CC->pOutputBufferList->pBufHdr[i++]
   2249                                                           );
   2250                 }
   2251             } else {
   2252 
   2253             OMX_PRBUFFER2(pComponentPrivate_CC->dbg, "%d :: OUTPUT: pBuffer = %p\n",__LINE__, pBuffer);
   2254 
   2255             pComponentPrivate_CC->nOutStandingFillDones++;
   2256 
   2257             eError = NBAMRENC_GetCorrespondingLCMLHeader(pComponentPrivate_CC, pBuffer, OMX_DirOutput, &pLcmlHdr);
   2258             if (eError != OMX_ErrorNone) {
   2259                 OMX_ERROR4(pComponentPrivate_CC->dbg, "%d :: Error: Invalid Buffer Came ...\n",__LINE__);
   2260                 goto EXIT;
   2261             }
   2262 
   2263             OMX_PRBUFFER2(pComponentPrivate_CC->dbg, "%d :: Output: pLcmlHdr->buffer->pBuffer = %p\n",__LINE__, pLcmlHdr->buffer->pBuffer);
   2264             pLcmlHdr->buffer->nFilledLen = (OMX_U32)args[8];
   2265             OMX_PRBUFFER1(pComponentPrivate_CC->dbg, "%d :: Output: pBuffer = %ld\n",__LINE__, pLcmlHdr->buffer->nFilledLen);
   2266 #if 0
   2267             pLcmlHdr->buffer->nFilledLen = 32;
   2268             OMX_PRBUFFER1(pComponentPrivate_CC->dbg, "%d :: Output: ::RESET:: pBuffer->nFilledLen = %ld\n",__LINE__, pLcmlHdr->buffer->nFilledLen);
   2269 #endif
   2270 
   2271 #ifdef __PERF_INSTRUMENTATION__
   2272             PERF_ReceivedFrame(pComponentPrivate_CC->pPERFcomp,
   2273                                PREF(pLcmlHdr->buffer,pBuffer),
   2274                                PREF(pLcmlHdr->buffer,nFilledLen),
   2275                                PERF_ModuleCommonLayer);
   2276 
   2277             pComponentPrivate_CC->nLcml_nCntOpReceived++;
   2278 
   2279             if ((pComponentPrivate_CC->nLcml_nCntIp >= 1) && (pComponentPrivate_CC->nLcml_nCntOpReceived == 1)) {
   2280                 PERF_Boundary(pComponentPrivate_CC->pPERFcomp,
   2281                               PERF_BoundaryStart | PERF_BoundarySteadyState);
   2282             }
   2283 #endif
   2284 
   2285                 NBAMRENC_ClearPending(pComponentPrivate_CC,pLcmlHdr->buffer,OMX_DirOutput,__LINE__);
   2286 
   2287                 pComponentPrivate_CC->LastOutbuf = pLcmlHdr->buffer;
   2288                 if(!pLcmlHdr->pBufferParam->usNbFrames){
   2289                         pLcmlHdr->pBufferParam->usNbFrames++;
   2290                 }
   2291                 if((pComponentPrivate_CC->frameMode == NBAMRENC_MIMEMODE)&&(pLcmlHdr->buffer->nFilledLen)) {
   2292                        nFrames = (OMX_U8)( pLcmlHdr->buffer->nFilledLen / NBAMRENC_OUTPUT_BUFFER_SIZE_MIME);
   2293                        frameLength=0;
   2294                        length=0;
   2295                        for(i=0;i<nFrames;i++){
   2296                              index = (pLcmlHdr->buffer->pBuffer[i*NBAMRENC_OUTPUT_BUFFER_SIZE_MIME] >> 3) & 0x0F;
   2297                              if(pLcmlHdr->buffer->nFilledLen == 0)
   2298                                   length = 0;
   2299                              else
   2300                                   length = (OMX_U16)pComponentPrivate_CC->amrMimeBytes[index];
   2301                              if (i){
   2302                                    memmove( pLcmlHdr->buffer->pBuffer + frameLength,
   2303                                             pLcmlHdr->buffer->pBuffer + (i * NBAMRENC_OUTPUT_BUFFER_SIZE_MIME),
   2304                                             length);
   2305                              }
   2306                              frameLength += length;
   2307                         }
   2308                         pLcmlHdr->buffer->nFilledLen= frameLength;
   2309                 }
   2310                  else if((pComponentPrivate_CC->frameMode == NBAMRENC_IF2)&&(pLcmlHdr->buffer->nFilledLen)) {
   2311                        nFrames = (OMX_U8)( pLcmlHdr->buffer->nFilledLen / NBAMRENC_OUTPUT_BUFFER_SIZE_IF2);
   2312                        frameLength=0;
   2313                        length=0;
   2314                        for(i=0;i<nFrames;i++){
   2315                              index = (pLcmlHdr->buffer->pBuffer[i*NBAMRENC_OUTPUT_BUFFER_SIZE_IF2]) & 0x0F;
   2316                              if(pLcmlHdr->buffer->nFilledLen == 0)
   2317                                   length = 0;
   2318                              else
   2319                                   length = (OMX_U16)pComponentPrivate_CC->amrIf2Bytes[index];
   2320                              if (i){
   2321                                    memmove( pLcmlHdr->buffer->pBuffer + frameLength,
   2322                                             pLcmlHdr->buffer->pBuffer + (i * NBAMRENC_OUTPUT_BUFFER_SIZE_IF2),
   2323                                             length);
   2324                              }
   2325                              frameLength += length;
   2326                         }
   2327                         pLcmlHdr->buffer->nFilledLen= frameLength;
   2328                 } else{
   2329                         if ((pComponentPrivate_CC->efrMode == 1)&&(pLcmlHdr->buffer->nFilledLen)){
   2330                            nFrames = pLcmlHdr->buffer->nFilledLen/NBAMRENC_OUTPUT_BUFFER_SIZE_EFR;
   2331                             }
   2332                         else
   2333                            nFrames = pLcmlHdr->buffer->nFilledLen/NBAMRENC_OUTPUT_FRAME_SIZE;
   2334                       }
   2335 
   2336                 OutputFrames = pLcmlHdr->buffer->pOutputPortPrivate;
   2337                 OutputFrames->nFrames = nFrames;
   2338 
   2339 
   2340                 if( !pComponentPrivate_CC->dasfMode){
   2341                         /* Copying time stamp information to output buffer */
   2342                         /*pLcmlHdr->buffer->nTimeStamp = (OMX_TICKS)pComponentPrivate_CC->arrBufIndex[pComponentPrivate_CC->OpBufindex];*/
   2343                         pLcmlHdr->buffer->nTimeStamp = pComponentPrivate_CC->TimeStamp;
   2344                         buffer_duration = (160*nFrames*1000000) /
   2345                           (pComponentPrivate_CC->pcmParams->nSamplingRate*pComponentPrivate_CC->pcmParams->nChannels) ;
   2346                         /* Update time stamp information */
   2347                         pComponentPrivate_CC->TimeStamp += (OMX_TICKS)buffer_duration;
   2348                         /* Copying nTickCount information to output buffer */
   2349                         pLcmlHdr->buffer->nTickCount = pComponentPrivate_CC->arrTickCount[pComponentPrivate_CC->OpBufindex];
   2350                         pComponentPrivate_CC->OpBufindex++;
   2351                         pComponentPrivate_CC->OpBufindex %= pComponentPrivate_CC->pPortDef[OMX_DirOutput]->nBufferCountActual;
   2352                 }
   2353 
   2354 #ifdef __PERF_INSTRUMENTATION__
   2355                 PERF_SendingBuffer(pComponentPrivate_CC->pPERFcomp,
   2356                                     pLcmlHdr->buffer->pBuffer,
   2357                                     pLcmlHdr->buffer->nFilledLen,
   2358                                     PERF_ModuleHLMM);
   2359 #endif
   2360                 /* Non Multi Frame Mode has been tested here */
   2361 
   2362                 pComponentPrivate_CC->nFillBufferDoneCount++;
   2363                 pComponentPrivate_CC->nOutStandingFillDones--;
   2364                 pComponentPrivate_CC->lcml_nOpBuf--;
   2365                 pComponentPrivate_CC->app_nBuf++;
   2366 
   2367                 pComponentPrivate_CC->cbInfo.FillBufferDone( pHandle,
   2368                                             pHandle->pApplicationPrivate,
   2369                                             pLcmlHdr->buffer);
   2370 
   2371                 OMX_PRBUFFER2(pComponentPrivate_CC->dbg, "%d :: Incrementing app_nBuf = %ld\n",__LINE__,pComponentPrivate_CC->app_nBuf);
   2372             }
   2373         }
   2374         }
   2375     else if (event == EMMCodecStrmCtrlAck) {
   2376         OMX_PRDSP2(pComponentPrivate_CC->dbg, "%d :: GOT MESSAGE USN_DSPACK_STRMCTRL \n",__LINE__);
   2377         if (args[1] == (void *)USN_STRMCMD_FLUSH) {
   2378             pHandle = pComponentPrivate_CC->pHandle;
   2379             if ( args[2] == (void *)EMMCodecInputBuffer) {
   2380                 if (args[0] == (void *)USN_ERR_NONE ) {
   2381                     OMX_PRCOMM1(pComponentPrivate_CC->dbg, "Flushing input port %d\n",__LINE__);
   2382 
   2383                     for (i=0; i < pComponentPrivate_CC->nNumInputBufPending; i++) {
   2384 
   2385 #ifdef __PERF_INSTRUMENTATION__
   2386                             PERF_SendingFrame(pComponentPrivate_CC->pPERFcomp,
   2387                                               pComponentPrivate_CC->pInputBufferList->pBufHdr[i]->pBuffer,
   2388                                               0,
   2389                                               PERF_ModuleHLMM);
   2390 #endif
   2391 
   2392                         pComponentPrivate_CC->cbInfo.EmptyBufferDone (
   2393                                        pHandle,
   2394                                        pHandle->pApplicationPrivate,
   2395                                        pComponentPrivate_CC->pInputBufHdrPending[i]
   2396                                        );
   2397                     pComponentPrivate_CC->nEmptyBufferDoneCount++;
   2398                     pComponentPrivate_CC->nOutStandingEmptyDones--;
   2399 
   2400                     }
   2401                     pComponentPrivate_CC->nNumInputBufPending=0;
   2402                     pComponentPrivate_CC->cbInfo.EventHandler(
   2403                          pHandle, pHandle->pApplicationPrivate,
   2404                          OMX_EventCmdComplete, OMX_CommandFlush,NBAMRENC_INPUT_PORT, NULL);
   2405                 } else {
   2406                      OMX_ERROR4(pComponentPrivate_CC->dbg, "LCML reported error while flushing input port\n");
   2407                      goto EXIT;
   2408                 }
   2409             }
   2410             else if ( args[2] == (void *)EMMCodecOuputBuffer) {
   2411                 if (args[0] == (void *)USN_ERR_NONE ) {
   2412                     OMX_PRCOMM1(pComponentPrivate_CC->dbg, "Flushing output port %d\n",__LINE__);
   2413 
   2414                     for (i=0; i < pComponentPrivate_CC->nNumOutputBufPending; i++) {
   2415 #ifdef __PERF_INSTRUMENTATION__
   2416                     PERF_SendingFrame(pComponentPrivate_CC->pPERFcomp,
   2417                                       pComponentPrivate_CC->pOutputBufferList->pBufHdr[i]->pBuffer,
   2418                                       pComponentPrivate_CC->pOutputBufferList->pBufHdr[i]->nFilledLen,
   2419                                       PERF_ModuleHLMM);
   2420 #endif
   2421                         pComponentPrivate_CC->cbInfo.FillBufferDone (
   2422                                        pHandle,
   2423                                        pHandle->pApplicationPrivate,
   2424                                        pComponentPrivate_CC->pOutputBufHdrPending[i]
   2425                                        );
   2426                     pComponentPrivate_CC->nFillBufferDoneCount++;
   2427                     pComponentPrivate_CC->nOutStandingFillDones--;
   2428 
   2429                     }
   2430                     pComponentPrivate_CC->nNumOutputBufPending=0;
   2431                     pComponentPrivate_CC->cbInfo.EventHandler(
   2432                              pHandle, pHandle->pApplicationPrivate,
   2433                              OMX_EventCmdComplete, OMX_CommandFlush,NBAMRENC_OUTPUT_PORT, NULL);
   2434                 } else {
   2435                     OMX_ERROR4(pComponentPrivate_CC->dbg, "LCML reported error while flushing output port\n");
   2436                     goto EXIT;
   2437                 }
   2438             }
   2439         }
   2440     }
   2441     else if(event == EMMCodecProcessingStoped) {
   2442 
   2443         pthread_mutex_lock(&pComponentPrivate_CC->codecStop_mutex);
   2444         if(pComponentPrivate_CC->codecStop_waitingsignal == 0){
   2445             pComponentPrivate_CC->codecStop_waitingsignal = 1;
   2446             pthread_cond_signal(&pComponentPrivate_CC->codecStop_threshold);
   2447             OMX_PRINT2(pComponentPrivate_CC->dbg, "stop ack. received. stop waiting for sending disable command completed\n");
   2448         }
   2449         pthread_mutex_unlock(&pComponentPrivate_CC->codecStop_mutex);
   2450 
   2451         OMX_PRDSP2(pComponentPrivate_CC->dbg, "%d :: GOT MESSAGE USN_DSPACK_STOP \n",__LINE__);
   2452 
   2453         for (i=0; i < pComponentPrivate_CC->pInputBufferList->numBuffers; i++) {
   2454 
   2455             if (pComponentPrivate_CC->pInputBufferList->bBufferPending[i]) {
   2456 
   2457 #ifdef __PERF_INSTRUMENTATION__
   2458                             PERF_SendingFrame(pComponentPrivate_CC->pPERFcomp,
   2459                                               pComponentPrivate_CC->pInputBufferList->pBufHdr[i]->pBuffer,
   2460                                               0,
   2461                                               PERF_ModuleHLMM);
   2462 #endif
   2463 
   2464                    pComponentPrivate_CC->cbInfo.EmptyBufferDone (
   2465                                        pComponentPrivate_CC->pHandle,
   2466                                        pComponentPrivate_CC->pHandle->pApplicationPrivate,
   2467                                        pComponentPrivate_CC->pInputBufferList->pBufHdr[i]
   2468                                        );
   2469                    pComponentPrivate_CC->nEmptyBufferDoneCount++;
   2470                    pComponentPrivate_CC->nOutStandingEmptyDones--;
   2471                    NBAMRENC_ClearPending(pComponentPrivate_CC, pComponentPrivate_CC->pInputBufferList->pBufHdr[i], OMX_DirInput,__LINE__);
   2472             }
   2473         }
   2474 
   2475         for (i=0; i < pComponentPrivate_CC->pOutputBufferList->numBuffers; i++) {
   2476             if (pComponentPrivate_CC->pOutputBufferList->bBufferPending[i]) {
   2477 #ifdef __PERF_INSTRUMENTATION__
   2478                     PERF_SendingFrame(pComponentPrivate_CC->pPERFcomp,
   2479                                       pComponentPrivate_CC->pOutputBufferList->pBufHdr[i]->pBuffer,
   2480                                       pComponentPrivate_CC->pOutputBufferList->pBufHdr[i]->nFilledLen,
   2481                                       PERF_ModuleHLMM);
   2482 #endif
   2483                 pComponentPrivate_CC->cbInfo.FillBufferDone (
   2484                                        pComponentPrivate_CC->pHandle,
   2485                                        pComponentPrivate_CC->pHandle->pApplicationPrivate,
   2486                                        pComponentPrivate_CC->pOutputBufferList->pBufHdr[i]
   2487                                        );
   2488                 pComponentPrivate_CC->nFillBufferDoneCount++;
   2489                 pComponentPrivate_CC->nOutStandingFillDones--;
   2490 
   2491                 NBAMRENC_ClearPending(pComponentPrivate_CC, pComponentPrivate_CC->pOutputBufferList->pBufHdr[i], OMX_DirOutput,__LINE__);
   2492             }
   2493         }
   2494 
   2495         if (!pComponentPrivate_CC->bNoIdleOnStop) {
   2496 
   2497             pComponentPrivate_CC->nNumOutputBufPending=0;
   2498 
   2499             pComponentPrivate_CC->ProcessingInputBuf=0;
   2500             pComponentPrivate_CC->ProcessingOutputBuf=0;
   2501 
   2502             pComponentPrivate_CC->nHoldLength = 0;
   2503             pComponentPrivate_CC->InBuf_Eos_alreadysent = 0;
   2504 
   2505             OMX_MEMFREE_STRUCT(pComponentPrivate_CC->pHoldBuffer);
   2506             OMX_MEMFREE_STRUCT(pComponentPrivate_CC->iMMFDataLastBuffer);
   2507 
   2508             pComponentPrivate_CC->curState = OMX_StateIdle;
   2509 #ifdef RESOURCE_MANAGER_ENABLED
   2510             eError = RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_NBAMR_Encoder_COMPONENT, OMX_StateIdle, 3456, NULL);
   2511 #endif
   2512 
   2513             if (pComponentPrivate_CC->bPreempted == 0) {
   2514                 /* Decrement reference count with signal enabled */
   2515                 if(RemoveStateTransition(pComponentPrivate_CC, OMX_TRUE) != OMX_ErrorNone) {
   2516                    return OMX_ErrorUndefined;
   2517                 }
   2518 
   2519                 pComponentPrivate_CC->cbInfo.EventHandler(pComponentPrivate_CC->pHandle,
   2520                                                         pComponentPrivate_CC->pHandle->pApplicationPrivate,
   2521                                                         OMX_EventCmdComplete,
   2522                                                         OMX_CommandStateSet,
   2523                                                         pComponentPrivate_CC->curState,
   2524                                                         NULL);
   2525             }
   2526             else{
   2527                                 pComponentPrivate_CC->cbInfo.EventHandler(pComponentPrivate_CC->pHandle,
   2528                                                         pComponentPrivate_CC->pHandle->pApplicationPrivate,
   2529                                                         OMX_EventError,
   2530                                                         OMX_ErrorResourcesPreempted,
   2531                                                         OMX_TI_ErrorSevere,
   2532                                                         NULL);
   2533             }
   2534 
   2535 
   2536         }
   2537         else {
   2538             pComponentPrivate_CC->bNoIdleOnStop= OMX_FALSE;
   2539             pComponentPrivate_CC->bDspStoppedWhileExecuting = OMX_TRUE;
   2540         }
   2541     }
   2542     else if(event == EMMCodecDspMessageRecieved) {
   2543         OMX_PRSTATE1(pComponentPrivate_CC->dbg, "%d :: commandedState  = %ld\n",__LINE__,(OMX_U32)args[0]);
   2544         OMX_PRINT2(pComponentPrivate_CC->dbg, "%d :: arg1 = %ld\n",__LINE__,(OMX_U32)args[1]);
   2545         OMX_PRINT2(pComponentPrivate_CC->dbg, "%d :: arg2 = %ld\n",__LINE__,(OMX_U32)args[2]);
   2546 
   2547         if(0x0500 == (OMX_U32)args[2]) {
   2548             OMX_PRDSP2(pComponentPrivate_CC->dbg, "%d :: EMMCodecDspMessageRecieved\n",__LINE__);
   2549         }
   2550     }
   2551     else if(event == EMMCodecAlgCtrlAck) {
   2552         OMX_PRINT2(pComponentPrivate_CC->dbg, "%d :: GOT MESSAGE USN_DSPACK_ALGCTRL \n",__LINE__);
   2553     }
   2554         else if (event == EMMCodecDspError) {
   2555             switch ( (OMX_U32) args [4])
   2556             {
   2557             /* USN_ERR_NONE,: Indicates that no error encountered during execution of the command and the command execution completed succesfully.
   2558              * USN_ERR_WARNING,: Indicates that process function returned a warning. The exact warning is returned in Arg2 of this message.
   2559              * USN_ERR_PROCESS,: Indicates that process function returned a error type. The exact error type is returnd in Arg2 of this message.
   2560              * USN_ERR_PAUSE,: Indicates that execution of pause resulted in error.
   2561              * USN_ERR_STOP,: Indicates that execution of stop resulted in error.
   2562              * USN_ERR_ALGCTRL,: Indicates that execution of alg control resulted in error.
   2563              * USN_ERR_STRMCTRL,: Indiactes the execution of STRM control command, resulted in error.
   2564              * USN_ERR_UNKNOWN_MSG,: Indicates that USN received an unknown command. */
   2565 
   2566 #ifdef _ERROR_PROPAGATION__
   2567                 case USN_ERR_PAUSE:
   2568                 case USN_ERR_STOP:
   2569                 case USN_ERR_ALGCTRL:
   2570                 case USN_ERR_STRMCTRL:
   2571                 case USN_ERR_UNKNOWN_MSG:
   2572                 {
   2573                     pComponentPrivate_CC->bIsInvalidState=OMX_TRUE;
   2574                     pComponentPrivate_CC->curState = OMX_StateInvalid;
   2575                     pHandle = pComponentPrivate_CC->pHandle;
   2576                     pComponentPrivate_CC->cbInfo.EventHandler(pHandle,
   2577                             pHandle->pApplicationPrivate,
   2578                             OMX_EventError,
   2579                             OMX_ErrorInvalidState,
   2580                             OMX_TI_ErrorSevere,
   2581                             NULL);
   2582                 }
   2583                     break;
   2584 #endif
   2585 
   2586                 case USN_ERR_WARNING:
   2587                 case USN_ERR_PROCESS:
   2588                     NBAMRENC_HandleUSNError (pComponentPrivate_CC, (OMX_U32)args[5]);
   2589                     break;
   2590                 default:
   2591                     break;
   2592             }
   2593         }
   2594         else if (event == EMMCodecProcessingPaused) {
   2595             pComponentPrivate_CC->nUnhandledEmptyThisBuffers = 0;
   2596             pComponentPrivate_CC->nUnhandledFillThisBuffers = 0;
   2597             pComponentPrivate_CC->curState = OMX_StatePause;
   2598 
   2599             /* Decrement reference count with signal enabled */
   2600             if(RemoveStateTransition(pComponentPrivate_CC, OMX_TRUE) != OMX_ErrorNone) {
   2601                    return OMX_ErrorUndefined;
   2602             }
   2603             pComponentPrivate_CC->cbInfo.EventHandler( pComponentPrivate_CC->pHandle,
   2604                                                     pComponentPrivate_CC->pHandle->pApplicationPrivate,
   2605                                                     OMX_EventCmdComplete,
   2606                                                     OMX_CommandStateSet,
   2607                                                     pComponentPrivate_CC->curState,
   2608                                                     NULL);
   2609         }
   2610 #ifdef _ERROR_PROPAGATION__
   2611         else if (event ==EMMCodecInitError){
   2612              /* Cheking for MMU_fault */
   2613              if(((int)args[4] == USN_ERR_UNKNOWN_MSG) && (args[5] == (void*)NULL)) {
   2614                     pComponentPrivate_CC->bIsInvalidState=OMX_TRUE;
   2615                     pComponentPrivate_CC->curState = OMX_StateInvalid;
   2616                     pHandle = pComponentPrivate_CC->pHandle;
   2617                     pComponentPrivate_CC->cbInfo.EventHandler(pHandle,
   2618                                    pHandle->pApplicationPrivate,
   2619                                    OMX_EventError,
   2620                                    OMX_ErrorInvalidState,
   2621                                    OMX_TI_ErrorSevere,
   2622                                    NULL);
   2623         }
   2624     }
   2625     else if (event ==EMMCodecInternalError){
   2626         /* Cheking for MMU_fault */
   2627         if(((int)args[4] == USN_ERR_UNKNOWN_MSG) && (args[5] == (void*)NULL)) {
   2628             OMX_ERROR4(pComponentPrivate_CC->dbg, "%d :: UTIL: MMU_Fault \n",__LINE__);
   2629             pComponentPrivate_CC->bIsInvalidState=OMX_TRUE;
   2630             pComponentPrivate_CC->curState = OMX_StateInvalid;
   2631             pHandle = pComponentPrivate_CC->pHandle;
   2632             pComponentPrivate_CC->cbInfo.EventHandler(pHandle,
   2633                                    pHandle->pApplicationPrivate,
   2634                                    OMX_EventError,
   2635                                    OMX_ErrorInvalidState,
   2636                                    OMX_TI_ErrorSevere,
   2637                                    NULL);
   2638         }
   2639 
   2640     }
   2641 #endif
   2642 EXIT:
   2643     if (pComponentPrivate_CC != NULL) {
   2644 	OMX_PRINT1(pComponentPrivate_CC->dbg, "%d :: Exiting the NBAMRENC_LCMLCallback Function\n", __LINE__);
   2645 	OMX_PRINT1(pComponentPrivate_CC->dbg, "%d :: Returning = 0x%x\n", __LINE__, eError);
   2646     }
   2647 
   2648     return eError;
   2649 }
   2650 
   2651 /* ================================================================================= */
   2652 /**
   2653   *  NBAMRENC_GetLCMLHandle()
   2654   *
   2655   * @retval OMX_HANDLETYPE
   2656   */
   2657 /* ================================================================================= */
   2658 #ifndef UNDER_CE
   2659 OMX_HANDLETYPE NBAMRENC_GetLCMLHandle(AMRENC_COMPONENT_PRIVATE *pComponentPrivate)
   2660 {
   2661     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2662     OMX_ERRORTYPE (*fpGetHandle)(OMX_HANDLETYPE);
   2663     OMX_HANDLETYPE pHandle = NULL;
   2664     void *handle;
   2665     char *error;
   2666     AMRENC_COMPONENT_PRIVATE* pComponentPrivate_CC = NULL;
   2667 
   2668     OMX_PRINT1 (pComponentPrivate->dbg, "%d :: Entering NBAMRENC_GetLCMLHandle..\n",__LINE__);
   2669     handle = dlopen("libLCML.so", RTLD_LAZY);
   2670     if (!handle) {
   2671         fputs(dlerror(), stderr);
   2672         goto EXIT;
   2673     }
   2674     fpGetHandle = dlsym (handle, "GetHandle");
   2675     if ((error = dlerror()) != NULL) {
   2676         fputs(error, stderr);
   2677         goto EXIT;
   2678     }
   2679     eError = (*fpGetHandle)(&pHandle);
   2680     if(eError != OMX_ErrorNone) {
   2681         eError = OMX_ErrorUndefined;
   2682         OMXDBG_PRINT(stderr, ERROR, 4, 0, "%d :: OMX_ErrorUndefined...\n",__LINE__);
   2683         pHandle = NULL;
   2684         goto EXIT;
   2685     }
   2686     pComponentPrivate_CC = (AMRENC_COMPONENT_PRIVATE*)pComponentPrivate;
   2687     ((LCML_DSP_INTERFACE*)pHandle)->pComponentPrivate = pComponentPrivate;
   2688 
   2689     pComponentPrivate->ptrLibLCML=handle;           /* saving LCML lib pointer  */
   2690 
   2691 EXIT:
   2692     OMX_PRINT1(pComponentPrivate_CC->dbg, "%d :: Exiting NBAMRENC_GetLCMLHandle..\n",__LINE__);
   2693     OMX_PRINT1(pComponentPrivate_CC->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
   2694     return pHandle;
   2695 }
   2696 
   2697 #else
   2698 /*WINDOWS Explicit dll load procedure*/
   2699 OMX_HANDLETYPE NBAMRENC_GetLCMLHandle(AMRENC_COMPONENT_PRIVATE *pComponentPrivate)
   2700 {
   2701     typedef OMX_ERRORTYPE (*LPFNDLLFUNC1)(OMX_HANDLETYPE);
   2702     OMX_HANDLETYPE pHandle = NULL;
   2703     OMX_ERRORTYPE eError;
   2704     LPFNDLLFUNC1 fpGetHandle1;
   2705 
   2706 
   2707     if (!fpGetHandle1) {
   2708       // handle the error
   2709       return pHandle;
   2710     }
   2711     // call the function
   2712     eError = fpGetHandle1(&pHandle);
   2713     if(eError != OMX_ErrorNone) {
   2714         eError = OMX_ErrorUndefined;
   2715         OMXDBG_PRINT(stderr, ERROR, 4, 0, "eError != OMX_ErrorNone...\n");
   2716         pHandle = NULL;
   2717         return pHandle;
   2718     }
   2719 
   2720     ((LCML_DSP_INTERFACE*)pHandle)->pComponentPrivate = pComponentPrivate;
   2721     return pHandle;
   2722 }
   2723 #endif
   2724 
   2725 /* ================================================================================= */
   2726 /**
   2727 * @fn NBAMRENC_SetPending() description for NBAMRENC_SetPending
   2728 NBAMRENC_SetPending().
   2729 This component is called when a buffer is queued to the LCML
   2730 * @param pComponent  handle for this instance of the component
   2731 *
   2732 * @pre
   2733 *
   2734 * @post
   2735 *
   2736 * @return OMX_ERRORTYPE
   2737 */
   2738 /* ================================================================================ */
   2739 void NBAMRENC_SetPending(AMRENC_COMPONENT_PRIVATE *pComponentPrivate,
   2740                          OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir, OMX_U32 lineNumber)
   2741 {
   2742     OMX_U16 i;
   2743 
   2744     if (eDir == OMX_DirInput) {
   2745         for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
   2746             if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
   2747                 pComponentPrivate->pInputBufferList->bBufferPending[i] = 1;
   2748                 OMX_PRBUFFER2(pComponentPrivate->dbg, "****INPUT BUFFER %d IS PENDING Line %ld******\n",i,lineNumber);
   2749             }
   2750         }
   2751     }
   2752     else {
   2753         for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
   2754             if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
   2755                 pComponentPrivate->pOutputBufferList->bBufferPending[i] = 1;
   2756                 OMX_PRBUFFER2(pComponentPrivate->dbg, "****OUTPUT BUFFER %d IS PENDING Line %ld*****\n",i,lineNumber);
   2757             }
   2758         }
   2759     }
   2760 }
   2761 /* ================================================================================= */
   2762 /**
   2763 * @fn NBAMRENC_ClearPending() description for NBAMRENC_ClearPending
   2764 NBAMRENC_ClearPending().
   2765 This component is called when a buffer is returned from the LCML
   2766 * @param pComponent  handle for this instance of the component
   2767 *
   2768 * @pre
   2769 *
   2770 * @post
   2771 *
   2772 * @return OMX_ERRORTYPE
   2773 */
   2774 /* ================================================================================ */
   2775 void NBAMRENC_ClearPending(AMRENC_COMPONENT_PRIVATE *pComponentPrivate,
   2776                            OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir, OMX_U32 lineNumber)
   2777 {
   2778     OMX_U16 i;
   2779 
   2780     if (eDir == OMX_DirInput) {
   2781         for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
   2782             if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
   2783                 pComponentPrivate->pInputBufferList->bBufferPending[i] = 0;
   2784                 OMX_PRBUFFER2(pComponentPrivate->dbg, "****INPUT BUFFER %d IS RECLAIMED Line %ld*****\n",i,lineNumber);
   2785             }
   2786         }
   2787     }
   2788     else {
   2789         for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
   2790             if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
   2791                 pComponentPrivate->pOutputBufferList->bBufferPending[i] = 0;
   2792                 OMX_PRBUFFER2(pComponentPrivate->dbg, "****OUTPUT BUFFER %d IS RECLAIMED Line %ld*****\n",i,lineNumber);
   2793             }
   2794         }
   2795     }
   2796 }
   2797 /* ================================================================================= */
   2798 /**
   2799 * @fn NBAMRENC_IsPending() description for NBAMRENC_IsPending
   2800 NBAMRENC_IsPending().
   2801 This method returns the pending status to the buffer
   2802 * @param pComponent  handle for this instance of the component
   2803 *
   2804 * @pre
   2805 *
   2806 * @post
   2807 *
   2808 * @return OMX_ERRORTYPE
   2809 */
   2810 /* ================================================================================ */
   2811 OMX_U32 NBAMRENC_IsPending(AMRENC_COMPONENT_PRIVATE *pComponentPrivate,
   2812                            OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir)
   2813 {
   2814     OMX_U16 i;
   2815 
   2816     if (eDir == OMX_DirInput) {
   2817         for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
   2818             if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
   2819                 return pComponentPrivate->pInputBufferList->bBufferPending[i];
   2820             }
   2821         }
   2822     }
   2823     else {
   2824         for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
   2825             OMX_PRBUFFER2(pComponentPrivate->dbg, "pBufHdr = %p\n",pBufHdr);
   2826             OMX_PRBUFFER2(pComponentPrivate->dbg, "pOutputBufferList->pBufHdr[i] = %p\n",pComponentPrivate->pOutputBufferList->pBufHdr[i]);
   2827             if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
   2828                 OMX_PRBUFFER2(pComponentPrivate->dbg, "returning %lx\n",pComponentPrivate->pOutputBufferList->bBufferPending[i]);
   2829                 return pComponentPrivate->pOutputBufferList->bBufferPending[i];
   2830             }
   2831         }
   2832     }
   2833     return -1;
   2834 }
   2835 /* ================================================================================= */
   2836 /**
   2837 * @fn NBAMRENC_IsValid() description for NBAMRENC_IsValid
   2838 NBAMRENC_IsValid().
   2839 This method checks to see if a buffer returned from the LCML is valid.
   2840 * @param pComponent  handle for this instance of the component
   2841 *
   2842 * @pre
   2843 *
   2844 * @post
   2845 *
   2846 * @return OMX_ERRORTYPE
   2847 */
   2848 /* ================================================================================ */
   2849 OMX_U32 NBAMRENC_IsValid(AMRENC_COMPONENT_PRIVATE *pComponentPrivate,
   2850                          OMX_U8 *pBuffer, OMX_DIRTYPE eDir)
   2851 {
   2852     OMX_U16 i;
   2853     int found=0;
   2854 
   2855     if (eDir == OMX_DirInput) {
   2856         for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
   2857             if (pBuffer == pComponentPrivate->pInputBufferList->pBufHdr[i]->pBuffer) {
   2858                 found = 1;
   2859             }
   2860         }
   2861     }
   2862     else {
   2863         for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
   2864             if (pBuffer == pComponentPrivate->pOutputBufferList->pBufHdr[i]->pBuffer) {
   2865                 found = 1;
   2866             }
   2867         }
   2868     }
   2869     return found;
   2870 }
   2871 /* ========================================================================== */
   2872 /**
   2873 * @NBAMRENC_FillLCMLInitParamsEx() This function is used by the component thread to
   2874 * fill the all of its initialization parameters, buffer deatils  etc
   2875 * to LCML structure,
   2876 *
   2877 * @param pComponent  handle for this instance of the component
   2878 * @param plcml_Init  pointer to LCML structure to be filled
   2879 *
   2880 * @pre
   2881 *
   2882 * @post
   2883 *
   2884 * @return none
   2885 */
   2886 /* ========================================================================== */
   2887 OMX_ERRORTYPE NBAMRENC_FillLCMLInitParamsEx(OMX_HANDLETYPE pComponent)
   2888 {
   2889     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2890     OMX_U32 nIpBuf,nIpBufSize,nOpBuf,nOpBufSize;
   2891     OMX_BUFFERHEADERTYPE *pTemp;
   2892     LCML_DSP_INTERFACE *pHandle = (LCML_DSP_INTERFACE *)pComponent;
   2893     AMRENC_COMPONENT_PRIVATE *pComponentPrivate = pHandle->pComponentPrivate;
   2894     NBAMRENC_LCML_BUFHEADERTYPE *pTemp_lcml = NULL;
   2895     OMX_U16 i;
   2896     OMX_U32 size_lcml;
   2897     OMX_PRINT1(pComponentPrivate->dbg, "%d :: NBAMRENC_FillLCMLInitParamsEx\n",__LINE__);
   2898     nIpBuf = pComponentPrivate->pInputBufferList->numBuffers;
   2899     nIpBufSize = pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nBufferSize;
   2900     nOpBuf = pComponentPrivate->pOutputBufferList->numBuffers;
   2901     nOpBufSize = pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nBufferSize;
   2902     OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: ------ Buffer Details -----------\n",__LINE__);
   2903     OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: Input  Buffer Count = %ld\n",__LINE__,nIpBuf);
   2904     OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: Input  Buffer Size = %ld\n",__LINE__,nIpBufSize);
   2905     OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: Output Buffer Count = %ld\n",__LINE__,nOpBuf);
   2906     OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: Output Buffer Size = %ld\n",__LINE__,nOpBufSize);
   2907     OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: ------ Buffer Details ------------\n",__LINE__);
   2908     /* Allocate memory for all input buffer headers..
   2909      * This memory pointer will be sent to LCML */
   2910     size_lcml = nIpBuf * sizeof(NBAMRENC_LCML_BUFHEADERTYPE);
   2911     OMX_MALLOC_SIZE(pTemp_lcml, size_lcml,NBAMRENC_LCML_BUFHEADERTYPE);
   2912 
   2913     pComponentPrivate->pLcmlBufHeader[NBAMRENC_INPUT_PORT] = pTemp_lcml;
   2914     for (i=0; i<nIpBuf; i++) {
   2915         OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: INPUT--------- Inside Ip Loop\n",__LINE__);
   2916         pTemp = pComponentPrivate->pInputBufferList->pBufHdr[i];
   2917         pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
   2918         pTemp->nFilledLen = nIpBufSize;
   2919         pTemp->nVersion.s.nVersionMajor = NBAMRENC_MAJOR_VER;
   2920         pTemp->nVersion.s.nVersionMinor = NBAMRENC_MINOR_VER;
   2921         pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
   2922         pTemp->nTickCount = NBAMRENC_NOT_USED;
   2923         pTemp_lcml->buffer = pTemp;
   2924         OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pTemp_lcml->buffer->pBuffer = %p \n",__LINE__,pTemp_lcml->buffer->pBuffer);
   2925         pTemp_lcml->eDir = OMX_DirInput;
   2926 
   2927         OMX_MALLOC_SIZE_DSPALIGN(pTemp_lcml->pBufferParam, sizeof(NBAMRENC_ParamStruct), OMX_U8);
   2928         pTemp_lcml->pBufferParam->usNbFrames=0;
   2929         pTemp_lcml->pBufferParam->pParamElem=NULL;
   2930         pTemp_lcml->pFrameParam=NULL;
   2931         OMX_MALLOC_GENERIC(pTemp_lcml->pDmmBuf, DMM_BUFFER_OBJ);
   2932 
   2933         pTemp->nFlags = NBAMRENC_NORMAL_BUFFER;
   2934         pTemp++;
   2935         pTemp_lcml++;
   2936     }
   2937 
   2938     /* Allocate memory for all output buffer headers..
   2939      * This memory pointer will be sent to LCML */
   2940     size_lcml = nOpBuf * sizeof(NBAMRENC_LCML_BUFHEADERTYPE);
   2941     OMX_MALLOC_SIZE(pTemp_lcml, size_lcml,NBAMRENC_LCML_BUFHEADERTYPE);
   2942 
   2943     pComponentPrivate->pLcmlBufHeader[NBAMRENC_OUTPUT_PORT] = pTemp_lcml;
   2944     for (i=0; i<nOpBuf; i++) {
   2945         OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: OUTPUT--------- Inside Op Loop\n",__LINE__);
   2946         pTemp = pComponentPrivate->pOutputBufferList->pBufHdr[i];
   2947         pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
   2948          pTemp->nFilledLen = nOpBufSize;
   2949         pTemp->nVersion.s.nVersionMajor = NBAMRENC_MAJOR_VER;
   2950         pTemp->nVersion.s.nVersionMinor = NBAMRENC_MINOR_VER;
   2951         pComponentPrivate->nVersion = pTemp->nVersion.nVersion;
   2952         pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
   2953         pTemp->nTickCount = NBAMRENC_NOT_USED;
   2954         pTemp_lcml->buffer = pTemp;
   2955         OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: pTemp_lcml->buffer->pBuffer = %p \n",__LINE__,pTemp_lcml->buffer->pBuffer);
   2956         pTemp_lcml->eDir = OMX_DirOutput;
   2957 
   2958         OMX_MALLOC_SIZE_DSPALIGN(pTemp_lcml->pBufferParam,
   2959                                 sizeof(NBAMRENC_ParamStruct),
   2960                                 NBAMRENC_ParamStruct);
   2961         pTemp_lcml->pBufferParam->usNbFrames=0;
   2962         pTemp_lcml->pBufferParam->pParamElem=NULL;
   2963         pTemp_lcml->pFrameParam=NULL;
   2964         OMX_MALLOC_GENERIC(pTemp_lcml->pDmmBuf, DMM_BUFFER_OBJ);
   2965 
   2966         pTemp->nFlags = NBAMRENC_NORMAL_BUFFER;
   2967         pTemp++;
   2968         pTemp_lcml++;
   2969     }
   2970 
   2971     pComponentPrivate->bInitParamsInitialized = 1;
   2972 EXIT:
   2973     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting NBAMRENC_FillLCMLInitParamsEx\n",__LINE__);
   2974     OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
   2975     return eError;
   2976 }
   2977 
   2978 /** ========================================================================
   2979 *  OMX_DmmMap () method is used to allocate the memory using DMM.
   2980 *
   2981 *  @param ProcHandle -  Component identification number
   2982 *  @param size  - Buffer header address, that needs to be sent to codec
   2983 *  @param pArmPtr - Message used to send the buffer to codec
   2984 *  @param pDmmBuf - buffer id
   2985 *
   2986 *  @retval OMX_ErrorNone  - Success
   2987 *          OMX_ErrorHardware  -  Hardware Error
   2988 ** ==========================================================================*/
   2989 OMX_ERRORTYPE OMX_DmmMap(DSP_HPROCESSOR ProcHandle,
   2990                      int size,
   2991                      void* pArmPtr,
   2992                      DMM_BUFFER_OBJ* pDmmBuf,
   2993                      struct OMX_TI_Debug dbg)
   2994 {
   2995     OMX_ERRORTYPE eError = OMX_ErrorUndefined;
   2996     DSP_STATUS status;
   2997     int nSizeReserved = 0;
   2998 
   2999     if(pDmmBuf == NULL)
   3000     {
   3001         OMX_ERROR4 (dbg, "pBuf is NULL\n");
   3002         eError = OMX_ErrorBadParameter;
   3003         goto EXIT;
   3004     }
   3005 
   3006     if(pArmPtr == NULL)
   3007     {
   3008         OMX_ERROR4 (dbg, "pBuf is NULL\n");
   3009         eError = OMX_ErrorBadParameter;
   3010         goto EXIT;
   3011     }
   3012     if(ProcHandle == NULL)
   3013     {
   3014         OMX_ERROR4 (dbg, "pProcHandle is NULL\n");
   3015         eError = OMX_ErrorBadParameter;
   3016         goto EXIT;
   3017     }
   3018 
   3019     /* Allocate */
   3020     pDmmBuf->pAllocated = pArmPtr;
   3021 
   3022     /* Reserve */
   3023     nSizeReserved = ROUND_TO_PAGESIZE(size) + 2*DMM_PAGE_SIZE ;
   3024 
   3025 
   3026     status = DSPProcessor_ReserveMemory(ProcHandle, nSizeReserved, &(pDmmBuf->pReserved));
   3027 
   3028     if(DSP_FAILED(status))
   3029     {
   3030         OMX_ERROR4 (dbg,  "DSPProcessor_ReserveMemory() failed - error 0x%x", (int)status);
   3031         eError = OMX_ErrorHardware;
   3032         goto EXIT;
   3033     }
   3034     pDmmBuf->nSize = size;
   3035     OMX_PRBUFFER2 (dbg, " DMM MAP Reserved: %p, size 0x%x (%d)\n", pDmmBuf->pReserved,nSizeReserved,nSizeReserved);
   3036 
   3037     /* Map */
   3038     status = DSPProcessor_Map(ProcHandle,
   3039                               pDmmBuf->pAllocated,/* Arm addres of data to Map on DSP*/
   3040                               size , /* size to Map on DSP*/
   3041                               pDmmBuf->pReserved, /* reserved space */
   3042                               &(pDmmBuf->pMapped), /* returned map pointer */
   3043                               0); /* final param is reserved.  set to zero. */
   3044     if(DSP_FAILED(status))
   3045     {
   3046         OMX_ERROR4 (dbg, "DSPProcessor_Map() failed - error 0x%x", (int)status);
   3047         eError = OMX_ErrorHardware;
   3048         goto EXIT;
   3049     }
   3050     OMX_PRBUFFER2 (dbg, "DMM Mapped: %p, size 0x%x (%d)\n",pDmmBuf->pMapped, size,size);
   3051 
   3052     /* Issue an initial memory flush to ensure cache coherency */
   3053     status = DSPProcessor_FlushMemory(ProcHandle, pDmmBuf->pAllocated, size, 0);
   3054     if(DSP_FAILED(status))
   3055     {
   3056         OMX_ERROR4 (dbg, "Unable to flush mapped buffer: error 0x%x",(int)status);
   3057         goto EXIT;
   3058     }
   3059     eError = OMX_ErrorNone;
   3060 
   3061 EXIT:
   3062    return eError;
   3063 }
   3064 
   3065 /** ========================================================================
   3066 *  OMX_DmmUnMap () method is used to de-allocate the memory using DMM.
   3067 *
   3068 *  @param ProcHandle -  Component identification number
   3069 *  @param pMapPtr  - Map address
   3070 *  @param pResPtr - reserve adress
   3071 *
   3072 *  @retval OMX_ErrorNone  - Success
   3073 *          OMX_ErrorHardware  -  Hardware Error
   3074 ** ==========================================================================*/
   3075 OMX_ERRORTYPE OMX_DmmUnMap(DSP_HPROCESSOR ProcHandle, void* pMapPtr, void* pResPtr, struct OMX_TI_Debug dbg)
   3076 {
   3077     DSP_STATUS status = DSP_SOK;
   3078     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3079     if(pMapPtr == NULL)
   3080     {
   3081         OMX_ERROR4(dbg, "pMapPtr is NULL\n");
   3082         eError = OMX_ErrorBadParameter;
   3083         goto EXIT;
   3084     }
   3085     if(pResPtr == NULL)
   3086     {
   3087         OMX_ERROR4(dbg, "pResPtr is NULL\n");
   3088         OMXDBG_PRINT(stderr, ERROR, 4, 0, "pResPtr is NULL\n");
   3089         eError = OMX_ErrorBadParameter;
   3090         goto EXIT;
   3091     }
   3092     if(ProcHandle == NULL)
   3093     {
   3094         OMX_ERROR4 (dbg, "--ProcHandle is NULL\n");
   3095         eError = OMX_ErrorBadParameter;
   3096         goto EXIT;
   3097     }
   3098 
   3099     status = DSPProcessor_UnMap(ProcHandle,pMapPtr);
   3100     if(DSP_FAILED(status))
   3101     {
   3102         OMX_ERROR4 (dbg, "DSPProcessor_UnMap() failed - error 0x%x",(int)status);
   3103     }
   3104 
   3105     OMX_PRSTATE2 (dbg, "unreserving  structure =0x%p\n",pResPtr);
   3106     status = DSPProcessor_UnReserveMemory(ProcHandle,pResPtr);
   3107     if(DSP_FAILED(status))
   3108     {
   3109         OMX_ERROR4 (dbg, "DSPProcessor_UnReserveMemory() failed - error 0x%x", (int)status);
   3110     }
   3111 
   3112 EXIT:
   3113     return eError;
   3114 }
   3115 
   3116 
   3117 #ifdef RESOURCE_MANAGER_ENABLED
   3118 void NBAMRENC_ResourceManagerCallback(RMPROXY_COMMANDDATATYPE cbData)
   3119 {
   3120     OMX_COMMANDTYPE Cmd = OMX_CommandStateSet;
   3121     OMX_STATETYPE state = OMX_StateIdle;
   3122     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)cbData.hComponent;
   3123     AMRENC_COMPONENT_PRIVATE *pCompPrivate = NULL;
   3124 
   3125     pCompPrivate = (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
   3126 
   3127     if (*(cbData.RM_Error) == OMX_RmProxyCallback_ResourcesPreempted) {
   3128         if (pCompPrivate->curState == OMX_StateExecuting ||
   3129             pCompPrivate->curState == OMX_StatePause) {
   3130             write (pCompPrivate->cmdPipe[1], &Cmd, sizeof(Cmd));
   3131             write (pCompPrivate->cmdDataPipe[1], &state ,sizeof(OMX_U32));
   3132 
   3133             pCompPrivate->bPreempted = 1;
   3134         }
   3135     }
   3136     else if (*(cbData.RM_Error) == OMX_RmProxyCallback_ResourcesAcquired){
   3137         pCompPrivate->cbInfo.EventHandler (
   3138                             pHandle, pHandle->pApplicationPrivate,
   3139                             OMX_EventResourcesAcquired, 0,0,
   3140                             NULL);
   3141 
   3142     }
   3143 }
   3144 #endif
   3145 
   3146 void NBAMRENC_HandleUSNError (AMRENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 arg)
   3147 {
   3148     OMX_COMPONENTTYPE *pHandle = NULL;
   3149     OMX_U8 pending_buffers = OMX_FALSE;
   3150     OMX_U32 i;
   3151     switch (arg)
   3152     {
   3153         case IUALG_WARN_CONCEALED:
   3154         case IUALG_WARN_UNDERFLOW:
   3155         case IUALG_WARN_OVERFLOW:
   3156         case IUALG_WARN_ENDOFDATA:
   3157             /* all of these are informative messages, Algo can recover, no need to notify the
   3158              * IL Client at this stage of the implementation */
   3159             break;
   3160 
   3161         case IUALG_WARN_PLAYCOMPLETED:
   3162         {
   3163             pHandle = pComponentPrivate->pHandle;
   3164             OMX_PRDSP2(pComponentPrivate->dbg, "%d :: GOT MESSAGE IUALG_WARN_PLAYCOMPLETED\n",__LINE__);
   3165             OMX_PRINT2(pComponentPrivate->dbg, "IUALG_WARN_PLAYCOMPLETED Received\n");
   3166             if(pComponentPrivate->LastOutbuf!=NULL){
   3167                 pComponentPrivate->LastOutbuf->nFlags |= OMX_BUFFERFLAG_EOS;
   3168             }
   3169             pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
   3170                             pComponentPrivate->pHandle->pApplicationPrivate,
   3171                             OMX_EventBufferFlag,
   3172                             (OMX_U32)NULL,
   3173                             OMX_BUFFERFLAG_EOS,
   3174                             NULL);
   3175         }
   3176             break;
   3177 
   3178 #ifdef _ERROR_PROPAGATION__
   3179         case IUALG_ERR_BAD_HANDLE:
   3180         case IUALG_ERR_DATA_CORRUPT:
   3181         case IUALG_ERR_NOT_SUPPORTED:
   3182         case IUALG_ERR_ARGUMENT:
   3183         case IUALG_ERR_NOT_READY:
   3184         case IUALG_ERR_GENERAL:
   3185         {
   3186         /* all of these are fatal messages, Algo can not recover
   3187                  * hence return an error */
   3188                 pComponentPrivate->bIsInvalidState=OMX_TRUE;
   3189                 pComponentPrivate->curState = OMX_StateInvalid;
   3190                 pHandle = pComponentPrivate->pHandle;
   3191                 pComponentPrivate->cbInfo.EventHandler(pHandle,
   3192                         pHandle->pApplicationPrivate,
   3193                         OMX_EventError,
   3194                         OMX_ErrorInvalidState,
   3195                         OMX_TI_ErrorSevere,
   3196                         NULL);
   3197             }
   3198             break;
   3199 #endif
   3200         default:
   3201             break;
   3202     }
   3203 }
   3204 OMX_ERRORTYPE AddStateTransition(AMRENC_COMPONENT_PRIVATE *pComponentPrivate) {
   3205     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3206 
   3207     if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
   3208        return OMX_ErrorUndefined;
   3209     }
   3210     /* Increment state change request reference count */
   3211     pComponentPrivate->nPendingStateChangeRequests++;
   3212 
   3213     if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
   3214        return OMX_ErrorUndefined;
   3215     }
   3216     return eError;
   3217 }
   3218 
   3219 OMX_ERRORTYPE RemoveStateTransition(AMRENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BOOL bEnableSignal) {
   3220     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3221 
   3222      /* Decrement state change request reference count*/
   3223     if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
   3224        return OMX_ErrorUndefined;
   3225     }
   3226     pComponentPrivate->nPendingStateChangeRequests--;
   3227 
   3228     /* If there are no more pending requests, signal the thread waiting on this*/
   3229     if(!pComponentPrivate->nPendingStateChangeRequests && bEnableSignal) {
   3230        pthread_cond_signal(&(pComponentPrivate->StateChangeCondition));
   3231     }
   3232     if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
   3233        return OMX_ErrorUndefined;
   3234     }
   3235 
   3236     return eError;
   3237 }
   3238 
   3239