[libcamera-devel] [PATCH v2 2/6] libcamera: formats: Add fields to info ease calculating stride

Paul Elder paul.elder at ideasonboard.com
Tue Jun 30 16:58:04 CEST 2020


Packed formats make it difficult to calculate stride as well as
frame size with the fields that PixelFormatInfo currently has.
bitsPerPixel is defined as the average number of bits per pixel, and
only counts effective bits, so it is not useful for calculating
stride and frame size.

To fix this, we introduce a concept of a "pixel group". The size of this
group is defined as the minimum number of pixels (including padding)
necessary in a row when the image has only one column of effective
pixels. The pixel group has one more attribute, that is the "bytes per
group". This determines how many bytes one pixel group consumes. These
are the fields pixelsPerGroup and bytesPerGroup that are defined in this
patch. Defining these two values makes it really simple to calculate
bytes-per-line, as ceil(width / pixelsPerGroup) * bytesPerGroup, where
width is measured in number of pixels. The ceiling accounts for padding.

For example, for something simple like BGR888, it is self-explanatory:
the pixel group size is 1, and the bytes necessary is 3. For YUYV, the
CbCr pair is shared between two pixels, so even if you have only one
pixel, you would still need a padded second Y, therefore the pixel
group size is 2, and bytes necessary is 4 (as opposed to 1 and 2). NV12
seems like it shold be 6 bytes with 4 pixels, since there is
downsampling in the Y axis as well, however, the pixel group is only
in terms of rows, so it is half of that, at 2 pixels and 3 bytes. The
IPU3 formats are also self-explanatory, coming from a comment in the
driver, a pixel group is 50, and it consumes 64 bytes.

Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>

---
Changes in v2:
- add documentation for bytesPerGroup pixelsPerGroup
- fix wording in commit message
  - bytes-per-line -> stride
  - buffer size -> frame size
- changed MJPEG todo to allowing pipeline handlers to set parameters of
  format infos
---
 include/libcamera/internal/formats.h |   4 +-
 src/libcamera/formats.cpp            | 111 ++++++++++++++++++++++++++-
 2 files changed, 113 insertions(+), 2 deletions(-)

diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h
index f59ac8f..dc19492 100644
--- a/include/libcamera/internal/formats.h
+++ b/include/libcamera/internal/formats.h
@@ -45,13 +45,15 @@ public:
 
 	static const PixelFormatInfo &info(const PixelFormat &format);
 
-	/* \todo Add support for non-contiguous memory planes */
 	const char *name;
 	PixelFormat format;
 	V4L2PixelFormat v4l2Format;
 	unsigned int bitsPerPixel;
 	enum ColourEncoding colourEncoding;
 	bool packed;
+
+	unsigned int bytesPerGroup;
+	unsigned int pixelsPerGroup;
 };
 
 } /* namespace libcamera */
diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp
index d3b722c..8076c39 100644
--- a/src/libcamera/formats.cpp
+++ b/src/libcamera/formats.cpp
@@ -152,6 +152,26 @@ const std::map<unsigned int, std::vector<SizeRange>> &ImageFormats::data() const
  * bytes. For instance, 12-bit Bayer data with two pixels stored in three bytes
  * is packed, while the same data stored with 4 bits of padding in two bytes
  * per pixel is not packed.
+ *
+ * \var PixelFormatInfo::bytesPerGroup
+ * \brief The number of bytes that a pixel group consumes
+ *
+ * \sa pixelsPerGroup
+ *
+ * \var PixelFormatInfo::pixelsPerGroup
+ * \brief The number of pixels in a pixel group
+ *
+ * The minimum number of pixels (including padding) necessary in a row
+ * when the frame has only one column of effective pixels
+ *
+ * A pixel group is defined as the minimum number of pixels (including padding)
+ * necessary in a row when the image has only one column of effective pixels.
+ * pixelsPerGroup refers to this value. bytesPerGroup, then, refers to the
+ * number of bytes that a pixel group consumes. This definition of a pixel
+ * group allows simple calculation of stride, as
+ * ceil(width / pixelsPerGroup) * bytesPerGroup. These values are determined
+ * only in terms of a row, and include bytes that are used in all planes (for
+ * multiplanar formats).
  */
 
 /**
@@ -179,6 +199,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 24,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
 		.packed = false,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 1,
 	} },
 	{ formats::RGB888, {
 		.name = "RGB888",
@@ -187,6 +209,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 24,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
 		.packed = false,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 1,
 	} },
 	{ formats::ABGR8888, {
 		.name = "ABGR8888",
@@ -195,6 +219,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 32,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 1,
 	} },
 	{ formats::ARGB8888, {
 		.name = "ARGB8888",
@@ -203,6 +229,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 32,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 1,
 	} },
 	{ formats::BGRA8888, {
 		.name = "BGRA8888",
@@ -211,6 +239,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 32,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 1,
 	} },
 	{ formats::RGBA8888, {
 		.name = "RGBA8888",
@@ -219,6 +249,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 32,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRGB,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 1,
 	} },
 
 	/* YUV packed formats. */
