From patchwork Fri Jul 9 14:58:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12888 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 605FBBD794 for ; Fri, 9 Jul 2021 14:58:37 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 14F6668539; Fri, 9 Jul 2021 16:58:37 +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="MJyovD9h"; dkim-atps=neutral Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 03E616851D for ; Fri, 9 Jul 2021 16:58:34 +0200 (CEST) Received: by mail-wr1-x433.google.com with SMTP id v5so12553317wrt.3 for ; Fri, 09 Jul 2021 07:58:34 -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=ypB7ftg8zDueJJ6fwvcqa12JR5INT0ou4cXYJuiy1Qg=; b=MJyovD9h7cW3YlAqiLtCBUpW1S4VDmm2jhKYVuvBpBUmDliF+AIHRPx8uNd5ep+qvi aFVg8HC6z7bzsXjNDB41OCxoqZs7G+bPlPm82biXgdUGUVhkGRY7/s1uFeO5UUsMiTB0 XQet285wxMFaEv4GPa729q125CAuvIKIOztMekkz4WKs4rDLvORBsmUaLJiaAMkFzQEq UW3PAqP+Am428eptb/0h2gcyrUe5GpJwkPpkx8dwXmpq8OzvRGaUxDwj5M+GhT92z4XD FqqY2fHlx2A6xijcbO2h1agECkz8IRrziiu5ptfg6pOwuE+vsjLqE7sM8V1tFxkM7dW0 jNNQ== 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=ypB7ftg8zDueJJ6fwvcqa12JR5INT0ou4cXYJuiy1Qg=; b=UAv9nzod+ddGdliFNHeUeGKrj/ehTGaBfRnzJj2FpcnbxK/V4r6OQasD9lCg6HkA1K +MhZmHvh8SKjoY0aADW0VGY1dMLIP3JughYOtbeEskqB1y8eG8fsjZdRHk9+JiD2MzK1 LkvB1fLqUgg3mVcwkBOcuFWiOEQFjGgIafzyM0mU1SIAdbzviNt2kNvlqjvQto/8fIOg iLTJjblofrJ5Tm6lM0XYebD6/LoVOM20wPOSRsVwFENcKppNVl3+/grBwUZ+4gWHo1TW Rfxp0ztkgXOegJDgHaFjbvGA9utkS8BC7vjcsX7JJho50KDlcuh7btdapPPscp7LpThn G3gA== X-Gm-Message-State: AOAM5328VyLVJ1pBL+4TqXBQ8QiTYNT+7BudtqFeoTHc2zSsspvhL8dJ CkpKF90RCVuTTtGd0t2hR70arzFt2bpSwg== X-Google-Smtp-Source: ABdhPJz8ByNG0a581ByD5/vz0AwrnLA37p6wwFBIdc4JTCGYPLQ664+ziUI1cQAgwBDrEyCtp90nBQ== X-Received: by 2002:adf:d1e6:: with SMTP id g6mr31186978wrd.140.1625842713473; Fri, 09 Jul 2021 07:58:33 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:35c2:26d3:e217:4410]) by smtp.gmail.com with ESMTPSA id c2sm1480995wrn.28.2021.07.09.07.58.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jul 2021 07:58:32 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Fri, 9 Jul 2021 15:58:22 +0100 Message-Id: <20210709145825.2943443-6-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210709145825.2943443-1-naush@raspberrypi.com> References: <20210709145825.2943443-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 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 Reviewed-by: Laurent Pinchart --- 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 {