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            eError = OMX_ErrorInsufficientResources;
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*)eError;
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        eError = OMX_ErrorInsufficientResources;
484        goto EXIT;
485    }
486    memset (pTemp_lcml, 0, size_lcml);
487    pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT] = pTemp_lcml;
488
489
490    G722ENC_DPRINT("nOpBuf = %d\n",nOpBuf);
491
492    for (i=0; i<nOpBuf; i++) {
493        pTemp = pComponentPrivate->pOutputBufferList->pBufHdr[i];
494        pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
495        pTemp->nAllocLen = nOpBufSize;
496        pTemp->nFilledLen = nOpBufSize;
497        pTemp->nVersion.s.nVersionMajor = G722ENC_MAJOR_VER;
498        pTemp->nVersion.s.nVersionMinor = G722ENC_MINOR_VER;
499        pComponentPrivate->nVersion = pTemp->nVersion.nVersion;
500        pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
501        pTemp->nTickCount = NOT_USED;
502        pTemp_lcml->pBufHdr = pTemp;
503        pTemp_lcml->eDir = OMX_DirOutput;
504        pTemp_lcml->pOtherParams[i] = NULL;
505
506        pTemp->nFlags = NORMAL_BUFFER;
507        ((G722ENC_COMPONENT_PRIVATE *)pTemp->pPlatformPrivate)->pHandle = pHandle;
508        G722ENC_DPRINT("%d ::Comp:  >>>>>>>>>>>>> OutBuffHeader[%d] = %p\n",
509                       __LINE__, i, pTemp);
510        G722ENC_DPRINT("%d ::Comp:  >>>> OutBuffHeader[%d]->pBuffer = %p\n",
511                       __LINE__, i, pTemp->pBuffer);
512        G722ENC_DPRINT("%d ::Comp: Op : pTemp_lcml[%d] = %p\n", __LINE__, i, pTemp_lcml);
513        pTemp++;
514        pTemp_lcml++;
515    }
516    pComponentPrivate->bPortDefsAllocated = 1;
517
518    G722ENC_DPRINT("%d :: Exiting G722ENC_Fill_LCMLInitParams",__LINE__);
519
520    pComponentPrivate->bInitParamsInitialized = 1;
521 EXIT:
522    return eError;
523}
524
525/* ================================================================================= */
526/**
527 * @fn G722Enc_StartCompThread() description for G722Enc_StartCompThread
528 G722Enc_StartCompThread().
529 This function is called by the component to create
530 the component thread, command pipe, data pipe and LCML Pipe
531 * @param pComponent  handle for this instance of the component
532 *
533 * @pre
534 *
535 * @post
536 *
537 * @return OMX_ERRORTYPE
538 */
539/* ================================================================================ */
540OMX_ERRORTYPE G722Enc_StartCompThread(OMX_HANDLETYPE pComponent)
541{
542    OMX_ERRORTYPE eError = OMX_ErrorNone;
543    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
544    G722ENC_COMPONENT_PRIVATE *pComponentPrivate =
545        (G722ENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
546#ifdef UNDER_CE
547    pthread_attr_t attr;
548    memset(&attr, 0, sizeof(attr));
549    attr.__inheritsched = PTHREAD_EXPLICIT_SCHED;
550    attr.__schedparam.__sched_priority = OMX_AUDIO_ENCODER_THREAD_PRIORITY;
551#endif
552
553    G722ENC_DPRINT ("%d :: Enetering  G722Enc_StartCompThread\n", __LINE__);
554    /* initialize values */
555    pComponentPrivate->bIsStopping = 0;
556    pComponentPrivate->lcml_nOpBuf = 0;
557    pComponentPrivate->lcml_nIpBuf = 0;
558    pComponentPrivate->app_nBuf = 0;
559    pComponentPrivate->bIsEOFSent = 0;
560    /* create the pipe used to send buffers to the thread */
561    eError = pipe (pComponentPrivate->dataPipe);
562    G722ENC_DPRINT ("%d :: G722Enc_StartCompThread() - error = %d\n", __LINE__,eError);
563    if (eError) {
564        eError = OMX_ErrorInsufficientResources;
565        goto EXIT;
566    }
567
568    /* create the pipe used to send commands to the thread */
569    eError = pipe (pComponentPrivate->cmdPipe);
570    G722ENC_DPRINT ("%d :: G722Enc_StartCompThread() - error = %d\n", __LINE__,eError);
571    if (eError) {
572        eError = OMX_ErrorInsufficientResources;
573        goto EXIT;
574    }
575
576    eError = pipe (pComponentPrivate->cmdDataPipe);
577    G722ENC_DPRINT ("%d :: G722Dec_StartCompThread() - error = %d\n", __LINE__,eError);
578    if (eError) {
579        eError = OMX_ErrorInsufficientResources;
580        goto EXIT;
581    }
582
583    /* create the pipe used to send commands to the thread */
584    eError = pipe (pComponentPrivate->lcml_Pipe);
585    G722ENC_DPRINT ("%d :: G722Enc_StartCompThread() - error = %d\n", __LINE__,eError);
586    if (eError) {
587        eError = OMX_ErrorInsufficientResources;
588        goto EXIT;
589    }
590    gLcmlPipeWr = pComponentPrivate->lcml_Pipe[1];
591
592    /* Create the Component Thread */
593#ifdef UNDER_CE
594    eError = pthread_create (&(pComponentPrivate->ComponentThread), &attr,
595                             ComponentThread, pComponentPrivate);
596#else
597    eError = pthread_create (&(pComponentPrivate->ComponentThread), NULL,
598                             ComponentThread, pComponentPrivate);
599#endif
600    G722ENC_DPRINT ("%d :: G722Enc_StartCompThread() - error = %d\n", __LINE__,eError);
601    if (eError || !pComponentPrivate->ComponentThread) {
602        eError = OMX_ErrorInsufficientResources;
603        goto EXIT;
604    }
605    G722ENC_DPRINT ("%d :: G722Enc_StartCompThread() - error = %d\n", __LINE__,eError);
606
607    pComponentPrivate_CC = pComponentPrivate;
608    pComponentPrivate->bCompThreadStarted = 1;
609    G722ENC_DPRINT ("%d :: G722Enc_StartCompThread() - error = %d\n", __LINE__,eError);
610
611 EXIT:
612    G722ENC_DPRINT ("%d :: G722Enc_StartCompThread() - error = %d\n", __LINE__,eError);
613    return eError;
614}
615
616
617/* ========================================================================== */
618/**
619 * G722Enc_FreeCompResources() This function is called by the component during
620 * de-init to close component thread, Command pipe, data pipe & LCML pipe.
621 *
622 * @param pComponent  handle for this instance of the component
623 *
624 * @pre
625 *
626 * @post
627 *
628 * @return none
629 */
630/* ========================================================================== */
631OMX_ERRORTYPE G722Enc_FreeCompResources(OMX_HANDLETYPE pComponent)
632{
633    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
634    G722ENC_COMPONENT_PRIVATE *pComponentPrivate = (G722ENC_COMPONENT_PRIVATE *)
635        pHandle->pComponentPrivate;
636
637    OMX_ERRORTYPE eError = OMX_ErrorNone;
638    OMX_ERRORTYPE err = OMX_ErrorNone;
639
640    G722ENC_DPRINT ("%d :: G722Enc_FreeCompResources()\n", __LINE__);
641    if (pComponentPrivate->pCreatePhaseArgs != NULL) {
642        G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pCreatePhaseArgs);
643        /*      free(pComponentPrivate->pCreatePhaseArgs);*/
644        pComponentPrivate->pCreatePhaseArgs = NULL;
645    }
646
647    if (pComponentPrivate->bCompThreadStarted) {
648        err = close (pComponentPrivate->dataPipe[0]);
649        if (0 != err && OMX_ErrorNone == eError) {
650            eError = OMX_ErrorHardware;
651            G722ENC_DPRINT ("%d :: Error while closing dataPipe\n",__LINE__);
652        }
653        err = close (pComponentPrivate->dataPipe[1]);
654        if (0 != err && OMX_ErrorNone == eError) {
655            eError = OMX_ErrorHardware;
656            G722ENC_DPRINT ("%d :: Error while closing dataPipe\n",__LINE__);
657        }
658
659        err = close (pComponentPrivate->cmdDataPipe[0]);
660        if (0 != err && OMX_ErrorNone == eError) {
661            eError = OMX_ErrorHardware;
662        }
663
664        err = close (pComponentPrivate->cmdDataPipe[1]);
665        if (0 != err && OMX_ErrorNone == eError) {
666            eError = OMX_ErrorHardware;
667        }
668
669        err = close (pComponentPrivate->cmdPipe[0]);
670        if (0 != err && OMX_ErrorNone == eError) {
671            eError = OMX_ErrorHardware;
672            G722ENC_DPRINT ("%d :: Error while closing cmdPipe\n",__LINE__);
673        }
674
675        err = close (pComponentPrivate->cmdPipe[1]);
676        if (0 != err && OMX_ErrorNone == eError) {
677            eError = OMX_ErrorHardware;
678            G722ENC_DPRINT ("%d :: Error while closing cmdPipe\n",__LINE__);
679        }
680
681        err = close (pComponentPrivate->lcml_Pipe[0]);
682        if (0 != err && OMX_ErrorNone == eError) {
683            eError = OMX_ErrorHardware;
684            G722ENC_DPRINT ("%d :: Error while closing lcml_Pipe\n",__LINE__);
685        }
686
687        err = close (pComponentPrivate->lcml_Pipe[1]);
688        if (0 != err && OMX_ErrorNone == eError) {
689            eError = OMX_ErrorHardware;
690            G722ENC_DPRINT ("%d :: Error while closing lcml_Pipe\n",__LINE__);
691        }
692    }
693
694    if (pComponentPrivate->bPortDefsAllocated != 0 /*NULL*/) {
695        if (pComponentPrivate->pPortDef[G722ENC_INPUT_PORT] != NULL) {
696            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]);
697            free(pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]);
698            pComponentPrivate->pPortDef[G722ENC_INPUT_PORT] = NULL;
699        }
700
701        if (pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]) {
702            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]);
703            free (pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]);
704            pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT] = NULL;
705        }
706
707        if (pComponentPrivate->pcmParams) {
708            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pcmParams);
709            free(pComponentPrivate->pcmParams);
710            pComponentPrivate->pcmParams = NULL;
711        }
712
713        if (pComponentPrivate->g722Params) {
714            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->g722Params);
715            free (pComponentPrivate->g722Params);
716            pComponentPrivate->g722Params = NULL;
717        }
718
719        if (pComponentPrivate->pOutputBufferList) {
720            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pOutputBufferList);
721            free (pComponentPrivate->pOutputBufferList);
722            pComponentPrivate->pOutputBufferList = NULL;
723        }
724
725        if (pComponentPrivate->pInputBufferList) {
726            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pInputBufferList);
727            free (pComponentPrivate->pInputBufferList);
728            pComponentPrivate->pInputBufferList = NULL;
729        }
730
731        if (pComponentPrivate->sDeviceString) {
732            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->sDeviceString);
733            free (pComponentPrivate->sDeviceString);
734            pComponentPrivate->sDeviceString = NULL;
735        }
736
737        if (pComponentPrivate->pOutPortFormat) {
738            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pOutPortFormat);
739            free (pComponentPrivate->pOutPortFormat);
740            pComponentPrivate->pOutPortFormat = NULL;
741        }
742
743        if (pComponentPrivate->pInPortFormat) {
744            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pInPortFormat);
745            free (pComponentPrivate->pInPortFormat);
746            pComponentPrivate->pInPortFormat = NULL;
747        }
748
749        if (pComponentPrivate->sPriorityMgmt) {
750            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->sPriorityMgmt);
751            free (pComponentPrivate->sPriorityMgmt);
752            pComponentPrivate->sPriorityMgmt = NULL;
753        }
754    }
755    pComponentPrivate->bPortDefsAllocated = 0;
756
757#ifndef UNDER_CE
758    pthread_mutex_destroy(&pComponentPrivate->InLoaded_mutex);
759    pthread_cond_destroy(&pComponentPrivate->InLoaded_threshold);
760
761    pthread_mutex_destroy(&pComponentPrivate->InIdle_mutex);
762    pthread_cond_destroy(&pComponentPrivate->InIdle_threshold);
763
764    pthread_mutex_destroy(&pComponentPrivate->AlloBuf_mutex);
765    pthread_cond_destroy(&pComponentPrivate->AlloBuf_threshold);
766#else
767    OMX_DestroyEvent(&(pComponentPrivate->InLoaded_event));
768    OMX_DestroyEvent(&(pComponentPrivate->InIdle_event));
769    OMX_DestroyEvent(&(pComponentPrivate->AlloBuf_event));
770#endif
771
772    /*eError = G722ENC_FreeLCMLHandle();*/
773    G722ENC_DPRINT ("Exiting Successfully G722ENC_FreeCompResources()\n");
774    return eError;
775}
776
777/* ================================================================================= */
778/**
779 * @fn G722ENC_CleanupInitParams() description for G722ENC_CleanupInitParams
780 G722ENC_CleanupInitParams().
781 This function is called by the component to cleanup initialization parameters
782 upon component exit.
783 * @param pComponent  handle for this instance of the component
784 *
785 * @pre
786 *
787 * @post
788 *
789 * @return OMX_ERRORTYPE
790 */
791/* ================================================================================ */
792OMX_ERRORTYPE G722ENC_CleanupInitParams(OMX_HANDLETYPE pComponent)
793{
794    OMX_U8* pParmsTemp =  NULL;
795    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
796    G722ENC_COMPONENT_PRIVATE *pComponentPrivate = (G722ENC_COMPONENT_PRIVATE *)
797        pHandle->pComponentPrivate;
798
799    OMX_ERRORTYPE eError = OMX_ErrorNone;
800    G722ENC_DPRINT ("%d :: G722ENC_CleanupInitParams()\n", __LINE__);
801    G722ENC_DPRINT ("%d :: pComponentPrivate = %p\n", __LINE__,pComponentPrivate);
802    G722ENC_DPRINT ("%d :: pComponentPrivate->strmAttr = %p\n", __LINE__,pComponentPrivate->strmAttr);
803
804    if ((pComponentPrivate->dasfmode == 1) && (pComponentPrivate->pParams)) {
805        pParmsTemp = (OMX_U8*)pComponentPrivate->pParams;
806        if (pParmsTemp != NULL){
807            pParmsTemp -= 128;
808        }
809        pComponentPrivate->pParams = (G722ENC_AudioCodecParams *)pParmsTemp;
810        G722ENC_MEMPRINT(":: Freeing: pComponentPrivate->pParams = %p\n",pComponentPrivate->pParams);
811        /*free(pComponentPrivate->pParams);
812          pComponentPrivate->pParams = NULL;*/
813        OMX_G722MEMFREE_STRUCT(pComponentPrivate->pParams);
814
815    }
816
817    if (pComponentPrivate->strmAttr) {
818        G722ENC_DPRINT ("%d :: G722ENC_CleanupInitParams()\n", __LINE__);
819        G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->strmAttr);
820        free(pComponentPrivate->strmAttr);
821        G722ENC_DPRINT ("%d :: G722ENC_CleanupInitParams()\n", __LINE__);
822        pComponentPrivate->strmAttr = NULL;
823    }
824    G722ENC_DPRINT ("%d :: G722ENC_CleanupInitParams()\n", __LINE__);
825    G722ENC_DPRINT ("Exiting Successfully G722ENC_CleanupInitParams()\n");
826    return eError;
827}
828
829
830/* ========================================================================== */
831/**
832 * @G722ENC_StopComponentThread() This function is called by the component during
833 * de-init to close component thread.
834 *
835 * @param pComponent  handle for this instance of the component
836 *
837 * @pre
838 *
839 * @post
840 *
841 * @return none
842 */
843/* ========================================================================== */
844
845OMX_ERRORTYPE G722ENC_StopComponentThread(OMX_HANDLETYPE pComponent)
846{
847    OMX_ERRORTYPE eError = OMX_ErrorNone;
848    OMX_ERRORTYPE threadError = OMX_ErrorNone;
849    int pthreadError = 0;
850    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
851    G722ENC_COMPONENT_PRIVATE *pComponentPrivate = (G722ENC_COMPONENT_PRIVATE *)
852        pHandle->pComponentPrivate;
853
854    pComponentPrivate->bIsStopping = 1;
855    pthreadError = pthread_join (pComponentPrivate->ComponentThread,
856                                 (void*)&threadError);
857
858
859    pComponentPrivate->bCompThreadStarted = 0;
860    if (0 != pthreadError)
861    {
862        eError = OMX_ErrorHardware;
863        G722ENC_DPRINT("OMX_ErrorHardware.\n");
864        goto EXIT;
865    }
866    if (OMX_ErrorNone != threadError && OMX_ErrorNone != eError)
867    {
868        eError = OMX_ErrorInsufficientResources;
869        G722ENC_DPRINT("OMX_ErrorInsufficientResources.\n");
870        goto EXIT;
871    }
872 EXIT:
873    G722ENC_DPRINT("Exiting StopComponentThread - Returning = 0x%x\n", eError);
874    return eError;
875}
876
877/* ========================================================================== */
878/**
879 * @G722ENC_HandleCommand() This function is called by the component when ever it
880 * receives the command from the application
881 *
882 * @param pComponentPrivate  Component private data
883 *
884 * @pre
885 *
886 * @post
887 *
888 * @return none
889 */
890/* ========================================================================== */
891
892OMX_U32 G722ENC_HandleCommand (G722ENC_COMPONENT_PRIVATE *pComponentPrivate)
893{
894    OMX_U16 i = 0;
895    OMX_ERRORTYPE eError = OMX_ErrorNone;
896    OMX_COMPONENTTYPE *pHandle =
897        (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
898    OMX_COMMANDTYPE command;
899    OMX_STATETYPE commandedState = OMX_StateInvalid;
900    OMX_U32 commandData = 0;
901    LCML_DSP_INTERFACE *pLcmlHandle = (LCML_DSP_INTERFACE *)
902        pComponentPrivate->pLcmlHandle;
903
904    G722ENC_DPRINT ("%d :: >>> Entering G722ENC_HandleCommand Function3\n",__LINE__);
905
906
907    read (pComponentPrivate->cmdPipe[0], &command, sizeof (command));
908    read (pComponentPrivate->cmdDataPipe[0], &commandData, sizeof (commandData));
909
910    if (command == OMX_CommandStateSet) {
911        commandedState = (OMX_STATETYPE)commandData;
912        switch(commandedState) {
913        case OMX_StateIdle:
914            eError = G722ENC_CommandToIdle(pComponentPrivate);
915            break;
916
917        case OMX_StateExecuting:
918            eError = G722ENC_CommandToExecuting(pComponentPrivate);
919            break;
920
921        case OMX_StateLoaded:
922            eError = G722ENC_CommandToLoaded(pComponentPrivate);
923            break;
924
925        case OMX_StatePause:
926            eError = G722ENC_CommandToPause(pComponentPrivate);
927            break;
928
929
930        case OMX_StateWaitForResources:
931            eError = G722ENC_CommandToWaitForResources(pComponentPrivate);
932            break;
933
934        case OMX_StateInvalid:
935            G722ENC_DPRINT("%d: G722ENC_HandleCommand: Cmd OMX_StateInvalid:\n",__LINE__);
936            if (pComponentPrivate->curState == commandedState){
937                pComponentPrivate->cbInfo.EventHandler (
938                                                        pHandle, pHandle->pApplicationPrivate,
939                                                        OMX_EventError, OMX_ErrorSameState,0,
940                                                        NULL);
941                G722ENC_DPRINT("%d :: Error: Same State Given by \
942                           Application\n",__LINE__);
943                break;
944            }
945            else {
946                pComponentPrivate->curState = OMX_StateInvalid;
947                pComponentPrivate->cbInfo.EventHandler(
948                                                       pHandle, pHandle->pApplicationPrivate,
949                                                       OMX_EventError, OMX_ErrorInvalidState,0, NULL);
950            }
951            break;
952
953        case OMX_StateMax:
954            G722ENC_DPRINT("%d: G722ENC_HandleCommand: Cmd OMX_StateMax::\n",__LINE__);
955            break;
956        } /* End of Switch */
957    }
958    else if (command == OMX_CommandMarkBuffer) {
959        G722ENC_DPRINT("command OMX_CommandMarkBuffer received %d\n",__LINE__);
960        if(!pComponentPrivate->pMarkBuf){
961            G722ENC_DPRINT("command OMX_CommandMarkBuffer received %d\n",__LINE__);
962            /* TODO Need to handle multiple marks */
963            pComponentPrivate->pMarkBuf = (OMX_MARKTYPE *)(commandData);
964        }
965    }
966    else if (command == OMX_CommandPortDisable) {
967        if (!pComponentPrivate->bDisableCommandPending) {
968            G722ENC_DPRINT("I'm here Line %d\n",__LINE__);
969            if(commandData == 0x0 || commandData == -1){
970                /* disable port */
971                pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bEnabled = OMX_FALSE;
972            }
973            if(commandData == 0x1 || commandData == -1){
974
975                char *pArgs = "damedesuStr";
976
977
978                pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bEnabled = OMX_FALSE;
979
980                if (pComponentPrivate->curState == OMX_StateExecuting) {
981                    pComponentPrivate->bNoIdleOnStop = OMX_TRUE;
982                    eError = LCML_ControlCodec(
983                                               ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
984                                               MMCodecControlStop,(void *)pArgs);
985                }
986
987
988            }
989
990        }
991        if(commandData == 0x0) {
992            if(!pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bPopulated){
993                /* return cmdcomplete event if input unpopulated */
994                pComponentPrivate->cbInfo.EventHandler(
995                                                       pHandle, pHandle->pApplicationPrivate,
996                                                       OMX_EventCmdComplete, OMX_CommandPortDisable,G722ENC_INPUT_PORT, NULL);
997                pComponentPrivate->bDisableCommandPending = 0;
998            }
999            else{
1000                pComponentPrivate->bDisableCommandPending = 1;
1001                pComponentPrivate->bDisableCommandParam = commandData;
1002            }
1003        }
1004
1005        if(commandData == 0x1) {
1006            if (!pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bPopulated){
1007                /* return cmdcomplete event if output unpopulated */
1008                pComponentPrivate->cbInfo.EventHandler(
1009                                                       pHandle, pHandle->pApplicationPrivate,
1010                                                       OMX_EventCmdComplete, OMX_CommandPortDisable,G722ENC_OUTPUT_PORT, NULL);
1011                pComponentPrivate->bDisableCommandPending = 0;
1012            }
1013            else {
1014                pComponentPrivate->bDisableCommandPending = 1;
1015                pComponentPrivate->bDisableCommandParam = commandData;
1016            }
1017        }
1018
1019        if(commandData == -1) {
1020            if (!pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bPopulated &&
1021                !pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bPopulated){
1022
1023                /* return cmdcomplete event if inout & output unpopulated */
1024                pComponentPrivate->cbInfo.EventHandler(
1025                                                       pHandle, pHandle->pApplicationPrivate,
1026                                                       OMX_EventCmdComplete, OMX_CommandPortDisable,G722ENC_INPUT_PORT, NULL);
1027
1028                pComponentPrivate->cbInfo.EventHandler(
1029                                                       pHandle, pHandle->pApplicationPrivate,
1030                                                       OMX_EventCmdComplete, OMX_CommandPortDisable,G722ENC_OUTPUT_PORT, NULL);
1031                pComponentPrivate->bDisableCommandPending = 0;
1032            }
1033            else {
1034                pComponentPrivate->bDisableCommandPending = 1;
1035                pComponentPrivate->bDisableCommandParam = commandData;
1036            }
1037
1038        }
1039
1040    }
1041    else if (command == OMX_CommandPortEnable) {
1042        if(commandData == 0x0 || commandData == -1){
1043            /* enable in port */
1044            G722ENC_DPRINT("setting input port to enabled\n");
1045            pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bEnabled = OMX_TRUE;
1046            G722ENC_DPRINT("WAKE UP!! HandleCommand: En utils setting output port to enabled. \n");
1047            if(pComponentPrivate->AlloBuf_waitingsignal)
1048            {
1049                pComponentPrivate->AlloBuf_waitingsignal = 0;
1050#ifndef UNDER_CE
1051                pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
1052                pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
1053                /* Sending signal to Allocate Buffer Task. */
1054                pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
1055#else
1056                OMX_SignalEvent(&(pComponentPrivate->AlloBuf_event));
1057#endif
1058            }
1059            G722ENC_DPRINT("pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bEnabled = %d\n",pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bEnabled);
1060        }
1061        if(commandData == 0x1 || commandData == -1){
1062            /* enable out port */
1063            if(pComponentPrivate->AlloBuf_waitingsignal)
1064            {
1065                pComponentPrivate->AlloBuf_waitingsignal = 0;
1066#ifndef UNDER_CE
1067                pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
1068                pthread_cond_signal(&pComponentPrivate->AlloBuf_threshold);
1069                /* Sending signal to Allocate Buffer Task. */
1070                pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
1071#else
1072                OMX_SignalEvent(&(pComponentPrivate->AlloBuf_event));
1073#endif
1074            }
1075
1076            if (pComponentPrivate->curState == OMX_StateExecuting) {
1077                char *pArgs = "damedesuStr";
1078                pComponentPrivate->bDspStoppedWhileExecuting = OMX_FALSE;
1079                eError = LCML_ControlCodec(
1080                                           ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1081                                           EMMCodecControlStart,(void *)pArgs);
1082            }
1083            G722ENC_DPRINT("setting output port to enabled\n");
1084            pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bEnabled = OMX_TRUE;
1085            G722ENC_DPRINT("pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bEnabled = %d\n",pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bEnabled);
1086        }
1087
1088        while (1) {
1089            G722ENC_DPRINT("commandData = %d\n",commandData);
1090            G722ENC_DPRINT("pComponentPrivate->curState = %d\n",pComponentPrivate->curState);
1091            G722ENC_DPRINT("pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bPopulated = %d\n",pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bPopulated);
1092            if(commandData == 0x0 && (pComponentPrivate->curState == OMX_StateLoaded || pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bPopulated)){
1093
1094                pComponentPrivate->cbInfo.EventHandler(
1095                                                       pHandle, pHandle->pApplicationPrivate,
1096                                                       OMX_EventCmdComplete, OMX_CommandPortEnable,G722ENC_INPUT_PORT, NULL);
1097                break;
1098            }
1099            else if(commandData == 0x1 && (pComponentPrivate->curState == OMX_StateLoaded || pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bPopulated)){
1100
1101                pComponentPrivate->cbInfo.EventHandler(
1102                                                       pHandle, pHandle->pApplicationPrivate,
1103                                                       OMX_EventCmdComplete, OMX_CommandPortEnable,G722ENC_OUTPUT_PORT, NULL);
1104                break;
1105            }
1106            else if(commandData == -1 && (pComponentPrivate->curState == OMX_StateLoaded || (pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bPopulated
1107                                                                                             && pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bPopulated))){
1108
1109                pComponentPrivate->cbInfo.EventHandler(
1110                                                       pHandle, pHandle->pApplicationPrivate,
1111                                                       OMX_EventCmdComplete, OMX_CommandPortEnable,G722ENC_INPUT_PORT, NULL);
1112
1113                pComponentPrivate->cbInfo.EventHandler(
1114                                                       pHandle, pHandle->pApplicationPrivate,
1115                                                       OMX_EventCmdComplete, OMX_CommandPortEnable,G722ENC_OUTPUT_PORT, NULL);
1116
1117                G722ENC_Fill_LCMLInitParamsEx(pHandle);
1118                break;
1119            }
1120        }
1121    }
1122    else if (command == OMX_CommandFlush) {
1123        if(commandData == 0x0 || commandData == -1){
1124            for (i=0; i < G722ENC_MAX_NUM_OF_BUFS; i++) {
1125                pComponentPrivate->pInputBufHdrPending[i] = NULL;
1126            }
1127            pComponentPrivate->nNumInputBufPending=0;
1128
1129            for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
1130                pComponentPrivate->cbInfo.EmptyBufferDone (
1131                                                           pComponentPrivate->pHandle,
1132                                                           pComponentPrivate->pHandle->pApplicationPrivate,
1133                                                           pComponentPrivate->pInputBufferList->pBufHdr[i]
1134                                                           );
1135
1136            }
1137
1138            /* return all input buffers */
1139            pComponentPrivate->cbInfo.EventHandler(
1140                                                   pHandle, pHandle->pApplicationPrivate,
1141                                                   OMX_EventCmdComplete, OMX_CommandFlush,G722ENC_INPUT_PORT, NULL);
1142        }
1143        if(commandData == 0x1 || commandData == -1){
1144            for (i=0; i < G722ENC_MAX_NUM_OF_BUFS; i++) {
1145                pComponentPrivate->pOutputBufHdrPending[i] = NULL;
1146            }
1147            pComponentPrivate->nNumOutputBufPending=0;
1148
1149            /* return all output buffers */
1150            for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
1151                G722ENC_DPRINT("[FillBufferDone] Call #%d Line %d\n",++FillBufferDoneCount,__LINE__);
1152                pComponentPrivate->cbInfo.FillBufferDone (
1153                                                          pComponentPrivate->pHandle,
1154                                                          pComponentPrivate->pHandle->pApplicationPrivate,
1155                                                          pComponentPrivate->pOutputBufferList->pBufHdr[i]
1156                                                          );
1157            }
1158
1159
1160            pComponentPrivate->cbInfo.EventHandler(
1161                                                   pHandle, pHandle->pApplicationPrivate,
1162                                                   OMX_EventCmdComplete, OMX_CommandFlush,G722ENC_OUTPUT_PORT, NULL);
1163        }
1164    }
1165    G722ENC_DPRINT ("%d :: Exiting NBAMRDECG722ENC_HandleCommand Function\n",__LINE__);
1166    G722ENC_DPRINT ("%d :: Returning %x\n",__LINE__,eError);
1167    return eError;
1168}
1169
1170
1171/* ========================================================================== */
1172/**
1173 * @G722ENC_HandleDataBuf_FromApp() This function is called by the component when ever it
1174 * receives the buffer from the application
1175 *
1176 * @param pComponentPrivate  Component private data
1177 * @param pBufHeader Buffer from the application
1178 *
1179 * @pre
1180 *
1181 * @post
1182 *
1183 * @return OMX_ERRORTYPE
1184 */
1185/* ========================================================================== */
1186
1187OMX_ERRORTYPE G722ENC_HandleDataBuf_FromApp(OMX_BUFFERHEADERTYPE* pBufHeader,
1188                                            G722ENC_COMPONENT_PRIVATE *pComponentPrivate)
1189{
1190    OMX_ERRORTYPE eError = OMX_ErrorNone;
1191    OMX_DIRTYPE eDir;
1192    LCML_DSP_INTERFACE *pLcmlHandle = NULL;
1193    G722ENC_LCML_BUFHEADERTYPE *pLcmlHdr = NULL;
1194
1195
1196    G722ENC_DPRINT ("%d :: Entering G722ENC_HandleDataBuf_FromApp Function\n",__LINE__);
1197    eError = G722ENC_GetBufferDirection(pBufHeader, &eDir, pComponentPrivate);
1198    if (eError != OMX_ErrorNone) {
1199        G722ENC_DPRINT ("%d :: The pBufHeader is not found in the list\n",__LINE__);
1200        goto EXIT;
1201    }
1202
1203    if (eDir == OMX_DirInput) {
1204        if(pComponentPrivate->dasfmode == 0) {
1205            if ((pBufHeader->nFilledLen > 0) || (pBufHeader->nFlags == OMX_BUFFERFLAG_EOS)) {
1206                pComponentPrivate->bBypassDSP = 0;
1207                pLcmlHandle = (LCML_DSP_INTERFACE *) pComponentPrivate->pLcmlHandle;
1208                G722ENC_DPRINT ("%d Comp:: Sending Filled Input buffer = %p, %p to LCML\n", __LINE__,pBufHeader,pBufHeader->pBuffer);
1209                eError = G722ENC_GetCorresponding_LCMLHeader(pBufHeader->pBuffer, OMX_DirInput, &pLcmlHdr);
1210                if (eError != OMX_ErrorNone) {
1211                    G722ENC_DPRINT("%d :: Error: Invalid Buffer Came ...\n",__LINE__);
1212                    goto EXIT;
1213                }
1214
1215                /* Store time stamp information */
1216                pComponentPrivate->arrTimestamp[pComponentPrivate->IpBufindex] = pBufHeader->nTimeStamp;
1217                /* Store nTickCount information */
1218                pComponentPrivate->arrTickCount[pComponentPrivate->IpBufindex] = pBufHeader->nTickCount;
1219                pComponentPrivate->IpBufindex++;
1220                pComponentPrivate->IpBufindex %= pComponentPrivate->pPortDef[OMX_DirOutput]->nBufferCountActual;
1221
1222
1223                if(pBufHeader->nFlags == OMX_BUFFERFLAG_EOS) {
1224                    pLcmlHdr->pIpParam->bLastBuffer = 1;
1225                }
1226                else{
1227                    pLcmlHdr->pIpParam->bLastBuffer = 0;
1228                }
1229
1230                if (pComponentPrivate->curState == OMX_StateExecuting) {
1231                    if(!pComponentPrivate->bDspStoppedWhileExecuting) {
1232                        if (!G722ENC_IsPending(pComponentPrivate,pBufHeader,OMX_DirInput)) {
1233                            G722ENC_SetPending(pComponentPrivate,pBufHeader,OMX_DirInput);
1234                            eError = LCML_QueueBuffer( pLcmlHandle->pCodecinterfacehandle,
1235                                                       EMMCodecInputBuffer,
1236                                                       pBufHeader->pBuffer,
1237                                                       pBufHeader->nAllocLen,
1238                                                       pBufHeader->nFilledLen,
1239                                                       (OMX_U8 *) pLcmlHdr->pIpParam,
1240                                                       sizeof(G722ENC_UAlgInBufParamStruct),
1241                                                       pBufHeader->pBuffer);
1242
1243                            if (eError != OMX_ErrorNone) {
1244                                G722ENC_DPRINT ("%d ::Comp: SetBuff: IP: Error Occurred\n",__LINE__);
1245                                eError = OMX_ErrorHardware;
1246                                goto EXIT;
1247                            }
1248                        }
1249                    }
1250                    else{
1251                        pComponentPrivate->cbInfo.EmptyBufferDone (
1252                                                                   pComponentPrivate->pHandle,
1253                                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1254                                                                   pBufHeader
1255                                                                   );
1256                    }
1257                    pComponentPrivate->lcml_nIpBuf++;
1258                    G722ENC_DPRINT ("Sending Input buffer to Codec\n");
1259                }
1260                else {
1261                    G722ENC_DPRINT("Queueing pending input buffers\n");
1262                    pComponentPrivate->pInputBufHdrPending[pComponentPrivate->nNumInputBufPending++] = pBufHeader;
1263                }
1264            }
1265            else {
1266                pComponentPrivate->bBypassDSP = 1;
1267                G722ENC_DPRINT("[FillBufferDone] Call #%d Line %d\n",++FillBufferDoneCount,__LINE__);
1268                pComponentPrivate->cbInfo.FillBufferDone (
1269                                                          pComponentPrivate->pHandle,
1270                                                          pComponentPrivate->pHandle->pApplicationPrivate,
1271                                                          pComponentPrivate->pOutputBufferList->pBufHdr[0]
1272                                                          );
1273
1274
1275
1276                G722ENC_DPRINT ("Forcing EmptyBufferDone\n");
1277                pComponentPrivate->cbInfo.EmptyBufferDone (
1278                                                           pComponentPrivate->pHandle,
1279                                                           pComponentPrivate->pHandle->pApplicationPrivate,
1280                                                           pComponentPrivate->pInputBufferList->pBufHdr[0]
1281                                                           );
1282
1283            }
1284            if(pBufHeader->nFlags == OMX_BUFFERFLAG_EOS) {
1285                G722ENC_DPRINT("%d :: Comp: EOF Flag Has Been Set Here\n",__LINE__);
1286                if(pComponentPrivate->dasfmode == 0) {
1287                    pComponentPrivate->pOutputBufferList->pBufHdr[0]->nFlags |= OMX_BUFFERFLAG_EOS;
1288                    pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1289                                                           pComponentPrivate->pHandle->pApplicationPrivate,
1290                                                           OMX_EventBufferFlag,
1291                                                           pComponentPrivate->pOutputBufferList->pBufHdr[0]->nOutputPortIndex,
1292                                                           pComponentPrivate->pOutputBufferList->pBufHdr[0]->nFlags, NULL);
1293                    G722ENC_DPRINT("[FillBufferDone] Call #%d Line %d\n",++FillBufferDoneCount,__LINE__);
1294                }
1295                /* pBufHeader->nFlags = 0; */
1296            }
1297            if(pBufHeader->pMarkData){
1298                /* copy mark to output buffer header */
1299                pComponentPrivate->pOutputBufferList->pBufHdr[0]->pMarkData = pBufHeader->pMarkData;
1300                pComponentPrivate->pOutputBufferList->pBufHdr[0]->hMarkTargetComponent = pBufHeader->hMarkTargetComponent;
1301
1302                /* trigger event handler if we are supposed to */
1303                if(pBufHeader->hMarkTargetComponent == pComponentPrivate->pHandle && pBufHeader->pMarkData){
1304                    pComponentPrivate_CC->cbInfo.EventHandler(pComponentPrivate->pHandle,
1305                                                              pComponentPrivate->pHandle->pApplicationPrivate,
1306                                                              OMX_EventMark,
1307                                                              0,
1308                                                              0,
1309                                                              pBufHeader->pMarkData);
1310                }
1311            }
1312        }
1313    }
1314    else if (eDir == OMX_DirOutput) {
1315        G722ENC_DPRINT ("%d Comp:: Sending Emptied Output buffer=%p to LCML\n",
1316                        __LINE__,pBufHeader);
1317        G722ENC_DPRINT("%d : pComponentPrivate->lcml_nOpBuf = %ld\n",__LINE__,
1318                       pComponentPrivate->lcml_nOpBuf);
1319        G722ENC_DPRINT("%d : pComponentPrivate->lcml_nIpBuf = %ld\n",__LINE__,
1320                       pComponentPrivate->lcml_nIpBuf);
1321        pLcmlHandle = (LCML_DSP_INTERFACE *) pComponentPrivate->pLcmlHandle;
1322
1323        eError = G722ENC_GetCorresponding_LCMLHeader(pBufHeader->pBuffer, OMX_DirOutput, &pLcmlHdr);
1324
1325        if (eError != OMX_ErrorNone) {
1326            G722ENC_DPRINT("%d :: Error: Invalid Buffer Came ...\n",__LINE__);
1327            goto EXIT;
1328        }
1329
1330        if (!(pComponentPrivate->bIsStopping)) {
1331            if (pComponentPrivate->bBypassDSP == 0) {
1332                if (pComponentPrivate->curState == OMX_StateExecuting) {
1333                    if (!G722ENC_IsPending(pComponentPrivate,pBufHeader,OMX_DirOutput)) {
1334                        G722ENC_SetPending(pComponentPrivate,pBufHeader,OMX_DirOutput);
1335                        eError = LCML_QueueBuffer(
1336                                                  pLcmlHandle->pCodecinterfacehandle,
1337                                                  EMMCodecOuputBuffer,
1338                                                  pBufHeader->pBuffer,
1339                                                  pBufHeader->nAllocLen,
1340                                                  0,
1341                                                  NULL,
1342                                                  0,
1343                                                  pBufHeader->pBuffer);
1344
1345
1346                        if (eError != OMX_ErrorNone ) {
1347                            G722ENC_DPRINT ("%d :: Comp:: SetBuff OP: Error Occurred\n",__LINE__);
1348                            eError = OMX_ErrorHardware;
1349                            goto EXIT;
1350                        }
1351                        pComponentPrivate->lcml_nOpBuf++;
1352                    }
1353                }
1354                else {
1355                    pComponentPrivate->pOutputBufHdrPending[pComponentPrivate->nNumOutputBufPending++] = pBufHeader;
1356                }
1357            }
1358        }
1359        else {
1360            if (pComponentPrivate->curState == OMX_StateExecuting) {
1361                if (!G722ENC_IsPending(pComponentPrivate,pBufHeader,OMX_DirOutput)) {
1362                    G722ENC_SetPending(pComponentPrivate,pBufHeader,OMX_DirOutput);
1363                    eError = LCML_QueueBuffer(
1364                                              pLcmlHandle->pCodecinterfacehandle,
1365                                              EMMCodecOuputBuffer,
1366                                              pBufHeader->pBuffer,
1367                                              pBufHeader->nAllocLen,
1368                                              0,
1369                                              NULL,
1370                                              0,
1371                                              pBufHeader->pBuffer);
1372
1373
1374                    if (eError != OMX_ErrorNone ) {
1375                        G722ENC_DPRINT ("%d :: Comp:: SetBuff: OP: Error Occurred\n",__LINE__);
1376                        eError = OMX_ErrorHardware;
1377                        goto EXIT;
1378                    }
1379
1380                    pComponentPrivate->lcml_nOpBuf++;
1381                }
1382            }
1383            else {
1384                G722ENC_DPRINT("Queueing output buffer\n");
1385                pComponentPrivate->pOutputBufHdrPending[pComponentPrivate->nNumOutputBufPending++] = pBufHeader;
1386            }
1387        }
1388    }
1389    else {
1390        G722ENC_DPRINT("%d : BufferHeader %p, Buffer %p Unknown ..........\n",
1391                       __LINE__,pBufHeader, pBufHeader->pBuffer);
1392        eError = OMX_ErrorBadParameter;
1393    }
1394 EXIT:
1395    G722ENC_DPRINT("%d : Exiting from  G722ENC_HandleDataBuf_FromApp \n",__LINE__);
1396    return eError;
1397}
1398
1399/*-------------------------------------------------------------------*/
1400/**
1401 * G722ENC_GetBufferDirection () This function is used by the component thread to
1402 * request a buffer from the application.  Since it was called from 2 places,
1403 * it made sense to turn this into a small function.
1404 *
1405 * @param pData pointer to G722 Encoder Context Structure
1406 * @param pCur pointer to the buffer to be requested to be filled
1407 *
1408 * @retval none
1409 **/
1410/*-------------------------------------------------------------------*/
1411
1412OMX_ERRORTYPE G722ENC_GetBufferDirection(OMX_BUFFERHEADERTYPE *pBufHeader,
1413                                         OMX_DIRTYPE *eDir,
1414                                         G722ENC_COMPONENT_PRIVATE *pComponentPrivate)
1415{
1416    OMX_ERRORTYPE eError = OMX_ErrorNone;
1417    OMX_BUFFERHEADERTYPE *pBuf = NULL;
1418    OMX_U16 flag = 1,i = 0;
1419    OMX_U32 nBuf = 0;
1420
1421
1422    nBuf = pComponentPrivate->pInputBufferList->numBuffers;
1423    for(i=0; i<nBuf; i++) {
1424        pBuf = pComponentPrivate->pInputBufferList->pBufHdr[i];
1425        if(pBufHeader == pBuf) {
1426            *eDir = OMX_DirInput;
1427            G722ENC_DPRINT ("%d :: Buffer %p is INPUT BUFFER\n",__LINE__, pBufHeader);
1428            flag = 0;
1429            goto EXIT;
1430        }
1431    }
1432    /*Search this buffer in input buffers list */
1433    nBuf = pComponentPrivate->pOutputBufferList->numBuffers;
1434    for(i=0; i<nBuf; i++) {
1435        pBuf = pComponentPrivate->pOutputBufferList->pBufHdr[i];
1436        if(pBufHeader == pBuf) {
1437            *eDir = OMX_DirOutput;
1438            G722ENC_DPRINT ("%d :: Buffer %p is OUTPUT BUFFER\n",__LINE__, pBufHeader);
1439            flag = 0;
1440            goto EXIT;
1441        }
1442    }
1443    if (flag == 1) {
1444        G722ENC_DPRINT ("%d :: Buffer %p is Not Found in the List\n",__LINE__,pBufHeader);
1445        eError = OMX_ErrorUndefined;
1446        goto EXIT;
1447    }
1448 EXIT:
1449    G722ENC_DPRINT ("%d :: Exiting G722ENC_GetBufferDirection Function\n",__LINE__);
1450    return eError;
1451}
1452
1453/* -------------------------------------------------------------------*/
1454/**
1455 *  Callback() function will be called LCML component to write the msg
1456 *
1457 * @param msgBuffer                 This buffer will be returned by the LCML
1458 *
1459 * @retval OMX_NoError              Success, ready to roll
1460 *         OMX_Error_BadParameter   The input parameter pointer is null
1461 **/
1462/*-------------------------------------------------------------------*/
1463
1464OMX_ERRORTYPE G722ENC_LCML_Callback (TUsnCodecEvent event,void * args [10])
1465{
1466    OMX_ERRORTYPE eError = OMX_ErrorNone;
1467    OMX_U8 *pBuffer = args[1];
1468    G722ENC_LCML_BUFHEADERTYPE *pLcmlHdr = NULL;
1469    OMX_COMPONENTTYPE *pHandle = pComponentPrivate_CC->pHandle;
1470
1471    LCML_DSP_INTERFACE *pLcmlHandle = NULL;
1472
1473    switch(event) {
1474
1475    case EMMCodecDspError:
1476        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecDspError\n");
1477        break;
1478
1479    case EMMCodecInternalError:
1480        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecInternalError\n");
1481        break;
1482
1483    case EMMCodecInitError:
1484        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecInitError\n");
1485        break;
1486
1487    case EMMCodecDspMessageRecieved:
1488        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecDspMessageRecieved\n");
1489        break;
1490
1491    case EMMCodecBufferProcessed:
1492        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecBufferProcessed\n");
1493        break;
1494
1495    case EMMCodecProcessingStarted:
1496        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecProcessingStarted\n");
1497        break;
1498
1499    case EMMCodecProcessingPaused:
1500        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecProcessingPaused\n");
1501        break;
1502
1503    case EMMCodecProcessingStoped:
1504        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecProcessingStoped\n");
1505        break;
1506
1507    case EMMCodecProcessingEof:
1508        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecProcessingEof\n");
1509        break;
1510
1511    case EMMCodecBufferNotProcessed:
1512        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecBufferNotProcessed\n");
1513        break;
1514
1515    case EMMCodecAlgCtrlAck:
1516        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecAlgCtrlAck\n");
1517        break;
1518
1519    case EMMCodecStrmCtrlAck:
1520        G722ENC_DPRINT("[LCML CALLBACK EVENT]  EMMCodecStrmCtrlAck\n");
1521        break;
1522
1523    default:
1524        G722ENC_DPRINT("[LCML CALLBACK EVENT]  Got event = %d\n",event);
1525        break;
1526    }
1527
1528
1529    G722ENC_DPRINT(" %d EMMCodecInputBuffer = %p \n",__LINE__,(void *)EMMCodecInputBuffer);
1530    G722ENC_DPRINT(" %d EMMCodecOuputBuffer = %p \n",__LINE__,(void *)EMMCodecOuputBuffer);
1531    G722ENC_DPRINT ("%d :: Entering the G722ENC_LCML_Callback() : event = %d\n",__LINE__,event);
1532    if(event == EMMCodecBufferProcessed) {
1533        if( args[0] == (void *)EMMCodecInputBuffer) {
1534            G722ENC_DPRINT (" :: Inside the G722ENC_LCML_Callback EMMCodecInputBuffer\n");
1535            G722ENC_DPRINT("%d :: Input: pBufferr = %p\n",__LINE__, pBuffer);
1536            eError = G722ENC_GetCorresponding_LCMLHeader(pBuffer, OMX_DirInput, &pLcmlHdr);
1537            G722ENC_ClearPending(pComponentPrivate_CC,pLcmlHdr->pBufHdr,OMX_DirInput);
1538
1539            if (eError != OMX_ErrorNone) {
1540                G722ENC_DPRINT("%d :: Error: Invalid Buffer Came ...\n",__LINE__);
1541                goto EXIT;
1542            }
1543            G722ENC_DPRINT("%d :: Input: pLcmlHeader = %p\n",__LINE__, pLcmlHdr);
1544            /* >>>>>>>>>>>>>>>>>>>>>>>>>>> */
1545            if(pComponentPrivate_CC->dasfmode == 0) {
1546                pComponentPrivate_CC->lcml_nIpBuf--;
1547                G722ENC_DPRINT("%d: Component Sending Empty Input buffer%p to App\n",__LINE__, pLcmlHdr->pBufHdr->pBuffer);
1548                pComponentPrivate_CC->cbInfo.EmptyBufferDone (
1549                                                              pHandle,
1550                                                              pHandle->pApplicationPrivate,
1551                                                              pLcmlHdr->pBufHdr
1552                                                              );
1553                pComponentPrivate_CC->app_nBuf++;
1554
1555            }
1556            /* <<<<<<<<<<<<<<<<<<<<<<<<<<< */
1557        }
1558        else if (args[0] == (void *)EMMCodecOuputBuffer) {
1559            if (!G722ENC_IsValid(pComponentPrivate_CC,pBuffer,OMX_DirOutput)) {
1560                /* If the buffer we get back from the DSP is not valid call FillBufferDone
1561                   on a valid buffer */
1562                pComponentPrivate_CC->cbInfo.FillBufferDone (
1563                                                             pComponentPrivate_CC->pHandle,
1564                                                             pComponentPrivate_CC->pHandle->pApplicationPrivate,
1565                                                             pComponentPrivate_CC->pOutputBufferList->pBufHdr[pComponentPrivate_CC->nInvalidFrameCount++]
1566                                                             );
1567            }
1568            else {
1569
1570                pComponentPrivate_CC->nOutStandingFillDones++;
1571                G722ENC_DPRINT("Incrementing pComponentPrivate->nOutStandingFillDones Line %d\n",__LINE__);
1572                G722ENC_DPRINT("%d::pComponentPrivate_CC->nOutStandingFillDones = %d\n",__LINE__,pComponentPrivate_CC->nOutStandingFillDones);
1573                eError = G722ENC_GetCorresponding_LCMLHeader(pBuffer, OMX_DirOutput, &pLcmlHdr);
1574                G722ENC_ClearPending(pComponentPrivate_CC,pLcmlHdr->pBufHdr,OMX_DirOutput);
1575
1576                if (eError != OMX_ErrorNone) {
1577                    G722ENC_DPRINT("%d :: Error: Invalid Buffer Came ...\n",__LINE__);
1578                    goto EXIT;
1579                }
1580                pLcmlHdr->pBufHdr->nFilledLen = (int)args[8];
1581                G722ENC_DPRINT("%d :: Output: pLcmlHeader = %p\n",__LINE__, pLcmlHdr);
1582
1583                pComponentPrivate_CC->lcml_nCntOpReceived++;
1584                /* >>>>>>>>>>>>>>>>>>>>>>> */
1585                pComponentPrivate_CC->lcml_nOpBuf--;
1586                G722ENC_DPRINT("%d: Component Sending Filled Output buffer%p to App\n",__LINE__,pLcmlHdr->pBufHdr->pBuffer);
1587                G722ENC_DPRINT("[FillBufferDone] Call #%d Line %d\n",++FillBufferDoneCount,__LINE__);
1588                pComponentPrivate_CC->cbInfo.FillBufferDone (
1589                                                             pHandle,
1590                                                             pHandle->pApplicationPrivate,
1591                                                             pLcmlHdr->pBufHdr
1592                                                             );
1593                pComponentPrivate_CC->app_nBuf++;
1594                pComponentPrivate_CC->nOutStandingFillDones--;
1595                G722ENC_DPRINT("Decrementing pComponentPrivate->nOutStandingFillDones Line %d\n",__LINE__);
1596                G722ENC_DPRINT("%d::pComponentPrivate->nOutStandingFillDones = %d\n",__LINE__,pComponentPrivate_CC->nOutStandingFillDones);
1597                /* <<<<<<<<<<<<<<<<<<<<<<< */
1598            }
1599        }
1600    }
1601    else if(event == EMMCodecProcessingStoped) {
1602        if (!pComponentPrivate_CC->bNoIdleOnStop) {
1603            pComponentPrivate_CC->bIdleCommandPending = OMX_TRUE;
1604            pComponentPrivate_CC->curState = OMX_StateIdle;
1605
1606            if (pComponentPrivate_CC->bPreempted == 0) {
1607                pComponentPrivate_CC->cbInfo.EventHandler(pHandle,
1608                                                          pHandle->pApplicationPrivate,
1609                                                          OMX_EventCmdComplete,
1610                                                          OMX_CommandStateSet,
1611                                                          pComponentPrivate_CC->curState,
1612                                                          NULL);
1613            }
1614            else {
1615                pComponentPrivate_CC->cbInfo.EventHandler(pHandle,
1616                                                          pHandle->pApplicationPrivate,
1617                                                          OMX_EventError,
1618                                                          OMX_ErrorResourcesPreempted,
1619                                                          0,
1620                                                          NULL);
1621            }
1622            pComponentPrivate_CC->bIdleCommandPending = OMX_FALSE;
1623        }
1624        else {
1625            G722ENC_DPRINT("setting bDspStoppedWhileExecuting\n");
1626            pComponentPrivate_CC->bDspStoppedWhileExecuting = OMX_TRUE;
1627        }
1628        pComponentPrivate_CC->bNoIdleOnStop= OMX_FALSE;
1629    }
1630    else if(event == EMMCodecAlgCtrlAck) {
1631        G722ENC_DPRINT ("GOT MESSAGE USN_DSPACK_ALGCTRL \n");
1632    }
1633    else if (event == EMMCodecDspError) {
1634        if(((int)args[4] == 1) && ((int)args[5] == 0x500)) {
1635            G722ENC_DPRINT ("%d :: Comp: Inside the G722ENC_LCML_Callback: USN_DSPMSG_ERROR \n", __LINE__);
1636            pLcmlHandle = (LCML_DSP_INTERFACE *)pComponentPrivate_CC->pLcmlHandle;
1637            G722ENC_DPRINT("Setting current state Idle Line %d\n",__LINE__);
1638            pComponentPrivate_CC->curState = OMX_StateIdle;
1639            pComponentPrivate_CC->cbInfo.EventHandler(pHandle, pHandle->pApplicationPrivate,
1640                                                      OMX_EventCmdComplete, OMX_CommandStateSet,
1641                                                      pComponentPrivate_CC->curState, NULL);
1642            G722ENC_DPRINT("%d :: G722ENC: State has been Set to Idle\n", __LINE__);
1643        }
1644        if(((int)args[4] == 1) && ((int)args[5] == 0x0300)) {
1645            G722ENC_DPRINT ("%d :: Comp: Inside the G722ENC_LCML_Callback: USN_DSPMSG_ERROR \n", __LINE__);
1646            pComponentPrivate_CC->cbInfo.EventHandler(pHandle, pHandle->pApplicationPrivate,
1647                                                      OMX_EventError, OMX_ErrorOverflow,
1648                                                      pComponentPrivate_CC->curState, NULL);
1649
1650
1651        }
1652        if(((int)args[4] == USN_ERR_WARNING) &&
1653           ((int)args[5] == IUALG_WARN_PLAYCOMPLETED)) {
1654            pComponentPrivate_CC->cbInfo.EventHandler(pHandle,
1655                                                      pHandle->pApplicationPrivate,
1656                                                      OMX_EventBufferFlag,
1657                                                      (OMX_U32)NULL,
1658                                                      OMX_BUFFERFLAG_EOS,
1659                                                      NULL);
1660        }
1661    }
1662    else if (event == EMMCodecStrmCtrlAck) {
1663        G722ENC_DPRINT("%d :: GOT MESSAGE USN_DSPACK_STRMCTRL ----\n",__LINE__);
1664        pComponentPrivate_CC->bStreamCtrlCalled = 1;
1665
1666    }
1667    else if (event == EMMCodecProcessingPaused) {
1668        pComponentPrivate_CC->curState = OMX_StatePause;
1669        pComponentPrivate_CC->cbInfo.EventHandler(pHandle, pHandle->pApplicationPrivate,
1670                                                  OMX_EventCmdComplete, OMX_CommandStateSet,
1671                                                  pComponentPrivate_CC->curState, NULL);
1672
1673    }
1674 EXIT:
1675    G722ENC_DPRINT ("%d :: Exiting the G722ENC_LCML_Callback() \n",__LINE__);
1676    return eError;
1677}
1678/* -------------------------------------------------------------------*/
1679/**
1680 *  G722ENC_GetCorresponding_LCMLHeader() function will be called by G722ENC_LCML_Callback
1681 *                                 component to write the msg
1682 * @param *pBuffer,          Event which gives to details about USN status
1683 * @param LCML_NBAMRENC_BUFHEADERTYPE **ppLcmlHdr
1684
1685 * @retval OMX_NoError              Success, ready to roll
1686 *         OMX_Error_BadParameter   The input parameter pointer is null
1687 **/
1688/* -------------------------------------------------------------------*/
1689
1690OMX_ERRORTYPE G722ENC_GetCorresponding_LCMLHeader(OMX_U8 *pBuffer,
1691                                                  OMX_DIRTYPE eDir,
1692                                                  G722ENC_LCML_BUFHEADERTYPE **ppLcmlHdr)
1693{
1694    OMX_ERRORTYPE eError = OMX_ErrorNone;
1695    G722ENC_LCML_BUFHEADERTYPE *pLcmlBufHeader = NULL;
1696    int nIpBuf = pComponentPrivate_CC->pInputBufferList->numBuffers;
1697    int nOpBuf = pComponentPrivate_CC->pOutputBufferList->numBuffers;
1698    OMX_U16 i = 0;
1699
1700    G722ENC_DPRINT ("%d :: Entering the G722ENC_GetCorresponding_LCMLHeader()\n",__LINE__);
1701    if(eDir == OMX_DirInput) {
1702        pLcmlBufHeader = pComponentPrivate_CC->pLcmlBufHeader[G722ENC_INPUT_PORT];
1703        for(i=0; i<nIpBuf; i++) {
1704            if(pBuffer == pLcmlBufHeader->pBufHdr->pBuffer) {
1705                *ppLcmlHdr = pLcmlBufHeader;
1706                G722ENC_DPRINT("%d::Corresponding LCML Header Found\n",__LINE__);
1707                goto EXIT;
1708            }
1709            pLcmlBufHeader++;
1710        }
1711    }
1712    else if (eDir == OMX_DirOutput) {
1713        pLcmlBufHeader = pComponentPrivate_CC->pLcmlBufHeader[G722ENC_OUTPUT_PORT];
1714        for(i=0; i<nOpBuf; i++) {
1715            if(pBuffer == pLcmlBufHeader->pBufHdr->pBuffer) {
1716                *ppLcmlHdr = pLcmlBufHeader;
1717                G722ENC_DPRINT("%d::Corresponding LCML Header Found\n",__LINE__);
1718                goto EXIT;
1719            }
1720            pLcmlBufHeader++;
1721        }
1722    }
1723    else {
1724        G722ENC_DPRINT("%d:: Invalid Buffer Type :: exiting...\n",__LINE__);
1725    }
1726
1727 EXIT:
1728    G722ENC_DPRINT ("%d :: Exiting the G722ENC_GetCorresponding_LCMLHeader() \n",__LINE__);
1729    return eError;
1730}
1731
1732
1733/* -------------------------------------------------------------------*/
1734/**
1735 *  G722ENC_GetLCMLHandle()
1736 *
1737 * @retval OMX_HANDLETYPE
1738 *
1739 -------------------------------------------------------------------*/
1740
1741#ifndef UNDER_CE
1742
1743OMX_HANDLETYPE G722ENC_GetLCMLHandle()
1744{
1745    void *handle = NULL;
1746    OMX_ERRORTYPE (*fpGetHandle)(OMX_HANDLETYPE);
1747    OMX_HANDLETYPE pHandle = NULL;
1748    char *error = NULL;
1749    OMX_ERRORTYPE eError = OMX_ErrorNone;
1750    handle = dlopen("libLCML.so", RTLD_LAZY);
1751    if (!handle) {
1752        fputs(dlerror(), stderr);
1753        goto EXIT;
1754    }
1755
1756    fpGetHandle = dlsym (handle, "GetHandle");
1757    if ((error = dlerror()) != NULL) {
1758        fputs(error, stderr);
1759        goto EXIT;
1760    }
1761    eError = (*fpGetHandle)(&pHandle);
1762    if(eError != OMX_ErrorNone) {
1763        eError = OMX_ErrorUndefined;
1764        G722ENC_DPRINT("eError != OMX_ErrorNone...\n");
1765        pHandle = NULL;
1766        goto EXIT;
1767    }
1768
1769    pComponentPrivate_CC->lcml_handle = handle;
1770    pComponentPrivate_CC->bLcmlHandleOpened = 1;
1771 EXIT:
1772    return pHandle;
1773}
1774#else
1775
1776//WINDOWS Explicit dll load procedure
1777
1778OMX_HANDLETYPE G722ENC_GetLCMLHandle()
1779{
1780    typedef OMX_ERRORTYPE (*LPFNDLLFUNC1)(OMX_HANDLETYPE);
1781    OMX_HANDLETYPE pHandle = NULL;
1782    OMX_ERRORTYPE eError = OMX_ErrorNone;
1783    HINSTANCE hDLL;               // Handle to DLL
1784    LPFNDLLFUNC1 fpGetHandle1;
1785
1786
1787
1788    hDLL = LoadLibraryEx(TEXT("OAF_BML.dll"), NULL,0);
1789    if (hDLL == NULL)
1790    {
1791        //fputs(dlerror(), stderr);
1792        G722ENC_DPRINT("BML Load Failed!!!\n");
1793        return pHandle;
1794    }
1795
1796    fpGetHandle1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,TEXT("GetHandle"));
1797    if (!fpGetHandle1)
1798    {
1799        // handle the error
1800        FreeLibrary(hDLL);
1801        return pHandle;
1802    }
1803
1804    // call the function
1805    eError = fpGetHandle1(&pHandle);
1806    if(eError != OMX_ErrorNone) {
1807        eError = OMX_ErrorUndefined;
1808        AMRDEC_DPRINT("eError != OMX_ErrorNone...\n");
1809        pHandle = NULL;
1810        return pHandle;
1811    }
1812    return pHandle;
1813}
1814#endif
1815
1816
1817#ifndef UNDER_CE
1818OMX_ERRORTYPE G722ENC_FreeLCMLHandle()
1819{
1820
1821    int retValue = 0;
1822    OMX_ERRORTYPE eError = OMX_ErrorNone;
1823    if (pComponentPrivate_CC->bLcmlHandleOpened) {
1824        retValue = dlclose(pComponentPrivate_CC->lcml_handle);
1825
1826        if (retValue != 0) {
1827            eError = OMX_ErrorUndefined;
1828        }
1829        pComponentPrivate_CC->bLcmlHandleOpened = 0;
1830    }
1831    return eError;
1832}
1833#else
1834OMX_ERRORTYPE G722ENC_FreeLCMLHandle()
1835{
1836
1837    int retValue = 0;
1838    OMX_ERRORTYPE eError = OMX_ErrorNone;
1839
1840    if (pComponentPrivate_CC->bLcmlHandleOpened) {
1841
1842        retValue = FreeLibrary(pComponentPrivate_CC->pLcmlHandle);
1843        if (retValue == 0) {          /* Zero Indicates failure */
1844            eError = OMX_ErrorUndefined;
1845        }
1846        pComponentPrivate_CC->bLcmlHandleOpened = 0;
1847    }
1848    return eError;
1849}
1850#endif
1851
1852
1853
1854/* ================================================================================= */
1855/**
1856 * @fn G722ENC_CommandToIdle() description for G722ENC_CommandToIdle
1857 G722ENC_CommandToIdle().
1858 This component is called by HandleCommand() when the component is commanded to Idle
1859 * @param pComponent  handle for this instance of the component
1860 *
1861 * @pre
1862 *
1863 * @post
1864 *
1865 * @return OMX_ERRORTYPE
1866 */
1867/* ================================================================================ */
1868OMX_ERRORTYPE G722ENC_CommandToIdle(G722ENC_COMPONENT_PRIVATE *pComponentPrivate)
1869{
1870
1871    OMX_ERRORTYPE eError = OMX_ErrorNone;
1872    char *pArgs = "damedesuStr";
1873    OMX_COMPONENTTYPE *pHandle =
1874        (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
1875    LCML_CALLBACKTYPE cb;
1876    LCML_DSP *pLcmlDsp =  NULL;
1877    char *p = "damedesuStr";
1878    OMX_HANDLETYPE pLcmlHandle = pComponentPrivate->pLcmlHandle;
1879
1880
1881#ifdef G722ENC_DEBUG
1882    LCML_CODEC_INTERFACE *pp = NULL;
1883#endif
1884
1885
1886
1887    G722ENC_DPRINT("%d: G722ENC_HandleCommand: Cmd Idle \n",__LINE__);
1888    if (pComponentPrivate->curState == OMX_StateIdle){
1889        pComponentPrivate->cbInfo.EventHandler (
1890                                                pHandle, pHandle->pApplicationPrivate,
1891                                                OMX_EventError, OMX_ErrorSameState,0,
1892                                                NULL);
1893        G722ENC_DPRINT("%d :: Error: Same State Given by Application\n",__LINE__);
1894    }
1895    else if (pComponentPrivate->curState == OMX_StateLoaded ||
1896             pComponentPrivate->curState == OMX_StateWaitForResources) {
1897        if (pComponentPrivate->dasfmode == 1)
1898        {
1899            if(pComponentPrivate->streamID == 0)
1900            {
1901                eError = OMX_ErrorInsufficientResources;
1902                G722ENC_DPRINT("OMX_ErrorInsufficientResources.\n");
1903                pComponentPrivate->curState = OMX_StateInvalid;
1904                pComponentPrivate->cbInfo.EventHandler(
1905                                                       pHandle, pHandle->pApplicationPrivate,
1906                                                       OMX_EventError, OMX_ErrorInvalidState,0, NULL);
1907                goto EXIT;
1908            }
1909        }
1910        if (pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->bEnabled == OMX_TRUE
1911            && pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bEnabled == OMX_TRUE)
1912        {
1913            if (!(pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bPopulated)
1914                && !(pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->bPopulated))
1915            {
1916                pComponentPrivate->InLoaded_readytoidle = 1;
1917#ifndef UNDER_CE
1918                pthread_mutex_lock(&pComponentPrivate->InLoaded_mutex);
1919                pthread_cond_wait(&pComponentPrivate->InLoaded_threshold,
1920                                  &pComponentPrivate->InLoaded_mutex);
1921                pthread_mutex_unlock(&pComponentPrivate->InLoaded_mutex);
1922#else
1923                OMX_WaitForEvent(&(pComponentPrivate->InLoaded_event));
1924#endif
1925            }
1926        }
1927
1928        pLcmlHandle = (OMX_HANDLETYPE) G722ENC_GetLCMLHandle();
1929
1930        if (pLcmlHandle == NULL)
1931        {
1932            G722ENC_DPRINT("LCML Handle is NULL........exiting..\n");
1933            goto EXIT;
1934        }
1935
1936        /* Got handle of dsp via phandle filling information about DSP Specific things */
1937        pLcmlDsp = (((LCML_DSP_INTERFACE*)pLcmlHandle)->dspCodec);
1938
1939        eError = G722ENC_Fill_LCMLInitParams(pHandle, pLcmlDsp);
1940        if(eError != OMX_ErrorNone)
1941        {
1942            G722ENC_DPRINT("from G722ENCFill_LCMLInitParams().\n");
1943            goto EXIT;
1944        }
1945
1946        pComponentPrivate->pLcmlHandle = (LCML_DSP_INTERFACE *)pLcmlHandle;
1947        cb.LCML_Callback = (void *) G722ENC_LCML_Callback;
1948
1949#ifndef UNDER_CE
1950        eError = LCML_InitMMCodecEx(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1951                                    p, &pLcmlHandle, (void *)p,
1952                                    &cb, (OMX_STRING)pComponentPrivate->sDeviceString);
1953
1954#else
1955        eError = LCML_InitMMCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1956                                  p, &pLcmlHandle, (void *)p,
1957                                  &cb);
1958#endif
1959        if(eError != OMX_ErrorNone)
1960        {
1961            G722ENC_DPRINT("from LCML_Init().\n");
1962            goto EXIT;
1963        }
1964
1965#ifdef HASHINGENABLE
1966        /* Enable the Hashing Code */
1967        eError = LCML_SetHashingState(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle, OMX_TRUE);
1968        if (eError != OMX_ErrorNone) {
1969            G722ENC_DPRINT("Failed to set Mapping State\n");
1970            goto EXIT;
1971        }
1972#endif
1973        pComponentPrivate->curState = OMX_StateIdle;
1974
1975#ifdef __PERF_INSTRUMENTATION__
1976        PERF_Boundary(pComponentPrivate->pPERFcomp,
1977                      PERF_BoundaryComplete | PERF_BoundarySetup);
1978#endif
1979        pComponentPrivate->cbInfo.EventHandler(pHandle,
1980                                               pHandle->pApplicationPrivate,
1981                                               OMX_EventCmdComplete,
1982                                               OMX_CommandStateSet,
1983                                               pComponentPrivate->curState,
1984                                               NULL);
1985
1986
1987    }
1988    else if (pComponentPrivate->curState == OMX_StateExecuting) {
1989#ifdef HASHINGENABLE
1990        /*Hashing Change*/
1991        pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLcmlHandle;
1992        /* clear out any mappings that might have accumulated */
1993        eError = LCML_FlushHashes(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle);
1994        if (eError != OMX_ErrorNone) {
1995            prinft("Error occurred in Codec mapping flush!\n");
1996            goto EXIT;
1997        }
1998#endif
1999
2000        G722ENC_DPRINT("%d :: In G722ENC_HandleCommand: Stopping the codec\n",__LINE__);
2001        G722ENC_DPRINT("%d: G722ENCUTILS::About to call LCML_ControlCodec\n",__LINE__);
2002        G722ENC_DPRINT("%d: Calling Codec Stop..\n");
2003        eError = LCML_ControlCodec(
2004                                   ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2005                                   MMCodecControlStop,(void *)pArgs);
2006        G722ENC_DPRINT("Called Codec Stop..\n");
2007        if(eError != OMX_ErrorNone) {
2008            G722ENC_DPRINT("%d: Error Occurred in Codec Stop..\n",__LINE__);
2009            goto EXIT;
2010        }
2011
2012        G722ENC_DPRINT ("%d :: The component is stopped\n",__LINE__);
2013        /*      pComponentPrivate->bIdleCommandPending = 1;*/
2014    }
2015    else if(pComponentPrivate->curState == OMX_StatePause) {
2016#ifdef HASHINGENABLE
2017        /*Hashing Change*/
2018        pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLcmlHandle;
2019        /* clear out any mappings that might have accumulated */
2020        eError = LCML_FlushHashes(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle);
2021        if (eError != OMX_ErrorNone) {
2022            G722ENC_DPRINT("Error occurred in Codec mapping flush!\n");
2023            goto EXIT;
2024        }
2025#endif
2026
2027        G722ENC_DPRINT("%d :: Comp: Stop Command Received\n",__LINE__);
2028        G722ENC_DPRINT("%d: G722ENCUTILS::About to call LCML_ControlCodec\n",__LINE__);
2029        pComponentPrivate->curState = OMX_StateIdle;
2030        G722ENC_DPRINT ("%d :: The component is stopped\n",__LINE__);
2031        pComponentPrivate->cbInfo.EventHandler(pHandle, pHandle->pApplicationPrivate,
2032                                               OMX_EventCmdComplete, OMX_CommandStateSet, pComponentPrivate->curState, NULL);
2033
2034    }
2035    else {
2036        G722ENC_DPRINT("%d: Comp: Sending ErrorNotification: Invalid State\n",__LINE__);
2037        pComponentPrivate->cbInfo.EventHandler(
2038                                               pHandle,
2039                                               pHandle->pApplicationPrivate,
2040                                               OMX_EventError,
2041                                               OMX_ErrorIncorrectStateTransition, 0,
2042                                               "Invalid State Error");
2043    }
2044 EXIT:
2045    return eError;
2046}
2047
2048/* ================================================================================= */
2049/**
2050 * @fn G722ENC_CommandToLoaded() description for G722ENC_CommandToLoaded
2051 G722ENC_CommandToLoaded().
2052 This component is called by HandleCommand() when the component is commanded to Loaded
2053 * @param pComponent  handle for this instance of the component
2054 *
2055 * @pre
2056 *
2057 * @post
2058 *
2059 * @return OMX_ERRORTYPE
2060 */
2061/* ================================================================================ */
2062OMX_ERRORTYPE G722ENC_CommandToLoaded(G722ENC_COMPONENT_PRIVATE *pComponentPrivate)
2063{
2064    OMX_ERRORTYPE eError = OMX_ErrorNone;
2065    char *pArgs = "damedesuStr";
2066    OMX_COMPONENTTYPE *pHandle =
2067        (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
2068    OMX_HANDLETYPE pLcmlHandle = pComponentPrivate->pLcmlHandle;
2069    G722ENC_LCML_BUFHEADERTYPE *pTemp_lcml = NULL;
2070    OMX_U16 i = 0;
2071
2072
2073    G722ENC_DPRINT("%d: G722ENC_HandleCommand: Cmd Loaded\n",__LINE__);
2074    if (pComponentPrivate->curState == OMX_StateLoaded){
2075        pComponentPrivate->cbInfo.EventHandler (
2076                                                pHandle, pHandle->pApplicationPrivate,
2077                                                OMX_EventError, OMX_ErrorSameState,0,
2078                                                NULL);
2079        G722ENC_DPRINT("%d :: Error: Same State Given by Application\n",__LINE__);
2080        goto EXIT;
2081    }
2082    if (pComponentPrivate->curState == OMX_StateWaitForResources ) {
2083        pComponentPrivate->curState = OMX_StateLoaded;
2084        pComponentPrivate->cbInfo.EventHandler (
2085                                                pHandle, pHandle->pApplicationPrivate,
2086                                                OMX_EventCmdComplete, OMX_CommandStateSet,pComponentPrivate->curState,
2087                                                NULL);
2088        G722ENC_DPRINT("%d :: Transitioning from WaitFor to Loaded\n",__LINE__);
2089        goto EXIT;
2090    }
2091
2092    if (pComponentPrivate->curState != OMX_StateIdle) {
2093        pComponentPrivate->cbInfo.EventHandler (
2094                                                pHandle, pHandle->pApplicationPrivate,
2095                                                OMX_EventError, OMX_ErrorIncorrectStateTransition, 0,
2096                                                "Invalid State");
2097        G722ENC_DPRINT("%d :: Error: Invalid State Given by Application\n",__LINE__);
2098        goto EXIT;
2099    }
2100
2101    G722ENC_DPRINT("%d: G722ENCUTILS::About to call LCML_ControlCodec\n",__LINE__);
2102    G722ENC_DPRINT("%d: G722ENCUTILS::Current State = %d\n",__LINE__,pComponentPrivate->curState);
2103    G722ENC_DPRINT("pComponentPrivate->pInputBufferList->numBuffers = %d\n",pComponentPrivate->pInputBufferList->numBuffers);
2104    G722ENC_DPRINT("pComponentPrivate->pOutputBufferList->numBuffers = %d\n",pComponentPrivate->pOutputBufferList->numBuffers);
2105
2106    if (pComponentPrivate->pInputBufferList->numBuffers ||
2107        pComponentPrivate->pOutputBufferList->numBuffers){
2108        pComponentPrivate->InIdle_goingtoloaded = 1;
2109#ifndef UNDER_CE
2110        pthread_mutex_lock(&pComponentPrivate->InIdle_mutex);
2111        pthread_cond_wait(&pComponentPrivate->InIdle_threshold, &pComponentPrivate->InIdle_mutex);
2112        pthread_mutex_unlock(&pComponentPrivate->InIdle_mutex);
2113#else
2114        OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
2115#endif
2116        pComponentPrivate->bLoadedCommandPending = OMX_FALSE;
2117    }
2118
2119    pComponentPrivate->curState = OMX_StateLoaded;
2120    G722ENC_DPRINT("About to destroy codec\n");
2121    eError = LCML_ControlCodec(
2122                               ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2123                               EMMCodecControlDestroy,(void *)pArgs);
2124    G722ENC_DPRINT("Finished destroying codec\n");
2125
2126    pComponentPrivate->bStreamCtrlCalled = 0;
2127
2128    if (eError != OMX_ErrorNone) {
2129        G722ENC_DPRINT("%d : Error: in Destroying the codec\n",__LINE__);
2130
2131        goto EXIT;
2132    }
2133
2134    if (pComponentPrivate->pCreatePhaseArgs == NULL) {
2135        G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pCreatePhaseArgs);
2136        free(pComponentPrivate->pCreatePhaseArgs);
2137        pComponentPrivate->pCreatePhaseArgs = NULL;
2138    }
2139
2140    pTemp_lcml = pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT];
2141    for(i=0; i<pComponentPrivate->noInitInputBuf; i++) {
2142        if (pTemp_lcml->pIpParam) {
2143            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pTemp_lcml->pIpParam);
2144            free(pTemp_lcml->pIpParam);
2145            pTemp_lcml->pIpParam = NULL;
2146        }
2147        pTemp_lcml++;
2148    }
2149    G722ENC_DPRINT ("%d :: G722ENC_CleanupInitParams()\n", __LINE__);
2150
2151
2152    G722ENC_DPRINT("pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT] = %p\n",pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT]);
2153    if (pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT]) {
2154        G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT]);
2155        free(pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT]);
2156        pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT] = NULL;
2157    }
2158
2159    G722ENC_DPRINT("freeing pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT] = 0x%x\n",pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT]);
2160
2161    G722ENC_DPRINT("pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT] = %p\n",pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT]);
2162
2163    if (pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT]) {
2164        G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT]);
2165        free(pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT]);
2166        pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT] = NULL;
2167    }
2168
2169    if (pComponentPrivate->strmAttr) {
2170        G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pComponentPrivate->strmAttr);
2171        free(pComponentPrivate->strmAttr);
2172        pComponentPrivate->strmAttr = NULL;
2173    }
2174    eError = EXIT_COMPONENT_THRD;
2175    pComponentPrivate->bInitParamsInitialized = 0;
2176
2177 EXIT:
2178    return eError;
2179}
2180
2181/* ================================================================================= */
2182/**
2183 * @fn G722ENC_CommandToExecuting() description for G722ENC_CommandToExecuting
2184 G722ENC_CommandToExecuting().
2185 This component is called by HandleCommand() when the component is commanded to Executing
2186 * @param pComponent  handle for this instance of the component
2187 *
2188 * @pre
2189 *
2190 * @post
2191 *
2192 * @return OMX_ERRORTYPE
2193 */
2194/* ================================================================================ */
2195OMX_ERRORTYPE G722ENC_CommandToExecuting(G722ENC_COMPONENT_PRIVATE *pComponentPrivate)
2196{
2197    OMX_U16 i = 0;
2198    OMX_ERRORTYPE eError = OMX_ErrorNone;
2199    char *pArgs = "damedesuStr";
2200    OMX_BUFFERHEADERTYPE *pBufHdr = NULL;
2201    OMX_U32 nBuf = 0;
2202    OMX_COMPONENTTYPE *pHandle =
2203        (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
2204    OMX_HANDLETYPE pLcmlHandle = pComponentPrivate->pLcmlHandle;
2205    G722ENC_LCML_BUFHEADERTYPE *pLcmlHdr = NULL;
2206
2207    G722ENC_DPRINT("%d: G722ENC_HandleCommand: Cmd Executing \n",__LINE__);
2208    pComponentPrivate->bBypassDSP = 0;
2209    if (pComponentPrivate->curState == OMX_StateExecuting){
2210        pComponentPrivate->cbInfo.EventHandler (
2211                                                pHandle, pHandle->pApplicationPrivate,
2212                                                OMX_EventError, OMX_ErrorSameState,0,
2213                                                NULL);
2214        G722ENC_DPRINT("%d :: Error: Same State Given by \
2215                               Application\n",__LINE__);
2216    }
2217    else if (pComponentPrivate->curState == OMX_StateIdle) {
2218        if(pComponentPrivate->dasfmode == 1 && pComponentPrivate->bStreamCtrlCalled == 0) {
2219            G722ENC_DPRINT("%d :: G722 ENCODER IS RUNNING UNDER DASF MODE\n",__LINE__);
2220            G722ENC_DPRINT("%d :: ---- Comp: DASF Functionality is ON ---\n",__LINE__);
2221
2222            pComponentPrivate->pParams->iAudioFormat = (unsigned short)pComponentPrivate->g722Params->nChannels;
2223            pComponentPrivate->pParams->iStrmId = pComponentPrivate->streamID;
2224            pComponentPrivate->pParams->iSamplingRate = G722ENC_SAMPLE_RATE;
2225
2226            G722ENC_DPRINT("%d ::pComponentPrivate->pParams->iAudioFormat   = %d\n",__LINE__,pComponentPrivate->pParams->iAudioFormat);
2227            G722ENC_DPRINT("%d ::pComponentPrivate->pParams->iSamplingRate  = %d\n",__LINE__,pComponentPrivate->pParams->iSamplingRate);
2228            G722ENC_DPRINT("%d ::pComponentPrivate->pParams->iStrmId = %d\n",__LINE__,pComponentPrivate->pParams->iStrmId);
2229
2230            pValues[0] = USN_STRMCMD_SETCODECPARAMS;
2231            pValues[1] = (OMX_U32)pComponentPrivate->pParams;
2232            pValues[2] = sizeof(G722ENC_AudioCodecParams);
2233
2234            G722ENC_DPRINT("%d: G722ENCUTILS::About to call LCML_ControlCodec\n",__LINE__);
2235            eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2236                                       EMMCodecControlStrmCtrl,(void *)pValues);
2237            if(eError != OMX_ErrorNone) {
2238                G722ENC_DPRINT("%d: Error Occurred in Codec StreamControl..\n",__LINE__);
2239                goto EXIT;
2240            }
2241        }
2242
2243        G722ENC_DPRINT("%d: G722ENCUTILS::About to call LCML_ControlCodec\n",__LINE__);
2244        eError = LCML_ControlCodec(
2245                                   ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2246                                   EMMCodecControlStart,(void *)pArgs);
2247        if(eError != OMX_ErrorNone) {
2248            G722ENC_DPRINT("%d: Error Occurred in Codec Start..\n",__LINE__);
2249            goto EXIT;
2250        }
2251        nBuf = pComponentPrivate->pInputBufferList->numBuffers;
2252
2253        if(pComponentPrivate->dasfmode == 0) {
2254            G722ENC_DPRINT ("%d :: Comp :: After LCML_StartCodec function \n",__LINE__);
2255            nBuf = pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->nBufferCountActual;
2256            pBufHdr = pComponentPrivate->pBufHeader[G722ENC_INPUT_PORT];
2257            G722ENC_DPRINT ("%d :: Comp :: After LCML_StartCodec function \n",__LINE__);
2258            G722ENC_DPRINT ("Sending Input buffer to Application\n");
2259            G722ENC_DPRINT ("%d :: Component Sending Input buffer to Application %p\n",__LINE__, pBufHdr);
2260            pLcmlHdr = pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT];
2261        }
2262    }
2263    else if (pComponentPrivate->curState == OMX_StatePause) {
2264        G722ENC_DPRINT("%d: Comp: Resume Command Came from App\n",__LINE__);
2265        G722ENC_DPRINT("%d: G722ENCUTILS::About to call LCML_ControlCodec\n",__LINE__);
2266        eError = LCML_ControlCodec(
2267                                   ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2268                                   EMMCodecControlStart,(void *)pArgs);
2269
2270        if (eError != OMX_ErrorNone) {
2271            G722ENC_DPRINT ("Error While Resuming the codec\n");
2272            goto EXIT;
2273        }
2274
2275        G722ENC_DPRINT("pComponentPrivate->nNumInputBufPending = %d\n",pComponentPrivate->nNumInputBufPending);
2276        if (pComponentPrivate->nNumInputBufPending < pComponentPrivate->pInputBufferList->numBuffers) {
2277            pComponentPrivate->nNumInputBufPending = pComponentPrivate->pInputBufferList->numBuffers;
2278        }
2279
2280        for (i=0; i < pComponentPrivate->nNumInputBufPending; i++) {
2281            if (pComponentPrivate->pInputBufHdrPending[i]) {
2282                if (!G722ENC_IsPending(pComponentPrivate, pComponentPrivate->pInputBufHdrPending[i], OMX_DirInput)) {
2283                    G722ENC_GetCorresponding_LCMLHeader(pComponentPrivate->pInputBufHdrPending[i]->pBuffer, OMX_DirInput, &pLcmlHdr);
2284                    G722ENC_SetPending(pComponentPrivate,pComponentPrivate->pInputBufHdrPending[i],OMX_DirInput);
2285
2286                    eError = LCML_QueueBuffer(
2287                                              ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2288                                              EMMCodecInputBuffer,
2289                                              pComponentPrivate->pInputBufHdrPending[i]->pBuffer,
2290                                              pComponentPrivate->pInputBufHdrPending[i]->nAllocLen,
2291                                              pComponentPrivate->pInputBufHdrPending[i]->nFilledLen,
2292                                              (OMX_U8 *) pLcmlHdr->pIpParam,
2293                                              sizeof(G722ENC_UAlgInBufParamStruct),
2294                                              pComponentPrivate->pInputBufHdrPending[i]->pBuffer);
2295                }
2296            }
2297        }
2298
2299        for (i=0; i < pComponentPrivate->nNumOutputBufPending; i++) {
2300            if (pComponentPrivate->pOutputBufHdrPending[i]) {
2301                if (!G722ENC_IsPending(pComponentPrivate, pComponentPrivate->pOutputBufHdrPending[i], OMX_DirOutput)) {
2302                    G722ENC_GetCorresponding_LCMLHeader(pComponentPrivate->pOutputBufHdrPending[i]->pBuffer, OMX_DirOutput, &pLcmlHdr);
2303                    G722ENC_SetPending(pComponentPrivate,pComponentPrivate->pOutputBufHdrPending[i],OMX_DirOutput);
2304
2305                    eError = LCML_QueueBuffer(
2306                                              ((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2307                                              EMMCodecOuputBuffer,
2308                                              pComponentPrivate->pOutputBufHdrPending[i]->pBuffer,
2309                                              pComponentPrivate->pOutputBufHdrPending[i]->nAllocLen,
2310                                              pComponentPrivate->pOutputBufHdrPending[i]->nFilledLen,
2311                                              NULL,
2312                                              0,
2313                                              pComponentPrivate->pOutputBufHdrPending[i]->pBuffer);
2314
2315
2316                }
2317            }
2318        }
2319    }
2320    else {
2321        pComponentPrivate->cbInfo.EventHandler (
2322                                                pHandle, pHandle->pApplicationPrivate,
2323                                                OMX_EventError, OMX_ErrorIncorrectStateTransition, 0,
2324                                                "Invalid State");
2325        G722ENC_DPRINT("%d :: Error: Invalid State Given by Application\n",__LINE__);
2326        goto EXIT;
2327    }
2328    pComponentPrivate->curState = OMX_StateExecuting;
2329    pComponentPrivate->cbInfo.EventHandler(pHandle,
2330                                           pHandle->pApplicationPrivate,
2331                                           OMX_EventCmdComplete,
2332                                           OMX_CommandStateSet, pComponentPrivate->curState, NULL);
2333 EXIT:
2334    return eError;
2335}
2336
2337/* ================================================================================= */
2338/**
2339 * @fn G722ENC_CommandToPause() description for G722ENC_CommandToPause
2340 G722ENC_CommandToPause().
2341 This component is called by HandleCommand() when the component is commanded to Paused
2342 * @param pComponent  handle for this instance of the component
2343 *
2344 * @pre
2345 *
2346 * @post
2347 *
2348 * @return OMX_ERRORTYPE
2349 */
2350/* ================================================================================ */
2351OMX_ERRORTYPE G722ENC_CommandToPause(G722ENC_COMPONENT_PRIVATE *pComponentPrivate)
2352{
2353    OMX_ERRORTYPE eError = OMX_ErrorNone;
2354    char *pArgs = "damedesuStr";
2355    OMX_COMPONENTTYPE *pHandle =
2356        (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
2357    OMX_HANDLETYPE pLcmlHandle = pComponentPrivate->pLcmlHandle;
2358
2359    G722ENC_DPRINT("%d: G722ENC_HandleCommand: Cmd Pause: Cur State = %d\n",__LINE__,pComponentPrivate->curState);
2360    if (pComponentPrivate->curState == OMX_StatePause){
2361        pComponentPrivate->cbInfo.EventHandler (
2362                                                pHandle, pHandle->pApplicationPrivate,
2363                                                OMX_EventError, OMX_ErrorSameState,0,
2364                                                NULL);
2365        G722ENC_DPRINT("%d :: Error: Same State Given by Application\n",__LINE__);
2366        goto EXIT;
2367    }
2368    if ((pComponentPrivate->curState != OMX_StateExecuting) &&
2369        (pComponentPrivate->curState != OMX_StateIdle)) {
2370
2371        pComponentPrivate->cbInfo.EventHandler (
2372                                                pHandle, pHandle->pApplicationPrivate,
2373                                                OMX_EventError, OMX_ErrorIncorrectStateTransition, 0,
2374                                                "Invalid State");
2375
2376        G722ENC_DPRINT("%d :: Error: Invalid State Given by Application\n",__LINE__);
2377        goto EXIT;
2378    }
2379
2380    G722ENC_DPRINT("%d: G722ENCUTILS::About to call LCML_ControlCodec\n",__LINE__);
2381    eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2382                               EMMCodecControlPause,(void *)pArgs);
2383
2384    if (eError != OMX_ErrorNone) {
2385        G722ENC_DPRINT("%d : Error: in Pausing the codec\n",__LINE__);
2386        goto EXIT;
2387    }
2388
2389    pComponentPrivate->curState = OMX_StatePause;
2390    /*    pComponentPrivate->cbInfo.EventHandler(pHandle,
2391          pHandle->pApplicationPrivate,
2392          OMX_EventCmdComplete,
2393          OMX_CommandStateSet,
2394          pComponentPrivate->curState,
2395          NULL); */
2396    G722ENC_DPRINT("%d :: Component: Codec Is Paused\n",__LINE__);
2397 EXIT:
2398    return eError;
2399}
2400
2401
2402
2403/* ================================================================================= */
2404/**
2405 * @fn G722ENC_CommandToWaitForResources() description for G722ENC_CommandToWaitForResources
2406 G722ENC_CommandToWaitForResources().
2407 This component is called by HandleCommand() when the component is commanded
2408 to WaitForResources
2409 * @param pComponent  handle for this instance of the component
2410 *
2411 * @pre
2412 *
2413 * @post
2414 *
2415 * @return OMX_ERRORTYPE
2416 */
2417/* ================================================================================ */
2418OMX_ERRORTYPE G722ENC_CommandToWaitForResources(G722ENC_COMPONENT_PRIVATE *pComponentPrivate)
2419{
2420    OMX_ERRORTYPE eError = OMX_ErrorNone;
2421    OMX_COMPONENTTYPE *pHandle =
2422        (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
2423
2424    G722ENC_DPRINT("%d: G722ENC_HandleCommand: Cmd : OMX_StateWaitForResources\n",__LINE__);
2425    if (pComponentPrivate->curState == OMX_StateWaitForResources){
2426        G722ENC_DPRINT("%d :: Comp: OMX_AmrDecUtils.c\n",__LINE__);
2427        pComponentPrivate->cbInfo.EventHandler (
2428                                                pHandle, pHandle->pApplicationPrivate,
2429                                                OMX_EventError, OMX_ErrorSameState,0,
2430                                                NULL);
2431        G722ENC_DPRINT("%d :: Error: Same State Given by Application\n",__LINE__);
2432        goto EXIT;
2433    }
2434    else if (pComponentPrivate->curState == OMX_StateLoaded) {
2435        pComponentPrivate->curState = OMX_StateWaitForResources;
2436        G722ENC_DPRINT("%d: Transitioning from Loaded to OMX_StateWaitForResources\n",__LINE__);
2437        pComponentPrivate->cbInfo.EventHandler(
2438                                               pHandle, pHandle->pApplicationPrivate,
2439                                               OMX_EventCmdComplete,
2440                                               OMX_CommandStateSet,pComponentPrivate->curState, NULL);
2441        goto EXIT;
2442    }
2443    else {
2444        pComponentPrivate->cbInfo.EventHandler(
2445                                               pHandle, pHandle->pApplicationPrivate,
2446                                               OMX_EventError, OMX_ErrorIncorrectStateTransition,0, NULL);
2447    }
2448 EXIT:
2449    return eError;
2450}
2451
2452
2453/* ================================================================================= */
2454/**
2455 * @fn G722ENC_SetPending() description for G722ENC_SetPending
2456 G722ENC_SetPending().
2457 This component is called when a buffer is queued to the LCML
2458 * @param pComponent  handle for this instance of the component
2459 *
2460 * @pre
2461 *
2462 * @post
2463 *
2464 * @return OMX_ERRORTYPE
2465 */
2466/* ================================================================================ */
2467void G722ENC_SetPending(G722ENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir)
2468{
2469    OMX_U16 i = 0;
2470
2471    if (eDir == OMX_DirInput) {
2472        for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
2473            if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
2474                pComponentPrivate->pInputBufferList->bBufferPending[i] = 1;
2475                G722ENC_DPRINT("*******************INPUT BUFFER %d IS PENDING ******************************\n",i);
2476            }
2477        }
2478    }
2479    else {
2480        for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
2481            if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
2482                pComponentPrivate->pOutputBufferList->bBufferPending[i] = 1;
2483                G722ENC_DPRINT("*******************OUTPUT BUFFER %d IS PENDING******************************\n",i);
2484            }
2485        }
2486    }
2487}
2488
2489/* ================================================================================= */
2490/**
2491 * @fn G722ENC_ClearPending() description for G722ENC_ClearPending
2492 G722ENC_ClearPending().
2493 This component is called when a buffer is returned from the LCML
2494 * @param pComponent  handle for this instance of the component
2495 *
2496 * @pre
2497 *
2498 * @post
2499 *
2500 * @return OMX_ERRORTYPE
2501 */
2502/* ================================================================================ */
2503void G722ENC_ClearPending(G722ENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir)
2504{
2505    OMX_U16 i = 0;
2506
2507    if (eDir == OMX_DirInput) {
2508        for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
2509            if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
2510                pComponentPrivate->pInputBufferList->bBufferPending[i] = 0;
2511                G722ENC_DPRINT("*******************INPUT BUFFER %d IS RECLAIMED******************************\n",i);
2512            }
2513        }
2514    }
2515    else {
2516        for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
2517            if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
2518                pComponentPrivate->pOutputBufferList->bBufferPending[i] = 0;
2519                G722ENC_DPRINT("*******************OUTPUT BUFFER %d IS RECLAIMED ******************************\n",i);
2520            }
2521        }
2522    }
2523}
2524
2525/* ================================================================================= */
2526/**
2527 * @fn G722ENC_IsPending() description for G722ENC_IsPending
2528 G722ENC_IsPending().
2529 This method returns the pending status to the buffer
2530 * @param pComponent  handle for this instance of the component
2531 *
2532 * @pre
2533 *
2534 * @post
2535 *
2536 * @return OMX_ERRORTYPE
2537 */
2538/* ================================================================================ */
2539OMX_U32 G722ENC_IsPending(G722ENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE *pBufHdr, OMX_DIRTYPE eDir)
2540{
2541    OMX_U16 i = 0;
2542
2543    if (eDir == OMX_DirInput) {
2544        for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
2545            if (pBufHdr == pComponentPrivate->pInputBufferList->pBufHdr[i]) {
2546                return pComponentPrivate->pInputBufferList->bBufferPending[i];
2547            }
2548        }
2549    }
2550    else {
2551        for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
2552            if (pBufHdr == pComponentPrivate->pOutputBufferList->pBufHdr[i]) {
2553                return pComponentPrivate->pOutputBufferList->bBufferPending[i];
2554            }
2555        }
2556    }
2557    return -1;
2558}
2559
2560/* ================================================================================= */
2561/**
2562 * @fn G722ENC_Fill_LCMLInitParamsEx() description for G722ENC_Fill_LCMLInitParamsEx
2563 G722ENC_Fill_LCMLInitParamsEx().
2564 This method fills the LCML init parameters.
2565 * @param pComponent  handle for this instance of the component
2566 *
2567 * @pre
2568 *
2569 * @post
2570 *
2571 * @return OMX_ERRORTYPE
2572 */
2573/* ================================================================================ */
2574OMX_ERRORTYPE G722ENC_Fill_LCMLInitParamsEx(OMX_HANDLETYPE pComponent)
2575{
2576    OMX_ERRORTYPE eError = OMX_ErrorNone;
2577    OMX_U32 nIpBuf = 0,nIpBufSize = 0,nOpBuf = 0,nOpBufSize = 0;
2578    OMX_BUFFERHEADERTYPE *pTemp = NULL;
2579    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
2580    G722ENC_COMPONENT_PRIVATE *pComponentPrivate = pHandle->pComponentPrivate;
2581    LCML_STRMATTR *strmAttr = NULL;
2582    G722ENC_LCML_BUFHEADERTYPE *pTemp_lcml = NULL;
2583    OMX_U16 i = 0;
2584    OMX_U32 size_lcml = 0;
2585
2586    G722ENC_DPRINT("%d :: G722ENCFill_LCMLInitParamsEx\n ",__LINE__);
2587    G722ENC_DPRINT("[G722ENC_Fill_LCMLInitParamsEx] pComponent = %p\n",pComponent);
2588    G722ENC_DPRINT("[G722ENC_Fill_LCMLInitParamsEx] pComponentPrivate = %p\n",pComponentPrivate);
2589
2590
2591    nIpBuf = pComponentPrivate->pInputBufferList->numBuffers;
2592    nIpBufSize = pComponentPrivate->pPortDef[G722ENC_INPUT_PORT]->nBufferSize;
2593    nOpBuf = pComponentPrivate->pOutputBufferList->numBuffers;
2594    nOpBufSize = pComponentPrivate->pPortDef[G722ENC_OUTPUT_PORT]->nBufferSize;
2595
2596    G722ENC_DPRINT("%d :: ------ Buffer Details -----------\n",__LINE__);
2597    G722ENC_DPRINT("%d :: Input  Buffer Count = %ld\n",__LINE__,nIpBuf);
2598    G722ENC_DPRINT("%d :: Input  Buffer Size = %ld\n",__LINE__,nIpBufSize);
2599    G722ENC_DPRINT("%d :: Output Buffer Count = %ld\n",__LINE__,nOpBuf);
2600    G722ENC_DPRINT("%d :: Output Buffer Size = %ld\n",__LINE__,nOpBufSize);
2601    G722ENC_DPRINT("%d :: ------ Buffer Details ------------\n",__LINE__);
2602
2603    /* Allocate memory for all input buffer headers..
2604     * This memory pointer will be sent to LCML */
2605    size_lcml = nIpBuf * sizeof(G722ENC_LCML_BUFHEADERTYPE);
2606    pTemp_lcml = (G722ENC_LCML_BUFHEADERTYPE *)malloc(size_lcml);
2607    G722ENC_MEMPRINT("%d :: [ALLOC] %p\n",__LINE__,pTemp_lcml);
2608    if(pTemp_lcml == NULL) {
2609        G722ENC_DPRINT("%d :: Memory Allocation Failed\n",__LINE__);
2610        eError = OMX_ErrorInsufficientResources;
2611        goto EXIT;
2612    }
2613    pComponentPrivate->pLcmlBufHeader[G722ENC_INPUT_PORT] = pTemp_lcml;
2614    for (i=0; i<nIpBuf; i++) {
2615        G722ENC_DPRINT("%d :: INPUT--------- Inside Ip Loop\n",__LINE__);
2616        pTemp = pComponentPrivate->pInputBufferList->pBufHdr[i];
2617        pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
2618        pTemp->nAllocLen = nIpBufSize;
2619        pTemp->nFilledLen = nIpBufSize;
2620        pTemp->nVersion.s.nVersionMajor = G722ENC_MAJOR_VER;
2621        pTemp->nVersion.s.nVersionMinor = G722ENC_MINOR_VER;
2622        pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
2623        pTemp->nTickCount = NOT_USED;
2624        pTemp_lcml->pBufHdr = pTemp;
2625        G722ENC_DPRINT("%d :: pTemp_lcml->pBufHdr->pBuffer = %p \n",__LINE__,pTemp_lcml->pBufHdr->pBuffer);
2626        pTemp_lcml->eDir = OMX_DirInput;
2627        pTemp_lcml->pOtherParams[i] = NULL;
2628        pTemp_lcml->pIpParam = (G722ENC_UAlgInBufParamStruct *)malloc(sizeof(G722ENC_UAlgInBufParamStruct));
2629
2630        G722ENC_MEMPRINT("%d:::[ALLOC] %p\n",__LINE__,pTemp_lcml->pIpParam);
2631        if (pTemp_lcml->pIpParam == NULL) {
2632            /* Free previously allocated memory before bailing */
2633            if (strmAttr) {
2634                G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,strmAttr);
2635                free(strmAttr);
2636                strmAttr = NULL;
2637            }
2638
2639
2640            if (pTemp_lcml) {
2641                G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,pTemp_lcml);
2642                free(pTemp_lcml);
2643                pTemp_lcml = NULL;
2644            }
2645
2646            G722ENC_DPRINT("%d :: Error: Malloc Failed...Exiting..\n",__LINE__);
2647            goto EXIT;
2648        }
2649        pTemp_lcml->pIpParam->bLastBuffer = 0;
2650
2651        pTemp->nFlags = NORMAL_BUFFER;
2652        ((G722ENC_COMPONENT_PRIVATE *) pTemp->pPlatformPrivate)->pHandle = pHandle;
2653        G722ENC_DPRINT("%d ::Comp: InBuffHeader[%d] = %p\n", __LINE__, i, pTemp);
2654        G722ENC_DPRINT("%d ::Comp:  >>>> InputBuffHeader[%d]->pBuffer = %p\n",
2655                       __LINE__, i, pTemp->pBuffer);
2656        G722ENC_DPRINT("%d ::Comp: Ip : pTemp_lcml[%d] = %p\n", __LINE__, i, pTemp_lcml);
2657
2658        pTemp_lcml++;
2659    }
2660
2661    pTemp = pComponentPrivate->pBufHeader[G722ENC_OUTPUT_PORT];
2662    size_lcml = nOpBuf * sizeof(G722ENC_LCML_BUFHEADERTYPE);
2663    pTemp_lcml = (G722ENC_LCML_BUFHEADERTYPE *)malloc(size_lcml);
2664    G722ENC_MEMPRINT("%d:::[ALLOC] %p\n",__LINE__,pTemp_lcml);
2665    if(pTemp_lcml == NULL) {
2666        G722ENC_DPRINT("%d :: Memory Allocation Failed\n",__LINE__);
2667        /* Free previously allocated memory before bailing */
2668        if (strmAttr) {
2669            G722ENC_MEMPRINT("%d:::[FREE] %p\n",__LINE__,strmAttr);
2670            free(strmAttr);
2671            strmAttr = NULL;
2672        }
2673
2674        eError = OMX_ErrorInsufficientResources;
2675        goto EXIT;
2676    }
2677    pComponentPrivate->pLcmlBufHeader[G722ENC_OUTPUT_PORT] = pTemp_lcml;
2678
2679    for (i=0; i<nOpBuf; i++) {
2680        pTemp = pComponentPrivate->pOutputBufferList->pBufHdr[i];
2681        pTemp->nSize = sizeof(OMX_BUFFERHEADERTYPE);
2682        pTemp->nAllocLen = nOpBufSize;
2683        pTemp->nFilledLen = nOpBufSize;
2684        pTemp->nVersion.s.nVersionMajor = G722ENC_MAJOR_VER;
2685        pTemp->nVersion.s.nVersionMinor = G722ENC_MINOR_VER;
2686        pComponentPrivate->nVersion = pTemp->nVersion.nVersion;
2687        pTemp->pPlatformPrivate = pHandle->pComponentPrivate;
2688        pTemp->nTickCount = NOT_USED;
2689        pTemp_lcml->pBufHdr = pTemp;
2690        pTemp_lcml->eDir = OMX_DirOutput;
2691        pTemp_lcml->pOtherParams[i] = NULL;
2692
2693        pTemp->nFlags = NORMAL_BUFFER;
2694        ((G722ENC_COMPONENT_PRIVATE *)pTemp->pPlatformPrivate)->pHandle = pHandle;
2695        G722ENC_DPRINT("%d ::Comp:  >>>>>>>>>>>>> OutBuffHeader[%d] = %p\n",
2696                       __LINE__, i, pTemp);
2697        G722ENC_DPRINT("%d ::Comp:  >>>> OutBuffHeader[%d]->pBuffer = %p\n",
2698                       __LINE__, i, pTemp->pBuffer);
2699        G722ENC_DPRINT("%d ::Comp: Op : pTemp_lcml[%d] = %p\n", __LINE__, i, pTemp_lcml);
2700        pTemp++;
2701        pTemp_lcml++;
2702    }
2703    pComponentPrivate->bPortDefsAllocated = 1;
2704
2705    G722ENC_DPRINT("%d :: Exiting G722ENC_Fill_LCMLInitParams",__LINE__);
2706
2707    pComponentPrivate->bInitParamsInitialized = 1;
2708 EXIT:
2709    return eError;
2710}
2711
2712
2713/* ================================================================================= */
2714/**
2715 * @fn G722ENC_IsValid() description for G722ENC_Fill_LCMLInitParamsEx
2716 G722ENC_IsValid().
2717 This method checks to see if a buffer returned from the LCML is valid.
2718 * @param pComponent  handle for this instance of the component
2719 *
2720 * @pre
2721 *
2722 * @post
2723 *
2724 * @return OMX_ERRORTYPE
2725 */
2726/* ================================================================================ */
2727OMX_U32 G722ENC_IsValid(G722ENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U8 *pBuffer, OMX_DIRTYPE eDir)
2728{
2729    OMX_U16 i = 0;
2730    int found=0;
2731
2732    if (eDir == OMX_DirInput) {
2733        for (i=0; i < pComponentPrivate->pInputBufferList->numBuffers; i++) {
2734            if (pBuffer == pComponentPrivate->pInputBufferList->pBufHdr[i]->pBuffer) {
2735                found = 1;
2736            }
2737        }
2738    }
2739    else {
2740        for (i=0; i < pComponentPrivate->pOutputBufferList->numBuffers; i++) {
2741            if (pBuffer == pComponentPrivate->pOutputBufferList->pBufHdr[i]->pBuffer) {
2742                found = 1;
2743            }
2744        }
2745    }
2746    return found;
2747}
2748
2749