[v1,5/5] pipeline: rpi: Add an example additional pipeline "stage"
diff mbox series

Message ID 20261203130113.47873-6-david.plowman@raspberrypi.com
State New
Headers show
Series
  • Extra pipeline processing "stages" for Raspberry Pi
Related show

Commit Message

David Plowman Dec. 2, 2026, 4:13 p.m. UTC
Add an example stage, consisting of both the IPA that controls it, and
the stage itself running under the pipeline handler. The example stage
shows how these, and any associated mojom parameters, need to be
plumbed into the code, which is the pattern other future "stages"
should follow.

This example stage does nothing useful itself, but we leave it here as
the canonical example of adding new stages. The only thing missing in
order to enable and run it, is to add the control algorithm
("rpi.example_stage") to the JSON tuning file.

Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
---
 include/libcamera/ipa/raspberrypi.mojom       | 22 +++++--
 src/ipa/rpi/common/ipa_base.cpp               | 17 ++++-
 src/ipa/rpi/controller/example_stage_status.h | 20 ++++++
 src/ipa/rpi/controller/meson.build            |  1 +
 src/ipa/rpi/controller/rpi/example_stage.cpp  | 63 ++++++++++++++++++
 src/ipa/rpi/controller/rpi/example_stage.h    | 33 ++++++++++
 src/libcamera/pipeline/rpi/common/meson.build |  1 +
 .../pipeline/rpi/common/pipeline_base.h       |  8 +--
 .../rpi/common/stages/example_stage.cpp       | 66 +++++++++++++++++++
 .../rpi/common/stages/example_stage.h         | 35 ++++++++++
 10 files changed, 255 insertions(+), 11 deletions(-)
 create mode 100644 src/ipa/rpi/controller/example_stage_status.h
 create mode 100644 src/ipa/rpi/controller/rpi/example_stage.cpp
 create mode 100644 src/ipa/rpi/controller/rpi/example_stage.h
 create mode 100644 src/libcamera/pipeline/rpi/common/stages/example_stage.cpp
 create mode 100644 src/libcamera/pipeline/rpi/common/stages/example_stage.h

Patch
diff mbox series

diff --git a/include/libcamera/ipa/raspberrypi.mojom b/include/libcamera/ipa/raspberrypi.mojom
index eaeb8398..574f3dfc 100644
--- a/include/libcamera/ipa/raspberrypi.mojom
+++ b/include/libcamera/ipa/raspberrypi.mojom
@@ -46,20 +46,30 @@  struct ConfigParams {
 	libcamera.SharedFD lsTableHandle;
 };
 
