{"id":15626,"url":"https://patchwork.libcamera.org/api/1.1/patches/15626/?format=json","web_url":"https://patchwork.libcamera.org/patch/15626/","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":"<20220406101100.23502-1-laurent.pinchart@ideasonboard.com>","date":"2022-04-06T10:11:00","name":"[libcamera-devel,v3] ipa: raspberrypi: alsc: Limit the calculated lambda values","commit_ref":"226792a1411bd485e087bf40e241611966099b52","pull_url":null,"state":"accepted","archived":false,"hash":"2b1f33311d39c23488e18cf9c619a335871627b2","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/15626/mbox/","series":[{"id":3032,"url":"https://patchwork.libcamera.org/api/1.1/series/3032/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3032","date":"2022-04-06T10:11:00","name":"[libcamera-devel,v3] ipa: raspberrypi: alsc: Limit the calculated lambda values","version":3,"mbox":"https://patchwork.libcamera.org/series/3032/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/15626/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/15626/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 4D103C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  6 Apr 2022 10:11:10 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6C73365644;\n\tWed,  6 Apr 2022 12:11:09 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 6E24C633A4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  6 Apr 2022 12:11:08 +0200 (CEST)","from pendragon.home (117.145-247-81.adsl-dyn.isp.belgacom.be\n\t[81.247.145.117])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id ED99D482;\n\tWed,  6 Apr 2022 12:11:07 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1649239869;\n\tbh=otSvnRmyVnhI5y0srgWByJOjR0FsDrD1cMlLsA6vZiw=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:From;\n\tb=vzN4FEOImQQT7a6vtH+ptGL+lYDK50hrB+LzkYQZEY0CDe33IJ6Gs59kQ8DLqvUbB\n\t4XRxicsgbqAwNfZo6iYk15PDxRbzaxWu1vqmvynp9hNwKSpYr3rrdg+qgfEbkpRdBg\n\trIDLJMmAGAg5I/8CMeurYWGL3opBbw6v9YBXJLLyQCOHiU87WDb4JapyBXOgVPzUAv\n\tg9arwSZ6U5OFOD3Bt4j0s9x4gwibwjVAr3/1Gi1bB+dhBtwoaszDLlmXBMd1Y5KdOJ\n\tzVnW43nQnSUw+yJZm44o110Muw6YA9C2l60VDxsq8W3sSTzwa/50VIgBWkcNjh/lpT\n\tQBluX62YvmxyA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1649239868;\n\tbh=otSvnRmyVnhI5y0srgWByJOjR0FsDrD1cMlLsA6vZiw=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=LrvoZbNs33h2/RPRFERWZyGCdJR+r7CcLZ8XfvUBeMt469cmyHfHhpuk1Fi0DmdXP\n\tn7Ic0KH0QqWv9ENuIyHsKJRAaX/NMcmYRyehcjldOk5zRKLDw/Fd/5vqCtyJqZjAuT\n\tDCmdpnL9dPvWJSX4QmCSkB4kP+huL+IYL5kFzZaU="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"LrvoZbNs\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  6 Apr 2022 13:11:00 +0300","Message-Id":"<20220406101100.23502-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3] ipa: raspberrypi: alsc: Limit the\n\tcalculated lambda values","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Naushir Patuck <naush@raspberrypi.com>\n\nUnder the right circumstances, the alsc calculations could spread the colour\nerrors across the entire image as lambda remains unbound. This would cause the\ncorrected image chroma values to slowly drift to incorrect values.\n\nThis change adds a config parameter (alsc.lambda_bound) that provides an upper\nand lower bound to the lambda value at every stage of the calculation. With this\nchange, we now adjust the lambda values so that the average across the entire\ngrid is 1 instead of normalising to the minimum value.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges since v2:\n\n- Fix typo\n- Use Span and std::accumulate\n\nNaush, could you please test this to make sure I haven't messed up\nanything ?\n\n src/ipa/raspberrypi/controller/rpi/alsc.cpp | 58 +++++++++++++++------\n src/ipa/raspberrypi/controller/rpi/alsc.hpp |  1 +\n 2 files changed, 44 insertions(+), 15 deletions(-)","diff":"diff --git a/src/ipa/raspberrypi/controller/rpi/alsc.cpp b/src/ipa/raspberrypi/controller/rpi/alsc.cpp\nindex be3d1ae476cd..e575c14a92db 100644\n--- a/src/ipa/raspberrypi/controller/rpi/alsc.cpp\n+++ b/src/ipa/raspberrypi/controller/rpi/alsc.cpp\n@@ -4,9 +4,12 @@\n  *\n  * alsc.cpp - ALSC (auto lens shading correction) control algorithm\n  */\n+\n #include <math.h>\n+#include <numeric>\n \n #include <libcamera/base/log.h>\n+#include <libcamera/base/span.h>\n \n #include \"../awb_status.h\"\n #include \"alsc.hpp\"\n@@ -149,6 +152,7 @@ void Alsc::Read(boost::property_tree::ptree const &params)\n \tread_calibrations(config_.calibrations_Cb, params, \"calibrations_Cb\");\n \tconfig_.default_ct = params.get<double>(\"default_ct\", 4500.0);\n \tconfig_.threshold = params.get<double>(\"threshold\", 1e-3);\n+\tconfig_.lambda_bound = params.get<double>(\"lambda_bound\", 0.05);\n }\n \n static double get_ct(Metadata *metadata, double default_ct);\n@@ -610,30 +614,47 @@ static double compute_lambda_top_end(int i, double const M[XY][4],\n \n // Gauss-Seidel iteration with over-relaxation.\n static double gauss_seidel2_SOR(double const M[XY][4], double omega,\n-\t\t\t\tdouble lambda[XY])\n+\t\t\t\tdouble lambda[XY], double lambda_bound)\n {\n+\tconst double min = 1 - lambda_bound, max = 1 + lambda_bound;\n \tdouble old_lambda[XY];\n \tint i;\n \tfor (i = 0; i < XY; i++)\n \t\told_lambda[i] = lambda[i];\n \tlambda[0] = compute_lambda_bottom_start(0, M, lambda);\n-\tfor (i = 1; i < X; i++)\n+\tlambda[0] = std::clamp(lambda[0], min, max);\n+\tfor (i = 1; i < X; i++) {\n \t\tlambda[i] = compute_lambda_bottom(i, M, lambda);\n-\tfor (; i < XY - X; i++)\n+\t\tlambda[i] = std::clamp(lambda[i], min, max);\n+\t}\n+\tfor (; i < XY - X; i++) {\n \t\tlambda[i] = compute_lambda_interior(i, M, lambda);\n-\tfor (; i < XY - 1; i++)\n+\t\tlambda[i] = std::clamp(lambda[i], min, max);\n+\t}\n+\tfor (; i < XY - 1; i++) {\n \t\tlambda[i] = compute_lambda_top(i, M, lambda);\n+\t\tlambda[i] = std::clamp(lambda[i], min, max);\n+\t}\n \tlambda[i] = compute_lambda_top_end(i, M, lambda);\n+\tlambda[i] = std::clamp(lambda[i], min, max);\n \t// Also solve the system from bottom to top, to help spread the updates\n \t// better.\n \tlambda[i] = compute_lambda_top_end(i, M, lambda);\n-\tfor (i = XY - 2; i >= XY - X; i--)\n+\tlambda[i] = std::clamp(lambda[i], min, max);\n+\tfor (i = XY - 2; i >= XY - X; i--) {\n \t\tlambda[i] = compute_lambda_top(i, M, lambda);\n-\tfor (; i >= X; i--)\n+\t\tlambda[i] = std::clamp(lambda[i], min, max);\n+\t}\n+\tfor (; i >= X; i--) {\n \t\tlambda[i] = compute_lambda_interior(i, M, lambda);\n-\tfor (; i >= 1; i--)\n+\t\tlambda[i] = std::clamp(lambda[i], min, max);\n+\t}\n+\tfor (; i >= 1; i--) {\n \t\tlambda[i] = compute_lambda_bottom(i, M, lambda);\n+\t\tlambda[i] = std::clamp(lambda[i], min, max);\n+\t}\n \tlambda[0] = compute_lambda_bottom_start(0, M, lambda);\n+\tlambda[0] = std::clamp(lambda[0], min, max);\n \tdouble max_diff = 0;\n \tfor (i = 0; i < XY; i++) {\n \t\tlambda[i] = old_lambda[i] + (lambda[i] - old_lambda[i]) * omega;\n@@ -653,15 +674,24 @@ static void normalise(double *ptr, size_t n)\n \t\tptr[i] /= minval;\n }\n \n+// Rescale the values so that the average value is 1.\n+static void reaverage(Span<double> data)\n+{\n+\tdouble sum = std::accumulate(data.begin(), data.end(), 0.0);\n+\tdouble ratio = 1 / (sum / data.size());\n+\tfor (double &d : data)\n+\t\td *= ratio;\n+}\n+\n static void run_matrix_iterations(double const C[XY], double lambda[XY],\n \t\t\t\t  double const W[XY][4], double omega,\n-\t\t\t\t  int n_iter, double threshold)\n+\t\t\t\t  int n_iter, double threshold, double lambda_bound)\n {\n \tdouble M[XY][4];\n \tconstruct_M(C, W, M);\n \tdouble last_max_diff = std::numeric_limits<double>::max();\n \tfor (int i = 0; i < n_iter; i++) {\n-\t\tdouble max_diff = fabs(gauss_seidel2_SOR(M, omega, lambda));\n+\t\tdouble max_diff = fabs(gauss_seidel2_SOR(M, omega, lambda, lambda_bound));\n \t\tif (max_diff < threshold) {\n \t\t\tLOG(RPiAlsc, Debug)\n \t\t\t\t<< \"Stop after \" << i + 1 << \" iterations\";\n@@ -675,10 +705,8 @@ static void run_matrix_iterations(double const C[XY], double lambda[XY],\n \t\t\t\t<< last_max_diff << \" to \" << max_diff;\n \t\tlast_max_diff = max_diff;\n \t}\n-\t// We're going to normalise the lambdas so the smallest is 1. Not sure\n-\t// this is really necessary as they get renormalised later, but I\n-\t// suppose it does stop these quantities from wandering off...\n-\tnormalise(lambda, XY);\n+\t// We're going to normalise the lambdas so the total average is 1.\n+\treaverage({ lambda, XY });\n }\n \n static void add_luminance_rb(double result[XY], double const lambda[XY],\n@@ -737,9 +765,9 @@ void Alsc::doAlsc()\n \tcompute_W(Cb, config_.sigma_Cb, Wb);\n \t// Run Gauss-Seidel iterations over the resulting matrix, for R and B.\n \trun_matrix_iterations(Cr, lambda_r_, Wr, config_.omega, config_.n_iter,\n-\t\t\t      config_.threshold);\n+\t\t\t      config_.threshold, config_.lambda_bound);\n \trun_matrix_iterations(Cb, lambda_b_, Wb, config_.omega, config_.n_iter,\n-\t\t\t      config_.threshold);\n+\t\t\t      config_.threshold, config_.lambda_bound);\n \t// Fold the calibrated gains into our final lambda values. (Note that on\n \t// the next run, we re-start with the lambda values that don't have the\n \t// calibration gains included.)\ndiff --git a/src/ipa/raspberrypi/controller/rpi/alsc.hpp b/src/ipa/raspberrypi/controller/rpi/alsc.hpp\nindex 9616b99ea7ca..d1dbe0d1d22d 100644\n--- a/src/ipa/raspberrypi/controller/rpi/alsc.hpp\n+++ b/src/ipa/raspberrypi/controller/rpi/alsc.hpp\n@@ -41,6 +41,7 @@ struct AlscConfig {\n \tstd::vector<AlscCalibration> calibrations_Cb;\n \tdouble default_ct; // colour temperature if no metadata found\n \tdouble threshold; // iteration termination threshold\n+\tdouble lambda_bound; // upper/lower bound for lambda from a value of 1\n };\n \n class Alsc : public Algorithm\n","prefixes":["libcamera-devel","v3"]}