{"id":12906,"url":"https://patchwork.libcamera.org/api/patches/12906/?format=json","web_url":"https://patchwork.libcamera.org/patch/12906/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20210712100209.447893-6-naush@raspberrypi.com>","date":"2021-07-12T10:02:06","name":"[libcamera-devel,v7,5/8] ipa: raspberrypi: Allow long exposure modes for imx477.","commit_ref":"6d89e33814d6f43873d2f38a13a12528f63a7e0d","pull_url":null,"state":"accepted","archived":false,"hash":"38c9a2f0c7d74b769dc5fcf8244b3e7acd364bf5","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/12906/mbox/","series":[{"id":2227,"url":"https://patchwork.libcamera.org/api/series/2227/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2227","date":"2021-07-12T10:02:01","name":"ipa: raspberrypi: Allow long exposure modes for imx477.","version":7,"mbox":"https://patchwork.libcamera.org/series/2227/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12906/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12906/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 1FF3EC3226\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 12 Jul 2021 10:02:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B62186853F;\n\tMon, 12 Jul 2021 12:02:22 +0200 (CEST)","from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com\n\t[IPv6:2a00:1450:4864:20::32c])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A7B8F68526\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 12 Jul 2021 12:02:19 +0200 (CEST)","by mail-wm1-x32c.google.com with SMTP id\n\ty21-20020a7bc1950000b02902161fccabf1so10822265wmi.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 12 Jul 2021 03:02:19 -0700 (PDT)","from naush-laptop.pitowers.org\n\t([2a00:1098:3142:14:3395:5e50:10bb:f8fd])\n\tby smtp.gmail.com with ESMTPSA id\n\tb16sm14249941wrs.51.2021.07.12.03.02.18\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 12 Jul 2021 03:02:18 -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=\"IO/MTVbh\"; 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=T6NinYvBUiWUrlkwJs5toZ8muaDrPBUvu8EVpeJf3Is=;\n\tb=IO/MTVbhv+b8k2FmCl39X5k0ZEmdT3ZzB9Z6sEyu753CMONwnI1nkP0GkjRVL99d51\n\tNyZxbQVUsxPXTLzpmisVjgYE4l9XusEPlIfIQcvN1M0lrjFpABHiHN0iKERlBXA+8fh+\n\te4+2RWHTviWMogdzsSfV8Zg7LbQB4VkOLBD4+QM2e5t5dzVj0eicVpZPMoaSaihnKDS3\n\tNk3J2A3SjL3BliVBLy2KG7x7OJzEgmhPM5rBEN47WXCj0Qz+Kqz2SbNxKfk1vv7wKKim\n\tn2sUhDy6qYO8hZ0m1O6VHoB6thWZhchnw5HFvigee5zgoRxdPEcppz//XXjmOO9rAxtA\n\t6bOw==","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=T6NinYvBUiWUrlkwJs5toZ8muaDrPBUvu8EVpeJf3Is=;\n\tb=m6X8O0qqiQ0fwv3OvFb5i0FP/nE7zn/rd0KbLl3bu+zraEakA0KNZMl9U0nt/x/sJ2\n\tR00WG1s+R5tEv+saHMDzdIX99BsEh2N0EFD2MCLbHWu/M0Ee7YGhKVuEb9xm/bAWMPnr\n\thzUsqlqCoRDmViTtwswCe62KcJRrWWtQgZVAqgHLd7Y+Uo4EPT4lMatwY+lD2H1fpN9i\n\tbXltIsijZunUi2ZejZZpDgHCmtn0aiKI2f1szmrIiv7b06dApwI0XKbXnXUfDU96Ly5Q\n\t71GOa8P00ZViz8YRge+x6NjiTRSbxCKu0UtLcdKvYTPEaFRyfsMdADSXwt3CxaM9TUCV\n\tdUOg==","X-Gm-Message-State":"AOAM530aKqeFDV7tixj0vCqONf5Matf3ywwlSilaYFC0y8aAMi+42sQV\n\tqv0sm8OO1xEUkpbfiAZngxXQbS2uKyldJw==","X-Google-Smtp-Source":"ABdhPJzGPK2MQZfBj8D12gQa3CPolXbDb85+lh2AV2FO6+t98BJP6Art2S9WnqZKRBYw4AzrJ6gUCw==","X-Received":"by 2002:a05:600c:20b:: with SMTP id\n\t11mr13989626wmi.112.1626084139147; \n\tMon, 12 Jul 2021 03:02:19 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 12 Jul 2021 11:02:06 +0100","Message-Id":"<20210712100209.447893-6-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20210712100209.447893-1-naush@raspberrypi.com>","References":"<20210712100209.447893-1-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v7 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>\nReviewed-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/ipa/raspberrypi/cam_helper_imx477.cpp | 86 +++++++++++++++++++++++\n 1 file changed, 86 insertions(+)","diff":"diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp\nindex 91d05d9226ff..338fdc0c416a 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,76 @@ 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;\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+\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\tDeviceStatus parsedDeviceStatus;\n+\n+\t\tmetadata.Get(\"device.status\", parsedDeviceStatus);\n+\t\tparsedDeviceStatus.shutter_speed = deviceStatus.shutter_speed;\n+\t\tparsedDeviceStatus.frame_length = deviceStatus.frame_length;\n+\t\tmetadata.Set(\"device.status\", parsedDeviceStatus);\n+\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","v7","5/8"]}