[{"id":18516,"web_url":"https://patchwork.libcamera.org/comment/18516/","msgid":"<38d654a6-8924-53e7-873b-47756e6d279e@ideasonboard.com>","date":"2021-08-03T13:44:17","subject":"Re: [libcamera-devel] [PATCH v4 2/2] ipa: raspberrypi: AGC: handle\n\tmodes with different sensitivities","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Hi David,\n\nOn 18/07/2021 19:51, David Plowman wrote:\n> When the sensor is switched to a mode with a different sensitivity,\n> the target exposure values need to be adjusted proportionately to\n> maintain the same image brightness.\n\nGreat, looks like my review comment is handled so:\n\n> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>\n> Reviewed-by: Naushir Patuck <naush@raspberrypi.com>\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nand I think these are ready for merge.\n\n--\nKieran\n\n\n> ---\n>  src/ipa/raspberrypi/controller/rpi/agc.cpp | 27 ++++++++++++++++++----\n>  src/ipa/raspberrypi/controller/rpi/agc.hpp |  1 +\n>  2 files changed, 23 insertions(+), 5 deletions(-)\n> \n> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp\n> index f57783f8..1cfdd3bb 100644\n> --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp\n> +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp\n> @@ -163,7 +163,7 @@ Agc::Agc(Controller *controller)\n>  \t: AgcAlgorithm(controller), metering_mode_(nullptr),\n>  \t  exposure_mode_(nullptr), constraint_mode_(nullptr),\n>  \t  frame_count_(0), lock_count_(0),\n> -\t  last_target_exposure_(0s),\n> +\t  last_target_exposure_(0s), last_sensitivity_(0.0),\n>  \t  ev_(1.0), flicker_period_(0s),\n>  \t  max_shutter_(0s), fixed_shutter_(0s), fixed_analogue_gain_(0.0)\n>  {\n> @@ -268,9 +268,12 @@ void Agc::SetConstraintMode(std::string const &constraint_mode_name)\n>  \tconstraint_mode_name_ = constraint_mode_name;\n>  }\n>  \n> -void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,\n> +void Agc::SwitchMode(CameraMode const &camera_mode,\n>  \t\t     Metadata *metadata)\n>  {\n> +\t/* AGC expects the mode sensitivity always to be non-zero. */\n> +\tASSERT(camera_mode.sensitivity);\n> +\n>  \thousekeepConfig();\n>  \n>  \tDuration fixed_shutter = clipShutter(fixed_shutter_);\n> @@ -292,9 +295,20 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,\n>  \t\tfiltered_.shutter = fixed_shutter;\n>  \t\tfiltered_.analogue_gain = fixed_analogue_gain_;\n>  \t} else if (status_.total_exposure_value) {\n> -\t\t// On a mode switch, it's possible the exposure profile could change,\n> -\t\t// or a fixed exposure/gain might be set so we divide up the exposure/\n> -\t\t// gain again, but we don't change any target values.\n> +\t\t// On a mode switch, various things could happen:\n> +\t\t// - the exposure profile might change\n> +\t\t// - a fixed exposure or gain might be set\n> +\t\t// - the new mode's sensitivity might be different\n> +\t\t// We cope with the last of these by scaling the target values. After\n> +\t\t// that we just need to re-divide the exposure/gain according to the\n> +\t\t// current exposure profile, which takes care of everything else.\n> +\n> +\t\tdouble ratio = last_sensitivity_ / camera_mode.sensitivity;\n> +\t\ttarget_.total_exposure_no_dg *= ratio;\n> +\t\ttarget_.total_exposure *= ratio;\n> +\t\tfiltered_.total_exposure_no_dg *= ratio;\n> +\t\tfiltered_.total_exposure *= ratio;\n> +\n>  \t\tdivideUpExposure();\n>  \t} else {\n>  \t\t// We come through here on startup, when at least one of the shutter\n> @@ -308,6 +322,9 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode,\n>  \t}\n>  \n>  \twriteAndFinish(metadata, false);\n> +\n> +\t// We must remember the sensitivity of this mode for the next SwitchMode.\n> +\tlast_sensitivity_ = camera_mode.sensitivity;\n>  }\n>  \n>  void Agc::Prepare(Metadata *image_metadata)\n> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.hpp b/src/ipa/raspberrypi/controller/rpi/agc.hpp\n> index 85067dc6..82063636 100644\n> --- a/src/ipa/raspberrypi/controller/rpi/agc.hpp\n> +++ b/src/ipa/raspberrypi/controller/rpi/agc.hpp\n> @@ -126,6 +126,7 @@ private:\n>  \tint lock_count_;\n>  \tDeviceStatus last_device_status_;\n>  \tlibcamera::utils::Duration last_target_exposure_;\n> +\tdouble last_sensitivity_; // sensitivity of the previous camera mode\n>  \t// Below here the \"settings\" that applications can change.\n>  \tstd::string metering_mode_name_;\n>  \tstd::string exposure_mode_name_;\n>","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 8D6C8C3235\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  3 Aug 2021 13:44:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 02FAB687CC;\n\tTue,  3 Aug 2021 15:44:23 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 30EF76026A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  3 Aug 2021 15:44:21 +0200 (CEST)","from [192.168.0.20]\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 92BAD3F0;\n\tTue,  3 Aug 2021 15:44:20 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"cg5kye3M\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1627998260;\n\tbh=VOnvoln7rKJUx8xuASTmb+fgvTw6LDo5dXmqf1SAFog=;\n\th=Subject:To:References:From:Date:In-Reply-To:From;\n\tb=cg5kye3M4o9xsb1LEA+kgvNjZK0KpdZZx28DBoWe+jf4xhltCv1eAZC8bGwn1FQ/s\n\tYgotU6ZWU8rAzI9jCkmcEMDUb6Z5KB9NKaheuFwAxRIxFAlH70Buhf9zRWJNHxUTX6\n\tqDfbDga/oe3Rpqma/g/z1kAaKh3beZYkDvboZpwk=","To":"David Plowman <david.plowman@raspberrypi.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20210718185125.17472-1-david.plowman@raspberrypi.com>\n\t<20210718185125.17472-3-david.plowman@raspberrypi.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<38d654a6-8924-53e7-873b-47756e6d279e@ideasonboard.com>","Date":"Tue, 3 Aug 2021 14:44:17 +0100","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101\n\tThunderbird/78.11.0","MIME-Version":"1.0","In-Reply-To":"<20210718185125.17472-3-david.plowman@raspberrypi.com>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-GB","Content-Transfer-Encoding":"7bit","Subject":"Re: [libcamera-devel] [PATCH v4 2/2] ipa: raspberrypi: AGC: handle\n\tmodes with different sensitivities","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]