From patchwork Tue Aug 2 16:01:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 16915 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 1A3DBBE173 for ; Tue, 2 Aug 2022 16:02:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C6F026331C; Tue, 2 Aug 2022 18:01:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1659456119; bh=y0PMnYQX1wrw58d60A9bYt6GRMkj2iIQ+C7cJcZmJWk=; 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=0PkiEsmqqnNG/vNs14tK9dC/NQ6TdQ+WkpwlhL0RBVZZSOx1MZ9MgBG8is1G6rdBl t+qoCQe+BsNYOli7pOvlK1F7xnDNFiiNl72/ca71g3KZW+R4SoGlFNjYujybK0GPj/ 0LzKJBPy9B6qmAmsLs+cw0yfylhRwkvZp8Zxm42ntrKBbO1vey9lfGSKwq1X9zobAv p/qVuen55enuw2RUwEkhg+RIorh1OFz2ySv4XPy+dQbHc/3FbCGDCe07HNjIrp0JdG qRSRjitDHXjIpUyMtWiRN1Xow5or6OzfRZXTk9kkHFiyDk3zUfY989FclBO3mwCMzU iFXq9Zlcr4Zvg== Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::222]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8D5C56331C for ; Tue, 2 Aug 2022 18:01:58 +0200 (CEST) Received: (Authenticated sender: jacopo@jmondi.org) by mail.gandi.net (Postfix) with ESMTPSA id D0C3F40012; Tue, 2 Aug 2022 16:01:56 +0000 (UTC) To: libcamera-devel@lists.libcamera.org Date: Tue, 2 Aug 2022 18:01:35 +0200 Message-Id: <20220802160136.63075-6-jacopo@jmondi.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220802160136.63075-1-jacopo@jmondi.org> References: <20220802160136.63075-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 5/6] libcamera: v4l2_videodevice: Match formats supported by the device 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: Jacopo Mondi via libcamera-devel From: Jacopo Mondi Reply-To: Jacopo Mondi Cc: jozef@mlich.cz, Pavel Machek Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Now that V4L2PixelFormat::fromPixelFormat() returns a list of formats to chose from, select the one supported by the video device by matching against the list of supported pixel formats. The first format found to match one of the device supported ones is returned. As the list of pixel formats supported by the video device does not change at run-time, cache it at device open() time. To maximize the lookup efficiency store the list of supported V4L2PixelFormat in an std::unordered_set<> which requires the V4L2PixelFormat class to be associated with an hash function object and to support the comparison operator. Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- include/libcamera/internal/v4l2_pixelformat.h | 15 +++++ include/libcamera/internal/v4l2_videodevice.h | 4 ++ src/libcamera/v4l2_pixelformat.cpp | 29 ++++++++++ src/libcamera/v4l2_videodevice.cpp | 58 ++++++++++++++----- 4 files changed, 92 insertions(+), 14 deletions(-) diff --git a/include/libcamera/internal/v4l2_pixelformat.h b/include/libcamera/internal/v4l2_pixelformat.h index d5400f90a67e..05069a30304d 100644 --- a/include/libcamera/internal/v4l2_pixelformat.h +++ b/include/libcamera/internal/v4l2_pixelformat.h @@ -8,6 +8,7 @@ #pragma once +#include #include #include #include @@ -22,6 +23,15 @@ namespace libcamera { class V4L2PixelFormat { public: + class Hasher + { + public: + std::size_t operator()(const V4L2PixelFormat &f) const + { + return f.fourcc(); + } + }; + struct Info { PixelFormat format; const char *description; @@ -37,6 +47,11 @@ public: { } + bool operator==(const V4L2PixelFormat &other) const + { + return fourcc_ == other.fourcc_; + } + bool isValid() const { return fourcc_ != 0; } uint32_t fourcc() const { return fourcc_; } operator uint32_t() const { return fourcc_; } diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index 29fa0bbaf670..c9064291a669 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -242,6 +243,8 @@ private: Stopped, }; + int initFormats(); + int getFormatMeta(V4L2DeviceFormat *format); int trySetFormatMeta(V4L2DeviceFormat *format, bool set); @@ -268,6 +271,7 @@ private: V4L2Capability caps_; V4L2DeviceFormat format_; const PixelFormatInfo *formatInfo_; + std::unordered_set pixelFormats_; enum v4l2_buf_type bufferType_; enum v4l2_memory memoryType_; diff --git a/src/libcamera/v4l2_pixelformat.cpp b/src/libcamera/v4l2_pixelformat.cpp index 90c8fa8d9aae..264ca80aab27 100644 --- a/src/libcamera/v4l2_pixelformat.cpp +++ b/src/libcamera/v4l2_pixelformat.cpp @@ -187,6 +187,28 @@ const std::map vpf2pf{ } /* namespace */ +/** + * \class V4L2PixelFormat::Hasher + * \brief Function object class that implement hash function for the + * V4L2PixelFormat class + * + * The Hasher class is a function object class that that allows to use the + * V4L2PixelFormat class with C++ STL unordered associative containers. + * + * The hash for a V4L2PixelFormat is obtained by using the function call + * operator(). + */ + +/** + * \fn V4L2PixelFormat::Hasher::operator()(const V4L2PixelFormat &f) const + * \brief Function call operator that computes the V4L2PixelFormat hash value + * \param[in] f The V4L2PixelFormat to compute the hash on + * + * Compute the hash value of \a f, which simply is the numerical FourCC value. + * + * \return The numerical FourCC value for \a f + */ + /** * \struct V4L2PixelFormat::Info * \brief Information about a V4L2 pixel format @@ -208,6 +230,13 @@ const std::map vpf2pf{ * invalid, calling the isValid() function returns false. */ +/** + * \fn V4L2PixelFormat::operator==() + * \brief Compare two V4L2PixelFormat by comparing their FourCC codes + * \param[in] other The other V4L2PixelFormat to compare with + * \return True if the FourCC codes are the same, false otherwise + */ + /** * \fn V4L2PixelFormat::V4L2PixelFormat(uint32_t fourcc) * \brief Construct a V4L2PixelFormat from a FourCC value diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 2ca22f485d45..c3343ca7078b 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -629,18 +629,14 @@ int V4L2VideoDevice::open() fdBufferNotifier_->activated.connect(this, &V4L2VideoDevice::bufferAvailable); fdBufferNotifier_->setEnabled(false); + ret = initFormats(); + if (ret) + return ret; + LOG(V4L2, Debug) << "Opened device " << caps_.bus_info() << ": " << caps_.driver() << ": " << caps_.card(); - ret = getFormat(&format_); - if (ret) { - LOG(V4L2, Error) << "Failed to get format"; - return ret; - } - - formatInfo_ = &PixelFormatInfo::info(format_.fourcc); - return 0; } @@ -722,11 +718,25 @@ int V4L2VideoDevice::open(SharedFD handle, enum v4l2_buf_type type) fdBufferNotifier_->activated.connect(this, &V4L2VideoDevice::bufferAvailable); fdBufferNotifier_->setEnabled(false); + ret = initFormats(); + if (ret) + return ret; + LOG(V4L2, Debug) << "Opened device " << caps_.bus_info() << ": " << caps_.driver() << ": " << caps_.card(); - ret = getFormat(&format_); + return 0; +} + +int V4L2VideoDevice::initFormats() +{ + const std::vector &deviceFormats = enumPixelformats(0); + pixelFormats_ = std::unordered_set + (deviceFormats.begin(), deviceFormats.end()); + + int ret = getFormat(&format_); if (ret) { LOG(V4L2, Error) << "Failed to get format"; return ret; @@ -1990,17 +2000,37 @@ V4L2VideoDevice::fromEntityName(const MediaDevice *media, } /** - * \brief Convert \a PixelFormat to its corresponding V4L2 FourCC + * \brief Convert \a PixelFormat to a V4L2PixelFormat supported by the device * \param[in] pixelFormat The PixelFormat to convert * - * The V4L2 format variant the function returns the contiguous version - * unconditionally. + * Convert \a pixelformat to a V4L2 FourCC that is known to be supported by + * the video device. * - * \return The V4L2_PIX_FMT_* pixel format code corresponding to \a pixelFormat + * A V4L2VideoDevice may support different V4L2 pixel formats that map the same + * PixelFormat. This is the case of the contiguous and non-contiguous variants + * of multiplanar formats, and with the V4L2 MJPEG and JPEG pixel formats. + * Converting a PixelFormat to a V4L2PixelFormat may thus have multiple answers. + * + * This function converts the \a pixelFormat using the list of V4L2 pixel + * formats that the V4L2VideoDevice supports. This guarantees that the returned + * V4L2PixelFormat will be valid for the device. If multiple matches are still + * possible, contiguous variants are preferred. If the \a pixelFormat is not + * supported by the device, the function returns an invalid V4L2PixelFormat. + * + * \return The V4L2PixelFormat corresponding to \a pixelFormat if supported by + * the device, or an invalid V4L2PixelFormat otherwise */ V4L2PixelFormat V4L2VideoDevice::toV4L2PixelFormat(const PixelFormat &pixelFormat) const { - return V4L2PixelFormat::fromPixelFormat(pixelFormat)[0]; + const std::vector &v4l2PixelFormats = + V4L2PixelFormat::fromPixelFormat(pixelFormat); + + for (const V4L2PixelFormat &v4l2Format : v4l2PixelFormats) { + if (pixelFormats_.count(v4l2Format)) + return v4l2Format; + } + + return {}; } /**