Merge pull request #22615 from cudawarped:nvcuvenc

Update CMake rules to include Nvidia Video Codec SDK encoding libs
This commit is contained in:
cudawarped 2022-10-28 11:03:51 +03:00 committed by GitHub
parent 778faddbd8
commit be670e168a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 102 deletions

View File

@ -266,6 +266,9 @@ OCV_OPTION(WITH_CUDNN "Include NVIDIA CUDA Deep Neural Network (cuDNN) library s
OCV_OPTION(WITH_NVCUVID "Include NVidia Video Decoding library support" OFF # disabled, details: https://github.com/opencv/opencv/issues/14850 OCV_OPTION(WITH_NVCUVID "Include NVidia Video Decoding library support" OFF # disabled, details: https://github.com/opencv/opencv/issues/14850
VISIBLE_IF WITH_CUDA VISIBLE_IF WITH_CUDA
VERIFY HAVE_NVCUVID) VERIFY HAVE_NVCUVID)
OCV_OPTION(WITH_NVCUVENC "Include NVidia Video Encoding library support" OFF
VISIBLE_IF WITH_CUDA
VERIFY HAVE_NVCUVENC)
OCV_OPTION(WITH_EIGEN "Include Eigen2/Eigen3 support" (NOT CV_DISABLE_OPTIMIZATION AND NOT CMAKE_CROSSCOMPILING) OCV_OPTION(WITH_EIGEN "Include Eigen2/Eigen3 support" (NOT CV_DISABLE_OPTIMIZATION AND NOT CMAKE_CROSSCOMPILING)
VISIBLE_IF NOT WINRT VISIBLE_IF NOT WINRT
VERIFY HAVE_EIGEN) VERIFY HAVE_EIGEN)
@ -1661,6 +1664,7 @@ if(WITH_CUDA OR HAVE_CUDA)
IF HAVE_CUFFT THEN "CUFFT" IF HAVE_CUFFT THEN "CUFFT"
IF HAVE_CUBLAS THEN "CUBLAS" IF HAVE_CUBLAS THEN "CUBLAS"
IF HAVE_NVCUVID THEN "NVCUVID" IF HAVE_NVCUVID THEN "NVCUVID"
IF HAVE_NVCUVENC THEN "NVCUVENC"
IF CUDA_FAST_MATH THEN "FAST_MATH" IF CUDA_FAST_MATH THEN "FAST_MATH"
ELSE "no extra features") ELSE "no extra features")
status("") status("")

View File

@ -287,7 +287,7 @@
# Only available for CUDA version 5.5+. # Only available for CUDA version 5.5+.
# CUDA_npps_LIBRARY -- NVIDIA Performance Primitives lib (signal processing). # CUDA_npps_LIBRARY -- NVIDIA Performance Primitives lib (signal processing).
# Only available for CUDA version 5.5+. # Only available for CUDA version 5.5+.
# CUDA_nvcuvenc_LIBRARY -- CUDA Video Encoder library. # CUDA_nvencodeapi_LIBRARY -- CUDA Video Encoder library.
# Only available for CUDA version 3.2+. # Only available for CUDA version 3.2+.
# Windows only. # Windows only.
# CUDA_nvcuvid_LIBRARY -- CUDA Video Decoder library. # CUDA_nvcuvid_LIBRARY -- CUDA Video Decoder library.
@ -530,7 +530,7 @@ macro(cuda_unset_include_and_libraries)
unset(CUDA_nppc_LIBRARY CACHE) unset(CUDA_nppc_LIBRARY CACHE)
unset(CUDA_nppi_LIBRARY CACHE) unset(CUDA_nppi_LIBRARY CACHE)
unset(CUDA_npps_LIBRARY CACHE) unset(CUDA_npps_LIBRARY CACHE)
unset(CUDA_nvcuvenc_LIBRARY CACHE) unset(CUDA_nvencodeapi_LIBRARY CACHE)
unset(CUDA_nvcuvid_LIBRARY CACHE) unset(CUDA_nvcuvid_LIBRARY CACHE)
endmacro() endmacro()
@ -790,7 +790,7 @@ if(NOT CUDA_VERSION VERSION_LESS "3.2")
find_cuda_helper_libs(cusparse) find_cuda_helper_libs(cusparse)
find_cuda_helper_libs(curand) find_cuda_helper_libs(curand)
if (WIN32) if (WIN32)
find_cuda_helper_libs(nvcuvenc) find_cuda_helper_libs(nvencodeapi)
find_cuda_helper_libs(nvcuvid) find_cuda_helper_libs(nvcuvid)
endif() endif()
endif() endif()

