/*****************************************************************************

Copyright (c) 2006 Analog Devices, Inc.	All	Rights Reserved. 

This software is proprietary and confidential to Analog	Devices, Inc. 
and	its	licensors.

*****************************************************************************

$RCSfile: VideoInEdgeDetection_Callbacks.c,v $
$Revision: 1.2 $
$Date: 2007/11/13 00:48:09 $

Project:	BlackfinSDK (Edge Detection on ITU656 Video In)
Title:		System Service callback routines
Author(s):	bmk
Revised	by:	

Description:
			PPI/Pushbutton callback service routines for Edge Detection 
			on ITU 656 Video Input

References:
			None

*****************************************************************************
Tab	Setting:			4

Target Processor:		ADSP-BF5xx
Target Tools Revision:	ADSP VisualDSP++ v4.5
******************************************************************************

Modification History:
====================
$Log: VideoInEdgeDetection_Callbacks.c,v $
Revision 1.2  2007/11/13 00:48:09  randreol
Changed Deferred callback

Revision 1.1  2007/05/22 18:38:06  gstephan
Creating VDSP 5.00 release folder.  Created from the head copy of
VDSP 4.50 release folder.

Revision 1.4  2006/07/28 05:11:05  bmk
modified code for actual NTSC resolution
Previous rev- 720 x 480, now - 720 x 486

Revision 1.3  2006/07/06 07:26:28  bmk
modified project to use video utility file macros

Revision 1.2  2006/07/04 05:33:19  bmk
added Slide-show mode for BF533

Revision 1.1  2006/06/30 01:33:55  bmk
Initial entry


******************************************************************************
Include files
*****************************************************************************/

#include <VideoInEdgeDetection_System.h>	// Video In Edge Detection system includes
#include <drivers\encoder\adi_adv717x.h>	// video encoder driver includes
#include <drivers\decoder\adi_adv7183.h>	// video decoder driver includes
#include <SDK-ezkitutilities.h>				// EZ-Kit utility definitions

/*********************************************************************
Local function prototypes
*********************************************************************/
void 	UpdateBufferIDs (void);			// Updates Video In/Out buffer IDs

/*********************************************************************
Globals
**********************************************************************
Video parameters
*****************/
// Video data per line (bytes)
u32	DataPerLine;
// Number of lines per ITU656 video frame (bytes)
u32	FrameLines;
// Number of bytes to skip to reach active video data in a single line
u32	ActiveVideoSkip;
// Number of bytes to skip to reach active video frame field1 (odd frame)
u32	Field1Skip;
// Number of bytes to skip to reach active video frame field2 (even frame)
u32	Field2Skip;
// Sobel buffer row size (for valid Edge Detected data only)
u16	SobelRowSize;
// ITU656 video frame - Active field spilt to # number of blocks
u8	SobelFieldBlocks;
// Number of Sobel Blocks for selected video frame
u16	SobelBlockCount;
// System flags for Push Button Trigger status
PB_TRIGGER_FLAGS	PBFlag;
// pointer to paused video 2D buffer
volatile u32		*pPausedBuffer;

#if defined(__ADSPBF533__)
#if defined(VIDEO_IN_SLIDE_SHOW)		// Conditional compilation macro to enable slide show mode
u32		SlideShowFrameCount;		// count to check frame capture frequency
#endif
#endif
/*********************************************************************

    Function:       VideoOut

    Description:    Video Out callback service routine

*********************************************************************/
section ("Callback_Code_L1")
void VideoOut(
	void *AppHandle,
	u32  Event,
	void *pArg
){            
    // Case of (event type)
    switch (Event) {
        
        // Case (buffer processed)
        case ADI_DEV_EVENT_BUFFER_PROCESSED:

#if defined(__ADSPBF533__)
#if defined(VIDEO_IN_SLIDE_SHOW)	// Conditional compilation macro to enable slide show mode
		++SlideShowFrameCount;	// increment slideshow count
		// check if its time to capture a new frame
		if (SlideShowFrameCount >= FRAME_CAPTURE_FREQUENCY)
		{
	        // Set Frame capture flag
    	    SystemFlag.Pause_Frame_Capture	= TRUE;
        	// set flag to update buffers
        	SystemFlag.UpdateVideoOutBufs 	= TRUE;
		}
#endif   
#endif			
        	// check UpdateVideoOutBufs flag or UpdateVideoMode flag is set
        	if (SystemFlag.UpdateVideoOutBufs)
        	{
				// BF561 - Toggle video or Pause video button was pressed or update video mode
				// BF533 - Frame capture / toggle video button was pressed
				// update the video in/out buffer id's
				UpdateBufferIDs();
			}

            break;	

        // Case (DMA Error)
        case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:

         	// turn on all LEDs and wait for help
            ezTurnOnAllLEDs();
            while (1) ;
                      
		default:	// other events
         	// turn on all LEDs and wait for help
            ezTurnOnAllLEDs();
            while (1) ;
    }
}

