From patchwork Sun Nov 29 15:24:16 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: 29107 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 BEC9FC3355 for ; Sun, 29 Nov 2026 15:24:56 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1D2446AA3D; Sun, 29 Nov 2026 16:24:56 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=laposte.net header.i=@laposte.net header.b="QAPA2yaM"; dkim-atps=neutral Received: from smtp-outgoing-1803.laposte.net (smtp-outgoing-1803.laposte.net [160.92.124.104]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id F24606AA38 for ; Sun, 29 Nov 2026 16:24:43 +0100 (CET) X-mail-filterd: {"version":"1.9.7", "queueID":"4jcrYQ5CPkz10MQ4", "contextId": "a36f143c-217d-4b5e-9316-192a4aba5ee2"} Received: from outgoing-mail.laposte.net (localhost.localdomain [127.0.0.1]) by mlpnf0120.laposte.net (SMTP Server) with ESMTP id 4jcrYQ5CPkz10MQ4; Sun, 29 Nov 2026 16:24:42 +0100 (CET) X-mail-filterd: {"version":"1.9.7", "queueID":"4jcrYQ2gHNz10MQQ", "contextId": "46d3f9da-21cc-493e-8957-ab85c5707b4c"} 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 4jcrYQ2gHNz10MQQ; Sun, 29 Nov 2026 16:24:42 +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 6/6] libcamera: software_isp: Add support for monochrome formats - TEST NO PROD Date: Sun, 29 Nov 2026 16:24:16 +0100 Message-ID: <20261129152416.174279-7-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=6P3eBAqe/cT/Fodq5LLY+cfWo8ZnfxURYTx4Jqrr0y4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding; b=QAPA2yaM95DYXzuvQ4u9fuBUFEEtYMH7owAiw9asF84xeetsKfFWJEHVMKD/cPu6uezSEQ8RG8blnN14aaL3Pa7tORr+5gXHUHX+IUkA5GMXeAksJeP99xZTnxS2dS5H3ZzB3QXWnK8ChQ3AkkzdwCFN/osBlXPVONX01P3uG1f31E179gmW3sSOvoCH7X6BPYc+FXHju8UhYWwm+EFWybUFaUK53TXEfad7m8EOOCad0YRbNwZ4WJiWeIRuJTFM0NfTU+0coMg0L8CQ9Zywo9o6NykuUc1QYKm/E1IoD/ju78Tyb1Iq3uBKWWHfigNnY90X0Y3ax3UtdycpFj6CFA==; 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 TEST PATCH ONLY, not meant to be merged. Convert a real unpacked R10 monochrome stream on the fly to the format selected by LIBCAMERA_SOFTISP_SIM_MONO (RAW8, RAW10, RAW10P, RAW12, RAW12P or RAW16), to exercise each layout without a matching sensor. The hooks in SoftwareIsp are only set up for monochrome input and do nothing for Bayer formats or when the variable is not set. The simple pipeline handler is modified because the packed format does not work with my og0va1b sensor (640x480) on Intel IPU6 ISYS. This change is therefore only a local workaround, to be able to test with my hardware: it prefers unpacked formats for monochrome sensors only and is not meant to be merged. The Bayer selection is not modified. Tests performed: - Hardware: og0va1b IR camera, 640x480, real unpacked R10, with the CPU debayer (LIBCAMERA_SOFTISP_MODE=cpu) and the GPU debayer, using qcam. Linux 7.4 og0ve1b module modified as proposed in https://lore.kernel.org/all/5bb49b41-368c-4889-9f56-9a477e85694f@laposte.net/ - Full size: the framing and look are comparable between CPU and GPU. - Simulated formats: R8, R12, R16, R10P, R12P with LIBCAMERA_SOFTISP_SIM_MONO. Observed: image visually identical to the R10 reference. - Output smaller than the input (320x240 on 640x480): the CPU crops the centre at the native resolution, while the GPU shrinks the whole image. This comes from the existing EGL projection, which the series does not modify. - Not tested: the simulated formats on real sensors, and a numerical comparison of the levels (no calibration chart) at equal brightness. Signed-off-by: Alain CousiniƩ --- src/libcamera/pipeline/simple/simple.cpp | 6 +- src/libcamera/software_isp/debayer.cpp | 205 +++++++++++++++++++- src/libcamera/software_isp/debayer.h | 18 ++ src/libcamera/software_isp/software_isp.cpp | 6 +- 4 files changed, 232 insertions(+), 3 deletions(-) diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp index 2f7f4c893..13660fd14 100644 --- a/src/libcamera/pipeline/simple/simple.cpp +++ b/src/libcamera/pipeline/simple/simple.cpp @@ -1247,6 +1247,7 @@ CameraConfiguration::Status SimpleCameraConfiguration::validate() const Size &maxOutputSize = pipeConfig->outputSizes.max; bool curCfgPacked = false; bool newCfgPacked = false; + bool newCfgMono = false; /* TEST ONLY, not for merge */ if (pipeConfig_) { BayerFormat curBayerFmt = BayerFormat::fromPixelFormat(pipeConfig_->captureFormat); @@ -1255,6 +1256,7 @@ CameraConfiguration::Status SimpleCameraConfiguration::validate() if (curBayerFmt.isValid() && newBayerFmt.isValid()) { curCfgPacked = curBayerFmt.packing == BayerFormat::Packing::CSI2; newCfgPacked = newBayerFmt.packing == BayerFormat::Packing::CSI2; + newCfgMono = newBayerFmt.order == BayerFormat::Order::MONO; } } @@ -1263,7 +1265,9 @@ CameraConfiguration::Status SimpleCameraConfiguration::validate() captureSize.width >= maxRawStreamSize.width && captureSize.height >= maxRawStreamSize.height) { if (!pipeConfig_ || captureSize < pipeConfig_->captureSize || - (captureSize <= pipeConfig_->captureSize && !curCfgPacked && newCfgPacked)) + (captureSize <= pipeConfig_->captureSize && + (newCfgMono ? (curCfgPacked && !newCfgPacked) /* TEST ONLY: unpacked for mono */ + : (!curCfgPacked && newCfgPacked)))) pipeConfig_ = pipeConfig; } diff --git a/src/libcamera/software_isp/debayer.cpp b/src/libcamera/software_isp/debayer.cpp index 4f6659000..029ced2f0 100644 --- a/src/libcamera/software_isp/debayer.cpp +++ b/src/libcamera/software_isp/debayer.cpp @@ -11,6 +11,10 @@ #include "debayer.h" +#include + +#include + namespace libcamera { /** @@ -98,7 +102,10 @@ LOG_DEFINE_CATEGORY(Debayer) * \param[in] cm The camera manager */ Debayer::Debayer(const CameraManager &cm) - : bench_(cm, "Debayer") + : bench_(cm, "Debayer"), + simAllocator_(DmaBufAllocator::DmaBufAllocatorFlag::CmaHeap | + DmaBufAllocator::DmaBufAllocatorFlag::SystemHeap | + DmaBufAllocator::DmaBufAllocatorFlag::UDmaBuf) { } @@ -339,4 +346,200 @@ bool Debayer::isStandardBayerOrder(BayerFormat::Order order) order == BayerFormat::GRBG || order == BayerFormat::RGGB; } +namespace { + +std::optional parseSimMonoFormat() +{ + const char *env = std::getenv("LIBCAMERA_SOFTISP_SIM_MONO"); + if (!env || !*env) + return std::nullopt; + + std::string val(env); + if (val == "RAW8") + return BayerFormat{ BayerFormat::Order::MONO, 8, BayerFormat::Packing::None }; + if (val == "RAW10") + return BayerFormat{ BayerFormat::Order::MONO, 10, BayerFormat::Packing::None }; + if (val == "RAW10P") + return BayerFormat{ BayerFormat::Order::MONO, 10, BayerFormat::Packing::CSI2 }; + if (val == "RAW12") + return BayerFormat{ BayerFormat::Order::MONO, 12, BayerFormat::Packing::None }; + if (val == "RAW12P") + return BayerFormat{ BayerFormat::Order::MONO, 12, BayerFormat::Packing::CSI2 }; + if (val == "RAW16") + return BayerFormat{ BayerFormat::Order::MONO, 16, BayerFormat::Packing::None }; + + LOG(Debayer, Warning) << "Unknown LIBCAMERA_SOFTISP_SIM_MONO value: " << val; + return std::nullopt; +} + +unsigned int simStrideForFormat(const BayerFormat &fmt, unsigned int width) +{ + unsigned int lineBytes = 0; + if (fmt.packing == BayerFormat::Packing::None) { + lineBytes = width * ((fmt.bitDepth + 7) / 8); + } else { + if (fmt.bitDepth == 10) + lineBytes = (width * 5) / 4; + else if (fmt.bitDepth == 12) + lineBytes = (width * 3) / 2; + } + return (lineBytes + 63) & ~63; +} + +} /* namespace */ + +const StreamConfiguration &Debayer::setupSimulation(const StreamConfiguration &inputCfg) +{ + simActive_ = false; + pendingRealInputs_.clear(); + simMappedBuffer_.reset(); + simFrameBuffer_.reset(); + + auto targetOpt = parseSimMonoFormat(); + if (!targetOpt) + return inputCfg; + + BayerFormat realFmt = BayerFormat::fromPixelFormat(inputCfg.pixelFormat); + if (realFmt.order != BayerFormat::Order::MONO || + realFmt.packing != BayerFormat::Packing::None || + realFmt.bitDepth != 10) { + LOG(Debayer, Warning) << "Simulation expects RAW10 unpacked input, got " + << realFmt; + return inputCfg; + } + + if (realFmt == *targetOpt) + return inputCfg; + + PixelFormat targetPixelFormat = targetOpt->toPixelFormat(); + if (!targetPixelFormat.isValid()) { + LOG(Debayer, Error) << "Simulation: no PixelFormat registered for " + << *targetOpt; + return inputCfg; + } + + simTargetFmt_ = *targetOpt; + realInputStride_ = inputCfg.stride; + + simInputCfg_ = inputCfg; + simInputCfg_.pixelFormat = targetPixelFormat; + simInputCfg_.stride = simStrideForFormat(simTargetFmt_, inputCfg.size.width); + simInputCfg_.frameSize = simInputCfg_.stride * inputCfg.size.height; + + if (!simAllocator_.isValid()) { + LOG(Debayer, Error) << "Simulation: DmaBufAllocator is not valid"; + return inputCfg; + } + + UniqueFD fd = simAllocator_.alloc("softisp-mono-sim", simInputCfg_.frameSize); + if (!fd.isValid()) { + LOG(Debayer, Error) << "Simulation: failed to allocate DmaBuf"; + return inputCfg; + } + + std::vector planes(1); + planes[0].fd = SharedFD(std::move(fd)); + planes[0].offset = 0; + planes[0].length = simInputCfg_.frameSize; + + simFrameBuffer_ = std::make_unique(planes); + simMappedBuffer_ = std::make_unique(simFrameBuffer_.get(), + MappedFrameBuffer::MapFlag::Write); + if (!simMappedBuffer_->isValid()) { + LOG(Debayer, Error) << "Simulation: failed to mmap DmaBuf"; + return inputCfg; + } + + simActive_ = true; + LOG(Debayer, Info) << "Mono simulation enabled: " << realFmt + << " -> " << simTargetFmt_ + << " (stride=" << simInputCfg_.stride << ")"; + + return simInputCfg_; +} + +FrameBuffer *Debayer::simulateInputFrame(FrameBuffer *realInput) +{ + if (!simActive_) + return realInput; + + DmaSyncer syncRead(realInput->planes()[0].fd, DmaSyncer::SyncType::Read); + DmaSyncer syncWrite(simFrameBuffer_->planes()[0].fd, DmaSyncer::SyncType::Write); + + MappedFrameBuffer in(realInput, MappedFrameBuffer::MapFlag::Read); + if (!in.isValid()) + return realInput; + + const unsigned int width = simInputCfg_.size.width; + const unsigned int height = simInputCfg_.size.height; + const uint8_t *srcBase = in.planes()[0].data(); + uint8_t *dstBase = simMappedBuffer_->planes()[0].data(); + + for (unsigned int y = 0; y < height; y++) { + const uint16_t *src = reinterpret_cast(srcBase + y * realInputStride_); + uint8_t *dst = dstBase + y * simInputCfg_.stride; + + if (simTargetFmt_.packing == BayerFormat::Packing::None) { + uint16_t *dst16 = reinterpret_cast(dst); + + switch (simTargetFmt_.bitDepth) { + case 8: + for (unsigned int x = 0; x < width; x++) + dst[x] = static_cast(src[x] >> 2); + break; + case 10: + for (unsigned int x = 0; x < width; x++) + dst16[x] = src[x]; + break; + case 12: + for (unsigned int x = 0; x < width; x++) + dst16[x] = (src[x] << 2) | (src[x] >> 8); + break; + case 16: + for (unsigned int x = 0; x < width; x++) + dst16[x] = (src[x] << 6) | (src[x] >> 4); + break; + } + } else if (simTargetFmt_.packing == BayerFormat::Packing::CSI2) { + switch (simTargetFmt_.bitDepth) { + case 10: + for (unsigned int x = 0; x < width; x += 4) { + dst[0] = static_cast(src[x] >> 2); + dst[1] = static_cast(src[x + 1] >> 2); + dst[2] = static_cast(src[x + 2] >> 2); + dst[3] = static_cast(src[x + 3] >> 2); + dst[4] = (src[x] & 0x03) | ((src[x + 1] & 0x03) << 2) | + ((src[x + 2] & 0x03) << 4) | ((src[x + 3] & 0x03) << 6); + dst += 5; + } + break; + case 12: + for (unsigned int x = 0; x < width; x += 2) { + uint16_t p0 = (src[x] << 2) | (src[x] >> 8); + uint16_t p1 = (src[x + 1] << 2) | (src[x + 1] >> 8); + dst[0] = static_cast(p0 >> 4); + dst[1] = static_cast(p1 >> 4); + dst[2] = (p0 & 0x0f) | ((p1 & 0x0f) << 4); + dst += 3; + } + break; + } + } + } + + simFrameBuffer_->_d()->metadata().planes()[0].bytesused = simInputCfg_.frameSize; + pendingRealInputs_.push_back(realInput); + return simFrameBuffer_.get(); +} + +FrameBuffer *Debayer::restoreRealInputFrame(FrameBuffer *buffer) +{ + if (!simActive_ || buffer != simFrameBuffer_.get() || pendingRealInputs_.empty()) + return buffer; + + FrameBuffer *realInput = pendingRealInputs_.front(); + pendingRealInputs_.pop_front(); + return realInput; +} + } /* namespace libcamera */ diff --git a/src/libcamera/software_isp/debayer.h b/src/libcamera/software_isp/debayer.h index e30a05279..2a45ff5f0 100644 --- a/src/libcamera/software_isp/debayer.h +++ b/src/libcamera/software_isp/debayer.h @@ -11,7 +11,10 @@ #pragma once +#include +#include #include +#include #include #include @@ -22,6 +25,8 @@ #include "libcamera/internal/bayer_format.h" #include "libcamera/internal/dma_buf_allocator.h" +#include "libcamera/internal/framebuffer.h" +#include "libcamera/internal/mapped_framebuffer.h" #include "libcamera/internal/software_isp/benchmark.h" #include "libcamera/internal/software_isp/debayer_params.h" @@ -82,9 +87,22 @@ public: bool swapRedBlueGains_; Benchmark bench_; + const StreamConfiguration &setupSimulation(const StreamConfiguration &inputCfg); + FrameBuffer *simulateInputFrame(FrameBuffer *realInput); + FrameBuffer *restoreRealInputFrame(FrameBuffer *buffer); + private: virtual Size patternSize(PixelFormat inputFormat) = 0; + DmaBufAllocator simAllocator_; + StreamConfiguration simInputCfg_; + BayerFormat simTargetFmt_; + std::unique_ptr simFrameBuffer_; + std::unique_ptr simMappedBuffer_; + std::deque pendingRealInputs_; + unsigned int realInputStride_ = 0; + bool simActive_ = false; + protected: void dmaSyncBegin(std::vector &dmaSyncers, FrameBuffer *input, FrameBuffer *output); static bool isStandardBayerOrder(BayerFormat::Order order); diff --git a/src/libcamera/software_isp/software_isp.cpp b/src/libcamera/software_isp/software_isp.cpp index 39d271728..9c4cb8e1f 100644 --- a/src/libcamera/software_isp/software_isp.cpp +++ b/src/libcamera/software_isp/software_isp.cpp @@ -301,7 +301,9 @@ int SoftwareIsp::configure(const StreamConfiguration &inputCfg, if (bayerFormat.order == BayerFormat::Order::MONO) runCcm = false; - ret = debayer_->configure(inputCfg, outputCfgs, runCcm, lscEnabled_); + const StreamConfiguration &debayerCfg = + bayerFormat.order == BayerFormat::Order::MONO ? debayer_->setupSimulation(inputCfg) : inputCfg; + ret = debayer_->configure(debayerCfg, outputCfgs, runCcm, lscEnabled_); if (ret < 0) return ret; @@ -441,6 +443,7 @@ void SoftwareIsp::stop() void SoftwareIsp::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output) { ipa_->computeParams(frame); + input = debayer_->simulateInputFrame(input); debayer_->invokeMethod(&Debayer::process, ConnectionTypeQueued, frame, input, output, debayerParams_); } @@ -462,6 +465,7 @@ void SoftwareIsp::statsReady(uint32_t frame, uint32_t bufferId) void SoftwareIsp::inputReady(FrameBuffer *input) { + input = debayer_->restoreRealInputFrame(input); ASSERT(queuedInputBuffers_.front() == input); queuedInputBuffers_.pop_front(); inputBufferReady.emit(input);