/*****************************************************************************

Copyright (c) 2006 Analog Devices, Inc.	All	Rights Reserved. 

This software is proprietary and confidential to Analog	Devices, Inc. 
and	its	licensors.

*****************************************************************************

$RCSfile: JPEGImageEdgeDetection_System.h,v $
$Revision: 1.4 $
$Date: 2007/11/15 04:00:56 $

Project:	BlackfinSDK (JPEG Image Edge Detection)
Title:		System Services defines for JPEG Image Edge Detection
Author(s):	bmk
Revised	by:	

Description:
			Defines, Data structures & Functions used for Edge Detection
			on JPEG image

References:
			None

*****************************************************************************
Tab	Setting:			4

Target Processor:		ADSP-BF5xx
Target Tools Revision:	ADSP VisualDSP++ v4.5
******************************************************************************

Modification History:
====================
$Log: JPEGImageEdgeDetection_System.h,v $
Revision 1.4  2007/11/15 04:00:56  randreol
Workaround to resolve interrupt conflict with new USB driver.  Push button Interrupt level is modified by USB driver.

Revision 1.3  2007/10/17 06:52:37  randreol
Port to VDSP 5.0

Revision 1.2  2007/05/23 02:29:18  randreol
Changes in ssl_init and VDSP5.0 porting

Revision 1.4  2006/11/03 07:11:17  bmk
SDK 2.0  files - Initial Entry

*****************************************************************************/

#ifndef	__JPEGIMAGEEDGEDETECTION_SYSTEM_H__
#define	__JPEGIMAGEEDGEDETECTION_SYSTEM_H__

/*********************************************************************
Includes
*********************************************************************/

#include <services/services.h>			// system services include
#include <drivers/adi_dev.h>			// device manager include
#include <SDK-ezkitutilities.h>			// ezkit utilities
#include <adi_ssl_Init.h>
#include <JPEGImageEdgeDetection.h>		// JPEG Image Edge Detection includes

/*********************************************************************
Typedefs & defines
*********************************************************************/

#define DO_CYCLE_COUNTS 		// enables cycle measurements

/******************************
Defines for ITU 656 Video Frame
*******************************/

// Size	of an in/out video frame buffer (maximum size)
#define		VIDEO_BUF_SIZE				PAL_VIDEO_FRAME_SIZE

// YUV buffer size is set to decode files upto SVGA resolution (which includes padding)
#define		MAX_JPEG_IMAGE_WIDTH		800
#define		MAX_JPEG_IMAGE_HEIGHT		608

// Number of YUV Buffers used (JPEG needs only 1)
#define		NUM_YUV_BUFS				1
// size of a single YUV Buffer
// size to support only selected ITU656 frame & YUV400/YUV420/YUV422 image format
#define		SINGLE_YUV_BUF_SIZE			(MAX_JPEG_IMAGE_WIDTH * MAX_JPEG_IMAGE_HEIGHT * 2)
// YUV buffer size (includes all YUV buffers used in this app)
#define		YUV_BUF_SIZE				(SINGLE_YUV_BUF_SIZE * NUM_YUV_BUFS)

// Number of 2D video out buffer(s) used
#define		NUM_VIDEO_OUT_BUFS			1

// Delay (in milli seconds) to allow video codecs to synchronize
#define		VIDEO_CODEC_SYNC_DELAY		500

// MDMA channels for YUV <-> video (ITU656) frame conversion
#if defined(__ADSPBF561__)		// for BF561
// MDMA 1 in DMA controller 1
#define	YUV_ITU656_MDMA_SRC_CHANNEL		ADI_DMA_MDMA1_S1
#define	YUV_ITU656_MDMA_DEST_CHANNEL	ADI_DMA_MDMA1_D1
#elif defined(__ADSPBF533__)	// for BF533
// MDMA in BF533 DMA controller
#define	YUV_ITU656_MDMA_SRC_CHANNEL		ADI_DMA_MDMA_S1
#define	YUV_ITU656_MDMA_DEST_CHANNEL	ADI_DMA_MDMA_D1
#endif

// Processor DMA bus size
#if defined(__ADSPBF561__)
// 4 bytes (PPI uses DMA0 & BF561 DMA0 bus size is 32bits)
#define		DMA_BUS_SIZE				4
#elif defined(__ADSPBF533__)
// 2 bytes (PPI uses DMA & BF533 DMA bus size is 16bits)
#define		DMA_BUS_SIZE				2
#endif

/*********************************************************************
Ez-Kit Push Buttons
*********************************************************************/
#if defined(__ADSPBF561__)
// Ez-Kit Push Button to switch display between Original & Edge Detected image
#define		TOGGLE_DISPLAY_BUTTON_ID		"SW6"	// SW6 for BF561
#define		TOGGLE_DISPLAY_BUTTON			ADI_FLAG_PF5
// Ez-Kit push button to terminate this app
#elif defined(__ADSPBF533__)
// Ez-Kit Push Button to switch display between Original & Edge Detected image
#define		TOGGLE_DISPLAY_BUTTON_ID		"SW4"	// SW4 for BF533
#define		TOGGLE_DISPLAY_BUTTON			ADI_FLAG_PF8
#endif

/****************************************************
JPEG-MJPEG file paths on host 
****************************************************/
// note that these paths are relative to the location of the host application
// path to where JPEG images are installed
#define		ROOT_DIR 					"..\\"
// jpeg file(s) location
#define     IMAGE_DIR_JPEG	        	"Media\\JPEG\\"	
// list of jpeg files to decode
#define     JPEG_LIST_FILE          	"jpeg_list.txt"
// above 'txt' files location
#define     SPEC_DIR		        	"..\\Multimedia\\JPEGImageEdgeDetection\\"

