From patchwork Fri Jul 29 16:00:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 16874 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 DF382C3276 for ; Fri, 29 Jul 2022 16:00:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 27E5863325; Fri, 29 Jul 2022 18:00:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1659110431; bh=xEIkQECEDo9mFcs3lL/UrHQOEcje4O278P0Pke+HUCY=; 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=AZ3Z3eG41+0oktmejd/01g0kg8UWWs9lWtvBESWIeZUK8WQw6LTIKtwWykCFm8acq gska1Rx5X2hODcCsqdZmJIshiLE+HVPR9JWHm+JORYSNSq+5KZCkNBz0A7Ps8IRwgo kBd0+o1FFlgGcqy9YEU5rIOwtiN7tJiaYICS8GykJSQ2Tz8I4e6T/tpLZ3CXwsfX5K 2cxKaHmOaa60GO4VVnS9EiXsF6dsSjDgJDob+R34HcWfyFiz/dIrh6ZaU6/RWOk9zn jv6pYQ4CjnptqTYdQ6t2Q1XSycWorHbgovNju7D1lPwq3ofLFF0kcm44ezPEn555O3 1Ad+3p8hiMYLQ== 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 4FDCE6330E for ; Fri, 29 Jul 2022 18:00:28 +0200 (CEST) Received: (Authenticated sender: jacopo@jmondi.org) by mail.gandi.net (Postfix) with ESMTPSA id 5690B4000B; Fri, 29 Jul 2022 16:00:27 +0000 (UTC) To: libcamera-devel@lists.libcamera.org Date: Fri, 29 Jul 2022 18:00:13 +0200 Message-Id: <20220729160014.101503-6-jacopo@jmondi.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220729160014.101503-1-jacopo@jmondi.org> References: <20220729160014.101503-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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. Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- include/libcamera/internal/v4l2_videodevice.h | 1 + src/libcamera/v4l2_videodevice.cpp | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index 29fa0bbaf670..6fe81d5e4cf0 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -268,6 +268,7 @@ private: V4L2Capability caps_; V4L2DeviceFormat format_; const PixelFormatInfo *formatInfo_; + std::vector pixelFormats_; enum v4l2_buf_type bufferType_; enum v4l2_memory memoryType_; diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 2ca22f485d45..0aeaae6ad573 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -633,6 +633,8 @@ int V4L2VideoDevice::open() << "Opened device " << caps_.bus_info() << ": " << caps_.driver() << ": " << caps_.card(); + pixelFormats_ = enumPixelformats(0); + ret = getFormat(&format_); if (ret) { LOG(V4L2, Error) << "Failed to get format"; @@ -726,6 +728,8 @@ int V4L2VideoDevice::open(SharedFD handle, enum v4l2_buf_type type) << "Opened device " << caps_.bus_info() << ": " << caps_.driver() << ": " << caps_.card(); + pixelFormats_ = enumPixelformats(0); + ret = getFormat(&format_); if (ret) { LOG(V4L2, Error) << "Failed to get format"; @@ -1990,17 +1994,31 @@ V4L2VideoDevice::fromEntityName(const MediaDevice *media, } /** - * \brief Convert \a PixelFormat to its corresponding V4L2 FourCC + * \brief Convert \a PixelFormat to one of the device supported V4L2 FourCC * \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 + * \return The V4L2PixelFormat corresponding to \a pixelFormat or an invalid + * PixelFormat if \a pixelFormat is not supported by the video device */ V4L2PixelFormat V4L2VideoDevice::toV4L2PixelFormat(const PixelFormat &pixelFormat) const { - return V4L2PixelFormat::fromPixelFormat(pixelFormat)[0]; + std::vector v4l2PixelFormats = + V4L2PixelFormat::fromPixelFormat(pixelFormat); + + for (const V4L2PixelFormat &v4l2Format : v4l2PixelFormats) { + auto it = std::find_if(pixelFormats_.begin(), pixelFormats_.end(), + [&v4l2Format](const V4L2PixelFormat &devFormat) { + return v4l2Format == devFormat; + }); + + if (it != pixelFormats_.end()) + return v4l2Format; + } + + return {}; } /**