From patchwork Wed Oct 20 11:08:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 14196 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 D8CF7BDB1C for ; Wed, 20 Oct 2021 11:08:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D18FD68F5C; Wed, 20 Oct 2021 13:08:33 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="VBn33umX"; dkim-atps=neutral Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C54326023A for ; Wed, 20 Oct 2021 13:08:29 +0200 (CEST) Received: by mail-wm1-x32c.google.com with SMTP id d198-20020a1c1dcf000000b00322f53b9b89so9308294wmd.0 for ; Wed, 20 Oct 2021 04:08:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BuIekqX960ITwcFo2R4vuTMKBt/WbY00sR8GY6vpIrs=; b=VBn33umXoT1iOATO06nxyDnyEaSW8vG7Ux3WXJqjYTzLLysOhrgNx9fIZq36UDUI67 iYz7Nh41poSWZeumepEZ56TV6tau51tixNX8QwCFHzzwfYor1oeFXfWVywfBzIZtpT9V MvkSIecoURyzD/8/lv2fTIMlRxMl6bszpLZZydzZ0MiawekiBiDIp0QfEpweeZ1VWNnW QNY3Tq5203iGrS+PH5DYXm+5K8FmyLBs312tIYFktczwQ9aU9o82B78TZmbA6ukd7n24 JQrBU6uP3QhkGX8dSJ9ZwmifwMXyN1qKtsqD1ytoeoK9G12f2shoqjBJZMm1vNe4D7/d knkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BuIekqX960ITwcFo2R4vuTMKBt/WbY00sR8GY6vpIrs=; b=mmtxcjesBVXS8rRzRCjR7xaTW9ch7ru7xRURyQeNxoOuofXaBcZ8nN6A3/ePG3VcoT RPW9JMaAp0dmY8L0fM6RiXKN5mDpPcgz+smXso9dXfTH7qvxuBbby+BCKXWAZt9A8djS 41RcfqZUciFbiq4oa8Be//KVmorKkutD54ST8e7lY89Eanj7QDZY94Mc/hm5ABm3wABp UgNkOjQI3RZcpNeW3wvGjddACdzIhCuIm0bCQxUnX3fq4tGChKPwBjIkXRyn2zO3J/tF W/v7okkPsu3azBOwqoev7U6SwqFcAy1QhQuzlBfrLJp2jhL6+jPtwCRMa7akd21SF90J HvlQ== X-Gm-Message-State: AOAM532/jg05TJq58GFFhGvv25sxNUMGXgkoE87A2jewLSCS++l3jF5S 2kmn7ON/SfOstVsMsXmDOyqTgLp45CmwkA== X-Google-Smtp-Source: ABdhPJzclAzP+ttP3iwPVvrIEP+CmmhgtRZTNPU+YwX6IcrX4miLYUZpTGbKrJA+leNp9UE0C7pEcg== X-Received: by 2002:a1c:5417:: with SMTP id i23mr12686350wmb.17.1634728109309; Wed, 20 Oct 2021 04:08:29 -0700 (PDT) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:1ce1:9965:4328:89c4]) by smtp.gmail.com with ESMTPSA id f20sm1929654wmq.38.2021.10.20.04.08.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 04:08:28 -0700 (PDT) From: David Plowman 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 Subject: [libcamera-devel] [PATCH v3 3/7] libcamera: Convert between ColorSpace class and V4L2 formats 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" These methods are added to the base V4L2Device class so that they can be shared both by the video device class and subdevices. With the ColorSpace class, the color space and related other fields are stored together, corresponding to a number of fields in the various different V4L2 format structures. Template methods are therefore a convenient implementation. Note that we must explicitly instantiate the templates that will be needed. Signed-off-by: David Plowman --- include/libcamera/internal/v4l2_device.h | 7 + src/libcamera/v4l2_device.cpp | 171 +++++++++++++++++++++++ 2 files changed, 178 insertions(+) diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index f21bc370..77ea30f6 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -17,6 +17,7 @@ #include #include +#include #include namespace libcamera { @@ -44,6 +45,12 @@ public: void updateControlInfo(); + template + static ColorSpace v4l2ToColorSpace(const T &v4l2Format); + + template + static int colorSpaceToV4l2(const ColorSpace &colorSpace, T &v4l2Format); + protected: V4L2Device(const std::string &deviceNode); ~V4L2Device(); diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index 9c783c9c..771dc096 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -16,6 +16,8 @@ #include #include +#include + #include #include #include @@ -731,4 +733,173 @@ void V4L2Device::eventAvailable() frameStart.emit(event.u.frame_sync.frame_sequence); } +static const std::map v4l2ToColorSpaceTable = { + { V4L2_COLORSPACE_RAW, ColorSpace::Raw }, + { V4L2_COLORSPACE_JPEG, ColorSpace::Jpeg }, + { V4L2_COLORSPACE_SRGB, ColorSpace::Jpeg }, + { V4L2_COLORSPACE_SMPTE170M, ColorSpace::Smpte170m }, + { V4L2_COLORSPACE_REC709, ColorSpace::Rec709 }, + { V4L2_COLORSPACE_BT2020, ColorSpace::Rec2020 }, +}; + +static const std::map v4l2ToYcbcrEncodingTable = { + { V4L2_YCBCR_ENC_601, ColorSpace::YcbcrEncoding::Rec601 }, + { V4L2_YCBCR_ENC_709, ColorSpace::YcbcrEncoding::Rec709 }, + { V4L2_YCBCR_ENC_BT2020, ColorSpace::YcbcrEncoding::Rec2020 }, +}; + +static const std::map v4l2ToTransferFunctionTable = { + { V4L2_XFER_FUNC_NONE, ColorSpace::TransferFunction::Linear }, + { V4L2_XFER_FUNC_SRGB, ColorSpace::TransferFunction::Srgb }, + { V4L2_XFER_FUNC_709, ColorSpace::TransferFunction::Rec709 }, +}; + +static const std::map v4l2ToRangeTable = { + { V4L2_QUANTIZATION_FULL_RANGE, ColorSpace::Range::Full }, + { V4L2_QUANTIZATION_LIM_RANGE, ColorSpace::Range::Limited }, +}; + +static const std::vector> colorSpaceToV4l2Table = { + { ColorSpace::Raw, V4L2_COLORSPACE_RAW }, + { ColorSpace::Jpeg, V4L2_COLORSPACE_JPEG }, + { ColorSpace::Smpte170m, V4L2_COLORSPACE_SMPTE170M }, + { ColorSpace::Rec709, V4L2_COLORSPACE_REC709 }, + { ColorSpace::Rec2020, V4L2_COLORSPACE_BT2020 }, +}; + +static const std::map primariesToV4l2Table = { + { ColorSpace::Primaries::Raw, V4L2_COLORSPACE_RAW }, + { ColorSpace::Primaries::Smpte170m, V4L2_COLORSPACE_SMPTE170M }, + { ColorSpace::Primaries::Rec709, V4L2_COLORSPACE_REC709 }, + { ColorSpace::Primaries::Rec2020, V4L2_COLORSPACE_BT2020 }, +}; + +static const std::map ycbcrEncodingToV4l2Table = { + { ColorSpace::YcbcrEncoding::Rec601, V4L2_YCBCR_ENC_601 }, + { ColorSpace::YcbcrEncoding::Rec709, V4L2_YCBCR_ENC_709 }, + { ColorSpace::YcbcrEncoding::Rec2020, V4L2_YCBCR_ENC_BT2020 }, +}; + +static const std::map transferFunctionToV4l2Table = { + { ColorSpace::TransferFunction::Linear, V4L2_XFER_FUNC_NONE }, + { ColorSpace::TransferFunction::Srgb, V4L2_XFER_FUNC_SRGB }, + { ColorSpace::TransferFunction::Rec709, V4L2_XFER_FUNC_709 }, +}; + +static const std::map rangeToV4l2Table = { + { ColorSpace::Range::Full, V4L2_QUANTIZATION_FULL_RANGE }, + { ColorSpace::Range::Limited, V4L2_QUANTIZATION_LIM_RANGE }, +}; + +/** + * \brief Convert the color space fields in a V4L2 format to a ColorSpace + * \param[in] v4l2Format A V4L2 format containing color space information + * + * The colorspace, ycbcr_enc, xfer_func and quantization fields within a + * V4L2 format structure are converted to a corresponding ColorSpace. + * + * If any V4L2 fields are not recognised those are marked as undefined in + * the ColorSpace, but other fields are still initialised where possible. + * This situation can be detected using the returned value's + * ColorSpace::isFullyDefined() method. + * + * \return The ColorSpace corresponding to the input V4L2 format + */ +template +ColorSpace V4L2Device::v4l2ToColorSpace(const T &v4l2Format) +{ + ColorSpace colorSpace; + + auto itColor = v4l2ToColorSpaceTable.find(v4l2Format.colorspace); + if (itColor != v4l2ToColorSpaceTable.end()) + colorSpace = itColor->second; + + auto itYcbcrEncoding = v4l2ToYcbcrEncodingTable.find(v4l2Format.ycbcr_enc); + if (itYcbcrEncoding != v4l2ToYcbcrEncodingTable.end()) + colorSpace.ycbcrEncoding = itYcbcrEncoding->second; + + auto itTransfer = v4l2ToTransferFunctionTable.find(v4l2Format.xfer_func); + if (itTransfer != v4l2ToTransferFunctionTable.end()) + colorSpace.transferFunction = itTransfer->second; + + auto itRange = v4l2ToRangeTable.find(v4l2Format.quantization); + if (itRange != v4l2ToRangeTable.end()) + colorSpace.range = itRange->second; + + return colorSpace; +} + +template ColorSpace V4L2Device::v4l2ToColorSpace(const struct v4l2_pix_format &); +template ColorSpace V4L2Device::v4l2ToColorSpace(const struct v4l2_pix_format_mplane &); +template ColorSpace V4L2Device::v4l2ToColorSpace(const struct v4l2_mbus_framefmt &); + +/** + * \brief Fill in the color space fields of a V4L2 format from a ColorSpace + * \param[in] colorSpace The ColorSpace to be converted + * \param[out] v4l2Format A V4L2 format containing color space information + * + * The colorspace, ycbcr_enc, xfer_func and quantization fields within a + * V4L2 format structure are filled in from a corresponding ColorSpace. + * + * An error is returned if any of the V4L2 fields do not support the + * value given in the ColorSpace. Such fields are set to the V4L2 + * "default" values, but all other fields are still filled in where + * possible. + * + * \return 0 on success or a negative error code otherwise + */ +template +int V4L2Device::colorSpaceToV4l2(const ColorSpace &colorSpace, T &v4l2Format) +{ + int ret = 0; + + v4l2Format.colorspace = V4L2_COLORSPACE_DEFAULT; + v4l2Format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; + v4l2Format.xfer_func = V4L2_XFER_FUNC_DEFAULT; + v4l2Format.quantization = V4L2_QUANTIZATION_DEFAULT; + + auto itColor = std::find_if(colorSpaceToV4l2Table.begin(), colorSpaceToV4l2Table.end(), + [&colorSpace](const auto &item) { + return colorSpace == item.first; + }); + if (itColor != colorSpaceToV4l2Table.end()) { + v4l2Format.colorspace = itColor->second; + return ret; + } + + /* + * If the colorSpace doesn't precisely match a standard color space, + * then we must choose a V4L2 colorspace with matching primaries. + */ + auto itPrimaries = primariesToV4l2Table.find(colorSpace.primaries); + if (itPrimaries != primariesToV4l2Table.end()) + v4l2Format.colorspace = itPrimaries->second; + else + ret = -1; + + auto itYcbcrEncoding = ycbcrEncodingToV4l2Table.find(colorSpace.ycbcrEncoding); + if (itYcbcrEncoding != ycbcrEncodingToV4l2Table.end()) + v4l2Format.ycbcr_enc = itYcbcrEncoding->second; + else + ret = -1; + + auto itTransfer = transferFunctionToV4l2Table.find(colorSpace.transferFunction); + if (itTransfer != transferFunctionToV4l2Table.end()) + v4l2Format.xfer_func = itTransfer->second; + else + ret = -1; + + auto itRange = rangeToV4l2Table.find(colorSpace.range); + if (itRange != rangeToV4l2Table.end()) + v4l2Format.quantization = itRange->second; + else + ret = -1; + + return ret; +} + +template int V4L2Device::colorSpaceToV4l2(const ColorSpace &, struct v4l2_pix_format &); +template int V4L2Device::colorSpaceToV4l2(const ColorSpace &, struct v4l2_pix_format_mplane &); +template int V4L2Device::colorSpaceToV4l2(const ColorSpace &, struct v4l2_mbus_framefmt &); + } /* namespace libcamera */