{"id":12062,"url":"https://patchwork.libcamera.org/api/1.1/patches/12062/?format=json","web_url":"https://patchwork.libcamera.org/patch/12062/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210422051857.20594-4-jeanmichel.hautbois@ideasonboard.com>","date":"2021-04-22T05:18:56","name":"[libcamera-devel,v6,3/4] ipa: ipu3: Add support for IPU3 AWB algorithm","commit_ref":"b2ddc9b11815976ec0bdf741fded993b8bc9ef4d","pull_url":null,"state":"accepted","archived":false,"hash":"894781ac25c7edba03b10c716e2f3f8e1773deeb","submitter":{"id":75,"url":"https://patchwork.libcamera.org/api/1.1/people/75/?format=json","name":"Jean-Michel Hautbois","email":"jeanmichel.hautbois@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/12062/mbox/","series":[{"id":1960,"url":"https://patchwork.libcamera.org/api/1.1/series/1960/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1960","date":"2021-04-22T05:18:53","name":"Implement IPA algorithms and demo with IPU3","version":6,"mbox":"https://patchwork.libcamera.org/series/1960/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12062/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12062/checks/","tags":{},"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 DE843BDB15\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 22 Apr 2021 05:19:04 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 94A1568861;\n\tThu, 22 Apr 2021 07:19:03 +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 BE1B060516\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 22 Apr 2021 07:19:01 +0200 (CEST)","from localhost.localdomain (unknown\n\t[IPv6:2a01:e0a:169:7140:e96c:3f42:9018:e1b2])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4BC21A5E;\n\tThu, 22 Apr 2021 07:19:01 +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=\"cbTcBkL8\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1619068741;\n\tbh=USloWZ+wer43yGafbV7Q4b14tGqbpRO5KKMDWkQ1bko=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=cbTcBkL8boEFUaUR5+/wNQfkgGpI7b9lI3MgLjQHXgh3pIgVOt5cVIIRA0czrj1/1\n\tucNP08TneHgKpDUT0NWdt2b9o77NNgwah+IUidFrplQVvL5CqMaIQhr2Ll77xtLSU8\n\tSuyhimNDHRs6YZs99fr3awHKoAN7NOHa4vQ/tpvo=","From":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 22 Apr 2021 07:18:56 +0200","Message-Id":"<20210422051857.20594-4-jeanmichel.hautbois@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20210422051857.20594-1-jeanmichel.hautbois@ideasonboard.com>","References":"<20210422051857.20594-1-jeanmichel.hautbois@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v6 3/4] ipa: ipu3: Add support for IPU3\n\tAWB algorithm","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The IPA will locally modify the parameters before they are passed down\nto the ImgU. Use a local parameter object to give a reference to those\nalgorithms.\n\nInherit from the Algorithm class to implement basic AWB functions.\n\nThe configure() call will set exposure and gain to their minimum value,\nso while AGC is not there, the frames will be dark.\n\nOnce AWB is done, a color temperature is estimated and a default CCM matrix\nwill be used (yet to be tuned).\nImplement a basic \"grey-world\" AWB algorithm just for demonstration purpose.\n\nThe BDS output size is passed by the pipeline handler to the IPA.\nThe best grid is then calculated to maximize the number of pixels taken\ninto account in each cells.\n\nAs commented in the source code, it can be improved, as it has (at least)\none limitation: if a cell is big (say 128 pixels wide) and indicated as\nsaturated, it won't be taken into account at all.\nMaybe is it possible to have a smaller one, at the cost of a few pixels\nto lose, in which case we can center the grid using the x_start and\ny_start parameters.\n\nSigned-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nTested-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/ipa/ipu3/ipu3.cpp     |  86 ++++++++-\n src/ipa/ipu3/ipu3_awb.cpp | 356 ++++++++++++++++++++++++++++++++++++++\n src/ipa/ipu3/ipu3_awb.h   |  91 ++++++++++\n src/ipa/ipu3/meson.build  |   7 +-\n 4 files changed, 531 insertions(+), 9 deletions(-)\n create mode 100644 src/ipa/ipu3/ipu3_awb.cpp\n create mode 100644 src/ipa/ipu3/ipu3_awb.h","diff":"diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp\nindex 38477cad..a4d327ab 100644\n--- a/src/ipa/ipu3/ipu3.cpp\n+++ b/src/ipa/ipu3/ipu3.cpp\n@@ -21,6 +21,11 @@\n #include \"libcamera/internal/buffer.h\"\n #include \"libcamera/internal/log.h\"\n \n+#include \"ipu3_awb.h\"\n+\n+static constexpr uint32_t kMaxCellWidthPerSet = 160;\n+static constexpr uint32_t kMaxCellHeightPerSet = 56;\n+\n namespace libcamera {\n \n LOG_DEFINE_CATEGORY(IPAIPU3)\n@@ -51,6 +56,7 @@ private:\n \t\t\t     const ipu3_uapi_stats_3a *stats);\n \n \tvoid setControls(unsigned int frame);\n+\tvoid calculateBdsGrid(const Size &bdsOutputSize);\n \n \tstd::map<unsigned int, MappedFrameBuffer> buffers_;\n \n@@ -63,6 +69,14 @@ private:\n \tuint32_t gain_;\n \tuint32_t minGain_;\n \tuint32_t maxGain_;\n+\n+\t/* Interface to the AWB algorithm */\n+\tstd::unique_ptr<IPU3Awb> awbAlgo_;\n+\n+\t/* Local parameter storage */\n+\tstruct ipu3_uapi_params params_;\n+\n+\tstruct ipu3_uapi_grid_config bdsGrid_;\n };\n \n int IPAIPU3::start()\n@@ -72,8 +86,58 @@ int IPAIPU3::start()\n \treturn 0;\n }\n \n+/**\n+ * This method 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+ * 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+\tbdsGrid_ = {};\n+\n+\tfor (uint32_t widthShift = 3; widthShift <= 7; ++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 <= 7; ++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+\t}\n+\n+\tbdsGrid_.width = best.width >> bestLog2.width;\n+\tbdsGrid_.block_width_log2 = bestLog2.width;\n+\tbdsGrid_.height = best.height >> bestLog2.height;\n+\tbdsGrid_.block_height_log2 = bestLog2.height;\n+\n+\tLOG(IPAIPU3, Debug) << \"Best grid found is: (\"\n+\t\t\t    << (int)bdsGrid_.width << \" << \" << (int)bdsGrid_.block_width_log2 << \") x (\"\n+\t\t\t    << (int)bdsGrid_.height << \" << \" << (int)bdsGrid_.block_height_log2 << \")\";\n+}\n+\n void IPAIPU3::configure(const std::map<uint32_t, ControlInfoMap> &entityControls,\n-\t\t\t[[maybe_unused]] const Size &bdsOutputSize)\n+\t\t\tconst Size &bdsOutputSize)\n {\n \tif (entityControls.empty())\n \t\treturn;\n@@ -94,11 +158,18 @@ void IPAIPU3::configure(const std::map<uint32_t, ControlInfoMap> &entityControls\n \n \tminExposure_ = std::max(itExp->second.min().get<int32_t>(), 1);\n \tmaxExposure_ = itExp->second.max().get<int32_t>();\n-\texposure_ = maxExposure_;\n+\texposure_ = minExposure_;\n \n \tminGain_ = std::max(itGain->second.min().get<int32_t>(), 1);\n \tmaxGain_ = itGain->second.max().get<int32_t>();\n-\tgain_ = maxGain_;\n+\tgain_ = minGain_;\n+\n+\tparams_ = {};\n+\n+\tcalculateBdsGrid(bdsOutputSize);\n+\n+\tawbAlgo_ = std::make_unique<IPU3Awb>();\n+\tawbAlgo_->initialise(params_, bdsOutputSize, bdsGrid_);\n }\n \n void IPAIPU3::mapBuffers(const std::vector<IPABuffer> &buffers)\n@@ -170,10 +241,10 @@ void IPAIPU3::processControls([[maybe_unused]] unsigned int frame,\n \n void IPAIPU3::fillParams(unsigned int frame, ipu3_uapi_params *params)\n {\n-\t/* Prepare parameters buffer. */\n-\tmemset(params, 0, sizeof(*params));\n+\t/* Pass a default gamma of 1.0 (default linear correction) */\n+\tawbAlgo_->updateWbParameters(params_, 1.0);\n \n-\t/* \\todo Fill in parameters buffer. */\n+\t*params = params_;\n \n \tIPU3Action op;\n \top.op = ActionParamFilled;\n@@ -186,8 +257,7 @@ void IPAIPU3::parseStatistics(unsigned int frame,\n {\n \tControlList ctrls(controls::controls);\n \n-\t/* \\todo React to statistics and update internal state machine. */\n-\t/* \\todo Add meta-data information to ctrls. */\n+\tawbAlgo_->calculateWBGains(stats);\n \n \tIPU3Action op;\n \top.op = ActionMetadataReady;\ndiff --git a/src/ipa/ipu3/ipu3_awb.cpp b/src/ipa/ipu3/ipu3_awb.cpp\nnew file mode 100644\nindex 00000000..060d1e18\n--- /dev/null\n+++ b/src/ipa/ipu3/ipu3_awb.cpp\n@@ -0,0 +1,356 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Ideas On Board\n+ *\n+ * ipu3_awb.cpp - AWB control algorithm\n+ */\n+#include \"ipu3_awb.h\"\n+\n+#include <cmath>\n+#include <numeric>\n+#include <unordered_map>\n+\n+#include \"libcamera/internal/log.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa::ipu3 {\n+\n+LOG_DEFINE_CATEGORY(IPU3Awb)\n+\n+static constexpr uint32_t kMinZonesCounted = 16;\n+static constexpr uint32_t kMinGreenLevelInZone = 32;\n+\n+/**\n+ * \\struct IspStatsRegion\n+ * \\brief RGB statistics for a given region\n+ *\n+ * The IspStatsRegion structure is intended to abstract the ISP specific\n+ * statistics and use an agnostic algorithm to compute AWB.\n+ *\n+ * \\var IspStatsRegion::counted\n+ * \\brief Number of pixels used to calculate the sums\n+ *\n+ * \\var IspStatsRegion::uncounted\n+ * \\brief Remaining number of pixels in the region\n+ *\n+ * \\var IspStatsRegion::rSum\n+ * \\brief Sum of the red values in the region\n+ *\n+ * \\var IspStatsRegion::gSum\n+ * \\brief Sum of the green values in the region\n+ *\n+ * \\var IspStatsRegion::bSum\n+ * \\brief Sum of the blue values in the region\n+ */\n+\n+/**\n+ * \\struct AwbStatus\n+ * \\brief AWB parameters calculated\n+ *\n+ * The AwbStatus structure is intended to store the AWB\n+ * parameters calculated by the algorithm\n+ *\n+ * \\var AwbStatus::temperatureK\n+ * \\brief Color temperature calculated\n+ *\n+ * \\var AwbStatus::redGain\n+ * \\brief Gain calculated for the red channel\n+ *\n+ * \\var AwbStatus::greenGain\n+ * \\brief Gain calculated for the green channel\n+ *\n+ * \\var AwbStatus::blueGain\n+ * \\brief Gain calculated for the blue channel\n+ */\n+\n+/**\n+ * \\struct Ipu3AwbCell\n+ * \\brief Memory layout for each cell in AWB metadata\n+ *\n+ * The Ipu3AwbCell structure is used to get individual values\n+ * such as red average or saturation ratio in a particular cell.\n+ *\n+ * \\var Ipu3AwbCell::greenRedAvg\n+ * \\brief Green average for red lines in the cell\n+ *\n+ * \\var Ipu3AwbCell::redAvg\n+ * \\brief Red average in the cell\n+ *\n+ * \\var Ipu3AwbCell::blueAvg\n+ * \\brief blue average in the cell\n+ *\n+ * \\var Ipu3AwbCell::greenBlueAvg\n+ * \\brief Green average for blue lines\n+ *\n+ * \\var Ipu3AwbCell::satRatio\n+ * \\brief Saturation ratio in the cell\n+ *\n+ * \\var Ipu3AwbCell::padding\n+ * \\brief array of unused bytes for padding\n+ */\n+\n+/* Default settings for Bayer noise reduction replicated from the Kernel */\n+static const struct ipu3_uapi_bnr_static_config imguCssBnrDefaults = {\n+\t.wb_gains = { 16, 16, 16, 16 },\n+\t.wb_gains_thr = { 255, 255, 255, 255 },\n+\t.thr_coeffs = { 1700, 0, 31, 31, 0, 16 },\n+\t.thr_ctrl_shd = { 26, 26, 26, 26 },\n+\t.opt_center{ -648, 0, -366, 0 },\n+\t.lut = {\n+\t\t{ 17, 23, 28, 32, 36, 39, 42, 45,\n+\t\t  48, 51, 53, 55, 58, 60, 62, 64,\n+\t\t  66, 68, 70, 72, 73, 75, 77, 78,\n+\t\t  80, 82, 83, 85, 86, 88, 89, 90 } },\n+\t.bp_ctrl = { 20, 0, 1, 40, 0, 6, 0, 6, 0 },\n+\t.dn_detect_ctrl{ 9, 3, 4, 0, 8, 0, 1, 1, 1, 1, 0 },\n+\t.column_size = 1296,\n+\t.opt_center_sqr = { 419904, 133956 },\n+};\n+\n+/* Default settings for Auto White Balance replicated from the Kernel*/\n+static const struct ipu3_uapi_awb_config_s imguCssAwbDefaults = {\n+\t.rgbs_thr_gr = 8191,\n+\t.rgbs_thr_r = 8191,\n+\t.rgbs_thr_gb = 8191,\n+\t.rgbs_thr_b = 8191 | IPU3_UAPI_AWB_RGBS_THR_B_EN | IPU3_UAPI_AWB_RGBS_THR_B_INCL_SAT,\n+\t.grid = {\n+\t\t.width = 160,\n+\t\t.height = 36,\n+\t\t.block_width_log2 = 3,\n+\t\t.block_height_log2 = 4,\n+\t\t.height_per_slice = 1, /* Overridden by kernel. */\n+\t\t.x_start = 0,\n+\t\t.y_start = 0,\n+\t\t.x_end = 0,\n+\t\t.y_end = 0,\n+\t},\n+};\n+\n+/* Default color correction matrix defined as an identity matrix */\n+static const struct ipu3_uapi_ccm_mat_config imguCssCcmDefault = {\n+\t8191, 0, 0, 0,\n+\t0, 8191, 0, 0,\n+\t0, 0, 8191, 0\n+};\n+\n+IPU3Awb::IPU3Awb()\n+\t: Algorithm()\n+{\n+\tasyncResults_.blueGain = 1.0;\n+\tasyncResults_.greenGain = 1.0;\n+\tasyncResults_.redGain = 1.0;\n+\tasyncResults_.temperatureK = 4500;\n+}\n+\n+IPU3Awb::~IPU3Awb()\n+{\n+}\n+\n+void IPU3Awb::initialise(ipu3_uapi_params &params, const Size &bdsOutputSize, struct ipu3_uapi_grid_config &bdsGrid)\n+{\n+\tparams.use.acc_awb = 1;\n+\tparams.acc_param.awb.config = imguCssAwbDefaults;\n+\n+\tawbGrid_ = bdsGrid;\n+\tparams.acc_param.awb.config.grid = awbGrid_;\n+\n+\tparams.use.acc_bnr = 1;\n+\tparams.acc_param.bnr = imguCssBnrDefaults;\n+\t/**\n+\t * Optical center is column (respectively row) startminus X (respectively Y) center.\n+\t * For the moment use BDS as a first approximation, but it should\n+\t * be calculated based on Shading (SHD) parameters.\n+\t */\n+\tparams.acc_param.bnr.column_size = bdsOutputSize.width;\n+\tparams.acc_param.bnr.opt_center.x_reset = awbGrid_.x_start - (bdsOutputSize.width / 2);\n+\tparams.acc_param.bnr.opt_center.y_reset = awbGrid_.y_start - (bdsOutputSize.height / 2);\n+\tparams.acc_param.bnr.opt_center_sqr.x_sqr_reset = params.acc_param.bnr.opt_center.x_reset\n+\t\t\t\t\t\t\t* params.acc_param.bnr.opt_center.x_reset;\n+\tparams.acc_param.bnr.opt_center_sqr.y_sqr_reset = params.acc_param.bnr.opt_center.y_reset\n+\t\t\t\t\t\t\t* params.acc_param.bnr.opt_center.y_reset;\n+\n+\tparams.use.acc_ccm = 1;\n+\tparams.acc_param.ccm = imguCssCcmDefault;\n+\n+\tparams.use.acc_gamma = 1;\n+\tparams.acc_param.gamma.gc_ctrl.enable = 1;\n+\n+\tzones_.reserve(kAwbStatsSizeX * kAwbStatsSizeY);\n+}\n+\n+/**\n+ * The function estimates the correlated color temperature using\n+ * from RGB color space input.\n+ * In physics and color science, the Planckian locus or black body locus is\n+ * the path or locus that the color of an incandescent black body would take\n+ * in a particular chromaticity space as the blackbody temperature changes.\n+ *\n+ * If a narrow range of color temperatures is considered (those encapsulating\n+ * daylight being the most practical case) one can approximate the Planckian\n+ * locus in order to calculate the CCT in terms of chromaticity coordinates.\n+ *\n+ * More detailed information can be found in:\n+ * https://en.wikipedia.org/wiki/Color_temperature#Approximation\n+ */\n+uint32_t IPU3Awb::estimateCCT(double red, double green, double blue)\n+{\n+\t/* Convert the RGB values to CIE tristimulus values (XYZ) */\n+\tdouble X = (-0.14282) * (red) + (1.54924) * (green) + (-0.95641) * (blue);\n+\tdouble Y = (-0.32466) * (red) + (1.57837) * (green) + (-0.73191) * (blue);\n+\tdouble Z = (-0.68202) * (red) + (0.77073) * (green) + (0.56332) * (blue);\n+\n+\t/* Calculate the normalized chromaticity values */\n+\tdouble x = X / (X + Y + Z);\n+\tdouble y = Y / (X + Y + Z);\n+\n+\t/* Calculate CCT */\n+\tdouble n = (x - 0.3320) / (0.1858 - y);\n+\treturn 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33;\n+}\n+\n+/* Generate an RGB vector with the average values for each region */\n+void IPU3Awb::generateZones(std::vector<RGB> &zones)\n+{\n+\tfor (unsigned int i = 0; i < kAwbStatsSizeX * kAwbStatsSizeY; i++) {\n+\t\tRGB zone;\n+\t\tdouble counted = awbStats_[i].counted;\n+\t\tif (counted >= kMinZonesCounted) {\n+\t\t\tzone.G = awbStats_[i].gSum / counted;\n+\t\t\tif (zone.G >= kMinGreenLevelInZone) {\n+\t\t\t\tzone.R = awbStats_[i].rSum / counted;\n+\t\t\t\tzone.B = awbStats_[i].bSum / counted;\n+\t\t\t\tzones.push_back(zone);\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n+\n+/* Translate the IPU3 statistics into the default statistics region array */\n+void IPU3Awb::generateAwbStats(const ipu3_uapi_stats_3a *stats)\n+{\n+\tuint32_t regionWidth = round(awbGrid_.width / static_cast<double>(kAwbStatsSizeX));\n+\tuint32_t regionHeight = round(awbGrid_.height / static_cast<double>(kAwbStatsSizeY));\n+\n+\t/*\n+\t * Generate a (kAwbStatsSizeX x kAwbStatsSizeY) array from the IPU3 grid which is\n+\t * (awbGrid_.width x awbGrid_.height).\n+\t */\n+\tfor (unsigned int j = 0; j < kAwbStatsSizeY * regionHeight; j++) {\n+\t\tfor (unsigned int i = 0; i < kAwbStatsSizeX * regionWidth; i++) {\n+\t\t\tuint32_t cellPosition = j * awbGrid_.width + i;\n+\t\t\tuint32_t cellX = (cellPosition / regionWidth) % kAwbStatsSizeX;\n+\t\t\tuint32_t cellY = ((cellPosition / awbGrid_.width) / regionHeight) % kAwbStatsSizeY;\n+\n+\t\t\tuint32_t awbRegionPosition = cellY * kAwbStatsSizeX + cellX;\n+\t\t\tcellPosition *= 8;\n+\n+\t\t\t/* Cast the initial IPU3 structure to simplify the reading */\n+\t\t\tIpu3AwbCell *currentCell = reinterpret_cast<Ipu3AwbCell *>(const_cast<uint8_t *>(&stats->awb_raw_buffer.meta_data[cellPosition]));\n+\t\t\tif (currentCell->satRatio == 0) {\n+\t\t\t\t/* The cell is not saturated, use the current cell */\n+\t\t\t\tawbStats_[awbRegionPosition].counted++;\n+\t\t\t\tuint32_t greenValue = currentCell->greenRedAvg + currentCell->greenBlueAvg;\n+\t\t\t\tawbStats_[awbRegionPosition].gSum += greenValue / 2;\n+\t\t\t\tawbStats_[awbRegionPosition].rSum += currentCell->redAvg;\n+\t\t\t\tawbStats_[awbRegionPosition].bSum += currentCell->blueAvg;\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n+\n+void IPU3Awb::clearAwbStats()\n+{\n+\tfor (unsigned int i = 0; i < kAwbStatsSizeX * kAwbStatsSizeY; i++) {\n+\t\tawbStats_[i].bSum = 0;\n+\t\tawbStats_[i].rSum = 0;\n+\t\tawbStats_[i].gSum = 0;\n+\t\tawbStats_[i].counted = 0;\n+\t\tawbStats_[i].uncounted = 0;\n+\t}\n+}\n+\n+void IPU3Awb::awbGreyWorld()\n+{\n+\tLOG(IPU3Awb, Debug) << \"Grey world AWB\";\n+\t/*\n+\t * Make a separate list of the derivatives for each of red and blue, so\n+\t * that we can sort them to exclude the extreme gains. We could\n+\t * consider some variations, such as normalising all the zones first, or\n+\t * doing an L2 average etc.\n+\t */\n+\tstd::vector<RGB> &redDerivative(zones_);\n+\tstd::vector<RGB> blueDerivative(redDerivative);\n+\tstd::sort(redDerivative.begin(), redDerivative.end(),\n+\t\t  [](RGB const &a, RGB const &b) {\n+\t\t\t  return a.G * b.R < b.G * a.R;\n+\t\t  });\n+\tstd::sort(blueDerivative.begin(), blueDerivative.end(),\n+\t\t  [](RGB const &a, RGB const &b) {\n+\t\t\t  return a.G * b.B < b.G * a.B;\n+\t\t  });\n+\n+\t/* Average the middle half of the values. */\n+\tint discard = redDerivative.size() / 4;\n+\n+\tRGB sumRed(0, 0, 0);\n+\tRGB sumBlue(0, 0, 0);\n+\tfor (auto ri = redDerivative.begin() + discard,\n+\t\t  bi = blueDerivative.begin() + discard;\n+\t     ri != redDerivative.end() - discard; ri++, bi++)\n+\t\tsumRed += *ri, sumBlue += *bi;\n+\n+\tdouble redGain = sumRed.G / (sumRed.R + 1),\n+\t       blueGain = sumBlue.G / (sumBlue.B + 1);\n+\n+\t/* Color temperature is not relevant in Grey world but still useful to estimate it :-) */\n+\tasyncResults_.temperatureK = estimateCCT(sumRed.R, sumRed.G, sumBlue.B);\n+\tasyncResults_.redGain = redGain;\n+\tasyncResults_.greenGain = 1.0;\n+\tasyncResults_.blueGain = blueGain;\n+}\n+\n+void IPU3Awb::calculateWBGains(const ipu3_uapi_stats_3a *stats)\n+{\n+\tASSERT(stats->stats_3a_status.awb_en);\n+\tzones_.clear();\n+\tclearAwbStats();\n+\tgenerateAwbStats(stats);\n+\tgenerateZones(zones_);\n+\tLOG(IPU3Awb, Debug) << \"Valid zones: \" << zones_.size();\n+\tif (zones_.size() > 10) {\n+\t\tawbGreyWorld();\n+\t\tLOG(IPU3Awb, Debug) << \"Gain found for red: \" << asyncResults_.redGain\n+\t\t\t\t    << \" and for blue: \" << asyncResults_.blueGain;\n+\t}\n+}\n+\n+void IPU3Awb::updateWbParameters(ipu3_uapi_params &params, double agcGamma)\n+{\n+\t/*\n+\t * Green gains should not be touched and considered 1.\n+\t * Default is 16, so do not change it at all.\n+\t * 4096 is the value for a gain of 1.0\n+\t */\n+\tparams.acc_param.bnr.wb_gains.gr = 16;\n+\tparams.acc_param.bnr.wb_gains.r = 4096 * asyncResults_.redGain;\n+\tparams.acc_param.bnr.wb_gains.b = 4096 * asyncResults_.blueGain;\n+\tparams.acc_param.bnr.wb_gains.gb = 16;\n+\n+\tLOG(IPU3Awb, Debug) << \"Color temperature estimated: \" << asyncResults_.temperatureK\n+\t\t\t    << \" and gamma calculated: \" << agcGamma;\n+\n+\t/* The CCM matrix may change when color temperature will be used */\n+\tparams.acc_param.ccm = imguCssCcmDefault;\n+\n+\tfor (uint32_t i = 0; i < 256; i++) {\n+\t\tdouble j = i / 255.0;\n+\t\tdouble gamma = std::pow(j, 1.0 / agcGamma);\n+\t\t/* The maximum value 255 is represented on 13 bits in the IPU3 */\n+\t\tparams.acc_param.gamma.gc_lut.lut[i] = gamma * 8191;\n+\t}\n+}\n+\n+} /* namespace ipa::ipu3 */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/ipu3/ipu3_awb.h b/src/ipa/ipu3/ipu3_awb.h\nnew file mode 100644\nindex 00000000..122cf68c\n--- /dev/null\n+++ b/src/ipa/ipu3/ipu3_awb.h\n@@ -0,0 +1,91 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Ideas On Board\n+ *\n+ * ipu3_awb.h - IPU3 AWB control algorithm\n+ */\n+#ifndef __LIBCAMERA_IPU3_AWB_H__\n+#define __LIBCAMERA_IPU3_AWB_H__\n+\n+#include <vector>\n+\n+#include <linux/intel-ipu3.h>\n+\n+#include <libcamera/geometry.h>\n+\n+#include \"libipa/algorithm.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa::ipu3 {\n+\n+/* Region size for the statistics generation algorithm */\n+static constexpr uint32_t kAwbStatsSizeX = 16;\n+static constexpr uint32_t kAwbStatsSizeY = 12;\n+\n+class IPU3Awb : public Algorithm\n+{\n+public:\n+\tIPU3Awb();\n+\t~IPU3Awb();\n+\n+\tvoid initialise(ipu3_uapi_params &params, const Size &bdsOutputSize, struct ipu3_uapi_grid_config &bdsGrid);\n+\tvoid calculateWBGains(const ipu3_uapi_stats_3a *stats);\n+\tvoid updateWbParameters(ipu3_uapi_params &params, double agcGamma);\n+\n+\tstruct Ipu3AwbCell {\n+\t\tunsigned char greenRedAvg;\n+\t\tunsigned char redAvg;\n+\t\tunsigned char blueAvg;\n+\t\tunsigned char greenBlueAvg;\n+\t\tunsigned char satRatio;\n+\t\tunsigned char padding[3];\n+\t} __attribute__((packed));\n+\n+\t/* \\todo Make these three structs available to all the ISPs ? */\n+\tstruct RGB {\n+\t\tRGB(double _R = 0, double _G = 0, double _B = 0)\n+\t\t\t: R(_R), G(_G), B(_B)\n+\t\t{\n+\t\t}\n+\t\tdouble R, G, B;\n+\t\tRGB &operator+=(RGB const &other)\n+\t\t{\n+\t\t\tR += other.R, G += other.G, B += other.B;\n+\t\t\treturn *this;\n+\t\t}\n+\t};\n+\n+\tstruct IspStatsRegion {\n+\t\tunsigned int counted;\n+\t\tunsigned int uncounted;\n+\t\tunsigned long long rSum;\n+\t\tunsigned long long gSum;\n+\t\tunsigned long long bSum;\n+\t};\n+\n+\tstruct AwbStatus {\n+\t\tdouble temperatureK;\n+\t\tdouble redGain;\n+\t\tdouble greenGain;\n+\t\tdouble blueGain;\n+\t};\n+\n+private:\n+\tvoid generateZones(std::vector<RGB> &zones);\n+\tvoid generateAwbStats(const ipu3_uapi_stats_3a *stats);\n+\tvoid clearAwbStats();\n+\tvoid awbGreyWorld();\n+\tuint32_t estimateCCT(double red, double green, double blue);\n+\n+\tstruct ipu3_uapi_grid_config awbGrid_;\n+\n+\tstd::vector<RGB> zones_;\n+\tIspStatsRegion awbStats_[kAwbStatsSizeX * kAwbStatsSizeY];\n+\tAwbStatus asyncResults_;\n+};\n+\n+} /* namespace ipa::ipu3 */\n+\n+} /* namespace libcamera*/\n+#endif /* __LIBCAMERA_IPU3_AWB_H__ */\ndiff --git a/src/ipa/ipu3/meson.build b/src/ipa/ipu3/meson.build\nindex a241f617..1040698e 100644\n--- a/src/ipa/ipu3/meson.build\n+++ b/src/ipa/ipu3/meson.build\n@@ -2,8 +2,13 @@\n \n ipa_name = 'ipa_ipu3'\n \n+ipu3_ipa_sources = files([\n+    'ipu3.cpp',\n+    'ipu3_awb.cpp',\n+])\n+\n mod = shared_module(ipa_name,\n-                    ['ipu3.cpp', libcamera_generated_ipa_headers],\n+                    [ipu3_ipa_sources, libcamera_generated_ipa_headers],\n                     name_prefix : '',\n                     include_directories : [ipa_includes, libipa_includes],\n                     dependencies : libcamera_dep,\n","prefixes":["libcamera-devel","v6","3/4"]}