Patch Detail
Show a patch.
GET /api/1.1/patches/19830/?format=api
{ "id": 19830, "url": "https://patchwork.libcamera.org/api/1.1/patches/19830/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19830/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20240404084657.353464-11-mzamazal@redhat.com>", "date": "2024-04-04T08:46:47", "name": "[v7,10/18] libcamera: ipa: add Soft IPA", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "20f880f4244bfdf3dd783cff4886f9620fc40f76", "submitter": { "id": 177, "url": "https://patchwork.libcamera.org/api/1.1/people/177/?format=api", "name": "Milan Zamazal", "email": "mzamazal@redhat.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/19830/mbox/", "series": [ { "id": 4246, "url": "https://patchwork.libcamera.org/api/1.1/series/4246/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4246", "date": "2024-04-04T08:46:37", "name": "libcamera: introduce Software ISP and Software IPA", "version": 7, "mbox": "https://patchwork.libcamera.org/series/4246/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19830/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19830/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 C4F03C32CE\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 4 Apr 2024 08:47:50 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 51F246338A;\n\tThu, 4 Apr 2024 10:47:50 +0200 (CEST)", "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 077556338F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 4 Apr 2024 10:47:47 +0200 (CEST)", "from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com\n\t[66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-79-wk8TaXLUPce7smQ4usoPCw-1; Thu, 04 Apr 2024 04:47:43 -0400", "from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mimecast-mx02.redhat.com (Postfix) with ESMTPS id 98F50101A562;\n\tThu, 4 Apr 2024 08:47:42 +0000 (UTC)", "from nuthatch.brq.redhat.com (unknown [10.43.17.39])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id E426E8173;\n\tThu, 4 Apr 2024 08:47:40 +0000 (UTC)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"TgEnTONT\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1712220466;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=qbpxnGBdpSyqNezYdl+6Vod+n+Xxvs94M+E0KM43dAM=;\n\tb=TgEnTONTtDA1XI7DUEKLhTl4kqHnASF4ZdKk8ahpt2Nf52Pvxq6DqpOfgSuPDX+auNYmx4\n\tKFramhSUipX9u5QG9Cylocn7a46zYmfKbS0oJL/APlA2bScR3MNOggTpITat3mZfKXKrUU\n\tssugZQHUQvMguBIlbFUmI5NIdekVJ+g=", "X-MC-Unique": "wk8TaXLUPce7smQ4usoPCw-1", "From": "Milan Zamazal <mzamazal@redhat.com>", "To": "libcamera-devel@lists.libcamera.org", "Cc": "Andrey Konovalov <andrey.konovalov@linaro.org>,\n\tAndrei Konovalov <andrey.konovalov.ynk@gmail.com>,\n\tBryan O'Donoghue <bryan.odonoghue@linaro.org>,\n\tMaxime Ripard <mripard@redhat.com>, Milan Zamazal <mzamazal@redhat.com>, \n\tPavel Machek <pavel@ucw.cz>, Hans de Goede <hdegoede@redhat.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tDennis Bonke <admin@dennisbonke.com>, Marttico <g.martti@gmail.com>, \n\tToon Langendam <t.langendam@gmail.com>", "Subject": "[PATCH v7 10/18] libcamera: ipa: add Soft IPA", "Date": "Thu, 4 Apr 2024 10:46:47 +0200", "Message-ID": "<20240404084657.353464-11-mzamazal@redhat.com>", "In-Reply-To": "<20240404084657.353464-1-mzamazal@redhat.com>", "References": "<20240404084657.353464-1-mzamazal@redhat.com>", "MIME-Version": "1.0", "X-Scanned-By": "MIMEDefang 3.4.1 on 10.11.54.5", "X-Mimecast-Spam-Score": "0", "X-Mimecast-Originator": "redhat.com", "Content-Transfer-Encoding": "8bit", "Content-Type": "text/plain; charset=\"US-ASCII\"; x-default=true", "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: Andrey Konovalov <andrey.konovalov@linaro.org>\n\nDefine the Soft IPA main and event interfaces, add the Soft IPA\nimplementation.\n\nThe current src/ipa/meson.build assumes the IPA name to match the\npipeline name. For this reason \"-Dipas=simple\" is used for the\nSoft IPA module.\n\nAuto exposure/gain and AWB implementation by Dennis, Toon and Martti.\n\nAuto exposure/gain targets a Mean Sample Value of 2.5 following\nthe MSV calculation algorithm from:\nhttps://www.araa.asn.au/acra/acra2007/papers/paper84final.pdf\n\nUse CameraSensorHelper to convert the analogue gain code read from the\ncamera sensor into real analogue gain value. In the future this makes\nit possible to use faster AE/AGC algorithm. Right now the CameraSensorHelper\nlets us use the full range of analogue gain values.\n\nIf there is no CameraSensorHelper for the camera sensor in use, a\nwarning log message is printed.\n\nTested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org> # sc8280xp Lenovo x13s\nTested-by: Pavel Machek <pavel@ucw.cz>\nReviewed-by: Pavel Machek <pavel@ucw.cz>\nSigned-off-by: Andrey Konovalov <andrey.konovalov@linaro.org>\nCo-developed-by: Dennis Bonke <admin@dennisbonke.com>\nSigned-off-by: Dennis Bonke <admin@dennisbonke.com>\nCo-developed-by: Marttico <g.martti@gmail.com>\nSigned-off-by: Marttico <g.martti@gmail.com>\nCo-developed-by: Toon Langendam <t.langendam@gmail.com>\nSigned-off-by: Toon Langendam <t.langendam@gmail.com>\nSigned-off-by: Hans de Goede <hdegoede@redhat.com>\n---\n Documentation/Doxyfile.in | 1 +\n include/libcamera/ipa/meson.build | 1 +\n include/libcamera/ipa/soft.mojom | 28 ++\n meson_options.txt | 2 +-\n src/ipa/meson.build | 3 +\n src/ipa/simple/data/meson.build | 10 +\n src/ipa/simple/data/uncalibrated.yaml | 5 +\n src/ipa/simple/meson.build | 25 ++\n src/ipa/simple/soft_simple.cpp | 393 ++++++++++++++++++++++++++\n src/libcamera/software_isp/TODO | 83 ++++++\n 10 files changed, 550 insertions(+), 1 deletion(-)\n create mode 100644 include/libcamera/ipa/soft.mojom\n create mode 100644 src/ipa/simple/data/meson.build\n create mode 100644 src/ipa/simple/data/uncalibrated.yaml\n create mode 100644 src/ipa/simple/meson.build\n create mode 100644 src/ipa/simple/soft_simple.cpp", "diff": "diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in\nindex a86ea6c1..2be8d47b 100644\n--- a/Documentation/Doxyfile.in\n+++ b/Documentation/Doxyfile.in\n@@ -44,6 +44,7 @@ EXCLUDE = @TOP_SRCDIR@/include/libcamera/base/span.h \\\n @TOP_SRCDIR@/src/libcamera/pipeline/ \\\n @TOP_SRCDIR@/src/libcamera/tracepoints.cpp \\\n @TOP_BUILDDIR@/include/libcamera/internal/tracepoints.h \\\n+ @TOP_BUILDDIR@/include/libcamera/ipa/soft_ipa_interface.h \\\n @TOP_BUILDDIR@/src/libcamera/proxy/\n \n EXCLUDE_PATTERNS = @TOP_BUILDDIR@/include/libcamera/ipa/*_serializer.h \\\ndiff --git a/include/libcamera/ipa/meson.build b/include/libcamera/ipa/meson.build\nindex f3b4881c..3352d08f 100644\n--- a/include/libcamera/ipa/meson.build\n+++ b/include/libcamera/ipa/meson.build\n@@ -65,6 +65,7 @@ pipeline_ipa_mojom_mapping = {\n 'ipu3': 'ipu3.mojom',\n 'rkisp1': 'rkisp1.mojom',\n 'rpi/vc4': 'raspberrypi.mojom',\n+ 'simple': 'soft.mojom',\n 'vimc': 'vimc.mojom',\n }\n \ndiff --git a/include/libcamera/ipa/soft.mojom b/include/libcamera/ipa/soft.mojom\nnew file mode 100644\nindex 00000000..c249bd75\n--- /dev/null\n+++ b/include/libcamera/ipa/soft.mojom\n@@ -0,0 +1,28 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+\n+/*\n+ * \\todo Document the interface and remove the related EXCLUDE_PATTERNS entry.\n+ */\n+\n+module ipa.soft;\n+\n+import \"include/libcamera/ipa/core.mojom\";\n+\n+interface IPASoftInterface {\n+\tinit(libcamera.IPASettings settings,\n+\t libcamera.SharedFD fdStats,\n+\t libcamera.SharedFD fdParams,\n+\t libcamera.ControlInfoMap sensorCtrlInfoMap)\n+\t\t=> (int32 ret);\n+\tstart() => (int32 ret);\n+\tstop();\n+\tconfigure(libcamera.ControlInfoMap sensorCtrlInfoMap)\n+\t\t=> (int32 ret);\n+\n+\t[async] processStats(libcamera.ControlList sensorControls);\n+};\n+\n+interface IPASoftEventInterface {\n+\tsetSensorControls(libcamera.ControlList sensorControls);\n+\tsetIspParams(int32 dummy);\n+};\ndiff --git a/meson_options.txt b/meson_options.txt\nindex 7c4f6d3a..c61eb555 100644\n--- a/meson_options.txt\n+++ b/meson_options.txt\n@@ -27,7 +27,7 @@ option('gstreamer',\n \n option('ipas',\n type : 'array',\n- choices : ['ipu3', 'rkisp1', 'rpi/vc4', 'vimc'],\n+ choices : ['ipu3', 'rkisp1', 'rpi/vc4', 'simple', 'vimc'],\n description : 'Select which IPA modules to build')\n \n option('lc-compliance',\ndiff --git a/src/ipa/meson.build b/src/ipa/meson.build\nindex 48793e07..0ad4631d 100644\n--- a/src/ipa/meson.build\n+++ b/src/ipa/meson.build\n@@ -41,6 +41,9 @@ ipa_names = []\n \n subdirs = []\n foreach pipeline : pipelines\n+ # The current implementation expects the IPA module name to match the\n+ # pipeline name.\n+ # \\todo Make the IPA naming scheme more flexible.\n if not ipa_modules.contains(pipeline)\n continue\n endif\ndiff --git a/src/ipa/simple/data/meson.build b/src/ipa/simple/data/meson.build\nnew file mode 100644\nindex 00000000..92795ee4\n--- /dev/null\n+++ b/src/ipa/simple/data/meson.build\n@@ -0,0 +1,10 @@\n+# SPDX-License-Identifier: CC0-1.0\n+\n+conf_files = files([\n+ 'uncalibrated.yaml',\n+])\n+\n+# The install_dir must match the name from the IPAModuleInfo\n+install_data(conf_files,\n+ install_dir : ipa_data_dir / 'simple',\n+ install_tag : 'runtime')\ndiff --git a/src/ipa/simple/data/uncalibrated.yaml b/src/ipa/simple/data/uncalibrated.yaml\nnew file mode 100644\nindex 00000000..ff981a1a\n--- /dev/null\n+++ b/src/ipa/simple/data/uncalibrated.yaml\n@@ -0,0 +1,5 @@\n+# SPDX-License-Identifier: CC0-1.0\n+%YAML 1.1\n+---\n+version: 1\n+...\ndiff --git a/src/ipa/simple/meson.build b/src/ipa/simple/meson.build\nnew file mode 100644\nindex 00000000..3e863db7\n--- /dev/null\n+++ b/src/ipa/simple/meson.build\n@@ -0,0 +1,25 @@\n+# SPDX-License-Identifier: CC0-1.0\n+\n+ipa_name = 'ipa_soft_simple'\n+\n+mod = shared_module(ipa_name,\n+ ['soft_simple.cpp', libcamera_generated_ipa_headers],\n+ name_prefix : '',\n+ include_directories : [ipa_includes, libipa_includes],\n+ dependencies : libcamera_private,\n+ link_with : libipa,\n+ install : true,\n+ install_dir : ipa_install_dir)\n+\n+if ipa_sign_module\n+ custom_target(ipa_name + '.so.sign',\n+ input : mod,\n+ output : ipa_name + '.so.sign',\n+ command : [ipa_sign, ipa_priv_key, '@INPUT@', '@OUTPUT@'],\n+ install : false,\n+ build_by_default : true)\n+endif\n+\n+subdir('data')\n+\n+ipa_names += ipa_name\ndiff --git a/src/ipa/simple/soft_simple.cpp b/src/ipa/simple/soft_simple.cpp\nnew file mode 100644\nindex 00000000..196b9c57\n--- /dev/null\n+++ b/src/ipa/simple/soft_simple.cpp\n@@ -0,0 +1,393 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2023, Linaro Ltd\n+ *\n+ * soft_simple.cpp - Simple Software Image Processing Algorithm module\n+ */\n+\n+#include <sys/mman.h>\n+\n+#include <linux/v4l2-controls.h>\n+\n+#include <libcamera/base/file.h>\n+#include <libcamera/base/log.h>\n+#include <libcamera/base/shared_fd.h>\n+\n+#include <libcamera/control_ids.h>\n+#include <libcamera/controls.h>\n+\n+#include <libcamera/ipa/ipa_interface.h>\n+#include <libcamera/ipa/ipa_module_info.h>\n+#include <libcamera/ipa/soft_ipa_interface.h>\n+\n+#include \"libcamera/internal/software_isp/debayer_params.h\"\n+#include \"libcamera/internal/software_isp/swisp_stats.h\"\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+#include \"libipa/camera_sensor_helper.h\"\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(IPASoft)\n+\n+namespace ipa::soft {\n+\n+/*\n+ * The number of bins to use for the optimal exposure calculations.\n+ */\n+static constexpr unsigned int kExposureBinsCount = 5;\n+\n+/*\n+ * The exposure is optimal when the mean sample value of the histogram is\n+ * in the middle of the range.\n+ */\n+static constexpr float kExposureOptimal = kExposureBinsCount / 2.0;\n+\n+/*\n+ * The below value implements the hysteresis for the exposure adjustment.\n+ * It is small enough to have the exposure close to the optimal, and is big\n+ * enough to prevent the exposure from wobbling around the optimal value.\n+ */\n+static constexpr float kExposureSatisfactory = 0.2;\n+\n+class IPASoftSimple : public ipa::soft::IPASoftInterface\n+{\n+public:\n+\tIPASoftSimple()\n+\t\t: params_(nullptr), stats_(nullptr), ignoreUpdates_(0)\n+\t{\n+\t}\n+\n+\t~IPASoftSimple();\n+\n+\tint init(const IPASettings &settings,\n+\t\t const SharedFD &fdStats,\n+\t\t const SharedFD &fdParams,\n+\t\t const ControlInfoMap &sensorInfoMap) override;\n+\tint configure(const ControlInfoMap &sensorInfoMap) override;\n+\n+\tint start() override;\n+\tvoid stop() override;\n+\n+\tvoid processStats(const ControlList &sensorControls) override;\n+\n+private:\n+\tvoid updateExposure(double exposureMSV);\n+\n+\tDebayerParams *params_;\n+\tSwIspStats *stats_;\n+\tstd::unique_ptr<CameraSensorHelper> camHelper_;\n+\tControlInfoMap sensorInfoMap_;\n+\n+\tint32_t exposureMin_, exposureMax_;\n+\tint32_t exposure_;\n+\tdouble againMin_, againMax_, againMinStep_;\n+\tdouble again_;\n+\tunsigned int ignoreUpdates_;\n+};\n+\n+IPASoftSimple::~IPASoftSimple()\n+{\n+\tif (stats_)\n+\t\tmunmap(stats_, sizeof(SwIspStats));\n+\tif (params_)\n+\t\tmunmap(params_, sizeof(DebayerParams));\n+}\n+\n+int IPASoftSimple::init(const IPASettings &settings,\n+\t\t\tconst SharedFD &fdStats,\n+\t\t\tconst SharedFD &fdParams,\n+\t\t\tconst ControlInfoMap &sensorInfoMap)\n+{\n+\tcamHelper_ = CameraSensorHelperFactoryBase::create(settings.sensorModel);\n+\tif (!camHelper_) {\n+\t\tLOG(IPASoft, Warning)\n+\t\t\t<< \"Failed to create camera sensor helper for \"\n+\t\t\t<< settings.sensorModel;\n+\t}\n+\n+\t/* Load the tuning data file */\n+\tFile file(settings.configurationFile);\n+\tif (!file.open(File::OpenModeFlag::ReadOnly)) {\n+\t\tint ret = file.error();\n+\t\tLOG(IPASoft, Error)\n+\t\t\t<< \"Failed to open configuration file \"\n+\t\t\t<< settings.configurationFile << \": \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tstd::unique_ptr<libcamera::YamlObject> data = YamlParser::parse(file);\n+\tif (!data)\n+\t\treturn -EINVAL;\n+\n+\t/* \\todo Use the IPA configuration file for real. */\n+\tunsigned int version = (*data)[\"version\"].get<uint32_t>(0);\n+\tLOG(IPASoft, Debug) << \"Tuning file version \" << version;\n+\n+\tparams_ = nullptr;\n+\tstats_ = nullptr;\n+\n+\tif (!fdStats.isValid()) {\n+\t\tLOG(IPASoft, Error) << \"Invalid Statistics handle\";\n+\t\treturn -ENODEV;\n+\t}\n+\n+\tif (!fdParams.isValid()) {\n+\t\tLOG(IPASoft, Error) << \"Invalid Parameters handle\";\n+\t\treturn -ENODEV;\n+\t}\n+\n+\t{\n+\t\tvoid *mem = mmap(nullptr, sizeof(DebayerParams), PROT_WRITE,\n+\t\t\t\t MAP_SHARED, fdParams.get(), 0);\n+\t\tif (mem == MAP_FAILED) {\n+\t\t\tLOG(IPASoft, Error) << \"Unable to map Parameters\";\n+\t\t\treturn -errno;\n+\t\t}\n+\n+\t\tparams_ = static_cast<DebayerParams *>(mem);\n+\t}\n+\n+\t{\n+\t\tvoid *mem = mmap(nullptr, sizeof(SwIspStats), PROT_READ,\n+\t\t\t\t MAP_SHARED, fdStats.get(), 0);\n+\t\tif (mem == MAP_FAILED) {\n+\t\t\tLOG(IPASoft, Error) << \"Unable to map Statistics\";\n+\t\t\treturn -errno;\n+\t\t}\n+\n+\t\tstats_ = static_cast<SwIspStats *>(mem);\n+\t}\n+\n+\t/*\n+\t * Check if the sensor driver supports the controls required by the\n+\t * Soft IPA.\n+\t * Don't save the min and max control values yet, as e.g. the limits\n+\t * for V4L2_CID_EXPOSURE depend on the configured sensor resolution.\n+\t */\n+\tif (sensorInfoMap.find(V4L2_CID_EXPOSURE) == sensorInfoMap.end()) {\n+\t\tLOG(IPASoft, Error) << \"Don't have exposure control\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (sensorInfoMap.find(V4L2_CID_ANALOGUE_GAIN) == sensorInfoMap.end()) {\n+\t\tLOG(IPASoft, Error) << \"Don't have gain control\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+int IPASoftSimple::configure(const ControlInfoMap &sensorInfoMap)\n+{\n+\tsensorInfoMap_ = sensorInfoMap;\n+\n+\tconst ControlInfo &exposureInfo = sensorInfoMap_.find(V4L2_CID_EXPOSURE)->second;\n+\tconst ControlInfo &gainInfo = sensorInfoMap_.find(V4L2_CID_ANALOGUE_GAIN)->second;\n+\n+\texposureMin_ = exposureInfo.min().get<int32_t>();\n+\texposureMax_ = exposureInfo.max().get<int32_t>();\n+\tif (!exposureMin_) {\n+\t\tLOG(IPASoft, Warning) << \"Minimum exposure is zero, that can't be linear\";\n+\t\texposureMin_ = 1;\n+\t}\n+\n+\tint32_t againMin = gainInfo.min().get<int32_t>();\n+\tint32_t againMax = gainInfo.max().get<int32_t>();\n+\n+\tif (camHelper_) {\n+\t\tagainMin_ = camHelper_->gain(againMin);\n+\t\tagainMax_ = camHelper_->gain(againMax);\n+\t\tagainMinStep_ = (againMax_ - againMin_) / 100.0;\n+\t} else {\n+\t\t/*\n+\t\t * The camera sensor gain (g) is usually not equal to the value written\n+\t\t * into the gain register (x). But the way how the AGC algorithm changes\n+\t\t * the gain value to make the total exposure closer to the optimum\n+\t\t * assumes that g(x) is not too far from linear function. If the minimal\n+\t\t * gain is 0, the g(x) is likely to be far from the linear, like\n+\t\t * g(x) = a / (b * x + c). To avoid unexpected changes to the gain by\n+\t\t * the AGC algorithm (abrupt near one edge, and very small near the\n+\t\t * other) we limit the range of the gain values used.\n+\t\t */\n+\t\tagainMax_ = againMax;\n+\t\tif (!againMin) {\n+\t\t\tLOG(IPASoft, Warning)\n+\t\t\t\t<< \"Minimum gain is zero, that can't be linear\";\n+\t\t\tagainMin_ = std::min(100, againMin / 2 + againMax / 2);\n+\t\t}\n+\t\tagainMinStep_ = 1.0;\n+\t}\n+\n+\tLOG(IPASoft, Info) << \"Exposure \" << exposureMin_ << \"-\" << exposureMax_\n+\t\t\t << \", gain \" << againMin_ << \"-\" << againMax_\n+\t\t\t << \" (\" << againMinStep_ << \")\";\n+\n+\treturn 0;\n+}\n+\n+int IPASoftSimple::start()\n+{\n+\treturn 0;\n+}\n+\n+void IPASoftSimple::stop()\n+{\n+}\n+\n+void IPASoftSimple::processStats(const ControlList &sensorControls)\n+{\n+\t/*\n+\t * Calculate red and blue gains for AWB.\n+\t * Clamp max gain at 4.0, this also avoids 0 division.\n+\t */\n+\tif (stats_->sumR_ <= stats_->sumG_ / 4)\n+\t\tparams_->gainR = 1024;\n+\telse\n+\t\tparams_->gainR = 256 * stats_->sumG_ / stats_->sumR_;\n+\n+\tif (stats_->sumB_ <= stats_->sumG_ / 4)\n+\t\tparams_->gainB = 1024;\n+\telse\n+\t\tparams_->gainB = 256 * stats_->sumG_ / stats_->sumB_;\n+\n+\t/* Green gain and gamma values are fixed */\n+\tparams_->gainG = 256;\n+\tparams_->gamma = 0.5;\n+\n+\tsetIspParams.emit(0);\n+\n+\t/* \\todo Switch to the libipa/algorithm.h API someday. */\n+\n+\t/*\n+\t * AE / AGC, use 2 frames delay to make sure that the exposure and\n+\t * the gain set have applied to the camera sensor.\n+\t * \\todo This could be handled better with DelayedControls.\n+\t */\n+\tif (ignoreUpdates_ > 0) {\n+\t\t--ignoreUpdates_;\n+\t\treturn;\n+\t}\n+\n+\t/*\n+\t * Calculate Mean Sample Value (MSV) according to formula from:\n+\t * https://www.araa.asn.au/acra/acra2007/papers/paper84final.pdf\n+\t */\n+\tconstexpr unsigned int yHistValsPerBin =\n+\t\tSwIspStats::kYHistogramSize / kExposureBinsCount;\n+\tconstexpr unsigned int yHistValsPerBinMod =\n+\t\tSwIspStats::kYHistogramSize /\n+\t\t(SwIspStats::kYHistogramSize % kExposureBinsCount + 1);\n+\tint exposureBins[kExposureBinsCount] = {};\n+\tunsigned int denom = 0;\n+\tunsigned int num = 0;\n+\n+\tfor (unsigned int i = 0; i < SwIspStats::kYHistogramSize; i++) {\n+\t\tunsigned int idx = (i - (i / yHistValsPerBinMod)) / yHistValsPerBin;\n+\t\texposureBins[idx] += stats_->yHistogram[i];\n+\t}\n+\n+\tfor (unsigned int i = 0; i < kExposureBinsCount; i++) {\n+\t\tLOG(IPASoft, Debug) << i << \": \" << exposureBins[i];\n+\t\tdenom += exposureBins[i];\n+\t\tnum += exposureBins[i] * (i + 1);\n+\t}\n+\n+\tfloat exposureMSV = static_cast<float>(num) / denom;\n+\n+\t/* Sanity check */\n+\tif (!sensorControls.contains(V4L2_CID_EXPOSURE) ||\n+\t !sensorControls.contains(V4L2_CID_ANALOGUE_GAIN)) {\n+\t\tLOG(IPASoft, Error) << \"Control(s) missing\";\n+\t\treturn;\n+\t}\n+\n+\texposure_ = sensorControls.get(V4L2_CID_EXPOSURE).get<int32_t>();\n+\tint32_t again = sensorControls.get(V4L2_CID_ANALOGUE_GAIN).get<int32_t>();\n+\tagain_ = camHelper_ ? camHelper_->gain(again) : again;\n+\n+\tupdateExposure(exposureMSV);\n+\n+\tControlList ctrls(sensorInfoMap_);\n+\n+\tctrls.set(V4L2_CID_EXPOSURE, exposure_);\n+\tctrls.set(V4L2_CID_ANALOGUE_GAIN,\n+\t\t static_cast<int32_t>(camHelper_ ? camHelper_->gainCode(again_) : again_));\n+\n+\tignoreUpdates_ = 2;\n+\n+\tsetSensorControls.emit(ctrls);\n+\n+\tLOG(IPASoft, Debug) << \"exposureMSV \" << exposureMSV\n+\t\t\t << \" exp \" << exposure_ << \" again \" << again_\n+\t\t\t << \" gain R/B \" << params_->gainR << \"/\" << params_->gainB;\n+}\n+\n+void IPASoftSimple::updateExposure(double exposureMSV)\n+{\n+\t/*\n+\t * kExpDenominator of 10 gives ~10% increment/decrement;\n+\t * kExpDenominator of 5 - about ~20%\n+\t */\n+\tstatic constexpr uint8_t kExpDenominator = 10;\n+\tstatic constexpr uint8_t kExpNumeratorUp = kExpDenominator + 1;\n+\tstatic constexpr uint8_t kExpNumeratorDown = kExpDenominator - 1;\n+\n+\tdouble next;\n+\n+\tif (exposureMSV < kExposureOptimal - kExposureSatisfactory) {\n+\t\tnext = exposure_ * kExpNumeratorUp / kExpDenominator;\n+\t\tif (next - exposure_ < 1)\n+\t\t\texposure_ += 1;\n+\t\telse\n+\t\t\texposure_ = next;\n+\t\tif (exposure_ >= exposureMax_) {\n+\t\t\tnext = again_ * kExpNumeratorUp / kExpDenominator;\n+\t\t\tif (next - again_ < againMinStep_)\n+\t\t\t\tagain_ += againMinStep_;\n+\t\t\telse\n+\t\t\t\tagain_ = next;\n+\t\t}\n+\t}\n+\n+\tif (exposureMSV > kExposureOptimal + kExposureSatisfactory) {\n+\t\tif (exposure_ == exposureMax_ && again_ > againMin_) {\n+\t\t\tnext = again_ * kExpNumeratorDown / kExpDenominator;\n+\t\t\tif (again_ - next < againMinStep_)\n+\t\t\t\tagain_ -= againMinStep_;\n+\t\t\telse\n+\t\t\t\tagain_ = next;\n+\t\t} else {\n+\t\t\tnext = exposure_ * kExpNumeratorDown / kExpDenominator;\n+\t\t\tif (exposure_ - next < 1)\n+\t\t\t\texposure_ -= 1;\n+\t\t\telse\n+\t\t\t\texposure_ = next;\n+\t\t}\n+\t}\n+\n+\texposure_ = std::clamp(exposure_, exposureMin_, exposureMax_);\n+\tagain_ = std::clamp(again_, againMin_, againMax_);\n+}\n+\n+} /* namespace ipa::soft */\n+\n+/*\n+ * External IPA module interface\n+ */\n+extern \"C\" {\n+const struct IPAModuleInfo ipaModuleInfo = {\n+\tIPA_MODULE_API_VERSION,\n+\t0,\n+\t\"SimplePipelineHandler\",\n+\t\"simple\",\n+};\n+\n+IPAInterface *ipaCreate()\n+{\n+\treturn new ipa::soft::IPASoftSimple();\n+}\n+\n+} /* extern \"C\" */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/software_isp/TODO b/src/libcamera/software_isp/TODO\nindex 29be5386..ae0af25b 100644\n--- a/src/libcamera/software_isp/TODO\n+++ b/src/libcamera/software_isp/TODO\n@@ -173,3 +173,86 @@ the need for performances and the need for a maintainable architecture.\n > I think this falls under the lets wait until we have a GPU\n > based SoftISP MVP/POC and then do some refactoring to see which\n > bits should go where.\n+\n+---\n+\n+8. Decouple pipeline and IPA naming\n+\n+> The current src/ipa/meson.build assumes the IPA name to match the\n+> pipeline name. For this reason \"-Dipas=simple\" is used for the\n+> Soft IPA module.\n+\n+This should be addressed.\n+\n+---\n+\n+9. Doxyfile cleanup\n+\n+>> diff --git a/Documentation/Doxyfile.in b/Documentation/Doxyfile.in\n+>> index a86ea6c1..2be8d47b 100644\n+>> --- a/Documentation/Doxyfile.in\n+>> +++ b/Documentation/Doxyfile.in\n+>> @@ -44,6 +44,7 @@ EXCLUDE = @TOP_SRCDIR@/include/libcamera/base/span.h \\\n+>> @TOP_SRCDIR@/src/libcamera/pipeline/ \\\n+>> @TOP_SRCDIR@/src/libcamera/tracepoints.cpp \\\n+>> @TOP_BUILDDIR@/include/libcamera/internal/tracepoints.h \\\n+>> + @TOP_BUILDDIR@/include/libcamera/ipa/soft_ipa_interface.h \\\n+> Why is this needed ?\n+>\n+>> @TOP_BUILDDIR@/src/libcamera/proxy/\n+>> EXCLUDE_PATTERNS = @TOP_BUILDDIR@/include/libcamera/ipa/*_serializer.h \\\n+>> diff --git a/include/libcamera/ipa/meson.build b/include/libcamera/ipa/meson.build\n+>> index f3b4881c..3352d08f 100644\n+>> --- a/include/libcamera/ipa/meson.build\n+>> +++ b/include/libcamera/ipa/meson.build\n+>> @@ -65,6 +65,7 @@ pipeline_ipa_mojom_mapping = {\n+>> 'ipu3': 'ipu3.mojom',\n+>> 'rkisp1': 'rkisp1.mojom',\n+>> 'rpi/vc4': 'raspberrypi.mojom',\n+>> + 'simple': 'soft.mojom',\n+>> 'vimc': 'vimc.mojom',\n+>> }\n+>> diff --git a/include/libcamera/ipa/soft.mojom b/include/libcamera/ipa/soft.mojom\n+>> new file mode 100644\n+>> index 00000000..c249bd75\n+>> --- /dev/null\n+>> +++ b/include/libcamera/ipa/soft.mojom\n+>> @@ -0,0 +1,28 @@\n+>> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+>> +\n+>> +/*\n+>> + * \\todo Document the interface and remove the related EXCLUDE_PATTERNS entry.\n+> Ah that's why.\n+\n+Yes, because, well... all the other IPAs were doing that...\n+\n+> It doesn't have to be done before merging, but could you\n+> address this sooner than later ?\n+\n+---\n+\n+10. Switch to libipa/algorithm.h API in processStats\n+\n+>> void IPASoftSimple::processStats(const ControlList &sensorControls)\n+>>\n+> Do you envision switching to the libipa/algorithm.h API at some point ?\n+\n+At some point, yes.\n+\n+---\n+\n+11. Improve handling the sensor controls which take effect with a delay\n+\n+> void IPASoftSimple::processStats(const ControlList &sensorControls)\n+> {\n+> ...\n+>\t/*\n+>\t * AE / AGC, use 2 frames delay to make sure that the exposure and\n+>\t * the gain set have applied to the camera sensor.\n+>\t */\n+>\tif (ignore_updates_ > 0) {\n+>\t\t--ignore_updates_;\n+>\t\treturn;\n+>\t}\n+\n+This could be handled better with DelayedControls.\n", "prefixes": [ "v7", "10/18" ] }