Skip to content

Commit 4fa9c71

Browse files
committed
Four factory method version.
1 parent e1bd434 commit 4fa9c71

File tree

5 files changed

+141
-371
lines changed

5 files changed

+141
-371
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

Lines changed: 18 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -68,40 +68,28 @@ using namespace cuda; // Stream
6868

6969
/** @brief Codecs supported by Video writer, refer to Nvidia's GPU Support Matrix to confirm your GPU supports them.
7070
*/
71-
enum class VideoWriterCodec
71+
enum class CODEC_VW
7272
{
7373
H264 = 0,
7474
HEVC = 1,
7575
NumCodecs = 2
7676
};
7777

78-
/** @brief OpenCV color formats.
78+
/** @brief Video writer color formats.
7979
*/
80-
enum COLOR_FORMAT_CV {
80+
enum COLOR_FORMAT_VW {
8181
UNDEFINED = 0,
8282
BGR = 1, //!< Default OpenCV color format.
8383
RGB = 2,
8484
BGRA = 3,
8585
RGBA = 4,
86-
GRAY = 5
87-
};
86+
GRAY = 5,
8887

89-
/** @brief Nvidia Video Codec SDK surface formats.
90-
*/
91-
enum ENC_BUFFER_FORMAT
92-
{
93-
BF_UNDEFINED = 0x00000000, //!< Undefined buffer format.
94-
BF_NV12 = 0x00000001, //!< Semi-Planar YUV [Y plane followed by interleaved UV plane].
95-
BF_YV12 = 0x00000010, //!< Planar YUV [Y plane followed by V and U planes].
96-
BF_IYUV = 0x00000100, //!< Planar YUV [Y plane followed by U and V planes].
97-
BF_YUV444 = 0x00001000, //!< Planar YUV [Y plane followed by U and V planes].
98-
BF_YUV420_10BIT = 0x00010000, //!< 10 bit Semi-Planar YUV [Y plane followed by interleaved UV plane]. Each pixel of size 2 bytes. Most Significant 10 bits contain pixel data.
99-
BF_YUV444_10BIT = 0x00100000, //!< 10 bit Planar YUV444 [Y plane followed by U and V planes]. Each pixel of size 2 bytes. Most Significant 10 bits contain pixel data.
100-
BF_ARGB = 0x01000000, //!< 8 bit Packed A8R8G8B8. This is a word-ordered format where a pixel is represented by a 32-bit word with B in the lowest 8 bits, G in the next 8 bits, R in the 8 bits after that and A in the highest 8 bits.
101-
BF_ARGB10 = 0x02000000, //!< 10 bit Packed A2R10G10B10. This is a word-ordered format where a pixel is represented by a 32-bit word with B in the lowest 10 bits, G in the next 10 bits, R in the 10 bits after that and A in the highest 2 bits.
102-
BF_AYUV = 0x04000000, //!< 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.
103-
BF_ABGR = 0x10000000, //!< 8 bit Packed A8B8G8R8. This is a word-ordered format where a pixel is represented by a 32-bit word with R in the lowest 8 bits, G in the next 8 bits, B in the 8 bits after that and A in the highest 8 bits.
104-
BF_ABGR10 = 0x20000000, //!< 10 bit Packed A2B10G10R10. This is a word-ordered format where a pixel is represented by a 32-bit word with R in the lowest 10 bits, G in the next 10 bits, B in the 10 bits after that and A in the highest 2 bits.
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.
10593
};
10694

10795
/** @brief Rate Control Modes.
@@ -233,7 +221,7 @@ class CV_EXPORTS_W VideoWriter
233221

234222
/** @brief Waits until the encoding process has finished before calling EncoderCallback::onEncodingFinished().
235223
*/
236-
CV_WRAP virtual void close() = 0;
224+
CV_WRAP virtual void release() = 0;
237225
};
238226

239227
/** @brief Creates video writer.
@@ -245,20 +233,8 @@ class CV_EXPORTS_W VideoWriter
245233
@param colorFormat OpenCv color format of the frames to be encoded.
246234
@param stream Stream for frame pre-processing.
247235
*/
248-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const VideoWriterCodec codec = VideoWriterCodec::H264,
249-
const double fps = 25.0, const COLOR_FORMAT_CV colorFormat = BGR, const Stream& stream = Stream::Null());
250-
251-
/** @brief Creates video writer.
252-
253-
@param fileName Name of the output video file. Only raw h264 or hevc files are supported.
254-
@param frameSize Size of the input video frames.
255-
@param codec Codec.
256-
@param fps Framerate of the created video stream.
257-
@param bufferFormat Nvidia Video Codec SDK buffer format of the frames to be encoded.
258-
@param stream Stream for frame pre-processing.
259-
*/
260-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const VideoWriterCodec codec,
261-
const double fps, const ENC_BUFFER_FORMAT bufferFormat, const Stream& stream = Stream::Null());
236+
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const CODEC_VW codec = CODEC_VW::H264,
237+
const double fps = 25.0, const COLOR_FORMAT_VW colorFormat = BGR, const Stream& stream = Stream::Null());
262238

