Skip to content

Commit 8679014

Browse files
committed
Extend Codec and ColorFormat enums instead of using VideoWriter specific ones and use camel-case for enum names.
1 parent d3b58df commit 8679014

File tree

6 files changed

+166
-171
lines changed

6 files changed

+166
-171
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

Lines changed: 57 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -66,43 +66,64 @@ using namespace cuda; // Stream
6666

6767
////////////////////////////////// Video Encoding //////////////////////////////////
6868

69-
/** @brief Codecs supported by Video writer, refer to Nvidia's GPU Support Matrix to confirm your GPU supports them.
70-
*/
71-
enum class CODEC_VW
69+
/** @brief Video codecs supported by cudacodec::VideoReader and cudacodec::VideoWriter.
70+
@note
71+
- Support will depend on your hardware, refer to the Nvidia Video Codec SDK Video Encode and Decode GPU Support Matrix for details.
72+
*/
73+
enum Codec
7274
{
73-
H264 = 0,
74-
HEVC = 1,
75-
NumCodecs = 2
75+
MPEG1 = 0,
76+
MPEG2,
77+
MPEG4,
78+
VC1,
79+
H264,
80+
JPEG,
81+
H264_SVC,
82+
H264_MVC,
83+
HEVC,
84+
VP8,
85+
VP9,
86+
AV1,
87+
NumCodecs,
88+
89+
Uncompressed_YUV420 = (('I' << 24) | ('Y' << 16) | ('U' << 8) | ('V')), //!< Y,U,V (4:2:0)
90+
Uncompressed_YV12 = (('Y' << 24) | ('V' << 16) | ('1' << 8) | ('2')), //!< Y,V,U (4:2:0)
91+
Uncompressed_NV12 = (('N' << 24) | ('V' << 16) | ('1' << 8) | ('2')), //!< Y,UV (4:2:0)
92+
Uncompressed_YUYV = (('Y' << 24) | ('U' << 16) | ('Y' << 8) | ('V')), //!< YUYV/YUY2 (4:2:2)
93+
Uncompressed_UYVY = (('U' << 24) | ('Y' << 16) | ('V' << 8) | ('Y')) //!< UYVY (4:2:2)
7694
};
7795

