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