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_G711Dec_Utils.c
30 *
31 * This file implements OMX Component for G711 decoder that
32 * is fully compliant with the OMX Audio specification.
33 *
34 * @path  $(CSLPATH)\
35 *
36 * @rev  0.1
37 */
38/* ----------------------------------------------------------------------------*/
39
40/* ------compilation control switches -------------------------*/
41/****************************************************************
42 *  INCLUDE FILES
43 ****************************************************************/
44/* ----- system and platform files ----------------------------*/
45#ifdef UNDER_CE
46#include <windows.h>
47#include <oaf_osal.h>
48#include <omx_core.h>
49#include <stdlib.h>
50#else
51#include <unistd.h>
52#include <sys/types.h>
53#include <sys/types.h>
54#include <sys/wait.h>
55#include <sys/stat.h>
56#include <dlfcn.h>
57#include <malloc.h>
58#include <memory.h>
59#include <fcntl.h>
60#include <errno.h>
61#include <dlfcn.h>
62#endif
63#include <dbapi.h>
64#include <string.h>
65#include <stdio.h>
66#include "OMX_G711Dec_Utils.h"
67#include "g711decsocket_ti.h"
68#include "decode_common_ti.h"
69#include "usn.h"
70#ifdef RESOURCE_MANAGER_ENABLED
71#include <ResourceManagerProxyAPI.h>
72#endif
73
74/* ======================================================================= */
75/**
76 * @def    DASF    Defines the value for identify DASF ON
77 */
78/* ======================================================================= */
79#define DASF 1
80
81#ifdef DASF
82int iAudioFormat = 1;
83int iSamplingRate = 8000;
84#endif
85
86#ifdef UNDER_CE
87#define HASHINGENABLE 1
88void sleep(DWORD Duration)
89{
90    Sleep(Duration);
91}
92#endif
93
94#ifdef G711DEC_MEMDEBUG
95#define newmalloc(x) mymalloc(__LINE__,__FILE__,x)
96#define newfree(z) myfree(z,__LINE__,__FILE__)
97#else
98#define newmalloc(x) malloc(x)
99#define newfree(z) free(z)
100#endif
101
102/* ========================================================================== */
103/**
104 * @G711DECFill_LCMLInitParams () This function is used by the component thread to
105 * fill the all of its initialization parameters, buffer deatils  etc
106 * to LCML structure,
107 *
108 * @param pComponent  handle for this instance of the component
109 * @param plcml_Init  pointer to LCML structure to be filled
110 *
111 * @pre
112 *
113 * @post
114 *
115 * @return none
116 */
117/* ========================================================================== */
118static G711DEC_COMPONENT_PRIVATE *pComponentPrivate_CC = NULL;
119
120OMX_ERRORTYPE G711DECFill_LCMLInitParams(OMX_HANDLETYPE pComponent,
121                                         LCML_DSP *plcml_Init, OMX_U16 arr[])
122{
123    OMX_ERRORTYPE eError = OMX_ErrorNone;
124    OMX_U32 nIpBuf = 0,nIpBufSize = 0,nOpBuf = 0,nOpBufSize = 0;
125    OMX_U16 i = 0;
126    OMX_BUFFERHEADERTYPE *pTemp = NULL;
127    OMX_U16 size_lcml = 0;
128    LCML_STRMATTR *strmAttr = NULL;
129
130    LCML_DSP_INTERFACE *pHandle = (LCML_DSP_INTERFACE *)pComponent;
131    G711DEC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
132    LCML_G711DEC_BUFHEADERTYPE *pTemp_lcml = NULL;
133
134    G711DEC_DPRINT("%d :: G711DECFill_LCMLInitParams\n ",__LINE__);
135    G711DEC_DPRINT("%d :: pHandle = %p\n",__LINE__,pHandle);
136    G711DEC_DPRINT("%d :: pHandle->pComponentPrivate = %p\n",__LINE__,pHandle->pComponentPrivate);
137
138    pComponentPrivate = pHandle->pComponentPrivate;
139
140    nIpBuf = (OMX_U16)pComponentPrivate->pInputBufferList->numBuffers;
141    nIpBufSize = pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferSize;
142    pComponentPrivate->nRuntimeInputBuffers = nIpBuf;
143    nOpBuf = pComponentPrivate->pOutputBufferList->numBuffers;
144    nOpBufSize = pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->nBufferSize;
145
146    /* Fill Input Buffers Info for LCML */
147    plcml_Init->In_BufInfo.nBuffers = nIpBuf;
148    plcml_Init->In_BufInfo.nSize = nIpBufSize;
149    plcml_Init->In_BufInfo.DataTrMethod = DMM_METHOD;
150
151
152    /* Fill Output Buffers Info for LCML */
153    plcml_Init->Out_BufInfo.nBuffers = nOpBuf;
154    plcml_Init->Out_BufInfo.nSize = nOpBufSize;
155    plcml_Init->Out_BufInfo.DataTrMethod = DMM_METHOD;
156
157    /*Copy the node information */
158    plcml_Init->NodeInfo.nNumOfDLLs = 3;
159
160    plcml_Init->NodeInfo.AllUUIDs[0].uuid = &G711DECSOCKET_TI_UUID;
161    strcpy ((char*)plcml_Init->NodeInfo.AllUUIDs[0].DllName,G711DEC_DLL_NAME);
162    plcml_Init->NodeInfo.AllUUIDs[0].eDllType = DLL_NODEOBJECT;
163
164    plcml_Init->NodeInfo.AllUUIDs[1].uuid = &G711DECSOCKET_TI_UUID;
165    strcpy ((char*)plcml_Init->NodeInfo.AllUUIDs[1].DllName,G711DEC_DLL_NAME);
166    plcml_Init->NodeInfo.AllUUIDs[1].eDllType = DLL_DEPENDENT;
167
168    plcml_Init->NodeInfo.AllUUIDs[2].uuid = &USN_TI_UUID;
169    strcpy ((char*)plcml_Init->NodeInfo.AllUUIDs[2].DllName,G711DEC_USN_DLL_NAME);
170    plcml_Init->NodeInfo.AllUUIDs[2].eDllType = DLL_DEPENDENT;
171
172
173    if(pComponentPrivate->dasfmode == 1) {
174        G711DEC_DPRINT("pComponentPrivate->dasfmode = %d\n",pComponentPrivate->dasfmode);
175        G711D_OMX_MALLOC(strmAttr, LCML_STRMATTR);
176        pComponentPrivate->strmAttr = strmAttr;
177
178        strmAttr->uSegid = 0;
179        strmAttr->uAlignment = 0;
180        strmAttr->uTimeout = G711D_TIMEOUT;
181        strmAttr->uBufsize = nOpBufSize;
182        strmAttr->uNumBufs = NUM_G711DEC_OUTPUT_BUFFERS_DASF;
183        strmAttr->lMode = STRMMODE_PROCCOPY;
184        plcml_Init->DeviceInfo.TypeofDevice =1;
185        plcml_Init->DeviceInfo.TypeofRender =0;
186
187        if(pComponentPrivate->acdnmode == 1)
188        {
189            /* DASF/TeeDN mode */
190            plcml_Init->DeviceInfo.AllUUIDs[0].uuid = &ACDN_TI_UUID;
191        }
192        else
193        {
194            /* ACDN mode */
195            plcml_Init->DeviceInfo.AllUUIDs[0].uuid = &DCTN_TI_UUID;
196        }
197
198        plcml_Init->DeviceInfo.DspStream = strmAttr;
199    }
200    else {
201        pComponentPrivate->strmAttr = NULL;
202    }
203
204    /*copy the other information */
205    plcml_Init->SegID = OMX_G711DEC_DEFAULT_SEGMENT;
206    plcml_Init->Timeout = OMX_G711DEC_SN_TIMEOUT;
207    plcml_Init->Alignment = 0;
208    plcml_Init->Priority = OMX_G711DEC_SN_PRIORITY;
209    plcml_Init->ProfileID = -1;
210
211    arr[0] = STREAM_COUNT;
212    arr[1] = G711DEC_INPUT_PORT;
213    arr[2] = G711DEC_DMM;
214
215    if (pComponentPrivate->pInputBufferList->numBuffers) {
216        arr[3] = (OMX_U16) (pComponentPrivate->pInputBufferList->numBuffers );
217    }
218    else {
219        arr[3] = 1;
220    }
221
222    arr[4] = G711DEC_OUTPUT_PORT;
223
224    if(pComponentPrivate->dasfmode == 1) {
225        G711DEC_DPRINT("Setting up create phase params for DASF mode\n");
226        arr[5] = G711DEC_OUTSTRM;
227        arr[6] = NUM_G711DEC_OUTPUT_BUFFERS_DASF;
228    }
229    else {
230
231        G711DEC_DPRINT("Setting up create phase params for FILE mode\n");
232
233        arr[5] = G711DEC_DMM;
234
235        if (pComponentPrivate->pOutputBufferList->numBuffers) {
236            arr[6] = (OMX_U16) pComponentPrivate->pOutputBufferList->numBuffers;
237        }
238        else {
239            arr[6] = 1;
240        }
241    }
242
243    /* set companding mode (A-Law or Mu-Law) */
244    arr[7] = (OMX_U16)pComponentPrivate->g711Params[G711DEC_INPUT_PORT]->ePCMMode;
245    arr[8] = (OMX_S16)pComponentPrivate->ftype;
246    arr[9] = pComponentPrivate->nmulevel;
247    arr[10] = pComponentPrivate->noiselp;
248    arr[11] = pComponentPrivate->dbmnoise;
249    arr[12] = pComponentPrivate->packetlostc;
250    arr[13] = END_OF_CR_PHASE_ARGS;
251
252    plcml_Init->pCrPhArgs = arr;
253
254    G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
255
256    size_lcml = (OMX_U16) (nIpBuf * sizeof(LCML_G711DEC_BUFHEADERTYPE));
257    G711D_OMX_MALLOC_SIZE(pTemp_lcml, size_lcml, LCML_G711DEC_BUFHEADERTYPE);
258
259    pComponentPrivate->pLcmlBufHeader[G711DEC_INPUT_PORT] = pTemp_lcml;
260
261    for (i=0; i<nIpBuf; i++) {
262        pTemp = pComponentPrivate->pInputBufferList->pBufHdr[i];
263        pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
264        pTemp->nAllocLen = nIpBufSize;
265        pTemp->nFilledLen = nIpBufSize;
266        pTemp->nVersion.s.nVersionMajor = G711DEC_MAJOR_VER;
267        pTemp->nVersion.s.nVersionMinor = G711DEC_MINOR_VER;
268        pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
269        pTemp->nTickCount = NOT_USED;
270        pTemp_lcml->buffer = pTemp;
271        pTemp_lcml->eDir = OMX_DirInput;
272
273        G711D_OMX_MALLOC(pTemp_lcml->pIpParam, G711DEC_UAlgInBufParamStruct);
274
275        pTemp_lcml->pIpParam->usFrameLost = 0;
276        pTemp_lcml->pIpParam->usEndOfFile = 0;
277
278        G711D_OMX_MALLOC(pTemp_lcml->pBufferParam,G711DEC_ParamStruct);
279        G711D_OMX_MALLOC(pTemp_lcml->pDmmBuf,DMM_BUFFER_OBJ);
280
281        /* This means, it is not a last buffer. This flag is to be modified by
282         * the application to indicate the last buffer */
283        pTemp->nFlags = NORMAL_BUFFER;
284
285        pTemp_lcml++;
286    }
287
288    /* Allocate memory for all output buffer headers..
289     * This memory pointer will be sent to LCML */
290    size_lcml = (OMX_U16) nOpBuf * sizeof(LCML_G711DEC_BUFHEADERTYPE);
291    G711D_OMX_MALLOC_SIZE(pTemp_lcml, size_lcml, LCML_G711DEC_BUFHEADERTYPE);
292
293    pComponentPrivate->pLcmlBufHeader[G711DEC_OUTPUT_PORT] = pTemp_lcml;
294
295    for (i=0; i<nOpBuf; i++) {
296        pTemp = pComponentPrivate->pOutputBufferList->pBufHdr[i];
297        pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
298        pTemp->nAllocLen = nOpBufSize;
299        pTemp->nFilledLen = nOpBufSize;
300        pTemp->nVersion.s.nVersionMajor = G711DEC_MAJOR_VER;
301        pTemp->nVersion.s.nVersionMinor = G711DEC_MINOR_VER;
302        pComponentPrivate->nVersion = pTemp->nVersion.nVersion;
303        pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
304        pTemp->nTickCount = NOT_USED;
305        /* This means, it is not a last buffer. This flag is to be modified by
306         * the application to indicate the last buffer */
307
308        pTemp_lcml->buffer = pTemp;
309        pTemp_lcml->eDir = OMX_DirOutput;
310
311        G711DEC_DPRINT("%d:::pTemp_lcml = %p\n",__LINE__,pTemp_lcml);
312        G711DEC_DPRINT("%d:::pTemp_lcml->buffer = %p\n",__LINE__,pTemp_lcml->buffer);
313
314        pTemp->nFlags = NORMAL_BUFFER;
315
316        pTemp_lcml++;
317    }
318
319    pComponentPrivate->bPortDefsAllocated = 1;
320    G711DEC_DPRINT("%d :: Exiting G711DECFill_LCMLInitParams",__LINE__);
321
322    pComponentPrivate->bInitParamsInitialized = 1;
323
324 EXIT:
325
326    if(eError == OMX_ErrorInsufficientResources)
327    {
328        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->strmAttr);
329        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pLcmlBufHeader[G711DEC_INPUT_PORT]);
330        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pLcmlBufHeader[G711DEC_OUTPUT_PORT]);
331	if (pTemp_lcml != NULL) {
332	    OMX_G711DECMEMFREE_STRUCT(pTemp_lcml->pIpParam);
333	}
334    }
335
336    return eError;
337}
338
339
340/* ========================================================================== */
341/**
342 * @G711DEC_StartComponentThread() This function is called by the component to create
343 * the component thread, command pipe, data pipe and LCML Pipe.
344 *
345 * @param pComponent  handle for this instance of the component
346 *
347 * @pre
348 *
349 * @post
350 *
351 * @return none
352 */
353/* ========================================================================== */
354
355OMX_ERRORTYPE G711DEC_StartComponentThread(OMX_HANDLETYPE pComponent)
356{
357    OMX_ERRORTYPE eError = OMX_ErrorNone;
358    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
359
360    G711DEC_COMPONENT_PRIVATE *pComponentPrivate =
361        (G711DEC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
362
363#ifdef UNDER_CE
364    pthread_attr_t attr;
365    memset(&attr, 0, sizeof(attr));
366    attr.__inheritsched = PTHREAD_EXPLICIT_SCHED;
367    attr.__schedparam.__sched_priority = OMX_AUDIO_DECODER_THREAD_PRIORITY;
368#endif
369
370    G711DEC_DPRINT ("%d :: Inside  G711DEC_StartComponentThread\n", __LINE__);
371
372    /* Initialize all the variables*/
373    pComponentPrivate->bIsStopping = 0;
374    pComponentPrivate->lcml_nOpBuf = 0;
375    pComponentPrivate->lcml_nIpBuf = 0;
376    pComponentPrivate->app_nBuf = 0;
377    pComponentPrivate->num_Op_Issued = 0;
378    pComponentPrivate->num_Sent_Ip_Buff = 0;
379    pComponentPrivate->num_Reclaimed_Op_Buff = 0;
380    pComponentPrivate->bIsEOFSent = 0;
381
382    /* create the pipe used to send buffers to the thread */
383    eError = pipe (pComponentPrivate->cmdDataPipe);
384    if (eError) {
385        eError = OMX_ErrorInsufficientResources;
386        goto EXIT;
387    }
388
389    /* create the pipe used to send buffers to the thread */
390    eError = pipe (pComponentPrivate->dataPipe);
391    if (eError) {
392        eError = OMX_ErrorInsufficientResources;
393        goto EXIT;
394    }
395
396    /* create the pipe used to send commands to the thread */
397    eError = pipe (pComponentPrivate->cmdPipe);
398    if (eError) {
399        eError = OMX_ErrorInsufficientResources;
400        goto EXIT;
401    }
402
403    /* Create the Component Thread */
404#ifdef UNDER_CE
405    eError = pthread_create (&(pComponentPrivate->ComponentThread), &attr,
406                             ComponentThread, pComponentPrivate);
407#else
408    eError = pthread_create (&(pComponentPrivate->ComponentThread), NULL,
409                             ComponentThread, pComponentPrivate);
410#endif
411
412    if (eError || !pComponentPrivate->ComponentThread) {
413        eError = OMX_ErrorInsufficientResources;
414        goto EXIT;
415    }
416
417    pComponentPrivate_CC = pComponentPrivate;
418    pComponentPrivate->bCompThreadStarted = 1;
419
420 EXIT:
421    return eError;
422}
423
424/* ========================================================================== */
425/**
426 * @G711Dec_FreeCompResources() This function is called by the component during
427 * de-init to close component thread, Command pipe, data pipe & LCML pipe.
428 *
429 * @param pComponent  handle for this instance of the component
430 *
431 * @pre
432 *
433 * @post
434 *
435 * @return none
436 */
437/* ========================================================================== */
438
439OMX_ERRORTYPE G711DEC_FreeCompResources(OMX_HANDLETYPE pComponent)
440{
441    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
442
443    G711DEC_COMPONENT_PRIVATE *pComponentPrivate =
444        (G711DEC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
445
446    OMX_ERRORTYPE eError = OMX_ErrorNone;
447    OMX_ERRORTYPE err = OMX_ErrorNone;
448    OMX_U32 nIpBuf = 0;
449    OMX_U32 nOpBuf = 0;
450
451    G711DEC_DPRINT ("%d :: G711DEC_FreeCompResources\n", __LINE__);
452
453    if (pComponentPrivate->bPortDefsAllocated) {
454        nIpBuf = pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferCountActual;
455        nOpBuf = pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->nBufferCountActual;
456    }
457
458    if (pComponentPrivate->bCompThreadStarted) {
459        err = close (pComponentPrivate->dataPipe[0]);
460
461        if (0 != err && OMX_ErrorNone == eError) {
462            eError = OMX_ErrorHardware;
463        }
464
465        err = close (pComponentPrivate->dataPipe[1]);
466        if (0 != err && OMX_ErrorNone == eError) {
467            eError = OMX_ErrorHardware;
468        }
469
470        err = close (pComponentPrivate->cmdPipe[0]);
471        if (0 != err && OMX_ErrorNone == eError) {
472            eError = OMX_ErrorHardware;
473        }
474
475        err = close (pComponentPrivate->cmdPipe[1]);
476        if (0 != err && OMX_ErrorNone == eError) {
477            eError = OMX_ErrorHardware;
478        }
479
480        err = close (pComponentPrivate->cmdDataPipe[0]);
481        if (0 != err && OMX_ErrorNone == eError) {
482            eError = OMX_ErrorHardware;
483        }
484
485        err = close (pComponentPrivate->cmdDataPipe[1]);
486        if (0 != err && OMX_ErrorNone == eError) {
487            eError = OMX_ErrorHardware;
488        }
489
490
491    }
492
493    OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pHoldBuffer);
494
495    if (pComponentPrivate->bPortDefsAllocated) {
496        G711DEC_DPRINT("%d:::[G711DEC_FreeCompResources] \n", __LINE__);
497        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]);
498        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]);
499        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->g711Params[G711DEC_INPUT_PORT]);
500        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->g711Params[G711DEC_OUTPUT_PORT]);
501        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pCompPort[G711DEC_INPUT_PORT]->pPortFormat);
502        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pCompPort[G711DEC_OUTPUT_PORT]->pPortFormat);
503        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pCompPort[G711DEC_INPUT_PORT] );
504        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pCompPort[G711DEC_OUTPUT_PORT] );
505        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pInputBufferList);
506        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pOutputBufferList);
507        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pParams);
508    }
509
510    pComponentPrivate->bPortDefsAllocated = 0;
511
512#ifndef UNDER_CE
513    G711DEC_DPRINT("\n\n FreeCompResources: Destroying mutexes.\n\n");
514    pthread_mutex_destroy(&pComponentPrivate->InLoaded_mutex);
515    pthread_cond_destroy(&pComponentPrivate->InLoaded_threshold);
516
517    pthread_mutex_destroy(&pComponentPrivate->InIdle_mutex);
518    pthread_cond_destroy(&pComponentPrivate->InIdle_threshold);
519
520    pthread_mutex_destroy(&pComponentPrivate->AlloBuf_mutex);
521    pthread_cond_destroy(&pComponentPrivate->AlloBuf_threshold);
522#else
523    OMX_DestroyEvent(&(pComponentPrivate->InLoaded_event));
524    OMX_DestroyEvent(&(pComponentPrivate->InIdle_event));
525    OMX_DestroyEvent(&(pComponentPrivate->AlloBuf_event));
526#endif
527
528    return eError;
529}
530/*==========================================================================================================*/
531/** Cleanup init params()                                                                            */
532/*===========================================================================================================*/
533OMX_ERRORTYPE G711DEC_CleanupInitParams(OMX_HANDLETYPE pComponent)
534{
535    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
536    G711DEC_COMPONENT_PRIVATE *pComponentPrivate = (G711DEC_COMPONENT_PRIVATE *)
537        pHandle->pComponentPrivate;
538
539    LCML_G711DEC_BUFHEADERTYPE *pTemp_lcml = NULL;
540    OMX_U8 *pBufParmsTemp = NULL;
541
542    OMX_ERRORTYPE eError = OMX_ErrorNone;
543    OMX_U32 nIpBuf = 0;
544    OMX_U16 i=0;
545
546    G711DEC_DPRINT ("%d :: G711DEC_CleanupInitParams()\n", __LINE__);
547
548    nIpBuf = pComponentPrivate->nRuntimeInputBuffers;
549    OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->strmAttr);
550
551    pTemp_lcml = pComponentPrivate->pLcmlBufHeader[G711DEC_INPUT_PORT];
552
553    for(i=0; i<nIpBuf; i++) {
554        OMX_G711DECMEMFREE_STRUCT(pTemp_lcml->pIpParam);
555        OMX_G711DECMEMFREE_STRUCT(pTemp_lcml->pBufferParam);
556        OMX_G711DECMEMFREE_STRUCT(pTemp_lcml->pDmmBuf);
557        pBufParmsTemp = (OMX_U8*)pTemp_lcml->pFrameParam;
558        pBufParmsTemp -= 128;
559        OMX_G711DECMEMFREE_STRUCT(pBufParmsTemp);
560        pTemp_lcml++;
561    }
562
563    OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pLcmlBufHeader[G711DEC_INPUT_PORT]);
564    OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pLcmlBufHeader[G711DEC_OUTPUT_PORT]);
565
566    return eError;
567}
568
569/* ========================================================================== */
570/**
571 * @G711DEC_StopComponentThread() This function is called by the component during
572 * de-init to close component thread, Command pipe, data pipe & LCML pipe.
573 *
574 * @param pComponent  handle for this instance of the component
575 *
576 * @pre
577 *
578 * @post
579 *
580 * @return none
581 */
582/* ========================================================================== */
583
584OMX_ERRORTYPE G711DEC_StopComponentThread(OMX_HANDLETYPE pComponent)
585{
586    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
587    G711DEC_COMPONENT_PRIVATE *pComponentPrivate = (G711DEC_COMPONENT_PRIVATE *)
588        pHandle->pComponentPrivate;
589
590    OMX_ERRORTYPE eError = OMX_ErrorNone;
591    OMX_ERRORTYPE threadError = OMX_ErrorNone;
592    OMX_S16 pthreadError = 0;
593
594    /*Join the component thread */
595    pComponentPrivate->bIsStopping = 1;
596    pthreadError = pthread_join (pComponentPrivate->ComponentThread,
597                                 (void*)&threadError);
598
599    if (0 != pthreadError) {
600        eError = OMX_ErrorHardware;
601    }
602
603    /*Check for the errors */
604    if (OMX_ErrorNone != threadError && OMX_ErrorNone != eError) {
605        eError = OMX_ErrorInsufficientResources;
606        G711DEC_DPRINT ("%d :: Error while closing Component Thread\n",__LINE__);
607    }
608
609    return eError;
610}
611
612
613/* ========================================================================== */
614/**
615 * @G711DECHandleCommand() This function is called by the component when ever it
616 * receives the command from the application
617 *
618 * @param pComponentPrivate  Component private data
619 *
620 * @pre
621 *
622 * @post
623 *
624 * @return none
625 */
626/* ========================================================================== */
627
628OMX_U32 G711DECHandleCommand (G711DEC_COMPONENT_PRIVATE *pComponentPrivate)
629{
630
631    OMX_COMPONENTTYPE *pHandle = NULL;
632    OMX_COMMANDTYPE command;
633    OMX_STATETYPE commandedState = OMX_StateInvalid;
634    OMX_U32 commandData = 0;
635    OMX_HANDLETYPE pLcmlHandle = pComponentPrivate->pLcmlHandle;
636
637#ifdef RESOURCE_MANAGER_ENABLED
638    OMX_ERRORTYPE rm_error = OMX_ErrorNone;
639#endif
640
641    OMX_U16 i = 0;
642    OMX_ERRORTYPE eError = OMX_ErrorNone;
643    OMX_U32 nBuf = 0;
644    OMX_U16 arr[100] = {0};
645    char *p = "hello";
646
647    LCML_CALLBACKTYPE cb;
648    LCML_DSP *pLcmlDsp = NULL;
649    G711DEC_AudioCodecParams *pParams = NULL;
650    OMX_S16 ret = 0;
651    LCML_G711DEC_BUFHEADERTYPE *pLcmlHdr = NULL;
652    int inputPortFlag=0,outputPortFlag=0;
653
654    pHandle = (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
655
656    G711DEC_DPRINT ("%d :: Entering G711DECHandleCommand Function - curState = %d\n",__LINE__,pComponentPrivate->curState);
657    G711DEC_DPRINT ("%d :: pComponentPrivate = %p\n", __LINE__, pComponentPrivate);
658    G711DEC_DPRINT ("%d :: pHandle = %p\n", __LINE__, pHandle);
659    G711DEC_DPRINT ("%d :: Reading from the cmdPipe\n",__LINE__);
660
661    ret = (OMX_U16) (read (pComponentPrivate->cmdPipe[0], &command, sizeof (command)));
662
663    if (ret == -1) {
664        G711DEC_DPRINT ("%d :: Error While reading from the Pipe\n",__LINE__);
665        eError = OMX_ErrorHardware;
666        goto EXIT;
667    }
668
669    G711DEC_DPRINT ("%d :: Reading from the cmdDataPipe\n",__LINE__);
670
671    ret = (OMX_U16) (read (pComponentPrivate->cmdDataPipe[0], &commandData, sizeof (commandData)));
672
673    if (ret == -1) {
674        G711DEC_DPRINT ("%d :: Error While reading from the Pipe\n",__LINE__);
675        eError = OMX_ErrorHardware;
676        goto EXIT;
677    }
678
679    if (command == OMX_CommandStateSet) {
680        commandedState = (OMX_STATETYPE)commandData;
681        switch(commandedState) {
682        case OMX_StateIdle:
683
684            G711DEC_DPRINT("%d :: G711DEC_HandleCommand :: OMX_StateIdle \n",__LINE__);
685            G711DEC_DPRINT("%d :: pComponentPrivate->curState = %d\n",__LINE__,pComponentPrivate->curState);
686
687            if (pComponentPrivate->curState == commandedState){
688                pComponentPrivate->cbInfo.EventHandler (pHandle,
689                                                        pHandle->pApplicationPrivate,
690                                                        OMX_EventError,
691                                                        OMX_ErrorSameState,
692                                                        0, NULL);
693            }
694            else if (pComponentPrivate->curState == OMX_StateLoaded ||
695                     pComponentPrivate->curState == OMX_StateWaitForResources) {
696
697                if (pComponentPrivate->dasfmode == 1)
698                {
699                    pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bEnabled= FALSE;
700                    pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bPopulated= FALSE;
701
702                    if(pComponentPrivate->streamID == 0)
703                    {
704                        G711DEC_DPRINT("**************************************\n");
705                        G711DEC_DPRINT(":: Error = OMX_ErrorInsufficientResources\n");
706                        G711DEC_DPRINT("**************************************\n");
707
708                        eError = OMX_ErrorInsufficientResources;
709                        pComponentPrivate->curState = OMX_StateInvalid;
710
711                        pComponentPrivate->cbInfo.EventHandler(
712                                                               pHandle, pHandle->pApplicationPrivate,
713                                                               OMX_EventError, OMX_ErrorInvalidState,0, NULL);
714
715                        goto EXIT;
716                    }
717                }
718
719                if (pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bPopulated &&
720                    pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bEnabled)
721                {
722                    inputPortFlag = 1;
723                }
724
725                if (!pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bPopulated &&
726                    !pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bEnabled)
727                {
728                    inputPortFlag = 1;
729                }
730
731                if (pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bPopulated &&
732                    pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bEnabled)
733                {
734                    outputPortFlag = 1;
735                }
736
737                if (!pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bPopulated &&
738                    !pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bEnabled)
739                {
740                    outputPortFlag = 1;
741                }
742
743                if(!(inputPortFlag && outputPortFlag))
744                {
745                    /* From Loaded to Idle. All enable ports are populated. */
746                    pComponentPrivate->InLoaded_readytoidle = 1;
747#ifndef UNDER_CE
748                    pthread_mutex_lock(&pComponentPrivate->InLoaded_mutex);
749                    pthread_cond_wait(&pComponentPrivate->InLoaded_threshold,
750                                      &pComponentPrivate->InLoaded_mutex);
751                    pthread_mutex_unlock(&pComponentPrivate->InLoaded_mutex);
752#else
753                    Sleep(0);
754#endif
755                }
756
757                G711DEC_DPRINT ("%d :: Inside G711DECHandleCommand\n",__LINE__);
758                cb.LCML_Callback = (void *) G711DECLCML_Callback;
759                pLcmlHandle = (OMX_HANDLETYPE) G711DECGetLCMLHandle();
760                G711DEC_DPRINT ("%d :: Inside G711DECHandleCommand\n",__LINE__);
761
762                if (pLcmlHandle == NULL) {
763                    G711DEC_DPRINT("%d :: LCML Handle is NULL........exiting..\n",__LINE__);
764                    goto EXIT;
765                }
766
767                G711DEC_DPRINT("G711DECHandleCommand %d\n",__LINE__);
768                G711DEC_DPRINT("pLcmlHandle = %p\n",pLcmlHandle);
769
770                /* Got handle of dsp via phandle filling information about DSP  specific things */
771                pLcmlDsp = (((LCML_DSP_INTERFACE*)pLcmlHandle)->dspCodec);
772                G711DEC_DPRINT("pLcmlDsp = %p\n",pLcmlDsp);
773
774                G711DEC_DPRINT("G711DECHandleCommand %d\n",__LINE__);
775                eError = G711DECFill_LCMLInitParams(pHandle, pLcmlDsp, arr);
776
777                if(eError != OMX_ErrorNone) {
778                    G711DEC_DPRINT("%d :: Error returned from\
779                                        G711DECFill_LCMLInitParams()\n",__LINE__);
780                    goto EXIT;
781                }
782
783                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
784                pComponentPrivate->pLcmlHandle = (LCML_DSP_INTERFACE *)pLcmlHandle;
785
786                /*filling create phase params */
787                cb.LCML_Callback = (void *) G711DECLCML_Callback;
788                G711DEC_DPRINT("%d :: Calling LCML_InitMMCodec...\n",__LINE__);
789
790                eError = LCML_InitMMCodecEx(((LCML_DSP_INTERFACE *)pLcmlHandle)->pCodecinterfacehandle,
791                                            p,&pLcmlHandle,(void *)p,&cb,(OMX_STRING)pComponentPrivate->sDeviceString);
792
793                if(eError != OMX_ErrorNone) {
794                    G711DEC_DPRINT("%d :: Error returned from\
795                            LCML_Init()\n",__LINE__);
796                    goto EXIT;
797                }
798
799#ifdef RESOURCE_MANAGER_ENABLED
800                /* need check the resource with RM */
801                G711DEC_DPRINT("%d :: G711DEC: About to call RMProxy_SendCommand\n", __LINE__);
802
803                pComponentPrivate->rmproxyCallback.RMPROXY_Callback =
804                    (void *) G711DEC_ResourceManagerCallback;
805
806                if (pComponentPrivate->curState != OMX_StateWaitForResources){
807                    rm_error = RMProxy_NewSendCommand(pHandle, RMProxy_RequestResource,
808                                                      OMX_PCM_Decoder_COMPONENT,
809                                                      OMX_G711DEC_CPU,
810                                                      1234, &(pComponentPrivate->rmproxyCallback));
811
812                    G711DEC_DPRINT("%d :: G711DEC: Returned from RMProxy_SendCommand\n", __LINE__);
813                    G711DEC_DPRINT("%d :: G711DEC: RMProxy_SendCommand returned %d\n", __LINE__,rm_error);
814
815                    if(rm_error == OMX_ErrorNone) {
816                        /* resource is available */
817                        G711DEC_DPRINT("Setting to OMX_StateIdle - Line %d\n",__LINE__);
818
819                        pComponentPrivate->curState = OMX_StateIdle;
820                        pComponentPrivate->cbInfo.EventHandler( pHandle,
821                                                                pHandle->pApplicationPrivate,
822                                                                OMX_EventCmdComplete,
823                                                                OMX_CommandStateSet,
824                                                                pComponentPrivate->curState,
825                                                                NULL);
826
827                        rm_error = RMProxy_NewSendCommand(pHandle,
828                                                          RMProxy_StateSet,
829                                                          OMX_G711_Decoder_COMPONENT,
830                                                          OMX_StateIdle, 1234, NULL);
831                    }
832                }
833                else if(rm_error == OMX_ErrorInsufficientResources) {
834                    /* resource is not available, need set state to OMX_StateWaitForResources */
835                    pComponentPrivate->curState = OMX_StateWaitForResources;
836                    pComponentPrivate->cbInfo.EventHandler(pHandle,
837                                                           pHandle->pApplicationPrivate,
838                                                           OMX_EventCmdComplete,
839                                                           OMX_CommandStateSet,
840                                                           pComponentPrivate->curState,
841                                                           NULL);
842                    G711DEC_DPRINT("%d :: G711DEC: Error - insufficient resources\n", __LINE__);
843                }
844#else
845                G711DEC_DPRINT("Setting to OMX_StateIdle - Line %d\n",__LINE__);
846                pComponentPrivate->curState = OMX_StateIdle;
847                pComponentPrivate->cbInfo.EventHandler( pHandle,
848                                                        pHandle->pApplicationPrivate,
849                                                        OMX_EventCmdComplete,
850                                                        OMX_CommandStateSet,
851                                                        pComponentPrivate->curState,
852                                                        NULL);
853#endif
854                if(pComponentPrivate->dasfmode == 1) {
855                    OMX_U32 pValues[4];
856                    G711DEC_DPRINT("%d :: ---- Comp: DASF Functionality is ON ---\n",__LINE__);
857                    G711D_OMX_MALLOC(pParams, G711DEC_AudioCodecParams);
858
859                    pComponentPrivate->pParams = pParams;
860                    pParams->iAudioFormat = iAudioFormat;
861                    pParams->iSamplingRate = iSamplingRate;
862
863                    pParams->iStrmId = pComponentPrivate->streamID;
864
865                    pValues[0] = USN_STRMCMD_SETCODECPARAMS;
866                    pValues[1] = (OMX_U32)pParams;
867                    pValues[2] = sizeof(G711DEC_AudioCodecParams);
868
869                    eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
870                                               EMMCodecControlStrmCtrl,(void *)pValues);
871
872                    if(eError != OMX_ErrorNone) {
873                        G711DEC_DPRINT("%d: Error Occurred in Codec StreamControl..\n",__LINE__);
874                        goto EXIT;
875                    }
876                }
877            }
878            else if (pComponentPrivate->curState == OMX_StateExecuting) {
879                char *pArgs = "damedesuStr";
880
881#ifdef HASHINGENABLE
882                /*Hashing Change*/
883                pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLcmlHandle;
884                eError = LCML_FlushHashes(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle);
885                if (eError != OMX_ErrorNone) {
886                    G711DEC_DPRINT("Error occurred in Codec mapping flush!\n");
887                    break;
888                }
889#endif
890                /*Set the bIsStopping bit */
891                G711DEC_DPRINT("%d :: G711DEC: About to set bIsStopping bit\n", __LINE__);
892                G711DEC_DPRINT("About to call LCML_ControlCodec(STOP)\n");
893
894                eError = LCML_ControlCodec(
895                                           ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
896                                           MMCodecControlStop,(void *)pArgs);
897
898                if(eError != OMX_ErrorNone) {
899                    G711DEC_DPRINT("%d: Error Occurred in Codec Stop..\n", __LINE__);
900                    goto EXIT;
901                }
902
903                pComponentPrivate->bStopSent=1;
904                OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pHoldBuffer);
905                pComponentPrivate->nHoldLength = 0;
906            }
907
908            else if(pComponentPrivate->curState == OMX_StatePause) {
909#ifdef HASHINGENABLE
910                /*Hashing Change*/
911                pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLcmlHandle;
912                eError = LCML_FlushHashes(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle);
913                if (eError != OMX_ErrorNone) {
914                    G711DEC_DPRINT("Error occurred in Codec mapping flush!\n");
915                    break;
916                }
917#endif
918                G711DEC_DPRINT("%d :: Comp: Stop Command Received\n",__LINE__);
919                G711DEC_DPRINT("Setting to OMX_StateIdle - Line %d\n",__LINE__);
920                pComponentPrivate->curState = OMX_StateIdle;
921#ifdef RESOURCE_MANAGER_ENABLED
922                rm_error = RMProxy_NewSendCommand(pHandle,
923                                                  RMProxy_StateSet,
924                                                  OMX_G711_Decoder_COMPONENT,
925                                                  OMX_StateIdle, 1234, NULL);
926#endif
927                G711DEC_DPRINT ("%d :: The component is stopped\n",__LINE__);
928                pComponentPrivate->cbInfo.EventHandler(pHandle,pHandle->pApplicationPrivate,
929                                                       OMX_EventCmdComplete,
930                                                       OMX_CommandStateSet,
931                                                       pComponentPrivate->curState,
932                                                       NULL);
933            }
934            else {
935                /* This means, it is invalid state from application */
936                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
937                pComponentPrivate->cbInfo.EventHandler(pHandle,
938                                                       pHandle->pApplicationPrivate,
939                                                       OMX_EventError,
940                                                       OMX_ErrorIncorrectStateTransition,
941                                                       0, NULL);
942            }
943            break;
944
945        case OMX_StateExecuting:
946            G711DEC_DPRINT("%d: G711DECHandleCommand: Cmd Executing \n",__LINE__);
947            if (pComponentPrivate->curState == commandedState){
948                pComponentPrivate->cbInfo.EventHandler (pHandle,
949                                                        pHandle->pApplicationPrivate,
950                                                        OMX_EventError,
951                                                        OMX_ErrorSameState,
952                                                        0, NULL);
953                G711DEC_DPRINT("%d :: Error: Same State Given by Application\n",__LINE__);
954                goto EXIT;
955            }
956            else if (pComponentPrivate->curState == OMX_StateIdle) {
957                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
958                /* Sending commands to DSP via LCML_ControlCodec third argument  is not used for time being */
959                pComponentPrivate->nFillBufferDoneCount = 0;
960                pComponentPrivate->bStopSent=0;
961
962                eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
963                                           EMMCodecControlStart, (void *)p);
964
965                if(eError != OMX_ErrorNone) {
966                    G711DEC_DPRINT("%d: Error Occurred in Codec Start..\n",__LINE__);
967                    goto EXIT;
968                }
969                /* Send input buffers to application */
970                nBuf = pComponentPrivate->pInputBufferList->numBuffers;
971                G711DEC_DPRINT ("nBuf =  %d\n",(int)nBuf);
972                /* Send output buffers to codec */
973            }
974            else if (pComponentPrivate->curState == OMX_StatePause) {
975                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
976                eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
977                                           EMMCodecControlStart, (void *)p);
978                if (eError != OMX_ErrorNone) {
979                    G711DEC_DPRINT ("Error While Resuming the codec\n");
980                    goto EXIT;
981                }
982
983                for (i=0; i < pComponentPrivate->nNumInputBufPending; i++) {
984                    if (pComponentPrivate->pInputBufHdrPending[i]) {
985                        G711DECGetCorresponding_LCMLHeader(pComponentPrivate->pInputBufHdrPending[i]->pBuffer, OMX_DirInput, &pLcmlHdr);
986                        G711DEC_SetPending(pComponentPrivate,pComponentPrivate->pInputBufHdrPending[i],OMX_DirInput);
987
988                        eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
989                                                  EMMCodecInputBuffer,
990                                                  pComponentPrivate->pInputBufHdrPending[i]->pBuffer,
991                                                  pComponentPrivate->pInputBufHdrPending[i]->nAllocLen,
992                                                  pComponentPrivate->pInputBufHdrPending[i]->nFilledLen,
993                                                  (OMX_U8 *) pLcmlHdr->pIpParam,
994                                                  sizeof(G711DEC_UAlgInBufParamStruct),
995                                                  NULL);
996                    }
997                }
998                pComponentPrivate->nNumInputBufPending = 0;
999
1000                G711DEC_DPRINT("%d :: pComponentPrivate->nNumOutputBufPending %d\n", __LINE__,(int)pComponentPrivate->nNumOutputBufPending);
1001                for (i=0; i < pComponentPrivate->nNumOutputBufPending; i++) {
1002                    if (pComponentPrivate->pOutputBufHdrPending[i]) {
1003                        G711DECGetCorresponding_LCMLHeader(pComponentPrivate->pOutputBufHdrPending[i]->pBuffer, OMX_DirOutput, &pLcmlHdr);
1004                        G711DEC_SetPending(pComponentPrivate,pComponentPrivate->pOutputBufHdrPending[i],OMX_DirOutput);
1005                        eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1006                                                  EMMCodecOuputBuffer,
1007                                                  pComponentPrivate->pOutputBufHdrPending[i]->pBuffer,
1008                                                  pComponentPrivate->pOutputBufHdrPending[i]->nAllocLen,
1009                                                  pComponentPrivate->pOutputBufHdrPending[i]->nFilledLen,
1010                                                  NULL, 0, NULL);
1011                        G711DEC_DPRINT("%d :: eError LCML_QueueBuffer 0x%x\n",__LINE__,eError);
1012                    }
1013
1014                }
1015                pComponentPrivate->nNumOutputBufPending = 0;
1016            }
1017            else {
1018                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
1019                pComponentPrivate->cbInfo.EventHandler (pHandle,
1020                                                        pHandle->pApplicationPrivate,
1021                                                        OMX_EventError,
1022                                                        OMX_ErrorIncorrectStateTransition,
1023                                                        0, NULL);
1024                G711DEC_DPRINT("%d :: Error: Invalid State Given by \
1025                       Application\n",__LINE__);
1026                goto EXIT;
1027            }
1028
1029#ifdef RESOURCE_MANAGER_ENABLED
1030            rm_error = RMProxy_NewSendCommand(pHandle, RMProxy_StateSet,
1031                                              OMX_G711_Decoder_COMPONENT,
1032                                              OMX_StateExecuting, 1234, NULL);
1033#endif
1034            /*Send state change notificaiton to Application */
1035            pComponentPrivate->curState = OMX_StateExecuting;
1036            pComponentPrivate->cbInfo.EventHandler(pHandle,
1037                                                   pHandle->pApplicationPrivate,
1038                                                   OMX_EventCmdComplete,
1039                                                   OMX_CommandStateSet,
1040                                                   pComponentPrivate->curState, NULL);
1041            break;
1042
1043        case OMX_StateLoaded:
1044            G711DEC_DPRINT("%d: G711DECHandleCommand: Cmd Loaded - curState = %d\n",__LINE__,pComponentPrivate->curState);
1045            if (pComponentPrivate->curState == commandedState){
1046                pComponentPrivate->cbInfo.EventHandler (pHandle,
1047                                                        pHandle->pApplicationPrivate,
1048                                                        OMX_EventError,
1049                                                        OMX_ErrorSameState,
1050                                                        0, NULL);
1051                G711DEC_DPRINT("%d :: Error: Same State Given by Application\n",__LINE__);
1052                break;
1053            }
1054            G711DEC_DPRINT("%d: pComponentPrivate->pInputBufferList->numBuffers = %d\n",__LINE__,(int)pComponentPrivate->pInputBufferList->numBuffers);
1055            G711DEC_DPRINT("%d: pComponentPrivate->pOutputBufferList->numBuffers = %d\n",__LINE__,(int)pComponentPrivate->pOutputBufferList->numBuffers);
1056
1057            if (pComponentPrivate->curState == OMX_StateWaitForResources){
1058                G711DEC_DPRINT("%d: G711DECHandleCommand: Cmd Loaded\n",__LINE__);
1059                pComponentPrivate->curState = OMX_StateLoaded;
1060
1061                pComponentPrivate->cbInfo.EventHandler (pHandle,
1062                                                        pHandle->pApplicationPrivate,
1063                                                        OMX_EventCmdComplete,
1064                                                        OMX_CommandStateSet,
1065                                                        pComponentPrivate->curState,NULL);
1066                break;
1067            }
1068
1069            G711DEC_DPRINT("%d :: In side OMX_StateLoaded State: \n",__LINE__);
1070
1071            if (pComponentPrivate->curState != OMX_StateIdle &&
1072                pComponentPrivate->curState != OMX_StateWaitForResources) {
1073                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
1074                pComponentPrivate->cbInfo.EventHandler (pHandle,
1075                                                        pHandle->pApplicationPrivate,
1076                                                        OMX_EventError,
1077                                                        OMX_ErrorIncorrectStateTransition,
1078                                                        0, NULL);
1079                G711DEC_DPRINT("%d :: Error: Invalid State Given by \
1080                                                Application\n",__LINE__);
1081                goto EXIT;
1082            }
1083
1084            G711DEC_DPRINT("pComponentPrivate->pInputBufferList->numBuffers = %d\n",
1085                           (int)pComponentPrivate->pInputBufferList->numBuffers);
1086            G711DEC_DPRINT("pComponentPrivate->pOutputBufferList->numBuffers = %d\n",
1087                           (int)pComponentPrivate->pOutputBufferList->numBuffers);
1088
1089            if (pComponentPrivate->pInputBufferList->numBuffers &&
1090                pComponentPrivate->pOutputBufferList->numBuffers) {
1091                pComponentPrivate->InIdle_goingtoloaded = 1;
1092#ifndef UNDER_CE
1093                pthread_mutex_lock(&pComponentPrivate->InIdle_mutex);
1094                pthread_cond_wait(&pComponentPrivate->InIdle_threshold,
1095                                  &pComponentPrivate->InIdle_mutex);
1096                pthread_mutex_unlock(&pComponentPrivate->InIdle_mutex);
1097#else
1098                OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
1099#endif
1100            }
1101
1102            /* Now Deinitialize the component No error should be returned from
1103             * this function. It should clean the system as much as possible */
1104            G711DEC_DPRINT("%d :: In side OMX_StateLoaded State: \n",__LINE__);
1105            eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1106                                       EMMCodecControlDestroy, (void *)p);
1107
1108            G711DEC_DPRINT("%d :: In side OMX_StateLoaded State: \n",__LINE__);
1109            if (eError != OMX_ErrorNone) {
1110                G711DEC_DPRINT("%d : Error: in Destroying the codec: no.  %x\n",__LINE__, eError);
1111                goto EXIT;
1112            }
1113
1114            G711DEC_DPRINT("%d: G711DECHandleCommand: Cmd Loaded\n",__LINE__);
1115            eError = EXIT_COMPONENT_THRD;
1116            pComponentPrivate->bInitParamsInitialized = 0;
1117            pComponentPrivate->bLoadedCommandPending = OMX_FALSE;
1118            /* Send StateChangeNotification to application */
1119            break;
1120
1121        case OMX_StatePause:
1122            G711DEC_DPRINT("%d: G711DECHandleCommand: Cmd Pause\n",__LINE__);
1123            if (pComponentPrivate->curState == commandedState){
1124                pComponentPrivate->cbInfo.EventHandler (pHandle,
1125                                                        pHandle->pApplicationPrivate,
1126                                                        OMX_EventError,
1127                                                        OMX_ErrorSameState, 0, NULL);
1128
1129                G711DEC_DPRINT("%d :: Error: Same State Given by \
1130                           Application\n",__LINE__);
1131                break;
1132            }
1133            if (pComponentPrivate->curState != OMX_StateExecuting &&
1134                pComponentPrivate->curState != OMX_StateIdle) {
1135                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
1136                pComponentPrivate->cbInfo.EventHandler (pHandle,
1137                                                        pHandle->pApplicationPrivate,
1138                                                        OMX_EventError,
1139                                                        OMX_ErrorIncorrectStateTransition,
1140                                                        0, NULL);
1141                G711DEC_DPRINT("%d :: Error: Invalid State Given by \
1142                       Application\n",__LINE__);
1143                goto EXIT;
1144            }
1145            eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1146                                       EMMCodecControlPause, (void *)p);
1147
1148            if (eError != OMX_ErrorNone) {
1149                G711DEC_DPRINT("%d : Error: in Pausing the codec\n",__LINE__);
1150                goto EXIT;
1151            }
1152            break;
1153
1154        case OMX_StateWaitForResources:
1155
1156            if (pComponentPrivate->curState == commandedState){
1157                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
1158                pComponentPrivate->cbInfo.EventHandler (pHandle,
1159                                                        pHandle->pApplicationPrivate,
1160                                                        OMX_EventError,
1161                                                        OMX_ErrorSameState,
1162                                                        0, NULL);
1163
1164                G711DEC_DPRINT("%d :: Error: Same State Given by \
1165                           Application\n",__LINE__);
1166            }
1167            else if (pComponentPrivate->curState == OMX_StateLoaded) {
1168                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
1169                pComponentPrivate->curState = OMX_StateWaitForResources;
1170                pComponentPrivate->cbInfo.EventHandler(pHandle,
1171                                                       pHandle->pApplicationPrivate,
1172                                                       OMX_EventCmdComplete,
1173                                                       OMX_CommandStateSet,
1174                                                       pComponentPrivate->curState,NULL);
1175            }
1176            else{
1177                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
1178                pComponentPrivate->cbInfo.EventHandler(pHandle,
1179                                                       pHandle->pApplicationPrivate,
1180                                                       OMX_EventError,
1181                                                       OMX_ErrorIncorrectStateTransition,
1182                                                       0, "NULL");
1183            }
1184            break;
1185
1186        case OMX_StateInvalid:
1187            G711DEC_DPRINT("%d: G711DECHandleCommand: Cmd OMX_StateInvalid:\n",__LINE__);
1188            if (pComponentPrivate->curState == commandedState){
1189                pComponentPrivate->cbInfo.EventHandler (pHandle,
1190                                                        pHandle->pApplicationPrivate,
1191                                                        OMX_EventError,
1192                                                        OMX_ErrorSameState,
1193                                                        0, NULL);
1194                G711DEC_DPRINT("%d :: Error: Same State Given by \
1195                           Application\n",__LINE__);
1196            }
1197            else{
1198                G711DEC_DPRINT("%d :: Comp: OMX_G711DecUtils.c\n",__LINE__);
1199                if (pComponentPrivate->curState != OMX_StateWaitForResources &&
1200                    pComponentPrivate->curState != OMX_StateLoaded) {
1201                    eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1202                                               EMMCodecControlDestroy, (void *)p);
1203                }
1204
1205                pComponentPrivate->curState = OMX_StateInvalid;
1206                pComponentPrivate->cbInfo.EventHandler(pHandle,
1207                                                       pHandle->pApplicationPrivate,
1208                                                       OMX_EventError,
1209                                                       OMX_ErrorInvalidState,
1210                                                       0, NULL);
1211                G711DEC_CleanupInitParams(pHandle);
1212            }
1213            break;
1214
1215        case OMX_StateMax:
1216            G711DEC_DPRINT("%d: G711DECHandleCommand: Cmd OMX_StateMax::\n",__LINE__);
1217            break;
1218        } /* End of Switch */
1219    }
1220    else if (command == OMX_CommandMarkBuffer) {
1221        G711DEC_DPRINT("command OMX_CommandMarkBuffer received %d\n",__LINE__);
1222        if(!pComponentPrivate->pMarkBuf){
1223            G711DEC_DPRINT("command OMX_CommandMarkBuffer received %d\n",__LINE__);
1224            pComponentPrivate->pMarkBuf = (OMX_MARKTYPE *)(commandData);
1225        }
1226    }
1227    else if (command == OMX_CommandPortDisable) {
1228        if (!pComponentPrivate->bDisableCommandPending) {
1229            if(commandData == 0x0 || commandData == -1){
1230                /* disable port */
1231                pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bEnabled = OMX_FALSE;
1232            }
1233
1234            if(commandData == 0x1 || commandData == -1){
1235                char *pArgs = "damedesuStr";
1236                pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bEnabled = OMX_FALSE;
1237
1238                if (pComponentPrivate->curState == OMX_StateExecuting) {
1239                    pComponentPrivate->bNoIdleOnStop = OMX_TRUE;
1240                    G711DEC_DPRINT("Calling LCML_ControlCodec() Line %d\n",__LINE__);
1241                    eError = LCML_ControlCodec(
1242                                               ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1243                                               MMCodecControlStop,(void *)pArgs);
1244                }
1245            }
1246        }
1247
1248        if(commandData == 0x0) {
1249            if(!pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bPopulated){
1250                /* return cmdcomplete event if input unpopulated */
1251                pComponentPrivate->cbInfo.EventHandler(pHandle,
1252                                                       pHandle->pApplicationPrivate,
1253                                                       OMX_EventCmdComplete,
1254                                                       OMX_CommandPortDisable,
1255                                                       G711DEC_INPUT_PORT, NULL);
1256                G711DEC_DPRINT("Clearing bDisableCommandPending Line %d\n",__LINE__);
1257                pComponentPrivate->bDisableCommandPending = 0;
1258            }
1259            else{
1260                pComponentPrivate->bDisableCommandPending = 1;
1261                pComponentPrivate->bDisableCommandParam = commandData;
1262            }
1263        }
1264
1265        if(commandData == 0x1) {
1266            if (!pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bPopulated){
1267                /* return cmdcomplete event if output unpopulated */
1268                pComponentPrivate->cbInfo.EventHandler(pHandle,
1269                                                       pHandle->pApplicationPrivate,
1270                                                       OMX_EventCmdComplete,
1271                                                       OMX_CommandPortDisable,
1272                                                       G711DEC_OUTPUT_PORT, NULL);
1273
1274                G711DEC_DPRINT("Clearing bDisableCommandPending Line %d\n",__LINE__);
1275                pComponentPrivate->bDisableCommandPending = 0;
1276            }
1277            else {
1278                pComponentPrivate->bDisableCommandPending = 1;
1279                pComponentPrivate->bDisableCommandParam = commandData;
1280            }
1281        }
1282
1283        if(commandData == -1) {
1284            if (!pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bPopulated &&
1285                !pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bPopulated){
1286
1287                /* return cmdcomplete event if inout & output unpopulated */
1288                pComponentPrivate->cbInfo.EventHandler(pHandle,
1289                                                       pHandle->pApplicationPrivate,
1290                                                       OMX_EventCmdComplete,
1291                                                       OMX_CommandPortDisable,
1292                                                       G711DEC_INPUT_PORT, NULL);
1293
1294                pComponentPrivate->cbInfo.EventHandler(pHandle,
1295                                                       pHandle->pApplicationPrivate,
1296                                                       OMX_EventCmdComplete,
1297                                                       OMX_CommandPortDisable,
1298                                                       G711DEC_OUTPUT_PORT, NULL);
1299
1300                G711DEC_DPRINT("Clearing bDisableCommandPending Line %d\n",__LINE__);
1301                pComponentPrivate->bDisableCommandPending = 0;
1302            }
1303            else {
1304                pComponentPrivate->bDisableCommandPending = 1;
1305                pComponentPrivate->bDisableCommandParam = commandData;
1306            }
1307        }
1308    }
1309    else if (command == OMX_CommandPortEnable) {
1310        if(!pComponentPrivate->bEnableCommandPending) {
1311            if(commandData == 0x0 || commandData == -1){
1312                /* enable in port */
1313                G711DEC_DPRINT ("setting input port to enabled\n");
1314
1315                G711DEC_DPRINT ("pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bEnabled = %d\n",pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bEnabled);
1316                if(pComponentPrivate->AlloBuf_waitingsignal)
1317                {
1318                    G711DEC_DPRINT ("%d :: Unblock AlloBuf_threshold\n", __LINE__);
1319                    pComponentPrivate->AlloBuf_waitingsignal = 0;
1320                }
1321                pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bEnabled = OMX_TRUE;
1322                /* Removing sleep calls. */
1323                G711DEC_DPRINT ("pComponentPrivate->pPortDef[INPUT_PORT]->bEnabled = %d\n",
1324                                pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bEnabled);
1325            }
1326            if(commandData == 0x1 || commandData == -1){
1327                /* Removing sleep() calls. */
1328                if(pComponentPrivate->AlloBuf_waitingsignal)
1329                {
1330                    G711DEC_DPRINT ("%d :: Unblock AlloBuf_threshold", __LINE__);
1331                    pComponentPrivate->AlloBuf_waitingsignal = 0;
1332#ifndef UNDER_CE
1333                    pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
1334                    pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
1335                    pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
1336#else
1337                    OMX_SignalEvent(&(pComponentPrivate->AlloBuf_event));
1338#endif
1339                }
1340
1341                /* Removing sleep() calls. */
1342                /* enable out port */
1343                if (pComponentPrivate->curState == OMX_StateExecuting)
1344                {
1345                    char *pArgs = "damedesuStr";
1346                    pComponentPrivate->bDspStoppedWhileExecuting = OMX_FALSE;
1347                    eError = LCML_ControlCodec(
1348                                               ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1349                                               EMMCodecControlStart,(void *)pArgs);
1350                }
1351                G711DEC_DPRINT("setting output port to enabled\n");
1352                pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bEnabled = OMX_TRUE;
1353                G711DEC_DPRINT("pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bEnabled = %d\n",pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bEnabled);
1354            }
1355        }
1356        if(commandData == 0x0)
1357        {
1358            if (pComponentPrivate->curState == OMX_StateLoaded ||
1359                pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bPopulated)
1360            {
1361                pComponentPrivate->cbInfo.EventHandler( pHandle,
1362                                                        pHandle->pApplicationPrivate,
1363                                                        OMX_EventCmdComplete,
1364                                                        OMX_CommandPortEnable,
1365                                                        G711DEC_INPUT_PORT,
1366                                                        NULL);
1367                pComponentPrivate->bEnableCommandPending = 0;
1368            }
1369            else {
1370                pComponentPrivate->bEnableCommandPending = 1;
1371                pComponentPrivate->bEnableCommandParam = commandData;
1372            }
1373        }
1374        else if(commandData == 0x1){
1375            if (pComponentPrivate->curState == OMX_StateLoaded ||
1376                pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bPopulated){
1377                pComponentPrivate->cbInfo.EventHandler( pHandle,
1378                                                        pHandle->pApplicationPrivate,
1379                                                        OMX_EventCmdComplete,
1380                                                        OMX_CommandPortEnable,
1381                                                        G711DEC_OUTPUT_PORT,
1382                                                        NULL);
1383                pComponentPrivate->bEnableCommandPending = 0;
1384            }
1385            else {
1386                pComponentPrivate->bEnableCommandPending = 1;
1387                pComponentPrivate->bEnableCommandParam = commandData;
1388            }
1389        }
1390        else if(commandData == -1 ){
1391            if (pComponentPrivate->curState == OMX_StateLoaded ||
1392                (pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->bPopulated
1393                 && pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->bPopulated)){
1394                pComponentPrivate->cbInfo.EventHandler( pHandle,
1395                                                        pHandle->pApplicationPrivate,
1396                                                        OMX_EventCmdComplete,
1397                                                        OMX_CommandPortEnable,
1398                                                        G711DEC_INPUT_PORT,
1399                                                        NULL);
1400
1401                pComponentPrivate->cbInfo.EventHandler( pHandle,
1402                                                        pHandle->pApplicationPrivate,
1403                                                        OMX_EventCmdComplete,
1404                                                        OMX_CommandPortEnable,
1405                                                        G711DEC_OUTPUT_PORT,
1406                                                        NULL);
1407                pComponentPrivate->bEnableCommandPending = 0;
1408                G711DECFill_LCMLInitParamsEx(pComponentPrivate->pHandle);
1409                pComponentPrivate->bJustReenabled = 1;
1410            }
1411            else {
1412                pComponentPrivate->bEnableCommandPending = 1;
1413                pComponentPrivate->bEnableCommandParam = commandData;
1414            }
1415        }
1416#ifndef UNDER_CE
1417        pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
1418        pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
1419        pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
1420#else
1421        OMX_SignalEvent(&(pComponentPrivate->AlloBuf_event));
1422#endif
1423    }
1424    else if (command == OMX_CommandFlush) {
1425        if(commandData == 0x0 || commandData == -1){
1426            for (i=0; i < MAX_NUM_OF_BUFS; i++)
1427            {
1428                pComponentPrivate->pInputBufHdrPending[i] = NULL;
1429            }
1430            for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++)
1431            {
1432                G711DEC_DPRINT("line %d:: Calling EmptyBufferDone\n",__LINE__);
1433                pComponentPrivate->cbInfo.EmptyBufferDone (
1434                                                           pComponentPrivate->pHandle,
1435                                                           pComponentPrivate->pHandle->pApplicationPrivate,
1436                                                           pComponentPrivate->pInputBufferList->pBufHdr[i]
1437                                                           );
1438                pComponentPrivate->nEmptyBufferDoneCount++;
1439                pComponentPrivate->nNumInputBufPending = 0;
1440            }
1441
1442            /* return all input buffers */
1443            pComponentPrivate->cbInfo.EventHandler(pHandle,
1444                                                   pHandle->pApplicationPrivate,
1445                                                   OMX_EventCmdComplete,
1446                                                   OMX_CommandFlush,
1447                                                   G711DEC_INPUT_PORT, NULL);
1448        }
1449        if(commandData == 0x1 || commandData == -1){
1450            for (i=0; i < MAX_NUM_OF_BUFS; i++)
1451            {
1452                pComponentPrivate->pOutputBufHdrPending[i] = NULL;
1453            }
1454            /* return all output buffers */
1455            for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
1456                G711DEC_DPRINT("Calling FillBufferDone From Line %d\n",__LINE__);
1457                pComponentPrivate->cbInfo.FillBufferDone (
1458                                                          pComponentPrivate->pHandle,
1459                                                          pComponentPrivate->pHandle->pApplicationPrivate,
1460                                                          pComponentPrivate->pOutputBufferList->pBufHdr[i]
1461                                                          );
1462                pComponentPrivate->nFillBufferDoneCount++;
1463                pComponentPrivate->nNumOutputBufPending = 0;
1464            }
1465
1466            pComponentPrivate->cbInfo.EventHandler(pHandle,
1467                                                   pHandle->pApplicationPrivate,
1468                                                   OMX_EventCmdComplete,
1469                                                   OMX_CommandFlush,
1470                                                   G711DEC_OUTPUT_PORT, NULL);
1471        }
1472    }
1473
1474 EXIT:
1475    G711DEC_DPRINT ("%d :: Exiting G711DECHandleCommand Function\n",__LINE__);
1476    G711DEC_DPRINT ("%d :: Returning %d\n",__LINE__,eError);
1477    return eError;
1478}
1479
1480
1481/* ========================================================================== */
1482/**
1483 * @G711DECHandleDataBuf_FromApp() This function is called by the component when ever it
1484 * receives the buffer from the application
1485 *
1486 * @param pComponentPrivate  Component private data
1487 * @param pBufHeader Buffer from the application
1488 *
1489 * @pre
1490 *
1491 * @post
1492 *
1493 * @return none
1494 */
1495/* ========================================================================== */
1496OMX_ERRORTYPE G711DECHandleDataBuf_FromApp(OMX_BUFFERHEADERTYPE* pBufHeader,
1497                                           G711DEC_COMPONENT_PRIVATE *pComponentPrivate)
1498{
1499    OMX_ERRORTYPE eError = OMX_ErrorNone;
1500    OMX_DIRTYPE eDir;
1501    LCML_G711DEC_BUFHEADERTYPE *pLcmlHdr = NULL;
1502    LCML_DSP_INTERFACE *pLcmlHandle = (LCML_DSP_INTERFACE *)
1503        pComponentPrivate->pLcmlHandle;
1504    OMX_U32 frameLength = 0;
1505    OMX_U8* pExtraData = NULL;
1506    OMX_U32 holdBufferSize = 0;
1507
1508    OMX_U8 nFrames = 0;
1509    OMX_U8 *frameType = NULL;
1510    LCML_DSP_INTERFACE * phandle = NULL;
1511    OMX_U8 *pBufParmsTemp = NULL;
1512
1513    G711DEC_DPRINT ("%d :: Entering G711DECHandleDataBuf_FromApp Function\n",__LINE__);
1514
1515    holdBufferSize = (pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferSize) *
1516        (pComponentPrivate->pInputBufferList->numBuffers + 1);
1517    /*Find the direction of the received buffer from buffer list */
1518    eError = G711DECGetBufferDirection(pBufHeader, &eDir);
1519    if (eError != OMX_ErrorNone) {
1520        G711DEC_DPRINT ("%d :: The PBufHeader is not found in the list\n",
1521                        __LINE__);
1522        goto EXIT;
1523    }
1524
1525    if (eDir == OMX_DirInput) {
1526
1527        if ( pBufHeader->nFilledLen > 0 ) {
1528            pComponentPrivate->bBypassDSP = 0;
1529
1530            if ( pComponentPrivate->nHoldLength == 0 ) {
1531
1532                G711DEC_DPRINT("G711DECHandleDataBuf_FromApp - reading G711DEC\n");
1533                frameLength = pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferSize;
1534
1535                if ( pBufHeader->nFilledLen >= frameLength ) {
1536
1537                    /* Copy the aFillBufferPtr from the 2nd frame to iHoldBuffer*/
1538                    /* The 1st frame will stay in aFillBufferPtr to be passed down.*/
1539                    /* the length of remaining Frames in aFillBufferPtr.*/
1540                    pComponentPrivate->nHoldLength = pBufHeader->nFilledLen - frameLength;
1541
1542                    if ( pComponentPrivate->nHoldLength > 0 ) {/* something need to be hold in iHoldBuffer */
1543                        if (pComponentPrivate->pHoldBuffer == NULL) {
1544                            G711D_OMX_MALLOC_SIZE(pComponentPrivate->pHoldBuffer, holdBufferSize, OMX_U8);
1545                        }
1546
1547                        /* Copy the extra data into pHoldBuffer. Size will be nHoldLength. */
1548                        pExtraData = pBufHeader->pBuffer + (pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferSize);
1549                        memcpy (pComponentPrivate->pHoldBuffer, pExtraData, pComponentPrivate->nHoldLength);
1550                    }
1551                }
1552                else {
1553                    /* received buffer with less than 1 G711 frame. Save the data in iHoldBuffer.*/
1554                    pComponentPrivate->nHoldLength = pBufHeader->nFilledLen;
1555
1556                    /* save the data into iHoldBuffer.*/
1557                    if (pComponentPrivate->pHoldBuffer == NULL) {
1558                        G711D_OMX_MALLOC_SIZE(pComponentPrivate->pHoldBuffer, holdBufferSize, OMX_U8);
1559                    }
1560                    /* Not enough data to be sent. Copy all received data into iHoldBuffer.*/
1561                    /* Size to be copied will be iHoldLen == mmData->BufferSize() */
1562                    memcpy (pComponentPrivate->pHoldBuffer,
1563                            pBufHeader->pBuffer, pComponentPrivate->nHoldLength);
1564
1565                    /* since not enough data, we shouldn't send anything to SN, but instead request to EmptyBufferDone again.*/
1566                    G711DEC_DPRINT("line %d:: Calling EmptyBufferDone\n",__LINE__);
1567                    if (pComponentPrivate->curState != OMX_StatePause) {
1568                        pComponentPrivate->cbInfo.EmptyBufferDone (
1569                                                                   pComponentPrivate->pHandle,
1570                                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1571                                                                   pBufHeader);
1572                        pComponentPrivate->nEmptyBufferDoneCount++;
1573                    }
1574                    else {
1575                        pComponentPrivate->pInputBufHdrPending[pComponentPrivate->nNumInputBufPending++] = pBufHeader;
1576                    }
1577
1578
1579                    goto EXIT;
1580                }
1581            }
1582            else {
1583                /* iHoldBuffer has data. There is no possibility that data in iHoldBuffer is less for 1 frame without*/
1584                /* lastBufferFlag being set. Unless it's a corrupt file.*/
1585                /* Copy the data in dataPtr to iHoldBuffer. Update the iHoldBuffer size (iHoldLen).*/
1586
1587
1588                pExtraData = pComponentPrivate->pHoldBuffer + pComponentPrivate->nHoldLength;
1589                memcpy(pExtraData,pBufHeader->pBuffer,pBufHeader->nFilledLen);
1590
1591                pComponentPrivate->nHoldLength += pBufHeader->nFilledLen;
1592
1593                frameLength = (pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferSize);
1594
1595                if ( pComponentPrivate->nHoldLength >= frameLength )  {
1596                    /* Copy the data from pComponentPrivate->pHoldBuffer to pBufHeader->pBuffer*/
1597                    memcpy(pBufHeader->pBuffer,pComponentPrivate->pHoldBuffer,frameLength);
1598
1599                    /* Now the pHoldBuffer has framelength fewer bytes - update nHoldLength*/
1600                    pComponentPrivate->nHoldLength = pComponentPrivate->nHoldLength - frameLength;
1601
1602                    /* Shift the remaining bytes to the beginning of the pHoldBuffer */
1603                    pExtraData = pComponentPrivate->pHoldBuffer + frameLength;
1604                    if (frameLength >= pComponentPrivate->nHoldLength)
1605                        memcpy(pComponentPrivate->pHoldBuffer,pExtraData, pComponentPrivate->nHoldLength);
1606                    else
1607                        memmove(pComponentPrivate->pHoldBuffer,pExtraData, pComponentPrivate->nHoldLength);
1608
1609
1610                    /* Clear the rest of the data from the pHoldBuffer */
1611                    pExtraData = pComponentPrivate->pHoldBuffer + pComponentPrivate->nHoldLength;
1612                    memset(pExtraData,0,holdBufferSize - pComponentPrivate->nHoldLength);
1613                }
1614                else {
1615                    if (pComponentPrivate->curState != OMX_StatePause) {
1616                        G711DEC_DPRINT("line %d:: Calling EmptyBufferDone\n",__LINE__);
1617                        pComponentPrivate->cbInfo.EmptyBufferDone (
1618                                                                   pComponentPrivate->pHandle,
1619                                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1620                                                                   pBufHeader);
1621                        pComponentPrivate->nEmptyBufferDoneCount++;
1622                        goto EXIT;
1623                    }
1624                    else {
1625                        pComponentPrivate->pInputBufHdrPending[pComponentPrivate->nNumInputBufPending++] = pBufHeader;
1626                    }
1627                }
1628            }
1629
1630            eError = G711DECGetCorresponding_LCMLHeader(pBufHeader->pBuffer, OMX_DirInput, &pLcmlHdr);
1631            if (eError != OMX_ErrorNone) {
1632                G711DEC_DPRINT("%d :: Error: Invalid Buffer Came ...\n",__LINE__);
1633                goto EXIT;
1634            }
1635            phandle = (LCML_DSP_INTERFACE *)(
1636                                             ((LCML_CODEC_INTERFACE *)pLcmlHandle->pCodecinterfacehandle)->pCodec);
1637
1638            nFrames = (OMX_U8)(pBufHeader->nFilledLen / RTP_Framesize);
1639            frameType = pBufHeader->pBuffer;
1640            frameType += RTP_Framesize - 1;
1641
1642            if( (pLcmlHdr->pBufferParam->usNbFrames < nFrames) &&
1643                (pLcmlHdr->pFrameParam != NULL)){
1644                OMX_DmmUnMap(phandle->dspCodec->hProc,
1645                             (void*)pLcmlHdr->pBufferParam->pParamElem,
1646                             pLcmlHdr->pDmmBuf->pReserved);
1647
1648                pLcmlHdr->pBufferParam->pParamElem = NULL;
1649
1650                pBufParmsTemp = (OMX_U8*)pLcmlHdr->pFrameParam;
1651                pBufParmsTemp -= 128;
1652                newfree(pBufParmsTemp);
1653                pLcmlHdr->pFrameParam = NULL;
1654            }
1655
1656            if(pLcmlHdr->pFrameParam == NULL ){
1657                G711D_OMX_MALLOC_SIZE(pBufParmsTemp,
1658                                      ((sizeof(G711DEC_FrameStruct)*nFrames) + 256),
1659                                      OMX_U8);
1660
1661                pLcmlHdr->pFrameParam = (G711DEC_FrameStruct*)(pBufParmsTemp + 128);
1662                eError = OMX_DmmMap(phandle->dspCodec->hProc,
1663                                    nFrames*sizeof(G711DEC_FrameStruct),
1664                                    (void*)pLcmlHdr->pFrameParam, (pLcmlHdr->pDmmBuf));
1665
1666                if (eError != OMX_ErrorNone){
1667                    G711DEC_PRINT("OMX_DmmMap ERRROR!!!!\n\n");
1668                    goto EXIT;
1669                }
1670                pLcmlHdr->pBufferParam->pParamElem =
1671                    (G711DEC_FrameStruct *)pLcmlHdr->pDmmBuf->pMapped;
1672            }
1673
1674            if(pBufHeader->nFlags == OMX_BUFFERFLAG_EOS) {
1675                (pLcmlHdr->pFrameParam+(nFrames-1))->usLastFrame = OMX_BUFFERFLAG_EOS;
1676                pComponentPrivate->bPlayCompleteFlag = 1;
1677                pBufHeader->nFlags = 0;
1678            }
1679
1680            pLcmlHdr->pBufferParam->usNbFrames = nFrames;
1681
1682            /*Store tick count information*/
1683            pComponentPrivate->arrBufIndexTick[pComponentPrivate->IpBufindex] = pBufHeader->nTickCount;
1684
1685            /* Store time stamp information */
1686            pComponentPrivate->arrBufIndex[pComponentPrivate->IpBufindex] = pBufHeader->nTimeStamp;
1687            pComponentPrivate->IpBufindex++;
1688            pComponentPrivate->IpBufindex %= pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferCountActual;
1689            if (pComponentPrivate->curState == OMX_StateExecuting) {
1690                if (!G711DEC_IsPending(pComponentPrivate,pBufHeader,OMX_DirInput)) {
1691                    G711DEC_SetPending(pComponentPrivate,pBufHeader,OMX_DirInput);
1692
1693                    eError = LCML_QueueBuffer(pLcmlHandle->pCodecinterfacehandle,
1694                                              EMMCodecInputBuffer,
1695                                              (OMX_U8 *)pBufHeader->pBuffer,
1696                                              STD_G711DEC_BUF_SIZE*nFrames,
1697                                              STD_G711DEC_BUF_SIZE*nFrames,
1698                                              (OMX_U8 *) pLcmlHdr->pBufferParam,
1699                                              sizeof(G711DEC_ParamStruct),
1700                                              NULL);
1701
1702                    if (eError != OMX_ErrorNone) {
1703                        eError = OMX_ErrorHardware;
1704                        goto EXIT;
1705                    }
1706                    pComponentPrivate->lcml_nCntIp++;
1707                    pComponentPrivate->lcml_nIpBuf++;
1708                    pComponentPrivate->num_Sent_Ip_Buff++;
1709                }
1710            }
1711            else if (pComponentPrivate->curState == OMX_StatePause){
1712                pComponentPrivate->pInputBufHdrPending[pComponentPrivate->nNumInputBufPending++] = pBufHeader;
1713            }
1714
1715        }
1716        else {
1717            pComponentPrivate->bBypassDSP = 1;
1718            if (pComponentPrivate->dasfmode == 0 && pBufHeader->nFlags != OMX_BUFFERFLAG_EOS) {
1719                G711DEC_DPRINT("line %d:: Calling EmptyBufferDone\n",__LINE__);
1720                pComponentPrivate->cbInfo.EmptyBufferDone (
1721                                                           pComponentPrivate->pHandle,
1722                                                           pComponentPrivate->pHandle->pApplicationPrivate,
1723                                                           pComponentPrivate->pInputBufferList->pBufHdr[0]
1724                                                           );
1725                pComponentPrivate->nEmptyBufferDoneCount++;
1726            }
1727        }
1728        if(pBufHeader->nFlags == OMX_BUFFERFLAG_EOS)
1729        {
1730            if(pComponentPrivate->dasfmode == 0 && pBufHeader->nFilledLen == 0)
1731            {
1732                pComponentPrivate->pOutputBufferList->pBufHdr[0]->nFlags |= OMX_BUFFERFLAG_EOS;
1733            }
1734
1735            pComponentPrivate->bIsEOFSent = 0;
1736            if(pComponentPrivate->dasfmode == 0) {
1737                pComponentPrivate->pOutputBufferList->pBufHdr[0]->nFlags |= OMX_BUFFERFLAG_EOS;
1738                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1739                                                       pComponentPrivate->pHandle->pApplicationPrivate,
1740                                                       OMX_EventBufferFlag,
1741                                                       pComponentPrivate->pOutputBufferList->pBufHdr[0]->nOutputPortIndex,
1742                                                       pComponentPrivate->pOutputBufferList->pBufHdr[0]->nFlags, NULL);
1743
1744                pComponentPrivate->cbInfo.FillBufferDone (pComponentPrivate->pHandle,
1745                                                          pComponentPrivate->pHandle->pApplicationPrivate,
1746                                                          pComponentPrivate->pOutputBufferList->pBufHdr[0]);
1747
1748                pComponentPrivate->cbInfo.EmptyBufferDone (pComponentPrivate->pHandle,
1749                                                           pComponentPrivate->pHandle->pApplicationPrivate,
1750                                                           pComponentPrivate->pInputBufferList->pBufHdr[0]);
1751                G711DEC_DPRINT ("%d :: Flags has been propagated from input to output port\n",__LINE__);
1752            }
1753            pBufHeader->nFlags = 0;
1754
1755        }
1756        if(pBufHeader->pMarkData){
1757            /* copy mark to output buffer header */
1758            pComponentPrivate->pOutputBufferList->pBufHdr[0]->pMarkData = pBufHeader->pMarkData;
1759            pComponentPrivate->pOutputBufferList->pBufHdr[0]->hMarkTargetComponent = pBufHeader->hMarkTargetComponent;
1760
1761            /* trigger event handler if we are supposed to */
1762            if(pBufHeader->hMarkTargetComponent == pComponentPrivate->pHandle && pBufHeader->pMarkData){
1763                pComponentPrivate_CC->cbInfo.EventHandler(pComponentPrivate->pHandle,
1764                                                          pComponentPrivate->pHandle->pApplicationPrivate,
1765                                                          OMX_EventMark, 0, 0, pBufHeader->pMarkData);
1766            }
1767        }
1768    }
1769    else if (eDir == OMX_DirOutput) {
1770        /* Make sure that output buffer is issued to output stream only when
1771         * there is an outstanding input buffer already issued on input stream
1772         */
1773        if (!(pComponentPrivate->bIsStopping)) {
1774            if (pComponentPrivate->bBypassDSP == 0) {
1775                G711DEC_DPRINT ("%d: Sending Empty OUTPUT BUFFER to Codec = %p\n",__LINE__,pBufHeader->pBuffer);
1776
1777                if (pComponentPrivate->curState == OMX_StateExecuting) {
1778                    if (!G711DEC_IsPending(pComponentPrivate,pBufHeader,OMX_DirOutput)) {
1779                        G711DEC_SetPending(pComponentPrivate,pBufHeader,OMX_DirOutput);
1780
1781
1782                        eError = LCML_QueueBuffer(pLcmlHandle->pCodecinterfacehandle,
1783                                                  EMMCodecOuputBuffer,
1784                                                  (OMX_U8 *)pBufHeader->pBuffer,
1785                                                  (pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->nBufferSize),
1786                                                  (pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->nBufferSize),
1787                                                  NULL, 0, NULL);
1788
1789                        G711DEC_DPRINT("%d :: LCML_QueueBuffer eError 0x%x\n",__LINE__, eError);
1790                        if (eError != OMX_ErrorNone ) {
1791                            G711DEC_DPRINT ("%d :: IssuingDSP OP: Error Occurred\n",__LINE__);
1792                            eError = OMX_ErrorHardware;
1793                            goto EXIT;
1794                        }
1795                        pComponentPrivate->lcml_nOpBuf++;
1796                        pComponentPrivate->num_Op_Issued++;
1797                    }
1798                }
1799                else if (pComponentPrivate->curState == OMX_StatePause){
1800                    pComponentPrivate->pOutputBufHdrPending[pComponentPrivate->nNumOutputBufPending++] = pBufHeader;
1801                    G711DEC_DPRINT("%d :: pComponentPrivate->nNumOutputBufPending 0x%x\n",__LINE__,(int)pComponentPrivate->nNumOutputBufPending);
1802                }
1803            }
1804
1805            else {
1806                pComponentPrivate->cbInfo.FillBufferDone (pComponentPrivate->pHandle,
1807                                                          pComponentPrivate->pHandle->pApplicationPrivate,
1808                                                          pComponentPrivate->pOutputBufferList->pBufHdr[0]);
1809            }
1810        }
1811        else {
1812            if (pComponentPrivate->curState == OMX_StateExecuting) {
1813                if (!G711DEC_IsPending(pComponentPrivate,pBufHeader,OMX_DirOutput)) {
1814
1815                    G711DEC_SetPending(pComponentPrivate,pBufHeader,OMX_DirOutput);
1816                    eError = LCML_QueueBuffer(pLcmlHandle->pCodecinterfacehandle,
1817                                              EMMCodecOuputBuffer, (OMX_U8 *)pBufHeader->pBuffer,
1818                                              (pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->nBufferSize),
1819                                              (pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->nBufferSize),
1820                                              NULL, 0, NULL);
1821
1822                    if (eError != OMX_ErrorNone ) {
1823                        G711DEC_DPRINT ("%d :: IssuingDSP OP: Error Occurred\n",__LINE__);
1824                        eError = OMX_ErrorHardware;
1825                        goto EXIT;
1826                    }
1827                    pComponentPrivate->lcml_nOpBuf++;
1828                    pComponentPrivate->num_Op_Issued++;
1829                }
1830            }
1831            else if (pComponentPrivate->curState == OMX_StatePause){
1832                pComponentPrivate->pOutputBufHdrPending[pComponentPrivate->nNumOutputBufPending++] = pBufHeader;
1833                G711DEC_DPRINT("%d :: pComponentPrivate->nNumOutputBufPending 0x%x\n",__LINE__,(int)pComponentPrivate->nNumOutputBufPending);
1834            }
1835        }
1836    }
1837    else {
1838        eError = OMX_ErrorBadParameter;
1839    }
1840
1841 EXIT:
1842    G711DEC_DPRINT("%d : Exiting from  G711DECHandleDataBuf_FromApp \n",__LINE__);
1843    G711DEC_DPRINT("Returning error %d\n",eError);
1844    return eError;
1845}
1846
1847/*-------------------------------------------------------------------*/
1848/**
1849 * G711DECGetBufferDirection () This function is used by the component thread to
1850 * request a buffer from the application.  Since it was called from 2 places,
1851 * it made sense to turn this into a small function.
1852 *
1853 * @param pData pointer to G711 Decoder Context Structure
1854 * @param pCur pointer to the buffer to be requested to be filled
1855 *
1856 * @retval none
1857 **/
1858/*-------------------------------------------------------------------*/
1859
1860OMX_ERRORTYPE G711DECGetBufferDirection(OMX_BUFFERHEADERTYPE *pBufHeader,
1861                                        OMX_DIRTYPE *eDir)
1862{
1863    OMX_ERRORTYPE eError = OMX_ErrorNone;
1864    G711DEC_COMPONENT_PRIVATE *pComponentPrivate = pBufHeader->pPlatformPrivate;
1865    OMX_U32 nBuf = 0;
1866    OMX_BUFFERHEADERTYPE *pBuf = NULL;
1867    OMX_S16 flag = 1;
1868    OMX_U16 i = 0;
1869
1870    G711DEC_DPRINT ("%d :: Entering G711DECGetBufferDirection Function\n",__LINE__);
1871
1872    /*Search this buffer in input buffers list */
1873    nBuf = pComponentPrivate->pInputBufferList->numBuffers;
1874
1875    for(i=0; i<nBuf; i++) {
1876        pBuf = pComponentPrivate->pInputBufferList->pBufHdr[i];
1877        if(pBufHeader == pBuf) {
1878            *eDir = OMX_DirInput;
1879            G711DEC_DPRINT ("%d :: Buffer %p is INPUT BUFFER\n",__LINE__, pBufHeader);
1880            flag = 0;
1881            goto EXIT;
1882        }
1883    }
1884
1885    /*Search this buffer in input buffers list */
1886    nBuf = pComponentPrivate->pOutputBufferList->numBuffers;
1887
1888    for(i=0; i<nBuf; i++) {
1889        pBuf = pComponentPrivate->pOutputBufferList->pBufHdr[i];
1890        if(pBufHeader == pBuf) {
1891            *eDir = OMX_DirOutput;
1892            G711DEC_DPRINT ("%d :: Buffer %p is OUTPUT BUFFER\n",__LINE__, pBufHeader);
1893            flag = 0;
1894            goto EXIT;
1895        }
1896    }
1897
1898    if (flag == 1) {
1899        G711DEC_DPRINT ("%d :: Buffer %p is Not Found in the List\n",__LINE__,pBufHeader);
1900        eError = OMX_ErrorUndefined;
1901        goto EXIT;
1902    }
1903 EXIT:
1904    G711DEC_DPRINT ("%d :: Exiting G711DECGetBufferDirection Function\n",__LINE__);
1905    return eError;
1906}
1907
1908/*-------------------------------------------------------------------*/
1909/**
1910 * G711DECHandleDataBuf_FromLCML () This function is used by the component thread to
1911 * request a buffer from the application.  Since it was called from 2 places,
1912 * it made sense to turn this into a small function.
1913 *
1914 * @param pData pointer to G711 Decoder Context Structure
1915 * @param pCur pointer to the buffer to be requested to be filled
1916 *
1917 * @retval none
1918 **/
1919/*-------------------------------------------------------------------*/
1920OMX_ERRORTYPE G711DECHandleDataBuf_FromLCML(G711DEC_COMPONENT_PRIVATE* pComponentPrivate,
1921                                            LCML_G711DEC_BUFHEADERTYPE* msgBuffer)
1922{
1923    OMX_ERRORTYPE eError = OMX_ErrorNone;
1924    OMX_U32 checkBeforeFilling = 0;
1925    OMX_U32 inputBufferSize = 0, frameLength = 0;
1926    LCML_DSP_INTERFACE *pLcmlHandle = (LCML_DSP_INTERFACE *)
1927        pComponentPrivate->pLcmlHandle;
1928    LCML_G711DEC_BUFHEADERTYPE *pLcmlHdr = NULL;
1929    OMX_U16 i = 0;
1930
1931    OMX_COMPONENTTYPE* pHandle = (OMX_COMPONENTTYPE*)pComponentPrivate->pHandle;
1932
1933    G711DEC_DPRINT ("%d :: Entering G711DECHandleDataBuf_FromLCML Function\n",__LINE__);
1934
1935    if (!(pComponentPrivate->bIsStopping)) {
1936        if (msgBuffer->eDir == G711DEC_DIRECTION_INPUT) {
1937            G711DEC_DPRINT("%d: Component Sending Empty Input buffer%p to App\n",
1938                           __LINE__,msgBuffer->buffer);
1939            inputBufferSize = (pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferSize);
1940
1941            if ( pComponentPrivate->pInputBufferList->numBuffers == 1 ) {
1942                checkBeforeFilling = inputBufferSize;
1943            }
1944            else {
1945                checkBeforeFilling = inputBufferSize * (pComponentPrivate->pInputBufferList->numBuffers - 1);
1946            }
1947
1948            if  (pComponentPrivate->nHoldLength < checkBeforeFilling) {
1949                G711DEC_DPRINT("line %d:: Calling EmptyBufferDone\n",__LINE__);
1950                G711DEC_DPRINT("pComponentPrivate->nHoldLength = %d\n",(int)pComponentPrivate->nHoldLength);
1951                G711DEC_DPRINT("checkBeforeFilling = %d\n",(int)checkBeforeFilling);
1952
1953                pComponentPrivate->cbInfo.EmptyBufferDone (pHandle,
1954                                                           pHandle->pApplicationPrivate,
1955                                                           msgBuffer->buffer);
1956
1957                pComponentPrivate->nEmptyBufferDoneCount++;
1958                pComponentPrivate->lcml_nIpBuf--;
1959                pComponentPrivate->app_nBuf++;
1960            }
1961
1962            /*
1963             * We possibly have enough data in iHoldBuffer.
1964             * If we don't have enough data to be send, then we need to refill when last buffer is not set
1965             * even though FillThisHwBuffer has already been sent.
1966             * Send QueueBuffer from iHoldBuffer then reflushed the iHoldBuffer.
1967             */
1968            else {
1969                if ( pComponentPrivate->nHoldLength > 0 ) {
1970
1971                    frameLength = (pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferSize);
1972
1973                    if ( pComponentPrivate->nHoldLength >= frameLength ) {
1974                        /* Copy the data from iHoldBuffer to dataPtr */
1975                        if ((msgBuffer->buffer->pBuffer == NULL) ||
1976                           (pComponentPrivate->pHoldBuffer == NULL)) {
1977                           eError = OMX_ErrorBadParameter;
1978			   goto EXIT;
1979			}
1980
1981                        memcpy(msgBuffer->buffer->pBuffer,
1982                               pComponentPrivate->pHoldBuffer,
1983                               frameLength);
1984
1985                        memcpy(pComponentPrivate->pHoldBuffer,
1986                               pComponentPrivate->pHoldBuffer + frameLength,
1987                               pComponentPrivate->nHoldLength - frameLength);
1988
1989                        pComponentPrivate->nHoldLength = pComponentPrivate->nHoldLength - frameLength;
1990                        eError = G711DECGetCorresponding_LCMLHeader(msgBuffer->buffer->pBuffer, OMX_DirInput, &pLcmlHdr);
1991
1992
1993                        for (i=0; i < inputBufferSize; i++) {
1994                            G711DEC_DPRINT("%d::Queueing msgBuffer->buffer->pBuffer[%d] = %x\n",__LINE__,i,msgBuffer->buffer->pBuffer[i]);
1995                        }
1996                        G711DEC_SetPending(pComponentPrivate,msgBuffer->buffer,OMX_DirInput);
1997                        eError = LCML_QueueBuffer(pLcmlHandle->pCodecinterfacehandle,
1998                                                  EMMCodecInputBuffer,
1999                                                  (OMX_U8 *)msgBuffer->buffer->pBuffer,
2000                                                  inputBufferSize,
2001                                                  frameLength,(OMX_U8 *) pLcmlHdr->pIpParam,
2002                                                  sizeof(G711DEC_UAlgInBufParamStruct),
2003                                                  NULL);
2004                    }
2005                    else {
2006                        /*We need to refill more since iHoldBuffer only has partial data.*/
2007
2008                        G711DEC_DPRINT("line %d:: Calling EmptyBufferDone\n",__LINE__);
2009
2010                        if (pComponentPrivate->curState != OMX_StatePause) {
2011                            pComponentPrivate->cbInfo.EmptyBufferDone (
2012                                                                       pHandle,
2013                                                                       pHandle->pApplicationPrivate,
2014                                                                       msgBuffer->buffer);
2015                            pComponentPrivate->nEmptyBufferDoneCount++;
2016                        }
2017                        else {
2018                            pComponentPrivate->pInputBufHdrPending[pComponentPrivate->nNumInputBufPending++] = msgBuffer->buffer;
2019                        }
2020
2021                    }
2022                }
2023            }
2024        }
2025        else if (msgBuffer->eDir == G711DEC_DIRECTION_OUTPUT) {
2026            pComponentPrivate->num_Reclaimed_Op_Buff++;
2027
2028            if (pComponentPrivate->bIsEOFSent) {
2029                msgBuffer->buffer->nFlags |= OMX_BUFFERFLAG_EOS;
2030                pComponentPrivate->bIsEOFSent = 0;
2031            }
2032
2033            /*Copying tick count information to output buffer*/
2034            msgBuffer->buffer->nTickCount = (OMX_U32)pComponentPrivate->arrBufIndexTick[pComponentPrivate->OpBufindex];
2035
2036            /* Copying time stamp information to output buffer */
2037            msgBuffer->buffer->nTimeStamp = (OMX_TICKS)pComponentPrivate->arrBufIndex[pComponentPrivate->OpBufindex];
2038            pComponentPrivate->OpBufindex++;
2039            pComponentPrivate->OpBufindex %= pComponentPrivate->pPortDef[OMX_DirInput]->nBufferCountActual;
2040
2041            G711DEC_DPRINT("Calling FillBufferDone From Line %d, buffer %p\n",__LINE__, msgBuffer->buffer);
2042            G711DEC_DPRINT("%d :: Output buff %p, TimeStamp %lld\n",__LINE__, msgBuffer->buffer, msgBuffer->buffer->nTimeStamp);
2043            pComponentPrivate->cbInfo.FillBufferDone ( pHandle,
2044                                                       pHandle->pApplicationPrivate,
2045                                                       msgBuffer->buffer);
2046            pComponentPrivate->lcml_nOpBuf--;
2047            pComponentPrivate->app_nBuf++;
2048            pComponentPrivate->nFillBufferDoneCount++;
2049            pComponentPrivate->nOutStandingFillDones--;
2050
2051            G711DEC_DPRINT("%d:Incrementing app_nBuf = %d\n",__LINE__,(int)pComponentPrivate->app_nBuf);
2052        }
2053        else {
2054            eError = OMX_ErrorBadParameter;
2055            goto EXIT;
2056        }
2057    }
2058    else {
2059
2060        if (msgBuffer->eDir == G711DEC_DIRECTION_INPUT) {
2061            pComponentPrivate->lcml_nIpBuf--;
2062            G711DEC_DPRINT("line %d:: Calling EmptyBufferDone\n",__LINE__);
2063            pComponentPrivate->cbInfo.EmptyBufferDone (pHandle,
2064                                                       pHandle->pApplicationPrivate,
2065                                                       msgBuffer->buffer);
2066            pComponentPrivate->app_nBuf++;
2067            pComponentPrivate->nEmptyBufferDoneCount++;
2068        }
2069        else if (msgBuffer->eDir == G711DEC_DIRECTION_OUTPUT) {
2070            pComponentPrivate->lcml_nOpBuf--;
2071            pComponentPrivate->num_Reclaimed_Op_Buff++;
2072            G711DEC_DPRINT("%d: Component Sending Filled Output buffer%p to App\n",__LINE__,msgBuffer->buffer);
2073            G711DEC_DPRINT ("Sending Filled OUTPUT BUFFER to App = %p\n",msgBuffer->buffer->pBuffer);
2074
2075            if (pComponentPrivate->bIsEOFSent) {
2076                msgBuffer->buffer->nFlags |= OMX_BUFFERFLAG_EOS;
2077                pComponentPrivate->bIsEOFSent = 0;
2078            }
2079            G711DEC_DPRINT("Calling FillBufferDone From Line %d\n",__LINE__);
2080
2081            pComponentPrivate->cbInfo.FillBufferDone (pHandle,
2082                                                      pHandle->pApplicationPrivate,
2083                                                      msgBuffer->buffer);
2084            pComponentPrivate->app_nBuf++;
2085            pComponentPrivate->nFillBufferDoneCount++;
2086            pComponentPrivate->nOutStandingFillDones--;
2087
2088
2089            G711DEC_DPRINT("%d:Incrementing app_nBuf = %d\n",__LINE__,(int)pComponentPrivate->app_nBuf);
2090        }
2091    }
2092 EXIT:
2093    G711DEC_DPRINT ("%d :: Exiting G711DECHandleDataBuf_FromLCML Function\n",__LINE__);
2094    return eError;
2095}
2096
2097/* -------------------------------------------------------------------*/
2098/**
2099 *  Callback() function will be called LCML component to write the msg
2100 *
2101 * @param msgBuffer                 This buffer will be returned by the LCML
2102 *
2103 * @retval OMX_NoError              Success, ready to roll
2104 *         OMX_Error_BadParameter   The input parameter pointer is null
2105 **/
2106/*-------------------------------------------------------------------*/
2107/*    gLcmlPipeWr = pComponentPrivate->lcml_Pipe[1];*/
2108
2109OMX_ERRORTYPE G711DECLCML_Callback (TUsnCodecEvent event,void * args [10])
2110{
2111    OMX_ERRORTYPE eError = OMX_ErrorNone;
2112    OMX_U8 *pBuffer = args[1];
2113    LCML_G711DEC_BUFHEADERTYPE *pLcmlHdr = NULL;
2114    OMX_S16 ret = 0;
2115
2116#ifdef G711DEC_DEBUG
2117    LCML_DSP_INTERFACE *phandle = (LCML_DSP_INTERFACE *)args[6];
2118#endif
2119
2120    G711DEC_DPRINT ("%d :: Entering the G711DECLCML_Callback Function\n",__LINE__);
2121    G711DEC_DPRINT("args = %d ",(int)args[0]);
2122    G711DEC_DPRINT("event = %d\n",event);
2123
2124
2125
2126    switch(event) {
2127
2128    case EMMCodecDspError:
2129        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecDspError\n");
2130        break;
2131
2132    case EMMCodecInternalError:
2133        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecInternalError\n");
2134        break;
2135
2136    case EMMCodecInitError:
2137        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecInitError\n");
2138        break;
2139
2140    case EMMCodecDspMessageRecieved:
2141        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecDspMessageRecieved\n");
2142        break;
2143
2144    case EMMCodecBufferProcessed:
2145        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecBufferProcessed\n");
2146        break;
2147
2148    case EMMCodecProcessingStarted:
2149        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecProcessingStarted\n");
2150        break;
2151
2152    case EMMCodecProcessingPaused:
2153        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecProcessingPaused\n");
2154        break;
2155
2156    case EMMCodecProcessingStoped:
2157        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecProcessingStoped\n");
2158        break;
2159
2160    case EMMCodecProcessingEof:
2161        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecProcessingEof\n");
2162        break;
2163
2164    case EMMCodecBufferNotProcessed:
2165        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecBufferNotProcessed\n");
2166        break;
2167
2168    case EMMCodecAlgCtrlAck:
2169        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecAlgCtrlAck\n");
2170        break;
2171
2172    case EMMCodecStrmCtrlAck:
2173        G711DEC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecStrmCtrlAck\n");
2174        break;
2175    }
2176
2177    if(event == EMMCodecBufferProcessed)
2178    {
2179        if( (OMX_U32)args [0] == EMMCodecInputBuffer) {
2180            G711DEC_DPRINT("%d :: Input: pBufferr = %p\n",__LINE__, pBuffer);
2181
2182            eError = G711DECGetCorresponding_LCMLHeader(pBuffer, OMX_DirInput, &pLcmlHdr);
2183            if (eError != OMX_ErrorNone) {
2184                G711DEC_DPRINT("%d :: Error: Invalid Buffer Came ...\n",__LINE__);
2185                goto EXIT;
2186            }
2187            G711DEC_ClearPending(pComponentPrivate_CC,pLcmlHdr->buffer,OMX_DirInput);
2188            ret = (OMX_U16) G711DECHandleDataBuf_FromLCML(pComponentPrivate_CC, pLcmlHdr);
2189            if (ret != OMX_ErrorNone) {
2190                G711DEC_DPRINT ("%d :: Error in Writing to the Data pipe\n", __LINE__);
2191                G711DEC_PRINT("Error writting pipe\n");
2192                eError = OMX_ErrorHardware;
2193                goto EXIT;
2194            }
2195        }
2196        else if ((OMX_U32)args [0] == EMMCodecOuputBuffer) {
2197
2198            G711DEC_DPRINT("%d :: Output: pBufferr = %p\n",__LINE__, pBuffer);
2199
2200            eError = G711DECGetCorresponding_LCMLHeader(pBuffer, OMX_DirOutput, &pLcmlHdr);
2201            if (eError != OMX_ErrorNone) {
2202                G711DEC_DPRINT("%d :: Error: Invalid Buffer Came ...\n",__LINE__);
2203                goto EXIT;
2204            }
2205            if (!pComponentPrivate_CC->bStopSent)
2206                pLcmlHdr->buffer->nFilledLen = (OMX_U32)args[2];
2207            else
2208                pLcmlHdr->buffer->nFilledLen = 0;
2209            G711DEC_DPRINT("G711DECLCML_Callback::: pLcmlHdr->buffer->nFilledLen = %d\n",(int)pLcmlHdr->buffer->nFilledLen);
2210            pComponentPrivate_CC->lcml_nCntOpReceived++;
2211
2212            G711DEC_ClearPending(pComponentPrivate_CC,pLcmlHdr->buffer,OMX_DirOutput);
2213            ret = (OMX_U16) G711DECHandleDataBuf_FromLCML(pComponentPrivate_CC, pLcmlHdr);
2214            pComponentPrivate_CC->nOutStandingFillDones++;
2215
2216            if (ret != OMX_ErrorNone) {
2217                G711DEC_DPRINT ("%d :: Error in Writing to the Data pipe\n", __LINE__);
2218                eError = OMX_ErrorHardware;
2219                goto EXIT;
2220            }
2221        }
2222    } else if (event == EMMCodecStrmCtrlAck) {
2223        G711DEC_DPRINT("%d :: GOT MESSAGE USN_DSPACK_STRMCTRL ----\n",__LINE__);
2224    }
2225    else if(event == EMMCodecProcessingStoped) {
2226        G711DEC_DPRINT("pComponentPrivate_CC->bNoIdleOnStop = %d\n",(int)pComponentPrivate_CC->bNoIdleOnStop);
2227
2228        if (!pComponentPrivate_CC->bNoIdleOnStop) {
2229            pComponentPrivate_CC->curState = OMX_StateIdle;
2230#ifdef RESOURCE_MANAGER_ENABLED
2231            eError = RMProxy_NewSendCommand(pComponentPrivate_CC->pHandle,
2232                                            RMProxy_StateSet, OMX_G711_Decoder_COMPONENT,
2233                                            OMX_StateIdle, 1234, NULL);
2234#endif
2235            pComponentPrivate_CC->cbInfo.EventHandler(pComponentPrivate_CC->pHandle,
2236                                                      pComponentPrivate_CC->pHandle->pApplicationPrivate,
2237                                                      OMX_EventCmdComplete,
2238                                                      OMX_CommandStateSet,
2239                                                      pComponentPrivate_CC->curState,
2240                                                      NULL);
2241        }
2242        else {
2243            pComponentPrivate_CC->bDspStoppedWhileExecuting = OMX_TRUE;
2244            pComponentPrivate_CC->bNoIdleOnStop= OMX_FALSE;
2245        }
2246
2247    }
2248    else if (event == EMMCodecProcessingPaused) {
2249        pComponentPrivate_CC->curState = OMX_StatePause;
2250        pComponentPrivate_CC->cbInfo.EventHandler( pComponentPrivate_CC->pHandle,
2251                                                   pComponentPrivate_CC->pHandle->pApplicationPrivate,
2252                                                   OMX_EventCmdComplete,
2253                                                   OMX_CommandStateSet,
2254                                                   pComponentPrivate_CC->curState,
2255                                                   NULL);
2256    }
2257
2258
2259
2260    if(event == EMMCodecDspMessageRecieved) {
2261        G711DEC_DPRINT("%d :: commandedState  = %d\n",__LINE__,(int)args[0]);
2262        G711DEC_DPRINT("%d :: arg1 = %d\n",__LINE__,(int)args[1]);
2263        G711DEC_DPRINT("%d :: arg2 = %d\n",__LINE__,(int)args[2]);
2264    }
2265
2266 EXIT:
2267    G711DEC_DPRINT ("%d :: Exiting the G711DECLCML_Callback Function\n",__LINE__);
2268    return eError;
2269}
2270/* -------------------------------------------------------------------*/
2271/**
2272 * G711DEC_GetCorrespondingLCMLHeader() function will be called by LCML_Callback
2273 * component to write the msg
2274 * @param *pBuffer,          Event which gives to details about USN status
2275 * @param G711DEC_LCML_BUFHEADERTYPE **ppLcmlHdr
2276 * @param  OMX_DIRTYPE eDir this gives direction of the buffer
2277 * @retval OMX_NoError              Success, ready to roll
2278 *         OMX_Error_BadParameter   The input parameter pointer is null
2279 **/
2280/* -------------------------------------------------------------------*/
2281OMX_ERRORTYPE G711DECGetCorresponding_LCMLHeader(OMX_U8 *pBuffer,
2282                                                 OMX_DIRTYPE eDir,
2283                                                 LCML_G711DEC_BUFHEADERTYPE **ppLcmlHdr)
2284{
2285    OMX_ERRORTYPE eError = OMX_ErrorNone;
2286
2287    LCML_G711DEC_BUFHEADERTYPE *pLcmlBufHeader = NULL;
2288    OMX_S16 nIpBuf = (OMX_S16) pComponentPrivate_CC->pInputBufferList->numBuffers;
2289    OMX_S16 nOpBuf = (OMX_S16) pComponentPrivate_CC->pOutputBufferList->numBuffers;
2290    OMX_U16 i = 0;
2291
2292    while (!pComponentPrivate_CC->bInitParamsInitialized) {
2293        G711DEC_DPRINT("Waiting for init to complete\n");
2294
2295#ifndef UNDER_CE
2296        sched_yield();
2297#else
2298        Sleep(0);
2299#endif
2300    }
2301
2302    G711DEC_DPRINT("%d :: Inside G711DECGetCorresponding_LCMLHeader..\n",__LINE__);
2303    G711DEC_DPRINT("pComponentPrivate_CC = %p\n",pComponentPrivate_CC);
2304    G711DEC_DPRINT("eDir = %d\n",eDir);
2305
2306    if(eDir == OMX_DirInput) {
2307        G711DEC_DPRINT("Line %d\n",__LINE__);
2308        G711DEC_DPRINT("%d :: Inside G711DECGetCorresponding_LCMLHeader..\n",__LINE__);
2309        G711DEC_DPRINT("pComponentPrivate_CC = %p\n",pComponentPrivate_CC);
2310
2311        pLcmlBufHeader = pComponentPrivate_CC->pLcmlBufHeader[G711DEC_INPUT_PORT];
2312        G711DEC_DPRINT("%d :: Inside G711DECGetCorresponding_LCMLHeader..\n",__LINE__);
2313
2314        for(i=0; i<nIpBuf; i++) {
2315            G711DEC_DPRINT("%d :: Inside G711DECGetCorresponding_LCMLHeader..\n",__LINE__);
2316            G711DEC_DPRINT("pBuffer = %p\n",pBuffer);
2317            G711DEC_DPRINT("%d :: Inside G711DECGetCorresponding_LCMLHeader..\n",__LINE__);
2318            G711DEC_DPRINT("pLcmlBufHeader->buffer->pBuffer = %p\n",pLcmlBufHeader->buffer->pBuffer);
2319            G711DEC_DPRINT("%d :: Inside G711DECGetCorresponding_LCMLHeader..\n",__LINE__);
2320
2321            if(pBuffer == pLcmlBufHeader->buffer->pBuffer) {
2322                G711DEC_DPRINT("%d :: Inside G711DECGetCorresponding_LCMLHeader..\n",__LINE__);
2323                *ppLcmlHdr = pLcmlBufHeader;
2324                G711DEC_DPRINT("%d::Corresponding LCML Header Found\n",__LINE__);
2325                goto EXIT;
2326            }
2327            pLcmlBufHeader++;
2328        }
2329    }
2330    else if (eDir == OMX_DirOutput) {
2331        G711DEC_DPRINT("Line %d\n",__LINE__);
2332        pLcmlBufHeader = pComponentPrivate_CC->pLcmlBufHeader[G711DEC_OUTPUT_PORT];
2333        G711DEC_DPRINT("Line %d\n",__LINE__);
2334
2335        for(i=0; i<nOpBuf; i++) {
2336            G711DEC_DPRINT("Line %d\n",__LINE__);
2337            if(pBuffer == pLcmlBufHeader->buffer->pBuffer) {
2338
2339                G711DEC_DPRINT("pBuffer = %p\n",pBuffer);
2340                G711DEC_DPRINT("pLcmlBufHeader->buffer->pBuffer = %p\n",pLcmlBufHeader->buffer->pBuffer);
2341                *ppLcmlHdr = pLcmlBufHeader;
2342                G711DEC_DPRINT("Line %d\n",__LINE__);
2343                G711DEC_DPRINT("%d::Corresponding LCML Header Found\n",__LINE__);
2344                G711DEC_DPRINT("Line %d\n",__LINE__);
2345
2346                goto EXIT;
2347            }
2348            pLcmlBufHeader++;
2349        }
2350    } else {
2351        G711DEC_DPRINT("Line %d\n",__LINE__);
2352        G711DEC_DPRINT("%d:: Invalid Buffer Type :: exiting...\n",__LINE__);
2353    }
2354    G711DEC_DPRINT("Line %d\n",__LINE__);
2355
2356 EXIT:
2357    return eError;
2358}
2359
2360
2361#ifndef UNDER_CE
2362
2363OMX_HANDLETYPE G711DECGetLCMLHandle()
2364{
2365    void *handle = NULL;
2366    OMX_ERRORTYPE (*fpGetHandle)(OMX_HANDLETYPE);
2367    OMX_HANDLETYPE pHandle = NULL;
2368    char *error = NULL;
2369    OMX_ERRORTYPE eError = OMX_ErrorNone;
2370
2371    G711DEC_DPRINT("G711DECGetLCMLHandle %d\n",__LINE__);
2372    handle = dlopen("libLCML.so", RTLD_LAZY);
2373    if (!handle) {
2374        fputs(dlerror(), stderr);
2375        goto EXIT;
2376    }
2377
2378    fpGetHandle = dlsym (handle, "GetHandle");
2379    if ((error = dlerror()) != NULL) {
2380        fputs(error, stderr);
2381        goto EXIT;
2382    }
2383    eError = (*fpGetHandle)(&pHandle);
2384    if(eError != OMX_ErrorNone) {
2385        eError = OMX_ErrorUndefined;
2386        G711DEC_DPRINT("eError != OMX_ErrorNone...\n");
2387        pHandle = NULL;
2388        goto EXIT;
2389    }
2390
2391    pComponentPrivate_CC->bLcmlHandleOpened = 1;
2392
2393 EXIT:
2394    G711DEC_DPRINT("G711DECGetLCMLHandle returning %p\n",pHandle);
2395
2396    return pHandle;
2397}
2398
2399
2400#else
2401/*=======================================================================*/
2402/**WINDOWS Explicit dll load procedure                                                            */
2403/**                                                                         */
2404/*=======================================================================*/
2405OMX_HANDLETYPE G711DECGetLCMLHandle()
2406{
2407    typedef OMX_ERRORTYPE (*LPFNDLLFUNC1)(OMX_HANDLETYPE);
2408    OMX_HANDLETYPE pHandle = NULL;
2409    OMX_ERRORTYPE eError = OMX_ErrorNone;
2410    HINSTANCE hDLL;               /* Handle to DLL */
2411    LPFNDLLFUNC1 fpGetHandle1;
2412
2413
2414
2415    hDLL = LoadLibraryEx(TEXT("OAF_BML.dll"), NULL,0);
2416    if (hDLL == NULL)
2417    {
2418        /* fputs(dlerror(), stderr); */
2419        G711DEC_DPRINT("BML Load Failed!!!\n");
2420        return pHandle;
2421    }
2422
2423    fpGetHandle1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,TEXT("GetHandle"));
2424    if (!fpGetHandle1)
2425    {
2426        /* handle the error */
2427        FreeLibrary(hDLL);
2428        return pHandle;
2429    }
2430
2431    /* call the function */
2432    eError = fpGetHandle1(&pHandle);
2433    if(eError != OMX_ErrorNone) {
2434        eError = OMX_ErrorUndefined;
2435        G711DEC_DPRINT("eError != OMX_ErrorNone...\n");
2436        pHandle = NULL;
2437        return pHandle;
2438    }
2439    return pHandle;
2440}
2441
2442
2443#endif
2444/*=======================================================================*/
2445/** FREE HANDLE                                                                          */
2446/**                                                                         */
2447/*=======================================================================*/
2448#ifndef UNDER_CE
2449
2450OMX_ERRORTYPE G711DECFreeLCMLHandle()
2451{
2452
2453    OMX_S16 retValue = 0;
2454    OMX_ERRORTYPE eError = OMX_ErrorNone;
2455
2456    if (pComponentPrivate_CC->bLcmlHandleOpened) {
2457        retValue = dlclose(pComponentPrivate_CC->pLcmlHandle);
2458
2459        if (retValue != 0) {
2460            eError = OMX_ErrorUndefined;
2461        }
2462        pComponentPrivate_CC->bLcmlHandleOpened = 0;
2463    }
2464
2465    return eError;
2466}
2467#else
2468
2469/*=======================================================================*/
2470/** FREE HANDLE                                                                          */
2471/**                                                                         */
2472/*=======================================================================*/
2473OMX_ERRORTYPE G711DECFreeLCMLHandle()
2474{
2475
2476    OMX_S16 retValue = 0;
2477    OMX_ERRORTYPE eError = OMX_ErrorNone;
2478
2479    if (pComponentPrivate_CC->bLcmlHandleOpened) {
2480
2481        retValue = FreeLibrary(pComponentPrivate_CC->pLcmlHandle);
2482        if (retValue == 0) {          /* Zero Indicates failure */
2483            eError = OMX_ErrorUndefined;
2484        }
2485        pComponentPrivate_CC->bLcmlHandleOpened = 0;
2486    }
2487
2488    return eError;
2489}
2490
2491/*=======================================================================*/
2492/** SET PENDING                                                                          */
2493/**                                                                         */
2494/*=======================================================================*/
2495#endif
2496void G711DEC_SetPending(G711DEC_COMPONENT_PRIVATE *pComponentPrivate,
2497                        OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir)
2498{
2499    OMX_U16 i = 0;
2500    if (eDir == OMX_DirInput) {
2501        for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
2502            if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
2503                pComponentPrivate->pInputBufferList->bBufferPending[i] = 1;
2504                G711DEC_DPRINT("*******************INPUT BUFFER %d IS PENDING******************************\n",i);
2505            }
2506        }
2507    }
2508    else {
2509        for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
2510            if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
2511                pComponentPrivate->pOutputBufferList->bBufferPending[i] = 1;
2512                G711DEC_DPRINT("*******************OUTPUT BUFFER %d IS PENDING******************************\n",i);
2513            }
2514        }
2515    }
2516}
2517/*=======================================================================*/
2518/** CLEAR PENDING                                                                            */
2519/**                                                                         */
2520/*=======================================================================*/
2521void G711DEC_ClearPending(G711DEC_COMPONENT_PRIVATE *pComponentPrivate,
2522                          OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir)
2523{
2524    OMX_U16 i = 0;
2525
2526    if (eDir == OMX_DirInput) {
2527        for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
2528            if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
2529                pComponentPrivate->pInputBufferList->bBufferPending[i] = 0;
2530                G711DEC_DPRINT("*******************INPUT BUFFER %d IS RECLAIMED******************************\n",i);
2531            }
2532        }
2533    }
2534    else {
2535        for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
2536            if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
2537                pComponentPrivate->pOutputBufferList->bBufferPending[i] = 0;
2538                G711DEC_DPRINT("*******************OUTPUT BUFFER %d IS RECLAIMED ******************************\n",i);
2539            }
2540        }
2541    }
2542}
2543
2544OMX_U32 G711DEC_IsPending(G711DEC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir)
2545{
2546    OMX_U16 i = 0;
2547
2548    if (eDir == OMX_DirInput) {
2549        for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
2550            if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
2551                return pComponentPrivate->pInputBufferList->bBufferPending[i];
2552            }
2553        }
2554    }
2555    else {
2556        for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
2557            if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
2558                return pComponentPrivate->pOutputBufferList->bBufferPending[i];
2559            }
2560        }
2561    }
2562    return -1;
2563}
2564
2565
2566OMX_U32 G711DEC_IsValid(G711DEC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U8 *pBuffer, OMX_DIRTYPE eDir)
2567{
2568    OMX_U16 i = 0;
2569    OMX_S16 found=0;
2570
2571    if (eDir == OMX_DirInput) {
2572        for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
2573            if (pBuffer == pComponentPrivate->pInputBufferList->pBufHdr[i]->pBuffer) {
2574                found = 1;
2575            }
2576        }
2577    }
2578    else {
2579        for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
2580            if (pBuffer == pComponentPrivate->pOutputBufferList->pBufHdr[i]->pBuffer) {
2581                found = 1;
2582            }
2583        }
2584    }
2585    return found;
2586}
2587
2588/*=======================================================================*/
2589/** G711DECFill_LCMLInitParamsEx                                                                    */
2590/**                                                                         */
2591/*=======================================================================*/
2592OMX_ERRORTYPE  G711DECFill_LCMLInitParamsEx (OMX_HANDLETYPE  pComponent )
2593{
2594    OMX_ERRORTYPE eError = OMX_ErrorNone;
2595    OMX_U32 nIpBuf = 0,nIpBufSize = 0,nOpBuf = 0,nOpBufSize = 0;
2596    OMX_U16 i = 0;
2597    OMX_BUFFERHEADERTYPE *pTemp = NULL;
2598    OMX_S16 size_lcml = 0;
2599    LCML_STRMATTR *strmAttr = NULL;
2600
2601    LCML_DSP_INTERFACE *pHandle = (LCML_DSP_INTERFACE *)pComponent;
2602    G711DEC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
2603    LCML_G711DEC_BUFHEADERTYPE *pTemp_lcml = NULL;
2604
2605    G711DEC_DPRINT("%d :: G711DECFill_LCMLInitParams\n ",__LINE__);
2606    G711DEC_DPRINT("%d :: pHandle = %p\n",__LINE__,pHandle);
2607    G711DEC_DPRINT("%d :: pHandle->pComponentPrivate = %p\n",__LINE__,pHandle->pComponentPrivate);
2608    pComponentPrivate = pHandle->pComponentPrivate;
2609
2610    nIpBuf = pComponentPrivate->pInputBufferList->numBuffers;
2611
2612    nIpBufSize = (pComponentPrivate->pPortDef[G711DEC_INPUT_PORT]->nBufferSize);
2613
2614    nOpBuf = pComponentPrivate->pOutputBufferList->numBuffers;
2615    nOpBufSize = (pComponentPrivate->pPortDef[G711DEC_OUTPUT_PORT]->nBufferSize);
2616
2617
2618    size_lcml = (OMX_U16) (nIpBuf * sizeof(LCML_G711DEC_BUFHEADERTYPE));
2619    G711D_OMX_MALLOC_SIZE(pTemp_lcml, size_lcml, LCML_G711DEC_BUFHEADERTYPE);
2620    pComponentPrivate->pLcmlBufHeader[G711DEC_INPUT_PORT] = pTemp_lcml;
2621
2622    for (i=0; i<nIpBuf; i++) {
2623        pTemp = pComponentPrivate->pInputBufferList->pBufHdr[i];
2624        pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
2625        pTemp->nAllocLen = nIpBufSize;
2626        pTemp->nFilledLen = nIpBufSize;
2627        pTemp->nVersion.s.nVersionMajor = G711DEC_MAJOR_VER;
2628        pTemp->nVersion.s.nVersionMinor = G711DEC_MINOR_VER;
2629        pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
2630        pTemp->nTickCount = NOT_USED;
2631        pTemp_lcml->buffer = pTemp;
2632        pTemp_lcml->eDir = OMX_DirInput;
2633
2634        G711D_OMX_MALLOC(pTemp_lcml->pIpParam, G711DEC_UAlgInBufParamStruct);
2635
2636        pTemp_lcml->pIpParam->usFrameLost = 0;
2637        pTemp_lcml->pIpParam->usEndOfFile = 0;
2638
2639        G711D_OMX_MALLOC(pTemp_lcml->pDmmBuf,DMM_BUFFER_OBJ);
2640        /* This means, it is not a last buffer. This flag is to be modified by the application to indicate the last buffer */
2641        pTemp->nFlags = NORMAL_BUFFER;
2642        pTemp_lcml++;
2643    }
2644
2645    /* Allocate memory for all output buffer headers. This memory pointer will be sent to LCML */
2646    size_lcml = (OMX_U16) (nOpBuf * sizeof(LCML_G711DEC_BUFHEADERTYPE) );
2647    G711D_OMX_MALLOC_SIZE(pTemp_lcml, size_lcml, LCML_G711DEC_BUFHEADERTYPE);
2648    pComponentPrivate->pLcmlBufHeader[G711DEC_OUTPUT_PORT] = pTemp_lcml;
2649
2650    for (i=0; i<nOpBuf; i++) {
2651        pTemp = pComponentPrivate->pOutputBufferList->pBufHdr[i];
2652        pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
2653        pTemp->nAllocLen = nOpBufSize;
2654        pTemp->nFilledLen = nOpBufSize;
2655        pTemp->nVersion.s.nVersionMajor = G711DEC_MAJOR_VER;
2656        pTemp->nVersion.s.nVersionMinor = G711DEC_MINOR_VER;
2657        pComponentPrivate->nVersion = pTemp->nVersion.nVersion;
2658        pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
2659        pTemp->nTickCount = NOT_USED;
2660
2661        /* This means, it is not a last buffer. This flag is to be modified by the application to indicate the last buffer */
2662        pTemp_lcml->buffer = pTemp;
2663        pTemp_lcml->eDir = OMX_DirOutput;
2664        G711DEC_DPRINT("%d:::pTemp_lcml = %p\n",__LINE__,pTemp_lcml);
2665        G711DEC_DPRINT("%d:::pTemp_lcml->buffer = %p\n",__LINE__,pTemp_lcml->buffer);
2666
2667        pTemp->nFlags = NORMAL_BUFFER;
2668
2669        pTemp++;
2670        pTemp_lcml++;
2671    }
2672    pComponentPrivate->bPortDefsAllocated = 1;
2673    G711DEC_DPRINT("%d :: Exiting G711DECFill_LCMLInitParams",__LINE__);
2674
2675    pComponentPrivate->bInitParamsInitialized = 1;
2676 EXIT:
2677    if(eError == OMX_ErrorInsufficientResources)
2678    {
2679        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pLcmlBufHeader[G711DEC_INPUT_PORT]);
2680        OMX_G711DECMEMFREE_STRUCT(strmAttr);
2681	if (pTemp_lcml != NULL) {
2682	    OMX_G711DECMEMFREE_STRUCT(pTemp_lcml->pIpParam);
2683	}
2684        OMX_G711DECMEMFREE_STRUCT(pComponentPrivate->pLcmlBufHeader[G711DEC_OUTPUT_PORT]);
2685    }
2686    return eError;
2687}
2688
2689/** ========================================================================
2690 *  OMX_DmmMap () method is used to allocate the memory using DMM.
2691 *
2692 *  @param ProcHandle -  Component identification number
2693 *  @param size  - Buffer header address, that needs to be sent to codec
2694 *  @param pArmPtr - Message used to send the buffer to codec
2695 *  @param pDmmBuf - buffer id
2696 *
2697 *  @retval OMX_ErrorNone  - Success
2698 *          OMX_ErrorHardware  -  Hardware Error
2699 ** ==========================================================================*/
2700OMX_ERRORTYPE OMX_DmmMap(DSP_HPROCESSOR ProcHandle,
2701                         int size,
2702                         void* pArmPtr,
2703                         DMM_BUFFER_OBJ* pDmmBuf)
2704{
2705    OMX_ERRORTYPE eError = OMX_ErrorUndefined;
2706    DSP_STATUS status = DSP_SOK;
2707    int nSizeReserved = 0;
2708
2709    G711DEC_PRINT("OMX_DmmMap %d\n",__LINE__);
2710    if(pDmmBuf == NULL)
2711    {
2712        G711DEC_PRINT("pBuf is NULL\n");
2713        eError = OMX_ErrorBadParameter;
2714        goto EXIT;
2715    }
2716
2717    if(pArmPtr == NULL)
2718    {
2719        G711DEC_PRINT("pBuf is NULL\n");
2720        eError = OMX_ErrorBadParameter;
2721        goto EXIT;
2722    }
2723
2724    /* Allocate */
2725    pDmmBuf->pAllocated = pArmPtr;
2726    /* Reserve */
2727    nSizeReserved = ROUND_TO_PAGESIZE(size) + 2*DMM_PAGE_SIZE ;
2728    status = DSPProcessor_ReserveMemory(ProcHandle, nSizeReserved, &(pDmmBuf->pReserved));
2729    G711DEC_DPRINT("\nOMX Reserve DSP: %p\n",pDmmBuf->pReserved);
2730
2731    if(DSP_FAILED(status))
2732    {
2733        G711DEC_PRINT("DSPProcessor_ReserveMemory() failed - error 0x%x", (int)status);
2734        eError = OMX_ErrorHardware;
2735        goto EXIT;
2736    }
2737    pDmmBuf->nSize = size;
2738    G711DEC_DPRINT(" DMM MAP Reserved: %p, size 0x%x (%d)\n", pDmmBuf->pReserved,nSizeReserved,nSizeReserved);
2739
2740    /* Map */
2741    status = DSPProcessor_Map(ProcHandle,
2742                              pDmmBuf->pAllocated,/* Arm addres of data to Map on DSP*/
2743                              size , /* size to Map on DSP*/
2744                              pDmmBuf->pReserved, /* reserved space */
2745                              &(pDmmBuf->pMapped), /* returned map pointer */
2746                              0); /* final param is reserved.  set to zero. */
2747
2748    if(DSP_FAILED(status))
2749    {
2750        G711DEC_PRINT("DSPProcessor_Map() failed - error 0x%x", (int)status);
2751        eError = OMX_ErrorHardware;
2752        goto EXIT;
2753    }
2754    G711DEC_DPRINT("DMM Mapped: %p, size 0x%x (%d)\n",pDmmBuf->pMapped, size,size);
2755
2756    /* Issue an initial memory flush to ensure cache coherency */
2757    status = DSPProcessor_FlushMemory(ProcHandle, pDmmBuf->pAllocated, size, 0);
2758    if(DSP_FAILED(status))
2759    {
2760        G711DEC_PRINT("Unable to flush mapped buffer: error 0x%x",(int)status);
2761        goto EXIT;
2762    }
2763    eError = OMX_ErrorNone;
2764
2765 EXIT:
2766    return eError;
2767}
2768
2769/** ========================================================================
2770 *  OMX_DmmUnMap () method is used to de-allocate the memory using DMM.
2771 *
2772 *  @param ProcHandle -  Component identification number
2773 *  @param pMapPtr  - Map address
2774 *  @param pResPtr - reserve adress
2775 *
2776 *  @retval OMX_ErrorNone  - Success
2777 *          OMX_ErrorHardware  -  Hardware Error
2778 ** ==========================================================================*/
2779OMX_ERRORTYPE OMX_DmmUnMap(DSP_HPROCESSOR ProcHandle, void* pMapPtr, void* pResPtr)
2780{
2781    G711DEC_PRINT("OMX_DmmUnMap %d\n",__LINE__);
2782    DSP_STATUS status = DSP_SOK;
2783    OMX_ERRORTYPE eError = OMX_ErrorNone;
2784    G711DEC_DPRINT("\nOMX UnReserve DSP: %p\n",pResPtr);
2785
2786    if(pMapPtr == NULL)
2787    {
2788        G711DEC_PRINT("pMapPtr is NULL\n");
2789        eError = OMX_ErrorBadParameter;
2790        goto EXIT;
2791    }
2792    if(pResPtr == NULL)
2793    {
2794        G711DEC_PRINT("pResPtr is NULL\n");
2795        eError = OMX_ErrorBadParameter;
2796        goto EXIT;
2797    }
2798    status = DSPProcessor_UnMap(ProcHandle,pMapPtr);
2799    if(DSP_FAILED(status))
2800    {
2801        G711DEC_PRINT("DSPProcessor_UnMap() failed - error 0x%x",(int)status);
2802    }
2803
2804    G711DEC_DPRINT("unreserving  structure =0x%p\n",pResPtr );
2805    status = DSPProcessor_UnReserveMemory(ProcHandle,pResPtr);
2806    if(DSP_FAILED(status))
2807    {
2808        G711DEC_PRINT("DSPProcessor_UnReserveMemory() failed - error 0x%x", (int)status);
2809    }
2810
2811 EXIT:
2812    return eError;
2813}
2814
2815#ifdef RESOURCE_MANAGER_ENABLED
2816/***********************************
2817 *  Callback to the RM                                       *
2818 ***********************************/
2819void G711DEC_ResourceManagerCallback(RMPROXY_COMMANDDATATYPE cbData)
2820{
2821    OMX_COMMANDTYPE Cmd = OMX_CommandStateSet;
2822    OMX_STATETYPE state = OMX_StateIdle;
2823    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)cbData.hComponent;
2824    G711DEC_COMPONENT_PRIVATE *pCompPrivate = NULL;
2825
2826    pCompPrivate = (G711DEC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
2827
2828    if (*(cbData.RM_Error) == OMX_RmProxyCallback_ResourcesPreempted){
2829        if (pCompPrivate->curState == OMX_StateExecuting ||
2830            pCompPrivate->curState == OMX_StatePause) {
2831
2832            write (pCompPrivate->cmdPipe[1], &Cmd, sizeof(Cmd));
2833            write (pCompPrivate->cmdDataPipe[1], &state ,sizeof(OMX_U32));
2834
2835            pCompPrivate->bPreempted = 1;
2836        }
2837    }
2838    else if (*(cbData.RM_Error) == OMX_RmProxyCallback_ResourcesAcquired){
2839        pCompPrivate->cbInfo.EventHandler ( pHandle,
2840                                            pHandle->pApplicationPrivate,
2841                                            OMX_EventResourcesAcquired,
2842                                            0, 0, NULL);
2843    }
2844}
2845#endif
2846