OMX_AmrEncoder.c revision b01194f357cd5bc2cdad578ac6431640ff3fb148
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                    switch (pCompAmrParam->eAMRBandMode)
1130                    {
1131                             case OMX_AUDIO_AMRBandModeNB7:
1132                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_BR122;
1133                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR122 \n",__LINE__);
1134                                  break;
1135                             case OMX_AUDIO_AMRBandModeNB6:
1136                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_BR102;
1137                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR102 \n",__LINE__);
1138                                  break;
1139                             case OMX_AUDIO_AMRBandModeNB5:
1140                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_BR795;
1141                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR795 \n",__LINE__);
1142                                  break;
1143                             case OMX_AUDIO_AMRBandModeNB4:
1144                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_BR74;
1145                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR74 \n",__LINE__);
1146                                  break;
1147                             case OMX_AUDIO_AMRBandModeNB3:
1148                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_BR67;
1149                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR67 \n",__LINE__);
1150                                  break;
1151                             case OMX_AUDIO_AMRBandModeNB2:
1152                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_BR59;
1153                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR59 \n",__LINE__);
1154                                  break;
1155                             case OMX_AUDIO_AMRBandModeNB1:
1156                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_BR515;
1157                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR515 \n",__LINE__);
1158                                  break;
1159                             case OMX_AUDIO_AMRBandModeNB0:
1160                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_475;
1161                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode = SN_AUDIO_BR475 \n",__LINE__);
1162                                  break;
1163                             default:
1164                                  pCompAmrParam->eAMRBandMode = SN_AUDIO_BR122;
1165                                  OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRBandMode =DEFAULT!! SN_AUDIO_BR122 \n",__LINE__);
1166                                  break;
1167                    }
1168		    if (((AMRENC_COMPONENT_PRIVATE *) pHandle->pComponentPrivate)->amrParams == NULL) {
1169                        eError = OMX_ErrorBadParameter;
1170			goto EXIT;
1171		    }
1172                    memcpy(((AMRENC_COMPONENT_PRIVATE *)
1173                            pHandle->pComponentPrivate)->amrParams, pCompAmrParam, sizeof(OMX_AUDIO_PARAM_AMRTYPE));
1174                    if (pCompAmrParam->eAMRFrameFormat == OMX_AUDIO_AMRFrameFormatConformance) {
1175                        pComponentPrivate->frameMode = NBAMRENC_FORMATCONFORMANCE;
1176                    }
1177                    else if((pCompAmrParam->eAMRFrameFormat == OMX_AUDIO_AMRFrameFormatFSF) ||
1178                            (pCompAmrParam->eAMRFrameFormat == OMX_AUDIO_AMRFrameFormatRTPPayload)){
1179                            pComponentPrivate->frameMode = NBAMRENC_MIMEMODE;
1180                            OMX_PRDSP1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRFrameFormate = FSF \n",__LINE__);
1181                    }
1182                    else {
1183                        pComponentPrivate->frameMode = NBAMRENC_IF2;
1184                        OMX_PRDSP1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRFrameFormate = IF2 \n",__LINE__);
1185                    }
1186                    if(pCompAmrParam->eAMRDTXMode == OMX_AUDIO_AMRDTXasEFR) {
1187                        OMX_PRDSP1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioAmr:: pCompAmrParam->eAMRDTXMode = XasEFR\n",__LINE__);
1188                        /*     pComponentPrivate->efrMode = 1; */
1189                    }
1190                }
1191                else {
1192                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from SetParameter",__LINE__);
1193                    eError = OMX_ErrorBadPortIndex;
1194                }
1195            break;
1196
1197        case OMX_IndexParamPortDefinition:
1198                pComponentParamPort = (OMX_PARAM_PORTDEFINITIONTYPE *)pCompParam;
1199                OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamPortDefinition \n",__LINE__);
1200                if(((OMX_PARAM_PORTDEFINITIONTYPE *)(pCompParam))->nPortIndex ==
1201                                    pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nPortIndex) {
1202                    OMX_PRINT1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamPortDefinition \n",__LINE__);
1203                    memcpy(pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT], pCompParam, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1204                }
1205                else if(((OMX_PARAM_PORTDEFINITIONTYPE *)(pCompParam))->nPortIndex ==
1206                                  pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nPortIndex) {
1207                    OMX_PRINT1(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamPortDefinition \n",__LINE__);
1208                    memcpy(pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT], pCompParam, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1209                }
1210                else {
1211                    OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadPortIndex from SetParameter",__LINE__);
1212                    eError = OMX_ErrorBadPortIndex;
1213                }
1214            break;
1215
1216        case OMX_IndexParamPriorityMgmt:
1217	        if (pComponentPrivate->sPriorityMgmt == NULL) {
1218                    eError = OMX_ErrorBadParameter;
1219		    break;
1220		}
1221                OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamPriorityMgmt \n",__LINE__);
1222                memcpy(pComponentPrivate->sPriorityMgmt, (OMX_PRIORITYMGMTTYPE*)pCompParam, sizeof(OMX_PRIORITYMGMTTYPE));
1223            break;
1224
1225        case OMX_IndexParamAudioInit:
1226	        if (pComponentPrivate->sPortParam == NULL) {
1227                    eError = OMX_ErrorBadParameter;
1228		    break;
1229		}
1230                OMX_PRDSP2(pComponentPrivate->dbg, "%d :: SetParameter OMX_IndexParamAudioInit \n",__LINE__);
1231                memcpy(pComponentPrivate->sPortParam, (OMX_PORT_PARAM_TYPE*)pCompParam, sizeof(OMX_PORT_PARAM_TYPE));
1232            break;
1233
1234        case OMX_IndexParamStandardComponentRole: /*-----------------------------*/
1235           if (pCompParam) {
1236               pRole = (OMX_PARAM_COMPONENTROLETYPE *)pCompParam;
1237               memcpy(&(pComponentPrivate->componentRole), (void *)pRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
1238           } else {
1239               eError = OMX_ErrorBadParameter;
1240           }
1241        break;
1242
1243        case OMX_IndexParamAudioPcm:
1244            if(pCompParam && (pComponentPrivate->pcmParams)){
1245                 amr_ip = (OMX_AUDIO_PARAM_PCMMODETYPE *)pCompParam;
1246                 memcpy(pComponentPrivate->pcmParams, amr_ip, sizeof(OMX_AUDIO_PARAM_PCMMODETYPE));
1247            }
1248            else{
1249                  eError = OMX_ErrorBadParameter;
1250            }
1251        break;
1252
1253        case OMX_IndexParamCompBufferSupplier:
1254            if(((OMX_PARAM_PORTDEFINITIONTYPE *)(pCompParam))->nPortIndex ==
1255                                    pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nPortIndex) {
1256                    OMX_PRBUFFER2(pComponentPrivate->dbg, ":: SetParameter OMX_IndexParamCompBufferSupplier \n");
1257                                   sBufferSupplier.eBufferSupplier = OMX_BufferSupplyInput;
1258                                   memcpy(&sBufferSupplier, pCompParam, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE));
1259
1260                }
1261                else if(((OMX_PARAM_PORTDEFINITIONTYPE *)(pCompParam))->nPortIndex ==
1262                                  pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nPortIndex) {
1263                    OMX_PRBUFFER1(pComponentPrivate->dbg, ":: SetParameter OMX_IndexParamCompBufferSupplier \n");
1264                    sBufferSupplier.eBufferSupplier = OMX_BufferSupplyOutput;
1265                    memcpy(&sBufferSupplier, pCompParam, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE));
1266                }
1267                else {
1268                    OMX_ERROR1(pComponentPrivate->dbg, ":: OMX_ErrorBadPortIndex from SetParameter");
1269                    eError = OMX_ErrorBadPortIndex;
1270                }
1271            break;
1272
1273        default:
1274                OMX_ERROR4(pComponentPrivate->dbg, "%d :: SetParameter OMX_ErrorUnsupportedIndex \n",__LINE__);
1275                eError = OMX_ErrorUnsupportedIndex;
1276            break;
1277    }
1278EXIT:
1279    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting SetParameter\n",__LINE__);
1280    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1281    return eError;
1282}
1283
1284/*-------------------------------------------------------------------*/
1285/**
1286  *  GetConfig() Gets the current configuration of to the component
1287  *
1288  * @param hComp         handle for this instance of the component
1289  * @param nConfigIndex
1290  * @param ComponentConfigStructure
1291  *
1292  * @retval OMX_NoError              Success, ready to roll
1293  *         OMX_Error_BadParameter   The input parameter pointer is null
1294  **/
1295/*-------------------------------------------------------------------*/
1296
1297static OMX_ERRORTYPE GetConfig (OMX_HANDLETYPE hComp,
1298                                OMX_INDEXTYPE nConfigIndex,
1299                                OMX_PTR ComponentConfigStructure)
1300{
1301    OMX_ERRORTYPE eError = OMX_ErrorNone;
1302    OMX_COMPONENTTYPE* pHandle = (OMX_COMPONENTTYPE*)hComp;
1303    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
1304                         (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1305
1306    TI_OMX_STREAM_INFO *streamInfo;
1307    OMX_MALLOC_GENERIC(streamInfo, TI_OMX_STREAM_INFO);
1308#ifdef _ERROR_PROPAGATION__
1309    if (pComponentPrivate->curState == OMX_StateInvalid){
1310        eError = OMX_ErrorInvalidState;
1311        goto EXIT;
1312    }
1313#endif
1314    if(nConfigIndex == OMX_IndexCustomNBAMRENCStreamIDConfig){
1315        streamInfo->streamId = pComponentPrivate->streamID;
1316        memcpy(ComponentConfigStructure,streamInfo,sizeof(TI_OMX_STREAM_INFO));
1317    }
1318    else if(nConfigIndex == OMX_IndexCustomDebug){
1319    OMX_DBG_GETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
1320    }
1321
1322EXIT:
1323    OMX_MEMFREE_STRUCT(streamInfo);
1324    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting GetConfig. Returning = 0x%x\n",__LINE__,eError);
1325    return eError;
1326}
1327/*-------------------------------------------------------------------*/
1328/**
1329  *  SetConfig() Sets the configraiton to the component
1330  *
1331  * @param hComp         handle for this instance of the component
1332  * @param nConfigIndex
1333  * @param ComponentConfigStructure
1334  *
1335  * @retval OMX_NoError              Success, ready to roll
1336  *         OMX_Error_BadParameter   The input parameter pointer is null
1337  **/
1338/*-------------------------------------------------------------------*/
1339
1340static OMX_ERRORTYPE SetConfig (OMX_HANDLETYPE hComp,
1341                                OMX_INDEXTYPE nConfigIndex,
1342                                OMX_PTR ComponentConfigStructure)
1343{
1344    OMX_ERRORTYPE eError = OMX_ErrorNone;
1345    AMRENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
1346    OMX_COMPONENTTYPE* pHandle = (OMX_COMPONENTTYPE*)hComp;
1347    TI_OMX_DSP_DEFINITION *pTiDspDefinition;
1348    OMX_S16 *customFlag = NULL;
1349
1350    TI_OMX_DATAPATH dataPath;
1351    OMX_AUDIO_CONFIG_VOLUMETYPE *pGainStructure = NULL;
1352#ifdef DSP_RENDERING_ON
1353    AM_COMMANDDATATYPE cmd_data;
1354#endif
1355
1356    AMRENC_DPRINT("%d :: Entering SetConfig\n", __LINE__);
1357    if (pHandle == NULL) {
1358	AMRENC_DPRINT("%d :: Invalid HANDLE OMX_ErrorBadParameter \n", __LINE__);
1359        eError = OMX_ErrorBadParameter;
1360        goto EXIT;
1361    }
1362
1363    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1364#ifdef _ERROR_PROPAGATION__
1365    if (pComponentPrivate->curState == OMX_StateInvalid){
1366        eError = OMX_ErrorInvalidState;
1367        goto EXIT;
1368    }
1369#endif
1370    switch (nConfigIndex) {
1371
1372        case OMX_IndexCustomNBAMRENCModeConfig:
1373            pTiDspDefinition = (TI_OMX_DSP_DEFINITION*)ComponentConfigStructure;
1374            if (pTiDspDefinition == NULL) {
1375                eError = OMX_ErrorBadParameter;
1376                OMX_ERROR4(pComponentPrivate->dbg, "%d :: OMX_ErrorBadParameter from SetConfig\n",__LINE__);
1377                goto EXIT;
1378             }
1379            pComponentPrivate->acdnMode = pTiDspDefinition->acousticMode;
1380            pComponentPrivate->dasfMode = pTiDspDefinition->dasfMode;
1381            pComponentPrivate->teeMode = pTiDspDefinition->teeMode;
1382             if( 2 == pComponentPrivate->dasfMode ){
1383                   pComponentPrivate->dasfMode--;
1384             }
1385             pComponentPrivate->streamID = pTiDspDefinition->streamId;
1386
1387            break;
1388/**-------------------**/
1389        case  OMX_IndexCustomNBAMRENCDataPath:
1390            customFlag = (OMX_S16*)ComponentConfigStructure;
1391/*            dataPath = *customFlag;             */
1392            switch(dataPath) {
1393                case DATAPATH_APPLICATION:
1394                    OMX_MMMIXER_DATAPATH(pComponentPrivate->sDeviceString, RENDERTYPE_ENCODER, pComponentPrivate->streamID);
1395                    break;
1396
1397                case DATAPATH_APPLICATION_TEE:
1398                    strcpy((char*)pComponentPrivate->sDeviceString, ":i0:o1\0");
1399                    break;
1400
1401                case DATAPATH_APPLICATION_RTMIXER:
1402                    strcpy((char*)pComponentPrivate->sDeviceString,(char*)RTM_STRING_ENCODER);
1403                    break;
1404
1405                case DATAPATH_ACDN:
1406                     strcpy((char*)pComponentPrivate->sDeviceString,(char*)ACDN_STRING_ENCODER);
1407                     break;
1408                default:
1409                break;
1410
1411            }
1412        break;
1413/**-------------------**/
1414        case OMX_IndexConfigAudioVolume:
1415#ifdef DSP_RENDERING_ON
1416            pGainStructure = (OMX_AUDIO_CONFIG_VOLUMETYPE *)ComponentConfigStructure;
1417            cmd_data.hComponent = hComp;
1418            cmd_data.AM_Cmd = AM_CommandSWGain;
1419            cmd_data.param1 = pGainStructure->sVolume.nValue;
1420            cmd_data.param2 = 0;
1421            cmd_data.streamID = pComponentPrivate->streamID;
1422
1423            if((write(pComponentPrivate->fdwrite, &cmd_data, sizeof(cmd_data)))<0)
1424            {
1425                OMX_PRMGR4(pComponentPrivate->dbg, "[NBAMRENC encoder] - fail to send command to audio manager\n");
1426            }
1427            else
1428            {
1429                OMX_PRMGR2(pComponentPrivate->dbg, "[NBAMRENC encoder] - ok to send command to audio manager\n");
1430            }
1431#endif
1432            break;
1433
1434    case OMX_IndexCustomDebug:
1435        OMX_DBG_SETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
1436        break;
1437
1438    default:
1439        eError = OMX_ErrorUnsupportedIndex;
1440        break;
1441    }
1442EXIT:
1443    if (pComponentPrivate != NULL) {
1444	OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting SetConfig\n", __LINE__);
1445	OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n", __LINE__, eError);
1446    }
1447    return eError;
1448}
1449
1450/*-------------------------------------------------------------------*/
1451/**
1452  *  GetState() Gets the current state of the component
1453  *
1454  * @param pCompomponent handle for this instance of the component
1455  * @param pState
1456  *
1457  * @retval OMX_NoError              Success, ready to roll
1458  *         OMX_Error_BadParameter   The input parameter pointer is null
1459  **/
1460/*-------------------------------------------------------------------*/
1461
1462static OMX_ERRORTYPE GetState (OMX_HANDLETYPE pComponent, OMX_STATETYPE* pState)
1463{
1464    OMX_ERRORTYPE eError                        = OMX_ErrorNone;
1465    OMX_COMPONENTTYPE* pHandle = NULL;
1466    AMRENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
1467    struct timespec abs_time = {0,0};
1468    int nPendingStateChangeRequests = 0;
1469    int ret = 0;
1470    /* Set to sufficiently high value */
1471    int mutex_timeout = 3;
1472
1473    if(pComponent == NULL || pState == NULL) {
1474        return OMX_ErrorBadParameter;
1475    }
1476
1477    pHandle = (OMX_COMPONENTTYPE*)pComponent;
1478    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
1479
1480    /* Retrieve current state */
1481    if (pHandle && pHandle->pComponentPrivate) {
1482        /* Check for any pending state transition requests */
1483        pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest);
1484        while (pComponentPrivate->nPendingStateChangeRequests != 0) {
1485           /* Wait for component to complete state transition */
1486           clock_gettime(CLOCK_REALTIME, &abs_time);
1487           abs_time.tv_sec += mutex_timeout;
1488           abs_time.tv_nsec = 0;
1489           ret = pthread_cond_timedwait(&(pComponentPrivate->StateChangeCondition),
1490                    &(pComponentPrivate->mutexStateChangeRequest), &abs_time);
1491           if (ret == ETIMEDOUT) {
1492              OMX_ERROR4(pComponentPrivate->dbg, "GetState() timeout at state %d",
1493                    pComponentPrivate->curState);
1494              *pState = OMX_StateInvalid;
1495              break;
1496           }
1497        }
1498        if (!ret) {
1499            *pState = pComponentPrivate->curState;
1500        }
1501        pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest);
1502     }
1503     else {
1504        eError = OMX_ErrorInvalidComponent;
1505        *pState = OMX_StateInvalid;
1506    }
1507
1508    return eError;
1509
1510EXIT:
1511    OMX_PRINT1 (pComponentPrivate->dbg, "%d :: AACENC: Exiting GetState\n", __LINE__);
1512    return eError;
1513
1514}
1515
1516/*-------------------------------------------------------------------*/
1517/**
1518  *  EmptyThisBuffer() This callback is used to send the input buffer to
1519  *  component
1520  *
1521  * @param pComponent       handle for this instance of the component
1522  * @param nPortIndex       input port index
1523  * @param pBuffer          buffer to be sent to codec
1524  *
1525  * @retval OMX_NoError              Success, ready to roll
1526  *         OMX_Error_BadParameter   The input parameter pointer is null
1527  **/
1528/*-------------------------------------------------------------------*/
1529
1530static OMX_ERRORTYPE EmptyThisBuffer (OMX_HANDLETYPE pComponent,
1531                                      OMX_BUFFERHEADERTYPE* pBuffer)
1532{
1533    OMX_ERRORTYPE eError = OMX_ErrorNone;
1534    int ret = 0;
1535
1536    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef;
1537
1538    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
1539    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
1540                         (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1541    pPortDef = ((AMRENC_COMPONENT_PRIVATE*)pComponentPrivate)->pPortDef[NBAMRENC_INPUT_PORT];
1542
1543#ifdef _ERROR_PROPAGATION__
1544    if (pComponentPrivate->curState == OMX_StateInvalid){
1545        eError = OMX_ErrorInvalidState;
1546        goto EXIT;
1547    }
1548#endif
1549#ifdef __PERF_INSTRUMENTATION__
1550    PERF_ReceivedFrame(pComponentPrivate->pPERF,
1551                       pBuffer->pBuffer,
1552                       pBuffer->nFilledLen,
1553                       PERF_ModuleHLMM);
1554#endif
1555
1556    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering EmptyThisBuffer\n", __LINE__);
1557    if (pBuffer == NULL) {
1558        eError = OMX_ErrorBadParameter;
1559        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorBadParameter\n",__LINE__);
1560        goto EXIT;
1561    }
1562
1563    if (pBuffer->nSize != sizeof(OMX_BUFFERHEADERTYPE)) {
1564        eError = OMX_ErrorBadParameter;
1565        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorBadParameter\n",__LINE__);
1566        goto EXIT;
1567    }
1568
1569    if (!pPortDef->bEnabled) {
1570        eError  = OMX_ErrorIncorrectStateOperation;
1571        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorIncorrectStateOperation\n",__LINE__);
1572        goto EXIT;
1573    }
1574
1575    if (pBuffer->nVersion.nVersion != pComponentPrivate->nVersion) {
1576        eError = OMX_ErrorVersionMismatch;
1577        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorVersionMismatch\n",__LINE__);
1578        goto EXIT;
1579    }
1580
1581    if (pBuffer->nInputPortIndex != NBAMRENC_INPUT_PORT) {
1582        eError  = OMX_ErrorBadPortIndex;
1583        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorBadPortIndex\n",__LINE__);
1584        goto EXIT;
1585    }
1586
1587    if (pComponentPrivate->curState != OMX_StateExecuting && pComponentPrivate->curState != OMX_StatePause) {
1588        eError= OMX_ErrorIncorrectStateOperation;
1589        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorIncorrectStateOperation\n",__LINE__);
1590        goto EXIT;
1591    }
1592
1593
1594    OMX_PRBUFFER2(pComponentPrivate->dbg, "----------------------------------------------------------------\n");
1595    OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Comp Sending Filled ip buff = %p to CompThread\n",__LINE__,pBuffer);
1596    OMX_PRBUFFER2(pComponentPrivate->dbg, "----------------------------------------------------------------\n");
1597
1598    pComponentPrivate->app_nBuf--;
1599    pComponentPrivate->ProcessingInputBuf++;
1600
1601    pComponentPrivate->pMarkData = pBuffer->pMarkData;
1602    pComponentPrivate->hMarkTargetComponent = pBuffer->hMarkTargetComponent;
1603
1604    pComponentPrivate->nUnhandledEmptyThisBuffers++;
1605    ret = write (pComponentPrivate->dataPipe[1], &pBuffer, sizeof(OMX_BUFFERHEADERTYPE*));
1606    if (ret == -1) {
1607        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error in Writing to the Data pipe\n", __LINE__);
1608        eError = OMX_ErrorHardware;
1609        goto EXIT;
1610    }
1611EXIT:
1612    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting EmptyThisBuffer\n", __LINE__);
1613    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1614    return eError;
1615}
1616/*-------------------------------------------------------------------*/
1617/**
1618  *  FillThisBuffer() This callback is used to send the output buffer to
1619  *  the component
1620  *
1621  * @param pComponent    handle for this instance of the component
1622  * @param nPortIndex    output port number
1623  * @param pBuffer       buffer to be sent to codec
1624  *
1625  * @retval OMX_NoError              Success, ready to roll
1626  *         OMX_Error_BadParameter   The input parameter pointer is null
1627  **/
1628/*-------------------------------------------------------------------*/
1629
1630static OMX_ERRORTYPE FillThisBuffer (OMX_HANDLETYPE pComponent,
1631                                     OMX_BUFFERHEADERTYPE* pBuffer)
1632{
1633    OMX_ERRORTYPE eError = OMX_ErrorNone;
1634    int ret = 0;
1635    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef;
1636    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
1637    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
1638                         (AMRENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1639    pPortDef = ((AMRENC_COMPONENT_PRIVATE*)pComponentPrivate)->pPortDef[NBAMRENC_OUTPUT_PORT];
1640
1641#ifdef _ERROR_PROPAGATION__
1642    if (pComponentPrivate->curState == OMX_StateInvalid){
1643        eError = OMX_ErrorInvalidState;
1644        goto EXIT;
1645    }
1646#endif
1647#ifdef __PERF_INSTRUMENTATION__
1648    PERF_ReceivedFrame(pComponentPrivate->pPERF,
1649                       pBuffer->pBuffer,
1650                       0,
1651                       PERF_ModuleHLMM);
1652#endif
1653    OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Entering FillThisBuffer\n", __LINE__);
1654    OMX_PRBUFFER2(pComponentPrivate->dbg, "------------------------------------------------------------------\n");
1655    OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Comp Sending Emptied op buff = %p to CompThread\n",__LINE__,pBuffer);
1656    OMX_PRBUFFER2(pComponentPrivate->dbg, "------------------------------------------------------------------\n");
1657    if (pBuffer == NULL) {
1658        eError = OMX_ErrorBadParameter;
1659        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorBadParameter\n",__LINE__);
1660        goto EXIT;
1661    }
1662
1663    if (pBuffer->nSize != sizeof(OMX_BUFFERHEADERTYPE)) {
1664        eError = OMX_ErrorBadParameter;
1665        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorBadParameter\n",__LINE__);
1666        goto EXIT;
1667    }
1668
1669    if (!pPortDef->bEnabled) {
1670        eError  = OMX_ErrorIncorrectStateOperation;
1671        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorIncorrectStateOperation\n",__LINE__);
1672        goto EXIT;
1673    }
1674
1675    if (pBuffer->nVersion.nVersion != pComponentPrivate->nVersion) {
1676        eError = OMX_ErrorVersionMismatch;
1677        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorVersionMismatch\n",__LINE__);
1678        goto EXIT;
1679    }
1680
1681    if (pBuffer->nOutputPortIndex != NBAMRENC_OUTPUT_PORT) {
1682        eError  = OMX_ErrorBadPortIndex;
1683        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorBadPortIndex\n",__LINE__);
1684        goto EXIT;
1685    }
1686
1687    if(pComponentPrivate->curState != OMX_StateExecuting && pComponentPrivate->curState != OMX_StatePause) {
1688        eError = OMX_ErrorIncorrectStateOperation;
1689        OMX_ERROR4(pComponentPrivate->dbg, "%d ::OMX_ErrorIncorrectStateOperation because state it is no more OMX_StatePause nor OMX_StateExecuting\n",__LINE__);
1690        goto EXIT;
1691    }
1692
1693    pComponentPrivate->app_nBuf--;
1694    if(pComponentPrivate->pMarkBuf != NULL){
1695        pBuffer->hMarkTargetComponent = pComponentPrivate->pMarkBuf->hMarkTargetComponent;
1696        pBuffer->pMarkData = pComponentPrivate->pMarkBuf->pMarkData;
1697        pComponentPrivate->pMarkBuf = NULL;
1698    }
1699
1700    if (pComponentPrivate->pMarkData != NULL) {
1701        pBuffer->hMarkTargetComponent = pComponentPrivate->hMarkTargetComponent;
1702        pBuffer->pMarkData = pComponentPrivate->pMarkData;
1703        pComponentPrivate->pMarkData = NULL;
1704    }
1705
1706
1707    pComponentPrivate->ProcessingOutputBuf++;
1708    pComponentPrivate->nUnhandledFillThisBuffers++;
1709    ret = write (pComponentPrivate->dataPipe[1], &pBuffer, sizeof (OMX_BUFFERHEADERTYPE*));
1710    if (ret == -1) {
1711        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Error in Writing to the Data pipe\n", __LINE__);
1712        eError = OMX_ErrorHardware;
1713        goto EXIT;
1714    }
1715    pComponentPrivate->nFillThisBufferCount++;
1716EXIT:
1717    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting FillThisBuffer\n", __LINE__);
1718    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1719    return eError;
1720}
1721/*-------------------------------------------------------------------*/
1722/**
1723  * OMX_ComponentDeinit() this methold will de init the component
1724  *
1725  * @param pComp         handle for this instance of the component
1726  *
1727  * @retval OMX_NoError              Success, ready to roll
1728  *         OMX_Error_BadParameter   The input parameter pointer is null
1729  **/
1730/*-------------------------------------------------------------------*/
1731
1732static OMX_ERRORTYPE ComponentDeInit(OMX_HANDLETYPE pHandle)
1733{
1734    OMX_ERRORTYPE eError = OMX_ErrorNone;
1735
1736    OMX_COMPONENTTYPE *pComponent = (OMX_COMPONENTTYPE *)pHandle;
1737    AMRENC_COMPONENT_PRIVATE *pComponentPrivate =
1738                         (AMRENC_COMPONENT_PRIVATE *)pComponent->pComponentPrivate;
1739    struct OMX_TI_Debug dbg;
1740    dbg = pComponentPrivate->dbg;
1741
1742    OMX_PRINT1(dbg, "%d :: Entering ComponentDeInit\n", __LINE__);
1743
1744#ifdef __PERF_INSTRUMENTATION__
1745    PERF_Boundary(pComponentPrivate->pPERF,
1746                  PERF_BoundaryStart | PERF_BoundaryCleanup);
1747#endif
1748
1749#ifdef DSP_RENDERING_ON
1750    close(pComponentPrivate->fdwrite);
1751    close(pComponentPrivate->fdread);
1752#endif
1753#ifdef RESOURCE_MANAGER_ENABLED
1754    /* eError = RMProxy_SendCommand(pHandle, RMProxy_FreeResource, OMX_NBAMR_Encoder_COMPONENT, 0, NULL); */
1755    eError = RMProxy_NewSendCommand(pHandle, RMProxy_FreeResource, OMX_NBAMR_Encoder_COMPONENT, 0, 3456, NULL);
1756    if (eError != OMX_ErrorNone) {
1757         OMX_ERROR4(dbg, "%d ::Error returned from destroy ResourceManagerProxy thread\n",
1758                                                        __LINE__);
1759    }
1760    eError = RMProxy_Deinitalize();
1761    if (eError != OMX_ErrorNone) {
1762         OMX_ERROR4(dbg, "%d :: Error from RMProxy_Deinitalize\n",__LINE__);
1763         goto EXIT;
1764    }
1765#endif
1766
1767    pComponentPrivate->bIsThreadstop = 1;
1768    eError = NBAMRENC_StopComponentThread(pHandle);
1769    if (eError != OMX_ErrorNone) {
1770         OMX_ERROR4(dbg, "%d :: Error from NBAMRENC_StopComponentThread\n",__LINE__);
1771         goto EXIT;
1772    }
1773    /* Wait for thread to exit so we can get the status into "eError" */
1774    /* close the pipe handles */
1775    eError = NBAMRENC_FreeCompResources(pHandle);
1776    if (eError != OMX_ErrorNone) {
1777         OMX_ERROR4(dbg, "%d :: Error from NBAMRENC_FreeCompResources\n",__LINE__);
1778         goto EXIT;
1779    }
1780
1781#ifndef UNDER_CE
1782    pthread_mutex_destroy(&pComponentPrivate->InLoaded_mutex);
1783    pthread_cond_destroy(&pComponentPrivate->InLoaded_threshold);
1784
1785    pthread_mutex_destroy(&pComponentPrivate->InIdle_mutex);
1786    pthread_cond_destroy(&pComponentPrivate->InIdle_threshold);
1787
1788    pthread_mutex_destroy(&pComponentPrivate->AlloBuf_mutex);
1789    pthread_cond_destroy(&pComponentPrivate->AlloBuf_threshold);
1790#else
1791    OMX_DestroyEvent(&(pComponentPrivate->InLoaded_event));
1792    OMX_DestroyEvent(&(pComponentPrivate->InIdle_event));
1793    OMX_DestroyEvent(&(pComponentPrivate->AlloBuf_event));
1794#endif
1795
1796    pthread_mutex_destroy(&pComponentPrivate->mutexStateChangeRequest);
1797    pthread_cond_destroy(&pComponentPrivate->StateChangeCondition);
1798#ifdef __PERF_INSTRUMENTATION__
1799    PERF_Boundary(pComponentPrivate->pPERF,
1800                  PERF_BoundaryComplete | PERF_BoundaryCleanup);
1801    PERF_Done(pComponentPrivate->pPERF);
1802#endif
1803
1804    OMX_MEMFREE_STRUCT(pComponentPrivate->sDeviceString);
1805    OMX_MEMFREE_STRUCT(pComponentPrivate);
1806
1807EXIT:
1808    OMXDBG_PRINT(stderr, PRINT, 1, 0, "%d :: Exiting ComponentDeInit Returning 0x%x\n", __LINE__, eError);
1809    return eError;
1810}
1811
1812/*-------------------------------------------------------------------*/
1813/**
1814  *  ComponentTunnelRequest() this method is not implemented in 1.5
1815  *
1816  * This method will update application callbacks
1817  * the application.
1818  *
1819  * @param pComp         handle for this instance of the component
1820  * @param pCallBacks    application callbacks
1821  * @param ptr
1822  *
1823  * @retval OMX_NoError              Success, ready to roll
1824  *         OMX_Error_BadParameter   The input parameter pointer is null
1825  **/
1826/*-------------------------------------------------------------------*/
1827
1828static OMX_ERRORTYPE ComponentTunnelRequest (OMX_HANDLETYPE hComp,
1829                                             OMX_U32 nPort, OMX_HANDLETYPE hTunneledComp,
1830                                             OMX_U32 nTunneledPort,
1831                                             OMX_TUNNELSETUPTYPE* pTunnelSetup)
1832{
1833    OMX_ERRORTYPE eError = OMX_ErrorNone;
1834    eError = OMX_ErrorNotImplemented;
1835    OMXDBG_PRINT(stderr, PRINT, 1, 0, "%d :: Entering ComponentTunnelRequest\n", __LINE__);
1836    OMXDBG_PRINT(stderr, PRINT, 1, 0, "%d :: Exiting ComponentTunnelRequest\n", __LINE__);
1837    OMXDBG_PRINT(stderr, PRINT, 1, 0, "%d :: Returning = 0x%x\n",__LINE__,eError);
1838    return eError;
1839}
1840
1841/*-------------------------------------------------------------------*/
1842/**
1843  *  AllocateBuffer()
1844
1845  * @param pComp         handle for this instance of the component
1846  * @param pCallBacks    application callbacks
1847  * @param ptr
1848  *
1849  * @retval OMX_NoError              Success, ready to roll
1850  *         OMX_Error_BadParameter   The input parameter pointer is null
1851  **/
1852/*-------------------------------------------------------------------*/
1853
1854static OMX_ERRORTYPE AllocateBuffer (OMX_IN OMX_HANDLETYPE hComponent,
1855                   OMX_INOUT OMX_BUFFERHEADERTYPE** pBuffer,
1856                   OMX_IN OMX_U32 nPortIndex,
1857                   OMX_IN OMX_PTR pAppPrivate,
1858                   OMX_IN OMX_U32 nSizeBytes)
1859
1860{
1861    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef;
1862    AMRENC_COMPONENT_PRIVATE *pComponentPrivate;
1863    OMX_ERRORTYPE eError = OMX_ErrorNone;
1864    OMX_BUFFERHEADERTYPE *pBufferHeader;
1865
1866    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
1867            (((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
1868
1869    pPortDef = ((AMRENC_COMPONENT_PRIVATE*)pComponentPrivate)->pPortDef[nPortIndex];
1870#ifdef _ERROR_PROPAGATION__
1871    if (pComponentPrivate->curState == OMX_StateInvalid){
1872        eError = OMX_ErrorInvalidState;
1873        goto EXIT;
1874    }
1875#endif
1876    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering AllocateBuffer\n", __LINE__);
1877    OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef = %p\n", __LINE__,pPortDef);
1878    OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef->bEnabled = %d\n", __LINE__,pPortDef->bEnabled);
1879    while (1) {
1880        if(pPortDef->bEnabled) {
1881            break;
1882        }
1883        pComponentPrivate->AlloBuf_waitingsignal = 1;
1884#ifndef UNDER_CE
1885        pthread_mutex_lock(&pComponentPrivate->AlloBuf_mutex);
1886        pthread_cond_wait(&pComponentPrivate->AlloBuf_threshold, &pComponentPrivate->AlloBuf_mutex);
1887        pthread_mutex_unlock(&pComponentPrivate->AlloBuf_mutex);
1888#else
1889        OMX_WaitForEvent(&(pComponentPrivate->AlloBuf_event));
1890#endif
1891        break;
1892    }
1893
1894    OMX_MALLOC_GENERIC(pBufferHeader, OMX_BUFFERHEADERTYPE);
1895
1896    OMX_MALLOC_SIZE_DSPALIGN(pBufferHeader->pBuffer, nSizeBytes, OMX_U8);
1897
1898    if (nPortIndex == NBAMRENC_INPUT_PORT) {
1899        pBufferHeader->nInputPortIndex = nPortIndex;
1900        pBufferHeader->nOutputPortIndex = -1;
1901        pComponentPrivate->pInputBufferList->pBufHdr[pComponentPrivate->pInputBufferList->numBuffers] = pBufferHeader;
1902        pComponentPrivate->pInputBufferList->bBufferPending[pComponentPrivate->pInputBufferList->numBuffers] = 0;
1903        pComponentPrivate->pInputBufferList->bufferOwner[pComponentPrivate->pInputBufferList->numBuffers++] = 1;
1904        if (pComponentPrivate->pInputBufferList->numBuffers == pPortDef->nBufferCountActual) {
1905            pPortDef->bPopulated = OMX_TRUE;
1906            OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef->bPopulated = %d\n", __LINE__, pPortDef->bPopulated);
1907        }
1908    }
1909    else if (nPortIndex == NBAMRENC_OUTPUT_PORT) {
1910        pBufferHeader->nInputPortIndex = -1;
1911        pBufferHeader->nOutputPortIndex = nPortIndex;
1912        pComponentPrivate->pOutputBufferList->pBufHdr[pComponentPrivate->pOutputBufferList->numBuffers] = pBufferHeader;
1913        pComponentPrivate->pOutputBufferList->bBufferPending[pComponentPrivate->pOutputBufferList->numBuffers] = 0;
1914        pComponentPrivate->pOutputBufferList->bufferOwner[pComponentPrivate->pOutputBufferList->numBuffers++] = 1;
1915        OMX_MALLOC_GENERIC(pBufferHeader->pOutputPortPrivate, NBAMRENC_BUFDATA);
1916
1917        if (pComponentPrivate->pOutputBufferList->numBuffers == pPortDef->nBufferCountActual) {
1918            pPortDef->bPopulated = OMX_TRUE;
1919            OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef->bPopulated = %d\n", __LINE__, pPortDef->bPopulated);
1920        }
1921    }
1922    else {
1923        eError = OMX_ErrorBadPortIndex;
1924        OMX_ERROR4(pComponentPrivate->dbg, " %d :: About to return OMX_ErrorBadPortIndex\n",__LINE__);
1925        goto EXIT;
1926    }
1927    if (((!pComponentPrivate->dasfMode) &&
1928       ((pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled)&&
1929       (pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled) &&
1930       (pComponentPrivate->InLoaded_readytoidle)))/*File Mode*/
1931       ||
1932       ((pComponentPrivate->dasfMode) &&
1933       ((pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled)&&
1934       (pComponentPrivate->InLoaded_readytoidle))))/*Dasf Mode*/
1935    {
1936        pComponentPrivate->InLoaded_readytoidle = 0;
1937#ifndef UNDER_CE
1938        pthread_mutex_lock(&pComponentPrivate->InLoaded_mutex);
1939        pthread_cond_signal(&pComponentPrivate->InLoaded_threshold);
1940        pthread_mutex_unlock(&pComponentPrivate->InLoaded_mutex);
1941#else
1942        OMX_SignalEvent(&(pComponentPrivate->InLoaded_event));
1943#endif
1944    }
1945    pBufferHeader->pAppPrivate = pAppPrivate;
1946    pBufferHeader->pPlatformPrivate = pComponentPrivate;
1947    pBufferHeader->nAllocLen = nSizeBytes;
1948    pBufferHeader->nVersion.s.nVersionMajor = NBAMRENC_MAJOR_VER;
1949    pBufferHeader->nVersion.s.nVersionMinor = NBAMRENC_MINOR_VER;
1950    pComponentPrivate->nVersion = pBufferHeader->nVersion.nVersion;
1951    pBufferHeader->nSize = sizeof(OMX_BUFFERHEADERTYPE);
1952    *pBuffer = pBufferHeader;
1953
1954    if (pComponentPrivate->bEnableCommandPending && pPortDef->bPopulated) {
1955        SendCommand (pComponentPrivate->pHandle,
1956                     OMX_CommandPortEnable,
1957                     pComponentPrivate->bEnableCommandParam,NULL);
1958    }
1959
1960
1961
1962EXIT:
1963
1964#ifdef __PERF_INSTRUMENTATION__
1965    PERF_ReceivedBuffer(pComponentPrivate->pPERF,
1966                        (*pBuffer)->pBuffer, nSizeBytes,
1967                        PERF_ModuleMemory);
1968#endif
1969
1970    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting AllocateBuffer\n",__LINE__);
1971    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
1972    return eError;
1973}
1974
1975/*-------------------------------------------------------------------*/
1976/**
1977  *  FreeBuffer()
1978
1979  * @param hComponent   handle for this instance of the component
1980  * @param pCallBacks   application callbacks
1981  * @param ptr
1982  *
1983  * @retval OMX_NoError              Success, ready to roll
1984  *         OMX_Error_BadParameter   The input parameter pointer is null
1985  **/
1986/*-------------------------------------------------------------------*/
1987
1988static OMX_ERRORTYPE FreeBuffer(
1989            OMX_IN  OMX_HANDLETYPE hComponent,
1990            OMX_IN  OMX_U32 nPortIndex,
1991            OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
1992{
1993    OMX_ERRORTYPE eError = OMX_ErrorNone;
1994    AMRENC_COMPONENT_PRIVATE * pComponentPrivate = NULL;
1995    OMX_BUFFERHEADERTYPE* buff;
1996    int i = 0;
1997    int inputIndex = -1;
1998    int outputIndex = -1;
1999    OMX_COMPONENTTYPE *pHandle;
2000
2001    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
2002                                (((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2003
2004    pHandle = (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
2005
2006    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Entering FreeBuffer\n", __LINE__);
2007    for (i=0; i < NBAMRENC_MAX_NUM_OF_BUFS; i++) {
2008        buff = pComponentPrivate->pInputBufferList->pBufHdr[i];
2009        if (buff == pBuffer) {
2010            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Found matching input buffer\n",__LINE__);
2011            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: buff = %p\n",__LINE__,buff);
2012            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
2013            inputIndex = i;
2014            break;
2015        }
2016        else {
2017            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: This is not a match\n",__LINE__);
2018            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: buff = %p\n",__LINE__,buff);
2019            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
2020        }
2021    }
2022
2023    for (i=0; i < NBAMRENC_MAX_NUM_OF_BUFS; i++) {
2024        buff = pComponentPrivate->pOutputBufferList->pBufHdr[i];
2025        if (buff == pBuffer) {
2026            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: Found matching output buffer\n",__LINE__);
2027            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: buff = %p\n",__LINE__,buff);
2028            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
2029            outputIndex = i;
2030            break;
2031        }
2032        else {
2033            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: This is not a match\n",__LINE__);
2034            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: buff = %p\n",__LINE__,buff);
2035            OMX_PRBUFFER2(pComponentPrivate->dbg, "%d :: pBuffer = %p\n",__LINE__,pBuffer);
2036        }
2037    }
2038
2039
2040    if (inputIndex != -1) {
2041        if (pComponentPrivate->pInputBufferList->bufferOwner[inputIndex] == 1) {
2042            OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pInputBufferList->pBufHdr[inputIndex]->pBuffer, OMX_U8);
2043        }
2044
2045#ifdef __PERF_INSTRUMENTATION__
2046    PERF_SendingBuffer(pComponentPrivate->pPERF,
2047                       pComponentPrivate->pInputBufferList->pBufHdr[inputIndex]->pBuffer,
2048                       pComponentPrivate->pInputBufferList->pBufHdr[inputIndex]->nAllocLen,
2049                       PERF_ModuleMemory );
2050#endif
2051
2052        OMX_MEMFREE_STRUCT(pComponentPrivate->pInputBufferList->pBufHdr[inputIndex]);
2053
2054            pComponentPrivate->pInputBufferList->numBuffers--;
2055        if (pComponentPrivate->pInputBufferList->numBuffers <
2056            pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->nBufferCountMin) {
2057
2058            pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated = OMX_FALSE;
2059            }
2060        if(pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled &&
2061               pComponentPrivate->bLoadedCommandPending == OMX_FALSE &&
2062                (pComponentPrivate->curState == OMX_StateIdle ||
2063                pComponentPrivate->curState == OMX_StateExecuting ||
2064                pComponentPrivate->curState == OMX_StatePause)) {
2065            pComponentPrivate->cbInfo.EventHandler( pHandle,
2066                                                    pHandle->pApplicationPrivate,
2067                                                    OMX_EventError,
2068                                                    OMX_ErrorPortUnpopulated,
2069                                                    OMX_TI_ErrorMinor,
2070                                                    "Input Port Unpopulated");
2071        }
2072    }
2073    else if (outputIndex != -1) {
2074         if (pComponentPrivate->pOutputBufferList->bufferOwner[outputIndex] == 1) {
2075            OMX_MEMFREE_STRUCT_DSPALIGN(pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]->pBuffer, OMX_U8);
2076        }
2077
2078#ifdef __PERF_INSTRUMENTATION__
2079    PERF_SendingBuffer(pComponentPrivate->pPERF,
2080                       pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]->pBuffer,
2081                       pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]->nAllocLen,
2082                       PERF_ModuleMemory );
2083#endif
2084
2085        OMX_MEMFREE_STRUCT(pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]->pOutputPortPrivate);
2086        OMX_MEMFREE_STRUCT(pComponentPrivate->pOutputBufferList->pBufHdr[outputIndex]);
2087
2088        pComponentPrivate->pOutputBufferList->numBuffers--;
2089        if (pComponentPrivate->pOutputBufferList->numBuffers <
2090            pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->nBufferCountMin) {
2091            pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated = OMX_FALSE;
2092        }
2093        if( pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled &&
2094            pComponentPrivate->bLoadedCommandPending == OMX_FALSE &&
2095            (pComponentPrivate->curState == OMX_StateIdle ||
2096            pComponentPrivate->curState == OMX_StateExecuting ||
2097            pComponentPrivate->curState == OMX_StatePause)) {
2098            pComponentPrivate->cbInfo.EventHandler( pHandle,
2099                                                    pHandle->pApplicationPrivate,
2100                                                    OMX_EventError,
2101                                                    OMX_ErrorPortUnpopulated,
2102                                                    OMX_TI_ErrorMinor,
2103                                                    "Output Port Unpopulated");
2104        }
2105    }
2106    else {
2107        OMX_ERROR4(pComponentPrivate->dbg, "%d :: Returning OMX_ErrorBadParameter\n",__LINE__);
2108        eError = OMX_ErrorBadParameter;
2109    }
2110
2111    pthread_mutex_lock(&pComponentPrivate->ToLoaded_mutex);
2112    OMX_PRBUFFER1(pComponentPrivate->dbg, "num input buffers es %d, \n num output buffers es %d,\n InIdle_goingtoloaded es %d \n",
2113            pComponentPrivate->pInputBufferList->numBuffers, pComponentPrivate->pOutputBufferList->numBuffers, pComponentPrivate->InIdle_goingtoloaded);
2114
2115       if ((!pComponentPrivate->pInputBufferList->numBuffers &&
2116            !pComponentPrivate->pOutputBufferList->numBuffers) &&
2117            pComponentPrivate->InIdle_goingtoloaded)
2118       {
2119#ifndef UNDER_CE
2120           pthread_mutex_lock(&pComponentPrivate->InIdle_mutex);
2121           pComponentPrivate->InIdle_goingtoloaded = 0;
2122           pthread_cond_signal(&pComponentPrivate->InIdle_threshold);
2123           pthread_mutex_unlock(&pComponentPrivate->InIdle_mutex);
2124#else
2125           OMX_SignalEvent(&(pComponentPrivate->InIdle_event));
2126#endif
2127       }
2128
2129    pthread_mutex_unlock(&pComponentPrivate->ToLoaded_mutex);
2130    if (pComponentPrivate->bDisableCommandPending &&
2131        (pComponentPrivate->pInputBufferList->numBuffers + pComponentPrivate->pOutputBufferList->numBuffers == 0)) {
2132            SendCommand (pComponentPrivate->pHandle,OMX_CommandPortDisable,pComponentPrivate->bDisableCommandParam,NULL);
2133    }
2134    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting FreeBuffer\n", __LINE__);
2135    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
2136    return eError;
2137}
2138
2139/*-------------------------------------------------------------------*/
2140/**
2141  *  UseBuffer()
2142
2143  * @param pComp         handle for this instance of the component
2144  * @param pCallBacks    application callbacks
2145  * @param ptr
2146  *
2147  * @retval OMX_NoError              Success, ready to roll
2148  *         OMX_Error_BadParameter   The input parameter pointer is null
2149  **/
2150/*-------------------------------------------------------------------*/
2151static OMX_ERRORTYPE UseBuffer (
2152            OMX_IN OMX_HANDLETYPE hComponent,
2153            OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
2154            OMX_IN OMX_U32 nPortIndex,
2155            OMX_IN OMX_PTR pAppPrivate,
2156            OMX_IN OMX_U32 nSizeBytes,
2157            OMX_IN OMX_U8* pBuffer)
2158{
2159    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef;
2160    AMRENC_COMPONENT_PRIVATE *pComponentPrivate;
2161    OMX_ERRORTYPE eError = OMX_ErrorNone;
2162    OMX_BUFFERHEADERTYPE *pBufferHeader;
2163
2164    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)
2165            (((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2166
2167#ifdef _ERROR_PROPAGATION__
2168    if (pComponentPrivate->curState == OMX_StateInvalid){
2169        eError = OMX_ErrorInvalidState;
2170        goto EXIT;
2171    }
2172
2173#endif
2174#ifdef __PERF_INSTRUMENTATION__
2175    PERF_ReceivedBuffer(pComponentPrivate->pPERF,
2176                        pBuffer, nSizeBytes,
2177                        PERF_ModuleHLMM);
2178#endif
2179
2180    pPortDef = ((AMRENC_COMPONENT_PRIVATE*)pComponentPrivate)->pPortDef[nPortIndex];
2181    OMX_PRBUFFER1(pComponentPrivate->dbg, "%d :: Entering UseBuffer\n", __LINE__);
2182    OMX_PRCOMM1(pComponentPrivate->dbg, "%d :: pPortDef->bPopulated = %d \n",__LINE__,pPortDef->bPopulated);
2183
2184    if(!pPortDef->bEnabled) {
2185        OMX_ERROR4(pComponentPrivate->dbg, "%d :: About to return OMX_ErrorIncorrectStateOperation\n",__LINE__);
2186        eError = OMX_ErrorIncorrectStateOperation;
2187        goto EXIT;
2188    }
2189
2190    OMX_MALLOC_GENERIC(pBufferHeader, OMX_BUFFERHEADERTYPE);
2191
2192    if (nPortIndex == NBAMRENC_OUTPUT_PORT) {
2193        pBufferHeader->nInputPortIndex = -1;
2194        pBufferHeader->nOutputPortIndex = nPortIndex;
2195        pComponentPrivate->pOutputBufferList->pBufHdr[pComponentPrivate->pOutputBufferList->numBuffers] = pBufferHeader;
2196        pComponentPrivate->pOutputBufferList->bBufferPending[pComponentPrivate->pOutputBufferList->numBuffers] = 0;
2197        pComponentPrivate->pOutputBufferList->bufferOwner[pComponentPrivate->pOutputBufferList->numBuffers++] = 0;
2198        OMX_MALLOC_GENERIC(pBufferHeader->pOutputPortPrivate, NBAMRENC_BUFDATA);
2199
2200        if (pComponentPrivate->pOutputBufferList->numBuffers == pPortDef->nBufferCountActual) {
2201            pPortDef->bPopulated = OMX_TRUE;
2202        }
2203    }
2204    else {
2205        pBufferHeader->nInputPortIndex = nPortIndex;
2206        pBufferHeader->nOutputPortIndex = -1;
2207        pComponentPrivate->pInputBufferList->pBufHdr[pComponentPrivate->pInputBufferList->numBuffers] = pBufferHeader;
2208        pComponentPrivate->pInputBufferList->bBufferPending[pComponentPrivate->pInputBufferList->numBuffers] = 0;
2209        pComponentPrivate->pInputBufferList->bufferOwner[pComponentPrivate->pInputBufferList->numBuffers++] = 0;
2210        if (pComponentPrivate->pInputBufferList->numBuffers == pPortDef->nBufferCountActual) {
2211            pPortDef->bPopulated = OMX_TRUE;
2212        }
2213    }
2214
2215    if (((!pComponentPrivate->dasfMode) &&
2216       ((pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled)&&
2217       (pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_INPUT_PORT]->bEnabled) &&
2218       (pComponentPrivate->InLoaded_readytoidle)))/*File Mode*/
2219       ||
2220       ((pComponentPrivate->dasfMode) &&
2221       ((pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bPopulated == pComponentPrivate->pPortDef[NBAMRENC_OUTPUT_PORT]->bEnabled)&&
2222       (pComponentPrivate->InLoaded_readytoidle))))/*Dasf Mode*/
2223    {
2224        pComponentPrivate->InLoaded_readytoidle = 0;
2225#ifndef UNDER_CE
2226        pthread_mutex_lock(&pComponentPrivate->InLoaded_mutex);
2227        pthread_cond_signal(&pComponentPrivate->InLoaded_threshold);
2228        pthread_mutex_unlock(&pComponentPrivate->InLoaded_mutex);
2229#else
2230        OMX_SignalEvent(&(pComponentPrivate->InLoaded_event));
2231#endif
2232    }
2233    pBufferHeader->pAppPrivate = pAppPrivate;
2234    pBufferHeader->pPlatformPrivate = pComponentPrivate;
2235    pBufferHeader->nAllocLen = nSizeBytes;
2236    pBufferHeader->nVersion.s.nVersionMajor = NBAMRENC_MAJOR_VER;
2237    pBufferHeader->nVersion.s.nVersionMinor = NBAMRENC_MINOR_VER;
2238    pComponentPrivate->nVersion = pBufferHeader->nVersion.nVersion;
2239    pBufferHeader->pBuffer = pBuffer;
2240    pBufferHeader->nSize = sizeof(OMX_BUFFERHEADERTYPE);
2241    *ppBufferHdr = pBufferHeader;
2242
2243    if (pComponentPrivate->bEnableCommandPending){
2244        SendCommand (pComponentPrivate->pHandle,
2245                     OMX_CommandPortEnable,
2246                     pComponentPrivate->bEnableCommandParam,NULL);
2247    }
2248
2249EXIT:
2250    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Exiting UseBuffer\n", __LINE__);
2251    OMX_PRINT1(pComponentPrivate->dbg, "%d :: Returning = 0x%x\n",__LINE__,eError);
2252    return eError;
2253}
2254
2255/* ================================================================================= */
2256/**
2257* @fn GetExtensionIndex() description for GetExtensionIndex
2258GetExtensionIndex().
2259Returns index for vendor specific settings.
2260*
2261*  @see         OMX_Core.h
2262*/
2263/* ================================================================================ */
2264static OMX_ERRORTYPE GetExtensionIndex(
2265            OMX_IN  OMX_HANDLETYPE hComponent,
2266            OMX_IN  OMX_STRING cParameterName,
2267            OMX_OUT OMX_INDEXTYPE* pIndexType)
2268{
2269    OMX_ERRORTYPE eError = OMX_ErrorNone;
2270
2271    OMXDBG_PRINT(stderr, PRINT, 1, 0, "GetExtensionIndex\n");
2272
2273    if (!(strcmp(cParameterName,"OMX.TI.index.config.tispecific"))) {
2274        *pIndexType = OMX_IndexCustomNBAMRENCModeConfig;
2275        OMXDBG_PRINT(stderr, PRINT, 2, 0, "OMX_IndexCustomNBAMRENCModeConfig\n");
2276    }
2277    else if(!(strcmp(cParameterName,"OMX.TI.index.config.nbamrstreamIDinfo"))) {
2278        *pIndexType = OMX_IndexCustomNBAMRENCStreamIDConfig;
2279    }
2280    else if(!(strcmp(cParameterName,"OMX.TI.index.config.nbamr.datapath")))
2281    {
2282        *pIndexType = OMX_IndexCustomNBAMRENCDataPath;
2283        }
2284    else if(!(strcmp(cParameterName,"OMX.TI.AMR.Encode.Debug")))
2285    {
2286        *pIndexType = OMX_IndexCustomDebug;
2287        }
2288    else {
2289        eError = OMX_ErrorBadParameter;
2290    }
2291
2292    OMXDBG_PRINT(stderr, PRINT, 1, 0, "Exiting GetExtensionIndex\n");
2293    return eError;
2294}
2295
2296#ifdef UNDER_CE
2297/* ================================================================================= */
2298/**
2299* @fns Sleep replace for WIN CE
2300*/
2301/* ================================================================================ */
2302int OMX_CreateEvent(OMX_Event *event){
2303    int ret = OMX_ErrorNone;
2304    HANDLE createdEvent = NULL;
2305    if(event == NULL){
2306        ret = OMX_ErrorBadParameter;
2307        goto EXIT;
2308    }
2309    event->event  = CreateEvent(NULL, TRUE, FALSE, NULL);
2310    if(event->event == NULL)
2311        ret = (int)GetLastError();
2312EXIT:
2313    return ret;
2314}
2315
2316int OMX_SignalEvent(OMX_Event *event){
2317     int ret = OMX_ErrorNone;
2318     if(event == NULL){
2319        ret = OMX_ErrorBadParameter;
2320        goto EXIT;
2321     }
2322     SetEvent(event->event);
2323     ret = (int)GetLastError();
2324EXIT:
2325    return ret;
2326}
2327
2328int OMX_WaitForEvent(OMX_Event *event) {
2329     int ret = OMX_ErrorNone;
2330     if(event == NULL){
2331        ret = OMX_ErrorBadParameter;
2332        goto EXIT;
2333     }
2334     WaitForSingleObject(event->event, INFINITE);
2335     ret = (int)GetLastError();
2336EXIT:
2337     return ret;
2338}
2339
2340int OMX_DestroyEvent(OMX_Event *event) {
2341     int ret = OMX_ErrorNone;
2342     if(event == NULL){
2343        ret = OMX_ErrorBadParameter;
2344        goto EXIT;
2345     }
2346     CloseHandle(event->event);
2347EXIT:
2348     return ret;
2349}
2350#endif
2351
2352/* ================================================================================= */
2353/**
2354* @fn ComponentRoleEnum() description for ComponentRoleEnum()
2355
2356Returns the role at the given index
2357*
2358*  @see         OMX_Core.h
2359*/
2360/* ================================================================================ */
2361static OMX_ERRORTYPE ComponentRoleEnum(
2362         OMX_IN OMX_HANDLETYPE hComponent,
2363      OMX_OUT OMX_U8 *cRole,
2364      OMX_IN OMX_U32 nIndex)
2365{
2366    AMRENC_COMPONENT_PRIVATE *pComponentPrivate;
2367
2368    OMX_ERRORTYPE eError = OMX_ErrorNone;
2369    pComponentPrivate = (AMRENC_COMPONENT_PRIVATE *)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2370
2371    if(nIndex == 0){
2372      if (cRole == NULL) {
2373          eError = OMX_ErrorBadParameter;
2374      }
2375      else {
2376          memcpy(cRole, &pComponentPrivate->componentRole.cRole, sizeof(OMX_U8) * OMX_MAX_STRINGNAME_SIZE);
2377          OMX_PRINT1(pComponentPrivate->dbg, "::::In ComponenetRoleEnum: cRole is set to %s\n",cRole);
2378      }
2379    }
2380    else {
2381      eError = OMX_ErrorNoMore;
2382        }
2383    return eError;
2384}
2385