summaryrefslogtreecommitdiff
path: root/src/ipa/libipa/algorithms/af_hill_climbing.cpp
blob: db06852e6fda9d2a54340f2e2d2c23cd8e505e33 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2021, Red Hat
 * Copyright (C) 2022, Ideas On Board
 * Copyright (C) 2022, Theobroma Systems
 *
 * af_hill_climbing.cpp - AF Hill Climbing common algorithm
 */

#include "af_hill_climbing.h"

#include "libcamera/internal/yaml_parser.h"

/**
 * \file af_hill_climbing.h
 * \brief AF Hill Climbing common algorithm
 */

namespace libcamera::ipa::common::algorithms {

LOG_DEFINE_CATEGORY(Af)

/**
 * \class AfHillClimbing
 * \brief The base class implementing hill climbing AF control algorithm
 * \tparam Module The IPA module type for this class of algorithms
 *
 * Control part of auto focus algorithm. It calculates the lens position basing
 * on contrast measure supplied by the higher level. This way it is independent
 * from the platform.
 *
 * Derived class should call processAutofocus() for each measured contrast value
 * and set the lens to the calculated position.
 */

/**
 * \brief Initialize the Algorithm with tuning data
 * \param[in] tuningData The tuning data for the algorithm
 *
 * This function should be called in the init() function of the derived class.
 * See alse: libcamera::ipa::Algorithm::init()
 *
 * \return 0 if successful, an error code otherwise
 */
int AfHillClimbing::initBase(const YamlObject &tuningData)
{
	minVcmPosition_ = tuningData["min-vcm-position"].get<uint32_t>(0);
	maxVcmPosition_ = tuningData["max-vcm-position"].get<uint32_t>(100);
	coarseSearchStep_ = tuningData["coarse-search-step"].get<uint32_t>(30);
	fineSearchStep_ = tuningData["fine-search-step"].get<uint32_t>(1);
	fineRange_ = tuningData["fine-scan-range"].get<double>(0.05);
	maxChange_ = tuningData["max-variance-change"].get<double>(0.5);

	LOG(Af, Debug) << "minVcmPosition_: " << minVcmPosition_
		       << ", maxVcmPosition_: " << maxVcmPosition_
		       << ", coarseSearchStep_: " << coarseSearchStep_
		       << ", fineSearchStep_: " << fineSearchStep_
		       << ", fineRange_: " << fineRange_
		       << ", maxChange_: " << maxChange_;

	setMode(controls::AfModeContinuous);

	return 0;
}

/**
 * \brief Provide control values to the algorithm
 * \param[in] frame The frame number to apply the control values
 * \param[in] controls The list of user controls
 *
 * This function should be called in the queueRequest() function of the derived class.
 * See alse: libcamera::ipa::Algorithm::queueRequest()
 */
void AfHillClimbing::queueRequestBase([[maybe_unused]] const uint32_t frame, const ControlList &controls)
{
	for (auto const &[id, value] : controls) {
		switch (id) {
		case controls::AF_MODE: {
			setMode(static_cast<controls::AfModeEnum>(value.get<int32_t>()));
			break;
		}
		case controls::AF_TRIGGER: {
			setTrigger(static_cast<controls::AfTriggerEnum>(value.get<int32_t>()));
			break;
		}
		case controls::AF_PAUSE: {
			setPause(static_cast<controls::AfPauseEnum>(value.get<int32_t>()));
			break;
		}
		case controls::LENS_POSITION: {
			setLensPosition(value.get<float>());
			break;
		}
		default:
			break;
		}
	}
}

/**
 * \brief Run the auto focus algorithm loop
 * \param[in] currentContrast New value of contrast measured for current frame
 *
 * This method should be called for each new contrast value that was measured,
 * usually in the process() method.
 *
 * \return New lens position calculated by AF algorithm
 */
uint32_t AfHillClimbing::processAutofocus(double currentContrast)
{
	currentContrast_ = currentContrast;

	if (shouldSkipFrame())
		return lensPosition_;

	switch (mode_) {
	case controls::AfModeManual:
		/* Nothing to process. */
		break;
	case controls::AfModeAuto:
		processAutoMode();
		break;
	case controls::AfModeContinuous:
		processContinousMode();
		break;
	default:
		break;
	}

	return lensPosition_;
}

void AfHillClimbing::processAutoMode()
{
	if (state_ == controls::AfStateScanning) {
		afCoarseScan();
		afFineScan();
	}
}

void AfHillClimbing::processContinousMode()
{
	/* If we are in a paused state, we won't process the stats */
	if (pauseState_ == controls::AfPauseStatePaused)
		return;

	if (state_ == controls::AfStateScanning) {
		afCoarseScan();
		afFineScan();
		return;
	}

	/* We can re-start the scan at any moment in AfModeContinuous */
	if (afIsOutOfFocus()) {
		afReset();
	}
}

/**
 * \brief Request AF to skip n frames
 * \param[in] n Number of frames to be skipped
 *
 * Requested number of frames will not be used for AF calculation.
 */
void AfHillClimbing::setFramesToSkip(uint32_t n)
{
	if (n > framesToSkip_)
		framesToSkip_ = n;
}

/**
 * \copydoc libcamera::ipa::common::algorithms::AfInterface::setMode
 */
void AfHillClimbing::setMode(controls::AfModeEnum mode)
{
	if (mode == mode_)
		return;

	LOG(Af, Debug) << "Switched AF mode from " << mode_ << " to " << mode;
	mode_ = mode;

	state_ = controls::AfStateIdle;
	pauseState_ = controls::AfPauseStateRunning;

	if (mode_ == controls::AfModeContinuous)
		afReset();
}

/**
 * \copydoc libcamera::ipa::common::algorithms::AfInterface::setRange
 */
void AfHillClimbing::setRange([[maybe_unused]] controls::AfRangeEnum range)
{
	LOG(Af, Error) << __FUNCTION__ << " not implemented!";
}

/**
 * \copydoc libcamera::ipa::common::algorithms::AfInterface::setSpeed
 */
void AfHillClimbing::setSpeed([[maybe_unused]] controls::AfSpeedEnum speed)
{
	LOG(Af, Error) << __FUNCTION__ << " not implemented!";
}

/**
 * \copydoc libcamera::ipa::common::algorithms::AfInterface::setTrigger
 */
void AfHillClimbing::setTrigger(controls::AfTriggerEnum trigger)
{
	if (mode_ != controls::AfModeAuto) {
		LOG(Af, Warning) << __FUNCTION__ << " not possible in mode " << mode_;
		return;
	}

	LOG(Af, Debug) << "Trigger called with " << trigger;

	if (trigger == controls::AfTriggerStart)
		afReset();
	else
		state_ = controls::AfStateIdle;
}

/**
 * \copydoc libcamera::ipa::common::algorithms::AfInterface::setPause
 */
void AfHillClimbing::setPause(controls::AfPauseEnum pause)
{
	if (mode_ != controls::AfModeContinuous) {
		LOG(Af, Warning) << __FUNCTION__ << " not possible in mode " << mode_;
		return;
	}

	switch (pause) {
	case controls::AfPauseImmediate:
		pauseState_ = controls::AfPauseStatePaused;
		break;
	case controls::AfPauseDeferred:
		/* \todo: add the AfPauseDeferred mode */
		LOG(Af, Warning) << "AfPauseDeferred is not supported!";
		break;
	case controls::AfPauseResume:
		pauseState_ = controls::AfPauseStateRunning;
		break;
	default:
		break;
	}
}

/**
 * \copydoc libcamera::ipa::common::algorithms::AfInterface::setLensPosition
 */
void AfHillClimbing::setLensPosition(float lensPosition)
{
	if (mode_ != controls::AfModeManual) {
		LOG(Af, Warning) << __FUNCTION__ << " not possible in mode " << mode_;
		return;
	}

	lensPosition_ = static_cast<uint32_t>(lensPosition);

	LOG(Af, Debug) << "Requesting lens position " << lensPosition_;
}

/**
 * \fn AfHillClimbing::setMeteringMode()
 * \copydoc libcamera::ipa::common::algorithms::AfInterface::setMeteringMode
 */

/**
 * \fn AfHillClimbing::setWindows()
 * \copydoc libcamera::ipa::common::algorithms::AfInterface::setWindows
 */

void AfHillClimbing::afCoarseScan()
{
	if (coarseCompleted_)
		return;

	if (afScan(coarseSearchStep_)) {
		coarseCompleted_ = true;
		maxContrast_ = 0;
		lensPosition_ = lensPosition_ - (lensPosition_ * fineRange_);
		previousContrast_ = 0;
		maxStep_ = std::clamp(lensPosition_ + static_cast<uint32_t>((lensPosition_ * fineRange_)),
				      0U, maxVcmPosition_);
	}
}

void AfHillClimbing::afFineScan()
{
	if (!coarseCompleted_)
		return;

	if (afScan(fineSearchStep_)) {
		LOG(Af, Debug) << "AF found the best focus position!";
		state_ = controls::AfStateFocused;
		fineCompleted_ = true;
	}
}

bool AfHillClimbing::afScan(uint32_t minSteps)
{
	if (lensPosition_ + minSteps > maxStep_) {
		/* If the max step is reached, move lens to the position. */
		lensPosition_ = bestPosition_;
		return true;
	} else {
		/*
		* Find the maximum of the variance by estimating its
		* derivative. If the direction changes, it means we have passed
		* a maximum one step before.
		*/
		if ((currentContrast_ - maxContrast_) >= -(maxContrast_ * 0.1)) {
			/*
			* Positive and zero derivative:
			* The variance is still increasing. The focus could be
			* increased for the next comparison. Also, the max
			* variance and previous focus value are updated.
			*/
			bestPosition_ = lensPosition_;
			lensPosition_ += minSteps;
			maxContrast_ = currentContrast_;
		} else {
			/*
			* Negative derivative:
			* The variance starts to decrease which means the maximum
			* variance is found. Set focus step to previous good one
			* then return immediately.
			*/
			lensPosition_ = bestPosition_;
			return true;
		}
	}

	previousContrast_ = currentContrast_;
	LOG(Af, Debug) << "Previous step is " << bestPosition_
		       << ", Current step is " << lensPosition_;
	return false;
}

void AfHillClimbing::afReset()
{
	LOG(Af, Debug) << "Reset AF parameters";
	lensPosition_ = minVcmPosition_;
	maxStep_ = maxVcmPosition_;
	state_ = controls::AfStateScanning;
	previousContrast_ = 0.0;
	coarseCompleted_ = false;
	fineCompleted_ = false;
	maxContrast_ = 0.0;
	setFramesToSkip(1);
}

bool AfHillClimbing::afIsOutOfFocus()
{
	const uint32_t diff_var = std::abs(currentContrast_ - maxContrast_);
	const double var_ratio = diff_var / maxContrast_;
	LOG(Af, Debug) << "Variance change rate: " << var_ratio
		       << ", Current VCM step: " << lensPosition_;
	if (var_ratio > maxChange_)
		return true;
	else
		return false;
}

bool AfHillClimbing::shouldSkipFrame()
{
	if (framesToSkip_ > 0) {
		framesToSkip_--;
		return true;
	}

	return false;
}

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