{"id":12796,"url":"https://patchwork.libcamera.org/api/1.1/patches/12796/?format=json","web_url":"https://patchwork.libcamera.org/patch/12796/","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":"<20210702150940.226941-6-naush@raspberrypi.com>","date":"2021-07-02T15:09:37","name":"[libcamera-devel,v3,5/8] ipa: raspberrypi: Allow long exposure modes for imx477.","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"15347020f509d3b70389eb939aa9703f89ee0498","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/12796/mbox/","series":[{"id":2203,"url":"https://patchwork.libcamera.org/api/1.1/series/2203/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2203","date":"2021-07-02T15:09:32","name":"Raspberry Pi: Enable imx477 long exposure modes","version":3,"mbox":"https://patchwork.libcamera.org/series/2203/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12796/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12796/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 67C35BD794\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  2 Jul 2021 15:09:57 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2A5A568500;\n\tFri,  2 Jul 2021 17:09:57 +0200 (CEST)","from mail-wm1-x333.google.com (mail-wm1-x333.google.com\n\t[IPv6:2a00:1450:4864:20::333])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 78552684F2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  2 Jul 2021 17:09:54 +0200 (CEST)","by mail-wm1-x333.google.com with SMTP id w13so6876657wmc.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 02 Jul 2021 08:09:54 -0700 (PDT)","from naush-laptop.patuck.local ([88.97.76.4])\n\tby smtp.gmail.com with ESMTPSA id\n\tn4sm3619335wrw.21.2021.07.02.08.09.53\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 02 Jul 2021 08:09:53 -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=\"od/O7oNY\"; 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=XBlZs4X9ro4jey+DyAWOiX9wIMQ7Shrs3R5dauejZKQ=;\n\tb=od/O7oNYGKIsaBIb0ZouUYFo/O7kC//CQHmHnepsUJJWir38ZskPo+BUAds84PJSFA\n\tM8kKhoBcD5UZO3GVA1WZYqeY33l53nTQ6rEFXt07uSxaJV5BFxL+39ywoLf8/uHttXVq\n\tnTvx/yNXKiQFSF0duFRZkn2eWYF/bnmyIVJEuiXMEj2Y0pBl3QLMGSBnAnuVdj/TLXoM\n\tF2+qjjCS/4LQkS5S2pvFC5SDU57/UJTVT106NN8Yg79ft8DWfPrbUArsd5aNiFegaSP1\n\tc6xNy7PGgeqTKiTinwNRwQoRFN08zmOQRpYpGCphCMBJ0/lUUI7sEG8/BpJDrjCkFvbe\n\t0HtQ==","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=XBlZs4X9ro4jey+DyAWOiX9wIMQ7Shrs3R5dauejZKQ=;\n\tb=pLk/6q7QofY/Xwn7LbbHj7/+s+kNWb2UQAcR/vSc+a8mjk4Hky0Vcqnb8Qb/xBkkcz\n\tA9ZO04voDmZbcI8bqdJFz4ATui6Xra3OeLRECDvz5laGzW3ViDijb7KGasVDFWQv1kt7\n\tHfCeq3uTXb5og2zqMW4clyvs0yMhsZ4LZtIwp4Ld+ID2IFKMYflcbKwk2NwEJ0FcHcMW\n\tUgBWvpmJQ5g7w2jplk04RhvzlXVLSthPt3s1064H5q1kzjqUBymPGU9KNAu0N9ys5o2A\n\t1TXF2jVuR85Wmr4sCOt/sWua3AU17HPSWkJyC00wRRJLJJnv0Ikvxuih+iL1fZpxf6o3\n\t7LYw==","X-Gm-Message-State":"AOAM532kWyaAF/ZFfewhBGhLTgnoettlRtIEpfpVyrQTHEr1fXkw3b6Q\n\tmcPd+D/FITbU6x3aaaneq9cqr/iR4KbOhQ==","X-Google-Smtp-Source":"ABdhPJzNIbg2ysX2ciyYgTpTUNrmyId9oUEoBC9ohMrJhHmfq1/1H0oUXpQS9KYmaZFnSr/Lv/liXw==","X-Received":"by 2002:a1c:59c3:: with SMTP id n186mr309017wmb.48.1625238593948;\n\tFri, 02 Jul 2021 08:09:53 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 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 | 95 +++++++++++++++++++++++\n 1 file changed, 95 insertions(+)","diff":"diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp\nindex 91d05d9226ff..ddf0863c11b4 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,85 @@ 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+\tMetadata parsedMetadata;\n+\n+\tif (buffer.empty())\n+\t\treturn;\n+\n+\tif (parser_->Parse(buffer, registers) != MdParser::Status::OK) {\n+\t\tLOG(IPARPI, Error) << \"Embedded data buffer parsing failed\";\n+\t\treturn;\n+\t}\n+\n+\tPopulateMetadata(registers, parsedMetadata);\n+\tmetadata.Merge(parsedMetadata);\n+\n+\tDeviceStatus deviceStatus, parsedDeviceStatus;\n+\tif (metadata.Get(\"device.status\", deviceStatus) ||\n+\t    parsedMetadata.Get(\"device.status\", parsedDeviceStatus)) {\n+\t\tLOG(IPARPI, Error) << \"DeviceStatus not found\";\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\tdeviceStatus.shutter_speed = parsedDeviceStatus.shutter_speed;\n+\t\tdeviceStatus.frame_length = parsedDeviceStatus.frame_length;\n+\t}\n+\tdeviceStatus.analogue_gain = parsedDeviceStatus.analogue_gain;\n+\n+\tLOG(IPARPI, Debug) << \"Metadata updated - \" << deviceStatus;\n+\n+\tmetadata.Set(\"device.status\", deviceStatus);\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 = CamHelper::ExposureLines(exposure);\n+\t\texposureLines = std::min(exposureLines, frameLength - frameIntegrationDiff);\n+\t\texposure = CamHelper::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","v3","5/8"]}