/*********************************************************************

    Function:       VideoIn

    Description:    Video In callback service routine

*********************************************************************/
section ("Callback_Code_L1")
void VideoIn(
	void *AppHandle,
	u32  Event,
	void *pArg
){
  				
    // Case of (event type)
    switch (Event) {
               
        // Case (buffer processed)
        case ADI_DEV_EVENT_BUFFER_PROCESSED:	// a video frame has been captured
                               
#if defined(__ADSPBF561__)

			// check if UpdateBufs_VideoIn falg is set
			if (SystemFlag.UpdateVideoInBufs)
			{			    
			    // if so, user has pressed Pause Video button and pause is enabled this time
				// retrive the address of present video in buffer which is selected to pause
				pPausedBuffer = (u32 *)pArg;
				// update the video in/out buffer id's
				UpdateBufferIDs();
			}

			// check if sobel video out is enabled (for edge detected video streaming)
			if (SystemFlag.SobelEnable)
			{
			   	// check if Sobel buffers are ready to take next frame
				if (SystemFlag.SobelDone)
				{
					// Initialise Video In to Sobel In data streaming
			   		// address of video in buffer that generated callback is passed as pArg
					SobelInInit ((u32 *)pArg);
					// clear the flag to start sobel conversion for next frame
					SystemFlag.SobelDone	= FALSE;					  
				}
			}
									
#elif defined(__ADSPBF533__)

			// retrive the address of present video in buffer that has the captured frame
			pPausedBuffer = (u32 *)pArg;

			// BF533 - A Video frame has been captured
			// Disable Video In dataflow
			EnableVideoIn (FALSE);
			// Close ADV7183
			ezErrorCheck(adi_dev_Close(ADV7183DeviceHandle));
	    	// copy captured video buf field to avoid interleave problem
	    	if (SystemFlag.ITU656_Mode == ITU656_NTSC)
	    	{
	        	// Incoming video stream is NTSC
		        // Arguments - pointer to video frame start location, frame type, field source, active data only
		   		adi_video_CopyField((char *)(((ADI_DEV_BUFFER *)pPausedBuffer)->TwoD.Data),	NTSC_IL,2, TRUE);
	    	}
	    	else if (SystemFlag.ITU656_Mode == ITU656_PAL)
	    	{
	    		// Incoming video stream is PAL
				// Arguments - pointer to video frame start location, frame type, field source, active data only
	   			adi_video_CopyField((char *)(((ADI_DEV_BUFFER *)pPausedBuffer)->TwoD.Data),	PAL_IL, 2, TRUE);
	    	}
	   		// Initialise Sobel edge detection for the new frame
			SobelInInit ((u32 *) pPausedBuffer);
	    	// clear the flag to indicate a new frame is available for edge detection
			SystemFlag.SobelDone	= FALSE;
			// enable video encoder (ADV7171) in BF533 Ez-kit
			ezEnableVideoEncoder ();
			// Instal Video Encoder (video output - ADV717x)
			InstallVideoEncoder ();	
			// Enable Video Out dataflow
			EnableVideoOut (TRUE);
			
#endif

            break;

        // Case DMA Error
        case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:

         	// turn on all LEDs and wait for help
            ezTurnOnAllLEDs();
            while (1) ;
                      
		default:	// other events
         	// turn on all LEDs and wait for help
            ezTurnOnAllLEDs();
            while (1) ;
    }
}

