Skip to contentSkip to conversation

v4l2-ctl: add is_compressed_format() helper

PATCH In review Tzu-Jung Lee 2 participants 4 messages Apr 11, 2013 – Apr 17, 2013 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 v4l2-ctl: add is_compressed_format() helper

To bypass precalculate_bars() for OUTPUT device
that takes encoded bitstreams.

---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 40 ++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +39 -1
index 9e361af..035c3c7 100644
@@ -115,6 +115,23 @@ static const flag_def tc_flags_def[] = {
 	{ 0, NULL }
 };
 
+static bool is_compressed_format(int fd, struct v4l2_format *f)
+{
+	struct v4l2_fmtdesc fmt;
+
+	memset(&fmt, 0, sizeof(fmt));
+	fmt.type = f->type;
+
+	while (test_ioctl(fd, VIDIOC_ENUM_FMT, &fmt) >= 0) {
+		if (fmt.pixelformat == f->fmt.pix.pixelformat)
+			return fmt.flags & V4L2_FMT_FLAG_COMPRESSED;
+
+		fmt.index++;
+	}
+
+	return false;
+}
+
 static void print_buffer(FILE *f, struct v4l2_buffer &buf)
 {
 	fprintf(f, "\tIndex    : %d\n", buf.index);
@@ -312,12 +329,16 @@ static void do_setup_out_buffers(int fd, struct v4l2_requestbuffers *reqbufs,
 				 bool is_mplane, unsigned &num_planes, bool is_mmap,
 				 void *buffers[], unsigned buffer_lengths[], FILE *fin)
 {
+	bool is_compressed;
+
 	struct v4l2_format fmt;
 	memset(&fmt, 0, sizeof(fmt));
 	fmt.type = reqbufs->type;
 	doioctl(fd, VIDIOC_G_FMT, &fmt);
 
-	if (!precalculate_bars(fmt.fmt.pix.pixelformat, stream_pat)) {
+	is_compressed = is_compressed_format(fd, &fmt);
+	if (!is_compressed &&
+	    !precalculate_bars(fmt.fmt.pix.pixelformat, stream_pat)) {
 		fprintf(stderr, "unsupported pixelformat\n");
 		return;
 	}
@@ -688,7 +709,9 @@ static void streaming_set_cap(int fd)
 static void streaming_set_out(int fd)
 {
 	struct v4l2_requestbuffers reqbufs;
+	struct v4l2_format fmt;
 	int fd_flags = fcntl(fd, F_GETFL);
+	bool is_compressed;
 	bool is_mplane = capabilities &
 			(V4L2_CAP_VIDEO_OUTPUT_MPLANE |
 				 V4L2_CAP_VIDEO_M2M_MPLANE);
@@ -710,6 +733,12 @@ static void streaming_set_out(int fd)
 	reqbufs.type = type;
 	reqbufs.memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
 
+	memset(&fmt, 0, sizeof(fmt));
+	fmt.type = reqbufs.type;
+	doioctl(fd, VIDIOC_G_FMT, &fmt);
+
+	is_compressed = is_compressed_format(fd, &fmt);
+
 	if (file_out) {
 		if (!strcmp(file_out, "-"))
 			fin = stdin;
@@ -795,6 +824,9 @@ enum stream_type {
 
 static void streaming_set_m2m(int fd)
 {
+	struct v4l2_format fmt;
+	bool is_compressed;
+
 	int fd_flags = fcntl(fd, F_GETFL);
 	bool use_poll = options[OptStreamPoll];
 
@@ -864,6 +896,12 @@ static void streaming_set_m2m(int fd)
 			     is_mmap, buffers_out, buffer_lengths_out,
 			     file[OUT]);
 
+	memset(&fmt, 0, sizeof(fmt));
+	fmt.type = reqbufs[OUT].type;
+	doioctl(fd, VIDIOC_G_FMT, &fmt);
+
+	is_compressed = is_compressed_format(fd, &fmt);
+
 	if (doioctl(fd, VIDIOC_STREAMON, &type[CAP]) ||
 	    doioctl(fd, VIDIOC_STREAMON, &type[OUT]))
 		return;
Signature · 1 line
1.8.1.5
#1 lore raw

PATCH v4 2/2 v4l2-ctl: handle the last chunk of input file

For bitstream input file, it could be non-aligned
to the buffer size. In this case, we still need
to QBUF it to the driver.

---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 72 +++++++++++++++++++++++++++--------
 1 file changed, 57 insertions(+), 15 deletions(-)
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +57 -15
index 035c3c7..d9b9146 100644
@@ -240,8 +240,37 @@ void streaming_cmd(int ch, char *optarg)
 }
 
 static bool fill_buffer_from_file(void *buffers[], unsigned buffer_lengths[],
-		unsigned buf_index, unsigned num_planes, FILE *fin)
+				  unsigned buffer_bytesused[], unsigned buf_index,
+				  unsigned num_planes, bool is_compressed, FILE *fin)
 {
+	if (num_planes == 1) {
+		unsigned i = buf_index;
+		unsigned sz = fread(buffers[i], 1,
+				    buffer_lengths[i], fin);
+
+		buffer_bytesused[i] = sz;
+		if (sz == 0 && stream_loop) {
+			fseek(fin, 0, SEEK_SET);
+			sz = fread(buffers[i], 1,
+				   buffer_lengths[i], fin);
+
+			buffer_bytesused[i] = sz;
+		}
+
+		if (!sz)
+			return false;
+
+		if (sz == buffer_lengths[i])
+			return true;
+
+		if (is_compressed)
+			return true;
+
+		fprintf(stderr, "%u != %u\n", sz, buffer_lengths[i]);
+
+		return false;
+	}
+
 	for (unsigned j = 0; j < num_planes; j++) {
 		unsigned p = buf_index * num_planes + j;
 		unsigned sz = fread(buffers[p], 1,
@@ -343,6 +372,8 @@ static void do_setup_out_buffers(int fd, struct v4l2_requestbuffers *reqbufs,
 		return;
 	}
 
+	unsigned buffer_bytesused[reqbufs->count * VIDEO_MAX_PLANES];
+
 	for (unsigned i = 0; i < reqbufs->count; i++) {
 		struct v4l2_plane planes[VIDEO_MAX_PLANES];
 		struct v4l2_buffer buf;
@@ -384,11 +415,11 @@ static void do_setup_out_buffers(int fd, struct v4l2_requestbuffers *reqbufs,
 			// TODO fill_buffer_mp(buffers[i], &fmt.fmt.pix_mp);
 			if (fin)
 				fill_buffer_from_file(buffers, buffer_lengths,
-						      buf.index, num_planes, fin);
+						      buffer_bytesused, buf.index,
+						      num_planes, is_compressed, fin);
 		}
 		else {
 			buffer_lengths[i] = buf.length;
-			buf.bytesused = buf.length;
 			if (is_mmap) {
 				buffers[i] = mmap(NULL, buf.length,
 						  PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);
@@ -402,9 +433,16 @@ static void do_setup_out_buffers(int fd, struct v4l2_requestbuffers *reqbufs,
 				buffers[i] = calloc(1, buf.length);
 				buf.m.userptr = (unsigned long)buffers[i];
 			}
-			if (!fin || !fill_buffer_from_file(buffers, buffer_lengths,
-							   buf.index, num_planes, fin))
+
+			if (fin && fill_buffer_from_file(buffers, buffer_lengths,
+							 buffer_bytesused, buf.index,
+							 num_planes, is_compressed,
+							 fin)) {
+				buf.bytesused = buffer_bytesused[buf.index];
+			}
+			else {
 				fill_buffer(buffers[i], &fmt.fmt.pix);
+			}
 		}
 		if (doioctl(fd, VIDIOC_QBUF, &buf))
 			return;
@@ -532,12 +570,13 @@ static int do_handle_cap(int fd, struct v4l2_requestbuffers *reqbufs,
 }
 
 static int do_handle_out(int fd, struct v4l2_requestbuffers *reqbufs,
-			 bool is_mplane, unsigned num_planes,
+			 bool is_compressed, bool is_mplane, unsigned num_planes,
 			 void *buffers[], unsigned buffer_lengths[], FILE *fin,
 			 unsigned &count, unsigned &last, struct timeval &tv_last)
 {
 	struct v4l2_plane planes[VIDEO_MAX_PLANES];
 	struct v4l2_buffer buf;
+	unsigned buffer_bytesused[reqbufs->count * VIDEO_MAX_PLANES];
 	int ret;
 
 	memset(&buf, 0, sizeof(buf));
@@ -556,14 +595,17 @@ static int do_handle_out(int fd, struct v4l2_requestbuffers *reqbufs,
 		fprintf(stderr, "%s: failed: %s\n", "VIDIOC_DQBUF", strerror(errno));
 		return -1;
 	}
-	if (fin && !fill_buffer_from_file(buffers, buffer_lengths,
-					  buf.index, num_planes, fin))
+	if (fin &&!fill_buffer_from_file(buffers, buffer_lengths,
+					 buffer_bytesused, buf.index,
+					 num_planes, is_compressed,
+					 fin)) {
 		return -1;
+	}
 	if (is_mplane) {
 		for (unsigned j = 0; j < buf.length; j++)
 			buf.m.planes[j].bytesused = buf.m.planes[j].length;
 	} else {
-		buf.bytesused = buf.length;
+		buf.bytesused = buffer_bytesused[buf.index];
 	}
 	if (test_ioctl(fd, VIDIOC_QBUF, &buf))
 		return -1;
@@ -794,9 +836,9 @@ static void streaming_set_out(int fd)
 				return;
 			}
 		}
-		r = do_handle_out(fd, &reqbufs, is_mplane, num_planes,
-				   buffers, buffer_lengths, fin,
-				   count, last, tv_last);
+		r = do_handle_out(fd, &reqbufs, is_compressed, is_mplane,
+				  num_planes, buffers, buffer_lengths,
+				  fin, count, last, tv_last);
 		if (r == -1)
 			break;
 
@@ -965,9 +1007,9 @@ static void streaming_set_m2m(int fd)
 		}
 
 		if (wr_fds && FD_ISSET(fd, wr_fds)) {
-			r  = do_handle_out(fd, &reqbufs[OUT], is_mplane, num_planes[OUT],
-					   buffers_out, buffer_lengths_out, file[OUT],
-					   count[OUT], last[OUT], tv_last[OUT]);
+			r  = do_handle_out(fd, &reqbufs[OUT], is_compressed, is_mplane,
+					   num_planes[OUT], buffers_out, buffer_lengths_out,
+					   file[OUT], count[OUT], last[OUT], tv_last[OUT]);
 			if (r < 0)  {
 				wr_fds = NULL;
 
Signature · 1 line
1.8.1.5
#2 lore raw
In reply to Tzu-Jung Lee
Quoted 2 lines · On Thu April 11 2013 21:46:54 Tzu-Jung Lee wrote:

To bypass precalculate_bars() for OUTPUT device
that takes encoded bitstreams.

I haven't forgotten your patches, but I want to clean up the streaming code some more first. I hope to have finished with that this weekend.

Regards,

Hans

Quoted 99 lines
---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 40 ++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +39 -1
index 9e361af..035c3c7 100644
@@ -115,6 +115,23 @@ static const flag_def tc_flags_def[] = {
 	{ 0, NULL }
 };
 
+static bool is_compressed_format(int fd, struct v4l2_format *f)
+{
+	struct v4l2_fmtdesc fmt;
+
+	memset(&fmt, 0, sizeof(fmt));
+	fmt.type = f->type;
+
+	while (test_ioctl(fd, VIDIOC_ENUM_FMT, &fmt) >= 0) {
+		if (fmt.pixelformat == f->fmt.pix.pixelformat)
+			return fmt.flags & V4L2_FMT_FLAG_COMPRESSED;
+
+		fmt.index++;
+	}
+
+	return false;
+}
+
 static void print_buffer(FILE *f, struct v4l2_buffer &buf)
 {
 	fprintf(f, "\tIndex    : %d\n", buf.index);
@@ -312,12 +329,16 @@ static void do_setup_out_buffers(int fd, struct v4l2_requestbuffers *reqbufs,
 				 bool is_mplane, unsigned &num_planes, bool is_mmap,
 				 void *buffers[], unsigned buffer_lengths[], FILE *fin)
 {
+	bool is_compressed;
+
 	struct v4l2_format fmt;
 	memset(&fmt, 0, sizeof(fmt));
 	fmt.type = reqbufs->type;
 	doioctl(fd, VIDIOC_G_FMT, &fmt);
 
-	if (!precalculate_bars(fmt.fmt.pix.pixelformat, stream_pat)) {
+	is_compressed = is_compressed_format(fd, &fmt);
+	if (!is_compressed &&
+	    !precalculate_bars(fmt.fmt.pix.pixelformat, stream_pat)) {
 		fprintf(stderr, "unsupported pixelformat\n");
 		return;
 	}
@@ -688,7 +709,9 @@ static void streaming_set_cap(int fd)
 static void streaming_set_out(int fd)
 {
 	struct v4l2_requestbuffers reqbufs;
+	struct v4l2_format fmt;
 	int fd_flags = fcntl(fd, F_GETFL);
+	bool is_compressed;
 	bool is_mplane = capabilities &
 			(V4L2_CAP_VIDEO_OUTPUT_MPLANE |
 				 V4L2_CAP_VIDEO_M2M_MPLANE);
@@ -710,6 +733,12 @@ static void streaming_set_out(int fd)
 	reqbufs.type = type;
 	reqbufs.memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
 
+	memset(&fmt, 0, sizeof(fmt));
+	fmt.type = reqbufs.type;
+	doioctl(fd, VIDIOC_G_FMT, &fmt);
+
+	is_compressed = is_compressed_format(fd, &fmt);
+
 	if (file_out) {
 		if (!strcmp(file_out, "-"))
 			fin = stdin;
@@ -795,6 +824,9 @@ enum stream_type {
 
 static void streaming_set_m2m(int fd)
 {
+	struct v4l2_format fmt;
+	bool is_compressed;
+
 	int fd_flags = fcntl(fd, F_GETFL);
 	bool use_poll = options[OptStreamPoll];
 
@@ -864,6 +896,12 @@ static void streaming_set_m2m(int fd)
 			     is_mmap, buffers_out, buffer_lengths_out,
 			     file[OUT]);
 
+	memset(&fmt, 0, sizeof(fmt));
+	fmt.type = reqbufs[OUT].type;
+	doioctl(fd, VIDIOC_G_FMT, &fmt);
+
+	is_compressed = is_compressed_format(fd, &fmt);
+
 	if (doioctl(fd, VIDIOC_STREAMON, &type[CAP]) ||
 	    doioctl(fd, VIDIOC_STREAMON, &type[OUT]))
 		return;
#3 lore raw
Quoted 6 lines · Hans Verkuil
Quoted 2 lines · On Thu April 11 2013 21:46:54 Tzu-Jung Lee wrote:

To bypass precalculate_bars() for OUTPUT device
that takes encoded bitstreams.

I haven't forgotten your patches, but I want to clean up the streaming code some more first. I hope to have finished with that this weekend.

One more thing about streaming code:

I just realized that the non-polling case for M2M isn't working, at least for bitstreams to bitstreams case.
Without timeout, it can be blocked indefinitely by DQBUF of either CAPTURE or OUTPUT queues.
QUERYBUF for any available buffers before issuing DQBUF might work, but it seems to be overkill.
And it just degenerates to another form of polling mode...

Thanks.

Roy

Quoted 100 lines
Quoted 100 lines
---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 40 ++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
utils/v4l2-ctl/v4l2-ctl-streaming.cpp +39 -1
index 9e361af..035c3c7 100644
@@ -115,6 +115,23 @@ static const flag_def tc_flags_def[] = {
      { 0, NULL }
 };
 
+static bool is_compressed_format(int fd, struct v4l2_format *f)
+{
+     struct v4l2_fmtdesc fmt;
+
+     memset(&fmt, 0, sizeof(fmt));
+     fmt.type = f->type;
+
+     while (test_ioctl(fd, VIDIOC_ENUM_FMT, &fmt) >= 0) {
+             if (fmt.pixelformat == f->fmt.pix.pixelformat)
+                     return fmt.flags & V4L2_FMT_FLAG_COMPRESSED;
+
+             fmt.index++;
+     }
+
+     return false;
+}
+
 static void print_buffer(FILE *f, struct v4l2_buffer &buf)
 {
      fprintf(f, "\tIndex    : %d\n", buf.index);
@@ -312,12 +329,16 @@ static void do_setup_out_buffers(int fd, struct v4l2_requestbuffers *reqbufs,
                               bool is_mplane, unsigned &num_planes, bool is_mmap,
                               void *buffers[], unsigned buffer_lengths[], FILE *fin)
 {
+     bool is_compressed;
+
      struct v4l2_format fmt;
      memset(&fmt, 0, sizeof(fmt));
      fmt.type = reqbufs->type;
      doioctl(fd, VIDIOC_G_FMT, &fmt);
 
-     if (!precalculate_bars(fmt.fmt.pix.pixelformat, stream_pat)) {
+     is_compressed = is_compressed_format(fd, &fmt);
+     if (!is_compressed &&
+         !precalculate_bars(fmt.fmt.pix.pixelformat, stream_pat)) {
              fprintf(stderr, "unsupported pixelformat\n");
              return;
      }
@@ -688,7 +709,9 @@ static void streaming_set_cap(int fd)
 static void streaming_set_out(int fd)
 {
      struct v4l2_requestbuffers reqbufs;
+     struct v4l2_format fmt;
      int fd_flags = fcntl(fd, F_GETFL);
+     bool is_compressed;
      bool is_mplane = capabilities &
                      (V4L2_CAP_VIDEO_OUTPUT_MPLANE |
                               V4L2_CAP_VIDEO_M2M_MPLANE);
@@ -710,6 +733,12 @@ static void streaming_set_out(int fd)
      reqbufs.type = type;
      reqbufs.memory = is_mmap ? V4L2_MEMORY_MMAP : V4L2_MEMORY_USERPTR;
 
+     memset(&fmt, 0, sizeof(fmt));
+     fmt.type = reqbufs.type;
+     doioctl(fd, VIDIOC_G_FMT, &fmt);
+
+     is_compressed = is_compressed_format(fd, &fmt);
+
      if (file_out) {
              if (!strcmp(file_out, "-"))
                      fin = stdin;
@@ -795,6 +824,9 @@ enum stream_type {
 
 static void streaming_set_m2m(int fd)
 {
+     struct v4l2_format fmt;
+     bool is_compressed;
+
      int fd_flags = fcntl(fd, F_GETFL);
      bool use_poll = options[OptStreamPoll];
 
@@ -864,6 +896,12 @@ static void streaming_set_m2m(int fd)
                           is_mmap, buffers_out, buffer_lengths_out,
                           file[OUT]);
 
+     memset(&fmt, 0, sizeof(fmt));
+     fmt.type = reqbufs[OUT].type;
+     doioctl(fd, VIDIOC_G_FMT, &fmt);
+
+     is_compressed = is_compressed_format(fd, &fmt);
+
      if (doioctl(fd, VIDIOC_STREAMON, &type[CAP]) ||
          doioctl(fd, VIDIOC_STREAMON, &type[OUT]))
              return;