/*****************************************************************************
Copyright(c) 2005 Analog Devices, Inc.  All Rights Reserved. This software is 
proprietary and confidential to Analog Devices, Inc. and its licensors.
******************************************************************************

$RCSfile: mjpeg_encode.c,v $
$Revision: 1.1 $
$Date: 2007/05/22 18:37:53 $

Project:	BlackfinSDK (JPEG-MJPEG)
Title:		MJPEG encode
Author(s):	ku, bmk
Revised by: 

Description:
			High level MJPEG encoding functions

References:
			None

******************************************************************************
Tab Setting:			4

Target Processor:		ADSP-BF5xx
Target Tools Revision:	ADSP VisualDSP++ v4.5
******************************************************************************

Modification History:
====================
$Log: mjpeg_encode.c,v $
Revision 1.1  2007/05/22 18:37:53  gstephan
Creating VDSP 5.00 release folder.  Created from the head copy of
VDSP 4.50 release folder.

Revision 1.7  2006/11/10 07:18:58  bmk
merged BF533 & BF561 apps
Fixed BF561 caches issue

Revision 1.6  2006/11/03 07:12:07  bmk
SDK 2.0  files - Initial Entry

*****************************************************************************/

#include <jpeg_mjpeg_system.h>		// JPEG-MJPEG System includes

#include <adi_usbio_blackfin.h>		// USB I/O functions for Blackfin
#include <string.h>					// string header
#include <math.h>
#include <cycle_count.h> 			// for basic cycle counting

// codec APIs
#include <JPEG_api_encoder.h>
#include <JPEG_BitsBuffer.h>
#include <IMG_common.h>
#include <JPEG_memalloc.h>
#include <MJPEG_AVI_FileWriter.h>

#include <jpeg_mjpeg_err.h>

// encoder settings (valid only for the JPEG encoder in this file) 
// Width, Heigth and Quality Factor can be set through the specification file
static unsigned int			QualityFactor 	 	= 40;		// Input Desired Quality Factor
static unsigned int			Threshold 		 	= 0;
static unsigned int			EncodingMode	 	= SEQUENTIAL;

// local prototype
int MJPEG_encode(char* input_file);

