[libcamera-devel] ipa: ipu3: Split width and height loops in calculateBdsGrid()
diff mbox series

Message ID 20210915023043.7703-1-laurent.pinchart@ideasonboard.com
State Rejected
Headers show
Series
  • [libcamera-devel] ipa: ipu3: Split width and height loops in calculateBdsGrid()
Related show

Commit Message

Laurent Pinchart Sept. 15, 2021, 2:30 a.m. UTC
The loops over the width and height of the image when calculating the
BDS grid parameters are imbricated, but they're actually independent.
Split them to reduce the complexity.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/ipa/ipu3/ipu3.cpp | 45 ++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 18 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp
index 30d2a53903ec..36fe4a6c9154 100644
--- a/src/ipa/ipu3/ipu3.cpp
+++ b/src/ipa/ipu3/ipu3.cpp
@@ -293,33 +293,42 @@  int IPAIPU3::start()
  */
 void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)
 {
-	uint32_t minError = std::numeric_limits<uint32_t>::max();
 	Size best;
 	Size bestLog2;
 
 	/* Set the BDS output size in the IPAConfiguration structure */
 	context_.configuration.grid.bdsOutputSize = bdsOutputSize;
 
-	for (uint32_t widthShift = 3; widthShift <= 7; ++widthShift) {
+	uint32_t minError = std::numeric_limits<uint32_t>::max();
+
+	for (uint32_t shift = 3; shift <= 7; ++shift) {
 		uint32_t width = std::min(kMaxCellWidthPerSet,
-					  bdsOutputSize.width >> widthShift);
-		width = width << widthShift;
-		for (uint32_t heightShift = 3; heightShift <= 7; ++heightShift) {
-			int32_t height = std::min(kMaxCellHeightPerSet,
-						  bdsOutputSize.height >> heightShift);
-			height = height << heightShift;
-			uint32_t error  = std::abs(static_cast<int>(width - bdsOutputSize.width))
-							+ std::abs(static_cast<int>(height - bdsOutputSize.height));
+					  bdsOutputSize.width >> shift);
+		width = width << shift;
+		uint32_t error = std::abs(static_cast<int>(width - bdsOutputSize.width));
 
-			if (error > minError)
-				continue;
+		if (error > minError)
+			continue;
 
-			minError = error;
-			best.width = width;
-			best.height = height;
-			bestLog2.width = widthShift;
-			bestLog2.height = heightShift;
-		}
+		minError = error;
+		best.width = width;
+		bestLog2.width = shift;
+	}
+
+	minError = std::numeric_limits<uint32_t>::max();
+
+	for (uint32_t shift = 3; shift <= 7; ++shift) {
+		uint32_t height = std::min(kMaxCellHeightPerSet,
+					   bdsOutputSize.height >> shift);
+		height = height << shift;
+		uint32_t error = std::abs(static_cast<int>(height - bdsOutputSize.height));
+
+		if (error > minError)
+			continue;
+
+		minError = error;
+		best.height = height;
+		bestLog2.height = shift;
 	}
 
 	struct ipu3_uapi_grid_config &bdsGrid = context_.configuration.grid.bdsGrid;