{"id":17713,"url":"https://patchwork.libcamera.org/api/patches/17713/?format=json","web_url":"https://patchwork.libcamera.org/patch/17713/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20221028031726.4849-2-nicholas@rothemail.net>","date":"2022-10-28T03:17:17","name":"[libcamera-devel,v5,01/10] ipa: workaround libcxx duration limitation","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"ab7e2b59a6d5a2e3fd512d58c53fb228bbb2e4dd","submitter":{"id":97,"url":"https://patchwork.libcamera.org/api/people/97/?format=json","name":"Nicolas Dufresne via libcamera-devel","email":"libcamera-devel@lists.libcamera.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/17713/mbox/","series":[{"id":3583,"url":"https://patchwork.libcamera.org/api/series/3583/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3583","date":"2022-10-28T03:17:17","name":"[libcamera-devel,v5,01/10] ipa: workaround libcxx duration limitation","version":5,"mbox":"https://patchwork.libcamera.org/series/3583/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/17713/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/17713/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 408D4BD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 28 Oct 2022 03:17:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C4C0962FB7;\n\tFri, 28 Oct 2022 05:17:33 +0200 (CEST)","from mail-oa1-x34.google.com (mail-oa1-x34.google.com\n\t[IPv6:2001:4860:4864:20::34])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C0BD562FA5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 28 Oct 2022 05:17:31 +0200 (CEST)","by mail-oa1-x34.google.com with SMTP id\n\t586e51a60fabf-13ba86b5ac0so5026911fac.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 27 Oct 2022 20:17:31 -0700 (PDT)","from nroth-pc.attlocal.net\n\t([2600:1700:20:20c0:293a:90ce:6463:244d])\n\tby smtp.gmail.com with ESMTPSA id\n\tfp19-20020a056870659300b0013626c1a5f6sm1527738oab.10.2022.10.27.20.17.29\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 27 Oct 2022 20:17:29 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1666927053;\n\tbh=KAG6VBvMLEaE+uQzo38FS9tf6GUTOandZ8YYv21Fh6o=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=QijdF/PgpvZA2bMSaFizmim0/tYkQEMm6hYX8aTRsgdqGlHdlvDiRrpvW7JTapCgW\n\thZgtjnh3RDpg8vxe3h9LFs6UT9XN9pOIzqGBkK/Xqilg3JdHIr7yOtpk+Zkf+KP7yU\n\ti81vl4q9ZwdCHxm6sURvWQIuwTVhpLKYgcLYCyxZoBpap+zMlaZYuRzE6vdMrdaLRn\n\tQVGklYyqkMytTBD/YOpuJjfnIoUM+ZT1Xa5kD/AUe3hh73e/Xk0ZAlhdUiZzl44GVz\n\tRSzGz2L20mz/miUXhJGCQ52cRGYoWlhXxQXehpUtFlRv+nrPK2HgVc3U3FguuEB5Sn\n\t1yrrEUyMvluIA==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=rothemail-net.20210112.gappssmtp.com; s=20210112;\n\th=content-transfer-encoding:mime-version:reply-to:references\n\t:in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject\n\t:date:message-id:reply-to;\n\tbh=vPPTrdE8P8aEZNt9QylvzE1IoIY92ZIoPAxBlN/CeEU=;\n\tb=P8A8pisJzUK8Coq8lKcoOQIZkjTF2FfMbt4ZaUcfp8DUbAYzqTdhGQe0Px3RdLxzeL\n\tRNZV2gT1mLfLHD7aZ3zBHHoW3xHFPt+046d9xrk13Ogd8Wb9sQGeoq0yGsT8+kyyAJzz\n\tFZE3gPDWsCpymKxUMdt74WN8w3uTdH6u3wd+DRCOAotWIClLzkkyYFmkyXqmg/UMxB5C\n\t/q52H9WgKz57QIFVADcwnnb+gg58r6TT0oMSObpmr3FDFtFiuKIGt0D1hpKT7QofNdwx\n\tduypEdrU7Y6S51YbPFfE1kkNgZJCNjPW6lCA0LhSubIGPutaPQ816X9B1MrXpZObCQGF\n\tXO+g=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected)\n\theader.d=rothemail-net.20210112.gappssmtp.com\n\theader.i=@rothemail-net.20210112.gappssmtp.com header.b=\"P8A8pisJ\"; \n\tdkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=content-transfer-encoding:mime-version:reply-to:references\n\t:in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state\n\t:from:to:cc:subject:date:message-id:reply-to;\n\tbh=vPPTrdE8P8aEZNt9QylvzE1IoIY92ZIoPAxBlN/CeEU=;\n\tb=y3xWDJTM/VVBXbRNeTC36Z11MAFKpqdVEFg/tvmt4vkUaYdlx9JE75ZWYb/oGk9cDm\n\tqK98R4mYNOc+O2woTDR2ThsKjJ92rWpvXmIKd8lANu3vlcuhgDigvRIF9URdXCl8jIaw\n\tgiTgJIc9ZgfhfyYjVgjFDoz8b8c+KQe9a7c8LoNz5amrWt0L68fo7m2oZzRnXy2LGSod\n\tCR5BZYgXRRT5Z/XMzdCY2UfjWewMowfWkyfOqTa/FF5ZpAClw1ZY02/j2m7XgQAXxJnE\n\tBJd12BsA1Arh3J9vh2YkOTiDgL99UiwOyGNjJSlSw/p3vhY5KJOZMt29PXeRIk0PWU8+\n\tJX9Q==","X-Gm-Message-State":"ACrzQf0jKY2gP0k0pM2LGBDTTyHMB813XH5ywA+P8i+UUUsVfKs9rseL\n\tJNYaZETaXuj8nMA2pDkaSlIXJk7FMb6SeBm1Izk=","X-Google-Smtp-Source":"AMsMyM4xzQkMeXYywpCGa32Q7+21sCJRV1ytfch+9gJ9rZpfRZ/zZm4yF77eFWah2oCmEwQgr6LIGA==","X-Received":"by 2002:a05:6870:58a4:b0:11c:9b6d:f066 with SMTP id\n\tbe36-20020a05687058a400b0011c9b6df066mr7802106oab.155.1666927050086; \n\tThu, 27 Oct 2022 20:17:30 -0700 (PDT)","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 27 Oct 2022 22:17:17 -0500","Message-Id":"<20221028031726.4849-2-nicholas@rothemail.net>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20221028031726.4849-1-nicholas@rothemail.net>","References":"<20221027224135.348115-1-nicholas@rothemail.net>\n\t<20221028031726.4849-1-nicholas@rothemail.net>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v5 01/10] ipa: workaround libcxx duration\n\tlimitation","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Nicholas Roth via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"libcamera-devel@lists.libcamera.org","Cc":"nicholas@rothemail.net","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Nicholas Roth <nicholas@rothemail.net>\n\nA bug in libcxx [0] used by clang version 11.0.2 is prevalent when\nbuilding libcamera for Android SDK30. This has been fixed and\nintegrated upstream with [1].\n\nAs a workaround, directly cast libcamera::utils::Duration objects to\nstd::chrono::duration when dividing.\n\nAlternatives evaluated:\nConsidered: Enable public inheritance of std::chrono::duration and\noverride operator/ in the class.\nOutcome: Does not fix the original compiler error.\n\nConsidered: Enable public inheritance of std::chrono::duration and\noverride operator/ in the libcamera namespace.\nOutcome: new compiler error:\nld.lld: error: duplicate symbol: libcamera::operator/\n(libcamera::utils::Duration const&, libcamera::utils::Duration const&)\n\nConsidered: Use private inheritance of std::chrono::duration and\nre-implement a pass-through version of each std::chrono::duration\noperator within libcamera::utils::Duration and use template\nmetaprogramming to fix the division operator.\nOutcome: Testing shows that this would introduce substantial\nlimitations, i.e. requring the Duration object to be on the LHS of any\narithmetic operation with other numeric types. This also substantially\nincreases implementation complexity.\n\nConsidered: Extract double values from libcamera::utils::Duration\nobjects and use those to divide.\nOutcome: This creates substantial readability and unit-safety issues.\n\n[0] https://github.com/llvm/llvm-project/issues/40475\n[1] https://github.com/llvm/llvm-project/commit/efa6d803c624f9251d0ab7881122501bb9d27368\nBug: https://bugs.libcamera.org/show_bug.cgi?id=156\n\nSigned-off-by: Nicholas Roth <nicholas@rothemail.net>\n---\n src/ipa/ipu3/algorithms/agc.cpp            | 16 ++++++++++------\n src/ipa/raspberrypi/cam_helper.cpp         |  6 +++---\n src/ipa/raspberrypi/cam_helper_imx296.cpp  |  3 ++-\n src/ipa/raspberrypi/controller/rpi/agc.cpp | 12 ++++++++----\n src/ipa/raspberrypi/controller/rpi/lux.cpp |  3 ++-\n src/ipa/rkisp1/algorithms/agc.cpp          | 12 ++++++++----\n 6 files changed, 33 insertions(+), 19 deletions(-)","diff":"diff --git a/src/ipa/ipu3/algorithms/agc.cpp b/src/ipa/ipu3/algorithms/agc.cpp\nindex a1a3c38f..f1650468 100644\n--- a/src/ipa/ipu3/algorithms/agc.cpp\n+++ b/src/ipa/ipu3/algorithms/agc.cpp\n@@ -100,7 +100,8 @@ int Agc::configure(IPAContext &context,\n \n \t/* Configure the default exposure and gain. */\n \tactiveState.agc.gain = std::max(minAnalogueGain_, kMinAnalogueGain);\n-\tactiveState.agc.exposure = 10ms / configuration.sensor.lineDuration;\n+\tactiveState.agc.exposure = 10ms /\n+\t\t\t\tstd::chrono::duration(configuration.sensor.lineDuration);\n \n \tframeCount_ = 0;\n \treturn 0;\n@@ -240,17 +241,20 @@ void Agc::computeExposure(IPAContext &context, IPAFrameContext &frameContext,\n \t * increase the gain.\n \t */\n \tutils::Duration shutterTime =\n-\t\tstd::clamp<utils::Duration>(exposureValue / minAnalogueGain_,\n-\t\t\t\t\t    minShutterSpeed_, maxShutterSpeed_);\n-\tdouble stepGain = std::clamp(exposureValue / shutterTime,\n-\t\t\t\t     minAnalogueGain_, maxAnalogueGain_);\n+\t\tstd::clamp<utils::Duration>(std::chrono::duration(exposureValue) /\n+\t\t\t\t\t\tminAnalogueGain_,\n+\t\t\t\t\tminShutterSpeed_, maxShutterSpeed_);\n+\tdouble stepGain = std::clamp(std::chrono::duration(exposureValue) /\n+\t\t\t\t\tstd::chrono::duration(shutterTime),\n+\t\t\t\tminAnalogueGain_, maxAnalogueGain_);\n \tLOG(IPU3Agc, Debug) << \"Divided up shutter and gain are \"\n \t\t\t    << shutterTime << \" and \"\n \t\t\t    << stepGain;\n \n \tIPAActiveState &activeState = context.activeState;\n \t/* Update the estimated exposure and gain. */\n-\tactiveState.agc.exposure = shutterTime / configuration.sensor.lineDuration;\n+\tactiveState.agc.exposure = std::chrono::duration(shutterTime) /\n+\t\t\t\tstd::chrono::duration(configuration.sensor.lineDuration);\n \tactiveState.agc.gain = stepGain;\n }\n \ndiff --git a/src/ipa/raspberrypi/cam_helper.cpp b/src/ipa/raspberrypi/cam_helper.cpp\nindex d90ac1de..48a8a068 100644\n--- a/src/ipa/raspberrypi/cam_helper.cpp\n+++ b/src/ipa/raspberrypi/cam_helper.cpp\n@@ -63,7 +63,7 @@ void CamHelper::process([[maybe_unused]] StatisticsPtr &stats,\n \n uint32_t CamHelper::exposureLines(const Duration exposure, const Duration lineLength) const\n {\n-\treturn exposure / lineLength;\n+\treturn std::chrono::duration(exposure) / std::chrono::duration(lineLength);\n }\n \n Duration CamHelper::exposure(uint32_t exposureLines, const Duration lineLength) const\n@@ -85,8 +85,8 @@ std::pair<uint32_t, uint32_t> CamHelper::getBlanking(Duration &exposure,\n \t * frameLengthMax gets calculated on the smallest line length as we do\n \t * not want to extend that unless absolutely necessary.\n \t */\n-\tframeLengthMin = minFrameDuration / mode_.minLineLength;\n-\tframeLengthMax = maxFrameDuration / mode_.minLineLength;\n+\tframeLengthMin = std::chrono::duration(minFrameDuration) / std::chrono::duration(mode_.minLineLength);\n+\tframeLengthMax = std::chrono::duration(maxFrameDuration) / std::chrono::duration(mode_.minLineLength);\n \n \t/*\n \t * Watch out for (exposureLines + frameIntegrationDiff_) overflowing a\ndiff --git a/src/ipa/raspberrypi/cam_helper_imx296.cpp b/src/ipa/raspberrypi/cam_helper_imx296.cpp\nindex ecb845e7..c5180de5 100644\n--- a/src/ipa/raspberrypi/cam_helper_imx296.cpp\n+++ b/src/ipa/raspberrypi/cam_helper_imx296.cpp\n@@ -57,7 +57,8 @@ double CamHelperImx296::gain(uint32_t gainCode) const\n uint32_t CamHelperImx296::exposureLines(const Duration exposure,\n \t\t\t\t\t[[maybe_unused]] const Duration lineLength) const\n {\n-\treturn std::max<uint32_t>(minExposureLines, (exposure - 14.26us) / timePerLine);\n+\treturn std::max<uint32_t>(minExposureLines, std::chrono::duration(exposure - 14.26us) /\n+\t\t\t\t\t\t\tstd::chrono::duration(timePerLine));\n }\n \n Duration CamHelperImx296::exposure(uint32_t exposureLines,\ndiff --git a/src/ipa/raspberrypi/controller/rpi/agc.cpp b/src/ipa/raspberrypi/controller/rpi/agc.cpp\nindex bd54a639..65baab99 100644\n--- a/src/ipa/raspberrypi/controller/rpi/agc.cpp\n+++ b/src/ipa/raspberrypi/controller/rpi/agc.cpp\n@@ -418,7 +418,8 @@ void Agc::prepare(Metadata *imageMetadata)\n \t\t\tDuration actualExposure = deviceStatus.shutterSpeed *\n \t\t\t\t\t\t  deviceStatus.analogueGain;\n \t\t\tif (actualExposure) {\n-\t\t\t\tstatus_.digitalGain = status_.totalExposureValue / actualExposure;\n+\t\t\t\tstatus_.digitalGain = std::chrono::duration(status_.totalExposureValue) /\n+\t\t\t\t\t\t\tstd::chrono::duration(actualExposure);\n \t\t\t\tLOG(RPiAgc, Debug) << \"Want total exposure \" << status_.totalExposureValue;\n \t\t\t\t/*\n \t\t\t\t * Never ask for a gain < 1.0, and also impose\n@@ -823,7 +824,8 @@ void Agc::divideUpExposure()\n \t\t\t}\n \t\t\tif (status_.fixedAnalogueGain == 0.0) {\n \t\t\t\tif (exposureMode_->gain[stage] * shutterTime >= exposureValue) {\n-\t\t\t\t\tanalogueGain = exposureValue / shutterTime;\n+\t\t\t\t\tanalogueGain = std::chrono::duration(exposureValue) /\n+\t\t\t\t\t\t\tstd::chrono::duration(shutterTime);\n \t\t\t\t\tbreak;\n \t\t\t\t}\n \t\t\t\tanalogueGain = exposureMode_->gain[stage];\n@@ -838,10 +840,12 @@ void Agc::divideUpExposure()\n \t */\n \tif (!status_.fixedShutter && !status_.fixedAnalogueGain &&\n \t    status_.flickerPeriod) {\n-\t\tint flickerPeriods = shutterTime / status_.flickerPeriod;\n+\t\tint flickerPeriods = std::chrono::duration(shutterTime) /\n+\t\t\t\tstd::chrono::duration(status_.flickerPeriod);\n \t\tif (flickerPeriods) {\n \t\t\tDuration newShutterTime = flickerPeriods * status_.flickerPeriod;\n-\t\t\tanalogueGain *= shutterTime / newShutterTime;\n+\t\t\tanalogueGain *= std::chrono::duration(shutterTime) /\n+\t\t\t\t\tstd::chrono::duration(newShutterTime);\n \t\t\t/*\n \t\t\t * We should still not allow the ag to go over the\n \t\t\t * largest value in the exposure mode. Note that this\ndiff --git a/src/ipa/raspberrypi/controller/rpi/lux.cpp b/src/ipa/raspberrypi/controller/rpi/lux.cpp\nindex 9759186a..410f6f44 100644\n--- a/src/ipa/raspberrypi/controller/rpi/lux.cpp\n+++ b/src/ipa/raspberrypi/controller/rpi/lux.cpp\n@@ -94,7 +94,8 @@ void Lux::process(StatisticsPtr &stats, Metadata *imageMetadata)\n \t\tdouble currentY = sum / (double)num + .5;\n \t\tdouble gainRatio = referenceGain_ / currentGain;\n \t\tdouble shutterSpeedRatio =\n-\t\t\treferenceShutterSpeed_ / deviceStatus.shutterSpeed;\n+\t\t\tstd::chrono::duration(referenceShutterSpeed_) /\n+\t\t\tstd::chrono::duration(deviceStatus.shutterSpeed);\n \t\tdouble apertureRatio = referenceAperture_ / currentAperture;\n \t\tdouble yRatio = currentY * (65536 / numBins) / referenceY_;\n \t\tdouble estimatedLux = shutterSpeedRatio * gainRatio *\ndiff --git a/src/ipa/rkisp1/algorithms/agc.cpp b/src/ipa/rkisp1/algorithms/agc.cpp\nindex 04062a36..bb874356 100644\n--- a/src/ipa/rkisp1/algorithms/agc.cpp\n+++ b/src/ipa/rkisp1/algorithms/agc.cpp\n@@ -74,7 +74,8 @@ int Agc::configure(IPAContext &context, const IPACameraSensorInfo &configInfo)\n {\n \t/* Configure the default exposure and gain. */\n \tcontext.activeState.agc.gain = std::max(context.configuration.agc.minAnalogueGain, kMinAnalogueGain);\n-\tcontext.activeState.agc.exposure = 10ms / context.configuration.sensor.lineDuration;\n+\tcontext.activeState.agc.exposure = 10ms /\n+\t\t\t\t\tstd::chrono::duration(context.configuration.sensor.lineDuration);\n \n \t/*\n \t * According to the RkISP1 documentation:\n@@ -212,16 +213,19 @@ void Agc::computeExposure(IPAContext &context, IPAFrameContext &frameContext,\n \t * Push the shutter time up to the maximum first, and only then\n \t * increase the gain.\n \t */\n-\tutils::Duration shutterTime = std::clamp<utils::Duration>(exposureValue / minAnalogueGain,\n+\tutils::Duration shutterTime = std::clamp<utils::Duration>(std::chrono::duration(exposureValue) /\n+\t\t\t\t\t\t\t\t\tminAnalogueGain,\n \t\t\t\t\t\t\t\t  minShutterSpeed, maxShutterSpeed);\n-\tdouble stepGain = std::clamp(exposureValue / shutterTime,\n+\tdouble stepGain = std::clamp(std::chrono::duration(exposureValue) /\n+\t\t\t\t\t\tstd::chrono::duration(shutterTime),\n \t\t\t\t     minAnalogueGain, maxAnalogueGain);\n \tLOG(RkISP1Agc, Debug) << \"Divided up shutter and gain are \"\n \t\t\t      << shutterTime << \" and \"\n \t\t\t      << stepGain;\n \n \t/* Update the estimated exposure and gain. */\n-\tactiveState.agc.exposure = shutterTime / configuration.sensor.lineDuration;\n+\tactiveState.agc.exposure = std::chrono::duration(shutterTime) /\n+\t\t\t\tstd::chrono::duration(configuration.sensor.lineDuration);\n \tactiveState.agc.gain = stepGain;\n }\n \n","prefixes":["libcamera-devel","v5","01/10"]}