From patchwork Thu Oct 21 16:43:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14222 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 E967CBF415 for ; Thu, 21 Oct 2021 16:44:11 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 32C5168F6D; Thu, 21 Oct 2021 18:44:08 +0200 (CEST) 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="DeAgWwtg"; 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 A5C0968F56 for ; Thu, 21 Oct 2021 18:44:05 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 432081B43; Thu, 21 Oct 2021 18:44:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834645; bh=DTB9tOM4k7ndNUbPWAAzFJdPZ+bZP+oAf6jSfKyEqEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DeAgWwtgwYBwb7i7eU6pvC7LqRLQwnhW1+QfE1Uub2Y40YKEzN3HcGNxpHmgchN2m Hz3pNN92NBqgd0gl2iAUJlNVNA3mzdlvVI4WNV0M8Vv2jU2bBHLF6Vs8/U3k8HC0nX LmwFAf+dVeiHBiJNOaAZ8CcY0dZyw8AI5v8xVcFE= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:48 +0200 Message-Id: <20211021164401.110033-2-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 01/14] ipa: ipu3: awb: Set a threshold for the green saturation 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" We can have a saturation ratio per cell, giving the percentage of pixels over a threshold within a cell where 100% is set to 0xff. The parameter structure 'ipu3_uapi_awb_config_s' contains four fields to set the threshold, one per channel. The blue field is also used to configure the ImgU and make it calculate the saturation ratio or not. Set a green value saturated when it is more than 230 (90% of the maximum value 255, coded as 8191). As this is the only channel used for AGC, there is no need to apply it to the other ones. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/ipa/ipu3/algorithms/awb.cpp | 27 +++++++++++++++++++++------ src/ipa/ipu3/algorithms/awb.h | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/ipa/ipu3/algorithms/awb.cpp b/src/ipa/ipu3/algorithms/awb.cpp index 809de66a..4364928c 100644 --- a/src/ipa/ipu3/algorithms/awb.cpp +++ b/src/ipa/ipu3/algorithms/awb.cpp @@ -328,14 +328,29 @@ void Awb::process(IPAContext &context, const ipu3_uapi_stats_3a *stats) context.frameContext.awb.gains.red = asyncResults_.redGain; } +constexpr uint16_t Awb::threshold(float value) +{ + /* AWB thresholds are in the range [0, 8191] */ + return value * 8191; +} + void Awb::prepare(IPAContext &context, ipu3_uapi_params *params) { - params->acc_param.awb.config.rgbs_thr_gr = 8191; - params->acc_param.awb.config.rgbs_thr_r = 8191; - params->acc_param.awb.config.rgbs_thr_gb = 8191; - params->acc_param.awb.config.rgbs_thr_b = IPU3_UAPI_AWB_RGBS_THR_B_INCL_SAT - | IPU3_UAPI_AWB_RGBS_THR_B_EN - | 8191; + /* + * Green saturation thresholds are reduced because we are using the + * green channel only in the exposure computation. + */ + params->acc_param.awb.config.rgbs_thr_r = threshold(1.0); + params->acc_param.awb.config.rgbs_thr_gr = threshold(0.9); + params->acc_param.awb.config.rgbs_thr_gb = threshold(0.9); + params->acc_param.awb.config.rgbs_thr_b = threshold(1.0); + + /* + * Enable saturation inclusion on thr_b for ImgU to update the + * ipu3_uapi_awb_set_item->sat_ratio field. + */ + params->acc_param.awb.config.rgbs_thr_b |= IPU3_UAPI_AWB_RGBS_THR_B_INCL_SAT | + IPU3_UAPI_AWB_RGBS_THR_B_EN; const ipu3_uapi_grid_config &grid = context.configuration.grid.bdsGrid; diff --git a/src/ipa/ipu3/algorithms/awb.h b/src/ipa/ipu3/algorithms/awb.h index 3b81f600..0c81e39e 100644 --- a/src/ipa/ipu3/algorithms/awb.h +++ b/src/ipa/ipu3/algorithms/awb.h @@ -70,6 +70,7 @@ private: void clearAwbStats(); void awbGreyWorld(); uint32_t estimateCCT(double red, double green, double blue); + static constexpr uint16_t threshold(float value); std::vector zones_; Accumulator awbStats_[kAwbStatsSizeX * kAwbStatsSizeY]; From patchwork Thu Oct 21 16:43:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14223 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 CC34ABF415 for ; Thu, 21 Oct 2021 16:44:13 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B004B68F71; Thu, 21 Oct 2021 18:44:08 +0200 (CEST) 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="XvhZj5qJ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C96B768F57 for ; Thu, 21 Oct 2021 18:44:05 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 72F311BF9; Thu, 21 Oct 2021 18:44:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834645; bh=WLcW3EpAUw6mRn12xBZYoxaEM+cAJa/igT+LGrJx8M8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XvhZj5qJX5oA2GD1Bg0fBPozr5zVFzHJuhtTIopTRj9ZOJsUVDxVi+jw3h8tlehVO sCqVpFRpBS048CHgfkZNESJP/G2l/HU/ItiA+lZaunZf5VwZk/uFSExEYMKOqzVdFZ zuO51OgGXPslZnpU0BcWF16zZgSKKIqEWCovofic= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:49 +0200 Message-Id: <20211021164401.110033-3-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 02/14] ipa: ipu3: set frameContext before controls 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 AGC frame context needs to be initialised correctly for the first iteration. Until now, the IPA uses the minimum exposure and gain values and caches those in local variables. In order to give the sensor limits to AGC, create a new structure in IPASessionConfiguration. Store the exposure in time (and not line duration) and the analogue gain after CameraSensorHelper conversion. Set the gain and exposure appropriately to the current values known to the IPA and remove the setting of exposure and gain in IPAIPU3 as those are now fully controlled by IPU3Agc. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 8 ++++- src/ipa/ipu3/ipa_context.h | 9 +++++ src/ipa/ipu3/ipu3.cpp | 64 +++++++++++++++++++++++++++++---- 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 970ec424..828e822c 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -60,7 +60,13 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo) lineDuration_ = configInfo.sensorInfo.lineLength * 1.0s / configInfo.sensorInfo.pixelRate; - maxExposureTime_ = kMaxExposure * lineDuration_; + maxExposureTime_ = context.configuration.agc.maxShutterSpeed; + + /* Configure the default exposure and gain. */ + context.frameContext.agc.gain = + context.configuration.agc.minAnalogueGain; + context.frameContext.agc.exposure = + context.configuration.agc.minShutterSpeed / lineDuration_; return 0; } diff --git a/src/ipa/ipu3/ipa_context.h b/src/ipa/ipu3/ipa_context.h index 5bab684c..847c03fe 100644 --- a/src/ipa/ipu3/ipa_context.h +++ b/src/ipa/ipu3/ipa_context.h @@ -10,6 +10,8 @@ #include +#include + #include namespace libcamera { @@ -22,6 +24,13 @@ struct IPASessionConfiguration { Size bdsOutputSize; uint32_t stride; } grid; + + struct { + utils::Duration minShutterSpeed; + utils::Duration maxShutterSpeed; + double minAnalogueGain; + double maxAnalogueGain; + } agc; }; struct IPAFrameContext { diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp index 388f1902..65d3fd2c 100644 --- a/src/ipa/ipu3/ipu3.cpp +++ b/src/ipa/ipu3/ipu3.cpp @@ -97,6 +97,23 @@ * \brief Number of cells on one line including the ImgU padding */ +/** + * \struct IPASessionConfiguration::agc + * \brief AGC parameters configuration of the IPA + * + * \var IPASessionConfiguration::agc::minShutterSpeed + * \brief Minimum shutter speed supported with the configured sensor + * + * \var IPASessionConfiguration::grid::maxShutterSpeed + * \brief Maximum shutter speed supported with the configured sensor + * + * \var IPASessionConfiguration::grid::minAnalogueGain + * \brief Minimum analogue gain supported with the configured sensor + * + * \var IPASessionConfiguration::grid::maxAnalogueGain + * \brief Maximum analogue gain supported with the configured sensor + */ + /** * \struct IPAFrameContext::agc * \brief Context for the Automatic Gain Control algorithm @@ -158,6 +175,8 @@ namespace libcamera { LOG_DEFINE_CATEGORY(IPAIPU3) +using namespace std::literals::chrono_literals; + namespace ipa::ipu3 { class IPAIPU3 : public IPAIPU3Interface @@ -182,6 +201,8 @@ private: void updateControls(const IPACameraSensorInfo &sensorInfo, const ControlInfoMap &sensorControls, ControlInfoMap *ipaControls); + void updateSessionConfiguration(const IPACameraSensorInfo &sensorInfo, + const ControlInfoMap &sensorControls); void processControls(unsigned int frame, const ControlList &controls); void fillParams(unsigned int frame, ipu3_uapi_params *params); void parseStatistics(unsigned int frame, @@ -216,6 +237,36 @@ private: struct IPAContext context_; }; +/* + * Compute IPASessionConfiguration using the sensor information and the sensor + * v4l2 controls. + */ +void IPAIPU3::updateSessionConfiguration(const IPACameraSensorInfo &sensorInfo, + const ControlInfoMap &sensorControls) +{ + const ControlInfo &v4l2Exposure = sensorControls.find(V4L2_CID_EXPOSURE)->second; + int32_t minExposure = v4l2Exposure.min().get(); + int32_t maxExposure = v4l2Exposure.max().get(); + + utils::Duration lineDuration = sensorInfo.lineLength * 1.0s + / sensorInfo.pixelRate; + + const ControlInfo &v4l2Gain = sensorControls.find(V4L2_CID_ANALOGUE_GAIN)->second; + int32_t minGain = v4l2Gain.min().get(); + int32_t maxGain = v4l2Gain.max().get(); + + /* + * When the AGC computes the new exposure values for a frame, it needs + * to know the limits for shutter speed and analogue gain. + * As it depends on the sensor, update it with the controls. + * + * \todo take VBLANK into account for maximum shutter speed + */ + context_.configuration.agc.minShutterSpeed = minExposure * lineDuration; + context_.configuration.agc.maxShutterSpeed = maxExposure * lineDuration; + context_.configuration.agc.minAnalogueGain = camHelper_->gain(minGain); + context_.configuration.agc.maxAnalogueGain = camHelper_->gain(maxGain); +} /* * Compute camera controls using the sensor information and the sensor @@ -436,15 +487,18 @@ int IPAIPU3::configure(const IPAConfigInfo &configInfo, calculateBdsGrid(configInfo.bdsOutputSize); + /* Update the camera controls using the new sensor settings. */ + updateControls(sensorInfo_, ctrls_, ipaControls); + + /* Update the IPASessionConfiguration using the sensor settings. */ + updateSessionConfiguration(sensorInfo_, ctrls_); + for (auto const &algo : algorithms_) { int ret = algo->configure(context_, configInfo); if (ret) return ret; } - /* Update the camera controls using the new sensor settings. */ - updateControls(sensorInfo_, ctrls_, ipaControls); - return 0; } @@ -543,10 +597,6 @@ void IPAIPU3::parseStatistics(unsigned int frame, { ControlList ctrls(controls::controls); - /* \todo These fields should not be written by the IPAIPU3 layer */ - context_.frameContext.agc.gain = camHelper_->gain(gain_); - context_.frameContext.agc.exposure = exposure_; - for (auto const &algo : algorithms_) algo->process(context_, stats); From patchwork Thu Oct 21 16:43:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14224 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 054EFBF415 for ; Thu, 21 Oct 2021 16:44:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C4627604FE; Thu, 21 Oct 2021 18:44:11 +0200 (CEST) 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="m29Xc7OF"; 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 F29FC68F58 for ; Thu, 21 Oct 2021 18:44:05 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A7478276; Thu, 21 Oct 2021 18:44:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834645; bh=keDBfUcAXAlJz708mF3rSdt9VbM8+247wgTlmK1kiQk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m29Xc7OFgNluybqDat5sju/3xSXX17fpdkSo9cCkN7oXfeXrUbYcFk5BemQXRss1C dkoXyhj4Lcloso8usH91GfedJJimo4Dndy0Mq0gyXtcc23hyGu5FtMbfEfMLcboKGe 0JgWNhfgu8o5Qn+4xHPXnR6mLTQFakcEwqXIxo4w= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:50 +0200 Message-Id: <20211021164401.110033-4-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 03/14] ipa: ipu3: awb: Use saturation under 90% 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 AWB grey world algorithm tries to find a grey value and it can't do it on over-exposed images. To exclude those, the saturation ratio is used for each cell, and the cell is included only if this ratio is 0. Now that we have changed the threshold, more cells may be considered as partially saturated and excluded, preventing the algorithm from running efficiently. Change that behaviour, and consider 90% as a good enough ratio. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/ipa/ipu3/algorithms/awb.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/ipa/ipu3/algorithms/awb.cpp b/src/ipa/ipu3/algorithms/awb.cpp index 4364928c..ce01791b 100644 --- a/src/ipa/ipu3/algorithms/awb.cpp +++ b/src/ipa/ipu3/algorithms/awb.cpp @@ -19,6 +19,18 @@ LOG_DEFINE_CATEGORY(IPU3Awb) static constexpr uint32_t kMinGreenLevelInZone = 32; +/* + * Minimum proportion of non-saturated cells in a zone for the zone to be used + * by the AWB algorithm. + */ +static constexpr double kMinRelevantCellsRatio = 0.8; + +/* + * Maximum ratio of saturated pixels in a cell for the cell to be considered + * non-saturated and counted by the AWB algorithm. + */ +static constexpr uint32_t kSaturationThreshold = 255 * 90 / 100; + /** * \struct Accumulator * \brief RGB statistics for a given zone @@ -160,7 +172,8 @@ int Awb::configure(IPAContext &context, * for it to be relevant for the grey world algorithm. * \todo This proportion could be configured. */ - cellsPerZoneThreshold_ = cellsPerZoneX_ * cellsPerZoneY_ * 80 / 100; + cellsPerZoneThreshold_ = cellsPerZoneX_ * cellsPerZoneY_ * kMinRelevantCellsRatio; + LOG(IPU3Awb, Debug) << "Threshold for AWB is set to " << cellsPerZoneThreshold_; return 0; } @@ -234,7 +247,18 @@ void Awb::generateAwbStats(const ipu3_uapi_stats_3a *stats) reinterpret_cast( &stats->awb_raw_buffer.meta_data[cellPosition] ); - if (currentCell->sat_ratio == 0) { + + /* + * Use cells which have less than 90% + * saturation as an initial means to include + * otherwise bright cells which are not fully + * saturated. + * + * \todo The 90% saturation rate may require + * further empirical measurements and + * optimisation during camera tuning phases. + */ + if (currentCell->sat_ratio <= kSaturationThreshold) { /* The cell is not saturated, use the current cell */ awbStats_[awbZonePosition].counted++; uint32_t greenValue = currentCell->Gr_avg + currentCell->Gb_avg; From patchwork Thu Oct 21 16:43:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14225 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 A384EBF415 for ; Thu, 21 Oct 2021 16:44:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1E55468F75; Thu, 21 Oct 2021 18:44:12 +0200 (CEST) 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="D80qY1yC"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 31FCC604FE for ; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D6D681B43; Thu, 21 Oct 2021 18:44:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834645; bh=E216AD2M1rlwmKEgOdZoe323Nvn2NliIkjgje2A59EU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D80qY1yC/9LmieTGjUXoOYCJtPVR2y94uX60ngUSURqH+wUlKE5udsQgjpyRCE9uC egPUuecJatk4YeczzoBDS547k6ngJiKurCXOY/ewdRwPY5wVbmk8232r+lqfgJmgQ0 +Bug6x2hciH8ha/bpjL1AZ7jvWRofOzE7UtXf2js= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:51 +0200 Message-Id: <20211021164401.110033-5-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 04/14] ipa: ipu3: awb: Change minimal green threshold 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 zones are used for the grey world algorithm, they are only considered if their average green value is at least 32/255 to exclude zones that are too dark and don't provide relevant colour information (on the opposite side of the spectrum, saturated regions are excluded by the ImgU statistics engine). The algorithm requires a minimal number of zones that meet this criteria in order to run. Now that we correct the black level, the 32/255 minimal value is a bit high and prevents the algorithm for running in low-light conditions. Lower the value to 16/255 to fix it. Signed-off-by: Jean-Michel Hautbois Acked-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/awb.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ipa/ipu3/algorithms/awb.cpp b/src/ipa/ipu3/algorithms/awb.cpp index ce01791b..ae78cb87 100644 --- a/src/ipa/ipu3/algorithms/awb.cpp +++ b/src/ipa/ipu3/algorithms/awb.cpp @@ -17,7 +17,14 @@ namespace ipa::ipu3::algorithms { LOG_DEFINE_CATEGORY(IPU3Awb) -static constexpr uint32_t kMinGreenLevelInZone = 32; +/* + * When zones are used for the grey world algorithm, they are only considered if + * their average green value is at least 16/255 (after black level subtraction) + * to exclude zones that are too dark and don't provide relevant colour + * information (on the opposite side of the spectrum, saturated regions are + * excluded by the ImgU statistics engine). + */ +static constexpr uint32_t kMinGreenLevelInZone = 16; /* * Minimum proportion of non-saturated cells in a zone for the zone to be used From patchwork Thu Oct 21 16:43:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14226 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 2D755C324E for ; Thu, 21 Oct 2021 16:44:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A4DB368F61; Thu, 21 Oct 2021 18:44:12 +0200 (CEST) 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="Hgvib1VG"; 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 6BCB168F5C for ; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1F4BE1BF9; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834646; bh=l+Mz/wHRifSiN6OzVUIf7GYOxdpXJIVhxxPXpPt0Sko=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hgvib1VGHk1qWMUdW0V+Ct2Yc4Y8bx9IYRp2o5cw9EXD79Ky5/YKULjdfhCbNuV4n +LBT5XYhAneC1+ch4tGqpIlcrAXcawDcwnTIFNCBvZky7BLcuP/1yAOmmKs9lIsPEX oBkX7asI1n05ehUkAcpDIEaLS8B/4YjjzvhmFe9M= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:52 +0200 Message-Id: <20211021164401.110033-6-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 05/14] ipa: ipu3: agc: Rename exposure values properly 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 exposure value is filtered in filterExposure() using the currentExposure_ and setting a prevExposure_ variable. This is misnamed as it is not the previous exposure, but a filtered value. Rename it accordingly. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 28 ++++++++++++++-------------- src/ipa/ipu3/algorithms/agc.h | 4 ++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 828e822c..62ea0916 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -49,7 +49,7 @@ static constexpr double kEvGainTarget = 0.5; Agc::Agc() : frameCount_(0), lastFrame_(0), iqMean_(0.0), lineDuration_(0s), - maxExposureTime_(0s), prevExposure_(0s), prevExposureNoDg_(0s), + maxExposureTime_(0s), filteredExposure_(0s), filteredExposureNoDg_(0s), currentExposure_(0s), currentExposureNoDg_(0s) { } @@ -100,24 +100,24 @@ void Agc::processBrightness(const ipu3_uapi_stats_3a *stats, void Agc::filterExposure() { double speed = 0.2; - if (prevExposure_ == 0s) { + if (filteredExposure_ == 0s) { /* DG stands for digital gain.*/ - prevExposure_ = currentExposure_; - prevExposureNoDg_ = currentExposureNoDg_; + filteredExposure_ = currentExposure_; + filteredExposureNoDg_ = currentExposureNoDg_; } else { /* * If we are close to the desired result, go faster to avoid making * multiple micro-adjustments. * \ todo: Make this customisable? */ - if (prevExposure_ < 1.2 * currentExposure_ && - prevExposure_ > 0.8 * currentExposure_) + if (filteredExposure_ < 1.2 * currentExposure_ && + filteredExposure_ > 0.8 * currentExposure_) speed = sqrt(speed); - prevExposure_ = speed * currentExposure_ + - prevExposure_ * (1.0 - speed); - prevExposureNoDg_ = speed * currentExposureNoDg_ + - prevExposureNoDg_ * (1.0 - speed); + filteredExposure_ = speed * currentExposure_ + + filteredExposure_ * (1.0 - speed); + filteredExposureNoDg_ = speed * currentExposureNoDg_ + + filteredExposureNoDg_ * (1.0 - speed); } /* * We can't let the no_dg exposure deviate too far below the @@ -125,10 +125,10 @@ void Agc::filterExposure() * in the ISP to hide it (which will cause nasty oscillation). */ double fastReduceThreshold = 0.4; - if (prevExposureNoDg_ < - prevExposure_ * fastReduceThreshold) - prevExposureNoDg_ = prevExposure_ * fastReduceThreshold; - LOG(IPU3Agc, Debug) << "After filtering, total_exposure " << prevExposure_; + if (filteredExposureNoDg_ < + filteredExposure_ * fastReduceThreshold) + filteredExposureNoDg_ = filteredExposure_ * fastReduceThreshold; + LOG(IPU3Agc, Debug) << "After filtering, total_exposure " << filteredExposure_; } void Agc::lockExposureGain(uint32_t &exposure, double &gain) diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h index e0c315fc..16b9fa0a 100644 --- a/src/ipa/ipu3/algorithms/agc.h +++ b/src/ipa/ipu3/algorithms/agc.h @@ -44,8 +44,8 @@ private: utils::Duration lineDuration_; utils::Duration maxExposureTime_; - utils::Duration prevExposure_; - utils::Duration prevExposureNoDg_; + utils::Duration filteredExposure_; + utils::Duration filteredExposureNoDg_; utils::Duration currentExposure_; utils::Duration currentExposureNoDg_; From patchwork Thu Oct 21 16:43:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14227 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 86C58C324F for ; Thu, 21 Oct 2021 16:44:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 836AD68F6E; Thu, 21 Oct 2021 18:44:15 +0200 (CEST) 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="MKay6o1o"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 988C268F5F for ; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 53CAE276; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834646; bh=Hy+TDqG/3B5iRlX4WoQ7KJSk+yCGiD9F16WtlbDpCyw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MKay6o1oDCowzE2xMKmWiHaCqNm6dwd/Ka0lIOtvOSm3ckPpxBYN/E9O2UCbbVdHY Pkm6RYlpDUqCL958dDe24n1vJsfCVoRdywmHMqKmXFXYfHSxo0yWOkWk2hd2Y0slNc n9PwWyxpIAanLse4SY1IcR81Qx+uyf9ojJGGbDDI= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:53 +0200 Message-Id: <20211021164401.110033-7-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 06/14] ipa: ipu3: agc: Change exposure limits 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" We are using arbitrary constants for the exposure limit in a number of lines. Instead of using static constants for those, use the limits of the sensor passed in IPASessionConfiguration and cache those. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 35 +++++++++++++++++++-------------- src/ipa/ipu3/algorithms/agc.h | 3 ++- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 62ea0916..58872410 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -39,18 +39,14 @@ static constexpr uint32_t kMaxISO = 1500; static constexpr uint32_t kMinGain = kMinISO / 100; static constexpr uint32_t kMaxGain = kMaxISO / 100; -/* \todo use calculated value based on sensor */ -static constexpr uint32_t kMinExposure = 1; -static constexpr uint32_t kMaxExposure = 1976; - /* Histogram constants */ static constexpr uint32_t knumHistogramBins = 256; static constexpr double kEvGainTarget = 0.5; Agc::Agc() : frameCount_(0), lastFrame_(0), iqMean_(0.0), lineDuration_(0s), - maxExposureTime_(0s), filteredExposure_(0s), filteredExposureNoDg_(0s), - currentExposure_(0s), currentExposureNoDg_(0s) + minExposureLines_(0), maxExposureLines_(0), filteredExposure_(0s), + filteredExposureNoDg_(0s), currentExposure_(0s), currentExposureNoDg_(0s) { } @@ -60,13 +56,15 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo) lineDuration_ = configInfo.sensorInfo.lineLength * 1.0s / configInfo.sensorInfo.pixelRate; - maxExposureTime_ = context.configuration.agc.maxShutterSpeed; + + /* \todo replace the exposure in lines storage with time based ones. */ + minExposureLines_ = context.configuration.agc.minShutterSpeed / lineDuration_; + maxExposureLines_ = context.configuration.agc.maxShutterSpeed / lineDuration_; /* Configure the default exposure and gain. */ context.frameContext.agc.gain = context.configuration.agc.minAnalogueGain; - context.frameContext.agc.exposure = - context.configuration.agc.minShutterSpeed / lineDuration_; + context.frameContext.agc.exposure = minExposureLines_; return 0; } @@ -151,23 +149,30 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain) LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_ << " Shutter speed " << currentShutter << " Gain " << gain; + currentExposure_ = currentExposureNoDg_ * newGain; - utils::Duration maxTotalExposure = maxExposureTime_ * kMaxGain; + utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_; + utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain; currentExposure_ = std::min(currentExposure_, maxTotalExposure); - LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_; + LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_ + << ", maximum is " << maxTotalExposure; /* \todo: estimate if we need to desaturate */ filterExposure(); utils::Duration newExposure = 0.0s; - if (currentShutter < maxExposureTime_) { - exposure = std::clamp(static_cast(exposure * currentExposure_ / currentExposureNoDg_), kMinExposure, kMaxExposure); + if (currentShutter < maxShutterSpeed) { + exposure = std::clamp(exposure * currentExposure_ / currentExposureNoDg_, + minExposureLines_, + maxExposureLines_); newExposure = currentExposure_ / exposure; gain = std::clamp(static_cast(gain * currentExposure_ / newExposure), kMinGain, kMaxGain); - } else if (currentShutter >= maxExposureTime_) { + } else { gain = std::clamp(static_cast(gain * currentExposure_ / currentExposureNoDg_), kMinGain, kMaxGain); newExposure = currentExposure_ / gain; - exposure = std::clamp(static_cast(exposure * currentExposure_ / newExposure), kMinExposure, kMaxExposure); + exposure = std::clamp(exposure * currentExposure_ / newExposure, + minExposureLines_, + maxExposureLines_); } LOG(IPU3Agc, Debug) << "Adjust exposure " << exposure * lineDuration_ << " and gain " << gain; } diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h index 16b9fa0a..cd26d08c 100644 --- a/src/ipa/ipu3/algorithms/agc.h +++ b/src/ipa/ipu3/algorithms/agc.h @@ -42,7 +42,8 @@ private: double iqMean_; utils::Duration lineDuration_; - utils::Duration maxExposureTime_; + uint32_t minExposureLines_; + uint32_t maxExposureLines_; utils::Duration filteredExposure_; utils::Duration filteredExposureNoDg_; From patchwork Thu Oct 21 16:43:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14230 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 204FEC324F for ; Thu, 21 Oct 2021 16:44:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C706168F6A; Thu, 21 Oct 2021 18:44:17 +0200 (CEST) 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="kkVqLmsi"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E645C68F63 for ; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 86AD61B43; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834646; bh=XTkopjKBNdiyvaIsQDg5GdSEqax9j7HZwEP8P0M4210=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kkVqLmsifURK8igc4KpJ7Va3y8imVttkWL8dtGjM9ky82xqXli6q8ZQdls3N/lhyv j2C3RGyHtw0VtD15CgSjT3iRoqi0oOalpOuGYeql6ZaNqosdV/ADlQCETUCr8NTNxa BB9awRVed2xECnPt498Ki2H2Dxlhlb6vAeJTarbo= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:54 +0200 Message-Id: <20211021164401.110033-8-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 07/14] ipa: ipu3: agc: Change analogue gain limits 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 gains is currently set as a uint32_t while the analogue gain is passed as a double. We also have a default maximum analogue gain of 15 which is quite high for a number of sensors. Use a maximum value of 8 which should really be configured by the IPA and not fixed as it is now. While at it make it a double. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 58872410..984aed53 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -30,14 +30,10 @@ static constexpr uint32_t kInitialFrameMinAECount = 4; /* Number of frames to wait between new gain/exposure estimations */ static constexpr uint32_t kFrameSkipCount = 6; -/* Maximum ISO value for analogue gain */ -static constexpr uint32_t kMinISO = 100; -static constexpr uint32_t kMaxISO = 1500; - /* Maximum analogue gain value * \todo grab it from a camera helper */ -static constexpr uint32_t kMinGain = kMinISO / 100; -static constexpr uint32_t kMaxGain = kMaxISO / 100; +static constexpr double kMinGain = 1.0; +static constexpr double kMaxGain = 8.0; /* Histogram constants */ static constexpr uint32_t knumHistogramBins = 256; @@ -166,9 +162,11 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain) minExposureLines_, maxExposureLines_); newExposure = currentExposure_ / exposure; - gain = std::clamp(static_cast(gain * currentExposure_ / newExposure), kMinGain, kMaxGain); + gain = std::clamp(gain * currentExposure_ / newExposure, + kMinGain, kMaxGain); } else { - gain = std::clamp(static_cast(gain * currentExposure_ / currentExposureNoDg_), kMinGain, kMaxGain); + gain = std::clamp(gain * currentExposure_ / currentExposureNoDg_, + kMinGain, kMaxGain); newExposure = currentExposure_ / gain; exposure = std::clamp(exposure * currentExposure_ / newExposure, minExposureLines_, From patchwork Thu Oct 21 16:43:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14228 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 E55A6BF415 for ; Thu, 21 Oct 2021 16:44:16 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 644FB68F6C; Thu, 21 Oct 2021 18:44:16 +0200 (CEST) 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="F4E3Vf9+"; 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 252F168F56 for ; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B885A276; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834646; bh=srdGptDZrbVSL5ezYI4sZ6vqkkOPFTP2tJfeJYCoLEA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F4E3Vf9+b05uA+zAYNnJYuhKRnAqcHOqdNcfKgMQjkGwXY2nm9UseulLCIQAI//jP vXHFEYvSac/eSM1GQOn5XRhKeYK62JansC194h6lNC3gWBEMWPmr2zizXLw0panpV6 U7/+oBiXd4PIys7GFPo70Ev2eyAK1wvqyy1N+xEU= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:55 +0200 Message-Id: <20211021164401.110033-9-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 08/14] ipa: ipu3: agc: Use filtered exposure values 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" We are filtering the exposure value to limit the gain to apply, but we are not using the result. Fix it. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 984aed53..f5bb3328 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -158,17 +158,17 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain) utils::Duration newExposure = 0.0s; if (currentShutter < maxShutterSpeed) { - exposure = std::clamp(exposure * currentExposure_ / currentExposureNoDg_, + exposure = std::clamp(exposure * filteredExposure_ / currentExposureNoDg_, minExposureLines_, maxExposureLines_); - newExposure = currentExposure_ / exposure; - gain = std::clamp(gain * currentExposure_ / newExposure, + newExposure = filteredExposure_ / exposure; + gain = std::clamp(gain * filteredExposure_ / newExposure, kMinGain, kMaxGain); } else { - gain = std::clamp(gain * currentExposure_ / currentExposureNoDg_, + gain = std::clamp(gain * filteredExposure_ / currentExposureNoDg_, kMinGain, kMaxGain); - newExposure = currentExposure_ / gain; - exposure = std::clamp(exposure * currentExposure_ / newExposure, + newExposure = filteredExposure_ / gain; + exposure = std::clamp(exposure * filteredExposure_ / newExposure, minExposureLines_, maxExposureLines_); } From patchwork Thu Oct 21 16:43:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14229 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 79FA6C324E for ; Thu, 21 Oct 2021 16:44:17 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3485468F67; Thu, 21 Oct 2021 18:44:17 +0200 (CEST) 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="i/70ghJs"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 543CB68F65 for ; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id E3CE81BF9; Thu, 21 Oct 2021 18:44:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834647; bh=1fvnUVg/yH6VB5c9tJHL1bOaHUCOYL8HY+KmS3iCQp4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i/70ghJs6HTdnWVy7zMPcNZVtE6JGMx7vgBheL0Lw0hVxc0B3f3FbDzoqsk56Ew8P zh99RqWhAWIXKJ/8WpCJMwt5OeotcfDLjpt0C0IM2gI8oOcDzPsy/eoqCEgeW43+Iw sMueSpVXpzYsuBWGyc+t0vRu8XhD3sbZs6ZRCySI= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:56 +0200 Message-Id: <20211021164401.110033-10-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 09/14] ipa: ipu3: agc: Simplify division of exposure/gain 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" Until now, the algorithm makes complex assumptions when dividing the exposure and analogue gain values. Instead, use a simpler clamping of the shutter speed first, and then of the analogue gain, based on the limits configured. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/ipa/ipu3/algorithms/agc.cpp | 36 +++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index f5bb3328..6f5b6a45 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -147,7 +147,9 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain) << " Gain " << gain; currentExposure_ = currentExposureNoDg_ * newGain; + utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_; utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_; + utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain; currentExposure_ = std::min(currentExposure_, maxTotalExposure); LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_ @@ -156,23 +158,23 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain) /* \todo: estimate if we need to desaturate */ filterExposure(); - utils::Duration newExposure = 0.0s; - if (currentShutter < maxShutterSpeed) { - exposure = std::clamp(exposure * filteredExposure_ / currentExposureNoDg_, - minExposureLines_, - maxExposureLines_); - newExposure = filteredExposure_ / exposure; - gain = std::clamp(gain * filteredExposure_ / newExposure, - kMinGain, kMaxGain); - } else { - gain = std::clamp(gain * filteredExposure_ / currentExposureNoDg_, - kMinGain, kMaxGain); - newExposure = filteredExposure_ / gain; - exposure = std::clamp(exposure * filteredExposure_ / newExposure, - minExposureLines_, - maxExposureLines_); - } - LOG(IPU3Agc, Debug) << "Adjust exposure " << exposure * lineDuration_ << " and gain " << gain; + utils::Duration exposureValue = filteredExposure_; + utils::Duration shutterTime = minShutterSpeed; + + /* + * Push the shutter time up to the maximum first, and only then + * increase the gain. + */ + shutterTime = std::clamp(exposureValue / kMinGain, + minShutterSpeed, maxShutterSpeed); + double stepGain = std::clamp(exposureValue / shutterTime, + kMinGain, kMaxGain); + LOG(IPU3Agc, Debug) << "Divided up shutter and gain are " + << shutterTime << " and " + << stepGain; + + exposure = shutterTime / lineDuration_; + gain = stepGain; } lastFrame_ = frameCount_; } From patchwork Thu Oct 21 16:43:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14231 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 CC0A9BF415 for ; Thu, 21 Oct 2021 16:44:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7930768F63; Thu, 21 Oct 2021 18:44:18 +0200 (CEST) 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="BtMTgAXG"; 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 759F768F67 for ; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1F54A1B43; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834647; bh=95b+8gHG7d1Chs0ugj5CAFjD02nbkB4j+53yWcB6gEg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BtMTgAXGlJ4VI7qxPtOarRvJbTcUja7064ZrdwVMNLrZzCuYbdM2h4ZfqruEfp9Zd uiErKnbACnwrUGmpH8Fc81Fy5Nq++3fvRBc8b8mzT/XO6LjUA2H4LM13WXkgPlgVCl 8ZtY4TLu5JwIfvMqE556QBUWT7r2i1bB+SYUiyV8= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:57 +0200 Message-Id: <20211021164401.110033-11-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 10/14] ipa: ipu3: agc: Rename gains properly 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" We have mixed terms between gain, analogue gain and the exposure value gain. Make it clear when we are using the analogue gain from the sensor, and when we are using the calculated gain to be applied to the exposure value to reach the target. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 6f5b6a45..e7c59f3d 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -125,7 +125,7 @@ void Agc::filterExposure() LOG(IPU3Agc, Debug) << "After filtering, total_exposure " << filteredExposure_; } -void Agc::lockExposureGain(uint32_t &exposure, double &gain) +void Agc::lockExposureGain(uint32_t &exposure, double &analogueGain) { /* Algorithm initialization should wait for first valid frames */ /* \todo - have a number of frames given by DelayedControls ? @@ -137,16 +137,17 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain) if (std::abs(iqMean_ - kEvGainTarget * knumHistogramBins) <= 1) { LOG(IPU3Agc, Debug) << "!!! Good exposure with iqMean = " << iqMean_; } else { - double newGain = kEvGainTarget * knumHistogramBins / iqMean_; + double evGain = kEvGainTarget * knumHistogramBins / iqMean_; /* extracted from Rpi::Agc::computeTargetExposure */ utils::Duration currentShutter = exposure * lineDuration_; - currentExposureNoDg_ = currentShutter * gain; + currentExposureNoDg_ = currentShutter * analogueGain; LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_ << " Shutter speed " << currentShutter - << " Gain " << gain; + << " Gain " << analogueGain + << " Needed ev gain " << evGain; - currentExposure_ = currentExposureNoDg_ * newGain; + currentExposure_ = currentExposureNoDg_ * evGain; utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_; utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_; @@ -174,7 +175,7 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain) << stepGain; exposure = shutterTime / lineDuration_; - gain = stepGain; + analogueGain = stepGain; } lastFrame_ = frameCount_; } @@ -182,9 +183,9 @@ void Agc::lockExposureGain(uint32_t &exposure, double &gain) void Agc::process(IPAContext &context, const ipu3_uapi_stats_3a *stats) { uint32_t &exposure = context.frameContext.agc.exposure; - double &gain = context.frameContext.agc.gain; + double &analogueGain = context.frameContext.agc.gain; processBrightness(stats, context.configuration.grid.bdsGrid); - lockExposureGain(exposure, gain); + lockExposureGain(exposure, analogueGain); frameCount_++; } From patchwork Thu Oct 21 16:43:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14233 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 E5BA5C324F for ; Thu, 21 Oct 2021 16:44:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A0DB368F7D; Thu, 21 Oct 2021 18:44:19 +0200 (CEST) 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="GTg5FcUh"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9A5DB68F5D for ; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 54ED8276; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834647; bh=svqh+A4Ef6cG7H2+0geRHj5YGbf2ySWXbeprnI7qqEA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GTg5FcUh76vahX2Iegsh6wMeKKrerkecz9Q2yXFEbCupcU66v5UMi45uSqfDzu9DZ uySYpVS31vtEeAPZ4pPRlTh18bDv/iGfGvCMvdu3xcWrhuo5hEQVJV9HJrNot8s503 P5yPJLXLDCz3hHl2jynfISIY0O2mQgFOA0fZEq3c= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:58 +0200 Message-Id: <20211021164401.110033-12-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 11/14] ipa: ipu3: agc: Introduce previous exposure 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" We need to calculate the gain on the previous exposure value calculated. Now that we initialise the exposure and gain values in configure(), we know the initial exposure value, and we can set it before any loop is running. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 19 +++++++++++++++++-- src/ipa/ipu3/algorithms/agc.h | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index e7c59f3d..43ef89df 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -42,7 +42,8 @@ static constexpr double kEvGainTarget = 0.5; Agc::Agc() : frameCount_(0), lastFrame_(0), iqMean_(0.0), lineDuration_(0s), minExposureLines_(0), maxExposureLines_(0), filteredExposure_(0s), - filteredExposureNoDg_(0s), currentExposure_(0s), currentExposureNoDg_(0s) + filteredExposureNoDg_(0s), currentExposure_(0s), + currentExposureNoDg_(0s), prevExposureValue_(0s) { } @@ -62,6 +63,10 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo) context.configuration.agc.minAnalogueGain; context.frameContext.agc.exposure = minExposureLines_; + prevExposureValue_ = context.frameContext.agc.gain + * context.frameContext.agc.exposure + * lineDuration_; + return 0; } @@ -147,7 +152,7 @@ void Agc::lockExposureGain(uint32_t &exposure, double &analogueGain) << " Gain " << analogueGain << " Needed ev gain " << evGain; - currentExposure_ = currentExposureNoDg_ * evGain; + currentExposure_ = prevExposureValue_ * evGain; utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_; utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_; @@ -176,6 +181,16 @@ void Agc::lockExposureGain(uint32_t &exposure, double &analogueGain) exposure = shutterTime / lineDuration_; analogueGain = stepGain; + + /* + * Update the exposure value for the next process call. + * + * \todo Obtain the values of the exposure time and analog gain + * that were actually used by the sensor, either from embedded + * data when available, or from the delayed controls + * infrastructure in case a slow down caused a mismatch. + */ + prevExposureValue_ = shutterTime * analogueGain; } lastFrame_ = frameCount_; } diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h index cd26d08c..2ae88e9f 100644 --- a/src/ipa/ipu3/algorithms/agc.h +++ b/src/ipa/ipu3/algorithms/agc.h @@ -49,6 +49,7 @@ private: utils::Duration filteredExposureNoDg_; utils::Duration currentExposure_; utils::Duration currentExposureNoDg_; + utils::Duration prevExposureValue_; uint32_t stride_; }; From patchwork Thu Oct 21 16:43:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14232 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 61B2DC324E for ; Thu, 21 Oct 2021 16:44:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 19DFD68F6F; Thu, 21 Oct 2021 18:44:19 +0200 (CEST) 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="DDBrpRbS"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D259168F59 for ; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 878581BF9; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834647; bh=u4QrSlBw8RRNyogIi79NoUHOvwIRMulp/WW640l/xjM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DDBrpRbSSoZgFd5RXO5D571EBlBqFl0Q/lh2g9hTzKGTnvleSnj2MiesqieGorrnC IzNizPUyR0ABUJaSk50czTa+ViPfEqp5xdCXjIzcxARWXdWqtBFvUrrbZoRnCyso82 QhfGVZKWZ07ysVxnYb7uce9SXtMPC1Vlh+XV3Gbw= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:43:59 +0200 Message-Id: <20211021164401.110033-13-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 12/14] ipa: ipu3: agc: Refactor condition on exposure correction 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" Until now, we can't know when the exposure and gains applied in the IPAIPU3::setControls() are really applied (it can be several frames). We don't want to use the values calculated as if they are already applied, and this is done by testing the frame number. When the exposure is estimated, we verify if it changed enough for exposure and gain to be updated. Keep the indentation level removal and simply return when the change is so small there is no need to apply the newly calculated value. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/ipa/ipu3/algorithms/agc.cpp | 91 ++++++++++++++++----------------- 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 43ef89df..7ff3029b 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -138,60 +138,59 @@ void Agc::lockExposureGain(uint32_t &exposure, double &analogueGain) if ((frameCount_ < kInitialFrameMinAECount) || (frameCount_ - lastFrame_ < kFrameSkipCount)) return; - /* Are we correctly exposed ? */ - if (std::abs(iqMean_ - kEvGainTarget * knumHistogramBins) <= 1) { - LOG(IPU3Agc, Debug) << "!!! Good exposure with iqMean = " << iqMean_; - } else { - double evGain = kEvGainTarget * knumHistogramBins / iqMean_; + if (std::abs(iqMean_ - kEvGainTarget * knumHistogramBins) <= 1) + return; - /* extracted from Rpi::Agc::computeTargetExposure */ - utils::Duration currentShutter = exposure * lineDuration_; - currentExposureNoDg_ = currentShutter * analogueGain; - LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_ - << " Shutter speed " << currentShutter - << " Gain " << analogueGain - << " Needed ev gain " << evGain; + double evGain = kEvGainTarget * knumHistogramBins / iqMean_; - currentExposure_ = prevExposureValue_ * evGain; - utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_; - utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_; + /* extracted from Rpi::Agc::computeTargetExposure */ + utils::Duration currentShutter = exposure * lineDuration_; + currentExposureNoDg_ = currentShutter * analogueGain; + LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_ + << " Shutter speed " << currentShutter + << " Gain " << analogueGain + << " Needed ev gain " << evGain; - utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain; - currentExposure_ = std::min(currentExposure_, maxTotalExposure); - LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_ - << ", maximum is " << maxTotalExposure; + currentExposure_ = prevExposureValue_ * evGain; + utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_; + utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_; - /* \todo: estimate if we need to desaturate */ - filterExposure(); + utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain; + currentExposure_ = std::min(currentExposure_, maxTotalExposure); + LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_ + << ", maximum is " << maxTotalExposure; - utils::Duration exposureValue = filteredExposure_; - utils::Duration shutterTime = minShutterSpeed; + /* \todo: estimate if we need to desaturate */ + filterExposure(); - /* - * Push the shutter time up to the maximum first, and only then - * increase the gain. - */ - shutterTime = std::clamp(exposureValue / kMinGain, - minShutterSpeed, maxShutterSpeed); - double stepGain = std::clamp(exposureValue / shutterTime, - kMinGain, kMaxGain); - LOG(IPU3Agc, Debug) << "Divided up shutter and gain are " - << shutterTime << " and " - << stepGain; + utils::Duration exposureValue = filteredExposure_; + utils::Duration shutterTime = minShutterSpeed; - exposure = shutterTime / lineDuration_; - analogueGain = stepGain; + /* + * Push the shutter time up to the maximum first, and only then + * increase the gain. + */ + shutterTime = std::clamp(exposureValue / kMinGain, + minShutterSpeed, maxShutterSpeed); + double stepGain = std::clamp(exposureValue / shutterTime, + kMinGain, kMaxGain); + LOG(IPU3Agc, Debug) << "Divided up shutter and gain are " + << shutterTime << " and " + << stepGain; + + exposure = shutterTime / lineDuration_; + analogueGain = stepGain; + + /* + * Update the exposure value for the next process call. + * + * \todo Obtain the values of the exposure time and analog gain + * that were actually used by the sensor, either from embedded + * data when available, or from the delayed controls + * infrastructure in case a slow down caused a mismatch. + */ + prevExposureValue_ = shutterTime * analogueGain; - /* - * Update the exposure value for the next process call. - * - * \todo Obtain the values of the exposure time and analog gain - * that were actually used by the sensor, either from embedded - * data when available, or from the delayed controls - * infrastructure in case a slow down caused a mismatch. - */ - prevExposureValue_ = shutterTime * analogueGain; - } lastFrame_ = frameCount_; } From patchwork Thu Oct 21 16:44:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14235 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 DEBA1BF415 for ; Thu, 21 Oct 2021 16:44:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 77E3A68F58; Thu, 21 Oct 2021 18:44:22 +0200 (CEST) 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="oFeKQ/AE"; 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 0F05468F66 for ; Thu, 21 Oct 2021 18:44:08 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id BAC88276; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834647; bh=YQz84iqHXQQkhtZTBWbSavUwMYXPPfr2v/mO0lATRgM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oFeKQ/AEESbub0l+ihZtkATnSwHXnrZmg0wgEejeAkJsY4OQEbpIgtEtqtW93uznG VG4iVfJ+ljNVu9/S2MjPhH6jRpHgL2xnE2TEKz5NjIma7PHbFWqwApwYUOJuZ0qitF woSQswOnYb0KulAU1EMB5hR9uHFsN8sqlzCW/+8A= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:44:00 +0200 Message-Id: <20211021164401.110033-14-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 13/14] ipa: ipu3: agc: Remove unused variables 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" We currently control the exposure value by the shutter speed and the analogue gain. We can't use the digital gain to have more than the maximum exposure value calculated because we are not controlling it. Remove unused code associated with this digital gain. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/ipa/ipu3/algorithms/agc.cpp | 19 +++---------------- src/ipa/ipu3/algorithms/agc.h | 2 -- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index 7ff3029b..be59d0b0 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -42,8 +42,7 @@ static constexpr double kEvGainTarget = 0.5; Agc::Agc() : frameCount_(0), lastFrame_(0), iqMean_(0.0), lineDuration_(0s), minExposureLines_(0), maxExposureLines_(0), filteredExposure_(0s), - filteredExposureNoDg_(0s), currentExposure_(0s), - currentExposureNoDg_(0s), prevExposureValue_(0s) + currentExposure_(0s), prevExposureValue_(0s) { } @@ -102,7 +101,6 @@ void Agc::filterExposure() if (filteredExposure_ == 0s) { /* DG stands for digital gain.*/ filteredExposure_ = currentExposure_; - filteredExposureNoDg_ = currentExposureNoDg_; } else { /* * If we are close to the desired result, go faster to avoid making @@ -115,18 +113,8 @@ void Agc::filterExposure() filteredExposure_ = speed * currentExposure_ + filteredExposure_ * (1.0 - speed); - filteredExposureNoDg_ = speed * currentExposureNoDg_ + - filteredExposureNoDg_ * (1.0 - speed); } - /* - * We can't let the no_dg exposure deviate too far below the - * total exposure, as there might not be enough digital gain available - * in the ISP to hide it (which will cause nasty oscillation). - */ - double fastReduceThreshold = 0.4; - if (filteredExposureNoDg_ < - filteredExposure_ * fastReduceThreshold) - filteredExposureNoDg_ = filteredExposure_ * fastReduceThreshold; + LOG(IPU3Agc, Debug) << "After filtering, total_exposure " << filteredExposure_; } @@ -145,8 +133,7 @@ void Agc::lockExposureGain(uint32_t &exposure, double &analogueGain) /* extracted from Rpi::Agc::computeTargetExposure */ utils::Duration currentShutter = exposure * lineDuration_; - currentExposureNoDg_ = currentShutter * analogueGain; - LOG(IPU3Agc, Debug) << "Actual total exposure " << currentExposureNoDg_ + LOG(IPU3Agc, Debug) << "Actual total exposure " << currentShutter * analogueGain << " Shutter speed " << currentShutter << " Gain " << analogueGain << " Needed ev gain " << evGain; diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h index 2ae88e9f..ad133b98 100644 --- a/src/ipa/ipu3/algorithms/agc.h +++ b/src/ipa/ipu3/algorithms/agc.h @@ -46,9 +46,7 @@ private: uint32_t maxExposureLines_; utils::Duration filteredExposure_; - utils::Duration filteredExposureNoDg_; utils::Duration currentExposure_; - utils::Duration currentExposureNoDg_; utils::Duration prevExposureValue_; uint32_t stride_; From patchwork Thu Oct 21 16:44:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 14234 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 483D2BF415 for ; Thu, 21 Oct 2021 16:44:22 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0399D68F59; Thu, 21 Oct 2021 18:44:22 +0200 (CEST) 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="TsBQdQA3"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3CAE068F6E for ; Thu, 21 Oct 2021 18:44:08 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:f9d:5926:ad90:4996]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id EB8731B43; Thu, 21 Oct 2021 18:44:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634834648; bh=wM2lLOuBwTd6CC8sQKovG3glGzpPlIoPZqyEqAXWu8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TsBQdQA3qvcveSuqqb17/P0LT4zGxN3fXCV44sVOtFW1SFZAyf6ZAuNub0JpkvHnC Dai8XdnikaYG5kIh5NOXKeC+H06XaI66avPwcE3+8Kc83vyLdalgPwTELW+636jFM8 rWp9M6lOEDeBA8vz9SkBFSGJTFBuuPtB6xJC5uFw= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Oct 2021 18:44:01 +0200 Message-Id: <20211021164401.110033-15-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> References: <20211021164401.110033-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 14/14] ipa: ipu3: Use sensor limits for analogue gain 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 using constants for the analogue gains limits, use the minimum and maximum from the configured sensor. Signed-off-by: Jean-Michel Hautbois Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/ipa/ipu3/algorithms/agc.cpp | 19 ++++++++++--------- src/ipa/ipu3/algorithms/agc.h | 3 +++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index be59d0b0..2981abc7 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -30,10 +30,9 @@ static constexpr uint32_t kInitialFrameMinAECount = 4; /* Number of frames to wait between new gain/exposure estimations */ static constexpr uint32_t kFrameSkipCount = 6; -/* Maximum analogue gain value - * \todo grab it from a camera helper */ -static constexpr double kMinGain = 1.0; -static constexpr double kMaxGain = 8.0; +/* Limits for analogue gain values */ +static constexpr double kMinAnalogueGain = 1.0; +static constexpr double kMaxAnalogueGain = 8.0; /* Histogram constants */ static constexpr uint32_t knumHistogramBins = 256; @@ -57,9 +56,11 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo) minExposureLines_ = context.configuration.agc.minShutterSpeed / lineDuration_; maxExposureLines_ = context.configuration.agc.maxShutterSpeed / lineDuration_; + minAnalogueGain_ = std::max(context.configuration.agc.minAnalogueGain, kMinAnalogueGain); + maxAnalogueGain_ = std::min(context.configuration.agc.maxAnalogueGain, kMaxAnalogueGain); + /* Configure the default exposure and gain. */ - context.frameContext.agc.gain = - context.configuration.agc.minAnalogueGain; + context.frameContext.agc.gain = minAnalogueGain_; context.frameContext.agc.exposure = minExposureLines_; prevExposureValue_ = context.frameContext.agc.gain @@ -142,7 +143,7 @@ void Agc::lockExposureGain(uint32_t &exposure, double &analogueGain) utils::Duration minShutterSpeed = minExposureLines_ * lineDuration_; utils::Duration maxShutterSpeed = maxExposureLines_ * lineDuration_; - utils::Duration maxTotalExposure = maxShutterSpeed * kMaxGain; + utils::Duration maxTotalExposure = maxShutterSpeed * maxAnalogueGain_; currentExposure_ = std::min(currentExposure_, maxTotalExposure); LOG(IPU3Agc, Debug) << "Target total exposure " << currentExposure_ << ", maximum is " << maxTotalExposure; @@ -157,10 +158,10 @@ void Agc::lockExposureGain(uint32_t &exposure, double &analogueGain) * Push the shutter time up to the maximum first, and only then * increase the gain. */ - shutterTime = std::clamp(exposureValue / kMinGain, + shutterTime = std::clamp(exposureValue / minAnalogueGain_, minShutterSpeed, maxShutterSpeed); double stepGain = std::clamp(exposureValue / shutterTime, - kMinGain, kMaxGain); + minAnalogueGain_, maxAnalogueGain_); LOG(IPU3Agc, Debug) << "Divided up shutter and gain are " << shutterTime << " and " << stepGain; diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h index ad133b98..1840205b 100644 --- a/src/ipa/ipu3/algorithms/agc.h +++ b/src/ipa/ipu3/algorithms/agc.h @@ -45,6 +45,9 @@ private: uint32_t minExposureLines_; uint32_t maxExposureLines_; + double minAnalogueGain_; + double maxAnalogueGain_; + utils::Duration filteredExposure_; utils::Duration currentExposure_; utils::Duration prevExposureValue_;