From patchwork Thu Aug 5 14:21:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 13212 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 1CB5FC3237 for ; Thu, 5 Aug 2021 14:22:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CFA0368822; Thu, 5 Aug 2021 16:22:00 +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="ApWBEUIr"; dkim-atps=neutral Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 857E16026D for ; Thu, 5 Aug 2021 16:21:59 +0200 (CEST) Received: by mail-wr1-x435.google.com with SMTP id n12so6812773wrr.2 for ; Thu, 05 Aug 2021 07:21:59 -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=ncd5E7VqwPo5qncdQR3S7YIkVS5r9s5B2EL/AyaJzTo=; b=ApWBEUIrKy4FSvlKiZylZy8rkGUXApWpL7pNDNn1US0ORGMeisO7Sm+eLw535fILxF jrQepwxFeQt8ll1QKe5z2U6e692CLP7ziIF9Y696pVSwnNjCDnUu3NiPxDy/JEF/x+xs Lxr/bxKZujY9i6XnxqWj5GFWjgGq8D8xKmAH5bFMP4+lMWdVv7KG03Wi5obGRm3qPTMg 19iodFNWeoi5Z2zSO2Vd3yfmFCNJ5QrhNjuA38PdJ/TFwWUdgcdN/uSwV77qi8BRJTvl p0SYmEsrRhrolzEUw9ykZXMmiIyOvHbZBtUJ4fK8JpKQIoa3Z9fKze3o61qgAXydU/es QBPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ncd5E7VqwPo5qncdQR3S7YIkVS5r9s5B2EL/AyaJzTo=; b=buWgbsCAT+vnuvmyFTPSMoi3qK/0Bpt8iKEllKi1GdH0th9lQg67j1tmQX4chN9SqA 1W+/r0vYrYDFnPLY4jn8WlrcZHi0+ayQIRY46R2zZsz1zRA6unv8H80WuK3DFVgKkt7O g2Pa8ZEuzfnouRzVP1ZU0KQp7+Li0ONuG7vdjYFxqzyBND0+MCPfg+hLiGOOVzqWfhyo ngFH0Ny/1EHkv0GEumEBASwS9IoTO4Ej65JbH/o/yg5MocjxjR9TeCggMt1e8BlZGoXh k1u5IesdCTkIoT6RXCdTIB6e80AbBlXTIGxD3U2L0AaFvqWOmGyQvoEGpxGehofc4yOc mIRw== X-Gm-Message-State: AOAM532e4Mnl7efgvyP3/kEn9fmiq1gX3FulK3weZmZy2fxex7iasSGc sMTMn2MBxe5dqAQXOe5k4gg4AcNrU5KFLg== X-Google-Smtp-Source: ABdhPJxpDDhfO5jj5qqSr013rKwN23P1TDgSQlPvDFP1Xdp9jo6RTINb0U70OFJxpt2GA1c+ua78BQ== X-Received: by 2002:adf:f64b:: with SMTP id x11mr5526222wrp.155.1628173318894; Thu, 05 Aug 2021 07:21:58 -0700 (PDT) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:1ce1:9965:4328:89c4]) by smtp.gmail.com with ESMTPSA id k17sm6410004wrw.53.2021.08.05.07.21.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Aug 2021 07:21:58 -0700 (PDT) From: David Plowman To: libcamera-devel@lists.libcamera.org Date: Thu, 5 Aug 2021 15:21:52 +0100 Message-Id: <20210805142154.20324-2-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210805142154.20324-1-david.plowman@raspberrypi.com> References: <20210805142154.20324-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/3] libcamera: Add ColorSpace class 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" This class represents a colour space by defining its YCbCr encoding, the transfer (gamma) function is uses, and whether the output is full or limited range. Signed-off-by: David Plowman Reviewed-by: Naushir Patuck --- include/libcamera/color_space.h | 94 +++++++++++++++++ include/libcamera/meson.build | 1 + src/libcamera/color_space.cpp | 180 ++++++++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 4 files changed, 276 insertions(+) create mode 100644 include/libcamera/color_space.h create mode 100644 src/libcamera/color_space.cpp diff --git a/include/libcamera/color_space.h b/include/libcamera/color_space.h new file mode 100644 index 00000000..3d990f99 --- /dev/null +++ b/include/libcamera/color_space.h @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Raspberry Pi (Trading) Limited + * + * color_space.h - color space definitions + */ + +#ifndef __LIBCAMERA_COLOR_SPACE_H__ +#define __LIBCAMERA_COLOR_SPACE_H__ + +#include + +namespace libcamera { + +class ColorSpace +{ +public: + enum class Encoding : int { + UNDEFINED, + RAW, + REC601, + REC709, + REC2020, + VIDEO, + }; + + enum class TransferFunction : int { + UNDEFINED, + IDENTITY, + SRGB, + REC709, + }; + + enum class Range : int { + UNDEFINED, + FULL, + LIMITED, + }; + + constexpr ColorSpace(Encoding e, TransferFunction t, Range r) + : encoding(e), transferFunction(t), range(r) + { + } + + constexpr ColorSpace() + : ColorSpace(Encoding::UNDEFINED, TransferFunction::UNDEFINED, Range::UNDEFINED) + { + } + + static const ColorSpace UNDEFINED; + static const ColorSpace RAW; + static const ColorSpace JFIF; + static const ColorSpace SMPTE170M; + static const ColorSpace REC709; + static const ColorSpace REC2020; + static const ColorSpace VIDEO; + + Encoding encoding; + TransferFunction transferFunction; + Range range; + + bool isFullyDefined() const + { + return encoding != Encoding::UNDEFINED && + transferFunction != TransferFunction::UNDEFINED && + range != Range::UNDEFINED; + } + + const std::string toString() const; +}; + +constexpr ColorSpace ColorSpace::UNDEFINED = { Encoding::UNDEFINED, TransferFunction::UNDEFINED, Range::UNDEFINED }; +constexpr ColorSpace ColorSpace::RAW = { Encoding::RAW, TransferFunction::IDENTITY, Range::FULL }; +constexpr ColorSpace ColorSpace::JFIF = { Encoding::REC601, TransferFunction::SRGB, Range::FULL }; +constexpr ColorSpace ColorSpace::SMPTE170M = { Encoding::REC601, TransferFunction::REC709, Range::LIMITED }; +constexpr ColorSpace ColorSpace::REC709 = { Encoding::REC709, TransferFunction::REC709, Range::LIMITED }; +constexpr ColorSpace ColorSpace::REC2020 = { Encoding::REC2020, TransferFunction::REC709, Range::LIMITED }; +constexpr ColorSpace ColorSpace::VIDEO = { Encoding::VIDEO, TransferFunction::REC709, Range::LIMITED }; + +static inline bool operator==(const ColorSpace &lhs, const ColorSpace &rhs) +{ + return lhs.encoding == rhs.encoding && + lhs.transferFunction == rhs.transferFunction && + lhs.range == rhs.range; +} + +static inline bool operator!=(const ColorSpace &lhs, const ColorSpace &rhs) +{ + return !(lhs == rhs); +} + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_COLOR_SPACE_H__ */ diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 5b25ef84..7a8a04e5 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -3,6 +3,7 @@ libcamera_public_headers = files([ 'camera.h', 'camera_manager.h', + 'color_space.h', 'compiler.h', 'controls.h', 'file_descriptor.h', diff --git a/src/libcamera/color_space.cpp b/src/libcamera/color_space.cpp new file mode 100644 index 00000000..c40264db --- /dev/null +++ b/src/libcamera/color_space.cpp @@ -0,0 +1,180 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Raspberry Pi (Trading) Limited + * + * color_space.cpp - color spaces. + */ + +#include + +/** + * \file color_space.h + * \brief Class and enums to represent colour spaces. + */ + +namespace libcamera { + +/** + * \class ColorSpace + * \brief Class to describe a color space. + * + * The color space class defines the encodings of the color primaries, the + * transfer function associated with the color space, and the range (sometimes + * also referred to as the quantisation) of the color space. + * + * Certain combinations of these fields form well-known standard color spaces, + * such as "JFIF" or "REC709", though there is flexibility to leave some or all + * of them undefined too. + */ + +/** + * \enum ColorSpace::Encoding + * \brief The encoding used for the color primaries. + * + * \var ColorSpace::Encoding::UNDEFINED + * \brief The encoding for the colour primaries is not specified. + * \var ColorSpace::Encoding::RAW + * \brief These are raw colours from the sensor. + * \var ColorSpace::Encoding::REC601 + * \brief REC601 colour primaries. + * \var ColorSpace::Encoding::REC709 + * \brief Rec709 colour primaries. + * \var ColorSpace::Encoding::REC2020 + * \brief REC2020 colour primaries. + * \var ColorSpace::Encoding::VIDEO + * \brief A place-holder for video streams which will be resolved to one + * of REC601, REC709 or REC2020 once the video resolution is known. + */ + +/** + * \enum ColorSpace::TransferFunction + * \brief The transfer function used for this colour space. + * + * \var ColorSpace::TransferFunction::UNDEFINED + * \brief The transfer function is not specified. + * \var ColorSpace::TransferFunction::IDENTITY + * \brief This color space uses an identity transfer function. + * \var ColorSpace::TransferFunction::SRGB + * \brief sRGB transfer function. + * \var ColorSpace::TransferFunction::REC709 + * \brief Rec709 transfer function. + */ + +/** + * \enum ColorSpace::Range + * \brief The range (sometimes "quantisation") for this color space. + * + * \var ColorSpace::Range::UNDEFINED + * \brief The range is not specified. + * \var ColorSpace::Range::FULL + * \brief This color space uses full range pixel values. + * \var ColorSpace::Range::LIMITED + * \brief This color space uses limited range pixel values. + */ + +/** + * \fn ColorSpace::ColorSpace(Encoding e, TransferFunction t, Range r) + * \brief Construct a ColorSpace from explicit values + * \param[in] e The encoding for the color primaries + * \param[in] t The transfer function for the color space + * \param[in] r The range of the pixel values in this color space + */ + +/** + * \fn ColorSpace::ColorSpace() + * \brief Construct a color space with undefined encoding, transfer function + * and range + */ + +/** + * \fn ColorSpace::isFullyDefined() const + * \brief Return whether all the fields of the color space are defined. + */ + +/** + * \brief Assemble and return a readable string representation of the + * ColorSpace + * \return A string describing the ColorSpace + */ +const std::string ColorSpace::toString() const +{ + static const char *encodings[] = { + "UNDEFINED", + "RAW", + "REC601", + "REC709", + "REC2020", + }; + static const char *transferFunctions[] = { + "UNDEFINED", + "IDENTITY", + "SRGB", + "REC709", + }; + static const char *ranges[] = { + "UNDEFINED", + "FULL", + "LIMITED", + }; + + return std::string(encodings[static_cast(encoding)]) + "+" + + std::string(transferFunctions[static_cast(transferFunction)]) + "+" + + std::string(ranges[static_cast(range)]); +} + +/** + * \var ColorSpace::encoding + * \brief The encoding of the color primaries + */ + +/** + * \var ColorSpace::transferFunction + * \brief The transfer function for this color space. + */ + +/** + * \var ColorSpace::range + * \brief The pixel range used by this color space. + */ + +/** + * \var ColorSpace::UNDEFINED + * \brief A constant representing a fully undefined color space. + */ + +/** + * \var ColorSpace::RAW + * \brief A constant representing a raw color space (from a sensor). + */ + +/** + * \var ColorSpace::JFIF + * \brief A constant representing the JFIF color space usually used for + * encoding JPEG images. + */ + +/** + * \var ColorSpace::SMPTE170M + * \brief A constant representing the SMPTE170M color space (sometimes also + * referred to as "full range BT601"). + */ + +/** + * \var ColorSpace::REC709 + * \brief A constant representing the REC709 color space. + */ + +/** + * \var ColorSpace::REC2020 + * \brief A constant representing the REC2020 color space. + */ + +/** + * \var ColorSpace::VIDEO + * \brief A constant that video streams can use to indicate the "default" + * color space for a video of this resolution, once that is is known. For + * exmample, SD streams would interpret this as SMPTE170M, HD streams as + * REC709 and ultra HD as REC2020. + */ + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 4f085801..e0748840 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -8,6 +8,7 @@ libcamera_sources = files([ 'camera_manager.cpp', 'camera_sensor.cpp', 'camera_sensor_properties.cpp', + 'color_space.cpp', 'controls.cpp', 'control_serializer.cpp', 'control_validator.cpp', From patchwork Thu Aug 5 14:21:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 13213 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 AE4EFC3238 for ; Thu, 5 Aug 2021 14:22:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 60A3368864; Thu, 5 Aug 2021 16:22:01 +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="NUnl2ko0"; dkim-atps=neutral Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CB10A6026D for ; Thu, 5 Aug 2021 16:21:59 +0200 (CEST) Received: by mail-wr1-x431.google.com with SMTP id d8so6805303wrm.4 for ; Thu, 05 Aug 2021 07:21:59 -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=XWhF3ysRr+r3b1PFO1nBDUmwHoN8VFvpRQhLtXCbKFM=; b=NUnl2ko0SGYkQb4siyCtCFX9e3Nuxw73hjhe1u1cz72Uld+GMCb3znu4AjDraY8QKj Aqtu8EhWNm7DLJaCfAjHc7WXApZ1iDFq8/Ff/3o9+X+hENOp87Zc1wjdpSw5xBYR21gV Ff8EAko6zS9WHTSasRrqb+tEhz5TVB3ZJLPzV3Krh6xatKH3N7UTnGdn0A9m52+6qkVR 3C/Cer0K0gTJXk2WcWFfZP5gU9+kLZBWASjBjDNy41y9oCcyfCzpoQt4vrzF8EQD5Hrk h6mWtGYjsgWi7NzKtRGX+cMTugoLvdaTU6346k2ltmM3Tp6SiFRx+hgLlZw/SPFcUeiz S3aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XWhF3ysRr+r3b1PFO1nBDUmwHoN8VFvpRQhLtXCbKFM=; b=RXirKCBTyTSmdEkSwXhAPUuzY3zAN4Ph/vrFuVn7H01IXHNvidCCwhKXr5vO9RDgPN /MwrcjV9g07hP2njNYxZ8KiBhR/DQyX++I+pIKaW9Ab92iNOCQCxWXgZu8HCk7oAOQ14 /16GFOOuDZJL7XhJEubCleGiFsbFzYGYeBvzosbDNrIx9ULxWoyuTBlCsPyelda5M7lq rLL3BeuKTbn2HUs60C8n3jKy9ggdJ+o++smJvsKebTyhLca0JyRysDjk0+lcCR63cmz5 T2drKX8S9D/FcFKqFPiG79QdNDY+eU0qSzd0HSzhEHAgUCVlTeYEeZhui8lWk9caxWbB WdNA== X-Gm-Message-State: AOAM531fS9V3F8HTtaousWdVxWA3kwtjRrZeDSYLqsKPMhd+I0LnX+y2 IMSqggrg0UT6XxpZ8xsR14++GhXccFk0yQ== X-Google-Smtp-Source: ABdhPJxGv+AduxiUcceoo0qBaK/OHF4Oe0/f7xTlODkMkzvgWhyyJObZsjxZwKPbjsMdF5b8/vtQPw== X-Received: by 2002:a5d:68cb:: with SMTP id p11mr5652424wrw.364.1628173319268; Thu, 05 Aug 2021 07:21:59 -0700 (PDT) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:1ce1:9965:4328:89c4]) by smtp.gmail.com with ESMTPSA id k17sm6410004wrw.53.2021.08.05.07.21.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Aug 2021 07:21:59 -0700 (PDT) From: David Plowman To: libcamera-devel@lists.libcamera.org Date: Thu, 5 Aug 2021 15:21:53 +0100 Message-Id: <20210805142154.20324-3-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210805142154.20324-1-david.plowman@raspberrypi.com> References: <20210805142154.20324-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/3] libcamera: Support passing ColorSpaces to V4L2 drivers 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" The ColorSpace class is added to the StreamConfiguration, and is now passed to V4L2 devices where it is handled appropriately. Note how this means that the colour space is configured per-stream (though platforms may restrict this). Signed-off-by: David Plowman --- include/libcamera/internal/v4l2_videodevice.h | 2 + include/libcamera/stream.h | 3 + src/libcamera/v4l2_videodevice.cpp | 117 ++++++++++++++++++ 3 files changed, 122 insertions(+) diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index e767ec84..c05899cf 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -163,6 +164,7 @@ public: V4L2PixelFormat fourcc; Size size; + ColorSpace colorSpace; std::array planes; unsigned int planesCount = 0; diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h index 0c55e716..131f7733 100644 --- a/include/libcamera/stream.h +++ b/include/libcamera/stream.h @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -47,6 +48,8 @@ struct StreamConfiguration { unsigned int bufferCount; + ColorSpace colorSpace; + Stream *stream() const { return stream_; } void setStream(Stream *stream) { stream_ = stream; } const StreamFormats &formats() const { return formats_; } diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index da2af6a1..7f30412c 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -366,6 +366,11 @@ bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const * \brief The image size in pixels */ +/** + * \var V4L2DeviceFormat::colorSpace + * \brief The color space of the pixels + */ + /** * \var V4L2DeviceFormat::fourcc * \brief The fourcc code describing the pixel encoding scheme @@ -731,6 +736,114 @@ int V4L2VideoDevice::getFormat(V4L2DeviceFormat *format) return getFormatSingleplane(format); } +static const std::vector> colorSpaceToV4l2 = { + { ColorSpace::RAW, V4L2_COLORSPACE_RAW }, + { ColorSpace::JFIF, V4L2_COLORSPACE_JPEG }, + { ColorSpace::SMPTE170M, V4L2_COLORSPACE_SMPTE170M }, + { ColorSpace::REC709, V4L2_COLORSPACE_REC709 }, + { ColorSpace::REC2020, V4L2_COLORSPACE_BT2020 }, +}; + +static const std::map encodingToV4l2 = { + { ColorSpace::Encoding::REC601, V4L2_YCBCR_ENC_601 }, + { ColorSpace::Encoding::REC709, V4L2_YCBCR_ENC_709 }, + { ColorSpace::Encoding::REC2020, V4L2_YCBCR_ENC_BT2020 }, +}; + +static const std::map transferFunctionToV4l2 = { + { ColorSpace::TransferFunction::IDENTITY, V4L2_XFER_FUNC_NONE }, + { ColorSpace::TransferFunction::SRGB, V4L2_XFER_FUNC_SRGB }, + { ColorSpace::TransferFunction::REC709, V4L2_XFER_FUNC_709 }, +}; + +static const std::map rangeToV4l2 = { + { ColorSpace::Range::FULL, V4L2_QUANTIZATION_FULL_RANGE }, + { ColorSpace::Range::LIMITED, V4L2_QUANTIZATION_LIM_RANGE }, +}; + +template +static void setColorSpace(const ColorSpace &colorSpace, T &v4l2PixFormat) +{ + if (!colorSpace.isFullyDefined()) + LOG(V4L2, Warning) << "Setting non-fully defined colour space" + << colorSpace.toString(); + + v4l2PixFormat.colorspace = V4L2_COLORSPACE_DEFAULT; + v4l2PixFormat.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; + v4l2PixFormat.xfer_func = V4L2_XFER_FUNC_DEFAULT; + v4l2PixFormat.quantization = V4L2_QUANTIZATION_DEFAULT; + + auto itColor = std::find_if(colorSpaceToV4l2.begin(), colorSpaceToV4l2.end(), + [&colorSpace](const std::pair &item) { return colorSpace == item.first; }); + if (itColor != colorSpaceToV4l2.end()) + v4l2PixFormat.colorspace = itColor->second; + + auto itEncoding = encodingToV4l2.find(colorSpace.encoding); + if (itEncoding != encodingToV4l2.end()) + v4l2PixFormat.ycbcr_enc = itEncoding->second; + + auto itTransfer = transferFunctionToV4l2.find(colorSpace.transferFunction); + if (itTransfer != transferFunctionToV4l2.end()) + v4l2PixFormat.xfer_func = itTransfer->second; + + auto itRange = rangeToV4l2.find(colorSpace.range); + if (itRange != rangeToV4l2.end()) + v4l2PixFormat.quantization = itRange->second; +} + +static const std::map v4l2ToColorSpace = { + { V4L2_COLORSPACE_RAW, ColorSpace::RAW }, + { V4L2_COLORSPACE_JPEG, ColorSpace::JFIF }, + { V4L2_COLORSPACE_SRGB, ColorSpace::JFIF }, + { V4L2_COLORSPACE_SMPTE170M, ColorSpace::SMPTE170M }, + { V4L2_COLORSPACE_REC709, ColorSpace::REC709 }, + { V4L2_COLORSPACE_BT2020, ColorSpace::REC2020 }, +}; + +static const std::map v4l2ToEncoding = { + { V4L2_YCBCR_ENC_601, ColorSpace::Encoding::REC601 }, + { V4L2_YCBCR_ENC_709, ColorSpace::Encoding::REC709 }, + { V4L2_YCBCR_ENC_BT2020, ColorSpace::Encoding::REC2020 }, +}; + +static const std::map v4l2ToTransferFunction = { + { V4L2_XFER_FUNC_NONE, ColorSpace::TransferFunction::IDENTITY }, + { V4L2_XFER_FUNC_SRGB, ColorSpace::TransferFunction::SRGB }, + { V4L2_XFER_FUNC_709, ColorSpace::TransferFunction::REC709 }, +}; + +static const std::map v4l2ToRange = { + { V4L2_QUANTIZATION_FULL_RANGE, ColorSpace::Range::FULL }, + { V4L2_QUANTIZATION_LIM_RANGE, ColorSpace::Range::LIMITED }, +}; + +template +static ColorSpace getColorSpace(const T &v4l2PixFormat) +{ + ColorSpace colorSpace; + + auto itColor = v4l2ToColorSpace.find(v4l2PixFormat.colorspace); + if (itColor != v4l2ToColorSpace.end()) + colorSpace = itColor->second; + + auto itEncoding = v4l2ToEncoding.find(v4l2PixFormat.ycbcr_enc); + if (itEncoding != v4l2ToEncoding.end()) + colorSpace.encoding = itEncoding->second; + + auto itTransfer = v4l2ToTransferFunction.find(v4l2PixFormat.xfer_func); + if (itTransfer != v4l2ToTransferFunction.end()) + colorSpace.transferFunction = itTransfer->second; + + auto itRange = v4l2ToRange.find(v4l2PixFormat.quantization); + if (itRange != v4l2ToRange.end()) + colorSpace.range = itRange->second; + + if (!colorSpace.isFullyDefined()) + LOG(V4L2, Warning) << "Returning non-fully defined colour space" + << colorSpace.toString(); + return colorSpace; +} + /** * \brief Try an image format on the V4L2 video device * \param[inout] format The image format to test applicability to the video device @@ -840,6 +953,7 @@ int V4L2VideoDevice::getFormatMultiplane(V4L2DeviceFormat *format) format->size.width = pix->width; format->size.height = pix->height; format->fourcc = V4L2PixelFormat(pix->pixelformat); + format->colorSpace = getColorSpace(*pix); format->planesCount = pix->num_planes; for (unsigned int i = 0; i < format->planesCount; ++i) { @@ -862,6 +976,7 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set) pix->pixelformat = format->fourcc; pix->num_planes = format->planesCount; pix->field = V4L2_FIELD_NONE; + setColorSpace(format->colorSpace, *pix); ASSERT(pix->num_planes <= std::size(pix->plane_fmt)); @@ -910,6 +1025,7 @@ int V4L2VideoDevice::getFormatSingleplane(V4L2DeviceFormat *format) format->size.width = pix->width; format->size.height = pix->height; format->fourcc = V4L2PixelFormat(pix->pixelformat); + format->colorSpace = getColorSpace(*pix); format->planesCount = 1; format->planes[0].bpl = pix->bytesperline; format->planes[0].size = pix->sizeimage; @@ -929,6 +1045,7 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set) pix->pixelformat = format->fourcc; pix->bytesperline = format->planes[0].bpl; pix->field = V4L2_FIELD_NONE; + setColorSpace(format->colorSpace, *pix); ret = ioctl(set ? VIDIOC_S_FMT : VIDIOC_TRY_FMT, &v4l2Format); if (ret) { LOG(V4L2, Error) From patchwork Thu Aug 5 14:21:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 13214 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 E9B2BC3239 for ; Thu, 5 Aug 2021 14:22:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A64516884E; Thu, 5 Aug 2021 16:22:02 +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="othEu7FX"; dkim-atps=neutral Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 93E3568811 for ; Thu, 5 Aug 2021 16:22:00 +0200 (CEST) Received: by mail-wr1-x434.google.com with SMTP id c9so6773294wri.8 for ; Thu, 05 Aug 2021 07:22:00 -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=eUdwrCoEDCpXkt838qfY4LnGgP6uSf9WmosSrHwUl70=; b=othEu7FXGxXIca/+L5zyGJXmQvYw6zHNEd+JEqg9yW86cf2hudBu47jOfG8nwwE0r0 Nlx9aH9wYOIRsMBpEImOJ8dyV9hOKXLrJNnhMkJkUdSz9KA7VnbpOooomdZF1AjdVjMn qxfb2sfDWfv0O5hARoy9APmIQwvzGfWLU12a90h2Vnch+aKLMGyckARv8+V2WcC5hke7 ZPm5az6EZHDWQ16p4PmYzluDveAjOedtRoG1Ref/XGIYuSqAjMQHyI4D0phmGIMmPHKk esDN8djWFi/ewl9EYsCXFB53tZpatG9dQdo6zlOcIV1w6qrqVi5RtiDyWJedBdveOGpy 10cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eUdwrCoEDCpXkt838qfY4LnGgP6uSf9WmosSrHwUl70=; b=bFfg7wFZTQp5ObLR/0YNkNXf0Dg0onfA9LgCC/2ORskftR/VCpA9tom56dljcrGiKE 7iF8CjN8Z1MkSc8Iy7rkhbrel/SMFlUs72vqF4B8m8L8aojpn64U19rkjEH3mrnRpBVN rEsLHT6mFHP3DwHncggmBsHHfZubPsOZOALnQs9jwaSvJtkfJ9oq9X4JyuBzSQ3r1qsa z++vf9wiCrvDpw+bQgjU8/h3AqJTfXRVe+JkQenWRs+dmwAar5Xgr2QUoWyF4OwMD3up 1bRWJpbbG+caxBOCY+HyaGTZd6g1P1Uen1SU26Z21Nwk3NMcTL5Zhiy7S43Rra3JDbmR GG3w== X-Gm-Message-State: AOAM532m/9JmhZMczGdOhRuhH1v1F89u2M9rRLEXYYeMqy+9u/1MMhyb 5GM9Dz406KBcSLaivlcmL2wC+CBJZSt/kA== X-Google-Smtp-Source: ABdhPJwMLZ5rQZmd68idg43kOZBuKsx/xaGCnxCIQNu3yKF+BXbXoz/8eyWLfIa2IFLLloTSjufIUQ== X-Received: by 2002:a5d:6447:: with SMTP id d7mr5705367wrw.72.1628173319945; Thu, 05 Aug 2021 07:21:59 -0700 (PDT) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:1ce1:9965:4328:89c4]) by smtp.gmail.com with ESMTPSA id k17sm6410004wrw.53.2021.08.05.07.21.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Aug 2021 07:21:59 -0700 (PDT) From: David Plowman To: libcamera-devel@lists.libcamera.org Date: Thu, 5 Aug 2021 15:21:54 +0100 Message-Id: <20210805142154.20324-4-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210805142154.20324-1-david.plowman@raspberrypi.com> References: <20210805142154.20324-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 3/3] libcamera: pipeline: raspberrypi: Support colour spaces 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" The Raspberry Pi pipeline handler now sets colour spaces correctly. In generateConfiguration() is sets them to reasonable default values based on the stream role. Note how video recording streams use the "VIDEO" YCbCr encoding, which will later be fixed up according to the requested resolution. validate() and configure() check the colour space is valid, and also force all (non-raw) output streams to share the same colour space (which is a hardware restriction). Finally, the "VIDEO" YCbCr encoding is corrected by configure() to be one of REC601, REC709 or REC2020. Signed-off-by: David Plowman --- .../pipeline/raspberrypi/raspberrypi.cpp | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp index 0bab3bed..0119b1ca 100644 --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp @@ -281,6 +281,24 @@ RPiCameraConfiguration::RPiCameraConfiguration(const RPiCameraData *data) { } +static bool validateColorSpace(ColorSpace &colorSpace) +{ + const std::vector validColorSpaces = { + ColorSpace::JFIF, + ColorSpace::SMPTE170M, + ColorSpace::REC709, + ColorSpace::REC2020, + ColorSpace::VIDEO, + }; + auto it = std::find_if(validColorSpaces.begin(), validColorSpaces.end(), + [&colorSpace](const ColorSpace &item) { return colorSpace == item; }); + if (it != validColorSpaces.end()) + return true; + + colorSpace = ColorSpace::JFIF; + return false; +} + CameraConfiguration::Status RPiCameraConfiguration::validate() { Status status = Valid; @@ -346,6 +364,7 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() unsigned int rawCount = 0, outCount = 0, count = 0, maxIndex = 0; std::pair outSize[2]; Size maxSize; + ColorSpace colorSpace; /* colour space for all non-raw output streams */ for (StreamConfiguration &cfg : config_) { if (isRaw(cfg.pixelFormat)) { /* @@ -354,6 +373,7 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() */ V4L2VideoDevice::Formats fmts = data_->unicam_[Unicam::Image].dev()->formats(); V4L2DeviceFormat sensorFormat = findBestMode(fmts, cfg.size); + sensorFormat.colorSpace = ColorSpace::RAW; int ret = data_->unicam_[Unicam::Image].dev()->tryFormat(&sensorFormat); if (ret) return Invalid; @@ -392,6 +412,7 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() if (maxSize < cfg.size) { maxSize = cfg.size; maxIndex = outCount; + colorSpace = cfg.colorSpace; } outCount++; } @@ -405,6 +426,10 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() } } + /* Ensure the output colour space (if present) is one we handle. */ + if (outCount) + validateColorSpace(colorSpace); + /* * Now do any fixups needed. For the two ISP outputs, one stream must be * equal or smaller than the other in all dimensions. @@ -446,10 +471,26 @@ CameraConfiguration::Status RPiCameraConfiguration::validate() status = Adjusted; } + /* Output streams must share the same colour space. */ + if (cfg.colorSpace != colorSpace) { + LOG(RPI, Warning) << "Output stream " << (i == maxIndex ? 0 : 1) + << " colour space changed"; + cfg.colorSpace = colorSpace; + status = Adjusted; + } + V4L2DeviceFormat format; format.fourcc = dev->toV4L2PixelFormat(cfg.pixelFormat); format.size = cfg.size; + /* + * Request a sensible colour space. Note that "VIDEO" isn't a real + * encoding, so substitute something else sensible. + */ + format.colorSpace = colorSpace; + if (format.colorSpace.encoding == ColorSpace::Encoding::VIDEO) + format.colorSpace.encoding = ColorSpace::Encoding::REC601; + int ret = dev->tryFormat(&format); if (ret) return Invalid; @@ -475,6 +516,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, V4L2DeviceFormat sensorFormat; unsigned int bufferCount; PixelFormat pixelFormat; + ColorSpace colorSpace; V4L2VideoDevice::Formats fmts; Size size; @@ -490,6 +532,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, fmts = data->unicam_[Unicam::Image].dev()->formats(); sensorFormat = findBestMode(fmts, size); pixelFormat = sensorFormat.fourcc.toPixelFormat(); + colorSpace = ColorSpace::RAW; ASSERT(pixelFormat.isValid()); bufferCount = 2; rawCount++; @@ -501,6 +544,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, /* Return the largest sensor resolution. */ size = data->sensor_->resolution(); bufferCount = 1; + colorSpace = ColorSpace::JFIF; outCount++; break; @@ -517,6 +561,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, pixelFormat = formats::YUV420; size = { 1920, 1080 }; bufferCount = 4; + colorSpace = ColorSpace::VIDEO; outCount++; break; @@ -525,6 +570,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, pixelFormat = formats::ARGB8888; size = { 800, 600 }; bufferCount = 4; + colorSpace = ColorSpace::JFIF; outCount++; break; @@ -554,6 +600,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera, StreamConfiguration cfg(formats); cfg.size = size; cfg.pixelFormat = pixelFormat; + cfg.colorSpace = colorSpace; cfg.bufferCount = bufferCount; config->addConfiguration(cfg); } @@ -575,6 +622,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) Size maxSize, sensorSize; unsigned int maxIndex = 0; bool rawStream = false; + ColorSpace colorSpace; /* colour space for all non-raw output streams */ /* * Look for the RAW stream (if given) size as well as the largest @@ -593,14 +641,40 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) } else { if (cfg.size > maxSize) { maxSize = config->at(i).size; + colorSpace = config->at(i).colorSpace; maxIndex = i; } } } + if (maxSize.isNull()) { + /* + * No non-raw streams, so some will get made below. Doesn't matter + * what colour space we assign to them. + */ + colorSpace = ColorSpace::JFIF; + } else { + /* Make sure we can handle this colour space. */ + validateColorSpace(colorSpace); + + /* + * The "VIDEO" colour encoding means that we choose one of REC601, + * REC709 or REC2020 automatically according to the resolution. + */ + if (colorSpace.encoding == ColorSpace::Encoding::VIDEO) { + if (maxSize.width >= 3840) + colorSpace.encoding = ColorSpace::Encoding::REC2020; + else if (maxSize.width >= 1280) + colorSpace.encoding = ColorSpace::Encoding::REC709; + else + colorSpace.encoding = ColorSpace::Encoding::REC601; + } + } + /* First calculate the best sensor mode we can use based on the user request. */ V4L2VideoDevice::Formats fmts = data->unicam_[Unicam::Image].dev()->formats(); V4L2DeviceFormat sensorFormat = findBestMode(fmts, rawStream ? sensorSize : maxSize); + sensorFormat.colorSpace = ColorSpace::RAW; /* * Unicam image output format. The ISP input format gets set at start, @@ -638,11 +712,18 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) StreamConfiguration &cfg = config->at(i); if (isRaw(cfg.pixelFormat)) { + cfg.colorSpace = ColorSpace::RAW; cfg.setStream(&data->unicam_[Unicam::Image]); data->unicam_[Unicam::Image].setExternal(true); continue; } + /* All other streams share the same colour space. */ + if (cfg.colorSpace != colorSpace) { + LOG(RPI, Warning) << "Stream " << i << " colour space changed"; + cfg.colorSpace = colorSpace; + } + /* The largest resolution gets routed to the ISP Output 0 node. */ RPi::Stream *stream = i == maxIndex ? &data->isp_[Isp::Output0] : &data->isp_[Isp::Output1]; @@ -650,6 +731,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) V4L2PixelFormat fourcc = stream->dev()->toV4L2PixelFormat(cfg.pixelFormat); format.size = cfg.size; format.fourcc = fourcc; + format.colorSpace = cfg.colorSpace; LOG(RPI, Debug) << "Setting " << stream->name() << " to " << format.toString(); @@ -689,6 +771,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) format = {}; format.size = maxSize; format.fourcc = V4L2PixelFormat::fromPixelFormat(formats::YUV420, false); + format.colorSpace = colorSpace; ret = data->isp_[Isp::Output0].dev()->setFormat(&format); if (ret) { LOG(RPI, Error) @@ -718,6 +801,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config) const Size limit = maxDimensions.boundedToAspectRatio(format.size); output1Format.size = (format.size / 2).boundedTo(limit).alignedDownTo(2, 2); + output1Format.colorSpace = colorSpace; LOG(RPI, Debug) << "Setting ISP Output1 (internal) to " << output1Format.toString();