Skip to contentSkip to conversation

Print V4L2 fourcc code in format enumeration

PATCH In review Sakari Ailus 2 participants 2 messages Apr 25, 2024 – May 2, 2024 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Print V4L2 fourcc code in format enumeration

Also print the V4L2 fourcc code in format enumeration (--enum-formats).

---
 yavta.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
yavta.c +19 -8
index b2283aed09cb..2153e8b5775f 100644
@@ -370,6 +370,18 @@ static struct v4l2_format_info {
 	{ "GENERIC_CSI2_24", V4L2_META_FMT_GENERIC_CSI2_24, 1 },
 };
 
+void v4l2_format_fourcc(__u32 fourcc, char name[5])
+{
+	unsigned int i;
+
+	for (i = 0; i < 4; ++i) {
+		name[i] = fourcc & 0xff;
+		fourcc >>= 8;
+	}
+
+	name[4] = '\0';
+}
+
 static void list_formats(void)
 {
 	unsigned int i;
@@ -412,18 +424,13 @@ static const char *v4l2_format_name(unsigned int fourcc)
 {
 	const struct v4l2_format_info *info;
 	static char name[5];
-	unsigned int i;
 
 	info = v4l2_format_by_fourcc(fourcc);
 	if (info)
 		return info->name;
 
-	for (i = 0; i < 4; ++i) {
-		name[i] = fourcc & 0xff;
-		fourcc >>= 8;
-	}
+	v4l2_format_fourcc(fourcc, name);
 
-	name[4] = '\0';
 	return name;
 }
 
@@ -1815,6 +1822,8 @@ static void video_enum_formats(struct device *dev, enum v4l2_buf_type type)
 	int ret;
 
 	for (i = 0; ; ++i) {
+		char fourcc[5];
+
 		memset(&fmt, 0, sizeof fmt);
 		fmt.index = i;
 		fmt.type = type;
@@ -1829,8 +1838,10 @@ static void video_enum_formats(struct device *dev, enum v4l2_buf_type type)
 			printf("Warning: driver returned wrong format type "
 				"%u.\n", fmt.type);
 
-		printf("\tFormat %u: %s (%08x)\n", i,
-			v4l2_format_name(fmt.pixelformat), fmt.pixelformat);
+		v4l2_format_fourcc(fmt.pixelformat, fourcc);
+		printf("\tFormat %u: %s (%08x, \"%s\")\n", i,
+			v4l2_format_name(fmt.pixelformat), fmt.pixelformat,
+			fourcc);
 		printf("\tType: %s (%u)\n", v4l2_buf_type_name(fmt.type),
 			fmt.type);
 		printf("\tName: %.32s\n", fmt.description);
Signature · 1 line
2.39.2
#1 lore raw

Hi Sakari,

Thank you for the patch.

Quoted 6 lines · Sakari Ailus

Also print the V4L2 fourcc code in format enumeration (--enum-formats).

---
 yavta.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

This doesn't apply cleanly.

Quoted 6 lines
yavta.c +0 -0
index b2283aed09cb..2153e8b5775f 100644

yavta.c static struct v4l2_format_info { · line 378

Quoted 9 lines
yavta.c +6 -0
@@ -370,6 +370,18 @@ static struct v4l2_format_info {
 	{ "GENERIC_CSI2_24", V4L2_META_FMT_GENERIC_CSI2_24, 1 },
 };
 
+void v4l2_format_fourcc(__u32 fourcc, char name[5])
+{
+	unsigned int i;
+
+	for (i = 0; i < 4; ++i) {
+		name[i] = fourcc & 0xff;

While at it, could we handle non-printable characters better ?

Quoted 39 lines
yavta.c +9 -6
+		fourcc >>= 8;
+	}
+
+	name[4] = '\0';
+}
+
 static void list_formats(void)
 {
 	unsigned int i;
@@ -412,18 +424,13 @@ static const char *v4l2_format_name(unsigned int fourcc)
 {
 	const struct v4l2_format_info *info;
 	static char name[5];
-	unsigned int i;
 
 	info = v4l2_format_by_fourcc(fourcc);
 	if (info)
 		return info->name;
 
-	for (i = 0; i < 4; ++i) {
-		name[i] = fourcc & 0xff;
-		fourcc >>= 8;
-	}
+	v4l2_format_fourcc(fourcc, name);
 
-	name[4] = '\0';
 	return name;
 }
 
@@ -1815,6 +1822,8 @@ static void video_enum_formats(struct device *dev, enum v4l2_buf_type type)
 	int ret;
 
 	for (i = 0; ; ++i) {
+		char fourcc[5];
+
 		memset(&fmt, 0, sizeof fmt);
 		fmt.index = i;
 		fmt.type = type;

yavta.c static void video_enum_formats(struct device *dev, enum v4l2_buf_type type) · line 1844

Quoted 9 lines
yavta.c +4 -2
@@ -1829,8 +1838,10 @@ static void video_enum_formats(struct device *dev, enum v4l2_buf_type type)
 			printf("Warning: driver returned wrong format type "
 				"%u.\n", fmt.type);
 
-		printf("\tFormat %u: %s (%08x)\n", i,
-			v4l2_format_name(fmt.pixelformat), fmt.pixelformat);
+		v4l2_format_fourcc(fmt.pixelformat, fourcc);
+		printf("\tFormat %u: %s (%08x, \"%s\")\n", i,
+			v4l2_format_name(fmt.pixelformat), fmt.pixelformat,
+			fourcc);

One could argue that it would be nice to extend this to all users of v4l2_format_fourcc(). I suppose that would be overkill.

If the format is unknown to yavta you'll end up printing the same thing twice, but consistency in the format is probably better than avoiding the duplication. The patch looks OK to me, I'll apply it if you send me a version that applies :-)

Quoted 3 lines
yavta.c +0 -0
 		printf("\tType: %s (%u)\n", v4l2_buf_type_name(fmt.type),
 			fmt.type);
 		printf("\tName: %.32s\n", fmt.description);
Signature · 3 lines
Regards,

Laurent Pinchart