Show a patch.

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

{
    "id": 20033,
    "url": "https://patchwork.libcamera.org/api/patches/20033/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/20033/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/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": "<20240509175555.43121-3-paul.elder@ideasonboard.com>",
    "date": "2024-05-09T17:55:54",
    "name": "[v2,2/3] ipa: libipa: agc: Change luminance target to piecewise linear function",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "15942895638f5b368dc809c32285391f71fa2386",
    "submitter": {
        "id": 17,
        "url": "https://patchwork.libcamera.org/api/people/17/?format=api",
        "name": "Paul Elder",
        "email": "paul.elder@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/20033/mbox/",
    "series": [
        {
            "id": 4304,
            "url": "https://patchwork.libcamera.org/api/series/4304/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4304",
            "date": "2024-05-09T17:55:52",
            "name": "ipa: rkisp1: Improve AGC (plumbing)",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/4304/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/20033/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/20033/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 A7601C32B5\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  9 May 2024 17:56:16 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 61B8C63467;\n\tThu,  9 May 2024 19:56:15 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7EF9A633FA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  9 May 2024 19:56:12 +0200 (CEST)",
            "from neptunite.hamster-moth.ts.net\n\t(h175-177-049-156.catv02.itscom.jp [175.177.49.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id AA3C0CC8;\n\tThu,  9 May 2024 19:56:07 +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=\"Nf7zK6I6\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1715277368;\n\tbh=GUHN6GfS2ArfjHzi/iNBAQWD3y2NfZIjf7y9B652ZSE=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Nf7zK6I6Nf6gLIPLtHxbvt813UohFF0pWsvLVv4lUBNGBkR9aM8194EpuBYptdkM4\n\thyNk5inqMV2O9EUjXWts7cWH6paiDMdfCyJVQR48IH5F6HByhRGERailh5yU4oiDx2\n\tJyQK33zeQcgdDtkAR0r/QhbzVVCbfyqxoI8V7Rhg=",
        "From": "Paul Elder <paul.elder@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Paul Elder <paul.elder@ideasonboard.com>,\n\tStefan Klug <stefan.klug@ideasonboard.com>",
        "Subject": "[PATCH v2 2/3] ipa: libipa: agc: Change luminance target to\n\tpiecewise linear function",
        "Date": "Fri, 10 May 2024 02:55:54 +0900",
        "Message-Id": "<20240509175555.43121-3-paul.elder@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.39.2",
        "In-Reply-To": "<20240509175555.43121-1-paul.elder@ideasonboard.com>",
        "References": "<20240509175555.43121-1-paul.elder@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": "Change the relative luminance target from a scalar value to a piecewise\nlinear function that needs to be sampled by the estimate lux value.\n\nAlso change the rkisp1 and ipu3 IPAs accordingly, as they use the libipa\nagc. As they both don't yet have lux modules, hardcode them to a single\nlux value for now.\n\nThis affects the format of the tuning files, but as there aren't yet any\nthis shouldn't be an issue.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n\n---\nChanges in v2:\n- s/FPoint/PointF/\n- add warning when using default relative luminance target when loading\n  from the tuning file fails\n---\n src/ipa/ipu3/algorithms/agc.cpp       |  5 ++++-\n src/ipa/libipa/agc_mean_luminance.cpp | 31 +++++++++++++++++++++------\n src/ipa/libipa/agc_mean_luminance.h   |  7 +++---\n src/ipa/rkisp1/algorithms/agc.cpp     |  5 ++++-\n 4 files changed, 36 insertions(+), 12 deletions(-)",
    "diff": "diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\nindex c9b5548c..984ed087 100644\n--- a/src/ipa/ipu3/algorithms/agc.cpp\n+++ b/src/ipa/ipu3/algorithms/agc.cpp\n@@ -222,12 +222,15 @@ void Agc::process(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n \tdouble analogueGain = frameContext.sensor.gain;\n \tutils::Duration effectiveExposureValue = exposureTime * analogueGain;\n \n+\t/* \\todo Plumb in the lux value. Requires a lux algo + tuning */\n+\tdouble lux = 400;\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, hist,\n-\t\t\t       effectiveExposureValue);\n+\t\t\t       effectiveExposureValue, lux);\n \n \tLOG(IPU3Agc, Debug)\n \t\t<< \"Divided up shutter, analogue gain and digital gain are \"\ndiff --git a/src/ipa/libipa/agc_mean_luminance.cpp b/src/ipa/libipa/agc_mean_luminance.cpp\nindex 2bf84d05..fe07777d 100644\n--- a/src/ipa/libipa/agc_mean_luminance.cpp\n+++ b/src/ipa/libipa/agc_mean_luminance.cpp\n@@ -134,7 +134,7 @@ static constexpr double kDefaultRelativeLuminanceTarget = 0.16;\n  */\n \n AgcMeanLuminance::AgcMeanLuminance()\n-\t: frameCount_(0), filteredExposure_(0s), relativeLuminanceTarget_(0)\n+\t: frameCount_(0), filteredExposure_(0s)\n {\n }\n \n@@ -142,8 +142,16 @@ AgcMeanLuminance::~AgcMeanLuminance() = default;\n \n void AgcMeanLuminance::parseRelativeLuminanceTarget(const YamlObject &tuningData)\n {\n-\trelativeLuminanceTarget_ =\n-\t\ttuningData[\"relativeLuminanceTarget\"].get<double>(kDefaultRelativeLuminanceTarget);\n+\tint ret = relativeLuminanceTarget_.readYaml(tuningData[\"relativeLuminanceTarget\"]);\n+\tif (ret == 0)\n+\t\treturn;\n+\n+\tLOG(AgcMeanLuminance, Warning)\n+\t\t<< \"Failed to load tuning parameter 'relativeLuminanceTarget', \"\n+\t\t<< \"using default [0, \" << kDefaultRelativeLuminanceTarget << \"]\";\n+\n+\tstd::vector<PointF> points = { { 0, kDefaultRelativeLuminanceTarget } };\n+\trelativeLuminanceTarget_ = Pwl(points);\n }\n \n void AgcMeanLuminance::parseConstraint(const YamlObject &modeDict, int32_t id)\n@@ -421,11 +429,18 @@ void AgcMeanLuminance::setLimits(utils::Duration minShutter,\n /**\n  * \\brief Estimate the initial gain needed to achieve a relative luminance\n  * target\n+ * \\param[in] lux The lux value at which to sample the luminance target pwl\n+ *\n+ * To account for non-linearity caused by saturation, the value needs to be\n+ * estimated in an iterative process, as multiplying by a gain will not increase\n+ * the relative luminance by the same factor if some image regions are saturated\n+ *\n  * \\return The calculated initial gain\n  */\n-double AgcMeanLuminance::estimateInitialGain() const\n+double AgcMeanLuminance::estimateInitialGain(double lux) const\n {\n-\tdouble yTarget = relativeLuminanceTarget_;\n+\tdouble yTarget =\n+\t\trelativeLuminanceTarget_.eval(relativeLuminanceTarget_.domain().clamp(lux));\n \tdouble yGain = 1.0;\n \n \t/*\n@@ -520,6 +535,7 @@ utils::Duration AgcMeanLuminance::filterExposure(utils::Duration exposureValue)\n  * the calculated gain\n  * \\param[in] effectiveExposureValue The EV applied to the frame from which the\n  * statistics in use derive\n+ * \\param[in] lux The lux value at which to sample the luminance target pwl\n  *\n  * Calculate a new exposure value to try to obtain the target. The calculated\n  * exposure value is filtered to prevent rapid changes from frame to frame, and\n@@ -531,7 +547,8 @@ std::tuple<utils::Duration, double, double>\n AgcMeanLuminance::calculateNewEv(uint32_t constraintModeIndex,\n \t\t\t\t uint32_t exposureModeIndex,\n \t\t\t\t const Histogram &yHist,\n-\t\t\t\t utils::Duration effectiveExposureValue)\n+\t\t\t\t utils::Duration effectiveExposureValue,\n+\t\t\t\t double lux)\n {\n \t/*\n \t * The pipeline handler should validate that we have received an allowed\n@@ -540,7 +557,7 @@ AgcMeanLuminance::calculateNewEv(uint32_t constraintModeIndex,\n \tstd::shared_ptr<ExposureModeHelper> exposureModeHelper =\n \t\texposureModeHelpers_.at(exposureModeIndex);\n \n-\tdouble gain = estimateInitialGain();\n+\tdouble gain = estimateInitialGain(lux);\n \tgain = constraintClampGain(constraintModeIndex, yHist, gain);\n \n \t/*\ndiff --git a/src/ipa/libipa/agc_mean_luminance.h b/src/ipa/libipa/agc_mean_luminance.h\nindex 0a81c6d2..6ec2a0dc 100644\n--- a/src/ipa/libipa/agc_mean_luminance.h\n+++ b/src/ipa/libipa/agc_mean_luminance.h\n@@ -18,6 +18,7 @@\n \n #include \"exposure_mode_helper.h\"\n #include \"histogram.h\"\n+#include \"pwl.h\"\n \n namespace libcamera {\n \n@@ -62,7 +63,7 @@ public:\n \n \tstd::tuple<utils::Duration, double, double>\n \tcalculateNewEv(uint32_t constraintModeIndex, uint32_t exposureModeIndex,\n-\t\t       const Histogram &yHist, utils::Duration effectiveExposureValue);\n+\t\t       const Histogram &yHist, utils::Duration effectiveExposureValue, double lux);\n \n \tvoid resetFrameCount()\n \t{\n@@ -76,7 +77,7 @@ private:\n \tvoid parseConstraint(const YamlObject &modeDict, int32_t id);\n \tint parseConstraintModes(const YamlObject &tuningData);\n \tint parseExposureModes(const YamlObject &tuningData);\n-\tdouble estimateInitialGain() const;\n+\tdouble estimateInitialGain(double lux) const;\n \tdouble constraintClampGain(uint32_t constraintModeIndex,\n \t\t\t\t   const Histogram &hist,\n \t\t\t\t   double gain);\n@@ -84,7 +85,7 @@ private:\n \n \tuint64_t frameCount_;\n \tutils::Duration filteredExposure_;\n-\tdouble relativeLuminanceTarget_;\n+\tPwl relativeLuminanceTarget_;\n \n \tstd::map<int32_t, std::vector<AgcConstraint>> constraintModes_;\n \tstd::map<int32_t, std::shared_ptr<ExposureModeHelper>> exposureModeHelpers_;\ndiff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp\nindex 4fc06277..bd9a6a66 100644\n--- a/src/ipa/rkisp1/algorithms/agc.cpp\n+++ b/src/ipa/rkisp1/algorithms/agc.cpp\n@@ -384,12 +384,15 @@ void Agc::process(IPAContext &context, [[maybe_unused]] const uint32_t frame,\n \tdouble analogueGain = frameContext.sensor.gain;\n \tutils::Duration effectiveExposureValue = exposureTime * analogueGain;\n \n+\t/* \\todo Plumb in the lux value. Requires a lux algo + tuning */\n+\tdouble lux = 400;\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       hist, effectiveExposureValue);\n+\t\t\t       hist, effectiveExposureValue, lux);\n \n \tLOG(RkISP1Agc, Debug)\n \t\t<< \"Divided up shutter, analogue gain and digital gain are \"\n",
    "prefixes": [
        "v2",
        "2/3"
    ]
}