{"id":13623,"url":"https://patchwork.libcamera.org/api/1.1/patches/13623/?format=json","web_url":"https://patchwork.libcamera.org/patch/13623/","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":"<20210906020100.14430-15-laurent.pinchart@ideasonboard.com>","date":"2021-09-06T02:00:47","name":"[libcamera-devel,v2,14/27] libcamera: v4l2_videodevice: Split planes when dequeuing buffer","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"48e31fad63d88bdf24ed1e52c756a35b6ff3967d","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/13623/mbox/","series":[{"id":2446,"url":"https://patchwork.libcamera.org/api/1.1/series/2446/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2446","date":"2021-09-06T02:00:33","name":"libcamera: Handle fallout of FrameBuffer offset support","version":2,"mbox":"https://patchwork.libcamera.org/series/2446/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13623/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13623/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 7CCDAC324B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  6 Sep 2021 02:01:46 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E59F26917B;\n\tMon,  6 Sep 2021 04:01:39 +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 8E0F469174\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  6 Sep 2021 04:01:26 +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 1E7838D7;\n\tMon,  6 Sep 2021 04:01:26 +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=\"p3ccUtT2\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1630893686;\n\tbh=ADgP6wyAOxxkbNZHhYHjOLTRGZIwB18N9UIOasiuSEc=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=p3ccUtT2jzcZCA6ZY+fgZN2mGhRHjW+MGR8Dz6SY66zmApfTNon8eNT80msnRy6sq\n\tpy9G98XFSzLV8nB75LpRvmCsmEHqGRtjFoCf2HEL1WangD55kCFt3F+bnfe47ao7Jd\n\tNT9u4Bk7jtATFYjk1WGDpmRYBGXSbCM8hwc3vx04=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon,  6 Sep 2021 05:00:47 +0300","Message-Id":"<20210906020100.14430-15-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20210906020100.14430-1-laurent.pinchart@ideasonboard.com>","References":"<20210906020100.14430-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 14/27] libcamera: v4l2_videodevice:\n\tSplit planes when dequeuing 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 dequeueing a buffer from a V4L2VideoDevice, the number of planes in\nthe FrameBuffer 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 split the single V4L2 buffer plane into FrameBuffer planes. Do\nso, and add checks to reject invalid V4L2 buffers in case of a driver\nissue.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges since v1:\n\n- Reduce indentation\n---\n src/libcamera/v4l2_videodevice.cpp | 53 +++++++++++++++++++++++++++---\n 1 file changed, 48 insertions(+), 5 deletions(-)","diff":"diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex 625d5da40337..0a7fbdb7e011 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -7,6 +7,7 @@\n \n #include \"libcamera/internal/v4l2_videodevice.h\"\n \n+#include <algorithm>\n #include <array>\n #include <fcntl.h>\n #include <iomanip>\n@@ -1664,12 +1665,54 @@ FrameBuffer *V4L2VideoDevice::dequeueBuffer()\n \tbuffer->metadata_.timestamp = buf.timestamp.tv_sec * 1000000000ULL\n \t\t\t\t    + buf.timestamp.tv_usec * 1000ULL;\n \n-\tbuffer->metadata_.planes.clear();\n-\tif (multiPlanar) {\n-\t\tfor (unsigned int nplane = 0; nplane < buf.length; nplane++)\n-\t\t\tbuffer->metadata_.planes.push_back({ planes[nplane].bytesused });\n+\tif (V4L2_TYPE_IS_OUTPUT(buf.type))\n+\t\treturn buffer;\n+\n+\tunsigned int numV4l2Planes = multiPlanar ? buf.length : 1;\n+\tFrameMetadata &metadata = buffer->metadata_;\n+\tmetadata.planes.clear();\n+\n+\tif (numV4l2Planes != buffer->planes().size()) {\n+\t\t/*\n+\t\t * If we have a multi-planar buffer with a V4L2\n+\t\t * single-planar format, split the V4L2 buffer across\n+\t\t * the buffer planes. Only the last plane may have less\n+\t\t * bytes used than its length.\n+\t\t */\n+\t\tif (numV4l2Planes != 1) {\n+\t\t\tLOG(V4L2, Error)\n+\t\t\t\t<< \"Invalid number of planes (\" << numV4l2Planes\n+\t\t\t\t<< \" != \" << buffer->planes().size() << \")\";\n+\n+\t\t\tmetadata.status = FrameMetadata::FrameError;\n+\t\t\treturn buffer;\n+\t\t}\n+\n+\t\tunsigned int bytesused = multiPlanar ? planes[0].bytesused\n+\t\t\t\t       : buf.bytesused;\n+\n+\t\tfor (auto [i, plane] : utils::enumerate(buffer->planes())) {\n+\t\t\tif (!bytesused) {\n+\t\t\t\tLOG(V4L2, Error)\n+\t\t\t\t\t<< \"Dequeued buffer is too small\";\n+\n+\t\t\t\tmetadata.status = FrameMetadata::FrameError;\n+\t\t\t\treturn buffer;\n+\t\t\t}\n+\n+\t\t\tmetadata.planes.push_back({ std::min(plane.length, bytesused) });\n+\t\t\tbytesused -= metadata.planes.back().bytesused;\n+\t\t}\n+\t} else if (multiPlanar) {\n+\t\t/*\n+\t\t * If we use the multi-planar API, fill in the planes.\n+\t\t * The number of planes in the frame buffer and in the\n+\t\t * V4L2 buffer is guaranteed to be equal at this point.\n+\t\t */\n+\t\tfor (unsigned int i = 0; i < numV4l2Planes; ++i)\n+\t\t\tmetadata.planes.push_back({ planes[i].bytesused });\n \t} else {\n-\t\tbuffer->metadata_.planes.push_back({ buf.bytesused });\n+\t\tmetadata.planes.push_back({ buf.bytesused });\n \t}\n \n \treturn buffer;\n","prefixes":["libcamera-devel","v2","14/27"]}