View File

@ -28,6 +28,7 @@ else()
endif() endif()
if(CUDA_FOUND) if(CUDA_FOUND)
unset(CUDA_nvcuvenc_LIBRARY CACHE)
set(HAVE_CUDA 1) set(HAVE_CUDA 1)
if(NOT CUDA_VERSION VERSION_LESS 11.0) if(NOT CUDA_VERSION VERSION_LESS 11.0)
# CUDA 11.0 removes nppicom # CUDA 11.0 removes nppicom
@ -53,7 +54,7 @@ if(CUDA_FOUND)
endif() endif()
endif() endif()
if(WITH_NVCUVID) if(WITH_NVCUVID OR WITH_NVCUVENC)
macro(ocv_cuda_SEARCH_NVCUVID_HEADER _filename _result) macro(ocv_cuda_SEARCH_NVCUVID_HEADER _filename _result)
# place header file under CUDA_TOOLKIT_TARGET_DIR or CUDA_TOOLKIT_ROOT_DIR # place header file under CUDA_TOOLKIT_TARGET_DIR or CUDA_TOOLKIT_ROOT_DIR
find_path(_header_result find_path(_header_result
@ -71,18 +72,25 @@ if(CUDA_FOUND)
endif() endif()
unset(_header_result CACHE) unset(_header_result CACHE)
endmacro() endmacro()
ocv_cuda_SEARCH_NVCUVID_HEADER("nvcuvid.h" HAVE_NVCUVID_HEADER) if(WITH_NVCUVID)
ocv_cuda_SEARCH_NVCUVID_HEADER("dynlink_nvcuvid.h" HAVE_DYNLINK_NVCUVID_HEADER) ocv_cuda_SEARCH_NVCUVID_HEADER("nvcuvid.h" HAVE_NVCUVID_HEADER)
find_cuda_helper_libs(nvcuvid) ocv_cuda_SEARCH_NVCUVID_HEADER("dynlink_nvcuvid.h" HAVE_DYNLINK_NVCUVID_HEADER)
if(WIN32) find_cuda_helper_libs(nvcuvid)
find_cuda_helper_libs(nvcuvenc) if(CUDA_nvcuvid_LIBRARY AND (${HAVE_NVCUVID_HEADER} OR ${HAVE_DYNLINK_NVCUVID_HEADER}))
# make sure to have both header and library before enabling
set(HAVE_NVCUVID 1)
endif()
endif() endif()
if(CUDA_nvcuvid_LIBRARY AND (${HAVE_NVCUVID_HEADER} OR ${HAVE_DYNLINK_NVCUVID_HEADER})) if(WITH_NVCUVENC)
# make sure to have both header and library before enabling ocv_cuda_SEARCH_NVCUVID_HEADER("nvEncodeAPI.h" HAVE_NVCUVENC_HEADER)
set(HAVE_NVCUVID 1) if(WIN32)
endif() find_cuda_helper_libs(nvencodeapi)
if(CUDA_nvcuvenc_LIBRARY) else()
set(HAVE_NVCUVENC 1) find_cuda_helper_libs(nvidia-encode)
endif()
if((CUDA_nvencodeapi_LIBRARY OR CUDA_nvidia-encode_LIBRARY) AND ${HAVE_NVCUVENC_HEADER})
set(HAVE_NVCUVENC 1)
endif()
endif() endif()
endif() endif()

View File

@ -5,7 +5,7 @@ set(OpenCV_CUDA_VERSION "@CUDA_VERSION_STRING@")
set(OpenCV_USE_CUBLAS "@HAVE_CUBLAS@") set(OpenCV_USE_CUBLAS "@HAVE_CUBLAS@")
set(OpenCV_USE_CUFFT "@HAVE_CUFFT@") set(OpenCV_USE_CUFFT "@HAVE_CUFFT@")
set(OpenCV_USE_NVCUVID "@HAVE_NVCUVID@") set(OpenCV_USE_NVCUVID "@HAVE_NVCUVID@")
set(OpenCV_USE_NVCUVENC "@HAVE_NVCUVENC@")
set(OpenCV_CUDNN_VERSION "@CUDNN_VERSION@") set(OpenCV_CUDNN_VERSION "@CUDNN_VERSION@")
set(OpenCV_USE_CUDNN "@HAVE_CUDNN@") set(OpenCV_USE_CUDNN "@HAVE_CUDNN@")
@ -36,14 +36,6 @@ if(OpenCV_USE_CUFFT)
list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_CUFFT_LIBRARIES}) list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_CUFFT_LIBRARIES})
endif() endif()
if(OpenCV_USE_NVCUVID)
list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nvcuvid_LIBRARIES})
endif()
if(WIN32)
list(APPEND OpenCV_CUDA_LIBS_ABSPATH ${CUDA_nvcuvenc_LIBRARIES})
endif()
set(OpenCV_CUDA_LIBS_RELPATH "") set(OpenCV_CUDA_LIBS_RELPATH "")
foreach(l ${OpenCV_CUDA_LIBS_ABSPATH}) foreach(l ${OpenCV_CUDA_LIBS_ABSPATH})
get_filename_component(_tmp ${l} PATH) get_filename_component(_tmp ${l} PATH)

