From patchwork Thu Oct 27 22:44:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Dufresne via libcamera-devel X-Patchwork-Id: 17712 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 23686BDB16 for ; Thu, 27 Oct 2022 22:44:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C126A62FA9; Fri, 28 Oct 2022 00:44:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1666910688; bh=KAG6VBvMLEaE+uQzo38FS9tf6GUTOandZ8YYv21Fh6o=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=2/cIVbN/NOmKD+g0a4YNOWM3yXlA9dNAw7FDSwP23N/P9yTSkTe++y30mDY6vXIrZ aK1aDCxFNDLBARFzE7yDmf9TOyVWUM7JIh+w+RG1DpKTV/aweXJfC9UusYpDMkEvz+ 9BBZt3+ozFBihqWRsZK2OrdgxSf8YRgJ1YfJHjshylv6yYL6729RsPPy7tMgNgzbno QqlJAGXy3GZPyN6V5o7/6rGo1kkxdO7CpjgBH2p29omcYdxFdnsEML8Mfu7PWJCafU T2akJDjtPOWYdJtOar15pbOYJgmgd9zxASvph/JZCPK5Gq0jouOsEHSG3xnLeBCWCI YdYqIawrC+ZCw== Received: from mail-oa1-x33.google.com (mail-oa1-x33.google.com [IPv6:2001:4860:4864:20::33]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DA24662FA3 for ; Fri, 28 Oct 2022 00:44:46 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=rothemail-net.20210112.gappssmtp.com header.i=@rothemail-net.20210112.gappssmtp.com header.b="mCQlxUcZ"; dkim-atps=neutral Received: by mail-oa1-x33.google.com with SMTP id 586e51a60fabf-13c2cfd1126so4294880fac.10 for ; Thu, 27 Oct 2022 15:44:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rothemail-net.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:reply-to:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=vPPTrdE8P8aEZNt9QylvzE1IoIY92ZIoPAxBlN/CeEU=; b=mCQlxUcZ2Ty+EZSimbwqpiZRe58ySl+QHvwajQk4og/Wk41xAc96Vz/eCIXZJrMv+6 uQaU+ZJkH7vEbxj9T3g3fZiQn4ao0mMjnDrUY4Rri6KsIIVtzISYawaQfyRpdNWQBj22 Mn8o5TFGZTc5f35ieuWO3AJdqQCwFDHQwpXcvBDOqAyENBtvMs4vlE47JnxUbsPv0Cfn Qv8/iW8TvgjHvy+UfXH9ZV9K8woTI0Ki2f9zoBFPauo8Hp/kMrL+cRVT0mqjqXnXGtlW MOg/5GASShhRWKgHOotqe/cuvLnQdvWClcPexDvQy7sjPxnKXP3kt5XD5Ps5n0fNLc4h H5sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to:message-id:date :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=vPPTrdE8P8aEZNt9QylvzE1IoIY92ZIoPAxBlN/CeEU=; b=HzLlDdt5xcMm0u3R7WFOHSgCGs1OsQOSqjlBzUIBvHytfgwxZeMBOgc5DwDgQFRpPK Blo4HzbQ8H+tmn3LLajIXZqtsgZFAHbheANOQ4Wa3lO1JS5/4ty7HVNuSv4hEx/ZiRl8 JBKk5BPd48KIXHk4VYEHFKweB5GasT7BfYnuO+jHFAtVmDF9uW4002vLe3tMtT/HKs55 aahsuowUNFmPEBZDuJnGs9hA5gYIvpyOCl+qv37YcAG2ckfF9RTmUyKV8aBrdYcNm6zV ct3m0zkHtnAWOVgtz3m8mAzyeHkThZxMnZhhCGtXS7UEQIXJjnhhsZX/l3DhWTTCsOht xfnw== X-Gm-Message-State: ACrzQf369b9eMJE5+wPqdFvbdFmNyRbZrVV+6qUQ6VjIA0bpUPl5QQ0i 60doUIn9YTMsR54RXZ9rDLjLsOME8I/7l29q X-Google-Smtp-Source: AMsMyM6Boi9zeCLzIyDT22CBIg2tfiu/QcYJL4ge3+dQyJAi5J1bnXCSoL51qt60+AktuIkAXaKcvw== X-Received: by 2002:a05:6871:1cf:b0:13c:12cf:2135 with SMTP id q15-20020a05687101cf00b0013c12cf2135mr7064627oad.121.1666910685223; Thu, 27 Oct 2022 15:44:45 -0700 (PDT) Received: from nroth-pc.attlocal.net ([2600:1700:20:20c0:6a4b:7aa7:dda2:75cb]) by smtp.gmail.com with ESMTPSA id a16-20020a05687073d000b00131c3d4d38fsm1246674oan.39.2022.10.27.15.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Oct 2022 15:44:44 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 27 Oct 2022 17:44:25 -0500 Message-Id: <20221027224434.348469-1-nicholas@rothemail.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 01/10] ipa: workaround libcxx duration limitation 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: Nicholas Roth via libcamera-devel From: Nicolas Dufresne via libcamera-devel Reply-To: libcamera-devel@lists.libcamera.org Cc: nicholas@rothemail.net Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Nicholas Roth A bug in libcxx [0] used by clang version 11.0.2 is prevalent when building libcamera for Android SDK30. This has been fixed and integrated upstream with [1]. As a workaround, directly cast libcamera::utils::Duration objects to std::chrono::duration when dividing. Alternatives evaluated: Considered: Enable public inheritance of std::chrono::duration and override operator/ in the class. Outcome: Does not fix the original compiler error. Considered: Enable public inheritance of std::chrono::duration and override operator/ in the libcamera namespace. Outcome: new compiler error: ld.lld: error: duplicate symbol: libcamera::operator/ (libcamera::utils::Duration const&, libcamera::utils::Duration const&) Considered: Use private inheritance of std::chrono::duration and re-implement a pass-through version of each std::chrono::duration operator within libcamera::utils::Duration and use template metaprogramming to fix the division operator. Outcome: Testing shows that this would introduce substantial limitations, i.e. requring the Duration object to be on the LHS of any arithmetic operation with other numeric types. This also substantially increases implementation complexity. Considered: Extract double values from libcamera::utils::Duration objects and use those to divide. Outcome: This creates substantial readability and unit-safety issues. [0] https://github.com/llvm/llvm-project/issues/40475 [1] https://github.com/llvm/llvm-project/commit/efa6d803c624f9251d0ab7881122501bb9d27368 Bug: https://bugs.libcamera.org/show_bug.cgi?id=156 Signed-off-by: Nicholas Roth --- src/ipa/ipu3/algorithms/agc.cpp | 16 ++++++++++------ src/ipa/raspberrypi/cam_helper.cpp | 6 +++--- src/ipa/raspberrypi/cam_helper_imx296.cpp | 3 ++- src/ipa/raspberrypi/controller/rpi/agc.cpp | 12 ++++++++---- src/ipa/raspberrypi/controller/rpi/lux.cpp | 3 ++- src/ipa/rkisp1/algorithms/agc.cpp | 12 ++++++++---- 6 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp index a1a3c38f..f1650468 100644 --- a/src/ipa/ipu3/algorithms/agc.cpp +++ b/src/ipa/ipu3/algorithms/agc.cpp @@ -100,7 +100,8 @@ int Agc::configure(IPAContext &context, /* Configure the default exposure and gain. */ activeState.agc.gain = std::max(minAnalogueGain_, kMinAnalogueGain); - activeState.agc.exposure = 10ms / configuration.sensor.lineDuration; + activeState.agc.exposure = 10ms / + std::chrono::duration(configuration.sensor.lineDuration); frameCount_ = 0; return 0; @@ -240,17 +241,20 @@ void Agc::computeExposure(IPAContext &context, IPAFrameContext &frameContext, * increase the gain. */ utils::Duration shutterTime = - std::clamp(exposureValue / minAnalogueGain_, - minShutterSpeed_, maxShutterSpeed_); - double stepGain = std::clamp(exposureValue / shutterTime, - minAnalogueGain_, maxAnalogueGain_); + std::clamp(std::chrono::duration(exposureValue) / + minAnalogueGain_, + minShutterSpeed_, maxShutterSpeed_); + double stepGain = std::clamp(std::chrono::duration(exposureValue) / + std::chrono::duration(shutterTime), + minAnalogueGain_, maxAnalogueGain_); LOG(IPU3Agc, Debug) << "Divided up shutter and gain are " << shutterTime << " and " << stepGain; IPAActiveState &activeState = context.activeState; /* Update the estimated exposure and gain. */ - activeState.agc.exposure = shutterTime / configuration.sensor.lineDuration; + activeState.agc.exposure = std::chrono::duration(shutterTime) / + std::chrono::duration(configuration.sensor.lineDuration); activeState.agc.gain = stepGain; } diff --git a/src/ipa/raspberrypi/cam_helper.cpp b/src/ipa/raspberrypi/cam_helper.cpp index d90ac1de..48a8a068 100644 --- a/src/ipa/raspberrypi/cam_helper.cpp +++ b/src/ipa/raspberrypi/cam_helper.cpp @@ -63,7 +63,7 @@ void CamHelper::process([[maybe_unused]] StatisticsPtr &stats, uint32_t CamHelper::exposureLines(const Duration exposure, const Duration lineLength) const { - return exposure / lineLength; + return std::chrono::duration(exposure) / std::chrono::duration(lineLength); } Duration CamHelper::exposure(uint32_t exposureLines, const Duration lineLength) const @@ -85,8 +85,8 @@ std::pair CamHelper::getBlanking(Duration &exposure, * frameLengthMax gets calculated on the smallest line length as we do * not want to extend that unless absolutely necessary. */ - frameLengthMin = minFrameDuration / mode_.minLineLength; - frameLengthMax = maxFrameDuration / mode_.minLineLength; + frameLengthMin = std::chrono::duration(minFrameDuration) / std::chrono::duration(mode_.minLineLength); + frameLengthMax = std::chrono::duration(maxFrameDuration) / std::chrono::duration(mode_.minLineLength); /* * Watch out for (exposureLines + frameIntegrationDiff_) overflowing a diff --git a/src/ipa/raspberrypi/cam_helper_imx296.cpp b/src/ipa/raspberrypi/cam_helper_imx296.cpp index ecb845e7..c5180de5 100644 --- a/src/ipa/raspberrypi/cam_helper_imx296.cpp +++ b/src/ipa/raspberrypi/cam_helper_imx296.cpp @@ -57,7 +57,8 @@ double CamHelperImx296::gain(uint32_t gainCode) const uint32_t CamHelperImx296::exposureLines(const Duration exposure, [[maybe_unused]] const Duration lineLength) const { - return std::max(minExposureLines, (exposure - 14.26us) / timePerLine); + return std::max(minExposureLines, std::chrono::duration(exposure - 14.26us) / + std::chrono::duration(timePerLine)); } Duration CamHelperImx296::exposure(uint32_t exposureLines, diff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp index bd54a639..65baab99 100644 --- a/src/ipa/raspberrypi/controller/rpi/agc.cpp +++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp @@ -418,7 +418,8 @@ void Agc::prepare(Metadata *imageMetadata) Duration actualExposure = deviceStatus.shutterSpeed * deviceStatus.analogueGain; if (actualExposure) { - status_.digitalGain = status_.totalExposureValue / actualExposure; + status_.digitalGain = std::chrono::duration(status_.totalExposureValue) / + std::chrono::duration(actualExposure); LOG(RPiAgc, Debug) << "Want total exposure " << status_.totalExposureValue; /* * Never ask for a gain < 1.0, and also impose @@ -823,7 +824,8 @@ void Agc::divideUpExposure() } if (status_.fixedAnalogueGain == 0.0) { if (exposureMode_->gain[stage] * shutterTime >= exposureValue) { - analogueGain = exposureValue / shutterTime; + analogueGain = std::chrono::duration(exposureValue) / + std::chrono::duration(shutterTime); break; } analogueGain = exposureMode_->gain[stage]; @@ -838,10 +840,12 @@ void Agc::divideUpExposure() */ if (!status_.fixedShutter && !status_.fixedAnalogueGain && status_.flickerPeriod) { - int flickerPeriods = shutterTime / status_.flickerPeriod; + int flickerPeriods = std::chrono::duration(shutterTime) / + std::chrono::duration(status_.flickerPeriod); if (flickerPeriods) { Duration newShutterTime = flickerPeriods * status_.flickerPeriod; - analogueGain *= shutterTime / newShutterTime; + analogueGain *= std::chrono::duration(shutterTime) / + std::chrono::duration(newShutterTime); /* * We should still not allow the ag to go over the * largest value in the exposure mode. Note that this diff --git a/src/ipa/raspberrypi/controller/rpi/lux.cpp b/src/ipa/raspberrypi/controller/rpi/lux.cpp index 9759186a..410f6f44 100644 --- a/src/ipa/raspberrypi/controller/rpi/lux.cpp +++ b/src/ipa/raspberrypi/controller/rpi/lux.cpp @@ -94,7 +94,8 @@ void Lux::process(StatisticsPtr &stats, Metadata *imageMetadata) double currentY = sum / (double)num + .5; double gainRatio = referenceGain_ / currentGain; double shutterSpeedRatio = - referenceShutterSpeed_ / deviceStatus.shutterSpeed; + std::chrono::duration(referenceShutterSpeed_) / + std::chrono::duration(deviceStatus.shutterSpeed); double apertureRatio = referenceAperture_ / currentAperture; double yRatio = currentY * (65536 / numBins) / referenceY_; double estimatedLux = shutterSpeedRatio * gainRatio * diff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp index 04062a36..bb874356 100644 --- a/src/ipa/rkisp1/algorithms/agc.cpp +++ b/src/ipa/rkisp1/algorithms/agc.cpp @@ -74,7 +74,8 @@ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo) { /* Configure the default exposure and gain. */ context.activeState.agc.gain = std::max(context.configuration.agc.minAnalogueGain, kMinAnalogueGain); - context.activeState.agc.exposure = 10ms / context.configuration.sensor.lineDuration; + context.activeState.agc.exposure = 10ms / + std::chrono::duration(context.configuration.sensor.lineDuration); /* * According to the RkISP1 documentation: @@ -212,16 +213,19 @@ void Agc::computeExposure(IPAContext &context, IPAFrameContext &frameContext, * Push the shutter time up to the maximum first, and only then * increase the gain. */ - utils::Duration shutterTime = std::clamp(exposureValue / minAnalogueGain, + utils::Duration shutterTime = std::clamp(std::chrono::duration(exposureValue) / + minAnalogueGain, minShutterSpeed, maxShutterSpeed); - double stepGain = std::clamp(exposureValue / shutterTime, + double stepGain = std::clamp(std::chrono::duration(exposureValue) / + std::chrono::duration(shutterTime), minAnalogueGain, maxAnalogueGain); LOG(RkISP1Agc, Debug) << "Divided up shutter and gain are " << shutterTime << " and " << stepGain; /* Update the estimated exposure and gain. */ - activeState.agc.exposure = shutterTime / configuration.sensor.lineDuration; + activeState.agc.exposure = std::chrono::duration(shutterTime) / + std::chrono::duration(configuration.sensor.lineDuration); activeState.agc.gain = stepGain; }