1/*
2 * Copyright (c) 2010, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * *  Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 *
12 * *  Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * *  Neither the name of Texas Instruments Incorporated nor the names of
17 *    its contributors may be used to endorse or promote products derived
18 *    from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#define COMPONENT_NAME "OMX.TI.DUCATI1.MISC.SAMPLE"	// needs to be specific for every configuration wrapper
34#define OMX_SAMPLE_USEBUF OMX_TRUE
35
36/****************************************************************
37*  INCLUDE FILES
38****************************************************************/
39/* ----- system and platform files ----------------------------*/
40#include <stdint.h>
41#include <string.h>
42#include <stdlib.h>
43#include <stdio.h>
44
45
46/*-------program files ----------------------------------------*/
47#include <OMX_Core.h>
48#include <OMX_Component.h>
49#include "timm_osal_interfaces.h"
50
51
52#define OMX_SAMPLE_TILER_TEST
53
54#ifdef OMX_SAMPLE_TILER_TEST
55
56#include "memmgr.h"
57
58#endif
59
60
61#define OMX_SAMPLE_INPUT_PORT 0
62#define OMX_SAMPLE_OUTPUT_PORT 1
63
64#ifdef OMX_SAMPLE_TILER_TEST
65
66
67#define OMX_SAMPLE_IN_2DYHEIGHT 4
68#define OMX_SAMPLE_IN_2DYWIDTH 8
69#define OMX_SAMPLE_IN_2DUVHEIGHT 2
70#define OMX_SAMPLE_IN_2DUVWIDTH 4
71/*
72 #define STRIDE_8BIT (16 * 1024)
73 #define STRIDE_16BIT (32 * 1024)
74*/
75#define OMX_SAMPLE_IN_HEIGHT 6
76#define OMX_SAMPLE_IN_WIDTH 8
77
78#define STRIDE_LINUX (4 * 1024)
79
80#define OMX_SAMPLE_BUFFER_SIZE 48
81
82
83void Test_Util_Memcpy_1Dto2D(TIMM_OSAL_PTR pDst2D, TIMM_OSAL_PTR pSrc1D,
84    TIMM_OSAL_U32 nSize1D, TIMM_OSAL_U32 nHeight2D, TIMM_OSAL_U32 nWidth2D,
85    TIMM_OSAL_U32 nStride2D)
86{
87	TIMM_OSAL_U8 *pInBuffer;
88	TIMM_OSAL_U8 *pOutBuffer;
89	TIMM_OSAL_U32 nSizeLeft, i;
90
91	nSizeLeft = nSize1D;
92	pInBuffer = (TIMM_OSAL_U8 *) pSrc1D;
93	pOutBuffer = (TIMM_OSAL_U8 *) pDst2D;
94	//The lower limit is copied. If nSize1D < H*W then 1Dsize is copied else H*W is copied
95	for (i = 0; i < nHeight2D; i++)
96	{
97		if (nSizeLeft >= nWidth2D)
98		{
99			TIMM_OSAL_Memcpy(pOutBuffer, pInBuffer, nWidth2D);
100		} else
101		{
102			TIMM_OSAL_Memcpy(pOutBuffer, pInBuffer, nSizeLeft);
103			break;
104		}
105		nSizeLeft -= nWidth2D;
106		pInBuffer =
107		    (TIMM_OSAL_U8 *) ((TIMM_OSAL_U32) pInBuffer + nWidth2D);
108		pOutBuffer =
109		    (TIMM_OSAL_U8 *) ((TIMM_OSAL_U32) pOutBuffer + nStride2D);
110	}
111}
112
113#endif
114
115
116/****************************************************************
117*  EXTERNAL REFERENCES NOTE : only use if not found in header file
118****************************************************************/
119/*--------data declarations -----------------------------------*/
120/*--------function prototypes ---------------------------------*/
121
122/****************************************************************
123*  PRIVATE DECLARATIONS Defined and used only here
124****************************************************************/
125/*--------function declarations -------------------------------*/
126
127/*--------data declarations -----------------------------------*/
128#define NUM_DOMAINS 0x4
129#define OMX_NOPORT 0xfffffffe
130
131//#define INPUT_FILE "../../omx/omx_il_1_x/omx_base/test/patterns/chikita.mp3"
132#define INPUT_FILE "sample_input.mp3"
133#define NON_TUN_OUTPUT_FILE "nt_output.mp3"
134
135static int gTest = 1;
136
137#define TIMM_OSAL_MallocaBuffer(_size, bContigous, nBlockAlignment) \
138        TIMM_OSAL_Malloc (_size, TIMM_OSAL_TRUE, 0, TIMMOSAL_MEM_SEGMENT_EXT)
139
140#define OMX_TEST_BUFFERS_OF_TRAFFIC 20
141
142#define OMX_TEST_BAIL_IF_ERROR(_eError)		\
143    if(OMX_ErrorNone != (eError = _eError)){	\
144        goto OMX_TEST_BAIL;		\
145    }
146
147#define OMX_TEST_SET_ERROR_BAIL(_eCode, _desc)	\
148{						\
149    eError = _eCode;				\
150    printf(_desc);\
151    goto OMX_TEST_BAIL;			\
152}
153
154#define OMX_TEST_INIT_STRUCT(_s_, _name_)	\
155    memset(&(_s_), 0x0, sizeof(_name_));	\
156    (_s_).nSize = sizeof(_name_);		\
157    (_s_).nVersion.s.nVersionMajor = 0x1;	\
158    (_s_).nVersion.s.nVersionMinor = 0x1;	\
159    (_s_).nVersion.s.nRevision = 0x0;		\
160    (_s_).nVersion.s.nStep = 0x0
161
162#define BUFFER_LIST_CLEAR_ENTRY(_pL, _pB)\
163    _pB = _pL->pBufHdr;        \
164    _pL->pBufHdr = NULL;    \
165    _pL = _pL->pNextBuf;
166
167#define BUFFER_LIST_SET_ENTRY(_pL, _pB)    \
168{                \
169    BufferList *_pT = _pL;    \
170    while(_pT && _pT->pBufHdr){    \
171        _pT = _pT->pNextBuf;    \
172    }                \
173    if(_pT)            \
174        _pT->pBufHdr = _pB;    \
175}
176
177#define BUFFERLIST_CLEAR_ENTRY(_pL, _pB)\
178    _pB = _pL->pBufHdr;        \
179    _pL->pBufHdr = NULL;    \
180    _pL = _pL->pNextBuf;
181
182typedef struct _BufferList BufferList;
183
184struct _BufferList
185{
186	OMX_BUFFERHEADERTYPE *pBufHdr;
187	OMX_BUFFERHEADERTYPE *pOrigBufHdr;
188	BufferList *pNextBuf;
189};
190
191typedef struct SampleCompTestCtxt
192{
193	OMX_HANDLETYPE hComp;
194	OMX_STATETYPE eState;
195	OMX_U32 nPorts;
196	OMX_HANDLETYPE hStateSetEvent;
197	OMX_HANDLETYPE hPortDisableEvent;
198	OMX_HANDLETYPE hPortEnableEvent;
199	OMX_STRING inFilePath;
200	OMX_STRING outFilePath;
201	FILE *pOutputFile;
202	FILE *pInputfile;
203	OMX_BOOL bEOS;
204	BufferList *pInBufferList;
205	BufferList *pOutBufferList;
206	OMX_BOOL bClientAllocBuf;
207	OMX_U32 nBufDoneCalls;
208	OMX_PORT_PARAM_TYPE sPortParam[NUM_DOMAINS];
209} SampleCompTestCtxt;
210
211//this test compiles only for ducati SYS
212typedef struct TestCtxt
213{
214	OMX_HANDLETYPE hComp;
215	OMX_STATETYPE eState;
216	OMX_U32 nPorts;
217	OMX_HANDLETYPE hStateSetEvent;
218	OMX_HANDLETYPE hPortDisableEvent;
219	OMX_HANDLETYPE hPortEnableEvent;
220	OMX_STRING inFilePath;
221	OMX_STRING outFilePath;
222	FILE *pOutputFile;
223	FILE *pInputfile;
224	OMX_BOOL bEOS;
225	BufferList *pInBufferList;
226	BufferList *pOutBufferList;
227	OMX_BOOL bClientAllocBuf;
228	OMX_U32 nBufDoneCalls;
229	//OMX_PORT_PARAM_TYPE sPortParam[NUM_DOMAINS];
230} TestCtxt;
231
232
233static OMX_U32 nInBufCount = 0;
234static OMX_U32 nOutBufCount = 0;
235
236
237//Semaphore_Handle EBDSem;
238//Semaphore_Params EBDsemParams;
239
240//Semaphore_Handle FBDSem;
241//Semaphore_Params FBDsemParams;
242/*========================================================*/
243/* @ fn OMX_TEST_ErrorToString :: ERROR  to  STRING   */
244/*========================================================*/
245OMX_STRING OMX_TEST_ErrorToString(OMX_ERRORTYPE eError)
246{
247
248	OMX_STRING errorString;
249
250	switch (eError)
251	{
252
253	case OMX_ErrorNone:
254		errorString = "ErrorNone";
255		break;
256	case OMX_ErrorInsufficientResources:
257		errorString = "ErrorInsufficientResources";
258		break;
259	case OMX_ErrorUndefined:
260		errorString = "ErrorUndefined";
261		break;
262	case OMX_ErrorInvalidComponentName:
263		errorString = "ErrorInvalidComponentName";
264		break;
265	case OMX_ErrorComponentNotFound:
266		errorString = "ErrorComponentNotFound";
267		break;
268	case OMX_ErrorInvalidComponent:
269		errorString = "ErrorInvalidComponent";
270		break;
271	case OMX_ErrorBadParameter:
272		errorString = "ErrorBadParameter";
273		break;
274	case OMX_ErrorNotImplemented:
275		errorString = "ErrorNotImplemented";
276		break;
277	case OMX_ErrorUnderflow:
278		errorString = "ErrorUnderflow";
279		break;
280	case OMX_ErrorOverflow:
281		errorString = "ErrorOverflow";
282		break;
283	case OMX_ErrorHardware:
284		errorString = "ErrorHardware";
285		break;
286	case OMX_ErrorInvalidState:
287		errorString = "ErrorInvalidState";
288		break;
289	case OMX_ErrorStreamCorrupt:
290		errorString = "ErrorStreamCorrupt";
291		break;
292	case OMX_ErrorPortsNotCompatible:
293		errorString = "ErrorPortsNotCompatible";
294		break;
295	case OMX_ErrorResourcesLost:
296		errorString = "ErrorResourcesLost";
297		break;
298	case OMX_ErrorNoMore:
299		errorString = "ErrorNoMore";
300		break;
301	case OMX_ErrorVersionMismatch:
302		errorString = "ErrorVersionMismatch";
303		break;
304	case OMX_ErrorNotReady:
305		errorString = "ErrorNotReady";
306		break;
307	case OMX_ErrorTimeout:
308		errorString = "ErrorTimeout";
309		break;
310	case OMX_ErrorSameState:
311		errorString = "ErrorSameState";
312		break;
313	case OMX_ErrorResourcesPreempted:
314		errorString = "ErrorResourcesPreempted";
315		break;
316	case OMX_ErrorPortUnresponsiveDuringAllocation:
317		errorString = "ErrorPortUnresponsiveDuringAllocation";
318		break;
319	case OMX_ErrorPortUnresponsiveDuringDeallocation:
320		errorString = "ErrorPortUnresponsiveDuringDeallocation";
321		break;
322	case OMX_ErrorPortUnresponsiveDuringStop:
323		errorString = "ErrorPortUnresponsiveDuringStop";
324		break;
325	case OMX_ErrorIncorrectStateTransition:
326		errorString = "ErrorIncorrectStateTransition";
327		break;
328	case OMX_ErrorIncorrectStateOperation:
329		errorString = "ErrorIncorrectStateOperation";
330		break;
331	case OMX_ErrorUnsupportedSetting:
332		errorString = "ErrorUnsupportedSetting";
333		break;
334	case OMX_ErrorUnsupportedIndex:
335		errorString = "ErrorUnsupportedIndex";
336		break;
337	case OMX_ErrorBadPortIndex:
338		errorString = "ErrorBadPortIndex";
339		break;
340	case OMX_ErrorPortUnpopulated:
341		errorString = "ErrorPortUnpopulated";
342		break;
343	case OMX_ErrorComponentSuspended:
344		errorString = "ErrorComponentSuspended";
345		break;
346	case OMX_ErrorDynamicResourcesUnavailable:
347		errorString = "ErrorDynamicResourcesUnavailable";
348		break;
349	case OMX_ErrorMbErrorsInFrame:
350		errorString = "ErrorMbErrorsInFrame";
351		break;
352	case OMX_ErrorFormatNotDetected:
353		errorString = "ErrorFormatNotDetected";
354		break;
355	case OMX_ErrorContentPipeOpenFailed:
356		errorString = "ErrorContentPipeOpenFailed";
357		break;
358	case OMX_ErrorContentPipeCreationFailed:
359		errorString = "ErrorContentPipeCreationFailed";
360		break;
361	case OMX_ErrorSeperateTablesUsed:
362		errorString = "ErrorSeperateTablesUsed";
363		break;
364	case OMX_ErrorTunnelingUnsupported:
365		errorString = "ErrorTunnelingUnsupported";
366		break;
367	default:
368		errorString = "<unknown>";
369		break;
370	}
371	return errorString;
372}
373
374/*========================================================*/
375/* @ fn OMX_TEST_StateToString :: STATE  to  STRING   */
376/*========================================================*/
377OMX_STRING OMX_TEST_StateToString(OMX_STATETYPE eState)
378{
379	OMX_STRING StateString;
380
381	switch (eState)
382	{
383	case OMX_StateInvalid:
384		StateString = "Invalid";
385		break;
386	case OMX_StateLoaded:
387		StateString = "Loaded";
388		break;
389	case OMX_StateIdle:
390		StateString = "Idle";
391		break;
392	case OMX_StateExecuting:
393		StateString = "Executing";
394		break;
395	case OMX_StatePause:
396		StateString = "Pause";
397		break;
398	case OMX_StateWaitForResources:
399		StateString = "WaitForResources ";
400		break;
401	default:
402		StateString = "<unknown>";
403		break;
404	}
405
406	return StateString;
407}
408
409/* Application callback Functions */
410/*========================================================*/
411/* @ fn SampleTest_EventHandler :: Application callback   */
412/*========================================================*/
413OMX_ERRORTYPE SampleTest_EventHandler(OMX_IN OMX_HANDLETYPE hComponent,
414    OMX_IN OMX_PTR pAppData,
415    OMX_IN OMX_EVENTTYPE eEvent,
416    OMX_IN OMX_U32 nData1, OMX_IN OMX_U32 nData2, OMX_IN OMX_PTR pEventData)
417{
418	SampleCompTestCtxt *pContext;
419
420	printf("\n___________ ENTERED CLIENT CALLBACK:%s", __FUNCTION__);
421
422	if (pAppData == NULL)
423		return OMX_ErrorNone;
424
425	pContext = (SampleCompTestCtxt *) pAppData;
426
427	switch (eEvent)
428	{
429	case OMX_EventCmdComplete:
430		printf("\n OMX_EventCmdComplete case: \n");
431		if (OMX_CommandStateSet == nData1)
432		{
433			printf(" Component Transitioned to %s state \n",
434			    OMX_TEST_StateToString((OMX_STATETYPE) nData2));
435			pContext->eState = (OMX_STATETYPE) nData2;
436			TIMM_OSAL_SemaphoreRelease(pContext->hStateSetEvent);
437		} else if (OMX_CommandFlush == nData1)
438		{
439			/* Nothing to do over here */
440		} else if (OMX_CommandPortDisable == nData1)
441		{
442			/* Nothing to do over here */
443			TIMM_OSAL_SemaphoreRelease(pContext->
444			    hPortDisableEvent);
445		} else if (OMX_CommandPortEnable == nData1)
446		{
447			/* Nothing to do over here */
448		} else if (OMX_CommandMarkBuffer == nData1)
449		{
450			/* Nothing to do over here */
451		}
452		break;
453
454	case OMX_EventError:
455		printf("\nOMX EVENT ERROR!!!!!! \n");
456		break;
457
458	case OMX_EventMark:
459		break;
460
461	case OMX_EventPortSettingsChanged:
462		break;
463
464	case OMX_EventBufferFlag:
465		break;
466
467	case OMX_EventResourcesAcquired:
468		break;
469
470	case OMX_EventComponentResumed:
471		break;
472
473	case OMX_EventDynamicResourcesAvailable:
474		break;
475
476	case OMX_EventPortFormatDetected:
477		break;
478
479	default:
480		break;
481	}
482
483	goto OMX_TEST_BAIL;
484      OMX_TEST_BAIL:
485	return OMX_ErrorNone;
486}
487
488/*========================================================*/
489/* @ fn SampleTest_EmptyBufferDone :: Application callback    */
490/*========================================================*/
491OMX_ERRORTYPE SampleTest_EmptyBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
492    OMX_IN OMX_PTR pAppData, OMX_IN OMX_BUFFERHEADERTYPE * pBuffer)
493{
494	SampleCompTestCtxt *pContext;
495
496	printf("\n___________ ENTERED CLIENT CALLBACK:%s", __FUNCTION__);
497
498	if (pAppData == NULL)
499		return OMX_ErrorNone;
500
501	pContext = (SampleCompTestCtxt *) pAppData;
502	pContext->nBufDoneCalls++;
503
504	BUFFER_LIST_SET_ENTRY(pContext->pInBufferList, pBuffer);
505	//Semaphore_post(EBDSem);
506
507	goto OMX_TEST_BAIL;
508      OMX_TEST_BAIL:
509	printf("\nEBD done\n");
510	return OMX_ErrorNone;
511}
512
513/*========================================================*/
514/* @ fn SampleTest_FillBufferDone ::   Application callback  */
515/*========================================================*/
516OMX_ERRORTYPE SampleTest_FillBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
517    OMX_IN OMX_PTR pAppData, OMX_IN OMX_BUFFERHEADERTYPE * pBuffHeader)
518{
519	SampleCompTestCtxt *pContext;
520
521	printf("\n___________ ENTERED CLIENT CALLBACK:%s", __FUNCTION__);
522
523	if (pAppData == NULL)
524		return OMX_ErrorNone;
525
526	pContext = (SampleCompTestCtxt *) pAppData;
527	pContext->nBufDoneCalls++;
528
529	if (pContext->pOutputFile && gTest)
530	{
531		printf(" writing to output file :: buffer cnt : %d\n",
532		    nOutBufCount);
533		nOutBufCount++;
534		fwrite(pBuffHeader->pBuffer, 1, pBuffHeader->nFilledLen,
535		    pContext->pOutputFile);
536	}
537
538	BUFFER_LIST_SET_ENTRY(pContext->pOutBufferList, pBuffHeader);
539	//Semaphore_post(FBDSem);
540
541	goto OMX_TEST_BAIL;
542      OMX_TEST_BAIL:
543	printf("\nFBD done\n");
544	return OMX_ErrorNone;
545}
546
547
548static void SampleTest_ReadInputFile(SampleCompTestCtxt * pContext,
549    OMX_PTR pData, OMX_U32 nBytes, FILE * fp)
550{
551	OMX_U32 nReadSize = 0;
552
553	printf("\nIn read i/p file\n");
554	if (pData == NULL)
555		printf("\npData is NULL\n");
556	else
557		printf("\npData = %x\n", pData);
558	printf("\nnBytes = %d\n", nBytes);
559	if (fp == NULL)
560		printf("\nfp is NULL\n");
561	else
562		printf("\nfp = %x\n", fp);
563	nReadSize = fread(pData, 1, nBytes, fp);
564	if (nReadSize != nBytes)
565	{
566		pContext->bEOS = OMX_TRUE;
567	}
568
569	printf(" Reading from file :: Buffer cont : %d \n", nInBufCount);
570	nInBufCount++;
571}
572
573/*========================================================*/
574/* @ fn SampleTest_WriteInBuffers ::   Reads a buffer from a file and send to Comp       */
575/*========================================================*/
576OMX_ERRORTYPE SampleTest_WriteInBuffers(SampleCompTestCtxt * pContext)
577{
578
579	OMX_ERRORTYPE eError = OMX_ErrorNone;
580	BufferList *pList;
581	OMX_BUFFERHEADERTYPE *pBufHeader;
582#ifdef OMX_SAMPLE_TILER_TEST
583	OMX_U8 *pTmpBuffer = NULL, *pOrigTmpBuffer = NULL;
584#endif
585	pList = pContext->pInBufferList;
586	while (pList && pList->pBufHdr)
587	{
588		BUFFERLIST_CLEAR_ENTRY(pList, pBufHeader);
589		printf("\nAbout to read from input file\n");
590#if defined(OMX_SAMPLE_TILER_TEST)
591
592		//OMX_SAMPLE_BUFFER_SIZE is the total amt of data to be sent in the buffer
593		pTmpBuffer =
594		    TIMM_OSAL_Malloc(OMX_SAMPLE_BUFFER_SIZE, 0, 0, 0);
595		if (pTmpBuffer == NULL)
596			OMX_TEST_SET_ERROR_BAIL
597			    (OMX_ErrorInsufficientResources,
598			    "malloc failed \n");
599		pOrigTmpBuffer = pTmpBuffer;
600
601		SampleTest_ReadInputFile(pContext, pTmpBuffer,
602		    OMX_SAMPLE_BUFFER_SIZE, pContext->pInputfile);
603
604		Test_Util_Memcpy_1Dto2D(pBufHeader->pBuffer, pTmpBuffer,
605		    OMX_SAMPLE_BUFFER_SIZE, OMX_SAMPLE_IN_HEIGHT,
606		    OMX_SAMPLE_IN_WIDTH, STRIDE_LINUX);
607		pBufHeader->nFilledLen = OMX_SAMPLE_BUFFER_SIZE;
608
609		printf("\nBefore ETB pBufHeader->nInputPortIndex = %d\n",
610		    pBufHeader->nInputPortIndex);
611
612		TIMM_OSAL_Free(pOrigTmpBuffer);
613
614#else
615		SampleTest_ReadInputFile(pContext, pBufHeader->pBuffer,
616		    pBufHeader->nAllocLen, pContext->pInputfile);
617		pBufHeader->nFilledLen = pBufHeader->nAllocLen;
618
619#endif
620		if (pContext->bEOS == OMX_TRUE)
621		{
622			pBufHeader->nFlags |= OMX_BUFFERFLAG_EOS;
623		}
624
625		eError = OMX_EmptyThisBuffer(pContext->hComp, pBufHeader);
626
627		OMX_TEST_BAIL_IF_ERROR(eError);
628	}
629
630      OMX_TEST_BAIL:
631	return eError;
632
633}
634
635/*========================================================*/
636/* @ fn SampleTest_ReadOutBuffers ::   Send out buffers    */
637/*========================================================*/
638
639OMX_ERRORTYPE SampleTest_ReadOutBuffers(SampleCompTestCtxt * pContext)
640{
641
642	OMX_ERRORTYPE eError = OMX_ErrorNone;
643	BufferList *pList;
644	OMX_BUFFERHEADERTYPE *pBufHeader;
645
646	pList = pContext->pOutBufferList;
647	while (pList && pList->pBufHdr)
648	{
649		printf("\nAbout to do FTB\n");
650
651		BUFFERLIST_CLEAR_ENTRY(pList, pBufHeader);
652
653		eError = OMX_FillThisBuffer(pContext->hComp, pBufHeader);
654
655		OMX_TEST_BAIL_IF_ERROR(eError);
656		if (pBufHeader->nFlags == OMX_BUFFERFLAG_EOS)
657		{
658			pContext->nBufDoneCalls = OMX_TEST_BUFFERS_OF_TRAFFIC;
659		}
660	}
661
662      OMX_TEST_BAIL:
663	return eError;
664
665}
666
667
668
669/*========================================================*/
670/* @ fn SampleTest_AllocateBuffers ::   Allocates the Resources on the available ports  */
671/*========================================================*/
672OMX_ERRORTYPE SampleTest_AllocateBuffers(SampleCompTestCtxt * pContext,
673    OMX_PARAM_PORTDEFINITIONTYPE * pPortDef)
674{
675	OMX_ERRORTYPE eError = OMX_ErrorNone;
676	OMX_U8 *pBuffer = NULL;
677	BufferList *pBufferList;
678	BufferList *pTemp;
679	OMX_BUFFERHEADERTYPE *pBufferHdr;
680	OMX_U32 i = 100;
681	OMX_COMPONENTTYPE *pComp;
682
683#ifdef OMX_SAMPLE_TILER_TEST
684	MemAllocBlock *pBlock = NULL;
685	OMX_U32 nNumBlocks = 1;
686
687/*For i/p port allocate 2D packed buffer, for o/p port allocate 1D buffer.
688Ideally client should get this from GetParams but this is just a sample test so
689values are hardcoded*/
690
691	if (pPortDef->nPortIndex == OMX_SAMPLE_INPUT_PORT)
692	{
693		nNumBlocks = 2;
694		pBlock =
695		    TIMM_OSAL_Malloc(sizeof(MemAllocBlock) * nNumBlocks, 0, 0,
696		    0);
697		TIMM_OSAL_Memset(pBlock, 0, sizeof(MemAllocBlock) * nNumBlocks);
698		pBlock[0].dim.area.width = OMX_SAMPLE_IN_2DYWIDTH;
699		pBlock[0].dim.area.height = OMX_SAMPLE_IN_2DYHEIGHT;
700		pBlock[0].pixelFormat = PIXEL_FMT_8BIT;
701		pBlock[1].dim.area.width = OMX_SAMPLE_IN_2DUVWIDTH;
702		pBlock[1].dim.area.height = OMX_SAMPLE_IN_2DUVHEIGHT;
703		pBlock[1].pixelFormat = PIXEL_FMT_16BIT;
704	} else
705	{
706		nNumBlocks = 1;
707		pBlock =
708		    TIMM_OSAL_Malloc(sizeof(MemAllocBlock) * nNumBlocks, 0, 0,
709		    0);
710		TIMM_OSAL_Memset(pBlock, 0, sizeof(MemAllocBlock) * nNumBlocks);
711		pBlock[0].dim.len = OMX_SAMPLE_BUFFER_SIZE;
712		pBlock[0].pixelFormat = PIXEL_FMT_PAGE;
713	}
714#endif
715
716	for (i = 0; i < pPortDef->nBufferCountActual; i++)
717	{
718		pBufferList =
719		    (BufferList *) TIMM_OSAL_Malloc(sizeof(BufferList),
720		    TIMM_OSAL_TRUE, 0, TIMMOSAL_MEM_SEGMENT_INT);
721		if (!pBufferList)
722		{
723			OMX_TEST_SET_ERROR_BAIL
724			    (OMX_ErrorInsufficientResources,
725			    "malloc failed \n");
726		}
727
728		if (pContext->bClientAllocBuf)
729		{
730
731#ifdef OMX_SAMPLE_TILER_TEST
732/*For i/p port allocate 2D packed buffer, for o/p port allocate 1D buffer.
733Ideally client should get this from GetParams but this is just a sample test so
734values are hardcoded*/
735			pBuffer = MemMgr_Alloc(pBlock, nNumBlocks);
736			printf("\nMemMgr allocated buffer = 0x%x\n", pBuffer);
737#else
738			pBuffer =
739			    (OMX_U8 *) TIMM_OSAL_MallocaBuffer(pPortDef->
740			    nBufferSize, pPortDef->bBuffersContiguous,
741			    pPortDef->nBufferAlignment);
742
743#endif
744
745			if (!pBufferList)
746			{
747				OMX_TEST_SET_ERROR_BAIL
748				    (OMX_ErrorInsufficientResources,
749				    "malloc failed \n");
750			}
751
752			printf("\nCalling UseBuf on port %d\n",
753			    pPortDef->nPortIndex);
754			eError =
755			    OMX_UseBuffer(pContext->hComp, &pBufferHdr,
756			    pPortDef->nPortIndex, 0, pPortDef->nBufferSize,
757			    pBuffer);
758
759			OMX_TEST_BAIL_IF_ERROR(eError);
760
761		} else
762		{
763
764			pComp = (OMX_COMPONENTTYPE *) pContext->hComp;
765			printf("\nCalling allocate buffer\n");
766			eError =
767			    OMX_AllocateBuffer(pContext->hComp, &pBufferHdr,
768			    pPortDef->nPortIndex, 0, pPortDef->nBufferSize);
769
770			OMX_TEST_BAIL_IF_ERROR(eError);
771		}
772		printf("\npBufferHdr->nOutputPortIndex = %d\n",
773		    pBufferHdr->nOutputPortIndex);
774		printf("\npBufferHdr->nInputPortIndex = %d\n",
775		    pBufferHdr->nInputPortIndex);
776		pBufferList->pNextBuf = NULL;
777		pBufferList->pBufHdr = pBufferHdr;
778		pBufferList->pOrigBufHdr = pBufferHdr;
779
780		if (pPortDef->eDir == OMX_DirInput)
781		{
782			printf("\npBufferHdr->nOutputPortIndex = %d\n",
783			    pBufferHdr->nOutputPortIndex);
784			printf("\npBufferHdr->nInputPortIndex = %d\n",
785			    pBufferHdr->nInputPortIndex);
786			pBufferHdr->nOutputPortIndex = OMX_NOPORT;
787			if (pContext->pInBufferList == NULL)
788			{
789				pContext->pInBufferList = pBufferList;
790			} else
791			{
792				pTemp = pContext->pInBufferList;
793				while (pTemp->pNextBuf)
794					pTemp = pTemp->pNextBuf;
795				pTemp->pNextBuf = pBufferList;
796			}
797		} else
798		{
799			pBufferHdr->nInputPortIndex = OMX_NOPORT;
800			printf("\npBufferHdr->nOutputPortIndex = %d\n",
801			    pBufferHdr->nOutputPortIndex);
802			printf("\npBufferHdr->nInputPortIndex = %d\n",
803			    pBufferHdr->nInputPortIndex);
804			if (pContext->pOutBufferList == NULL)
805			{
806				pContext->pOutBufferList = pBufferList;
807			} else
808			{
809				pTemp = pContext->pOutBufferList;
810				while (pTemp->pNextBuf)
811					pTemp = pTemp->pNextBuf;
812				pTemp->pNextBuf = pBufferList;
813			}
814		}
815	}
816
817      OMX_TEST_BAIL:
818#ifdef OMX_SAMPLE_TILER_TEST
819	if (pBlock != NULL)
820		TIMM_OSAL_Free(pBlock);
821#endif
822	if (eError != OMX_ErrorNone)
823	{
824		if (pBufferList)
825		{
826			TIMM_OSAL_Free(pBufferList);
827		}
828	}
829
830	return eError;
831}
832
833/*========================================================*/
834/* @ fn SampleTest_DeInitBuffers ::   Destroy the resources  */
835/*========================================================*/
836OMX_ERRORTYPE SampleTest_DeInitBuffers(SampleCompTestCtxt * pContext)
837{
838	OMX_ERRORTYPE eError = OMX_ErrorNone;
839	OMX_U8 *pBuffer;
840	BufferList *pBufferList;
841	BufferList *pTemp;
842
843	OMX_U32 nRetVal = 0;
844
845	pTemp = pContext->pInBufferList;
846
847	while (pTemp)
848	{
849		pBufferList = (BufferList *) pTemp;
850		pBuffer = (OMX_U8 *) pTemp->pOrigBufHdr->pBuffer;
851
852		printf("\nCalling Free Buffer on port no. %d\n",
853		    pTemp->pOrigBufHdr->nInputPortIndex);
854
855		eError =
856		    OMX_FreeBuffer(pContext->hComp,
857		    pTemp->pOrigBufHdr->nInputPortIndex, pTemp->pOrigBufHdr);
858		OMX_TEST_BAIL_IF_ERROR(eError);
859
860                if (pContext->bClientAllocBuf)
861                {
862
863#ifdef OMX_SAMPLE_TILER_TEST
864                        nRetVal = MemMgr_Free(pBuffer);
865                        if (nRetVal)
866                        {
867                                printf("\nError in MemMgr free\n");
868                        }
869#else
870                        TIMM_OSAL_Free(pBuffer);
871#endif
872                }
873
874		pTemp = pTemp->pNextBuf;
875		if (pBufferList)
876			TIMM_OSAL_Free(pBufferList);
877	}
878
879	pContext->pInBufferList = NULL;
880
881	pTemp = pContext->pOutBufferList;
882	while (pTemp)
883	{
884		pBufferList = (BufferList *) pTemp;
885		pBuffer = (OMX_U8 *) pTemp->pOrigBufHdr->pBuffer;
886		printf("\nCalling Free Buffer on port no. %d\n",
887		    pTemp->pOrigBufHdr->nOutputPortIndex);
888
889		eError =
890		    OMX_FreeBuffer(pContext->hComp,
891		    pTemp->pOrigBufHdr->nOutputPortIndex, pTemp->pOrigBufHdr);
892		OMX_TEST_BAIL_IF_ERROR(eError);
893                if (pContext->bClientAllocBuf)
894                {
895#ifdef OMX_SAMPLE_TILER_TEST
896                        nRetVal = MemMgr_Free(pBuffer);
897                        if (nRetVal)
898                        {
899                                printf("\nError in MemMgr free\n");
900                        }
901#else
902                        TIMM_OSAL_Free(pBuffer);
903#endif
904                }
905
906		pTemp = pTemp->pNextBuf;
907		if (pBufferList)
908			TIMM_OSAL_Free(pBufferList);
909
910	}
911
912	pContext->pOutBufferList = NULL;
913
914      OMX_TEST_BAIL:
915	return eError;
916}
917
918/*========================================================*/
919/* @ fn SampleTest_TransitionWait ::   Waits for the transition to be completed ,
920 *  incase of loaded to idle Allocates the Resources and while idle to loaded
921 *  destroys the resources */
922/*========================================================*/
923OMX_ERRORTYPE SampleTest_TransitionWait(OMX_STATETYPE eToState,
924    SampleCompTestCtxt * pContext)
925{
926	OMX_ERRORTYPE eError = OMX_ErrorNone;
927	OMX_PARAM_PORTDEFINITIONTYPE tPortDef;
928	OMX_U32 i, j;
929
930	eError = OMX_SendCommand(pContext->hComp, OMX_CommandStateSet,
931	    eToState, NULL);
932	OMX_TEST_BAIL_IF_ERROR(eError);
933
934	if ((eToState == OMX_StateIdle) &&
935	    (pContext->eState == OMX_StateLoaded))
936	{
937		for (i = 0; i < NUM_DOMAINS; i++)
938		{
939			for (j = pContext->sPortParam[i].nStartPortNumber;
940			    j < pContext->sPortParam[i].nStartPortNumber
941			    + pContext->sPortParam[i].nPorts; j++)
942			{
943
944				OMX_TEST_INIT_STRUCT(tPortDef,
945				    OMX_PARAM_PORTDEFINITIONTYPE);
946				tPortDef.nPortIndex = j;
947//printf("\nCalling GetParam before UseBuf on port %d\n",j);
948				eError = OMX_GetParameter(pContext->hComp,
949				    OMX_IndexParamPortDefinition,
950				    (OMX_PTR) & tPortDef);
951				OMX_TEST_BAIL_IF_ERROR(eError);
952
953//                    if(tPortDef.bEnabled)//AD
954				eError =
955				    SampleTest_AllocateBuffers(pContext,
956				    &tPortDef);
957
958				OMX_TEST_BAIL_IF_ERROR(eError);
959			}
960		}
961	} else if ((eToState == OMX_StateLoaded) &&
962	    (pContext->eState == OMX_StateIdle))
963	{
964
965		eError = SampleTest_DeInitBuffers(pContext);
966		OMX_TEST_BAIL_IF_ERROR(eError);
967
968	}
969	printf("\nWaiting for state set event\n");
970	TIMM_OSAL_SemaphoreObtain(pContext->hStateSetEvent,
971	    TIMM_OSAL_SUSPEND);
972	printf("\nState set event recd.\n");
973
974	if (pContext->eState != eToState)
975		OMX_TEST_SET_ERROR_BAIL(OMX_ErrorUndefined,
976		    " InComplete Transition \n");
977
978      OMX_TEST_BAIL:
979	return eError;
980}
981
982
983/*========================================================*/
984/* @ fn OMX_Sample_UT0001 ::  Initializes, move to Idle and then to executing, process
985*    buffers and then destroy the component by moving back to idle, loaded, invalid */
986/*========================================================*/
987void main(void)
988{
989	OMX_ERRORTYPE eError = OMX_ErrorNone;
990	OMX_HANDLETYPE hComp = NULL;
991	OMX_CALLBACKTYPE oCallbacks;
992	SampleCompTestCtxt *pContext;
993	SampleCompTestCtxt oAppData;
994
995	int ch1, ch2;
996	int pass;
997	int while_pass = 0, loc_diff = 0;
998
999
1000	pContext = &oAppData;
1001	printf(" Entering : %s \n", __FUNCTION__);
1002	memset(pContext, 0x0, sizeof(SampleCompTestCtxt));
1003
1004	oCallbacks.EventHandler = SampleTest_EventHandler;
1005	oCallbacks.EmptyBufferDone = SampleTest_EmptyBufferDone;
1006	oCallbacks.FillBufferDone = SampleTest_FillBufferDone;
1007	printf("\nCalling sem create\n");
1008	/* Initialize Events to Track callbacks  */
1009	TIMM_OSAL_SemaphoreCreate(&pContext->hStateSetEvent, 0);
1010	TIMM_OSAL_SemaphoreCreate(&pContext->hPortDisableEvent, 0);
1011	//TIMM_OSAL_MutexObtain(pContext->hStateSetEvent, TIMM_OSAL_SUSPEND);
1012	printf("\nSem created\n");
1013
1014
1015	pContext->pInputfile = fopen(INPUT_FILE, "rb");
1016	if (NULL == pContext->pInputfile)
1017	{
1018		eError = OMX_ErrorInsufficientResources;
1019		//goto OMX_TEST_BAIL;
1020	}
1021	printf("\nInput file opened\n");
1022	pContext->pOutputFile = fopen(NON_TUN_OUTPUT_FILE, "wb");
1023	if (NULL == pContext->pOutputFile)
1024	{
1025		eError = OMX_ErrorInsufficientResources;
1026		//goto OMX_TEST_BAIL;
1027	}
1028
1029
1030	/* Initialize OpenMAX */
1031	printf("\nInitialize OpenMAX\n");
1032	eError = OMX_Init();
1033	OMX_TEST_BAIL_IF_ERROR(eError);
1034	/* Load a component  */
1035	printf("\nLoad a component\n");
1036	eError =
1037	    OMX_GetHandle(&hComp, (OMX_STRING) "OMX.TI.DUCATI1.MISC.SAMPLE",
1038	    pContext, &oCallbacks);
1039	OMX_TEST_BAIL_IF_ERROR(eError);
1040	pContext->hComp = hComp;
1041printf ("\neError = 0x%x\n", eError);
1042if(hComp)
1043	printf("\nhComp = 0x%x\n", hComp);
1044	/* Verify that the component is in Loaded state */
1045	printf("\nVerify that the component is in Loaded state\n");
1046
1047	eError = OMX_GetState(pContext->hComp, &pContext->eState);
1048	OMX_TEST_BAIL_IF_ERROR(eError);
1049	if (OMX_StateLoaded != pContext->eState)
1050	{
1051		OMX_TEST_SET_ERROR_BAIL(OMX_ErrorUndefined,
1052		    "not in loaded state \n");
1053	}
1054
1055	/* detect all Audio ports on the component */
1056	OMX_TEST_INIT_STRUCT(pContext->sPortParam[0], OMX_PORT_PARAM_TYPE);
1057	eError = OMX_GetParameter(hComp, OMX_IndexParamAudioInit,
1058	    (OMX_PTR) & pContext->sPortParam[0]);
1059	OMX_TEST_BAIL_IF_ERROR(eError);
1060
1061	/* detect all video ports on the component */
1062	OMX_TEST_INIT_STRUCT(pContext->sPortParam[1], OMX_PORT_PARAM_TYPE);
1063	eError = OMX_GetParameter(hComp, OMX_IndexParamVideoInit,
1064	    (OMX_PTR) & pContext->sPortParam[1]);
1065	OMX_TEST_BAIL_IF_ERROR(eError);
1066
1067	/* detect all image ports on the component */
1068	OMX_TEST_INIT_STRUCT(pContext->sPortParam[2], OMX_PORT_PARAM_TYPE);
1069	eError = OMX_GetParameter(hComp, OMX_IndexParamImageInit,
1070	    (OMX_PTR) & pContext->sPortParam[2]);
1071	OMX_TEST_BAIL_IF_ERROR(eError);
1072
1073	/* detect all other ports on the component */
1074	OMX_TEST_INIT_STRUCT(pContext->sPortParam[3], OMX_PORT_PARAM_TYPE);
1075	eError = OMX_GetParameter(hComp, OMX_IndexParamOtherInit,
1076	    (OMX_PTR) & pContext->sPortParam[3]);
1077	OMX_TEST_BAIL_IF_ERROR(eError);
1078
1079	pContext->nPorts = pContext->sPortParam[0].nPorts +
1080	    pContext->sPortParam[1].nPorts +
1081	    pContext->sPortParam[2].nPorts + pContext->sPortParam[3].nPorts;
1082
1083
1084	pContext->bClientAllocBuf = OMX_SAMPLE_USEBUF;
1085	if (pContext->bClientAllocBuf == OMX_TRUE)
1086	{
1087		printf(" Client does Allocation of buffers \n");
1088	} else
1089	{
1090		printf(" Component does Allocation of buffers \n");
1091	}
1092
1093	/* Transition to Idle state */
1094
1095	eError = SampleTest_TransitionWait(OMX_StateIdle, pContext);
1096	OMX_TEST_BAIL_IF_ERROR(eError);
1097	/* Transition to Executing state */
1098	eError = SampleTest_TransitionWait(OMX_StateExecuting, pContext);
1099	OMX_TEST_BAIL_IF_ERROR(eError);
1100	printf("\nAbout to start processing buffers\n");
1101
1102
1103	/*  process buffers   */
1104
1105	while (pContext->nBufDoneCalls < OMX_TEST_BUFFERS_OF_TRAFFIC)
1106	{
1107
1108		eError = SampleTest_WriteInBuffers(pContext);
1109		OMX_TEST_BAIL_IF_ERROR(eError);
1110
1111		eError = SampleTest_ReadOutBuffers(pContext);
1112		OMX_TEST_BAIL_IF_ERROR(eError);
1113		while_pass++;
1114		//printf("\n WHILE PROCESS LOOP COUNT:  BufDoneCount = %d %d",
1115		//    while_pass, pContext->nBufDoneCalls);
1116
1117		//printf("\n Waiting on Sample component to respond");
1118
1119	}
1120
1121
1122	gTest = 0;
1123
1124	/* Transition back to Idle state  */
1125	eError = SampleTest_TransitionWait(OMX_StateIdle, pContext);
1126	OMX_TEST_BAIL_IF_ERROR(eError);
1127
1128	/* Trasnition back to Loaded state */
1129	eError = SampleTest_TransitionWait(OMX_StateLoaded, pContext);
1130	OMX_TEST_BAIL_IF_ERROR(eError);
1131
1132
1133      OMX_TEST_BAIL:
1134
1135	fclose(pContext->pInputfile);
1136	fclose(pContext->pOutputFile);
1137
1138	if (eError == OMX_ErrorNone)
1139	{
1140		eError = OMX_FreeHandle(pContext->hComp);
1141		eError = OMX_Deinit();
1142	} else
1143	{
1144		SampleTest_TransitionWait(OMX_StateInvalid, pContext);
1145		SampleTest_DeInitBuffers(pContext);
1146
1147		OMX_FreeHandle(pContext->hComp);
1148		OMX_Deinit();
1149	}
1150
1151	TIMM_OSAL_SemaphoreDelete(pContext->hStateSetEvent);
1152	TIMM_OSAL_SemaphoreDelete(pContext->hPortDisableEvent);
1153
1154	/* emit the Test Result */
1155	if (OMX_ErrorNone != eError)
1156	{
1157		printf(" TestCase Failed and returned an error:: %s \n",
1158		    OMX_TEST_ErrorToString(eError));
1159	} else
1160	{
1161		printf
1162		    ("\nTest case has ended, now comparing input and output files\n");
1163		pContext->pInputfile = fopen(INPUT_FILE, "rb");
1164		if (NULL == pContext->pInputfile)
1165		{
1166			printf("\n Error in opening INPUT_FILE");
1167			eError = OMX_ErrorInsufficientResources;
1168		}
1169
1170		pContext->pOutputFile = fopen(NON_TUN_OUTPUT_FILE, "rb");
1171		if (NULL == pContext->pOutputFile)
1172		{
1173			printf("\n Error in opening NON_TUN_OUTPUT_FILE");
1174			eError = OMX_ErrorInsufficientResources;
1175		}
1176		pass = 1;
1177		while (1)
1178		{
1179			if (eError != OMX_ErrorNone)
1180			{
1181				pass = 0;
1182				break;
1183			}
1184			ch1 = fgetc(pContext->pInputfile);
1185			ch2 = fgetc(pContext->pOutputFile);
1186			loc_diff++;
1187			if (ch1 == EOF || ch2 == EOF)
1188			{
1189				break;
1190			}
1191			if (ch1 != ch2)
1192			{
1193				pass = 0;
1194				printf("\n \n");
1195				printf
1196				    ("\n FILE SIZE = 320 KB - NUMBER OF ITERATIONS = 20 (10 for INPUT AND 10 FOR OUTPUT buffers\n");
1197				printf
1198				    ("\n ################### LOCATION OF DIFFERENCE: %d ################### \n",
1199				    loc_diff);
1200				break;
1201			}
1202		}
1203		fclose(pContext->pInputfile);
1204		fclose(pContext->pOutputFile);
1205		if (pass == 1)
1206			printf(" Test Case has Passed\n");
1207		else
1208		{
1209			printf("\nTest case has failed. (EOF not reached)\n");
1210			eError = OMX_ErrorUndefined;
1211		}
1212	}
1213
1214	if (eError == OMX_ErrorNone)
1215	{
1216		printf(" Test Case has Passed :) \n");
1217	} else
1218	{
1219		printf("\nTest case has failed.(OMX Error)\n");
1220	}
1221}
1222
1223