[libcamera-devel,2/2] pipeline: simple: Use uniform-cost search algorithm to setup media pipeline
diff mbox series

Message ID 20210309112949.26251-3-m.cichy@pengutronix.de
State Superseded
Headers show
Series
  • [libcamera-devel,1/2] media_entity: Overload == operator to use media-entities in maps
Related show

Commit Message

Marian Cichy March 9, 2021, 11:29 a.m. UTC
When the SimplePipeline is setting up its data and media pipeline in the
SimpleCameraData constructor, it merely tries to find the first valid
pad and link to the next entity, starting from the camera sensor.
Following this path may not always lead to a valid capture device and
therefore the setup will fail on some machines. This is for example an
issue when using the SimplePipeline on an i.MX-6Q with its IMX IPU.

This commit implements a different approach to setup the media-pipeline
by finding the shortest path to a valid capture device, using the
uniform-cost search algorithm. The shortest path has a good chance to be
the path from the sensor to the CSI capture device, as other paths may
involve image converters, encoders or other IPU blocks and will have
therefore more nodes.

Signed-off-by: Marian Cichy <m.cichy@pengutronix.de>
---
 src/libcamera/pipeline/simple/simple.cpp | 104 +++++++++++------------
 1 file changed, 52 insertions(+), 52 deletions(-)

Comments

Laurent Pinchart March 13, 2021, 2:04 a.m. UTC | #1
Hi Marian,

Thank you for the patch.

