{"id":17748,"url":"https://patchwork.libcamera.org/api/patches/17748/?format=json","web_url":"https://patchwork.libcamera.org/patch/17748/","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":"<20221102160556.33086-1-jacopo@jmondi.org>","date":"2022-11-02T16:05:56","name":"[libcamera-devel] cam: capture_script: Support parsing array controls","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"a533af59c49c5518b862685f1cd12eea60e4bbf3","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17748/mbox/","series":[{"id":3594,"url":"https://patchwork.libcamera.org/api/series/3594/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3594","date":"2022-11-02T16:05:56","name":"[libcamera-devel] cam: capture_script: Support parsing array controls","version":1,"mbox":"https://patchwork.libcamera.org/series/3594/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17748/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17748/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 A0B2ABD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  2 Nov 2022 16:06:04 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id F0ADE63075;\n\tWed,  2 Nov 2022 17:06:03 +0100 (CET)","from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net\n\t[IPv6:2001:4b98:dc4:8::228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BF25E63075\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  2 Nov 2022 17:06:02 +0100 (CET)","(Authenticated sender: jacopo@jmondi.org)\n\tby mail.gandi.net (Postfix) with ESMTPSA id 33F181BF211;\n\tWed,  2 Nov 2022 16:06:01 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1667405164;\n\tbh=kWJCwMrYtYA7YYsvAfUaBZPCdyByOq/pHBM5DthqQ1U=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:From;\n\tb=HWyQyh/3V+McJwkF9+ksl+hiT6joZWeDWijxTb2juNdEH8PShSinrHuTMe/6Dx5j6\n\tEfnhmz0dVbI0cu6sc4sbbyXeAn6s1Nn7OrErcqVr5DNVr1FutY8tvU/YKJvnNSC0x9\n\txFVpE3ueGaH08ujTSgFvgQ6qJ1gvJ7eiblulkS9ufcnks6S/gTyhu5b9y5GRL8SPv0\n\t0AzfOSFB82oEvTYpXvhCVAGLDPpM3TCvV0hskXmJk2eWN3gZjSoPhwMXpcRNlphPnM\n\tNk4IQQUhluyjgbqLFQ1m8lmEI0re120u8H75cQnRpTkpFWu8e2eeINS3yEAh4JICRg\n\t4Hhmg3HjkdsaQ==","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  2 Nov 2022 17:05:56 +0100","Message-Id":"<20221102160556.33086-1-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.38.1","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH] cam: capture_script: Support parsing\n\tarray controls","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>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo@jmondi.org>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add support for parsing array controls to the cam capture script.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n\n---\nv1->v2:\n- Rebased on src/apps/\n- Use the correct size when parsing boolean controls as noted by Paul\n---\n src/apps/cam/capture_script.cpp | 158 ++++++++++++++++++++++++++++----\n src/apps/cam/capture_script.h   |   5 +\n 2 files changed, 146 insertions(+), 17 deletions(-)\n\n--\n2.38.1","diff":"diff --git a/src/apps/cam/capture_script.cpp b/src/apps/cam/capture_script.cpp\nindex 5a27361ce4d9..062a7258e414 100644\n--- a/src/apps/cam/capture_script.cpp\n+++ b/src/apps/cam/capture_script.cpp\n@@ -454,24 +454,9 @@ void CaptureScript::unpackFailure(const ControlId *id, const std::string &repr)\n \t\t  << typeName << \" control \" << id->name() << std::endl;\n }\n\n-ControlValue CaptureScript::unpackControl(const ControlId *id)\n+ControlValue CaptureScript::parseScalarControl(const ControlId *id,\n+\t\t\t\t\t       const std::string repr)\n {\n-\t/* Parse complex types. */\n-\tswitch (id->type()) {\n-\tcase ControlTypeRectangle:\n-\t\treturn parseRectangles();\n-\tcase ControlTypeSize:\n-\t\t/* \\todo Parse Sizes. */\n-\t\treturn {};\n-\tdefault:\n-\t\tbreak;\n-\t}\n-\n-\t/* Parse basic types represented by a single scalar. */\n-\tconst std::string repr = parseScalar();\n-\tif (repr.empty())\n-\t\treturn {};\n-\n \tControlValue value{};\n\n \tswitch (id->type()) {\n@@ -524,6 +509,145 @@ ControlValue CaptureScript::unpackControl(const ControlId *id)\n \treturn value;\n }\n\n+ControlValue CaptureScript::parseArrayControl(const ControlId *id,\n+\t\t\t\t\t      const std::vector<std::string> &repr)\n+{\n+\tControlValue value{};\n+\n+\tswitch (id->type()) {\n+\tcase ControlTypeNone:\n+\t\tbreak;\n+\tcase ControlTypeBool: {\n+\t\t/*\n+\t\t * This is unpleasant, but we cannot use an std::vector<> as its\n+\t\t * boolean type overload does not allow to access the raw data,\n+\t\t * as boolean values are stored in a bitmask for efficiency.\n+\t\t *\n+\t\t * As we need a contiguous memory region to wrap in a Span<>,\n+\t\t * use an array instead but be strict about not overflowing it\n+\t\t * by limiting the number of controls we can store.\n+\t\t *\n+\t\t * Be loud but do not fail, as the issue would present at\n+\t\t * runtime and it's not fatal.\n+\t\t */\n+\t\tstatic constexpr unsigned int kMaxNumBooleanControls = 1024;\n+\t\tstd::array<bool, kMaxNumBooleanControls> values;\n+\t\tunsigned int idx = 0;\n+\n+\t\tfor (const std::string &s : repr) {\n+\t\t\tbool val;\n+\n+\t\t\tif (s == \"true\") {\n+\t\t\t\tval = true;\n+\t\t\t} else if (s == \"false\") {\n+\t\t\t\tval = false;\n+\t\t\t} else {\n+\t\t\t\tunpackFailure(id, s);\n+\t\t\t\treturn value;\n+\t\t\t}\n+\n+\t\t\tif (idx == kMaxNumBooleanControls) {\n+\t\t\t\tstd::cerr << \"Cannot parse more than \"\n+\t\t\t\t\t  << kMaxNumBooleanControls\n+\t\t\t\t\t  << \" boolean controls\" << std::endl;\n+\t\t\t\tbreak;\n+\t\t\t}\n+\n+\t\t\tvalues[idx++] = val;\n+\t\t}\n+\n+\t\tvalue = Span<bool>(values.data(), idx);\n+\t\tbreak;\n+\t}\n+\tcase ControlTypeByte: {\n+\t\tstd::vector<uint8_t> values;\n+\t\tfor (const std::string &s : repr) {\n+\t\t\tuint8_t val = strtoll(s.c_str(), NULL, 10);\n+\t\t\tvalues.push_back(val);\n+\t\t}\n+\n+\t\tvalue = Span<const uint8_t>(values.data(), values.size());\n+\t\tbreak;\n+\t}\n+\tcase ControlTypeInteger32: {\n+\t\tstd::vector<int32_t> values;\n+\t\tfor (const std::string &s : repr) {\n+\t\t\tint32_t val = strtoll(s.c_str(), NULL, 10);\n+\t\t\tvalues.push_back(val);\n+\t\t}\n+\n+\t\tvalue = Span<const int32_t>(values.data(), values.size());\n+\t\tbreak;\n+\t}\n+\tcase ControlTypeInteger64: {\n+\t\tstd::vector<int64_t> values;\n+\t\tfor (const std::string &s : repr) {\n+\t\t\tint64_t val = strtoll(s.c_str(), NULL, 10);\n+\t\t\tvalues.push_back(val);\n+\t\t}\n+\n+\t\tvalue = Span<const int64_t>(values.data(), values.size());\n+\t\tbreak;\n+\t}\n+\tcase ControlTypeFloat: {\n+\t\tstd::vector<float> values;\n+\t\tfor (const std::string &s : repr)\n+\t\t\tvalues.push_back(strtof(s.c_str(), NULL));\n+\n+\t\tvalue = Span<const float>(values.data(), values.size());\n+\t\tbreak;\n+\t}\n+\tcase ControlTypeString: {\n+\t\tvalue = Span<const std::string>(repr.data(), repr.size());\n+\t\tbreak;\n+\t}\n+\tdefault:\n+\t\tstd::cerr << \"Unsupported control type\" << std::endl;\n+\t\tbreak;\n+\t}\n+\n+\treturn value;\n+}\n+\n+ControlValue CaptureScript::unpackControl(const ControlId *id)\n+{\n+\t/* Parse complex types. */\n+\tswitch (id->type()) {\n+\tcase ControlTypeRectangle:\n+\t\treturn parseRectangles();\n+\tcase ControlTypeSize:\n+\t\t/* \\todo Parse Sizes. */\n+\t\treturn {};\n+\tdefault:\n+\t\tbreak;\n+\t}\n+\n+\t/* Check if the control has a single scalar value or is an array. */\n+\tEventPtr event = nextEvent();\n+\tif (!event)\n+\t\treturn {};\n+\n+\tswitch (event->type) {\n+\tcase YAML_SCALAR_EVENT: {\n+\t\tconst std::string repr = eventScalarValue(event);\n+\t\tif (repr.empty())\n+\t\t\treturn {};\n+\n+\t\treturn parseScalarControl(id, repr);\n+\t}\n+\tcase YAML_SEQUENCE_START_EVENT: {\n+\t\tstd::vector<std::string> array = parseSingleArray();\n+\t\tif (array.empty())\n+\t\t\treturn {};\n+\n+\t\treturn parseArrayControl(id, array);\n+\t}\n+\tdefault:\n+\t\tstd::cerr << \"Unexpected event type: \" << event->type << std::endl;\n+\t\treturn {};\n+\t}\n+}\n+\n libcamera::Rectangle CaptureScript::unpackRectangle(const std::vector<std::string> &strVec)\n {\n \tint x = strtol(strVec[0].c_str(), NULL, 10);\ndiff --git a/src/apps/cam/capture_script.h b/src/apps/cam/capture_script.h\nindex 7a0ddebb00b5..40042c0330f0 100644\n--- a/src/apps/cam/capture_script.h\n+++ b/src/apps/cam/capture_script.h\n@@ -56,6 +56,11 @@ private:\n \tint parseFrame(EventPtr event);\n \tint parseControl(EventPtr event, libcamera::ControlList &controls);\n\n+\tlibcamera::ControlValue parseScalarControl(const libcamera::ControlId *id,\n+\t\t\t\t\t\t   const std::string repr);\n+\tlibcamera::ControlValue parseArrayControl(const libcamera::ControlId *id,\n+\t\t\t\t\t\t  const std::vector<std::string> &repr);\n+\n \tstd::string parseScalar();\n \tlibcamera::ControlValue parseRectangles();\n \tstd::vector<std::vector<std::string>> parseArrays();\n","prefixes":["libcamera-devel"]}