linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [v4l-utils PATCH v5 1/6] v4l2-ctl: in streaming_set_m2m, close file pointers upon error
@ 2019-03-06 21:17 Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 2/6] v4l2-ctl: check that the size read/write fit the buffer size Dafna Hirschfeld
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Dafna Hirschfeld @ 2019-03-06 21:17 UTC (permalink / raw)
  To: linux-media; +Cc: hverkuil, helen.koike, Dafna Hirschfeld

In streaming_set_m2m, make sure to close all file pointers
and file descriptors before returning.

Signed-off-by: Dafna Hirschfeld <dafna3@gmail.com>
---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
index 0eda8449..ee84abbe 100644
--- a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
@@ -2141,18 +2141,19 @@ static void streaming_set_m2m(cv4l_fd &fd, cv4l_fd &exp_fd)
 
 	if (options[OptStreamDmaBuf]) {
 		if (exp_q.reqbufs(&exp_fd, reqbufs_count_cap))
-			return;
+			goto done;
 		exp_fd_p = &exp_fd;
 	}
 
 	if (options[OptStreamOutDmaBuf]) {
 		if (exp_q.reqbufs(&exp_fd, reqbufs_count_out))
-			return;
+			goto done;
 		if (out.export_bufs(&exp_fd, exp_fd.g_type()))
-			return;
+			goto done;
 	}
 	stateful_m2m(fd, in, out, file[CAP], file[OUT], exp_fd_p);
 
+done:
 	if (options[OptStreamDmaBuf] || options[OptStreamOutDmaBuf])
 		exp_q.close_exported_fds();
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [v4l-utils PATCH v5 2/6] v4l2-ctl: check that the size read/write fit the buffer size
  2019-03-06 21:17 [v4l-utils PATCH v5 1/6] v4l2-ctl: in streaming_set_m2m, close file pointers upon error Dafna Hirschfeld
