[{"id":1929,"web_url":"https://patchwork.libcamera.org/comment/1929/","msgid":"<20190619094645.GB5045@pendragon.ideasonboard.com>","date":"2019-06-19T09:46:45","subject":"Re: [libcamera-devel] [PATCH v4 09/16] libcamera: v4l2_device: Add\n\tenumeration of pixelformats and frame sizes","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Wed, Jun 19, 2019 at 04:51:22AM +0200, Niklas Söderlund wrote:\n> Add methods to enumerate pixelformats and frame sizes from a v4l2\n> device. The interface is similar to how V4L2Subdevice enumerates mbus\n> codes and frame sizes.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n> * Changes since v3\n> - Catch the error from V4L2Device::enumSizes() in V4L2Device::formats()\n> - s/pixelformats/pixel formats/\n> ---\n>  src/libcamera/include/v4l2_device.h |   5 ++\n>  src/libcamera/v4l2_device.cpp       | 114 ++++++++++++++++++++++++++++\n>  2 files changed, 119 insertions(+)\n> \n> diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\n> index bdecc087fe5afae0..1a67850ac4c1088f 100644\n> --- a/src/libcamera/include/v4l2_device.h\n> +++ b/src/libcamera/include/v4l2_device.h\n> @@ -16,6 +16,7 @@\n>  #include <libcamera/geometry.h>\n>  #include <libcamera/signal.h>\n>  \n> +#include \"formats.h\"\n>  #include \"log.h\"\n>  \n>  namespace libcamera {\n> @@ -132,6 +133,7 @@ public:\n>  \n>  \tint getFormat(V4L2DeviceFormat *format);\n>  \tint setFormat(V4L2DeviceFormat *format);\n> +\tImageFormats formats();\n>  \n>  \tint exportBuffers(BufferPool *pool);\n>  \tint importBuffers(BufferPool *pool);\n> @@ -163,6 +165,9 @@ private:\n>  \tint createPlane(Buffer *buffer, unsigned int plane,\n>  \t\t\tunsigned int length);\n>  \n> +\tstd::vector<unsigned int> enumPixelformats();\n> +\tstd::vector<SizeRange> enumSizes(unsigned int pixelFormat);\n> +\n>  \tBuffer *dequeueBuffer();\n>  \tvoid bufferAvailable(EventNotifier *notifier);\n>  \n> diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\n> index 0821bd75fb428766..e14fc6109a4db470 100644\n> --- a/src/libcamera/v4l2_device.cpp\n> +++ b/src/libcamera/v4l2_device.cpp\n> @@ -634,6 +634,33 @@ int V4L2Device::setFormatSingleplane(V4L2DeviceFormat *format)\n>  \treturn 0;\n>  }\n>  \n> +/**\n> + * \\brief Enumerate all pixel formats and frame sizes\n> + *\n> + * Enumerate all pixel formats and frame sizes supported by the video device.\n> + *\n> + * \\return A list of the supported device formats\n> + */\n> +ImageFormats V4L2Device::formats()\n> +{\n> +\tImageFormats formats;\n> +\n> +\tfor (unsigned int pixelformat : enumPixelformats()) {\n> +\t\tstd::vector<SizeRange> sizes = enumSizes(pixelformat);\n> +\t\tif (sizes.empty())\n> +\t\t\treturn {};\n> +\n> +\t\tif (formats.addFormat(pixelformat, sizes)) {\n> +\t\t\tLOG(V4L2, Error)\n> +\t\t\t\t<< \"Could not add sizes for pixel format \"\n> +\t\t\t\t<< pixelformat;\n> +\t\t\treturn {};\n> +\t\t}\n> +\t}\n> +\n> +\treturn formats;\n> +}\n> +\n>  int V4L2Device::requestBuffers(unsigned int count)\n>  {\n>  \tstruct v4l2_requestbuffers rb = {};\n> @@ -763,6 +790,93 @@ int V4L2Device::createPlane(Buffer *buffer, unsigned int planeIndex,\n>  \treturn 0;\n>  }\n>  \n> +std::vector<unsigned int> V4L2Device::enumPixelformats()\n> +{\n> +\tstd::vector<unsigned int> formats;\n> +\tint ret;\n> +\n> +\tfor (unsigned int index = 0; ; index++) {\n> +\t\tstruct v4l2_fmtdesc pixelformatEnum = {};\n> +\t\tpixelformatEnum.index = index;\n> +\t\tpixelformatEnum.type = bufferType_;\n> +\n> +\t\tret = ioctl(fd_, VIDIOC_ENUM_FMT, &pixelformatEnum);\n> +\t\tif (ret)\n> +\t\t\tbreak;\n> +\n> +\t\tformats.push_back(pixelformatEnum.pixelformat);\n> +\t}\n> +\n> +\tif (ret && errno != EINVAL) {\n> +\t\tret = -errno;\n> +\t\tLOG(V4L2, Error)\n> +\t\t\t<< \"Unable to enumerate pixel formats: \"\n> +\t\t\t<< strerror(-ret);\n> +\t\treturn {};\n> +\t}\n> +\n> +\treturn formats;\n> +}\n> +\n> +std::vector<SizeRange> V4L2Device::enumSizes(unsigned int pixelFormat)\n> +{\n> +\tstd::vector<SizeRange> sizes;\n> +\tint ret;\n> +\n> +\tfor (unsigned int index = 0;; index++) {\n> +\t\tstruct v4l2_frmsizeenum frameSize = {};\n> +\t\tframeSize.index = index;\n> +\t\tframeSize.pixel_format = pixelFormat;\n> +\n> +\t\tret = ioctl(fd_, VIDIOC_ENUM_FRAMESIZES, &frameSize);\n> +\t\tif (ret)\n> +\t\t\tbreak;\n> +\n> +\t\tif (index != 0 &&\n> +\t\t    frameSize.type != V4L2_FRMSIZE_TYPE_DISCRETE) {\n> +\t\t\tLOG(V4L2, Error)\n> +\t\t\t\t<< \"Non-zero index for non discrete type\";\n> +\t\t\treturn {};\n> +\t\t}\n> +\n> +\t\tswitch (frameSize.type) {\n> +\t\tcase V4L2_FRMSIZE_TYPE_DISCRETE:\n> +\t\t\tsizes.emplace_back(frameSize.discrete.width,\n> +\t\t\t\t\t   frameSize.discrete.height);\n> +\t\t\tbreak;\n> +\t\tcase V4L2_FRMSIZE_TYPE_CONTINUOUS:\n> +\t\t\tsizes.emplace_back(frameSize.stepwise.min_width,\n> +\t\t\t\t\t   frameSize.stepwise.min_height,\n> +\t\t\t\t\t   frameSize.stepwise.max_width,\n> +\t\t\t\t\t   frameSize.stepwise.max_height);\n> +\t\t\tbreak;\n> +\t\tcase V4L2_FRMSIZE_TYPE_STEPWISE:\n> +\t\t\tsizes.emplace_back(frameSize.stepwise.min_width,\n> +\t\t\t\t\t   frameSize.stepwise.min_height,\n> +\t\t\t\t\t   frameSize.stepwise.max_width,\n> +\t\t\t\t\t   frameSize.stepwise.max_height,\n> +\t\t\t\t\t   frameSize.stepwise.step_width,\n> +\t\t\t\t\t   frameSize.stepwise.step_height);\n> +\t\t\tbreak;\n> +\t\tdefault:\n> +\t\t\tLOG(V4L2, Error)\n> +\t\t\t\t<< \"Unknown VIDIOC_ENUM_FRAMESIZES type \"\n> +\t\t\t\t<< frameSize.type;\n> +\t\t\treturn {};\n> +\t\t}\n> +\t}\n> +\n> +\tif (ret && errno != EINVAL) {\n> +\t\tret = -errno;\n> +\t\tLOG(V4L2, Error)\n> +\t\t\t<< \"Unable to enumerate frame sizes: \"\n> +\t\t\t<< strerror(-ret);\n> +\t\treturn {};\n> +\t}\n> +\n> +\treturn sizes;\n> +}\n> +\n>  /**\n>   * \\brief Import the externally allocated \\a pool of buffers\n>   * \\param[in] pool BufferPool of buffers to import","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5A6BA61A13\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 19 Jun 2019 11:47:08 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C8B9A333;\n\tWed, 19 Jun 2019 11:47:07 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1560937627;\n\tbh=6mmIwHX6tVwIupsihTBjNHUE9+z+sH7+xmWjQYo9KIA=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=KD8L+o4FXK9Dhh7aGYQV1I6sOJrGOAZyD0kM/QJZhYfwqA72UpHh9QNRZJdnzWTSD\n\t/5bY3vfsNgEvfIreT8bbw9/ShwNEpe2LrbdOBEEFW1HTYDt61CN87TcSz0Y+na8f9S\n\ti5jr8/D3dAhCflD7TPWQAU7u1B+LvsxiLOYesCMM=","Date":"Wed, 19 Jun 2019 12:46:45 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190619094645.GB5045@pendragon.ideasonboard.com>","References":"<20190619025129.21164-1-niklas.soderlund@ragnatech.se>\n\t<20190619025129.21164-10-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190619025129.21164-10-niklas.soderlund@ragnatech.se>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v4 09/16] libcamera: v4l2_device: Add\n\tenumeration of pixelformats and frame sizes","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Wed, 19 Jun 2019 09:47:08 -0000"}}]