From patchwork Wed Dec 14 15:52:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xavier Roumegue X-Patchwork-Id: 18011 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 15B33C3200 for ; Wed, 14 Dec 2022 15:52:40 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5D83A63364; Wed, 14 Dec 2022 16:52:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1671033159; bh=EU4C7PAT/+bEdad50glPCDBW/lROWbpHPF0xSXjuCQQ=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=r/FMIb3hGSsVh8Gi2BhviBAYGl8gB/JFWUAckIMYZ5mc0c6UqSDm5X+1copsAXRcc RT9XnuMZYnE92c7SNsq1NMMrdlvmcGzBcg4x7eZQmOhxjL7vQwGbuNDDBKBxH7+eL9 b4ESVZTglHhxTncKtSk/AR8urIlIqla10XhsjF9CJDybctZ+v1c0PoZ0Nvk6uh+JsG 7CAalgJvLwemgV4IlsR4rHXYvF0tEArQCLF5llohzlRti7S4NIdSjCtkJyctQfSOiU wiAB455EYkv8yKuN6YK7V6TxDNcYoR4bhoZXoBqXH9X2amVzB0utzFmkyQTcMhbjhM lA3QoVCHz4TnA== Received: from EUR03-VI1-obe.outbound.protection.outlook.com (mail-vi1eur03on2084.outbound.protection.outlook.com [40.107.103.84]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A1EEA61F23 for ; Wed, 14 Dec 2022 16:52:37 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="IEekvD2h"; dkim-atps=neutral ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d8jRBFm3URmROc1j9LzfsH8JoueYSuE9m7LavI9ocjK+abiM9vjlaCNxKPgizOpgmC3WFva1XHpAsuntfNjV6ZJwxlSSw82GfseyFlKLCJBAEoWKdcfHaU9kAkf3oDrcKqE8CuDvrzaQ0LQBTJSZkJG/DVuxeiGWqCDesz5sD7oR7LRtMOZEOqHKiXp5gBKheMa6mkUL4NnOivkeB7ClbX3Ux8yqglGK0d66EjnJ8YBB/V11RNEnbHdfyLqB0LN90V19+DJdJ1bhI12EXVIR3X30gDR4vlsiXwQnpq51kAcO3PK3x6uZUNkKWaUVrr2MAeow+YICHe0nHnYj93S1Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ki9ox3S9LIJ+Et3ECKUh0PL9vo3CzIWN7RdT1Hxp2TA=; b=UPsap24G02aqbIGAwU22+Tb8DZ1w19cAuOMN7EektVd+F3CEFhYtF15DwmEXGVrRlIG88nzxJ6V7sGU7mbL6OQu25gpKxiNIP5yqidxEqO2kIf/JByVKMO4q/zqDkvU/HpZ7lUC1tvKq6MbaAci+bJem0jrojMAAioWWBDN/An7ZW3FE9bmq2BQzBDNZTKoJJ0s/Qbn8kNMLSTIzHIrc7TztbDROv5tDiYpMD2Q8J2Pa1EdfS/6oHd6gzboHvFkOUgbAKlqC0Kl7f6DiwcaZO7CFl/dkFbjkgWD2ioBRTngI0dsjJ/6pMQFjUafDDbzQiGMy0QnQe+53VUTP4Yzjxw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector2-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ki9ox3S9LIJ+Et3ECKUh0PL9vo3CzIWN7RdT1Hxp2TA=; b=IEekvD2hfxnwzXeoiqWL6TEN6V6T+qufWXXXudvSFU2MV93vKpMrSphC6B5lU69lC3pzWRiR3M28R4bQKR9vDYinSF5YU0D7+FM5rqZZzAEfubDEXSM5z81sjI8i5ZJrqXpgtHElZ5tlh9hAi2SUeSE60cy2h88e9MAWH1xNi7I= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oss.nxp.com; Received: from PAXPR04MB8703.eurprd04.prod.outlook.com (2603:10a6:102:21e::22) by DU0PR04MB9694.eurprd04.prod.outlook.com (2603:10a6:10:312::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14; Wed, 14 Dec 2022 15:52:34 +0000 Received: from PAXPR04MB8703.eurprd04.prod.outlook.com ([fe80::14d3:8e4:cf07:810d]) by PAXPR04MB8703.eurprd04.prod.outlook.com ([fe80::14d3:8e4:cf07:810d%3]) with mapi id 15.20.5880.019; Wed, 14 Dec 2022 15:52:33 +0000 To: jacopo@jmondi.org, laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com Date: Wed, 14 Dec 2022 16:52:17 +0100 Message-Id: <20221214155218.419497-1-xavier.roumegue@oss.nxp.com> X-Mailer: git-send-email 2.38.1 X-ClientProxiedBy: PA7P264CA0049.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:34a::7) To PAXPR04MB8703.eurprd04.prod.outlook.com (2603:10a6:102:21e::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB8703:EE_|DU0PR04MB9694:EE_ X-MS-Office365-Filtering-Correlation-Id: eb2580a1-3402-4a80-b1ec-08daddeb353e X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fIyhOARnRVS0Zk0POSglKqjj16koQ9VL2ZjXj+81md88+tS8FvIdID7V7kPLT8O7XGEozCyczudocTv7dEdkj+fMWRe2POOzGk7XpD0Vl5XXmtPhm+aGAZo6uCh0orlv7Npqdxx92WzmPzT+g+6PZkAcZvNpKf+jM1yOewC5VyvmH/rZTSp+lxAMxEwpfsCCo90NF1nai5C8YRAcVCYP5K5IsywvCuh5qRAdu7srAHtIYqVbjxFOwsAHul/OichL+ZdGhIz/T+cCPt+Xes7uE4NcK8KtKDFCYH/hTwGfgFrU0MbhAZVYsJDOYDL4aMj2jVJCjpxzFKJmad3obzumhO0yLxr9OgQjWvdy06EEFwLSvqyA6ZTslbDXuHKtJ6r7MtGjzXgbq9LLK+w0pTrrsCZs2/8rbE+UBdhJAPMT4ei8JY2fTX91jiMkwPkTOeKULT8w4so9+WBK0FdjNlrJW6y+Jdf9JjUFzVTQp+67vPM4ipwDGdzO7K5AFjG9yqLT81X7HnEDfLD2MQRSBKyM1ycHPXK5N6U05rpwRpflMSKFek/D25X+j1W4KcsNr4sGep3zPWWvJjKrvx/tW5mvepW2Mqox5LKdPSxTVRYUudkB8XrzbhKBBoObYAdJL2x765EjYdVoyAvPHw65widVGSl887haxwlBM2/0Bm8Aog5aFu+GAJY6ZOiXePvHW8Wr X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PAXPR04MB8703.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(396003)(39860400002)(346002)(366004)(136003)(376002)(451199015)(66899015)(6666004)(86362001)(52116002)(6486002)(38100700002)(6506007)(478600001)(6512007)(83380400001)(2906002)(8936002)(316002)(186003)(66556008)(66476007)(1076003)(66946007)(5660300002)(44832011)(41300700001)(2616005)(4326008)(8676002)(30864003)(41533002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GVGR4XfR4k0nLrY9LyMWO47nQ10EiY0CRUI/UyVmoz+QccWIBIODCULZbhud1HlfI8o8V0i+HEK5yKxXdN0FoO4ZNK4QwGrktcl4Sm4YNkfvj46DFqVhUxc8teLPI7yr5F6a4+1NuqvAffnC2pURgyS5BoGwEdaI+cDkyx1esBVo3QS33t6/JYJD6AqeEn/VvBzUM30Twu1BJszpMNX+KuUKK5ZjwoPnVfXYZxOwrt6Ycl5M2pZs5PHmzrBzz7F7xvJZN13QE1Khl3trLZboBUxSLsMIZndJ0O/sYOAWXIGl6y0Mz1AM/DWoKuy7f/3lG2xHpULd2vBpvK3OTFi0abh2WoomeLfyxneHKsvsDWiHwbchwiYSloQzJYHidypc1sIf+vBTOrtz5YvU6yDiE2do+Fbl+6F9Dvu4Sr4XiXRhhGuzALHep/jtkbRhxK4WQm/pb1s3dlG3tVMtlQLeV67bXvFKFf9Bg5fjwCbzFFbRCJbaKXGea4OeZDUx2CJH3hxviXteua+mtGgLkq9VWZJEfItGy6YY+MQrOhvKgKDkWj2uppBaKLsvHaaXRUf93evcRZL6mE4GoSF3/ZNfXfY1OO1stImXTFaVt23NI62KIhZhuRCf79/21s34gWeTBBaZ1cFdcN+jUu+kNZBxbFgb4Jy9dHABC++ZPRM2Y5czuXVWozmyhOO2dQOUM4Zz8QqAhGZK3CM7YShK66Y+Bl/MkRNywODjq2yJiEk5dUlyI2J6U2gSfjBg9JfYe5yFDOcPcqL6YM1E9LNb1HJRJqXAZ7n5tY2pDHJU3PfZ/tPAGeS27y6S+i1s6zu9Uxn0q4EUVmOIKKykqR85Klr8BwM8XsdE7v1HK1Bl1+6ZjA18zjgY7RkpLeNhUl6VzHc/qQHONWv4TlogfvGhpa3EcXriAu0RUCUYtdd3OgcP02JHbyJaOe/AHfx9bYPWblM6U1cJoYZcitjkyMZc3QcsDJscxy9IUed7GTAIt5LaQOotwlc8yqpUtcok36DPivGLW0LlUBuVGESDcv67TXQ9EQSF2BTb+YwzIu5h4V6vovX+RDUC/a1duNcLiLBIjDP797HRclndJI7zGHbZ2tzOK4eYni9p5wnlRbPc6Ebpi9K3P5k4/R7B8BXUQBFj+y/6j7nOkCnmf6fdCIRUgPJ8gBbwvMaOdr7kfUPXq5ei2TLvSlUTcWYESdtZlrG11nve2Em8s9mR7bIi46bRPzEgZxY0+uUHtE3l9VlirEo6YMVnHdZgYnhi9HfV1FePp41XzcpdTpbvlAdT9lYS2cahUnUDPEXh0KL2zW9uVKX3+K3x1jaqxeDj6CmgizZpcV6KwD+8pj6tWijLupU9uwc5dHu0jwcV3CUoLggFCrQB/GkOf2Sk1psWiV5ddVusoHQoL/KlBGdc4exOZn4piwJi9HGiBb3Iz2d037FMVNH/Ynw+FGOAmzmBnf0Ck0fpOf56CEKmvrdAWGgV47UoJWgqbp71a+1eAIAJXHu/vIrtQg/vYq9NVBFhGNFi9E3Ym29XvRJ+t97bbS3VTaBAb/6ZMkty+i/Zu8xYhvoioVcxD1DXV+wYJhS0Sg3K/fo1PYGrgp13tiPah+ZcNbBf0U0tdULbcxTNCIjQ2XbGQpMwXmf5NVtHVyHRw9+mL665fuo9sHz6ClLHnEUKspl90ISFiQ== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: eb2580a1-3402-4a80-b1ec-08daddeb353e X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB8703.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 15:52:33.5258 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 6unWcfYgXC/k+iJvZVHpqWSaHqfm24aR2F/5HDVkM/OhdapjY4IprZ7xUfl3StLuvjiKO7TYt3bZveAOklK5Fw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR04MB9694 Subject: [libcamera-devel] [PATCH v5 1/2] libcamera: Declare generic converter interface 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: , X-Patchwork-Original-From: Xavier Roumegue via libcamera-devel From: Xavier Roumegue Reply-To: Xavier Roumegue Cc: libcamera-devel@lists.libcamera.org Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Declare a converter Abstract Base Class intended to provide generic interfaces to hardware offering size and format conversion services on streams. This is mainly based on the public interfaces of the current converter class implementation found in the simple pipeline handler. The main change is the introduction of loadConfiguration() function which can be used by the concrete implementation to load hardware specific runtime parameters defined by the application. Signed-off-by: Xavier Roumegue Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham --- include/libcamera/internal/converter.h | 108 ++++++++ include/libcamera/internal/meson.build | 1 + src/libcamera/converter.cpp | 332 +++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 4 files changed, 442 insertions(+) create mode 100644 include/libcamera/internal/converter.h create mode 100644 src/libcamera/converter.cpp diff --git a/include/libcamera/internal/converter.h b/include/libcamera/internal/converter.h new file mode 100644 index 00000000..834ec5ab --- /dev/null +++ b/include/libcamera/internal/converter.h @@ -0,0 +1,108 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Laurent Pinchart + * Copyright 2022 NXP + * + * converter.h - Generic format converter interface + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +namespace libcamera { + +class FrameBuffer; +class MediaDevice; +class PixelFormat; +struct StreamConfiguration; + +class Converter +{ +public: + Converter(MediaDevice *media); + virtual ~Converter(); + + virtual int loadConfiguration(const std::string &filename) = 0; + + virtual bool isValid() const = 0; + + virtual std::vector formats(PixelFormat input) = 0; + virtual SizeRange sizes(const Size &input) = 0; + + virtual std::tuple + strideAndFrameSize(const PixelFormat &pixelFormat, const Size &size) = 0; + + virtual int configure(const StreamConfiguration &inputCfg, + const std::vector> &outputCfgs) = 0; + virtual int exportBuffers(unsigned int output, unsigned int count, + std::vector> *buffers) = 0; + + virtual int start() = 0; + virtual void stop() = 0; + + virtual int queueBuffers(FrameBuffer *input, + const std::map &outputs) = 0; + + Signal inputBufferReady; + Signal outputBufferReady; + + const std::string &deviceNode() const { return deviceNode_; } + +private: + std::string deviceNode_; +}; + +class ConverterFactoryBase +{ +public: + ConverterFactoryBase(const std::string name, std::initializer_list compatibles); + virtual ~ConverterFactoryBase() = default; + + const std::vector &compatibles() const { return compatibles_; } + + static std::unique_ptr create(MediaDevice *media); + static std::vector &factories(); + static std::vector names(); + +private: + LIBCAMERA_DISABLE_COPY_AND_MOVE(ConverterFactoryBase) + + static void registerType(ConverterFactoryBase *factory); + + virtual std::unique_ptr createInstance(MediaDevice *media) const = 0; + + std::string name_; + std::vector compatibles_; +}; + +template +class ConverterFactory : public ConverterFactoryBase +{ +public: + ConverterFactory(const char *name, std::initializer_list compatibles) + : ConverterFactoryBase(name, compatibles) + { + } + + std::unique_ptr createInstance(MediaDevice *media) const override + { + return std::make_unique<_Converter>(media); + } +}; + +#define REGISTER_CONVERTER(name, converter, compatibles) \ + static ConverterFactory global_##converter##Factory(name, compatibles); + +} /* namespace libcamera */ diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index f8be86e0..341af8a2 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -19,6 +19,7 @@ libcamera_internal_headers = files([ 'camera_sensor_properties.h', 'control_serializer.h', 'control_validator.h', + 'converter.h', 'delayed_controls.h', 'device_enumerator.h', 'device_enumerator_sysfs.h', diff --git a/src/libcamera/converter.cpp b/src/libcamera/converter.cpp new file mode 100644 index 00000000..3de39cff --- /dev/null +++ b/src/libcamera/converter.cpp @@ -0,0 +1,332 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright 2022 NXP + * + * converter.cpp - Generic format converter interface + */ + +#include "libcamera/internal/converter.h" + +#include + +#include + +#include "libcamera/internal/media_device.h" + +#include "linux/media.h" + +/** + * \file internal/converter.h + * \brief Abstract converter + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(Converter) + +/** + * \class Converter + * \brief Abstract Base Class for converter + * + * The Converter class is an Abstract Base Class defining the interfaces of + * converter implementations. + * + * Converters offer scaling and pixel format conversion services on an input + * stream. The converter can output multiple streams with individual conversion + * parameters from the same input stream. + */ + +/** + * \brief Construct a Converter instance + * \param[in] media The media device implementing the converter + * + * This searches for the entity implementing the data streaming function in the + * media graph entities and use its device node as the converter device node. + */ +Converter::Converter(MediaDevice *media) +{ + const std::vector &entities = media->entities(); + auto it = std::find_if(entities.begin(), entities.end(), + [](MediaEntity *entity) { + return entity->function() == MEDIA_ENT_F_IO_V4L; + }); + if (it == entities.end()) { + LOG(Converter, Error) + << "No entity suitable for implementing a converter in " + << media->driver() << " entities list."; + return; + } + + deviceNode_ = (*it)->deviceNode(); +} + +Converter::~Converter() +{ +} + +/** + * \fn Converter::loadConfiguration() + * \brief Load converter configuration from file + * \param[in] filename The file name path + * + * Load converter dependent configuration parameters to apply on the hardware. + * + * \return 0 on success or a negative error code otherwise + */ + +/** + * \fn Converter::isValid() + * \brief Check if the converter configuration is valid + * \return True is the converter is valid, false otherwise + */ + +/** + * \fn Converter::formats() + * \brief Retrieve the list of supported pixel formats for an input pixel format + * \param[in] input Input pixel format to retrieve output pixel format list for + * \return The list of supported output pixel formats + */ + +/** + * \fn Converter::sizes() + * \brief Retrieve the range of minimum and maximum output sizes for an input size + * \param[in] input Input stream size to retrieve range for + * \return A range of output image sizes + */ + +/** + * \fn Converter::strideAndFrameSize() + * \brief Retrieve the output stride and frame size for an input configutation + * \param[in] pixelFormat Input stream pixel format + * \param[in] size Input stream size + * \return A tuple indicating the stride and frame size or an empty tuple on error + */ + +/** + * \fn Converter::configure() + * \brief Configure a set of output stream conversion from an input stream + * \param[in] inputCfg Input stream configuration + * \param[out] outputCfgs A list of output stream configurations + * \return 0 on success or a negative error code otherwise + */ + +/** + * \fn Converter::exportBuffers() + * \brief Export buffers from the converter device + * \param[in] output Output stream index exporting the buffers + * \param[in] count Number of buffers to allocate + * \param[out] buffers Vector to store allocated buffers + * + * This function operates similarly to V4L2VideoDevice::exportBuffers() on the + * output stream indicated by the \a output index. + * + * \return The number of allocated buffers on success or a negative error code + * otherwise + */ + +/** + * \fn Converter::start() + * \brief Start the converter streaming operation + * \return 0 on success or a negative error code otherwise + */ + +/** + * \fn Converter::stop() + * \brief Stop the converter streaming operation + */ + +/** + * \fn Converter::queueBuffers() + * \brief Queue buffers to converter device + * \param[in] input The frame buffer to apply the conversion + * \param[out] outputs The container holding the output stream indexes and + * their respective frame buffer outputs. + * + * This function queues the \a input frame buffer on the output streams of the + * \a outputs map key and retrieve the output frame buffer indicated by the + * buffer map value. + * + * \return 0 on success or a negative error code otherwise + */ + +/** + * \var Converter::inputBufferReady + * \brief A signal emitted when the input frame buffer completes + */ + +/** + * \var Converter::outputBufferReady + * \brief A signal emitted on each frame buffer completion of the output queue + */ + +/** + * \fn Converter::deviceNode() + * \brief The converter device node attribute accessor + * \return The converter device node string + */ + +/** + * \class ConverterFactoryBase + * \brief Base class for converter factories + * + * The ConverterFactoryBase class is the base of all specializations of the + * ConverterFactory class template. It implements the factory registration, + * maintains a registry of factories, and provides access to the registered + * factories. + */ + +/** + * \brief Construct a converter factory base + * \param[in] name Name of the converter class + * \param[in] compatibles Name aliases of the converter class + * + * Creating an instance of the factory base registers it with the global list of + * factories, accessible through the factories() function. + * + * The factory \a name is used as unique identifier. If the converter + * implementation fully relies on a generic framework, the name should be the + * same as the framework. Otherwise, if the implementation is specialized, the + * factory name should match the driver name implementing the function. + * + * The factory \a compatibles holds a list of driver names implementing a generic + * subsystem without any personalizations. + */ +ConverterFactoryBase::ConverterFactoryBase(const std::string name, std::initializer_list compatibles) + : name_(name), compatibles_(compatibles) +{ + registerType(this); +} + +/** + * \fn ConverterFactoryBase::compatibles() + * \return The names compatibles + */ + +/** + * \brief Create an instance of the converter corresponding to a named factory + * \param[in] media Name of the factory + * + * \return A unique pointer to a new instance of the converter subclass + * corresponding to the named factory or one of its alias. Otherwise a null + * pointer if no such factory exists + */ +std::unique_ptr ConverterFactoryBase::create(MediaDevice *media) +{ + const std::vector &factories = + ConverterFactoryBase::factories(); + + for (const ConverterFactoryBase *factory : factories) { + const std::vector &compatibles = factory->compatibles(); + auto it = std::find(compatibles.begin(), compatibles.end(), media->driver()); + + if (it == compatibles.end() && media->driver() != factory->name_) + continue; + + LOG(Converter, Debug) + << "Creating converter from " + << factory->name_ << " factory with " + << (it == compatibles.end() ? "no" : media->driver()) << " alias."; + + return factory->createInstance(media); + } + + return nullptr; +} + +/** + * \brief Add a converter class to the registry + * \param[in] factory Factory to use to construct the converter class + * + * The caller is responsible to guarantee the uniqueness of the converter name. + */ +void ConverterFactoryBase::registerType(ConverterFactoryBase *factory) +{ + std::vector &factories = + ConverterFactoryBase::factories(); + + factories.push_back(factory); +} + +/** + * \brief Retrieve the list of all converter factory names + * \return The list of all converter factory names + */ +std::vector ConverterFactoryBase::names() +{ + std::vector list; + + std::vector &factories = + ConverterFactoryBase::factories(); + + for (ConverterFactoryBase *factory : factories) { + list.push_back(factory->name_); + for (auto alias : factory->compatibles()) + list.push_back(alias); + } + + return list; +} + +/** + * \brief Retrieve the list of all converter factories + * \return The list of converter factories + */ +std::vector &ConverterFactoryBase::factories() +{ + /* + * The static factories map is defined inside the function to ensure + * it gets initialized on first use, without any dependency on link + * order. + */ + static std::vector factories; + return factories; +} + +/** + * \var ConverterFactoryBase::name_ + * \brief The name of the factory + */ + +/** + * \var ConverterFactoryBase::compatibles_ + * \brief The list holding the factory compatibles + */ + +/** + * \class ConverterFactory + * \brief Registration of ConverterFactory classes and creation of instances + * \param _Converter The converter class type for this factory + * + * To facilitate discovery and instantiation of Converter classes, the + * ConverterFactory class implements auto-registration of converter helpers. + * Each Converter subclass shall register itself using the REGISTER_CONVERTER() + * macro, which will create a corresponding instance of a ConverterFactory + * subclass and register it with the static list of factories. + */ + +/** + * \fn ConverterFactory::ConverterFactory(const char *name, std::initializer_list compatibles) + * \brief Construct a converter factory + * \details \copydetails ConverterFactoryBase::ConverterFactoryBase + */ + +/** + * \fn ConverterFactory::createInstance() const + * \brief Create an instance of the Converter corresponding to the factory + * \param[in] media Media device pointer + * \return A unique pointer to a newly constructed instance of the Converter + * subclass corresponding to the factory + */ + +/** + * \def REGISTER_CONVERTER + * \brief Register a converter with the Converter factory + * \param[in] name Converter name used to register the class + * \param[in] converter Class name of Converter derived class to register + * \param[in] compatibles List of compatible names + * + * Register a Converter subclass with the factory and make it available to try + * and match converters. + */ + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 0494e808..e9d0324e 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -13,6 +13,7 @@ libcamera_sources = files([ 'controls.cpp', 'control_serializer.cpp', 'control_validator.cpp', + 'converter.cpp', 'delayed_controls.cpp', 'device_enumerator.cpp', 'device_enumerator_sysfs.cpp', From patchwork Wed Dec 14 15:52:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xavier Roumegue X-Patchwork-Id: 18012 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 52F26C328E for ; Wed, 14 Dec 2022 15:52:41 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6EA9A6335E; Wed, 14 Dec 2022 16:52:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1671033160; bh=asBEg9gu/6xz561UUWgnChpzK4x7mHo658WhVKwaZ/o=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=GqGSyUsDuA9oHg7VOc925+Fo9zkv3bQLrnSEwGidxK+MusyvolFAludSC2/KMvHkT dEImkjiET6vnDHiL4O8j3JZo+5JNQU53Xum5LmzUVgwI1euRz5eYLC2/jZzYCqg1p2 uJX80SGAJ7kZIHs5cyu58KiT+ypU0D1EQUvyAfq9n/Ui0S3CJ8imXeQXQX6nnPSaOA rgUeKegsriubLGzbFdqFH9umSo60PsSfMS9TZzCAqYsbKfK/qyfHioS0gWqRs/+hcq 42Td+oGlttgHO89K/zPRu53nSzRNS8nkTVswettA/RQBY5WSQdzozZWKnSPnFW/4NB xWtv5UlVAcnrg== Received: from EUR03-VI1-obe.outbound.protection.outlook.com (mail-vi1eur03on2084.outbound.protection.outlook.com [40.107.103.84]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E8E2A63354 for ; Wed, 14 Dec 2022 16:52:37 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="NvP6B1zi"; dkim-atps=neutral ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aK9VkVzDbNnSBE+Ka2JdN1BZ4bg380yIpxZUq+HbMWWWSaMcU8h6FhyeAJFKvzb6uKiwmq2LjXPa0H6uReGDJK3OjOmybfngfc+VZRHIyuLQ/TI4Z0MlurwHv5ZgkTBkXS41IT1LuIDnSWsiKajW7gd52gUZYBrJ6ltPD4iPMPYK/hMSfM/KGUTb4lHYbipQCM7IbmLwJ9UfA3/yCeU2JWbRC43m8lLpARJBcNlN7lUPIkyHt0IrUwpB59fqWNyobd/T+MuRWxM1yBKIDFeQTDkfGcmL7zsOoP2ODzbES9R6JYOqIc4PD7Y+xNArMUZoJD7tiMij/cUb4fPcpORx4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=o4JZjBUhhllRAwizgfQIWWv6qMtEq+ttX0q3VpWkPzI=; b=BjfiFpqEjaOaropZlUFLp/Ym/s/pPjO2Dvn+iKNL9lt5Np4m/VeIHLrXD+YhfuXkC3nnhfZcYsnBluuPMyxuer3J4dWwitbgdl/78Fe9YpFhYrc5BVKXHCgb8CaCvcf4yd+IzOtCxGW1DPNa/UzfrTgoBRZ96jqoCOreraEA01MO8vlvNo8pBXEU3sa3PuZFUwLOYfzKR9TIW0yRhtr4gfx6rt7MrfNd/kF3t/9b/WtTuEVL8CV7WUq2bdzgUN5IDD0BFGkwqNtSIwUDTXJNB8dIUqgSbNd4DEokfx9z3hN4sIV3uEt6a5Wr0w2zhZpFchJigGHvnUQImADFFnJt5g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector2-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=o4JZjBUhhllRAwizgfQIWWv6qMtEq+ttX0q3VpWkPzI=; b=NvP6B1ziggXsSPt/igkNZyjmCtVnU+bXbsaaW6FYN0NJcHD8iZRarzixSNUnGD9V7ET0WGcHeqvObNDA/aN+BBODQrf6NsQhtLTLcvWaUGVChaECNDPl3OSbUo4VJl61z2js8Jeb2WXj1mzFp/pkoLXY7J5GtRio4NjbdQswB0c= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oss.nxp.com; Received: from PAXPR04MB8703.eurprd04.prod.outlook.com (2603:10a6:102:21e::22) by DU0PR04MB9694.eurprd04.prod.outlook.com (2603:10a6:10:312::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.14; Wed, 14 Dec 2022 15:52:34 +0000 Received: from PAXPR04MB8703.eurprd04.prod.outlook.com ([fe80::14d3:8e4:cf07:810d]) by PAXPR04MB8703.eurprd04.prod.outlook.com ([fe80::14d3:8e4:cf07:810d%3]) with mapi id 15.20.5880.019; Wed, 14 Dec 2022 15:52:34 +0000 To: jacopo@jmondi.org, laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com Date: Wed, 14 Dec 2022 16:52:18 +0100 Message-Id: <20221214155218.419497-2-xavier.roumegue@oss.nxp.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221214155218.419497-1-xavier.roumegue@oss.nxp.com> References: <20221214155218.419497-1-xavier.roumegue@oss.nxp.com> X-ClientProxiedBy: PA7P264CA0049.FRAP264.PROD.OUTLOOK.COM (2603:10a6:102:34a::7) To PAXPR04MB8703.eurprd04.prod.outlook.com (2603:10a6:102:21e::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB8703:EE_|DU0PR04MB9694:EE_ X-MS-Office365-Filtering-Correlation-Id: 05362614-4694-4b0a-2c44-08daddeb373a X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: mt+2xTrEUA3o8QTo3yFPWCIKGMNUAF5hmOk304kQvtRonKGw6ft0/lr503932g+kkKKCaCNvFo7mZOl5OERUHg4JMEOnk6FdXcBSPNIgx4mXDPpM1SKs1IcJzBpYh14aqnc/cigxeW1qgkz7T7ol7XYFJCJe6rh734RxRNAYBkB1bY8JNGMYz/1XZUAnQNZlNDDhiH/ReLI/pAEZ3P+60iv7aN5Zrky2qd+ULRrMZUL7bmVQPr+4Dsba0jNE39+9Ev9Ywv3NCH2slLGzwqtdHCAROhp92mtfJ2jLVunZVg3FAmEvCcf7AVnI52+YFUm//y8KhrML9lO8P3ktdrpJP6MxFtO/X0etZ1XSLTX7SuRsBfCYKoLf8wHVoCcCKw8SG6ijGC4tcnosaVu3Lib5dq/06W4Bkc1MhJP6M9VWaBHqlsPGYUMaidxzZeeiHKnWV4clrI61RNgQs84BdSMULpfdAPDNExwTROYXPi3lsuiWYTR1OYlPqEpNojayB5yZbDDzjPVGTNue51zLnyOCjuOjERAYiG+T4180JlOZX4623ixKONDRU4geWDvMSSJDqi+Stf1chk9U4r6PoLthRYb0S+haGx51dDPcJoGr8mDwBmIoiU+U2buZQCZMYcKDngrACldBUuZmLTcqNwFscJCxW3Xk140fVjqZ5OAMivFGUXh/KYsYoyx0E49PuRsq X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PAXPR04MB8703.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(396003)(39860400002)(346002)(366004)(136003)(376002)(451199015)(6666004)(86362001)(52116002)(6486002)(38100700002)(6506007)(478600001)(6512007)(83380400001)(2906002)(8936002)(316002)(186003)(66556008)(66476007)(1076003)(66946007)(5660300002)(44832011)(41300700001)(2616005)(4326008)(8676002)(30864003)(41533002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: s3Rk0D15v/z3FhJY3a1XqfYLZRwViDVpBOugN2P6i4F3XFgL7TFQeBAP8icoV5+43q9owvOxEizhBBSJKXvIgSzBVe+97kd9OfhmnloziWB0JXVngax0+PJWEwoFxou6y2bwQNQHYXqfICpOACsdZOsZz/EHHJF09hxa5tL5nRaeLu3WVORLaqBqdgYMEXiEEAt85MW+ktml1xyDLlprkq6s0z77KelTrOUP7Za18niCQK3poDcyMqfnx/D0aOwFfPJkvytyGsQRXfeP+nxjS/JNu2K8wGctiSxQ/hj2nwItLg5iiHte/wfp3OgzE5tQVpr2sTSPa1jcHzNfP/55fF6Tvq8ZJJGc7XNBWvE3P0f/NOc0YrG15hiATJO/mlGplZAqdLvD4dqqNqaJpuHA1CkF/tj7KW2yJfmUHQ7Z9J1CQQo7ogJY0/iewQeJ29zj2EwAJoZ0Jikctnn1k0Au42bOAJhcaeQX7UBs1Gjxl/gb+bhY1L652zugV8Lcmwk1KRWvscoe/MXEr5s42d7ovQ9rZrolzmEr/TSuIneZsdIoC+BRrbIIf6SWMTLWlFRMfkC8eP92yzoyXAm4LP0OyxjsCJHQuBfoCUgtWhf/OV1krb0JkG0787ELnOp9f3MdEd4JMpVXGvbuEtGAPTw45PoiQa26XQEdDL8Y5kM5eDj7ZZisGxyU/SuHmzV+c3AJldU2QVwFLXOf+kJ/cnNVQQx3hknSm8r/7jFoIUgGMscfphvBchhpsGEcifbucTPxCXxBn+c7DklcPAOgArtflUp9AQm7aFKO8LdSj6v/rwrnBLK9Q+ViOqfBb5TZJcWS50URGsK8pUDKPmKC3OiOSrA1pmZlHCGatENcSVRcL59oFcP3canuANBfZbMW9xcIE0rcKmvQ5np3USv/OWNmnLHEvxJbsPFaTTy9gTiJGRznhugBj66YG0ol7nl7lh6HFzOSF/Ogi9VCk/3UCSCfAWpwf8K5pZpalj/QfQwZUDDvZafXujibIjZ7X2AZGuc3XkslxB9wNUNS3SSWmkqRwQ4aIPhAxNrmyjvwwKSCaHlxm4hhTm/txtKr07gWz5dLxlnmd/RT0frwXUW6NLFZ2EWOstGtEMbaxa4LTRDwRFOczTYpP90iL8Qheb49ZiR9Lvwlywobafc9Sy/gkjefiHs/wqnCMrUxhafZi5nU9L4HPQjC73159TAbH/JH1revsXKoasG2K4XMse+PLtUgpsQajr69uE6jlnlbswld3URLPesfcjF5qutNSd2BTsW8iVFeMuOYRsYf0JiJ4kcTUcO7FKdbabhbVTqGxj1Nz8eAljibcCpxibBfz4fU+FSUcEdOHhsAcpl/LXnQXXtM/zwpYRxAkhNv6ad727h+DH31nqHgLXbM53WyzbOqWLjdMa+kT13sLgWO3+unKNyUxDqg+UPRGjbny1XwqFj5azA4lUpAtRTBku5oYnLUbtZuNtm0LJdA/FqDTZMYIBkI2N6kfO77FRvSRSl/W3XGltGLKbrMZeP6aobi1RQYC84ttWsEuqtVUNdp93s3RXVHRvhoMUi1JZYZ4HgKyU3qztBF/mVt65yltNULeDjKFXunLzAGcoTp4HPK4RCrH68X2lmdQwV2rmQveKukhHszXN173CmBfJ0UXGegC+WjKYRzQkrjlwnKKZPzxAo6XuESmA== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 05362614-4694-4b0a-2c44-08daddeb373a X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB8703.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 15:52:34.2302 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: B5Ek7OuYCp6166HZ26JcYRvt9PsvGTN9MxwaVzT0kPMYxMYY5F8Y1/oGTGnk+G9V30RQRXUlezDXjOcqEVNNkw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR04MB9694 Subject: [libcamera-devel] [PATCH v5 2/2] libcamera: pipeline: simple: converter: Use generic converter interface 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: , X-Patchwork-Original-From: Xavier Roumegue via libcamera-devel From: Xavier Roumegue Reply-To: Xavier Roumegue Cc: libcamera-devel@lists.libcamera.org Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Move the simple converter implementation to a generic V4L2 M2M class derived from the converter interface. This latter could be used by other pipeline implementations and as base class for customized V4L2 M2M converters. Signed-off-by: Xavier Roumegue Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham --- .../internal/converter/converter_v4l2_m2m.h | 22 +-- .../libcamera/internal/converter/meson.build | 5 + include/libcamera/internal/meson.build | 2 + .../converter_v4l2_m2m.cpp} | 156 ++++++++++++------ src/libcamera/converter/meson.build | 5 + src/libcamera/meson.build | 1 + src/libcamera/pipeline/simple/meson.build | 1 - src/libcamera/pipeline/simple/simple.cpp | 6 +- 8 files changed, 129 insertions(+), 69 deletions(-) rename src/libcamera/pipeline/simple/converter.h => include/libcamera/internal/converter/converter_v4l2_m2m.h (83%) create mode 100644 include/libcamera/internal/converter/meson.build rename src/libcamera/{pipeline/simple/converter.cpp => converter/converter_v4l2_m2m.cpp} (67%) create mode 100644 src/libcamera/converter/meson.build diff --git a/src/libcamera/pipeline/simple/converter.h b/include/libcamera/internal/converter/converter_v4l2_m2m.h similarity index 83% rename from src/libcamera/pipeline/simple/converter.h rename to include/libcamera/internal/converter/converter_v4l2_m2m.h index f0ebe2e0..815916d0 100644 --- a/src/libcamera/pipeline/simple/converter.h +++ b/include/libcamera/internal/converter/converter_v4l2_m2m.h @@ -1,8 +1,9 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2020, Laurent Pinchart + * Copyright 2022 NXP * - * converter.h - Format converter for simple pipeline handler + * converter_v4l2_m2m.h - V4l2 M2M Format converter interface */ #pragma once @@ -14,11 +15,13 @@ #include #include -#include - #include #include +#include + +#include "libcamera/internal/converter.h" + namespace libcamera { class FrameBuffer; @@ -28,11 +31,12 @@ class SizeRange; struct StreamConfiguration; class V4L2M2MDevice; -class SimpleConverter +class V4L2M2MConverter : public Converter { public: - SimpleConverter(MediaDevice *media); + V4L2M2MConverter(MediaDevice *media); + int loadConfiguration([[maybe_unused]] const std::string &filename) { return 0; } bool isValid() const { return m2m_ != nullptr; } std::vector formats(PixelFormat input); @@ -52,14 +56,11 @@ public: int queueBuffers(FrameBuffer *input, const std::map &outputs); - Signal inputBufferReady; - Signal outputBufferReady; - private: class Stream : protected Loggable { public: - Stream(SimpleConverter *converter, unsigned int index); + Stream(V4L2M2MConverter *converter, unsigned int index); bool isValid() const { return m2m_ != nullptr; } @@ -80,7 +81,7 @@ private: void captureBufferReady(FrameBuffer *buffer); void outputBufferReady(FrameBuffer *buffer); - SimpleConverter *converter_; + V4L2M2MConverter *converter_; unsigned int index_; std::unique_ptr m2m_; @@ -88,7 +89,6 @@ private: unsigned int outputBufferCount_; }; - std::string deviceNode_; std::unique_ptr m2m_; std::vector streams_; diff --git a/include/libcamera/internal/converter/meson.build b/include/libcamera/internal/converter/meson.build new file mode 100644 index 00000000..891e79e7 --- /dev/null +++ b/include/libcamera/internal/converter/meson.build @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: CC0-1.0 + +libcamera_internal_headers += files([ + 'converter_v4l2_m2m.h', +]) diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build index 341af8a2..d7508805 100644 --- a/include/libcamera/internal/meson.build +++ b/include/libcamera/internal/meson.build @@ -45,3 +45,5 @@ libcamera_internal_headers = files([ 'v4l2_videodevice.h', 'yaml_parser.h', ]) + +subdir('converter') diff --git a/src/libcamera/pipeline/simple/converter.cpp b/src/libcamera/converter/converter_v4l2_m2m.cpp similarity index 67% rename from src/libcamera/pipeline/simple/converter.cpp rename to src/libcamera/converter/converter_v4l2_m2m.cpp index acaaa64c..2a4d1d99 100644 --- a/src/libcamera/pipeline/simple/converter.cpp +++ b/src/libcamera/converter/converter_v4l2_m2m.cpp @@ -1,11 +1,12 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2020, Laurent Pinchart + * Copyright 2022 NXP * - * converter.cpp - Format converter for simple pipeline handler + * converter_v4l2_m2m.cpp - V4L2 M2M Format converter */ -#include "converter.h" +#include "libcamera/internal/converter/converter_v4l2_m2m.h" #include #include @@ -21,18 +22,23 @@ #include "libcamera/internal/media_device.h" #include "libcamera/internal/v4l2_videodevice.h" +/** + * \file internal/converter/converter_v4l2_m2m.h + * \brief V4L2 M2M based converter + */ + namespace libcamera { -LOG_DECLARE_CATEGORY(SimplePipeline) +LOG_DECLARE_CATEGORY(Converter) /* ----------------------------------------------------------------------------- - * SimpleConverter::Stream + * V4L2M2MConverter::Stream */ -SimpleConverter::Stream::Stream(SimpleConverter *converter, unsigned int index) +V4L2M2MConverter::Stream::Stream(V4L2M2MConverter *converter, unsigned int index) : converter_(converter), index_(index) { - m2m_ = std::make_unique(converter->deviceNode_); + m2m_ = std::make_unique(converter->deviceNode()); m2m_->output()->bufferReady.connect(this, &Stream::outputBufferReady); m2m_->capture()->bufferReady.connect(this, &Stream::captureBufferReady); @@ -42,8 +48,8 @@ SimpleConverter::Stream::Stream(SimpleConverter *converter, unsigned int index) m2m_.reset(); } -int SimpleConverter::Stream::configure(const StreamConfiguration &inputCfg, - const StreamConfiguration &outputCfg) +int V4L2M2MConverter::Stream::configure(const StreamConfiguration &inputCfg, + const StreamConfiguration &outputCfg) { V4L2PixelFormat videoFormat = m2m_->output()->toV4L2PixelFormat(inputCfg.pixelFormat); @@ -56,14 +62,14 @@ int SimpleConverter::Stream::configure(const StreamConfiguration &inputCfg, int ret = m2m_->output()->setFormat(&format); if (ret < 0) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Failed to set input format: " << strerror(-ret); return ret; } if (format.fourcc != videoFormat || format.size != inputCfg.size || format.planes[0].bpl != inputCfg.stride) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Input format not supported (requested " << inputCfg.size << "-" << videoFormat << ", got " << format << ")"; @@ -78,13 +84,13 @@ int SimpleConverter::Stream::configure(const StreamConfiguration &inputCfg, ret = m2m_->capture()->setFormat(&format); if (ret < 0) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Failed to set output format: " << strerror(-ret); return ret; } if (format.fourcc != videoFormat || format.size != outputCfg.size) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Output format not supported"; return -EINVAL; } @@ -95,13 +101,13 @@ int SimpleConverter::Stream::configure(const StreamConfiguration &inputCfg, return 0; } -int SimpleConverter::Stream::exportBuffers(unsigned int count, - std::vector> *buffers) +int V4L2M2MConverter::Stream::exportBuffers(unsigned int count, + std::vector> *buffers) { return m2m_->capture()->exportBuffers(count, buffers); } -int SimpleConverter::Stream::start() +int V4L2M2MConverter::Stream::start() { int ret = m2m_->output()->importBuffers(inputBufferCount_); if (ret < 0) @@ -128,7 +134,7 @@ int SimpleConverter::Stream::start() return 0; } -void SimpleConverter::Stream::stop() +void V4L2M2MConverter::Stream::stop() { m2m_->capture()->streamOff(); m2m_->output()->streamOff(); @@ -136,8 +142,7 @@ void SimpleConverter::Stream::stop() m2m_->output()->releaseBuffers(); } -int SimpleConverter::Stream::queueBuffers(FrameBuffer *input, - FrameBuffer *output) +int V4L2M2MConverter::Stream::queueBuffers(FrameBuffer *input, FrameBuffer *output) { int ret = m2m_->output()->queueBuffer(input); if (ret < 0) @@ -150,12 +155,12 @@ int SimpleConverter::Stream::queueBuffers(FrameBuffer *input, return 0; } -std::string SimpleConverter::Stream::logPrefix() const +std::string V4L2M2MConverter::Stream::logPrefix() const { return "stream" + std::to_string(index_); } -void SimpleConverter::Stream::outputBufferReady(FrameBuffer *buffer) +void V4L2M2MConverter::Stream::outputBufferReady(FrameBuffer *buffer) { auto it = converter_->queue_.find(buffer); if (it == converter_->queue_.end()) @@ -167,32 +172,34 @@ void SimpleConverter::Stream::outputBufferReady(FrameBuffer *buffer) } } -void SimpleConverter::Stream::captureBufferReady(FrameBuffer *buffer) +void V4L2M2MConverter::Stream::captureBufferReady(FrameBuffer *buffer) { converter_->outputBufferReady.emit(buffer); } /* ----------------------------------------------------------------------------- - * SimpleConverter + * V4L2M2MConverter + */ + +/** + * \class libcamera::V4L2M2MConverter + * \brief The V4L2 M2M converter implements the converter interface based on + * V4L2 M2M device. +*/ + +/** + * \fn V4L2M2MConverter::V4L2M2MConverter + * \brief Construct a V4L2M2MConverter instance + * \param[in] media The media device implementing the converter */ -SimpleConverter::SimpleConverter(MediaDevice *media) +V4L2M2MConverter::V4L2M2MConverter(MediaDevice *media) + : Converter(media) { - /* - * Locate the video node. There's no need to validate the pipeline - * further, the caller guarantees that this is a V4L2 mem2mem device. - */ - const std::vector &entities = media->entities(); - auto it = std::find_if(entities.begin(), entities.end(), - [](MediaEntity *entity) { - return entity->function() == MEDIA_ENT_F_IO_V4L; - }); - if (it == entities.end()) + if (deviceNode().empty()) return; - deviceNode_ = (*it)->deviceNode(); - - m2m_ = std::make_unique(deviceNode_); + m2m_ = std::make_unique(deviceNode()); int ret = m2m_->open(); if (ret < 0) { m2m_.reset(); @@ -200,7 +207,21 @@ SimpleConverter::SimpleConverter(MediaDevice *media) } } -std::vector SimpleConverter::formats(PixelFormat input) +/** + * \fn libcamera::V4L2M2MConverter::loadConfiguration + * \details \copydetails libcamera::Converter::loadConfiguration + */ + +/** + * \fn libcamera::V4L2M2MConverter::isValid + * \details \copydetails libcamera::Converter::isValid + */ + +/** + * \fn libcamera::V4L2M2MConverter::formats + * \details \copydetails libcamera::Converter::formats + */ +std::vector V4L2M2MConverter::formats(PixelFormat input) { if (!m2m_) return {}; @@ -215,13 +236,13 @@ std::vector SimpleConverter::formats(PixelFormat input) int ret = m2m_->output()->setFormat(&v4l2Format); if (ret < 0) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Failed to set format: " << strerror(-ret); return {}; } if (v4l2Format.fourcc != m2m_->output()->toV4L2PixelFormat(input)) { - LOG(SimplePipeline, Debug) + LOG(Converter, Debug) << "Input format " << input << " not supported."; return {}; } @@ -237,7 +258,10 @@ std::vector SimpleConverter::formats(PixelFormat input) return pixelFormats; } -SizeRange SimpleConverter::sizes(const Size &input) +/** + * \copydoc libcamera::Converter::sizes + */ +SizeRange V4L2M2MConverter::sizes(const Size &input) { if (!m2m_) return {}; @@ -252,7 +276,7 @@ SizeRange SimpleConverter::sizes(const Size &input) int ret = m2m_->output()->setFormat(&format); if (ret < 0) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Failed to set format: " << strerror(-ret); return {}; } @@ -262,7 +286,7 @@ SizeRange SimpleConverter::sizes(const Size &input) format.size = { 1, 1 }; ret = m2m_->capture()->setFormat(&format); if (ret < 0) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Failed to set format: " << strerror(-ret); return {}; } @@ -272,7 +296,7 @@ SizeRange SimpleConverter::sizes(const Size &input) format.size = { UINT_MAX, UINT_MAX }; ret = m2m_->capture()->setFormat(&format); if (ret < 0) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Failed to set format: " << strerror(-ret); return {}; } @@ -282,9 +306,12 @@ SizeRange SimpleConverter::sizes(const Size &input) return sizes; } +/** + * \copydoc libcamera::Converter::strideAndFrameSize + */ std::tuple -SimpleConverter::strideAndFrameSize(const PixelFormat &pixelFormat, - const Size &size) +V4L2M2MConverter::strideAndFrameSize(const PixelFormat &pixelFormat, + const Size &size) { V4L2DeviceFormat format; format.fourcc = m2m_->capture()->toV4L2PixelFormat(pixelFormat); @@ -297,8 +324,11 @@ SimpleConverter::strideAndFrameSize(const PixelFormat &pixelFormat, return std::make_tuple(format.planes[0].bpl, format.planes[0].size); } -int SimpleConverter::configure(const StreamConfiguration &inputCfg, - const std::vector> &outputCfgs) +/** + * \copydoc libcamera::Converter::configure + */ +int V4L2M2MConverter::configure(const StreamConfiguration &inputCfg, + const std::vector> &outputCfgs) { int ret = 0; @@ -309,7 +339,7 @@ int SimpleConverter::configure(const StreamConfiguration &inputCfg, Stream &stream = streams_.emplace_back(this, i); if (!stream.isValid()) { - LOG(SimplePipeline, Error) + LOG(Converter, Error) << "Failed to create stream " << i; ret = -EINVAL; break; @@ -328,8 +358,11 @@ int SimpleConverter::configure(const StreamConfiguration &inputCfg, return 0; } -int SimpleConverter::exportBuffers(unsigned int output, unsigned int count, - std::vector> *buffers) +/** + * \copydoc libcamera::Converter::exportBuffers + */ +int V4L2M2MConverter::exportBuffers(unsigned int output, unsigned int count, + std::vector> *buffers) { if (output >= streams_.size()) return -EINVAL; @@ -337,7 +370,10 @@ int SimpleConverter::exportBuffers(unsigned int output, unsigned int count, return streams_[output].exportBuffers(count, buffers); } -int SimpleConverter::start() +/** + * \copydoc libcamera::Converter::start + */ +int V4L2M2MConverter::start() { int ret; @@ -352,14 +388,20 @@ int SimpleConverter::start() return 0; } -void SimpleConverter::stop() +/** + * \copydoc libcamera::Converter::stop + */ +void V4L2M2MConverter::stop() { for (Stream &stream : utils::reverse(streams_)) stream.stop(); } -int SimpleConverter::queueBuffers(FrameBuffer *input, - const std::map &outputs) +/** + * \copydoc libcamera::Converter::queueBuffers + */ +int V4L2M2MConverter::queueBuffers(FrameBuffer *input, + const std::map &outputs) { unsigned int mask = 0; int ret; @@ -402,4 +444,10 @@ int SimpleConverter::queueBuffers(FrameBuffer *input, return 0; } +static std::initializer_list compatibles = { + "pxp", +}; + +REGISTER_CONVERTER("v4l2_m2m", V4L2M2MConverter, compatibles) + } /* namespace libcamera */ diff --git a/src/libcamera/converter/meson.build b/src/libcamera/converter/meson.build new file mode 100644 index 00000000..2aa72fe4 --- /dev/null +++ b/src/libcamera/converter/meson.build @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: CC0-1.0 + +libcamera_sources += files([ + 'converter_v4l2_m2m.cpp' +]) diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index e9d0324e..ffc294f3 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -62,6 +62,7 @@ libatomic = cc.find_library('atomic', required : false) libthreads = dependency('threads') subdir('base') +subdir('converter') subdir('ipa') subdir('pipeline') subdir('proxy') diff --git a/src/libcamera/pipeline/simple/meson.build b/src/libcamera/pipeline/simple/meson.build index 9c99b32f..42b0896d 100644 --- a/src/libcamera/pipeline/simple/meson.build +++ b/src/libcamera/pipeline/simple/meson.build @@ -1,6 +1,5 @@ # SPDX-License-Identifier: CC0-1.0 libcamera_sources += files([ - 'converter.cpp', 'simple.cpp', ]) diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp index a32de7f3..24ded4db 100644 --- a/src/libcamera/pipeline/simple/simple.cpp +++ b/src/libcamera/pipeline/simple/simple.cpp @@ -30,13 +30,13 @@ #include "libcamera/internal/camera.h" #include "libcamera/internal/camera_sensor.h" +#include "libcamera/internal/converter.h" #include "libcamera/internal/device_enumerator.h" #include "libcamera/internal/media_device.h" #include "libcamera/internal/pipeline_handler.h" #include "libcamera/internal/v4l2_subdevice.h" #include "libcamera/internal/v4l2_videodevice.h" -#include "converter.h" namespace libcamera { @@ -266,7 +266,7 @@ public: std::vector configs_; std::map> formats_; - std::unique_ptr converter_; + std::unique_ptr converter_; std::vector> converterBuffers_; bool useConverter_; std::queue> converterQueue_; @@ -492,7 +492,7 @@ int SimpleCameraData::init() /* Open the converter, if any. */ MediaDevice *converter = pipe->converter(); if (converter) { - converter_ = std::make_unique(converter); + converter_ = ConverterFactoryBase::create(converter); if (!converter_->isValid()) { LOG(SimplePipeline, Warning) << "Failed to create converter, disabling format conversion";