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_AmrEncoder.c
30*
31* This file implements OpenMAX (TM) 1.0 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\nbamr_enc\src
35*
36* @rev  1.0
37*/
38/* ----------------------------------------------------------------------------
39*!
40*! Revision History
41*! ===================================
42*! 21-sept-2006 bk: updated review findings for alpha release
43*! 24-Aug-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests some more
44*! 18-July-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests validated for few cases
45*! 21-Jun-2006 bk: Khronos OpenMAX (TM) 1.0 migration done
46*! 22-May-2006 bk: DASF recording quality improved
47*! 19-Apr-2006 bk: DASF recording speed issue resloved
48*! 23-Feb-2006 bk: DASF functionality added
49*! 18-Jan-2006 bk: Repated recording issue fixed and LCML changes taken care
50*! 14-Dec-2005 bk: Initial Version
51*! 16-Nov-2005 bk: Initial Version
52*! 23-Sept-2005 bk: Initial Version
53*! 10-Sept-2005 bk: Initial Version
54*! 10-Sept-2005 bk:
55*! This is newest file
56* =========================================================================== */
57/* ------compilation control switches -------------------------*/
58/****************************************************************
59*  INCLUDE FILES
60****************************************************************/
61/* ----- system and platform files ----------------------------*/
62#ifdef UNDER_CE
63#include <windows.h>
64#include <oaf_osal.h>
65#include <omx_core.h>
66#else
67#include <wchar.h>
68#include <unistd.h>
69#include <sys/time.h>
70#include <sys/types.h>
71#include <sys/ioctl.h>
72#include <sys/select.h>
73#include <errno.h>
74#include <pthread.h>
75#endif
76
77#include <string.h>
78#include <fcntl.h>
79#include <stdlib.h>
80#include <stdio.h>
81#include <dbapi.h>
82
83#ifdef DSP_RENDERING_ON
84    #include <AudioManagerAPI.h>
85#endif
86
87#ifdef RESOURCE_MANAGER_ENABLED
88    #include <ResourceManagerProxyAPI.h>
89#endif
90
91#ifdef __PERF_INSTRUMENTATION__
92    #include "perf.h"
93#endif
94
95/*-------program files ----------------------------------------*/
96#include <OMX_Component.h>
97
98#include "OMX_AmrEnc_Utils.h"
99
100/****************************************************************
101*  EXTERNAL REFERENCES NOTE : only use if not found in header file
102****************************************************************/
103/*--------data declarations -----------------------------------*/
104
105/*--------function prototypes ---------------------------------*/
106
107/****************************************************************
108*  PUBLIC DECLARATIONS Defined here, used elsewhere
109****************************************************************/
110/*--------data declarations -----------------------------------*/
111
112/*--------function prototypes ---------------------------------*/
113
114/****************************************************************
115*  PRIVATE DECLARATIONS Defined here, used only here
116****************************************************************/
117/*--------data declarations -----------------------------------*/
118
119/*--------function prototypes ---------------------------------*/
120#define AMRNB_DEC_ROLE "audio_encoder.amrnb"
121
122static OMX_ERRORTYPE SetCallbacks (OMX_HANDLETYPE hComp,
123        OMX_CALLBACKTYPE* pCallBacks, OMX_PTR pAppData);
124static OMX_ERRORTYPE GetComponentVersion (OMX_HANDLETYPE hComp,
125                                          OMX_STRING pComponentName,
126                                          OMX_VERSIONTYPE* pComponentVersion,
127                                          OMX_VERSIONTYPE* pSpecVersion,
128                                          OMX_UUIDTYPE* pComponentUUID);
129static OMX_ERRORTYPE SendCommand (OMX_HANDLETYPE hComp, OMX_COMMANDTYPE nCommand,
130       OMX_U32 nParam,OMX_PTR pCmdData);
131static OMX_ERRORTYPE GetParameter(OMX_HANDLETYPE hComp, OMX_INDEXTYPE nParamIndex,
132                                  OMX_PTR ComponentParamStruct);
133static OMX_ERRORTYPE SetParameter (OMX_HANDLETYPE hComp,
134                                   OMX_INDEXTYPE nParamIndex,
135                                   OMX_PTR ComponentParamStruct);
136static OMX_ERRORTYPE GetConfig (OMX_HANDLETYPE hComp,
137                                OMX_INDEXTYPE nConfigIndex,
138                                OMX_PTR pComponentConfigStructure);
139static OMX_ERRORTYPE SetConfig (OMX_HANDLETYPE hComp,
140                                OMX_INDEXTYPE nConfigIndex,
141                                OMX_PTR pComponentConfigStructure);
142
143static OMX_ERRORTYPE EmptyThisBuffer (OMX_HANDLETYPE hComp, OMX_BUFFERHEADERTYPE* pBuffer);
144
145static OMX_ERRORTYPE FillThisBuffer (OMX_HANDLETYPE hComp, OMX_BUFFERHEADERTYPE* pBuffer);
146static OMX_ERRORTYPE GetState (OMX_HANDLETYPE hComp, OMX_STATETYPE* pState);
147static OMX_ERRORTYPE ComponentTunnelRequest (OMX_HANDLETYPE hComp,
148                                             OMX_U32 nPort, OMX_HANDLETYPE hTunneledComp,
149                                             OMX_U32 nTunneledPort,
150                                             OMX_TUNNELSETUPTYPE* pTunnelSetup);
151static OMX_ERRORTYPE ComponentDeInit(OMX_HANDLETYPE pHandle);
152static OMX_ERRORTYPE AllocateBuffer (OMX_IN OMX_HANDLETYPE hComponent,
153                   OMX_INOUT OMX_BUFFERHEADERTYPE** pBuffer,
154                   OMX_IN OMX_U32 nPortIndex,
155                   OMX_IN OMX_PTR pAppPrivate,
156                   OMX_IN OMX_U32 nSizeBytes);
157
158static OMX_ERRORTYPE FreeBuffer(
159            OMX_IN  OMX_HANDLETYPE hComponent,
160            OMX_IN  OMX_U32 nPortIndex,
161            OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
162
163static OMX_ERRORTYPE UseBuffer (
164            OMX_IN OMX_HANDLETYPE hComponent,
165            OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
166            OMX_IN OMX_U32 nPortIndex,
167            OMX_IN OMX_PTR pAppPrivate,
168            OMX_IN OMX_U32 nSizeBytes,
169            OMX_IN OMX_U8* pBuffer);
170
171static OMX_ERRORTYPE GetExtensionIndex(
172            OMX_IN  OMX_HANDLETYPE hComponent,
173            OMX_IN  OMX_STRING cParameterName,
174            OMX_OUT OMX_INDEXTYPE* pIndexType);
175
176static OMX_ERRORTYPE ComponentRoleEnum(
177        OMX_IN OMX_HANDLETYPE hComponent,
178        OMX_OUT OMX_U8 *cRole,
179        OMX_IN OMX_U32 nIndex);
180
181#ifdef DSP_RENDERING_ON
182AM_COMMANDDATATYPE cmd_data;
183#endif
184
185/* interface with audio manager*/
186#define FIFO1 "/dev/fifo.1"
187#define FIFO2 "/dev/fifo.2"
188#define PERMS 0666
189
190
191
192/*-------------------------------------------------------------------*/
193/**
194  * OMX_ComponentInit() Set the all the function pointers of component
195  *
196  * This method will update the component function pointer to the handle
197  *
198  * @param hComp         handle for this instance of the component
199  *
200  * @retval OMX_NoError              Success, ready to roll
201  *         OMX_ErrorInsufficientResources If the newmalloc fails
202  **/
203/*-------------------------------------------------------------------*/
204OMX_ERRORTYPE OMX_ComponentInit (OMX_HANDLETYPE hComp)
205{
206    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef_ip, *pPortDef_op;
207    AMRENC_COMPONENT_PRIVATE *pComponentPrivate;
208    OMX_AUDIO_PARAM_PCMMODETYPE *amr_ip;
209    OMX_AUDIO_PARAM_AMRTYPE  *amr_op;
210    OMX_ERRORTYPE eError = OMX_ErrorNone;
211    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE*) hComp;
212    NBAMRENC_PORT_TYPE *pCompPort = NULL;
213    OMX_AUDIO_PARAM_PORTFORMATTYPE *pPortFormat = NULL;
214    int i = 0;
215
216    OMXDBG_PRINT(stderr, PRINT, 1, 0, "Entering OMX_ComponentInit\n");
217
218    /*Set the all component function pointer to the handle */
219    pHandle->SetCallbacks = SetCallbacks;
220    pHandle->GetComponentVersion = GetComponentVersion;
221    pHandle->SendCommand = SendCommand;
222    pHandle->GetParameter = GetParameter;
223    pHandle->SetParameter = SetParameter;
224    pHandle->GetConfig = GetConfig;
225    pHandle->SetConfig = SetConfig;
226    pHandle->GetState = GetState;
227    pHandle->EmptyThisBuffer = EmptyThisBuffer;
228    pHandle->FillThisBuffer = FillThisBuffer;
229    pHandle->ComponentTunnelRequest = ComponentTunnelRequest;
230    pHandle->ComponentDeInit = ComponentDeInit;
231    pHandle->AllocateBuffer = AllocateBuffer;
232    pHandle->FreeBuffer = FreeBuffer;
233    pHandle->UseBuffer = UseBuffer;
234    pHandle->GetExtensionIndex = GetExtensionIndex;
235    pHandle->ComponentRoleEnum = ComponentRoleEnum;
236
237
238    /*Allocate the memory for Component private data area */
239    OMX_MALLOC_GENERIC(pHandle->pComponentPrivate, AMRENC_COMPONENT_PRIVATE);
240
241
242    ((AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pHandle = pHandle;
243    pComponentPrivate = pHandle->pComponentPrivate;
244
245    OMX_DBG_INIT(pComponentPrivate->dbg, "OMX_DBG_NBAMRENC");
246
247#ifdef ANDROID /* leave this now, we may need them later. */
248    pComponentPrivate->iPVCapabilityFlags.iIsOMXComponentMultiThreaded = OMX_TRUE;
249    pComponentPrivate->iPVCapabilityFlags.iOMXComponentNeedsNALStartCode = OMX_FALSE;
250    pComponentPrivate->iPVCapabilityFlags.iOMXComponentSupportsExternalOutputBufferAlloc = OMX_FALSE;
251    pComponentPrivate->iPVCapabilityFlags.iOMXComponentSupportsExternalInputBufferAlloc = OMX_FALSE;
252    pComponentPrivate->iPVCapabilityFlags.iOMXComponentSupportsMovableInputBuffers = OMX_FALSE;
253    pComponentPrivate->iPVCapabilityFlags.iOMXComponentSupportsPartialFrames = OMX_FALSE;
254    pComponentPrivate->iPVCapabilityFlags.iOMXComponentCanHandleIncompleteFrames = OMX_FALSE;
255#endif
256
257#ifdef __PERF_INSTRUMENTATION__
258    pComponentPrivate->pPERF = PERF_Create(PERF_FOURCC('N','B','E','_'),
259                                           PERF_ModuleLLMM | PERF_ModuleAudioDecode);
260#endif
261
262    OMX_MALLOC_GENERIC(pCompPort, NBAMRENC_PORT_TYPE);
263    pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT] = pCompPort;
264
265    OMX_MALLOC_GENERIC(pCompPort, NBAMRENC_PORT_TYPE);
266    pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT] = pCompPort;
267
268    OMX_MALLOC_GENERIC(pComponentPrivate->sPortParam, OMX_PORT_PARAM_TYPE);
269    OMX_NBCONF_INIT_STRUCT(pComponentPrivate->sPortParam, OMX_PORT_PARAM_TYPE);
270
271    /* Initialize sPortParam data structures to default values */
272    pComponentPrivate->sPortParam->nPorts = 0x2;
273    pComponentPrivate->sPortParam->nStartPortNumber = 0x0;
274
275    /* Malloc and Set pPriorityMgmt defaults */
276    OMX_MALLOC_GENERIC(pComponentPrivate->sPriorityMgmt, OMX_PRIORITYMGMTTYPE);
277    OMX_NBCONF_INIT_STRUCT(pComponentPrivate->sPriorityMgmt, OMX_PRIORITYMGMTTYPE);
278
279    /* Initialize sPriorityMgmt data structures to default values */
280    pComponentPrivate->sPriorityMgmt->nGroupPriority = -1;
281    pComponentPrivate->sPriorityMgmt->nGroupID = -1;
282
283    OMX_MALLOC_GENERIC(amr_ip, OMX_AUDIO_PARAM_PCMMODETYPE);
284    OMX_NBCONF_INIT_STRUCT(amr_ip, OMX_AUDIO_PARAM_PCMMODETYPE);
285    pComponentPrivate->pcmParams = amr_ip;
286
287
288    OMX_MALLOC_GENERIC(amr_op, OMX_AUDIO_PARAM_AMRTYPE);
289    OMX_NBCONF_INIT_STRUCT(amr_op, OMX_AUDIO_PARAM_AMRTYPE);
290    pComponentPrivate->amrParams = amr_op;
291
292    /* newmalloc and initialize number of input buffers */
293    OMX_MALLOC_GENERIC(pComponentPrivate->pInputBufferList, NBAMRENC_BUFFERLIST);
294    pComponentPrivate->pInputBufferList->numBuffers = 0;
295
296    /* newmalloc and initialize number of output buffers */
297    OMX_MALLOC_GENERIC(pComponentPrivate->pOutputBufferList, NBAMRENC_BUFFERLIST);
298    pComponentPrivate->pOutputBufferList->numBuffers = 0;
299
300    for (i=0; i < NBAMRENC_MAX_NUM_OF_BUFS; i++) {
301        pComponentPrivate->pOutputBufferList->pBufHdr[i] = NULL;
302        pComponentPrivate->pInputBufferList->pBufHdr[i] = NULL;
303    }
304
305    /* Set input port defaults */
306    OMX_MALLOC_GENERIC(pPortDef_ip, OMX_PARAM_PORTDEFINITIONTYPE);
307    OMX_NBCONF_INIT_STRUCT(pPortDef_ip, OMX_PARAM_PORTDEFINITIONTYPE);
308    pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT] = pPortDef_ip;
309
310
311    /* Let's set defaults for IF2 conditions on Android and see what happens */
312    pPortDef_ip->nPortIndex                         = NBAMRENC_INPUT_PORT;
313    pPortDef_ip->eDir                               = OMX_DirInput;
314    pPortDef_ip->nBufferCountActual                 = 5; //NBAMRENC_NUM_INPUT_BUFFERS;
315    pPortDef_ip->nBufferCountMin                    = 5; //NBAMRENC_NUM_INPUT_BUFFERS;
316    pPortDef_ip->nBufferSize                        = NBAMRENC_INPUT_FRAME_SIZE;
317    pPortDef_ip->nBufferAlignment                   = DSP_CACHE_ALIGNMENT;
318    pPortDef_ip->bEnabled                           = OMX_TRUE;
319    pPortDef_ip->bPopulated                         = OMX_FALSE;
320    pPortDef_ip->eDomain                            = OMX_PortDomainAudio;
321    pPortDef_ip->format.audio.eEncoding             = OMX_AUDIO_CodingPCM;
322    pPortDef_ip->format.audio.cMIMEType             = NULL;
323    pPortDef_ip->format.audio.pNativeRender         = NULL;
324    pPortDef_ip->format.audio.bFlagErrorConcealment = OMX_FALSE;
325
326    /* Set output port defaults */
327    OMX_MALLOC_GENERIC(pPortDef_op, OMX_PARAM_PORTDEFINITIONTYPE);
328    OMX_NBCONF_INIT_STRUCT(pPortDef_op, OMX_PARAM_PORTDEFINITIONTYPE);
329    pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT] = pPortDef_op;
330
331    pPortDef_op->nPortIndex                         = NBAMRENC_OUTPUT_PORT;
332    pPortDef_op->eDir                               = OMX_DirOutput;
333    pPortDef_op->nBufferCountMin                    = 9; //NBAMRENC_NUM_OUTPUT_BUFFERS;
334    pPortDef_op->nBufferCountActual                 = 9; //NBAMRENC_NUM_OUTPUT_BUFFERS;
335    pPortDef_op->nBufferSize                        = 320; //NBAMRENC_OUTPUT_FRAME_SIZE;
336    pPortDef_ip->nBufferAlignment                   = DSP_CACHE_ALIGNMENT;
337    pPortDef_op->bEnabled                           = OMX_TRUE;
338    pPortDef_op->bPopulated                         = OMX_FALSE;
339    pPortDef_op->eDomain                            = OMX_PortDomainAudio;
340    pPortDef_op->format.audio.eEncoding             = OMX_AUDIO_CodingAMR;
341    pPortDef_op->format.audio.cMIMEType             = NULL;
342    pPortDef_op->format.audio.pNativeRender         = NULL;
343    pPortDef_op->format.audio.bFlagErrorConcealment = OMX_FALSE;
344
345    OMX_MALLOC_GENERIC(pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]->pPortFormat, OMX_AUDIO_PARAM_PORTFORMATTYPE);
346    OMX_NBCONF_INIT_STRUCT(pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]->pPortFormat, OMX_AUDIO_PARAM_PORTFORMATTYPE);
347    /* Set input port format defaults */
348    pPortFormat = pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]->pPortFormat;
349    OMX_NBCONF_INIT_STRUCT(pPortFormat, OMX_AUDIO_PARAM_PORTFORMATTYPE);
350    pPortFormat->nPortIndex         = NBAMRENC_INPUT_PORT;
351    pPortFormat->nIndex             = OMX_IndexParamAudioAmr;
352    pPortFormat->eEncoding          = OMX_AUDIO_CodingPCM;  /*Data Expected on Input Port*/
353
354    pComponentPrivate->bPreempted = OMX_FALSE;
355
356    amr_ip->nPortIndex = NBAMRENC_INPUT_PORT;
357/*ck*/    amr_ip->nChannels = 1;  //was 2
358    amr_ip->eNumData= OMX_NumericalDataSigned;
359    amr_ip->nBitPerSample = 16;
360/*ck*/    amr_ip->nSamplingRate = 8000; //44100;
361    amr_ip->ePCMMode = OMX_AUDIO_PCMModeLinear;
362    amr_ip->bInterleaved = OMX_TRUE; /*For Encoders Only*/
363    strcpy((char *)pComponentPrivate->componentRole.cRole, "audio_encoder.amrnb");
364
365    amr_op->nPortIndex = NBAMRENC_OUTPUT_PORT;
366    amr_op->nChannels = 1;
367    amr_op->eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
368    amr_op->eAMRDTXMode= OMX_AUDIO_AMRDTXModeOff;
369    amr_op->eAMRFrameFormat =OMX_AUDIO_AMRFrameFormatConformance;
370
371
372    OMX_MALLOC_GENERIC(pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]->pPortFormat, OMX_AUDIO_PARAM_PORTFORMATTYPE);
373    OMX_NBCONF_INIT_STRUCT(pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]->pPortFormat, OMX_AUDIO_PARAM_PORTFORMATTYPE);
374    /* Set output port format defaults */
375    pPortFormat = pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]->pPortFormat;
376    OMX_NBCONF_INIT_STRUCT(pPortFormat, OMX_AUDIO_PARAM_PORTFORMATTYPE);
377    pPortFormat->nPortIndex         = NBAMRENC_OUTPUT_PORT;
378    pPortFormat->nIndex             = OMX_IndexParamAudioAmr;
379    pPortFormat->eEncoding          = OMX_AUDIO_CodingAMR;
380
381    OMX_PRDSP2(pComponentPrivate->dbg, "%d :: Setting dasf & mime & efr & acdn & amr and MultiFrame modes to 0\n",__LINE__);
382    pComponentPrivate->dasfMode = 0;
383    pComponentPrivate->teeMode = 0;
384    pComponentPrivate->frameMode = 0;
385    pComponentPrivate->acdnMode = 0;
386    pComponentPrivate->efrMode  = 0;
387    pComponentPrivate->amrMode  = 0;
388    pComponentPrivate->nMultiFrameMode = 0;
389    pComponentPrivate->bCompThreadStarted = 0;
390    pComponentPrivate->pParams = NULL;
391    pComponentPrivate->pAlgParam = NULL;
392    pComponentPrivate->bInitParamsInitialized = 0;
393    pComponentPrivate->amrMimeBytes[0]  = NBAMRENC_FRAME_SIZE_13;
394    pComponentPrivate->amrMimeBytes[1]  = NBAMRENC_FRAME_SIZE_14;
395    pComponentPrivate->amrMimeBytes[2]  = NBAMRENC_FRAME_SIZE_16;
396    pComponentPrivate->amrMimeBytes[3]  = NBAMRENC_FRAME_SIZE_18;
397    pComponentPrivate->amrMimeBytes[4]  = NBAMRENC_FRAME_SIZE_20;
398    pComponentPrivate->amrMimeBytes[5]  = NBAMRENC_FRAME_SIZE_21;
399    pComponentPrivate->amrMimeBytes[6]  = NBAMRENC_FRAME_SIZE_27;
400    pComponentPrivate->amrMimeBytes[7]  = NBAMRENC_FRAME_SIZE_32;
401    pComponentPrivate->amrMimeBytes[8]  = NBAMRENC_FRAME_SIZE_6;
402    pComponentPrivate->amrMimeBytes[9]  = NBAMRENC_FRAME_SIZE_0;
403    pComponentPrivate->amrMimeBytes[10] = NBAMRENC_FRAME_SIZE_0;
404    pComponentPrivate->amrMimeBytes[11] = NBAMRENC_FRAME_SIZE_0;
405    pComponentPrivate->amrMimeBytes[12] = NBAMRENC_FRAME_SIZE_0;
406    pComponentPrivate->amrMimeBytes[13] = NBAMRENC_FRAME_SIZE_0;
407    pComponentPrivate->amrMimeBytes[14] = NBAMRENC_FRAME_SIZE_0;
408    pComponentPrivate->amrMimeBytes[15] = NBAMRENC_FRAME_SIZE_1;
409
410    pComponentPrivate->amrIf2Bytes[0]  = NBAMRENC_FRAME_SIZE_13;
411    pComponentPrivate->amrIf2Bytes[1]  = NBAMRENC_FRAME_SIZE_14;
412    pComponentPrivate->amrIf2Bytes[2]  = NBAMRENC_FRAME_SIZE_16;
413    pComponentPrivate->amrIf2Bytes[3]  = NBAMRENC_FRAME_SIZE_18;
414    pComponentPrivate->amrIf2Bytes[4]  = NBAMRENC_FRAME_SIZE_19;
415    pComponentPrivate->amrIf2Bytes[5]  = NBAMRENC_FRAME_SIZE_21;
416    pComponentPrivate->amrIf2Bytes[6]  = NBAMRENC_FRAME_SIZE_26;
417    pComponentPrivate->amrIf2Bytes[7]  = NBAMRENC_FRAME_SIZE_31;
418    pComponentPrivate->amrIf2Bytes[8]  = NBAMRENC_FRAME_SIZE_6;
419    pComponentPrivate->amrIf2Bytes[9]  = NBAMRENC_FRAME_SIZE_0;
420    pComponentPrivate->amrIf2Bytes[10] = NBAMRENC_FRAME_SIZE_0;
421    pComponentPrivate->amrIf2Bytes[11] = NBAMRENC_FRAME_SIZE_0;
422    pComponentPrivate->amrIf2Bytes[12] = NBAMRENC_FRAME_SIZE_0;
423    pComponentPrivate->amrIf2Bytes[13] = NBAMRENC_FRAME_SIZE_0;
424    pComponentPrivate->amrIf2Bytes[14] = NBAMRENC_FRAME_SIZE_0;
425    pComponentPrivate->amrIf2Bytes[15] = NBAMRENC_FRAME_SIZE_1;
426
427    pComponentPrivate->pMarkBuf = NULL;
428    pComponentPrivate->pMarkData = NULL;
429    pComponentPrivate->nEmptyBufferDoneCount = 0;
430    pComponentPrivate->nEmptyThisBufferCount = 0;
431    pComponentPrivate->nFillBufferDoneCount = 0;
432    pComponentPrivate->nFillThisBufferCount = 0;
433    pComponentPrivate->strmAttr = NULL;
434    pComponentPrivate->bDisableCommandParam = 0;
435    pComponentPrivate->bEnableCommandParam = 0;
436    pComponentPrivate->iHoldLen = 0;
437    pComponentPrivate->iHoldBuffer = NULL;
438    pComponentPrivate->pHoldBuffer = NULL;
439    pComponentPrivate->nHoldLength = 0;
440    pComponentPrivate->bFirstInputBufReceived = OMX_FALSE;
441    pComponentPrivate->TimeStamp = 0;
442
443    pComponentPrivate->nUnhandledFillThisBuffers=0;
444    pComponentPrivate->nUnhandledEmptyThisBuffers = 0;
445
446    pComponentPrivate->bFlushOutputPortCommandPending = OMX_FALSE;
447    pComponentPrivate->bFlushInputPortCommandPending = OMX_FALSE;
448
449    for (i=0; i < NBAMRENC_MAX_NUM_OF_BUFS; i++) {
450        pComponentPrivate->pInputBufHdrPending[i] = NULL;
451        pComponentPrivate->pOutputBufHdrPending[i] = NULL;
452        pComponentPrivate->arrTickCount[i] = 0;
453        pComponentPrivate->arrBufIndex[i] = 0;
454    }
455    pComponentPrivate->nNumInputBufPending = 0;
456    pComponentPrivate->nNumOutputBufPending = 0;
457    pComponentPrivate->bDisableCommandPending = 0;
458    pComponentPrivate->bEnableCommandPending = 0;
459    pComponentPrivate->bNoIdleOnStop= OMX_FALSE;
460
461    pComponentPrivate->bDspStoppedWhileExecuting    = OMX_FALSE;
462
463    pComponentPrivate->nOutStandingFillDones = 0;
464    pComponentPrivate->nRuntimeInputBuffers=0;
465    pComponentPrivate->nRuntimeOutputBuffers=0;
466    pComponentPrivate->nNumOfFramesSent=0;
467    pComponentPrivate->ptrLibLCML = NULL;
468    pComponentPrivate->IpBufindex = 0;
469    pComponentPrivate->OpBufindex = 0;
470    pComponentPrivate->nOutStandingEmptyDones = 0;
471
472    pComponentPrivate->bLoadedWaitingFreeBuffers    = OMX_FALSE;
473    pComponentPrivate->ProcessingInputBuf = 0;
474    pComponentPrivate->ProcessingOutputBuf = 0;
475
476    OMX_MALLOC_SIZE(pComponentPrivate->sDeviceString, 100*sizeof(OMX_STRING),OMX_STRING);
477
478    /* Initialize device string to the default value */
479    strcpy((char*)pComponentPrivate->sDeviceString,":srcul/codec\0");
480
481#ifndef UNDER_CE
482    pthread_mutex_init(&pComponentPrivate->AlloBuf_mutex, NULL);
483    pthread_cond_init (&pComponentPrivate->AlloBuf_threshold, NULL);
484    pComponentPrivate->AlloBuf_waitingsignal = 0;
485
486    pthread_mutex_init(&pComponentPrivate->codecStop_mutex, NULL);
487    pthread_cond_init (&pComponentPrivate->codecStop_threshold, NULL);
488    pComponentPrivate->codecStop_waitingsignal = 0;
489
490    pthread_mutex_init(&pComponentPrivate->InLoaded_mutex, NULL);
491    pthread_cond_init (&pComponentPrivate->InLoaded_threshold, NULL);
492    pComponentPrivate->InLoaded_readytoidle = 0;
493
494    pthread_mutex_init(&pComponentPrivate->InIdle_mutex, NULL);
495    pthread_cond_init (&pComponentPrivate->InIdle_threshold, NULL);
496    pComponentPrivate->InIdle_goingtoloaded = 0;
497
498    pthread_mutex_init(&pComponentPrivate->ToLoaded_mutex, NULL);
499#else
500    OMX_CreateEvent(&(pComponentPrivate->AlloBuf_event));
501    pComponentPrivate->AlloBuf_waitingsignal = 0;
502
503    OMX_CreateEvent(&(pComponentPrivate->InLoaded_event));
504    pComponentPrivate->InLoaded_readytoidle = 0;
505
506    OMX_CreateEvent(&(pComponentPrivate->InIdle_event));
507    pComponentPrivate->InIdle_goingtoloaded = 0;
508#endif
509    pComponentPrivate->bIsInvalidState = OMX_FALSE;
510
511#ifdef RESOURCE_MANAGER_ENABLED
512    eError = RMProxy_NewInitalize();
513
514    OMX_PRINT1(pComponentPrivate->dbg, "%d :: OMX_ComponentInit\n", __LINE__);
515    if (eError != OMX_ErrorNone) {
516        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error returned from loading ResourceManagerProxy thread\n",__LINE__);
517        goto EXIT;
518    }
519#endif
520
521    eError = NBAMRENC_StartComponentThread(pHandle);
522    OMX_PRINT1(pComponentPrivate->dbg, "%d :: OMX_ComponentInit\n", __LINE__);
523    if (eError != OMX_ErrorNone) {
524      OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error returned from the Component\n",__LINE__);
525      goto EXIT;
526    }
527
528
529#ifdef __PERF_INSTRUMENTATION__
530    PERF_ThreadCreated(pComponentPrivate->pPERF, pComponentPrivate->ComponentThread,
531                       PERF_FOURCC('N','B','E','T'));
532#endif
533
534#ifndef UNDER_CE
535#ifdef DSP_RENDERING_ON
536    /* start Audio Manager to get streamId */
537    if((pComponentPrivate->fdwrite=open(FIFO1,O_WRONLY))<0) {
538        OMX_ERROR4(pComponentPrivate->dbg, "%d :: [NBAMRE Encoder Component] - failure to open WRITE pipe\n",__LINE__);
539    }
540
541    if((pComponentPrivate->fdread=open(FIFO2,O_RDONLY))<0) {
542        OMX_ERROR4(pComponentPrivate->dbg, "%d :: [NBAMRE Encoder Component] - failure to open READ pipe\n",__LINE__);
543    }
544#endif
545#endif
546   if(pthread_mutex_init(&pComponentPrivate->mutexStateChangeRequest, NULL)) {
547       return OMX_ErrorUndefined;
548   }
549
550   if(pthread_cond_init (&pComponentPrivate->StateChangeCondition, NULL)) {
551       return OMX_ErrorUndefined;
552   }
553
554   pComponentPrivate->nPendingStateChangeRequests = 0;
555
556EXIT:
557    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting OMX_ComponentInit\n", __LINE__);
558    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
559    return eError;
560}
561
562/*-------------------------------------------------------------------*/
563/**
564  *  SetCallbacks() Sets application callbacks to the component
565  *
566  * This method will update application callbacks
567  * to the component. So that component can make use of those call back
568  * while sending buffers to the application. And also it will copy the
569  * application private data to component memory
570  *
571  * @param pComponent    handle for this instance of the component
572  * @param pCallBacks    application callbacks
573  * @param pAppData      Application private data
574  *
575  * @retval OMX_NoError              Success, ready to roll
576  *         OMX_Error_BadParameter   The input parameter pointer is null
577  **/
578/*-------------------------------------------------------------------*/
579
580static OMX_ERRORTYPE SetCallbacks (OMX_HANDLETYPE pComponent,
581                                   OMX_CALLBACKTYPE* pCallBacks,
582                                   OMX_PTR pAppData)
583{
584    OMX_ERRORTYPE eError = OMX_ErrorNone;
585
586    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE*)pComponent;
587
588    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
589                    (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
590    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering SetCallbacks\n", __LINE__);
591    if (pCallBacks == NULL) {
592        eError = OMX_ErrorBadParameter;
593        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Received the empty callbacks from the application\n",__LINE__);
594        goto EXIT;
595    }
596
597    /*Copy the callbacks of the application to the component private*/
598    memcpy (&(pComponentPrivate->cbInfo), pCallBacks, sizeof(OMX_CALLBACKTYPE));
599
600    /*copy the application private data to component memory */
601    pHandle->pApplicationPrivate = pAppData;
602
603    pComponentPrivate->curState = OMX_StateLoaded;
604EXIT:
605    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting SetCallbacks\n", __LINE__);
606    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
607    return eError;
608}
609
610/*-------------------------------------------------------------------*/
611/**
612  *  GetComponentVersion() This will return the component version
613  *
614  * This method will retrun the component version
615  *
616  * @param hComp               handle for this instance of the component
617  * @param pCompnentName       Name of the component
618  * @param pCompnentVersion    handle for this instance of the component
619  * @param pSpecVersion        application callbacks
620  * @param pCompnentUUID
621  *
622  * @retval OMX_NoError              Success, ready to roll
623  *         OMX_Error_BadParameter   The input parameter pointer is null
624  **/
625/*-------------------------------------------------------------------*/
626
627static OMX_ERRORTYPE GetComponentVersion (OMX_HANDLETYPE hComp,
628                                          OMX_STRING pComponentName,
629                                          OMX_VERSIONTYPE* pComponentVersion,
630                                          OMX_VERSIONTYPE* pSpecVersion,
631                                          OMX_UUIDTYPE* pComponentUUID)
632{
633    OMX_ERRORTYPE eError = OMX_ErrorNone;
634
635    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE*) hComp;
636    AMRENC_COMPONENT_PRIVATE *pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *) pHandle->pComponentPrivate;
637    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering GetComponentVersion\n", __LINE__);
638
639#ifdef _ERROR_PROPAGATION__
640    if (pComponentPrivate->curState == OMX_StateInvalid){
641        eError = OMX_ErrorInvalidState;
642        goto EXIT;
643    }
644#endif
645
646    /* Copy component version structure */
647    if(pComponentVersion != NULL && pComponentName != NULL) {
648        strcpy(pComponentName, pComponentPrivate->cComponentName);
649        memcpy(pComponentVersion, &(pComponentPrivate->ComponentVersion.s), sizeof(pComponentPrivate->ComponentVersion.s));
650    }
651    else {
652        OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadParameter from GetComponentVersion",__LINE__);
653        eError = OMX_ErrorBadParameter;
654    }
655
656EXIT:
657    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting GetComponentVersion\n", __LINE__);
658    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
659    return eError;
660}
661/*-------------------------------------------------------------------*/
662/**
663  *  SendCommand() used to send the commands to the component
664  *
665  * This method will be used by the application.
666  *
667  * @param phandle         handle for this instance of the component
668  * @param Cmd             Command to be sent to the component
669  * @param nParam          indicates commmad is sent using this method
670  *
671  * @retval OMX_NoError              Success, ready to roll
672  *         OMX_Error_BadParameter   The input parameter pointer is null
673  **/
674/*-------------------------------------------------------------------*/
675
676static OMX_ERRORTYPE SendCommand (OMX_HANDLETYPE phandle,
677                                  OMX_COMMANDTYPE Cmd,
678                                  OMX_U32 nParam,
679                                  OMX_PTR pCmdData)
680{
681    OMX_ERRORTYPE eError = OMX_ErrorNone;
682    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)phandle;
683    AMRENC_COMPONENT_PRIVATE *pCompPrivate =
684             (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
685    int nRet = 0;
686
687#ifdef _ERROR_PROPAGATION__
688    if (pCompPrivate->curState == OMX_StateInvalid){
689        eError = OMX_ErrorInvalidState;
690        goto EXIT;
691    }
692#else
693    OMX_PRINT1(pCompPrivate->dbg, "%d :: Entering SendCommand()\n", __LINE__);
694    if(pCompPrivate->curState == OMX_StateInvalid) {
695        eError = OMX_ErrorInvalidState;
696        OMX_ERROR4(pCompPrivate->dbg, "%d :: Error OMX_ErrorInvalidState Sent to App\n",__LINE__);
697        pCompPrivate->cbInfo.EventHandler (pHandle,
698                                            pHandle->pApplicationPrivate,
699                                            OMX_EventError,
700                                            OMX_ErrorInvalidState,
701                                            OMX_TI_ErrorMinor,
702                                            "Invalid State");
703        goto EXIT;
704    }
705#endif
706#ifdef __PERF_INSTRUMENTATION__
707    PERF_SendingCommand(pCompPrivate->pPERF,
708                        Cmd,
709                        (Cmd == OMX_CommandMarkBuffer) ? ((OMX_U32) pCmdData) : nParam,
710                        PERF_ModuleComponent);
711#endif
712    switch(Cmd) {
713        case OMX_CommandStateSet:
714            OMX_PRINT1(pCompPrivate->dbg, "%d :: OMX_CommandStateSet SendCommand\n",__LINE__);
715            if (nParam == OMX_StateLoaded) {
716                pCompPrivate->bLoadedCommandPending = OMX_TRUE;
717            }
718            if(pCompPrivate->curState == OMX_StateLoaded) {
719                if((nParam == OMX_StateExecuting) || (nParam == OMX_StatePause)) {
720                    pCompPrivate->cbInfo.EventHandler ( pHandle,
721                                                        pHandle->pApplicationPrivate,
722                                                        OMX_EventError,
723                                                        OMX_ErrorIncorrectStateTransition,
724                                                        OMX_TI_ErrorMinor,
725                                                        NULL);
726                    goto EXIT;
727                }
728
729                if(nParam == OMX_StateInvalid) {
730                    OMX_PRINT1(pCompPrivate->dbg, "%d :: OMX_CommandStateSet SendCommand\n",__LINE__);
731                    pCompPrivate->curState = OMX_StateInvalid;
732                    pCompPrivate->cbInfo.EventHandler ( pHandle,
733                                                        pHandle->pApplicationPrivate,
734                                                        OMX_EventError,
735                                                        OMX_ErrorInvalidState,
736                                                        OMX_TI_ErrorMinor,
737                                                        NULL);
738                    goto EXIT;
739                }
740            }
741
742            /* Add a pending transition */
743            if(AddStateTransition(pCompPrivate) != OMX_ErrorNone) {
744               return OMX_ErrorUndefined;
745            }
746            break;
747
748        case OMX_CommandFlush:
749            OMX_PRINT1(pCompPrivate->dbg, "%d :: OMX_CommandFlush SendCommand\n",__LINE__);
750            if(nParam > 1 && nParam != -1) {
751                eError = OMX_ErrorBadPortIndex;
752                OMX_ERROR4(pCompPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from SendCommand",__LINE__);
753                goto EXIT;
754            }
755            break;
756        case OMX_CommandPortDisable:
757            OMX_PRINT1(pCompPrivate->dbg, "%d :: OMX_CommandPortDisable SendCommand\n",__LINE__);
758            break;
759        case OMX_CommandPortEnable:
760            OMX_PRINT1(pCompPrivate->dbg, "%d :: OMX_CommandPortEnable SendCommand\n",__LINE__);
761            break;
762        case OMX_CommandMarkBuffer:
763            OMX_PRINT1(pCompPrivate->dbg, "%d :: OMX_CommandMarkBuffer SendCommand\n",__LINE__);
764            if (nParam > 0) {
765                eError = OMX_ErrorBadPortIndex;
766                OMX_ERROR4(pCompPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from SendCommand",__LINE__);
767                goto EXIT;
768            }
769            break;
770        default:
771            OMX_ERROR4(pCompPrivate->dbg, "%d :: Command Received Default eError\n",__LINE__);
772            pCompPrivate->cbInfo.EventHandler ( pHandle,
773                                                pHandle->pApplicationPrivate,
774                                                OMX_EventError,
775                                                OMX_ErrorUndefined,
776                                                OMX_TI_ErrorMinor,
777                                                "Invalid Command");
778            break;
779
780    }
781
782    nRet = write (pCompPrivate->cmdPipe[1], &Cmd, sizeof(Cmd));
783    if (nRet == -1) {
784       eError = OMX_ErrorInsufficientResources;
785       OMX_ERROR4(pCompPrivate->dbg, "%d :: OMX_ErrorInsufficientResources from SendCommand",__LINE__);
786       goto EXIT;
787    }
788
789    if (Cmd == OMX_CommandMarkBuffer) {
790        nRet = write(pCompPrivate->cmdDataPipe[1], &pCmdData,
791                            sizeof(OMX_PTR));
792    } else {
793        nRet = write(pCompPrivate->cmdDataPipe[1], &nParam,
794                            sizeof(OMX_U32));
795    }
796    if (nRet == -1) {
797        OMX_ERROR4(pCompPrivate->dbg, "%d :: OMX_ErrorInsufficientResources from SendCommand",__LINE__);
798        if(Cmd == OMX_CommandStateSet) {
799           if(RemoveStateTransition(pCompPrivate, OMX_FALSE) != OMX_ErrorNone) {
800              return OMX_ErrorUndefined;
801           }
802        }
803        eError = OMX_ErrorInsufficientResources;
804        goto EXIT;
805    }
806
807#ifdef DSP_RENDERING_ON
808
809
810        /* add for Tee device control*/
811    if(Cmd == OMX_CommandStateSet && nParam == OMX_StateExecuting) {
812        if(pCompPrivate->teeMode != TEEMODE_NONE) {
813            /* enable acoustic supporting*/
814            OMX_PRINT2(pCompPrivate->dbg, "[NBAMR encoder] --- supporting TeeDN\n");
815            OMX_PRINT2(pCompPrivate->dbg, "[NBAMR encoder] --- Send Tee commnad\n");
816
817            /* enable Tee device command*/
818            cmd_data.hComponent = pHandle;
819            /*
820            AM_CommandTDNPlayMode           --- play mode only
821            AM_CommandTDNLoopBackMode       --- loopback mode pnly
822            AM_CommandTDNPlayLoopBackMode   --- loopback + playback mode
823            */
824            if (pCompPrivate->teeMode == TEEMODE_PLAYBACK) {
825                cmd_data.AM_Cmd = AM_CommandTDNPlayMode ;
826            }
827            else if (pCompPrivate->teeMode == TEEMODE_LOOPBACK) {
828
829                cmd_data.AM_Cmd = AM_CommandTDNLoopBackMode ;
830            }
831            else if (pCompPrivate->teeMode == TEEMODE_PLAYLOOPBACK) {
832                cmd_data.AM_Cmd = AM_CommandTDNPlayLoopBackMode ;
833            }
834            cmd_data.param1 = 0;
835            cmd_data.param2 = 0;
836            cmd_data.streamID = 0;
837            if((write(pCompPrivate->fdwrite, &cmd_data, sizeof(cmd_data)))<0) {
838                OMX_TRACE4(pCompPrivate->dbg, "[NBAMR encoder] - fail to send Tee command to audio manager\n");
839                eError = OMX_ErrorHardware;
840                goto EXIT;
841            }
842        }
843        else {
844            OMX_PRDSP2(pCompPrivate->dbg, "[NBAMR encoder] --- Normal DASF Mode\n");
845        }
846    }
847#endif
848
849
850EXIT:
851    OMX_PRINT1(pCompPrivate->dbg, "%d :: Exiting SendCommand()\n", __LINE__);
852    OMX_PRINT1(pCompPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
853    return eError;
854}
855/*-------------------------------------------------------------------*/
856/**
857  *  GetParameter() Gets the current configurations of the component
858  *
859  * @param hComp         handle for this instance of the component
860  * @param nParamIndex
861  * @param ComponentParameterStructure
862  *
863  * @retval OMX_NoError              Success, ready to roll
864  *         OMX_Error_BadParameter   The input parameter pointer is null
865  **/
866/*-------------------------------------------------------------------*/
867
868static OMX_ERRORTYPE GetParameter (OMX_HANDLETYPE hComp,
869                                   OMX_INDEXTYPE nParamIndex,
870                                   OMX_PTR ComponentParameterStructure)
871{
872    OMX_ERRORTYPE eError = OMX_ErrorNone;
873    AMRENC_COMPONENT_PRIVATE  *pComponentPrivate;
874    OMX_PARAM_PORTDEFINITIONTYPE *pParameterStructure;
875
876    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)(((OMX_COMPONENTTYPE*)hComp)->pComponentPrivate);
877    pParameterStructure = (OMX_PARAM_PORTDEFINITIONTYPE*)ComponentParameterStructure;
878    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering the GetParameter\n",__LINE__);
879    if (pParameterStructure == NULL) {
880        eError = OMX_ErrorBadParameter;
881        OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from GetParameter",__LINE__);
882        goto EXIT;
883    }
884
885#ifdef _ERROR_PROPAGATION__
886    if (pComponentPrivate->curState == OMX_StateInvalid){
887        eError = OMX_ErrorInvalidState;
888        goto EXIT;
889    }
890#else
891    if(pComponentPrivate->curState == OMX_StateInvalid) {
892        eError = OMX_ErrorIncorrectStateOperation;
893        OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorIncorrectStateOperation from GetParameter",__LINE__);
894        goto EXIT;
895    }
896#endif
897    switch(nParamIndex){
898        case OMX_IndexParamAudioInit:
899            if (pComponentPrivate->sPortParam == NULL) {
900                eError = OMX_ErrorBadParameter;
901		break;
902	    }
903            OMX_PRDSP2(pComponentPrivate->dbg, "%d :: GetParameter OMX_IndexParamAudioInit \n",__LINE__);
904            memcpy(ComponentParameterStructure, pComponentPrivate->sPortParam, sizeof(OMX_PORT_PARAM_TYPE));
905            break;
906
907        case OMX_IndexParamPortDefinition:
908
909            OMX_PRDSP2(pComponentPrivate->dbg, "%d :: GetParameter OMX_IndexParamPortDefinition \n",__LINE__);
910            if(((OMX_PARAM_PORTDEFINITIONTYPE *)(ComponentParameterStructure))->nPortIndex ==
911                pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nPortIndex) {
912                    memcpy(ComponentParameterStructure, pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT], sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
913            }
914            else if(((OMX_PARAM_PORTDEFINITIONTYPE *)(ComponentParameterStructure))->nPortIndex ==
915                                      pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nPortIndex) {
916                memcpy(ComponentParameterStructure, pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT], sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
917            }
918            else {
919                OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from GetParameter \n",__LINE__);
920                eError = OMX_ErrorBadPortIndex;
921            }
922            break;
923
924        case OMX_IndexParamAudioPortFormat:
925
926            OMX_PRDSP2(pComponentPrivate->dbg, "%d :: GetParameter OMX_IndexParamAudioPortFormat \n",__LINE__);
927            if(((OMX_AUDIO_PARAM_PORTFORMATTYPE *)(ComponentParameterStructure))->nPortIndex ==
928                pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nPortIndex) {
929                    if(((OMX_AUDIO_PARAM_PORTFORMATTYPE *)(ComponentParameterStructure))->nIndex >
930                      pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]->pPortFormat->nPortIndex) {
931                        eError = OMX_ErrorNoMore;
932                    }
933                    else {
934                        memcpy(ComponentParameterStructure, pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]->pPortFormat, sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE));
935                    }
936                }
937                else if(((OMX_AUDIO_PARAM_PORTFORMATTYPE *)(ComponentParameterStructure))->nPortIndex ==
938                            pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nPortIndex){
939                    if(((OMX_AUDIO_PARAM_PORTFORMATTYPE *)(ComponentParameterStructure))->nIndex >
940                                                 pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]->pPortFormat->nPortIndex) {
941                        eError = OMX_ErrorNoMore;
942                    }
943                    else {
944                        memcpy(ComponentParameterStructure, pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]->pPortFormat, sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE));
945                    }
946                }
947                else {
948                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from GetParameter \n",__LINE__);
949                    eError = OMX_ErrorBadPortIndex;
950                }
951            break;
952
953        case OMX_IndexParamAudioAmr:
954            if (pComponentPrivate->amrParams == NULL) {
955                eError = OMX_ErrorBadParameter;
956	        break;
957	    }
958            memcpy(ComponentParameterStructure, pComponentPrivate->amrParams, sizeof(OMX_AUDIO_PARAM_AMRTYPE));
959            switch (((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode)
960            {
961                 case SN_AUDIO_BR122:
962                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode =   OMX_AUDIO_AMRBandModeNB7;
963                      break;
964                 case SN_AUDIO_BR102:
965                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode =   OMX_AUDIO_AMRBandModeNB6;
966                      break;
967                 case SN_AUDIO_BR795:
968                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode =   OMX_AUDIO_AMRBandModeNB5;
969                      break;
970                 case SN_AUDIO_BR74:
971                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode =   OMX_AUDIO_AMRBandModeNB4;
972                      break;
973                 case SN_AUDIO_BR67:
974                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode =   OMX_AUDIO_AMRBandModeNB3;
975                      break;
976                 case SN_AUDIO_BR59:
977                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode =   OMX_AUDIO_AMRBandModeNB2;
978                      break;
979                 case SN_AUDIO_BR515:
980                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode =   OMX_AUDIO_AMRBandModeNB1;
981                      break;
982                 case SN_AUDIO_475:
983                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode =   OMX_AUDIO_AMRBandModeNB0;
984                      break;
985                 default:
986                      ((OMX_AUDIO_PARAM_AMRTYPE *)(ComponentParameterStructure))->eAMRBandMode = OMX_AUDIO_AMRBandModeNB0;
987                      break;
988            }
989            break;
990
991      case OMX_IndexParamPriorityMgmt:
992            if (pComponentPrivate->sPriorityMgmt == NULL) {
993                eError = OMX_ErrorBadParameter;
994	        break;
995	    }
996            OMX_PRDSP2(pComponentPrivate->dbg, "%d :: GetParameter OMX_IndexParamPriorityMgmt \n",__LINE__);
997            memcpy(ComponentParameterStructure, pComponentPrivate->sPriorityMgmt, sizeof(OMX_PRIORITYMGMTTYPE));
998            break;
999
1000      case OMX_IndexParamAudioPcm:
1001          if (pComponentPrivate->pcmParams == NULL) {
1002              eError = OMX_ErrorBadParameter;
1003	      break;
1004	  }
1005          memcpy(ComponentParameterStructure, pComponentPrivate->pcmParams, sizeof(OMX_AUDIO_PARAM_PCMMODETYPE));
1006          break;
1007
1008       case OMX_IndexParamCompBufferSupplier:
1009     if(((OMX_PARAM_BUFFERSUPPLIERTYPE *)(ComponentParameterStructure))->nPortIndex == OMX_DirInput) {
1010                    OMX_PRDSP2(pComponentPrivate->dbg, ":: GetParameter OMX_IndexParamCompBufferSupplier \n");
1011                    /*  memcpy(ComponentParameterStructure, pBufferSupplier, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE)); */
1012                }
1013                else if(((OMX_PARAM_BUFFERSUPPLIERTYPE *)(ComponentParameterStructure))->nPortIndex == OMX_DirOutput) {
1014                    OMX_PRBUFFER1(pComponentPrivate->dbg, ":: GetParameter OMX_IndexParamCompBufferSupplier \n");
1015                    /*memcpy(ComponentParameterStructure, pBufferSupplier, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE)); */
1016                }
1017                else {
1018                    OMX_ERROR1(pComponentPrivate->dbg, ":: OMX_ErrorBadPortIndex from GetParameter");
1019                    eError = OMX_ErrorBadPortIndex;
1020                }
1021                break;
1022
1023#ifdef ANDROID
1024    case (OMX_INDEXTYPE) PV_OMX_COMPONENT_CAPABILITY_TYPE_INDEX:
1025    {
1026    OMX_PRINT1(pComponentPrivate->dbg, "Entering PV_OMX_COMPONENT_CAPABILITY_TYPE_INDEX::%d\n", __LINE__);
1027        PV_OMXComponentCapabilityFlagsType* pCap_flags = (PV_OMXComponentCapabilityFlagsType *) ComponentParameterStructure;
1028        if (NULL == pCap_flags)
1029        {
1030            OMX_ERROR4(pComponentPrivate->dbg, "%d :: ERROR PV_OMX_COMPONENT_CAPABILITY_TYPE_INDEX\n", __LINE__);
1031            eError =  OMX_ErrorBadParameter;
1032            goto EXIT;
1033        }
1034        OMX_PRINT2(pComponentPrivate->dbg, "%d :: Copying PV_OMX_COMPONENT_CAPABILITY_TYPE_INDEX\n", __LINE__);
1035        memcpy(pCap_flags, &(pComponentPrivate->iPVCapabilityFlags), sizeof(PV_OMXComponentCapabilityFlagsType));
1036    eError = OMX_ErrorNone;
1037    }
1038    break;
1039#endif
1040
1041        case OMX_IndexParamVideoInit:
1042                break;
1043
1044         case OMX_IndexParamImageInit:
1045                break;
1046
1047         case OMX_IndexParamOtherInit:
1048                break;
1049
1050        default:
1051            OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorUnsupportedIndex GetParameter \n",__LINE__);
1052            eError = OMX_ErrorUnsupportedIndex;
1053            break;
1054    }
1055EXIT:
1056    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting GetParameter\n",__LINE__);
1057    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1058    return eError;
1059}
1060/*-------------------------------------------------------------------*/
1061/**
1062  *  SetParameter() Sets configuration paramets to the component
1063  *
1064  * @param hComp         handle for this instance of the component
1065  * @param nParamIndex
1066  * @param pCompParam
1067  *
1068  * @retval OMX_NoError              Success, ready to roll
1069  *         OMX_Error_BadParameter   The input parameter pointer is null
1070  **/
1071/*-------------------------------------------------------------------*/
1072
1073static OMX_ERRORTYPE SetParameter (OMX_HANDLETYPE hComp,
1074                                   OMX_INDEXTYPE nParamIndex,
1075                                   OMX_PTR pCompParam)
1076{
1077    OMX_ERRORTYPE eError = OMX_ErrorNone;
1078    OMX_COMPONENTTYPE* pHandle= (OMX_COMPONENTTYPE*)hComp;
1079    AMRENC_COMPONENT_PRIVATE  *pComponentPrivate;
1080    OMX_AUDIO_PARAM_PORTFORMATTYPE* pComponentParam = NULL;
1081    OMX_PARAM_PORTDEFINITIONTYPE *pComponentParamPort = NULL;
1082    OMX_AUDIO_PARAM_AMRTYPE *pCompAmrParam = NULL;
1083    OMX_PARAM_COMPONENTROLETYPE  *pRole;
1084    OMX_AUDIO_PARAM_PCMMODETYPE *amr_ip;
1085    OMX_PARAM_BUFFERSUPPLIERTYPE sBufferSupplier;
1086
1087    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)(((OMX_COMPONENTTYPE*)hComp)->pComponentPrivate);
1088
1089
1090    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering the SetParameter\n",__LINE__);
1091    if (pCompParam == NULL) {
1092        eError = OMX_ErrorBadParameter;
1093        OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadParameter from SetParameter",__LINE__);
1094        goto EXIT;
1095    }
1096    if (pComponentPrivate->curState != OMX_StateLoaded) {
1097        eError = OMX_ErrorIncorrectStateOperation;
1098        OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorIncorrectStateOperation from SetParameter",__LINE__);
1099        goto EXIT;
1100    }
1101#ifdef _ERROR_PROPAGATION__
1102    if (pComponentPrivate->curState == OMX_StateInvalid){
1103        eError = OMX_ErrorInvalidState;
1104        goto EXIT;
1105    }
1106#endif
1107    switch(nParamIndex) {
1108        case OMX_IndexParamAudioPortFormat:
1109                OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioPortFormat \n",__LINE__);
1110                pComponentParam = (OMX_AUDIO_PARAM_PORTFORMATTYPE *)pCompParam;
1111                if ( pComponentParam->nPortIndex == pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]->pPortFormat->nPortIndex ) {
1112                    memcpy(pComponentPrivate->pCompPort[NBAMRENC_INPUT_PORT]->pPortFormat, pComponentParam, sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE));
1113                } else if ( pComponentParam->nPortIndex == pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]->pPortFormat->nPortIndex ) {
1114                    memcpy(pComponentPrivate->pCompPort[NBAMRENC_OUTPUT_PORT]->pPortFormat, pComponentParam, sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE));
1115                } else {
1116                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from SetParameter",__LINE__);
1117                    eError = OMX_ErrorBadPortIndex;
1118                }
1119            break;
1120
1121        case OMX_IndexParamAudioAmr:
1122
1123                OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr \n",__LINE__);
1124                pCompAmrParam = (OMX_AUDIO_PARAM_AMRTYPE *)pCompParam;
1125                if(pCompAmrParam->nPortIndex == 0) {         /* 0 means Input port */
1126                    memcpy(((AMRENC_COMPONENT_PRIVATE*)
1127                            pHandle->pComponentPrivate)->pcmParams, pCompAmrParam, sizeof(OMX_AUDIO_PARAM_AMRTYPE));
1128                } else if (pCompAmrParam->nPortIndex == 1) { /* 1 means Output port */
1129                    if (((AMRENC_COMPONENT_PRIVATE *) pHandle->pComponentPrivate)->amrParams == NULL) {
1130                        eError = OMX_ErrorBadParameter;
1131                        goto EXIT;
1132                    }
1133
1134                    memcpy(((AMRENC_COMPONENT_PRIVATE *)
1135                            pHandle->pComponentPrivate)->amrParams, pCompAmrParam, sizeof(OMX_AUDIO_PARAM_AMRTYPE));
1136
1137                    switch (pCompAmrParam->eAMRBandMode)
1138                    {
1139                             case OMX_AUDIO_AMRBandModeNB7:
1140                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_BR122;
1141                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR122 \n",__LINE__);
1142                                  break;
1143                             case OMX_AUDIO_AMRBandModeNB6:
1144                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_BR102;
1145                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR102 \n",__LINE__);
1146                                  break;
1147                             case OMX_AUDIO_AMRBandModeNB5:
1148                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_BR795;
1149                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR795 \n",__LINE__);
1150                                  break;
1151                             case OMX_AUDIO_AMRBandModeNB4:
1152                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_BR74;
1153                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR74 \n",__LINE__);
1154                                  break;
1155                             case OMX_AUDIO_AMRBandModeNB3:
1156                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_BR67;
1157                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR67 \n",__LINE__);
1158                                  break;
1159                             case OMX_AUDIO_AMRBandModeNB2:
1160                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_BR59;
1161                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR59 \n",__LINE__);
1162                                  break;
1163                             case OMX_AUDIO_AMRBandModeNB1:
1164                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_BR515;
1165                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR515 \n",__LINE__);
1166                                  break;
1167                             case OMX_AUDIO_AMRBandModeNB0:
1168                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_475;
1169                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR475 \n",__LINE__);
1170                                  break;
1171                             default:
1172                                  pComponentPrivate->amrParams->eAMRBandMode = SN_AUDIO_BR122;
1173                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode =DEFAULT!! SN_AUDIO_BR122 \n",__LINE__);
1174                                  break;
1175                    }
1176                    if (pCompAmrParam->eAMRFrameFormat == OMX_AUDIO_AMRFrameFormatConformance) {
1177                        pComponentPrivate->frameMode = NBAMRENC_FORMATCONFORMANCE;
1178                    }
1179                    else if((pCompAmrParam->eAMRFrameFormat == OMX_AUDIO_AMRFrameFormatFSF) ||
1180                            (pCompAmrParam->eAMRFrameFormat == OMX_AUDIO_AMRFrameFormatRTPPayload)){
1181                            pComponentPrivate->frameMode = NBAMRENC_MIMEMODE;
1182                            OMX_PRDSP1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRFrameFormate = FSF \n",__LINE__);
1183                    }
1184                    else {
1185                        pComponentPrivate->frameMode = NBAMRENC_IF2;
1186                        OMX_PRDSP1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRFrameFormate = IF2 \n",__LINE__);
1187                    }
1188                    if(pCompAmrParam->eAMRDTXMode == OMX_AUDIO_AMRDTXasEFR) {
1189                        OMX_PRDSP1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRDTXMode = XasEFR\n",__LINE__);
1190                        /*     pComponentPrivate->efrMode = 1; */
1191                    }
1192                }
1193                else {
1194                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from SetParameter",__LINE__);
1195                    eError = OMX_ErrorBadPortIndex;
1196                }
1197            break;
1198
1199        case OMX_IndexParamPortDefinition:
1200                pComponentParamPort = (OMX_PARAM_PORTDEFINITIONTYPE *)pCompParam;
1201                OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamPortDefinition \n",__LINE__);
1202                if(((OMX_PARAM_PORTDEFINITIONTYPE *)(pCompParam))->nPortIndex ==
1203                                    pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nPortIndex) {
1204                    OMX_PRINT1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamPortDefinition \n",__LINE__);
1205                    memcpy(pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT], pCompParam, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1206                }
1207                else if(((OMX_PARAM_PORTDEFINITIONTYPE *)(pCompParam))->nPortIndex ==
1208                                  pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nPortIndex) {
1209                    OMX_PRINT1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamPortDefinition \n",__LINE__);
1210                    memcpy(pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT], pCompParam, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1211                }
1212                else {
1213                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from SetParameter",__LINE__);
1214                    eError = OMX_ErrorBadPortIndex;
1215                }
1216            break;
1217
1218        case OMX_IndexParamPriorityMgmt:
1219	        if (pComponentPrivate->sPriorityMgmt == NULL) {
1220                    eError = OMX_ErrorBadParameter;
1221		    break;
1222		}
1223                OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamPriorityMgmt \n",__LINE__);
1224                memcpy(pComponentPrivate->sPriorityMgmt, (OMX_PRIORITYMGMTTYPE*)pCompParam, sizeof(OMX_PRIORITYMGMTTYPE));
1225            break;
1226
1227        case OMX_IndexParamAudioInit:
1228	        if (pComponentPrivate->sPortParam == NULL) {
1229                    eError = OMX_ErrorBadParameter;
1230		    break;
1231		}
1232                OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioInit \n",__LINE__);
1233                memcpy(pComponentPrivate->sPortParam, (OMX_PORT_PARAM_TYPE*)pCompParam, sizeof(OMX_PORT_PARAM_TYPE));
1234            break;
1235
1236        case OMX_IndexParamStandardComponentRole: /*-----------------------------*/
1237           if (pCompParam) {
1238               pRole = (OMX_PARAM_COMPONENTROLETYPE *)pCompParam;
1239               memcpy(&(pComponentPrivate->componentRole), (void *)pRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
1240           } else {
1241               eError = OMX_ErrorBadParameter;
1242           }
1243        break;
1244
1245        case OMX_IndexParamAudioPcm:
1246            if(pCompParam && (pComponentPrivate->pcmParams)){
1247                 amr_ip = (OMX_AUDIO_PARAM_PCMMODETYPE *)pCompParam;
1248                 memcpy(pComponentPrivate->pcmParams, amr_ip, sizeof(OMX_AUDIO_PARAM_PCMMODETYPE));
1249            }
1250            else{
1251                  eError = OMX_ErrorBadParameter;
1252            }
1253        break;
1254
1255        case OMX_IndexParamCompBufferSupplier:
1256            if(((OMX_PARAM_PORTDEFINITIONTYPE *)(pCompParam))->nPortIndex ==
1257                                    pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nPortIndex) {
1258                    OMX_PRBUFFER2(pComponentPrivate->dbg, ":: SetParameter OMX_IndexParamCompBufferSupplier \n");
1259                                   sBufferSupplier.eBufferSupplier = OMX_BufferSupplyInput;
1260                                   memcpy(&sBufferSupplier, pCompParam, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE));
1261
1262                }
1263                else if(((OMX_PARAM_PORTDEFINITIONTYPE *)(pCompParam))->nPortIndex ==
1264                                  pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nPortIndex) {
1265                    OMX_PRBUFFER1(pComponentPrivate->dbg, ":: SetParameter OMX_IndexParamCompBufferSupplier \n");
1266                    sBufferSupplier.eBufferSupplier = OMX_BufferSupplyOutput;
1267                    memcpy(&sBufferSupplier, pCompParam, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE));
1268                }
1269                else {
1270                    OMX_ERROR1(pComponentPrivate->dbg, ":: OMX_ErrorBadPortIndex from SetParameter");
1271                    eError = OMX_ErrorBadPortIndex;
1272                }
1273            break;
1274
1275        default:
1276                OMX_ERROR4(pComponentPrivate->dbg, "%d :: SetParameter OMX_ErrorUnsupportedIndex \n",__LINE__);
1277                eError = OMX_ErrorUnsupportedIndex;
1278            break;
1279    }
1280EXIT:
1281    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting SetParameter\n",__LINE__);
1282    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1283    return eError;
1284}
1285
1286/*-------------------------------------------------------------------*/
1287/**
1288  *  GetConfig() Gets the current configuration of to the component
1289  *
1290  * @param hComp         handle for this instance of the component
1291  * @param nConfigIndex
1292  * @param ComponentConfigStructure
1293  *
1294  * @retval OMX_NoError              Success, ready to roll
1295  *         OMX_Error_BadParameter   The input parameter pointer is null
1296  **/
1297/*-------------------------------------------------------------------*/
1298
1299static OMX_ERRORTYPE GetConfig (OMX_HANDLETYPE hComp,
1300                                OMX_INDEXTYPE nConfigIndex,
1301                                OMX_PTR ComponentConfigStructure)
1302{
1303    OMX_ERRORTYPE eError = OMX_ErrorNone;
1304    OMX_COMPONENTTYPE* pHandle = (OMX_COMPONENTTYPE*)hComp;
1305    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
1306                         (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1307
1308    TI_OMX_STREAM_INFO *streamInfo;
1309    OMX_MALLOC_GENERIC(streamInfo, TI_OMX_STREAM_INFO);
1310#ifdef _ERROR_PROPAGATION__
1311    if (pComponentPrivate->curState == OMX_StateInvalid){
1312        eError = OMX_ErrorInvalidState;
1313        goto EXIT;
1314    }
1315#endif
1316    if(nConfigIndex == OMX_IndexCustomNBAMRENCStreamIDConfig){
1317        streamInfo->streamId = pComponentPrivate->streamID;
1318        memcpy(ComponentConfigStructure,streamInfo,sizeof(TI_OMX_STREAM_INFO));
1319    }
1320    else if(nConfigIndex == OMX_IndexCustomDebug){
1321    OMX_DBG_GETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
1322    }
1323
1324EXIT:
1325    OMX_MEMFREE_STRUCT(streamInfo);
1326    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting GetConfig. Returning = 0x%x\n",__LINE__,eError);
1327    return eError;
1328}
1329/*-------------------------------------------------------------------*/
1330/**
1331  *  SetConfig() Sets the configraiton to the component
1332  *
1333  * @param hComp         handle for this instance of the component
1334  * @param nConfigIndex
1335  * @param ComponentConfigStructure
1336  *
1337  * @retval OMX_NoError              Success, ready to roll
1338  *         OMX_Error_BadParameter   The input parameter pointer is null
1339  **/
1340/*-------------------------------------------------------------------*/
1341
1342static OMX_ERRORTYPE SetConfig (OMX_HANDLETYPE hComp,
1343                                OMX_INDEXTYPE nConfigIndex,
1344                                OMX_PTR ComponentConfigStructure)
1345{
1346    OMX_ERRORTYPE eError = OMX_ErrorNone;
1347    AMRENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
1348    OMX_COMPONENTTYPE* pHandle = (OMX_COMPONENTTYPE*)hComp;
1349    TI_OMX_DSP_DEFINITION *pTiDspDefinition;
1350    OMX_S16 *customFlag = NULL;
1351
1352    TI_OMX_DATAPATH dataPath;
1353    OMX_AUDIO_CONFIG_VOLUMETYPE *pGainStructure = NULL;
1354#ifdef DSP_RENDERING_ON
1355    AM_COMMANDDATATYPE cmd_data;
1356#endif
1357
1358    AMRENC_DPRINT("%d :: Entering SetConfig\n", __LINE__);
1359    if (pHandle == NULL) {
1360	AMRENC_DPRINT("%d :: Invalid HANDLE OMX_ErrorBadParameter \n", __LINE__);
1361        eError = OMX_ErrorBadParameter;
1362        goto EXIT;
1363    }
1364
1365    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1366#ifdef _ERROR_PROPAGATION__
1367    if (pComponentPrivate->curState == OMX_StateInvalid){
1368        eError = OMX_ErrorInvalidState;
1369        goto EXIT;
1370    }
1371#endif
1372    switch (nConfigIndex) {
1373
1374        case OMX_IndexCustomNBAMRENCModeConfig:
1375            pTiDspDefinition = (TI_OMX_DSP_DEFINITION*)ComponentConfigStructure;
1376            if (pTiDspDefinition == NULL) {
1377                eError = OMX_ErrorBadParameter;
1378                OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadParameter from SetConfig\n",__LINE__);
1379                goto EXIT;
1380             }
1381            pComponentPrivate->acdnMode = pTiDspDefinition->acousticMode;
1382            pComponentPrivate->dasfMode = pTiDspDefinition->dasfMode;
1383            pComponentPrivate->teeMode = pTiDspDefinition->teeMode;
1384             if( 2 == pComponentPrivate->dasfMode ){
1385                   pComponentPrivate->dasfMode--;
1386             }
1387             pComponentPrivate->streamID = pTiDspDefinition->streamId;
1388
1389            break;
1390/**-------------------**/
1391        case  OMX_IndexCustomNBAMRENCDataPath:
1392            customFlag = (OMX_S16*)ComponentConfigStructure;
1393/*            dataPath = *customFlag;             */
1394            switch(dataPath) {
1395                case DATAPATH_APPLICATION:
1396                    OMX_MMMIXER_DATAPATH(pComponentPrivate->sDeviceString, RENDERTYPE_ENCODER, pComponentPrivate->streamID);
1397                    break;
1398
1399                case DATAPATH_APPLICATION_TEE:
1400                    strcpy((char*)pComponentPrivate->sDeviceString, ":i0:o1\0");
1401                    break;
1402
1403                case DATAPATH_APPLICATION_RTMIXER:
1404                    strcpy((char*)pComponentPrivate->sDeviceString,(char*)RTM_STRING_ENCODER);
1405                    break;
1406
1407                case DATAPATH_ACDN:
1408                     strcpy((char*)pComponentPrivate->sDeviceString,(char*)ACDN_STRING_ENCODER);
1409                     break;
1410                default:
1411                break;
1412
1413            }
1414        break;
1415/**-------------------**/
1416        case OMX_IndexConfigAudioVolume:
1417#ifdef DSP_RENDERING_ON
1418            pGainStructure = (OMX_AUDIO_CONFIG_VOLUMETYPE *)ComponentConfigStructure;
1419            cmd_data.hComponent = hComp;
1420            cmd_data.AM_Cmd = AM_CommandSWGain;
1421            cmd_data.param1 = pGainStructure->sVolume.nValue;
1422            cmd_data.param2 = 0;
1423            cmd_data.streamID = pComponentPrivate->streamID;
1424
1425            if((write(pComponentPrivate->fdwrite, &cmd_data, sizeof(cmd_data)))<0)
1426            {
1427                OMX_PRMGR4(pComponentPrivate->dbg, "[NBAMRENC encoder] - fail to send command to audio manager\n");
1428            }
1429            else
1430            {
1431                OMX_PRMGR2(pComponentPrivate->dbg, "[NBAMRENC encoder] - ok to send command to audio manager\n");
1432            }
1433#endif
1434            break;
1435
1436    case OMX_IndexCustomDebug:
1437        OMX_DBG_SETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
1438        break;
1439
1440    default:
1441        eError = OMX_ErrorUnsupportedIndex;
1442        break;
1443    }
1444EXIT:
1445    if (pComponentPrivate != NULL) {
1446	OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting SetConfig\n", __LINE__);
1447	OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n", __LINE__, eError);
1448    }
1449    return eError;
1450}
1451
1452/*-------------------------------------------------------------------*/
1453/**
1454  *  GetState() Gets the current state of the component
1455  *
1456  * @param pCompomponent handle for this instance of the component
1457  * @param pState
1458  *
1459  * @retval OMX_NoError              Success, ready to roll
1460  *         OMX_Error_BadParameter   The input parameter pointer is null
1461  **/
1462/*-------------------------------------------------------------------*/
1463
1464static OMX_ERRORTYPE GetState (OMX_HANDLETYPE pComponent, OMX_STATETYPE* pState)
1465{
1466    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
1467    OMX_COMPONENTTYPE* pHandle = NULL;
1468    AMRENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
1469    struct timespec abs_time = {0,0};
1470    int nPendingStateChangeRequests = 0;
1471    int ret = 0;
1472    /* Set to sufficiently high value */
1473    int mutex_timeout = 3;
1474
1475    if(pComponent == NULL || pState == NULL) {
1476        return OMX_ErrorBadParameter;
1477    }
1478
1479    pHandle = (OMX_COMPONENTTYPE*)pComponent;
1480    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
1481
1482    /* Retrieve current state */
1483    if (pHandle && pHandle->pComponentPrivate) {
1484        /* Check for any pending state transition requests */
1485        pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest);
1486        while (pComponentPrivate->nPendingStateChangeRequests != 0) {
1487           /* Wait for component to complete state transition */
1488           clock_gettime(CLOCK_REALTIME, &abs_time);
1489           abs_time.tv_sec += mutex_timeout;
1490           abs_time.tv_nsec = 0;
1491           ret = pthread_cond_timedwait(&(pComponentPrivate->StateChangeCondition),
1492                    &(pComponentPrivate->mutexStateChangeRequest), &abs_time);
1493           if (ret == ETIMEDOUT) {
1494              OMX_ERROR4(pComponentPrivate->dbg, "GetState() timeout at state %d",
1495                    pComponentPrivate->curState);
1496              *pState = OMX_StateInvalid;
1497              break;
1498           }
1499        }
1500        if (!ret) {
1501            *pState = pComponentPrivate->curState;
1502        }
1503        pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest);
1504     }
1505     else {
1506        eError = OMX_ErrorInvalidComponent;
1507        *pState = OMX_StateInvalid;
1508    }
1509
1510    return eError;
1511
1512EXIT:
1513    OMX_PRINT1 (pComponentPrivate->dbg, "%d :: AACENC: Exiting GetState\n", __LINE__);
1514    return eError;
1515
1516}
1517
1518/*-------------------------------------------------------------------*/
1519/**
1520  *  EmptyThisBuffer() This callback is used to send the input buffer to
1521  *  component
1522  *
1523  * @param pComponent       handle for this instance of the component
1524  * @param nPortIndex       input port index
1525  * @param pBuffer          buffer to be sent to codec
1526  *
1527  * @retval OMX_NoError              Success, ready to roll
1528  *         OMX_Error_BadParameter   The input parameter pointer is null
1529  **/
1530/*-------------------------------------------------------------------*/
1531
1532static OMX_ERRORTYPE EmptyThisBuffer (OMX_HANDLETYPE pComponent,
1533                                      OMX_BUFFERHEADERTYPE* pBuffer)
1534{
1535    OMX_ERRORTYPE eError = OMX_ErrorNone;
1536    int ret = 0;
1537
1538    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef;
1539
1540    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
1541    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
1542                         (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1543    pPortDef = ((AMRENC_COMPONENT_PRIVATE*)pComponentPrivate)->pPortDef[NBAMRENC_INPUT_PORT];
1544
1545#ifdef _ERROR_PROPAGATION__
1546    if (pComponentPrivate->curState == OMX_StateInvalid){
1547        eError = OMX_ErrorInvalidState;
1548        goto EXIT;
1549    }
1550#endif
1551#ifdef __PERF_INSTRUMENTATION__
1552    PERF_ReceivedFrame(pComponentPrivate->pPERF,
1553                       pBuffer->pBuffer,
1554                       pBuffer->nFilledLen,
1555                       PERF_ModuleHLMM);
1556#endif
1557
1558    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering EmptyThisBuffer\n", __LINE__);
1559    if (pBuffer == NULL) {
1560        eError = OMX_ErrorBadParameter;
1561        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorBadParameter\n",__LINE__);
1562        goto EXIT;
1563    }
1564
1565    if (pBuffer->nSize != sizeof(OMX_BUFFERHEADERTYPE)) {
1566        eError = OMX_ErrorBadParameter;
1567        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorBadParameter\n",__LINE__);
1568        goto EXIT;
1569    }
1570
1571    if (!pPortDef->bEnabled) {
1572        eError  = OMX_ErrorIncorrectStateOperation;
1573        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorIncorrectStateOperation\n",__LINE__);
1574        goto EXIT;
1575    }
1576
1577    if (pBuffer->nVersion.nVersion != pComponentPrivate->nVersion) {
1578        eError = OMX_ErrorVersionMismatch;
1579        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorVersionMismatch\n",__LINE__);
1580        goto EXIT;
1581    }
1582
1583    if (pBuffer->nInputPortIndex != NBAMRENC_INPUT_PORT) {
1584        eError  = OMX_ErrorBadPortIndex;
1585        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorBadPortIndex\n",__LINE__);
1586        goto EXIT;
1587    }
1588
1589    if (pComponentPrivate->curState != OMX_StateExecuting && pComponentPrivate->curState != OMX_StatePause) {
1590        eError= OMX_ErrorIncorrectStateOperation;
1591        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorIncorrectStateOperation\n",__LINE__);
1592        goto EXIT;
1593    }
1594
1595
1596    OMX_PRBUFFER2(pComponentPrivate->dbg, "----------------------------------------------------------------\n");
1597    OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Comp Sending Filled ip buff = %p to CompThread\n",__LINE__,pBuffer);
1598    OMX_PRBUFFER2(pComponentPrivate->dbg, "----------------------------------------------------------------\n");
1599
1600    pComponentPrivate->app_nBuf--;
1601    pComponentPrivate->ProcessingInputBuf++;
1602
1603    pComponentPrivate->pMarkData = pBuffer->pMarkData;
1604    pComponentPrivate->hMarkTargetComponent = pBuffer->hMarkTargetComponent;
1605
1606    pComponentPrivate->nUnhandledEmptyThisBuffers++;
1607    ret = write (pComponentPrivate->dataPipe[1], &pBuffer, sizeof(OMX_BUFFERHEADERTYPE*));
1608    if (ret == -1) {
1609        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error in Writing to the Data pipe\n", __LINE__);
1610        eError = OMX_ErrorHardware;
1611        goto EXIT;
1612    }
1613EXIT:
1614    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting EmptyThisBuffer\n", __LINE__);
1615    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1616    return eError;
1617}
1618/*-------------------------------------------------------------------*/
1619/**
1620  *  FillThisBuffer() This callback is used to send the output buffer to
1621  *  the component
1622  *
1623  * @param pComponent    handle for this instance of the component
1624  * @param nPortIndex    output port number
1625  * @param pBuffer       buffer to be sent to codec
1626  *
1627  * @retval OMX_NoError              Success, ready to roll
1628  *         OMX_Error_BadParameter   The input parameter pointer is null
1629  **/
1630/*-------------------------------------------------------------------*/
1631
1632static OMX_ERRORTYPE FillThisBuffer (OMX_HANDLETYPE pComponent,
1633                                     OMX_BUFFERHEADERTYPE* pBuffer)
1634{
1635    OMX_ERRORTYPE eError = OMX_ErrorNone;
1636    int ret = 0;
1637    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef;
1638    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
1639    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
1640                         (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1641    pPortDef = ((AMRENC_COMPONENT_PRIVATE*)pComponentPrivate)->pPortDef[NBAMRENC_OUTPUT_PORT];
1642
1643#ifdef _ERROR_PROPAGATION__
1644    if (pComponentPrivate->curState == OMX_StateInvalid){
1645        eError = OMX_ErrorInvalidState;
1646        goto EXIT;
1647    }
1648#endif
1649#ifdef __PERF_INSTRUMENTATION__
1650    PERF_ReceivedFrame(pComponentPrivate->pPERF,
1651                       pBuffer->pBuffer,
1652                       0,
1653                       PERF_ModuleHLMM);
1654#endif
1655    OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Entering FillThisBuffer\n", __LINE__);
1656    OMX_PRBUFFER2(pComponentPrivate->dbg, "------------------------------------------------------------------\n");
1657    OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Comp Sending Emptied op buff = %p to CompThread\n",__LINE__,pBuffer);
1658    OMX_PRBUFFER2(pComponentPrivate->dbg, "------------------------------------------------------------------\n");
1659    if (pBuffer == NULL) {
1660        eError = OMX_ErrorBadParameter;
1661        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorBadParameter\n",__LINE__);
1662        goto EXIT;
1663    }
1664
1665    if (pBuffer->nSize != sizeof(OMX_BUFFERHEADERTYPE)) {
1666        eError = OMX_ErrorBadParameter;
1667        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorBadParameter\n",__LINE__);
1668        goto EXIT;
1669    }
1670
1671    if (!pPortDef->bEnabled) {
1672        eError  = OMX_ErrorIncorrectStateOperation;
1673        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorIncorrectStateOperation\n",__LINE__);
1674        goto EXIT;
1675    }
1676
1677    if (pBuffer->nVersion.nVersion != pComponentPrivate->nVersion) {
1678        eError = OMX_ErrorVersionMismatch;
1679        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorVersionMismatch\n",__LINE__);
1680        goto EXIT;
1681    }
1682
1683    if (pBuffer->nOutputPortIndex != NBAMRENC_OUTPUT_PORT) {
1684        eError  = OMX_ErrorBadPortIndex;
1685        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorBadPortIndex\n",__LINE__);
1686        goto EXIT;
1687    }
1688
1689    if(pComponentPrivate->curState != OMX_StateExecuting && pComponentPrivate->curState != OMX_StatePause) {
1690        eError = OMX_ErrorIncorrectStateOperation;
1691        OMX_ERROR4(pComponentPrivate->dbg, "%d ::OMX_ErrorIncorrectStateOperation because state it is no more OMX_StatePause nor OMX_StateExecuting\n",__LINE__);
1692        goto EXIT;
1693    }
1694
1695    pComponentPrivate->app_nBuf--;
1696    if(pComponentPrivate->pMarkBuf != NULL){
1697        pBuffer->hMarkTargetComponent = pComponentPrivate->pMarkBuf->hMarkTargetComponent;
1698        pBuffer->pMarkData = pComponentPrivate->pMarkBuf->pMarkData;
1699        pComponentPrivate->pMarkBuf = NULL;
1700    }
1701
1702    if (pComponentPrivate->pMarkData != NULL) {
1703        pBuffer->hMarkTargetComponent = pComponentPrivate->hMarkTargetComponent;
1704        pBuffer->pMarkData = pComponentPrivate->pMarkData;
1705        pComponentPrivate->pMarkData = NULL;
1706    }
1707
1708
1709    pComponentPrivate->ProcessingOutputBuf++;
1710    pComponentPrivate->nUnhandledFillThisBuffers++;
1711    ret = write (pComponentPrivate->dataPipe[1], &pBuffer, sizeof (OMX_BUFFERHEADERTYPE*));
1712    if (ret == -1) {
1713        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error in Writing to the Data pipe\n", __LINE__);
1714        eError = OMX_ErrorHardware;
1715        goto EXIT;
1716    }
1717    pComponentPrivate->nFillThisBufferCount++;
1718EXIT:
1719    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting FillThisBuffer\n", __LINE__);
1720    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1721    return eError;
1722}
1723/*-------------------------------------------------------------------*/
1724/**
1725  * OMX_ComponentDeinit() this methold will de init the component
1726  *
1727  * @param pComp         handle for this instance of the component
1728  *
1729  * @retval OMX_NoError              Success, ready to roll
1730  *         OMX_Error_BadParameter   The input parameter pointer is null
1731  **/
1732/*-------------------------------------------------------------------*/
1733
1734static OMX_ERRORTYPE ComponentDeInit(OMX_HANDLETYPE pHandle)
1735{
1736    OMX_ERRORTYPE eError = OMX_ErrorNone;
1737
1738    OMX_COMPONENTTYPE *pComponent = (OMX_COMPONENTTYPE *)pHandle;
1739    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
1740                         (AMRENC_COMPONENT_PRIVATE *)pComponent->pComponentPrivate;
1741    struct OMX_TI_Debug dbg;
1742    dbg = pComponentPrivate->dbg;
1743
1744    OMX_PRINT1(dbg, "%d :: Entering ComponentDeInit\n", __LINE__);
1745
1746#ifdef __PERF_INSTRUMENTATION__
1747    PERF_Boundary(pComponentPrivate->pPERF,
1748                  PERF_BoundaryStart | PERF_BoundaryCleanup);
1749#endif
1750
1751#ifdef DSP_RENDERING_ON
1752    close(pComponentPrivate->fdwrite);
1753    close(pComponentPrivate->fdread);
1754#endif
1755#ifdef RESOURCE_MANAGER_ENABLED
1756    /* eError = RMProxy_SendCommand(pHandle, RMProxy_FreeResource, OMX_NBAMR_Encoder_COMPONENT, 0, NULL); */
1757    eError = RMProxy_NewSendCommand(pHandle, RMProxy_FreeResource, OMX_NBAMR_Encoder_COMPONENT, 0, 3456, NULL);
1758    if (eError != OMX_ErrorNone) {
1759         OMX_ERROR4(dbg, "%d ::Error returned from destroy ResourceManagerProxy thread\n",
1760                                                        __LINE__);
1761    }
1762    eError = RMProxy_Deinitalize();
1763    if (eError != OMX_ErrorNone) {
1764         OMX_ERROR4(dbg, "%d :: Error from RMProxy_Deinitalize\n",__LINE__);
1765         goto EXIT;
1766    }
1767#endif
1768
1769    pComponentPrivate->bIsThreadstop = 1;
1770    eError = NBAMRENC_StopComponentThread(pHandle);
1771    if (eError != OMX_ErrorNone) {
1772         OMX_ERROR4(dbg, "%d :: Error from NBAMRENC_StopComponentThread\n",__LINE__);
1773         goto EXIT;
1774    }
1775    /* Wait for thread to exit so we can get the status into "eError" */
1776    /* close the pipe handles */
1777    eError = NBAMRENC_FreeCompResources(pHandle);
1778    if (eError != OMX_ErrorNone) {
1779         OMX_ERROR4(dbg, "%d :: Error from NBAMRENC_FreeCompResources\n",__LINE__);
1780         goto EXIT;
1781    }
1782
1783#ifndef UNDER_CE
1784    pthread_mutex_destroy(&pComponentPrivate->InLoaded_mutex);
1785    pthread_cond_destroy(&pComponentPrivate->InLoaded_threshold);
1786
1787    pthread_mutex_destroy(&pComponentPrivate->InIdle_mutex);
1788    pthread_cond_destroy(&pComponentPrivate->InIdle_threshold);
1789
1790    pthread_mutex_destroy(&pComponentPrivate->AlloBuf_mutex);
1791    pthread_cond_destroy(&pComponentPrivate->AlloBuf_threshold);
1792#else
1793    OMX_DestroyEvent(&(pComponentPrivate->InLoaded_event));
1794    OMX_DestroyEvent(&(pComponentPrivate->InIdle_event));
1795    OMX_DestroyEvent(&(pComponentPrivate->AlloBuf_event));
1796#endif
1797
1798    pthread_mutex_destroy(&pComponentPrivate->mutexStateChangeRequest);
1799    pthread_cond_destroy(&pComponentPrivate->StateChangeCondition);
1800#ifdef __PERF_INSTRUMENTATION__
1801    PERF_Boundary(pComponentPrivate->pPERF,
1802                  PERF_BoundaryComplete | PERF_BoundaryCleanup);
1803    PERF_Done(pComponentPrivate->pPERF);
1804#endif
1805
1806    OMX_MEMFREE_STRUCT(pComponentPrivate->sDeviceString);
1807    OMX_MEMFREE_STRUCT(pComponentPrivate);
1808
1809EXIT:
1810    OMXDBG_PRINT(stderr, PRINT, 1, 0, "%d :: Exiting ComponentDeInit Returning 0x%x\n", __LINE__, eError);
1811    return eError;
1812}
1813
1814/*-------------------------------------------------------------------*/
1815/**
1816  *  ComponentTunnelRequest() this method is not implemented in 1.5
1817  *
1818  * This method will update application callbacks
1819  * the application.
1820  *
1821  * @param pComp         handle for this instance of the component
1822  * @param pCallBacks    application callbacks
1823  * @param ptr
1824  *
1825  * @retval OMX_NoError              Success, ready to roll
1826  *         OMX_Error_BadParameter   The input parameter pointer is null
1827  **/
1828/*-------------------------------------------------------------------*/
1829
1830static OMX_ERRORTYPE ComponentTunnelRequest (OMX_HANDLETYPE hComp,
1831                                             OMX_U32 nPort, OMX_HANDLETYPE hTunneledComp,
1832                                             OMX_U32 nTunneledPort,
1833                                             OMX_TUNNELSETUPTYPE* pTunnelSetup)
1834{
1835    OMX_ERRORTYPE eError = OMX_ErrorNone;
1836    eError = OMX_ErrorNotImplemented;
1837    OMXDBG_PRINT(stderr, PRINT, 1, 0, "%d :: Entering ComponentTunnelRequest\n", __LINE__);
1838    OMXDBG_PRINT(stderr, PRINT, 1, 0, "%d :: Exiting ComponentTunnelRequest\n", __LINE__);
1839    OMXDBG_PRINT(stderr, PRINT, 1, 0, "%d :: Returning = 0x%x\n",__LINE__,eError);
1840    return eError;
1841}
1842
1843/*-------------------------------------------------------------------*/
1844/**
1845  *  AllocateBuffer()
1846
1847  * @param pComp         handle for this instance of the component
1848  * @param pCallBacks    application callbacks
1849  * @param ptr
1850  *
1851  * @retval OMX_NoError              Success, ready to roll
1852  *         OMX_Error_BadParameter   The input parameter pointer is null
1853  **/
1854/*-------------------------------------------------------------------*/
1855
1856static OMX_ERRORTYPE AllocateBuffer (OMX_IN OMX_HANDLETYPE hComponent,
1857                   OMX_INOUT OMX_BUFFERHEADERTYPE** pBuffer,
1858                   OMX_IN OMX_U32 nPortIndex,
1859                   OMX_IN OMX_PTR pAppPrivate,
1860                   OMX_IN OMX_U32 nSizeBytes)
1861
1862{
1863    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef;
1864    AMRENC_COMPONENT_PRIVATE *pComponentPrivate;
1865    OMX_ERRORTYPE eError = OMX_ErrorNone;
1866    OMX_BUFFERHEADERTYPE *pBufferHeader;
1867
1868    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
1869            (((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
1870
1871    pPortDef = ((AMRENC_COMPONENT_PRIVATE*)pComponentPrivate)->pPortDef[nPortIndex];
1872#ifdef _ERROR_PROPAGATION__
1873    if (pComponentPrivate->curState == OMX_StateInvalid){
1874        eError = OMX_ErrorInvalidState;
1875        goto EXIT;
1876    }
1877#endif
1878    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering AllocateBuffer\n", __LINE__);
1879    OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef = %p\n", __LINE__,pPortDef);
1880    OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef->bEnabled = %d\n", __LINE__,pPortDef->bEnabled);
1881    while (1) {
1882        if(pPortDef->bEnabled) {
1883            break;
1884        }
1885        pComponentPrivate->AlloBuf_waitingsignal = 1;
1886#ifndef UNDER_CE
1887        pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
1888        pthread_cond_wait(&pComponentPrivate->AlloBuf_threshold, &pComponentPrivate->AlloBuf_mutex);
1889        pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
1890#else
1891        OMX_WaitForEvent(&(pComponentPrivate->AlloBuf_event));
1892#endif
1893        break;
1894    }
1895
1896    OMX_MALLOC_GENERIC(pBufferHeader, OMX_BUFFERHEADERTYPE);
1897
1898    OMX_MALLOC_SIZE_DSPALIGN(pBufferHeader->pBuffer, nSizeBytes, OMX_U8);
1899
1900    if (nPortIndex == NBAMRENC_INPUT_PORT) {
1901        pBufferHeader->nInputPortIndex = nPortIndex;
1902        pBufferHeader->nOutputPortIndex = -1;
1903        pComponentPrivate->pInputBufferList->pBufHdr[pComponentPrivate->pInputBufferList->numBuffers] = pBufferHeader;
1904        pComponentPrivate->pInputBufferList->bBufferPending[pComponentPrivate->pInputBufferList->numBuffers] = 0;
1905        pComponentPrivate->pInputBufferList->bufferOwner[pComponentPrivate->pInputBufferList->numBuffers++] = 1;
1906        if (pComponentPrivate->pInputBufferList->numBuffers == pPortDef->nBufferCountActual) {
1907            pPortDef->bPopulated = OMX_TRUE;
1908            OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef->bPopulated = %d\n", __LINE__, pPortDef->bPopulated);
1909        }
1910    }
1911    else if (nPortIndex == NBAMRENC_OUTPUT_PORT) {
1912        pBufferHeader->nInputPortIndex = -1;
1913        pBufferHeader->nOutputPortIndex = nPortIndex;
1914        pComponentPrivate->pOutputBufferList->pBufHdr[pComponentPrivate->pOutputBufferList->numBuffers] = pBufferHeader;
1915        pComponentPrivate->pOutputBufferList->bBufferPending[pComponentPrivate->pOutputBufferList->numBuffers] = 0;
1916        pComponentPrivate->pOutputBufferList->bufferOwner[pComponentPrivate->pOutputBufferList->numBuffers++] = 1;
1917        OMX_MALLOC_GENERIC(pBufferHeader->pOutputPortPrivate, NBAMRENC_BUFDATA);
1918
1919        if (pComponentPrivate->pOutputBufferList->numBuffers == pPortDef->nBufferCountActual) {
1920            pPortDef->bPopulated = OMX_TRUE;
1921            OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef->bPopulated = %d\n", __LINE__, pPortDef->bPopulated);
1922        }
1923    }
1924    else {
1925        eError = OMX_ErrorBadPortIndex;
1926        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorBadPortIndex\n",__LINE__);
1927        goto EXIT;
1928    }
1929    if (((!pComponentPrivate->dasfMode) &&
1930       ((pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled)&&
1931       (pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled) &&
1932       (pComponentPrivate->InLoaded_readytoidle)))/*File Mode*/
1933       ||
1934       ((pComponentPrivate->dasfMode) &&
1935       ((pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled)&&
1936       (pComponentPrivate->InLoaded_readytoidle))))/*Dasf Mode*/
1937    {
1938        pComponentPrivate->InLoaded_readytoidle = 0;
1939#ifndef UNDER_CE
1940        pthread_mutex_lock(&pComponentPrivate->InLoaded_mutex);
1941        pthread_cond_signal(&pComponentPrivate->InLoaded_threshold);
1942        pthread_mutex_unlock(&pComponentPrivate->InLoaded_mutex);
1943#else
1944        OMX_SignalEvent(&(pComponentPrivate->InLoaded_event));
1945#endif
1946    }
1947    pBufferHeader->pAppPrivate = pAppPrivate;
1948    pBufferHeader->pPlatformPrivate = pComponentPrivate;
1949    pBufferHeader->nAllocLen = nSizeBytes;
1950    pBufferHeader->nVersion.s.nVersionMajor = NBAMRENC_MAJOR_VER;
1951    pBufferHeader->nVersion.s.nVersionMinor = NBAMRENC_MINOR_VER;
1952    pComponentPrivate->nVersion = pBufferHeader->nVersion.nVersion;
1953    pBufferHeader->nSize = sizeof(OMX_BUFFERHEADERTYPE);
1954    *pBuffer = pBufferHeader;
1955
1956    if (pComponentPrivate->bEnableCommandPending && pPortDef->bPopulated) {
1957        SendCommand (pComponentPrivate->pHandle,
1958                     OMX_CommandPortEnable,
1959                     pComponentPrivate->bEnableCommandParam,NULL);
1960    }
1961
1962
1963
1964EXIT:
1965
1966#ifdef __PERF_INSTRUMENTATION__
1967    PERF_ReceivedBuffer(pComponentPrivate->pPERF,
1968                        (*pBuffer)->pBuffer, nSizeBytes,
1969                        PERF_ModuleMemory);
1970#endif
1971
1972    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting AllocateBuffer\n",__LINE__);
1973    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1974    return eError;
1975}
1976
1977/*-------------------------------------------------------------------*/
1978/**
1979  *  FreeBuffer()
1980
1981  * @param hComponent   handle for this instance of the component
1982  * @param pCallBacks   application callbacks
1983  * @param ptr
1984  *
1985  * @retval OMX_NoError              Success, ready to roll
1986  *         OMX_Error_BadParameter   The input parameter pointer is null
1987  **/
1988/*-------------------------------------------------------------------*/
1989
1990static OMX_ERRORTYPE FreeBuffer(
1991            OMX_IN  OMX_HANDLETYPE hComponent,
1992            OMX_IN  OMX_U32 nPortIndex,
1993            OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
1994{
1995    OMX_ERRORTYPE eError = OMX_ErrorNone;
1996    AMRENC_COMPONENT_PRIVATE * pComponentPrivate = NULL;
1997    OMX_BUFFERHEADERTYPE* buff;
1998    int i = 0;
1999    int inputIndex = -1;
2000    int outputIndex = -1;
2001    OMX_COMPONENTTYPE *pHandle;
2002
2003    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
2004                                (((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2005
2006    pHandle = (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
2007
2008    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering FreeBuffer\n", __LINE__);
2009    for (i=0; i < NBAMRENC_MAX_NUM_OF_BUFS; i++) {
2010        buff = pComponentPrivate->pInputBufferList->pBufHdr[i];
2011        if (buff == pBuffer) {
2012            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Found matching input buffer\n",__LINE__);
2013            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: buff = %p\n",__LINE__,buff);
2014            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
2015            inputIndex = i;
2016            break;
2017        }
2018        else {
2019            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: This is not a match\n",__LINE__);
2020            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: buff = %p\n",__LINE__,buff);
2021            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
2022        }
2023    }
2024
2025    for (i=0; i < NBAMRENC_MAX_NUM_OF_BUFS; i++) {
2026        buff = pComponentPrivate->pOutputBufferList->pBufHdr[i];
2027        if (buff == pBuffer) {
2028            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Found matching output buffer\n",__LINE__);
2029            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: buff = %p\n",__LINE__,buff);
2030            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
2031            outputIndex = i;
2032            break;
2033        }
2034        else {
2035            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: This is not a match\n",__LINE__);
2036            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: buff = %p\n",__LINE__,buff);
2037            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
2038        }
2039    }
2040
2041
2042    if (inputIndex != -1) {
2043        if (pComponentPrivate->pInputBufferList->bufferOwner[inputIndex] == 1) {
2044            OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pInputBufferList->pBufHdr[inputIndex]->pBuffer, OMX_U8);
2045        }
2046
2047#ifdef __PERF_INSTRUMENTATION__
2048    PERF_SendingBuffer(pComponentPrivate->pPERF,
2049                       pComponentPrivate->pInputBufferList->pBufHdr[inputIndex]->pBuffer,
2050                       pComponentPrivate->pInputBufferList->pBufHdr[inputIndex]->nAllocLen,
2051                       PERF_ModuleMemory );
2052#endif
2053
2054        OMX_MEMFREE_STRUCT(pComponentPrivate->pInputBufferList->pBufHdr[inputIndex]);
2055
2056            pComponentPrivate->pInputBufferList->numBuffers--;
2057        if (pComponentPrivate->pInputBufferList->numBuffers <
2058            pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nBufferCountMin) {
2059
2060            pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated = OMX_FALSE;
2061            }
2062        if(pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled &&
2063               pComponentPrivate->bLoadedCommandPending == OMX_FALSE &&
2064                (pComponentPrivate->curState == OMX_StateIdle ||
2065                pComponentPrivate->curState == OMX_StateExecuting ||
2066                pComponentPrivate->curState == OMX_StatePause)) {
2067            pComponentPrivate->cbInfo.EventHandler( pHandle,
2068                                                    pHandle->pApplicationPrivate,
2069                                                    OMX_EventError,
2070                                                    OMX_ErrorPortUnpopulated,
2071                                                    OMX_TI_ErrorMinor,
2072                                                    "Input Port Unpopulated");
2073        }
2074    }
2075    else if (outputIndex != -1) {
2076         if (pComponentPrivate->pOutputBufferList->bufferOwner[outputIndex] == 1) {
2077            OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]->pBuffer, OMX_U8);
2078        }
2079
2080#ifdef __PERF_INSTRUMENTATION__
2081    PERF_SendingBuffer(pComponentPrivate->pPERF,
2082                       pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]->pBuffer,
2083                       pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]->nAllocLen,
2084                       PERF_ModuleMemory );
2085#endif
2086
2087        OMX_MEMFREE_STRUCT(pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]->pOutputPortPrivate);
2088        OMX_MEMFREE_STRUCT(pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]);
2089
2090        pComponentPrivate->pOutputBufferList->numBuffers--;
2091        if (pComponentPrivate->pOutputBufferList->numBuffers <
2092            pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nBufferCountMin) {
2093            pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated = OMX_FALSE;
2094        }
2095        if( pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled &&
2096            pComponentPrivate->bLoadedCommandPending == OMX_FALSE &&
2097            (pComponentPrivate->curState == OMX_StateIdle ||
2098            pComponentPrivate->curState == OMX_StateExecuting ||
2099            pComponentPrivate->curState == OMX_StatePause)) {
2100            pComponentPrivate->cbInfo.EventHandler( pHandle,
2101                                                    pHandle->pApplicationPrivate,
2102                                                    OMX_EventError,
2103                                                    OMX_ErrorPortUnpopulated,
2104                                                    OMX_TI_ErrorMinor,
2105                                                    "Output Port Unpopulated");
2106        }
2107    }
2108    else {
2109        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Returning OMX_ErrorBadParameter\n",__LINE__);
2110        eError = OMX_ErrorBadParameter;
2111    }
2112
2113    pthread_mutex_lock(&pComponentPrivate->ToLoaded_mutex);
2114    OMX_PRBUFFER1(pComponentPrivate->dbg, "num input buffers es %d, \n num output buffers es %d,\n InIdle_goingtoloaded es %d \n",
2115            pComponentPrivate->pInputBufferList->numBuffers, pComponentPrivate->pOutputBufferList->numBuffers, pComponentPrivate->InIdle_goingtoloaded);
2116
2117       if ((!pComponentPrivate->pInputBufferList->numBuffers &&
2118            !pComponentPrivate->pOutputBufferList->numBuffers) &&
2119            pComponentPrivate->InIdle_goingtoloaded)
2120       {
2121#ifndef UNDER_CE
2122           pthread_mutex_lock(&pComponentPrivate->InIdle_mutex);
2123           pComponentPrivate->InIdle_goingtoloaded = 0;
2124           pthread_cond_signal(&pComponentPrivate->InIdle_threshold);
2125           pthread_mutex_unlock(&pComponentPrivate->InIdle_mutex);
2126#else
2127           OMX_SignalEvent(&(pComponentPrivate->InIdle_event));
2128#endif
2129       }
2130
2131    pthread_mutex_unlock(&pComponentPrivate->ToLoaded_mutex);
2132    if (pComponentPrivate->bDisableCommandPending &&
2133        (pComponentPrivate->pInputBufferList->numBuffers + pComponentPrivate->pOutputBufferList->numBuffers == 0)) {
2134            SendCommand (pComponentPrivate->pHandle,OMX_CommandPortDisable,pComponentPrivate->bDisableCommandParam,NULL);
2135    }
2136    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting FreeBuffer\n", __LINE__);
2137    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
2138    return eError;
2139}
2140
2141/*-------------------------------------------------------------------*/
2142/**
2143  *  UseBuffer()
2144
2145  * @param pComp         handle for this instance of the component
2146  * @param pCallBacks    application callbacks
2147  * @param ptr
2148  *
2149  * @retval OMX_NoError              Success, ready to roll
2150  *         OMX_Error_BadParameter   The input parameter pointer is null
2151  **/
2152/*-------------------------------------------------------------------*/
2153static OMX_ERRORTYPE UseBuffer (
2154            OMX_IN OMX_HANDLETYPE hComponent,
2155            OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
2156            OMX_IN OMX_U32 nPortIndex,
2157            OMX_IN OMX_PTR pAppPrivate,
2158            OMX_IN OMX_U32 nSizeBytes,
2159            OMX_IN OMX_U8* pBuffer)
2160{
2161    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef;
2162    AMRENC_COMPONENT_PRIVATE *pComponentPrivate;
2163    OMX_ERRORTYPE eError = OMX_ErrorNone;
2164    OMX_BUFFERHEADERTYPE *pBufferHeader;
2165
2166    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
2167            (((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2168
2169#ifdef _ERROR_PROPAGATION__
2170    if (pComponentPrivate->curState == OMX_StateInvalid){
2171        eError = OMX_ErrorInvalidState;
2172        goto EXIT;
2173    }
2174
2175#endif
2176#ifdef __PERF_INSTRUMENTATION__
2177    PERF_ReceivedBuffer(pComponentPrivate->pPERF,
2178                        pBuffer, nSizeBytes,
2179                        PERF_ModuleHLMM);
2180#endif
2181
2182    pPortDef = ((AMRENC_COMPONENT_PRIVATE*)pComponentPrivate)->pPortDef[nPortIndex];
2183    OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: Entering UseBuffer\n", __LINE__);
2184    OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef->bPopulated = %d \n",__LINE__,pPortDef->bPopulated);
2185
2186    if(!pPortDef->bEnabled) {
2187        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorIncorrectStateOperation\n",__LINE__);
2188        eError = OMX_ErrorIncorrectStateOperation;
2189        goto EXIT;
2190    }
2191
2192    OMX_MALLOC_GENERIC(pBufferHeader, OMX_BUFFERHEADERTYPE);
2193
2194    if (nPortIndex == NBAMRENC_OUTPUT_PORT) {
2195        pBufferHeader->nInputPortIndex = -1;
2196        pBufferHeader->nOutputPortIndex = nPortIndex;
2197        pComponentPrivate->pOutputBufferList->pBufHdr[pComponentPrivate->pOutputBufferList->numBuffers] = pBufferHeader;
2198        pComponentPrivate->pOutputBufferList->bBufferPending[pComponentPrivate->pOutputBufferList->numBuffers] = 0;
2199        pComponentPrivate->pOutputBufferList->bufferOwner[pComponentPrivate->pOutputBufferList->numBuffers++] = 0;
2200        OMX_MALLOC_GENERIC(pBufferHeader->pOutputPortPrivate, NBAMRENC_BUFDATA);
2201
2202        if (pComponentPrivate->pOutputBufferList->numBuffers == pPortDef->nBufferCountActual) {
2203            pPortDef->bPopulated = OMX_TRUE;
2204        }
2205    }
2206    else {
2207        pBufferHeader->nInputPortIndex = nPortIndex;
2208        pBufferHeader->nOutputPortIndex = -1;
2209        pComponentPrivate->pInputBufferList->pBufHdr[pComponentPrivate->pInputBufferList->numBuffers] = pBufferHeader;
2210        pComponentPrivate->pInputBufferList->bBufferPending[pComponentPrivate->pInputBufferList->numBuffers] = 0;
2211        pComponentPrivate->pInputBufferList->bufferOwner[pComponentPrivate->pInputBufferList->numBuffers++] = 0;
2212        if (pComponentPrivate->pInputBufferList->numBuffers == pPortDef->nBufferCountActual) {
2213            pPortDef->bPopulated = OMX_TRUE;
2214        }
2215    }
2216
2217    if (((!pComponentPrivate->dasfMode) &&
2218       ((pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled)&&
2219       (pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled) &&
2220       (pComponentPrivate->InLoaded_readytoidle)))/*File Mode*/
2221       ||
2222       ((pComponentPrivate->dasfMode) &&
2223       ((pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled)&&
2224       (pComponentPrivate->InLoaded_readytoidle))))/*Dasf Mode*/
2225    {
2226        pComponentPrivate->InLoaded_readytoidle = 0;
2227#ifndef UNDER_CE
2228        pthread_mutex_lock(&pComponentPrivate->InLoaded_mutex);
2229        pthread_cond_signal(&pComponentPrivate->InLoaded_threshold);
2230        pthread_mutex_unlock(&pComponentPrivate->InLoaded_mutex);
2231#else
2232        OMX_SignalEvent(&(pComponentPrivate->InLoaded_event));
2233#endif
2234    }
2235    pBufferHeader->pAppPrivate = pAppPrivate;
2236    pBufferHeader->pPlatformPrivate = pComponentPrivate;
2237    pBufferHeader->nAllocLen = nSizeBytes;
2238    pBufferHeader->nVersion.s.nVersionMajor = NBAMRENC_MAJOR_VER;
2239    pBufferHeader->nVersion.s.nVersionMinor = NBAMRENC_MINOR_VER;
2240    pComponentPrivate->nVersion = pBufferHeader->nVersion.nVersion;
2241    pBufferHeader->pBuffer = pBuffer;
2242    pBufferHeader->nSize = sizeof(OMX_BUFFERHEADERTYPE);
2243    *ppBufferHdr = pBufferHeader;
2244
2245    if (pComponentPrivate->bEnableCommandPending){
2246        SendCommand (pComponentPrivate->pHandle,
2247                     OMX_CommandPortEnable,
2248                     pComponentPrivate->bEnableCommandParam,NULL);
2249    }
2250
2251EXIT:
2252    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting UseBuffer\n", __LINE__);
2253    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
2254    return eError;
2255}
2256
2257/* ================================================================================= */
2258/**
2259* @fn GetExtensionIndex() description for GetExtensionIndex
2260GetExtensionIndex().
2261Returns index for vendor specific settings.
2262*
2263*  @see         OMX_Core.h
2264*/
2265/* ================================================================================ */
2266static OMX_ERRORTYPE GetExtensionIndex(
2267            OMX_IN  OMX_HANDLETYPE hComponent,
2268            OMX_IN  OMX_STRING cParameterName,
2269            OMX_OUT OMX_INDEXTYPE* pIndexType)
2270{
2271    OMX_ERRORTYPE eError = OMX_ErrorNone;
2272
2273    OMXDBG_PRINT(stderr, PRINT, 1, 0, "GetExtensionIndex\n");
2274
2275    if (!(strcmp(cParameterName,"OMX.TI.index.config.tispecific"))) {
2276        *pIndexType = OMX_IndexCustomNBAMRENCModeConfig;
2277        OMXDBG_PRINT(stderr, PRINT, 2, 0, "OMX_IndexCustomNBAMRENCModeConfig\n");
2278    }
2279    else if(!(strcmp(cParameterName,"OMX.TI.index.config.nbamrstreamIDinfo"))) {
2280        *pIndexType = OMX_IndexCustomNBAMRENCStreamIDConfig;
2281    }
2282    else if(!(strcmp(cParameterName,"OMX.TI.index.config.nbamr.datapath")))
2283    {
2284        *pIndexType = OMX_IndexCustomNBAMRENCDataPath;
2285        }
2286    else if(!(strcmp(cParameterName,"OMX.TI.AMR.Encode.Debug")))
2287    {
2288        *pIndexType = OMX_IndexCustomDebug;
2289        }
2290    else {
2291        eError = OMX_ErrorBadParameter;
2292    }
2293
2294    OMXDBG_PRINT(stderr, PRINT, 1, 0, "Exiting GetExtensionIndex\n");
2295    return eError;
2296}
2297
2298#ifdef UNDER_CE
2299/* ================================================================================= */
2300/**
2301* @fns Sleep replace for WIN CE
2302*/
2303/* ================================================================================ */
2304int OMX_CreateEvent(OMX_Event *event){
2305    int ret = OMX_ErrorNone;
2306    HANDLE createdEvent = NULL;
2307    if(event == NULL){
2308        ret = OMX_ErrorBadParameter;
2309        goto EXIT;
2310    }
2311    event->event  = CreateEvent(NULL, TRUE, FALSE, NULL);
2312    if(event->event == NULL)
2313        ret = (int)GetLastError();
2314EXIT:
2315    return ret;
2316}
2317
2318int OMX_SignalEvent(OMX_Event *event){
2319     int ret = OMX_ErrorNone;
2320     if(event == NULL){
2321        ret = OMX_ErrorBadParameter;
2322        goto EXIT;
2323     }
2324     SetEvent(event->event);
2325     ret = (int)GetLastError();
2326EXIT:
2327    return ret;
2328}
2329
2330int OMX_WaitForEvent(OMX_Event *event) {
2331     int ret = OMX_ErrorNone;
2332     if(event == NULL){
2333        ret = OMX_ErrorBadParameter;
2334        goto EXIT;
2335     }
2336     WaitForSingleObject(event->event, INFINITE);
2337     ret = (int)GetLastError();
2338EXIT:
2339     return ret;
2340}
2341
2342int OMX_DestroyEvent(OMX_Event *event) {
2343     int ret = OMX_ErrorNone;
2344     if(event == NULL){
2345        ret = OMX_ErrorBadParameter;
2346        goto EXIT;
2347     }
2348     CloseHandle(event->event);
2349EXIT:
2350     return ret;
2351}
2352#endif
2353
2354/* ================================================================================= */
2355/**
2356* @fn ComponentRoleEnum() description for ComponentRoleEnum()
2357
2358Returns the role at the given index
2359*
2360*  @see         OMX_Core.h
2361*/
2362/* ================================================================================ */
2363static OMX_ERRORTYPE ComponentRoleEnum(
2364         OMX_IN OMX_HANDLETYPE hComponent,
2365      OMX_OUT OMX_U8 *cRole,
2366      OMX_IN OMX_U32 nIndex)
2367{
2368    AMRENC_COMPONENT_PRIVATE *pComponentPrivate;
2369
2370    OMX_ERRORTYPE eError = OMX_ErrorNone;
2371    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2372
2373    if(nIndex == 0){
2374      if (cRole == NULL) {
2375          eError = OMX_ErrorBadParameter;
2376      }
2377      else {
2378          memcpy(cRole, &pComponentPrivate->componentRole.cRole, sizeof(OMX_U8) * OMX_MAX_STRINGNAME_SIZE);
2379          OMX_PRINT1(pComponentPrivate->dbg, "::::In ComponenetRoleEnum: cRole is set to %s\n",cRole);
2380      }
2381    }
2382    else {
2383      eError = OMX_ErrorNoMore;
2384        }
2385    return eError;
2386}
2387