Skip to content

Commit

Permalink
cudacodec: Address build warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cudawarped committed Aug 2, 2023
1 parent daaf645 commit 720e0b8
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 95 deletions.
41 changes: 38 additions & 3 deletions modules/cudacodec/include/opencv2/cudacodec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ enum class VideoReaderProps {
PROP_RAW_PACKAGES_BASE_INDEX = 2, //!< Base index for retrieving raw encoded data using retrieve().
PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB = 3, //!< Number of raw packages recieved since the last call to grab().
PROP_RAW_MODE = 4, //!< Status of raw mode.
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.
PROP_COLOR_FORMAT = 6, //!< Set the ColorFormat of the decoded frame. This can be changed before every call to nextFrame() and retrieve().
PROP_UDP_SOURCE = 7, //!< Status of VideoReaderInitParams::udpSource initialization.
PROP_ALLOW_FRAME_DROP = 8, //!< Status of VideoReaderInitParams::allowFrameDrop initialization.
Expand Down Expand Up @@ -487,8 +486,7 @@ class CV_EXPORTS_W VideoReader
- Out: Value of the property.
@return `true` unless the property is not supported.
*/
virtual bool get(const VideoReaderProps propertyId, double& propertyVal) const = 0;
CV_WRAP virtual bool getVideoReaderProps(const VideoReaderProps propertyId, CV_OUT double& propertyValOut, double propertyValIn = 0) const = 0;
CV_WRAP_AS(getVideoReaderProps) virtual bool get(const VideoReaderProps propertyId, CV_OUT size_t& propertyVal) const = 0;

/** @brief Retrieves the specified property used by the VideoSource.
Expand All @@ -499,6 +497,43 @@ class CV_EXPORTS_W VideoReader
@return `true` unless the property is unset set or not supported.
*/
CV_WRAP virtual bool get(const int propertyId, CV_OUT double& propertyVal) const = 0;

/** @brief Determine whether the raw package at \a idx contains encoded data for a key frame.
@param idx Index of the encoded package to check.
@returns `true` if the raw package at \a idx contains encoded data for a key frame and `false` otherwise.
@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
```
VideoReaderInitParams params;
params.rawMode = false;
Ptr<VideoReader> reader = createVideoReader(rtspUrl, {}, params);
```
and then at some point in the future raw mode is enabled to enable the footage to be archived
```
reader->set(VideoReaderProps::PROP_RAW_MODE, true);
```
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.
```
double iRawPacketBase = -1;
reader->get(VideoReaderProps::PROP_RAW_PACKAGES_BASE_INDEX, iRawPacketBase);
GpuMat frame;
while (reader->nextFrame(frame)) {
double nRawPackets = -1;
reader->get(VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB, nRawPackets);
for (int iRawPacketToWrite = static_cast<int>(iRawPacketBase); iRawPacketToWrite < static_cast<int>(iRawPacketBase + nRawPackets); iRawPacketToWrite++) {
if (reader->rawPackageHasKeyFrame(iRawPacketToWrite)) {
Mat packageToWrite;
reader->retrieve(packageToWrite, iRawPacketToWrite);
...
}
}
}
```
\sa retrieve
*/
CV_WRAP virtual bool rawPackageHasKeyFrame(const size_t idx) const = 0;
};