/*********************************************************************

    Function:       ButtonPressed

    Description:    Ez-Kit button press callback service routine

*********************************************************************/
section ("Callback_Code_L1")
void ButtonPressed	(	
	void *AppHandle,
	u32  Event,
	void *pArg
){
    int i;
    
     // Case of (event type)
    switch (Event) {
        
       // Case (flag interrupt occurred)
        case ADI_FLAG_EVENT_CALLBACK:
			       	
        	// check if terminate application button was been pressed
        	if ((int)pArg == TERMINATE_BUTTON)
        	{
        	    // if so, clear the KeepRunningFlag
        	    SystemFlag.KeepRunningFlag		= FALSE;
        	}
#if defined(__ADSPBF561__)
			// check for Update Video Mode button press
			// Video update will start only after this button gets released
       		else if ((int)pArg == UPDATE_VIDEO_MODE_BUTTON)
        	{
       			// Check Pause Button system status flag
				if (PBFlag.Video_Mode)
				{
			       	// if the flag is set, then the button was previously pressed & just released
			       	// Update Video Mode button has been pressed & released. change trigger status to wait for next button press
			       	ezErrorCheck(adi_flag_SetTrigger(UPDATE_VIDEO_MODE_BUTTON,ADI_FLAG_TRIGGER_RISING_EDGE));
			       	// set the Update Video Mode button system flag to indicate the button has been released
			       	PBFlag.Video_Mode	= FALSE;
			       		
					// ignore multiple button press as only one button should be serviced at a time
        			// check Update flags and even if one of them is set, ignore this button press
					if (!(SystemFlag.UpdateVideoInBufs || SystemFlag.UpdateVideoOutBufs || SystemFlag.UpdateVideoMode))
					{
				      	// set flag to update video mode   	       	    
    	    	   		SystemFlag.UpdateVideoMode 		= TRUE;
    	    	   		// update video mode in video out callback
    	    	   		SystemFlag.UpdateVideoOutBufs 	= TRUE;
					}
			   	}
			   	else	// Update Video Mode button is in pressed state
			   	{
				   	// Update Video Mode button has been pressed. change trigger status to wait until its released
				    ezErrorCheck(adi_flag_SetTrigger(UPDATE_VIDEO_MODE_BUTTON,ADI_FLAG_TRIGGER_FALLING_EDGE));
				    // set the Update Video Mode button system flag to indicate the button has been pressed
				    PBFlag.Video_Mode	= TRUE;
			   	}
        	}

	      	// check for Pause Video button press
    	   	else if ((int)pArg == PAUSE_FRAME_CAPTURE_BUTTON)
       		{
       		    // ignore multiple button press as only one button should be serviced at a time
        		// check Update flags and even if one of them is set, ignore this button press
				if (!(SystemFlag.UpdateVideoInBufs || SystemFlag.UpdateVideoOutBufs || SystemFlag.UpdateVideoMode))
				{
						    
        			SystemFlag.Pause_Frame_Capture ^= 1;	// toggle pause video flag
        	           	    
        			// check pause flag
        			if (SystemFlag.Pause_Frame_Capture == TRUE)
        	 		{
        	   			// if this is a pause enable operation, buffer update should be done only in Video In Callback
	        			SystemFlag.UpdateVideoInBufs	= TRUE;
		   			}        	    
        			else
	        		{
		        		// this is to enable video stream(disable pause), can be done only in Video Out callback
    		   		   	// because video in will be off during pause enable mode
        				SystemFlag.UpdateVideoOutBufs = TRUE;
        		    }        		    
			   	}
       		}
#elif defined(__ADSPBF533__)
      		// check for Frame Capture button press
      		// Frame Capture will start only after this button gets released
       		else if ((int)pArg == PAUSE_FRAME_CAPTURE_BUTTON)
       		{
       			// Check Frame Capture Button system status flag
				if (PBFlag.Frame_Capture)
				{
					// if the flag is set, then the button was previously pressed & just released
					// Frame capture button has been pressed & released. change trigger status to wait for next button press
					ezErrorCheck(adi_flag_SetTrigger(PAUSE_FRAME_CAPTURE_BUTTON,ADI_FLAG_TRIGGER_RISING_EDGE));
					// set the Pause button system flag to indicate the button has been released
			    	PBFlag.Frame_Capture	= FALSE;
			    
       		    	// ignore multiple button press as only one button should be serviced at a time
        			// check Update flags and even if one of them is set, ignore this button press
					if (!(SystemFlag.UpdateVideoInBufs || SystemFlag.UpdateVideoOutBufs || SystemFlag.UpdateVideoMode))
					{
	        	    	// Set Frame capture flag
    	    			SystemFlag.Pause_Frame_Capture	= TRUE;
        				// BF533 - video out callback will be active almost 100% of the time
        				SystemFlag.UpdateVideoOutBufs 	= TRUE;
			   		}
				}
				else	// Frame Capture button is in pressed state
			   	{
				   	// Frame Capture button has been pressed. change trigger status to wait until its released
				    ezErrorCheck(adi_flag_SetTrigger(PAUSE_FRAME_CAPTURE_BUTTON,ADI_FLAG_TRIGGER_FALLING_EDGE));
				    // set the Frame Capture button system flag to indicate the button has been pressed
				    PBFlag.Frame_Capture	= TRUE;
			   	}
       		}
#endif			   	
      		// check if Toggle display button was pressed
    		else if ((int)pArg == TOGGLE_DISPLAY_BUTTON)
       		{
       		    
       			// Check Toggle Display Button system status flag
				if (PBFlag.Toggle_Display == TRUE)
				{
					// if the flag is set, then the button was previously pressed & just released
					// Toggle Display button has been pressed & released. change trigger status to wait for next button press
					ezErrorCheck(adi_flag_SetTrigger(TOGGLE_DISPLAY_BUTTON,ADI_FLAG_TRIGGER_RISING_EDGE));
					// set the Toggle Display button system flag to indicate the button has been released
			    	PBFlag.Toggle_Display	= FALSE;
			    
       		    	// ignore multiple button press as only one button should be serviced at a time
        			// check Update flags and even if one of them is set, ignore this button press
					if (!(SystemFlag.UpdateVideoInBufs || SystemFlag.UpdateVideoOutBufs || SystemFlag.UpdateVideoMode))
					{   
			         	// toggle sobel enable flag
   						SystemFlag.SobelEnable 	^= 	1;
						// set UpdateBufs_VideoOut to update video buffers Video Out callback 
						// Video Out callback will be active in all modes (pause enable/disable mode or toggle video mode)
						SystemFlag.UpdateVideoOutBufs = TRUE;
			  		}
				}
				else	// Toggle Display button is in pressed state
			   	{
				   	// Toggle Display button has been pressed. change trigger status to wait until its released
				    ezErrorCheck(adi_flag_SetTrigger(TOGGLE_DISPLAY_BUTTON,ADI_FLAG_TRIGGER_FALLING_EDGE));
				    // set the Toggle Display button system flag to indicate the button has been pressed
				    PBFlag.Toggle_Display	= TRUE;
			   	}       		    
       		}       		
			break;
			
		default:	// other events
         	// turn on all LEDs and wait for help
            ezTurnOnAllLEDs();
            while (1) ;
    }
}

