From 3e51cc30bf9d71e06b0fdc252c5aed70047429cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20S=C3=B6derlund?= Date: Sat, 25 May 2019 00:32:00 +0200 Subject: libcamera: v4l2_subdevice: Breakout mbus code enumeration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify frame size enumeration by breaking out mbus code enumeration in a helper, making the code easier to read while also preparing for enhancing the frame size enumeration. There is no functional change. Signed-off-by: Niklas Söderlund Reviewed-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- src/libcamera/v4l2_subdevice.cpp | 59 ++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 26 deletions(-) (limited to 'src/libcamera/v4l2_subdevice.cpp') diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp index fceee331..99e202fa 100644 --- a/src/libcamera/v4l2_subdevice.cpp +++ b/src/libcamera/v4l2_subdevice.cpp @@ -203,37 +203,15 @@ int V4L2Subdevice::setCompose(unsigned int pad, Rectangle *rect) FormatEnum V4L2Subdevice::formats(unsigned int pad) { FormatEnum formatMap = {}; - struct v4l2_subdev_mbus_code_enum mbusEnum = {}; - int ret; if (pad >= entity_->pads().size()) { LOG(V4L2Subdev, Error) << "Invalid pad: " << pad; - return formatMap; + return {}; } - mbusEnum.pad = pad; - mbusEnum.index = 0; - mbusEnum.which = V4L2_SUBDEV_FORMAT_ACTIVE; - while (true) { - ret = ioctl(fd_, VIDIOC_SUBDEV_ENUM_MBUS_CODE, &mbusEnum); - if (ret) - break; - - ret = enumPadSizes(pad, mbusEnum.code, - &formatMap[mbusEnum.code]); - if (ret) - break; - - mbusEnum.index++; - } - - if (ret && (errno != EINVAL && errno != ENOTTY)) { - ret = -errno; - LOG(V4L2Subdev, Error) - << "Unable to enumerate formats on pad " << pad - << ": " << strerror(-ret); - formatMap.clear(); - } + for (unsigned int code : enumPadCodes(pad)) + if (enumPadSizes(pad, code, &formatMap[code])) + return {}; return formatMap; } @@ -328,6 +306,35 @@ std::string V4L2Subdevice::logPrefix() const return "'" + entity_->name() + "'"; } +std::vector V4L2Subdevice::enumPadCodes(unsigned int pad) +{ + std::vector codes; + int ret; + + for (unsigned int index = 0; ; index++) { + struct v4l2_subdev_mbus_code_enum mbusEnum = {}; + mbusEnum.pad = pad; + mbusEnum.index = index; + mbusEnum.which = V4L2_SUBDEV_FORMAT_ACTIVE; + + ret = ioctl(fd_, VIDIOC_SUBDEV_ENUM_MBUS_CODE, &mbusEnum); + if (ret) + break; + + codes.push_back(mbusEnum.code); + } + + if (ret && errno != EINVAL) { + ret = errno; + LOG(V4L2Subdev, Error) + << "Unable to enumerate formats on pad " << pad + << ": " << strerror(ret); + return {}; + } + + return codes; +} + int V4L2Subdevice::enumPadSizes(unsigned int pad,unsigned int code, std::vector *sizes) { -- cgit v1.2.1