diff options
author | Kaaira Gupta <kgupta@es.iitr.ac.in> | 2020-03-27 01:44:00 +0530 |
---|---|---|
committer | Kieran Bingham <kieran.bingham@ideasonboard.com> | 2020-03-27 15:17:30 +0000 |
commit | 2fbab8b07748b790b5896c24a0aa09444ca48f97 (patch) | |
tree | fddd0cc0ab416706c2c626b74cecf8e8152699fc | |
parent | 6f553040fddd35cd8d59d033bed4ab1b9c2dd634 (diff) |
libcamera: v4l2PixelFormat: Replace hex with fourCC
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@es.iitr.ac.in>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
-rw-r--r-- | src/libcamera/v4l2_videodevice.cpp | 20 | ||||
-rw-r--r-- | test/v4l2_videodevice/formats.cpp | 23 |
2 files changed, 40 insertions, 3 deletions
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index b778181a..eb33a68e 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -336,9 +336,23 @@ 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[8] = { 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 < 4; 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 d504d178..a7421421 100644 --- a/test/v4l2_videodevice/formats.cpp +++ b/test/v4l2_videodevice/formats.cpp @@ -8,6 +8,7 @@ #include <iostream> #include <limits.h> +#include "utils.h" #include "v4l2_videodevice.h" #include "v4l2_videodevice_test.h" @@ -47,6 +48,28 @@ protected: return TestFail; } + std::vector<std::pair<uint32_t, const char *>> formats{ + { V4L2_PIX_FMT_YUYV, "YUYV" }, + { 0, "<INVALID>" }, + { v4l2_fourcc(0, 1, 2, 3), "...." }, + { V4L2_PIX_FMT_Y16_BE, "Y16 -BE" } + }; + + for (const auto &format : formats) { + if (V4L2PixelFormat(format.first).toString() != format.second) { + cerr << "Failed to convert V4L2PixelFormat" + << utils::hex(format.first) << "to string" + << endl; + return TestFail; + } + } + + if (V4L2PixelFormat().toString() != "<INVALID>") { + cerr << "Failed to convert default V4L2PixelFormat to string" + << endl; + return TestFail; + } + return TestPass; } }; |