@ 2019-03-06 21:17 ` Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 3/6] v4l2-ctl: set the in/out fmt variables in streaming_set_m2m Dafna Hirschfeld
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Dafna Hirschfeld @ 2019-03-06 21:17 UTC (permalink / raw)
  To: linux-media; +Cc: hverkuil, helen.koike, Dafna Hirschfeld

'read_write_padded_frame' should check that the
expected size to read/write is not larger than
the size of the buffer.

Signed-off-by: Dafna Hirschfeld <dafna3@gmail.com>
---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 41 ++++++++++++++++-----------
 1 file changed, 24 insertions(+), 17 deletions(-)

diff --git a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
index ee84abbe..465ba50c 100644
--- a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
@@ -749,9 +749,10 @@ void streaming_cmd(int ch, char *optarg)
 	}
 }
 
-static void read_write_padded_frame(cv4l_fmt &fmt, unsigned char *buf,
+static bool read_write_padded_frame(cv4l_fmt &fmt, unsigned char *buf,
 				    FILE *fpointer, unsigned &sz,
-				    unsigned &len, bool is_read)
+				    unsigned &expected_len, unsigned buf_len,
+				    bool is_read)
 {
 	const struct v4l2_fwht_pixfmt_info *info =
 			v4l2_fwht_find_pixfmt(fmt.g_pixelformat());
@@ -771,8 +772,9 @@ static void read_write_padded_frame(cv4l_fmt &fmt, unsigned char *buf,
 	}
 
 	sz = 0;
-	len = real_width * real_height * info->sizeimage_mult / info->sizeimage_div;
-
+	expected_len = real_width * real_height * info->sizeimage_mult / info->sizeimage_div;
+	if (expected_len > buf_len)
+		return false;
 	for (unsigned plane_idx = 0; plane_idx < info->planes_num; plane_idx++) {
 		bool is_chroma_plane = plane_idx == 1 || plane_idx == 2;
 		unsigned h_div = is_chroma_plane ? info->height_div : 1;
@@ -800,7 +802,7 @@ static void read_write_padded_frame(cv4l_fmt &fmt, unsigned char *buf,
 				break;
 			if (wsz != consume_sz) {
 				fprintf(stderr, "padding: needed %u bytes, got %u\n", consume_sz, wsz);
-				return;
+				return true;
 			}
 			sz += wsz;
 			row_p += stride;
@@ -809,6 +811,7 @@ static void read_write_padded_frame(cv4l_fmt &fmt, unsigned char *buf,
 		if (sz == 0)
 			break;
 	}
+	return true;
 }
 
 static bool fill_buffer_from_file(cv4l_fd &fd, cv4l_queue &q, cv4l_buffer &b,
@@ -929,26 +932,30 @@ restart:
 
 	for (unsigned j = 0; j < q.g_num_planes(); j++) {
 		void *buf = q.g_dataptr(b.g_index(), j);
-		unsigned len = q.g_length(j);
+		unsigned buf_len = q.g_length(j);
+		unsigned expected_len = q.g_length(j);
 		unsigned sz;
 		cv4l_fmt fmt;
 
 		fd.g_fmt(fmt, q.g_type());
 		if (from_with_hdr) {
-			len = read_u32(fin);
-			if (len > q.g_length(j)) {
+			expected_len = read_u32(fin);
+			if (expected_len > q.g_length(j)) {
 				fprintf(stderr, "plane size is too large (%u > %u)\n",
-					len, q.g_length(j));
+					expected_len, q.g_length(j));
 				return false;
 			}
 		}
 
-		if (support_out_crop && v4l2_fwht_find_pixfmt(fmt.g_pixelformat()))
-			read_write_padded_frame(fmt, (unsigned char *)buf, fin, sz, len, true);
-		else
-			sz = fread(buf, 1, len, fin);
+		if (support_out_crop && v4l2_fwht_find_pixfmt(fmt.g_pixelformat())) {
+			if (!read_write_padded_frame(fmt, (unsigned char *)buf,
+			    fin, sz, expected_len, buf_len, true))
+				return false;
+		} else {
+			sz = fread(buf, 1, expected_len, fin);
+		}
 
-		if (first && sz != len) {
+		if (first && sz != expected_len) {
 			fprintf(stderr, "Insufficient data\n");
 			return false;
 		}
@@ -958,12 +965,12 @@ restart:
 			goto restart;
 		}
 		b.s_bytesused(sz, j);
-		if (sz == len)
+		if (sz == expected_len)
 			continue;
 		if (sz == 0)
 			return false;
 		if (sz)
-			fprintf(stderr, "%u != %u\n", sz, len);
+			fprintf(stderr, "%u != %u\n", sz, expected_len);
 		continue;
 	}
 	first = false;
@@ -1151,7 +1158,7 @@ static void write_buffer_to_file(cv4l_fd &fd, cv4l_queue &q, cv4l_buffer &buf,
 			sz = fwrite(comp_ptr[j] + offset, 1, used, fout);
 		else if (support_cap_compose && v4l2_fwht_find_pixfmt(fmt.g_pixelformat()))
 			read_write_padded_frame(fmt, (u8 *)q.g_dataptr(buf.g_index(), j) + offset,
-						fout, sz, used, false);
+						fout, sz, used, used, false);
 		else
 			sz = fwrite((u8 *)q.g_dataptr(buf.g_index(), j) + offset, 1, used, fout);
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [v4l-utils PATCH v5 3/6] v4l2-ctl: set the in/out fmt variables in streaming_set_m2m
  2019-03-06 21:17 [v4l-utils PATCH v5 1/6] v4l2-ctl: in streaming_set_m2m, close file pointers upon error Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 2/6] v4l2-ctl: check that the size read/write fit the buffer size Dafna Hirschfeld
@ 2019-03-06 21:17 ` Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 4/6] v4l-utils: copy fwht-ctrls.h from kernel dir Dafna Hirschfeld
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Dafna Hirschfeld @ 2019-03-06 21:17 UTC (permalink / raw)
  To: linux-media; +Cc: hverkuil, helen.koike, Dafna Hirschfeld

set the in/out fmt variables in streaming_set_m2m
This is needed later to check for stateless
fwht pixel format.

