Skip to contentSkip to conversation

Move finding the best match for size to V4L2 common

PATCH In review Sakari Ailus 2 participants 7 messages Feb 8, 2018 – Feb 22, 2018

Subsystems (4)
lore
Share & tools
lore

Sakari Ailus

author maintainer
#0 lore raw

cover Move finding the best match for size to V4L2 common

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(-)
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH 1/5 v4l: common: Add a function to obtain best size from a list

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.

---
 drivers/media/v4l2-core/v4l2-common.c | 30 ++++++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)
drivers/media/v4l2-core/v4l2-common.c +30 -0
index 8650ad9..c7a48f2 100644
@@ -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;
include/media/v4l2-common.h +33 -0
index e0d95a7..520463e 100644
@@ -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.
Signature · 1 line
2.7.4
author maintainer
#2 lore raw

PATCH 2/5 vivid: Use v4l2_find_nearest_size

In reply to Sakari Ailus

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

---
 drivers/media/platform/vivid/vivid-vid-cap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/platform/vivid/vivid-vid-cap.c +3 -3
index 0fbbcde..62a42f6 100644
@@ -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;
Signature · 1 line
2.7.4
author maintainer
#3 lore raw

PATCH 3/5 v4l: common: Remove v4l2_find_nearest_format

In reply to Sakari Ailus

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.

---
 drivers/media/v4l2-core/v4l2-common.c | 26 --------------------------
 include/media/v4l2-common.h           | 17 -----------------
 2 files changed, 43 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +0 -26
index c7a48f2..7bda367 100644
@@ -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,
include/media/v4l2-common.h +0 -17
index 520463e..0895942 100644
@@ -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.
  *
Signature · 1 line
2.7.4
author maintainer
#4 lore raw

PATCH 4/5 ov13858: Use v4l2_find_nearest_size

In reply to Sakari Ailus

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

---
 drivers/media/i2c/ov13858.c | 37 +++----------------------------------
 1 file changed, 3 insertions(+), 34 deletions(-)
drivers/media/i2c/ov13858.c +3 -34
index bf7d06f..a7c42f8 100644
@@ -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);
Signature · 1 line
2.7.4
author maintainer
#5 lore raw

PATCH 5/5 ov5670: Use v4l2_find_nearest_size

In reply to Sakari Ailus

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

---
 drivers/media/i2c/ov5670.c | 34 +++-------------------------------
 1 file changed, 3 insertions(+), 31 deletions(-)
drivers/media/i2c/ov5670.c +3 -31
index 9f91965..028abc8 100644
@@ -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;
Signature · 1 line
2.7.4
Acked-by on the v1 series
#6 lore raw
In reply to Sakari Ailus
Quoted 7 lines · Sakari Ailus

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:

Regards,

Hans

Quoted 15 lines

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(-)