{"id":13679,"url":"https://patchwork.libcamera.org/api/patches/13679/?format=json","web_url":"https://patchwork.libcamera.org/patch/13679/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20210906225636.14683-13-laurent.pinchart@ideasonboard.com>","date":"2021-09-06T22:56:19","name":"[libcamera-devel,v3,13/30] libcamera: v4l2_videodevice: Coalesce planes when queuing buffer","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"b0d080713c421724f974bb68f95f4942fec9d72b","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/13679/mbox/","series":[{"id":2482,"url":"https://patchwork.libcamera.org/api/series/2482/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2482","date":"2021-09-06T22:54:20","name":"libcamera: Handle fallout of FrameBuffer offset support","version":3,"mbox":"https://patchwork.libcamera.org/series/2482/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13679/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13679/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 77E78BE175\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  6 Sep 2021 22:57:20 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 05F746916E;\n\tTue,  7 Sep 2021 00:57:20 +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 44D3B6916E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  7 Sep 2021 00:57:04 +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 D0589993;\n\tTue,  7 Sep 2021 00:57:03 +0200 (CEST)"],"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=\"RI11skCM\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1630969024;\n\tbh=XxS7cI+sDkbVoE77PdIrZtkm4p8tHhFJ4OQ3455RPQM=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=RI11skCMZULKvV/Xchf8TbPLgc5Gf+hcFwT8U41wbD+BiVDZLpTieI+uKYkzOtJQz\n\ttNma1TB6Adphb0+7ZM6SkgkH0zWe99z1uQdpYqgwdrie2Y2RtLAGLoT9ge/BckO05a\n\tx0KTEC/H7UZj9sE7CjqoXqg01npGeBjOc1iAvRa8=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue,  7 Sep 2021 01:56:19 +0300","Message-Id":"<20210906225636.14683-13-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20210906225420.13275-1-laurent.pinchart@ideasonboard.com>","References":"<20210906225420.13275-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 13/30] libcamera: v4l2_videodevice:\n\tCoalesce planes when queuing buffer","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":"When queueing a buffer to a V4L2VideoDevice, the number of planes in the\nFrameBuffer may not match the number of V4L2 buffer planes if the\nPixelFormat is multi-planar (has multiple colour planes) and the V4L2\nformat is single-planar (has a single buffer plane). In this case, we\nneed to coalesce all FrameBuffer planes into a single V4L2 buffer plane.\nDo so, and add validity checks to reject frame buffers that can't be\ndescribed using a single V4L2 buffer plane.\n\nThis change prepares for proper multi-planar support, but isn't expected\nto result in a change of behaviour with existing pipeline handlers, as\nnone of them queue an output buffer with multiple FrameBuffer planes or\nuse non-contiguous buffers for either capture or output.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nReviewed-by: Hirokazu Honda <hiroh@chromium.org>\n---\nChanges since v2:\n\n- Ensure that the queued frame buffer has enough planes\n\nChanges since v1:\n\n- Make numV4l2Planes const\n- Use format_.planesCount\n---\n src/libcamera/v4l2_videodevice.cpp | 72 +++++++++++++++++++++++++-----\n 1 file changed, 62 insertions(+), 10 deletions(-)","diff":"diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex c6c9263c49e9..6c1ca9bb0a5c 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -22,10 +22,12 @@\n \n #include <libcamera/base/event_notifier.h>\n #include <libcamera/base/log.h>\n+#include <libcamera/base/utils.h>\n \n #include <libcamera/file_descriptor.h>\n \n #include \"libcamera/internal/formats.h\"\n+#include \"libcamera/internal/framebuffer.h\"\n #include \"libcamera/internal/media_device.h\"\n #include \"libcamera/internal/media_object.h\"\n \n@@ -1496,10 +1498,25 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)\n \n \tbool multiPlanar = V4L2_TYPE_IS_MULTIPLANAR(buf.type);\n \tconst std::vector<FrameBuffer::Plane> &planes = buffer->planes();\n+\tconst unsigned int numV4l2Planes = format_.planesCount;\n+\n+\t/*\n+\t * Ensure that the frame buffer has enough planes, and that they're\n+\t * contiguous if the V4L2 format requires them to be.\n+\t */\n+\tif (planes.size() < numV4l2Planes) {\n+\t\tLOG(V4L2, Error) << \"Frame buffer has too few planes\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (planes.size() != numV4l2Planes && !buffer->_d()->isContiguous()) {\n+\t\tLOG(V4L2, Error) << \"Device format requires contiguous buffer\";\n+\t\treturn -EINVAL;\n+\t}\n \n \tif (buf.memory == V4L2_MEMORY_DMABUF) {\n \t\tif (multiPlanar) {\n-\t\t\tfor (unsigned int p = 0; p < planes.size(); ++p)\n+\t\t\tfor (unsigned int p = 0; p < numV4l2Planes; ++p)\n \t\t\t\tv4l2Planes[p].m.fd = planes[p].fd.fd();\n \t\t} else {\n \t\t\tbuf.m.fd = planes[0].fd.fd();\n@@ -1507,23 +1524,58 @@ int V4L2VideoDevice::queueBuffer(FrameBuffer *buffer)\n \t}\n \n \tif (multiPlanar) {\n-\t\tbuf.length = planes.size();\n+\t\tbuf.length = numV4l2Planes;\n \t\tbuf.m.planes = v4l2Planes;\n \t}\n \n \tif (V4L2_TYPE_IS_OUTPUT(buf.type)) {\n \t\tconst FrameMetadata &metadata = buffer->metadata();\n \n-\t\tif (multiPlanar) {\n-\t\t\tunsigned int nplane = 0;\n-\t\t\tfor (const FrameMetadata::Plane &plane : metadata.planes) {\n-\t\t\t\tv4l2Planes[nplane].bytesused = plane.bytesused;\n-\t\t\t\tv4l2Planes[nplane].length = buffer->planes()[nplane].length;\n-\t\t\t\tnplane++;\n+\t\tif (numV4l2Planes != planes.size()) {\n+\t\t\t/*\n+\t\t\t * If we have a multi-planar buffer with a V4L2\n+\t\t\t * single-planar format, coalesce all planes. The length\n+\t\t\t * and number of bytes used may only differ in the last\n+\t\t\t * plane as any other situation can't be represented.\n+\t\t\t */\n+\t\t\tunsigned int bytesused = 0;\n+\t\t\tunsigned int length = 0;\n+\n+\t\t\tfor (auto [i, plane] : utils::enumerate(planes)) {\n+\t\t\t\tbytesused += metadata.planes[i].bytesused;\n+\t\t\t\tlength += plane.length;\n+\n+\t\t\t\tif (i != planes.size() - 1 && bytesused != length) {\n+\t\t\t\t\tLOG(V4L2, Error)\n+\t\t\t\t\t\t<< \"Holes in multi-planar buffer not supported\";\n+\t\t\t\t\treturn -EINVAL;\n+\t\t\t\t}\n+\t\t\t}\n+\n+\t\t\tif (multiPlanar) {\n+\t\t\t\tv4l2Planes[0].bytesused = bytesused;\n+\t\t\t\tv4l2Planes[0].length = length;\n+\t\t\t} else {\n+\t\t\t\tbuf.bytesused = bytesused;\n+\t\t\t\tbuf.length = length;\n+\t\t\t}\n+\t\t} else if (multiPlanar) {\n+\t\t\t/*\n+\t\t\t * If we use the multi-planar API, fill in the planes.\n+\t\t\t * The number of planes in the frame buffer and in the\n+\t\t\t * V4L2 buffer is guaranteed to be equal at this point.\n+\t\t\t */\n+\t\t\tfor (auto [i, plane] : utils::enumerate(planes)) {\n+\t\t\t\tv4l2Planes[i].bytesused = metadata.planes[i].bytesused;\n+\t\t\t\tv4l2Planes[i].length = plane.length;\n \t\t\t}\n \t\t} else {\n-\t\t\tif (metadata.planes.size())\n-\t\t\t\tbuf.bytesused = metadata.planes[0].bytesused;\n+\t\t\t/*\n+\t\t\t * Single-planar API with a single plane in the buffer\n+\t\t\t * is trivial to handle.\n+\t\t\t */\n+\t\t\tbuf.bytesused = metadata.planes[0].bytesused;\n+\t\t\tbuf.length = planes[0].length;\n \t\t}\n \n \t\tbuf.sequence = metadata.sequence;\n","prefixes":["libcamera-devel","v3","13/30"]}