From 95ce9c28601afc5da0c11792601ad32dd14cdd44 Mon Sep 17 00:00:00 2001 From: Sakari Ailus Date: Fri, 23 Feb 2018 04:50:14 -0500 Subject: media: 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. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab --- drivers/media/v4l2-core/v4l2-common.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'drivers/media/v4l2-core') diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c index 96c1b31de9e3..9b65529dfaf6 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 *array, size_t array_size, + 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 (!array) + return NULL; + + for (i = 0; i < array_size; i++, array += entry_size) { + const u32 *entry_width = array + width_offset; + const u32 *entry_height = array + height_offset; + + error = abs(*entry_width - width) + abs(*entry_height - height); + if (error > min_error) + continue; + + min_error = error; + best = array; + if (!error) + break; + } + + return best; +} +EXPORT_SYMBOL_GPL(__v4l2_find_nearest_size); + void v4l2_get_timestamp(struct timeval *tv) { struct timespec ts; -- cgit v1.2.1