{"id":21267,"url":"https://patchwork.libcamera.org/api/patches/21267/?format=json","web_url":"https://patchwork.libcamera.org/patch/21267/","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":"<01020191f288fe5f-fb094635-2cc7-4e76-8492-a42e0b6978e3-000000@eu-west-1.amazonses.com>","date":"2024-09-14T21:55:49","name":"[v2] libcamera: pipeline: uvcvideo: Map focus controls","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"3da7863bc4f7e96f120abccb79606471ee48d546","submitter":{"id":207,"url":"https://patchwork.libcamera.org/api/people/207/?format=json","name":"Cláudio Paulo","email":"claudio.paulo@makewise.pt"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21267/mbox/","series":[{"id":4594,"url":"https://patchwork.libcamera.org/api/series/4594/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4594","date":"2024-09-14T21:55:49","name":"[v2] libcamera: pipeline: uvcvideo: Map focus controls","version":2,"mbox":"https://patchwork.libcamera.org/series/4594/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21267/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21267/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 14BB2C324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat, 14 Sep 2024 21:55:54 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D801E634FC;\n\tSat, 14 Sep 2024 23:55:52 +0200 (CEST)","from a7-10.smtp-out.eu-west-1.amazonses.com\n\t(a7-10.smtp-out.eu-west-1.amazonses.com [54.240.7.10])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id EA8CD618F6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 14 Sep 2024 23:55: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=\"R1gdLW3X\"; dkim=pass (1024-bit key;\n\tunprotected) header.d=amazonses.com header.i=@amazonses.com\n\theader.b=\"Vo8VMTJA\"; dkim-atps=neutral","DKIM-Signature":["v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple;\n\ts=4ymqmigrodjwq5luewph2eedidgwlpqq; d=makewise.pt; t=1726350950;\n\th=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type:Content-Transfer-Encoding;\n\tbh=xF6krJ89uqGIlwL2dTBlR0Kqqx7t5lgn11B6INaGTOw=;\n\tb=R1gdLW3X69sX4JwCDs1RLGmIX2DCq2pRYMheg6Mui6frsoweo90TVp1SnIFqKEy/\n\tZ3DWZgklBY6Fs5AaT1EivNQkenfpnHd070Ohi2w6PtzkehFhm2eBbwVnpKfe547enrC\n\tlar33QYfp4dsx/uQJbVBi3qt6dVgZhuby0BlY2w5lmATQB+9v4tvtZYyTOIl3ImKo8o\n\tlUetHKlH4tv+Fz5HR9w+lOexnd1MiuxXnakLtPVj+AvtDdIq8G4B7+qCqAJC4AcItut\n\tgSR9mlhRNyHkPqH2I1XWAsVsYopej5vzHEweDYHV9/Hmc503SQPJkSFJlgIzMBV0gGI\n\tfkD1wEKpYQ==","v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple;\n\ts=uku4taia5b5tsbglxyj6zym32efj7xqv; d=amazonses.com; t=1726350950;\n\th=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type:Content-Transfer-Encoding:Feedback-ID;\n\tbh=xF6krJ89uqGIlwL2dTBlR0Kqqx7t5lgn11B6INaGTOw=;\n\tb=Vo8VMTJAF5Hz3JCHfIwwFDrm38sqU780b9ytPFIS+A27VbdQeDSp35ciyWR5sZza\n\tYOsGUb/Uxmc9UP631IFQoapkutIwEJ3W+mdpO2odM9OJxCjFL5KzAi9VTd+3X0K0hJ6\n\tLQWrDtKgi5j57yvjgxKhQP1AK+VTuGG6MhNyuZm8="],"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 v2] libcamera: pipeline: uvcvideo: Map focus controls","Date":"Sat, 14 Sep 2024 21:55:49 +0000","Message-ID":"<01020191f288fe5f-fb094635-2cc7-4e76-8492-a42e0b6978e3-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.14-54.240.7.10","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 pointed out on feedback.\n\n src/libcamera/pipeline/uvcvideo/uvcvideo.cpp | 101 +++++++++++++++++--\n 1 file changed, 93 insertions(+), 8 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\nindex 6b32fa18..11e34e95 100644\n--- a/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n+++ b/src/libcamera/pipeline/uvcvideo/uvcvideo.cpp\n@@ -304,13 +304,26 @@ 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 = -1, def = -1, max = -1;\n+\tif (v4l2Info.min().type() == ControlTypeInteger32)\n+\t\tmin = v4l2Info.min().get<int32_t>();\n+\tif (v4l2Info.min().type() == ControlTypeInteger32)\n+\t\tdef = v4l2Info.def().get<int32_t>();\n+\tif (v4l2Info.min().type() == ControlTypeInteger32)\n+\t\tmax = v4l2Info.max().get<int32_t>();\n \n \t/*\n \t * See UVCCameraData::addControl() for explanations of the different\n@@ -333,8 +346,8 @@ int PipelineHandlerUVC::processControl(ControlList *controls, unsigned int id,\n \n \tcase V4L2_CID_EXPOSURE_AUTO: {\n \t\tint32_t ivalue = value.get<bool>()\n-\t\t\t       ? V4L2_EXPOSURE_APERTURE_PRIORITY\n-\t\t\t       : V4L2_EXPOSURE_MANUAL;\n+\t\t\t\t\t ? V4L2_EXPOSURE_APERTURE_PRIORITY\n+\t\t\t\t\t : V4L2_EXPOSURE_MANUAL;\n \t\tcontrols->set(V4L2_CID_EXPOSURE_AUTO, ivalue);\n \t\tbreak;\n \t}\n@@ -358,6 +371,20 @@ 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\tcontrols->set(cid, static_cast<int32_t>(value.get<float>() * scale + min));\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 +682,32 @@ 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 = -1, def = -1, max = -1;\n+\tif (v4l2Info.min().type() == ControlTypeInteger32)\n+\t\tmin = v4l2Info.min().get<int32_t>();\n+\telse if (v4l2Info.min().type() == ControlTypeBool)\n+\t\tmin = v4l2Info.min().get<bool>();\n+\n+\tif (v4l2Info.def().type() == ControlTypeInteger32)\n+\t\tdef = v4l2Info.def().get<int32_t>();\n+\telse if (v4l2Info.def().type() == ControlTypeBool)\n+\t\tdef = v4l2Info.def().get<bool>();\n+\n+\tif (v4l2Info.max().type() == ControlTypeInteger32)\n+\t\tmax = v4l2Info.max().get<int32_t>();\n+\telse if (v4l2Info.max().type() == ControlTypeBool)\n+\t\tmax = v4l2Info.max().get<bool>();\n \n \tswitch (cid) {\n \tcase V4L2_CID_BRIGHTNESS: {\n@@ -738,6 +783,46 @@ void UVCCameraData::addControl(uint32_t cid, const ControlInfo &v4l2Info,\n \t\t};\n \t\tbreak;\n \t}\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 * In this case 0.15f (~= 150 / (1023 - 1)) was a value read\n+\t\t * from V4L2_CID_FOCUS_ABSOLUTE control when using a random\n+\t\t * camera 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).\n+\t\t *\n+\t\t * These values will definitely vary for each different\n+\t\t * hardware. Also, the values might not scale linearly, but\n+\t\t * 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+\tcase V4L2_CID_FOCUS_AUTO: {\n+\t\tint32_t manual = controls::AfModeManual;\n+\t\tint32_t continuous = controls::AfModeContinuous;\n+\n+\t\tinfo = ControlInfo{\n+\t\t\t{ manual },\n+\t\t\t{ continuous },\n+\t\t\t{ def ? continuous : manual },\n+\t\t};\n+\t\tbreak;\n+\t}\n \n \tdefault:\n \t\tinfo = v4l2Info;\n","prefixes":["v2"]}