From patchwork Thu Nov 27 02:22:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bryan O'Donoghue X-Patchwork-Id: 25222 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 28006C3260 for ; Thu, 27 Nov 2025 02:23:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BFB3760AB3; Thu, 27 Nov 2025 03:23:24 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="u73BjTwB"; 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 DDDDC609DE for ; Thu, 27 Nov 2025 03:23:21 +0100 (CET) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-477b198f4bcso1688635e9.3 for ; Wed, 26 Nov 2025 18:23:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1764210201; x=1764815001; darn=lists.libcamera.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=A9hMkgjtaaGL70LDUYBXAqBqbvWKiYH7aLBkdndlC9Q=; b=u73BjTwBbiOSpQUpDRTBccAZ11l3PbZa7ni3GR8RefCHevzETztXw/Int2FDsdrbiG DmIF//nTBBS1+d4FtmKeb8h/5UJsskW9JRPoyJWG5KTtnWlgzWxStKs1MKRZJos0wXap dGP+yhVPCnAsqi/Buj1n8ikDZNUbzMWgjpcVrzQsq7VC16b2PAToFe++bpPQvZ+Li5wo 2RiwpXdMLirhbLm2SUElOBM7S7D2G5BqDiexsaRbYtxEzTf2SNB8y/G1UA5x3YxnpmoW J2BIatzCw8+ulyQBqoENigyBIRb6h8SwqyQPHr+YBknuq4AfUxVS0fQlYzQPjpLm6PQt hJNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764210201; x=1764815001; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=A9hMkgjtaaGL70LDUYBXAqBqbvWKiYH7aLBkdndlC9Q=; b=Pga976cad6RY6PFqUeR35RLJe4XEWeUm1iLUbD70MwfRyQYdphwIH83nvBgUt728mJ 2BC1Wz43WnU/yjzgpF3HeKBtzTBwIIzJsLtN6mLxbLi/jpBuEQjQX7WrlOpq0YIFNPKd qGl249J5IHE6Ia26S/BRtgmLrA4bY9Ob6eqonHkD0NFDH3F4rMdc40ILyCOYFc4oBK80 veLQsIBxW3STeatmapfDWfB83VJgtbTOlq1WP5Q1IU++kvuRCOrrmJrnPCEA3tJcMeJZ OOg0sYVX2YIUbs9/Fap0fT+EJ+ajbXuUbLWRszLMic3eSX97JuMHw0RtJkqsWPwL95Y0 kndQ== X-Gm-Message-State: AOJu0YxOHbW6pXwRj9vmwR9EgWYk9Zi2UjTo6hdcP76NkpYk5q2x9yLR 8DxeGU2GJHunBpYX2QG9gurMLp3sRsP4g6iumsxUUGw/LDfcFvDudXtBN4siGqOB5pg1mAZfnEu fLS3+JcA= X-Gm-Gg: ASbGnctzHd4W9ymudStcrQ0xAF9yYlGgwAUcXqbh52zdbT+ARmByECGkO3E3klyE30D OGnbNNNzi3mYHUiIB+Wkv6pXxGfkCJMnK24ukMdFxaa5RNBjkK8O0nU7NL/NGtgrCtvdB/aQWCV d+/6XIjMSG6rBl+nmF1MV0YopxPIVITcACjvfBaBdL1+l1/10fP0SwboY259ocLN5+67q+QC8Cx 72Xr9N0XWwNv9qXha7yK+rMHT+RoWtyPCb4QrV+ChNVd6COn9KI6KF7E+OolzmN4s+CvcNR4szp pKDgdsu78D0leFKhrNXOXuI5pVlfgXaIxvWtiTzLnAxR7N4+L9sFqVpcZ3bC7NIOXvDoEzLHZjY p46Ie5MnTvqm9EE89QynbT1qBuhG4/kY0RIRF5iEP2KInynkm7MZL2nv8PyECUfd+jDcjehz8kj PVzZq9nq8ll9WXpazvaRTNONrcsQTYkcp+2J2IbOY7YF3hXFZ6Qqv3/WXtRMnp64SLvHs= X-Google-Smtp-Source: AGHT+IElCx5uzIUcxPxm6/XJmlolZJ83ntm3mRPL5g7cgwhhAMYJMmYcLAO08dTIfLU6gXA8y4vhjg== X-Received: by 2002:a05:600c:1c88:b0:477:9cdb:e337 with SMTP id 5b1f17b1804b1-477c0165badmr250069365e9.7.1764210201190; Wed, 26 Nov 2025 18:23:21 -0800 (PST) Received: from inspiron14p-linux.ht.home (188-141-3-146.dynamic.upc.ie. [188.141.3.146]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4790b0e21e8sm71549495e9.15.2025.11.26.18.23.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Nov 2025 18:23:20 -0800 (PST) From: Bryan O'Donoghue To: libcamera-devel@lists.libcamera.org Cc: pavel@ucw.cz, Hans de Goede , Milan Zamazal , Kieran Bingham , Bryan O'Donoghue Subject: [PATCH v2 08/22] libcamera: swstats_cpu: Add processFrame() method Date: Thu, 27 Nov 2025 02:22:40 +0000 Message-ID: <20251127022256.178929-9-bryan.odonoghue@linaro.org> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20251127022256.178929-1-bryan.odonoghue@linaro.org> References: <20251127022256.178929-1-bryan.odonoghue@linaro.org> 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: Hans de Goede Add a method to the SwstatsCpu class to process a whole Framebuffer in one go, rather then line by line. This is useful for gathering stats when debayering is not necessary or is not done on the CPU. Reviewed-by: Milan Zamazal Signed-off-by: Hans de Goede [bod: various rebase splats fixed] [bod: Added constructor Doxygen header] [bod: Squashed a fix from Hans to calculate stats on every 4th frame] Reviewed-by: Kieran Bingham Signed-off-by: Bryan O'Donoghue --- .../internal/software_isp/swstats_cpu.h | 15 +++- src/libcamera/software_isp/software_isp.cpp | 5 +- src/libcamera/software_isp/swstats_cpu.cpp | 69 ++++++++++++++++++- 3 files changed, 84 insertions(+), 5 deletions(-) diff --git a/include/libcamera/internal/software_isp/swstats_cpu.h b/include/libcamera/internal/software_isp/swstats_cpu.h index fae575f85..64b3e23f5 100644 --- a/include/libcamera/internal/software_isp/swstats_cpu.h +++ b/include/libcamera/internal/software_isp/swstats_cpu.h @@ -18,18 +18,23 @@ #include #include "libcamera/internal/bayer_format.h" +#include "libcamera/internal/framebuffer.h" +#include "libcamera/internal/global_configuration.h" #include "libcamera/internal/shared_mem_object.h" #include "libcamera/internal/software_isp/swisp_stats.h" +#include "benchmark.h" + namespace libcamera { class PixelFormat; +class MappedFrameBuffer; struct StreamConfiguration; class SwStatsCpu { public: - SwStatsCpu(); + SwStatsCpu(const GlobalConfiguration &configuration); ~SwStatsCpu() = default; /* @@ -50,6 +55,7 @@ public: void setWindow(const Rectangle &window); void startFrame(uint32_t frame); void finishFrame(uint32_t frame, uint32_t bufferId); + void processFrame(uint32_t frame, uint32_t bufferId, FrameBuffer *input); void processLine0(uint32_t frame, unsigned int y, const uint8_t *src[]) { @@ -79,6 +85,7 @@ public: private: using statsProcessFn = void (SwStatsCpu::*)(const uint8_t *src[]); + using processFrameFn = void (SwStatsCpu::*)(MappedFrameBuffer &in); int setupStandardBayerOrder(BayerFormat::Order order); /* Bayer 8 bpp unpacked */ @@ -91,6 +98,10 @@ private: void statsBGGR10PLine0(const uint8_t *src[]); void statsGBRG10PLine0(const uint8_t *src[]); + void processBayerFrame2(MappedFrameBuffer &in); + + processFrameFn processFrame_; + /* Variables set by configure(), used every line */ statsProcessFn stats0_; statsProcessFn stats2_; @@ -103,9 +114,11 @@ private: Size patternSize_; unsigned int xShift_; + unsigned int stride_; SharedMemObject sharedStats_; SwIspStats stats_; + Benchmark bench_; }; } /* namespace libcamera */ diff --git a/src/libcamera/software_isp/software_isp.cpp b/src/libcamera/software_isp/software_isp.cpp index 341c0352c..928a2520c 100644 --- a/src/libcamera/software_isp/software_isp.cpp +++ b/src/libcamera/software_isp/software_isp.cpp @@ -108,14 +108,15 @@ SoftwareIsp::SoftwareIsp(PipelineHandler *pipe, const CameraSensor *sensor, return; } - auto stats = std::make_unique(); + const GlobalConfiguration &configuration = pipe->cameraManager()->_d()->configuration(); + + auto stats = std::make_unique(configuration); if (!stats->isValid()) { LOG(SoftwareIsp, Error) << "Failed to create SwStatsCpu object"; return; } stats->statsReady.connect(this, &SoftwareIsp::statsReady); - const GlobalConfiguration &configuration = pipe->cameraManager()->_d()->configuration(); debayer_ = std::make_unique(std::move(stats), configuration); debayer_->inputBufferReady.connect(this, &SoftwareIsp::inputReady); debayer_->outputBufferReady.connect(this, &SoftwareIsp::outputReady); diff --git a/src/libcamera/software_isp/swstats_cpu.cpp b/src/libcamera/software_isp/swstats_cpu.cpp index 55e764b0a..c931edb41 100644 --- a/src/libcamera/software_isp/swstats_cpu.cpp +++ b/src/libcamera/software_isp/swstats_cpu.cpp @@ -16,6 +16,7 @@ #include #include "libcamera/internal/bayer_format.h" +#include "libcamera/internal/mapped_framebuffer.h" namespace libcamera { @@ -34,6 +35,15 @@ namespace libcamera { * instead of processing the whole frame. */ +/** + * \fn SwStatsCpu::SwStatsCpu(const GlobalConfiguration &configuration) + * \brief Construct a SwStatsCpu object + * \param[in] configuration Global configuration reference + * + * Creates a SwStatsCpu object and initialises shared memory for statistics + * exchange. + */ + /** * \fn bool SwStatsCpu::isValid() const * \brief Gets whether the statistics object is valid @@ -144,8 +154,8 @@ namespace libcamera { LOG_DEFINE_CATEGORY(SwStatsCpu) -SwStatsCpu::SwStatsCpu() - : sharedStats_("softIsp_stats") +SwStatsCpu::SwStatsCpu(const GlobalConfiguration &configuration) + : sharedStats_("softIsp_stats"), bench_(configuration) { if (!sharedStats_) LOG(SwStatsCpu, Error) @@ -386,11 +396,14 @@ int SwStatsCpu::setupStandardBayerOrder(BayerFormat::Order order) */ int SwStatsCpu::configure(const StreamConfiguration &inputCfg) { + stride_ = inputCfg.stride; + BayerFormat bayerFormat = BayerFormat::fromPixelFormat(inputCfg.pixelFormat); if (bayerFormat.packing == BayerFormat::Packing::None && setupStandardBayerOrder(bayerFormat.order) == 0) { + processFrame_ = &SwStatsCpu::processBayerFrame2; switch (bayerFormat.bitDepth) { case 8: stats0_ = &SwStatsCpu::statsBGGR8Line0; @@ -411,6 +424,7 @@ int SwStatsCpu::configure(const StreamConfiguration &inputCfg) /* Skip every 3th and 4th line, sample every other 2x2 block */ ySkipMask_ = 0x02; xShift_ = 0; + processFrame_ = &SwStatsCpu::processBayerFrame2; switch (bayerFormat.order) { case BayerFormat::BGGR: @@ -475,4 +489,55 @@ void SwStatsCpu::setWindow(const Rectangle &window) window_.height &= ~(patternSize_.height - 1); } +void SwStatsCpu::processBayerFrame2(MappedFrameBuffer &in) +{ + const uint8_t *src = in.planes()[0].data(); + const uint8_t *linePointers[3]; + + /* Adjust src for starting at window_.y */ + src += window_.y * stride_; + + for (unsigned int y = 0; y < window_.height; y += 2) { + if (y & ySkipMask_) { + src += stride_ * 2; + continue; + } + + /* linePointers[0] is not used by any stats0_ functions */ + linePointers[1] = src; + linePointers[2] = src + stride_; + (this->*stats0_)(linePointers); + src += stride_ * 2; + } +} + +/** + * \brief Calculate statistics for a frame in one go + * \param[in] frame The frame number + * \param[in] bufferId ID of the statistics buffer + * \param[in] input The frame to process + * + * This may only be called after a successful setWindow() call. + */ +void SwStatsCpu::processFrame(uint32_t frame, uint32_t bufferId, FrameBuffer *input) +{ + if (frame % kStatPerNumFrames) { + finishFrame(frame, bufferId); + return; + } + + bench_.startFrame(); + startFrame(frame); + + MappedFrameBuffer in(input, MappedFrameBuffer::MapFlag::Read); + if (!in.isValid()) { + LOG(SwStatsCpu, Error) << "mmap-ing buffer(s) failed"; + return; + } + + (this->*processFrame_)(in); + finishFrame(frame, bufferId); + bench_.finishFrame(); +} + } /* namespace libcamera */