From patchwork Wed Aug 27 09:07:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 24242 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 D641FBD87C for ; Wed, 27 Aug 2025 09:08:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AA34E692FA; Wed, 27 Aug 2025 11:07:58 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="KKctlzDv"; dkim-atps=neutral Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id F3EDA692F7 for ; Wed, 27 Aug 2025 11:07:47 +0200 (CEST) Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-3c7edd71bbfso3636341f8f.0 for ; Wed, 27 Aug 2025 02:07:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1756285667; x=1756890467; 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=UzXPozUYv5GDAfASkNVE9/MfzThAtu3k+xq8xNPN7tk=; b=KKctlzDvidkFK7SBzhCXek4G1g1JIe2C56D7K1YgVUYe5nz/g1Nq73f/FBPsIZHyQD eYLF5Z5S4M5MoZn5cDozJ15q0yST15rryZsBgE0XD38ayN7igcdg3p5Sr3zOFkLNoTj/ y5hLCUdmGi4Y17VBY4tu74WYOighzJNZ8la6X73+l+zHzZEvj2lBFEhac/DMvQ2V0aTe RUn8uLiHdSZGLKfBH8S1E6f69uw6I7yRW/K5O1PGZWlLVmadRbMN7JUDkhaPPzU+AZWm rtbYuKM7+eZ8GWWwsQwiEcfldkr3Sjib6ecr2q+3mUUEViYl3nzBczPVPl97MEpIxtdn /FPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756285667; x=1756890467; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UzXPozUYv5GDAfASkNVE9/MfzThAtu3k+xq8xNPN7tk=; b=C6YNH4ygMFExeJTQdO+YbA72fADqK+Nfm/Cb2ZJ+YvW7iltfitJWt2cxJJmgLrXian ySjh3ba/bPTmU7/WyhcEHouEFgLT89itSkevlFR/RlOEaWt29WXjUoaAfZa/XDi5QuH+ tgiSB9/YgtoffJGc79sIyedl6D3LKvPwhyWBQ/bZeYgXIWKnValG7G10fVRnHKU64sR+ 4TVDysgE9e67cD4RZgUS4U+KrCga19pGdc6FP5yYGslHdjnICMwcTUMle0ZgFpxlQXNQ f14LEbmtjyhiVYZoYORBd3G9KVqNzEn+QmlFMDRlh9AJFpWqaVGwvRzDpUbHxmgISvvL JTjw== X-Gm-Message-State: AOJu0Ywdhmipg8McuKVfw/Bhu+NPkuRufT0qraRvR6WAL8LqvQ3O157G dbf2iAOv3j55Iawd2w75L4/UzDMYX/ZntVCiKX3ou0J0dH0tDAmElBCRevPRGeyueV55D8HxO9f N5oLK X-Gm-Gg: ASbGncsHJBYXYf1qUFfP4q+/LlHlE3PbsDX02ReFtiY6uvINA/vW/qvfnfdAT0pUzyM lEzCv/UAQfjFFCwj6fjwky+XUhzMWVZMgDlGaZlIOP/tvs3k7bmhQrSFo9yOuBhcx8e7Wc0tBas De8kfJIy/s2x050DepALZzwv0VgQXrcTk7SFrEcqYsz6uzZ2nibHL6LnwJfrisbzRgSt4ntrpKm +nf5ktXNkkafR3cHa4MVyc/2IZg/KdSu3vrP9PAarXuGMbH++n+WzEx3kbmIeEP6YyehY9KK3BO bTfZNmOHR5lu9m0sQREQg1dP/gvjjkwnjmy/HOWCJzpTdTAsjyp/MGy87EpeN5C1MwQmbalSsqZ OPooorcznXNQSvEZhXKeqQCS8vmWORMf35T/LsV0DvvTosTgDkbyCauB7q+lfATeyfuIvgAuFTE e135pYeCufdARJm4Lh+MyrOBuCuNP1UUJmHr+vJHs2uiZ6aoQZOA== X-Google-Smtp-Source: AGHT+IHTxJDYbkbqL+XrgmjrdSex1k4ATWeiNZ0VWpCeSYkG/ksNWa7D00eeYqBSHsWQevtWwwgHgA== X-Received: by 2002:a05:6000:43d7:b0:3c9:ad8:fec9 with SMTP id ffacd0b85a97d-3c90ad9064dmr9354434f8f.58.1756285666758; Wed, 27 Aug 2025 02:07:46 -0700 (PDT) Received: from raspberrypi.pitowers.org ([2a00:1098:3142:1f:ffc9:aff6:7f7f:893b]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45b6f30fe02sm21498675e9.18.2025.08.27.02.07.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Aug 2025 02:07:46 -0700 (PDT) From: David Plowman To: libcamera-devel@lists.libcamera.org Cc: David Plowman Subject: [RFC PATCH 05/12] libcamera: sensor: Add CameraSensorMemory class Date: Wed, 27 Aug 2025 10:07:32 +0100 Message-Id: <20250827090739.86955-6-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250827090739.86955-1-david.plowman@raspberrypi.com> References: <20250827090739.86955-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 memoery buffer. Signed-off-by: David Plowman --- include/libcamera/internal/camera_sensor.h | 2 + .../libcamera/internal/camera_sensor_memory.h | 109 ++++++++ include/libcamera/internal/meson.build | 1 + src/libcamera/sensor/camera_sensor_memory.cpp | 237 ++++++++++++++++++ src/libcamera/sensor/meson.build | 1 + 5 files changed, 350 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 13048f32..2b719d8c 100644 --- a/include/libcamera/internal/camera_sensor.h +++ b/include/libcamera/internal/camera_sensor.h @@ -48,6 +48,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..4552bff7 --- /dev/null +++ b/include/libcamera/internal/camera_sensor_memory.h @@ -0,0 +1,109 @@ +/* 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); + ~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(const std::vector &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; + + const ControlInfoMap &controls() const override; + ControlList getControls(const std::vector &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 5c80a28c..bba9df4f 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..dcb8679f --- /dev/null +++ b/src/libcamera/sensor/camera_sensor_memory.cpp @@ -0,0 +1,237 @@ +/* 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) + : 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); + unsigned int mbusCode = bayerFormat_.toMbusCode(); + 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(const std::vector &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; +} + +const ControlInfoMap &CameraSensorMemory::controls() const +{ + return *controls_.infoMap(); +} + +ControlList CameraSensorMemory::getControls([[maybe_unused]] const std::vector &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', ])