From patchwork Wed May 6 23:06:45 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: devve X-Patchwork-Id: 26656 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E237EC32FB for ; Wed, 6 May 2026 23:07:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 635116302F; Thu, 7 May 2026 01:07:02 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="YpC9thUs"; dkim-atps=neutral Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 33BC363025 for ; Thu, 7 May 2026 01:06:53 +0200 (CEST) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-488a8ca4aadso2087445e9.3 for ; Wed, 06 May 2026 16:06:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778108813; x=1778713613; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=ANVPg7VR/jgoiN08fPKUsvHJnWWyqQ90OtjRAf9XFxo=; b=YpC9thUsGro8sLSmKRWLMYPstbZmHs1D2nJ6VZNjNn07Mwr3Px28b2lVzjYQ5OpaBJ JHmR7Ins111cX2NFDhav5MiktIhNFAoaNKc2ZDkbw2+NGH3rkmolaBdOj0oPFVLDQ7R4 37z10eYhFpLqGpQPdqKTNyDY0tiP9ebUTsOPmXYPtPyM0456ztjxsvCsPRr9q6rnBRuM KU+xTjmxaia+YAClXzioXhwpsSirycK5kh/axwZNyW9wXMoV+yURhmL63yxEjCAUBUTk Gj3R5cvojsiDUoZDwCijUv4+3YM8lIgnJzmN3AlX1uGZfAS8MxruqlI24QEjImRTzvNC i9Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778108813; x=1778713613; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ANVPg7VR/jgoiN08fPKUsvHJnWWyqQ90OtjRAf9XFxo=; b=PsiHexaxQ/gHGyTPeF7pq0Em41BlEB2Zza31MAYpJ05llBhlESm0fp+gID08CfJL1r 3QaPyKFn46dEc4UCIcrLTRkKhQCkLnT7hhU4RC4DL4scqrP03cECymp3CwLfW+gPFDVs w2BEz8VlZbvVdtQqgFfXhJWOYTIyYdOvPpIrvP+y1/b/AT62PzNu/cmDSyNbuJ8yoSj1 2Nw8LJu/waluS10nxZSFMZiq6U86sEvH9pFLL8v8Z/NjZPQ9J+mMBiY72J3cm3WGNeRC O27ynpc2+U+/9EusqvvaG0aCpy7fSRl1IZaklcoGAYzY5OmnJv7BdU9kLNnQqnnOJL9P npUw== X-Gm-Message-State: AOJu0YyJy3yHcjc90ryIwLwrYQb1kO2IMIz4NeuypCJzkX+RbRkSW8tH HxE5U8fVBD3aeuf2mJWqgy3X8A3Myu8aOiMSqAjwFZZvTi/rU4n1qpM6hyYwfQ== X-Gm-Gg: AeBDiesi4gcZAnzHRJYIXzcS0RlewMx/goLbFH95TDaOreCJ7d2yrgF6hRgtpQp8OwA Iag+IxpBlLDwcUugOqMMaJILphhaYa1cyTfqInJgKDZv65LtalwOtlKxOfG5ZjPmRODAZZ358Rh Xt5cChnC1rVhFKxY7WwA/m0IgJ0qiicYM0xySx47g9ureFWtBcUVjcE8L3fMbHrMFWHLbvHeq+s TtkMEk4I5POLTQQpmw59KY312xGzpWhYlr5jF9Dd0gbJGEaud3NWEN33Sd34093sluMJ72OkfxT tCgAFRb51BEqyWT5/Yry/QSILtLBcA3xYuHkgpx5Da/uHwmXloqm63v6c3tQ30CIsYzvVseXnys x6mp378cWjCyiwAXelMHBQ03H9QRNIZVMMsvLzJU6TItwTCfUA/ThezqnGlUEVgjm3z7WPqJJjX jn+kVmPkpGaT6mVT9mM3hscf7uhivehVTdyUmL7xXYlkl9KeDQwPrrb4tzrCnXI+ZbEYaIPsnAQ 9Dpq3x4zcXCxb8afpXCS/nd5KLuFAsa X-Received: by 2002:a05:600c:c094:b0:487:5c0:671f with SMTP id 5b1f17b1804b1-48e51e1a7c4mr66305185e9.9.1778108812642; Wed, 06 May 2026 16:06:52 -0700 (PDT) Received: from dexps.speedport.ip (p200300eda74453cf3cf3f7929e513b94.dip0.t-ipconnect.de. [2003:ed:a744:53cf:3cf3:f792:9e51:3b94]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e538a547bsm85951935e9.5.2026.05.06.16.06.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 16:06:52 -0700 (PDT) From: d3vv3 To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2 06/10] libcamera: software_isp: Normalize statistics sums to 8-bit Date: Thu, 7 May 2026 01:06:45 +0200 Message-ID: <20260506230649.1040794-7-devve.3@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506230649.1040794-1-devve.3@gmail.com> References: <20260506230649.1040794-1-devve.3@gmail.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Javier Tia The SWSTATS_ACCUMULATE_LINE_STATS() macro divides the luminance value for the histogram to normalize it to 8-bit range, but does not apply the same normalization to the RGB sums. For 10-bit and 12-bit unpacked Bayer formats this means the sums are accumulated at native bit depth (0-1023 or 0-4095 per pixel) while the AWB algorithm subtracts an 8-bit black level from them, under-correcting by 4x or 16x respectively. Fix this by right-shifting the RGB sums in finishFrame() to normalize them to 8-bit scale, matching the histogram and the 8-bit black level used by AWB. A per-format sumShift_ value is set in configure(): 0 for 8-bit and CSI-2 packed formats (already 8-bit), 2 for 10-bit, and 4 for 12-bit unpacked formats. Signed-off-by: Javier Tia Reviewed-by: Milan Zamazal Tested-by: Milan Zamazal Reviewed-by: Barnabás Pőcze Tested-by: Barnabás Pőcze Signed-off-by: d3vv3 --- include/libcamera/internal/software_isp/swstats_cpu.h | 1 + src/libcamera/software_isp/swstats_cpu.cpp | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/include/libcamera/internal/software_isp/swstats_cpu.h b/include/libcamera/internal/software_isp/swstats_cpu.h index 802370bd..2dac6945 100644 --- a/include/libcamera/internal/software_isp/swstats_cpu.h +++ b/include/libcamera/internal/software_isp/swstats_cpu.h @@ -116,6 +116,7 @@ private: unsigned int xShift_; unsigned int stride_; + unsigned int sumShift_; std::vector stats_; SharedMemObject sharedStats_; diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp index 5366e019..2ed906e1 100644 --- a/src/libcamera/software_isp/swstats_cpu.cpp +++ b/src/libcamera/software_isp/swstats_cpu.cpp @@ -362,6 +362,11 @@ void SwStatsCpu::finishFrame(uint32_t frame, uint32_t bufferId) for (unsigned int j = 0; j < SwIspStats::kYHistogramSize; j++) sharedStats_->yHistogram[j] += s.yHistogram[j]; } + if (sumShift_) { + sharedStats_->sum_.r() >>= sumShift_; + sharedStats_->sum_.g() >>= sumShift_; + sharedStats_->sum_.b() >>= sumShift_; + } } sharedStats_->valid = valid; @@ -422,6 +427,7 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg, unsigned int stat if (bayerFormat.packing == BayerFormat::Packing::None && setupStandardBayerOrder(bayerFormat.order) == 0) { processFrame_ = &SwStatsCpu::processBayerFrame2; + sumShift_ = bayerFormat.bitDepth - 8; switch (bayerFormat.bitDepth) { case 8: stats0_ = &SwStatsCpu::statsBGGR8Line0; @@ -442,6 +448,7 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg, unsigned int stat /* Skip every 3th and 4th line, sample every other 2x2 block */ ySkipMask_ = 0x02; xShift_ = 0; + sumShift_ = 0; processFrame_ = &SwStatsCpu::processBayerFrame2; switch (bayerFormat.order) {