[libcamera-devel] [PATCH v2] libcamera: v4l2PixelFormat: Replace hex with fourCC

Kaaira Gupta kgupta at es.iitr.ac.in
Wed Mar 25 23:02:52 CET 2020


Print fourCC characters instead of the hex value in toString() as they are
more informative. Also, write the tests for this in formats.cpp

Signed-off-by: Kaaira Gupta <kgupta at es.iitr.ac.in>
---

Changes since v1:
	- Add tests for checking this function.
	- use char[] instead of stringstream.
	- add checks for default value.
	- Print '.' for non-printable characters.

 src/libcamera/v4l2_videodevice.cpp | 19 ++++++++++++++++---
 test/v4l2_videodevice/formats.cpp  | 15 +++++++++++++++
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index b778181..f5c14aa 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -336,9 +336,22 @@ bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const
  */
 std::string V4L2PixelFormat::toString() const
 {
-	char str[11];
-	snprintf(str, 11, "0x%08x", fourcc_);
-	return str;
+	if (fourcc_ == 0)
+		return "Invalid";
+
+	char ss[7] = {static_cast<char>(fourcc_ & 0x7f),
+				  static_cast<char>((fourcc_ >> 8) & 0x7f),
+				  static_cast<char>((fourcc_ >> 16) & 0x7f),
+				  static_cast<char>((fourcc_ >> 24) & 0x7f)};
+
+	for (unsigned int i = 0; i < strlen(ss); i++){
+		if (!isprint(ss[i]))
+			ss[i]= '.';
+	}
+
+	if (fourcc_ & (1 << 31))
+		strcat(ss, "-BE");
+	return ss;
 }
 
 /**
diff --git a/test/v4l2_videodevice/formats.cpp b/test/v4l2_videodevice/formats.cpp
index d504d17..f129c5c 100644
--- a/test/v4l2_videodevice/formats.cpp
+++ b/test/v4l2_videodevice/formats.cpp
@@ -47,6 +47,21 @@ protected:
 			return TestFail;
 		}
 
+		auto badfourcc = v4l2_fourcc( 1, 2, 3, 4 );
+		std::vector<std::pair<uint32_t, const char*>> formats{
+			{V4L2_PIX_FMT_YUYV,"YUYV"},{0, "Invalid"}, {badfourcc, "...."},
+			{V4L2_PIX_FMT_Y16_BE,"Y16 -BE"}};
+
+		for (const auto &format :formats){
+			if (V4L2PixelFormat(format.first).toString()!=format.second){
+				return TestFail;
+			}
+		}
+
+		if (V4L2PixelFormat().toString() != "Invalid"){
+			return TestFail;
+		}
+
 		return TestPass;
 	}
 };
-- 
2.17.1



More information about the libcamera-devel mailing list