From patchwork Fri Jul 9 09:56:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12876 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 67F2BC3225 for ; Fri, 9 Jul 2021 09:56:55 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EFE1A68533; Fri, 9 Jul 2021 11:56:54 +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="o62Af9ZR"; dkim-atps=neutral Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4EB1968531 for ; Fri, 9 Jul 2021 11:56:52 +0200 (CEST) Received: by mail-wm1-x32b.google.com with SMTP id h18-20020a05600c3512b029020e4ceb9588so8703862wmq.5 for ; Fri, 09 Jul 2021 02:56:52 -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=maS9Ine8zP34587cltgrRUI4ixqmUO8BFXsZOcr5y1k=; b=o62Af9ZREFIjmuVf6laLpdHc3rWx3AE4CyPIIRdphNdbjkefmsMw3zEaC2XgcXFTLj 3uAZ2jqVlBOpbV6Q04NMhOhCTuTj7y9SxKu/rK2aOHgcs48Hf5IyaUci5vEVysVk0CWz I5cL9R6EN+XhKE+4274lU5ufzFtyLpZzGFC3ndttY3CzkqDhI2Mz25D/UHQyYPae2/aC /xi3thK9tEuUui/kQlywiOWdx/xGKa8n79xPs0QmDu6soZ2LxZYiBqcVyUSv1VD/anUG ZdF0h61a4vE1C152HAXM8nojB0I0Z+vzIcoKqAM+lvOfiTB45Ze4qq+fo1uDBr5i9PFM 5EFA== 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=maS9Ine8zP34587cltgrRUI4ixqmUO8BFXsZOcr5y1k=; b=Rk5zR+pxQzDa3jMN27VgsVPhwvbB4zz2Ze8eOkkle2AnLJw15Ctyu6G7lq39pvpoxp AC3VGk/jNMOxMGxDeL+bbFuc2S/XO/XRNkbFQpd7mINz1Xm2aP4L84waerTnnyMdRuCG E6vSzNRugXIiDAmu4L6H8UWhk/xnB29YOdMP2aQMXxeyGG/2nPedTI+pD7vlHUDe+CZK qedQYiM2AvtYqxzy/gSZiiPT32MdV1Exv4UiruTO/XsTUkMiQLBkByBqnr2Hk7fwjjFo PV2OoyAm1DRWUMyTFQALIjxmVsDnmSP9qZp/F/UXFZ/kPj+CQtbJQZWKQkvuEf1FxTs/ kwOQ== X-Gm-Message-State: AOAM532/KMyeofpHB1yLSMBdyiYsI3KNtU0hNPwvjkxi8XLB6CxMqotn Nf0ySwvX8KOUjqpH4hPFY/V/VGutbPAqpg== X-Google-Smtp-Source: ABdhPJwufo8CLx+YDrtXHf+jqQLMCx44jJP181KBft+dx/Z7MULItf1QPX28QFJYW5mixpZpURQkTA== X-Received: by 2002:a1c:f312:: with SMTP id q18mr39256394wmq.12.1625824611743; Fri, 09 Jul 2021 02:56:51 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:35c2:26d3:e217:4410]) by smtp.gmail.com with ESMTPSA id b16sm1599014wrw.46.2021.07.09.02.56.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jul 2021 02:56:51 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Fri, 9 Jul 2021 10:56:35 +0100 Message-Id: <20210709095638.2801713-5-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210709095638.2801713-1-naush@raspberrypi.com> References: <20210709095638.2801713-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 5/8] ipa: raspberrypi: Allow long exposure modes for imx477. 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" Update the imx477 CamHelper to use long exposure modes if needed. This is done by overloading the CamHelper::GetVBlanking function to return a frame length (and vblank value) computed using a scaling factor when the value would be larger than what the sensor register could otherwise hold. CamHelperImx477::Prepare is also overloaded to ensure that the "device.status" metadata returns the right value if the long exposure scaling factor is used. The scaling factor is unfortunately not returned back in metadata. With the current imx477 driver, we can achieve a maximum exposure time of approx 127 seconds since the HBLANK control is read-only. Signed-off-by: Naushir Patuck Reviewed-by: David Plowman Reviewed-by: Kieran Bingham --- src/ipa/raspberrypi/cam_helper_imx477.cpp | 86 +++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp index 91d05d9226ff..338fdc0c416a 100644 --- a/src/ipa/raspberrypi/cam_helper_imx477.cpp +++ b/src/ipa/raspberrypi/cam_helper_imx477.cpp @@ -6,14 +6,23 @@ */ #include +#include #include #include #include +#include + #include "cam_helper.hpp" #include "md_parser.hpp" using namespace RPiController; +using namespace libcamera; +using libcamera::utils::Duration; + +namespace libcamera { +LOG_DECLARE_CATEGORY(IPARPI) +} /* * We care about two gain registers and a pair of exposure registers. Their @@ -34,6 +43,9 @@ public: CamHelperImx477(); uint32_t GainCode(double gain) const override; double Gain(uint32_t gain_code) const override; + void Prepare(libcamera::Span buffer, Metadata &metadata) override; + uint32_t GetVBlanking(Duration &exposure, Duration minFrameDuration, + Duration maxFrameDuration) const override; void GetDelays(int &exposure_delay, int &gain_delay, int &vblank_delay) const override; bool SensorEmbeddedDataPresent() const override; @@ -44,6 +56,10 @@ private: * in units of lines. */ static constexpr int frameIntegrationDiff = 22; + /* Maximum frame length allowable for long exposure calculations. */ + static constexpr int frameLengthMax = 0xffdc; + /* Largest long exposure scale factor given as a left shift on the frame length. */ + static constexpr int longExposureShiftMax = 7; void PopulateMetadata(const MdParser::RegisterMap ®isters, Metadata &metadata) const override; @@ -64,6 +80,76 @@ double CamHelperImx477::Gain(uint32_t gain_code) const return 1024.0 / (1024 - gain_code); } +void CamHelperImx477::Prepare(libcamera::Span buffer, Metadata &metadata) +{ + MdParser::RegisterMap registers; + DeviceStatus deviceStatus; + + if (metadata.Get("device.status", deviceStatus)) { + LOG(IPARPI, Error) << "DeviceStatus not found from DelayedControls"; + return; + } + + parseEmbeddedData(buffer, metadata); + + /* + * The DeviceStatus struct is first populated with values obtained from + * DelayedControls. If this reports frame length is > frameLengthMax, + * it means we are using a long exposure mode. Since the long exposure + * scale factor is not returned back through embedded data, we must rely + * on the existing exposure lines and frame length values returned by + * DelayedControls. + * + * Otherwise, all values are updated with what is reported in the + * embedded data. + */ + if (deviceStatus.frame_length > frameLengthMax) { + DeviceStatus parsedDeviceStatus; + + metadata.Get("device.status", parsedDeviceStatus); + parsedDeviceStatus.shutter_speed = deviceStatus.shutter_speed; + parsedDeviceStatus.frame_length = deviceStatus.frame_length; + metadata.Set("device.status", parsedDeviceStatus); + + LOG(IPARPI, Debug) << "Metadata updated for long exposure: " + << parsedDeviceStatus; + } +} + +uint32_t CamHelperImx477::GetVBlanking(Duration &exposure, + Duration minFrameDuration, + Duration maxFrameDuration) const +{ + uint32_t frameLength, exposureLines; + unsigned int shift = 0; + + frameLength = mode_.height + CamHelper::GetVBlanking(exposure, minFrameDuration, + maxFrameDuration); + /* + * Check if the frame length calculated needs to be setup for long + * exposure mode. This will require us to use a long exposure scale + * factor provided by a shift operation in the sensor. + */ + while (frameLength > frameLengthMax) { + if (++shift > longExposureShiftMax) { + shift = longExposureShiftMax; + frameLength = frameLengthMax; + break; + } + frameLength >>= 1; + } + + if (shift) { + /* Account for any rounding in the scaled frame length value. */ + frameLength <<= shift; + exposureLines = ExposureLines(exposure); + exposureLines = std::min(exposureLines, frameLength - frameIntegrationDiff); + exposure = Exposure(exposureLines); + } + + return frameLength - mode_.height; +} + void CamHelperImx477::GetDelays(int &exposure_delay, int &gain_delay, int &vblank_delay) const {