{"id":13952,"url":"https://patchwork.libcamera.org/api/1.1/patches/13952/?format=json","web_url":"https://patchwork.libcamera.org/patch/13952/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20210927123327.14554-3-david.plowman@raspberrypi.com>","date":"2021-09-27T12:33:26","name":"[libcamera-devel,v2,2/3] libcamera: Support passing ColorSpaces to V4L2 drivers","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"97e04c9bb23841985d1abe533ff70abce5686772","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/1.1/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/13952/mbox/","series":[{"id":2574,"url":"https://patchwork.libcamera.org/api/1.1/series/2574/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2574","date":"2021-09-27T12:33:24","name":"Colour spaces","version":2,"mbox":"https://patchwork.libcamera.org/series/2574/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13952/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13952/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 0DA6BBDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 27 Sep 2021 12:33:37 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 49F416919A;\n\tMon, 27 Sep 2021 14:33:36 +0200 (CEST)","from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com\n\t[IPv6:2a00:1450:4864:20::42d])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3119769189\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 27 Sep 2021 14:33:33 +0200 (CEST)","by mail-wr1-x42d.google.com with SMTP id i23so51759090wrb.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 27 Sep 2021 05:33:33 -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\tr9sm16285110wru.2.2021.09.27.05.33.32\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 27 Sep 2021 05:33:32 -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=\"B562Qh2v\"; 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=G0rebdG51e0aDi44uhZ3M4+B0tGBPEoMAQ34J//YoUY=;\n\tb=B562Qh2vnmIBz49G6sIaDqMLGWIFenrGfTJecWtuCYiO1M5m1AZ9ng4YTesLz1EdOK\n\t/hH0qXFpdSqOaaqI7gF3QWJy5DjrZLpvXsjPdaXxx19BqsWvOPyJXkET5wVEsIVYGoaM\n\t0OYr+LaSV761bjKNRfyUZjGBCTzc1r3cvcJFTSfVfvuIgtqH4eve3EQfSmBbCj75p+03\n\tpDyroIoHzxh/jNgPtjsm+Lt8xZrT3MiJx6I0R50Jf9HweLxz+RTfpDGlDLdsB4k7Lxug\n\tHE81HlCmqAkG0hPgFFo0cfZ1z2UnOFftlD1JnoHeRxDyAUm0oaf/Pfc2y87qS1tsR+Vh\n\t1PgQ==","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=G0rebdG51e0aDi44uhZ3M4+B0tGBPEoMAQ34J//YoUY=;\n\tb=hDGBxwMcuWoDk22k9YlnphxBLU7r51/TW4AfYGq4EViEIr+kkm5nT0Urq/Ehs+hAJq\n\tRss6PB0XQWqExmXzlolRssGo3JAxKWYkJxk2ffAfog8BCO+WEcNWo+rYPZ/u5vaVoexd\n\t59Ds72UyoN4UFs/Bs3+zfFTbPPZp2BiA971AFeWvDLPV2nblR70duPYNHT2IGpDrNmgP\n\tll4RKYL4+P0V7ey+kqJ16krXTKdT8hWFmfC0e/yD1/p3RjfxxOSxH2xLAwmYHj1pROlp\n\tbIu2m5j+gNmwqHLEoDBnTGtWMAU5NUXHnrmr81uWkg7kjTFAwzpx6DecQaIYcqjWzgJx\n\tBJRQ==","X-Gm-Message-State":"AOAM533IXS6m8BUfDo+8NbZSzc+tCzXHy+e0P5uM/6qzkBhQ3ml5WqiL\n\tg/wh26Dkf5FC4P+dX+OU0/wFwDfaEInAKiLs","X-Google-Smtp-Source":"ABdhPJy/3Fnr4diaeeMeGBg7ZnLU26BMUMZ+O1jWF4+HwbeUIZ9cSQ4Ed94+MQ0yeKgtqBoGdujlYw==","X-Received":"by 2002:a5d:64e6:: with SMTP id\n\tg6mr26718336wri.151.1632746012617; \n\tMon, 27 Sep 2021 05:33:32 -0700 (PDT)","From":"David Plowman <david.plowman@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 27 Sep 2021 13:33:26 +0100","Message-Id":"<20210927123327.14554-3-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<20210927123327.14554-1-david.plowman@raspberrypi.com>","References":"<20210927123327.14554-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 2/3] libcamera: Support passing\n\tColorSpaces to V4L2 drivers","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":"The ColorSpace class is added to the StreamConfiguration, and is now\npassed to V4L2 devices where it is handled appropriately. Note how\nthis means that the colour space is configured per-stream (though\nplatforms may restrict this).\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\n---\n include/libcamera/internal/v4l2_videodevice.h |   2 +\n include/libcamera/stream.h                    |   3 +\n src/libcamera/v4l2_videodevice.cpp            | 119 ++++++++++++++++++\n 3 files changed, 124 insertions(+)","diff":"diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h\nindex efe34d47..34cc9cdd 100644\n--- a/include/libcamera/internal/v4l2_videodevice.h\n+++ b/include/libcamera/internal/v4l2_videodevice.h\n@@ -20,6 +20,7 @@\n #include <libcamera/base/log.h>\n #include <libcamera/base/signal.h>\n \n+#include <libcamera/color_space.h>\n #include <libcamera/framebuffer.h>\n #include <libcamera/geometry.h>\n #include <libcamera/pixel_format.h>\n@@ -163,6 +164,7 @@ public:\n \n \tV4L2PixelFormat fourcc;\n \tSize size;\n+\tColorSpace colorSpace;\n \n \tstd::array<Plane, 3> planes;\n \tunsigned int planesCount = 0;\ndiff --git a/include/libcamera/stream.h b/include/libcamera/stream.h\nindex 0c55e716..131f7733 100644\n--- a/include/libcamera/stream.h\n+++ b/include/libcamera/stream.h\n@@ -12,6 +12,7 @@\n #include <string>\n #include <vector>\n \n+#include <libcamera/color_space.h>\n #include <libcamera/framebuffer.h>\n #include <libcamera/geometry.h>\n #include <libcamera/pixel_format.h>\n@@ -47,6 +48,8 @@ struct StreamConfiguration {\n \n \tunsigned int bufferCount;\n \n+\tColorSpace colorSpace;\n+\n \tStream *stream() const { return stream_; }\n \tvoid setStream(Stream *stream) { stream_ = stream; }\n \tconst StreamFormats &formats() const { return formats_; }\ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex ba5f88cd..3e6c8b87 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -369,6 +369,11 @@ bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const\n  * \\brief The image size in pixels\n  */\n \n+/**\n+ * \\var V4L2DeviceFormat::colorSpace\n+ * \\brief The color space of the pixels\n+ */\n+\n /**\n  * \\var V4L2DeviceFormat::fourcc\n  * \\brief The fourcc code describing the pixel encoding scheme\n@@ -752,6 +757,114 @@ int V4L2VideoDevice::getFormat(V4L2DeviceFormat *format)\n \t\treturn getFormatSingleplane(format);\n }\n \n+static const std::vector<std::pair<ColorSpace, v4l2_colorspace>> colorSpaceToV4l2 = {\n+\t{ ColorSpace::RAW, V4L2_COLORSPACE_RAW },\n+\t{ ColorSpace::JFIF, 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::Encoding, v4l2_ycbcr_encoding> encodingToV4l2 = {\n+\t{ ColorSpace::Encoding::REC601, V4L2_YCBCR_ENC_601 },\n+\t{ ColorSpace::Encoding::REC709, V4L2_YCBCR_ENC_709 },\n+\t{ ColorSpace::Encoding::REC2020, V4L2_YCBCR_ENC_BT2020 },\n+};\n+\n+static const std::map<ColorSpace::TransferFunction, v4l2_xfer_func> transferFunctionToV4l2 = {\n+\t{ ColorSpace::TransferFunction::IDENTITY, 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> rangeToV4l2 = {\n+\t{ ColorSpace::Range::FULL, V4L2_QUANTIZATION_FULL_RANGE },\n+\t{ ColorSpace::Range::LIMITED, V4L2_QUANTIZATION_LIM_RANGE },\n+};\n+\n+static const std::map<uint32_t, ColorSpace> v4l2ToColorSpace = {\n+\t{ V4L2_COLORSPACE_RAW, ColorSpace::RAW },\n+\t{ V4L2_COLORSPACE_JPEG, ColorSpace::JFIF },\n+\t{ V4L2_COLORSPACE_SRGB, ColorSpace::JFIF },\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::Encoding> v4l2ToEncoding = {\n+\t{ V4L2_YCBCR_ENC_601, ColorSpace::Encoding::REC601 },\n+\t{ V4L2_YCBCR_ENC_709, ColorSpace::Encoding::REC709 },\n+\t{ V4L2_YCBCR_ENC_BT2020, ColorSpace::Encoding::REC2020 },\n+};\n+\n+static const std::map<uint32_t, ColorSpace::TransferFunction> v4l2ToTransferFunction = {\n+\t{ V4L2_XFER_FUNC_NONE, ColorSpace::TransferFunction::IDENTITY },\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> v4l2ToRange = {\n+\t{ V4L2_QUANTIZATION_FULL_RANGE, ColorSpace::Range::FULL },\n+\t{ V4L2_QUANTIZATION_LIM_RANGE, ColorSpace::Range::LIMITED },\n+};\n+\n+template<typename T>\n+static void setColorSpace(const ColorSpace &colorSpace, T &v4l2PixFormat)\n+{\n+\tif (!colorSpace.isFullyDefined())\n+\t\tLOG(V4L2, Warning) << \"Setting non-fully defined colour space\"\n+\t\t\t\t   << colorSpace.toString();\n+\n+\tv4l2PixFormat.colorspace = V4L2_COLORSPACE_DEFAULT;\n+\tv4l2PixFormat.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;\n+\tv4l2PixFormat.xfer_func = V4L2_XFER_FUNC_DEFAULT;\n+\tv4l2PixFormat.quantization = V4L2_QUANTIZATION_DEFAULT;\n+\n+\tauto itColor = std::find_if(colorSpaceToV4l2.begin(), colorSpaceToV4l2.end(),\n+\t\t\t\t     [&colorSpace](const std::pair<ColorSpace, v4l2_colorspace> &item) { return colorSpace == item.first; });\n+\tif (itColor != colorSpaceToV4l2.end())\n+\t\tv4l2PixFormat.colorspace = itColor->second;\n+\n+\tauto itEncoding = encodingToV4l2.find(colorSpace.encoding);\n+\tif (itEncoding != encodingToV4l2.end())\n+\t\tv4l2PixFormat.ycbcr_enc = itEncoding->second;\n+\n+\tauto itTransfer = transferFunctionToV4l2.find(colorSpace.transferFunction);\n+\tif (itTransfer != transferFunctionToV4l2.end())\n+\t\tv4l2PixFormat.xfer_func = itTransfer->second;\n+\n+\tauto itRange = rangeToV4l2.find(colorSpace.range);\n+\tif (itRange != rangeToV4l2.end())\n+\t\tv4l2PixFormat.quantization = itRange->second;\n+}\n+\n+template<typename T>\n+static ColorSpace getColorSpace(const T &v4l2PixFormat)\n+{\n+\tColorSpace colorSpace;\n+\n+\tauto itColor = v4l2ToColorSpace.find(v4l2PixFormat.colorspace);\n+\tif (itColor != v4l2ToColorSpace.end())\n+\t\tcolorSpace = itColor->second;\n+\n+\tauto itEncoding = v4l2ToEncoding.find(v4l2PixFormat.ycbcr_enc);\n+\tif (itEncoding != v4l2ToEncoding.end())\n+\t\tcolorSpace.encoding = itEncoding->second;\n+\n+\tauto itTransfer = v4l2ToTransferFunction.find(v4l2PixFormat.xfer_func);\n+\tif (itTransfer != v4l2ToTransferFunction.end())\n+\t\tcolorSpace.transferFunction = itTransfer->second;\n+\n+\tauto itRange = v4l2ToRange.find(v4l2PixFormat.quantization);\n+\tif (itRange != v4l2ToRange.end())\n+\t\tcolorSpace.range = itRange->second;\n+\n+\tif (!colorSpace.isFullyDefined())\n+\t\tLOG(V4L2, Warning) << \"Returning non-fully defined colour space\"\n+\t\t\t\t   << colorSpace.toString();\n+\treturn colorSpace;\n+}\n+\n /**\n  * \\brief Try an image format on the V4L2 video device\n  * \\param[inout] format The image format to test applicability to the video device\n@@ -871,6 +984,7 @@ int V4L2VideoDevice::getFormatMultiplane(V4L2DeviceFormat *format)\n \tformat->size.width = pix->width;\n \tformat->size.height = pix->height;\n \tformat->fourcc = V4L2PixelFormat(pix->pixelformat);\n+\tformat->colorSpace = getColorSpace(*pix);\n \tformat->planesCount = pix->num_planes;\n \n \tfor (unsigned int i = 0; i < format->planesCount; ++i) {\n@@ -893,6 +1007,7 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set)\n \tpix->pixelformat = format->fourcc;\n \tpix->num_planes = format->planesCount;\n \tpix->field = V4L2_FIELD_NONE;\n+\tsetColorSpace(format->colorSpace, *pix);\n \n \tASSERT(pix->num_planes <= std::size(pix->plane_fmt));\n \n@@ -921,6 +1036,7 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set)\n \t\tformat->planes[i].bpl = pix->plane_fmt[i].bytesperline;\n \t\tformat->planes[i].size = pix->plane_fmt[i].sizeimage;\n \t}\n+\tformat->colorSpace = getColorSpace(*pix);\n \n \treturn 0;\n }\n@@ -941,6 +1057,7 @@ int V4L2VideoDevice::getFormatSingleplane(V4L2DeviceFormat *format)\n \tformat->size.width = pix->width;\n \tformat->size.height = pix->height;\n \tformat->fourcc = V4L2PixelFormat(pix->pixelformat);\n+\tformat->colorSpace = getColorSpace(*pix);\n \tformat->planesCount = 1;\n \tformat->planes[0].bpl = pix->bytesperline;\n \tformat->planes[0].size = pix->sizeimage;\n@@ -960,6 +1077,7 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set)\n \tpix->pixelformat = format->fourcc;\n \tpix->bytesperline = format->planes[0].bpl;\n \tpix->field = V4L2_FIELD_NONE;\n+\tsetColorSpace(format->colorSpace, *pix);\n \tret = ioctl(set ? VIDIOC_S_FMT : VIDIOC_TRY_FMT, &v4l2Format);\n \tif (ret) {\n \t\tLOG(V4L2, Error)\n@@ -978,6 +1096,7 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set)\n \tformat->planesCount = 1;\n \tformat->planes[0].bpl = pix->bytesperline;\n \tformat->planes[0].size = pix->sizeimage;\n+\tformat->colorSpace = getColorSpace(*pix);\n \n \treturn 0;\n }\n","prefixes":["libcamera-devel","v2","2/3"]}