Update to 2.0.0 tree from current Fremantle build
[opencv] / src / highgui / cvcap_vfw.cpp
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 "_highgui.h"
43
44 #include <vfw.h>
45
46 #if _MSC_VER >= 1200
47 #pragma warning( disable: 4711 )
48 #endif
49
50 #ifdef __GNUC__
51 #define WM_CAP_FIRSTA              (WM_USER)
52 #define capSendMessage(hwnd,m,w,l) (IsWindow(hwnd)?SendMessage(hwnd,m,w,l):0)
53 #endif
54
55 #if (defined WIN64 || defined _WIN64) && defined _MSC_VER && !defined __ICL
56 #pragma optimize("",off)
57 #endif
58
59 /********************* Capturing video from AVI via VFW ************************/
60
61 static BITMAPINFOHEADER icvBitmapHeader( int width, int height, int bpp, int compression = BI_RGB )
62 {
63     BITMAPINFOHEADER bmih;
64     memset( &bmih, 0, sizeof(bmih));
65     bmih.biSize = sizeof(bmih);
66     bmih.biWidth = width;
67     bmih.biHeight = height;
68     bmih.biBitCount = (WORD)bpp;
69     bmih.biCompression = compression;
70     bmih.biPlanes = 1;
71
72     return bmih;
73 }
74
75
76 static void icvInitCapture_VFW()
77 {
78     static int isInitialized = 0;
79     if( !isInitialized )
80     {
81         AVIFileInit();
82         isInitialized = 1;
83     }
84 }
85
86
87 class CvCaptureAVI_VFW : public CvCapture
88 {
89 public:
90     CvCaptureAVI_VFW()
91     {
92       CoInitialize(NULL);
93       init();
94     }
95
96     virtual ~CvCaptureAVI_VFW()
97     {
98         close();
99         CoUninitialize();
100     }
101
102     virtual bool open( const char* filename );
103     virtual void close();
104
105     virtual double getProperty(int);
106     virtual bool setProperty(int, double);
107     virtual bool grabFrame();
108     virtual IplImage* retrieveFrame(int);
109         virtual int getCaptureDomain() { return CV_CAP_VFW; } // Return the type of the capture object: CV_CAP_VFW, etc...
110
111 protected:
112     void init();
113
114     PAVIFILE            avifile;
115     PAVISTREAM          avistream;
116     PGETFRAME           getframe;
117     AVISTREAMINFO       aviinfo;
118     BITMAPINFOHEADER  * bmih;
119     CvSlice             film_range;
120     double              fps;
121     int                 pos;
122     IplImage*           frame;
123     CvSize              size;
124 };
125
126
127 void CvCaptureAVI_VFW::init()
128 {
129     avifile = 0;
130     avistream = 0;
131     getframe = 0;
132     memset( &aviinfo, 0, sizeof(aviinfo) );
133     bmih = 0;
134     film_range = cvSlice(0,0);
135     fps = 0;
136     pos = 0;
137     frame = 0;
138     size = cvSize(0,0);
139 }
140
141
142 void CvCaptureAVI_VFW::close()
143 {
144     if( getframe )
145         AVIStreamGetFrameClose( getframe );
146
147     if( avistream )
148         AVIStreamRelease( avistream );
149
150     if( avifile )
151         AVIFileRelease( avifile );
152
153     if (frame)
154         cvReleaseImage( &frame );
155
156     init();
157 }
158
159
160 bool CvCaptureAVI_VFW::open( const char* filename )
161 {
162     close();
163     icvInitCapture_VFW();
164
165     if( !filename )
166         return false;
167
168     HRESULT hr = AVIFileOpen( &avifile, filename, OF_READ, NULL );
169     if( SUCCEEDED(hr))
170     {
171         hr = AVIFileGetStream( avifile, &avistream, streamtypeVIDEO, 0 );
172         if( SUCCEEDED(hr))
173         {
174             hr = AVIStreamInfo( avistream, &aviinfo, sizeof(aviinfo));
175             if( SUCCEEDED(hr))
176             {
177                 size.width = aviinfo.rcFrame.right - aviinfo.rcFrame.left;
178                 size.height = aviinfo.rcFrame.bottom - aviinfo.rcFrame.top;
179                 BITMAPINFOHEADER bmih = icvBitmapHeader( size.width, size.height, 24 );
180
181                 film_range.start_index = (int)aviinfo.dwStart;
182                 film_range.end_index = film_range.start_index + (int)aviinfo.dwLength;
183                 fps = (double)aviinfo.dwRate/aviinfo.dwScale;
184                 pos = film_range.start_index;
185                 getframe = AVIStreamGetFrameOpen( avistream, &bmih );
186                 if( getframe != 0 )
187                     return true;
188             }
189         }
190     }
191
192     close();
193     return false;
194 }
195
196 bool CvCaptureAVI_VFW::grabFrame()
197 {
198     if( avistream )
199         bmih = (BITMAPINFOHEADER*)AVIStreamGetFrame( getframe, pos++ );
200     return bmih != 0;
201 }
202
203 IplImage* CvCaptureAVI_VFW::retrieveFrame(int)
204 {
205     if( avistream && bmih )
206     {
207         IplImage src;
208         cvInitImageHeader( &src, cvSize( bmih->biWidth, bmih->biHeight ),
209                            IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 );
210         cvSetData( &src, (char*)(bmih + 1), src.widthStep );
211         if( !frame || frame->width != src.width || frame->height != src.height )
212         {
213             cvReleaseImage( &frame );
214             frame = cvCreateImage( cvGetSize(&src), 8, 3 );
215         }
216
217         cvFlip( &src, frame, 0 );
218         return frame;
219     }
220
221     return 0;
222 }
223
224 double CvCaptureAVI_VFW::getProperty( int property_id )
225 {
226     switch( property_id )
227     {
228     case CV_CAP_PROP_POS_MSEC:
229         return cvRound(pos*1000./fps);
230     case CV_CAP_PROP_POS_FRAMES:
231         return pos;
232     case CV_CAP_PROP_POS_AVI_RATIO:
233         return (pos - film_range.start_index)/
234                (film_range.end_index - film_range.start_index + 1e-10);
235     case CV_CAP_PROP_FRAME_WIDTH:
236         return size.width;
237     case CV_CAP_PROP_FRAME_HEIGHT:
238         return size.height;
239     case CV_CAP_PROP_FPS:
240         return fps;
241     case CV_CAP_PROP_FOURCC:
242         return aviinfo.fccHandler;
243     case CV_CAP_PROP_FRAME_COUNT:
244         return film_range.end_index - film_range.start_index;
245     }
246     return 0;
247 }
248
249 bool CvCaptureAVI_VFW::setProperty( int property_id, double value )
250 {
251     switch( property_id )
252     {
253     case CV_CAP_PROP_POS_MSEC:
254     case CV_CAP_PROP_POS_FRAMES:
255     case CV_CAP_PROP_POS_AVI_RATIO:
256         {
257             switch( property_id )
258             {
259             case CV_CAP_PROP_POS_MSEC:
260                 pos = cvRound(value*fps*0.001);
261                 break;
262             case CV_CAP_PROP_POS_AVI_RATIO:
263                 pos = cvRound(value*(film_range.end_index -
264                                      film_range.start_index) +
265                               film_range.start_index);
266                 break;
267             default:
268                 pos = cvRound(value);
269             }
270             if( pos < film_range.start_index )
271                 pos = film_range.start_index;
272             if( pos > film_range.end_index )
273                 pos = film_range.end_index;
274         }
275         break;
276     default:
277         return false;
278     }
279
280     return true;
281 }
282
283 CvCapture* cvCreateFileCapture_VFW (const char* filename)
284 {
285     CvCaptureAVI_VFW* capture = new CvCaptureAVI_VFW;
286     if( capture->open(filename) )
287         return capture;
288     delete capture;
289     return 0;
290 }
291
292
293 /********************* Capturing video from camera via VFW *********************/
294
295 class CvCaptureCAM_VFW : public CvCapture
296 {
297 public:
298     CvCaptureCAM_VFW() { init(); }
299     virtual ~CvCaptureCAM_VFW() { close(); }
300
301     virtual bool open( int index );
302     virtual void close();
303     virtual double getProperty(int);
304     virtual bool setProperty(int, double) { return false; }
305     virtual bool grabFrame();
306     virtual IplImage* retrieveFrame(int);
307         virtual int getCaptureDomain() { return CV_CAP_VFW; } // Return the type of the capture object: CV_CAP_VFW, etc...
308
309 protected:
310     void init();
311     void closeHIC();
312     static LRESULT PASCAL frameCallback( HWND hWnd, VIDEOHDR* hdr );
313
314     CAPDRIVERCAPS caps;
315     HWND   capWnd;
316     VIDEOHDR* hdr;
317     DWORD  fourcc;
318     HIC    hic;
319     IplImage* frame;
320 };
321
322
323 void CvCaptureCAM_VFW::init()
324 {
325     memset( &caps, 0, sizeof(caps) );
326     capWnd = 0;
327     hdr = 0;
328     fourcc = 0;
329     hic = 0;
330     frame = 0;
331 }
332
333 void CvCaptureCAM_VFW::closeHIC()
334 {
335     if( hic )
336     {
337         ICDecompressEnd( hic );
338         ICClose( hic );
339         hic = 0;
340     }
341 }
342
343
344 LRESULT PASCAL CvCaptureCAM_VFW::frameCallback( HWND hWnd, VIDEOHDR* hdr )
345 {
346     CvCaptureCAM_VFW* capture = 0;
347
348     if (!hWnd) return FALSE;
349
350     capture = (CvCaptureCAM_VFW*)capGetUserData(hWnd);
351     capture->hdr = hdr;
352
353     return (LRESULT)TRUE;
354 }
355
356
357 // Initialize camera input
358 bool CvCaptureCAM_VFW::open( int wIndex )
359 {
360     char szDeviceName[80];
361     char szDeviceVersion[80];
362     HWND hWndC = 0;
363
364     close();
365
366     if( (unsigned)wIndex >= 10 )
367         wIndex = 0;
368
369     for( ; wIndex < 10; wIndex++ )
370     {
371         if( capGetDriverDescription( wIndex, szDeviceName,
372             sizeof (szDeviceName), szDeviceVersion,
373             sizeof (szDeviceVersion)))
374         {
375             hWndC = capCreateCaptureWindow ( "My Own Capture Window",
376                 WS_POPUP | WS_CHILD, 0, 0, 320, 240, 0, 0);
377             if( capDriverConnect (hWndC, wIndex))
378                 break;
379             DestroyWindow( hWndC );
380             hWndC = 0;
381         }
382     }
383
384     if( hWndC )
385     {
386         capWnd = hWndC;
387         hdr = 0;
388         hic = 0;
389         fourcc = (DWORD)-1;
390
391         memset( &caps, 0, sizeof(caps));
392         capDriverGetCaps( hWndC, &caps, sizeof(&caps));
393         ::MoveWindow( hWndC, 0, 0, 320, 240, TRUE );
394         capSetUserData( hWndC, (size_t)this );
395         capSetCallbackOnFrame( hWndC, frameCallback );
396         CAPTUREPARMS p;
397         capCaptureGetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
398         p.dwRequestMicroSecPerFrame = 66667/2;
399         capCaptureSetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
400         //capPreview( hWndC, 1 );
401         capPreviewScale(hWndC,FALSE);
402         capPreviewRate(hWndC,1);
403     }
404     return capWnd != 0;
405 }
406
407
408 void CvCaptureCAM_VFW::close()
409 {
410     if( capWnd )
411     {
412         capSetCallbackOnFrame( capWnd, NULL );
413         capDriverDisconnect( capWnd );
414         DestroyWindow( capWnd );
415         closeHIC();
416     }
417     cvReleaseImage( &frame );
418     init();
419 }
420
421
422 bool CvCaptureCAM_VFW::grabFrame()
423 {
424     if( capWnd )
425     {
426         SendMessage( capWnd, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0 );
427         return true;
428     }
429     return false;
430 }
431
432
433 IplImage* CvCaptureCAM_VFW::retrieveFrame(int)
434 {
435     BITMAPINFO vfmt;
436     memset( &vfmt, 0, sizeof(vfmt));
437     BITMAPINFOHEADER& vfmt0 = vfmt.bmiHeader;
438     int sz, prevWidth, prevHeight;
439
440     if( !capWnd )
441         return 0;
442
443     sz = capGetVideoFormat( capWnd, &vfmt, sizeof(vfmt));
444     prevWidth = frame ? frame->width : 0;
445     prevHeight = frame ? frame->height : 0;
446
447     if( !hdr || hdr->lpData == 0 || sz == 0 )
448         return 0;
449
450     if( !frame || frame->width != vfmt0.biWidth || frame->height != vfmt0.biHeight )
451     {
452         cvReleaseImage( &frame );
453         frame = cvCreateImage( cvSize( vfmt0.biWidth, vfmt0.biHeight ), 8, 3 );
454     }
455
456     if( vfmt.bmiHeader.biCompression != BI_RGB ||
457         vfmt.bmiHeader.biBitCount != 24 )
458     {
459         BITMAPINFOHEADER vfmt1 = icvBitmapHeader( vfmt0.biWidth, vfmt0.biHeight, 24 );
460
461         if( hic == 0 || fourcc != vfmt0.biCompression ||
462             prevWidth != vfmt0.biWidth || prevHeight != vfmt0.biHeight )
463         {
464             closeHIC();
465             hic = ICOpen( MAKEFOURCC('V','I','D','C'),
466                           vfmt0.biCompression, ICMODE_DECOMPRESS );
467             if( hic )
468             {
469                 if( ICDecompressBegin( hic, &vfmt0, &vfmt1 ) != ICERR_OK )
470                 {
471                     closeHIC();
472                     return 0;
473                 }
474             }
475         }
476
477         if( !hic || ICDecompress( hic, 0, &vfmt0, hdr->lpData,
478             &vfmt1, frame->imageData ) != ICERR_OK )
479         {
480             closeHIC();
481             return 0;
482         }
483
484         cvFlip( frame, frame, 0 );
485     }
486     else
487     {
488         IplImage src;
489         cvInitImageHeader( &src, cvSize(vfmt0.biWidth, vfmt0.biHeight),
490             IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 );
491         cvSetData( &src, hdr->lpData, src.widthStep );
492         cvFlip( &src, frame, 0 );
493     }
494
495     return frame;
496 }
497
498
499 double CvCaptureCAM_VFW::getProperty( int property_id )
500 {
501     switch( property_id )
502     {
503     case CV_CAP_PROP_FRAME_WIDTH:
504         return frame ? frame->width : 0;
505     case CV_CAP_PROP_FRAME_HEIGHT:
506         return frame ? frame->height : 0;
507     case CV_CAP_PROP_FOURCC:
508         return fourcc;
509     }
510     return 0;
511 }
512
513
514 CvCapture* cvCreateCameraCapture_VFW( int index )
515 {
516     CvCaptureCAM_VFW* capture = new CvCaptureCAM_VFW;
517
518     if( capture->open( index ))
519         return capture;
520
521     delete capture;
522     return 0;
523 }
524
525
526 /*************************** writing AVIs ******************************/
527
528 class CvVideoWriter_VFW : public CvVideoWriter
529 {
530 public:
531     CvVideoWriter_VFW() { init(); }
532     virtual ~CvVideoWriter_VFW() { close(); }
533
534     virtual bool open( const char* filename, int fourcc,
535                        double fps, CvSize frameSize, bool isColor );
536     virtual void close();
537     virtual bool writeFrame( const IplImage* );
538
539 protected:
540     void init();
541     bool createStreams( CvSize frameSize, bool isColor );
542
543     PAVIFILE      avifile;
544     PAVISTREAM    compressed;
545     PAVISTREAM    uncompressed;
546     double        fps;
547     IplImage*     tempFrame;
548     long          pos;
549     int           fourcc;
550 };
551
552
553 void CvVideoWriter_VFW::init()
554 {
555     avifile = 0;
556     compressed = uncompressed = 0;
557     fps = 0;
558     tempFrame = 0;
559     pos = 0;
560     fourcc = 0;
561 }
562
563 void CvVideoWriter_VFW::close()
564 {
565     if( uncompressed )
566         AVIStreamRelease( uncompressed );
567     if( compressed )
568         AVIStreamRelease( compressed );
569     if( avifile )
570         AVIFileRelease( avifile );
571     cvReleaseImage( &tempFrame );
572     init();
573 }
574
575
576 // philipg.  Made this code capable of writing 8bpp gray scale bitmaps
577 struct BITMAPINFO_8Bit
578 {
579     BITMAPINFOHEADER bmiHeader;
580     RGBQUAD          bmiColors[256];
581 };
582
583
584 bool CvVideoWriter_VFW::open( const char* filename, int _fourcc, double _fps, CvSize frameSize, bool isColor )
585 {
586     close();
587
588     icvInitCapture_VFW();
589     if( AVIFileOpen( &avifile, filename, OF_CREATE | OF_WRITE, 0 ) == AVIERR_OK )
590     {
591         fourcc = _fourcc;
592         fps = _fps;
593         if( frameSize.width > 0 && frameSize.height > 0 &&
594             !createStreams( frameSize, isColor ) )
595         {
596             close();
597             return false;
598         }
599     }
600     return true;
601 }
602
603
604 bool CvVideoWriter_VFW::createStreams( CvSize frameSize, bool isColor )
605 {
606     if( !avifile )
607         return false;
608     AVISTREAMINFO aviinfo;
609
610     BITMAPINFO_8Bit bmih;
611     bmih.bmiHeader = icvBitmapHeader( frameSize.width, frameSize.height, isColor ? 24 : 8 );
612     for( int i = 0; i < 256; i++ )
613     {
614         bmih.bmiColors[i].rgbBlue = (BYTE)i;
615         bmih.bmiColors[i].rgbGreen = (BYTE)i;
616         bmih.bmiColors[i].rgbRed = (BYTE)i;
617         bmih.bmiColors[i].rgbReserved = 0;
618     }
619
620     memset( &aviinfo, 0, sizeof(aviinfo));
621     aviinfo.fccType = streamtypeVIDEO;
622     aviinfo.fccHandler = 0;
623     // use highest possible accuracy for dwRate/dwScale
624     aviinfo.dwScale = (DWORD)((double)0x7FFFFFFF / fps);
625     aviinfo.dwRate = cvRound(fps * aviinfo.dwScale);
626     aviinfo.rcFrame.top = aviinfo.rcFrame.left = 0;
627     aviinfo.rcFrame.right = frameSize.width;
628     aviinfo.rcFrame.bottom = frameSize.height;
629
630     if( AVIFileCreateStream( avifile, &uncompressed, &aviinfo ) == AVIERR_OK )
631     {
632         AVICOMPRESSOPTIONS copts, *pcopts = &copts;
633         copts.fccType = streamtypeVIDEO;
634         copts.fccHandler = fourcc != -1 ? fourcc : 0;
635         copts.dwKeyFrameEvery = 1;
636         copts.dwQuality = 10000;
637         copts.dwBytesPerSecond = 0;
638         copts.dwFlags = AVICOMPRESSF_VALID;
639         copts.lpFormat = &bmih;
640         copts.cbFormat = (isColor ? sizeof(BITMAPINFOHEADER) : sizeof(bmih));
641         copts.lpParms = 0;
642         copts.cbParms = 0;
643         copts.dwInterleaveEvery = 0;
644
645         if( fourcc != -1 || AVISaveOptions( 0, 0, 1, &uncompressed, &pcopts ) == TRUE )
646         {
647             if( AVIMakeCompressedStream( &compressed, uncompressed, pcopts, 0 ) == AVIERR_OK &&
648                 AVIStreamSetFormat( compressed, 0, &bmih, sizeof(bmih)) == AVIERR_OK )
649             {
650                 fps = fps;
651                 fourcc = (int)copts.fccHandler;
652                 frameSize = frameSize;
653                 tempFrame = cvCreateImage( frameSize, 8, (isColor ? 3 : 1) );
654                 return true;
655             }
656         }
657     }
658     return false;
659 }
660
661
662 bool CvVideoWriter_VFW::writeFrame( const IplImage* image )
663 {
664     bool result = false;
665     CV_FUNCNAME( "CvVideoWriter_VFW::writeFrame" );
666
667     __BEGIN__;
668
669     if( !image )
670         EXIT;
671
672     if( !compressed && !createStreams( cvGetSize(image), image->nChannels > 1 ))
673         EXIT;
674
675     if( image->width != tempFrame->width || image->height != tempFrame->height )
676         CV_ERROR( CV_StsUnmatchedSizes,
677             "image size is different from the currently set frame size" );
678
679     if( image->nChannels != tempFrame->nChannels ||
680         image->depth != tempFrame->depth ||
681         image->origin == 0 ||
682         image->widthStep != cvAlign(image->width*image->nChannels*((image->depth & 255)/8), 4))
683     {
684         cvConvertImage( image, tempFrame, image->origin == 0 ? CV_CVTIMG_FLIP : 0 );
685         image = (const IplImage*)tempFrame;
686     }
687
688     result = AVIStreamWrite( compressed, pos++, 1, image->imageData,
689                              image->imageSize, AVIIF_KEYFRAME, 0, 0 ) == AVIERR_OK;
690
691     __END__;
692
693     return result;
694 }
695
696 CvVideoWriter* cvCreateVideoWriter_VFW( const char* filename, int fourcc,
697                                         double fps, CvSize frameSize, int isColor )
698 {
699     CvVideoWriter_VFW* writer = new CvVideoWriter_VFW;
700     if( writer->open( filename, fourcc, fps, frameSize, isColor != 0 ))
701         return writer;
702     delete writer;
703     return 0;
704 }