[libcamera-devel] [PATCH 3/6] libcamera: v4l2_device: Add META support in g/s_fmt

Laurent Pinchart laurent.pinchart at ideasonboard.com
Fri May 24 21:34:08 CEST 2019


Hi Jacopo,

Thank you for the patch.

On Fri, May 24, 2019 at 06:21:36PM +0200, Jacopo Mondi wrote:
> Add two operations to set and get format on devices implementing the
> V4L2 Metadata Interface, identified by the META_OUTPUT or META_CAPTURE
> capabilities.
> 
> While at it, sort get/setFormat operations alphabetically and unify
> their style (eg. no empty line before ioctl).
> 
> Signed-off-by: Jacopo Mondi <jacopo at jmondi.org>
> ---
>  src/libcamera/include/v4l2_device.h |   7 +-
>  src/libcamera/v4l2_device.cpp       | 110 +++++++++++++++++++++-------
>  2 files changed, 89 insertions(+), 28 deletions(-)
> 
> diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h
> index cecafa151caa..bdecc087fe5a 100644
> --- a/src/libcamera/include/v4l2_device.h
> +++ b/src/libcamera/include/v4l2_device.h
> @@ -150,12 +150,15 @@ protected:
>  	std::string logPrefix() const;
>  
>  private:
> -	int getFormatSingleplane(V4L2DeviceFormat *format);
> -	int setFormatSingleplane(V4L2DeviceFormat *format);
> +	int getFormatMeta(V4L2DeviceFormat *format);
> +	int setFormatMeta(V4L2DeviceFormat *format);
>  
>  	int getFormatMultiplane(V4L2DeviceFormat *format);
>  	int setFormatMultiplane(V4L2DeviceFormat *format);
>  
> +	int getFormatSingleplane(V4L2DeviceFormat *format);
> +	int setFormatSingleplane(V4L2DeviceFormat *format);
> +
>  	int requestBuffers(unsigned int count);
>  	int createPlane(Buffer *buffer, unsigned int plane,
>  			unsigned int length);
> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
> index e42f6ef0c340..789f76b0f69f 100644
> --- a/src/libcamera/v4l2_device.cpp
> +++ b/src/libcamera/v4l2_device.cpp
> @@ -428,8 +428,12 @@ std::string V4L2Device::logPrefix() const
>   */
>  int V4L2Device::getFormat(V4L2DeviceFormat *format)
>  {
> -	return caps_.isMultiplanar() ? getFormatMultiplane(format) :
> -				       getFormatSingleplane(format);
> +	if (caps_.isMeta())
> +		return getFormatMeta(format);
> +	if (caps_.isMultiplanar())

I would either use an else if here, or remove the else below. Same for
the next function.

> +		return getFormatMultiplane(format);
> +	else
> +		return getFormatSingleplane(format);
>  }
>  
>  /**
> @@ -443,14 +447,18 @@ int V4L2Device::getFormat(V4L2DeviceFormat *format)
>   */
>  int V4L2Device::setFormat(V4L2DeviceFormat *format)
>  {
> -	return caps_.isMultiplanar() ? setFormatMultiplane(format) :
> -				       setFormatSingleplane(format);
> +	if (caps_.isMeta())
> +		return setFormatMeta(format);
> +	if (caps_.isMultiplanar())
> +		return setFormatMultiplane(format);
> +	else
> +		return setFormatSingleplane(format);
>  }
>  
> -int V4L2Device::getFormatSingleplane(V4L2DeviceFormat *format)
> +int V4L2Device::getFormatMeta(V4L2DeviceFormat *format)
>  {
>  	struct v4l2_format v4l2Format = {};
> -	struct v4l2_pix_format *pix = &v4l2Format.fmt.pix;
> +	struct v4l2_meta_format *pix = &v4l2Format.fmt.meta;
>  	int ret;
>  
>  	v4l2Format.type = bufferType_;
> @@ -461,29 +469,24 @@ int V4L2Device::getFormatSingleplane(V4L2DeviceFormat *format)
>  		return ret;
>  	}
>  
> -	format->size.width = pix->width;
> -	format->size.height = pix->height;
> -	format->fourcc = pix->pixelformat;
> +	format->size.width = pix->buffersize;
> +	format->size.height = 1;

I would set those two fields to 0, the buffer size is already reported
through format->planes[0].bpl. Same for the other locations below.

