Update to 2.0.0 tree from current Fremantle build
[opencv] / src / 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 void* (*CvCreateFileCapture_Plugin)( const char* filename );
47 typedef void* (*CvCreateCameraCapture_Plugin)( int index );
48 typedef int (*CvGrabFrame_Plugin)( void* capture_handle );
49 typedef IplImage* (*CvRetrieveFrame_Plugin)( void* capture_handle, int );
50 typedef int (*CvSetCaptureProperty_Plugin)( void* capture_handle, int prop_id, double value );
51 typedef double (*CvGetCaptureProperty_Plugin)( void* capture_handle, int prop_id );
52 typedef void (*CvReleaseCapture_Plugin)( void** capture_handle );
53 typedef void* (*CvCreateVideoWriter_Plugin)( const char* filename, int fourcc,
54                                              double fps, CvSize frameSize, int isColor );
55 typedef int (*CvWriteFrame_Plugin)( void* writer_handle, const IplImage* frame );
56 typedef void (*CvReleaseVideoWriter_Plugin)( void** writer );
57 }
58
59 static HMODULE icvFFOpenCV = 0;
60 static CvCreateFileCapture_Plugin icvCreateFileCapture_FFMPEG_p = 0;
61 static CvReleaseCapture_Plugin icvReleaseCapture_FFMPEG_p = 0;
62 static CvGrabFrame_Plugin icvGrabFrame_FFMPEG_p = 0;
63 static CvRetrieveFrame_Plugin icvRetrieveFrame_FFMPEG_p = 0;
64 static CvSetCaptureProperty_Plugin icvSetCaptureProperty_FFMPEG_p = 0;
65 static CvGetCaptureProperty_Plugin icvGetCaptureProperty_FFMPEG_p = 0;
66 static CvCreateVideoWriter_Plugin icvCreateVideoWriter_FFMPEG_p = 0;
67 static CvReleaseVideoWriter_Plugin icvReleaseVideoWriter_FFMPEG_p = 0;
68 static CvWriteFrame_Plugin icvWriteFrame_FFMPEG_p = 0;
69
70 static void
71 icvInitFFMPEG(void)
72 {
73     static int ffmpegInitialized = 0;
74     if( !ffmpegInitialized )
75     {
76 #ifdef __MINGW32__
77 #define ffopencv_prefix "lib"
78 #else
79 #define ffopencv_prefix ""
80 #endif
81 #if defined(_DEBUG) && defined(__MINGW32__)
82 #define ffopencv_suffix_dbg "d"
83 #else
84 #define ffopencv_suffix_dbg ""
85 #endif
86 #if defined EM64T
87 #define ffopencv_suffix "_64"
88 #else
89 #define ffopencv_suffix ""
90 #endif
91
92 #define ffopencv_name_m2(a,b,c) ffopencv_prefix "opencv_ffmpeg" #a #b #c ffopencv_suffix ffopencv_suffix_dbg ".dll"
93 #define ffopencv_name_m(a,b,c) ffopencv_name_m2(a,b,c)
94         const char* ffopencv_name =
95             ffopencv_name_m(CV_MAJOR_VERSION,CV_MINOR_VERSION,CV_SUBMINOR_VERSION);
96
97         icvFFOpenCV = LoadLibrary( ffopencv_name );
98         if( icvFFOpenCV )
99         {
100             icvCreateFileCapture_FFMPEG_p =
101                 (CvCreateFileCapture_Plugin)GetProcAddress(icvFFOpenCV, "cvCreateFileCapture_FFMPEG");
102             icvReleaseCapture_FFMPEG_p =
103                 (CvReleaseCapture_Plugin)GetProcAddress(icvFFOpenCV, "cvReleaseCapture_FFMPEG");
104             icvGrabFrame_FFMPEG_p =
105                 (CvGrabFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvGrabFrame_FFMPEG");
106             icvRetrieveFrame_FFMPEG_p =
107                 (CvRetrieveFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvRetrieveFrame_FFMPEG");
108             icvSetCaptureProperty_FFMPEG_p =
109                 (CvSetCaptureProperty_Plugin)GetProcAddress(icvFFOpenCV, "cvSetCaptureProperty_FFMPEG");
110             icvGetCaptureProperty_FFMPEG_p =
111                 (CvGetCaptureProperty_Plugin)GetProcAddress(icvFFOpenCV, "cvGetCaptureProperty_FFMPEG");
112             icvCreateVideoWriter_FFMPEG_p =
113                 (CvCreateVideoWriter_Plugin)GetProcAddress(icvFFOpenCV, "cvCreateVideoWriter_FFMPEG");
114             icvReleaseVideoWriter_FFMPEG_p =
115                 (CvReleaseVideoWriter_Plugin)GetProcAddress(icvFFOpenCV, "cvReleaseVideoWriter_FFMPEG");
116             icvWriteFrame_FFMPEG_p =
117                 (CvWriteFrame_Plugin)GetProcAddress(icvFFOpenCV, "cvWriteFrame_FFMPEG");
118         }
119         ffmpegInitialized = 1;
120     }
121 }
122
123
124 class CvCapture_FFMPEG_proxy : public CvCapture
125 {
126 public:
127     CvCapture_FFMPEG_proxy() { ffmpegCapture = 0; }
128     virtual ~CvCapture_FFMPEG_proxy() { close(); }
129
130     virtual double getProperty(int propId)
131     {
132         return ffmpegCapture ? icvGetCaptureProperty_FFMPEG_p(ffmpegCapture, propId) : 0;
133     }
134     virtual bool setProperty(int propId, double value)
135     {
136         return ffmpegCapture ? icvSetCaptureProperty_FFMPEG_p(ffmpegCapture, propId, value)!=0 : false;
137     }
138     virtual bool grabFrame()
139     {
140         return ffmpegCapture ? icvGrabFrame_FFMPEG_p(ffmpegCapture)!=0 : false;
141     }
142     virtual IplImage* retrieveFrame(int)
143     {
144         return ffmpegCapture ? icvRetrieveFrame_FFMPEG_p(ffmpegCapture,0) : 0;
145     }
146     virtual bool open( const char* filename )
147     {
148         close();
149
150         icvInitFFMPEG();
151         if( !icvCreateFileCapture_FFMPEG_p )
152             return false;
153         ffmpegCapture = icvCreateFileCapture_FFMPEG_p( filename );
154         return ffmpegCapture != 0;
155     }
156     virtual void close()
157     {
158         if( ffmpegCapture && icvReleaseCapture_FFMPEG_p )
159             icvReleaseCapture_FFMPEG_p( &ffmpegCapture );
160         assert( ffmpegCapture == 0 );
161         ffmpegCapture = 0;
162     }
163
164 protected:
165     void* ffmpegCapture;
166 };
167
168
169 CvCapture* cvCreateFileCapture_Win32(const char * filename)
170 {
171     CvCapture_FFMPEG_proxy* result = new CvCapture_FFMPEG_proxy;
172     if( result->open( filename ))
173         return result;
174     delete result;
175     return cvCreateFileCapture_VFW(filename);
176 }
177
178
179 class CvVideoWriter_FFMPEG_proxy : public CvVideoWriter
180 {
181 public:
182     CvVideoWriter_FFMPEG_proxy() { ffmpegWriter = 0; }
183     virtual ~CvVideoWriter_FFMPEG_proxy() { close(); }
184
185     virtual bool writeFrame( const IplImage* image )
186     {
187         return ffmpegWriter ? icvWriteFrame_FFMPEG_p(ffmpegWriter, image)!=0 : false;
188     }
189     virtual bool open( const char* filename, int fourcc, double fps, CvSize frameSize, bool isColor )
190     {
191         close();
192         icvInitFFMPEG();
193         if( !icvCreateVideoWriter_FFMPEG_p )
194             return false;
195         ffmpegWriter = icvCreateVideoWriter_FFMPEG_p( filename, fourcc, fps, frameSize, isColor );
196         return ffmpegWriter != 0;
197     }
198
199     virtual void close()
200     {
201         if( ffmpegWriter && icvReleaseVideoWriter_FFMPEG_p )
202             icvReleaseVideoWriter_FFMPEG_p( &ffmpegWriter );
203         assert( ffmpegWriter == 0 );
204         ffmpegWriter = 0;
205     }
206
207 protected:
208     void* ffmpegWriter;
209 };
210
211
212 CvVideoWriter* cvCreateVideoWriter_Win32( const char* filename, int fourcc,
213                                           double fps, CvSize frameSize, int isColor )
214 {
215     CvVideoWriter_FFMPEG_proxy* result = new CvVideoWriter_FFMPEG_proxy;
216
217     if( result->open( filename, fourcc, fps, frameSize, isColor != 0 ))
218         return result;
219     delete result;
220
221     return cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, isColor);
222 }