/*********************************************************************

    Function:       UpdateVideoMode
    Description:    Switches between PAL and NTSC on-the-fly
    				Reads ADV7183 Status1 Reg

	Status1 Reg:
	bit 0 => ADV7183 is in Lock (1) with video / lost(0)
	bit 1 => ADV7183 have lost sync (1) since previous read
	bits 6,5,4 => Autodetection Result (value 0,1=NTSC / 2,3,4,6=PAL)	

*********************************************************************/
section("sdram_bank1")
void	UpdateVideoMode()
{
    u32 Result;
	ADI_DEV_ACCESS_REGISTER		Status1;		// read ADV7183 Status1 reg
        
    Status1.Address		= ADV7183_STATUS1_RO;	// Status1 reg address
    Status1.Data		= 0;					// clear the location where the data will be read to
    
// call Device Access to read Status1 reg
	Result = adi_dev_Control(ADV7183DeviceHandle, ADI_DEV_CMD_REGISTER_READ, (void *) &Status1);
	if (Result !=0)
	{
		printf("Result %x\n",Result);
		ezErrorCheck(Result);
	}

	// check if ADV7183 have lost lock
	if (!(Status1.Data & 1))
	{
	    // if so, update ITU656 mode as no video input available
	    SystemFlag.ITU656_Mode 			= NO_VIDEO_INPUT;
	    // don't update buffers if No video input is found
	}
	// check if ADV7183 have lost sync since previous reg read	
	else if (Status1.Data & 2)
	{
	    // if so, then update ITU656 video mode	    
	    switch ((Status1.Data >> 4) & 0x07)	// retrive ITU656 video mode
	    {
	        // case (NTSC video)
	        case 0:
	        case 1:	        
				// update ITU656 mode NTSC
	    		SystemFlag.ITU656_Mode 		= ITU656_NTSC;
	    		// New video mode detected. Set flag to Update video mode
	    		SystemFlag.UpdateVideoFlag	= TRUE;
	    		break;
	    	
	    	// case (PAL video)
	    	case 2:
	    	case 3:
	    	case 4:
	    	case 6:
				// update ITU656 mode NTSC
	    		SystemFlag.ITU656_Mode 		= ITU656_PAL;
	    		// New video mode detected. Set flag to Update buffers
	    		SystemFlag.UpdateVideoFlag	= TRUE;
	    		break;
			
	    	// other video modes (SECAM) not supported yet
	    	default:
	    		SystemFlag.ITU656_Mode 	= NO_VIDEO_INPUT;
	    		// don't update buffers if No video input is found
	    		break;
	    }        
	}
	else	// Re-sync the present video mode
		SystemFlag.UpdateVideoFlag	= TRUE;
		

	// update video parameters
	// Check video mode
	if (SystemFlag.ITU656_Mode == ITU656_PAL)
	{
		// video mode is ITU656 PAL
	   	// Video data per line (bytes)
		DataPerLine		= PAL_DATA_PER_LINE;
		// Number of lines per ITU656 video frame (bytes)
		FrameLines		= PAL_FRAME_LINES;
		// Number of bytes to skip to reach active video data in a single line
		ActiveVideoSkip	= PAL_ACTIVE_VIDEO_SKIP;
		// Number of bytes to skip to reach active video frame field1 (odd frame)
		Field1Skip		= PAL_F1_SKIP;
		// Number of bytes to skip to reach active video frame field2 (even frame)
		Field2Skip		= PAL_F2_SKIP;
		// Sobel buffer row size (for valid Edge Detected data only)
		SobelRowSize	= (PAL_ACTIVE_FLINES / SOBEL_FIELD_BLOCKS_PAL);	
		// PAL video frame - Active field spilt to # number of blocks
		SobelFieldBlocks= SOBEL_FIELD_BLOCKS_PAL;		
		// Number of Sobel blocks per active field (for NTSC)
		SobelBlockCount	= SOBEL_BLOCK_COUNT_PAL;			
	}
	else	// load NTSC values for for no video mode or NTSC video mode
	{
	   	// Video mode is ITU656 NTSC or no video input
	   	// Video data per line (bytes)
		DataPerLine		= NTSC_DATA_PER_LINE;
		// Number of lines per ITU656 video frame (bytes)
		FrameLines		= NTSC_FRAME_LINES;
		// Number of bytes to skip to reach active video data in a single line
		ActiveVideoSkip	= NTSC_ACTIVE_VIDEO_SKIP;
		// Number of bytes to skip to reach active video frame field1 (odd frame)
		Field1Skip		= NTSC_F1_SKIP;
		// Number of bytes to skip to reach active video frame field2 (even frame)
		Field2Skip		= NTSC_F2_SKIP;
		// Sobel buffer row size (for valid Edge Detected data only)
		SobelRowSize	= (NTSC_ACTIVE_FLINES / SOBEL_FIELD_BLOCKS_NTSC);
		// NTSC video frame - Active field spilt to # number of blocks
		SobelFieldBlocks= SOBEL_FIELD_BLOCKS_NTSC;
		// Number of Sobel blocks per active field (for NTSC)
		SobelBlockCount	= SOBEL_BLOCK_COUNT_NTSC;
	}
   	
	return;
}

