From patchwork Mon May 27 09:05:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 1314 Return-Path: Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 756FA61900 for ; Mon, 27 May 2019 11:05:03 +0200 (CEST) X-Originating-IP: 2.224.242.101 Received: from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101]) (Authenticated sender: jacopo@jmondi.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id DEF9E60017; Mon, 27 May 2019 09:05:02 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Mon, 27 May 2019 11:05:56 +0200 Message-Id: <20190527090559.26549-4-jacopo@jmondi.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190527090559.26549-1-jacopo@jmondi.org> References: <20190527090559.26549-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/6] libcamera: v4l2_device: Add META support in g/s_fmt X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 May 2019 09:05:04 -0000 Add two operations to set and get format on devices implementing the V4L2 Metadata Interface, identified by the META_OUTPUT or META_CAPTURE capabilities. While at it, sort get/setFormat operations alphabetically and unify their style (eg. no empty line before ioctl). Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- src/libcamera/include/v4l2_device.h | 7 +- src/libcamera/v4l2_device.cpp | 108 +++++++++++++++++++++------- 2 files changed, 89 insertions(+), 26 deletions(-) diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h index cecafa151caa..bdecc087fe5a 100644 --- a/src/libcamera/include/v4l2_device.h +++ b/src/libcamera/include/v4l2_device.h @@ -150,12 +150,15 @@ protected: std::string logPrefix() const; private: - int getFormatSingleplane(V4L2DeviceFormat *format); - int setFormatSingleplane(V4L2DeviceFormat *format); + int getFormatMeta(V4L2DeviceFormat *format); + int setFormatMeta(V4L2DeviceFormat *format); int getFormatMultiplane(V4L2DeviceFormat *format); int setFormatMultiplane(V4L2DeviceFormat *format); + int getFormatSingleplane(V4L2DeviceFormat *format); + int setFormatSingleplane(V4L2DeviceFormat *format); + int requestBuffers(unsigned int count); int createPlane(Buffer *buffer, unsigned int plane, unsigned int length); diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp index e42f6ef0c340..c9251da465af 100644 --- a/src/libcamera/v4l2_device.cpp +++ b/src/libcamera/v4l2_device.cpp @@ -428,8 +428,12 @@ std::string V4L2Device::logPrefix() const */ int V4L2Device::getFormat(V4L2DeviceFormat *format) { - return caps_.isMultiplanar() ? getFormatMultiplane(format) : - getFormatSingleplane(format); + if (caps_.isMeta()) + return getFormatMeta(format); + else if (caps_.isMultiplanar()) + return getFormatMultiplane(format); + else + return getFormatSingleplane(format); } /** @@ -443,14 +447,18 @@ int V4L2Device::getFormat(V4L2DeviceFormat *format) */ int V4L2Device::setFormat(V4L2DeviceFormat *format) { - return caps_.isMultiplanar() ? setFormatMultiplane(format) : - setFormatSingleplane(format); + if (caps_.isMeta()) + return setFormatMeta(format); + else if (caps_.isMultiplanar()) + return setFormatMultiplane(format); + else + return setFormatSingleplane(format); } -int V4L2Device::getFormatSingleplane(V4L2DeviceFormat *format) +int V4L2Device::getFormatMeta(V4L2DeviceFormat *format) { struct v4l2_format v4l2Format = {}; - struct v4l2_pix_format *pix = &v4l2Format.fmt.pix; + struct v4l2_meta_format *pix = &v4l2Format.fmt.meta; int ret; v4l2Format.type = bufferType_; @@ -461,29 +469,24 @@ int V4L2Device::getFormatSingleplane(V4L2DeviceFormat *format) return ret; } - format->size.width = pix->width; - format->size.height = pix->height; - format->fourcc = pix->pixelformat; + format->size.width = 0; + format->size.height = 0; + format->fourcc = pix->dataformat; format->planesCount = 1; - format->planes[0].bpl = pix->bytesperline; - format->planes[0].size = pix->sizeimage; + format->planes[0].bpl = pix->buffersize; + format->planes[0].size = pix->buffersize; return 0; } -int V4L2Device::setFormatSingleplane(V4L2DeviceFormat *format) +int V4L2Device::setFormatMeta(V4L2DeviceFormat *format) { struct v4l2_format v4l2Format = {}; - struct v4l2_pix_format *pix = &v4l2Format.fmt.pix; + struct v4l2_meta_format *pix = &v4l2Format.fmt.meta; int ret; v4l2Format.type = bufferType_; - pix->width = format->size.width; - pix->height = format->size.height; - pix->pixelformat = format->fourcc; - pix->bytesperline = format->planes[0].bpl; - pix->field = V4L2_FIELD_NONE; - + pix->dataformat = format->fourcc; ret = ioctl(fd_, VIDIOC_S_FMT, &v4l2Format); if (ret) { ret = -errno; @@ -495,12 +498,12 @@ int V4L2Device::setFormatSingleplane(V4L2DeviceFormat *format) * Return to caller the format actually applied on the device, * which might differ from the requested one. */ - format->size.width = pix->width; - format->size.height = pix->height; - format->fourcc = pix->pixelformat; + format->size.width = 0; + format->size.height = 0; + format->fourcc = format->fourcc; format->planesCount = 1; - format->planes[0].bpl = pix->bytesperline; - format->planes[0].size = pix->sizeimage; + format->planes[0].bpl = pix->buffersize; + format->planes[0].size = pix->buffersize; return 0; } @@ -573,6 +576,63 @@ int V4L2Device::setFormatMultiplane(V4L2DeviceFormat *format) return 0; } +int V4L2Device::getFormatSingleplane(V4L2DeviceFormat *format) +{ + struct v4l2_format v4l2Format = {}; + struct v4l2_pix_format *pix = &v4l2Format.fmt.pix; + int ret; + + v4l2Format.type = bufferType_; + ret = ioctl(fd_, VIDIOC_G_FMT, &v4l2Format); + if (ret) { + ret = -errno; + LOG(V4L2, Error) << "Unable to get format: " << strerror(-ret); + return ret; + } + + format->size.width = pix->width; + format->size.height = pix->height; + format->fourcc = pix->pixelformat; + format->planesCount = 1; + format->planes[0].bpl = pix->bytesperline; + format->planes[0].size = pix->sizeimage; + + return 0; +} + +int V4L2Device::setFormatSingleplane(V4L2DeviceFormat *format) +{ + struct v4l2_format v4l2Format = {}; + struct v4l2_pix_format *pix = &v4l2Format.fmt.pix; + int ret; + + v4l2Format.type = bufferType_; + pix->width = format->size.width; + pix->height = format->size.height; + pix->pixelformat = format->fourcc; + pix->bytesperline = format->planes[0].bpl; + pix->field = V4L2_FIELD_NONE; + ret = ioctl(fd_, VIDIOC_S_FMT, &v4l2Format); + if (ret) { + ret = -errno; + LOG(V4L2, Error) << "Unable to set format: " << strerror(-ret); + return ret; + } + + /* + * Return to caller the format actually applied on the device, + * which might differ from the requested one. + */ + format->size.width = pix->width; + format->size.height = pix->height; + format->fourcc = pix->pixelformat; + format->planesCount = 1; + format->planes[0].bpl = pix->bytesperline; + format->planes[0].size = pix->sizeimage; + + return 0; +} + int V4L2Device::requestBuffers(unsigned int count) { struct v4l2_requestbuffers rb = {};