263239
/** @brief Creates video writer.
264240
@@ -270,22 +246,8 @@ CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileNam
270246
@param params Additional encoding parameters.
271247
@param stream Stream for frame pre-processing.
272248
*/
273-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const VideoWriterCodec codec,
274-
const double fps, const COLOR_FORMAT_CV colorFormat, const EncoderParams& params, const Stream& stream = Stream::Null());
275-
276-
277-
/** @brief Creates video writer.
278-
279-
@param fileName Name of the output video file. Only raw h264 or hevc files are supported.
280-
@param frameSize Size of the input video frames.
281-
@param codec Codec.
282-
@param fps Framerate of the created video stream.
283-
@param bufferFormat Nvidia Video Codec SDK buffer format of the frames to be encoded.
284-
@param params Additional encoding parameters.
285-
@param stream Stream for frame pre-processing.
286-
*/
287-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const VideoWriterCodec codec,
288-
const double fps, const ENC_BUFFER_FORMAT bufferFormat, const EncoderParams& params, const Stream& stream = Stream::Null());
249+
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileName, const Size frameSize, const CODEC_VW codec,
250+
const double fps, const COLOR_FORMAT_VW colorFormat, const EncoderParams& params, const Stream& stream = Stream::Null());
289251

290252
/** @brief Creates video writer.
291253
@@ -298,22 +260,8 @@ CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const String& fileNam
298260
299261
The constructors initialize video writer. User can implement their own multiplexing with cudacodec::EncoderCallback.
300262
*/
301-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const Ptr<EncoderCallback>& encoderCallback, const Size frameSize, const VideoWriterCodec codec = VideoWriterCodec::H264,
302-
const double fps = 25.0, const COLOR_FORMAT_CV colorFormat = BGR, const Stream& stream = Stream::Null());
303-
304-
/** @brief Creates video writer.
305-
306-
@param encoderCallback Callbacks for video encoder. See cudacodec::EncoderCallback . Use it if you want to work with the raw video stream.
307-
@param frameSize Size of the input video frames.
308-
@param codec Codec.
309-
@param fps Framerate of the created video stream.
310-
@param bufferFormat Nvidia Video Codec SDK buffer format of the frames to be encoded.
311-
@param stream Stream for frame pre-processing.
312-
313-
The constructors initialize video writer. User can implement their own multiplexing with cudacodec::EncoderCallback.
314-
*/
315-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const Ptr<EncoderCallback>& encoderCallback, const Size frameSize, const VideoWriterCodec codec,
316-
const double fps, const ENC_BUFFER_FORMAT bufferFormat, const Stream& stream = Stream::Null());
263+
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const Ptr<EncoderCallback>& encoderCallback, const Size frameSize, const CODEC_VW codec = CODEC_VW::H264,
264+
const double fps = 25.0, const COLOR_FORMAT_VW colorFormat = BGR, const Stream& stream = Stream::Null());
317265

318266
/** @brief Creates video writer.
319267
@@ -327,23 +275,8 @@ CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const Ptr<EncoderCall
327275
328276
The constructors initialize video writer. User can implement their own multiplexing with cudacodec::EncoderCallback.
329277
*/
330-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const Ptr<EncoderCallback>& encoderCallback, const Size frameSize, const VideoWriterCodec codec,
331-
const double fps, const COLOR_FORMAT_CV colorFormat, const EncoderParams& params, const Stream& stream = Stream::Null());
332-
333-
/** @brief Creates video writer.
334-
335-
@param encoderCallback Callbacks for video encoder. See cudacodec::EncoderCallback . Use it if you want to work with the raw video stream.
336-
@param frameSize Size of the input video frames.
337-
@param codec Codec.
338-
@param fps Framerate of the created video stream.
339-
@param bufferFormat Nvidia Video Codec SDK buffer format of the frames to be encoded.
340-
@param params Additional encoding parameters.
341-
@param stream Stream for frame pre-processing.
342-
343-
The constructors initialize video writer. User can implement own their multiplexing with cudacodec::EncoderCallback.
344-
*/
345-
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const Ptr<EncoderCallback>& encoderCallback, const Size frameSize, const VideoWriterCodec codec,
346-
const double fps, const ENC_BUFFER_FORMAT bufferFormat, const EncoderParams& params, const Stream& stream = Stream::Null());
278+
CV_EXPORTS_W Ptr<cudacodec::VideoWriter> createVideoWriter(const Ptr<EncoderCallback>& encoderCallback, const Size frameSize, const CODEC_VW codec,
279+
const double fps, const COLOR_FORMAT_VW colorFormat, const EncoderParams& params, const Stream& stream = Stream::Null());
347280

