[{"id":24251,"web_url":"https://patchwork.libcamera.org/comment/24251/","msgid":"<YuQpec9k1prAU12m@pendragon.ideasonboard.com>","date":"2022-07-29T18:39:53","subject":"Re: [libcamera-devel] [PATCH v3 5/6] libcamera: v4l2_videodevice:\n\tMatch formats supported by the device","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Fri, Jul 29, 2022 at 06:00:13PM +0200, Jacopo Mondi via libcamera-devel wrote:\n> Now that V4L2PixelFormat::fromPixelFormat() returns a list of formats\n> to chose from, select the one supported by the video device by matching\n> against the list of supported pixel formats.\n> \n> The first format found to match one of the device supported ones is\n> returned.\n> \n> As the list of pixel formats supported by the video device does not\n> change at run-time, cache it at device open() time.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n>  include/libcamera/internal/v4l2_videodevice.h |  1 +\n>  src/libcamera/v4l2_videodevice.cpp            | 28 +++++++++++++++----\n>  2 files changed, 24 insertions(+), 5 deletions(-)\n> \n> diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\n> index 29fa0bbaf670..6fe81d5e4cf0 100644\n> --- a/include/libcamera/internal/v4l2_videodevice.h\n> +++ b/include/libcamera/internal/v4l2_videodevice.h\n> @@ -268,6 +268,7 @@ private:\n>  \tV4L2Capability caps_;\n>  \tV4L2DeviceFormat format_;\n>  \tconst PixelFormatInfo *formatInfo_;\n> +\tstd::vector<V4L2PixelFormat> pixelFormats_;\n>  \n>  \tenum v4l2_buf_type bufferType_;\n>  \tenum v4l2_memory memoryType_;\n> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\n> index 2ca22f485d45..0aeaae6ad573 100644\n> --- a/src/libcamera/v4l2_videodevice.cpp\n> +++ b/src/libcamera/v4l2_videodevice.cpp\n> @@ -633,6 +633,8 @@ int V4L2VideoDevice::open()\n>  \t\t<< \"Opened device \" << caps_.bus_info() << \": \"\n>  \t\t<< caps_.driver() << \": \" << caps_.card();\n>  \n> +\tpixelFormats_ = enumPixelformats(0);\n> +\n>  \tret = getFormat(&format_);\n>  \tif (ret) {\n>  \t\tLOG(V4L2, Error) << \"Failed to get format\";\n> @@ -726,6 +728,8 @@ int V4L2VideoDevice::open(SharedFD handle, enum v4l2_buf_type type)\n>  \t\t<< \"Opened device \" << caps_.bus_info() << \": \"\n>  \t\t<< caps_.driver() << \": \" << caps_.card();\n>  \n> +\tpixelFormats_ = enumPixelformats(0);\n> +\n>  \tret = getFormat(&format_);\n>  \tif (ret) {\n>  \t\tLOG(V4L2, Error) << \"Failed to get format\";\n> @@ -1990,17 +1994,31 @@ 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\n * \\brief Convert \\a PixelFormat to a V4L2PixelFormat supported by the device\n\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\ns/a\\/\\a/\n\n> + * the video device.\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\ns/PixelFormat/V4L2PixelFormat/\n\nI'd expand this as it's also meant to cover the JPEG vs. MJPEG case:\n\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>   */\n>  V4L2PixelFormat V4L2VideoDevice::toV4L2PixelFormat(const PixelFormat &pixelFormat) const\n>  {\n> -\treturn V4L2PixelFormat::fromPixelFormat(pixelFormat)[0];\n> +\tstd::vector<V4L2PixelFormat> v4l2PixelFormats =\n\n\tconst std::vector<V4L2PixelFormat> &v4l2PixelFormats =\n\n> +\t\tV4L2PixelFormat::fromPixelFormat(pixelFormat);\n> +\n> +\tfor (const V4L2PixelFormat &v4l2Format : v4l2PixelFormats) {\n> +\t\tauto it = std::find_if(pixelFormats_.begin(), pixelFormats_.end(),\n> +\t\t\t\t       [&v4l2Format](const V4L2PixelFormat &devFormat) {\n> +\t\t\t\t\t       return v4l2Format == devFormat;\n> +\t\t\t\t       });\n> +\n> +\t\tif (it != pixelFormats_.end())\n> +\t\t\treturn v4l2Format;\n\nShould we convert pixelFormats_ to a std::unordered_set to make the\nlookup more efficient ?\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\t}\n> +\n> +\treturn {};\n>  }\n>  \n>  /**","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 9FBCAC3275\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 29 Jul 2022 18:39:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E4E7D63312;\n\tFri, 29 Jul 2022 20:39:58 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E7676603EC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 29 Jul 2022 20:39:56 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 490C86D4;\n\tFri, 29 Jul 2022 20:39:56 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1659119998;\n\tbh=ISeN67IpBXjzC+U7IGE+RddnVnKZZOctvesvafHbFPQ=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=Chi/ZBdL8kVSeSLYlQmHDsIwgrQjY6ve13gMUDphKrGjgAImbNXXCbssxwWIx7uuu\n\tecFp/XTNtCIY46ldX0ZREkPu5tYEULgsgOLL/n+aIKXSxJpw2gM7kkaB1U1JfnP2Tx\n\tAoY+UJBjmiOtOdLQtnlVNJMj7EGYqjiPVryXh+pXdf5RHYlPrbNq9X6PafexCXrCMI\n\tRnmeebLqcb1Me5rER5VktzMv1VjG0tu1/nUdoM5CH0S2xo+7t/dp2jHzr49aWby70b\n\t3DUbdEdUkxcrUzpR6+oYGXuwdJPtJEVoP1ZS5tFDMRBZ0VqtNq8IlEsHrZRqu0YDps\n\tU9GWDGpoNNa9Q==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1659119996;\n\tbh=ISeN67IpBXjzC+U7IGE+RddnVnKZZOctvesvafHbFPQ=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=LC/BZOighdU5EaB3qV/W+gO/J9Uot9KEXAm8Sn0xpMc4RVG4zoCaT+N+At7uU6HzI\n\t13DvphGTgyO5FUoMBOXNpmn9YIDF+o71wndrPfjF76qmC+cl2SfRE9JWL4xwD77TUk\n\tz1AJIlEiwzV4qC4Htk0cakmupame2ycHkdaY7XHA="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"LC/BZOig\"; dkim-atps=neutral","Date":"Fri, 29 Jul 2022 21:39:53 +0300","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<YuQpec9k1prAU12m@pendragon.ideasonboard.com>","References":"<20220729160014.101503-1-jacopo@jmondi.org>\n\t<20220729160014.101503-6-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220729160014.101503-6-jacopo@jmondi.org>","Subject":"Re: [libcamera-devel] [PATCH v3 5/6] libcamera: v4l2_videodevice:\n\tMatch formats 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":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org, jozef@mlich.cz,\n\tPavel Machek <pavel@ucw.cz>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]