All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] Move finding the best match for size to V4L2 common
@ 2018-02-08 12:44 Sakari Ailus
  2018-02-08 12:44 ` [PATCH 1/5] v4l: common: Add a function to obtain best size from a list Sakari Ailus
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Sakari Ailus @ 2018-02-08 12:44 UTC (permalink / raw)
  To: linux-media; +Cc: yong.zhi, Yang, Hyungwoo, Rapolu, Chiranjeevi, andy.yeh

Hi folks,

This set should make it a bit easier to support finding the right size in
sensor drivers. Two sensor drivers and vivid are converted as an example.

I've tested the vivid change only but the patches are effectively the same
and trivial.

Sakari Ailus (5):
  v4l: common: Add a function to obtain best size from a list
  vivid: Use v4l2_find_nearest_size
  v4l: common: Remove v4l2_find_nearest_format
  ov13858: Use v4l2_find_nearest_size
  ov5670: Use v4l2_find_nearest_size

 drivers/media/i2c/ov13858.c                  | 37 +++-------------------------
 drivers/media/i2c/ov5670.c                   | 34 +++----------------------
 drivers/media/platform/vivid/vivid-vid-cap.c |  6 ++---
 drivers/media/v4l2-core/v4l2-common.c        | 34 ++++++++++++++-----------
 include/media/v4l2-common.h                  | 34 ++++++++++++++++++-------
 5 files changed, 53 insertions(+), 92 deletions(-)

-- 
2.7.4

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

* [PATCH 1/5] v4l: common: Add a function to obtain best size from a list
  2018-02-08 12:44 [PATCH 0/5] Move finding the best match for size to V4L2 common Sakari Ailus
@ 2018-02-08 12:44 ` Sakari Ailus
  2018-02-08 12:44 ` [PATCH 2/5] vivid: Use v4l2_find_nearest_size Sakari Ailus
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sakari Ailus @ 2018-02-08 12:44 UTC (permalink / raw)
  To: linux-media; +Cc: yong.zhi, Yang, Hyungwoo, Rapolu, Chiranjeevi, andy.yeh

Add a function (as well as a helper macro) to obtain the best size in a
list of device specific sizes. This helps writing drivers as well as
aligns interface behaviour across drivers.

The struct in which this information is contained in is typically specific
to the driver, therefore the existing function v4l2_find_nearest_format()
does not address the need.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/v4l2-core/v4l2-common.c | 30 ++++++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
index 8650ad9..c7a48f2 100644
--- a/drivers/media/v4l2-core/v4l2-common.c
+++ b/drivers/media/v4l2-core/v4l2-common.c
@@ -383,6 +383,36 @@ v4l2_find_nearest_format(const struct v4l2_frmsize_discrete *sizes,
 }
 EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
 
+const void *
+__v4l2_find_nearest_size(const void *arr, size_t num_entries, size_t entry_size,
+			 size_t width_offset, size_t height_offset,
+			 s32 width, s32 height)
+{
+	u32 error, min_error = U32_MAX;
+	const void *best = NULL;
+	unsigned int i;
+
+	if (!arr)
+		return NULL;
+
+	for (i = 0; i < num_entries; i++, arr += entry_size) {
+		const u32 *entry_width = arr + width_offset;
+		const u32 *entry_height = arr + height_offset;
+
+		error = abs(*entry_width - width) + abs(*entry_height - height);
+		if (error > min_error)
+			continue;
+
+		min_error = error;
+		best = arr;
+		if (!error)
+			break;
+	}
+
+	return best;
+}
+EXPORT_SYMBOL_GPL(__v4l2_find_nearest_size);
+
 void v4l2_get_timestamp(struct timeval *tv)
 {
 	struct timespec ts;
diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
index e0d95a7..520463e 100644
--- a/include/media/v4l2-common.h
+++ b/include/media/v4l2-common.h
@@ -333,6 +333,39 @@ v4l2_find_nearest_format(const struct v4l2_frmsize_discrete *sizes,
 			  s32 width, s32 height);
 
 /**
+ * v4l2_find_nearest_size - Find the nearest size among a discrete
+ *	set of resolutions contained in an array of a driver specific struct.
+ *
+ * @sizes: a driver specific array of image sizes
+ * @width_field: the name of the width field in the driver specific struct
+ * @height_field: the name of the height field in the driver specific struct
+ * @width: desired width.
+ * @height: desired height.
+ *
+ * Finds the closest resolution to minimize the width and height differences
+ * between what requested and the supported resolutions. The size of the width
+ * and height fields in the driver specific must equal to that of u32, i.e. four
+ * bytes.
+ *
+ * Returns the best match or NULL if the length of the array is zero.
+ */
+#define v4l2_find_nearest_size(array, array_size, width_field, height_field, \
+			       width, height)				\
+	({								\
+		BUILD_BUG_ON(sizeof((array)->width_field) != sizeof(u32) || \
+			     sizeof((array)->height_field) != sizeof(u32)); \
+		(typeof(&(*(array))))__v4l2_find_nearest_size(		\
+			(array), array_size, sizeof(*(array)),		\
+			offsetof(typeof(*(array)), width_field),	\
+			offsetof(typeof(*(array)), height_field),	\
+			width, height);					\
+	})
+const void *
+__v4l2_find_nearest_size(const void *arr, size_t entry_size,
+			 size_t width_offset, size_t height_offset,
+			 size_t num_entries, s32 width, s32 height);
+
+/**
  * v4l2_get_timestamp - helper routine to get a timestamp to be used when
  *	filling streaming metadata. Internally, it uses ktime_get_ts(),
  *	which is the recommended way to get it.
-- 
2.7.4

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

* [PATCH 2/5] vivid: Use v4l2_find_nearest_size
  2018-02-08 12:44 [PATCH 0/5] Move finding the best match for size to V4L2 common Sakari Ailus
  2018-02-08 12:44 ` [PATCH 1/5] v4l: common: Add a function to obtain best size from a list Sakari Ailus
