Update to 2.0.0 tree from current Fremantle build
[opencv] / src / 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 _WIN64) && 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, int idx )
76 {
77     return capture ? capture->retrieveFrame(idx) : 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 CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
91 {
92     return capture ? capture->getCaptureDomain() : 0;
93 }
94
95
96 /**
97  * Camera dispatching method: index is the camera number.
98  * If given an index from 0 to 99, it tries to find the first
99  * API that can access a given camera index.
100  * Add multiples of 100 to select an API.
101  */
102 CV_IMPL CvCapture * cvCreateCameraCapture (int index)
103 {
104         int  domains[] =
105         {
106 #ifdef HAVE_VIDEOINPUT
107         CV_CAP_DSHOW,
108 #endif
109                 CV_CAP_IEEE1394,   // identical to CV_CAP_DC1394
110                 CV_CAP_STEREO,
111                 CV_CAP_VFW,        // identical to CV_CAP_V4L
112                 CV_CAP_MIL,
113                 CV_CAP_QT,
114                 CV_CAP_UNICAP,
115                 -1
116         };
117
118         // interpret preferred interface (0 = autodetect)
119         int pref = (index / 100) * 100;
120         if (pref)
121         {
122                 domains[0]=pref;
123                 index %= 100;
124                 domains[1]=-1;
125         }
126
127         // try every possibly installed camera API
128         for (int i = 0; domains[i] >= 0; i++)
129         {
130         #if defined(HAVE_VIDEOINPUT) || defined(HAVE_TYZX) || defined(HAVE_VFW) || \
131             defined(HAVE_CAMV4L) || defined (HAVE_CAMV4L2) || defined(HAVE_GSTREAMER) || \
132             defined(HAVE_DC1394_2) || defined(HAVE_DC1394) || defined(HAVE_CMU1394) || \
133             defined(HAVE_GSTREAMER) || defined(HAVE_MIL) || defined(HAVE_QUICKTIME) || \
134             defined(HAVE_UNICAP)
135                 // local variable to memorize the captured device
136                 CvCapture *capture;
137         #endif
138
139                 switch (domains[i])
140                 {
141         #ifdef HAVE_VIDEOINPUT
142         case CV_CAP_DSHOW:
143             capture = cvCreateCameraCapture_DShow (index);
144             if (capture)
145                 return capture;
146             break;
147         #endif
148
149                 #ifdef HAVE_TYZX
150                 case CV_CAP_STEREO:
151                         capture = cvCreateCameraCapture_TYZX (index);
152                         if (capture)
153                                 return capture;
154                         break;
155                 #endif
156
157                 case CV_CAP_VFW:
158                 #ifdef HAVE_VFW
159                         capture = cvCreateCameraCapture_VFW (index);
160                         if (capture)
161                                 return capture;
162                 #endif
163                 #if defined (HAVE_CAMV4L) || defined (HAVE_CAMV4L2)
164                         capture = cvCreateCameraCapture_V4L (index);
165                         if (capture)
166                                 return capture;
167                 #endif
168                 #ifdef HAVE_GSTREAMER
169                         capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, 0);
170                         if (capture)
171                                 return capture;
172                         capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, 0);
173                         if (capture)
174                                 return capture;
175                 #endif
176                         break;
177
178                 case CV_CAP_FIREWIRE:
179                 #ifdef HAVE_DC1394_2
180                         capture = cvCreateCameraCapture_DC1394_2 (index);
181                         if (capture)
182                                 return capture;
183                 #endif
184                 #ifdef HAVE_DC1394
185                         capture = cvCreateCameraCapture_DC1394 (index);
186                         if (capture)
187                                 return capture;
188                 #endif
189                 #ifdef HAVE_CMU1394
190                         capture = cvCreateCameraCapture_CMU (index);
191                         if (capture)
192                                 return capture;
193                 #endif
194                 #ifdef HAVE_GSTREAMER
195                         capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
196                         if (capture)
197                                 return capture;
198                 #endif
199                         break;
200
201                 #ifdef HAVE_MIL
202                 case CV_CAP_MIL:
203                         capture = cvCreateCameraCapture_MIL (index);
204                         if (capture)
205                                 return capture;
206                         break;
207                 #endif
208
209                 #ifdef HAVE_QUICKTIME
210                 case CV_CAP_QT:
211                         capture = cvCreateCameraCapture_QT (index);
212                         if (capture)
213                                 return capture;
214                         break;
215                 #endif
216
217                 #ifdef HAVE_UNICAP
218                 case CV_CAP_UNICAP:
219                   capture = cvCreateCameraCapture_Unicap (index);
220                   if (capture)
221                     return capture;
222                   break;
223                 #endif
224
225                 }
226         }
227
228         // failed open a camera
229         return 0;
230 }
231
232 /**
233  * Videoreader dispatching method: it tries to find the first
234  * API that can access a given filename.
235  */
236 CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
237 {
238     CvCapture * result = 0;
239
240     #ifdef WIN32
241     if (! result)
242         result = cvCreateFileCapture_Win32 (filename);
243     #endif
244
245     #ifdef HAVE_FFMPEG
246         if (! result)
247             result = cvCreateFileCapture_FFMPEG (filename);
248     #endif
249     
250     #ifdef HAVE_XINE
251     if (! result)
252         result = cvCreateFileCapture_XINE (filename);
253     #endif
254
255     #ifdef HAVE_GSTREAMER
256     if (! result)
257         result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
258     #endif
259
260     #ifdef HAVE_QUICKTIME
261     if (! result)
262         result = cvCreateFileCapture_QT (filename);
263     #endif
264     
265     if (! result)
266         result = cvCreateFileCapture_Images (filename);
267
268     return result;
269 }
270
271 /**
272  * Videowriter dispatching method: it tries to find the first
273  * API that can write a given stream.
274  */
275 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
276                                             double fps, CvSize frameSize, int is_color )
277 {
278         //CV_FUNCNAME( "cvCreateVideoWriter" );
279
280         CvVideoWriter *result = 0;
281
282         if(!fourcc || !fps)
283                 result = cvCreateVideoWriter_Images(filename);
284
285         #ifdef WIN32
286         if(!result)
287                 result = cvCreateVideoWriter_Win32(filename, fourcc, fps, frameSize, is_color);
288         #endif
289
290 /*      #ifdef HAVE_XINE
291         if(!result)
292                 result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
293         #endif
294 */
295         #ifdef HAVE_FFMPEG
296         if(!result)
297                 result = cvCreateVideoWriter_FFMPEG(filename, fourcc, fps, frameSize, is_color);
298         #endif
299
300         #ifdef HAVE_QUICKTIME
301         if(!result)
302                 result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
303         #endif
304
305         if(!result)
306                 result = cvCreateVideoWriter_Images(filename);
307
308         return result;
309 }
310
311 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
312 {
313     return writer ? writer->writeFrame(image) : 0;
314 }
315
316 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
317 {
318     if( pwriter && *pwriter )
319     {
320         delete *pwriter;
321         *pwriter = 0;
322     }
323 }
324
325 namespace cv
326 {
327
328 VideoCapture::VideoCapture()
329 {}
330         
331 VideoCapture::VideoCapture(const string& filename)
332 {
333     open(filename);
334 }
335     
336 VideoCapture::VideoCapture(int device)
337 {
338     open(device);
339 }
340
341 VideoCapture::~VideoCapture()
342 {
343     cap.release();
344 }
345     
346 bool VideoCapture::open(const string& filename)
347 {
348     cap = cvCreateFileCapture(filename.c_str());
349     return isOpened();
350 }
351     
352 bool VideoCapture::open(int device)
353 {
354     cap = cvCreateCameraCapture(device);
355     return isOpened();
356 }
357     
358 bool VideoCapture::isOpened() const { return !cap.empty(); }
359     
360 void VideoCapture::release()
361 {
362     cap.release();
363 }
364
365 bool VideoCapture::grab()
366 {
367     return cvGrabFrame(cap) != 0;
368 }
369     
370 bool VideoCapture::retrieve(Mat& image, int channel)
371 {
372     IplImage* _img = cvRetrieveFrame(cap, channel);
373     if( !_img )
374     {
375         image.release();
376         return false;
377     }
378     if(_img->origin == IPL_ORIGIN_TL)
379         image = Mat(_img);
380     else
381     {
382         Mat temp(_img);
383         flip(temp, image, 0);
384     }
385     return true;
386 }
387     
388 VideoCapture& VideoCapture::operator >> (Mat& image)
389 {
390     if(!grab())
391         image.release();
392     else
393         retrieve(image);
394     return *this;
395 }
396     
397 bool VideoCapture::set(int propId, double value)
398 {
399     return cvSetCaptureProperty(cap, propId, value) != 0;
400 }
401     
402 double VideoCapture::get(int propId)
403 {
404     return cvGetCaptureProperty(cap, propId);
405 }
406
407 VideoWriter::VideoWriter()
408 {}
409     
410 VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor)
411 {
412     open(filename, fourcc, fps, frameSize, isColor);
413 }
414
415 VideoWriter::~VideoWriter()
416 {
417     writer.release();
418 }
419     
420 bool VideoWriter::open(const string& filename, int fourcc, double fps, Size frameSize, bool isColor)
421 {
422     writer = cvCreateVideoWriter(filename.c_str(), fourcc, fps, frameSize, isColor);
423     return isOpened();
424 }
425
426 bool VideoWriter::isOpened() const
427 {
428     return !writer.empty();
429 }    
430     
431 VideoWriter& VideoWriter::operator << (const Mat& image)
432 {
433     IplImage _img = image;
434     cvWriteFrame(writer, &_img);
435     return *this;    
436 }
437
438 }