[libcamera-devel] [PATCH v3 06/22] libcamera: PixelFormatInfo: Add functions stride and frameSize

Paul Elder paul.elder at ideasonboard.com
Sat Jul 4 15:31:24 CEST 2020


Add member functions to PixelFormatInfo for calculating stride and frame
size. This will simplify existing code that calculates these things.

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

---
Changes in v3:
- rename functions to stride and frameSize, from bytesPerLine and
  imageSize, respectively

Changes in v2:
- make these functions const
- add documentation
- inline DIV_ROUND_UP
---
 include/libcamera/internal/formats.h |  3 +++
 src/libcamera/formats.cpp            | 40 ++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h
index cf00814..8957059 100644
--- a/include/libcamera/internal/formats.h
+++ b/include/libcamera/internal/formats.h
@@ -52,6 +52,9 @@ public:
 	static const PixelFormatInfo &info(const PixelFormat &format);
 	static const PixelFormatInfo &info(const V4L2PixelFormat &format);
 
+	unsigned int stride(unsigned int width, unsigned int plane) const;
+	unsigned int frameSize(const Size &size) const;
+
 	const char *name;
 	PixelFormat format;
 	V4L2PixelFormat v4l2Format;
diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp
index f96bf1f..6d558f2 100644
--- a/src/libcamera/formats.cpp
+++ b/src/libcamera/formats.cpp
@@ -710,4 +710,44 @@ const PixelFormatInfo &PixelFormatInfo::info(const V4L2PixelFormat &format)
 	return info->second;
 }
 
+/**
+ * \brief Compute the stride
+ * \param[in] width The width of the line, in pixels
+ * \param[in] plane The index of the plane whose stride is to be computed
+ * \return The number of bytes necessary to store a line, or 0 if the
+ * PixelFormatInfo instance not valid
+ */
+unsigned int PixelFormatInfo::stride(unsigned int width, unsigned int plane) const
+{
+	if (!isValid())
+		return 0;
+
+	if (plane > planes.size() || !planes[plane].bytesPerGroup)
+		return 0;
+
+	/* ceil(width / pixelsPerGroup) * bytesPerGroup */
+	return ((width + pixelsPerGroup - 1) / pixelsPerGroup) * planes[plane].bytesPerGroup;
+}
+
+/**
+ * \brief Compute the bytes necessary to store the frame
+ * \param[in] size The size of the frame, in pixels
+ * \return The number of bytes necessary to store the frame, or 0 if the
+ * PixelFormatInfo instance is not valid
+ */
+unsigned int PixelFormatInfo::frameSize(const Size &size) const
+{
+	/* stride * ceil(height / verticalSubSampling) */
+	unsigned int sum = 0;
+	for (int i = 0; i < 3; i++) {
+		unsigned int vertSubSample = planes[i].verticalSubSampling;
+		if (!vertSubSample)
+			continue;
+		sum += stride(size.width, i)
+		     * ((size.height + vertSubSample - 1) / vertSubSample);
+	}
+
+	return sum;
+}
+
 } /* namespace libcamera */
-- 
2.27.0



More information about the libcamera-devel mailing list