diff mbox series

[v1] media: vivid: Extend FPS rates offered by simulated webcam

Message ID 20230414072419.1397808-1-mstaudt@chromium.org (mailing list archive)
State New, archived
Headers show
Series [v1] media: vivid: Extend FPS rates offered by simulated webcam | expand

Commit Message

Max Staudt April 14, 2023, 7:24 a.m. UTC
This adds an option for higher frame rates from a simulated webcam.

Currently, vivid emulates (amongst other things) a webcam with somewhat
limited bandwidth - higher resolutions deliver fewer frames per second.

$ yavta --enum-formats -c /dev/video0
Device /dev/video0 opened.
Device `vivid' on `platform:vivid-000' (driver 'vivid') supports video, capture, without mplanes.
- Available formats:
	Format 0: YUYV (56595559)
	Type: Video capture (1)
	Name: YUYV 4:2:2
	Frame size: 320x180 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15, 1/25, 1/30, 1/40, 1/50, 1/60)
	Frame size: 640x360 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15, 1/25, 1/30, 1/40)
	Frame size: 640x480 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15, 1/25)
	Frame size: 1280x720 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25)
	Frame size: 1920x1080 (1/1, 1/2, 1/4, 1/5)
	Frame size: 3840x2160 (1/1, 1/2)

In some test cases, it is useful to allow for higher frame rates, as
configurations such as 720p@30 FPS have become commonplace now.

This patch allows:
   0- 719p - 120fps
 720-1079p - 60fps
1080-2159p - 30fps
     2160p - 15fps

$ yavta --enum-formats -c /dev/video0
Device /dev/video0 opened.
Device `vivid' on `platform:vivid-000' (driver 'vivid') supports video, capture, without mplanes.
- Available formats:
	Format 0: YUYV (56595559)
	Type: Video capture (1)
	Name: YUYV 4:2:2
	Frame size: 320x180 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15, 1/25, 1/30, 1/40, 1/50, 1/60, 1/120)
	Frame size: 640x360 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15, 1/25, 1/30, 1/40, 1/50, 1/60, 1/120)
	Frame size: 640x480 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15, 1/25, 1/30, 1/40, 1/50, 1/60, 1/120)
	Frame size: 1280x720 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15, 1/25, 1/30, 1/40, 1/50, 1/60)
	Frame size: 1920x1080 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15, 1/25, 1/30)
	Frame size: 3840x2160 (1/1, 1/2, 1/4, 1/5, 1/10, 2/25, 1/15)

Passes: v4l2-compliance 1.25.0-5039 from v4l-utils git ccc08732823f

Signed-off-by: Max Staudt <mstaudt@chromium.org>
---
 .../media/test-drivers/vivid/vivid-vid-cap.c  | 49 +++++++++++++------
 1 file changed, 34 insertions(+), 15 deletions(-)

Comments

Max Staudt April 21, 2023, 7:07 a.m. UTC | #1
Hi Hans,

Is this patch close to what you had in mind when you suggested updating 
the simulated webcam parameters in vivid?



Thanks,

Max
Hans Verkuil April 21, 2023, 7:17 a.m. UTC | #2
On 21/04/2023 09:07, Max Staudt wrote:
> Hi Hans,
> 
> Is this patch close to what you had in mind when you suggested updating the simulated webcam parameters in vivid?

Yes, that makes sense.

Regards,

	Hans

> 
> 
> 
> Thanks,
> 
> Max
>
Ricardo Ribalda April 22, 2023, 5:09 a.m. UTC | #3
Hi Max

Thanks for the patch!

> Signed-off-by: Max Staudt <mstaudt@chromium.org>

For what is worth:
Reviewed-by: Ricardo Ribalda <ribalda@chromium.org>


nit: maybe update the doc at:
https://www.kernel.org/doc/html/latest/admin-guide/media/vivid.html#webcam-input
although the current description is generic enough for our usecase.

Regards!
Max Staudt May 9, 2023, 5:09 a.m. UTC | #4
On 4/21/23 16:17, Hans Verkuil wrote:
> On 21/04/2023 09:07, Max Staudt wrote:
>> Is this patch close to what you had in mind when you suggested updating the simulated webcam parameters in vivid?
> 
> Yes, that makes sense.

Hi Hans,

Quick ping on this patch.

Are you still interested in upstreaming an updated FPS table?



Thanks,

Max
Hans Verkuil May 9, 2023, 6:14 a.m. UTC | #5
On 09/05/2023 07:09, Max Staudt wrote:
> On 4/21/23 16:17, Hans Verkuil wrote:
>> On 21/04/2023 09:07, Max Staudt wrote:
>>> Is this patch close to what you had in mind when you suggested updating the simulated webcam parameters in vivid?
>>
>> Yes, that makes sense.
> 
> Hi Hans,
> 
> Quick ping on this patch.
> 
> Are you still interested in upstreaming an updated FPS table?

Yes. I've been very busy, so it will take some time before I pick it up, but it
should go in for the 6.5 kernel.

Regards,

	Hans

