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