1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
44 #if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
45 #pragma optimize("",off)
46 #pragma warning(disable: 4748)
52 template<> void Ptr<CvCapture>::delete_obj()
53 { cvReleaseCapture(&obj); }
55 template<> void Ptr<CvVideoWriter>::delete_obj()
56 { cvReleaseVideoWriter(&obj); }
60 /************************* Reading AVIs & Camera data **************************/
62 CV_IMPL void cvReleaseCapture( CvCapture** pcapture )
64 if( pcapture && *pcapture )
71 CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
75 if(!capture->grabFrame())
77 return capture->retrieveFrame(0);
81 CV_IMPL int cvGrabFrame( CvCapture* capture )
83 return capture ? capture->grabFrame() : 0;
86 CV_IMPL IplImage* cvRetrieveFrame( CvCapture* capture, int idx )
88 return capture ? capture->retrieveFrame(idx) : 0;
91 CV_IMPL double cvGetCaptureProperty( CvCapture* capture, int id )
93 return capture ? capture->getProperty(id) : 0;
96 CV_IMPL int cvSetCaptureProperty( CvCapture* capture, int id, double value )
98 return capture ? capture->setProperty(id, value) : 0;
101 CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
103 return capture ? capture->getCaptureDomain() : 0;
108 * Camera dispatching method: index is the camera number.
109 * If given an index from 0 to 99, it tries to find the first
110 * API that can access a given camera index.
111 * Add multiples of 100 to select an API.
113 CV_IMPL CvCapture * cvCreateCameraCapture (int index)
121 CV_CAP_IEEE1394, // identical to CV_CAP_DC1394
130 CV_CAP_VFW, // identical to CV_CAP_V4L
135 #ifdef HAVE_QUICKTIME
144 #ifdef HAVE_ANDROID_NATIVE_CAMERA
150 #ifdef HAVE_AVFOUNDATION
159 // interpret preferred interface (0 = autodetect)
160 int pref = (index / 100) * 100;
168 // try every possibly installed camera API
169 for (int i = 0; domains[i] >= 0; i++)
171 #if defined(HAVE_DSHOW) || \
172 defined(HAVE_MSMF) || \
173 defined(HAVE_TYZX) || \
174 defined(HAVE_VFW) || \
175 defined(HAVE_LIBV4L) || \
176 defined(HAVE_CAMV4L) || \
177 defined(HAVE_CAMV4L2) || \
178 defined(HAVE_VIDEOIO) || \
179 defined(HAVE_GSTREAMER) || \
180 defined(HAVE_DC1394_2) || \
181 defined(HAVE_DC1394) || \
182 defined(HAVE_CMU1394) || \
183 defined(HAVE_MIL) || \
184 defined(HAVE_QUICKTIME) || \
185 defined(HAVE_UNICAP) || \
186 defined(HAVE_PVAPI) || \
187 defined(HAVE_OPENNI) || \
188 defined(HAVE_XIMEA) || \
189 defined(HAVE_AVFOUNDATION) || \
190 defined(HAVE_ANDROID_NATIVE_CAMERA) || \
191 defined(HAVE_GIGE_API) || \
193 // local variable to memorize the captured device
201 capture = cvCreateCameraCapture_MSMF (index);
208 capture = cvCreateCameraCapture_DShow (index);
216 capture = cvCreateCameraCapture_TYZX (index);
224 capture = cvCreateCameraCapture_VFW (index);
229 #if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
230 capture = cvCreateCameraCapture_V4L (index);
235 #ifdef HAVE_GSTREAMER
236 capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, 0);
239 capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, 0);
245 case CV_CAP_FIREWIRE:
247 capture = cvCreateCameraCapture_DC1394_2 (index);
253 capture = cvCreateCameraCapture_DC1394 (index);
259 capture = cvCreateCameraCapture_CMU (index);
264 #if defined(HAVE_GSTREAMER) && 0
265 //Re-enable again when gstreamer 1394 support will land in the backend code
266 capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
270 break; //CV_CAP_FIREWIRE
274 capture = cvCreateCameraCapture_MIL (index);
280 #ifdef HAVE_QUICKTIME
282 capture = cvCreateCameraCapture_QT (index);
290 capture = cvCreateCameraCapture_Unicap (index);
298 capture = cvCreateCameraCapture_PvAPI (index);
306 capture = cvCreateCameraCapture_OpenNI (index);
312 #ifdef HAVE_ANDROID_NATIVE_CAMERA
314 capture = cvCreateCameraCapture_Android (index);
322 capture = cvCreateCameraCapture_XIMEA (index);
328 #ifdef HAVE_AVFOUNDATION
329 case CV_CAP_AVFOUNDATION:
330 capture = cvCreateCameraCapture_AVFoundation (index);
337 case CV_CAP_GIGANETIX:
338 capture = cvCreateCameraCapture_Giganetix (index);
341 break; // CV_CAP_GIGANETIX
346 // failed open a camera
351 * Videoreader dispatching method: it tries to find the first
352 * API that can access a given filename.
354 CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
356 CvCapture * result = 0;
359 result = cvCreateFileCapture_FFMPEG_proxy (filename);
363 result = cvCreateFileCapture_XINE (filename);
366 #ifdef HAVE_GSTREAMER
368 result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
371 #ifdef HAVE_QUICKTIME
373 result = cvCreateFileCapture_QT (filename);
376 #ifdef HAVE_AVFOUNDATION
378 result = cvCreateFileCapture_AVFoundation (filename);
383 result = cvCreateFileCapture_OpenNI (filename);
387 result = cvCreateFileCapture_Images (filename);
393 * Videowriter dispatching method: it tries to find the first
394 * API that can write a given stream.
396 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
397 double fps, CvSize frameSize, int is_color )
399 //CV_FUNCNAME( "cvCreateVideoWriter" );
401 CvVideoWriter *result = 0;
404 result = cvCreateVideoWriter_Images(filename);
407 result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
411 result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
414 #ifdef HAVE_AVFOUNDATION
416 result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
419 #ifdef HAVE_QUICKTIME
421 result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
424 #ifdef HAVE_GSTREAMER
426 result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
430 result = cvCreateVideoWriter_Images(filename);
435 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
437 return writer ? writer->writeFrame(image) : 0;
440 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
442 if( pwriter && *pwriter )
452 VideoCapture::VideoCapture()
455 VideoCapture::VideoCapture(const string& filename)
460 VideoCapture::VideoCapture(int device)
465 VideoCapture::~VideoCapture()
470 bool VideoCapture::open(const string& filename)
473 cap = cvCreateFileCapture(filename.c_str());
477 bool VideoCapture::open(int device)
480 cap = cvCreateCameraCapture(device);
484 bool VideoCapture::isOpened() const { return !cap.empty(); }
486 void VideoCapture::release()
491 bool VideoCapture::grab()
493 return cvGrabFrame(cap) != 0;
496 bool VideoCapture::retrieve(Mat& image, int channel)
498 IplImage* _img = cvRetrieveFrame(cap, channel);
504 if(_img->origin == IPL_ORIGIN_TL)
509 flip(temp, image, 0);
514 bool VideoCapture::read(Mat& image)
520 return !image.empty();
523 VideoCapture& VideoCapture::operator >> (Mat& image)
529 bool VideoCapture::set(int propId, double value)
531 return cvSetCaptureProperty(cap, propId, value) != 0;
534 double VideoCapture::get(int propId)
536 return cvGetCaptureProperty(cap, propId);
539 VideoWriter::VideoWriter()
542 VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor)
544 open(filename, fourcc, fps, frameSize, isColor);
547 void VideoWriter::release()
552 VideoWriter::~VideoWriter()
557 bool VideoWriter::open(const string& filename, int fourcc, double fps, Size frameSize, bool isColor)
559 writer = cvCreateVideoWriter(filename.c_str(), fourcc, fps, frameSize, isColor);
563 bool VideoWriter::isOpened() const
565 return !writer.empty();
568 void VideoWriter::write(const Mat& image)
570 IplImage _img = image;
571 cvWriteFrame(writer, &_img);
574 VideoWriter& VideoWriter::operator << (const Mat& image)