[{"id":38855,"web_url":"https://patchwork.libcamera.org/comment/38855/","msgid":"<85h5oetdvs.fsf@mzamazal-thinkpadp1gen7.tpbc.csb>","date":"2026-05-11T10:54:47","subject":"Re: [PATCH v2 08/10] ipa: simple: agc: Read exposure target and\n\tgain from YAML","submitter":{"id":177,"url":"https://patchwork.libcamera.org/api/people/177/","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"content":"d3vv3 <devve.3@gmail.com> writes:\n\n> Replace the hardcoded kExposureOptimal, kExposureSatisfactory, and\n> kExpProportionalGain constants with member variables read from the\n> tuning file as exposureTarget, hysteresis, and proportionalGain.\n> Defaults match the previous values (2.5, 0.2, 0.04).\n>\n> The constants are renamed to match their YAML keys and to use standard\n> control-theory terminology:\n>\n> - kExposureOptimal -> exposureTarget: \"optimal\" implies a single\n>   universally correct value; \"target\" is the conventional ISP/AGC term\n>   for the setpoint the controller drives towards and is sensor-dependent.\n>\n> - kExposureSatisfactory -> hysteresis: the old name described the effect\n>   (exposure is satisfactory within this band) rather than the mechanism.\n>   \"hysteresis\" is the standard term for a deadband that prevents\n>   oscillation around a setpoint.\n>\n> - kExpProportionalGain -> proportionalGain: drops the redundant kExp\n>   prefix and matches the YAML key name directly.\n>\n> Signed-off-by: d3vv3 <devve.3@gmail.com>\n\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\n\n> ---\n>  src/ipa/simple/algorithms/agc.cpp | 33 ++++++++++++++++++++-----------\n>  src/ipa/simple/algorithms/agc.h   |  8 ++++++++\n>  2 files changed, 30 insertions(+), 11 deletions(-)\n>\n> diff --git a/src/ipa/simple/algorithms/agc.cpp b/src/ipa/simple/algorithms/agc.cpp\n> index ac977d5f..66618d0f 100644\n> --- a/src/ipa/simple/algorithms/agc.cpp\n> +++ b/src/ipa/simple/algorithms/agc.cpp\n> @@ -28,45 +28,56 @@ static constexpr unsigned int kExposureBinsCount = 5;\n>  \n>  /*\n>   * The exposure is optimal when the mean sample value of the histogram is\n> - * in the middle of the range.\n> + * in the middle of the range. Overridable via YAML exposureTarget.\n>   */\n> -static constexpr float kExposureOptimal = kExposureBinsCount / 2.0;\n> +static constexpr float kExposureTargetDefault = kExposureBinsCount / 2.0;\n>  \n>  /*\n>   * This implements the hysteresis for the exposure adjustment.\n>   * It is small enough to have the exposure close to the optimal, and is big\n>   * enough to prevent the exposure from wobbling around the optimal value.\n>   */\n> -static constexpr float kExposureSatisfactory = 0.2;\n> +static constexpr float kHysteresisDefault = 0.2;\n>  \n>  /*\n>   * Proportional gain for exposure/gain adjustment. Maps the MSV error to a\n>   * multiplicative correction factor:\n>   *\n> - *   factor = 1.0 + kExpProportionalGain * error\n> + *   factor = 1.0 + proportionalGain_ * error\n>   *\n> - * With kExpProportionalGain = 0.04:\n> + * With proportionalGain_ = 0.04:\n>   *   - max error ~2.5 -> factor 1.10 (~10% step, same as before)\n>   *   - error 1.0      -> factor 1.04 (~4% step)\n>   *   - error 0.3      -> factor 1.012 (~1.2% step)\n>   *\n> - * This replaces the fixed 10% bang-bang step with a proportional correction\n> - * that converges smoothly and avoids overshooting near the target.\n> + * Overridable via YAML proportionalGain.\n>   */\n> -static constexpr float kExpProportionalGain = 0.04;\n> +static constexpr float kProportionalGainDefault = 0.04;\n>  \n>  Agc::Agc()\n>  {\n>  }\n>  \n> +int Agc::init([[maybe_unused]] IPAContext &context, const ValueNode &tuningData)\n> +{\n> +\texposureTarget_ = tuningData[\"exposureTarget\"].get<float>()\n> +\t\t.value_or(kExposureTargetDefault);\n> +\thysteresis_ = tuningData[\"hysteresis\"].get<float>()\n> +\t\t.value_or(kHysteresisDefault);\n> +\tproportionalGain_ = tuningData[\"proportionalGain\"].get<float>()\n> +\t\t.value_or(kProportionalGainDefault);\n> +\n> +\treturn 0;\n> +}\n> +\n>  void Agc::updateExposure(IPAContext &context, IPAFrameContext &frameContext, double exposureMSV)\n>  {\n>  \tint32_t &exposure = frameContext.sensor.exposure;\n>  \tdouble &again = frameContext.sensor.gain;\n>  \n> -\tdouble error = kExposureOptimal - exposureMSV;\n> +\tdouble error = exposureTarget_ - exposureMSV;\n>  \n> -\tif (std::abs(error) <= kExposureSatisfactory)\n> +\tif (std::abs(error) <= hysteresis_)\n>  \t\treturn;\n>  \n>  \t/*\n> @@ -74,7 +85,7 @@ void Agc::updateExposure(IPAContext &context, IPAFrameContext &frameContext, dou\n>  \t * determines the direction: positive error means too dark (increase),\n>  \t * negative means too bright (decrease).\n>  \t */\n> -\tfloat factor = 1.0f + static_cast<float>(error) * kExpProportionalGain;\n> +\tfloat factor = 1.0f + static_cast<float>(error) * proportionalGain_;\n>  \n>  \tif (factor > 1.0f) {\n>  \t\t/* Scene too dark: increase exposure first, then gain. */\n> diff --git a/src/ipa/simple/algorithms/agc.h b/src/ipa/simple/algorithms/agc.h\n> index 112d9f5a..b8ed542b 100644\n> --- a/src/ipa/simple/algorithms/agc.h\n> +++ b/src/ipa/simple/algorithms/agc.h\n> @@ -7,6 +7,8 @@\n>  \n>  #pragma once\n>  \n> +#include <libcamera/internal/yaml_parser.h>\n> +\n>  #include \"algorithm.h\"\n>  \n>  namespace libcamera {\n> @@ -19,6 +21,8 @@ public:\n>  \tAgc();\n>  \t~Agc() = default;\n>  \n> +\tint init(IPAContext &context, const ValueNode &tuningData) override;\n> +\n>  \tvoid process(IPAContext &context, const uint32_t frame,\n>  \t\t     IPAFrameContext &frameContext,\n>  \t\t     const SwIspStats *stats,\n> @@ -26,6 +30,10 @@ public:\n>  \n>  private:\n>  \tvoid updateExposure(IPAContext &context, IPAFrameContext &frameContext, double exposureMSV);\n> +\n> +\tfloat exposureTarget_;\n> +\tfloat hysteresis_;\n> +\tfloat proportionalGain_;\n>  };\n>  \n>  } /* namespace ipa::soft::algorithms */","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 CC253BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 11 May 2026 10:54:55 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 13C9863025;\n\tMon, 11 May 2026 12:54:55 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C586D62DC4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 11 May 2026 12:54:53 +0200 (CEST)","from mail-ej1-f71.google.com (mail-ej1-f71.google.com\n\t[209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-54-cEIri4ToPqujWPqVjaoD6g-1; Mon, 11 May 2026 06:54:51 -0400","by mail-ej1-f71.google.com with SMTP id\n\ta640c23a62f3a-bcd4170426eso178454166b.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 11 May 2026 03:54:51 -0700 (PDT)","from mzamazal-thinkpadp1gen7.tpbc.csb\n\t(ip-77-48-47-4.net.vodafone.cz. [77.48.47.4])\n\tby smtp.gmail.com with ESMTPSA id\n\ta640c23a62f3a-bd026b13c3esm118681466b.27.2026.05.11.03.54.48\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 11 May 2026 03:54:48 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"ZfyVajUI\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1778496892;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=umIJkfalaUICwQN3F5OCIg6wbJiOSP9TpULaIAsD3NQ=;\n\tb=ZfyVajUIORZkrRNpVETg2uFvbwLSFJDibAESPULybFriPXjn5TTNP3evA2qsymSFwWnm/r\n\tOmh3/IecRhMCO1XyY6ymyvmAxadlcOO4jpHP4eVUVv7JdShKU9S0ukX6Sb9AZwcNzRCPLz\n\t1tnfMiQtmpMrB10WZ27jvLE3UHc4kWg=","X-MC-Unique":"cEIri4ToPqujWPqVjaoD6g-1","X-Mimecast-MFC-AGG-ID":"cEIri4ToPqujWPqVjaoD6g_1778496890","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20251104; t=1778496890; x=1779101690;\n\th=mime-version:user-agent:message-id:date:references:in-reply-to\n\t:subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject\n\t:date:message-id:reply-to;\n\tbh=umIJkfalaUICwQN3F5OCIg6wbJiOSP9TpULaIAsD3NQ=;\n\tb=Hp+Bz8O/atq2QSNGvD0jpFGS/LAJusgu7Cvi2JeBJWnt5SfHXa3L8GaiHHJrdp5B0x\n\ti1aeoQ1NjNhR4MSdw1skRiTMRSmAalDIw6A2VZOikEfb4hXpspu8ZooaPepFNR2rvExd\n\tZw07N8vrG8Ru6jqgN7pJkXwxgjLCmEmA9iAJSECae11zAPUk2b7n3ce8dn3NtKQ95QYA\n\tO+JdrS5va3YAT53KeSwpBczrrTO2D7CkMOpVJTQ2DDDPdTd1F8HFaHZlQy63QSdDmgF8\n\twQfY+1ZgQaoTOF5LwCo5XVdnhoIvufqBsUF+UhOUwe4rr2Hgcy4r0wjAA1XgW2Cu8USq\n\tyh8A==","X-Gm-Message-State":"AOJu0Yx0xVSAsIcVB0G/QLntUUoeWad13t1FKHIy4Lvpw69qealyYbIf\n\tGhU63sT+h9vyBXBBhKxT09YCGF9rorPHFZjAoZIkkEyAMlTGFV0ea70TvgDkDqSZHx45A/rPixw\n\t90law1WmHSJDfV7MBs8npATDqOTLeAkq3zV++7BJDuOjgq+PpAkRpo62L3nX3KrhSmg9LoQ818Q\n\tTrsLyP/I2Ao7iiJrnmpKHD7pIzCEtcTrQbKN3mhnWsr5Oa5Gx9MY67JL7dS7o=","X-Gm-Gg":"Acq92OGqwJEptzEl80WsksBRqDPZD9FLMFZqcWln3XHm1QKPM9TtxL7AdkqxnLqz3Ag\n\trtPELb3Xfe/tPLuUuGrySXzDGyL2kqFoQNWjxVE7eTWMqGcr+ljLN/Z94FWTdMVFIRbx9LdtcWp\n\tcsKqz5Gde+95aKzB32T1rSs9MQFH7DSYM6+YlF+w1tiy2ArEoSDJv7gp4f50A6iLUE2aiNiZVcV\n\tdp5vluYMgTVNQr6ChR/ZuNA27wj2MobmUFJ+Ih4t/nAt+t1Oh/Zh9dpAFWkbRZWncFXnm/fcMLQ\n\td4jrvLBdkppwSi2C5XNTpLKvylU4XBNTogrhRxn/EA6IgEHFCQwApmexPACzE3dyQ1WW98D8hbW\n\tjLCbASZXv6Lljycps0+U5rQR58/D65fsA81dtlDs+TCbe1IT4WJC/AdNtk8dQGGzZ98/6E7Jc/7\n\tA=","X-Received":["by 2002:a17:907:97c8:b0:bbf:bab5:ecd9 with SMTP id\n\ta640c23a62f3a-bc85caaaaa6mr828119566b.15.1778496889660; \n\tMon, 11 May 2026 03:54:49 -0700 (PDT)","by 2002:a17:907:97c8:b0:bbf:bab5:ecd9 with SMTP id\n\ta640c23a62f3a-bc85caaaaa6mr828115966b.15.1778496888925; \n\tMon, 11 May 2026 03:54:48 -0700 (PDT)"],"From":"Milan Zamazal <mzamazal@redhat.com>","To":"d3vv3 <devve.3@gmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v2 08/10] ipa: simple: agc: Read exposure target and\n\tgain from YAML","In-Reply-To":"<20260506230722.1041596-9-devve.3@gmail.com> (d3vv3's message of\n\t\"Thu, 7 May 2026 01:07:20 +0200\")","References":"<20260506230722.1041596-1-devve.3@gmail.com>\n\t<20260506230722.1041596-9-devve.3@gmail.com>","Date":"Mon, 11 May 2026 12:54:47 +0200","Message-ID":"<85h5oetdvs.fsf@mzamazal-thinkpadp1gen7.tpbc.csb>","User-Agent":"Gnus/5.13 (Gnus v5.13)","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"XBEqmdE-ZjKfW9-CeXUtFafnZec4BrbVuZKxGkHKbpg_1778496890","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain","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>"}}]