From patchwork Mon Dec 13 12:28:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kate Hsuan X-Patchwork-Id: 15148 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E46A2BDB13 for ; Mon, 13 Dec 2021 12:29:20 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 994F36087E; Mon, 13 Dec 2021 13:29:20 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="g65daocc"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A7CB060868 for ; Mon, 13 Dec 2021 13:29:18 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639398557; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/QAzdHmNCUDs7yasrdyZE59ZkF3FK8w956HxHvsI5gs=; b=g65daocc6BBbCHB8Mx0NAu3WalpB+BZ7PL/rsSwRgzzjIqfsHFCeeuJ8tB+1AQAModbWmJ X0Tw9R/optJhb2IlyTBe6jFvWxgib4E9yEA1VS2fgxS7OBEL2i6YWedqZ2mtJ/wYH292E3 rqUbHtenignEHniiC8MtZQvfU9ej5II= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-486-vqKPyZCmPOSYRAa9zy6fsw-1; Mon, 13 Dec 2021 07:29:16 -0500 X-MC-Unique: vqKPyZCmPOSYRAa9zy6fsw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A8807874982 for ; Mon, 13 Dec 2021 12:29:15 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.39.193.56]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1B1207AB46; Mon, 13 Dec 2021 12:29:12 +0000 (UTC) From: Kate Hsuan To: libcamera devel Date: Mon, 13 Dec 2021 20:28:47 +0800 Message-Id: <20211213122847.26260-2-hpa@redhat.com> In-Reply-To: <20211213122847.26260-1-hpa@redhat.com> References: <20211213122847.26260-1-hpa@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=hpa@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [libcamera-devel] [RFC v4 1/1] ipa: ipu3: af: Auto focus for dw9719 Surface Go2 VCM X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Since VCM for surface Go 2 (dw9719) had been successfully driven, this Af module can be used to control the VCM and determine the focus value based on the IPU3 AF state. The variance of each focus step is determined and a greedy approah is used to find the maximum variance of the AF state and a appropriate focus value. Changes in v2: 1. Add grid configuration interface. 2. Add AF accelerator configuration. 3. Move default focus area to the center of the image. Changes in v3: 1. Squash Daniel's commit- Remove v4l2 interaction from AF algorithm. 2. Fix greedy AF algorithm since V4l2 interface had been removed. 3. Simplify default grid config for AF. 4. AF start area is determined by bdsOutputSize. Changes in v4: In v4, it significant improves the AF scan time using a two pass scaning method and the AF scan will be terminated when it finds a negative gradient. 1. Introduce 2 pass AF scan (coarse and fine scan) to increase the AF success rate. 2. The low pass filter convolution results are used to determine a coarse AF result. 3. The high pass convolution result is used to find a good lens step in a given AF range determined in pass 1. Signed-off-by: Kate Hsuan --- src/ipa/ipu3/algorithms/af.cpp | 334 ++++++++++++++++++++++++++++ src/ipa/ipu3/algorithms/af.h | 66 ++++++ src/ipa/ipu3/algorithms/meson.build | 3 +- src/ipa/ipu3/ipa_context.cpp | 27 +++ src/ipa/ipu3/ipa_context.h | 11 + src/ipa/ipu3/ipu3.cpp | 2 + 6 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 src/ipa/ipu3/algorithms/af.cpp create mode 100644 src/ipa/ipu3/algorithms/af.h diff --git a/src/ipa/ipu3/algorithms/af.cpp b/src/ipa/ipu3/algorithms/af.cpp new file mode 100644 index 00000000..620be9fa --- /dev/null +++ b/src/ipa/ipu3/algorithms/af.cpp @@ -0,0 +1,334 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Red Hat + * + * af.cpp - IPU3 auto focus control + */ + +#include "af.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include + +#include "libipa/histogram.h" + +/** + * \file af.h + */ + +namespace libcamera { + +using namespace std::literals::chrono_literals; + +namespace ipa::ipu3::algorithms { + +/** + * \class Af + * \brief A IPU3 auto-focus accelerator based auto focus algorthim + * + * This algorithm is used to determine the position of the lens and get a + * focused image. The IPU3 AF accelerator computes the statistics, composed + * by high pass and low pass filtered value and stores in a AF buffer. + * Typically, for a focused image, it has relative high contrast than a + * blurred image, i.e. an out of focus image. Therefore, if an image with the + * highest contrast can be found from the AF scan, the lens' position is the + * best step of the focus. + * + */ + +LOG_DEFINE_CATEGORY(IPU3Af) + +/** + * Maximum focus value of the VCM control + * \todo should be obtained from the VCM driver + */ +static constexpr uint32_t MaxFocusSteps_ = 1023; + +/* minimum focus step for searching appropriate focus*/ +static constexpr uint32_t coarseSearchStep_ = 10; +static constexpr uint32_t fineSearchStep_ = 1; + +/* max ratio of variance change, 0.0 < MaxChange_ < 1.0*/ +static constexpr double MaxChange_ = 0.8; + +/* settings for Auto Focus from the kernel */ +static struct ipu3_uapi_af_config_s imgu_css_af_defaults = { + .filter_config = { + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 128 }, + 0, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 128 }, + 0, + .y_calc = { 8, 8, 8, 8 }, + .nf = { 0, 7, 0, 7, 0 }, + }, + .grid_cfg = { + .width = 16, + .height = 16, + .block_width_log2 = 3, + .block_height_log2 = 3, + .x_start = 10, + .y_start = 2 | IPU3_UAPI_GRID_Y_START_EN, + }, +}; + +Af::Af() + : focus_(0), goodFocus_(0), currentVariance_(0.0), previousVariance_(0.0), + pass1Done_(false), pass2Done_(false) +{ + maxStep_ = MaxFocusSteps_; +} + +Af::~Af() +{ +} + +void Af::prepare(IPAContext &context, ipu3_uapi_params *params) +{ + params->use.acc_af = 1; + params->acc_param.af = imgu_css_af_defaults; + params->acc_param.af.grid_cfg.x_start = context.configuration.af.start_x; + params->acc_param.af.grid_cfg.y_start = context.configuration.af.start_y | IPU3_UAPI_GRID_Y_START_EN; +} + +/** + * \brief Configure the Af given a configInfo + * \param[in] context The shared IPA context + * \param[in] configInfo The IPA configuration data + * + * \return 0 + */ +int Af::configure(IPAContext &context, const IPAConfigInfo &configInfo) +{ + /* determined focus value i.e. current focus value */ + context.frameContext.af.focus = 0; + /* maximum variance of the AF statistics */ + context.frameContext.af.maxVariance = 0; + /* is focused? if it is true, the AF should be in a stable state. */ + context.frameContext.af.stable = false; + /* frame to be ignored before start to estimate AF variance. */ + ignoreFrame_ = 10; + + /* + * AF default area configuration + * Move AF area to the center of the image. + */ + /* Default AF width is 16x8 = 128 */ + context.configuration.af.start_x = (configInfo.bdsOutputSize.width / 2) - 64; + context.configuration.af.start_y = (configInfo.bdsOutputSize.height / 2) - 64; + + LOG(IPU3Af, Debug) << "BDS X: " + << configInfo.bdsOutputSize.width + << " Y: " + << configInfo.bdsOutputSize.height; + LOG(IPU3Af, Debug) << "AF start from X: " + << context.configuration.af.start_x + << " Y: " + << context.configuration.af.start_y; + + return 0; +} + +/** + * \brief AF coarse scan + * \param[in] context The shared IPA context + * + */ +void Af::af_coarse_scan(IPAContext &context) +{ + if (pass1Done_ == true) + return; + + if (af_scan(context, coarseSearchStep_)) { + pass1Done_ = true; + context.frameContext.af.maxVariance = 0; + focus_ = context.frameContext.af.focus - (context.frameContext.af.focus * 0.1); + context.frameContext.af.focus = focus_; + previousVariance_ = 0; + maxStep_ = focus_ + (focus_ * 0.2); + } +} + +/** + * \brief AF fine scan + * \param[in] context The shared IPA context + * + */ +void Af::af_fine_scan(IPAContext &context) +{ + if (pass1Done_ != true) + return; + + if (af_scan(context, fineSearchStep_)) { + context.frameContext.af.stable = true; + pass2Done_ = true; + } +} + +/** + * \brief AF reset + * \param[in] context The shared IPA context + * + */ +void Af::af_reset(IPAContext &context) +{ + context.frameContext.af.maxVariance = 0; + context.frameContext.af.focus = 0; + focus_ = 0; + context.frameContext.af.stable = false; + ignoreFrame_ = 60; + previousVariance_ = 0.0; + pass1Done_ = false; + pass2Done_ = false; + maxStep_ = MaxFocusSteps_; +} + +/** + * \brief AF scan + * \param[in] context The shared IPA context + * + * \return True, if it finds a AF value. + */ +bool Af::af_scan(IPAContext &context, int min_step) +{ + /* find the maximum variance during the AF scan using a greedy strategy */ + if (currentVariance_ > context.frameContext.af.maxVariance) { + context.frameContext.af.maxVariance = currentVariance_; + goodFocus_ = focus_; + } + + if (focus_ > maxStep_) { + /* if reach the max step, move lens to the position and set "focus stable". */ + context.frameContext.af.focus = goodFocus_; + return true; + } else { + /* check negative gradient */ + if ((currentVariance_ - context.frameContext.af.maxVariance) > -(context.frameContext.af.maxVariance * 0.15)) { + focus_ += min_step; + context.frameContext.af.focus = focus_; + } else { + context.frameContext.af.focus = goodFocus_; + previousVariance_ = currentVariance_; + return true; + } + } + LOG(IPU3Af, Debug) << "Variance prevrious: " + << previousVariance_ + << " current: " + << currentVariance_ + << " Diff: " + << (currentVariance_ - context.frameContext.af.maxVariance); + previousVariance_ = currentVariance_; + LOG(IPU3Af, Debug) << "Focus searching max variance is: " + << context.frameContext.af.maxVariance + << " Focus step is " + << goodFocus_ + << " Current scan is " + << focus_; + return false; +} + +/** + * \brief Determine the max contrast image and lens position. y_table is the + * statictic data from IPU3 and is composed of low pass and high pass filtered + * value. High pass filtered value also represents the sharpness of the image. + * Based on this, if the image with highest variance of the high pass filtered + * value (contrast) during the AF scan, the position of the len should be the + * best focus. + * \param[in] context The shared IPA context. + * \param[in] stats The statistic buffer of 3A from the IPU3. + */ +void Af::process(IPAContext &context, const ipu3_uapi_stats_3a *stats) +{ + uint32_t total = 0; + double mean; + uint64_t var_sum = 0; + y_table_item_t *y_item; + int z = 0; + + y_item = (y_table_item_t *)stats->af_raw_buffer.y_table; + + /** + * Calculate the mean and the varience of each non-zero AF statistics, since IPU3 only determine the AF value + * for a given grid. + * For pass1: low pass results are used. + * For pass2: high pass results are used. + */ + if (pass1Done_) { + for (z = 0; z < (IPU3_UAPI_AF_Y_TABLE_MAX_SIZE) / 4; z++) { + total = total + y_item[z].y2_avg; + if (y_item[z].y2_avg == 0) + break; + } + mean = total / z; + + for (z = 0; z < (IPU3_UAPI_AF_Y_TABLE_MAX_SIZE) / 4 && y_item[z].y2_avg != 0; z++) { + var_sum = var_sum + ((y_item[z].y2_avg - mean) * (y_item[z].y2_avg - mean)); + if (y_item[z].y2_avg == 0) + break; + } + } else { + for (z = 0; z < (IPU3_UAPI_AF_Y_TABLE_MAX_SIZE) / 4; z++) { + total = total + y_item[z].y1_avg; + if (y_item[z].y1_avg == 0) + break; + } + mean = total / z; + + for (z = 0; z < (IPU3_UAPI_AF_Y_TABLE_MAX_SIZE) / 4 && y_item[z].y1_avg != 0; z++) { + var_sum = var_sum + ((y_item[z].y1_avg - mean) * (y_item[z].y1_avg - mean)); + if (y_item[z].y1_avg == 0) + break; + } + } + /* Determine the average variance of the frame. */ + currentVariance_ = static_cast(var_sum) / static_cast(z); + LOG(IPU3Af, Debug) << "variance: " << currentVariance_; + + if (context.frameContext.af.stable == true) { + const uint32_t diff_var = std::abs(currentVariance_ - context.frameContext.af.maxVariance); + const double var_ratio = diff_var / context.frameContext.af.maxVariance; + LOG(IPU3Af, Debug) << "Change ratio: " + << var_ratio + << " current focus: " + << context.frameContext.af.focus; + /** + * If the change ratio of contrast is over Maxchange_ (out of focus), + * trigger AF again. + */ + if (var_ratio > MaxChange_) { + if (ignoreFrame_ == 0) { + af_reset(context); + } else + ignoreFrame_--; + } else + ignoreFrame_ = 10; + } else { + if (ignoreFrame_ != 0) + ignoreFrame_--; + else { + af_coarse_scan(context); + af_fine_scan(context); + } + } +} + +} /* namespace ipa::ipu3::algorithms */ + +} /* namespace libcamera */ diff --git a/src/ipa/ipu3/algorithms/af.h b/src/ipa/ipu3/algorithms/af.h new file mode 100644 index 00000000..b9295b19 --- /dev/null +++ b/src/ipa/ipu3/algorithms/af.h @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Red Hat + * + * af.h - IPU3 Af control + */ +#ifndef __LIBCAMERA_IPU3_ALGORITHMS_AF_H__ +#define __LIBCAMERA_IPU3_ALGORITHMS_AF_H__ + +#include + +#include + +#include + +#include "algorithm.h" + +namespace libcamera { + +namespace ipa::ipu3::algorithms { + +class Af : public Algorithm +{ + /* The format of y_table. From ipu3-ipa repo */ + typedef struct y_table_item { + uint16_t y1_avg; + uint16_t y2_avg; + } y_table_item_t; + +public: + Af(); + ~Af(); + + void prepare(IPAContext &context, ipu3_uapi_params *params) override; + int configure(IPAContext &context, const IPAConfigInfo &configInfo) override; + void process(IPAContext &context, const ipu3_uapi_stats_3a *stats) override; + +private: + void af_coarse_scan(IPAContext &context); + void af_fine_scan(IPAContext &context); + bool af_scan(IPAContext &context, int min_step); + void af_reset(IPAContext &context); + + /* Used for focus scan. */ + uint32_t focus_; + /* Focus good */ + uint32_t goodFocus_; + /* Recent AF statistic variance. */ + double currentVariance_; + /* The frames to be ignore before starting measuring. */ + uint32_t ignoreFrame_; + /* previous variance. it is used to determine the gradient */ + double previousVariance_; + /* Max scan steps of each pass of AF scaning */ + uint32_t maxStep_; + /* Pass 1 stable. Complete low pass search (coarse) scan) */ + bool pass1Done_; + /* Pass 2 stable. Complete high pass scan (fine scan) */ + bool pass2Done_; +}; + +} /* namespace ipa::ipu3::algorithms */ + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_IPU3_ALGORITHMS_AF_H__ */ diff --git a/src/ipa/ipu3/algorithms/meson.build b/src/ipa/ipu3/algorithms/meson.build index 4db6ae1d..e1099169 100644 --- a/src/ipa/ipu3/algorithms/meson.build +++ b/src/ipa/ipu3/algorithms/meson.build @@ -1,8 +1,9 @@ # SPDX-License-Identifier: CC0-1.0 ipu3_ipa_algorithms = files([ + 'af.cpp', 'agc.cpp', 'awb.cpp', 'blc.cpp', - 'tone_mapping.cpp', + 'tone_mapping.cpp' ]) diff --git a/src/ipa/ipu3/ipa_context.cpp b/src/ipa/ipu3/ipa_context.cpp index 86794ac1..ee644d3c 100644 --- a/src/ipa/ipu3/ipa_context.cpp +++ b/src/ipa/ipu3/ipa_context.cpp @@ -67,6 +67,33 @@ namespace libcamera::ipa::ipu3 { * * \var IPASessionConfiguration::grid.stride * \brief Number of cells on one line including the ImgU padding + * + */ + +/** + * \var IPASessionConfiguration::af + * \brief AF parameters configuration of the IPA + * + * \var IPASessionConfiguration::af.start_x + * \brief The start X position of the AF area + * + * \var IPASessionConfiguration::af.start_y + * \brief The start Y position of the AF area + */ + +/** + * \var IPAFrameContext::af + * \brief Context for the Automatic Focus algorithm + * + * \struct IPAFrameContext::af + * \var IPAFrameContext::af.focus + * \brief Current position of the lens + * + * \var IPAFrameContext::af.maxVariance + * \brief The maximum variance of the current image. + * + * \var IPAFrameContext::af.stable + * \brief is the image focused? */ /** diff --git a/src/ipa/ipu3/ipa_context.h b/src/ipa/ipu3/ipa_context.h index c6dc0814..aa5bf97f 100644 --- a/src/ipa/ipu3/ipa_context.h +++ b/src/ipa/ipu3/ipa_context.h @@ -31,6 +31,11 @@ struct IPASessionConfiguration { double minAnalogueGain; double maxAnalogueGain; } agc; + + struct { + uint16_t start_x; + uint16_t start_y; + } af; }; struct IPAFrameContext { @@ -49,6 +54,12 @@ struct IPAFrameContext { double temperatureK; } awb; + struct { + uint32_t focus; + double maxVariance; + bool stable; + } af; + struct { uint32_t exposure; double gain; diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp index 3d307708..93966c6f 100644 --- a/src/ipa/ipu3/ipu3.cpp +++ b/src/ipa/ipu3/ipu3.cpp @@ -30,6 +30,7 @@ #include "libcamera/internal/mapped_framebuffer.h" +#include "algorithms/af.h" #include "algorithms/agc.h" #include "algorithms/algorithm.h" #include "algorithms/awb.h" @@ -294,6 +295,7 @@ int IPAIPU3::init(const IPASettings &settings, } /* Construct our Algorithms */ + algorithms_.push_back(std::make_unique()); algorithms_.push_back(std::make_unique()); algorithms_.push_back(std::make_unique()); algorithms_.push_back(std::make_unique());