Patch Detail
Show a patch.
GET /api/patches/11801/?format=api
{ "id": 11801, "url": "https://patchwork.libcamera.org/api/patches/11801/?format=api", "web_url": "https://patchwork.libcamera.org/patch/11801/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20210330211210.194806-4-jeanmichel.hautbois@ideasonboard.com>", "date": "2021-03-30T21:12:09", "name": "[libcamera-devel,v4,3/4] ipa: ipu3: Add support for IPU3 AWB algorithm", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "7c129bdf3bed43624a9065eb0553e089de60936d", "submitter": { "id": 75, "url": "https://patchwork.libcamera.org/api/people/75/?format=api", "name": "Jean-Michel Hautbois", "email": "jeanmichel.hautbois@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/11801/mbox/", "series": [ { "id": 1874, "url": "https://patchwork.libcamera.org/api/series/1874/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=1874", "date": "2021-03-30T21:12:06", "name": "Implement IPA algorithms and demo with IPU3", "version": 4, "mbox": "https://patchwork.libcamera.org/series/1874/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/11801/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/11801/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 478FDC0DA3\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 30 Mar 2021 21:12:23 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0677768790;\n\tTue, 30 Mar 2021 23:12:23 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 16367602D2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 30 Mar 2021 23:12:20 +0200 (CEST)", "from localhost.localdomain (unknown\n\t[IPv6:2a01:e0a:169:7140:40dc:d947:395c:88d2])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B5B03292;\n\tTue, 30 Mar 2021 23:12:19 +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=\"N7Vp0a5A\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1617138739;\n\tbh=hA4OiaEjRNq34LHwfMkDrAjN6FgB4GY3i7Q+YhPEF2E=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=N7Vp0a5AO3/T2EAMegIuqW3M5Yu6H3HTmLEAtwgwl60UComtO1DuQYy++BLqGYQvI\n\tcjju+DKkdh8+YVjJGgGcGqUYmrqw+zohAou5ZY4rdqu3CA8fVosf2L1y1OUZFl3sa9\n\t454kJeSnP7KD8XJvtBhJ6Mam1kGsovhOmGcZLwyA=", "From": "Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Tue, 30 Mar 2021 23:12:09 +0200", "Message-Id": "<20210330211210.194806-4-jeanmichel.hautbois@ideasonboard.com>", "X-Mailer": "git-send-email 2.27.0", "In-Reply-To": "<20210330211210.194806-1-jeanmichel.hautbois@ideasonboard.com>", "References": "<20210330211210.194806-1-jeanmichel.hautbois@ideasonboard.com>", "MIME-Version": "1.0", "Subject": "[libcamera-devel] [PATCH v4 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\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>\n---\n src/ipa/ipu3/ipu3.cpp | 86 ++++++++++++--\n src/ipa/ipu3/ipu3_awb.cpp | 235 ++++++++++++++++++++++++++++++++++++++\n src/ipa/ipu3/ipu3_awb.h | 44 +++++++\n src/ipa/ipu3/meson.build | 7 +-\n 4 files changed, 363 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 34a907f2..1cce11c9 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 = 80;\n+\n namespace libcamera {\n \n LOG_DEFINE_CATEGORY(IPAIPU3)\n@@ -49,6 +54,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@@ -61,6 +67,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<ipa::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@@ -70,8 +84,59 @@ 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+\tuint32_t bestWidth = 0;\n+\tuint32_t bestHeight = 0;\n+\tuint32_t bestLog2Width = 0;\n+\tuint32_t bestLog2Height = 0;\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)) + 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\tbestWidth = width;\n+\t\t\tbestHeight = height;\n+\t\t\tbestLog2Width = widthShift;\n+\t\t\tbestLog2Height = heightShift;\n+\t\t}\n+\t}\n+\n+\tbdsGrid_.width = bestWidth >> bestLog2Width;\n+\tbdsGrid_.block_width_log2 = bestLog2Width;\n+\tbdsGrid_.height = bestHeight >> bestLog2Height;\n+\tbdsGrid_.block_height_log2 = bestLog2Height;\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@@ -92,11 +157,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<ipa::IPU3Awb>();\n+\tawbAlgo_->initialise(params_, bdsOutputSize, bdsGrid_);\n }\n \n void IPAIPU3::mapBuffers(const std::vector<IPABuffer> &buffers)\n@@ -168,10 +240,9 @@ 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+\tawbAlgo_->updateWbParameters(params_, 1.0);\n \n-\t/* \\todo Fill in parameters buffer. */\n+\t*params = params_;\n \n \tipa::ipu3::IPU3Action op;\n \top.op = ipa::ipu3::ActionParamFilled;\n@@ -184,8 +255,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 \tipa::ipu3::IPU3Action op;\n \top.op = ipa::ipu3::ActionMetadataReady;\ndiff --git a/src/ipa/ipu3/ipu3_awb.cpp b/src/ipa/ipu3/ipu3_awb.cpp\nnew file mode 100644\nindex 00000000..18d19d36\n--- /dev/null\n+++ b/src/ipa/ipu3/ipu3_awb.cpp\n@@ -0,0 +1,235 @@\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 {\n+\n+LOG_DEFINE_CATEGORY(IPU3Awb)\n+\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+/* settings for Auto White Balance */\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+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+}\n+\n+IPU3Awb::~IPU3Awb()\n+{\n+}\n+\n+void IPU3Awb::initialise(ipu3_uapi_params ¶ms, 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.obgrid = 0;\n+\tparams.obgrid_param.gr = 20;\n+\tparams.obgrid_param.r = 28;\n+\tparams.obgrid_param.b = 28;\n+\tparams.obgrid_param.gb = 20;\n+\n+\tparams.use.acc_bnr = 1;\n+\tparams.acc_param.bnr = imguCssBnrDefaults;\n+\tparams.acc_param.bnr.opt_center.x_reset = -1 * (bdsOutputSize.width / 2);\n+\tparams.acc_param.bnr.opt_center.y_reset = -1 * (bdsOutputSize.height / 2);\n+\tparams.acc_param.bnr.column_size = bdsOutputSize.width;\n+\tparams.acc_param.bnr.opt_center_sqr.x_sqr_reset = params.acc_param.bnr.opt_center.x_reset * 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 * 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+\tparams.use.acc_green_disparity = 0;\n+\tparams.acc_param.green_disparity.gd_black = 2440;\n+\tparams.acc_param.green_disparity.gd_red = 4;\n+\tparams.acc_param.green_disparity.gd_blue = 4;\n+\tparams.acc_param.green_disparity.gd_green = 4;\n+\tparams.acc_param.green_disparity.gd_shading = 24;\n+\tparams.acc_param.green_disparity.gd_support = 2;\n+\tparams.acc_param.green_disparity.gd_clip = 1;\n+\tparams.acc_param.green_disparity.gd_central_weight = 5;\n+\n+\tparams.use.acc_cds = 1;\n+\tparams.acc_param.cds.csc_en = 1;\n+\tparams.acc_param.cds.uv_bin_output = 0;\n+\tparams.acc_param.cds.ds_c00 = 0;\n+\tparams.acc_param.cds.ds_c01 = 1;\n+\tparams.acc_param.cds.ds_c02 = 1;\n+\tparams.acc_param.cds.ds_c03 = 0;\n+\tparams.acc_param.cds.ds_c10 = 0;\n+\tparams.acc_param.cds.ds_c11 = 1;\n+\tparams.acc_param.cds.ds_c12 = 1;\n+\tparams.acc_param.cds.ds_c13 = 0;\n+\tparams.acc_param.cds.ds_nf = 2;\n+\n+\twbGains_[0] = 16;\n+\twbGains_[1] = 4096;\n+\twbGains_[2] = 4096;\n+\twbGains_[3] = 16;\n+\n+\tframe_count_ = 0;\n+}\n+\n+uint32_t IPU3Awb::estimateCCT(uint8_t red, uint8_t green, uint8_t blue)\n+{\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+\tdouble x = X / (X + Y + Z);\n+\tdouble y = Y / (X + Y + Z);\n+\n+\tdouble n = (x - 0.3320) / (0.1858 - y);\n+\treturn static_cast<uint32_t>(449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33);\n+}\n+\n+double meanValue(std::vector<uint32_t> colorValues)\n+{\n+\tuint32_t count = 0;\n+\tuint32_t hist[256] = { 0 };\n+\tfor (uint32_t const &val : colorValues) {\n+\t\thist[val]++;\n+\t\tcount++;\n+\t}\n+\n+\tdouble mean = 0.0;\n+\tfor (uint32_t i = 0; i < 256; i++) {\n+\t\tmean += hist[i] * i;\n+\t}\n+\treturn mean /= count;\n+}\n+\n+void IPU3Awb::calculateWBGains(const ipu3_uapi_stats_3a *stats)\n+{\n+\tASSERT(stats->stats_3a_status.awb_en);\n+\n+\tstd::vector<uint32_t> redValues, greenValues, blueValues;\n+\tconst struct ipu3_uapi_grid_config statsAwbGrid = stats->stats_4a_config.awb_config.grid;\n+\tRectangle awbRegion = { statsAwbGrid.x_start,\n+\t\t\t\tstatsAwbGrid.y_start,\n+\t\t\t\tstatic_cast<unsigned int>(statsAwbGrid.x_end - statsAwbGrid.x_start) + 1,\n+\t\t\t\tstatic_cast<unsigned int>(statsAwbGrid.y_end - statsAwbGrid.y_start) + 1 };\n+\n+\tPoint topleft = awbRegion.topLeft();\n+\tuint32_t startY = (topleft.y >> awbGrid_.block_height_log2) * awbGrid_.width << awbGrid_.block_width_log2;\n+\tuint32_t startX = (topleft.x >> awbGrid_.block_width_log2) << awbGrid_.block_width_log2;\n+\tuint32_t endX = (startX + (awbRegion.size().width >> awbGrid_.block_width_log2)) << awbGrid_.block_width_log2;\n+\tuint32_t count = 0;\n+\tuint32_t i, j;\n+\n+\tawbCounted_ = 0;\n+\tfor (j = (topleft.y >> awbGrid_.block_height_log2);\n+\t j < (topleft.y >> awbGrid_.block_height_log2) + (awbRegion.size().height >> awbGrid_.block_height_log2);\n+\t j++) {\n+\t\tfor (i = startX + startY; i < endX + startY; i += 8) {\n+\t\t\tif (stats->awb_raw_buffer.meta_data[i + 4 + j * awbGrid_.width] == 0) {\n+\t\t\t\tgreenValues.push_back(stats->awb_raw_buffer.meta_data[i + j * awbGrid_.width]);\n+\t\t\t\tredValues.push_back(stats->awb_raw_buffer.meta_data[i + 1 + j * awbGrid_.width]);\n+\t\t\t\tblueValues.push_back(stats->awb_raw_buffer.meta_data[i + 2 + j * awbGrid_.width]);\n+\t\t\t\tgreenValues.push_back(stats->awb_raw_buffer.meta_data[i + 3 + j * awbGrid_.width]);\n+\t\t\t\tawbCounted_++;\n+\t\t\t}\n+\t\t\tcount++;\n+\t\t}\n+\t}\n+\n+\tdouble rMean = meanValue(redValues);\n+\tdouble bMean = meanValue(blueValues);\n+\tdouble gMean = meanValue(greenValues);\n+\n+\tdouble rGain = gMean / rMean;\n+\tdouble bGain = gMean / bMean;\n+\n+\twbGains_[0] = 16;\n+\twbGains_[1] = 4096 * rGain;\n+\twbGains_[2] = 4096 * bGain;\n+\twbGains_[3] = 16;\n+\n+\tframe_count_++;\n+\n+\tcct_ = estimateCCT(rMean, gMean, bMean);\n+}\n+\n+void IPU3Awb::updateWbParameters(ipu3_uapi_params ¶ms, double agcGamma)\n+{\n+\tif ((wbGains_[0] == 0) || (wbGains_[1] == 0) || (wbGains_[2] == 0) || (wbGains_[3] == 0)) {\n+\t\tLOG(IPU3Awb, Error) << \"Gains can't be 0, check the stats\";\n+\t} else {\n+\t\tparams.acc_param.bnr.wb_gains.gr = wbGains_[0];\n+\t\tparams.acc_param.bnr.wb_gains.r = wbGains_[1];\n+\t\tparams.acc_param.bnr.wb_gains.b = wbGains_[2];\n+\t\tparams.acc_param.bnr.wb_gains.gb = wbGains_[3];\n+\n+\t\tLOG(IPU3Awb, Debug) << \"Color temperature estimated: \" << cct_\n+\t\t\t\t << \" and gamma calculated: \" << agcGamma;\n+\t\tparams.acc_param.ccm = imguCssCcmDefault;\n+\n+\t\tfor (uint32_t i = 0; i < 256; i++) {\n+\t\t\tdouble j = i / 255.0;\n+\t\t\tdouble gamma = std::pow(j, 1.0 / agcGamma);\n+\t\t\tparams.acc_param.gamma.gc_lut.lut[i] = gamma * 8191;\n+\t\t}\n+\t}\n+}\n+\n+} /* namespace ipa */\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..a14401a0\n--- /dev/null\n+++ b/src/ipa/ipu3/ipu3_awb.h\n@@ -0,0 +1,44 @@\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 <linux/intel-ipu3.h>\n+\n+#include <libcamera/geometry.h>\n+\n+#include \"libipa/algorithm.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+class IPU3Awb : public Algorithm\n+{\n+public:\n+\tIPU3Awb();\n+\t~IPU3Awb();\n+\n+\tvoid initialise(ipu3_uapi_params ¶ms, const Size &bdsOutputSize, struct ipu3_uapi_grid_config &bdsGrid);\n+\tvoid calculateWBGains(const ipu3_uapi_stats_3a *stats);\n+\tvoid updateWbParameters(ipu3_uapi_params ¶ms, double agcGamma);\n+\n+private:\n+\tuint32_t estimateCCT(uint8_t red, uint8_t green, uint8_t blue);\n+\n+\t/* WB calculated gains */\n+\tuint16_t wbGains_[4];\n+\tuint32_t cct_;\n+\tuint32_t awbCounted_;\n+\tstruct ipu3_uapi_grid_config awbGrid_;\n+\tuint32_t frame_count_;\n+};\n+\n+} /* namespace ipa */\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", "v4", "3/4" ] }