[{"id":20093,"web_url":"https://patchwork.libcamera.org/comment/20093/","msgid":"<YV2rpkRdXCKn0vKo@pendragon.ideasonboard.com>","date":"2021-10-06T13:59:02","subject":"Re: [libcamera-devel] [PATCH v2 06/12] ipa: ipu3: Change limits and\n\tsplit loops in calculateBdsGrid()","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jean-Michel,\n\nThank you for the patch.\n\nOn Thu, Sep 30, 2021 at 11:37:09AM +0200, Jean-Michel Hautbois wrote:\n> The loops over the width and height of the image when calculating the\n> BDS grid parameters are nested, but they're actually independent. Split\n> them to reduce the complexity.\n> \n> While at it, split out the constants to documented const expressions\n> for the grid sizes.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nFor the changes I haven't authored myself,\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/ipa/ipu3/ipu3.cpp | 70 ++++++++++++++++++++++++++++---------------\n>  1 file changed, 46 insertions(+), 24 deletions(-)\n> \n> diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp\n> index b3ac96ed..757a5d50 100644\n> --- a/src/ipa/ipu3/ipu3.cpp\n> +++ b/src/ipa/ipu3/ipu3.cpp\n> @@ -19,6 +19,7 @@\n>  #include <linux/v4l2-controls.h>\n>  \n>  #include <libcamera/base/log.h>\n> +#include <libcamera/base/utils.h>\n>  \n>  #include <libcamera/control_ids.h>\n>  #include <libcamera/framebuffer.h>\n> @@ -136,8 +137,18 @@\n>   * <linux/intel-ipu3.h> struct ipu3_uapi_gamma_corr_lut for further details.\n>   */\n>  \n> -static constexpr uint32_t kMaxCellWidthPerSet = 80;\n> -static constexpr uint32_t kMaxCellHeightPerSet = 60;\n> +/* Minimum grid width, expressed as a number of cells */\n> +static constexpr uint32_t kMinGridWidth = 16;\n> +/* Maximum grid width, expressed as a number of cells */\n> +static constexpr uint32_t kMaxGridWidth = 80;\n> +/* Minimum grid height, expressed as a number of cells */\n> +static constexpr uint32_t kMinGridHeight = 16;\n> +/* Maximum grid height, expressed as a number of cells */\n> +static constexpr uint32_t kMaxGridHeight = 60;\n> +/* log2 of the minimum grid cell width and height, in pixels */\n> +static constexpr uint32_t kMinCellSizeLog2 = 3;\n> +/* log2 of the maximum grid cell width and height, in pixels */\n> +static constexpr uint32_t kMaxCellSizeLog2 = 6;\n>  \n>  namespace libcamera {\n>  \n> @@ -281,45 +292,56 @@ int IPAIPU3::start()\n>  }\n>  \n>  /**\n> + * \\brief Calculate a grid for the AWB statistics\n> + *\n>   * This function calculates a grid for the AWB algorithm in the IPU3 firmware.\n>   * Its input is the BDS output size calculated in the ImgU.\n>   * It is limited for now to the simplest method: find the lesser error\n>   * with the width/height and respective log2 width/height of the cells.\n>   *\n> - * \\todo The frame is divided into cells which can be 8x8 => 128x128.\n> + * \\todo The frame is divided into cells which can be 8x8 => 64x64.\n>   * As a smaller cell improves the algorithm precision, adapting the\n>   * x_start and y_start parameters of the grid would provoke a loss of\n>   * some pixels but would also result in more accurate algorithms.\n>   */\n>  void IPAIPU3::calculateBdsGrid(const Size &bdsOutputSize)\n>  {\n> -\tuint32_t minError = std::numeric_limits<uint32_t>::max();\n>  \tSize best;\n>  \tSize bestLog2;\n>  \n>  \t/* Set the BDS output size in the IPAConfiguration structure */\n>  \tcontext_.configuration.grid.bdsOutputSize = bdsOutputSize;\n>  \n> -\tfor (uint32_t widthShift = 3; widthShift <= 6; ++widthShift) {\n> -\t\tuint32_t width = std::min(kMaxCellWidthPerSet,\n> -\t\t\t\t\t  bdsOutputSize.width >> widthShift);\n> -\t\twidth = width << widthShift;\n> -\t\tfor (uint32_t heightShift = 3; heightShift <= 6; ++heightShift) {\n> -\t\t\tint32_t height = std::min(kMaxCellHeightPerSet,\n> -\t\t\t\t\t\t  bdsOutputSize.height >> heightShift);\n> -\t\t\theight = height << heightShift;\n> -\t\t\tuint32_t error  = std::abs(static_cast<int>(width - bdsOutputSize.width))\n> -\t\t\t\t\t\t\t+ std::abs(static_cast<int>(height - bdsOutputSize.height));\n> -\n> -\t\t\tif (error > minError)\n> -\t\t\t\tcontinue;\n> -\n> -\t\t\tminError = error;\n> -\t\t\tbest.width = width;\n> -\t\t\tbest.height = height;\n> -\t\t\tbestLog2.width = widthShift;\n> -\t\t\tbestLog2.height = heightShift;\n> -\t\t}\n> +\tuint32_t minError = std::numeric_limits<uint32_t>::max();\n> +\tfor (uint32_t shift = kMinCellSizeLog2; shift <= kMaxCellSizeLog2; ++shift) {\n> +\t\tuint32_t width = std::clamp(bdsOutputSize.width >> shift,\n> +\t\t\t\t\t    kMinGridWidth,\n> +\t\t\t\t\t    kMaxGridWidth);\n> +\n> +\t\twidth = width << shift;\n> +\t\tuint32_t error = std::abs(static_cast<int>(width - bdsOutputSize.width));\n> +\t\tif (error >= minError)\n> +\t\t\tcontinue;\n> +\n> +\t\tminError = error;\n> +\t\tbest.width = width;\n> +\t\tbestLog2.width = shift;\n> +\t}\n> +\n> +\tminError = std::numeric_limits<uint32_t>::max();\n> +\tfor (uint32_t shift = kMinCellSizeLog2; shift <= kMaxCellSizeLog2; ++shift) {\n> +\t\tuint32_t height = std::clamp(bdsOutputSize.height >> shift,\n> +\t\t\t\t\t     kMinGridHeight,\n> +\t\t\t\t\t     kMaxGridHeight);\n> +\n> +\t\theight = height << shift;\n> +\t\tuint32_t error = std::abs(static_cast<int>(height - bdsOutputSize.height));\n> +\t\tif (error >= minError)\n> +\t\t\tcontinue;\n> +\n> +\t\tminError = error;\n> +\t\tbest.height = height;\n> +\t\tbestLog2.height = shift;\n>  \t}\n>  \n>  \tstruct ipu3_uapi_grid_config &bdsGrid = context_.configuration.grid.bdsGrid;","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id F3740C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  6 Oct 2021 13:59:14 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3FA056023F;\n\tWed,  6 Oct 2021 15:59:14 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C0CE66023D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  6 Oct 2021 15:59:11 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 26FF3FFD;\n\tWed,  6 Oct 2021 15:59:11 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"LAfY0Lll\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1633528751;\n\tbh=chEsYFl9U9id9IRhKZc0Jo4DBoMowhrch0/agna965Y=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=LAfY0LllL/ysWoSV57IliXUenopidmYq80AbW4juyoLoyi50hSxFF36FX/SPMRGb9\n\t91gheIW5IJTs9MgtAQgP5fFhFg5ZVPtlMPivvq+iuPnBrgaBxlT2yxqYCmf6IGAavW\n\t0k0wKXQYrAxRMYVbI7KvKOHbevazWq5ckBg4dbbQ=","Date":"Wed, 6 Oct 2021 16:59:02 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>","Message-ID":"<YV2rpkRdXCKn0vKo@pendragon.ideasonboard.com>","References":"<20210930093715.73293-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20210930093715.73293-7-jeanmichel.hautbois@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210930093715.73293-7-jeanmichel.hautbois@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2 06/12] ipa: ipu3: Change limits and\n\tsplit loops in calculateBdsGrid()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]