{"id":18467,"url":"https://patchwork.libcamera.org/api/1.1/patches/18467/?format=json","web_url":"https://patchwork.libcamera.org/patch/18467/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20230327093439.24670-4-naush@raspberrypi.com>","date":"2023-03-27T09:34:38","name":"[libcamera-devel,v2,3/4] ipa: raspberrypi: Ensure shutter speed and gain are clipped in the AGC","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"87cdd74447b68a5ed495002030d8cd7f2302c4ab","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/1.1/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18467/mbox/","series":[{"id":3818,"url":"https://patchwork.libcamera.org/api/1.1/series/3818/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3818","date":"2023-03-27T09:34:35","name":"Raspberry Pi: Sensor limits","version":2,"mbox":"https://patchwork.libcamera.org/series/3818/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18467/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18467/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 222C5C0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 27 Mar 2023 09:34:50 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B3E4562722;\n\tMon, 27 Mar 2023 11:34:49 +0200 (CEST)","from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com\n\t[IPv6:2607:f8b0:4864:20::d2a])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 98A7862734\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 27 Mar 2023 11:34:45 +0200 (CEST)","by mail-io1-xd2a.google.com with SMTP id z1so3596792iot.11\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 27 Mar 2023 02:34:45 -0700 (PDT)","from localhost.localdomain ([93.93.133.154])\n\tby smtp.gmail.com with ESMTPSA id\n\td12-20020a056602328c00b0075464a69c10sm7658345ioz.10.2023.03.27.02.34.43\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 27 Mar 2023 02:34:43 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679909689;\n\tbh=LtIvZoQ2b9m0ZRNttQCaBfvcaSM+z/JFpHVCN58WoZU=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=tUC/o68R112u1Xng/lcTItxKgacGrYr/jZ5iFOCvnxazbJY9n1AytEQG3NsFnMLqE\n\tYXy0oaXvP1KwGwQq4CuG48BVMePpn3044fj1DFsmUEgkn+CrO30PuerWSdoVPgfcU9\n\tvTzFNdGFlyeeEMjMzigKYGYKqkp865jbnyTyW/cZWJytHqNkTofNdwYmP57RNXW9uQ\n\t/B60+opChQ9SGNFdQuYcAAVqNzRbYyvVDzYViN5mblY5JuqvCiN6yxIBXG+eDV0Omt\n\tF20kmoTDGxAkzhblgFGIa1+IGTEuISOX62purwjetw736f6bw5KirVQs+AoxPB5XhB\n\tswovBWfMrkaMw==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1679909684;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=J1p8odmcQLQ8khz9y4InWwm5liYe3di4rkdWz5nZxo0=;\n\tb=jcq7uoRLUHqcNNyTv0Xv1GDd8NBTdOO7ge2Rzqcwe87fNDth1PEb+GQCfcPx0aYKpI\n\tGJQc0yvlCewEI33vFRXhYitn0PNnEWv0v7vGM7mPoISuyG3r8ek23Lkmq66hbsODxiQX\n\tlPCjvaiLMt7i9qP0RtTgYCkw4/abliyhphWZTRBNlIu/7srBpRTHf1UVznfjZSqeKC1m\n\t57KTUPPXh0/q53EQ8k6rVTmB8UqDDb/eLbcazykpwcO2idw58t+snki7BYs7r11yN7Df\n\t+LqyoR8CfpMCBFlsP+DHoMPkLUW6grrIgk5OqW1GhH0wVGZokOo1BrhUHWttb+g/xwKg\n\t296w=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"jcq7uoRL\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112; t=1679909684;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=J1p8odmcQLQ8khz9y4InWwm5liYe3di4rkdWz5nZxo0=;\n\tb=BGqTBZpspqAzi7muQIggod/10sKsYWXB3Xxf7Zww8Ds7fxftKRquqSYaTTii7sV3BI\n\tK2f2qF2FQX1lDSRF63jN51XIFwvC97995D3l78rGiv1lBKdGf3xhmuFtv/+Uu65fZJuf\n\tCoenOIz0zHXMDRUxrWMEDmAUX0bgDZDlxIwPJGT/sRNjFLdsDh2raUpjRYSDM7LXiETo\n\tRY97Kuom5S+ZKwYF90W+ZHgchPOErGPqUp5L52GH/0qqA7FLGfdvCLoosZAuF7CMFtzf\n\tKgfIG5gPaslZgWBHE4a2FnzK3rNKXQ1bOS1zqbFYoCxHDqRXw0FeuV6IpINzmkTbV2uo\n\tvRUg==","X-Gm-Message-State":"AO0yUKXuzs6A2Vg8UDq4RHYkYWpbzQ+LMe9g3x8KEIl3Dmm+hHnBOHmd\n\tQ72a+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\n\tv1-20020a6b6101000000b0071b5cd7fcd9mr8123158iob.20.1679909684066; \n\tMon, 27 Mar 2023 02:34:44 -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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 3/4] ipa: raspberrypi: Ensure shutter\n\tspeed and gain are clipped in the AGC","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Naushir Patuck via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Naushir Patuck <naush@raspberrypi.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Make a copy of the CameraMode structure on a switch mode call. This\nreplaces the existing lastSensitivity_ field.\n\nLimit the AGC gain calculations to the minimum value given by the\nCameraMode structure. The maximum value remains unclipped as any gain\nover the sensor maximum will be made up by digital gain.\n\nRename clipShutter to limitShutter for consistency, and have the latter\nlimit the shutter speed to both upper and lower bounds.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n src/ipa/raspberrypi/controller/rpi/agc.cpp | 55 +++++++++++++++++-----\n src/ipa/raspberrypi/controller/rpi/agc.h   |  4 +-\n 2 files changed, 45 insertions(+), 14 deletions(-)","diff":"diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp\nindex 4ea0dd41e66c..ed7ecbe05a8e 100644\n--- a/src/ipa/raspberrypi/controller/rpi/agc.cpp\n+++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp\n@@ -309,14 +309,14 @@ void Agc::setFixedShutter(Duration fixedShutter)\n {\n \tfixedShutter_ = fixedShutter;\n \t/* Set this in case someone calls disableAuto() straight after. */\n-\tstatus_.shutterTime = clipShutter(fixedShutter_);\n+\tstatus_.shutterTime = limitShutter(fixedShutter_);\n }\n \n void Agc::setFixedAnalogueGain(double fixedAnalogueGain)\n {\n \tfixedAnalogueGain_ = fixedAnalogueGain;\n \t/* Set this in case someone calls disableAuto() straight after. */\n-\tstatus_.analogueGain = fixedAnalogueGain;\n+\tstatus_.analogueGain = limitGain(fixedAnalogueGain);\n }\n \n void Agc::setMeteringMode(std::string const &meteringModeName)\n@@ -342,7 +342,7 @@ void Agc::switchMode(CameraMode const &cameraMode,\n \n \thousekeepConfig();\n \n-\tDuration fixedShutter = clipShutter(fixedShutter_);\n+\tDuration fixedShutter = limitShutter(fixedShutter_);\n \tif (fixedShutter && fixedAnalogueGain_) {\n \t\t/* We're going to reset the algorithm here with these fixed values. */\n \n@@ -371,7 +371,7 @@ void Agc::switchMode(CameraMode const &cameraMode,\n \t\t * current exposure profile, which takes care of everything else.\n \t\t */\n \n-\t\tdouble ratio = lastSensitivity_ / cameraMode.sensitivity;\n+\t\tdouble ratio = mode_.sensitivity / cameraMode.sensitivity;\n \t\ttarget_.totalExposureNoDG *= ratio;\n \t\ttarget_.totalExposure *= ratio;\n \t\tfiltered_.totalExposureNoDG *= ratio;\n@@ -393,8 +393,11 @@ void Agc::switchMode(CameraMode const &cameraMode,\n \n \twriteAndFinish(metadata, false);\n \n-\t/* We must remember the sensitivity of this mode for the next SwitchMode. */\n-\tlastSensitivity_ = cameraMode.sensitivity;\n+\t/*\n+\t * Store the more in the local state. We must remember the sensitivity\n+\t * of this mode for the next SwitchMode.\n+\t */\n+\tmode_ = cameraMode;\n }\n \n void Agc::prepare(Metadata *imageMetadata)\n@@ -516,7 +519,7 @@ void Agc::housekeepConfig()\n {\n \t/* First fetch all the up-to-date settings, so no one else has to do it. */\n \tstatus_.ev = ev_;\n-\tstatus_.fixedShutter = clipShutter(fixedShutter_);\n+\tstatus_.fixedShutter = limitShutter(fixedShutter_);\n \tstatus_.fixedAnalogueGain = fixedAnalogueGain_;\n \tstatus_.flickerPeriod = flickerPeriod_;\n \tLOG(RPiAgc, Debug) << \"ev \" << status_.ev << \" fixedShutter \"\n@@ -703,7 +706,7 @@ void Agc::computeTargetExposure(double gain)\n \t\tDuration maxShutter = status_.fixedShutter\n \t\t\t\t\t      ? status_.fixedShutter\n \t\t\t\t\t      : exposureMode_->shutter.back();\n-\t\tmaxShutter = clipShutter(maxShutter);\n+\t\tmaxShutter = limitShutter(maxShutter);\n \t\tDuration maxTotalExposure =\n \t\t\tmaxShutter *\n \t\t\t(status_.fixedAnalogueGain != 0.0\n@@ -803,15 +806,16 @@ void Agc::divideUpExposure()\n \tdouble analogueGain;\n \tshutterTime = status_.fixedShutter ? status_.fixedShutter\n \t\t\t\t\t   : exposureMode_->shutter[0];\n-\tshutterTime = clipShutter(shutterTime);\n+\tshutterTime = limitShutter(shutterTime);\n \tanalogueGain = status_.fixedAnalogueGain != 0.0 ? status_.fixedAnalogueGain\n \t\t\t\t\t\t\t: exposureMode_->gain[0];\n+\tanalogueGain = limitGain(analogueGain);\n \tif (shutterTime * analogueGain < exposureValue) {\n \t\tfor (unsigned int stage = 1;\n \t\t     stage < exposureMode_->gain.size(); stage++) {\n \t\t\tif (!status_.fixedShutter) {\n \t\t\t\tDuration stageShutter =\n-\t\t\t\t\tclipShutter(exposureMode_->shutter[stage]);\n+\t\t\t\t\tlimitShutter(exposureMode_->shutter[stage]);\n \t\t\t\tif (stageShutter * analogueGain >= exposureValue) {\n \t\t\t\t\tshutterTime = exposureValue / analogueGain;\n \t\t\t\t\tbreak;\n@@ -824,6 +828,7 @@ void Agc::divideUpExposure()\n \t\t\t\t\tbreak;\n \t\t\t\t}\n \t\t\t\tanalogueGain = exposureMode_->gain[stage];\n+\t\t\t\tanalogueGain = limitGain(analogueGain);\n \t\t\t}\n \t\t}\n \t}\n@@ -846,6 +851,7 @@ void Agc::divideUpExposure()\n \t\t\t * gain as a side-effect.\n \t\t\t */\n \t\t\tanalogueGain = std::min(analogueGain, exposureMode_->gain.back());\n+\t\t\tanalogueGain = limitGain(analogueGain);\n \t\t\tshutterTime = newShutterTime;\n \t\t}\n \t\tLOG(RPiAgc, Debug) << \"After flicker avoidance, shutter \"\n@@ -872,13 +878,36 @@ void Agc::writeAndFinish(Metadata *imageMetadata, bool desaturate)\n \t\t\t   << \" analogue gain \" << filtered_.analogueGain;\n }\n \n-Duration Agc::clipShutter(Duration shutter)\n+Duration Agc::limitShutter(Duration shutter)\n {\n-\tif (maxShutter_)\n-\t\tshutter = std::min(shutter, maxShutter_);\n+\t/*\n+\t * shutter == 0 is a special case for fixed shutter values, and must pass\n+\t * through unchanged\n+\t */\n+\tif (!shutter)\n+\t\treturn shutter;\n+\n+\tshutter = std::clamp(shutter, mode_.minShutter, maxShutter_);\n \treturn shutter;\n }\n \n+double Agc::limitGain(double gain) const\n+{\n+\t/*\n+\t * Only limit the lower bounds of the gain value to what the sensor limits.\n+\t * The upper bound on analogue gain will be made up with additional digital\n+\t * gain applied by the ISP.\n+\t *\n+\t * gain == 0.0 is a special case for fixed shutter values, and must pass\n+\t * through unchanged\n+\t */\n+\tif (!gain)\n+\t\treturn gain;\n+\n+\tgain = std::max(gain, mode_.minAnalogueGain);\n+\treturn gain;\n+}\n+\n /* Register algorithm with the system. */\n static Algorithm *create(Controller *controller)\n {\ndiff --git a/src/ipa/raspberrypi/controller/rpi/agc.h b/src/ipa/raspberrypi/controller/rpi/agc.h\nindex f04896ca25ad..661d8b08cea2 100644\n--- a/src/ipa/raspberrypi/controller/rpi/agc.h\n+++ b/src/ipa/raspberrypi/controller/rpi/agc.h\n@@ -103,10 +103,12 @@ private:\n \tvoid filterExposure(bool desaturate);\n \tvoid divideUpExposure();\n \tvoid writeAndFinish(Metadata *imageMetadata, bool desaturate);\n-\tlibcamera::utils::Duration clipShutter(libcamera::utils::Duration shutter);\n+\tlibcamera::utils::Duration limitShutter(libcamera::utils::Duration shutter);\n+\tdouble limitGain(double gain) const;\n \tAgcMeteringMode *meteringMode_;\n \tAgcExposureMode *exposureMode_;\n \tAgcConstraintMode *constraintMode_;\n+\tCameraMode mode_;\n \tuint64_t frameCount_;\n \tAwbStatus awb_;\n \tstruct ExposureValues {\n","prefixes":["libcamera-devel","v2","3/4"]}