Skip to contentSkip to conversation

media: common: Add v4l2_find_nearest_size_conditional()

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

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 drivers/media/v4l2-core/v4l2-common.c | 10 ++++-
 include/media/v4l2-common.h           | 56 +++++++++++++++++++++------
 2 files changed, 53 insertions(+), 13 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +8 -2
index e4b2de3833ee..7da4fa937976 100644
@@ -156,11 +156,14 @@ 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)
+			 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 +172,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;
include/media/v4l2-common.h +45 -11

82 lines not on this page · raw message

Signature · 1 line
2.39.5