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