From patchwork Wed Aug 23 12:09:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 18955 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 1EDD5BF415 for ; Wed, 23 Aug 2023 12:09:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 795A6628D3; Wed, 23 Aug 2023 14:09:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1692792568; bh=GXR53e9ZMMN7FEeQLxwMvMJw0aKoGYRWt1/n+HsBzS4=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=W1kNP57zjmybdPjGl90EghtcscMZ1+I2OjWNLD/AEOxelgG9jDyQOH6qIjRphaJAA levcfdipczEKFx2caWXof3KCOQ+l0c0j9z7+nY1NLz1Ox7L3yF4O1Y8xj805llryFa HkNOhWX+e9rbm1voqGNgUsdkgm7J+85SeStauyj8W76jUPrztEjKgPtLdV5nnhRDc6 lBbFhF3AIxdRZGCykIW1g7sBk+dFY/7NYKeKoHma17Vnv1puydKp43NTRB05X90+2s ZIjolHfHMlA+ZcD4SP9HxeA7pCjhnTLbKJtgaQs8DE3Z54pXXWJBfpt/G3dL4uiO9A wFcVsGVe1r5tA== Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DF094627E0 for ; Wed, 23 Aug 2023 14:09:23 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="tf+TFdtT"; dkim-atps=neutral Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-4fe15bfb1adso8368046e87.0 for ; Wed, 23 Aug 2023 05:09:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1692792563; x=1693397363; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WVYw2x9Nser63k+jpeDj6sKU4/vs2CXhB/RQyQOHhlA=; b=tf+TFdtTYq9S8TlGtS8llERvtXTHkGQBoGTcg+lbngxx5+dz5AVOz7S5e+gujRIYRo EHCRAi2Dqvl+Q8uDgZ+F47HHjTdI7pzcOOSs9RgYVwltkTENFhAKOXbSRlusW9lPB/Jc etYeUFAlh4n0I2Rd3R1vmlto2KMoktXet9p/sP9/R7KsM2RYz2/61ryFJsOX/kFctQU9 BYRxhb39pnaaDsFyLTO5HrBEw0cwSm9oQofuOM9667jQrv+XXRtsiZhyeT8ar8Kp5rWH mvHpBW1Jb3AxPgOAaO2ezBUMIwg2gGWbVhCsODwnLqTAHfw0x+ltt4AkAAwPKJix7zgD 9Xzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692792563; x=1693397363; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WVYw2x9Nser63k+jpeDj6sKU4/vs2CXhB/RQyQOHhlA=; b=PP2YAnkcUhruAAr+PhyqCrdQ5Oahh4w+im/8l6gGXkvEUX/pPNVIRLs75HBMuSYDk4 te1FvisQhjdfNQA1PAJ3bCq1vyj4+lfh3grMHUwOV6cYjT3RqXw6WMtMZiBQHb6PrmHO fYdi4ihsRuyFSBBIRtA0YYz+6JE+8X0dxq/CKZfZwNGl33n4VTDtx46kCgk7i1yOoZ4Y fzQk7Dmixk/8MGFHB4ACrKULOJ3lemXqK2P7F+x7/adL5Z7TVWWIlG9bIpRSQg3eZCBD HjttalnlOyjqZ83IL16V/Y0eHsb1IQEZvzQA+3Ba2MupXNCzvZbn5UgiQAeUDVIIjGS+ lIeA== X-Gm-Message-State: AOJu0YwbwF199QmHMAZ5zL+aWr6UsFBysGjvL97j9qyEyyHfi4x/zsRy JNHI0J1FJ2BfO60InG9yPA6m7bW4p8TWBoxDnrc= X-Google-Smtp-Source: AGHT+IENdKcJs+SMKhq+egJGF/BWKb845VBkMQqlk9zULgo1tzAnt4k8vlS/HpiWYY8sXIEcjhpb6g== X-Received: by 2002:a05:6512:3d1a:b0:4fe:18be:ef37 with SMTP id d26-20020a0565123d1a00b004fe18beef37mr11332691lfv.61.1692792562662; Wed, 23 Aug 2023 05:09:22 -0700 (PDT) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:2bce:64d6:1a5c:49a2]) by smtp.gmail.com with ESMTPSA id u1-20020a7bcb01000000b003fefcbe7fa8sm3800589wmj.28.2023.08.23.05.09.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 05:09:21 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 23 Aug 2023 13:09:15 +0100 Message-Id: <20230823120915.18621-6-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230823120915.18621-1-david.plowman@raspberrypi.com> References: <20230823120915.18621-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 5/5] ipa: rpi: agc: Use channel constraints in the AGC algorithm 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: , X-Patchwork-Original-From: David Plowman via libcamera-devel From: David Plowman Reply-To: David Plowman Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Whenever we run Agc::process(), we store the most recent total exposure requested for each channel. With these values we can apply the channel constraints after time-filtering the requested total exposure, but before working out how much digital gain is needed. Signed-off-by: David Plowman Reviewed-by: Naushir Patuck --- src/ipa/rpi/controller/rpi/agc.cpp | 21 ++++-- src/ipa/rpi/controller/rpi/agc.h | 1 + src/ipa/rpi/controller/rpi/agc_channel.cpp | 75 +++++++++++++++++++--- src/ipa/rpi/controller/rpi/agc_channel.h | 8 ++- 4 files changed, 88 insertions(+), 17 deletions(-) diff --git a/src/ipa/rpi/controller/rpi/agc.cpp b/src/ipa/rpi/controller/rpi/agc.cpp index 7e627bba..3c730d4c 100644 --- a/src/ipa/rpi/controller/rpi/agc.cpp +++ b/src/ipa/rpi/controller/rpi/agc.cpp @@ -40,6 +40,7 @@ int Agc::read(const libcamera::YamlObject ¶ms) */ if (!params.contains("channels")) { LOG(RPiAgc, Debug) << "Single channel only"; + channelTotalExposures_.resize(1, 0s); channelData_.emplace_back(); return channelData_.back().channel.read(params, getHardwareConfig()); } @@ -59,6 +60,8 @@ int Agc::read(const libcamera::YamlObject ¶ms) return -1; } + channelTotalExposures_.resize(channelData_.size(), 0s); + return 0; } @@ -234,15 +237,21 @@ static void getChannelIndex(Metadata *metadata, const char *message, unsigned in LOG(RPiAgc, Debug) << message; } -static void setChannelIndex(Metadata *metadata, const char *message, unsigned int channelIndex) +static libcamera::utils::Duration +setChannelIndex(Metadata *metadata, const char *message, unsigned int channelIndex) { std::unique_lock lock(*metadata); AgcStatus *status = metadata->getLocked("agc.status"); - if (status) + libcamera::utils::Duration dur = 0s; + + if (status) { status->channel = channelIndex; - else + dur = status->totalExposureValue; + } else /* This does happen at startup, otherwise it would be a Warning or Error. */ LOG(RPiAgc, Debug) << message; + + return dur; } void Agc::prepare(Metadata *imageMetadata) @@ -306,8 +315,10 @@ void Agc::process(StatisticsPtr &stats, Metadata *imageMetadata) /* Can also happen when new channels start. */ LOG(RPiAgc, Debug) << "process: channel " << channelIndex << " not seen yet"; - channelData.channel.process(stats, &deviceStatus, imageMetadata); - setChannelIndex(imageMetadata, "process: no AGC status found", channelIndex); + channelData.channel.process(stats, &deviceStatus, imageMetadata, channelTotalExposures_); + auto dur = setChannelIndex(imageMetadata, "process: no AGC status found", channelIndex); + if (dur) + channelTotalExposures_[channelIndex] = dur; /* And onto the next channel for the next call. */ index_ = (index_ + 1) % activeChannels_.size(); diff --git a/src/ipa/rpi/controller/rpi/agc.h b/src/ipa/rpi/controller/rpi/agc.h index 2eed2bab..e301c5cb 100644 --- a/src/ipa/rpi/controller/rpi/agc.h +++ b/src/ipa/rpi/controller/rpi/agc.h @@ -54,6 +54,7 @@ private: std::vector channelData_; std::vector activeChannels_; unsigned int index_; /* index into the activeChannels_ */ + AgcChannelTotalExposures channelTotalExposures_; }; } /* namespace RPiController */ diff --git a/src/ipa/rpi/controller/rpi/agc_channel.cpp b/src/ipa/rpi/controller/rpi/agc_channel.cpp index 44198c2c..54fd08e8 100644 --- a/src/ipa/rpi/controller/rpi/agc_channel.cpp +++ b/src/ipa/rpi/controller/rpi/agc_channel.cpp @@ -175,6 +175,11 @@ readConstraintModes(std::map &constraintModes, int AgcChannelConstraint::read(const libcamera::YamlObject ¶ms) { + auto channelValue = params["channel"].get(); + if (!channelValue) + return -EINVAL; + channel = *channelValue; + std::string boundString = params["bound"].get(""); transform(boundString.begin(), boundString.end(), boundString.begin(), ::toupper); @@ -184,10 +189,10 @@ int AgcChannelConstraint::read(const libcamera::YamlObject ¶ms) } bound = boundString == "UPPER" ? Bound::UPPER : Bound::LOWER; - auto value = params["factor"].get(); - if (!value) + auto factorValue = params["factor"].get(); + if (!factorValue) return -EINVAL; - factor = *value; + factor = *factorValue; return 0; } @@ -231,6 +236,7 @@ int AgcConfig::read(const libcamera::YamlObject ¶ms) ret = readChannelConstraints(channelConstraints, params["channel_constraints"]); if (ret) return ret; + LOG(RPiAgc, Info) << "Read " << channelConstraints.size() << " channel constraints"; } ret = yTarget.read(params["y_target"]); @@ -489,7 +495,9 @@ void AgcChannel::prepare(Metadata *imageMetadata) } } -void AgcChannel::process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata) +void AgcChannel::process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, + Metadata *imageMetadata, + const AgcChannelTotalExposures &channelTotalExposures) { frameCount_++; /* @@ -508,12 +516,17 @@ void AgcChannel::process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, computeTargetExposure(gain); /* The results have to be filtered so as not to change too rapidly. */ filterExposure(); + /* + * We may be asked to limit the exposure using other channels. If another channel + * determines our upper bound we may want to know this later. + */ + bool channelBound = applyChannelConstraints(channelTotalExposures); /* * Some of the exposure has to be applied as digital gain, so work out - * what that is. This function also tells us whether it's decided to - * "desaturate" the image more quickly. + * what that is. It also tells us whether it's trying to desaturate the image + * more quickly, which can only happen when another channel is not limiting us. */ - bool desaturate = applyDigitalGain(gain, targetY); + bool desaturate = applyDigitalGain(gain, targetY, channelBound); /* * The last thing is to divide up the exposure value into a shutter time * and analogue gain, according to the current exposure mode. @@ -792,7 +805,49 @@ void AgcChannel::computeTargetExposure(double gain) LOG(RPiAgc, Debug) << "Target totalExposure " << target_.totalExposure; } -bool AgcChannel::applyDigitalGain(double gain, double targetY) +bool AgcChannel::applyChannelConstraints(const AgcChannelTotalExposures &channelTotalExposures) +{ + bool channelBound = false; + LOG(RPiAgc, Debug) + << "Total exposure before channel constraints " << filtered_.totalExposure; + + for (const auto &constraint : config_.channelConstraints) { + LOG(RPiAgc, Debug) + << "Check constraint: channel " << constraint.channel << " bound " + << (constraint.bound == AgcChannelConstraint::Bound::UPPER ? "UPPER" : "LOWER") + << " factor " << constraint.factor; + if (constraint.channel >= channelTotalExposures.size() || + !channelTotalExposures[constraint.channel]) { + LOG(RPiAgc, Debug) << "no such channel - skipped"; + continue; + } + + if (channelTotalExposures[constraint.channel] == 0s) { + LOG(RPiAgc, Debug) << "zero exposure - skipped"; + continue; + } + + libcamera::utils::Duration limitExposure = + channelTotalExposures[constraint.channel] * constraint.factor; + LOG(RPiAgc, Debug) << "Limit exposure " << limitExposure; + if ((constraint.bound == AgcChannelConstraint::Bound::UPPER && + filtered_.totalExposure > limitExposure) || + (constraint.bound == AgcChannelConstraint::Bound::LOWER && + filtered_.totalExposure < limitExposure)) { + filtered_.totalExposure = limitExposure; + LOG(RPiAgc, Debug) << "Applies"; + channelBound = true; + } else + LOG(RPiAgc, Debug) << "Does not apply"; + } + + LOG(RPiAgc, Debug) + << "Total exposure after channel constraints " << filtered_.totalExposure; + + return channelBound; +} + +bool AgcChannel::applyDigitalGain(double gain, double targetY, bool channelBound) { double minColourGain = std::min({ awb_.gainR, awb_.gainG, awb_.gainB, 1.0 }); ASSERT(minColourGain != 0.0); @@ -812,8 +867,8 @@ bool AgcChannel::applyDigitalGain(double gain, double targetY) * quickly (and we then approach the correct value more quickly from * below). */ - bool desaturate = targetY > config_.fastReduceThreshold && - gain < sqrt(targetY); + bool desaturate = !channelBound && + targetY > config_.fastReduceThreshold && gain < sqrt(targetY); if (desaturate) dg /= config_.fastReduceThreshold; LOG(RPiAgc, Debug) << "Digital gain " << dg << " desaturate? " << desaturate; diff --git a/src/ipa/rpi/controller/rpi/agc_channel.h b/src/ipa/rpi/controller/rpi/agc_channel.h index 446125ef..c3f701eb 100644 --- a/src/ipa/rpi/controller/rpi/agc_channel.h +++ b/src/ipa/rpi/controller/rpi/agc_channel.h @@ -19,6 +19,8 @@ namespace RPiController { +using AgcChannelTotalExposures = std::vector; + struct AgcMeteringMode { std::vector weights; int read(const libcamera::YamlObject ¶ms); @@ -93,7 +95,8 @@ public: void disableAuto(); void switchMode(CameraMode const &cameraMode, Metadata *metadata); void prepare(Metadata *imageMetadata); - void process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata); + void process(StatisticsPtr &stats, const DeviceStatus *deviceStatus, Metadata *imageMetadata, + const AgcChannelTotalExposures &channelTotalExposures); private: bool updateLockStatus(DeviceStatus const &deviceStatus); @@ -105,7 +108,8 @@ private: double &gain, double &targetY); void computeTargetExposure(double gain); void filterExposure(); - bool applyDigitalGain(double gain, double targetY); + bool applyChannelConstraints(const AgcChannelTotalExposures &channelTotalExposures); + bool applyDigitalGain(double gain, double targetY, bool channelBound); void divideUpExposure(); void writeAndFinish(Metadata *imageMetadata, bool desaturate); libcamera::utils::Duration limitShutter(libcamera::utils::Duration shutter);