{"id":15169,"url":"https://patchwork.libcamera.org/api/patches/15169/?format=json","web_url":"https://patchwork.libcamera.org/patch/15169/","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":"<20211219232714.11427-4-laurent.pinchart@ideasonboard.com>","date":"2021-12-19T23:27:13","name":"[libcamera-devel,3/4] ipa: raspberrypi: Add camera helper for Sony IMX296 sensor","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"21152f4ba8e9d4ca88593619ee0409605de1fd52","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":{"id":14,"url":"https://patchwork.libcamera.org/api/users/14/?format=json","username":"pinchartl","first_name":"Laurent","last_name":"Pinchart","email":"laurent.pinchart@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/15169/mbox/","series":[{"id":2847,"url":"https://patchwork.libcamera.org/api/series/2847/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2847","date":"2021-12-19T23:27:10","name":"libcamera: Add support for the IMX296 sensor in the Raspberry Pi IPA","version":1,"mbox":"https://patchwork.libcamera.org/series/2847/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/15169/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/15169/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 5CF1DC3258\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 19 Dec 2021 23:27:26 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 00221608A2;\n\tMon, 20 Dec 2021 00:27:25 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 11702605A7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 20 Dec 2021 00:27:22 +0100 (CET)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 869F011CD; \n\tMon, 20 Dec 2021 00:27:21 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"F0EO+E/Y\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1639956441;\n\tbh=pBmnSKjSwQVS3Fek/2ep4ISkZFRrg0Z6zru5iYxIUWI=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=F0EO+E/YWXKnOv7r+SjsIWP7OmmSEzQvGBWvMRHhxBue35ubnv+/NDhhsleYNrnZB\n\t/ZQLhZbyyOXj4fkFBTOWODjoKtqKbK663XgAsdGw9U7mGKNKgSsy2Oi5Wk9D6ACTHY\n\tDDZzOBz99NUOxi13IgO0+8NHF3WQC8pJ2uCQ1ytg=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 20 Dec 2021 01:27:13 +0200","Message-Id":"<20211219232714.11427-4-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20211219232714.11427-1-laurent.pinchart@ideasonboard.com>","References":"<20211219232714.11427-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 3/4] ipa: raspberrypi: Add camera helper\n\tfor Sony IMX296 sensor","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":"From: Naushir Patuck <naush@raspberrypi.com>\n\nThe Sony IMX296 sensor has an exponential gain model, and adds a fixed\n14.26µs offset to the exposure time expressed in line units.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/ipa/raspberrypi/cam_helper_imx296.cpp | 126 ++++++++++++++++++++++\n src/ipa/raspberrypi/meson.build           |   1 +\n 2 files changed, 127 insertions(+)\n create mode 100644 src/ipa/raspberrypi/cam_helper_imx296.cpp","diff":"diff --git a/src/ipa/raspberrypi/cam_helper_imx296.cpp b/src/ipa/raspberrypi/cam_helper_imx296.cpp\nnew file mode 100644\nindex 000000000000..9c34582861f1\n--- /dev/null\n+++ b/src/ipa/raspberrypi/cam_helper_imx296.cpp\n@@ -0,0 +1,126 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2020, Raspberry Pi (Trading) Limited\n+ *\n+ * cam_helper_imx296.cpp - camera helper for imx296 sensor\n+ */\n+\n+#include <assert.h>\n+#include <cmath>\n+#include <stddef.h>\n+#include <stdio.h>\n+#include <stdlib.h>\n+\n+/*\n+ * We have observed the imx296 embedded data stream randomly return junk\n+ * reister values.  Do not rely on embedded data until this has been resolved.\n+ */\n+#define ENABLE_EMBEDDED_DATA 0\n+\n+#include \"cam_helper.hpp\"\n+#if ENABLE_EMBEDDED_DATA\n+#include \"md_parser.hpp\"\n+#endif\n+\n+//#include \"imx296_gain_table.hpp\"\n+\n+using namespace RPiController;\n+using libcamera::utils::Duration;\n+using namespace std::literals::chrono_literals;\n+\n+/*\n+ * We care about one gain register and a pair of exposure registers. Their I2C\n+ * addresses from the Sony IMX296 datasheet:\n+ */\n+constexpr uint32_t gainReg = 0x157;\n+constexpr uint32_t expHiReg = 0x15a;\n+constexpr uint32_t expLoReg = 0x15b;\n+constexpr std::initializer_list<uint32_t> registerList [[maybe_unused]] = { expHiReg, expLoReg, gainReg };\n+\n+class CamHelperImx296 : public CamHelper\n+{\n+public:\n+\tCamHelperImx296();\n+\tuint32_t GainCode(double gain) const override;\n+\tdouble Gain(uint32_t gain_code) const override;\n+\tuint32_t ExposureLines(Duration exposure) const override;\n+\tDuration Exposure(uint32_t exposure_lines) const override;\n+\tunsigned int MistrustFramesModeSwitch() const override;\n+\tbool SensorEmbeddedDataPresent() const override;\n+\n+private:\n+\tstatic constexpr uint32_t maxGainCode = 239;\n+\tstatic constexpr Duration timePerLine = 550.0 / 37.125e6 * 1.0s;\n+\n+\t/*\n+\t * Smallest difference between the frame length and integration time,\n+\t * in units of lines.\n+\t */\n+\tstatic constexpr int frameIntegrationDiff = 4;\n+\n+\tvoid PopulateMetadata(const MdParser::RegisterMap &registers,\n+\t\t\t      Metadata &metadata) const override;\n+};\n+\n+CamHelperImx296::CamHelperImx296()\n+#if ENABLE_EMBEDDED_DATA\n+\t: CamHelper(std::make_unique<MdParserSmia>(registerList), frameIntegrationDiff)\n+#else\n+\t: CamHelper(nullptr, frameIntegrationDiff)\n+#endif\n+{\n+}\n+\n+uint32_t CamHelperImx296::GainCode(double gain) const\n+{\n+\tuint32_t code = 20 * std::log10(gain) * 10;\n+\treturn std::min(code, maxGainCode);\n+}\n+\n+double CamHelperImx296::Gain(uint32_t gain_code) const\n+{\n+\treturn std::pow(10.0, gain_code / 200.0);\n+}\n+\n+uint32_t CamHelperImx296::ExposureLines(Duration exposure) const\n+{\n+\treturn (exposure - 14.26us) / timePerLine;\n+}\n+\n+Duration CamHelperImx296::Exposure(uint32_t exposure_lines) const\n+{\n+\treturn exposure_lines * timePerLine + 14.26us;\n+}\n+\n+unsigned int CamHelperImx296::MistrustFramesModeSwitch() const\n+{\n+\t/*\n+\t * For reasons unknown, we do occasionally get a bogus metadata frame\n+\t * at a mode switch (though not at start-up). Possibly warrants some\n+\t * investigation, though not a big deal.\n+\t */\n+\treturn 1;\n+}\n+\n+bool CamHelperImx296::SensorEmbeddedDataPresent() const\n+{\n+\treturn ENABLE_EMBEDDED_DATA;\n+}\n+\n+void CamHelperImx296::PopulateMetadata(const MdParser::RegisterMap &registers,\n+\t\t\t\t       Metadata &metadata) const\n+{\n+\tDeviceStatus deviceStatus;\n+\n+\tdeviceStatus.shutter_speed = Exposure(registers.at(expHiReg) * 256 + registers.at(expLoReg));\n+\tdeviceStatus.analogue_gain = Gain(registers.at(gainReg));\n+\n+\tmetadata.Set(\"device.status\", deviceStatus);\n+}\n+\n+static CamHelper *Create()\n+{\n+\treturn new CamHelperImx296();\n+}\n+\n+static RegisterCamHelper reg(\"imx296\", &Create);\ndiff --git a/src/ipa/raspberrypi/meson.build b/src/ipa/raspberrypi/meson.build\nindex 176055f42248..32897e07dad9 100644\n--- a/src/ipa/raspberrypi/meson.build\n+++ b/src/ipa/raspberrypi/meson.build\n@@ -21,6 +21,7 @@ rpi_ipa_sources = files([\n     'cam_helper_ov5647.cpp',\n     'cam_helper_imx219.cpp',\n     'cam_helper_imx290.cpp',\n+    'cam_helper_imx296.cpp',\n     'cam_helper_imx477.cpp',\n     'cam_helper_imx519.cpp',\n     'cam_helper_ov9281.cpp',\n","prefixes":["libcamera-devel","3/4"]}