/
AviFile.cpp
388 lines (315 loc) · 10.2 KB
/
AviFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "StdAfx.h"
#include "avifile.h"
#ifndef __countof
#define __countof(x) ((sizeof(x)/sizeof(x[0])))
#endif
CAviFile:: CAviFile(LPCSTR lpszFileName /* =_T("Output.avi") */,
DWORD dwCodec /* = mmioFOURCC('M','P','G','4') */,
DWORD dwFrameRate /* = 1 */)
{
AVIFileInit();
m_hHeap=NULL;
m_hAviDC=NULL;
m_lpBits=NULL;
m_lSample=NULL;
m_pAviFile=NULL;
m_pAviStream=NULL;
m_pAviCompressedStream=NULL;
m_dwFCCHandler = dwCodec;
m_dwFrameRate = dwFrameRate;
m_szFileName = lpszFileName;
m_szErrMsg = "Method Succeeded";
m_LastError = S_OK;
pAppendFrame[0]= &CAviFile::AppendDummy; // VC8 requires & for Function Pointer; Remove it if your compiler complains;
pAppendFrame[1]= &CAviFile::AppendFrameFirstTime;
pAppendFrame[2]= &CAviFile::AppendFrameUsual;
pAppendFrameDIBSection[0]= &CAviFile::AppendDummy; // VC8 requires & for Function Pointer; Remove it if your compiler complains;
pAppendFrameDIBSection[1]= &CAviFile::AppendFrameFirstTime;
pAppendFrameDIBSection[2]= &CAviFile::AppendFrameUsual;
pAppendFrameBits[0]=&CAviFile::AppendDummy;
pAppendFrameBits[1]=&CAviFile::AppendFrameFirstTime;
pAppendFrameBits[2]=&CAviFile::AppendFrameUsual;
m_nAppendFuncSelector=1; //0=Dummy 1=FirstTime 2=Usual
}
CAviFile::~CAviFile(void)
{
ReleaseMemory();
AVIFileExit();
}
void CAviFile::GetCompressorList( CStringList &List, BITMAPINFO &SourceBitmapInfo )
{
///////////////////////////////////////////////////////////
LPBITMAPINFOHEADER lpbi = 0;
HIC hIC;
ICINFO icinfo;
int fccType = ICTYPE_VIDEO; //0 to get all installed codecs
for (int i=0; ICInfo(fccType, i, &icinfo); i++) {
hIC = ICOpen(icinfo.fccType, icinfo.fccHandler, ICMODE_QUERY);
if (hIC) {
//Find out the compressor name.
ICGetInfo(hIC, &icinfo, sizeof(icinfo));
//Skip this compressor if it can't handle the format.
if (fccType == ICTYPE_VIDEO )
{
if( icinfo.dwFlags != 0 )
{
if( ICCompressQuery( hIC, &SourceBitmapInfo, NULL ) == ICERR_OK )
{
CString Temp = CString( icinfo.szDescription );
if( icinfo.fccHandler == 1129730893 )
Temp += " - Low Resolution";
Temp += " (";
Temp.Append( (const char*)&icinfo.fccHandler, 4 ); // Assuming little endian system.
Temp += ")";
List.AddTail( Temp );
}
}
}
//check here whether it is the driver you want
ICClose(hIC);
}
}
}
void CAviFile::ReleaseMemory()
{
m_nAppendFuncSelector=0; //Point to DummyFunction
if(m_hAviDC)
{
DeleteDC(m_hAviDC);
m_hAviDC=NULL;
}
if(m_pAviCompressedStream)
{
AVIStreamRelease(m_pAviCompressedStream);
m_pAviCompressedStream=NULL;
}
if(m_pAviStream)
{
AVIStreamRelease(m_pAviStream);
m_pAviStream=NULL;
}
if(m_pAviFile)
{
AVIFileRelease(m_pAviFile);
m_pAviFile=NULL;
}
if(m_lpBits)
{
HeapFree(m_hHeap,HEAP_NO_SERIALIZE,m_lpBits);
m_lpBits=NULL;
}
if(m_hHeap)
{
HeapDestroy(m_hHeap);
m_hHeap=NULL;
}
}
void CAviFile::SetErrorMessage(LPCTSTR lpszErrorMessage)
{
m_szErrMsg = lpszErrorMessage;
}
HRESULT CAviFile::InitMovieCreation(int nFrameWidth, int nFrameHeight, int nBitsPerPixel)
{
int nMaxWidth=GetSystemMetrics(SM_CXSCREEN),nMaxHeight=GetSystemMetrics(SM_CYSCREEN);
m_hAviDC = CreateCompatibleDC(NULL);
if(m_hAviDC==NULL)
{
SetErrorMessage("Unable to Create Compatible DC");
m_LastError = E_FAIL;
return E_FAIL;
}
if(nFrameWidth > nMaxWidth) nMaxWidth= nFrameWidth;
if(nFrameHeight > nMaxHeight) nMaxHeight = nFrameHeight;
m_hHeap=HeapCreate(HEAP_NO_SERIALIZE, nMaxWidth*nMaxHeight*4, 0);
if(m_hHeap==NULL)
{
SetErrorMessage("Unable to Create Heap");
m_LastError = E_FAIL;
return E_FAIL;
}
m_lpBits=HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY|HEAP_NO_SERIALIZE, nMaxWidth*nMaxHeight*4);
if(m_lpBits==NULL)
{
SetErrorMessage("Unable to Allocate Memory on Heap");
m_LastError = E_FAIL;
return E_FAIL;
}
// Make sure that the file doesn't exist before acting on it.
_unlink( m_szFileName );
DWORD Result = AVIFileOpen( &m_pAviFile, m_szFileName, OF_CREATE|OF_WRITE, NULL );
if( FAILED( Result ) )
{
SetErrorMessage("Unable to Create the Movie File");
m_LastError = E_FAIL;
return E_FAIL;
}
ZeroMemory(&m_AviStreamInfo,sizeof(AVISTREAMINFO));
m_AviStreamInfo.fccType = streamtypeVIDEO;
m_AviStreamInfo.fccHandler = m_dwFCCHandler;
m_AviStreamInfo.dwScale = 1;
m_AviStreamInfo.dwRate = m_dwFrameRate; // Frames Per Second;
m_AviStreamInfo.dwQuality = -1; // Default Quality
m_AviStreamInfo.dwSuggestedBufferSize = nMaxWidth*nMaxHeight*4;
SetRect(&m_AviStreamInfo.rcFrame, 0, 0, nFrameWidth, nFrameHeight);
strcpy_s( m_AviStreamInfo.szName, sizeof( m_AviStreamInfo.szName ), _T("Video Stream"));
if(FAILED(AVIFileCreateStream(m_pAviFile,&m_pAviStream,&m_AviStreamInfo)))
{
SetErrorMessage("Unable to Create Video Stream in the Movie File");
m_LastError = E_FAIL;
return E_FAIL;
}
ZeroMemory(&m_AviCompressOptions,sizeof(AVICOMPRESSOPTIONS));
m_AviCompressOptions.fccType=streamtypeVIDEO;
m_AviCompressOptions.fccHandler=m_AviStreamInfo.fccHandler;
m_AviCompressOptions.dwFlags=AVICOMPRESSF_VALID;
// m_AviCompressOptions.dwKeyFrameEvery=100;
// m_AviCompressOptions.dwBytesPerSecond=1000; this is ignored for xvid recording
// m_AviCompressOptions.dwQuality=500; this is ignored for xvid recording
HRESULT hResult = AVIMakeCompressedStream( &m_pAviCompressedStream, m_pAviStream, &m_AviCompressOptions, NULL);
if(FAILED(hResult))
{
// One reason this error might occur is if you are using a Codec that is not
// available on your system. Check the mmioFOURCC() code you are using and make
// sure you have that codec installed properly on your machine.
SetErrorMessage("Unable to Create Compressed Stream: Check your CODEC options");
m_LastError = E_FAIL;
return E_FAIL;
}
BITMAPINFO bmpInfo;
ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biWidth = nFrameWidth;
bmpInfo.bmiHeader.biHeight = nFrameHeight;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biBitCount = nBitsPerPixel;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
int LineByteWidth = ( ( bmpInfo.bmiHeader.biWidth * 3 ) + 3 ) & ~3;
bmpInfo.bmiHeader.biSizeImage = LineByteWidth*bmpInfo.bmiHeader.biHeight;
if(FAILED(AVIStreamSetFormat(m_pAviCompressedStream,0,(LPVOID)&bmpInfo, bmpInfo.bmiHeader.biSize)))
{
// One reason this error might occur is if your bitmap does not meet the Codec requirements.
// For example,
// your bitmap is 32bpp while the Codec supports only 16 or 24 bpp; Or
// your bitmap is 274 * 258 size, while the Codec supports only sizes that are powers of 2; etc...
// Possible solution to avoid this is: make your bitmap suit the codec requirements,
// or Choose a codec that is suitable for your bitmap.
SetErrorMessage("Unable to Set Video Stream Format");
m_LastError = E_FAIL;
return E_FAIL;
}
return S_OK; // Everything went Fine
}
HRESULT CAviFile::AppendFrameFirstTime(HBITMAP hBitmap, int Count)
{
BITMAP Bitmap;
GetObject(hBitmap, sizeof(BITMAP), &Bitmap);
if(SUCCEEDED(InitMovieCreation( Bitmap.bmWidth,
Bitmap.bmHeight,
Bitmap.bmBitsPixel)))
{
m_nAppendFuncSelector=2; //Point to the UsualAppend Function
return AppendFrameUsual(hBitmap, Count);
}
ReleaseMemory();
return E_FAIL;
}
HRESULT CAviFile::AppendFrameFirstTime( CDIBSection *pDIBSection, int Count )
{
BITMAP Bitmap;
GetObject( pDIBSection->m_Bitmap, sizeof(BITMAP), &Bitmap);
if(SUCCEEDED(InitMovieCreation( Bitmap.bmWidth,
Bitmap.bmHeight,
Bitmap.bmBitsPixel)))
{
m_nAppendFuncSelector=2; //Point to the UsualAppend Function
return AppendFrameUsual( pDIBSection, Count );
}
ReleaseMemory();
return E_FAIL;
}
HRESULT CAviFile::AppendFrameUsual(HBITMAP hBitmap, int Count)
{
BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biBitCount=0;
bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
GetDIBits(m_hAviDC,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
bmpInfo.bmiHeader.biCompression=BI_RGB;
GetDIBits(m_hAviDC,hBitmap,0,bmpInfo.bmiHeader.biHeight,m_lpBits,&bmpInfo,DIB_RGB_COLORS);
while( Count > 0 )
{
if(FAILED(AVIStreamWrite(m_pAviCompressedStream,m_lSample++,1,m_lpBits,bmpInfo.bmiHeader.biSizeImage,0,NULL,NULL)))
{
SetErrorMessage(_T("Unable to Write Video Stream to the output Movie File"));
ReleaseMemory();
m_LastError = E_FAIL;
return E_FAIL;
}
--Count;
}
return S_OK;
}
HRESULT CAviFile::AppendFrameUsual( CDIBSection *pDIBSection, int Count )
{
while( Count > 0 )
{
if(FAILED(AVIStreamWrite(m_pAviCompressedStream,m_lSample++,1,pDIBSection->m_pBits,pDIBSection->m_bmpInfo.bmiHeader.biSizeImage,0,NULL,NULL)))
{
SetErrorMessage(_T("Unable to Write Video Stream to the output Movie File"));
ReleaseMemory();
m_LastError = E_FAIL;
return E_FAIL;
}
--Count;
}
return S_OK;
}
HRESULT CAviFile::AppendDummy(HBITMAP, int)
{
return E_FAIL;
}
HRESULT CAviFile::AppendDummy( CDIBSection *, int )
{
return E_FAIL;
}
HRESULT CAviFile::AppendNewFrame(HBITMAP hBitmap, int Count)
{
return (this->*pAppendFrame[m_nAppendFuncSelector])((HBITMAP)hBitmap, Count);
}
HRESULT CAviFile::AppendNewFrame( CDIBSection *pDIBSection, int Count )
{
return (this->*pAppendFrameDIBSection[m_nAppendFuncSelector])(pDIBSection,Count);
}
HRESULT CAviFile::AppendNewFrame(int nWidth, int nHeight, LPVOID pBits,int nBitsPerPixel, int Count)
{
return (this->*pAppendFrameBits[m_nAppendFuncSelector])(nWidth,nHeight,pBits,nBitsPerPixel,Count);
}
HRESULT CAviFile::AppendFrameFirstTime(int nWidth, int nHeight, LPVOID pBits,int nBitsPerPixel,int Count)
{
if(SUCCEEDED(InitMovieCreation(nWidth, nHeight, nBitsPerPixel)))
{
m_nAppendFuncSelector=2; //Point to the UsualAppend Function
return AppendFrameUsual(nWidth, nHeight, pBits, nBitsPerPixel, Count);
}
ReleaseMemory();
return E_FAIL;
}
HRESULT CAviFile::AppendFrameUsual(int nWidth, int nHeight, LPVOID pBits,int nBitsPerPixel,int Count)
{
DWORD dwSize=nWidth*nHeight*nBitsPerPixel/8;
while( Count > 0 )
{
if(FAILED(AVIStreamWrite(m_pAviCompressedStream,m_lSample++,1,pBits,dwSize,0,NULL,NULL)))
{
SetErrorMessage(_T("Unable to Write Video Stream to the output Movie File"));
ReleaseMemory();
m_LastError = E_FAIL;
return E_FAIL;
}
--Count;
}
return S_OK;
}
HRESULT CAviFile::AppendDummy(int nWidth, int nHeight, LPVOID pBits,int nBitsPerPixel,int Count)
{
return E_FAIL;
}