From patchwork Tue Mar 9 11:29:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marian Cichy X-Patchwork-Id: 11537 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D7082BD80C for ; Tue, 9 Mar 2021 11:30:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EB2BD6051F; Tue, 9 Mar 2021 12:30:33 +0100 (CET) Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 956646051F for ; Tue, 9 Mar 2021 12:30:31 +0100 (CET) Received: from dude02.hi.pengutronix.de ([2001:67c:670:100:1d::28]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lJaZK-000841-SU; Tue, 09 Mar 2021 12:30:30 +0100 Received: from mci by dude02.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lJaZK-0007So-KJ; Tue, 09 Mar 2021 12:30:30 +0100 From: Marian Cichy To: libcamera-devel@lists.libcamera.org Date: Tue, 9 Mar 2021 12:29:48 +0100 Message-Id: <20210309112949.26251-2-m.cichy@pengutronix.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210309112949.26251-1-m.cichy@pengutronix.de> References: <20210309112949.26251-1-m.cichy@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::28 X-SA-Exim-Mail-From: mci@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH 1/2] media_entity: Overload == operator to use media-entities in maps X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: graphics@pengutronix.de, Marian Cichy Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Signed-off-by: Marian Cichy --- include/libcamera/internal/media_object.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/libcamera/internal/media_object.h b/include/libcamera/internal/media_object.h index 1c82c279..1ed95863 100644 --- a/include/libcamera/internal/media_object.h +++ b/include/libcamera/internal/media_object.h @@ -102,6 +102,8 @@ public: int setDeviceNode(const std::string &deviceNode); + bool operator==(const MediaEntity &other) { return name() == other.name(); } + private: LIBCAMERA_DISABLE_COPY_AND_MOVE(MediaEntity) From patchwork Tue Mar 9 11:29:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marian Cichy X-Patchwork-Id: 11536 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 09F8FBD80C for ; Tue, 9 Mar 2021 11:30:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5C1C468AA3; Tue, 9 Mar 2021 12:30:33 +0100 (CET) Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 92AF1602E6 for ; Tue, 9 Mar 2021 12:30:31 +0100 (CET) Received: from dude02.hi.pengutronix.de ([2001:67c:670:100:1d::28]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lJaZK-000842-TG; Tue, 09 Mar 2021 12:30:30 +0100 Received: from mci by dude02.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lJaZK-0007Sr-Ks; Tue, 09 Mar 2021 12:30:30 +0100 From: Marian Cichy To: libcamera-devel@lists.libcamera.org Date: Tue, 9 Mar 2021 12:29:49 +0100 Message-Id: <20210309112949.26251-3-m.cichy@pengutronix.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210309112949.26251-1-m.cichy@pengutronix.de> References: <20210309112949.26251-1-m.cichy@pengutronix.de> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::28 X-SA-Exim-Mail-From: mci@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH 2/2] pipeline: simple: Use uniform-cost search algorithm to setup media pipeline X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: graphics@pengutronix.de, Marian Cichy Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 --- 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. */ - 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 visited; + std::queue queue; + + /* Remember at each entity where we came from */ + std::unordered_map> 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(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(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 } }); + } + } + } + } + } } }