Skip to content

Commit

Permalink
Merge pull request opencv#3209 from cudawarped:cudacodec_change_n_dec…
Browse files Browse the repository at this point in the history
…ode_surfaces
  • Loading branch information
alalek committed Mar 24, 2022
2 parents 172a044 + c3ac120 commit ed38f75
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 17 deletions.
12 changes: 10 additions & 2 deletions modules/cudacodec/include/opencv2/cudacodec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,24 @@ The `params` parameter allows to specify extra parameters encoded as pairs `(par
See cv::VideoCaptureProperties
e.g. when streaming from an RTSP source CAP_PROP_OPEN_TIMEOUT_MSEC may need to be set.
@param rawMode Allow the raw encoded data which has been read up until the last call to grab() to be retrieved by calling retrieve(rawData,RAW_DATA_IDX).
@param minNumDecodeSurfaces Minimum number of internal decode surfaces used by the hardware decoder. NVDEC will automatically determine the minimum number of
surfaces it requires for correct functionality and optimal video memory usage but not necessarily for best performance, which depends on the design of the
overall application. The optimal number of decode surfaces (in terms of performance and memory utilization) should be decided by experimentation for each application,
but it cannot go below the number determined by NVDEC.
FFMPEG is used to read videos. User can implement own demultiplexing with cudacodec::RawVideoSource
*/
CV_EXPORTS_W Ptr<VideoReader> createVideoReader(const String& filename, const std::vector<int>& params = {}, const bool rawMode = false);
CV_EXPORTS_W Ptr<VideoReader> createVideoReader(const String& filename, const std::vector<int>& params = {}, const bool rawMode = false, const int minNumDecodeSurfaces = 0);

/** @overload
@param source RAW video source implemented by user.
@param rawMode Allow the raw encoded data which has been read up until the last call to grab() to be retrieved by calling retrieve(rawData,RAW_DATA_IDX).
@param minNumDecodeSurfaces Minimum number of internal decode surfaces used by the hardware decoder. NVDEC will automatically determine the minimum number of
surfaces it requires for correct functionality and optimal video memory usage but not necessarily for best performance, which depends on the design of the
overall application. The optimal number of decode surfaces (in terms of performance and memory utilization) should be decided by experimentation for each application,
but it cannot go below the number determined by NVDEC.
*/
CV_EXPORTS_W Ptr<VideoReader> createVideoReader(const Ptr<RawVideoSource>& source, const bool rawMode = false);
CV_EXPORTS_W Ptr<VideoReader> createVideoReader(const Ptr<RawVideoSource>& source, const bool rawMode = false, const int minNumDecodeSurfaces = 0);

//! @}

Expand Down
5 changes: 3 additions & 2 deletions modules/cudacodec/src/video_decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ namespace cv { namespace cudacodec { namespace detail {
class VideoDecoder
{
public:
VideoDecoder(const Codec& codec, CUcontext ctx, CUvideoctxlock lock) : ctx_(ctx), lock_(lock), decoder_(0)
VideoDecoder(const Codec& codec, const int minNumDecodeSurfaces, CUcontext ctx, CUvideoctxlock lock) : ctx_(ctx), lock_(lock), decoder_(0)
{
videoFormat_.codec = codec;
videoFormat_.ulNumDecodeSurfaces = minNumDecodeSurfaces;
}

~VideoDecoder()
Expand All @@ -64,7 +65,7 @@ class VideoDecoder

// Get the code-type currently used.
cudaVideoCodec codec() const { return static_cast<cudaVideoCodec>(videoFormat_.codec); }
unsigned long maxDecodeSurfaces() const { return videoFormat_.ulNumDecodeSurfaces; }
int nDecodeSurfaces() const { return videoFormat_.ulNumDecodeSurfaces; }

unsigned long frameWidth() const { return videoFormat_.ulWidth; }
unsigned long frameHeight() const { return videoFormat_.ulHeight; }
Expand Down
6 changes: 3 additions & 3 deletions modules/cudacodec/src/video_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int CUDAAPI cv::cudacodec::detail::VideoParser::HandleVideoSequence(void* userDa
format->coded_height != thiz->videoDecoder_->frameHeight() ||
format->chroma_format != thiz->videoDecoder_->chromaFormat()||
format->bit_depth_luma_minus8 != thiz->videoDecoder_->nBitDepthMinus8() ||
format->min_num_decode_surfaces != thiz->videoDecoder_->maxDecodeSurfaces())
format->min_num_decode_surfaces != thiz->videoDecoder_->nDecodeSurfaces())
{
FormatInfo newFormat;
newFormat.codec = static_cast<Codec>(format->codec);
Expand All @@ -122,7 +122,7 @@ int CUDAAPI cv::cudacodec::detail::VideoParser::HandleVideoSequence(void* userDa
newFormat.height = format->coded_height;
newFormat.displayArea = Rect(Point(format->display_area.left, format->display_area.top), Point(format->display_area.right, format->display_area.bottom));
newFormat.fps = format->frame_rate.numerator / static_cast<float>(format->frame_rate.denominator);
newFormat.ulNumDecodeSurfaces = format->min_num_decode_surfaces;
newFormat.ulNumDecodeSurfaces = max(thiz->videoDecoder_->nDecodeSurfaces(), static_cast<int>(format->min_num_decode_surfaces));
if (format->progressive_sequence)
newFormat.deinterlaceMode = Weave;
else
Expand Down Expand Up @@ -154,7 +154,7 @@ int CUDAAPI cv::cudacodec::detail::VideoParser::HandleVideoSequence(void* userDa
}
}

return thiz->videoDecoder_->maxDecodeSurfaces();
return thiz->videoDecoder_->nDecodeSurfaces();
}

int CUDAAPI cv::cudacodec::detail::VideoParser::HandlePictureDecode(void* userData, CUVIDPICPARAMS* picParams)
Expand Down
18 changes: 9 additions & 9 deletions modules/cudacodec/src/video_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ using namespace cv::cudacodec;

#ifndef HAVE_NVCUVID

Ptr<VideoReader> cv::cudacodec::createVideoReader(const String&, const std::vector<int>&, const bool) { throw_no_cuda(); return Ptr<VideoReader>(); }
Ptr<VideoReader> cv::cudacodec::createVideoReader(const Ptr<RawVideoSource>&, const bool) { throw_no_cuda(); return Ptr<VideoReader>(); }
Ptr<VideoReader> cv::cudacodec::createVideoReader(const String&, const std::vector<int>&, const bool, const int) { throw_no_cuda(); return Ptr<VideoReader>(); }
Ptr<VideoReader> cv::cudacodec::createVideoReader(const Ptr<RawVideoSource>&, const bool, const int) { throw_no_cuda(); return Ptr<VideoReader>(); }

#else // HAVE_NVCUVID

Expand All @@ -62,7 +62,7 @@ namespace
class VideoReaderImpl : public VideoReader
{
public:
explicit VideoReaderImpl(const Ptr<VideoSource>& source);
explicit VideoReaderImpl(const Ptr<VideoSource>& source, const int minNumDecodeSurfaces);
~VideoReaderImpl();

bool nextFrame(GpuMat& frame, Stream& stream) CV_OVERRIDE;
Expand Down Expand Up @@ -103,7 +103,7 @@ namespace
return videoSource_->format();
}

VideoReaderImpl::VideoReaderImpl(const Ptr<VideoSource>& source) :
VideoReaderImpl::VideoReaderImpl(const Ptr<VideoSource>& source, const int minNumDecodeSurfaces) :
videoSource_(source),
lock_(0)
{
Expand All @@ -115,7 +115,7 @@ namespace
cuSafeCall( cuCtxGetCurrent(&ctx) );
cuSafeCall( cuvidCtxLockCreate(&lock_, ctx) );
frameQueue_.reset(new FrameQueue());
videoDecoder_.reset(new VideoDecoder(videoSource_->format().codec, ctx, lock_));
videoDecoder_.reset(new VideoDecoder(videoSource_->format().codec, minNumDecodeSurfaces, ctx, lock_));
videoParser_.reset(new VideoParser(videoDecoder_, frameQueue_));
videoSource_->setVideoParser(videoParser_);
videoSource_->start();
Expand Down Expand Up @@ -291,7 +291,7 @@ namespace
}
}

Ptr<VideoReader> cv::cudacodec::createVideoReader(const String& filename, const std::vector<int>& params, const bool rawMode)
Ptr<VideoReader> cv::cudacodec::createVideoReader(const String& filename, const std::vector<int>& params, const bool rawMode, const int minNumDecodeSurfaces)
{
CV_Assert(!filename.empty());

Expand All @@ -309,13 +309,13 @@ Ptr<VideoReader> cv::cudacodec::createVideoReader(const String& filename, const
videoSource.reset(new CuvidVideoSource(filename));
}

return makePtr<VideoReaderImpl>(videoSource);
return makePtr<VideoReaderImpl>(videoSource, minNumDecodeSurfaces);
}

Ptr<VideoReader> cv::cudacodec::createVideoReader(const Ptr<RawVideoSource>& source, const bool rawMode)
Ptr<VideoReader> cv::cudacodec::createVideoReader(const Ptr<RawVideoSource>& source, const bool rawMode, const int minNumDecodeSurfaces)
{
Ptr<VideoSource> videoSource(new RawVideoSourceWrapper(source, rawMode));
return makePtr<VideoReaderImpl>(videoSource);
return makePtr<VideoReaderImpl>(videoSource, minNumDecodeSurfaces);
}

#endif // HAVE_NVCUVID
51 changes: 50 additions & 1 deletion modules/cudacodec/test/test_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ PARAM_TEST_CASE(CheckKeyFrame, cv::cuda::DeviceInfo, std::string)
{
};

PARAM_TEST_CASE(CheckDecodeSurfaces, cv::cuda::DeviceInfo, std::string)
{
};

struct CheckParams : testing::TestWithParam<cv::cuda::DeviceInfo>
{
cv::cuda::DeviceInfo devInfo;
Expand Down Expand Up @@ -281,14 +285,55 @@ CUDA_TEST_P(CheckParams, Reader)
cv::Ptr<cv::cudacodec::VideoReader> reader = cv::cudacodec::createVideoReader(inputFile, {
cv::VideoCaptureProperties::CAP_PROP_FORMAT, capPropFormats.at(i) });
}
catch (cv::Exception ex) {
catch (cv::Exception &ex) {
if (ex.code == Error::StsUnsupportedFormat)
exceptionThrown = true;
}
ASSERT_EQ(exceptionThrown, exceptionsThrown.at(i));
}
}
}

CUDA_TEST_P(CheckDecodeSurfaces, Reader)
{
cv::cuda::setDevice(GET_PARAM(0).deviceID());
const std::string inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "../" + GET_PARAM(1);
int ulNumDecodeSurfaces = 0;
{
cv::Ptr<cv::cudacodec::VideoReader> reader = cv::cudacodec::createVideoReader(inputFile);
cv::cudacodec::FormatInfo fmt = reader->format();
if (!fmt.valid) {
reader->grab();
fmt = reader->format();
ASSERT_TRUE(fmt.valid);
}
ulNumDecodeSurfaces = fmt.ulNumDecodeSurfaces;
}

{
cv::Ptr<cv::cudacodec::VideoReader> reader = cv::cudacodec::createVideoReader(inputFile, {}, false, ulNumDecodeSurfaces - 1);
cv::cudacodec::FormatInfo fmt = reader->format();
if (!fmt.valid) {
reader->grab();
fmt = reader->format();
ASSERT_TRUE(fmt.valid);
}
ASSERT_TRUE(fmt.ulNumDecodeSurfaces == ulNumDecodeSurfaces);
for (int i = 0; i < 100; i++) ASSERT_TRUE(reader->grab());
}

{
cv::Ptr<cv::cudacodec::VideoReader> reader = cv::cudacodec::createVideoReader(inputFile, {}, false, ulNumDecodeSurfaces + 1);
cv::cudacodec::FormatInfo fmt = reader->format();
if (!fmt.valid) {
reader->grab();
fmt = reader->format();
ASSERT_TRUE(fmt.valid);
}
ASSERT_TRUE(fmt.ulNumDecodeSurfaces == ulNumDecodeSurfaces + 1);
for (int i = 0; i < 100; i++) ASSERT_TRUE(reader->grab());
}
}
#endif // HAVE_NVCUVID

#if defined(_WIN32) && defined(HAVE_NVCUVENC)
Expand Down Expand Up @@ -372,5 +417,9 @@ INSTANTIATE_TEST_CASE_P(CUDA_Codec, CheckKeyFrame, testing::Combine(

INSTANTIATE_TEST_CASE_P(CUDA_Codec, CheckParams, ALL_DEVICES);

INSTANTIATE_TEST_CASE_P(CUDA_Codec, CheckDecodeSurfaces, testing::Combine(
ALL_DEVICES,
testing::Values("highgui/video/big_buck_bunny.mp4")));

#endif // HAVE_NVCUVID || HAVE_NVCUVENC
}} // namespace

0 comments on commit ed38f75

Please sign in to comment.