{"id":11977,"url":"https://patchwork.libcamera.org/api/patches/11977/?format=json","web_url":"https://patchwork.libcamera.org/patch/11977/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20210418090459.38506-5-naush@raspberrypi.com>","date":"2021-04-18T09:04:59","name":"[libcamera-devel,v3,4/4] ipa: raspberrypi: Rate-limit the controller algorithms","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"bace97c7370db5f6b37097c7262c35aec88a00a8","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/11977/mbox/","series":[{"id":1944,"url":"https://patchwork.libcamera.org/api/series/1944/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1944","date":"2021-04-18T09:04:55","name":"ipa: raspberrypi: Rate-limit the controller algorithms","version":3,"mbox":"https://patchwork.libcamera.org/series/1944/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/11977/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11977/checks/","tags":{},"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 9A27BBD812\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 18 Apr 2021 09:05:11 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 553E86881E;\n\tSun, 18 Apr 2021 11:05:11 +0200 (CEST)","from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com\n\t[IPv6:2a00:1450:4864:20::52c])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 07DEF68819\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 18 Apr 2021 11:05:08 +0200 (CEST)","by mail-ed1-x52c.google.com with SMTP id h10so37104895edt.13\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 18 Apr 2021 02:05:08 -0700 (PDT)","from naush-laptop.patuck.local ([88.97.76.4])\n\tby smtp.gmail.com with ESMTPSA id\n\tz4sm9080679edb.97.2021.04.18.02.05.06\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tSun, 18 Apr 2021 02:05:07 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"NrHLbc5C\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=4RcEdwi1QWwOUa0feax3IKgOFFbXuYpYgXBgd3TfJCg=;\n\tb=NrHLbc5Cm4ekk026S9iQoP3F09BqwMtqr/BXSqJh9C00wpFwJhyMsi2AIRz5U9QvbT\n\tIohvKqF9azDeFuZ7dPa0nVXLZxveb6fSs4FlUm9LUBrlOYjeq8Gs0j4MFCzg0uGmCBHu\n\tTfD62ExqYUeRRTggcGPrvo8nR4gYbOSg3C+JxFDrAL0metNcXsQUPid9y0tvHyA4gAyU\n\trpOuE6dIqPWlBqd4jPj5ayQ+VELUjOFelYHZUuCYlS0CX0DFzBqywLQIMR32YyzqrFto\n\tKlhqrHvKY2gwSQHL/m5VCg0lxMTgRZ2kpDzFpOCF93jigifRcEAlKGyy+qqQYoQdLuIi\n\t629Q==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=4RcEdwi1QWwOUa0feax3IKgOFFbXuYpYgXBgd3TfJCg=;\n\tb=fvKSWa61cA9vzwJ7Xqevh/x0eC1MQU4B9kDXVfMlqZi9OxNuXJ6GpYzZQmRN3fngA1\n\tcIdxGrXCHT90VEXaMWbYJXYSdydBqvyhc/7qVSEF5AhBC0gIASgD0fCZd2ywInWTVldP\n\tHYeoSShc8NtfYWc0jgAhNKpTcf1L6dtgH7BEO0vLGP7ZRkZ8EHCllR8j9r/yqt6WxcCF\n\tivNllIu2AHJOlgMpnvMHbi3edOrthM4El2+31REXIg9/fjnDFxjVjbjimOMkWcnHIOSB\n\t9a2uVRz94zmju3Tbh8ZRYtiA7jDkoNcvF+sllDmDfzqAQvLbIcszIf72jgC6sVl+mFE0\n\tHgNQ==","X-Gm-Message-State":"AOAM533PItv/4LmD5a8d6igYVOVvQHEhd0eUO7JXBVdpgnEyWHIV+tEd\n\tGsvPvOL1qcm5qCrkq7Tpk7o0+w+GVGqpaA==","X-Google-Smtp-Source":"ABdhPJxkGiFO1zu8svX4heIVSJNzngSRNSGHGwI4/rlYrA2eKC1WmYoFIvYSubdMuM12Jw1CvEP2Hg==","X-Received":"by 2002:aa7:c2c8:: with SMTP id m8mr5273522edp.86.1618736707361; \n\tSun, 18 Apr 2021 02:05:07 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sun, 18 Apr 2021 10:04:59 +0100","Message-Id":"<20210418090459.38506-5-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20210418090459.38506-1-naush@raspberrypi.com>","References":"<20210418090459.38506-1-naush@raspberrypi.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v3 4/4] ipa: raspberrypi: Rate-limit the\n\tcontroller algorithms","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The controller algorithms currently run on every frame provided to the\nIPA by the pipeline handler. This may be undesirable for very fast fps\noperating modes where it could significantly increase the computation\ncycles (per unit time) without providing any significant changes to the\nIQ parameters. The added latencies could also cause dropped frames.\n\nPass the FrameBuffer timestamp to the IPA through the controls. This\ntimestamp will be used to rate-limit the controller algorithms to run\nwith a minimum inter-frame time given by a compile time constant,\ncurrently set to 16.66ms. On startup, we don't rate-limit the algorithms\nuntil after the number of frames required for convergence.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\nReviewed-by: David Plowman <david.plowman@raspberrypi.com>\n---\n src/ipa/raspberrypi/raspberrypi.cpp           | 48 +++++++++++++++++--\n .../pipeline/raspberrypi/raspberrypi.cpp      |  5 ++\n 2 files changed, 49 insertions(+), 4 deletions(-)","diff":"diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\nindex f6d1ab16a290..a9b7bb1acb92 100644\n--- a/src/ipa/raspberrypi/raspberrypi.cpp\n+++ b/src/ipa/raspberrypi/raspberrypi.cpp\n@@ -61,6 +61,14 @@ constexpr unsigned int DefaultExposureTime = 20000;\n constexpr double defaultMinFrameDuration = 1e6 / 30.0;\n constexpr double defaultMaxFrameDuration = 1e6 / 0.01;\n \n+/*\n+ * Determine the minimum allowable inter-frame duration (in us) to run the\n+ * controller algorithms. If the pipeline handler provider frames at a rate\n+ * higher than this, we rate-limit the controller Prepare() and Process() calls\n+ * to lower than or equal to this rate.\n+ */\n+constexpr double controllerMinFrameDuration = 1e6 / 60.0;\n+\n LOG_DEFINE_CATEGORY(IPARPI)\n \n class IPARPi : public ipa::RPi::IPARPiInterface\n@@ -68,7 +76,7 @@ class IPARPi : public ipa::RPi::IPARPiInterface\n public:\n \tIPARPi()\n \t\t: controller_(), frameCount_(0), checkCount_(0), mistrustCount_(0),\n-\t\t  lsTable_(nullptr), firstStart_(true)\n+\t\t  lastRunTimestamp_(0), lsTable_(nullptr), firstStart_(true)\n \t{\n \t}\n \n@@ -146,6 +154,12 @@ private:\n \t/* Number of frames that need to be dropped on startup. */\n \tunsigned int dropFrameCount_;\n \n+\t/* Frame timestamp for the last run of the controller. */\n+\tuint64_t lastRunTimestamp_;\n+\n+\t/* Do we run a Controller::process() for this frame? */\n+\tbool processPending_;\n+\n \t/* LS table allocation passed in from the pipeline handler. */\n \tFileDescriptor lsTableHandle_;\n \tvoid *lsTable_;\n@@ -262,6 +276,7 @@ void IPARPi::start(const ControlList &controls, ipa::RPi::StartConfig *startConf\n \tstartConfig->dropFrameCount = dropFrameCount_;\n \n \tfirstStart_ = false;\n+\tlastRunTimestamp_ = 0;\n }\n \n void IPARPi::setMode(const CameraSensorInfo &sensorInfo)\n@@ -406,7 +421,7 @@ void IPARPi::signalStatReady(uint32_t bufferId)\n {\n \tif (++checkCount_ != frameCount_) /* assert here? */\n \t\tLOG(IPARPI, Error) << \"WARNING: Prepare/Process mismatch!!!\";\n-\tif (frameCount_ > mistrustCount_)\n+\tif (processPending_ && frameCount_ > mistrustCount_)\n \t\tprocessStats(bufferId);\n \n \treportMetadata();\n@@ -894,10 +909,11 @@ void IPARPi::returnEmbeddedBuffer(unsigned int bufferId)\n \n void IPARPi::prepareISP(const ipa::RPi::ISPConfig &data)\n {\n+\tint64_t frameTimestamp = data.controls.get(controls::draft::SensorTimestamp);\n+\tRPiController::Metadata lastMetadata;\n \tSpan<uint8_t> embeddedBuffer;\n \n-\trpiMetadata_.Clear();\n-\n+\tlastMetadata = std::move(rpiMetadata_);\n \tfillDeviceStatus(data.controls);\n \n \tif (data.embeddedBufferPresent) {\n@@ -920,6 +936,30 @@ void IPARPi::prepareISP(const ipa::RPi::ISPConfig &data)\n \tif (data.embeddedBufferPresent)\n \t\treturnEmbeddedBuffer(data.embeddedBufferId);\n \n+\tif (lastRunTimestamp_ && frameCount_ > dropFrameCount_ &&\n+\t    frameTimestamp - lastRunTimestamp_ < controllerMinFrameDuration * 1e3) {\n+\t\t/*\n+\t\t * Ensure we update the controller metadata with the new frame's\n+\t\t * exposure/gain values so that the correct values are returned\n+\t\t * out in libcamera metadata later on. All other metadata values\n+\t\t * must remain the same as the last frame.\n+\t\t */\n+\t\tDeviceStatus currentDeviceStatus;\n+\n+\t\trpiMetadata_.Get<DeviceStatus>(\"device.status\", currentDeviceStatus);\n+\t\trpiMetadata_ = std::move(lastMetadata);\n+\t\trpiMetadata_.Set(\"device.status\", currentDeviceStatus);\n+\t\tprocessPending_ = false;\n+\t\tLOG(IPARPI, Debug) << \"Rate-limiting the controller! inter-frame duration: \"\n+\t\t\t\t   << frameTimestamp - lastRunTimestamp_\n+\t\t\t\t   << \", min duration: \"\n+\t\t\t\t   << controllerMinFrameDuration * 1e3;\n+\t\treturn;\n+\t}\n+\n+\tlastRunTimestamp_ = frameTimestamp;\n+\tprocessPending_ = true;\n+\n \tControlList ctrls(ispCtrls_);\n \n \tcontroller_.Prepare(&rpiMetadata_);\ndiff --git a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\nindex 2a917455500f..9cf9c8c6cebd 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -1414,6 +1414,11 @@ void RPiCameraData::unicamBufferDequeue(FrameBuffer *buffer)\n \t\t * DelayedControl and queue them along with the frame buffer.\n \t\t */\n \t\tControlList ctrl = delayedCtrls_->get(buffer->metadata().sequence);\n+\t\t/*\n+\t\t * Add the frame timestamp to the ControlList for the IPA to use\n+\t\t * as it does not receive the FrameBuffer object.\n+\t\t */\n+\t\tctrl.set(controls::draft::SensorTimestamp, buffer->metadata().timestamp);\n \t\tbayerQueue_.push({ buffer, std::move(ctrl) });\n \t} else {\n \t\tembeddedQueue_.push(buffer);\n","prefixes":["libcamera-devel","v3","4/4"]}