From patchwork Mon Feb 7 13:59:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 15335 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 DD9CDBDCBF for ; Mon, 7 Feb 2022 13:59:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1E29D61079; Mon, 7 Feb 2022 14:59:45 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="MGZiIqLc"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 714466105F for ; Mon, 7 Feb 2022 14:59:42 +0100 (CET) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f807:d5e7:1307:1df2]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 0B790A04; Mon, 7 Feb 2022 14:59:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1644242382; bh=z1Qs1WSEcAbn5T1b4eUCbDKEh5hHF8+OExTPMAF16CA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MGZiIqLcVF3XMdshfORXd01rekJtm8wokQKehH3oFcMejQMeCqPHyHcBYiH0vIaG4 svrpjTgZ2w01CN5OOuMXN1aKHdyhBWniMu4DLn3NWFL5ORiNUz0Lv3VMlZgnDxa0sl QQuTe/pgFO27SCtolXBLNEXE2YC8CwU6o39fia/Q= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Mon, 7 Feb 2022 14:59:35 +0100 Message-Id: <20220207135937.221226-2-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220207135937.221226-1-jeanmichel.hautbois@ideasonboard.com> References: <20220207135937.221226-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/3] ipa: ipu3: Return filtered value 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" When the current exposure value is calculated, it is cached and used by filterExposure(). Use private filteredExposure_ and pass currentExposure as a parameter. In order to limit the use of filteredExposure_, return the value from filterExposure(). While at it, remove a stale comment. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 74 ++++++++++++++++++--------------- src/ipa/ipu3/algorithms/agc.h | 3 +- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 8d6f18f6..a929085c 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -72,7 +72,7 @@ static constexpr double kRelativeLuminanceTarget = 0.16; Agc::Agc() : frameCount_(0), lineDuration_(0s), minShutterSpeed_(0s), - maxShutterSpeed_(0s), filteredExposure_(0s), currentExposure_(0s) + maxShutterSpeed_(0s), filteredExposure_(0s) { } @@ -143,33 +143,37 @@ double Agc::measureBrightness(const ipu3_uapi_stats_3a *stats, /** * \brief Apply a filter on the exposure value to limit the speed of changes + * \param[in] exposureValue The target exposure from the AGC algorithm + * + * The speed of the filter is adaptive, and will produce the target quicker + * during startup, or when the target exposure is within 20% of the most recent + * filter output. + * + * \return The filtered exposure */ -void Agc::filterExposure() +utils::Duration Agc::filterExposure(utils::Duration exposureValue) { double speed = 0.2; - /* Adapt instantly if we are in startup phase */ + /* Adapt instantly if we are in startup phase. */ if (frameCount_ < kNumStartupFrames) speed = 1.0; - if (filteredExposure_ == 0s) { - /* DG stands for digital gain.*/ - filteredExposure_ = currentExposure_; - } else { - /* - * If we are close to the desired result, go faster to avoid making - * multiple micro-adjustments. - * \todo Make this customisable? - */ - if (filteredExposure_ < 1.2 * currentExposure_ && - filteredExposure_ > 0.8 * currentExposure_) - speed = sqrt(speed); - - filteredExposure_ = speed * currentExposure_ + - filteredExposure_ * (1.0 - speed); - } + /* + * If we are close to the desired result, go faster to avoid making + * multiple micro-adjustments. + * \todo Make this customisable? + */ + if (filteredExposure_ < 1.2 * exposureValue && + filteredExposure_ > 0.8 * exposureValue) + speed = sqrt(speed); + + filteredExposure_ = speed * exposureValue + + filteredExposure_ * (1.0 - speed); - LOG(IPU3Agc, Debug) << "After filtering, total_exposure " << filteredExposure_; + LOG(IPU3Agc, Debug) << "After filtering, exposure " << filteredExposure_; + + return filteredExposure_; } /** @@ -213,27 +217,29 @@ void Agc::computeExposure(IPAFrameContext &frameContext, double yGain, * Calculate the current exposure value for the scene as the latest * exposure value applied multiplied by the new estimated gain. */ - currentExposure_ = effectiveExposureValue * evGain; + utils::Duration exposureValue = effectiveExposureValue * evGain; /* Clamp the exposure value to the min and max authorized */ utils::Duration maxTotalExposure = maxShutterSpeed_ * maxAnalogueGain_; - currentExposure_ = std::min(currentExposure_, maxTotalExposure); - LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_ + exposureValue = std::min(exposureValue, maxTotalExposure); + LOG(IPU3Agc, Debug) << "Target total exposure " << exposureValue << ", maximum is " << maxTotalExposure; - /* \todo: estimate if we need to desaturate */ - filterExposure(); - - /* Divide the exposure value as new exposure and gain values */ - utils::Duration exposureValue = filteredExposure_; - utils::Duration shutterTime; + /* + * Filter the exposure. + * \todo: estimate if we need to desaturate + */ + exposureValue = filterExposure(exposureValue); /* - * Push the shutter time up to the maximum first, and only then - * increase the gain. - */ - shutterTime = std::clamp(exposureValue / minAnalogueGain_, - minShutterSpeed_, maxShutterSpeed_); + * Divide the exposure value as new exposure and gain values. + * + * Push the shutter time up to the maximum first, and only then + * increase the gain. + */ + utils::Duration shutterTime = + std::clamp(exposureValue / minAnalogueGain_, + minShutterSpeed_, maxShutterSpeed_); double stepGain = std::clamp(exposureValue / shutterTime, minAnalogueGain_, maxAnalogueGain_); LOG(IPU3Agc, Debug) << "Divided up shutter and gain are " diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h index 96ec7005..84bfe045 100644 --- a/src/ipa/ipu3/algorithms/agc.h +++ b/src/ipa/ipu3/algorithms/agc.h @@ -33,7 +33,7 @@ public: private: double measureBrightness(const ipu3_uapi_stats_3a *stats, const ipu3_uapi_grid_config &grid) const; - void filterExposure(); + utils::Duration filterExposure(utils::Duration currentExposure); void computeExposure(IPAFrameContext &frameContext, double yGain, double iqMeanGain); double estimateLuminance(IPAFrameContext &frameContext, @@ -51,7 +51,6 @@ private: double maxAnalogueGain_; utils::Duration filteredExposure_; - utils::Duration currentExposure_; uint32_t stride_; }; From patchwork Mon Feb 7 13:59:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 15336 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 EDB58BDCBF for ; Mon, 7 Feb 2022 13:59:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8CC4161085; Mon, 7 Feb 2022 14:59:45 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ryD17Quu"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9F3CE6106C for ; Mon, 7 Feb 2022 14:59:42 +0100 (CET) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f807:d5e7:1307:1df2]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 41FE2A50; Mon, 7 Feb 2022 14:59:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1644242382; bh=ZDm9VKGFF43duA1wbbFF797P0YbVhnM4JL6qJmSB5V4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ryD17QuuxSnmZCJuH9v4IK+l8yz709V2iqFPg0ogOcSbLcBBC4ITItbJvA0ubAkdv +EoO4NUiBbzf4d+ZudfQtPcuOapqj2sKGqit4JquIFNF9TAiOVX0QLmaqgJ9S9IwCk oaimPtoxbmR7QDRwXlZgWlyBtHEUzgSeJnzW89Uo= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Mon, 7 Feb 2022 14:59:36 +0100 Message-Id: <20220207135937.221226-3-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220207135937.221226-1-jeanmichel.hautbois@ideasonboard.com> References: <20220207135937.221226-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/3] ipa: ipu3: Shorten exposure and gain lines 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" When the effective sensor values are stored during the EventStatReady event, the lines are long. Fix it. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/ipu3.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp index 3d307708..c0c29416 100644 --- a/src/ipa/ipu3/ipu3.cpp +++ b/src/ipa/ipu3/ipu3.cpp @@ -547,8 +547,11 @@ void IPAIPU3::processEvent(const IPU3Event &event) const ipu3_uapi_stats_3a *stats = reinterpret_cast(mem.data()); - context_.frameContext.sensor.exposure = event.sensorControls.get(V4L2_CID_EXPOSURE).get(); - context_.frameContext.sensor.gain = camHelper_->gain(event.sensorControls.get(V4L2_CID_ANALOGUE_GAIN).get()); + int32_t exposure = event.sensorControls.get(V4L2_CID_EXPOSURE).get(); + int32_t gain = event.sensorControls.get(V4L2_CID_ANALOGUE_GAIN).get(); + + context_.frameContext.sensor.exposure = exposure; + context_.frameContext.sensor.gain = camHelper_->gain(gain); parseStatistics(event.frame, event.frameTimestamp, stats); break; From patchwork Mon Feb 7 13:59:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 15337 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 8CD2DC3257 for ; Mon, 7 Feb 2022 13:59:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D6A916108F; Mon, 7 Feb 2022 14:59:45 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="lBR9T9LZ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C01AB61070 for ; Mon, 7 Feb 2022 14:59:42 +0100 (CET) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f807:d5e7:1307:1df2]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 77001499; Mon, 7 Feb 2022 14:59:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1644242382; bh=9/vtAg0HeK956Mv0GPf3VCD4uXKfQbUx1pE3UA7hak8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lBR9T9LZaFO4Rl2HrmP9oeMCL4bLSxkFfwp+2RhlZArwVOIEwVCb8DdvcdZlbGbvR yjpkCH8QPdDiaClyuNF79S2TeFWHvcXZ48HR7Ocm9FnbpEK0yXAgPX6EpwWSCQhZ8T gNWBwnI1U+HxKR6r6jVQw4Yv5k4zEccvfQCVdmYk= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Mon, 7 Feb 2022 14:59:37 +0100 Message-Id: <20220207135937.221226-4-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220207135937.221226-1-jeanmichel.hautbois@ideasonboard.com> References: <20220207135937.221226-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/3] ipa: ipu3: agc: Introduce lineDuration in IPASessionConfiguration 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" Instead of having a local cached value for line duration, store it in the IPASessionConfiguration::agc structure. While at it, configure the default analogue gain and shutter speed to controlled fixed values. The latter is set to be 10ms as it will in most cases be close to the one needed, making the AGC faster to converge. Signed-off-by: Jean-Michel Hautbois --- src/ipa/ipu3/algorithms/agc.cpp | 25 +++++++++++++++---------- src/ipa/ipu3/algorithms/agc.h | 3 +-- src/ipa/ipu3/ipa_context.cpp | 3 +++ src/ipa/ipu3/ipa_context.h | 1 + 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index a929085c..137c36cf 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -71,7 +71,7 @@ static constexpr uint32_t kNumStartupFrames = 10; static constexpr double kRelativeLuminanceTarget = 0.16; Agc::Agc() - : frameCount_(0), lineDuration_(0s), minShutterSpeed_(0s), + : frameCount_(0), minShutterSpeed_(0s), maxShutterSpeed_(0s), filteredExposure_(0s) { } @@ -85,11 +85,14 @@ Agc::Agc() */ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo) { - stride_ = context.configuration.grid.stride; + IPASessionConfiguration &configuration = context.configuration; + IPAFrameContext &frameContext = context.frameContext; + + stride_ = configuration.grid.stride; /* \todo use the IPAContext to provide the limits */ - lineDuration_ = configInfo.sensorInfo.lineLength * 1.0s - / configInfo.sensorInfo.pixelRate; + configuration.agc.lineDuration = configInfo.sensorInfo.lineLength * 1.0s + / configInfo.sensorInfo.pixelRate; minShutterSpeed_ = context.configuration.agc.minShutterSpeed; maxShutterSpeed_ = std::min(context.configuration.agc.maxShutterSpeed, @@ -99,8 +102,8 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo) maxAnalogueGain_ = std::min(context.configuration.agc.maxAnalogueGain, kMaxAnalogueGain); /* Configure the default exposure and gain. */ - context.frameContext.agc.gain = minAnalogueGain_; - context.frameContext.agc.exposure = minShutterSpeed_ / lineDuration_; + frameContext.agc.gain = std::max(minAnalogueGain_, kMinAnalogueGain); + frameContext.agc.exposure = 10ms / configuration.agc.lineDuration; return 0; } @@ -182,9 +185,11 @@ utils::Duration Agc::filterExposure(utils::Duration exposureValue) * \param[in] yGain The gain calculated based on the relative luminance target * \param[in] iqMeanGain The gain calculated based on the relative luminance target */ -void Agc::computeExposure(IPAFrameContext &frameContext, double yGain, +void Agc::computeExposure(IPAContext &context, double yGain, double iqMeanGain) { + const IPASessionConfiguration &configuration = context.configuration; + IPAFrameContext &frameContext = context.frameContext; /* Get the effective exposure and gain applied on the sensor. */ uint32_t exposure = frameContext.sensor.exposure; double analogueGain = frameContext.sensor.gain; @@ -200,7 +205,7 @@ void Agc::computeExposure(IPAFrameContext &frameContext, double yGain, /* extracted from Rpi::Agc::computeTargetExposure */ /* Calculate the shutter time in seconds */ - utils::Duration currentShutter = exposure * lineDuration_; + utils::Duration currentShutter = exposure * configuration.agc.lineDuration; /* * Update the exposure value for the next computation using the values @@ -247,7 +252,7 @@ void Agc::computeExposure(IPAFrameContext &frameContext, double yGain, << stepGain; /* Update the estimated exposure and gain. */ - frameContext.agc.exposure = shutterTime / lineDuration_; + frameContext.agc.exposure = shutterTime / configuration.agc.lineDuration; frameContext.agc.gain = stepGain; } @@ -354,7 +359,7 @@ void Agc::process(IPAContext &context, const ipu3_uapi_stats_3a *stats) break; } - computeExposure(context.frameContext, yGain, iqMeanGain); + computeExposure(context, yGain, iqMeanGain); frameCount_++; } diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h index 84bfe045..ad705605 100644 --- a/src/ipa/ipu3/algorithms/agc.h +++ b/src/ipa/ipu3/algorithms/agc.h @@ -34,7 +34,7 @@ private: double measureBrightness(const ipu3_uapi_stats_3a *stats, const ipu3_uapi_grid_config &grid) const; utils::Duration filterExposure(utils::Duration currentExposure); - void computeExposure(IPAFrameContext &frameContext, double yGain, + void computeExposure(IPAContext &context, double yGain, double iqMeanGain); double estimateLuminance(IPAFrameContext &frameContext, const ipu3_uapi_grid_config &grid, @@ -43,7 +43,6 @@ private: uint64_t frameCount_; - utils::Duration lineDuration_; utils::Duration minShutterSpeed_; utils::Duration maxShutterSpeed_; diff --git a/src/ipa/ipu3/ipa_context.cpp b/src/ipa/ipu3/ipa_context.cpp index 86794ac1..ace9c66f 100644 --- a/src/ipa/ipu3/ipa_context.cpp +++ b/src/ipa/ipu3/ipa_context.cpp @@ -84,6 +84,9 @@ namespace libcamera::ipa::ipu3 { * * \var IPASessionConfiguration::agc.maxAnalogueGain * \brief Maximum analogue gain supported with the configured sensor + * + * \var IPASessionConfiguration::agc.lineDuration + * \brief Line duration in microseconds */ /** diff --git a/src/ipa/ipu3/ipa_context.h b/src/ipa/ipu3/ipa_context.h index c6dc0814..7696fd14 100644 --- a/src/ipa/ipu3/ipa_context.h +++ b/src/ipa/ipu3/ipa_context.h @@ -30,6 +30,7 @@ struct IPASessionConfiguration { utils::Duration maxShutterSpeed; double minAnalogueGain; double maxAnalogueGain; + utils::Duration lineDuration; } agc; };