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