From patchwork Wed May 6 23:07:18 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: devve X-Patchwork-Id: 26663 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 16D69C3303 for ; Wed, 6 May 2026 23:07:38 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CAF1A630BB; Thu, 7 May 2026 01:07:37 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="LCoBA+YH"; dkim-atps=neutral Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id AC7D16303C for ; Thu, 7 May 2026 01:07:33 +0200 (CEST) Received: by mail-qk1-x72f.google.com with SMTP id af79cd13be357-8d6d5e45c43so26538485a.3 for ; Wed, 06 May 2026 16:07:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778108853; x=1778713653; 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=LCoBA+YH38KGgrhMeYE0Gv0ax5e0GYrslyQTqphthDwtB922cUGqJtC5LlM6T3Kq3E 4Cw895OhdaxO3Gm5J6ABJKXcdvKBl2UDN/rP/GKFzIgYBpPmE7ENwChpaugA34LsDPkn os+NVxM7E31b55t101sOZ2jeb68yiDSY07D3sFChpvXkIcrvsRzrAcv+YAk50IZ65DT3 0o64sZqcrSpUwQyb5jGj0UvOBZZfJPqbJFXTSN7IHxl5ChAAR/GQXn4RPQmf9ORsBm4X MqEgT0D6dx3h3askibqpOcKKsJTi5XP1jYXiXPtGXEW0FCZxodpyg8R7naQ7odTUH1w8 X9hQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778108853; x=1778713653; 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=FPCTe/0ovqki5IvHE0oin0v9bd1Glrp4p5IFTARZYHNEQr7yKMJMHi09AuwO6hrdt8 hT40wGUV/At57HI01YkCcxJYxDXZ2GfXjoufJLdnCgKPeCjAL20dnGAQy6ODMaZU1aeV nZ/EbeuUSSoq5VussOBM3YnDwavKU/fXCtsCsFfhHAa7/J0th7xOQkl+zdB0EIPEjYck XTFFFU9wNyK9Gcer/gsBAXaG86D50Lv+uLMihH36BjAIIakixjXflRkedX4ohU/ytrsm iR4Cy1R7YzjLW0js0iESHAeO/P3NvSkUuFnU00dZjVLuZ7P9zYPFmu3RgbtmF/4U9IeM pQnw== X-Gm-Message-State: AOJu0Yz+UCriO52Z5K5V248Z1UOoM5sVa1UbR5TA6XL/F6UjquFPXykh 8G01b6A5PgS0qyS2QUjNrbZ0Ifu9hiJbpwTmDWU2yV1MY+DqLYjMjFoSmWWeEw== X-Gm-Gg: AeBDieuFcP3iaA5bC9d97ydgFjytMmL3HhHnI9mntWdMnadrsrW/esIjAjRT/bY6jVw iJHys/Lf/LVEtlBdPAstpMBnTJZAee4Le4dywb24In1daWH7wBwjrTBXPBDaYKrpUoY2DxC1VgM lYx8S328kP1p5AtehRkOkoIUu7DTM0vwIdScc2VS/44hKojqQt96eu6o68KfXq3R7+RCL8Teguw N13JUSNSF8VTlQuXq/YW+FEbHI1dOBRb7TktyWc0ONmgSSYoecqXwsv5iETewkPZtw/qSmNF7T5 aPO/PFcdYEXfj8dW30KNgp1d/7WTyKMIr88mrr/Gi+kqi5Ys2SHB50GEOyrFP6bTp1i9l8jI/8m AJSExotYiuaa8X3m+6jBsaDdaaEIu+kQU56rlhpNTUXPnPPSwb1co6Pr9hePWhut3q4OhrYev1/ eP2Kvua3Rmj7zWoxS1ZE4SPNJp7wsyyG/+Fd8z++Dkkv8teJca9RhyLEtmJyiLQWZgPXd/95XUo pYgEV7WuDM3adtwTaO8Pp5c/eNHo0my X-Received: by 2002:a05:620a:408e:b0:8ee:30e:cb97 with SMTP id af79cd13be357-904d4a61c65mr785228685a.21.1778108852492; Wed, 06 May 2026 16:07:32 -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 af79cd13be357-8fc2c25324esm2035266385a.23.2026.05.06.16.07.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 16:07:32 -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:07:18 +0200 Message-ID: <20260506230722.1041596-7-devve.3@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506230722.1041596-1-devve.3@gmail.com> References: <20260506230722.1041596-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) {