From patchwork Thu Nov 18 15:19:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 14629 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 067B9BF415 for ; Thu, 18 Nov 2021 15:20:36 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B02C4603C2; Thu, 18 Nov 2021 16:20:35 +0100 (CET) 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="LcZPmmT5"; dkim-atps=neutral Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A693860376 for ; Thu, 18 Nov 2021 16:20:28 +0100 (CET) Received: by mail-wr1-x436.google.com with SMTP id b12so12253134wrh.4 for ; Thu, 18 Nov 2021 07:20:28 -0800 (PST) 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=XLKHkKrG/UEEcs/2bX824fXCZkEOH3339Y2W5nVKFIg=; b=LcZPmmT5guaAKEXzQ9fr6NSMmhzsdY2a5/AEvAT4QW6WUJseA/Vm670ie0EPCrQwdb C1dXc6kzL6bIYIIgc14dhKnqyd39pHdwlR3dSTljznj5U0PC/Xo1ChN8NvHwNy/9fvUX zqnrHdEkezHNExAo1ZqApaJTMSLM3P0EEjdDirob7Q6RjfsM4leR9MO4M7CctwXwjNCM QsdntB3j+PupVNLMKRnj8EJlAgH29g35e6Ib4BAQ88QnlG6/O3U5x5ipf69YuzifpMcg 6k5vfk5mccMwR4EW0Go7retcZZYl++tmuxBfhxQH2ntUsmICX9lmFRQSRqs1sBknyu2+ mWjg== 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=XLKHkKrG/UEEcs/2bX824fXCZkEOH3339Y2W5nVKFIg=; b=gWkRC/gY2pXuRaDqqASDO9ohOCtKDRhsVTIq9FyHjkauKzoEbdE5kJcJ8vwV/tUFy7 lysh7CNFYyu13xUFiqhmIkMjea3pWemciQhYbj9s2VZGBl63C6IhpdcgiNanzBlB2Roi QRUVZN5mpEnoalQQjNx4ILSobVxFDPUeFZkIueuuSIfyTL59vVeMYN7B9IC6+wDt3xnC 0M4R0ds3BZA3/xBuG3A6Xs44roRr+EuLwnOU2xitPLrAvGPWi4C9SWIto9V0IabQ/qwc MZR+5i354e9vjbM+u0UYDpgHxJnR5KXPpxVfym42Ll3Wfr2BU0qjRxKm2/MDvgG70du+ aF+A== X-Gm-Message-State: AOAM530SD163Joi5aWi4u38JEk0fL23jcFZqMhOlxf5a1QNz5nJ5N/YQ N8MNJJHTu/FnBGLnmnYTg5NxwQ== X-Google-Smtp-Source: ABdhPJxS7qv7lBYa7xwgcTVMyjpOQbcxYZpAYia/uJ97C0TYwe4JsZefwRB1zWiAb6uohDvoX7rj7Q== X-Received: by 2002:a5d:6152:: with SMTP id y18mr32364355wrt.271.1637248828146; Thu, 18 Nov 2021 07:20:28 -0800 (PST) Received: from pi4-davidp.pitowers.org ([2a00:1098:3142:14:1ce1:9965:4328:89c4]) by smtp.gmail.com with ESMTPSA id p12sm147367wro.33.2021.11.18.07.20.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Nov 2021 07:20:27 -0800 (PST) From: David Plowman To: laurent.pinchart@ideasonboard.com, kieran.bingham@ideasonboard.com, hverkuil-cisco@xs4all.nl, tfiga@google.com, jacopo@jmondi.org, naush@raspberrypi.com, libcamera-devel@lists.libcamera.org Date: Thu, 18 Nov 2021 15:19:30 +0000 Message-Id: <20211118151933.15627-5-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20211118151933.15627-1-david.plowman@raspberrypi.com> References: <20211118151933.15627-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 4/7] libcamera: Support passing ColorSpaces to V4L2 video devices 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 from the StreamConfiguration is now handled appropriately in the V4L2VideoDevice. Signed-off-by: David Plowman --- include/libcamera/internal/v4l2_videodevice.h | 2 + src/libcamera/v4l2_videodevice.cpp | 53 +++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index a1c458e4..00bc50e7 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 @@ -167,6 +168,7 @@ public: V4L2PixelFormat fourcc; Size size; + std::optional colorSpace; std::array planes; unsigned int planesCount = 0; diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 4f04212d..e03ff8b5 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -370,17 +370,27 @@ bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const * \brief The plane line stride (in bytes) */ +/** + * \var V4L2DeviceFormat::fourcc + * \brief The fourcc code describing the pixel encoding scheme + * + * The fourcc code, as defined by the V4L2 API with the V4L2_PIX_FMT_* macros, + * that identifies the image format pixel encoding scheme. + */ + /** * \var V4L2DeviceFormat::size * \brief The image size in pixels */ /** - * \var V4L2DeviceFormat::fourcc - * \brief The fourcc code describing the pixel encoding scheme + * \var V4L2DeviceFormat::colorSpace + * \brief The color space of the pixels * - * The fourcc code, as defined by the V4L2 API with the V4L2_PIX_FMT_* macros, - * that identifies the image format pixel encoding scheme. + * The color space of the image. When setting the format this may be + * unset, in which case the driver gets to use its default color space. + * After being set, this value should contain the color space that the + * was actually used. */ /** @@ -879,6 +889,10 @@ int V4L2VideoDevice::getFormatMultiplane(V4L2DeviceFormat *format) format->fourcc = V4L2PixelFormat(pix->pixelformat); format->planesCount = pix->num_planes; + format->colorSpace = toColorSpace(*pix); + if (!format->colorSpace) + LOG(V4L2, Error) << "Retrieved unrecognised color space"; + for (unsigned int i = 0; i < format->planesCount; ++i) { format->planes[i].bpl = pix->plane_fmt[i].bytesperline; format->planes[i].size = pix->plane_fmt[i].sizeimage; @@ -893,6 +907,9 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set) struct v4l2_pix_format_mplane *pix = &v4l2Format.fmt.pix_mp; int ret; + if (!format->colorSpace) + LOG(V4L2, Warning) << "Setting undefined color space"; + v4l2Format.type = bufferType_; pix->width = format->size.width; pix->height = format->size.height; @@ -900,6 +917,12 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set) pix->num_planes = format->planesCount; pix->field = V4L2_FIELD_NONE; + ret = fromColorSpace(format->colorSpace, *pix); + if (ret < 0) + LOG(V4L2, Warning) + << "Setting color space unrecognised by V4L2: " + << ColorSpace::toString(format->colorSpace); + ASSERT(pix->num_planes <= std::size(pix->plane_fmt)); for (unsigned int i = 0; i < pix->num_planes; ++i) { @@ -928,6 +951,10 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set) format->planes[i].size = pix->plane_fmt[i].sizeimage; } + format->colorSpace = toColorSpace(*pix); + if (!format->colorSpace) + LOG(V4L2, Warning) << "Unrecognised color space has been set"; + return 0; } @@ -951,6 +978,10 @@ int V4L2VideoDevice::getFormatSingleplane(V4L2DeviceFormat *format) format->planes[0].bpl = pix->bytesperline; format->planes[0].size = pix->sizeimage; + format->colorSpace = toColorSpace(*pix); + if (!format->colorSpace) + LOG(V4L2, Error) << "Retrieved unrecognised color space"; + return 0; } @@ -960,12 +991,22 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set) struct v4l2_pix_format *pix = &v4l2Format.fmt.pix; int ret; + if (!format->colorSpace) + LOG(V4L2, Error) << "Trying to set unrecognised color space"; + v4l2Format.type = bufferType_; pix->width = format->size.width; pix->height = format->size.height; pix->pixelformat = format->fourcc; pix->bytesperline = format->planes[0].bpl; pix->field = V4L2_FIELD_NONE; + + ret = fromColorSpace(format->colorSpace, *pix); + if (ret < 0) + LOG(V4L2, Warning) + << "Set color space unrecognised by V4L2: " + << ColorSpace::toString(format->colorSpace); + ret = ioctl(set ? VIDIOC_S_FMT : VIDIOC_TRY_FMT, &v4l2Format); if (ret) { LOG(V4L2, Error) @@ -985,6 +1026,10 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set) format->planes[0].bpl = pix->bytesperline; format->planes[0].size = pix->sizeimage; + format->colorSpace = toColorSpace(*pix); + if (!format->colorSpace) + LOG(V4L2, Error) << "Undefined color space has been set"; + return 0; }