[{"id":13736,"web_url":"https://patchwork.libcamera.org/comment/13736/","msgid":"<CAEmqJPo0m8dNwH-SjGM3QSPj89A5e1L1aUH8yHxZJ8VigoZMKw@mail.gmail.com>","date":"2020-11-17T10:45:27","subject":"Re: [libcamera-devel] [PATCH 05/10] libcamera: ipa: raspberrypi:\n\tagc: Fetch AWB status only once","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"content":"Hi David,\n\n\nOn Mon, 16 Nov 2020 at 16:49, David Plowman <david.plowman@raspberrypi.com>\nwrote:\n\n> Introduce a function to fetch the AwbStatus (fetchAwbStatus), and call\n> it unconditionally at the top of Prepare so that both Prepare and\n> Process know thereafter that it's been done.\n>\n> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>\n> ---\n>  src/ipa/raspberrypi/controller/rpi/agc.cpp | 37 ++++++++++++----------\n>  src/ipa/raspberrypi/controller/rpi/agc.hpp |  5 +--\n>  2 files changed, 23 insertions(+), 19 deletions(-)\n>\n> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp\n> b/src/ipa/raspberrypi/controller/rpi/agc.cpp\n> index ead28398..6de56def 100644\n> --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp\n> +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp\n> @@ -5,6 +5,8 @@\n>   * agc.cpp - AGC/AEC control algorithm\n>   */\n>\n> +#include <assert.h>\n>\n\nPerhaps you can use the ASSERT() macro defined in libcamera (log.h)?  I\nthink all code under libcamera is meant to use it instead of the standard\none.\n\n\n> +\n>  #include <map>\n>\n>  #include \"linux/bcm2835-isp.h\"\n> @@ -235,6 +237,8 @@ void Agc::Prepare(Metadata *image_metadata)\n>         int lock_count = lock_count_;\n>         lock_count_ = 0;\n>         status_.digital_gain = 1.0;\n> +       fetchAwbStatus(image_metadata); // always fetch it so that Process\n> knows it's been done\n> +\n>         if (status_.total_exposure_value) {\n>                 // Process has run, so we have meaningful values.\n>                 DeviceStatus device_status;\n> @@ -301,7 +305,7 @@ void Agc::Process(StatisticsPtr &stats, Metadata\n> *image_metadata)\n>         // Some of the exposure has to be applied as digital gain, so work\n> out\n>         // what that is. This function also tells us whether it's decided\n> to\n>         // \"desaturate\" the image more quickly.\n> -       bool desaturate = applyDigitalGain(image_metadata, gain, target_Y);\n> +       bool desaturate = applyDigitalGain(gain, target_Y);\n>         // The results have to be filtered so as not to change too rapidly.\n>         filterExposure(desaturate);\n>         // The last thing is to divide up the exposure value into a\n> shutter time\n> @@ -378,14 +382,19 @@ void Agc::fetchCurrentExposure(Metadata\n> *image_metadata)\n>         current_.total_exposure_no_dg = current_.shutter *\n> current_.analogue_gain;\n>  }\n>\n> -static double compute_initial_Y(bcm2835_isp_stats *stats, Metadata\n> *image_metadata,\n> +void Agc::fetchAwbStatus(Metadata *image_metadata)\n> +{\n> +       awb_.gain_r = 1.0; // in case not found in metadata\n> +       awb_.gain_g = 1.0;\n> +       awb_.gain_b = 1.0;\n> +       if (image_metadata->Get(\"awb.status\", awb_) != 0)\n> +               LOG(RPiAgc, Warning) << \"Agc: no AWB status found\";\n> +}\n> +\n> +static double compute_initial_Y(bcm2835_isp_stats *stats, AwbStatus const\n> &awb,\n>                                 double weights[])\n>  {\n>         bcm2835_isp_stats_region *regions = stats->agc_stats;\n> -       struct AwbStatus awb;\n> -       awb.gain_r = awb.gain_g = awb.gain_b = 1.0; // in case no metadata\n> -       if (image_metadata->Get(\"awb.status\", awb) != 0)\n> -               LOG(RPiAgc, Warning) << \"Agc: no AWB status found\";\n>         // Note how the calculation below means that equal weights give you\n>         // \"average\" metering (i.e. all pixels equally important).\n>         double R_sum = 0, G_sum = 0, B_sum = 0, pixel_sum = 0;\n> @@ -437,7 +446,7 @@ void Agc::computeGain(bcm2835_isp_stats *statistics,\n> Metadata *image_metadata,\n>         target_Y =\n>\n> config_.Y_target.Eval(config_.Y_target.Domain().Clip(lux.lux));\n>         target_Y = std::min(EV_GAIN_Y_TARGET_LIMIT, target_Y * ev_gain);\n> -       double initial_Y = compute_initial_Y(statistics, image_metadata,\n> +       double initial_Y = compute_initial_Y(statistics, awb_,\n>                                              metering_mode_->weights);\n>         gain = std::min(10.0, target_Y / (initial_Y + .001));\n>         LOG(RPiAgc, Debug) << \"Initially Y \" << initial_Y << \" target \" <<\n> target_Y\n> @@ -483,19 +492,13 @@ void Agc::computeTargetExposure(double gain)\n>         LOG(RPiAgc, Debug) << \"Target total_exposure \" <<\n> target_.total_exposure;\n>  }\n>\n> -bool Agc::applyDigitalGain(Metadata *image_metadata, double gain,\n> -                          double target_Y)\n> +bool Agc::applyDigitalGain(double gain, double target_Y)\n>  {\n> -       double dg = 1.0;\n> +       double min_colour_gain = std::min({ awb_.gain_r, awb_.gain_g,\n> awb_.gain_b, 1.0 });\n> +       assert(min_colour_gain != 0.0);\n> +       double dg = 1.0 / min_colour_gain;\n>         // I think this pipeline subtracts black level and rescales before\n> we\n>         // get the stats, so no need to worry about it.\n> -       struct AwbStatus awb;\n> -       if (image_metadata->Get(\"awb.status\", awb) == 0) {\n> -               double min_gain = std::min(awb.gain_r,\n> -                                          std::min(awb.gain_g,\n> awb.gain_b));\n> -               dg *= std::max(1.0, 1.0 / min_gain);\n> -       } else\n> -               LOG(RPiAgc, Warning) << \"Agc: no AWB status found\";\n>         LOG(RPiAgc, Debug) << \"after AWB, target dg \" << dg << \" gain \" <<\n> gain\n>                            << \" target_Y \" << target_Y;\n>         // Finally, if we're trying to reduce exposure but the target_Y is\n> diff --git a/src/ipa/raspberrypi/controller/rpi/agc.hpp\n> b/src/ipa/raspberrypi/controller/rpi/agc.hpp\n> index 2442fc03..e7ac480f 100644\n> --- a/src/ipa/raspberrypi/controller/rpi/agc.hpp\n> +++ b/src/ipa/raspberrypi/controller/rpi/agc.hpp\n> @@ -83,11 +83,11 @@ private:\n>         AgcConfig config_;\n>         void housekeepConfig();\n>         void fetchCurrentExposure(Metadata *image_metadata);\n> +       void fetchAwbStatus(Metadata *image_metadata);\n>         void computeGain(bcm2835_isp_stats *statistics, Metadata\n> *image_metadata,\n>                          double &gain, double &target_Y);\n>         void computeTargetExposure(double gain);\n> -       bool applyDigitalGain(Metadata *image_metadata, double gain,\n> -                             double target_Y);\n> +       bool applyDigitalGain(double gain, double target_Y);\n>         void filterExposure(bool desaturate);\n>         void divideUpExposure();\n>         void writeAndFinish(Metadata *image_metadata, bool desaturate);\n> @@ -95,6 +95,7 @@ private:\n>         AgcExposureMode *exposure_mode_;\n>         AgcConstraintMode *constraint_mode_;\n>         uint64_t frame_count_;\n> +       AwbStatus awb_;\n>         struct ExposureValues {\n>                 ExposureValues() : shutter(0), analogue_gain(0),\n>                                    total_exposure(0),\n> total_exposure_no_dg(0) {}\n>\n\nAparat from the minor:\n\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n\n\n> --\n> 2.20.1\n>\n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel\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 79825BE081\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 17 Nov 2020 10:45:45 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id EF7A763328;\n\tTue, 17 Nov 2020 11:45:44 +0100 (CET)","from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com\n\t[IPv6:2a00:1450:4864:20::12c])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E90B763320\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 Nov 2020 11:45:43 +0100 (CET)","by mail-lf1-x12c.google.com with SMTP id z21so29417749lfe.12\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 Nov 2020 02:45:43 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"n2T+0bbj\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=mime-version:references:in-reply-to:from:date:message-id:subject:to\n\t:cc; bh=scpbdtYmTeSpi4T5QoGoIT5cpQHAnTqfnRPkSuvc83Q=;\n\tb=n2T+0bbj1GnIYUgWMS8BWlZLcXLgIUQ2rdvPYgvCpIGL8nciBoSa45jHHEb485VPPg\n\tFuTVf27ydQl9dpqQR1IxXsbcPj7Naq5mJONppBxFkMYL6UPI4oow531a699qTloe/ZL+\n\tm2jBNsD/jtoLGaEOnf9V9N55ipET9CbEgFmyo74jKhQSjnUerX+wqOFLTRRw7BfN4fl9\n\tihwfPQS7rYbTGi7jHhX43chT+YuD1/5+mtdeBAXcj/8zPQRiCZohGons0J7Ojgv9JpZh\n\tiOdzomD8BNJpjU54pm5sOj9SnUfUfM5fbUXLkTkJssWzxXfNcgt5scoZ/T8UMv94IZsA\n\tjwmw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:mime-version:references:in-reply-to:from:date\n\t:message-id:subject:to:cc;\n\tbh=scpbdtYmTeSpi4T5QoGoIT5cpQHAnTqfnRPkSuvc83Q=;\n\tb=QgTENAIs8BcKpAKoAkpyE+Y6N/3fRKSocF45Nukhkc49T1yjPbN0iC3cowLG7sJnOQ\n\tPxJAV+tYmYCvSNhI4w50bsFL2SIjP1iHB7mKt0Q0oejUxS5cTyANLqCdfoO+gK+08ico\n\tG6EcZAS8Eq6qyFtxL5q1HGxxw01yNgau8iByGsI4vpinEsYPERClxOhZWKYw3E5JOU3n\n\tvuNFeUUxCZf3cLCN2pGon2TBZvg6yz8BpMeG0XMc8oyBmn1e5pQHq96P/7rio2tvMvqk\n\td1dJ2qs8QoCzVDaH5N2HNEH0TaAhChFvwlREyqiogrziUuNrd+qWSAgrsQJtlQmpKy3P\n\tpl+g==","X-Gm-Message-State":"AOAM530r9y5HICu+iBCKxAwU7m7RVr9yEC88SrxyXeKEe3h5VSLF5AVK\n\tGfKMRYbNGFgwUAqT6N185yvtnYwY/5kHL46fOcnTKw==","X-Google-Smtp-Source":"ABdhPJwhO762bKc8IqsThPuLchzWgeRetI0+I0ApBfW5JS5alLHKbP1lOaiZaoZ5onqgpFdKII4e4C0UkTjUhIlmDF0=","X-Received":"by 2002:a19:428c:: with SMTP id\n\tp134mr1605485lfa.413.1605609943334; \n\tTue, 17 Nov 2020 02:45:43 -0800 (PST)","MIME-Version":"1.0","References":"<20201116164918.2055-1-david.plowman@raspberrypi.com>\n\t<20201116164918.2055-6-david.plowman@raspberrypi.com>","In-Reply-To":"<20201116164918.2055-6-david.plowman@raspberrypi.com>","From":"Naushir Patuck <naush@raspberrypi.com>","Date":"Tue, 17 Nov 2020 10:45:27 +0000","Message-ID":"<CAEmqJPo0m8dNwH-SjGM3QSPj89A5e1L1aUH8yHxZJ8VigoZMKw@mail.gmail.com>","To":"David Plowman <david.plowman@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH 05/10] libcamera: ipa: raspberrypi:\n\tagc: Fetch AWB status only once","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","Content-Type":"multipart/mixed;\n\tboundary=\"===============2189963364756191298==\"","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]