Patch Detail
Show a patch.
GET /api/1.1/patches/14196/?format=api
{ "id": 14196, "url": "https://patchwork.libcamera.org/api/1.1/patches/14196/?format=api", "web_url": "https://patchwork.libcamera.org/patch/14196/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/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": "<20211020110825.12902-4-david.plowman@raspberrypi.com>", "date": "2021-10-20T11:08:21", "name": "[libcamera-devel,v3,3/7] libcamera: Convert between ColorSpace class and V4L2 formats", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "5f20c3a65e8d079938dd9f8419b22046eb2bd7dd", "submitter": { "id": 42, "url": "https://patchwork.libcamera.org/api/1.1/people/42/?format=api", "name": "David Plowman", "email": "david.plowman@raspberrypi.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/14196/mbox/", "series": [ { "id": 2641, "url": "https://patchwork.libcamera.org/api/1.1/series/2641/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2641", "date": "2021-10-20T11:08:18", "name": "Colour spaces", "version": 3, "mbox": "https://patchwork.libcamera.org/series/2641/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/14196/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/14196/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 D8CF7BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 20 Oct 2021 11:08:35 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D18FD68F5C;\n\tWed, 20 Oct 2021 13:08:33 +0200 (CEST)", "from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com\n\t[IPv6:2a00:1450:4864:20::32c])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C54326023A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 20 Oct 2021 13:08:29 +0200 (CEST)", "by mail-wm1-x32c.google.com with SMTP id\n\td198-20020a1c1dcf000000b00322f53b9b89so9308294wmd.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 20 Oct 2021 04:08:29 -0700 (PDT)", "from pi4-davidp.pitowers.org\n\t([2a00:1098:3142:14:1ce1:9965:4328:89c4])\n\tby smtp.gmail.com with ESMTPSA id\n\tf20sm1929654wmq.38.2021.10.20.04.08.28\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 20 Oct 2021 04:08:28 -0700 (PDT)" ], "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"VBn33umX\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=BuIekqX960ITwcFo2R4vuTMKBt/WbY00sR8GY6vpIrs=;\n\tb=VBn33umXoT1iOATO06nxyDnyEaSW8vG7Ux3WXJqjYTzLLysOhrgNx9fIZq36UDUI67\n\tiYz7Nh41poSWZeumepEZ56TV6tau51tixNX8QwCFHzzwfYor1oeFXfWVywfBzIZtpT9V\n\tMvkSIecoURyzD/8/lv2fTIMlRxMl6bszpLZZydzZ0MiawekiBiDIp0QfEpweeZ1VWNnW\n\tQNY3Tq5203iGrS+PH5DYXm+5K8FmyLBs312tIYFktczwQ9aU9o82B78TZmbA6ukd7n24\n\tJQrBU6uP3QhkGX8dSJ9ZwmifwMXyN1qKtsqD1ytoeoK9G12f2shoqjBJZMm1vNe4D7/d\n\tknkQ==", "X-Google-DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=BuIekqX960ITwcFo2R4vuTMKBt/WbY00sR8GY6vpIrs=;\n\tb=mmtxcjesBVXS8rRzRCjR7xaTW9ch7ru7xRURyQeNxoOuofXaBcZ8nN6A3/ePG3VcoT\n\tRPW9JMaAp0dmY8L0fM6RiXKN5mDpPcgz+smXso9dXfTH7qvxuBbby+BCKXWAZt9A8djS\n\t41RcfqZUciFbiq4oa8Be//KVmorKkutD54ST8e7lY89Eanj7QDZY94Mc/hm5ABm3wABp\n\tUgNkOjQI3RZcpNeW3wvGjddACdzIhCuIm0bCQxUnX3fq4tGChKPwBjIkXRyn2zO3J/tF\n\tW/v7okkPsu3azBOwqoev7U6SwqFcAy1QhQuzlBfrLJp2jhL6+jPtwCRMa7akd21SF90J\n\tHvlQ==", "X-Gm-Message-State": "AOAM532/jg05TJq58GFFhGvv25sxNUMGXgkoE87A2jewLSCS++l3jF5S\n\t2kmn7ON/SfOstVsMsXmDOyqTgLp45CmwkA==", "X-Google-Smtp-Source": "ABdhPJzclAzP+ttP3iwPVvrIEP+CmmhgtRZTNPU+YwX6IcrX4miLYUZpTGbKrJA+leNp9UE0C7pEcg==", "X-Received": "by 2002:a1c:5417:: with SMTP id\n\ti23mr12686350wmb.17.1634728109309; \n\tWed, 20 Oct 2021 04:08:29 -0700 (PDT)", "From": "David Plowman <david.plowman@raspberrypi.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Wed, 20 Oct 2021 12:08:21 +0100", "Message-Id": "<20211020110825.12902-4-david.plowman@raspberrypi.com>", "X-Mailer": "git-send-email 2.20.1", "In-Reply-To": "<20211020110825.12902-1-david.plowman@raspberrypi.com>", "References": "<20211020110825.12902-1-david.plowman@raspberrypi.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v3 3/7] libcamera: Convert between\n\tColorSpace class and V4L2 formats", "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": "These methods are added to the base V4L2Device class so that they can\nbe shared both by the video device class and subdevices.\n\nWith the ColorSpace class, the color space and related other fields\nare stored together, corresponding to a number of fields in the\nvarious different V4L2 format structures. Template methods are\ntherefore a convenient implementation.\n\nNote that we must explicitly instantiate the templates that will be\nneeded.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\n---\n include/libcamera/internal/v4l2_device.h | 7 +\n src/libcamera/v4l2_device.cpp | 171 +++++++++++++++++++++++\n 2 files changed, 178 insertions(+)", "diff": "diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h\nindex f21bc370..77ea30f6 100644\n--- a/include/libcamera/internal/v4l2_device.h\n+++ b/include/libcamera/internal/v4l2_device.h\n@@ -17,6 +17,7 @@\n #include <libcamera/base/signal.h>\n #include <libcamera/base/span.h>\n \n+#include <libcamera/color_space.h>\n #include <libcamera/controls.h>\n \n namespace libcamera {\n@@ -44,6 +45,12 @@ public:\n \n \tvoid updateControlInfo();\n \n+\ttemplate<typename T>\n+\tstatic ColorSpace v4l2ToColorSpace(const T &v4l2Format);\n+\n+\ttemplate<typename T>\n+\tstatic int colorSpaceToV4l2(const ColorSpace &colorSpace, T &v4l2Format);\n+\n protected:\n \tV4L2Device(const std::string &deviceNode);\n \t~V4L2Device();\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex 9c783c9c..771dc096 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -16,6 +16,8 @@\n #include <sys/syscall.h>\n #include <unistd.h>\n \n+#include <linux/v4l2-mediabus.h>\n+\n #include <libcamera/base/event_notifier.h>\n #include <libcamera/base/log.h>\n #include <libcamera/base/utils.h>\n@@ -731,4 +733,173 @@ void V4L2Device::eventAvailable()\n \tframeStart.emit(event.u.frame_sync.frame_sequence);\n }\n \n+static const std::map<uint32_t, ColorSpace> v4l2ToColorSpaceTable = {\n+\t{ V4L2_COLORSPACE_RAW, ColorSpace::Raw },\n+\t{ V4L2_COLORSPACE_JPEG, ColorSpace::Jpeg },\n+\t{ V4L2_COLORSPACE_SRGB, ColorSpace::Jpeg },\n+\t{ V4L2_COLORSPACE_SMPTE170M, ColorSpace::Smpte170m },\n+\t{ V4L2_COLORSPACE_REC709, ColorSpace::Rec709 },\n+\t{ V4L2_COLORSPACE_BT2020, ColorSpace::Rec2020 },\n+};\n+\n+static const std::map<uint32_t, ColorSpace::YcbcrEncoding> v4l2ToYcbcrEncodingTable = {\n+\t{ V4L2_YCBCR_ENC_601, ColorSpace::YcbcrEncoding::Rec601 },\n+\t{ V4L2_YCBCR_ENC_709, ColorSpace::YcbcrEncoding::Rec709 },\n+\t{ V4L2_YCBCR_ENC_BT2020, ColorSpace::YcbcrEncoding::Rec2020 },\n+};\n+\n+static const std::map<uint32_t, ColorSpace::TransferFunction> v4l2ToTransferFunctionTable = {\n+\t{ V4L2_XFER_FUNC_NONE, ColorSpace::TransferFunction::Linear },\n+\t{ V4L2_XFER_FUNC_SRGB, ColorSpace::TransferFunction::Srgb },\n+\t{ V4L2_XFER_FUNC_709, ColorSpace::TransferFunction::Rec709 },\n+};\n+\n+static const std::map<uint32_t, ColorSpace::Range> v4l2ToRangeTable = {\n+\t{ V4L2_QUANTIZATION_FULL_RANGE, ColorSpace::Range::Full },\n+\t{ V4L2_QUANTIZATION_LIM_RANGE, ColorSpace::Range::Limited },\n+};\n+\n+static const std::vector<std::pair<ColorSpace, v4l2_colorspace>> colorSpaceToV4l2Table = {\n+\t{ ColorSpace::Raw, V4L2_COLORSPACE_RAW },\n+\t{ ColorSpace::Jpeg, V4L2_COLORSPACE_JPEG },\n+\t{ ColorSpace::Smpte170m, V4L2_COLORSPACE_SMPTE170M },\n+\t{ ColorSpace::Rec709, V4L2_COLORSPACE_REC709 },\n+\t{ ColorSpace::Rec2020, V4L2_COLORSPACE_BT2020 },\n+};\n+\n+static const std::map<ColorSpace::Primaries, v4l2_colorspace> primariesToV4l2Table = {\n+\t{ ColorSpace::Primaries::Raw, V4L2_COLORSPACE_RAW },\n+\t{ ColorSpace::Primaries::Smpte170m, V4L2_COLORSPACE_SMPTE170M },\n+\t{ ColorSpace::Primaries::Rec709, V4L2_COLORSPACE_REC709 },\n+\t{ ColorSpace::Primaries::Rec2020, V4L2_COLORSPACE_BT2020 },\n+};\n+\n+static const std::map<ColorSpace::YcbcrEncoding, v4l2_ycbcr_encoding> ycbcrEncodingToV4l2Table = {\n+\t{ ColorSpace::YcbcrEncoding::Rec601, V4L2_YCBCR_ENC_601 },\n+\t{ ColorSpace::YcbcrEncoding::Rec709, V4L2_YCBCR_ENC_709 },\n+\t{ ColorSpace::YcbcrEncoding::Rec2020, V4L2_YCBCR_ENC_BT2020 },\n+};\n+\n+static const std::map<ColorSpace::TransferFunction, v4l2_xfer_func> transferFunctionToV4l2Table = {\n+\t{ ColorSpace::TransferFunction::Linear, V4L2_XFER_FUNC_NONE },\n+\t{ ColorSpace::TransferFunction::Srgb, V4L2_XFER_FUNC_SRGB },\n+\t{ ColorSpace::TransferFunction::Rec709, V4L2_XFER_FUNC_709 },\n+};\n+\n+static const std::map<ColorSpace::Range, v4l2_quantization> rangeToV4l2Table = {\n+\t{ ColorSpace::Range::Full, V4L2_QUANTIZATION_FULL_RANGE },\n+\t{ ColorSpace::Range::Limited, V4L2_QUANTIZATION_LIM_RANGE },\n+};\n+\n+/**\n+ * \\brief Convert the color space fields in a V4L2 format to a ColorSpace\n+ * \\param[in] v4l2Format A V4L2 format containing color space information\n+ *\n+ * The colorspace, ycbcr_enc, xfer_func and quantization fields within a\n+ * V4L2 format structure are converted to a corresponding ColorSpace.\n+ *\n+ * If any V4L2 fields are not recognised those are marked as undefined in\n+ * the ColorSpace, but other fields are still initialised where possible.\n+ * This situation can be detected using the returned value's\n+ * ColorSpace::isFullyDefined() method.\n+ *\n+ * \\return The ColorSpace corresponding to the input V4L2 format\n+ */\n+template<typename T>\n+ColorSpace V4L2Device::v4l2ToColorSpace(const T &v4l2Format)\n+{\n+\tColorSpace colorSpace;\n+\n+\tauto itColor = v4l2ToColorSpaceTable.find(v4l2Format.colorspace);\n+\tif (itColor != v4l2ToColorSpaceTable.end())\n+\t\tcolorSpace = itColor->second;\n+\n+\tauto itYcbcrEncoding = v4l2ToYcbcrEncodingTable.find(v4l2Format.ycbcr_enc);\n+\tif (itYcbcrEncoding != v4l2ToYcbcrEncodingTable.end())\n+\t\tcolorSpace.ycbcrEncoding = itYcbcrEncoding->second;\n+\n+\tauto itTransfer = v4l2ToTransferFunctionTable.find(v4l2Format.xfer_func);\n+\tif (itTransfer != v4l2ToTransferFunctionTable.end())\n+\t\tcolorSpace.transferFunction = itTransfer->second;\n+\n+\tauto itRange = v4l2ToRangeTable.find(v4l2Format.quantization);\n+\tif (itRange != v4l2ToRangeTable.end())\n+\t\tcolorSpace.range = itRange->second;\n+\n+\treturn colorSpace;\n+}\n+\n+template ColorSpace V4L2Device::v4l2ToColorSpace(const struct v4l2_pix_format &);\n+template ColorSpace V4L2Device::v4l2ToColorSpace(const struct v4l2_pix_format_mplane &);\n+template ColorSpace V4L2Device::v4l2ToColorSpace(const struct v4l2_mbus_framefmt &);\n+\n+/**\n+ * \\brief Fill in the color space fields of a V4L2 format from a ColorSpace\n+ * \\param[in] colorSpace The ColorSpace to be converted\n+ * \\param[out] v4l2Format A V4L2 format containing color space information\n+ *\n+ * The colorspace, ycbcr_enc, xfer_func and quantization fields within a\n+ * V4L2 format structure are filled in from a corresponding ColorSpace.\n+ *\n+ * An error is returned if any of the V4L2 fields do not support the\n+ * value given in the ColorSpace. Such fields are set to the V4L2\n+ * \"default\" values, but all other fields are still filled in where\n+ * possible.\n+ *\n+ * \\return 0 on success or a negative error code otherwise\n+ */\n+template<typename T>\n+int V4L2Device::colorSpaceToV4l2(const ColorSpace &colorSpace, T &v4l2Format)\n+{\n+\tint ret = 0;\n+\n+\tv4l2Format.colorspace = V4L2_COLORSPACE_DEFAULT;\n+\tv4l2Format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;\n+\tv4l2Format.xfer_func = V4L2_XFER_FUNC_DEFAULT;\n+\tv4l2Format.quantization = V4L2_QUANTIZATION_DEFAULT;\n+\n+\tauto itColor = std::find_if(colorSpaceToV4l2Table.begin(), colorSpaceToV4l2Table.end(),\n+\t\t\t\t [&colorSpace](const auto &item) {\n+\t\t\t\t\t return colorSpace == item.first;\n+\t\t\t\t });\n+\tif (itColor != colorSpaceToV4l2Table.end()) {\n+\t\tv4l2Format.colorspace = itColor->second;\n+\t\treturn ret;\n+\t}\n+\n+\t/*\n+\t * If the colorSpace doesn't precisely match a standard color space,\n+\t * then we must choose a V4L2 colorspace with matching primaries.\n+\t */\n+\tauto itPrimaries = primariesToV4l2Table.find(colorSpace.primaries);\n+\tif (itPrimaries != primariesToV4l2Table.end())\n+\t\tv4l2Format.colorspace = itPrimaries->second;\n+\telse\n+\t\tret = -1;\n+\n+\tauto itYcbcrEncoding = ycbcrEncodingToV4l2Table.find(colorSpace.ycbcrEncoding);\n+\tif (itYcbcrEncoding != ycbcrEncodingToV4l2Table.end())\n+\t\tv4l2Format.ycbcr_enc = itYcbcrEncoding->second;\n+\telse\n+\t\tret = -1;\n+\n+\tauto itTransfer = transferFunctionToV4l2Table.find(colorSpace.transferFunction);\n+\tif (itTransfer != transferFunctionToV4l2Table.end())\n+\t\tv4l2Format.xfer_func = itTransfer->second;\n+\telse\n+\t\tret = -1;\n+\n+\tauto itRange = rangeToV4l2Table.find(colorSpace.range);\n+\tif (itRange != rangeToV4l2Table.end())\n+\t\tv4l2Format.quantization = itRange->second;\n+\telse\n+\t\tret = -1;\n+\n+\treturn ret;\n+}\n+\n+template int V4L2Device::colorSpaceToV4l2(const ColorSpace &, struct v4l2_pix_format &);\n+template int V4L2Device::colorSpaceToV4l2(const ColorSpace &, struct v4l2_pix_format_mplane &);\n+template int V4L2Device::colorSpaceToV4l2(const ColorSpace &, struct v4l2_mbus_framefmt &);\n+\n } /* namespace libcamera */\n", "prefixes": [ "libcamera-devel", "v3", "3/7" ] }