From patchwork Thu Jul 1 11:34:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 12756 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 A8230C3222 for ; Thu, 1 Jul 2021 11:34:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 65A7B684F7; Thu, 1 Jul 2021 13:34:49 +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="ZYgoJ0P6"; 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 B2A4D60289 for ; Thu, 1 Jul 2021 13:34:47 +0200 (CEST) Received: by mail-wr1-x433.google.com with SMTP id u8so7755231wrq.8 for ; Thu, 01 Jul 2021 04:34:47 -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=d/qzJ12HXMrt3litZ/q3bNli735+KJm88bJeUyFLX2k=; b=ZYgoJ0P6QDiBTL+SLgyePiJnuGTJJk8dTLLQcOHksuLuy3d063m1r8CazDfC0UMyhx v4kgElNcoih1dPFV/fYRtwUvTl/oeV4di0C8Ystp3+AH1/f2VrMRy/M6lCnALaGACq9c XYzNSWysJHimGulNYnhCFqTJnE4jqJCvMLibRJ7pXXJm84+rPsG+odTisJ81h3K1+VPp rjKK8xO2WS9LgoYSHjkFoZpvv/jKIQnEnn52oC3+cg76i1W+QTjRCytW+lKOwDWuDduu 7I6IIWKBrt8fbK4CVSGdOcUyKSWAz48XSFrOqQjM02drxVOqbtiBdNYDKeS3P+DxzfT5 /FMg== 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=d/qzJ12HXMrt3litZ/q3bNli735+KJm88bJeUyFLX2k=; b=KG0wd7HCnH5kVTbcNEOSAGKxQiTUUjKueq2lQoUgAsh3P64iUbbUPzelqX2vTjC1pB bf/kEz6+e4ROt5/DulsiBjtfGk991ARYbsOOwjLbAeDZqFg2cXwBd1OwuV6Wp8GY1ul7 qW86RF6hj4/1kaB8CYskt0I64C7Z4RMndxOSK8i0pkZYC9DHHk8cl9qG1/UMjJp/Eqc5 iucJDHSUoWxpHvR6jjzZHNuNuZHzPfjaVIdH5vJtNcKVN3i2JGQz28Wp9A13nYZdvFbo MxRoHrnftCpNwyBbBpU2rydH5yILl20XNGkHsqYwvY+vQCWaIM4Uk2/Y9kWC4eXTfA5a 8X/g== X-Gm-Message-State: AOAM532dpXKBK2rxXqHejsIPxNOe+g5lnvX+PRB+aIQ9smLu36qECUV8 YZAVChsq3JQjjLtfHMN6o2Af+LnamBZJVg== X-Google-Smtp-Source: ABdhPJydDGlEqPPNmFAt6WgPWEWSVAa4vW1YJXe+cOHBOZ+3erw6GPkPN6u1818vDOL7LLpP53+hpQ== X-Received: by 2002:adf:f74c:: with SMTP id z12mr46159338wrp.18.1625139287314; Thu, 01 Jul 2021 04:34:47 -0700 (PDT) Received: from naush-laptop.patuck.local ([88.97.76.4]) by smtp.gmail.com with ESMTPSA id f6sm8076495wrs.13.2021.07.01.04.34.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jul 2021 04:34:46 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Thu, 1 Jul 2021 12:34:39 +0100 Message-Id: <20210701113442.111718-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210701113442.111718-1-naush@raspberrypi.com> References: <20210701113442.111718-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/4] 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 | 69 +++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp index 4098fde6f322..139cc7dbd39a 100644 --- a/src/ipa/raspberrypi/cam_helper_imx477.cpp +++ b/src/ipa/raspberrypi/cam_helper_imx477.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -14,6 +15,7 @@ #include "md_parser.hpp" using namespace RPiController; +using libcamera::utils::Duration; /* * We care about two gain registers and a pair of exposure registers. Their @@ -31,6 +33,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; @@ -41,6 +46,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; @@ -61,6 +70,66 @@ double CamHelperImx477::Gain(uint32_t gain_code) const return 1024.0 / (1024 - gain_code); } +void CamHelperImx477::Prepare(libcamera::Span buffer, Metadata &metadata) +{ + DeviceStatus deviceStatus, parsedStatus; + + /* Get the device status provided by DelayedControls */ + if (metadata.Get("device.status", deviceStatus) != 0) + return; + + /* Get the device status provided by the embedded data buffer. */ + CamHelper::parseEmbeddedData(buffer, metadata); + metadata.Get("device.status", parsedStatus); + + /* + * If the ratio of DelayedControls to embedded data shutter speed is > 1 + * and is a factor of 2^N, then we can assume this is a long exposure mode + * frame. Since embedded data does not provide any hints of long exposure + * modes, make sure we use the DelayedControls values in the metadata. + * Otherwise, just go with the embedded data values. + */ + unsigned long ratio = std::lround(deviceStatus.shutter_speed / parsedStatus.shutter_speed); + bool replace = (ratio > 1) && ((ratio & (~ratio + 1)) == ratio); + + if (replace) + 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 {