1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef AVI_GENERATOR_H
4 #define AVI_GENERATOR_H
5 
6 #ifdef WIN32
7 
8 #include <boost/thread/thread.hpp>
9 #include <boost/thread/mutex.hpp>
10 #include <boost/thread/condition.hpp>
11 #include <boost/utility.hpp>
12 
13 #include <windows.h>
14 #include <vfw.h>
15 
16 #include <string>
17 #include <list>
18 
19 
20 class CAVIGenerator : boost::noncopyable {
21 public:
22 
23 	CAVIGenerator(const std::string& fileName, int videoSizeX, int videoSizeY, DWORD videoFPS);
24 	~CAVIGenerator();
25 
26 	/// Initialize engine and choose codec
27 	bool InitEngine();
28 
29 	/// Returns last error message
GetLastErrorMessage()30 	std::string GetLastErrorMessage() const	{return errorMsg;}
31 
32 	bool readOpenglPixelDataThreaded();
33 
34 
35 private:
36 	/// name of output file
37 	std::string fileName;
38 	/// Frame rate
39 	DWORD videoFPS;
40 	/// structure contains information for a single stream
41 	BITMAPINFOHEADER bitmapInfo;
42 	/// last error string
43 	std::string errorMsg;
44 
45 	HINSTANCE msvfw32;
46 	HINSTANCE avifil32;
47 
48 	volatile bool quitAVIgen;
49 
50 	boost::thread* AVIThread;
51 	boost::mutex AVIMutex;
52 	boost::condition AVICondition;
53 
54 
55 	std::list<unsigned char*> freeImageBuffers;
56 	std::list<unsigned char*> imageBuffers;
57 
58 	unsigned char* readBuf;
59 
60 
61 
62 	bool initVFW();
63 
64 	HRESULT InitAVICompressionEngine();
65 
66 	/// Release streams allocated for movie compression.
67 	void ReleaseAVICompressionEngine();
68 
69 	/// Adds a frame to the movie.
70 	HRESULT AddFrame(unsigned char* pixelData);
71 
72 	void AVIGeneratorThreadProc();
73 
74 
75 	/// frame counter
76 	long m_lFrame;
77 	/// file interface pointer
78 	PAVIFILE m_pAVIFile;
79 	/// Address of the stream interface
80 	PAVISTREAM m_pStream;
81 	/// Address of the compressed video stream
82 	PAVISTREAM m_pStreamCompressed;
83 	/// Holds compression settings
84 	COMPVARS cv;
85 
86 	typedef DWORD (__stdcall *VideoForWindowsVersion_type)(void);
87 	typedef void (__stdcall *AVIFileInit_type)(void);
88 	typedef HRESULT (__stdcall *AVIFileOpenA_type)(PAVIFILE FAR *, LPCSTR, UINT, LPCLSID);
89 	typedef HRESULT (__stdcall *AVIFileCreateStreamA_type)(PAVIFILE, PAVISTREAM FAR *, AVISTREAMINFOA FAR *);
90 	typedef HRESULT (__stdcall *AVIMakeCompressedStream_type)(PAVISTREAM FAR *, PAVISTREAM, AVICOMPRESSOPTIONS FAR *, CLSID FAR *);
91 	typedef HRESULT (__stdcall *AVIStreamSetFormat_type)(PAVISTREAM, LONG, LPVOID, LONG);
92 	typedef ULONG (__stdcall *AVIStreamRelease_type)(PAVISTREAM);
93 	typedef ULONG (__stdcall *AVIFileRelease_type)(PAVIFILE);
94 	typedef void (__stdcall *AVIFileExit_type)(void);
95 	typedef HRESULT (__stdcall *AVIStreamWrite_type)(PAVISTREAM, LONG, LONG, LPVOID, LONG, DWORD, LONG FAR *, LONG FAR *);
96 	typedef BOOL (__stdcall *ICCompressorChoose_type)(HWND, UINT, LPVOID, LPVOID, PCOMPVARS, LPSTR);
97 	typedef void (__stdcall *ICCompressorFree_type)(PCOMPVARS);
98 	typedef HIC (__stdcall *ICOpen_type)(DWORD, DWORD, UINT);
99 
100 
101 
102 	VideoForWindowsVersion_type VideoForWindowsVersion_ptr;
103 	AVIFileInit_type AVIFileInit_ptr;
104 	AVIFileOpenA_type AVIFileOpenA_ptr;
105 	AVIFileCreateStreamA_type AVIFileCreateStreamA_ptr;
106 	AVIMakeCompressedStream_type AVIMakeCompressedStream_ptr;
107 	AVIStreamSetFormat_type AVIStreamSetFormat_ptr;
108 	AVIStreamRelease_type AVIStreamRelease_ptr;
109 	AVIFileRelease_type AVIFileRelease_ptr;
110 	AVIFileExit_type AVIFileExit_ptr;
111 	AVIStreamWrite_type AVIStreamWrite_ptr;
112 	ICCompressorChoose_type ICCompressorChoose_ptr;
113 	ICCompressorFree_type ICCompressorFree_ptr;
114 	ICOpen_type ICOpen_ptr;
115 
116 };
117 
118 #endif /* WIN32 */
119 #endif /* AVI_GENERATOR_H */
120