Show a patch.

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

{
    "id": 14620,
    "url": "https://patchwork.libcamera.org/api/patches/14620/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/14620/",
    "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": "<20211116162615.27777-5-laurent.pinchart@ideasonboard.com>",
    "date": "2021-11-16T16:26:14",
    "name": "[libcamera-devel,4/5] ipa: ipu3: agc: Return the inter-quantile mean from measureBrightness()",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "51ae56aa7efee54c20094dec141d282815aa778c",
    "submitter": {
        "id": 2,
        "url": "https://patchwork.libcamera.org/api/people/2/?format=api",
        "name": "Laurent Pinchart",
        "email": "laurent.pinchart@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/14620/mbox/",
    "series": [
        {
            "id": 2722,
            "url": "https://patchwork.libcamera.org/api/series/2722/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2722",
            "date": "2021-11-16T16:26:10",
            "name": "ipa: ipu3: agc: Misc improvements",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/2722/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/14620/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/14620/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 42510BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 16 Nov 2021 16:26:51 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9097F603BE;\n\tTue, 16 Nov 2021 17:26:49 +0100 (CET)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 54B7C6037A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 16 Nov 2021 17:26:43 +0100 (CET)",
            "from localhost.localdomain (117.145-247-81.adsl-dyn.isp.belgacom.be\n\t[81.247.145.117])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 19B509CA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 16 Nov 2021 17:26:43 +0100 (CET)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"XtVRIp2M\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1637080003;\n\tbh=ZnZ8fVY1xj8ztC8Zei3e73ubKzZ8ZUeEbhNt+nY4bPg=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=XtVRIp2MbIPduXfeyWbVwIIL3tG0sM77vsldQNQ1TtmKOGnDiLeMxr5l9oWLjRMq+\n\tx7kPg4s3+Xf/xsTUwmCw55V6iGnMTdT1dSsuqsyNa+rdnc63meKwfRolR6Hpl6TwA6\n\taGRiwg5TmNV/NCF1PlsLUxbK1Na7cz/jpgoiXI0c=",
        "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Tue, 16 Nov 2021 18:26:14 +0200",
        "Message-Id": "<20211116162615.27777-5-laurent.pinchart@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.32.0",
        "In-Reply-To": "<20211116162615.27777-1-laurent.pinchart@ideasonboard.com>",
        "References": "<20211116162615.27777-1-laurent.pinchart@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH 4/5] ipa: ipu3: agc: Return the\n\tinter-quantile mean from measureBrightness()",
        "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": "The inter-quantile mean is a value that is computed as part of the AGC\nrun. It doesn't need to be stored in a member variable. Return it from\nmeasureBrightness(), which makes the flow of data easier to follow.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/ipa/ipu3/algorithms/agc.cpp | 53 ++++++++++++++++++---------------\n src/ipa/ipu3/algorithms/agc.h   |  9 +++---\n 2 files changed, 33 insertions(+), 29 deletions(-)",
    "diff": "diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\nindex 6aab9fd5ebb5..71398fdd96a6 100644\n--- a/src/ipa/ipu3/algorithms/agc.cpp\n+++ b/src/ipa/ipu3/algorithms/agc.cpp\n@@ -70,7 +70,7 @@ static constexpr uint32_t kNumStartupFrames = 10;\n static constexpr double kRelativeLuminanceTarget = 0.16;\n \n Agc::Agc()\n-\t: frameCount_(0), iqMean_(0.0), lineDuration_(0s), minShutterSpeed_(0s),\n+\t: frameCount_(0), lineDuration_(0s), minShutterSpeed_(0s),\n \t  maxShutterSpeed_(0s), filteredExposure_(0s), currentExposure_(0s)\n {\n }\n@@ -108,9 +108,10 @@ int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo)\n  * \\brief Estimate the mean value of the top 2% of the histogram\n  * \\param[in] stats The statistics computed by the ImgU\n  * \\param[in] grid The grid used to store the statistics in the IPU3\n+ * \\return The mean value of the top 2% of the histogram\n  */\n-void Agc::measureBrightness(const ipu3_uapi_stats_3a *stats,\n-\t\t\t    const ipu3_uapi_grid_config &grid)\n+double Agc::measureBrightness(const ipu3_uapi_stats_3a *stats,\n+\t\t\t      const ipu3_uapi_grid_config &grid) const\n {\n \t/* Initialise the histogram array */\n \tuint32_t hist[knumHistogramBins] = { 0 };\n@@ -135,8 +136,8 @@ void Agc::measureBrightness(const ipu3_uapi_stats_3a *stats,\n \t\t}\n \t}\n \n-\t/* Estimate the quantile mean of the top 2% of the histogram */\n-\tiqMean_ = Histogram(Span<uint32_t>(hist)).interQuantileMean(0.98, 1.0);\n+\t/* Estimate the quantile mean of the top 2% of the histogram. */\n+\treturn Histogram(Span<uint32_t>(hist)).interQuantileMean(0.98, 1.0);\n }\n \n /**\n@@ -174,28 +175,22 @@ void Agc::filterExposure()\n  * \\brief Estimate the new exposure and gain values\n  * \\param[inout] frameContext The shared IPA frame Context\n  * \\param[in] yGain The gain calculated based on the relative luminance target\n+ * \\param[in] iqMeanGain The gain calculated based on the relative luminance target\n  */\n-void Agc::computeExposure(IPAFrameContext &frameContext, double yGain)\n+void Agc::computeExposure(IPAFrameContext &frameContext, double yGain,\n+\t\t\t  double iqMeanGain)\n {\n \t/* Get the effective exposure and gain applied on the sensor. */\n \tuint32_t exposure = frameContext.sensor.exposure;\n \tdouble analogueGain = frameContext.sensor.gain;\n \n-\t/*\n-\t * Estimate the gain needed to have the proportion of pixels in a given\n-\t * desired range. iqMean_ returns the mean value of the top 2% of the\n-\t * cumulative histogram, and we want it to be as close as possible to a\n-\t * configured target.\n-\t */\n-\tdouble evGain = kEvGainTarget * knumHistogramBins / iqMean_;\n-\n-\tif (evGain < yGain)\n-\t\tevGain = yGain;\n+\t/* Use the highest of the two gain estimates. */\n+\tdouble evGain = std::max(yGain, iqMeanGain);\n \n \t/* Consider within 1% of the target as correctly exposed */\n \tif (std::abs(evGain - 1.0) < 0.01)\n-\t\tLOG(IPU3Agc, Debug) << \"We are well exposed (iqMean = \"\n-\t\t\t\t    << iqMean_ << \")\";\n+\t\tLOG(IPU3Agc, Debug) << \"We are well exposed (evGain = \"\n+\t\t\t\t    << evGain << \")\";\n \n \t/* extracted from Rpi::Agc::computeTargetExposure */\n \n@@ -308,15 +303,25 @@ double Agc::estimateLuminance(IPAFrameContext &frameContext,\n  */\n void Agc::process(IPAContext &context, const ipu3_uapi_stats_3a *stats)\n {\n-\tmeasureBrightness(stats, context.configuration.grid.bdsGrid);\n+\t/*\n+\t * Estimate the gain needed to have the proportion of pixels in a given\n+\t * desired range. iqMean is the mean value of the top 2% of the\n+\t * cumulative histogram, and we want it to be as close as possible to a\n+\t * configured target.\n+\t */\n+\tdouble iqMean = measureBrightness(stats, context.configuration.grid.bdsGrid);\n+\tdouble iqMeanGain = kEvGainTarget * knumHistogramBins / iqMean;\n \n+\t/*\n+\t * Estimate the gain needed to achieve a relative luminance target. To\n+\t * account for non-linearity caused by saturation, the value needs to be\n+\t * estimated in an iterative process, as multiplying by a gain will not\n+\t * increase the relative luminance by the same factor if some image\n+\t * regions are saturated.\n+\t */\n \tdouble yGain = 1.0;\n \tdouble yTarget = kRelativeLuminanceTarget;\n \n-\t/*\n-\t * Do this calculation a few times as brightness increase can be\n-\t * non-linear when there are saturated regions.\n-\t */\n \tfor (unsigned int i = 0; i < 8; i++) {\n \t\tdouble yValue = estimateLuminance(context.frameContext,\n \t\t\t\t\t\t  context.configuration.grid.bdsGrid,\n@@ -331,7 +336,7 @@ void Agc::process(IPAContext &context, const ipu3_uapi_stats_3a *stats)\n \t\t\tbreak;\n \t}\n \n-\tcomputeExposure(context.frameContext, yGain);\n+\tcomputeExposure(context.frameContext, yGain, iqMeanGain);\n \tframeCount_++;\n }\n \ndiff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h\nindex 0c868d6737f1..a04a81fb8eae 100644\n--- a/src/ipa/ipu3/algorithms/agc.h\n+++ b/src/ipa/ipu3/algorithms/agc.h\n@@ -31,10 +31,11 @@ public:\n \tvoid process(IPAContext &context, const ipu3_uapi_stats_3a *stats) override;\n \n private:\n-\tvoid measureBrightness(const ipu3_uapi_stats_3a *stats,\n-\t\t\t       const ipu3_uapi_grid_config &grid);\n+\tdouble measureBrightness(const ipu3_uapi_stats_3a *stats,\n+\t\t\t\t const ipu3_uapi_grid_config &grid) const;\n \tvoid filterExposure();\n-\tvoid computeExposure(IPAFrameContext &frameContext, double yGain);\n+\tvoid computeExposure(IPAFrameContext &frameContext, double yGain,\n+\t\t\t     double iqMeanGain);\n \tdouble estimateLuminance(IPAFrameContext &frameContext,\n \t\t\t\t const ipu3_uapi_grid_config &grid,\n \t\t\t\t const ipu3_uapi_stats_3a *stats,\n@@ -42,8 +43,6 @@ private:\n \n \tuint64_t frameCount_;\n \n-\tdouble iqMean_;\n-\n \tutils::Duration lineDuration_;\n \tutils::Duration minShutterSpeed_;\n \tutils::Duration maxShutterSpeed_;\n",
    "prefixes": [
        "libcamera-devel",
        "4/5"
    ]
}