> +	format->fourcc = pix->dataformat;
>  	format->planesCount = 1;
> -	format->planes[0].bpl = pix->bytesperline;
> -	format->planes[0].size = pix->sizeimage;
> +	format->planes[0].bpl = pix->buffersize;
> +	format->planes[0].size = pix->buffersize;
>  
>  	return 0;
>  }
>  
> -int V4L2Device::setFormatSingleplane(V4L2DeviceFormat *format)
> +int V4L2Device::setFormatMeta(V4L2DeviceFormat *format)
>  {
>  	struct v4l2_format v4l2Format = {};
> -	struct v4l2_pix_format *pix = &v4l2Format.fmt.pix;
> +	struct v4l2_meta_format *pix = &v4l2Format.fmt.meta;
>  	int ret;
>  
>  	v4l2Format.type = bufferType_;
> -	pix->width = format->size.width;
> -	pix->height = format->size.height;
> -	pix->pixelformat = format->fourcc;
> -	pix->bytesperline = format->planes[0].bpl;
> -	pix->field = V4L2_FIELD_NONE;
> -
> +	pix->dataformat = format->fourcc;
>  	ret = ioctl(fd_, VIDIOC_S_FMT, &v4l2Format);
>  	if (ret) {
>  		ret = -errno;
> @@ -495,12 +498,12 @@ int V4L2Device::setFormatSingleplane(V4L2DeviceFormat *format)
>  	 * Return to caller the format actually applied on the device,
>  	 * which might differ from the requested one.
>  	 */
> -	format->size.width = pix->width;
> -	format->size.height = pix->height;
> -	format->fourcc = pix->pixelformat;
> +	format->size.width = pix->buffersize;
> +	format->size.height = 1;
> +	format->fourcc = format->fourcc;
>  	format->planesCount = 1;
> -	format->planes[0].bpl = pix->bytesperline;
> -	format->planes[0].size = pix->sizeimage;
> +	format->planes[0].bpl = pix->buffersize;
> +	format->planes[0].size = pix->buffersize;
>  
>  	return 0;
>  }
> @@ -523,7 +526,6 @@ int V4L2Device::getFormatMultiplane(V4L2DeviceFormat *format)
>  	format->size.height = pix->height;
>  	format->fourcc = pix->pixelformat;
>  	format->planesCount = pix->num_planes;
> -

I would keep this, I think it makes the code more readable.

>  	for (unsigned int i = 0; i < format->planesCount; ++i) {
>  		format->planes[i].bpl = pix->plane_fmt[i].bytesperline;
>  		format->planes[i].size = pix->plane_fmt[i].sizeimage;
> @@ -544,7 +546,6 @@ int V4L2Device::setFormatMultiplane(V4L2DeviceFormat *format)
>  	pix->pixelformat = format->fourcc;
>  	pix->num_planes = format->planesCount;
>  	pix->field = V4L2_FIELD_NONE;
> -
>  	for (unsigned int i = 0; i < pix->num_planes; ++i) {
>  		pix->plane_fmt[i].bytesperline = format->planes[i].bpl;
>  		pix->plane_fmt[i].sizeimage = format->planes[i].size;
> @@ -573,6 +574,63 @@ int V4L2Device::setFormatMultiplane(V4L2DeviceFormat *format)
>  	return 0;
>  }
>  
> +int V4L2Device::getFormatSingleplane(V4L2DeviceFormat *format)
> +{
> +	struct v4l2_format v4l2Format = {};
> +	struct v4l2_pix_format *pix = &v4l2Format.fmt.pix;
> +	int ret;
> +
> +	v4l2Format.type = bufferType_;
> +	ret = ioctl(fd_, VIDIOC_G_FMT, &v4l2Format);
> +	if (ret) {
> +		ret = -errno;
> +		LOG(V4L2, Error) << "Unable to get format: " << strerror(-ret);
> +		return ret;
> +	}
> +
> +	format->size.width = pix->width;
> +	format->size.height = pix->height;
> +	format->fourcc = pix->pixelformat;
> +	format->planesCount = 1;
> +	format->planes[0].bpl = pix->bytesperline;
> +	format->planes[0].size = pix->sizeimage;
> +
> +	return 0;
> +}
> +
> +int V4L2Device::setFormatSingleplane(V4L2DeviceFormat *format)
> +{
> +	struct v4l2_format v4l2Format = {};
> +	struct v4l2_pix_format *pix = &v4l2Format.fmt.pix;
> +	int ret;
> +
> +	v4l2Format.type = bufferType_;
> +	pix->width = format->size.width;
> +	pix->height = format->size.height;
> +	pix->pixelformat = format->fourcc;
> +	pix->bytesperline = format->planes[0].bpl;
> +	pix->field = V4L2_FIELD_NONE;
> +	ret = ioctl(fd_, VIDIOC_S_FMT, &v4l2Format);
> +	if (ret) {
> +		ret = -errno;
> +		LOG(V4L2, Error) << "Unable to set format: " << strerror(-ret);
> +		return ret;
> +	}
> +
> +	/*
> +	 * Return to caller the format actually applied on the device,
> +	 * which might differ from the requested one.
> +	 */
> +	format->size.width = pix->width;
> +	format->size.height = pix->height;
> +	format->fourcc = pix->pixelformat;
> +	format->planesCount = 1;
> +	format->planes[0].bpl = pix->bytesperline;
> +	format->planes[0].size = pix->sizeimage;
> +
> +	return 0;
> +}
> +
>  int V4L2Device::requestBuffers(unsigned int count)
>  {
>  	struct v4l2_requestbuffers rb = {};

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list