Skip to content

Commit 6d155cc

Browse files
committed
cudacodec: Address build warnings
1 parent f10c84d commit 6d155cc

File tree

13 files changed

+113
-73
lines changed

13 files changed

+113
-73
lines changed

modules/cudacodec/include/opencv2/cudacodec.hpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ enum class VideoReaderProps {
341341
PROP_RAW_PACKAGES_BASE_INDEX = 2, //!< Base index for retrieving raw encoded data using retrieve().
342342
PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB = 3, //!< Number of raw packages recieved since the last call to grab().
343343
PROP_RAW_MODE = 4, //!< Status of raw mode.
344-
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.
345344
PROP_COLOR_FORMAT = 6, //!< Set the ColorFormat of the decoded frame. This can be changed before every call to nextFrame() and retrieve().
346345
PROP_UDP_SOURCE = 7, //!< Status of VideoReaderInitParams::udpSource initialization.
347346
PROP_ALLOW_FRAME_DROP = 8, //!< Status of VideoReaderInitParams::allowFrameDrop initialization.
@@ -462,8 +461,7 @@ class CV_EXPORTS_W VideoReader
462461
- Out: Value of the property.
463462
@return `true` unless the property is not supported.
464463
*/
465-
virtual bool get(const VideoReaderProps propertyId, double& propertyVal) const = 0;
466-
CV_WRAP virtual bool getVideoReaderProps(const VideoReaderProps propertyId, CV_OUT double& propertyValOut, double propertyValIn = 0) const = 0;
464+
CV_WRAP_AS(getVideoReaderProps) virtual bool get(const VideoReaderProps propertyId, CV_OUT double& propertyVal) const = 0;
467465

468466
/** @brief Retrieves the specified property used by the VideoSource.
469467
@@ -474,6 +472,43 @@ class CV_EXPORTS_W VideoReader
474472
@return `true` unless the property is unset set or not supported.
475473
*/
476474
CV_WRAP virtual bool get(const int propertyId, CV_OUT double& propertyVal) const = 0;
475+
476+
/** @brief Determine whether the raw package at \a idx contains encoded data for a key frame.
477+
478+
@param idx Index of the encoded package to check.
479+
480+
@returns `true` if the raw package at \a idx contains encoded data for a key frame and `false` otherwise.
481+
482+
@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
483+
```
484+
VideoReaderInitParams params;
485+
params.rawMode = false;
486+
Ptr<VideoReader> reader = createVideoReader(rtspUrl, {}, params);
487+
```
488+
and then at some point in the future raw mode is enabled to enable the footage to be archived
489+
```
490+
reader->set(VideoReaderProps::PROP_RAW_MODE, true);
491+
```
492+
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.
493+
```
494+
double iRawPacketBase = -1;
495+
reader->get(VideoReaderProps::PROP_RAW_PACKAGES_BASE_INDEX, iRawPacketBase);
496+
GpuMat frame;
497+
while (reader->nextFrame(frame)) {
498+
double nRawPackets = -1;
499+
reader->get(VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB, nRawPackets);
500+
for (int iRawPacketToWrite = static_cast<int>(iRawPacketBase); iRawPacketToWrite < static_cast<int>(iRawPacketBase + nRawPackets); iRawPacketToWrite++) {
501+
if (reader->rawPackageHasKeyFrame(iRawPacketToWrite)) {
502+
Mat packageToWrite;
503+
reader->retrieve(packageToWrite, iRawPacketToWrite);
504+
...
505+
}
506+
}
507+
}
508+
```
509+
\sa retrieve
510+
*/
511+
CV_WRAP virtual bool rawPackageHasKeyFrame(const int idx) const = 0;
477512
};
478513

479514
/** @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
@@ -53,13 +53,13 @@ def test_reader(self):
5353

5454
ret, i_base = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_RAW_PACKAGES_BASE_INDEX)
5555
self.assertTrue(ret and i_base == 2.0)
56-
self.assertTrue(reader.grab())
57-
ret, gpu_mat3 = reader.retrieve()
56+
ret, gpu_mat3 = reader.nextFrame()
5857
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
5958
ret = reader.retrieve(gpu_mat3)
6059
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
6160
ret, n_raw_packages_since_last_grab = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
6261
self.assertTrue(ret and n_raw_packages_since_last_grab > 0)
62+
self.assertTrue(reader.rawPackageHasKeyFrame(int(i_base)))
6363
ret, raw_data = reader.retrieve(int(i_base))
6464
self.assertTrue(ret and isinstance(raw_data,np.ndarray) and np.any(raw_data))
6565

modules/cudacodec/src/NvEncoder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ void NvEncoder::WaitForCompletionEvent(int iEvent)
556556
NVENC_THROW_ERROR("Failed to encode frame", NV_ENC_ERR_GENERIC);
557557
}
558558
#endif
559+
#else
560+
CV_UNUSED(iEvent);
559561
#endif
560562
}
561563

@@ -782,4 +784,4 @@ void NvEncoder::DestroyBitstreamBuffer()
782784
m_vBitstreamOutputBuffer.clear();
783785
}
784786
}}
785-
#endif
787+
#endif

modules/cudacodec/src/NvEncoder.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ class NvEncoder
252252
/**
253253
* @brief This function returns the completion event.
254254
*/
255-
void* GetCompletionEvent(uint32_t eventIdx) { return (m_vpCompletionEvent.size() == m_nEncoderBuffer) ? m_vpCompletionEvent[eventIdx] : nullptr; }
255+
void* GetCompletionEvent(uint32_t eventIdx) {
256+
CV_Assert(m_nEncoderBuffer >= 0);
257+
return (m_vpCompletionEvent.size() == static_cast<size_t>(m_nEncoderBuffer)) ? m_vpCompletionEvent[eventIdx] : nullptr;
258+
}
256259

