[{"id":14836,"web_url":"https://patchwork.libcamera.org/comment/14836/","msgid":"<c96e0732-f560-1d35-7e79-97a499572b69@ideasonboard.com>","date":"2021-01-27T17:39:11","subject":"Re: [libcamera-devel] [PATCH v5 1/8] libcamera: delayed_controls:\n\tAdd helper for controls that apply with a delay","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Hi Niklas,\n\nOn 16/01/2021 11:47, Niklas Söderlund wrote:\n> Some sensor controls take effect with a delay as the sensor needs time\n> to adjust, for example exposure. Add an optional helper DelayedControls\n> to help pipelines deal with such controls.\n> \n> The idea is to provide a queue of controls towards the V4L2 device and\n> apply individual controls with the specified delay with the aim to get\n> predictable and retrievable control values for any given frame. To do\n> this the queue of controls needs to be at least as deep as the control\n> with the largest delay.\n> \n> The DelayedControls needs to be informed of every start of exposure.\n> This can be emulated but the helper is designed to be used with this\n> event being provide by the kernel through V4L2 events.\n> \n> This helper is based on StaggeredCtrl from the Raspberry Pi pipeline\n> handler but expands on its API. This helpers aims to replace the\n> Raspberry Pi implementations and mimics it behavior perfectly.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> Reviewed-by: Naushir Patuck <naush@raspberrypi.com>\n> ---\n> * Changes since v4\n> - Fold queue() into push() as the mutex was dropped in v3 having a\n>   public accessors that takes the mutex is no longer needed.\n> - Add delayed_controls.h to meson.build.\n> - Update patch subject and commit message.\n> - Have class Info inherit from ControlValue.\n> - Add todo to reevaluate if maps should be indexed on ControlID or\n>   unsigned int.\n> - Update documentation.\n> \n> * Changes since v3\n> - Drop optional argument to reset().\n> - Update commit message.\n> - Remove usage of Mutex.\n> - Rename frameStart() to applyControls)_.\n> - Rename ControlInfo to Into.\n> - Rename ControlArray to ControlRingBuffer.\n> - Drop ControlsDelays and ControlsValues.\n> - Sort headers.\n> - Rename iterators.\n> - Simplify queueCount_ handeling in reset().\n> - Add more warnings.\n> - Update documentation.\n> \n> * Changes since v2\n> - Improve error logic in queue() as suggested by Jean-Michel Hautbois.\n> - s/fistSequence_/firstSequence_/\n> \n> * Changes since v1\n> - Correct copyright to reflect work is derived from Raspberry Pi\n>   pipeline handler. This was always the intention and was wrong in v1.\n> - Rewrite large parts of the documentation.\n> - Join two loops to one in DelayedControls::DelayedControls()\n> ---\n>  include/libcamera/internal/delayed_controls.h |  81 ++++++\n>  include/libcamera/internal/meson.build        |   1 +\n>  src/libcamera/delayed_controls.cpp            | 247 ++++++++++++++++++\n>  src/libcamera/meson.build                     |   1 +\n>  4 files changed, 330 insertions(+)\n>  create mode 100644 include/libcamera/internal/delayed_controls.h\n>  create mode 100644 src/libcamera/delayed_controls.cpp\n> \n> diff --git a/include/libcamera/internal/delayed_controls.h b/include/libcamera/internal/delayed_controls.h\n> new file mode 100644\n> index 0000000000000000..dc447a882514182f\n> --- /dev/null\n> +++ b/include/libcamera/internal/delayed_controls.h\n> @@ -0,0 +1,81 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd.\n> + *\n> + * delayed_controls.h - Helper to deal with controls that take effect with a delay\n> + */\n> +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__\n> +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__\n> +\n> +#include <stdint.h>\n> +#include <unordered_map>\n> +\n> +#include <libcamera/controls.h>\n> +\n> +namespace libcamera {\n> +\n> +class V4L2Device;\n> +\n> +class DelayedControls\n> +{\n> +public:\n> +\tDelayedControls(V4L2Device *device,\n> +\t\t\tconst std::unordered_map<uint32_t, unsigned int> &delays);\n> +\n> +\tvoid reset();\n> +\n> +\tbool push(const ControlList &controls);\n> +\tControlList get(uint32_t sequence);\n> +\n> +\tvoid applyControls(uint32_t sequence);\n> +\n> +private:\n> +\tclass Info : public ControlValue\n> +\t{\n> +\tpublic:\n> +\t\tInfo()\n> +\t\t\t: updated(false)\n> +\t\t{\n> +\t\t}\n> +\n> +\t\tInfo(const ControlValue &v)\n> +\t\t\t: ControlValue(v), updated(true)\n> +\t\t{\n> +\t\t}\n> +\n> +\t\tbool updated;\n> +\t};\n> +\n> +\t/* \\todo: Make the listSize configurable at instance creation time. */\n> +\tstatic constexpr int listSize = 16;\n> +\tclass ControlRingBuffer : public std::array<Info, listSize>\n> +\t{\n> +\tpublic:\n> +\t\tInfo &operator[](unsigned int index)\n> +\t\t{\n> +\t\t\treturn std::array<Info, listSize>::operator[](index % listSize);\n> +\t\t}\n> +\n> +\t\tconst Info &operator[](unsigned int index) const\n> +\t\t{\n> +\t\t\treturn std::array<Info, listSize>::operator[](index % listSize);\n> +\t\t}\n> +\t};\n> +\n> +\tV4L2Device *device_;\n> +\t/* \\todo Evaluate if we should index on ControlId * or unsigned int */\n> +\tstd::unordered_map<const ControlId *, unsigned int> delays_;\n> +\tunsigned int maxDelay_;\n> +\n> +\tbool running_;\n> +\tuint32_t firstSequence_;\n> +\n> +\tuint32_t queueCount_;\n> +\tuint32_t writeCount_;\n> +\t/* \\todo Evaluate if we should index on ControlId * or unsigned int */\n> +\tstd::unordered_map<const ControlId *, ControlRingBuffer> values_;\n\nOh, so we have a ring buffer for every control. That sort of surprises\nme a little.\n\nWould a ring buffer of ControlLists make sense? Or are they more\ndifficult to instantiate? (That wouldn't surprise me ;D)\n\n\n> +};\n> +\n> +} /* namespace libcamera */\n> +\n> +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */\n> diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build\n> index 1b1bdc77951235a5..e67a359fb8656f71 100644\n> --- a/include/libcamera/internal/meson.build\n> +++ b/include/libcamera/internal/meson.build\n> @@ -17,6 +17,7 @@ libcamera_internal_headers = files([\n>      'camera_sensor.h',\n>      'control_serializer.h',\n>      'control_validator.h',\n> +    'delayed_controls.h',\n>      'device_enumerator.h',\n>      'device_enumerator_sysfs.h',\n>      'device_enumerator_udev.h',\n> diff --git a/src/libcamera/delayed_controls.cpp b/src/libcamera/delayed_controls.cpp\n> new file mode 100644\n> index 0000000000000000..f24db43d7f1e357d\n> --- /dev/null\n> +++ b/src/libcamera/delayed_controls.cpp\n> @@ -0,0 +1,247 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd.\n> + *\n> + * delayed_controls.h - Helper to deal with controls that take effect with a delay\n> + */\n> +\n> +#include \"libcamera/internal/delayed_controls.h\"\n> +\n> +#include <libcamera/controls.h>\n> +\n> +#include \"libcamera/internal/log.h\"\n> +#include \"libcamera/internal/v4l2_device.h\"\n> +\n> +/**\n> + * \\file delayed_controls.h\n> + * \\brief Helper to deal with controls that take effect with a delay\n> + */\n> +\n> +namespace libcamera {\n> +\n> +LOG_DEFINE_CATEGORY(DelayedControls)\n> +\n> +/**\n> + * \\class DelayedControls\n> + * \\brief Helper to deal with controls that take effect with a delay\n> + *\n> + * Some sensor controls take effect with a delay as the sensor needs time to\n> + * adjust, for example exposure and analog gain. This is a helper class to deal\n> + * with such controls and the intended users are pipeline handlers.\n> + *\n> + * The idea is to extend the concept of the buffer depth of a pipeline the\n> + * application needs to maintain to also cover controls. Just as with buffer\n> + * depth if the application keeps the number of requests queued above the\n> + * control depth the controls are guaranteed to take effect for the correct\n> + * request. The control depth is determined by the control with the greatest\n> + * delay.\n> + */\n> +\n> +/**\n> + * \\brief Construct a DelayedControls instance\n> + * \\param[in] device The V4L2 device the controls have to be applied to\n> + * \\param[in] delays Map of the numerical V4L2 control ids to their associated\n> + * delays (in frames)\n> + *\n> + * Only controls specified in \\a delays are handled. If it's desired to mix\n> + * delayed controls and controls that take effect immediately the immediate\n> + * controls must be listed in the \\a delays map with a delay value of 0.\n> + */\n> +DelayedControls::DelayedControls(V4L2Device *device,\n> +\t\t\t\t const std::unordered_map<uint32_t, unsigned int> &delays)\n> +\t: device_(device), maxDelay_(0)\n> +{\n> +\tconst ControlInfoMap &controls = device_->controls();\n> +\n> +\t/*\n> +\t * Create a map of control ids to delays for controls exposed by the\n> +\t * device.\n> +\t */\n> +\tfor (auto const &delay : delays) {\n> +\t\tauto it = controls.find(delay.first);\n> +\t\tif (it == controls.end()) {\n> +\t\t\tLOG(DelayedControls, Error)\n> +\t\t\t\t<< \"Delay request for control id \"\n> +\t\t\t\t<< utils::hex(delay.first)\n> +\t\t\t\t<< \" but control is not exposed by device \"\n> +\t\t\t\t<< device_->deviceNode();\n> +\t\t\tcontinue;\n> +\t\t}\n> +\n> +\t\tconst ControlId *id = it->first;\n> +\n> +\t\tdelays_[id] = delay.second;\n> +\n> +\t\tLOG(DelayedControls, Debug)\n> +\t\t\t<< \"Set a delay of \" << delays_[id]\n> +\t\t\t<< \" for \" << id->name();\n> +\n> +\t\tmaxDelay_ = std::max(maxDelay_, delays_[id]);\n> +\t}\n> +\n> +\treset();\n> +}\n> +\n> +/**\n> + * \\brief Reset state machine\n> + *\n> + * Resets the state machine to a starting position based on control values\n> + * retrieved from the device.\n> + */\n> +void DelayedControls::reset()\n> +{\n> +\trunning_ = false;\n> +\tfirstSequence_ = 0;\n> +\tqueueCount_ = 1;\n> +\twriteCount_ = 0;\n> +\n> +\t/* Retrieve control as reported by the device. */\n> +\tstd::vector<uint32_t> ids;\n> +\tfor (auto const &delay : delays_)\n> +\t\tids.push_back(delay.first->id());\n> +\n> +\tControlList controls = device_->getControls(ids);\n> +\n> +\t/* Seed the control queue with the controls reported by the device. */\n> +\tvalues_.clear();\n> +\tfor (const auto &ctrl : controls) {\n> +\t\tconst ControlId *id = device_->controls().idmap().at(ctrl.first);\n> +\t\tvalues_[id][0] = Info(ctrl.second);\n> +\t}\n> +}\n> +\n> +/**\n> + * \\brief Push a set of controls on the queue\n> + * \\param[in] controls List of controls to add to the device queue\n> + *\n> + * Push a set of controls to the control queue. This increases the control queue\n> + * depth by one.\n> + *\n> + * \\returns true if \\a controls are accepted, or false otherwise\n> + */\n> +bool DelayedControls::push(const ControlList &controls)\n> +{\n> +\t/* Copy state from previous frame. */\n> +\tfor (auto &ctrl : values_) {\n> +\t\tInfo &info = ctrl.second[queueCount_];\n> +\t\tinfo = values_[ctrl.first][queueCount_ - 1];\n> +\t\tinfo.updated = false;\n> +\t}\n> +\n> +\t/* Update with new controls. */\n> +\tconst ControlIdMap &idmap = device_->controls().idmap();\n> +\tfor (const auto &control : controls) {\n> +\t\tconst auto &it = idmap.find(control.first);\n> +\t\tif (it == idmap.end()) {\n> +\t\t\tLOG(DelayedControls, Warning)\n> +\t\t\t\t<< \"Unknown control \" << control.first;\n> +\t\t\treturn false;\n> +\t\t}\n> +\n> +\t\tconst ControlId *id = it->second;\n> +\n> +\t\tif (delays_.find(id) == delays_.end())\n> +\t\t\treturn false;\n> +\n> +\t\tInfo &info = values_[id][queueCount_];\n> +\n> +\t\tinfo = control.second;\n> +\t\tinfo.updated = true;\n> +\n> +\t\tLOG(DelayedControls, Debug)\n> +\t\t\t<< \"Queuing \" << id->name()\n> +\t\t\t<< \" to \" << info.toString()\n> +\t\t\t<< \" at index \" << queueCount_;\n> +\t}\n> +\n> +\tqueueCount_++;\n> +\n> +\treturn true;\n> +}\n> +\n> +/**\n> + * \\brief Read back controls in effect at a sequence number\n> + * \\param[in] sequence The sequence number to get controls for\n> + *\n> + * Read back what controls where in effect at a specific sequence number. The\n> + * history is a ring buffer of 16 entries where new and old values coexist. It's\n> + * the callers responsibility to not read too old sequence numbers that have been\n> + * pushed out of the history.\n> + *\n> + * Historic values are evicted by pushing new values onto the queue using\n> + * push(). The max history from the current sequence number that yields valid\n> + * values are thus 16 minus number of controls pushed.\n> + *\n> + * \\return The controls at \\a sequence number\n> + */\n> +ControlList DelayedControls::get(uint32_t sequence)\n> +{\n> +\tuint32_t adjustedSeq = sequence - firstSequence_ + 1;\n> +\tunsigned int index = std::max<int>(0, adjustedSeq - maxDelay_);\n> +\n> +\tControlList out(device_->controls());\n> +\tfor (const auto &ctrl : values_) {\n\nI think I was a bit surprised to see that we have a ring per control, so\nI now see that this function is taking all the controls in parallel ring\nbuffers of a specific sequence and generating a list from them.\n\nThat makes me think even more that the ControlList could be the item on\nthe RingBuffer in the first place - but the list would have to be reset\nwhen 'removed' from the Ring I expect.\n\nPerhaps that's as 'simple' as a std::move() operation though.\n\n\n\n> +\t\tconst ControlId *id = ctrl.first;\n> +\t\tconst Info &info = ctrl.second[index];\n> +\n> +\t\tout.set(id->id(), info);\n> +\n> +\t\tLOG(DelayedControls, Debug)\n> +\t\t\t<< \"Reading \" << id->name()\n> +\t\t\t<< \" to \" << info.toString()\n> +\t\t\t<< \" at index \" << index;\n> +\t}\n> +\n> +\treturn out;\n> +}\n> +\n> +/**\n> + * \\brief Inform DelayedControls of the start of a new frame\n> + * \\param[in] sequence Sequence number of the frame that started\n> + *\n> + * Inform the state machine that a new frame has started and of its sequence\n> + * number. Any user of these helpers is responsible to inform the helper about\n> + * the start of any frame.This can be connected with ease to the start of a\n\ns/frame.This/frame. This/\n\n> + * exposure (SOE) V4L2 event.\n\nThis doesn't feel very clear to me.\n\nThis function 'writes' controls to the device, and\n\n> + */\n> +void DelayedControls::applyControls(uint32_t sequence)\n> +{\n\nIf this is occurring as a callback from the SOE v4L2 event, should there\nbe locking in here to protect variables such as writeCount_ or queueCount_ ?\n\n\n> +\tLOG(DelayedControls, Debug) << \"frame \" << sequence << \" started\";\n> +\n> +\tif (!running_) {\n> +\t\tfirstSequence_ = sequence;\n> +\t\trunning_ = true;\n> +\t}\n> +\n> +\t/*\n> +\t * Create control list peaking ahead in the value queue to ensure\n\ns/peaking/peeking/ ?\n\n\n> +\t * values are set in time to satisfy the sensor delay.\n> +\t */\n> +\tControlList out(device_->controls());\n> +\tfor (const auto &ctrl : values_) {\n> +\t\tconst ControlId *id = ctrl.first;\n> +\t\tunsigned int delayDiff = maxDelay_ - delays_[id];\n\nAha - actually - seeing that each control has different delays probably\nchanges my mind about storing a full ControlList in the ring buffer....\n\n\n\n> +\t\tunsigned int index = std::max<int>(0, writeCount_ - delayDiff);\n> +\t\tconst Info &info = ctrl.second[index];\n> +\n> +\t\tif (info.updated) {\n> +\t\t\tout.set(id->id(), info);\n> +\t\t\tLOG(DelayedControls, Debug)\n> +\t\t\t\t<< \"Setting \" << id->name()\n> +\t\t\t\t<< \" to \" << info.toString()\n> +\t\t\t\t<< \" at index \" << index;\n> +\t\t}\n> +\t}\n> +\n> +\twriteCount_++;\n> +\n> +\twhile (writeCount_ >= queueCount_) {\n> +\t\tLOG(DelayedControls, Debug)\n> +\t\t\t<< \"Queue is empty, auto queue no-op.\";\n> +\t\tpush({});\n> +\t}\n> +\n> +\tdevice_->setControls(&out);\n> +}\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644\n> --- a/src/libcamera/meson.build\n> +++ b/src/libcamera/meson.build\n> @@ -12,6 +12,7 @@ libcamera_sources = files([\n>      'controls.cpp',\n>      'control_serializer.cpp',\n>      'control_validator.cpp',\n> +    'delayed_controls.cpp',\n>      'device_enumerator.cpp',\n>      'device_enumerator_sysfs.cpp',\n>      'event_dispatcher.cpp',\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 5A1D9C0F2B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 27 Jan 2021 17:39:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8C02D6836F;\n\tWed, 27 Jan 2021 18:39:15 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F178360F2C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 27 Jan 2021 18:39:13 +0100 (CET)","from [192.168.0.20]\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8068F2C1;\n\tWed, 27 Jan 2021 18:39:13 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"QS7NycRn\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1611769153;\n\tbh=n0ZXcjPS1OCzFMqBEClwXChbYhl51ymzJ0k2jXJpmr4=;\n\th=Reply-To:Subject:To:References:From:Date:In-Reply-To:From;\n\tb=QS7NycRnXV26UfaftANPXXwgM3t4nPekVlyvHjHLPS9NhWTzI1oFVLLzMN/tvRp/W\n\t/mlvEu4KKkUcB+fy4bkJO+f2CRfDUayMWp1eqVLI3PygncG7lMzc7oN8C5V/XTS6eC\n\teyWq3K1ki0QwXfLthPp9L4HjMjjFbu9JO1Fevi5o=","To":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20210116114805.905397-1-niklas.soderlund@ragnatech.se>\n\t<20210116114805.905397-2-niklas.soderlund@ragnatech.se>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Autocrypt":"addr=kieran.bingham@ideasonboard.com; keydata=\n\t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\n\tWZw01QYHU/GUV/zHJSFk","Organization":"Ideas on Board","Message-ID":"<c96e0732-f560-1d35-7e79-97a499572b69@ideasonboard.com>","Date":"Wed, 27 Jan 2021 17:39:11 +0000","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101\n\tThunderbird/68.10.0","MIME-Version":"1.0","In-Reply-To":"<20210116114805.905397-2-niklas.soderlund@ragnatech.se>","Content-Language":"en-GB","Subject":"Re: [libcamera-devel] [PATCH v5 1/8] libcamera: delayed_controls:\n\tAdd helper for controls that apply with a delay","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","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>","Reply-To":"kieran.bingham@ideasonboard.com","Content-Type":"text/plain; charset=\"utf-8\"","Content-Transfer-Encoding":"base64","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":14854,"web_url":"https://patchwork.libcamera.org/comment/14854/","msgid":"<YBQb2B++u62Mp16M@oden.dyn.berto.se>","date":"2021-01-29T14:29:44","subject":"Re: [libcamera-devel] [PATCH v5 1/8] libcamera: delayed_controls:\n\tAdd helper for controls that apply with a delay","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Hi Kieran,\n\nThanks for your feedback.\n\nOn 2021-01-27 17:39:11 +0000, Kieran Bingham wrote:\n> Hi Niklas,\n> \n> On 16/01/2021 11:47, Niklas Söderlund wrote:\n> > Some sensor controls take effect with a delay as the sensor needs time\n> > to adjust, for example exposure. Add an optional helper DelayedControls\n> > to help pipelines deal with such controls.\n> > \n> > The idea is to provide a queue of controls towards the V4L2 device and\n> > apply individual controls with the specified delay with the aim to get\n> > predictable and retrievable control values for any given frame. To do\n> > this the queue of controls needs to be at least as deep as the control\n> > with the largest delay.\n> > \n> > The DelayedControls needs to be informed of every start of exposure.\n> > This can be emulated but the helper is designed to be used with this\n> > event being provide by the kernel through V4L2 events.\n> > \n> > This helper is based on StaggeredCtrl from the Raspberry Pi pipeline\n> > handler but expands on its API. This helpers aims to replace the\n> > Raspberry Pi implementations and mimics it behavior perfectly.\n> > \n> > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> > Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> > Reviewed-by: Naushir Patuck <naush@raspberrypi.com>\n> > ---\n> > * Changes since v4\n> > - Fold queue() into push() as the mutex was dropped in v3 having a\n> >   public accessors that takes the mutex is no longer needed.\n> > - Add delayed_controls.h to meson.build.\n> > - Update patch subject and commit message.\n> > - Have class Info inherit from ControlValue.\n> > - Add todo to reevaluate if maps should be indexed on ControlID or\n> >   unsigned int.\n> > - Update documentation.\n> > \n> > * Changes since v3\n> > - Drop optional argument to reset().\n> > - Update commit message.\n> > - Remove usage of Mutex.\n> > - Rename frameStart() to applyControls)_.\n> > - Rename ControlInfo to Into.\n> > - Rename ControlArray to ControlRingBuffer.\n> > - Drop ControlsDelays and ControlsValues.\n> > - Sort headers.\n> > - Rename iterators.\n> > - Simplify queueCount_ handeling in reset().\n> > - Add more warnings.\n> > - Update documentation.\n> > \n> > * Changes since v2\n> > - Improve error logic in queue() as suggested by Jean-Michel Hautbois.\n> > - s/fistSequence_/firstSequence_/\n> > \n> > * Changes since v1\n> > - Correct copyright to reflect work is derived from Raspberry Pi\n> >   pipeline handler. This was always the intention and was wrong in v1.\n> > - Rewrite large parts of the documentation.\n> > - Join two loops to one in DelayedControls::DelayedControls()\n> > ---\n> >  include/libcamera/internal/delayed_controls.h |  81 ++++++\n> >  include/libcamera/internal/meson.build        |   1 +\n> >  src/libcamera/delayed_controls.cpp            | 247 ++++++++++++++++++\n> >  src/libcamera/meson.build                     |   1 +\n> >  4 files changed, 330 insertions(+)\n> >  create mode 100644 include/libcamera/internal/delayed_controls.h\n> >  create mode 100644 src/libcamera/delayed_controls.cpp\n> > \n> > diff --git a/include/libcamera/internal/delayed_controls.h b/include/libcamera/internal/delayed_controls.h\n> > new file mode 100644\n> > index 0000000000000000..dc447a882514182f\n> > --- /dev/null\n> > +++ b/include/libcamera/internal/delayed_controls.h\n> > @@ -0,0 +1,81 @@\n> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > +/*\n> > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd.\n> > + *\n> > + * delayed_controls.h - Helper to deal with controls that take effect with a delay\n> > + */\n> > +#ifndef __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__\n> > +#define __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__\n> > +\n> > +#include <stdint.h>\n> > +#include <unordered_map>\n> > +\n> > +#include <libcamera/controls.h>\n> > +\n> > +namespace libcamera {\n> > +\n> > +class V4L2Device;\n> > +\n> > +class DelayedControls\n> > +{\n> > +public:\n> > +\tDelayedControls(V4L2Device *device,\n> > +\t\t\tconst std::unordered_map<uint32_t, unsigned int> &delays);\n> > +\n> > +\tvoid reset();\n> > +\n> > +\tbool push(const ControlList &controls);\n> > +\tControlList get(uint32_t sequence);\n> > +\n> > +\tvoid applyControls(uint32_t sequence);\n> > +\n> > +private:\n> > +\tclass Info : public ControlValue\n> > +\t{\n> > +\tpublic:\n> > +\t\tInfo()\n> > +\t\t\t: updated(false)\n> > +\t\t{\n> > +\t\t}\n> > +\n> > +\t\tInfo(const ControlValue &v)\n> > +\t\t\t: ControlValue(v), updated(true)\n> > +\t\t{\n> > +\t\t}\n> > +\n> > +\t\tbool updated;\n> > +\t};\n> > +\n> > +\t/* \\todo: Make the listSize configurable at instance creation time. */\n> > +\tstatic constexpr int listSize = 16;\n> > +\tclass ControlRingBuffer : public std::array<Info, listSize>\n> > +\t{\n> > +\tpublic:\n> > +\t\tInfo &operator[](unsigned int index)\n> > +\t\t{\n> > +\t\t\treturn std::array<Info, listSize>::operator[](index % listSize);\n> > +\t\t}\n> > +\n> > +\t\tconst Info &operator[](unsigned int index) const\n> > +\t\t{\n> > +\t\t\treturn std::array<Info, listSize>::operator[](index % listSize);\n> > +\t\t}\n> > +\t};\n> > +\n> > +\tV4L2Device *device_;\n> > +\t/* \\todo Evaluate if we should index on ControlId * or unsigned int */\n> > +\tstd::unordered_map<const ControlId *, unsigned int> delays_;\n> > +\tunsigned int maxDelay_;\n> > +\n> > +\tbool running_;\n> > +\tuint32_t firstSequence_;\n> > +\n> > +\tuint32_t queueCount_;\n> > +\tuint32_t writeCount_;\n> > +\t/* \\todo Evaluate if we should index on ControlId * or unsigned int */\n> > +\tstd::unordered_map<const ControlId *, ControlRingBuffer> values_;\n> \n> Oh, so we have a ring buffer for every control. That sort of surprises\n> me a little.\n> \n> Would a ring buffer of ControlLists make sense? Or are they more\n> difficult to instantiate? (That wouldn't surprise me ;D)\n> \n\nI think you answer this yourself at the end ;-)\n\n> \n> > +};\n> > +\n> > +} /* namespace libcamera */\n> > +\n> > +#endif /* __LIBCAMERA_INTERNAL_DELAYED_CONTROLS_H__ */\n> > diff --git a/include/libcamera/internal/meson.build b/include/libcamera/internal/meson.build\n> > index 1b1bdc77951235a5..e67a359fb8656f71 100644\n> > --- a/include/libcamera/internal/meson.build\n> > +++ b/include/libcamera/internal/meson.build\n> > @@ -17,6 +17,7 @@ libcamera_internal_headers = files([\n> >      'camera_sensor.h',\n> >      'control_serializer.h',\n> >      'control_validator.h',\n> > +    'delayed_controls.h',\n> >      'device_enumerator.h',\n> >      'device_enumerator_sysfs.h',\n> >      'device_enumerator_udev.h',\n> > diff --git a/src/libcamera/delayed_controls.cpp b/src/libcamera/delayed_controls.cpp\n> > new file mode 100644\n> > index 0000000000000000..f24db43d7f1e357d\n> > --- /dev/null\n> > +++ b/src/libcamera/delayed_controls.cpp\n> > @@ -0,0 +1,247 @@\n> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > +/*\n> > + * Copyright (C) 2020, Raspberry Pi (Trading) Ltd.\n> > + *\n> > + * delayed_controls.h - Helper to deal with controls that take effect with a delay\n> > + */\n> > +\n> > +#include \"libcamera/internal/delayed_controls.h\"\n> > +\n> > +#include <libcamera/controls.h>\n> > +\n> > +#include \"libcamera/internal/log.h\"\n> > +#include \"libcamera/internal/v4l2_device.h\"\n> > +\n> > +/**\n> > + * \\file delayed_controls.h\n> > + * \\brief Helper to deal with controls that take effect with a delay\n> > + */\n> > +\n> > +namespace libcamera {\n> > +\n> > +LOG_DEFINE_CATEGORY(DelayedControls)\n> > +\n> > +/**\n> > + * \\class DelayedControls\n> > + * \\brief Helper to deal with controls that take effect with a delay\n> > + *\n> > + * Some sensor controls take effect with a delay as the sensor needs time to\n> > + * adjust, for example exposure and analog gain. This is a helper class to deal\n> > + * with such controls and the intended users are pipeline handlers.\n> > + *\n> > + * The idea is to extend the concept of the buffer depth of a pipeline the\n> > + * application needs to maintain to also cover controls. Just as with buffer\n> > + * depth if the application keeps the number of requests queued above the\n> > + * control depth the controls are guaranteed to take effect for the correct\n> > + * request. The control depth is determined by the control with the greatest\n> > + * delay.\n> > + */\n> > +\n> > +/**\n> > + * \\brief Construct a DelayedControls instance\n> > + * \\param[in] device The V4L2 device the controls have to be applied to\n> > + * \\param[in] delays Map of the numerical V4L2 control ids to their associated\n> > + * delays (in frames)\n> > + *\n> > + * Only controls specified in \\a delays are handled. If it's desired to mix\n> > + * delayed controls and controls that take effect immediately the immediate\n> > + * controls must be listed in the \\a delays map with a delay value of 0.\n> > + */\n> > +DelayedControls::DelayedControls(V4L2Device *device,\n> > +\t\t\t\t const std::unordered_map<uint32_t, unsigned int> &delays)\n> > +\t: device_(device), maxDelay_(0)\n> > +{\n> > +\tconst ControlInfoMap &controls = device_->controls();\n> > +\n> > +\t/*\n> > +\t * Create a map of control ids to delays for controls exposed by the\n> > +\t * device.\n> > +\t */\n> > +\tfor (auto const &delay : delays) {\n> > +\t\tauto it = controls.find(delay.first);\n> > +\t\tif (it == controls.end()) {\n> > +\t\t\tLOG(DelayedControls, Error)\n> > +\t\t\t\t<< \"Delay request for control id \"\n> > +\t\t\t\t<< utils::hex(delay.first)\n> > +\t\t\t\t<< \" but control is not exposed by device \"\n> > +\t\t\t\t<< device_->deviceNode();\n> > +\t\t\tcontinue;\n> > +\t\t}\n> > +\n> > +\t\tconst ControlId *id = it->first;\n> > +\n> > +\t\tdelays_[id] = delay.second;\n> > +\n> > +\t\tLOG(DelayedControls, Debug)\n> > +\t\t\t<< \"Set a delay of \" << delays_[id]\n> > +\t\t\t<< \" for \" << id->name();\n> > +\n> > +\t\tmaxDelay_ = std::max(maxDelay_, delays_[id]);\n> > +\t}\n> > +\n> > +\treset();\n> > +}\n> > +\n> > +/**\n> > + * \\brief Reset state machine\n> > + *\n> > + * Resets the state machine to a starting position based on control values\n> > + * retrieved from the device.\n> > + */\n> > +void DelayedControls::reset()\n> > +{\n> > +\trunning_ = false;\n> > +\tfirstSequence_ = 0;\n> > +\tqueueCount_ = 1;\n> > +\twriteCount_ = 0;\n> > +\n> > +\t/* Retrieve control as reported by the device. */\n> > +\tstd::vector<uint32_t> ids;\n> > +\tfor (auto const &delay : delays_)\n> > +\t\tids.push_back(delay.first->id());\n> > +\n> > +\tControlList controls = device_->getControls(ids);\n> > +\n> > +\t/* Seed the control queue with the controls reported by the device. */\n> > +\tvalues_.clear();\n> > +\tfor (const auto &ctrl : controls) {\n> > +\t\tconst ControlId *id = device_->controls().idmap().at(ctrl.first);\n> > +\t\tvalues_[id][0] = Info(ctrl.second);\n> > +\t}\n> > +}\n> > +\n> > +/**\n> > + * \\brief Push a set of controls on the queue\n> > + * \\param[in] controls List of controls to add to the device queue\n> > + *\n> > + * Push a set of controls to the control queue. This increases the control queue\n> > + * depth by one.\n> > + *\n> > + * \\returns true if \\a controls are accepted, or false otherwise\n> > + */\n> > +bool DelayedControls::push(const ControlList &controls)\n> > +{\n> > +\t/* Copy state from previous frame. */\n> > +\tfor (auto &ctrl : values_) {\n> > +\t\tInfo &info = ctrl.second[queueCount_];\n> > +\t\tinfo = values_[ctrl.first][queueCount_ - 1];\n> > +\t\tinfo.updated = false;\n> > +\t}\n> > +\n> > +\t/* Update with new controls. */\n> > +\tconst ControlIdMap &idmap = device_->controls().idmap();\n> > +\tfor (const auto &control : controls) {\n> > +\t\tconst auto &it = idmap.find(control.first);\n> > +\t\tif (it == idmap.end()) {\n> > +\t\t\tLOG(DelayedControls, Warning)\n> > +\t\t\t\t<< \"Unknown control \" << control.first;\n> > +\t\t\treturn false;\n> > +\t\t}\n> > +\n> > +\t\tconst ControlId *id = it->second;\n> > +\n> > +\t\tif (delays_.find(id) == delays_.end())\n> > +\t\t\treturn false;\n> > +\n> > +\t\tInfo &info = values_[id][queueCount_];\n> > +\n> > +\t\tinfo = control.second;\n> > +\t\tinfo.updated = true;\n> > +\n> > +\t\tLOG(DelayedControls, Debug)\n> > +\t\t\t<< \"Queuing \" << id->name()\n> > +\t\t\t<< \" to \" << info.toString()\n> > +\t\t\t<< \" at index \" << queueCount_;\n> > +\t}\n> > +\n> > +\tqueueCount_++;\n> > +\n> > +\treturn true;\n> > +}\n> > +\n> > +/**\n> > + * \\brief Read back controls in effect at a sequence number\n> > + * \\param[in] sequence The sequence number to get controls for\n> > + *\n> > + * Read back what controls where in effect at a specific sequence number. The\n> > + * history is a ring buffer of 16 entries where new and old values coexist. It's\n> > + * the callers responsibility to not read too old sequence numbers that have been\n> > + * pushed out of the history.\n> > + *\n> > + * Historic values are evicted by pushing new values onto the queue using\n> > + * push(). The max history from the current sequence number that yields valid\n> > + * values are thus 16 minus number of controls pushed.\n> > + *\n> > + * \\return The controls at \\a sequence number\n> > + */\n> > +ControlList DelayedControls::get(uint32_t sequence)\n> > +{\n> > +\tuint32_t adjustedSeq = sequence - firstSequence_ + 1;\n> > +\tunsigned int index = std::max<int>(0, adjustedSeq - maxDelay_);\n> > +\n> > +\tControlList out(device_->controls());\n> > +\tfor (const auto &ctrl : values_) {\n> \n> I think I was a bit surprised to see that we have a ring per control, so\n> I now see that this function is taking all the controls in parallel ring\n> buffers of a specific sequence and generating a list from them.\n> \n> That makes me think even more that the ControlList could be the item on\n> the RingBuffer in the first place - but the list would have to be reset\n> when 'removed' from the Ring I expect.\n> \n> Perhaps that's as 'simple' as a std::move() operation though.\n> \n> \n> \n> > +\t\tconst ControlId *id = ctrl.first;\n> > +\t\tconst Info &info = ctrl.second[index];\n> > +\n> > +\t\tout.set(id->id(), info);\n> > +\n> > +\t\tLOG(DelayedControls, Debug)\n> > +\t\t\t<< \"Reading \" << id->name()\n> > +\t\t\t<< \" to \" << info.toString()\n> > +\t\t\t<< \" at index \" << index;\n> > +\t}\n> > +\n> > +\treturn out;\n> > +}\n> > +\n> > +/**\n> > + * \\brief Inform DelayedControls of the start of a new frame\n> > + * \\param[in] sequence Sequence number of the frame that started\n> > + *\n> > + * Inform the state machine that a new frame has started and of its sequence\n> > + * number. Any user of these helpers is responsible to inform the helper about\n> > + * the start of any frame.This can be connected with ease to the start of a\n> \n> s/frame.This/frame. This/\n> \n> > + * exposure (SOE) V4L2 event.\n> \n> This doesn't feel very clear to me.\n> \n> This function 'writes' controls to the device, and\n\nand ?\n\n> \n> > + */\n> > +void DelayedControls::applyControls(uint32_t sequence)\n> > +{\n> \n> If this is occurring as a callback from the SOE v4L2 event, should there\n> be locking in here to protect variables such as writeCount_ or queueCount_ ?\n\nThere where locking for this in earlier versions but after review it was \njudged not needed, at least not yet.\n\n> \n> \n> > +\tLOG(DelayedControls, Debug) << \"frame \" << sequence << \" started\";\n> > +\n> > +\tif (!running_) {\n> > +\t\tfirstSequence_ = sequence;\n> > +\t\trunning_ = true;\n> > +\t}\n> > +\n> > +\t/*\n> > +\t * Create control list peaking ahead in the value queue to ensure\n> \n> s/peaking/peeking/ ?\n\nThanks.\n\n> \n> \n> > +\t * values are set in time to satisfy the sensor delay.\n> > +\t */\n> > +\tControlList out(device_->controls());\n> > +\tfor (const auto &ctrl : values_) {\n> > +\t\tconst ControlId *id = ctrl.first;\n> > +\t\tunsigned int delayDiff = maxDelay_ - delays_[id];\n> \n> Aha - actually - seeing that each control has different delays probably\n> changes my mind about storing a full ControlList in the ring buffer....\n\nI'm sure we will rework this design a few times as we learn more.\n\n> \n> \n> \n> > +\t\tunsigned int index = std::max<int>(0, writeCount_ - delayDiff);\n> > +\t\tconst Info &info = ctrl.second[index];\n> > +\n> > +\t\tif (info.updated) {\n> > +\t\t\tout.set(id->id(), info);\n> > +\t\t\tLOG(DelayedControls, Debug)\n> > +\t\t\t\t<< \"Setting \" << id->name()\n> > +\t\t\t\t<< \" to \" << info.toString()\n> > +\t\t\t\t<< \" at index \" << index;\n> > +\t\t}\n> > +\t}\n> > +\n> > +\twriteCount_++;\n> > +\n> > +\twhile (writeCount_ >= queueCount_) {\n> > +\t\tLOG(DelayedControls, Debug)\n> > +\t\t\t<< \"Queue is empty, auto queue no-op.\";\n> > +\t\tpush({});\n> > +\t}\n> > +\n> > +\tdevice_->setControls(&out);\n> > +}\n> > +\n> > +} /* namespace libcamera */\n> > diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\n> > index 387d5d88ecae11ad..5a4bf0d7ba4fd231 100644\n> > --- a/src/libcamera/meson.build\n> > +++ b/src/libcamera/meson.build\n> > @@ -12,6 +12,7 @@ libcamera_sources = files([\n> >      'controls.cpp',\n> >      'control_serializer.cpp',\n> >      'control_validator.cpp',\n> > +    'delayed_controls.cpp',\n> >      'device_enumerator.cpp',\n> >      'device_enumerator_sysfs.cpp',\n> >      'event_dispatcher.cpp',\n> > \n> \n> -- \n> Regards\n> --\n> Kieran","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id D76FDC33BB\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 29 Jan 2021 14:29:49 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 386BC683AD;\n\tFri, 29 Jan 2021 15:29:49 +0100 (CET)","from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com\n\t[IPv6:2a00:1450:4864:20::22a])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D7A14683A2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 29 Jan 2021 15:29:46 +0100 (CET)","by mail-lj1-x22a.google.com with SMTP id f2so10657227ljp.11\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 29 Jan 2021 06:29:46 -0800 (PST)","from localhost (h-209-203.A463.priv.bahnhof.se. [155.4.209.203])\n\tby smtp.gmail.com with ESMTPSA id\n\ta6sm2441886ljd.62.2021.01.29.06.29.44\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 29 Jan 2021 06:29:44 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=ragnatech-se.20150623.gappssmtp.com\n\theader.i=@ragnatech-se.20150623.gappssmtp.com\n\theader.b=\"w+qH9xv/\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to;\n\tbh=cffKEA6o9f/2eD5GsC1iqg4i0pVZEyc9sdFROFpZLeA=;\n\tb=w+qH9xv/gDeUoSP7YOiXZkWppuZLAPjXxbKYKQjEXdgWLc23HkQRYradpfk5stQ0bZ\n\txuQaRmqZopCTffQfmuUHfuO0OCZkY6GdUNAt5Ga3ihthOS7bIhu2nIU3g3LQNbheRwUu\n\tCeLNhIijuNLjFn6VkqMoOua1qVv95py9Y1HYxiVxv94G7fo1Vo/TusBHcGIhGVr6do8P\n\txDWK/fi2WdTy8gDZYhOdWIk3GBIrJF0SjwItqaq4h+aKqbMgBCQD8urzYKHU2uK8K1bV\n\tFk3WUqy3Q9Te4cE6fW0cCLPCXP45C+eY9jCEwH6kC6mdW2rJ5oQCLZut76oH3LalYS9i\n\tHsLQ==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to;\n\tbh=cffKEA6o9f/2eD5GsC1iqg4i0pVZEyc9sdFROFpZLeA=;\n\tb=ZDafbHNr/3iuUBbV2jqyD8n69akEvEZeHx6VWB0Ml47UECJKhV14ZJQjjtlAjXW2uP\n\t2Gn4g7nDl207XrzOMAzZ9FKKu/nwnBd7hulDrG5pmBi8Ixpw3coL6IeQVZtspG144UgB\n\tQPOq/E/wYU4qUayLe5Hm+Me6oC4XV8c2VeIbaXEETPGRxhWKDExDujTWi4qFI5MdKEY8\n\tm7x9yml7bNmk3dQro2bj75w2zWC1omtzu7EK6PAEdYZRILflZmM1C+RhR3JzGuXILv9U\n\tImmo02SlBFbq84oOeUSAVPZBySuhSNM55hcVfs5XpoH08Y8/dXo2pL4qh4vh6ddA5xL5\n\tM1jQ==","X-Gm-Message-State":"AOAM530TBQW1bG9i0OFgdvl+rEVwisjmQxt4LyTQDJQrfOVaB4tEvlxM\n\tLhze03NDWw7YgLdFEq2zad2venxZQrljaw==","X-Google-Smtp-Source":"ABdhPJzxCDs8Tc0nzXqAbSTq32eI5EWIktLlHjevIYsKiAiX4Af4yl1tHSpshNwJOWP9mW6j3nM9hA==","X-Received":"by 2002:a2e:3614:: with SMTP id\n\td20mr2483729lja.429.1611930585951; \n\tFri, 29 Jan 2021 06:29:45 -0800 (PST)","Date":"Fri, 29 Jan 2021 15:29:44 +0100","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<YBQb2B++u62Mp16M@oden.dyn.berto.se>","References":"<20210116114805.905397-1-niklas.soderlund@ragnatech.se>\n\t<20210116114805.905397-2-niklas.soderlund@ragnatech.se>\n\t<c96e0732-f560-1d35-7e79-97a499572b69@ideasonboard.com>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<c96e0732-f560-1d35-7e79-97a499572b69@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v5 1/8] libcamera: delayed_controls:\n\tAdd helper for controls that apply with a delay","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","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>","Cc":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"iso-8859-1\"","Content-Transfer-Encoding":"quoted-printable","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]