[{"id":38667,"web_url":"https://patchwork.libcamera.org/comment/38667/","msgid":"<177747637532.45302.7432121546861212968@ping.linuxembedded.co.uk>","date":"2026-04-29T15:26:15","subject":"Re: [PATCH v4 2/3] libcamera: software_isp: Normalize statistics\n\tsums to 8-bit","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Hi Javier,\n\n\nQuoting Javier Tia (2026-03-06 18:46:41)\n> The SWSTATS_ACCUMULATE_LINE_STATS() macro divides the luminance value\n> for the histogram to normalize it to 8-bit range, but does not apply\n> the same normalization to the RGB sums. For 10-bit and 12-bit unpacked\n> Bayer 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\n> 8-bit black level from them, under-correcting by 4x or 16x\n> respectively.\n> \n> This mismatch between the AWB's gain calculation (using incorrectly\n> BLC-subtracted sums) and the debayer's correct normalized BLC\n> subtraction produces a visible color cast. For example, with the\n> OV2740 sensor (10-bit, BLC=16), the under-subtraction skews R/G gain\n> by ~9%.\n> \n> Fix this by right-shifting the RGB sums in finishFrame() to normalize\n> them to 8-bit scale, matching the histogram and the 8-bit black level\n> used by AWB. A per-format sumShift_ value is set in configure():\n> 0 for 8-bit and CSI-2 packed formats (already 8-bit), 2 for 10-bit,\n> and 4 for 12-bit unpacked formats.\n> \n> Signed-off-by: Javier Tia <floss@jetm.me>\n> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>\n> Tested-by: Milan Zamazal <mzamazal@redhat.com>\n> Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> Tested-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> ---\n>  include/libcamera/internal/software_isp/swstats_cpu.h | 1 +\n>  src/libcamera/software_isp/swstats_cpu.cpp            | 7 +++++++\n>  2 files changed, 8 insertions(+)\n> \n> diff --git a/include/libcamera/internal/software_isp/swstats_cpu.h b/include/libcamera/internal/software_isp/swstats_cpu.h\n> index 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>         unsigned int xShift_;\n>         unsigned int stride_;\n> +       unsigned int sumShift_;\n>  \n>         SharedMemObject<SwIspStats> sharedStats_;\n>         SwIspStats stats_;\n> diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp\n> index 1cedcfbc..744a7560 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>         stats_.valid = frame % kStatPerNumFrames == 0;\n> +       if (stats_.valid && sumShift_) {\n> +               stats_.sum_.r() >>= sumShift_;\n> +               stats_.sum_.g() >>= sumShift_;\n> +               stats_.sum_.b() >>= sumShift_;\n> +       }\n\nI was trying to get this merged, but unfortunately something changed\nhere.\n\nCould you take a look and rebase on v0.7.1 please to submit a v5, then I\nthink we should get this series merged.\n\nAlthough I think we could do somethign else for 3/3 - that could be done\nlater and on top.\n\nI'd be interested to know if this is the cure for the flicker - or if\nthe 3/3 patch is actually needed in fact.\n\n--\nKieran\n\n>         *sharedStats_ = stats_;\n>         statsReady.emit(frame, bufferId);\n>  }\n> @@ -402,6 +407,7 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg)\n>         if (bayerFormat.packing == BayerFormat::Packing::None &&\n>             setupStandardBayerOrder(bayerFormat.order) == 0) {\n>                 processFrame_ = &SwStatsCpu::processBayerFrame2;\n> +               sumShift_ = bayerFormat.bitDepth - 8;\n>                 switch (bayerFormat.bitDepth) {\n>                 case 8:\n>                         stats0_ = &SwStatsCpu::statsBGGR8Line0;\n> @@ -422,6 +428,7 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg)\n>                 /* Skip every 3th and 4th line, sample every other 2x2 block */\n>                 ySkipMask_ = 0x02;\n>                 xShift_ = 0;\n> +               sumShift_ = 0;\n>                 processFrame_ = &SwStatsCpu::processBayerFrame2;\n>  \n>                 switch (bayerFormat.order) {\n> \n> -- \n> 2.53.0\n>","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 15728BDCB5\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 29 Apr 2026 15:26:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 009F262FD9;\n\tWed, 29 Apr 2026 17:26:20 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CC64962010\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 29 Apr 2026 17:26:18 +0200 (CEST)","from monstersaurus.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EA41A4BB;\n\tWed, 29 Apr 2026 17:24:34 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"U5F6jnGP\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1777476275;\n\tbh=7dyqujgiqpr1Kzg87SPvyxIMZ9+yAznzwmQ1VEzS4TI=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=U5F6jnGPOnklJP/hw7uHoeI7C4vxGlRe2hkA9d4UGdLRfOWoXX9fPYfL/jnkKUK1R\n\tolEKqxokqYrSK7m/qh0HtmfEP6gWXvnei4mKo613ujZKNCeEMvhBAoaxg5BP0ZyndV\n\tEYtyLOXZfzW7yzbe2tJ//Yl8AIMedXLwhS8VM+Ms=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20260306-agc-proportional-v4-2-e87c7e0d837a@jetm.me>","References":"<20260306-agc-proportional-v4-0-e87c7e0d837a@jetm.me>\n\t<20260306-agc-proportional-v4-2-e87c7e0d837a@jetm.me>","Subject":"Re: [PATCH v4 2/3] libcamera: software_isp: Normalize statistics\n\tsums to 8-bit","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Javier Tia <floss@jetm.me>, Milan Zamazal <mzamazal@redhat.com>,\n\t=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","To":"Javier Tia <floss@jetm.me>, libcamera-devel@lists.libcamera.org","Date":"Wed, 29 Apr 2026 16:26:15 +0100","Message-ID":"<177747637532.45302.7432121546861212968@ping.linuxembedded.co.uk>","User-Agent":"alot/0.9.1","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>"}},{"id":38747,"web_url":"https://patchwork.libcamera.org/comment/38747/","msgid":"<20260506224252.C48E71EA006B@mailuser.phl.internal>","date":"2026-05-06T22:28:12","subject":"Re: [PATCH v4 2/3] libcamera: software_isp: Normalize statistics\n\tsums to 8-bit","submitter":{"id":261,"url":"https://patchwork.libcamera.org/api/people/261/","name":"Javier Tia","email":"floss@jetm.me"},"content":"Hi Kieran,\n\nThanks for the review. Rebased on current master for v5 - the diff\ncontext in finishFrame() changed because of the multi-threading\nrefactor (6e53e72e), which restructured the function to aggregate\nfrom a stats_ vector before applying the shift. No functional\nchanges to the patch itself.\n\nOn 3/3: I tested this on OV2740 (ThinkPad X1 Carbon Gen 10). Patch\n1/3 alone cures the flicker. A 40-frame capture with patches 1/3\nand 2/3 applied shows smooth monotonic brightness convergence from\nframe 4 onwards with no oscillation. Patch 3/3 (OV2740 BLC=16)\nimproves AWB color accuracy by providing the correct black level\nbaseline for channel gain calculation, but it is independent of the\nflicker fix.\n\nIncluding all three in v5 since 3/3 is a genuine calibration\nimprovement regardless.\n\nv5 has been sent:\nhttps://lists.libcamera.org/pipermail/libcamera-devel/2026-May/058560.html\n\nBest,\nJavier","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 AA2E0C32F6\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  6 May 2026 22:42:56 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A4DEB6301A;\n\tThu,  7 May 2026 00:42:55 +0200 (CEST)","from fout-a5-smtp.messagingengine.com\n\t(fout-a5-smtp.messagingengine.com [103.168.172.148])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 95CF262DC4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  7 May 2026 00:42:53 +0200 (CEST)","from phl-compute-02.internal (phl-compute-02.internal\n\t[10.202.2.42])\n\tby mailfout.phl.internal (Postfix) with ESMTP id E154AEC013B;\n\tWed,  6 May 2026 18:42:52 -0400 (EDT)","from phl-imap-07 ([10.202.2.97])\n\tby phl-compute-02.internal (MEProxy); Wed, 06 May 2026 18:42:52 -0400","by mailuser.phl.internal (Postfix, from userid 501)\n\tid C48E71EA006B; Wed,  6 May 2026 18:42:52 -0400 (EDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=jetm.me header.i=@jetm.me header.b=\"faCufQYu\";\n\tdkim=pass (2048-bit key;\n\tunprotected) header.d=messagingengine.com\n\theader.i=@messagingengine.com header.b=\"E2RTiqpV\"; \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=fm2; t=1778107372;\n\tx=1778193772; bh=uxix2Mc8PaorOjsirFmhsWUfthU/oQcxv1DUOzRvDMc=; b=\n\tfaCufQYutKop6AJ+kOMxTuwcp+qo8L2ulLj8VMl/Fd0uPETuTkRELsNo7+hh9j+s\n\ti45cOZ6mbUdw3QjxHpOzRVhZgaSNCD4nSioTso2xMpXm9U2ZuQ/fXlaFAqJKH0jM\n\tw9v7ZgVqC3wpCPGoCEuZXt1zLwS2YANpUgvUY+pt6s52TVyZFV92/MW/Jxw294Hr\n\t0yU6rtD+7bZvO8KvnD9HVyEZPbWEPnXpLgLVexhzaI+62Ywhg14kDpVP9lzQXJup\n\t6ZikBYjHPuQSgZQ7mxL3C7gvPp1mKGaxkCFucwhFLoRg1XYlnmFpYJMmpxUynaji\n\tCx8UBIDNjd16KQaYwAcnow==","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=fm3; t=1778107372; x=\n\t1778193772; bh=uxix2Mc8PaorOjsirFmhsWUfthU/oQcxv1DUOzRvDMc=; b=E\n\t2RTiqpVMBnZ49JvHHM31XiGpprWq+XPiRuGItXa30LGjseEJLlfGluTpF0iyxkQj\n\tZ/3M+Kt8oeVJf23LvhJR2VsVpwJDKp6uiF0nLZ3bCEl4jq1TsIp1L93PmV17ezux\n\thuYrLX/i1B4KqTFhzacVX5OUqobGT5NIQhvrRtm4760rKtdZpayZhorW5CJ4+KlM\n\tWPDZHdaMrnNfWvcYam+Hi4k3zsJSWYzj+1diYusueXjzLd5QHtR/QxhwGUa1brKH\n\teqOBN7jtA80JIa5n73W4WVHd/LsJSWeNDpBNIhm5Vee6hNTa8h0keCHVVT1p2OW3\n\tHpNIK8nClxNh/5rm/jv8Q=="],"X-ME-Sender":"<xms:7MP7aVlOZxRhlyeeqLyDlalZeJLB9CYlPQNgYv7xAMT5IgP2BOh7mg>\n\t<xme:7MP7abol-8N1v1ISj3jiG8lOLNyTtQXJ3nNC1iJ5NRN02zM--1myFiq4KTnYxJjRa\n\tsfxoQPqkZp9e51tIEHY6IHgJ0ZUMj3M-_A-dq8ilwqfIRyL0YknD0k>","X-ME-Proxy-Cause":"gggruggvucftvghtrhhoucdtuddrgeefhedrtddtgddutdehkedvucetufdoteggodetrf\n\tdotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu\n\trghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnegopf\n\thokfffucdluddtmdenucfjughrpefotggggffhvfffufevjghfsehtkedttdertdejnecu\n\thfhrohhmpeflrghvihgvrhcuvfhirgcuoehflhhoshhssehjvghtmhdrmhgvqeenucggtf\n\tfrrghtthgvrhhnpeeijeekvdfgfeeileettdfffedutedvteegvdekleduuedufefhheef\n\tueekveeitdenucffohhmrghinheplhhisggtrghmvghrrgdrohhrghenucevlhhushhtvg\n\thrufhiiigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpehflhhoshhssehjvghtmhdr\n\tmhgvpdhnsggprhgtphhtthhopeegpdhmohguvgepshhmthhpohhuthdprhgtphhtthhope\n\tgsrghrnhgrsggrshdrphhotgiivgesihguvggrshhonhgsohgrrhgurdgtohhmpdhrtghp\n\tthhtohepkhhivghrrghnrdgsihhnghhhrghmsehiuggvrghsohhnsghorghrugdrtghomh\n\tdprhgtphhtthhopehlihgstggrmhgvrhgrqdguvghvvghlsehlihhsthhsrdhlihgstggr\n\tmhgvrhgrrdhorhhgpdhrtghpthhtohepmhiirghmrgiirghlsehrvgguhhgrthdrtghomh","X-ME-Proxy":"<xmx:7MP7afP4Lx4IfOUiUVx-mSy0HnNlWGXh7rzVImizZMmHzRZ4OMu-qg>\n\t<xmx:7MP7aXxfOwe3q97rV2iw3FXdS6x6oObi7jHw4lMu483u4UxF2M5oJg>\n\t<xmx:7MP7aYtFoqdswv4L-ghXG0z2wU632QJ_qaQ-cJT3MWxKW36G3-HEIA>\n\t<xmx:7MP7aQ7MTB-6FNEYyPRpxaxnJHaK9ToGNbO-27B2cbJYguN07zOYjg>\n\t<xmx:7MP7aSbepw2iPCtzgthh5aotPSFaoqKw8P6Eag411Vxa0lIujnywi4Vu>","Feedback-ID":"i9dde48b3:Fastmail","X-Mailer":"MessagingEngine.com Webmail Interface","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","From":"Javier Tia <floss@jetm.me>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Date":"Wed, 06 May 2026 16:28:12 -0600","Subject":"Re: [PATCH v4 2/3] libcamera: software_isp: Normalize statistics\n\tsums to 8-bit","Cc":"libcamera-devel@lists.libcamera.org, Milan Zamazal <mzamazal@redhat.com>,\n\t=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","In-Reply-To":"<177747637532.45302.7432121546861212968@ping.linuxembedded.co.uk>","References":"<20260306-agc-proportional-v4-0-e87c7e0d837a@jetm.me>\n\t<20260306-agc-proportional-v4-2-e87c7e0d837a@jetm.me>\n\t<177747637532.45302.7432121546861212968@ping.linuxembedded.co.uk>","Message-Id":"<20260506224252.C48E71EA006B@mailuser.phl.internal>","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>"}}]