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