From patchwork Mon Nov 15 05:44:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kate Hsuan X-Patchwork-Id: 14605 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 E8C58BF415 for ; Mon, 15 Nov 2021 05:44:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1255D60368; Mon, 15 Nov 2021 06:44:35 +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="ZqCeVEve"; 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 0BF8860120 for ; Mon, 15 Nov 2021 06:44:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1636955071; 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; bh=YlKRJ0UQHCMPalzQMv9GGF46L5HrIiTwnpQWbkn2mAw=; b=ZqCeVEveFIhmc7WZNJu0IA2dG79PBJyRbCm+12AxWv/iopcU2XzGp31KO7zgmGMis1E4T2 FBDwdfnDLnGl4i5ooJE2XnNWn4YpAIdd7yJW4p/QAdSr+111yD8PgtpuKtvSesaeEzpQnE NsUy9He1lnZ9OgxyAVsmHukqSzRyqFs= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-154-sSdJM9pWNbO2ZYCNasNj8g-1; Mon, 15 Nov 2021 00:44:24 -0500 X-MC-Unique: sSdJM9pWNbO2ZYCNasNj8g-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 12B921808303 for ; Mon, 15 Nov 2021 05:44:24 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.39.192.106]) by smtp.corp.redhat.com (Postfix) with ESMTP id 099065C1BB; Mon, 15 Nov 2021 05:44:21 +0000 (UTC) From: Kate Hsuan To: libcamera devel Date: Mon, 15 Nov 2021 13:44:00 +0800 Message-Id: <20211115054400.17797-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] [PATCH] 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. Signed-off-by: Kate Hsuan --- src/ipa/ipu3/algorithms/af.cpp | 165 ++++++++++++++++++++++++++++ src/ipa/ipu3/algorithms/af.h | 48 ++++++++ src/ipa/ipu3/algorithms/meson.build | 3 +- src/ipa/ipu3/ipa_context.h | 6 + src/ipa/ipu3/ipu3.cpp | 2 + 5 files changed, 223 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..c276b539 --- /dev/null +++ b/src/ipa/ipu3/algorithms/af.cpp @@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Red Hat + * + * af.cpp - IPU3 Af 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 { + +LOG_DEFINE_CATEGORY(IPU3Af) + +Af::Af(): focus_(0), maxVariance_(0.0), currentVar_(0.0) +{ + /* For surface Go 2 back camera VCM */ + vcmFd_ = open("/dev/v4l-subdev8", O_RDWR); +} + +Af::~Af() +{ + if(vcmFd_ != -1) + close(vcmFd_); +} + +int Af::configure(IPAContext &context, const IPAConfigInfo &configInfo) +{ + const IPAConfigInfo tmp __attribute__((unused)) = configInfo; + context.frameContext.af.focus = 0; + context.frameContext.af.maxVar = 0; + context.frameContext.af.stable = false; + + maxVariance_ = 0; + ignoreFrame_ = 100; + + return 0; +} + +void Af::vcmSet(int value) +{ + int ret; + struct v4l2_control ctrl; + if(vcmFd_ == -1) + return; + memset(&ctrl, 0, sizeof(struct v4l2_control)); + ctrl.id = V4L2_CID_FOCUS_ABSOLUTE; + ctrl.value = value; + ret = ioctl(vcmFd_, VIDIOC_S_CTRL, &ctrl); + +} + +void Af::process(IPAContext &context, const ipu3_uapi_stats_3a *stats ) +{ + typedef struct y_table_item { + uint16_t y1_avg; + uint16_t y2_avg; + }y_table_item_t; + + uint32_t total = 0; + double mean; + uint64_t var_sum = 0; + y_table_item_t *y_item; + + y_item = (y_table_item_t *)stats->af_raw_buffer.y_table; + int z = 0; + + 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; + } + currentVar_ = static_cast(var_sum) /static_cast(z); + LOG(IPU3Af, Debug) << "variance: " << currentVar_; + + if( context.frameContext.af.stable == true ) + { + const uint32_t diff_var = std::abs(currentVar_ - context.frameContext.af.maxVar); + const double var_ratio = diff_var / context.frameContext.af.maxVar; + LOG(IPU3Af, Debug) << "Change ratio: " + << var_ratio + << " current focus: " + << context.frameContext.af.focus; + if(var_ratio > 0.8) + { + if(ignoreFrame_ == 0) + { + context.frameContext.af.maxVar = 0; + context.frameContext.af.focus = 0; + focus_ = 0; + context.frameContext.af.stable = false; + ignoreFrame_ = 60; + } + else + ignoreFrame_--; + }else + ignoreFrame_ = 60; + }else + { + if(ignoreFrame_ != 0) + ignoreFrame_--; + else{ + if(currentVar_ > context.frameContext.af.maxVar) + { + context.frameContext.af.maxVar = currentVar_; + context.frameContext.af.focus = focus_; + } + + if(focus_ > 1023) + { + context.frameContext.af.stable = true; + vcmSet(context.frameContext.af.focus); + LOG(IPU3Af, Debug) << "Finall Focus " + << context.frameContext.af.focus; + }else + { + focus_ += 5; + vcmSet(focus_); + } + LOG(IPU3Af, Debug) << "Focus searching max var is: " + << context.frameContext.af.maxVar + << " Focus step is " + << context.frameContext.af.focus; + } + } +} + + +} /* namespace ipa::ipu3::algorithms */ + +} /* namespace libcamera */ \ No newline at end of file diff --git a/src/ipa/ipu3/algorithms/af.h b/src/ipa/ipu3/algorithms/af.h new file mode 100644 index 00000000..64c704b1 --- /dev/null +++ b/src/ipa/ipu3/algorithms/af.h @@ -0,0 +1,48 @@ +/* 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 +{ +public: + Af(); + ~Af(); + + int configure(IPAContext &context, const IPAConfigInfo &configInfo) override; + void process(IPAContext &context, const ipu3_uapi_stats_3a *stats) override; + +private: + void filterVariance(double new_var); + + void vcmSet(int value); + + int vcmFd_; + int focus_; + unsigned int ignoreFrame_; + double maxVariance_; + double currentVar_; + +}; + +} /* 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 3ec42f72..d32c61d2 100644 --- a/src/ipa/ipu3/algorithms/meson.build +++ b/src/ipa/ipu3/algorithms/meson.build @@ -5,5 +5,6 @@ ipu3_ipa_algorithms = files([ 'algorithm.cpp', 'awb.cpp', 'blc.cpp', - 'tone_mapping.cpp', + 'af.cpp', + 'tone_mapping.cpp' ]) diff --git a/src/ipa/ipu3/ipa_context.h b/src/ipa/ipu3/ipa_context.h index 1e46c61a..5d92f63c 100644 --- a/src/ipa/ipu3/ipa_context.h +++ b/src/ipa/ipu3/ipa_context.h @@ -47,6 +47,12 @@ struct IPAFrameContext { } gains; } awb; + struct { + uint32_t focus; + double maxVar; + bool stable; + } af; + struct { double gamma; struct ipu3_uapi_gamma_corr_lut gammaCorrection; diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp index 5c51607d..980815ee 100644 --- a/src/ipa/ipu3/ipu3.cpp +++ b/src/ipa/ipu3/ipu3.cpp @@ -31,6 +31,7 @@ #include "libcamera/internal/mapped_framebuffer.h" #include "algorithms/agc.h" +#include "algorithms/af.h" #include "algorithms/algorithm.h" #include "algorithms/awb.h" #include "algorithms/blc.h" @@ -298,6 +299,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());