[{"id":21640,"web_url":"https://patchwork.libcamera.org/comment/21640/","msgid":"<Ya9oLbFTmzVNCS91@pendragon.ideasonboard.com>","date":"2021-12-07T13:57:01","subject":"Re: [libcamera-devel] [PATCH v9 8/8] libcamera: pipeline:\n\traspberrypi: Support color spaces","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi David,\n\nThank you for the patch.\n\nOn Mon, Dec 06, 2021 at 10:50:31AM +0000, David Plowman wrote:\n> The Raspberry Pi pipeline handler now sets color spaces correctly.\n> \n> In generateConfiguration() it sets them to reasonable default values\n> based on the stream role.\n> \n> validate() now calls validateColorSpaces() to ensure that the\n> requested color spaces are sensible, before proceeding to check what\n> the hardware can deliver.\n> \n> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>\n> ---\n>  .../pipeline/raspberrypi/raspberrypi.cpp      | 43 +++++++++++++++++++\n>  1 file changed, 43 insertions(+)\n> \n> diff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> index 321b72ad..eb4fbf90 100644\n> --- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> +++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n> @@ -97,6 +97,7 @@ V4L2DeviceFormat toV4L2DeviceFormat(const V4L2SubdeviceFormat &format,\n>  \n>  \tdeviceFormat.fourcc = V4L2PixelFormat::fromPixelFormat(pix);\n>  \tdeviceFormat.size = format.size;\n> +\tdeviceFormat.colorSpace = format.colorSpace;\n>  \treturn deviceFormat;\n>  }\n>  \n> @@ -133,6 +134,7 @@ V4L2SubdeviceFormat findBestFormat(const SensorFormats &formatsMap, const Size &\n>  {\n>  \tdouble bestScore = std::numeric_limits<double>::max(), score;\n>  \tV4L2SubdeviceFormat bestFormat;\n> +\tbestFormat.colorSpace = ColorSpace::Raw;\n>  \n>  \tconstexpr float penaltyAr = 1500.0;\n>  \tconstexpr float penaltyBitDepth = 500.0;\n> @@ -330,6 +332,8 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n>  \tif (config_.empty())\n>  \t\treturn Invalid;\n>  \n> +\tstatus = validateColorSpaces(true);\n\nWouldn't it be better to validate colour spaces after pixel formats have\nbeen validated ? Otherwise your isRaw() check during colour space\nvalidation may not match with the pixel format after validation.\n\n> +\n>  \t/*\n>  \t * What if the platform has a non-90 degree rotation? We can't even\n>  \t * \"adjust\" the configuration and carry on. Alternatively, raising an\n> @@ -497,11 +501,23 @@ CameraConfiguration::Status RPiCameraConfiguration::validate()\n>  \t\tV4L2DeviceFormat format;\n>  \t\tformat.fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);\n>  \t\tformat.size = cfg.size;\n> +\t\tformat.colorSpace = cfg.colorSpace;\n\nI'd add a blank line here.\n\n> +\t\tLOG(RPI, Debug)\n> +\t\t\t<< \"Try color space \" << ColorSpace::toString(cfg.colorSpace);\n>  \n>  \t\tint ret = dev->tryFormat(&format);\n>  \t\tif (ret)\n>  \t\t\treturn Invalid;\n>  \n> +\t\tif (!format.colorSpace || cfg.colorSpace != format.colorSpace) {\n> +\t\t\tstatus = Adjusted;\n> +\t\t\tLOG(RPI, Warning)\n> +\t\t\t\t<< \"Color space changed from \"\n> +\t\t\t\t<< ColorSpace::toString(cfg.colorSpace) << \" to \"\n> +\t\t\t\t<< ColorSpace::toString(format.colorSpace);\n\nWarning or Debug ?\n\n> +\t\t}\n\nAnd here too.\n\n> +\t\tcfg.colorSpace = format.colorSpace;\n> +\n>  \t\tcfg.stride = format.planes[0].bpl;\n>  \t\tcfg.frameSize = format.planes[0].size;\n>  \n> @@ -525,6 +541,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n>  \tPixelFormat pixelFormat;\n>  \tV4L2VideoDevice::Formats fmts;\n>  \tSize size;\n> +\tstd::optional<ColorSpace> colorSpace;\n>  \n>  \tif (roles.empty())\n>  \t\treturn config;\n> @@ -539,6 +556,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n>  \t\t\tpixelFormat = mbusCodeToPixelFormat(sensorFormat.mbus_code,\n>  \t\t\t\t\t\t\t    BayerFormat::Packing::CSI2);\n>  \t\t\tASSERT(pixelFormat.isValid());\n> +\t\t\tcolorSpace = ColorSpace::Raw;\n>  \t\t\tbufferCount = 2;\n>  \t\t\trawCount++;\n>  \t\t\tbreak;\n> @@ -546,6 +564,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n>  \t\tcase StreamRole::StillCapture:\n>  \t\t\tfmts = data->isp_[Isp::Output0].dev()->formats();\n>  \t\t\tpixelFormat = formats::NV12;\n> +\t\t\tcolorSpace = ColorSpace::Jpeg;\n>  \t\t\t/* Return the largest sensor resolution. */\n>  \t\t\tsize = data->sensor_->resolution();\n>  \t\t\tbufferCount = 1;\n> @@ -563,6 +582,8 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n>  \t\t\t */\n>  \t\t\tfmts = data->isp_[Isp::Output0].dev()->formats();\n>  \t\t\tpixelFormat = formats::YUV420;\n> +\t\t\t/* This will be reasonable for many applications. */\n> +\t\t\tcolorSpace = ColorSpace::Rec709;\n\nWhy does NV12 produce Jpeg and YUV420 Rec709, could you document the\nrationale ?\n\n>  \t\t\tsize = { 1920, 1080 };\n>  \t\t\tbufferCount = 4;\n>  \t\t\toutCount++;\n> @@ -571,6 +592,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n>  \t\tcase StreamRole::Viewfinder:\n>  \t\t\tfmts = data->isp_[Isp::Output0].dev()->formats();\n>  \t\t\tpixelFormat = formats::ARGB8888;\n> +\t\t\tcolorSpace = ColorSpace::Jpeg;\n>  \t\t\tsize = { 800, 600 };\n>  \t\t\tbufferCount = 4;\n>  \t\t\toutCount++;\n> @@ -602,6 +624,7 @@ CameraConfiguration *PipelineHandlerRPi::generateConfiguration(Camera *camera,\n>  \t\tStreamConfiguration cfg(formats);\n>  \t\tcfg.size = size;\n>  \t\tcfg.pixelFormat = pixelFormat;\n> +\t\tcfg.colorSpace = colorSpace;\n>  \t\tcfg.bufferCount = bufferCount;\n>  \t\tconfig->addConfiguration(cfg);\n>  \t}\n> @@ -709,6 +732,7 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n>  \t\tV4L2PixelFormat fourcc = V4L2PixelFormat::fromPixelFormat(cfg.pixelFormat);\n>  \t\tformat.size = cfg.size;\n>  \t\tformat.fourcc = fourcc;\n> +\t\tformat.colorSpace = cfg.colorSpace;\n>  \n>  \t\tLOG(RPI, Debug) << \"Setting \" << stream->name() << \" to \"\n>  \t\t\t\t<< format.toString();\n> @@ -724,6 +748,23 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n>  \t\t\treturn -EINVAL;\n>  \t\t}\n>  \n> +\t\tif (!format.colorSpace || cfg.colorSpace != format.colorSpace) {\n> +\t\t\t/*\n> +\t\t\t * We should have been through validate() before so this\n> +\t\t\t * shouldn't be possible, but we mustn't sweep color space\n> +\t\t\t * problems under the carpet.\n> +\t\t\t */\n\nThere's a guarantee that the configuration has been validated, so this\nreally can't happen.\n\n> +\t\t\tLOG(RPI, Warning)\n> +\t\t\t\t<< \"Unexpected color space (\"\n> +\t\t\t\t<< ColorSpace::toString(cfg.colorSpace) << \" to \"\n> +\t\t\t\t<< ColorSpace::toString(format.colorSpace) << \") in stream \"\n> +\t\t\t\t<< stream->name();\n> +\t\t\tcfg.colorSpace = format.colorSpace;\n> +\t\t}\n> +\t\tLOG(RPI, Debug)\n> +\t\t\t<< \"Stream \" << stream->name() << \" has color space \"\n> +\t\t\t<< ColorSpace::toString(cfg.colorSpace);\n> +\n>  \t\tcfg.setStream(stream);\n>  \t\tstream->setExternal(true);\n>  \n> @@ -748,6 +789,8 @@ int PipelineHandlerRPi::configure(Camera *camera, CameraConfiguration *config)\n>  \t\tformat = {};\n>  \t\tformat.size = maxSize;\n>  \t\tformat.fourcc = V4L2PixelFormat::fromPixelFormat(formats::YUV420);\n> +\t\t/* No one asked for output, so the color space doesn't matter. */\n> +\t\tformat.colorSpace = ColorSpace::Jpeg;\n>  \t\tret = data->isp_[Isp::Output0].dev()->setFormat(&format);\n>  \t\tif (ret) {\n>  \t\t\tLOG(RPI, Error)","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 846C1BDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  7 Dec 2021 13:57:32 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 37C496086A;\n\tTue,  7 Dec 2021 14:57:32 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1218660592\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  7 Dec 2021 14:57:31 +0100 (CET)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 827D8556;\n\tTue,  7 Dec 2021 14:57:30 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"FRbhsR65\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638885450;\n\tbh=Oaw+wJO46cL1ouAhikaJbS94Ip/VSGa5S62Dzvu0nng=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=FRbhsR65BCw1N9ZrPQs0fOkoLU6PnoCmUVICN3YXinU89j6EzDvivbyKUyK4qQN70\n\tnDJ5wOrAYFKZEZ8XySGvwH4MwdwEYb3q4qyvAZP6N+KLtsmRfyM2Hsl9hdAx1yqRhd\n\tqa0G8mWsiqyBdykmTy7AAEaKyIgcu4JpqcuivAVQ=","Date":"Tue, 7 Dec 2021 15:57:01 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"David Plowman <david.plowman@raspberrypi.com>","Message-ID":"<Ya9oLbFTmzVNCS91@pendragon.ideasonboard.com>","References":"<20211206105032.13876-1-david.plowman@raspberrypi.com>\n\t<20211206105032.13876-9-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211206105032.13876-9-david.plowman@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v9 8/8] libcamera: pipeline:\n\traspberrypi: Support color 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>","Cc":"tfiga@google.com, libcamera-devel@lists.libcamera.org,\n\thverkuil-cisco@xs4all.nl","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]