{"id":2033,"url":"https://patchwork.libcamera.org/api/1.1/patches/2033/?format=json","web_url":"https://patchwork.libcamera.org/patch/2033/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190927024417.725906-10-niklas.soderlund@ragnatech.se>","date":"2019-09-27T02:44:13","name":"[libcamera-devel,v3,09/13] libcamera: timeline: Add a basic timeline implementation","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"dd938466a28ed5364b5d86798f4cec73932e709b","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/1.1/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2033/mbox/","series":[{"id":508,"url":"https://patchwork.libcamera.org/api/1.1/series/508/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=508","date":"2019-09-27T02:44:04","name":"libcamera: ipa: Add basic IPA support","version":3,"mbox":"https://patchwork.libcamera.org/series/508/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2033/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2033/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net\n\t[195.74.38.229])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C6C4761767\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 27 Sep 2019 04:45:32 +0200 (CEST)","from bismarck.berto.se (unknown [84.172.88.101])\n\tby bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA\n\tid d00a3bf4-e0d0-11e9-bdc3-005056917a89;\n\tFri, 27 Sep 2019 04:45:06 +0200 (CEST)"],"X-Halon-ID":"d00a3bf4-e0d0-11e9-bdc3-005056917a89","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 27 Sep 2019 04:44:13 +0200","Message-Id":"<20190927024417.725906-10-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.23.0","In-Reply-To":"<20190927024417.725906-1-niklas.soderlund@ragnatech.se>","References":"<20190927024417.725906-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 09/13] libcamera: timeline: Add a basic\n\ttimeline implementation","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>","X-List-Received-Date":"Fri, 27 Sep 2019 02:45:33 -0000"},"content":"The timeline is a helper for pipeline handlers to ease interacting with\nan IPA. The idea is that the pipeline handler runs a timeline which\nschedules and executes actions on the hardware. The pipeline listens to\nsignals from the IPA which it translates into actions which are schedule\non the timeline.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n src/libcamera/include/meson.build |   1 +\n src/libcamera/include/timeline.h  |  71 ++++++++\n src/libcamera/meson.build         |   1 +\n src/libcamera/timeline.cpp        | 267 ++++++++++++++++++++++++++++++\n 4 files changed, 340 insertions(+)\n create mode 100644 src/libcamera/include/timeline.h\n create mode 100644 src/libcamera/timeline.cpp","diff":"diff --git a/src/libcamera/include/meson.build b/src/libcamera/include/meson.build\nindex 933be8543a8d5f02..fc6402b6ffb3d47c 100644\n--- a/src/libcamera/include/meson.build\n+++ b/src/libcamera/include/meson.build\n@@ -16,6 +16,7 @@ libcamera_headers = files([\n     'pipeline_handler.h',\n     'process.h',\n     'thread.h',\n+    'timeline.h',\n     'utils.h',\n     'v4l2_controls.h',\n     'v4l2_device.h',\ndiff --git a/src/libcamera/include/timeline.h b/src/libcamera/include/timeline.h\nnew file mode 100644\nindex 0000000000000000..519caf5a923f35a7\n--- /dev/null\n+++ b/src/libcamera/include/timeline.h\n@@ -0,0 +1,71 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * timeline.h - Timeline for per-frame controls\n+ */\n+#ifndef __LIBCAMERA_TIMELINE_H__\n+#define __LIBCAMERA_TIMELINE_H__\n+\n+#include <list>\n+#include <map>\n+\n+#include <libcamera/timer.h>\n+\n+#include \"utils.h\"\n+\n+namespace libcamera {\n+\n+class FrameAction\n+{\n+public:\n+\tFrameAction(unsigned int type, unsigned int frame)\n+\t\t: type_(type), frame_(frame) {}\n+\n+\tvirtual ~FrameAction() {}\n+\n+\tunsigned int type() const { return type_; }\n+\tunsigned int frame() const { return frame_; }\n+\n+\tvirtual void run() = 0;\n+\n+private:\n+\tunsigned int type_;\n+\tunsigned int frame_;\n+};\n+\n+class Timeline\n+{\n+public:\n+\tTimeline();\n+\tvirtual ~Timeline() {}\n+\n+\tvirtual void reset();\n+\tvirtual void scheduleAction(FrameAction *action);\n+\tvirtual void notifyStartOfExposure(unsigned int frame, utils::time_point time);\n+\n+\tutils::duration frameInterval() const { return frameInterval_; }\n+\n+protected:\n+\tint frameOffset(unsigned int type) const;\n+\tutils::duration timeOffset(unsigned int type) const;\n+\n+\tvoid setRawDelay(unsigned int type, int frame, utils::duration time);\n+\n+private:\n+\tstatic constexpr unsigned int historyDepth = 10;\n+\n+\tvoid timeout(Timer *timer);\n+\tvoid updateDeadline(const utils::time_point &deadline);\n+\n+\tstd::list<std::pair<unsigned int, utils::time_point>> history_;\n+\tstd::multimap<utils::time_point, FrameAction *> actions_;\n+\tstd::map<unsigned int, std::pair<int, utils::duration>> delays_;\n+\tutils::duration frameInterval_;\n+\n+\tTimer timer_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_TIMELINE_H__ */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 755149c55c7b4f84..fef2e8619a42e053 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -28,6 +28,7 @@ libcamera_sources = files([\n     'signal.cpp',\n     'stream.cpp',\n     'thread.cpp',\n+    'timeline.cpp',\n     'timer.cpp',\n     'utils.cpp',\n     'v4l2_controls.cpp',\ndiff --git a/src/libcamera/timeline.cpp b/src/libcamera/timeline.cpp\nnew file mode 100644\nindex 0000000000000000..b5a713abbf3235eb\n--- /dev/null\n+++ b/src/libcamera/timeline.cpp\n@@ -0,0 +1,267 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * timeline.cpp - Timeline for per-frame controls\n+ */\n+\n+#include \"timeline.h\"\n+\n+#include \"log.h\"\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(Timeline)\n+\n+/**\n+ * \\class FrameAction\n+ * \\brief Action which can be schedule within a frames lifetime\n+ *\n+ * A frame action is a event which takes place at some point during a frames\n+ * lifetime inside libcamera. Each action have two primal attributes; type and\n+ * frame number.\n+ *\n+ * The type is a numerical ID which identifies the action within the pipeline\n+ * handler. The type is pipeline specific and have no meaning to anyone outside\n+ * the specific implementation. The frame number is the frame the action applies\n+ * to.\n+ */\n+\n+/**\n+ * \\fn FrameAction::FrameAction\n+ * \\brief Create a frame action\n+ * \\param[in] type Action type\n+ * \\param[in] frame Frame number\n+ */\n+\n+/**\n+ * \\fn FrameAction::type()\n+ * \\brief Retrieve the type of action\n+ * \\return Action type\n+ */\n+\n+/**\n+ * \\fn FrameAction::frame()\n+ * \\brief Retrieve the frame number\n+ * \\return Frame number\n+ */\n+\n+/**\n+ * \\fn FrameAction::run()\n+ * \\brief The action to perform when the action is triggered\n+ *\n+ * Pipeline handlers shall subclass the FrameAction object and implement run()\n+ * functions which describes the actions they wish to happen when the act is\n+ * schedule using the Timeline.\n+ *\n+ * \\sa Timeline\n+ */\n+\n+/**\n+ * \\class Timeline\n+ * \\brief Scheduler and executor of FrameAction's\n+ *\n+ * On the timeline the pipeline can schedule FrameActions and expect them to be\n+ * executed at the correct time. The correct time to execute them are a sum\n+ * of which frame the action should apply to and a list of timing delays for\n+ * each action the pipeline handler describes.\n+ *\n+ * The timing delays can either be set by the pipeline handler or the IPA. The\n+ * exact implementation of how the timing delays are setup are pipeline\n+ * specific. It is expected that the IPA will update the timing delays as it\n+ * make changes to how the pipeline operates in different situations.\n+ *\n+ * The pipeline handler is responsible for feeding the timeline as accurate as\n+ * possible information of the exposures it observes.\n+ */\n+\n+Timeline::Timeline()\n+\t: frameInterval_(std::chrono::milliseconds(1))\n+{\n+\ttimer_.timeout.connect(this, &Timeline::timeout);\n+}\n+\n+/**\n+ * \\brief Reset the timeline\n+ */\n+void Timeline::reset()\n+{\n+\ttimer_.stop();\n+\n+\tactions_.clear();\n+\thistory_.clear();\n+}\n+\n+/**\n+ * \\brief Add a action to the timeline\n+ * \\param[in] action FrameAction to add\n+ *\n+ * Schedule an action at a specific time taking the action and timing delays\n+ * into account. If a action is schedule too late execute it immediately and\n+ * try to recover.\n+ */\n+void Timeline::scheduleAction(FrameAction *action)\n+{\n+\tunsigned int lastFrame;\n+\tutils::time_point lastTime;\n+\n+\tif (history_.empty()) {\n+\t\t/*\n+\t\t * This only happens for the first number of frames, up to\n+\t\t * pipeline depth.\n+\t\t */\n+\t\tlastFrame = 0;\n+\t\tlastTime = std::chrono::steady_clock::now();\n+\t} else {\n+\t\tlastFrame = history_.back().first;\n+\t\tlastTime = history_.back().second;\n+\t}\n+\n+\t/*\n+\t * Calculate action trigger time by first figuring the out the start of\n+\t * exposure (SOE) of the frame the action corresponds to and then adding\n+\t * the frame and timing offsets.\n+\t */\n+\tint frame = action->frame() + frameOffset(action->type()) - lastFrame;\n+\tutils::time_point deadline = lastTime + frame * frameInterval_ + timeOffset(action->type());\n+\n+\tutils::time_point now = std::chrono::steady_clock::now();\n+\tif (deadline < now) {\n+\t\tLOG(Timeline, Warning)\n+\t\t\t<< \"Action schedule too late \"\n+\t\t\t<< utils::time_point_to_string(deadline)\n+\t\t\t<< \", run now \" << utils::time_point_to_string(now);\n+\t\taction->run();\n+\t} else {\n+\t\tactions_.insert({ deadline, action });\n+\t\tupdateDeadline(deadline);\n+\t}\n+}\n+\n+/**\n+ * \\brief Inform timeline of a new exposure\n+ * \\param[in] frame Frame number of the exposure\n+ * \\param[in] time The best approximation of when the exposure started\n+ */\n+void Timeline::notifyStartOfExposure(unsigned int frame, utils::time_point time)\n+{\n+\thistory_.push_back(std::make_pair(frame, time));\n+\n+\tif (history_.size() <= historyDepth / 2)\n+\t\treturn;\n+\n+\twhile (history_.size() > historyDepth)\n+\t\thistory_.pop_front();\n+\n+\t/* Update esitmated time between two start of exposures. */\n+\tutils::duration sumExposures = std::chrono::duration_values<std::chrono::nanoseconds>::zero();\n+\tunsigned int numExposures = 0;\n+\n+\tutils::time_point lastTime;\n+\tfor (auto it = history_.begin(); it != history_.end(); it++) {\n+\t\tif (it != history_.begin()) {\n+\t\t\tsumExposures += it->second - lastTime;\n+\t\t\tnumExposures++;\n+\t\t}\n+\n+\t\tlastTime = it->second;\n+\t}\n+\n+\tframeInterval_ = sumExposures;\n+\tif (numExposures)\n+\t\tframeInterval_ /= numExposures;\n+}\n+\n+/**\n+ * \\fn Timeline::frameInterval()\n+ * \\brief Retrieve the best estimate of the frame interval\n+ * \\return Frame interval estimate\n+ */\n+\n+/**\n+ * \\brief Retrieve the frame offset for an action type\n+ * \\param[in] type Action type\n+ * \\return Frame offset for the action type\n+ */\n+int Timeline::frameOffset(unsigned int type) const\n+{\n+\tconst auto it = delays_.find(type);\n+\tif (it == delays_.end()) {\n+\t\tLOG(Timeline, Error)\n+\t\t\t<< \"No frame offset set for action type \" << type;\n+\t\treturn 0;\n+\t}\n+\n+\treturn it->second.first;\n+}\n+\n+/**\n+ * \\brief Retrieve the time offset for an action type\n+ * \\param[in] type Action type\n+ * \\return Time offset for the action type\n+ */\n+utils::duration Timeline::timeOffset(unsigned int type) const\n+{\n+\tconst auto it = delays_.find(type);\n+\tif (it == delays_.end()) {\n+\t\tLOG(Timeline, Error)\n+\t\t\t<< \"No time offset set for action type \" << type;\n+\t\treturn utils::duration::zero();\n+\t}\n+\n+\treturn it->second.second;\n+}\n+\n+/**\n+ * \\brief Set the frame and time offset delays for an action type\n+ * \\param[in] type Action type\n+ * \\param[in] frame Frame offset\n+ * \\param[in] time Time offset\n+ */\n+void Timeline::setRawDelay(unsigned int type, int frame, utils::duration time)\n+{\n+\tdelays_[type] = std::make_pair(frame, time);\n+}\n+\n+void Timeline::updateDeadline(const utils::time_point &deadline)\n+{\n+\tif (timer_.isRunning() && deadline >= timer_.deadline())\n+\t\treturn;\n+\n+\tutils::time_point now = std::chrono::steady_clock::now();\n+\tutils::duration duration = deadline - now;\n+\n+\t/*\n+\t * Translate nanoseconds resolution the timeline to milliseconds using\n+\t * a ceiling approach to not trigger an action before it's scheduled.\n+\t *\n+\t * \\todo: Should the Timer operate using nanoseconds?\n+\t */\n+\tunsigned long nsecs = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();\n+\tunsigned int msecs = nsecs / 1000000 + (nsecs % 1000000 ? 1 : 0);\n+\n+\ttimer_.stop();\n+\ttimer_.start(msecs);\n+}\n+\n+void Timeline::timeout(Timer *timer)\n+{\n+\tfor (auto it = actions_.begin(); it != actions_.end();) {\n+\t\tutils::time_point now = std::chrono::steady_clock::now();\n+\n+\t\tutils::time_point sched = it->first;\n+\t\tFrameAction *action = it->second;\n+\n+\t\tif (sched >= now) {\n+\t\t\tupdateDeadline(sched);\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\taction->run();\n+\n+\t\tit = actions_.erase(it);\n+\t\tdelete action;\n+\t}\n+}\n+\n+} /* namespace libcamera */\n","prefixes":["libcamera-devel","v3","09/13"]}