Show a patch.

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

{
    "id": 14536,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/14536/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/14536/",
    "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": "<20211110195901.85597-8-jeanmichel.hautbois@ideasonboard.com>",
    "date": "2021-11-10T19:58:54",
    "name": "[libcamera-devel,v2,07/14] ipa: ipu3: agc: Improve gain calculation",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "ea42005c8db7bdde4b41bfaeb6044ce0aee449d5",
    "submitter": {
        "id": 75,
        "url": "https://patchwork.libcamera.org/api/1.1/people/75/?format=api",
        "name": "Jean-Michel Hautbois",
        "email": "jeanmichel.hautbois@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/14536/mbox/",
    "series": [
        {
            "id": 2708,
            "url": "https://patchwork.libcamera.org/api/1.1/series/2708/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2708",
            "date": "2021-11-10T19:58:47",
            "name": "IPA: IPU3: Introduce per-frame controls",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/2708/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/14536/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/14536/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 4B859C324E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 10 Nov 2021 19:59:17 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EC8726035D;\n\tWed, 10 Nov 2021 20:59:16 +0100 (CET)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6B16A60361\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 10 Nov 2021 20:59:07 +0100 (CET)",
            "from tatooine.ideasonboard.com (unknown\n\t[IPv6:2a01:e0a:169:7140:7713:3465:89e6:c5cd])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2E0961203;\n\tWed, 10 Nov 2021 20:59:07 +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=\"CMcJxqQZ\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1636574347;\n\tbh=QNlsYC/OZjS6lvW+7UJez1A3UxSSPFqQiRM/o5r9eSg=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=CMcJxqQZMG15+G0ceAIimZEL1Spcbmlsh8J3JWcdF+pj+mDQolSvRAfm3FD4KeaQY\n\tO7FTTtlyX50RaQoU1QpfBUgI08CxbuiRRyHhR9OsVKwmwnBODarXDEGT6tFzrHW2fU\n\tw8FDdX4SbjrN8b83WsZqJZenwaqZKtrcC9HoNr1M=",
        "From": "Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Wed, 10 Nov 2021 20:58:54 +0100",
        "Message-Id": "<20211110195901.85597-8-jeanmichel.hautbois@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.32.0",
        "In-Reply-To": "<20211110195901.85597-1-jeanmichel.hautbois@ideasonboard.com>",
        "References": "<20211110195901.85597-1-jeanmichel.hautbois@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v2 07/14] ipa: ipu3: agc: Improve gain\n\tcalculation",
        "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": "When an image is partially saturated, its brightness is not increasing\nlinearly when the shutter time or gain increases. It is a big issue with\na backlight as the algorithm is fading to darkness right now.\n\nIntroduce a function to estimate the brightness of the frame, based on\nthe current exposure/gain and loop on it several times to estimate it\nagain and approach the non linear function.\n\nInspired-by: 7de5506c30b3 (\"libcamera: src: ipa: raspberrypi: agc: Improve gain update calculation for partly saturated images\")\nSigned-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n---\n src/ipa/ipu3/algorithms/agc.cpp | 103 +++++++++++++++++++++++++++++++-\n src/ipa/ipu3/algorithms/agc.h   |   6 +-\n 2 files changed, 105 insertions(+), 4 deletions(-)",
    "diff": "diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\nindex 119a7938..ee37a9d5 100644\n--- a/src/ipa/ipu3/algorithms/agc.cpp\n+++ b/src/ipa/ipu3/algorithms/agc.cpp\n@@ -67,6 +67,9 @@ static constexpr uint32_t kMinCellsPerZoneRatio = 255 * 20 / 100;\n /* Number of frames to wait before calculating stats on minimum exposure */\n static constexpr uint32_t kNumStartupFrames = 10;\n \n+/* Maximum luminance used for brightness normalization */\n+static constexpr uint32_t kMaxLuminance = 255;\n+\n Agc::Agc()\n \t: frameCount_(0), iqMean_(0.0), lineDuration_(0s), minExposureLines_(0),\n \t  maxExposureLines_(0), filteredExposure_(0s), currentExposure_(0s),\n@@ -186,10 +189,16 @@ void Agc::filterExposure()\n  * \\brief Estimate the new exposure and gain values\n  * \\param[inout] exposure The exposure value reference as a number of lines\n  * \\param[inout] gain The gain reference to be updated\n+ * \\param[in] currentYGain The gain calculated on the current brightness level\n  */\n-void Agc::computeExposure(uint32_t &exposure, double &analogueGain)\n+void Agc::computeExposure(uint32_t &exposure, double &analogueGain, double currentYGain)\n {\n-\t/* Estimate the gain needed to have the proportion wanted */\n+\t/*\n+\t * Estimate the gain needed to have the proportion of pixels in a given\n+\t * range wanted. 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 (std::abs(evGain - 1.0) < 0.01) {\n@@ -199,6 +208,7 @@ void Agc::computeExposure(uint32_t &exposure, double &analogueGain)\n \t}\n \n \t/* extracted from Rpi::Agc::computeTargetExposure */\n+\n \t/* Calculate the shutter time in seconds */\n \tutils::Duration currentShutter = exposure * lineDuration_;\n \tLOG(IPU3Agc, Debug) << \"Actual total exposure \" << currentShutter * analogueGain\n@@ -206,6 +216,14 @@ void Agc::computeExposure(uint32_t &exposure, double &analogueGain)\n \t\t\t    << \" Gain \" << analogueGain\n \t\t\t    << \" Needed ev gain \" << evGain;\n \n+\tif (evGain < currentYGain)\n+\t\tevGain = currentYGain;\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+\n \t/*\n \t * Calculate the current exposure value for the scene as the latest\n \t * exposure value applied multiplied by the new estimated gain.\n@@ -253,6 +271,57 @@ void Agc::computeExposure(uint32_t &exposure, double &analogueGain)\n \tprevExposureValue_ = shutterTime * analogueGain;\n }\n \n+/**\n+ * \\brief Estimate the average brightness of the frame\n+ * \\param[in] context The shared IPA context\n+ * \\param[in] grid The grid used to store the statistics in the IPU3\n+ * \\param[in] stats The IPU3 statistics and ISP results\n+ * \\param[in] currentYGain The gain calculated on the current brightness level\n+ * \\return The normalized luma\n+ *\n+ * Luma is the weighted sum of gamma-compressed R′G′B′ components of a color\n+ * video. The luma values are normalized as 0.0 to 1.0, with 1.0 being a\n+ * theoretical perfect reflector of 100% reference white. We use the Rec. 601\n+ * luma here.\n+ *\n+ * More detailed information can be found in:\n+ * https://en.wikipedia.org/wiki/Luma_(video)\n+ */\n+double Agc::computeInitialY(IPAFrameContext &frameContext,\n+\t\t\t    const ipu3_uapi_grid_config &grid,\n+\t\t\t    const ipu3_uapi_stats_3a *stats,\n+\t\t\t    double currentYGain)\n+{\n+\tdouble redSum = 0, greenSum = 0, blueSum = 0;\n+\n+\tfor (unsigned int cellY = 0; cellY < grid.height; cellY++) {\n+\t\tfor (unsigned int cellX = 0; cellX < grid.width; cellX++) {\n+\t\t\tuint32_t cellPosition = cellY * stride_ + cellX;\n+\n+\t\t\tconst ipu3_uapi_awb_set_item *cell =\n+\t\t\t\treinterpret_cast<const ipu3_uapi_awb_set_item *>(\n+\t\t\t\t\t&stats->awb_raw_buffer.meta_data[cellPosition]\n+\t\t\t\t);\n+\n+\t\t\tredSum += cell->R_avg * currentYGain;\n+\t\t\tgreenSum += (cell->Gr_avg + cell->Gb_avg) / 2 * currentYGain;\n+\t\t\tblueSum += cell->B_avg * currentYGain;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * Estimate the sum of the brightness values, weighted with the gains\n+\t * applied on the channels in AWB as the Rec. 601 luma.\n+\t */\n+\tdouble Y_sum = redSum * frameContext.awb.gains.red * .299 +\n+\t\t       greenSum * frameContext.awb.gains.green * .587 +\n+\t\t       blueSum * frameContext.awb.gains.blue * .114;\n+\n+\t/* Return the normalized relative luminance. */\n+\treturn Y_sum / (grid.height * grid.width) / kMaxLuminance;\n+}\n+\n+\n /**\n  * \\brief Process IPU3 statistics, and run AGC operations\n  * \\param[in] context The shared IPA context\n@@ -267,7 +336,35 @@ void Agc::process(IPAContext &context, const ipu3_uapi_stats_3a *stats)\n \tuint32_t &exposure = context.frameContext.agc.exposure;\n \tdouble &analogueGain = context.frameContext.agc.gain;\n \tmeasureBrightness(stats, context.configuration.grid.bdsGrid);\n-\tcomputeExposure(exposure, analogueGain);\n+\n+\tdouble currentYGain = 1.0;\n+\t/*\n+\t * Normalized luma value target.\n+\t *\n+\t * It's a number that's chosen so that, when the camera points at a grey\n+\t * target, the resulting image brightness is considered right.\n+\t */\n+\tdouble targetY = 0.16;\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 (int i = 0; i < 8; i++) {\n+\t\tdouble initialY = computeInitialY(context.frameContext,\n+\t\t\t\t\t\t  context.configuration.grid.bdsGrid,\n+\t\t\t\t\t\t  stats, currentYGain);\n+\t\tdouble extra_gain = std::min(10.0, targetY / (initialY + .001));\n+\n+\t\tcurrentYGain *= extra_gain;\n+\t\tLOG(IPU3Agc, Debug) << \"Initial Y \" << initialY\n+\t\t\t\t    << \" target \" << targetY\n+\t\t\t\t    << \" gives gain \" << currentYGain;\n+\t\tif (extra_gain < 1.01)\n+\t\t\tbreak;\n+\t}\n+\n+\tcomputeExposure(exposure, analogueGain, currentYGain);\n \tframeCount_++;\n }\n \ndiff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h\nindex 69e0b831..0a9152a9 100644\n--- a/src/ipa/ipu3/algorithms/agc.h\n+++ b/src/ipa/ipu3/algorithms/agc.h\n@@ -34,7 +34,11 @@ private:\n \tvoid measureBrightness(const ipu3_uapi_stats_3a *stats,\n \t\t\t       const ipu3_uapi_grid_config &grid);\n \tvoid filterExposure();\n-\tvoid computeExposure(uint32_t &exposure, double &gain);\n+\tvoid computeExposure(uint32_t &exposure, double &gain, double currentYGain);\n+\tdouble computeInitialY(IPAFrameContext &frameContext,\n+\t\t\t       const ipu3_uapi_grid_config &grid,\n+\t\t\t       const ipu3_uapi_stats_3a *stats,\n+\t\t\t       double currentYGain);\n \n \tuint64_t frameCount_;\n \tuint64_t lastFrame_;\n",
    "prefixes": [
        "libcamera-devel",
        "v2",
        "07/14"
    ]
}