{"id":21821,"url":"https://patchwork.libcamera.org/api/patches/21821/?format=json","web_url":"https://patchwork.libcamera.org/patch/21821/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20241106175901.83960-4-jacopo.mondi@ideasonboard.com>","date":"2024-11-06T17:58:53","name":"[3/3] apps: cam: Add support for loading configuration from capture script","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"97b9b7c3a4809c339fd898686bb1f66e32417686","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/?format=json","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21821/mbox/","series":[{"id":4773,"url":"https://patchwork.libcamera.org/api/series/4773/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4773","date":"2024-11-06T17:58:50","name":"libcamera: Add support for dumping capture script in YAML","version":1,"mbox":"https://patchwork.libcamera.org/series/4773/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21821/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21821/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id BD05DC32CE\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  6 Nov 2024 17:59:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EE4746545E;\n\tWed,  6 Nov 2024 18:59:14 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B644865446\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  6 Nov 2024 18:59:10 +0100 (CET)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 33F46C80;\n\tWed,  6 Nov 2024 18:59:02 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"TEjGt3Ok\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1730915942;\n\tbh=LzkEqwnvZ8o5189gKCovTM9dpHA1Kq316oRZwbMntlc=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=TEjGt3OkmSGjCcLsfxiJoYRTgyknHp6Qz+bksRxrxcD1uF28uOXXtjTZHTgwUCyl8\n\thpuFqI5x8GBLdI88PdocyfIm8tP13y5wk0qBG9f/tLmlDUqHr7gUH/jFmHggWHtBY5\n\tucbAM5GZYL9Rro+HRMQyGr9BNtOv3fX2pjhR2e3o=","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>,\n\tPaul Elder <paul.elder@ideasonboard.com>","Subject":"[PATCH 3/3] apps: cam: Add support for loading configuration from\n\tcapture script","Date":"Wed,  6 Nov 2024 18:58:53 +0100","Message-ID":"<20241106175901.83960-4-jacopo.mondi@ideasonboard.com>","X-Mailer":"git-send-email 2.47.0","In-Reply-To":"<20241106175901.83960-1-jacopo.mondi@ideasonboard.com>","References":"<20241106175901.83960-1-jacopo.mondi@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Paul Elder <paul.elder@ideasonboard.com>\n\nAdd support to the cam application for loading the camera configuration\nfrom a capture script. These are not expected to be written by hand, but\nrather dumped via the LIBCAMERA_DUMP_CAPTURE_SCRIPT environment\nvariable.\n\nIf any configuration options are specified by command line parameters,\nthose will take precedence.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n src/apps/cam/camera_session.cpp |  22 +++--\n src/apps/cam/capture_script.cpp | 164 ++++++++++++++++++++++++++++++++\n src/apps/cam/capture_script.h   |   8 ++\n 3 files changed, 184 insertions(+), 10 deletions(-)","diff":"diff --git a/src/apps/cam/camera_session.cpp b/src/apps/cam/camera_session.cpp\nindex 6e9890ccfda1..f31b5282c706 100644\n--- a/src/apps/cam/camera_session.cpp\n+++ b/src/apps/cam/camera_session.cpp\n@@ -70,6 +70,18 @@ CameraSession::CameraSession(CameraManager *cm,\n \t\treturn;\n \t}\n \n+\tif (options_.isSet(OptCaptureScript)) {\n+\t\tstd::string scriptName = options_[OptCaptureScript].toString();\n+\t\tscript_ = std::make_unique<CaptureScript>(camera_, scriptName);\n+\t\tif (!script_->valid()) {\n+\t\t\tstd::cerr << \"Invalid capture script '\" << scriptName\n+\t\t\t\t  << \"'\" << std::endl;\n+\t\t\treturn;\n+\t\t}\n+\n+\t\tscript_->populateConfiguration(config.get());\n+\t}\n+\n \tif (options_.isSet(OptOrientation)) {\n \t\tstd::string orientOpt = options_[OptOrientation].toString();\n \t\tstatic const std::map<std::string, libcamera::Orientation> orientations{\n@@ -119,16 +131,6 @@ CameraSession::CameraSession(CameraManager *cm,\n \t}\n #endif\n \n-\tif (options_.isSet(OptCaptureScript)) {\n-\t\tstd::string scriptName = options_[OptCaptureScript].toString();\n-\t\tscript_ = std::make_unique<CaptureScript>(camera_, scriptName);\n-\t\tif (!script_->valid()) {\n-\t\t\tstd::cerr << \"Invalid capture script '\" << scriptName\n-\t\t\t\t  << \"'\" << std::endl;\n-\t\t\treturn;\n-\t\t}\n-\t}\n-\n \tswitch (config->validate()) {\n \tcase CameraConfiguration::Valid:\n \t\tbreak;\ndiff --git a/src/apps/cam/capture_script.cpp b/src/apps/cam/capture_script.cpp\nindex fc1dfa75f2d4..7f166f45657e 100644\n--- a/src/apps/cam/capture_script.cpp\n+++ b/src/apps/cam/capture_script.cpp\n@@ -7,6 +7,7 @@\n \n #include \"capture_script.h\"\n \n+#include <algorithm>\n #include <iostream>\n #include <stdio.h>\n #include <stdlib.h>\n@@ -162,6 +163,10 @@ int CaptureScript::parseScript(FILE *script)\n \t\t\tret = parseFrames();\n \t\t\tif (ret)\n \t\t\t\treturn ret;\n+\t\t} else if (section == \"configuration\") {\n+\t\t\tret = parseConfiguration();\n+\t\t\tif (ret)\n+\t\t\t\treturn ret;\n \t\t} else {\n \t\t\tstd::cerr << \"Unsupported section '\" << section << \"'\"\n \t\t\t\t  << std::endl;\n@@ -322,6 +327,165 @@ int CaptureScript::parseControl(EventPtr event, ControlList &controls)\n \treturn 0;\n }\n \n+int CaptureScript::parseOrientation(EventPtr event)\n+{\n+\tstatic const std::map<std::string, libcamera::Orientation> orientations{\n+\t\t{ \"Rotate0\", libcamera::Orientation::Rotate0 },\n+\t\t{ \"Rotate0Mirror\", libcamera::Orientation::Rotate0Mirror },\n+\t\t{ \"Rotate180\", libcamera::Orientation::Rotate180 },\n+\t\t{ \"Rotate180Mirror\", libcamera::Orientation::Rotate180Mirror },\n+\t\t{ \"Rotate90Mirror\", libcamera::Orientation::Rotate90Mirror },\n+\t\t{ \"Rotate270\", libcamera::Orientation::Rotate270 },\n+\t\t{ \"Rotate270Mirror\", libcamera::Orientation::Rotate270Mirror },\n+\t\t{ \"Rotate90\", libcamera::Orientation::Rotate90 },\n+\t};\n+\n+\tstd::string orientation = eventScalarValue(event);\n+\n+\tauto it = orientations.find(orientation);\n+\tif (it == orientations.end()) {\n+\t\tstd::cerr << \"Invalid orientation '\" << orientation\n+\t\t\t  << \"' in capture script\" << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\torientation_ = it->second;\n+\n+\treturn 0;\n+}\n+\n+int CaptureScript::parseStream(EventPtr event, unsigned int index)\n+{\n+\tif (!checkEvent(event, YAML_MAPPING_START_EVENT))\n+\t\treturn -EINVAL;\n+\n+\tStreamConfiguration config;\n+\twhile (1) {\n+\t\tevent = nextEvent();\n+\t\tif (!event)\n+\t\t\treturn -EINVAL;\n+\t\tif (event->type == YAML_MAPPING_END_EVENT)\n+\t\t\tbreak;\n+\n+\t\tstd::string key = eventScalarValue(event);\n+\n+\t\tevent = nextEvent();\n+\t\tif (!event)\n+\t\t\treturn -EINVAL;\n+\t\tif (event->type == YAML_MAPPING_END_EVENT)\n+\t\t\tbreak;\n+\n+\t\tstd::string value = eventScalarValue(event);\n+\n+\t\tif (key == \"pixelFormat\") {\n+\t\t\tconfig.pixelFormat = libcamera::PixelFormat::fromString(value);\n+\t\t} else if (key == \"size\") {\n+\t\t\tunsigned int split = value.find(\"x\");\n+\t\t\tif (split == std::string::npos) {\n+\t\t\t\tstd::cerr << \"Invalid size '\" << value\n+\t\t\t\t\t  << \"' in stream configuration \"\n+\t\t\t\t\t  << index << std::endl;\n+\t\t\t}\n+\n+\t\t\tstd::string width = value.substr(0, split);\n+\t\t\tstd::string height = value.substr(split + 1);\n+\t\t\tconfig.size = Size(std::stoi(width), std::stoi(height));\n+\t\t} else if (key == \"stride\") {\n+\t\t\tconfig.stride = std::stoi(value);\n+\t\t} else if (key == \"frameSize\") {\n+\t\t\tconfig.frameSize = std::stoi(value);\n+\t\t} else if (key == \"bufferCount\") {\n+\t\t\tconfig.bufferCount = std::stoi(value);\n+\t\t} else if (key == \"colorSpace\") {\n+\t\t\tconfig.colorSpace = libcamera::ColorSpace::fromString(value);\n+\t\t} else {\n+\t\t\tstd::cerr << \"Unknown key-value pair '\"\n+\t\t\t\t  << key << \"': '\" << value\n+\t\t\t\t  << \"' in stream configuration \"\n+\t\t\t\t  << index << std::endl;\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t}\n+\n+\tstreamConfigs_.push_back(config);\n+\n+\treturn 0;\n+}\n+\n+int CaptureScript::parseStreams(EventPtr event)\n+{\n+\tif (!checkEvent(event, YAML_SEQUENCE_START_EVENT))\n+\t\treturn -EINVAL;\n+\n+\tunsigned int index = 0;\n+\twhile (1) {\n+\t\tevent = nextEvent();\n+\t\tif (!event)\n+\t\t\treturn -EINVAL;\n+\t\tif (event->type == YAML_SEQUENCE_END_EVENT)\n+\t\t\treturn 0;\n+\n+\t\tif (event->type == YAML_MAPPING_START_EVENT) {\n+\t\t\tparseStream(std::move(event), index++);\n+\t\t\tcontinue;\n+\t\t} else {\n+\t\t\tstd::cerr << \"UNKNOWN TYPE\" << std::endl;\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n+int CaptureScript::parseConfiguration()\n+{\n+\tint ret;\n+\n+\tEventPtr event = nextEvent(YAML_MAPPING_START_EVENT);\n+\tif (!event)\n+\t\treturn -EINVAL;\n+\n+\twhile (1) {\n+\t\tevent = nextEvent();\n+\t\tif (!event)\n+\t\t\treturn -EINVAL;\n+\t\tif (event->type == YAML_MAPPING_END_EVENT)\n+\t\t\tbreak;\n+\n+\t\tstd::string key = eventScalarValue(event);\n+\n+\t\tevent = nextEvent();\n+\t\tif (!event)\n+\t\t\treturn -EINVAL;\n+\t\tif (event->type == YAML_MAPPING_END_EVENT)\n+\t\t\tbreak;\n+\n+\t\t/* TODO Load sensor configuration */\n+\t\tif (key == \"orientation\") {\n+\t\t\tret = parseOrientation(std::move(event));\n+\t\t\tif (ret)\n+\t\t\t\treturn ret;\n+\t\t} else if (key == \"streams\") {\n+\t\t\tret = parseStreams(std::move(event));\n+\t\t\tif (ret)\n+\t\t\t\treturn ret;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\n+\n+void CaptureScript::populateConfiguration(CameraConfiguration *configuration) const\n+{\n+\tif (!configuration)\n+\t\treturn;\n+\n+\tconfiguration->orientation = orientation_;\n+\n+\tfor (unsigned int i = 0; i < streamConfigs_.size(); i++)\n+\t\t(*configuration)[i] = streamConfigs_[i];\n+}\n+\n std::string CaptureScript::parseScalar()\n {\n \tEventPtr event = nextEvent(YAML_SCALAR_EVENT);\ndiff --git a/src/apps/cam/capture_script.h b/src/apps/cam/capture_script.h\nindex 294b920363ba..4ba862d742cf 100644\n--- a/src/apps/cam/capture_script.h\n+++ b/src/apps/cam/capture_script.h\n@@ -26,6 +26,7 @@ public:\n \n \tconst libcamera::ControlList &frameControls(unsigned int frame);\n \n+\tvoid populateConfiguration(libcamera::CameraConfiguration *configuration) const;\n private:\n \tstruct EventDeleter {\n \t\tvoid operator()(yaml_event_t *event) const\n@@ -43,6 +44,9 @@ private:\n \tunsigned int loop_;\n \tbool valid_;\n \n+\tlibcamera::Orientation orientation_;\n+\tstd::vector<libcamera::StreamConfiguration> streamConfigs_;\n+\n \tEventPtr nextEvent(yaml_event_type_t expectedType = YAML_NO_EVENT);\n \tbool checkEvent(const EventPtr &event, yaml_event_type_t expectedType) const;\n \tstatic std::string eventScalarValue(const EventPtr &event);\n@@ -55,6 +59,10 @@ private:\n \tint parseFrames();\n \tint parseFrame(EventPtr event);\n \tint parseControl(EventPtr event, libcamera::ControlList &controls);\n+\tint parseConfiguration();\n+\tint parseOrientation(EventPtr event);\n+\tint parseStreams(EventPtr event);\n+\tint parseStream(EventPtr event, unsigned int index);\n \n \tlibcamera::ControlValue parseScalarControl(const libcamera::ControlId *id,\n \t\t\t\t\t\t   const std::string repr);\n","prefixes":["3/3"]}