/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2024, Ideas On Board * * RkISP1 Color Correction Matrix control algorithm */ #include "ccm.h" #include #include #include #include #include #include "libcamera/internal/yaml_parser.h" #include "../utils.h" #include "libipa/interpolator.h" /** * \file ccm.h */ namespace libcamera { namespace ipa::rkisp1::algorithms { /** * \class Ccm * \brief A color correction matrix algorithm */ LOG_DEFINE_CATEGORY(RkISP1Ccm) /** * \copydoc libcamera::ipa::Algorithm::init */ int Ccm::init([[maybe_unused]] IPAContext &context, const YamlObject &tuningData) { int ret = ccm_.readYaml(tuningData["ccms"], "ct", "ccm"); if (ret < 0) { LOG(RkISP1Ccm, Warning) << "Failed to parse 'ccm' " << "parameter from tuning file; falling back to unit matrix"; ccm_.setData({ { 0, Matrix::identity() } }); } ret = offsets_.readYaml(tuningData["ccms"], "ct", "offsets"); if (ret < 0) { LOG(RkISP1Ccm, Warning) << "Failed to parse 'offsets' " << "parameter from tuning file; falling back to zero offsets"; offsets_.setData({ { 0, Matrix({ 0, 0, 0 }) } }); } return 0; } void Ccm::setParameters(struct rkisp1_cif_isp_ctk_config &config, const Matrix &matrix, const Matrix &offsets) { /* * 4 bit integer and 7 bit fractional, ranging from -8 (0x400) to * +7.992 (0x3ff) */ for (unsigned int i = 0; i < 3; i++) { for (unsigned int j = 0; j < 3; j++) config.coeff[i][j] = utils::floatingToFixedPoint<4, 7, uint16_t, double>(matrix[i][j]); } for (unsigned int i = 0; i < 3; i++) config.ct_offset[i] = offsets[i][0] & 0xfff; LOG(RkISP1Ccm, Debug) << "Setting matrix " << matrix; LOG(RkISP1Ccm, Debug) << "Setting offsets " << offsets; } /** * \copydoc libcamera::ipa::Algorithm::prepare */ void Ccm::prepare(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, RkISP1Params *params) { uint32_t ct = context.activeState.awb.temperatureK; /* * \todo The colour temperature will likely be noisy, add filtering to * avoid updating the CCM matrix all the time. */ if (frame > 0 && ct == ct_) { frameContext.ccm.ccm = context.activeState.ccm.ccm; return; } ct_ = ct; Matrix ccm = ccm_.getInterpolated(ct); Matrix offsets = offsets_.getInterpolated(ct); context.activeState.ccm.ccm = ccm; frameContext.ccm.ccm = ccm; auto config = params->block(); config.setEnabled(true); setParameters(*config, ccm, offsets); } /** * \copydoc libcamera::ipa::Algorithm::process */ void Ccm::process([[maybe_unused]] IPAContext &context, [[maybe_unused]] const uint32_t frame, IPAFrameContext &frameContext, [[maybe_unused]] const rkisp1_stat_buffer *stats, ControlList &metadata) { float m[9]; for (unsigned int i = 0; i < 3; i++) { for (unsigned int j = 0; j < 3; j++) m[i * 3 + j] = frameContext.ccm.ccm[i][j]; } metadata.set(controls::ColourCorrectionMatrix, m); } REGISTER_IPA_ALGORITHM(Ccm, "Ccm") } /* namespace ipa::rkisp1::algorithms */ } /* namespace libcamera */ 0&id=f4fe8cf58820c171d6d355001ff4bdf17484275a'>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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2021, Google Inc.
 *
 * cros_camera_buffer.cpp - Chromium OS buffer backend using CameraBufferManager
 */

#include "../camera_buffer.h"

#include "libcamera/internal/log.h"

#include "cros-camera/camera_buffer_manager.h"

using namespace libcamera;

LOG_DECLARE_CATEGORY(HAL)

class CameraBuffer::Private : public Extensible::Private
{
	LIBCAMERA_DECLARE_PUBLIC(CameraBuffer)

public:
	Private(CameraBuffer *cameraBuffer,
		buffer_handle_t camera3Buffer, int flags);
	~Private();

	bool isValid() const { return valid_; }

	unsigned int numPlanes() const;

	Span<uint8_t> plane(unsigned int plane);

	size_t jpegBufferSize(size_t maxJpegBufferSize) const;

private:
	cros::CameraBufferManager *bufferManager_;
	buffer_handle_t handle_;
	unsigned int numPlanes_;
	bool valid_;
	union {
		void *addr;
		android_ycbcr ycbcr;
	} mem;

	const uint8_t *planeAddr(unsigned int plane) const;
	uint8_t *planeAddr(unsigned int plane);
};

CameraBuffer::Private::Private(CameraBuffer *cameraBuffer,
			       buffer_handle_t camera3Buffer, int flags)
	: Extensible::Private(cameraBuffer), handle_(camera3Buffer),
	  numPlanes_(0), valid_(false)
{
	bufferManager_ = cros::CameraBufferManager::GetInstance();

	bufferManager_->Register(camera3Buffer);

	numPlanes_ = bufferManager_->GetNumPlanes(camera3Buffer);
	switch (numPlanes_) {
	case 1: {
		int ret = bufferManager_->Lock(handle_, 0, 0, 0, 0, 0, &mem.addr);
		if (ret) {
			LOG(HAL, Error) << "Single plane buffer mapping failed";
			return;
		}
		break;
	}
	case 2:
	case 3: {
		int ret = bufferManager_->LockYCbCr(handle_, 0, 0, 0, 0, 0,
						    &mem.ycbcr);
		if (ret) {
			LOG(HAL, Error) << "YCbCr buffer mapping failed";
			return;
		}
		break;
	}
	default:
		LOG(HAL, Error) << "Invalid number of planes: " << numPlanes_;
		return;
	}

	valid_ = true;
}

CameraBuffer::Private::~Private()
{
	bufferManager_->Unlock(handle_);
	bufferManager_->Deregister(handle_);
}

unsigned int CameraBuffer::Private::numPlanes() const
{
	return bufferManager_->GetNumPlanes(handle_);
}

Span<uint8_t> CameraBuffer::Private::plane(unsigned int plane)
{
	void *addr;

	switch (numPlanes()) {
	case 1:
		addr = mem.addr;
		break;
	default:
		switch (plane) {
		case 1:
			addr = mem.ycbcr.y;
			break;
		case 2:
			addr = mem.ycbcr.cb;
			break;
		case 3:
			addr = mem.ycbcr.cr;
			break;
		}
	}

	return { static_cast<uint8_t *>(addr),
		 bufferManager_->GetPlaneSize(handle_, plane) };
}

size_t CameraBuffer::Private::jpegBufferSize(size_t maxJpegBufferSize) const
{
	return bufferManager_->GetPlaneSize(handle_, 0);
}

PUBLIC_CAMERA_BUFFER_IMPLEMENTATION