View File

@ -22,65 +22,41 @@ int main(int argc, const char* argv[])
const std::string fname(argv[1]); const std::string fname(argv[1]);
cv::namedWindow("CPU", cv::WINDOW_NORMAL); cv::namedWindow("CPU", cv::WINDOW_NORMAL);
#if defined(HAVE_OPENGL)
cv::namedWindow("GPU", cv::WINDOW_OPENGL); cv::namedWindow("GPU", cv::WINDOW_OPENGL);
cv::cuda::setGlDevice(); cv::cuda::setGlDevice();
#else
cv::namedWindow("GPU", cv::WINDOW_NORMAL);
#endif
cv::TickMeter tm;
cv::Mat frame; cv::Mat frame;
cv::VideoCapture reader(fname); cv::VideoCapture reader(fname);
for (;;)
{
if (!reader.read(frame))
break;
cv::imshow("CPU", frame);
if (cv::waitKey(3) > 0)
break;
}
cv::cuda::GpuMat d_frame; cv::cuda::GpuMat d_frame;
cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname); cv::Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(fname);
cv::TickMeter tm;
std::vector<double> cpu_times;
std::vector<double> gpu_times;
int gpu_frame_count=0, cpu_frame_count=0;
for (;;) for (;;)
{ {
tm.reset(); tm.start();
if (!reader.read(frame))
break;
tm.stop();
cpu_times.push_back(tm.getTimeMilli());
cpu_frame_count++;
cv::imshow("CPU", frame);
if (cv::waitKey(3) > 0)
break;
}
for (;;)
{
tm.reset(); tm.start();
if (!d_reader->nextFrame(d_frame)) if (!d_reader->nextFrame(d_frame))
break; break;
tm.stop(); #if defined(HAVE_OPENGL)
gpu_times.push_back(tm.getTimeMilli()); cv::imshow("GPU", cv::ogl::Texture2D(d_frame));
gpu_frame_count++; #else
d_frame.download(frame);
cv::imshow("GPU", d_frame); cv::imshow("GPU", frame);
#endif
if (cv::waitKey(3) > 0) if (cv::waitKey(3) > 0)
break; break;
} }
if (!cpu_times.empty() && !gpu_times.empty())
{
std::cout << std::endl << "Results:" << std::endl;
std::sort(cpu_times.begin(), cpu_times.end());
std::sort(gpu_times.begin(), gpu_times.end());
double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
std::cout << "CPU : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << " Frames " << cpu_frame_count << std::endl;
std::cout << "GPU : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << " Frames " << gpu_frame_count << std::endl;
}
return 0; return 0;
} }

