From patchwork Sun Nov 29 15:24:12 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alain_Cousini=C3=A9?= X-Patchwork-Id: 29105 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 BD86AC3355 for ; Sun, 29 Nov 2026 15:24:53 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 017AD6AA4E; Sun, 29 Nov 2026 16:24:51 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=laposte.net header.i=@laposte.net header.b="jPg+Nv3Z"; dkim-atps=neutral Received: from smtp-outgoing-1602.laposte.net (smtp-outgoing-1602.laposte.net [160.92.124.97]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9E5ED6AA32 for ; Sun, 29 Nov 2026 16:24:43 +0100 (CET) X-mail-filterd: {"version":"1.9.7", "queueID":"4jcrYP37Zqz10MQ4", "contextId": "6b419b7c-01c4-4ec9-a06c-9bfa3aaf68e4"} Received: from outgoing-mail.laposte.net (localhost.localdomain [127.0.0.1]) by mlpnf0120.laposte.net (SMTP Server) with ESMTP id 4jcrYP37Zqz10MQ4; Sun, 29 Nov 2026 16:24:41 +0100 (CET) X-mail-filterd: {"version":"1.9.7", "queueID":"4jcrYP1Hnzz10MQQ", "contextId": "007f0289-a614-4ff0-a754-eccd4942f6c8"} X-lpn-mailing: LEGIT X-lpn-spamrating: 36 X-lpn-spamlevel: not-spam Received: from debian.home (m-dj.hd.free.fr [82.67.82.126]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mlpnf0120.laposte.net (SMTP Server) with ESMTPSA id 4jcrYP1Hnzz10MQQ; Sun, 29 Nov 2026 16:24:41 +0100 (CET) From: =?utf-8?q?Alain_Cousini=C3=A9?= To: libcamera-devel@lists.libcamera.org, Milan Zamazal Cc: Hans de Goede , Bryan O'Donoghue , =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= , Laurent Pinchart , Jai Luthra , =?utf-8?q?Alain_Cousini=C3=A9?= Subject: [PATCH v8 2/6] libcamera: software_isp: Add support for monochrome formats - Stats Date: Sun, 29 Nov 2026 16:24:12 +0100 Message-ID: <20261129152416.174279-3-alain.cousinie@laposte.net> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261129152416.174279-1-alain.cousinie@laposte.net> References: <20261129152416.174279-1-alain.cousinie@laposte.net> MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=laposte.net; s=lpn-wlmd; t=1795965883; bh=bB7L6kmtNBYcvF5fEhWyUzQVWituEoZZnlr2jwVVFRk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding; b=jPg+Nv3Zby0jfSBrxBA/twdfhlk0EWPBApx1bu/CefdVQCiX4Rt6wQtxLG9jVdOtWReX3XuazfdH5O+/ZIa+k32WFJl+8lXCIbjkT3cbOTB8pCo9/936jLVzi4b1u6MSzMvbvTj/wX/dAYvJuUkRdnbDCvJvhR8jVgHYkEEl+tvr5JOxYCv6XN4xPPJfCcQZ+Vf/L+/1mKS5cSMk6ZiiUVTpYEjyj3p95ZfNbYYyIeJJNwCYUX7GuigeAxDOQJq3x40FNawjyBSBkK2PzWlnnWR1ExbNbBr6ZbEM43qVCKVvLXhsAXzUT2kUGrE6qGLqSrY7ak1g/Eb6DELvzj07ng==; 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 Add support for monochrome formats in SwStatsCpu: one function per format for unpacked R8, R10, R12 and R16, and for CSI-2 packed R10 and R12. The pattern size follows the Bayer formats: 2x2 for unpacked formats, and a width equal to the group of packed pixels for R10P (4 pixels in 5 bytes) and R12P (2 pixels in 3 bytes). The CPU and GPU debayers use the same pattern. One pixel out of four is sampled horizontally on each line, for all formats, to limit the cost. Each sample is weighted by 4 so the totals keep the density of a full sum. Values are brought to the 8-bit scale as for the Bayer statistics, and the same sum feeds R, G and B. The 64-bin histogram uses the most significant bits of the pixel. For packed formats, only the most significant byte of the sampled pixel is read, which is enough for statistics. Bayer statistics visit two lines out of four and one 2x2 block out of two, i.e. a quarter of the pixels. The cost is therefore similar, but no line is skipped for monochrome. This avoids aliasing on scenes with fine structures along the vertical axis (scanning, line patterns). Signed-off-by: Alain CousiniƩ --- .../internal/software_isp/swstats_cpu.h | 13 ++ src/libcamera/software_isp/swstats_cpu.cpp | 184 ++++++++++++++++++ 2 files changed, 197 insertions(+) diff --git a/include/libcamera/internal/software_isp/swstats_cpu.h b/include/libcamera/internal/software_isp/swstats_cpu.h index 551870921..f702cf126 100644 --- a/include/libcamera/internal/software_isp/swstats_cpu.h +++ b/include/libcamera/internal/software_isp/swstats_cpu.h @@ -101,8 +101,21 @@ private: /* Bayer 12 bpp packed */ void statsBGGR12PLine0(const uint8_t *src[], SwIspStats &stats); void statsGBRG12PLine0(const uint8_t *src[], SwIspStats &stats); + /* Mono 8 bpp unpacked */ + void statsMono8Line0(const uint8_t *src[], SwIspStats &stats); + /* Mono 10 bpp unpacked */ + void statsMono10Line0(const uint8_t *src[], SwIspStats &stats); + /* Mono 10 bpp packed */ + void statsMono10PLine0(const uint8_t *src[], SwIspStats &stats); + /* Mono 12 bpp unpacked */ + void statsMono12Line0(const uint8_t *src[], SwIspStats &stats); + /* Mono 12 bpp packed */ + void statsMono12PLine0(const uint8_t *src[], SwIspStats &stats); + /* Mono 16 bpp unpacked */ + void statsMono16Line0(const uint8_t *src[], SwIspStats &stats); void processBayerFrame2(MappedFrameBuffer &in); + void processMonoFrame2(MappedFrameBuffer &in); processFrameFn processFrame_; diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp index 74633815e..852f391de 100644 --- a/src/libcamera/software_isp/swstats_cpu.cpp +++ b/src/libcamera/software_isp/swstats_cpu.cpp @@ -390,6 +390,124 @@ void SwStatsCpu::statsGBRG12PLine0(const uint8_t *src[], SwIspStats &stats) SWSTATS_FINISH_LINE_STATS() } +/* + * Horizontal sub-sampling optimization: evaluates 1 pixel out of 4 (x += 4) + * to reduce CPU overhead. Each sample gets a weight of +4 to preserve global + * frame statistical density required by the Auto Exposure (AE) algorithm. + */ +inline void storeMonoAccumulation(SwIspStats &stats, uint64_t value) +{ + stats.sum_.r() += value; + stats.sum_.g() += value; + stats.sum_.b() += value; +} + +void SwStatsCpu::statsMono8Line0(const uint8_t *src[], SwIspStats &stats) +{ + const uint8_t *cur = src[1] + window_.x; + uint64_t sum = 0; + + for (unsigned int x = 0; x < window_.width; x += 4) { + sum += *cur; + stats.yHistogram[*cur >> 2] += 4; + cur += 4; + } + /* Process 4 pixels per iteration : x << 2 */ + storeMonoAccumulation(stats, sum << 2); +} + +void SwStatsCpu::statsMono10Line0(const uint8_t *src[], SwIspStats &stats) +{ + /* + * Little-Endian optimization: Unpacked 10-bit format stores data + * right-aligned in a 2-byte container. Reading via a 16-bit pointer + * allows clean data access before applying the bit-shift. + */ + const uint16_t *cur = reinterpret_cast(src[1] + window_.x * 2); + uint64_t sum = 0; + + for (unsigned int x = 0; x < window_.width; x += 4) { + uint16_t val = *cur; + sum += val; + stats.yHistogram[(val >> 4) & 0x3f] += 4; + cur += 4; + } + /* Process 4 pixels per iteration : x 10 >> 2 to 8bits and x << 2 */ + storeMonoAccumulation(stats, sum); +} + +void SwStatsCpu::statsMono12Line0(const uint8_t *src[], SwIspStats &stats) +{ + /* + * 12-bit unpacked processing. Data is read via a native 16-bit pointer + * and right-shifted to fit the histogram range. + */ + const uint16_t *cur = reinterpret_cast(src[1] + window_.x * 2); + uint64_t sum = 0; + + for (unsigned int x = 0; x < window_.width; x += 4) { + uint16_t val = *cur; + sum += val; + stats.yHistogram[(val >> 6) & 0x3f] += 4; + cur += 4; + } + /* Process 4 pixels per iteration : 12 >> 4 to 8bits and x << 2 */ + storeMonoAccumulation(stats, sum >> 2); +} + +void SwStatsCpu::statsMono10PLine0(const uint8_t *src[], SwIspStats &stats) +{ + /* + * MIPI CSI-2 10-bit packed: 4 pixels in 5 bytes. Stepping 5 bytes + * evaluates the head MSB pixel of each block as a native 8-bit value. + */ + const uint8_t *cur = src[1] + window_.x * 5 / 4; + uint64_t sum = 0; + + for (unsigned int x = 0; x < window_.width; x += 4) { + uint8_t val = *cur; + sum += val; + stats.yHistogram[val >> 2] += 4; + cur += 5; + } + /* Process 4 pixels per iteration : first byte x << 2 */ + storeMonoAccumulation(stats, sum << 2); +} + +void SwStatsCpu::statsMono12PLine0(const uint8_t *src[], SwIspStats &stats) +{ + /* + * MIPI CSI-2 12-bit packed: 2 pixels in 3 bytes. Stepping 6 bytes + * advances by two full blocks for optimal data alignment. + */ + const uint8_t *cur = src[1] + window_.x * 3 / 2; + uint64_t sum = 0; + + for (unsigned int x = 0; x < window_.width; x += 4) { + uint8_t val = *cur; + sum += val; + stats.yHistogram[val >> 2] += 4; + cur += 6; + } + /* Process 4 pixels per iteration : first byte x << 2 */ + storeMonoAccumulation(stats, sum << 2); +} + +void SwStatsCpu::statsMono16Line0(const uint8_t *src[], SwIspStats &stats) +{ + const uint16_t *cur = reinterpret_cast(src[1] + window_.x * 2); + uint64_t sum = 0; + + for (unsigned int x = 0; x < window_.width; x += 4) { + uint16_t val = *cur; + sum += val; + stats.yHistogram[val >> 10] += 4; + cur += 4; + } + /* Process 4 pixels per iteration : 16 >> 8 to 8bits and x << 2 */ + storeMonoAccumulation(stats, sum >> 6); +} + /** * \brief Reset state to start statistics gathering for a new frame * \param[in] frame The frame number @@ -512,6 +630,53 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg, unsigned int stat uint8_t bitDepth = bayerFormat.bitDepth; + if (bayerFormat.order == BayerFormat::Order::MONO) { + /* + * Bayer statistics visit 2 lines out of 4 and every other 2x2 block + * (a quarter of the pixels). Sample the same share here, one pixel out + * of four on every line, so that no line is skipped. + */ + ySkipMask_ = 0; + xShift_ = 0; + sumShift_ = 0; + processFrame_ = &SwStatsCpu::processMonoFrame2; + + if (bayerFormat.packing == BayerFormat::Packing::None) { + patternSize_.width = 2; + patternSize_.height = 2; + + switch (bitDepth) { + case 8: + stats0_ = &SwStatsCpu::statsMono8Line0; + return 0; + case 10: + stats0_ = &SwStatsCpu::statsMono10Line0; + return 0; + case 12: + stats0_ = &SwStatsCpu::statsMono12Line0; + return 0; + case 16: + stats0_ = &SwStatsCpu::statsMono16Line0; + return 0; + } + } + if (bayerFormat.packing == BayerFormat::Packing::CSI2) { + patternSize_.height = 2; + + switch (bitDepth) { + case 10: + patternSize_.width = 4; + stats0_ = &SwStatsCpu::statsMono10PLine0; + return 0; + case 12: + patternSize_.width = 2; + stats0_ = &SwStatsCpu::statsMono12PLine0; + return 0; + } + } + return -EINVAL; + } + if ((bitDepth == 10 || bitDepth == 12) && bayerFormat.packing == BayerFormat::Packing::CSI2) { if (bitDepth == 10) @@ -612,6 +777,25 @@ void SwStatsCpu::processBayerFrame2(MappedFrameBuffer &in) } } +void SwStatsCpu::processMonoFrame2(MappedFrameBuffer &in) +{ + const uint8_t *src = in.planes()[0].data(); + const uint8_t *linePointers[3]; + + src += window_.y * stride_; + + for (unsigned int y = 0; y < window_.height; y++) { + if (y & ySkipMask_) { + src += stride_; + continue; + } + + linePointers[1] = src; + (this->*stats0_)(linePointers, stats_[0]); + src += stride_; + } +} + /** * \brief Calculate statistics for a frame in one go * \param[in] frame The frame number