{"id":16915,"url":"https://patchwork.libcamera.org/api/patches/16915/?format=json","web_url":"https://patchwork.libcamera.org/patch/16915/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220802160136.63075-6-jacopo@jmondi.org>","date":"2022-08-02T16:01:35","name":"[libcamera-devel,v4,5/6] libcamera: v4l2_videodevice: Match formats supported by the device","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"24b43b2d7483fcaf21a4fc6e72d5d20cc4086587","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16915/mbox/","series":[{"id":3359,"url":"https://patchwork.libcamera.org/api/series/3359/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3359","date":"2022-08-02T16:01:30","name":"libcamera: Map multiple V4L2 formats to a single libcamera::format","version":4,"mbox":"https://patchwork.libcamera.org/series/3359/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16915/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16915/checks/","tags":{},"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 1A3DBBE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  2 Aug 2022 16:02:00 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C6F026331C;\n\tTue,  2 Aug 2022 18:01:59 +0200 (CEST)","from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net\n\t[IPv6:2001:4b98:dc4:8::222])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8D5C56331C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  2 Aug 2022 18:01:58 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby mail.gandi.net (Postfix) with ESMTPSA id D0C3F40012;\n\tTue,  2 Aug 2022 16:01:56 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1659456119;\n\tbh=y0PMnYQX1wrw58d60A9bYt6GRMkj2iIQ+C7cJcZmJWk=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=0PkiEsmqqnNG/vNs14tK9dC/NQ6TdQ+WkpwlhL0RBVZZSOx1MZ9MgBG8is1G6rdBl\n\tt+qoCQe+BsNYOli7pOvlK1F7xnDNFiiNl72/ca71g3KZW+R4SoGlFNjYujybK0GPj/\n\t0LzKJBPy9B6qmAmsLs+cw0yfylhRwkvZp8Zxm42ntrKBbO1vey9lfGSKwq1X9zobAv\n\tp/qVuen55enuw2RUwEkhg+RIorh1OFz2ySv4XPy+dQbHc/3FbCGDCe07HNjIrp0JdG\n\tqRSRjitDHXjIpUyMtWiRN1Xow5or6OzfRZXTk9kkHFiyDk3zUfY989FclBO3mwCMzU\n\tiFXq9Zlcr4Zvg==","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 5/6] libcamera: v4l2_videodevice: Match\n\tformats supported by the device","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>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"jozef@mlich.cz, Pavel Machek <pavel@ucw.cz>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Now that V4L2PixelFormat::fromPixelFormat() returns a list of formats\nto chose from, select the one supported by the video device by matching\nagainst the list of supported pixel formats.\n\nThe first format found to match one of the device supported ones is\nreturned.\n\nAs the list of pixel formats supported by the video device does not\nchange at run-time, cache it at device open() time. To maximize the\nlookup efficiency store the list of supported V4L2PixelFormat in an\nstd::unordered_set<> which requires the V4L2PixelFormat class to be\nassociated with an hash function object and to support the comparison\noperator.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n include/libcamera/internal/v4l2_pixelformat.h | 15 +++++\n include/libcamera/internal/v4l2_videodevice.h |  4 ++\n src/libcamera/v4l2_pixelformat.cpp            | 29 ++++++++++\n src/libcamera/v4l2_videodevice.cpp            | 58 ++++++++++++++-----\n 4 files changed, 92 insertions(+), 14 deletions(-)","diff":"diff --git a/include/libcamera/internal/v4l2_pixelformat.h b/include/libcamera/internal/v4l2_pixelformat.h\nindex d5400f90a67e..05069a30304d 100644\n--- a/include/libcamera/internal/v4l2_pixelformat.h\n+++ b/include/libcamera/internal/v4l2_pixelformat.h\n@@ -8,6 +8,7 @@\n \n #pragma once\n \n+#include <functional>\n #include <ostream>\n #include <stdint.h>\n #include <string>\n@@ -22,6 +23,15 @@ namespace libcamera {\n class V4L2PixelFormat\n {\n public:\n+\tclass Hasher\n+\t{\n+\tpublic:\n+\t\tstd::size_t operator()(const V4L2PixelFormat &f) const\n+\t\t{\n+\t\t\treturn f.fourcc();\n+\t\t}\n+\t};\n+\n \tstruct Info {\n \t\tPixelFormat format;\n \t\tconst char *description;\n@@ -37,6 +47,11 @@ public:\n \t{\n \t}\n \n+\tbool operator==(const V4L2PixelFormat &other) const\n+\t{\n+\t\treturn fourcc_ == other.fourcc_;\n+\t}\n+\n \tbool isValid() const { return fourcc_ != 0; }\n \tuint32_t fourcc() const { return fourcc_; }\n \toperator uint32_t() const { return fourcc_; }\ndiff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\nindex 29fa0bbaf670..c9064291a669 100644\n--- a/include/libcamera/internal/v4l2_videodevice.h\n+++ b/include/libcamera/internal/v4l2_videodevice.h\n@@ -14,6 +14,7 @@\n #include <ostream>\n #include <stdint.h>\n #include <string>\n+#include <unordered_set>\n #include <vector>\n \n #include <linux/videodev2.h>\n@@ -242,6 +243,8 @@ private:\n \t\tStopped,\n \t};\n \n+\tint initFormats();\n+\n \tint getFormatMeta(V4L2DeviceFormat *format);\n \tint trySetFormatMeta(V4L2DeviceFormat *format, bool set);\n \n@@ -268,6 +271,7 @@ private:\n \tV4L2Capability caps_;\n \tV4L2DeviceFormat format_;\n \tconst PixelFormatInfo *formatInfo_;\n+\tstd::unordered_set<V4L2PixelFormat, V4L2PixelFormat::Hasher> pixelFormats_;\n \n \tenum v4l2_buf_type bufferType_;\n \tenum v4l2_memory memoryType_;\ndiff --git a/src/libcamera/v4l2_pixelformat.cpp b/src/libcamera/v4l2_pixelformat.cpp\nindex 90c8fa8d9aae..264ca80aab27 100644\n--- a/src/libcamera/v4l2_pixelformat.cpp\n+++ b/src/libcamera/v4l2_pixelformat.cpp\n@@ -187,6 +187,28 @@ const std::map<V4L2PixelFormat, V4L2PixelFormat::Info> vpf2pf{\n \n } /* namespace */\n \n+/**\n+ * \\class V4L2PixelFormat::Hasher\n+ * \\brief Function object class that implement hash function for the\n+ * V4L2PixelFormat class\n+ *\n+ * The Hasher class is a function object class that that allows to use the\n+ * V4L2PixelFormat class with C++ STL unordered associative containers.\n+ *\n+ * The hash for a V4L2PixelFormat is obtained by using the function call\n+ * operator().\n+ */\n+\n+/**\n+ * \\fn V4L2PixelFormat::Hasher::operator()(const V4L2PixelFormat &f) const\n+ * \\brief Function call operator that computes the V4L2PixelFormat hash value\n+ * \\param[in] f The V4L2PixelFormat to compute the hash on\n+ *\n+ * Compute the hash value of \\a f, which simply is the numerical FourCC value.\n+ *\n+ * \\return The numerical FourCC value for \\a f\n+ */\n+\n /**\n  * \\struct V4L2PixelFormat::Info\n  * \\brief Information about a V4L2 pixel format\n@@ -208,6 +230,13 @@ const std::map<V4L2PixelFormat, V4L2PixelFormat::Info> vpf2pf{\n  * invalid, calling the isValid() function returns false.\n  */\n \n+/**\n+ * \\fn V4L2PixelFormat::operator==()\n+ * \\brief Compare two V4L2PixelFormat by comparing their FourCC codes\n+ * \\param[in] other The other V4L2PixelFormat to compare with\n+ * \\return True if the FourCC codes are the same, false otherwise\n+ */\n+\n /**\n  * \\fn V4L2PixelFormat::V4L2PixelFormat(uint32_t fourcc)\n  * \\brief Construct a V4L2PixelFormat from a FourCC value\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 2ca22f485d45..c3343ca7078b 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -629,18 +629,14 @@ int V4L2VideoDevice::open()\n \tfdBufferNotifier_->activated.connect(this, &V4L2VideoDevice::bufferAvailable);\n \tfdBufferNotifier_->setEnabled(false);\n \n+\tret = initFormats();\n+\tif (ret)\n+\t\treturn ret;\n+\n \tLOG(V4L2, Debug)\n \t\t<< \"Opened device \" << caps_.bus_info() << \": \"\n \t\t<< caps_.driver() << \": \" << caps_.card();\n \n-\tret = getFormat(&format_);\n-\tif (ret) {\n-\t\tLOG(V4L2, Error) << \"Failed to get format\";\n-\t\treturn ret;\n-\t}\n-\n-\tformatInfo_ = &PixelFormatInfo::info(format_.fourcc);\n-\n \treturn 0;\n }\n \n@@ -722,11 +718,25 @@ int V4L2VideoDevice::open(SharedFD handle, enum v4l2_buf_type type)\n \tfdBufferNotifier_->activated.connect(this, &V4L2VideoDevice::bufferAvailable);\n \tfdBufferNotifier_->setEnabled(false);\n \n+\tret = initFormats();\n+\tif (ret)\n+\t\treturn ret;\n+\n \tLOG(V4L2, Debug)\n \t\t<< \"Opened device \" << caps_.bus_info() << \": \"\n \t\t<< caps_.driver() << \": \" << caps_.card();\n \n-\tret = getFormat(&format_);\n+\treturn 0;\n+}\n+\n+int V4L2VideoDevice::initFormats()\n+{\n+\tconst std::vector<V4L2PixelFormat> &deviceFormats = enumPixelformats(0);\n+\tpixelFormats_ = std::unordered_set<V4L2PixelFormat,\n+\t\t\t\t\t   V4L2PixelFormat::Hasher>\n+\t\t\t(deviceFormats.begin(), deviceFormats.end());\n+\n+\tint ret = getFormat(&format_);\n \tif (ret) {\n \t\tLOG(V4L2, Error) << \"Failed to get format\";\n \t\treturn ret;\n@@ -1990,17 +2000,37 @@ V4L2VideoDevice::fromEntityName(const MediaDevice *media,\n }\n \n /**\n- * \\brief Convert \\a PixelFormat to its corresponding V4L2 FourCC\n+ * \\brief Convert \\a PixelFormat to a V4L2PixelFormat supported by the device\n  * \\param[in] pixelFormat The PixelFormat to convert\n  *\n- * The V4L2 format variant the function returns the contiguous version\n- * unconditionally.\n+ * Convert \\a pixelformat to a V4L2 FourCC that is known to be supported by\n+ * the video device.\n  *\n- * \\return The V4L2_PIX_FMT_* pixel format code corresponding to \\a pixelFormat\n+ * A V4L2VideoDevice may support different V4L2 pixel formats that map the same\n+ * PixelFormat. This is the case of the contiguous and non-contiguous variants\n+ * of multiplanar formats, and with the V4L2 MJPEG and JPEG pixel formats.\n+ * Converting a PixelFormat to a V4L2PixelFormat may thus have multiple answers.\n+ *\n+ * This function converts the \\a pixelFormat using the list of V4L2 pixel\n+ * formats that the V4L2VideoDevice supports. This guarantees that the returned\n+ * V4L2PixelFormat will be valid for the device. If multiple matches are still\n+ * possible, contiguous variants are preferred. If the \\a pixelFormat is not\n+ * supported by the device, the function returns an invalid V4L2PixelFormat.\n+ *\n+ * \\return The V4L2PixelFormat corresponding to \\a pixelFormat if supported by\n+ * the device, or an invalid V4L2PixelFormat otherwise\n  */\n V4L2PixelFormat V4L2VideoDevice::toV4L2PixelFormat(const PixelFormat &pixelFormat) const\n {\n-\treturn V4L2PixelFormat::fromPixelFormat(pixelFormat)[0];\n+\tconst std::vector<V4L2PixelFormat> &v4l2PixelFormats =\n+\t\tV4L2PixelFormat::fromPixelFormat(pixelFormat);\n+\n+\tfor (const V4L2PixelFormat &v4l2Format : v4l2PixelFormats) {\n+\t\tif (pixelFormats_.count(v4l2Format))\n+\t\t\treturn v4l2Format;\n+\t}\n+\n+\treturn {};\n }\n \n /**\n","prefixes":["libcamera-devel","v4","5/6"]}