Skip to contentSkip to conversation

media: vimc: Add a V4L2 output device

PATCH In review André Almeida 3 participants 15 messages Jul 2, 2019 – Jul 31, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover media: vimc: Add a V4L2 output device

Hello,

This patch adds a V4L2 output device on vimc, that comply with V4L2 API for video output. If there is an output device and a capture device at the same pipeline, one can get a video loopback pipeline feeding frames at the output and then seeing them at the capture. It's possible to insert vimc submodules at the pipeline to modify the image (e.g. a scaler).

If one starts a streaming at the capture, with the output off, the capture will display a noisy frame. If one starts a streaming at the output with the capture off, the output will just consume the buffers, without sending them to the pipeline. If both output and capture are streaming, the loopback will happen.

The patches 1 and 2 provide some ground to create the output device. The patch 3 creates the device and modify how the vimc-streamer was dealing with the s_stream callback on other vimc modules, to make simpler implementing this callback at vimc-output. Patch 4 change the behavior of the pipeline in order to be closer to a real life hardware. Patches 5-7 updates the default pipeline and the documentation to include the new output device.

This is the result of v4l2-compliance after this patch series:

$ v4l2-compliance -m0 -s50
Grand Total for vimc device /dev/media0: 476, Succeeded: 476, Failed: 0,
Warnings: 0

A git tree up to date with media-master and with this changes can be found
at: https://gitlab.collabora.com/tonyk/linux/tree/vimc/output

In order to test it, one can follow these instructions:

1 - Configure the pipeline (requires v4l-utils):

$ media-ctl -d platform:vimc -V '"Sensor A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Sensor B":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer B":0[fmt:SBGGR8_1X8/640x480]'
$ v4l2-ctl -z platform:vimc -d "RGB/YUV Capture" -v width=1920,height=1440
$ v4l2-ctl -z platform:vimc -d "Raw Capture 0" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -d "Raw Capture 1" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -e "RGB/YUV Input" -v width=640,height=480

2 - Use a userspace application:
2.a gst-launch (requires gstreamer and gst-plugins-good):

Feed frames into the output and grab from the capture (rescaled for convenience):

$ gst-launch-1.0 videotestsrc pattern=ball ! \
	video/x-raw,width=640,height=480,format=RGB \
	! v4l2sink device=/dev/video2 v4l2src device=/dev/video3 ! \
	video/x-raw,width=1920,height=1440,format=RGB ! videoscale ! \
	video/x-raw,width=640,height=480 ! videoconvert ! ximagesink

2.b qv4l2 (requires v4l-utils):

Open the output device:

$ qv4l2 -d2

Open the capture device:

$ qv4l2 -d3

Start the streaming at both, at any order. You can change the frame content at "Test Pattern Generator" -> "Test Pattern" on the output.

Thanks,
André

André Almeida (7):
media: vimc: Create video module
media: vimc: video: Add write file operation
media: vimc: Create a V4L2 output device
media: vimc: Send null buffer through the pipeline
media: vimc: core: Add output device on the pipeline
media: vimc.dot: Update default topology diagram
media: vimc.rst: Add output device

 Documentation/media/v4l-drivers/vimc.dot    |   4 +-
 Documentation/media/v4l-drivers/vimc.rst    |  12 +-
 drivers/media/platform/vimc/Makefile        |   4 +-
 drivers/media/platform/vimc/vimc-capture.c  | 356 +++----------------
 drivers/media/platform/vimc/vimc-common.h   |   5 +-
 drivers/media/platform/vimc/vimc-core.c     |   7 +-
 drivers/media/platform/vimc/vimc-debayer.c  |  14 +-
 drivers/media/platform/vimc/vimc-output.c   | 362 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-scaler.c   |  13 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  10 +-
 drivers/media/platform/vimc/vimc-streamer.c |  24 +-
 drivers/media/platform/vimc/vimc-video.c    | 273 +++++++++++++++
 drivers/media/platform/vimc/vimc-video.h    | 130 +++++++
 13 files changed, 849 insertions(+), 365 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-output.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.h
Signature · 1 line
2.22.0
#1 lore raw

PATCH 1/7 media: vimc: Create video module

V4L2 video capture and video output devices shares a lot of common code. To enhance code reuse with future video output, split vimc-capture.c in three files: vimc-capture.c and vimc-video.{c,h}. Keep strict capture related functions on vimc-capture.c. This change is meant to the future addition of a video output device in vimc, as it will make easier for code reuse and simplicity.

---
 drivers/media/platform/vimc/Makefile       |   2 +-
 drivers/media/platform/vimc/vimc-capture.c | 341 ++-------------------
 drivers/media/platform/vimc/vimc-video.c   | 264 ++++++++++++++++
 drivers/media/platform/vimc/vimc-video.h   | 127 ++++++++
 4 files changed, 417 insertions(+), 317 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-video.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.h
drivers/media/platform/vimc/Makefile +1 -1
index 96d06f030c31..fb90aa0f33a5 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 vimc-y := vimc-core.o vimc-common.o vimc-streamer.o
 
-obj-$(CONFIG_VIDEO_VIMC) += vimc.o vimc-capture.o vimc-debayer.o \
+obj-$(CONFIG_VIDEO_VIMC) += vimc.o vimc-video.o vimc-capture.o vimc-debayer.o \
                 vimc-scaler.o vimc-sensor.o
drivers/media/platform/vimc/vimc-capture.c +25 -316

433 lines not on this page · raw message

drivers/media/platform/vimc/vimc-video.c new file +264 -0

264 lines not on this page · raw message

drivers/media/platform/vimc/vimc-video.h new file +127 -0

127 lines not on this page · raw message

Signature · 1 line
2.22.0
#2 lore raw

PATCH 2/7 media: vimc: video: Add write file operation

In reply to André Almeida

Add write on the list of vb2 file operations. This is required to create a V4L2 output device.

---
 drivers/media/platform/vimc/vimc-video.h | 1 +
 1 file changed, 1 insertion(+)
