summaryrefslogtreecommitdiff
path: root/src/ipa/rkisp1/algorithms/af.cpp
blob: 65768fc45e5bfcd680e1990286f50a351370e1fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2022, Theobroma Systems
 *
 * af.cpp - RkISP1 AF hill climbing based control algorithm
 */

#include "af.h"

/**
 * \file af.h
 */

namespace libcamera::ipa::rkisp1::algorithms {

/**
 * \class Af
 * \brief AF control algorithm
 */

LOG_DEFINE_CATEGORY(RkISP1Af)

namespace {

constexpr rkisp1_cif_isp_window rectangleToIspWindow(const Rectangle &rectangle)
{
	return rkisp1_cif_isp_window{
		.h_offs = static_cast<uint16_t>(rectangle.x),
		.v_offs = static_cast<uint16_t>(rectangle.y),
		.h_size = static_cast<uint16_t>(rectangle.width),
		.v_size = static_cast<uint16_t>(rectangle.height)
	};
}

} /* namespace */

/**
 * \copydoc libcamera::ipa::Algorithm::init
 */
int Af::init([[maybe_unused]] IPAContext &context, const YamlObject &tuningData)
{
	waitFramesLens_ = tuningData["wait-frames-lens"].get<uint32_t>(1);
	ispThreshold_ = tuningData["isp-threshold"].get<uint32_t>(128);
	ispVarShift_ = tuningData["isp-var-shift"].get<uint32_t>(4);

	LOG(RkISP1Af, Debug) << "waitFramesLens_: " << waitFramesLens_
			     << ", ispThreshold_: " << ispThreshold_
			     << ", ispVarShift_: " << ispVarShift_;

	return initBase(tuningData);
}

/**
 * \copydoc libcamera::ipa::Algorithm::configure
 */
int Af::configure([[maybe_unused]] IPAContext &context,
		  const IPACameraSensorInfo &configInfo)
{
	/* Default AF window of 3/4 size of the screen placed at the center */
	defaultWindow_ = Rectangle(configInfo.outputSize.width / 8,
				   configInfo.outputSize.height / 8,
				   3 * configInfo.outputSize.width / 4,
				   3 * configInfo.outputSize.height / 4);
	updateCurrentWindow(defaultWindow_);

	return 0;
}

/**
 * \copydoc libcamera::ipa::Algorithm::queueRequest
 */
void Af::queueRequest([[maybe_unused]] IPAContext &context,
		      const uint32_t frame,
		      [[maybe_unused]] IPAFrameContext &frameContext,
		      const ControlList &controls)
{
	queueRequestBase(frame, controls);

	for (auto const &[id, value] : controls) {
		switch (id) {
		case controls::AF_METERING: {
			setMeteringMode(static_cast<controls::AfMeteringEnum>(value.get<int32_t>()));
			break;
		}
		case controls::AF_WINDOWS: {
			setWindows(value.get<Span<const Rectangle>>());
			break;
		}
		default:
			break;
		}
	}
}

/**
 * \copydoc libcamera::ipa::Algorithm::prepare
 */
void Af::prepare([[maybe_unused]] IPAContext &context,
		 [[maybe_unused]] const uint32_t frame,
		 [[maybe_unused]] IPAFrameContext &frameContext,
		 rkisp1_params_cfg *params)
{
	if (updateWindow_) {
		params->meas.afc_config.num_afm_win = 1;
		params->meas.afc_config.thres = ispThreshold_;
		params->meas.afc_config.var_shift = ispVarShift_;
		params->meas.afc_config.afm_win[0] = rectangleToIspWindow(*updateWindow_);

		params->module_cfg_update |= RKISP1_CIF_ISP_MODULE_AFC;
		params->module_ens |= RKISP1_CIF_ISP_MODULE_AFC;
		params->module_en_update |= RKISP1_CIF_ISP_MODULE_AFC;

		updateWindow_.reset();

		/* Wait one frame for the ISP to apply changes */
		setFramesToSkip(1);
	}
}

/**
 * \copydoc libcamera::ipa::Algorithm::process
 */
void Af::process(IPAContext &context, [[maybe_unused]] const uint32_t frame,
		 [[maybe_unused]] IPAFrameContext &frameContext,
		 const rkisp1_stat_buffer *stats,
		 [[maybe_unused]] ControlList &metadata)
{
	uint32_t sharpness = stats->params.af.window[0].sum;
	uint32_t luminance = stats->params.af.window[0].lum;

	LOG(RkISP1Af, Debug) << "lensPosition: " << context.activeState.af.lensPosition
			     << ", Sharpness: " << sharpness
			     << ", Luminance: " << luminance;

	uint32_t lensPosition = processAutofocus(sharpness);

	if (lensPosition != context.activeState.af.lensPosition) {
		context.activeState.af.lensPosition = lensPosition;
		context.activeState.af.applyLensCtrls = true;
		setFramesToSkip(waitFramesLens_);
	}
}

void Af::setMeteringMode([[maybe_unused]] controls::AfMeteringEnum metering)
{
	if (metering == meteringMode_)
		return;

	if (metering == controls::AfMeteringWindows) {
		updateCurrentWindow(userWindow_);
	} else {
		updateCurrentWindow(defaultWindow_);
	}

	meteringMode_ = metering;
}

void Af::setWindows(Span<const Rectangle> windows)
{
	if (windows.size() != 1) {
		LOG(RkISP1Af, Error) << "Only one AF window is supported";
		return;
	}

	/* \todo Check if window size is valid for ISP */

	LOG(RkISP1Af, Debug) << "setWindows: " << userWindow_;

	userWindow_ = windows[0];

	if (meteringMode_ == controls::AfMeteringWindows)
		updateCurrentWindow(userWindow_);
}

void Af::updateCurrentWindow(const Rectangle &window)
{
	updateWindow_ = window;
}

REGISTER_IPA_ALGORITHM(Af, "Af")

} /* namespace libcamera::ipa::rkisp1::algorithms */