{"id":23171,"url":"https://patchwork.libcamera.org/api/1.1/patches/23171/?format=json","web_url":"https://patchwork.libcamera.org/patch/23171/","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":"<20250411130423.2164577-5-stefan.klug@ideasonboard.com>","date":"2025-04-11T13:04:11","name":"[4/8] ipa: rkisp1: Add correction for exposure quantization","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"883081ce3b69571a7f1605cdab0c108ac634d526","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/1.1/people/184/?format=json","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/23171/mbox/","series":[{"id":5122,"url":"https://patchwork.libcamera.org/api/1.1/series/5122/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5122","date":"2025-04-11T13:04:07","name":"Implement WDR algorithm","version":1,"mbox":"https://patchwork.libcamera.org/series/5122/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/23171/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/23171/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 C761AC3213\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 11 Apr 2025 13:04:41 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 819F668AB0;\n\tFri, 11 Apr 2025 15:04:41 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3EC5368A90\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 11 Apr 2025 15:04:39 +0200 (CEST)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:5b21:2ad5:1023:7179])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E293C667;\n\tFri, 11 Apr 2025 15:02:39 +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=\"ZNPdXWj7\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1744376560;\n\tbh=B2fZGNGruVYdJyc8L5k+mXbvXdTVuBui0YHXQkKomoQ=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=ZNPdXWj7fee1+oRgNs0k08k5U1mxKfonhoT+ryfaqNdBilR5Lc/45WZxi9klSZMf3\n\tO4wELSFKW/qXO+K9AQTofzof7k2vwrdFj0/JPuUsEIkiTwbrRcjXoIpEOHO1qEvJTl\n\tH/puZz89A08uIrBP+nq6CEOIPVZKCBeztmMufIkM=","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>","Subject":"[PATCH 4/8] ipa: rkisp1: Add correction for exposure quantization","Date":"Fri, 11 Apr 2025 15:04:11 +0200","Message-ID":"<20250411130423.2164577-5-stefan.klug@ideasonboard.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20250411130423.2164577-1-stefan.klug@ideasonboard.com>","References":"<20250411130423.2164577-1-stefan.klug@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":"When WDR regulation is active, it can happen that exposure times get set\nto very low values (Sometimes 2-3 lines). This intentionally introduced\nunderexposure is corrected by the GWDR module. As exposure time is\nquantized by lines, the smalles possible change in exposure time now\nresults in a quite visible change in perceived brightness.\n\nMitigate that by applying a small global gain to account for the error\nintroduced by the exposure quantization.\n\nToDo: This needs perfect frame synchronous control of the sensor to work\nproperly which is not guaranteed in all cases. It still improves the\nbehavior with the current regulation.\n\nSigned-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n---\n src/ipa/rkisp1/algorithms/agc.cpp |  2 ++\n src/ipa/rkisp1/algorithms/awb.cpp | 12 ++++++++----\n src/ipa/rkisp1/ipa_context.h      |  2 ++\n 3 files changed, 12 insertions(+), 4 deletions(-)","diff":"diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp\nindex 8e77455e7afd..0e4fd3663167 100644\n--- a/src/ipa/rkisp1/algorithms/agc.cpp\n+++ b/src/ipa/rkisp1/algorithms/agc.cpp\n@@ -583,6 +583,8 @@ void Agc::process(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n \tactiveState.agc.automatic.exposure = newExposureTime / lineDuration;\n \tactiveState.agc.automatic.gain = aGain;\n \n+\tactiveState.agc.automatic.gainLostInExposureQuantization =\n+\t\tnewExposureTime / (activeState.agc.automatic.exposure * lineDuration);\n \t/*\n \t * Expand the target frame duration so that we do not run faster than\n \t * the minimum frame duration when we have short exposures.\ndiff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp\nindex 84aa1d29419d..cc78136a22c8 100644\n--- a/src/ipa/rkisp1/algorithms/awb.cpp\n+++ b/src/ipa/rkisp1/algorithms/awb.cpp\n@@ -170,6 +170,7 @@ void Awb::queueRequest(IPAContext &context,\n \tawbAlgo_->handleControls(controls);\n \n \tframeContext.awb.autoEnabled = awb.autoEnabled;\n+\tframeContext.awb.additionalGain = 1.0;\n \n \tif (awb.autoEnabled)\n \t\treturn;\n@@ -218,15 +219,18 @@ void Awb::prepare(IPAContext &context, const uint32_t frame,\n \t\tauto &awb = context.activeState.awb;\n \t\tframeContext.awb.gains = awb.automatic.gains;\n \t\tframeContext.awb.temperatureK = awb.automatic.temperatureK;\n+\t\tframeContext.awb.additionalGain =\n+\t\t\tcontext.activeState.agc.automatic.gainLostInExposureQuantization;\n \t}\n \n+\tauto gains = frameContext.awb.gains * frameContext.awb.additionalGain;\n \tauto gainConfig = params->block<BlockType::AwbGain>();\n \tgainConfig.setEnabled(true);\n \n-\tgainConfig->gain_green_b = std::clamp<int>(256 * frameContext.awb.gains.g(), 0, 0x3ff);\n-\tgainConfig->gain_blue = std::clamp<int>(256 * frameContext.awb.gains.b(), 0, 0x3ff);\n-\tgainConfig->gain_red = std::clamp<int>(256 * frameContext.awb.gains.r(), 0, 0x3ff);\n-\tgainConfig->gain_green_r = std::clamp<int>(256 * frameContext.awb.gains.g(), 0, 0x3ff);\n+\tgainConfig->gain_green_b = std::clamp<int>(256 * gains.g(), 0, 0x3ff);\n+\tgainConfig->gain_blue = std::clamp<int>(256 * gains.b(), 0, 0x3ff);\n+\tgainConfig->gain_red = std::clamp<int>(256 * gains.r(), 0, 0x3ff);\n+\tgainConfig->gain_green_r = std::clamp<int>(256 * gains.g(), 0, 0x3ff);\n \n \t/* If we have already set the AWB measurement parameters, return. */\n \tif (frame > 0)\ndiff --git a/src/ipa/rkisp1/ipa_context.h b/src/ipa/rkisp1/ipa_context.h\nindex 7ccc7b501aff..182203880ac9 100644\n--- a/src/ipa/rkisp1/ipa_context.h\n+++ b/src/ipa/rkisp1/ipa_context.h\n@@ -76,6 +76,7 @@ struct IPAActiveState {\n \t\t} manual;\n \t\tstruct {\n \t\t\tuint32_t exposure;\n+\t\t\tdouble gainLostInExposureQuantization;\n \t\t\tdouble gain;\n \t\t} automatic;\n \n@@ -149,6 +150,7 @@ struct IPAFrameContext : public FrameContext {\n \t\tRGB<double> gains;\n \t\tbool autoEnabled;\n \t\tunsigned int temperatureK;\n+\t\tdouble additionalGain;\n \t} awb;\n \n \tstruct {\n","prefixes":["4/8"]}