17cbf6191cfb587655420327dfc539762ae6a9f1
[profile/ivi/opencv.git] / modules / highgui / src / cap.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 "precomp.hpp"
43 #include "cap_intelperc.hpp"
44 #include "cap_dshow.hpp"
45
46 #if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
47 #pragma optimize("",off)
48 #pragma warning(disable: 4748)
49 #endif
50
51 namespace cv
52 {
53
54 template<> void DefaultDeleter<CvCapture>::operator ()(CvCapture* obj) const
55 { cvReleaseCapture(&obj); }
56
57 template<> void DefaultDeleter<CvVideoWriter>::operator ()(CvVideoWriter* obj) const
58 { cvReleaseVideoWriter(&obj); }
59
60 }
61
62 /************************* Reading AVIs & Camera data **************************/
63
64 CV_IMPL void cvReleaseCapture( CvCapture** pcapture )
65 {
66     if( pcapture && *pcapture )
67     {
68         delete *pcapture;
69         *pcapture = 0;
70     }
71 }
72
73 CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
74 {
75     if(!capture)
76         return 0;
77     if(!capture->grabFrame())
78         return 0;
79     return capture->retrieveFrame(0);
80 }
81
82
83 CV_IMPL int cvGrabFrame( CvCapture* capture )
84 {
85     return capture ? capture->grabFrame() : 0;
86 }
87
88 CV_IMPL IplImage* cvRetrieveFrame( CvCapture* capture, int idx )
89 {
90     return capture ? capture->retrieveFrame(idx) : 0;
91 }
92
93 CV_IMPL double cvGetCaptureProperty( CvCapture* capture, int id )
94 {
95     return capture ? capture->getProperty(id) : 0;
96 }
97
98 CV_IMPL int cvSetCaptureProperty( CvCapture* capture, int id, double value )
99 {
100     return capture ? capture->setProperty(id, value) : 0;
101 }
102
103 CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
104 {
105     return capture ? capture->getCaptureDomain() : 0;
106 }
107
108
109 /**
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.
114  */
115 CV_IMPL CvCapture * cvCreateCameraCapture (int index)
116 {
117     int  domains[] =
118     {
119 #ifdef HAVE_MSMF
120         CV_CAP_MSMF,
121 #endif
122 #if 1
123         CV_CAP_IEEE1394,   // identical to CV_CAP_DC1394
124 #endif
125 #ifdef HAVE_TYZX
126         CV_CAP_STEREO,
127 #endif
128 #ifdef HAVE_PVAPI
129         CV_CAP_PVAPI,
130 #endif
131 #if 1
132         CV_CAP_VFW,        // identical to CV_CAP_V4L
133 #endif
134 #ifdef HAVE_MIL
135         CV_CAP_MIL,
136 #endif
137 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
138         CV_CAP_QT,
139 #endif
140 #ifdef HAVE_UNICAP
141         CV_CAP_UNICAP,
142 #endif
143 #ifdef HAVE_OPENNI
144         CV_CAP_OPENNI,
145 #endif
146 #ifdef HAVE_ANDROID_NATIVE_CAMERA
147         CV_CAP_ANDROID,
148 #endif
149 #ifdef HAVE_XIMEA
150         CV_CAP_XIAPI,
151 #endif
152 #ifdef HAVE_AVFOUNDATION
153         CV_CAP_AVFOUNDATION,
154 #endif
155 #ifdef HAVE_GIGE_API
156         CV_CAP_GIGANETIX,
157 #endif
158 #ifdef HAVE_INTELPERC
159         CV_CAP_INTELPERC,
160 #endif
161         -1
162     };
163
164     // interpret preferred interface (0 = autodetect)
165     int pref = (index / 100) * 100;
166     if (pref)
167     {
168         domains[0]=pref;
169         index %= 100;
170         domains[1]=-1;
171     }
172
173     // try every possibly installed camera API
174     for (int i = 0; domains[i] >= 0; i++)
175     {
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)    || \
198     (0)
199         // local variable to memorize the captured device
200         CvCapture *capture;
201 #endif
202
203         switch (domains[i])
204         {
205 #ifdef HAVE_MSMF
206         case CV_CAP_MSMF:
207              capture = cvCreateCameraCapture_MSMF (index);
208              if (capture)
209                  return capture;
210             break;
211 #endif
212 #ifdef HAVE_TYZX
213         case CV_CAP_STEREO:
214             capture = cvCreateCameraCapture_TYZX (index);
215             if (capture)
216                 return capture;
217             break;
218 #endif
219         case CV_CAP_VFW:
220 #ifdef HAVE_VFW
221             capture = cvCreateCameraCapture_VFW (index);
222             if (capture)
223                 return capture;
224 #endif
225 #if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
226             capture = cvCreateCameraCapture_V4L (index);
227             if (capture)
228                 return capture;
229 #endif
230
231 #ifdef HAVE_GSTREAMER
232             capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, 0);
233             if (capture)
234                 return capture;
235             capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, 0);
236             if (capture)
237                 return capture;
238 #endif
239             break; //CV_CAP_VFW
240
241         case CV_CAP_FIREWIRE:
242 #ifdef HAVE_DC1394_2
243             capture = cvCreateCameraCapture_DC1394_2 (index);
244             if (capture)
245                 return capture;
246 #endif
247
248 #ifdef HAVE_DC1394
249             capture = cvCreateCameraCapture_DC1394 (index);
250             if (capture)
251                 return capture;
252 #endif
253
254 #ifdef HAVE_CMU1394
255             capture = cvCreateCameraCapture_CMU (index);
256             if (capture)
257                 return capture;
258 #endif
259
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);
263             if (capture)
264                 return capture;
265 #endif
266             break; //CV_CAP_FIREWIRE
267
268 #ifdef HAVE_MIL
269         case CV_CAP_MIL:
270             capture = cvCreateCameraCapture_MIL (index);
271             if (capture)
272                 return capture;
273             break;
274 #endif
275
276 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
277         case CV_CAP_QT:
278             capture = cvCreateCameraCapture_QT (index);
279             if (capture)
280                 return capture;
281             break;
282 #endif
283
284 #ifdef HAVE_UNICAP
285         case CV_CAP_UNICAP:
286             capture = cvCreateCameraCapture_Unicap (index);
287             if (capture)
288                 return capture;
289         break;
290 #endif
291
292 #ifdef HAVE_PVAPI
293         case CV_CAP_PVAPI:
294             capture = cvCreateCameraCapture_PvAPI (index);
295             if (capture)
296                 return capture;
297         break;
298 #endif
299
300 #ifdef HAVE_OPENNI
301         case CV_CAP_OPENNI:
302             capture = cvCreateCameraCapture_OpenNI (index);
303             if (capture)
304                 return capture;
305         break;
306 #endif
307
308 #ifdef HAVE_ANDROID_NATIVE_CAMERA
309         case CV_CAP_ANDROID:
310             capture = cvCreateCameraCapture_Android (index);
311             if (capture)
312                 return capture;
313         break;
314 #endif
315
316 #ifdef HAVE_XIMEA
317         case CV_CAP_XIAPI:
318             capture = cvCreateCameraCapture_XIMEA (index);
319             if (capture)
320                 return capture;
321         break;
322 #endif
323
324 #ifdef HAVE_AVFOUNDATION
325         case CV_CAP_AVFOUNDATION:
326             capture = cvCreateCameraCapture_AVFoundation (index);
327             if (capture)
328                 return capture;
329         break;
330 #endif
331
332 #ifdef HAVE_GIGE_API
333         case CV_CAP_GIGANETIX:
334             capture = cvCreateCameraCapture_Giganetix (index);
335             if (capture)
336                 return capture;
337         break; // CV_CAP_GIGANETIX
338 #endif
339         }
340     }
341
342     // failed open a camera
343     return 0;
344 }
345
346 /**
347  * Videoreader dispatching method: it tries to find the first
348  * API that can access a given filename.
349  */
350 CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
351 {
352     CvCapture * result = 0;
353
354     if (! result)
355         result = cvCreateFileCapture_FFMPEG_proxy (filename);
356
357 #ifdef HAVE_VFW
358     if (! result)
359         result = cvCreateFileCapture_VFW (filename);
360 #endif
361
362 #ifdef HAVE_MSMF
363     if (! result)
364         result = cvCreateFileCapture_MSMF (filename);
365 #endif
366
367 #ifdef HAVE_XINE
368     if (! result)
369         result = cvCreateFileCapture_XINE (filename);
370 #endif
371
372 #ifdef HAVE_GSTREAMER
373     if (! result)
374         result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
375 #endif
376
377 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
378     if (! result)
379         result = cvCreateFileCapture_QT (filename);
380 #endif
381
382 #ifdef HAVE_AVFOUNDATION
383     if (! result)
384         result = cvCreateFileCapture_AVFoundation (filename);
385 #endif
386
387 #ifdef HAVE_OPENNI
388     if (! result)
389         result = cvCreateFileCapture_OpenNI (filename);
390 #endif
391
392     if (! result)
393         result = cvCreateFileCapture_Images (filename);
394
395     return result;
396 }
397
398 /**
399  * Videowriter dispatching method: it tries to find the first
400  * API that can write a given stream.
401  */
402 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
403                                             double fps, CvSize frameSize, int is_color )
404 {
405     //CV_FUNCNAME( "cvCreateVideoWriter" );
406
407     CvVideoWriter *result = 0;
408
409     if(!fourcc || !fps)
410         result = cvCreateVideoWriter_Images(filename);
411
412     if(!result)
413         result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
414
415 #ifdef HAVE_VFW
416     if(!result)
417         result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
418 #endif
419
420 #ifdef HAVE_MSMF
421     if (!result)
422         result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
423 #endif
424
425 /*  #ifdef HAVE_XINE
426     if(!result)
427         result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
428     #endif
429 */
430 #ifdef HAVE_AVFOUNDATION
431     if (! result)
432         result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
433 #endif
434
435 #if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
436     if(!result)
437         result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
438 #endif
439
440 #ifdef HAVE_GSTREAMER
441     if (! result)
442         result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
443 #endif
444
445     if(!result)
446         result = cvCreateVideoWriter_Images(filename);
447
448     return result;
449 }
450
451 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
452 {
453     return writer ? writer->writeFrame(image) : 0;
454 }
455
456 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
457 {
458     if( pwriter && *pwriter )
459     {
460         delete *pwriter;
461         *pwriter = 0;
462     }
463 }
464
465 namespace cv
466 {
467
468 VideoCapture::VideoCapture()
469 {}
470
471 VideoCapture::VideoCapture(const String& filename)
472 {
473     open(filename);
474 }
475
476 VideoCapture::VideoCapture(int device)
477 {
478     open(device);
479 }
480
481 VideoCapture::~VideoCapture()
482 {
483     icap.release();
484     cap.release();
485 }
486
487 bool VideoCapture::open(const String& filename)
488 {
489     if (isOpened()) release();
490     cap.reset(cvCreateFileCapture(filename.c_str()));
491     return isOpened();
492 }
493
494 bool VideoCapture::open(int device)
495 {
496     if (isOpened()) release();
497     icap = createCameraCapture(device);
498     if (!icap.empty())
499         return true;
500     cap.reset(cvCreateCameraCapture(device));
501     return isOpened();
502 }
503
504 bool VideoCapture::isOpened() const
505 {
506     return (!cap.empty() || !icap.empty());
507 }
508
509 void VideoCapture::release()
510 {
511     icap.release();
512     cap.release();
513 }
514
515 bool VideoCapture::grab()
516 {
517     if (!icap.empty())
518         return icap->grabFrame();
519     return cvGrabFrame(cap) != 0;
520 }
521
522 bool VideoCapture::retrieve(OutputArray image, int channel)
523 {
524     if (!icap.empty())
525         return icap->retrieveFrame(channel, image);
526
527     IplImage* _img = cvRetrieveFrame(cap, channel);
528     if( !_img )
529     {
530         image.release();
531         return false;
532     }
533     if(_img->origin == IPL_ORIGIN_TL)
534         cv::cvarrToMat(_img).copyTo(image);
535     else
536     {
537         Mat temp = cv::cvarrToMat(_img);
538         flip(temp, image, 0);
539     }
540     return true;
541 }
542
543 bool VideoCapture::read(OutputArray image)
544 {
545     if(grab())
546         retrieve(image);
547     else
548         image.release();
549     return !image.empty();
550 }
551
552 VideoCapture& VideoCapture::operator >> (Mat& image)
553 {
554     read(image);
555     return *this;
556 }
557
558 VideoCapture& VideoCapture::operator >> (UMat& image)
559 {
560     read(image);
561     return *this;
562 }
563
564 bool VideoCapture::set(int propId, double value)
565 {
566     if (!icap.empty())
567         return icap->setProperty(propId, value);
568     return cvSetCaptureProperty(cap, propId, value) != 0;
569 }
570
571 double VideoCapture::get(int propId)
572 {
573     if (!icap.empty())
574         return icap->getProperty(propId);
575     return cvGetCaptureProperty(cap, propId);
576 }
577
578 Ptr<IVideoCapture> VideoCapture::createCameraCapture(int index)
579 {
580     int  domains[] =
581     {
582 #ifdef HAVE_DSHOW
583         CV_CAP_DSHOW,
584 #endif
585 #ifdef HAVE_INTELPERC
586         CV_CAP_INTELPERC,
587 #endif
588         -1, -1
589     };
590
591     // interpret preferred interface (0 = autodetect)
592     int pref = (index / 100) * 100;
593     if (pref)
594     {
595         domains[0]=pref;
596         index %= 100;
597         domains[1]=-1;
598     }
599
600     // try every possibly installed camera API
601     for (int i = 0; domains[i] >= 0; i++)
602     {
603 #if defined(HAVE_DSHOW)        || \
604     defined(HAVE_INTELPERC)    || \
605     (0)
606         Ptr<IVideoCapture> capture;
607
608         switch (domains[i])
609         {
610 #ifdef HAVE_DSHOW
611         case CV_CAP_DSHOW:
612             capture = Ptr<IVideoCapture>(new cv::VideoCapture_DShow(index));
613             if (capture)
614                 return capture;
615             break; // CV_CAP_DSHOW
616 #endif
617 #ifdef HAVE_INTELPERC
618         case CV_CAP_INTELPERC:
619             capture = Ptr<IVideoCapture>(new cv::VideoCapture_IntelPerC());
620             if (capture)
621                 return capture;
622             break; // CV_CAP_INTEL_PERC
623 #endif
624         }
625 #endif
626     }
627
628     // failed open a camera
629     return Ptr<IVideoCapture>();
630 }
631
632 VideoWriter::VideoWriter()
633 {}
634
635 VideoWriter::VideoWriter(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
636 {
637     open(filename, _fourcc, fps, frameSize, isColor);
638 }
639
640 void VideoWriter::release()
641 {
642     writer.release();
643 }
644
645 VideoWriter::~VideoWriter()
646 {
647     release();
648 }
649
650 bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
651 {
652     writer.reset(cvCreateVideoWriter(filename.c_str(), _fourcc, fps, frameSize, isColor));
653     return isOpened();
654 }
655
656 bool VideoWriter::isOpened() const
657 {
658     return !writer.empty();
659 }
660
661 void VideoWriter::write(const Mat& image)
662 {
663     IplImage _img = image;
664     cvWriteFrame(writer, &_img);
665 }
666
667 VideoWriter& VideoWriter::operator << (const Mat& image)
668 {
669     write(image);
670     return *this;
671 }
672
673 int VideoWriter::fourcc(char c1, char c2, char c3, char c4)
674 {
675     return (c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24);
676 }
677
678 }