[libcamera-devel,v4,09/12] libcamera: rpi: Add some helpers to PipelineHandlerBase
diff mbox series

Message ID 20230916121930.29490-10-jacopo.mondi@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: Introduce SensorConfiguration
Related show

Commit Message

Jacopo Mondi Sept. 16, 2023, 12:19 p.m. UTC
From: Naushir Patuck <naush@raspberrypi.com>

Add a helper updateStreamConfig() that updates the format related fields
in a StreamConfiguration from a given V4L2DeviceFormat structure.

Add and override to the toV4L2DeviceFormat() helper that returns a
V4L2DeviceFormat structure populated from the format related fields in
a StreamConfiguration.

Both these helper functions will be used in a future commit to simplify
the Raspberry Pi pipeline handler configuration/validation code.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 .../pipeline/rpi/common/pipeline_base.cpp     | 47 +++++++++++++++++++
 .../pipeline/rpi/common/pipeline_base.h       |  4 ++
 2 files changed, 51 insertions(+)

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp
index e7fedca7cc8d..93779556fd81 100644
--- a/src/libcamera/pipeline/rpi/common/pipeline_base.cpp
+++ b/src/libcamera/pipeline/rpi/common/pipeline_base.cpp
@@ -349,6 +349,53 @@  bool PipelineHandlerBase::isRaw(const PixelFormat &pixFmt)
 	return BayerFormat::fromPixelFormat(pixFmt).isValid();
 }
 
+/*
+ * Adjust a StreamConfiguration fields to match a video device format.
+ * Returns true if the StreamConfiguration has been adjusted.
+ */
+bool PipelineHandlerBase::updateStreamConfig(StreamConfiguration *stream,
+					     const V4L2DeviceFormat &format)
+{
+	const PixelFormat &pixFormat = format.fourcc.toPixelFormat();
+	bool adjusted = false;
+
+	if (stream->pixelFormat != pixFormat || stream->size != format.size) {
+		stream->pixelFormat = pixFormat;
+		stream->size = format.size;
+		adjusted = true;
+	}
+
+	if (stream->colorSpace != format.colorSpace) {
+		stream->colorSpace = format.colorSpace;
+		adjusted = true;
+		LOG(RPI, Debug)
+			<< "Color space changed from "
+			<< ColorSpace::toString(stream->colorSpace) << " to "
+			<< ColorSpace::toString(format.colorSpace);
+	}
+
+	stream->stride = format.planes[0].bpl;
+	stream->frameSize = format.planes[0].size;
+
+	return adjusted;
+}
+
+/*
+ * Populate and return a video device format using a StreamConfiguration. */
+V4L2DeviceFormat PipelineHandlerBase::toV4L2DeviceFormat(const V4L2VideoDevice *dev,
+							 const StreamConfiguration *stream)
+{
+	V4L2DeviceFormat deviceFormat;
+
+	const PixelFormatInfo &info = PixelFormatInfo::info(stream->pixelFormat);
+	deviceFormat.planesCount = info.numPlanes();
+	deviceFormat.fourcc = dev->toV4L2PixelFormat(stream->pixelFormat);
+	deviceFormat.size = stream->size;
+	deviceFormat.colorSpace = stream->colorSpace;
+
+	return deviceFormat;
+}
+
 V4L2DeviceFormat PipelineHandlerBase::toV4L2DeviceFormat(const V4L2VideoDevice *dev,
 							 const V4L2SubdeviceFormat &format,
 							 BayerFormat::Packing packingReq)
diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.h b/src/libcamera/pipeline/rpi/common/pipeline_base.h
index 1a3a13dbb6cf..491c5e98c4a1 100644
--- a/src/libcamera/pipeline/rpi/common/pipeline_base.h
+++ b/src/libcamera/pipeline/rpi/common/pipeline_base.h
@@ -198,6 +198,10 @@  public:
 	static bool isYuv(const PixelFormat &pixFmt);
 	static bool isRaw(const PixelFormat &pixFmt);
 
+	static bool updateStreamConfig(StreamConfiguration *stream,
+				       const V4L2DeviceFormat &format);
+	static V4L2DeviceFormat toV4L2DeviceFormat(const V4L2VideoDevice *dev,
+						   const StreamConfiguration *stream);
 	static V4L2DeviceFormat toV4L2DeviceFormat(const V4L2VideoDevice *dev,
 						   const V4L2SubdeviceFormat &format,
 						   BayerFormat::Packing packingReq);