[{"id":20015,"web_url":"https://patchwork.libcamera.org/comment/20015/","msgid":"<d83172b9-fab8-ee72-0f8f-721ea1bde17e@ideasonboard.com>","date":"2021-09-30T15:17:27","subject":"Re: [libcamera-devel] [PATCH v2 11/12] ipa: ipu3: agc: Rewrite and\n\tsimplify the brightness loop","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"On 30/09/2021 10:37, Jean-Michel Hautbois wrote:\n> Now that we know how the AWB statistics are formatted, use a simplified\n> loop in processBrightness() to parse the green values and get the\n> histogram.\n> \n> Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n> ---\n>  src/ipa/ipu3/algorithms/agc.cpp | 55 ++++++++++++---------------------\n>  src/ipa/ipu3/algorithms/agc.h   |  2 ++\n>  2 files changed, 22 insertions(+), 35 deletions(-)\n> \n> diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\n> index 5ff50f4a..46e01fc4 100644\n> --- a/src/ipa/ipu3/algorithms/agc.cpp\n> +++ b/src/ipa/ipu3/algorithms/agc.cpp\n> @@ -6,6 +6,7 @@\n>   */\n>  \n>  #include \"agc.h\"\n> +#include \"awb.h\"\n>  \n>  #include <algorithm>\n>  #include <chrono>\n> @@ -47,9 +48,6 @@ static constexpr uint32_t kMaxExposure = 1976;\n>  static constexpr uint32_t knumHistogramBins = 256;\n>  static constexpr double kEvGainTarget = 0.5;\n>  \n> -/* A cell is 8 bytes and contains averages for RGB values and saturation ratio */\n> -static constexpr uint8_t kCellSize = 8;\n> -\n>  Agc::Agc()\n>  \t: frameCount_(0), lastFrame_(0), iqMean_(0.0), lineDuration_(0s),\n>  \t  maxExposureTime_(0s), prevExposure_(0s), prevExposureNoDg_(0s),\n> @@ -57,9 +55,10 @@ Agc::Agc()\n>  {\n>  }\n>  \n> -int Agc::configure([[maybe_unused]] IPAContext &context,\n> -\t\t   const IPAConfigInfo &configInfo)\n> +int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo)\n>  {\n> +\tstride_ = context.configuration.grid.stride;\n> +\n>  \tlineDuration_ = configInfo.sensorInfo.lineLength * 1.0s\n>  \t\t      / configInfo.sensorInfo.pixelRate;\n>  \tmaxExposureTime_ = kMaxExposure * lineDuration_;\n> @@ -70,37 +69,23 @@ int Agc::configure([[maybe_unused]] IPAContext &context,\n>  void Agc::processBrightness(const ipu3_uapi_stats_3a *stats,\n>  \t\t\t    const ipu3_uapi_grid_config &grid)\n>  {\n> -\tconst struct ipu3_uapi_grid_config statsAeGrid = stats->stats_4a_config.awb_config.grid;\n> -\tRectangle aeRegion = { statsAeGrid.x_start,\n> -\t\t\t       statsAeGrid.y_start,\n> -\t\t\t       static_cast<unsigned int>(statsAeGrid.x_end - statsAeGrid.x_start) + 1,\n> -\t\t\t       static_cast<unsigned int>(statsAeGrid.y_end - statsAeGrid.y_start) + 1 };\n> -\tPoint topleft = aeRegion.topLeft();\n> -\tint topleftX = topleft.x >> grid.block_width_log2;\n> -\tint topleftY = topleft.y >> grid.block_height_log2;\n> -\n> -\t/* Align to the grid cell width and height */\n> -\tuint32_t startX = topleftX << grid.block_width_log2;\n> -\tuint32_t startY = topleftY * grid.width << grid.block_width_log2;\n> -\tuint32_t endX = (startX + (aeRegion.size().width >> grid.block_width_log2)) << grid.block_width_log2;\n> -\tuint32_t i, j;\n> -\tuint32_t count = 0;\n> -\n>  \tuint32_t hist[knumHistogramBins] = { 0 };\n> -\tfor (j = topleftY;\n> -\t     j < topleftY + (aeRegion.size().height >> grid.block_height_log2);\n> -\t     j++) {\n> -\t\tfor (i = startX + startY; i < endX + startY; i += kCellSize) {\n> -\t\t\t/*\n> -\t\t\t * The grid width (and maybe height) is not reliable.\n> -\t\t\t * We observed a bit shift which makes the value 160 to be 32 in the stats grid.\n> -\t\t\t * Use the one passed at init time.\n> -\t\t\t */\n> -\t\t\tif (stats->awb_raw_buffer.meta_data[i + 4 + j * grid.width] == 0) {\n> -\t\t\t\tuint8_t Gr = stats->awb_raw_buffer.meta_data[i + 0 + j * grid.width];\n> -\t\t\t\tuint8_t Gb = stats->awb_raw_buffer.meta_data[i + 3 + j * grid.width];\n> -\t\t\t\thist[(Gr + Gb) / 2]++;\n> -\t\t\t\tcount++;\n> +\n> +\tfor (unsigned int cellY = 0; cellY < grid.height; cellY++) {\n> +\t\tfor (unsigned int cellX = 0; cellX < grid.width; cellX++) {\n> +\t\t\tuint32_t cellPosition = cellY * stride_ + cellX\n> +\t\t\t\t\t      * sizeof(Ipu3AwbCell);\n> +\n> +\t\t\t/* Cast the initial IPU3 structure to simplify the reading */\n\nI'm not sure that comment adds much, but it doesn't hurt anything.\n\nStride issues are all fixed up, and I think the Ipu3AwbCell gets changed\nto the new structure in the next patch...\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n\n> +\t\t\tconst Ipu3AwbCell *cell =\n> +\t\t\t\treinterpret_cast<const Ipu3AwbCell *>(\n> +\t\t\t\t\t&stats->awb_raw_buffer.meta_data[cellPosition]\n> +\t\t\t\t);\n> +\n> +\t\t\tif (cell->satRatio == 0) {\n> +\t\t\t\tuint8_t gr = cell->greenRedAvg;\n> +\t\t\t\tuint8_t gb = cell->greenBlueAvg;\n> +\t\t\t\thist[(gr + gb) / 2]++;\n>  \t\t\t}\n>  \t\t}\n>  \t}\n> diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h\n> index e36797d3..64b71c65 100644\n> --- a/src/ipa/ipu3/algorithms/agc.h\n> +++ b/src/ipa/ipu3/algorithms/agc.h\n> @@ -50,6 +50,8 @@ private:\n>  \tDuration prevExposureNoDg_;\n>  \tDuration currentExposure_;\n>  \tDuration currentExposureNoDg_;\n> +\n> +\tuint32_t stride_;\n>  };\n>  \n>  } /* namespace ipa::ipu3::algorithms */\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 2E116C3243\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 30 Sep 2021 15:17:33 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7BCE2691AE;\n\tThu, 30 Sep 2021 17:17:32 +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 1EDE069190\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 30 Sep 2021 17:17:31 +0200 (CEST)","from [192.168.0.20]\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 92123B2B;\n\tThu, 30 Sep 2021 17:17:30 +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=\"sExlpuMJ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1633015050;\n\tbh=0X+/RTZPmzQhd2wX+lQ9XjMxDN4QvUjiCLM8Yr3Mz7s=;\n\th=Subject:To:References:From:Date:In-Reply-To:From;\n\tb=sExlpuMJcQSfpO4sP46kTHaltBsLFuqzTWKOv+L5TXJbBLl5p0nqsP6+bYX5QHmY4\n\tyzuNhNxmBiyewNWcN6Uqf0LgE2w9KdBcl9gtqMUl7ZWP6dCvj39LzZSEcTRlhHtuhk\n\tuMFfGjkb1wKshiq81iDDQ+/hcofYfHcGRMkHtl68=","To":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20210930093715.73293-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20210930093715.73293-12-jeanmichel.hautbois@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<d83172b9-fab8-ee72-0f8f-721ea1bde17e@ideasonboard.com>","Date":"Thu, 30 Sep 2021 16:17:27 +0100","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101\n\tThunderbird/78.13.0","MIME-Version":"1.0","In-Reply-To":"<20210930093715.73293-12-jeanmichel.hautbois@ideasonboard.com>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-GB","Content-Transfer-Encoding":"7bit","Subject":"Re: [libcamera-devel] [PATCH v2 11/12] ipa: ipu3: agc: Rewrite and\n\tsimplify the brightness loop","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":20044,"web_url":"https://patchwork.libcamera.org/comment/20044/","msgid":"<YVusgP19EZMIRWPt@pendragon.ideasonboard.com>","date":"2021-10-05T01:38:08","subject":"Re: [libcamera-devel] [PATCH v2 11/12] ipa: ipu3: agc: Rewrite and\n\tsimplify the brightness loop","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 04:17:27PM +0100, Kieran Bingham wrote:\n> On 30/09/2021 10:37, Jean-Michel Hautbois wrote:\n> > Now that we know how the AWB statistics are formatted, use a simplified\n> > loop in processBrightness() to parse the green values and get the\n> > histogram.\n> > \n> > Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n> > ---\n> >  src/ipa/ipu3/algorithms/agc.cpp | 55 ++++++++++++---------------------\n> >  src/ipa/ipu3/algorithms/agc.h   |  2 ++\n> >  2 files changed, 22 insertions(+), 35 deletions(-)\n> > \n> > diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\n> > index 5ff50f4a..46e01fc4 100644\n> > --- a/src/ipa/ipu3/algorithms/agc.cpp\n> > +++ b/src/ipa/ipu3/algorithms/agc.cpp\n> > @@ -6,6 +6,7 @@\n> >   */\n> >  \n> >  #include \"agc.h\"\n> > +#include \"awb.h\"\n> >  \n> >  #include <algorithm>\n> >  #include <chrono>\n> > @@ -47,9 +48,6 @@ static constexpr uint32_t kMaxExposure = 1976;\n> >  static constexpr uint32_t knumHistogramBins = 256;\n> >  static constexpr double kEvGainTarget = 0.5;\n> >  \n> > -/* A cell is 8 bytes and contains averages for RGB values and saturation ratio */\n> > -static constexpr uint8_t kCellSize = 8;\n> > -\n> >  Agc::Agc()\n> >  \t: frameCount_(0), lastFrame_(0), iqMean_(0.0), lineDuration_(0s),\n> >  \t  maxExposureTime_(0s), prevExposure_(0s), prevExposureNoDg_(0s),\n> > @@ -57,9 +55,10 @@ Agc::Agc()\n> >  {\n> >  }\n> >  \n> > -int Agc::configure([[maybe_unused]] IPAContext &context,\n> > -\t\t   const IPAConfigInfo &configInfo)\n> > +int Agc::configure(IPAContext &context, const IPAConfigInfo &configInfo)\n> >  {\n> > +\tstride_ = context.configuration.grid.stride;\n> > +\n> >  \tlineDuration_ = configInfo.sensorInfo.lineLength * 1.0s\n> >  \t\t      / configInfo.sensorInfo.pixelRate;\n> >  \tmaxExposureTime_ = kMaxExposure * lineDuration_;\n> > @@ -70,37 +69,23 @@ int Agc::configure([[maybe_unused]] IPAContext &context,\n> >  void Agc::processBrightness(const ipu3_uapi_stats_3a *stats,\n> >  \t\t\t    const ipu3_uapi_grid_config &grid)\n> >  {\n> > -\tconst struct ipu3_uapi_grid_config statsAeGrid = stats->stats_4a_config.awb_config.grid;\n> > -\tRectangle aeRegion = { statsAeGrid.x_start,\n> > -\t\t\t       statsAeGrid.y_start,\n> > -\t\t\t       static_cast<unsigned int>(statsAeGrid.x_end - statsAeGrid.x_start) + 1,\n> > -\t\t\t       static_cast<unsigned int>(statsAeGrid.y_end - statsAeGrid.y_start) + 1 };\n> > -\tPoint topleft = aeRegion.topLeft();\n> > -\tint topleftX = topleft.x >> grid.block_width_log2;\n> > -\tint topleftY = topleft.y >> grid.block_height_log2;\n> > -\n> > -\t/* Align to the grid cell width and height */\n> > -\tuint32_t startX = topleftX << grid.block_width_log2;\n> > -\tuint32_t startY = topleftY * grid.width << grid.block_width_log2;\n> > -\tuint32_t endX = (startX + (aeRegion.size().width >> grid.block_width_log2)) << grid.block_width_log2;\n> > -\tuint32_t i, j;\n> > -\tuint32_t count = 0;\n> > -\n> >  \tuint32_t hist[knumHistogramBins] = { 0 };\n> > -\tfor (j = topleftY;\n> > -\t     j < topleftY + (aeRegion.size().height >> grid.block_height_log2);\n> > -\t     j++) {\n> > -\t\tfor (i = startX + startY; i < endX + startY; i += kCellSize) {\n> > -\t\t\t/*\n> > -\t\t\t * The grid width (and maybe height) is not reliable.\n> > -\t\t\t * We observed a bit shift which makes the value 160 to be 32 in the stats grid.\n> > -\t\t\t * Use the one passed at init time.\n> > -\t\t\t */\n> > -\t\t\tif (stats->awb_raw_buffer.meta_data[i + 4 + j * grid.width] == 0) {\n> > -\t\t\t\tuint8_t Gr = stats->awb_raw_buffer.meta_data[i + 0 + j * grid.width];\n> > -\t\t\t\tuint8_t Gb = stats->awb_raw_buffer.meta_data[i + 3 + j * grid.width];\n> > -\t\t\t\thist[(Gr + Gb) / 2]++;\n> > -\t\t\t\tcount++;\n> > +\n> > +\tfor (unsigned int cellY = 0; cellY < grid.height; cellY++) {\n> > +\t\tfor (unsigned int cellX = 0; cellX < grid.width; cellX++) {\n> > +\t\t\tuint32_t cellPosition = cellY * stride_ + cellX\n> > +\t\t\t\t\t      * sizeof(Ipu3AwbCell);\n\n\t\t\tuint32_t cellPosition = (cellY * stride_ + cellX)\n\t\t\t\t\t      * sizeof(Ipu3AwbCell);\n\nLikely went unnoticed because the next patch modifies this.\n\n> > +\n> > +\t\t\t/* Cast the initial IPU3 structure to simplify the reading */\n> \n> I'm not sure that comment adds much, but it doesn't hurt anything.\n\nIndeed, I would drop it too.\n\n> Stride issues are all fixed up, and I think the Ipu3AwbCell gets changed\n> to the new structure in the next patch...\n> \n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> > +\t\t\tconst Ipu3AwbCell *cell =\n> > +\t\t\t\treinterpret_cast<const Ipu3AwbCell *>(\n> > +\t\t\t\t\t&stats->awb_raw_buffer.meta_data[cellPosition]\n> > +\t\t\t\t);\n> > +\n> > +\t\t\tif (cell->satRatio == 0) {\n> > +\t\t\t\tuint8_t gr = cell->greenRedAvg;\n> > +\t\t\t\tuint8_t gb = cell->greenBlueAvg;\n> > +\t\t\t\thist[(gr + gb) / 2]++;\n> >  \t\t\t}\n> >  \t\t}\n> >  \t}\n> > diff --git a/src/ipa/ipu3/algorithms/agc.h b/src/ipa/ipu3/algorithms/agc.h\n> > index e36797d3..64b71c65 100644\n> > --- a/src/ipa/ipu3/algorithms/agc.h\n> > +++ b/src/ipa/ipu3/algorithms/agc.h\n> > @@ -50,6 +50,8 @@ private:\n> >  \tDuration prevExposureNoDg_;\n> >  \tDuration currentExposure_;\n> >  \tDuration currentExposureNoDg_;\n> > +\n> > +\tuint32_t stride_;\n> >  };\n> >  \n> >  } /* namespace ipa::ipu3::algorithms */","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 F41D1BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  5 Oct 2021 01:38:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6A173691B6;\n\tTue,  5 Oct 2021 03:38:16 +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 6146F69189\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  5 Oct 2021 03:38:15 +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 BB26525B;\n\tTue,  5 Oct 2021 03:38:14 +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=\"EL55UCGh\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1633397894;\n\tbh=t624DCSiRMVuRlvtmRT0x/dTjYvCWbVAsNyR65cm0FE=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=EL55UCGhv3taiD+0i9fGhj/U+OSTrLL0qAVIW7SBFstr52oLHKz8YghgGB6L4G6kY\n\tqwv/MhY2YVR4zw9/LJ1KJQVfyyQh4D5gf1ZGUI2Q3K+w19FvfTPkVAf4Ojl8Gyh2zS\n\tPY9fHNbDMPHxGJ9LlFMiyUcDqUpjH5KOwFApQPOQ=","Date":"Tue, 5 Oct 2021 04:38:08 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>","Message-ID":"<YVusgP19EZMIRWPt@pendragon.ideasonboard.com>","References":"<20210930093715.73293-1-jeanmichel.hautbois@ideasonboard.com>\n\t<20210930093715.73293-12-jeanmichel.hautbois@ideasonboard.com>\n\t<d83172b9-fab8-ee72-0f8f-721ea1bde17e@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<d83172b9-fab8-ee72-0f8f-721ea1bde17e@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2 11/12] ipa: ipu3: agc: Rewrite and\n\tsimplify the brightness loop","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>"}}]