{"id":12239,"url":"https://patchwork.libcamera.org/api/1.1/patches/12239/?format=json","web_url":"https://patchwork.libcamera.org/patch/12239/","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":"<20210510095814.3732400-7-naush@raspberrypi.com>","date":"2021-05-10T09:58:15","name":"[libcamera-devel,v6,6/6] ipa: raspberrypi: Rate-limit the controller algorithms","commit_ref":"99feb66df02988831c9e8535649b99fad9a78069","pull_url":null,"state":"accepted","archived":false,"hash":"aa221c6c26185a9c7a6a464b53eacc6137be98c4","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/1.1/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/12239/mbox/","series":[{"id":2015,"url":"https://patchwork.libcamera.org/api/1.1/series/2015/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2015","date":"2021-05-10T09:58:09","name":"ipa: raspberrypi: Rate-limit the controller algorithms","version":6,"mbox":"https://patchwork.libcamera.org/series/2015/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12239/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12239/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 2382DBF829\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 10 May 2021 09:59:14 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B47C668926;\n\tMon, 10 May 2021 11:59:13 +0200 (CEST)","from mail-wm1-x333.google.com (mail-wm1-x333.google.com\n\t[IPv6:2a00:1450:4864:20::333])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BC90D6890C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 10 May 2021 11:59:10 +0200 (CEST)","by mail-wm1-x333.google.com with SMTP id\n\ty124-20020a1c32820000b029010c93864955so10787448wmy.5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 10 May 2021 02:59:10 -0700 (PDT)","from naush-laptop.pitowers.org\n\t([2a00:1098:3142:14:e710:3ec:6dad:dc2])\n\tby smtp.gmail.com with ESMTPSA id\n\ty17sm24767917wrw.90.2021.05.10.02.59.09\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 10 May 2021 02:59:09 -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=\"inIbQJsR\"; 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=fmFRyvKG40VFNgvY7s5IVKLxkKqN5DwxSDI/eMitC4I=;\n\tb=inIbQJsRbdaz3bZNW+8Ay47vaaALuQDwvMCvX5cjzMjE9KpQAdCD7//rVc7AfQy7vJ\n\tAySjpD4vnMdoTLuHz7bRVxhbCrCThtjCcJ7LN8xzA/dKYlq7DuD6Rq9nor9+EL4u1jbO\n\toDx8ytdcZZIqq6fVEMGj5gacP6QT0+oAEv/4hmOgLkEnI4XfhDFE6p25toqQjv75WNJ7\n\tRmOMEVNZ7oFYY2V9FAkkCVaxYfS0ts9UZ8kzTLUft//HbR+Wu6Hx17WyXyIFaxhOcA5D\n\tj0vh/PJ+EgqN2bX1WEfMnsCBvWCjVLDIjwqSvvC7mmTE0HE09WM8CozxwW5sepCoO5fS\n\tbQIg==","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=fmFRyvKG40VFNgvY7s5IVKLxkKqN5DwxSDI/eMitC4I=;\n\tb=nNqx2JH/7NwB4AIFhIXNNG7TsQr5vzTa3j2e2nb49FRbf/ET66x/EEM0pFt7EdRhJd\n\teW0z8zVie3QGBxVazc644x+/RZe1fLCFRjFMzeXBYSRfZhNN79Tlw+2GN++wQqYmck3G\n\tX1ccXJdOqIcvQC8JaNnTco0WiSmyFOnMBE2//1GWRdjkmGBpV7AT7JADo5UCPwa6zoEk\n\tBauG2Z0/Vlk9l4fkk/wNsaKQyl53tLuAXmcJMMgbIT341qUErY6RrYc8jwX/5lOyT63Y\n\toH9QPvhN75TZFBxXmg9I+jfmjLrHi1NxK5Pm+sWNv+zGXd7TGz4z8cu0sT+RwFPJzXYz\n\tbkdw==","X-Gm-Message-State":"AOAM531+Ih6HC3dk1/wSO4DwvO5Dvk/pf22U1+T65VNf0Jh6kGUSeFgi\n\tms2Tx5+PTCFgAgBH5UM7+j/Xt9ekx/whJw==","X-Google-Smtp-Source":"ABdhPJzoCHHfMCK6qULSshNl970toyxie62p6nni30TannPChpFWn/k1JouwmEUB1+al+ftJyaTzXQ==","X-Received":"by 2002:a1c:7402:: with SMTP id p2mr35839969wmc.88.1620640750116;\n\tMon, 10 May 2021 02:59:10 -0700 (PDT)","From":"Naushir Patuck <naush@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 10 May 2021 10:58:15 +0100","Message-Id":"<20210510095814.3732400-7-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20210510095814.3732400-1-naush@raspberrypi.com>","References":"<20210510095814.3732400-1-naush@raspberrypi.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v6 6/6] 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>\n---\n src/ipa/raspberrypi/raspberrypi.cpp           | 42 +++++++++++++++++--\n .../pipeline/raspberrypi/raspberrypi.cpp      |  5 +++\n 2 files changed, 43 insertions(+), 4 deletions(-)","diff":"diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\nindex 07409621a03a..52d91db282ea 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::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,24 @@ void IPARPi::prepareISP(const ipa::RPi::ISPConfig &data)\n \tif (data.embeddedBufferPresent)\n \t\treturnEmbeddedBuffer(data.embeddedBufferId);\n \n+\t/* Allow a 10% margin on the comparison below. */\n+\tconstexpr double eps = controllerMinFrameDuration * 1e3 * 0.1;\n+\tif (lastRunTimestamp_ && frameCount_ > dropFrameCount_ &&\n+\t    frameTimestamp - lastRunTimestamp_ + eps < controllerMinFrameDuration * 1e3) {\n+\t\t/*\n+\t\t * Ensure we merge the previous frame's metadata with the current\n+\t\t * frame. This will not overwrite exposure/gain values for the\n+\t\t * current frame, or any other bits of metadata that were added\n+\t\t * in helper_->Prepare().\n+\t\t */\n+\t\trpiMetadata_.Merge(lastMetadata);\n+\t\tprocessPending_ = false;\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 b22564938cdc..d26ae2a9772a 100644\n--- a/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n+++ b/src/libcamera/pipeline/raspberrypi/raspberrypi.cpp\n@@ -1426,6 +1426,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::SensorTimestamp, buffer->metadata().timestamp);\n \t\tbayerQueue_.push({ buffer, std::move(ctrl) });\n \t} else {\n \t\tembeddedQueue_.push(buffer);\n","prefixes":["libcamera-devel","v6","6/6"]}