{"id":14621,"url":"https://patchwork.libcamera.org/api/1.1/patches/14621/?format=json","web_url":"https://patchwork.libcamera.org/patch/14621/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","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-6-laurent.pinchart@ideasonboard.com>","date":"2021-11-16T16:26:15","name":"[libcamera-devel,5/5] ipa: ipu3: agc: Saturate the averages when computing relative luminance","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"9dd4f0fa95239e98b70bf406533737552e722d5e","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14621/mbox/","series":[{"id":2722,"url":"https://patchwork.libcamera.org/api/1.1/series/2722/?format=json","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/14621/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14621/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 D9284C324F\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 9A4696037A;\n\tTue, 16 Nov 2021 17:26:50 +0100 (CET)","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 88BF86038A\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 4A968E7\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=\"N44gLHUv\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1637080003;\n\tbh=45PdbriSu7VhWwqIlkK5J/wQLTFcQuaxEfq6m9OuXvg=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=N44gLHUvqtxerIvYAJocK8gLakm4IJvIyD4H8TOL5pu0H3ZH2d3okfok1CZYnW/4D\n\t7vOwJeU8uqtpIZagAgDBBkswcxSEtu10AVcoI97fPsGx3mQR8VBSOTMB4+1COI2S2V\n\tDIbkodeaw1Ly1IatOKDjxYS4KSeqyJEzd4FI8/yc=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 16 Nov 2021 18:26:15 +0200","Message-Id":"<20211116162615.27777-6-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-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 5/5] ipa: ipu3: agc: Saturate the averages\n\twhen computing relative luminance","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 relative luminance is calculated using an iterative process to\naccount for saturation in the sensor, as multiplying pixels by a gain\ndoesn't increase the relative luminance by the same factor if some\nregions are saturated. Relative luminance estimation doesn't apply a\nsaturation, which produces a value that doesn't match what the sensor\nwill output, and defeats the point of the iterative process. Fix it.\n\nFixes: f8f07f9468c6 (\"ipa: ipu3: agc: Improve gain calculation\")\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/ipa/ipu3/algorithms/agc.cpp | 25 ++++++++++++++++++-------\n 1 file changed, 18 insertions(+), 7 deletions(-)","diff":"diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\nindex 71398fdd96a6..46aa1b14a100 100644\n--- a/src/ipa/ipu3/algorithms/agc.cpp\n+++ b/src/ipa/ipu3/algorithms/agc.cpp\n@@ -252,10 +252,19 @@ void Agc::computeExposure(IPAFrameContext &frameContext, double yGain,\n  * \\param[in] gain The analogue gain to apply to the frame\n  * \\return The relative luminance\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+ * This function estimates the average relative luminance of the frame that\n+ * would be output by the sensor if an additional analogue \\a gain was applied.\n+ *\n+ * The estimation is based on the AWB statistics for the current frame. Red,\n+ * green and blue averages for all cells are first multiplied by the gain, and\n+ * then saturated to approximate the sensor behaviour at high brightness\n+ * values. The approximation is quitte rough, as it doesn't take into account\n+ * non-linearities when approaching saturation.\n+ *\n+ * The relative luminance (Y) is computed from the linear RGB components using\n+ * the Rec. 601 formula. The values is normalized to the [0.0, 1.0] range,\n+ * where 1.0 corresponds to a theoretical perfect reflector of 100% reference\n+ * white.\n  *\n  * More detailed information can be found in:\n  * https://en.wikipedia.org/wiki/Relative_luminance\n@@ -267,6 +276,7 @@ double Agc::estimateLuminance(IPAFrameContext &frameContext,\n {\n \tdouble redSum = 0, greenSum = 0, blueSum = 0;\n \n+\t/* Sum the per-channel averages, saturated to 255. */\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@@ -275,10 +285,11 @@ double Agc::estimateLuminance(IPAFrameContext &frameContext,\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+\t\t\tconst uint8_t G_avg = (cell->Gr_avg + cell->Gb_avg) / 2;\n \n-\t\t\tredSum += cell->R_avg * gain;\n-\t\t\tgreenSum += (cell->Gr_avg + cell->Gb_avg) / 2 * gain;\n-\t\t\tblueSum += cell->B_avg * gain;\n+\t\t\tredSum += std::min(cell->R_avg * gain, 255.0);\n+\t\t\tgreenSum += std::min(G_avg * gain, 255.0);\n+\t\t\tblueSum += std::min(cell->B_avg * gain, 255.0);\n \t\t}\n \t}\n \n","prefixes":["libcamera-devel","5/5"]}