From patchwork Mon Jun 14 10:00: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: 12587 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 0A553BD78E for ; Mon, 14 Jun 2021 10:00:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A590A68938; Mon, 14 Jun 2021 12:00:48 +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="gW00TOjr"; dkim-atps=neutral Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3A3A16029D for ; Mon, 14 Jun 2021 12:00:47 +0200 (CEST) Received: by mail-wm1-x334.google.com with SMTP id l18-20020a1ced120000b029014c1adff1edso12599335wmh.4 for ; Mon, 14 Jun 2021 03:00: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=ZX/Hvx+4FWa/VdRkJFtOz6i8I4UWBAZ7YoGZTOfMBsM=; b=gW00TOjrhyBdhV0ly2cTJ0hjYqRmaXaI9A+xRePwFWjwHITSnGvK8KMheWUuTHADip WkD++bzwKiGPApzXBUUrpxXJS58KBJiMDZL+40ihU53sOuzz3oyxIgmRg+Da7NLlfu5D 59mQxs9eKkvZ+mxemMJRXTM4jIJEoel12Z2Jki//Wsr73LDw9wyduV4bpsC+U7ZgRp7A PyhdvTG37SUgEuRjX27bP4qlNL0kd14bEcKzGHKXBbnkW0V5CsttAGdmMBIgJYhqKey8 fShm+unV/PCoNhjZRnDQNWUWGoeUGCAGx2DgQ3/reh3BH19Lb8NuqC145aSB6bdlxcGM N6YQ== 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=ZX/Hvx+4FWa/VdRkJFtOz6i8I4UWBAZ7YoGZTOfMBsM=; b=YgTfDGC8kiPIKxvYRZVHIcNx6QH0N1J8/r/KSO4K4r4ks+P307E6rRTzTdgeow7yf7 vsCY/V0PC4NOkcSi4VfgajG4yj2PHao+wDrL32TP8QGX1wtQw+WYVQM2WiE+8ZpvtxiQ LXOttwpnUOwzdwf7lZCVoGWMq2eti+na9zX6U9oO/qv6tocwMBsVSZKAcR4fKViluMmO zy8KHuWzgifsckOHlvWbyDShT0qw2k6Ca0NtmZY8KWVF+kqCAhNEg4H17pj1al3jCgrX +tGJP0OMG9GP4NTj8VAI059QyfyuWuR2N8ULbfcvcFk8ekxvC9P8kjObS5AeyJbwj6SG Zc0A== X-Gm-Message-State: AOAM532ZmGox+wgdg94+1ZjE7FNFXFJSNqv2YuPE8RL226TCkEtmj6Wh TH0XzOWssYJl8irHRDMAD8r9tKP5BzP5Qg== X-Google-Smtp-Source: ABdhPJw/6UabzHEAqSc0U9GTDhWRwKWrosbrTyeUBCtWOehBpkA4qlJkcIajF2qLZIOYYacZkKw1tA== X-Received: by 2002:a1c:41c5:: with SMTP id o188mr31506780wma.60.1623664846511; Mon, 14 Jun 2021 03:00:46 -0700 (PDT) Received: from naush-laptop.pitowers.org ([2a00:1098:3142:14:fd93:d554:2dff:83ca]) by smtp.gmail.com with ESMTPSA id 89sm16235959wrq.14.2021.06.14.03.00.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 03:00:46 -0700 (PDT) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Mon, 14 Jun 2021 11:00:37 +0100 Message-Id: <20210614100040.3054433-2-naush@raspberrypi.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210614100040.3054433-1-naush@raspberrypi.com> References: <20210614100040.3054433-1-naush@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 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 use. 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 | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp index 7a1100c25afc..549c2620765a 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 @@ -30,6 +32,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; @@ -40,6 +45,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; MdParserSmia imx477_parser; @@ -72,6 +81,64 @@ 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; + } + + /* 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 {