cap.cpp 18.7 KB
Newer Older
wester committed
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/*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"
#include "cap_intelperc.hpp"
#include "cap_dshow.hpp"

// All WinRT versions older than 8.0 should provide classes used for video support
#if defined(WINRT) && !defined(WINRT_8_0) && defined(__cplusplus_winrt)
#   include "cap_winrt_capture.hpp"
#   include "cap_winrt_bridge.hpp"
#   define WINRT_VIDEO
#endif

#if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
#pragma optimize("",off)
#pragma warning(disable: 4748)
#endif

namespace cv
{

template<> void DefaultDeleter<CvCapture>::operator ()(CvCapture* obj) const
{ cvReleaseCapture(&obj); }

template<> void DefaultDeleter<CvVideoWriter>::operator ()(CvVideoWriter* obj) const
{ cvReleaseVideoWriter(&obj); }

}

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

static inline double icvGetCaptureProperty( const CvCapture* capture, int id )
{
    return capture ? capture->getProperty(id) : 0;
}

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

CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
{
    if(!capture)
        return 0;
    if(!capture->grabFrame())
        return 0;
    return capture->retrieveFrame(0);
}


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 icvGetCaptureProperty(capture, id);
}

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)
{
    // interpret preferred interface (0 = autodetect)
    int pref = (index / 100) * 100;

    // remove pref from index
    index -= pref;

    // local variable to memorize the captured device
    CvCapture *capture = 0;

    switch (pref)
    {
    default:
        // user specified an API we do not know
        // bail out to let the user know that it is not available
        if (pref) break;

#ifdef HAVE_MSMF
a  
Kai Westerkamp committed
146 147 148
    case CV_CAP_MSMF:
        if (!capture)
            capture = cvCreateCameraCapture_MSMF(index);
wester committed
149 150
        if (pref) break;
#endif
a  
Kai Westerkamp committed
151 152 153 154 155 156 157
#ifdef HAVE_TYZX
    case CV_CAP_STEREO:
        if (!capture)
            capture = cvCreateCameraCapture_TYZX(index);
        if (pref) break;
#endif
    case CV_CAP_VFW:
wester committed
158
#ifdef HAVE_VFW
a  
Kai Westerkamp committed
159 160
        if (!capture)
            capture = cvCreateCameraCapture_VFW(index);
wester committed
161 162
#endif
#if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
a  
Kai Westerkamp committed
163 164
        if (!capture)
            capture = cvCreateCameraCapture_V4L(index);
wester committed
165 166 167
#endif

#ifdef HAVE_GSTREAMER
a  
Kai Westerkamp committed
168 169 170
        if (!capture)
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2,
                                                reinterpret_cast<char *>(index));
wester committed
171

a  
Kai Westerkamp committed
172 173 174
        if (!capture)
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L,
                                                reinterpret_cast<char *>(index));
wester committed
175
#endif
a  
Kai Westerkamp committed
176
        if (pref) break; // CV_CAP_VFW
wester committed
177

a  
Kai Westerkamp committed
178
    case CV_CAP_FIREWIRE:
wester committed
179
#ifdef HAVE_DC1394_2
a  
Kai Westerkamp committed
180 181
        if (!capture)
            capture = cvCreateCameraCapture_DC1394_2(index);
wester committed
182 183 184
#endif

#ifdef HAVE_DC1394
a  
Kai Westerkamp committed
185 186
        if (!capture)
            capture = cvCreateCameraCapture_DC1394(index);
wester committed
187 188 189
#endif

#ifdef HAVE_CMU1394
a  
Kai Westerkamp committed
190 191
        if (!capture)
            capture = cvCreateCameraCapture_CMU(index);
wester committed
192 193 194 195
#endif

#if defined(HAVE_GSTREAMER) && 0
        // Re-enable again when gstreamer 1394 support will land in the backend code
a  
Kai Westerkamp committed
196 197
        if (!capture)
            capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
wester committed
198
#endif
a  
Kai Westerkamp committed
199
        if (pref) break; // CV_CAP_FIREWIRE
wester committed
200 201

#ifdef HAVE_MIL
a  
Kai Westerkamp committed
202 203 204
    case CV_CAP_MIL:
        if (!capture)
            capture = cvCreateCameraCapture_MIL(index);
wester committed
205 206 207 208
        if (pref) break;
#endif

#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
a  
Kai Westerkamp committed
209 210 211
    case CV_CAP_QT:
        if (!capture)
            capture = cvCreateCameraCapture_QT(index);
wester committed
212 213 214 215
        if (pref) break;
#endif

#ifdef HAVE_UNICAP
a  
Kai Westerkamp committed
216 217 218
    case CV_CAP_UNICAP:
        if (!capture)
            capture = cvCreateCameraCapture_Unicap(index);
wester committed
219 220 221 222
        if (pref) break;
#endif

#ifdef HAVE_PVAPI
a  
Kai Westerkamp committed
223 224 225
    case CV_CAP_PVAPI:
        if (!capture)
            capture = cvCreateCameraCapture_PvAPI(index);
wester committed
226 227 228 229
        if (pref) break;
#endif

#ifdef HAVE_OPENNI
a  
Kai Westerkamp committed
230 231 232
    case CV_CAP_OPENNI:
        if (!capture)
            capture = cvCreateCameraCapture_OpenNI(index);
wester committed
233 234 235 236
        if (pref) break;
#endif

#ifdef HAVE_OPENNI2
a  
Kai Westerkamp committed
237 238 239
    case CV_CAP_OPENNI2:
        if (!capture)
            capture = cvCreateCameraCapture_OpenNI2(index);
wester committed
240 241 242 243
        if (pref) break;
#endif

#ifdef HAVE_XIMEA
a  
Kai Westerkamp committed
244 245 246
    case CV_CAP_XIAPI:
        if (!capture)
            capture = cvCreateCameraCapture_XIMEA(index);
wester committed
247 248 249 250
        if (pref) break;
#endif

#ifdef HAVE_AVFOUNDATION
a  
Kai Westerkamp committed
251 252 253
    case CV_CAP_AVFOUNDATION:
        if (!capture)
            capture = cvCreateCameraCapture_AVFoundation(index);
wester committed
254 255 256 257
        if (pref) break;
#endif

#ifdef HAVE_GIGE_API
a  
Kai Westerkamp committed
258 259 260 261
    case CV_CAP_GIGANETIX:
        if (!capture)
            capture = cvCreateCameraCapture_Giganetix(index);
        if (pref) break; // CV_CAP_GIGANETIX
wester committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
#endif
    }

    return capture;
}

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

    switch(apiPreference) {
    default:
        // user specified an API we do not know
        // bail out to let the user know that it is not available
        if (apiPreference) break;

#ifdef HAVE_FFMPEG
a  
Kai Westerkamp committed
283 284 285
    case CV_CAP_FFMPEG:
        if (! result)
            result = cvCreateFileCapture_FFMPEG_proxy (filename);
wester committed
286 287 288 289
        if (apiPreference) break;
#endif

#ifdef HAVE_VFW
a  
Kai Westerkamp committed
290 291 292
    case CV_CAP_VFW:
        if (! result)
            result = cvCreateFileCapture_VFW (filename);
wester committed
293
        if (apiPreference) break;
a  
Kai Westerkamp committed
294
#endif
wester committed
295

a  
Kai Westerkamp committed
296
    case CV_CAP_MSMF:
wester committed
297
#ifdef HAVE_MSMF
a  
Kai Westerkamp committed
298 299
        if (! result)
            result = cvCreateFileCapture_MSMF (filename);
wester committed
300 301 302
#endif

#ifdef HAVE_XINE
a  
Kai Westerkamp committed
303 304
        if (! result)
            result = cvCreateFileCapture_XINE (filename);
wester committed
305 306 307 308
#endif
        if (apiPreference) break;

#ifdef HAVE_GSTREAMER
a  
Kai Westerkamp committed
309 310 311
    case CV_CAP_GSTREAMER:
        if (! result)
            result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
wester committed
312 313 314 315
        if (apiPreference) break;
#endif

#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
a  
Kai Westerkamp committed
316 317 318
    case CV_CAP_QT:
        if (! result)
            result = cvCreateFileCapture_QT (filename);
wester committed
319 320 321 322
        if (apiPreference) break;
#endif

#ifdef HAVE_AVFOUNDATION
a  
Kai Westerkamp committed
323 324 325
    case CV_CAP_AVFOUNDATION:
        if (! result)
            result = cvCreateFileCapture_AVFoundation (filename);
wester committed
326 327 328 329
        if (apiPreference) break;
#endif

#ifdef HAVE_OPENNI
a  
Kai Westerkamp committed
330 331 332
    case CV_CAP_OPENNI:
        if (! result)
            result = cvCreateFileCapture_OpenNI (filename);
wester committed
333 334 335
        if (apiPreference) break;
#endif

a  
Kai Westerkamp committed
336 337 338
    case CV_CAP_IMAGES:
        if (! result)
            result = cvCreateFileCapture_Images (filename);
wester committed
339 340 341 342 343 344 345
    }

    return result;
}

CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
{
a  
Kai Westerkamp committed
346
    return cvCreateFileCaptureWithPreference(filename, CV_CAP_ANY);
wester committed
347 348 349 350 351 352
}

/**
 * Videowriter dispatching method: it tries to find the first
 * API that can write a given stream.
 */
