{"id":2148,"url":"https://patchwork.libcamera.org/api/1.1/patches/2148/?format=json","web_url":"https://patchwork.libcamera.org/patch/2148/","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":"<20191008004534.1585142-11-niklas.soderlund@ragnatech.se>","date":"2019-10-08T00:45:34","name":"[libcamera-devel,v5,10/10] libcamera: pipeline: rkisp1: Attach to an IPA","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"49ff0a649d402c53862c4586419769c119e77ce9","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/2148/mbox/","series":[{"id":527,"url":"https://patchwork.libcamera.org/api/1.1/series/527/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=527","date":"2019-10-08T00:45:24","name":"libcamera: ipa: Add basic IPA support","version":5,"mbox":"https://patchwork.libcamera.org/series/527/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2148/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2148/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net\n\t[195.74.38.228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id DE41061968\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Oct 2019 02:46:50 +0200 (CEST)","from bismarck.berto.se (unknown [84.172.88.101])\n\tby bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA\n\tid f889d882-e964-11e9-837a-0050569116f7;\n\tTue, 08 Oct 2019 02:45:48 +0200 (CEST)"],"X-Halon-ID":"f889d882-e964-11e9-837a-0050569116f7","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":"Tue,  8 Oct 2019 02:45:34 +0200","Message-Id":"<20191008004534.1585142-11-niklas.soderlund@ragnatech.se>","X-Mailer":"git-send-email 2.23.0","In-Reply-To":"<20191008004534.1585142-1-niklas.soderlund@ragnatech.se>","References":"<20191008004534.1585142-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v5 10/10] libcamera: pipeline: rkisp1:\n\tAttach to an IPA","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":"Tue, 08 Oct 2019 00:46:51 -0000"},"content":"Add the plumbing to the pipeline handler to interact with an IPA module.\nThis change makes the usage of an IPA module mandatory for the rkisp1\npipeline.\n\nThe RkISP1 pipeline handler makes use of a timeline component to\nschedule actions. This might be useful for other pipeline handlers going\nforward so keep the generic timeline implementation separate to make it\neasy to break out.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n src/libcamera/pipeline/rkisp1/meson.build     |   2 +\n .../pipeline/rkisp1/rkisp1-timeline.cpp       |  51 +++\n src/libcamera/pipeline/rkisp1/rkisp1.cpp      | 328 +++++++++++++++++-\n src/libcamera/pipeline/rkisp1/rkisp1.h        |  78 +++++\n src/libcamera/pipeline/rkisp1/timeline.cpp    | 229 ++++++++++++\n src/libcamera/pipeline/rkisp1/timeline.h      |  72 ++++\n 6 files changed, 747 insertions(+), 13 deletions(-)\n create mode 100644 src/libcamera/pipeline/rkisp1/rkisp1-timeline.cpp\n create mode 100644 src/libcamera/pipeline/rkisp1/rkisp1.h\n create mode 100644 src/libcamera/pipeline/rkisp1/timeline.cpp\n create mode 100644 src/libcamera/pipeline/rkisp1/timeline.h","diff":"diff --git a/src/libcamera/pipeline/rkisp1/meson.build b/src/libcamera/pipeline/rkisp1/meson.build\nindex f1cc4046b5d064cb..7791e209175d95a9 100644\n--- a/src/libcamera/pipeline/rkisp1/meson.build\n+++ b/src/libcamera/pipeline/rkisp1/meson.build\n@@ -1,3 +1,5 @@\n libcamera_sources += files([\n     'rkisp1.cpp',\n+    'rkisp1-timeline.cpp',\n+    'timeline.cpp',\n ])\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1-timeline.cpp b/src/libcamera/pipeline/rkisp1/rkisp1-timeline.cpp\nnew file mode 100644\nindex 0000000000000000..e6c3f2cc9a09a571\n--- /dev/null\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1-timeline.cpp\n@@ -0,0 +1,51 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * rkisp1-timeline.cpp - Timeline handler for Rockchip ISP1\n+ */\n+\n+#include \"rkisp1.h\"\n+\n+#include \"log.h\"\n+\n+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(RkISP1)\n+\n+void RkISP1ActionSetSensor::run()\n+{\n+\tsensor_->setControls(&controls_);\n+}\n+\n+void RkISP1ActionQueueBuffer::run()\n+{\n+\tint ret = device_->queueBuffer(buffer_);\n+\tif (ret < 0)\n+\t\tLOG(RkISP1, Error) << \"Failed to queue buffer\";\n+}\n+\n+void RkISP1Timeline::bufferReady(Buffer *buffer)\n+{\n+\t/*\n+\t * Calculate SOE by taking the end of DMA set by the kernel and applying\n+\t * the time offsets provideprovided by the IPA to find the best estimate\n+\t * of SOE.\n+\t */\n+\n+\tASSERT(frameOffset(SOE) == 0);\n+\n+\tutils::time_point soe = std::chrono::time_point<utils::clock>()\n+\t\t+ std::chrono::nanoseconds(buffer->timestamp())\n+\t\t+ timeOffset(SOE);\n+\n+\tnotifyStartOfExposure(buffer->sequence(), soe);\n+}\n+\n+void RkISP1Timeline::setDelay(unsigned int type, int frame, int msdelay)\n+{\n+\tutils::duration delay = std::chrono::milliseconds(msdelay);\n+\tsetRawDelay(type, frame, delay);\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\nindex de4ab523d0e4fe36..74dfc8ca670ffa26 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n@@ -5,20 +5,22 @@\n  * rkisp1.cpp - Pipeline handler for Rockchip ISP1\n  */\n \n+#include \"rkisp1.h\"\n+\n #include <algorithm>\n #include <array>\n #include <iomanip>\n #include <memory>\n-#include <vector>\n \n #include <linux/media-bus-format.h>\n \n #include <libcamera/camera.h>\n+#include <libcamera/control_ids.h>\n #include <libcamera/request.h>\n #include <libcamera/stream.h>\n \n-#include \"camera_sensor.h\"\n #include \"device_enumerator.h\"\n+#include \"ipa_manager.h\"\n #include \"log.h\"\n #include \"media_device.h\"\n #include \"pipeline_handler.h\"\n@@ -34,7 +36,7 @@ class RkISP1CameraData : public CameraData\n {\n public:\n \tRkISP1CameraData(PipelineHandler *pipe)\n-\t\t: CameraData(pipe), sensor_(nullptr)\n+\t\t: CameraData(pipe), sensor_(nullptr), frame_(0)\n \t{\n \t}\n \n@@ -43,8 +45,21 @@ public:\n \t\tdelete sensor_;\n \t}\n \n+\tint loadIPA();\n+\n \tStream stream_;\n \tCameraSensor *sensor_;\n+\tunsigned int frame_;\n+\tstd::vector<IPABuffer> ipaBuffers_;\n+\tstd::map<unsigned int, Request *> frameInfo_;\n+\tRkISP1Timeline timeline_;\n+\n+private:\n+\tvoid queueFrameAction(const IPAOperationData &action);\n+\n+\tvoid queueBuffer(unsigned int frame, unsigned int type,\n+\t\t\t unsigned int id);\n+\tvoid metadataReady(unsigned int frame, unsigned int aeState);\n };\n \n class RkISP1CameraConfiguration : public CameraConfiguration\n@@ -99,18 +114,116 @@ private:\n \t\t\tPipelineHandler::cameraData(camera));\n \t}\n \n+\tfriend RkISP1CameraData;\n+\n \tint initLinks();\n \tint createCamera(MediaEntity *sensor);\n+\tvoid tryCompleteRequest(Request *request);\n \tvoid bufferReady(Buffer *buffer);\n+\tvoid paramReady(Buffer *buffer);\n+\tvoid statReady(Buffer *buffer);\n \n \tMediaDevice *media_;\n \tV4L2Subdevice *dphy_;\n \tV4L2Subdevice *isp_;\n \tV4L2VideoDevice *video_;\n+\tV4L2VideoDevice *param_;\n+\tV4L2VideoDevice *stat_;\n+\n+\tBufferPool paramPool_;\n+\tBufferPool statPool_;\n+\n+\tstd::map<unsigned int, Buffer *> paramBuffers_;\n+\tstd::map<unsigned int, Buffer *> statBuffers_;\n \n \tCamera *activeCamera_;\n };\n \n+int RkISP1CameraData::loadIPA()\n+{\n+\tipa_ = IPAManager::instance()->createIPA(pipe_, 1, 1);\n+\tif (!ipa_)\n+\t\treturn -ENOENT;\n+\n+\tipa_->queueFrameAction.connect(this,\n+\t\t\t\t       &RkISP1CameraData::queueFrameAction);\n+\n+\treturn 0;\n+}\n+\n+void RkISP1CameraData::queueFrameAction(const IPAOperationData &action)\n+{\n+\tswitch (action.operation) {\n+\tcase RKISP1_IPA_ACTION_V4L2_SET: {\n+\t\tunsigned int frame = action.data[0];\n+\t\tV4L2ControlList controls = action.v4l2controls[0];\n+\t\ttimeline_.scheduleAction(utils::make_unique<RkISP1ActionSetSensor>(frame,\n+\t\t\t\t\t\t\t\t\t\t   sensor_,\n+\t\t\t\t\t\t\t\t\t\t   controls));\n+\t\tbreak;\n+\t}\n+\tcase RKISP1_IPA_ACTION_QUEUE_BUFFER: {\n+\t\tunsigned int frame = action.data[0];\n+\t\tunsigned int type = action.data[1];\n+\t\tunsigned int id = action.data[2];\n+\t\tqueueBuffer(frame, type, id);\n+\t\tbreak;\n+\t}\n+\tcase RKISP1_IPA_ACTION_METADATA: {\n+\t\tunsigned int frame = action.data[0];\n+\t\tunsigned aeState = action.data[1];\n+\t\tmetadataReady(frame, aeState);\n+\t\tbreak;\n+\t}\n+\tdefault:\n+\t\tLOG(RkISP1, Error) << \"Unkown action \" << action.operation;\n+\t\tbreak;\n+\t}\n+}\n+\n+void RkISP1CameraData::queueBuffer(unsigned int frame, unsigned int type,\n+\t\t\t\t   unsigned int id)\n+{\n+\tPipelineHandlerRkISP1 *pipe =\n+\t\tstatic_cast<PipelineHandlerRkISP1 *>(pipe_);\n+\n+\tRkISP1ActionType acttype;\n+\tV4L2VideoDevice *device;\n+\tBuffer *buffer;\n+\tswitch (type) {\n+\tcase RKISP1_BUFFER_PARAM:\n+\t\tacttype = QueueParameters;\n+\t\tdevice = pipe->param_;\n+\t\tbuffer = pipe->paramBuffers_[id];\n+\t\tbreak;\n+\tcase RKISP1_BUFFER_STAT:\n+\t\tacttype = QueueStatistics;\n+\t\tdevice = pipe->stat_;\n+\t\tbuffer = pipe->statBuffers_[id];\n+\t\tbreak;\n+\tdefault:\n+\t\tLOG(RkISP1, Error) << \"Unkown IPA buffer type \" << type;\n+\t\treturn;\n+\t}\n+\n+\ttimeline_.scheduleAction(utils::make_unique<RkISP1ActionQueueBuffer>(frame,\n+\t\t\t\t\t\t\t\t\t     acttype,\n+\t\t\t\t\t\t\t\t\t     device,\n+\t\t\t\t\t\t\t\t\t     buffer));\n+}\n+\n+void RkISP1CameraData::metadataReady(unsigned int frame, unsigned int aeStatus)\n+{\n+\tRequest *request = frameInfo_[frame];\n+\tPipelineHandlerRkISP1 *pipe =\n+\t\tstatic_cast<PipelineHandlerRkISP1 *>(pipe_);\n+\n+\tif (aeStatus)\n+\t\trequest->metadata().set<bool>(controls::AeLocked, aeStatus == 2);\n+\n+\tpipe->tryCompleteRequest(request);\n+}\n+\n RkISP1CameraConfiguration::RkISP1CameraConfiguration(Camera *camera,\n \t\t\t\t\t\t     RkISP1CameraData *data)\n \t: CameraConfiguration()\n@@ -202,12 +315,14 @@ CameraConfiguration::Status RkISP1CameraConfiguration::validate()\n \n PipelineHandlerRkISP1::PipelineHandlerRkISP1(CameraManager *manager)\n \t: PipelineHandler(manager), dphy_(nullptr), isp_(nullptr),\n-\t  video_(nullptr)\n+\t  video_(nullptr), param_(nullptr), stat_(nullptr)\n {\n }\n \n PipelineHandlerRkISP1::~PipelineHandlerRkISP1()\n {\n+\tdelete param_;\n+\tdelete stat_;\n \tdelete video_;\n \tdelete isp_;\n \tdelete dphy_;\n@@ -317,6 +432,20 @@ int PipelineHandlerRkISP1::configure(Camera *camera, CameraConfiguration *c)\n \tif (ret)\n \t\treturn ret;\n \n+\tV4L2DeviceFormat paramFormat = {};\n+\tparamFormat.fourcc = V4L2_META_FMT_RK_ISP1_PARAMS;\n+\n+\tret = param_->setFormat(&paramFormat);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tV4L2DeviceFormat statFormat = {};\n+\tstatFormat.fourcc = V4L2_META_FMT_RK_ISP1_STAT_3A;\n+\n+\tret = stat_->setFormat(&statFormat);\n+\tif (ret)\n+\t\treturn ret;\n+\n \tif (outputFormat.size != cfg.size ||\n \t    outputFormat.fourcc != cfg.pixelFormat) {\n \t\tLOG(RkISP1, Error)\n@@ -332,39 +461,133 @@ int PipelineHandlerRkISP1::configure(Camera *camera, CameraConfiguration *c)\n int PipelineHandlerRkISP1::allocateBuffers(Camera *camera,\n \t\t\t\t\t   const std::set<Stream *> &streams)\n {\n+\tRkISP1CameraData *data = cameraData(camera);\n \tStream *stream = *streams.begin();\n+\tint ret;\n \n \tif (stream->memoryType() == InternalMemory)\n-\t\treturn video_->exportBuffers(&stream->bufferPool());\n+\t\tret = video_->exportBuffers(&stream->bufferPool());\n \telse\n-\t\treturn video_->importBuffers(&stream->bufferPool());\n+\t\tret = video_->importBuffers(&stream->bufferPool());\n+\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tparamPool_.createBuffers(stream->configuration().bufferCount + 1);\n+\tret = param_->exportBuffers(&paramPool_);\n+\tif (ret) {\n+\t\tvideo_->releaseBuffers();\n+\t\treturn ret;\n+\t}\n+\n+\tstatPool_.createBuffers(stream->configuration().bufferCount + 1);\n+\tret = stat_->exportBuffers(&statPool_);\n+\tif (ret) {\n+\t\tparam_->releaseBuffers();\n+\t\tvideo_->releaseBuffers();\n+\t\treturn ret;\n+\t}\n+\n+\tfor (unsigned int i = 0; i < stream->configuration().bufferCount + 1; i++) {\n+\t\tparamBuffers_[i] = new Buffer(i);\n+\t\tstatBuffers_[i] = new Buffer(i);\n+\n+\t\tdata->ipaBuffers_.push_back({\n+\t\t\t.type = RKISP1_BUFFER_PARAM,\n+\t\t\t.id = i,\n+\t\t\t.buffer = paramPool_.buffers()[i],\n+\t\t});\n+\t\tdata->ipaBuffers_.push_back({\n+\t\t\t.type = RKISP1_BUFFER_STAT,\n+\t\t\t.id = i,\n+\t\t\t.buffer = statPool_.buffers()[i],\n+\t\t});\n+\t}\n+\n+\tdata->ipa_->mapBuffers(data->ipaBuffers_);\n+\n+\treturn ret;\n }\n \n int PipelineHandlerRkISP1::freeBuffers(Camera *camera,\n \t\t\t\t       const std::set<Stream *> &streams)\n {\n+\tRkISP1CameraData *data = cameraData(camera);\n+\n+\tdata->ipa_->unmapBuffers(data->ipaBuffers_);\n+\tdata->ipaBuffers_.clear();\n+\n+\tfor (auto it : paramBuffers_)\n+\t\tdelete it.second;\n+\n+\tparamBuffers_.clear();\n+\n+\tfor (auto it : statBuffers_)\n+\t\tdelete it.second;\n+\n+\tstatBuffers_.clear();\n+\n+\tif (param_->releaseBuffers())\n+\t\tLOG(RkISP1, Error) << \"Failed to release parameters buffers\";\n+\n+\tif (stat_->releaseBuffers())\n+\t\tLOG(RkISP1, Error) << \"Failed to release stat buffers\";\n+\n \tif (video_->releaseBuffers())\n-\t\tLOG(RkISP1, Error) << \"Failed to release buffers\";\n+\t\tLOG(RkISP1, Error) << \"Failed to release video buffers\";\n \n \treturn 0;\n }\n \n int PipelineHandlerRkISP1::start(Camera *camera)\n {\n+\tRkISP1CameraData *data = cameraData(camera);\n \tint ret;\n \n+\tret = param_->streamOn();\n+\tif (ret) {\n+\t\tLOG(RkISP1, Error)\n+\t\t\t<< \"Failed to start parameters \" << camera->name();\n+\t\treturn ret;\n+\t}\n+\n+\tret = stat_->streamOn();\n+\tif (ret) {\n+\t\tparam_->streamOff();\n+\t\tLOG(RkISP1, Error)\n+\t\t\t<< \"Failed to start statistics \" << camera->name();\n+\t\treturn ret;\n+\t}\n+\n \tret = video_->streamOn();\n-\tif (ret)\n+\tif (ret) {\n+\t\tparam_->streamOff();\n+\t\tstat_->streamOff();\n+\n \t\tLOG(RkISP1, Error)\n \t\t\t<< \"Failed to start camera \" << camera->name();\n+\t}\n \n \tactiveCamera_ = camera;\n \n+\t/* Inform IPA of stream configuration and sensor controls. */\n+\tstd::map<unsigned int, IPAStream> streamConfig;\n+\tstreamConfig[0] = {\n+\t\t.pixelFormat = data->stream_.configuration().pixelFormat,\n+\t\t.size = data->stream_.configuration().size,\n+\t};\n+\n+\tstd::map<unsigned int, V4L2ControlInfoMap> entityControls;\n+\tentityControls[0] = data->sensor_->controls();\n+\n+\tdata->ipa_->configure(streamConfig, entityControls);\n+\n \treturn ret;\n }\n \n void PipelineHandlerRkISP1::stop(Camera *camera)\n {\n+\tRkISP1CameraData *data = cameraData(camera);\n \tint ret;\n \n \tret = video_->streamOff();\n@@ -372,6 +595,18 @@ void PipelineHandlerRkISP1::stop(Camera *camera)\n \t\tLOG(RkISP1, Warning)\n \t\t\t<< \"Failed to stop camera \" << camera->name();\n \n+\tret = stat_->streamOff();\n+\tif (ret)\n+\t\tLOG(RkISP1, Warning)\n+\t\t\t<< \"Failed to stop statistics \" << camera->name();\n+\n+\tret = param_->streamOff();\n+\tif (ret)\n+\t\tLOG(RkISP1, Warning)\n+\t\t\t<< \"Failed to stop parameters \" << camera->name();\n+\n+\tdata->timeline_.reset();\n+\n \tactiveCamera_ = nullptr;\n }\n \n@@ -387,12 +622,22 @@ int PipelineHandlerRkISP1::queueRequest(Camera *camera, Request *request)\n \t\treturn -ENOENT;\n \t}\n \n-\tint ret = video_->queueBuffer(buffer);\n-\tif (ret < 0)\n-\t\treturn ret;\n-\n \tPipelineHandler::queueRequest(camera, request);\n \n+\tdata->frameInfo_[data->frame_] = request;\n+\n+\tIPAOperationData op;\n+\top.operation = RKISP1_IPA_EVENT_QUEUE_REQUEST;\n+\top.data = { data->frame_ };\n+\top.controls = { request->controls() };\n+\tdata->ipa_->processEvent(op);\n+\n+\tdata->timeline_.scheduleAction(utils::make_unique<RkISP1ActionQueueBuffer>(data->frame_,\n+\t\t\t\t\t\t\t\t\t\t   QueueVideo,\n+\t\t\t\t\t\t\t\t\t\t   video_,\n+\t\t\t\t\t\t\t\t\t\t   buffer));\n+\tdata->frame_++;\n+\n \treturn 0;\n }\n \n@@ -435,11 +680,19 @@ int PipelineHandlerRkISP1::createCamera(MediaEntity *sensor)\n \tstd::unique_ptr<RkISP1CameraData> data =\n \t\tutils::make_unique<RkISP1CameraData>(this);\n \n+\tdata->controlInfo_.emplace(std::piecewise_construct,\n+\t\t\t\t   std::forward_as_tuple(&controls::AeEnable),\n+\t\t\t\t   std::forward_as_tuple(false, true));\n+\n \tdata->sensor_ = new CameraSensor(sensor);\n \tret = data->sensor_->init();\n \tif (ret)\n \t\treturn ret;\n \n+\tret = data->loadIPA();\n+\tif (ret)\n+\t\treturn ret;\n+\n \tstd::set<Stream *> streams{ &data->stream_ };\n \tstd::shared_ptr<Camera> camera =\n \t\tCamera::create(this, sensor->name(), streams);\n@@ -478,7 +731,17 @@ bool PipelineHandlerRkISP1::match(DeviceEnumerator *enumerator)\n \tif (video_->open() < 0)\n \t\treturn false;\n \n+\tstat_ = V4L2VideoDevice::fromEntityName(media_, \"rkisp1-statistics\");\n+\tif (stat_->open() < 0)\n+\t\treturn false;\n+\n+\tparam_ = V4L2VideoDevice::fromEntityName(media_, \"rkisp1-input-params\");\n+\tif (param_->open() < 0)\n+\t\treturn false;\n+\n \tvideo_->bufferReady.connect(this, &PipelineHandlerRkISP1::bufferReady);\n+\tstat_->bufferReady.connect(this, &PipelineHandlerRkISP1::statReady);\n+\tparam_->bufferReady.connect(this, &PipelineHandlerRkISP1::paramReady);\n \n \t/* Configure default links. */\n \tif (initLinks() < 0) {\n@@ -504,13 +767,52 @@ bool PipelineHandlerRkISP1::match(DeviceEnumerator *enumerator)\n  * Buffer Handling\n  */\n \n+void PipelineHandlerRkISP1::tryCompleteRequest(Request *request)\n+{\n+\tif (request->hasPendingBuffers())\n+\t\treturn;\n+\n+\tif (request->metadata().empty())\n+\t\treturn;\n+\n+\tcompleteRequest(activeCamera_, request);\n+}\n+\n void PipelineHandlerRkISP1::bufferReady(Buffer *buffer)\n {\n \tASSERT(activeCamera_);\n+\tRkISP1CameraData *data = cameraData(activeCamera_);\n \tRequest *request = buffer->request();\n \n+\tdata->timeline_.bufferReady(buffer);\n+\n+\tif (data->frame_ <= buffer->sequence())\n+\t\tdata->frame_ = buffer->sequence() + 1;\n+\n \tcompleteBuffer(activeCamera_, request, buffer);\n-\tcompleteRequest(activeCamera_, request);\n+\ttryCompleteRequest(request);\n+}\n+\n+void PipelineHandlerRkISP1::paramReady(Buffer *buffer)\n+{\n+\tASSERT(activeCamera_);\n+\tRkISP1CameraData *data = cameraData(activeCamera_);\n+\n+\tIPAOperationData op;\n+\top.operation = RKISP1_IPA_EVENT_SIGNAL_BUFFER;\n+\top.data = { RKISP1_BUFFER_PARAM, buffer->index() };\n+\tdata->ipa_->processEvent(op);\n+}\n+\n+void PipelineHandlerRkISP1::statReady(Buffer *buffer)\n+{\n+\tASSERT(activeCamera_);\n+\tRkISP1CameraData *data = cameraData(activeCamera_);\n+\n+\tIPAOperationData op;\n+\top.operation = RKISP1_IPA_EVENT_SIGNAL_BUFFER;\n+\top.data = { RKISP1_BUFFER_STAT, buffer->index() };\n+\tdata->ipa_->processEvent(op);\n }\n \n REGISTER_PIPELINE_HANDLER(PipelineHandlerRkISP1);\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1.h b/src/libcamera/pipeline/rkisp1/rkisp1.h\nnew file mode 100644\nindex 0000000000000000..f9ea3a822cb1d0f3\n--- /dev/null\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1.h\n@@ -0,0 +1,78 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * rkisp1.h - Pipeline handler for Rockchip ISP1\n+ */\n+#ifndef __LIBCAMERA_RKISP1_H__\n+#define __LIBCAMERA_RKISP1_H__\n+\n+#include <ipa/rkisp1.h>\n+#include <libcamera/buffer.h>\n+\n+#include \"camera_sensor.h\"\n+#include \"timeline.h\"\n+#include \"v4l2_videodevice.h\"\n+\n+namespace libcamera {\n+\n+enum RkISP1ActionType {\n+\tSetSensor,\n+\tSOE,\n+\tQueueVideo,\n+\tQueueParameters,\n+\tQueueStatistics,\n+};\n+\n+class RkISP1ActionSetSensor : public FrameAction\n+{\n+public:\n+\tRkISP1ActionSetSensor(unsigned int frame, CameraSensor *sensor, V4L2ControlList controls)\n+\t\t: FrameAction(frame, SetSensor), sensor_(sensor), controls_(controls) {}\n+\n+protected:\n+\tvoid run() override;\n+\n+private:\n+\tCameraSensor *sensor_;\n+\tV4L2ControlList controls_;\n+};\n+\n+class RkISP1ActionQueueBuffer : public FrameAction\n+{\n+public:\n+\tRkISP1ActionQueueBuffer(unsigned int frame, RkISP1ActionType type,\n+\t\t\t\tV4L2VideoDevice *device, Buffer *buffer)\n+\t\t: FrameAction(frame, type), device_(device), buffer_(buffer)\n+\t{\n+\t}\n+\n+protected:\n+\tvoid run() override;\n+\n+private:\n+\tV4L2VideoDevice *device_;\n+\tBuffer *buffer_;\n+};\n+\n+class RkISP1Timeline : public Timeline\n+{\n+public:\n+\tRkISP1Timeline()\n+\t\t: Timeline()\n+\t{\n+\t\tsetDelay(SetSensor, -1, 5);\n+\t\tsetDelay(SOE, 0, -1);\n+\t\tsetDelay(QueueVideo, -1, 10);\n+\t\tsetDelay(QueueParameters, -1, 8);\n+\t\tsetDelay(QueueStatistics, -1, 8);\n+\t}\n+\n+\tvoid bufferReady(Buffer *buffer);\n+\n+\tvoid setDelay(unsigned int type, int frame, int msdelay);\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_RKISP1_H__ */\ndiff --git a/src/libcamera/pipeline/rkisp1/timeline.cpp b/src/libcamera/pipeline/rkisp1/timeline.cpp\nnew file mode 100644\nindex 0000000000000000..95ec6b7ca0a0237a\n--- /dev/null\n+++ b/src/libcamera/pipeline/rkisp1/timeline.cpp\n@@ -0,0 +1,229 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * timeline.cpp - Timeline for per-frame control\n+ */\n+\n+#include \"timeline.h\"\n+\n+#include \"log.h\"\n+\n+/**\n+ * \\file timeline.h\n+ * \\brief Timeline for per-frame control\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(Timeline)\n+\n+/**\n+ * \\class FrameAction\n+ * \\brief Action that can be schedule on a Timeline\n+ *\n+ * A frame action is an event that can be schedule to be executed at a point\n+ * in time. A frame action has two primal attributes: frame number and type.\n+ *\n+ * The frame number describes the frame for which the action should have an\n+ * effect. The type is a numerical ID which identifies the action within the\n+ * pipeline and IPA protocol.\n+ *\n+ * \\sa Timeline\n+ */\n+\n+/**\n+ * \\class Timeline\n+ * \\brief Executor of FrameActions\n+ *\n+ * The timeline have three primary functions.\n+ *\n+ * 1. Keep track of the Start of Exposure (SOE) for every frame processed by\n+ *    the hardware. Using this information it shall keep an up-to-date estimate\n+ *    of the frame interval (time between two SOEs).\n+ *\n+ *    The estimated frame interval together with recorded SOE events is the\n+ *    foundation for how the timeline schedule FrameActions at specific points\n+ *    in time.\n+ *    \\todo Improve the frame interval estimation algorithm.\n+ *\n+ * 2. Keep track of current delays for different types of actions. The delays\n+ *    for different actions might differ during a capture session. Exposure time\n+ *    effects the over all FPS and different ISP parameters might impacts its\n+ *    processing time.\n+ *\n+ *    The action type delays shall be updated by the IPA in conjunction with\n+ *    how it changes the capture parameters.\n+ *\n+ * 3. Schedule actions on the timeline. This is the process of taking a\n+ *    FrameAction which contains an abstract description of what frame and\n+ *    what type of action it contains and turning that into an time point\n+ *    and make sure the action is executed at that time.\n+ */\n+\n+Timeline::Timeline()\n+\t: frameInterval_(0)\n+{\n+\ttimer_.timeout.connect(this, &Timeline::timeout);\n+}\n+\n+/**\n+ * \\brief Reset and stop the timeline\n+ *\n+ * The timeline needs to be reset when the timeline should no longer execute\n+ * actions. A timeline should be reset between two capture sessions to prevent\n+ * the old capture session to effect the second one.\n+ */\n+void Timeline::reset()\n+{\n+\ttimer_.stop();\n+\n+\tactions_.clear();\n+\thistory_.clear();\n+}\n+\n+/**\n+ * \\brief Schedule action on the timeline\n+ * \\param[in] action FrameAction to schedule\n+ *\n+ * The act of scheduling an action to the timeline is the process of taking\n+ * the properties of the action (type, frame and time offsets) and translating\n+ * that to a time point using the current values for the action type timings\n+ * value recorded in the timeline. If an action is scheduled too late, execute\n+ * it immediately.\n+ */\n+void Timeline::scheduleAction(std::unique_ptr<FrameAction> action)\n+{\n+\tunsigned int lastFrame;\n+\tutils::time_point lastTime;\n+\n+\tif (history_.empty()) {\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 when the action shall be schedule by first finding out how\n+\t * many frames in the future the action acts on and then add the actions\n+\t * frame offset. After the spatial frame offset is found out translate\n+\t * that to a time point by using the last estimated start of exposure\n+\t * (SOE) as the fixed offset. Lastly add the actions time offset to the\n+\t * time point.\n+\t */\n+\tint frame = action->frame() - lastFrame + frameOffset(action->type());\n+\tutils::time_point deadline = lastTime + frame * frameInterval_\n+\t\t+ 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 scheduled 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, std::move(action) });\n+\t\tupdateDeadline();\n+\t}\n+}\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() <= HISTORY_DEPTH / 2)\n+\t\treturn;\n+\n+\twhile (history_.size() > HISTORY_DEPTH)\n+\t\thistory_.pop_front();\n+\n+\t/* Update esitmated time between two start of exposures. */\n+\tutils::duration sumExposures(0);\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+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+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+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()\n+{\n+\tif (actions_.empty())\n+\t\treturn;\n+\n+\tconst utils::time_point &deadline = actions_.begin()->first;\n+\n+\tif (timer_.isRunning() && deadline >= timer_.deadline())\n+\t\treturn;\n+\n+\tif (deadline <= std::chrono::steady_clock::now()) {\n+\t\ttimeout(&timer_);\n+\t\treturn;\n+\t}\n+\n+\ttimer_.start(deadline);\n+}\n+\n+void Timeline::timeout(Timer *timer)\n+{\n+\tutils::time_point now = std::chrono::steady_clock::now();\n+\n+\tfor (auto it = actions_.begin(); it != actions_.end();) {\n+\t\tconst utils::time_point &sched = it->first;\n+\n+\t\tif (sched > now)\n+\t\t\tbreak;\n+\n+\t\tFrameAction *action = it->second.get();\n+\n+\t\taction->run();\n+\n+\t\tit = actions_.erase(it);\n+\t}\n+\n+\tupdateDeadline();\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/pipeline/rkisp1/timeline.h b/src/libcamera/pipeline/rkisp1/timeline.h\nnew file mode 100644\nindex 0000000000000000..9d30e4eaf8743d07\n--- /dev/null\n+++ b/src/libcamera/pipeline/rkisp1/timeline.h\n@@ -0,0 +1,72 @@\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 frame, unsigned int type)\n+\t\t: frame_(frame), type_(type) {}\n+\n+\tvirtual ~FrameAction() {}\n+\n+\tunsigned int frame() const { return frame_; }\n+\tunsigned int type() const { return type_; }\n+\n+\tvirtual void run() = 0;\n+\n+private:\n+\tunsigned int frame_;\n+\tunsigned int type_;\n+};\n+\n+class Timeline\n+{\n+public:\n+\tTimeline();\n+\tvirtual ~Timeline() {}\n+\n+\tvirtual void reset();\n+\tvirtual void scheduleAction(std::unique_ptr<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+\tstd::map<unsigned int, std::pair<int, utils::duration>> delays_;\n+\n+private:\n+\tstatic constexpr unsigned int HISTORY_DEPTH = 10;\n+\n+\tvoid timeout(Timer *timer);\n+\tvoid updateDeadline();\n+\n+\tstd::list<std::pair<unsigned int, utils::time_point>> history_;\n+\tstd::multimap<utils::time_point, std::unique_ptr<FrameAction>> actions_;\n+\tutils::duration frameInterval_;\n+\n+\tTimer timer_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_TIMELINE_H__ */\n","prefixes":["libcamera-devel","v5","10/10"]}