Skip to content

Commit 50c24db

Browse files
committed
Address cudacodec build warnings.
1 parent ea9f108 commit 50c24db

File tree

13 files changed

+177
-132
lines changed

13 files changed

+177
-132
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ struct CV_EXPORTS_W_SIMPLE FormatInfo
385385
*/
386386
class CV_EXPORTS_W NVSurfaceToColorConverter {
387387
public:
388+
virtual ~NVSurfaceToColorConverter() {}
389+
388390
/** @brief Performs the conversion from the raw YUV Surface output from VideoReader to the requested color format. Use this function when you want to convert the raw YUV Surface output from VideoReader to more than one color format or you want both the raw Surface output in addition to a color frame.
389391
* @param yuv The raw YUV Surface output from VideoReader see @ref SurfaceFormat.
390392
* @param color The converted frame.
@@ -551,8 +553,7 @@ class CV_EXPORTS_W VideoReader
551553
- Out: Value of the property.
552554
@return `true` unless the property is not supported.
553555
*/
554-
virtual bool get(const VideoReaderProps propertyId, double& propertyVal) const = 0;
555-
CV_WRAP virtual bool getVideoReaderProps(const VideoReaderProps propertyId, CV_OUT double& propertyValOut, double propertyValIn = 0) const = 0;
556+
CV_WRAP_AS(getVideoReaderProps) virtual bool get(const VideoReaderProps propertyId, CV_OUT size_t& propertyVal) const = 0;
556557

557558
/** @brief Retrieves the specified property used by the VideoSource.
558559
@@ -563,6 +564,43 @@ class CV_EXPORTS_W VideoReader
563564
@return `true` unless the property is unset set or not supported.
564565
*/
565566
CV_WRAP virtual bool get(const int propertyId, CV_OUT double& propertyVal) const = 0;
567+
568+
/** @brief Determine whether the raw package at \a idx contains encoded data for a key frame.
569+
570+
@param idx Index of the encoded package to check.
571+
572+
@returns `true` if the raw package at \a idx contains encoded data for a key frame and `false` otherwise.
573+
574+
@note A typical use case is deciding to archive live video after streaming has been initialized. In this scenario you would not want to write any encoded frames before a key frame. This is simulated in the example below where VideoReader is initialized without enabling raw mode
575+
```
576+
VideoReaderInitParams params;
577+
params.rawMode = false;
578+
Ptr<VideoReader> reader = createVideoReader(rtspUrl, {}, params);
579+
```
580+
and then at some point in the future raw mode is enabled to enable the footage to be archived
581+
```
582+
reader->set(VideoReaderProps::PROP_RAW_MODE, true);
583+
```
584+
Because raw mode has been enabled mid stream the first raw package retrieved now is not guaranteed to contain a key frame. To locate the next raw package containing a key frame rawPackageHasKeyFrame() can then be used as shown below.
585+
```
586+
double iRawPacketBase = -1;
587+
reader->get(VideoReaderProps::PROP_RAW_PACKAGES_BASE_INDEX, iRawPacketBase);
588+
GpuMat frame;
589+
while (reader->nextFrame(frame)) {
590+
double nRawPackets = -1;
591+
reader->get(VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB, nRawPackets);
592+
for (int iRawPacketToWrite = static_cast<int>(iRawPacketBase); iRawPacketToWrite < static_cast<int>(iRawPacketBase + nRawPackets); iRawPacketToWrite++) {
593+
if (reader->rawPackageHasKeyFrame(iRawPacketToWrite)) {
594+
Mat packageToWrite;
595+
reader->retrieve(packageToWrite, iRawPacketToWrite);
596+
...
597+
}
598+
}
599+
}
600+
```
601+
\sa retrieve
602+
*/
603+
CV_WRAP virtual bool rawPackageHasKeyFrame(const size_t idx) const = 0;
566604
};
567605

