cuda::GpuMat mapFrame(int picIdx, CUVIDPROCPARAMS& videoProcParams) { CUdeviceptr ptr; unsigned int pitch; cuSafeCall( cuvidMapVideoFrame(decoder_, picIdx, &ptr, &pitch, &videoProcParams) ); return cuda::GpuMat(targetHeight() * 3 / 2, targetWidth(), CV_8UC1, (void*) ptr, pitch); }
cv::gpu::detail::CuvidVideoSource::CuvidVideoSource(const std::string& fname) { CUVIDSOURCEPARAMS params; std::memset(¶ms, 0, sizeof(CUVIDSOURCEPARAMS)); // Fill parameter struct params.pUserData = this; // will be passed to data handlers params.pfnVideoDataHandler = HandleVideoData; // our local video-handler callback params.pfnAudioDataHandler = 0; // now create the actual source CUresult res = cuvidCreateVideoSource(&videoSource_, fname.c_str(), ¶ms); if (res == CUDA_ERROR_INVALID_SOURCE) throw std::runtime_error("Unsupported video source"); cuSafeCall( res ); CUVIDEOFORMAT vidfmt; cuSafeCall( cuvidGetSourceVideoFormat(videoSource_, &vidfmt, 0) ); format_.codec = static_cast<VideoReader_GPU::Codec>(vidfmt.codec); format_.chromaFormat = static_cast<VideoReader_GPU::ChromaFormat>(vidfmt.chroma_format); format_.width = vidfmt.coded_width; format_.height = vidfmt.coded_height; }
cv::gpu::VideoReader_GPU::Impl::Impl(const cv::Ptr<VideoSource>& source) : videoSource_(source), lock_(0) { // init context GpuMat temp(1, 1, CV_8UC1); temp.release(); DeviceInfo devInfo; CV_Assert( devInfo.supports(FEATURE_SET_COMPUTE_11) ); CUcontext ctx; cuSafeCall( cuCtxGetCurrent(&ctx) ); cuSafeCall( cuvidCtxLockCreate(&lock_, ctx) ); frameQueue_.reset(new detail::FrameQueue); videoDecoder_.reset(new detail::VideoDecoder(videoSource_->format(), lock_)); videoParser_.reset(new detail::VideoParser(videoDecoder_.get(), frameQueue_.get())); videoSource_->setFrameQueue(frameQueue_.get()); videoSource_->setVideoParser(videoParser_.get()); videoSource_->start(); }
cv::gpu::detail::VideoParser::VideoParser(VideoDecoder* videoDecoder, FrameQueue* frameQueue) : videoDecoder_(videoDecoder), frameQueue_(frameQueue), unparsedPackets_(0), hasError_(false) { CUVIDPARSERPARAMS params; memset(¶ms, 0, sizeof(CUVIDPARSERPARAMS)); params.CodecType = videoDecoder->codec(); params.ulMaxNumDecodeSurfaces = videoDecoder->maxDecodeSurfaces(); params.ulMaxDisplayDelay = 1; // this flag is needed so the parser will push frames out to the decoder as quickly as it can params.pUserData = this; params.pfnSequenceCallback = HandleVideoSequence; // Called before decoding frames and/or whenever there is a format change params.pfnDecodePicture = HandlePictureDecode; // Called when a picture is ready to be decoded (decode order) params.pfnDisplayPicture = HandlePictureDisplay; // Called whenever a picture is ready to be displayed (display order) cuSafeCall( cuvidCreateVideoParser(&parser_, ¶ms) ); }
void cv::gpu::detail::CuvidVideoSource::stop() { cuSafeCall( cuvidSetVideoSourceState(videoSource_, cudaVideoState_Stopped) ); }
void unmapFrame(cuda::GpuMat& frame) { cuSafeCall( cuvidUnmapVideoFrame(decoder_, (CUdeviceptr) frame.data) ); frame.release(); }