4 #pragma comment (lib, "vfw32.lib")
7 #define __countof(x) ((sizeof(x)/sizeof(x[0])))
23 m_pAviCompressedStream=NULL;
25 m_dwFCCHandler = dwCodec;
26 m_dwFrameRate = dwFrameRate;
28 _tcscpy(m_szFileName, lpszFileName);
29 _tcscpy(m_szErrMsg, _T(
"Method Succeeded"));
30 m_szErrMsg[__countof(m_szErrMsg)-1] = _T(
'\0');
32 pAppendFrame[0]= &CAviFile::AppendDummy;
33 pAppendFrame[1]= &CAviFile::AppendFrameFirstTime;
34 pAppendFrame[2]= &CAviFile::AppendFrameUsual;
36 pAppendFrameBits[0]=&CAviFile::AppendDummy;
37 pAppendFrameBits[1]=&CAviFile::AppendFrameFirstTime;
38 pAppendFrameBits[2]=&CAviFile::AppendFrameUsual;
40 m_nAppendFuncSelector=1;
50 void CAviFile::ReleaseMemory()
52 m_nAppendFuncSelector=0;
59 if(m_pAviCompressedStream)
61 AVIStreamRelease(m_pAviCompressedStream);
62 m_pAviCompressedStream=NULL;
66 AVIStreamRelease(m_pAviStream);
71 AVIFileRelease(m_pAviFile);
76 HeapFree(m_hHeap,HEAP_NO_SERIALIZE,m_lpBits);
86 void CAviFile::SetErrorMessage(LPCTSTR lpszErrorMessage)
88 _tcsncpy(m_szErrMsg, lpszErrorMessage, __countof(m_szErrMsg)-1);
91 HRESULT CAviFile::InitMovieCreation(
int nFrameWidth,
int nFrameHeight,
int nBitsPerPixel)
93 int nMaxWidth=GetSystemMetrics(SM_CXSCREEN),nMaxHeight=GetSystemMetrics(SM_CYSCREEN);
95 m_hAviDC = CreateCompatibleDC(NULL);
98 SetErrorMessage(_T(
"Unable to Create Compatible DC"));
102 if(nFrameWidth > nMaxWidth) nMaxWidth= nFrameWidth;
103 if(nFrameHeight > nMaxHeight) nMaxHeight = nFrameHeight;
105 m_hHeap=HeapCreate(HEAP_NO_SERIALIZE, nMaxWidth*nMaxHeight*4, 0);
108 SetErrorMessage(_T(
"Unable to Create Heap"));
112 m_lpBits=HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY|HEAP_NO_SERIALIZE, nMaxWidth*nMaxHeight*4);
115 SetErrorMessage(_T(
"Unable to Allocate Memory on Heap"));
119 if(FAILED(AVIFileOpen(&m_pAviFile, m_szFileName, OF_CREATE|OF_WRITE, NULL)))
121 SetErrorMessage(_T(
"Unable to Create the Movie File"));
125 ZeroMemory(&m_AviStreamInfo,
sizeof(AVISTREAMINFO));
126 m_AviStreamInfo.fccType = streamtypeVIDEO;
127 m_AviStreamInfo.fccHandler = m_dwFCCHandler;
128 m_AviStreamInfo.dwScale = 1;
129 m_AviStreamInfo.dwRate = m_dwFrameRate;
130 m_AviStreamInfo.dwQuality = -1;
131 m_AviStreamInfo.dwSuggestedBufferSize = nMaxWidth*nMaxHeight*4;
132 SetRect(&m_AviStreamInfo.rcFrame, 0, 0, nFrameWidth, nFrameHeight);
133 _tcscpy(m_AviStreamInfo.szName, _T(
"Video Stream"));
135 if(FAILED(AVIFileCreateStream(m_pAviFile,&m_pAviStream,&m_AviStreamInfo)))
137 SetErrorMessage(_T(
"Unable to Create Video Stream in the Movie File"));
141 ZeroMemory(&m_AviCompressOptions,
sizeof(AVICOMPRESSOPTIONS));
142 m_AviCompressOptions.fccType=streamtypeVIDEO;
143 m_AviCompressOptions.fccHandler=m_AviStreamInfo.fccHandler;
144 m_AviCompressOptions.dwFlags=AVICOMPRESSF_KEYFRAMES|AVICOMPRESSF_VALID;
145 m_AviCompressOptions.dwKeyFrameEvery=1;
149 if(FAILED(AVIMakeCompressedStream(&m_pAviCompressedStream,m_pAviStream,&m_AviCompressOptions,NULL)))
154 SetErrorMessage(_T(
"Unable to Create Compressed Stream: Check your CODEC options"));
159 ZeroMemory(&bmpInfo,
sizeof(BITMAPINFO));
160 bmpInfo.bmiHeader.biPlanes = 1;
161 bmpInfo.bmiHeader.biWidth = nFrameWidth;
162 bmpInfo.bmiHeader.biHeight = nFrameHeight;
163 bmpInfo.bmiHeader.biCompression = BI_RGB;
164 bmpInfo.bmiHeader.biBitCount = nBitsPerPixel;
165 bmpInfo.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
166 bmpInfo.bmiHeader.biSizeImage = bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biHeight*bmpInfo.bmiHeader.biBitCount/8;
168 if(FAILED(AVIStreamSetFormat(m_pAviCompressedStream,0,(LPVOID)&bmpInfo, bmpInfo.bmiHeader.biSize)))
176 SetErrorMessage(_T(
"Unable to Set Video Stream Format"));
184 HRESULT CAviFile::AppendFrameFirstTime(HBITMAP hBitmap)
188 GetObject(hBitmap,
sizeof(BITMAP), &Bitmap);
190 if(SUCCEEDED(InitMovieCreation( Bitmap.bmWidth,
192 Bitmap.bmBitsPixel)))
194 m_nAppendFuncSelector=2;
196 return AppendFrameUsual(hBitmap);
204 HRESULT CAviFile::AppendFrameUsual(HBITMAP hBitmap)
208 bmpInfo.bmiHeader.biBitCount=0;
209 bmpInfo.bmiHeader.biSize=
sizeof(BITMAPINFOHEADER);
211 GetDIBits(m_hAviDC,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
213 bmpInfo.bmiHeader.biCompression=BI_RGB;
215 GetDIBits(m_hAviDC,hBitmap,0,bmpInfo.bmiHeader.biHeight,m_lpBits,&bmpInfo,DIB_RGB_COLORS);
217 if(FAILED(AVIStreamWrite(m_pAviCompressedStream,m_lSample++,1,m_lpBits,bmpInfo.bmiHeader.biSizeImage,0,NULL,NULL)))
219 SetErrorMessage(_T(
"Unable to Write Video Stream to the output Movie File"));
229 HRESULT CAviFile::AppendDummy(HBITMAP)
236 return (this->*pAppendFrame[m_nAppendFuncSelector])((HBITMAP)hBitmap);
241 return (this->*pAppendFrameBits[m_nAppendFuncSelector])(nWidth,nHeight,pBits,nBitsPerPixel);
244 HRESULT CAviFile::AppendFrameFirstTime(
int nWidth,
int nHeight, LPVOID pBits,
int nBitsPerPixel)
246 if(SUCCEEDED(InitMovieCreation(nWidth, nHeight, nBitsPerPixel)))
248 m_nAppendFuncSelector=2;
250 return AppendFrameUsual(nWidth, nHeight, pBits, nBitsPerPixel);
258 HRESULT CAviFile::AppendFrameUsual(
int nWidth,
int nHeight, LPVOID pBits,
int nBitsPerPixel)
260 DWORD dwSize=nWidth*nHeight*nBitsPerPixel/8;
262 if(FAILED(AVIStreamWrite(m_pAviCompressedStream,m_lSample++,1,pBits,dwSize,0,NULL,NULL)))
264 SetErrorMessage(_T(
"Unable to Write Video Stream to the output Movie File"));
274 HRESULT CAviFile::AppendDummy(
int nWidth,
int nHeight, LPVOID pBits,
int nBitsPerPixel)
HRESULT AppendNewFrame(HBITMAP hBitmap)
CAviFile(LPCTSTR lpszFileName=_T("Output.avi"), DWORD dwCodec=mmioFOURCC('M','P','G','4'), DWORD dwFrameRate=1)