[{"id":38726,"web_url":"https://patchwork.libcamera.org/comment/38726/","msgid":"<85qznq2f0l.fsf@mzamazal-thinkpadp1gen7.tpbc.csb>","date":"2026-05-05T12:54:18","subject":"Re: [PATCH] ipa: simple: agc: Read exposure target and gain from\n\tYAML","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\nCould you explain the renaming?  (I don't say the new names are not\nbetter; I just don't know.)\n\n> Defaults match the previous values (2.5, 0.2, 0.04).\n>\n> Signed-off-by: d3vv3 <devve.3@gmail.com>\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 2E7B1BDCB5\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  5 May 2026 12:54:28 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E8FA56301E;\n\tTue,  5 May 2026 14:54:26 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AA13062DC4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  5 May 2026 14:54:25 +0200 (CEST)","from mail-wm1-f72.google.com (mail-wm1-f72.google.com\n\t[209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-313-Pt7Lx1jxMJC_kA0dnn8uZQ-1; Tue, 05 May 2026 08:54:22 -0400","by mail-wm1-f72.google.com with SMTP id\n\t5b1f17b1804b1-48d046fac74so14318795e9.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 05 May 2026 05:54:21 -0700 (PDT)","from mzamazal-thinkpadp1gen7.tpbc.csb ([213.175.37.14])\n\tby smtp.gmail.com with ESMTPSA id\n\t5b1f17b1804b1-48a8eb75fc1sm358860925e9.7.2026.05.05.05.54.18\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tTue, 05 May 2026 05:54:19 -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=\"e1fTlIl/\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1777985664;\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=YX+rdnL6Mc8fdzJit9yXx+hTMSReLCuAitX3D9kvU9w=;\n\tb=e1fTlIl/mt+LdtphUwPG8e+4tp+NSWpo48aeIkq2hvcYCBxaYvMlDeAK/mRKxo/Lc3/pBt\n\ta7KXX5BwJ0QMjUEOHuI3fTQHMPhRkMYJXDHMUgsRRPlOXwBhhglX884FDpjlVT84VXI7wd\n\ttUDDSIOlj14INNm3M/SNeFCKGuvSET8=","X-MC-Unique":"Pt7Lx1jxMJC_kA0dnn8uZQ-1","X-Mimecast-MFC-AGG-ID":"Pt7Lx1jxMJC_kA0dnn8uZQ_1777985661","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20251104; t=1777985660; x=1778590460;\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=YX+rdnL6Mc8fdzJit9yXx+hTMSReLCuAitX3D9kvU9w=;\n\tb=kQIs0lZdEqfIUsZQ9xZgfzmXrCJ1hDXF+gGY59+wSybB62iUlDZJkT9UGizEg/yDal\n\tQ5cYV0mZSTj6cb/bEehI3i6nImp00wwgCFTsDBkhk7an8tBU5d3an+zuNnd/dEDNVsY3\n\tWUJL1u8CAWk9IoxcxCm6stP8liZy01IGR8hAE0fpHt0cjx3FYskDdCIgfKWny3OTcbI3\n\tr1Ec7RfnQN3KnRGA4FKOX0w71OPssXASF+6NuPMfUjL/3kv5PEOfognsnfG08k96cpYm\n\tl7MsS9luzg/4GIR4TmJqk77nPQ+zRg2Sslvhout8ZTCR65TsTQ/ahODlmCSbeidna68G\n\tuJ7w==","X-Gm-Message-State":"AOJu0YyQCiLJgP7bH1Blo99PTPRgodFLixTGICD18ry0J/4rLf0+tG1B\n\t3pVsWgoq54/hhIFwWA4I3mTz72is7uQ/8y/lbgistPo0y3YcRnQbLXUKHHiwGfSjMoEnom8hauG\n\tHNKUL5h2aY5aqsVd7IBUm673AEFgbf8vw4fxfA93M4M2UQI7uWRyhhkqHN28RY3mKB32OH6q+49\n\t4dmIB2NctMiDT8HnTxBBRKhNQT9FfYkUedD7Zz5y5o4Z/KyqW1L6LUnSi0KVU=","X-Gm-Gg":"AeBDieuMR2wLADgnsCFDInRelSl0OPwulz6E2PpYkbMBVcwZSENrb6GOTdH0jCUlJln\n\tg845BtFuf5A2O+0nEahmfkLvkzGeUaIo90T6TtQC7CtaPmSL8hHpfUnR4uhKzisL4CCX6rGcwEX\n\t+1MrGuNSAInVdZYJW2J1h5JO/KNWwj6DQKoWp8ZBrx5Qyr+PzsHKCy68zP5f8Shevr+R4r+xzsg\n\t42t9jPwQkHzP8eAp+HDFYz+AXPOWwQR6tzTt+FFVz65BMcm06tj9VT46plrq19e/suKyOMwLYvv\n\tKkrd6z2Ed2XK8hCucb+zTKlV3cTeA3+2lkliyE9VTsMLxlkvvfO74XMj0g2xR/gikXQdO5J1MkV\n\tMXoM56bE/H0qbkQgM3uGtWKNjLU4EJ8ImzEOu/YIl0dmTUt73","X-Received":["by 2002:a05:600c:a401:b0:488:b241:2c5f with SMTP id\n\t5b1f17b1804b1-48d18ce9332mr38739115e9.26.1777985660570; \n\tTue, 05 May 2026 05:54:20 -0700 (PDT)","by 2002:a05:600c:a401:b0:488:b241:2c5f with SMTP id\n\t5b1f17b1804b1-48d18ce9332mr38738775e9.26.1777985660034; \n\tTue, 05 May 2026 05:54:20 -0700 (PDT)"],"From":"Milan Zamazal <mzamazal@redhat.com>","To":"d3vv3 <devve.3@gmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH] ipa: simple: agc: Read exposure target and gain from\n\tYAML","In-Reply-To":"<20260501191400.985920-4-devve.3@gmail.com> (d3vv3's message of\n\t\"Fri, 1 May 2026 21:13:12 +0200\")","References":"<20260501191400.985920-1-devve.3@gmail.com>\n\t<20260501191400.985920-4-devve.3@gmail.com>","Date":"Tue, 05 May 2026 14:54:18 +0200","Message-ID":"<85qznq2f0l.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":"VQjobYMmO64B7S2Hbp_Al-zyJsum9oDVs4x2SecwUTA_1777985661","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>"}}]