Skip to content

Commit 50c79b1

Browse files
committed
cudacodec: Address build warnings
1 parent faa5468 commit 50c79b1

File tree

13 files changed

+137
-96
lines changed

13 files changed

+137
-96
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,6 @@ enum class VideoReaderProps {
352352
PROP_RAW_PACKAGES_BASE_INDEX = 2, //!< Base index for retrieving raw encoded data using retrieve().
353353
PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB = 3, //!< Number of raw packages recieved since the last call to grab().
354354
PROP_RAW_MODE = 4, //!< Status of raw mode.
355-
PROP_LRF_HAS_KEY_FRAME = 5, //!< FFmpeg source only - Indicates whether the Last Raw Frame (LRF), output from VideoReader::retrieve() when VideoReader is initialized in raw mode, contains encoded data for a key frame.
356355
PROP_COLOR_FORMAT = 6, //!< Set the ColorFormat of the decoded frame. This can be changed before every call to nextFrame() and retrieve().
357356
PROP_UDP_SOURCE = 7, //!< Status of VideoReaderInitParams::udpSource initialization.
358357
PROP_ALLOW_FRAME_DROP = 8, //!< Status of VideoReaderInitParams::allowFrameDrop initialization.
@@ -487,8 +486,7 @@ class CV_EXPORTS_W VideoReader
487486
- Out: Value of the property.
488487
@return `true` unless the property is not supported.
489488
*/
490-
virtual bool get(const VideoReaderProps propertyId, double& propertyVal) const = 0;
491-
CV_WRAP virtual bool getVideoReaderProps(const VideoReaderProps propertyId, CV_OUT double& propertyValOut, double propertyValIn = 0) const = 0;
489+
CV_WRAP_AS(getVideoReaderProps) virtual bool get(const VideoReaderProps propertyId, CV_OUT size_t& propertyVal) const = 0;
492490

493491
/** @brief Retrieves the specified property used by the VideoSource.
494492
@@ -499,6 +497,43 @@ class CV_EXPORTS_W VideoReader
499497
@return `true` unless the property is unset set or not supported.
500498
*/
501499
CV_WRAP virtual bool get(const int propertyId, CV_OUT double& propertyVal) const = 0;
500+
501+
/** @brief Determine whether the raw package at \a idx contains encoded data for a key frame.
502+
503+
@param idx Index of the encoded package to check.
504+
505+
@returns `true` if the raw package at \a idx contains encoded data for a key frame and `false` otherwise.
506+
507+
@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
508+
```
509+
VideoReaderInitParams params;
510+
params.rawMode = false;
511+
Ptr<VideoReader> reader = createVideoReader(rtspUrl, {}, params);
512+
```
513+
and then at some point in the future raw mode is enabled to enable the footage to be archived
514+
```
515+
reader->set(VideoReaderProps::PROP_RAW_MODE, true);
516+
```
517+
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.
518+
```
519+
double iRawPacketBase = -1;
520+
reader->get(VideoReaderProps::PROP_RAW_PACKAGES_BASE_INDEX, iRawPacketBase);
521+
GpuMat frame;
522+
while (reader->nextFrame(frame)) {
523+
double nRawPackets = -1;
524+
reader->get(VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB, nRawPackets);
525+
for (int iRawPacketToWrite = static_cast<int>(iRawPacketBase); iRawPacketToWrite < static_cast<int>(iRawPacketBase + nRawPackets); iRawPacketToWrite++) {
526+
if (reader->rawPackageHasKeyFrame(iRawPacketToWrite)) {
527+
Mat packageToWrite;
528+
reader->retrieve(packageToWrite, iRawPacketToWrite);
529+
...
530+
}
531+
}
532+
}
533+
```
534+
\sa retrieve
535+
*/
536+
CV_WRAP virtual bool rawPackageHasKeyFrame(const size_t idx) const = 0;
502537
};
503538

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ def test_reader(self):
6969
# Read raw encoded bitstream
7070
ret, i_base = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_RAW_PACKAGES_BASE_INDEX)
7171
self.assertTrue(ret and i_base == 2.0)
72-
self.assertTrue(reader.grab())
73-
ret, gpu_mat3 = reader.retrieve()
72+
ret, gpu_mat3 = reader.nextFrame()
7473
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
7574
ret = reader.retrieve(gpu_mat3)
7675
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
7776
ret, n_raw_packages_since_last_grab = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
7877
self.assertTrue(ret and n_raw_packages_since_last_grab > 0)
78+
self.assertTrue(reader.rawPackageHasKeyFrame(int(i_base)))
7979
ret, raw_data = reader.retrieve(int(i_base))
8080
self.assertTrue(ret and isinstance(raw_data,np.ndarray) and np.any(raw_data))
8181