#define     MAX_FILE_NAME_LEN					100
#define     MAX_PATH_NAME_LEN					300

#define		PROMPT_FOR_FILE_SETTINGS			1
#define		GET_FILE_SETTINGS_FROM_TXT_FILE		2

/******************************************************
Device (Codec) parameters
******************************************************/

enum						// supported coding algorithm(s)
{
	CODING_ALGORITHM_JPEG,	// Jpeg
};

// ITU656 Output video modem supported by this application
enum
{
    ITU656_NTSC,		// NTSC Interlaced Video
    ITU656_PAL,			// PAL Interlaced Video
};

/************
Globals
************/


// Video parameters
// Video data per line (bytes)
extern u32	DataPerLine;
// Number of lines per ITU656 video frame (bytes)
extern u32	FrameLines;
// Number of Active lines per ITU656 video frame (bytes)
extern u32	ActiveFrameLines;
// Number of bytes to skip to reach active video data in a single line
extern u32	ActiveVideoSkip;
// Number of bytes to skip to reach active video frame field1 (odd frame)
extern u32	Field1Skip;
// Number of bytes to skip to reach active video frame field2 (even frame)
extern u32	Field2Skip;
// Selected Video Output mode
extern u8	OutputVideoMode;

// System parameters
// flag to indicate Shared MDMA channel status (YUV to ITU656 conversion & Edge Detection shares a MDMA Stream)
extern volatile	bool	MDMASharedFlag;
// Flag to indicate Sobel Edge Detection status
extern volatile	bool	SobelDone;
// Flag to switch display between original JPEG image (TRUE) and Edge Detected output (FALSE)
extern volatile	bool	DisplayJPEG;
// Flag indicates whether video output buffer needs to be updated(TRUE) or not (FALSE)
extern volatile	bool	UpdateVideoOutBufs;

// coding parameters
extern u8 	Coding_Algorithm;	// coding algorithm
extern u8 	File_Settings;		// JPEG Decoder file settings

// Buffer pointers
// Pointer to buffer containing YUV data of decoded JPEG image 
extern u8 	*pYUVBuf;
// Pointer to buffer containing original JPEG image in ITU656 frame format
extern u8 	*pJPEGImageBuf;
// Pointer to edge detected output buffer of ITU656 frame format
extern u8 	*pEdgeDetectBuf;

/*********************************************************************
Function prototypes
*********************************************************************/

/**** Initialization Functions ****/
void 	InitSystem					(void);			// Initialises EZ kit & system parameters
void 	InstallMemory				(void);			// Initialises & calls Cache/Heap install for JPEG
void 	InstallSystemServices		(void);			// Installs system services
void 	InstallVideoEncoder			(void);			// Installs video encoder (ADV717x)
int 	InstallCodecHeaps			(void);			// Installs Heap


// Initialises Blackfin Flags connected to EZ-kit buttons
void 		InitButton 			(ADI_FLAG_ID FlagID);

/**** JPEG image handling functions ****/
int 	do_JPEG_Decode				(void);			// File Handling for JPEG decoding


/**** USB I/O functions ****/
u32		PerformUSBIO				(void);			// Performs USB I/O
u32		SetCodecParameters 				(				// Set Device parameters in relevance to selected user option
		u32 							*UserOptions	// pointer to array of user selected options
);	

/**** Functions to handle YUV to ITU656 conversion ****/
void 	SetupMDMA_YUVtoITU656 		(void);			// Initialises Descriptors for MDMA
void	SetupMDMA_YUV420toITU656 	(				// Configures MDMA Descriptors for YUV420 to ITU656 conversion
		u32							JPEGImageWidth,	// Actual width of JPEG image
		u32							JPEGImageHeigth	// Actual height of JPEG image
);
void 	KickOffMDMA_YUVtoITU656		(void);			// Kicks off MDMA for YUV to ITU656 frame conversion
void 	FreeMDMA_YUVtoITU656		(void);			// Frees MDMA channels used for YUV to ITU656 conversion

/**** Functions to handle Video Buffers ****/
void	SubmitVideoOutBufs			(				// Prepares & submits Video Output buffers
		u8							NumBufs			// number of video out buffers to be submitted
);

/**** Functions to handle MDMA ****/
void	ConfigureMDMA				(void);			// Configures MDMA for sobel edge detection

/**** Sobel Edge Detection Functions ****/
void 	KickOffEdgeDetection 		(void);			// Initialises & starts (Sobel) Edge Detection

/**** Service routines for System service Callbacks ****/
void 	VideoOut					(				// Video Out callback service routine
		void 						*AppHandle, 	// Application Handle
		u32  						Event,			// Event that generated callback
		void 						*pArg			// client handle
);

void 	USB_Callback					(				// Net2272 callback service routine
		void 							*AppHandle, 	// Application Handle
		u32  							Event,			// Event that generated callback
		void 							*pArg			// client handle
);

/**** Termination functions ****/
void	CloseMDMA_Streams			(void);			// Closes MDMA streams used for Edge Detection
void 	FreeMDMA_YUVtoITU656		(void);			// Frees MDMA channels used for YUV to ITU656 conversion

#endif // __JPEGIMAGEEDGEDETECTION_SYSTEM_H__

/*****/
