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"
43 #include "cap_intelperc.hpp"
44 #include "cap_dshow.hpp"
46 #if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
47 #pragma optimize("",off)
48 #pragma warning(disable: 4748)
54 template<> void DefaultDeleter<CvCapture>::operator ()(CvCapture* obj) const
55 { cvReleaseCapture(&obj); }
57 template<> void DefaultDeleter<CvVideoWriter>::operator ()(CvVideoWriter* obj) const
58 { cvReleaseVideoWriter(&obj); }
62 /************************* Reading AVIs & Camera data **************************/
64 CV_IMPL void cvReleaseCapture( CvCapture** pcapture )
66 if( pcapture && *pcapture )
73 CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
77 if(!capture->grabFrame())
79 return capture->retrieveFrame(0);
83 CV_IMPL int cvGrabFrame( CvCapture* capture )
85 return capture ? capture->grabFrame() : 0;
88 CV_IMPL IplImage* cvRetrieveFrame( CvCapture* capture, int idx )
90 return capture ? capture->retrieveFrame(idx) : 0;
93 CV_IMPL double cvGetCaptureProperty( CvCapture* capture, int id )
95 return capture ? capture->getProperty(id) : 0;
98 CV_IMPL int cvSetCaptureProperty( CvCapture* capture, int id, double value )
100 return capture ? capture->setProperty(id, value) : 0;
103 CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
105 return capture ? capture->getCaptureDomain() : 0;
110 * Camera dispatching method: index is the camera number.
111 * If given an index from 0 to 99, it tries to find the first
112 * API that can access a given camera index.
113 * Add multiples of 100 to select an API.
115 CV_IMPL CvCapture * cvCreateCameraCapture (int index)
123 CV_CAP_IEEE1394, // identical to CV_CAP_DC1394
132 CV_CAP_VFW, // identical to CV_CAP_V4L
137 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
149 #ifdef HAVE_ANDROID_NATIVE_CAMERA
155 #ifdef HAVE_AVFOUNDATION
161 #ifdef HAVE_INTELPERC
167 // interpret preferred interface (0 = autodetect)
168 int pref = (index / 100) * 100;
176 // try every possibly installed camera API
177 for (int i = 0; domains[i] >= 0; i++)
179 #if defined(HAVE_MSMF) || \
180 defined(HAVE_TYZX) || \
181 defined(HAVE_VFW) || \
182 defined(HAVE_LIBV4L) || \
183 defined(HAVE_CAMV4L) || \
184 defined(HAVE_CAMV4L2) || \
185 defined(HAVE_VIDEOIO) || \
186 defined(HAVE_GSTREAMER) || \
187 defined(HAVE_DC1394_2) || \
188 defined(HAVE_DC1394) || \
189 defined(HAVE_CMU1394) || \
190 defined(HAVE_MIL) || \
191 defined(HAVE_QUICKTIME) || \
192 defined(HAVE_QTKIT) || \
193 defined(HAVE_UNICAP) || \
194 defined(HAVE_PVAPI) || \
195 defined(HAVE_OPENNI) || \
196 defined(HAVE_OPENNI2) || \
197 defined(HAVE_XIMEA) || \
198 defined(HAVE_AVFOUNDATION) || \
199 defined(HAVE_ANDROID_NATIVE_CAMERA) || \
200 defined(HAVE_GIGE_API) || \
201 defined(HAVE_INTELPERC) || \
203 // local variable to memorize the captured device
211 capture = cvCreateCameraCapture_MSMF (index);
218 capture = cvCreateCameraCapture_TYZX (index);
225 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 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
282 capture = cvCreateCameraCapture_QT (index);
290 capture = cvCreateCameraCapture_Unicap (index);
298 capture = cvCreateCameraCapture_PvAPI (index);
306 capture = cvCreateCameraCapture_OpenNI (index);
314 capture = cvCreateCameraCapture_OpenNI(index);
320 #ifdef HAVE_ANDROID_NATIVE_CAMERA
322 capture = cvCreateCameraCapture_Android (index);
330 capture = cvCreateCameraCapture_XIMEA (index);
336 #ifdef HAVE_AVFOUNDATION
337 case CV_CAP_AVFOUNDATION:
338 capture = cvCreateCameraCapture_AVFoundation (index);
345 case CV_CAP_GIGANETIX:
346 capture = cvCreateCameraCapture_Giganetix (index);
349 break; // CV_CAP_GIGANETIX
354 // failed open a camera
359 * Videoreader dispatching method: it tries to find the first
360 * API that can access a given filename.
362 CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
364 CvCapture * result = 0;
367 result = cvCreateFileCapture_FFMPEG_proxy (filename);
371 result = cvCreateFileCapture_VFW (filename);
376 result = cvCreateFileCapture_MSMF (filename);
381 result = cvCreateFileCapture_XINE (filename);
384 #ifdef HAVE_GSTREAMER
386 result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
389 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
391 result = cvCreateFileCapture_QT (filename);
394 #ifdef HAVE_AVFOUNDATION
396 result = cvCreateFileCapture_AVFoundation (filename);
401 result = cvCreateFileCapture_OpenNI (filename);
405 result = cvCreateFileCapture_Images (filename);
411 * Videowriter dispatching method: it tries to find the first
412 * API that can write a given stream.
414 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
415 double fps, CvSize frameSize, int is_color )
417 //CV_FUNCNAME( "cvCreateVideoWriter" );
419 CvVideoWriter *result = 0;
422 result = cvCreateVideoWriter_Images(filename);
425 result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
429 result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
434 result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
439 result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
442 #ifdef HAVE_AVFOUNDATION
444 result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
447 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
449 result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
452 #ifdef HAVE_GSTREAMER
454 result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
458 result = cvCreateVideoWriter_Images(filename);
463 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
465 return writer ? writer->writeFrame(image) : 0;
468 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
470 if( pwriter && *pwriter )
480 VideoCapture::VideoCapture()
483 VideoCapture::VideoCapture(const String& filename)
488 VideoCapture::VideoCapture(int device)
493 VideoCapture::~VideoCapture()
499 bool VideoCapture::open(const String& filename)
501 if (isOpened()) release();
502 cap.reset(cvCreateFileCapture(filename.c_str()));
506 bool VideoCapture::open(int device)
508 if (isOpened()) release();
509 icap = createCameraCapture(device);
512 cap.reset(cvCreateCameraCapture(device));
516 bool VideoCapture::isOpened() const
518 return (!cap.empty() || !icap.empty());
521 void VideoCapture::release()
527 bool VideoCapture::grab()
530 return icap->grabFrame();
531 return cvGrabFrame(cap) != 0;
534 bool VideoCapture::retrieve(OutputArray image, int channel)
537 return icap->retrieveFrame(channel, image);
539 IplImage* _img = cvRetrieveFrame(cap, channel);
545 if(_img->origin == IPL_ORIGIN_TL)
546 cv::cvarrToMat(_img).copyTo(image);
549 Mat temp = cv::cvarrToMat(_img);
550 flip(temp, image, 0);
555 bool VideoCapture::read(OutputArray image)
561 return !image.empty();
564 VideoCapture& VideoCapture::operator >> (Mat& image)
570 VideoCapture& VideoCapture::operator >> (UMat& image)
576 bool VideoCapture::set(int propId, double value)
579 return icap->setProperty(propId, value);
580 return cvSetCaptureProperty(cap, propId, value) != 0;
583 double VideoCapture::get(int propId)
586 return icap->getProperty(propId);
587 return cvGetCaptureProperty(cap, propId);
590 Ptr<IVideoCapture> VideoCapture::createCameraCapture(int index)
597 #ifdef HAVE_INTELPERC
603 // interpret preferred interface (0 = autodetect)
604 int pref = (index / 100) * 100;
612 // try every possibly installed camera API
613 for (int i = 0; domains[i] >= 0; i++)
615 #if defined(HAVE_DSHOW) || \
616 defined(HAVE_INTELPERC) || \
618 Ptr<IVideoCapture> capture;
624 capture = Ptr<IVideoCapture>(new cv::VideoCapture_DShow(index));
627 break; // CV_CAP_DSHOW
629 #ifdef HAVE_INTELPERC
630 case CV_CAP_INTELPERC:
631 capture = Ptr<IVideoCapture>(new cv::VideoCapture_IntelPerC());
634 break; // CV_CAP_INTEL_PERC
640 // failed open a camera
641 return Ptr<IVideoCapture>();
644 VideoWriter::VideoWriter()
647 VideoWriter::VideoWriter(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
649 open(filename, _fourcc, fps, frameSize, isColor);
652 void VideoWriter::release()
657 VideoWriter::~VideoWriter()
662 bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
664 writer.reset(cvCreateVideoWriter(filename.c_str(), _fourcc, fps, frameSize, isColor));
668 bool VideoWriter::isOpened() const
670 return !writer.empty();
673 void VideoWriter::write(const Mat& image)
675 IplImage _img = image;
676 cvWriteFrame(writer, &_img);
679 VideoWriter& VideoWriter::operator << (const Mat& image)
685 int VideoWriter::fourcc(char c1, char c2, char c3, char c4)
687 return (c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24);