cap.cpp 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

44
#if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
45
#pragma optimize("",off)
46
#pragma warning(disable: 4748)
47 48 49 50 51
#endif

namespace cv
{

R
Roman Donchenko 已提交
52
template<> void DefaultDeleter<CvCapture>::operator ()(CvCapture* obj) const
53 54
{ cvReleaseCapture(&obj); }

R
Roman Donchenko 已提交
55
template<> void DefaultDeleter<CvVideoWriter>::operator ()(CvVideoWriter* obj) const
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
{ cvReleaseVideoWriter(&obj); }

}

/************************* Reading AVIs & Camera data **************************/

CV_IMPL void cvReleaseCapture( CvCapture** pcapture )
{
    if( pcapture && *pcapture )
    {
        delete *pcapture;
        *pcapture = 0;
    }
}

CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
{
73 74 75 76 77
    if(!capture)
        return 0;
    if(!capture->grabFrame())
        return 0;
    return capture->retrieveFrame(0);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}


CV_IMPL int cvGrabFrame( CvCapture* capture )
{
    return capture ? capture->grabFrame() : 0;
}

CV_IMPL IplImage* cvRetrieveFrame( CvCapture* capture, int idx )
{
    return capture ? capture->retrieveFrame(idx) : 0;
}

CV_IMPL double cvGetCaptureProperty( CvCapture* capture, int id )
{
    return capture ? capture->getProperty(id) : 0;
}

CV_IMPL int cvSetCaptureProperty( CvCapture* capture, int id, double value )
{
    return capture ? capture->setProperty(id, value) : 0;
}

CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
{
    return capture ? capture->getCaptureDomain() : 0;
}


/**
 * Camera dispatching method: index is the camera number.
 * If given an index from 0 to 99, it tries to find the first
 * API that can access a given camera index.
 * Add multiples of 100 to select an API.
 */