modules/cudacodec/src/NvEncoder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ void NvEncoder::WaitForCompletionEvent(int iEvent)
546546
NVENC_THROW_ERROR("Failed to encode frame", NV_ENC_ERR_GENERIC);
547547
}
548548
#endif
549+
#else
550+
CV_UNUSED(iEvent);
549551
#endif
550552
}
551553

@@ -772,4 +774,4 @@ void NvEncoder::DestroyBitstreamBuffer()
772774
m_vBitstreamOutputBuffer.clear();
773775
}
774776
}}
775-
#endif
777+
#endif

modules/cudacodec/src/NvEncoder.h

Lines changed: 5 additions & 2 deletions
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.
@@ -385,4 +388,4 @@ class NvEncoder
385388
std::vector<NV_ENC_OUTPUT_PTR> m_vBitstreamOutputBuffer;
386389
};
387390
}}
388-
#endif
391+
#endif

modules/cudacodec/src/cuda/nv12_to_rgb.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
using namespace cv;
6161
using namespace cv::cudev;
6262

63-
void nv12ToBgra(const GpuMat& decodedFrame, GpuMat& outFrame, int width, int height, cudaStream_t stream);
63+
void nv12ToBgra(const GpuMat& decodedFrame, GpuMat& outFrame, int width, int height, const bool videoFullRangeFlag, cudaStream_t stream);
6464

6565
namespace
6666
{
@@ -95,7 +95,7 @@ namespace
9595
(chromaCr * constHueColorSpaceMat[8]);
9696
}
9797

