diff --git a/modules/cudacodec/include/opencv2/cudacodec.hpp b/modules/cudacodec/include/opencv2/cudacodec.hpp index 307fa79ed..35f5b28cc 100644 --- a/modules/cudacodec/include/opencv2/cudacodec.hpp +++ b/modules/cudacodec/include/opencv2/cudacodec.hpp @@ -186,7 +186,7 @@ struct CV_EXPORTS_W_SIMPLE EncoderParams public: CV_WRAP EncoderParams() : nvPreset(ENC_PRESET_P3), tuningInfo(ENC_TUNING_INFO_HIGH_QUALITY), encodingProfile(ENC_CODEC_PROFILE_AUTOSELECT), rateControlMode(ENC_PARAMS_RC_VBR), multiPassEncoding(ENC_MULTI_PASS_DISABLED), constQp({ 0,0,0 }), averageBitRate(0), maxBitRate(0), - targetQuality(30), gopLength(250), idrPeriod(250) {}; + targetQuality(30), gopLength(250), idrPeriod(250), videoFullRangeFlag(false){}; CV_PROP_RW EncodePreset nvPreset; CV_PROP_RW EncodeTuningInfo tuningInfo; CV_PROP_RW EncodeProfile encodingProfile; @@ -198,6 +198,7 @@ public: CV_PROP_RW uint8_t targetQuality; //!< value 0 - 51 where video quality decreases as targetQuality increases, used with \ref ENC_PARAMS_RC_VBR. CV_PROP_RW int gopLength; //!< the number of pictures in one GOP, ensuring \ref idrPeriod >= \ref gopLength. CV_PROP_RW int idrPeriod; //!< IDR interval, ensuring \ref idrPeriod >= \ref gopLength. + CV_PROP_RW bool videoFullRangeFlag;//!< Indicates if the black level, luma and chroma of the source are represented using the full or limited range (AKA TV or "analogue" range) of values as defined in Annex E of the ITU-T Specification. }; CV_EXPORTS bool operator==(const EncoderParams& lhs, const EncoderParams& rhs); diff --git a/modules/cudacodec/src/NvEncoder.cpp b/modules/cudacodec/src/NvEncoder.cpp index b94bea200..3beb09514 100644 --- a/modules/cudacodec/src/NvEncoder.cpp +++ b/modules/cudacodec/src/NvEncoder.cpp @@ -101,7 +101,7 @@ void NvEncoder::CreateDefaultEncoderParams(NV_ENC_INITIALIZE_PARAMS* pIntializeP #endif pIntializeParams->tuningInfo = tuningInfo; pIntializeParams->encodeConfig->rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP; -#if (NVENCAPI_MAJOR_VERSION >= 12 && NVENCAPI_MINOR_VERSION >= 2) +#if ((NVENCAPI_MAJOR_VERSION == 12 && NVENCAPI_MINOR_VERSION >= 2) || NVENCAPI_MAJOR_VERSION > 12) NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, 0, { NV_ENC_CONFIG_VER } }; #else NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } }; @@ -119,7 +119,7 @@ void NvEncoder::CreateDefaultEncoderParams(NV_ENC_INITIALIZE_PARAMS* pIntializeP } else if (pIntializeParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) { -#if (NVENCAPI_MAJOR_VERSION >= 12 && NVENCAPI_MINOR_VERSION >= 2) +#if ((NVENCAPI_MAJOR_VERSION == 12 && NVENCAPI_MINOR_VERSION >= 2) || NVENCAPI_MAJOR_VERSION > 12) pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.inputBitDepth = pIntializeParams->encodeConfig->encodeCodecConfig.hevcConfig.outputBitDepth = (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8; #else @@ -179,7 +179,7 @@ void NvEncoder::CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams) if (pEncoderParams->encodeGUID == NV_ENC_CODEC_HEVC_GUID) { bool yuv10BitFormat = (m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV420_10BIT || m_eBufferFormat == NV_ENC_BUFFER_FORMAT_YUV444_10BIT) ? true : false; -#if (NVENCAPI_MAJOR_VERSION >= 12 && NVENCAPI_MINOR_VERSION >= 2) +#if ((NVENCAPI_MAJOR_VERSION == 12 && NVENCAPI_MINOR_VERSION >= 2) || NVENCAPI_MAJOR_VERSION > 12) if (yuv10BitFormat && pEncoderParams->encodeConfig->encodeCodecConfig.hevcConfig.inputBitDepth != NV_ENC_BIT_DEPTH_10) #else if (yuv10BitFormat && pEncoderParams->encodeConfig->encodeCodecConfig.hevcConfig.pixelBitDepthMinus8 != 2) @@ -205,7 +205,7 @@ void NvEncoder::CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams) } else { -#if (NVENCAPI_MAJOR_VERSION >= 12 && NVENCAPI_MINOR_VERSION >= 2) +#if ((NVENCAPI_MAJOR_VERSION == 12 && NVENCAPI_MINOR_VERSION >= 2) || NVENCAPI_MAJOR_VERSION > 12) NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, 0, { NV_ENC_CONFIG_VER } }; #else NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } }; diff --git a/modules/cudacodec/src/video_writer.cpp b/modules/cudacodec/src/video_writer.cpp index 5bb1a533f..9b36febda 100644 --- a/modules/cudacodec/src/video_writer.cpp +++ b/modules/cudacodec/src/video_writer.cpp @@ -53,10 +53,6 @@ Ptr createVideoWriter(const String&, const Size, const C #else // !defined HAVE_NVCUVENC -#if defined(WIN32) // remove when FFmpeg wrapper includes PR25874 -#define WIN32_WAIT_FOR_FFMPEG_WRAPPER_UPDATE -#endif - NV_ENC_BUFFER_FORMAT EncBufferFormat(const ColorFormat colorFormat); int NChannels(const ColorFormat colorFormat); GUID CodecGuid(const Codec codec); @@ -86,7 +82,7 @@ private: FFmpegVideoWriter::FFmpegVideoWriter(const String& fileName, const Codec codec, const int fps, const Size sz, const int idrPeriod) { if (!videoio_registry::hasBackend(CAP_FFMPEG)) CV_Error(Error::StsNotImplemented, "FFmpeg backend not found"); - const int fourcc = codec == Codec::H264 ? cv::VideoWriter::fourcc('a', 'v', 'c', '1') : cv::VideoWriter::fourcc('h', 'e', 'v', '1'); + const int fourcc = codec == Codec::H264 ? cv::VideoWriter::fourcc('a', 'v', 'c', '1') : cv::VideoWriter::fourcc('h', 'v', 'c', '1'); writer.open(fileName, fourcc, fps, sz, { VideoWriterProperties::VIDEOWRITER_PROP_RAW_VIDEO, 1, VideoWriterProperties::VIDEOWRITER_PROP_KEY_INTERVAL, idrPeriod }); if (!writer.isOpened()) CV_Error(Error::StsUnsupportedFormat, "Unsupported video sink"); @@ -107,9 +103,7 @@ void FFmpegVideoWriter::onEncoded(const std::vector>& vPack Mat wrappedPacket(1, packet.size(), CV_8UC1, (void*)packet.data()); const double ptsDouble = static_cast(pts.at(i)); CV_Assert(static_cast(ptsDouble) == pts.at(i)); -#if !defined(WIN32_WAIT_FOR_FFMPEG_WRAPPER_UPDATE) CV_Assert(writer.set(VIDEOWRITER_PROP_PTS, ptsDouble)); -#endif writer.write(wrappedPacket); } } @@ -321,6 +315,13 @@ GUID EncodingPresetGuid(const EncodePreset nvPreset) { CV_Error(Error::StsUnsupportedFormat, msg); } +std::string GetVideoCodecString(const GUID codec) { + if (codec == NV_ENC_CODEC_H264_GUID) return "AVC/H.264"; + else if (codec == NV_ENC_CODEC_HEVC_GUID) return "H.265/HEVC"; + else if (codec == NV_ENC_CODEC_AV1_GUID) return "AV1"; + else return "Unknown"; +} + void VideoWriterImpl::InitializeEncoder(const GUID codec, const double fps) { NV_ENC_INITIALIZE_PARAMS initializeParams = {}; @@ -337,15 +338,27 @@ void VideoWriterImpl::InitializeEncoder(const GUID codec, const double fps) initializeParams.encodeConfig->rcParams.maxBitRate = encoderParams.maxBitRate; initializeParams.encodeConfig->rcParams.targetQuality = encoderParams.targetQuality; initializeParams.encodeConfig->gopLength = encoderParams.gopLength; -#if !defined(WIN32_WAIT_FOR_FFMPEG_WRAPPER_UPDATE) if (initializeParams.encodeConfig->frameIntervalP > 1) { CV_Assert(encoderCallback->setFrameIntervalP(initializeParams.encodeConfig->frameIntervalP)); } -#endif - if (codec == NV_ENC_CODEC_H264_GUID) + if (codec == NV_ENC_CODEC_H264_GUID) { initializeParams.encodeConfig->encodeCodecConfig.h264Config.idrPeriod = encoderParams.idrPeriod; - else if (codec == NV_ENC_CODEC_HEVC_GUID) + if (encoderParams.videoFullRangeFlag) { + initializeParams.encodeConfig->encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag = 1; + initializeParams.encodeConfig->encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1; + } + } + else if (codec == NV_ENC_CODEC_HEVC_GUID) { initializeParams.encodeConfig->encodeCodecConfig.hevcConfig.idrPeriod = encoderParams.idrPeriod; + if (encoderParams.videoFullRangeFlag) { + initializeParams.encodeConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.videoFullRangeFlag = 1; + initializeParams.encodeConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.videoSignalTypePresentFlag = 1; + } + } + else { + std::string msg = "videoFullRangeFlag is not supported by codec: " + GetVideoCodecString(codec); + CV_LOG_WARNING(NULL, msg); + } pEnc->CreateEncoder(&initializeParams); } diff --git a/modules/cudacodec/test/test_video.cpp b/modules/cudacodec/test/test_video.cpp index 29f25b2ca..deb75aafb 100644 --- a/modules/cudacodec/test/test_video.cpp +++ b/modules/cudacodec/test/test_video.cpp @@ -1011,10 +1011,6 @@ struct H264ToH265 : SetDevice { }; -#if defined(WIN32) // remove when FFmpeg wrapper includes PR25874 -#define WIN32_WAIT_FOR_FFMPEG_WRAPPER_UPDATE -#endif - CUDA_TEST_P(H264ToH265, Transcode) { const std::string inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "../highgui/video/big_buck_bunny.h264"; @@ -1054,10 +1050,8 @@ CUDA_TEST_P(H264ToH265, Transcode) for (int i = 0; i < nFrames; ++i) { cap >> frame; ASSERT_FALSE(frame.empty()); -#if !defined(WIN32_WAIT_FOR_FFMPEG_WRAPPER_UPDATE) const int pts = static_cast(cap.get(CAP_PROP_PTS)); ASSERT_EQ(i, pts > 0 ? pts : 0); // FFmpeg back end returns dts if pts is zero. -#endif } } ASSERT_EQ(0, remove(outputFile.c_str())); @@ -1066,7 +1060,7 @@ CUDA_TEST_P(H264ToH265, Transcode) INSTANTIATE_TEST_CASE_P(CUDA_Codec, H264ToH265, ALL_DEVICES); CV_ENUM(YuvColorFormats, cudacodec::ColorFormat::NV_YUV444, cudacodec::ColorFormat::NV_YUV420_10BIT, cudacodec::ColorFormat::NV_YUV444_10BIT) -PARAM_TEST_CASE(YUVFormats, cv::cuda::DeviceInfo, YuvColorFormats) +PARAM_TEST_CASE(YUVFormats, cv::cuda::DeviceInfo, YuvColorFormats, bool) { }; @@ -1075,6 +1069,7 @@ CUDA_TEST_P(YUVFormats, Transcode) cv::cuda::setDevice(GET_PARAM(0).deviceID()); const std::string inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "../highgui/video/big_buck_bunny.h265"; const cv::cudacodec::ColorFormat writerColorFormat = static_cast(static_cast(GET_PARAM(1))); + const bool fullRange = GET_PARAM(2); constexpr double fps = 25; const cudacodec::Codec codec = cudacodec::Codec::HEVC; const std::string ext = ".mp4"; @@ -1088,6 +1083,7 @@ CUDA_TEST_P(YUVFormats, Transcode) cv::cudacodec::EncoderParams params; params.tuningInfo = cv::cudacodec::EncodeTuningInfo::ENC_TUNING_INFO_LOSSLESS; params.rateControlMode = cv::cudacodec::EncodeParamsRcMode::ENC_PARAMS_RC_CONSTQP; + params.videoFullRangeFlag = fullRange; for (int i = 0; i < nFrames; ++i) { ASSERT_TRUE(cap.read(frame)); ASSERT_FALSE(frame.empty()); @@ -1101,7 +1097,7 @@ CUDA_TEST_P(YUVFormats, Transcode) yuvFormat = cudacodec::SurfaceFormat::SF_P016; bitDepth = cudacodec::BitDepth::SIXTEEN; } - generateTestImages(frame, yuv, bgr, yuvFormat, cudacodec::ColorFormat::BGR, bitDepth, false); + generateTestImages(frame, yuv, bgr, yuvFormat, cudacodec::ColorFormat::BGR, bitDepth, false, fullRange); bgrGs.push_back(bgr.clone()); if (writer.empty()) writer = cv::cudacodec::createVideoWriter(outputFile, frame.size(), codec, fps, writerColorFormat, params); @@ -1125,7 +1121,7 @@ CUDA_TEST_P(YUVFormats, Transcode) ASSERT_EQ(0, remove(outputFile.c_str())); } -INSTANTIATE_TEST_CASE_P(CUDA_Codec, YUVFormats, testing::Combine(ALL_DEVICES, YuvColorFormats::all())); +INSTANTIATE_TEST_CASE_P(CUDA_Codec, YUVFormats, testing::Combine(ALL_DEVICES, YuvColorFormats::all(), testing::Bool())); #endif #if defined(HAVE_NVCUVENC) @@ -1201,10 +1197,8 @@ CUDA_TEST_P(Write, Writer) for (int i = 0; i < nFrames; ++i) { cap >> frame; ASSERT_FALSE(frame.empty()); -#if !defined(WIN32_WAIT_FOR_FFMPEG_WRAPPER_UPDATE) const int pts = static_cast(cap.get(CAP_PROP_PTS)); ASSERT_EQ(i, pts > 0 ? pts : 0); // FFmpeg back end returns dts if pts is zero. -#endif } } ASSERT_EQ(0, remove(outputFile.c_str())); @@ -1299,10 +1293,8 @@ CUDA_TEST_P(EncoderParams, Writer) const bool keyFrameActual = capRaw.get(CAP_PROP_LRF_HAS_KEY_FRAME) == 1.0; const bool keyFrameReference = i % idrPeriod == 0; ASSERT_EQ(keyFrameActual, keyFrameReference); -#if !defined(WIN32_WAIT_FOR_FFMPEG_WRAPPER_UPDATE) const int pts = static_cast(cap.get(CAP_PROP_PTS)); ASSERT_EQ(i, pts > 0 ? pts : 0); // FFmpeg back end returns dts if pts is zero. -#endif } } } diff --git a/modules/cudalegacy/include/opencv2/cudalegacy/private.hpp b/modules/cudalegacy/include/opencv2/cudalegacy/private.hpp index fd3286c17..945a6bc4f 100644 --- a/modules/cudalegacy/include/opencv2/cudalegacy/private.hpp +++ b/modules/cudalegacy/include/opencv2/cudalegacy/private.hpp @@ -91,6 +91,6 @@ namespace cv { namespace cuda //! @endcond -#endif HAVE_CUDA +#endif // HAVE_CUDA #endif // OPENCV_CORE_CUDALEGACY_PRIVATE_HPP diff --git a/modules/ml/CMakeLists.txt b/modules/ml/CMakeLists.txt index 794653e81..7f029f5f3 100644 --- a/modules/ml/CMakeLists.txt +++ b/modules/ml/CMakeLists.txt @@ -7,4 +7,5 @@ ocv_create_module() ocv_add_accuracy_tests() ocv_add_perf_tests() -ocv_add_samples(opencv_imgproc opencv_objdetect opencv_video) \ No newline at end of file +# dnn is transitive dependency pf opencv_video +ocv_add_samples(opencv_imgproc opencv_objdetect opencv_video opencv_dnn)