257260
/**
258261
* @brief This function returns the current pixel format.
@@ -374,4 +377,4 @@ class NvEncoder
374377
std::vector<NV_ENC_OUTPUT_PTR> m_vBitstreamOutputBuffer;
375378
};
376379
}}
377-
#endif
380+
#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
@@ -188,11 +189,11 @@ cv::cudacodec::detail::FFmpegVideoSource::FFmpegVideoSource(const String& fname,
188189
if (cap.retrieve(tmpExtraData, codecExtradataIndex) && tmpExtraData.total())
189190
extraData = tmpExtraData.clone();
190191

191-
int codec = (int)cap.get(CAP_PROP_FOURCC);
192-
int pixelFormat = (int)cap.get(CAP_PROP_CODEC_PIXEL_FORMAT);
192+
int codec = static_cast<int>(cap.get(CAP_PROP_FOURCC));
193+
int pixelFormat = static_cast<int>(cap.get(CAP_PROP_CODEC_PIXEL_FORMAT));
193194
format_.codec = FourccToCodec(codec);
194-
format_.height = cap.get(CAP_PROP_FRAME_HEIGHT);
195-
format_.width = cap.get(CAP_PROP_FRAME_WIDTH);
195+
format_.height = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT));
196+
format_.width = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH));
196197
format_.displayArea = Rect(0, 0, format_.width, format_.height);
197198
format_.valid = false;
198199
format_.fps = cap.get(CAP_PROP_FPS);
@@ -244,7 +245,8 @@ bool cv::cudacodec::detail::FFmpegVideoSource::getNextPacket(unsigned char** dat
244245
{
245246
const size_t nBytesToTrimFromData = format_.codec == Codec::MPEG4 ? 3 : 0;
246247
const size_t newSz = extraData.total() + *size - nBytesToTrimFromData;
247-
dataWithHeader = Mat(1, newSz, CV_8UC1);
248+
CV_Assert(newSz <= std::numeric_limits<int>::max());
249+
dataWithHeader = Mat(1, static_cast<int>(newSz), CV_8UC1);
248250
memcpy(dataWithHeader.data, extraData.data, extraData.total());
249251
memcpy(dataWithHeader.data + extraData.total(), (*data) + nBytesToTrimFromData, *size - nBytesToTrimFromData);
250252
*data = dataWithHeader.data;

modules/cudacodec/src/video_decoder.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
127127
CV_LOG_ERROR(NULL, "Video source is not supported by hardware video decoder.");
128128
CV_Error(Error::StsUnsupportedFormat, "Video source is not supported by hardware video decoder");
129129
}
130-
CV_Assert(videoFormat.ulWidth >= decodeCaps.nMinWidth &&
131-
videoFormat.ulHeight >= decodeCaps.nMinHeight &&
132-
videoFormat.ulWidth <= decodeCaps.nMaxWidth &&
133-
videoFormat.ulHeight <= decodeCaps.nMaxHeight);
130+
CV_Assert(videoFormat.ulWidth >= static_cast<int>(decodeCaps.nMinWidth) &&
131+
videoFormat.ulHeight >= static_cast<int>(decodeCaps.nMinHeight) &&
132+
videoFormat.ulWidth <= static_cast<int>(decodeCaps.nMaxWidth) &&
133+
videoFormat.ulHeight <= static_cast<int>(decodeCaps.nMaxHeight));
134134

135-
CV_Assert((videoFormat.width >> 4)* (videoFormat.height >> 4) <= decodeCaps.nMaxMBCount);
135+
CV_Assert((static_cast<unsigned int>(videoFormat.width) >> 4) * (static_cast<unsigned int>(videoFormat.height) >> 4) <= decodeCaps.nMaxMBCount);
136136
#endif
137137
// Create video decoder
138138
CUVIDDECODECREATEINFO createInfo_ = {};
@@ -148,14 +148,14 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
148148
createInfo_.ulTargetHeight = videoFormat.height;
149149
createInfo_.ulMaxWidth = videoFormat.ulMaxWidth;
150150
createInfo_.ulMaxHeight = videoFormat.ulMaxHeight;
151-
createInfo_.display_area.left = videoFormat.displayArea.x;
152-
createInfo_.display_area.right = videoFormat.displayArea.x + videoFormat.displayArea.width;
153-
createInfo_.display_area.top = videoFormat.displayArea.y;
154-
createInfo_.display_area.bottom = videoFormat.displayArea.y + videoFormat.displayArea.height;
155-
createInfo_.target_rect.left = videoFormat.targetRoi.x;
156-
createInfo_.target_rect.right = videoFormat.targetRoi.x + videoFormat.targetRoi.width;
157-
createInfo_.target_rect.top = videoFormat.targetRoi.y;
158-
createInfo_.target_rect.bottom = videoFormat.targetRoi.y + videoFormat.targetRoi.height;
151+
createInfo_.display_area.left = static_cast<short>(videoFormat.displayArea.x);
152+
createInfo_.display_area.right = static_cast<short>(videoFormat.displayArea.x + videoFormat.displayArea.width);
153+
createInfo_.display_area.top = static_cast<short>(videoFormat.displayArea.y);
154+
createInfo_.display_area.bottom = static_cast<short>(videoFormat.displayArea.y + videoFormat.displayArea.height);
155+
createInfo_.target_rect.left = static_cast<short>(videoFormat.targetRoi.x);
156+
createInfo_.target_rect.right = static_cast<short>(videoFormat.targetRoi.x + videoFormat.targetRoi.width);
157+
createInfo_.target_rect.top = static_cast<short>(videoFormat.targetRoi.y);
158+
createInfo_.target_rect.bottom = static_cast<short>(videoFormat.targetRoi.y + videoFormat.targetRoi.height);
159159
createInfo_.ulNumOutputSurfaces = 2;
160160
createInfo_.ulCreationFlags = videoCreateFlags;
161161
createInfo_.vidLock = lock_;
@@ -199,19 +199,19 @@ int cv::cudacodec::detail::VideoDecoder::reconfigure(const FormatInfo& videoForm
199199
videoFormat_.targetRoi = videoFormat.targetRoi;
200200
}
201201

202-
CUVIDRECONFIGUREDECODERINFO reconfigParams = { 0 };
202+
CUVIDRECONFIGUREDECODERINFO reconfigParams = {};
203203
reconfigParams.ulWidth = videoFormat_.ulWidth;
204204
reconfigParams.ulHeight = videoFormat_.ulHeight;
205-
reconfigParams.display_area.left = videoFormat_.displayArea.x;
206-
reconfigParams.display_area.right = videoFormat_.displayArea.x + videoFormat_.displayArea.width;
207-
reconfigParams.display_area.top = videoFormat_.displayArea.y;
208-
reconfigParams.display_area.bottom = videoFormat_.displayArea.y + videoFormat_.displayArea.height;
205+
reconfigParams.display_area.left = static_cast<short>(videoFormat_.displayArea.x);
206+
reconfigParams.display_area.right = static_cast<short>(videoFormat_.displayArea.x + videoFormat_.displayArea.width);
207+
reconfigParams.display_area.top = static_cast<short>(videoFormat_.displayArea.y);
208+
reconfigParams.display_area.bottom = static_cast<short>(videoFormat_.displayArea.y + videoFormat_.displayArea.height);
209209
reconfigParams.ulTargetWidth = videoFormat_.width;
210210
reconfigParams.ulTargetHeight = videoFormat_.height;
211-
reconfigParams.target_rect.left = videoFormat_.targetRoi.x;
212-
reconfigParams.target_rect.right = videoFormat_.targetRoi.x + videoFormat_.targetRoi.width;
213-
reconfigParams.target_rect.top = videoFormat_.targetRoi.y;
214-
reconfigParams.target_rect.bottom = videoFormat_.targetRoi.y + videoFormat_.targetRoi.height;
211+
reconfigParams.target_rect.left = static_cast<short>(videoFormat_.targetRoi.x);
212+
reconfigParams.target_rect.right = static_cast<short>(videoFormat_.targetRoi.x + videoFormat_.targetRoi.width);
213+
reconfigParams.target_rect.top = static_cast<short>(videoFormat_.targetRoi.y);
214+
reconfigParams.target_rect.bottom = static_cast<short>(videoFormat_.targetRoi.y + videoFormat_.targetRoi.height);
215215
reconfigParams.ulNumDecodeSurfaces = videoFormat_.ulNumDecodeSurfaces;
216216

217217
cuSafeCall(cuCtxPushCurrent(ctx_));

0 commit comments

Comments
 (0)