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