[libcamera-devel] [PATCH v3 2/6] libcamera: formats: Search PixelFormatInfo on multiple formats

Laurent Pinchart laurent.pinchart at ideasonboard.com
Fri Jul 29 19:16:29 CEST 2022


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 at 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;

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list