{"id":12849,"url":"https://patchwork.libcamera.org/api/1.1/patches/12849/?format=json","web_url":"https://patchwork.libcamera.org/patch/12849/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210707094815.1500917-6-naush@raspberrypi.com>","date":"2021-07-07T09:48:12","name":"[libcamera-devel,v4,5/8] ipa: raspberrypi: Allow long exposure modes for imx477.","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"eea152a950a34989c5df641c7577071262480541","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/1.1/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/12849/mbox/","series":[{"id":2214,"url":"https://patchwork.libcamera.org/api/1.1/series/2214/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2214","date":"2021-07-07T09:48:07","name":"Raspberry Pi: Enable imx477 long exposure modes","version":4,"mbox":"https://patchwork.libcamera.org/series/2214/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12849/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12849/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 1AF98C3224\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  7 Jul 2021 09:48:25 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A4D7A68532;\n\tWed,  7 Jul 2021 11:48:24 +0200 (CEST)","from mail-wr1-x436.google.com (mail-wr1-x436.google.com\n\t[IPv6:2a00:1450:4864:20::436])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 846AD6851D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  7 Jul 2021 11:48:21 +0200 (CEST)","by mail-wr1-x436.google.com with SMTP id q17so2334651wrv.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 07 Jul 2021 02:48:21 -0700 (PDT)","from naush-laptop.pitowers.org\n\t([2a00:1098:3142:14:f800:302e:bf7e:a06b])\n\tby smtp.gmail.com with ESMTPSA id\n\tr16sm23377836wrx.63.2021.07.07.02.48.20\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 07 Jul 2021 02:48:20 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"YeBVcR0r\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=0g23v4pBAFw285BuG1E6vpBAHfnc6mX/ZaNFigt0a3Q=;\n\tb=YeBVcR0r9yazAveuz7bCmHc9XFlcvC/Z5WhRuilyuZV2Ug9hW05DKetEu9+pl1o2sM\n\t5bEwBnCeQKqxTbFA64/bCY5xIk+eGLBraITzW7Q+uj0p7yD2lHPIluabcjO9EpjJv29r\n\tPVvwXDZshjUnF/Io7bmaeUt9VLwq/MglGyV36yKShDqPX2vVSXVSMwr08Da/F0Y3mH+2\n\tGaZ2EoaxeV/9t3h0bF9Q6aro2YNoQEaqoPW+y+si/tQrEKFg/+K9frYBqXohfQt1KWNe\n\t5mZHft8nXL9G60lr441molUKPP1pUjetVvoGaN7est5KsDur3K0qMoH7fPeACvz3XdHa\n\t1Ijw==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=0g23v4pBAFw285BuG1E6vpBAHfnc6mX/ZaNFigt0a3Q=;\n\tb=F3Z8NBcx+nNXVyPR088/2lsjbCDkhXp2FxCmTpi7KRdPL+GbZ4Rk/hcG5iWHsPTz5g\n\tj3Ewf5g5lpvoARGNLJnjUT/2bwfn2K/JoxZzfNersNyApq/XCCrs2tw7i8iG1sTrrWuq\n\tG89Avwt5cKbPOYVZFGW2o8HBRiClID8OOGQkXDOIZw3PHIOhahAITXBCptvc2h0/PEjL\n\tQh1jhPpsK6Fm0jE7uMnszRx4qTtRJV6Z5idd2TwsXVo0bVk3QosQmUhYjHlb0QTHti9a\n\tA0TANB8Jyd3OKcLELLOADXe8M+zuP0lFyr7nWB1MPtpo7oAcLovYm+ZSwTJalhA7cpUW\n\tcKoQ==","X-Gm-Message-State":"AOAM533ooqHjQ9XQl3jZ+ezM1L+EztCVIaBHR1FquhwLXqd0/Wy8ciP/\n\tlI+5XOwqS4K03fx4q0DxOCG6TZyI9DJYzQ==","X-Google-Smtp-Source":"ABdhPJz+MmwnO4nFjacOS3v3vga7aE1p3DDrFhCwQoXSW28+MFqx4NzeO3MyzlqeD+iR2r0zn1UYfg==","X-Received":"by 2002:adf:f850:: with SMTP id\n\td16mr26703086wrq.258.1625651301093; \n\tWed, 07 Jul 2021 02:48:21 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 5/8] ipa: raspberrypi: Allow long\n\texposure modes for imx477.","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Update the imx477 CamHelper to use long exposure modes if needed.\nThis is done by overloading the CamHelper::GetVBlanking function to return a\nframe length (and vblank value) computed using a scaling factor when the value\nwould be larger than what the sensor register could otherwise hold.\n\nCamHelperImx477::Prepare is also overloaded to ensure that the \"device.status\"\nmetadata returns the right value if the long exposure scaling factor is used.\nThe scaling factor is unfortunately not returned back in metadata.\n\nWith the current imx477 driver, we can achieve a maximum exposure time of approx\n127 seconds since the HBLANK control is read-only.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n src/ipa/raspberrypi/cam_helper_imx477.cpp | 87 +++++++++++++++++++++++\n 1 file changed, 87 insertions(+)","diff":"diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp\nindex 91d05d9226ff..3059a5c6c4d7 100644\n--- a/src/ipa/raspberrypi/cam_helper_imx477.cpp\n+++ b/src/ipa/raspberrypi/cam_helper_imx477.cpp\n@@ -6,14 +6,23 @@\n  */\n \n #include <assert.h>\n+#include <cmath>\n #include <stddef.h>\n #include <stdio.h>\n #include <stdlib.h>\n \n+#include <libcamera/base/log.h>\n+\n #include \"cam_helper.hpp\"\n #include \"md_parser.hpp\"\n \n using namespace RPiController;\n+using namespace libcamera;\n+using libcamera::utils::Duration;\n+\n+namespace libcamera {\n+LOG_DECLARE_CATEGORY(IPARPI)\n+}\n \n /*\n  * We care about two gain registers and a pair of exposure registers. Their\n@@ -34,6 +43,9 @@ public:\n \tCamHelperImx477();\n \tuint32_t GainCode(double gain) const override;\n \tdouble Gain(uint32_t gain_code) const override;\n+\tvoid Prepare(libcamera::Span<const uint8_t> buffer, Metadata &metadata) override;\n+\tuint32_t GetVBlanking(Duration &exposure, Duration minFrameDuration,\n+\t\t\t      Duration maxFrameDuration) const override;\n \tvoid GetDelays(int &exposure_delay, int &gain_delay,\n \t\t       int &vblank_delay) const override;\n \tbool SensorEmbeddedDataPresent() const override;\n@@ -44,6 +56,10 @@ private:\n \t * in units of lines.\n \t */\n \tstatic constexpr int frameIntegrationDiff = 22;\n+\t/* Maximum frame length allowable for long exposure calculations. */\n+\tstatic constexpr int frameLengthMax = 0xffdc;\n+\t/* Largest long exposure scale factor given as a left shift on the frame length. */\n+\tstatic constexpr int longExposureShiftMax = 7;\n \n \tvoid PopulateMetadata(const MdParser::RegisterMap &registers,\n \t\t\t      Metadata &metadata) const override;\n@@ -64,6 +80,77 @@ double CamHelperImx477::Gain(uint32_t gain_code) const\n \treturn 1024.0 / (1024 - gain_code);\n }\n \n+void CamHelperImx477::Prepare(libcamera::Span<const uint8_t> buffer, Metadata &metadata)\n+{\n+\tMdParser::RegisterMap registers;\n+\tDeviceStatus deviceStatus, parsedDeviceStatus;\n+\n+\tif (metadata.Get(\"device.status\", deviceStatus)) {\n+\t\tLOG(IPARPI, Error) << \"DeviceStatus not found from DelayedControls\";\n+\t\treturn;\n+\t}\n+\n+\tparseEmbeddedData(buffer, metadata);\n+\n+\tif (metadata.Get(\"device.status\", parsedDeviceStatus)) {\n+\t\tLOG(IPARPI, Error) << \"DeviceStatus not found after parsing\";\n+\t\treturn;\n+\t}\n+\n+\t/*\n+\t * The DeviceStatus struct is first populated with values obtained from\n+\t * DelayedControls. If this reports frame length is > frameLengthMax,\n+\t * it means we are using a long exposure mode. Since the long exposure\n+\t * scale factor is not returned back through embedded data, we must rely\n+\t * on the existing exposure lines and frame length values returned by\n+\t * DelayedControls.\n+\t *\n+\t * Otherwise, all values are updated with what is reported in the\n+\t * embedded data.\n+\t */\n+\tif (deviceStatus.frame_length > frameLengthMax) {\n+\t\tparsedDeviceStatus.shutter_speed = deviceStatus.shutter_speed;\n+\t\tparsedDeviceStatus.frame_length = deviceStatus.frame_length;\n+\t\tmetadata.Set(\"device.status\", parsedDeviceStatus);\n+\t\tLOG(IPARPI, Debug) << \"Metadata updated for long exposure: \"\n+\t\t\t\t   << parsedDeviceStatus;\n+\t}\n+}\n+\n+uint32_t CamHelperImx477::GetVBlanking(Duration &exposure,\n+\t\t\t\t       Duration minFrameDuration,\n+\t\t\t\t       Duration maxFrameDuration) const\n+{\n+\tuint32_t frameLength, exposureLines;\n+\tunsigned int shift = 0;\n+\n+\tframeLength = mode_.height + CamHelper::GetVBlanking(exposure, minFrameDuration,\n+\t\t\t\t\t\t\t     maxFrameDuration);\n+\t/*\n+\t * Check if the frame length calculated needs to be setup for long\n+\t * exposure mode. This will require us to use a long exposure scale\n+\t * factor provided by a shift operation in the sensor.\n+\t */\n+\twhile (frameLength > frameLengthMax) {\n+\t\tif (++shift > longExposureShiftMax) {\n+\t\t\tshift = longExposureShiftMax;\n+\t\t\tframeLength = frameLengthMax;\n+\t\t\tbreak;\n+\t\t}\n+\t\tframeLength >>= 1;\n+\t}\n+\n+\tif (shift) {\n+\t\t/* Account for any rounding in the scaled frame length value. */\n+\t\tframeLength <<= shift;\n+\t\texposureLines = ExposureLines(exposure);\n+\t\texposureLines = std::min(exposureLines, frameLength - frameIntegrationDiff);\n+\t\texposure = Exposure(exposureLines);\n+\t}\n+\n+\treturn frameLength - mode_.height;\n+}\n+\n void CamHelperImx477::GetDelays(int &exposure_delay, int &gain_delay,\n \t\t\t\tint &vblank_delay) const\n {\n","prefixes":["libcamera-devel","v4","5/8"]}