From patchwork Thu May 5 10:41:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 15796 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 D089CC3272 for ; Thu, 5 May 2022 10:41:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 73A4E6564A; Thu, 5 May 2022 12:41:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1651747299; bh=pmogevaeNcSjdHQ7Hgy5H641piE912B+uxhkbOw+v34=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=YnTP/T/aXaDhBGNbZWPFBUAgwsr0z5x23AsQEkRYXYnhGPdGNHTz169d43PDcl2vj 1gaCrMZChuwBJMYY7d0AX/S13aTqRlIMChACoG8G1pxWciHxoga+g/Nspbydl+88ec X94UiuFgeSQl9L5LGXFkTMICGLagmIENDTRRnKjzMa7uVc2jcJjMENd8nPzVaZpYOr +WGxGd56cuRkP7hjBTTuEsUFf7F/a6794v1s2y5k0/XTIqIldArzec7UJw+g955zes ckzteEx0WVI67iEM4Q5wFWEEHAvW2xabybG0jDMOXkPmc6TD0w1q9rhmpKfZiLXAw4 5b+FMNuNsmo+A== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9A2F865664 for ; Thu, 5 May 2022 12:41:32 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="R47LJtFq"; dkim-atps=neutral Received: from deskari.lan (91-156-85-209.elisa-laajakaista.fi [91.156.85.209]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 0A80D4A8; Thu, 5 May 2022 12:41:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1651747292; bh=pmogevaeNcSjdHQ7Hgy5H641piE912B+uxhkbOw+v34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R47LJtFqKkaxe4CnB8vC67c8c6HTABikZ2J9EKEP+cqF/YAeBb1FrjHl740TgH4Sj gdmxtRtNxDMaMnE7Be6I62Qx4UpRWy2v5KGLPCs5dCv56ZLYMDl+DOqGZqjrLGBE8/ 1NQxJRm4a4lAJNwRXOqWfgKSUsBtMVoCP9xa0BTE= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Thu, 5 May 2022 13:41:04 +0300 Message-Id: <20220505104104.70841-14-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220505104104.70841-1-tomi.valkeinen@ideasonboard.com> References: <20220505104104.70841-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 13/13] py: implement MappedFrameBuffer 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: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Instead of just exposing plain mmap via fb.mmap(planenum), implement a MappedFrameBuffer class, similar to C++'s MappedFrameBuffer. MappedFrameBuffer mmaps the underlying filedescriptors and provides Python memoryviews for each plane. As an example, to save a Framebuffer to a file: with fb.mmap() as mfb: with open(filename, "wb") as f: for p in mfb.planes: f.write(p) The objects in mfb.planes are memoryviews that cover only the plane in question. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart --- src/py/cam/cam.py | 11 +++--- src/py/cam/cam_qt.py | 6 +-- src/py/libcamera/__init__.py | 72 +++++++++++++++++++++++++++++++++++- src/py/libcamera/pymain.cpp | 7 ++++ 4 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/py/cam/cam.py b/src/py/cam/cam.py index 4efa6459..c0ebb186 100755 --- a/src/py/cam/cam.py +++ b/src/py/cam/cam.py @@ -329,9 +329,9 @@ def request_handler(state, ctx, req): crcs = [] if ctx["opt-crc"]: - with fb.mmap(0) as b: - crc = binascii.crc32(b) - crcs.append(crc) + with fb.mmap() as mfb: + plane_crcs = [binascii.crc32(p) for p in mfb.planes] + crcs.append(plane_crcs) meta = fb.metadata @@ -347,10 +347,11 @@ def request_handler(state, ctx, req): print(f"\t{ctrl} = {val}") if ctx["opt-save-frames"]: - with fb.mmap(0) as b: + with fb.mmap() as mfb: filename = "frame-{}-{}-{}.data".format(ctx["id"], stream_name, ctx["reqs-completed"]) with open(filename, "wb") as f: - f.write(b) + for p in mfb.planes: + f.write(p) state["renderer"].request_handler(ctx, req) diff --git a/src/py/cam/cam_qt.py b/src/py/cam/cam_qt.py index 30fb7a1d..d394987b 100644 --- a/src/py/cam/cam_qt.py +++ b/src/py/cam/cam_qt.py @@ -324,17 +324,17 @@ class MainWindow(QtWidgets.QWidget): controlsLayout.addStretch() def buf_to_qpixmap(self, stream, fb): - with fb.mmap(0) as b: + with fb.mmap() as mfb: cfg = stream.configuration w, h = cfg.size pitch = cfg.stride if cfg.pixelFormat == "MJPEG": - img = Image.open(BytesIO(b)) + img = Image.open(BytesIO(mfb.planes[0])) qim = ImageQt(img).copy() pix = QtGui.QPixmap.fromImage(qim) else: - data = np.array(b, dtype=np.uint8) + data = np.array(mfb.planes[0], dtype=np.uint8) rgb = to_rgb(cfg.pixelFormat, cfg.size, data) if rgb is None: diff --git a/src/py/libcamera/__init__.py b/src/py/libcamera/__init__.py index cd7512a2..caf06af7 100644 --- a/src/py/libcamera/__init__.py +++ b/src/py/libcamera/__init__.py @@ -1,12 +1,80 @@ # SPDX-License-Identifier: LGPL-2.1-or-later # Copyright (C) 2021, Tomi Valkeinen +from os import lseek, SEEK_END from ._libcamera import * import mmap -def __FrameBuffer__mmap(self, plane): - return mmap.mmap(self.fd(plane), self.length(plane), mmap.MAP_SHARED, mmap.PROT_READ) +def __FrameBuffer__mmap(self): + return MappedFrameBuffer(self) FrameBuffer.mmap = __FrameBuffer__mmap + + +class MappedFrameBuffer: + def __init__(self, fb): + self.fb = fb + + # Collect information about the buffers + + bufinfos = {} + + for i in range(fb.num_planes): + fd = fb.fd(i) + + if fd not in bufinfos: + buflen = lseek(fd, 0, SEEK_END) + bufinfos[fd] = {"maplen": 0, "buflen": buflen} + else: + buflen = bufinfos[fd]["buflen"] + + if fb.offset(i) > buflen or fb.offset(i) + fb.length(i) > buflen: + raise RuntimeError(f"plane is out of buffer: buffer length={buflen}, " + f"plane offset={fb.offset(i)}, plane length={fb.length(i)}") + + bufinfos[fd]["maplen"] = max(bufinfos[fd]["maplen"], fb.offset(i) + fb.length(i)) + + # mmap the buffers + + maps = [] + + for fd, info in bufinfos.items(): + map = mmap.mmap(fd, info["maplen"], mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE) + info["map"] = map + maps.append(map) + + self.maps = tuple(maps) + + # Create memoryviews for the planes + + planes = [] + + for i in range(fb.num_planes): + fd = fb.fd(i) + info = bufinfos[fd] + + mv = memoryview(info["map"]) + + start = fb.offset(i) + end = fb.offset(i) + fb.length(i) + + mv = mv[start:end] + + planes.append(mv) + + self.planes = tuple(planes) + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, exc_traceback): + for p in self.planes: + p.release() + + for mm in self.maps: + mm.close() + + def planes(self): + return self.planes diff --git a/src/py/libcamera/pymain.cpp b/src/py/libcamera/pymain.cpp index b9b52f6b..73d29479 100644 --- a/src/py/libcamera/pymain.cpp +++ b/src/py/libcamera/pymain.cpp @@ -439,6 +439,9 @@ PYBIND11_MODULE(_libcamera, m) return new FrameBuffer(v, cookie); })) .def_property_readonly("metadata", &FrameBuffer::metadata, py::return_value_policy::reference_internal) + .def_property_readonly("num_planes", [](const FrameBuffer &self) { + return self.planes().size(); + }) .def("length", [](FrameBuffer &self, uint32_t idx) { const FrameBuffer::Plane &plane = self.planes()[idx]; return plane.length; @@ -447,6 +450,10 @@ PYBIND11_MODULE(_libcamera, m) const FrameBuffer::Plane &plane = self.planes()[idx]; return plane.fd.get(); }) + .def("offset", [](FrameBuffer &self, uint32_t idx) { + const FrameBuffer::Plane &plane = self.planes()[idx]; + return plane.offset; + }) .def_property("cookie", &FrameBuffer::cookie, &FrameBuffer::setCookie); pyStream