[3/5] libcamera: format: Extend plane size calculation to accept scanline alignment
diff mbox series

Message ID 20241009074642.2965791-4-chenghaoyang@chromium.org
State New
Headers show
Series
  • Add InfoFrame
Related show

Commit Message

Cheng-Hao Yang Oct. 9, 2024, 7:41 a.m. UTC
From: Han-Lin Chen <hanlinchen@chromium.org>

As some platforms need alignment on scanline, this patch extends the
format::planeSize() method to accept scanline alignment.

Signed-off-by: Han-Lin Chen <hanlinchen@chromium.org>
Co-developed-by: Harvey Yang <chenghaoyang@chromium.org>
Signed-off-by: Harvey Yang <chenghaoyang@chromium.org>
---
 include/libcamera/internal/formats.h |  5 +++--
 src/libcamera/formats.cpp            | 17 +++++++++++++----
 2 files changed, 16 insertions(+), 6 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h
index 6a3e9c16a..143ab1dce 100644
--- a/include/libcamera/internal/formats.h
+++ b/include/libcamera/internal/formats.h
@@ -40,10 +40,11 @@  public:
 	unsigned int stride(unsigned int width, unsigned int plane,
 			    unsigned int align = 1) const;
 	unsigned int planeSize(const Size &size, unsigned int plane,
-			       unsigned int align = 1) const;
+			       unsigned int align = 1, unsigned scanAlign = 1) const;
 	unsigned int planeSize(unsigned int height, unsigned int plane,
 			       unsigned int stride) const;
-	unsigned int frameSize(const Size &size, unsigned int align = 1) const;
+	unsigned int frameSize(const Size &size, unsigned int align = 1,
+			       unsigned scanAlign = 1) const;
 	unsigned int frameSize(const Size &size,
 			       const std::array<unsigned int, 3> &strides) const;
 
diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp
index dbefb0947..7643a5a23 100644
--- a/src/libcamera/formats.cpp
+++ b/src/libcamera/formats.cpp
@@ -1093,6 +1093,7 @@  unsigned int PixelFormatInfo::stride(unsigned int width, unsigned int plane,
  * \param[in] size The size of the frame, in pixels
  * \param[in] plane The plane index
  * \param[in] align The stride alignment, in bytes (1 for default alignment)
+ * \param[in] scanAlign The scanline alignment, in bytes (1 for default alignment)
  *
  * The plane size is computed by multiplying the line stride and the frame
  * height, taking subsampling and other format characteristics into account.
@@ -1105,13 +1106,19 @@  unsigned int PixelFormatInfo::stride(unsigned int width, unsigned int plane,
  * format
  */
 unsigned int PixelFormatInfo::planeSize(const Size &size, unsigned int plane,
-					unsigned int align) const
+					unsigned int align, unsigned scanAlign) const
 {
 	unsigned int stride = PixelFormatInfo::stride(size.width, plane, align);
 	if (!stride)
 		return 0;
 
-	return planeSize(size.height, plane, stride);
+	unsigned int vertSubSample = planes[plane].verticalSubSampling;
+	if (!vertSubSample)
+		return 0;
+
+	unsigned int planeHeight = (size.height + vertSubSample - 1) / vertSubSample;
+
+	return stride * ((planeHeight + scanAlign - 1) / scanAlign * scanAlign);
 }
 
 /**
@@ -1143,6 +1150,7 @@  unsigned int PixelFormatInfo::planeSize(unsigned int height, unsigned int plane,
  * \brief Compute the number of bytes necessary to store a frame
  * \param[in] size The size of the frame, in pixels
  * \param[in] align The stride alignment, in bytes (1 for default alignment)
+ * \param[in] scanAlign The scanline alignment, in bytes (1 for default alignment)
  *
  * The frame size is computed by adding the size of all planes, as computed by
  * planeSize(), using the specified alignment constraints for all planes. For
@@ -1154,7 +1162,8 @@  unsigned int PixelFormatInfo::planeSize(unsigned int height, unsigned int plane,
  * \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, unsigned int align) const
+unsigned int PixelFormatInfo::frameSize(const Size &size, unsigned int align,
+					unsigned scanAlign) const
 {
 	unsigned int sum = 0;
 
@@ -1162,7 +1171,7 @@  unsigned int PixelFormatInfo::frameSize(const Size &size, unsigned int align) co
 		if (plane.bytesPerGroup == 0)
 			break;
 
-		sum += planeSize(size, i, align);
+		sum += planeSize(size, i, align, scanAlign);
 	}
 
 	return sum;