/*********************************************************************

    Function:       do_MJPEG_Encode

    Description:    File Handling for MJPEG encoding

*********************************************************************/
section("sdram0_bank1_nocache")
int do_MJPEG_Encode(void)
{
	char  current_input_file_name[MAX_FILE_NAME_LEN+4];
	static char current_path[MAX_PATH_NAME_LEN];
	static char temp_path[MAX_PATH_NAME_LEN];

	FILE *fp_mjpg_dir;			// file pointer to spec file

	int next_file = 1;	// let next file value not be a null
	char key;

	int return_code = CODEC_SUCCESS;

	printf("Blackfin(r) Multimedia Starter Kit - MJPEG Encoder\n");
	printf("(c) 2006 Analog Devices Inc.\n\n");

	// Reset Video codecs
	ResetVideoCodecs();
		
	if (File_Settings == PROMPT_FOR_FILE_SETTINGS) 
	{
	    printf("Enter directory path to store MJPEG (.avi) file(s): ");
	    fflush(stdout);
	    scanf ("%s", current_path);
	    strcat(current_path,"\\");
	    printf("Name this MJPEG file as: ");
	    fflush(stdout);
	    scanf ("%s", current_input_file_name);
	}   
	else if (File_Settings == GET_FILE_SETTINGS_FROM_TXT_FILE) 
	{
  		// Open MJPEG list-and-specification file
  		strcpy(current_path, SPEC_DIR);  	
 	 	fp_mjpg_dir = fopen( strcat(current_path, MJPEG_SPEC_FILE), "r" );
 		if (fp_mjpg_dir == NULL)
		{
			fprintf( fperr, "Error: could not read specification file\n" );
			return CODEC_FILE_ERROR;
		}
		printf("\nReading list of MJPEG files and parameters from '%s'\n", MJPEG_SPEC_FILE);

		// loop through all the files in the list-file
		// Get first File name from directory file
		next_file = fscanf(fp_mjpg_dir, "%s %d %d %d", current_input_file_name, &JPEGImageWidth, &JPEGImageHeight, &QualityFactor);
	}

	// Format a JPEG image buffer as ITU656 frame
	if (VideoMode == ITU656_PAL)
	{
		// Format as PAL frame
		adi_video_FrameFormat ((char *)pITU656Frames[0],PAL_IL);
		// Format as PAL frame
		adi_video_FrameFormat ((char *)pITU656Frames[1],PAL_IL);
	}			
	else // VideoMode must be ITU656_NTSC
	{
		// Format as NTSC frame
		adi_video_FrameFormat ((char *)pITU656Frames[0],NTSC_IL);
		// Format as NTSC frame
		adi_video_FrameFormat ((char *)pITU656Frames[1],NTSC_IL);		
	}
	
	// continue till end of list or user selects to quit
	while((next_file != EOF) && (next_file != (int)NULL))
	{   
		if (File_Settings == PROMPT_FOR_FILE_SETTINGS) 
		{
		    printf("\n");
	    	printf("Desired Frame Size (Width) : ");
	    	fflush(stdout);
	    	scanf ("%d", &JPEGImageWidth);
	    	printf("Desired Frame Size (Height): ");
	    	fflush(stdout);	    	
	    	scanf ("%d", &JPEGImageHeight);
	    	printf("Desired Quality factor     : ");
	    	fflush(stdout);
	    	scanf ("%d", &QualityFactor);	    	
	    	key = getchar();	// clear the final character (\n) in the input buffer
		}
			    
		// delimit image sizes to valid values
		// minimum image size must be 16 x 16 pixels
		// maximum can be size of input resolution (720 x 486 for NTSC, 720 x 576 for PAL)
		if (JPEGImageWidth < 0x10)
			JPEGImageWidth = 0x10;
		else if (JPEGImageWidth > ITU_PIXEL_PER_LINE)
			JPEGImageWidth = ITU_PIXEL_PER_LINE;

		if (JPEGImageHeight < 0x10)
			JPEGImageHeight = 0x10;
		else if (JPEGImageHeight > ActiveFrameLines)
			JPEGImageHeight = ActiveFrameLines;	

		// Sizes should be in multiples of 16
		JPEGImageWidth &= ~0x0F;
		JPEGImageHeight &= ~0x0F;
		
		// Quality factor should be between 1 and 100
		if (QualityFactor < 1)
			QualityFactor = 1;
		else if (QualityFactor > 100)
			QualityFactor = 100;	

		// clear out video frame (black screen)
		if (VideoMode == ITU656_PAL)
		{
			// Fill the Video out frame with background color
			adi_video_FrameFill ((char *)pITU656Frames[0],PAL_IL,JPEGImageBackground);
			// Fill the Video out frame with background color
			adi_video_FrameFill ((char *)pITU656Frames[1],PAL_IL,JPEGImageBackground);
		}
		else // VideoMode must be ITU656_NTSC
		{
			// Fill the Video out frame with background color
			adi_video_FrameFill ((char *)pITU656Frames[0],NTSC_IL,JPEGImageBackground);
			// Fill the Video out frame with background color
			adi_video_FrameFill ((char *)pITU656Frames[1],NTSC_IL,JPEGImageBackground);
		}

		// Install video decoder (ADV7183)
		InstallVideoDecoder();	    	
  		// Setup MDMA for ITU656 to YUV conversion
 		SetupMDMA_YUV_ITU656();
					
#ifdef ADI_MMSK_EXTRA_INFO		// Extra debug information
		if (File_Settings == GET_FILE_SETTINGS_FROM_TXT_FILE)
		{
			// print actual parameters
			printf("\nImage size to capture   = %d x %d pixels\n", JPEGImageWidth, JPEGImageHeight);
			printf("Encoding Quality Factor = %d \n", QualityFactor);
		}
#endif
		if (File_Settings == PROMPT_FOR_FILE_SETTINGS) 
		{
			// build the next input file name in tempstring
	    	strcpy (temp_path,current_path);
			strcat(temp_path, current_input_file_name);			
		}   
		else if (File_Settings == GET_FILE_SETTINGS_FROM_TXT_FILE) 
		{
			// build the next input file name in tempstring
			strcpy(temp_path, ROOT_DIR);
			strcat(strcat(temp_path, IMAGE_DIR_MJPEG), current_input_file_name);
		}

		// start MJPEG encoding
		if ((return_code = MJPEG_encode(temp_path)) != CODEC_SUCCESS)
		{
			// if it was a file error, try next file in the list
			if (return_code != CODEC_FILE_ERROR)    
				break;			// error! Abort loop
		}
		
		// Wait for Key stroke to go to next file
		printf("\nHit 'Return' key to go to next file or 'q' to quit: ");
		fflush(stdout);
		while (key = getchar())
		{
			if ( (key == 'q') || (key == 'Q') )
				next_file = EOF;
			else if (key == '\n')
				break;
		}

		// check for end of file
		if((next_file != EOF) && (next_file != (int)NULL))
		{
			if (File_Settings == PROMPT_FOR_FILE_SETTINGS) 
			{
		    	printf("\nName this MJPEG file as: ");
		    	fflush(stdout);
	    		scanf ("%s", current_input_file_name);
			}   
			else if (File_Settings == GET_FILE_SETTINGS_FROM_TXT_FILE) 
			{
				// Get Next File name from directory file
				next_file = fscanf(fp_mjpg_dir, "%s %d %d %d", current_input_file_name, &JPEGImageWidth, &JPEGImageHeight, &QualityFactor);				
			}
		}
		
		// free up Video and DMA resources	
		// Free MDMA stream used for ITU656 to YUV conversion
		FreeMDMA_YUV_ITU656 ();		
		// Close Video Decoder (ADV7183)
		ezErrorCheck(adi_dev_Close(ADV7183DeviceHandle));		
	}
	
	if(return_code)
		fprintf(fperr, "!!! MJPEG Encoding Failed !!!\n");
		
	if (File_Settings == GET_FILE_SETTINGS_FROM_TXT_FILE)
	{
		printf("Reached end of list\n");
		// close the list file
		fclose( fp_mjpg_dir );
	}
	
	return CODEC_SUCCESS;
}

