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)
146 #ifdef HAVE_ANDROID_NATIVE_CAMERA
152 #ifdef HAVE_AVFOUNDATION
158 #ifdef HAVE_INTELPERC
164 // interpret preferred interface (0 = autodetect)
165 int pref = (index / 100) * 100;
173 // try every possibly installed camera API
174 for (int i = 0; domains[i] >= 0; i++)
176 #if defined(HAVE_MSMF) || \
177 defined(HAVE_TYZX) || \
178 defined(HAVE_VFW) || \
179 defined(HAVE_LIBV4L) || \
180 defined(HAVE_CAMV4L) || \
181 defined(HAVE_CAMV4L2) || \
182 defined(HAVE_VIDEOIO) || \
183 defined(HAVE_GSTREAMER) || \
184 defined(HAVE_DC1394_2) || \
185 defined(HAVE_DC1394) || \
186 defined(HAVE_CMU1394) || \
187 defined(HAVE_MIL) || \
188 defined(HAVE_QUICKTIME) || \
189 defined(HAVE_QTKIT) || \
190 defined(HAVE_UNICAP) || \
191 defined(HAVE_PVAPI) || \
192 defined(HAVE_OPENNI) || \
193 defined(HAVE_XIMEA) || \
194 defined(HAVE_AVFOUNDATION) || \
195 defined(HAVE_ANDROID_NATIVE_CAMERA) || \
196 defined(HAVE_GIGE_API) || \
197 defined(HAVE_INTELPERC) || \
199 // local variable to memorize the captured device
207 capture = cvCreateCameraCapture_MSMF (index);
214 capture = cvCreateCameraCapture_TYZX (index);
221 capture = cvCreateCameraCapture_VFW (index);
225 #if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
226 capture = cvCreateCameraCapture_V4L (index);
231 #ifdef HAVE_GSTREAMER
232 capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, 0);
235 capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, 0);
241 case CV_CAP_FIREWIRE:
243 capture = cvCreateCameraCapture_DC1394_2 (index);
249 capture = cvCreateCameraCapture_DC1394 (index);
255 capture = cvCreateCameraCapture_CMU (index);
260 #if defined(HAVE_GSTREAMER) && 0
261 //Re-enable again when gstreamer 1394 support will land in the backend code
262 capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
266 break; //CV_CAP_FIREWIRE
270 capture = cvCreateCameraCapture_MIL (index);
276 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
278 capture = cvCreateCameraCapture_QT (index);
286 capture = cvCreateCameraCapture_Unicap (index);
294 capture = cvCreateCameraCapture_PvAPI (index);
302 capture = cvCreateCameraCapture_OpenNI (index);
308 #ifdef HAVE_ANDROID_NATIVE_CAMERA
310 capture = cvCreateCameraCapture_Android (index);
318 capture = cvCreateCameraCapture_XIMEA (index);
324 #ifdef HAVE_AVFOUNDATION
325 case CV_CAP_AVFOUNDATION:
326 capture = cvCreateCameraCapture_AVFoundation (index);
333 case CV_CAP_GIGANETIX:
334 capture = cvCreateCameraCapture_Giganetix (index);
337 break; // CV_CAP_GIGANETIX
342 // failed open a camera
347 * Videoreader dispatching method: it tries to find the first
348 * API that can access a given filename.
350 CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
352 CvCapture * result = 0;
355 result = cvCreateFileCapture_FFMPEG_proxy (filename);
359 result = cvCreateFileCapture_VFW (filename);
364 result = cvCreateFileCapture_MSMF (filename);
369 result = cvCreateFileCapture_XINE (filename);
372 #ifdef HAVE_GSTREAMER
374 result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
377 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
379 result = cvCreateFileCapture_QT (filename);
382 #ifdef HAVE_AVFOUNDATION
384 result = cvCreateFileCapture_AVFoundation (filename);
389 result = cvCreateFileCapture_OpenNI (filename);
393 result = cvCreateFileCapture_Images (filename);
399 * Videowriter dispatching method: it tries to find the first
400 * API that can write a given stream.
402 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
403 double fps, CvSize frameSize, int is_color )
405 //CV_FUNCNAME( "cvCreateVideoWriter" );
407 CvVideoWriter *result = 0;
410 result = cvCreateVideoWriter_Images(filename);
413 result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
417 result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
422 result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
427 result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
430 #ifdef HAVE_AVFOUNDATION
432 result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
435 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
437 result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
440 #ifdef HAVE_GSTREAMER
442 result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
446 result = cvCreateVideoWriter_Images(filename);
451 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
453 return writer ? writer->writeFrame(image) : 0;
456 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
458 if( pwriter && *pwriter )
468 VideoCapture::VideoCapture()
471 VideoCapture::VideoCapture(const String& filename)
476 VideoCapture::VideoCapture(int device)
481 VideoCapture::~VideoCapture()
487 bool VideoCapture::open(const String& filename)
489 if (isOpened()) release();
490 cap.reset(cvCreateFileCapture(filename.c_str()));
494 bool VideoCapture::open(int device)
496 if (isOpened()) release();
497 icap = createCameraCapture(device);
500 cap.reset(cvCreateCameraCapture(device));
504 bool VideoCapture::isOpened() const
506 return (!cap.empty() || !icap.empty());
509 void VideoCapture::release()
515 bool VideoCapture::grab()
518 return icap->grabFrame();
519 return cvGrabFrame(cap) != 0;
522 bool VideoCapture::retrieve(OutputArray image, int channel)
525 return icap->retrieveFrame(channel, image);
527 IplImage* _img = cvRetrieveFrame(cap, channel);
533 if(_img->origin == IPL_ORIGIN_TL)
534 cv::cvarrToMat(_img).copyTo(image);
537 Mat temp = cv::cvarrToMat(_img);
538 flip(temp, image, 0);
543 bool VideoCapture::read(OutputArray image)
549 return !image.empty();
552 VideoCapture& VideoCapture::operator >> (Mat& image)
558 VideoCapture& VideoCapture::operator >> (UMat& image)
564 bool VideoCapture::set(int propId, double value)
567 return icap->setProperty(propId, value);
568 return cvSetCaptureProperty(cap, propId, value) != 0;
571 double VideoCapture::get(int propId)
574 return icap->getProperty(propId);
575 return cvGetCaptureProperty(cap, propId);
578 Ptr<IVideoCapture> VideoCapture::createCameraCapture(int index)
585 #ifdef HAVE_INTELPERC
591 // interpret preferred interface (0 = autodetect)
592 int pref = (index / 100) * 100;
600 // try every possibly installed camera API
601 for (int i = 0; domains[i] >= 0; i++)
603 #if defined(HAVE_DSHOW) || \
604 defined(HAVE_INTELPERC) || \
606 Ptr<IVideoCapture> capture;
612 capture = Ptr<IVideoCapture>(new cv::VideoCapture_DShow(index));
615 break; // CV_CAP_DSHOW
617 #ifdef HAVE_INTELPERC
618 case CV_CAP_INTELPERC:
619 capture = Ptr<IVideoCapture>(new cv::VideoCapture_IntelPerC());
622 break; // CV_CAP_INTEL_PERC
628 // failed open a camera
629 return Ptr<IVideoCapture>();
632 VideoWriter::VideoWriter()
635 VideoWriter::VideoWriter(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
637 open(filename, _fourcc, fps, frameSize, isColor);
640 void VideoWriter::release()
645 VideoWriter::~VideoWriter()
650 bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
652 writer.reset(cvCreateVideoWriter(filename.c_str(), _fourcc, fps, frameSize, isColor));
656 bool VideoWriter::isOpened() const
658 return !writer.empty();
661 void VideoWriter::write(const Mat& image)
663 IplImage _img = image;
664 cvWriteFrame(writer, &_img);
667 VideoWriter& VideoWriter::operator << (const Mat& image)
673 int VideoWriter::fourcc(char c1, char c2, char c3, char c4)
675 return (c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24);