{"id":22636,"url":"https://patchwork.libcamera.org/api/patches/22636/?format=json","web_url":"https://patchwork.libcamera.org/patch/22636/","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":"<20250123140727.458567-3-dan.scally@ideasonboard.com>","date":"2025-01-23T14:07:26","name":"[v2,2/3] ipa: libipa: Adjust for flicker in ExposureModeHelper","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"b3b0e81a52adc4387b1419beed6554469f81ab75","submitter":{"id":156,"url":"https://patchwork.libcamera.org/api/people/156/?format=json","name":"Dan Scally","email":"dan.scally@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22636/mbox/","series":[{"id":4968,"url":"https://patchwork.libcamera.org/api/series/4968/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4968","date":"2025-01-23T14:07:24","name":"Add flicker mitigation controls to AgcMeanLuminance","version":2,"mbox":"https://patchwork.libcamera.org/series/4968/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22636/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22636/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 62AA9C3226\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 23 Jan 2025 14:07:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8D6046855C;\n\tThu, 23 Jan 2025 15:07:51 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0564E61878\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Jan 2025 15:07:46 +0100 (CET)","from mail.ideasonboard.com\n\t(cpc141996-chfd3-2-0-cust928.12-3.cable.virginm.net [86.13.91.161])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 697AD1081;\n\tThu, 23 Jan 2025 15:06:42 +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=\"ceGjsVlZ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1737641202;\n\tbh=TOMu82SphVk7MFOmJTgjlewOu9jdsrMrgssQKtJznRM=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=ceGjsVlZnURtY0jnM50PotDi5sPWzwLKR2k689xHU2lIvZL17OT91p+z7s3f56iC+\n\tCylibrHvXl54HQf6J3mUh3qpCVPT4up/JeBFfNbZp9IReTP+7HrkuJ+g6TxT9vHm0z\n\tkczBmY1ZItB+ZSdItkSLUDcli3pZyZFz7DME4ROA=","From":"Daniel Scally <dan.scally@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Daniel Scally <dan.scally@ideasonboard.com>","Subject":"[PATCH v2 2/3] ipa: libipa: Adjust for flicker in ExposureModeHelper","Date":"Thu, 23 Jan 2025 14:07:26 +0000","Message-Id":"<20250123140727.458567-3-dan.scally@ideasonboard.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20250123140727.458567-1-dan.scally@ideasonboard.com>","References":"<20250123140727.458567-1-dan.scally@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":"Update the ExposureModeHelper class to compensate for flickering\nlight sources in the ExposureModeHelper::splitExposure() function.\nThe adjustment simply caps exposure time at a multiple of the given\nflicker period and compensates for any loss in the effective exposure\nvalue by increasing analogue and then digital gain.\n\nInitially in the one call-site for this function, a std::nullopt is\npassed to make this a no-op.\n\nSigned-off-by: Daniel Scally <dan.scally@ideasonboard.com>\n---\nChanges in v2:\n\n\t- Added a function to perform all the exposure time functionality in a\n\t  single place so we're not repeating ourselves\n\t- Called that function in all the return sites rather than just one so\n\t  the flicker mitigation takes effect using exposure from the stages\n\t  list\n\t- Switched the flickerPeriod input to a std::optional\n\t- Clamped the calculated exposure time to guarantee it can't go beneath\n\t  the configured minima\n\n src/ipa/libipa/agc_mean_luminance.cpp   |  3 +-\n src/ipa/libipa/exposure_mode_helper.cpp | 37 ++++++++++++++++++++++---\n src/ipa/libipa/exposure_mode_helper.h   |  6 +++-\n 3 files changed, 40 insertions(+), 6 deletions(-)","diff":"diff --git a/src/ipa/libipa/agc_mean_luminance.cpp b/src/ipa/libipa/agc_mean_luminance.cpp\nindex 02555a44..273ec4e5 100644\n--- a/src/ipa/libipa/agc_mean_luminance.cpp\n+++ b/src/ipa/libipa/agc_mean_luminance.cpp\n@@ -8,6 +8,7 @@\n #include \"agc_mean_luminance.h\"\n \n #include <cmath>\n+#include <optional>\n \n #include <libcamera/base/log.h>\n #include <libcamera/control_ids.h>\n@@ -560,7 +561,7 @@ AgcMeanLuminance::calculateNewEv(uint32_t constraintModeIndex,\n \tnewExposureValue = filterExposure(newExposureValue);\n \n \tframeCount_++;\n-\treturn exposureModeHelper->splitExposure(newExposureValue);\n+\treturn exposureModeHelper->splitExposure(newExposureValue, std::nullopt);\n }\n \n /**\ndiff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp\nindex f235316d..4e1ba943 100644\n--- a/src/ipa/libipa/exposure_mode_helper.cpp\n+++ b/src/ipa/libipa/exposure_mode_helper.cpp\n@@ -7,6 +7,7 @@\n #include \"exposure_mode_helper.h\"\n \n #include <algorithm>\n+#include <optional>\n \n #include <libcamera/base/log.h>\n \n@@ -118,9 +119,31 @@ double ExposureModeHelper::clampGain(double gain) const\n \treturn std::clamp(gain, minGain_, maxGain_);\n }\n \n+utils::Duration\n+ExposureModeHelper::calculateExposureTime(utils::Duration exposure, double stageGain,\n+\t\t\t\t\t  std::optional<utils::Duration> flickerPeriod) const\n+{\n+\tutils::Duration exposureTime;\n+\n+\texposureTime = clampExposureTime(exposure / stageGain);\n+\n+\t/*\n+\t * If we haven't been given a flicker period to adjust for or if it's\n+\t * longer than the exposure time that we need to set then there's not\n+\t * much we can do to compensate.\n+\t */\n+\tif (!flickerPeriod.has_value() || flickerPeriod.value() >= exposureTime)\n+\t\treturn exposureTime;\n+\n+\tunsigned int flickerPeriods = exposureTime / flickerPeriod.value();\n+\n+\treturn clampExposureTime(flickerPeriods * flickerPeriod.value());\n+}\n+\n /**\n  * \\brief Split exposure into exposure time and gain\n  * \\param[in] exposure Exposure value\n+ * \\param[in] flickerPeriod The period of a flickering light source\n  *\n  * This function divides a given exposure into exposure time, analogue and\n  * digital gain by iterating through stages of exposure time and gain limits.\n@@ -147,10 +170,15 @@ double ExposureModeHelper::clampGain(double gain) const\n  * required exposure, the helper falls-back to simply maximising the exposure\n  * time first, followed by analogue gain, followed by digital gain.\n  *\n+ * Once the exposure time has been determined from the modes, an adjustment is\n+ * made to compensate for a flickering light source by fixing the exposure time\n+ * to an exact multiple of the flicker period. Any effective exposure value that\n+ * is lost is added back via analogue and digital gain.\n+ *\n  * \\return Tuple of exposure time, analogue gain, and digital gain\n  */\n std::tuple<utils::Duration, double, double>\n-ExposureModeHelper::splitExposure(utils::Duration exposure) const\n+ExposureModeHelper::splitExposure(utils::Duration exposure, std::optional<utils::Duration> flickerPeriod) const\n {\n \tASSERT(maxExposureTime_);\n \tASSERT(maxGain_);\n@@ -183,14 +211,14 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const\n \t\t */\n \n \t\tif (stageExposureTime * lastStageGain >= exposure) {\n-\t\t\texposureTime = clampExposureTime(exposure / clampGain(lastStageGain));\n+\t\t\texposureTime = calculateExposureTime(exposure, clampGain(lastStageGain), flickerPeriod);\n \t\t\tgain = clampGain(exposure / exposureTime);\n \n \t\t\treturn { exposureTime, gain, exposure / (exposureTime * gain) };\n \t\t}\n \n \t\tif (stageExposureTime * stageGain >= exposure) {\n-\t\t\texposureTime = clampExposureTime(exposure / clampGain(stageGain));\n+\t\t\texposureTime = calculateExposureTime(exposure, clampGain(stageGain), flickerPeriod);\n \t\t\tgain = clampGain(exposure / exposureTime);\n \n \t\t\treturn { exposureTime, gain, exposure / (exposureTime * gain) };\n@@ -204,7 +232,8 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const\n \t * stages to use then the default stageGain of 1.0 is used so that\n \t * exposure time is maxed before gain is touched at all.\n \t */\n-\texposureTime = clampExposureTime(exposure / clampGain(stageGain));\n+\texposureTime = calculateExposureTime(exposure, clampGain(stageGain), flickerPeriod);\n+\n \tgain = clampGain(exposure / exposureTime);\n \n \treturn { exposureTime, gain, exposure / (exposureTime * gain) };\ndiff --git a/src/ipa/libipa/exposure_mode_helper.h b/src/ipa/libipa/exposure_mode_helper.h\nindex c5be1b67..a1d8c6bf 100644\n--- a/src/ipa/libipa/exposure_mode_helper.h\n+++ b/src/ipa/libipa/exposure_mode_helper.h\n@@ -7,6 +7,7 @@\n \n #pragma once\n \n+#include <optional>\n #include <tuple>\n #include <utility>\n #include <vector>\n@@ -28,7 +29,7 @@ public:\n \t\t       double minGain, double maxGain);\n \n \tstd::tuple<utils::Duration, double, double>\n-\tsplitExposure(utils::Duration exposure) const;\n+\tsplitExposure(utils::Duration exposure, std::optional<utils::Duration> flickerPeriod) const;\n \n \tutils::Duration minExposureTime() const { return minExposureTime_; }\n \tutils::Duration maxExposureTime() const { return maxExposureTime_; }\n@@ -38,6 +39,9 @@ public:\n private:\n \tutils::Duration clampExposureTime(utils::Duration exposureTime) const;\n \tdouble clampGain(double gain) const;\n+\tutils::Duration\n+\tcalculateExposureTime(utils::Duration exposureTime, double stageGain,\n+\t\t\t      std::optional<utils::Duration> flickerPeriod) const;\n \n \tstd::vector<utils::Duration> exposureTimes_;\n \tstd::vector<double> gains_;\n","prefixes":["v2","2/3"]}