Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / otherlibs / highgui / cvcap_w32.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 extern "C"
45 {
46 typedef CvCapture* (*CvCreateFileCapture_Plugin)( const char* filename );
47 typedef CvCapture* (*CvCreateCameraCapture_Plugin)( int index );
48 typedef void (*CvReleaseCapture_Plugin)( CvCapture** capture );
49 typedef CvVideoWriter* (*CvCreateVideoWriter_Plugin)( const char* filename, int fourcc,
50                                                       double fps, CvSize frameSize, int isColor );
51 typedef void (*CvReleaseVideoWriter_Plugin)( CvVideoWriter** writer );
52 }
53
54 static HMODULE icvFFOpenCV = 0;
55 static CvCreateFileCapture_Plugin icvCreateFileCapture_FFMPEG_p = 0;
56 static CvReleaseCapture_Plugin icvReleaseCapture_FFMPEG_p = 0;
57 static CvCreateVideoWriter_Plugin icvCreateVideoWriter_FFMPEG_p = 0;
58 static CvReleaseVideoWriter_Plugin icvReleaseVideoWriter_FFMPEG_p = 0;
59
60
61 static void
62 icvInitFFMPEG(void)
63 {
64     static int ffmpegInitialized = 0;
65     if( !ffmpegInitialized )
66     {
67 #ifdef _DEBUG
68 //#define ffopencv_suffix_dbg "d"
69 #define ffopencv_suffix_dbg ""
70 #else
71 #define ffopencv_suffix_dbg ""
72 #endif
73 #if defined EM64T
74 #define ffopencv_suffix "_64"
75 #else
76 #define ffopencv_suffix ""
77 #endif
78
79 #define ffopencv_name_m2(a,b,c) "ffopencv" #a #b #c ffopencv_suffix ffopencv_suffix_dbg ".dll"
80 #define ffopencv_name_m(a,b,c) ffopencv_name_m2(a,b,c)
81         const char* ffopencv_name =
82             ffopencv_name_m(CV_MAJOR_VERSION,CV_MINOR_VERSION,CV_SUBMINOR_VERSION);
83
84         icvFFOpenCV = LoadLibrary( ffopencv_name );
85         if( icvFFOpenCV )
86         {
87             icvCreateFileCapture_FFMPEG_p =
88                 (CvCreateFileCapture_Plugin)GetProcAddress(icvFFOpenCV, "cvCreateFileCapture_FFMPEG");
89             icvCreateVideoWriter_FFMPEG_p =
90                 (CvCreateVideoWriter_Plugin)GetProcAddress(icvFFOpenCV, "cvCreateVideoWriter_FFMPEG");
91             icvReleaseCapture_FFMPEG_p =
92                 (CvReleaseCapture_Plugin)GetProcAddress(icvFFOpenCV, "cvReleaseCapture_FFMPEG");
93             icvReleaseVideoWriter_FFMPEG_p =
94                 (CvReleaseVideoWriter_Plugin)GetProcAddress(icvFFOpenCV, "cvReleaseVideoWriter_FFMPEG");
95         }
96         ffmpegInitialized = 1;
97     }
98 }
99
100
101 class CvCapture_FFMPEG_proxy : public CvCapture
102 {
103 public:
104     CvCapture_FFMPEG_proxy() { ffmpegCapture = 0; }
105     virtual ~CvCapture_FFMPEG_proxy() { close(); }
106
107     virtual double getProperty(int propId)
108     {
109         return ffmpegCapture ? ffmpegCapture->getProperty(propId) : 0;
110     }
111     virtual bool setProperty(int propId, double value)
112     {
113         return ffmpegCapture ? ffmpegCapture->setProperty(propId, value) : false;
114     }
115     virtual bool grabFrame()
116     {
117         return ffmpegCapture ? ffmpegCapture->grabFrame() : false;
118     }
119     virtual IplImage* retrieveFrame()
120     {
121         return ffmpegCapture ? ffmpegCapture->retrieveFrame() : 0;
122     }
123     virtual bool open( const char* filename )
124     {
125         close();
126
127         icvInitFFMPEG();
128         if( !icvCreateFileCapture_FFMPEG_p )
129             return false;
130         ffmpegCapture = icvCreateFileCapture_FFMPEG_p( filename );
131         return ffmpegCapture != 0;
132     }
133     virtual void close()
134     {
135         if( ffmpegCapture && icvReleaseCapture_FFMPEG_p )
136             icvReleaseCapture_FFMPEG_p( &ffmpegCapture );
137         assert( ffmpegCapture == 0 );
138         ffmpegCapture = 0;
139     }
140
141 protected:
142     CvCapture* ffmpegCapture;
143 };
144
145
146 CvCapture* cvCreateFileCapture_Win32(const char * filename)
147 {
148     CvCapture_FFMPEG_proxy* result = new CvCapture_FFMPEG_proxy;
149     if( result->open( filename ))
150         return result;
151     delete result;
152     return cvCreateFileCapture_VFW(filename);
153 }
154
155
156 class CvVideoWriter_FFMPEG_proxy : public CvVideoWriter
157 {
158 public:
159     CvVideoWriter_FFMPEG_proxy() { ffmpegWriter = 0; }
160     virtual ~CvVideoWriter_FFMPEG_proxy() { close(); }
161
162     virtual bool writeFrame( const IplImage* image )
163     {
164         return ffmpegWriter ? ffmpegWriter->writeFrame(image) : false;
165     }
166     virtual bool open( const char* filename, int fourcc, double fps, CvSize frameSize, bool isColor )
167     {
168         close();
169         icvInitFFMPEG();
170         if( !icvCreateVideoWriter_FFMPEG_p )
171             return false;
172         ffmpegWriter = icvCreateVideoWriter_FFMPEG_p( filename, fourcc, fps, frameSize, isColor );
173         return ffmpegWriter != 0;
174     }
175
176     virtual void close()
177     {
178         if( ffmpegWriter && icvReleaseVideoWriter_FFMPEG_p )
179             icvReleaseVideoWriter_FFMPEG_p( &ffmpegWriter );
180         assert( ffmpegWriter == 0 );
181         ffmpegWriter = 0;
182     }
183
184 protected:
185     CvVideoWriter* ffmpegWriter;
186 };
187
188
189 CvVideoWriter* cvCreateVideoWriter_Win32( const char* filename, int fourcc,
190                                           double fps, CvSize frameSize, int isColor )
191 {
192     CvVideoWriter_FFMPEG_proxy* result = new CvVideoWriter_FFMPEG_proxy;
193
194     if( result->open( filename, fourcc, fps, frameSize, isColor != 0 ))
195         return result;
196     delete result;
197     
198     return cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, isColor);
199 }