From patchwork Mon Jan 6 16:14:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2515 Return-Path: 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 61B3460463 for ; Mon, 6 Jan 2020 17:14:31 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id E584D123D; Mon, 6 Jan 2020 17:14:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1578327271; bh=h/KRYq2jcOQc5RNLGBGWmui5ap4uEpheS/JEK0VTal0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DUMTRgkb6aiPHtZTbEBWrzC+Q2cVy4nuGZRRqjc0SVOe/AwigNwSiOyHVJuZVg9FP 9XuvfmfXCYx2LpII3wYJavMlmYm8hlDOKdJ/Vk6F3KIOIKRDGHN6opxNY6nUbbkoi2 XEItfMEabynucGl1Gr3XzOjTKqI82AWOjUPJMbg8= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 6 Jan 2020 18:14:14 +0200 Message-Id: <20200106161417.19150-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200106161417.19150-1-laurent.pinchart@ideasonboard.com> References: <20200106161417.19150-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/4] v4l2: compat_manager: Move file operations to new struct FileOperations 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-List-Received-Date: Mon, 06 Jan 2020 16:14:31 -0000 Create a new FileOperations structure to hold all the dynamically-loaded C library file operations. The file operations are now exposed publicly, to prepare for usage of mmap in the V4L2CameraProxy. A new template helper function is added to retrieve a symbol with dlsym() with proper casting to simplify the V4L2CompatManager constructor. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- src/v4l2/v4l2_compat_manager.cpp | 34 ++++++++++++++++++++------------ src/v4l2/v4l2_compat_manager.h | 34 +++++++++++++++++++------------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/v4l2/v4l2_compat_manager.cpp b/src/v4l2/v4l2_compat_manager.cpp index a3d693745dd6..fe453445a9fb 100644 --- a/src/v4l2/v4l2_compat_manager.cpp +++ b/src/v4l2/v4l2_compat_manager.cpp @@ -28,15 +28,23 @@ using namespace libcamera; LOG_DEFINE_CATEGORY(V4L2Compat) +namespace { +template +void get_symbol(T &func, const char *name) +{ + func = reinterpret_cast(dlsym(RTLD_NEXT, name)); +} +} /* namespace */ + V4L2CompatManager::V4L2CompatManager() : cm_(nullptr), initialized_(false) { - openat_func_ = (openat_func_t)dlsym(RTLD_NEXT, "openat"); - dup_func_ = (dup_func_t )dlsym(RTLD_NEXT, "dup"); - close_func_ = (close_func_t )dlsym(RTLD_NEXT, "close"); - ioctl_func_ = (ioctl_func_t )dlsym(RTLD_NEXT, "ioctl"); - mmap_func_ = (mmap_func_t )dlsym(RTLD_NEXT, "mmap"); - munmap_func_ = (munmap_func_t)dlsym(RTLD_NEXT, "munmap"); + get_symbol(fops_.openat, "openat"); + get_symbol(fops_.dup, "dup"); + get_symbol(fops_.close, "close"); + get_symbol(fops_.ioctl, "ioctl"); + get_symbol(fops_.mmap, "mmap"); + get_symbol(fops_.munmap, "munmap"); } V4L2CompatManager::~V4L2CompatManager() @@ -141,7 +149,7 @@ int V4L2CompatManager::getCameraIndex(int fd) int V4L2CompatManager::openat(int dirfd, const char *path, int oflag, mode_t mode) { - int fd = openat_func_(dirfd, path, oflag, mode); + int fd = fops_.openat(dirfd, path, oflag, mode); if (fd < 0) return fd; @@ -160,7 +168,7 @@ int V4L2CompatManager::openat(int dirfd, const char *path, int oflag, mode_t mod return fd; } - close_func_(fd); + fops_.close(fd); unsigned int camera_index = static_cast(ret); @@ -182,7 +190,7 @@ int V4L2CompatManager::openat(int dirfd, const char *path, int oflag, mode_t mod int V4L2CompatManager::dup(int oldfd) { - int newfd = dup_func_(oldfd); + int newfd = fops_.dup(oldfd); if (newfd < 0) return newfd; @@ -205,7 +213,7 @@ int V4L2CompatManager::close(int fd) return 0; } - return close_func_(fd); + return fops_.close(fd); } void *V4L2CompatManager::mmap(void *addr, size_t length, int prot, int flags, @@ -213,7 +221,7 @@ void *V4L2CompatManager::mmap(void *addr, size_t length, int prot, int flags, { V4L2CameraProxy *proxy = getProxy(fd); if (!proxy) - return mmap_func_(addr, length, prot, flags, fd, offset); + return fops_.mmap(addr, length, prot, flags, fd, offset); void *map = proxy->mmap(length, prot, flags, offset); if (map == MAP_FAILED) @@ -227,7 +235,7 @@ int V4L2CompatManager::munmap(void *addr, size_t length) { auto device = mmaps_.find(addr); if (device == mmaps_.end()) - return munmap_func_(addr, length); + return fops_.munmap(addr, length); V4L2CameraProxy *proxy = device->second; @@ -244,7 +252,7 @@ int V4L2CompatManager::ioctl(int fd, unsigned long request, void *arg) { V4L2CameraProxy *proxy = getProxy(fd); if (!proxy) - return ioctl_func_(fd, request, arg); + return fops_.ioctl(fd, request, arg); return proxy->ioctl(request, arg); } diff --git a/src/v4l2/v4l2_compat_manager.h b/src/v4l2/v4l2_compat_manager.h index dd9d321078e4..d51b5953d930 100644 --- a/src/v4l2/v4l2_compat_manager.h +++ b/src/v4l2/v4l2_compat_manager.h @@ -26,11 +26,30 @@ using namespace libcamera; class V4L2CompatManager : public Thread { public: + struct FileOperations { + using openat_func_t = int (*)(int dirfd, const char *path, + int oflag, ...); + using dup_func_t = int (*)(int oldfd); + using close_func_t = int (*)(int fd); + using ioctl_func_t = int (*)(int fd, unsigned long request, ...); + using mmap_func_t = void *(*)(void *addr, size_t length, int prot, + int flags, int fd, off_t offset); + using munmap_func_t = int (*)(void *addr, size_t length); + + openat_func_t openat; + dup_func_t dup; + close_func_t close; + ioctl_func_t ioctl; + mmap_func_t mmap; + munmap_func_t munmap; + }; + static V4L2CompatManager *instance(); int init(); V4L2CameraProxy *getProxy(int fd); + const FileOperations &fops() const { return fops_; } int openat(int dirfd, const char *path, int oflag, mode_t mode); @@ -48,20 +67,7 @@ private: void run() override; int getCameraIndex(int fd); - typedef int (*openat_func_t)(int dirfd, const char *path, int oflag, ...); - typedef int (*dup_func_t)(int oldfd); - typedef int (*close_func_t)(int fd); - typedef int (*ioctl_func_t)(int fd, unsigned long request, ...); - typedef void *(*mmap_func_t)(void *addr, size_t length, int prot, - int flags, int fd, off_t offset); - typedef int (*munmap_func_t)(void *addr, size_t length); - - openat_func_t openat_func_; - dup_func_t dup_func_; - close_func_t close_func_; - ioctl_func_t ioctl_func_; - mmap_func_t mmap_func_; - munmap_func_t munmap_func_; + FileOperations fops_; CameraManager *cm_; From patchwork Mon Jan 6 16:14:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2516 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A823E60463 for ; Mon, 6 Jan 2020 17:14:31 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4870E52F; Mon, 6 Jan 2020 17:14:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1578327271; bh=aVkREMBjCfFQ9bZNNQQ7PSwX1C8PJ4OR6HN2PishuO8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=u4tpHCaMOIo0Us+/3ryCFhznxcpgO9tMApvDY0iTniom9sAGmNRlbj279GF8e7vAm gblPy6gzI8Jf19tUNF607Va18NxWRY3K555VE3QOTbiaaB+kmdTmlDChVdMtTUiWHa /Es6jU9gLhOHkJa1009XFYC7HyM039H1I/CYcLN0= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 6 Jan 2020 18:14:15 +0200 Message-Id: <20200106161417.19150-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200106161417.19150-1-laurent.pinchart@ideasonboard.com> References: <20200106161417.19150-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/4] v4l2: camera_proxy: Return correct type from VIDIOC_G_FMT 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-List-Received-Date: Mon, 06 Jan 2020 16:14:31 -0000 The VIDIOC_G_FMT implementation overwrites the v4l2_format type field with 0. Fix it. Signed-off-by: Laurent Pinchart --- src/v4l2/v4l2_camera_proxy.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index 2eeb12396d90..dd3ee3e6c6ff 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -229,6 +229,7 @@ int V4L2CameraProxy::vidioc_g_fmt(struct v4l2_format *arg) return -EINVAL; memset(&arg->fmt, 0, sizeof(arg->fmt)); + arg->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; arg->fmt.pix = curV4L2Format_.fmt.pix; return 0; From patchwork Mon Jan 6 16:14:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2517 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 12F1A60463 for ; Mon, 6 Jan 2020 17:14:32 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 9C72C123D; Mon, 6 Jan 2020 17:14:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1578327271; bh=PDjeAZLzgyFTovBIyKuatYwUN94hUX9iMc0UuB3KDjA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aEkDB/aXROOQnOsfTKfvby0k+Wejj64MZCR+ktp8OZxOSiXShMd3GUxMfJw7mMD6S 7qulWXx18ufjsltzIoVNRg+53tjUwtBbT+V8+T9tNpBuB70PqgNYQawrEF4F0jEE7i EP20aejEqeGeggRs7owzWemqMjF/fDAPr2TRtAg4= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 6 Jan 2020 18:14:16 +0200 Message-Id: <20200106161417.19150-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200106161417.19150-1-laurent.pinchart@ideasonboard.com> References: <20200106161417.19150-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 3/4] v4l2: camera_proxy: Rationalize arguments to format helpers 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-List-Received-Date: Mon, 06 Jan 2020 16:14:32 -0000 To clarify code, adopt the following rules for format helpers: - All variables representing V4L2 pixel formats shall use uint32_t - All variables representing DRM pixel formats shall use PixelFormat - Functions returning positive values only shall not have a signed return type Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- src/v4l2/v4l2_camera_proxy.cpp | 18 +++++++++--------- src/v4l2/v4l2_camera_proxy.h | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index dd3ee3e6c6ff..6a222d702e13 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -510,7 +510,7 @@ int V4L2CameraProxy::ioctl(unsigned long request, void *arg) } /* \todo make libcamera export these */ -int V4L2CameraProxy::bplMultiplier(unsigned int format) +unsigned int V4L2CameraProxy::bplMultiplier(uint32_t format) { switch (format) { case V4L2_PIX_FMT_NV12: @@ -535,8 +535,8 @@ int V4L2CameraProxy::bplMultiplier(unsigned int format) }; } -int V4L2CameraProxy::imageSize(unsigned int format, - unsigned int width, unsigned int height) +unsigned int V4L2CameraProxy::imageSize(uint32_t format, unsigned int width, + unsigned int height) { switch (format) { case V4L2_PIX_FMT_NV12: @@ -563,9 +563,9 @@ int V4L2CameraProxy::imageSize(unsigned int format, }; } -unsigned int V4L2CameraProxy::v4l2ToDrm(unsigned int pixelformat) +PixelFormat V4L2CameraProxy::v4l2ToDrm(uint32_t format) { - switch (pixelformat) { + switch (format) { /* RGB formats. */ case V4L2_PIX_FMT_RGB24: return DRM_FORMAT_BGR888; @@ -598,13 +598,13 @@ unsigned int V4L2CameraProxy::v4l2ToDrm(unsigned int pixelformat) case V4L2_PIX_FMT_NV42: return DRM_FORMAT_NV42; default: - return pixelformat; + return format; }; } -unsigned int V4L2CameraProxy::drmToV4L2(unsigned int pixelformat) +uint32_t V4L2CameraProxy::drmToV4L2(PixelFormat format) { - switch (pixelformat) { + switch (format) { /* RGB formats. */ case DRM_FORMAT_BGR888: return V4L2_PIX_FMT_RGB24; @@ -637,6 +637,6 @@ unsigned int V4L2CameraProxy::drmToV4L2(unsigned int pixelformat) case DRM_FORMAT_NV42: return V4L2_PIX_FMT_NV42; default: - return pixelformat; + return format; } } diff --git a/src/v4l2/v4l2_camera_proxy.h b/src/v4l2/v4l2_camera_proxy.h index bef0f0afab3b..3d702084f8ff 100644 --- a/src/v4l2/v4l2_camera_proxy.h +++ b/src/v4l2/v4l2_camera_proxy.h @@ -54,12 +54,12 @@ private: int vidioc_streamon(int *arg); int vidioc_streamoff(int *arg); - static int bplMultiplier(unsigned int format); - static int imageSize(unsigned int format, unsigned int width, - unsigned int height); + static unsigned int bplMultiplier(uint32_t format); + static unsigned int imageSize(uint32_t format, unsigned int width, + unsigned int height); - static unsigned int v4l2ToDrm(unsigned int pixelformat); - static unsigned int drmToV4L2(unsigned int pixelformat); + static PixelFormat v4l2ToDrm(uint32_t format); + static uint32_t drmToV4L2(PixelFormat format); unsigned int refcount_; unsigned int index_; From patchwork Mon Jan 6 16:14:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2518 Return-Path: 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 6212A60463 for ; Mon, 6 Jan 2020 17:14:32 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F3DC252F; Mon, 6 Jan 2020 17:14:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1578327272; bh=GuyLiI0Bdoy/6G7Asr8JP9/Nb+Pf5u1HOl6PW4PJQLc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D94CvEoyp6GTuHo/MD9ymYXyDRwfgL+UdOX7QXshepfBZHo8wnswpHuxFbIXfbshF e2xS61/cxwzawVvtuOR4oYh3rkGkx/oDkBRdQ4Akg9/Ol+ohAUDWjvx8irFvOMwDSF duq8MSzWBx584o0oi90ajSdinMqrHbW+wcujouXY= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 6 Jan 2020 18:14:17 +0200 Message-Id: <20200106161417.19150-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200106161417.19150-1-laurent.pinchart@ideasonboard.com> References: <20200106161417.19150-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 4/4] v4l2: camera_proxy: Create format info array 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-List-Received-Date: Mon, 06 Jan 2020 16:14:32 -0000 Create a PixelFormatInfo structure to store information about a format, and add a global array of format info for all the formats currently supported. Move the format helpers to use the information from the array. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- src/v4l2/v4l2_camera_proxy.cpp | 180 +++++++++++++-------------------- 1 file changed, 70 insertions(+), 110 deletions(-) diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index 6a222d702e13..b8c1a53af1c2 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -509,134 +509,94 @@ int V4L2CameraProxy::ioctl(unsigned long request, void *arg) return ret; } +struct PixelFormatPlaneInfo { + unsigned int bitsPerPixel; + unsigned int hSubSampling; + unsigned int vSubSampling; +}; + +struct PixelFormatInfo { + PixelFormat format; + uint32_t v4l2Format; + unsigned int numPlanes; + std::array planes; +}; + +namespace { + +constexpr std::array pixelFormatInfo = {{ + /* RGB formats. */ + { DRM_FORMAT_RGB888, V4L2_PIX_FMT_BGR24, 1, {{ { 24, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_BGR888, V4L2_PIX_FMT_RGB24, 1, {{ { 24, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_BGRA8888, V4L2_PIX_FMT_ARGB32, 1, {{ { 32, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + /* YUV packed formats. */ + { DRM_FORMAT_UYVY, V4L2_PIX_FMT_UYVY, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_VYUY, V4L2_PIX_FMT_VYUY, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_YUYV, V4L2_PIX_FMT_YUYV, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_YVYU, V4L2_PIX_FMT_YVYU, 1, {{ { 16, 1, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }} }, + /* YUY planar formats. */ + { DRM_FORMAT_NV12, V4L2_PIX_FMT_NV12, 2, {{ { 8, 1, 1 }, { 16, 2, 2 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_NV21, V4L2_PIX_FMT_NV21, 2, {{ { 8, 1, 1 }, { 16, 2, 2 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_NV16, V4L2_PIX_FMT_NV16, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_NV61, V4L2_PIX_FMT_NV61, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_NV24, V4L2_PIX_FMT_NV24, 2, {{ { 8, 1, 1 }, { 16, 2, 1 }, { 0, 0, 0 } }} }, + { DRM_FORMAT_NV42, V4L2_PIX_FMT_NV42, 2, {{ { 8, 1, 1 }, { 16, 1, 1 }, { 0, 0, 0 } }} }, +}}; + +} /* namespace */ + /* \todo make libcamera export these */ unsigned int V4L2CameraProxy::bplMultiplier(uint32_t format) { - switch (format) { - case V4L2_PIX_FMT_NV12: - case V4L2_PIX_FMT_NV21: - case V4L2_PIX_FMT_NV16: - case V4L2_PIX_FMT_NV61: - case V4L2_PIX_FMT_NV24: - case V4L2_PIX_FMT_NV42: - return 1; - case V4L2_PIX_FMT_BGR24: - case V4L2_PIX_FMT_RGB24: - return 3; - case V4L2_PIX_FMT_ARGB32: - return 4; - case V4L2_PIX_FMT_VYUY: - case V4L2_PIX_FMT_YVYU: - case V4L2_PIX_FMT_UYVY: - case V4L2_PIX_FMT_YUYV: - return 2; - default: + auto info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(), + [format](const PixelFormatInfo &info) { + return info.v4l2Format == format; + }); + if (info == pixelFormatInfo.end()) return 0; - }; + + return info->planes[0].bitsPerPixel / 8; } unsigned int V4L2CameraProxy::imageSize(uint32_t format, unsigned int width, unsigned int height) { - switch (format) { - case V4L2_PIX_FMT_NV12: - case V4L2_PIX_FMT_NV21: - return width * height * 3 / 2; - case V4L2_PIX_FMT_NV16: - case V4L2_PIX_FMT_NV61: - return width * height * 2; - case V4L2_PIX_FMT_NV24: - case V4L2_PIX_FMT_NV42: - return width * height * 3; - case V4L2_PIX_FMT_BGR24: - case V4L2_PIX_FMT_RGB24: - return width * height * 3; - case V4L2_PIX_FMT_ARGB32: - return width * height * 4; - case V4L2_PIX_FMT_VYUY: - case V4L2_PIX_FMT_YVYU: - case V4L2_PIX_FMT_UYVY: - case V4L2_PIX_FMT_YUYV: - return width * height * 2; - default: + auto info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(), + [format](const PixelFormatInfo &info) { + return info.v4l2Format == format; + }); + if (info == pixelFormatInfo.end()) return 0; - }; + + unsigned int multiplier = 0; + for (unsigned int i = 0; i < info->numPlanes; ++i) + multiplier += info->planes[i].bitsPerPixel + / info->planes[i].hSubSampling + / info->planes[i].vSubSampling; + + return width * height * multiplier / 8; } PixelFormat V4L2CameraProxy::v4l2ToDrm(uint32_t format) { - switch (format) { - /* RGB formats. */ - case V4L2_PIX_FMT_RGB24: - return DRM_FORMAT_BGR888; - case V4L2_PIX_FMT_BGR24: - return DRM_FORMAT_RGB888; - case V4L2_PIX_FMT_ARGB32: - return DRM_FORMAT_BGRA8888; - - /* YUV packed formats. */ - case V4L2_PIX_FMT_YUYV: - return DRM_FORMAT_YUYV; - case V4L2_PIX_FMT_YVYU: - return DRM_FORMAT_YVYU; - case V4L2_PIX_FMT_UYVY: - return DRM_FORMAT_UYVY; - case V4L2_PIX_FMT_VYUY: - return DRM_FORMAT_VYUY; - - /* YUY planar formats. */ - case V4L2_PIX_FMT_NV16: - return DRM_FORMAT_NV16; - case V4L2_PIX_FMT_NV61: - return DRM_FORMAT_NV61; - case V4L2_PIX_FMT_NV12: - return DRM_FORMAT_NV12; - case V4L2_PIX_FMT_NV21: - return DRM_FORMAT_NV21; - case V4L2_PIX_FMT_NV24: - return DRM_FORMAT_NV24; - case V4L2_PIX_FMT_NV42: - return DRM_FORMAT_NV42; - default: + auto info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(), + [format](const PixelFormatInfo &info) { + return info.v4l2Format == format; + }); + if (info == pixelFormatInfo.end()) return format; - }; + + return info->format; } uint32_t V4L2CameraProxy::drmToV4L2(PixelFormat format) { - switch (format) { - /* RGB formats. */ - case DRM_FORMAT_BGR888: - return V4L2_PIX_FMT_RGB24; - case DRM_FORMAT_RGB888: - return V4L2_PIX_FMT_BGR24; - case DRM_FORMAT_BGRA8888: - return V4L2_PIX_FMT_ARGB32; - - /* YUV packed formats. */ - case DRM_FORMAT_YUYV: - return V4L2_PIX_FMT_YUYV; - case DRM_FORMAT_YVYU: - return V4L2_PIX_FMT_YVYU; - case DRM_FORMAT_UYVY: - return V4L2_PIX_FMT_UYVY; - case DRM_FORMAT_VYUY: - return V4L2_PIX_FMT_VYUY; - - /* YUY planar formats. */ - case DRM_FORMAT_NV16: - return V4L2_PIX_FMT_NV16; - case DRM_FORMAT_NV61: - return V4L2_PIX_FMT_NV61; - case DRM_FORMAT_NV12: - return V4L2_PIX_FMT_NV12; - case DRM_FORMAT_NV21: - return V4L2_PIX_FMT_NV21; - case DRM_FORMAT_NV24: - return V4L2_PIX_FMT_NV24; - case DRM_FORMAT_NV42: - return V4L2_PIX_FMT_NV42; - default: + auto info = std::find_if(pixelFormatInfo.begin(), pixelFormatInfo.end(), + [format](const PixelFormatInfo &info) { + return info.format == format; + }); + if (info == pixelFormatInfo.end()) return format; - } + + return info->v4l2Format; }