[v8,6/6] libcamera: software_isp: Add support for monochrome formats - TEST NO PROD
diff mbox series

Message ID 20261129152416.174279-7-alain.cousinie@laposte.net
State New
Headers show
Series
  • libcamera: software_isp: Add support for raw monochrome formats
Related show

Commit Message

Alain CousiniƩ Nov. 29, 2026, 3:24 p.m. UTC
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Ʃ <alain.cousinie@laposte.net>
---
 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(-)

Patch
diff mbox series

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 <string>
+
+#include <libcamera/formats.h>
+
 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<BayerFormat> 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<FrameBuffer::Plane> planes(1);
+	planes[0].fd = SharedFD(std::move(fd));
+	planes[0].offset = 0;
+	planes[0].length = simInputCfg_.frameSize;
+
+	simFrameBuffer_ = std::make_unique<FrameBuffer>(planes);
+	simMappedBuffer_ = std::make_unique<MappedFrameBuffer>(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<const uint16_t *>(srcBase + y * realInputStride_);
+		uint8_t *dst = dstBase + y * simInputCfg_.stride;
+
+		if (simTargetFmt_.packing == BayerFormat::Packing::None) {
+			uint16_t *dst16 = reinterpret_cast<uint16_t *>(dst);
+
+			switch (simTargetFmt_.bitDepth) {
+			case 8:
+				for (unsigned int x = 0; x < width; x++)
+					dst[x] = static_cast<uint8_t>(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<uint8_t>(src[x] >> 2);
+					dst[1] = static_cast<uint8_t>(src[x + 1] >> 2);
+					dst[2] = static_cast<uint8_t>(src[x + 2] >> 2);
+					dst[3] = static_cast<uint8_t>(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<uint8_t>(p0 >> 4);
+					dst[1] = static_cast<uint8_t>(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 <deque>
+#include <memory>
 #include <stdint.h>
+#include <vector>
 
 #include <libcamera/base/log.h>
 #include <libcamera/base/object.h>
@@ -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<FrameBuffer> simFrameBuffer_;
+	std::unique_ptr<MappedFrameBuffer> simMappedBuffer_;
+	std::deque<FrameBuffer *> pendingRealInputs_;
+	unsigned int realInputStride_ = 0;
+	bool simActive_ = false;
+
 protected:
 	void dmaSyncBegin(std::vector<DmaSyncer> &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);