/** @brief Interface for video demultiplexing. :
Expand Down
4 changes: 2 additions & 2 deletions modules/cudacodec/misc/python/test/test_cudacodec.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ def test_reader(self):
# Read raw encoded bitstream
ret, i_base = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_RAW_PACKAGES_BASE_INDEX)
self.assertTrue(ret and i_base == 2.0)
self.assertTrue(reader.grab())
ret, gpu_mat3 = reader.retrieve()
ret, gpu_mat3 = reader.nextFrame()
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
ret = reader.retrieve(gpu_mat3)
self.assertTrue(ret and isinstance(gpu_mat3,cv.cuda.GpuMat) and not gpu_mat3.empty())
ret, n_raw_packages_since_last_grab = reader.getVideoReaderProps(cv.cudacodec.VideoReaderProps_PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB)
self.assertTrue(ret and n_raw_packages_since_last_grab > 0)
self.assertTrue(reader.rawPackageHasKeyFrame(int(i_base)))
ret, raw_data = reader.retrieve(int(i_base))
self.assertTrue(ret and isinstance(raw_data,np.ndarray) and np.any(raw_data))

Expand Down
4 changes: 3 additions & 1 deletion modules/cudacodec/src/NvEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ void NvEncoder::WaitForCompletionEvent(int iEvent)
NVENC_THROW_ERROR("Failed to encode frame", NV_ENC_ERR_GENERIC);
}
#endif
#else
CV_UNUSED(iEvent);
#endif
}

Expand Down Expand Up @@ -782,4 +784,4 @@ void NvEncoder::DestroyBitstreamBuffer()
m_vBitstreamOutputBuffer.clear();
}
}}
#endif
#endif
7 changes: 5 additions & 2 deletions modules/cudacodec/src/NvEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ class NvEncoder
/**
* @brief This function returns the completion event.
*/
void* GetCompletionEvent(uint32_t eventIdx) { return (m_vpCompletionEvent.size() == m_nEncoderBuffer) ? m_vpCompletionEvent[eventIdx] : nullptr; }
void* GetCompletionEvent(uint32_t eventIdx) {
CV_Assert(m_nEncoderBuffer >= 0);
return (m_vpCompletionEvent.size() == static_cast<size_t>(m_nEncoderBuffer)) ? m_vpCompletionEvent[eventIdx] : nullptr;
}

