From 93c99c438086ac30eb498b6c6ca4057632f804a5 Mon Sep 17 00:00:00 2001
From: Sebastian Hahta <joseha@utu.fi>
Date: Wed, 4 Sep 2019 12:14:48 +0300
Subject: [PATCH] frame: sync/async versions (CUDA streams)

---
 .../rgbd-sources/include/ftl/rgbd/frame.hpp   | 10 +++++---
 components/rgbd-sources/src/frame.cpp         | 24 +++++++++++++++----
 components/rgbd-sources/src/offilter.cpp      |  3 ++-
 components/rgbd-sources/src/stereovideo.cpp   |  4 +---
 components/rgbd-sources/src/stereovideo.hpp   |  3 ++-
 5 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/components/rgbd-sources/include/ftl/rgbd/frame.hpp b/components/rgbd-sources/include/ftl/rgbd/frame.hpp
index a2ec21789..aec182503 100644
--- a/components/rgbd-sources/include/ftl/rgbd/frame.hpp
+++ b/components/rgbd-sources/include/ftl/rgbd/frame.hpp
@@ -56,6 +56,7 @@ public:
 
 	/* @brief	Method to get reference to the channel content
 	 * @param	Channel type
+	 * @param	CUDA stream
 	 * @returns	Const reference to channel data
 	 * 
 	 * Result is valid only if hasChannel() is true. Host/Gpu transfer is
@@ -63,10 +64,9 @@ public:
 	 * changed by calling setChannel(). Return value valid only if
 	 * hasChannel(channel) is true.
 	 */
-
+	template <typename T> const T& getChannel(const ftl::rgbd::channel_t& channel, cv::cuda::Stream& stream);
 	template <typename T> const T& getChannel(const ftl::rgbd::channel_t& channel);
 