+/* Configuration for the example pipeline stage. */
+struct ExampleStageConfig {
+	bool enabled;
+	uint32 delayMs; /* sleep this long on every frame */
+};
+
+/* Per-frame parameters for the example pipeline stage. */
+struct ExampleStageParams {
+	uint32 frame;
+	float analogueGain;
+};
+
 /*
  * Configuration of the pipeline handler "stages", which operate on the raw
- * Bayer buffers after prepareIsp() and before the ISP proper. There will be
- * one structure per stage type here.
+ * Bayer buffers after prepareIsp() and before the ISP proper. There is one
+ * structure per stage type here.
  */
 struct StageConfig {
-	/* Placeholder - remove once we have a stage. */
-	bool unused;
+	ExampleStageConfig example;
 };
 
 /* Per-frame parameters for the pipeline handler stages, one structure per stage. */
 struct StageParams {
-	/* Placeholder - remove once we have a stage. */
-	bool unused;
+	ExampleStageParams example;
 };
 
 struct ConfigResult {
diff --git a/src/ipa/rpi/common/ipa_base.cpp b/src/ipa/rpi/common/ipa_base.cpp
index 7726a768..a131fb72 100644
--- a/src/ipa/rpi/common/ipa_base.cpp
+++ b/src/ipa/rpi/common/ipa_base.cpp
@@ -25,6 +25,7 @@ 
 #include "controller/ccm_status.h"
 #include "controller/contrast_algorithm.h"
 #include "controller/denoise_algorithm.h"
+#include "controller/example_stage_status.h"
 #include "controller/hdr_algorithm.h"
 #include "controller/lux_status.h"
 #include "controller/sharpen_algorithm.h"
@@ -529,13 +530,27 @@  void IpaBase::prepareIsp(const PrepareParams &params)
 /* Fill in the configuration for the pipeline handler stages. */
 void IpaBase::fillStageConfig(StageConfig &config)
 {
+	RPiController::Metadata &globalMetadata = controller_.getGlobalMetadata();
+
 	config = {};
+
+	ExampleStageTuning exampleTuning;
+	if (globalMetadata.get("example_stage.config", exampleTuning) == 0) {
+		config.example.enabled = true;
+		config.example.delayMs = exampleTuning.delayMs;
+	}
 }
 
 /* Fill in the parameters for the pipeline handler stages for this frame. */
-void IpaBase::prepareStageParams([[maybe_unused]] const RPiController::Metadata &rpiMetadata)
+void IpaBase::prepareStageParams(const RPiController::Metadata &rpiMetadata)
 {
 	lastStageParams_ = {};
+
+	ExampleStageStatus exampleStatus;
+	if (rpiMetadata.get("example_stage.status", exampleStatus) == 0) {
+		lastStageParams_.example.frame = exampleStatus.frame;
+		lastStageParams_.example.analogueGain = exampleStatus.analogueGain;
+	}
 }
 
 /*
diff --git a/src/ipa/rpi/controller/example_stage_status.h b/src/ipa/rpi/controller/example_stage_status.h
new file mode 100644
index 00000000..43b25649
--- /dev/null
+++ b/src/ipa/rpi/controller/example_stage_status.h
@@ -0,0 +1,20 @@ 
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (C) 2026, Raspberry Pi Ltd
+ *
+ * Example pipeline stage algorithm status
+ */
+#pragma once
+
+#include <stdint.h>
+
+/* The example stage algorithm controls the example pipeline handler stage. */
+
+struct ExampleStageTuning {
+	uint32_t delayMs; /* sleep this long on every frame */
+};
+
+struct ExampleStageStatus {
+	uint32_t frame;
+	double analogueGain;
+};
diff --git a/src/ipa/rpi/controller/meson.build b/src/ipa/rpi/controller/meson.build
index 03ee7c20..35156245 100644
--- a/src/ipa/rpi/controller/meson.build
+++ b/src/ipa/rpi/controller/meson.build
@@ -18,6 +18,7 @@  rpi_ipa_controller_sources = files([
     'rpi/decompand.cpp',
     'rpi/denoise.cpp',
     'rpi/dpc.cpp',
+    'rpi/example_stage.cpp',
     'rpi/geq.cpp',
     'rpi/hdr.cpp',
     'rpi/lux.cpp',
diff --git a/src/ipa/rpi/controller/rpi/example_stage.cpp b/src/ipa/rpi/controller/rpi/example_stage.cpp
new file mode 100644
index 00000000..b1adad1e
--- /dev/null
+++ b/src/ipa/rpi/controller/rpi/example_stage.cpp
@@ -0,0 +1,63 @@ 
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (C) 2026, Raspberry Pi Ltd
+ *
+ * Example pipeline stage algorithm
+ */
+
+#include "example_stage.h"
+
+#include <libcamera/base/log.h>
+
+#include "../device_status.h"
+
+using namespace RPiController;
+using namespace libcamera;
+
+LOG_DEFINE_CATEGORY(RPiExampleStage)
+
+#define NAME "rpi.example_stage"
+
+ExampleStage::ExampleStage(Controller *controller)
+	: Algorithm(controller), tuning_{}, frame_(0)
+{
+}
+
+char const *ExampleStage::name() const
+{
+	return NAME;
+}
+
+int ExampleStage::read(const libcamera::ValueNode &params)
+{
+	tuning_.delayMs = params["delay_ms"].get<uint32_t>(0);
+
+	return 0;
+}
+
+void ExampleStage::initialise()
+{
+	/* Send the config data to the pipeline handler early. */
+	getGlobalMetadata().set("example_stage.config", tuning_);
+}
+
+void ExampleStage::prepare(Metadata *imageMetadata)
+{
+	ExampleStageStatus status{};
+	status.frame = frame_++;
+
+	DeviceStatus deviceStatus;
+	if (imageMetadata->get("device.status", deviceStatus) == 0)
+		status.analogueGain = deviceStatus.analogueGain;
+	else
+		LOG(RPiExampleStage, Warning) << "No device metadata";
+
+	/* Per-frame stage parameters. */
+	imageMetadata->set("example_stage.status", status);
+}
+
+static Algorithm *create(Controller *controller)
+{
+	return (Algorithm *)new ExampleStage(controller);
+}
+static RegisterAlgorithm reg(NAME, &create);
diff --git a/src/ipa/rpi/controller/rpi/example_stage.h b/src/ipa/rpi/controller/rpi/example_stage.h
new file mode 100644
index 00000000..b446b62a
--- /dev/null
+++ b/src/ipa/rpi/controller/rpi/example_stage.h
@@ -0,0 +1,33 @@ 
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (C) 2026, Raspberry Pi Ltd
+ *
+ * Example pipeline stage algorithm
+ */
+#pragma once
+
+#include "../algorithm.h"
+#include "../example_stage_status.h"
+
+/*
+ * This algorithm drives the example pipeline handler stage. It does nothing
+ * useful, but shows how to plumb in a new pipeline handler stage.
+ */
+
+namespace RPiController {
+
+class ExampleStage : public Algorithm
+{
+public:
+	ExampleStage(Controller *controller);
+	char const *name() const override;
+	int read(const libcamera::ValueNode &params) override;
+	void initialise() override;
+	void prepare(Metadata *imageMetadata) override;
+
+private:
+	ExampleStageTuning tuning_;
+	uint32_t frame_;
+};
+
+} /* namespace RPiController */
diff --git a/src/libcamera/pipeline/rpi/common/meson.build b/src/libcamera/pipeline/rpi/common/meson.build
index 5411ae84..d3281a49 100644
--- a/src/libcamera/pipeline/rpi/common/meson.build
+++ b/src/libcamera/pipeline/rpi/common/meson.build
@@ -5,4 +5,5 @@  libcamera_internal_sources += files([
     'pipeline_base.cpp',
     'rpi_stream.cpp',
     'stage_runner.cpp',
+    'stages/example_stage.cpp',
 ])
diff --git a/src/libcamera/pipeline/rpi/common/pipeline_base.h b/src/libcamera/pipeline/rpi/common/pipeline_base.h
index 408a4b58..f744b329 100644
--- a/src/libcamera/pipeline/rpi/common/pipeline_base.h
+++ b/src/libcamera/pipeline/rpi/common/pipeline_base.h
@@ -31,6 +31,8 @@ 
 #include <libcamera/ipa/raspberrypi_ipa_interface.h>
 #include <libcamera/ipa/raspberrypi_ipa_proxy.h>
 
+#include "stages/example_stage.h"
+
 #include "delayed_controls.h"
 #include "rpi_stream.h"
 #include "stage.h"
@@ -55,10 +57,8 @@  public:
 	{
 		stageRunner_.stagesComplete.connect(this, &CameraData::stagesComplete);
 
-		/*
-		 * Stages that can run on every platform would be added here:
-		 * e.g. addStage(std::make_unique<ExampleStage>());
-		 */
+		/* Stages that can run on every platform are added here. */
+		addStage(std::make_unique<ExampleStage>());
 	}
 
 	virtual ~CameraData()
diff --git a/src/libcamera/pipeline/rpi/common/stages/example_stage.cpp b/src/libcamera/pipeline/rpi/common/stages/example_stage.cpp
new file mode 100644
index 00000000..330e9c7c
--- /dev/null
+++ b/src/libcamera/pipeline/rpi/common/stages/example_stage.cpp
@@ -0,0 +1,66 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2026, Raspberry Pi Ltd
+ *
+ * Example pipeline handler stage
+ *
+ * Add
+ *        {
+ *            "rpi.example_stage":
+ *            {
+ *            }
+ *        }
+ * to the tuning file algorithms to run this stage.
+ */
+
+#include "example_stage.h"
+
+#include <chrono>
+#include <thread>
+
+#include <libcamera/base/log.h>
+
+#include "libcamera/internal/dma_buf_allocator.h"
+
+namespace libcamera {
+
+LOG_DEFINE_CATEGORY(RPiExampleStage)
+
+namespace RPi {
+
+int ExampleStage::configure(const ipa::RPi::StageConfig &config, const StageFormat &format)
+{
+	config_ = config.example;
+
+	if (!config_.enabled)
+		return 0;
+
+	LOG(RPiExampleStage, Info)
+		<< "Configured: delay " << config_.delayMs << "ms, raw format "
+		<< format.pixelFormat << " " << format.size << " stride " << format.stride;
+
+	return 1;
+}
+
+void ExampleStage::run(StageJob &job)
+{
+	const ipa::RPi::ExampleStageParams &params = job.params.example;
+
+	/* Print out the first Bayer quad as 16-bit values, regardless of the format. */
+	DmaSyncer sync(job.bayerFd, DmaSyncer::SyncType::Read);
+
+	const uint8_t *data = job.bayerData.data(); /* assume large enough! */
+	const uint16_t *row0 = reinterpret_cast<const uint16_t *>(data);
+	const uint16_t *row1 = reinterpret_cast<const uint16_t *>(data + job.bayerFormat.stride);
+
+	LOG(RPiExampleStage, Info)
+		<< "Frame " << params.frame << ", analogue gain " << params.analogueGain
+		<< ", first quad " << row0[0] << " " << row0[1] << " "
+		<< row1[0] << " " << row1[1];
+
+	std::this_thread::sleep_for(std::chrono::milliseconds(config_.delayMs));
+}
+
+} /* namespace RPi */
+
+} /* namespace libcamera */
diff --git a/src/libcamera/pipeline/rpi/common/stages/example_stage.h b/src/libcamera/pipeline/rpi/common/stages/example_stage.h
new file mode 100644
index 00000000..d5b55bc5
--- /dev/null
+++ b/src/libcamera/pipeline/rpi/common/stages/example_stage.h
@@ -0,0 +1,35 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2026, Raspberry Pi Ltd
+ *
+ * Example pipeline handler stage
+ */
+
+#pragma once
+
+#include "../stage.h"
+
+namespace libcamera {
+
+namespace RPi {
+
+/*
+ * An example stage that does nothing useful. For every frame it logs the
+ * parameters it was given and the first few raw pixel values, and can add a
+ * delay, just to mix up the timings.
+ */
+class ExampleStage : public Stage
+{
+public:
+	const char *name() const override { return "example"; }
+
+	int configure(const ipa::RPi::StageConfig &config, const StageFormat &format) override;
+	void run(StageJob &job) override;
+
+private:
+	ipa::RPi::ExampleStageConfig config_;
+};
+
+} /* namespace RPi */
+
+} /* namespace libcamera */