Skip to contentSkip to conversation

media: common: Add v4l2_find_nearest_size_conditional()

PATCH New Sakari Ailus 1 participant 1 message Mar 5, 2025 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 media: common: Add v4l2_find_nearest_size_conditional()

v4l2_find_nearest_size() returns a mode from sensor driver's mode list that is a best match width and height wise for the sensor. Some drivers have different set of available modes depending on the number of lanes. While this could be handled within a driver by providing different lists of modes, provide a helper v4l2_find_nearest_size_conditional() to ignore modes that aren't available.

Also use size_t for the array index and remove extra commas while at it.

---
since v2:

- Remove the leftover semicolon from v4l2_find_nearest_size() macro definition.

 drivers/media/v4l2-core/v4l2-common.c | 18 ++++++---
 include/media/v4l2-common.h           | 58 +++++++++++++++++++++------
 2 files changed, 59 insertions(+), 17 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +13 -5
index e4b2de3833ee..06d2f468e1af 100644
@@ -154,13 +154,18 @@ void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
 EXPORT_SYMBOL_GPL(v4l_bound_align_image);
 
 const void *
-__v4l2_find_nearest_size(const void *array, size_t array_size,
-			 size_t entry_size, size_t width_offset,
-			 size_t height_offset, s32 width, s32 height)
+__v4l2_find_nearest_size_conditional(const void *array, size_t array_size,
+				     size_t entry_size, size_t width_offset,
+				     size_t height_offset, s32 width,
+				     s32 height,
+				     bool (*func)(const void *array,
+						  size_t index,
+						  const void *context),
+				     const void *context)
 {
 	u32 error, min_error = U32_MAX;
 	const void *best = NULL;
-	unsigned int i;
+	size_t i;
 
 	if (!array)
 		return NULL;
@@ -169,6 +174,9 @@ __v4l2_find_nearest_size(const void *array, size_t array_size,
 		const u32 *entry_width = array + width_offset;
 		const u32 *entry_height = array + height_offset;
 
+		if (func && !func(array, i, context))
+			continue;
+
 		error = abs(*entry_width - width) + abs(*entry_height - height);
 		if (error > min_error)
 			continue;
@@ -181,7 +189,7 @@ __v4l2_find_nearest_size(const void *array, size_t array_size,
 
 	return best;
 }
-EXPORT_SYMBOL_GPL(__v4l2_find_nearest_size);
+EXPORT_SYMBOL_GPL(__v4l2_find_nearest_size_conditional);
 
 int v4l2_g_parm_cap(struct video_device *vdev,
 		    struct v4l2_subdev *sd, struct v4l2_streamparm *a)
include/media/v4l2-common.h +46 -12

84 lines not on this page · raw message

Signature · 1 line
2.39.5