{"id":26263,"url":"https://patchwork.libcamera.org/api/patches/26263/?format=json","web_url":"https://patchwork.libcamera.org/patch/26263/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20260305-agc-proportional-v3-2-25abc1bfacca@jetm.me>","date":"2026-03-05T20:10:38","name":"[v3,2/3] libcamera: software_isp: Normalize statistics sums to 8-bit","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"f15aa23cf03fd5bcd192016a8f94eeb3e6edf6df","submitter":{"id":261,"url":"https://patchwork.libcamera.org/api/people/261/?format=json","name":"Javier Tia","email":"floss@jetm.me"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/26263/mbox/","series":[{"id":5822,"url":"https://patchwork.libcamera.org/api/series/5822/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5822","date":"2026-03-05T20:10:36","name":"Simple pipeline: proportional AGC, AWB stats fix, OV2740 black level","version":3,"mbox":"https://patchwork.libcamera.org/series/5822/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/26263/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/26263/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 D8289BE086\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  5 Mar 2026 20:16:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 869E9625FC;\n\tThu,  5 Mar 2026 21:16:29 +0100 (CET)","from fhigh-b7-smtp.messagingengine.com\n\t(fhigh-b7-smtp.messagingengine.com [202.12.124.158])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1C2EB623B1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  5 Mar 2026 21:16:28 +0100 (CET)","from phl-compute-02.internal (phl-compute-02.internal\n\t[10.202.2.42])\n\tby mailfhigh.stl.internal (Postfix) with ESMTP id 1769A7A015B;\n\tThu,  5 Mar 2026 15:16:27 -0500 (EST)","from phl-imap-07 ([10.202.2.97])\n\tby phl-compute-02.internal (MEProxy); Thu, 05 Mar 2026 15:16:27 -0500","by mailuser.phl.internal (Postfix, from userid 501)\n\tid C9B5F1EA006B; Thu,  5 Mar 2026 15:16:26 -0500 (EST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=jetm.me header.i=@jetm.me header.b=\"i4oqO1+8\";\n\tdkim=pass (2048-bit key;\n\tunprotected) header.d=messagingengine.com\n\theader.i=@messagingengine.com header.b=\"kN2hH9Kx\"; \n\tdkim-atps=neutral","DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/relaxed; d=jetm.me; h=cc:cc\n\t:content-transfer-encoding:content-type:content-type:date:date\n\t:from:from:in-reply-to:in-reply-to:message-id:mime-version\n\t:references:reply-to:subject:subject:to:to; s=fm3; t=1772741786;\n\tx=1772828186; bh=uy0t7nB/HlnYq82HqVt/dgAUvE9Kh6vaam5WKqkGTo4=; b=\n\ti4oqO1+8Mo9s2JaqJSswEUbWH6V+iS2aW7DNhtM5c03V2qGcrzZzbW3Xk1aDkknH\n\tbPrh+TKmUiGBKTRyUcFgMM0TPB7DgpHv3oP+UUFcfCvQwQBDmUKdEwNsHOrPa8gy\n\ty14cHGtqTDW0gOK9BvG/umZmMwx7kRydTojR/ite0kG98LEFmdD1e2D3v4gqPEFr\n\tGeE24FM249kUMv7GnlFGqD40sHWy7sFqKLXm6u7gOk+8fp0H9QR/vs2g8gBz57Og\n\tAlbYvJfT/MndtUMpVcX053/7QlskdyiOxU+a0B7GtH3nxI0xr+rAiHbiTb+VZ8d2\n\tcwzefPS4xFNIXqD9KsRzZA==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=\n\tmessagingengine.com; h=cc:cc:content-transfer-encoding\n\t:content-type:content-type:date:date:feedback-id:feedback-id\n\t:from:from:in-reply-to:in-reply-to:message-id:mime-version\n\t:references:reply-to:subject:subject:to:to:x-me-proxy\n\t:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1772741786; x=\n\t1772828186; bh=uy0t7nB/HlnYq82HqVt/dgAUvE9Kh6vaam5WKqkGTo4=; b=k\n\tN2hH9Kx/ertCsX8wwVU7su2bbood4kxoO74yhmaShL5pZkApjOoEXt3wwKOu5mzb\n\t3AcCB4S091RFxLrhvgi/ISqrF5F9GcsYtY2NmHwTUkJQy6yjD9wDljYN7GhgLrrJ\n\tdP4J3rfGpisqm/NShS071eE4k+IFyDH3cXU7Ufr+5IVV6cj1+WWY5cNzIsiP3WIi\n\tlnw7xR2nela6kmqVYUZ4vj8Rw0YDaLgIOhA+8vZU+5oNBkBRmWwUXT/24iCqZtwo\n\tmeWmPAjS872DlfKf6FXPLMeVmW0jO74QUsBJTKpdRIUfbXzU05B+NqRstS9OtSbP\n\tq2c2mOlDVYYZZa0ZKg7Cw=="],"X-ME-Sender":"<xms:muSpaaGpc3ir2qQN5pptJA9w8DvKHlv2U5qfmAqC7E-JICSC4CHDjg>\n\t<xme:muSpaWKzoA1wehSGCmxgmxJL4coYB4wfD0OIOBsI4QUdjlEgJnHf9NxNCBNreUVlh\n\tYmSgGRatJWBBwe4hpCMHi4EOEVKvvtc6DuXiv5R0eeXYuhN8eujTlU>","X-ME-Proxy-Cause":"gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvieejfedvucetufdoteggodetrf\n\tdotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu\n\trghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf\n\tgurhepoffhfffugggtgffkvfevofgjfhesthejredtredtjeenucfhrhhomheplfgrvhhi\n\tvghrucfvihgruceofhhlohhsshesjhgvthhmrdhmvgeqnecuggftrfgrthhtvghrnhepvd\n\tdtjeeiheeijedtveeujeevvdekjeeuveekleeijeekfeejhfefveeiffffvdeinecuvehl\n\tuhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepfhhlohhsshesjh\n\tgvthhmrdhmvgdpnhgspghrtghpthhtohepfedpmhhouggvpehsmhhtphhouhhtpdhrtghp\n\tthhtohepfhhlohhsshesjhgvthhmrdhmvgdprhgtphhtthhopehlihgstggrmhgvrhgrqd\n\tguvghvvghlsehlihhsthhsrdhlihgstggrmhgvrhgrrdhorhhgpdhrtghpthhtohepmhii\n\trghmrgiirghlsehrvgguhhgrthdrtghomh","X-ME-Proxy":"<xmx:muSpaTDKE7dibEJpzSiS1jx7kSyx8splCl0bABrbkXzdGkRb3lec-Q>\n\t<xmx:muSpaRS6HqiwUgNkYWCZTolbxJ0LfzwyZnRynhrANogJtwUMEqNdSQ>\n\t<xmx:muSpaRquFH0Q4i7XPDgZa8j8nRKe0gwwvxCKQGxdCxb4Kfaa8fctzA>\n\t<xmx:muSpaTz-wuzzFijOQAS1G2I6SUS6NK3n_z2YI28l1YX-C-Z1oNqmNg>\n\t<xmx:muSpafGGfvEdNKcEruBd9so5oxGDX9_Gl2bavz3Td5s7uD1Lrbyig5-L>","Feedback-ID":"i9dde48b3:Fastmail","X-Mailer":["MessagingEngine.com Webmail Interface","b4 0.14.3"],"From":"Javier Tia <floss@jetm.me>","Date":"Thu, 05 Mar 2026 14:10:38 -0600","Subject":"[PATCH v3 2/3] libcamera: software_isp: Normalize statistics sums\n\tto 8-bit","MIME-Version":"1.0","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"7bit","Message-Id":"<20260305-agc-proportional-v3-2-25abc1bfacca@jetm.me>","To":"libcamera-devel@lists.libcamera.org","Cc":"Javier Tia <floss@jetm.me>, Milan Zamazal <mzamazal@redhat.com>","X-Developer-Signature":"v=1; a=openpgp-sha256; l=3160; i=floss@jetm.me;\n\th=from:subject:message-id;\n\tbh=HsgWWVAXwOLxqb3uUWqLxv+Ul62ExD/zovCwpEj+wLw=; \n\tb=owEB7QES/pANAwAKAbXuwwuoZ3cfAcsmYgBpqeM8tDwRJaA9ALl2+h5jBF0ZY5tJoAILEdFLW\n\t/MEuaOFkGCJAbMEAAEKAB0WIQSbE7ILzw7eI0VKk8m17sMLqGd3HwUCaanjPAAKCRC17sMLqGd3\n\tH/EGC/9FuyP1/PMql7WXSHj1vHl2nC82Vc0PzOlVUFR+aegslcwblSw2lu6xgY3TngpZtMh5umE\n\tCFvnsPmL/P2sVLLRGGPyLogCr9NnXxZDtx70hHAejDPOl49SI8gqmZECjoE5jHo3xZTSoOhPAjs\n\t9bNPy2aeM4kulC+oeT/yjP9qfE7oeIjd6/wGPoZ+WKAnIVRZSGa0aYVqeSwLfSXg6IzICitNht9\n\tJhaqa6260HYakDNiuMGx+6D3Wmm1jz7xPioyYLD5KeM1CY5dDe48AWo3ffuHn4NVRSpJM3jGukw\n\t5YyfIA0lGxNqAm6wV54cJq9T9RTWHathjN3VydSggY9HHe6OJGU0aOQGPLkErrDd3OSQ4VfbpQH\n\tHAtPAO3gEdRdqIjsG1rHj9Y+lxs/Wm1CLLhhqxiRZISgpjQTr13NhfPDZeSqZ3NhEVvqWIzgo7g\n\tukCO55x+PVEeYyyORs3N1dIwWKVIeRynz4wjUuYKx3GQS2Pmubv7NfLSrwXPl58WPlVxo=","X-Developer-Key":"i=floss@jetm.me; a=openpgp;\n\tfpr=9B13B20BCF0EDE23454A93C9B5EEC30BA867771F","In-Reply-To":"<20260305-agc-proportional-v3-0-25abc1bfacca@jetm.me>","References":"<20260305-agc-proportional-v3-0-25abc1bfacca@jetm.me>","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 SWSTATS_ACCUMULATE_LINE_STATS() macro divides the luminance value\nfor the histogram to normalize it to 8-bit range, but does not apply\nthe same normalization to the RGB sums. For 10-bit and 12-bit unpacked\nBayer formats this means the sums are accumulated at native bit depth\n(0-1023 or 0-4095 per pixel) while the AWB algorithm subtracts an\n8-bit black level from them, under-correcting by 4x or 16x\nrespectively.\n\nThis mismatch between the AWB's gain calculation (using incorrectly\nBLC-subtracted sums) and the debayer's correct normalized BLC\nsubtraction produces a visible color cast. For example, with the\nOV2740 sensor (10-bit, BLC=16), the under-subtraction skews R/G gain\nby ~9%.\n\nFix this by right-shifting the RGB sums in finishFrame() to normalize\nthem to 8-bit scale, matching the histogram and the 8-bit black level\nused by AWB. A per-format sumShift_ value is set in configure():\n0 for 8-bit and CSI-2 packed formats (already 8-bit), 2 for 10-bit,\nand 4 for 12-bit unpacked formats.\n\nSigned-off-by: Javier Tia <floss@jetm.me>\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\nTested-by: Milan Zamazal <mzamazal@redhat.com>\n---\n include/libcamera/internal/software_isp/swstats_cpu.h | 1 +\n src/libcamera/software_isp/swstats_cpu.cpp            | 9 +++++++++\n 2 files changed, 10 insertions(+)","diff":"diff --git a/include/libcamera/internal/software_isp/swstats_cpu.h b/include/libcamera/internal/software_isp/swstats_cpu.h\nindex 64b3e23f..37bfde53 100644\n--- a/include/libcamera/internal/software_isp/swstats_cpu.h\n+++ b/include/libcamera/internal/software_isp/swstats_cpu.h\n@@ -115,6 +115,7 @@ private:\n \n \tunsigned int xShift_;\n \tunsigned int stride_;\n+\tunsigned int sumShift_;\n \n \tSharedMemObject<SwIspStats> sharedStats_;\n \tSwIspStats stats_;\ndiff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp\nindex 1cedcfbc..6f834207 100644\n--- a/src/libcamera/software_isp/swstats_cpu.cpp\n+++ b/src/libcamera/software_isp/swstats_cpu.cpp\n@@ -346,6 +346,11 @@ void SwStatsCpu::startFrame(uint32_t frame)\n void SwStatsCpu::finishFrame(uint32_t frame, uint32_t bufferId)\n {\n \tstats_.valid = frame % kStatPerNumFrames == 0;\n+\tif (stats_.valid && sumShift_) {\n+\t\tstats_.sum_.r() >>= sumShift_;\n+\t\tstats_.sum_.g() >>= sumShift_;\n+\t\tstats_.sum_.b() >>= sumShift_;\n+\t}\n \t*sharedStats_ = stats_;\n \tstatsReady.emit(frame, bufferId);\n }\n@@ -405,12 +410,15 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg)\n \t\tswitch (bayerFormat.bitDepth) {\n \t\tcase 8:\n \t\t\tstats0_ = &SwStatsCpu::statsBGGR8Line0;\n+\t\t\tsumShift_ = 0;\n \t\t\treturn 0;\n \t\tcase 10:\n \t\t\tstats0_ = &SwStatsCpu::statsBGGR10Line0;\n+\t\t\tsumShift_ = 2;\n \t\t\treturn 0;\n \t\tcase 12:\n \t\t\tstats0_ = &SwStatsCpu::statsBGGR12Line0;\n+\t\t\tsumShift_ = 4;\n \t\t\treturn 0;\n \t\t}\n \t}\n@@ -422,6 +430,7 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg)\n \t\t/* Skip every 3th and 4th line, sample every other 2x2 block */\n \t\tySkipMask_ = 0x02;\n \t\txShift_ = 0;\n+\t\tsumShift_ = 0;\n \t\tprocessFrame_ = &SwStatsCpu::processBayerFrame2;\n \n \t\tswitch (bayerFormat.order) {\n","prefixes":["v3","2/3"]}