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