[{"id":29593,"web_url":"https://patchwork.libcamera.org/comment/29593/","msgid":"<5ff7439a-91cf-43b9-a409-330d0ff7dcc1@gmail.com>","date":"2024-05-22T09:45:22","subject":"Re: [PATCH v2 2/5] libcamera: software_isp: Honor black level in AWB","submitter":{"id":179,"url":"https://patchwork.libcamera.org/api/people/179/","name":"Andrei Konovalov","email":"andrey.konovalov.ynk@gmail.com"},"content":"Hi Milan,\n\nThank you for the patch!\n\nReviewed-by: Andrei Konovalov <andrey.konovalov.ynk@gmail.com>\n\nOn 30.04.2024 20:34, Milan Zamazal wrote:\n> The white balance computation didn't consider black level.  This is\n> wrong because then the computed ratios are off when they are computed\n> from the whole brightness range rather than the sensor range.\n> \n> This patch adjusts white balance computation for the black level.  There\n> is no need to change white balance application in debayering as this is\n> already applied after black level correction.\n> \n> Exposure computation already subtracts black level, no changes are\n> needed there.\n> \n> Signed-off-by: Milan Zamazal <mzamazal@redhat.com>\n> ---\n>   src/ipa/simple/soft_simple.cpp | 36 ++++++++++++++++++++++------------\n>   1 file changed, 23 insertions(+), 13 deletions(-)\n> \n> diff --git a/src/ipa/simple/soft_simple.cpp b/src/ipa/simple/soft_simple.cpp\n> index b9fb58b5..f595abc2 100644\n> --- a/src/ipa/simple/soft_simple.cpp\n> +++ b/src/ipa/simple/soft_simple.cpp\n> @@ -5,6 +5,8 @@\n>    * soft_simple.cpp - Simple Software Image Processing Algorithm module\n>    */\n>   \n> +#include <numeric>\n> +#include <stdint.h>\n>   #include <sys/mman.h>\n>   \n>   #include <linux/v4l2-controls.h>\n> @@ -240,28 +242,36 @@ void IPASoftSimple::stop()\n>   \n>   void IPASoftSimple::processStats(const ControlList &sensorControls)\n>   {\n> +\tSwIspStats::Histogram histogram = stats_->yHistogram;\n> +\tif (ignoreUpdates_ > 0)\n> +\t\tblackLevel_.update(histogram);\n> +\tconst uint8_t blackLevel = blackLevel_.get();\n> +\tparams_->blackLevel = blackLevel;\n> +\n>   \t/*\n>   \t * Calculate red and blue gains for AWB.\n>   \t * Clamp max gain at 4.0, this also avoids 0 division.\n> +\t * Black level must be subtracted to get the correct AWB ratios,\n> +\t * from the sensor range.\n>   \t */\n> -\tif (stats_->sumR_ <= stats_->sumG_ / 4)\n> -\t\tparams_->gainR = 1024;\n> -\telse\n> -\t\tparams_->gainR = 256 * stats_->sumG_ / stats_->sumR_;\n> -\n> -\tif (stats_->sumB_ <= stats_->sumG_ / 4)\n> -\t\tparams_->gainB = 1024;\n> -\telse\n> -\t\tparams_->gainB = 256 * stats_->sumG_ / stats_->sumB_;\n> +\tconst uint64_t nPixels = std::accumulate(\n> +\t\thistogram.begin(), histogram.end(), 0);\n> +\tauto subtractBlackLevel = [blackLevel, nPixels](\n> +\t\t\t\t\t  uint64_t sum, uint64_t div)\n> +\t\t-> uint64_t {\n> +\t\treturn sum - blackLevel * nPixels / div;\n> +\t};\n> +\tconst uint64_t sumR = subtractBlackLevel(stats_->sumR_, 4);\n> +\tconst uint64_t sumG = subtractBlackLevel(stats_->sumG_, 2);\n> +\tconst uint64_t sumB = subtractBlackLevel(stats_->sumB_, 4);\n> +\n> +\tparams_->gainR = sumR <= sumG / 4 ? 1024 : 256 * sumG / sumR;\n> +\tparams_->gainB = sumB <= sumG / 4 ? 1024 : 256 * sumG / sumB;\n>   \n>   \t/* Green gain and gamma values are fixed */\n>   \tparams_->gainG = 256;\n>   \tparams_->gamma = 0.5;\n>   \n> -\tif (ignoreUpdates_ > 0)\n> -\t\tblackLevel_.update(stats_->yHistogram);\n> -\tparams_->blackLevel = blackLevel_.get();\n> -\n>   \tsetIspParams.emit();\n>   \n>   \t/* \\todo Switch to the libipa/algorithm.h API someday. */","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 B0BD3BD78E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 22 May 2024 09:45:28 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 665B963488;\n\tWed, 22 May 2024 11:45:28 +0200 (CEST)","from mail-qt1-x832.google.com (mail-qt1-x832.google.com\n\t[IPv6:2607:f8b0:4864:20::832])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 21B0D61A50\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 22 May 2024 11:45:26 +0200 (CEST)","by mail-qt1-x832.google.com with SMTP id\n\td75a77b69052e-43e1593d633so6851721cf.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 22 May 2024 02:45:26 -0700 (PDT)","from [192.168.118.26] ([87.116.162.89])\n\tby smtp.gmail.com with ESMTPSA id\n\td75a77b69052e-43e4a89b61dsm3587921cf.45.2024.05.22.02.45.23\n\t(version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);\n\tWed, 22 May 2024 02:45:24 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=gmail.com header.i=@gmail.com\n\theader.b=\"LLWX70my\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=gmail.com; s=20230601; t=1716371125; x=1716975925;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:in-reply-to:from:content-language\n\t:references:cc:to:subject:user-agent:mime-version:date:message-id\n\t:from:to:cc:subject:date:message-id:reply-to;\n\tbh=f3Dfkuo7EXKCn7Sl8FC7A8MMU2IiuJDgg25u160L/gI=;\n\tb=LLWX70my7VmzrDLQJodB5EfvItCclrTw3LxY9RnvYt3cov96SRYOJ8wr4o3IvyQ6vE\n\tk+eyTcY4pjS1q9LjQzCTWC0hPEAwXYES8SMcSUMnzIu8P1Ih0LTXR4qMm7txTa5lQ5k4\n\t5V4skiGZtjCZs6PM5EL3Fe68iYBRdNq3cAjhp6PK3L3+W8Wg0hnxCm8GBPENa9deWxg5\n\tm/Y0pYCXkruyRqbtV3uhkV8MeWASX7yx8DgW/HEQlOgtVCkKfzgfh3HEkvfLNsSKRWxt\n\tr5Pm/jxKs1B17JN4sov0abenG274X6vSP8XQjH750BGJo3UFSUp+BL/95+HxL/DjoQsB\n\tu7Yw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1716371125; x=1716975925;\n\th=content-transfer-encoding:in-reply-to:from:content-language\n\t:references:cc:to:subject:user-agent:mime-version:date:message-id\n\t:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;\n\tbh=f3Dfkuo7EXKCn7Sl8FC7A8MMU2IiuJDgg25u160L/gI=;\n\tb=aeIkG/m6i8cbQ7CnL3Aqjudt5S5OPIxKHa++XbIRhdTWRqCprmo7gMYdFdvWVEoDIC\n\t50iVblriSS1xhdzCj7sa+OylLHEIR51U3KiT10lY+4Xpv4XmlmGwOaM6v+WBa1gXmZC3\n\ttdA8tih3muC9wHXBFtxGVvGyDInv72Dkl/ycziigmxtpMXSUIV1A3Y4eN9EKW6VH3kYO\n\tD+WjWYJ5CqPt6kLUf6/3wdztiYIUiaybte37aAE4RoWp40oqcxRPnG9IWzsbp/6hRle0\n\tB22OW3Bl8fVF32hqRjF2hT7Am5MQz7RZAxIYQTEgcBTkcfBeAjKdNJzvaHsaCaFNrD18\n\tnkcg==","X-Forwarded-Encrypted":"i=1;\n\tAJvYcCUqI1H3GLezZEbk5peD5mAn4wMZINfDDSBB38KcHDhMSabq8XUECu3EpqgrumCR0IadAPAcYGxFLgsA54wtZqlvErNdsoGNwVt/JiNVOMC1/BsrLw==","X-Gm-Message-State":"AOJu0YxxbCL3RpgKdZz3Fkp7Tm6Vlm/2DrBub0rjRET2zq2x8Tmez3L4\n\tMklxCOOogmB1moS48owIi9lksszXqd6cIgB9zdjtK5P/hidhOcr5","X-Google-Smtp-Source":"AGHT+IEAj5djBkbqaztZ8joGy2Y7K3aVCil2eypALDVbwjxJA/5HxaLs37kXmV4d0WnGPTD+3BNj6Q==","X-Received":"by 2002:ac8:7f43:0:b0:43a:dd9f:7aa8 with SMTP id\n\td75a77b69052e-43f9e0a7c44mr16765851cf.9.1716371125041; \n\tWed, 22 May 2024 02:45:25 -0700 (PDT)","Message-ID":"<5ff7439a-91cf-43b9-a409-330d0ff7dcc1@gmail.com>","Date":"Wed, 22 May 2024 12:45:22 +0300","MIME-Version":"1.0","User-Agent":"Mozilla Thunderbird","Subject":"Re: [PATCH v2 2/5] libcamera: software_isp: Honor black level in AWB","To":"Milan Zamazal <mzamazal@redhat.com>, libcamera-devel@lists.libcamera.org","Cc":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","References":"<20240430173430.200392-1-mzamazal@redhat.com>\n\t<20240430173430.200392-3-mzamazal@redhat.com>","Content-Language":"en-US","From":"Andrei Konovalov <andrey.konovalov.ynk@gmail.com>","In-Reply-To":"<20240430173430.200392-3-mzamazal@redhat.com>","Content-Type":"text/plain; charset=UTF-8; format=flowed","Content-Transfer-Encoding":"7bit","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>"}}]