348281
////////////////////////////////// Video Decoding //////////////////////////////////////////
349282

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def test_writer_existence(self):
7979
#Test at least the existence of wrapped functions for now
8080

8181
try:
82-
_, fname = tempfile.mkstemp(suffix=".h264")
82+
fd, fname = tempfile.mkstemp(suffix=".h264")
83+
os.close(fd)
8384
encoder_params_in = cv.cudacodec.EncoderParams()
8485
encoder_params_in.gopLength = 10
8586
stream = cv.cuda.Stream()
@@ -99,5 +100,7 @@ def test_writer_existence(self):
99100
self.assertEqual(e.code, cv.Error.StsNotImplemented)
100101
self.skipTest("Either NVCUVENC or a GPU hardware encoder is missing or the encoding profile is not supported.")
101102

103+
os.remove(fname)
104+
102105
if __name__ == '__main__':
103106
NewOpenCVTests.bootstrap()

modules/cudacodec/perf/perf_video.cpp

Lines changed: 17 additions & 13 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_CV, cv::cudacodec::VideoWriterCodec);
100+
DEF_PARAM_TEST(WriteToFile, string, cv::cudacodec::COLOR_FORMAT_VW, cv::cudacodec::CODEC_VW);
101101

102-
#define COLOR_FORMAT Values(cv::cudacodec::COLOR_FORMAT_CV::BGR, cv::cudacodec::COLOR_FORMAT_CV::RGB, cv::cudacodec::COLOR_FORMAT_CV::BGRA, \
103-
cv::cudacodec::COLOR_FORMAT_CV::RGBA, cv::cudacodec::COLOR_FORMAT_CV::GRAY)
104-
#define CODEC Values(cv::cudacodec::VideoWriterCodec::H264, cv::cudacodec::VideoWriterCodec::HEVC)
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)
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_CV surfaceFormat = GET_PARAM(1);
111-
const cudacodec::VideoWriterCodec codec = GET_PARAM(2);
110+
const cv::cudacodec::COLOR_FORMAT_VW surfaceFormat = GET_PARAM(1);
111+
const cudacodec::CODEC_VW 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::VideoWriterCodec::H264 ? ".h264" : ".hevc";
118+
const std::string ext = codec == cudacodec::CODEC_VW::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_CV::RGB:
125+
case cudacodec::COLOR_FORMAT_VW::RGB:
126126
conversionCode = COLOR_BGR2RGB;
127127
break;
128-
case cudacodec::COLOR_FORMAT_CV::BGRA:
128+
case cudacodec::COLOR_FORMAT_VW::BGRA:
129129
conversionCode = COLOR_BGR2BGRA;
130130
break;
131-
case cudacodec::COLOR_FORMAT_CV::RGBA:
131+
case cudacodec::COLOR_FORMAT_VW::RGBA:
132132
conversionCode = COLOR_BGR2RGBA;
133133
break;
134-
case cudacodec::COLOR_FORMAT_CV::GRAY:
134+
case cudacodec::COLOR_FORMAT_VW::GRAY:
135135
conversionCode = COLOR_BGR2GRAY;
136136
default:
137137
break;
@@ -155,11 +155,13 @@ PERF_TEST_P(WriteToFile, VideoWriter, Combine(VIDEO_SRC, COLOR_FORMAT, CODEC))
155155
}
156156
startTimer();
157157
d_writer->write(frames[nFrames - 1]);
158-
d_writer->close();
158+
d_writer->release();
159159
stopTimer();
160+
161+
ASSERT_EQ(0, remove(outputFile.c_str()));
160162
}
161163
else {
162-
if (surfaceFormat != cv::cudacodec::COLOR_FORMAT_CV::BGR || codec != cv::cudacodec::VideoWriterCodec::H264)
164+
if (surfaceFormat != cv::cudacodec::COLOR_FORMAT_VW::BGR || codec != cv::cudacodec::CODEC_VW::H264)
163165
throw PerfSkipTestException();
164166
cv::VideoWriter writer;
165167
const string outputFile = cv::tempfile(".avi");
@@ -178,6 +180,8 @@ PERF_TEST_P(WriteToFile, VideoWriter, Combine(VIDEO_SRC, COLOR_FORMAT, CODEC))
178180
writer.write(frameBgr);
179181
writer.release();
180182
stopTimer();
183+
184+
ASSERT_EQ(0, remove(outputFile.c_str()));
181185
}
182186
SANITY_CHECK(frameBgr);
183187
}

0 commit comments

Comments
 (0)