diff mbox series

[v4l-utils,6/6] v4l2-ctl: Add --stream-pixformat option

Message ID 20190120111520.114305-7-dafna3@gmail.com (mailing list archive)
State New, archived
Headers show
Series Support for source change in m2m decoder | expand

Commit Message

Dafna Hirschfeld Jan. 20, 2019, 11:15 a.m. UTC
This option sets the capture pixelformat in the
capture setup sequence.
If the format is not supported decoding will stop.
If the option is not given then the default is to set
the first supported format.

Signed-off-by: Dafna Hirschfeld <dafna3@gmail.com>
---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 29 +++++++++++++++++++-
 utils/v4l2-ctl/v4l2-ctl.cpp           | 39 ++++++++++++++++++---------
 utils/v4l2-ctl/v4l2-ctl.h             |  2 ++
 3 files changed, 56 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
index 61dd84db..7ee472b1 100644
--- a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
@@ -78,6 +78,7 @@  static unsigned int composed_width;
 static unsigned int composed_height;
 static bool support_cap_compose;
 static bool support_out_crop;
+static unsigned int cap_pixelformat;
 static bool in_source_change_event;
 
 #define TS_WINDOW 241
@@ -272,6 +273,10 @@  void streaming_usage(void)
 	       "  --stream-from <file>\n"
 	       "                     stream from this file. The default is to generate a pattern.\n"
 	       "                     If <file> is '-', then the data is read from stdin.\n"
+	       "  --stream-pixformat <pixformat>\n"
+	       "                     set the video pixelformat."
+	       "                     <pixelformat> is either the format index as reported by\n"
+	       "                     --list-formats-out, or the fourcc value as a string.\n"
 	       "  --stream-from-hdr <file> stream from this file. Same as --stream-from, but each\n"
 	       "                     frame is prefixed by a header. Use for compressed data.\n"
 	       "  --stream-from-host <hostname[:port]>\n"
