1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11
// For Open Source Computer Vision Library
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.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
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.
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.
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.
43
#include "precomp.hpp"
46
using namespace cv::cuda;
47
using namespace cv::cudacodec;
51
Ptr<VideoReader> cv::cudacodec::createVideoReader(const String&) { throw_no_cuda(); return Ptr<VideoReader>(); }
52
Ptr<VideoReader> cv::cudacodec::createVideoReader(const Ptr<RawVideoSource>&) { throw_no_cuda(); return Ptr<VideoReader>(); }
56
void videoDecPostProcessFrame(const GpuMat& decodedFrame, OutputArray _outFrame, int width, int height);
58
using namespace cv::cudacodec::detail;
62
class VideoReaderImpl : public VideoReader
65
explicit VideoReaderImpl(const Ptr<VideoSource>& source);
68
bool nextFrame(OutputArray frame);
70
FormatInfo format() const;
73
Ptr<VideoSource> videoSource_;
75
Ptr<FrameQueue> frameQueue_;
76
Ptr<VideoDecoder> videoDecoder_;
77
Ptr<VideoParser> videoParser_;
81
std::deque< std::pair<CUVIDPARSERDISPINFO, CUVIDPROCPARAMS> > frames_;
84
FormatInfo VideoReaderImpl::format() const
86
return videoSource_->format();
89
VideoReaderImpl::VideoReaderImpl(const Ptr<VideoSource>& source) :
94
GpuMat temp(1, 1, CV_8UC1);
98
cuSafeCall( cuCtxGetCurrent(&ctx) );
99
cuSafeCall( cuvidCtxLockCreate(&lock_, ctx) );
101
frameQueue_.reset(new FrameQueue);
102
videoDecoder_.reset(new VideoDecoder(videoSource_->format(), lock_));
103
videoParser_.reset(new VideoParser(videoDecoder_, frameQueue_));
105
videoSource_->setVideoParser(videoParser_);
106
videoSource_->start();
109
VideoReaderImpl::~VideoReaderImpl()
111
frameQueue_->endDecode();
112
videoSource_->stop();
115
class VideoCtxAutoLock
118
VideoCtxAutoLock(CUvideoctxlock lock) : m_lock(lock) { cuSafeCall( cuvidCtxLock(m_lock, 0) ); }
119
~VideoCtxAutoLock() { cuvidCtxUnlock(m_lock, 0); }
122
CUvideoctxlock m_lock;
125
bool VideoReaderImpl::nextFrame(OutputArray frame)
127
if (videoSource_->hasError() || videoParser_->hasError())
128
CV_Error(Error::StsUnsupportedFormat, "Unsupported video source");
130
if (!videoSource_->isStarted() || frameQueue_->isEndOfDecode())
135
CUVIDPARSERDISPINFO displayInfo;
139
if (frameQueue_->dequeue(displayInfo))
142
if (videoSource_->hasError() || videoParser_->hasError())
143
CV_Error(Error::StsUnsupportedFormat, "Unsupported video source");
145
if (frameQueue_->isEndOfDecode())
152
bool isProgressive = displayInfo.progressive_frame != 0;
153
const int num_fields = isProgressive ? 1 : 2 + displayInfo.repeat_first_field;
155
for (int active_field = 0; active_field < num_fields; ++active_field)
157
CUVIDPROCPARAMS videoProcParams;
158
std::memset(&videoProcParams, 0, sizeof(CUVIDPROCPARAMS));
160
videoProcParams.progressive_frame = displayInfo.progressive_frame;
161
videoProcParams.second_field = active_field;
162
videoProcParams.top_field_first = displayInfo.top_field_first;
163
videoProcParams.unpaired_field = (num_fields == 1);
165
frames_.push_back(std::make_pair(displayInfo, videoProcParams));
172
std::pair<CUVIDPARSERDISPINFO, CUVIDPROCPARAMS> frameInfo = frames_.front();
176
VideoCtxAutoLock autoLock(lock_);
178
// map decoded video frame to CUDA surface
179
GpuMat decodedFrame = videoDecoder_->mapFrame(frameInfo.first.picture_index, frameInfo.second);
181
// perform post processing on the CUDA surface (performs colors space conversion and post processing)
182
// comment this out if we inclue the line of code seen above
183
videoDecPostProcessFrame(decodedFrame, frame, videoDecoder_->targetWidth(), videoDecoder_->targetHeight());
186
// unmapFrame() synchronizes with the VideoDecode API (ensures the frame has finished decoding)
187
videoDecoder_->unmapFrame(decodedFrame);
190
// release the frame, so it can be re-used in decoder
192
frameQueue_->releaseFrame(frameInfo.first);
198
Ptr<VideoReader> cv::cudacodec::createVideoReader(const String& filename)
200
CV_Assert( !filename.empty() );
202
Ptr<VideoSource> videoSource;
206
videoSource.reset(new CuvidVideoSource(filename));
210
Ptr<RawVideoSource> source(new FFmpegVideoSource(filename));
211
videoSource.reset(new RawVideoSourceWrapper(source));
214
return makePtr<VideoReaderImpl>(videoSource);
217
Ptr<VideoReader> cv::cudacodec::createVideoReader(const Ptr<RawVideoSource>& source)
219
Ptr<VideoSource> videoSource(new RawVideoSourceWrapper(source));
220
return makePtr<VideoReaderImpl>(videoSource);
223
#endif // HAVE_NVCUVID