[libcamera-devel,v5,13/13] apps: cam: Add option to configure sensor
diff mbox series

Message ID 20230921165550.50956-14-jacopo.mondi@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: Introduce SensorConfiguration
Related show

Commit Message

Jacopo Mondi Sept. 21, 2023, 4:55 p.m. UTC
Add a '-f|--sensor_format' option to cam to allow forcing a sensor
configuration from the command line.

As an example:
cam -c1 -C -S --stream pixelformat=YUYV -f width=3840,height=2160,bitDepth=10

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 src/apps/cam/camera_session.cpp    |  7 +++++
 src/apps/cam/main.cpp              |  4 +++
 src/apps/cam/main.h                |  1 +
 src/apps/common/stream_options.cpp | 42 ++++++++++++++++++++++++++++++
 src/apps/common/stream_options.h   |  8 ++++++
 5 files changed, 62 insertions(+)

Patch
diff mbox series

diff --git a/src/apps/cam/camera_session.cpp b/src/apps/cam/camera_session.cpp
index 066e397b5f47..4e01ebdc3e90 100644
--- a/src/apps/cam/camera_session.cpp
+++ b/src/apps/cam/camera_session.cpp
@@ -72,6 +72,13 @@  CameraSession::CameraSession(CameraManager *cm,
 		return;
 	}
 
+	/* Apply a sensor configuration is requested. */
+	if (SensorKeyValueParser::updateConfiguration(config.get(),
+						      options_[OptSensorFmt])) {
+		std::cerr << "Failed to apply sensor configuration" << std::endl;
+		return;
+	}
+
 	bool strictFormats = options_.isSet(OptStrictFormats);
 
 #ifdef HAVE_KMS
diff --git a/src/apps/cam/main.cpp b/src/apps/cam/main.cpp
index 5d8a57bc14e5..d15ee875d6b8 100644
--- a/src/apps/cam/main.cpp
+++ b/src/apps/cam/main.cpp
@@ -110,6 +110,7 @@  void CamApp::quit()
 int CamApp::parseOptions(int argc, char *argv[])
 {
 	StreamKeyValueParser streamKeyValue;
+	SensorKeyValueParser sensorKeyValue;
 
 	OptionsParser parser;
 	parser.addOption(OptCamera, OptionString,
@@ -171,6 +172,9 @@  int CamApp::parseOptions(int argc, char *argv[])
 			 "Load a capture session configuration script from a file",
 			 "script", ArgumentRequired, "script", false,
 			 OptCamera);
+	parser.addOption(OptSensorFmt, &sensorKeyValue,
+			 "Apply a format to the sensor", "sensor_format", true,
+			 OptCamera);
 
 	options_ = parser.parse(argc, argv);
 	if (!options_.valid())
diff --git a/src/apps/cam/main.h b/src/apps/cam/main.h
index 526aecece3f3..1432cfeb5052 100644
--- a/src/apps/cam/main.h
+++ b/src/apps/cam/main.h
@@ -19,6 +19,7 @@  enum {
 	OptMonitor = 'm',
 	OptSDL = 'S',
 	OptStream = 's',
+	OptSensorFmt = 'f',
 	OptListControls = 256,
 	OptStrictFormats = 257,
 	OptMetadata = 258,
diff --git a/src/apps/common/stream_options.cpp b/src/apps/common/stream_options.cpp
index 663b979aa763..a61317ab7fad 100644
--- a/src/apps/common/stream_options.cpp
+++ b/src/apps/common/stream_options.cpp
@@ -119,3 +119,45 @@  std::optional<libcamera::StreamRole> StreamKeyValueParser::parseRole(const KeyVa
 
 	return {};
 }
+
+SensorKeyValueParser::SensorKeyValueParser()
+{
+	addOption("bitDepth", OptionInteger, "Sensor format bit depth",
+		  ArgumentRequired);
+	addOption("width", OptionInteger, "Sensor frame width in pixels",
+		  ArgumentRequired);
+	addOption("height", OptionInteger, "Sensor frame height in pixels",
+		  ArgumentRequired);
+}
+
+int SensorKeyValueParser::updateConfiguration(CameraConfiguration *config,
+					      const OptionValue &values)
+{
+	if (!config) {
+		std::cerr << "No configuration provided" << std::endl;
+		return -EINVAL;
+	}
+
+	/* If no configuration values nothing to do. */
+	if (values.empty())
+		return 0;
+
+	const std::vector<OptionValue> &streamParameters = values.toArray();
+	SensorConfiguration sensorConfig;
+
+	for (auto const &value : streamParameters) {
+		KeyValueParser::Options opts = value.toKeyValues();
+
+		if (opts.isSet("width") && opts.isSet("height")) {
+			sensorConfig.outputSize.width = opts["width"];
+			sensorConfig.outputSize.height = opts["height"];
+		}
+
+		if (opts.isSet("bitDepth"))
+			sensorConfig.bitDepth = opts["bitDepth"];
+	}
+
+	config->sensorConfig = sensorConfig;
+
+	return 0;
+}
diff --git a/src/apps/common/stream_options.h b/src/apps/common/stream_options.h
index a5f3bde08da8..a338206bd005 100644
--- a/src/apps/common/stream_options.h
+++ b/src/apps/common/stream_options.h
@@ -27,3 +27,11 @@  public:
 private:
 	static std::optional<libcamera::StreamRole> parseRole(const KeyValueParser::Options &options);
 };
+
+class SensorKeyValueParser : public KeyValueParser
+{
+public:
+	SensorKeyValueParser();
+	static int updateConfiguration(libcamera::CameraConfiguration *config,
+				       const OptionValue &values);
+};