[libcamera-devel,v2,10/14] ipa: raspberrypi: Handle autofocus algorithm results
diff mbox series

Message ID 20230123154934.27803-11-naush@raspberrypi.com
State Accepted
Commit 8418473c5178c112d279d7b9ffab3a61f6306693
Headers show
Series
  • Raspberry Pi: Camera Module 3 support
Related show

Commit Message

Naushir Patuck Jan. 23, 2023, 3:49 p.m. UTC
From: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>

Handle the results returned from the autofocus algorithm by updating lens
actuator position to the desired value. Update the Request metadata with the
algorithm status to provide back to the application.

Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
Reviewed-by: David Plowman <david.plowman@raspberrypi.com>
---
 src/ipa/raspberrypi/raspberrypi.cpp | 48 +++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

Patch
diff mbox series

diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp
index 0eb55ed605b6..fbace14b1afe 100644
--- a/src/ipa/raspberrypi/raspberrypi.cpp
+++ b/src/ipa/raspberrypi/raspberrypi.cpp
@@ -32,6 +32,7 @@ 
 #include "libcamera/internal/mapped_framebuffer.h"
 
 #include "af_algorithm.h"
+#include "af_status.h"
 #include "agc_algorithm.h"
 #include "agc_status.h"
 #include "alsc_status.h"
@@ -164,6 +165,7 @@  private:
 	void applySharpen(const struct SharpenStatus *sharpenStatus, ControlList &ctrls);
 	void applyDPC(const struct DpcStatus *dpcStatus, ControlList &ctrls);
 	void applyLS(const struct AlscStatus *lsStatus, ControlList &ctrls);
+	void applyAF(const struct AfStatus *afStatus, ControlList &lensCtrls);
 	void resampleTable(uint16_t dest[], double const src[12][16], int destW, int destH);
 
 	std::map<unsigned int, MappedFrameBuffer> buffers_;
@@ -629,6 +631,36 @@  void IPARPi::reportMetadata(unsigned int ipaContext)
 			m[i] = ccmStatus->matrix[i];
 		libcameraMetadata_.set(controls::ColourCorrectionMatrix, m);
 	}
+
+	const AfStatus *afStatus = rpiMetadata.getLocked<AfStatus>("af.status");
+	if (afStatus) {
+		int32_t s, p;
+		switch (afStatus->state) {
+		case AfState::Scanning:
+			s = controls::AfStateScanning;
+			break;
+		case AfState::Focused:
+			s = controls::AfStateFocused;
+			break;
+		case AfState::Failed:
+			s = controls::AfStateFailed;
+			break;
+		default:
+			s = controls::AfStateIdle;
+		}
+		switch (afStatus->pauseState) {
+		case AfPauseState::Pausing:
+			p = controls::AfPauseStatePausing;
+			break;
+		case AfPauseState::Paused:
+			p = controls::AfPauseStatePaused;
+			break;
+		default:
+			p = controls::AfPauseStateRunning;
+		}
+		libcameraMetadata_.set(controls::AfState, s);
+		libcameraMetadata_.set(controls::AfPauseState, p);
+	}
 }
 
 bool IPARPi::validateSensorControls()
@@ -1294,6 +1326,14 @@  void IPARPi::prepareISP(const ISPConfig &data)
 	if (dpcStatus)
 		applyDPC(dpcStatus, ctrls);
 
+	const AfStatus *afStatus = rpiMetadata.getLocked<AfStatus>("af.status");
+	if (afStatus) {
+		ControlList lensctrls(lensCtrls_);
+		applyAF(afStatus, lensctrls);
+		if (!lensctrls.empty())
+			setLensControls.emit(lensctrls);
+	}
+
 	if (!ctrls.empty())
 		setIspControls.emit(ctrls);
 }
@@ -1619,6 +1659,14 @@  void IPARPi::applyLS(const struct AlscStatus *lsStatus, ControlList &ctrls)
 	ctrls.set(V4L2_CID_USER_BCM2835_ISP_LENS_SHADING, c);
 }
 
+void IPARPi::applyAF(const struct AfStatus *afStatus, ControlList &lensCtrls)
+{
+	if (afStatus->lensSetting) {
+		ControlValue v(afStatus->lensSetting.value());
+		lensCtrls.set(V4L2_CID_FOCUS_ABSOLUTE, v);
+	}
+}
+
 /*
  * Resamples a 16x12 table with central sampling to destW x destH with corner
  * sampling.