Path: blob/master/modules/superres/src/frame_source.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., 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 documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// 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 damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/41#include "precomp.hpp"4243using namespace cv;44using namespace cv::cuda;45using namespace cv::superres;46using namespace cv::superres::detail;4748cv::superres::FrameSource::~FrameSource()49{50}5152//////////////////////////////////////////////////////53// EmptyFrameSource5455namespace56{57class EmptyFrameSource : public FrameSource58{59public:60void nextFrame(OutputArray frame) CV_OVERRIDE;61void reset() CV_OVERRIDE;62};6364void EmptyFrameSource::nextFrame(OutputArray frame)65{66frame.release();67}6869void EmptyFrameSource::reset()70{71}72}7374Ptr<FrameSource> cv::superres::createFrameSource_Empty()75{76return makePtr<EmptyFrameSource>();77}7879//////////////////////////////////////////////////////80// VideoFrameSource & CameraFrameSource8182#ifndef HAVE_OPENCV_VIDEOIO8384Ptr<FrameSource> cv::superres::createFrameSource_Video(const String& fileName)85{86CV_UNUSED(fileName);87CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");88return Ptr<FrameSource>();89}9091Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)92{93CV_UNUSED(deviceId);94CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");95return Ptr<FrameSource>();96}9798#else // HAVE_OPENCV_VIDEOIO99100namespace101{102class CaptureFrameSource : public FrameSource103{104public:105void nextFrame(OutputArray frame) CV_OVERRIDE;106107protected:108VideoCapture vc_;109110private:111Mat frame_;112};113114void CaptureFrameSource::nextFrame(OutputArray _frame)115{116if (_frame.kind() == _InputArray::MAT)117vc_ >> _frame.getMatRef();118else if(_frame.kind() == _InputArray::CUDA_GPU_MAT)119{120vc_ >> frame_;121arrCopy(frame_, _frame);122}123else if (_frame.isUMat())124vc_ >> *(UMat *)_frame.getObj();125else126{127// should never get here128CV_Error(Error::StsBadArg, "Failed to detect input frame kind" );129}130}131132class VideoFrameSource : public CaptureFrameSource133{134public:135VideoFrameSource(const String& fileName);136137void reset() CV_OVERRIDE;138139private:140String fileName_;141};142143VideoFrameSource::VideoFrameSource(const String& fileName) : fileName_(fileName)144{145reset();146}147148void VideoFrameSource::reset()149{150vc_.release();151vc_.open(fileName_);152CV_Assert( vc_.isOpened() );153}154155class CameraFrameSource : public CaptureFrameSource156{157public:158CameraFrameSource(int deviceId);159160void reset() CV_OVERRIDE;161162private:163int deviceId_;164};165166CameraFrameSource::CameraFrameSource(int deviceId) : deviceId_(deviceId)167{168reset();169}170171void CameraFrameSource::reset()172{173vc_.release();174vc_.open(deviceId_);175CV_Assert( vc_.isOpened() );176}177}178179Ptr<FrameSource> cv::superres::createFrameSource_Video(const String& fileName)180{181return makePtr<VideoFrameSource>(fileName);182}183184Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)185{186return makePtr<CameraFrameSource>(deviceId);187}188189#endif // HAVE_OPENCV_VIDEOIO190191//////////////////////////////////////////////////////192// VideoFrameSource_CUDA193194#ifndef HAVE_OPENCV_CUDACODEC195196Ptr<FrameSource> cv::superres::createFrameSource_Video_CUDA(const String& fileName)197{198CV_UNUSED(fileName);199CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");200}201202#else // HAVE_OPENCV_CUDACODEC203204namespace205{206class VideoFrameSource_CUDA : public FrameSource207{208public:209VideoFrameSource_CUDA(const String& fileName);210211void nextFrame(OutputArray frame);212void reset();213214private:215String fileName_;216Ptr<cudacodec::VideoReader> reader_;217GpuMat frame_;218};219220VideoFrameSource_CUDA::VideoFrameSource_CUDA(const String& fileName) : fileName_(fileName)221{222reset();223}224225void VideoFrameSource_CUDA::nextFrame(OutputArray _frame)226{227if (_frame.kind() == _InputArray::CUDA_GPU_MAT)228{229bool res = reader_->nextFrame(_frame.getGpuMatRef());230if (!res)231_frame.release();232}233else234{235bool res = reader_->nextFrame(frame_);236if (!res)237_frame.release();238else239arrCopy(frame_, _frame);240}241}242243void VideoFrameSource_CUDA::reset()244{245reader_ = cudacodec::createVideoReader(fileName_);246}247}248249Ptr<FrameSource> cv::superres::createFrameSource_Video_CUDA(const String& fileName)250{251return makePtr<VideoFrameSource_CUDA>(fileName);252}253254#endif // HAVE_OPENCV_CUDACODEC255256257