568606
/** @brief Interface for video demultiplexing. :

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,37 @@ def test_reader(self):
4848
ret, raw_mode = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_RAW_MODE)
4949
self.assertTrue(ret and raw_mode)
5050

51-
# Retrieve image histogram. Not all GPUs support histogram. Just check the method is called correctly
52-
ret, gpu_mat, hist = reader.nextFrameWithHist()
53-
self.assertTrue(ret and not gpu_mat.empty())
54-
ret, gpu_mat_, hist_ = reader.nextFrameWithHist(gpu_mat, hist)
55-
self.assertTrue(ret and not gpu_mat.empty())
56-
self.assertTrue(gpu_mat_.cudaPtr() == gpu_mat.cudaPtr())
51+
# Read raw encoded bitstream
52+
ret, i_base = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_RAW_PACKAGES_BASE_INDEX)
53+
self.assertTrue(ret and i_base == 2.0)
54+
ret, gpu_mat_2 = reader.nextFrame()
55+
self.assertTrue(ret and isinstance(gpu_mat_2,cv.cuda.GpuMat) and not gpu_mat_2.empty())
56+
ret = reader.retrieve(gpu_mat_2)
57+
self.assertTrue(ret and isinstance(gpu_mat_2,cv.cuda.GpuMat) and not gpu_mat_2.empty())
58+
ret, n_raw_packages_since_last_grab = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
59+
self.assertTrue(ret and n_raw_packages_since_last_grab > 0)
60+
self.assertTrue(reader.rawPackageHasKeyFrame(int(i_base)))
61+
ret, raw_data = reader.retrieve(int(i_base))
62+
self.assertTrue(ret and isinstance(raw_data,np.ndarray) and np.any(raw_data))
5763

5864
# Check post processing applied
59-
self.assertTrue(gpu_mat.size() == post_processed_sz)
65+
self.assertTrue(gpu_mat_2.size() == post_processed_sz)
66+
67+
# Retrieve image histogram. Not all GPUs support histogram. Just check the method is called correctly
68+
ret, gpu_mat_3, hist = reader.nextFrameWithHist()
69+
self.assertTrue(ret and not gpu_mat_3.empty())
70+
ret, gpu_mat_3_, hist_ = reader.nextFrameWithHist(gpu_mat_3, hist)
71+
self.assertTrue(ret and not gpu_mat_3.empty())
72+
self.assertTrue(gpu_mat_3_.cudaPtr() == gpu_mat_3.cudaPtr())
6073

6174
# Change color format
6275
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
76+
self.assertTrue(ret and colour_code == cv.cudacodec.BGRA)
77+
colour_code_gs = cv.cudacodec.GRAY
6578
reader.set(colour_code_gs)
6679
ret, colour_code = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_COLOR_FORMAT)
6780
self.assertTrue(ret and colour_code == colour_code_gs)
6881

69-
# Read raw encoded bitstream
70-
ret, i_base = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_RAW_PACKAGES_BASE_INDEX)
71-
self.assertTrue(ret and i_base == 2.0)
72-
self.assertTrue(reader.grab())
73-
ret, gpu_mat3 = reader.retrieve()
74-
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
75-
ret = reader.retrieve(gpu_mat3)
76-
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
77-
ret, n_raw_packages_since_last_grab = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
78-
self.assertTrue(ret and n_raw_packages_since_last_grab > 0)
79-
ret, raw_data = reader.retrieve(int(i_base))
80-
self.assertTrue(ret and isinstance(raw_data,np.ndarray) and np.any(raw_data))
81-
8282
except cv.error as e:
8383
notSupported = (e.code == cv.Error.StsNotImplemented or e.code == cv.Error.StsUnsupportedFormat or e.code == cv.Error.GPU_API_CALL_ERROR)
8484
self.assertTrue(notSupported)
@@ -107,7 +107,7 @@ def test_writer(self):
107107
encoder_params_in.gopLength = 10
108108
stream = cv.cuda.Stream()
109109
sz = (1920,1080)
110-
writer = cv.cudacodec.createVideoWriter(fname, sz, cv.cudacodec.H264, 30, cv.cudacodec.ColorFormat_BGR,
110+
writer = cv.cudacodec.createVideoWriter(fname, sz, cv.cudacodec.H264, 30, cv.cudacodec.BGR,
111111
encoder_params_in, stream=stream)
112112
blankFrameIn = cv.cuda.GpuMat(sz,cv.CV_8UC3)
113113
writer.write(blankFrameIn)

modules/cudacodec/src/NvEncoder.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,9 @@ void NvEncoder::CreateDefaultEncoderParams(NV_ENC_INITIALIZE_PARAMS* pIntializeP
101101
#endif
102102
pIntializeParams->tuningInfo = tuningInfo;
103103
pIntializeParams->encodeConfig->rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
104-
#if ((NVENCAPI_MAJOR_VERSION == 12 && NVENCAPI_MINOR_VERSION >= 2) || NVENCAPI_MAJOR_VERSION > 12)
105-
NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, 0, { NV_ENC_CONFIG_VER } };
106-
#else
107-
NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } };
108-
#endif
104+
NV_ENC_PRESET_CONFIG presetConfig = {};
105+
presetConfig.version = NV_ENC_PRESET_CONFIG_VER;
106+
presetConfig.presetCfg.version = NV_ENC_CONFIG_VER;
109107
m_nvenc.nvEncGetEncodePresetConfigEx(m_hEncoder, codecGuid, presetGuid, tuningInfo, &presetConfig);
110108
memcpy(pIntializeParams->encodeConfig, &presetConfig.presetCfg, sizeof(NV_ENC_CONFIG));
111109

@@ -205,11 +203,9 @@ void NvEncoder::CreateEncoder(const NV_ENC_INITIALIZE_PARAMS* pEncoderParams)
205203
}
206204
else
207205
{
208-
#if ((NVENCAPI_MAJOR_VERSION == 12 && NVENCAPI_MINOR_VERSION >= 2) || NVENCAPI_MAJOR_VERSION > 12)
209-
NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, 0, { NV_ENC_CONFIG_VER } };
210-
#else
211-
NV_ENC_PRESET_CONFIG presetConfig = { NV_ENC_PRESET_CONFIG_VER, { NV_ENC_CONFIG_VER } };
212-
#endif
206+
NV_ENC_PRESET_CONFIG presetConfig = {};
207+
presetConfig.version = NV_ENC_PRESET_CONFIG_VER;
208+
presetConfig.presetCfg.version = NV_ENC_CONFIG_VER;
213209
m_nvenc.nvEncGetEncodePresetConfigEx(m_hEncoder, pEncoderParams->encodeGUID, pEncoderParams->presetGUID, pEncoderParams->tuningInfo, &presetConfig);
214210
memcpy(&m_encodeConfig, &presetConfig.presetCfg, sizeof(NV_ENC_CONFIG));
215211
}
@@ -570,6 +566,8 @@ void NvEncoder::WaitForCompletionEvent(int iEvent)
570566
NVENC_THROW_ERROR("Failed to encode frame", NV_ENC_ERR_GENERIC);
571567
}
572568
#endif
569+
#else
570+
CV_UNUSED(iEvent);
573571
#endif
574572
}
575573

modules/cudacodec/src/NvEncoder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ class NvEncoder
263263
/**
264264
* @brief This function returns the completion event.
265265
*/
266-
void* GetCompletionEvent(uint32_t eventIdx) { return (m_vpCompletionEvent.size() == m_nEncoderBuffer) ? m_vpCompletionEvent[eventIdx] : nullptr; }
266+
void* GetCompletionEvent(uint32_t eventIdx) {
267+
CV_Assert(m_nEncoderBuffer >= 0);
268+
return (m_vpCompletionEvent.size() == static_cast<size_t>(m_nEncoderBuffer)) ? m_vpCompletionEvent[eventIdx] : nullptr;
269+
}
267270

