[{"id":2019,"web_url":"https://patchwork.libcamera.org/comment/2019/","msgid":"<20190625114406.GA5002@pendragon.ideasonboard.com>","date":"2019-06-25T11:44:06","subject":"Re: [libcamera-devel] [PATCH v7 1/6] libcamera: Add V4L2Controls","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Tue, Jun 25, 2019 at 08:48:25AM +0200, Jacopo Mondi wrote:\n> Add libcamera V4L2 control support, implemented using the V4L2 Extended\n> Control APIs. This patch defines the types used to create and manage\n> controls.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> ---\n>  src/libcamera/include/v4l2_controls.h |  80 +++++++\n>  src/libcamera/meson.build             |   1 +\n>  src/libcamera/v4l2_controls.cpp       | 289 ++++++++++++++++++++++++++\n>  3 files changed, 370 insertions(+)\n>  create mode 100644 src/libcamera/include/v4l2_controls.h\n>  create mode 100644 src/libcamera/v4l2_controls.cpp\n> \n> diff --git a/src/libcamera/include/v4l2_controls.h b/src/libcamera/include/v4l2_controls.h\n> new file mode 100644\n> index 000000000000..2c8cb9003f25\n> --- /dev/null\n> +++ b/src/libcamera/include/v4l2_controls.h\n> @@ -0,0 +1,80 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * v4l2_controls.h - V4L2 Controls Support\n> + */\n> +\n> +#ifndef __LIBCAMERA_V4L2_CONTROLS_H__\n> +#define __LIBCAMERA_V4L2_CONTROLS_H__\n> +\n> +#include <string>\n> +#include <vector>\n> +\n> +#include <stdint.h>\n> +\n> +#include <linux/v4l2-controls.h>\n> +#include <linux/videodev2.h>\n> +\n> +namespace libcamera {\n> +\n> +class V4L2ControlInfo\n> +{\n> +public:\n> +\tV4L2ControlInfo(const struct v4l2_query_ext_ctrl &ctrl);\n> +\n> +\tunsigned int id() const { return id_; }\n> +\tunsigned int type() const { return type_; }\n> +\tsize_t size() const { return size_; }\n> +\tconst std::string &name() const { return name_; }\n> +\n> +private:\n> +\tunsigned int id_;\n> +\tunsigned int type_;\n> +\tsize_t size_;\n> +\tstd::string name_;\n> +};\n> +\n> +class V4L2Control\n> +{\n> +public:\n> +\tV4L2Control(unsigned int id, int value = 0)\n> +\t\t: id_(id), value_(value) {}\n> +\n> +\tint64_t value() const { return value_; }\n> +\tvoid setValue(int64_t value) { value_ = value; }\n> +\n> +\tunsigned int id() const { return id_; }\n> +\n> +private:\n> +\tunsigned int id_;\n> +\tint64_t value_;\n> +};\n> +\n> +class V4L2ControlList\n> +{\n> +public:\n> +\tusing iterator = std::vector<V4L2Control>::iterator;\n> +\tusing const_iterator = std::vector<V4L2Control>::const_iterator;\n> +\n> +\titerator begin() { return controls_.begin(); }\n> +\tconst_iterator begin() const { return controls_.begin(); }\n> +\titerator end() { return controls_.end(); }\n> +\tconst_iterator end() const { return controls_.end(); }\n> +\n> +\tbool empty() const { return controls_.empty(); }\n> +\tstd::size_t size() const { return controls_.size(); }\n> +\n> +\tvoid clear() { controls_.clear(); }\n> +\tvoid add(unsigned int id, int64_t value = 0);\n> +\n> +\tV4L2Control *getByIndex(unsigned int index);\n> +\tV4L2Control *operator[](unsigned int id);\n> +\n> +private:\n> +\tstd::vector<V4L2Control> controls_;\n> +};\n> +\n> +} /* namespace libcamera */\n> +\n> +#endif /* __LIBCAMERA_V4L2_CONTROLS_H__ */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index f26ad5b2dc57..985aa7e8ab0e 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -23,6 +23,7 @@ libcamera_sources = files([\n>      'stream.cpp',\n>      'timer.cpp',\n>      'utils.cpp',\n> +    'v4l2_controls.cpp',\n>      'v4l2_device.cpp',\n>      'v4l2_subdevice.cpp',\n>      'v4l2_videodevice.cpp',\n> diff --git a/src/libcamera/v4l2_controls.cpp b/src/libcamera/v4l2_controls.cpp\n> new file mode 100644\n> index 000000000000..78888de29642\n> --- /dev/null\n> +++ b/src/libcamera/v4l2_controls.cpp\n> @@ -0,0 +1,289 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2019, Google Inc.\n> + *\n> + * v4l2_controls.cpp - V4L2 Controls Support\n> + */\n> +\n> +#include \"v4l2_controls.h\"\n> +\n> +/**\n> + * \\file v4l2_controls.h\n> + * \\brief Support for V4L2 Controls using the V4L2 Extended Controls APIs\n> + *\n> + * The V4L2 Control API allows application to inspect and modify sets of\n> + * configurable parameters on a video device or subdevice. The nature of the\n> + * parameters an application can modify using the control framework depends on\n> + * what the driver implements support for, and on the characteristics of the\n> + * underlying hardware platform. Generally controls are used to modify user\n> + * visible settings, such as the image brightness and exposure time, or\n> + * non-standard parameters which cannot be controlled through the V4L2 format\n> + * negotiation API.\n> + *\n> + * Controls are identified by a numerical ID, defined by the V4L2 kernel headers\n> + * and have an associated type. Each control has a value, which is the data that\n> + * can be modified with V4L2Device::setControls() or retrieved with\n> + * V4L2Device::getControls().\n> + *\n> + * The control's type along with the control's flags define the type of the\n> + * control's value content. Controls can transport a single data value stored in\n> + * variable inside the control, or they might as well deal with more complex\n> + * data types, such as arrays of matrices, stored in a contiguous memory\n> + * locations associated with the control and called 'the payload'. Such controls\n> + * are called 'compound controls' and are currently not supported by the\n> + * libcamera V4L2 control framework.\n> + *\n> + * libcamera implements support for controls using the V4L2 Extended Control\n> + * API, which allows future handling of controls with payloads of arbitrary\n> + * sizes.\n> + *\n> + * The libcamera V4L2 Controls framework operates on lists of controls, wrapped\n> + * by the V4L2ControlList class, to match the V4L2 extended controls API. The\n> + * interface to set and get control is implemented by the V4L2Device class, and\n> + * this file only provides the data type definitions.\n> + *\n> + * \\todo Add support for compound controls\n> + */\n> +\n> +namespace libcamera {\n> +\n> +/**\n> + * \\class V4L2ControlInfo\n> + * \\brief Information on a V4L2 control\n> + *\n> + * The V4L2ControlInfo class represents all the information related to a V4L2\n> + * control, such as its ID, its type, its user-readable name and the expected\n> + * size of its value data.\n> + *\n> + * V4L2ControlInfo instances are created by inspecting the fieldS of a struct\n> + * v4l2_query_ext_ctrl structure, after it has been filled by the device driver\n> + * as a consequence of a VIDIOC_QUERY_EXT_CTRL ioctl call.\n> + *\n> + * This class does not contain the control value, but only static information on\n> + * the control, which shall be cached by the caller at initialisation time or\n> + * the first time the control information is accessed.\n> + */\n> +\n> +/**\n> + * \\brief Construct a V4L2ControlInfo from a struct v4l2_query_ext_ctrl\n> + * \\param ctrl The struct v4l2_query_ext_ctrl as returned by the kernel\n> + */\n> +V4L2ControlInfo::V4L2ControlInfo(const struct v4l2_query_ext_ctrl &ctrl)\n> +{\n> +\tid_ = ctrl.id;\n> +\ttype_ = ctrl.type;\n> +\tname_ = static_cast<const char *>(ctrl.name);\n> +\tsize_ = ctrl.elem_size * ctrl.elems;\n> +}\n> +\n> +/**\n> + * \\fn V4L2ControlInfo::id()\n> + * \\brief Retrieve the control ID\n> + * \\return The V4L2 control ID\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlInfo::type()\n> + * \\brief Retrieve the control type as defined by V4L2_CTRL_TYPE_*\n> + * \\return The V4L2 control type\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlInfo::size()\n> + * \\brief Retrieve the control value data size (in bytes)\n> + * \\return The V4L2 control value data size\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlInfo::name()\n> + * \\brief Retrieve the control user readable name\n> + * \\return The V4L2 control user readable name\n> + */\n> +\n> +/**\n> + * \\class V4L2Control\n> + * \\brief A V4L2 control value\n> + *\n> + * The V4L2Control class represent the value of a V4L2 control. The class\n> + * stores values that have been read from or will be applied to a V4L2 device.\n> + *\n> + * The value stored in the class instances does not reflect what is actually\n> + * applied to the hardware but is a pure software cache optionally initialized\n> + * at control creation time and modified by a control read or write operation.\n> + *\n> + * The write and read controls the V4L2Control class instances are not meant\n> + * to be directly used but are instead intended to be grouped in\n> + * V4L2ControlList instances, which are then passed as parameters to\n> + * V4L2Device::setControls() and V4L2Device::getControls() operations.\n> + */\n> +\n> +/**\n> + * \\fn V4L2Control::V4L2Control\n> + * \\brief Construct a V4L2 control with \\a id and \\a value\n> + * \\param id The V4L2 control ID\n> + * \\param value The control value\n> + */\n> +\n> +/**\n> + * \\fn V4L2Control::value()\n> + * \\brief Retrieve the value of the control\n> + *\n> + * This method returns the cached control value, initially set by\n> + * V4L2ControlList::add() and then updated when the controls are read or\n> + * written with V4L2Device::getControls() and V4L2Device::setControls().\n> + *\n> + * \\return The V4L2 control value\n> + */\n> +\n> +/**\n> + * \\fn V4L2Control::setValue()\n> + * \\brief Set the value of the control\n> + * \\param value The new V4L2 control value\n> + *\n> + * This method stores the control value, which will be applied to the\n> + * device when calling V4L2Device::setControls().\n> + */\n> +\n> +/**\n> + * \\fn V4L2Control::id()\n> + * \\brief Retrieve the control ID this instance refers to\n> + * \\return The V4L2Control ID\n> + */\n> +\n> +/**\n> + * \\class V4L2ControlList\n> + * \\brief Container of V4L2Control instances\n> + *\n> + * The V4L2ControlList class works as a container for a list of V4L2Control\n> + * instances. The class provides operations to add a new control to the list,\n> + * get back a control value, and reset the list of controls it contains.\n> + *\n> + * In order to set and get controls, user of the libcamera V4L2 control\n> + * framework should operate on instances of the V4L2ControlList class, and use\n> + * them as argument for the V4L2Device::setControls() and\n> + * V4L2Device::getControls() operations, which write and read a list of\n> + * controls to or from a V4L2 device (a video device or a subdevice).\n> + *\n> + * Controls are added to a V4L2ControlList instance with the add() method, with\n> + * or without a value.\n> + *\n> + * To write controls to a device, the controls of interest shall be added with\n> + * an initial value by calling V4L2ControlList::add(unsigned int id, int64_t\n> + * value) to prepare for a write operation. Once the values of all controls of\n> + * interest have been added, the V4L2ControlList instance is passed to the\n> + * V4L2Device::setControls(), which sets the controls on the device.\n> + *\n> + * To read controls from a device, the desired controls are added with\n> + * V4L2ControlList::add(unsigned int id) to prepare for a read operation. The\n> + * V4L2ControlList instance is then passed to V4L2Device::getControls(), which\n> + * reads the controls from the device and updates the values stored in\n> + * V4L2ControlList.\n> + *\n> + * V4L2ControlList instances can be reset to remove all controls they contain\n> + * and prepare to be re-used for a new control write/read sequence.\n> + */\n> +\n> +/**\n> + * \\typedef V4L2ControlList::iterator\n> + * \\brief Iterator on the V4L2 controls contained in the instance\n> + */\n> +\n> +/**\n> + * \\typedef V4L2ControlList::const_iterator\n> + * \\brief Const iterator on the V4L2 controls contained in the instance\n> + */\n> +\n> +/**\n> + * \\fn iterator V4L2ControlList::begin()\n> + * \\brief Retrieve an iterator to the first V4L2Control in the instance\n> + * \\return An iterator to the first V4L2 control\n> + */\n> +\n> +/**\n> + * \\fn const_iterator V4L2ControlList::begin() const\n> + * \\brief Retrieve a constant iterator to the first V4L2Control in the instance\n> + * \\return A constant iterator to the first V4L2 control\n> + */\n> +\n> +/**\n> + * \\fn iterator V4L2ControlList::end()\n> + * \\brief Retrieve an iterator pointing to the past-the-end V4L2Control in the\n> + * instance\n> + * \\return An iterator to the element following the last V4L2 control in the\n> + * instance\n> + */\n> +\n> +/**\n> + * \\fn const_iterator V4L2ControlList::end() const\n> + * \\brief Retrieve a constant iterator pointing to the past-the-end V4L2Control\n> + * in the instance\n> + * \\return A constant iterator to the element following the last V4L2 control\n> + * in the instance\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlList::empty()\n> + * \\brief Verify if the instance does not contain any control\n> + * \\return True if the instance does not contain any control, false otherwise\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlList::size()\n> + * \\brief Retrieve the number on controls in the instance\n> + * \\return The number of V4L2Control stored in the instance\n> + */\n> +\n> +/**\n> + * \\fn V4L2ControlList::clear()\n> + * \\brief Remove all controls in the instance\n> + */\n> +\n> +/**\n> + * \\brief Add control with \\a id and optional \\a value to the instance\n> + * \\param id The V4L2 control ID (V4L2_CID_*)\n> + * \\param value The V4L2 control value\n> + *\n> + * This method adds a new V4L2 control to the V4L2ControlList. The newly\n> + * inserted control shall not already be present in the control lists, otherwise\n> + * this method, and further use of the control list lead to undefined behaviour.\n> + */\n> +void V4L2ControlList::add(unsigned int id, int64_t value)\n> +{\n> +\tcontrols_.emplace_back(id, value);\n> +}\n> +\n> +/**\n> + * \\brief Retrieve the control at \\a index\n> + * \\param[in] index The control index\n> + *\n> + * Controls are stored in a V4L2ControlList in order of insertion and this\n> + * method retrieves the control at \\a index.\n> + *\n> + * \\return A pointer to the V4L2Control at \\a index or nullptr if the\n> + * index is larger than the number of controls\n> + */\n> +V4L2Control *V4L2ControlList::getByIndex(unsigned int index)\n> +{\n> +\tif (index >= controls_.size())\n> +\t\treturn nullptr;\n> +\n> +\treturn &controls_[index];\n> +}\n> +\n> +/**\n> + * \\brief Retrieve the control with \\a id\n> + * \\param[in] id The V4L2 control ID (V4L2_CID_xx)\n> + * \\return A pointer to the V4L2Control with \\a id or nullptr if the\n> + * control ID is not part of this instance.\n> + */\n> +V4L2Control *V4L2ControlList::operator[](unsigned int id)\n> +{\n> +\tfor (V4L2Control &ctrl : controls_) {\n> +\t\tif (ctrl.id() == id)\n> +\t\t\treturn &ctrl;\n> +\t}\n> +\n> +\treturn nullptr;\n> +}\n> +\n> +} /* namespace libcamera */\n> -- \n> 2.21.0\n> \n> _______________________________________________\n> libcamera-devel mailing list\n> libcamera-devel@lists.libcamera.org\n> https://lists.libcamera.org/listinfo/libcamera-devel","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0722A60C29\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 25 Jun 2019 13:46:41 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8D08D510;\n\tTue, 25 Jun 2019 13:46:39 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1561463199;\n\tbh=CfcrKGEwTlrGBgS6JEW0uxjXzAPvFOuEcT6JgPuathM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=wSbJKxt/5FgL1fYee3Z7cQyR+TgfLdbCvf1A7XlSvet7xxqSQUq/Xx+9eXUiEC/2H\n\twRqJQ7O1lZLNS0BOVkvtKVK+e64bW1RerjLYLJOT3ntsPJwXkGdbfaSdReb+IuAm8i\n\t+OmxmyoUztl7y/GEuBX45sDzsbSfVa90UlItDZkY=","Date":"Tue, 25 Jun 2019 14:44:06 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190625114406.GA5002@pendragon.ideasonboard.com>","References":"<20190625064830.951-1-jacopo@jmondi.org>\n\t<20190625064830.951-2-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20190625064830.951-2-jacopo@jmondi.org>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v7 1/6] libcamera: Add V4L2Controls","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":"Tue, 25 Jun 2019 11:46:41 -0000"}}]