/*********************************************************************

	Function:		UpdateBufferIDs

	Description:	Updates Video In/Out buffers IDs 

*********************************************************************/
section("App_Code_L1")
void UpdateBufferIDs (void)
{	
#if defined(__ADSPBF561__)

	// check Update Video Mode flag
	if (SystemFlag.UpdateVideoMode	== TRUE)
	{
	    // Update video mode button has been pressed.
	    UpdateVideoMode();	// check the video mode	    
	    // check update video flag
	    if (!SystemFlag.UpdateVideoFlag)
	    {
	        // no need to update video mode
	        SystemFlag.UpdateVideoMode	= FALSE;
	        return;
	    }
	}

	// Disable Video In dataflow
	EnableVideoIn (FALSE);		  
	// Disable Video Out dataflow
	EnableVideoOut (FALSE);	
	// Close ADV717x PPI device
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_PPI_STATUS, (void *) ADI_ADV717x_PPI_CLOSE));

	// check if new video mode was detected
	if (SystemFlag.UpdateVideoFlag	== TRUE)
	{
	    // if so, reset the whole system to new video mode
		// Close MDMA streams
    	CloseMDMA_Streams ();	    
		// start video streaming with new video mode
		SystemFlag.Pause_Frame_Capture 	= FALSE;
		// clear UpdateVideo flag
		SystemFlag.UpdateVideoFlag		= FALSE;
	    // Configure and Install MDMA for Sobel Edge Detection with new video mode
		ConfigureMDMA();
		// Update ADV717x Operating video mode
		UpdateADV717xVideoMode ();
	    // Format the Video Out frame to specified video mode
		FormatVideoOutFrames();		
	}

	// check Pause flag status
	if (SystemFlag.Pause_Frame_Capture == FALSE)	
	{
	    // Pause is disabled. So Enable Video streaming	    
		// Close ADV7183 to submit new buffers
		ezErrorCheck(adi_dev_Close(ADV7183DeviceHandle));	    
		// install ADV7183 device
		InstallVideoDecoder();
		// Enable Video In dataflow
		EnableVideoIn (TRUE);
		// Clear field copy flag as Pause is disabled
		SystemFlag.FieldcopyDone = FALSE;
	}
	// check if pause is enabled and Field copy is already done
	else if (SystemFlag.Pause_Frame_Capture && (!SystemFlag.FieldcopyDone))
	{	
	    // Pause enabled. copy paused video in buf field to avoid interleave problem
	    if (SystemFlag.ITU656_Mode == ITU656_NTSC)
	    {
	        // Incoming video stream is NTSC
	        // Arguments - pointer to video frame start location, frame type, field source, active data only
	   		adi_video_CopyField((char *)(((ADI_DEV_BUFFER *)pPausedBuffer)->TwoD.Data),	NTSC_IL,2, TRUE);
	    }
	    else if (SystemFlag.ITU656_Mode == ITU656_PAL)
	    {
	    	// Incoming video stream is PAL
			// Arguments - pointer to video frame start location, frame type, field source, active data only
	   		adi_video_CopyField((char *)(((ADI_DEV_BUFFER *)pPausedBuffer)->TwoD.Data),	PAL_IL, 2, TRUE);
	    }
	    // Mark as Field copy is already done
	    SystemFlag.FieldcopyDone = TRUE;
	    // Initialise Sobel edge detection for the paused frame
		SobelInInit ((u32 *) pPausedBuffer);
	    // clear the flag as new frame is ready for edge detection
		SystemFlag.SobelDone	= FALSE;
	}
	
	// Open ADV717x PPI device
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_ADV717x_CMD_SET_PPI_STATUS, (void *) ADI_ADV717x_PPI_OPEN));
	// Set Dataflow method
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void *) ADI_DEV_MODE_CHAINED_LOOPBACK));
	// Enable streaming
	ezErrorCheck(adi_dev_Control(ADV717xDeviceHandle, ADI_DEV_CMD_SET_STREAMING, (void *) TRUE));
	// submit Video Out buffer(s)
	SubmitVideoOutBuffers();
	// Enable Video Out dataflow
	EnableVideoOut (TRUE);
	
