From patchwork Thu Sep 23 08:16:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 13897 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 2EB7BBF01C for ; Thu, 23 Sep 2021 08:16:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E056A69192; Thu, 23 Sep 2021 10:16:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="YVtMax2w"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7BFD86918A for ; Thu, 23 Sep 2021 10:16:31 +0200 (CEST) Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:392e:dcd2:2bf6:d61c]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2B08745E; Thu, 23 Sep 2021 10:16:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1632384991; bh=rBn5Htj5mFaJ/l98t4TJrQ/5QZ3p5ctqQO2EF+pPqJA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YVtMax2wviwkwbHO0HeqvxiatjHja9ZoEkcdHZR5kjou3gc0ZbFzoXjGY+xf/GTti 6vJYORXQJCZ39vpTQbYjUUMsWxMz+M3zu7hznGfXaVbER4Hd+Fz28E+ljZYvu43Uvv g9TL9WJ8dVKaxongxDMSAK8r1obKkdWlWX09yqnk= From: Jean-Michel Hautbois To: libcamera-devel@lists.libcamera.org Date: Thu, 23 Sep 2021 10:16:19 +0200 Message-Id: <20210923081625.60276-7-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210923081625.60276-1-jeanmichel.hautbois@ideasonboard.com> References: <20210923081625.60276-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 06/12] ipa: ipu3: Change limits and split loops in calculateBdsGrid() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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. While at it, change the limits with the known limitations for the grid size. Signed-off-by: Laurent Pinchart Signed-off-by: Jean-Michel Hautbois Reviewed-by: Kieran Bingham --- src/ipa/ipu3/ipu3.cpp | 66 ++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp index b97aff80..06d9bcb8 100644 --- a/src/ipa/ipu3/ipu3.cpp +++ b/src/ipa/ipu3/ipu3.cpp @@ -136,8 +136,18 @@ * struct ipu3_uapi_gamma_corr_lut for further details. */ +/* Minimum width of a cell of the grid */ +static constexpr uint32_t kMinCellWidthPerSet = 16; +/* Maximum width of a cell of the grid */ static constexpr uint32_t kMaxCellWidthPerSet = 80; +/* Minimum height of a cell of the grid */ +static constexpr uint32_t kMinCellHeightPerSet = 16; +/* Minimum height of a cell of the grid */ static constexpr uint32_t kMaxCellHeightPerSet = 60; +/* Minimum log2 of the width of each cell in pixels of the grid */ +static constexpr uint32_t kMinCellSizeLog2 = 3; +/* Maximum log2 of the width of each cell in pixels of the grid */ +static constexpr uint32_t kMaxCellSizeLog2 = 6; namespace libcamera { @@ -285,41 +295,45 @@ int IPAIPU3::start() * Its input is the BDS output size calculated in the ImgU. * It is limited for now to the simplest method: find the lesser error * with the width/height and respective log2 width/height of the cells. - * - * \todo The frame is divided into cells which can be 8x8 => 128x128. - * As a smaller cell improves the algorithm precision, adapting the - * x_start and y_start parameters of the grid would provoke a loss of - * some pixels but would also result in more accurate algorithms. */ void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize) { - uint32_t minError = std::numeric_limits::max(); Size best; Size bestLog2; + uint32_t shift; /* Set the BDS output size in the IPAConfiguration structure */ context_.configuration.grid.bdsOutputSize = bdsOutputSize; - for (uint32_t widthShift = 3; widthShift <= 6; ++widthShift) { - uint32_t width = std::min(kMaxCellWidthPerSet, - bdsOutputSize.width >> widthShift); - width = width << widthShift; - for (uint32_t heightShift = 3; heightShift <= 6; ++heightShift) { - int32_t height = std::min(kMaxCellHeightPerSet, - bdsOutputSize.height >> heightShift); - height = height << heightShift; - uint32_t error = std::abs(static_cast(width - bdsOutputSize.width)) - + std::abs(static_cast(height - bdsOutputSize.height)); - - if (error > minError) - continue; - - minError = error; - best.width = width; - best.height = height; - bestLog2.width = widthShift; - bestLog2.height = heightShift; - } + uint32_t minError = std::numeric_limits::max(); + for (shift = kMinCellSizeLog2; shift <= kMaxCellSizeLog2; ++shift) { + uint32_t width = std::clamp(bdsOutputSize.width >> shift, + kMinCellWidthPerSet, + kMaxCellWidthPerSet); + + width = width << shift; + uint32_t error = std::abs(static_cast(width - bdsOutputSize.width)); + if (error >= minError) + continue; + + minError = error; + best.width = width; + bestLog2.width = shift; + } + + for (shift = kMinCellSizeLog2; shift <= kMaxCellSizeLog2; ++shift) { + uint32_t height = std::clamp(bdsOutputSize.height >> shift, + kMinCellHeightPerSet, + kMaxCellHeightPerSet); + + height = height << shift; + uint32_t error = std::abs(static_cast(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;