{"id":22582,"url":"https://patchwork.libcamera.org/api/1.1/patches/22582/?format=json","web_url":"https://patchwork.libcamera.org/patch/22582/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20250117143410.20363-3-dan.scally@ideasonboard.com>","date":"2025-01-17T14:34:09","name":"[2/3] ipa: libipa: Adjust for flicker in ExposureModeHelper","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"d1b0fb051281b820a8d23afd4f86d78f5e3450dc","submitter":{"id":156,"url":"https://patchwork.libcamera.org/api/1.1/people/156/?format=json","name":"Dan Scally","email":"dan.scally@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22582/mbox/","series":[{"id":4954,"url":"https://patchwork.libcamera.org/api/1.1/series/4954/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4954","date":"2025-01-17T14:34:07","name":"Add flicker mitigation controls to AgcMeanLuminance","version":1,"mbox":"https://patchwork.libcamera.org/series/4954/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22582/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22582/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 D021CBD7D8\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 17 Jan 2025 14:34:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A9EC068549;\n\tFri, 17 Jan 2025 15:34:26 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 951456851D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 17 Jan 2025 15:34:21 +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 49C27CE6;\n\tFri, 17 Jan 2025 15:33:22 +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=\"DBDaRhQN\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1737124402;\n\tbh=fEvddwlwSY6laB9o9fibIRkBZw8Bd0TVC8qqw3R32mk=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=DBDaRhQNIc3yqFghK1x1Pl35nVmz3arsuRSZvlfXSGtPq3Bs0lDKGcSsUQe3dncTe\n\tuR5P8y42gszZ4XffYTrfnONnJR3K4QF8xGkSf69dWtw3eShbaPy3KPXUlnfAhQAV6W\n\tc9DpLemKKaQ9dhw8X8Nv+EK3QXx9ldCE06iux9mM=","From":"Daniel Scally <dan.scally@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Daniel Scally <dan.scally@ideasonboard.com>","Subject":"[PATCH 2/3] ipa: libipa: Adjust for flicker in ExposureModeHelper","Date":"Fri, 17 Jan 2025 14:34:09 +0000","Message-Id":"<20250117143410.20363-3-dan.scally@ideasonboard.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20250117143410.20363-1-dan.scally@ideasonboard.com>","References":"<20250117143410.20363-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 ::splitExposure() function. The adjustment\nsimply caps exposure time at a multiple of the given flicker period\nand compensates for any loss in the effective exposure value by\nincreasing analogue and then digital gain.\n\nInitially in the one call-site for this function, a flicker period\nof 0us is passed, making this a no-op.\n\nSigned-off-by: Daniel Scally <dan.scally@ideasonboard.com>\n---\n src/ipa/libipa/agc_mean_luminance.cpp   |  2 +-\n src/ipa/libipa/exposure_mode_helper.cpp | 20 +++++++++++++++++++-\n src/ipa/libipa/exposure_mode_helper.h   |  2 +-\n 3 files changed, 21 insertions(+), 3 deletions(-)","diff":"diff --git a/src/ipa/libipa/agc_mean_luminance.cpp b/src/ipa/libipa/agc_mean_luminance.cpp\nindex 02555a44..b5e6afe3 100644\n--- a/src/ipa/libipa/agc_mean_luminance.cpp\n+++ b/src/ipa/libipa/agc_mean_luminance.cpp\n@@ -560,7 +560,7 @@ AgcMeanLuminance::calculateNewEv(uint32_t constraintModeIndex,\n \tnewExposureValue = filterExposure(newExposureValue);\n \n \tframeCount_++;\n-\treturn exposureModeHelper->splitExposure(newExposureValue);\n+\treturn exposureModeHelper->splitExposure(newExposureValue, 0us);\n }\n \n /**\ndiff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp\nindex f235316d..038aa33c 100644\n--- a/src/ipa/libipa/exposure_mode_helper.cpp\n+++ b/src/ipa/libipa/exposure_mode_helper.cpp\n@@ -121,6 +121,7 @@ double ExposureModeHelper::clampGain(double gain) const\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 +148,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, utils::Duration flickerPeriod) const\n {\n \tASSERT(maxExposureTime_);\n \tASSERT(maxGain_);\n@@ -205,6 +211,18 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const\n \t * exposure time is maxed before gain is touched at all.\n \t */\n \texposureTime = clampExposureTime(exposure / clampGain(stageGain));\n+\n+\t/*\n+\t * Finally, if we have been given a flicker period we need to reduce the\n+\t * exposure time to be a multiple of that period (if possible). The\n+\t * effect of this should be to hide the flicker.\n+\t */\n+\tif (flickerPeriod > 0us && flickerPeriod < exposureTime) {\n+\t\tunsigned int flickerPeriods = exposureTime / flickerPeriod;\n+\t\tif (flickerPeriods)\n+\t\t\texposureTime = flickerPeriods * flickerPeriod;\n+\t}\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..a5fcc366 100644\n--- a/src/ipa/libipa/exposure_mode_helper.h\n+++ b/src/ipa/libipa/exposure_mode_helper.h\n@@ -28,7 +28,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, utils::Duration flickerPeriod) const;\n \n \tutils::Duration minExposureTime() const { return minExposureTime_; }\n \tutils::Duration maxExposureTime() const { return maxExposureTime_; }\n","prefixes":["2/3"]}