{"id":21614,"url":"https://patchwork.libcamera.org/api/patches/21614/?format=json","web_url":"https://patchwork.libcamera.org/patch/21614/","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":"<20241014095937.24924-3-jacopo.mondi@ideasonboard.com>","date":"2024-10-14T09:59:34","name":"[RFC,2/4] 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/21614/mbox/","series":[{"id":4695,"url":"https://patchwork.libcamera.org/api/series/4695/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4695","date":"2024-10-14T09:59:32","name":"libcamera: Implement YamlEmitter","version":1,"mbox":"https://patchwork.libcamera.org/series/4695/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21614/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21614/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 C79EFC326C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 14 Oct 2024 10:00:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E4D5265383;\n\tMon, 14 Oct 2024 12:00:01 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3CDB265371\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 14 Oct 2024 11:59:58 +0200 (CEST)","from ideasonboard.com (unknown [5.77.95.224])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 117458BE;\n\tMon, 14 Oct 2024 11:58:16 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ISMwRoyO\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1728899897;\n\tbh=bE3dig4hKkrj14JN455AaVX1K8ENqB8xqqOqAdnaBdc=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=ISMwRoyOeStQQs1iodu6EHiCp9AlgwUqHtj96WIUmCSl0ZRt/E4D8/rKSvWtbn9Tw\n\tHrUhWCn4q4Z18zqSCYS9VjQKYvK2xSMpKzCqWvwpivMEoxMrJXgEKEGxaDilk0qaOM\n\tliVSxEwxM2+XbZJjkoMD64H4VZ0GmK4JpKwZih9I=","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":"[RFC 2/4] apps: cam: Add support for loading configuration from\n\tcapture script","Date":"Mon, 14 Oct 2024 11:59:34 +0200","Message-ID":"<20241014095937.24924-3-jacopo.mondi@ideasonboard.com>","X-Mailer":"git-send-email 2.46.2","In-Reply-To":"<20241014095937.24924-1-jacopo.mondi@ideasonboard.com>","References":"<20241014095937.24924-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>\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 edc49b875450..6f1d8b58870f 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":["RFC","2/4"]}