Skip to content

Commit 2af5458

Browse files
authored
Merge pull request #3868 from cudawarped:cudacodec_videowriter_add_full_range_flag
`cudacodec::VideoWriter` add flag for writing full range luma chroma video
2 parents 227687d + b4a91cd commit 2af5458

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ struct CV_EXPORTS_W_SIMPLE EncoderParams
186186
public:
187187
CV_WRAP EncoderParams() : nvPreset(ENC_PRESET_P3), tuningInfo(ENC_TUNING_INFO_HIGH_QUALITY), encodingProfile(ENC_CODEC_PROFILE_AUTOSELECT),
188188
rateControlMode(ENC_PARAMS_RC_VBR), multiPassEncoding(ENC_MULTI_PASS_DISABLED), constQp({ 0,0,0 }), averageBitRate(0), maxBitRate(0),
189-
targetQuality(30), gopLength(250), idrPeriod(250) {};
189+
targetQuality(30), gopLength(250), idrPeriod(250), videoFullRangeFlag(false){};
190190
CV_PROP_RW EncodePreset nvPreset;
191191
CV_PROP_RW EncodeTuningInfo tuningInfo;
192192
CV_PROP_RW EncodeProfile encodingProfile;
@@ -198,6 +198,7 @@ struct CV_EXPORTS_W_SIMPLE EncoderParams
198198
CV_PROP_RW uint8_t targetQuality; //!< value 0 - 51 where video quality decreases as targetQuality increases, used with \ref ENC_PARAMS_RC_VBR.
199199
CV_PROP_RW int gopLength; //!< the number of pictures in one GOP, ensuring \ref idrPeriod >= \ref gopLength.
200200
CV_PROP_RW int idrPeriod; //!< IDR interval, ensuring \ref idrPeriod >= \ref gopLength.
201+
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.
201202
};
202203
CV_EXPORTS bool operator==(const EncoderParams& lhs, const EncoderParams& rhs);
203204

modules/cudacodec/src/video_writer.cpp

+23-2
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ GUID EncodingPresetGuid(const EncodePreset nvPreset) {
315315
CV_Error(Error::StsUnsupportedFormat, msg);
316316
}
317317

318+
std::string GetVideoCodecString(const GUID codec) {
319+
if (codec == NV_ENC_CODEC_H264_GUID) return "AVC/H.264";
320+
else if (codec == NV_ENC_CODEC_HEVC_GUID) return "H.265/HEVC";
321+
else if (codec == NV_ENC_CODEC_AV1_GUID) return "AV1";
322+
else return "Unknown";
323+
}
324+
318325
void VideoWriterImpl::InitializeEncoder(const GUID codec, const double fps)
319326
{
320327
NV_ENC_INITIALIZE_PARAMS initializeParams = {};
@@ -334,10 +341,24 @@ void VideoWriterImpl::InitializeEncoder(const GUID codec, const double fps)
334341
if (initializeParams.encodeConfig->frameIntervalP > 1) {
335342
CV_Assert(encoderCallback->setFrameIntervalP(initializeParams.encodeConfig->frameIntervalP));
336343
}
337-
if (codec == NV_ENC_CODEC_H264_GUID)
344+
if (codec == NV_ENC_CODEC_H264_GUID) {
338345
initializeParams.encodeConfig->encodeCodecConfig.h264Config.idrPeriod = encoderParams.idrPeriod;
339-
else if (codec == NV_ENC_CODEC_HEVC_GUID)
346+
if (encoderParams.videoFullRangeFlag) {
347+
initializeParams.encodeConfig->encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag = 1;
348+
initializeParams.encodeConfig->encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1;
349+
}
350+
}
351+
else if (codec == NV_ENC_CODEC_HEVC_GUID) {
340352
initializeParams.encodeConfig->encodeCodecConfig.hevcConfig.idrPeriod = encoderParams.idrPeriod;
353+
if (encoderParams.videoFullRangeFlag) {
354+
initializeParams.encodeConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.videoFullRangeFlag = 1;
355+
initializeParams.encodeConfig->encodeCodecConfig.hevcConfig.hevcVUIParameters.videoSignalTypePresentFlag = 1;
356+
}
357+
}
358+
else {
359+
std::string msg = "videoFullRangeFlag is not supported by codec: " + GetVideoCodecString(codec);
360+
CV_LOG_WARNING(NULL, msg);
361+
}
341362
pEnc->CreateEncoder(&initializeParams);
342363
}
343364

