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