{"id":12587,"url":"https://patchwork.libcamera.org/api/1.1/patches/12587/?format=json","web_url":"https://patchwork.libcamera.org/patch/12587/","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":"<20210614100040.3054433-2-naush@raspberrypi.com>","date":"2021-06-14T10:00:37","name":"[libcamera-devel,1/4] ipa: raspberrypi: Allow long exposure modes for imx477.","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"e91c877d4e0d9f19a479621a4dcd26e326350912","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/12587/mbox/","series":[{"id":2129,"url":"https://patchwork.libcamera.org/api/1.1/series/2129/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2129","date":"2021-06-14T10:00:36","name":"Raspberry Pi: Enable imx477 long exposure modes","version":1,"mbox":"https://patchwork.libcamera.org/series/2129/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12587/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12587/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 0A553BD78E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 14 Jun 2021 10:00:49 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A590A68938;\n\tMon, 14 Jun 2021 12:00:48 +0200 (CEST)","from mail-wm1-x334.google.com (mail-wm1-x334.google.com\n\t[IPv6:2a00:1450:4864:20::334])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3A3A16029D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 14 Jun 2021 12:00:47 +0200 (CEST)","by mail-wm1-x334.google.com with SMTP id\n\tl18-20020a1ced120000b029014c1adff1edso12599335wmh.4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 14 Jun 2021 03:00:47 -0700 (PDT)","from naush-laptop.pitowers.org\n\t([2a00:1098:3142:14:fd93:d554:2dff:83ca])\n\tby smtp.gmail.com with ESMTPSA id\n\t89sm16235959wrq.14.2021.06.14.03.00.46\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 14 Jun 2021 03:00:46 -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=\"gW00TOjr\"; 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=ZX/Hvx+4FWa/VdRkJFtOz6i8I4UWBAZ7YoGZTOfMBsM=;\n\tb=gW00TOjrhyBdhV0ly2cTJ0hjYqRmaXaI9A+xRePwFWjwHITSnGvK8KMheWUuTHADip\n\tWkD++bzwKiGPApzXBUUrpxXJS58KBJiMDZL+40ihU53sOuzz3oyxIgmRg+Da7NLlfu5D\n\t59mQxs9eKkvZ+mxemMJRXTM4jIJEoel12Z2Jki//Wsr73LDw9wyduV4bpsC+U7ZgRp7A\n\tPyhdvTG37SUgEuRjX27bP4qlNL0kd14bEcKzGHKXBbnkW0V5CsttAGdmMBIgJYhqKey8\n\tfShm+unV/PCoNhjZRnDQNWUWGoeUGCAGx2DgQ3/reh3BH19Lb8NuqC145aSB6bdlxcGM\n\tN6YQ==","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=ZX/Hvx+4FWa/VdRkJFtOz6i8I4UWBAZ7YoGZTOfMBsM=;\n\tb=YgTfDGC8kiPIKxvYRZVHIcNx6QH0N1J8/r/KSO4K4r4ks+P307E6rRTzTdgeow7yf7\n\tvsCY/V0PC4NOkcSi4VfgajG4yj2PHao+wDrL32TP8QGX1wtQw+WYVQM2WiE+8ZpvtxiQ\n\tLXOttwpnUOwzdwf7lZCVoGWMq2eti+na9zX6U9oO/qv6tocwMBsVSZKAcR4fKViluMmO\n\tzy8KHuWzgifsckOHlvWbyDShT0qw2k6Ca0NtmZY8KWVF+kqCAhNEg4H17pj1al3jCgrX\n\t+tGJP0OMG9GP4NTj8VAI059QyfyuWuR2N8ULbfcvcFk8ekxvC9P8kjObS5AeyJbwj6SG\n\tZc0A==","X-Gm-Message-State":"AOAM532ZmGox+wgdg94+1ZjE7FNFXFJSNqv2YuPE8RL226TCkEtmj6Wh\n\tTH0XzOWssYJl8irHRDMAD8r9tKP5BzP5Qg==","X-Google-Smtp-Source":"ABdhPJw/6UabzHEAqSc0U9GTDhWRwKWrosbrTyeUBCtWOehBpkA4qlJkcIajF2qLZIOYYacZkKw1tA==","X-Received":"by 2002:a1c:41c5:: with SMTP id\n\to188mr31506780wma.60.1623664846511; \n\tMon, 14 Jun 2021 03:00:46 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 1/4] ipa: raspberrypi: Allow long exposure\n\tmodes 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 use.\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 | 67 +++++++++++++++++++++++\n 1 file changed, 67 insertions(+)","diff":"diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp\nindex 7a1100c25afc..549c2620765a 100644\n--- a/src/ipa/raspberrypi/cam_helper_imx477.cpp\n+++ b/src/ipa/raspberrypi/cam_helper_imx477.cpp\n@@ -6,6 +6,7 @@\n  */\n \n #include <assert.h>\n+#include <cmath>\n #include <stddef.h>\n #include <stdio.h>\n #include <stdlib.h>\n@@ -14,6 +15,7 @@\n #include \"md_parser.hpp\"\n \n using namespace RPiController;\n+using libcamera::utils::Duration;\n \n /*\n  * We care about two gain registers and a pair of exposure registers. Their\n@@ -30,6 +32,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@@ -40,6 +45,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 \tMdParserSmia imx477_parser;\n \n@@ -72,6 +81,64 @@ 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+\tDeviceStatus deviceStatus, parsedStatus;\n+\n+\t/* Get the device status provided by DelayedControls */\n+\tif (metadata.Get(\"device.status\", deviceStatus) != 0)\n+\t\treturn;\n+\n+\t/* Get the device status provided by the embedded data buffer. */\n+\tCamHelper::parseEmbeddedData(buffer, metadata);\n+\tmetadata.Get(\"device.status\", parsedStatus);\n+\n+\t/*\n+\t * If the ratio of DelayedControls to embedded data shutter speed is > 1\n+\t * and is a factor of 2^N, then we can assume this is a long exposure mode\n+\t * frame. Since embedded data does not provide any hints of long exposure\n+\t * modes, make sure we use the DelayedControls values in the metadata.\n+\t * Otherwise, just go with the embedded data values.\n+\t */\n+\tunsigned long ratio = std::lround(deviceStatus.shutter_speed / parsedStatus.shutter_speed);\n+\tbool replace = (ratio > 1) && ((ratio & (~ratio + 1)) == ratio);\n+\n+\tif (replace)\n+\t\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+\t/* Account for any rounding in the scaled frame length value. */\n+\tframeLength <<= shift;\n+\texposureLines = CamHelper::ExposureLines(exposure);\n+\texposureLines = std::min(exposureLines, frameLength - frameIntegrationDiff);\n+\texposure = CamHelper::Exposure(exposureLines);\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","1/4"]}