From patchwork Sun Dec 19 23:27:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 15169 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 5CF1DC3258 for ; Sun, 19 Dec 2021 23:27:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 00221608A2; Mon, 20 Dec 2021 00:27:25 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="F0EO+E/Y"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 11702605A7 for ; Mon, 20 Dec 2021 00:27:22 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 869F011CD; Mon, 20 Dec 2021 00:27:21 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1639956441; bh=pBmnSKjSwQVS3Fek/2ep4ISkZFRrg0Z6zru5iYxIUWI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F0EO+E/YWXKnOv7r+SjsIWP7OmmSEzQvGBWvMRHhxBue35ubnv+/NDhhsleYNrnZB /ZQLhZbyyOXj4fkFBTOWODjoKtqKbK663XgAsdGw9U7mGKNKgSsy2Oi5Wk9D6ACTHY DDZzOBz99NUOxi13IgO0+8NHF3WQC8pJ2uCQ1ytg= From: Laurent Pinchart 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 Subject: [libcamera-devel] [PATCH 3/4] ipa: raspberrypi: Add camera helper for Sony IMX296 sensor X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Naushir Patuck The Sony IMX296 sensor has an exponential gain model, and adds a fixed 14.26µs offset to the exposure time expressed in line units. Signed-off-by: Laurent Pinchart Signed-off-by: Naushir Patuck @raspberrypi.com> Reviewed-by: Naushir Patuck @raspberrypi.com> --- src/ipa/raspberrypi/cam_helper_imx296.cpp | 126 ++++++++++++++++++++++ src/ipa/raspberrypi/meson.build | 1 + 2 files changed, 127 insertions(+) create mode 100644 src/ipa/raspberrypi/cam_helper_imx296.cpp diff --git a/src/ipa/raspberrypi/cam_helper_imx296.cpp b/src/ipa/raspberrypi/cam_helper_imx296.cpp new file mode 100644 index 000000000000..9c34582861f1 --- /dev/null +++ b/src/ipa/raspberrypi/cam_helper_imx296.cpp @@ -0,0 +1,126 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2020, Raspberry Pi (Trading) Limited + * + * cam_helper_imx296.cpp - camera helper for imx296 sensor + */ + +#include +#include +#include +#include +#include + +/* + * We have observed the imx296 embedded data stream randomly return junk + * reister values. Do not rely on embedded data until this has been resolved. + */ +#define ENABLE_EMBEDDED_DATA 0 + +#include "cam_helper.hpp" +#if ENABLE_EMBEDDED_DATA +#include "md_parser.hpp" +#endif + +//#include "imx296_gain_table.hpp" + +using namespace RPiController; +using libcamera::utils::Duration; +using namespace std::literals::chrono_literals; + +/* + * We care about one gain register and a pair of exposure registers. Their I2C + * addresses from the Sony IMX296 datasheet: + */ +constexpr uint32_t gainReg = 0x157; +constexpr uint32_t expHiReg = 0x15a; +constexpr uint32_t expLoReg = 0x15b; +constexpr std::initializer_list registerList [[maybe_unused]] = { expHiReg, expLoReg, gainReg }; + +class CamHelperImx296 : public CamHelper +{ +public: + CamHelperImx296(); + uint32_t GainCode(double gain) const override; + double Gain(uint32_t gain_code) const override; + uint32_t ExposureLines(Duration exposure) const override; + Duration Exposure(uint32_t exposure_lines) const override; + unsigned int MistrustFramesModeSwitch() const override; + bool SensorEmbeddedDataPresent() const override; + +private: + static constexpr uint32_t maxGainCode = 239; + static constexpr Duration timePerLine = 550.0 / 37.125e6 * 1.0s; + + /* + * Smallest difference between the frame length and integration time, + * in units of lines. + */ + static constexpr int frameIntegrationDiff = 4; + + void PopulateMetadata(const MdParser::RegisterMap ®isters, + Metadata &metadata) const override; +}; + +CamHelperImx296::CamHelperImx296() +#if ENABLE_EMBEDDED_DATA + : CamHelper(std::make_unique(registerList), frameIntegrationDiff) +#else + : CamHelper(nullptr, frameIntegrationDiff) +#endif +{ +} + +uint32_t CamHelperImx296::GainCode(double gain) const +{ + uint32_t code = 20 * std::log10(gain) * 10; + return std::min(code, maxGainCode); +} + +double CamHelperImx296::Gain(uint32_t gain_code) const +{ + return std::pow(10.0, gain_code / 200.0); +} + +uint32_t CamHelperImx296::ExposureLines(Duration exposure) const +{ + return (exposure - 14.26us) / timePerLine; +} + +Duration CamHelperImx296::Exposure(uint32_t exposure_lines) const +{ + return exposure_lines * timePerLine + 14.26us; +} + +unsigned int CamHelperImx296::MistrustFramesModeSwitch() const +{ + /* + * For reasons unknown, we do occasionally get a bogus metadata frame + * at a mode switch (though not at start-up). Possibly warrants some + * investigation, though not a big deal. + */ + return 1; +} + +bool CamHelperImx296::SensorEmbeddedDataPresent() const +{ + return ENABLE_EMBEDDED_DATA; +} + +void CamHelperImx296::PopulateMetadata(const MdParser::RegisterMap ®isters, + Metadata &metadata) const +{ + DeviceStatus deviceStatus; + + deviceStatus.shutter_speed = Exposure(registers.at(expHiReg) * 256 + registers.at(expLoReg)); + deviceStatus.analogue_gain = Gain(registers.at(gainReg)); + + metadata.Set("device.status", deviceStatus); +} + +static CamHelper *Create() +{ + return new CamHelperImx296(); +} + +static RegisterCamHelper reg("imx296", &Create); diff --git a/src/ipa/raspberrypi/meson.build b/src/ipa/raspberrypi/meson.build index 176055f42248..32897e07dad9 100644 --- a/src/ipa/raspberrypi/meson.build +++ b/src/ipa/raspberrypi/meson.build @@ -21,6 +21,7 @@ rpi_ipa_sources = files([ 'cam_helper_ov5647.cpp', 'cam_helper_imx219.cpp', 'cam_helper_imx290.cpp', + 'cam_helper_imx296.cpp', 'cam_helper_imx477.cpp', 'cam_helper_imx519.cpp', 'cam_helper_ov9281.cpp',