[{"id":19932,"web_url":"https://patchwork.libcamera.org/comment/19932/","msgid":"<20210928153440.vscpilxxdxhvqjk2@ideasonboard.com>","date":"2021-09-28T15:34:40","subject":"Re: [libcamera-devel] [PATCH 06/12] ipa: ipu3: Change limits and\n\tsplit loops in calculateBdsGrid()","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"On Thu, Sep 23, 2021 at 10:16:19AM +0200, Jean-Michel Hautbois wrote:\n> The loops over the width and height of the image when calculating the\n> BDS grid parameters are imbricated, but they're actually independent.\n\nWhat's an imbricated ?\n\nOk - I had to look it up ... it means overlapped ...?\n\n\n\n> Split them to reduce the complexity.\n> \n> While at it, change the limits with the known limitations for the grid\n> size.\n\nDidn't we already change the limits earlier?\nPerhaps you mean\n\n\"While at it, split out the constants to documented const expressions\nfor the grid sizes.\" ?\n\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n\nThe calculations look a lot more readable I think.\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n>  src/ipa/ipu3/ipu3.cpp | 66 ++++++++++++++++++++++++++-----------------\n>  1 file changed, 40 insertions(+), 26 deletions(-)\n> \n> diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp\n> index b97aff80..06d9bcb8 100644\n> --- a/src/ipa/ipu3/ipu3.cpp\n> +++ b/src/ipa/ipu3/ipu3.cpp\n> @@ -136,8 +136,18 @@\n>   * <linux/intel-ipu3.h> struct ipu3_uapi_gamma_corr_lut for further details.\n>   */\n>  \n> +/* Minimum width of a cell of the grid */\n> +static constexpr uint32_t kMinCellWidthPerSet = 16;\n> +/* Maximum width of a cell of the grid */\n>  static constexpr uint32_t kMaxCellWidthPerSet = 80;\n> +/* Minimum height of a cell of the grid */\n> +static constexpr uint32_t kMinCellHeightPerSet = 16;\n> +/* Minimum height of a cell of the grid */\n>  static constexpr uint32_t kMaxCellHeightPerSet = 60;\n> +/* Minimum log2 of the width of each cell in pixels of the grid */\n> +static constexpr uint32_t kMinCellSizeLog2 = 3;\n> +/* Maximum log2 of the width of each cell in pixels of the grid */\n> +static constexpr uint32_t kMaxCellSizeLog2 = 6;\n>  \n>  namespace libcamera {\n>  \n> @@ -285,41 +295,45 @@ int IPAIPU3::start()\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> - * 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> +\tuint32_t shift;\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 (shift = kMinCellSizeLog2; shift <= kMaxCellSizeLog2; ++shift) {\n> +\t\tuint32_t width = std::clamp(bdsOutputSize.width >> shift,\n> +\t\t\t\t\t    kMinCellWidthPerSet,\n> +\t\t\t\t\t    kMaxCellWidthPerSet);\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> +\tfor (shift = kMinCellSizeLog2; shift <= kMaxCellSizeLog2; ++shift) {\n> +\t\tuint32_t height = std::clamp(bdsOutputSize.height >> shift,\n> +\t\t\t\t\t     kMinCellHeightPerSet,\n> +\t\t\t\t\t     kMaxCellHeightPerSet);\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;\n> -- \n> 2.30.2\n>","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 4AFABBDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 28 Sep 2021 15:34:44 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8A33769191;\n\tTue, 28 Sep 2021 17:34:43 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3B7B169188\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 28 Sep 2021 17:34:42 +0200 (CEST)","from ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id ABB923F1;\n\tTue, 28 Sep 2021 17:34:41 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"MPNdnkz9\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1632843281;\n\tbh=2jZDlfyCY9tfUF6gcknfrtv0hWz1ypbk08FZBa/jwZU=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=MPNdnkz9FZCS/rzsyw5CVRZh5x7vFJeX4nqQUDx6nvwo2xmQe0r26kF2GGAheinXW\n\tiwD8QZQYLAntLsdFSo46fcIebcSKm4JJRGVa/bWOGRZoIqAnWmOdAdDr0MlWyZC5kH\n\tjaEjmg7OPzLXJ6zowxNFEQmLf2zfbAzqQi3B+2k4=","Date":"Tue, 28 Sep 2021 16:34:40 +0100","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>","Message-ID":"<20210928153440.vscpilxxdxhvqjk2@ideasonboard.com>","References":"<20210923081625.60276-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20210923081625.60276-7-jeanmichel.hautbois@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210923081625.60276-7-jeanmichel.hautbois@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH 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>"}},{"id":19990,"web_url":"https://patchwork.libcamera.org/comment/19990/","msgid":"<YVRWgcwiM0F5w1lv@pendragon.ideasonboard.com>","date":"2021-09-29T12:05:21","subject":"Re: [libcamera-devel] [PATCH 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":"On Tue, Sep 28, 2021 at 04:34:40PM +0100, Kieran Bingham wrote:\n> On Thu, Sep 23, 2021 at 10:16:19AM +0200, Jean-Michel Hautbois wrote:\n> > The loops over the width and height of the image when calculating the\n> > BDS grid parameters are imbricated, but they're actually independent.\n> \n> What's an imbricated ?\n> \n> Ok - I had to look it up ... it means overlapped ...?\n\nI think \"nested\" would be a better word here.\n\n> > Split them to reduce the complexity.\n> > \n> > While at it, change the limits with the known limitations for the grid\n> > size.\n> \n> Didn't we already change the limits earlier?\n> Perhaps you mean\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> \n> The calculations look a lot more readable I think.\n> \n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> > ---\n> >  src/ipa/ipu3/ipu3.cpp | 66 ++++++++++++++++++++++++++-----------------\n> >  1 file changed, 40 insertions(+), 26 deletions(-)\n> > \n> > diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp\n> > index b97aff80..06d9bcb8 100644\n> > --- a/src/ipa/ipu3/ipu3.cpp\n> > +++ b/src/ipa/ipu3/ipu3.cpp\n> > @@ -136,8 +136,18 @@\n> >   * <linux/intel-ipu3.h> struct ipu3_uapi_gamma_corr_lut for further details.\n> >   */\n> >  \n> > +/* Minimum width of a cell of the grid */\n\nThat's not right, this is the minimum number of cells in the grid, not\nthe minimum width of a cell. Same for the other comments.\n\n> > +static constexpr uint32_t kMinCellWidthPerSet = 16;\n\nThe name of the constant is also not great. I'd go for\n\n/* Minimum grid width, expressed as a number of cells */\nstatic constexpr uint32_t kMinGridWidth = 16;\n\nassuming we will always express the grid size in cells units. If there's\na need to mix grid sizes in cells and in pixels, then we need more\nprecise names.\n\n> > +/* Maximum width of a cell of the grid */\n> >  static constexpr uint32_t kMaxCellWidthPerSet = 80;\n> > +/* Minimum height of a cell of the grid */\n> > +static constexpr uint32_t kMinCellHeightPerSet = 16;\n> > +/* Minimum height of a cell of the grid */\n> >  static constexpr uint32_t kMaxCellHeightPerSet = 60;\n> > +/* Minimum log2 of the width of each cell in pixels of the grid */\n\n/* log2 of the minimum grid cell width and height, in pixels */\n\n> > +static constexpr uint32_t kMinCellSizeLog2 = 3;\n> > +/* Maximum log2 of the width of each cell in pixels of the grid */\n\n/* log2 of the maximum grid cell width and height, in pixels */\n\n> > +static constexpr uint32_t kMaxCellSizeLog2 = 6;\n> >  \n> >  namespace libcamera {\n> >  \n> > @@ -285,41 +295,45 @@ int IPAIPU3::start()\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> > - * 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\nWhy is this dropped ?\n\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> > +\tuint32_t shift;\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 (shift = kMinCellSizeLog2; shift <= kMaxCellSizeLog2; ++shift) {\n\n\tfor (unsigned int shift = ...)\n\nand you can drop the variable declaration above.\n\n> > +\t\tuint32_t width = std::clamp(bdsOutputSize.width >> shift,\n> > +\t\t\t\t\t    kMinCellWidthPerSet,\n> > +\t\t\t\t\t    kMaxCellWidthPerSet);\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> > +\tfor (shift = kMinCellSizeLog2; shift <= kMaxCellSizeLog2; ++shift) {\n> > +\t\tuint32_t height = std::clamp(bdsOutputSize.height >> shift,\n> > +\t\t\t\t\t     kMinCellHeightPerSet,\n> > +\t\t\t\t\t     kMaxCellHeightPerSet);\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 0E742BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 29 Sep 2021 12:05:26 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7E13E691AC;\n\tWed, 29 Sep 2021 14:05:25 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AF05C69185\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 29 Sep 2021 14:05:23 +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 1FB163F0;\n\tWed, 29 Sep 2021 14:05:23 +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=\"iPB7OHSg\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1632917123;\n\tbh=FqMZOynh/lElK9gIWa4agceuNao6gZ/eU5KUa11ZyNU=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=iPB7OHSgTK5JUeT0mUw6JCVDG6nkJ1gJ0KqMsvYDOSbY6TK3HJ/aIFcon7U7RrT87\n\tR4Hg7cLmysvD56o1vlEHd9+LlFP9UTDGZoXcNc60poLZkUxNnVaqFU/vfcf3nPQd33\n\tqDZDogiQjACuRjlMa1QiB+E+XUqNVhXXy4K6s+Tk=","Date":"Wed, 29 Sep 2021 15:05:21 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<YVRWgcwiM0F5w1lv@pendragon.ideasonboard.com>","References":"<20210923081625.60276-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20210923081625.60276-7-jeanmichel.hautbois@ideasonboard.com>\n\t<20210928153440.vscpilxxdxhvqjk2@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210928153440.vscpilxxdxhvqjk2@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH 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>"}}]