/**
* @brief This function returns the current pixel format.
Expand Down Expand Up @@ -374,4 +377,4 @@ class NvEncoder
std::vector<NV_ENC_OUTPUT_PTR> m_vBitstreamOutputBuffer;
};
}}
#endif
#endif
4 changes: 2 additions & 2 deletions modules/cudacodec/src/cuda/nv12_to_rgb.cu
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
using namespace cv;
using namespace cv::cudev;

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

namespace
{
Expand Down Expand Up @@ -95,7 +95,7 @@ namespace
(chromaCr * constHueColorSpaceMat[8]);
}

__device__ static uint RGBA_pack_10bit(float red, float green, float blue, uint alpha)
__device__ __forceinline__ uint RGBA_pack_10bit(float red, float green, float blue, uint alpha)
{
uint ARGBpixel = 0;

Expand Down
2 changes: 1 addition & 1 deletion modules/cudacodec/src/cuvid_video_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cv::cudacodec::detail::CuvidVideoSource::CuvidVideoSource(const String& fname)
CUVIDEOFORMAT vidfmt;
cuSafeCall( cuvidGetSourceVideoFormat(videoSource_, &vidfmt, 0) );

CV_Assert(Codec::NumCodecs == cudaVideoCodec::cudaVideoCodec_NumCodecs);
CV_Assert(static_cast<int>(Codec::NumCodecs) == static_cast<int>(cudaVideoCodec::cudaVideoCodec_NumCodecs));
format_.codec = static_cast<Codec>(vidfmt.codec);
format_.chromaFormat = static_cast<ChromaFormat>(vidfmt.chroma_format);
format_.nBitDepthMinus8 = vidfmt.bit_depth_luma_minus8;
Expand Down
1 change: 1 addition & 0 deletions modules/cudacodec/src/cuvid_video_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class CuvidVideoSource : public VideoSource

FormatInfo format() const CV_OVERRIDE;
void updateFormat(const FormatInfo& videoFormat) CV_OVERRIDE;
bool get(const int, double&) const { return false; }
void start() CV_OVERRIDE;
void stop() CV_OVERRIDE;
bool isStarted() const CV_OVERRIDE;
Expand Down
16 changes: 9 additions & 7 deletions modules/cudacodec/src/ffmpeg_video_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void FourccToChromaFormat(const int pixelFormat, ChromaFormat &chromaFormat, int
}

static
int StartCodeLen(unsigned char* data, const int sz) {
int StartCodeLen(unsigned char* data, const size_t sz) {
if (sz >= 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
return 3;
else if (sz >= 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1)
Expand All @@ -162,7 +162,8 @@ int StartCodeLen(unsigned char* data, const int sz) {
return 0;
}

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

int codec = (int)cap.get(CAP_PROP_FOURCC);
int pixelFormat = (int)cap.get(CAP_PROP_CODEC_PIXEL_FORMAT);
int codec = static_cast<int>(cap.get(CAP_PROP_FOURCC));
int pixelFormat = static_cast<int>(cap.get(CAP_PROP_CODEC_PIXEL_FORMAT));
format_.codec = FourccToCodec(codec);
format_.height = cap.get(CAP_PROP_FRAME_HEIGHT);
format_.width = cap.get(CAP_PROP_FRAME_WIDTH);
format_.height = static_cast<int>(cap.get(CAP_PROP_FRAME_HEIGHT));
format_.width = static_cast<int>(cap.get(CAP_PROP_FRAME_WIDTH));
format_.displayArea = Rect(0, 0, format_.width, format_.height);
format_.valid = false;
format_.fps = cap.get(CAP_PROP_FPS);
Expand Down Expand Up @@ -244,7 +245,8 @@ bool cv::cudacodec::detail::FFmpegVideoSource::getNextPacket(unsigned char** dat
{
const size_t nBytesToTrimFromData = format_.codec == Codec::MPEG4 ? 3 : 0;
const size_t newSz = extraData.total() + *size - nBytesToTrimFromData;
dataWithHeader = Mat(1, newSz, CV_8UC1);
CV_Assert(newSz <= std::numeric_limits<int>::max());
dataWithHeader = Mat(1, static_cast<int>(newSz), CV_8UC1);
memcpy(dataWithHeader.data, extraData.data, extraData.total());
memcpy(dataWithHeader.data + extraData.total(), (*data) + nBytesToTrimFromData, *size - nBytesToTrimFromData);
*data = dataWithHeader.data;
Expand Down
46 changes: 23 additions & 23 deletions modules/cudacodec/src/video_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
codecSupported |= cudaVideoCodec_VP8 == _codec || cudaVideoCodec_VP9 == _codec;
#endif
#if (CUDART_VERSION >= 9000)
codecSupported |= cudaVideoCodec_AV1;
codecSupported |= cudaVideoCodec_AV1 == _codec;
#endif
CV_Assert(codecSupported);
CV_Assert( cudaVideoChromaFormat_Monochrome == _chromaFormat ||
Expand Down Expand Up @@ -143,12 +143,12 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
}
}

CV_Assert(videoFormat.ulWidth >= decodeCaps.nMinWidth &&
videoFormat.ulHeight >= decodeCaps.nMinHeight &&
videoFormat.ulWidth <= decodeCaps.nMaxWidth &&
videoFormat.ulHeight <= decodeCaps.nMaxHeight);
CV_Assert(videoFormat.ulWidth >= static_cast<int>(decodeCaps.nMinWidth) &&
videoFormat.ulHeight >= static_cast<int>(decodeCaps.nMinHeight) &&
videoFormat.ulWidth <= static_cast<int>(decodeCaps.nMaxWidth) &&
videoFormat.ulHeight <= static_cast<int>(decodeCaps.nMaxHeight));

CV_Assert((videoFormat.width >> 4) * (videoFormat.height >> 4) <= decodeCaps.nMaxMBCount);
CV_Assert((static_cast<unsigned int>(videoFormat.width) >> 4) * (static_cast<unsigned int>(videoFormat.height) >> 4) <= decodeCaps.nMaxMBCount);
#else
if (videoFormat.enableHistogram) {
CV_Error(Error::StsBadArg, "Luma histogram output is not supported when CUDA Toolkit version <= 9.0.");
Expand All @@ -172,14 +172,14 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat)
createInfo_.DeinterlaceMode = static_cast<cudaVideoDeinterlaceMode>(videoFormat.deinterlaceMode);
createInfo_.ulTargetWidth = videoFormat.width;
createInfo_.ulTargetHeight = videoFormat.height;
createInfo_.display_area.left = videoFormat.displayArea.x;
createInfo_.display_area.right = videoFormat.displayArea.x + videoFormat.displayArea.width;
createInfo_.display_area.top = videoFormat.displayArea.y;
createInfo_.display_area.bottom = videoFormat.displayArea.y + videoFormat.displayArea.height;
createInfo_.target_rect.left = videoFormat.targetRoi.x;
createInfo_.target_rect.right = videoFormat.targetRoi.x + videoFormat.targetRoi.width;
createInfo_.target_rect.top = videoFormat.targetRoi.y;
createInfo_.target_rect.bottom = videoFormat.targetRoi.y + videoFormat.targetRoi.height;
createInfo_.display_area.left = static_cast<short>(videoFormat.displayArea.x);
createInfo_.display_area.right = static_cast<short>(videoFormat.displayArea.x + videoFormat.displayArea.width);
createInfo_.display_area.top = static_cast<short>(videoFormat.displayArea.y);
createInfo_.display_area.bottom = static_cast<short>(videoFormat.displayArea.y + videoFormat.displayArea.height);
createInfo_.target_rect.left = static_cast<short>(videoFormat.targetRoi.x);
createInfo_.target_rect.right = static_cast<short>(videoFormat.targetRoi.x + videoFormat.targetRoi.width);
createInfo_.target_rect.top = static_cast<short>(videoFormat.targetRoi.y);
createInfo_.target_rect.bottom = static_cast<short>(videoFormat.targetRoi.y + videoFormat.targetRoi.height);
createInfo_.ulNumOutputSurfaces = 2;
createInfo_.ulCreationFlags = videoCreateFlags;
createInfo_.vidLock = lock_;
Expand Down Expand Up @@ -220,19 +220,19 @@ int cv::cudacodec::detail::VideoDecoder::reconfigure(const FormatInfo& videoForm
videoFormat_.targetRoi = videoFormat.targetRoi;
}

CUVIDRECONFIGUREDECODERINFO reconfigParams = { 0 };
CUVIDRECONFIGUREDECODERINFO reconfigParams = {};
reconfigParams.ulWidth = videoFormat_.ulWidth;
reconfigParams.ulHeight = videoFormat_.ulHeight;
reconfigParams.display_area.left = videoFormat_.displayArea.x;
reconfigParams.display_area.right = videoFormat_.displayArea.x + videoFormat_.displayArea.width;
reconfigParams.display_area.top = videoFormat_.displayArea.y;
reconfigParams.display_area.bottom = videoFormat_.displayArea.y + videoFormat_.displayArea.height;
reconfigParams.display_area.left = static_cast<short>(videoFormat_.displayArea.x);
reconfigParams.display_area.right = static_cast<short>(videoFormat_.displayArea.x + videoFormat_.displayArea.width);
reconfigParams.display_area.top = static_cast<short>(videoFormat_.displayArea.y);
reconfigParams.display_area.bottom = static_cast<short>(videoFormat_.displayArea.y + videoFormat_.displayArea.height);
reconfigParams.ulTargetWidth = videoFormat_.width;
reconfigParams.ulTargetHeight = videoFormat_.height;
reconfigParams.target_rect.left = videoFormat_.targetRoi.x;
reconfigParams.target_rect.right = videoFormat_.targetRoi.x + videoFormat_.targetRoi.width;
reconfigParams.target_rect.top = videoFormat_.targetRoi.y;
reconfigParams.target_rect.bottom = videoFormat_.targetRoi.y + videoFormat_.targetRoi.height;
reconfigParams.target_rect.left = static_cast<short>(videoFormat_.targetRoi.x);
reconfigParams.target_rect.right = static_cast<short>(videoFormat_.targetRoi.x + videoFormat_.targetRoi.width);
reconfigParams.target_rect.top = static_cast<short>(videoFormat_.targetRoi.y);
reconfigParams.target_rect.bottom = static_cast<short>(videoFormat_.targetRoi.y + videoFormat_.targetRoi.height);
reconfigParams.ulNumDecodeSurfaces = videoFormat_.ulNumDecodeSurfaces;

cuSafeCall(cuCtxPushCurrent(ctx_));
Expand Down

0 comments on commit 720e0b8

Please sign in to comment.