View File

@ -2,7 +2,7 @@
#include "opencv2/opencv_modules.hpp" #include "opencv2/opencv_modules.hpp"
#if defined(HAVE_OPENCV_CUDACODEC) && defined(_WIN32) #if defined(HAVE_OPENCV_CUDACODEC)
#include <vector> #include <vector>
#include <numeric> #include <numeric>
@ -20,7 +20,7 @@ int main(int argc, const char* argv[])
return -1; return -1;
} }
const double FPS = 25.0; constexpr double fps = 25.0;
cv::VideoCapture reader(argv[1]); cv::VideoCapture reader(argv[1]);
@ -37,17 +37,12 @@ int main(int argc, const char* argv[])
cv::Mat frame; cv::Mat frame;
cv::cuda::GpuMat d_frame; cv::cuda::GpuMat d_frame;
cv::cuda::Stream stream;
std::vector<double> cpu_times;
std::vector<double> gpu_times;
TickMeter tm;
for (int i = 1;; ++i) for (int i = 1;; ++i)
{ {
std::cout << "Read " << i << " frame" << std::endl; std::cout << "Read " << i << " frame" << std::endl;
reader >> frame; reader >> frame;
if (frame.empty()) if (frame.empty())
{ {
std::cout << "Stop" << std::endl; std::cout << "Stop" << std::endl;
@ -57,47 +52,27 @@ int main(int argc, const char* argv[])
if (!writer.isOpened()) if (!writer.isOpened())
{ {
std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl; std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
std::cout << "Open CPU Writer" << std::endl; std::cout << "Open CPU Writer" << std::endl;
const String outputFilename = "output_cpu.avi";
if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size())) if (!writer.open(outputFilename, cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), fps, frame.size()))
return -1; return -1;
std::cout << "Writing to " << outputFilename << std::endl;
} }
if (d_writer.empty()) if (d_writer.empty())
{ {
std::cout << "Open CUDA Writer" << std::endl; std::cout << "Open CUDA Writer" << std::endl;
const cv::String outputFilename = "output_gpu.h264";
const cv::String outputFilename = "output_gpu.avi"; d_writer = cv::cudacodec::createVideoWriter(outputFilename, frame.size(), cv::cudacodec::Codec::H264, fps, cv::cudacodec::ColorFormat::BGR, 0, stream);
d_writer = cv::cudacodec::createVideoWriter(outputFilename, frame.size(), FPS); std::cout << "Writing to " << outputFilename << std::endl;
} }
d_frame.upload(frame); d_frame.upload(frame, stream);
std::cout << "Write " << i << " frame" << std::endl; std::cout << "Write " << i << " frame" << std::endl;
tm.reset(); tm.start();
writer.write(frame); writer.write(frame);
tm.stop();
cpu_times.push_back(tm.getTimeMilli());
tm.reset(); tm.start();
d_writer->write(d_frame); d_writer->write(d_frame);
tm.stop();
gpu_times.push_back(tm.getTimeMilli());
} }
std::cout << std::endl << "Results:" << std::endl;
std::sort(cpu_times.begin(), cpu_times.end());
std::sort(gpu_times.begin(), gpu_times.end());
double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
std::cout << "CPU [XVID] : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
std::cout << "GPU [H264] : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
return 0; return 0;
} }