From 720e0b8d3fd21d2a0167bf70ed7e976d292714a2 Mon Sep 17 00:00:00 2001 From: cudawarped <12133430+cudawarped@users.noreply.github.com> Date: Fri, 30 Jun 2023 12:56:58 +0300 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFcudacodec:=20Address=20build=20warning?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cudacodec/include/opencv2/cudacodec.hpp | 41 +++++++++++++-- .../misc/python/test/test_cudacodec.py | 4 +- modules/cudacodec/src/NvEncoder.cpp | 4 +- modules/cudacodec/src/NvEncoder.h | 7 ++- modules/cudacodec/src/cuda/nv12_to_rgb.cu | 4 +- modules/cudacodec/src/cuvid_video_source.cpp | 2 +- modules/cudacodec/src/cuvid_video_source.hpp | 1 + modules/cudacodec/src/ffmpeg_video_source.cpp | 16 +++--- modules/cudacodec/src/video_decoder.cpp | 46 ++++++++--------- modules/cudacodec/src/video_reader.cpp | 50 +++++++++--------- modules/cudacodec/src/video_source.hpp | 2 +- modules/cudacodec/src/video_writer.cpp | 2 +- modules/cudacodec/test/test_video.cpp | 51 +++++++++---------- 13 files changed, 135 insertions(+), 95 deletions(-) diff --git a/modules/cudacodec/include/opencv2/cudacodec.hpp b/modules/cudacodec/include/opencv2/cudacodec.hpp index 42325c64613..82035f54e4a 100644 --- a/modules/cudacodec/include/opencv2/cudacodec.hpp +++ b/modules/cudacodec/include/opencv2/cudacodec.hpp @@ -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. @@ -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. @@ -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 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(iRawPacketBase); iRawPacketToWrite < static_cast(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. : diff --git a/modules/cudacodec/misc/python/test/test_cudacodec.py b/modules/cudacodec/misc/python/test/test_cudacodec.py index cf20daeb181..573311a05c4 100644 --- a/modules/cudacodec/misc/python/test/test_cudacodec.py +++ b/modules/cudacodec/misc/python/test/test_cudacodec.py @@ -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)) diff --git a/modules/cudacodec/src/NvEncoder.cpp b/modules/cudacodec/src/NvEncoder.cpp index 249f6f1c61e..a52726e8247 100644 --- a/modules/cudacodec/src/NvEncoder.cpp +++ b/modules/cudacodec/src/NvEncoder.cpp @@ -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 } @@ -782,4 +784,4 @@ void NvEncoder::DestroyBitstreamBuffer() m_vBitstreamOutputBuffer.clear(); } }} -#endif \ No newline at end of file +#endif diff --git a/modules/cudacodec/src/NvEncoder.h b/modules/cudacodec/src/NvEncoder.h index dd13d2c1501..0f41e301ade 100644 --- a/modules/cudacodec/src/NvEncoder.h +++ b/modules/cudacodec/src/NvEncoder.h @@ -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(m_nEncoderBuffer)) ? m_vpCompletionEvent[eventIdx] : nullptr; + } /** * @brief This function returns the current pixel format. @@ -374,4 +377,4 @@ class NvEncoder std::vector m_vBitstreamOutputBuffer; }; }} -#endif \ No newline at end of file +#endif diff --git a/modules/cudacodec/src/cuda/nv12_to_rgb.cu b/modules/cudacodec/src/cuda/nv12_to_rgb.cu index 049041a7588..295cc54f682 100644 --- a/modules/cudacodec/src/cuda/nv12_to_rgb.cu +++ b/modules/cudacodec/src/cuda/nv12_to_rgb.cu @@ -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 { @@ -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; diff --git a/modules/cudacodec/src/cuvid_video_source.cpp b/modules/cudacodec/src/cuvid_video_source.cpp index 3d7bbbb8c05..5c78288f9b6 100644 --- a/modules/cudacodec/src/cuvid_video_source.cpp +++ b/modules/cudacodec/src/cuvid_video_source.cpp @@ -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(Codec::NumCodecs) == static_cast(cudaVideoCodec::cudaVideoCodec_NumCodecs)); format_.codec = static_cast(vidfmt.codec); format_.chromaFormat = static_cast(vidfmt.chroma_format); format_.nBitDepthMinus8 = vidfmt.bit_depth_luma_minus8; diff --git a/modules/cudacodec/src/cuvid_video_source.hpp b/modules/cudacodec/src/cuvid_video_source.hpp index 21b28980a20..5c1099ee4d8 100644 --- a/modules/cudacodec/src/cuvid_video_source.hpp +++ b/modules/cudacodec/src/cuvid_video_source.hpp @@ -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; diff --git a/modules/cudacodec/src/ffmpeg_video_source.cpp b/modules/cudacodec/src/ffmpeg_video_source.cpp index 20a02f84b55..d6e0eda23bd 100644 --- a/modules/cudacodec/src/ffmpeg_video_source.cpp +++ b/modules/cudacodec/src/ffmpeg_video_source.cpp @@ -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) @@ -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 @@ -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(cap.get(CAP_PROP_FOURCC)); + int pixelFormat = static_cast(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(cap.get(CAP_PROP_FRAME_HEIGHT)); + format_.width = static_cast(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); @@ -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::max()); + dataWithHeader = Mat(1, static_cast(newSz), CV_8UC1); memcpy(dataWithHeader.data, extraData.data, extraData.total()); memcpy(dataWithHeader.data + extraData.total(), (*data) + nBytesToTrimFromData, *size - nBytesToTrimFromData); *data = dataWithHeader.data; diff --git a/modules/cudacodec/src/video_decoder.cpp b/modules/cudacodec/src/video_decoder.cpp index 10008d9b033..bee7a2a16ff 100644 --- a/modules/cudacodec/src/video_decoder.cpp +++ b/modules/cudacodec/src/video_decoder.cpp @@ -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 || @@ -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(decodeCaps.nMinWidth) && + videoFormat.ulHeight >= static_cast(decodeCaps.nMinHeight) && + videoFormat.ulWidth <= static_cast(decodeCaps.nMaxWidth) && + videoFormat.ulHeight <= static_cast(decodeCaps.nMaxHeight)); - CV_Assert((videoFormat.width >> 4) * (videoFormat.height >> 4) <= decodeCaps.nMaxMBCount); + CV_Assert((static_cast(videoFormat.width) >> 4) * (static_cast(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."); @@ -172,14 +172,14 @@ void cv::cudacodec::detail::VideoDecoder::create(const FormatInfo& videoFormat) createInfo_.DeinterlaceMode = static_cast(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(videoFormat.displayArea.x); + createInfo_.display_area.right = static_cast(videoFormat.displayArea.x + videoFormat.displayArea.width); + createInfo_.display_area.top = static_cast(videoFormat.displayArea.y); + createInfo_.display_area.bottom = static_cast(videoFormat.displayArea.y + videoFormat.displayArea.height); + createInfo_.target_rect.left = static_cast(videoFormat.targetRoi.x); + createInfo_.target_rect.right = static_cast(videoFormat.targetRoi.x + videoFormat.targetRoi.width); + createInfo_.target_rect.top = static_cast(videoFormat.targetRoi.y); + createInfo_.target_rect.bottom = static_cast(videoFormat.targetRoi.y + videoFormat.targetRoi.height); createInfo_.ulNumOutputSurfaces = 2; createInfo_.ulCreationFlags = videoCreateFlags; createInfo_.vidLock = lock_; @@ -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(videoFormat_.displayArea.x); + reconfigParams.display_area.right = static_cast(videoFormat_.displayArea.x + videoFormat_.displayArea.width); + reconfigParams.display_area.top = static_cast(videoFormat_.displayArea.y); + reconfigParams.display_area.bottom = static_cast(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(videoFormat_.targetRoi.x); + reconfigParams.target_rect.right = static_cast(videoFormat_.targetRoi.x + videoFormat_.targetRoi.width); + reconfigParams.target_rect.top = static_cast(videoFormat_.targetRoi.y); + reconfigParams.target_rect.bottom = static_cast(videoFormat_.targetRoi.y + videoFormat_.targetRoi.height); reconfigParams.ulNumDecodeSurfaces = videoFormat_.ulNumDecodeSurfaces; cuSafeCall(cuCtxPushCurrent(ctx_)); diff --git a/modules/cudacodec/src/video_reader.cpp b/modules/cudacodec/src/video_reader.cpp index b6ef2ca5376..00e512ff0c0 100644 --- a/modules/cudacodec/src/video_reader.cpp +++ b/modules/cudacodec/src/video_reader.cpp @@ -56,6 +56,8 @@ void cv::cudacodec::MapHist(const GpuMat&, Mat&) { throw_no_cuda(); } void nv12ToBgra(const GpuMat& decodedFrame, GpuMat& outFrame, int width, int height, const bool videoFullRangeFlag, cudaStream_t stream); bool ValidColorFormat(const ColorFormat colorFormat); +void cvtFromNv12(const GpuMat& decodedFrame, GpuMat& outFrame, int width, int height, const ColorFormat colorFormat, const bool videoFullRangeFlag, + Stream stream); void cvtFromNv12(const GpuMat& decodedFrame, GpuMat& outFrame, int width, int height, const ColorFormat colorFormat, const bool videoFullRangeFlag, Stream stream) @@ -68,24 +70,25 @@ void cvtFromNv12(const GpuMat& decodedFrame, GpuMat& outFrame, int width, int he outFrame.create(height, width, CV_8UC3); Npp8u* pSrc[2] = { decodedFrame.data, &decodedFrame.data[decodedFrame.step * height] }; NppiSize oSizeROI = { width,height }; + CV_Assert(decodedFrame.step <= std::numeric_limits::max() && outFrame.step <= std::numeric_limits::max()); #if (CUDART_VERSION < 10010) cv::cuda::NppStreamHandler h(stream); if (videoFullRangeFlag) - nppSafeCall(nppiNV12ToBGR_709HDTV_8u_P2C3R(pSrc, decodedFrame.step, outFrame.data, outFrame.step, oSizeROI)); + nppSafeCall(nppiNV12ToBGR_709HDTV_8u_P2C3R(pSrc, static_cast(decodedFrame.step), outFrame.data, static_cast(outFrame.step), oSizeROI)); else { - nppSafeCall(nppiNV12ToBGR_8u_P2C3R(pSrc, decodedFrame.step, outFrame.data, outFrame.step, oSizeROI)); + nppSafeCall(nppiNV12ToBGR_8u_P2C3R(pSrc, static_cast(decodedFrame.step), outFrame.data, static_cast(outFrame.step), oSizeROI)); } #elif (CUDART_VERSION >= 10010) NppStreamContext nppStreamCtx; nppSafeCall(nppGetStreamContext(&nppStreamCtx)); nppStreamCtx.hStream = StreamAccessor::getStream(stream); if (videoFullRangeFlag) - nppSafeCall(nppiNV12ToBGR_709HDTV_8u_P2C3R_Ctx(pSrc, decodedFrame.step, outFrame.data, outFrame.step, oSizeROI, nppStreamCtx)); + nppSafeCall(nppiNV12ToBGR_709HDTV_8u_P2C3R_Ctx(pSrc, static_cast(decodedFrame.step), outFrame.data, static_cast(outFrame.step), oSizeROI, nppStreamCtx)); else { #if (CUDART_VERSION < 11000) - nppSafeCall(nppiNV12ToBGR_8u_P2C3R_Ctx(pSrc, decodedFrame.step, outFrame.data, outFrame.step, oSizeROI, nppStreamCtx)); + nppSafeCall(nppiNV12ToBGR_8u_P2C3R_Ctx(pSrc, static_cast(decodedFrame.step), outFrame.data, static_cast(outFrame.step), oSizeROI, nppStreamCtx)); #else - nppSafeCall(nppiNV12ToBGR_709CSC_8u_P2C3R_Ctx(pSrc, decodedFrame.step, outFrame.data, outFrame.step, oSizeROI, nppStreamCtx)); + nppSafeCall(nppiNV12ToBGR_709CSC_8u_P2C3R_Ctx(pSrc, static_cast(decodedFrame.step), outFrame.data, static_cast(outFrame.step), oSizeROI, nppStreamCtx)); #endif } #endif @@ -129,11 +132,12 @@ namespace bool set(const ColorFormat colorFormat_) CV_OVERRIDE; - bool get(const VideoReaderProps propertyId, double& propertyVal) const CV_OVERRIDE; - bool getVideoReaderProps(const VideoReaderProps propertyId, double& propertyValOut, double propertyValIn) const CV_OVERRIDE; + bool get(const VideoReaderProps propertyId, size_t& propertyVal) const CV_OVERRIDE; bool get(const int propertyId, double& propertyVal) const CV_OVERRIDE; + bool rawPackageHasKeyFrame(const size_t idx) const CV_OVERRIDE; + private: bool internalGrab(GpuMat & frame, GpuMat & histogram, Stream & stream); void waitForDecoderInit(); @@ -307,8 +311,9 @@ namespace if (idx >= rawPacketsBaseIdx && idx < rawPacketsBaseIdx + rawPackets.size()) { if (!frame.isMat()) CV_Error(Error::StsUnsupportedFormat, "Raw data is stored on the host and must be retrieved using a cv::Mat"); - const size_t i = idx - rawPacketsBaseIdx; - Mat tmp(1, rawPackets.at(i).Size(), CV_8UC1, const_cast(rawPackets.at(i).Data()), rawPackets.at(i).Size()); + const size_t i = idx - static_cast(rawPacketsBaseIdx); + CV_Assert(rawPackets.at(i).Size() <= std::numeric_limits::max()); + Mat tmp(1, static_cast(rawPackets.at(i).Size()), CV_8UC1, const_cast(rawPackets.at(i).Data()), rawPackets.at(i).Size()); frame.getMatRef() = tmp; } } @@ -320,6 +325,8 @@ namespace case VideoReaderProps::PROP_RAW_MODE : videoSource_->SetRawMode(static_cast(propertyVal)); return true; + default: + break; } return false; } @@ -345,7 +352,7 @@ namespace return true; } - bool VideoReaderImpl::get(const VideoReaderProps propertyId, double& propertyVal) const { + bool VideoReaderImpl::get(const VideoReaderProps propertyId, size_t& propertyVal) const { switch (propertyId) { case VideoReaderProps::PROP_DECODED_FRAME_IDX: @@ -367,15 +374,6 @@ namespace case VideoReaderProps::PROP_RAW_MODE: propertyVal = videoSource_->RawModeEnabled(); return true; - case VideoReaderProps::PROP_LRF_HAS_KEY_FRAME: { - const int iPacket = propertyVal - rawPacketsBaseIdx; - if (videoSource_->RawModeEnabled() && iPacket >= 0 && iPacket < rawPackets.size()) { - propertyVal = rawPackets.at(iPacket).ContainsKeyFrame(); - return true; - } - else - break; - } case VideoReaderProps::PROP_ALLOW_FRAME_DROP: propertyVal = videoParser_->allowFrameDrops(); return true; @@ -383,7 +381,7 @@ namespace propertyVal = videoParser_->udpSource(); return true; case VideoReaderProps::PROP_COLOR_FORMAT: - propertyVal = static_cast(colorFormat); + propertyVal = static_cast(colorFormat); return true; default: break; @@ -391,11 +389,13 @@ namespace return false; } - bool VideoReaderImpl::getVideoReaderProps(const VideoReaderProps propertyId, double& propertyValOut, double propertyValIn) const { - double propertyValInOut = propertyValIn; - const bool ret = get(propertyId, propertyValInOut); - propertyValOut = propertyValInOut; - return ret; + bool VideoReaderImpl::rawPackageHasKeyFrame(const size_t idx) const { + if (idx < rawPacketsBaseIdx) return false; + const size_t iPacket = idx - rawPacketsBaseIdx; + if (videoSource_->RawModeEnabled() && iPacket < rawPackets.size()) + return rawPackets.at(iPacket).ContainsKeyFrame(); + else + return false; } bool VideoReaderImpl::get(const int propertyId, double& propertyVal) const { diff --git a/modules/cudacodec/src/video_source.hpp b/modules/cudacodec/src/video_source.hpp index 8c96a34f2d5..8bf05fa4ab2 100644 --- a/modules/cudacodec/src/video_source.hpp +++ b/modules/cudacodec/src/video_source.hpp @@ -57,7 +57,7 @@ class VideoSource virtual FormatInfo format() const = 0; virtual void updateFormat(const FormatInfo& videoFormat) = 0; - virtual bool get(const int propertyId, double& propertyVal) const { return false; } + virtual bool get(const int, double&) const = 0; virtual void start() = 0; virtual void stop() = 0; virtual bool isStarted() const = 0; diff --git a/modules/cudacodec/src/video_writer.cpp b/modules/cudacodec/src/video_writer.cpp index db3e2e36306..409af03fa64 100644 --- a/modules/cudacodec/src/video_writer.cpp +++ b/modules/cudacodec/src/video_writer.cpp @@ -164,7 +164,7 @@ VideoWriterImpl::VideoWriterImpl(const Ptr& encoderCallBack_, c CV_Assert(colorFormat != ColorFormat::UNDEFINED); surfaceFormat = EncBufferFormat(colorFormat); if (surfaceFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) { - String msg = cv::format("Unsupported input surface format: %i", colorFormat); + String msg = cv::format("Unsupported input surface format: %i", static_cast(colorFormat)); CV_LOG_WARNING(NULL, msg); CV_Error(Error::StsUnsupportedFormat, msg); } diff --git a/modules/cudacodec/test/test_video.cpp b/modules/cudacodec/test/test_video.cpp index ead5fa944ca..4a349d89160 100644 --- a/modules/cudacodec/test/test_video.cpp +++ b/modules/cudacodec/test/test_video.cpp @@ -128,9 +128,9 @@ CUDA_TEST_P(CheckSet, Reader) std::string inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + +"../" + GET_PARAM(1); cv::Ptr reader = cv::cudacodec::createVideoReader(inputFile); - double unsupportedVal = -1; + size_t unsupportedVal = 0; ASSERT_FALSE(reader->get(cv::cudacodec::VideoReaderProps::PROP_NOT_SUPPORTED, unsupportedVal)); - double rawModeVal = -1; + size_t rawModeVal = 0; ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_RAW_MODE, rawModeVal)); ASSERT_FALSE(rawModeVal); ASSERT_TRUE(reader->set(cv::cudacodec::VideoReaderProps::PROP_RAW_MODE,true)); @@ -138,7 +138,7 @@ CUDA_TEST_P(CheckSet, Reader) ASSERT_TRUE(rawModeVal); bool rawPacketsAvailable = false; while (reader->grab()) { - double nRawPackages = -1; + size_t nRawPackages = 0; ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB, nRawPackages)); if (nRawPackages > 0) { rawPacketsAvailable = true; @@ -156,17 +156,17 @@ CUDA_TEST_P(CheckExtraData, Reader) cv::cuda::setDevice(GET_PARAM(0).deviceID()); const string path = get<0>(GET_PARAM(1)); - const int sz = get<1>(GET_PARAM(1)); + const size_t sz = get<1>(GET_PARAM(1)); std::string inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "../" + path; cv::cudacodec::VideoReaderInitParams params; params.rawMode = true; cv::Ptr reader = cv::cudacodec::createVideoReader(inputFile, {}, params); - double extraDataIdx = -1; + size_t extraDataIdx = 0; ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_EXTRA_DATA_INDEX, extraDataIdx)); - ASSERT_EQ(extraDataIdx, 1 ); + ASSERT_EQ(extraDataIdx, static_cast(1) ); ASSERT_TRUE(reader->grab()); cv::Mat extraData; - const bool newData = reader->retrieve(extraData, static_cast(extraDataIdx)); + const bool newData = reader->retrieve(extraData, extraDataIdx); ASSERT_TRUE((newData && sz) || (!newData && !sz)); ASSERT_EQ(extraData.total(), sz); } @@ -184,19 +184,18 @@ CUDA_TEST_P(CheckKeyFrame, Reader) cv::cudacodec::VideoReaderInitParams params; params.rawMode = true; cv::Ptr reader = cv::cudacodec::createVideoReader(inputFile, {}, params); - double rawIdxBase = -1; + size_t rawIdxBase = 0; ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_RAW_PACKAGES_BASE_INDEX, rawIdxBase)); - ASSERT_EQ(rawIdxBase, 2); + ASSERT_EQ(rawIdxBase, static_cast(2)); constexpr int maxNPackagesToCheck = 2; int nPackages = 0; while (nPackages < maxNPackagesToCheck) { ASSERT_TRUE(reader->grab()); - double N = -1; + size_t N = 0; ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB,N)); - for (int i = static_cast(rawIdxBase); i < static_cast(N + rawIdxBase); i++) { + for (size_t i = rawIdxBase; i < N + rawIdxBase; i++) { nPackages++; - double containsKeyFrame = i; - ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_LRF_HAS_KEY_FRAME, containsKeyFrame)); + const bool containsKeyFrame = reader->rawPackageHasKeyFrame(i); ASSERT_TRUE((nPackages == 1 && containsKeyFrame) || (nPackages == 2 && !containsKeyFrame)) << "nPackage: " << i; if (nPackages >= maxNPackagesToCheck) break; @@ -277,9 +276,7 @@ CUDA_TEST_P(DisplayResolution, Reader) readerCodedSz->set(cudacodec::ColorFormat::GRAY); GpuMat frameCodedSz; ASSERT_TRUE(readerCodedSz->nextFrame(frameCodedSz)); - const cudacodec::FormatInfo formatCodedSz = readerCodedSz->format(); - const double err = cv::cuda::norm(frame, frameCodedSz(displayArea), NORM_INF); - ASSERT_TRUE(err == 0); + ASSERT_EQ(cv::cuda::norm(frame, frameCodedSz(displayArea), NORM_INF), 0); } } @@ -309,7 +306,7 @@ CUDA_TEST_P(Video, Reader) // request a different colour format for each frame const std::pair< cudacodec::ColorFormat, int>& formatToChannels = formatsToChannels[i % formatsToChannels.size()]; ASSERT_TRUE(reader->set(formatToChannels.first)); - double colorFormat; + size_t colorFormat; ASSERT_TRUE(reader->get(cudacodec::VideoReaderProps::PROP_COLOR_FORMAT, colorFormat) && static_cast(colorFormat) == formatToChannels.first); ASSERT_TRUE(reader->nextFrame(frame)); const int height = formatToChannels.first == cudacodec::ColorFormat::NV_NV12 ? static_cast(1.5 * fmt.height) : fmt.height; @@ -417,8 +414,9 @@ CUDA_TEST_P(ReconfigureDecoder, Reader) ASSERT_TRUE(frame.size() == initialSize); ASSERT_TRUE(fmt.srcRoi.empty()); const bool resChanged = (initialCodedSize.width != fmt.ulWidth) || (initialCodedSize.height != fmt.ulHeight); - if (resChanged) + if (resChanged) { ASSERT_TRUE(fmt.targetRoi.empty()); + } } ASSERT_TRUE(nFrames == 40); } @@ -439,19 +437,18 @@ CUDA_TEST_P(VideoReadRaw, Reader) cv::cudacodec::VideoReaderInitParams params; params.rawMode = true; cv::Ptr reader = cv::cudacodec::createVideoReader(inputFile, {}, params); - double rawIdxBase = -1; + size_t rawIdxBase = 0; ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_RAW_PACKAGES_BASE_INDEX, rawIdxBase)); - ASSERT_EQ(rawIdxBase, 2); + ASSERT_EQ(rawIdxBase, static_cast(2)); cv::cuda::GpuMat frame; for (int i = 0; i < 100; i++) { ASSERT_TRUE(reader->grab()); ASSERT_TRUE(reader->retrieve(frame)); ASSERT_FALSE(frame.empty()); - double N = -1; - ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB,N)); - ASSERT_TRUE(N >= 0) << N << " < 0"; - for (int j = static_cast(rawIdxBase); j <= static_cast(N + rawIdxBase); j++) { + size_t N = 0; + ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_NUMBER_OF_RAW_PACKAGES_SINCE_LAST_GRAB, N)); + for (size_t j = rawIdxBase; j <= N + rawIdxBase; j++) { Mat rawPackets; reader->retrieve(rawPackets, j); file.write((char*)rawPackets.data, rawPackets.total()); @@ -466,7 +463,7 @@ CUDA_TEST_P(VideoReadRaw, Reader) cv::cudacodec::VideoReaderInitParams params; params.rawMode = true; cv::Ptr readerActual = cv::cudacodec::createVideoReader(fileNameOut, {}, params); - double decodedFrameIdx = -1; + size_t decodedFrameIdx = 0; ASSERT_TRUE(readerActual->get(cv::cudacodec::VideoReaderProps::PROP_DECODED_FRAME_IDX, decodedFrameIdx)); ASSERT_EQ(decodedFrameIdx, 0); cv::cuda::GpuMat reference, actual; @@ -546,7 +543,7 @@ CUDA_TEST_P(CheckParams, Reader) { std::vector exceptionsThrown = { false,true }; std::vector capPropFormats = { -1,0 }; - for (int i = 0; i < capPropFormats.size(); i++) { + for (size_t i = 0; i < capPropFormats.size(); i++) { bool exceptionThrown = false; try { cv::Ptr reader = cv::cudacodec::createVideoReader(inputFile, { @@ -612,7 +609,7 @@ CUDA_TEST_P(CheckInitParams, Reader) params.udpSource = GET_PARAM(2); params.allowFrameDrop = GET_PARAM(3); params.rawMode = GET_PARAM(4); - double udpSource = 0, allowFrameDrop = 0, rawMode = 0; + size_t udpSource = 0, allowFrameDrop = 0, rawMode = 0; cv::Ptr reader = cv::cudacodec::createVideoReader(inputFile, {}, params); ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_UDP_SOURCE, udpSource) && static_cast(udpSource) == params.udpSource); ASSERT_TRUE(reader->get(cv::cudacodec::VideoReaderProps::PROP_ALLOW_FRAME_DROP, allowFrameDrop) && static_cast(allowFrameDrop) == params.allowFrameDrop);