{"id":18169,"url":"https://patchwork.libcamera.org/api/patches/18169/?format=json","web_url":"https://patchwork.libcamera.org/patch/18169/","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":"<20230120162258.7039-2-david.plowman@raspberrypi.com>","date":"2023-01-20T16:22:58","name":"[libcamera-devel,v3,1/1] pipeline: raspberrypi: Fix handling of colour spaces","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"5345f096ad692dee40e4ce692a47833a5af3fa84","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18169/mbox/","series":[{"id":3717,"url":"https://patchwork.libcamera.org/api/series/3717/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3717","date":"2023-01-20T16:22:57","name":"Fix Raspberry Pi colour spaces","version":3,"mbox":"https://patchwork.libcamera.org/series/3717/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18169/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18169/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 CC058C3295\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 20 Jan 2023 16:23:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AF10F625E8;\n\tFri, 20 Jan 2023 17:23:05 +0100 (CET)","from mail-wr1-x431.google.com (mail-wr1-x431.google.com\n\t[IPv6:2a00:1450:4864:20::431])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 132C261EFD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 20 Jan 2023 17:23:03 +0100 (CET)","by mail-wr1-x431.google.com with SMTP id h12so1309766wrv.10\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 20 Jan 2023 08:23:03 -0800 (PST)","from pi4-davidp.pitowers.org\n\t([2a00:1098:3142:14:e4a2:3070:eea4:e434])\n\tby smtp.gmail.com with ESMTPSA id\n\tco22-20020a0560000a1600b002a01e64f7a1sm12886596wrb.88.2023.01.20.08.23.01\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 20 Jan 2023 08:23:02 -0800 (PST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1674231785;\n\tbh=xu6QiBb8ZaqJ53QUDRFV5+0Fmt6NmkLQwqQKAam2u3g=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=Qu6if45+y4yOCIU8dKGZYLS6jKWZn0B6g9tn3/rplS4Tc3RuRoqIsbLg4HyFGrnKd\n\tKtz4Ra/XkEXRDh6BaYIS11fHVeaG1hG73dybWUTHyOotrptTo4qLKP8RQmi8VK4gNs\n\tQ9SyS7+FI3XPRqadkl24bdFvocxV9sGhdxXO2pxKly5cIgma2FoBF5YAlDUY5j+a6b\n\tBmuAO53OkCS57JWahmXtLcVDNEQHgED/6aZq3KEvTZ3ihJVE8BM+23r123/h4kW65Q\n\tYmkT5tc99ttDZcQPtCghStcU2inF21uqsaMV3hGY+2DbtRhY0tH4j9a4pomMVGTYUu\n\tAA3ctLD07z3cw==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=ODoZgKfpxPA6bRL6CULiMN9sH5VsFqJLHMoJRBga7sk=;\n\tb=FMy3namUiun+CAWCXD92JQ+sQtZMuu7kWQnt9CNCha9PwySZuRG2ne2IU5SNXm7RdR\n\tGgA3j+XjwNlu4HktbVOy7t/6cD8NrMTNPW2XsIX4ZFiw6i+3hFreQ+5pc8sy0kLvr8IG\n\t448vIdMETRGOC8nxXcnFwStrBlqH4YQ6urQE2sQXuz24PYONg0gVomrvpGSt6qC32d0/\n\tJdNdkxVvcijp7RaDTIBAkWtCb4iQ5zmJD23jspghhhnwT+iuScfsQiwZIlsb0QqDynUK\n\tdkG6AIRRg0UwdDk55V2CTiKYBpg9fa537DWg/8hhTz+2gR3vW/n0gFusW7rZOy5yQ4h7\n\tDPbA=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"FMy3namU\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=ODoZgKfpxPA6bRL6CULiMN9sH5VsFqJLHMoJRBga7sk=;\n\tb=Cb9EPlcqAk7ycsF7ZKKFyzl4Tq4ExpZQx35ukAnIlZA5eK4MEeDp1BLzDeDgnbBfdO\n\t8UyI1znszbivxP707hHwTBKLttvAkNq2QWYrNyNNlmXyZTHVwlNr5wuZG7JsAyvlb/94\n\t4BP/5EcVyqXk+aTHoj5GQpQYwIW+y88Y+ZVqrsUG0nGk6J0Rc6fjZFi8QnozG7CIQ85a\n\tAbW13OzpNWaD1dpiOg6EXfxhyzRnCQBpXMSnmmEL9jPdcuR/Lhb0+LdbW9uXbZgKAl6D\n\tOSfEjDzRxnTCrgNBjOLmI5IrTkGRZj0T/VoUkL1FqRdH4gcyaS7OD0lhF3jRwP1a5eSt\n\tnGeA==","X-Gm-Message-State":"AFqh2krXgR3DSFvcI86NTwJBy++LmZ0OI9LcvIY3uwk8wVP410SABtxT\n\to+80FLTcrr4+tKRDsqJA3vNPzQbELJcHc//1","X-Google-Smtp-Source":"AMrXdXued6fR/1NvmA5giejGzwYQfKNfp0bIzqivTeB0G1/b69KEpkDMMxeDh3dQMOYkXnbjvdxFuw==","X-Received":"by 2002:adf:fe03:0:b0:2be:516a:e6da with SMTP id\n\tn3-20020adffe03000000b002be516ae6damr3743653wrr.14.1674231782437; \n\tFri, 20 Jan 2023 08:23:02 -0800 (PST)","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 20 Jan 2023 16:22:58 +0000","Message-Id":"<20230120162258.7039-2-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.30.2","In-Reply-To":"<20230120162258.7039-1-david.plowman@raspberrypi.com>","References":"<20230120162258.7039-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 1/1] pipeline: raspberrypi: Fix\n\thandling of colour spaces","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>","From":"David Plowman via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"David Plowman <david.plowman@raspberrypi.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"We implement a custom validateColorSpaces method that forces all\n(non-raw) streams to same colour space, whilst distinguishing RGB\nstreams from YUV ones, as the former must have the YCbCr encoding and\nrange over-written.\n\nWhen we apply the colour space, we always send the full YUV version as\nthat gets converted correctly to what our hardware drivers expect. It\nis also careful to check what comes back as the YCbCr information gets\noverwritten again on the way back.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n---\n .../pipeline/raspberrypi/raspberrypi.cpp      | 115 +++++++++++++++++-\n 1 file changed, 114 insertions(+), 1 deletion(-)","diff":"diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 8569df17..f768dced 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -310,6 +310,7 @@ class RPiCameraConfiguration : public CameraConfiguration\n public:\n \tRPiCameraConfiguration(const RPiCameraData *data);\n \n+\tCameraConfiguration::Status validateColorSpaces(ColorSpaceFlags flags);\n \tStatus validate() override;\n \n \t/* Cache the combinedTransform_ that will be applied to the sensor */\n@@ -317,6 +318,13 @@ public:\n \n private:\n \tconst RPiCameraData *data_;\n+\n+\t/*\n+\t * Store the colour spaces that all our streams will have. RGB format streams\n+\t * will be the same but need the YCbCr fields cleared.\n+\t */\n+\tstd::optional<ColorSpace> yuvColorSpace_;\n+\tstd::optional<ColorSpace> rgbColorSpace_;\n };\n \n class PipelineHandlerRPi : public PipelineHandler\n@@ -357,6 +365,105 @@ RPiCameraConfiguration::RPiCameraConfiguration(const RPiCameraData *data)\n {\n }\n \n+static const std::vector<ColorSpace> validColorSpaces = {\n+\tColorSpace::Sycc,\n+\tColorSpace::Smpte170m,\n+\tColorSpace::Rec709\n+};\n+\n+static std::optional<ColorSpace> findValidColorSpace(const ColorSpace &colourSpace)\n+{\n+\tfor (auto cs : validColorSpaces) {\n+\t\tif (colourSpace.primaries == cs.primaries &&\n+\t\t    colourSpace.transferFunction == cs.transferFunction)\n+\t\t\treturn cs;\n+\t}\n+\n+\treturn std::nullopt;\n+}\n+\n+static bool isRgb(const PixelFormat &pixFmt)\n+{\n+\tconst PixelFormatInfo &info = PixelFormatInfo::info(pixFmt);\n+\treturn info.colourEncoding == PixelFormatInfo::ColourEncodingRGB;\n+}\n+\n+static bool isYuv(const PixelFormat &pixFmt)\n+{\n+\t/* The code below would return true for raw mono streams, so weed those out first. */\n+\tif (isRaw(pixFmt))\n+\t\treturn false;\n+\n+\tconst PixelFormatInfo &info = PixelFormatInfo::info(pixFmt);\n+\treturn info.colourEncoding == PixelFormatInfo::ColourEncodingYUV;\n+}\n+\n+/*\n+ * Raspberry Pi drivers expect the following colour spaces:\n+ * - V4L2_COLORSPACE_RAW for raw streams.\n+ * - One of V4L2_COLORSPACE_JPEG, V4L2_COLORSPACE_SMPTE170M, V4L2_COLORSPACE_REC709 for\n+ *   non-raw streams. Other fields such as transfer function, YCbCr encoding and\n+ *   quantisation are not used.\n+ *\n+ * The libcamera colour spaces that we wish to use corresponding to these are therefore:\n+ * - ColorSpace::Raw for V4L2_COLORSPACE_RAW\n+ * - ColorSpace::Sycc for V4L2_COLORSPACE_JPEG\n+ * - ColorSpace::Smpte170m for V4L2_COLORSPACE_SMPTE170M\n+ * - ColorSpace::Rec709 for V4L2_COLORSPACE_REC709\n+ */\n+\n+CameraConfiguration::Status RPiCameraConfiguration::validateColorSpaces([[maybe_unused]] ColorSpaceFlags flags)\n+{\n+\tStatus status = Valid;\n+\tyuvColorSpace_.reset();\n+\n+\tfor (auto cfg : config_) {\n+\t\t/* First fix up raw streams to have the \"raw\" colour space. */\n+\t\tif (isRaw(cfg.pixelFormat)) {\n+\t\t\t/* If there was no value here, that doesn't count as \"adjusted\". */\n+\t\t\tif (cfg.colorSpace && cfg.colorSpace != ColorSpace::Raw) {\n+\t\t\t\tstatus = Adjusted;\n+\t\t\t\tcfg.colorSpace = ColorSpace::Raw;\n+\t\t\t}\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\t/* Next we need to find our shared colour space. The first valid one will do. */\n+\t\tif (cfg.colorSpace && !yuvColorSpace_)\n+\t\t\tyuvColorSpace_ = findValidColorSpace(cfg.colorSpace.value());\n+\t}\n+\n+\t/* If no colour space was given anywhere, choose sYCC. */\n+\tif (!yuvColorSpace_)\n+\t\tyuvColorSpace_ = ColorSpace::Sycc;\n+\n+\t/* Note the version of this that any RGB streams will have to use. */\n+\trgbColorSpace_ = yuvColorSpace_;\n+\trgbColorSpace_->ycbcrEncoding = ColorSpace::YcbcrEncoding::None;\n+\trgbColorSpace_->range = ColorSpace::Range::Full;\n+\n+\t/* Go through the streams again and force everyone to the same colour space. */\n+\tfor (auto cfg : config_) {\n+\t\tif (cfg.colorSpace == ColorSpace::Raw)\n+\t\t\tcontinue;\n+\n+\t\tif (isYuv(cfg.pixelFormat) && cfg.colorSpace != yuvColorSpace_) {\n+\t\t\t/* Again, no value means \"not adjusted\". */\n+\t\t\tif (cfg.colorSpace)\n+\t\t\t\tstatus = Adjusted;\n+\t\t\tcfg.colorSpace = yuvColorSpace_;\n+\t\t}\n+\t\tif (isRgb(cfg.pixelFormat) && cfg.colorSpace != rgbColorSpace_) {\n+\t\t\t/* Be nice, and let the YUV version count as non-adjusted too. */\n+\t\t\tif (cfg.colorSpace && cfg.colorSpace != yuvColorSpace_)\n+\t\t\t\tstatus = Adjusted;\n+\t\t\tcfg.colorSpace = rgbColorSpace_;\n+\t\t}\n+\t}\n+\n+\treturn status;\n+}\n+\n CameraConfiguration::Status RPiCameraConfiguration::validate()\n {\n \tStatus status = Valid;\n@@ -533,7 +640,8 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \t\tV4L2DeviceFormat format;\n \t\tformat.fourcc = dev->toV4L2PixelFormat(cfg.pixelFormat);\n \t\tformat.size = cfg.size;\n-\t\tformat.colorSpace = cfg.colorSpace;\n+\t\t/* We want to send the associated YCbCr info through to the driver. */\n+\t\tformat.colorSpace = yuvColorSpace_;\n \n \t\tLOG(RPI, Debug)\n \t\t\t<< \"Try color space \" << ColorSpace::toString(cfg.colorSpace);\n@@ -542,6 +650,11 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n \t\tif (ret)\n \t\t\treturn Invalid;\n \n+\t\t/*\n+\t\t * But for RGB streams, the YCbCr info gets overwritten on the way back\n+\t\t * so we must check against what the stream cfg says, not what we actually\n+\t\t * requested (which carefully included the YCbCr info)!\n+\t\t */\n \t\tif (cfg.colorSpace != format.colorSpace) {\n \t\t\tstatus = Adjusted;\n \t\t\tLOG(RPI, Debug)\n","prefixes":["libcamera-devel","v3","1/1"]}