From patchwork Tue May 18 10:07:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12309 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 1EAE0C31FF for ; Tue, 18 May 2021 10:09:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AF3AD68923; Tue, 18 May 2021 12:09:14 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="ipvv506i"; dkim-atps=neutral Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 30B8F68918 for ; Tue, 18 May 2021 12:09:12 +0200 (CEST) Received: by mail-wr1-x435.google.com with SMTP id j14so7804056wrq.5 for ; Tue, 18 May 2021 03:09:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pc4bXwGv6lhgm9H31iNKrGOKtTpaANgZvy51COpEY2A=; b=ipvv506irWktLmwQdw3/pZsiiSKvDUwI6Tr9eKOROdV2zZ2NgG0BvW+bPZrceLq0RL LNEP7uwEKERQuAu0UDXqxgWQHsD3i75SRVQwV5ifdZ4/prDd5lNf4kCfG39yymiWYNm7 LCXC+K+TeWIEr7CMT1ofK0jU4mQ4UyFh3rVncPp0Brz3qy0fNth/UxzqH96mu2VquVLS CtKxPgbc/AkrBgINXMzEWk9fywX2VtW2GEsUe/TA4U7bnixm6ufbo27+LEX0+H8LW7O/ uYRLPhK6KptYoTB3kceFU08jULYB1g9tpl6NUnfgH/44DM8SRwxbjpSyg05Bj1Nf5HKL l0BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pc4bXwGv6lhgm9H31iNKrGOKtTpaANgZvy51COpEY2A=; b=MgihViYRqUXSMgeTMMhfFybbreDYdhazSCCBguYn64m5vqYy9nf86SzkAtk40aVLtc UMl0RoMC1B1TG11FNgxEW3GuFABcnkS0Jo85EWsdyWKtUR8hUNKkErLCRSr11C+pFAQy 43Yz2BJRmS4orgFhlYMBQjL2WzNlTmwDB0F7FgH2uiaDjqWKqVgAQnlUJ6j3EtmGuO1W OGfvFnXNQzzXm5b2J/DJ+wkCvDLcbd0KCe2QeHrmSGVT+zzPps8Z1PUTnqmjcn7cM22r cv/q6I8+d/YdPX984De8K2RZYIJn11y02YA4jx8ykwS/3ge6WLHRp1Ok/57Cp9E6ItNE fpUA== X-Gm-Message-State: AOAM532AtT663BtuYkMnZLVe+hh73PS1Ot3JK3Hfit1kUXZW6SBwogj/ ke2kO/kLq2Dlhs9xqPasXOjsMxdVZIpCRQ== X-Google-Smtp-Source: ABdhPJz39hxjWULjwuwYawdOPhgcgxRFR7wsgKwphQK6Ncs8t1vQk03hsItTBgM5d0VfvnPthyb5Qg== X-Received: by 2002:a5d:6804:: with SMTP id w4mr5983183wru.363.1621332551493; Tue, 18 May 2021 03:09:11 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:34e4:187b:f2:ed28]) by smtp.gmail.com with ESMTPSA id o11sm6566682wrq.93.2021.05.18.03.09.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 May 2021 03:09:10 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 18 May 2021 11:07:03 +0100 Message-Id: <20210518100706.578526-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210518100706.578526-1-naush@raspberrypi.com> References: <20210518100706.578526-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/4] ipa: raspberrypi: Add helper macros for std::chrono::duration 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" A new RPiController::Duration typedef is defined to represent a std::chrono::duration type with double precision nanosecond timebase that will be used throughout. This minimises the loss of precision when converting timebases. A function for returning the duration count in any timebase is also provided. An operator << overload is define to help with displaying RPiController::Duration value in stream objects. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman --- src/ipa/raspberrypi/controller/duration.hpp | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/ipa/raspberrypi/controller/duration.hpp diff --git a/src/ipa/raspberrypi/controller/duration.hpp b/src/ipa/raspberrypi/controller/duration.hpp new file mode 100644 index 000000000000..98aa3d78f52f --- /dev/null +++ b/src/ipa/raspberrypi/controller/duration.hpp @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2021, Raspberry Pi (Trading) Limited + * + * duration.hpp - Helper macros for std::chrono::duration handling. + */ +#pragma once + +#include +#include +#include + +namespace RPiController { + +using Duration = std::chrono::duration; + +// Helper to convert and return the count of any std::chrono::duration type to +// another timebase. Use a double rep type to try and preserve precision. +template +static constexpr double DurationValue(T const &d) +{ + return std::chrono::duration_cast>(d).count(); +}; + +static inline std::ostream &operator<<(std::ostream &os, const Duration &duration) +{ + std::streamsize ss = os.precision(); + os << std::fixed << std::setprecision(2) << DurationValue(duration) << " us"; + os << std::setprecision(ss) << std::defaultfloat; + return os; +} + +} // namespace RPiController From patchwork Tue May 18 10:07:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12310 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 71ED5C31FF for ; Tue, 18 May 2021 10:09:17 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1384E68925; Tue, 18 May 2021 12:09:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="PYwHskUA"; dkim-atps=neutral Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B88D068918 for ; Tue, 18 May 2021 12:09:12 +0200 (CEST) Received: by mail-wr1-x42f.google.com with SMTP id i17so9515847wrq.11 for ; Tue, 18 May 2021 03:09:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NCIOs3bWMPbQlzgpBqn2iparYJkE6e2ot9O/kNv2k7U=; b=PYwHskUA9vvBPtIZ5mqSRhFqEwHpkVVo9MSjENEPBW9tM7j+Ct8SMGScmzPrF8klWs m802f0gl9K6NJVrELfswley+xeX4gHYosn6Ys9nmKmyRdTbi3C590eaZEmB8LC44zpf8 UoPRRTD1bZAE7u1ZZgPeEvjXjCeKXMC/j9mYcm+hGnKHYtEGrU2PXi38jmWQo6ppRJ+u 268j5fkWpITw7O77pAulBRTZbS/PPwKTiYUaCDzRujKWzcId2qMuXoDAZM3xR9ss6BQa RfbuKuQCdZM275K3WF/r8oVuj2Qjicqn7F4Nqk9C4VD0262maUU+FLJrggwfXq3hHVE7 vlpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NCIOs3bWMPbQlzgpBqn2iparYJkE6e2ot9O/kNv2k7U=; b=HXNL0LHBr2aSds/ttUug7Nu62IcBgAiT4OoXWIlEQ6Hmk92WzZco4OmHqkbGgzOGyH NnvhOq3brHlj28LSQrjOWpuGI3hjx2m7ykF6ZdNqHAMAwt0BLhpk4sY9fgtRFK5VygpM o2pLEqoIJTmIGCciMZRNCnVi1HNr8VplUFTWnM8Lc1CCDOAwgFGpzUs+oRNkmIugbq24 W4Yf8/z/uiTFU4ZyLQfJa3Ay90QWbZc2UpqvmQHMhFTN9ImsHipzqPiVeVH/gZy17ngk 3Cy/ROutzsbvUQO6ykmJk2WhE0rIoWNdnWHDfMp+NkmppOgwRpGqkiJqKgBEXf+I9z9+ lkPQ== X-Gm-Message-State: AOAM530gBZ1zN4/rGMQ/5g3svj9P7OvFHPGdqxdXWKsueNc0eISdvwZ9 MAR+LmpoxwFlPZ96fuAIoj7HBZ5U2lzUJg== X-Google-Smtp-Source: ABdhPJxzd5PaMoBnAEbSHjRst8jx49c0Fa3cSSr5Xwj0BnTM/Cjt9L+UWhje+YSuwxH56+SuAnAKLg== X-Received: by 2002:a05:6000:44:: with SMTP id k4mr5905595wrx.76.1621332551983; Tue, 18 May 2021 03:09:11 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:34e4:187b:f2:ed28]) by smtp.gmail.com with ESMTPSA id o11sm6566682wrq.93.2021.05.18.03.09.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 May 2021 03:09:11 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 18 May 2021 11:07:04 +0100 Message-Id: <20210518100706.578526-3-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210518100706.578526-1-naush@raspberrypi.com> References: <20210518100706.578526-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/4] ipa: raspberrypi: Switch ipa/cam_helper to use RPiController::Duration 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" Switch the ipa and cam_helper code to use RPiController::Duration for all time based variables. This improves code readability and avoids possible errors when converting between time bases. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman --- src/ipa/raspberrypi/cam_helper.cpp | 19 +++--- src/ipa/raspberrypi/cam_helper.hpp | 10 +-- src/ipa/raspberrypi/controller/camera_mode.h | 5 +- src/ipa/raspberrypi/raspberrypi.cpp | 67 +++++++++++--------- 4 files changed, 56 insertions(+), 45 deletions(-) diff --git a/src/ipa/raspberrypi/cam_helper.cpp b/src/ipa/raspberrypi/cam_helper.cpp index 09917f3cc079..e2b6c8eb8e03 100644 --- a/src/ipa/raspberrypi/cam_helper.cpp +++ b/src/ipa/raspberrypi/cam_helper.cpp @@ -61,20 +61,21 @@ void CamHelper::Process([[maybe_unused]] StatisticsPtr &stats, { } -uint32_t CamHelper::ExposureLines(double exposure_us) const +uint32_t CamHelper::ExposureLines(Duration exposure) const { assert(initialized_); - return exposure_us * 1000.0 / mode_.line_length; + return exposure / mode_.line_length; } -double CamHelper::Exposure(uint32_t exposure_lines) const +Duration CamHelper::Exposure(uint32_t exposure_lines) const { assert(initialized_); - return exposure_lines * mode_.line_length / 1000.0; + return exposure_lines * mode_.line_length; } -uint32_t CamHelper::GetVBlanking(double &exposure, double minFrameDuration, - double maxFrameDuration) const +uint32_t CamHelper::GetVBlanking(Duration &exposure, + Duration minFrameDuration, + Duration maxFrameDuration) const { uint32_t frameLengthMin, frameLengthMax, vblank; uint32_t exposureLines = ExposureLines(exposure); @@ -85,8 +86,8 @@ uint32_t CamHelper::GetVBlanking(double &exposure, double minFrameDuration, * minFrameDuration and maxFrameDuration are clamped by the caller * based on the limits for the active sensor mode. */ - frameLengthMin = 1e3 * minFrameDuration / mode_.line_length; - frameLengthMax = 1e3 * maxFrameDuration / mode_.line_length; + frameLengthMin = minFrameDuration / mode_.line_length; + frameLengthMax = maxFrameDuration / mode_.line_length; /* * Limit the exposure to the maximum frame duration requested, and @@ -182,7 +183,7 @@ void CamHelper::parseEmbeddedData(Span buffer, return; } - deviceStatus.shutter_speed = Exposure(exposureLines); + deviceStatus.shutter_speed = DurationValue(Exposure(exposureLines)); deviceStatus.analogue_gain = Gain(gainCode); LOG(IPARPI, Debug) << "Metadata updated - Exposure : " diff --git a/src/ipa/raspberrypi/cam_helper.hpp b/src/ipa/raspberrypi/cam_helper.hpp index a52f3f0b583c..a91afaa59909 100644 --- a/src/ipa/raspberrypi/cam_helper.hpp +++ b/src/ipa/raspberrypi/cam_helper.hpp @@ -13,6 +13,7 @@ #include "camera_mode.h" #include "controller/controller.hpp" #include "controller/metadata.hpp" +#include "duration.hpp" #include "md_parser.hpp" #include "libcamera/internal/v4l2_videodevice.h" @@ -72,10 +73,11 @@ public: virtual void Prepare(libcamera::Span buffer, Metadata &metadata); virtual void Process(StatisticsPtr &stats, Metadata &metadata); - uint32_t ExposureLines(double exposure_us) const; - double Exposure(uint32_t exposure_lines) const; // in us - virtual uint32_t GetVBlanking(double &exposure_us, double minFrameDuration, - double maxFrameDuration) const; + uint32_t ExposureLines(Duration exposure) const; + Duration Exposure(uint32_t exposure_lines) const; + virtual uint32_t GetVBlanking(Duration &exposure, + Duration minFrameDuration, + Duration maxFrameDuration) const; virtual uint32_t GainCode(double gain) const = 0; virtual double Gain(uint32_t gain_code) const = 0; virtual void GetDelays(int &exposure_delay, int &gain_delay, diff --git a/src/ipa/raspberrypi/controller/camera_mode.h b/src/ipa/raspberrypi/controller/camera_mode.h index 256438c931d9..ab7cf2912a06 100644 --- a/src/ipa/raspberrypi/controller/camera_mode.h +++ b/src/ipa/raspberrypi/controller/camera_mode.h @@ -6,6 +6,7 @@ */ #pragma once +#include "duration.hpp" #include // Description of a "camera mode", holding enough information for control @@ -33,8 +34,8 @@ struct CameraMode { double scale_x, scale_y; // scaling of the noise compared to the native sensor mode double noise_factor; - // line time in nanoseconds - double line_length; + // line time + RPiController::Duration line_length; // any camera transform *not* reflected already in the camera tuning libcamera::Transform transform; // minimum and maximum fame lengths in units of lines diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp index 52d91db282ea..994fb7e057a9 100644 --- a/src/ipa/raspberrypi/raspberrypi.cpp +++ b/src/ipa/raspberrypi/raspberrypi.cpp @@ -45,6 +45,7 @@ #include "denoise_algorithm.hpp" #include "denoise_status.h" #include "dpc_status.h" +#include "duration.hpp" #include "focus_status.h" #include "geq_status.h" #include "lux_status.h" @@ -55,19 +56,24 @@ namespace libcamera { +using namespace std::literals::chrono_literals; +using RPiController::Duration; +using RPiController::DurationValue; +using RPiController::operator<<; + /* Configure the sensor with these values initially. */ constexpr double DefaultAnalogueGain = 1.0; -constexpr unsigned int DefaultExposureTime = 20000; -constexpr double defaultMinFrameDuration = 1e6 / 30.0; -constexpr double defaultMaxFrameDuration = 1e6 / 0.01; +constexpr Duration DefaultExposureTime = 20.0ms; +constexpr Duration defaultMinFrameDuration = 1.0s / 30.0; +constexpr Duration defaultMaxFrameDuration = 100.0s; /* - * Determine the minimum allowable inter-frame duration (in us) to run the - * controller algorithms. If the pipeline handler provider frames at a rate - * higher than this, we rate-limit the controller Prepare() and Process() calls - * to lower than or equal to this rate. + * Determine the minimum allowable inter-frame duration to run the controller + * algorithms. If the pipeline handler provider frames at a rate higher than this, + * we rate-limit the controller Prepare() and Process() calls to lower than or + * equal to this rate. */ -constexpr double controllerMinFrameDuration = 1e6 / 60.0; +constexpr Duration controllerMinFrameDuration = 1.0s / 60.0; LOG_DEFINE_CATEGORY(IPARPI) @@ -111,7 +117,8 @@ private: void reportMetadata(); void fillDeviceStatus(const ControlList &sensorControls); void processStats(unsigned int bufferId); - void applyFrameDurations(double minFrameDuration, double maxFrameDuration); + void applyFrameDurations(const Duration &minFrameDuration, + const Duration &maxFrameDuration); void applyAGC(const struct AgcStatus *agcStatus, ControlList &ctrls); void applyAWB(const struct AwbStatus *awbStatus, ControlList &ctrls); void applyDG(const struct AgcStatus *dgStatus, ControlList &ctrls); @@ -167,9 +174,9 @@ private: /* Distinguish the first camera start from others. */ bool firstStart_; - /* Frame duration (1/fps) limits, given in microseconds. */ - double minFrameDuration_; - double maxFrameDuration_; + /* Frame duration (1/fps) limits. */ + Duration minFrameDuration_; + Duration maxFrameDuration_; }; int IPARPi::init(const IPASettings &settings, ipa::RPi::SensorConfig *sensorConfig) @@ -314,7 +321,7 @@ void IPARPi::setMode(const CameraSensorInfo &sensorInfo) * Calculate the line length in nanoseconds as the ratio between * the line length in pixels and the pixel rate. */ - mode_.line_length = 1e9 * sensorInfo.lineLength / sensorInfo.pixelRate; + mode_.line_length = sensorInfo.lineLength * (1.0s / sensorInfo.pixelRate); /* * Set the frame length limits for the mode to ensure exposure and @@ -387,7 +394,7 @@ int IPARPi::configure(const CameraSensorInfo &sensorInfo, /* Supply initial values for gain and exposure. */ ControlList ctrls(sensorCtrls_); AgcStatus agcStatus; - agcStatus.shutter_time = DefaultExposureTime; + agcStatus.shutter_time = DurationValue(DefaultExposureTime); agcStatus.analogue_gain = DefaultAnalogueGain; applyAGC(&agcStatus, ctrls); @@ -862,7 +869,7 @@ void IPARPi::queueRequest(const ControlList &controls) case controls::FRAME_DURATIONS: { auto frameDurations = ctrl.second.get>(); - applyFrameDurations(frameDurations[0], frameDurations[1]); + applyFrameDurations(frameDurations[0] * 1.0us, frameDurations[1] * 1.0us); break; } @@ -937,9 +944,9 @@ void IPARPi::prepareISP(const ipa::RPi::ISPConfig &data) returnEmbeddedBuffer(data.embeddedBufferId); /* Allow a 10% margin on the comparison below. */ - constexpr double eps = controllerMinFrameDuration * 1e3 * 0.1; + Duration delta = (frameTimestamp - lastRunTimestamp_) * 1.0ns; if (lastRunTimestamp_ && frameCount_ > dropFrameCount_ && - frameTimestamp - lastRunTimestamp_ + eps < controllerMinFrameDuration * 1e3) { + delta < controllerMinFrameDuration * 0.9) { /* * Ensure we merge the previous frame's metadata with the current * frame. This will not overwrite exposure/gain values for the @@ -1012,7 +1019,7 @@ void IPARPi::fillDeviceStatus(const ControlList &sensorControls) int32_t exposureLines = sensorControls.get(V4L2_CID_EXPOSURE).get(); int32_t gainCode = sensorControls.get(V4L2_CID_ANALOGUE_GAIN).get(); - deviceStatus.shutter_speed = helper_->Exposure(exposureLines); + deviceStatus.shutter_speed = DurationValue(helper_->Exposure(exposureLines)); deviceStatus.analogue_gain = helper_->Gain(gainCode); LOG(IPARPI, Debug) << "Metadata - Exposure : " @@ -1057,17 +1064,18 @@ void IPARPi::applyAWB(const struct AwbStatus *awbStatus, ControlList &ctrls) static_cast(awbStatus->gain_b * 1000)); } -void IPARPi::applyFrameDurations(double minFrameDuration, double maxFrameDuration) +void IPARPi::applyFrameDurations(const Duration &minFrameDuration, + const Duration &maxFrameDuration) { - const double minSensorFrameDuration = 1e-3 * mode_.min_frame_length * mode_.line_length; - const double maxSensorFrameDuration = 1e-3 * mode_.max_frame_length * mode_.line_length; + const Duration minSensorFrameDuration = mode_.min_frame_length * mode_.line_length; + const Duration maxSensorFrameDuration = mode_.max_frame_length * mode_.line_length; /* * This will only be applied once AGC recalculations occur. * The values may be clamped based on the sensor mode capabilities as well. */ - minFrameDuration_ = minFrameDuration ? minFrameDuration : defaultMaxFrameDuration; - maxFrameDuration_ = maxFrameDuration ? maxFrameDuration : defaultMinFrameDuration; + minFrameDuration_ = (minFrameDuration > 0.0s) ? minFrameDuration : defaultMaxFrameDuration; + maxFrameDuration_ = (maxFrameDuration > 0.0s) ? maxFrameDuration : defaultMinFrameDuration; minFrameDuration_ = std::clamp(minFrameDuration_, minSensorFrameDuration, maxSensorFrameDuration); maxFrameDuration_ = std::clamp(maxFrameDuration_, @@ -1076,20 +1084,20 @@ void IPARPi::applyFrameDurations(double minFrameDuration, double maxFrameDuratio /* Return the validated limits via metadata. */ libcameraMetadata_.set(controls::FrameDurations, - { static_cast(minFrameDuration_), - static_cast(maxFrameDuration_) }); + { static_cast(DurationValue(minFrameDuration_)), + static_cast(DurationValue(minFrameDuration_)) }); /* * Calculate the maximum exposure time possible for the AGC to use. * GetVBlanking() will update maxShutter with the largest exposure * value possible. */ - double maxShutter = std::numeric_limits::max(); + Duration maxShutter = Duration::max(); helper_->GetVBlanking(maxShutter, minFrameDuration_, maxFrameDuration_); RPiController::AgcAlgorithm *agc = dynamic_cast( controller_.GetAlgorithm("agc")); - agc->SetMaxShutter(maxShutter); + agc->SetMaxShutter(DurationValue(maxShutter)); } void IPARPi::applyAGC(const struct AgcStatus *agcStatus, ControlList &ctrls) @@ -1097,9 +1105,8 @@ void IPARPi::applyAGC(const struct AgcStatus *agcStatus, ControlList &ctrls) int32_t gainCode = helper_->GainCode(agcStatus->analogue_gain); /* GetVBlanking might clip exposure time to the fps limits. */ - double exposure = agcStatus->shutter_time; - int32_t vblanking = helper_->GetVBlanking(exposure, minFrameDuration_, - maxFrameDuration_); + Duration exposure = agcStatus->shutter_time * 1.0us; + int32_t vblanking = helper_->GetVBlanking(exposure, minFrameDuration_, maxFrameDuration_); int32_t exposureLines = helper_->ExposureLines(exposure); LOG(IPARPI, Debug) << "Applying AGC Exposure: " << exposure From patchwork Tue May 18 10:07:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12311 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 40B7BC31FF for ; Tue, 18 May 2021 10:09:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D024168926; Tue, 18 May 2021 12:09:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="KSV/obGj"; dkim-atps=neutral Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1852C602B2 for ; Tue, 18 May 2021 12:09:13 +0200 (CEST) Received: by mail-wr1-x42c.google.com with SMTP id r12so9567043wrp.1 for ; Tue, 18 May 2021 03:09:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Y5RXuJHyUTxuAceYnXQsVzi1M30dRbpIawpuv7wWDUM=; b=KSV/obGjDaLaXguvASYAOLMdDgKpU2zk5jyjgY8XAuCFeSOoOtGUyjXJpkh317HgpW Hzs4Xd6qmGTlHitURIP2Lg5s2OAT6mgrzGJhGK+pRtVjralfdv9CoSycjggT1QypOgT4 rFva6Sop9InHbbOeDwuVcumDDaL9ifUoCImkCdfhcxtssqa6qNSf3L+RX9dVI4nFOHKx H18XXhm1o1Hev/4z+rxtKgs3H3u/9KMyYJkPGL61/EYLc6bVvIfOT0+SYNaQlK9sl/Ha YkcUw/7sOvz14qktpmxyR7JeYSq42de1Y6YoOiMpIvEvzE4UOTf2BXEcYVvbl00YoCHM rpQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Y5RXuJHyUTxuAceYnXQsVzi1M30dRbpIawpuv7wWDUM=; b=E3sIzc5aN+XYVEoDZKlVTc+NsMXXsnZum8DWkgq6gm15qloNrMZ5MwNdeTghutNLrE jxM6iDQeTVfYNkHTuFKe9a+NAnibIAAAtTkIo0Vq41EFkbYg6MoVWSpnvvvoBWHl6ia+ Cd0MeX2Nw4BC47hRgv723xVaxQgw9KNBZGDwDFA0auSygr9P+mg1Y1y5qRRr8KWHaF3Y +2QD/Bw23pvqL36jvspg4tyrLP8QrGDsAXsXX66r9igUD0CE0A96RClZsRxKVFD3SCtD zRlCNSMgBH5FOf07g9lPszhpterRJKA/IiudvqoP2WgY66Yk01TVl2ovz/uNDBzJY24u X+ww== X-Gm-Message-State: AOAM533VSmDJNW9Xn9tieeMueAvP7Kjt5Itc2lOfYQufE0RcszHFgpp7 3q5kbGE5O89mcN2CEHuUMnASy2i6K7IeHA== X-Google-Smtp-Source: ABdhPJzKq8ySsdtJbDD+kxUIQEhc5+DMKSwyqpkInYPQA06iv6vCP1wCR94EMea9oUXqR8KjDwYr+Q== X-Received: by 2002:adf:fa45:: with SMTP id y5mr5935819wrr.311.1621332552540; Tue, 18 May 2021 03:09:12 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:34e4:187b:f2:ed28]) by smtp.gmail.com with ESMTPSA id o11sm6566682wrq.93.2021.05.18.03.09.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 May 2021 03:09:12 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 18 May 2021 11:07:05 +0100 Message-Id: <20210518100706.578526-4-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210518100706.578526-1-naush@raspberrypi.com> References: <20210518100706.578526-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 3/4] ipa: raspberrypi: Switch AgcAlgorithm API to use RPiController::Duration 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" Switch the AgcAlgorithm API functions to use RPiController::Duration for all time based variables. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman --- src/ipa/raspberrypi/controller/agc_algorithm.hpp | 7 ++++--- src/ipa/raspberrypi/controller/rpi/agc.cpp | 12 ++++++------ src/ipa/raspberrypi/controller/rpi/agc.hpp | 6 +++--- src/ipa/raspberrypi/raspberrypi.cpp | 6 +++--- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/ipa/raspberrypi/controller/agc_algorithm.hpp b/src/ipa/raspberrypi/controller/agc_algorithm.hpp index f97deb0fca59..668912c47388 100644 --- a/src/ipa/raspberrypi/controller/agc_algorithm.hpp +++ b/src/ipa/raspberrypi/controller/agc_algorithm.hpp @@ -7,6 +7,7 @@ #pragma once #include "algorithm.hpp" +#include "duration.hpp" namespace RPiController { @@ -17,9 +18,9 @@ public: // An AGC algorithm must provide the following: virtual unsigned int GetConvergenceFrames() const = 0; virtual void SetEv(double ev) = 0; - virtual void SetFlickerPeriod(double flicker_period) = 0; - virtual void SetFixedShutter(double fixed_shutter) = 0; // microseconds - virtual void SetMaxShutter(double max_shutter) = 0; // microseconds + virtual void SetFlickerPeriod(Duration flicker_period) = 0; + virtual void SetFixedShutter(Duration fixed_shutter) = 0; + virtual void SetMaxShutter(Duration max_shutter) = 0; virtual void SetFixedAnalogueGain(double fixed_analogue_gain) = 0; virtual void SetMeteringMode(std::string const &metering_mode_name) = 0; virtual void SetExposureMode(std::string const &exposure_mode_name) = 0; diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp index f4cd5d26fb4e..1cb4472b2691 100644 --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp @@ -222,19 +222,19 @@ void Agc::SetEv(double ev) ev_ = ev; } -void Agc::SetFlickerPeriod(double flicker_period) +void Agc::SetFlickerPeriod(Duration flicker_period) { - flicker_period_ = flicker_period; + flicker_period_ = DurationValue(flicker_period); } -void Agc::SetMaxShutter(double max_shutter) +void Agc::SetMaxShutter(Duration max_shutter) { - max_shutter_ = max_shutter; + max_shutter_ = DurationValue(max_shutter); } -void Agc::SetFixedShutter(double fixed_shutter) +void Agc::SetFixedShutter(Duration fixed_shutter) { - fixed_shutter_ = fixed_shutter; + fixed_shutter_ = DurationValue(fixed_shutter); // Set this in case someone calls Pause() straight after. status_.shutter_time = clipShutter(fixed_shutter_); } diff --git a/src/ipa/raspberrypi/controller/rpi/agc.hpp b/src/ipa/raspberrypi/controller/rpi/agc.hpp index 0427fb59ec1b..cb79bf61ba42 100644 --- a/src/ipa/raspberrypi/controller/rpi/agc.hpp +++ b/src/ipa/raspberrypi/controller/rpi/agc.hpp @@ -77,9 +77,9 @@ public: void Resume() override; unsigned int GetConvergenceFrames() const override; void SetEv(double ev) override; - void SetFlickerPeriod(double flicker_period) override; - void SetMaxShutter(double max_shutter) override; // microseconds - void SetFixedShutter(double fixed_shutter) override; // microseconds + void SetFlickerPeriod(Duration flicker_period) override; + void SetMaxShutter(Duration max_shutter) override; + void SetFixedShutter(Duration fixed_shutter) override; void SetFixedAnalogueGain(double fixed_analogue_gain) override; void SetMeteringMode(std::string const &metering_mode_name) override; void SetExposureMode(std::string const &exposure_mode_name) override; diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp index 994fb7e057a9..f080f2e53bac 100644 --- a/src/ipa/raspberrypi/raspberrypi.cpp +++ b/src/ipa/raspberrypi/raspberrypi.cpp @@ -643,8 +643,8 @@ void IPARPi::queueRequest(const ControlList &controls) break; } - /* This expects units of micro-seconds. */ - agc->SetFixedShutter(ctrl.second.get()); + /* The control provides units of microseconds. */ + agc->SetFixedShutter(ctrl.second.get() * 1.0us); libcameraMetadata_.set(controls::ExposureTime, ctrl.second.get()); break; @@ -1097,7 +1097,7 @@ void IPARPi::applyFrameDurations(const Duration &minFrameDuration, RPiController::AgcAlgorithm *agc = dynamic_cast( controller_.GetAlgorithm("agc")); - agc->SetMaxShutter(DurationValue(maxShutter)); + agc->SetMaxShutter(maxShutter); } void IPARPi::applyAGC(const struct AgcStatus *agcStatus, ControlList &ctrls) From patchwork Tue May 18 10:07:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12312 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 ED2DEC31FF for ; Tue, 18 May 2021 10:09:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5C51A68927; Tue, 18 May 2021 12:09:18 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="SQBMqvwR"; dkim-atps=neutral Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 516936891C for ; Tue, 18 May 2021 12:09:14 +0200 (CEST) Received: by mail-wr1-x42d.google.com with SMTP id h4so9516598wrt.12 for ; Tue, 18 May 2021 03:09:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rzqUqXRYodsYfDJ7fMhpwHw1+0YrwlbKRdZNyEcHcLc=; b=SQBMqvwRii7trYwarsZxcMZyNKvYCF0mH675a/jq0FnNfgxDaUHzDYKNMPs7lOjfkY 1sJapq8qD9o5/+LYXwP5BB6miV1wbo/2dxD8F1RGK2C/dQQvtuqYMdDBySQfRITmaHzv KbdsFhoOdtT6UYIHwCce9rx7jeBzG0RYIivJSk1WDH/gCoA3ul5LGKODHN25T7T9CRcL rEDEL7J5JHYy3mczt3wa1cM442y8aKrb6fk5aW9V68OTxEupRKQFqkwxl4W6P01NuXwa u5N1ZN1uJaD6PVSuemjXbumh7aoR19YSmZxwwkUfPWRl8FHt8gHimLjCh/U1ZfXN0kFD 9HOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rzqUqXRYodsYfDJ7fMhpwHw1+0YrwlbKRdZNyEcHcLc=; b=ABV3GJjeVwLaDWiaqysz5q8uzLF4lZcxA7rjCK/lIChQydeSIB2Dzg/0Tpw8PHBvXl EUylHu/G4nwGNo9IMKFFn5m1Ep2AdiqZ/HJmoLlWeN/e/rxKNjssc3zHhdqcJesTUj27 e7vzxDjEl6M0T7jruaoPso2VdhISuYWPR2uAdeTT9A1Fm8zwi9ttvGdobF9HqXv0tRuq tENdasYymChMD3ZMA2IoLBCrsyVPwywlAkza3+pluIKMmMifjybZPjTCtreEd5RZnDz2 TYU2TNmtnkRNkCO6M9ztt9FYEqPsq9G0ppXsF2ZIwpNBS8BZdse4RHV7qz/DnuhIzxSk V3Vw== X-Gm-Message-State: AOAM532Ck/qqxxqfRNKpJqr+cUtZclrMY42n4PR+S3zvMgz5eE6qdAAr 2G8ZVGyrfNziYOs3D6IgcFbvpL7URLJz+A== X-Google-Smtp-Source: ABdhPJwG73kgoEbGUOE543HOHhdiqrek1eUZR/qrGnRTMpafueq+S6SP4//WKa2oxodiCIshLmDz/g== X-Received: by 2002:adf:c002:: with SMTP id z2mr5900906wre.100.1621332553210; Tue, 18 May 2021 03:09:13 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:34e4:187b:f2:ed28]) by smtp.gmail.com with ESMTPSA id o11sm6566682wrq.93.2021.05.18.03.09.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 May 2021 03:09:12 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 18 May 2021 11:07:06 +0100 Message-Id: <20210518100706.578526-5-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210518100706.578526-1-naush@raspberrypi.com> References: <20210518100706.578526-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 4/4] ipa: raspberrypi: Switch the AGC/Lux code to use RPiController::Duration 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" Convert the core AGC and Lux controller code to use RPiController::Duration for all exposure time related variables and calculations. Convert the exposure/shutter time fields in AgcStatus and DeviceStatus to use RPiController::Duration. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman --- src/ipa/raspberrypi/cam_helper.cpp | 2 +- src/ipa/raspberrypi/controller/agc_status.h | 12 +-- .../raspberrypi/controller/device_status.h | 6 +- src/ipa/raspberrypi/controller/rpi/agc.cpp | 89 ++++++++++--------- src/ipa/raspberrypi/controller/rpi/agc.hpp | 26 +++--- src/ipa/raspberrypi/controller/rpi/lux.cpp | 17 ++-- src/ipa/raspberrypi/controller/rpi/lux.hpp | 2 +- src/ipa/raspberrypi/raspberrypi.cpp | 13 +-- 8 files changed, 91 insertions(+), 76 deletions(-) diff --git a/src/ipa/raspberrypi/cam_helper.cpp b/src/ipa/raspberrypi/cam_helper.cpp index e2b6c8eb8e03..c399987e47bf 100644 --- a/src/ipa/raspberrypi/cam_helper.cpp +++ b/src/ipa/raspberrypi/cam_helper.cpp @@ -183,7 +183,7 @@ void CamHelper::parseEmbeddedData(Span buffer, return; } - deviceStatus.shutter_speed = DurationValue(Exposure(exposureLines)); + deviceStatus.shutter_speed = Exposure(exposureLines); deviceStatus.analogue_gain = Gain(gainCode); LOG(IPARPI, Debug) << "Metadata updated - Exposure : " diff --git a/src/ipa/raspberrypi/controller/agc_status.h b/src/ipa/raspberrypi/controller/agc_status.h index 10381c90a313..b2a64ce562fa 100644 --- a/src/ipa/raspberrypi/controller/agc_status.h +++ b/src/ipa/raspberrypi/controller/agc_status.h @@ -6,6 +6,8 @@ */ #pragma once +#include "duration.hpp" + // The AGC algorithm should post the following structure into the image's // "agc.status" metadata. @@ -18,17 +20,17 @@ extern "C" { // ignored until then. struct AgcStatus { - double total_exposure_value; // value for all exposure and gain for this image - double target_exposure_value; // (unfiltered) target total exposure AGC is aiming for - double shutter_time; + RPiController::Duration total_exposure_value; // value for all exposure and gain for this image + RPiController::Duration target_exposure_value; // (unfiltered) target total exposure AGC is aiming for + RPiController::Duration shutter_time; double analogue_gain; char exposure_mode[32]; char constraint_mode[32]; char metering_mode[32]; double ev; - double flicker_period; + RPiController::Duration flicker_period; int floating_region_enable; - double fixed_shutter; + RPiController::Duration fixed_shutter; double fixed_analogue_gain; double digital_gain; int locked; diff --git a/src/ipa/raspberrypi/controller/device_status.h b/src/ipa/raspberrypi/controller/device_status.h index aa08608b5d40..a8496176eb92 100644 --- a/src/ipa/raspberrypi/controller/device_status.h +++ b/src/ipa/raspberrypi/controller/device_status.h @@ -6,6 +6,8 @@ */ #pragma once +#include "duration.hpp" + // Definition of "device metadata" which stores things like shutter time and // analogue gain that downstream control algorithms will want to know. @@ -14,8 +16,8 @@ extern "C" { #endif struct DeviceStatus { - // time shutter is open, in microseconds - double shutter_speed; + // time shutter is open + RPiController::Duration shutter_speed; double analogue_gain; // 1.0/distance-in-metres, or 0 if unknown double lens_position; diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp index 1cb4472b2691..3af2ef3cf6ed 100644 --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp @@ -21,6 +21,7 @@ using namespace RPiController; using namespace libcamera; +using namespace std::literals::chrono_literals; LOG_DEFINE_CATEGORY(RPiAgc) @@ -55,19 +56,27 @@ read_metering_modes(std::map &metering_modes, return first; } -static int read_double_list(std::vector &list, - boost::property_tree::ptree const ¶ms) +static int read_list(std::vector &list, + boost::property_tree::ptree const ¶ms) { for (auto &p : params) list.push_back(p.second.get_value()); return list.size(); } +static int read_list(std::vector &list, + boost::property_tree::ptree const ¶ms) +{ + for (auto &p : params) + list.push_back(p.second.get_value() * 1us); + return list.size(); +} + void AgcExposureMode::Read(boost::property_tree::ptree const ¶ms) { int num_shutters = - read_double_list(shutter, params.get_child("shutter")); - int num_ags = read_double_list(gain, params.get_child("gain")); + read_list(shutter, params.get_child("shutter")); + int num_ags = read_list(gain, params.get_child("gain")); if (num_shutters < 2 || num_ags < 2) throw std::runtime_error( "AgcConfig: must have at least two entries in exposure profile"); @@ -147,7 +156,7 @@ void AgcConfig::Read(boost::property_tree::ptree const ¶ms) params.get("fast_reduce_threshold", 0.4); base_ev = params.get("base_ev", 1.0); // Start with quite a low value as ramping up is easier than ramping down. - default_exposure_time = params.get("default_exposure_time", 1000); + default_exposure_time = params.get("default_exposure_time", 1000) * 1us; default_analogue_gain = params.get("default_analogue_gain", 1.0); } @@ -157,7 +166,7 @@ Agc::Agc(Controller *controller) frame_count_(0), lock_count_(0), last_target_exposure_(0.0), ev_(1.0), flicker_period_(0.0), - max_shutter_(0), fixed_shutter_(0), fixed_analogue_gain_(0.0) + max_shutter_(0.0s), fixed_shutter_(0.0s), fixed_analogue_gain_(0.0) { memset(&awb_, 0, sizeof(awb_)); // Setting status_.total_exposure_value_ to zero initially tells us @@ -203,7 +212,7 @@ void Agc::Pause() void Agc::Resume() { - fixed_shutter_ = 0; + fixed_shutter_ = 0.0s; fixed_analogue_gain_ = 0; } @@ -211,7 +220,7 @@ unsigned int Agc::GetConvergenceFrames() const { // If shutter and gain have been explicitly set, there is no // convergence to happen, so no need to drop any frames - return zero. - if (fixed_shutter_ && fixed_analogue_gain_) + if (fixed_shutter_ > 0.0s && fixed_analogue_gain_) return 0; else return config_.convergence_frames; @@ -224,17 +233,17 @@ void Agc::SetEv(double ev) void Agc::SetFlickerPeriod(Duration flicker_period) { - flicker_period_ = DurationValue(flicker_period); + flicker_period_ = flicker_period; } void Agc::SetMaxShutter(Duration max_shutter) { - max_shutter_ = DurationValue(max_shutter); + max_shutter_ = max_shutter; } void Agc::SetFixedShutter(Duration fixed_shutter) { - fixed_shutter_ = DurationValue(fixed_shutter); + fixed_shutter_ = fixed_shutter; // Set this in case someone calls Pause() straight after. status_.shutter_time = clipShutter(fixed_shutter_); } @@ -266,8 +275,8 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode, { housekeepConfig(); - double fixed_shutter = clipShutter(fixed_shutter_); - if (fixed_shutter != 0.0 && fixed_analogue_gain_ != 0.0) { + Duration fixed_shutter = clipShutter(fixed_shutter_); + if (fixed_shutter != 0.0s && fixed_analogue_gain_ != 0.0) { // We're going to reset the algorithm here with these fixed values. fetchAwbStatus(metadata); @@ -284,7 +293,7 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode, // Equivalent of divideUpExposure. filtered_.shutter = fixed_shutter; filtered_.analogue_gain = fixed_analogue_gain_; - } else if (status_.total_exposure_value) { + } else if (status_.total_exposure_value > 0.0s) { // On a mode switch, it's possible the exposure profile could change, // or a fixed exposure/gain might be set so we divide up the exposure/ // gain again, but we don't change any target values. @@ -296,7 +305,7 @@ void Agc::SwitchMode([[maybe_unused]] CameraMode const &camera_mode, // for any that weren't set. // Equivalent of divideUpExposure. - filtered_.shutter = fixed_shutter ? fixed_shutter : config_.default_exposure_time; + filtered_.shutter = fixed_shutter > 0.0s ? fixed_shutter : config_.default_exposure_time; filtered_.analogue_gain = fixed_analogue_gain_ ? fixed_analogue_gain_ : config_.default_analogue_gain; } @@ -308,13 +317,12 @@ void Agc::Prepare(Metadata *image_metadata) status_.digital_gain = 1.0; fetchAwbStatus(image_metadata); // always fetch it so that Process knows it's been done - if (status_.total_exposure_value) { + if (status_.total_exposure_value > 0.0s) { // Process has run, so we have meaningful values. DeviceStatus device_status; if (image_metadata->Get("device.status", device_status) == 0) { - double actual_exposure = device_status.shutter_speed * - device_status.analogue_gain; - if (actual_exposure) { + Duration actual_exposure = device_status.shutter_speed * device_status.analogue_gain; + if (actual_exposure > 0.0s) { status_.digital_gain = status_.total_exposure_value / actual_exposure; @@ -370,9 +378,9 @@ void Agc::updateLockStatus(DeviceStatus const &device_status) const double RESET_MARGIN = 1.5; // Add 200us to the exposure time error to allow for line quantisation. - double exposure_error = last_device_status_.shutter_speed * ERROR_FACTOR + 200; + Duration exposure_error = last_device_status_.shutter_speed * ERROR_FACTOR + 200us; double gain_error = last_device_status_.analogue_gain * ERROR_FACTOR; - double target_error = last_target_exposure_ * ERROR_FACTOR; + Duration target_error = last_target_exposure_ * ERROR_FACTOR; // Note that we don't know the exposure/gain limits of the sensor, so // the values we keep requesting may be unachievable. For this reason @@ -462,7 +470,7 @@ void Agc::fetchCurrentExposure(Metadata *image_metadata) current_.analogue_gain = device_status->analogue_gain; AgcStatus *agc_status = image_metadata->GetLocked("agc.status"); - current_.total_exposure = agc_status ? agc_status->total_exposure_value : 0; + current_.total_exposure = agc_status ? agc_status->total_exposure_value : 0s; current_.total_exposure_no_dg = current_.shutter * current_.analogue_gain; } @@ -573,7 +581,7 @@ void Agc::computeGain(bcm2835_isp_stats *statistics, Metadata *image_metadata, void Agc::computeTargetExposure(double gain) { - if (status_.fixed_shutter != 0.0 && status_.fixed_analogue_gain != 0.0) { + if (status_.fixed_shutter != 0.0s && status_.fixed_analogue_gain != 0.0) { // When ag and shutter are both fixed, we need to drive the // total exposure so that we end up with a digital gain of at least // 1/min_colour_gain. Otherwise we'd desaturate channels causing @@ -588,11 +596,11 @@ void Agc::computeTargetExposure(double gain) target_.total_exposure = current_.total_exposure_no_dg * gain; // The final target exposure is also limited to what the exposure // mode allows. - double max_shutter = status_.fixed_shutter != 0.0 + Duration max_shutter = status_.fixed_shutter != 0.0s ? status_.fixed_shutter : exposure_mode_->shutter.back(); max_shutter = clipShutter(max_shutter); - double max_total_exposure = + Duration max_total_exposure = max_shutter * (status_.fixed_analogue_gain != 0.0 ? status_.fixed_analogue_gain @@ -634,10 +642,10 @@ void Agc::filterExposure(bool desaturate) double speed = config_.speed; // AGC adapts instantly if both shutter and gain are directly specified // or we're in the startup phase. - if ((status_.fixed_shutter && status_.fixed_analogue_gain) || + if ((status_.fixed_shutter > 0.0s && status_.fixed_analogue_gain) || frame_count_ <= config_.startup_frames) speed = 1.0; - if (filtered_.total_exposure == 0.0) { + if (filtered_.total_exposure == 0.0s) { filtered_.total_exposure = target_.total_exposure; filtered_.total_exposure_no_dg = target_.total_exposure_no_dg; } else { @@ -674,9 +682,10 @@ void Agc::divideUpExposure() // Sending the fixed shutter/gain cases through the same code may seem // unnecessary, but it will make more sense when extend this to cover // variable aperture. - double exposure_value = filtered_.total_exposure_no_dg; - double shutter_time, analogue_gain; - shutter_time = status_.fixed_shutter != 0.0 + Duration exposure_value = filtered_.total_exposure_no_dg; + Duration shutter_time; + double analogue_gain; + shutter_time = status_.fixed_shutter != 0.0s ? status_.fixed_shutter : exposure_mode_->shutter[0]; shutter_time = clipShutter(shutter_time); @@ -686,8 +695,8 @@ void Agc::divideUpExposure() if (shutter_time * analogue_gain < exposure_value) { for (unsigned int stage = 1; stage < exposure_mode_->gain.size(); stage++) { - if (status_.fixed_shutter == 0.0) { - double stage_shutter = + if (status_.fixed_shutter == 0.0s) { + Duration stage_shutter = clipShutter(exposure_mode_->shutter[stage]); if (stage_shutter * analogue_gain >= exposure_value) { @@ -713,12 +722,12 @@ void Agc::divideUpExposure() << analogue_gain; // Finally adjust shutter time for flicker avoidance (require both // shutter and gain not to be fixed). - if (status_.fixed_shutter == 0.0 && + if (status_.fixed_shutter == 0.0s && status_.fixed_analogue_gain == 0.0 && - status_.flicker_period != 0.0) { - int flicker_periods = shutter_time / status_.flicker_period; - if (flicker_periods > 0) { - double new_shutter_time = flicker_periods * status_.flicker_period; + status_.flicker_period != 0.0s) { + double flicker_periods = shutter_time / status_.flicker_period; + if (flicker_periods > 0.0) { + Duration new_shutter_time = flicker_periods * status_.flicker_period; analogue_gain *= shutter_time / new_shutter_time; // We should still not allow the ag to go over the // largest value in the exposure mode. Note that this @@ -738,7 +747,7 @@ void Agc::divideUpExposure() void Agc::writeAndFinish(Metadata *image_metadata, bool desaturate) { status_.total_exposure_value = filtered_.total_exposure; - status_.target_exposure_value = desaturate ? 0 : target_.total_exposure_no_dg; + status_.target_exposure_value = desaturate ? 0s : target_.total_exposure_no_dg; status_.shutter_time = filtered_.shutter; status_.analogue_gain = filtered_.analogue_gain; // Write to metadata as well, in case anyone wants to update the camera @@ -750,9 +759,9 @@ void Agc::writeAndFinish(Metadata *image_metadata, bool desaturate) << " analogue gain " << filtered_.analogue_gain; } -double Agc::clipShutter(double shutter) +Duration Agc::clipShutter(Duration shutter) { - if (max_shutter_) + if (max_shutter_ > 0.0s) shutter = std::min(shutter, max_shutter_); return shutter; } diff --git a/src/ipa/raspberrypi/controller/rpi/agc.hpp b/src/ipa/raspberrypi/controller/rpi/agc.hpp index cb79bf61ba42..68b97ce91c99 100644 --- a/src/ipa/raspberrypi/controller/rpi/agc.hpp +++ b/src/ipa/raspberrypi/controller/rpi/agc.hpp @@ -22,13 +22,15 @@ namespace RPiController { +using namespace std::literals::chrono_literals; + struct AgcMeteringMode { double weights[AGC_STATS_SIZE]; void Read(boost::property_tree::ptree const ¶ms); }; struct AgcExposureMode { - std::vector shutter; + std::vector shutter; std::vector gain; void Read(boost::property_tree::ptree const ¶ms); }; @@ -61,7 +63,7 @@ struct AgcConfig { std::string default_exposure_mode; std::string default_constraint_mode; double base_ev; - double default_exposure_time; + Duration default_exposure_time; double default_analogue_gain; }; @@ -101,19 +103,19 @@ private: void filterExposure(bool desaturate); void divideUpExposure(); void writeAndFinish(Metadata *image_metadata, bool desaturate); - double clipShutter(double shutter); + Duration clipShutter(Duration shutter); AgcMeteringMode *metering_mode_; AgcExposureMode *exposure_mode_; AgcConstraintMode *constraint_mode_; uint64_t frame_count_; AwbStatus awb_; struct ExposureValues { - ExposureValues() : shutter(0), analogue_gain(0), - total_exposure(0), total_exposure_no_dg(0) {} - double shutter; + ExposureValues() : shutter(0.0s), analogue_gain(0), + total_exposure(0.0s), total_exposure_no_dg(0.0s) {} + Duration shutter; double analogue_gain; - double total_exposure; - double total_exposure_no_dg; // without digital gain + Duration total_exposure; + Duration total_exposure_no_dg; // without digital gain }; ExposureValues current_; // values for the current frame ExposureValues target_; // calculate the values we want here @@ -121,15 +123,15 @@ private: AgcStatus status_; int lock_count_; DeviceStatus last_device_status_; - double last_target_exposure_; + Duration last_target_exposure_; // Below here the "settings" that applications can change. std::string metering_mode_name_; std::string exposure_mode_name_; std::string constraint_mode_name_; double ev_; - double flicker_period_; - double max_shutter_; - double fixed_shutter_; + Duration flicker_period_; + Duration max_shutter_; + Duration fixed_shutter_; double fixed_analogue_gain_; }; diff --git a/src/ipa/raspberrypi/controller/rpi/lux.cpp b/src/ipa/raspberrypi/controller/rpi/lux.cpp index f74381cab2b4..46d3f3fab2c6 100644 --- a/src/ipa/raspberrypi/controller/rpi/lux.cpp +++ b/src/ipa/raspberrypi/controller/rpi/lux.cpp @@ -16,6 +16,7 @@ using namespace RPiController; using namespace libcamera; +using namespace std::literals::chrono_literals; LOG_DEFINE_CATEGORY(RPiLux) @@ -38,7 +39,7 @@ char const *Lux::Name() const void Lux::Read(boost::property_tree::ptree const ¶ms) { reference_shutter_speed_ = - params.get("reference_shutter_speed"); + params.get("reference_shutter_speed") * 1us; reference_gain_ = params.get("reference_gain"); reference_aperture_ = params.get("reference_aperture", 1.0); reference_Y_ = params.get("reference_Y"); @@ -60,15 +61,13 @@ void Lux::Prepare(Metadata *image_metadata) void Lux::Process(StatisticsPtr &stats, Metadata *image_metadata) { // set some initial values to shut the compiler up - DeviceStatus device_status = - { .shutter_speed = 1.0, - .analogue_gain = 1.0, - .lens_position = 0.0, - .aperture = 0.0, - .flash_intensity = 0.0 }; + DeviceStatus device_status = { .shutter_speed = 1.0ms, + .analogue_gain = 1.0, + .lens_position = 0.0, + .aperture = 0.0, + .flash_intensity = 0.0 }; if (image_metadata->Get("device.status", device_status) == 0) { double current_gain = device_status.analogue_gain; - double current_shutter_speed = device_status.shutter_speed; double current_aperture = device_status.aperture; if (current_aperture == 0) current_aperture = current_aperture_; @@ -83,7 +82,7 @@ void Lux::Process(StatisticsPtr &stats, Metadata *image_metadata) double current_Y = sum / (double)num + .5; double gain_ratio = reference_gain_ / current_gain; double shutter_speed_ratio = - reference_shutter_speed_ / current_shutter_speed; + reference_shutter_speed_ / device_status.shutter_speed; double aperture_ratio = reference_aperture_ / current_aperture; double Y_ratio = current_Y * (65536 / num_bins) / reference_Y_; double estimated_lux = shutter_speed_ratio * gain_ratio * diff --git a/src/ipa/raspberrypi/controller/rpi/lux.hpp b/src/ipa/raspberrypi/controller/rpi/lux.hpp index f9090484a136..726a7f7ca627 100644 --- a/src/ipa/raspberrypi/controller/rpi/lux.hpp +++ b/src/ipa/raspberrypi/controller/rpi/lux.hpp @@ -28,7 +28,7 @@ public: private: // These values define the conditions of the reference image, against // which we compare the new image. - double reference_shutter_speed_; // in micro-seconds + Duration reference_shutter_speed_; double reference_gain_; double reference_aperture_; // units of 1/f double reference_Y_; // out of 65536 diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp index f080f2e53bac..15f51162afec 100644 --- a/src/ipa/raspberrypi/raspberrypi.cpp +++ b/src/ipa/raspberrypi/raspberrypi.cpp @@ -227,11 +227,11 @@ void IPARPi::start(const ControlList &controls, ipa::RPi::StartConfig *startConf /* SwitchMode may supply updated exposure/gain values to use. */ AgcStatus agcStatus; - agcStatus.shutter_time = 0.0; + agcStatus.shutter_time = 0.0s; agcStatus.analogue_gain = 0.0; metadata.Get("agc.status", agcStatus); - if (agcStatus.shutter_time != 0.0 && agcStatus.analogue_gain != 0.0) { + if (agcStatus.shutter_time != 0.0s && agcStatus.analogue_gain != 0.0) { ControlList ctrls(sensorCtrls_); applyAGC(&agcStatus, ctrls); startConfig->controls = std::move(ctrls); @@ -394,7 +394,7 @@ int IPARPi::configure(const CameraSensorInfo &sensorInfo, /* Supply initial values for gain and exposure. */ ControlList ctrls(sensorCtrls_); AgcStatus agcStatus; - agcStatus.shutter_time = DurationValue(DefaultExposureTime); + agcStatus.shutter_time = DefaultExposureTime; agcStatus.analogue_gain = DefaultAnalogueGain; applyAGC(&agcStatus, ctrls); @@ -466,7 +466,8 @@ void IPARPi::reportMetadata() */ DeviceStatus *deviceStatus = rpiMetadata_.GetLocked("device.status"); if (deviceStatus) { - libcameraMetadata_.set(controls::ExposureTime, deviceStatus->shutter_speed); + libcameraMetadata_.set(controls::ExposureTime, + DurationValue(deviceStatus->shutter_speed)); libcameraMetadata_.set(controls::AnalogueGain, deviceStatus->analogue_gain); } @@ -1019,7 +1020,7 @@ void IPARPi::fillDeviceStatus(const ControlList &sensorControls) int32_t exposureLines = sensorControls.get(V4L2_CID_EXPOSURE).get(); int32_t gainCode = sensorControls.get(V4L2_CID_ANALOGUE_GAIN).get(); - deviceStatus.shutter_speed = DurationValue(helper_->Exposure(exposureLines)); + deviceStatus.shutter_speed = helper_->Exposure(exposureLines); deviceStatus.analogue_gain = helper_->Gain(gainCode); LOG(IPARPI, Debug) << "Metadata - Exposure : " @@ -1105,7 +1106,7 @@ void IPARPi::applyAGC(const struct AgcStatus *agcStatus, ControlList &ctrls) int32_t gainCode = helper_->GainCode(agcStatus->analogue_gain); /* GetVBlanking might clip exposure time to the fps limits. */ - Duration exposure = agcStatus->shutter_time * 1.0us; + Duration exposure = agcStatus->shutter_time; int32_t vblanking = helper_->GetVBlanking(exposure, minFrameDuration_, maxFrameDuration_); int32_t exposureLines = helper_->ExposureLines(exposure);