[libcamera-devel] [PATCH 1/6] libcamera: bayer_format: Add conversion helpers to the BayerFormat class

Naushir Patuck naush at raspberrypi.com
Fri Oct 22 13:55:32 CEST 2021


Add a BayerFormat::toMbusCode() member function to convert a BayerFormat to an
mbus code defined by the V4L2 specification.

Add a BayerFormat::toPixelFormat() member function to convert a BayerFormat to a
PixelFormat type. This conversion uses the existing bayerToV4l2 conversion
table.

Signed-off-by: Naushir Patuck <naush at raspberrypi.com>
---
 include/libcamera/internal/bayer_format.h |  4 +++
 src/libcamera/bayer_format.cpp            | 31 +++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/include/libcamera/internal/bayer_format.h b/include/libcamera/internal/bayer_format.h
index 723382d4168d..217da54e90a2 100644
--- a/include/libcamera/internal/bayer_format.h
+++ b/include/libcamera/internal/bayer_format.h
@@ -10,6 +10,8 @@
 #include <stdint.h>
 #include <string>
 
+#include <libcamera/pixel_format.h>
+
 #include "libcamera/internal/v4l2_pixelformat.h"
 
 namespace libcamera {
@@ -43,6 +45,7 @@ public:
 	{
 	}
 
+	unsigned int toMbusCode() const;
 	static const BayerFormat &fromMbusCode(unsigned int mbusCode);
 	bool isValid() const { return bitDepth != 0; }
 
@@ -50,6 +53,7 @@ public:
 
 	V4L2PixelFormat toV4L2PixelFormat() const;
 	static BayerFormat fromV4L2PixelFormat(V4L2PixelFormat v4l2Format);
+	PixelFormat toPixelFormat() const;
 	BayerFormat transform(Transform t) const;
 
 	Order order;
diff --git a/src/libcamera/bayer_format.cpp b/src/libcamera/bayer_format.cpp
index 11355f144f66..c138e7a09597 100644
--- a/src/libcamera/bayer_format.cpp
+++ b/src/libcamera/bayer_format.cpp
@@ -169,6 +169,24 @@ const std::unordered_map<unsigned int, BayerFormat> mbusCodeToBayer{
  * \param[in] p The type of packing applied to the pixel values
  */
 
+/**
+ * \brief Retrieve the media bus code associated with a BayerFormat
+ *
+ * The media bus code numeric identifiers are defined by the V4L2 specification.
+ */
+unsigned int BayerFormat::toMbusCode() const
+{
+	const auto it = std::find_if(mbusCodeToBayer.begin(), mbusCodeToBayer.end(),
+				     [this](const auto &p) {
+					     return p.second == *this;
+				     });
+
+	if (it == mbusCodeToBayer.end())
+		return it->first;
+
+	return 0;
+}
+
 /**
  * \brief Retrieve the BayerFormat associated with a media bus code
  * \param[in] mbusCode The media bus code to convert into a BayerFormat
@@ -269,6 +287,19 @@ BayerFormat BayerFormat::fromV4L2PixelFormat(V4L2PixelFormat v4l2Format)
 	return BayerFormat();
 }
 
+/**
+ * \brief Convert a BayerFormat into the corresponding PixelFormat
+ * \return The PixelFormat corresponding to this BayerFormat
+ */
+PixelFormat BayerFormat::toPixelFormat() const
+{
+	const auto it = bayerToV4l2.find(*this);
+	if (it != bayerToV4l2.end())
+		return PixelFormat(it->second);
+
+	return PixelFormat();
+}
+
 /**
  * \brief Apply a transform to this BayerFormat
  * \param[in] t The transform to apply
-- 
2.25.1



More information about the libcamera-devel mailing list