[libcamera-devel,v7,4/7] libcamera: Support passing ColorSpaces to V4L2 video devices
diff mbox series

Message ID 20211126104045.4756-5-david.plowman@raspberrypi.com
State Superseded
Headers show
Series
  • Colour spaces
Related show

Commit Message

David Plowman Nov. 26, 2021, 10:40 a.m. UTC
The ColorSpace from the StreamConfiguration is now handled
appropriately in the V4L2VideoDevice.

Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
---
 include/libcamera/internal/v4l2_videodevice.h |  2 ++
 src/libcamera/v4l2_videodevice.cpp            | 32 +++++++++++++++++++
 2 files changed, 34 insertions(+)

Comments

Jacopo Mondi Nov. 30, 2021, 9:12 p.m. UTC | #1
Hi David,
   I gave my opinion about setting colorspace and formats through the
same fuction in the previous version. Let's wait for more comments
which I hope will arrive soon!

Thanks
   j

On Fri, Nov 26, 2021 at 10:40:42AM +0000, David Plowman wrote:
> The ColorSpace from the StreamConfiguration is now handled
> appropriately in the V4L2VideoDevice.
>
> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> ---
>  include/libcamera/internal/v4l2_videodevice.h |  2 ++
>  src/libcamera/v4l2_videodevice.cpp            | 32 +++++++++++++++++++
>  2 files changed, 34 insertions(+)
>
> diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
> index 4a44b7fd..602f40fb 100644
> --- a/include/libcamera/internal/v4l2_videodevice.h
> +++ b/include/libcamera/internal/v4l2_videodevice.h
> @@ -20,6 +20,7 @@
>  #include <libcamera/base/log.h>
>  #include <libcamera/base/signal.h>
>
> +#include <libcamera/color_space.h>
>  #include <libcamera/framebuffer.h>
>  #include <libcamera/geometry.h>
>  #include <libcamera/pixel_format.h>
> @@ -167,6 +168,7 @@ public:
>
>  	V4L2PixelFormat fourcc;
>  	Size size;
> +	std::optional<ColorSpace> colorSpace;
>
>  	std::array<Plane, 3> planes;
>  	unsigned int planesCount = 0;
> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
> index 4f04212d..c8f78602 100644
> --- a/src/libcamera/v4l2_videodevice.cpp
> +++ b/src/libcamera/v4l2_videodevice.cpp
> @@ -383,6 +383,21 @@ bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const
>   * that identifies the image format pixel encoding scheme.
>   */
>
> +/**
> + * \var V4L2DeviceFormat::colorSpace
> + * \brief The color space of the pixels
> + *
> + * The color space of the image. When setting the format this may be
> + * unset, in which case the driver gets to use its default color space.
> + * After being set, this value should contain the color space that
> + * was actually used. If this value is unset, then the color space chosen
> + * by the driver could not be represented by the ColorSpace class (and
> + * should probably be added).
> + *
> + * It is up to the pipeline handler or application to check if the
> + * resulting color space is acceptable.
> + */
> +
>  /**
>   * \var V4L2DeviceFormat::planes
>   * \brief The per-plane memory size information
> @@ -878,6 +893,7 @@ int V4L2VideoDevice::getFormatMultiplane(V4L2DeviceFormat *format)
>  	format->size.height = pix->height;
>  	format->fourcc = V4L2PixelFormat(pix->pixelformat);
>  	format->planesCount = pix->num_planes;
> +	format->colorSpace = toColorSpace(*pix);
>
>  	for (unsigned int i = 0; i < format->planesCount; ++i) {
>  		format->planes[i].bpl = pix->plane_fmt[i].bytesperline;
> @@ -900,6 +916,12 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set)
>  	pix->num_planes = format->planesCount;
>  	pix->field = V4L2_FIELD_NONE;
>
> +	ret = fromColorSpace(format->colorSpace, *pix);
> +	if (ret < 0)
> +		LOG(V4L2, Warning)
> +			<< "Setting color space unrecognised by V4L2: "
> +			<< ColorSpace::toString(format->colorSpace);
> +
>  	ASSERT(pix->num_planes <= std::size(pix->plane_fmt));
>
>  	for (unsigned int i = 0; i < pix->num_planes; ++i) {
> @@ -927,6 +949,7 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set)
>  		format->planes[i].bpl = pix->plane_fmt[i].bytesperline;
>  		format->planes[i].size = pix->plane_fmt[i].sizeimage;
>  	}
> +	format->colorSpace = toColorSpace(*pix);
>
>  	return 0;
>  }
> @@ -950,6 +973,7 @@ int V4L2VideoDevice::getFormatSingleplane(V4L2DeviceFormat *format)
>  	format->planesCount = 1;
>  	format->planes[0].bpl = pix->bytesperline;
>  	format->planes[0].size = pix->sizeimage;
> +	format->colorSpace = toColorSpace(*pix);
>
>  	return 0;
>  }
> @@ -966,6 +990,13 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set)
>  	pix->pixelformat = format->fourcc;
>  	pix->bytesperline = format->planes[0].bpl;
>  	pix->field = V4L2_FIELD_NONE;
> +
> +	ret = fromColorSpace(format->colorSpace, *pix);
> +	if (ret < 0)
> +		LOG(V4L2, Warning)
> +			<< "Set color space unrecognised by V4L2: "
> +			<< ColorSpace::toString(format->colorSpace);
> +
>  	ret = ioctl(set ? VIDIOC_S_FMT : VIDIOC_TRY_FMT, &v4l2Format);
>  	if (ret) {
>  		LOG(V4L2, Error)
> @@ -984,6 +1015,7 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set)
>  	format->planesCount = 1;
>  	format->planes[0].bpl = pix->bytesperline;
>  	format->planes[0].size = pix->sizeimage;
> +	format->colorSpace = toColorSpace(*pix);
>
>  	return 0;
>  }
> --
> 2.30.2
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
index 4a44b7fd..602f40fb 100644
--- a/include/libcamera/internal/v4l2_videodevice.h
+++ b/include/libcamera/internal/v4l2_videodevice.h
@@ -20,6 +20,7 @@ 
 #include <libcamera/base/log.h>
 #include <libcamera/base/signal.h>
 
+#include <libcamera/color_space.h>
 #include <libcamera/framebuffer.h>
 #include <libcamera/geometry.h>
 #include <libcamera/pixel_format.h>
@@ -167,6 +168,7 @@  public:
 
 	V4L2PixelFormat fourcc;
 	Size size;
+	std::optional<ColorSpace> colorSpace;
 
 	std::array<Plane, 3> planes;
 	unsigned int planesCount = 0;
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 4f04212d..c8f78602 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -383,6 +383,21 @@  bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const
  * that identifies the image format pixel encoding scheme.
  */
 
