From patchwork Mon Jun 3 23:03:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 20197 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 EB0F9BD87C for ; Mon, 3 Jun 2024 23:03:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 07500634C5; Tue, 4 Jun 2024 01:03:19 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UjBf5IV+"; 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 59BA7634B2 for ; Tue, 4 Jun 2024 01:03:17 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D11FF16D4 for ; Tue, 4 Jun 2024 01:03:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1717455790; bh=NIneKZ/DxZgol2JzpgMTqt13+r3JhDNk5zYZxO0rdqA=; h=From:To:Subject:Date:From; b=UjBf5IV+eexorJ40z2NRc2pY8ErqXPGbvxuyFhJSYVOzZREofgrvN5sppaZOXbXll 5JfwdJKvtmRTqhEH3xHr34Uigg5mmuUP2/B4hnCws7nCU4FdUs2zyfwayI2Y1aoVyU 3SD2JFkt6wFzi6Vs9U/qCRArjquhFm1Fkg1PN1hM= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [RFC/PATCH] libcamera: libipa: camera_sensor: Add onsemi AR0144 sensor properties Date: Tue, 4 Jun 2024 02:03:00 +0300 Message-ID: <20240603230301.26550-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.44.2 MIME-Version: 1.0 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" Provide the onsemi AR0144 camera sensor properties and registration with libipa for the gain code helpers. Signed-off-by: Laurent Pinchart --- I'm working on a driver for the AR0144 camera sensor, which implements an exotic gain model. While I would normally post the kernel driver before submitting the libcamera integration patch, I thought reversing the order would be useful for discussion purpose, as we recently resurected the topic of gain rounding (see https://patchwork.libcamera.org/cover/20097/). The approach I've taken here is to ensure that the code -> gain -> code roundtrip conversion always results to the same gain code, for all valid gain codes. To achieve this, I had to increase the multiple m in the gain() function by the machine epsilon. In theory increasing other factors and terms in the formula may have been needed, but this proved enough in practice by running the roundtrip conversion on all possible code values. One challenge to test this in unit tests is that the valid gain code ranges is not contiguous. The sensor rounds the fine gain differently depending on the coarse gain, which creates holes in the range. It would be feasible (and is considered) to extend the helpers to expose the [min, max] range of valid codes, but exposing arbitrary holes is more problematic. One option would be to specify that the gain() function should return NaN when the gain code is invalid. To make this safe, we would need to prove that the gainCode() function will never an invalid gain code, ideally formally but possibly by brute force. --- src/ipa/libipa/camera_sensor_helper.cpp | 87 +++++++++++++++++++ .../sensor/camera_sensor_properties.cpp | 9 ++ 2 files changed, 96 insertions(+) base-commit: 6cd17515ffeb67fb38ffcc4d57aadf9732b54800 diff --git a/src/ipa/libipa/camera_sensor_helper.cpp b/src/ipa/libipa/camera_sensor_helper.cpp index 782ff9904e81..c2be011300b1 100644 --- a/src/ipa/libipa/camera_sensor_helper.cpp +++ b/src/ipa/libipa/camera_sensor_helper.cpp @@ -8,6 +8,7 @@ #include "camera_sensor_helper.h" #include +#include #include @@ -366,6 +367,92 @@ static constexpr double expGainDb(double step) return log2_10 * step / 20; } +class CameraSensorHelperAr0144 : public CameraSensorHelper +{ +public: + uint32_t gainCode(double gain) const override + { + /* The recommended minimum gain is 1.6842 to avoid artifacts. */ + gain = std::clamp(gain, 1.0 / (1.0 - 13.0 / 32.0), 18.45); + + /* + * The analogue gain is made of a coarse exponential gain in + * the range [2^0, 2^4] and a fine inversely linear gain in the + * range [1.0, 2.0[. There is an additional fixed 1.153125 + * multiplier when the coarse gain reaches 2^2. + */ + + if (gain > 4.0) + gain /= 1.153125; + + unsigned int coarse = std::log2(gain); + unsigned int fine = (1 - (1 << coarse) / gain) * 32; + + /* The fine gain rounding depends on the coarse gain. */ + if (coarse == 1 || coarse == 3) + fine &= ~1; + else if (coarse == 4) + fine &= ~3; + + return (coarse << 4) | (fine & 0xf); + } + + double gain(uint32_t gainCode) const override + { + unsigned int coarse = gainCode >> 4; + unsigned int fine = gainCode & 0xf; + unsigned int d1; + double d2, m; + + switch (coarse) { + case 0: + d1 = 1; + d2 = 32.0; + m = 1.0; + break; + case 1: + d1 = 2; + d2 = 16.0; + m = 1.0; + break; + case 2: + d1 = 1; + d2 = 32.0; + m = 1.153125; + break; + case 3: + d1 = 2; + d2 = 16.0; + m = 1.153125; + break; + case 4: + d1 = 4; + d2 = 8.0; + m = 1.153125; + break; + } + + /* + * With infinite precision, the calculated gain would be exact, + * and the reverse conversion with gainCode() would produce the + * same gain code. In the real world, rounding errors may cause + * the calculated gain to be lower by an amount negligible for + * all purposes, except for the reverse conversion. Converting + * the gain to a gain code could then return the quantized value + * just lower than the original gain code. To avoid this, tests + * showed that adding the machine epsilon to the multiplier m is + * sufficient. + */ + m += std::numeric_limits::epsilon(); + + return m * (1 << coarse) / (1.0 - (fine / d1) / d2); + } + +private: + static constexpr double kStep_ = 16; +}; +REGISTER_CAMERA_SENSOR_HELPER("ar0144", CameraSensorHelperAr0144) + class CameraSensorHelperAr0521 : public CameraSensorHelper { public: diff --git a/src/libcamera/sensor/camera_sensor_properties.cpp b/src/libcamera/sensor/camera_sensor_properties.cpp index b18524d85b37..4e5217ab51ef 100644 --- a/src/libcamera/sensor/camera_sensor_properties.cpp +++ b/src/libcamera/sensor/camera_sensor_properties.cpp @@ -52,6 +52,15 @@ LOG_DEFINE_CATEGORY(CameraSensorProperties) const CameraSensorProperties *CameraSensorProperties::get(const std::string &sensor) { static const std::map sensorProps = { + { "ar0144", { + .unitCellSize = { 3000, 3000 }, + .testPatternModes = { + { controls::draft::TestPatternModeOff, 0 }, + { controls::draft::TestPatternModeSolidColor, 1 }, + { controls::draft::TestPatternModeColorBars, 2 }, + { controls::draft::TestPatternModeColorBarsFadeToGray, 3 }, + }, + } }, { "ar0521", { .unitCellSize = { 2200, 2200 }, .testPatternModes = {