From patchwork Mon Mar 27 09:34:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 18467 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 222C5C0F2A for ; Mon, 27 Mar 2023 09:34:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B3E4562722; Mon, 27 Mar 2023 11:34:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1679909689; bh=LtIvZoQ2b9m0ZRNttQCaBfvcaSM+z/JFpHVCN58WoZU=; 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=tUC/o68R112u1Xng/lcTItxKgacGrYr/jZ5iFOCvnxazbJY9n1AytEQG3NsFnMLqE YXy0oaXvP1KwGwQq4CuG48BVMePpn3044fj1DFsmUEgkn+CrO30PuerWSdoVPgfcU9 vTzFNdGFlyeeEMjMzigKYGYKqkp865jbnyTyW/cZWJytHqNkTofNdwYmP57RNXW9uQ /B60+opChQ9SGNFdQuYcAAVqNzRbYyvVDzYViN5mblY5JuqvCiN6yxIBXG+eDV0Omt F20kmoTDGxAkzhblgFGIa1+IGTEuISOX62purwjetw736f6bw5KirVQs+AoxPB5XhB swovBWfMrkaMw== Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 98A7862734 for ; Mon, 27 Mar 2023 11:34:45 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="jcq7uoRL"; dkim-atps=neutral Received: by mail-io1-xd2a.google.com with SMTP id z1so3596792iot.11 for ; Mon, 27 Mar 2023 02:34:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1679909684; 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=J1p8odmcQLQ8khz9y4InWwm5liYe3di4rkdWz5nZxo0=; b=jcq7uoRLUHqcNNyTv0Xv1GDd8NBTdOO7ge2Rzqcwe87fNDth1PEb+GQCfcPx0aYKpI GJQc0yvlCewEI33vFRXhYitn0PNnEWv0v7vGM7mPoISuyG3r8ek23Lkmq66hbsODxiQX lPCjvaiLMt7i9qP0RtTgYCkw4/abliyhphWZTRBNlIu/7srBpRTHf1UVznfjZSqeKC1m 57KTUPPXh0/q53EQ8k6rVTmB8UqDDb/eLbcazykpwcO2idw58t+snki7BYs7r11yN7Df +LqyoR8CfpMCBFlsP+DHoMPkLUW6grrIgk5OqW1GhH0wVGZokOo1BrhUHWttb+g/xwKg 296w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679909684; 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=J1p8odmcQLQ8khz9y4InWwm5liYe3di4rkdWz5nZxo0=; b=BGqTBZpspqAzi7muQIggod/10sKsYWXB3Xxf7Zww8Ds7fxftKRquqSYaTTii7sV3BI K2f2qF2FQX1lDSRF63jN51XIFwvC97995D3l78rGiv1lBKdGf3xhmuFtv/+Uu65fZJuf CoenOIz0zHXMDRUxrWMEDmAUX0bgDZDlxIwPJGT/sRNjFLdsDh2raUpjRYSDM7LXiETo RY97Kuom5S+ZKwYF90W+ZHgchPOErGPqUp5L52GH/0qqA7FLGfdvCLoosZAuF7CMFtzf KgfIG5gPaslZgWBHE4a2FnzK3rNKXQ1bOS1zqbFYoCxHDqRXw0FeuV6IpINzmkTbV2uo vRUg== X-Gm-Message-State: AO0yUKXuzs6A2Vg8UDq4RHYkYWpbzQ+LMe9g3x8KEIl3Dmm+hHnBOHmd Q72a+f8nh6IXkn0r/l2D7ppdSmoSEH9VCH1xpf4EKQ== X-Google-Smtp-Source: AK7set/iielrwtrI/ajdwkjEq3aUZTj+gW52er+epZ1AwOwJC4UgrsY8zTNkBkRPP1Vj5WqojEfUFQ== X-Received: by 2002:a6b:6101:0:b0:71b:5cd7:fcd9 with SMTP id v1-20020a6b6101000000b0071b5cd7fcd9mr8123158iob.20.1679909684066; Mon, 27 Mar 2023 02:34:44 -0700 (PDT) Received: from localhost.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id d12-20020a056602328c00b0075464a69c10sm7658345ioz.10.2023.03.27.02.34.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 02:34:43 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Mon, 27 Mar 2023 10:34:38 +0100 Message-Id: <20230327093439.24670-4-naush@raspberrypi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230327093439.24670-1-naush@raspberrypi.com> References: <20230327093439.24670-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/4] ipa: raspberrypi: Ensure shutter speed and gain are clipped in the AGC 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: Naushir Patuck via libcamera-devel From: Naushir Patuck Reply-To: Naushir Patuck Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Make a copy of the CameraMode structure on a switch mode call. This replaces the existing lastSensitivity_ field. Limit the AGC gain calculations to the minimum value given by the CameraMode structure. The maximum value remains unclipped as any gain over the sensor maximum will be made up by digital gain. Rename clipShutter to limitShutter for consistency, and have the latter limit the shutter speed to both upper and lower bounds. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman Reviewed-by: Jacopo Mondi --- src/ipa/raspberrypi/controller/rpi/agc.cpp | 55 +++++++++++++++++----- src/ipa/raspberrypi/controller/rpi/agc.h | 4 +- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp index 4ea0dd41e66c..ed7ecbe05a8e 100644 --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp @@ -309,14 +309,14 @@ void Agc::setFixedShutter(Duration fixedShutter) { fixedShutter_ = fixedShutter; /* Set this in case someone calls disableAuto() straight after. */ - status_.shutterTime = clipShutter(fixedShutter_); + status_.shutterTime = limitShutter(fixedShutter_); } void Agc::setFixedAnalogueGain(double fixedAnalogueGain) { fixedAnalogueGain_ = fixedAnalogueGain; /* Set this in case someone calls disableAuto() straight after. */ - status_.analogueGain = fixedAnalogueGain; + status_.analogueGain = limitGain(fixedAnalogueGain); } void Agc::setMeteringMode(std::string const &meteringModeName) @@ -342,7 +342,7 @@ void Agc::switchMode(CameraMode const &cameraMode, housekeepConfig(); - Duration fixedShutter = clipShutter(fixedShutter_); + Duration fixedShutter = limitShutter(fixedShutter_); if (fixedShutter && fixedAnalogueGain_) { /* We're going to reset the algorithm here with these fixed values. */ @@ -371,7 +371,7 @@ void Agc::switchMode(CameraMode const &cameraMode, * current exposure profile, which takes care of everything else. */ - double ratio = lastSensitivity_ / cameraMode.sensitivity; + double ratio = mode_.sensitivity / cameraMode.sensitivity; target_.totalExposureNoDG *= ratio; target_.totalExposure *= ratio; filtered_.totalExposureNoDG *= ratio; @@ -393,8 +393,11 @@ void Agc::switchMode(CameraMode const &cameraMode, writeAndFinish(metadata, false); - /* We must remember the sensitivity of this mode for the next SwitchMode. */ - lastSensitivity_ = cameraMode.sensitivity; + /* + * Store the more in the local state. We must remember the sensitivity + * of this mode for the next SwitchMode. + */ + mode_ = cameraMode; } void Agc::prepare(Metadata *imageMetadata) @@ -516,7 +519,7 @@ void Agc::housekeepConfig() { /* First fetch all the up-to-date settings, so no one else has to do it. */ status_.ev = ev_; - status_.fixedShutter = clipShutter(fixedShutter_); + status_.fixedShutter = limitShutter(fixedShutter_); status_.fixedAnalogueGain = fixedAnalogueGain_; status_.flickerPeriod = flickerPeriod_; LOG(RPiAgc, Debug) << "ev " << status_.ev << " fixedShutter " @@ -703,7 +706,7 @@ void Agc::computeTargetExposure(double gain) Duration maxShutter = status_.fixedShutter ? status_.fixedShutter : exposureMode_->shutter.back(); - maxShutter = clipShutter(maxShutter); + maxShutter = limitShutter(maxShutter); Duration maxTotalExposure = maxShutter * (status_.fixedAnalogueGain != 0.0 @@ -803,15 +806,16 @@ void Agc::divideUpExposure() double analogueGain; shutterTime = status_.fixedShutter ? status_.fixedShutter : exposureMode_->shutter[0]; - shutterTime = clipShutter(shutterTime); + shutterTime = limitShutter(shutterTime); analogueGain = status_.fixedAnalogueGain != 0.0 ? status_.fixedAnalogueGain : exposureMode_->gain[0]; + analogueGain = limitGain(analogueGain); if (shutterTime * analogueGain < exposureValue) { for (unsigned int stage = 1; stage < exposureMode_->gain.size(); stage++) { if (!status_.fixedShutter) { Duration stageShutter = - clipShutter(exposureMode_->shutter[stage]); + limitShutter(exposureMode_->shutter[stage]); if (stageShutter * analogueGain >= exposureValue) { shutterTime = exposureValue / analogueGain; break; @@ -824,6 +828,7 @@ void Agc::divideUpExposure() break; } analogueGain = exposureMode_->gain[stage]; + analogueGain = limitGain(analogueGain); } } } @@ -846,6 +851,7 @@ void Agc::divideUpExposure() * gain as a side-effect. */ analogueGain = std::min(analogueGain, exposureMode_->gain.back()); + analogueGain = limitGain(analogueGain); shutterTime = newShutterTime; } LOG(RPiAgc, Debug) << "After flicker avoidance, shutter " @@ -872,13 +878,36 @@ void Agc::writeAndFinish(Metadata *imageMetadata, bool desaturate) << " analogue gain " << filtered_.analogueGain; } -Duration Agc::clipShutter(Duration shutter) +Duration Agc::limitShutter(Duration shutter) { - if (maxShutter_) - shutter = std::min(shutter, maxShutter_); + /* + * shutter == 0 is a special case for fixed shutter values, and must pass + * through unchanged + */ + if (!shutter) + return shutter; + + shutter = std::clamp(shutter, mode_.minShutter, maxShutter_); return shutter; } +double Agc::limitGain(double gain) const +{ + /* + * Only limit the lower bounds of the gain value to what the sensor limits. + * The upper bound on analogue gain will be made up with additional digital + * gain applied by the ISP. + * + * gain == 0.0 is a special case for fixed shutter values, and must pass + * through unchanged + */ + if (!gain) + return gain; + + gain = std::max(gain, mode_.minAnalogueGain); + return gain; +} + /* Register algorithm with the system. */ static Algorithm *create(Controller *controller) { diff --git a/src/ipa/raspberrypi/controller/rpi/agc.h b/src/ipa/raspberrypi/controller/rpi/agc.h index f04896ca25ad..661d8b08cea2 100644 --- a/src/ipa/raspberrypi/controller/rpi/agc.h +++ b/src/ipa/raspberrypi/controller/rpi/agc.h @@ -103,10 +103,12 @@ private: void filterExposure(bool desaturate); void divideUpExposure(); void writeAndFinish(Metadata *imageMetadata, bool desaturate); - libcamera::utils::Duration clipShutter(libcamera::utils::Duration shutter); + libcamera::utils::Duration limitShutter(libcamera::utils::Duration shutter); + double limitGain(double gain) const; AgcMeteringMode *meteringMode_; AgcExposureMode *exposureMode_; AgcConstraintMode *constraintMode_; + CameraMode mode_; uint64_t frameCount_; AwbStatus awb_; struct ExposureValues {