Show a patch.

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

{
    "id": 21412,
    "url": "https://patchwork.libcamera.org/api/patches/21412/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/21412/",
    "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": "<01020192340ab214-3d46266e-b780-407e-941f-8f8d5a841007-000000@eu-west-1.amazonses.com>",
    "date": "2024-09-27T15:12:49",
    "name": "[v4] libcamera: pipeline: uvcvideo: Map focus controls",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "ff2c36eb2c7e5924e3b55e015c8cf95625f7ffb4",
    "submitter": {
        "id": 207,
        "url": "https://patchwork.libcamera.org/api/people/207/?format=api",
        "name": "Cláudio Paulo",
        "email": "claudio.paulo@makewise.pt"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/21412/mbox/",
    "series": [
        {
            "id": 4635,
            "url": "https://patchwork.libcamera.org/api/series/4635/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4635",
            "date": "2024-09-27T15:12:49",
            "name": "[v4] libcamera: pipeline: uvcvideo: Map focus controls",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/4635/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/21412/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/21412/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 2B618C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 27 Sep 2024 15:12:54 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 020026350F;\n\tFri, 27 Sep 2024 17:12:53 +0200 (CEST)",
            "from a7-18.smtp-out.eu-west-1.amazonses.com\n\t(a7-18.smtp-out.eu-west-1.amazonses.com [54.240.7.18])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B2007634F2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 27 Sep 2024 17:12:50 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (2048-bit key;\n\tunprotected) header.d=makewise.pt header.i=@makewise.pt\n\theader.b=\"b4jfirpV\"; dkim=pass (1024-bit key;\n\tunprotected) header.d=amazonses.com header.i=@amazonses.com\n\theader.b=\"eUv6/bHh\"; dkim-atps=neutral",
        "DKIM-Signature": [
            "v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple;\n\ts=4ymqmigrodjwq5luewph2eedidgwlpqq; d=makewise.pt; t=1727449969;\n\th=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type:Content-Transfer-Encoding;\n\tbh=fv4guwdRbWMWTLVxG7Nu2PNb9x5IRezKZuInNz8fXFA=;\n\tb=b4jfirpVFBzsO2ayDNk0i1M/5rq7CDpFS66GKQ4KO8EH7XMu4lbGCOMWgfseVSva\n\tFkCyjd5R4zfAIRZuz2l4Hn+J8gpCvZx65inVStRs9PhlIhHEovitdetaGG54oizIOTo\n\t6ycZQrA9Jg4K2Szp9dVLHNOCUg88Ma9kSp5xQJwAYd2G0snswkmstCiyLKwN4KgR+tV\n\tRW8DGVDlgUrL9Xv8iopAe3hU6bgFBXaCO2QJcJ5Frk/fIbXpKZuH/oE6PwfDakr/KQ+\n\tye5XVz/qxOVYZwiz/oseQPCBBcQ+GiC2301TTJYvQ7zrsdBSby4cqwDfwAhVJ79LrgC\n\tYg/hNHc5PQ==",
            "v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple;\n\ts=uku4taia5b5tsbglxyj6zym32efj7xqv; d=amazonses.com; t=1727449969;\n\th=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type:Content-Transfer-Encoding:Feedback-ID;\n\tbh=fv4guwdRbWMWTLVxG7Nu2PNb9x5IRezKZuInNz8fXFA=;\n\tb=eUv6/bHhg5PQp5U000sP72rovypl6CXSxcQqT5T3+81QJvlUSpY5ZmH2YuZUeLLJ\n\td5HSnxnNFIFCEC3rpgYHwjXR30KwhvmCnwyOYVM0fwXlQUPWcVYc8ekSwytBeo0uj+b\n\tj5soiju2cb8bNAUOoAleCnjiR3VBcCdyxjfiKsV8="
        ],
        "From": "=?utf-8?q?Cl=C3=A1udio_Paulo?= <claudio.paulo@makewise.pt>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "=?utf-8?q?Cl=C3=A1udio_Paulo?= <claudio.paulo@makewise.pt>",
        "Subject": "[PATCH v4] libcamera: pipeline: uvcvideo: Map focus controls",
        "Date": "Fri, 27 Sep 2024 15:12:49 +0000",
        "Message-ID": "<01020192340ab214-3d46266e-b780-407e-941f-8f8d5a841007-000000@eu-west-1.amazonses.com>",
        "X-Mailer": "git-send-email 2.34.1",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "Feedback-ID": "::1.eu-west-1.z8xjvhxVxUk+kM5mx7i/KMS/a2avhcAXFe/noI67NVQ=:AmazonSES",
        "X-SES-Outgoing": "2024.09.27-54.240.7.18",
        "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": "Added mapping of controls::LensPosition and controls::AfMode to\nv4l2 controls V4L2_CID_FOCUS_ABSOLUTE and V4L2_CID_FOCUS_AUTO\nrespectively when the device supports them.\n\nIf not supported, they are not registered.\n\nSigned-off-by: Cláudio Paulo <claudio.paulo@makewise.pt>\n---\nFixed issues raised in review.\n\n src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 107 +++++++++++++++++--\n 1 file changed, 100 insertions(+), 7 deletions(-)",
    "diff": "diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\nindex 6b32fa18..3a0d6ab6 100644\n--- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n+++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n@@ -6,9 +6,9 @@\n  */\n \n #include <algorithm>\n+#include <cmath>\n #include <fstream>\n #include <map>\n-#include <math.h>\n #include <memory>\n #include <set>\n #include <string>\n@@ -304,13 +304,33 @@ int PipelineHandlerUVC::processControl(ControlList *controls, unsigned int id,\n \t\tcid = V4L2_CID_EXPOSURE_ABSOLUTE;\n \telse if (id == controls::AnalogueGain)\n \t\tcid = V4L2_CID_GAIN;\n+\telse if (id == controls::LensPosition)\n+\t\tcid = V4L2_CID_FOCUS_ABSOLUTE;\n+\telse if (id == controls::AfMode)\n+\t\tcid = V4L2_CID_FOCUS_AUTO;\n \telse\n \t\treturn -EINVAL;\n \n+\t/* Check if the device supports this control. */\n+\tif (controls->idMap()->find(cid) == controls->idMap()->end())\n+\t\treturn -EINVAL;\n+\n \tconst ControlInfo &v4l2Info = controls->infoMap()->at(cid);\n-\tint32_t min = v4l2Info.min().get<int32_t>();\n-\tint32_t def = v4l2Info.def().get<int32_t>();\n-\tint32_t max = v4l2Info.max().get<int32_t>();\n+\n+\tint32_t min, def, max;\n+\tif (v4l2Info.min().type() == ControlTypeInteger32) {\n+\t\tmin = v4l2Info.min().get<int32_t>();\n+\t\tdef = v4l2Info.def().get<int32_t>();\n+\t\tmax = v4l2Info.max().get<int32_t>();\n+\t} else if (v4l2Info.min().type() == ControlTypeBool) {\n+\t\tmin = v4l2Info.min().get<bool>();\n+\t\tdef = v4l2Info.def().get<bool>();\n+\t\tmax = v4l2Info.max().get<bool>();\n+\t} else {\n+\t\tLOG(UVC, Error) << \"v4l2 ControlInfo with value type \"\n+\t\t\t\t<< v4l2Info.min().type() << \" not mapped\";\n+\t\treturn -EINVAL;\n+\t}\n \n \t/*\n \t * See UVCCameraData::addControl() for explanations of the different\n@@ -358,6 +378,21 @@ int PipelineHandlerUVC::processControl(ControlList *controls, unsigned int id,\n \t\tbreak;\n \t}\n \n+\tcase V4L2_CID_FOCUS_ABSOLUTE: {\n+\t\tfloat focusedAt50Cm = 0.15f * (max - min);\n+\t\tfloat scale = focusedAt50Cm / 2.0f;\n+\n+\t\tfloat fvalue = value.get<float>() * scale + min;\n+\t\tcontrols->set(cid, static_cast<int32_t>(std::lround(fvalue)));\n+\t\tbreak;\n+\t}\n+\n+\tcase V4L2_CID_FOCUS_AUTO: {\n+\t\tint32_t ivalue = value.get<int32_t>() != controls::AfModeManual;\n+\t\tcontrols->set(cid, ivalue);\n+\t\tbreak;\n+\t}\n+\n \tdefault: {\n \t\tint32_t ivalue = value.get<int32_t>();\n \t\tcontrols->set(cid, ivalue);\n@@ -655,14 +690,31 @@ void UVCCameraData::addControl(uint32_t cid, const ControlInfo &v4l2Info,\n \tcase V4L2_CID_GAIN:\n \t\tid = &controls::AnalogueGain;\n \t\tbreak;\n+\tcase V4L2_CID_FOCUS_ABSOLUTE:\n+\t\tid = &controls::LensPosition;\n+\t\tbreak;\n+\tcase V4L2_CID_FOCUS_AUTO:\n+\t\tid = &controls::AfMode;\n+\t\tbreak;\n \tdefault:\n \t\treturn;\n \t}\n \n \t/* Map the control info. */\n-\tint32_t min = v4l2Info.min().get<int32_t>();\n-\tint32_t max = v4l2Info.max().get<int32_t>();\n-\tint32_t def = v4l2Info.def().get<int32_t>();\n+\tint32_t min, def, max;\n+\tif (v4l2Info.min().type() == ControlTypeInteger32) {\n+\t\tmin = v4l2Info.min().get<int32_t>();\n+\t\tdef = v4l2Info.def().get<int32_t>();\n+\t\tmax = v4l2Info.max().get<int32_t>();\n+\t} else if (v4l2Info.min().type() == ControlTypeBool) {\n+\t\tmin = v4l2Info.min().get<bool>();\n+\t\tdef = v4l2Info.def().get<bool>();\n+\t\tmax = v4l2Info.max().get<bool>();\n+\t} else {\n+\t\tLOG(UVC, Error) << \"v4l2 ControlInfo with value type \"\n+\t\t\t\t<< v4l2Info.min().type() << \" not mapped\";\n+\t\treturn;\n+\t}\n \n \tswitch (cid) {\n \tcase V4L2_CID_BRIGHTNESS: {\n@@ -739,6 +791,47 @@ void UVCCameraData::addControl(uint32_t cid, const ControlInfo &v4l2Info,\n \t\tbreak;\n \t}\n \n+\tcase V4L2_CID_FOCUS_ABSOLUTE: {\n+\t\t/*\n+\t\t * LensPosition expects values to be in the range of\n+\t\t * [0.0f, maxDioptres], while a value of 0 means focused to\n+\t\t * infinity, 0.5 means focused at 2m, 2 means focused at 50cm\n+\t\t * and maxDioptres will be the closest possible focus which\n+\t\t * will equate to a focus distance of (1 / maxDioptres) metres.\n+\t\t *\n+\t\t * \\todo These values will definitely vary for each different\n+\t\t * hardware, so this should be tunable somehow. In this case\n+\t\t * 0.15f (~= 150 / (1023 - 1)) was a value read from\n+\t\t * V4L2_CID_FOCUS_ABSOLUTE control when using a random camera\n+\t\t * and having it autofocus at a distance of about 50cm.\n+\t\t * This means the minimum focus distance of this camera should\n+\t\t * be 75mm (0.15 / 2) which equals a maxDioptres value of ~=\n+\t\t * 13.3333 (2 / 0.15). Also, the values might not scale\n+\t\t * linearly, but this implementation assumes they do.\n+\t\t */\n+\t\tfloat focusedAt50Cm = 0.15f * (max - min);\n+\t\tfloat scale = 2.0f / focusedAt50Cm;\n+\n+\t\tinfo = ControlInfo{\n+\t\t\t{ 0.0f },\n+\t\t\t{ (max - min) * scale },\n+\t\t\t{ (def - min) * scale }\n+\t\t};\n+\t\tbreak;\n+\t}\n+\n+\tcase V4L2_CID_FOCUS_AUTO: {\n+\t\tstd::vector<ControlValue> values;\n+\n+\t\tif (!min || !max)\n+\t\t\tvalues.emplace_back(static_cast<int32_t>(controls::AfModeManual));\n+\t\tif (min || max)\n+\t\t\tvalues.emplace_back(static_cast<int32_t>(controls::AfModeContinuous));\n+\n+\t\tinfo = ControlInfo{ values, values.back() };\n+\t\tbreak;\n+\t}\n+\n \tdefault:\n \t\tinfo = v4l2Info;\n \t\tbreak;\n",
    "prefixes": [
        "v4"
    ]
}