summaryrefslogtreecommitdiff
path: root/src/libcamera/pipeline/rkisp1/rkisp1_path.cpp
blob: 96c18b352472a417b03783d071a64984564fb6ac (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * rkisp1path.cpp - Rockchip ISP1 path helper
 */

#include "rkisp1_path.h"

#include <libcamera/formats.h>
#include <libcamera/stream.h>

#include "libcamera/internal/media_device.h"
#include "libcamera/internal/v4l2_subdevice.h"
#include "libcamera/internal/v4l2_videodevice.h"

namespace libcamera {

LOG_DECLARE_CATEGORY(RkISP1)

RkISP1Path::RkISP1Path(const char *name, const Span<const PixelFormat> &formats,
		       const Size &minResolution, const Size &maxResolution)
	: video_(nullptr), name_(name), formats_(formats),
	  minResolution_(minResolution), maxResolution_(maxResolution),
	  resizer_(nullptr)
{
}

RkISP1Path::~RkISP1Path()
{
	delete video_;
	delete resizer_;
}

bool RkISP1Path::init(MediaDevice *media)
{
	std::string resizer = std::string("rkisp1_resizer_") + name_ + "path";
	std::string video = std::string("rkisp1_") + name_ + "path";

	resizer_ = V4L2Subdevice::fromEntityName(media, resizer);
	if (resizer_->open() < 0)
		return false;

	video_ = V4L2VideoDevice::fromEntityName(media, video);
	if (video_->open() < 0)
		return false;

	return true;
}

StreamConfiguration RkISP1Path::generateConfiguration(const Size &resolution)
{
	Size maxResolution = resolution;
	maxResolution.boundTo(maxResolution_);

	std::map<PixelFormat, std::vector<SizeRange>> streamFormats;
	for (const PixelFormat &format : formats_)
		streamFormats[format] = { { minResolution_, maxResolution } };

	StreamFormats formats(streamFormats);
	StreamConfiguration cfg(formats);
	cfg.pixelFormat = formats::NV12;
	cfg.size = maxResolution;
	cfg.bufferCount = RKISP1_BUFFER_COUNT;

	return cfg;
}

CameraConfiguration::Status RkISP1Path::validate(StreamConfiguration *cfg)
{
	const StreamConfiguration reqCfg = *cfg;
	CameraConfiguration::Status status = CameraConfiguration::Valid;

	if (std::find(formats_.begin(), formats_.end(), cfg->pixelFormat) ==
	    formats_.end())
		cfg->pixelFormat = formats::NV12;

	cfg->size.boundTo(maxResolution_);
	cfg->size.expandTo(minResolution_);
	cfg->bufferCount = RKISP1_BUFFER_COUNT;

	V4L2DeviceFormat format = {};
	format.fourcc = video_->toV4L2PixelFormat(cfg->pixelFormat);
	format.size = cfg->size;

	int ret = video_->tryFormat(&format);
	if (ret)
		return CameraConfiguration::Invalid;

	cfg->stride = format.planes[0].bpl;
	cfg->frameSize = format.planes[0].size;

	if (cfg->pixelFormat != reqCfg.pixelFormat || cfg->size != reqCfg.size) {
		LOG(RkISP1, Debug)
			<< "Adjusting format from " << reqCfg.toString()
			<< " to " << cfg->toString();
		status = CameraConfiguration::Adjusted;
	}

	return status;
}

int RkISP1Path::configure(const StreamConfiguration &config,
			  const V4L2SubdeviceFormat &inputFormat)
{
	int ret;

	V4L2SubdeviceFormat ispFormat = inputFormat;

	ret = resizer_->setFormat(0, &ispFormat);
	if (ret < 0)
		return ret;

	LOG(RkISP1, Debug)
		<< "Configured " << name_ << " resizer input pad with "
		<< ispFormat.toString();

	ispFormat.size = config.size;

	LOG(RkISP1, Debug)
		<< "Configuring " << name_ << " resizer output pad with "
		<< ispFormat.toString();

	ret = resizer_->setFormat(1, &ispFormat);
	if (ret < 0)
		return ret;

	LOG(RkISP1, Debug)
		<< "Configured " << name_ << " resizer output pad with "
		<< ispFormat.toString();

	const PixelFormatInfo &info = PixelFormatInfo::info(config.pixelFormat);
	V4L2DeviceFormat outputFormat = {};
	outputFormat.fourcc = video_->toV4L2PixelFormat(config.pixelFormat);
	outputFormat.size = config.size;
	outputFormat.planesCount = info.numPlanes();

	ret = video_->setFormat(&outputFormat);
	if (ret)
		return ret;

	if (outputFormat.size != config.size ||
	    outputFormat.fourcc != video_->toV4L2PixelFormat(config.pixelFormat)) {
		LOG(RkISP1, Error)
			<< "Unable to configure capture in " << config.toString();
		return -EINVAL;
	}

	return 0;
}

namespace {
constexpr Size RKISP1_RSZ_MP_SRC_MIN{ 32, 16 };
constexpr Size RKISP1_RSZ_MP_SRC_MAX{ 4416, 3312 };
constexpr std::array<PixelFormat, 7> RKISP1_RSZ_MP_FORMATS{
	formats::YUYV,
	formats::YVYU,
	formats::VYUY,
	formats::NV16,
	formats::NV61,
	formats::NV21,
	formats::NV12,
	/* \todo Add support for 8-bit greyscale to DRM formats */
};

constexpr Size RKISP1_RSZ_SP_SRC_MIN{ 32, 16 };
constexpr Size RKISP1_RSZ_SP_SRC_MAX{ 1920, 1920 };
constexpr std::array<PixelFormat, 7> RKISP1_RSZ_SP_FORMATS{
	formats::YUYV,
	formats::YVYU,
	formats::VYUY,
	formats::NV16,
	formats::NV61,
	formats::NV21,
	formats::NV12,
	/* \todo Add support for BGR888 and RGB565 */
};
} /* namespace */

RkISP1MainPath::RkISP1MainPath()
	: RkISP1Path("main", RKISP1_RSZ_MP_FORMATS,
		     RKISP1_RSZ_MP_SRC_MIN, RKISP1_RSZ_MP_SRC_MAX)
{
}

RkISP1SelfPath::RkISP1SelfPath()
	: RkISP1Path("self", RKISP1_RSZ_SP_FORMATS,
		     RKISP1_RSZ_SP_SRC_MIN, RKISP1_RSZ_SP_SRC_MAX)
{
}

} /* namespace libcamera */