+/**
+ * \var V4L2DeviceFormat::colorSpace
+ * \brief The color space of the pixels
+ *
+ * The color space of the image. When setting the format this may be
+ * unset, in which case the driver gets to use its default color space.
+ * After being set, this value should contain the color space that
+ * was actually used. If this value is unset, then the color space chosen
+ * by the driver could not be represented by the ColorSpace class (and
+ * should probably be added).
+ *
+ * It is up to the pipeline handler or application to check if the
+ * resulting color space is acceptable.
+ */
+
 /**
  * \var V4L2DeviceFormat::planes
  * \brief The per-plane memory size information
@@ -878,6 +893,7 @@  int V4L2VideoDevice::getFormatMultiplane(V4L2DeviceFormat *format)
 	format->size.height = pix->height;
 	format->fourcc = V4L2PixelFormat(pix->pixelformat);
 	format->planesCount = pix->num_planes;
+	format->colorSpace = toColorSpace(*pix);
 
 	for (unsigned int i = 0; i < format->planesCount; ++i) {
 		format->planes[i].bpl = pix->plane_fmt[i].bytesperline;
@@ -900,6 +916,12 @@  int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set)
 	pix->num_planes = format->planesCount;
 	pix->field = V4L2_FIELD_NONE;
 
+	ret = fromColorSpace(format->colorSpace, *pix);
+	if (ret < 0)
+		LOG(V4L2, Warning)
+			<< "Setting color space unrecognised by V4L2: "
+			<< ColorSpace::toString(format->colorSpace);
+
 	ASSERT(pix->num_planes <= std::size(pix->plane_fmt));
 
 	for (unsigned int i = 0; i < pix->num_planes; ++i) {
@@ -927,6 +949,7 @@  int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set)
 		format->planes[i].bpl = pix->plane_fmt[i].bytesperline;
 		format->planes[i].size = pix->plane_fmt[i].sizeimage;
 	}
+	format->colorSpace = toColorSpace(*pix);
 
 	return 0;
 }
@@ -950,6 +973,7 @@  int V4L2VideoDevice::getFormatSingleplane(V4L2DeviceFormat *format)
 	format->planesCount = 1;
 	format->planes[0].bpl = pix->bytesperline;
 	format->planes[0].size = pix->sizeimage;
+	format->colorSpace = toColorSpace(*pix);
 
 	return 0;
 }
@@ -966,6 +990,13 @@  int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set)
 	pix->pixelformat = format->fourcc;
 	pix->bytesperline = format->planes[0].bpl;
 	pix->field = V4L2_FIELD_NONE;
+
+	ret = fromColorSpace(format->colorSpace, *pix);
+	if (ret < 0)
+		LOG(V4L2, Warning)
+			<< "Set color space unrecognised by V4L2: "
+			<< ColorSpace::toString(format->colorSpace);
+
 	ret = ioctl(set ? VIDIOC_S_FMT : VIDIOC_TRY_FMT, &v4l2Format);
 	if (ret) {
 		LOG(V4L2, Error)
@@ -984,6 +1015,7 @@  int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set)
 	format->planesCount = 1;
 	format->planes[0].bpl = pix->bytesperline;
 	format->planes[0].size = pix->sizeimage;
+	format->colorSpace = toColorSpace(*pix);
 
 	return 0;
 }