summaryrefslogtreecommitdiff
path: root/src/libcamera/v4l2_pixelformat.cpp
blob: 876f5de321f64f27fb953a519f73a3121bd32892 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 * Copyright (C) 2020, Raspberry Pi (Trading) Ltd.
 *
 * v4l2_pixelformat.cpp - V4L2 Pixel Format
 */

#include "v4l2_pixelformat.h"

#include <ctype.h>
#include <map>
#include <string.h>

#include <linux/drm_fourcc.h>

#include <libcamera/pixelformats.h>

#include "log.h"

/**
 * \file v4l2_pixelformat.h
 * \brief V4L2 Pixel Format
 */
namespace libcamera {

LOG_DECLARE_CATEGORY(V4L2)

/**
 * \class V4L2PixelFormat
 * \brief V4L2 pixel format FourCC wrapper
 *
 * The V4L2PixelFormat class describes the pixel format of a V4L2 buffer. It
 * wraps the V4L2 numerical FourCC, and shall be used in all APIs that deal with
 * V4L2 pixel formats. Its purpose is to prevent unintentional confusion of
 * V4L2 and DRM FourCCs in code by catching implicit conversion attempts at
 * compile time.
 *
 * To achieve this goal, construction of a V4L2PixelFormat from an integer value
 * is explicit. To retrieve the integer value of a V4L2PixelFormat, both the
 * explicit value() and implicit uint32_t conversion operators may be used.
 */

/**
 * \fn V4L2PixelFormat::V4L2PixelFormat()
 * \brief Construct a V4L2PixelFormat with an invalid format
 *
 * V4L2PixelFormat instances constructed with the default constructor are
 * invalid, calling the isValid() function returns false.
 */

/**
 * \fn V4L2PixelFormat::V4L2PixelFormat(uint32_t fourcc)
 * \brief Construct a V4L2PixelFormat from a FourCC value
 * \param[in] fourcc The pixel format FourCC numerical value
 */

/**
 * \fn bool V4L2PixelFormat::isValid() const
 * \brief Check if the pixel format is valid
 *
 * V4L2PixelFormat instances constructed with the default constructor are
 * invalid. Instances constructed with a FourCC defined in the V4L2 API are
 * valid. The behaviour is undefined otherwise.
 *
 * \return True if the pixel format is valid, false otherwise
 */

/**
 * \fn uint32_t V4L2PixelFormat::fourcc() const
 * \brief Retrieve the pixel format FourCC numerical value
 * \return The pixel format FourCC numerical value
 */

/**
 * \fn V4L2PixelFormat::operator uint32_t() const
 * \brief Convert to the pixel format FourCC numerical value
 * \return The pixel format FourCC numerical value
 */

/**
 * \brief Assemble and return a string describing the pixel format
 * \return A string describing the pixel format
 */
std::string V4L2PixelFormat::toString() const
{
	if (fourcc_ == 0)
		return "<INVALID>";

	char ss[8] = { static_cast<char>(fourcc_ & 0x7f),
		       static_cast<char>((fourcc_ >> 8) & 0x7f),
		       static_cast<char>((fourcc_ >> 16) & 0x7f),
		       static_cast<char>((fourcc_ >> 24) & 0x7f) };

	for (unsigned int i = 0; i < 4; i++) {
		if (!isprint(ss[i]))
			ss[i] = '.';
	}

	if (fourcc_ & (1 << 31))
		strcat(ss, "-BE");

	return ss;
}

/**
 * \brief Convert the V4L2 pixel format to the corresponding PixelFormat
 * \return The PixelFormat corresponding to the V4L2 pixel format
 */
PixelFormat V4L2PixelFormat::toPixelFormat() const
{
	switch (fourcc_) {
	/* RGB formats. */
	case V4L2_PIX_FMT_RGB24:
		return PixelFormat(DRM_FORMAT_BGR888);
	case V4L2_PIX_FMT_BGR24:
		return PixelFormat(DRM_FORMAT_RGB888);
	case V4L2_PIX_FMT_RGBA32:
		return PixelFormat(DRM_FORMAT_ABGR8888);
	case V4L2_PIX_FMT_ABGR32:
		return PixelFormat(DRM_FORMAT_ARGB8888);
	case V4L2_PIX_FMT_ARGB32:
		return PixelFormat(DRM_FORMAT_BGRA8888);
	case V4L2_PIX_FMT_BGRA32:
		return PixelFormat(DRM_FORMAT_RGBA8888);

	/* YUV packed formats. */
	case V4L2_PIX_FMT_YUYV:
		return PixelFormat(DRM_FORMAT_YUYV);
	case V4L2_PIX_FMT_YVYU:
		return PixelFormat(DRM_FORMAT_YVYU);
	case V4L2_PIX_FMT_UYVY:
		return PixelFormat(DRM_FORMAT_UYVY);
	case V4L2_PIX_FMT_VYUY:
		return PixelFormat(DRM_FORMAT_VYUY);

	/* YUY planar formats. */
	case V4L2_PIX_FMT_NV16:
	case V4L2_PIX_FMT_NV16M:
		return PixelFormat(DRM_FORMAT_NV16);
	case V4L2_PIX_FMT_NV61:
	case V4L2_PIX_FMT_NV61M:
		return PixelFormat(DRM_FORMAT_NV61);
	case V4L2_PIX_FMT_NV12:
	case V4L2_PIX_FMT_NV12M:
		return PixelFormat(DRM_FORMAT_NV12);
	case V4L2_PIX_FMT_NV21:
	case V4L2_PIX_FMT_NV21M:
		return PixelFormat(DRM_FORMAT_NV21);

	/* Greyscale formats. */
	case V4L2_PIX_FMT_GREY:
		return PixelFormat(DRM_FORMAT_R8);

	/* Compressed formats. */
	case V4L2_PIX_FMT_MJPEG:
		return PixelFormat(DRM_FORMAT_MJPEG);

	/* V4L2 formats not yet supported by DRM. */
	default:
		LOG(V4L2, Warning)
			<< "Unsupported V4L2 pixel format "
			<< toString();
		return PixelFormat();
	}
}

/**
 * \brief Convert \a pixelFormat to its corresponding V4L2PixelFormat
 * \param[in] pixelFormat The PixelFormat to convert
 * \param[in] multiplanar V4L2 Multiplanar API support flag
 *
 * Multiple V4L2 formats may exist for one PixelFormat when the format uses
 * multiple planes, as V4L2 defines separate 4CCs for contiguous and separate
 * planes formats. Set the \a multiplanar parameter to false to select a format
 * with contiguous planes, or to true to select a format with non-contiguous
 * planes.
 *
 * \return The V4L2PixelFormat corresponding to \a pixelFormat
 */
V4L2PixelFormat V4L2PixelFormat::fromPixelFormat(const PixelFormat &pixelFormat,
						 bool multiplanar)
{
	switch (pixelFormat) {
	/* RGB formats. */
	case DRM_FORMAT_BGR888:
		return V4L2PixelFormat(V4L2_PIX_FMT_RGB24);
	case DRM_FORMAT_RGB888:
		return V4L2PixelFormat(V4L2_PIX_FMT_BGR24);
	case DRM_FORMAT_ABGR8888:
		return V4L2PixelFormat(V4L2_PIX_FMT_RGBA32);
	case DRM_FORMAT_ARGB8888:
		return V4L2PixelFormat(V4L2_PIX_FMT_ABGR32);
	case DRM_FORMAT_BGRA8888:
		return V4L2PixelFormat(V4L2_PIX_FMT_ARGB32);
	case DRM_FORMAT_RGBA8888:
		return V4L2PixelFormat(V4L2_PIX_FMT_BGRA32);

	/* YUV packed formats. */
	case DRM_FORMAT_YUYV:
		return V4L2PixelFormat(V4L2_PIX_FMT_YUYV);
	case DRM_FORMAT_YVYU:
		return V4L2PixelFormat(V4L2_PIX_FMT_YVYU);
	case DRM_FORMAT_UYVY:
		return V4L2PixelFormat(V4L2_PIX_FMT_UYVY);
	case DRM_FORMAT_VYUY:
		return V4L2PixelFormat(V4L2_PIX_FMT_VYUY);

	/*
	 * YUY planar formats.
	 * \todo Add support for non-contiguous memory planes
	 * \todo Select the format variant not only based on \a multiplanar but
	 * also take into account the formats supported by the device.
	 */
	case DRM_FORMAT_NV16:
		return V4L2PixelFormat(V4L2_PIX_FMT_NV16);
	case DRM_FORMAT_NV61:
		return V4L2PixelFormat(V4L2_PIX_FMT_NV61);
	case DRM_FORMAT_NV12:
		return V4L2PixelFormat(V4L2_PIX_FMT_NV12);
	case DRM_FORMAT_NV21:
		return V4L2PixelFormat(V4L2_PIX_FMT_NV21);

	/* Greyscale formats. */
	case DRM_FORMAT_R8:
		return V4L2PixelFormat(V4L2_PIX_FMT_GREY);

	/* Compressed formats. */
	case DRM_FORMAT_MJPEG:
		return V4L2PixelFormat(V4L2_PIX_FMT_MJPEG);

	default:
		LOG(V4L2, Warning)
			<< "Unsupported pixel format "
			<< pixelFormat.toString();
		return V4L2PixelFormat();
	}
}

} /* namespace libcamera */