Skip to content

Commit 21e05d0

Browse files
committed
Expose NVSurfaceToColorConverter::convert to python and remove unecessary videoFullRangeFlag argument.
1 parent c4c74c4 commit 21e05d0

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,9 @@ class CV_EXPORTS_W NVSurfaceToColorConverter {
392392
* @param outputFormat The requested output color format.
393393
* @param bitDepth The requested bit depth of the output frame.
394394
* @param planar Request seperate planes for each color plane.
395-
* @param 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.
396395
* @param stream Stream for the asynchronous version.
397396
*/
398-
virtual bool convert(InputArray yuv, OutputArray color, const SurfaceFormat surfaceFormat, const ColorFormat outputFormat, const BitDepth bitDepth = BitDepth::UNCHANGED, const bool planar = false, const bool videoFullRangeFlag = false, cuda::Stream& stream = cuda::Stream::Null()) = 0;
397+
CV_WRAP virtual bool convert(InputArray yuv, OutputArray color, const SurfaceFormat surfaceFormat, const ColorFormat outputFormat, const BitDepth bitDepth = BitDepth::UNCHANGED, const bool planar = false, cuda::Stream& stream = cuda::Stream::Null()) = 0;
399398
};
400399

401400
/** @brief Creates a NVSurfaceToColorConverter.

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def test_reader(self):
6060

6161
# Change color format
6262
ret, colour_code = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_COLOR_FORMAT)
63-
self.assertTrue(ret and colour_code == cv.cudacodec.ColorFormat_BGRA)
64-
colour_code_gs = cv.cudacodec.ColorFormat_GRAY
63+
self.assertTrue(ret and colour_code == cv.cudacodec.BGRA)
64+
colour_code_gs = cv.cudacodec.GRAY
6565
reader.set(colour_code_gs)
6666
ret, colour_code = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_COLOR_FORMAT)
6767
self.assertTrue(ret and colour_code == colour_code_gs)
@@ -91,6 +91,14 @@ def test_reader(self):
9191
else:
9292
self.skipTest(e.err)
9393

94+
def test_NVSurfaceToColorConverter(self):
95+
converter = cv.cudacodec.createNVSurfaceToColorConverter(cv.cudacodec.ColorSpaceStandard_BT601,False)
96+
bgr_sz = (1920,1080)
97+
nv12_sz = (1920, int(1.5*1080))
98+
blank_nv12_frame = cv.cuda.GpuMat(nv12_sz,cv.CV_8U)
99+
ret, bgr = converter.convert(blank_nv12_frame, cv.cudacodec.SF_NV12, cv.cudacodec.BGR)
100+
self.assertTrue(ret == True and bgr.size() == bgr_sz)
101+
94102
def test_map_histogram(self):
95103
hist = cv.cuda_GpuMat((1,256), cv.CV_8UC1)
96104
hist.setTo(1)
@@ -107,7 +115,7 @@ def test_writer(self):
107115
encoder_params_in.gopLength = 10
108116
stream = cv.cuda.Stream()
109117
sz = (1920,1080)
110-
writer = cv.cudacodec.createVideoWriter(fname, sz, cv.cudacodec.H264, 30, cv.cudacodec.ColorFormat_BGR,
118+
writer = cv.cudacodec.createVideoWriter(fname, sz, cv.cudacodec.H264, 30, cv.cudacodec.BGR,
111119
encoder_params_in, stream=stream)
112120
blankFrameIn = cv.cuda.GpuMat(sz,cv.CV_8UC3)
113121
writer.write(blankFrameIn)

modules/cudacodec/src/nvidia_surface_format_to_color_converter.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ void SetMatYuv2Rgb(int iMatrix, bool);
9696
using namespace cuda::device;
9797
class NVSurfaceToColorConverterImpl : public NVSurfaceToColorConverter {
9898
public:
99-
NVSurfaceToColorConverterImpl(ColorSpaceStandard colorSpace, bool fullColorRange = false) {
99+
NVSurfaceToColorConverterImpl(ColorSpaceStandard colorSpace, bool fullColorRange = false) :
100+
videoFullRangeFlag(fullColorRange)
101+
{
100102
SetMatYuv2Rgb(static_cast<int>(colorSpace), fullColorRange);
101103
}
102104

@@ -136,7 +138,7 @@ class NVSurfaceToColorConverterImpl : public NVSurfaceToColorConverter {
136138
}
137139
}
138140

139-
bool convert(const InputArray yuv, const OutputArray out, const SurfaceFormat surfaceFormat, const ColorFormat outputFormat, const BitDepth bitDepth, const bool planar, const bool videoFullRangeFlag, cuda::Stream& stream) {
141+
bool convert(const InputArray yuv, const OutputArray out, const SurfaceFormat surfaceFormat, const ColorFormat outputFormat, const BitDepth bitDepth, const bool planar, cuda::Stream& stream) {
140142
CV_Assert(outputFormat == ColorFormat::BGR || outputFormat == ColorFormat::BGRA || outputFormat == ColorFormat::RGB || outputFormat == ColorFormat::RGBA || outputFormat == ColorFormat::GRAY);
141143
CV_Assert(yuv.depth() == CV_8U || yuv.depth() == CV_16U);
142144
const bool yuv420 = surfaceFormat == SurfaceFormat::SF_NV12 || surfaceFormat == SurfaceFormat::SF_P016;
@@ -281,7 +283,8 @@ class NVSurfaceToColorConverterImpl : public NVSurfaceToColorConverter {
281283
return true;
282284
}
283285

284-
286+
private:
287+
bool videoFullRangeFlag;
285288
};
286289

287290
Ptr<NVSurfaceToColorConverter> cv::cudacodec::createNVSurfaceToColorConverter(const ColorSpaceStandard colorSpace, const bool videoFullRangeFlag) {

modules/cudacodec/src/video_reader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace
9292
void releaseFrameInfo(const std::pair<CUVIDPARSERDISPINFO, CUVIDPROCPARAMS>& frameInfo);
9393
bool internalGrab(GpuMat & frame, GpuMat & histogram, Stream & stream);
9494
void waitForDecoderInit();
95-
void cvtFromYuv(const GpuMat& decodedFrame, GpuMat& outFrame, const SurfaceFormat surfaceFormat, const bool videoFullRangeFlag, Stream& stream);
95+
void cvtFromYuv(const GpuMat& decodedFrame, GpuMat& outFrame, const SurfaceFormat surfaceFormat, Stream& stream);
9696

9797
Ptr<VideoSource> videoSource_;
9898

@@ -260,7 +260,7 @@ namespace
260260
cuSafeCall(cuMemcpyDtoDAsync((CUdeviceptr)(histogram.data), cuHistogramPtr, histogramSz, StreamAccessor::getStream(stream)));
261261
}
262262

263-
cvtFromYuv(decodedFrame, frame, videoDecoder_->format().surfaceFormat, videoDecoder_->format().videoFullRangeFlag, stream);
263+
cvtFromYuv(decodedFrame, frame, videoDecoder_->format().surfaceFormat, stream);
264264
// unmap video frame
265265
// unmapFrame() synchronizes with the VideoDecode API (ensures the frame has finished decoding)
266266
videoDecoder_->unmapFrame(decodedFrame);
@@ -414,13 +414,13 @@ namespace
414414
return true;
415415
}
416416

417-
void VideoReaderImpl::cvtFromYuv(const GpuMat& decodedFrame, GpuMat& outFrame, const SurfaceFormat surfaceFormat, const bool videoFullRangeFlag, Stream& stream)
417+
void VideoReaderImpl::cvtFromYuv(const GpuMat& decodedFrame, GpuMat& outFrame, const SurfaceFormat surfaceFormat, Stream& stream)
418418
{
419419
if (colorFormat == ColorFormat::NV_YUV_SURFACE_FORMAT) {
420420
decodedFrame.copyTo(outFrame, stream);
421421
return;
422422
}
423-
yuvConverter->convert(decodedFrame, outFrame, surfaceFormat, colorFormat, bitDepth, planar, videoFullRangeFlag, stream);
423+
yuvConverter->convert(decodedFrame, outFrame, surfaceFormat, colorFormat, bitDepth, planar, stream);
424424
}
425425
}
426426

modules/cudacodec/test/test_video.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ CUDA_TEST_P(YuvConverter, Reader)
998998
Mat nv12Interleaved, bgrFromYuv;
999999
generateTestImages(bgr, nv12Interleaved, bgrFromYuv, surfaceFormat, outputFormat, bitDepth, planar, fullRange);
10001000
GpuMat nv12Device(nv12Interleaved), bgrDevice(bgrFromYuv.size(), bgrFromYuv.type());
1001-
yuvConverter->convert(nv12Device, bgrDevice, surfaceFormat, outputFormat, bitDepth, planar, fullRange);
1001+
yuvConverter->convert(nv12Device, bgrDevice, surfaceFormat, outputFormat, bitDepth, planar);
10021002
bgrDevice.download(bgrHost);
10031003
EXPECT_MAT_NEAR(bgrFromYuv, bgrHost, bitDepth == cudacodec::BitDepth::EIGHT ? 2 :512);
10041004
}

0 commit comments

Comments
 (0)