CV_IMPL CvCapture * cvCreateCameraCapture (int index)
{
    int  domains[] =
    {
117
#ifdef HAVE_DSHOW
118 119
        CV_CAP_DSHOW,
#endif
120 121 122
#ifdef HAVE_MSMF
        CV_CAP_MSMF,
#endif
V
Vadim Pisarevsky 已提交
123
#if 1
124
        CV_CAP_IEEE1394,   // identical to CV_CAP_DC1394
V
Vadim Pisarevsky 已提交
125 126
#endif
#ifdef HAVE_TYZX
127
        CV_CAP_STEREO,
V
Vadim Pisarevsky 已提交
128 129
#endif
#ifdef HAVE_PVAPI
130
        CV_CAP_PVAPI,
V
Vadim Pisarevsky 已提交
131 132
#endif
#if 1
133
        CV_CAP_VFW,        // identical to CV_CAP_V4L
V
Vadim Pisarevsky 已提交
134 135
#endif
#ifdef HAVE_MIL
136
        CV_CAP_MIL,
V
Vadim Pisarevsky 已提交
137
#endif
K
kamjagin 已提交
138
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
139
        CV_CAP_QT,
V
Vadim Pisarevsky 已提交
140 141
#endif
#ifdef HAVE_UNICAP
142
        CV_CAP_UNICAP,
V
Vadim Pisarevsky 已提交
143
#endif
H
hartmut 已提交
144
#ifdef HAVE_OPENNI
145
        CV_CAP_OPENNI,
H
hartmut 已提交
146 147
#endif
#ifdef HAVE_ANDROID_NATIVE_CAMERA
148
        CV_CAP_ANDROID,
149 150 151
#endif
#ifdef HAVE_XIMEA
        CV_CAP_XIAPI,
H
hartmut 已提交
152
#endif
V
Vadim Pisarevsky 已提交
153 154
#ifdef HAVE_AVFOUNDATION
        CV_CAP_AVFOUNDATION,
155 156 157
#endif
#ifdef HAVE_GIGE_API
        CV_CAP_GIGANETIX,
V
Vadim Pisarevsky 已提交
158
#endif
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        -1
    };

    // interpret preferred interface (0 = autodetect)
    int pref = (index / 100) * 100;
    if (pref)
    {
        domains[0]=pref;
        index %= 100;
        domains[1]=-1;
    }

    // try every possibly installed camera API
    for (int i = 0; domains[i] >= 0; i++)
    {
174 175
#if defined(HAVE_DSHOW)        || \
    defined(HAVE_MSMF)         || \
V
Vadim Pisarevsky 已提交
176 177 178
    defined(HAVE_TYZX)         || \
    defined(HAVE_VFW)          || \
    defined(HAVE_LIBV4L)       || \
179 180
    defined(HAVE_CAMV4L)       || \
    defined(HAVE_CAMV4L2)      || \
181
    defined(HAVE_VIDEOIO)      || \
V
Vadim Pisarevsky 已提交
182 183 184 185 186 187
    defined(HAVE_GSTREAMER)    || \
    defined(HAVE_DC1394_2)     || \
    defined(HAVE_DC1394)       || \
    defined(HAVE_CMU1394)      || \
    defined(HAVE_MIL)          || \
    defined(HAVE_QUICKTIME)    || \
188
    defined(HAVE_QTKIT)        || \
V
Vadim Pisarevsky 已提交
189 190 191 192 193 194
    defined(HAVE_UNICAP)       || \
    defined(HAVE_PVAPI)        || \
    defined(HAVE_OPENNI)       || \
    defined(HAVE_XIMEA)        || \
    defined(HAVE_AVFOUNDATION) || \
    defined(HAVE_ANDROID_NATIVE_CAMERA) || \
195
    defined(HAVE_GIGE_API) || \
V
Vadim Pisarevsky 已提交
196
    (0)
197 198
        // local variable to memorize the captured device
        CvCapture *capture;
V
Vadim Pisarevsky 已提交
199
#endif
200 201 202

        switch (domains[i])
        {
203 204 205
#ifdef HAVE_DSHOW
        case CV_CAP_DSHOW:
             capture = cvCreateCameraCapture_DShow (index);
206 207 208 209
             if (capture)
                 return capture;
            break;
#endif
210 211 212
#ifdef HAVE_MSMF
        case CV_CAP_MSMF:
             capture = cvCreateCameraCapture_MSMF (index);
A
Alexander Smorkalov 已提交
213 214
             if (capture)
                 return capture;
215
            break;
V
Vadim Pisarevsky 已提交
216 217
#endif
#ifdef HAVE_TYZX
218 219 220 221 222
        case CV_CAP_STEREO:
            capture = cvCreateCameraCapture_TYZX (index);
            if (capture)
                return capture;
            break;
V
Vadim Pisarevsky 已提交
223
#endif
224
        case CV_CAP_VFW:
225
#ifdef HAVE_VFW
226 227 228
            capture = cvCreateCameraCapture_VFW (index);
            if (capture)
                return capture;
V
Vadim Pisarevsky 已提交
229
#endif
230
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
231 232 233
            capture = cvCreateCameraCapture_V4L (index);
            if (capture)
                return capture;
V
Vadim Pisarevsky 已提交
234 235 236
#endif

#ifdef HAVE_GSTREAMER
237 238 239 240 241 242
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, 0);
            if (capture)
                return capture;
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, 0);
            if (capture)
                return capture;
V
Vadim Pisarevsky 已提交
243 244
#endif
            break; //CV_CAP_VFW
245 246

        case CV_CAP_FIREWIRE:
V
Vadim Pisarevsky 已提交
247
#ifdef HAVE_DC1394_2
248 249 250
            capture = cvCreateCameraCapture_DC1394_2 (index);
            if (capture)
                return capture;
V
Vadim Pisarevsky 已提交
251 252 253
#endif

#ifdef HAVE_DC1394
254 255 256
            capture = cvCreateCameraCapture_DC1394 (index);
            if (capture)
                return capture;
V
Vadim Pisarevsky 已提交
257 258 259
#endif

#ifdef HAVE_CMU1394
260 261 262
            capture = cvCreateCameraCapture_CMU (index);
            if (capture)
                return capture;
V
Vadim Pisarevsky 已提交
263 264 265 266
#endif

#if defined(HAVE_GSTREAMER) && 0
            //Re-enable again when gstreamer 1394 support will land in the backend code
267 268 269
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
            if (capture)
                return capture;
V
Vadim Pisarevsky 已提交
270 271 272 273
#endif
            break; //CV_CAP_FIREWIRE

#ifdef HAVE_MIL
274 275 276 277 278
        case CV_CAP_MIL:
            capture = cvCreateCameraCapture_MIL (index);
            if (capture)
                return capture;
            break;
V
Vadim Pisarevsky 已提交
279
#endif
280

281
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
282 283 284 285 286
        case CV_CAP_QT:
            capture = cvCreateCameraCapture_QT (index);
            if (capture)
                return capture;
            break;
V
Vadim Pisarevsky 已提交
287
#endif
288

V
Vadim Pisarevsky 已提交
289
#ifdef HAVE_UNICAP
290
        case CV_CAP_UNICAP:
291 292 293
            capture = cvCreateCameraCapture_Unicap (index);
            if (capture)
                return capture;
294
        break;
V
Vadim Pisarevsky 已提交
295
#endif
296

V
Vadim Pisarevsky 已提交
297
#ifdef HAVE_PVAPI
298
        case CV_CAP_PVAPI:
299 300 301
            capture = cvCreateCameraCapture_PvAPI (index);
            if (capture)
                return capture;
302
        break;
V
Vadim Pisarevsky 已提交
303
#endif
304

V
Vadim Pisarevsky 已提交
305
#ifdef HAVE_OPENNI
306
        case CV_CAP_OPENNI:
307 308 309
            capture = cvCreateCameraCapture_OpenNI (index);
            if (capture)
                return capture;
310
        break;
V
Vadim Pisarevsky 已提交
311
#endif
312

V
Vadim Pisarevsky 已提交
313
#ifdef HAVE_ANDROID_NATIVE_CAMERA
314
        case CV_CAP_ANDROID:
315 316 317 318
            capture = cvCreateCameraCapture_Android (index);
            if (capture)
                return capture;
        break;
V
Vadim Pisarevsky 已提交
319
#endif
320

V
Vadim Pisarevsky 已提交
321
#ifdef HAVE_XIMEA
322
        case CV_CAP_XIAPI:
V
Vadim Pisarevsky 已提交
323
            capture = cvCreateCameraCapture_XIMEA (index);
324 325
            if (capture)
                return capture;
326
        break;
V
Vadim Pisarevsky 已提交
327
#endif
328

V
Vadim Pisarevsky 已提交
329
#ifdef HAVE_AVFOUNDATION
330 331 332 333 334
        case CV_CAP_AVFOUNDATION:
            capture = cvCreateCameraCapture_AVFoundation (index);
            if (capture)
                return capture;
        break;
V
Vadim Pisarevsky 已提交
335
#endif
336 337 338 339 340 341 342 343 344

#ifdef HAVE_GIGE_API
        case CV_CAP_GIGANETIX:
            capture = cvCreateCameraCapture_Giganetix (index);
            if (capture)
                return capture;
        break; // CV_CAP_GIGANETIX
#endif

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
        }
    }

    // failed open a camera
    return 0;
}

