/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2022, Ideas on Board Oy * * sdl_sink.h - SDL Sink */ #pragma once #include #include #include #include #include "frame_sink.h" class Image; class SDLTexture; class SDLSink : public FrameSink { public: SDLSink(); ~SDLSink(); int configure(const libcamera::CameraConfiguration &config) override; int start() override; int stop() override; void mapBuffer(libcamera::FrameBuffer *buffer) override; bool processRequest(libcamera::Request *request) override; private: void renderBuffer(libcamera::FrameBuffer *buffer); void processSDLEvents(); std::map> mappedBuffers_; std::unique_ptr texture_; SDL_Window *window_; SDL_Renderer *renderer_; SDL_Rect rect_; bool init_; }; bcamera.git
Jacopo Mondi's clone of libcameragit repository hosting on libcamera.org
summaryrefslogtreecommitdiff
blob: da8d25c3e03d9f7202fbc7292b9373d011731d0f (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * Rockchip ISP1 path helper
 */

#include "rkisp1_path.h"

#include <linux/media-bus-format.h>

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

#include "libcamera/internal/camera_sensor.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)

namespace {

/* Keep in sync with the supported raw formats in rkisp1.cpp. */
const std::map<PixelFormat, uint32_t> formatToMediaBus = {
	{ formats::UYVY, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::YUYV, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::NV12, MEDIA_BUS_FMT_YUYV8_1_5X8 },
	{ formats::NV21, MEDIA_BUS_FMT_YUYV8_1_5X8 },
	{ formats::NV16, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::NV61, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::YUV420, MEDIA_BUS_FMT_YUYV8_1_5X8 },
	{ formats::YVU420, MEDIA_BUS_FMT_YUYV8_1_5X8 },
	{ formats::YUV422, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::YVU422, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::R8, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::RGB565, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::XRGB8888, MEDIA_BUS_FMT_YUYV8_2X8 },
	{ formats::SBGGR8, MEDIA_BUS_FMT_SBGGR8_1X8 },
	{ formats::SGBRG8, MEDIA_BUS_FMT_SGBRG8_1X8 },
	{ formats::SGRBG8, MEDIA_BUS_FMT_SGRBG8_1X8 },
	{ formats::SRGGB8, MEDIA_BUS_FMT_SRGGB8_1X8 },
	{ formats::SBGGR10, MEDIA_BUS_FMT_SBGGR10_1X10 },
	{ formats::SGBRG10, MEDIA_BUS_FMT_SGBRG10_1X10 },
	{ formats::SGRBG10, MEDIA_BUS_FMT_SGRBG10_1X10 },
	{ formats::SRGGB10, MEDIA_BUS_FMT_SRGGB10_1X10 },
	{ formats::SBGGR12, MEDIA_BUS_FMT_SBGGR12_1X12 },
	{ formats::SGBRG12, MEDIA_BUS_FMT_SGBRG12_1X12 },
	{ formats::SGRBG12, MEDIA_BUS_FMT_SGRBG12_1X12 },
	{ formats::SRGGB12, MEDIA_BUS_FMT_SRGGB12_1X12 },
};

} /* namespace */

RkISP1Path::RkISP1Path(const char *name, const Span<const PixelFormat> &formats)
	: name_(name), running_(false), formats_(formats), link_(nullptr)
{
}

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;

	populateFormats();

	link_ = media->link("rkisp1_isp", 2, resizer, 0);
	if (!link_)
		return false;

	return true;
}