@ 2018-02-08 12:44 ` Sakari Ailus
  2018-02-08 12:44 ` [PATCH 3/5] v4l: common: Remove v4l2_find_nearest_format Sakari Ailus
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sakari Ailus @ 2018-02-08 12:44 UTC (permalink / raw)
  To: linux-media; +Cc: yong.zhi, Yang, Hyungwoo, Rapolu, Chiranjeevi, andy.yeh

Use v4l2_find_nearest_size instead of a driver specific function to find
nearest matching size.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/platform/vivid/vivid-vid-cap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/media/platform/vivid/vivid-vid-cap.c b/drivers/media/platform/vivid/vivid-vid-cap.c
index 0fbbcde..62a42f6 100644
--- a/drivers/media/platform/vivid/vivid-vid-cap.c
+++ b/drivers/media/platform/vivid/vivid-vid-cap.c
@@ -573,9 +573,9 @@ int vivid_try_fmt_vid_cap(struct file *file, void *priv,
 	mp->field = vivid_field_cap(dev, mp->field);
 	if (vivid_is_webcam(dev)) {
 		const struct v4l2_frmsize_discrete *sz =
-			v4l2_find_nearest_format(webcam_sizes,
-						 VIVID_WEBCAM_SIZES,
-						 mp->width, mp->height);
+			v4l2_find_nearest_size(webcam_sizes,
+					       VIVID_WEBCAM_SIZES, width,
+					       height, mp->width, mp->height);
 
 		w = sz->width;
 		h = sz->height;
-- 
2.7.4

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

* [PATCH 3/5] v4l: common: Remove v4l2_find_nearest_format
  2018-02-08 12:44 [PATCH 0/5] Move finding the best match for size to V4L2 common Sakari Ailus
  2018-02-08 12:44 ` [PATCH 1/5] v4l: common: Add a function to obtain best size from a list Sakari Ailus
  2018-02-08 12:44 ` [PATCH 2/5] vivid: Use v4l2_find_nearest_size Sakari Ailus
@ 2018-02-08 12:44 ` Sakari Ailus
  2018-02-08 12:44 ` [PATCH 4/5] ov13858: Use v4l2_find_nearest_size Sakari Ailus
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sakari Ailus @ 2018-02-08 12:44 UTC (permalink / raw)
  To: linux-media; +Cc: yong.zhi, Yang, Hyungwoo, Rapolu, Chiranjeevi, andy.yeh

v4l2_find_nearest_format is not useful for drivers in finding the best
matching format as it assumes a V4L2 specific struct. Drivers will use
v4l2_find_nearest_size instead.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/v4l2-core/v4l2-common.c | 26 --------------------------
 include/media/v4l2-common.h           | 17 -----------------
 2 files changed, 43 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
index c7a48f2..7bda367 100644
--- a/drivers/media/v4l2-core/v4l2-common.c
+++ b/drivers/media/v4l2-core/v4l2-common.c
@@ -357,32 +357,6 @@ void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
 }
 EXPORT_SYMBOL_GPL(v4l_bound_align_image);
 
-const struct v4l2_frmsize_discrete *
-v4l2_find_nearest_format(const struct v4l2_frmsize_discrete *sizes,
-			  size_t num_sizes,
-			  s32 width, s32 height)
-{
-	int i;
-	u32 error, min_error = UINT_MAX;
-	const struct v4l2_frmsize_discrete *size, *best = NULL;
-
-	if (!sizes)
-		return NULL;
-
-	for (i = 0, size = sizes; i < num_sizes; i++, size++) {
-		error = abs(size->width - width) + abs(size->height - height);
-		if (error < min_error) {
-			min_error = error;
-			best = size;
-		}
-		if (!error)
-			break;
-	}
-
-	return best;
-}
-EXPORT_SYMBOL_GPL(v4l2_find_nearest_format);
-
 const void *
 __v4l2_find_nearest_size(const void *arr, size_t num_entries, size_t entry_size,
 			 size_t width_offset, size_t height_offset,
diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
index 520463e..0895942 100644
--- a/include/media/v4l2-common.h
+++ b/include/media/v4l2-common.h
@@ -316,23 +316,6 @@ void v4l_bound_align_image(unsigned int *width, unsigned int wmin,
 			   unsigned int salign);
 
 /**
- * v4l2_find_nearest_format - find the nearest format size among a discrete
- *	set of resolutions.
- *
- * @sizes: array of &struct v4l2_frmsize_discrete image sizes.
- * @num_sizes: length of @sizes array.
- * @width: desired width.
- * @height: desired height.
- *
- * Finds the closest resolution to minimize the width and height differences
- * between what requested and the supported resolutions.
- */
-const struct v4l2_frmsize_discrete *
-v4l2_find_nearest_format(const struct v4l2_frmsize_discrete *sizes,
-			  const size_t num_sizes,
-			  s32 width, s32 height);
-
-/**
  * v4l2_find_nearest_size - Find the nearest size among a discrete
  *	set of resolutions contained in an array of a driver specific struct.
  *
-- 
2.7.4

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

* [PATCH 4/5] ov13858: Use v4l2_find_nearest_size
  2018-02-08 12:44 [PATCH 0/5] Move finding the best match for size to V4L2 common Sakari Ailus
                   ` (2 preceding siblings ...)
  2018-02-08 12:44 ` [PATCH 3/5] v4l: common: Remove v4l2_find_nearest_format Sakari Ailus
@ 2018-02-08 12:44 ` Sakari Ailus
  2018-02-08 12:44 ` [PATCH 5/5] ov5670: " Sakari Ailus
  2018-02-22 12:35 ` [PATCH 0/5] Move finding the best match for size to V4L2 common Hans Verkuil
  5 siblings, 0 replies; 7+ messages in thread
From: Sakari Ailus @ 2018-02-08 12:44 UTC (permalink / raw)
  To: linux-media; +Cc: yong.zhi, Yang, Hyungwoo, Rapolu, Chiranjeevi, andy.yeh

Use v4l2_find_nearest_size instead of a driver specific function to find
nearest matching size.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/i2c/ov13858.c | 37 +++----------------------------------
 1 file changed, 3 insertions(+), 34 deletions(-)

diff --git a/drivers/media/i2c/ov13858.c b/drivers/media/i2c/ov13858.c
index bf7d06f..a7c42f8 100644
--- a/drivers/media/i2c/ov13858.c
+++ b/drivers/media/i2c/ov13858.c
@@ -1348,39 +1348,6 @@ static int ov13858_get_pad_format(struct v4l2_subdev *sd,
 	return ret;
 }
 
-/*
- * Calculate resolution distance
- */
-static int
-ov13858_get_resolution_dist(const struct ov13858_mode *mode,
-			    struct v4l2_mbus_framefmt *framefmt)
-{
-	return abs(mode->width - framefmt->width) +
-	       abs(mode->height - framefmt->height);
-}
-
-/*
- * Find the closest supported resolution to the requested resolution
- */
-static const struct ov13858_mode *
-ov13858_find_best_fit(struct ov13858 *ov13858,
-		      struct v4l2_subdev_format *fmt)
-{
-	int i, dist, cur_best_fit = 0, cur_best_fit_dist = -1;
-	struct v4l2_mbus_framefmt *framefmt = &fmt->format;
-
-	for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
-		dist = ov13858_get_resolution_dist(&supported_modes[i],
-						   framefmt);
-		if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
-			cur_best_fit_dist = dist;
-			cur_best_fit = i;
-		}
-	}
-
-	return &supported_modes[cur_best_fit];
-}
-
 static int
 ov13858_set_pad_format(struct v4l2_subdev *sd,
 		       struct v4l2_subdev_pad_config *cfg,
@@ -1401,7 +1368,9 @@ ov13858_set_pad_format(struct v4l2_subdev *sd,
 	if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
 		fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
 
-	mode = ov13858_find_best_fit(ov13858, fmt);
+	mode = v4l2_find_nearest_size(
+		supported_modes, ARRAY_SIZE(supported_modes), width, height,
+		fmt->format.width, fmt->format.height);
 	ov13858_update_pad_format(mode, fmt);
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
-- 
2.7.4

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

* [PATCH 5/5] ov5670: Use v4l2_find_nearest_size
  2018-02-08 12:44 [PATCH 0/5] Move finding the best match for size to V4L2 common Sakari Ailus
                   ` (3 preceding siblings ...)
  2018-02-08 12:44 ` [PATCH 4/5] ov13858: Use v4l2_find_nearest_size Sakari Ailus
@ 2018-02-08 12:44 ` Sakari Ailus
  2018-02-22 12:35 ` [PATCH 0/5] Move finding the best match for size to V4L2 common Hans Verkuil
  5 siblings, 0 replies; 7+ messages in thread
From: Sakari Ailus @ 2018-02-08 12:44 UTC (permalink / raw)
  To: linux-media; +Cc: yong.zhi, Yang, Hyungwoo, Rapolu, Chiranjeevi, andy.yeh

Use v4l2_find_nearest_size instead of a driver specific function to find
nearest matching size.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/i2c/ov5670.c | 34 +++-------------------------------
 1 file changed, 3 insertions(+), 31 deletions(-)

diff --git a/drivers/media/i2c/ov5670.c b/drivers/media/i2c/ov5670.c
index 9f91965..028abc8 100644
--- a/drivers/media/i2c/ov5670.c
+++ b/drivers/media/i2c/ov5670.c
@@ -2180,36 +2180,6 @@ static int ov5670_enum_frame_size(struct v4l2_subdev *sd,
 	return 0;
 }
 
-/* Calculate resolution distance */
-static int ov5670_get_reso_dist(const struct ov5670_mode *mode,
-				struct v4l2_mbus_framefmt *framefmt)
-{
-	return abs(mode->width - framefmt->width) +
-	       abs(mode->height - framefmt->height);
-}
-
-/* Find the closest supported resolution to the requested resolution */
-static const struct ov5670_mode *ov5670_find_best_fit(
-						struct ov5670 *ov5670,
-						struct v4l2_subdev_format *fmt)
-{
-	struct v4l2_mbus_framefmt *framefmt = &fmt->format;
-	int dist;
-	int cur_best_fit = 0;
-	int cur_best_fit_dist = -1;
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
-		dist = ov5670_get_reso_dist(&supported_modes[i], framefmt);
-		if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
-			cur_best_fit_dist = dist;
-			cur_best_fit = i;
-		}
-	}
-
-	return &supported_modes[cur_best_fit];
-}
-
 static void ov5670_update_pad_format(const struct ov5670_mode *mode,
 				     struct v4l2_subdev_format *fmt)
 {
@@ -2259,7 +2229,9 @@ static int ov5670_set_pad_format(struct v4l2_subdev *sd,
 
 	fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
 
-	mode = ov5670_find_best_fit(ov5670, fmt);
+	mode = v4l2_find_nearest_size(
+		supported_modes, ARRAY_SIZE(supported_modes), width, height,
+		fmt->format.width, fmt->format.height);
 	ov5670_update_pad_format(mode, fmt);
 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
 		*v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
-- 
2.7.4

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

* Re: [PATCH 0/5] Move finding the best match for size to V4L2 common
  2018-02-08 12:44 [PATCH 0/5] Move finding the best match for size to V4L2 common Sakari Ailus
                   ` (4 preceding siblings ...)
  2018-02-08 12:44 ` [PATCH 5/5] ov5670: " Sakari Ailus
@ 2018-02-22 12:35 ` Hans Verkuil
  5 siblings, 0 replies; 7+ messages in thread
From: Hans Verkuil @ 2018-02-22 12:35 UTC (permalink / raw)
  To: Sakari Ailus, linux-media; +Cc: yong.zhi, Hyungwoo, Chiranjeevi, andy.yeh

On 02/08/18 13:44, Sakari Ailus wrote:
> Hi folks,
> 
> This set should make it a bit easier to support finding the right size in
> sensor drivers. Two sensor drivers and vivid are converted as an example.
> 
> I've tested the vivid change only but the patches are effectively the same
> and trivial.

For this series:

Acked-by: Hans Verkuil <hans.verkuil@cisco.com>

Regards,

	Hans

> 
> Sakari Ailus (5):
>   v4l: common: Add a function to obtain best size from a list
>   vivid: Use v4l2_find_nearest_size
>   v4l: common: Remove v4l2_find_nearest_format
>   ov13858: Use v4l2_find_nearest_size
>   ov5670: Use v4l2_find_nearest_size
> 
>  drivers/media/i2c/ov13858.c                  | 37 +++-------------------------
>  drivers/media/i2c/ov5670.c                   | 34 +++----------------------
>  drivers/media/platform/vivid/vivid-vid-cap.c |  6 ++---
>  drivers/media/v4l2-core/v4l2-common.c        | 34 ++++++++++++++-----------
>  include/media/v4l2-common.h                  | 34 ++++++++++++++++++-------
>  5 files changed, 53 insertions(+), 92 deletions(-)
> 

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

end of thread, other threads:[~2018-02-22 12:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-08 12:44 [PATCH 0/5] Move finding the best match for size to V4L2 common Sakari Ailus
2018-02-08 12:44 ` [PATCH 1/5] v4l: common: Add a function to obtain best size from a list Sakari Ailus
2018-02-08 12:44 ` [PATCH 2/5] vivid: Use v4l2_find_nearest_size Sakari Ailus
2018-02-08 12:44 ` [PATCH 3/5] v4l: common: Remove v4l2_find_nearest_format Sakari Ailus
2018-02-08 12:44 ` [PATCH 4/5] ov13858: Use v4l2_find_nearest_size Sakari Ailus
2018-02-08 12:44 ` [PATCH 5/5] ov5670: " Sakari Ailus
2018-02-22 12:35 ` [PATCH 0/5] Move finding the best match for size to V4L2 common Hans Verkuil

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.