Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videoio/src/cap_vfw.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "precomp.hpp"
43
44
#include <vfw.h>
45
46
#ifdef __GNUC__
47
#define WM_CAP_FIRSTA (WM_USER)
48
#define capSendMessage(hwnd,m,w,l) (IsWindow(hwnd)?SendMessage(hwnd,m,w,l):0)
49
#endif
50
51
#if defined _M_X64 && defined _MSC_VER
52
#pragma optimize("",off)
53
#pragma warning(disable: 4748)
54
#endif
55
56
/********************* Capturing video from AVI via VFW ************************/
57
58
static BITMAPINFOHEADER icvBitmapHeader( int width, int height, int bpp, int compression = BI_RGB )
59
{
60
BITMAPINFOHEADER bmih;
61
memset( &bmih, 0, sizeof(bmih));
62
bmih.biSize = sizeof(bmih);
63
bmih.biWidth = width;
64
bmih.biHeight = height;
65
bmih.biBitCount = (WORD)bpp;
66
bmih.biCompression = compression;
67
bmih.biPlanes = 1;
68
69
return bmih;
70
}
71
72
73
static void icvInitCapture_VFW()
74
{
75
static int isInitialized = 0;
76
if( !isInitialized )
77
{
78
AVIFileInit();
79
isInitialized = 1;
80
}
81
}
82
83
84
class CvCaptureAVI_VFW : public CvCapture
85
{
86
public:
87
CvCaptureAVI_VFW()
88
{
89
CoInitialize(NULL);
90
init();
91
}
92
93
virtual ~CvCaptureAVI_VFW()
94
{
95
close();
96
CoUninitialize();
97
}
98
99
virtual bool open( const char* filename );
100
virtual void close();
101
102
virtual double getProperty(int) const CV_OVERRIDE;
103
virtual bool setProperty(int, double) CV_OVERRIDE;
104
virtual bool grabFrame() CV_OVERRIDE;
105
virtual IplImage* retrieveFrame(int) CV_OVERRIDE;
106
virtual int getCaptureDomain() CV_OVERRIDE { return CV_CAP_VFW; }
107
108
protected:
109
void init();
110
111
PAVIFILE avifile;
112
PAVISTREAM avistream;
113
PGETFRAME getframe;
114
AVISTREAMINFO aviinfo;
115
BITMAPINFOHEADER * bmih;
116
CvSlice film_range;
117
double fps;
118
int pos;
119
IplImage* frame;
120
CvSize size;
121
};
122
123
124
void CvCaptureAVI_VFW::init()
125
{
126
avifile = 0;
127
avistream = 0;
128
getframe = 0;
129
memset( &aviinfo, 0, sizeof(aviinfo) );
130
bmih = 0;
131
film_range = cvSlice(0,0);
132
fps = 0;
133
pos = 0;
134
frame = 0;
135
size = cvSize(0,0);
136
}
137
138
139
void CvCaptureAVI_VFW::close()
140
{
141
if( getframe )
142
AVIStreamGetFrameClose( getframe );
143
144
if( avistream )
145
AVIStreamRelease( avistream );
146
147
if( avifile )
148
AVIFileRelease( avifile );
149
150
if (frame)
151
cvReleaseImage( &frame );
152
153
init();
154
}
155
156
157
bool CvCaptureAVI_VFW::open( const char* filename )
158
{
159
close();
160
icvInitCapture_VFW();
161
162
if( !filename )
163
return false;
164
165
HRESULT hr = AVIFileOpen( &avifile, filename, OF_READ, NULL );
166
if( SUCCEEDED(hr))
167
{
168
hr = AVIFileGetStream( avifile, &avistream, streamtypeVIDEO, 0 );
169
if( SUCCEEDED(hr))
170
{
171
hr = AVIStreamInfo( avistream, &aviinfo, sizeof(aviinfo));
172
if( SUCCEEDED(hr))
173
{
174
size.width = aviinfo.rcFrame.right - aviinfo.rcFrame.left;
175
size.height = aviinfo.rcFrame.bottom - aviinfo.rcFrame.top;
176
BITMAPINFOHEADER bmihdr = icvBitmapHeader( size.width, size.height, 24 );
177
178
film_range.start_index = (int)aviinfo.dwStart;
179
film_range.end_index = film_range.start_index + (int)aviinfo.dwLength;
180
fps = (double)aviinfo.dwRate/aviinfo.dwScale;
181
pos = film_range.start_index;
182
getframe = AVIStreamGetFrameOpen( avistream, &bmihdr );
183
if( getframe != 0 )
184
return true;
185
186
// Attempt to open as 8-bit AVI.
187
bmihdr = icvBitmapHeader( size.width, size.height, 8);
188
getframe = AVIStreamGetFrameOpen( avistream, &bmihdr );
189
if( getframe != 0 )
190
return true;
191
}
192
}
193
}
194
195
close();
196
return false;
197
}
198
199
bool CvCaptureAVI_VFW::grabFrame()
200
{
201
if( avistream )
202
bmih = (BITMAPINFOHEADER*)AVIStreamGetFrame( getframe, pos++ );
203
return bmih != 0;
204
}
205
206
IplImage* CvCaptureAVI_VFW::retrieveFrame(int)
207
{
208
if( avistream && bmih )
209
{
210
bool isColor = bmih->biBitCount == 24;
211
int nChannels = (isColor) ? 3 : 1;
212
IplImage src;
213
cvInitImageHeader( &src, cvSize( bmih->biWidth, bmih->biHeight ),
214
IPL_DEPTH_8U, nChannels, IPL_ORIGIN_BL, 4 );
215
216
char* dataPtr = (char*)(bmih + 1);
217
218
// Only account for the color map size if we are an 8-bit image and the color map is used
219
if (!isColor)
220
{
221
static int RGBQUAD_SIZE_PER_BYTE = sizeof(RGBQUAD)/sizeof(BYTE);
222
int offsetFromColormapToData = (int)bmih->biClrUsed*RGBQUAD_SIZE_PER_BYTE;
223
dataPtr += offsetFromColormapToData;
224
}
225
226
cvSetData( &src, dataPtr, src.widthStep );
227
228
if( !frame || frame->width != src.width || frame->height != src.height )
229
{
230
cvReleaseImage( &frame );
231
frame = cvCreateImage( cvGetSize(&src), 8, nChannels );
232
}
233
234
cvFlip( &src, frame, 0 );
235
return frame;
236
}
237
238
return 0;
239
}
240
241
double CvCaptureAVI_VFW::getProperty( int property_id ) const
242
{
243
switch( property_id )
244
{
245
case CV_CAP_PROP_POS_MSEC:
246
return cvRound(pos*1000./fps);
247
case CV_CAP_PROP_POS_FRAMES:
248
return pos;
249
case CV_CAP_PROP_POS_AVI_RATIO:
250
return (pos - film_range.start_index)/
251
(film_range.end_index - film_range.start_index + 1e-10);
252
case CV_CAP_PROP_FRAME_WIDTH:
253
return size.width;
254
case CV_CAP_PROP_FRAME_HEIGHT:
255
return size.height;
256
case CV_CAP_PROP_FPS:
257
return fps;
258
case CV_CAP_PROP_FOURCC:
259
return aviinfo.fccHandler;
260
case CV_CAP_PROP_FRAME_COUNT:
261
return film_range.end_index - film_range.start_index;
262
}
263
return 0;
264
}
265
266
bool CvCaptureAVI_VFW::setProperty( int property_id, double value )
267
{
268
switch( property_id )
269
{
270
case CV_CAP_PROP_POS_MSEC:
271
case CV_CAP_PROP_POS_FRAMES:
272
case CV_CAP_PROP_POS_AVI_RATIO:
273
{
274
switch( property_id )
275
{
276
case CV_CAP_PROP_POS_MSEC:
277
pos = cvRound(value*fps*0.001);
278
break;
279
case CV_CAP_PROP_POS_AVI_RATIO:
280
pos = cvRound(value*(film_range.end_index -
281
film_range.start_index) +
282
film_range.start_index);
283
break;
284
default:
285
pos = cvRound(value);
286
}
287
if( pos < film_range.start_index )
288
pos = film_range.start_index;
289
if( pos > film_range.end_index )
290
pos = film_range.end_index;
291
}
292
break;
293
default:
294
return false;
295
}
296
297
return true;
298
}
299
300
CvCapture* cvCreateFileCapture_VFW (const char* filename)
301
{
302
CvCaptureAVI_VFW* capture = new CvCaptureAVI_VFW;
303
if( capture->open(filename) )
304
return capture;
305
delete capture;
306
return 0;
307
}
308
309
310
/********************* Capturing video from camera via VFW *********************/
311
312
class CvCaptureCAM_VFW : public CvCapture
313
{
314
public:
315
CvCaptureCAM_VFW()
316
{
317
CoInitialize(NULL);
318
init();
319
}
320
virtual ~CvCaptureCAM_VFW()
321
{
322
close();
323
CoUninitialize();
324
}
325
326
virtual bool open( int index );
327
virtual void close();
328
virtual double getProperty(int) const;
329
virtual bool setProperty(int, double);
330
virtual bool grabFrame();
331
virtual IplImage* retrieveFrame(int);
332
virtual int getCaptureDomain() { return CV_CAP_VFW; } // Return the type of the capture object: CV_CAP_VFW, etc...
333
334
protected:
335
void init();
336
void closeHIC();
337
static LRESULT PASCAL frameCallback( HWND hWnd, VIDEOHDR* hdr );
338
339
CAPDRIVERCAPS caps;
340
HWND capWnd;
341
VIDEOHDR* hdr;
342
DWORD fourcc;
343
int width, height;
344
int widthSet, heightSet;
345
HIC hic;
346
IplImage* frame;
347
};
348
349
350
void CvCaptureCAM_VFW::init()
351
{
352
memset( &caps, 0, sizeof(caps) );
353
capWnd = 0;
354
hdr = 0;
355
fourcc = 0;
356
hic = 0;
357
frame = 0;
358
width = height = -1;
359
widthSet = heightSet = 0;
360
}
361
362
void CvCaptureCAM_VFW::closeHIC()
363
{
364
if( hic )
365
{
366
ICDecompressEnd( hic );
367
ICClose( hic );
368
hic = 0;
369
}
370
}
371
372
373
LRESULT PASCAL CvCaptureCAM_VFW::frameCallback( HWND hWnd, VIDEOHDR* hdr )
374
{
375
CvCaptureCAM_VFW* capture = 0;
376
377
if (!hWnd) return FALSE;
378
379
capture = (CvCaptureCAM_VFW*)capGetUserData(hWnd);
380
if (!capture) return (LRESULT)FALSE;
381
capture->hdr = hdr;
382
return (LRESULT)TRUE;
383
}
384
385
386
// Initialize camera input
387
bool CvCaptureCAM_VFW::open( int wIndex )
388
{
389
char szDeviceName[80];
390
char szDeviceVersion[80];
391
HWND hWndC = 0;
392
393
close();
394
395
if( (unsigned)wIndex >= 10 )
396
wIndex = 0;
397
398
for( ; wIndex < 10; wIndex++ )
399
{
400
if( capGetDriverDescription( wIndex, szDeviceName,
401
sizeof (szDeviceName), szDeviceVersion,
402
sizeof (szDeviceVersion)))
403
{
404
hWndC = capCreateCaptureWindow ( "My Own Capture Window",
405
WS_POPUP | WS_CHILD, 0, 0, 320, 240, 0, 0);
406
if( capDriverConnect (hWndC, wIndex))
407
break;
408
DestroyWindow( hWndC );
409
hWndC = 0;
410
}
411
}
412
413
if( hWndC )
414
{
415
capWnd = hWndC;
416
hdr = 0;
417
hic = 0;
418
fourcc = (DWORD)-1;
419
420
memset( &caps, 0, sizeof(caps));
421
capDriverGetCaps( hWndC, &caps, sizeof(caps));
422
CAPSTATUS status = {};
423
capGetStatus(hWndC, &status, sizeof(status));
424
::SetWindowPos(hWndC, NULL, 0, 0, status.uiImageWidth, status.uiImageHeight, SWP_NOZORDER|SWP_NOMOVE);
425
capSetUserData( hWndC, (size_t)this );
426
capSetCallbackOnFrame( hWndC, frameCallback );
427
CAPTUREPARMS p;
428
capCaptureGetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
429
p.dwRequestMicroSecPerFrame = 66667/2; // 30 FPS
430
capCaptureSetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
431
//capPreview( hWndC, 1 );
432
capPreviewScale(hWndC,FALSE);
433
capPreviewRate(hWndC,1);
434
435
// Get frame initial parameters.
436
const DWORD size = capGetVideoFormatSize(capWnd);
437
if( size > 0 )
438
{
439
unsigned char *pbi = new unsigned char[size];
440
if( pbi )
441
{
442
if( capGetVideoFormat(capWnd, pbi, size) == size )
443
{
444
BITMAPINFOHEADER& vfmt = ((BITMAPINFO*)pbi)->bmiHeader;
445
widthSet = vfmt.biWidth;
446
heightSet = vfmt.biHeight;
447
fourcc = vfmt.biCompression;
448
}
449
delete []pbi;
450
}
451
}
452
// And alternative way in case of failure.
453
if( widthSet == 0 || heightSet == 0 )
454
{
455
widthSet = status.uiImageWidth;
456
heightSet = status.uiImageHeight;
457
}
458
459
}
460
return capWnd != 0;
461
}
462
463
464
void CvCaptureCAM_VFW::close()
465
{
466
if( capWnd )
467
{
468
capSetCallbackOnFrame( capWnd, NULL );
469
capDriverDisconnect( capWnd );
470
DestroyWindow( capWnd );
471
closeHIC();
472
}
473
cvReleaseImage( &frame );
474
init();
475
}
476
477
478
bool CvCaptureCAM_VFW::grabFrame()
479
{
480
if( capWnd )
481
return capGrabFrameNoStop(capWnd) == TRUE;
482
483
return false;
484
}
485
486
487
IplImage* CvCaptureCAM_VFW::retrieveFrame(int)
488
{
489
BITMAPINFO vfmt;
490
memset( &vfmt, 0, sizeof(vfmt));
491
BITMAPINFOHEADER& vfmt0 = vfmt.bmiHeader;
492
493
if( !capWnd )
494
return 0;
495
496
const DWORD sz = capGetVideoFormat( capWnd, &vfmt, sizeof(vfmt));
497
const int prevWidth = frame ? frame->width : 0;
498
const int prevHeight = frame ? frame->height : 0;
499
500
if( !hdr || hdr->lpData == 0 || sz == 0 )
501
return 0;
502
503
if( !frame || frame->width != vfmt0.biWidth || frame->height != vfmt0.biHeight )
504
{
505
cvReleaseImage( &frame );
506
frame = cvCreateImage( cvSize( vfmt0.biWidth, vfmt0.biHeight ), 8, 3 );
507
}
508
509
if ( vfmt0.biCompression == MAKEFOURCC('N','V','1','2') )
510
{
511
// Frame is in YUV 4:2:0 NV12 format, convert to BGR color space
512
// See https://msdn.microsoft.com/en-us/library/windows/desktop/dd206750(v=vs.85).aspx#nv12)
513
IplImage src;
514
cvInitImageHeader( &src, cvSize( vfmt0.biWidth, vfmt0.biHeight * 3 / 2 ), IPL_DEPTH_8U, 1, IPL_ORIGIN_BL, 4 );
515
cvSetData( &src, hdr->lpData, src.widthStep );
516
cvCvtColor( &src, frame, CV_YUV2BGR_NV12 );
517
}
518
else if( vfmt0.biCompression != BI_RGB ||
519
vfmt0.biBitCount != 24 )
520
{
521
BITMAPINFOHEADER vfmt1 = icvBitmapHeader( vfmt0.biWidth, vfmt0.biHeight, 24 );
522
523
if( hic == 0 || fourcc != vfmt0.biCompression ||
524
prevWidth != vfmt0.biWidth || prevHeight != vfmt0.biHeight )
525
{
526
closeHIC();
527
hic = ICOpen( MAKEFOURCC('V','I','D','C'),
528
vfmt0.biCompression, ICMODE_DECOMPRESS );
529
if( hic )
530
{
531
if( ICDecompressBegin( hic, &vfmt0, &vfmt1 ) != ICERR_OK )
532
{
533
closeHIC();
534
return 0;
535
}
536
}
537
}
538
539
if( !hic || ICDecompress( hic, 0, &vfmt0, hdr->lpData,
540
&vfmt1, frame->imageData ) != ICERR_OK )
541
{
542
closeHIC();
543
return 0;
544
}
545
546
cvFlip( frame, frame, 0 );
547
}
548
else
549
{
550
IplImage src;
551
cvInitImageHeader( &src, cvSize(vfmt0.biWidth, vfmt0.biHeight),
552
IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 );
553
cvSetData( &src, hdr->lpData, src.widthStep );
554
cvFlip( &src, frame, 0 );
555
}
556
557
return frame;
558
}
559
560
561
double CvCaptureCAM_VFW::getProperty( int property_id ) const
562
{
563
switch( property_id )
564
{
565
case CV_CAP_PROP_FRAME_WIDTH:
566
return widthSet;
567
case CV_CAP_PROP_FRAME_HEIGHT:
568
return heightSet;
569
case CV_CAP_PROP_FOURCC:
570
return fourcc;
571
case CV_CAP_PROP_FPS:
572
{
573
CAPTUREPARMS params = {};
574
if( capCaptureGetSetup(capWnd, &params, sizeof(params)) )
575
return 1e6 / params.dwRequestMicroSecPerFrame;
576
}
577
break;
578
default:
579
break;
580
}
581
return 0;
582
}
583
584
bool CvCaptureCAM_VFW::setProperty(int property_id, double value)
585
{
586
bool handledSize = false;
587
588
switch( property_id )
589
{
590
case CV_CAP_PROP_FRAME_WIDTH:
591
width = cvRound(value);
592
handledSize = true;
593
break;
594
case CV_CAP_PROP_FRAME_HEIGHT:
595
height = cvRound(value);
596
handledSize = true;
597
break;
598
case CV_CAP_PROP_FOURCC:
599
break;
600
case CV_CAP_PROP_FPS:
601
if( value > 0 )
602
{
603
CAPTUREPARMS params;
604
if( capCaptureGetSetup(capWnd, &params, sizeof(params)) )
605
{
606
params.dwRequestMicroSecPerFrame = cvRound(1e6/value);
607
return capCaptureSetSetup(capWnd, &params, sizeof(params)) == TRUE;
608
}
609
}
610
break;
611
default:
612
break;
613
}
614
615
if ( handledSize )
616
{
617
// If both width and height are set then change frame size.
618
if( width > 0 && height > 0 )
619
{
620
const DWORD size = capGetVideoFormatSize(capWnd);
621
if( size == 0 )
622
return false;
623
624
unsigned char *pbi = new unsigned char[size];
625
if( !pbi )
626
return false;
627
628
if( capGetVideoFormat(capWnd, pbi, size) != size )
629
{
630
delete []pbi;
631
return false;
632
}
633
634
BITMAPINFOHEADER& vfmt = ((BITMAPINFO*)pbi)->bmiHeader;
635
bool success = true;
636
if( width != vfmt.biWidth || height != vfmt.biHeight )
637
{
638
// Change frame size.
639
vfmt.biWidth = width;
640
vfmt.biHeight = height;
641
vfmt.biSizeImage = height * ((width * vfmt.biBitCount + 31) / 32) * 4;
642
vfmt.biCompression = BI_RGB;
643
success = capSetVideoFormat(capWnd, pbi, size) == TRUE;
644
}
645
if( success )
646
{
647
// Adjust capture window size.
648
CAPSTATUS status = {};
649
capGetStatus(capWnd, &status, sizeof(status));
650
::SetWindowPos(capWnd, NULL, 0, 0, status.uiImageWidth, status.uiImageHeight, SWP_NOZORDER|SWP_NOMOVE);
651
// Store frame size.
652
widthSet = width;
653
heightSet = height;
654
}
655
delete []pbi;
656
width = height = -1;
657
658
return success;
659
}
660
661
return true;
662
}
663
664
return false;
665
}
666
667
CvCapture* cvCreateCameraCapture_VFW( int index )
668
{
669
CvCaptureCAM_VFW* capture = new CvCaptureCAM_VFW;
670
671
if( capture->open( index ))
672
return capture;
673
674
delete capture;
675
return 0;
676
}
677
678
679
/*************************** writing AVIs ******************************/
680
681
class CvVideoWriter_VFW : public CvVideoWriter
682
{
683
public:
684
CvVideoWriter_VFW()
685
{
686
CoInitialize(NULL);
687
init();
688
}
689
virtual ~CvVideoWriter_VFW()
690
{
691
close();
692
CoUninitialize();
693
}
694
695
virtual bool open( const char* filename, int fourcc,
696
double fps, CvSize frameSize, bool isColor );
697
virtual void close();
698
virtual bool writeFrame( const IplImage* );
699
700
int getCaptureDomain() const CV_OVERRIDE { return cv::CAP_VFW; }
701
protected:
702
void init();
703
bool createStreams( CvSize frameSize, bool isColor );
704
705
PAVIFILE avifile;
706
PAVISTREAM compressed;
707
PAVISTREAM uncompressed;
708
double fps;
709
IplImage* tempFrame;
710
long pos;
711
int fourcc;
712
};
713
714
715
void CvVideoWriter_VFW::init()
716
{
717
avifile = 0;
718
compressed = uncompressed = 0;
719
fps = 0;
720
tempFrame = 0;
721
pos = 0;
722
fourcc = 0;
723
}
724
725
void CvVideoWriter_VFW::close()
726
{
727
if( uncompressed )
728
AVIStreamRelease( uncompressed );
729
if( compressed )
730
AVIStreamRelease( compressed );
731
if( avifile )
732
AVIFileRelease( avifile );
733
cvReleaseImage( &tempFrame );
734
init();
735
}
736
737
738
// philipg. Made this code capable of writing 8bpp gray scale bitmaps
739
struct BITMAPINFO_8Bit
740
{
741
BITMAPINFOHEADER bmiHeader;
742
RGBQUAD bmiColors[256];
743
};
744
745
746
bool CvVideoWriter_VFW::open( const char* filename, int _fourcc, double _fps, CvSize frameSize, bool isColor )
747
{
748
close();
749
750
icvInitCapture_VFW();
751
if( AVIFileOpen( &avifile, filename, OF_CREATE | OF_WRITE, 0 ) == AVIERR_OK )
752
{
753
fourcc = _fourcc;
754
fps = _fps;
755
if( frameSize.width > 0 && frameSize.height > 0 &&
756
!createStreams( frameSize, isColor ) )
757
{
758
close();
759
return false;
760
}
761
return true;
762
}
763
else
764
return false;
765
}
766
767
768
bool CvVideoWriter_VFW::createStreams( CvSize frameSize, bool isColor )
769
{
770
if( !avifile )
771
return false;
772
AVISTREAMINFO aviinfo;
773
774
BITMAPINFO_8Bit bmih;
775
bmih.bmiHeader = icvBitmapHeader( frameSize.width, frameSize.height, isColor ? 24 : 8 );
776
for( int i = 0; i < 256; i++ )
777
{
778
bmih.bmiColors[i].rgbBlue = (BYTE)i;
779
bmih.bmiColors[i].rgbGreen = (BYTE)i;
780
bmih.bmiColors[i].rgbRed = (BYTE)i;
781
bmih.bmiColors[i].rgbReserved = 0;
782
}
783
784
memset( &aviinfo, 0, sizeof(aviinfo));
785
aviinfo.fccType = streamtypeVIDEO;
786
aviinfo.fccHandler = 0;
787
// use highest possible accuracy for dwRate/dwScale
788
aviinfo.dwScale = (DWORD)((double)0x7FFFFFFF / fps);
789
aviinfo.dwRate = cvRound(fps * aviinfo.dwScale);
790
aviinfo.rcFrame.top = aviinfo.rcFrame.left = 0;
791
aviinfo.rcFrame.right = frameSize.width;
792
aviinfo.rcFrame.bottom = frameSize.height;
793
794
if( AVIFileCreateStream( avifile, &uncompressed, &aviinfo ) == AVIERR_OK )
795
{
796
AVICOMPRESSOPTIONS copts, *pcopts = &copts;
797
copts.fccType = streamtypeVIDEO;
798
copts.fccHandler = fourcc != -1 ? fourcc : 0;
799
copts.dwKeyFrameEvery = 1;
800
copts.dwQuality = 10000;
801
copts.dwBytesPerSecond = 0;
802
copts.dwFlags = AVICOMPRESSF_VALID;
803
copts.lpFormat = &bmih;
804
copts.cbFormat = (isColor ? sizeof(BITMAPINFOHEADER) : sizeof(bmih));
805
copts.lpParms = 0;
806
copts.cbParms = 0;
807
copts.dwInterleaveEvery = 0;
808
809
if( fourcc != -1 || AVISaveOptions( 0, 0, 1, &uncompressed, &pcopts ) == TRUE )
810
{
811
if( AVIMakeCompressedStream( &compressed, uncompressed, pcopts, 0 ) == AVIERR_OK &&
812
AVIStreamSetFormat( compressed, 0, &bmih, sizeof(bmih)) == AVIERR_OK )
813
{
814
fps = fps;
815
fourcc = (int)copts.fccHandler;
816
frameSize = frameSize;
817
tempFrame = cvCreateImage( frameSize, 8, (isColor ? 3 : 1) );
818
return true;
819
}
820
}
821
}
822
return false;
823
}
824
825
826
bool CvVideoWriter_VFW::writeFrame( const IplImage* image )
827
{
828
bool result = false;
829
CV_FUNCNAME( "CvVideoWriter_VFW::writeFrame" );
830
831
__BEGIN__;
832
833
if( !image )
834
EXIT;
835
836
if( !compressed && !createStreams( cvGetSize(image), image->nChannels > 1 ))
837
EXIT;
838
839
if( image->width != tempFrame->width || image->height != tempFrame->height )
840
CV_ERROR( CV_StsUnmatchedSizes,
841
"image size is different from the currently set frame size" );
842
843
if( image->nChannels != tempFrame->nChannels ||
844
image->depth != tempFrame->depth ||
845
image->origin == 0 ||
846
image->widthStep != cvAlign(image->width*image->nChannels*((image->depth & 255)/8), 4))
847
{
848
cvConvertImage( image, tempFrame, image->origin == 0 ? CV_CVTIMG_FLIP : 0 );
849
image = (const IplImage*)tempFrame;
850
}
851
852
result = AVIStreamWrite( compressed, pos++, 1, image->imageData,
853
image->imageSize, AVIIF_KEYFRAME, 0, 0 ) == AVIERR_OK;
854
855
__END__;
856
857
return result;
858
}
859
860
CvVideoWriter* cvCreateVideoWriter_VFW( const char* filename, int fourcc,
861
double fps, CvSize frameSize, int isColor )
862
{
863
CvVideoWriter_VFW* writer = new CvVideoWriter_VFW;
864
if( writer->open( filename, fourcc, fps, frameSize, isColor != 0 ))
865
return writer;
866
delete writer;
867
return 0;
868
}
869
870