void RkISP1Path::populateFormats()
{
	V4L2VideoDevice::Formats v4l2Formats = video_->formats();
	if (v4l2Formats.empty()) {
		LOG(RkISP1, Info)
			<< "Failed to enumerate supported formats and sizes, using defaults";

		for (const PixelFormat &format : formats_)
			streamFormats_.insert(format);
		return;
	}

	minResolution_ = { 65535, 65535 };
	maxResolution_ = { 0, 0 };

	std::vector<PixelFormat> formats;
	for (const auto &[format, sizes] : v4l2Formats) {
		const PixelFormat pixelFormat = format.toPixelFormat();

		/*
		 * As a defensive measure, skip any pixel format exposed by the
		 * driver that we don't know about. This ensures that looking up
		 * formats in formatToMediaBus using a key from streamFormats_
		 * will never fail in any of the other functions.
		 */
		if (!formatToMediaBus.count(pixelFormat)) {
			LOG(RkISP1, Warning)
				<< "Unsupported pixel format " << pixelFormat;
			continue;
		}

		streamFormats_.insert(pixelFormat);

		for (const auto &size : sizes) {
			if (minResolution_ > size.min)
				minResolution_ = size.min;
			if (maxResolution_ < size.max)
				maxResolution_ = size.max;
		}
	}
}

/**
 * \brief Filter the sensor resolutions that can be supported
 * \param[in] sensor The camera sensor
 *
 * This function retrieves all the sizes supported by the sensor and
 * filters all the resolutions that can be supported on the pipeline.
 * It is possible that the sensor's maximum output resolution is higher
 * than the ISP maximum input. In that case, this function filters out all
 * the resolution incapable of being supported and returns the maximum
 * sensor resolution that can be supported by the pipeline.
 *
 * \return Maximum sensor size supported on the pipeline
 */
Size RkISP1Path::filterSensorResolution(const CameraSensor *sensor)
{
	auto iter = sensorSizesMap_.find(sensor);
	if (iter != sensorSizesMap_.end())
		return iter->second.back();

	std::vector<Size> &sizes = sensorSizesMap_[sensor];
	for (unsigned int code : sensor->mbusCodes()) {
		for (const Size &size : sensor->sizes(code)) {
			if (size.width > maxResolution_.width ||
			    size.height > maxResolution_.height)
				continue;

			sizes.push_back(size);
		}
	}

	/* Sort in increasing order and remove duplicates. */
	std::sort(sizes.begin(), sizes.end());
	auto last = std::unique(sizes.begin(), sizes.end());
	sizes.erase(last, sizes.end());

	return sizes.back();
}

StreamConfiguration
RkISP1Path::generateConfiguration(const CameraSensor *sensor, const Size &size,
				  StreamRole role)
{
	const std::vector<unsigned int> &mbusCodes = sensor->mbusCodes();
	Size resolution = filterSensorResolution(sensor);

	/* Min and max resolutions to populate the available stream formats. */
	Size maxResolution = maxResolution_.boundedToAspectRatio(resolution)
					   .boundedTo(resolution);
	Size minResolution = minResolution_.expandedToAspectRatio(resolution);

	/* The desired stream size, bound to the max resolution. */
	Size streamSize = size.boundedTo(maxResolution);

	/* Create the list of supported stream formats. */
	std::map<PixelFormat, std::vector<SizeRange>> streamFormats;
	unsigned int rawBitsPerPixel = 0;
	PixelFormat rawFormat;

	for (const auto &format : streamFormats_) {
		const PixelFormatInfo &info = PixelFormatInfo::info(format);

		/* Populate stream formats for non-RAW configurations. */
		if (info.colourEncoding != PixelFormatInfo::ColourEncodingRAW) {
			if (role == StreamRole::Raw)
				continue;

			streamFormats[format] = { { minResolution, maxResolution } };
			continue;
		}

		/* Skip RAW formats for non-raw roles. */
		if (role != StreamRole::Raw)
			continue;

		/* Populate stream formats for RAW configurations. */
		uint32_t mbusCode = formatToMediaBus.at(format);
		if (std::find(mbusCodes.begin(), mbusCodes.end(), mbusCode) ==
		    mbusCodes.end())
			/* Skip formats not supported by sensor. */
			continue;

		/* Add all the RAW sizes the sensor can produce for this code. */
		for (const auto &rawSize : sensor->sizes(mbusCode)) {
			if (rawSize.width > maxResolution_.width ||
			    rawSize.height > maxResolution_.height)
				continue;

			streamFormats[format].push_back({ rawSize, rawSize });
		}

		/*
		 * Store the raw format with the highest bits per pixel for
		 * later usage.
		 */
		if (info.bitsPerPixel > rawBitsPerPixel) {
			rawBitsPerPixel = info.bitsPerPixel;
			rawFormat = format;
		}
	}

	/*
	 * Pick a suitable pixel format for the role. Raw streams need to use a
	 * raw format, processed streams use NV12 by default.
	 */
	PixelFormat format;

	if (role == StreamRole::Raw) {
		if (!rawFormat.isValid()) {
			LOG(RkISP1, Error)
				<< "Sensor " << sensor->model()
				<< " doesn't support raw capture";
			return {};
		}

		format = rawFormat;
	} else {
		format = formats::NV12;
	}

	StreamFormats formats(streamFormats);
	StreamConfiguration cfg(formats);
	cfg.pixelFormat = format;
	cfg.size = streamSize;
	cfg.bufferCount = RKISP1_BUFFER_COUNT;

	return cfg;
}

