// aviwrite.h
//
// MPEG2AVI
// --------
//  v0.16B31 added reset_counters() function, so the counters can be
//    reset independently of AVI writing,
//    corrected some printf() statements with %lu instead of %ld
//  v0.16B3 added m_fOverwrite (overwrite control flag, default == FALSE )
//    added set_fileoverwrite() 
//  v0.16B24 changed something, forgot what...
//  v0.16B1 initial release
//
//   This is a C++ object that (sort of) encapsulates the function calls
//   necessary to set up and use the VFW library for writing to an AVI
//   file.  This file was derived from avistore.c
//     Usage :
/*
	1) declare cAVIwriter object
	   cAVIwriter oAVIwriter;
	2) oAVIwriter.set_bmi( ) ;
	3) oAVIwriter.set_fps( ) ;
	4) oAVIwriter.set_avistring();


*/

#ifndef _CAVIWRITER_H
#define _CAVIWRITER_H 1

#define AVWERR_OK 0

#define AVWERR_VARS_NOT_INIT 1
	// AVI variables not initialized

#define AVWERR_BUSY 2
	// AVI writer is busy (file may be open)

#define AVWERR_OPEN 3
	// AVI writer already has open AVI-file open!

#define AVWERR_INVALID_PARAM 4
	// one or more parameters to the function were invalid

#define AVWERR_FAIL 5
	// generic fail code

// the following four defines are for the housekeeping functions/vars
#define TEXTMSG_LENGTH 1024
	// maximum text length of a single log-entry (message or error)
#define LOG_LENGTH 32767
	// maximum text length of messagelog and errorlog
#define LOG_NEW_ENTRY 0x0000001L // flag
#define LOG_RESET 0x0000002L	// flag

#include<time.h>

class cAVIwriter
{
public:

		// the AVIrelated variables are updated only by avi_ functions
	PAVIFILE         m_pAVIfile; 
	PAVISTREAM       m_pAVIstream;
	PAVISTREAM		 m_pAVIstream_compressed;
	BITMAPINFOHEADER m_bmih; // active bitmap format
	BITMAPINFO		 m_bmi;
	LPVOID			m_lpVideoData;
	COMPVARS		m_compvars, m_compvars2;
		// m_compvars2 = backup, set by avi_fileopen() to preserve initial state
	AVISTREAMINFO    m_AVIstreaminfo; 
	AVICOMPRESSOPTIONS m_avicompressoptions;
	ICCOMPRESSFRAMES m_iccompressframes;  // for IC
	BITMAPINFO m_outbmi; // for IC

	DWORD m_dwStreamSize;
	DWORD m_dwFrameCount; // running counter of all frames written to 
								// AVI file, includes both key and non-key frames
	DWORD m_cbWritten; // running counter of bytes written to AVI file
	DWORD m_dwKFCount; // running counter of key-frames written to AVI file
	
	time_t m_start_time; // clock snapshot
	
	unsigned char *m_lpCompBuf; // Compressor buffer, current frame
	unsigned char *m_lpCompBufP; // Compressor buffer, previous frame

//SVE 03.06.2001
	runstate* m_pRunState;

public:
	// public interface
	cAVIwriter();	// constructor
	~cAVIwriter();	// destructor


	DWORD getLastError( char errText[] )
	{
//SVE 03.06.2001 TextLogMsg nur im Debug-Modus
#ifdef _DEBUG
		if ( errText != NULL )
			strcpy( errText, m_szLastLogErr);
#endif
		return m_lastError; 
	};

	DWORD ui_codec( BOOL fNewCodec = TRUE ); // ask user to choose ICCompressor codec
	void reset_counters(); // reset AVI counters


//SVE 20.05.2001 Neuer Parameter: Erzwungener Keyframe
	DWORD store_avi_one( const char aviFilename[], unsigned char *source_frame,
		LPRECT CropRect, long FrameNumber, BOOL bKeyFrame );
//SVE ENDE
	DWORD set_fps( DWORD rate, DWORD scale );  // set AVIframerate
	DWORD set_bmi( BITMAPINFO bmi ); // set BITMAP format of input
	DWORD set_fileoverwrite( BOOL fOverwrite ); // set m_fOverwrite variable
	DWORD get_bmi( LPBITMAPINFO lpbmi ); // get BITMAP format of AVI-file
	DWORD get_framecount( void ); // get frame count
	DWORD get_kfcount( void ); // get keyframe count
	DWORD get_cbWritten( void ); // get bytes written to AVI file


	
	DWORD set_avistring( const char avistring[] ); // set AVIstreaminfo.szName
	DWORD avi_fileopen( const char aviFilename[], LPRECT CropRect );

	DWORD avi_fileclose( void );

protected:
	//
	// housekeeping variables for logging purposes
	//

//SVE 03.06.2001 TextLogMsg nur im Debug-Modus
#ifdef _DEBUG
	char *m_szErrorLog, *m_szMessageLog; // text logs
	char *m_szText, *m_szText2; // temporary text workspace
	char *m_szLastLogErr; // last error message, set by TextLogErr(),
		// reset by TextLogMsg( LOG_NEW_ENTRY, ... )
		// set by avi_fileopen(), reset to NULL by avi_fileclose()
	DWORD m_logCounter; // message ID counter
#endif

	//
	// status variables, updated by other processes/functions
	//

	BOOL fInUse; // status, AVIfile writer in use?
	DWORD m_lastError; // last error code, set by TextLogErr(),
		// reset by TextLogMsg( LOG_NEW_ENTRY, ... )
	BOOL m_fUseYUV; // Is g_bmih a YUV pixelformat?
	BOOL m_fOverwrite; // v0.16B3 overwrite AVI-file if already exists?

	unsigned char *m_lpState; // temporary storage space for codec state
	char *m_szFileName; // AVI filename

	/* private functions */
//SVE 03.06.2001 TextLogMsg nur im Debug-Modus
#ifdef _DEBUG
	void TextLogErr( DWORD errorID, char text[] );
	void TextLogMsg( DWORD dwFlags, char text[] );
#endif
	DWORD avi_store_rgb24( unsigned char *source_frame );
//SVE 20.05.2001 Neuer Parameter: Out-Rect,Erzwungener Keyframe
	DWORD avi_store_yuv( unsigned char *source_frame, LPRECT lpOut, BOOL bKeyFrame);
//SVE ENDE
	DWORD avi_initvars( void );	// set-up avistream and info vars
	int get_fps( LPDWORD lpfpsRate, LPDWORD lpfpsScale );
	BOOL fIsAVIOpen( void ) // is AVIfile currently open?
		{ return fInUse; };
	
	DWORD SetRectCenter( LPRECT lprc, int src_x, int src_y, int dest_x, 
		int dest_y);

	
};
#endif
