[{"id":29947,"web_url":"https://patchwork.libcamera.org/comment/29947/","msgid":"<171837036913.2248009.15480778219000807212@ping.linuxembedded.co.uk>","date":"2024-06-14T13:06:09","subject":"Re: [PATCH v6 2/2] ipa: rkisp1: agc: Plumb mode-selection and frame\n\tduration controls","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Paul Elder (2024-06-14 08:42:14)\n> Plumb controls for setting metering mode, exposure mode, constraint\n> mode, and frame duration limits. Also report them as available controls,\n> as well as in metadata.\n> \n> While at it, add the missing #include for tuple, as a std::tie is used.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> \n> ---\n> Changes in v6:\n> - remove the extra Algorithm::controls_\n> - remove the intermediate variable for accumulating the metering modes\n> - only update parameters when they are changed\n> - change the types of the control modes in the IPA cotext from ints to\n>   enums\n>   - add the appropriate casts when used\n> - initialize maxShutterSpeed at configure time to avoid the awkward\n>   extra check to ensure that it isn't zero\n>   - add a todo that FrameDurationLimits should be initialized in agc and\n>     not in the IPA\n> \n> No change in v5\n> \n> No change in v4\n> \n> Changes in v3:\n> - prevent maxShutterSpeed (for setLimits) from choosing 0 shutter speed\n>   if one of the inputs std::min() is not set (this fixes an assertion\n>   failure in the exposure mode helper)\n> \n> Changes in v2:\n> - add #include <tuple>\n> - don't overwrite metering/exposure/constraint mode controls to default\n>   if no control is supplied, and retain the old value instead (same for\n>   frame duration limits)\n> ---\n>  src/ipa/rkisp1/algorithms/agc.cpp | 76 ++++++++++++++++++++++++++++---\n>  src/ipa/rkisp1/ipa_context.h      | 12 ++++-\n>  2 files changed, 79 insertions(+), 9 deletions(-)\n> \n> diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp\n> index 5a9c6be118d9..8702145187c7 100644\n> --- a/src/ipa/rkisp1/algorithms/agc.cpp\n> +++ b/src/ipa/rkisp1/algorithms/agc.cpp\n> @@ -10,6 +10,8 @@\n>  #include <algorithm>\n>  #include <chrono>\n>  #include <cmath>\n> +#include <tuple>\n> +#include <vector>\n>  \n>  #include <libcamera/base/log.h>\n>  #include <libcamera/base/utils.h>\n> @@ -74,6 +76,13 @@ int Agc::parseMeteringModes(IPAContext &context, const YamlObject &tuningData)\n>                 meteringModes_[meteringModeId] = weights;\n>         }\n>  \n> +       std::vector<ControlValue> meteringModes;\n> +       std::vector<int> meteringModeKeys = utils::map_keys(meteringModes_);\n> +       std::transform(meteringModeKeys.begin(), meteringModeKeys.end(),\n> +                      std::back_inserter(meteringModes),\n> +                      [](int x) { return ControlValue(x); });\n> +       context.ctrlMap[&controls::AeMeteringMode] = ControlInfo(meteringModes);\n> +\n>         return 0;\n>  }\n>  \n> @@ -167,8 +176,19 @@ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo)\n>         context.activeState.agc.manual.exposure = context.activeState.agc.automatic.exposure;\n>         context.activeState.agc.autoEnabled = !context.configuration.raw;\n>  \n> -       context.activeState.agc.constraintMode = constraintModes().begin()->first;\n> -       context.activeState.agc.exposureMode = exposureModeHelpers().begin()->first;\n> +       context.activeState.agc.constraintMode =\n> +               static_cast<controls::AeConstraintModeEnum>(constraintModes().begin()->first);\n> +       context.activeState.agc.exposureMode =\n> +               static_cast<controls::AeExposureModeEnum>(exposureModeHelpers().begin()->first);\n> +       context.activeState.agc.meteringMode =\n> +               static_cast<controls::AeMeteringModeEnum>(meteringModes_.begin()->first);\n> +\n> +       /*\n> +        * \\todo This should probably come from FrameDurationLimits instead,\n> +        * except it's computed in the IPA and not here so we'd have to\n> +        * recompute it.\n> +        */\n> +       context.activeState.agc.maxShutterSpeed = context.configuration.sensor.maxShutterSpeed;\n>  \n>         /*\n>          * Define the measurement window for AGC as a centered rectangle\n> @@ -179,7 +199,6 @@ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo)\n>         context.configuration.agc.measureWindow.h_size = 3 * configInfo.outputSize.width / 4;\n>         context.configuration.agc.measureWindow.v_size = 3 * configInfo.outputSize.height / 4;\n>  \n> -       /* \\todo Run this again when FrameDurationLimits is passed in */\n>         setLimits(context.configuration.sensor.minShutterSpeed,\n>                   context.configuration.sensor.maxShutterSpeed,\n>                   context.configuration.sensor.minAnalogueGain,\n> @@ -233,6 +252,39 @@ void Agc::queueRequest(IPAContext &context,\n>                 frameContext.agc.exposure = agc.manual.exposure;\n>                 frameContext.agc.gain = agc.manual.gain;\n>         }\n> +\n> +       const auto &meteringMode = controls.get(controls::AeMeteringMode);\n> +       if (meteringMode) {\n> +               frameContext.agc.update = agc.meteringMode != *meteringMode;\n> +               agc.meteringMode =\n> +                       static_cast<controls::AeMeteringModeEnum>(*meteringMode);\n> +       }\n> +       frameContext.agc.meteringMode = agc.meteringMode;\n> +\n> +       const auto &exposureMode = controls.get(controls::AeExposureMode);\n> +       if (exposureMode) {\n> +               frameContext.agc.update = agc.exposureMode != *exposureMode;\n> +               agc.exposureMode =\n> +                       static_cast<controls::AeExposureModeEnum>(*exposureMode);\n> +       }\n> +       frameContext.agc.exposureMode = agc.exposureMode;\n> +\n> +       const auto &constraintMode = controls.get(controls::AeConstraintMode);\n> +       if (constraintMode) {\n> +               frameContext.agc.update = agc.constraintMode != *constraintMode;\n> +               agc.constraintMode =\n> +                       static_cast<controls::AeConstraintModeEnum>(*constraintMode);\n> +       }\n> +       frameContext.agc.constraintMode = agc.constraintMode;\n> +\n> +       const auto &frameDurationLimits = controls.get(controls::FrameDurationLimits);\n> +       if (frameDurationLimits) {\n> +               utils::Duration maxShutterSpeed =\n> +                       std::chrono::milliseconds((*frameDurationLimits).back());\n> +               frameContext.agc.update = agc.maxShutterSpeed != maxShutterSpeed;\n> +               agc.maxShutterSpeed = maxShutterSpeed;\n> +       }\n> +       frameContext.agc.maxShutterSpeed = agc.maxShutterSpeed;\n>  }\n>  \n>  /**\n> @@ -246,8 +298,7 @@ void Agc::prepare(IPAContext &context, const uint32_t frame,\n>                 frameContext.agc.gain = context.activeState.agc.automatic.gain;\n>         }\n>  \n> -       /* \\todo Remove this when we can set the below with controls */\n> -       if (frame > 0)\n> +       if (frame > 0 && !frameContext.agc.update)\n>                 return;\n>  \n>         /* Configure the measurement window. */\n> @@ -271,8 +322,7 @@ void Agc::prepare(IPAContext &context, const uint32_t frame,\n>                 params->meas.hst_config.hist_weight,\n>                 context.hw->numHistogramWeights\n>         };\n> -       /* \\todo Get this from control */\n> -       std::vector<uint8_t> &modeWeights = meteringModes_.at(controls::MeteringMatrix);\n> +       std::vector<uint8_t> &modeWeights = meteringModes_.at(frameContext.agc.meteringMode);\n>         std::copy(modeWeights.begin(), modeWeights.end(), weights.begin());\n>  \n>         struct rkisp1_cif_isp_window window = params->meas.hst_config.meas_window;\n> @@ -295,6 +345,7 @@ void Agc::fillMetadata(IPAContext &context, IPAFrameContext &frameContext,\n>                                      * frameContext.sensor.exposure;\n>         metadata.set(controls::AnalogueGain, frameContext.sensor.gain);\n>         metadata.set(controls::ExposureTime, exposureTime.get<std::micro>());\n> +       metadata.set(controls::AeEnable, frameContext.agc.autoEnabled);\n>  \n>         /* \\todo Use VBlank value calculated from each frame exposure. */\n>         uint32_t vTotal = context.configuration.sensor.size.height\n> @@ -302,6 +353,10 @@ void Agc::fillMetadata(IPAContext &context, IPAFrameContext &frameContext,\n>         utils::Duration frameDuration = context.configuration.sensor.lineDuration\n>                                       * vTotal;\n>         metadata.set(controls::FrameDuration, frameDuration.get<std::micro>());\n> +\n> +       metadata.set(controls::AeMeteringMode, frameContext.agc.meteringMode);\n> +       metadata.set(controls::AeExposureMode, frameContext.agc.exposureMode);\n> +       metadata.set(controls::AeConstraintMode, frameContext.agc.constraintMode);\n>  }\n>  \n>  /**\n> @@ -376,6 +431,13 @@ void Agc::process(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n>                        [](uint32_t x) { return x >> 4; });\n>         expMeans_ = { params->ae.exp_mean, context.hw->numAeCells };\n>  \n> +       utils::Duration maxShutterSpeed = std::min(context.configuration.sensor.maxShutterSpeed,\n> +                                                  frameContext.agc.maxShutterSpeed);\n> +       setLimits(context.configuration.sensor.minShutterSpeed,\n> +                 maxShutterSpeed,\n> +                 context.configuration.sensor.minAnalogueGain,\n> +                 context.configuration.sensor.maxAnalogueGain);\n> +\n>         /*\n>          * The Agc algorithm needs to know the effective exposure value that was\n>          * applied to the sensor when the statistics were collected.\n> diff --git a/src/ipa/rkisp1/ipa_context.h b/src/ipa/rkisp1/ipa_context.h\n> index 2a994d81ae41..6022480d4fd2 100644\n> --- a/src/ipa/rkisp1/ipa_context.h\n> +++ b/src/ipa/rkisp1/ipa_context.h\n> @@ -12,6 +12,7 @@\n>  \n>  #include <libcamera/base/utils.h>\n>  \n> +#include <libcamera/control_ids.h>\n>  #include <libcamera/controls.h>\n>  #include <libcamera/geometry.h>\n>  \n> @@ -68,8 +69,10 @@ struct IPAActiveState {\n>                 } automatic;\n>  \n>                 bool autoEnabled;\n> -               uint32_t constraintMode;\n> -               uint32_t exposureMode;\n> +               controls::AeConstraintModeEnum constraintMode;\n> +               controls::AeExposureModeEnum exposureMode;\n> +               controls::AeMeteringModeEnum meteringMode;\n> +               utils::Duration maxShutterSpeed;\n>         } agc;\n>  \n>         struct {\n> @@ -115,6 +118,11 @@ struct IPAFrameContext : public FrameContext {\n>                 uint32_t exposure;\n>                 double gain;\n>                 bool autoEnabled;\n> +               controls::AeConstraintModeEnum constraintMode;\n> +               controls::AeExposureModeEnum exposureMode;\n> +               controls::AeMeteringModeEnum meteringMode;\n> +               utils::Duration maxShutterSpeed;\n> +               bool update;\n>         } agc;\n>  \n>         struct {\n> -- \n> 2.39.2\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 0C5D4BD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 14 Jun 2024 13:06:15 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E64836548D;\n\tFri, 14 Jun 2024 15:06:13 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 00AB161A2A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 14 Jun 2024 15:06:11 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 112BB6FF8;\n\tFri, 14 Jun 2024 15:05:57 +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=\"FNLIffcj\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1718370357;\n\tbh=0N6IpiKRJtoDzFFEt4yhmPEk7n/BEcSPFS9KBDGHrKw=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=FNLIffcjVV3FoAO5wk51hvYl8Ub6myMkniQcHv8nq/XtR6AV2g7l47s2nr3XrtEep\n\tXqjLnDsCarQiXphinW+hohON3fZ3nFN+yPVZKfKBA1RdZKd3n/B21EpWfW+XBqplA0\n\t4JyEb1+2vXLVm229ETe3IITrxNwY9j/NGc1BJYE8=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20240614074214.3600996-3-paul.elder@ideasonboard.com>","References":"<20240614074214.3600996-1-paul.elder@ideasonboard.com>\n\t<20240614074214.3600996-3-paul.elder@ideasonboard.com>","Subject":"Re: [PATCH v6 2/2] ipa: rkisp1: agc: Plumb mode-selection and frame\n\tduration controls","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Paul Elder <paul.elder@ideasonboard.com>,\n\tStefan Klug <stefan.klug@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Fri, 14 Jun 2024 14:06:09 +0100","Message-ID":"<171837036913.2248009.15480778219000807212@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>"}}]