From patchwork Wed Mar 23 13:36:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 15515 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 8E3B9C0F1B for ; Wed, 23 Mar 2022 13:36:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6A01C604DC; Wed, 23 Mar 2022 14:36:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1648042589; bh=hAYcoqx5KqhNup/OmO2+ilIeuLssAinCiaas26Uw+ww=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=p4sfgS2hW9rv2OHN4un4X3jTgm8PF8l/l4izuOxvIeSEdhD79UQGvxeq9DLZ1jCJG X6Bb+m+OSYJRYTom6CYm7saoInGkD3yqVBX0TjOlGSq5lcZ+rtMnPlZjPZ5C0x0d3U HZ0b5ckl083WujR+MFQ4ftAwPI5lwQcirs3rRTkQ4mUl3YUUNnl0HJdeIBR9/EjTYD jyrOSwKWbOAhVLZJKJ/H5I61AIch5PMkL2F3N9pDluCWaR2LivOFboj7egv6Gsmu3J 1sos7nMMmT6VLe0JFi3UnhK45yqZlaGS0FViuMKg3hq//phvKQYRC3HvMgivoR1bhU eTKivHOCsXwvQ== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E784F604C6 for ; Wed, 23 Mar 2022 14:36:26 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="L1JIrIKJ"; dkim-atps=neutral Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:2e05:b10:1542:d51b]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 84F56130D; Wed, 23 Mar 2022 14:36:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1648042586; bh=hAYcoqx5KqhNup/OmO2+ilIeuLssAinCiaas26Uw+ww=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L1JIrIKJJXYaEEsxylwawozgzpSbJlwUM+AamG3X3Rc40eRDhCJ5gzWW7imjl+Mqf FGluAjjc+nGqBUwq4EmqJK0/AAH983uk/Bq5VV0Ut/1THxaGEsumKUeYi8JvBa4eFs 6ZSgj5YhSnZH8LvgZMtyxxkK6iX/Czigi/HdJ+4M= To: libcamera-devel@lists.libcamera.org Date: Wed, 23 Mar 2022 14:36:20 +0100 Message-Id: <20220323133622.61593-2-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220323133622.61593-1-jeanmichel.hautbois@ideasonboard.com> References: <20220323133622.61593-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 1/3] ipa: raspberrypi: Introduce an autofocus algorithm 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: , X-Patchwork-Original-From: Jean-Michel Hautbois via libcamera-devel From: Jean-Michel Hautbois Reply-To: Jean-Michel Hautbois Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Now that the ancillary links are plumbed and we can set the lens position, implement a contrast-based algorithm for RPi. This algorithm is adapted from the one proposed for the IPU3 IPA. It is currently taking all the regions and tries to make the focus on the global scene in a first attempt. Signed-off-by: Jean-Michel Hautbois --- .../raspberrypi/controller/af_algorithm.hpp | 20 ++ src/ipa/raspberrypi/controller/af_status.h | 31 +++ src/ipa/raspberrypi/controller/focus_status.h | 3 + src/ipa/raspberrypi/controller/iob/af.cpp | 231 ++++++++++++++++++ src/ipa/raspberrypi/controller/iob/af.h | 55 +++++ src/ipa/raspberrypi/meson.build | 1 + 6 files changed, 341 insertions(+) create mode 100644 src/ipa/raspberrypi/controller/af_algorithm.hpp create mode 100644 src/ipa/raspberrypi/controller/af_status.h create mode 100644 src/ipa/raspberrypi/controller/iob/af.cpp create mode 100644 src/ipa/raspberrypi/controller/iob/af.h diff --git a/src/ipa/raspberrypi/controller/af_algorithm.hpp b/src/ipa/raspberrypi/controller/af_algorithm.hpp new file mode 100644 index 00000000..553a37e1 --- /dev/null +++ b/src/ipa/raspberrypi/controller/af_algorithm.hpp @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2019, Raspberry Pi (Trading) Limited + * + * af_algorithm.hpp - autofocus control algorithm interface + */ +#pragma once + +#include "algorithm.hpp" + +namespace RPiController { + +class AfAlgorithm : public Algorithm +{ +public: + AfAlgorithm(Controller *controller) : Algorithm(controller) {} + // An af algorithm must provide the following: +}; + +} // namespace RPiController diff --git a/src/ipa/raspberrypi/controller/af_status.h b/src/ipa/raspberrypi/controller/af_status.h new file mode 100644 index 00000000..835e1e2f --- /dev/null +++ b/src/ipa/raspberrypi/controller/af_status.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2020, Raspberry Pi (Trading) Limited + * Copyright (C) 2022, Ideas On Board + * + * af_status.h - autofocus measurement status + */ +#pragma once + +#include + +/* + * The focus algorithm should post the following structure into the image's + * "af.status" metadata. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +struct AfStatus { + unsigned int num; + uint32_t focus_measures[FOCUS_REGIONS]; + bool stable; + uint32_t focus; + double maxVariance; +}; + +#ifdef __cplusplus +} +#endif diff --git a/src/ipa/raspberrypi/controller/focus_status.h b/src/ipa/raspberrypi/controller/focus_status.h index ace2fe2c..8122df4b 100644 --- a/src/ipa/raspberrypi/controller/focus_status.h +++ b/src/ipa/raspberrypi/controller/focus_status.h @@ -19,6 +19,9 @@ extern "C" { struct FocusStatus { unsigned int num; uint32_t focus_measures[FOCUS_REGIONS]; + bool stable; + uint32_t focus; + double maxVariance; }; #ifdef __cplusplus diff --git a/src/ipa/raspberrypi/controller/iob/af.cpp b/src/ipa/raspberrypi/controller/iob/af.cpp new file mode 100644 index 00000000..dc5258ba --- /dev/null +++ b/src/ipa/raspberrypi/controller/iob/af.cpp @@ -0,0 +1,231 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Red Hat + * Copyright (C) 2022, Ideas On Board + * + * af.cpp - automatic contrast-based focus algorithm + */ +#include + +#include + +#include + +#include "af.h" + +using namespace RPiController; +using namespace libcamera; + +LOG_DEFINE_CATEGORY(IoBAf) + +#define NAME "iob.af" + +/* + * Maximum focus steps of the VCM control + * \todo should be obtained from the VCM driver + */ +static constexpr uint32_t kMaxFocusSteps = 1023; + +/* Minimum focus step for searching appropriate focus */ +static constexpr uint32_t kCoarseSearchStep = 30; +static constexpr uint32_t kFineSearchStep = 1; + +/* Max ratio of variance change, 0.0 < kMaxChange < 1.0 */ +static constexpr double kMaxChange = 0.5; + +/* The numbers of frame to be ignored, before performing focus scan. */ +static constexpr uint32_t kIgnoreFrame = 10; + +/* Fine scan range 0 < kFineRange < 1 */ +static constexpr double kFineRange = 0.05; + +Af::Af(Controller *controller) + : AfAlgorithm(controller), focus_(0), bestFocus_(0), ignoreCounter_(0), + currentVariance_(0.0), previousVariance_(0.0), maxStep_(0), + coarseCompleted_(false), fineCompleted_(false) +{ +} + +char const *Af::Name() const +{ + return NAME; +} + +void Af::Initialise() +{ + status_.focus = 0.0; + status_.maxVariance = 0.0; + status_.stable = false; +} + +void Af::Prepare(Metadata *image_metadata) +{ + image_metadata->Set("af.status", status_); +} + +double Af::estimateVariance() +{ + unsigned int i; + double mean; + uint64_t total = 0; + double var_sum = 0.0; + + /* Compute the mean value. */ + for (i = 0; i < FOCUS_REGIONS; i++) + total += status_.focus_measures[i]; + mean = total / FOCUS_REGIONS; + + /* Compute the sum of the squared variance. */ + for (i = 0; i < FOCUS_REGIONS; i++) + var_sum += std::pow(status_.focus_measures[i] - mean, 2); + + return var_sum / FOCUS_REGIONS; +} + +bool Af::afNeedIgnoreFrame() +{ + if (ignoreCounter_ == 0) + return false; + else + ignoreCounter_--; + return true; +} + +void Af::afCoarseScan() +{ + if (coarseCompleted_) + return; + + if (afNeedIgnoreFrame()) + return; + + if (afScan(kCoarseSearchStep)) { + coarseCompleted_ = true; + status_.maxVariance = 0; + focus_ = status_.focus - (status_.focus * kFineRange); + status_.focus = focus_; + previousVariance_ = 0; + maxStep_ = std::clamp(focus_ + static_cast((focus_ * kFineRange)), + 0U, kMaxFocusSteps); + } +} + +void Af::afFineScan() +{ + if (!coarseCompleted_) + return; + + if (afNeedIgnoreFrame()) + return; + + if (afScan(kFineSearchStep)) { + status_.stable = true; + fineCompleted_ = true; + } +} + +bool Af::afScan(uint32_t minSteps) +{ + if (focus_ > maxStep_) { + /* If the max step is reached, move lens to the position. */ + status_.focus = bestFocus_; + return true; + } else { + /* + * Find the maximum of the variance by estimating its + * derivative. If the direction changes, it means we have passed + * a maximum one step before. + */ + if ((currentVariance_ - status_.maxVariance) >= + -(status_.maxVariance * 0.1)) { + /* + * Positive and zero derivative: + * The variance is still increasing. The focus could be + * increased for the next comparison. Also, the max + * variance and previous focus value are updated. + */ + bestFocus_ = focus_; + focus_ += minSteps; + status_.focus = focus_; + status_.maxVariance = currentVariance_; + } else { + /* + * Negative derivative: + * The variance starts to decrease which means the maximum + * variance is found. Set focus step to previous good one + * then return immediately. + */ + status_.focus = bestFocus_; + return true; + } + } + + previousVariance_ = currentVariance_; + LOG(IoBAf, Debug) << " Previous step is " + << bestFocus_ + << " Current step is " + << focus_; + return false; +} + +void Af::afReset() +{ + if (afNeedIgnoreFrame()) + return; + + status_.maxVariance = 0; + status_.focus = 0; + focus_ = 0; + status_.stable = false; + ignoreCounter_ = kIgnoreFrame; + previousVariance_ = 0.0; + coarseCompleted_ = false; + fineCompleted_ = false; + maxStep_ = kMaxFocusSteps; +} + +bool Af::afIsOutOfFocus() +{ + const uint32_t diff_var = std::abs(currentVariance_ - + status_.maxVariance); + const double var_ratio = diff_var / status_.maxVariance; + LOG(IoBAf, Debug) << "Variance change rate: " + << var_ratio + << " Current VCM step: " + << status_.focus; + if (var_ratio > kMaxChange) + return true; + else + return false; +} + +void Af::Process(StatisticsPtr &stats, Metadata *image_metadata) +{ + unsigned int i; + image_metadata->Get("af.status", status_); + + /* Use the second filter results only, and cache those. */ + for (i = 0; i < FOCUS_REGIONS; i++) + status_.focus_measures[i] = stats->focus_stats[i].contrast_val[1][1] + / stats->focus_stats[i].contrast_val_num[1][1]; + status_.num = i; + + currentVariance_ = estimateVariance(); + + if (!status_.stable) { + afCoarseScan(); + afFineScan(); + } else { + if (afIsOutOfFocus()) + afReset(); + else + ignoreCounter_ = kIgnoreFrame; + } +} + +/* Register algorithm with the system. */ +static Algorithm *Create(Controller *controller) +{ + return new Af(controller); +} +static RegisterAlgorithm reg(NAME, &Create); diff --git a/src/ipa/raspberrypi/controller/iob/af.h b/src/ipa/raspberrypi/controller/iob/af.h new file mode 100644 index 00000000..45c9711f --- /dev/null +++ b/src/ipa/raspberrypi/controller/iob/af.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Red Hat + * Copyright (C) 2022, Ideas On Board + * + * af.h - automatic contrast-based focus algorithm + */ +#pragma once + +#include "../af_algorithm.hpp" +#include "../af_status.h" +#include "../metadata.hpp" + +namespace RPiController { + +class Af : public AfAlgorithm +{ +public: + Af(Controller *controller); + char const *Name() const override; + void Initialise() override; + void Prepare(Metadata *image_metadata) override; + void Process(StatisticsPtr &stats, Metadata *image_metadata) override; +private: + double estimateVariance(); + bool afNeedIgnoreFrame(); + void afCoarseScan(); + void afFineScan(); + bool afScan(uint32_t minSteps); + void afReset(); + bool afIsOutOfFocus(); + + AfStatus status_; + + /* VCM step configuration. It is the current setting of the VCM step. */ + uint32_t focus_; + /* The best VCM step. It is a local optimum VCM step during scanning. */ + uint32_t bestFocus_; + + /* The frames ignored before starting measuring. */ + uint32_t ignoreCounter_; + + /* Current AF statistic variance. */ + double currentVariance_; + /* It is used to determine the derivative during scanning */ + double previousVariance_; + /* The designated maximum range of focus scanning. */ + uint32_t maxStep_; + /* If the coarse scan completes, it is set to true. */ + bool coarseCompleted_; + /* If the fine scan completes, it is set to true. */ + bool fineCompleted_; +}; + +} /* namespace RPiController */ diff --git a/src/ipa/raspberrypi/meson.build b/src/ipa/raspberrypi/meson.build index 32897e07..37068ecc 100644 --- a/src/ipa/raspberrypi/meson.build +++ b/src/ipa/raspberrypi/meson.build @@ -28,6 +28,7 @@ rpi_ipa_sources = files([ 'controller/controller.cpp', 'controller/histogram.cpp', 'controller/algorithm.cpp', + 'controller/iob/af.cpp', 'controller/rpi/alsc.cpp', 'controller/rpi/awb.cpp', 'controller/rpi/sharpen.cpp', From patchwork Wed Mar 23 13:36:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 15516 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 5C110C3264 for ; Wed, 23 Mar 2022 13:36:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id E0EAB604DB; Wed, 23 Mar 2022 14:36:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1648042589; bh=EA287Hwt5glVUgdzaeQKWcbKnnGZWRTVqjWta/B9euI=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=gilN7xxpxirbM8goI9w1uV9x/GEX3AJPKKLT0Z2+2uwFR//vZeNtDRq/JAdM0zQoD 8iOjF+SEK+e/NgzIXEz2p/IMTxwFUAD7B92mmuPGhT6IVhDFIvSDwTyC2q31rgQkv1 CD0hYZJGyUWPJ1eg4iDqwi1K4/GEsjkk4XVDsai9aE2bvSyT2bTus1P9a/8aHNI3Xl DZJkV/ZVxWyNwANQmuUhRJthGVAeNWDthzdEFfWfoKQRooF+S40wOY2pWyUWfJYmFk fKr0iqmYRL2EDmvLZ2POXuOoPwMcfhnLLL/8LZ+JRa7qBOdeoru9wzYtBJXOHy1R2Q Zy4iYIa1oczlA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EF226604D5 for ; Wed, 23 Mar 2022 14:36:26 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="qtOY0oZA"; dkim-atps=neutral Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:2e05:b10:1542:d51b]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id ACA9F1645; Wed, 23 Mar 2022 14:36:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1648042586; bh=EA287Hwt5glVUgdzaeQKWcbKnnGZWRTVqjWta/B9euI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qtOY0oZAA6VF1CLAjo5CZ49vxrGV1rTdQ0CqRRhODhUbGmx6TX2Ayxwn8JJkeJ1WN e3AUSe/3LkIFn/CYqUKE+Dr+lL42R1fCeDt3kiZsB3fAcwNTUJcirpqgVu7zbL3dfi hVFfKGo/p5Aw19bSaoN+ClX7cRwjtw0EFT0Ffs6M= To: libcamera-devel@lists.libcamera.org Date: Wed, 23 Mar 2022 14:36:21 +0100 Message-Id: <20220323133622.61593-3-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220323133622.61593-1-jeanmichel.hautbois@ideasonboard.com> References: <20220323133622.61593-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 2/3] libcamera: raspberrypi: Control the lens from pipeline 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: , X-Patchwork-Original-From: Jean-Michel Hautbois via libcamera-devel From: Jean-Michel Hautbois Reply-To: Jean-Michel Hautbois Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The lens focus is controled by a VCM, which is linked to the sensor using the ancillary links. Pass the control to the config info structure and make it possible to update by the IPA. Signed-off-by: Jean-Michel Hautbois --- include/libcamera/ipa/raspberrypi.mojom | 1 + .../pipeline/raspberrypi/raspberrypi.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/libcamera/ipa/raspberrypi.mojom b/include/libcamera/ipa/raspberrypi.mojom index acd3cafe..0c3922b0 100644 --- a/include/libcamera/ipa/raspberrypi.mojom +++ b/include/libcamera/ipa/raspberrypi.mojom @@ -125,4 +125,5 @@ interface IPARPiEventInterface { embeddedComplete(uint32 bufferId); setIspControls(libcamera.ControlList controls); setDelayedControls(libcamera.ControlList controls); + setLensControls(libcamera.ControlList controls); }; diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index c2230199..970f9fe7 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -33,6 +33,7 @@ #include "libcamera/internal/bayer_format.h" #include "libcamera/internal/camera.h" +#include "libcamera/internal/camera_lens.h" #include "libcamera/internal/camera_sensor.h" #include "libcamera/internal/delayed_controls.h" #include "libcamera/internal/device_enumerator.h" @@ -202,6 +203,7 @@ public: void setIspControls(const ControlList &controls); void setDelayedControls(const ControlList &controls); void setSensorControls(ControlList &controls); + void setLensControls(const ControlList &controls); /* bufferComplete signal handlers. */ void unicamBufferDequeue(FrameBuffer *buffer); @@ -1483,6 +1485,7 @@ int RPiCameraData::loadIPA(ipa::RPi::SensorConfig *sensorConfig) ipa_->embeddedComplete.connect(this, &RPiCameraData::embeddedComplete); ipa_->setIspControls.connect(this, &RPiCameraData::setIspControls); ipa_->setDelayedControls.connect(this, &RPiCameraData::setDelayedControls); + ipa_->setLensControls.connect(this, &RPiCameraData::setLensControls); /* * The configuration (tuning file) is made from the sensor name unless @@ -1519,6 +1522,10 @@ int RPiCameraData::configureIPA(const CameraConfiguration *config) entityControls.emplace(0, sensor_->controls()); entityControls.emplace(1, isp_[Isp::Input].dev()->controls()); + CameraLens *lens = sensor_->focusLens(); + if (lens) + entityControls.emplace(2, lens->controls()); + /* Always send the user transform to the IPA. */ ipaConfig.transform = static_cast(config->transform); @@ -1735,6 +1742,16 @@ void RPiCameraData::setDelayedControls(const ControlList &controls) handleState(); } +void RPiCameraData::setLensControls(const ControlList &ctrls) +{ + CameraLens *lens = sensor_->focusLens(); + if (!lens) + return; + + /* \todo Should we keep track of the latest value applied ? */ + lens->setFocusPosition(ctrls.get(V4L2_CID_FOCUS_ABSOLUTE).get()); +} + void RPiCameraData::setSensorControls(ControlList &controls) { /* From patchwork Wed Mar 23 13:36:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Michel Hautbois X-Patchwork-Id: 15517 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 D155EC0F1B for ; Wed, 23 Mar 2022 13:36:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 529AF6118A; Wed, 23 Mar 2022 14:36:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1648042590; bh=mt2NjO5beNELiNjOzkMNAs3+1O7pEEsXNbhbqF3dxCU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=x4fREEsmghelKCgGbdiFuQxWQLHIxTpMuA2Sja2mukLFFVS2eT6DTC5PJht6SKQ5d E44kTftuG8VQwHZ7OxvulhW/84jsuqPesTWP2/jswofzE4fKK89NGaHxBwaI1nBZ/4 4a3P71k7b2f+9EQXqiOGHgYD24CY/DTTmY8LPSZ9Z7y/oxttHnjCZr6eTymnTbxPpQ ybyCImepBvbk5WSERzI7LeMRBXiGmcJojJZ/ZCT1gD8oEiXt87cyTmErlE+vCSxCBm P5uB9XJGRaAh70wB1F95ghub0aWazZUcDLbH+WS5UWM/jei3TfTsh/KOYOzB3SVqsn TNyvY4GjPW/rQ== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 27C80604DA for ; Wed, 23 Mar 2022 14:36:27 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="NlSWJz7u"; dkim-atps=neutral Received: from tatooine.ideasonboard.com (unknown [IPv6:2a01:e0a:169:7140:2e05:b10:1542:d51b]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D4A791856; Wed, 23 Mar 2022 14:36:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1648042586; bh=mt2NjO5beNELiNjOzkMNAs3+1O7pEEsXNbhbqF3dxCU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NlSWJz7uhF9xGE806CgNu66B2R1I0yHPdosPBN57tWuC4b0P2fG3YtjwHm9QvWhkH 8a8sFpe0kLZQ0YAFIkq+sYRRZFTAl1+nQOLd9ZxATVqNzWb3xaz3sNd7Zh9YIgqUmF u6ZwoGUJ8bN41CigbS4cs0Pjhb0zgIJgDKrDuUyQ= To: libcamera-devel@lists.libcamera.org Date: Wed, 23 Mar 2022 14:36:22 +0100 Message-Id: <20220323133622.61593-4-jeanmichel.hautbois@ideasonboard.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220323133622.61593-1-jeanmichel.hautbois@ideasonboard.com> References: <20220323133622.61593-1-jeanmichel.hautbois@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 3/3] ipa: raspberrypi: Control the lens position 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: , X-Patchwork-Original-From: Jean-Michel Hautbois via libcamera-devel From: Jean-Michel Hautbois Reply-To: Jean-Michel Hautbois Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Now that the ancillary links are configured, we can use the CameraLens class and control the VCM through the IPA. For now, force a default value for the lens position, until the AF algorithm is introduced. Signed-off-by: Jean-Michel Hautbois --- src/ipa/raspberrypi/raspberrypi.cpp | 36 +++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp index 1bf4e270..2e862f37 100644 --- a/src/ipa/raspberrypi/raspberrypi.cpp +++ b/src/ipa/raspberrypi/raspberrypi.cpp @@ -108,6 +108,7 @@ private: void setMode(const IPACameraSensorInfo &sensorInfo); bool validateSensorControls(); bool validateIspControls(); + bool validateLensControls(); void queueRequest(const ControlList &controls); void returnEmbeddedBuffer(unsigned int bufferId); void prepareISP(const ipa::RPi::ISPConfig &data); @@ -132,6 +133,7 @@ private: ControlInfoMap sensorCtrls_; ControlInfoMap ispCtrls_; + ControlInfoMap lensCtrls_; ControlList libcameraMetadata_; /* Camera sensor params. */ @@ -342,13 +344,14 @@ int IPARPi::configure(const IPACameraSensorInfo &sensorInfo, const ipa::RPi::IPAConfig &ipaConfig, ControlList *controls) { - if (entityControls.size() != 2) { - LOG(IPARPI, Error) << "No ISP or sensor controls found."; + if (entityControls.size() != 3) { + LOG(IPARPI, Error) << "No ISP, lens or sensor controls found."; return -1; } sensorCtrls_ = entityControls.at(0); ispCtrls_ = entityControls.at(1); + lensCtrls_ = entityControls.at(2); if (!validateSensorControls()) { LOG(IPARPI, Error) << "Sensor control validation failed."; @@ -360,6 +363,10 @@ int IPARPi::configure(const IPACameraSensorInfo &sensorInfo, return -1; } + if (!validateLensControls()) { + LOG(IPARPI, Error) << "Lens control validation failed."; + } + maxSensorGainCode_ = sensorCtrls_.at(V4L2_CID_ANALOGUE_GAIN).max().get(); /* Setup a metadata ControlList to output metadata. */ @@ -578,6 +585,23 @@ bool IPARPi::validateIspControls() return true; } +bool IPARPi::validateLensControls() +{ + static const uint32_t ctrls[] = { + V4L2_CID_FOCUS_ABSOLUTE, + }; + + for (auto c : ctrls) { + if (lensCtrls_.find(c) == lensCtrls_.end()) { + LOG(IPARPI, Error) << "Unable to find lens control " + << utils::hex(c); + return false; + } + } + + return true; +} + /* * Converting between enums (used in the libcamera API) and the names that * we use to identify different modes. Unfortunately, the conversion tables @@ -1066,6 +1090,14 @@ void IPARPi::processStats(unsigned int bufferId) setDelayedControls.emit(ctrls); } + + struct FocusStatus focusStatus; + if (rpiMetadata_.Get("af.status", focusStatus) == 0) { + ControlList lensCtrls(lensCtrls_); + lensCtrls.set(V4L2_CID_FOCUS_ABSOLUTE, + static_cast(focusStatus.focus)); + setLensControls.emit(lensCtrls); + } } void IPARPi::applyAWB(const struct AwbStatus *awbStatus, ControlList &ctrls)