{"id":16744,"url":"https://patchwork.libcamera.org/api/1.1/patches/16744/?format=json","web_url":"https://patchwork.libcamera.org/patch/16744/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20220723095330.43542-6-jacopo@jmondi.org>","date":"2022-07-23T09:53:27","name":"[libcamera-devel,v2,5/8] libcamera: v4l2_videodevice: Match formats supported by the device","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"adaafe8f51b2ba29f881bd30202b0ee21acc6d4a","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/1.1/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16744/mbox/","series":[{"id":3320,"url":"https://patchwork.libcamera.org/api/1.1/series/3320/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3320","date":"2022-07-23T09:53:22","name":"libcamera: Map multiple V4L2 formats to a single libcamera::format","version":2,"mbox":"https://patchwork.libcamera.org/series/3320/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16744/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16744/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 BBED7BE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 23 Jul 2022 09:54:13 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 789AD63318;\n\tSat, 23 Jul 2022 11:54:13 +0200 (CEST)","from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net\n\t[IPv6:2001:4b98:dc4:8::228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 42A9D6330E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 23 Jul 2022 11:54:12 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby mail.gandi.net (Postfix) with ESMTPSA id 3BFE01BF207;\n\tSat, 23 Jul 2022 09:53:57 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1658570053;\n\tbh=b8tt0KPTmJnOfdIuY69kIJODREf8ppGAjSdnD/iuccw=;\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=QshmGr3oH+eydLjFKM7lpg0cxGDV5AmpBF9RG5Rug07s9W/3spwMtFvQZP2UaxV41\n\t/r7BhEt1Z/t9/XrjgYfwaY82rcd7zHwRz8aMj3kFyjIrzsxSEoOhEj3diR/Y0QZSEd\n\tc4bMNN18sQ3/6h/tXBuDEjaiHBEwQYrLGuHaRPj+K3QDRwf9pdo8tW7iLv/8s/YIZz\n\tpNmCrPdCCOG2laeE8EF4waLpgMU08kqevjVdGQEH+wsSEz9uy/f58FU7T69x2qai1x\n\tFJ+sJYr4VbZKgPTLaZpOsRC2d50YGfXM6SzMezUXQnrcz9SZj8ZAa9RBZTJTZEgh7C\n\tNwEsLe+GUESQA==","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 23 Jul 2022 11:53:27 +0200","Message-Id":"<20220723095330.43542-6-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.37.1","In-Reply-To":"<20220723095330.43542-1-jacopo@jmondi.org>","References":"<20220723095330.43542-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 5/8] 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 by checking which ones are actually supported by the video\ndevice.\n\nThe first format found to match one of the device supported ones is\nreturned.\n\nAs V4L2VideoDevice::toV4L2PixelFormat() is a const function which uses\nother functions of the class, those functions has to be made const as\nwell.\n\nIn particular:\n\n- enumPixelformats() and enumSizes() do not modify the class state and\n  can safely be made const.\n- formats() uses the above functions and does not itself modify the\n  class state and can be made const\n- add a const version of V4L2Device::ioctl() to be used by the now const\n  functions\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n include/libcamera/internal/v4l2_device.h      |  1 +\n include/libcamera/internal/v4l2_videodevice.h |  6 ++--\n src/libcamera/v4l2_device.cpp                 | 15 +++++++++\n src/libcamera/v4l2_videodevice.cpp            | 31 ++++++++++++++-----\n 4 files changed, 43 insertions(+), 10 deletions(-)","diff":"diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h\nindex a52a5f2c99f9..f7ec3c7004a6 100644\n--- a/include/libcamera/internal/v4l2_device.h\n+++ b/include/libcamera/internal/v4l2_device.h\n@@ -55,6 +55,7 @@ protected:\n \tint setFd(UniqueFD fd);\n \n \tint ioctl(unsigned long request, void *argp);\n+\tint ioctl(unsigned long request, void *argp) const;\n \n \tint fd() const { return fd_.get(); }\n \ndiff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\nindex 29fa0bbaf670..6d8850c99afd 100644\n--- a/include/libcamera/internal/v4l2_videodevice.h\n+++ b/include/libcamera/internal/v4l2_videodevice.h\n@@ -205,7 +205,7 @@ public:\n \tint getFormat(V4L2DeviceFormat *format);\n \tint tryFormat(V4L2DeviceFormat *format);\n \tint setFormat(V4L2DeviceFormat *format);\n-\tFormats formats(uint32_t code = 0);\n+\tFormats formats(uint32_t code = 0) const;\n \n \tint setSelection(unsigned int target, Rectangle *rect);\n \n@@ -251,8 +251,8 @@ private:\n \tint getFormatSingleplane(V4L2DeviceFormat *format);\n \tint trySetFormatSingleplane(V4L2DeviceFormat *format, bool set);\n \n-\tstd::vector<V4L2PixelFormat> enumPixelformats(uint32_t code);\n-\tstd::vector<SizeRange> enumSizes(V4L2PixelFormat pixelFormat);\n+\tstd::vector<V4L2PixelFormat> enumPixelformats(uint32_t code) const;\n+\tstd::vector<SizeRange> enumSizes(V4L2PixelFormat pixelFormat) const;\n \n \tint requestBuffers(unsigned int count, enum v4l2_memory memoryType);\n \tint createBuffers(unsigned int count,\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex 3fc8438f6579..59f92403db80 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -459,6 +459,21 @@ int V4L2Device::ioctl(unsigned long request, void *argp)\n \treturn 0;\n }\n \n+/**\n+ * \\copydoc ioctl()\n+ */\n+int V4L2Device::ioctl(unsigned long request, void *argp) const\n+{\n+\t/*\n+\t * Printing out an error message is usually better performed\n+\t * in the caller, which can provide more context.\n+\t */\n+\tif (::ioctl(fd_.get(), request, argp) < 0)\n+\t\treturn -errno;\n+\n+\treturn 0;\n+}\n+\n /**\n  * \\fn V4L2Device::deviceNode()\n  * \\brief Retrieve the device node path\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 43c3d0f69266..a3242ba755c0 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -1045,7 +1045,7 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set)\n  *\n  * \\return A list of the supported video device formats\n  */\n-V4L2VideoDevice::Formats V4L2VideoDevice::formats(uint32_t code)\n+V4L2VideoDevice::Formats V4L2VideoDevice::formats(uint32_t code) const\n {\n \tFormats formats;\n \n@@ -1067,7 +1067,7 @@ V4L2VideoDevice::Formats V4L2VideoDevice::formats(uint32_t code)\n \treturn formats;\n }\n \n-std::vector<V4L2PixelFormat> V4L2VideoDevice::enumPixelformats(uint32_t code)\n+std::vector<V4L2PixelFormat> V4L2VideoDevice::enumPixelformats(uint32_t code) const\n {\n \tstd::vector<V4L2PixelFormat> formats;\n \tint ret;\n@@ -1101,7 +1101,7 @@ std::vector<V4L2PixelFormat> V4L2VideoDevice::enumPixelformats(uint32_t code)\n \treturn formats;\n }\n \n-std::vector<SizeRange> V4L2VideoDevice::enumSizes(V4L2PixelFormat pixelFormat)\n+std::vector<SizeRange> V4L2VideoDevice::enumSizes(V4L2PixelFormat pixelFormat) const\n {\n \tstd::vector<SizeRange> sizes;\n \tint ret;\n@@ -1990,20 +1990,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 one of the device supported V4L2 FourCC\n  * \\param[in] pixelFormat The PixelFormat to convert\n  *\n+ * Convert a\\ pixelformat to a V4L2 FourCC that is known to be supported by\n+ * the video device.\n+ *\n  * For multiplanar formats, the V4L2 format variant (contiguous or\n  * non-contiguous planes) is selected automatically based on the capabilities\n  * of the video device. If the video device supports the V4L2 multiplanar API,\n  * non-contiguous formats are preferred.\n  *\n- * \\return The V4L2_PIX_FMT_* pixel format code corresponding to \\a pixelFormat\n+ * \\return The V4L2PixelFormat corresponding to \\a pixelFormat or an invalid\n+ * PixelFormat if \\a pixelFormat is not supported by the video device\n  */\n V4L2PixelFormat V4L2VideoDevice::toV4L2PixelFormat(const PixelFormat &pixelFormat) const\n {\n-\treturn V4L2PixelFormat::fromPixelFormat(pixelFormat,\n-\t\t\t\t\t\tcaps_.isMultiplanar())[0];\n+\tstd::vector<V4L2PixelFormat> deviceFormats = enumPixelformats(0);\n+\tstd::vector<V4L2PixelFormat> v4l2PixelFormats =\n+\t\tV4L2PixelFormat::fromPixelFormat(pixelFormat, caps_.isMultiplanar());\n+\n+\tfor (const V4L2PixelFormat &v4l2Format : v4l2PixelFormats) {\n+\t\tauto it = std::find_if(deviceFormats.begin(), deviceFormats.end(),\n+\t\t\t\t       [&v4l2Format](const V4L2PixelFormat &deviceFormat) {\n+\t\t\t\t\t       return v4l2Format == deviceFormat;\n+\t\t\t\t       });\n+\n+\t\tif (it != deviceFormats.end())\n+\t\t\treturn v4l2Format;\n+\t}\n+\n+\treturn {};\n }\n \n /**\n","prefixes":["libcamera-devel","v2","5/8"]}