From patchwork Tue Aug 30 07:47:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 17251 X-Patchwork-Delegate: umang.jain@ideasonboard.com 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 0EE7FC3272 for ; Tue, 30 Aug 2022 07:47:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CAD1B61FC7; Tue, 30 Aug 2022 09:47:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1661845661; bh=YqjMQP/yqXTzS70rx2uwu27GT7lMJ5U35FUq+fek4Gc=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=G8lyKVgVXHbvoCFOS1MRYFIZLkbYMCDdJsvSrS//QsTdbkFrPA7RehDvN3fiFKjUq paVnSjd77RKzTJ7XoMtZJA7xwquX9uBRPvWE1KSNZW0TszrNwtZLcGERlIfUZYQ5mm FmnJGFs9NXtPOlBYWkD7DIr9o4gLW/vHCFB0Bycys5rDWa6fM3Fbdka6KPt2WWxLo6 ii3tU77Z1KhcuqxU1WyxN7xS4yI/I31FnvKaU2r5ue6N1XTqEiHNSggQpY689KHREd u1rjCYKk1TNAU5VOTLZgxw6w9mP0Etc4rp5FLJKHmt5PimMm5mMj3USbblx3YLKU4u BP8HVH1nHTFDg== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 43B5561FB9 for ; Tue, 30 Aug 2022 09:47:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="vvBYdWQe"; dkim-atps=neutral Received: from umang.jainideasonboard.com (unknown [IPv6:2401:4900:1f3f:1548:78ac:4a3:edc3:c28a]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 66C41481; Tue, 30 Aug 2022 09:47:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1661845661; bh=YqjMQP/yqXTzS70rx2uwu27GT7lMJ5U35FUq+fek4Gc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vvBYdWQel3o1M4mVVJLg30SfHCfLw8J9+BvaGpw2+1FI7V6S6MjR5UxZBfii0Q+r2 RHT9zoeU/XQ8ymSUv1L12q6rVmJWGReddyMyXMHyTejVCFcXD27Ut6Z4VZHpxzPWoa hZs70G3qiNssuhrGxSg2t+NdSbCqVy9HNfUGZDSM= To: libcamera-devel@lists.libcamera.org Date: Tue, 30 Aug 2022 13:17:20 +0530 Message-Id: <20220830074725.1059643-3-umang.jain@ideasonboard.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220830074725.1059643-1-umang.jain@ideasonboard.com> References: <20220830074725.1059643-1-umang.jain@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 2/7] libcamera: v4l2_device: Adjust colorspace based on pixel format 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: , X-Patchwork-Original-From: Umang Jain via libcamera-devel From: Umang Jain Reply-To: Umang Jain Cc: rishikeshdonadkar@gmail.com Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" V4L2 has no "none" YCbCr encoding, and thus reports an encoding for all formats, including RGB and raw formats. This causes the libcamera ColorSpace to report incorrect encodings for non-YUV formats. Fix it by overriding the encoding reported by the kernel to YCbCrEncoding::None for non-YUV pixel formats and media bus formats. Similarly, override the quantization range of non-YUV formats to full range, as limited range isn't used for RGB and raw formats. Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart Reviewed-by: Paul Elder --- include/libcamera/internal/v4l2_device.h | 5 +++- src/libcamera/v4l2_device.cpp | 29 +++++++++++++++++++---- src/libcamera/v4l2_subdevice.cpp | 30 ++++++++++++++++++++++-- src/libcamera/v4l2_videodevice.cpp | 12 ++++++---- 4 files changed, 65 insertions(+), 11 deletions(-) diff --git a/include/libcamera/internal/v4l2_device.h b/include/libcamera/internal/v4l2_device.h index a52a5f2c..75304be1 100644 --- a/include/libcamera/internal/v4l2_device.h +++ b/include/libcamera/internal/v4l2_device.h @@ -22,6 +22,8 @@ #include #include +#include "libcamera/internal/formats.h" + namespace libcamera { class EventNotifier; @@ -59,7 +61,8 @@ protected: int fd() const { return fd_.get(); } template - static std::optional toColorSpace(const T &v4l2Format); + static std::optional toColorSpace(const T &v4l2Format, + PixelFormatInfo::ColourEncoding colourEncoding); template static int fromColorSpace(const std::optional &colorSpace, T &v4l2Format); diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index b22a981f..301620f8 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -24,6 +24,7 @@ #include #include +#include "libcamera/internal/formats.h" #include "libcamera/internal/sysfs.h" /** @@ -805,6 +806,7 @@ static const std::map rangeToV4l2 = { /** * \brief Convert the color space fields in a V4L2 format to a ColorSpace * \param[in] v4l2Format A V4L2 format containing color space information + * \param[in] colourEncoding Type of colour encoding * * The colorspace, ycbcr_enc, xfer_func and quantization fields within a * V4L2 format structure are converted to a corresponding ColorSpace. @@ -816,7 +818,8 @@ static const std::map rangeToV4l2 = { * \retval std::nullopt One or more V4L2 color space fields were not recognised */ template -std::optional V4L2Device::toColorSpace(const T &v4l2Format) +std::optional V4L2Device::toColorSpace(const T &v4l2Format, + PixelFormatInfo::ColourEncoding colourEncoding) { auto itColor = v4l2ToColorSpace.find(v4l2Format.colorspace); if (itColor == v4l2ToColorSpace.end()) @@ -839,6 +842,14 @@ std::optional V4L2Device::toColorSpace(const T &v4l2Format) return std::nullopt; colorSpace.ycbcrEncoding = itYcbcrEncoding->second; + + /* + * V4L2 has no "none" encoding, override the value returned by + * the kernel for non-YUV formats as YCbCr encoding isn't + * applicable in that case. + */ + if (colourEncoding != PixelFormatInfo::ColourEncodingYUV) + colorSpace.ycbcrEncoding = ColorSpace::YcbcrEncoding::None; } if (v4l2Format.quantization != V4L2_QUANTIZATION_DEFAULT) { @@ -847,14 +858,24 @@ std::optional V4L2Device::toColorSpace(const T &v4l2Format) return std::nullopt; colorSpace.range = itRange->second; + + /* + * "Limited" quantization range is only meant for YUV formats. + * Override the range to "Full" for all other formats. + */ + if (colourEncoding != PixelFormatInfo::ColourEncodingYUV) + colorSpace.range = ColorSpace::Range::Full; } return colorSpace; } -template std::optional V4L2Device::toColorSpace(const struct v4l2_pix_format &); -template std::optional V4L2Device::toColorSpace(const struct v4l2_pix_format_mplane &); -template std::optional V4L2Device::toColorSpace(const struct v4l2_mbus_framefmt &); +template std::optional V4L2Device::toColorSpace(const struct v4l2_pix_format &, + PixelFormatInfo::ColourEncoding); +template std::optional V4L2Device::toColorSpace(const struct v4l2_pix_format_mplane &, + PixelFormatInfo::ColourEncoding); +template std::optional V4L2Device::toColorSpace(const struct v4l2_mbus_framefmt &, + PixelFormatInfo::ColourEncoding); /** * \brief Fill in the color space fields of a V4L2 format from a ColorSpace diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp index 07a0bb07..95bfde34 100644 --- a/src/libcamera/v4l2_subdevice.cpp +++ b/src/libcamera/v4l2_subdevice.cpp @@ -503,7 +503,20 @@ int V4L2Subdevice::getFormat(unsigned int pad, V4L2SubdeviceFormat *format, format->size.width = subdevFmt.format.width; format->size.height = subdevFmt.format.height; format->mbus_code = subdevFmt.format.code; - format->colorSpace = toColorSpace(subdevFmt.format); + + PixelFormatInfo::ColourEncoding colourEncoding; + auto iter = formatInfoMap.find(subdevFmt.format.code); + if (iter != formatInfoMap.end()) { + colourEncoding = iter->second.colourEncoding; + } else { + LOG(V4L2, Warning) + << "Unknown subdev format " + << utils::hex(subdevFmt.format.code, 4) + << ", defaulting to RGB encoding"; + + colourEncoding = PixelFormatInfo::ColourEncodingRGB; + } + format->colorSpace = toColorSpace(subdevFmt.format, colourEncoding); return 0; } @@ -549,7 +562,20 @@ int V4L2Subdevice::setFormat(unsigned int pad, V4L2SubdeviceFormat *format, format->size.width = subdevFmt.format.width; format->size.height = subdevFmt.format.height; format->mbus_code = subdevFmt.format.code; - format->colorSpace = toColorSpace(subdevFmt.format); + + PixelFormatInfo::ColourEncoding colourEncoding; + auto iter = formatInfoMap.find(subdevFmt.format.code); + if (iter != formatInfoMap.end()) { + colourEncoding = iter->second.colourEncoding; + } else { + LOG(V4L2, Warning) + << "Unknown subdev format " + << utils::hex(subdevFmt.format.code, 4) + << ", defaulting to RGB encoding"; + + colourEncoding = PixelFormatInfo::ColourEncodingRGB; + } + format->colorSpace = toColorSpace(subdevFmt.format, colourEncoding); return 0; } diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 5a2d0e5b..0e3f5436 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -931,7 +931,8 @@ int V4L2VideoDevice::getFormatMultiplane(V4L2DeviceFormat *format) format->size.height = pix->height; format->fourcc = V4L2PixelFormat(pix->pixelformat); format->planesCount = pix->num_planes; - format->colorSpace = toColorSpace(*pix); + format->colorSpace = + toColorSpace(*pix, PixelFormatInfo::info(format->fourcc).colourEncoding); for (unsigned int i = 0; i < format->planesCount; ++i) { format->planes[i].bpl = pix->plane_fmt[i].bytesperline; @@ -987,7 +988,8 @@ int V4L2VideoDevice::trySetFormatMultiplane(V4L2DeviceFormat *format, bool set) format->planes[i].bpl = pix->plane_fmt[i].bytesperline; format->planes[i].size = pix->plane_fmt[i].sizeimage; } - format->colorSpace = toColorSpace(*pix); + format->colorSpace = + toColorSpace(*pix, PixelFormatInfo::info(format->fourcc).colourEncoding); return 0; } @@ -1011,7 +1013,8 @@ int V4L2VideoDevice::getFormatSingleplane(V4L2DeviceFormat *format) format->planesCount = 1; format->planes[0].bpl = pix->bytesperline; format->planes[0].size = pix->sizeimage; - format->colorSpace = toColorSpace(*pix); + format->colorSpace = + toColorSpace(*pix, PixelFormatInfo::info(format->fourcc).colourEncoding); return 0; } @@ -1053,7 +1056,8 @@ int V4L2VideoDevice::trySetFormatSingleplane(V4L2DeviceFormat *format, bool set) format->planesCount = 1; format->planes[0].bpl = pix->bytesperline; format->planes[0].size = pix->sizeimage; - format->colorSpace = toColorSpace(*pix); + format->colorSpace = + toColorSpace(*pix, PixelFormatInfo::info(format->fourcc).colourEncoding); return 0; }