Patch Detail
Show a patch.
GET /api/1.1/patches/18480/?format=api
{ "id": 18480, "url": "https://patchwork.libcamera.org/api/1.1/patches/18480/?format=api", "web_url": "https://patchwork.libcamera.org/patch/18480/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20230327140625.6624-1-naush@raspberrypi.com>", "date": "2023-03-27T14:06:25", "name": "[libcamera-devel] ipa: raspberrypi: Ensure shutter speed and gain are clipped in the AGC", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "9f32909db45bbbe8d24ee9e0024c535fedf1fcfb", "submitter": { "id": 34, "url": "https://patchwork.libcamera.org/api/1.1/people/34/?format=api", "name": "Naushir Patuck", "email": "naush@raspberrypi.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/18480/mbox/", "series": [ { "id": 3820, "url": "https://patchwork.libcamera.org/api/1.1/series/3820/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=3820", "date": "2023-03-27T14:06:25", "name": "[libcamera-devel] ipa: raspberrypi: Ensure shutter speed and gain are clipped in the AGC", "version": 1, "mbox": "https://patchwork.libcamera.org/series/3820/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/18480/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/18480/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 F3217C0F2A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 27 Mar 2023 14:06:40 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1EAF66271C;\n\tMon, 27 Mar 2023 16:06:40 +0200 (CEST)", "from mail-il1-x12f.google.com (mail-il1-x12f.google.com\n\t[IPv6:2607:f8b0:4864:20::12f])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 539F4626D7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 27 Mar 2023 16:06:38 +0200 (CEST)", "by mail-il1-x12f.google.com with SMTP id h14so3313852ilj.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 27 Mar 2023 07:06:38 -0700 (PDT)", "from localhost.localdomain ([93.93.133.154])\n\tby smtp.gmail.com with ESMTPSA id\n\ta12-20020a056e020e0c00b00325f7bfc651sm1483297ilk.59.2023.03.27.07.06.35\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 27 Mar 2023 07:06:36 -0700 (PDT)" ], "DKIM-Signature": [ "v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1679926000;\n\tbh=GKuSCN9Yy/eVZDMQ1xwKyTmP6GB4FWUbJg8lH/iBHGI=;\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:Cc:\n\tFrom;\n\tb=dFmeVfL/g9KtbkPuaWcvIUZ0puoKDgG3jDDIg/n2uQ/X5zDflZ31pb5C/kfybhzMy\n\tlscl5GUpoXqMQibb9VS38jHgFiIvQfqVJLifgXkCcF5xXWVIWFUixDPaQrjs2AGNom\n\tirRnG/+R3U9FiRMKPvbN/pklwaeUqd9E2Pk8M2kKVxmRWoPBSHZN/pDM6WAa1TUtDv\n\tT8deDmAS8HAFjiuxtXGQ/vWrWnUfWslXZfHB8OME+x+Amew6ElP/yBowe3gUmI2esP\n\tC4X2mcvprya1igjW6mw1RXOimusdgLi9RcFIlBfvv7bmc1Cxxjr8x4MMCHauaHJWU4\n\t4/mX38vJ1vMVg==", "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1679925997;\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=/4bT4+XhpmutDlxLJohIF3zTi9O9kJXYZaoNU/eOy8Q=;\n\tb=L+wNnZwg5LfMsby8+7PtEuc1wu8SQSGeLl9thf6L+L3BjWBKK1wBBGatbJ/gxPkyoY\n\t4LZq9if4H6O8WAUspmuEctSo4wQ9yiOmdV7qt2v/nBGp5ymikjjgBxoIb7bUKuYQ4fjt\n\tJ83KD3liTRGq4ihJrQA1kQAH92z4fxUpro7mUCGZicJuU7PMizUOsgKPAHRXGPPEa3de\n\tIDToN3PkKt8zPd7s9zVkJ1jJMLaOTIJ573PTFgNqpShCZvXidacn/Q3n3cCwg2roQr6A\n\tSRHZyKhwtNwwonDA+XepPLIGcNtok5G/8QU6MhDamlvJrnz+Esk/oEOpMunOe7GgSLCE\n\tM8rg==" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"L+wNnZwg\"; dkim-atps=neutral", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112; t=1679925997;\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=/4bT4+XhpmutDlxLJohIF3zTi9O9kJXYZaoNU/eOy8Q=;\n\tb=RUvz2aVk1gHVPjC5DN5lej6J7Mc9MOOsfYupmvDq+S+HzaqI6F7Xo8JkHlJcNI1RJF\n\txOS++mjcoigdj5bodQAuVIVvm9NeKrpExPdcH7/3Ag4jIFmkH7WDQKBXY5EsbZXC5dR7\n\t7bDXgI9VNnKZh1UBmUvaNYVm7qw0dMNJ3lasDgClfTIb6Tja0RNab0rhyFsWukpK4no4\n\tl76tFZlyRxf3bElsnUNOXd7BZEqNsgUgFl2FWaOlv3V7KbGPHxCZPxN/qz2xBFx9q7vX\n\taH0BTAtGmBlC7db37VPxfVBIY2kv9c6AWlCpyqXnVMYeT8aGAqt/obpQ3xayGEx7dMDl\n\tX4Jw==", "X-Gm-Message-State": "AAQBX9e0w0GYS9Jw3jLhtr1th6HCLPXeGT1dAXT2h6itLuus5VPia+7m\n\twz5lCaz4FI3NXdVquPzxPDkdlLTL315zARgMxR85MA==", "X-Google-Smtp-Source": "AKy350a8n+f46HQQTEoUsQmvd8qxsswwxuqNNk/qjBK/Cagb/GLiqyxZhgJchEJCPKFjqfuoxQSOBQ==", "X-Received": "by 2002:a92:d812:0:b0:325:cfe4:8c3 with SMTP id\n\ty18-20020a92d812000000b00325cfe408c3mr9117397ilm.30.1679925996748; \n\tMon, 27 Mar 2023 07:06:36 -0700 (PDT)", "To": "libcamera-devel@lists.libcamera.org", "Date": "Mon, 27 Mar 2023 15:06:25 +0100", "Message-Id": "<20230327140625.6624-1-naush@raspberrypi.com>", "X-Mailer": "git-send-email 2.34.1", "In-Reply-To": "<20230327093439.24670-4-naush@raspberrypi.com>", "References": "<20230327093439.24670-4-naush@raspberrypi.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH] ipa: raspberrypi: Ensure shutter speed\n\tand 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>", "Cc": "Jacopo Mondi <jacopo.mondi@ideasonboard.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>\nReviewed-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n src/ipa/raspberrypi/controller/rpi/agc.cpp | 58 ++++++++++++++++------\n src/ipa/raspberrypi/controller/rpi/agc.h | 5 +-\n 2 files changed, 47 insertions(+), 16 deletions(-)", "diff": "diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp\nindex 4ea0dd41e66c..1776262edeb5 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,14 @@ void Agc::switchMode(CameraMode const &cameraMode,\n \n \thousekeepConfig();\n \n-\tDuration fixedShutter = clipShutter(fixedShutter_);\n+\t/*\n+\t * Store the mode in the local state. We must cache the sensitivity of\n+\t * of the previous mode for the calculations below.\n+\t */\n+\tdouble lastSensitivity = mode_.sensitivity;\n+\tmode_ = cameraMode;\n+\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 +378,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 = lastSensitivity / cameraMode.sensitivity;\n \t\ttarget_.totalExposureNoDG *= ratio;\n \t\ttarget_.totalExposure *= ratio;\n \t\tfiltered_.totalExposureNoDG *= ratio;\n@@ -392,9 +399,6 @@ void Agc::switchMode(CameraMode const &cameraMode,\n \t}\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 }\n \n void Agc::prepare(Metadata *imageMetadata)\n@@ -516,7 +520,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 +707,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 +807,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 +829,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 +852,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 +879,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..4fa9b8de5fb2 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@@ -124,7 +126,6 @@ private:\n \tint lockCount_;\n \tDeviceStatus lastDeviceStatus_;\n \tlibcamera::utils::Duration lastTargetExposure_;\n-\tdouble lastSensitivity_; /* sensitivity of the previous camera mode */\n \t/* Below here the \"settings\" that applications can change. */\n \tstd::string meteringModeName_;\n \tstd::string exposureModeName_;\n", "prefixes": [ "libcamera-devel" ] }