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
// All WinRT versions older than 8.0 should provide classes used for video support
47
#if defined(WINRT) && !defined(WINRT_8_0) && defined(__cplusplus_winrt)
48
# include "cap_winrt_capture.hpp"
49
# include "cap_winrt_bridge.hpp"
53
#if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
54
#pragma optimize("",off)
55
#pragma warning(disable: 4748)
61
template<> void DefaultDeleter<CvCapture>::operator ()(CvCapture* obj) const
62
{ cvReleaseCapture(&obj); }
64
template<> void DefaultDeleter<CvVideoWriter>::operator ()(CvVideoWriter* obj) const
65
{ cvReleaseVideoWriter(&obj); }
69
/************************* Reading AVIs & Camera data **************************/
71
static inline double icvGetCaptureProperty( const CvCapture* capture, int id )
73
return capture ? capture->getProperty(id) : 0;
76
CV_IMPL void cvReleaseCapture( CvCapture** pcapture )
78
if( pcapture && *pcapture )
85
CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
89
if(!capture->grabFrame())
91
return capture->retrieveFrame(0);
95
CV_IMPL int cvGrabFrame( CvCapture* capture )
97
return capture ? capture->grabFrame() : 0;
100
CV_IMPL IplImage* cvRetrieveFrame( CvCapture* capture, int idx )
102
return capture ? capture->retrieveFrame(idx) : 0;
105
CV_IMPL double cvGetCaptureProperty( CvCapture* capture, int id )
107
return icvGetCaptureProperty(capture, id);
110
CV_IMPL int cvSetCaptureProperty( CvCapture* capture, int id, double value )
112
return capture ? capture->setProperty(id, value) : 0;
115
CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
117
return capture ? capture->getCaptureDomain() : 0;
122
* Camera dispatching method: index is the camera number.
123
* If given an index from 0 to 99, it tries to find the first
124
* API that can access a given camera index.
125
* Add multiples of 100 to select an API.
127
CV_IMPL CvCapture * cvCreateCameraCapture (int index)
129
// interpret preferred interface (0 = autodetect)
130
int pref = (index / 100) * 100;
132
// remove pref from index
135
// local variable to memorize the captured device
136
CvCapture *capture = 0;
141
// user specified an API we do not know
142
// bail out to let the user know that it is not available
148
capture = cvCreateCameraCapture_MSMF(index);
154
capture = cvCreateCameraCapture_TYZX(index);
160
capture = cvCreateCameraCapture_VFW(index);
162
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
164
capture = cvCreateCameraCapture_V4L(index);
167
#ifdef HAVE_GSTREAMER
169
capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2,
170
reinterpret_cast<char *>(index));
173
capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L,
174
reinterpret_cast<char *>(index));
176
if (pref) break; // CV_CAP_VFW
178
case CV_CAP_FIREWIRE:
181
capture = cvCreateCameraCapture_DC1394_2(index);
186
capture = cvCreateCameraCapture_DC1394(index);
191
capture = cvCreateCameraCapture_CMU(index);
194
#if defined(HAVE_GSTREAMER) && 0
195
// Re-enable again when gstreamer 1394 support will land in the backend code
197
capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
199
if (pref) break; // CV_CAP_FIREWIRE
204
capture = cvCreateCameraCapture_MIL(index);
208
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
211
capture = cvCreateCameraCapture_QT(index);
218
capture = cvCreateCameraCapture_Unicap(index);
225
capture = cvCreateCameraCapture_PvAPI(index);
232
capture = cvCreateCameraCapture_OpenNI(index);
239
capture = cvCreateCameraCapture_OpenNI2(index);
246
capture = cvCreateCameraCapture_XIMEA(index);
250
#ifdef HAVE_AVFOUNDATION
251
case CV_CAP_AVFOUNDATION:
253
capture = cvCreateCameraCapture_AVFoundation(index);
258
case CV_CAP_GIGANETIX:
260
capture = cvCreateCameraCapture_Giganetix(index);
261
if (pref) break; // CV_CAP_GIGANETIX
269
* Videoreader dispatching method: it tries to find the first
270
* API that can access a given filename.
272
CV_IMPL CvCapture * cvCreateFileCaptureWithPreference (const char * filename, int apiPreference)
274
CvCapture * result = 0;
276
switch(apiPreference) {
278
// user specified an API we do not know
279
// bail out to let the user know that it is not available
280
if (apiPreference) break;
285
result = cvCreateFileCapture_FFMPEG_proxy (filename);
286
if (apiPreference) break;
292
result = cvCreateFileCapture_VFW (filename);
293
if (apiPreference) break;
299
result = cvCreateFileCapture_MSMF (filename);
304
result = cvCreateFileCapture_XINE (filename);
306
if (apiPreference) break;
308
#ifdef HAVE_GSTREAMER
309
case CV_CAP_GSTREAMER:
311
result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
312
if (apiPreference) break;
315
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
318
result = cvCreateFileCapture_QT (filename);
319
if (apiPreference) break;
322
#ifdef HAVE_AVFOUNDATION
323
case CV_CAP_AVFOUNDATION:
325
result = cvCreateFileCapture_AVFoundation (filename);
326
if (apiPreference) break;
332
result = cvCreateFileCapture_OpenNI (filename);
333
if (apiPreference) break;
338
result = cvCreateFileCapture_Images (filename);
344
CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
346
return cvCreateFileCaptureWithPreference(filename, CV_CAP_ANY);
350
* Videowriter dispatching method: it tries to find the first
351
* API that can write a given stream.
353
CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
354
double fps, CvSize frameSize, int is_color )
356
//CV_FUNCNAME( "cvCreateVideoWriter" );
358
CvVideoWriter *result = 0;
361
result = cvCreateVideoWriter_Images(filename);
365
result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
370
result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
375
result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
380
result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
383
#ifdef HAVE_AVFOUNDATION
385
result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
388
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
390
result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
393
#ifdef HAVE_GSTREAMER
395
result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
398
#if !defined(HAVE_FFMPEG) && \
399
!defined(HAVE_VFW) && \
400
!defined(HAVE_MSMF) && \
401
!defined(HAVE_AVFOUNDATION) && \
402
!defined(HAVE_QUICKTIME) && \
403
!defined(HAVE_QTKIT) && \
404
!defined(HAVE_GSTREAMER)
405
// If none of the writers is used
406
// these statements suppress 'unused parameter' warnings.
412
result = cvCreateVideoWriter_Images(filename);
417
CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
419
return writer ? writer->writeFrame(image) : 0;
422
CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
424
if( pwriter && *pwriter )
434
static Ptr<IVideoCapture> IVideoCapture_create(int index)
441
#ifdef HAVE_INTELPERC
453
// interpret preferred interface (0 = autodetect)
454
int pref = (index / 100) * 100;
462
// try every possibly installed camera API
463
for (int i = 0; domains[i] >= 0; i++)
465
#if defined(HAVE_DSHOW) || \
466
defined(HAVE_INTELPERC) || \
467
defined(WINRT_VIDEO) || \
468
defined(HAVE_GPHOTO2) || \
470
Ptr<IVideoCapture> capture;
476
capture = makePtr<VideoCapture_DShow>(index);
477
break; // CV_CAP_DSHOW
479
#ifdef HAVE_INTELPERC
480
case CV_CAP_INTELPERC:
481
capture = makePtr<VideoCapture_IntelPerC>();
482
break; // CV_CAP_INTEL_PERC
486
capture = Ptr<IVideoCapture>(new cv::VideoCapture_WinRT(index));
493
capture = createGPhoto2Capture(index);
497
if (capture && capture->isOpened())
502
// failed open a camera
503
return Ptr<IVideoCapture>();
507
static Ptr<IVideoCapture> IVideoCapture_create(const String& filename)
518
// try every possibly installed camera API
519
for (int i = 0; domains[i] >= 0; i++)
521
Ptr<IVideoCapture> capture;
526
capture = createMotionJpegCapture(filename);
530
capture = createGPhoto2Capture(filename);
535
if (capture && capture->isOpened())
540
// failed open a camera
541
return Ptr<IVideoCapture>();
544
static Ptr<IVideoWriter> IVideoWriter_create(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
546
Ptr<IVideoWriter> iwriter;
547
if( _fourcc == CV_FOURCC('M', 'J', 'P', 'G') )
548
iwriter = createMotionJpegWriter(filename, fps, frameSize, isColor);
552
VideoCapture::VideoCapture()
555
VideoCapture::VideoCapture(const String& filename, int apiPreference)
557
open(filename, apiPreference);
560
VideoCapture::VideoCapture(const String& filename)
562
open(filename, CAP_ANY);
565
VideoCapture::VideoCapture(int index)
570
VideoCapture::~VideoCapture()
576
bool VideoCapture::open(const String& filename, int apiPreference)
578
if (isOpened()) release();
579
icap = IVideoCapture_create(filename);
583
cap.reset(cvCreateFileCaptureWithPreference(filename.c_str(), apiPreference));
587
bool VideoCapture::open(const String& filename)
589
return open(filename, CAP_ANY);
592
bool VideoCapture::open(int index)
594
if (isOpened()) release();
595
icap = IVideoCapture_create(index);
598
cap.reset(cvCreateCameraCapture(index));
602
bool VideoCapture::isOpened() const
604
return (!cap.empty() || !icap.empty());
607
void VideoCapture::release()
613
bool VideoCapture::grab()
616
return icap->grabFrame();
617
return cvGrabFrame(cap) != 0;
620
bool VideoCapture::retrieve(OutputArray image, int channel)
623
return icap->retrieveFrame(channel, image);
625
IplImage* _img = cvRetrieveFrame(cap, channel);
631
if(_img->origin == IPL_ORIGIN_TL)
632
cv::cvarrToMat(_img).copyTo(image);
635
Mat temp = cv::cvarrToMat(_img);
636
flip(temp, image, 0);
641
bool VideoCapture::read(OutputArray image)
647
return !image.empty();
650
VideoCapture& VideoCapture::operator >> (Mat& image)
657
std::lock_guard<std::mutex> lock(VideoioBridge::getInstance().inputBufferMutex);
658
VideoioBridge& bridge = VideoioBridge::getInstance();
661
bridge.swapInputBuffers();
662
auto p = bridge.frontInputPtr;
664
bridge.bIsFrameNew = false;
666
// needed here because setting Mat 'image' is not allowed by OutputArray in read()
667
Mat m(bridge.getHeight(), bridge.getWidth(), CV_8UC3, p);
678
VideoCapture& VideoCapture::operator >> (UMat& image)
684
bool VideoCapture::set(int propId, double value)
687
return icap->setProperty(propId, value);
688
return cvSetCaptureProperty(cap, propId, value) != 0;
691
double VideoCapture::get(int propId) const
694
return icap->getProperty(propId);
695
return icvGetCaptureProperty(cap, propId);
699
VideoWriter::VideoWriter()
702
VideoWriter::VideoWriter(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
704
open(filename, _fourcc, fps, frameSize, isColor);
707
void VideoWriter::release()
713
VideoWriter::~VideoWriter()
718
bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
720
if (isOpened()) release();
721
iwriter = IVideoWriter_create(filename, _fourcc, fps, frameSize, isColor);
722
if (!iwriter.empty())
724
writer.reset(cvCreateVideoWriter(filename.c_str(), _fourcc, fps, frameSize, isColor));
728
bool VideoWriter::isOpened() const
730
return !iwriter.empty() || !writer.empty();
734
bool VideoWriter::set(int propId, double value)
736
if (!iwriter.empty())
737
return iwriter->setProperty(propId, value);
741
double VideoWriter::get(int propId) const
743
if (!iwriter.empty())
744
return iwriter->getProperty(propId);
748
void VideoWriter::write(const Mat& image)
751
iwriter->write(image);
754
IplImage _img = image;
755
cvWriteFrame(writer, &_img);
759
VideoWriter& VideoWriter::operator << (const Mat& image)
765
int VideoWriter::fourcc(char c1, char c2, char c3, char c4)
767
return (c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24);