From patchwork Fri Jul 2 15:09:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12796 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 67C35BD794 for ; Fri, 2 Jul 2021 15:09:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2A5A568500; Fri, 2 Jul 2021 17:09:57 +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="od/O7oNY"; dkim-atps=neutral Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 78552684F2 for ; Fri, 2 Jul 2021 17:09:54 +0200 (CEST) Received: by mail-wm1-x333.google.com with SMTP id w13so6876657wmc.3 for ; Fri, 02 Jul 2021 08:09:54 -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=XBlZs4X9ro4jey+DyAWOiX9wIMQ7Shrs3R5dauejZKQ=; b=od/O7oNYGKIsaBIb0ZouUYFo/O7kC//CQHmHnepsUJJWir38ZskPo+BUAds84PJSFA M8kKhoBcD5UZO3GVA1WZYqeY33l53nTQ6rEFXt07uSxaJV5BFxL+39ywoLf8/uHttXVq nTvx/yNXKiQFSF0duFRZkn2eWYF/bnmyIVJEuiXMEj2Y0pBl3QLMGSBnAnuVdj/TLXoM F2+qjjCS/4LQkS5S2pvFC5SDU57/UJTVT106NN8Yg79ft8DWfPrbUArsd5aNiFegaSP1 c6xNy7PGgeqTKiTinwNRwQoRFN08zmOQRpYpGCphCMBJ0/lUUI7sEG8/BpJDrjCkFvbe 0HtQ== 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=XBlZs4X9ro4jey+DyAWOiX9wIMQ7Shrs3R5dauejZKQ=; b=pLk/6q7QofY/Xwn7LbbHj7/+s+kNWb2UQAcR/vSc+a8mjk4Hky0Vcqnb8Qb/xBkkcz A9ZO04voDmZbcI8bqdJFz4ATui6Xra3OeLRECDvz5laGzW3ViDijb7KGasVDFWQv1kt7 HfCeq3uTXb5og2zqMW4clyvs0yMhsZ4LZtIwp4Ld+ID2IFKMYflcbKwk2NwEJ0FcHcMW UgBWvpmJQ5g7w2jplk04RhvzlXVLSthPt3s1064H5q1kzjqUBymPGU9KNAu0N9ys5o2A 1TXF2jVuR85Wmr4sCOt/sWua3AU17HPSWkJyC00wRRJLJJnv0Ikvxuih+iL1fZpxf6o3 7LYw== X-Gm-Message-State: AOAM532kWyaAF/ZFfewhBGhLTgnoettlRtIEpfpVyrQTHEr1fXkw3b6Q mcPd+D/FITbU6x3aaaneq9cqr/iR4KbOhQ== X-Google-Smtp-Source: ABdhPJzNIbg2ysX2ciyYgTpTUNrmyId9oUEoBC9ohMrJhHmfq1/1H0oUXpQS9KYmaZFnSr/Lv/liXw== X-Received: by 2002:a1c:59c3:: with SMTP id n186mr309017wmb.48.1625238593948; Fri, 02 Jul 2021 08:09:53 -0700 (PDT) Received: from naush-laptop.patuck.local ([88.97.76.4]) by smtp.gmail.com with ESMTPSA id n4sm3619335wrw.21.2021.07.02.08.09.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jul 2021 08:09:53 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Fri, 2 Jul 2021 16:09:37 +0100 Message-Id: <20210702150940.226941-6-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210702150940.226941-1-naush@raspberrypi.com> References: <20210702150940.226941-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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 | 95 +++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp index 91d05d9226ff..ddf0863c11b4 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,85 @@ 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; + Metadata parsedMetadata; + + if (buffer.empty()) + return; + + if (parser_->Parse(buffer, registers) != MdParser::Status::OK) { + LOG(IPARPI, Error) << "Embedded data buffer parsing failed"; + return; + } + + PopulateMetadata(registers, parsedMetadata); + metadata.Merge(parsedMetadata); + + DeviceStatus deviceStatus, parsedDeviceStatus; + if (metadata.Get("device.status", deviceStatus) || + parsedMetadata.Get("device.status", parsedDeviceStatus)) { + LOG(IPARPI, Error) << "DeviceStatus not found"; + 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) { + deviceStatus.shutter_speed = parsedDeviceStatus.shutter_speed; + deviceStatus.frame_length = parsedDeviceStatus.frame_length; + } + deviceStatus.analogue_gain = parsedDeviceStatus.analogue_gain; + + LOG(IPARPI, Debug) << "Metadata updated - " << deviceStatus; + + metadata.Set("device.status", deviceStatus); +} + +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 = CamHelper::ExposureLines(exposure); + exposureLines = std::min(exposureLines, frameLength - frameIntegrationDiff); + exposure = CamHelper::Exposure(exposureLines); + } + + return frameLength - mode_.height; +} + void CamHelperImx477::GetDelays(int &exposure_delay, int &gain_delay, int &vblank_delay) const {