@@ -229,6 +261,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 16,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::YVYU, {
 		.name = "YVYU",
@@ -237,6 +271,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 16,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::UYVY, {
 		.name = "UYVY",
@@ -245,6 +281,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 16,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::VYUY, {
 		.name = "VYUY",
@@ -253,6 +291,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 16,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 
 	/* YUV planar formats. */
@@ -263,6 +303,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::NV21, {
 		.name = "NV21",
@@ -271,6 +313,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::NV16, {
 		.name = "NV16",
@@ -279,6 +323,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 16,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::NV61, {
 		.name = "NV61",
@@ -287,6 +333,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 16,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::NV24, {
 		.name = "NV24",
@@ -295,6 +343,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 24,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 1,
 	} },
 	{ formats::NV42, {
 		.name = "NV42",
@@ -303,6 +353,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 24,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 1,
 	} },
 	{ formats::YUV420, {
 		.name = "YUV420",
@@ -311,6 +363,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::YUV422, {
 		.name = "YUV422",
@@ -319,6 +373,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 16,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 
 	/* Greyscale formats. */
@@ -329,6 +385,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 8,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 1,
+		.pixelsPerGroup = 1,
 	} },
 
 	/* Bayer formats. */
@@ -339,6 +397,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 8,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 2,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SGBRG8, {
 		.name = "SGBRG8",
@@ -347,6 +407,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 8,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 2,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SGRBG8, {
 		.name = "SGRBG8",
@@ -355,6 +417,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 8,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 2,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SRGGB8, {
 		.name = "SRGGB8",
@@ -363,6 +427,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 8,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 2,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SBGGR10, {
 		.name = "SBGGR10",
@@ -371,6 +437,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SGBRG10, {
 		.name = "SGBRG10",
@@ -379,6 +447,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SGRBG10, {
 		.name = "SGRBG10",
@@ -387,6 +457,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SRGGB10, {
 		.name = "SRGGB10",
@@ -395,6 +467,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SBGGR10_CSI2P, {
 		.name = "SBGGR10_CSI2P",
@@ -403,6 +477,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 5,
+		.pixelsPerGroup = 4,
 	} },
 	{ formats::SGBRG10_CSI2P, {
 		.name = "SGBRG10_CSI2P",
@@ -411,6 +487,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 5,
+		.pixelsPerGroup = 4,
 	} },
 	{ formats::SGRBG10_CSI2P, {
 		.name = "SGRBG10_CSI2P",
@@ -419,6 +497,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 5,
+		.pixelsPerGroup = 4,
 	} },
 	{ formats::SRGGB10_CSI2P, {
 		.name = "SRGGB10_CSI2P",
@@ -427,6 +507,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 5,
+		.pixelsPerGroup = 4,
 	} },
 	{ formats::SBGGR12, {
 		.name = "SBGGR12",
@@ -435,6 +517,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SGBRG12, {
 		.name = "SGBRG12",
@@ -443,6 +527,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SGRBG12, {
 		.name = "SGRBG12",
@@ -451,6 +537,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SRGGB12, {
 		.name = "SRGGB12",
@@ -459,6 +547,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SBGGR12_CSI2P, {
 		.name = "SBGGR12_CSI2P",
@@ -467,6 +557,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SGBRG12_CSI2P, {
 		.name = "SGBRG12_CSI2P",
@@ -475,6 +567,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SGRBG12_CSI2P, {
 		.name = "SGRBG12_CSI2P",
@@ -483,6 +577,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SRGGB12_CSI2P, {
 		.name = "SRGGB12_CSI2P",
@@ -491,6 +587,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 12,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 3,
+		.pixelsPerGroup = 2,
 	} },
 	{ formats::SBGGR10_IPU3, {
 		.name = "SBGGR10_IPU3",
@@ -499,6 +597,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 64,
+		.pixelsPerGroup = 50,
 	} },
 	{ formats::SGBRG10_IPU3, {
 		.name = "SGBRG10_IPU3",
@@ -507,6 +607,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 64,
+		.pixelsPerGroup = 50,
 	} },
 	{ formats::SGRBG10_IPU3, {
 		.name = "SGRBG10_IPU3",
@@ -515,6 +617,8 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 64,
+		.pixelsPerGroup = 50,
 	} },
 	{ formats::SRGGB10_IPU3, {
 		.name = "SRGGB10_IPU3",
@@ -523,16 +627,21 @@ const std::map<PixelFormat, PixelFormatInfo> pixelFormatInfo{
 		.bitsPerPixel = 10,
 		.colourEncoding = PixelFormatInfo::ColourEncodingRAW,
 		.packed = true,
+		.bytesPerGroup = 64,
+		.pixelsPerGroup = 50,
 	} },
 
 	/* Compressed formats. */
+	/* \todo Allow pipeline handlers to fill in parameters of formats. */
 	{ formats::MJPEG, {
 		.name = "MJPEG",
 		.format = formats::MJPEG,
 		.v4l2Format = V4L2PixelFormat(V4L2_PIX_FMT_MJPEG),
-		.bitsPerPixel = 0,
+		.bitsPerPixel = 16,
 		.colourEncoding = PixelFormatInfo::ColourEncodingYUV,
 		.packed = false,
+		.bytesPerGroup = 4,
+		.pixelsPerGroup = 2,
 	} },
 };
 
-- 
2.27.0



More information about the libcamera-devel mailing list