drivers/media/platform/vimc/vimc-video.h +1 -0
index d329345cc77f..22cb0e2dbdbb 100644
@@ -35,6 +35,7 @@ static const struct v4l2_file_operations vimc_vid_fops = {
 	.open		= v4l2_fh_open,
 	.release	= vb2_fop_release,
 	.read           = vb2_fop_read,
+	.write          = vb2_fop_write,
 	.poll		= vb2_fop_poll,
 	.unlocked_ioctl = video_ioctl2,
 	.mmap           = vb2_fop_mmap,
Signature · 1 line
2.22.0
#3 lore raw

PATCH 3/7 media: vimc: Create a V4L2 output device

In reply to André Almeida

Using the functions on vimc-video, create a V4L2 output device. When a streaming is initialized on the output device, it'll start returning those buffers on to the userspace, while always keeping one on the list. When the capture device starts streaming on the same pipeline, it will copy the first frame on the list to the process_frame pipeline.

The vimc-streamer has a callback to warn all subdevices when a streaming will start or stop. This is useful so the subdevices can allocate a frame with suitable size to send through the pipeline, and then free then when it not used anymore. Since the vimc-output will also be part of the pipeline (but it is not a V4L2 subdevice), change the callback to be part of vimc_ent_device, rather than a linked with the subdevice API. In this way, it's possible to have a generic solution to call start/stop streaming callbacks to all devices in the pipeline.

Add a vb2 buffer validate function. This is required to create a V4L2 output device.

---
 drivers/media/platform/vimc/Makefile        |   4 +-
 drivers/media/platform/vimc/vimc-common.h   |   5 +-
 drivers/media/platform/vimc/vimc-debayer.c  |  11 +-
 drivers/media/platform/vimc/vimc-output.c   | 362 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-scaler.c   |  10 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  10 +-
 drivers/media/platform/vimc/vimc-streamer.c |  22 +-
 drivers/media/platform/vimc/vimc-video.c    |   9 +
 drivers/media/platform/vimc/vimc-video.h    |   2 +
 9 files changed, 395 insertions(+), 40 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-output.c
drivers/media/platform/vimc/Makefile +2 -2
index fb90aa0f33a5..611331b9fceb 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 vimc-y := vimc-core.o vimc-common.o vimc-streamer.o
 
-obj-$(CONFIG_VIDEO_VIMC) += vimc.o vimc-video.o vimc-capture.o vimc-debayer.o \
-                vimc-scaler.o vimc-sensor.o
+obj-$(CONFIG_VIDEO_VIMC) += vimc.o vimc-video.o vimc-capture.o vimc-output.o \
+                vimc-debayer.o vimc-scaler.o vimc-sensor.o
drivers/media/platform/vimc/vimc-common.h +4 -1
index 7b4d988b208b..003024bcdaed 100644
@@ -78,6 +78,8 @@ struct vimc_platform_data {
  * @vdev_get_format:	callback that returns the current format a pad, used
  *			only when is_media_entity_v4l2_video_device(ent) returns
  *			true
+ * @s_stream:		callback used when vimc-capture will start/stop a
+ *			streaming, to subdevice alloc/free it frames
  *
  * Each node of the topology must create a vimc_ent_device struct. Depending on
  * the node it will be of an instance of v4l2_subdev or video_device struct
@@ -94,7 +96,8 @@ struct vimc_ent_device {
 	void * (*process_frame)(struct vimc_ent_device *ved,
 				const void *frame);
 	void (*vdev_get_format)(struct vimc_ent_device *ved,
-			      struct v4l2_pix_format *fmt);
+				struct v4l2_pix_format *fmt);
+	int (*s_stream)(struct vimc_ent_device *ved, int enable);
 };
 
 /**
drivers/media/platform/vimc/vimc-debayer.c +4 -7
index 00598fbf3cba..9e0c7c2c3c72 100644
@@ -320,9 +320,10 @@ static void vimc_deb_set_rgb_pix_rgb24(struct vimc_deb_device *vdeb,
 		vdeb->src_frame[index + i] = rgb[i];
 }
 
-static int vimc_deb_s_stream(struct v4l2_subdev *sd, int enable)
+static int vimc_deb_s_stream(struct vimc_ent_device *ved, int enable)
 {
-	struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
+	struct vimc_deb_device *vdeb = container_of(ved, struct vimc_deb_device,
+						    ved);
 
 	if (enable) {
 		u32 src_pixelformat = vdeb->ved.stream->producer_pixfmt;
@@ -373,13 +374,8 @@ static int vimc_deb_s_stream(struct v4l2_subdev *sd, int enable)
 	return 0;
 }
 
-static const struct v4l2_subdev_video_ops vimc_deb_video_ops = {
-	.s_stream = vimc_deb_s_stream,
-};
-
 static const struct v4l2_subdev_ops vimc_deb_ops = {
 	.pad = &vimc_deb_pad_ops,
-	.video = &vimc_deb_video_ops,
 };
 
 static unsigned int vimc_deb_get_val(const u8 *bytes,
@@ -549,6 +545,7 @@ static int vimc_deb_comp_bind(struct device *comp, struct device *master,
 	}
 
 	vdeb->ved.process_frame = vimc_deb_process_frame;
+	vdeb->ved.s_stream = vimc_deb_s_stream;
 	dev_set_drvdata(comp, &vdeb->ved);
 	vdeb->dev = comp;
 
drivers/media/platform/vimc/vimc-output.c new file +362 -0

362 lines not on this page · raw message

drivers/media/platform/vimc/vimc-scaler.c +4 -6
index c7123a45c55b..2c3e486a29c0 100644
@@ -194,9 +194,10 @@ static const struct v4l2_subdev_pad_ops vimc_sca_pad_ops = {
 	.set_fmt		= vimc_sca_set_fmt,
 };
 
-static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
+static int vimc_sca_s_stream(struct vimc_ent_device *ved, int enable)
 {
-	struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
+	struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
+						    ved);
 
 	if (enable) {
 		u32 pixelformat = vsca->ved.stream->producer_pixfmt;
@@ -239,13 +240,9 @@ static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
 	return 0;
 }
 
-static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
-	.s_stream = vimc_sca_s_stream,
-};
 
 static const struct v4l2_subdev_ops vimc_sca_ops = {
 	.pad = &vimc_sca_pad_ops,
-	.video = &vimc_sca_video_ops,
 };
 
 static void vimc_sca_fill_pix(u8 *const ptr,
@@ -381,6 +378,7 @@ static int vimc_sca_comp_bind(struct device *comp, struct device *master,
 	}
 
 	vsca->ved.process_frame = vimc_sca_process_frame;
+	vsca->ved.s_stream = vimc_sca_s_stream,
 	dev_set_drvdata(comp, &vsca->ved);
 	vsca->dev = comp;
 
drivers/media/platform/vimc/vimc-sensor.c +3 -7
index 51359472eef2..a1afb840493d 100644
@@ -177,10 +177,10 @@ static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
 	return vsen->frame;
 }
 
-static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
+static int vimc_sen_s_stream(struct vimc_ent_device *ved, int enable)
 {
 	struct vimc_sen_device *vsen =
-				container_of(sd, struct vimc_sen_device, sd);
+				container_of(ved, struct vimc_sen_device, ved);
 
 	if (enable) {
 		u32 pixelformat = vsen->ved.stream->producer_pixfmt;
@@ -218,14 +218,9 @@ static const struct v4l2_subdev_core_ops vimc_sen_core_ops = {
 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
 };
 
-static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
-	.s_stream = vimc_sen_s_stream,
-};
-
 static const struct v4l2_subdev_ops vimc_sen_ops = {
 	.core = &vimc_sen_core_ops,
 	.pad = &vimc_sen_pad_ops,
-	.video = &vimc_sen_video_ops,
 };
 
 static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl)
@@ -351,6 +346,7 @@ static int vimc_sen_comp_bind(struct device *comp, struct device *master,
 		goto err_free_hdl;
 
 	vsen->ved.process_frame = vimc_sen_process_frame;
+	vsen->ved.s_stream = vimc_sen_s_stream;
 	dev_set_drvdata(comp, &vsen->ved);
 	vsen->dev = comp;
 
drivers/media/platform/vimc/vimc-streamer.c +5 -17
index 3b3f36357a0e..fd330415ac38 100644
@@ -47,7 +47,6 @@ static struct media_entity *vimc_get_source_entity(struct media_entity *ent)
 static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
 {
 	struct vimc_ent_device *ved;
-	struct v4l2_subdev *sd;
 
 	while (stream->pipe_size) {
 		stream->pipe_size--;
@@ -55,11 +54,8 @@ static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
 		ved->stream = NULL;
 		stream->ved_pipeline[stream->pipe_size] = NULL;
 
-		if (!is_media_entity_v4l2_subdev(ved->ent))
-			continue;
-
-		sd = media_entity_to_v4l2_subdev(ved->ent);
-		v4l2_subdev_call(sd, video, s_stream, 0);
+		if (ved->s_stream)
+			ved->s_stream(ved, 0);
 	}
 }
 
@@ -79,7 +75,6 @@ static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
 	struct media_entity *entity;
 	struct video_device *vdev;
 	struct v4l2_subdev *sd;
-	int ret = 0;
 
 	stream->pipe_size = 0;
 	while (stream->pipe_size < VIMC_STREAMER_PIPELINE_MAX_SIZE) {
@@ -90,16 +85,8 @@ static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
 		stream->ved_pipeline[stream->pipe_size++] = ved;
 		ved->stream = stream;
 
-		if (is_media_entity_v4l2_subdev(ved->ent)) {
-			sd = media_entity_to_v4l2_subdev(ved->ent);
-			ret = v4l2_subdev_call(sd, video, s_stream, 1);
-			if (ret && ret != -ENOIOCTLCMD) {
-				pr_err("subdev_call error %s\n",
-				       ved->ent->name);
-				vimc_streamer_pipeline_terminate(stream);
-				return ret;
-			}
-		}
+		if (ved->s_stream)
+			ved->s_stream(ved, 1);
 
 		entity = vimc_get_source_entity(ved->ent);
 		/* Check if the end of the pipeline was reached*/
@@ -149,6 +136,7 @@ static int vimc_streamer_thread(void *data)
 			if (!frame || IS_ERR(frame))
 				break;
 		}
+
 		//wait for 60hz
 		set_current_state(TASK_UNINTERRUPTIBLE);
 		schedule_timeout(HZ / 60);
drivers/media/platform/vimc/vimc-video.c +9 -0
index f7ccb2e9d6c5..0187e699543a 100644
@@ -236,6 +236,15 @@ int vimc_vid_buffer_prepare(struct vb2_buffer *vb)
 }
 EXPORT_SYMBOL_GPL(vimc_vid_buffer_prepare);
 
