Show a patch.

GET /api/patches/12552/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 12552,
    "url": "https://patchwork.libcamera.org/api/patches/12552/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/12552/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20210610145716.2630772-2-jeanmichel.hautbois@ideasonboard.com>",
    "date": "2021-06-10T14:57:15",
    "name": "[libcamera-devel,v2,1/2] ipa: Create a camera sensor helper class",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "9867068307ea207d29f93510583491f6d8beda25",
    "submitter": {
        "id": 75,
        "url": "https://patchwork.libcamera.org/api/people/75/?format=api",
        "name": "Jean-Michel Hautbois",
        "email": "jeanmichel.hautbois@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/12552/mbox/",
    "series": [
        {
            "id": 2118,
            "url": "https://patchwork.libcamera.org/api/series/2118/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2118",
            "date": "2021-06-10T14:57:14",
            "name": "libipa: Add support for a new camera sensor",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/2118/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/12552/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/12552/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 6F375C320B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 10 Jun 2021 14:57:22 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 63DCD6029C;\n\tThu, 10 Jun 2021 16:57:21 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 81BAE6029B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 10 Jun 2021 16:57:19 +0200 (CEST)",
            "from tatooine.ideasonboard.com (unknown\n\t[IPv6:2a01:e0a:169:7140:81ac:d9e4:203e:60a8])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3A63BE86;\n\tThu, 10 Jun 2021 16:57:19 +0200 (CEST)"
        ],
        "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=\"aBqeCIej\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1623337039;\n\tbh=csCOe4w1Ll4JhewjmB/w224Flgtq+F2TtO0GoN0BAGA=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=aBqeCIej9OyJKlUeG2lYgNNHKmxzvjzPj7G7v51FNnV00kBUsQJZ75kY7arAKi3Al\n\t2IYrU5r52c1lm46biFON9qMQJZuP71iTaQjonkZWpH31Sprb3odgBrOWE6vMD2VJmE\n\tf9ecsbtruYsYOAcnMmsGaz0znW/D4Kh3+uYjror4=",
        "From": "Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Thu, 10 Jun 2021 16:57:15 +0200",
        "Message-Id": "<20210610145716.2630772-2-jeanmichel.hautbois@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.30.2",
        "In-Reply-To": "<20210610145716.2630772-1-jeanmichel.hautbois@ideasonboard.com>",
        "References": "<20210610145716.2630772-1-jeanmichel.hautbois@ideasonboard.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v2 1/2] ipa: Create a camera sensor helper\n\tclass",
        "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": "Setting analogue gain for a specific sensor is not a straghtforward\noperation, as one needs to know how the gain is calculated for it.\n\nThis commit introduces a new camera sensor helper in libipa which aims\nto solve this specific issue.\nIt is based on the MIPI alliance Specification for Camera Command Set\nand implements, for now, only the analogue \"Global gain\" mode.\n\nIs makes it possible to only have 4 parameters to store per sensor, and\nthe gain calculation is then done identically for all of them. This\ncommit gives the gains for imx219 (based on RPi cam_helper), ov5670 and\nov5693.\n\nAdding a new sensor is pretty straightfoward as one only needs to\nimplement the sub-class for it and register that class to the\nCameraSensorHelperFactory.\n\nSigned-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n---\n src/ipa/libipa/camera_sensor_helper.cpp | 322 ++++++++++++++++++++++++\n src/ipa/libipa/camera_sensor_helper.h   |  89 +++++++\n src/ipa/libipa/meson.build              |   2 +\n 3 files changed, 413 insertions(+)\n create mode 100644 src/ipa/libipa/camera_sensor_helper.cpp\n create mode 100644 src/ipa/libipa/camera_sensor_helper.h",
    "diff": "diff --git a/src/ipa/libipa/camera_sensor_helper.cpp b/src/ipa/libipa/camera_sensor_helper.cpp\nnew file mode 100644\nindex 00000000..c2bb055a\n--- /dev/null\n+++ b/src/ipa/libipa/camera_sensor_helper.cpp\n@@ -0,0 +1,322 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * camera_sensor_helper.cpp - helper class providing camera information\n+ */\n+#include \"camera_sensor_helper.h\"\n+\n+#include <map>\n+\n+#include \"libcamera/internal/log.h\"\n+\n+/**\n+ * \\file camera_sensor_helper.h\n+ * \\brief Create helper class for each sensor\n+ *\n+ * Each camera sensor supported by libcamera may need specific informations to\n+ * be sent (analogue gain is sensor dependant for instance).\n+ *\n+ * Every subclass of CameraSensorHelper shall be registered with libipa using\n+ * the REGISTER_CAMERA_SENSOR_HELPER() macro.\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(CameraSensorHelper)\n+\n+namespace ipa {\n+\n+/**\n+ * \\class CameraSensorHelper\n+ * \\brief Create and give helpers for each camera sensor in the pipeline\n+ *\n+ * CameraSensorHelper instances are unique and sensor dependant.\n+ */\n+\n+/**\n+ * \\brief Construct a CameraSensorHelper instance\n+ * \\param[in] name The sensor model name\n+ *\n+ * The CameraSensorHelper instances shall never be constructed manually, but always\n+ * through the CameraSensorHelperFactory::create() method.\n+ */\n+\n+CameraSensorHelper::CameraSensorHelper()\n+{\n+}\n+\n+CameraSensorHelper::~CameraSensorHelper()\n+{\n+}\n+\n+/**\n+ * \\fn CameraSensorHelper::GainCode(double gain)\n+ * \\brief Get the analogue gain code to pass to V4L2 subdev control\n+ * \\param[in] gain The real gain to pass\n+ * \\return the gain code to pass to V4l2\n+ *\n+ * This function aims to abstract the calculation of the gain letting the IPA\n+ * use the real gain for its estimations.\n+ *\n+ * The parameters come from the MIPI Alliance Camera Specification for\n+ * Camera Command Set (CCS).\n+ */\n+uint32_t CameraSensorHelper::GainCode(double gain) const\n+{\n+\tASSERT((analogueGainConstants_.m0 == 0) || (analogueGainConstants_.m1 == 0));\n+\tASSERT(analogueGainConstants_.type == AnalogueGainLinear);\n+\t\n+\treturn (analogueGainConstants_.c0 - analogueGainConstants_.c1 * gain) /\n+\t       (analogueGainConstants_.m1 * gain - analogueGainConstants_.m0);\n+}\n+\n+/**\n+ * \\fn CameraSensorHelper::Gain\n+ * \\brief Get the real gain from the V4l2 subdev control gain\n+ * \\param[in] gainCode The V4l2 subdev control gain\n+ * \\return The real gain\n+ *\n+ * This function aims to abstract the calculation of the gain letting the IPA\n+ * use the real gain for its estimations. It is the counterpart of the function\n+ * CameraSensorHelper::getGainCode.\n+ *\n+ * The parameters come from the MIPI Alliance Camera Specification for\n+ * Camera Command Set (CCS).\n+ */\n+double CameraSensorHelper::Gain(uint32_t gainCode) const\n+{\n+\tASSERT((analogueGainConstants_.m0 == 0) || (analogueGainConstants_.m1 == 0));\n+\tASSERT(analogueGainConstants_.type == AnalogueGainLinear);\n+\n+\treturn (analogueGainConstants_.m0 * gainCode + analogueGainConstants_.c0) /\n+\t       (analogueGainConstants_.m1 * gainCode + analogueGainConstants_.c1);\n+}\n+\n+/**\n+ * \\enum CameraSensorHelper::AnalogueGainType\n+ * \\brief Specify the Gain mode supported by the sensor\n+ *\n+ * Describes the image sensor analog Gain capabilities.\n+ * Two modes are possible, depending on the sensor: Global and Alternate.\n+ */\n+\n+/**\n+ * \\var CameraSensorHelper::AnalogueGainLinear\n+ * \\brief Sensor supports linear gain\n+ *\n+ * The relationship between the integer Gain parameter and the resulting Gain\n+ * multiplier is given by the following equation:\n+ *\n+ * \\f$gain=\\frac{m0x+c0}{m1x+c1}\\f$\n+ *\n+ * Where 'x' is the gain control parameter, and m0, m1, c0 and c1 are\n+ * image-sensor-specific constants exposed by the sensor.\n+ * These constants are static parameters, and for any given image sensor either\n+ * m0 or m1 shall be zero.\n+ *\n+ * The full Gain equation therefore reduces to either:\n+ *\n+ * \\f$gain=\\frac{c0}{m1x+c1}\\f$ or \\f$\\frac{m0x+c0}{c1}\\f$\n+ */\n+\n+/**\n+ * \\var CameraSensorHelper::AnalogueGainExponential\n+ * \\brief Sensor supports exponential gain (introduced in CCS v1.1)\n+ *\n+ * Starting with CCS v1.1, Alternate Global Analog Gain is also available.\n+ * If the image sensor supports it, then the global analog Gain can be controlled\n+ * by linear and exponential gain formula:\n+ *\n+ * \\f$gain = analogLinearGainGlobal * 2^{analogExponentialGainGlobal}\\f$\n+ * \\todo not implemented in libipa\n+ */\n+\n+/**\n+ * \\struct CameraSensorHelper::AnalogueGainConstants\n+ * \\brief Analogue gain constants used for gain calculation\n+ */\n+\n+/**\n+ * \\var CameraSensorHelper::AnalogueGainConstants::type\n+ * \\brief Analogue gain coding type\n+ */\n+\n+/**\n+ * \\var CameraSensorHelper::AnalogueGainConstants::m0\n+ * \\brief Constant used in the analog Gain control coding/decoding\n+ *\n+ * Note: either m0 or m1 shall be zero.\n+ */\n+\n+/**\n+ * \\var CameraSensorHelper::AnalogueGainConstants::c0\n+ * \\brief Constant used in the analog Gain control coding/decoding\n+ */\n+\n+/**\n+ * \\var CameraSensorHelper::AnalogueGainConstants::m1\n+ * \\brief Constant used in the analog Gain control coding/decoding\n+ *\n+ * Note: either m0 or m1 shall be zero.\n+ */\n+\n+/**\n+ * \\var CameraSensorHelper::AnalogueGainConstants::c1\n+ * \\brief Constant used in the analog Gain control coding/decoding\n+ */\n+\n+/**\n+ * \\var CameraSensorHelper::analogueGainConstants_\n+ * \\brief The analogue gain parameters used for calculation\n+ *\n+ * The analogue gain is calculated through a formula, and its parameters are\n+ * sensor specific. Use this variable to store the values at init time.\n+ *\n+ */\n+\n+/**\n+ * \\class CameraSensorHelperFactory\n+ * \\brief Registration of CameraSensorHelperFactory classes and creation of instances\n+ *\n+ * To facilitate discovery and instantiation of CameraSensorHelper classes, the\n+ * CameraSensorHelperFactory class maintains a registry of camera sensor helper\n+ * classes. Each CameraSensorHelper subclass shall register itself using the\n+ * REGISTER_CAMERA_SENSOR_HELPER() macro, which will create a corresponding\n+ * instance of a CameraSensorHelperFactory subclass and register it with the\n+ * static list of factories.\n+ */\n+\n+/**\n+ * \\brief Construct a camera sensor helper factory\n+ * \\param[in] name Name of the camera sensor helper class\n+ *\n+ * Creating an instance of the factory registers it with the global list of\n+ * factories, accessible through the factories() function.\n+ *\n+ * The factory \\a name is used for debug purpose and shall be unique.\n+ */\n+\n+CameraSensorHelperFactory::CameraSensorHelperFactory(const std::string name)\n+\t: name_(name)\n+{\n+\tregisterType(this);\n+}\n+\n+/**\n+ * \\brief Create an instance of the CameraSensorHelper corresponding to the factory\n+ *\n+ * \\return A unique pointer to a new instance of the CameraSensorHelper subclass\n+ * corresponding to the factory\n+ */\n+std::unique_ptr<CameraSensorHelper> CameraSensorHelperFactory::create(const std::string &name)\n+{\n+\tstd::vector<CameraSensorHelperFactory *> &factories =\n+\t\tCameraSensorHelperFactory::factories();\n+\n+\tfor (CameraSensorHelperFactory *factory : factories) {\n+\t\tif (name != factory->name_)\n+\t\t\tcontinue;\n+\n+\t\tCameraSensorHelper *helper = factory->createInstance();\n+\t\treturn std::unique_ptr<CameraSensorHelper>(helper);\n+\t}\n+\treturn nullptr;\n+}\n+\n+/**\n+ * \\brief Add a camera sensor helper class to the registry\n+ * \\param[in] factory Factory to use to construct the camera sensor helper\n+ *\n+ * The caller is responsible to guarantee the uniqueness of the camera sensor helper\n+ * name.\n+ */\n+void CameraSensorHelperFactory::registerType(CameraSensorHelperFactory *factory)\n+{\n+\tstd::vector<CameraSensorHelperFactory *> &factories = CameraSensorHelperFactory::factories();\n+\n+\tfactories.push_back(factory);\n+}\n+\n+/**\n+ * \\brief Retrieve the list of all camera sensor helper factories\n+ *\n+ * The static factories map is defined inside the function to ensures it gets\n+ * initialized on first use, without any dependency on link order.\n+ *\n+ * \\return The list of camera sensor helper factories\n+ */\n+std::vector<CameraSensorHelperFactory *> &CameraSensorHelperFactory::factories()\n+{\n+\tstatic std::vector<CameraSensorHelperFactory *> factories;\n+\treturn factories;\n+}\n+\n+/**\n+ * \\fn CameraSensorHelperFactory::createInstance()\n+ * \\brief Create an instance of the CameraSensorHelper corresponding to the factory\n+ *\n+ * This virtual function is implemented by the REGISTER_CAMERA_SENSOR_HELPER()\n+ * macro. It creates a camera sensor helper instance associated with the camera\n+ * sensor model.\n+ *\n+ * \\return A pointer to a newly constructed instance of the CameraSensorHelper\n+ * subclass corresponding to the factory\n+ */\n+\n+/**\n+ * \\def REGISTER_CAMERA_SENSOR_HELPER\n+ * \\brief Register a camera sensor helper with the camera sensor helper factory\n+ * \\param[in] name Sensor model name used to register the class\n+ * \\param[in] helper Class name of CameraSensorHelper derived class to register\n+ *\n+ * Register a CameraSensorHelper subclass with the factory and make it available to\n+ * try and match devices.\n+ */\n+\n+/**\n+ * \\class CameraSensorHelperImx219\n+ * \\brief Create and give helpers for the imx219 sensor\n+ */\n+class CameraSensorHelperImx219 : public CameraSensorHelper\n+{\n+public:\n+\tCameraSensorHelperImx219()\n+\t{\n+\t\tanalogueGainConstants_ = { AnalogueGainLinear, 0, -1, 256, 256 };\n+\t}\n+};\n+REGISTER_CAMERA_SENSOR_HELPER(\"imx219\", CameraSensorHelperImx219)\n+\n+/**\n+ * \\class CameraSensorHelperOv5670\n+ * \\brief Create and give helpers for the ov5670 sensor\n+ */\n+class CameraSensorHelperOv5670 : public CameraSensorHelper\n+{\n+public:\n+\tCameraSensorHelperOv5670()\n+\t{\n+\t\tanalogueGainConstants_ = { AnalogueGainLinear, 1, 0, 0, 256 };\n+\t}\n+};\n+REGISTER_CAMERA_SENSOR_HELPER(\"ov5670\", CameraSensorHelperOv5670)\n+\n+/**\n+ * \\class CameraSensorHelperOv5693\n+ * \\brief Create and give helpers for the ov5693 sensor\n+ */\n+class CameraSensorHelperOv5693 : public CameraSensorHelper\n+{\n+public:\n+\tCameraSensorHelperOv5693()\n+\t{\n+\t\tanalogueGainConstants_ = { AnalogueGainLinear, 1, 0, 0, 16 };\n+\t}\n+};\n+REGISTER_CAMERA_SENSOR_HELPER(\"ov5693\", CameraSensorHelperOv5693)\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\n+\ndiff --git a/src/ipa/libipa/camera_sensor_helper.h b/src/ipa/libipa/camera_sensor_helper.h\nnew file mode 100644\nindex 00000000..976f8d33\n--- /dev/null\n+++ b/src/ipa/libipa/camera_sensor_helper.h\n@@ -0,0 +1,89 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * camera_sensor_helper.h - helper class providing camera information\n+ */\n+#ifndef __LIBCAMERA_IPA_LIBIPA_CAMERA_SENSOR_HELPER_H__\n+#define __LIBCAMERA_IPA_LIBIPA_CAMERA_SENSOR_HELPER_H__\n+\n+#include <stdint.h>\n+\n+#include <string>\n+\n+#include <libcamera/class.h>\n+#include <libcamera/object.h>\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+class CameraSensorHelper;\n+\n+class CameraSensorHelper\n+{\n+public:\n+\tCameraSensorHelper();\n+\tvirtual ~CameraSensorHelper();\n+\n+\tuint32_t GainCode(double gain) const;\n+\tdouble Gain(uint32_t gainCode) const;\n+\n+protected:\n+\tenum AnalogueGainType {\n+\t\tAnalogueGainLinear = 0,\n+\t\tAnalogueGainExponential = 2,\n+\t};\n+\n+\tstruct AnalogueGainConstants {\n+\t\tuint16_t type;\n+\t\tint16_t m0;\n+\t\tint16_t c0;\n+\t\tint16_t m1;\n+\t\tint16_t c1;\n+\t};\n+\n+\tAnalogueGainConstants analogueGainConstants_;\n+\n+private:\n+\tLIBCAMERA_DISABLE_COPY_AND_MOVE(CameraSensorHelper)\n+};\n+\n+class CameraSensorHelperFactory\n+{\n+public:\n+\tCameraSensorHelperFactory(const std::string name);\n+\tvirtual ~CameraSensorHelperFactory() = default;\n+\n+\tstatic std::unique_ptr<CameraSensorHelper> create(const std::string &name);\n+\n+\tstatic void registerType(CameraSensorHelperFactory *factory);\n+\tstatic std::vector<CameraSensorHelperFactory *> &factories();\n+\n+private:\n+\tLIBCAMERA_DISABLE_COPY_AND_MOVE(CameraSensorHelperFactory)\n+\tvirtual CameraSensorHelper *createInstance() = 0;\n+\n+\tstd::string name_;\n+};\n+\n+#define REGISTER_CAMERA_SENSOR_HELPER(name, helper)               \\\n+class helper##Factory final : public CameraSensorHelperFactory    \\\n+{                                                                 \\\n+public:                                                           \\\n+\thelper##Factory() : CameraSensorHelperFactory(name) {} \\\n+\t\t\t\t\t\t\t\t  \\\n+private:                                                          \\\n+\tCameraSensorHelper *createInstance()                      \\\n+\t{                                                         \\\n+\t\treturn new helper();                          \\\n+\t}                                                         \\\n+};                                                                \\\n+static helper##Factory global_##helper##Factory;\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_IPA_LIBIPA_CAMERA_SENSOR_HELPER_H__ */\n+\ndiff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex 038fc490..dc90542c 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -2,11 +2,13 @@\n \n libipa_headers = files([\n     'algorithm.h',\n+    'camera_sensor_helper.h',\n     'histogram.h'\n ])\n \n libipa_sources = files([\n     'algorithm.cpp',\n+    'camera_sensor_helper.cpp',\n     'histogram.cpp'\n ])\n \n",
    "prefixes": [
        "libcamera-devel",
        "v2",
        "1/2"
    ]
}