[libcamera-devel,v3,2/6] libcamera: formats: Search PixelFormatInfo on multiple formats
diff mbox series

Message ID 20220729160014.101503-3-jacopo@jmondi.org
State Superseded, archived
Headers show
Series
  • libcamera: Map multiple V4L2 formats to a single libcamera::format
Related show

Commit Message

Jacopo Mondi July 29, 2022, 4 p.m. UTC
The PixelFormatInfo::info(const V4L2PixelFormat &format) function
returns the PixelFormatInfo associated with a V4L2 pixel format.

To correctly support multiple V4L2 formats mapped to a single
PixelFormatInfo rework the function to search the given V4L2 format
in a list.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/libcamera/formats.cpp | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

--
2.37.1

Comments

Laurent Pinchart July 29, 2022, 5:16 p.m. UTC | #1
Hi Jacopo,

Thank you for the patch.

On Fri, Jul 29, 2022 at 06:00:10PM +0200, Jacopo Mondi via libcamera-devel wrote:
> The PixelFormatInfo::info(const V4L2PixelFormat &format) function
> returns the PixelFormatInfo associated with a V4L2 pixel format.
> 
> To correctly support multiple V4L2 formats mapped to a single
> PixelFormatInfo rework the function to search the given V4L2 format
> in a list.
> 
> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>

I think I'd squash this with 1/6.

> ---
>  src/libcamera/formats.cpp | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp
> index 3c536722f375..6921d5c43bcb 100644
> --- a/src/libcamera/formats.cpp
> +++ b/src/libcamera/formats.cpp
> @@ -829,9 +829,18 @@ const PixelFormatInfo &PixelFormatInfo::info(const PixelFormat &format)
>   */
>  const PixelFormatInfo &PixelFormatInfo::info(const V4L2PixelFormat &format)
>  {
> +	auto matchFormats = [&format](const std::vector<V4L2PixelFormat> &formats) {
> +		const auto &it = std::find_if(formats.begin(), formats.end(),
> +					      [&format](const V4L2PixelFormat &fmt) {
> +						      return format == fmt;
> +					      });
> +
> +		return it != formats.end();

You can simplify this to

		return std::find(formats.begin(), formats.end(), format)
			!= formats.end();

> +	};
> +
>  	const auto &info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(),
> -					[format](auto pair) {
> -						return pair.second.v4l2Formats[0] == format;
> +					[&matchFormats](auto &pair) {
> +						return matchFormats(pair.second.v4l2Formats);
>  					});

Or possibly

	const auto &info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(),
					[](auto &pair) {
						const auto &formats = pair.second.v4l2Formats;
						return std::find(formats.begin(), formats.end(), format)
							!= formats.end();
					});

Actually, how about just reimplementing this function as

	return info(format.toPixelFormat());

as a 1/6 ? I think that would be more efficient, and would be simpler to
maintain.

>  	if (info == pixelFormatInfo.end())
>  		return pixelFormatInfoInvalid;

Patch
diff mbox series

diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp
index 3c536722f375..6921d5c43bcb 100644
--- a/src/libcamera/formats.cpp
+++ b/src/libcamera/formats.cpp
@@ -829,9 +829,18 @@  const PixelFormatInfo &PixelFormatInfo::info(const PixelFormat &format)
  */
 const PixelFormatInfo &PixelFormatInfo::info(const V4L2PixelFormat &format)
 {
+	auto matchFormats = [&format](const std::vector<V4L2PixelFormat> &formats) {
+		const auto &it = std::find_if(formats.begin(), formats.end(),
+					      [&format](const V4L2PixelFormat &fmt) {
+						      return format == fmt;
+					      });
+
+		return it != formats.end();
+	};
+
 	const auto &info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(),
-					[format](auto pair) {
-						return pair.second.v4l2Formats[0] == format;
+					[&matchFormats](auto &pair) {
+						return matchFormats(pair.second.v4l2Formats);
 					});
 	if (info == pixelFormatInfo.end())
 		return pixelFormatInfoInvalid;