> 
> 
> 
> Thanks,
> 
> Max
>
Max Staudt May 9, 2023, 6:16 a.m. UTC | #6
On 5/9/23 15:14, Hans Verkuil wrote:
> On 09/05/2023 07:09, Max Staudt wrote:
>> Are you still interested in upstreaming an updated FPS table?
> 
> Yes. I've been very busy, so it will take some time before I pick it up, but it
> should go in for the 6.5 kernel.

Thanks for the quick reply, and looking forward :)


Max
diff mbox series

Patch

diff --git a/drivers/media/test-drivers/vivid/vivid-vid-cap.c b/drivers/media/test-drivers/vivid/vivid-vid-cap.c
index c0999581c599..27214f5f0811 100644
--- a/drivers/media/test-drivers/vivid/vivid-vid-cap.c
+++ b/drivers/media/test-drivers/vivid/vivid-vid-cap.c
@@ -45,13 +45,8 @@  static const struct vivid_fmt formats_ovl[] = {
 	},
 };
 
-/* The number of discrete webcam framesizes */
-#define VIVID_WEBCAM_SIZES 6
-/* The number of discrete webcam frameintervals */
-#define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
-
 /* Sizes must be in increasing order */
-static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
+static const struct v4l2_frmsize_discrete webcam_sizes[] = {
 	{  320, 180 },
 	{  640, 360 },
 	{  640, 480 },
@@ -64,21 +59,43 @@  static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
  * Intervals must be in increasing order and there must be twice as many
  * elements in this array as there are in webcam_sizes.
  */
-static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
+static const struct v4l2_fract webcam_intervals[] = {
 	{  1, 1 },
 	{  1, 2 },
 	{  1, 4 },
 	{  1, 5 },
 	{  1, 10 },
 	{  2, 25 },
-	{  1, 15 },
+	{  1, 15 }, /* 7 - maximum for 2160p */
 	{  1, 25 },
-	{  1, 30 },
+	{  1, 30 }, /* 9 - maximum for 1080p */
 	{  1, 40 },
 	{  1, 50 },
-	{  1, 60 },
+	{  1, 60 }, /* 12 - maximum for 720p */
+	{  1, 120 },
 };
 
+/* Limit maximum FPS rates for high resolutions */
+#define IVAL_COUNT_720P 12 /* 720p and up is limited to 60 fps */
+#define IVAL_COUNT_1080P 9 /* 1080p and up is limited to 30 fps */
+#define IVAL_COUNT_2160P 7 /* 2160p and up is limited to 15 fps */
+
+static inline unsigned webcam_ival_count(const struct vivid_dev *dev,
+					 unsigned frmsize_idx)
+{
+	if (webcam_sizes[frmsize_idx].height >= 2160)
+		return IVAL_COUNT_2160P;
+
+	if (webcam_sizes[frmsize_idx].height >= 1080)
+		return IVAL_COUNT_1080P;
+
+	if (webcam_sizes[frmsize_idx].height >= 720)
+		return IVAL_COUNT_720P;
+
+	/* For low resolutions, allow all FPS rates */
+	return ARRAY_SIZE(webcam_intervals);
+}
+
 static int vid_cap_queue_setup(struct vb2_queue *vq,
 		       unsigned *nbuffers, unsigned *nplanes,
 		       unsigned sizes[], struct device *alloc_devs[])
@@ -592,7 +609,7 @@  int vivid_try_fmt_vid_cap(struct file *file, void *priv,
 	if (vivid_is_webcam(dev)) {
 		const struct v4l2_frmsize_discrete *sz =
 			v4l2_find_nearest_size(webcam_sizes,
-					       VIVID_WEBCAM_SIZES, width,
+					       ARRAY_SIZE(webcam_sizes), width,
 					       height, mp->width, mp->height);
 
 		w = sz->width;
@@ -773,14 +790,16 @@  int vivid_s_fmt_vid_cap(struct file *file, void *priv,
 			compose->height /= factor;
 		}
 	} else if (vivid_is_webcam(dev)) {
+		unsigned ival_sz = webcam_ival_count(dev, dev->webcam_size_idx);
+
 		/* Guaranteed to be a match */
 		for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
 			if (webcam_sizes[i].width == mp->width &&
 					webcam_sizes[i].height == mp->height)
 				break;
 		dev->webcam_size_idx = i;
-		if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
-			dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
+		if (dev->webcam_ival_idx >= ival_sz)
+			dev->webcam_ival_idx = ival_sz - 1;
 		vivid_update_format_cap(dev, false);
 	} else {
 		struct v4l2_rect r = { 0, 0, mp->width, mp->height };
@@ -1908,7 +1927,7 @@  int vidioc_enum_frameintervals(struct file *file, void *priv,
 			break;
 	if (i == ARRAY_SIZE(webcam_sizes))
 		return -EINVAL;
-	if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
+	if (fival->index >= webcam_ival_count(dev, i))
 		return -EINVAL;
 	fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
 	fival->discrete = webcam_intervals[fival->index];
@@ -1935,7 +1954,7 @@  int vivid_vid_cap_s_parm(struct file *file, void *priv,
 			  struct v4l2_streamparm *parm)
 {
 	struct vivid_dev *dev = video_drvdata(file);
-	unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
+	unsigned ival_sz = webcam_ival_count(dev, dev->webcam_size_idx);
 	struct v4l2_fract tpf;
 	unsigned i;