#elif defined(__ADSPBF533__)
	
	// Disable Video Out dataflow
	EnableVideoOut (FALSE);
	// Close ADV717x device (video encoder)
	ezErrorCheck(adi_dev_Close(ADV717xDeviceHandle));
		
	// Check if Frame capture button is pressed
	if (SystemFlag.Pause_Frame_Capture == TRUE)
	{
	    // Close MDMA streams
    	CloseMDMA_Streams ();
		// Enable Video decoder (ADV7183) in BF533 Ez-Kit
		ezEnableVideoDecoder();
		// delay to stabilise 7183
    	ezDelay(ADV7183_DELAY);
		// Install video decoder (ADV7183)
		InstallVideoDecoder();
		// Enable Video In dataflow to kick off frame capture
		EnableVideoIn (TRUE);
		// Clear frame capture flag 
		SystemFlag.Pause_Frame_Capture = FALSE;
				
#if defined(VIDEO_IN_SLIDE_SHOW)		// Conditional compilation macro to enable slide show mode
		SlideShowFrameCount = 0;	// clear frame frequency check count
#endif
	
	}
	else	// toggle between sobel/original image
	{
		ezEnableVideoEncoder();// enable the video encoder (7171)

		// Instal Video Encoder (video output - ADV717x)
		InstallVideoEncoder ();
		// Enable Video Out dataflow
		EnableVideoOut (TRUE);
	}

#endif

	SystemFlag.UpdateVideoMode		= FALSE;	// Video mode has been updated
	SystemFlag.UpdateVideoInBufs	= FALSE;	// clear buffer update flag (video in callback)
	SystemFlag.UpdateVideoOutBufs	= FALSE;	// clear buffer update flag (video out callback)
	
	return;
}	

/*****/
