summaryrefslogtreecommitdiff
path: root/src/ipa/raspberrypi/controller/rpi/sharpen.cpp
blob: 4f6f020a417b3dde09304f20ad8bf40b4e3922bd (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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi Ltd
 *
 * sharpen.cpp - sharpening control algorithm
 */

#include <math.h>

#include <libcamera/base/log.h>

#include "../sharpen_status.h"

#include "sharpen.h"

using namespace RPiController;
using namespace libcamera;

LOG_DEFINE_CATEGORY(RPiSharpen)

#define NAME "rpi.sharpen"

Sharpen::Sharpen(Controller *controller)
	: SharpenAlgorithm(controller), userStrength_(1.0)
{
}

char const *Sharpen::name() const
{
	return NAME;
}

void Sharpen::switchMode(CameraMode const &cameraMode,
			 [[maybe_unused]] Metadata *metadata)
{
	/* can't be less than one, right? */
	modeFactor_ = std::max(1.0, cameraMode.noiseFactor);
}

int Sharpen::read(const libcamera::YamlObject &params)
{
	threshold_ = params["threshold"].get<double>(1.0);
	strength_ = params["strength"].get<double>(1.0);
	limit_ = params["limit"].get<double>(1.0);
	LOG(RPiSharpen, Debug)
		<< "Read threshold " << threshold_
		<< " strength " << strength_
		<< " limit " << limit_;
	return 0;
}

void Sharpen::setStrength(double strength)
{
	/*
	 * Note that this function is how an application sets the overall
	 * sharpening "strength". We call this the "user strength" field
	 * as there already is a strength_ field - being an internal gain
	 * parameter that gets passed to the ISP control code. Negative
	 * values are not allowed - coerce them to zero (no sharpening).
	 */
	userStrength_ = std::max(0.0, strength);
}

void Sharpen::prepare(Metadata *imageMetadata)
{
	/*
	 * The userStrength_ affects the algorithm's internal gain directly, but
	 * we adjust the limit and threshold less aggressively. Using a sqrt
	 * function is an arbitrary but gentle way of accomplishing this.
	 */
	double userStrengthSqrt = sqrt(userStrength_);
	struct SharpenStatus status;
	/*
	 * Binned modes seem to need the sharpening toned down with this
	 * pipeline, thus we use the modeFactor_ here. Also avoid
	 * divide-by-zero with the userStrengthSqrt.
	 */
	status.threshold = threshold_ * modeFactor_ /
			   std::max(0.01, userStrengthSqrt);
	status.strength = strength_ / modeFactor_ * userStrength_;
	status.limit = limit_ / modeFactor_ * userStrengthSqrt;
	/* Finally, report any application-supplied parameters that were used. */
	status.userStrength = userStrength_;
	imageMetadata->set("sharpen.status", status);
}

/* Register algorithm with the system. */
static Algorithm *create(Controller *controller)
{
	return new Sharpen(controller);
}
static RegisterAlgorithm reg(NAME, &create);
ass="hl kwb">void stop() override {} void configure(const CameraSensorInfo &info, const std::map<unsigned int, IPAStream> &streamConfig, const std::map<unsigned int, const ControlInfoMap &> &entityControls, const IPAOperationData &ipaConfig, IPAOperationData *response) override; void mapBuffers(const std::vector<IPABuffer> &buffers) override; void unmapBuffers(const std::vector<unsigned int> &ids) override; void processEvent(const IPAOperationData &event) override; private: void queueRequest(unsigned int frame, rkisp1_params_cfg *params, const ControlList &controls); void updateStatistics(unsigned int frame, const rkisp1_stat_buffer *stats); void setControls(unsigned int frame); void metadataReady(unsigned int frame, unsigned int aeState); std::map<unsigned int, FrameBuffer> buffers_; std::map<unsigned int, void *> buffersMemory_; ControlInfoMap ctrls_; /* Camera sensor controls. */ bool autoExposure_; uint32_t exposure_; uint32_t minExposure_; uint32_t maxExposure_; uint32_t gain_; uint32_t minGain_; uint32_t maxGain_; }; /** * \todo The RkISP1 pipeline currently provides an empty CameraSensorInfo * if the connected sensor does not provide enough information to properly * assemble one. Make sure the reported sensor information are relevant * before accessing them. */ void IPARkISP1::configure([[maybe_unused]] const CameraSensorInfo &info, [[maybe_unused]] const std::map<unsigned int, IPAStream> &streamConfig, const std::map<unsigned int, const ControlInfoMap &> &entityControls, [[maybe_unused]] const IPAOperationData &ipaConfig, [[maybe_unused]] IPAOperationData *result) { if (entityControls.empty()) return; ctrls_ = entityControls.at(0); const auto itExp = ctrls_.find(V4L2_CID_EXPOSURE); if (itExp == ctrls_.end()) { LOG(IPARkISP1, Error) << "Can't find exposure control"; return; } const auto itGain = ctrls_.find(V4L2_CID_ANALOGUE_GAIN); if (itGain == ctrls_.end()) { LOG(IPARkISP1, Error) << "Can't find gain control"; return; } autoExposure_ = true; minExposure_ = std::max<uint32_t>(itExp->second.min().get<int32_t>(), 1); maxExposure_ = itExp->second.max().get<int32_t>(); exposure_ = minExposure_; minGain_ = std::max<uint32_t>(itGain->second.min().get<int32_t>(), 1); maxGain_ = itGain->second.max().get<int32_t>(); gain_ = minGain_; LOG(IPARkISP1, Info) << "Exposure: " << minExposure_ << "-" << maxExposure_ << " Gain: " << minGain_ << "-" << maxGain_; setControls(0); } void IPARkISP1::mapBuffers(const std::vector<IPABuffer> &buffers) { for (const IPABuffer &buffer : buffers) { auto elem = buffers_.emplace(std::piecewise_construct, std::forward_as_tuple(buffer.id), std::forward_as_tuple(buffer.planes)); const FrameBuffer &fb = elem.first->second; /* * \todo Provide a helper to mmap() buffers (possibly exposed * to applications). */ buffersMemory_[buffer.id] = mmap(NULL, fb.planes()[0].length, PROT_READ | PROT_WRITE, MAP_SHARED, fb.planes()[0].fd.fd(), 0); if (buffersMemory_[buffer.id] == MAP_FAILED) { int ret = -errno; LOG(IPARkISP1, Fatal) << "Failed to mmap buffer: " << strerror(-ret); } } } void IPARkISP1::unmapBuffers(const std::vector<unsigned int> &ids) { for (unsigned int id : ids) { const auto fb = buffers_.find(id); if (fb == buffers_.end()) continue; munmap(buffersMemory_[id], fb->second.planes()[0].length); buffersMemory_.erase(id); buffers_.erase(id); } } void IPARkISP1::processEvent(const IPAOperationData &event) { switch (event.operation) { case RKISP1_IPA_EVENT_SIGNAL_STAT_BUFFER: { unsigned int frame = event.data[0]; unsigned int bufferId = event.data[1]; const rkisp1_stat_buffer *stats = static_cast<rkisp1_stat_buffer *>(buffersMemory_[bufferId]); updateStatistics(frame, stats); break; } case RKISP1_IPA_EVENT_QUEUE_REQUEST: { unsigned int frame = event.data[0]; unsigned int bufferId = event.data[1]; rkisp1_params_cfg *params = static_cast<rkisp1_params_cfg *>(buffersMemory_[bufferId]); queueRequest(frame, params, event.controls[0]); break; } default: LOG(IPARkISP1, Error) << "Unknown event " << event.operation; break; } } void IPARkISP1::queueRequest(unsigned int frame, rkisp1_params_cfg *params, const ControlList &controls) { /* Prepare parameters buffer. */ memset(params, 0, sizeof(*params)); /* Auto Exposure on/off. */ if (controls.contains(controls::AeEnable)) { autoExposure_ = controls.get(controls::AeEnable); if (autoExposure_) params->module_ens = RKISP1_CIF_ISP_MODULE_AEC; params->module_en_update = RKISP1_CIF_ISP_MODULE_AEC; } IPAOperationData op; op.operation = RKISP1_IPA_ACTION_PARAM_FILLED; queueFrameAction.emit(frame, op); } void IPARkISP1::updateStatistics(unsigned int frame, const rkisp1_stat_buffer *stats) { const rkisp1_cif_isp_stat *params = &stats->params; unsigned int aeState = 0; if (stats->meas_type & RKISP1_CIF_ISP_STAT_AUTOEXP) { const rkisp1_cif_isp_ae_stat *ae = &params->ae; const unsigned int target = 60; unsigned int value = 0; unsigned int num = 0; for (int i = 0; i < RKISP1_CIF_ISP_AE_MEAN_MAX; i++) { if (ae->exp_mean[i] <= 15) continue; value += ae->exp_mean[i]; num++; } value /= num; double factor = (double)target / value; if (frame % 3 == 0) { double exposure; exposure = factor * exposure_ * gain_ / minGain_; exposure_ = std::clamp<uint64_t>((uint64_t)exposure, minExposure_, maxExposure_); exposure = exposure / exposure_ * minGain_; gain_ = std::clamp<uint64_t>((uint64_t)exposure, minGain_, maxGain_); setControls(frame + 1); } aeState = fabs(factor - 1.0f) < 0.05f ? 2 : 1; } metadataReady(frame, aeState); } void IPARkISP1::setControls(unsigned int frame) { IPAOperationData op; op.operation = RKISP1_IPA_ACTION_V4L2_SET; ControlList ctrls(ctrls_); ctrls.set(V4L2_CID_EXPOSURE, static_cast<int32_t>(exposure_)); ctrls.set(V4L2_CID_ANALOGUE_GAIN, static_cast<int32_t>(gain_)); op.controls.push_back(ctrls); queueFrameAction.emit(frame, op); } void IPARkISP1::metadataReady(unsigned int frame, unsigned int aeState) { ControlList ctrls(controls::controls); if (aeState) ctrls.set(controls::AeLocked, aeState == 2); IPAOperationData op; op.operation = RKISP1_IPA_ACTION_METADATA; op.controls.push_back(ctrls); queueFrameAction.emit(frame, op); } /* * External IPA module interface */ extern "C" { const struct IPAModuleInfo ipaModuleInfo = { IPA_MODULE_API_VERSION, 1, "PipelineHandlerRkISP1", "rkisp1", }; struct ipa_context *ipaCreate() { return new IPAInterfaceWrapper(std::make_unique<IPARkISP1>()); } } } /* namespace libcamera */