/**
 * Videoreader dispatching method: it tries to find the first
 * API that can access a given filename.
 */
CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
{
    CvCapture * result = 0;

    if (! result)
361
        result = cvCreateFileCapture_FFMPEG_proxy (filename);
362

363
#ifdef HAVE_VFW
364
    if (! result)
365
        result = cvCreateFileCapture_VFW (filename);
366 367
#endif

368
#ifdef HAVE_MSMF
369
    if (! result)
370
        result = cvCreateFileCapture_MSMF (filename);
371 372
#endif

V
Vadim Pisarevsky 已提交
373
#ifdef HAVE_XINE
374 375
    if (! result)
        result = cvCreateFileCapture_XINE (filename);
V
Vadim Pisarevsky 已提交
376
#endif
377

V
Vadim Pisarevsky 已提交
378
#ifdef HAVE_GSTREAMER
379 380
    if (! result)
        result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
V
Vadim Pisarevsky 已提交
381
#endif
382

383
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
384 385
    if (! result)
        result = cvCreateFileCapture_QT (filename);
V
Vadim Pisarevsky 已提交
386
#endif
387

V
Vadim Pisarevsky 已提交
388
#ifdef HAVE_AVFOUNDATION
389 390
    if (! result)
        result = cvCreateFileCapture_AVFoundation (filename);
V
Vadim Pisarevsky 已提交
391
#endif
392

V
Vadim Pisarevsky 已提交
393
#ifdef HAVE_OPENNI
394 395
    if (! result)
        result = cvCreateFileCapture_OpenNI (filename);
V
Vadim Pisarevsky 已提交
396
#endif
397

398 399 400 401 402 403 404 405 406 407 408 409 410
    if (! result)
        result = cvCreateFileCapture_Images (filename);

    return result;
}

/**
 * Videowriter dispatching method: it tries to find the first
 * API that can write a given stream.
 */
CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
                                            double fps, CvSize frameSize, int is_color )
{
411
    //CV_FUNCNAME( "cvCreateVideoWriter" );
412

413
    CvVideoWriter *result = 0;
414

415 416
    if(!fourcc || !fps)
        result = cvCreateVideoWriter_Images(filename);
417

418 419
    if(!result)
        result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
420

421 422
#ifdef HAVE_VFW
    if(!result)
423
        result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
424 425 426 427 428 429
#endif

#ifdef HAVE_MSMF
    if (!result)
        result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
#endif
430

431 432 433 434
/*  #ifdef HAVE_XINE
    if(!result)
        result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
    #endif
435
*/
436
#ifdef HAVE_AVFOUNDATION
437 438
    if (! result)
        result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
V
Vadim Pisarevsky 已提交
439
#endif
440

441
#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
442 443
    if(!result)
        result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
V
Vadim Pisarevsky 已提交
444 445 446
#endif

#ifdef HAVE_GSTREAMER
447 448
    if (! result)
        result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
V
Vadim Pisarevsky 已提交
449 450
#endif

451 452
    if(!result)
        result = cvCreateVideoWriter_Images(filename);
453

454
    return result;
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
{
    return writer ? writer->writeFrame(image) : 0;
}

CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
{
    if( pwriter && *pwriter )
    {
        delete *pwriter;
        *pwriter = 0;
    }
}

namespace cv
{

VideoCapture::VideoCapture()
{}
476

477
VideoCapture::VideoCapture(const String& filename)
478 479 480
{
    open(filename);
}
481

482 483 484 485 486 487 488 489 490
VideoCapture::VideoCapture(int device)
{
    open(device);
}

VideoCapture::~VideoCapture()
{
    cap.release();
}
491

492
bool VideoCapture::open(const String& filename)
493
{
494
    if (isOpened()) release();
R
Roman Donchenko 已提交
495
    cap.reset(cvCreateFileCapture(filename.c_str()));
496 497
    return isOpened();
}
498

499 500
bool VideoCapture::open(int device)
{
501
    if (isOpened()) release();
R
Roman Donchenko 已提交
502
    cap.reset(cvCreateCameraCapture(device));
503 504
    return isOpened();
}
505

506
bool VideoCapture::isOpened() const { return !cap.empty(); }
507

508 509 510 511 512 513 514 515 516
void VideoCapture::release()
{
    cap.release();
}

bool VideoCapture::grab()
{
    return cvGrabFrame(cap) != 0;
}
517

518 519 520 521 522 523 524 525 526
bool VideoCapture::retrieve(Mat& image, int channel)
{
    IplImage* _img = cvRetrieveFrame(cap, channel);
    if( !_img )
    {
        image.release();
        return false;
    }
    if(_img->origin == IPL_ORIGIN_TL)
A
Andrey Kamaev 已提交
527
        image = cv::cvarrToMat(_img);
528 529
    else
    {
A
Andrey Kamaev 已提交
530
        Mat temp = cv::cvarrToMat(_img);
531 532 533 534
        flip(temp, image, 0);
    }
    return true;
}
V
Vadim Pisarevsky 已提交
535 536 537

bool VideoCapture::read(Mat& image)
{
538 539
    if(grab())
        retrieve(image);
540 541
    else
        image.release();
V
Vadim Pisarevsky 已提交
542 543
    return !image.empty();
}
544

545 546
VideoCapture& VideoCapture::operator >> (Mat& image)
{
547
    read(image);
548 549
    return *this;
}
550

551 552 553 554
bool VideoCapture::set(int propId, double value)
{
    return cvSetCaptureProperty(cap, propId, value) != 0;
}
555

556 557 558 559 560 561 562
double VideoCapture::get(int propId)
{
    return cvGetCaptureProperty(cap, propId);
}

VideoWriter::VideoWriter()
{}
563

564
VideoWriter::VideoWriter(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
565
{
566
    open(filename, _fourcc, fps, frameSize, isColor);
567 568
}

569
void VideoWriter::release()
570 571
{
    writer.release();
572 573
}

574 575 576
VideoWriter::~VideoWriter()
{
    release();
577
}
578

579
bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
580
{
R
Roman Donchenko 已提交
581
    writer.reset(cvCreateVideoWriter(filename.c_str(), _fourcc, fps, frameSize, isColor));
582 583 584 585 586 587
    return isOpened();
}

bool VideoWriter::isOpened() const
{
    return !writer.empty();
588
}
V
Vadim Pisarevsky 已提交
589 590

void VideoWriter::write(const Mat& image)
591 592 593
{
    IplImage _img = image;
    cvWriteFrame(writer, &_img);
V
Vadim Pisarevsky 已提交
594
}
595

V
Vadim Pisarevsky 已提交
596 597 598
VideoWriter& VideoWriter::operator << (const Mat& image)
{
    write(image);
599
    return *this;
600 601
}

602 603 604 605 606
int VideoWriter::fourcc(char c1, char c2, char c3, char c4)
{
    return (c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24);
}

607
}