From 1cf709b1d2bbaf95e8a4bd3eb8962340e372a76a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20S=C3=B6derlund?= Date: Fri, 24 May 2019 23:44:47 +0200 Subject: libcamera: formats: Add ImageFormats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new class to hold format information for V4L2 devices and subdevices. The object describes the relationship between either pixel formats (V4L2 devices) or media bus codes (V4L2 subdevice) and a list of image sizes which can be produced with that format. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/libcamera/formats.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) (limited to 'src/libcamera/formats.cpp') diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp index 56f4ddb5..312db655 100644 --- a/src/libcamera/formats.cpp +++ b/src/libcamera/formats.cpp @@ -24,4 +24,92 @@ namespace libcamera { * resolutions represented by SizeRange items. */ +/** + * \class ImageFormats + * \brief Describe V4L2Device and V4L2SubDevice image formats + * + * This class stores a list of image formats, each associated with a + * corresponding set of image sizes. It is used to describe the formats and + * sizes supported by a V4L2Device or V4L2Subdevice. + * + * Formats are stored as an integer. When used for a V4L2Device, the image + * formats are fourcc pixel formats. When used for a V4L2Subdevice they are + * media bus codes. Both are defined by the V4L2 specification. + * + * Sizes are stored as a list of SizeRange. + */ + +/** + * \brief Add a format and corresponding sizes to the description + * \param[in] format Pixel format or media bus code to describe + * \param[in] sizes List of supported size ranges for the format + * + * \return 0 on success or a negative error code otherwise + * \retval -EEXIST The format is already described + */ +int ImageFormats::addFormat(unsigned int format, const std::vector &sizes) +{ + if (data_.find(format) != data_.end()) + return -EEXIST; + + data_[format] = sizes; + + return 0; +} + +/** + * \brief Check if the list of devices supported formats is empty + * \return True if the list of supported formats is empty + */ +bool ImageFormats::isEmpty() const +{ + return data_.empty(); +} + +/** + * \brief Retrieve a list of all supported image formats + * \return List of pixel formats or media bus codes + */ +std::vector ImageFormats::formats() const +{ + std::vector formats; + formats.reserve(data_.size()); + + /* \todo: Should this be cached instead of computed each time? */ + for (auto const &it : data_) + formats.push_back(it.first); + + return formats; +} + +/** + * \brief Retrieve all sizes for a specific format + * \param[in] format The pixel format or mbus code + * + * Retrieve all size ranges for a specific format. For V4L2Device \a format is a + * pixel format while for a V4L2Subdevice \a format is a media bus code. + * + * \return The list of image sizes supported for \a format, or an empty list if + * the format is not supported + */ +const std::vector &ImageFormats::sizes(unsigned int format) const +{ + static std::vector empty; + + auto const &it = data_.find(format); + if (it == data_.end()) + return empty; + + return it->second; +} + +/** + * \brief Retrieve the map that associates formats to image sizes + * \return The map that associates formats to image sizes + */ +const std::map> &ImageFormats::data() const +{ + return data_; +} + } /* namespace libcamera */ -- cgit v1.2.1