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