+int vimc_vid_buffer_validate(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+
+	vbuf->field = V4L2_FIELD_NONE;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(vimc_vid_buffer_validate);
+
 void vimc_vid_release(struct video_device *vdev)
 {
 	struct vimc_vid_device *vid =
drivers/media/platform/vimc/vimc-video.h +2 -0
index 22cb0e2dbdbb..67f13244bf92 100644
@@ -116,6 +116,8 @@ int vimc_vid_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
 
 int vimc_vid_buffer_prepare(struct vb2_buffer *vb);
 
+int vimc_vid_buffer_validate(struct vb2_buffer *vb);
+
 int vimc_vid_probe(struct platform_device *pdev);
 
 int vimc_vid_remove(struct platform_device *pdev);
Signature · 1 line
2.22.0
#4 lore raw

Re: [PATCH 3/7] media: vimc: Create a V4L2 output device

Hi André,

Thanks for the patch, just some minor comments below.

Quoted 49 lines · André Almeida

Using the functions on vimc-video, create a V4L2 output device. When a streaming is initialized on the output device, it'll start returning those buffers on to the userspace, while always keeping one on the list. When the capture device starts streaming on the same pipeline, it will copy the first frame on the list to the process_frame pipeline.

The vimc-streamer has a callback to warn all subdevices when a streaming will start or stop. This is useful so the subdevices can allocate a frame with suitable size to send through the pipeline, and then free then when it not used anymore. Since the vimc-output will also be part of the pipeline (but it is not a V4L2 subdevice), change the callback to be part of vimc_ent_device, rather than a linked with the subdevice API. In this way, it's possible to have a generic solution to call start/stop streaming callbacks to all devices in the pipeline.

Add a vb2 buffer validate function. This is required to create a V4L2 output device.

---
 drivers/media/platform/vimc/Makefile        |   4 +-
 drivers/media/platform/vimc/vimc-common.h   |   5 +-
 drivers/media/platform/vimc/vimc-debayer.c  |  11 +-
 drivers/media/platform/vimc/vimc-output.c   | 362 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-scaler.c   |  10 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  10 +-
 drivers/media/platform/vimc/vimc-streamer.c |  22 +-
 drivers/media/platform/vimc/vimc-video.c    |   9 +
 drivers/media/platform/vimc/vimc-video.h    |   2 +
 9 files changed, 395 insertions(+), 40 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-output.c
drivers/media/platform/vimc/Makefile +2 -2
index fb90aa0f33a5..611331b9fceb 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 vimc-y := vimc-core.o vimc-common.o vimc-streamer.o
 
-obj-$(CONFIG_VIDEO_VIMC) += vimc.o vimc-video.o vimc-capture.o vimc-debayer.o \
-                vimc-scaler.o vimc-sensor.o
+obj-$(CONFIG_VIDEO_VIMC) += vimc.o vimc-video.o vimc-capture.o vimc-output.o \
+                vimc-debayer.o vimc-scaler.o vimc-sensor.o
drivers/media/platform/vimc/vimc-common.h +0 -0
index 7b4d988b208b..003024bcdaed 100644

drivers/media/platform/vimc/vimc-common.h struct vimc_platform_data { · line 82

Quoted 5 lines
drivers/media/platform/vimc/vimc-common.h +2 -0
@@ -78,6 +78,8 @@ struct vimc_platform_data {
  * @vdev_get_format:	callback that returns the current format a pad, used
  *			only when is_media_entity_v4l2_video_device(ent) returns
  *			true
+ * @s_stream:		callback used when vimc-capture will start/stop a
+ *			streaming, to subdevice alloc/free it frames

I would re-word this a bit:
callback used when vimc-capture start/stop streaming, so subdevices can alloc/free frames.

Quoted 87 lines
drivers/media/platform/vimc/vimc-common.h +2 -1
  *
  * Each node of the topology must create a vimc_ent_device struct. Depending on
  * the node it will be of an instance of v4l2_subdev or video_device struct
@@ -94,7 +96,8 @@ struct vimc_ent_device {
 	void * (*process_frame)(struct vimc_ent_device *ved,
 				const void *frame);
 	void (*vdev_get_format)(struct vimc_ent_device *ved,
-			      struct v4l2_pix_format *fmt);
+				struct v4l2_pix_format *fmt);
+	int (*s_stream)(struct vimc_ent_device *ved, int enable);
 };
 
 /**
drivers/media/platform/vimc/vimc-debayer.c +4 -7
index 00598fbf3cba..9e0c7c2c3c72 100644
@@ -320,9 +320,10 @@ static void vimc_deb_set_rgb_pix_rgb24(struct vimc_deb_device *vdeb,
 		vdeb->src_frame[index + i] = rgb[i];
 }
 
-static int vimc_deb_s_stream(struct v4l2_subdev *sd, int enable)
+static int vimc_deb_s_stream(struct vimc_ent_device *ved, int enable)
 {
-	struct vimc_deb_device *vdeb = v4l2_get_subdevdata(sd);
+	struct vimc_deb_device *vdeb = container_of(ved, struct vimc_deb_device,
+						    ved);
 
 	if (enable) {
 		u32 src_pixelformat = vdeb->ved.stream->producer_pixfmt;
@@ -373,13 +374,8 @@ static int vimc_deb_s_stream(struct v4l2_subdev *sd, int enable)
 	return 0;
 }
 
-static const struct v4l2_subdev_video_ops vimc_deb_video_ops = {
-	.s_stream = vimc_deb_s_stream,
-};
-
 static const struct v4l2_subdev_ops vimc_deb_ops = {
 	.pad = &vimc_deb_pad_ops,
-	.video = &vimc_deb_video_ops,
 };
 
 static unsigned int vimc_deb_get_val(const u8 *bytes,
@@ -549,6 +545,7 @@ static int vimc_deb_comp_bind(struct device *comp, struct device *master,
 	}
 
 	vdeb->ved.process_frame = vimc_deb_process_frame;
+	vdeb->ved.s_stream = vimc_deb_s_stream;
 	dev_set_drvdata(comp, &vdeb->ved);
 	vdeb->dev = comp;
 
drivers/media/platform/vimc/vimc-output.c new file +29 -0
new file mode 100644
index 000000000000..3fb9441adc43
@@ -0,0 +1,362 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * vimc-output.c Virtual Media Controller Driver
+ *
+ * Copyright 2019 Collabora, Ltd.
+ */
+
+#include "vimc-video.h"
+#include <linux/kthread.h>
+#include <linux/freezer.h>
+
+#define VIMC_OUT_DRV_NAME "vimc-output"
+
+static const struct v4l2_ioctl_ops vimc_out_ioctl_ops = {
+	.vidioc_querycap = vimc_vid_querycap,
+
+	.vidioc_g_fmt_vid_out = vimc_vid_g_fmt,
+	.vidioc_s_fmt_vid_out = vimc_vid_s_fmt,
+	.vidioc_try_fmt_vid_out = vimc_vid_try_fmt,
+	.vidioc_enum_fmt_vid_out = vimc_vid_enum_fmt,
+	.vidioc_enum_framesizes = vimc_vid_enum_framesizes,
+
+	.vidioc_reqbufs = vb2_ioctl_reqbufs,
+	.vidioc_create_bufs = vb2_ioctl_create_bufs,
+	.vidioc_prepare_buf = vb2_ioctl_prepare_buf,
+	.vidioc_querybuf = vb2_ioctl_querybuf,
+	.vidioc_qbuf = vb2_ioctl_qbuf,
+	.vidioc_dqbuf = vb2_ioctl_dqbuf,
+	.vidioc_expbuf = vb2_ioctl_expbuf,

drivers/media/platform/vimc/vimc-output.c line 39

Quoted 10 lines
drivers/media/platform/vimc/vimc-output.c new file +10 -0
+	.vidioc_streamon = vb2_ioctl_streamon,
+	.vidioc_streamoff = vb2_ioctl_streamoff,
+};
+
+/**
+ * vimc_out_consume_frame - grab a frame from the queue and release it
+ *
+ * @arg: *vimc_vid_device passed as argument to kthread
+ *
+ * This function is called when the stream is on. If there's a frame available

s/This function is called/This thread is started/

Quoted 19 lines
drivers/media/platform/vimc/vimc-output.c new file +19 -0
+ * on the queue, it will get it and "consume" it. This means, marks the frame
+ * as done and make sure there're at least one frame to be on the list.
+ * It should stop when the streaming stops too
+ *
+ * Return: always 0
+ */
+static int vimc_out_consume_frame(void *arg)
+{
+	struct vimc_vid_device *vout = arg;
+	struct vimc_vid_buffer *vimc_buf;
+
+	set_freezable();
+
+	for (;;) {
+		try_to_freeze();
+		if (kthread_should_stop())
+			break;
+
+		/* TODO: support custom framerate */

drivers/media/platform/vimc/vimc-output.c line 68

Quoted 10 lines
drivers/media/platform/vimc/vimc-output.c new file +10 -0
+		set_current_state(TASK_UNINTERRUPTIBLE);
+		schedule_timeout(HZ / 60);
+
+		spin_lock(&vout->qlock);
+
+		/*
+		 * if there's only on frame on the buffer, keep it there
+		 * this is done in a way that the output always can send a
+		 * frame to the pipeline, even if the capture framerate is
+		 * higher than the output framarate

s/framarate/framerate/

Quoted 29 lines
drivers/media/platform/vimc/vimc-output.c new file +29 -0
+		 */
+		if (list_empty(&vout->buf_list) ||
+		    list_is_singular(&vout->buf_list)) {
+			spin_unlock(&vout->qlock);
+			continue;
+		}
+
+		vimc_buf = list_first_entry(&vout->buf_list,
+				    typeof(*vimc_buf), list);
+		list_del(&vimc_buf->list);
+		spin_unlock(&vout->qlock);
+
+		vimc_buf->vb2.vb2_buf.timestamp = ktime_get_ns();
+		vimc_buf->vb2.sequence = vout->sequence++;
+		vimc_buf->vb2.field = vout->format.field;
+		vb2_buffer_done(&vimc_buf->vb2.vb2_buf, VB2_BUF_STATE_DONE);
+	}
+
+	return 0;
+}
+
+/**
+ * vimc_out_start_streaming - callback used when userspace starts streaming
+ *
+ * @vq:	   queue of vb2 video buffers
+ * @count: number of already queued buffers
+ *
+ * Allocate the output frame and starts the consume thread
+ *

drivers/media/platform/vimc/vimc-output.c line 107

Quoted 10 lines
drivers/media/platform/vimc/vimc-output.c new file +10 -0
+ * Return: 0 on success or error code
+ */
+static int vimc_out_start_streaming(struct vb2_queue *vq, unsigned int count)
+{
+	struct vimc_vid_device *vout = vb2_get_drv_priv(vq);
+
+	vout->sequence = 0;
+
+	vout->kthread_fd = kthread_run(vimc_out_consume_frame, vout,
+					      "vimc-output consume thread");

Alignment

Quoted 49 lines
drivers/media/platform/vimc/vimc-output.c new file +49 -0
+
+	if (IS_ERR(vout->kthread_fd))
+		return PTR_ERR(vout->kthread_fd);
+
+	return 0;
+}
+
+/**
+ * vimc_out_stop_streaming - callback used when userspace stops streaming
+ *
+ * @vq:	   queue of vb2 video buffers
+ *
+ * Wait until vimc_stream stops consuming a frame (using a mutex) and free it.
+ * Stops the thread and return all unused buffers to userspace.
+ */
+static void vimc_out_stop_streaming(struct vb2_queue *vq)
+{
+	struct vimc_vid_device *vout = vb2_get_drv_priv(vq);
+
+	vout->sequence = 0;
+
+	kthread_stop(vout->kthread_fd);
+
+	vimc_vid_return_all_buffers(vout, VB2_BUF_STATE_ERROR);
+}
+
+static const struct vb2_ops vimc_out_qops = {
+	.start_streaming	= vimc_out_start_streaming,
+	.stop_streaming		= vimc_out_stop_streaming,
+	.buf_queue		= vimc_vid_buf_queue,
+	.queue_setup		= vimc_vid_queue_setup,
+	.buf_prepare		= vimc_vid_buffer_prepare,
+	.buf_out_validate	= vimc_vid_buffer_validate,
+	/*
+	 * Since q->lock is set we can use the standard
+	 * vb2_ops_wait_prepare/finish helper functions.
+	 */
+	.wait_prepare		= vb2_ops_wait_prepare,
+	.wait_finish		= vb2_ops_wait_finish,
+};
+
+static const struct media_entity_operations vimc_out_mops = {
+	.link_validate		= vimc_link_validate,
+};
+
+/**
+ * vimc_out_process_frame - process a frame on media pipeline
+ *
+ * @ved:   the vimc entity structure to process the frame

drivers/media/platform/vimc/vimc-output.c line 166

Quoted 10 lines
drivers/media/platform/vimc/vimc-output.c new file +10 -0
+ * @frame: pointer to the pipeline frame. Unused by output
+ *
+ * If there's a frame to be sent to the pipeline, copy the newest frame and
+ * send it through the pipeline.
+ * If there's no frame on vimc_output buffer list (this probably means that
+ * there's no streaming going on at the output) or the pixelformat of the stream
+ * mismatch the output format, send NULL.
+ */
+static void *vimc_out_process_frame(struct vimc_ent_device *ved,
+						 const void *frame)

Alignment

drivers/media/platform/vimc/vimc-output.c line 174

Quoted 8 lines
drivers/media/platform/vimc/vimc-output.c new file +8 -0
+{
+	struct vimc_vid_device *vout = container_of(ved, struct vimc_vid_device,
+						    ved);
+	struct vimc_vid_buffer *vimc_buf;
+	void *vbuf;
+
+	if (ved->stream->producer_pixfmt != vout->format.pixelformat)
+		return NULL;

fyi, after reverting "media: vimc: propagate pixel format in the stream" this check should be done in vimc_link_validate

Quoted 11 lines
drivers/media/platform/vimc/vimc-output.c new file +11 -0
+
+	spin_lock(&vout->qlock);
+	vimc_buf = list_first_entry_or_null(&vout->buf_list,
+					    typeof(*vimc_buf), list);
+	spin_unlock(&vout->qlock);
+
+	if (!vimc_buf)
+		return NULL;
+
+	vbuf = vb2_plane_vaddr(&vimc_buf->vb2.vb2_buf, 0);
+	memcpy(vout->frame, vbuf, vout->format.sizeimage);

drivers/media/platform/vimc/vimc-output.c line 195

Quoted 10 lines
drivers/media/platform/vimc/vimc-output.c new file +10 -0
+
+	return vout->frame;
+}
+
+/**
+ * vimc_out_s_stream - callback before/after the start/stop of pipeline stream
+ *
+ * @ved:	the vimc_ent_device compenent of vimc_vid_device
+ * @enable:	0 means that the streaming has stop, non-zero means the
+ *		will begin

s/has/has to/
I think some word is missing. Maybe just:
0 stop the stream, non-zero start the stream

Quoted 61 lines
drivers/media/platform/vimc/vimc-output.c new file +61 -0
+ *
+ */
+static int vimc_out_s_stream(struct vimc_ent_device *ved, int enable)
+{
+	struct vimc_vid_device *vout =
+				container_of(ved, struct vimc_vid_device, ved);
+
+	if (enable) {
+		vout->frame = vmalloc(vout->format.sizeimage);
+		if (!vout->frame)
+			return -ENOMEM;
+
+	} else {
+		vfree(vout->frame);
+		vout->frame = NULL;
+	}
+
+	return 0;
+}
+
+static int vimc_out_comp_bind(struct device *comp, struct device *master,
+		void *master_data)
+{
+	struct v4l2_device *v4l2_dev = master_data;
+	struct vimc_platform_data *pdata = comp->platform_data;
+	struct vimc_vid_device *vout;
+	struct video_device *vdev;
+	struct vb2_queue *q;
+	int ret;
+
+	/* Allocate the vimc_vid_device struct */
+	vout = kzalloc(sizeof(*vout), GFP_KERNEL);
+	if (!vout)
+		return -ENOMEM;
+
+	vout->stream.pipe_size = 0;
+	/* Allocate the pads */
+	vout->ved.pads =
+		vimc_pads_init(1, (const unsigned long[1]) {MEDIA_PAD_FL_SOURCE});
+	if (IS_ERR(vout->ved.pads)) {
+		ret = PTR_ERR(vout->ved.pads);
+		goto err_free_vout;
+	}
+
+	/* Initialize the media entity */
+	vout->vdev.entity.name = pdata->entity_name;
+	vout->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
+	ret = media_entity_pads_init(&vout->vdev.entity,
+				     1, vout->ved.pads);
+	if (ret)
+		goto err_clean_pads;
+
+	/* Initialize the lock */
+	mutex_init(&vout->lock);
+
+	/* Initialize the vb2 queue */
+	q = &vout->queue;
+	q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
+	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_USERPTR;
+	q->drv_priv = vout;
+	q->buf_struct_size = sizeof(struct vimc_vid_buffer);

drivers/media/platform/vimc/vimc-output.c line 266

Quoted 10 lines
drivers/media/platform/vimc/vimc-output.c new file +10 -0
+	q->ops = &vimc_out_qops;
+	q->mem_ops = &vb2_vmalloc_memops;
+	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	q->min_buffers_needed = 2;
+	q->lock = &vout->lock;
+
+	ret = vb2_queue_init(q);
+	if (ret) {
+		dev_err(comp, "%s: vb2 queue init failed (err=%d)\n",
+				pdata->entity_name, ret);

alignment

Quoted 242 lines
drivers/media/platform/vimc/vimc-output.c new file +96 -0
+		goto err_clean_m_ent;
+	}
+
+	/* Initialize buffer list and its lock */
+	INIT_LIST_HEAD(&vout->buf_list);
+	spin_lock_init(&vout->qlock);
+
+	/* Set default frame format */
+	vout->format = fmt_default;
+	v4l2_fill_pixfmt(&vout->format, vout->format.pixelformat,
+			 vout->format.width, vout->format.height);
+
+	/* Fill the vimc_ent_device struct */
+	vout->ved.ent = &vout->vdev.entity;
+	vout->ved.process_frame = vimc_out_process_frame;
+	vout->ved.s_stream = vimc_out_s_stream;
+	vout->ved.vdev_get_format = vimc_vid_get_format;
+	dev_set_drvdata(comp, &vout->ved);
+	vout->dev = comp;
+
+	/* Initialize the video_device struct */
+	vdev = &vout->vdev;
+	vdev->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
+	vdev->entity.ops = &vimc_out_mops;
+	vdev->release = vimc_vid_release;
+	vdev->fops = &vimc_vid_fops;
+	vdev->ioctl_ops = &vimc_out_ioctl_ops;
+	vdev->lock = &vout->lock;
+	vdev->queue = q;
+	vdev->v4l2_dev = v4l2_dev;
+	vdev->vfl_dir = VFL_DIR_TX;
+	strscpy(vdev->name, pdata->entity_name, sizeof(vdev->name));
+	video_set_drvdata(vdev, &vout->ved);
+
+	/* Register the video_device with the v4l2 and the media framework */
+	ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
+	if (ret) {
+		dev_err(comp, "%s: video register failed (err=%d)\n",
+				vout->vdev.name, ret);
+		goto err_release_queue;
+	}
+
+	return 0;
+
+err_release_queue:
+	vb2_queue_release(q);
+err_clean_m_ent:
+	media_entity_cleanup(&vout->vdev.entity);
+err_clean_pads:
+	vimc_pads_cleanup(vout->ved.pads);
+err_free_vout:
+	kfree(vout);
+
+	return ret;
+}
+
+static const struct component_ops vimc_out_comp_ops = {
+	.bind = vimc_out_comp_bind,
+	.unbind = vimc_vid_comp_unbind,
+};
+
+static int vimc_out_probe(struct platform_device *pdev)
+{
+	return component_add(&pdev->dev, &vimc_out_comp_ops);
+}
+
+static int vimc_out_remove(struct platform_device *pdev)
+{
+	component_del(&pdev->dev, &vimc_out_comp_ops);
+
+	return 0;
+}
+
+static const struct platform_device_id vimc_out_driver_ids[] = {
+	{
+		.name           = VIMC_OUT_DRV_NAME,
+	},
+	{ }
+};
+
+static struct platform_driver vimc_out_pdrv = {
+	.probe		= vimc_out_probe,
+	.remove		= vimc_out_remove,
+	.id_table	= vimc_out_driver_ids,
+	.driver		= {
+		.name	= VIMC_OUT_DRV_NAME,
+	},
+};
+
+module_platform_driver(vimc_out_pdrv);
+
+MODULE_DEVICE_TABLE(platform, vimc_out_driver_ids);
+
+MODULE_DESCRIPTION("Virtual Media Controller Driver (VIMC) Output");
+MODULE_AUTHOR("André Almeida <[email protected]>");
+MODULE_LICENSE("GPL");
drivers/media/platform/vimc/vimc-scaler.c +4 -6
index c7123a45c55b..2c3e486a29c0 100644
@@ -194,9 +194,10 @@ static const struct v4l2_subdev_pad_ops vimc_sca_pad_ops = {
 	.set_fmt		= vimc_sca_set_fmt,
 };
 
-static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
+static int vimc_sca_s_stream(struct vimc_ent_device *ved, int enable)
 {
-	struct vimc_sca_device *vsca = v4l2_get_subdevdata(sd);
+	struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
+						    ved);
 
 	if (enable) {
 		u32 pixelformat = vsca->ved.stream->producer_pixfmt;
@@ -239,13 +240,9 @@ static int vimc_sca_s_stream(struct v4l2_subdev *sd, int enable)
 	return 0;
 }
 
-static const struct v4l2_subdev_video_ops vimc_sca_video_ops = {
-	.s_stream = vimc_sca_s_stream,
-};
 
 static const struct v4l2_subdev_ops vimc_sca_ops = {
 	.pad = &vimc_sca_pad_ops,
-	.video = &vimc_sca_video_ops,
 };
 
 static void vimc_sca_fill_pix(u8 *const ptr,
@@ -381,6 +378,7 @@ static int vimc_sca_comp_bind(struct device *comp, struct device *master,
 	}
 
 	vsca->ved.process_frame = vimc_sca_process_frame;
+	vsca->ved.s_stream = vimc_sca_s_stream,
 	dev_set_drvdata(comp, &vsca->ved);
 	vsca->dev = comp;
 
drivers/media/platform/vimc/vimc-sensor.c +3 -7
index 51359472eef2..a1afb840493d 100644
@@ -177,10 +177,10 @@ static void *vimc_sen_process_frame(struct vimc_ent_device *ved,
 	return vsen->frame;
 }
 
-static int vimc_sen_s_stream(struct v4l2_subdev *sd, int enable)
+static int vimc_sen_s_stream(struct vimc_ent_device *ved, int enable)
 {
 	struct vimc_sen_device *vsen =
-				container_of(sd, struct vimc_sen_device, sd);
+				container_of(ved, struct vimc_sen_device, ved);
 
 	if (enable) {
 		u32 pixelformat = vsen->ved.stream->producer_pixfmt;
@@ -218,14 +218,9 @@ static const struct v4l2_subdev_core_ops vimc_sen_core_ops = {
 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
 };
 
-static const struct v4l2_subdev_video_ops vimc_sen_video_ops = {
-	.s_stream = vimc_sen_s_stream,
-};
-
 static const struct v4l2_subdev_ops vimc_sen_ops = {
 	.core = &vimc_sen_core_ops,
 	.pad = &vimc_sen_pad_ops,
-	.video = &vimc_sen_video_ops,
 };
 
 static int vimc_sen_s_ctrl(struct v4l2_ctrl *ctrl)
@@ -351,6 +346,7 @@ static int vimc_sen_comp_bind(struct device *comp, struct device *master,
 		goto err_free_hdl;
 
 	vsen->ved.process_frame = vimc_sen_process_frame;
+	vsen->ved.s_stream = vimc_sen_s_stream;
 	dev_set_drvdata(comp, &vsen->ved);
 	vsen->dev = comp;
 
drivers/media/platform/vimc/vimc-streamer.c +5 -17
index 3b3f36357a0e..fd330415ac38 100644
@@ -47,7 +47,6 @@ static struct media_entity *vimc_get_source_entity(struct media_entity *ent)
 static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
 {
 	struct vimc_ent_device *ved;
-	struct v4l2_subdev *sd;
 
 	while (stream->pipe_size) {
 		stream->pipe_size--;
@@ -55,11 +54,8 @@ static void vimc_streamer_pipeline_terminate(struct vimc_stream *stream)
 		ved->stream = NULL;
 		stream->ved_pipeline[stream->pipe_size] = NULL;
 
-		if (!is_media_entity_v4l2_subdev(ved->ent))
-			continue;
-
-		sd = media_entity_to_v4l2_subdev(ved->ent);
-		v4l2_subdev_call(sd, video, s_stream, 0);
+		if (ved->s_stream)
+			ved->s_stream(ved, 0);
 	}
 }
 
@@ -79,7 +75,6 @@ static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
 	struct media_entity *entity;
 	struct video_device *vdev;
 	struct v4l2_subdev *sd;
-	int ret = 0;
 
 	stream->pipe_size = 0;
 	while (stream->pipe_size < VIMC_STREAMER_PIPELINE_MAX_SIZE) {
@@ -90,16 +85,8 @@ static int vimc_streamer_pipeline_init(struct vimc_stream *stream,
 		stream->ved_pipeline[stream->pipe_size++] = ved;
 		ved->stream = stream;
 
-		if (is_media_entity_v4l2_subdev(ved->ent)) {
-			sd = media_entity_to_v4l2_subdev(ved->ent);
-			ret = v4l2_subdev_call(sd, video, s_stream, 1);
-			if (ret && ret != -ENOIOCTLCMD) {
-				pr_err("subdev_call error %s\n",
-				       ved->ent->name);
-				vimc_streamer_pipeline_terminate(stream);
-				return ret;
-			}
-		}
+		if (ved->s_stream)
+			ved->s_stream(ved, 1);
 
 		entity = vimc_get_source_entity(ved->ent);
 		/* Check if the end of the pipeline was reached*/
@@ -149,6 +136,7 @@ static int vimc_streamer_thread(void *data)
 			if (!frame || IS_ERR(frame))
 				break;
 		}
+
 		//wait for 60hz
 		set_current_state(TASK_UNINTERRUPTIBLE);
 		schedule_timeout(HZ / 60);
drivers/media/platform/vimc/vimc-video.c +0 -0
index f7ccb2e9d6c5..0187e699543a 100644
@@ -236,6 +236,15 @@ int vimc_vid_buffer_prepare(struct vb2_buffer *vb)
 }

drivers/media/platform/vimc/vimc-video.c int vimc_vid_buffer_prepare(struct vb2_buffer *vb) · line 246

Quoted 10 lines
drivers/media/platform/vimc/vimc-video.c +8 -0
 EXPORT_SYMBOL_GPL(vimc_vid_buffer_prepare);
 
+int vimc_vid_buffer_validate(struct vb2_buffer *vb)
+{
+	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+
+	vbuf->field = V4L2_FIELD_NONE;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(vimc_vid_buffer_validate);

Is this function only used by the output? Maybe just keep it private in the output.

Quoted 10 lines
drivers/media/platform/vimc/vimc-video.c +1 -0
+
 void vimc_vid_release(struct video_device *vdev)
 {
 	struct vimc_vid_device *vid =
drivers/media/platform/vimc/vimc-video.h +0 -0
index 22cb0e2dbdbb..67f13244bf92 100644

drivers/media/platform/vimc/vimc-video.h int vimc_vid_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, · line 123

Quoted 8 lines
drivers/media/platform/vimc/vimc-video.h +2 -0
@@ -116,6 +116,8 @@ int vimc_vid_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
 
 int vimc_vid_buffer_prepare(struct vb2_buffer *vb);
 
+int vimc_vid_buffer_validate(struct vb2_buffer *vb);
+
 int vimc_vid_probe(struct platform_device *pdev);
 
 int vimc_vid_remove(struct platform_device *pdev);

Thanks,
Helen

#5 lore raw

PATCH 4/7 media: vimc: Send null buffer through the pipeline

In reply to André Almeida

Send a NULL buffer through the video pipeline. If the Capture device gets a NULL buffer, it uses it default fallback frame. Make the capture device behave more like real devices when there's no frame to show.

---
 drivers/media/platform/vimc/vimc-capture.c  | 15 +++++++++++++++
 drivers/media/platform/vimc/vimc-debayer.c  |  3 +++
 drivers/media/platform/vimc/vimc-scaler.c   |  3 +++
 drivers/media/platform/vimc/vimc-streamer.c |  2 +-
 4 files changed, 22 insertions(+), 1 deletion(-)
drivers/media/platform/vimc/vimc-capture.c +15 -0
index e80fa1ee3dc1..4c65bf73530f 100644
@@ -37,6 +37,15 @@ static int vimc_cap_start_streaming(struct vb2_queue *vq, unsigned int count)
 
 	vcap->sequence = 0;
 
+	/* this is a fallback frame, it will be used if the pipeline
+	 * is sending NULL frames
+	 */
+	vcap->frame = vmalloc(vcap->format.sizeimage);
+	if (!vcap->frame) {
+		vimc_vid_return_all_buffers(vcap, VB2_BUF_STATE_QUEUED);
+		return -ENOMEM;
+	}
+
 	/* Start the media pipeline */
 	ret = media_pipeline_start(entity, &vcap->stream.pipe);
 	if (ret) {
@@ -65,6 +74,9 @@ void vimc_cap_stop_streaming(struct vb2_queue *vq)
 
 	vimc_streamer_s_stream(&vcap->stream, &vcap->ved, 0);
 
+	vfree(vcap->frame);
+	vcap->frame = NULL;
+
 	media_pipeline_stop(&vcap->vdev.entity);
 
 	vimc_vid_return_all_buffers(vcap, VB2_BUF_STATE_ERROR);
@@ -96,6 +108,9 @@ static void *vimc_cap_process_frame(struct vimc_ent_device *ved,
 	struct vimc_vid_buffer *vimc_buf;
 	void *vbuf;
 
+	if (!frame)
+		frame = &vcap->frame;
+
 	spin_lock(&vcap->qlock);
 
 	/* Get the first entry of the list */
drivers/media/platform/vimc/vimc-debayer.c +3 -0
index 9e0c7c2c3c72..d5f370f94573 100644
@@ -483,6 +483,9 @@ static void *vimc_deb_process_frame(struct vimc_ent_device *ved,
 	unsigned int rgb[3];
 	unsigned int i, j;
 
+	if (!sink_frame)
+		return NULL;
+
 	/* If the stream in this node is not active, just return */
 	if (!vdeb->src_frame)
 		return ERR_PTR(-EINVAL);
drivers/media/platform/vimc/vimc-scaler.c +3 -0
index 2c3e486a29c0..9edad3b14526 100644
@@ -320,6 +320,9 @@ static void *vimc_sca_process_frame(struct vimc_ent_device *ved,
 	struct vimc_sca_device *vsca = container_of(ved, struct vimc_sca_device,
 						    ved);
 
+	if (!sink_frame)
+		return NULL;
+
 	/* If the stream in this node is not active, just return */
 	if (!ved->stream)
 		return ERR_PTR(-EINVAL);
drivers/media/platform/vimc/vimc-streamer.c +1 -1
index fd330415ac38..f06c9308a41b 100644
@@ -133,7 +133,7 @@ static int vimc_streamer_thread(void *data)
 		for (i = stream->pipe_size - 1; i >= 0; i--) {
 			frame = stream->ved_pipeline[i]->process_frame(
 					stream->ved_pipeline[i], frame);
-			if (!frame || IS_ERR(frame))
+			if (IS_ERR(frame))
 				break;
 		}
 
Signature · 1 line
2.22.0
#6 lore raw

PATCH 5/7 media: vimc: core: Add output device on the pipeline

In reply to André Almeida

Add the output video device on the hardcoded pipeline. Change the link to it be enabled by default.

---
 drivers/media/platform/vimc/vimc-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/media/platform/vimc/vimc-core.c +3 -4
index 571c55aa0e16..ecdea1d631c5 100644
@@ -95,8 +95,7 @@ static const struct vimc_ent_config ent_config[] = {
 	},
 	{
 		.name = "RGB/YUV Input",
-		/* TODO: change this to vimc-input when it is implemented */
-		.drv = "vimc-sensor",
+		.drv = "vimc-output",
 	},
 	{
 		.name = "Scaler",
@@ -118,11 +117,11 @@ static const struct vimc_ent_link ent_links[] = {
 	/* Link: Sensor B (Pad 0)->(Pad 0) Raw Capture 1 */
 	VIMC_ENT_LINK(1, 0, 5, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
 	/* Link: Debayer A (Pad 1)->(Pad 0) Scaler */
-	VIMC_ENT_LINK(2, 1, 7, 0, MEDIA_LNK_FL_ENABLED),
+	VIMC_ENT_LINK(2, 1, 7, 0, 0),
 	/* Link: Debayer B (Pad 1)->(Pad 0) Scaler */
 	VIMC_ENT_LINK(3, 1, 7, 0, 0),
 	/* Link: RGB/YUV Input (Pad 0)->(Pad 0) Scaler */
-	VIMC_ENT_LINK(6, 0, 7, 0, 0),
+	VIMC_ENT_LINK(6, 0, 7, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
 	/* Link: Scaler (Pad 1)->(Pad 0) RGB/YUV Capture */
 	VIMC_ENT_LINK(7, 1, 8, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
 };
Signature · 1 line
2.22.0
#7 lore raw

Re: [PATCH 5/7] media: vimc: core: Add output device on the pipeline

Quoted 13 lines · André Almeida

Add the output video device on the hardcoded pipeline. Change the link to it be enabled by default.

---
 drivers/media/platform/vimc/vimc-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/media/platform/vimc/vimc-core.c +0 -0
index 571c55aa0e16..ecdea1d631c5 100644

drivers/media/platform/vimc/vimc-core.c static const struct vimc_ent_config ent_config[] = { · line 97

Quoted 3 lines
drivers/media/platform/vimc/vimc-core.c +0 -0
@@ -95,8 +95,7 @@ static const struct vimc_ent_config ent_config[] = {
 	},
 	{
 		.name = "RGB/YUV Input",

Could you also change the name here to Output? I think it makes more sense.

Helen

Quoted 21 lines
drivers/media/platform/vimc/vimc-core.c +3 -4
-		/* TODO: change this to vimc-input when it is implemented */
-		.drv = "vimc-sensor",
+		.drv = "vimc-output",
 	},
 	{
 		.name = "Scaler",
@@ -118,11 +117,11 @@ static const struct vimc_ent_link ent_links[] = {
 	/* Link: Sensor B (Pad 0)->(Pad 0) Raw Capture 1 */
 	VIMC_ENT_LINK(1, 0, 5, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
 	/* Link: Debayer A (Pad 1)->(Pad 0) Scaler */
-	VIMC_ENT_LINK(2, 1, 7, 0, MEDIA_LNK_FL_ENABLED),
+	VIMC_ENT_LINK(2, 1, 7, 0, 0),
 	/* Link: Debayer B (Pad 1)->(Pad 0) Scaler */
 	VIMC_ENT_LINK(3, 1, 7, 0, 0),
 	/* Link: RGB/YUV Input (Pad 0)->(Pad 0) Scaler */
-	VIMC_ENT_LINK(6, 0, 7, 0, 0),
+	VIMC_ENT_LINK(6, 0, 7, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
 	/* Link: Scaler (Pad 1)->(Pad 0) RGB/YUV Capture */
 	VIMC_ENT_LINK(7, 1, 8, 0, MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE),
 };
#8 lore raw

PATCH 6/7 media: vimc.dot: Update default topology diagram

In reply to André Almeida

Update the default topology diagram to reflect the current state of the driver.

---
 Documentation/media/v4l-drivers/vimc.dot | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/media/v4l-drivers/vimc.dot +2 -2
index 57863a13fa39..b40c151ff790 100644
@@ -9,13 +9,13 @@ digraph board {
 	n00000003:port0 -> n00000008:port0 [style=bold]
 	n00000003:port0 -> n0000000f [style=bold]
 	n00000005 [label="{{<port0> 0} | Debayer A\n/dev/v4l-subdev2 | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
-	n00000005:port1 -> n00000017:port0
+	n00000005:port1 -> n00000017:port0 [style=dashed]
 	n00000008 [label="{{<port0> 0} | Debayer B\n/dev/v4l-subdev3 | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
 	n00000008:port1 -> n00000017:port0 [style=dashed]
 	n0000000b [label="Raw Capture 0\n/dev/video0", shape=box, style=filled, fillcolor=yellow]
 	n0000000f [label="Raw Capture 1\n/dev/video1", shape=box, style=filled, fillcolor=yellow]
 	n00000013 [label="RGB/YUV Input\n/dev/video2", shape=box, style=filled, fillcolor=yellow]
-	n00000013 -> n00000017:port0 [style=dashed]
+	n00000013 -> n00000017:port0 [style=bold]
 	n00000017 [label="{{<port0> 0} | Scaler\n/dev/v4l-subdev4 | {<port1> 1}}", shape=Mrecord, style=filled, fillcolor=green]
 	n00000017:port1 -> n0000001a [style=bold]
 	n0000001a [label="RGB/YUV Capture\n/dev/video3", shape=box, style=filled, fillcolor=yellow]
Signature · 1 line
2.22.0
#9 lore raw

PATCH 7/7 media: vimc.rst: Add output device

In reply to André Almeida

Add information about the output device. Remove wrong information about what the capture exposes.

---
 Documentation/media/v4l-drivers/vimc.rst | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
Documentation/media/v4l-drivers/vimc.rst +10 -2
index 4628b12d417f..ccc04101ea51 100644
@@ -4,7 +4,8 @@ The Virtual Media Controller Driver (vimc)
 ==========================================
 
 The vimc driver emulates complex video hardware using the V4L2 API and the Media
-API. It has a capture device and three subdevices: sensor, debayer and scaler.
+API. It has a capture device, an output device and three subdevices: sensor,
+debayer and scaler.
 
 Topology
 --------
@@ -40,6 +41,7 @@ of commands fits for the default topology:
         v4l2-ctl -z platform:vimc -d "RGB/YUV Capture" -v width=1920,height=1440
         v4l2-ctl -z platform:vimc -d "Raw Capture 0" -v pixelformat=BA81
         v4l2-ctl -z platform:vimc -d "Raw Capture 1" -v pixelformat=BA81
+        v4l2-ctl -z platform:vimc -e "RGB/YUV Input" -v width=640,height=480
 
 Subdevices
 ----------
@@ -74,7 +76,13 @@ vimc-capture:
 	Exposes:
 
 	* 1 Pad sink
-	* 1 Pad source
+
+vimc-output:
+        Exposes node /dev/videoX to allow userspace to send frames to the
+        stream.
+        Exposes:
+
+        * 1 Source sink
 
 Module options
 ---------------
Signature · 1 line
2.22.0
#10 lore raw
In reply to André Almeida

Hi André,

Thanks for the patches.

Quoted 8 lines · André Almeida

Hello,

This patch adds a V4L2 output device on vimc, that comply with V4L2 API for video output. If there is an output device and a capture device at the same pipeline, one can get a video loopback pipeline feeding frames at the output and then seeing them at the capture. It's possible to insert vimc submodules at the pipeline to modify the image (e.g. a scaler).

Quoted 5 lines

If one starts a streaming at the capture, with the output off, the capture will display a noisy frame. If one starts a streaming at the output with the capture off, the output will just consume the buffers, without sending them to the pipeline. If both output and capture are streaming, the loopback will happen.

I understand why it is done like this in vivid, but I was wondering, if we have a pipeline like:
output -> capture
Shouldn't streaming from the capture just stalls if there is no frame available in the output (i.e. streaming in the output is off) ? But then I'm not sure what the framerate in the capture would mean.

Hans, what do you think?

Thanks,
Helen

Quoted 85 lines

The patches 1 and 2 provide some ground to create the output device. The patch 3 creates the device and modify how the vimc-streamer was dealing with the s_stream callback on other vimc modules, to make simpler implementing this callback at vimc-output. Patch 4 change the behavior of the pipeline in order to be closer to a real life hardware. Patches 5-7 updates the default pipeline and the documentation to include the new output device.

This is the result of v4l2-compliance after this patch series:

$ v4l2-compliance -m0 -s50
Grand Total for vimc device /dev/media0: 476, Succeeded: 476, Failed: 0,
Warnings: 0

A git tree up to date with media-master and with this changes can be found
at: https://gitlab.collabora.com/tonyk/linux/tree/vimc/output

In order to test it, one can follow these instructions:

1 - Configure the pipeline (requires v4l-utils):

$ media-ctl -d platform:vimc -V '"Sensor A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Sensor B":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer B":0[fmt:SBGGR8_1X8/640x480]'
$ v4l2-ctl -z platform:vimc -d "RGB/YUV Capture" -v width=1920,height=1440
$ v4l2-ctl -z platform:vimc -d "Raw Capture 0" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -d "Raw Capture 1" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -e "RGB/YUV Input" -v width=640,height=480

2 - Use a userspace application:
2.a gst-launch (requires gstreamer and gst-plugins-good):

Feed frames into the output and grab from the capture (rescaled for convenience):

$ gst-launch-1.0 videotestsrc pattern=ball ! \
	video/x-raw,width=640,height=480,format=RGB \
	! v4l2sink device=/dev/video2 v4l2src device=/dev/video3 ! \
	video/x-raw,width=1920,height=1440,format=RGB ! videoscale ! \
	video/x-raw,width=640,height=480 ! videoconvert ! ximagesink

2.b qv4l2 (requires v4l-utils):

Open the output device:

$ qv4l2 -d2

Open the capture device:

$ qv4l2 -d3

Start the streaming at both, at any order. You can change the frame content at "Test Pattern Generator" -> "Test Pattern" on the output.

Thanks,
André

André Almeida (7):
media: vimc: Create video module
media: vimc: video: Add write file operation
media: vimc: Create a V4L2 output device
media: vimc: Send null buffer through the pipeline
media: vimc: core: Add output device on the pipeline
media: vimc.dot: Update default topology diagram
media: vimc.rst: Add output device

 Documentation/media/v4l-drivers/vimc.dot    |   4 +-
 Documentation/media/v4l-drivers/vimc.rst    |  12 +-
 drivers/media/platform/vimc/Makefile        |   4 +-
 drivers/media/platform/vimc/vimc-capture.c  | 356 +++----------------
 drivers/media/platform/vimc/vimc-common.h   |   5 +-
 drivers/media/platform/vimc/vimc-core.c     |   7 +-
 drivers/media/platform/vimc/vimc-debayer.c  |  14 +-
 drivers/media/platform/vimc/vimc-output.c   | 362 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-scaler.c   |  13 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  10 +-
 drivers/media/platform/vimc/vimc-streamer.c |  24 +-
 drivers/media/platform/vimc/vimc-video.c    | 273 +++++++++++++++
 drivers/media/platform/vimc/vimc-video.h    | 130 +++++++
 13 files changed, 849 insertions(+), 365 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-output.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.h
#11 lore raw
Quoted 19 lines · Helen Koike

Hi André,

Thanks for the patches.

Quoted 8 lines · André Almeida

Hello,

This patch adds a V4L2 output device on vimc, that comply with V4L2 API for video output. If there is an output device and a capture device at the same pipeline, one can get a video loopback pipeline feeding frames at the output and then seeing them at the capture. It's possible to insert vimc submodules at the pipeline to modify the image (e.g. a scaler).

Quoted 5 lines

If one starts a streaming at the capture, with the output off, the capture will display a noisy frame. If one starts a streaming at the output with the capture off, the output will just consume the buffers, without sending them to the pipeline. If both output and capture are streaming, the loopback will happen.

Quoted 8 lines

I understand why it is done like this in vivid, but I was wondering, if we have a pipeline like:
output -> capture
Shouldn't streaming from the capture just stalls if there is no frame available in the output (i.e. streaming in the output is off) ? But then I'm not sure what the framerate in the capture would mean.

Hans, what do you think?

If you set up the pipeline like this:

Video Output -> Scaler -> Video Capture

Then this is a mem2mem device (except with two separate video devices) and framerate doesn't apply anymore. And video capture will just stall if there is no video output frame provided.

It's how e.g. omap3isp works.

Regards,

Hans

Quoted 89 lines

Thanks,
Helen

Quoted 85 lines

The patches 1 and 2 provide some ground to create the output device. The patch 3 creates the device and modify how the vimc-streamer was dealing with the s_stream callback on other vimc modules, to make simpler implementing this callback at vimc-output. Patch 4 change the behavior of the pipeline in order to be closer to a real life hardware. Patches 5-7 updates the default pipeline and the documentation to include the new output device.

This is the result of v4l2-compliance after this patch series:

$ v4l2-compliance -m0 -s50
Grand Total for vimc device /dev/media0: 476, Succeeded: 476, Failed: 0,
Warnings: 0

A git tree up to date with media-master and with this changes can be found
at: https://gitlab.collabora.com/tonyk/linux/tree/vimc/output

In order to test it, one can follow these instructions:

1 - Configure the pipeline (requires v4l-utils):

$ media-ctl -d platform:vimc -V '"Sensor A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Sensor B":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer B":0[fmt:SBGGR8_1X8/640x480]'
$ v4l2-ctl -z platform:vimc -d "RGB/YUV Capture" -v width=1920,height=1440
$ v4l2-ctl -z platform:vimc -d "Raw Capture 0" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -d "Raw Capture 1" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -e "RGB/YUV Input" -v width=640,height=480

2 - Use a userspace application:
2.a gst-launch (requires gstreamer and gst-plugins-good):

Feed frames into the output and grab from the capture (rescaled for convenience):

$ gst-launch-1.0 videotestsrc pattern=ball ! \
	video/x-raw,width=640,height=480,format=RGB \
	! v4l2sink device=/dev/video2 v4l2src device=/dev/video3 ! \
	video/x-raw,width=1920,height=1440,format=RGB ! videoscale ! \
	video/x-raw,width=640,height=480 ! videoconvert ! ximagesink

2.b qv4l2 (requires v4l-utils):

Open the output device:

$ qv4l2 -d2

Open the capture device:

$ qv4l2 -d3

Start the streaming at both, at any order. You can change the frame content at "Test Pattern Generator" -> "Test Pattern" on the output.

Thanks,
André

André Almeida (7):
media: vimc: Create video module
media: vimc: video: Add write file operation
media: vimc: Create a V4L2 output device
media: vimc: Send null buffer through the pipeline
media: vimc: core: Add output device on the pipeline
media: vimc.dot: Update default topology diagram
media: vimc.rst: Add output device

 Documentation/media/v4l-drivers/vimc.dot    |   4 +-
 Documentation/media/v4l-drivers/vimc.rst    |  12 +-
 drivers/media/platform/vimc/Makefile        |   4 +-
 drivers/media/platform/vimc/vimc-capture.c  | 356 +++----------------
 drivers/media/platform/vimc/vimc-common.h   |   5 +-
 drivers/media/platform/vimc/vimc-core.c     |   7 +-
 drivers/media/platform/vimc/vimc-debayer.c  |  14 +-
 drivers/media/platform/vimc/vimc-output.c   | 362 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-scaler.c   |  13 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  10 +-
 drivers/media/platform/vimc/vimc-streamer.c |  24 +-
 drivers/media/platform/vimc/vimc-video.c    | 273 +++++++++++++++
 drivers/media/platform/vimc/vimc-video.h    | 130 +++++++
 13 files changed, 849 insertions(+), 365 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-output.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.h
#12 lore raw

Hello,

Quoted 26 lines · Hans Verkuil
Quoted 25 lines · Helen Koike

Hi André,

Thanks for the patches.

Quoted 8 lines · André Almeida

Hello,

This patch adds a V4L2 output device on vimc, that comply with V4L2 API for video output. If there is an output device and a capture device at the same pipeline, one can get a video loopback pipeline feeding frames at the output and then seeing them at the capture. It's possible to insert vimc submodules at the pipeline to modify the image (e.g. a scaler).

Quoted 5 lines

If one starts a streaming at the capture, with the output off, the capture will display a noisy frame. If one starts a streaming at the output with the capture off, the output will just consume the buffers, without sending them to the pipeline. If both output and capture are streaming, the loopback will happen.

I understand why it is done like this in vivid, but I was wondering, if we have a pipeline like:
output -> capture
Shouldn't streaming from the capture just stalls if there is no frame available in the output (i.e. streaming in the output is off) ? But then I'm not sure what the framerate in the capture would mean.

Quoted 4 lines
Quoted 1 line

Hans, what do you think?

If you set up the pipeline like this:

Video Output -> Scaler -> Video Capture

If the capture will stall if there's no frame from the video output, how can I add support for this kind of pipeline at test-media? It would be required to send frames to the output device while running v4l2-compliance at the capture device to make testing possible.

Thanks,
    André

Quoted 97 lines

Then this is a mem2mem device (except with two separate video devices) and framerate doesn't apply anymore. And video capture will just stall if there is no video output frame provided.

It's how e.g. omap3isp works.

Regards,

Hans

Quoted 87 lines

Thanks,
Helen

Quoted 84 lines

The patches 1 and 2 provide some ground to create the output device. The patch 3 creates the device and modify how the vimc-streamer was dealing with the s_stream callback on other vimc modules, to make simpler implementing this callback at vimc-output. Patch 4 change the behavior of the pipeline in order to be closer to a real life hardware. Patches 5-7 updates the default pipeline and the documentation to include the new output device.

This is the result of v4l2-compliance after this patch series:

$ v4l2-compliance -m0 -s50
Grand Total for vimc device /dev/media0: 476, Succeeded: 476, Failed: 0,
Warnings: 0

A git tree up to date with media-master and with this changes can be found
at: https://gitlab.collabora.com/tonyk/linux/tree/vimc/output

In order to test it, one can follow these instructions:

1 - Configure the pipeline (requires v4l-utils):

$ media-ctl -d platform:vimc -V '"Sensor A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Sensor B":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer B":0[fmt:SBGGR8_1X8/640x480]'
$ v4l2-ctl -z platform:vimc -d "RGB/YUV Capture" -v width=1920,height=1440
$ v4l2-ctl -z platform:vimc -d "Raw Capture 0" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -d "Raw Capture 1" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -e "RGB/YUV Input" -v width=640,height=480

2 - Use a userspace application:
2.a gst-launch (requires gstreamer and gst-plugins-good):

Feed frames into the output and grab from the capture (rescaled for convenience):

$ gst-launch-1.0 videotestsrc pattern=ball ! \
	video/x-raw,width=640,height=480,format=RGB \
	! v4l2sink device=/dev/video2 v4l2src device=/dev/video3 ! \
	video/x-raw,width=1920,height=1440,format=RGB ! videoscale ! \
	video/x-raw,width=640,height=480 ! videoconvert ! ximagesink

2.b qv4l2 (requires v4l-utils):

Open the output device:

$ qv4l2 -d2

Open the capture device:

$ qv4l2 -d3

Start the streaming at both, at any order. You can change the frame content at "Test Pattern Generator" -> "Test Pattern" on the output.

Thanks,
André

André Almeida (7):
media: vimc: Create video module
media: vimc: video: Add write file operation
media: vimc: Create a V4L2 output device
media: vimc: Send null buffer through the pipeline
media: vimc: core: Add output device on the pipeline
media: vimc.dot: Update default topology diagram
media: vimc.rst: Add output device

 Documentation/media/v4l-drivers/vimc.dot    |   4 +-
 Documentation/media/v4l-drivers/vimc.rst    |  12 +-
 drivers/media/platform/vimc/Makefile        |   4 +-
 drivers/media/platform/vimc/vimc-capture.c  | 356 +++----------------
 drivers/media/platform/vimc/vimc-common.h   |   5 +-
 drivers/media/platform/vimc/vimc-core.c     |   7 +-
 drivers/media/platform/vimc/vimc-debayer.c  |  14 +-
 drivers/media/platform/vimc/vimc-output.c   | 362 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-scaler.c   |  13 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  10 +-
 drivers/media/platform/vimc/vimc-streamer.c |  24 +-
 drivers/media/platform/vimc/vimc-video.c    | 273 +++++++++++++++
 drivers/media/platform/vimc/vimc-video.h    | 130 +++++++
 13 files changed, 849 insertions(+), 365 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-output.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.h
#13 lore raw
Quoted 30 lines · André Almeida

Hello,

Quoted 27 lines · Hans Verkuil
Quoted 26 lines · Helen Koike

Hi André,

Thanks for the patches.

Quoted 8 lines · André Almeida

Hello,

This patch adds a V4L2 output device on vimc, that comply with V4L2 API for video output. If there is an output device and a capture device at the same pipeline, one can get a video loopback pipeline feeding frames at the output and then seeing them at the capture. It's possible to insert vimc submodules at the pipeline to modify the image (e.g. a scaler).

Quoted 5 lines

If one starts a streaming at the capture, with the output off, the capture will display a noisy frame. If one starts a streaming at the output with the capture off, the output will just consume the buffers, without sending them to the pipeline. If both output and capture are streaming, the loopback will happen.

I understand why it is done like this in vivid, but I was wondering, if we have a pipeline like:
output -> capture
Shouldn't streaming from the capture just stalls if there is no frame available in the output (i.e. streaming in the output is off) ? But then I'm not sure what the framerate in the capture would mean.

Hans, what do you think?

Quoted 8 lines
Quoted 3 lines

If you set up the pipeline like this:

Video Output -> Scaler -> Video Capture

If the capture will stall if there's no frame from the video output, how can I add support for this kind of pipeline at test-media? It would be required to send frames to the output device while running v4l2-compliance at the capture device to make testing possible.

The compliance test doesn't support such devices at the moment.

I think a new option (or options) are needed to tell the compliance test that the capture and output video devices together constitute an m2m device.

Regards,

Hans

Quoted 101 lines

Thanks,
    André

Quoted 97 lines

Then this is a mem2mem device (except with two separate video devices) and framerate doesn't apply anymore. And video capture will just stall if there is no video output frame provided.

It's how e.g. omap3isp works.

Regards,

Hans

Quoted 87 lines

Thanks,
Helen

Quoted 84 lines

The patches 1 and 2 provide some ground to create the output device. The patch 3 creates the device and modify how the vimc-streamer was dealing with the s_stream callback on other vimc modules, to make simpler implementing this callback at vimc-output. Patch 4 change the behavior of the pipeline in order to be closer to a real life hardware. Patches 5-7 updates the default pipeline and the documentation to include the new output device.

This is the result of v4l2-compliance after this patch series:

$ v4l2-compliance -m0 -s50
Grand Total for vimc device /dev/media0: 476, Succeeded: 476, Failed: 0,
Warnings: 0

A git tree up to date with media-master and with this changes can be found
at: https://gitlab.collabora.com/tonyk/linux/tree/vimc/output

In order to test it, one can follow these instructions:

1 - Configure the pipeline (requires v4l-utils):

$ media-ctl -d platform:vimc -V '"Sensor A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Sensor B":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer B":0[fmt:SBGGR8_1X8/640x480]'
$ v4l2-ctl -z platform:vimc -d "RGB/YUV Capture" -v width=1920,height=1440
$ v4l2-ctl -z platform:vimc -d "Raw Capture 0" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -d "Raw Capture 1" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -e "RGB/YUV Input" -v width=640,height=480

2 - Use a userspace application:
2.a gst-launch (requires gstreamer and gst-plugins-good):

Feed frames into the output and grab from the capture (rescaled for convenience):

$ gst-launch-1.0 videotestsrc pattern=ball ! \
	video/x-raw,width=640,height=480,format=RGB \
	! v4l2sink device=/dev/video2 v4l2src device=/dev/video3 ! \
	video/x-raw,width=1920,height=1440,format=RGB ! videoscale ! \
	video/x-raw,width=640,height=480 ! videoconvert ! ximagesink

2.b qv4l2 (requires v4l-utils):

Open the output device:

$ qv4l2 -d2

Open the capture device:

$ qv4l2 -d3

Start the streaming at both, at any order. You can change the frame content at "Test Pattern Generator" -> "Test Pattern" on the output.

Thanks,
André

André Almeida (7):
media: vimc: Create video module
media: vimc: video: Add write file operation
media: vimc: Create a V4L2 output device
media: vimc: Send null buffer through the pipeline
media: vimc: core: Add output device on the pipeline
media: vimc.dot: Update default topology diagram
media: vimc.rst: Add output device

 Documentation/media/v4l-drivers/vimc.dot    |   4 +-
 Documentation/media/v4l-drivers/vimc.rst    |  12 +-
 drivers/media/platform/vimc/Makefile        |   4 +-
 drivers/media/platform/vimc/vimc-capture.c  | 356 +++----------------
 drivers/media/platform/vimc/vimc-common.h   |   5 +-
 drivers/media/platform/vimc/vimc-core.c     |   7 +-
 drivers/media/platform/vimc/vimc-debayer.c  |  14 +-
 drivers/media/platform/vimc/vimc-output.c   | 362 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-scaler.c   |  13 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  10 +-
 drivers/media/platform/vimc/vimc-streamer.c |  24 +-
 drivers/media/platform/vimc/vimc-video.c    | 273 +++++++++++++++
 drivers/media/platform/vimc/vimc-video.h    | 130 +++++++
 13 files changed, 849 insertions(+), 365 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-output.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.h
#14 lore raw
Quoted 33 lines · Hans Verkuil
Quoted 32 lines · André Almeida

Hello,

Quoted 29 lines · Hans Verkuil
Quoted 18 lines · Helen Koike

Hi André,

Thanks for the patches.

Quoted 8 lines · André Almeida

Hello,

This patch adds a V4L2 output device on vimc, that comply with V4L2 API for video output. If there is an output device and a capture device at the same pipeline, one can get a video loopback pipeline feeding frames at the output and then seeing them at the capture. It's possible to insert vimc submodules at the pipeline to modify the image (e.g. a scaler).

Quoted 5 lines

If one starts a streaming at the capture, with the output off, the capture will display a noisy frame. If one starts a streaming at the output with the capture off, the output will just consume the buffers, without sending them to the pipeline. If both output and capture are streaming, the loopback will happen.

Quoted 8 lines

I understand why it is done like this in vivid, but I was wondering, if we have a pipeline like:
output -> capture
Shouldn't streaming from the capture just stalls if there is no frame available in the output (i.e. streaming in the output is off) ? But then I'm not sure what the framerate in the capture would mean.

Hans, what do you think?

If you set up the pipeline like this:

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Video Output -> Scaler -> Video Capture

If the capture will stall if there's no frame from the video output, how can I add support for this kind of pipeline at test-media? It would be required to send frames to the output device while running v4l2-compliance at the capture device to make testing possible.

The compliance test doesn't support such devices at the moment.

The implementation of the expected behavior can be found here:
https://gitlab.collabora.com/tonyk/linux/tree/vimc/output-v2

Quoted 3 lines

I think a new option (or options) are needed to tell the compliance test that the capture and output video devices together constitute an m2m device.

I've reading the v4l-utils code base and I had a look at both m2m tests and capture/output tests, but I'm not sure how to implement this new option. How do you think it should be implemented? Should it resemble how v4l2-compliance tests vim2m? Something like this:

v4l2-compliance -m platform:vim2m -z platform:vivid-002 -e
vivid-002-vid-cap -s10 -P -a

Thanks,
André

Quoted 107 lines

Regards,

Hans

Quoted 101 lines

Thanks,
    André

Quoted 97 lines

Then this is a mem2mem device (except with two separate video devices) and framerate doesn't apply anymore. And video capture will just stall if there is no video output frame provided.

It's how e.g. omap3isp works.

Regards,

Hans

Quoted 87 lines

Thanks,
Helen

Quoted 84 lines

The patches 1 and 2 provide some ground to create the output device. The patch 3 creates the device and modify how the vimc-streamer was dealing with the s_stream callback on other vimc modules, to make simpler implementing this callback at vimc-output. Patch 4 change the behavior of the pipeline in order to be closer to a real life hardware. Patches 5-7 updates the default pipeline and the documentation to include the new output device.

This is the result of v4l2-compliance after this patch series:

$ v4l2-compliance -m0 -s50
Grand Total for vimc device /dev/media0: 476, Succeeded: 476, Failed: 0,
Warnings: 0

A git tree up to date with media-master and with this changes can be found
at: https://gitlab.collabora.com/tonyk/linux/tree/vimc/output

In order to test it, one can follow these instructions:

1 - Configure the pipeline (requires v4l-utils):

$ media-ctl -d platform:vimc -V '"Sensor A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer A":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Sensor B":0[fmt:SBGGR8_1X8/640x480]'
$ media-ctl -d platform:vimc -V '"Debayer B":0[fmt:SBGGR8_1X8/640x480]'
$ v4l2-ctl -z platform:vimc -d "RGB/YUV Capture" -v width=1920,height=1440
$ v4l2-ctl -z platform:vimc -d "Raw Capture 0" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -d "Raw Capture 1" -v pixelformat=BA81
$ v4l2-ctl -z platform:vimc -e "RGB/YUV Input" -v width=640,height=480

2 - Use a userspace application:
2.a gst-launch (requires gstreamer and gst-plugins-good):

Feed frames into the output and grab from the capture (rescaled for convenience):

$ gst-launch-1.0 videotestsrc pattern=ball ! \
	video/x-raw,width=640,height=480,format=RGB \
	! v4l2sink device=/dev/video2 v4l2src device=/dev/video3 ! \
	video/x-raw,width=1920,height=1440,format=RGB ! videoscale ! \
	video/x-raw,width=640,height=480 ! videoconvert ! ximagesink

2.b qv4l2 (requires v4l-utils):

Open the output device:

$ qv4l2 -d2

Open the capture device:

$ qv4l2 -d3

Start the streaming at both, at any order. You can change the frame content at "Test Pattern Generator" -> "Test Pattern" on the output.

Thanks,
André

André Almeida (7):
media: vimc: Create video module
media: vimc: video: Add write file operation
media: vimc: Create a V4L2 output device
media: vimc: Send null buffer through the pipeline
media: vimc: core: Add output device on the pipeline
media: vimc.dot: Update default topology diagram
media: vimc.rst: Add output device

 Documentation/media/v4l-drivers/vimc.dot    |   4 +-
 Documentation/media/v4l-drivers/vimc.rst    |  12 +-
 drivers/media/platform/vimc/Makefile        |   4 +-
 drivers/media/platform/vimc/vimc-capture.c  | 356 +++----------------
 drivers/media/platform/vimc/vimc-common.h   |   5 +-
 drivers/media/platform/vimc/vimc-core.c     |   7 +-
 drivers/media/platform/vimc/vimc-debayer.c  |  14 +-
 drivers/media/platform/vimc/vimc-output.c   | 362 ++++++++++++++++++++
 drivers/media/platform/vimc/vimc-scaler.c   |  13 +-
 drivers/media/platform/vimc/vimc-sensor.c   |  10 +-
 drivers/media/platform/vimc/vimc-streamer.c |  24 +-
 drivers/media/platform/vimc/vimc-video.c    | 273 +++++++++++++++
 drivers/media/platform/vimc/vimc-video.h    | 130 +++++++
 13 files changed, 849 insertions(+), 365 deletions(-)
 create mode 100644 drivers/media/platform/vimc/vimc-output.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.c
 create mode 100644 drivers/media/platform/vimc/vimc-video.h