98-
__device__ static uint RGBA_pack_10bit(float red, float green, float blue, uint alpha)
98+
__device__ __forceinline__ uint RGBA_pack_10bit(float red, float green, float blue, uint alpha)
9999
{
100100
uint ARGBpixel = 0;
101101

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: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void FourccToChromaFormat(const int pixelFormat, ChromaFormat &chromaFormat, int
153153
}
154154

155155
static
156-
int StartCodeLen(unsigned char* data, const int sz) {
156+
int StartCodeLen(unsigned char* data, const size_t sz) {
157157
if (sz >= 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
158158
return 3;
159159
else if (sz >= 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1)
@@ -162,7 +162,8 @@ int StartCodeLen(unsigned char* data, const int sz) {
162162
return 0;
163163
}
164164

165-
bool ParamSetsExist(unsigned char* parameterSets, const int szParameterSets, unsigned char* data, const int szData) {
165+
static
166+
bool ParamSetsExist(unsigned char* parameterSets, const size_t szParameterSets, unsigned char* data, const size_t szData) {
166167
const int paramSetStartCodeLen = StartCodeLen(parameterSets, szParameterSets);
167168
const int packetStartCodeLen = StartCodeLen(data, szData);
168169
// weak test to see if the parameter set has already been included in the RTP stream
@@ -190,11 +191,11 @@ cv::cudacodec::detail::FFmpegVideoSource::FFmpegVideoSource(const String& fname,
190191
if (cap.retrieve(tmpExtraData, codecExtradataIndex) && tmpExtraData.total())
191192
extraData = tmpExtraData.clone();
192193

193-
int codec = (int)cap.get(CAP_PROP_FOURCC);
194-
int pixelFormat = (int)cap.get(CAP_PROP_CODEC_PIXEL_FORMAT);
194+
int codec = static_cast<int>(cap.get(CAP_PROP_FOURCC));
195+
int pixelFormat = static_cast<int>(cap.get(CAP_PROP_CODEC_PIXEL_FORMAT));
195196
format_.codec = FourccToCodec(codec);
196-
format_.height = cap.get(CAP_PROP_FRAME_HEIGHT);
197-
format_.width = cap.get(CAP_PROP_FRAME_WIDTH);
197+
format_.height = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT));
198+
format_.width = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH));
198199
format_.displayArea = Rect(0, 0, format_.width, format_.height);
199200
format_.valid = false;
200201
format_.fps = cap.get(CAP_PROP_FPS);
@@ -246,7 +247,8 @@ bool cv::cudacodec::detail::FFmpegVideoSource::getNextPacket(unsigned char** dat
246247
{
247248
const size_t nBytesToTrimFromData = format_.codec == Codec::MPEG4 ? 3 : 0;
248249
const size_t newSz = extraData.total() + *size - nBytesToTrimFromData;
249-
dataWithHeader = Mat(1, newSz, CV_8UC1);
250+
CV_Assert(newSz <= std::numeric_limits<int>::max());
251+
dataWithHeader = Mat(1, static_cast<int>(newSz), CV_8UC1);
250252
memcpy(dataWithHeader.data, extraData.data, extraData.total());
251253
memcpy(dataWithHeader.data + extraData.total(), (*data) + nBytesToTrimFromData, *size - nBytesToTrimFromData);
252254
*data = dataWithHeader.data;

modules/cudacodec/src/video_decoder.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
106106
codecSupported |= cudaVideoCodec_VP8 == _codec || cudaVideoCodec_VP9 == _codec;
107107
#endif
108108
#if (CUDART_VERSION >= 9000)
109-
codecSupported |= cudaVideoCodec_AV1;
109+
codecSupported |= cudaVideoCodec_AV1 == _codec;
110110
#endif
111111
CV_Assert(codecSupported);
112112
CV_Assert( cudaVideoChromaFormat_Monochrome == _chromaFormat ||
@@ -143,12 +143,12 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
143143
}
144144
}
145145

146-
CV_Assert(videoFormat.ulWidth >= decodeCaps.nMinWidth &&
147-
videoFormat.ulHeight >= decodeCaps.nMinHeight &&
148-
videoFormat.ulWidth <= decodeCaps.nMaxWidth &&
149-
videoFormat.ulHeight <= decodeCaps.nMaxHeight);
146+
CV_Assert(videoFormat.ulWidth >= static_cast<int>(decodeCaps.nMinWidth) &&
147+
videoFormat.ulHeight >= static_cast<int>(decodeCaps.nMinHeight) &&
148+
videoFormat.ulWidth <= static_cast<int>(decodeCaps.nMaxWidth) &&
149+
videoFormat.ulHeight <= static_cast<int>(decodeCaps.nMaxHeight));
150150

151-
CV_Assert((videoFormat.width >> 4) * (videoFormat.height >> 4) <= decodeCaps.nMaxMBCount);
151+
CV_Assert((static_cast<unsigned int>(videoFormat.width) >> 4) * (static_cast<unsigned int>(videoFormat.height) >> 4) <= decodeCaps.nMaxMBCount);
152152
#else
153153
if (videoFormat.enableHistogram) {
154154
CV_Error(Error::StsBadArg, "Luma histogram output is not supported when CUDA Toolkit version <= 9.0.");
@@ -172,14 +172,14 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
172172
createInfo_.DeinterlaceMode = static_cast<cudaVideoDeinterlaceMode>(videoFormat.deinterlaceMode);
173173
createInfo_.ulTargetWidth = videoFormat.width;
174174
createInfo_.ulTargetHeight = videoFormat.height;
175-
createInfo_.display_area.left = videoFormat.displayArea.x;
176-
createInfo_.display_area.right = videoFormat.displayArea.x + videoFormat.displayArea.width;
177-
createInfo_.display_area.top = videoFormat.displayArea.y;
178-
createInfo_.display_area.bottom = videoFormat.displayArea.y + videoFormat.displayArea.height;
179-
createInfo_.target_rect.left = videoFormat.targetRoi.x;
180-
createInfo_.target_rect.right = videoFormat.targetRoi.x + videoFormat.targetRoi.width;
181-
createInfo_.target_rect.top = videoFormat.targetRoi.y;
182-
createInfo_.target_rect.bottom = videoFormat.targetRoi.y + videoFormat.targetRoi.height;
175+
createInfo_.display_area.left = static_cast<short>(videoFormat.displayArea.x);
176+
createInfo_.display_area.right = static_cast<short>(videoFormat.displayArea.x + videoFormat.displayArea.width);
177+
createInfo_.display_area.top = static_cast<short>(videoFormat.displayArea.y);
178+
createInfo_.display_area.bottom = static_cast<short>(videoFormat.displayArea.y + videoFormat.displayArea.height);
179+
createInfo_.target_rect.left = static_cast<short>(videoFormat.targetRoi.x);
180+
createInfo_.target_rect.right = static_cast<short>(videoFormat.targetRoi.x + videoFormat.targetRoi.width);
181+
createInfo_.target_rect.top = static_cast<short>(videoFormat.targetRoi.y);
182+
createInfo_.target_rect.bottom = static_cast<short>(videoFormat.targetRoi.y + videoFormat.targetRoi.height);
183183
createInfo_.ulNumOutputSurfaces = 2;
184184
createInfo_.ulCreationFlags = videoCreateFlags;
185185
createInfo_.vidLock = lock_;
@@ -220,19 +220,19 @@ int cv::cudacodec::detail::VideoDecoder::reconfigure(const FormatInfo& videoForm
220220
videoFormat_.targetRoi = videoFormat.targetRoi;
221221
}
222222

223-
CUVIDRECONFIGUREDECODERINFO reconfigParams = { 0 };
223+
CUVIDRECONFIGUREDECODERINFO reconfigParams = {};
224224
reconfigParams.ulWidth = videoFormat_.ulWidth;
225225
reconfigParams.ulHeight = videoFormat_.ulHeight;
226-
reconfigParams.display_area.left = videoFormat_.displayArea.x;
227-
reconfigParams.display_area.right = videoFormat_.displayArea.x + videoFormat_.displayArea.width;
228-
reconfigParams.display_area.top = videoFormat_.displayArea.y;
229-
reconfigParams.display_area.bottom = videoFormat_.displayArea.y + videoFormat_.displayArea.height;
226+
reconfigParams.display_area.left = static_cast<short>(videoFormat_.displayArea.x);
227+
reconfigParams.display_area.right = static_cast<short>(videoFormat_.displayArea.x + videoFormat_.displayArea.width);
228+
reconfigParams.display_area.top = static_cast<short>(videoFormat_.displayArea.y);
229+
reconfigParams.display_area.bottom = static_cast<short>(videoFormat_.displayArea.y + videoFormat_.displayArea.height);
230230
reconfigParams.ulTargetWidth = videoFormat_.width;
231231
reconfigParams.ulTargetHeight = videoFormat_.height;
232-
reconfigParams.target_rect.left = videoFormat_.targetRoi.x;
233-
reconfigParams.target_rect.right = videoFormat_.targetRoi.x + videoFormat_.targetRoi.width;
234-
reconfigParams.target_rect.top = videoFormat_.targetRoi.y;
235-
reconfigParams.target_rect.bottom = videoFormat_.targetRoi.y + videoFormat_.targetRoi.height;
232+
reconfigParams.target_rect.left = static_cast<short>(videoFormat_.targetRoi.x);
233+
reconfigParams.target_rect.right = static_cast<short>(videoFormat_.targetRoi.x + videoFormat_.targetRoi.width);
234+
reconfigParams.target_rect.top = static_cast<short>(videoFormat_.targetRoi.y);
235+
reconfigParams.target_rect.bottom = static_cast<short>(videoFormat_.targetRoi.y + videoFormat_.targetRoi.height);
236236
reconfigParams.ulNumDecodeSurfaces = videoFormat_.ulNumDecodeSurfaces;
237237

238238
cuSafeCall(cuCtxPushCurrent(ctx_));

0 commit comments

Comments
 (0)