modules/cudacodec/test/test_video.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ CUDA_TEST_P(H264ToH265, Transcode)
10601060
INSTANTIATE_TEST_CASE_P(CUDA_Codec, H264ToH265, ALL_DEVICES);
10611061

10621062
CV_ENUM(YuvColorFormats, cudacodec::ColorFormat::NV_YUV444, cudacodec::ColorFormat::NV_YUV420_10BIT, cudacodec::ColorFormat::NV_YUV444_10BIT)
1063-
PARAM_TEST_CASE(YUVFormats, cv::cuda::DeviceInfo, YuvColorFormats)
1063+
PARAM_TEST_CASE(YUVFormats, cv::cuda::DeviceInfo, YuvColorFormats, bool)
10641064
{
10651065
};
10661066

@@ -1069,6 +1069,7 @@ CUDA_TEST_P(YUVFormats, Transcode)
10691069
cv::cuda::setDevice(GET_PARAM(0).deviceID());
10701070
const std::string inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "../highgui/video/big_buck_bunny.h265";
10711071
const cv::cudacodec::ColorFormat writerColorFormat = static_cast<cudacodec::ColorFormat>(static_cast<int>(GET_PARAM(1)));
1072+
const bool fullRange = GET_PARAM(2);
10721073
constexpr double fps = 25;
10731074
const cudacodec::Codec codec = cudacodec::Codec::HEVC;
10741075
const std::string ext = ".mp4";
@@ -1082,6 +1083,7 @@ CUDA_TEST_P(YUVFormats, Transcode)
10821083
cv::cudacodec::EncoderParams params;
10831084
params.tuningInfo = cv::cudacodec::EncodeTuningInfo::ENC_TUNING_INFO_LOSSLESS;
10841085
params.rateControlMode = cv::cudacodec::EncodeParamsRcMode::ENC_PARAMS_RC_CONSTQP;
1086+
params.videoFullRangeFlag = fullRange;
10851087
for (int i = 0; i < nFrames; ++i) {
10861088
ASSERT_TRUE(cap.read(frame));
10871089
ASSERT_FALSE(frame.empty());
@@ -1095,7 +1097,7 @@ CUDA_TEST_P(YUVFormats, Transcode)
10951097
yuvFormat = cudacodec::SurfaceFormat::SF_P016;
10961098
bitDepth = cudacodec::BitDepth::SIXTEEN;
10971099
}
1098-
generateTestImages(frame, yuv, bgr, yuvFormat, cudacodec::ColorFormat::BGR, bitDepth, false);
1100+
generateTestImages(frame, yuv, bgr, yuvFormat, cudacodec::ColorFormat::BGR, bitDepth, false, fullRange);
10991101
bgrGs.push_back(bgr.clone());
11001102
if (writer.empty())
11011103
writer = cv::cudacodec::createVideoWriter(outputFile, frame.size(), codec, fps, writerColorFormat, params);
@@ -1119,7 +1121,7 @@ CUDA_TEST_P(YUVFormats, Transcode)
11191121
ASSERT_EQ(0, remove(outputFile.c_str()));
11201122
}
11211123

1122-
INSTANTIATE_TEST_CASE_P(CUDA_Codec, YUVFormats, testing::Combine(ALL_DEVICES, YuvColorFormats::all()));
1124+
INSTANTIATE_TEST_CASE_P(CUDA_Codec, YUVFormats, testing::Combine(ALL_DEVICES, YuvColorFormats::all(), testing::Bool()));
11231125
#endif
11241126

11251127
#if defined(HAVE_NVCUVENC)

0 commit comments

Comments
 (0)