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 *             Texas Instruments OMAP (TM) Platform Software
19 *  (c) Copyright Texas Instruments, Incorporated.  All Rights Reserved.
20 *
21 *  Use of this software is controlled by the terms and conditions found
22 *  in the license agreement under which this software has been supplied.
23 * =========================================================================== */
24/**
25 * @file G729Enc_Test.c
26 *
27 * This file implements G729 Encoder Component Test Application to verify
28 * which is fully compliant with the Khronos OpenMAX (TM) 1.0 Specification
29 *
30 * @path  $(CSLPATH)\OMAPSW_MPU\linux\audio\src\openmax_il\g729_enc\tests
31 *
32 * @rev  1.0
33 */
34/* ----------------------------------------------------------------------------
35 *!
36 *! Revision History
37 *! ===================================
38 *! 21-sept-2006 bk: updated review findings for alpha release
39 *! 24-Aug-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests some more
40 *! 18-July-2006 bk: Khronos OpenMAX (TM) 1.0 Conformance tests validated for few cases
41 *! 21-Jun-2006 bk: Khronos OpenMAX (TM) 1.0 migration done
42 *! 22-May-2006 bk: DASF recording quality improved
43 *! 19-Apr-2006 bk: DASF recording speed issue resloved
44 *! 23-Feb-2006 bk: DASF functionality added
45 *! 18-Jan-2006 bk: Repated recording issue fixed and LCML changes taken care
46 *! 14-Dec-2005 bk: Initial Version
47 *! 16-Nov-2005 bk: Initial Version
48 *! 23-Sept-2005 bk: Initial Version
49 *! 10-Sept-2005 bk: Initial Version
50 *! 10-Sept-2005 bk:
51 *! This is newest file
52 * =========================================================================== */
53/* ------compilation control switches -------------------------*/
54/****************************************************************
55 *  INCLUDE FILES
56 ****************************************************************/
57/* ----- system and platform files ----------------------------*/
58
59#include <unistd.h>
60#include <sys/ioctl.h>
61#include <sys/select.h>
62#include <errno.h>
63#include <linux/vt.h>
64#include <signal.h>
65#include <sys/stat.h>
66#include <pthread.h>
67#include <linux/soundcard.h>
68
69#include <string.h>
70#include <fcntl.h>
71#include <stdlib.h>
72#include <stdio.h>
73
74#include <OMX_Index.h>
75#include <OMX_Types.h>
76#include <OMX_Core.h>
77#include <OMX_Audio.h>
78#include <OMX_Component.h>
79#include <TIDspOmx.h>
80
81
82#ifdef OMX_GETTIME
83#include <OMX_Common_Utils.h>
84#include <OMX_GetTime.h>     /*Headers for Performance & measuremet    */
85#endif
86
87FILE *fpRes = NULL;
88
89/* ======================================================================= */
90/**
91 * @def G729ENC_INPUT_BUFFER_SIZE        Default input buffer size
92 *              G729ENC_INPUT_BUFFER_SIZE_DASF  Default input buffer size DASF
93 */
94/* ======================================================================= */
95#define G729APP_INPUT_BUFFER_SIZE 160
96#define G729APP_INPUT_BUFFER_SIZE_DASF 160
97/* ======================================================================= */
98/**
99 * @def G729ENC_OUTPUT_BUFFER_SIZE   Default output buffer size
100 */
101/* ======================================================================= */
102#define G729APP_OUTPUT_BUFFER_SIZE 66
103
104/* ======================================================================= */
105/*
106 * @def G729ENC_APP_ID  App ID Value setting
107 */
108/* ======================================================================= */
109#define G729ENC_APP_ID 100
110#define SLEEP_TIME 5
111#define G729ENC_HEADER_LEN 2
112#define G729_NO_TX_FRAME_TYPE   0                /* No Data: 0 bytes */
113#define G729_SPEECH_FRAME_TYPE  1                /* Speech: 10 bytes */
114#define G729_SID_FRAME_TYPE             2                /* SID: 2 bytes */
115#define G729_ERASURE_FRAME_TYPE 3                /* Erasure frame */
116#define BIT_0 0x007f
117#define BIT_1 0x0081
118#define FIFO1 "/dev/fifo.1"
119#define FIFO2 "/dev/fifo.2"
120#undef APP_DEBUG
121#undef APP_MEMCHECK
122#undef USE_BUFFER
123#define STRESS_TEST_ITERATIONS 20
124
125#ifdef APP_DEBUG
126#define APP_DPRINT(...)    fprintf(stderr,__VA_ARGS__)
127#else
128#define APP_DPRINT(...)
129#endif
130
131#ifdef APP_MEMCHECK
132#define APP_MEMPRINT(...)    fprintf(stderr,__VA_ARGS__)
133#else
134#define APP_MEMPRINT(...)
135#endif
136
137#ifdef OMX_GETTIME
138OMX_ERRORTYPE eError = OMX_ErrorNone;
139int GT_FlagE = 0;  /* Fill Buffer 1 = First Buffer,  0 = Not First Buffer  */
140int GT_FlagF = 0;  /*Empty Buffer  1 = First Buffer,  0 = Not First Buffer  */
141static OMX_NODE* pListHead = NULL;
142#endif
143
144
145typedef struct AUDIO_INFO {
146    OMX_U32 efrMode;
147    OMX_U32 g729Mode;
148    OMX_U32 acdnMode;
149    OMX_U32 dasfMode;
150    OMX_U32 mimeMode;
151    OMX_U32 nIpBufs;
152    OMX_U32 nOpBufs;
153    OMX_U32 nMFrameMode;
154} AUDIO_INFO;
155
156/* ======================================================================= */
157/**
158 *  M A C R O S FOR MALLOC and MEMORY FREE and CLOSING PIPES
159 */
160/* ======================================================================= */
161
162#define OMX_G729APP_CONF_INIT_STRUCT(_s_, _name_)   \
163    memset((_s_), 0x0, sizeof(_name_));             \
164    (_s_)->nSize = sizeof(_name_);                  \
165    (_s_)->nVersion.s.nVersionMajor = 0x1;          \
166    (_s_)->nVersion.s.nVersionMinor = 0x1;          \
167    (_s_)->nVersion.s.nRevision = 0x0;              \
168    (_s_)->nVersion.s.nStep = 0x0
169
170#define OMX_G729APP_INIT_STRUCT(_s_, _name_)    \
171    memset((_s_), 0x0, sizeof(_name_)); \
172
173#define OMX_G729APP_MALLOC_STRUCT(_pStruct_, _sName_)                   \
174    _pStruct_ = (_sName_*)malloc(sizeof(_sName_));                      \
175    if(_pStruct_ == NULL){                                              \
176        printf("***********************************\n");                \
177        printf("%d :: Malloc Failed\n",__LINE__);                       \
178        printf("***********************************\n");                \
179        eError = OMX_ErrorInsufficientResources;                        \
180        goto EXIT;                                                      \
181    }                                                                   \
182    APP_MEMPRINT("%d :: ALLOCATING MEMORY = %p\n",__LINE__,_pStruct_);
183
184/* ======================================================================= */
185/** G729APP_COMP_PORT_TYPE  Port types
186 *
187 *  @param  G729APP_INPUT_PORT                  Input port
188 *
189 *  @param  G729APP_OUTPUT_PORT                 Output port
190 */
191/*  ====================================================================== */
192/*This enum must not be changed. */
193typedef enum G729APP_COMP_PORT_TYPE {
194    G729APP_INPUT_PORT = 0,
195    G729APP_OUTPUT_PORT
196}G729APP_COMP_PORT_TYPE;
197
198/* ======================================================================= */
199/**
200 * @def G729APP_MAX_NUM_OF_BUFS         Maximum number of buffers
201 * @def G729APP_NUM_OF_CHANNELS                 Number of Channels
202 * @def G729APP_SAMPLING_FREQUENCY    Sampling frequency
203 */
204/* ======================================================================= */
205#define G729APP_MAX_NUM_OF_BUFS 10
206#define G729APP_NUM_OF_CHANNELS 1
207#define G729APP_SAMPLING_FREQUENCY 8000
208
209int maxint(int a, int b);
210
211int inputPortDisabled = 0;
212int outputPortDisabled = 0;
213OMX_STRING strG729Encoder = "OMX.TI.G729.encode";
214int IpBuf_Pipe[2] = {0};
215int OpBuf_Pipe[2] = {0};
216fd_set rfds;
217int done = 0;
218int doneWriting = 0;
219int tcID = 0;
220OMX_BOOL bExitOnError = OMX_TRUE;
221int ebd = 0;
222int fbd = 0;
223
224
225void writeITUFormat(OMX_U8* buffer, OMX_U32 length, FILE* fOut) ;
226
227#ifdef USE_BUFFER
228OMX_ERRORTYPE FreeResources(OMX_AUDIO_PARAM_G729TYPE* pG729Param,
229                            OMX_PARAM_PORTDEFINITIONTYPE* pCompPrivateStruct,
230                            AUDIO_INFO* audioinfo,
231                            OMX_U8* pInputBuffer[G729APP_MAX_NUM_OF_BUFS],
232                            OMX_U8* pOutputBuffer[G729APP_MAX_NUM_OF_BUFS]);
233#else
234OMX_ERRORTYPE FreeResources(OMX_AUDIO_PARAM_G729TYPE* pG729Param,
235                            OMX_PARAM_PORTDEFINITIONTYPE* pCompPrivateStruct,
236                            AUDIO_INFO* audioinfo,
237                            OMX_BUFFERHEADERTYPE* pInputBufferHeader[G729APP_MAX_NUM_OF_BUFS],
238                            OMX_BUFFERHEADERTYPE* pOutputBufferHeader[G729APP_MAX_NUM_OF_BUFS],
239                            OMX_HANDLETYPE pHandle);
240#endif
241
242/* safe routine to get the maximum of 2 integers */
243int maxint(int a, int b)
244{
245    return (a>b) ? a : b;
246}
247
248/* This method will wait for the component to get to the state
249 * specified by the DesiredState input. */
250static OMX_ERRORTYPE WaitForState(OMX_HANDLETYPE* pHandle,
251                                  OMX_STATETYPE DesiredState)
252{
253    OMX_STATETYPE CurState = OMX_StateInvalid;
254    OMX_ERRORTYPE eError = OMX_ErrorNone;
255    int nCnt = 0;
256    OMX_COMPONENTTYPE *pComponent = (OMX_COMPONENTTYPE *)pHandle;
257
258    eError = pComponent->GetState(pHandle, &CurState);
259    if(eError != OMX_ErrorNone) {
260        APP_DPRINT("%d :: App: Error returned from GetState\n",__LINE__);
261        goto EXIT;
262    }
263    while( (eError == OMX_ErrorNone) && (CurState != DesiredState) ) {
264        sleep(2);
265        if(nCnt++ == 10) {
266            APP_DPRINT("%d :: Still Waiting, press CTL-C to continue\n",__LINE__);
267        }
268        eError = pComponent->GetState(pHandle, &CurState);
269        if(eError != OMX_ErrorNone) {
270            APP_DPRINT("%d :: App: Error returned from GetState\n",__LINE__);
271            goto EXIT;
272        }
273    }
274 EXIT:
275    return eError;
276}
277
278OMX_ERRORTYPE EventHandler(
279                           OMX_HANDLETYPE hComponent,
280                           OMX_PTR pAppData,
281                           OMX_EVENTTYPE eEvent,
282                           OMX_U32 nData1,
283                           OMX_U32 nData2,
284                           OMX_PTR pEventData)
285{
286    APP_DPRINT( "%d :: App: Entering EventHandler \n", __LINE__);
287    OMX_ERRORTYPE eError = OMX_ErrorNone;
288    OMX_COMPONENTTYPE *pComponent = (OMX_COMPONENTTYPE *)hComponent;
289    OMX_STATETYPE state = OMX_StateInvalid;
290
291    eError = pComponent->GetState (hComponent, &state);
292    if(eError != OMX_ErrorNone) {
293        APP_DPRINT("%d :: App: Error returned from GetState\n",__LINE__);
294        goto EXIT;
295    }
296    APP_DPRINT( "%d :: App: Component eEvent = %d\n", __LINE__,eEvent);
297    switch (eEvent) {
298        APP_DPRINT( "%d :: App: Component State Changed To %d\n", __LINE__,state);
299    case OMX_EventCmdComplete:
300        APP_DPRINT( "%d :: App: Component State Changed To %d\n", __LINE__,state);
301        if (nData1 == OMX_CommandPortDisable) {
302            if (nData2 == G729APP_INPUT_PORT) {
303                inputPortDisabled = 1;
304            }
305            if (nData2 == G729APP_OUTPUT_PORT) {
306                outputPortDisabled = 1;
307            }
308        }
309        break;
310    case OMX_EventError:
311        break;
312    case OMX_EventMax:
313        APP_DPRINT( "%d :: App: Component OMX_EventMax = %d\n", __LINE__,eEvent);
314        break;
315    case OMX_EventMark:
316        APP_DPRINT( "%d :: App: Component OMX_EventMark = %d\n", __LINE__,eEvent);
317        break;
318    case OMX_EventPortSettingsChanged:
319        APP_DPRINT( "%d :: App: Component OMX_EventPortSettingsChanged = %d\n", __LINE__,eEvent);
320        break;
321    case OMX_EventBufferFlag:
322        APP_DPRINT( "%d :: App: Component OMX_EventBufferFlag = %d\n", __LINE__,eEvent);
323        break;
324    case OMX_EventResourcesAcquired:
325        APP_DPRINT( "%d :: App: Component OMX_EventResourcesAcquired = %d\n", __LINE__,eEvent);
326        break;
327    default:
328        break;
329    }
330 EXIT:
331    APP_DPRINT( "%d :: App: Exiting EventHandler \n", __LINE__);
332    return eError;
333}
334
335void FillBufferDone (OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
336{
337    write(OpBuf_Pipe[1], &pBuffer, sizeof(pBuffer));
338    fbd++;
339#ifdef OMX_GETTIME
340    if (GT_FlagF == 1 ) /* First Buffer Reply*/  /* 1 = First Buffer,  0 = Not First Buffer  */
341    {
342        GT_END("Call to FillBufferDone  <First: FillBufferDone>");
343        GT_FlagF = 0 ;   /* 1 = First Buffer,  0 = Not First Buffer  */
344    }
345#endif
346}
347
348void EmptyBufferDone(OMX_HANDLETYPE hComponent, OMX_PTR ptr, OMX_BUFFERHEADERTYPE* pBuffer)
349{
350    write(IpBuf_Pipe[1], &pBuffer, sizeof(pBuffer));
351    ebd++;
352#ifdef OMX_GETTIME
353    if (GT_FlagE == 1 ) /* First Buffer Reply*/  /* 1 = First Buffer,  0 = Not First Buffer  */
354    {
355        GT_END("Call to EmptyBufferDone <First: EmptyBufferDone>");
356        GT_FlagE = 0;   /* 1 = First Buffer,  0 = Not First Buffer  */
357    }
358#endif
359}
360
361int main(int argc, char* argv[])
362{
363    OMX_CALLBACKTYPE G729CaBa = {(void *)EventHandler,
364                                 (void*)EmptyBufferDone,
365                                 (void*)FillBufferDone};
366    OMX_HANDLETYPE pHandle;
367    OMX_ERRORTYPE eError = OMX_ErrorNone;
368    OMX_U32 AppData = G729ENC_APP_ID;
369    OMX_PARAM_PORTDEFINITIONTYPE* pCompPrivateStruct = NULL;
370    OMX_AUDIO_PARAM_G729TYPE *pG729Param = NULL;
371    OMX_COMPONENTTYPE *pComponent = NULL;
372    OMX_STATETYPE state = OMX_StateInvalid;
373    OMX_BUFFERHEADERTYPE* pInputBufferHeader[G729APP_MAX_NUM_OF_BUFS] = {NULL};
374    OMX_BUFFERHEADERTYPE* pOutputBufferHeader[G729APP_MAX_NUM_OF_BUFS] = {NULL};
375#ifdef USE_BUFFER
376    OMX_U8* pInputBuffer[G729APP_MAX_NUM_OF_BUFS] = {NULL};
377    OMX_U8* pOutputBuffer[G729APP_MAX_NUM_OF_BUFS] = {NULL};
378#endif
379    AUDIO_INFO* audioinfo = NULL;
380    TI_OMX_DSP_DEFINITION tiOmxDspDefinition;
381    FILE* fIn = NULL;
382    FILE* fOut = NULL;
383    struct timeval tv;
384    int retval = 0, i = 0, j = 0, k = 0, kk = 0;
385    int frmCount = 0;
386    int frmCnt = 1;
387    int testcnt = 0;
388    int testcnt1 = 0;
389    int status = 0;
390    int fdmax = 0;
391    int nRead = 0;
392    int nFrameCount = 1;
393    int nFrameLen = 0;
394    int nIpBuff = 1;
395    int nOutBuff = 1;
396    OMX_INDEXTYPE index = 0;
397    int frameLengthInBytes = 0;
398    int frameType = 0;
399    int framesPerBuffer=1;
400    TI_OMX_DATAPATH dataPath;
401    int g729encfdwrite = 0;
402    int g729encfdread = 0;
403
404    printf("------------------------------------------------------\n");
405    printf("This is Main Thread In G729 ENCODER Test Application:\n");
406    printf("Test Core 1.5 - " __DATE__ ":" __TIME__ "\n");
407    printf("------------------------------------------------------\n");
408
409#ifdef OMX_GETTIME
410    GTeError = OMX_ListCreate(&pListHead);
411    printf("eError = %d\n",GTeError);
412    GT_START();
413#endif
414
415    bExitOnError = OMX_FALSE;
416
417#ifdef DSP_RENDERING_ON
418    if((g729encfdwrite=open(FIFO1,O_WRONLY))<0) {
419        printf("[G729TEST] - failure to open WRITE pipe\n");
420    }
421    else {
422        printf("[G729TEST] - opened WRITE pipe\n");
423    }
424
425    if((g729encfdread=open(FIFO2,O_RDONLY))<0) {
426        printf("[G729TEST] - failure to open READ pipe\n");
427        goto EXIT;
428    }
429    else {
430        printf("[G729TEST] - opened READ pipe\n");
431    }
432#endif
433
434    /* check the input parameters */
435    if(argc != 11) {
436        printf("%d :: Usage: [TestApp] [O/P] [FUNC_ID_X] [FM/DM]   [DTXON/OFF] [ACDNON/OFF] [FRAMES] [1 to N] [1 to N] [MFON]\n",__LINE__);
437        goto EXIT;
438    }
439    /* check to see that the input file exists */
440    struct stat sb = {0};
441    status = stat(argv[1], &sb);
442    if( status != 0 ) {
443        APP_DPRINT("Cannot find file %s. (%u)\n", argv[1], errno);
444        goto EXIT;
445    }
446    /* Open the file of data to be encoded. */
447    fIn = fopen(argv[1], "r");
448    if( fIn == NULL ) {
449
450        APP_DPRINT("Error:  failed to open the input file %s\n", argv[1]);
451        goto EXIT;
452    }
453    /* Open the file of data to be written. */
454    fOut = fopen(argv[2], "w");
455    if( fOut == NULL ) {
456
457        APP_DPRINT("Error:  failed to open the output file %s\n", argv[2]);
458        goto EXIT;
459    }
460
461    if(!strcmp(argv[3],"FUNC_ID_0")) {
462
463        printf("%d :: ### Testing TESTCASE 1 PLAY TILL END ###\n",__LINE__);
464        testcnt = 1;
465        testcnt1 = 1;
466        tcID = 0;
467    }
468    else if(!strcmp(argv[3],"FUNC_ID_1")) {
469
470        printf("%d :: ### Testing TESTCASE 1 PLAY TILL END ###\n",__LINE__);
471        testcnt = 1;
472        testcnt1 = 1;
473        tcID = 1;
474    }
475    else if(!strcmp(argv[3],"FUNC_ID_2")) {
476        printf("%d :: ### Testing TESTCASE 2 STOP IN THE END ###\n",__LINE__);
477        testcnt = 1;
478        testcnt1 = 1;
479        tcID = 2;
480    }
481    else if(!strcmp(argv[3],"FUNC_ID_3")) {
482        printf("%d :: ### Testing TESTCASE 3 PAUSE - RESUME IN BETWEEN ###\n",__LINE__);
483        testcnt = 1;
484        testcnt1 = 1;
485        tcID = 3;
486    }
487    else if(!strcmp(argv[3],"FUNC_ID_4")) {
488        printf("%d :: ### Testing TESTCASE 4 STOP IN BETWEEN ###\n",__LINE__);
489        testcnt = 2;
490        testcnt1 = 1;
491        tcID = 4;
492        printf("######## testcnt = %d #########\n",testcnt);
493    }
494    if(!strcmp(argv[3],"FUNC_ID_5")){
495        printf("%d :: ### Testing TESTCASE 5 ENCODE without Deleting component Here ###\n",__LINE__);
496        testcnt = STRESS_TEST_ITERATIONS;
497        testcnt1 = 1;
498        tcID = 5;
499    }
500    if(!strcmp(argv[3],"FUNC_ID_6")) {
501        printf("%d :: ### Testing TESTCASE 6 ENCODE with Deleting component Here ###\n",__LINE__);
502        testcnt = 1;
503        testcnt1 = STRESS_TEST_ITERATIONS;
504        tcID = 6;
505    }
506    eError = TIOMX_Init();
507    if(eError != OMX_ErrorNone) {
508        APP_DPRINT("%d :: Error returned by OMX_Init()\n",__LINE__);
509        goto EXIT;
510    }
511
512    for(j = 0; j < testcnt1; j++) {
513
514
515        /* Create a pipe used to queue data from the callback. */
516        retval = pipe(IpBuf_Pipe);
517        if( retval != 0) {
518            APP_DPRINT("Error:Fill Data Pipe failed to open\n");
519            goto EXIT;
520        }
521        retval = pipe(OpBuf_Pipe);
522        if( retval != 0) {
523            APP_DPRINT("Error:Empty Data Pipe failed to open\n");
524            goto EXIT;
525        }
526        /* save off the "max" of the handles for the selct statement */
527        fdmax = maxint(IpBuf_Pipe[0], OpBuf_Pipe[0]);
528
529        if(j > 0) {
530            printf ("%d :: Encoding the file for %d Time in TESTCASE 6\n",__LINE__,j+1);
531            fIn = fopen(argv[1], "r");
532            if( fIn == NULL ) {
533                fprintf(stderr, "Error:  failed to open the file %s for read only access\n",argv[1]);
534                goto EXIT;
535            }
536            fOut = fopen("TC6_G7291.g729", "w");
537            if( fOut == NULL ) {
538                fprintf(stderr, "Error:  failed to create the output file %s\n",argv[2]);
539                goto EXIT;
540            }
541        }
542        /* Load the G729 Encoder Component */
543#ifdef OMX_GETTIME
544        GT_START();
545        eError = OMX_GetHandle(&pHandle, strG729Encoder, &AppData, &G729CaBa);
546        GT_END("Call to GetHandle");
547#else
548        eError = TIOMX_GetHandle(&pHandle, strG729Encoder, &AppData, &G729CaBa);
549#endif
550        if((eError != OMX_ErrorNone) || (pHandle == NULL)) {
551            APP_DPRINT("Error in Get Handle function\n");
552            goto EXIT;
553        }
554        OMX_G729APP_MALLOC_STRUCT(audioinfo, AUDIO_INFO);
555        OMX_G729APP_INIT_STRUCT(audioinfo, AUDIO_INFO);
556        /* Setting No.Of Input and Output Buffers for the Component */
557        if(atoi(argv[8]) <1 || atoi(argv[8]) > 4)
558        {
559            printf("Please enter a valid number of buffers between 1 and 4\n");
560            bExitOnError = 1;
561            goto EXIT;
562        }
563        else
564        {
565            audioinfo->nIpBufs = atoi(argv[8]);
566            APP_DPRINT("\n%d :: App: audioinfo->nIpBufs = %ld \n",__LINE__,audioinfo->nIpBufs);
567        }
568
569        if(atoi(argv[9]) <1 || atoi(argv[9]) > 4)
570        {
571            printf("Please enter a valid number of buffers between 1 and 4\n");
572            bExitOnError = 1;
573            goto EXIT;
574        }
575        else
576        {
577            audioinfo->nOpBufs = atoi(argv[9]);
578            APP_DPRINT("\n%d :: App: audioinfo->nOpBufs = %ld \n",__LINE__,audioinfo->nOpBufs);
579        }
580        if(atoi(argv[10]) <1 || atoi(argv[10]) > 8)
581        {
582            printf("Please enter a valid number of frames per buffer between 1 and 8\n");
583            bExitOnError = 1;
584            goto EXIT;
585        }
586        else
587        {
588            framesPerBuffer = atoi(argv[10]);
589        }
590        OMX_G729APP_MALLOC_STRUCT(pCompPrivateStruct, OMX_PARAM_PORTDEFINITIONTYPE);
591        OMX_G729APP_CONF_INIT_STRUCT(pCompPrivateStruct, OMX_PARAM_PORTDEFINITIONTYPE);
592        OMX_G729APP_MALLOC_STRUCT(pG729Param, OMX_AUDIO_PARAM_G729TYPE);
593        OMX_G729APP_CONF_INIT_STRUCT(pG729Param, OMX_AUDIO_PARAM_G729TYPE);
594        APP_DPRINT("%d :: Setting input port config\n",__LINE__);
595        pCompPrivateStruct->nSize                                                          = sizeof (OMX_PARAM_PORTDEFINITIONTYPE);
596        pCompPrivateStruct->nVersion.s.nVersionMajor           = 0xF1;
597        pCompPrivateStruct->nVersion.s.nVersionMinor               = 0xF2;
598        pCompPrivateStruct->nPortIndex                         = G729APP_INPUT_PORT;
599        pCompPrivateStruct->eDir                               = OMX_DirInput;
600        pCompPrivateStruct->nBufferCountActual                 = audioinfo->nIpBufs;
601        pCompPrivateStruct->nBufferCountMin                    = audioinfo->nIpBufs;
602        pCompPrivateStruct->nBufferSize                        = G729APP_INPUT_BUFFER_SIZE*framesPerBuffer;
603        pCompPrivateStruct->bEnabled                           = OMX_TRUE;
604        pCompPrivateStruct->bPopulated                         = OMX_FALSE;
605        pCompPrivateStruct->eDomain                            = OMX_PortDomainAudio;
606        pCompPrivateStruct->format.audio.eEncoding             = OMX_AUDIO_CodingG729;
607        pCompPrivateStruct->format.audio.cMIMEType             = NULL;
608        pCompPrivateStruct->format.audio.pNativeRender         = NULL;
609        pCompPrivateStruct->format.audio.bFlagErrorConcealment = OMX_FALSE;    /*Send input port config*/
610        APP_DPRINT("%d :: Setting input port config\n",__LINE__);
611        if(!(strcmp(argv[4],"FM"))) {
612            audioinfo->dasfMode = 0;
613            tiOmxDspDefinition.dasfMode = OMX_FALSE;
614            APP_DPRINT("\n%d :: App: audioinfo->dasfMode = %ld \n",__LINE__,audioinfo->dasfMode);
615        }
616        else if(!(strcmp(argv[4],"DM"))){
617            audioinfo->dasfMode = 1;
618            tiOmxDspDefinition.dasfMode = OMX_TRUE;
619            APP_DPRINT("\n%d :: App: audioinfo->dasfMode = %ld \n",__LINE__,audioinfo->dasfMode);
620            APP_DPRINT("%d :: G729 ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
621            pCompPrivateStruct->nBufferCountActual = 0;
622        }
623        else {
624            eError = OMX_ErrorBadParameter;
625            printf("\n%d :: App: audioinfo->dasfMode Sending Bad Parameter\n",__LINE__);
626            printf("%d :: App: Should Be One of these Modes FM, DM\n",__LINE__);
627            bExitOnError = OMX_TRUE;
628            goto EXIT;
629        }
630        if(audioinfo->dasfMode == 0) {
631            if((atoi(argv[7])) != 0) {
632                eError = OMX_ErrorBadParameter;
633                printf("\n%d :: App: No. of Frames Sending Bad Parameter\n",__LINE__);
634                printf("%d :: App: For FILE mode argv[7] Should Be --> 0\n",__LINE__);
635                printf("%d :: App: For DASF mode argv[7] Should be greater than zero depends on number of frames user want to encode\n",__LINE__);
636                bExitOnError = OMX_TRUE;
637                goto EXIT;
638            }
639        }
640        else {
641            if((atoi(argv[7])) == 0) {
642                eError = OMX_ErrorBadParameter;
643                printf("\n%d :: App: No. of Frames Sending Bad Parameter\n",__LINE__);
644                printf("%d :: App: For DASF mode argv[7] Should be greater than zero depends on number of frames user want to encode\n",__LINE__);
645                printf("%d :: App: For FILE mode argv[7] Should Be --> 0\n",__LINE__);
646                bExitOnError = OMX_TRUE;
647                goto EXIT;
648            }
649        }
650        if(!(strcmp(argv[6],"ACDNOFF"))) {
651            audioinfo->acdnMode = 0;
652            tiOmxDspDefinition.acousticMode = OMX_FALSE;
653            APP_DPRINT("\n%d :: App: audioinfo->acdnMode = %ld \n",__LINE__,audioinfo->acdnMode);
654        }
655        else if(!(strcmp(argv[6],"ACDNON"))) {
656            audioinfo->acdnMode = 1;
657            tiOmxDspDefinition.acousticMode = OMX_TRUE;
658            APP_DPRINT("\n%d :: App: audioinfo->acdnMode = %ld \n",__LINE__,audioinfo->acdnMode);
659        }
660        else {
661            eError = OMX_ErrorBadParameter;
662            printf("\n%d :: App: audioinfo->acdnMode Sending Bad Parameter\n",__LINE__);
663            printf("%d :: App: Should Be One of these Modes ACDNON, ACDNOFF\n",__LINE__);
664            bExitOnError = OMX_TRUE;
665            goto EXIT;
666        }
667#ifdef OMX_GETTIME
668        GT_START();
669        eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
670        GT_END("Set Parameter Test-SetParameter");
671#else
672        eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
673#endif
674        if (eError != OMX_ErrorNone) {
675            eError = OMX_ErrorBadParameter;
676            APP_DPRINT("%d :: OMX_ErrorBadParameter\n",__LINE__);
677            bExitOnError = OMX_TRUE;
678            goto EXIT;
679        }
680        APP_MEMPRINT("%d :: Setting output port config\n",__LINE__);
681        pCompPrivateStruct->nSize                                                          = sizeof (OMX_PARAM_PORTDEFINITIONTYPE);
682        pCompPrivateStruct->nVersion.s.nVersionMajor           = 0x1;
683        pCompPrivateStruct->nVersion.s.nVersionMinor               = 0x1;
684        pCompPrivateStruct->nPortIndex                         = G729APP_OUTPUT_PORT;
685        pCompPrivateStruct->eDir                               = OMX_DirOutput;
686        pCompPrivateStruct->nBufferCountActual                 = audioinfo->nOpBufs;
687        pCompPrivateStruct->nBufferCountMin                    = audioinfo->nOpBufs;
688        pCompPrivateStruct->nBufferSize                        = G729APP_OUTPUT_BUFFER_SIZE;
689        pCompPrivateStruct->bEnabled                           = OMX_TRUE;
690        pCompPrivateStruct->bPopulated                         = OMX_FALSE;
691        pCompPrivateStruct->eDomain                            = OMX_PortDomainAudio;
692        pCompPrivateStruct->format.audio.eEncoding             = OMX_AUDIO_CodingG729;
693        pCompPrivateStruct->format.audio.cMIMEType             = NULL;
694        pCompPrivateStruct->format.audio.pNativeRender         = NULL;
695        pCompPrivateStruct->format.audio.bFlagErrorConcealment = OMX_FALSE;    /*Send input port config*/
696#ifdef OMX_GETTIME
697        GT_START();
698        eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
699        GT_END("Set Parameter Test-SetParameter");
700#else
701        eError = OMX_SetParameter (pHandle, OMX_IndexParamPortDefinition, pCompPrivateStruct);
702#endif
703        if (eError != OMX_ErrorNone) {
704            eError = OMX_ErrorBadParameter;
705            APP_DPRINT("%d :: OMX_ErrorBadParameter\n",__LINE__);
706            bExitOnError = OMX_TRUE;
707            goto EXIT;
708        }
709        pG729Param->nSize                    = sizeof(OMX_AUDIO_PARAM_G729TYPE);
710        pG729Param->nVersion.s.nVersionMajor = 0x1;
711        pG729Param->nVersion.s.nVersionMinor = 0x1;
712        pG729Param->nPortIndex               = G729APP_OUTPUT_PORT;
713        pG729Param->nChannels                = G729APP_NUM_OF_CHANNELS;
714        /*        APP_DPRINT("\n%d :: App: pG729Param->eG729BandMode --> %d \n",__LINE__,pG729Param->eG729BandMode); */
715        if(!(strcmp(argv[5],"DTXON"))) {
716            /**< G729 Discontinuous Transmission Mode is enabled  */
717            /*            APP_DPRINT("\n%d :: App: pG729Param->eARMDTXMode --> %s \n",__LINE__,argv[5]); */
718            pG729Param->bDTX = OMX_TRUE;
719        }
720        else if(!(strcmp(argv[5],"DTXOFF"))) {
721            pG729Param->bDTX = OMX_FALSE;
722            /**< G729 Discontinuous Transmission Mode is disabled */
723        }
724        else {
725            eError = OMX_ErrorBadParameter;
726            printf("\n%d :: App: pG729Param->eARMDTXMode Sending Bad Parameter\n",__LINE__);
727            printf("%d :: App: Should Be One of these Modes DTXON, DTXOFF\n",__LINE__);
728            bExitOnError = OMX_TRUE;
729            goto EXIT;
730        }
731#ifdef OMX_GETTIME
732        GT_START();
733        eError = OMX_SetParameter (pHandle, OMX_IndexParamAudioG729, pG729Param);
734        GT_END("Set Parameter Test-SetParameter");
735#else
736        eError = OMX_SetParameter (pHandle, OMX_IndexParamAudioG729, pG729Param);
737#endif
738        if (eError != OMX_ErrorNone) {
739            eError = OMX_ErrorBadParameter;
740            APP_DPRINT("%d :: OMX_ErrorBadParameter\n",__LINE__);
741            bExitOnError = OMX_TRUE;
742            goto EXIT;
743        }
744
745#ifndef USE_BUFFER
746        APP_DPRINT("%d :: About to call OMX_AllocateBuffer\n",__LINE__);
747        for(i = 0; i < audioinfo->nIpBufs; i++) {
748            /* allocate input buffer */
749            APP_DPRINT("%d :: About to call OMX_AllocateBuffer for pInputBufferHeader[%d]\n",__LINE__, i);
750            eError = OMX_AllocateBuffer(pHandle, &pInputBufferHeader[i], 0, NULL, G729APP_INPUT_BUFFER_SIZE*framesPerBuffer);
751            if(eError != OMX_ErrorNone) {
752                APP_DPRINT("%d :: Error returned by OMX_AllocateBuffer for pInputBufferHeader[%d]\n",__LINE__, i);
753                bExitOnError = OMX_TRUE;
754                goto EXIT;
755            }
756        }
757        APP_DPRINT("\n%d :: App: pCompPrivateStruct->nBufferSize --> %ld \n",__LINE__,pCompPrivateStruct->nBufferSize);
758        for(i = 0; i < audioinfo->nOpBufs; i++) {
759            /* allocate output buffer */
760            APP_DPRINT("%d :: About to call OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
761            eError = OMX_AllocateBuffer(pHandle, &pOutputBufferHeader[i], 1, NULL, pCompPrivateStruct->nBufferSize);
762            if(eError != OMX_ErrorNone) {
763                APP_DPRINT("%d :: Error returned by OMX_AllocateBuffer for pOutputBufferHeader[%d]\n",__LINE__, i);
764                bExitOnError = OMX_TRUE;
765                goto EXIT;
766            }
767        }
768#else
769        for(i = 0; i < audioinfo->nIpBufs; i++) {
770            pInputBuffer[i] = (OMX_U8*)malloc(G729APP_INPUT_BUFFER_SIZE*framesPerBuffer);
771            pInputBuffer[i] += 128;
772
773            APP_MEMPRINT("%d :: [TESTAPP ALLOC] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
774            if(NULL == pInputBuffer[i]) {
775                APP_DPRINT("%d :: Malloc Failed\n",__LINE__);
776                eError = OMX_ErrorInsufficientResources;
777                goto EXIT;
778            }
779            /*  allocate input buffer */
780            APP_DPRINT("%d :: About to call OMX_UseBuffer\n",__LINE__);
781            eError = OMX_UseBuffer(pHandle, &pInputBufferHeader[i], 0, NULL, G729APP_INPUT_BUFFER_SIZE*framesPerBuffer, pInputBuffer[i]);
782            if(eError != OMX_ErrorNone) {
783                APP_DPRINT("%d :: Error returned by OMX_UseBuffer()\n",__LINE__);
784                bExitOnError = OMX_TRUE;
785                goto EXIT;
786            }
787        }
788
789        for(i = 0; i < audioinfo->nOpBufs; i++) {
790            pOutputBuffer[i] = malloc (pCompPrivateStruct->nBufferSize + 256);
791            APP_MEMPRINT("%d :: [TESTAPP ALLOC] pOutputBuffer[%d] = %p\n",__LINE__,i,pOutputBuffer[i]);
792            if(NULL == pOutputBuffer[i]) {
793                APP_DPRINT("%d :: Malloc Failed\n",__LINE__);
794                eError = OMX_ErrorInsufficientResources;
795                bExitOnError = OMX_TRUE;
796                goto EXIT;
797            }
798            pOutputBuffer[i] = pOutputBuffer[i] + 128;
799
800            /* allocate output buffer */
801            APP_DPRINT("%d :: About to call OMX_UseBuffer\n",__LINE__);
802            eError = OMX_UseBuffer(pHandle, &pOutputBufferHeader[i], 1, NULL, pCompPrivateStruct->nBufferSize, pOutputBuffer[i]);
803            if(eError != OMX_ErrorNone) {
804                APP_DPRINT("%d :: Error returned by OMX_UseBuffer()\n",__LINE__);
805                bExitOnError = OMX_TRUE;
806                goto EXIT;
807            }
808        }
809#endif
810
811        eError = OMX_GetExtensionIndex(pHandle, "OMX.TI.index.config.tispecific",&index);
812        if (eError != OMX_ErrorNone) {
813            APP_DPRINT("Error returned from OMX_GetExtensionIndex\n");
814            bExitOnError = OMX_TRUE;
815            goto EXIT;
816        }
817
818#ifdef DSP_RENDERING_ON
819        cmd_data.hComponent = pHandle;
820        cmd_data.AM_Cmd = AM_CommandIsInputStreamAvailable;
821
822        cmd_data.param1 = 0;
823        if((write(g729encfdwrite, &cmd_data, sizeof(cmd_data)))<0) {
824        }
825        if((read(g729encfdread, &cmd_data, sizeof(cmd_data)))<0) {
826            bExitOnError = OMX_TRUE;
827            goto EXIT;
828        }
829        tiOmxDspDefinition.streamId = cmd_data.streamID;
830#endif
831
832        eError = OMX_SetConfig (pHandle, index, &tiOmxDspDefinition);
833        if(eError != OMX_ErrorNone) {
834            eError = OMX_ErrorBadParameter;
835            APP_DPRINT("%d :: Error from OMX_SetConfig() function\n",__LINE__);
836            bExitOnError = OMX_TRUE;
837            goto EXIT;
838        }
839
840        if (tiOmxDspDefinition.dasfMode) {
841#ifdef RTM_PATH
842            dataPath = DATAPATH_APPLICATION_RTMIXER;
843#endif
844
845#ifdef ETEEDN_PATH
846            dataPath = DATAPATH_APPLICATION;
847#endif
848        }
849
850        eError = OMX_GetExtensionIndex(pHandle, "OMX.TI.index.config.g729.datapath",&index);
851        if (eError != OMX_ErrorNone) {
852            printf("Error getting extension index\n");
853            bExitOnError = OMX_TRUE;
854            goto EXIT;
855        }
856
857        eError = OMX_SetConfig (pHandle, index, &dataPath);
858        if(eError != OMX_ErrorNone) {
859            eError = OMX_ErrorBadParameter;
860            bExitOnError = OMX_TRUE;
861            goto EXIT;
862        }
863#ifdef OMX_GETTIME
864        GT_START();
865#endif
866        eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
867        if(eError != OMX_ErrorNone) {
868            APP_DPRINT("Error from SendCommand-Idle(Init) State function\n");
869            bExitOnError = OMX_TRUE;
870            goto EXIT;
871        }
872        /* Wait for startup to complete */
873        eError = WaitForState(pHandle, OMX_StateIdle);
874#ifdef OMX_GETTIME
875        GT_END("Call to SendCommand <OMX_StateIdle>");
876#endif
877        if(eError != OMX_ErrorNone) {
878            APP_DPRINT( "Error:  hG729Encoder->WaitForState reports an eError %X\n", eError);
879            bExitOnError = OMX_TRUE;
880            goto EXIT;
881        }
882        for(i = 0; i < testcnt; i++) {
883            frmCnt = 1;
884            nFrameCount = 1;
885            nOutBuff = 1;
886            nIpBuff  = 1;
887            if(i > 0) {
888                printf("%d :: Encoding the file for %d Time in TESTCASE 5 OR TESTCSE 4\n",__LINE__,i+1);
889                fIn = fopen(argv[1], "r");
890                if(fIn == NULL) {
891                    fprintf(stderr, "Error:  failed to open the file %s for readonly access\n", argv[1]);
892                    bExitOnError = OMX_TRUE;
893                    goto EXIT;
894                }
895                fOut = fopen("TC5_G7291.g729", "w");
896                if(fOut == NULL) {
897                    fprintf(stderr, "Error:  failed to create the output file %s\n", argv[2]);
898                    bExitOnError = OMX_TRUE;
899                    goto EXIT;
900                }
901            }
902
903
904            done = 0;
905            printf("%d :: App: Sending OMX_StateExecuting Command\n",__LINE__);
906
907#ifdef OMX_GETTIME
908            GT_START();
909#endif
910
911            eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
912            if(eError != OMX_ErrorNone) {
913                printf("Error from SendCommand-Executing State function\n");
914                bExitOnError = OMX_TRUE;
915                goto EXIT;
916            }
917            eError = WaitForState(pHandle, OMX_StateExecuting);
918
919#ifdef OMX_GETTIME
920            GT_END("Call to SendCommand <OMX_StateExecuting>");
921#endif
922
923            if(eError != OMX_ErrorNone) {
924                APP_DPRINT( "Error:  hG729Encoder->WaitForState reports an eError %X\n", eError);
925                bExitOnError = OMX_TRUE;
926                goto EXIT;
927            }
928            if(audioinfo->dasfMode == 1) {
929                printf("%d :: App: No.of Frames Encoding = %d\n",__LINE__, atoi(argv[7]));
930            }
931            pComponent = (OMX_COMPONENTTYPE *)pHandle;
932            if(audioinfo->dasfMode == 0) {
933                for (k=0; k < audioinfo->nIpBufs; k++) {
934                    OMX_BUFFERHEADERTYPE* pBuffer = pInputBufferHeader[k];
935                    nRead = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
936                    APP_DPRINT("%d :: App :: Reading InputBuffer = %d from the input file nRead = %d\n",__LINE__,nIpBuff,nRead);
937                    pBuffer->nFilledLen = nRead;
938                    pBuffer->nTimeStamp = (OMX_S64)rand() % 70;
939                    pBuffer->nTickCount = rand() % 70;
940
941                    if((nRead == 0) && (done == 0)) {
942                        printf("%d :: Sending Last Input Buffer from TestApp.............. \n",__LINE__);
943                        done = 1;
944                        pBuffer->nFilledLen = 0;
945                        pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
946                    }
947                    APP_DPRINT("%d :: App:: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
948#ifdef OMX_GETTIME
949                    if (k==0)
950                    {
951                        GT_FlagE=1;  /* 1 = First Buffer,  0 = Not First Buffer  */
952                        GT_START(); /* Empty Bufffer */
953                    }
954#endif
955                    OMX_EmptyThisBuffer(pHandle, pBuffer);
956                    nIpBuff++;
957                }
958            }
959
960            for (kk = 0; kk < audioinfo->nOpBufs; kk++) {
961#ifdef OMX_GETTIME
962                if (kk==0)
963                {
964                    GT_FlagF=1;  /* 1 = First Buffer,  0 = Not First Buffer  */
965                    GT_START(); /* Fill Buffer */
966                }
967#endif
968                OMX_FillThisBuffer(pHandle, pOutputBufferHeader[kk]);
969
970            }
971            eError = OMX_GetState(pHandle, &state);
972            if(eError != OMX_ErrorNone) {
973                APP_DPRINT("%d :: pComponent->GetState has returned status %X\n",__LINE__, eError);
974                bExitOnError = OMX_TRUE;
975                goto EXIT;
976            }
977
978            while((eError == OMX_ErrorNone) && (state != OMX_StateIdle)) {
979                FD_ZERO(&rfds);
980                FD_SET(IpBuf_Pipe[0], &rfds);
981                FD_SET(OpBuf_Pipe[0], &rfds);
982                tv.tv_sec = 1;
983                tv.tv_usec = 0;
984                frmCount++;
985
986                retval = select(fdmax+1, &rfds, NULL, NULL, &tv);
987                if(retval == -1) {
988                    perror("select()");
989                    APP_DPRINT( " :: Error \n");
990                    break;
991                }
992
993                if(retval == 0) {
994                    sleep(1);
995                    APP_DPRINT("%d :: BasicFn App Timeout !!!!!!!!!!! \n",__LINE__);
996                }
997
998                switch (tcID) {
999                case 0:
1000                case 1:
1001                case 2:
1002                case 5:
1003                case 6:
1004                    if(audioinfo->dasfMode == 0) {
1005                        if(FD_ISSET(IpBuf_Pipe[0], &rfds)) {
1006                            OMX_BUFFERHEADERTYPE* pBuffer = NULL;
1007                            read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
1008                            if(done == 0) {
1009                                nRead = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
1010                                APP_DPRINT("%d :: App :: Reading InputBuffer = %d from the input file nRead = %d\n",__LINE__,nIpBuff,nRead);
1011                                pBuffer->nFilledLen = nRead;
1012                                pBuffer->nTimeStamp = (OMX_S64)rand() % 2;
1013                                pBuffer->nTickCount = (OMX_S64)rand() % 70;
1014                                if (tcID == 0){
1015                                    APP_DPRINT("Input time stamp = %ld\n", (long int)pBuffer->nTimeStamp);
1016                                    APP_DPRINT("Input tick count = %ld\n", pBuffer->nTickCount);
1017                                }
1018                                if((nRead < 160) && (done == 0)) {
1019                                    printf("%d :: App: Sending Last Input Buffer from TestApp.............. \n",__LINE__);
1020                                    done = 1;
1021                                    pBuffer->nFilledLen = 0;
1022                                    pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
1023                                }
1024                                APP_DPRINT("%d :: App: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
1025                                OMX_EmptyThisBuffer(pHandle, pBuffer);
1026                                nIpBuff++;
1027                            }
1028                        }
1029                    }
1030                    else {
1031                        APP_DPRINT("%d :: G729 ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
1032                        if(nFrameCount == atoi(argv[7])) {
1033                            printf("%d :: App: Sending Stop.........From APP \n",__LINE__);
1034                            printf("%d :: App: Shutting down ---------- \n",__LINE__);
1035#ifdef OMX_GETTIME
1036                            GT_START();
1037#endif
1038                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1039                            if(eError != OMX_ErrorNone) {
1040                                fprintf (stderr,"Error from SendCommand-Idle(Stop) State function\n");
1041                                bExitOnError = OMX_TRUE;
1042                                goto EXIT;
1043                            }
1044                            eError = WaitForState(pHandle, OMX_StateIdle);
1045#ifdef OMX_GETTIME
1046                            GT_END("Call to SendCommand <OMX_StateIdle>");
1047#endif
1048                            if(eError != OMX_ErrorNone) {
1049                                APP_DPRINT( "Error:  G729Encoder->WaitForState reports an error         %X\n", eError);
1050                                bExitOnError = OMX_TRUE;
1051                                goto EXIT;
1052                            }
1053                            done = 1;
1054                        }
1055                        APP_DPRINT("%d :: G729 ENCODER READING DATA FROM DASF  \n",__LINE__);
1056                    }
1057                    break;
1058
1059                case 3:
1060                    if(audioinfo->dasfMode == 0) {
1061                        APP_DPRINT("%d :: G729 ENCODER RUNNING UNDER FILE 2 FILE MODE \n",__LINE__);
1062                        if(FD_ISSET(IpBuf_Pipe[0], &rfds)) {
1063                            OMX_BUFFERHEADERTYPE* pBuffer = NULL;
1064                            read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
1065                            if(done == 0) {
1066                                APP_DPRINT("%d :: App: Read from IpBuf_Pipe InBufHeader %p\n",__LINE__,pBuffer);
1067                                nRead = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
1068                                APP_DPRINT("%d :: App: Reading I/P Buffer = %d & Size = %d from IpBuf_Pipe\n",__LINE__,nFrameCount,nRead);
1069                                pBuffer->nFilledLen = nRead;
1070                                if((nRead < pBuffer->nAllocLen) && (done == 0)) {
1071                                    printf("%d :: App: Shutting down ---------- \n",__LINE__);
1072                                    done = 1;
1073                                    pBuffer->nFilledLen = 0;
1074                                    pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
1075                                    APP_DPRINT("%d :: App: Sending Last Input Buffer from TestApp.............. \n",__LINE__);
1076                                    APP_DPRINT("%d :: App: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
1077                                    OMX_EmptyThisBuffer(pHandle, pBuffer);
1078#ifdef OMX_GETTIME
1079                                    GT_START();
1080#endif
1081                                    eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
1082                                    if(eError != OMX_ErrorNone) {
1083                                        printf("Error from SendCommand-Idle(Stop) State function\n");
1084                                        bExitOnError = OMX_TRUE;
1085                                        goto EXIT;
1086                                    }
1087                                    eError = WaitForState(pHandle, OMX_StateIdle);
1088#ifdef OMX_GETTIME
1089                                    GT_END("Call to SendCommand <OMX_StateIdle>");
1090#endif
1091                                    if ( eError != OMX_ErrorNone ){
1092                                        printf("Error:WaitForState has timed out %d", eError);
1093                                        bExitOnError = OMX_TRUE;
1094                                        goto EXIT;
1095                                    }
1096                                }
1097                                else {
1098                                    APP_DPRINT("%d :: App: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
1099                                    OMX_EmptyThisBuffer(pHandle, pBuffer);
1100                                }
1101                            }
1102                        }
1103                    }
1104                    else {
1105                        APP_DPRINT("%d :: G729 ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
1106                        if(nFrameCount == atoi(argv[7])) {
1107                            printf("%d :: App: Sending Stop.........From APP \n",__LINE__);
1108                            printf("%d :: App: Shutting down ---------- \n",__LINE__);
1109#ifdef OMX_GETTIME
1110                            GT_START();
1111#endif
1112                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1113                            if(eError != OMX_ErrorNone) {
1114                                fprintf (stderr,"Error from SendCommand-Idle(Stop) State function\n");
1115                                bExitOnError = OMX_TRUE;
1116                                goto EXIT;
1117                            }
1118                            eError = WaitForState(pHandle, OMX_StateIdle);
1119#ifdef OMX_GETTIME
1120                            GT_END("Call to SendCommand <OMX_StateIdle>");
1121#endif
1122                            if(eError != OMX_ErrorNone) {
1123                                APP_DPRINT( "Error:  G729Encoder->WaitForState reports an error         %X\n", eError);
1124                                bExitOnError = OMX_TRUE;
1125                                goto EXIT;
1126                            }
1127                            done = 1;
1128                        }
1129                        APP_DPRINT("%d :: G729 ENCODER READING DATA FROM DASF  \n",__LINE__);
1130                    }
1131                    if (frmCount == 15) {
1132                        printf ("%d :: App: $$$$$ Sending Resume command to Codec $$$$$$$\n",__LINE__);
1133#ifdef OMX_GETTIME
1134                        GT_START();
1135#endif
1136                        eError = OMX_SendCommand(pHandle, OMX_CommandStateSet,OMX_StateExecuting, NULL);
1137                        if(eError != OMX_ErrorNone) {
1138                            fprintf (stderr,"Error from SendCommand-Executing State function\n");
1139                            bExitOnError = OMX_TRUE;
1140                            goto EXIT;
1141                        }
1142
1143                        /* Wait for startup to complete */
1144                        eError = WaitForState(pHandle, OMX_StateExecuting);
1145#ifdef OMX_GETTIME
1146                        GT_END("Call to SendCommand <OMX_StateExecuting>");
1147#endif
1148                        if(eError != OMX_ErrorNone) {
1149                            fprintf(stderr, "Error:  hPcmDecoder->WaitForState reports an eError %X\n", eError);
1150                            bExitOnError = OMX_TRUE;
1151                            goto EXIT;
1152                        }
1153                    }
1154                    if(frmCount == 10) {
1155                        printf ("%d :: App: $$$$$ Sending Pause command to Codec $$$$$$\n",__LINE__);
1156#ifdef OMX_GETTIME
1157                        GT_START();
1158#endif
1159                        eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StatePause, NULL);
1160                        if(eError != OMX_ErrorNone) {
1161                            printf("Error from SendCommand-Pasue State function\n");
1162                            bExitOnError = OMX_TRUE;
1163                            goto EXIT;
1164                        }
1165                        /* Wait for startup to complete */
1166                        eError = WaitForState(pHandle, OMX_StatePause);
1167#ifdef OMX_GETTIME
1168                        GT_END("Call to SendCommand <OMX_StatePause>");
1169#endif
1170                        if(eError != OMX_ErrorNone) {
1171                            printf("hG729Encoder->WaitForState reports error\n");
1172                            bExitOnError = OMX_TRUE;
1173                            goto EXIT;
1174                        }
1175                    }
1176                    break;
1177                case 4:
1178                    if(audioinfo->dasfMode == 0) {
1179                        APP_DPRINT("%d :: G729 ENCODER RUNNING UNDER FILE 2 FILE MODE \n",__LINE__);
1180                        if( FD_ISSET(IpBuf_Pipe[0], &rfds) ) {
1181                            if(frmCnt > 20) {
1182                                OMX_BUFFERHEADERTYPE* pBuffer = NULL;
1183                                read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
1184                                printf("%d :: App: Shutting down ---------- \n",__LINE__);
1185                                done = 1;
1186                                pBuffer->nFilledLen = 0;
1187                                pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
1188                                APP_DPRINT("%d :: App:: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
1189                                OMX_EmptyThisBuffer(pHandle, pBuffer);
1190                            }
1191                            else {
1192                                OMX_BUFFERHEADERTYPE* pBuffer;
1193                                read(IpBuf_Pipe[0], &pBuffer, sizeof(pBuffer));
1194                                if(done == 0) {
1195                                    APP_DPRINT("%d :: App: Read from IpBuf_Pipe InBufHeader %p\n",__LINE__,pBuffer);
1196                                    nRead = fread(pBuffer->pBuffer, 1, pBuffer->nAllocLen, fIn);
1197                                    APP_DPRINT("%d :: App: Reading I/P Buffer = %d & Size = %d from IpBuf_Pipe\n",__LINE__,nFrameCount,nRead);
1198                                    pBuffer->nFilledLen = nRead;
1199                                    if((nRead == 0) && (done == 0)) {
1200                                        printf("%d :: App: Shutting down ---------- \n",__LINE__);
1201                                        done = 1;
1202                                        pBuffer->nFilledLen = 0;
1203                                        pBuffer->nFlags = OMX_BUFFERFLAG_EOS;
1204                                    }
1205                                    APP_DPRINT("%d :: App: Input Buffer: Calling EmptyThisBuffer: %p\n",__LINE__,pBuffer);
1206                                    OMX_EmptyThisBuffer(pHandle, pBuffer);
1207                                }
1208                            }
1209                        }
1210                        frmCnt++;
1211                    } else {
1212                        APP_DPRINT("%d :: G729 ENCODER RUNNING UNDER DASF MODE \n",__LINE__);
1213                        if(nFrameCount == atoi(argv[7])) {
1214                            printf("%d :: App: Shutting down ---------- \n",__LINE__);
1215#ifdef OMX_GETTIME
1216                            GT_START();
1217#endif
1218                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1219                            if(eError != OMX_ErrorNone) {
1220                                printf("Error from SendCommand-Idle(Stop) State function\n");
1221                                bExitOnError = OMX_TRUE;
1222                                goto EXIT;
1223                            }
1224                            eError = WaitForState(pHandle, OMX_StateIdle);
1225#ifdef OMX_GETTIME
1226                            GT_END("Call to SendCommand <OMX_StateIdle>");
1227#endif
1228                            if(eError != OMX_ErrorNone) {
1229                                APP_DPRINT( "Error:  G729Encoder->WaitForState reports an error         %X\n", eError);
1230                                bExitOnError = OMX_TRUE;
1231                                goto EXIT;
1232                            }
1233                            done = 1;
1234                        }
1235                        if(nFrameCount == 20) {
1236                            printf("%d :: App: Sending Stop After %d frames \n",__LINE__,nFrameCount);
1237#ifdef OMX_GETTIME
1238                            GT_START();
1239#endif
1240                            eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateIdle, NULL);
1241                            if(eError != OMX_ErrorNone) {
1242                                printf("Error from SendCommand-Idle(Stop) State function\n");
1243                                bExitOnError = OMX_TRUE;
1244                                goto EXIT;
1245                            }
1246
1247                            eError = WaitForState(pHandle, OMX_StateIdle);
1248#ifdef OMX_GETTIME
1249                            GT_END("Call to SendCommand <OMX_StateIdle>");
1250#endif
1251                            if(eError != OMX_ErrorNone) {
1252                                APP_DPRINT( "Error:  G729Encoder->WaitForState reports an error         %X\n", eError);
1253                                bExitOnError = OMX_TRUE;
1254                                goto EXIT;
1255                            }
1256                            done = 1;
1257                        }
1258                    }
1259                    break;
1260                default:
1261                    APP_DPRINT("%d :: ### Simple DEFAULT Case Here ###\n",__LINE__);
1262                }
1263
1264                if( FD_ISSET(OpBuf_Pipe[0], &rfds) ) {
1265                    OMX_BUFFERHEADERTYPE* pBuf = NULL;
1266                    read(OpBuf_Pipe[0], &pBuf, sizeof(pBuf));
1267                    APP_DPRINT("%d :: App: pBuf->nFilledLen = %ld\n",__LINE__, pBuf->nFilledLen);
1268                    nFrameLen = pBuf->nFilledLen;
1269                    /* strip off the G.729 Encoder frame format */
1270                    int subFrameLength = 0;
1271                    char* temp = (char *)pBuf->pBuffer;
1272                    while (nFrameLen > 0) {
1273                        frameType = *(temp);
1274                        temp++;
1275                        switch (frameType) {
1276                        case 0:
1277                            subFrameLength = 0;
1278                            break;
1279                        case 1:
1280                            subFrameLength = 10;
1281                            break;
1282                        case 2:
1283                            subFrameLength = 2;
1284                            break;
1285                        }
1286                        char Header[] = {0x21, 0x6b};
1287                        if (!doneWriting) {
1288                            if (!(subFrameLength == 0 && done)) {
1289
1290                                fwrite(Header, 1, G729ENC_HEADER_LEN, fOut);
1291                                fflush(fOut);
1292                                APP_DPRINT("%d :: App: nFrameLen = %d \n",__LINE__, nFrameLen);
1293                                APP_DPRINT("%d :: Writing OutputBuffer No: %d to the file nWrite = %d \n",__LINE__, nOutBuff, nFrameLen);
1294                                frameLengthInBytes = subFrameLength*8;
1295                                fwrite(&frameLengthInBytes,2,1,fOut);
1296                                writeITUFormat((OMX_U8*)temp, subFrameLength,fOut);
1297                                temp+= subFrameLength;
1298                                fflush(fOut);
1299                            }
1300                        }
1301                        nFrameLen = nFrameLen - subFrameLength -1;
1302                    }
1303                    if (done) doneWriting = 1;
1304                    nFrameCount++;
1305                    nOutBuff++;
1306                    OMX_FillThisBuffer(pHandle, pBuf);
1307                    APP_DPRINT("%d :: App: pBuf->nFlags = %ld\n",__LINE__, pBuf->nFlags);
1308                    if (tcID == 0){
1309                        APP_DPRINT("-----------------------------------------------------\n");
1310                        APP_DPRINT("Output time stamp = %ld\n",(long int) pBuf->nTimeStamp);
1311                        APP_DPRINT("Output tick count = %ld\n", pBuf->nTickCount);
1312                        APP_DPRINT("-----------------------------------------------------\n");
1313                    }
1314
1315
1316                    if(pBuf->nFlags == 1) {
1317                        if(tcID != 3) {
1318                            printf("%d :: App: Sending OMX_StateIdle.........From APP \n",__LINE__);
1319#ifdef OMX_GETTIME
1320                            GT_START();
1321#endif
1322                            eError = OMX_SendCommand(pHandle, OMX_CommandStateSet, OMX_StateIdle, NULL);
1323                            if(eError != OMX_ErrorNone) {
1324                                printf("Error from SendCommand-Idle(Stop) State function\n");
1325                                bExitOnError = OMX_TRUE;
1326                                goto EXIT;
1327                            }
1328                            eError = WaitForState(pHandle, OMX_StateIdle);
1329#ifdef OMX_GETTIME
1330                            GT_END("Call to SendCommand <OMX_StateIdle>");
1331#endif
1332                            if ( eError != OMX_ErrorNone ){
1333                                printf("Error:WaitForState has timed out %d", eError);
1334                                bExitOnError = OMX_TRUE;
1335                                goto EXIT;
1336                            }
1337                            audioinfo->dasfMode = 0;
1338                            pBuf->nFlags = 0;
1339                            printf("%d :: App: Shutting down ---------- \n",__LINE__);
1340                        }
1341                    }
1342                }
1343                if(done == 1) {
1344                    eError = OMX_GetState(pHandle, &state);
1345                    if(eError != OMX_ErrorNone) {
1346                        APP_DPRINT("%d :: pComponent->GetState has returned status %X\n",__LINE__, eError);
1347                        bExitOnError = OMX_TRUE;
1348                        goto EXIT;
1349                    }
1350                }
1351            } /* While Loop Ending Here */
1352            printf("%d :: App: The current state of the component = %d \n",__LINE__,state);
1353            fclose(fOut);
1354            fclose(fIn);
1355
1356            if(tcID == 5 || tcID == 4) {
1357                sleep (2);
1358            }
1359            else {
1360                sleep (0);
1361            }
1362            printf("%d :: App: G729 Encoded = %d Frames \n",__LINE__,(nOutBuff));
1363        } /*Test Case 4 & 5 Inner for loop ends here  */
1364
1365        printf ("%d :: App: Sending the OMX_StateLoaded Command\n",__LINE__);
1366#ifdef OMX_GETTIME
1367        GT_START();
1368#endif
1369        eError = OMX_SendCommand(pHandle,OMX_CommandStateSet, OMX_StateLoaded, NULL);
1370        if(eError != OMX_ErrorNone) {
1371            APP_DPRINT("%d:: Error from SendCommand-Idle State function\n",__LINE__);
1372            bExitOnError = OMX_TRUE;
1373            goto EXIT;
1374        }
1375
1376        printf ("%d :: App: Sending the OMX_CommandPortDisable Command\n",__LINE__);
1377        eError = OMX_SendCommand(pHandle, OMX_CommandPortDisable, -1, NULL);
1378        if(eError != OMX_ErrorNone) {
1379            APP_DPRINT("%d:: Error from SendCommand OMX_CommandPortDisable\n",__LINE__);
1380            bExitOnError = OMX_TRUE;
1381            goto EXIT;
1382        }
1383
1384
1385        /* free the Allocate and Use Buffers */
1386        printf("%d :: App: Freeing the Allocate OR Use Buffers in TestApp\n",__LINE__);
1387        for(i=0; i < audioinfo->nIpBufs; i++) {
1388            APP_DPRINT("%d :: App: About to free pInputBufferHeader[%d]\n",__LINE__, i);
1389            eError = OMX_FreeBuffer(pHandle, G729APP_INPUT_PORT, pInputBufferHeader[i]);
1390            if((eError != OMX_ErrorNone)) {
1391                APP_DPRINT("%d:: Error in FreeBuffer function\n",__LINE__);
1392                goto EXIT;
1393            }
1394        }
1395        for(i=0; i < audioinfo->nOpBufs; i++) {
1396            APP_DPRINT("%d :: App: About to free pOutputBufferHeader[%d]\n",__LINE__, i);
1397            eError = OMX_FreeBuffer(pHandle, G729APP_OUTPUT_PORT, pOutputBufferHeader[i]);
1398            if((eError != OMX_ErrorNone)) {
1399                APP_DPRINT("%d :: Error in Free Buffer function\n",__LINE__);
1400                goto EXIT;
1401            }
1402        }
1403#ifdef USE_BUFFER
1404        /* free the App Allocated Buffers */
1405        printf("%d :: App: Freeing the App Allocated Buffers in TestApp\n",__LINE__);
1406        for(i=0; i < audioinfo->nIpBufs; i++) {
1407            pInputBuffer[i] = pInputBuffer[i] -128;
1408            APP_MEMPRINT("%d :: App: [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
1409            if(pInputBuffer[i] != NULL){
1410                free(pInputBuffer[i]);
1411                pInputBuffer[i] = NULL;
1412            }
1413        }
1414
1415        for(i=0; i < audioinfo->nOpBufs; i++) {
1416            pOutputBuffer[i] = pOutputBuffer[i] - 128;
1417            APP_MEMPRINT("%d :: App: [TESTAPPFREE] pOutputBuffer[%d] = %p\n",__LINE__,i, pOutputBuffer[i]);
1418            if(pOutputBuffer[i] != NULL){
1419                free(pOutputBuffer[i]);
1420                pOutputBuffer[i] = NULL;
1421            }
1422        }
1423#endif
1424
1425        eError = WaitForState(pHandle, OMX_StateLoaded);
1426
1427
1428#ifdef OMX_GETTIME
1429        GT_END("Call to SendCommand <OMX_StateLoaded>, Disableport and cleared the buffers");
1430#endif
1431        if(eError != OMX_ErrorNone) {
1432            APP_DPRINT( "Error:  G729Encoder->WaitForState reports an error         %X\n", eError);
1433            bExitOnError = OMX_TRUE;
1434            goto EXIT;
1435        }
1436
1437        APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,pG729Param);
1438        if(pG729Param != NULL){
1439            free(pG729Param);
1440            pG729Param = NULL;
1441        }
1442        APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,pCompPrivateStruct);
1443        if(pCompPrivateStruct != NULL){
1444            free(pCompPrivateStruct);
1445            pCompPrivateStruct = NULL;
1446        }
1447        APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,audioinfo);
1448        if(audioinfo != NULL){
1449            free(audioinfo);
1450            audioinfo = NULL;
1451        }
1452        /* Unload the G729 Encoder Component */
1453        eError = TIOMX_FreeHandle(pHandle);
1454        if((eError != OMX_ErrorNone)) {
1455            APP_DPRINT("%d :: Error in Free Handle function\n",__LINE__);
1456            goto EXIT;
1457        }
1458
1459    } /*Outer for loop ends here */
1460    /* De-Initialize OMX Core */
1461    eError = TIOMX_Deinit();
1462    if (eError != OMX_ErrorNone) {
1463        printf("App::Failed to de-init OMX Core!\n");
1464    }
1465    printf("EBD = %d\n FBD = %d\n", ebd, fbd);
1466    printf("%d :: *********************************************************************\n",__LINE__);
1467    printf("%d :: NOTE: An output file %s has been created in file system\n",__LINE__,argv[2]);
1468    printf("%d :: *********************************************************************\n",__LINE__);
1469
1470 EXIT:
1471
1472    if (bExitOnError){
1473        printf("Application exiting due to an error!!\n");
1474#ifdef USE_BUFFER
1475        FreeResources(pG729Param, pCompPrivateStruct, audioinfo, pInputBuffer, pOutputBuffer);
1476#else
1477        FreeResources(pG729Param, pCompPrivateStruct, audioinfo, pInputBufferHeader, pOutputBufferHeader, pHandle);
1478#endif
1479        /* Unload the G729 Encoder Component */
1480        eError = TIOMX_FreeHandle(pHandle);
1481        if((eError != OMX_ErrorNone)) {
1482            APP_DPRINT("%d :: Error in Free Handle function\n",__LINE__);
1483            goto EXIT;
1484        }
1485    }
1486
1487#ifdef DSP_RENDERING_ON
1488    cmd_data.hComponent = pHandle;
1489    cmd_data.AM_Cmd = AM_Exit;
1490    if((write(g729encfdwrite, &cmd_data, sizeof(cmd_data)))<0)
1491        printf("%d ::- send command to audio manager\n",__LINE__);
1492
1493    close(g729encfdwrite);
1494    close(g729encfdread);
1495#endif
1496#ifdef OMX_GETTIME
1497    GT_END("G729Enc test <End>");
1498    OMX_ListDestroy(pListHead);
1499#endif
1500
1501    return eError;
1502}
1503
1504
1505/* This function writes the output into a format that is expected by the decoder */
1506void writeITUFormat(OMX_U8* buffer, OMX_U32 length, FILE* fOut)
1507{
1508
1509    int i = 0,j = 0;
1510    OMX_U8 theByte = 0;
1511    OMX_U8 theMask = 0;
1512    OMX_U8 theBit = 0;
1513    OMX_U16 theWord = 0;
1514
1515    for (j=0; j < length; j++) {
1516
1517        theByte = buffer[j];
1518        theMask = 128;
1519        for (i=0; i < 8; i++) {
1520            theBit = (theMask & theByte) >> (7-i);
1521            theMask = theMask >> 1;
1522            if (theBit == 0) {
1523                theWord = BIT_0;
1524            }
1525            else if (theBit == 1) {
1526                theWord = BIT_1;
1527            }
1528            fwrite(&theWord,2,1,fOut);
1529        }
1530    }
1531}
1532
1533#ifdef USE_BUFFER
1534OMX_ERRORTYPE FreeResources(OMX_AUDIO_PARAM_G729TYPE* pG729Param,
1535                            OMX_PARAM_PORTDEFINITIONTYPE* pCompPrivateStruct,
1536                            AUDIO_INFO* audioinfo,
1537                            OMX_U8* pInputBuffer[G729APP_MAX_NUM_OF_BUFS],
1538                            OMX_U8* pOutputBuffer[G729APP_MAX_NUM_OF_BUFS])
1539{
1540
1541
1542    OMX_ERRORTYPE eError = OMX_ErrorNone;
1543    int i = 0;
1544
1545    /* free the App Allocated Buffers */
1546    printf("%d :: App: Freeing the App Allocated Buffers in TestApp\n",__LINE__);
1547    for(i=0; i < audioinfo->nIpBufs; i++) {
1548        pInputBuffer[i] = pInputBuffer[i] -128;
1549        APP_MEMPRINT("%d :: App: [TESTAPPFREE] pInputBuffer[%d] = %p\n",__LINE__,i,pInputBuffer[i]);
1550        if(pInputBuffer[i] != NULL){
1551            free(pInputBuffer[i]);
1552            pInputBuffer[i] = NULL;
1553        }
1554    }
1555
1556    for(i=0; i < audioinfo->nOpBufs; i++) {
1557        pOutputBuffer[i] = pOutputBuffer[i] - 128;
1558        APP_MEMPRINT("%d :: App: [TESTAPPFREE] pOutputBuffer[%d] = %p\n",__LINE__,i, pOutputBuffer[i]);
1559        if(pOutputBuffer[i] != NULL){
1560            free(pOutputBuffer[i]);
1561            pOutputBuffer[i] = NULL;
1562        }
1563    }
1564
1565
1566    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,pG729Param);
1567    if(pG729Param != NULL){
1568        free(pG729Param);
1569        pG729Param = NULL;
1570    }
1571    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,pCompPrivateStruct);
1572    if(pCompPrivateStruct != NULL){
1573        free(pCompPrivateStruct);
1574        pCompPrivateStruct = NULL;
1575    }
1576    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,audioinfo);
1577    if(audioinfo != NULL){
1578        free(audioinfo);
1579        audioinfo = NULL;
1580    }
1581
1582    printf("%d :: App: Closing the Input and Output Pipes\n",__LINE__);
1583    eError = close (IpBuf_Pipe[0]);
1584    if (0 != eError && OMX_ErrorNone == eError) {
1585        eError = OMX_ErrorHardware;
1586        APP_DPRINT("%d :: Error while closing IpBuf_Pipe[0]\n",__LINE__);
1587        goto EXIT;
1588    }
1589    eError = close (IpBuf_Pipe[1]);
1590    if (0 != eError && OMX_ErrorNone == eError) {
1591        eError = OMX_ErrorHardware;
1592        APP_DPRINT("%d :: Error while closing IpBuf_Pipe[1]\n",__LINE__);
1593        goto EXIT;
1594    }
1595    eError = close (OpBuf_Pipe[0]);
1596    if (0 != eError && OMX_ErrorNone == eError) {
1597        eError = OMX_ErrorHardware;
1598        APP_DPRINT("%d :: Error while closing OpBuf_Pipe[0]\n",__LINE__);
1599        goto EXIT;
1600    }
1601    eError = close (OpBuf_Pipe[1]);
1602    if (0 != eError && OMX_ErrorNone == eError) {
1603        eError = OMX_ErrorHardware;
1604        APP_DPRINT("%d :: Error while closing OpBuf_Pipe[1]\n",__LINE__);
1605        goto EXIT;
1606    }
1607
1608
1609
1610 EXIT:
1611    return eError;
1612}
1613
1614#else
1615
1616OMX_ERRORTYPE FreeResources(OMX_AUDIO_PARAM_G729TYPE* pG729Param,
1617                            OMX_PARAM_PORTDEFINITIONTYPE* pCompPrivateStruct,
1618                            AUDIO_INFO* audioinfo,
1619                            OMX_BUFFERHEADERTYPE* pInputBufferHeader[G729APP_MAX_NUM_OF_BUFS],
1620                            OMX_BUFFERHEADERTYPE* pOutputBufferHeader[G729APP_MAX_NUM_OF_BUFS],
1621                            OMX_HANDLETYPE pHandle)
1622{
1623
1624
1625    OMX_ERRORTYPE eError = OMX_ErrorNone;
1626    int i = 0;
1627
1628    /* free the Allocate and Use Buffers */
1629    printf("%d :: App: Freeing the Allocate OR Use Buffers in TestApp\n",__LINE__);
1630    for(i=0; i < audioinfo->nIpBufs; i++) {
1631        APP_DPRINT("%d :: App: About to free pInputBufferHeader[%d]\n",__LINE__, i);
1632        eError = OMX_FreeBuffer(pHandle, G729APP_INPUT_PORT, pInputBufferHeader[i]);
1633        if((eError != OMX_ErrorNone)) {
1634            APP_DPRINT("%d:: Error in FreeBuffer function\n",__LINE__);
1635            bExitOnError = OMX_TRUE;
1636            goto EXIT;
1637        }
1638    }
1639    for(i=0; i < audioinfo->nOpBufs; i++) {
1640        APP_DPRINT("%d :: App: About to free pOutputBufferHeader[%d]\n",__LINE__, i);
1641        eError = OMX_FreeBuffer(pHandle, G729APP_OUTPUT_PORT, pOutputBufferHeader[i]);
1642        if((eError != OMX_ErrorNone)) {
1643            APP_DPRINT("%d :: Error in Free Buffer function\n",__LINE__);
1644            bExitOnError = OMX_TRUE;
1645            goto EXIT;
1646        }
1647    }
1648
1649    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,pG729Param);
1650    if(pG729Param != NULL){
1651        free(pG729Param);
1652        pG729Param = NULL;
1653    }
1654    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,pCompPrivateStruct);
1655    if(pCompPrivateStruct != NULL){
1656        free(pCompPrivateStruct);
1657        pCompPrivateStruct = NULL;
1658    }
1659    APP_MEMPRINT("%d :: App: [TESTAPPFREE] %p\n",__LINE__,audioinfo);
1660    if(audioinfo != NULL){
1661        free(audioinfo);
1662        audioinfo = NULL;
1663    }
1664
1665    printf("%d :: App: Closing the Input and Output Pipes\n",__LINE__);
1666    eError = close (IpBuf_Pipe[0]);
1667    if (0 != eError && OMX_ErrorNone == eError) {
1668        eError = OMX_ErrorHardware;
1669        APP_DPRINT("%d :: Error while closing IpBuf_Pipe[0]\n",__LINE__);
1670        goto EXIT;
1671    }
1672    eError = close (IpBuf_Pipe[1]);
1673    if (0 != eError && OMX_ErrorNone == eError) {
1674        eError = OMX_ErrorHardware;
1675        APP_DPRINT("%d :: Error while closing IpBuf_Pipe[1]\n",__LINE__);
1676        goto EXIT;
1677    }
1678    eError = close (OpBuf_Pipe[0]);
1679    if (0 != eError && OMX_ErrorNone == eError) {
1680        eError = OMX_ErrorHardware;
1681        APP_DPRINT("%d :: Error while closing OpBuf_Pipe[0]\n",__LINE__);
1682        goto EXIT;
1683    }
1684    eError = close (OpBuf_Pipe[1]);
1685    if (0 != eError && OMX_ErrorNone == eError) {
1686        eError = OMX_ErrorHardware;
1687        APP_DPRINT("%d :: Error while closing OpBuf_Pipe[1]\n",__LINE__);
1688        goto EXIT;
1689    }
1690
1691 EXIT:
1692
1693    return eError;
1694}
1695
1696#endif
1697