{"id":14667,"url":"https://patchwork.libcamera.org/api/1.1/patches/14667/?format=json","web_url":"https://patchwork.libcamera.org/patch/14667/","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":"<20211119210239.18540-6-laurent.pinchart@ideasonboard.com>","date":"2021-11-19T21:02:39","name":"[libcamera-devel,v2,5/5] ipa: ipu3: agc: Saturate the averages when computing relative luminance","commit_ref":"b0e31c902084b9f4dfa2bafa07a1d62c83eb6292","pull_url":null,"state":"accepted","archived":false,"hash":"29d740aa73fef2877ec7e5691100ba9e2924f6c5","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/14667/mbox/","series":[{"id":2738,"url":"https://patchwork.libcamera.org/api/1.1/series/2738/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2738","date":"2021-11-19T21:02:34","name":"ipa: ipu3: agc: Misc improvements","version":2,"mbox":"https://patchwork.libcamera.org/series/2738/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14667/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14667/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 540C9C3250\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 19 Nov 2021 21:03:14 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DC36F60398;\n\tFri, 19 Nov 2021 22:03:13 +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 9558A600B5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 19 Nov 2021 22:03:07 +0100 (CET)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3B99B1C72\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 19 Nov 2021 22:03: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=\"NPGhxA4y\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1637355787;\n\tbh=JPKLiwTHxFE2lUqAo+jBhpHlDvWBvy+KofV9Z+zhqQ0=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=NPGhxA4yj5qcf8KqQvsyhfl4l/wQJuguFQjv33WpOkWSTe/D8/qINy1VBNuBe83UT\n\tGzE4WTq5eBAonJ2fqxzqVn9u1mA0rQrq27r76BgObYJXM9WOTYipckX86dS+QElPgC\n\t4oKjkvftKwx6OvXRHs1CSZlorP58yJW1fztKITLg=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 19 Nov 2021 23:02:39 +0200","Message-Id":"<20211119210239.18540-6-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20211119210239.18540-1-laurent.pinchart@ideasonboard.com>","References":"<20211119210239.18540-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 5/5] ipa: ipu3: agc: Saturate the\n\taverages when 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>\nTested-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\nReviewed-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\nTested-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\nChanges since v1:\n\n- Replace incorrect mention of analogue gain with gain\n- Fix typos\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 c39e213f1dac..582f0ae1b54b 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 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 \\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 quite 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 are 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","v2","5/5"]}