Signed-off-by: Dafna Hirschfeld <dafna3@gmail.com>
---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
index 465ba50c..9bb58a0b 100644
--- a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
@@ -1949,7 +1949,8 @@ static int capture_setup(cv4l_fd &fd, cv4l_queue &in, cv4l_fd *exp_fd)
 }
 
 static void stateful_m2m(cv4l_fd &fd, cv4l_queue &in, cv4l_queue &out,
-			 FILE *fin, FILE *fout, cv4l_fd *exp_fd_p)
+			 FILE *fin, FILE *fout, cv4l_fmt &fmt_in,
+			 cv4l_fmt &fmt_out, cv4l_fd *exp_fd_p)
 {
 	int fd_flags = fcntl(fd.g_fd(), F_GETFL);
 	fps_timestamps fps_ts[2];
@@ -1959,10 +1960,6 @@ static void stateful_m2m(cv4l_fd &fd, cv4l_queue &in, cv4l_queue &out,
 	fd_set *ex_fds = &fds[1]; /* for capture */
 	fd_set *wr_fds = &fds[2]; /* for output */
 	bool cap_streaming = false;
-	cv4l_fmt fmt[2];
-
-	fd.g_fmt(fmt[OUT], out.g_type());
-	fd.g_fmt(fmt[CAP], in.g_type());
 
 	struct v4l2_event_subscription sub;
 
@@ -2040,7 +2037,7 @@ static void stateful_m2m(cv4l_fd &fd, cv4l_queue &in, cv4l_queue &out,
 
 		if (rd_fds && FD_ISSET(fd.g_fd(), rd_fds)) {
 			r = do_handle_cap(fd, in, fin, NULL,
-					  count[CAP], fps_ts[CAP], fmt[CAP]);
+					  count[CAP], fps_ts[CAP], fmt_in);
 			if (r < 0) {
 				rd_fds = NULL;
 				if (!have_eos) {
@@ -2052,7 +2049,7 @@ static void stateful_m2m(cv4l_fd &fd, cv4l_queue &in, cv4l_queue &out,
 
 		if (wr_fds && FD_ISSET(fd.g_fd(), wr_fds)) {
 			r = do_handle_out(fd, out, fout, NULL,
-					  count[OUT], fps_ts[OUT], fmt[OUT]);
+					  count[OUT], fps_ts[OUT], fmt_out);
 			if (r < 0)  {
 				wr_fds = NULL;
 
@@ -2103,8 +2100,8 @@ static void stateful_m2m(cv4l_fd &fd, cv4l_queue &in, cv4l_queue &out,
 				last_buffer = false;
 				if (capture_setup(fd, in, exp_fd_p))
 					return;
-				fd.g_fmt(fmt[OUT], out.g_type());
-				fd.g_fmt(fmt[CAP], in.g_type());
+				fd.g_fmt(fmt_out, out.g_type());
+				fd.g_fmt(fmt_in, in.g_type());
 				cap_streaming = true;
 			} else {
 				break;
@@ -2129,6 +2126,10 @@ static void streaming_set_m2m(cv4l_fd &fd, cv4l_fd &exp_fd)
 	cv4l_queue exp_q(exp_fd.g_type(), V4L2_MEMORY_MMAP);
 	cv4l_fd *exp_fd_p = NULL;
 	FILE *file[2] = {NULL, NULL};
+	cv4l_fmt fmt[2];
+
+	fd.g_fmt(fmt[OUT], out.g_type());
+	fd.g_fmt(fmt[CAP], in.g_type());
 
 	if (!fd.has_vid_m2m()) {
 		fprintf(stderr, "unsupported m2m stream type\n");
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [v4l-utils PATCH v5 4/6] v4l-utils: copy fwht-ctrls.h from kernel dir
  2019-03-06 21:17 [v4l-utils PATCH v5 1/6] v4l2-ctl: in streaming_set_m2m, close file pointers upon error Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 2/6] v4l2-ctl: check that the size read/write fit the buffer size Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 3/6] v4l2-ctl: set the in/out fmt variables in streaming_set_m2m Dafna Hirschfeld
@ 2019-03-06 21:17 ` Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 5/6] v4l2-ctl: Add functions and variables to support fwht stateless decoder Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 6/6] v4l2-ctl: Add implementation for the stateless fwht decoder Dafna Hirschfeld
  4 siblings, 0 replies; 6+ messages in thread
From: Dafna Hirschfeld @ 2019-03-06 21:17 UTC (permalink / raw)
  To: linux-media; +Cc: hverkuil, helen.koike, Dafna Hirschfeld

copy fwht-ctrls.h from the kernel dir when
running 'sync' and add typedef for u64
in codec-fwht.h

Signed-off-by: Dafna Hirschfeld <dafna3@gmail.com>
---
 Makefile.am                   | 1 +
 utils/common/codec-fwht.patch | 7 ++++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index b0b8a098..8abe4f94 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -52,6 +52,7 @@ sync-with-kernel:
 	cp -a $(KERNEL_DIR)/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c $(top_srcdir)/utils/common
 	cp -a $(KERNEL_DIR)/drivers/media/common/v4l2-tpg/v4l2-tpg-colors.c $(top_srcdir)/utils/common
 	cp -a $(KERNEL_DIR)/include/media/tpg/v4l2-tpg* $(top_srcdir)/utils/common
+	cp -a $(KERNEL_DIR)/include/media/fwht-ctrls.h $(top_srcdir)/utils/common
 	patch -d $(top_srcdir) --no-backup-if-mismatch -p0 <$(top_srcdir)/utils/common/v4l2-tpg.patch
 	cp -a $(KERNEL_DIR)/drivers/media/platform/vicodec/codec-fwht.[ch] $(top_srcdir)/utils/common/
 	cp -a $(KERNEL_DIR)/drivers/media/platform/vicodec/codec-v4l2-fwht.[ch] $(top_srcdir)/utils/common/
diff --git a/utils/common/codec-fwht.patch b/utils/common/codec-fwht.patch
index 37ac4672..ad27b37a 100644
--- a/utils/common/codec-fwht.patch
+++ b/utils/common/codec-fwht.patch
@@ -1,6 +1,6 @@
---- a/utils/common/codec-fwht.h.old	2018-12-29 11:23:58.128328613 -0800
-+++ b/utils/common/codec-fwht.h	2018-12-29 11:24:16.099127560 -0800
-@@ -8,8 +8,26 @@
+--- a/utils/common/codec-fwht.h.old	2019-02-23 09:38:59.454065366 -0800
++++ b/utils/common/codec-fwht.h	2019-02-23 09:40:44.007803414 -0800
+@@ -8,8 +8,27 @@
  #define CODEC_FWHT_H
  
  #include <linux/types.h>
@@ -21,6 +21,7 @@
 +#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
 +
 +
++typedef __u64 u64;
 +typedef __u32 u32;
 +typedef __u16 u16;
 +typedef __s16 s16;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [v4l-utils PATCH v5 5/6] v4l2-ctl: Add functions and variables to support fwht stateless decoder
  2019-03-06 21:17 [v4l-utils PATCH v5 1/6] v4l2-ctl: in streaming_set_m2m, close file pointers upon error Dafna Hirschfeld
                   ` (2 preceding siblings ...)
  2019-03-06 21:17 ` [v4l-utils PATCH v5 4/6] v4l-utils: copy fwht-ctrls.h from kernel dir Dafna Hirschfeld
@ 2019-03-06 21:17 ` Dafna Hirschfeld
  2019-03-06 21:17 ` [v4l-utils PATCH v5 6/6] v4l2-ctl: Add implementation for the stateless fwht decoder Dafna Hirschfeld
  4 siblings, 0 replies; 6+ messages in thread
From: Dafna Hirschfeld @ 2019-03-06 21:17 UTC (permalink / raw)
  To: linux-media; +Cc: hverkuil, helen.koike, Dafna Hirschfeld

Add the variable 'last_fwht_bf_ts' and the array 'fwht_reqs' to
allow the fwht stateless decoder to maintain the requests.

Signed-off-by: Dafna Hirschfeld <dafna3@gmail.com>
---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 138 ++++++++++++++++++++++++++
 1 file changed, 138 insertions(+)

diff --git a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
index 9bb58a0b..4bb2d301 100644
--- a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
@@ -17,9 +17,12 @@
 #include <sys/mman.h>
 #include <dirent.h>
 #include <math.h>
+#include <linux/media.h>
 
 #include "v4l2-ctl.h"
 #include "v4l-stream.h"
+#include <media-info.h>
+#include <fwht-ctrls.h>
 
 extern "C" {
 #include "v4l2-tpg.h"
@@ -80,6 +83,17 @@ static bool support_cap_compose;
 static bool support_out_crop;
 static bool in_source_change_event;
 
+static __u64 last_fwht_bf_ts;
+static fwht_cframe_hdr last_fwht_hdr;
+
+struct request_fwht {
+	int fd;
+	__u64 ts;
+	struct v4l2_ctrl_fwht_params params;
+};
+
+static request_fwht fwht_reqs[VIDEO_MAX_FRAME];
+
 #define TS_WINDOW 241
 #define FILE_HDR_ID			v4l2_fourcc('V', 'h', 'd', 'r')
 
@@ -420,6 +434,12 @@ static int get_out_crop_rect(cv4l_fd &fd)
 	return 0;
 }
 
+static __u64 get_ns_timestamp(cv4l_buffer &buf)
+{
+	const struct timeval tv = buf.g_timestamp();
+	return v4l2_timeval_to_ns(&tv);
+}
+
 static void set_time_stamp(cv4l_buffer &buf)
 {
 	if ((buf.g_flags() & V4L2_BUF_FLAG_TIMESTAMP_MASK) != V4L2_BUF_FLAG_TIMESTAMP_COPY)
@@ -749,6 +769,124 @@ void streaming_cmd(int ch, char *optarg)
 	}
 }
 
+/*
+ * Assume that the fwht stream is valid and that each
+ * frame starts right after the previous one.
+ */
+static bool read_fwht_frame(cv4l_fmt &fmt, unsigned char *buf,
+			    FILE *fpointer, unsigned &sz,
+			    unsigned &expected_len, unsigned buf_len)
+{
+	expected_len = sizeof(struct fwht_cframe_hdr);
+	if (expected_len > buf_len)
+		return false;
+	sz = fread(&last_fwht_hdr, 1, sizeof(struct fwht_cframe_hdr), fpointer);
+	if (sz < sizeof(struct fwht_cframe_hdr))
+		return true;
+
+	expected_len = ntohl(last_fwht_hdr.size);
+	if (expected_len > buf_len)
+		return false;
+	sz = fread(buf, 1, ntohl(last_fwht_hdr.size), fpointer);
+	return true;
+}
+
+static void set_fwht_stateless_params(struct v4l2_ctrl_fwht_params &fwht_params,
+				      const struct fwht_cframe_hdr *hdr,
+				      __u64 last_bf_ts)
+{
+	fwht_params.backward_ref_ts = last_bf_ts;
+	fwht_params.version = ntohl(hdr->version);
+	fwht_params.width = ntohl(hdr->width);
+	fwht_params.height = ntohl(hdr->height);
+	fwht_params.flags = ntohl(hdr->flags);
+	fwht_params.colorspace = ntohl(hdr->colorspace);
+	fwht_params.xfer_func = ntohl(hdr->xfer_func);
+	fwht_params.ycbcr_enc = ntohl(hdr->ycbcr_enc);
+	fwht_params.quantization = ntohl(hdr->quantization);
+
+	/*
+	 * if last_bf_ts is 0 it indicates that either this is the
+	 * first frame, or that last frame returned with an error so
+	 * it is better not to reference it so the error won't propagate
+	 */
+	if (!last_bf_ts)
+		fwht_params.flags |= FWHT_FL_I_FRAME;
+}
+
+static int alloc_fwht_req(int media_fd, unsigned index)
+{
+	int rc = 0;
+
+	rc = ioctl(media_fd, MEDIA_IOC_REQUEST_ALLOC, &fwht_reqs[index]);
+	if (rc < 0) {
+		fprintf(stderr, "Unable to allocate media request: %s\n",
+			strerror(errno));
+		return rc;
+	}
+
+	return 0;
+}
+
+static void set_fwht_req_by_idx(unsigned idx, struct fwht_cframe_hdr *hdr,
+				__u64 last_bf_ts, __u64 ts)
+{
+	struct v4l2_ctrl_fwht_params fwht_params;
+
+	set_fwht_stateless_params(fwht_params, hdr, last_bf_ts);
+
+	fwht_reqs[idx].ts = ts;
+	fwht_reqs[idx].params = fwht_params;
+}
+
+static int get_fwht_req_by_ts(__u64 ts)
+{
+	for (int idx = 0; idx < VIDEO_MAX_FRAME; idx++) {
+		if (fwht_reqs[idx].ts == ts)
+			return idx;
+	}
+	return -1;
+}
+
+static bool set_fwht_req_by_fd(struct fwht_cframe_hdr *hdr,
+			       int req_fd, __u64 last_bf_ts, __u64 ts)
+{
+	struct v4l2_ctrl_fwht_params fwht_params;
+
+	set_fwht_stateless_params(fwht_params, hdr, last_bf_ts);
+
+	for (int idx = 0; idx < VIDEO_MAX_FRAME; idx++) {
+		if (fwht_reqs[idx].fd == req_fd) {
+			fwht_reqs[idx].ts = ts;
+			fwht_reqs[idx].params = fwht_params;
+			return true;
+		}
+	}
+	return false;
+}
+
+static int set_fwht_ext_ctrl(cv4l_fd &fd, struct fwht_cframe_hdr *hdr,
+			     __u64 last_bf_ts, int req_fd)
+{
+	v4l2_ext_controls controls;
+	struct v4l2_ext_control control;
+	struct v4l2_ctrl_fwht_params fwht_params;
+
+	memset(&control, 0, sizeof(control));
+	memset(&controls, 0, sizeof(controls));
+
+	set_fwht_stateless_params(fwht_params, hdr, last_bf_ts);
+
+	control.id = V4L2_CID_MPEG_VIDEO_FWHT_PARAMS;
+	control.ptr = &fwht_params;
+	control.size = sizeof(fwht_params);
+	controls.which = V4L2_CTRL_WHICH_REQUEST_VAL;
+	controls.request_fd = req_fd;
+	controls.controls = &control;
+	controls.count = 1;
+	return fd.s_ext_ctrls(controls);
+}
+
 static bool read_write_padded_frame(cv4l_fmt &fmt, unsigned char *buf,
 				    FILE *fpointer, unsigned &sz,
 				    unsigned &expected_len, unsigned buf_len,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [v4l-utils PATCH v5 6/6] v4l2-ctl: Add implementation for the stateless fwht decoder.
  2019-03-06 21:17 [v4l-utils PATCH v5 1/6] v4l2-ctl: in streaming_set_m2m, close file pointers upon error Dafna Hirschfeld
                   ` (3 preceding siblings ...)
  2019-03-06 21:17 ` [v4l-utils PATCH v5 5/6] v4l2-ctl: Add functions and variables to support fwht stateless decoder Dafna Hirschfeld
@ 2019-03-06 21:17 ` Dafna Hirschfeld
  4 siblings, 0 replies; 6+ messages in thread
From: Dafna Hirschfeld @ 2019-03-06 21:17 UTC (permalink / raw)
  To: linux-media; +Cc: hverkuil, helen.koike, Dafna Hirschfeld

Add code to support the stateless decoder
and the function 'stateless_m2m' that implements it.

Signed-off-by: Dafna Hirschfeld <dafna3@gmail.com>
---
 utils/v4l2-ctl/v4l2-ctl-streaming.cpp | 218 +++++++++++++++++++++++++-
 1 file changed, 210 insertions(+), 8 deletions(-)

diff --git a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
index 4bb2d301..2911e74f 100644
--- a/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
+++ b/utils/v4l2-ctl/v4l2-ctl-streaming.cpp
@@ -1074,6 +1074,7 @@ restart:
 		unsigned expected_len = q.g_length(j);
 		unsigned sz;
 		cv4l_fmt fmt;
+		bool res = true;
 
 		fd.g_fmt(fmt, q.g_type());
 		if (from_with_hdr) {
@@ -1085,16 +1086,22 @@ restart:
 			}
 		}
 
-		if (support_out_crop && v4l2_fwht_find_pixfmt(fmt.g_pixelformat())) {
-			if (!read_write_padded_frame(fmt, (unsigned char *)buf,
-			    fin, sz, expected_len, buf_len, true))
-				return false;
-		} else {
+		if (fmt.g_pixelformat() == V4L2_PIX_FMT_FWHT_STATELESS)
+			res = read_fwht_frame(fmt, (unsigned char *)buf, fin,
+					      sz, expected_len, buf_len);
+		else if (support_out_crop && v4l2_fwht_find_pixfmt(fmt.g_pixelformat()))
+			res = read_write_padded_frame(fmt, (unsigned char *)buf,
+						      fin, sz, expected_len, buf_len, true);
+		else
 			sz = fread(buf, 1, expected_len, fin);
-		}
 
+		if (!res) {
+			fprintf(stderr, "amount intended to be read/written is larger than the buffer size\n");
+			return false;
+		}
 		if (first && sz != expected_len) {
-			fprintf(stderr, "Insufficient data\n");
+			fprintf(stderr, "%s: size read (%u) is different than needed (%u) in the first frame\n",
+				__func__, sz, expected_len);
 			return false;
 		}
 		if (j == 0 && sz == 0 && stream_loop) {
@@ -1219,6 +1226,26 @@ static int do_setup_out_buffers(cv4l_fd &fd, cv4l_queue &q, FILE *fin, bool qbuf
 		if (fin && !fill_buffer_from_file(fd, q, buf, fmt, fin))
 			return -2;
 
+		if (fmt.g_pixelformat() == V4L2_PIX_FMT_FWHT_STATELESS) {
+			int media_fd = mi_get_media_fd(fd.g_fd());
+
+			if (media_fd < 0) {
+				fprintf(stderr, "%s: mi_get_media_fd failed\n", __func__);
+				return media_fd;
+			}
+
+			if (alloc_fwht_req(media_fd, i))
+				return -1;
+			buf.s_request_fd(fwht_reqs[i].fd);
+			buf.or_flags(V4L2_BUF_FLAG_REQUEST_FD);
+
+			if (set_fwht_ext_ctrl(fd, &last_fwht_hdr, last_fwht_bf_ts,
+					      buf.g_request_fd())) {
+				fprintf(stderr, "%s: set_fwht_ext_ctrls failed on %dth buf: %s\n",
+					__func__, i, strerror(errno));
+				return -1;
+			}
+		}
 		if (qbuf) {
 			set_time_stamp(buf);
 			if (fd.qbuf(buf))
@@ -1228,6 +1255,16 @@ static int do_setup_out_buffers(cv4l_fd &fd, cv4l_queue &q, FILE *fin, bool qbuf
 				fprintf(stderr, ">");
 			fflush(stderr);
 		}
+		if (fmt.g_pixelformat() == V4L2_PIX_FMT_FWHT_STATELESS) {
+			set_fwht_req_by_idx(i, &last_fwht_hdr,
+					    last_fwht_bf_ts, get_ns_timestamp(buf));
+			last_fwht_bf_ts = get_ns_timestamp(buf);
+			if (ioctl(buf.g_request_fd(), MEDIA_REQUEST_IOC_QUEUE) < 0) {
+				fprintf(stderr, "Unable to queue media request: %s\n",
+					strerror(errno));
+				return -1;
+			}
+		}
 	}
 	if (qbuf)
 		output_field = field;
@@ -1466,12 +1503,43 @@ static int do_handle_out(cv4l_fd &fd, cv4l_queue &q, FILE *fin, cv4l_buffer *cap
 				       (u8 *)q.g_dataptr(buf.g_index(), j));
 	}
 
+	if (fmt.g_pixelformat() == V4L2_PIX_FMT_FWHT_STATELESS) {
+		if (ioctl(buf.g_request_fd(), MEDIA_REQUEST_IOC_REINIT, NULL)) {
+			fprintf(stderr, "Unable to reinit media request: %s\n",
+				strerror(errno));
+			return -1;
+		}
+
+		if (set_fwht_ext_ctrl(fd, &last_fwht_hdr, last_fwht_bf_ts,
+				      buf.g_request_fd())) {
+			fprintf(stderr, "%s: set_fwht_ext_ctrls failed: %s\n",
+				__func__, strerror(errno));
+			return -1;
+		}
+	}
+
 	set_time_stamp(buf);
 
 	if (fd.qbuf(buf)) {
 		fprintf(stderr, "%s: failed: %s\n", "VIDIOC_QBUF", strerror(errno));
 		return -1;
 	}
+	if (fmt.g_pixelformat() == V4L2_PIX_FMT_FWHT_STATELESS) {
+		if (!set_fwht_req_by_fd(&last_fwht_hdr, buf.g_request_fd(), last_fwht_bf_ts,
+					get_ns_timestamp(buf))) {
+			fprintf(stderr, "%s: request for fd %d does not exist\n",
+				__func__, buf.g_request_fd());
+			return -1;
+		}
+
+		last_fwht_bf_ts = get_ns_timestamp(buf);
+		if (ioctl(buf.g_request_fd(), MEDIA_REQUEST_IOC_QUEUE) < 0) {
+			fprintf(stderr, "Unable to queue media request: %s\n",
+				strerror(errno));
+			return -1;
+		}
+	}
+
 	tpg_update_mv_count(&tpg, V4L2_FIELD_HAS_T_OR_B(output_field));
 
 	if (!verbose)
@@ -2257,6 +2325,137 @@ static void stateful_m2m(cv4l_fd &fd, cv4l_queue &in, cv4l_queue &out,
 	tpg_free(&tpg);
 }
 
+static void stateless_m2m(cv4l_fd &fd, cv4l_queue &in, cv4l_queue &out,
+			  FILE *fin, FILE *fout, cv4l_fmt &fmt_in,
+			  cv4l_fmt &fmt_out, cv4l_fd *exp_fd_p)
+{
+	fps_timestamps fps_ts[2];
+	unsigned count[2] = { 0, 0 };
+	int fd_flags = fcntl(fd.g_fd(), F_GETFL);
+
+	if (out.reqbufs(&fd, reqbufs_count_out)) {
+		fprintf(stderr, "%s: out.reqbufs failed\n", __func__);
+		return;
+	}
+
+	if (in.reqbufs(&fd, reqbufs_count_cap)) {
+		fprintf(stderr, "%s: in.reqbufs failed\n", __func__);
+		return;
+	}
+
+	if (exp_fd_p && in.export_bufs(exp_fd_p, exp_fd_p->g_type()))
+		return;
+
+	if (in.obtain_bufs(&fd)) {
+		fprintf(stderr, "%s: in.obtain_bufs error\n", __func__);
+		return;
+	}
+
+	if (do_setup_out_buffers(fd, out, fout, true) == -1) {
+		fprintf(stderr, "%s: do_setup_out_buffers failed\n", __func__);
+		return;
+	}
+
+	if (in.queue_all(&fd)) {
+		fprintf(stderr, "%s: in.queue_all failed\n", __func__);
+		return;
+	}
+
+	if (fd.streamon(out.g_type())) {
+		fprintf(stderr, "%s: streamon for out failed\n", __func__);
+		return;
+	}
+
+	if (fd.streamon(in.g_type())) {
+		fprintf(stderr, "%s: streamon for in failed\n", __func__);
+		return;
+	}
+	int index = 0;
+	bool queue_lst_buf = false;
+	cv4l_buffer last_in_buf;
+
+	fcntl(fd.g_fd(), F_SETFL, fd_flags | O_NONBLOCK);
+
+	while (true) {
+		fd_set except_fds;
+		int req_fd =fwht_reqs[index].fd;
+		struct timeval tv = { 2, 0 };
+
+		FD_ZERO(&except_fds);
+		FD_SET(req_fd, &except_fds);
+
+		int rc = select(req_fd + 1, NULL, NULL, &except_fds, &tv);
+
+		if (rc == 0) {
+			fprintf(stderr, "Timeout when waiting for media request\n");
+			return;
+		} else if (rc < 0) {
+			fprintf(stderr, "Unable to select media request: %s\n",
+				strerror(errno));
+			return;
+		}
+		/*
+		 * it is safe to queue back last cap buffer only after
+		 * the following request is done so that the buffer
+		 * is not needed anymore as a reference frame
+		 */
+		if (queue_lst_buf) {
+			if (fd.qbuf(last_in_buf) < 0) {
+				fprintf(stderr, "%s: qbuf failed\n", __func__);
+				return;
+			}
+		}
+		int buf_idx = -1;
+		  /*
+		   * fin is not sent to do_handle_cap since the capture buf is
+		   * written to the file in current function
+		   */
+		rc = do_handle_cap(fd, in, NULL, &buf_idx, count[CAP],
+				   fps_ts[CAP], fmt_in);
+		if (rc) {
+			fprintf(stderr, "%s: do_handle_cap err\n", __func__);
+			return;
+		}
+		/*
+		 * in case of an error in the frame, set last ts to 0 as a mean
+		 * to recover so that next request will not use a
+		 * reference buffer. Otherwise the error flag will be set to
+		 * all the future capture buffers.
+		 */
+		if (buf_idx == -1) {
+			fprintf(stderr, "%s: frame returned with error\n", __func__);
+			last_fwht_bf_ts	= 0;
+		} else {
+			cv4l_buffer cap_buf(in, index);
+			if (fd.querybuf(cap_buf))
+				return;
+			last_in_buf = cap_buf;
+			queue_lst_buf = true;
+			if (fin && cap_buf.g_bytesused(0) &&
+			    !(cap_buf.g_flags() & V4L2_BUF_FLAG_ERROR)) {
+				int idx = get_fwht_req_by_ts(get_ns_timestamp(cap_buf));
+
+				if (idx < 0) {
+					fprintf(stderr, "%s: could not find request from buffer\n", __func__);
+					fprintf(stderr, "%s: ts = %llu\n", __func__, get_ns_timestamp(cap_buf));
+					return;
+				}
+				composed_width = fwht_reqs[idx].params.width;
+				composed_height = fwht_reqs[idx].params.height;
+				write_buffer_to_file(fd, in, cap_buf,
+						     fmt_in, fin);
+			}
+		}
+		rc = do_handle_out(fd, out, fout, NULL, count[OUT],
+				   fps_ts[OUT], fmt_out);
+		if (rc) {
+			fprintf(stderr, "%s: output stream ended\n", __func__);
+			close(req_fd);
+		}
+		index = (index + 1) % out.g_buffers();
+	}
+}
+
 static void streaming_set_m2m(cv4l_fd &fd, cv4l_fd &exp_fd)
 {
 	cv4l_queue in(fd.g_type(), memory);
@@ -2297,7 +2496,10 @@ static void streaming_set_m2m(cv4l_fd &fd, cv4l_fd &exp_fd)
 		if (out.export_bufs(&exp_fd, exp_fd.g_type()))
 			goto done;
 	}
-	stateful_m2m(fd, in, out, file[CAP], file[OUT], exp_fd_p);
+	if (fmt[OUT].g_pixelformat() == V4L2_PIX_FMT_FWHT_STATELESS)
+		stateless_m2m(fd, in, out, file[CAP], file[OUT], fmt[CAP], fmt[OUT], exp_fd_p);
+	else
+		stateful_m2m(fd, in, out, file[CAP], file[OUT], fmt[CAP], fmt[OUT], exp_fd_p);
 
 done:
 	if (options[OptStreamDmaBuf] || options[OptStreamOutDmaBuf])
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-03-06 21:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-06 21:17 [v4l-utils PATCH v5 1/6] v4l2-ctl: in streaming_set_m2m, close file pointers upon error Dafna Hirschfeld
2019-03-06 21:17 ` [v4l-utils PATCH v5 2/6] v4l2-ctl: check that the size read/write fit the buffer size Dafna Hirschfeld
2019-03-06 21:17 ` [v4l-utils PATCH v5 3/6] v4l2-ctl: set the in/out fmt variables in streaming_set_m2m Dafna Hirschfeld
2019-03-06 21:17 ` [v4l-utils PATCH v5 4/6] v4l-utils: copy fwht-ctrls.h from kernel dir Dafna Hirschfeld
2019-03-06 21:17 ` [v4l-utils PATCH v5 5/6] v4l2-ctl: Add functions and variables to support fwht stateless decoder Dafna Hirschfeld
2019-03-06 21:17 ` [v4l-utils PATCH v5 6/6] v4l2-ctl: Add implementation for the stateless fwht decoder Dafna Hirschfeld

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).