[{"id":37501,"web_url":"https://patchwork.libcamera.org/comment/37501/","msgid":"<176771565327.112111.2941659105078103679@isaac-ThinkPad-T16-Gen-2>","date":"2026-01-06T16:07:33","subject":"Re: [PATCH 03/11] libcamera: sensor: Add CameraSensorMemory class","submitter":{"id":215,"url":"https://patchwork.libcamera.org/api/people/215/","name":"Isaac Scott","email":"isaac.scott@ideasonboard.com"},"content":"Hi David,\n\nThank you for the patch!\n\nQuoting David Plowman (2025-12-10 16:15:18)\n> Representation of a \"camera\" that actually takes its input from\n> a memory buffer.\n> \n> libcamera is, unsurprisingly, very dependent on having a camera\n> connected to the system. But sometimes we may want to process raw\n> camera images from elsewhere, and we may not have a camera connected\n> to the system at all.\n> \n> In such cases, the path of a \"memory buffer\" through the code is eased\n> considerably by introducing the CameraSensorMemory class, which allows\n> the memory buffer to behave, to an extent at least, like a real\n> camera.\n> \n> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>\n> ---\n>  include/libcamera/internal/camera_sensor.h    |   2 +\n>  .../libcamera/internal/camera_sensor_memory.h | 110 ++++++++\n>  include/libcamera/internal/meson.build        |   1 +\n>  src/libcamera/sensor/camera_sensor_memory.cpp | 241 ++++++++++++++++++\n>  src/libcamera/sensor/meson.build              |   1 +\n>  5 files changed, 355 insertions(+)\n>  create mode 100644 include/libcamera/internal/camera_sensor_memory.h\n>  create mode 100644 src/libcamera/sensor/camera_sensor_memory.cpp\n> \n> diff --git a/include/libcamera/internal/camera_sensor.h b/include/libcamera/internal/camera_sensor.h\n> index e6b72d22..5580d6ec 100644\n> --- a/include/libcamera/internal/camera_sensor.h\n> +++ b/include/libcamera/internal/camera_sensor.h\n> @@ -49,6 +49,8 @@ public:\n>  \n>         virtual CameraLens *focusLens() = 0;\n>  \n> +       virtual bool isMemory() const { return false; }\n> +\n>         virtual const std::vector<unsigned int> &mbusCodes() const = 0;\n>         virtual std::vector<Size> sizes(unsigned int mbusCode) const = 0;\n>         virtual Size resolution() const = 0;\n> diff --git a/include/libcamera/internal/camera_sensor_memory.h b/include/libcamera/internal/camera_sensor_memory.h\n> new file mode 100644\n> index 00000000..944d4c96\n> --- /dev/null\n> +++ b/include/libcamera/internal/camera_sensor_memory.h\n> @@ -0,0 +1,110 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2025, Raspberry Pi plc\n> + *\n> + * camera_sensor_memory.h - A fake camera sensor for reading raw data from memory\n> + */\n> +\n> +#pragma once\n> +\n> +#include <optional>\n> +#include <string>\n> +#include <vector>\n> +\n> +#include <libcamera/camera.h>\n> +\n> +#include \"libcamera/internal/camera_sensor.h\"\n> +\n> +namespace libcamera {\n> +\n> +class BayerFormat;\n> +class Camera;\n> +class CameraLens;\n> +class MediaEntity;\n> +class SensorConfiguration;\n> +\n> +struct CameraSensorProperties;\n> +\n> +enum class Orientation;\n> +\n> +LOG_DECLARE_CATEGORY(CameraSensor)\n> +\n> +class CameraSensorMemory : public CameraSensor, protected Loggable\n> +{\n> +public:\n> +       CameraSensorMemory(const StreamConfiguration &rawInput, unsigned int mbusCode);\n> +       ~CameraSensorMemory();\n> +\n> +       static std::variant<std::unique_ptr<CameraSensor>, int>\n> +       match(MediaEntity *entity);\n> +\n> +       const std::string &model() const override { return model_; }\n> +       const std::string &id() const override { return id_; }\n> +\n> +       const MediaEntity *entity() const override { return nullptr; }\n> +       V4L2Subdevice *device() override { return nullptr; }\n> +\n> +       CameraLens *focusLens() override { return nullptr; }\n> +\n> +       virtual bool isMemory() const override { return true; }\n> +\n> +       const std::vector<unsigned int> &mbusCodes() const override;\n> +       std::vector<Size> sizes(unsigned int mbusCode) const override;\n> +       Size resolution() const override;\n> +\n> +       V4L2SubdeviceFormat getFormat(Span<const unsigned int> mbusCodes,\n> +                                     const Size &size,\n> +                                     const Size maxSize) const override;\n> +       int setFormat(V4L2SubdeviceFormat *format,\n> +                     Transform transform = Transform::Identity) override;\n> +       int tryFormat(V4L2SubdeviceFormat *format) const override;\n> +\n> +       int applyConfiguration(const SensorConfiguration &config,\n> +                              Transform transform = Transform::Identity,\n> +                              V4L2SubdeviceFormat *sensorFormat = nullptr) override;\n> +\n> +       V4L2Subdevice::Stream imageStream() const override;\n> +       std::optional<V4L2Subdevice::Stream> embeddedDataStream() const override;\n> +       V4L2SubdeviceFormat embeddedDataFormat() const override;\n> +       int setEmbeddedDataEnabled(bool enable) override;\n> +\n> +       const ControlList &properties() const override;\n> +       int sensorInfo(IPACameraSensorInfo *info) const override;\n> +       Transform computeTransform(Orientation *orientation) const override;\n> +       BayerFormat::Order bayerOrder(Transform t) const override;\n> +       Orientation mountingOrientation() const override;\n> +\n> +       const ControlInfoMap &controls() const override;\n> +       ControlList getControls(Span<const uint32_t> ids) override;\n> +       int setControls(ControlList *ctrls) override;\n> +\n> +       const std::vector<controls::draft::TestPatternModeEnum> &\n> +       testPatternModes() const override { return testPatternModes_; }\n> +       int setTestPatternMode(controls::draft::TestPatternModeEnum mode) override;\n> +       const CameraSensorProperties::SensorDelays &sensorDelays() override;\n> +\n> +protected:\n> +       std::string logPrefix() const override;\n> +\n> +private:\n> +       LIBCAMERA_DISABLE_COPY(CameraSensorMemory)\n> +\n> +       StreamConfiguration rawInput_;\n> +\n> +       std::string model_;\n> +       std::string id_;\n> +\n> +       BayerFormat bayerFormat_;\n> +       std::vector<unsigned int> mbusCodes_;\n> +\n> +       V4L2SubdeviceFormat v4l2SubdeviceFormat_;\n> +\n> +       ControlInfoMap propertiesInfoMap_;\n> +       ControlInfoMap controlsInfoMap_;\n> +       ControlList properties_;\n> +       ControlList controls_;\n> +\n> +       std::vector<controls::draft::TestPatternModeEnum> testPatternModes_;\n> +};\n> +\n> +} /* namespace libcamera */\n> diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build\n> index e9540a2f..9994baae 100644\n> --- a/include/libcamera/internal/meson.build\n> +++ b/include/libcamera/internal/meson.build\n> @@ -10,6 +10,7 @@ libcamera_internal_headers = files([\n>      'camera_lens.h',\n>      'camera_manager.h',\n>      'camera_sensor.h',\n> +    'camera_sensor_memory.h',\n>      'camera_sensor_properties.h',\n>      'clock_recovery.h',\n>      'control_serializer.h',\n> diff --git a/src/libcamera/sensor/camera_sensor_memory.cpp b/src/libcamera/sensor/camera_sensor_memory.cpp\n> new file mode 100644\n> index 00000000..8e344016\n> --- /dev/null\n> +++ b/src/libcamera/sensor/camera_sensor_memory.cpp\n> @@ -0,0 +1,241 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2025, Raspberry Pi plc\n> + *\n> + * camera_sensor_memory.cpp - A fake camera sensor for reading raw data from memory\n> + */\n> +\n> +#include \"libcamera/internal/camera_sensor_memory.h\"\n> +\n> +#include <algorithm>\n> +#include <map>\n> +#include <sstream>\n> +\n> +#include <libcamera/base/log.h>\n> +#include <libcamera/base/utils.h>\n> +\n> +#include <libcamera/control_ids.h>\n> +#include <libcamera/controls.h>\n> +#include <libcamera/geometry.h>\n> +#include <libcamera/orientation.h>\n> +#include <libcamera/property_ids.h>\n> +#include <libcamera/transform.h>\n> +\n> +#include <libcamera/ipa/core_ipa_interface.h>\n> +\n> +#include \"libcamera/internal/bayer_format.h\"\n> +#include \"libcamera/internal/formats.h\"\n> +#include \"libcamera/internal/v4l2_subdevice.h\"\n> +\n> +namespace libcamera {\n> +\n> +LOG_DECLARE_CATEGORY(CameraSensor)\n> +\n> +static bool v4l2SubdeviceFormatEqual(const V4L2SubdeviceFormat &lhs, const V4L2SubdeviceFormat &rhs)\n> +{\n> +       return lhs.code == rhs.code && lhs.size == rhs.size && lhs.colorSpace == rhs.colorSpace;\n> +}\n> +\n> +CameraSensorMemory::CameraSensorMemory(const StreamConfiguration &rawInput, unsigned int mbusCode)\n> +       : rawInput_(rawInput), properties_(propertiesInfoMap_), controls_(controlsInfoMap_)\n> +{\n> +       model_ = \"memory\";\n> +\n> +       std::ostringstream oss;\n> +       oss << &rawInput;\n> +       id_ = oss.str();\n> +\n> +       /* The \"camera\" must appear to return the format the raw input wants. */\n> +       bayerFormat_ = BayerFormat::fromPixelFormat(rawInput.pixelFormat);\n> +       mbusCodes_ = { mbusCode };\n> +\n> +       v4l2SubdeviceFormat_ = V4L2SubdeviceFormat{\n> +               .code = mbusCode,\n> +               .size = rawInput.size,\n> +               .colorSpace = ColorSpace::Raw,\n> +       };\n> +}\n> +\n> +CameraSensorMemory::~CameraSensorMemory() = default;\n> +\n> +std::variant<std::unique_ptr<CameraSensor>, int>\n> +CameraSensorMemory::match([[maybe_unused]] MediaEntity *entity)\n> +{\n> +       return {};\n> +}\n> +\n> +const std::vector<unsigned int> &CameraSensorMemory::mbusCodes() const\n> +{\n> +       return mbusCodes_;\n> +}\n> +\n> +std::vector<Size> CameraSensorMemory::sizes(unsigned int mbusCode) const\n> +{\n> +       if (mbusCode == mbusCodes_[0])\n> +               return { rawInput_.size };\n> +       else\n> +               return {};\n> +}\n> +\n> +Size CameraSensorMemory::resolution() const\n> +{\n> +       return rawInput_.size;\n> +}\n> +\n> +V4L2SubdeviceFormat CameraSensorMemory::getFormat(Span<const unsigned int> mbusCodes,\n> +                                                 [[maybe_unused]] const Size &size,\n> +                                                 const Size maxSize) const\n> +{\n> +       if (std::find(mbusCodes.begin(), mbusCodes.end(), mbusCodes_[0]) == mbusCodes.end())\n> +               return {};\n> +\n> +       if (maxSize.width < rawInput_.size.width || maxSize.height < rawInput_.size.height)\n> +               return {};\n> +\n> +       return v4l2SubdeviceFormat_;\n> +}\n> +\n> +int CameraSensorMemory::setFormat(V4L2SubdeviceFormat *format,\n> +                                 Transform transform)\n> +{\n> +       if (v4l2SubdeviceFormatEqual(*format, v4l2SubdeviceFormat_) &&\n> +           transform == Transform::Identity)\n> +               return 0;\n> +\n> +       return -EPERM;\n> +}\n> +\n> +int CameraSensorMemory::tryFormat(V4L2SubdeviceFormat *format) const\n> +{\n> +       if (v4l2SubdeviceFormatEqual(*format, v4l2SubdeviceFormat_))\n> +               return 0;\n> +\n> +       return -EPERM;\n> +}\n> +\n> +int CameraSensorMemory::applyConfiguration(const SensorConfiguration &config,\n> +                                          Transform transform,\n> +                                          V4L2SubdeviceFormat *sensorFormat)\n> +{\n> +       if (config.bitDepth != bayerFormat_.bitDepth ||\n> +           config.outputSize != rawInput_.size ||\n> +           config.binning.binX != 1 || config.binning.binY != 1 ||\n> +           config.skipping.xOddInc != 1 || config.skipping.xEvenInc != 1 ||\n> +           config.skipping.yOddInc != 1 || config.skipping.yEvenInc != 1 ||\n> +           transform != Transform::Identity)\n> +               return -EPERM;\n> +\n> +       if (sensorFormat)\n> +               *sensorFormat = v4l2SubdeviceFormat_;\n> +\n> +       return 0;\n> +}\n> +\n> +V4L2Subdevice::Stream CameraSensorMemory::imageStream() const\n> +{\n> +       return V4L2Subdevice::Stream();\n> +}\n> +\n> +std::optional<V4L2Subdevice::Stream> CameraSensorMemory::embeddedDataStream() const\n> +{\n> +       return {};\n> +}\n> +\n> +V4L2SubdeviceFormat CameraSensorMemory::embeddedDataFormat() const\n> +{\n> +       return {};\n> +}\n> +\n> +int CameraSensorMemory::setEmbeddedDataEnabled(bool enable)\n> +{\n> +       return enable ? -ENOSTR : 0;\n> +}\n> +\n> +const ControlList &CameraSensorMemory::properties() const\n> +{\n> +       return properties_;\n> +}\n> +\n> +int CameraSensorMemory::sensorInfo([[maybe_unused]] IPACameraSensorInfo *info) const\n> +{\n> +       info->model = model();\n> +\n> +       info->bitsPerPixel = bayerFormat_.bitDepth;\n> +       info->cfaPattern = properties::draft::RGB;\n> +\n> +       info->activeAreaSize = rawInput_.size;\n> +       info->analogCrop = Rectangle(rawInput_.size);\n> +       info->outputSize = rawInput_.size;\n> +\n> +       /*\n> +        * These are meaningless for us, fill with ones rather than zeros because the\n> +        * code will divide by some of these numbers.\n> +        */\n> +       info->pixelRate = 1;\n> +       info->minLineLength = 1;\n> +       info->maxLineLength = 1;\n> +       info->minFrameLength = 1;\n> +       info->maxFrameLength = 1;\n> +\n> +       return 0;\n> +}\n> +\n> +Transform CameraSensorMemory::computeTransform(Orientation *orientation) const\n> +{\n> +       *orientation = Orientation::Rotate0;\n> +       return Transform::Identity;\n> +}\n> +\n> +BayerFormat::Order CameraSensorMemory::bayerOrder([[maybe_unused]] Transform t) const\n> +{\n> +       return bayerFormat_.order;\n> +}\n> +\n> +Orientation CameraSensorMemory::mountingOrientation() const\n> +{\n> +       return Orientation::Rotate0;\n> +}\n> +\n> +const ControlInfoMap &CameraSensorMemory::controls() const\n> +{\n> +       return *controls_.infoMap();\n> +}\n> +\n> +ControlList CameraSensorMemory::getControls([[maybe_unused]] Span<const uint32_t> ids)\n> +{\n> +       return ControlList();\n> +}\n> +\n> +int CameraSensorMemory::setControls([[maybe_unused]] ControlList *ctrls)\n> +{\n> +       return -EPERM;\n> +}\n> +\n> +int CameraSensorMemory::setTestPatternMode([[maybe_unused]] controls::draft::TestPatternModeEnum mode)\n> +{\n> +       return -EPERM;\n> +}\n> +\n> +const CameraSensorProperties::SensorDelays &CameraSensorMemory::sensorDelays()\n> +{\n> +       static constexpr CameraSensorProperties::SensorDelays defaultSensorDelays = {\n> +               .exposureDelay = 2,\n> +               .gainDelay = 1,\n> +               .vblankDelay = 2,\n> +               .hblankDelay = 2,\n> +       };\n> +\n> +       return defaultSensorDelays; /* but doesn't mean anything */\n> +}\n> +\n> +std::string CameraSensorMemory::logPrefix() const\n> +{\n> +       return \"'memory'\";\n> +}\n> +\n> +/*\n> + * We're not going to register this camera sensor as it doesn't match media entities\n> + * like other sensors. Pipeline handlers will have to call it explicitly.\n> + */\n\nVery interesting!\n\nReviewed-by: Isaac Scott <isaac.scott@ideasonboard.com>\n\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/sensor/meson.build b/src/libcamera/sensor/meson.build\n> index dce74ed6..b9b87612 100644\n> --- a/src/libcamera/sensor/meson.build\n> +++ b/src/libcamera/sensor/meson.build\n> @@ -3,6 +3,7 @@\n>  libcamera_internal_sources += files([\n>      'camera_sensor.cpp',\n>      'camera_sensor_legacy.cpp',\n> +    'camera_sensor_memory.cpp',\n>      'camera_sensor_properties.cpp',\n>      'camera_sensor_raw.cpp',\n>  ])\n> -- \n> 2.47.3\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 6D5A1BDCC0\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  6 Jan 2026 16:07:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A725361FA0;\n\tTue,  6 Jan 2026 17:07:37 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 203C161F9F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  6 Jan 2026 17:07:36 +0100 (CET)","from thinkpad.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id F1310129;\n\tTue,  6 Jan 2026 17:07:14 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"bg3+4bt2\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1767715635;\n\tbh=FSd1Wwd55W9CwrLM0xLxUBSzYpKBkLYWjtcg/O/USE8=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=bg3+4bt2PfYQvxn7AbXOrdRnIraevYmAJZEo38uSeK/g44XD2kg/b9a2SHhe/NTzU\n\tiQDAC/4HJr90W4KhPzYgruG4UzVbg16N8ydhgnKViS5KHKNIGQhpCD1KtHQsTQkddk\n\tMBOmUZXd3bLuQ4dkYtaxA9jbCv1ebvNni2izQnbQ=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20251210164055.17856-4-david.plowman@raspberrypi.com>","References":"<20251210164055.17856-1-david.plowman@raspberrypi.com>\n\t<20251210164055.17856-4-david.plowman@raspberrypi.com>","Subject":"Re: [PATCH 03/11] libcamera: sensor: Add CameraSensorMemory class","From":"Isaac Scott <isaac.scott@ideasonboard.com>","Cc":"David Plowman <david.plowman@raspberrypi.com>","To":"David Plowman <david.plowman@raspberrypi.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Tue, 06 Jan 2026 16:07:33 +0000","Message-ID":"<176771565327.112111.2941659105078103679@isaac-ThinkPad-T16-Gen-2>","User-Agent":"alot/0.10","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]