From patchwork Thu Apr 30 03:07:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 3630 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BE8AC613F6 for ; Thu, 30 Apr 2020 05:07:29 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="RAdiqFJe"; dkim-atps=neutral Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1DBEC503 for ; Thu, 30 Apr 2020 05:07:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1588216049; bh=IjCYSCJYvuIdU9TTcvdZO2iIDTHGLKZPr7jhP41Xhzs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=RAdiqFJeXhtd5jqNrpODWoiErwi/1rTFOd28amnJqKLJ2TkcdvkhV5Br42KvU6VV7 fx8Bh7KWw9/kBmmY2/SNWGbazTwazdC565a6smt4WtZksCZMj0qIzfr2T/u7pdxIv/ q3akpUNyuFMc7Gox5UoNp5jfvkGf3mTOP6vB2K+I= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Thu, 30 Apr 2020 06:07:21 +0300 Message-Id: <20200430030723.8908-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.25.3 In-Reply-To: <20200430030723.8908-1-laurent.pinchart@ideasonboard.com> References: <20200430030723.8908-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 4/6] libcamera: formats: Expose PixelFormatInfo as an internal API 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-List-Received-Date: Thu, 30 Apr 2020 03:07:31 -0000 To prepare for storing more information about pixel formats in PixelFormatInfo, move the class to formats.cpp and document it. The pixel formats database is moved to the same file, and a new static function is added to PixelFormatInfo to retrieve a PixelFormatInfo for a PixelFormat. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/libcamera/formats.cpp | 126 +++++++++++++++++++++++++++++ src/libcamera/include/formats.h | 15 ++++ src/libcamera/v4l2_pixelformat.cpp | 75 +---------------- 3 files changed, 144 insertions(+), 72 deletions(-) diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp index 5f6552a4e06c..4a351020b0d9 100644 --- a/src/libcamera/formats.cpp +++ b/src/libcamera/formats.cpp @@ -9,6 +9,8 @@ #include +#include "log.h" + /** * \file formats.h * \brief Types and helper methods to handle libcamera image formats @@ -16,6 +18,8 @@ namespace libcamera { +LOG_DEFINE_CATEGORY(Formats) + /** * \class ImageFormats * \brief Describe V4L2Device and V4L2SubDevice image formats @@ -104,4 +108,126 @@ const std::map> &ImageFormats::data() const return data_; } +/** + * \class PixelFormatInfo + * \brief Information about pixel formats + * + * The PixelFormatInfo class groups together information describing a pixel + * format. It facilitates handling of pixel formats by providing data commonly + * used in pipeline handlers. + * + * \var PixelFormatInfo::format + * \brief The PixelFormat described by this instance + * + * \var PixelFormatInfo::v4l2Format + * \brief The V4L2 pixel format corresponding to the PixelFormat + */ + +namespace { + +const std::map pixelFormatInfo{ + /* RGB formats. */ + { PixelFormat(DRM_FORMAT_BGR888), { + .format = PixelFormat(DRM_FORMAT_BGR888), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_RGB24), + } }, + { PixelFormat(DRM_FORMAT_RGB888), { + .format = PixelFormat(DRM_FORMAT_RGB888), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_BGR24), + } }, + { PixelFormat(DRM_FORMAT_ABGR8888), { + .format = PixelFormat(DRM_FORMAT_ABGR8888), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_RGBA32), + } }, + { PixelFormat(DRM_FORMAT_ARGB8888), { + .format = PixelFormat(DRM_FORMAT_ARGB8888), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_ABGR32), + } }, + { PixelFormat(DRM_FORMAT_BGRA8888), { + .format = PixelFormat(DRM_FORMAT_BGRA8888), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_ARGB32), + } }, + { PixelFormat(DRM_FORMAT_RGBA8888), { + .format = PixelFormat(DRM_FORMAT_RGBA8888), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_BGRA32), + } }, + + /* YUV packed formats. */ + { PixelFormat(DRM_FORMAT_YUYV), { + .format = PixelFormat(DRM_FORMAT_YUYV), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_YUYV), + } }, + { PixelFormat(DRM_FORMAT_YVYU), { + .format = PixelFormat(DRM_FORMAT_YVYU), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_YVYU), + } }, + { PixelFormat(DRM_FORMAT_UYVY), { + .format = PixelFormat(DRM_FORMAT_UYVY), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_UYVY), + } }, + { PixelFormat(DRM_FORMAT_VYUY), { + .format = PixelFormat(DRM_FORMAT_VYUY), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_VYUY), + } }, + + /* YUV planar formats. */ + { PixelFormat(DRM_FORMAT_NV16), { + .format = PixelFormat(DRM_FORMAT_NV16), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_NV16), + } }, + { PixelFormat(DRM_FORMAT_NV61), { + .format = PixelFormat(DRM_FORMAT_NV61), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_NV61), + } }, + { PixelFormat(DRM_FORMAT_NV12), { + .format = PixelFormat(DRM_FORMAT_NV12), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_NV12), + } }, + { PixelFormat(DRM_FORMAT_NV21), { + .format = PixelFormat(DRM_FORMAT_NV21), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_NV21), + } }, + + /* Greyscale formats. */ + { PixelFormat(DRM_FORMAT_R8), { + .format = PixelFormat(DRM_FORMAT_R8), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_GREY), + } }, + + /* Compressed formats. */ + { PixelFormat(DRM_FORMAT_MJPEG), { + .format = PixelFormat(DRM_FORMAT_MJPEG), + .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_MJPEG), + } }, +}; + +} /* namespace */ + +/** + * \fn bool PixelFormatInfo::isValid() const + * \brief Check if the pixel format info is valid + * \return True if the pixel format info is valid, false otherwise + */ + +/** + * \brief Retrieve information about a pixel format + * \param[in] format The pixel format + * \return The PixelFormatInfo describing the \a format if known, or an invalid + * PixelFormatInfo otherwise + */ +const PixelFormatInfo &PixelFormatInfo::info(const PixelFormat &format) +{ + static const PixelFormatInfo invalid{}; + + const auto iter = pixelFormatInfo.find(format); + if (iter == pixelFormatInfo.end()) { + LOG(Formats, Warning) + << "Unsupported pixel format " + << format.toString(); + return invalid; + } + + return iter->second; +} + } /* namespace libcamera */ diff --git a/src/libcamera/include/formats.h b/src/libcamera/include/formats.h index f43bc8c004f6..560df07c4451 100644 --- a/src/libcamera/include/formats.h +++ b/src/libcamera/include/formats.h @@ -12,6 +12,9 @@ #include #include +#include + +#include "v4l2_pixelformat.h" namespace libcamera { @@ -29,6 +32,18 @@ private: std::map> data_; }; +class PixelFormatInfo +{ +public: + bool isValid() const { return format.isValid(); } + + static const PixelFormatInfo &info(const PixelFormat &format); + + /* \todo Add support for non-contiguous memory planes */ + PixelFormat format; + V4L2PixelFormat v4l2Format; +}; + } /* namespace libcamera */ #endif /* __LIBCAMERA_FORMATS_H__ */ diff --git a/src/libcamera/v4l2_pixelformat.cpp b/src/libcamera/v4l2_pixelformat.cpp index e1c96b9862c3..580c0fc9d983 100644 --- a/src/libcamera/v4l2_pixelformat.cpp +++ b/src/libcamera/v4l2_pixelformat.cpp @@ -16,6 +16,7 @@ #include +#include "formats.h" #include "log.h" /** @@ -43,71 +44,6 @@ LOG_DECLARE_CATEGORY(V4L2) namespace { -struct PixelFormatInfo { - /* \todo Add support for non-contiguous memory planes */ - V4L2PixelFormat v4l2Format; -}; - -const std::map pf2vpf{ - /* RGB formats. */ - { PixelFormat(DRM_FORMAT_BGR888), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_RGB24), - } }, - { PixelFormat(DRM_FORMAT_RGB888), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_BGR24), - } }, - { PixelFormat(DRM_FORMAT_ABGR8888), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_RGBA32), - } }, - { PixelFormat(DRM_FORMAT_ARGB8888), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_ABGR32), - } }, - { PixelFormat(DRM_FORMAT_BGRA8888), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_ARGB32), - } }, - { PixelFormat(DRM_FORMAT_RGBA8888), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_BGRA32), - } }, - - /* YUV packed formats. */ - { PixelFormat(DRM_FORMAT_YUYV), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_YUYV), - } }, - { PixelFormat(DRM_FORMAT_YVYU), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_YVYU), - } }, - { PixelFormat(DRM_FORMAT_UYVY), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_UYVY), - } }, - { PixelFormat(DRM_FORMAT_VYUY), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_VYUY), - } }, - - /* YUV planar formats. */ - { PixelFormat(DRM_FORMAT_NV16), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_NV16), - } }, - { PixelFormat(DRM_FORMAT_NV61), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_NV61), - } }, - { PixelFormat(DRM_FORMAT_NV12), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_NV12), - } }, - { PixelFormat(DRM_FORMAT_NV21), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_NV21), - } }, - - /* Greyscale formats. */ - { PixelFormat(DRM_FORMAT_R8), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_GREY), - } }, - - /* Compressed formats. */ - { PixelFormat(DRM_FORMAT_MJPEG), { - .v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_MJPEG), - } }, -}; - const std::map vpf2pf{ /* RGB formats. */ { V4L2PixelFormat(V4L2_PIX_FMT_RGB24), PixelFormat(DRM_FORMAT_BGR888) }, @@ -233,15 +169,10 @@ PixelFormat V4L2PixelFormat::toPixelFormat() const V4L2PixelFormat V4L2PixelFormat::fromPixelFormat(const PixelFormat &pixelFormat, bool multiplanar) { - const auto iter = pf2vpf.find(pixelFormat); - if (iter == pf2vpf.end()) { - LOG(V4L2, Warning) - << "Unsupported pixel format " - << pixelFormat.toString(); + const PixelFormatInfo &info = PixelFormatInfo::info(pixelFormat); + if (!info.isValid()) return V4L2PixelFormat(); - } - const PixelFormatInfo &info = iter->second; return info.v4l2Format; }