Home | History | Annotate | Download | only in tests
      1 
      2 /*
      3  *  Copyright 2001-2008 Texas Instruments - http://www.ti.com/
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 /*
     18  *    LIBRARY INCLUDE
     19  */
     20 
     21 #ifdef UNDER_CE
     22 #include <windows.h>
     23 #else
     24 #include <sys/ioctl.h>
     25 #include <sys/select.h>
     26 #include <pthread.h>
     27 #include <linux/vt.h>
     28 #include <signal.h>
     29 #include <sys/stat.h>
     30 #include <linux/soundcard.h>
     31 #endif
     32 
     33 #include <unistd.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <stdio.h>
     37 #include <fcntl.h>
     38 #include <errno.h>
     39 
     40 #include <OMX_Index.h>
     41 #include <OMX_Types.h>
     42 #include <OMX_Core.h>
     43 #include <OMX_Component.h>
     44 #include <OMX_Audio.h>
     45 #include <TIDspOmx.h>
     46 #include <stdio.h>
     47 #ifdef DSP_RENDERING_ON
     48 #include <AudioManagerAPI.h>
     49 #endif
     50 #ifdef OMX_GETTIME
     51 #include <OMX_Common_Utils.h>
     52 #include <OMX_GetTime.h>
     53 #endif
     54 /*
     55  *     M A C R O S
     56  */
     57 
     58 #undef APP_DEBUG
     59 #define APP_INFO
     60 #define APP_ERROR
     61 #define DASF
     62 #define USE_BUFFER
     63 #undef AACENC_DEBUGMEM
     64 
     65 /*#define GT_PERFM  *//*Defines the Performance and measurements mode*/
     66 /*#undef GT_PERFM Defines the Performance and measurements mode*/
     67 
     68 #ifdef  APP_INFO
     69         #define APP_IPRINT(...)    fprintf(stderr,__VA_ARGS__)			/* Information prints */
     70 #else
     71         #define APP_IPRINT(...)
     72 #endif
     73 
     74 
     75 #ifdef  APP_ERROR
     76         #define APP_EPRINT(...)    fprintf(stderr,__VA_ARGS__)			/* errors & warnings prints */
     77 #else
     78         #define APP_EPRINT(...)
     79 #endif
     80 
     81 
     82 #ifdef  APP_DEBUG
     83         #define APP_DPRINT(...)    fprintf(stderr,__VA_ARGS__)			/* Debug prints */
     84 #else
     85         #define APP_DPRINT(...)
     86 #endif
     87 
     88 #ifdef OMX_GETTIME
     89   OMX_ERRORTYPE eError = OMX_ErrorNone;
     90   int GT_FlagE = 0;  /* Fill Buffer 1 = First Buffer,  0 = Not First Buffer  */
     91   int GT_FlagF = 0;  /*Empty Buffer  1 = First Buffer,  0 = Not First Buffer  */
     92   static OMX_NODE* pListHead = NULL;
     93 #endif
     94 
     95 
     96 #ifdef APP_MEMCHECK
     97     #define APP_MEMPRINT(...)    fprintf(stderr,__VA_ARGS__)
     98 #else
     99     #define APP_MEMPRINT(...)
    100 #endif
    101 
    102 #define MONO 			 1
    103 #define STEREO 			 2
    104 #define SLEEP_TIME  	 10
    105 #define INPUT_PORT  	 0
    106 #define OUTPUT_PORT 	 1
    107 #define MAX_NUM_OF_BUFS  5
    108 #define INPUT_AACENC_BUFFER_SIZE 8192
    109 #define FIFO1 "/dev/fifo.1"
    110 #define FIFO2 "/dev/fifo.2"
    111 
    112 #define Min_8Kbps 		 8000
    113 #define Max_576Kbps 	 576000
    114 #define Min_volume 		 0										/* Minimum volume 					*/
    115 #define Act_volume 		 50										/* Current volume 					*/
    116 #define Max_volume 		 100
    117 
    118 #define Max_48Kbps		 48000
    119 #define Max_64Kbps		 64000
    120 
    121 #define ObjectTypeLC	 2
    122 #define ObjectTypeHE	 5
    123 #define ObjectTypeHE2	 29
    124 
    125 #define BITS16			 16										/* unmasking  command line parameter */
    126 #define Upto48kHz		 48000
    127 
    128 #define newmalloc(x) mymalloc((x),(&ListHeader))				/* new prototype of malloc function 	*/
    129 #define newfree(z) myfree((z),(&ListHeader))					/* new prototype of free function 		*/
    130 
    131 
    132 #undef  WAITFORRESOURCES
    133 
    134 #ifdef AACENC_DEBUGMEM
    135 void *arr[500];
    136 int lines[500];
    137 int bytes[500];
    138 char file[500][50];
    139 int r=0;
    140 #endif
    141 
    142 /*
    143  *    TYPE DEFINITIONS
    144  */
    145 
    146 /* Structure for Linked List */
    147 typedef struct DataList ListMember;
    148 struct DataList
    149 {
    150 	int ListCounter;											/* Instance Counter 					*/
    151 	void* Struct_Ptr;											/* Pointer to new alocate data 		*/
    152 	ListMember* NextListMember;									/* Pointer to next instance	 		*/
    153 };
    154 
    155 typedef enum COMPONENTS
    156 {
    157     COMP_1,
    158     COMP_2
    159 }COMPONENTS;
    160 
    161 /* Ouput file format */
    162 typedef enum FILE_FORMAT
    163 {
    164     RAW = 0,
    165     ADIF,
    166 	ADTS
    167 }FILE_FORMAT;
    168 
    169 /*Structure for Wait for state sync */
    170 typedef struct
    171 {
    172 	OMX_BOOL WaitForStateFlag;									/* flag  				 */
    173 	pthread_cond_t  cond; 										/* conditional mutex 	*/
    174 	pthread_mutex_t Mymutex;									/* Mutex 			*/
    175 }Mutex;
    176 
    177 
    178 /*
    179  *    FUNTIONS DECLARATION
    180  */
    181 
    182 OMX_ERRORTYPE AddMemberToList(void* ptr, ListMember** ListHeader);
    183 
    184 OMX_ERRORTYPE FreeListMember(void* ptr, ListMember** ListHeader);
    185 
    186 OMX_ERRORTYPE CleanList(ListMember** ListHeader);
    187 
    188 void * mymalloc(int size, ListMember** ListHeader );
    189 
    190 int myfree(void *dp, ListMember** ListHeader);
    191 
    192 static OMX_ERRORTYPE FreeAllResources( OMX_HANDLETYPE pHandle,
    193 			                OMX_BUFFERHEADERTYPE* pBufferIn,
    194 			                OMX_BUFFERHEADERTYPE* pBufferOut,
    195 			                int NIB, int NOB,
    196 			                FILE* fIn, FILE* fOut,
    197 			                ListMember* ListHeader);
    198 
    199 #ifdef USE_BUFFER
    200 static OMX_ERRORTYPE  freeAllUseResources(OMX_HANDLETYPE pHandle,
    201 						  OMX_U8* UseInpBuf[],
    202 						  OMX_U8* UseOutBuf[],
    203 						  int NIB, int NOB,
    204 						  FILE* fIn, FILE* fOut,
    205 						  ListMember* ListHeader );
    206 
    207 #endif
    208 
    209 OMX_BOOL ValidateParameters(OMX_U32 SampleRate, OMX_U32 numChannels, OMX_U32 BitRate, OMX_U32 ObjectType);
    210 
    211 OMX_U32 CalculateOutputBufferSize (OMX_U32 SampleFrec, OMX_U32 BitRate, OMX_U16 FramesPerOutBuf );
    212 
    213 
    214 
    215 /*
    216  *   GLOBAL VARIBLES
    217  */
    218 
    219 int IpBuf_Pipe[2];
    220 int OpBuf_Pipe[2];
    221 int Event_Pipe[2];
    222 
    223 int channel = 0;
    224 int ObjectType=0;
    225 
    226 int preempted = 0;
    227 int firstbuffer = 1;
    228 
    229 #ifdef DSP_RENDERING_ON
    230 AM_COMMANDDATATYPE cmd_data;
    231 #endif
    232 OMX_STRING strAacEncoder = "OMX.TI.AAC.encode";
    233 
    234 
    235 /* flag for Invalid State condition*/
    236 static OMX_BOOL bInvalidState;
    237 
    238 /* New instance of Mutex Structure */
    239 Mutex WaitForStateMutex={OMX_TRUE,PTHREAD_COND_INITIALIZER,PTHREAD_MUTEX_INITIALIZER};
    240 
    241 /* Flags for mutex control */
    242 OMX_BOOL bWaiting = OMX_FALSE;
    243 OMX_STATETYPE waiting_state=OMX_StateInvalid;
    244 
    245 /* Flag to stop component */
    246 OMX_BOOL bPlayCompleted = OMX_FALSE;
    247 
    248 
    249 /*
    250  *   FUNCTIONS DEFINITION
    251  */
    252 
    253 
    254 /*-------------------------------------------------------------------*/
    255 /**
    256   * maxint()  Returns the biggest from two number
    257   *
    258   *  @param a					First Integer number
    259   *  @param  b					Second Integer number
    260   *
    261   * @retval 						The biggest number
    262   *
    263   **/
    264 /*-------------------------------------------------------------------*/
    265 int maxint(int a, int b)
    266 {
    267    return (a>b) ? a : b;
    268 }
    269 
    270 
    271 /*-------------------------------------------------------------------*/
    272 /**
    273   * WaitForState()  Waits for signal state transition if  the transition has not ocurred
    274   *
    275   *  @param pHandle					Component pointer
    276   *		      DesiredState				State to wait
    277   *
    278   * @retval OMX_ErrorNone   			Success on Transition
    279   *              OMX_StateInvalid		 	Wrong transition
    280   **/
    281 /*-------------------------------------------------------------------*/
    282 
    283 static OMX_ERRORTYPE WaitForState(OMX_HANDLETYPE pHandle,
    284                                 OMX_STATETYPE DesiredState)
    285 {
    286      OMX_STATETYPE CurState = OMX_StateInvalid;
    287      OMX_ERRORTYPE eError   = OMX_ErrorNone;
    288 
    289 	 APP_DPRINT("%d: APP: waiting for %d \n",__LINE__,DesiredState);
    290      eError = OMX_GetState(pHandle, &CurState);
    291 	 if(eError !=OMX_ErrorNone)
    292 	 {
    293 		APP_EPRINT("App: Error in GetState from WaitForState() \n" );
    294 		goto EXIT;
    295 	 }
    296 
    297    	 if (CurState != DesiredState)
    298    	 {
    299    		 bWaiting	   = OMX_TRUE;													/*	flag is enable since now we have to wait to the event */
    300    		 waiting_state = DesiredState;
    301 		 APP_DPRINT("Now is waiting.... \n");
    302 
    303    		 pthread_mutex_lock(&WaitForStateMutex.Mymutex);
    304    		 pthread_cond_wait(&WaitForStateMutex.cond,&WaitForStateMutex.Mymutex);		/*  Block on a Condition Variable"  */
    305    		 pthread_mutex_unlock( &WaitForStateMutex.Mymutex);
    306    	 }
    307    	 else if(CurState == DesiredState)
    308    	 {
    309 		APP_DPRINT("...No need to wait \n");
    310    		 eError = OMX_ErrorNone;
    311    	 }
    312 
    313 
    314 EXIT:
    315 	return eError;
    316 }
    317 
    318 
    319 /*-------------------------------------------------------------------*/
    320 /**
    321   * EventHandler()  Event Callback from Component. Method to notify when an event of
    322   *				   interest occurs within the component.
    323   *
    324   *  @param hComponent				The handle of the component that is calling this function.
    325   *			pAppData				additional event-specific data.
    326   *			eEvent					The event that the component is communicating
    327   *			nData1					The first integer event-specific parameter.
    328   *			nData2					The second integer event-specific parameter.
    329   *
    330   * @retval OMX_ErrorNone   				Success, Event Gotten
    331   *              OMX_ErrorBadParameter		Error on parameters
    332   **/
    333 /*-------------------------------------------------------------------*/
    334 
    335 OMX_ERRORTYPE EventHandler(OMX_HANDLETYPE hComponent,
    336 				  OMX_PTR pAppData,
    337                   OMX_EVENTTYPE eEvent,
    338                   OMX_U32 nData1,
    339                   OMX_U32 nData2,
    340                   OMX_PTR pEventData)
    341 {
    342 
    343    APP_DPRINT( "%d :: App: Entering EventHandler \n", __LINE__);
    344    OMX_STATETYPE state;
    345    OMX_ERRORTYPE eError = OMX_ErrorNone;
    346 
    347    OMX_U8 writeValue;
    348 
    349 #ifdef  APP_DEBUG
    350    int iComp = *((int *)(pAppData));
    351 #endif
    352 
    353    eError = OMX_GetState (hComponent, &state);
    354    if(eError != OMX_ErrorNone)
    355    {
    356        APP_DPRINT("%d :: App: Error returned from GetState\n",__LINE__);
    357 	   goto EXIT;
    358    }
    359 
    360    APP_DPRINT( "%d :: App: Component eEvent = %d\n", __LINE__,eEvent);
    361    switch (eEvent) {
    362        case OMX_EventCmdComplete:
    363 
    364 	   if( (waiting_state== nData2) && (nData1 == OMX_CommandStateSet) && (bWaiting) )		/* ensure that came from transition */
    365 		   {
    366 					bWaiting = OMX_FALSE;
    367 					pthread_mutex_lock(&WaitForStateMutex.Mymutex);
    368 					pthread_cond_signal(&WaitForStateMutex.cond);								/* Unblock a Specific Thread" */
    369 					APP_DPRINT("App: Mutex signal sent \n");
    370 					pthread_mutex_unlock( &WaitForStateMutex.Mymutex);							/*  unlock.  */
    371 
    372 					APP_DPRINT ("%d :: App: Component State Changed To %d\n", __LINE__,state);
    373 		   }
    374 
    375 		   APP_DPRINT(	"%d :: App: OMX_EventCmdComplete %d\n", __LINE__,eEvent);
    376            break;
    377        case OMX_EventError:
    378            if (nData1 != OMX_ErrorNone)
    379 		   {
    380                APP_DPRINT ("%d:: App: ErrorNotofication Came: \
    381                            Component Name : %d : Error Num %x \n",
    382                            __LINE__,iComp, (unsigned int)nData1);
    383            }
    384 		   if (nData1 == OMX_ErrorInvalidState) {
    385 		   		bInvalidState =OMX_TRUE;
    386 		   }
    387 		   else if(nData1 == OMX_ErrorResourcesPreempted) {
    388             preempted=1;
    389             writeValue = 0;
    390             write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
    391 	       }
    392 
    393 	       else if (nData1 == OMX_ErrorResourcesLost) {
    394 	            bWaiting = 0;
    395 	            pthread_mutex_lock(&WaitForStateMutex.Mymutex);
    396 	            pthread_cond_signal(&WaitForStateMutex.cond);/*Sending Waking Up Signal*/
    397 	            pthread_mutex_unlock(&WaitForStateMutex.Mymutex);
    398 	        }
    399 
    400            break;
    401        case OMX_EventMax:
    402 		   APP_DPRINT( "%d :: App: Component OMX_EventMax = %d\n", __LINE__,eEvent);
    403            break;
    404        case OMX_EventMark:
    405 		   APP_DPRINT( "%d :: App: Component OMX_EventMark = %d\n", __LINE__,eEvent);
    406            break;
    407        case OMX_EventPortSettingsChanged:
    408 		   APP_DPRINT( "%d :: App: Component OMX_EventPortSettingsChanged = %d\n", __LINE__,eEvent);
    409        	   break;
    410        case OMX_EventBufferFlag:
    411 		   APP_DPRINT( "%d :: App: Component OMX_EventBufferFlag = %d\n", __LINE__,eEvent);
    412 		   APP_DPRINT( "%d :: App: OMX_EventBufferFlag on port = %d\n", __LINE__, (int)nData1);
    413 		   /* event for the input port to stop component  , since there is one for output port */
    414 		   if(nData1 == INPUT_PORT && nData2 == OMX_BUFFERFLAG_EOS)
    415 		   {
    416 		   		bPlayCompleted = OMX_TRUE;
    417 			    APP_DPRINT( "%d :: App: Setting flag for playcompleted \n", __LINE__);
    418 		   }
    419 
    420 		   #ifdef WAITFORRESOURCES
    421 		   writeValue = 2;
    422 	       write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
    423 		   #endif
    424 
    425        	   break;
    426        case OMX_EventResourcesAcquired:
    427 		   APP_DPRINT( "%d :: App: Component OMX_EventResourcesAcquired = %d\n", __LINE__,eEvent);
    428            writeValue = 1;
    429            write(Event_Pipe[1], &writeValue, sizeof(OMX_U8));
    430            preempted=0;
    431 
    432        	   break;
    433 	   default:
    434 		   break;
    435    }
    436 EXIT:
    437 	APP_DPRINT( "%d :: App: Exiting EventHandler \n", __LINE__);
    438 	return eError;
    439 
    440 }
    441 
    442 
    443 /*-------------------------------------------------------------------*/
    444 /**
    445   * FillBufferDone()  Callback from component which returns an ouput buffer
    446   *
    447   *  @param hComponent				The handle of the component that is calling this function.
    448   *			pBuffer					Returned output buffer
    449   *			ptr						A pointer to IL client-defined data
    450   *
    451   *
    452   * @retval  None
    453   *
    454   **/
    455 /*-------------------------------------------------------------------*/
    456 
    457 void FillBufferDone (OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
    458 {
    459     write(OpBuf_Pipe[1], &pBuffer, sizeof(pBuffer));
    460    #ifdef OMX_GETTIME
    461       if (GT_FlagF == 1 ) /* First Buffer Reply*/  /* 1 = First Buffer,  0 = Not First Buffer  */
    462       {
    463         GT_END("Call to FillBufferDone  <First: FillBufferDone>");
    464         GT_FlagF = 0 ;   /* 1 = First Buffer,  0 = Not First Buffer  */
    465       }
    466     #endif
    467 }
    468 
    469 
    470 /*-------------------------------------------------------------------*/
    471 /**
    472   * EmptyBufferDone()  Callback from component which returns an input buffer
    473   *
    474   *  @param hComponent				The handle of the component that is calling this function.
    475   *			pBuffer					Returned input buffer
    476   *			ptr						A pointer to IL client-defined data
    477   *
    478   *
    479   * @retval  None
    480   *
    481   **/
    482 /*-------------------------------------------------------------------*/
    483 
    484 void EmptyBufferDone(OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
    485 {
    486    APP_DPRINT("%d:: APP: Inside empty buffer done \n",__LINE__);
    487 
    488 	if (!preempted)
    489 	   write(IpBuf_Pipe[1], &pBuffer, sizeof(pBuffer));
    490 
    491    	#ifdef OMX_GETTIME
    492       if (GT_FlagE == 1 ) /* First Buffer Reply*/  /* 1 = First Buffer,  0 = Not First Buffer  */
    493       {
    494         GT_END("Call to EmptyBufferDone <First: EmptyBufferDone>");
    495        GT_FlagE = 0;   /* 1 = First Buffer,  0 = Not First Buffer  */
    496       }
    497    #endif
    498 }
    499 
    500 
    501 
    502 /*-------------------------------------------------------------------*/
    503 /**
    504   * main()  Test App main function. Function called from the runtime startup routine after
    505   		   the runtime environment has been initialized
    506   *
    507   *  @param argv				Argument vector. Pointer to an array of string pointers passed to  main function
    508   *		      argc				Holds the number of arguments that are passed.
    509   *
    510   *
    511   *
    512   * @retval   error 			Return Value to OS
    513   *
    514   **/
    515 /*-------------------------------------------------------------------*/
    516 
    517 int main(int argc, char* argv[])
    518 {
    519 
    520 
    521     struct timeval tv;
    522     int retval,i =0;
    523     int frmCount = 0;
    524 	int testcnt  = 1;
    525 	int testcnt1 = 1;
    526     char fname[20] = "output";
    527 	int nRead = 0;
    528 	int done  = 0;
    529 	int nIpBuffs = 0;
    530 	int nOpBuffs = 0;
    531 	int numofinbuff  = 0;
    532 	int numofoutbuff = 0;
    533 	int frmCnt = 1;
    534     int status,fdmax,jj,kk,k;
    535     int nFrameCount = 0;
    536 
    537     OMX_CALLBACKTYPE AacCaBa = {(void *)EventHandler,(void*) EmptyBufferDone,
    538                                                      (void*)FillBufferDone};
    539     OMX_HANDLETYPE *pHandle = NULL;
    540     OMX_ERRORTYPE  error = OMX_ErrorNone;
    541     OMX_U32 AppData = 100;
    542     OMX_PARAM_PORTDEFINITIONTYPE* pCompPrivateStruct =NULL;
    543     OMX_AUDIO_PARAM_AACPROFILETYPE *pAacParam =NULL;
    544 	OMX_AUDIO_PARAM_PCMMODETYPE *iAacParam =NULL;
    545     OMX_COMPONENTTYPE *pComponent=NULL;
    546     OMX_STATETYPE state;
    547 	TI_OMX_DSP_DEFINITION audioinfo;
    548 	OMX_AUDIO_CONFIG_VOLUMETYPE* pCompPrivateStructGain = NULL;
    549     OMX_BUFFERHEADERTYPE* pInputBufferHeader[MAX_NUM_OF_BUFS];
    550     OMX_BUFFERHEADERTYPE* pOutputBufferHeader[MAX_NUM_OF_BUFS];
    551 	OMX_INDEXTYPE index;
    552 	TI_OMX_STREAM_INFO *streaminfo =NULL;
    553 	OMX_U32 ArrValidFrecLC[9]={8000,11025,16000,22050,32000,44100,48000,88200,96000};
    554 	OMX_U16 NumOfFrecsLC=9;
    555 	OMX_U32 ArrValidFrecHE[6]={16000,22050,24000,32000,44100,48000};
    556 	OMX_U16 NumOfFrecsHE=6;
    557 	OMX_U16 FramesPerOutBuf  =12;							/* desired number of frames                                 */
    558 	OMX_U32 OutputBufferSize =0;							/* Calculated size per Ouput buffer                      */
    559 	bInvalidState=OMX_FALSE;								/* flag for invalid state transition                          */
    560 	OMX_BOOL isValidCombination ;							/* flag for  bit rate and sample rate combination  */
    561     TI_OMX_DATAPATH dataPath;
    562 	ListMember* ListHeader = NULL;							/* Linked list Header							 */
    563 	FILE* fIn = NULL;										/* input File pointer 							 */
    564     FILE* fOut= NULL;										/* ouput File pointer 							 */
    565 #ifdef DSP_RENDERING_ON
    566 int Aacenc_fdwrite;
    567 int Aacenc_fdread;
    568 #endif
    569 
    570 	/*----------------------------------------------
    571  	First allocated structure
    572  	----------------------------------------------*/
    573 
    574 	streaminfo = newmalloc(sizeof(TI_OMX_STREAM_INFO));
    575 	if(NULL == streaminfo)
    576 	{
    577         APP_EPRINT("%d :: App: Malloc Failed\n",__LINE__);
    578         goto EXIT;
    579 	}
    580 
    581 	OMX_U32	streamId;
    582 #ifdef USE_BUFFER
    583     OMX_U8* pInputBuffer[5];
    584     OMX_U8* pOutputBuffer[5];
    585 #endif
    586 
    587     fd_set rfds;
    588 
    589 	APP_IPRINT("------------------------------------------------------------\n");
    590     APP_IPRINT("This is Main Thread In MPEG AAC ENCODER Test Application:\n");
    591     APP_IPRINT("Test Core 1.5 - " __DATE__ ":" __TIME__ "\n");
    592     APP_IPRINT("------------------------------------------------------------\n");
    593 
    594 #ifdef OMX_GETTIME
    595     APP_IPRINT("Line %d\n",__LINE__);
    596       GTeError = OMX_ListCreate(&pListHead);
    597         APP_IPRINT("Line %d\n",__LINE__);
    598       APP_IPRINT("eError = %d\n",GTeError);
    599       GT_START();
    600   APP_IPRINT("Line %d\n",__LINE__);
    601 #endif
    602 
    603 #ifdef USE_BUFFER
    604 	APP_IPRINT("                     Use Buffer Enabled\n");
    605 	APP_IPRINT("------------------------------------------------------------\n");
    606 #endif
    607 
    608 	/* check to see the number of parameters from the command line */
    609     if(  (argc < 14) || (argc > 15)  )
    610 	{
    611         APP_EPRINT("Usage:   test: [INFILE] [OUTFILE] [MONO/STEREO] [TESTCASE] [DASF/FILE] [SFREQ] [BITRATE] [OBJECTTYPE] [FRAMES] [IP_BUF] [OUT_BUF] [BITRATE] [FILEFORMAT] [ROBUSTNESS]\n");
    612         APP_EPRINT("Example: AacEncTest_common ip.pcm op.aac MONO 5 0 44100 128000 2 0 1 1 0 1 <ROBUST>\n");
    613         goto EXIT;
    614     }
    615 
    616     /* check to see that the input file exists */
    617     struct stat sb = {0};
    618     status = stat(argv[1], &sb);
    619     if( status != 0 )
    620 	{
    621         APP_EPRINT("%d :: App: Cannot find file %s. (%u)\n",__LINE__, argv[1], errno);
    622         goto EXIT;
    623     }
    624 
    625 	/* check to see the test case number */
    626     switch (atoi(argv[4]))
    627 	{
    628 		case 1:
    629 			APP_IPRINT ("---------------------------------------------\n");
    630 			APP_IPRINT ("Testing Simple Record till Predefined frames \n");
    631 			APP_IPRINT ("---------------------------------------------\n");
    632 			break;
    633 		case 2:
    634 			APP_IPRINT ("---------------------------------------------\n");
    635 			APP_IPRINT ("Testing Stop After Record \n");
    636 			APP_IPRINT ("---------------------------------------------\n");
    637 			break;
    638 		case 3:
    639 			APP_IPRINT ("---------------------------------------------\n");
    640 			APP_IPRINT ("Testing PAUSE & RESUME Command\n");
    641 			APP_IPRINT ("---------------------------------------------\n");
    642 			break;
    643 		case 4:
    644 			APP_IPRINT ("-------------------------------------------------\n");
    645 			APP_IPRINT ("Testing Repeated PLAY without Deleting Component\n");
    646 			APP_IPRINT ("-------------------------------------------------\n");
    647 			strcat (fname,"_tc5.aac");
    648 			if( (argc == 15) )
    649 			{
    650 				if(!strcmp("ROBUST",argv[14]))
    651 				{
    652 					APP_IPRINT("%d :: APP: AAC: 100 Iterations - ROBUSTNESS Test mode\n",__LINE__);
    653 					testcnt = 100;
    654 				}
    655 			}
    656 			else
    657 				testcnt = 20;
    658 			break;
    659 		case 5:
    660 			APP_IPRINT ("------------------------------------------------\n");
    661 			APP_IPRINT ("Testing Repeated PLAY with Deleting Component\n");
    662 			APP_IPRINT ("------------------------------------------------\n");
    663 			strcat (fname,"_tc6.aac");
    664 			if( (argc == 15))
    665 			{
    666 				if(!strcmp("ROBUST",argv[14]))
    667 				{
    668 					APP_IPRINT("%d :: APP: AAC: 100 Iterations - ROBUSTNESS Test mode\n",__LINE__);
    669 					testcnt1 = 100;
    670 				}
    671 			}
    672 			else
    673 				testcnt1 = 20;
    674 			break;
    675 		case 6:
    676 			APP_IPRINT ("-------------------------------------\n");
    677 			APP_IPRINT ("Testing Stop and Play \n");
    678 			APP_IPRINT ("-------------------------------------\n");
    679 			strcat(fname,"_tc7.aac");
    680 			testcnt = 2;
    681 			break;
    682 		case 7:
    683 			APP_IPRINT ("-------------------------------------\n");
    684 			APP_IPRINT ("VOLUME \n");
    685 			APP_IPRINT ("-------------------------------------\n");
    686 			break;
    687     }
    688 
    689 	/*Opening INPUT FILE */
    690 	fIn = fopen(argv[1], "r");
    691     if(fIn == NULL)
    692 	{
    693         APP_EPRINT("APP: Error:  failed to open the file %s for readonly access\n", argv[1]);
    694         goto EXIT;
    695     }
    696 
    697 	/*Opening  OUTPUT FILE */
    698     fOut = fopen(argv[2], "w");
    699     if(fOut == NULL)
    700 	{
    701         APP_EPRINT("APP: Error:  failed to create the output file %s\n", argv[2]);
    702         goto EXIT;
    703     }
    704 
    705 	APP_DPRINT("%d :: APP: AAC Encoding Test --- first create file handle\n",__LINE__);
    706 	APP_DPRINT("%d :: APP: AAC Encoding Test --- fIn = [%p]\n", __LINE__, fIn);
    707 	APP_DPRINT("%d :: APP: AAC Encoding Test --- fOut = [%p]\n",__LINE__, fOut);
    708 
    709 	/* check to see the STEREO/MONO mode */
    710     if(!strcmp("MONO",argv[3]))
    711 	{
    712         APP_IPRINT("%d :: APP: AAC: Encoding in Mono Mode\n",__LINE__);
    713         channel = MONO;
    714     }
    715 	else if(!strcmp("STEREO",argv[3]))
    716 	{
    717         APP_IPRINT("%d :: APP: AAC: Encoding in Stereo Mode\n",__LINE__);
    718         channel = STEREO;
    719     }
    720 	else
    721 	{
    722         APP_DPRINT("%d :: APP: Error: Invalid Mode Specifier, Check argument 3\n",__LINE__);
    723     }
    724 
    725 
    726 /*----------------------------------------------
    727  Main Loop for Deleting component test
    728  ----------------------------------------------*/
    729     jj=0;
    730     APP_IPRINT("%d :: APP: AAC ENC Test --- will call [%d] time for encoder\n",__LINE__, jj+1);
    731     for(jj=0; jj<testcnt1; jj++)
    732 	{
    733 
    734 		if ( atoi(argv[4])== 5)
    735 		{
    736 			APP_IPRINT ("***************************************\n");
    737 			APP_IPRINT ("%d :: TC-5 counter = %d\n",__LINE__,jj);
    738 			APP_IPRINT ("***************************************\n");
    739 		}
    740 
    741 #ifdef DSP_RENDERING_ON
    742 		if((Aacenc_fdwrite=open(FIFO1,O_WRONLY))<0) {
    743 	        APP_EPRINT("%d :: APP: - failure to open WRITE pipe\n",__LINE__);
    744 	    }
    745 	    else {
    746 	        APP_DPRINT("%d :: APP: - opened WRITE pipe\n",__LINE__);
    747 	    }
    748 
    749 	    if((Aacenc_fdread=open(FIFO2,O_RDONLY))<0) {
    750 	        APP_EPRINT("%d :: APP: - failure to open READ pipe\n",__LINE__);
    751 	        goto EXIT;
    752 	    }
    753 	    else {
    754 	        APP_DPRINT("%d :: APP: - opened READ pipe\n",__LINE__);
    755 	    }
    756 #endif
    757 	    /* Create a pipe used to queue data from the callback. */
    758         retval = pipe(IpBuf_Pipe);
    759         if( retval != 0)
    760 		{
    761             APP_EPRINT("App: Error: Fill Data Pipe failed to open\n");
    762             goto EXIT;
    763 		}
    764 
    765         retval = pipe(OpBuf_Pipe);
    766         if( retval != 0)
    767 		{
    768             APP_EPRINT( "App: Error: Empty Data Pipe failed to open\n");
    769             goto EXIT;
    770 		}
    771 
    772 		retval = pipe(Event_Pipe);
    773     	if( retval != 0) {
    774 		    APP_DPRINT( "Error:Empty Data Pipe failed to open\n");
    775 		    goto EXIT;
    776 	    }
    777 
    778 
    779 		/* save off the "max" of the handles for the selct statement */
    780         fdmax = maxint(IpBuf_Pipe[0], OpBuf_Pipe[0]);
    781 		fdmax = maxint(fdmax,Event_Pipe[0]);
    782 
    783         error = TIOMX_Init();
    784         if(error != OMX_ErrorNone)
    785 		{
    786             APP_EPRINT("%d :: APP: Error returned by OMX_Init()\n",__LINE__);
    787             goto EXIT;
    788 		}
    789 
    790 		if(fIn == NULL)
    791 		{
    792 			fIn = fopen(argv[1], "r");
    793 			if( fIn == NULL )
    794 			{
    795 				APP_EPRINT("App: Error:  failed to open the file %s for readonly access\n", argv[1]);
    796 				goto EXIT;
    797 			}
    798 		}
    799 		if(fOut == NULL)
    800 		{
    801 			fOut = fopen(fname, "w");
    802 			if( fOut == NULL )
    803 			{
    804 				APP_EPRINT("App: Error:  failed to create the output file %s\n", argv[2]);
    805 				goto EXIT;
    806 			}
    807 		}
    808 
    809 		/*Component handler */
    810 		pHandle = newmalloc(sizeof(OMX_HANDLETYPE));
    811         if(NULL == pHandle)
    812 		{
    813             APP_EPRINT("%d :: App: Malloc Failed\n",__LINE__);
    814             goto EXIT;
    815 		}
    816         APP_IPRINT("%d :: App: pHandle = %p\n",__LINE__,pHandle);
    817 #ifdef OMX_GETTIME
    818 	    GT_START();
    819 	    error = OMX_GetHandle(pHandle,strAacEncoder,&AppData, &AacCaBa);
    820 	    GT_END("Call to GetHandle");
    821 #else
    822 	    error = TIOMX_GetHandle(pHandle,strAacEncoder,&AppData, &AacCaBa);
    823 #endif
    824 		if( (error != OMX_ErrorNone) || (*pHandle == NULL) )
    825 		{
    826             APP_EPRINT("%d :: App: Error in Get Handle function %d \n",__LINE__,error);
    827 			goto EXIT;
    828 		}
    829 		APP_DPRINT("%d :: APP: GetHandle Done..........\n",__LINE__);
    830 
    831 
    832 		/* Setting input parameters */
    833         audioinfo.aacencHeaderInfo = newmalloc(sizeof(AACENC_HeadInfo));
    834         if (audioinfo.aacencHeaderInfo == NULL)
    835 		{
    836             APP_EPRINT("%d :: APP: Could not allocate audioinfo.aacencHeaderInfo\n",__LINE__);
    837             goto EXIT;
    838 		}
    839 
    840 		/* Check to see the F2F or DASF mode */
    841 	    audioinfo.dasfMode = atoi(argv[5]);
    842 	    if(audioinfo.dasfMode == 1)
    843 		{
    844 		    APP_IPRINT("%d :: APP: AAC Encoding in DASF MODE\n",__LINE__);
    845 		}
    846 	    else if(audioinfo.dasfMode == 0)
    847 		{
    848 		    APP_IPRINT("%d :: APP: AAC Encoding in FILE MODE\n",__LINE__);
    849 		}
    850 	    else
    851 		{
    852 		    APP_EPRINT("APP: Error: Enter proper DASF mode\n");
    853 		    APP_EPRINT("DASF:1\n");
    854 		    APP_EPRINT("NON DASF:0\n");
    855 		    goto EXIT;
    856 		}
    857 
    858 		/* Setting No. of Input and Output Buffers for the Component */
    859 	    numofinbuff = atoi(argv[10]);
    860 		numofoutbuff = atoi(argv[11]);
    861 
    862 		/* Ensuring the propper value of input buffers for DASF mode : Should be 0 */
    863 		if (audioinfo.dasfMode == 1)  /* DASF MODE */
    864 		{
    865 			if (numofinbuff != 0)
    866 			{
    867 				APP_EPRINT ("%d :: App: WARNING: DASF-Mode should not use input buffers \n",__LINE__);
    868 				APP_EPRINT ("%d :: App: WARNING: Changing the number of input buffers to 0 \n",__LINE__);
    869 				numofinbuff = 0;
    870 			}
    871 			if(  numofoutbuff != 2)
    872 			{
    873 				APP_EPRINT ("%d :: App: WARNING: DASF-Mode should  use 2 Output buffers \n",__LINE__);
    874 				APP_EPRINT ("%d :: App: WARNING: Changing the number of Output buffers to 2 \n",__LINE__);
    875 				numofoutbuff = 2;
    876 			}
    877 		}
    878 	    else if (audioinfo.dasfMode == 0) /* F2F MODE */
    879 		{
    880 
    881 			if( (numofinbuff < 0) || (numofinbuff > 4) ){
    882 				APP_EPRINT ("%d :: App: ERROR: Input buffers value incorrect (0-4) \n",__LINE__);
    883 				goto EXIT;
    884 			}
    885 
    886 			if( (numofoutbuff< 0) || (numofoutbuff > 4) ){
    887 				APP_EPRINT ("%d :: App: ERROR: Output buffers value incorrect (0-4) \n",__LINE__);
    888 				goto EXIT;
    889 			}
    890 
    891 			/* Ensuring the propper value of buffers for STEREO mode */
    892 			if((channel == STEREO) && (numofoutbuff != 4))
    893 			{
    894 				APP_EPRINT ("%d :: App: WARNING: STEREO-Mode should use 4 output buffers \n",__LINE__);
    895 				APP_EPRINT ("%d :: App: WARNING: Changing the number of output buffers to 4 \n",__LINE__);
    896 				numofoutbuff = 4;
    897 			}
    898 	    }
    899 		APP_DPRINT("\n%d :: App: numofinbuff = %ld \n",__LINE__, (long int)numofinbuff);
    900 	    APP_DPRINT("\n%d :: App: numofoutbuff = %ld \n",__LINE__, (long int)numofoutbuff);
    901 
    902 		pCompPrivateStruct = newmalloc (sizeof (OMX_PARAM_PORTDEFINITIONTYPE));
    903         if(NULL == pCompPrivateStruct)
    904 		{
    905            APP_DPRINT("%d :: APP: Malloc Failed\n",__LINE__);
    906            error = OMX_ErrorInsufficientResources;
    907            goto EXIT;
    908 		}
    909 
    910 	    /*Calculating an optimun size of Ouput  buffer according to number of frames*/
    911 		OutputBufferSize = CalculateOutputBufferSize(atoi(argv[6]),atoi(argv[7]),FramesPerOutBuf);   /*Sample frec,  Bit rate , frames*/
    912 
    913 		/* getting index for  framesPerOutBuf */
    914 		error = OMX_GetExtensionIndex(*pHandle, "OMX.TI.index.config.aacencframesPerOutBuf",&index);
    915 	    if (error != OMX_ErrorNone)
    916 		{
    917 		    APP_DPRINT("%d :: APP: Error getting extension index\n",__LINE__);
    918 		    goto EXIT;
    919 		}
    920 		/* Setting the Number of Frames per ouput buffer to component */
    921 		error = OMX_SetConfig (*pHandle, index, &FramesPerOutBuf);
    922         if(error != OMX_ErrorNone)
    923 		{
    924             error = OMX_ErrorBadParameter;
    925             APP_DPRINT("%d :: APP: Error from OMX_SetConfig() function\n",__LINE__);
    926             goto EXIT;
    927 		}
    928 
    929 		/* Setting INPUT port */
    930 		APP_DPRINT("%d :: APP: Setting input port config\n",__LINE__);
    931 		pCompPrivateStruct->nSize                             = sizeof (OMX_PARAM_PORTDEFINITIONTYPE);
    932 		pCompPrivateStruct->nVersion.s.nVersionMajor          = 0xF1;
    933 		pCompPrivateStruct->nVersion.s.nVersionMinor          = 0xF2;
    934 		pCompPrivateStruct->nPortIndex                        = INPUT_PORT;
    935 		pCompPrivateStruct->eDir                              = OMX_DirInput;
    936 		pCompPrivateStruct->nBufferCountActual                = numofinbuff;
    937 		pCompPrivateStruct->nBufferCountMin                   = numofinbuff;
    938 		pCompPrivateStruct->nBufferSize                       = INPUT_AACENC_BUFFER_SIZE;
    939 		pCompPrivateStruct->bEnabled                          = OMX_TRUE;
    940         pCompPrivateStruct->bPopulated                        = OMX_FALSE;
    941 		pCompPrivateStruct->format.audio.eEncoding            = OMX_AUDIO_CodingAAC;
    942 #ifdef OMX_GETTIME
    943 	GT_START();
    944 		error = OMX_SetParameter (*pHandle,OMX_IndexParamPortDefinition, pCompPrivateStruct);
    945 	GT_END("Set Parameter Test-SetParameter");
    946 #else
    947 		error = OMX_SetParameter (*pHandle,OMX_IndexParamPortDefinition, pCompPrivateStruct);
    948 #endif
    949 		if(error != OMX_ErrorNone)
    950 		{
    951 			error = OMX_ErrorBadParameter;
    952 			APP_DPRINT("%d :: APP: OMX_ErrorBadParameter\n",__LINE__);
    953 			goto EXIT;
    954 		}
    955 
    956 		/* Setting OUPUT port */
    957 		APP_DPRINT("%d :: APP: Setting output port config\n",__LINE__);
    958 		pCompPrivateStruct->nSize                             = sizeof (OMX_PARAM_PORTDEFINITIONTYPE);
    959 		pCompPrivateStruct->nVersion.s.nVersionMajor          = 0xF1;
    960 		pCompPrivateStruct->nVersion.s.nVersionMinor          = 0xF2;
    961 		pCompPrivateStruct->nPortIndex                        = OUTPUT_PORT;
    962 		pCompPrivateStruct->eDir                              = OMX_DirOutput;
    963 		pCompPrivateStruct->nBufferCountActual                = numofoutbuff;
    964 		pCompPrivateStruct->nBufferCountMin                   = numofoutbuff;
    965 		pCompPrivateStruct->nBufferSize                       = OutputBufferSize;
    966 		pCompPrivateStruct->bEnabled                          = OMX_TRUE;
    967         pCompPrivateStruct->bPopulated                        = OMX_FALSE;
    968 		pCompPrivateStruct->format.audio.eEncoding            = OMX_AUDIO_CodingAAC;
    969 #ifdef OMX_GETTIME
    970 	GT_START();
    971 	    error = OMX_SetParameter (*pHandle,OMX_IndexParamPortDefinition, pCompPrivateStruct);
    972 	GT_END("Set Parameter Test-SetParameter");
    973 #else
    974 	    error = OMX_SetParameter (*pHandle,OMX_IndexParamPortDefinition, pCompPrivateStruct);
    975 #endif
    976 		if(error != OMX_ErrorNone)
    977 		{
    978 			error = OMX_ErrorBadParameter;
    979 			APP_DPRINT("%d :: APP: OMX_ErrorBadParameter\n",__LINE__);
    980 			goto EXIT;
    981 		}
    982 
    983 		/*Ensuring  Valid Bits Per sample value  */
    984 		ObjectType = atoi(argv[8]);
    985 
    986 		if( ( ObjectType != ObjectTypeLC) && (ObjectType != ObjectTypeHE) && (ObjectType != ObjectTypeHE2) )
    987 		{
    988 			APP_EPRINT("%d :: APP: Incorrect Value for Object Type \n",__LINE__);
    989 			goto EXIT;
    990 		}
    991 
    992 		iAacParam = newmalloc (sizeof (OMX_AUDIO_PARAM_PCMMODETYPE));
    993 		if(NULL == iAacParam)
    994 		{
    995            APP_DPRINT("%d :: APP: Malloc Failed\n",__LINE__);
    996            error = OMX_ErrorInsufficientResources;
    997            goto EXIT;
    998 		}
    999 		/* Setting PCM params */
   1000 		iAacParam->nSize 					= sizeof (OMX_AUDIO_PARAM_PCMMODETYPE);
   1001 		iAacParam->nVersion.s.nVersionMajor = 0xF1;
   1002 		iAacParam->nVersion.s.nVersionMinor = 0xF2;
   1003 		iAacParam->nPortIndex 				= INPUT_PORT;
   1004 		iAacParam->nBitPerSample 			= BITS16;   /* BitsPerSample; */
   1005 #ifdef OMX_GETTIME
   1006 	GT_START();
   1007 		error = OMX_SetParameter (*pHandle, OMX_IndexParamAudioPcm, iAacParam);
   1008 	GT_END("Set Parameter Test-SetParameter");
   1009 #else
   1010 		error = OMX_SetParameter (*pHandle, OMX_IndexParamAudioPcm, iAacParam);
   1011 #endif
   1012 		if(error != OMX_ErrorNone)
   1013 		{
   1014 			error = OMX_ErrorBadParameter;
   1015 			APP_DPRINT("%d :: APP: OMX_ErrorBadParameter\n",__LINE__);
   1016 			goto EXIT;
   1017 		}
   1018 
   1019 		/*Ensuring a valid Sample rate */
   1020 
   1021 		if((atoi(argv[8]) == ObjectTypeHE) || (atoi(argv[8]) == ObjectTypeHE2)){
   1022 			for (i=0; i<NumOfFrecsHE; i++)
   1023 			{
   1024 				if(  atoi(argv[6]) == ArrValidFrecHE[i] )
   1025 					break;
   1026 				else if (i == (NumOfFrecsHE-1) )
   1027 				{
   1028 					APP_EPRINT("%d :: APP: Incorrect Value for Sample Rate for AAC-HE -- AAC-HEv2 \n",__LINE__);
   1029 					goto EXIT;
   1030 				}
   1031 			}
   1032 		}
   1033 		else{			/* atoi(argv[8]) == ObjectTypeLC */
   1034 			for (i=0; i<NumOfFrecsLC; i++)
   1035 			{
   1036 				if(  atoi(argv[6]) == ArrValidFrecLC[i] )
   1037 					break;
   1038 				else if (i == (NumOfFrecsLC-1) )
   1039 				{
   1040 					APP_EPRINT("%d :: APP: Incorrect Value for Sample Rate for AAC-LC\n",__LINE__);
   1041 					goto EXIT;
   1042 				}
   1043 			}
   1044 		}
   1045 
   1046 		/*Ensuring a valid Bit Rate */
   1047 		if(  (atoi(argv[7]) < Min_8Kbps) || (atoi(argv[7]) > Max_576Kbps) )
   1048 		{
   1049 			APP_EPRINT("%d :: APP: Incorrect Value for Bit Rate \n",__LINE__);
   1050 			goto EXIT;
   1051 		}
   1052 
   1053 		/*Ensuring a valid parameters combination */		/*NOTE: block should be moved up and function improved */
   1054 		isValidCombination = ValidateParameters( atoi(argv[6]), channel, atoi(argv[7]), atoi(argv[8]) );
   1055 		APP_IPRINT("%d :: APP: Parameters Combination %d \n",__LINE__,isValidCombination);
   1056 		if (!isValidCombination)
   1057 		{
   1058 			APP_EPRINT("%d :: App: Error: Invalid Samplerate, Bitrate or Channels parameters Combination \n",__LINE__);
   1059 			goto EXIT;
   1060 		}
   1061 		else if ((isValidCombination) &&( atoi(argv[6]) > Upto48kHz))			/*parameters out of combination table */
   1062 		{
   1063 			APP_EPRINT("%d :: App: Warning: Combination of Samplerate and Bitrate parameters has not been validated\n",__LINE__);
   1064 		}
   1065 
   1066 		/*Ensuring Valid use of HE and HEv2  */
   1067 
   1068 		if ((atoi(argv[8]) == ObjectTypeHE) && ((atoi(argv[7])) > Max_48Kbps))
   1069 		{
   1070 			APP_EPRINT("%d :: App: Error: HE AAC Type supports a Maximum of 48 Kbps BitRate \n",__LINE__);
   1071 			goto EXIT;
   1072 		}
   1073 
   1074 		if ((atoi(argv[8]) == ObjectTypeHE2) && ((atoi(argv[7])) > Max_64Kbps))
   1075 		{
   1076 			APP_EPRINT("%d :: App: Error: HEv2 AAC Type supports a Maximum of 128 Kbps BitRate \n",__LINE__);
   1077 			goto EXIT;
   1078 		}
   1079 
   1080 		pAacParam = newmalloc (sizeof (OMX_AUDIO_PARAM_AACPROFILETYPE));
   1081 		if(NULL == pAacParam)
   1082 		{
   1083            APP_EPRINT("%d :: APP: Malloc Failed\n",__LINE__);
   1084            error = OMX_ErrorInsufficientResources;
   1085            goto EXIT;
   1086 		}
   1087 		/* Setting AAC params */
   1088 	    pAacParam->nSize 					= sizeof (OMX_AUDIO_PARAM_AACPROFILETYPE);
   1089 		pAacParam->nVersion.s.nVersionMajor = 0xF1;
   1090 		pAacParam->nVersion.s.nVersionMinor = 0xF2;
   1091 		pAacParam->nPortIndex 				= OUTPUT_PORT;
   1092 		pAacParam->nChannels 				= channel;
   1093 		pAacParam->nBitRate					= atoi(argv[7]);
   1094 		pAacParam->nSampleRate 				= atoi(argv[6]);
   1095 		pAacParam->nAudioBandWidth 			= 0;
   1096 		pAacParam->nFrameLength 			= 0;
   1097 		pAacParam->nAACtools 				= 0x0000000C;
   1098 		pAacParam->nAACERtools 				= 0x00000000;
   1099 
   1100 		if (atoi(argv[8]) == ObjectTypeLC)
   1101 			pAacParam->eAACProfile = OMX_AUDIO_AACObjectLC;
   1102 		else if (atoi(argv[8]) == ObjectTypeHE)
   1103 			pAacParam->eAACProfile = OMX_AUDIO_AACObjectHE;
   1104 		else if (atoi(argv[8]) == ObjectTypeHE2)
   1105 			pAacParam->eAACProfile = OMX_AUDIO_AACObjectHE_PS;
   1106 
   1107 		if (0 == atoi(argv[13])){
   1108             pAacParam->eAACStreamFormat = OMX_AUDIO_AACStreamFormatRAW;
   1109 		}
   1110         else if (1 == atoi(argv[13])) {
   1111 			pAacParam->eAACStreamFormat = OMX_AUDIO_AACStreamFormatADIF;
   1112 		}
   1113 		else if (2 == atoi(argv[13])) {
   1114 			pAacParam->eAACStreamFormat = OMX_AUDIO_AACStreamFormatMP4ADTS;
   1115 		}
   1116 		if(channel == STEREO) {
   1117 			pAacParam->eChannelMode 	= OMX_AUDIO_ChannelModeStereo;
   1118 		}
   1119 		else if(channel == MONO) {
   1120 			pAacParam->eChannelMode 	= OMX_AUDIO_ChannelModeMono;
   1121 		}
   1122 #ifdef OMX_GETTIME
   1123 	GT_START();
   1124 		error = OMX_SetParameter (*pHandle, OMX_IndexParamAudioAac, pAacParam);
   1125 	GT_END("Set Parameter Test-SetParameter");
   1126 #else
   1127 		error = OMX_SetParameter (*pHandle, OMX_IndexParamAudioAac, pAacParam);
   1128 #endif
   1129 		if(error != OMX_ErrorNone)
   1130 		{
   1131 			error = OMX_ErrorBadParameter;
   1132 			APP_DPRINT("%d :: APP: OMX_ErrorBadParameter\n",__LINE__);
   1133 			goto EXIT;
   1134 		}
   1135 
   1136 		/*Setting the Bit rate mode parameter */
   1137 		audioinfo.aacencHeaderInfo->bitratemode = atoi(argv[12]);
   1138 
   1139 		/* setting for stream gain */
   1140 		pCompPrivateStructGain = newmalloc (sizeof(OMX_AUDIO_CONFIG_VOLUMETYPE));
   1141 		if(pCompPrivateStructGain == NULL)
   1142 		{
   1143 			APP_EPRINT("%d :: App: Malloc Failed\n",__LINE__);
   1144 			goto EXIT;
   1145 		}
   1146 		/* default setting for gain */
   1147 		pCompPrivateStructGain->nSize 						= sizeof(OMX_AUDIO_CONFIG_VOLUMETYPE);
   1148 		pCompPrivateStructGain->nVersion.s.nVersionMajor	= 0xF1;
   1149 		pCompPrivateStructGain->nVersion.s.nVersionMinor	= 0xF2;
   1150 		pCompPrivateStructGain->nPortIndex					= OMX_DirOutput;
   1151 		pCompPrivateStructGain->bLinear						= OMX_FALSE;
   1152 		pCompPrivateStructGain->sVolume.nValue				= Act_volume;		/* actual volume */
   1153 		pCompPrivateStructGain->sVolume.nMin				= Min_volume;		/* min volume */
   1154 		pCompPrivateStructGain->sVolume.nMax				= Max_volume;		/* max volume */
   1155 
   1156 
   1157 		error = OMX_GetExtensionIndex(*pHandle, "OMX.TI.index.config.aacencHeaderInfo",&index);
   1158 	    if (error != OMX_ErrorNone)
   1159 		{
   1160 		    APP_EPRINT("%d :: APP: Error getting extension index\n",__LINE__);
   1161 		    goto EXIT;
   1162 		}
   1163 #ifdef DSP_RENDERING_ON
   1164 		cmd_data.hComponent = *pHandle;
   1165 	    cmd_data.AM_Cmd = AM_CommandIsInputStreamAvailable;
   1166 	    cmd_data.param1 = 0;
   1167 
   1168 	    if((write(Aacenc_fdwrite, &cmd_data, sizeof(cmd_data)))<0)
   1169 		{
   1170 	        APP_EPRINT("%d :: APP: failure to Send command to audio manager\n", __LINE__);
   1171 	    }
   1172 	    if((read(Aacenc_fdread, &cmd_data, sizeof(cmd_data)))<0)
   1173 		{
   1174 	        APP_EPRINT("%d :: APP: failure to get data from the audio manager\n", __LINE__);
   1175 			goto EXIT;
   1176 	    }
   1177 
   1178 	    audioinfo.streamId = cmd_data.streamID;
   1179 	    streamId = audioinfo.streamId;
   1180 #endif
   1181 		error = OMX_SetConfig (*pHandle, index, &audioinfo);
   1182         if(error != OMX_ErrorNone)
   1183 		{
   1184             error = OMX_ErrorBadParameter;
   1185             APP_DPRINT("%d :: APP: Error from OMX_SetConfig() function\n",__LINE__);
   1186             goto EXIT;
   1187 		}
   1188 
   1189 	    if (audioinfo.dasfMode)
   1190 		{
   1191 #ifdef RTM_PATH
   1192 	        dataPath = DATAPATH_APPLICATION_RTMIXER;
   1193 			APP_DPRINT("APP: datapath: %d \n",dataPath);
   1194 #endif
   1195 
   1196 #ifdef ETEEDN_PATH
   1197         	dataPath = DATAPATH_APPLICATION;
   1198 #endif
   1199     	}
   1200 
   1201 	    error = OMX_GetExtensionIndex(*pHandle, "OMX.TI.index.config.aac.datapath",&index);
   1202 		if (error != OMX_ErrorNone)
   1203 		{
   1204 			APP_EPRINT("%d :: APP: Error getting extension index\n",__LINE__);
   1205 			goto EXIT;
   1206 		}
   1207 		error = OMX_SetConfig (*pHandle, index, &dataPath);
   1208 	    if(error != OMX_ErrorNone)
   1209 		{
   1210 	        error = OMX_ErrorBadParameter;
   1211 	        APP_EPRINT("%d :: APP: Error from OMX_SetConfig() function\n",__LINE__);
   1212 	        goto EXIT;
   1213 	    }
   1214 
   1215 
   1216 #ifndef USE_BUFFER
   1217 	    APP_DPRINT("%d :: APP: About to call OMX_AllocateBuffer\n",__LINE__);
   1218 	    for(i = 0; i < numofinbuff; i++)
   1219 		{
   1220 		   /* allocate input buffer */
   1221 		   APP_DPRINT("%d :: APP: About to call OMX_AllocateBuffer for pInputBufferHeader[%d]\n",__LINE__, i);
   1222 		   error = OMX_AllocateBuffer(*pHandle, &pInputBufferHeader[i], 0, NULL, INPUT_AACENC_BUFFER_SIZE);
   1223 		   if(error != OMX_ErrorNone)
   1224 		   {
   1225 			  APP_EPRINT("%d :: APP: Error returned by OMX_AllocateBuffer for pInputBufferHeader[%d]\n",__LINE__, i);
   1226 			  goto EXIT;
   1227 		   }
   1228 		}
   1229         APP_DPRINT("\n%d :: APP: pCompPrivateStruct->nBufferSize --> %ld \n",__LINE__,
   1230     												pCompPrivateStruct->nBufferSize);
   1231 	    for(i = 0; i < numofoutbuff; i++)
   1232 		{
   1233 		   /* allocate output buffer */
   1234 		   APP_DPRINT("%d :: APP: About to call OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
   1235 		   error = OMX_AllocateBuffer(*pHandle, &pOutputBufferHeader[i], 1, NULL, OutputBufferSize);
   1236 		   if(error != OMX_ErrorNone)
   1237 		   {
   1238 			  APP_EPRINT("%d :: APP: Error returned by OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
   1239 			  goto EXIT;
   1240 		   }
   1241 		}
   1242 #else
   1243 	    for(i = 0; i < numofinbuff; i++)
   1244 		{
   1245 		   pInputBuffer[i] = (OMX_U8*)newmalloc(INPUT_AACENC_BUFFER_SIZE + 256);
   1246 		   APP_DPRINT("%d :: APP: pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
   1247 		   if(NULL == pInputBuffer[i])
   1248 		   {
   1249 			  APP_EPRINT("%d :: APP: Malloc Failed\n",__LINE__);
   1250 			  error = OMX_ErrorInsufficientResources;
   1251 			  goto EXIT;
   1252 		   }
   1253 		   pInputBuffer[i] = pInputBuffer[i] + 128;
   1254 		   /* pass input buffer */
   1255 		   APP_DPRINT("%d :: APP: About to call OMX_UseBuffer\n",__LINE__);
   1256 		   APP_DPRINT("%d :: APP: pInputBufferHeader[%d] = %p\n",__LINE__,i,pInputBufferHeader[i]);
   1257 		   error = OMX_UseBuffer(*pHandle, &pInputBufferHeader[i], 0, NULL, INPUT_AACENC_BUFFER_SIZE, pInputBuffer[i]);
   1258 		   if(error != OMX_ErrorNone)
   1259 		   {
   1260 			  APP_EPRINT("%d :: APP: Error returned by OMX_UseBuffer()\n",__LINE__);
   1261 			  goto EXIT;
   1262 		   }
   1263 		}
   1264 
   1265 	    for(i = 0; i < numofoutbuff; i++)
   1266 		{
   1267 		   pOutputBuffer[i] = (OMX_U8*) newmalloc (OutputBufferSize + 256);
   1268 		   APP_DPRINT("%d :: APP: pOutputBuffer[%d] = %p\n",__LINE__,i,pOutputBuffer[i]);
   1269 		   if(NULL == pOutputBuffer[i])
   1270 		   {
   1271 			  APP_EPRINT("%d :: APP: Malloc Failed\n",__LINE__);
   1272 			  error = OMX_ErrorInsufficientResources;
   1273 			  goto EXIT;
   1274 		   }
   1275 		   pOutputBuffer[i] = pOutputBuffer[i] + 128;
   1276 
   1277 		   /* allocate output buffer */
   1278 		   APP_DPRINT("%d :: APP: About to call OMX_UseBuffer\n",__LINE__);
   1279 		   APP_DPRINT("%d :: APP: pOutputBufferHeader[%d] = %p\n",__LINE__,i,pOutputBufferHeader[i]);
   1280 		   error = OMX_UseBuffer(*pHandle, &pOutputBufferHeader[i], 1, NULL, OutputBufferSize, pOutputBuffer[i]);
   1281 		   if(error != OMX_ErrorNone)
   1282 		   {
   1283 			  APP_EPRINT("%d :: APP: Error returned by OMX_UseBuffer()\n",__LINE__);
   1284 			  goto EXIT;
   1285 		   }
   1286 		}
   1287 #endif
   1288 
   1289 		/* --------Change to Idle  ---------*/
   1290 		APP_DPRINT ("%d:: APP: Sending OMX_StateIdle Command\n",__LINE__);
   1291 	#ifdef OMX_GETTIME
   1292 		GT_START();
   1293 	#endif
   1294 		error = OMX_SendCommand(*pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
   1295 		if(error != OMX_ErrorNone)
   1296 		{
   1297 			APP_EPRINT("APP: Error from SendCommand-Idle(Init) State function\n");
   1298 			goto EXIT;
   1299 		}
   1300 		/* Wait for startup to complete */
   1301 	error = WaitForState(*pHandle, OMX_StateIdle);
   1302 #ifdef OMX_GETTIME
   1303 	GT_END("Call to SendCommand <OMX_StateIdle>");
   1304 #endif
   1305 		if(error != OMX_ErrorNone)
   1306 		{
   1307 			APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   1308 			goto EXIT;
   1309 		}
   1310 
   1311 
   1312 		if (audioinfo.dasfMode == 1)
   1313 		{
   1314 			/* get streamID back to application */
   1315 		    error = OMX_GetExtensionIndex(*pHandle, "OMX.TI.index.config.aacencstreamIDinfo",&index);
   1316 		    if (error != OMX_ErrorNone)
   1317 			{
   1318 			    APP_EPRINT("APP: Error getting extension index\n");
   1319 			    goto EXIT;
   1320 			}
   1321 
   1322 		    error = OMX_GetConfig (*pHandle, index, streaminfo);
   1323 	        if(error != OMX_ErrorNone)
   1324 			{
   1325 	            error = OMX_ErrorBadParameter;
   1326 	            APP_EPRINT("%d :: APP: Error from OMX_GetConfig() function\n",__LINE__);
   1327 	            goto EXIT;
   1328 			}
   1329 
   1330 		    streamId = ((TI_OMX_STREAM_INFO*)streaminfo)->streamId;
   1331 		    APP_IPRINT(" ***************StreamId=%d******************\n", (int)streamId);
   1332 		}
   1333 
   1334 
   1335 /*----------------------------------------------
   1336  Main Loop for Non Deleting component test
   1337  ----------------------------------------------*/
   1338 		kk = 0;
   1339 		for(kk=0; kk<testcnt; kk++)
   1340 		{
   1341 			APP_DPRINT ("%d :: APP: Test counter = %d \n",__LINE__,kk);
   1342 			if(kk > 0)
   1343 			{
   1344 	            APP_IPRINT ("Encoding the file one more Time\n");
   1345 
   1346 	            close(IpBuf_Pipe[0]);
   1347 	            close(IpBuf_Pipe[1]);
   1348 	            close(OpBuf_Pipe[0]);
   1349 	            close(OpBuf_Pipe[1]);
   1350 
   1351 	            /* Create a pipe used to queue data from the callback. */
   1352 	            retval = pipe(IpBuf_Pipe);
   1353 	            if( retval != 0)
   1354 				{
   1355 	                APP_EPRINT( "%d :: APP: Error: Fill Data Pipe failed to open\n",__LINE__);
   1356 	                goto EXIT;
   1357 	            }
   1358 
   1359 	            retval = pipe(OpBuf_Pipe);
   1360 	            if( retval != 0)
   1361 				{
   1362 	                APP_EPRINT( "%d :: APP: Error: Empty Data Pipe failed to open\n",__LINE__);
   1363 	                goto EXIT;
   1364 	            }
   1365 				if (audioinfo.dasfMode == 0) /*No need for dasf mode */
   1366 				{
   1367 		            fIn = fopen(argv[1], "r");
   1368 		            if(fIn == NULL)
   1369 					{
   1370 		                APP_EPRINT("Error:  failed to open the file %s for readonly access\n", argv[1]);
   1371 		                goto EXIT;
   1372 		            }
   1373 				}
   1374 	            fOut = fopen(fname, "w");
   1375 	            if(fOut == NULL)
   1376 				{
   1377 	               APP_EPRINT("Error:  failed to create the output file \n");
   1378 	                goto EXIT;
   1379 	            }
   1380         	}
   1381 			nFrameCount = 0;
   1382 			APP_IPRINT("------------------------------------------------------------\n");
   1383 			APP_IPRINT ("%d :: APP: Encoding the file [%d] Time\n",__LINE__, kk+1);
   1384 			APP_IPRINT("------------------------------------------------------------\n");
   1385 			if ( atoi(argv[4])== 4)
   1386 			{
   1387 				APP_IPRINT("------------------------------------------------------------\n");
   1388 				APP_IPRINT ("Testing Repeated RECORD without Deleting Component\n");
   1389 				APP_IPRINT("------------------------------------------------------------\n");
   1390 			}
   1391 			if(fIn == NULL)
   1392 			{
   1393 				fIn = fopen(argv[1], "r");
   1394 				if(fIn == NULL)
   1395 				{
   1396 					APP_EPRINT("APP: Error:  failed to open the file %s for readonly access\n", argv[1]);
   1397 					goto EXIT;
   1398 				}
   1399 			}
   1400 
   1401 			if(fOut == NULL)
   1402 			{
   1403 				fOut = fopen(fname, "w");
   1404 				if(fOut == NULL)
   1405 				{
   1406 					APP_EPRINT("APP: Error:  failed to create the output file %s\n", argv[2]);
   1407 					goto EXIT;
   1408 				}
   1409 			}
   1410 
   1411 			/* -------- Change to Executing ------------ */
   1412 			done = 0;
   1413 			APP_DPRINT ("%d :: APP: Sending OMX_StateExecuting Command\n",__LINE__);
   1414 		#ifdef OMX_GETTIME
   1415 			GT_START();
   1416 		#endif
   1417 			error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateExecuting, NULL);
   1418 			if(error != OMX_ErrorNone)
   1419 			{
   1420 				APP_EPRINT ("APP: Error from SendCommand-Executing State function \n");
   1421 				goto EXIT;
   1422 			}
   1423 			error = WaitForState(*pHandle, OMX_StateExecuting);
   1424 		#ifdef OMX_GETTIME
   1425 			GT_END("Call to SendCommand <OMX_StateExecuting>");
   1426 		#endif
   1427 			if(error != OMX_ErrorNone)
   1428 			{
   1429 				APP_EPRINT ( "APP: WaitForState reports an error \n");
   1430 				goto EXIT;
   1431 			}
   1432 
   1433 			pComponent = (OMX_COMPONENTTYPE *)*pHandle;
   1434 		    error = OMX_GetState(*pHandle, &state);
   1435 	        if(error != OMX_ErrorNone)
   1436 			{
   1437 		        APP_EPRINT ("%d :: APP: OMX_GetState has returned status %X\n",__LINE__, error);
   1438 		        goto EXIT;
   1439 			}
   1440 
   1441 			if (audioinfo.dasfMode == 0)
   1442 			{
   1443 				for(i = 0; i < numofinbuff; i++)
   1444 				{
   1445 		            nRead = fread(pInputBufferHeader[i]->pBuffer, 1, pInputBufferHeader[i]->nAllocLen, fIn);
   1446 		            APP_DPRINT("%d :: APP: Reading InputBuffer = %d from the input file nRead = %d\n",__LINE__, nIpBuffs, nRead);
   1447 				    if((nRead < pInputBufferHeader[i]->nAllocLen) && (done == 0))
   1448 					{
   1449 			            APP_DPRINT("%d :: APP: Sending Last Input Buffer from TestApp(which can be zero or less than Buffer length) ---------- \n",__LINE__);
   1450 					    pInputBufferHeader[i]->nFlags = OMX_BUFFERFLAG_EOS;
   1451 					}
   1452 				    APP_DPRINT("%d :: APP :: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pInputBufferHeader[i]);
   1453 				    pInputBufferHeader[i]->nFilledLen = nRead;
   1454 					pInputBufferHeader[i]->nTimeStamp= rand() % 100;
   1455                     pInputBufferHeader[i]->nTickCount = rand() % 100;
   1456 				#ifdef OMX_GETTIME
   1457 					if (k==0)
   1458 					{
   1459 						GT_FlagE=1;  /* 1 = First Buffer,  0 = Not First Buffer  */
   1460 						GT_START(); /* Empty Bufffer */
   1461 					}
   1462 				#endif
   1463 
   1464 				if (!preempted)
   1465 				    OMX_EmptyThisBuffer(*pHandle, pInputBufferHeader[i]);
   1466 
   1467 
   1468 					APP_DPRINT("APP: pInputBufferHeader[%d]->nTimeStamp = %lli\n",i,pInputBufferHeader[i]->nTimeStamp);
   1469 				    nIpBuffs++;
   1470 				}
   1471 			}
   1472 
   1473 	        for (k=0; k < numofoutbuff; k++)
   1474 			{
   1475 		        APP_DPRINT("%d :: APP: Before Fill this buffer is called = %x\n",__LINE__, (unsigned int)pOutputBufferHeader[k]);
   1476 			#ifdef OMX_GETTIME
   1477 				if (k==0)
   1478 					{
   1479 						GT_FlagF=1;  /* 1 = First Buffer,  0 = Not First Buffer  */
   1480 						GT_START(); /* Fill Buffer */
   1481 					}
   1482 			#endif
   1483                 OMX_FillThisBuffer(*pHandle,  pOutputBufferHeader[k]);
   1484 			}
   1485 
   1486 /*----------------------------------------------
   1487  Main while for the buffers process
   1488  ----------------------------------------------*/
   1489 
   1490  			 /* Component is stopping now by procesing the playcomplete event  (bPlayCompleted Flag) */
   1491 
   1492 #ifndef WAITFORRESOURCES
   1493 			while(( (error == OMX_ErrorNone) && (state != OMX_StateIdle)) && (state != OMX_StateInvalid) && (!bPlayCompleted))
   1494 			{
   1495 			if(1){
   1496 #else
   1497     		while(1) {
   1498        		if((error == OMX_ErrorNone) && (state != OMX_StateIdle) && (state != OMX_StateInvalid) && (!bPlayCompleted)){
   1499 #endif
   1500 
   1501 				FD_ZERO(&rfds);
   1502 				FD_SET(IpBuf_Pipe[0], &rfds);
   1503 				FD_SET(OpBuf_Pipe[0], &rfds);
   1504 				FD_SET(Event_Pipe[0], &rfds);
   1505 
   1506 				tv.tv_sec = 1;
   1507 				tv.tv_usec = 0;
   1508 
   1509 				retval = select(fdmax+1, &rfds, NULL, NULL, &tv);
   1510 				if(retval == -1)
   1511 				{
   1512 					perror("select()");
   1513 					fprintf (stderr, " : Error \n");
   1514 					break;
   1515 				}
   1516 
   1517 				if(retval == 0)
   1518 				{
   1519 					APP_DPRINT("%d :: APP: The current state of the component = %d \n",__LINE__,state);
   1520 					APP_DPRINT("\n\n\n%d ::!!!!!!!     App Timeout !!!!!!!!!!! \n",__LINE__);
   1521 					APP_DPRINT("%d :: ---------------------------------------\n\n\n",__LINE__);
   1522 				}
   1523 
   1524 				switch (atoi(argv[4]))
   1525 				{
   1526 					case 1:
   1527 					case 4:
   1528 					case 5:
   1529 						if(audioinfo.dasfMode == 0)
   1530 						{
   1531 						    APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER FILE 2 FILE MODE \n",__LINE__);
   1532 		    		        if(FD_ISSET(IpBuf_Pipe[0], &rfds))
   1533 							{
   1534             		            OMX_BUFFERHEADERTYPE* pBuffer;
   1535             		            read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
   1536             		            if(done == 0)
   1537 								{
   1538 						            nRead = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
   1539 						            APP_DPRINT("%d :: APP: Reading InputBuffer = %d from the input file nRead = %d\n",__LINE__, nIpBuffs, nRead);
   1540 						            if((nRead < pBuffer->nAllocLen) && (done == 0))
   1541 									{
   1542 							            APP_IPRINT("%d :: APP: Sending Last Input Buffer from TestApp \n",__LINE__);
   1543 							            done 			= 1;
   1544 							            pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
   1545 									}
   1546 						            APP_DPRINT("%d :: APP :: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
   1547 						            pBuffer->nFilledLen = nRead;
   1548                                     pBuffer->nTimeStamp= rand() % 100;
   1549                                     pBuffer->nTickCount = rand() % 100;
   1550 						            OMX_EmptyThisBuffer(*pHandle, pBuffer);
   1551 						            nIpBuffs++;
   1552 								}
   1553 						   }
   1554 						}
   1555 						else {
   1556 						    if(done == 0)
   1557 							{
   1558 							    APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
   1559 								if(nFrameCount == atoi(argv[9]))
   1560 								{
   1561 									APP_DPRINT("%d :: APP: Sending Stop.........From APP \n",__LINE__);
   1562 									APP_DPRINT("%d :: APP: Shutting down ---------- \n",__LINE__);
   1563 								#ifdef OMX_GETTIME
   1564 									GT_START();
   1565 								#endif
   1566 									error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
   1567 									if(error != OMX_ErrorNone)
   1568 									{
   1569 										APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1570 										goto EXIT;
   1571 									}
   1572 									done = 1;
   1573 									error = WaitForState(*pHandle, OMX_StateIdle);
   1574 								#ifdef OMX_GETTIME
   1575 									GT_END("Call to SendCommand <OMX_StateIdle>");
   1576 								#endif
   1577 									if(error != OMX_ErrorNone)
   1578 									{
   1579 										APP_DPRINT ( "APP: WaitForState reports an error \n");
   1580 										goto EXIT;
   1581 									}
   1582 
   1583 								}
   1584 								APP_DPRINT("%d :: APP: AAC ENCODER READING DATA FROM DASF  \n",__LINE__);
   1585 							}
   1586 						}
   1587 						break;
   1588 
   1589 					case 2:
   1590 						if(audioinfo.dasfMode == 0)
   1591 						{
   1592 					  	    APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER FILE 2 FILE MODE \n",__LINE__);
   1593 					        if( FD_ISSET(IpBuf_Pipe[0], &rfds) )
   1594 							{
   1595 					            OMX_BUFFERHEADERTYPE* pBuffer;
   1596 					            read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
   1597 					            if(done == 0)
   1598 								{
   1599 						            APP_DPRINT("%d :: APP: Reading InputBuffer = %d from the input file nRead = %d\n",__LINE__, nIpBuffs, nRead);
   1600 						            nRead = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
   1601 						            if(frmCnt == 20)
   1602 									{
   1603 						                APP_DPRINT("%d :: APP: Sending Stop.........From APP \n",__LINE__);
   1604 						                APP_DPRINT("%d :: APP: Shutting down ---------- \n",__LINE__);
   1605 									#ifdef OMX_GETTIME
   1606 										GT_START();
   1607 									#endif
   1608 										error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
   1609 						                if(error != OMX_ErrorNone)
   1610 										{
   1611 							               APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1612 							                goto EXIT;
   1613 										}
   1614 										error = WaitForState(*pHandle, OMX_StateIdle);
   1615 									#ifdef OMX_GETTIME
   1616 										GT_END("Call to SendCommand <OMX_StateIdle>");
   1617 									#endif
   1618 										if(error != OMX_ErrorNone)
   1619 										{
   1620 											APP_EPRINT("APP: WaitForState reports an error \n");
   1621 											goto EXIT;
   1622 										}
   1623 						                done 				 = 1;
   1624 						                pBuffer->nFlags 	 = OMX_BUFFERFLAG_EOS;
   1625 						                pBuffer->nFilledLen = 0;
   1626 									}
   1627 									else if((nRead < pBuffer->nAllocLen) && (done == 0))
   1628 									{
   1629 							             APP_DPRINT("%d :: APP: Sending Last Input Buffer from TestApp(which can be zero or less than Buffer length)\n",__LINE__);
   1630 							             done = 1;
   1631 							             pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
   1632 									}
   1633 							        APP_DPRINT("%d :: APP :: Input buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
   1634                 					pBuffer->nTimeStamp= rand() % 100;				/* random value for time stamp */
   1635                                     pBuffer->nTickCount = rand() % 100;
   1636 							        OMX_EmptyThisBuffer(*pHandle, pBuffer);
   1637 									nIpBuffs++;
   1638 							        frmCnt++;
   1639 								}
   1640 							}
   1641 						}
   1642 						else
   1643 						{
   1644 							APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
   1645 							if(nFrameCount == atoi(argv[9]))
   1646 							{
   1647 								APP_DPRINT("%d :: APP: Sending Stop.........From APP \n",__LINE__);
   1648 								APP_DPRINT("%d :: APP: Shutting down ---------- \n",__LINE__);
   1649 							#ifdef OMX_GETTIME
   1650 								GT_START();
   1651 							#endif
   1652 								error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
   1653 								if(error != OMX_ErrorNone)
   1654 								{
   1655 									APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1656 									goto EXIT;
   1657 								}
   1658 								done = 1;
   1659 								error = WaitForState(*pHandle, OMX_StateIdle);
   1660 							#ifdef OMX_GETTIME
   1661 								GT_END("Call to SendCommand <OMX_StateIdle>");
   1662 							#endif
   1663 								if(error != OMX_ErrorNone)
   1664 								{
   1665 									APP_EPRINT ( "APP: WaitForState reports an error \n");
   1666 									goto EXIT;
   1667 								}
   1668 							}
   1669 							APP_DPRINT("%d :: APP: AAC ENCODER READING DATA FROM DASF  \n",__LINE__);
   1670 						}
   1671 						break;
   1672 
   1673 					case 3:
   1674 						if(audioinfo.dasfMode == 0)
   1675 						{
   1676 							APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER FILE 2 FILE MODE \n",__LINE__);
   1677 		    		        if(FD_ISSET(IpBuf_Pipe[0], &rfds))
   1678 							{
   1679             		            OMX_BUFFERHEADERTYPE* pBuffer;
   1680             		            read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
   1681             		            if(done == 0)
   1682 								{
   1683 						            nRead = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
   1684 						            APP_DPRINT("%d :: APP: Reading InputBuffer = %d from the input file nRead = %d\n",__LINE__, nIpBuffs, nRead);
   1685 						            if((nRead < pBuffer->nAllocLen) && (done == 0))
   1686 									{
   1687 							            APP_DPRINT("%d :: APP: Sending Last Input Buffer from TestApp(which can be zero or less than Buffer length) ---------- \n",__LINE__);
   1688 							            done 			= 1;
   1689 							            pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
   1690 									}
   1691 						            APP_DPRINT("%d :: APP : Input Buffer- Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
   1692 						            pBuffer->nFilledLen = nRead;
   1693                 					pBuffer->nTimeStamp= rand() % 100;				/* random value for time stamp */
   1694                                     pBuffer->nTickCount = rand() % 100;
   1695 						            OMX_EmptyThisBuffer(*pHandle, pBuffer);
   1696 						            nIpBuffs++;
   1697 								}
   1698 							}
   1699 							if(nIpBuffs == 9)
   1700 							{
   1701 							#ifdef OMX_GETTIME
   1702 								GT_START();
   1703 							#endif
   1704 								error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StatePause, NULL);
   1705 								if(error != OMX_ErrorNone)
   1706 								{
   1707 									APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1708 									goto EXIT;
   1709 								}
   1710 								APP_DPRINT("%d :: APP: Pause: OpBuffs received = %d\n",__LINE__,nOpBuffs);
   1711 								error = WaitForState(*pHandle, OMX_StatePause);
   1712 							#ifdef OMX_GETTIME
   1713 								GT_END("Call to SendCommand <OMX_StatePause>");
   1714 							#endif
   1715 								if(error != OMX_ErrorNone)
   1716 								{
   1717 									APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   1718 									goto EXIT;
   1719 								}
   1720 								APP_DPRINT("%d :: APP: Pause: State paused = %d\n",__LINE__,nOpBuffs);
   1721 								APP_IPRINT("%d :: APP: Pausing component...\n",__LINE__);
   1722 								APP_DPRINT("%d :: APP: Is Sleeping here for %d seconds\n",__LINE__, SLEEP_TIME);
   1723 								sleep(SLEEP_TIME);
   1724 							#ifdef OMX_GETTIME
   1725 								GT_START();
   1726 							#endif
   1727 								error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateExecuting, NULL);
   1728 								if(error != OMX_ErrorNone)
   1729 								{
   1730 									APP_EPRINT("APP: Error from SendCommand-Executing State function\n");
   1731 									goto EXIT;
   1732 								}
   1733 								APP_DPRINT("%d :: APP: Resumed: OpBuffs received = %d\n",__LINE__,nOpBuffs);
   1734 								error = WaitForState(*pHandle, OMX_StateExecuting);
   1735 							#ifdef OMX_GETTIME
   1736 								GT_END("Call to SendCommand <OMX_StateIdle>");
   1737 							#endif
   1738 								if(error != OMX_ErrorNone)
   1739 								{
   1740 									APP_DPRINT ( "APP: WaitForState reports an error \n");
   1741 									goto EXIT;
   1742 								}
   1743 							}
   1744 						}
   1745 						else
   1746 						{
   1747 							APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
   1748 							if(nFrameCount == atoi(argv[9]))
   1749 							{
   1750 								APP_DPRINT("%d :: APP: Sending Stop.........From APP \n",__LINE__);
   1751 								APP_IPRINT("%d :: APP: Shutting down ---------- \n",__LINE__);
   1752 							#ifdef OMX_GETTIME
   1753 								GT_START();
   1754 							#endif
   1755 								error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
   1756 								if(error != OMX_ErrorNone)
   1757 								{
   1758 									APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1759 									goto EXIT;
   1760 								}
   1761 								done = 1;
   1762 								error = WaitForState(*pHandle, OMX_StateIdle);
   1763 							#ifdef OMX_GETTIME
   1764 								GT_END("Call to SendCommand <OMX_StateIdle>");
   1765 							#endif
   1766 								if(error != OMX_ErrorNone)
   1767 								{
   1768 									APP_DPRINT ( "APP: WaitForState reports an error \n");
   1769 									goto EXIT;
   1770 								}
   1771 
   1772 							}
   1773 							if(nFrameCount == 9)
   1774 							{
   1775 							#ifdef OMX_GETTIME
   1776 								GT_START();
   1777 							#endif
   1778 								error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StatePause, NULL);
   1779 								if(error != OMX_ErrorNone)
   1780 								{
   1781 									APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1782 									goto EXIT;
   1783 								}
   1784 								APP_DPRINT("%d :: APP: Pause: OpBuffs received = %d\n",__LINE__,nOpBuffs);
   1785 								error = WaitForState(*pHandle, OMX_StatePause);
   1786 							#ifdef OMX_GETTIME
   1787 								GT_END("Call to SendCommand <OMX_StatePause>");
   1788 							#endif
   1789 								if(error != OMX_ErrorNone)
   1790 								{
   1791 									APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   1792 									goto EXIT;
   1793 								}
   1794 								APP_DPRINT("%d :: APP: Pause: State paused = %d\n",__LINE__,nOpBuffs);
   1795 								APP_DPRINT("%d :: APP: Is Sleeping here for %d seconds\n",__LINE__, SLEEP_TIME);
   1796 								sleep(SLEEP_TIME);		/* Pause time */
   1797 							#ifdef OMX_GETTIME
   1798 								GT_START();
   1799 							#endif
   1800 								error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateExecuting, NULL);
   1801 								if(error != OMX_ErrorNone)
   1802 								{
   1803 									APP_EPRINT("APP: Error from SendCommand-Executing State function\n");
   1804 									goto EXIT;
   1805 								}
   1806 								APP_DPRINT("%d :: APP: Resumed: OpBuffs received = %d\n",__LINE__,nOpBuffs);
   1807 								error = WaitForState(*pHandle, OMX_StateExecuting);
   1808 							#ifdef OMX_GETTIME
   1809 								GT_END("Call to SendCommand <OMX_StateExecuting>");
   1810 							#endif
   1811 
   1812 								if(error != OMX_ErrorNone)
   1813 								{
   1814 									APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   1815 									goto EXIT;
   1816 								}
   1817 
   1818 							}
   1819 							APP_DPRINT("%d :: APP: AAC ENCODER READING DATA FROM DASF  \n",__LINE__);
   1820 						}
   1821 						break;
   1822 					case 6:
   1823 						if(audioinfo.dasfMode == 0) {
   1824 						    APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER FILE 2 FILE MODE \n",__LINE__);
   1825 							if(nIpBuffs == 20)
   1826 							{
   1827 									APP_DPRINT("APP: Sending Stop Command after sending 20 frames \n");
   1828 								#ifdef OMX_GETTIME
   1829 									GT_START();
   1830 								#endif
   1831 									error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
   1832 									if(error != OMX_ErrorNone)
   1833 									{
   1834 										APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1835 										goto EXIT;
   1836 									}
   1837 									error = WaitForState(*pHandle, OMX_StateIdle);
   1838 								#ifdef OMX_GETTIME
   1839 									GT_END("Call to SendCommand <OMX_StateIdle>");
   1840 								#endif
   1841 									if(error != OMX_ErrorNone)
   1842 									{
   1843 										APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   1844 										goto EXIT;
   1845 									}
   1846 									APP_DPRINT("%d :: APP: About to call GetState() \n",__LINE__);
   1847 					                error = OMX_GetState(*pHandle, &state);
   1848 					                if(error != OMX_ErrorNone)
   1849 									{
   1850 						                APP_EPRINT("APP: Warning:  hAacEncoder->GetState has returned status %X\n", error);
   1851 						                goto EXIT;
   1852 									}
   1853 							}
   1854 						    else if(FD_ISSET(IpBuf_Pipe[0], &rfds))
   1855 							{
   1856             		            OMX_BUFFERHEADERTYPE* pBuffer;
   1857             		            read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
   1858             		            if(done == 0)
   1859 								{
   1860 						            nRead = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
   1861 						            APP_DPRINT("%d :: APP: Reading InputBuffer = %d from the input file nRead = %d\n",__LINE__, nIpBuffs, nRead);
   1862 						            if((nRead < pBuffer->nAllocLen) && (done == 0))
   1863 									{
   1864 							            APP_DPRINT("%d :: APP: Sending Last Input Buffer from TestApp(which can be zero or less than Buffer length) \n",__LINE__);
   1865 							            done 			= 1;
   1866 							            pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
   1867 									}
   1868 									APP_DPRINT("%d :: APP: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
   1869 						            pBuffer->nFilledLen = nRead;
   1870                 					pBuffer->nTimeStamp= rand() % 100;				/* random value for time stamp */
   1871                                     pBuffer->nTickCount = rand() % 100;
   1872 						            OMX_EmptyThisBuffer(*pHandle, pBuffer);
   1873 						            nIpBuffs++;
   1874 								}
   1875 							}
   1876 						}
   1877 						else {
   1878 						    APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
   1879 							if(nFrameCount == 5)
   1880 							{
   1881 								APP_DPRINT("APP: Sending Stop Command after sending 4 frames \n");
   1882 							#ifdef OMX_GETTIME
   1883 								GT_START();
   1884 							#endif
   1885 								error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
   1886 								if(error != OMX_ErrorNone)
   1887 								{
   1888 									APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1889 									goto EXIT;
   1890 								}
   1891 								error = WaitForState(*pHandle, OMX_StateIdle);
   1892 							#ifdef OMX_GETTIME
   1893 								GT_END("Call to SendCommand <OMX_StateIdle>");
   1894 							#endif
   1895 								if(error != OMX_ErrorNone)
   1896 								{
   1897 									APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   1898 									goto EXIT;
   1899 								}
   1900 								done = 1;
   1901 							}
   1902 							else if(nFrameCount == atoi(argv[9]))
   1903 							{
   1904 								APP_DPRINT("%d :: APP: Sending Stop.........From APP \n",__LINE__);
   1905 								APP_DPRINT("%d :: APP: Shutting down ---------- \n",__LINE__);
   1906 							#ifdef OMX_GETTIME
   1907 								GT_START();
   1908 							#endif
   1909 								error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
   1910 								if(error != OMX_ErrorNone)
   1911 								{
   1912 									APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   1913 									goto EXIT;
   1914 								}
   1915 								done = 1;
   1916 								error = WaitForState(*pHandle, OMX_StateIdle);
   1917 							#ifdef OMX_GETTIME
   1918 								GT_END("Call to SendCommand <OMX_StateIdle>");
   1919 							#endif
   1920 								if(error != OMX_ErrorNone)
   1921 								{
   1922 									APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   1923 									goto EXIT;
   1924 								}
   1925 							}
   1926 							APP_DPRINT("%d :: APP: AAC ENCODER READING DATA FROM DASF  \n",__LINE__);
   1927 						}
   1928 						break;
   1929 
   1930 					case 7:
   1931 					    if(audioinfo.dasfMode == 0)
   1932 						{
   1933 						    APP_IPRINT("%d :: APP: This test is not applied to file mode\n",__LINE__);
   1934 						    goto EXIT;
   1935 						}
   1936 					    else
   1937 						{
   1938 							APP_DPRINT("%d :: APP: AAC ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
   1939 						    if(nFrameCount == 5)
   1940 							{
   1941 							    /* set high gain for record stream */
   1942 							    APP_DPRINT("APP: [AAC encoder] --- will set stream gain to high\n");
   1943             				    pCompPrivateStructGain->sVolume.nValue = 0x8000;
   1944 							    error = OMX_SetConfig(*pHandle, OMX_IndexConfigAudioVolume, pCompPrivateStructGain);
   1945 							    if (error != OMX_ErrorNone)
   1946 								{
   1947 								    error = OMX_ErrorBadParameter;
   1948 								    goto EXIT;
   1949 								}
   1950 							}
   1951 						    if(nFrameCount == 10)
   1952 							{
   1953 							    /* set low gain for record stream */
   1954 							    APP_DPRINT("APP: [AAC encoder] --- will set stream gain to low\n");
   1955             				    pCompPrivateStructGain->sVolume.nValue = 0x2000;
   1956 							    error = OMX_SetConfig(*pHandle, OMX_IndexConfigAudioVolume, pCompPrivateStructGain);
   1957 							    if (error != OMX_ErrorNone)
   1958 								{
   1959 								    error = OMX_ErrorBadParameter;
   1960 								    goto EXIT;
   1961 								}
   1962 							}
   1963 						    if(nFrameCount == 15)
   1964 							{
   1965 							#ifdef OMX_GETTIME
   1966 								GT_START();
   1967 							#endif
   1968 						        error = OMX_SendCommand(*pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
   1969 						        if(error != OMX_ErrorNone)
   1970 								{
   1971 							        APP_DPRINT ("%d :: APP: Error from SendCommand-Idle(Stop) State function\n",__LINE__);
   1972 							        goto EXIT;
   1973 								}
   1974 						        done = 1;
   1975 								error = WaitForState(*pHandle, OMX_StateIdle);
   1976 							#ifdef OMX_GETTIME
   1977 								GT_END("Call to SendCommand <OMX_StateIdle>");
   1978 							#endif
   1979 								if(error != OMX_ErrorNone)
   1980 								{
   1981 									APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   1982 									goto EXIT;
   1983 								}
   1984 						        APP_DPRINT("%d :: APP: Shutting down ---------- \n",__LINE__);
   1985 							}
   1986 						    APP_DPRINT("%d :: APP: AAC ENCODER READING DATA FROM DASF  \n",__LINE__);
   1987 					}
   1988 					break;
   1989 
   1990 					default:
   1991 						APP_DPRINT("%d :: APP: ### Running Simple DEFAULT Case Here ###\n",__LINE__);
   1992 				} /* end of switch loop */
   1993 
   1994 
   1995 				if(FD_ISSET(OpBuf_Pipe[0], &rfds))
   1996 				{
   1997 					OMX_BUFFERHEADERTYPE* pBuf;
   1998 					read(OpBuf_Pipe[0], &pBuf, sizeof(pBuf));
   1999 
   2000 					if (firstbuffer){   /* Discard first buffer - Config audio (PV) */
   2001 						memset(pBuf->pBuffer, 0x0, pBuf->nAllocLen);
   2002 	                    pBuf->nFilledLen=0;
   2003 						firstbuffer = 0;
   2004 					}
   2005 					APP_DPRINT("%d :: App: Buffer to write to the file: %p \n",__LINE__,pBuf);
   2006 					APP_DPRINT("%d :: ------------- App File Write --------------\n",__LINE__);
   2007 					APP_DPRINT("%d :: App: %ld bytes are being written\n",__LINE__,(pBuf->nFilledLen));
   2008 					APP_DPRINT("%d :: ------------- App File Write --------------\n\n",__LINE__);
   2009 					nOpBuffs++;
   2010 					fwrite (pBuf->pBuffer, 1, (pBuf->nFilledLen), fOut);
   2011 					OMX_FillThisBuffer(*pHandle, pBuf);
   2012 					APP_DPRINT("%d :: APP: Sent %p Emptied Output Buffer = %d to Comp\n",__LINE__,pBuf,nFrameCount+1);
   2013 					nFrameCount++;
   2014 				}
   2015 
   2016 				if( FD_ISSET(Event_Pipe[0], &rfds) ) {
   2017 
   2018 				                OMX_U8 pipeContents;
   2019 				                read(Event_Pipe[0], &pipeContents, sizeof(OMX_U8));
   2020 
   2021 				                if (pipeContents == 0) {
   2022 				                    APP_IPRINT("Test app received OMX_ErrorResourcesPreempted\n");
   2023 				                    WaitForState(*pHandle,OMX_StateIdle);
   2024 
   2025 				                    error = OMX_FreeBuffer(pHandle,OMX_DirInput,pInputBufferHeader[i]);
   2026 				                    if( (error != OMX_ErrorNone)) {
   2027 				                        APP_DPRINT ("%d :: Error in Free Handle function\n",__LINE__);
   2028 				                    }
   2029 
   2030 				                    error = OMX_FreeBuffer(pHandle,OMX_DirOutput,pOutputBufferHeader[i]);
   2031 				                    if( (error != OMX_ErrorNone)) {
   2032 				                        APP_DPRINT ("%d:: Error in Free Handle function\n",__LINE__);
   2033 				                    }
   2034 #ifdef USE_BUFFER
   2035 
   2036 						for(i=0; i < numofinbuff; i++)
   2037 						{
   2038 							if (pInputBuffer[i] != NULL)
   2039 							{
   2040 							   pInputBuffer[i] = pInputBuffer[i] - 128;
   2041 							   APP_DPRINT("%d :: [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
   2042 							   if(pInputBuffer[i] != NULL)
   2043 							   {
   2044 									newfree(pInputBuffer[i]);
   2045 									pInputBuffer[i] = NULL;
   2046 							   }
   2047 							}
   2048 						}
   2049 
   2050 						for(i=0; i < numofoutbuff; i++)
   2051 						{
   2052 							if (pOutputBuffer[i] != NULL)
   2053 							{
   2054 								pOutputBuffer[i] = pOutputBuffer[i] - 128;
   2055 								APP_DPRINT("%d :: [TESTAPPFREE] pOutputBuffer[%d] = %p\n",__LINE__,i, pOutputBuffer[i]);
   2056 								if(pOutputBuffer[i] != NULL)
   2057 								{
   2058 									newfree(pOutputBuffer[i]);
   2059 									pOutputBuffer[i] = NULL;
   2060 							   }
   2061 							}
   2062 						}
   2063 #endif
   2064 
   2065 				                	OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
   2066 				                    WaitForState(*pHandle, OMX_StateLoaded);
   2067 
   2068 				                    OMX_SendCommand(*pHandle,OMX_CommandStateSet,OMX_StateWaitForResources,NULL);
   2069 				                    WaitForState(*pHandle,OMX_StateWaitForResources);
   2070 
   2071 				                }
   2072 				                else if (pipeContents == 1) {
   2073 
   2074 				                    APP_IPRINT("Test app received OMX_ErrorResourcesAcquired\n");
   2075 
   2076 				                    OMX_SendCommand(*pHandle,OMX_CommandStateSet,OMX_StateIdle,NULL);
   2077 				                    error = OMX_AllocateBuffer(pHandle,
   2078 				                                            &pOutputBufferHeader[i],
   2079 				                                            1,
   2080 				                                            NULL,
   2081 				                                            OutputBufferSize);
   2082 
   2083 				            		APP_DPRINT("%d :: called OMX_AllocateBuffer\n",__LINE__);
   2084 				            		if(error != OMX_ErrorNone) {
   2085 				            			APP_DPRINT("%d :: Error returned by OMX_AllocateBuffer()\n",__LINE__);
   2086 				            			goto EXIT;
   2087 				            		}
   2088 
   2089 				                    WaitForState(*pHandle,OMX_StateIdle);
   2090 
   2091 				                    OMX_SendCommand(*pHandle,OMX_CommandStateSet,OMX_StateExecuting,NULL);
   2092 				                    WaitForState(*pHandle,OMX_StateExecuting);
   2093 
   2094 				                    rewind(fIn);
   2095 
   2096 									if (!preempted)
   2097 									    OMX_EmptyThisBuffer(*pHandle, pInputBufferHeader[i]);
   2098 
   2099 				                   /* send_input_buffer (pHandle, pOutputBufferHeader, fIn); */
   2100 				                }
   2101 
   2102 				                if (pipeContents == 2) {
   2103 
   2104 #ifdef OMX_GETTIME
   2105                     GT_START();
   2106 #endif
   2107 				                    OMX_SendCommand(*pHandle,OMX_CommandStateSet,OMX_StateIdle,NULL);
   2108 				                    WaitForState(*pHandle,OMX_StateIdle);
   2109 #ifdef OMX_GETTIME
   2110                     GT_END("Call to SendCommand <OMX_StateIdle>");
   2111 #endif
   2112 
   2113 #ifdef WAITFORRESOURCES
   2114 
   2115 					for(i=0; i<numofinbuff; i++) {
   2116 
   2117 				                    error = OMX_FreeBuffer(pHandle,OMX_DirInput,pInputBufferHeader[i]);
   2118 				                    if( (error != OMX_ErrorNone)) {
   2119 				                        APP_DPRINT ("%d :: Error in Free Handle function\n",__LINE__);
   2120 				                    }
   2121 					}
   2122 					for(i=0; i<numofoutbuff; i++) {
   2123 
   2124 				                    error = OMX_FreeBuffer(pHandle,OMX_DirOutput,pOutputBufferHeader[i]);
   2125 				                    if( (error != OMX_ErrorNone)) {
   2126 				                        APP_DPRINT ("%d:: Error in Free Handle function\n",__LINE__);
   2127 				                    }
   2128 
   2129 					}
   2130 
   2131 				                	OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
   2132 				                    WaitForState(*pHandle, OMX_StateLoaded);
   2133 
   2134 				                    goto SHUTDOWN;
   2135 
   2136 #endif
   2137 
   2138 				                }
   2139 				            }
   2140 
   2141 				if(done == 1)
   2142 				{
   2143                     APP_DPRINT("%d :: APP: About to call GetState() \n",__LINE__);
   2144 					error = OMX_GetState(*pHandle, &state);
   2145 					if(error != OMX_ErrorNone)
   2146 					{
   2147 						APP_EPRINT("APP: Warning:  hAacEncoder->GetState has returned status %X\n", error);
   2148 						goto EXIT;
   2149 					}
   2150 				}
   2151 
   2152 				}
   2153 	            else if (preempted) {
   2154 	                sched_yield();
   2155 	            }
   2156 	            else {
   2157 	                goto SHUTDOWN;
   2158 	            }
   2159 			} /* end of while loop */
   2160 
   2161 			if (bPlayCompleted == OMX_TRUE )	/* Stop componet - just for F2F  mode*/
   2162 			{
   2163 			#ifdef OMX_GETTIME
   2164 				GT_START();
   2165 			#endif
   2166 				error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
   2167 				if(error != OMX_ErrorNone)
   2168 				{
   2169 					APP_EPRINT("APP: Error from SendCommand-Idle(Stop) State function\n");
   2170 					goto EXIT;
   2171 				}
   2172 				error = WaitForState(*pHandle, OMX_StateIdle);
   2173 
   2174 			#ifdef OMX_GETTIME
   2175 				GT_END("Call to SendCommand <OMX_StateIdle>");
   2176 			#endif
   2177 
   2178 				if(error != OMX_ErrorNone)
   2179 				{
   2180 					APP_EPRINT("APP: Error: WaitForState reports an error %X\n", error);
   2181 					goto EXIT;
   2182 				}
   2183 				bPlayCompleted = OMX_FALSE;
   2184 			}
   2185 
   2186 
   2187 /*----------------------------------------------
   2188 	 Final stage : cleaning and closing
   2189  ----------------------------------------------*/
   2190 			APP_DPRINT("%d :: APP: The current state of the component = %d \n",__LINE__,state);
   2191 			fclose(fOut);
   2192 			fclose(fIn);
   2193 			fOut=NULL;
   2194 			fIn=NULL;
   2195 			frmCount = 0;
   2196 		} /* End of internal loop*/
   2197 
   2198 	    error = OMX_SendCommand(*pHandle, OMX_CommandPortDisable, -1, NULL);
   2199         if(error != OMX_ErrorNone)
   2200 		{
   2201            APP_DPRINT("%d:: APP: Error from SendCommand OMX_CommandPortDisable\n",__LINE__);
   2202         	goto EXIT;
   2203 		}
   2204 
   2205 	    /* free the Allocate Buffers */
   2206 	    for(i=0; i < numofinbuff; i++)
   2207 		{
   2208 		   APP_DPRINT("%d :: APP: About to free pInputBufferHeader[%d]\n",__LINE__, i);
   2209 		   error = OMX_FreeBuffer(*pHandle, INPUT_PORT, pInputBufferHeader[i]);
   2210 		   if((error != OMX_ErrorNone))
   2211 		   {
   2212 			  APP_DPRINT("%d:: APP: Error in FreeBuffer function\n",__LINE__);
   2213 			  goto EXIT;
   2214 		   }
   2215 		}
   2216 	    for(i=0; i < numofoutbuff; i++)
   2217 		{
   2218 		   APP_DPRINT("%d :: APP: About to free pOutputBufferHeader[%d]\n",__LINE__, i);
   2219 		   error = OMX_FreeBuffer(*pHandle, OUTPUT_PORT, pOutputBufferHeader[i]);
   2220 		   if((error != OMX_ErrorNone))
   2221 		   {
   2222 			  APP_DPRINT("%d :: APP: Error in Free Buffer function\n",__LINE__);
   2223 			  goto EXIT;
   2224 		   }
   2225 		}
   2226 #ifdef USE_BUFFER
   2227 	    /* free the UseBuffers */
   2228 	    for(i=0; i < numofinbuff; i++)
   2229 		{
   2230 			if (pInputBuffer[i] != NULL)
   2231 			{
   2232 			   pInputBuffer[i] = pInputBuffer[i] - 128;
   2233 			   APP_DPRINT("%d :: [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
   2234 			   if(pInputBuffer[i] != NULL)
   2235 			   {
   2236 				  newfree(pInputBuffer[i]);
   2237 				  pInputBuffer[i] = NULL;
   2238 			   }
   2239 			}
   2240 		}
   2241 
   2242 	    for(i=0; i < numofoutbuff; i++)
   2243 		{
   2244 			if (pOutputBuffer[i] != NULL)
   2245 			{
   2246 			   pOutputBuffer[i] = pOutputBuffer[i] - 128;
   2247 			   APP_DPRINT("%d :: [TESTAPPFREE] pOutputBuffer[%d] = %p\n",__LINE__,i, pOutputBuffer[i]);
   2248 			   if(pOutputBuffer[i] != NULL)
   2249 			   {
   2250 				  newfree(pOutputBuffer[i]);
   2251 				  pOutputBuffer[i] = NULL;
   2252 			   }
   2253 			}
   2254 		}
   2255 #endif
   2256 
   2257 		/* --------Change to Loaded  ---------*/
   2258 		APP_DPRINT("%d :: APP: Sending the StateLoaded Command\n",__LINE__);
   2259 	#ifdef OMX_GETTIME
   2260 		GT_START();
   2261 	#endif
   2262 		error = OMX_SendCommand(*pHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
   2263 		if(error != OMX_ErrorNone)
   2264 		{
   2265 			APP_EPRINT("APP: Error from SendCommand-Idle State function\n");
   2266 			goto EXIT;
   2267 		}
   2268 		/* Wait for new state */
   2269 		error = WaitForState(*pHandle, OMX_StateLoaded);
   2270 	#ifdef OMX_GETTIME
   2271 		GT_END("Call to SendCommand <OMX_StateLoaded>");
   2272 	#endif
   2273 		if(error != OMX_ErrorNone)
   2274 		{
   2275 			APP_EPRINT("APP: Error:  hAacEncoder->WaitForState reports an error %X\n", error);
   2276 			goto EXIT;
   2277 
   2278 		}
   2279 		APP_DPRINT("%d :: APP: State Of Component Is Loaded Now\n",__LINE__);
   2280 
   2281 
   2282 #ifdef WAITFORRESOURCES
   2283     error = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateWaitForResources, NULL);
   2284     if(error != OMX_ErrorNone) {
   2285         APP_DPRINT ("%d Error from SendCommand-Idle State function\n",__LINE__);
   2286         goto EXIT;
   2287     }
   2288     error = WaitForState(pHandle, OMX_StateWaitForResources);
   2289 
   2290     /* temporarily put this here until I figure out what should really happen here */
   2291     sleep(10);
   2292     /* temporarily put this here until I figure out what should really happen here */
   2293 #endif
   2294 
   2295 
   2296 SHUTDOWN:
   2297 
   2298 		APP_MEMPRINT("%d :: [TESTAPPFREE] %p\n",__LINE__,pAacParam);
   2299         if(pAacParam != NULL)
   2300 		{
   2301 	        newfree(pAacParam);
   2302 	        pAacParam = NULL;
   2303 		}
   2304 		APP_MEMPRINT("%d :: [TESTAPPFREE] %p\n",__LINE__,iAacParam);
   2305         if(iAacParam != NULL)
   2306 		{
   2307 			APP_DPRINT("iAacParam %p \n",iAacParam);
   2308 	        newfree(iAacParam);
   2309 	        iAacParam = NULL;
   2310 			APP_DPRINT("iAacParam %p \n",iAacParam);
   2311 		}
   2312         APP_MEMPRINT("%d :: [TESTAPPFREE] %p\n",__LINE__,pCompPrivateStruct);
   2313 	    if(pCompPrivateStruct != NULL)
   2314 		{
   2315 		    newfree(pCompPrivateStruct);
   2316 		    pCompPrivateStruct = NULL;
   2317 		}
   2318 		APP_MEMPRINT("%d :: [TESTAPPFREE] %p\n",__LINE__,pCompPrivateStructGain);
   2319 	    if(pCompPrivateStructGain != NULL)
   2320 		{
   2321 		   newfree(pCompPrivateStructGain);
   2322 		    pCompPrivateStructGain = NULL;
   2323 		}
   2324 		APP_MEMPRINT("%d :: [TESTAPPFREE] %p\n",__LINE__,audioinfo.aacencHeaderInfo);
   2325 	    if(audioinfo.aacencHeaderInfo != NULL)
   2326 		{
   2327 		    newfree(audioinfo.aacencHeaderInfo);
   2328 		    audioinfo.aacencHeaderInfo = NULL;
   2329 		}
   2330 	    error = close (IpBuf_Pipe[0]);
   2331 	    if (0 != error && OMX_ErrorNone == error)
   2332 		{
   2333 		    error = OMX_ErrorHardware;
   2334 		    APP_DPRINT("%d :: APP: Error while closing IpBuf_Pipe[0]\n",__LINE__);
   2335 		    goto EXIT;
   2336 		}
   2337 	    error = close (IpBuf_Pipe[1]);
   2338 	    if (0 != error && OMX_ErrorNone == error)
   2339 		{
   2340 		    error = OMX_ErrorHardware;
   2341 		    APP_DPRINT("%d :: APP: Error while closing IpBuf_Pipe[1]\n",__LINE__);
   2342 		    goto EXIT;
   2343 		}
   2344 	    error = close (OpBuf_Pipe[0]);
   2345 	    if (0 != error && OMX_ErrorNone == error)
   2346 		{
   2347 		    error = OMX_ErrorHardware;
   2348 		    APP_DPRINT("%d :: APP: Error while closing OpBuf_Pipe[0]\n",__LINE__);
   2349 		    goto EXIT;
   2350 		}
   2351 	    error = close (OpBuf_Pipe[1]);
   2352 	    if (0 != error && OMX_ErrorNone == error)
   2353 		{
   2354 		    error = OMX_ErrorHardware;
   2355 		    APP_DPRINT("%d :: APP: Error while closing OpBuf_Pipe[1]\n",__LINE__);
   2356 		    goto EXIT;
   2357 		}
   2358 
   2359 		error = close(Event_Pipe[0]);
   2360 	if (0 != error && OMX_ErrorNone == error) {
   2361 		error = OMX_ErrorHardware;
   2362 		APP_DPRINT("%d :: Error while closing Event_Pipe[0]\n",__LINE__);
   2363 		goto EXIT;
   2364 	}
   2365 
   2366 	error = close(Event_Pipe[1]);
   2367 	if (0 != error && OMX_ErrorNone == error) {
   2368 		error = OMX_ErrorHardware;
   2369 		APP_DPRINT("%d :: Error while closing Event_Pipe[1]\n",__LINE__);
   2370 		goto EXIT;
   2371 	}
   2372 
   2373 #ifdef DSP_RENDERING_ON
   2374 	    cmd_data.hComponent = *pHandle;
   2375 	    cmd_data.AM_Cmd = AM_Exit;
   2376 
   2377 	    if((write(Aacenc_fdwrite, &cmd_data, sizeof(cmd_data)))<0)
   2378 	        APP_EPRINT("%d :: APP: ---send command to audio manager\n",__LINE__);
   2379 
   2380 	    close(Aacenc_fdwrite);
   2381 	    close(Aacenc_fdread);
   2382 #endif
   2383 
   2384 	    /* Free the AacEncoder Component */
   2385 		error = TIOMX_FreeHandle(*pHandle);
   2386 		if( (error != OMX_ErrorNone)) {
   2387 			APP_EPRINT("APP: Error in Free Handle function\n");
   2388 			goto EXIT;
   2389 		}
   2390 		APP_DPRINT("%d :: App: pHandle = %p\n",__LINE__,pHandle);
   2391 		APP_IPRINT("%d :: APP: Free Handle returned Successfully \n",__LINE__);
   2392 
   2393 		error = TIOMX_Deinit();
   2394 		if( (error != OMX_ErrorNone)) {
   2395 			APP_EPRINT("APP: Error in Deinit Core function\n");
   2396 			goto EXIT;
   2397 		}
   2398 
   2399 		error= newfree(pHandle);
   2400 		if( (error != OMX_ErrorNone)) {
   2401 			APP_EPRINT("APP: Error in free PHandle\n");
   2402 			goto EXIT;
   2403 		}
   2404 
   2405 
   2406     } /*--------- end of for loop--------- */
   2407 
   2408 	error= CleanList(&ListHeader);			/* it frees streaminfo */
   2409 	if( (error != OMX_ErrorNone))
   2410 	{
   2411 		APP_DPRINT("APP: Error in CleanList function\n");
   2412 		goto EXIT;
   2413 	}
   2414 
   2415 	pthread_cond_destroy(&WaitForStateMutex.cond);
   2416 	pthread_mutex_destroy(&WaitForStateMutex.Mymutex);
   2417 
   2418 #ifdef AACENC_DEBUGMEM
   2419 	APP_IPRINT("\n-Printing memory not delete-\n");
   2420     for(r=0;r<500;r++)
   2421 	{
   2422         if (lines[r]!=0){
   2423              APP_IPRINT(" --->%d Bytes allocated on %p File:%s Line: %d\n",bytes[r],arr[r],file[r],lines[r]);
   2424         }
   2425 
   2426     }
   2427 #endif
   2428 
   2429 
   2430 EXIT:
   2431 	if(bInvalidState==OMX_TRUE)
   2432 	{
   2433 #ifndef USE_BUFFER
   2434 
   2435 		error = FreeAllResources(*pHandle,
   2436 								pInputBufferHeader[0],
   2437 								pOutputBufferHeader[0],
   2438 								numofinbuff,
   2439 								numofoutbuff,
   2440 								fIn,fOut,ListHeader);
   2441 #else
   2442 		error = freeAllUseResources(*pHandle,
   2443 									pInputBuffer,
   2444 									pOutputBuffer,
   2445 									numofinbuff,
   2446 									numofoutbuff,
   2447 									fIn,fOut,ListHeader);
   2448 #endif
   2449 	}
   2450 #ifdef OMX_GETTIME
   2451   GT_END("AAC_ENC test <End>");
   2452   OMX_ListDestroy(pListHead);
   2453 #endif
   2454 
   2455     return error;
   2456 }
   2457 
   2458 
   2459 /*-------------------------------------------------------------------*/
   2460 /**
   2461   *  mymalloc() function to perform dynamic memory allocation.
   2462   *
   2463   * @param size         			Size of memory requested
   2464   * @param ListHeader		Top pointer of the linked List
   2465   *
   2466   * @retval p   				Pointer to the allocated memory
   2467   *
   2468   **/
   2469 /*-------------------------------------------------------------------*/
   2470 
   2471 void * mymalloc(int size,ListMember** ListHeader)
   2472 {
   2473    int error=0;
   2474    void *p;
   2475    p = malloc(size);
   2476 
   2477    if(p==NULL)
   2478    	{
   2479        APP_EPRINT("APP: Memory not available\n");
   2480        exit(1);
   2481     }
   2482    else
   2483    	{
   2484 		error = AddMemberToList(p,ListHeader);
   2485 		if(error)
   2486 			exit(1);
   2487 	    return p;
   2488     }
   2489 }
   2490 
   2491  /*-------------------------------------------------------------------*/
   2492  /**
   2493    *  myfree() function to free dynamic memory allocated.
   2494    *
   2495    * @param dp				 Dinamic memory pointer to be freed
   2496    * @param ListHeader		 Top pointer of the linked List
   2497    *
   2498    * @retval OMX_ErrorNone	 Success on freeing memory
   2499    *
   2500    **/
   2501  /*-------------------------------------------------------------------*/
   2502 
   2503 int myfree(void *dp, ListMember** ListHeader)
   2504 {
   2505 	  int error=0;
   2506 	  error = FreeListMember(dp, ListHeader);
   2507      /* free(dp);  */
   2508 	  if (error)
   2509 	  	APP_EPRINT("APP: Error freeing \n");
   2510 
   2511 	  return error;
   2512 }
   2513 
   2514 
   2515 /*-------------------------------------------------------------------*/
   2516 /**
   2517   *  FreeAllResources() function that release all allocated resources when an important error is produced.
   2518  * 					 Buffers were allocated by component
   2519   *
   2520   * @parameters  pointers  from most of allocated resources
   2521   *
   2522   *
   2523   * @retval OMX_ErrorNone		Success on freeing resources
   2524   *
   2525   **/
   2526 /*-------------------------------------------------------------------*/
   2527 
   2528 OMX_ERRORTYPE FreeAllResources( OMX_HANDLETYPE pHandle,
   2529 			                OMX_BUFFERHEADERTYPE* pBufferIn,
   2530 			                OMX_BUFFERHEADERTYPE* pBufferOut,
   2531 			                int NIB, int NOB,
   2532 			                FILE* fIn, FILE* fOut,
   2533 			                ListMember* ListHeader)
   2534 {
   2535 	APP_DPRINT("%d:: APP: Freeing all resources by state invalid \n",__LINE__);
   2536 	OMX_ERRORTYPE eError = OMX_ErrorNone;
   2537 	OMX_U16 i;
   2538 	for(i=0; i < NIB; i++)
   2539 	{
   2540 		   APP_DPRINT("%d :: APP: About to free pInputBufferHeader[%d]\n",__LINE__, i);
   2541 		   eError = OMX_FreeBuffer(pHandle, INPUT_PORT, pBufferIn+i);
   2542 		 	if(eError != OMX_ErrorNone)
   2543 			{
   2544 				APP_DPRINT("APP: Error:  Freebuffer by MMU_Fault %X\n", eError);
   2545 				goto EXIT_ERROR;
   2546 			}
   2547 
   2548 	}
   2549 	for(i=0; i < NOB; i++)
   2550 	{
   2551 		   APP_DPRINT("%d :: APP: About to free pOutputBufferHeader[%d]\n",__LINE__, i);
   2552 		   eError = OMX_FreeBuffer(pHandle, OUTPUT_PORT, pBufferOut+i);
   2553 		   if(eError != OMX_ErrorNone)
   2554 		   {
   2555 			   APP_DPRINT("APP: Error:	Freebuffer by MMU_Fault %X\n", eError);
   2556 			   goto EXIT_ERROR;
   2557 		   }
   2558 	}
   2559 
   2560 	/* Freeing Linked list */
   2561 	eError= CleanList(&ListHeader);
   2562 	if( (eError != OMX_ErrorNone))
   2563 	{
   2564 		APP_DPRINT("APP: Error in CleanList function\n");
   2565 		goto EXIT_ERROR;
   2566 	}
   2567 
   2568 	pthread_cond_destroy(&WaitForStateMutex.cond);
   2569 	pthread_mutex_destroy(&WaitForStateMutex.Mymutex);
   2570 
   2571     eError = close (IpBuf_Pipe[0]);
   2572     eError = close (IpBuf_Pipe[1]);
   2573     eError = close (OpBuf_Pipe[0]);
   2574     eError = close (OpBuf_Pipe[1]);
   2575 	if(fOut != NULL)
   2576 	{
   2577 		fclose(fOut);
   2578 		fOut=NULL;
   2579 	}
   2580 
   2581 	if(fIn != NULL)
   2582 	{	fclose(fIn);
   2583 		fIn=NULL;
   2584 	}
   2585 
   2586 	eError = TIOMX_FreeHandle(pHandle);
   2587 	if( (eError != OMX_ErrorNone))
   2588 	{
   2589 		APP_EPRINT("APP: Error in Free Handle function\n");
   2590 		goto EXIT_ERROR;
   2591 	}
   2592 	eError = TIOMX_Deinit();
   2593 
   2594 EXIT_ERROR:
   2595 
   2596 	return eError;
   2597 }
   2598 
   2599 
   2600 /*-------------------------------------------------------------------*/
   2601 /**
   2602   *  freeAllUseResources() function that release all allocated resources from APP
   2603   * 						when an important error is produced. Buffers were allocated by App.
   2604   *
   2605   * @parameters  pointers  from most of allocated resources
   2606   *
   2607   *
   2608   * @retval OMX_ErrorNone		Success on freeing resources
   2609   *
   2610   **/
   2611 /*-------------------------------------------------------------------*/
   2612 
   2613 #ifdef USE_BUFFER
   2614 
   2615 OMX_ERRORTYPE freeAllUseResources(OMX_HANDLETYPE pHandle,
   2616 							OMX_U8* UseInpBuf[],
   2617 							OMX_U8* UseOutBuf[],
   2618 							int NIB,int NOB,
   2619 							FILE* fIn, FILE* fOut,
   2620 							ListMember* ListHeader)
   2621 {
   2622 
   2623 		OMX_ERRORTYPE eError = OMX_ErrorNone;
   2624 		OMX_U16 i;
   2625 		APP_DPRINT("%d ::APP: Freeing all resources by state invalid \n",__LINE__);
   2626     	/* free the UseBuffers */
   2627 	    for(i=0; i < NIB; i++)
   2628 		{
   2629 
   2630 			if( UseInpBuf[i] != NULL )
   2631 			{
   2632 			   UseInpBuf[i] = UseInpBuf[i] - 128;
   2633 			   APP_DPRINT("%d :: [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,(UseInpBuf[i]));
   2634 			   if(UseInpBuf[i] != NULL)
   2635 			   {
   2636 				  newfree(UseInpBuf[i]);
   2637 				  UseInpBuf[i] = NULL;
   2638 			   }
   2639 			}
   2640 		}
   2641 
   2642 	    for(i=0; i < NOB; i++)
   2643 		{
   2644 			if (UseOutBuf[i] != NULL)
   2645 			{
   2646 			   UseOutBuf[i] = UseOutBuf[i] - 128;
   2647 			   APP_DPRINT("%d :: [TESTAPPFREE] pOutputBuffer[%d] = %p\n",__LINE__,i, UseOutBuf[i]);
   2648 			   if(UseOutBuf[i] != NULL)
   2649 			   {
   2650 				  newfree(UseOutBuf[i]);
   2651 				  UseOutBuf[i] = NULL;
   2652 			   }
   2653 			}
   2654 		}
   2655 
   2656 		/* Freeing Linked list */
   2657 		eError= CleanList(&ListHeader);
   2658 		if( (eError != OMX_ErrorNone))
   2659 		{
   2660 			APP_DPRINT("APP: Error in CleanList function\n");
   2661 			goto EXIT_ERROR;
   2662 		}
   2663 
   2664 		pthread_cond_destroy(&WaitForStateMutex.cond);
   2665 		pthread_mutex_destroy(&WaitForStateMutex.Mymutex);
   2666 
   2667 		eError = close (IpBuf_Pipe[0]);
   2668 		eError = close (IpBuf_Pipe[1]);
   2669 		eError = close (OpBuf_Pipe[0]);
   2670 		eError = close (OpBuf_Pipe[1]);
   2671 
   2672 		if (fOut != NULL)	/* Could have been closed  previously */
   2673 		{
   2674 			fclose(fOut);
   2675 			fOut=NULL;
   2676 		}
   2677 
   2678 		if (fIn != NULL)
   2679 		{	fclose(fIn);
   2680 			fIn=NULL;
   2681 		}
   2682 
   2683 		eError = TIOMX_FreeHandle(pHandle);
   2684 		if( (eError != OMX_ErrorNone))
   2685 		{
   2686 			APP_EPRINT("APP: Error in Free Handle function\n");
   2687 			goto EXIT_ERROR;
   2688 		}
   2689 		eError = TIOMX_Deinit();
   2690 
   2691 EXIT_ERROR:
   2692 
   2693 		return eError;
   2694 
   2695 }
   2696 
   2697 #endif
   2698 
   2699 
   2700 
   2701 /*-------------------------------------------------------------------*/
   2702 /**
   2703   *  ValidateParameters()  Function that validates the sample rate and Bitarete combination according to a
   2704  * 						defined values table
   2705   *
   2706   * @parameters  SampleRate		  Sample rate value
   2707   *				numChannels	  Number of channels
   2708   *				BitRate			  Bit rate value
   2709   *
   2710   *
   2711   * @retval ValidParameter		  Boolean value for validation
   2712   *
   2713   **/
   2714 /*-------------------------------------------------------------------*/
   2715 
   2716 OMX_BOOL ValidateParameters(OMX_U32 SampleRate, OMX_U32 numChannels, OMX_U32 BitRate, OMX_U32 ObjectType)
   2717 {
   2718 	OMX_U32 LimitsLC [12][4]={{8000  ,MONO   ,8000  ,42000 },
   2719 				   			{8000  ,STEREO ,16000 ,84000 },
   2720 				   			{16000 ,MONO   ,8000  ,84000 },
   2721 				   			{16000 ,STEREO ,16000 ,168000},
   2722 				   			{22050 ,STEREO ,16000 ,232000},
   2723 							{22050 ,STEREO ,16000 ,232000},
   2724 				   			{32000 ,STEREO ,16000 ,320000},
   2725 				   			{32000 ,STEREO ,16000 ,320000},
   2726 				   			{44100 ,MONO   ,8000  ,160000},
   2727 				  			{44100 ,STEREO ,16000 ,320000},
   2728 				   			{48000 ,MONO   ,8000  ,160000},
   2729 				   			{48000 ,STEREO ,16000 ,320000} };
   2730 
   2731 	OMX_U32 LimitsHE [12][4]={{16000,MONO  ,8000  ,48000 },
   2732 				   			{16000 ,STEREO ,16000 ,128000},
   2733 				   			{22050 ,MONO   ,8000  ,64000 },
   2734 				   			{22050 ,STEREO ,16000 ,128000},
   2735 				   			{24000 ,MONO   ,8000  ,64000 },
   2736 							{24000 ,STEREO ,16000 ,128000},
   2737 				   			{32000 ,MONO   ,8000  ,64000 },
   2738 				   			{32000 ,STEREO ,16000 ,128000},
   2739 				   			{44100 ,MONO   ,12000 ,64000 },
   2740 				  			{44100 ,STEREO ,16000 ,128000},
   2741 				   			{48000 ,MONO   ,12000 ,64000 },
   2742 				   			{48000 ,STEREO ,16000 ,128000} };
   2743 
   2744 	OMX_U32 LimitsHE2 [6][4]={{16000,STEREO,8000  ,48000},
   2745 				   			{22050 ,STEREO ,8000  ,64000},
   2746 				   			{24000 ,STEREO ,8000  ,64000},
   2747 				   			{32000 ,STEREO ,8000  ,64000},
   2748 				   			{44100 ,STEREO ,12000 ,64000},
   2749 							{48000 ,STEREO ,12000 ,64000}, };
   2750 
   2751 	OMX_BOOL ValidParameter = OMX_TRUE;
   2752 	OMX_BOOL isDone = OMX_FALSE;
   2753 	OMX_U16 i,j;
   2754 
   2755 
   2756 	APP_DPRINT("Inside validateparameters \n");
   2757 	APP_DPRINT("sample rate %d \n",(int)SampleRate);
   2758 	APP_DPRINT("Bit rate %d \n", (int)BitRate);
   2759 
   2760 
   2761 	if(ObjectType == ObjectTypeHE) {
   2762 		for (i=0;i<12;i+=2)
   2763 		{
   2764 			APP_DPRINT("APP: sample compared %d : %d \n",(int)SampleRate, (int)LimitsHE[i][0]);
   2765 			if( SampleRate == LimitsHE[i][0] )  							/*check for  sample rate */
   2766 			{
   2767 				for ( j=0; j<2; j++)
   2768 				{
   2769 					APP_DPRINT("APP: channels compared %d : %d \n", (int)numChannels, (int)LimitsHE[i+j][1]);
   2770 					if ( numChannels== LimitsHE[i+j][1] )					/* check for mono/stereo */
   2771 					{
   2772 						if ( (BitRate >= LimitsHE[i+j][2] ) && (BitRate <= LimitsHE[i+j][3]) )
   2773 						{
   2774 							ValidParameter = OMX_TRUE;					/* check for value is within range */
   2775 							isDone		   = OMX_TRUE;
   2776 							break;
   2777 						}
   2778 						else
   2779 						{
   2780 							ValidParameter = OMX_FALSE;					/* Value is out is out of range */
   2781 							break;
   2782 						}
   2783 					}
   2784 					else
   2785 					{
   2786 						ValidParameter		=OMX_FALSE;
   2787 					}
   2788 				}
   2789 				if( (!ValidParameter) || (isDone) )						/* No need to keep searching */
   2790 					break;
   2791 			}
   2792 		}
   2793 	}
   2794 
   2795 
   2796 	else if(ObjectType == ObjectTypeHE2) {
   2797 		for (i=0;i<6;i++)
   2798 		{
   2799 			APP_DPRINT("APP: sample compared %d : %d \n",(int)SampleRate, (int)LimitsHE2[i][0]);
   2800 			if( SampleRate == LimitsHE2[i][0] )  							/*check for  sample rate */
   2801 			{
   2802 					APP_DPRINT("APP: channels compared %d : %d \n", (int)numChannels, (int)LimitsHE2[i+j][1]);
   2803 					if ( numChannels== STEREO )					/* check for stereo only*/
   2804 					{
   2805 						if ( (BitRate >= LimitsHE2[i][2] ) && (BitRate <= LimitsHE2[i][3]) )
   2806 						{
   2807 							ValidParameter = OMX_TRUE;					/* check for value is within range */
   2808 							isDone		   = OMX_TRUE;
   2809 							break;
   2810 						}
   2811 						else
   2812 						{
   2813 							ValidParameter = OMX_FALSE;					/* Value is out is out of range */
   2814 							break;
   2815 						}
   2816 					}
   2817 					else
   2818 					{
   2819 						ValidParameter		=OMX_FALSE;
   2820 					}
   2821 				if( (!ValidParameter) || (isDone) )						/* No need to keep searching */
   2822 					break;
   2823 			}
   2824 		}
   2825 
   2826 	}
   2827 
   2828 	else{   						/* ObjectType == ObjectTypeLC */
   2829 		for (i=0;i<12;i+=2)
   2830 		{
   2831 			APP_DPRINT("APP: sample compared %d : %d \n",(int)SampleRate, (int)LimitsLC[i][0]);
   2832 			if( SampleRate == LimitsLC[i][0] )  							/*check for  sample rate */
   2833 			{
   2834 				for ( j=0; j<2; j++)
   2835 				{
   2836 					APP_DPRINT("APP: channels compared %d : %d \n", (int)numChannels, (int)LimitsLC[i+j][1]);
   2837 					if ( numChannels== LimitsLC[i+j][1] )					/* check for mono/stereo */
   2838 					{
   2839 						if ( (BitRate >= LimitsLC[i+j][2] ) && (BitRate <= LimitsLC[i+j][3]) )
   2840 						{
   2841 							ValidParameter = OMX_TRUE;					/* check for value is within range */
   2842 							isDone		   = OMX_TRUE;
   2843 							break;
   2844 						}
   2845 						else
   2846 						{
   2847 							ValidParameter = OMX_FALSE;					/* Value is out is out of range */
   2848 							break;
   2849 						}
   2850 					}
   2851 					else
   2852 					{
   2853 						ValidParameter		=OMX_FALSE;
   2854 					}
   2855 				}
   2856 				if( (!ValidParameter) || (isDone) )						/* No need to keep searching */
   2857 					break;
   2858 			}
   2859 		}
   2860 	}
   2861 
   2862 
   2863 return ValidParameter;
   2864 
   2865 /*NOTE: 11025 and 22050 values for Sample rates are missing in the table. */
   2866 
   2867 }
   2868 
   2869 
   2870 /*-------------------------------------------------------------------*/
   2871 /**
   2872   *  CalculateOutputBufferSize() 	Calculates an optimun size per output
   2873   * 								buffer according to the number of frames
   2874   *
   2875   * @param   SampleFrec			Sample frecuency test parameter
   2876   * @param   BitRate				Bit rate test parameter
   2877   * @param   FramesPerOutBuf		Required number of frames
   2878   *
   2879   * @retval 	OutputBufferSize		Calculated buffer size
   2880   **/
   2881 /*-------------------------------------------------------------------*/
   2882 
   2883 OMX_U32 CalculateOutputBufferSize (OMX_U32 SampleFrec, OMX_U32 BitRate, OMX_U16 FramesPerOutBuf  )
   2884 {
   2885 	float AvgFrameSize		=0;
   2886 	float FramePerSec		=0;
   2887 	float fOutBufSize		=0;
   2888 	float mantissa			=0;
   2889 	OMX_U32 OutputBufferSize=0;
   2890 
   2891 	FramePerSec= (float)SampleFrec / 1024;								/*	 Sample frec /1024	*/
   2892 		APP_DPRINT("FramePerSec %f \n",FramePerSec);
   2893 	AvgFrameSize= ((BitRate/FramePerSec)/8)* 1.3; 						/*	 ( (BitRate/framesPerSec)/8 ) * 1.3  */
   2894 		APP_DPRINT("AvgFrameSize %f \n",AvgFrameSize);
   2895 	fOutBufSize= (AvgFrameSize * (float)FramesPerOutBuf)+1200;			/*	(AverageFrameSize * NumFrames)+1200   */
   2896 		APP_DPRINT("float- size output buffers %f \n",fOutBufSize);
   2897 	OutputBufferSize = (OMX_U32)fOutBufSize;
   2898 		APP_DPRINT("U32- size output buffers %d \n", (int)OutputBufferSize);
   2899 	mantissa = fOutBufSize - OutputBufferSize;
   2900 	if(mantissa > 0)		/*rounding-up*/
   2901 	{
   2902 		OutputBufferSize++; 											/* Caluculated Ouput Buffer size */
   2903 	}
   2904 	APP_DPRINT("(Rounded) size output buffers %d \n",(int)OutputBufferSize);
   2905 
   2906 	return OutputBufferSize;
   2907 
   2908 
   2909 }
   2910 
   2911 
   2912 /*-------------------------------------------------------------------*/
   2913 /**
   2914   *  AddMemberToList() Adds a memeber to the list for allocated memory pointers
   2915   *
   2916   * @param ptr         			memory pointer to add to the member list
   2917   * @param ListHeader		Top pointer of the List
   2918   *   *
   2919   * @retval OMX_ErrorNone   					 Success, member added
   2920  *               OMX_ErrorInsufficientResources		 Memory  failure
   2921   **/
   2922 /*-------------------------------------------------------------------*/
   2923 
   2924 OMX_ERRORTYPE AddMemberToList(void* ptr, ListMember** ListHeader)
   2925 {
   2926 	int Error = OMX_ErrorNone;										/* No Error  */
   2927 	static int InstanceCounter = 0;
   2928 	ListMember* temp;
   2929 	if(*ListHeader == NULL)
   2930 	{
   2931 		InstanceCounter =0;											/* reset counter */
   2932 	}
   2933 
   2934 	temp = (ListMember*)malloc(sizeof(ListMember));					/* New Member */
   2935 	if(NULL == temp)
   2936 	{
   2937 		APP_EPRINT("%d :: App: Malloc Failed\n",__LINE__);
   2938 		Error = OMX_ErrorInsufficientResources;						/* propper Error */
   2939 		goto EXIT_ERROR;
   2940 	}
   2941 	APP_DPRINT("\nNew Instance created pointer : %p \n",temp);
   2942 
   2943 	APP_DPRINT("Header parameter pointer : %p \n",*ListHeader);
   2944 	temp->NextListMember 		= *ListHeader;						/* Adding the new member */
   2945 	APP_DPRINT("Next linked pointer  : %p \n",temp->NextListMember);
   2946 	temp->ListCounter			= ++InstanceCounter;				/* Pre-increment */
   2947 	APP_DPRINT("Instance counter %d \n",temp->ListCounter);
   2948 	temp->Struct_Ptr 			= ptr;								/* Saving passed pointer (allocated memory)*/
   2949 	APP_DPRINT("Parameter pointer to save : %p \n",ptr);
   2950 	*ListHeader				 	= temp;								/* saving the Header */
   2951 	APP_DPRINT("New Header pointer : %p \n",*ListHeader);
   2952 
   2953 
   2954 EXIT_ERROR:
   2955 	return Error;
   2956 }
   2957 
   2958 
   2959 /*-------------------------------------------------------------------*/
   2960 /**
   2961   * CleanList() Frees the complete Linked list from memory
   2962   *
   2963   *  @param ListHeader				Root List  pointer
   2964   *
   2965   * @retval OMX_ErrorNone   			 Success, memory freed
   2966  *               OMX_ErrorUndefined		 Memory  failure
   2967   **/
   2968 /*-------------------------------------------------------------------*/
   2969 
   2970 OMX_ERRORTYPE CleanList(ListMember** ListHeader)
   2971 {
   2972 	int Error = OMX_ErrorNone;										/* No Error  */
   2973 	int ListCounter=0;
   2974 	ListMember* Temp;												/* Temporal pointer */
   2975 	ListCounter = (*ListHeader)->ListCounter;
   2976 
   2977 	while (*ListHeader != NULL)
   2978 	{
   2979 		APP_DPRINT("\nNum Instance to free %d \n",(*ListHeader)->ListCounter);
   2980 		Temp=(*ListHeader)->NextListMember;
   2981 		if( (*ListHeader)->Struct_Ptr != NULL)						/* Ensure there is something to free */
   2982 		{
   2983 			APP_DPRINT(" Struct to free %p \n",(*ListHeader)->Struct_Ptr);
   2984 			free((*ListHeader)->Struct_Ptr);						/* Free memory */
   2985 			(*ListHeader)->Struct_Ptr = NULL;						/* Point to NULL */
   2986 		}
   2987 		else
   2988 		{
   2989 			APP_DPRINT("APP:  Warning: Content has alreadey been freed \n");
   2990 		}
   2991 
   2992 		APP_DPRINT("freeing List Member %p \n",*ListHeader);
   2993 		free(*ListHeader);											/* Free member (first) */
   2994 		*ListHeader=Temp;											/* Asign Next member to header  */
   2995 	}
   2996 	if(*ListHeader== NULL)
   2997 	{
   2998 		APP_DPRINT("%d :: App: Freed the complete list: Header = %p \n",__LINE__,*ListHeader);
   2999 	}
   3000 	else
   3001 	{
   3002 		APP_EPRINT("%d :: App: Error Freeing List \n",__LINE__);
   3003 		Error =OMX_ErrorUndefined;
   3004 	}
   3005 	return Error;
   3006 }
   3007 
   3008 
   3009 /*-------------------------------------------------------------------*/
   3010 /**
   3011   * FreeListMember() Frees  a member from the Linked list with its member allocated memory
   3012   *
   3013   *  @param ListHeader				 Root List  pointer
   3014   *
   3015   * @retval OMX_ErrorNone   			 Success, member freed
   3016  *              OMX_ErrorUndefined		 Memory  failure
   3017   **/
   3018 /*-------------------------------------------------------------------*/
   3019 
   3020 OMX_ERRORTYPE FreeListMember(void* ptr, ListMember** ListHeader)
   3021 {
   3022 
   3023 	int Error = OMX_ErrorNone;													/* No Error  */
   3024 	int ListCounter=0;
   3025 	ListMember* Temp= NULL;															/* Temporal pointer */
   3026 	ListMember*	Backtemp=NULL;														/* follower of temporal pointer */
   3027 	ListCounter = (*ListHeader)->ListCounter;
   3028 	int count=0;
   3029 
   3030 	Temp =(*ListHeader);
   3031 	if (Temp != NULL)
   3032 	{
   3033 		while(Temp != NULL && Temp->Struct_Ptr != ptr)
   3034 		{	Backtemp = Temp;													/* One member back  */
   3035 			Temp = Temp->NextListMember;										/* next member */
   3036 			count++;
   3037 		}
   3038 		APP_DPRINT("Search ends: %p \n",Temp);
   3039 		if (Temp != NULL)														/* found it */
   3040 		{
   3041 			if (count > 0)														/* inside the List */
   3042 			{
   3043 				Backtemp->NextListMember = Temp->NextListMember;
   3044 				APP_DPRINT("About to free Content of List member: %p \n",Temp->Struct_Ptr);
   3045 				free(Temp->Struct_Ptr); 										/* free content */
   3046 				APP_DPRINT("About to free List member: %p Number: %d \n",Temp,Temp->ListCounter);
   3047 				free(Temp); 													/* free element */
   3048 			}
   3049 			else if (count == 0)												/* it was the first */
   3050 			{
   3051 					APP_DPRINT("About to free FIRST List member: %p \n",*ListHeader);
   3052 					Temp = Temp->NextListMember;								/* next member */
   3053 					free((*ListHeader)->Struct_Ptr);							/* free content */
   3054 					free(*ListHeader);											/* Free member (first) */
   3055 					*ListHeader=Temp;											/* Asign Next member to header  */
   3056 			}
   3057 		}
   3058 		else{																	/* Not found it */
   3059 			APP_DPRINT("Nothing to free \n");
   3060 		}
   3061 
   3062 	}
   3063 	else{
   3064 		APP_DPRINT("List is empty \n");
   3065 	}
   3066 
   3067 	return Error;
   3068 
   3069 }
   3070 
   3071 
   3072 
   3073 
   3074