Show a patch.

GET /api/1.1/patches/19899/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 19899,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/19899/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/19899/",
    "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": "<20240417131536.484129-8-dan.scally@ideasonboard.com>",
    "date": "2024-04-17T13:15:35",
    "name": "[v2,7/8] ipa: rkisp1: Derive rkisp1::algorithms::Agc from MeanLuminanceAgc",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "2aba94cf68b4120380eeea151160ae3a1a64f79c",
    "submitter": {
        "id": 156,
        "url": "https://patchwork.libcamera.org/api/1.1/people/156/?format=api",
        "name": "Dan Scally",
        "email": "dan.scally@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/19899/mbox/",
    "series": [
        {
            "id": 4260,
            "url": "https://patchwork.libcamera.org/api/1.1/series/4260/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4260",
            "date": "2024-04-17T13:15:28",
            "name": "Centralise Agc into libipa",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/4260/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/19899/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/19899/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 63136C32CA\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 17 Apr 2024 13:16:09 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B5B5563368;\n\tWed, 17 Apr 2024 15:16:08 +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 5FB3F63365\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 17 Apr 2024 15:15:57 +0200 (CEST)",
            "from mail.ideasonboard.com\n\t(cpc141996-chfd3-2-0-cust928.12-3.cable.virginm.net [86.13.91.161])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4E8841815;\n\tWed, 17 Apr 2024 15:15:10 +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=\"GmEfe7Oq\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1713359710;\n\tbh=sgYmjPNwvsNEFKyq4BgXci7sH83fHuf1IlXouiX4HA8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=GmEfe7OqT9eqR84OH1zgH+Oc/rS+onWMIlNxUF70SSHa3mL5ED3ueLIUg6q5CYJRG\n\tc+MYSh4HLoGc0ecyoJYW2hcID7yGQ+QrHo5WnTMyfiR08CR5EW4+mWnrOhPAl+25d5\n\tLmtn3zDo6EZo3cqxlfnetcTv0ktniDWEvjpdfjOw=",
        "From": "Daniel Scally <dan.scally@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Daniel Scally <dan.scally@ideasonboard.com>",
        "Subject": "[PATCH v2 7/8] ipa: rkisp1: Derive rkisp1::algorithms::Agc from\n\tMeanLuminanceAgc",
        "Date": "Wed, 17 Apr 2024 14:15:35 +0100",
        "Message-Id": "<20240417131536.484129-8-dan.scally@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.34.1",
        "In-Reply-To": "<20240417131536.484129-1-dan.scally@ideasonboard.com>",
        "References": "<20240417131536.484129-1-dan.scally@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "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>"
    },
    "content": "Now that we have a MeanLuminanceAgc class that centralises our AEGC\nalgorithm, derive the RkISP1's Agc class from it and plumb in the\nnecessary framework to enable it to be used. For simplicities sake\nthis commit switches the algorithm to use the derived class, but\ndoes not remove the bespoke functions at this time.\n\nSigned-off-by: Daniel Scally <dan.scally@ideasonboard.com>\n---\nChanges in v2:\n\n\t- Squashed the patch adding parseStatistics()\n\t- Used the first available constraint and exposure modes rather than\n\t  assuming the \"Normal\" mode was available\n\t- Stopped storing a Histogram in the class members when it's only needed\n\t  during ::process()\n\t- Remembered to report the controls out to IPARkISP1::updateControls()\n\n src/ipa/rkisp1/algorithms/agc.cpp | 82 +++++++++++++++++++++++++++++--\n src/ipa/rkisp1/algorithms/agc.h   |  9 +++-\n src/ipa/rkisp1/ipa_context.h      |  5 ++\n src/ipa/rkisp1/rkisp1.cpp         |  3 +-\n 4 files changed, 92 insertions(+), 7 deletions(-)",
    "diff": "diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp\nindex 47a6f7b2..0d66fcca 100644\n--- a/src/ipa/rkisp1/algorithms/agc.cpp\n+++ b/src/ipa/rkisp1/algorithms/agc.cpp\n@@ -64,6 +64,30 @@ Agc::Agc()\n \tsupportsRaw_ = true;\n }\n \n+/**\n+ * \\brief Initialise the AGC algorithm from tuning files\n+ *\n+ * \\param[in] context The shared IPA context\n+ * \\param[in] tuningData The YamlObject containing Agc tuning data\n+ *\n+ * This function calls the base class' tuningData parsers to discover which\n+ * control values are supported.\n+ *\n+ * \\return 0 on success or errors from the base class\n+ */\n+int Agc::init(IPAContext &context, const YamlObject &tuningData)\n+{\n+\tint ret;\n+\n+\tret = parseTuningData(tuningData);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tcontext.ctrlMap.merge(controls());\n+\n+\treturn 0;\n+}\n+\n /**\n  * \\brief Configure the AGC given a configInfo\n  * \\param[in] context The shared IPA context\n@@ -81,6 +105,9 @@ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo)\n \tcontext.activeState.agc.manual.exposure = context.activeState.agc.automatic.exposure;\n \tcontext.activeState.agc.autoEnabled = !context.configuration.raw;\n \n+\tcontext.activeState.agc.constraintMode = constraintModes().begin()->first;\n+\tcontext.activeState.agc.exposureMode = exposureModeHelpers().begin()->first;\n+\n \t/*\n \t * Define the measurement window for AGC as a centered rectangle\n \t * covering 3/4 of the image width and height.\n@@ -95,6 +122,15 @@ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo)\n \t * frame index\n \t */\n \tframeCount_ = 0;\n+\n+\t/* \\todo Run this again when FrameDurationLimits is passed in */\n+\tconfigureExposureModeHelpers(context.configuration.sensor.minShutterSpeed,\n+\t\t\t\t     context.configuration.sensor.maxShutterSpeed,\n+\t\t\t\t     context.configuration.sensor.minAnalogueGain,\n+\t\t\t\t     context.configuration.sensor.maxAnalogueGain);\n+\n+\tresetFrameCount();\n+\n \treturn 0;\n }\n \n@@ -234,7 +270,6 @@ void Agc::computeExposure(IPAContext &context, IPAFrameContext &frameContext,\n \t\t\t  double yGain, double iqMeanGain)\n {\n \tIPASessionConfiguration &configuration = context.configuration;\n-\tIPAActiveState &activeState = context.activeState;\n \n \t/* Get the effective exposure and gain applied on the sensor. */\n \tuint32_t exposure = frameContext.sensor.exposure;\n@@ -300,10 +335,6 @@ void Agc::computeExposure(IPAContext &context, IPAFrameContext &frameContext,\n \tLOG(RkISP1Agc, Debug) << \"Divided up shutter and gain are \"\n \t\t\t      << shutterTime << \" and \"\n \t\t\t      << stepGain;\n-\n-\t/* Update the estimated exposure and gain. */\n-\tactiveState.agc.automatic.exposure = shutterTime / configuration.sensor.lineDuration;\n-\tactiveState.agc.automatic.gain = stepGain;\n }\n \n /**\n@@ -373,6 +404,19 @@ void Agc::fillMetadata(IPAContext &context, IPAFrameContext &frameContext,\n \tmetadata.set(controls::FrameDuration, frameDuration.get<std::micro>());\n }\n \n+double Agc::estimateLuminance(double gain)\n+{\n+\tdouble ySum = 0.0;\n+\n+\t/* Sum the averages, saturated to 255. */\n+\tfor (uint8_t expMean : expMeans_)\n+\t\tySum += std::min(expMean * gain, 255.0);\n+\n+\t/* \\todo Weight with the AWB gains */\n+\n+\treturn ySum / expMeans_.size() / 255;\n+}\n+\n /**\n  * \\brief Process RkISP1 statistics, and run AGC operations\n  * \\param[in] context The shared IPA context\n@@ -438,7 +482,35 @@ void Agc::process(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n \tcomputeExposure(context, frameContext, yGain, iqMeanGain);\n \tframeCount_++;\n \n+\texpMeans_ = { params->ae.exp_mean, context.hw->numAeCells };\n+\n+\t/*\n+\t * The Agc algorithm needs to know the effective exposure value that was\n+\t * applied to the sensor when the statistics were collected.\n+\t */\n+\tutils::Duration exposureTime = context.configuration.sensor.lineDuration\n+\t\t\t\t       * frameContext.sensor.exposure;\n+\tdouble analogueGain = frameContext.sensor.gain;\n+\tutils::Duration effectiveExposureValue = exposureTime * analogueGain;\n+\n+\tutils::Duration shutterTime;\n+\tdouble aGain, dGain;\n+\tstd::tie(shutterTime, aGain, dGain) =\n+\t\tcalculateNewEv(context.activeState.agc.constraintMode,\n+\t\t\t       context.activeState.agc.exposureMode,\n+\t\t\t       Histogram(hist), effectiveExposureValue);\n+\n+\tLOG(RkISP1Agc, Debug)\n+\t\t<< \"Divided up shutter, analogue gain and digital gain are \"\n+\t\t<< shutterTime << \", \" << aGain << \" and \" << dGain;\n+\n+\tIPAActiveState &activeState = context.activeState;\n+\t/* Update the estimated exposure and gain. */\n+\tactiveState.agc.automatic.exposure = shutterTime / context.configuration.sensor.lineDuration;\n+\tactiveState.agc.automatic.gain = aGain;\n+\n \tfillMetadata(context, frameContext, metadata);\n+\texpMeans_ = {};\n }\n \n REGISTER_IPA_ALGORITHM(Agc, \"Agc\")\ndiff --git a/src/ipa/rkisp1/algorithms/agc.h b/src/ipa/rkisp1/algorithms/agc.h\nindex fb82a33f..a8080228 100644\n--- a/src/ipa/rkisp1/algorithms/agc.h\n+++ b/src/ipa/rkisp1/algorithms/agc.h\n@@ -14,18 +14,22 @@\n \n #include <libcamera/geometry.h>\n \n+#include \"libipa/agc_mean_luminance.h\"\n+#include \"libipa/histogram.h\"\n+\n #include \"algorithm.h\"\n \n namespace libcamera {\n \n namespace ipa::rkisp1::algorithms {\n \n-class Agc : public Algorithm\n+class Agc : public Algorithm, public AgcMeanLuminance\n {\n public:\n \tAgc();\n \t~Agc() = default;\n \n+\tint init(IPAContext &context, const YamlObject &tuningData) override;\n \tint configure(IPAContext &context, const IPACameraSensorInfo &configInfo) override;\n \tvoid queueRequest(IPAContext &context,\n \t\t\t  const uint32_t frame,\n@@ -47,10 +51,13 @@ private:\n \tdouble measureBrightness(Span<const uint32_t> hist) const;\n \tvoid fillMetadata(IPAContext &context, IPAFrameContext &frameContext,\n \t\t\t  ControlList &metadata);\n+\tdouble estimateLuminance(double gain) override;\n \n \tuint64_t frameCount_;\n \n \tutils::Duration filteredExposure_;\n+\n+\tSpan<const uint8_t> expMeans_;\n };\n \n } /* namespace ipa::rkisp1::algorithms */\ndiff --git a/src/ipa/rkisp1/ipa_context.h b/src/ipa/rkisp1/ipa_context.h\nindex 10d8f38c..256b75eb 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/controls.h>\n #include <libcamera/geometry.h>\n \n #include <libipa/fc_queue.h>\n@@ -67,6 +68,8 @@ struct IPAActiveState {\n \t\t} automatic;\n \n \t\tbool autoEnabled;\n+\t\tuint32_t constraintMode;\n+\t\tuint32_t exposureMode;\n \t} agc;\n \n \tstruct {\n@@ -151,6 +154,8 @@ struct IPAContext {\n \tIPAActiveState activeState;\n \n \tFCQueue<IPAFrameContext> frameContexts;\n+\n+\tControlInfoMap::Map ctrlMap;\n };\n \n } /* namespace ipa::rkisp1 */\ndiff --git a/src/ipa/rkisp1/rkisp1.cpp b/src/ipa/rkisp1/rkisp1.cpp\nindex 9dc5f53c..d8610095 100644\n--- a/src/ipa/rkisp1/rkisp1.cpp\n+++ b/src/ipa/rkisp1/rkisp1.cpp\n@@ -119,7 +119,7 @@ const ControlInfoMap::Map rkisp1Controls{\n } /* namespace */\n \n IPARkISP1::IPARkISP1()\n-\t: context_({ {}, {}, {}, { kMaxFrameContexts } })\n+\t: context_({ {}, {}, {}, { kMaxFrameContexts }, {} })\n {\n }\n \n@@ -427,6 +427,7 @@ void IPARkISP1::updateControls(const IPACameraSensorInfo &sensorInfo,\n \t\t\t\t\t\t\t      frameDurations[1],\n \t\t\t\t\t\t\t      frameDurations[2]);\n \n+\tctrlMap.merge(context_.ctrlMap);\n \t*ipaControls = ControlInfoMap(std::move(ctrlMap), controls::controls);\n }\n \n",
    "prefixes": [
        "v2",
        "7/8"
    ]
}