Update the trunk to the OpenCV's CVS (2008-07-14)
[opencv] / otherlibs / highgui / cvcap.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 #if _MSC_VER >= 1200
45 #pragma warning( disable: 4711 )
46 #endif
47
48 #if defined WIN64 && defined EM64T && defined _MSC_VER && !defined __ICL
49 #pragma optimize("",off)
50 #endif
51
52
53 /************************* Reading AVIs & Camera data **************************/
54
55 CV_IMPL void cvReleaseCapture( CvCapture** pcapture )
56 {
57     if( pcapture && *pcapture )
58     {
59         delete *pcapture;
60         *pcapture = 0;
61     }
62 }
63
64 CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
65 {
66     return capture ? capture->queryFrame() : 0;
67 }
68
69
70 CV_IMPL int cvGrabFrame( CvCapture* capture )
71 {
72     return capture ? capture->grabFrame() : 0;
73 }
74
75 CV_IMPL IplImage* cvRetrieveFrame( CvCapture* capture )
76 {
77     return capture ? capture->retrieveFrame() : 0;
78 }
79
80 CV_IMPL double cvGetCaptureProperty( CvCapture* capture, int id )
81 {
82     return capture ? capture->getProperty(id) : 0;
83 }
84
85 CV_IMPL int cvSetCaptureProperty( CvCapture* capture, int id, double value )
86 {
87     return capture ? capture->setProperty(id, value) : 0;
88 }
89
90
91 /**
92  * Camera dispatching method: index is the camera number.
93  * If given an index from 0 to 99, it tries to find the first
94  * API that can access a given camera index.
95  * Add multiples of 100 to select an API.
96  */
97 CV_IMPL CvCapture * cvCreateCameraCapture (int index)
98 {
99         int  domains[] =
100         {
101                 CV_CAP_IEEE1394,   // identical to CV_CAP_DC1394
102                 CV_CAP_STEREO,
103                 CV_CAP_VFW,        // identical to CV_CAP_V4L
104                 CV_CAP_MIL,
105                 CV_CAP_QT,
106                 CV_CAP_UNICAP,
107                 -1
108         };
109
110
111         // interpret preferred interface (0 = autodetect)
112         int pref = (index / 100) * 100;
113         if (pref)
114         {
115                 domains[0]=pref;
116                 index %= 100;
117                 domains[1]=-1;
118         }
119
120         // try every possibly installed camera API
121         for (int i = 0; domains[i] >= 0; i++)
122         {
123                 // local variable to memorize the captured device
124                 CvCapture *capture;
125
126                 switch (domains[i])
127                 {
128                 #ifdef HAVE_TYZX
129                 case CV_CAP_STEREO:
130                         capture = cvCreateCameraCapture_TYZX (index);
131                         if (capture)
132                                 return capture;
133                         break;
134                 #endif
135
136                 case CV_CAP_VFW:
137                 #ifdef HAVE_VFW
138                         capture = cvCreateCameraCapture_VFW (index);
139                         if (capture)
140                                 return capture;
141                 #endif
142                 #if defined (HAVE_CAMV4L) || defined (HAVE_CAMV4L2)
143                         capture = cvCreateCameraCapture_V4L (index);
144                         if (capture)
145                                 return capture;
146                 #endif
147                 #ifdef HAVE_GSTREAMER
148                         capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, 0);
149                         if (capture)
150                                 return capture;
151                         capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, 0);
152                         if (capture)
153                                 return capture;
154                 #endif
155                         break;
156
157                 case CV_CAP_FIREWIRE:
158                 #ifdef HAVE_DC1394
159                         capture = cvCreateCameraCapture_DC1394 (index);
160                         if (capture)
161                                 return capture;
162                 #endif
163                 #ifdef HAVE_CMU1394
164                         capture = cvCreateCameraCapture_CMU (index);
165                         if (capture)
166                                 return capture;
167                 #endif
168                 #ifdef HAVE_GSTREAMER
169                         capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
170                         if (capture)
171                                 return capture;
172                 #endif
173                         break;
174
175                 #ifdef HAVE_MIL
176                 case CV_CAP_MIL:
177                         capture = cvCreateCameraCapture_MIL (index);
178                         if (capture)
179                                 return capture;
180                         break;
181                 #endif
182
183                 #ifdef HAVE_QUICKTIME
184                 case CV_CAP_QT:
185                         capture = cvCreateCameraCapture_QT (index);
186                         if (capture)
187                                 return capture;
188                         break;
189                 #endif
190                         
191                 #ifdef HAVE_UNICAP
192                 case CV_CAP_UNICAP:
193                   capture = cvCreateCameraCapture_Unicap (index);
194                   if (capture)
195                     return capture;
196                   break;
197                 #endif
198
199                 }
200         }
201
202         // failed open a camera
203         return 0;
204 }
205
206 /**
207  * Videoreader dispatching method: it tries to find the first
208  * API that can access a given filename.
209  */
210 CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
211 {
212     CvCapture * result = 0;
213
214     if (! result)
215         result = cvCreateFileCapture_Images (filename);
216
217     #ifdef WIN32
218     if (! result)
219         result = cvCreateFileCapture_Win32 (filename);
220     #endif
221
222     #ifdef HAVE_XINE
223     if (! result)
224         result = cvCreateFileCapture_XINE (filename);
225     #endif
226
227     #ifdef HAVE_GSTREAMER
228     if (! result)
229         result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
230     #endif
231
232     #ifdef HAVE_FFMPEG
233     if (! result)
234         result = cvCreateFileCapture_FFMPEG (filename);
235     #endif
236
237     #ifdef HAVE_QUICKTIME
238     if (! result)
239         result = cvCreateFileCapture_QT (filename);
240     #endif
241
242     return result;
243 }
244
245 /**
246  * Videowriter dispatching method: it tries to find the first
247  * API that can write a given stream.
248  */
249 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
250                                             double fps, CvSize frameSize, int is_color )
251 {
252         CV_FUNCNAME( "cvCreateVideoWriter" );
253
254         CvVideoWriter *result = 0;
255
256         if(!fourcc || !fps)
257                 result = cvCreateVideoWriter_Images(filename);
258
259         #ifdef WIN32
260         if(!result)
261                 result = cvCreateVideoWriter_Win32(filename, fourcc, fps, frameSize, is_color);
262         #endif
263
264 /*      #ifdef HAVE_XINE
265         if(!result)
266                 result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
267         #endif
268 */
269         #ifdef HAVE_FFMPEG
270         if(!result)
271                 result = cvCreateVideoWriter_FFMPEG(filename, fourcc, fps, frameSize, is_color);
272         #endif
273
274         #ifdef HAVE_QUICKTIME
275         if(!result)
276                 result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
277         #endif
278
279         if(!result)
280                 result = cvCreateVideoWriter_Images(filename);
281
282         return result;
283 }
284
285 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
286 {
287     return writer ? writer->writeFrame(image) : 0;
288 }
289
290 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
291 {
292     if( pwriter && *pwriter )
293     {
294         delete *pwriter;
295         *pwriter = 0;
296     }
297 }