/*********************************************************************

    Function:       MJPEG_encode

    Description:    Encodes ITU656 video frames to MJPEG file
    				(by interacting with MJPEG Encode library)

*********************************************************************/	
section("App_Code_L1")
int MJPEG_encode(char* input_file)
{
	tJpegParam		   			lImageEncParam = { 0 };
    MemObjHandle                *StreamBuffer_Obj;
    uint8						*StreamBuffer = NULL;
    uint32 						lStreamFileOutHandle = 0;
    uint32 						lFormatLength = 0;
	tJpegEncoder				*lJpegEnc = 0;
	tMJPEG_AVI_STREAMINFO 		lStreamInfo;
    tMJPEG_AVI_BITMAPINFO  		lBitMapInfo;
    uint32 						lStreamHandle = 0;
	uint8	        			*lInputBuffer;

	int32 				    	lResult = 0;
	uint32						NumBytes = 0;
	static unsigned int			FrameRate;
	unsigned int				lNumFrames = 0;

#ifdef ADI_MMSK_EXTRA_INFO		// macro to enable extra debug information    	
	// cycle counting
	static cycle_t 				start_count;
	static cycle_t 				final_count;

	static cycle_t 				min_frame_cycles;	// decode time
	static cycle_t 				max_frame_cycles;

	static cycle_t 				min_usb_cycles;	// usb read time / frame
	static cycle_t 				max_usb_cycles;
	static unsigned int 		min_usb_bytes;	// bytes / frame read
	static unsigned int 		max_usb_bytes;
	float  						cycle_sum;
#endif
		
	// Configure JPEG encoder
	// set the desired input image width
	lResult = JPEG_Param_CONFIG(&lImageEncParam, JPEG_FRAME_WIDTH, JPEGImageWidth);
	if(lResult != E_SUCCESS)	JPEG_ProcessErrorCode(lResult, "JPEG_Param_CONFIG");
    // set the desired input image height
	lResult = JPEG_Param_CONFIG(&lImageEncParam, JPEG_FRAME_HEIGHT, JPEGImageHeight);
	if(lResult != E_SUCCESS)	JPEG_ProcessErrorCode(lResult, "JPEG_Param_CONFIG");
    // set the desired Quality factor
	lResult = JPEG_Param_CONFIG(&lImageEncParam, JPEG_QUALITYFACTOR, QualityFactor);
	if(lResult != E_SUCCESS)	JPEG_ProcessErrorCode(lResult, "JPEG_Param_CONFIG");
	// set the desired encoding mode
	lResult = JPEG_Param_CONFIG(&lImageEncParam, JPEG_ENCODINGMODE, EncodingMode);
	if(lResult != E_SUCCESS)	JPEG_ProcessErrorCode(lResult, "JPEG_Param_CONFIG");
	// set the desired threshold
	lResult = JPEG_Param_CONFIG(&lImageEncParam, JPEG_THRESHOLD, Threshold);
	if(lResult != E_SUCCESS)	JPEG_ProcessErrorCode(lResult, "JPEG_Param_CONFIG");
	// set the desired image frame format
	lResult = JPEG_Param_CONFIG(&lImageEncParam, JPEG_IMAGEFORMAT, JPEGFrameFormat);
	if(lResult != E_SUCCESS)    JPEG_ProcessErrorCode(lResult, "JPEG_Param_CONFIG");
		
	// allocate the output streambuffer
	StreamBuffer_Obj = JPEG_MemAlloc_NEW(3 * JPEGImageWidth * JPEGImageHeight,1,MEM_TYPE_DATA);
	if(StreamBuffer_Obj == NULL)
	{
		fprintf(fperr, "Memory allocation for StreamBuffer failed.\n");
		exit(1);
	}
	// Dynamically allocate memory to store compressed image
	StreamBuffer = (uint8*)JPEG_MemAlloc_ADDRESS(StreamBuffer_Obj);

	// JPEG image storage buffer
	lResult = JPEG_Param_CONFIG(&lImageEncParam, JPEG_POINTER_OUTPUT, (int)StreamBuffer);
	if(lResult != E_SUCCESS)	JPEG_ProcessErrorCode(lResult, "JPEG_Param_CONFIG");

	// Create Instance of JPEG Encoder
	lJpegEnc = JPEG_Encoder_NEW(&lImageEncParam);
	if(NULL == lJpegEnc)
	{
		fprintf(fperr, "Encoder creation failed\n");
    	JPEG_MemAlloc_DELETE(StreamBuffer_Obj);		
		return CODEC_MEMORY_ALLOCATION_ERROR;
	}

	/****************************
	Setup MJPEG Encoder
	****************************/
	// Create the AVI output file, with the required file
	// write result to output file
	// check for file extension. add file extension if user didn't provide it
	if ((strstr(input_file, ".avi") == NULL) &&	(strstr(input_file, ".AVI") == NULL))
		strcat(input_file,".avi");	// add file extension	  	

	printf("\nWriting result to Output file: %s\n", input_file);
	
	// wait for user action
	printf("\npress and release button %s on EZ-kit to start recording\n",JPEG_MJPEG_REC_BUTTON_ID);	

	// wait for button to be pressed
	while(!ezIsButtonPushed(JPEG_MJPEG_RECORD_BUTTON));
	// wait for button to be released
	while(ezIsButtonPushed(JPEG_MJPEG_RECORD_BUTTON));

	// Get the input video mode & update the video parameters
	UpdateInputVideoMode();
	
	// Frame rate
	if (VideoMode == ITU656_NTSC)
		// NTSC frame rate (30fps)
		FrameRate = ITU656_NTSC_FRAME_RATE;
	else if (VideoMode == ITU656_PAL)
		// PAL frame rate (25fps)
		FrameRate = ITU656_PAL_FRAME_RATE;	
			
	// Open input file to write encoded video
	lResult = MJPEG_AVI_OpenFileWrite(&lStreamFileOutHandle, (uint8*)input_file,&lImageEncParam, FrameRate);
    if(lResult != MJPEG_AVI_RETURN_OK)
	{
        MJPEG_ProcessErrorCode(lResult, "MJPEG_AVI_OpenFileWrite");
        fprintf(fperr, "Error while opening the output file: %s \n", input_file);
		JPEG_Encoder_DELETE(lJpegEnc);
    	JPEG_MemAlloc_DELETE(StreamBuffer_Obj);
		return CODEC_FILE_ERROR;
	}
					
	// Set stream parameters from the JPEG parameters
	lResult = MJPEG_AVI_SetStreamParams (&lStreamInfo, &lImageEncParam, FrameRate);
	if (MJPEG_AVI_RETURN_ERROR == lResult)
	{
		fprintf(fperr, "Cannot set MJPEG AVI encoder stream parameters\n");
		JPEG_Encoder_DELETE(lJpegEnc);
    	JPEG_MemAlloc_DELETE(StreamBuffer_Obj);		
		return CODEC_ALGORITHM_ERROR;
	}

	// Create the stream
	lResult = MJPEG_AVI_OpenStreamWrite(lStreamFileOutHandle, &lStreamHandle, &lStreamInfo);
	if(lResult == MJPEG_AVI_RETURN_ERROR)
	{
		fprintf(fperr, "Cannot create MJPEG AVI encoder stream\n");
		JPEG_Encoder_DELETE(lJpegEnc);
    	JPEG_MemAlloc_DELETE(StreamBuffer_Obj);		
		return CODEC_ALGORITHM_ERROR;
	}

	// Copy the frame format from the JPEG parameters
	lResult = MJPEG_AVI_CopyParams (&lBitMapInfo, &lImageEncParam);
	if(lResult == MJPEG_AVI_RETURN_ERROR)
	{
		fprintf(fperr, "Cannot copy MJPEG AVI encoder frame format from JPEG parameters\n");
		JPEG_Encoder_DELETE(lJpegEnc);
    	JPEG_MemAlloc_DELETE(StreamBuffer_Obj);		
		return CODEC_ALGORITHM_ERROR;
	}

	// Set the stream format from the JPEG parameters
	lFormatLength = sizeof(tMJPEG_AVI_BITMAPINFO);
	lResult = MJPEG_AVI_SetFormat(lStreamHandle, 0, (uint8 *)&lBitMapInfo, lFormatLength);
	if(lResult == MJPEG_AVI_RETURN_ERROR)
	{
		fprintf(fperr, "Cannot set the MJPEG AVI encoder stream format\n");
		JPEG_Encoder_DELETE(lJpegEnc);
    	JPEG_MemAlloc_DELETE(StreamBuffer_Obj);		
		return CODEC_ALGORITHM_ERROR;
	}

	/****************************
	Encode video frames
	****************************/
	lNumFrames = 0;
	isMJEInputDataAvailable = E_TRUE;
	BufferLevel 	= NUM_YUV_BUFS;
    FrameReady		= TRUE;
	MJPEG_YUV_BufID = 0;
	YUV_MDMA_BufID	= 0;
	StartMDMA 		= TRUE;

#ifdef ADI_MMSK_EXTRA_INFO		// macro to enable extra debug information    	
	// initialise statistics
	max_frame_cycles = 0;
	min_frame_cycles = (cycle_t)10*600*1000000;		// set to time approx equivalent to 10Seconds at 600MHz - no frame should take longer than that to decode :)

	max_usb_cycles = 0;
	min_usb_cycles = (cycle_t)10*600*1000000;		// set to time approx equivalent to 10Seconds at 600MHz - no frame should take longer than that to be read :)
	max_usb_bytes = 0;
	min_usb_bytes = (unsigned int)0x0fffffff;		// big value for min calculation
	cycle_sum = 0;
#endif

	
	// Initialise ITU656 to YUV conversion parameters
 	// Source Descriptor start address - Video (ITU656) buffer(s)
 	pSrcDescStartAddr	= pITU656Frames[0];
 	// Destination Descriptor start buffer - YUV buffer(s)
 	pDestDescStartAddr	= pYUVBufs[0];
 	
	// check interleave format
	if (JPEGFrameFormat == 1)
	{
	    // Image will be encoded in YUV420 format
	    // initialise MDMA descriptors for ITU656 to YUV420 conversion
	    SetupMDMA_ITU656toYUV420 ();
	}

	// Check input video mode
	if (VideoMode == ITU656_PAL)
		printf ("\nRecording from ITU656 PAL Video source...\n");
	else if (VideoMode == ITU656_NTSC)
		printf ("\nRecording from ITU656 NTSC Video source...\n");
				
	printf("\npress and release button %s AGAIN to stop recording...\n",JPEG_MJPEG_REC_BUTTON_ID);

	// Submit Video Input buffers for MJPEG encoding (two ITU656 frame buffers will be used)
	SubmitVideoInBufs ();
		
	// wait until a video frame is captured		
	VideoFrameCompletedFlag = false;	
	while(VideoFrameCompletedFlag != true)
		asm("nop;");
	VideoFrameCompletedFlag = false;	

	// Reset counters
	VideoFrameCounter = 1;
	FrameDroppedCounter = 0;
	
	// Main loop for each frame
	do
	{
	    // Wait for a filled buffer to encode
		while (BufferLevel >= NUM_YUV_BUFS);
		
		// Get address of next YUV buffer
		lInputBuffer = (uint8*)(pYUVBufs[MJPEG_YUV_BufID]);		
		// Set input YUV buffer pointer
		JPEG_McuBuffer_CONFIG(mcu_ex, MCU_POINTER_C1, (unsigned int)lInputBuffer);		
		
#ifdef ADI_MMSK_EXTRA_INFO		// Extra debug information
	START_CYCLE_COUNT(start_count)	// begin counting time
#endif
	
		// Encode this frame	
		lResult = JPEG_EncodeSequentialImage(lJpegEnc, &NumBytes);

#ifdef ADI_MMSK_EXTRA_INFO		// Extra debug information
		STOP_CYCLE_COUNT(final_count,start_count)	// stop counting
		cycle_sum += (float)final_count;
		min_frame_cycles = min(min_frame_cycles, final_count);
		max_frame_cycles = max(max_frame_cycles, final_count);
#endif

		// Process Encoder result
		if (lResult != E_SUCCESS)
		{
	    	MJPEG_ProcessErrorCode(lResult, "JPEG_EncodeSequentialImage");			
	    	fprintf(fperr, "Failed to Encode this file!\n");
			JPEG_Encoder_DELETE(lJpegEnc);
    		JPEG_MemAlloc_DELETE(StreamBuffer_Obj);
			return CODEC_ALGORITHM_ERROR;
		}

		// Update the count (next YUV buffer to be used by MJPEG encoder)
		MJPEG_YUV_BufID++;		
		if (MJPEG_YUV_BufID >= NUM_YUV_BUFS)
			MJPEG_YUV_BufID = 0;
		
		// increment buffer level
		BufferLevel++;

#ifdef ADI_MMSK_EXTRA_INFO		// Extra debug information
	START_CYCLE_COUNT(start_count)	// begin counting time
#endif

		// Write encoded frame to the file		
		lResult = MJPEG_AVI_WriteStream(lStreamHandle, StreamBuffer, NumBytes, 0);

#ifdef ADI_MMSK_EXTRA_INFO		// Extra debug information
		STOP_CYCLE_COUNT(final_count,start_count)	// stop counting
		// update cycle measurements
		min_usb_cycles = min(min_usb_cycles, final_count);
		max_usb_cycles = max(max_usb_cycles, final_count);
		min_usb_bytes = min(min_usb_bytes, NumBytes);
		max_usb_bytes = max(max_usb_bytes, NumBytes);
#endif

		// Process Write encoded frame result
		if (lResult != MJPEG_AVI_RETURN_OK)
		{
			MJPEG_ProcessErrorCode(lResult, "MJPEG_AVI_WriteStream");
			fprintf(fperr, "\nCan not write MJPEG AVI encoder frame to stream\n");
			JPEG_Encoder_DELETE(lJpegEnc);
    		JPEG_MemAlloc_DELETE(StreamBuffer_Obj);			
			return CODEC_FILE_ERROR;
		}
		else 
		{
#ifdef ADI_MMSK_EXTRA_INFO		// Extra debug information
			if ((lNumFrames % FrameRate) == 0)	printf("\nRecorded %8.8d frames: ", lNumFrames);
			printf(".");
#endif				
			lNumFrames++;
		}

	} while ((!ezIsButtonPushed(JPEG_MJPEG_RECORD_BUTTON)));
	
	// Disable MDMA
	StartMDMA = false;

	// some statistics
	printf("\nRecorded %d Frames", lNumFrames);
#ifdef ADI_MMSK_EXTRA_INFO		// Extra debug information
	printf(", Dropped %d Frame(s)\n\n", FrameDroppedCounter);
	printf("Average Frame Rate of %d Frames/sec\n", FrameRate * lNumFrames/VideoFrameCounter);
	printf("Slowest Frame encoded in %llu cycles, equivalent to %lu cyles/pixel\n", max_frame_cycles,max_frame_cycles/(JPEGImageWidth * JPEGImageHeight));
	printf("Fastest Frame encoded in %llu cycles, equivalent to %lu cyles/pixel\n", min_frame_cycles,min_frame_cycles/(JPEGImageWidth * JPEGImageHeight));	
	printf("Encoding with an average of %.0f cycles, equivalent to %.2f cycles/pixel\n",cycle_sum/lNumFrames,cycle_sum/lNumFrames/(JPEGImageWidth * JPEGImageHeight));
	printf("Slowest Frame written %d bytes over USB in %llu cycles\n", max_usb_bytes, max_usb_cycles);
	printf("Fastest Frame written %d bytes over USB in %llu cycles\n", min_usb_bytes, min_usb_cycles);
#endif //#ifdef ADI_MMSK_EXTRA_INFO

	/************************************
	Finalise & Destroy Encoder		
	************************************/
	
	printf("\nPlease wait while closing this file stream...\n");
	
	// Close the AVI stream
	lResult = MJPEG_AVI_CloseStreamWrite(lStreamHandle);
	if(lResult == MJPEG_AVI_RETURN_ERROR)
	{
		fprintf(fperr, "Cannot close MJPEG AVI encoder stream\n");
		JPEG_Encoder_DELETE(lJpegEnc);
    	JPEG_MemAlloc_DELETE(StreamBuffer_Obj);		
		return CODEC_ALGORITHM_ERROR;
	}

	// Close the output file
	lResult = MJPEG_AVI_CloseFileWrite(lStreamFileOutHandle);
	if(lResult == MJPEG_AVI_RETURN_ERROR)
		fprintf(fperr, "Cannot close MJPEG AVI encoder file\n");
		
	// Free and destroy used resources
	JPEG_Encoder_DELETE(lJpegEnc);
    JPEG_MemAlloc_DELETE(StreamBuffer_Obj);

	printf("\n***  MJPEG Conversion successful ***\n");
	
  	return CODEC_SUCCESS;
}

/*****/
