From patchwork Sun Aug 16 20:42:58 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Alexander X-Patchwork-Id: 27776 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 0D8F1C330A for ; Mon, 17 Aug 2026 07:30:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E134068240; Mon, 17 Aug 2026 09:30:11 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=inspiredexperts.com header.i=@inspiredexperts.com header.b="PahA/MyF"; dkim-atps=neutral Received: from s1.inspiredexperts.com (s1.inspiredexperts.com [162.243.156.162]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D150F681A0 for ; Sun, 16 Aug 2026 22:43:03 +0200 (CEST) Received: from localhost (s1.inspiredexperts.com [127.0.0.1]) by s1.inspiredexperts.com (Postfix) with ESMTP id 16BD9D2D68C for ; Sun, 16 Aug 2026 14:43:02 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=inspiredexperts.com; s=default; t=1786912982; bh=mZ0djEnBvYwYaU5LEsqFV1XVqm6JpEK8MX0tQ962dYU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=PahA/MyFwSvkUOhg42StBg/FzlW3J/E9Cv3oojjce6wp7UC3UD10RwanDk5SasRrH j1sB7qU3WRtcGiv2z4QScylmdRPD7X6Uh8QtHLMgnINaUTrDLuIu+HYkdbXcEMSxlh wBhuyt/Dh3cWjBOHxhhyuOtcmbl9PvwKSzRnjEcQ= From: James Alexander To: libcamera-devel@lists.libcamera.org Subject: [PATCH 2/3] ipa: simple: agc: Read limits from tuning Date: Sun, 16 Aug 2026 14:42:58 -0600 Message-ID: <20260816204259.2845517-3-opensource@inspiredexperts.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260816204259.2845517-1-opensource@inspiredexperts.com> References: <20260816204259.2845517-1-opensource@inspiredexperts.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on s1.inspiredexperts.com X-Mailman-Approved-At: Mon, 17 Aug 2026 09:30:05 +0200 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The simple IPA AGC currently uses one target and the full exposure and gain ranges reported by the sensor. On the tested ov08x40 setup that produced more noise and longer exposures than were useful for a webcam. Allow tuning to set the histogram target, maximum analogue gain and maximum exposure time. Missing values preserve the current behaviour, and configured limits are clamped to the sensor's reported range. Build-tested against libcamera base b8910c9a4961 and hardware-tested as part of the complete patch set on the target HP Spectre. Signed-off-by: James Alexander diff --git a/src/ipa/simple/algorithms/agc.cpp b/src/ipa/simple/algorithms/agc.cpp index a13a755..fa93d03 100644 --- a/src/ipa/simple/algorithms/agc.cpp +++ b/src/ipa/simple/algorithms/agc.cpp @@ -8,6 +8,7 @@ #include "agc.h" #include +#include #include #include @@ -15,6 +16,8 @@ #include "control_ids.h" +using namespace std::literals::chrono_literals; + namespace libcamera { LOG_DEFINE_CATEGORY(IPASoftExposure) @@ -62,7 +65,46 @@ static constexpr float kExpProportionalGain = 0.04; static constexpr float kExpMaxStep = 0.15; Agc::Agc() + : exposureOptimal_(kExposureOptimal) +{ +} + +int Agc::init([[maybe_unused]] IPAContext &context, const ValueNode &tuningData) +{ + auto target = tuningData["target"].get(); + if (target.has_value()) + exposureOptimal_ = std::clamp(target.value(), 1.0, 5.0); + + auto maxAnalogueGain = tuningData["maxAnalogueGain"].get(); + if (maxAnalogueGain.has_value()) + maxAnalogueGain_ = std::max(1.0, maxAnalogueGain.value()); + + auto maxExposureTimeMs = tuningData["maxExposureTimeMs"].get(); + if (maxExposureTimeMs.has_value()) + maxExposureTimeMs_ = std::max(1.0, maxExposureTimeMs.value()); + + return 0; +} + +int Agc::configure(IPAContext &context, + [[maybe_unused]] const IPAConfigInfo &configInfo) { + if (maxAnalogueGain_.has_value()) + context.configuration.agc.againMax = + std::clamp(maxAnalogueGain_.value(), + context.configuration.agc.againMin, + context.configuration.agc.againMax); + + if (maxExposureTimeMs_.has_value()) { + utils::Duration maxExposure = maxExposureTimeMs_.value() * 1.0ms; + int32_t maxExposureLines = + std::max(context.configuration.agc.exposureMin, + maxExposure / context.configuration.agc.lineDuration); + context.configuration.agc.exposureMax = + std::min(context.configuration.agc.exposureMax, maxExposureLines); + } + + return 0; } void Agc::updateExposure(IPAContext &context, IPAFrameContext &frameContext, double exposureMSV) @@ -70,7 +112,7 @@ void Agc::updateExposure(IPAContext &context, IPAFrameContext &frameContext, dou int32_t &exposure = frameContext.sensor.exposure; double &again = frameContext.sensor.gain; - double error = kExposureOptimal - exposureMSV; + double error = exposureOptimal_ - exposureMSV; if (std::abs(error) <= kExposureSatisfactory) return; diff --git a/src/ipa/simple/algorithms/agc.h b/src/ipa/simple/algorithms/agc.h index 112d9f5..501252f 100644 --- a/src/ipa/simple/algorithms/agc.h +++ b/src/ipa/simple/algorithms/agc.h @@ -7,6 +7,8 @@ #pragma once +#include + #include "algorithm.h" namespace libcamera { @@ -19,6 +21,9 @@ public: Agc(); ~Agc() = default; + int init(IPAContext &context, const ValueNode &tuningData) override; + int configure(IPAContext &context, + const IPAConfigInfo &configInfo) override; void process(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, const SwIspStats *stats, @@ -26,6 +31,10 @@ public: private: void updateExposure(IPAContext &context, IPAFrameContext &frameContext, double exposureMSV); + + double exposureOptimal_; + std::optional maxAnalogueGain_; + std::optional maxExposureTimeMs_; }; } /* namespace ipa::soft::algorithms */