/[pcsx2_0.9.7]/trunk/plugins/zzogl-pg/opengl/Win32/aviUtil.h
ViewVC logotype

Contents of /trunk/plugins/zzogl-pg/opengl/Win32/aviUtil.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 2 months ago) by william
File MIME type: text/plain
File size: 11992 byte(s)
re-commit (had local access denied errors when committing)
1 #ifndef __AVIUTIL_H__
2 #define __AVIUTIL_H__
3
4 #define AVIIF_KEYFRAME 0x00000010L // this frame is a key frame.
5
6 #include <string>
7 using namespace std;
8
9 #include <memory.h>
10 #include <mmsystem.h>
11 #include <vfw.h>
12
13 BOOL AVI_Init()
14 {
15 /* first let's make sure we are running on 1.1 */
16 WORD wVer = HIWORD(VideoForWindowsVersion());
17 if (wVer < 0x010a){
18 /* oops, we are too old, blow out of here */
19 //MessageBeep(MB_ICONHAND);
20 MessageBox(NULL, "Cant't init AVI File - Video for Windows version is to old", "Error", MB_OK|MB_ICONSTOP);
21 return FALSE;
22 }
23
24 AVIFileInit();
25
26 return TRUE;
27 }
28
29 BOOL AVI_FileOpenWrite(PAVIFILE * pfile, const char *filename)
30 {
31 HRESULT hr = AVIFileOpen(pfile, // returned file pointer
32 filename, // file name
33 OF_WRITE | OF_CREATE, // mode to open file with
34 NULL); // use handler determined
35 // from file extension....
36 if (hr != AVIERR_OK)
37 return FALSE;
38
39 return TRUE;
40 }
41
42 DWORD getFOURCC(const char* value)
43 {
44 if(_stricmp(value, "DIB") == 0)
45 {
46 return mmioFOURCC(value[0],value[1],value[2],' ');
47 }
48 else if((_stricmp(value, "CVID") == 0)
49 || (_stricmp(value, "IV32") == 0)
50 || (_stricmp(value, "MSVC") == 0)
51 || (_stricmp(value, "IV50") == 0))
52 {
53 return mmioFOURCC(value[0],value[1],value[2],value[3]);
54 }
55 else
56 {
57 return NULL;
58 }
59 }
60
61 // Fill in the header for the video stream....
62 // The video stream will run in rate ths of a second....
63 BOOL AVI_CreateStream(PAVIFILE pfile, PAVISTREAM * ps, int rate, // sample/second
64 unsigned long buffersize, int rectwidth, int rectheight,
65 const char* _compressor)
66 {
67 AVISTREAMINFO strhdr;
68 memset(&strhdr, 0, sizeof(strhdr));
69 strhdr.fccType = streamtypeVIDEO;// stream type
70 strhdr.fccHandler = getFOURCC(_compressor);
71 //strhdr.fccHandler = 0; // no compression!
72 //strhdr.fccHandler = mmioFOURCC('D','I','B',' '); // Uncompressed
73 //strhdr.fccHandler = mmioFOURCC('C','V','I','D'); // Cinpak
74 //strhdr.fccHandler = mmioFOURCC('I','V','3','2'); // Intel video 3.2
75 //strhdr.fccHandler = mmioFOURCC('M','S','V','C'); // Microsoft video 1
76 //strhdr.fccHandler = mmioFOURCC('I','V','5','0'); // Intel video 5.0
77 //strhdr.dwFlags = AVISTREAMINFO_DISABLED;
78 //strhdr.dwCaps =
79 //strhdr.wPriority =
80 //strhdr.wLanguage =
81 strhdr.dwScale = 1;
82 strhdr.dwRate = rate; // rate fps
83 //strhdr.dwStart =
84 //strhdr.dwLength =
85 //strhdr.dwInitialFrames =
86 strhdr.dwSuggestedBufferSize = buffersize;
87 strhdr.dwQuality = -1; // use the default
88 //strhdr.dwSampleSize =
89 SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream
90 (int) rectwidth,
91 (int) rectheight);
92 //strhdr.dwEditCount =
93 //strhdr.dwFormatChangeCount =
94 //strcpy(strhdr.szName, "Full Frames (Uncompressed)");
95
96 // And create the stream;
97 HRESULT hr = AVIFileCreateStream(pfile, // file pointer
98 ps, // returned stream pointer
99 &strhdr); // stream header
100 if (hr != AVIERR_OK) {
101 return FALSE;
102 }
103
104 return TRUE;
105 }
106
107 string getFOURCCVAsString(DWORD value)
108 {
109 string returnValue = "";
110 if( value == 0 )
111 return returnValue;
112
113 DWORD ch0 = value & 0x000000FF;
114 returnValue.push_back((char) ch0);
115 DWORD ch1 = (value & 0x0000FF00)>>8;
116 returnValue.push_back((char) ch1);
117 DWORD ch2 = (value & 0x00FF0000)>>16;
118 returnValue.push_back((char) ch2);
119 DWORD ch3 = (value & 0xFF000000)>>24;
120 returnValue.push_back((char) ch3);
121
122 return returnValue;
123 }
124
125 string dumpAVICOMPRESSOPTIONS(AVICOMPRESSOPTIONS opts)
126 {
127 char tmp[255];
128 string returnValue = "Dump of AVICOMPRESSOPTIONS\n";
129
130 returnValue += "DWORD fccType = streamtype("; returnValue += getFOURCCVAsString(opts.fccType); returnValue += ")\n";
131 returnValue += "DWORD fccHandler = "; returnValue += getFOURCCVAsString(opts.fccHandler); returnValue += "\n";
132
133 _snprintf(tmp, 255, "DWORD dwKeyFrameEvery = %d\n", opts.dwKeyFrameEvery);
134 returnValue += tmp;
135
136 _snprintf(tmp, 255, "DWORD dwQuality = %d\n", opts.dwQuality);
137 returnValue += tmp;
138
139 _snprintf(tmp, 255, "DWORD dwBytesPerSecond = %d\n", opts.dwBytesPerSecond);
140 returnValue += tmp;
141
142 if((opts.dwFlags & AVICOMPRESSF_DATARATE) == AVICOMPRESSF_DATARATE){strcpy(tmp, "DWORD fccType = AVICOMPRESSF_DATARATE\n");}
143 else if((opts.dwFlags & AVICOMPRESSF_INTERLEAVE) == AVICOMPRESSF_INTERLEAVE){strcpy(tmp, "DWORD fccType = AVICOMPRESSF_INTERLEAVE\n");}
144 else if((opts.dwFlags & AVICOMPRESSF_KEYFRAMES) == AVICOMPRESSF_KEYFRAMES){strcpy(tmp, "DWORD fccType = AVICOMPRESSF_KEYFRAMES\n");}
145 else if((opts.dwFlags & AVICOMPRESSF_VALID) == AVICOMPRESSF_VALID){strcpy(tmp, "DWORD fccType = AVICOMPRESSF_VALID\n");}
146 else {_snprintf(tmp, 255, "DWORD dwFlags = Unknown(%d)\n", opts.dwFlags);}
147 returnValue += tmp;
148
149 _snprintf(tmp, 255, "LPVOID lpFormat = %d\n", (int)opts.lpFormat);
150 returnValue += tmp;
151
152 _snprintf(tmp, 255, "DWORD cbFormat = %d\n", opts.cbFormat);
153 returnValue += tmp;
154
155 _snprintf(tmp, 255, "LPVOID lpParms = %d\n", (int)opts.lpParms);
156 returnValue += tmp;
157
158 _snprintf(tmp, 255, "DWORD cbParms = %d\n", opts.cbParms);
159 returnValue += tmp;
160
161 _snprintf(tmp, 255, "DWORD dwInterleaveEvery = %d\n", opts.dwInterleaveEvery);
162 returnValue += tmp;
163
164 return returnValue;
165 }
166
167 BOOL AVI_SetOptions(PAVISTREAM * ps, PAVISTREAM * psCompressed, LPBITMAPINFOHEADER lpbi,
168 const char* _compressor)
169 {
170
171 AVICOMPRESSOPTIONS opts;
172 AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts};
173
174 memset(&opts, 0, sizeof(opts));
175 opts.fccType = streamtypeVIDEO;
176 opts.fccHandler = getFOURCC(_compressor);
177 //opts.fccHandler = 0;
178 //opts.fccHandler = mmioFOURCC('D','I','B',' '); // Uncompressed
179 //opts.fccHandler = mmioFOURCC('C','V','I','D'); // Cinpak
180 //opts.fccHandler = mmioFOURCC('I','V','3','2'); // Intel video 3.2
181 //opts.fccHandler = mmioFOURCC('M','S','V','C'); // Microsoft video 1
182 //opts.fccHandler = mmioFOURCC('I','V','5','0'); // Intel video 5.0
183 //opts.dwKeyFrameEvery = 5;
184 //opts.dwQuality
185 //opts.dwBytesPerSecond
186 //opts.dwFlags = AVICOMPRESSF_KEYFRAMES;
187 //opts.lpFormat
188 //opts.cbFormat
189 //opts.lpParms
190 //opts.cbParms
191 //opts.dwInterleaveEvery
192
193 /* display the compression options dialog box if specified compressor is unknown */
194 if(getFOURCC(_compressor) == NULL)
195 {
196 if (!AVISaveOptions(NULL, 0, 1, ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts))
197 {
198 return FALSE;
199 }
200
201 //printf("%s", dumpAVICOMPRESSOPTIONS(opts));
202 //MessageBox(NULL, dumpAVICOMPRESSOPTIONS(opts).c_str(), "AVICOMPRESSOPTIONS", MB_OK);
203 }
204
205 HRESULT hr = AVIMakeCompressedStream(psCompressed, *ps, &opts, NULL);
206 if (hr != AVIERR_OK) {
207 return FALSE;
208 }
209
210 hr = AVIStreamSetFormat(*psCompressed, 0,
211 lpbi, // stream format
212 lpbi->biSize // format size
213 + lpbi->biClrUsed * sizeof(RGBQUAD)
214 );
215 if (hr != AVIERR_OK) {
216 return FALSE;
217 }
218
219 return TRUE;
220 }
221
222 BOOL AVI_SetText(PAVIFILE pfile, PAVISTREAM psText, char *szText, int width, int height, int TextHeight)
223 {
224 // Fill in the stream header for the text stream....
225 AVISTREAMINFO strhdr;
226 DWORD dwTextFormat;
227 // The text stream is in 60ths of a second....
228
229 memset(&strhdr, 0, sizeof(strhdr));
230 strhdr.fccType = streamtypeTEXT;
231 strhdr.fccHandler = mmioFOURCC('D', 'R', 'A', 'W');
232 strhdr.dwScale = 1;
233 strhdr.dwRate = 60;
234 strhdr.dwSuggestedBufferSize = sizeof(szText);
235 SetRect(&strhdr.rcFrame, 0, (int) height,
236 (int) width, (int) height + TextHeight); // #define TEXT_HEIGHT 20
237
238 // ....and create the stream.
239 HRESULT hr = AVIFileCreateStream(pfile, &psText, &strhdr);
240 if (hr != AVIERR_OK) {
241 return FALSE;
242 }
243
244 dwTextFormat = sizeof(dwTextFormat);
245 hr = AVIStreamSetFormat(psText, 0, &dwTextFormat, sizeof(dwTextFormat));
246 if (hr != AVIERR_OK) {
247 return FALSE;
248 }
249
250 return TRUE;
251 }
252
253 BOOL AVI_AddFrame(PAVISTREAM psCompressed, int time, LPBITMAPINFOHEADER lpbi)
254 {
255 int ImageSize = lpbi->biSizeImage;
256 if (ImageSize == 0)
257 {
258 if (lpbi->biBitCount == 24)
259 {
260 ImageSize = lpbi->biWidth * lpbi->biHeight * 3;
261 }
262 }
263 HRESULT hr = AVIStreamWrite(psCompressed, // stream pointer
264 time, // time of this frame
265 1, // number to write
266 (LPBYTE) lpbi + // pointer to data
267 lpbi->biSize +
268 lpbi->biClrUsed * sizeof(RGBQUAD),
269 ImageSize, // lpbi->biSizeImage, // size of this frame
270 AVIIF_KEYFRAME, // flags....
271 NULL,
272 NULL);
273 if (hr != AVIERR_OK)
274 {
275 char strMsg[255];
276 _snprintf(strMsg, 255, "Error: AVIStreamWrite, error %d",hr);
277 MessageBox(NULL, strMsg, "", MB_OK);
278 return FALSE;
279 }
280
281 return TRUE;
282 }
283
284 BOOL AVI_AddText(PAVISTREAM psText, int time, char *szText)
285 {
286 int iLen = (int)strlen(szText);
287
288 HRESULT hr = AVIStreamWrite(psText,
289 time,
290 1,
291 szText,
292 iLen + 1,
293 AVIIF_KEYFRAME,
294 NULL,
295 NULL);
296 if (hr != AVIERR_OK)
297 return FALSE;
298
299 return TRUE;
300 }
301
302 BOOL AVI_CloseStream(PAVISTREAM ps, PAVISTREAM psCompressed, PAVISTREAM psText)
303 {
304 if (ps)
305 AVIStreamClose(ps);
306
307 if (psCompressed)
308 AVIStreamClose(psCompressed);
309
310 if (psText)
311 AVIStreamClose(psText);
312
313
314
315 return TRUE;
316 }
317
318 BOOL AVI_CloseFile(PAVIFILE pfile)
319 {
320 if (pfile)
321 AVIFileClose(pfile);
322
323 return TRUE;
324 }
325
326 BOOL AVI_Exit()
327 {
328 AVIFileExit();
329
330 return TRUE;
331 }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 /* Here are the additional functions we need! */
349
350
351 static PAVIFILE pfile = NULL;
352 static PAVISTREAM ps = NULL;
353 static PAVISTREAM psCompressed = NULL;
354 static int avi_count = 0;
355
356
357 // Initialization...
358 bool START_AVI(const char* file_name)
359 {
360 if(! AVI_Init())
361 {
362 //printf("Error - AVI_Init()\n");
363 return false;
364 }
365
366 if(! AVI_FileOpenWrite(&pfile, file_name))
367 {
368 //printf("Error - AVI_FileOpenWrite()\n");
369 return false;
370 }
371
372 return true;
373 }
374
375 bool ADD_FRAME_FROM_DIB_TO_AVI(const char* _compressor, int _frameRate, int width, int height, int bits, void* pdata)
376 {
377 if(avi_count == 0)
378 {
379 if(! AVI_CreateStream(pfile, &ps, _frameRate,
380 width*height/bits,
381 width,
382 height, _compressor))
383 {
384 //printf("Error - AVI_CreateStream()\n");
385 return false;
386 }
387
388 BITMAPINFOHEADER bi;
389 memset(&bi, 0, sizeof(bi));
390 bi.biSize = sizeof(BITMAPINFOHEADER);
391 bi.biWidth = width;
392 bi.biHeight = height;
393 bi.biPlanes = 1;
394 bi.biBitCount = bits;
395 bi.biCompression = BI_RGB;
396 bi.biSizeImage = width * height * bits /8;
397 if(! AVI_SetOptions(&ps, &psCompressed, &bi, _compressor))
398 {
399 return false;
400 }
401 }
402
403 HRESULT hr = AVIStreamWrite(psCompressed, // stream pointer
404 avi_count, // time of this frame
405 1, // number to write
406 pdata,
407 width*height/8, // lpbi->biSizeImage, // size of this frame
408 AVIIF_KEYFRAME, // flags....
409 NULL,
410 NULL);
411 if (hr != AVIERR_OK)
412 {
413 char strMsg[255];
414 _snprintf(strMsg, 255, "Error: AVIStreamWrite, error %d",hr);
415 MessageBox(NULL, strMsg, "", MB_OK);
416 return FALSE;
417 }
418
419 avi_count++;
420 return true;
421 }
422
423 //Now we can add frames
424 // ie. ADD_FRAME_FROM_DIB_TO_AVI(yourDIB, "CVID", 25);
425 bool ADD_FRAME_FROM_DIB_TO_AVI(HANDLE dib, const char* _compressor, int _frameRate)
426 {
427 LPBITMAPINFOHEADER lpbi;
428 if(avi_count == 0)
429 {
430 lpbi = (LPBITMAPINFOHEADER)GlobalLock(dib);
431 if(! AVI_CreateStream(pfile, &ps, _frameRate,
432 (unsigned long) lpbi->biSizeImage,
433 (int) lpbi->biWidth,
434 (int) lpbi->biHeight, _compressor))
435 {
436 //printf("Error - AVI_CreateStream()\n");
437 GlobalUnlock(lpbi);
438 return false;
439 }
440
441 if(! AVI_SetOptions(&ps, &psCompressed, lpbi, _compressor))
442 {
443 //printf("Error - AVI_SetOptions()\n");
444 GlobalUnlock(lpbi);
445 return false;
446 }
447
448 GlobalUnlock(lpbi);
449 }
450
451 lpbi = (LPBITMAPINFOHEADER)GlobalLock(dib);
452 if(! AVI_AddFrame(psCompressed, avi_count * 1, lpbi))
453 {
454 //printf("Error - AVI_AddFrame()\n");
455 GlobalUnlock(lpbi);
456 return false;
457 }
458
459 GlobalUnlock(lpbi);
460 avi_count++;
461 return true;
462 }
463
464 // The end...
465 bool STOP_AVI()
466 {
467 if(! AVI_CloseStream(ps, psCompressed, NULL))
468 {
469 //printf("Error - AVI_CloseStream()\n");
470 return false;
471 }
472
473 if(! AVI_CloseFile(pfile))
474 {
475 //printf("Error - AVI_CloseFile()\n");
476 return false;
477 }
478
479 if(! AVI_Exit())
480 {
481 //printf("Error - AVI_Exit()\n");
482 return false;
483 }
484
485 return true;
486 }
487
488 #endif

  ViewVC Help
Powered by ViewVC 1.1.22