From patchwork Wed Dec 10 00:31:25 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: 25404 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 34903BD1F1 for ; Wed, 10 Dec 2025 00:32:21 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DEB796143D; Wed, 10 Dec 2025 01:32:20 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="zf1yMxU7"; dkim-atps=neutral Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E38C06142A for ; Wed, 10 Dec 2025 01:32:18 +0100 (CET) Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-343514c7854so180144a91.1 for ; Tue, 09 Dec 2025 16:32:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1765326737; x=1765931537; 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=nLU/ifYTrqqo3T8wLQWtPEibbZ9LrJLctjntqk+s2D8=; b=zf1yMxU7HGX+0U2oxQM3zFrMb98Ap9UciDan+a/Jv7/Y5XXm/EWPm4x1+YKkTUB19X 93/2KEPeyatYG4tIRK7BUtIuktTO1DuzSqnWRtsNKJknK+pduADGnCIfWXsPhVBCtNzO BIImsLPqM3cwJh4LXIcgP2cMXEAfHWFFjsa5Qi0DwkftjGh2RbNVWj1g57cVFtdKo2Cc /f5Xc9IYKoZJgaFfEa5N1uMzq2wA/e8yodCpW07DoChVc4K4JTPDrF7Ui5JqemlPNTlB nrMeFV0CiHmrd3Jf10L8om/kEZYk62OugD5ySy3/PjycSvF7EGk5FU0DTT8wQlUS1MPK OtiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765326737; x=1765931537; 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=nLU/ifYTrqqo3T8wLQWtPEibbZ9LrJLctjntqk+s2D8=; b=UQAJpsvb/buFRrXW8aO11p+6mwLCpowZQdL/c8EkhL5NWAf0tnXBoClO3pib+mVQDb qz9KtaxukTLqWHbJqCKng9NNDHEUNIFwZ+plVHO36DYhofR3I6NagGAQRhmJAzd6lcFo YwnTD108Ww0ec5ZEGdZj4+/2PDmpLFhp/wzBudVQ+hsVDSUWIMfZ158iuO94OptwwFrR hDd/EKrijnIYZQAaU8N9PNPFHgT5h7ylc7oD1eK3xJDZp722w+fJaThwVMwLLupeQbEU CmVvvk8lW7MGjlvqge7OJxf8jQCaA/QQbjbfwffvqZD1meCyQ9OjnNt7qUadYVeLKkDH ElMA== X-Gm-Message-State: AOJu0YxGfqx00EducNmbUGK8UAjSLkLJj4o6fU+3q7GgoxdxpZZ2jZWx FtQ/g0zEY6iHhWL0NRwooa6kfNczIKVyOgkf80nSi230vwPwPtq3Ctc2qbQC51hlAoMtJ5GWsyz blHWV X-Gm-Gg: AY/fxX5iJSZxsGqpa1zq1OjYPcjh4otVcPKR9f8pS6vni0Nz8basKSgEWXVptgNNmxs o+7twyX3OVQv4Yjv6Tzmlz4xiNobnnjClaEgGfO6mhSHinbmPw+41dDASoOLURD8mjKcMH6EDJA OJJ9RIPozzAcpZh/OSfow5k14ZrMgaOCA7IyQPXCVohwRTQCvBnIv+1bFuiZ1HLkGKrKBeqk3+g UQh8BD9zg0Aqko8JDwcZ8OrNJNFrhdjG1nXyzuuUGvNqkzs8U7/87HVDrfMGoZuPrRGzdvELqJW gtuKrIIADyHZRr+6lKD3HFPPy4BxGYtXWXsL52DJBmqOdELaKgAasynlDa52vhd9Zuurs3pfGN2 FpGHxoG/EbRt4KP/9iqIUrRBejZxqMiBsHReEHTtZ03dfsBYc7C9cXhFJyqjeOUNLPtTOcnaFZr xQsMP8ucUBGrhsLW0posqs4uD1Vs3IMym30fRzsQNE7a/lpHb5hZZ2IECEs0B8G7bB X-Google-Smtp-Source: AGHT+IFKNZiqjjyeqToULgHNenvAI23o6yXjUG+lj6msTTZFOxDQUZjBLP6WLO//3/y62mcxx1ouzg== X-Received: by 2002:a17:90b:1645:b0:343:e692:f8d7 with SMTP id 98e67ed59e1d1-34a728d5782mr521146a91.11.1765326737174; Tue, 09 Dec 2025 16:32:17 -0800 (PST) Received: from inspiron14p-linux (p96936b06.osaknt01.ap.so-net.ne.jp. [150.147.107.6]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34a70427ea6sm566580a91.3.2025.12.09.16.32.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Dec 2025 16:32:16 -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 v4 08/20] libcamera: swstats_cpu: Add processFrame() method Date: Wed, 10 Dec 2025 00:31:25 +0000 Message-ID: <20251210003139.43606-9-bryan.odonoghue@linaro.org> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251210003139.43606-1-bryan.odonoghue@linaro.org> References: <20251210003139.43606-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 */