From patchwork Wed Jul 7 09:48:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12849 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 1AF98C3224 for ; Wed, 7 Jul 2021 09:48:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A4D7A68532; Wed, 7 Jul 2021 11:48:24 +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="YeBVcR0r"; dkim-atps=neutral Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 846AD6851D for ; Wed, 7 Jul 2021 11:48:21 +0200 (CEST) Received: by mail-wr1-x436.google.com with SMTP id q17so2334651wrv.2 for ; Wed, 07 Jul 2021 02:48:21 -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=0g23v4pBAFw285BuG1E6vpBAHfnc6mX/ZaNFigt0a3Q=; b=YeBVcR0r9yazAveuz7bCmHc9XFlcvC/Z5WhRuilyuZV2Ug9hW05DKetEu9+pl1o2sM 5bEwBnCeQKqxTbFA64/bCY5xIk+eGLBraITzW7Q+uj0p7yD2lHPIluabcjO9EpjJv29r PVvwXDZshjUnF/Io7bmaeUt9VLwq/MglGyV36yKShDqPX2vVSXVSMwr08Da/F0Y3mH+2 GaZ2EoaxeV/9t3h0bF9Q6aro2YNoQEaqoPW+y+si/tQrEKFg/+K9frYBqXohfQt1KWNe 5mZHft8nXL9G60lr441molUKPP1pUjetVvoGaN7est5KsDur3K0qMoH7fPeACvz3XdHa 1Ijw== 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=0g23v4pBAFw285BuG1E6vpBAHfnc6mX/ZaNFigt0a3Q=; b=F3Z8NBcx+nNXVyPR088/2lsjbCDkhXp2FxCmTpi7KRdPL+GbZ4Rk/hcG5iWHsPTz5g j3Ewf5g5lpvoARGNLJnjUT/2bwfn2K/JoxZzfNersNyApq/XCCrs2tw7i8iG1sTrrWuq G89Avwt5cKbPOYVZFGW2o8HBRiClID8OOGQkXDOIZw3PHIOhahAITXBCptvc2h0/PEjL Qh1jhPpsK6Fm0jE7uMnszRx4qTtRJV6Z5idd2TwsXVo0bVk3QosQmUhYjHlb0QTHti9a A0TANB8Jyd3OKcLELLOADXe8M+zuP0lFyr7nWB1MPtpo7oAcLovYm+ZSwTJalhA7cpUW cKoQ== X-Gm-Message-State: AOAM533ooqHjQ9XQl3jZ+ezM1L+EztCVIaBHR1FquhwLXqd0/Wy8ciP/ lI+5XOwqS4K03fx4q0DxOCG6TZyI9DJYzQ== X-Google-Smtp-Source: ABdhPJz+MmwnO4nFjacOS3v3vga7aE1p3DDrFhCwQoXSW28+MFqx4NzeO3MyzlqeD+iR2r0zn1UYfg== X-Received: by 2002:adf:f850:: with SMTP id d16mr26703086wrq.258.1625651301093; Wed, 07 Jul 2021 02:48:21 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:f800:302e:bf7e:a06b]) by smtp.gmail.com with ESMTPSA id r16sm23377836wrx.63.2021.07.07.02.48.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 02:48:20 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Wed, 7 Jul 2021 10:48:12 +0100 Message-Id: <20210707094815.1500917-6-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707094815.1500917-1-naush@raspberrypi.com> References: <20210707094815.1500917-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 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 --- src/ipa/raspberrypi/cam_helper_imx477.cpp | 87 +++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp index 91d05d9226ff..3059a5c6c4d7 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,77 @@ 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, parsedDeviceStatus; + + if (metadata.Get("device.status", deviceStatus)) { + LOG(IPARPI, Error) << "DeviceStatus not found from DelayedControls"; + return; + } + + parseEmbeddedData(buffer, metadata); + + if (metadata.Get("device.status", parsedDeviceStatus)) { + LOG(IPARPI, Error) << "DeviceStatus not found after parsing"; + return; + } + + /* + * 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) { + 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 {