a  
Kai Westerkamp committed
353
CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
wester committed
354 355
                                            double fps, CvSize frameSize, int is_color )
{
a  
Kai Westerkamp committed
356
    //CV_FUNCNAME( "cvCreateVideoWriter" );
wester committed
357 358 359 360

    CvVideoWriter *result = 0;

    if(!fourcc || !fps)
a  
Kai Westerkamp committed
361
        result = cvCreateVideoWriter_Images(filename);
wester committed
362

a  
Kai Westerkamp committed
363 364 365 366
#ifdef HAVE_FFMPEG
    if(!result)
        result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
#endif
wester committed
367

a  
Kai Westerkamp committed
368 369 370 371
#ifdef HAVE_VFW
    if(!result)
        result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
#endif
wester committed
372

a  
Kai Westerkamp committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
#ifdef HAVE_MSMF
    if (!result)
        result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
#endif

/*  #ifdef HAVE_XINE
    if(!result)
        result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
    #endif
*/
#ifdef HAVE_AVFOUNDATION
    if (! result)
        result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
#endif

#if defined(HAVE_QUICKTIME) || defined(HAVE_QTKIT)
    if(!result)
        result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
#endif

#ifdef HAVE_GSTREAMER
    if (! result)
        result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
#endif

#if !defined(HAVE_FFMPEG) && \
    !defined(HAVE_VFW) && \
    !defined(HAVE_MSMF) && \
    !defined(HAVE_AVFOUNDATION) && \
    !defined(HAVE_QUICKTIME) && \
    !defined(HAVE_QTKIT) && \
    !defined(HAVE_GSTREAMER)
// If none of the writers is used
// these statements suppress 'unused parameter' warnings.
    (void)frameSize;
    (void)is_color;
#endif

    if(!result)
        result = cvCreateVideoWriter_Images(filename);

    return result;
wester committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
}

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
{

static Ptr<IVideoCapture> IVideoCapture_create(int index)
{
    int  domains[] =
    {
#ifdef HAVE_DSHOW
a  
Kai Westerkamp committed
439
        CV_CAP_DSHOW,
wester committed
440 441
#endif
#ifdef HAVE_INTELPERC
a  
Kai Westerkamp committed
442
        CV_CAP_INTELPERC,
wester committed
443 444 445 446 447
#endif
#ifdef WINRT_VIDEO
        CAP_WINRT,
#endif
#ifdef HAVE_GPHOTO2
a  
Kai Westerkamp committed
448
        CV_CAP_GPHOTO2,
wester committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
#endif
        -1, -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++)
    {
#if defined(HAVE_DSHOW)        || \
    defined(HAVE_INTELPERC)    || \
    defined(WINRT_VIDEO)       || \
    defined(HAVE_GPHOTO2)      || \
    (0)
        Ptr<IVideoCapture> capture;

        switch (domains[i])
        {
#ifdef HAVE_DSHOW
a  
Kai Westerkamp committed
475
            case CV_CAP_DSHOW:
wester committed
476
                capture = makePtr<VideoCapture_DShow>(index);
a  
Kai Westerkamp committed
477
                break; // CV_CAP_DSHOW
wester committed
478 479
#endif
#ifdef HAVE_INTELPERC
a  
Kai Westerkamp committed
480
            case CV_CAP_INTELPERC:
wester committed
481
                capture = makePtr<VideoCapture_IntelPerC>();
a  
Kai Westerkamp committed
482
                break; // CV_CAP_INTEL_PERC
wester committed
483 484 485 486 487 488 489 490 491
#endif
#ifdef WINRT_VIDEO
        case CAP_WINRT:
            capture = Ptr<IVideoCapture>(new cv::VideoCapture_WinRT(index));
            if (capture)
                return capture;
            break; // CAP_WINRT
#endif
#ifdef HAVE_GPHOTO2
a  
Kai Westerkamp committed
492
            case CV_CAP_GPHOTO2:
wester committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
                capture = createGPhoto2Capture(index);
                break;
#endif
        }
        if (capture && capture->isOpened())
            return capture;
#endif
    }

    // failed open a camera
    return Ptr<IVideoCapture>();
}


static Ptr<IVideoCapture> IVideoCapture_create(const String& filename)
{
    int  domains[] =
    {
a  
Kai Westerkamp committed
511
        CV_CAP_ANY,
wester committed
512
#ifdef HAVE_GPHOTO2
a  
Kai Westerkamp committed
513
        CV_CAP_GPHOTO2,
wester committed
514 515 516 517 518 519 520 521 522 523 524
#endif
        -1, -1
    };

    // try every possibly installed camera API
    for (int i = 0; domains[i] >= 0; i++)
    {
        Ptr<IVideoCapture> capture;

        switch (domains[i])
        {
a  
Kai Westerkamp committed
525
        case CV_CAP_ANY:
wester committed
526 527 528
            capture = createMotionJpegCapture(filename);
            break;
#ifdef HAVE_GPHOTO2
a  
Kai Westerkamp committed
529
        case CV_CAP_GPHOTO2:
wester committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543
            capture = createGPhoto2Capture(filename);
            break;
#endif
        }

        if (capture && capture->isOpened())
        {
            return capture;
        }
    }
    // failed open a camera
    return Ptr<IVideoCapture>();
}

a  
Kai Westerkamp committed
544
static Ptr<IVideoWriter> IVideoWriter_create(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
wester committed
545 546
{
    Ptr<IVideoWriter> iwriter;
a  
Kai Westerkamp committed
547
    if( _fourcc == CV_FOURCC('M', 'J', 'P', 'G') )
wester committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
        iwriter = createMotionJpegWriter(filename, fps, frameSize, isColor);
    return iwriter;
}

VideoCapture::VideoCapture()
{}

VideoCapture::VideoCapture(const String& filename, int apiPreference)
{
    open(filename, apiPreference);
}

VideoCapture::VideoCapture(const String& filename)
{
    open(filename, CAP_ANY);
}

VideoCapture::VideoCapture(int index)
{
    open(index);
}

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

bool VideoCapture::open(const String& filename, int apiPreference)
{
    if (isOpened()) release();
    icap = IVideoCapture_create(filename);
    if (!icap.empty())
        return true;

    cap.reset(cvCreateFileCaptureWithPreference(filename.c_str(), apiPreference));
    return isOpened();
}

bool VideoCapture::open(const String& filename)
{
    return open(filename, CAP_ANY);
}

bool VideoCapture::open(int index)
{
    if (isOpened()) release();
    icap = IVideoCapture_create(index);
    if (!icap.empty())
        return true;
    cap.reset(cvCreateCameraCapture(index));
    return isOpened();
}

bool VideoCapture::isOpened() const
{
    return (!cap.empty() || !icap.empty());
}

void VideoCapture::release()
{
    icap.release();
    cap.release();
}

bool VideoCapture::grab()
{
    if (!icap.empty())
        return icap->grabFrame();
    return cvGrabFrame(cap) != 0;
}

bool VideoCapture::retrieve(OutputArray image, int channel)
{
    if (!icap.empty())
        return icap->retrieveFrame(channel, image);

    IplImage* _img = cvRetrieveFrame(cap, channel);
    if( !_img )
    {
        image.release();
        return false;
    }
    if(_img->origin == IPL_ORIGIN_TL)
        cv::cvarrToMat(_img).copyTo(image);
    else
    {
        Mat temp = cv::cvarrToMat(_img);
        flip(temp, image, 0);
    }
    return true;
}

bool VideoCapture::read(OutputArray image)
{
    if(grab())
        retrieve(image);
    else
        image.release();
    return !image.empty();
}

VideoCapture& VideoCapture::operator >> (Mat& image)
{
#ifdef WINRT_VIDEO
    if (grab())
    {
        if (retrieve(image))
        {
            std::lock_guard<std::mutex> lock(VideoioBridge::getInstance().inputBufferMutex);
            VideoioBridge& bridge = VideoioBridge::getInstance();

            // double buffering
            bridge.swapInputBuffers();
            auto p = bridge.frontInputPtr;

            bridge.bIsFrameNew = false;

            // needed here because setting Mat 'image' is not allowed by OutputArray in read()
            Mat m(bridge.getHeight(), bridge.getWidth(), CV_8UC3, p);
            image = m;
        }
    }
#else
    read(image);
#endif

    return *this;
}

VideoCapture& VideoCapture::operator >> (UMat& image)
{
    read(image);
    return *this;
}

bool VideoCapture::set(int propId, double value)
{
    if (!icap.empty())
        return icap->setProperty(propId, value);
    return cvSetCaptureProperty(cap, propId, value) != 0;
}

double VideoCapture::get(int propId) const
{
    if (!icap.empty())
        return icap->getProperty(propId);
    return icvGetCaptureProperty(cap, propId);
}


VideoWriter::VideoWriter()
{}

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

void VideoWriter::release()
{
    iwriter.release();
    writer.release();
}

VideoWriter::~VideoWriter()
{
    release();
}

bool VideoWriter::open(const String& filename, int _fourcc, double fps, Size frameSize, bool isColor)
{
    if (isOpened()) release();
a  
Kai Westerkamp committed
721
    iwriter = IVideoWriter_create(filename, _fourcc, fps, frameSize, isColor);
wester committed
722 723
    if (!iwriter.empty())
        return true;
a  
Kai Westerkamp committed
724
    writer.reset(cvCreateVideoWriter(filename.c_str(), _fourcc, fps, frameSize, isColor));
wester committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    return isOpened();
}

bool VideoWriter::isOpened() const
{
    return !iwriter.empty() || !writer.empty();
}


bool VideoWriter::set(int propId, double value)
{
    if (!iwriter.empty())
        return iwriter->setProperty(propId, value);
    return false;
}

double VideoWriter::get(int propId) const
{
    if (!iwriter.empty())
        return iwriter->getProperty(propId);
    return 0.;
}

void VideoWriter::write(const Mat& image)
{
    if( iwriter )
        iwriter->write(image);
    else
    {
        IplImage _img = image;
        cvWriteFrame(writer, &_img);
    }
}

VideoWriter& VideoWriter::operator << (const Mat& image)
{
    write(image);
    return *this;
}

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

}