{"id":16253,"url":"https://patchwork.libcamera.org/api/1.1/patches/16253/?format=json","web_url":"https://patchwork.libcamera.org/patch/16253/","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":"<20220616182350.17352-6-laurent.pinchart@ideasonboard.com>","date":"2022-06-16T18:23:48","name":"[libcamera-devel,5/7] media: v4l2: Sanitize colorspace values in the framework","commit_ref":null,"pull_url":null,"state":"not-applicable","archived":false,"hash":"9502895566149dc0718aee6e4729103d61d4e18f","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16253/mbox/","series":[{"id":3183,"url":"https://patchwork.libcamera.org/api/1.1/series/3183/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3183","date":"2022-06-16T18:23:44","name":"[libcamera-devel,1/7] media: v4l: Add packed YUV 4:4:4 YUVA and YUVX pixel formats","version":1,"mbox":"https://patchwork.libcamera.org/series/3183/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16253/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16253/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 C2A0FC3276\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 16 Jun 2022 18:24:15 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id F2F0A65645;\n\tThu, 16 Jun 2022 20:24:14 +0200 (CEST)","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 F28DC600F0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 16 Jun 2022 20:24:09 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3AF3ABC0;\n\tThu, 16 Jun 2022 20:24:09 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1655403855;\n\tbh=bWNlCtgrMVPV7UjDm0QpNxf3zyYr7EaxNscRNaxH4jw=;\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:Cc:\n\tFrom;\n\tb=bW6SuRFyBplRjYKZW5ayFc/i1ZVF18wZcYN0w1EYxlTpJ6ppHkohr3RkB8FY+qNUd\n\tgXcc08+6otWr0iuIGP49K14toeFVZQxWv9dQaFpKwqOH6ZUQ+LUnFz24fewKYuq4CA\n\tLoAZIfiz5puB3AxDnFRN44y0VNw8Ge7gXE9AzkAhiW1jhw/VVizyvsJlJ3sYoqIpBc\n\tUFHVLi4izBYdyBADviHEZQ5VMYcY1sknLAI5ge/HepvNbGBaA2133dQm42RvDg4X8Y\n\tFpt0BJmONygQn45eGehrurdgZQLRXIV+olHtF77+qR9jASAb1YDjqkdeAFmtxW6BCH\n\tyJFrn4bL1RDsA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1655403849;\n\tbh=bWNlCtgrMVPV7UjDm0QpNxf3zyYr7EaxNscRNaxH4jw=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=vJMHmKCDQNT99G/rz6gEGI4ajhlaDWhmCPRup+swIpKaHw5kvaFdhWt+66/zo2Hjt\n\tAkY3Pg4kN/4x0FxL4Sy03tgbJBGn4cw6TTFO2j/3MxbHtMJmffsr2FA9xwhNu/UFfk\n\tlOdjCMPlLW0ok+M4sunCVqmJa2yjTai7fvBZPxCQ="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"vJMHmKCD\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 16 Jun 2022 21:23:48 +0300","Message-Id":"<20220616182350.17352-6-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220616182350.17352-1-laurent.pinchart@ideasonboard.com>","References":"<20220616182350.17352-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 5/7] media: v4l2: Sanitize colorspace\n\tvalues in the framework","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":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"Sakari Ailus <sakari.ailus@iki.fi>, linux-imx@nxp.com,\n\tkernel@pengutronix.de, Hans Verkuil <hverkuil-cisco@xs4all.nl>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Extend the format sanitization code in the framework to handle invalid\nvalues for the colorspace-related fields.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nAcked-by: Sakari Ailus <sakari.ailus@linux.intel.com>\n---\n drivers/media/v4l2-core/v4l2-ioctl.c | 65 +++++++++++++++++++++++-----\n 1 file changed, 55 insertions(+), 10 deletions(-)","diff":"diff --git a/drivers/media/v4l2-core/v4l2-ioctl.c b/drivers/media/v4l2-core/v4l2-ioctl.c\nindex 18ed2227255a..24b5968e8f32 100644\n--- a/drivers/media/v4l2-core/v4l2-ioctl.c\n+++ b/drivers/media/v4l2-core/v4l2-ioctl.c\n@@ -1008,6 +1008,31 @@ static int check_fmt(struct file *file, enum v4l2_buf_type type)\n \treturn -EINVAL;\n }\n \n+static void v4l_sanitize_colorspace(u32 pixelformat, u32 *colorspace,\n+\t\t\t\t    u32 *encoding, u32 *quantization,\n+\t\t\t\t    u32 *xfer_func)\n+{\n+\tbool is_hsv = pixelformat == V4L2_PIX_FMT_HSV24 ||\n+\t\t      pixelformat == V4L2_PIX_FMT_HSV32;\n+\n+\tif (!v4l2_is_colorspace_valid(*colorspace)) {\n+\t\t*colorspace = V4L2_COLORSPACE_DEFAULT;\n+\t\t*encoding = V4L2_YCBCR_ENC_DEFAULT;\n+\t\t*quantization = V4L2_QUANTIZATION_DEFAULT;\n+\t\t*xfer_func = V4L2_XFER_FUNC_DEFAULT;\n+\t}\n+\n+\tif ((!is_hsv && !v4l2_is_ycbcr_enc_valid(*encoding)) ||\n+\t    (is_hsv && !v4l2_is_hsv_enc_valid(*encoding)))\n+\t\t*encoding = V4L2_YCBCR_ENC_DEFAULT;\n+\n+\tif (!v4l2_is_quant_valid(*quantization))\n+\t\t*quantization = V4L2_QUANTIZATION_DEFAULT;\n+\n+\tif (!v4l2_is_xfer_func_valid(*xfer_func))\n+\t\t*xfer_func = V4L2_XFER_FUNC_DEFAULT;\n+}\n+\n static void v4l_sanitize_format(struct v4l2_format *fmt)\n {\n \tunsigned int offset;\n@@ -1027,20 +1052,40 @@ static void v4l_sanitize_format(struct v4l2_format *fmt)\n \t * field to the magic value when the extended pixel format structure\n \t * isn't used by applications.\n \t */\n+\tif (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||\n+\t    fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {\n+\t\tif (fmt->fmt.pix.priv != V4L2_PIX_FMT_PRIV_MAGIC) {\n+\t\t\tfmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;\n \n-\tif (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&\n-\t    fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)\n-\t\treturn;\n+\t\t\toffset = offsetof(struct v4l2_pix_format, priv)\n+\t\t\t       + sizeof(fmt->fmt.pix.priv);\n+\t\t\tmemset(((void *)&fmt->fmt.pix) + offset, 0,\n+\t\t\t       sizeof(fmt->fmt.pix) - offset);\n+\t\t}\n+\t}\n \n-\tif (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)\n-\t\treturn;\n+\t/* Replace invalid colorspace values with defaults. */\n+\tif (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||\n+\t    fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {\n+\t\tv4l_sanitize_colorspace(fmt->fmt.pix.pixelformat,\n+\t\t\t\t\t&fmt->fmt.pix.colorspace,\n+\t\t\t\t\t&fmt->fmt.pix.ycbcr_enc,\n+\t\t\t\t\t&fmt->fmt.pix.quantization,\n+\t\t\t\t\t&fmt->fmt.pix.xfer_func);\n+\t} else if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||\n+\t\t   fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {\n+\t\tu32 ycbcr_enc = fmt->fmt.pix_mp.ycbcr_enc;\n+\t\tu32 quantization = fmt->fmt.pix_mp.quantization;\n+\t\tu32 xfer_func = fmt->fmt.pix_mp.xfer_func;\n \n-\tfmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;\n+\t\tv4l_sanitize_colorspace(fmt->fmt.pix_mp.pixelformat,\n+\t\t\t\t\t&fmt->fmt.pix_mp.colorspace, &ycbcr_enc,\n+\t\t\t\t\t&quantization, &xfer_func);\n \n-\toffset = offsetof(struct v4l2_pix_format, priv)\n-\t       + sizeof(fmt->fmt.pix.priv);\n-\tmemset(((void *)&fmt->fmt.pix) + offset, 0,\n-\t       sizeof(fmt->fmt.pix) - offset);\n+\t\tfmt->fmt.pix_mp.ycbcr_enc = ycbcr_enc;\n+\t\tfmt->fmt.pix_mp.quantization = quantization;\n+\t\tfmt->fmt.pix_mp.xfer_func = xfer_func;\n+\t}\n }\n \n static int v4l_querycap(const struct v4l2_ioctl_ops *ops,\n","prefixes":["libcamera-devel","5/7"]}