From patchwork Wed Dec 10 16:15:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 25468 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 77DF2C326B for ; Wed, 10 Dec 2025 16:41:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 05288614A5; Wed, 10 Dec 2025 17:41:08 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="lZDyan4T"; dkim-atps=neutral Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 65062614B1 for ; Wed, 10 Dec 2025 17:41:02 +0100 (CET) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-47795f6f5c0so46405385e9.1 for ; Wed, 10 Dec 2025 08:41:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1765384862; x=1765989662; 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=8qIf10Gl0HoH/74okVhYV2SwichdzIgd8dv13WEYdyk=; b=lZDyan4TJAaNVdYcuruMwbUErwkwwd5imu1voPpGpJ5Q+bSjHVhpStIid4GaVjcgMJ VoP38tg9CZUr/AsG+9S2VgREm4XBhcsrNq9MMjLD60C+wN0pGqK9W5NX69uNRUgyIPJQ 1SE+OteBQR8iXSb8r7K8IWqBuQBr8CkokXka43W1JQ9TslHdm+MloEvCJrNhmdmaiEzY iPvHiMraIt31L7LA/03TTYjuiVW7GmoPh9Uyal2z0Il6ydKXUBoLCZChXhglMEHHcna3 u/whhl8kNu/rmxVWZXgrQ+BuZ61CAwyI67z9R0BzlMWRMCK/mA5VoDSv2rNtwyKJvXqv ADzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765384862; x=1765989662; 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=8qIf10Gl0HoH/74okVhYV2SwichdzIgd8dv13WEYdyk=; b=eWDto0oluwmv+WmGI7v9glYif/RmCSyQNuMLhwMdhnDyAppL3wbvtOwOUPBKQWJQfC u39Oo+INpIwZVhopLT7IfsMSWuCHDWniIuGtYFs1BwtfaN+hgFq1np7HUrLLKCGFn8zy WHjljACH5R3Y6gOp0FSzrzNGgGN81sZ7LCHkI0k4D0MeUnl2R3AcO2JizVey0VflEpVG QSOZPXOU9vWanP9cjHGF3MdL46ixkm6F9uK3iUx3DBJu14ivom4ZvDeGrAi0JeZo2wDf pFtVZARhf4cSZnFR7Mr0QvwaAfTmuOpQm0lejY/8pmSXWv3xy7KL4xCv0cPY9GDQ1TQL pPJQ== X-Gm-Message-State: AOJu0YxAuQ+kL7U3Of+r4drbgkb9PwQedbfhkqajCbs0ZvUbLtvr9dxT xFnPVyWWw2TmgOvJ9U2P0Xf8DtVFCC+wiyrCahk7EYLHz7TGL8lCRnvWq7daKtu1KDjBXQf0fAf OnYpz X-Gm-Gg: ASbGncvdHHi9Mza+ekI/c5uIA+E6XGuIyxFez/U1aCOz+ZZCsP1wmiaBEy41denbO3Z UHg7uGeZceDNyqzvThF6PU+Qp382Ce424ZZr8m7FI4ZcQYlQgqw2LyGhJ4f9arixsE2yWtvUmj3 Rcs7YP8jqpo2XWTAhgLhDu3lqG/YdvyH91rsYuyEo+E849Y1FxbvKZkn76HAXyFe/+06w9aIjkQ dR/P/15FD6UE/8ven3tpW7lEf2iDRhpzT8WzQXmWpC/5ZA56Fnh4Of4thQ0oJpWWbjpAQdfR2Lu wcC6XbRIcCwh9LuF/LAQdtuUHcZRYJ9rNINmLtCx+Av7JseMAbx6VM5rAEdSRugjLvg4xZEQMu+ Ekq28/md+VbWe4Wrsni9aGQXUcm7q2gB94Sf227AKQ6+IjJd84OZO/5iM88Sb3RFfg/LeHtH6R4 cy7vmjGZkI+b8XzmDRPXsB0saSiyVCXQj+ln00/uBANibVxyNRAAXu+jXUnEQBrwbQawCU7nib8 Psp11jT5VZ6gMjkCFSawe4zG3iGEm5vPo2WSQoa X-Google-Smtp-Source: AGHT+IEgIn/mqhl99WKqU5WnIkNOcPnWJnVMAl7iGF2NU8xIgvlSLpiliP51wDTxEEFROhuUNXF7ZQ== X-Received: by 2002:a05:600c:3513:b0:479:3a89:121e with SMTP id 5b1f17b1804b1-47a8381e5a5mr24587125e9.37.1765384861397; Wed, 10 Dec 2025 08:41:01 -0800 (PST) Received: from davidp-pi5.pitowers.org ([2a00:1098:3142:1f:88ea:c658:5b20:5e46]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47a88371e13sm1270415e9.11.2025.12.10.08.41.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Dec 2025 08:41:00 -0800 (PST) From: David Plowman To: libcamera-devel@lists.libcamera.org Cc: David Plowman Subject: [PATCH 03/11] libcamera: sensor: Add CameraSensorMemory class Date: Wed, 10 Dec 2025 16:15:18 +0000 Message-ID: <20251210164055.17856-4-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251210164055.17856-1-david.plowman@raspberrypi.com> References: <20251210164055.17856-1-david.plowman@raspberrypi.com> 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" Representation of a "camera" that actually takes its input from a memory buffer. libcamera is, unsurprisingly, very dependent on having a camera connected to the system. But sometimes we may want to process raw camera images from elsewhere, and we may not have a camera connected to the system at all. In such cases, the path of a "memory buffer" through the code is eased considerably by introducing the CameraSensorMemory class, which allows the memory buffer to behave, to an extent at least, like a real camera. Signed-off-by: David Plowman --- include/libcamera/internal/camera_sensor.h | 2 + .../libcamera/internal/camera_sensor_memory.h | 110 ++++++++ include/libcamera/internal/meson.build | 1 + src/libcamera/sensor/camera_sensor_memory.cpp | 241 ++++++++++++++++++ src/libcamera/sensor/meson.build | 1 + 5 files changed, 355 insertions(+) create mode 100644 include/libcamera/internal/camera_sensor_memory.h create mode 100644 src/libcamera/sensor/camera_sensor_memory.cpp diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h index e6b72d22..5580d6ec 100644 --- a/include/libcamera/internal/camera_sensor.h +++ b/include/libcamera/internal/camera_sensor.h @@ -49,6 +49,8 @@ public: virtual CameraLens *focusLens() = 0; + virtual bool isMemory() const { return false; } + virtual const std::vector &mbusCodes() const = 0; virtual std::vector sizes(unsigned int mbusCode) const = 0; virtual Size resolution() const = 0; diff --git a/include/libcamera/internal/camera_sensor_memory.h b/include/libcamera/internal/camera_sensor_memory.h new file mode 100644 index 00000000..944d4c96 --- /dev/null +++ b/include/libcamera/internal/camera_sensor_memory.h @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2025, Raspberry Pi plc + * + * camera_sensor_memory.h - A fake camera sensor for reading raw data from memory + */ + +#pragma once + +#include +#include +#include + +#include + +#include "libcamera/internal/camera_sensor.h" + +namespace libcamera { + +class BayerFormat; +class Camera; +class CameraLens; +class MediaEntity; +class SensorConfiguration; + +struct CameraSensorProperties; + +enum class Orientation; + +LOG_DECLARE_CATEGORY(CameraSensor) + +class CameraSensorMemory : public CameraSensor, protected Loggable +{ +public: + CameraSensorMemory(const StreamConfiguration &rawInput, unsigned int mbusCode); + ~CameraSensorMemory(); + + static std::variant, int> + match(MediaEntity *entity); + + const std::string &model() const override { return model_; } + const std::string &id() const override { return id_; } + + const MediaEntity *entity() const override { return nullptr; } + V4L2Subdevice *device() override { return nullptr; } + + CameraLens *focusLens() override { return nullptr; } + + virtual bool isMemory() const override { return true; } + + const std::vector &mbusCodes() const override; + std::vector sizes(unsigned int mbusCode) const override; + Size resolution() const override; + + V4L2SubdeviceFormat getFormat(Span mbusCodes, + const Size &size, + const Size maxSize) const override; + int setFormat(V4L2SubdeviceFormat *format, + Transform transform = Transform::Identity) override; + int tryFormat(V4L2SubdeviceFormat *format) const override; + + int applyConfiguration(const SensorConfiguration &config, + Transform transform = Transform::Identity, + V4L2SubdeviceFormat *sensorFormat = nullptr) override; + + V4L2Subdevice::Stream imageStream() const override; + std::optional embeddedDataStream() const override; + V4L2SubdeviceFormat embeddedDataFormat() const override; + int setEmbeddedDataEnabled(bool enable) override; + + const ControlList &properties() const override; + int sensorInfo(IPACameraSensorInfo *info) const override; + Transform computeTransform(Orientation *orientation) const override; + BayerFormat::Order bayerOrder(Transform t) const override; + Orientation mountingOrientation() const override; + + const ControlInfoMap &controls() const override; + ControlList getControls(Span ids) override; + int setControls(ControlList *ctrls) override; + + const std::vector & + testPatternModes() const override { return testPatternModes_; } + int setTestPatternMode(controls::draft::TestPatternModeEnum mode) override; + const CameraSensorProperties::SensorDelays &sensorDelays() override; + +protected: + std::string logPrefix() const override; + +private: + LIBCAMERA_DISABLE_COPY(CameraSensorMemory) + + StreamConfiguration rawInput_; + + std::string model_; + std::string id_; + + BayerFormat bayerFormat_; + std::vector mbusCodes_; + + V4L2SubdeviceFormat v4l2SubdeviceFormat_; + + ControlInfoMap propertiesInfoMap_; + ControlInfoMap controlsInfoMap_; + ControlList properties_; + ControlList controls_; + + std::vector testPatternModes_; +}; + +} /* namespace libcamera */ diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index e9540a2f..9994baae 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -10,6 +10,7 @@ libcamera_internal_headers = files([ 'camera_lens.h', 'camera_manager.h', 'camera_sensor.h', + 'camera_sensor_memory.h', 'camera_sensor_properties.h', 'clock_recovery.h', 'control_serializer.h', diff --git a/src/libcamera/sensor/camera_sensor_memory.cpp b/src/libcamera/sensor/camera_sensor_memory.cpp new file mode 100644 index 00000000..8e344016 --- /dev/null +++ b/src/libcamera/sensor/camera_sensor_memory.cpp @@ -0,0 +1,241 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2025, Raspberry Pi plc + * + * camera_sensor_memory.cpp - A fake camera sensor for reading raw data from memory + */ + +#include "libcamera/internal/camera_sensor_memory.h" + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "libcamera/internal/bayer_format.h" +#include "libcamera/internal/formats.h" +#include "libcamera/internal/v4l2_subdevice.h" + +namespace libcamera { + +LOG_DECLARE_CATEGORY(CameraSensor) + +static bool v4l2SubdeviceFormatEqual(const V4L2SubdeviceFormat &lhs, const V4L2SubdeviceFormat &rhs) +{ + return lhs.code == rhs.code && lhs.size == rhs.size && lhs.colorSpace == rhs.colorSpace; +} + +CameraSensorMemory::CameraSensorMemory(const StreamConfiguration &rawInput, unsigned int mbusCode) + : rawInput_(rawInput), properties_(propertiesInfoMap_), controls_(controlsInfoMap_) +{ + model_ = "memory"; + + std::ostringstream oss; + oss << &rawInput; + id_ = oss.str(); + + /* The "camera" must appear to return the format the raw input wants. */ + bayerFormat_ = BayerFormat::fromPixelFormat(rawInput.pixelFormat); + mbusCodes_ = { mbusCode }; + + v4l2SubdeviceFormat_ = V4L2SubdeviceFormat{ + .code = mbusCode, + .size = rawInput.size, + .colorSpace = ColorSpace::Raw, + }; +} + +CameraSensorMemory::~CameraSensorMemory() = default; + +std::variant, int> +CameraSensorMemory::match([[maybe_unused]] MediaEntity *entity) +{ + return {}; +} + +const std::vector &CameraSensorMemory::mbusCodes() const +{ + return mbusCodes_; +} + +std::vector CameraSensorMemory::sizes(unsigned int mbusCode) const +{ + if (mbusCode == mbusCodes_[0]) + return { rawInput_.size }; + else + return {}; +} + +Size CameraSensorMemory::resolution() const +{ + return rawInput_.size; +} + +V4L2SubdeviceFormat CameraSensorMemory::getFormat(Span mbusCodes, + [[maybe_unused]] const Size &size, + const Size maxSize) const +{ + if (std::find(mbusCodes.begin(), mbusCodes.end(), mbusCodes_[0]) == mbusCodes.end()) + return {}; + + if (maxSize.width < rawInput_.size.width || maxSize.height < rawInput_.size.height) + return {}; + + return v4l2SubdeviceFormat_; +} + +int CameraSensorMemory::setFormat(V4L2SubdeviceFormat *format, + Transform transform) +{ + if (v4l2SubdeviceFormatEqual(*format, v4l2SubdeviceFormat_) && + transform == Transform::Identity) + return 0; + + return -EPERM; +} + +int CameraSensorMemory::tryFormat(V4L2SubdeviceFormat *format) const +{ + if (v4l2SubdeviceFormatEqual(*format, v4l2SubdeviceFormat_)) + return 0; + + return -EPERM; +} + +int CameraSensorMemory::applyConfiguration(const SensorConfiguration &config, + Transform transform, + V4L2SubdeviceFormat *sensorFormat) +{ + if (config.bitDepth != bayerFormat_.bitDepth || + config.outputSize != rawInput_.size || + config.binning.binX != 1 || config.binning.binY != 1 || + config.skipping.xOddInc != 1 || config.skipping.xEvenInc != 1 || + config.skipping.yOddInc != 1 || config.skipping.yEvenInc != 1 || + transform != Transform::Identity) + return -EPERM; + + if (sensorFormat) + *sensorFormat = v4l2SubdeviceFormat_; + + return 0; +} + +V4L2Subdevice::Stream CameraSensorMemory::imageStream() const +{ + return V4L2Subdevice::Stream(); +} + +std::optional CameraSensorMemory::embeddedDataStream() const +{ + return {}; +} + +V4L2SubdeviceFormat CameraSensorMemory::embeddedDataFormat() const +{ + return {}; +} + +int CameraSensorMemory::setEmbeddedDataEnabled(bool enable) +{ + return enable ? -ENOSTR : 0; +} + +const ControlList &CameraSensorMemory::properties() const +{ + return properties_; +} + +int CameraSensorMemory::sensorInfo([[maybe_unused]] IPACameraSensorInfo *info) const +{ + info->model = model(); + + info->bitsPerPixel = bayerFormat_.bitDepth; + info->cfaPattern = properties::draft::RGB; + + info->activeAreaSize = rawInput_.size; + info->analogCrop = Rectangle(rawInput_.size); + info->outputSize = rawInput_.size; + + /* + * These are meaningless for us, fill with ones rather than zeros because the + * code will divide by some of these numbers. + */ + info->pixelRate = 1; + info->minLineLength = 1; + info->maxLineLength = 1; + info->minFrameLength = 1; + info->maxFrameLength = 1; + + return 0; +} + +Transform CameraSensorMemory::computeTransform(Orientation *orientation) const +{ + *orientation = Orientation::Rotate0; + return Transform::Identity; +} + +BayerFormat::Order CameraSensorMemory::bayerOrder([[maybe_unused]] Transform t) const +{ + return bayerFormat_.order; +} + +Orientation CameraSensorMemory::mountingOrientation() const +{ + return Orientation::Rotate0; +} + +const ControlInfoMap &CameraSensorMemory::controls() const +{ + return *controls_.infoMap(); +} + +ControlList CameraSensorMemory::getControls([[maybe_unused]] Span ids) +{ + return ControlList(); +} + +int CameraSensorMemory::setControls([[maybe_unused]] ControlList *ctrls) +{ + return -EPERM; +} + +int CameraSensorMemory::setTestPatternMode([[maybe_unused]] controls::draft::TestPatternModeEnum mode) +{ + return -EPERM; +} + +const CameraSensorProperties::SensorDelays &CameraSensorMemory::sensorDelays() +{ + static constexpr CameraSensorProperties::SensorDelays defaultSensorDelays = { + .exposureDelay = 2, + .gainDelay = 1, + .vblankDelay = 2, + .hblankDelay = 2, + }; + + return defaultSensorDelays; /* but doesn't mean anything */ +} + +std::string CameraSensorMemory::logPrefix() const +{ + return "'memory'"; +} + +/* + * We're not going to register this camera sensor as it doesn't match media entities + * like other sensors. Pipeline handlers will have to call it explicitly. + */ + +} /* namespace libcamera */ diff --git a/src/libcamera/sensor/meson.build b/src/libcamera/sensor/meson.build index dce74ed6..b9b87612 100644 --- a/src/libcamera/sensor/meson.build +++ b/src/libcamera/sensor/meson.build @@ -3,6 +3,7 @@ libcamera_internal_sources += files([ 'camera_sensor.cpp', 'camera_sensor_legacy.cpp', + 'camera_sensor_memory.cpp', 'camera_sensor_properties.cpp', 'camera_sensor_raw.cpp', ])