From 0f3534d3be5750ff9cb03cb997a8efc91de07347 Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Tue, 4 Oct 2022 10:03:53 +0300 Subject: py: cam.py: fixes Fix cam.py to get it working again and add a bunch of small improvements. Signed-off-by: Tomi Valkeinen --- py/tests/cam.py | 71 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 15 deletions(-) (limited to 'py/tests') diff --git a/py/tests/cam.py b/py/tests/cam.py index 957eb11..b2d88c4 100755 --- a/py/tests/cam.py +++ b/py/tests/cam.py @@ -3,24 +3,32 @@ import sys import selectors import pykms +import pyv4l2 as v4l2 import argparse import time parser = argparse.ArgumentParser() parser.add_argument("width", type=int) parser.add_argument("height", type=int) -parser.add_argument("fourcc", type=str, nargs="?", default="YUVY") +parser.add_argument("fourcc", type=str, nargs="?", default="UYVY") +parser.add_argument("-t", "--type", type=str, default="drm", help="buffer type (drm/v4l2)") args = parser.parse_args() +if not args.type in ["drm", "v4l2"]: + print("Bad buffer type", args.type) + exit(-1) + w = args.width h = args.height fmt = pykms.fourcc_to_pixelformat(args.fourcc) +vfmt = v4l2.drm_fourcc_to_pixelformat(pykms.pixelformat_to_fourcc(fmt)) -print("Capturing in {}x{}".format(w, h)) +print("Capturing in {}x{}, drm fmt {} ({}), v4l2 fmt {} ({})".format(w, h, + fmt, pykms.pixelformat_to_fourcc(fmt), vfmt, v4l2.pixelformat_to_fourcc(vfmt))) card = pykms.Card() res = pykms.ResourceManager(card) -conn = res.reserve_connector() +conn = res.reserve_connector("hdmi") crtc = res.reserve_crtc(conn) plane = res.reserve_overlay_plane(crtc, fmt) @@ -35,27 +43,60 @@ req.commit_sync(allow_modeset = True) NUM_BUFS = 5 +vidpath = v4l2.VideoDevice.get_capture_devices()[0] + +vid = v4l2.VideoDevice(vidpath) +cap = vid.capture_streamer +cap.set_port(0) +cap.set_format(vfmt, w, h) +cap.set_queue_size(NUM_BUFS, v4l2.VideoMemoryType.DMABUF if args.type == "drm" else v4l2.VideoMemoryType.MMAP) + fbs = [] + +def export_v4l2_buf_to_drm(card, i, vfmt): + finfo = pykms.get_pixel_format_info(fmt) + + if fmt == pykms.PixelFormat.NV12: + fd = cap.export_buffer(i) + buf_fds = [fd, fd] + pitches = [w * finfo.plane(0).bitspp // 8, w * finfo.plane(1).bitspp // 8] + offsets = [0, w * h * finfo.plane(0).bitspp // 8] + else: + buf_fds = [cap.export_buffer(i)] + pitches = [w * finfo.plane(0).bitspp // 8] + offsets = [0] + + fb = pykms.DmabufFramebuffer(card, w, h, fmt, + buf_fds, pitches, offsets) + + return fb + + for i in range(NUM_BUFS): - fb = pykms.DumbFramebuffer(card, w, h, fmt) - fbs.append(fb) + if args.type == "drm": + fb = pykms.DumbFramebuffer(card, w, h, fmt) + else: + fb = export_v4l2_buf_to_drm(card, i, vfmt) -vidpath = pykms.VideoDevice.get_capture_devices()[0] + print(fb) -vid = pykms.VideoDevice(vidpath) -cap = vid.capture_streamer -cap.set_port(0) -cap.set_format(fmt, w, h) -cap.set_queue_size(NUM_BUFS) + fbs.append(fb) -for fb in fbs: - cap.queue(fb) +for i in range(NUM_BUFS): + if args.type == "drm": + vbuf = v4l2.create_dmabuffer(fbs[i].fd(0)) + else: + vbuf = v4l2.create_mmapbuffer() + cap.queue(vbuf) cap.stream_on() def readvid(conn, mask): - fb = cap.dequeue() + vbuf = cap.dequeue() + + fb = fbs[vbuf.index] + assert(fb != None) if card.has_atomic: plane.set_props({ @@ -70,7 +111,7 @@ def readvid(conn, mask): crtc.set_plane(plane, fb, 0, 0, fb.width, fb.height, 0, 0, fb.width, fb.height) - cap.queue(fb) + cap.queue(vbuf) def readkey(conn, mask): #print("KEY EVENT"); -- cgit v1.2.3