78-
/** @brief Video writer color formats.
96+
/** @brief ColorFormat for the frame returned by VideoReader::nextFrame()/VideoReader::retrieve() or used to initialize a VideoWriter.
7997
*/
80-
enum COLOR_FORMAT_VW {
98+
enum class ColorFormat {
8199
UNDEFINED = 0,
82-
BGR = 1, //!< Default OpenCV color format.
83-
RGB = 2,
84-
BGRA = 3,
85-
RGBA = 4,
86-
GRAY = 5,
87-
88-
NV_NV12 = 6, //!< Nvidia Buffer Format - Semi-Planar YUV [Y plane followed by interleaved UV plane].
89-
NV_YV12 = 7, //!< Nvidia Buffer Format - Planar YUV [Y plane followed by V and U planes].
90-
NV_IYUV = 8, //!< Nvidia Buffer Format - Planar YUV [Y plane followed by U and V planes].
91-
NV_YUV444 = 9, //!< Nvidia Buffer Format - Planar YUV [Y plane followed by U and V planes].
92-
NV_AYUV = 10 //!< Nvidia Buffer Format - 8 bit Packed A8Y8U8V8. This is a word-ordered format where a pixel is represented by a 32-bit word with V in the lowest 8 bits, U in the next 8 bits, Y in the 8 bits after that and A in the highest 8 bits.
100+
BGRA = 1, //!< OpenCV color format, can be used with both VideoReader and VideoWriter.
101+
BGR = 2, //!< OpenCV color format, can be used with both VideoReader and VideoWriter.
102+
GRAY = 3, //!< OpenCV color format, can be used with both VideoReader and VideoWriter.
103+
NV_NV12 = 4, //!< Nvidia color format - equivalent to YUV - Semi-Planar YUV [Y plane followed by interleaved UV plane], can be used with both VideoReader and VideoWriter.
104+
105+
RGB = 5, //!< OpenCV color format, can only be used with VideoWriter.
106+
RGBA = 6, //!< OpenCV color format, can only be used with VideoWriter.
107+
NV_YV12 = 8, //!< Nvidia Buffer Format - Planar YUV [Y plane followed by V and U planes], use with VideoReader, can only be used with VideoWriter.
108+
NV_IYUV = 9, //!< Nvidia Buffer Format - Planar YUV [Y plane followed by U and V planes], use with VideoReader, can only be used with VideoWriter.
109+
NV_YUV444 = 10, //!< Nvidia Buffer Format - Planar YUV [Y plane followed by U and V planes], use with VideoReader, can only be used with VideoWriter.
110+
NV_AYUV = 11, //!< Nvidia Buffer Format - 8 bit Packed A8Y8U8V8. This is a word-ordered format where a pixel is represented by a 32-bit word with V in the lowest 8 bits, U in the next 8 bits, Y in the 8 bits after that and A in the highest 8 bits, can only be used with VideoWriter.
111+
#ifndef CV_DOXYGEN
112+
PROP_NOT_SUPPORTED
113+
#endif
93114
};
94115

95116
/** @brief Rate Control Modes.
96117
*/
97-
enum ENC_PARAMS_RC_MODE {
118+
enum EncodeParamsRcMode {
98119
ENC_PARAMS_RC_CONSTQP = 0x0, //!< Constant QP mode.
99120
ENC_PARAMS_RC_VBR = 0x1, //!< Variable bitrate mode.
100121
ENC_PARAMS_RC_CBR = 0x2 //!< Constant bitrate mode.
101122
};
102123

103124
/** @brief Multi Pass Encoding.
104125
*/
105-
enum ENC_MULTI_PASS
126+
enum EncodeMultiPass
106127
{
107128
ENC_MULTI_PASS_DISABLED = 0x0, //!< Single Pass.
108129
ENC_TWO_PASS_QUARTER_RESOLUTION = 0x1, //!< Two Pass encoding is enabled where first Pass is quarter resolution.
@@ -112,7 +133,7 @@ enum ENC_MULTI_PASS
112133

113134
/** @brief Supported Encoder Profiles.
114135
*/
115-
enum ENC_PROFILE {
136+
enum EncodeProfile {
116137
ENC_CODEC_PROFILE_AUTOSELECT = 0,
117138
ENC_H264_PROFILE_BASELINE = 1,
118139
ENC_H264_PROFILE_MAIN = 2,
@@ -128,7 +149,7 @@ enum ENC_PROFILE {
128149

129150
/** @brief Nvidia Encoding Presets. Performance degrades and quality improves as we move from P1 to P7.
130151
*/
131-
enum ENC_PRESET {
152+
enum EncodePreset {
132153
ENC_PRESET_P1 = 1,
133154
ENC_PRESET_P2 = 2,
134155
ENC_PRESET_P3 = 3,
@@ -140,7 +161,7 @@ enum ENC_PRESET {
140161

141162
/** @brief Tuning information.
142163
*/
143-
enum ENC_TUNING_INFO {
164+
enum EncodeTuningInfo {
144165
ENC_TUNING_INFO_UNDEFINED = 0, //!< Undefined tuningInfo. Invalid value for encoding.
145166
ENC_TUNING_INFO_HIGH_QUALITY = 1, //!< Tune presets for latency tolerant encoding.
146167
ENC_TUNING_INFO_LOW_LATENCY = 2, //!< Tune presets for low latency streaming.
@@ -151,7 +172,7 @@ enum ENC_TUNING_INFO {
151172

152173
/** Quantization Parameter for each type of frame when using ENC_PARAMS_RC_MODE::ENC_PARAMS_RC_CONSTQP.
153174
*/
154-
struct CV_EXPORTS_W_SIMPLE ENC_QP
175+
struct CV_EXPORTS_W_SIMPLE EncodeQp
155176
{
156177
CV_PROP_RW uint32_t qpInterP; //!< Specifies QP value for P-frame.
157178
CV_PROP_RW uint32_t qpInterB; //!< Specifies QP value for B-frame.
@@ -164,12 +185,12 @@ struct CV_EXPORTS_W_SIMPLE EncoderParams
164185
{
165186
public:
166187
CV_WRAP EncoderParams();
167-
CV_PROP_RW ENC_PRESET nvPreset;
168-
CV_PROP_RW ENC_TUNING_INFO tuningInfo;
169-
CV_PROP_RW ENC_PROFILE encodingProfile;
170-
CV_PROP_RW ENC_PARAMS_RC_MODE rateControlMode;
171-
CV_PROP_RW ENC_MULTI_PASS multiPassEncoding;
172-
CV_PROP_RW ENC_QP constQp; //!< QP's for ENC_PARAMS_RC_CONSTQP.
188+
CV_PROP_RW EncodePreset nvPreset;
189+
CV_PROP_RW EncodeTuningInfo tuningInfo;
190+
CV_PROP_RW EncodeProfile encodingProfile;
191+
CV_PROP_RW EncodeParamsRcMode rateControlMode;
192+
CV_PROP_RW EncodeMultiPass multiPassEncoding;
193+
CV_PROP_RW EncodeQp constQp; //!< QP's for ENC_PARAMS_RC_CONSTQP.
173194
CV_PROP_RW int averageBitRate; //!< target bitrate for ENC_PARAMS_RC_VBR and ENC_PARAMS_RC_CBR.
174195
CV_PROP_RW int maxBitRate; //!< upper bound on bitrate for ENC_PARAMS_RC_VBR and ENC_PARAMS_RC_CONSTQP.
175196
CV_PROP_RW uint8_t targetQuality; //!< value 0 - 51 where video quality decreases as targetQuality increases, used with ENC_PARAMS_RC_VBR.
@@ -234,8 +255,8 @@ class CV_EXPORTS_W VideoWriter
234255
@param encoderCallback Callbacks for video encoder. See cudacodec::EncoderCallback. Required for working with the encoded video stream.
235256
@param stream Stream for frame pre-processing.
236257
*/
237-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const CODEC_VW codec = CODEC_VW::H264, const double fps = 25.0,
238-
const COLOR_FORMAT_VW colorFormat = BGR, Ptr<EncoderCallback> encoderCallback = 0, const Stream& stream = Stream::Null());
258+
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const Codec codec = Codec::H264, const double fps = 25.0,
259+
const ColorFormat colorFormat = ColorFormat::BGR, Ptr<EncoderCallback> encoderCallback = 0, const Stream& stream = Stream::Null());
239260

240261
/** @brief Creates video writer.
241262
@@ -248,36 +269,11 @@ CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileNam
248269
@param encoderCallback Callbacks for video encoder. See cudacodec::EncoderCallback. Required for working with the encoded video stream.
249270
@param stream Stream for frame pre-processing.
250271
*/
251-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const CODEC_VW codec, const double fps, const COLOR_FORMAT_VW colorFormat,
272+
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const Codec codec, const double fps, const ColorFormat colorFormat,
252273
const EncoderParams& params, Ptr<EncoderCallback> encoderCallback = 0, const Stream& stream = Stream::Null());
253274

254275
////////////////////////////////// Video Decoding //////////////////////////////////////////
255276

256-
/** @brief Video codecs supported by cudacodec::VideoReader.
257-
*/
258-
enum Codec
259-
{
260-
MPEG1 = 0,
261-
MPEG2,
262-
MPEG4,
263-
VC1,
264-
H264,
265-
JPEG,
266-
H264_SVC,
267-
H264_MVC,
268-
HEVC,
269-
VP8,
270-
VP9,
271-
AV1,
272-
NumCodecs,
273-
274-
Uncompressed_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), //!< Y,U,V (4:2:0)
275-
Uncompressed_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), //!< Y,V,U (4:2:0)
276-
Uncompressed_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), //!< Y,UV (4:2:0)
277-
Uncompressed_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), //!< YUYV/YUY2 (4:2:2)
278-
Uncompressed_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')) //!< UYVY (4:2:2)
279-
};
280-
281277
/** @brief Chroma formats supported by cudacodec::VideoReader.
282278
*/
283279
enum ChromaFormat
@@ -344,18 +340,6 @@ enum class VideoReaderProps {
344340
#endif
345341
};
346342

347-
/** @brief ColorFormat for the frame returned by nextFrame()/retrieve().
348-
*/
349-
enum class ColorFormat {
350-
BGRA = 1,
351-
BGR = 2,
352-
GRAY = 3,
353-
YUV = 4,
354-
#ifndef CV_DOXYGEN
355-
PROP_NOT_SUPPORTED
356-
#endif
357-
};
358-
359343
/** @brief Video reader interface.
360344
361345
@note
@@ -451,8 +435,9 @@ class CV_EXPORTS_W VideoReader
451435
/** @brief Set the desired ColorFormat for the frame returned by nextFrame()/retrieve().
452436
453437
@param colorFormat Value of the ColorFormat.
438+
@return `true` unless the colorFormat is not supported.
454439
*/
455-
CV_WRAP virtual void set(const ColorFormat colorFormat) = 0;
440+
CV_WRAP virtual bool set(const ColorFormat colorFormat) = 0;
456441

457442
/** @brief Returns the specified VideoReader property
458443

modules/cudacodec/misc/python/test/test_cudacodec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ def test_writer_existence(self):
8585
encoder_params_in.gopLength = 10
8686
stream = cv.cuda.Stream()
8787
sz = (1920,1080)
88-
writer = cv.cudacodec.createVideoWriter(fname, sz, cv.cudacodec.VideoWriterCodec_H264, 30, cv.cudacodec.ColorFormat_BGR,
89-
encoder_params_in, stream)
88+
writer = cv.cudacodec.createVideoWriter(fname, sz, cv.cudacodec.H264, 30, cv.cudacodec.ColorFormat_BGR,
89+
encoder_params_in, stream=stream)
9090
blankFrameIn = cv.cuda.GpuMat(sz,cv.CV_8UC3)
9191
writer.write(blankFrameIn)
92-
writer.close()
92+
writer.release()
9393
encoder_params_out = writer.getEncoderParams()
9494
self.assert_true(encoder_params_in.gopLength == encoder_params_out.gopLength)
9595
cap = cv.VideoCapture(fname,cv.CAP_FFMPEG)

modules/cudacodec/perf/perf_video.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,41 +97,41 @@ PERF_TEST_P(FileName, VideoReader, VIDEO_SRC)
9797

9898
#if defined(HAVE_NVCUVENC)
9999

100-
DEF_PARAM_TEST(WriteToFile, string, cv::cudacodec::COLOR_FORMAT_VW, cv::cudacodec::CODEC_VW);
100+
DEF_PARAM_TEST(WriteToFile, string, cv::cudacodec::ColorFormat, cv::cudacodec::Codec);
101101

102-
#define COLOR_FORMAT Values(cv::cudacodec::COLOR_FORMAT_VW::BGR, cv::cudacodec::COLOR_FORMAT_VW::RGB, cv::cudacodec::COLOR_FORMAT_VW::BGRA, \
103-
cv::cudacodec::COLOR_FORMAT_VW::RGBA, cv::cudacodec::COLOR_FORMAT_VW::GRAY)
104-
#define CODEC Values(cv::cudacodec::CODEC_VW::H264, cv::cudacodec::CODEC_VW::HEVC)
102+
#define COLOR_FORMAT Values(cv::cudacodec::ColorFormat::BGR, cv::cudacodec::ColorFormat::RGB, cv::cudacodec::ColorFormat::BGRA, \
103+
cv::cudacodec::ColorFormat::RGBA, cv::cudacodec::ColorFormat::GRAY)
104+
#define CODEC Values(cv::cudacodec::Codec::H264, cv::cudacodec::Codec::HEVC)
105105

106106
PERF_TEST_P(WriteToFile, VideoWriter, Combine(VIDEO_SRC, COLOR_FORMAT, CODEC))
107107
{
108108
declare.time(30);
109109
const string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));
110-
const cv::cudacodec::COLOR_FORMAT_VW surfaceFormat = GET_PARAM(1);
111-
const cudacodec::CODEC_VW codec = GET_PARAM(2);
110+
const cv::cudacodec::ColorFormat surfaceFormat = GET_PARAM(1);
111+
const cudacodec::Codec codec = GET_PARAM(2);
112112
const double fps = 25;
113113
const int nFrames = 20;
114114
cv::VideoCapture reader(inputFile);
115115
ASSERT_TRUE(reader.isOpened());
116116
Mat frameBgr;
117117
if (PERF_RUN_CUDA()) {
118-
const std::string ext = codec == cudacodec::CODEC_VW::H264 ? ".h264" : ".hevc";
118+
const std::string ext = codec == cudacodec::Codec::H264 ? ".h264" : ".hevc";
119119
const string outputFile = cv::tempfile(ext.c_str());
120120
std::vector<GpuMat> frames;
121121
cv::Mat frameNewSf;
122122
cuda::Stream stream;
123123
ColorConversionCodes conversionCode = COLOR_COLORCVT_MAX;
124124
switch (surfaceFormat) {
125-
case cudacodec::COLOR_FORMAT_VW::RGB:
125+
case cudacodec::ColorFormat::RGB:
126126
conversionCode = COLOR_BGR2RGB;
127127
break;
128-
case cudacodec::COLOR_FORMAT_VW::BGRA:
128+
case cudacodec::ColorFormat::BGRA:
129129
conversionCode = COLOR_BGR2BGRA;
130130
break;
131-
case cudacodec::COLOR_FORMAT_VW::RGBA:
131+
case cudacodec::ColorFormat::RGBA:
132132
conversionCode = COLOR_BGR2RGBA;
133133
break;
134-
case cudacodec::COLOR_FORMAT_VW::GRAY:
134+
case cudacodec::ColorFormat::GRAY:
135135
conversionCode = COLOR_BGR2GRAY;
136136
default:
137137
break;
@@ -161,7 +161,7 @@ PERF_TEST_P(WriteToFile, VideoWriter, Combine(VIDEO_SRC, COLOR_FORMAT, CODEC))
161161
ASSERT_EQ(0, remove(outputFile.c_str()));
162162
}
163163
else {
164-
if (surfaceFormat != cv::cudacodec::COLOR_FORMAT_VW::BGR || codec != cv::cudacodec::CODEC_VW::H264)
164+
if (surfaceFormat != cv::cudacodec::ColorFormat::BGR || codec != cv::cudacodec::Codec::H264)
165165
throw PerfSkipTestException();
166166
cv::VideoWriter writer;
167167
const string outputFile = cv::tempfile(".avi");

modules/cudacodec/src/video_reader.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Ptr<VideoReader> cv::cudacodec::createVideoReader(const Ptr<RawVideoSource>&, co
5454
#else // HAVE_NVCUVID
5555

5656
void nv12ToBgra(const GpuMat& decodedFrame, GpuMat& outFrame, int width, int height, cudaStream_t stream);
57+
bool ValidColorFormat(const ColorFormat colorFormat);
5758

5859
void videoDecPostProcessFrame(const GpuMat& decodedFrame, GpuMat& outFrame, int width, int height, const ColorFormat colorFormat,
5960
Stream stream)
@@ -74,7 +75,7 @@ void videoDecPostProcessFrame(const GpuMat& decodedFrame, GpuMat& outFrame, int
7475
outFrame.create(height, width, CV_8UC1);
7576
cudaMemcpy2DAsync(outFrame.ptr(), outFrame.step, decodedFrame.ptr(), decodedFrame.step, width, height, cudaMemcpyDeviceToDevice, StreamAccessor::getStream(stream));
7677
}
77-
else if (colorFormat == ColorFormat::YUV) {
78+
else if (colorFormat == ColorFormat::NV_NV12) {
7879
decodedFrame.copyTo(outFrame, stream);
7980
}
8081
}
@@ -100,7 +101,7 @@ namespace
100101

101102
bool set(const VideoReaderProps propertyId, const double propertyVal) CV_OVERRIDE;
102103

103-
void set(const ColorFormat _colorFormat) CV_OVERRIDE;
104+
bool set(const ColorFormat colorFormat_) CV_OVERRIDE;
104105

105106
bool get(const VideoReaderProps propertyId, double& propertyVal) const CV_OVERRIDE;
106107
bool getVideoReaderProps(const VideoReaderProps propertyId, double& propertyValOut, double propertyValIn) const CV_OVERRIDE;
@@ -273,8 +274,16 @@ namespace
273274
return false;
274275
}
275276

276-
void VideoReaderImpl::set(const ColorFormat _colorFormat) {
277-
colorFormat = _colorFormat;
277+
bool ValidColorFormat(const ColorFormat colorFormat) {
278+
if (colorFormat == ColorFormat::BGRA || colorFormat == ColorFormat::BGR || colorFormat == ColorFormat::GRAY || colorFormat == ColorFormat::NV_NV12)
279+
return true;
280+
return false;
281+
}
282+
283+
bool VideoReaderImpl::set(const ColorFormat colorFormat_) {
284+
if (!ValidColorFormat(colorFormat_)) return false;
285+
colorFormat = colorFormat_;
286+
return true;
278287
}
279288

280289
bool VideoReaderImpl::get(const VideoReaderProps propertyId, double& propertyVal) const {

0 commit comments

Comments
 (0)