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 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 #include "precomp.hpp"
43 
44 using namespace cv;
45 using namespace cv::cuda;
46 using namespace cv::superres;
47 using namespace cv::superres::detail;
48 
~FrameSource()49 cv::superres::FrameSource::~FrameSource()
50 {
51 }
52 
53 //////////////////////////////////////////////////////
54 // EmptyFrameSource
55 
56 namespace
57 {
58     class EmptyFrameSource : public FrameSource
59     {
60     public:
61         void nextFrame(OutputArray frame);
62         void reset();
63     };
64 
nextFrame(OutputArray frame)65     void EmptyFrameSource::nextFrame(OutputArray frame)
66     {
67         frame.release();
68     }
69 
reset()70     void EmptyFrameSource::reset()
71     {
72     }
73 }
74 
createFrameSource_Empty()75 Ptr<FrameSource> cv::superres::createFrameSource_Empty()
76 {
77     return makePtr<EmptyFrameSource>();
78 }
79 
80 //////////////////////////////////////////////////////
81 // VideoFrameSource & CameraFrameSource
82 
83 #ifndef HAVE_OPENCV_VIDEOIO
84 
createFrameSource_Video(const String & fileName)85 Ptr<FrameSource> cv::superres::createFrameSource_Video(const String& fileName)
86 {
87     (void) fileName;
88     CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
89     return Ptr<FrameSource>();
90 }
91 
createFrameSource_Camera(int deviceId)92 Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
93 {
94     (void) deviceId;
95     CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
96     return Ptr<FrameSource>();
97 }
98 
99 #else // HAVE_OPENCV_VIDEOIO
100 
101 namespace
102 {
103     class CaptureFrameSource : public FrameSource
104     {
105     public:
106         void nextFrame(OutputArray frame);
107 
108     protected:
109         VideoCapture vc_;
110 
111     private:
112         Mat frame_;
113     };
114 
nextFrame(OutputArray _frame)115     void CaptureFrameSource::nextFrame(OutputArray _frame)
116     {
117         if (_frame.kind() == _InputArray::MAT)
118             vc_ >> _frame.getMatRef();
119         else if(_frame.kind() == _InputArray::CUDA_GPU_MAT)
120         {
121             vc_ >> frame_;
122             arrCopy(frame_, _frame);
123         }
124         else if (_frame.isUMat())
125             vc_ >> *(UMat *)_frame.getObj();
126         else
127         {
128             // should never get here
129             CV_Error(Error::StsBadArg, "Failed to detect input frame kind" );
130         }
131     }
132 
133     class VideoFrameSource : public CaptureFrameSource
134     {
135     public:
136         VideoFrameSource(const String& fileName);
137 
138         void reset();
139 
140     private:
141         String fileName_;
142     };
143 
VideoFrameSource(const String & fileName)144     VideoFrameSource::VideoFrameSource(const String& fileName) : fileName_(fileName)
145     {
146         reset();
147     }
148 
reset()149     void VideoFrameSource::reset()
150     {
151         vc_.release();
152         vc_.open(fileName_);
153         CV_Assert( vc_.isOpened() );
154     }
155 
156     class CameraFrameSource : public CaptureFrameSource
157     {
158     public:
159         CameraFrameSource(int deviceId);
160 
161         void reset();
162 
163     private:
164         int deviceId_;
165     };
166 
CameraFrameSource(int deviceId)167     CameraFrameSource::CameraFrameSource(int deviceId) : deviceId_(deviceId)
168     {
169         reset();
170     }
171 
reset()172     void CameraFrameSource::reset()
173     {
174         vc_.release();
175         vc_.open(deviceId_);
176         CV_Assert( vc_.isOpened() );
177     }
178 }
179 
createFrameSource_Video(const String & fileName)180 Ptr<FrameSource> cv::superres::createFrameSource_Video(const String& fileName)
181 {
182     return makePtr<VideoFrameSource>(fileName);
183 }
184 
createFrameSource_Camera(int deviceId)185 Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
186 {
187     return makePtr<CameraFrameSource>(deviceId);
188 }
189 
190 #endif // HAVE_OPENCV_VIDEOIO
191 
192 //////////////////////////////////////////////////////
193 // VideoFrameSource_CUDA
194 
195 #ifndef HAVE_OPENCV_CUDACODEC
196 
createFrameSource_Video_CUDA(const String & fileName)197 Ptr<FrameSource> cv::superres::createFrameSource_Video_CUDA(const String& fileName)
198 {
199     (void) fileName;
200     CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
201     return Ptr<FrameSource>();
202 }
203 
204 #else // HAVE_OPENCV_CUDACODEC
205 
206 namespace
207 {
208     class VideoFrameSource_CUDA : public FrameSource
209     {
210     public:
211         VideoFrameSource_CUDA(const String& fileName);
212 
213         void nextFrame(OutputArray frame);
214         void reset();
215 
216     private:
217         String fileName_;
218         Ptr<cudacodec::VideoReader> reader_;
219         GpuMat frame_;
220     };
221 
VideoFrameSource_CUDA(const String & fileName)222     VideoFrameSource_CUDA::VideoFrameSource_CUDA(const String& fileName) : fileName_(fileName)
223     {
224         reset();
225     }
226 
nextFrame(OutputArray _frame)227     void VideoFrameSource_CUDA::nextFrame(OutputArray _frame)
228     {
229         if (_frame.kind() == _InputArray::CUDA_GPU_MAT)
230         {
231             bool res = reader_->nextFrame(_frame.getGpuMatRef());
232             if (!res)
233                 _frame.release();
234         }
235         else
236         {
237             bool res = reader_->nextFrame(frame_);
238             if (!res)
239                 _frame.release();
240             else
241                 arrCopy(frame_, _frame);
242         }
243     }
244 
reset()245     void VideoFrameSource_CUDA::reset()
246     {
247         reader_ = cudacodec::createVideoReader(fileName_);
248     }
249 }
250 
createFrameSource_Video_CUDA(const String & fileName)251 Ptr<FrameSource> cv::superres::createFrameSource_Video_CUDA(const String& fileName)
252 {
253     return makePtr<VideoFrameSource_CUDA>(fileName);
254 }
255 
256 #endif // HAVE_OPENCV_CUDACODEC
257