@@ -609,8 +614,16 @@  void streaming_cmd(int ch, char *optarg)
 {
 	unsigned i;
 	int speed;
+	int r;
 
 	switch (ch) {
+	case OptStreamPixformat:
+		r = parse_pixelfmt(optarg, cap_pixelformat);
+		if (r) {
+			streaming_usage();
+			exit(1);
+		}
+		break;
 	case OptStreamCount:
 		stream_count = strtoul(optarg, 0L, 0);
 		break;
@@ -1869,6 +1882,7 @@  static int capture_setup(cv4l_fd &fd, cv4l_queue &in)
 {
 	struct v4l2_fmtdesc fmt_desc;
 	cv4l_fmt fmt;
+	unsigned int chosen_pixformat;
 
 	if (fd.streamoff(in.g_type())) {
 		fprintf(stderr, "%s: fd.streamoff error\n", __func__);
@@ -1887,8 +1901,21 @@  static int capture_setup(cv4l_fd &fd, cv4l_queue &in)
 		return -1;
 	}
 
+	if (cap_pixelformat) {
+		do {
+			if (cap_pixelformat == fmt_desc.pixelformat)
+				break;
+		} while (!fd.enum_fmt(fmt_desc));
+
+		if (cap_pixelformat != fmt_desc.pixelformat) {
+			fprintf(stderr, "%s: format from user not supported\n", __func__);
+			return -1;
+		}
+	}
+
+	chosen_pixformat = fmt_desc.pixelformat;
 	fd.g_fmt(fmt, in.g_type());
-	fmt.s_pixelformat(fmt_desc.pixelformat);
+	fmt.s_pixelformat(chosen_pixformat);
 	fd.s_fmt(fmt, in.g_type());
 
 	if (in.reqbufs(&fd, reqbufs_count_cap)) {
diff --git a/utils/v4l2-ctl/v4l2-ctl.cpp b/utils/v4l2-ctl/v4l2-ctl.cpp
index 1783979d..2cbf519e 100644
--- a/utils/v4l2-ctl/v4l2-ctl.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl.cpp
@@ -238,6 +238,7 @@  static struct option long_options[] = {
 	{"list-buffers-sdr", no_argument, 0, OptListBuffersSdr},
 	{"list-buffers-sdr-out", no_argument, 0, OptListBuffersSdrOut},
 	{"list-buffers-meta", no_argument, 0, OptListBuffersMeta},
+	{"stream-pixformat", required_argument, 0, OptStreamPixformat},
 	{"stream-count", required_argument, 0, OptStreamCount},
 	{"stream-skip", required_argument, 0, OptStreamSkip},
 	{"stream-loop", no_argument, 0, OptStreamLoop},
@@ -722,6 +723,30 @@  __u32 parse_quantization(const char *s)
 	return V4L2_QUANTIZATION_DEFAULT;
 }
 
+int parse_pixelfmt(char *value,  __u32 &pixelformat)
+{
+	int fmts = 0;
+	bool be_pixfmt;
+
+	if(!value)
+		return -EINVAL;
+
+	be_pixfmt = strlen(value) == 7 && !memcmp(value + 4, "-BE", 3);
+	if (be_pixfmt)
+		value[4] = 0;
+	if (strlen(value) == 4) {
+		pixelformat =
+			v4l2_fourcc(value[0], value[1],
+					value[2], value[3]);
+		if (be_pixfmt)
+			pixelformat |= 1 << 31;
+	} else {
+		pixelformat = strtol(value, 0L, 0);
+	}
+	fmts |= FmtPixelFormat;
+	return 0;
+}
+
 int parse_fmt(char *optarg, __u32 &width, __u32 &height, __u32 &pixelformat,
 	      __u32 &field, __u32 &colorspace, __u32 &xfer_func, __u32 &ycbcr,
 	      __u32 &quantization, __u32 &flags, __u32 *bytesperline)
@@ -729,7 +754,6 @@  int parse_fmt(char *optarg, __u32 &width, __u32 &height, __u32 &pixelformat,
 	char *value, *subs;
 	int fmts = 0;
 	unsigned bpl_index = 0;
-	bool be_pixfmt;
 
 	field = V4L2_FIELD_ANY;
 	flags = 0;
@@ -760,18 +784,7 @@  int parse_fmt(char *optarg, __u32 &width, __u32 &height, __u32 &pixelformat,
 			fmts |= FmtHeight;
 			break;
 		case 2:
-			be_pixfmt = strlen(value) == 7 && !memcmp(value + 4, "-BE", 3);
-			if (be_pixfmt)
-				value[4] = 0;
-			if (strlen(value) == 4) {
-				pixelformat =
-					v4l2_fourcc(value[0], value[1],
-							value[2], value[3]);
-				if (be_pixfmt)
-					pixelformat |= 1 << 31;
-			} else {
-				pixelformat = strtol(value, 0L, 0);
-			}
+			parse_pixelfmt(value, pixelformat);
 			fmts |= FmtPixelFormat;
 			break;
 		case 3:
diff --git a/utils/v4l2-ctl/v4l2-ctl.h b/utils/v4l2-ctl/v4l2-ctl.h
index 5a52a0a4..8eee5351 100644
--- a/utils/v4l2-ctl/v4l2-ctl.h
+++ b/utils/v4l2-ctl/v4l2-ctl.h
@@ -205,6 +205,7 @@  enum Option {
 	OptListBuffersSdr,
 	OptListBuffersSdrOut,
 	OptListBuffersMeta,
+	OptStreamPixformat,
 	OptStreamCount,
 	OptStreamSkip,
 	OptStreamLoop,
@@ -299,6 +300,7 @@  __u32 parse_xfer_func(const char *s);
 __u32 parse_ycbcr(const char *s);
 __u32 parse_hsv(const char *s);
 __u32 parse_quantization(const char *s);
+int parse_pixelfmt(char *value,  __u32 &pixelformat);
 int parse_fmt(char *optarg, __u32 &width, __u32 &height, __u32 &pixelformat,
 	      __u32 &field, __u32 &colorspace, __u32 &xfer, __u32 &ycbcr,
 	      __u32 &quantization, __u32 &flags, __u32 *bytesperline);