[{"id":18081,"web_url":"https://patchwork.libcamera.org/comment/18081/","msgid":"<YOtJM3BXo6lMmIw/@pendragon.ideasonboard.com>","date":"2021-07-11T19:40:35","subject":"Re: [libcamera-devel] [PATCH v6 5/8] ipa: raspberrypi: Allow long\n\texposure modes for imx477.","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Naush,\n\nThank you for the patch.\n\nOn Fri, Jul 09, 2021 at 03:58:22PM +0100, Naushir Patuck wrote:\n> Update the imx477 CamHelper to use long exposure modes if needed.\n> This is done by overloading the CamHelper::GetVBlanking function to return a\n> frame length (and vblank value) computed using a scaling factor when the value\n> would be larger than what the sensor register could otherwise hold.\n> \n> CamHelperImx477::Prepare is also overloaded to ensure that the \"device.status\"\n> metadata returns the right value if the long exposure scaling factor is used.\n> The scaling factor is unfortunately not returned back in metadata.\n> \n> With the current imx477 driver, we can achieve a maximum exposure time of approx\n> 127 seconds since the HBLANK control is read-only.\n> \n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n> Reviewed-by: David Plowman <david.plowman@raspberrypi.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/ipa/raspberrypi/cam_helper_imx477.cpp | 86 +++++++++++++++++++++++\n>  1 file changed, 86 insertions(+)\n> \n> diff --git a/src/ipa/raspberrypi/cam_helper_imx477.cpp b/src/ipa/raspberrypi/cam_helper_imx477.cpp\n> index 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>  {","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 3877EBD794\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 11 Jul 2021 19:41:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 99F606851B;\n\tSun, 11 Jul 2021 21:41:22 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4D3EA68519\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 11 Jul 2021 21:41:21 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id CC95CCC;\n\tSun, 11 Jul 2021 21:41:20 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"Or/16lWS\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1626032481;\n\tbh=FDJ7n8/r9UhGWhEhLO9eWfdCZqU+C3Gma6km1WPrm6k=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Or/16lWSIOsWlk1XuhkM/Efeu4XI3Ixe+rWmqJd/S4Ie3UHyZPwPQLKQf+lzHaC0H\n\t0DWIq1NK2DjcUpjz/r2k43lb6KRxpw3GKOA15+rxYamKioftsvwoSQV2NmyRbc17FI\n\t9OW/7uu54ldcvF1GR/eNS/O1dCLuQMSVOebPlwR0=","Date":"Sun, 11 Jul 2021 22:40:35 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Naushir Patuck <naush@raspberrypi.com>","Message-ID":"<YOtJM3BXo6lMmIw/@pendragon.ideasonboard.com>","References":"<20210709145825.2943443-1-naush@raspberrypi.com>\n\t<20210709145825.2943443-6-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210709145825.2943443-6-naush@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v6 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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]