From 18df0bc874412248feb29f44b22eddfa787d8406 Mon Sep 17 00:00:00 2001 From: William Vinnicombe Date: Tue, 3 Oct 2023 13:02:10 +0100 Subject: [PATCH] Fix flake8 compliance Signed-off-by: William Vinnicombe --- picamera2/allocators/dmaallocator.py | 9 ++++-- picamera2/buffer_sync.py | 42 ---------------------------- picamera2/dma_heap.py | 25 ++++++++++------- picamera2/request.py | 1 - tests/codecs.py | 7 +++-- 5 files changed, 25 insertions(+), 59 deletions(-) delete mode 100644 picamera2/buffer_sync.py diff --git a/picamera2/allocators/dmaallocator.py b/picamera2/allocators/dmaallocator.py index 8fc2b44a..5b95b0ab 100644 --- a/picamera2/allocators/dmaallocator.py +++ b/picamera2/allocators/dmaallocator.py @@ -1,10 +1,14 @@ +import fcntl import logging import mmap +import os import libcamera from picamera2.allocators.allocator import Allocator, Sync -from picamera2.dma_heap import * +from picamera2.dma_heap import (DMA_BUF_IOCTL_SYNC, DMA_BUF_SYNC_END, + DMA_BUF_SYNC_READ, DMA_BUF_SYNC_RW, + DMA_BUF_SYNC_START, DmaHeap, dma_buf_sync) _log = logging.getLogger("picamera2") @@ -47,7 +51,7 @@ def allocate(self, libcamera_config): plane[0].fd = fd.get() plane[0].offset = 0 plane[0].length = stream_config.frame_size - + self.libcamera_fds.append(plane[0].fd) self.mapped_buffers_used[plane[0].fd] = False @@ -83,7 +87,6 @@ def cleanup(self): for k in [k for k, v in self.mapped_buffers.items() if v.closed]: del self.mapped_buffers[k] - class DmaSync(Sync): """Dma Buffer Sync""" diff --git a/picamera2/buffer_sync.py b/picamera2/buffer_sync.py deleted file mode 100644 index fa7089b7..00000000 --- a/picamera2/buffer_sync.py +++ /dev/null @@ -1,42 +0,0 @@ -import ctypes -import fcntl -import logging -import os - -from .dma_heap import * - -_log = logging.getLogger("picamera2") - - -class BufferSync: - def __init__(self, picam2, fb, write): - self.picam2 = picam2 - self.__fb = fb - self.__write = write - - def __enter__(self): - dma_sync = dma_buf_sync() - dma_sync.flags = DMA_BUF_SYNC_START | (DMA_BUF_SYNC_RW if self.__write else DMA_BUF_SYNC_READ) - - it = self.picam2.mapped_buffers.get(self.__fb, None) - if it is None: - _log.error("failed to find buffer in BufferWriteSync") - return - - ret = fcntl.ioctl(self.__fb.planes[0].fd, DMA_BUF_IOCTL_SYNC, dma_sync) - if ret: - _log.error("failed to lock-sync-write dma buf") - return - - self.__planes = it - - def __exit__(self, exc_type, exc_value, exc_traceback): - dma_sync = dma_buf_sync() - dma_sync.flags = DMA_BUF_SYNC_END | (DMA_BUF_SYNC_RW if self.__write else DMA_BUF_SYNC_READ) - - ret = fcntl.ioctl(self.__fb.planes[0].fd, DMA_BUF_IOCTL_SYNC, dma_sync) - if ret: - _log.error("failed to unlock-sync-write dma buf") - - def get(self): - return self.__planes diff --git a/picamera2/dma_heap.py b/picamera2/dma_heap.py index 1fff2b2b..3b7e1a12 100644 --- a/picamera2/dma_heap.py +++ b/picamera2/dma_heap.py @@ -2,7 +2,8 @@ import fcntl import logging import os -from v4l2 import _IOWR, _IOW + +from v4l2 import _IOW, _IOWR _log = logging.getLogger("picamera2") heapNames = [ @@ -17,11 +18,12 @@ class dma_buf_sync(ctypes.Structure): ('flags', ctypes.c_uint64), ] -DMA_BUF_SYNC_READ = (1 << 0) -DMA_BUF_SYNC_WRITE = (2 << 0) -DMA_BUF_SYNC_RW = (DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE) -DMA_BUF_SYNC_START = (0 << 2) -DMA_BUF_SYNC_END = (1 << 2) + +DMA_BUF_SYNC_READ = (1 << 0) +DMA_BUF_SYNC_WRITE = (2 << 0) +DMA_BUF_SYNC_RW = (DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE) +DMA_BUF_SYNC_START = (0 << 2) +DMA_BUF_SYNC_END = (1 << 2) DMA_BUF_BASE = 'b' DMA_BUF_IOCTL_SYNC = _IOW(DMA_BUF_BASE, 0, dma_buf_sync) @@ -38,6 +40,7 @@ class dma_heap_allocation_data(ctypes.Structure): ('heap_flags', ctypes.c_uint64), ] + DMA_HEAP_IOC_MAGIC = 'H' DMA_HEAP_IOCTL_ALLOC = _IOWR(DMA_HEAP_IOC_MAGIC, 0, dma_heap_allocation_data) @@ -45,7 +48,8 @@ class dma_heap_allocation_data(ctypes.Structure): # Libcamera C++ classes class UniqueFD: - """Libcamera UniqueFD Class """ + """Libcamera UniqueFD Class""" + def __init__(self, fd=-1): if isinstance(fd, UniqueFD): self.__fd = fd.release() @@ -56,16 +60,17 @@ def release(self): fd = self.__fd self.__fd = -1 return fd - + def get(self): return self.__fd - + def isValid(self): return self.__fd >= 0 class DmaHeap: """DmaHeap""" + def __init__(self): self.__dmaHeapHandle = UniqueFD() for name in heapNames: @@ -100,5 +105,5 @@ def alloc(self, name, size) -> UniqueFD: if not isinstance(ret, bytes) and ret < 0: _log.error(f"dmaHeap naming failure for {name}") return UniqueFD() - + return allocFd diff --git a/picamera2/request.py b/picamera2/request.py index 42df187c..a54e12af 100644 --- a/picamera2/request.py +++ b/picamera2/request.py @@ -14,7 +14,6 @@ import picamera2.formats as formats from .controls import Controls -from .buffer_sync import BufferSync from .sensor_format import SensorFormat from .utils import convert_from_libcamera_type diff --git a/tests/codecs.py b/tests/codecs.py index d2f3c35f..fe841dee 100644 --- a/tests/codecs.py +++ b/tests/codecs.py @@ -18,7 +18,8 @@ def do_encode_by_hand(encoder, stream_name, output_name): request.release() encoder.stop() print(encoder, "finished!") - + + def do_encode_auto(encoder, stream_name, output_name): print("Start encode by hand using", encoder, "to", output_name) picam2.start_encoder(encoder, output=output_name, name=stream_name) @@ -51,10 +52,10 @@ def do_encode_auto(encoder, stream_name, output_name): do_encode_auto(H264Encoder(), "lores", "outb.h264") -jpeg_encoder = JpegEncoder() # needs RGB +jpeg_encoder = JpegEncoder() # needs RGB jpeg_encoder.size = config["main"]["size"] jpeg_encoder.format = config["main"]["format"] -jpeg_encoder.q = 50 # wants a quality parameter, not a bitrate (and no framerate) +jpeg_encoder.q = 50 # wants a quality parameter, not a bitrate (and no framerate) do_encode_by_hand(jpeg_encoder, "main", "out2.mjpeg") do_encode_auto(JpegEncoder(), "main", "out2b.mjpeg")