CameraConfiguration::Status RkISP1Path::validate(const CameraSensor *sensor,
						 StreamConfiguration *cfg)
{
	const std::vector<unsigned int> &mbusCodes = sensor->mbusCodes();
	Size resolution = filterSensorResolution(sensor);

	const StreamConfiguration reqCfg = *cfg;
	CameraConfiguration::Status status = CameraConfiguration::Valid;

	/*
	 * Validate the pixel format. If the requested format isn't supported,
	 * default to either NV12 (all versions of the ISP are guaranteed to
	 * support NV12 on both the main and self paths) if the requested format
	 * is not a raw format, or to the supported raw format with the highest
	 * bits per pixel otherwise.
	 */
	unsigned int rawBitsPerPixel = 0;
	PixelFormat rawFormat;
	bool found = false;

	for (const auto &format : streamFormats_) {
		const PixelFormatInfo &info = PixelFormatInfo::info(format);

		if (info.colourEncoding == PixelFormatInfo::ColourEncodingRAW) {
			/* Skip raw formats not supported by the sensor. */
			uint32_t mbusCode = formatToMediaBus.at(format);
			if (std::find(mbusCodes.begin(), mbusCodes.end(), mbusCode) ==
			    mbusCodes.end())
				continue;

			/*
			 * Store the raw format with the highest bits per pixel
			 * for later usage.
			 */
			if (info.bitsPerPixel > rawBitsPerPixel) {
				rawBitsPerPixel = info.bitsPerPixel;
				rawFormat = format;
			}
		}

		if (cfg->pixelFormat == format) {
			found = true;
			break;
		}
	}

	bool isRaw = PixelFormatInfo::info(cfg->pixelFormat).colourEncoding ==
		     PixelFormatInfo::ColourEncodingRAW;

	/*
	 * If no raw format supported by the sensor has been found, use a
	 * processed format.
	 */
	if (!rawFormat.isValid())
		isRaw = false;

	if (!found)
		cfg->pixelFormat = isRaw ? rawFormat : formats::NV12;

	Size minResolution;
	Size maxResolution;

	if (isRaw) {
		/*
		 * Use the sensor output size closest to the requested stream
		 * size.
		 */
		uint32_t mbusCode = formatToMediaBus.at(cfg->pixelFormat);
		V4L2SubdeviceFormat sensorFormat =
			sensor->getFormat({ mbusCode }, cfg->size);

		minResolution = sensorFormat.size;
		maxResolution = sensorFormat.size;
	} else {
		/*
		 * Adjust the size based on the sensor resolution and absolute
		 * limits of the ISP.
		 */
		minResolution = minResolution_.expandedToAspectRatio(resolution);
		maxResolution = maxResolution_.boundedToAspectRatio(resolution)
					      .boundedTo(resolution);
	}

	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)
{