-
 	/* @brief	Method to set/modify channel content
 	 * @param	Channel type
 	 * @returns	Reference to channel data
@@ -113,9 +113,13 @@ private:
 	std::vector<uint> available_;
 };
 
+template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel, cv::cuda::Stream& stream);
+template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& channel, cv::cuda::Stream& stream);
+
 template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel);
-template<> cv::Mat& Frame::setChannel(const ftl::rgbd::channel_t& channel);
 template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& channel);
+
+template<> cv::Mat& Frame::setChannel(const ftl::rgbd::channel_t& channel);
 template<> cv::cuda::GpuMat& Frame::setChannel(const ftl::rgbd::channel_t& channel);
 
 }
diff --git a/components/rgbd-sources/src/frame.cpp b/components/rgbd-sources/src/frame.cpp
index ea6a94f4f..8145280cc 100644
--- a/components/rgbd-sources/src/frame.cpp
+++ b/components/rgbd-sources/src/frame.cpp
@@ -4,14 +4,14 @@
 namespace ftl {
 namespace rgbd {
 
-template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel)
+template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel, cv::cuda::Stream &stream)
 {
 	size_t idx = _channelIdx(channel);
 	if (!(available_[idx] & mask_host))
 	{
 		if (available_[idx] & mask_gpu)
 		{
-			channels_gpu_[idx].download(channels_host_[idx]);
+			channels_gpu_[idx].download(channels_host_[idx], stream);
 			available_[idx] |= mask_host;
 		}
 	}
@@ -19,6 +19,14 @@ template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel)
 	return channels_host_[idx];
 }
 
+template<> const cv::Mat& Frame::getChannel(const ftl::rgbd::channel_t& channel)
+{
+	auto &stream = cv::cuda::Stream::Null();
+	auto &retval = getChannel<cv::Mat>(channel, stream);
+	stream.waitForCompletion();
+	return retval;
+}
+
 template<> cv::Mat& Frame::setChannel(const ftl::rgbd::channel_t& channel)
 {
 	size_t idx = _channelIdx(channel);
@@ -26,14 +34,14 @@ template<> cv::Mat& Frame::setChannel(const ftl::rgbd::channel_t& channel)
 	return channels_host_[idx];
 }
 
-template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& channel)
+template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& channel, cv::cuda::Stream &stream)
 {
 	size_t idx = _channelIdx(channel);
 	if (!(available_[idx] & mask_gpu))
 	{
 		if (available_[idx] & mask_host)
 		{
-			channels_gpu_[idx].upload(channels_host_[idx]);
+			channels_gpu_[idx].upload(channels_host_[idx], stream);
 			available_[idx] |= mask_gpu;
 		}
 	}
@@ -41,6 +49,14 @@ template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t&
 	return channels_gpu_[idx];
 }
 
+template<> const cv::cuda::GpuMat& Frame::getChannel(const ftl::rgbd::channel_t& channel)
+{
+	auto &stream = cv::cuda::Stream::Null();
+	auto &retval = getChannel<cv::cuda::GpuMat>(channel, stream);
+	stream.waitForCompletion();
+	return retval;
+}
+
 template<> cv::cuda::GpuMat& Frame::setChannel(const ftl::rgbd::channel_t& channel)
 {
 	size_t idx = _channelIdx(channel);
diff --git a/components/rgbd-sources/src/offilter.cpp b/components/rgbd-sources/src/offilter.cpp
index 913dc5ec5..03db4807a 100644
--- a/components/rgbd-sources/src/offilter.cpp
+++ b/components/rgbd-sources/src/offilter.cpp
@@ -16,7 +16,8 @@ template<typename T> static bool inline isValidDisparity(T d) { return (0.0 < d)
 OFDisparityFilter::OFDisparityFilter(Size size, int n_frames, float threshold) :
 	n_(0), n_max_(n_frames), threshold_(threshold), size_(size)
 {
-	disp_ = Mat::zeros(size, CV_64FC(n_frames));
+	
+	disp_ = Mat::zeros(cv::Size(size.width * n_frames, size.height), CV_64FC1);
 	gray_ = Mat::zeros(size, CV_8UC1);
 
 	nvof_ = cv::cuda::NvidiaOpticalFlow_1_0::create(size.width, size.height,
diff --git a/components/rgbd-sources/src/stereovideo.cpp b/components/rgbd-sources/src/stereovideo.cpp
index 5a1ecfdb6..58ad0d923 100644
--- a/components/rgbd-sources/src/stereovideo.cpp
+++ b/components/rgbd-sources/src/stereovideo.cpp
@@ -61,13 +61,11 @@ void StereoVideoSource::init(const string &file)
 	frames_ = std::vector<Frame>(2);
 
 #ifdef HAVE_OPTFLOW
-/*
-	// TODO make optional, can be calculated at later step
+	// TODO could be calculated at later step too if have access to old frames
 	
 	nvof_ = cv::cuda::NvidiaOpticalFlow_1_0::create(size.width, size.height,
 													cv::cuda::NvidiaOpticalFlow_1_0::NV_OF_PERF_LEVEL_SLOW,
 													true, false, false, 0);
-*/
 #endif
 
 	calib_ = ftl::create<Calibrate>(host_, "calibration", size, stream_);
diff --git a/components/rgbd-sources/src/stereovideo.hpp b/components/rgbd-sources/src/stereovideo.hpp
index cfbe9f5ef..2f9fa1831 100644
--- a/components/rgbd-sources/src/stereovideo.hpp
+++ b/components/rgbd-sources/src/stereovideo.hpp
@@ -41,6 +41,7 @@ class StereoVideoSource : public detail::Source {
 	Disparity *disp_;
 	
 	bool ready_;
+	bool use_optflow_;
 	
 	cv::cuda::Stream stream_;
 	cv::cuda::Stream stream2_;
@@ -50,7 +51,7 @@ class StereoVideoSource : public detail::Source {
 	cv::Mat mask_l_;
 
 #ifdef HAVE_OPTFLOW
-//	cv::Ptr<cv::cuda::NvidiaOpticalFlow_1_0> nvof_;
+	cv::Ptr<cv::cuda::NvidiaOpticalFlow_1_0> nvof_;
 #endif
 
 	void init(const std::string &);
-- 
GitLab