On Tue, Mar 09, 2021 at 12:29:49PM +0100, Marian Cichy wrote:
> When the SimplePipeline is setting up its data and media pipeline in the
> SimpleCameraData constructor, it merely tries to find the first valid
> pad and link to the next entity, starting from the camera sensor.
> Following this path may not always lead to a valid capture device and
> therefore the setup will fail on some machines. This is for example an
> issue when using the SimplePipeline on an i.MX-6Q with its IMX IPU.
> 
> This commit implements a different approach to setup the media-pipeline
> by finding the shortest path to a valid capture device, using the
> uniform-cost search algorithm. The shortest path has a good chance to be
> the path from the sensor to the CSI capture device, as other paths may
> involve image converters, encoders or other IPU blocks and will have
> therefore more nodes.
> 
> Signed-off-by: Marian Cichy <m.cichy@pengutronix.de>
> ---
>  src/libcamera/pipeline/simple/simple.cpp | 104 +++++++++++------------
>  1 file changed, 52 insertions(+), 52 deletions(-)
> 
> diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp
> index 1fcf3671..82a3b402 100644
> --- a/src/libcamera/pipeline/simple/simple.cpp
> +++ b/src/libcamera/pipeline/simple/simple.cpp
> @@ -274,62 +274,62 @@ SimpleCameraData::SimpleCameraData(SimplePipelineHandler *pipe,
>  	int ret;
>  
>  	/*
> -	 * Walk the pipeline towards the video node and store all entities
> -	 * along the way.
> +	 * Use the uniform-cost search algorithm to find the shortest path to a
> +	 * capture device. The shortest path has a good chance to end in a
> +	 * CSI capture device, as this is the most direct path for a camera.
> +	 * There are other capture devices which ends in encoders or image
> +	 * converters which will write the capture image to RAM. These paths
> +	 * would be invalid for the purpose of SimplePipeline. These paths are
> +	 * also usually longer as they would naturally contain IC- or encoder
> +	 * IPU-blocks. Therefore, this algorithm will avoid these paths.

This comment is specific to the i.MX6, while the simple pipeline handler
isn't. Could it be rephrased in a generic way ? The i.MX6-specific parts
could be kept as an example.

>  	 */
> -	MediaEntity *source = sensor;
> -
> -	while (source) {
> -		/* If we have reached a video node, we're done. */
> -		if (source->function() == MEDIA_ENT_F_IO_V4L)
> -			break;
> -
> -		/*
> -		 * Use the first output pad that has links and follow its first
> -		 * link.
> -		 */
> -		MediaPad *sourcePad = nullptr;
> -		MediaLink *sourceLink = nullptr;
> -		for (MediaPad *pad : source->pads()) {
> -			if ((pad->flags() & MEDIA_PAD_FL_SOURCE) &&
> -			    !pad->links().empty()) {
> -				sourcePad = pad;
> -				sourceLink = pad->links().front();
> -				break;
> +	std::unordered_set<MediaEntity*> visited;

This should be

	std::unordered_set<MediaEntity *> visited;

and similarly below. We have a script to catch coding style issues, in
utils/checkstyle.py. I'd recommend enabling it as a git hook by copying
either utils/hooks/post-commit or utils/hooks/pre-commit to the
.git/hooks/ directory.

Please note that coding style decisions are not always clear-cut, the
recommendations of the script can be ignored when appropriate.

> +	std::queue<MediaEntity*> queue;
> +
> +	/* Remember at each entity where we came from */
> +	std::unordered_map<MediaEntity*, std::pair<MediaEntity*, MediaLink*>> parentList;

You should include <unordered_map>.

How about replacing the pair with Entity ? And as it's not a list, maybe
we should just it parentMap, or just parents ?

> +	queue.push(sensor);
> +
> +	while (!queue.empty()) {
> +		MediaEntity *entity = queue.back();
> +		queue.pop();

Let's give the code a bit of room to breath with blank lines to separate
functional blocks :-)

> +		/* Found the capture device */
> +		if (entity->function() == MEDIA_ENT_F_IO_V4L) {
> +			video_ = pipe->video(entity);

How about a
			break;

here, and moving the next block after the while() ? It will keep the
search simpler to read and reduce indentation.

> +			LOG(SimplePipeline, Debug) << "Capture Device: "
> +						   << entity->name();

Something that checkstyle.py doesn't know about yet, we write multi-line
log statements with a line break just after LOG():

			LOG(SimplePipeline, Debug)
				<< "Capture Device: " << entity->name();

And maybe "Found capture device" to make it a bit more explicit ?

> +			/* With the parentList, we can follow back our way from
> +			 * the capture device to the sensor. */
> +			while (parentList.find(entity) != parentList.end()) {
> +				std::pair p = parentList[entity];
> +				Entity e { p.first, p.second };
> +				entities_.push_front(e);
> +				entity = p.first;
> +			}

We could avoid a double-lookup (in the condition of the while look, and
through the map's [] operator) with

			for (auto it = parents.find(entity); it != parents.end();
			     it = parents.find(entity)) {
				const Entity &e = it->second;
				entities_.push_front(e);
				entity = e.entity;
			}

> +			/* Finally also remember the sensor */
> +			sensor_ = std::make_unique<CameraSensor>(sensor);
> +			ret = sensor_->init();
> +			if (ret) {
> +				sensor_.reset();
>  			}
> -		}
> -
> -		if (!sourcePad)
> -			return;
> -
> -		entities_.push_back({ source, sourceLink });
> -
> -		source = sourceLink->sink()->entity();
> -
> -		/* Avoid infinite loops. */
> -		auto iter = std::find_if(entities_.begin(), entities_.end(),
> -					 [&](const Entity &entity) {
> -						 return entity.entity == source;
> -					 });
> -		if (iter != entities_.end()) {
> -			LOG(SimplePipeline, Info) << "Loop detected in pipeline";
>  			return;
>  		}
> -	}
> -
> -	/*
> -	 * We have a valid pipeline, get the video device and create the camera
> -	 * sensor.
> -	 */
> -	video_ = pipe->video(source);
> -	if (!video_)
> -		return;
> -
> -	sensor_ = std::make_unique<CameraSensor>(sensor);
> -	ret = sensor_->init();
> -	if (ret) {
> -		sensor_.reset();
> -		return;
> +		/* The actual uniform-cost search algorithm */
> +		visited.insert(entity);
> +		for (MediaPad *pad : entity->pads()) {
> +			if (pad->flags() & MEDIA_PAD_FL_SOURCE) {

			if (!(pad->flags() & MEDIA_PAD_FL_SOURCE))
				continue;

will reduce indentation of the next block.

> +				for (MediaLink *link : pad->links()) {
> +					if ((link->flags() & MEDIA_LNK_FL_ENABLED) ||
> +					    !(link->flags() & MEDIA_LNK_FL_IMMUTABLE)) {

You can drop this check, as immutable links are always enabled.

> +						MediaEntity *next = link->sink()->entity();
> +						if (visited.find(next) == visited.end()) {
> +							queue.push(next);
> +							parentList.insert( { next, { entity, link } });
> +						}
> +					}
> +				}
> +			}
> +		}
>  	}
>  }

As I've implemented the changes described above in order to test the
code on i.MX7D, here's the result to save you from having to duplicate
the work. Feel free to include any of this in v2 after testing it on the
i.MX6Q.

diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp
index c0f870de9b2e..449c30651876 100644
--- a/src/libcamera/pipeline/simple/simple.cpp
+++ b/src/libcamera/pipeline/simple/simple.cpp
@@ -15,6 +15,7 @@
 #include <set>
 #include <string>
 #include <string.h>
+#include <unordered_map>
 #include <utility>
 #include <vector>
 
@@ -272,63 +273,71 @@ SimpleCameraData::SimpleCameraData(SimplePipelineHandler *pipe,
 	int ret;
 
 	/*
-	 * Walk the pipeline towards the video node and store all entities
-	 * along the way.
+	 * Find the shortest path from the camera sensor to a video capture
+	 * device using the uniform-cost search algorithm. This heuristic will
+	 * be most likely to skip paths that aren't suitable for the simple
+	 * pipeline handler on more complex devices, and is guaranteed to
+	 * produce a valid path on all devices that have a single option.
+	 *
+	 * For instance, on the IPU-based i.MX6Q, the shortest path will skip
+	 * encoders and image converters, and will end in a CSI capture device.
 	 */
-	MediaEntity *source = sensor;
+	std::unordered_set<MediaEntity *> visited;
+	std::queue<MediaEntity *> queue;
 
-	while (source) {
-		/* If we have reached a video node, we're done. */
-		if (source->function() == MEDIA_ENT_F_IO_V4L)
+	/* Remember at each entity where we came from. */
+	std::unordered_map<MediaEntity *, Entity> parents;
+	queue.push(sensor);
+
+	MediaEntity *entity = nullptr;
+
+	while (!queue.empty()) {
+		entity = queue.back();
+		queue.pop();
+
+		/* Found the capture device. */
+		if (entity->function() == MEDIA_ENT_F_IO_V4L) {
+			LOG(SimplePipeline, Debug)
+				<< "Found capture device " << entity->name();
+			video_ = pipe->video(entity);
 			break;
+		}
 
-		/*
-		 * Use the first output pad that has links and follow its first
-		 * link.
-		 */
-		MediaPad *sourcePad = nullptr;
-		MediaLink *sourceLink = nullptr;
-		for (MediaPad *pad : source->pads()) {
-			if ((pad->flags() & MEDIA_PAD_FL_SOURCE) &&
-			    !pad->links().empty()) {
-				sourcePad = pad;
-				sourceLink = pad->links().front();
-				break;
+		/* The actual uniform-cost search algorithm. */
+		visited.insert(entity);
+		for (MediaPad *pad : entity->pads()) {
+			if (!(pad->flags() & MEDIA_PAD_FL_SOURCE))
+				continue;
+
+			for (MediaLink *link : pad->links()) {
+				MediaEntity *next = link->sink()->entity();
+				if (visited.find(next) == visited.end()) {
+					queue.push(next);
+					parents.insert({ next, { entity, link } });
+				}
 			}
 		}
-
-		if (!sourcePad)
-			return;
-
-		entities_.push_back({ source, sourceLink });
-
-		source = sourceLink->sink()->entity();
-
-		/* Avoid infinite loops. */
-		auto iter = std::find_if(entities_.begin(), entities_.end(),
-					 [&](const Entity &entity) {
-						 return entity.entity == source;
-					 });
-		if (iter != entities_.end()) {
-			LOG(SimplePipeline, Info) << "Loop detected in pipeline";
-			return;
-		}
 	}
 
-	/*
-	 * We have a valid pipeline, get the video device and create the camera
-	 * sensor.
-	 */
-	video_ = pipe->video(source);
 	if (!video_)
 		return;
 
+	/*
+	 * With the parents, we can follow back our way from the capture device
+	 * to the sensor.
+	 */
+	for (auto it = parents.find(entity); it != parents.end();
+	     it = parents.find(entity)) {
+		const Entity &e = it->second;
+		entities_.push_front(e);
+		entity = e.entity;
+	}
+
+	/* Finally also remember the sensor. */
 	sensor_ = std::make_unique<CameraSensor>(sensor);
 	ret = sensor_->init();
-	if (ret) {
+	if (ret)
 		sensor_.reset();
-		return;
-	}
 }
 
 int SimpleCameraData::init()

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp
index 1fcf3671..82a3b402 100644
--- a/src/libcamera/pipeline/simple/simple.cpp
+++ b/src/libcamera/pipeline/simple/simple.cpp
@@ -274,62 +274,62 @@  SimpleCameraData::SimpleCameraData(SimplePipelineHandler *pipe,
 	int ret;
 
 	/*
-	 * Walk the pipeline towards the video node and store all entities
-	 * along the way.
+	 * Use the uniform-cost search algorithm to find the shortest path to a
+	 * capture device. The shortest path has a good chance to end in a
+	 * CSI capture device, as this is the most direct path for a camera.
+	 * There are other capture devices which ends in encoders or image
+	 * converters which will write the capture image to RAM. These paths
+	 * would be invalid for the purpose of SimplePipeline. These paths are
+	 * also usually longer as they would naturally contain IC- or encoder
+	 * IPU-blocks. Therefore, this algorithm will avoid these paths.
 	 */
-	MediaEntity *source = sensor;
-
-	while (source) {
-		/* If we have reached a video node, we're done. */
-		if (source->function() == MEDIA_ENT_F_IO_V4L)
-			break;
-
-		/*
-		 * Use the first output pad that has links and follow its first
-		 * link.
-		 */
-		MediaPad *sourcePad = nullptr;
-		MediaLink *sourceLink = nullptr;
-		for (MediaPad *pad : source->pads()) {
-			if ((pad->flags() & MEDIA_PAD_FL_SOURCE) &&
-			    !pad->links().empty()) {
-				sourcePad = pad;
-				sourceLink = pad->links().front();
-				break;
+	std::unordered_set<MediaEntity*> visited;
+	std::queue<MediaEntity*> queue;
+
+	/* Remember at each entity where we came from */
+	std::unordered_map<MediaEntity*, std::pair<MediaEntity*, MediaLink*>> parentList;
+	queue.push(sensor);
+
+	while (!queue.empty()) {
+		MediaEntity *entity = queue.back();
+		queue.pop();
+		/* Found the capture device */
+		if (entity->function() == MEDIA_ENT_F_IO_V4L) {
+			video_ = pipe->video(entity);
+			LOG(SimplePipeline, Debug) << "Capture Device: "
+						   << entity->name();
+			/* With the parentList, we can follow back our way from
+			 * the capture device to the sensor. */
+			while (parentList.find(entity) != parentList.end()) {
+				std::pair p = parentList[entity];
+				Entity e { p.first, p.second };
+				entities_.push_front(e);
+				entity = p.first;
+			}
+			/* Finally also remember the sensor */
+			sensor_ = std::make_unique<CameraSensor>(sensor);
+			ret = sensor_->init();
+			if (ret) {
+				sensor_.reset();
 			}
-		}
-
-		if (!sourcePad)
-			return;
-
-		entities_.push_back({ source, sourceLink });
-
-		source = sourceLink->sink()->entity();
-
-		/* Avoid infinite loops. */
-		auto iter = std::find_if(entities_.begin(), entities_.end(),
-					 [&](const Entity &entity) {
-						 return entity.entity == source;
-					 });
-		if (iter != entities_.end()) {
-			LOG(SimplePipeline, Info) << "Loop detected in pipeline";
 			return;
 		}
-	}
-
-	/*
-	 * We have a valid pipeline, get the video device and create the camera
-	 * sensor.
-	 */
-	video_ = pipe->video(source);
-	if (!video_)
-		return;
-
-	sensor_ = std::make_unique<CameraSensor>(sensor);
-	ret = sensor_->init();
-	if (ret) {
-		sensor_.reset();
-		return;
+		/* The actual uniform-cost search algorithm */
+		visited.insert(entity);
+		for (MediaPad *pad : entity->pads()) {
+			if (pad->flags() & MEDIA_PAD_FL_SOURCE) {
+				for (MediaLink *link : pad->links()) {
+					if ((link->flags() & MEDIA_LNK_FL_ENABLED) ||
+					    !(link->flags() & MEDIA_LNK_FL_IMMUTABLE)) {
+						MediaEntity *next = link->sink()->entity();
+						if (visited.find(next) == visited.end()) {
+							queue.push(next);
+							parentList.insert( { next, { entity, link } });
+						}
+					}
+				}
+			}
+		}
 	}
 }