268271
/**
269272
* @brief This function returns the current pixel format.

modules/cudacodec/src/cuda/ColorSpace.cu

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
namespace cv { namespace cuda { namespace device {
1010

11+
void SetMatYuv2Rgb(int iMatrix, bool fullRange = false);
12+
void Y8ToGray8(uint8_t* dpY8, int nY8Pitch, uint8_t* dpGray, int nGrayPitch, int nWidth, int nHeight, bool videoFullRangeFlag, const cudaStream_t stream);
13+
void Y8ToGray16(uint8_t* dpY8, int nY8Pitch, uint8_t* dpGray, int nGrayPitch, int nWidth, int nHeight, bool videoFullRangeFlag, const cudaStream_t stream);
14+
void Y16ToGray8(uint8_t* dpY16, int nY16Pitch, uint8_t* dpGray, int nGrayPitch, int nWidth, int nHeight, bool videoFullRangeFlag, const cudaStream_t stream);
15+
void Y16ToGray16(uint8_t* dpY16, int nY16Pitch, uint8_t* dpGray, int nGrayPitch, int nWidth, int nHeight, bool videoFullRangeFlag, const cudaStream_t stream);
1116
__constant__ float matYuv2Color[3][3];
1217

1318
void inline GetConstants(int iMatrix, float& wr, float& wb, int& black, int& white, int& uvWhite, int& max, bool fullRange = false) {
@@ -49,7 +54,7 @@ void inline GetConstants(int iMatrix, float& wr, float& wb, int& black, int& whi
4954
}
5055
}
5156

52-
void SetMatYuv2Rgb(int iMatrix, bool fullRange = false) {
57+
void SetMatYuv2Rgb(int iMatrix, bool fullRange) {
5358
float wr, wb;
5459
int black, white, max, uvWhite;
5560
GetConstants(iMatrix, wr, wb, black, white, uvWhite, max, fullRange);
@@ -160,15 +165,15 @@ __global__ static void YuvToColorKernel(uint8_t* pYuv, int nYuvPitch, uint8_t* p
160165

161166
union ColorOutx2 {
162167
Colorx2 d;
163-
Color Color[2];
168+
Color color[2];
164169
};
165170
ColorOutx2 l1Out;
166-
l1Out.Color[0] = YuvToColorForPixel<Color>(l0.x, ch.x, ch.y, videoFullRangeFlag);
167-
l1Out.Color[1] = YuvToColorForPixel<Color>(l0.y, ch.x, ch.y, videoFullRangeFlag);
171+
l1Out.color[0] = YuvToColorForPixel<Color>(l0.x, ch.x, ch.y, videoFullRangeFlag);
172+
l1Out.color[1] = YuvToColorForPixel<Color>(l0.y, ch.x, ch.y, videoFullRangeFlag);
168173
*(Colorx2*)pDst = l1Out.d;
169174
ColorOutx2 l2Out;
170-
l2Out.Color[0] = YuvToColorForPixel<Color>(l1.x, ch.x, ch.y, videoFullRangeFlag);
171-
l2Out.Color[1] = YuvToColorForPixel<Color>(l1.y, ch.x, ch.y, videoFullRangeFlag);
175+
l2Out.color[0] = YuvToColorForPixel<Color>(l1.x, ch.x, ch.y, videoFullRangeFlag);
176+
l2Out.color[1] = YuvToColorForPixel<Color>(l1.y, ch.x, ch.y, videoFullRangeFlag);
172177
*(Colorx2*)(pDst + nColorPitch) = l2Out.d;
173178
}
174179

@@ -214,11 +219,11 @@ __global__ static void Yuv444ToColorKernel(uint8_t* pYuv, int nYuvPitch, uint8_t
214219

215220
union ColorOutx2 {
216221
Colorx2 d;
217-
Color Color[2];
222+
Color color[2];
218223
};
219224
ColorOutx2 out;
220-
out.Color[0] = YuvToColorForPixel<Color>(l0.x, ch1.x, ch2.x, videoFullRangeFlag);
221-
out.Color[1] = YuvToColorForPixel<Color>(l0.y, ch1.y, ch2.y, videoFullRangeFlag);
225+
out.color[0] = YuvToColorForPixel<Color>(l0.x, ch1.x, ch2.x, videoFullRangeFlag);
226+
out.color[1] = YuvToColorForPixel<Color>(l0.y, ch1.y, ch2.y, videoFullRangeFlag);
222227
*(Colorx2*)pDst = out.d;
223228
}
224229

modules/cudacodec/src/cuvid_video_source.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ cv::cudacodec::detail::CuvidVideoSource::CuvidVideoSource(const String& fname)
6868
CUVIDEOFORMAT vidfmt;
6969
cuSafeCall( cuvidGetSourceVideoFormat(videoSource_, &vidfmt, 0) );
7070

71-
CV_Assert(Codec::NumCodecs == cudaVideoCodec::cudaVideoCodec_NumCodecs);
71+
CV_Assert(static_cast<int>(Codec::NumCodecs) == static_cast<int>(cudaVideoCodec::cudaVideoCodec_NumCodecs));
7272
format_.codec = static_cast<Codec>(vidfmt.codec);
7373
format_.chromaFormat = static_cast<ChromaFormat>(vidfmt.chroma_format);
7474
format_.nBitDepthMinus8 = vidfmt.bit_depth_luma_minus8;

modules/cudacodec/src/cuvid_video_source.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CuvidVideoSource : public VideoSource
5656

5757
FormatInfo format() const CV_OVERRIDE;
5858
void updateFormat(const FormatInfo& videoFormat) CV_OVERRIDE;
59+
bool get(const int, double&) const { return false; }
5960
void start() CV_OVERRIDE;
6061
void stop() CV_OVERRIDE;
6162
bool isStarted() const CV_OVERRIDE;

modules/cudacodec/src/ffmpeg_video_source.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Codec FourccToCodec(int codec)
9090
}
9191

9292
static
93-
int StartCodeLen(unsigned char* data, const int sz) {
93+
int StartCodeLen(unsigned char* data, const size_t sz) {
9494
if (sz >= 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
9595
return 3;
9696
else if (sz >= 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1)
@@ -99,7 +99,8 @@ int StartCodeLen(unsigned char* data, const int sz) {
9999
return 0;
100100
}
101101

102-
bool ParamSetsExist(unsigned char* parameterSets, const int szParameterSets, unsigned char* data, const int szData) {
102+
static
103+
bool ParamSetsExist(unsigned char* parameterSets, const size_t szParameterSets, unsigned char* data, const size_t szData) {
103104
const int paramSetStartCodeLen = StartCodeLen(parameterSets, szParameterSets);
104105
const int packetStartCodeLen = StartCodeLen(data, szData);
105106
// weak test to see if the parameter set has already been included in the RTP stream
@@ -129,8 +130,8 @@ cv::cudacodec::detail::FFmpegVideoSource::FFmpegVideoSource(const String& fname,
129130

130131
int codec = (int)cap.get(CAP_PROP_FOURCC);
131132
format_.codec = FourccToCodec(codec);
132-
format_.height = cap.get(CAP_PROP_FRAME_HEIGHT);
133-
format_.width = cap.get(CAP_PROP_FRAME_WIDTH);
133+
format_.height = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT));
134+
format_.width = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH));
134135
format_.displayArea = Rect(0, 0, format_.width, format_.height);
135136
format_.valid = false;
136137
format_.fps = cap.get(CAP_PROP_FPS);
@@ -181,7 +182,8 @@ bool cv::cudacodec::detail::FFmpegVideoSource::getNextPacket(unsigned char** dat
181182
{
182183
const size_t nBytesToTrimFromData = format_.codec == Codec::MPEG4 ? 3 : 0;
183184
const size_t newSz = extraData.total() + *size - nBytesToTrimFromData;
184-
dataWithHeader = Mat(1, newSz, CV_8UC1);
185+
CV_Assert(newSz <= std::numeric_limits<int>::max());
186+
dataWithHeader = Mat(1, static_cast<int>(newSz), CV_8UC1);
185187
memcpy(dataWithHeader.data, extraData.data, extraData.total());
186188
memcpy(dataWithHeader.data + extraData.total(), (*data) + nBytesToTrimFromData, *size - nBytesToTrimFromData);
187189
*data = dataWithHeader.data;

0 commit comments

Comments
 (0)